2 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
12 #include "hw_device.h"
14 #include "vbus_factory.h"
18 #include <l4/vbus/vbus_gpio-ops.h>
26 class Gpio : public Device, public Dev_feature
29 int dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios);
31 explicit Gpio(Hw::Device *d, Tagged_parameter *filter)
32 : _hwd(dynamic_cast<Hw::Gpio_chip*>(d)), _mask(0)
36 for (unsigned i = 0; i < Max_pins; ++i)
42 for (Expression *x = filter->get("pins"); x; x = x->next())
48 _mask |= (1UL << v.val.num);
51 _mask |= ~(~0UL << (v.val.range.e - v.val.range.s + 1)) << v.val.range.s;
54 d_printf(DBG_ERR, "ERROR: GPIO: Invalid value for 'pins' statement in config, ignored.\n");
60 char const *hid() const { return "GPIO"; }
61 void set_host(Device *d) { _host = d; }
62 Device *host() const { return _host; }
64 bool match_hw_feature(const Hw::Dev_feature*) const
68 enum { Max_pins = 32 };
75 int setup(L4::Ipc::Iostream &ios);
76 int config_pad(L4::Ipc::Iostream &ios);
77 int get(L4::Ipc::Iostream &ios);
78 int set(L4::Ipc::Iostream &ios);
79 int multi_setup(L4::Ipc::Iostream &ios);
80 int multi_config_pad(L4::Ipc::Iostream &ios);
81 int multi_get(L4::Ipc::Iostream &ios);
82 int multi_set(L4::Ipc::Iostream &ios);
83 int to_irq(L4::Ipc::Iostream &ios);
85 void check(unsigned pin)
90 if (!((1UL << pin) & _mask))
94 void check_mask(unsigned mask)
104 Gpio::setup(L4::Ipc::Iostream &ios)
108 ios >> pin >> mode >> value;
110 _hwd->setup(pin, mode, value);
115 Gpio::config_pad(L4::Ipc::Iostream &ios)
117 unsigned pin, func, value;
118 ios >> pin >> func >> value;
120 _hwd->config_pad(pin, _mask, func, value);
125 Gpio::get(L4::Ipc::Iostream &ios)
130 return _hwd->get(pin);
134 Gpio::set(L4::Ipc::Iostream &ios)
140 _hwd->set(pin, value);
145 Gpio::multi_setup(L4::Ipc::Iostream &ios)
147 unsigned mask, mode, outvalue;
148 ios >> mask >> mode >> outvalue;
150 _hwd->multi_setup(mask, mode, outvalue);
155 Gpio::multi_config_pad(L4::Ipc::Iostream &ios)
157 unsigned mask, func, value;
158 ios >> mask >> func >> value;
160 _hwd->multi_config_pad(mask, func, value);
165 Gpio::multi_get(L4::Ipc::Iostream &ios)
167 unsigned data = _hwd->multi_get();
168 ios << (unsigned)(data & _mask);
173 Gpio::multi_set(L4::Ipc::Iostream &ios)
178 _hwd->multi_set(mask, data);
183 Gpio::to_irq(L4::Ipc::Iostream &ios)
189 if (_irqs[pin] == -1)
191 // we have to allocate the IRQ...
192 // if it fails we mark the IRQ as unavailable (-L4_ENODEV)
193 _irqs[pin] = -L4_ENODEV;
195 int irqnum = _hwd->get_irq(pin);
199 if (System_bus *sb = dynamic_cast<System_bus *>(get_root()))
201 int virq = sb->sw_icu()->alloc_irq(Sw_icu::S_allow_set_mode,
202 Sw_icu::real_irq(irqnum));
215 Gpio::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios)
221 case L4VBUS_GPIO_OP_SETUP: return setup(ios);
222 case L4VBUS_GPIO_OP_CONFIG_PAD: return config_pad(ios);
223 case L4VBUS_GPIO_OP_GET: return get(ios);
224 case L4VBUS_GPIO_OP_SET: return set(ios);
225 case L4VBUS_GPIO_OP_MULTI_SETUP: return multi_setup(ios);
226 case L4VBUS_GPIO_OP_MULTI_CONFIG_PAD: return multi_config_pad(ios);
227 case L4VBUS_GPIO_OP_MULTI_GET: return multi_get(ios);
228 case L4VBUS_GPIO_OP_MULTI_SET: return multi_set(ios);
229 case L4VBUS_GPIO_OP_TO_IRQ: return to_irq(ios);
230 default: return -L4_ENOSYS;
240 static Dev_factory_t<Gpio, Hw::Gpio_device> __gpio_factory;