xref: /openbmc/qemu/hw/gpio/omap_gpio.c (revision 062cfce8d4c077800d252b84c65da8a2dd03fd6f)
15193899aSPaolo Bonzini /*
25193899aSPaolo Bonzini  * TI OMAP processors GPIO emulation.
35193899aSPaolo Bonzini  *
45193899aSPaolo Bonzini  * Copyright (C) 2006-2008 Andrzej Zaborowski  <balrog@zabor.org>
55193899aSPaolo Bonzini  * Copyright (C) 2007-2009 Nokia Corporation
65193899aSPaolo Bonzini  *
75193899aSPaolo Bonzini  * This program is free software; you can redistribute it and/or
85193899aSPaolo Bonzini  * modify it under the terms of the GNU General Public License as
95193899aSPaolo Bonzini  * published by the Free Software Foundation; either version 2 or
105193899aSPaolo Bonzini  * (at your option) version 3 of the License.
115193899aSPaolo Bonzini  *
125193899aSPaolo Bonzini  * This program is distributed in the hope that it will be useful,
135193899aSPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
145193899aSPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155193899aSPaolo Bonzini  * GNU General Public License for more details.
165193899aSPaolo Bonzini  *
175193899aSPaolo Bonzini  * You should have received a copy of the GNU General Public License along
185193899aSPaolo Bonzini  * with this program; if not, see <http://www.gnu.org/licenses/>.
195193899aSPaolo Bonzini  */
205193899aSPaolo Bonzini 
2117b7f2dbSPeter Maydell #include "qemu/osdep.h"
22cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
2364552b6bSMarkus Armbruster #include "hw/irq.h"
24a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
255193899aSPaolo Bonzini #include "hw/arm/omap.h"
265193899aSPaolo Bonzini #include "hw/sysbus.h"
2784a3a53cSMarkus Armbruster #include "qemu/error-report.h"
280b8fa32fSMarkus Armbruster #include "qemu/module.h"
29ebc116f8Sxiaoqiang zhao #include "qapi/error.h"
305193899aSPaolo Bonzini 
315193899aSPaolo Bonzini struct omap_gpio_s {
325193899aSPaolo Bonzini     qemu_irq irq;
335193899aSPaolo Bonzini     qemu_irq handler[16];
345193899aSPaolo Bonzini 
355193899aSPaolo Bonzini     uint16_t inputs;
365193899aSPaolo Bonzini     uint16_t outputs;
375193899aSPaolo Bonzini     uint16_t dir;
385193899aSPaolo Bonzini     uint16_t edge;
395193899aSPaolo Bonzini     uint16_t mask;
405193899aSPaolo Bonzini     uint16_t ints;
415193899aSPaolo Bonzini     uint16_t pins;
425193899aSPaolo Bonzini };
435193899aSPaolo Bonzini 
44bbcdf7d0SPhilippe Mathieu-Daudé struct Omap1GpioState {
451d300b5fSAndreas Färber     SysBusDevice parent_obj;
461d300b5fSAndreas Färber 
475193899aSPaolo Bonzini     MemoryRegion iomem;
485193899aSPaolo Bonzini     int mpu_model;
495193899aSPaolo Bonzini     void *clk;
505193899aSPaolo Bonzini     struct omap_gpio_s omap1;
515193899aSPaolo Bonzini };
525193899aSPaolo Bonzini 
535193899aSPaolo Bonzini /* General-Purpose I/O of OMAP1 */
omap_gpio_set(void * opaque,int line,int level)545193899aSPaolo Bonzini static void omap_gpio_set(void *opaque, int line, int level)
555193899aSPaolo Bonzini {
56bbcdf7d0SPhilippe Mathieu-Daudé     Omap1GpioState *p = opaque;
5728180159SPhilippe Mathieu-Daudé     struct omap_gpio_s *s = &p->omap1;
585193899aSPaolo Bonzini     uint16_t prev = s->inputs;
595193899aSPaolo Bonzini 
605193899aSPaolo Bonzini     if (level)
615193899aSPaolo Bonzini         s->inputs |= 1 << line;
625193899aSPaolo Bonzini     else
635193899aSPaolo Bonzini         s->inputs &= ~(1 << line);
645193899aSPaolo Bonzini 
655193899aSPaolo Bonzini     if (((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) &
665193899aSPaolo Bonzini                     (1 << line) & s->dir & ~s->mask) {
675193899aSPaolo Bonzini         s->ints |= 1 << line;
685193899aSPaolo Bonzini         qemu_irq_raise(s->irq);
695193899aSPaolo Bonzini     }
705193899aSPaolo Bonzini }
715193899aSPaolo Bonzini 
omap_gpio_read(void * opaque,hwaddr addr,unsigned size)725193899aSPaolo Bonzini static uint64_t omap_gpio_read(void *opaque, hwaddr addr,
735193899aSPaolo Bonzini                                unsigned size)
745193899aSPaolo Bonzini {
75a75ed3c4SPhilippe Mathieu-Daudé     struct omap_gpio_s *s = opaque;
765193899aSPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
775193899aSPaolo Bonzini 
785193899aSPaolo Bonzini     if (size != 2) {
795193899aSPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
805193899aSPaolo Bonzini     }
815193899aSPaolo Bonzini 
825193899aSPaolo Bonzini     switch (offset) {
835193899aSPaolo Bonzini     case 0x00:	/* DATA_INPUT */
845193899aSPaolo Bonzini         return s->inputs & s->pins;
855193899aSPaolo Bonzini 
865193899aSPaolo Bonzini     case 0x04:	/* DATA_OUTPUT */
875193899aSPaolo Bonzini         return s->outputs;
885193899aSPaolo Bonzini 
895193899aSPaolo Bonzini     case 0x08:	/* DIRECTION_CONTROL */
905193899aSPaolo Bonzini         return s->dir;
915193899aSPaolo Bonzini 
925193899aSPaolo Bonzini     case 0x0c:	/* INTERRUPT_CONTROL */
935193899aSPaolo Bonzini         return s->edge;
945193899aSPaolo Bonzini 
955193899aSPaolo Bonzini     case 0x10:	/* INTERRUPT_MASK */
965193899aSPaolo Bonzini         return s->mask;
975193899aSPaolo Bonzini 
985193899aSPaolo Bonzini     case 0x14:	/* INTERRUPT_STATUS */
995193899aSPaolo Bonzini         return s->ints;
1005193899aSPaolo Bonzini 
1015193899aSPaolo Bonzini     case 0x18:	/* PIN_CONTROL (not in OMAP310) */
1025193899aSPaolo Bonzini         OMAP_BAD_REG(addr);
1035193899aSPaolo Bonzini         return s->pins;
1045193899aSPaolo Bonzini     }
1055193899aSPaolo Bonzini 
1065193899aSPaolo Bonzini     OMAP_BAD_REG(addr);
1075193899aSPaolo Bonzini     return 0;
1085193899aSPaolo Bonzini }
1095193899aSPaolo Bonzini 
omap_gpio_write(void * opaque,hwaddr addr,uint64_t value,unsigned size)1105193899aSPaolo Bonzini static void omap_gpio_write(void *opaque, hwaddr addr,
1115193899aSPaolo Bonzini                             uint64_t value, unsigned size)
1125193899aSPaolo Bonzini {
113a75ed3c4SPhilippe Mathieu-Daudé     struct omap_gpio_s *s = opaque;
1145193899aSPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
1155193899aSPaolo Bonzini     uint16_t diff;
1165193899aSPaolo Bonzini     int ln;
1175193899aSPaolo Bonzini 
1185193899aSPaolo Bonzini     if (size != 2) {
11977a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
12077a8257eSStefan Weil         return;
1215193899aSPaolo Bonzini     }
1225193899aSPaolo Bonzini 
1235193899aSPaolo Bonzini     switch (offset) {
1245193899aSPaolo Bonzini     case 0x00:	/* DATA_INPUT */
1255193899aSPaolo Bonzini         OMAP_RO_REG(addr);
1265193899aSPaolo Bonzini         return;
1275193899aSPaolo Bonzini 
1285193899aSPaolo Bonzini     case 0x04:	/* DATA_OUTPUT */
1295193899aSPaolo Bonzini         diff = (s->outputs ^ value) & ~s->dir;
1305193899aSPaolo Bonzini         s->outputs = value;
131bd2a8884SStefan Hajnoczi         while ((ln = ctz32(diff)) != 32) {
1325193899aSPaolo Bonzini             if (s->handler[ln])
1335193899aSPaolo Bonzini                 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
1345193899aSPaolo Bonzini             diff &= ~(1 << ln);
1355193899aSPaolo Bonzini         }
1365193899aSPaolo Bonzini         break;
1375193899aSPaolo Bonzini 
1385193899aSPaolo Bonzini     case 0x08:	/* DIRECTION_CONTROL */
1395193899aSPaolo Bonzini         diff = s->outputs & (s->dir ^ value);
1405193899aSPaolo Bonzini         s->dir = value;
1415193899aSPaolo Bonzini 
1425193899aSPaolo Bonzini         value = s->outputs & ~s->dir;
143bd2a8884SStefan Hajnoczi         while ((ln = ctz32(diff)) != 32) {
1445193899aSPaolo Bonzini             if (s->handler[ln])
1455193899aSPaolo Bonzini                 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
1465193899aSPaolo Bonzini             diff &= ~(1 << ln);
1475193899aSPaolo Bonzini         }
1485193899aSPaolo Bonzini         break;
1495193899aSPaolo Bonzini 
1505193899aSPaolo Bonzini     case 0x0c:	/* INTERRUPT_CONTROL */
1515193899aSPaolo Bonzini         s->edge = value;
1525193899aSPaolo Bonzini         break;
1535193899aSPaolo Bonzini 
1545193899aSPaolo Bonzini     case 0x10:	/* INTERRUPT_MASK */
1555193899aSPaolo Bonzini         s->mask = value;
1565193899aSPaolo Bonzini         break;
1575193899aSPaolo Bonzini 
1585193899aSPaolo Bonzini     case 0x14:	/* INTERRUPT_STATUS */
1595193899aSPaolo Bonzini         s->ints &= ~value;
1605193899aSPaolo Bonzini         if (!s->ints)
1615193899aSPaolo Bonzini             qemu_irq_lower(s->irq);
1625193899aSPaolo Bonzini         break;
1635193899aSPaolo Bonzini 
1645193899aSPaolo Bonzini     case 0x18:	/* PIN_CONTROL (not in OMAP310 TRM) */
1655193899aSPaolo Bonzini         OMAP_BAD_REG(addr);
1665193899aSPaolo Bonzini         s->pins = value;
1675193899aSPaolo Bonzini         break;
1685193899aSPaolo Bonzini 
1695193899aSPaolo Bonzini     default:
1705193899aSPaolo Bonzini         OMAP_BAD_REG(addr);
1715193899aSPaolo Bonzini         return;
1725193899aSPaolo Bonzini     }
1735193899aSPaolo Bonzini }
1745193899aSPaolo Bonzini 
1755193899aSPaolo Bonzini /* *Some* sources say the memory region is 32-bit.  */
1765193899aSPaolo Bonzini static const MemoryRegionOps omap_gpio_ops = {
1775193899aSPaolo Bonzini     .read = omap_gpio_read,
1785193899aSPaolo Bonzini     .write = omap_gpio_write,
1795193899aSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1805193899aSPaolo Bonzini };
1815193899aSPaolo Bonzini 
omap_gpio_reset(struct omap_gpio_s * s)1825193899aSPaolo Bonzini static void omap_gpio_reset(struct omap_gpio_s *s)
1835193899aSPaolo Bonzini {
1845193899aSPaolo Bonzini     s->inputs = 0;
1855193899aSPaolo Bonzini     s->outputs = ~0;
1865193899aSPaolo Bonzini     s->dir = ~0;
1875193899aSPaolo Bonzini     s->edge = ~0;
1885193899aSPaolo Bonzini     s->mask = ~0;
1895193899aSPaolo Bonzini     s->ints = 0;
1905193899aSPaolo Bonzini     s->pins = ~0;
1915193899aSPaolo Bonzini }
1925193899aSPaolo Bonzini 
omap_gpif_reset(DeviceState * dev)1935193899aSPaolo Bonzini static void omap_gpif_reset(DeviceState *dev)
1945193899aSPaolo Bonzini {
195bbcdf7d0SPhilippe Mathieu-Daudé     Omap1GpioState *s = OMAP1_GPIO(dev);
1961d300b5fSAndreas Färber 
1975193899aSPaolo Bonzini     omap_gpio_reset(&s->omap1);
1985193899aSPaolo Bonzini }
1995193899aSPaolo Bonzini 
omap_gpio_init(Object * obj)200ebc116f8Sxiaoqiang zhao static void omap_gpio_init(Object *obj)
2015193899aSPaolo Bonzini {
202ebc116f8Sxiaoqiang zhao     DeviceState *dev = DEVICE(obj);
203bbcdf7d0SPhilippe Mathieu-Daudé     Omap1GpioState *s = OMAP1_GPIO(obj);
204ebc116f8Sxiaoqiang zhao     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2051d300b5fSAndreas Färber 
2061d300b5fSAndreas Färber     qdev_init_gpio_in(dev, omap_gpio_set, 16);
2071d300b5fSAndreas Färber     qdev_init_gpio_out(dev, s->omap1.handler, 16);
2081d300b5fSAndreas Färber     sysbus_init_irq(sbd, &s->omap1.irq);
209ebc116f8Sxiaoqiang zhao     memory_region_init_io(&s->iomem, obj, &omap_gpio_ops, &s->omap1,
2105193899aSPaolo Bonzini                           "omap.gpio", 0x1000);
2111d300b5fSAndreas Färber     sysbus_init_mmio(sbd, &s->iomem);
2125193899aSPaolo Bonzini }
2135193899aSPaolo Bonzini 
omap_gpio_realize(DeviceState * dev,Error ** errp)214ebc116f8Sxiaoqiang zhao static void omap_gpio_realize(DeviceState *dev, Error **errp)
2155193899aSPaolo Bonzini {
216bbcdf7d0SPhilippe Mathieu-Daudé     Omap1GpioState *s = OMAP1_GPIO(dev);
217ebc116f8Sxiaoqiang zhao 
218ebc116f8Sxiaoqiang zhao     if (!s->clk) {
219ebc116f8Sxiaoqiang zhao         error_setg(errp, "omap-gpio: clk not connected");
220ebc116f8Sxiaoqiang zhao     }
221ebc116f8Sxiaoqiang zhao }
222ebc116f8Sxiaoqiang zhao 
omap_gpio_set_clk(Omap1GpioState * gpio,omap_clk clk)223bbcdf7d0SPhilippe Mathieu-Daudé void omap_gpio_set_clk(Omap1GpioState *gpio, omap_clk clk)
224ba2aba83SMarc-André Lureau {
225ba2aba83SMarc-André Lureau     gpio->clk = clk;
226ba2aba83SMarc-André Lureau }
2275193899aSPaolo Bonzini 
2285193899aSPaolo Bonzini static Property omap_gpio_properties[] = {
229bbcdf7d0SPhilippe Mathieu-Daudé     DEFINE_PROP_INT32("mpu_model", Omap1GpioState, mpu_model, 0),
2305193899aSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
2315193899aSPaolo Bonzini };
2325193899aSPaolo Bonzini 
omap_gpio_class_init(ObjectClass * klass,void * data)2335193899aSPaolo Bonzini static void omap_gpio_class_init(ObjectClass *klass, void *data)
2345193899aSPaolo Bonzini {
2355193899aSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
2365193899aSPaolo Bonzini 
237ebc116f8Sxiaoqiang zhao     dc->realize = omap_gpio_realize;
238*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, omap_gpif_reset);
2394f67d30bSMarc-André Lureau     device_class_set_props(dc, omap_gpio_properties);
2401b111dc1SMarkus Armbruster     /* Reason: pointer property "clk" */
241e90f2a8cSEduardo Habkost     dc->user_creatable = false;
2425193899aSPaolo Bonzini }
2435193899aSPaolo Bonzini 
2445193899aSPaolo Bonzini static const TypeInfo omap_gpio_info = {
2451d300b5fSAndreas Färber     .name          = TYPE_OMAP1_GPIO,
2465193899aSPaolo Bonzini     .parent        = TYPE_SYS_BUS_DEVICE,
247bbcdf7d0SPhilippe Mathieu-Daudé     .instance_size = sizeof(Omap1GpioState),
248ebc116f8Sxiaoqiang zhao     .instance_init = omap_gpio_init,
2495193899aSPaolo Bonzini     .class_init    = omap_gpio_class_init,
2505193899aSPaolo Bonzini };
2515193899aSPaolo Bonzini 
omap_gpio_register_types(void)2525193899aSPaolo Bonzini static void omap_gpio_register_types(void)
2535193899aSPaolo Bonzini {
2545193899aSPaolo Bonzini     type_register_static(&omap_gpio_info);
2555193899aSPaolo Bonzini }
2565193899aSPaolo Bonzini 
2575193899aSPaolo Bonzini type_init(omap_gpio_register_types)
258