xref: /openbmc/qemu/hw/arm/omap1.c (revision ba2aba83)
1dd285b06SPaolo Bonzini /*
2dd285b06SPaolo Bonzini  * TI OMAP processors emulation.
3dd285b06SPaolo Bonzini  *
4dd285b06SPaolo Bonzini  * Copyright (C) 2006-2008 Andrzej Zaborowski  <balrog@zabor.org>
5dd285b06SPaolo Bonzini  *
6dd285b06SPaolo Bonzini  * This program is free software; you can redistribute it and/or
7dd285b06SPaolo Bonzini  * modify it under the terms of the GNU General Public License as
8dd285b06SPaolo Bonzini  * published by the Free Software Foundation; either version 2 or
9dd285b06SPaolo Bonzini  * (at your option) version 3 of the License.
10dd285b06SPaolo Bonzini  *
11dd285b06SPaolo Bonzini  * This program is distributed in the hope that it will be useful,
12dd285b06SPaolo Bonzini  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13dd285b06SPaolo Bonzini  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14dd285b06SPaolo Bonzini  * GNU General Public License for more details.
15dd285b06SPaolo Bonzini  *
16dd285b06SPaolo Bonzini  * You should have received a copy of the GNU General Public License along
17dd285b06SPaolo Bonzini  * with this program; if not, see <http://www.gnu.org/licenses/>.
18dd285b06SPaolo Bonzini  */
19c8623c02SDirk Müller 
2012b16722SPeter Maydell #include "qemu/osdep.h"
21c0dbca36SAlistair Francis #include "qemu/error-report.h"
22db725815SMarkus Armbruster #include "qemu/main-loop.h"
23da34e65cSMarkus Armbruster #include "qapi/error.h"
244771d756SPaolo Bonzini #include "qemu-common.h"
254771d756SPaolo Bonzini #include "cpu.h"
264387b253SPhilippe Mathieu-Daudé #include "exec/address-spaces.h"
27c8623c02SDirk Müller #include "hw/boards.h"
28dd285b06SPaolo Bonzini #include "hw/hw.h"
2964552b6bSMarkus Armbruster #include "hw/irq.h"
30a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3112ec8bd5SPeter Maydell #include "hw/arm/boot.h"
320d09e41aSPaolo Bonzini #include "hw/arm/omap.h"
3312e9493dSMarkus Armbruster #include "sysemu/blockdev.h"
34dd285b06SPaolo Bonzini #include "sysemu/sysemu.h"
350d09e41aSPaolo Bonzini #include "hw/arm/soc_dma.h"
36a82929a2SThomas Huth #include "sysemu/qtest.h"
3771e8a915SMarkus Armbruster #include "sysemu/reset.h"
3854d31236SMarkus Armbruster #include "sysemu/runstate.h"
39dd285b06SPaolo Bonzini #include "qemu/range.h"
40dd285b06SPaolo Bonzini #include "hw/sysbus.h"
41f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
42f348b6d1SVeronia Bahaa #include "qemu/bcd.h"
43dd285b06SPaolo Bonzini 
44415202d4SPhilippe Mathieu-Daudé static inline void omap_log_badwidth(const char *funcname, hwaddr addr, int sz)
45415202d4SPhilippe Mathieu-Daudé {
46415202d4SPhilippe Mathieu-Daudé     qemu_log_mask(LOG_GUEST_ERROR, "%s: %d-bit register %#08" HWADDR_PRIx "\n",
47415202d4SPhilippe Mathieu-Daudé                   funcname, 8 * sz, addr);
48415202d4SPhilippe Mathieu-Daudé }
49415202d4SPhilippe Mathieu-Daudé 
50dd285b06SPaolo Bonzini /* Should signal the TCMI/GPMC */
51dd285b06SPaolo Bonzini uint32_t omap_badwidth_read8(void *opaque, hwaddr addr)
52dd285b06SPaolo Bonzini {
53dd285b06SPaolo Bonzini     uint8_t ret;
54dd285b06SPaolo Bonzini 
55415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 1);
56e1fe50dcSStefan Weil     cpu_physical_memory_read(addr, &ret, 1);
57dd285b06SPaolo Bonzini     return ret;
58dd285b06SPaolo Bonzini }
59dd285b06SPaolo Bonzini 
60dd285b06SPaolo Bonzini void omap_badwidth_write8(void *opaque, hwaddr addr,
61dd285b06SPaolo Bonzini                 uint32_t value)
62dd285b06SPaolo Bonzini {
63dd285b06SPaolo Bonzini     uint8_t val8 = value;
64dd285b06SPaolo Bonzini 
65415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 1);
66e1fe50dcSStefan Weil     cpu_physical_memory_write(addr, &val8, 1);
67dd285b06SPaolo Bonzini }
68dd285b06SPaolo Bonzini 
69dd285b06SPaolo Bonzini uint32_t omap_badwidth_read16(void *opaque, hwaddr addr)
70dd285b06SPaolo Bonzini {
71dd285b06SPaolo Bonzini     uint16_t ret;
72dd285b06SPaolo Bonzini 
73415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 2);
74e1fe50dcSStefan Weil     cpu_physical_memory_read(addr, &ret, 2);
75dd285b06SPaolo Bonzini     return ret;
76dd285b06SPaolo Bonzini }
77dd285b06SPaolo Bonzini 
78dd285b06SPaolo Bonzini void omap_badwidth_write16(void *opaque, hwaddr addr,
79dd285b06SPaolo Bonzini                 uint32_t value)
80dd285b06SPaolo Bonzini {
81dd285b06SPaolo Bonzini     uint16_t val16 = value;
82dd285b06SPaolo Bonzini 
83415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 2);
84e1fe50dcSStefan Weil     cpu_physical_memory_write(addr, &val16, 2);
85dd285b06SPaolo Bonzini }
86dd285b06SPaolo Bonzini 
87dd285b06SPaolo Bonzini uint32_t omap_badwidth_read32(void *opaque, hwaddr addr)
88dd285b06SPaolo Bonzini {
89dd285b06SPaolo Bonzini     uint32_t ret;
90dd285b06SPaolo Bonzini 
91415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 4);
92e1fe50dcSStefan Weil     cpu_physical_memory_read(addr, &ret, 4);
93dd285b06SPaolo Bonzini     return ret;
94dd285b06SPaolo Bonzini }
95dd285b06SPaolo Bonzini 
96dd285b06SPaolo Bonzini void omap_badwidth_write32(void *opaque, hwaddr addr,
97dd285b06SPaolo Bonzini                 uint32_t value)
98dd285b06SPaolo Bonzini {
99415202d4SPhilippe Mathieu-Daudé     omap_log_badwidth(__func__, addr, 4);
100e1fe50dcSStefan Weil     cpu_physical_memory_write(addr, &value, 4);
101dd285b06SPaolo Bonzini }
102dd285b06SPaolo Bonzini 
103dd285b06SPaolo Bonzini /* MPU OS timers */
104dd285b06SPaolo Bonzini struct omap_mpu_timer_s {
105dd285b06SPaolo Bonzini     MemoryRegion iomem;
106dd285b06SPaolo Bonzini     qemu_irq irq;
107dd285b06SPaolo Bonzini     omap_clk clk;
108dd285b06SPaolo Bonzini     uint32_t val;
109dd285b06SPaolo Bonzini     int64_t time;
110dd285b06SPaolo Bonzini     QEMUTimer *timer;
111dd285b06SPaolo Bonzini     QEMUBH *tick;
112dd285b06SPaolo Bonzini     int64_t rate;
113dd285b06SPaolo Bonzini     int it_ena;
114dd285b06SPaolo Bonzini 
115dd285b06SPaolo Bonzini     int enable;
116dd285b06SPaolo Bonzini     int ptv;
117dd285b06SPaolo Bonzini     int ar;
118dd285b06SPaolo Bonzini     int st;
119dd285b06SPaolo Bonzini     uint32_t reset_val;
120dd285b06SPaolo Bonzini };
121dd285b06SPaolo Bonzini 
122dd285b06SPaolo Bonzini static inline uint32_t omap_timer_read(struct omap_mpu_timer_s *timer)
123dd285b06SPaolo Bonzini {
124bc72ad67SAlex Bligh     uint64_t distance = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - timer->time;
125dd285b06SPaolo Bonzini 
126dd285b06SPaolo Bonzini     if (timer->st && timer->enable && timer->rate)
127dd285b06SPaolo Bonzini         return timer->val - muldiv64(distance >> (timer->ptv + 1),
12873bcb24dSRutuja Shah                                      timer->rate, NANOSECONDS_PER_SECOND);
129dd285b06SPaolo Bonzini     else
130dd285b06SPaolo Bonzini         return timer->val;
131dd285b06SPaolo Bonzini }
132dd285b06SPaolo Bonzini 
133dd285b06SPaolo Bonzini static inline void omap_timer_sync(struct omap_mpu_timer_s *timer)
134dd285b06SPaolo Bonzini {
135dd285b06SPaolo Bonzini     timer->val = omap_timer_read(timer);
136bc72ad67SAlex Bligh     timer->time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
137dd285b06SPaolo Bonzini }
138dd285b06SPaolo Bonzini 
139dd285b06SPaolo Bonzini static inline void omap_timer_update(struct omap_mpu_timer_s *timer)
140dd285b06SPaolo Bonzini {
141dd285b06SPaolo Bonzini     int64_t expires;
142dd285b06SPaolo Bonzini 
143dd285b06SPaolo Bonzini     if (timer->enable && timer->st && timer->rate) {
144dd285b06SPaolo Bonzini         timer->val = timer->reset_val;	/* Should skip this on clk enable */
145dd285b06SPaolo Bonzini         expires = muldiv64((uint64_t) timer->val << (timer->ptv + 1),
14673bcb24dSRutuja Shah                            NANOSECONDS_PER_SECOND, timer->rate);
147dd285b06SPaolo Bonzini 
148dd285b06SPaolo Bonzini         /* If timer expiry would be sooner than in about 1 ms and
149dd285b06SPaolo Bonzini          * auto-reload isn't set, then fire immediately.  This is a hack
150dd285b06SPaolo Bonzini          * to make systems like PalmOS run in acceptable time.  PalmOS
151dd285b06SPaolo Bonzini          * sets the interval to a very low value and polls the status bit
152dd285b06SPaolo Bonzini          * in a busy loop when it wants to sleep just a couple of CPU
153dd285b06SPaolo Bonzini          * ticks.  */
15473bcb24dSRutuja Shah         if (expires > (NANOSECONDS_PER_SECOND >> 10) || timer->ar) {
155bc72ad67SAlex Bligh             timer_mod(timer->timer, timer->time + expires);
15673bcb24dSRutuja Shah         } else {
157dd285b06SPaolo Bonzini             qemu_bh_schedule(timer->tick);
15873bcb24dSRutuja Shah         }
159dd285b06SPaolo Bonzini     } else
160bc72ad67SAlex Bligh         timer_del(timer->timer);
161dd285b06SPaolo Bonzini }
162dd285b06SPaolo Bonzini 
163dd285b06SPaolo Bonzini static void omap_timer_fire(void *opaque)
164dd285b06SPaolo Bonzini {
165dd285b06SPaolo Bonzini     struct omap_mpu_timer_s *timer = opaque;
166dd285b06SPaolo Bonzini 
167dd285b06SPaolo Bonzini     if (!timer->ar) {
168dd285b06SPaolo Bonzini         timer->val = 0;
169dd285b06SPaolo Bonzini         timer->st = 0;
170dd285b06SPaolo Bonzini     }
171dd285b06SPaolo Bonzini 
172dd285b06SPaolo Bonzini     if (timer->it_ena)
173dd285b06SPaolo Bonzini         /* Edge-triggered irq */
174dd285b06SPaolo Bonzini         qemu_irq_pulse(timer->irq);
175dd285b06SPaolo Bonzini }
176dd285b06SPaolo Bonzini 
177dd285b06SPaolo Bonzini static void omap_timer_tick(void *opaque)
178dd285b06SPaolo Bonzini {
179dd285b06SPaolo Bonzini     struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
180dd285b06SPaolo Bonzini 
181dd285b06SPaolo Bonzini     omap_timer_sync(timer);
182dd285b06SPaolo Bonzini     omap_timer_fire(timer);
183dd285b06SPaolo Bonzini     omap_timer_update(timer);
184dd285b06SPaolo Bonzini }
185dd285b06SPaolo Bonzini 
186dd285b06SPaolo Bonzini static void omap_timer_clk_update(void *opaque, int line, int on)
187dd285b06SPaolo Bonzini {
188dd285b06SPaolo Bonzini     struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
189dd285b06SPaolo Bonzini 
190dd285b06SPaolo Bonzini     omap_timer_sync(timer);
191dd285b06SPaolo Bonzini     timer->rate = on ? omap_clk_getrate(timer->clk) : 0;
192dd285b06SPaolo Bonzini     omap_timer_update(timer);
193dd285b06SPaolo Bonzini }
194dd285b06SPaolo Bonzini 
195dd285b06SPaolo Bonzini static void omap_timer_clk_setup(struct omap_mpu_timer_s *timer)
196dd285b06SPaolo Bonzini {
197dd285b06SPaolo Bonzini     omap_clk_adduser(timer->clk,
198f3c7d038SAndreas Färber                     qemu_allocate_irq(omap_timer_clk_update, timer, 0));
199dd285b06SPaolo Bonzini     timer->rate = omap_clk_getrate(timer->clk);
200dd285b06SPaolo Bonzini }
201dd285b06SPaolo Bonzini 
202dd285b06SPaolo Bonzini static uint64_t omap_mpu_timer_read(void *opaque, hwaddr addr,
203dd285b06SPaolo Bonzini                                     unsigned size)
204dd285b06SPaolo Bonzini {
205dd285b06SPaolo Bonzini     struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
206dd285b06SPaolo Bonzini 
207dd285b06SPaolo Bonzini     if (size != 4) {
208dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
209dd285b06SPaolo Bonzini     }
210dd285b06SPaolo Bonzini 
211dd285b06SPaolo Bonzini     switch (addr) {
212dd285b06SPaolo Bonzini     case 0x00:	/* CNTL_TIMER */
213dd285b06SPaolo Bonzini         return (s->enable << 5) | (s->ptv << 2) | (s->ar << 1) | s->st;
214dd285b06SPaolo Bonzini 
215dd285b06SPaolo Bonzini     case 0x04:	/* LOAD_TIM */
216dd285b06SPaolo Bonzini         break;
217dd285b06SPaolo Bonzini 
218dd285b06SPaolo Bonzini     case 0x08:	/* READ_TIM */
219dd285b06SPaolo Bonzini         return omap_timer_read(s);
220dd285b06SPaolo Bonzini     }
221dd285b06SPaolo Bonzini 
222dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
223dd285b06SPaolo Bonzini     return 0;
224dd285b06SPaolo Bonzini }
225dd285b06SPaolo Bonzini 
226dd285b06SPaolo Bonzini static void omap_mpu_timer_write(void *opaque, hwaddr addr,
227dd285b06SPaolo Bonzini                                  uint64_t value, unsigned size)
228dd285b06SPaolo Bonzini {
229dd285b06SPaolo Bonzini     struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
230dd285b06SPaolo Bonzini 
231dd285b06SPaolo Bonzini     if (size != 4) {
23277a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
23377a8257eSStefan Weil         return;
234dd285b06SPaolo Bonzini     }
235dd285b06SPaolo Bonzini 
236dd285b06SPaolo Bonzini     switch (addr) {
237dd285b06SPaolo Bonzini     case 0x00:	/* CNTL_TIMER */
238dd285b06SPaolo Bonzini         omap_timer_sync(s);
239dd285b06SPaolo Bonzini         s->enable = (value >> 5) & 1;
240dd285b06SPaolo Bonzini         s->ptv = (value >> 2) & 7;
241dd285b06SPaolo Bonzini         s->ar = (value >> 1) & 1;
242dd285b06SPaolo Bonzini         s->st = value & 1;
243dd285b06SPaolo Bonzini         omap_timer_update(s);
244dd285b06SPaolo Bonzini         return;
245dd285b06SPaolo Bonzini 
246dd285b06SPaolo Bonzini     case 0x04:	/* LOAD_TIM */
247dd285b06SPaolo Bonzini         s->reset_val = value;
248dd285b06SPaolo Bonzini         return;
249dd285b06SPaolo Bonzini 
250dd285b06SPaolo Bonzini     case 0x08:	/* READ_TIM */
251dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
252dd285b06SPaolo Bonzini         break;
253dd285b06SPaolo Bonzini 
254dd285b06SPaolo Bonzini     default:
255dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
256dd285b06SPaolo Bonzini     }
257dd285b06SPaolo Bonzini }
258dd285b06SPaolo Bonzini 
259dd285b06SPaolo Bonzini static const MemoryRegionOps omap_mpu_timer_ops = {
260dd285b06SPaolo Bonzini     .read = omap_mpu_timer_read,
261dd285b06SPaolo Bonzini     .write = omap_mpu_timer_write,
262dd285b06SPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
263dd285b06SPaolo Bonzini };
264dd285b06SPaolo Bonzini 
265dd285b06SPaolo Bonzini static void omap_mpu_timer_reset(struct omap_mpu_timer_s *s)
266dd285b06SPaolo Bonzini {
267bc72ad67SAlex Bligh     timer_del(s->timer);
268dd285b06SPaolo Bonzini     s->enable = 0;
269dd285b06SPaolo Bonzini     s->reset_val = 31337;
270dd285b06SPaolo Bonzini     s->val = 0;
271dd285b06SPaolo Bonzini     s->ptv = 0;
272dd285b06SPaolo Bonzini     s->ar = 0;
273dd285b06SPaolo Bonzini     s->st = 0;
274dd285b06SPaolo Bonzini     s->it_ena = 1;
275dd285b06SPaolo Bonzini }
276dd285b06SPaolo Bonzini 
277dd285b06SPaolo Bonzini static struct omap_mpu_timer_s *omap_mpu_timer_init(MemoryRegion *system_memory,
278dd285b06SPaolo Bonzini                 hwaddr base,
279dd285b06SPaolo Bonzini                 qemu_irq irq, omap_clk clk)
280dd285b06SPaolo Bonzini {
281b45c03f5SMarkus Armbruster     struct omap_mpu_timer_s *s = g_new0(struct omap_mpu_timer_s, 1);
282dd285b06SPaolo Bonzini 
283dd285b06SPaolo Bonzini     s->irq = irq;
284dd285b06SPaolo Bonzini     s->clk = clk;
285bc72ad67SAlex Bligh     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, s);
286dd285b06SPaolo Bonzini     s->tick = qemu_bh_new(omap_timer_fire, s);
287dd285b06SPaolo Bonzini     omap_mpu_timer_reset(s);
288dd285b06SPaolo Bonzini     omap_timer_clk_setup(s);
289dd285b06SPaolo Bonzini 
2902c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_mpu_timer_ops, s,
291dd285b06SPaolo Bonzini                           "omap-mpu-timer", 0x100);
292dd285b06SPaolo Bonzini 
293dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
294dd285b06SPaolo Bonzini 
295dd285b06SPaolo Bonzini     return s;
296dd285b06SPaolo Bonzini }
297dd285b06SPaolo Bonzini 
298dd285b06SPaolo Bonzini /* Watchdog timer */
299dd285b06SPaolo Bonzini struct omap_watchdog_timer_s {
300dd285b06SPaolo Bonzini     struct omap_mpu_timer_s timer;
301dd285b06SPaolo Bonzini     MemoryRegion iomem;
302dd285b06SPaolo Bonzini     uint8_t last_wr;
303dd285b06SPaolo Bonzini     int mode;
304dd285b06SPaolo Bonzini     int free;
305dd285b06SPaolo Bonzini     int reset;
306dd285b06SPaolo Bonzini };
307dd285b06SPaolo Bonzini 
308dd285b06SPaolo Bonzini static uint64_t omap_wd_timer_read(void *opaque, hwaddr addr,
309dd285b06SPaolo Bonzini                                    unsigned size)
310dd285b06SPaolo Bonzini {
311dd285b06SPaolo Bonzini     struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
312dd285b06SPaolo Bonzini 
313dd285b06SPaolo Bonzini     if (size != 2) {
314dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
315dd285b06SPaolo Bonzini     }
316dd285b06SPaolo Bonzini 
317dd285b06SPaolo Bonzini     switch (addr) {
318dd285b06SPaolo Bonzini     case 0x00:	/* CNTL_TIMER */
319dd285b06SPaolo Bonzini         return (s->timer.ptv << 9) | (s->timer.ar << 8) |
320dd285b06SPaolo Bonzini                 (s->timer.st << 7) | (s->free << 1);
321dd285b06SPaolo Bonzini 
322dd285b06SPaolo Bonzini     case 0x04:	/* READ_TIMER */
323dd285b06SPaolo Bonzini         return omap_timer_read(&s->timer);
324dd285b06SPaolo Bonzini 
325dd285b06SPaolo Bonzini     case 0x08:	/* TIMER_MODE */
326dd285b06SPaolo Bonzini         return s->mode << 15;
327dd285b06SPaolo Bonzini     }
328dd285b06SPaolo Bonzini 
329dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
330dd285b06SPaolo Bonzini     return 0;
331dd285b06SPaolo Bonzini }
332dd285b06SPaolo Bonzini 
333dd285b06SPaolo Bonzini static void omap_wd_timer_write(void *opaque, hwaddr addr,
334dd285b06SPaolo Bonzini                                 uint64_t value, unsigned size)
335dd285b06SPaolo Bonzini {
336dd285b06SPaolo Bonzini     struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
337dd285b06SPaolo Bonzini 
338dd285b06SPaolo Bonzini     if (size != 2) {
33977a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
34077a8257eSStefan Weil         return;
341dd285b06SPaolo Bonzini     }
342dd285b06SPaolo Bonzini 
343dd285b06SPaolo Bonzini     switch (addr) {
344dd285b06SPaolo Bonzini     case 0x00:	/* CNTL_TIMER */
345dd285b06SPaolo Bonzini         omap_timer_sync(&s->timer);
346dd285b06SPaolo Bonzini         s->timer.ptv = (value >> 9) & 7;
347dd285b06SPaolo Bonzini         s->timer.ar = (value >> 8) & 1;
348dd285b06SPaolo Bonzini         s->timer.st = (value >> 7) & 1;
349dd285b06SPaolo Bonzini         s->free = (value >> 1) & 1;
350dd285b06SPaolo Bonzini         omap_timer_update(&s->timer);
351dd285b06SPaolo Bonzini         break;
352dd285b06SPaolo Bonzini 
353dd285b06SPaolo Bonzini     case 0x04:	/* LOAD_TIMER */
354dd285b06SPaolo Bonzini         s->timer.reset_val = value & 0xffff;
355dd285b06SPaolo Bonzini         break;
356dd285b06SPaolo Bonzini 
357dd285b06SPaolo Bonzini     case 0x08:	/* TIMER_MODE */
358dd285b06SPaolo Bonzini         if (!s->mode && ((value >> 15) & 1))
359dd285b06SPaolo Bonzini             omap_clk_get(s->timer.clk);
360dd285b06SPaolo Bonzini         s->mode |= (value >> 15) & 1;
361dd285b06SPaolo Bonzini         if (s->last_wr == 0xf5) {
362dd285b06SPaolo Bonzini             if ((value & 0xff) == 0xa0) {
363dd285b06SPaolo Bonzini                 if (s->mode) {
364dd285b06SPaolo Bonzini                     s->mode = 0;
365dd285b06SPaolo Bonzini                     omap_clk_put(s->timer.clk);
366dd285b06SPaolo Bonzini                 }
367dd285b06SPaolo Bonzini             } else {
368dd285b06SPaolo Bonzini                 /* XXX: on T|E hardware somehow this has no effect,
369dd285b06SPaolo Bonzini                  * on Zire 71 it works as specified.  */
370dd285b06SPaolo Bonzini                 s->reset = 1;
371cf83f140SEric Blake                 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
372dd285b06SPaolo Bonzini             }
373dd285b06SPaolo Bonzini         }
374dd285b06SPaolo Bonzini         s->last_wr = value & 0xff;
375dd285b06SPaolo Bonzini         break;
376dd285b06SPaolo Bonzini 
377dd285b06SPaolo Bonzini     default:
378dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
379dd285b06SPaolo Bonzini     }
380dd285b06SPaolo Bonzini }
381dd285b06SPaolo Bonzini 
382dd285b06SPaolo Bonzini static const MemoryRegionOps omap_wd_timer_ops = {
383dd285b06SPaolo Bonzini     .read = omap_wd_timer_read,
384dd285b06SPaolo Bonzini     .write = omap_wd_timer_write,
385dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
386dd285b06SPaolo Bonzini };
387dd285b06SPaolo Bonzini 
388dd285b06SPaolo Bonzini static void omap_wd_timer_reset(struct omap_watchdog_timer_s *s)
389dd285b06SPaolo Bonzini {
390bc72ad67SAlex Bligh     timer_del(s->timer.timer);
391dd285b06SPaolo Bonzini     if (!s->mode)
392dd285b06SPaolo Bonzini         omap_clk_get(s->timer.clk);
393dd285b06SPaolo Bonzini     s->mode = 1;
394dd285b06SPaolo Bonzini     s->free = 1;
395dd285b06SPaolo Bonzini     s->reset = 0;
396dd285b06SPaolo Bonzini     s->timer.enable = 1;
397dd285b06SPaolo Bonzini     s->timer.it_ena = 1;
398dd285b06SPaolo Bonzini     s->timer.reset_val = 0xffff;
399dd285b06SPaolo Bonzini     s->timer.val = 0;
400dd285b06SPaolo Bonzini     s->timer.st = 0;
401dd285b06SPaolo Bonzini     s->timer.ptv = 0;
402dd285b06SPaolo Bonzini     s->timer.ar = 0;
403dd285b06SPaolo Bonzini     omap_timer_update(&s->timer);
404dd285b06SPaolo Bonzini }
405dd285b06SPaolo Bonzini 
406dd285b06SPaolo Bonzini static struct omap_watchdog_timer_s *omap_wd_timer_init(MemoryRegion *memory,
407dd285b06SPaolo Bonzini                 hwaddr base,
408dd285b06SPaolo Bonzini                 qemu_irq irq, omap_clk clk)
409dd285b06SPaolo Bonzini {
410b45c03f5SMarkus Armbruster     struct omap_watchdog_timer_s *s = g_new0(struct omap_watchdog_timer_s, 1);
411dd285b06SPaolo Bonzini 
412dd285b06SPaolo Bonzini     s->timer.irq = irq;
413dd285b06SPaolo Bonzini     s->timer.clk = clk;
414bc72ad67SAlex Bligh     s->timer.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, &s->timer);
415dd285b06SPaolo Bonzini     omap_wd_timer_reset(s);
416dd285b06SPaolo Bonzini     omap_timer_clk_setup(&s->timer);
417dd285b06SPaolo Bonzini 
4182c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_wd_timer_ops, s,
419dd285b06SPaolo Bonzini                           "omap-wd-timer", 0x100);
420dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &s->iomem);
421dd285b06SPaolo Bonzini 
422dd285b06SPaolo Bonzini     return s;
423dd285b06SPaolo Bonzini }
424dd285b06SPaolo Bonzini 
425dd285b06SPaolo Bonzini /* 32-kHz timer */
426dd285b06SPaolo Bonzini struct omap_32khz_timer_s {
427dd285b06SPaolo Bonzini     struct omap_mpu_timer_s timer;
428dd285b06SPaolo Bonzini     MemoryRegion iomem;
429dd285b06SPaolo Bonzini };
430dd285b06SPaolo Bonzini 
431dd285b06SPaolo Bonzini static uint64_t omap_os_timer_read(void *opaque, hwaddr addr,
432dd285b06SPaolo Bonzini                                    unsigned size)
433dd285b06SPaolo Bonzini {
434dd285b06SPaolo Bonzini     struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
435dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
436dd285b06SPaolo Bonzini 
437dd285b06SPaolo Bonzini     if (size != 4) {
438dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
439dd285b06SPaolo Bonzini     }
440dd285b06SPaolo Bonzini 
441dd285b06SPaolo Bonzini     switch (offset) {
442dd285b06SPaolo Bonzini     case 0x00:	/* TVR */
443dd285b06SPaolo Bonzini         return s->timer.reset_val;
444dd285b06SPaolo Bonzini 
445dd285b06SPaolo Bonzini     case 0x04:	/* TCR */
446dd285b06SPaolo Bonzini         return omap_timer_read(&s->timer);
447dd285b06SPaolo Bonzini 
448dd285b06SPaolo Bonzini     case 0x08:	/* CR */
449dd285b06SPaolo Bonzini         return (s->timer.ar << 3) | (s->timer.it_ena << 2) | s->timer.st;
450dd285b06SPaolo Bonzini 
451dd285b06SPaolo Bonzini     default:
452dd285b06SPaolo Bonzini         break;
453dd285b06SPaolo Bonzini     }
454dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
455dd285b06SPaolo Bonzini     return 0;
456dd285b06SPaolo Bonzini }
457dd285b06SPaolo Bonzini 
458dd285b06SPaolo Bonzini static void omap_os_timer_write(void *opaque, hwaddr addr,
459dd285b06SPaolo Bonzini                                 uint64_t value, unsigned size)
460dd285b06SPaolo Bonzini {
461dd285b06SPaolo Bonzini     struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
462dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
463dd285b06SPaolo Bonzini 
464dd285b06SPaolo Bonzini     if (size != 4) {
46577a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
46677a8257eSStefan Weil         return;
467dd285b06SPaolo Bonzini     }
468dd285b06SPaolo Bonzini 
469dd285b06SPaolo Bonzini     switch (offset) {
470dd285b06SPaolo Bonzini     case 0x00:	/* TVR */
471dd285b06SPaolo Bonzini         s->timer.reset_val = value & 0x00ffffff;
472dd285b06SPaolo Bonzini         break;
473dd285b06SPaolo Bonzini 
474dd285b06SPaolo Bonzini     case 0x04:	/* TCR */
475dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
476dd285b06SPaolo Bonzini         break;
477dd285b06SPaolo Bonzini 
478dd285b06SPaolo Bonzini     case 0x08:	/* CR */
479dd285b06SPaolo Bonzini         s->timer.ar = (value >> 3) & 1;
480dd285b06SPaolo Bonzini         s->timer.it_ena = (value >> 2) & 1;
481dd285b06SPaolo Bonzini         if (s->timer.st != (value & 1) || (value & 2)) {
482dd285b06SPaolo Bonzini             omap_timer_sync(&s->timer);
483dd285b06SPaolo Bonzini             s->timer.enable = value & 1;
484dd285b06SPaolo Bonzini             s->timer.st = value & 1;
485dd285b06SPaolo Bonzini             omap_timer_update(&s->timer);
486dd285b06SPaolo Bonzini         }
487dd285b06SPaolo Bonzini         break;
488dd285b06SPaolo Bonzini 
489dd285b06SPaolo Bonzini     default:
490dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
491dd285b06SPaolo Bonzini     }
492dd285b06SPaolo Bonzini }
493dd285b06SPaolo Bonzini 
494dd285b06SPaolo Bonzini static const MemoryRegionOps omap_os_timer_ops = {
495dd285b06SPaolo Bonzini     .read = omap_os_timer_read,
496dd285b06SPaolo Bonzini     .write = omap_os_timer_write,
497dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
498dd285b06SPaolo Bonzini };
499dd285b06SPaolo Bonzini 
500dd285b06SPaolo Bonzini static void omap_os_timer_reset(struct omap_32khz_timer_s *s)
501dd285b06SPaolo Bonzini {
502bc72ad67SAlex Bligh     timer_del(s->timer.timer);
503dd285b06SPaolo Bonzini     s->timer.enable = 0;
504dd285b06SPaolo Bonzini     s->timer.it_ena = 0;
505dd285b06SPaolo Bonzini     s->timer.reset_val = 0x00ffffff;
506dd285b06SPaolo Bonzini     s->timer.val = 0;
507dd285b06SPaolo Bonzini     s->timer.st = 0;
508dd285b06SPaolo Bonzini     s->timer.ptv = 0;
509dd285b06SPaolo Bonzini     s->timer.ar = 1;
510dd285b06SPaolo Bonzini }
511dd285b06SPaolo Bonzini 
512dd285b06SPaolo Bonzini static struct omap_32khz_timer_s *omap_os_timer_init(MemoryRegion *memory,
513dd285b06SPaolo Bonzini                 hwaddr base,
514dd285b06SPaolo Bonzini                 qemu_irq irq, omap_clk clk)
515dd285b06SPaolo Bonzini {
516b45c03f5SMarkus Armbruster     struct omap_32khz_timer_s *s = g_new0(struct omap_32khz_timer_s, 1);
517dd285b06SPaolo Bonzini 
518dd285b06SPaolo Bonzini     s->timer.irq = irq;
519dd285b06SPaolo Bonzini     s->timer.clk = clk;
520bc72ad67SAlex Bligh     s->timer.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_timer_tick, &s->timer);
521dd285b06SPaolo Bonzini     omap_os_timer_reset(s);
522dd285b06SPaolo Bonzini     omap_timer_clk_setup(&s->timer);
523dd285b06SPaolo Bonzini 
5242c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_os_timer_ops, s,
525dd285b06SPaolo Bonzini                           "omap-os-timer", 0x800);
526dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &s->iomem);
527dd285b06SPaolo Bonzini 
528dd285b06SPaolo Bonzini     return s;
529dd285b06SPaolo Bonzini }
530dd285b06SPaolo Bonzini 
531dd285b06SPaolo Bonzini /* Ultra Low-Power Device Module */
532dd285b06SPaolo Bonzini static uint64_t omap_ulpd_pm_read(void *opaque, hwaddr addr,
533dd285b06SPaolo Bonzini                                   unsigned size)
534dd285b06SPaolo Bonzini {
535dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
536dd285b06SPaolo Bonzini     uint16_t ret;
537dd285b06SPaolo Bonzini 
538dd285b06SPaolo Bonzini     if (size != 2) {
539dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
540dd285b06SPaolo Bonzini     }
541dd285b06SPaolo Bonzini 
542dd285b06SPaolo Bonzini     switch (addr) {
543dd285b06SPaolo Bonzini     case 0x14:	/* IT_STATUS */
544dd285b06SPaolo Bonzini         ret = s->ulpd_pm_regs[addr >> 2];
545dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = 0;
546dd285b06SPaolo Bonzini         qemu_irq_lower(qdev_get_gpio_in(s->ih[1], OMAP_INT_GAUGE_32K));
547dd285b06SPaolo Bonzini         return ret;
548dd285b06SPaolo Bonzini 
549dd285b06SPaolo Bonzini     case 0x18:	/* Reserved */
550dd285b06SPaolo Bonzini     case 0x1c:	/* Reserved */
551dd285b06SPaolo Bonzini     case 0x20:	/* Reserved */
552dd285b06SPaolo Bonzini     case 0x28:	/* Reserved */
553dd285b06SPaolo Bonzini     case 0x2c:	/* Reserved */
554dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
555dd285b06SPaolo Bonzini         /* fall through */
556dd285b06SPaolo Bonzini     case 0x00:	/* COUNTER_32_LSB */
557dd285b06SPaolo Bonzini     case 0x04:	/* COUNTER_32_MSB */
558dd285b06SPaolo Bonzini     case 0x08:	/* COUNTER_HIGH_FREQ_LSB */
559dd285b06SPaolo Bonzini     case 0x0c:	/* COUNTER_HIGH_FREQ_MSB */
560dd285b06SPaolo Bonzini     case 0x10:	/* GAUGING_CTRL */
561dd285b06SPaolo Bonzini     case 0x24:	/* SETUP_ANALOG_CELL3_ULPD1 */
562dd285b06SPaolo Bonzini     case 0x30:	/* CLOCK_CTRL */
563dd285b06SPaolo Bonzini     case 0x34:	/* SOFT_REQ */
564dd285b06SPaolo Bonzini     case 0x38:	/* COUNTER_32_FIQ */
565dd285b06SPaolo Bonzini     case 0x3c:	/* DPLL_CTRL */
566dd285b06SPaolo Bonzini     case 0x40:	/* STATUS_REQ */
567dd285b06SPaolo Bonzini         /* XXX: check clk::usecount state for every clock */
568dd285b06SPaolo Bonzini     case 0x48:	/* LOCL_TIME */
569dd285b06SPaolo Bonzini     case 0x4c:	/* APLL_CTRL */
570dd285b06SPaolo Bonzini     case 0x50:	/* POWER_CTRL */
571dd285b06SPaolo Bonzini         return s->ulpd_pm_regs[addr >> 2];
572dd285b06SPaolo Bonzini     }
573dd285b06SPaolo Bonzini 
574dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
575dd285b06SPaolo Bonzini     return 0;
576dd285b06SPaolo Bonzini }
577dd285b06SPaolo Bonzini 
578dd285b06SPaolo Bonzini static inline void omap_ulpd_clk_update(struct omap_mpu_state_s *s,
579dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
580dd285b06SPaolo Bonzini {
581dd285b06SPaolo Bonzini     if (diff & (1 << 4))				/* USB_MCLK_EN */
582dd285b06SPaolo Bonzini         omap_clk_onoff(omap_findclk(s, "usb_clk0"), (value >> 4) & 1);
583dd285b06SPaolo Bonzini     if (diff & (1 << 5))				/* DIS_USB_PVCI_CLK */
584dd285b06SPaolo Bonzini         omap_clk_onoff(omap_findclk(s, "usb_w2fc_ck"), (~value >> 5) & 1);
585dd285b06SPaolo Bonzini }
586dd285b06SPaolo Bonzini 
587dd285b06SPaolo Bonzini static inline void omap_ulpd_req_update(struct omap_mpu_state_s *s,
588dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
589dd285b06SPaolo Bonzini {
590dd285b06SPaolo Bonzini     if (diff & (1 << 0))				/* SOFT_DPLL_REQ */
591dd285b06SPaolo Bonzini         omap_clk_canidle(omap_findclk(s, "dpll4"), (~value >> 0) & 1);
592dd285b06SPaolo Bonzini     if (diff & (1 << 1))				/* SOFT_COM_REQ */
593dd285b06SPaolo Bonzini         omap_clk_canidle(omap_findclk(s, "com_mclk_out"), (~value >> 1) & 1);
594dd285b06SPaolo Bonzini     if (diff & (1 << 2))				/* SOFT_SDW_REQ */
595dd285b06SPaolo Bonzini         omap_clk_canidle(omap_findclk(s, "bt_mclk_out"), (~value >> 2) & 1);
596dd285b06SPaolo Bonzini     if (diff & (1 << 3))				/* SOFT_USB_REQ */
597dd285b06SPaolo Bonzini         omap_clk_canidle(omap_findclk(s, "usb_clk0"), (~value >> 3) & 1);
598dd285b06SPaolo Bonzini }
599dd285b06SPaolo Bonzini 
600dd285b06SPaolo Bonzini static void omap_ulpd_pm_write(void *opaque, hwaddr addr,
601dd285b06SPaolo Bonzini                                uint64_t value, unsigned size)
602dd285b06SPaolo Bonzini {
603dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
604dd285b06SPaolo Bonzini     int64_t now, ticks;
605dd285b06SPaolo Bonzini     int div, mult;
606dd285b06SPaolo Bonzini     static const int bypass_div[4] = { 1, 2, 4, 4 };
607dd285b06SPaolo Bonzini     uint16_t diff;
608dd285b06SPaolo Bonzini 
609dd285b06SPaolo Bonzini     if (size != 2) {
61077a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
61177a8257eSStefan Weil         return;
612dd285b06SPaolo Bonzini     }
613dd285b06SPaolo Bonzini 
614dd285b06SPaolo Bonzini     switch (addr) {
615dd285b06SPaolo Bonzini     case 0x00:	/* COUNTER_32_LSB */
616dd285b06SPaolo Bonzini     case 0x04:	/* COUNTER_32_MSB */
617dd285b06SPaolo Bonzini     case 0x08:	/* COUNTER_HIGH_FREQ_LSB */
618dd285b06SPaolo Bonzini     case 0x0c:	/* COUNTER_HIGH_FREQ_MSB */
619dd285b06SPaolo Bonzini     case 0x14:	/* IT_STATUS */
620dd285b06SPaolo Bonzini     case 0x40:	/* STATUS_REQ */
621dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
622dd285b06SPaolo Bonzini         break;
623dd285b06SPaolo Bonzini 
624dd285b06SPaolo Bonzini     case 0x10:	/* GAUGING_CTRL */
625dd285b06SPaolo Bonzini         /* Bits 0 and 1 seem to be confused in the OMAP 310 TRM */
626dd285b06SPaolo Bonzini         if ((s->ulpd_pm_regs[addr >> 2] ^ value) & 1) {
627bc72ad67SAlex Bligh             now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
628dd285b06SPaolo Bonzini 
629dd285b06SPaolo Bonzini             if (value & 1)
630dd285b06SPaolo Bonzini                 s->ulpd_gauge_start = now;
631dd285b06SPaolo Bonzini             else {
632dd285b06SPaolo Bonzini                 now -= s->ulpd_gauge_start;
633dd285b06SPaolo Bonzini 
634dd285b06SPaolo Bonzini                 /* 32-kHz ticks */
63573bcb24dSRutuja Shah                 ticks = muldiv64(now, 32768, NANOSECONDS_PER_SECOND);
636dd285b06SPaolo Bonzini                 s->ulpd_pm_regs[0x00 >> 2] = (ticks >>  0) & 0xffff;
637dd285b06SPaolo Bonzini                 s->ulpd_pm_regs[0x04 >> 2] = (ticks >> 16) & 0xffff;
638dd285b06SPaolo Bonzini                 if (ticks >> 32)	/* OVERFLOW_32K */
639dd285b06SPaolo Bonzini                     s->ulpd_pm_regs[0x14 >> 2] |= 1 << 2;
640dd285b06SPaolo Bonzini 
641dd285b06SPaolo Bonzini                 /* High frequency ticks */
64273bcb24dSRutuja Shah                 ticks = muldiv64(now, 12000000, NANOSECONDS_PER_SECOND);
643dd285b06SPaolo Bonzini                 s->ulpd_pm_regs[0x08 >> 2] = (ticks >>  0) & 0xffff;
644dd285b06SPaolo Bonzini                 s->ulpd_pm_regs[0x0c >> 2] = (ticks >> 16) & 0xffff;
645dd285b06SPaolo Bonzini                 if (ticks >> 32)	/* OVERFLOW_HI_FREQ */
646dd285b06SPaolo Bonzini                     s->ulpd_pm_regs[0x14 >> 2] |= 1 << 1;
647dd285b06SPaolo Bonzini 
648dd285b06SPaolo Bonzini                 s->ulpd_pm_regs[0x14 >> 2] |= 1 << 0;	/* IT_GAUGING */
649dd285b06SPaolo Bonzini                 qemu_irq_raise(qdev_get_gpio_in(s->ih[1], OMAP_INT_GAUGE_32K));
650dd285b06SPaolo Bonzini             }
651dd285b06SPaolo Bonzini         }
652dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value;
653dd285b06SPaolo Bonzini         break;
654dd285b06SPaolo Bonzini 
655dd285b06SPaolo Bonzini     case 0x18:	/* Reserved */
656dd285b06SPaolo Bonzini     case 0x1c:	/* Reserved */
657dd285b06SPaolo Bonzini     case 0x20:	/* Reserved */
658dd285b06SPaolo Bonzini     case 0x28:	/* Reserved */
659dd285b06SPaolo Bonzini     case 0x2c:	/* Reserved */
660dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
661dd285b06SPaolo Bonzini         /* fall through */
662dd285b06SPaolo Bonzini     case 0x24:	/* SETUP_ANALOG_CELL3_ULPD1 */
663dd285b06SPaolo Bonzini     case 0x38:	/* COUNTER_32_FIQ */
664dd285b06SPaolo Bonzini     case 0x48:	/* LOCL_TIME */
665dd285b06SPaolo Bonzini     case 0x50:	/* POWER_CTRL */
666dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value;
667dd285b06SPaolo Bonzini         break;
668dd285b06SPaolo Bonzini 
669dd285b06SPaolo Bonzini     case 0x30:	/* CLOCK_CTRL */
670dd285b06SPaolo Bonzini         diff = s->ulpd_pm_regs[addr >> 2] ^ value;
671dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value & 0x3f;
672dd285b06SPaolo Bonzini         omap_ulpd_clk_update(s, diff, value);
673dd285b06SPaolo Bonzini         break;
674dd285b06SPaolo Bonzini 
675dd285b06SPaolo Bonzini     case 0x34:	/* SOFT_REQ */
676dd285b06SPaolo Bonzini         diff = s->ulpd_pm_regs[addr >> 2] ^ value;
677dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value & 0x1f;
678dd285b06SPaolo Bonzini         omap_ulpd_req_update(s, diff, value);
679dd285b06SPaolo Bonzini         break;
680dd285b06SPaolo Bonzini 
681dd285b06SPaolo Bonzini     case 0x3c:	/* DPLL_CTRL */
682dd285b06SPaolo Bonzini         /* XXX: OMAP310 TRM claims bit 3 is PLL_ENABLE, and bit 4 is
683dd285b06SPaolo Bonzini          * omitted altogether, probably a typo.  */
684dd285b06SPaolo Bonzini         /* This register has identical semantics with DPLL(1:3) control
685dd285b06SPaolo Bonzini          * registers, see omap_dpll_write() */
686dd285b06SPaolo Bonzini         diff = s->ulpd_pm_regs[addr >> 2] & value;
687dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value & 0x2fff;
688dd285b06SPaolo Bonzini         if (diff & (0x3ff << 2)) {
689dd285b06SPaolo Bonzini             if (value & (1 << 4)) {			/* PLL_ENABLE */
690dd285b06SPaolo Bonzini                 div = ((value >> 5) & 3) + 1;		/* PLL_DIV */
691dd285b06SPaolo Bonzini                 mult = MIN((value >> 7) & 0x1f, 1);	/* PLL_MULT */
692dd285b06SPaolo Bonzini             } else {
693dd285b06SPaolo Bonzini                 div = bypass_div[((value >> 2) & 3)];	/* BYPASS_DIV */
694dd285b06SPaolo Bonzini                 mult = 1;
695dd285b06SPaolo Bonzini             }
696dd285b06SPaolo Bonzini             omap_clk_setrate(omap_findclk(s, "dpll4"), div, mult);
697dd285b06SPaolo Bonzini         }
698dd285b06SPaolo Bonzini 
699dd285b06SPaolo Bonzini         /* Enter the desired mode.  */
700dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] =
701dd285b06SPaolo Bonzini                 (s->ulpd_pm_regs[addr >> 2] & 0xfffe) |
702dd285b06SPaolo Bonzini                 ((s->ulpd_pm_regs[addr >> 2] >> 4) & 1);
703dd285b06SPaolo Bonzini 
704dd285b06SPaolo Bonzini         /* Act as if the lock is restored.  */
705dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] |= 2;
706dd285b06SPaolo Bonzini         break;
707dd285b06SPaolo Bonzini 
708dd285b06SPaolo Bonzini     case 0x4c:	/* APLL_CTRL */
709dd285b06SPaolo Bonzini         diff = s->ulpd_pm_regs[addr >> 2] & value;
710dd285b06SPaolo Bonzini         s->ulpd_pm_regs[addr >> 2] = value & 0xf;
711dd285b06SPaolo Bonzini         if (diff & (1 << 0))				/* APLL_NDPLL_SWITCH */
712dd285b06SPaolo Bonzini             omap_clk_reparent(omap_findclk(s, "ck_48m"), omap_findclk(s,
713dd285b06SPaolo Bonzini                                     (value & (1 << 0)) ? "apll" : "dpll4"));
714dd285b06SPaolo Bonzini         break;
715dd285b06SPaolo Bonzini 
716dd285b06SPaolo Bonzini     default:
717dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
718dd285b06SPaolo Bonzini     }
719dd285b06SPaolo Bonzini }
720dd285b06SPaolo Bonzini 
721dd285b06SPaolo Bonzini static const MemoryRegionOps omap_ulpd_pm_ops = {
722dd285b06SPaolo Bonzini     .read = omap_ulpd_pm_read,
723dd285b06SPaolo Bonzini     .write = omap_ulpd_pm_write,
724dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
725dd285b06SPaolo Bonzini };
726dd285b06SPaolo Bonzini 
727dd285b06SPaolo Bonzini static void omap_ulpd_pm_reset(struct omap_mpu_state_s *mpu)
728dd285b06SPaolo Bonzini {
729dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x00 >> 2] = 0x0001;
730dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x04 >> 2] = 0x0000;
731dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x08 >> 2] = 0x0001;
732dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x0c >> 2] = 0x0000;
733dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x10 >> 2] = 0x0000;
734dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x18 >> 2] = 0x01;
735dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x1c >> 2] = 0x01;
736dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x20 >> 2] = 0x01;
737dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x24 >> 2] = 0x03ff;
738dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x28 >> 2] = 0x01;
739dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x2c >> 2] = 0x01;
740dd285b06SPaolo Bonzini     omap_ulpd_clk_update(mpu, mpu->ulpd_pm_regs[0x30 >> 2], 0x0000);
741dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x30 >> 2] = 0x0000;
742dd285b06SPaolo Bonzini     omap_ulpd_req_update(mpu, mpu->ulpd_pm_regs[0x34 >> 2], 0x0000);
743dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x34 >> 2] = 0x0000;
744dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x38 >> 2] = 0x0001;
745dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x3c >> 2] = 0x2211;
746dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x40 >> 2] = 0x0000; /* FIXME: dump a real STATUS_REQ */
747dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x48 >> 2] = 0x960;
748dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x4c >> 2] = 0x08;
749dd285b06SPaolo Bonzini     mpu->ulpd_pm_regs[0x50 >> 2] = 0x08;
750dd285b06SPaolo Bonzini     omap_clk_setrate(omap_findclk(mpu, "dpll4"), 1, 4);
751dd285b06SPaolo Bonzini     omap_clk_reparent(omap_findclk(mpu, "ck_48m"), omap_findclk(mpu, "dpll4"));
752dd285b06SPaolo Bonzini }
753dd285b06SPaolo Bonzini 
754dd285b06SPaolo Bonzini static void omap_ulpd_pm_init(MemoryRegion *system_memory,
755dd285b06SPaolo Bonzini                 hwaddr base,
756dd285b06SPaolo Bonzini                 struct omap_mpu_state_s *mpu)
757dd285b06SPaolo Bonzini {
7582c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->ulpd_pm_iomem, NULL, &omap_ulpd_pm_ops, mpu,
759dd285b06SPaolo Bonzini                           "omap-ulpd-pm", 0x800);
760dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &mpu->ulpd_pm_iomem);
761dd285b06SPaolo Bonzini     omap_ulpd_pm_reset(mpu);
762dd285b06SPaolo Bonzini }
763dd285b06SPaolo Bonzini 
764dd285b06SPaolo Bonzini /* OMAP Pin Configuration */
765dd285b06SPaolo Bonzini static uint64_t omap_pin_cfg_read(void *opaque, hwaddr addr,
766dd285b06SPaolo Bonzini                                   unsigned size)
767dd285b06SPaolo Bonzini {
768dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
769dd285b06SPaolo Bonzini 
770dd285b06SPaolo Bonzini     if (size != 4) {
771dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
772dd285b06SPaolo Bonzini     }
773dd285b06SPaolo Bonzini 
774dd285b06SPaolo Bonzini     switch (addr) {
775dd285b06SPaolo Bonzini     case 0x00:	/* FUNC_MUX_CTRL_0 */
776dd285b06SPaolo Bonzini     case 0x04:	/* FUNC_MUX_CTRL_1 */
777dd285b06SPaolo Bonzini     case 0x08:	/* FUNC_MUX_CTRL_2 */
778dd285b06SPaolo Bonzini         return s->func_mux_ctrl[addr >> 2];
779dd285b06SPaolo Bonzini 
780dd285b06SPaolo Bonzini     case 0x0c:	/* COMP_MODE_CTRL_0 */
781dd285b06SPaolo Bonzini         return s->comp_mode_ctrl[0];
782dd285b06SPaolo Bonzini 
783dd285b06SPaolo Bonzini     case 0x10:	/* FUNC_MUX_CTRL_3 */
784dd285b06SPaolo Bonzini     case 0x14:	/* FUNC_MUX_CTRL_4 */
785dd285b06SPaolo Bonzini     case 0x18:	/* FUNC_MUX_CTRL_5 */
786dd285b06SPaolo Bonzini     case 0x1c:	/* FUNC_MUX_CTRL_6 */
787dd285b06SPaolo Bonzini     case 0x20:	/* FUNC_MUX_CTRL_7 */
788dd285b06SPaolo Bonzini     case 0x24:	/* FUNC_MUX_CTRL_8 */
789dd285b06SPaolo Bonzini     case 0x28:	/* FUNC_MUX_CTRL_9 */
790dd285b06SPaolo Bonzini     case 0x2c:	/* FUNC_MUX_CTRL_A */
791dd285b06SPaolo Bonzini     case 0x30:	/* FUNC_MUX_CTRL_B */
792dd285b06SPaolo Bonzini     case 0x34:	/* FUNC_MUX_CTRL_C */
793dd285b06SPaolo Bonzini     case 0x38:	/* FUNC_MUX_CTRL_D */
794dd285b06SPaolo Bonzini         return s->func_mux_ctrl[(addr >> 2) - 1];
795dd285b06SPaolo Bonzini 
796dd285b06SPaolo Bonzini     case 0x40:	/* PULL_DWN_CTRL_0 */
797dd285b06SPaolo Bonzini     case 0x44:	/* PULL_DWN_CTRL_1 */
798dd285b06SPaolo Bonzini     case 0x48:	/* PULL_DWN_CTRL_2 */
799dd285b06SPaolo Bonzini     case 0x4c:	/* PULL_DWN_CTRL_3 */
800dd285b06SPaolo Bonzini         return s->pull_dwn_ctrl[(addr & 0xf) >> 2];
801dd285b06SPaolo Bonzini 
802dd285b06SPaolo Bonzini     case 0x50:	/* GATE_INH_CTRL_0 */
803dd285b06SPaolo Bonzini         return s->gate_inh_ctrl[0];
804dd285b06SPaolo Bonzini 
805dd285b06SPaolo Bonzini     case 0x60:	/* VOLTAGE_CTRL_0 */
806dd285b06SPaolo Bonzini         return s->voltage_ctrl[0];
807dd285b06SPaolo Bonzini 
808dd285b06SPaolo Bonzini     case 0x70:	/* TEST_DBG_CTRL_0 */
809dd285b06SPaolo Bonzini         return s->test_dbg_ctrl[0];
810dd285b06SPaolo Bonzini 
811dd285b06SPaolo Bonzini     case 0x80:	/* MOD_CONF_CTRL_0 */
812dd285b06SPaolo Bonzini         return s->mod_conf_ctrl[0];
813dd285b06SPaolo Bonzini     }
814dd285b06SPaolo Bonzini 
815dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
816dd285b06SPaolo Bonzini     return 0;
817dd285b06SPaolo Bonzini }
818dd285b06SPaolo Bonzini 
819dd285b06SPaolo Bonzini static inline void omap_pin_funcmux0_update(struct omap_mpu_state_s *s,
820dd285b06SPaolo Bonzini                 uint32_t diff, uint32_t value)
821dd285b06SPaolo Bonzini {
822dd285b06SPaolo Bonzini     if (s->compat1509) {
823dd285b06SPaolo Bonzini         if (diff & (1 << 9))			/* BLUETOOTH */
824dd285b06SPaolo Bonzini             omap_clk_onoff(omap_findclk(s, "bt_mclk_out"),
825dd285b06SPaolo Bonzini                             (~value >> 9) & 1);
826dd285b06SPaolo Bonzini         if (diff & (1 << 7))			/* USB.CLKO */
827dd285b06SPaolo Bonzini             omap_clk_onoff(omap_findclk(s, "usb.clko"),
828dd285b06SPaolo Bonzini                             (value >> 7) & 1);
829dd285b06SPaolo Bonzini     }
830dd285b06SPaolo Bonzini }
831dd285b06SPaolo Bonzini 
832dd285b06SPaolo Bonzini static inline void omap_pin_funcmux1_update(struct omap_mpu_state_s *s,
833dd285b06SPaolo Bonzini                 uint32_t diff, uint32_t value)
834dd285b06SPaolo Bonzini {
835dd285b06SPaolo Bonzini     if (s->compat1509) {
836d2f41a11SPeter Maydell         if (diff & (1U << 31)) {
837d2f41a11SPeter Maydell             /* MCBSP3_CLK_HIZ_DI */
838d2f41a11SPeter Maydell             omap_clk_onoff(omap_findclk(s, "mcbsp3.clkx"), (value >> 31) & 1);
839d2f41a11SPeter Maydell         }
840d2f41a11SPeter Maydell         if (diff & (1 << 1)) {
841d2f41a11SPeter Maydell             /* CLK32K */
842d2f41a11SPeter Maydell             omap_clk_onoff(omap_findclk(s, "clk32k_out"), (~value >> 1) & 1);
843d2f41a11SPeter Maydell         }
844dd285b06SPaolo Bonzini     }
845dd285b06SPaolo Bonzini }
846dd285b06SPaolo Bonzini 
847dd285b06SPaolo Bonzini static inline void omap_pin_modconf1_update(struct omap_mpu_state_s *s,
848dd285b06SPaolo Bonzini                 uint32_t diff, uint32_t value)
849dd285b06SPaolo Bonzini {
850d2f41a11SPeter Maydell     if (diff & (1U << 31)) {
851d2f41a11SPeter Maydell         /* CONF_MOD_UART3_CLK_MODE_R */
852dd285b06SPaolo Bonzini         omap_clk_reparent(omap_findclk(s, "uart3_ck"),
853dd285b06SPaolo Bonzini                           omap_findclk(s, ((value >> 31) & 1) ?
854dd285b06SPaolo Bonzini                                        "ck_48m" : "armper_ck"));
855d2f41a11SPeter Maydell     }
856dd285b06SPaolo Bonzini     if (diff & (1 << 30))			/* CONF_MOD_UART2_CLK_MODE_R */
857dd285b06SPaolo Bonzini          omap_clk_reparent(omap_findclk(s, "uart2_ck"),
858dd285b06SPaolo Bonzini                          omap_findclk(s, ((value >> 30) & 1) ?
859dd285b06SPaolo Bonzini                                  "ck_48m" : "armper_ck"));
860dd285b06SPaolo Bonzini     if (diff & (1 << 29))			/* CONF_MOD_UART1_CLK_MODE_R */
861dd285b06SPaolo Bonzini          omap_clk_reparent(omap_findclk(s, "uart1_ck"),
862dd285b06SPaolo Bonzini                          omap_findclk(s, ((value >> 29) & 1) ?
863dd285b06SPaolo Bonzini                                  "ck_48m" : "armper_ck"));
864dd285b06SPaolo Bonzini     if (diff & (1 << 23))			/* CONF_MOD_MMC_SD_CLK_REQ_R */
865dd285b06SPaolo Bonzini          omap_clk_reparent(omap_findclk(s, "mmc_ck"),
866dd285b06SPaolo Bonzini                          omap_findclk(s, ((value >> 23) & 1) ?
867dd285b06SPaolo Bonzini                                  "ck_48m" : "armper_ck"));
868dd285b06SPaolo Bonzini     if (diff & (1 << 12))			/* CONF_MOD_COM_MCLK_12_48_S */
869dd285b06SPaolo Bonzini          omap_clk_reparent(omap_findclk(s, "com_mclk_out"),
870dd285b06SPaolo Bonzini                          omap_findclk(s, ((value >> 12) & 1) ?
871dd285b06SPaolo Bonzini                                  "ck_48m" : "armper_ck"));
872dd285b06SPaolo Bonzini     if (diff & (1 << 9))			/* CONF_MOD_USB_HOST_HHC_UHO */
873dd285b06SPaolo Bonzini          omap_clk_onoff(omap_findclk(s, "usb_hhc_ck"), (value >> 9) & 1);
874dd285b06SPaolo Bonzini }
875dd285b06SPaolo Bonzini 
876dd285b06SPaolo Bonzini static void omap_pin_cfg_write(void *opaque, hwaddr addr,
877dd285b06SPaolo Bonzini                                uint64_t value, unsigned size)
878dd285b06SPaolo Bonzini {
879dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
880dd285b06SPaolo Bonzini     uint32_t diff;
881dd285b06SPaolo Bonzini 
882dd285b06SPaolo Bonzini     if (size != 4) {
88377a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
88477a8257eSStefan Weil         return;
885dd285b06SPaolo Bonzini     }
886dd285b06SPaolo Bonzini 
887dd285b06SPaolo Bonzini     switch (addr) {
888dd285b06SPaolo Bonzini     case 0x00:	/* FUNC_MUX_CTRL_0 */
889dd285b06SPaolo Bonzini         diff = s->func_mux_ctrl[addr >> 2] ^ value;
890dd285b06SPaolo Bonzini         s->func_mux_ctrl[addr >> 2] = value;
891dd285b06SPaolo Bonzini         omap_pin_funcmux0_update(s, diff, value);
892dd285b06SPaolo Bonzini         return;
893dd285b06SPaolo Bonzini 
894dd285b06SPaolo Bonzini     case 0x04:	/* FUNC_MUX_CTRL_1 */
895dd285b06SPaolo Bonzini         diff = s->func_mux_ctrl[addr >> 2] ^ value;
896dd285b06SPaolo Bonzini         s->func_mux_ctrl[addr >> 2] = value;
897dd285b06SPaolo Bonzini         omap_pin_funcmux1_update(s, diff, value);
898dd285b06SPaolo Bonzini         return;
899dd285b06SPaolo Bonzini 
900dd285b06SPaolo Bonzini     case 0x08:	/* FUNC_MUX_CTRL_2 */
901dd285b06SPaolo Bonzini         s->func_mux_ctrl[addr >> 2] = value;
902dd285b06SPaolo Bonzini         return;
903dd285b06SPaolo Bonzini 
904dd285b06SPaolo Bonzini     case 0x0c:	/* COMP_MODE_CTRL_0 */
905dd285b06SPaolo Bonzini         s->comp_mode_ctrl[0] = value;
906dd285b06SPaolo Bonzini         s->compat1509 = (value != 0x0000eaef);
907dd285b06SPaolo Bonzini         omap_pin_funcmux0_update(s, ~0, s->func_mux_ctrl[0]);
908dd285b06SPaolo Bonzini         omap_pin_funcmux1_update(s, ~0, s->func_mux_ctrl[1]);
909dd285b06SPaolo Bonzini         return;
910dd285b06SPaolo Bonzini 
911dd285b06SPaolo Bonzini     case 0x10:	/* FUNC_MUX_CTRL_3 */
912dd285b06SPaolo Bonzini     case 0x14:	/* FUNC_MUX_CTRL_4 */
913dd285b06SPaolo Bonzini     case 0x18:	/* FUNC_MUX_CTRL_5 */
914dd285b06SPaolo Bonzini     case 0x1c:	/* FUNC_MUX_CTRL_6 */
915dd285b06SPaolo Bonzini     case 0x20:	/* FUNC_MUX_CTRL_7 */
916dd285b06SPaolo Bonzini     case 0x24:	/* FUNC_MUX_CTRL_8 */
917dd285b06SPaolo Bonzini     case 0x28:	/* FUNC_MUX_CTRL_9 */
918dd285b06SPaolo Bonzini     case 0x2c:	/* FUNC_MUX_CTRL_A */
919dd285b06SPaolo Bonzini     case 0x30:	/* FUNC_MUX_CTRL_B */
920dd285b06SPaolo Bonzini     case 0x34:	/* FUNC_MUX_CTRL_C */
921dd285b06SPaolo Bonzini     case 0x38:	/* FUNC_MUX_CTRL_D */
922dd285b06SPaolo Bonzini         s->func_mux_ctrl[(addr >> 2) - 1] = value;
923dd285b06SPaolo Bonzini         return;
924dd285b06SPaolo Bonzini 
925dd285b06SPaolo Bonzini     case 0x40:	/* PULL_DWN_CTRL_0 */
926dd285b06SPaolo Bonzini     case 0x44:	/* PULL_DWN_CTRL_1 */
927dd285b06SPaolo Bonzini     case 0x48:	/* PULL_DWN_CTRL_2 */
928dd285b06SPaolo Bonzini     case 0x4c:	/* PULL_DWN_CTRL_3 */
929dd285b06SPaolo Bonzini         s->pull_dwn_ctrl[(addr & 0xf) >> 2] = value;
930dd285b06SPaolo Bonzini         return;
931dd285b06SPaolo Bonzini 
932dd285b06SPaolo Bonzini     case 0x50:	/* GATE_INH_CTRL_0 */
933dd285b06SPaolo Bonzini         s->gate_inh_ctrl[0] = value;
934dd285b06SPaolo Bonzini         return;
935dd285b06SPaolo Bonzini 
936dd285b06SPaolo Bonzini     case 0x60:	/* VOLTAGE_CTRL_0 */
937dd285b06SPaolo Bonzini         s->voltage_ctrl[0] = value;
938dd285b06SPaolo Bonzini         return;
939dd285b06SPaolo Bonzini 
940dd285b06SPaolo Bonzini     case 0x70:	/* TEST_DBG_CTRL_0 */
941dd285b06SPaolo Bonzini         s->test_dbg_ctrl[0] = value;
942dd285b06SPaolo Bonzini         return;
943dd285b06SPaolo Bonzini 
944dd285b06SPaolo Bonzini     case 0x80:	/* MOD_CONF_CTRL_0 */
945dd285b06SPaolo Bonzini         diff = s->mod_conf_ctrl[0] ^ value;
946dd285b06SPaolo Bonzini         s->mod_conf_ctrl[0] = value;
947dd285b06SPaolo Bonzini         omap_pin_modconf1_update(s, diff, value);
948dd285b06SPaolo Bonzini         return;
949dd285b06SPaolo Bonzini 
950dd285b06SPaolo Bonzini     default:
951dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
952dd285b06SPaolo Bonzini     }
953dd285b06SPaolo Bonzini }
954dd285b06SPaolo Bonzini 
955dd285b06SPaolo Bonzini static const MemoryRegionOps omap_pin_cfg_ops = {
956dd285b06SPaolo Bonzini     .read = omap_pin_cfg_read,
957dd285b06SPaolo Bonzini     .write = omap_pin_cfg_write,
958dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
959dd285b06SPaolo Bonzini };
960dd285b06SPaolo Bonzini 
961dd285b06SPaolo Bonzini static void omap_pin_cfg_reset(struct omap_mpu_state_s *mpu)
962dd285b06SPaolo Bonzini {
963dd285b06SPaolo Bonzini     /* Start in Compatibility Mode.  */
964dd285b06SPaolo Bonzini     mpu->compat1509 = 1;
965dd285b06SPaolo Bonzini     omap_pin_funcmux0_update(mpu, mpu->func_mux_ctrl[0], 0);
966dd285b06SPaolo Bonzini     omap_pin_funcmux1_update(mpu, mpu->func_mux_ctrl[1], 0);
967dd285b06SPaolo Bonzini     omap_pin_modconf1_update(mpu, mpu->mod_conf_ctrl[0], 0);
968dd285b06SPaolo Bonzini     memset(mpu->func_mux_ctrl, 0, sizeof(mpu->func_mux_ctrl));
969dd285b06SPaolo Bonzini     memset(mpu->comp_mode_ctrl, 0, sizeof(mpu->comp_mode_ctrl));
970dd285b06SPaolo Bonzini     memset(mpu->pull_dwn_ctrl, 0, sizeof(mpu->pull_dwn_ctrl));
971dd285b06SPaolo Bonzini     memset(mpu->gate_inh_ctrl, 0, sizeof(mpu->gate_inh_ctrl));
972dd285b06SPaolo Bonzini     memset(mpu->voltage_ctrl, 0, sizeof(mpu->voltage_ctrl));
973dd285b06SPaolo Bonzini     memset(mpu->test_dbg_ctrl, 0, sizeof(mpu->test_dbg_ctrl));
974dd285b06SPaolo Bonzini     memset(mpu->mod_conf_ctrl, 0, sizeof(mpu->mod_conf_ctrl));
975dd285b06SPaolo Bonzini }
976dd285b06SPaolo Bonzini 
977dd285b06SPaolo Bonzini static void omap_pin_cfg_init(MemoryRegion *system_memory,
978dd285b06SPaolo Bonzini                 hwaddr base,
979dd285b06SPaolo Bonzini                 struct omap_mpu_state_s *mpu)
980dd285b06SPaolo Bonzini {
9812c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->pin_cfg_iomem, NULL, &omap_pin_cfg_ops, mpu,
982dd285b06SPaolo Bonzini                           "omap-pin-cfg", 0x800);
983dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &mpu->pin_cfg_iomem);
984dd285b06SPaolo Bonzini     omap_pin_cfg_reset(mpu);
985dd285b06SPaolo Bonzini }
986dd285b06SPaolo Bonzini 
987dd285b06SPaolo Bonzini /* Device Identification, Die Identification */
988dd285b06SPaolo Bonzini static uint64_t omap_id_read(void *opaque, hwaddr addr,
989dd285b06SPaolo Bonzini                              unsigned size)
990dd285b06SPaolo Bonzini {
991dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
992dd285b06SPaolo Bonzini 
993dd285b06SPaolo Bonzini     if (size != 4) {
994dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
995dd285b06SPaolo Bonzini     }
996dd285b06SPaolo Bonzini 
997dd285b06SPaolo Bonzini     switch (addr) {
998dd285b06SPaolo Bonzini     case 0xfffe1800:	/* DIE_ID_LSB */
999dd285b06SPaolo Bonzini         return 0xc9581f0e;
1000dd285b06SPaolo Bonzini     case 0xfffe1804:	/* DIE_ID_MSB */
1001dd285b06SPaolo Bonzini         return 0xa8858bfa;
1002dd285b06SPaolo Bonzini 
1003dd285b06SPaolo Bonzini     case 0xfffe2000:	/* PRODUCT_ID_LSB */
1004dd285b06SPaolo Bonzini         return 0x00aaaafc;
1005dd285b06SPaolo Bonzini     case 0xfffe2004:	/* PRODUCT_ID_MSB */
1006dd285b06SPaolo Bonzini         return 0xcafeb574;
1007dd285b06SPaolo Bonzini 
1008dd285b06SPaolo Bonzini     case 0xfffed400:	/* JTAG_ID_LSB */
1009dd285b06SPaolo Bonzini         switch (s->mpu_model) {
1010dd285b06SPaolo Bonzini         case omap310:
1011dd285b06SPaolo Bonzini             return 0x03310315;
1012dd285b06SPaolo Bonzini         case omap1510:
1013dd285b06SPaolo Bonzini             return 0x03310115;
1014dd285b06SPaolo Bonzini         default:
1015a89f364aSAlistair Francis             hw_error("%s: bad mpu model\n", __func__);
1016dd285b06SPaolo Bonzini         }
1017dd285b06SPaolo Bonzini         break;
1018dd285b06SPaolo Bonzini 
1019dd285b06SPaolo Bonzini     case 0xfffed404:	/* JTAG_ID_MSB */
1020dd285b06SPaolo Bonzini         switch (s->mpu_model) {
1021dd285b06SPaolo Bonzini         case omap310:
1022dd285b06SPaolo Bonzini             return 0xfb57402f;
1023dd285b06SPaolo Bonzini         case omap1510:
1024dd285b06SPaolo Bonzini             return 0xfb47002f;
1025dd285b06SPaolo Bonzini         default:
1026a89f364aSAlistair Francis             hw_error("%s: bad mpu model\n", __func__);
1027dd285b06SPaolo Bonzini         }
1028dd285b06SPaolo Bonzini         break;
1029dd285b06SPaolo Bonzini     }
1030dd285b06SPaolo Bonzini 
1031dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1032dd285b06SPaolo Bonzini     return 0;
1033dd285b06SPaolo Bonzini }
1034dd285b06SPaolo Bonzini 
1035dd285b06SPaolo Bonzini static void omap_id_write(void *opaque, hwaddr addr,
1036dd285b06SPaolo Bonzini                           uint64_t value, unsigned size)
1037dd285b06SPaolo Bonzini {
1038dd285b06SPaolo Bonzini     if (size != 4) {
103977a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
104077a8257eSStefan Weil         return;
1041dd285b06SPaolo Bonzini     }
1042dd285b06SPaolo Bonzini 
1043dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1044dd285b06SPaolo Bonzini }
1045dd285b06SPaolo Bonzini 
1046dd285b06SPaolo Bonzini static const MemoryRegionOps omap_id_ops = {
1047dd285b06SPaolo Bonzini     .read = omap_id_read,
1048dd285b06SPaolo Bonzini     .write = omap_id_write,
1049dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1050dd285b06SPaolo Bonzini };
1051dd285b06SPaolo Bonzini 
1052dd285b06SPaolo Bonzini static void omap_id_init(MemoryRegion *memory, struct omap_mpu_state_s *mpu)
1053dd285b06SPaolo Bonzini {
10542c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->id_iomem, NULL, &omap_id_ops, mpu,
1055dd285b06SPaolo Bonzini                           "omap-id", 0x100000000ULL);
10562c9b15caSPaolo Bonzini     memory_region_init_alias(&mpu->id_iomem_e18, NULL, "omap-id-e18", &mpu->id_iomem,
1057dd285b06SPaolo Bonzini                              0xfffe1800, 0x800);
1058dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, 0xfffe1800, &mpu->id_iomem_e18);
10592c9b15caSPaolo Bonzini     memory_region_init_alias(&mpu->id_iomem_ed4, NULL, "omap-id-ed4", &mpu->id_iomem,
1060dd285b06SPaolo Bonzini                              0xfffed400, 0x100);
1061dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, 0xfffed400, &mpu->id_iomem_ed4);
1062dd285b06SPaolo Bonzini     if (!cpu_is_omap15xx(mpu)) {
10632c9b15caSPaolo Bonzini         memory_region_init_alias(&mpu->id_iomem_ed4, NULL, "omap-id-e20",
1064dd285b06SPaolo Bonzini                                  &mpu->id_iomem, 0xfffe2000, 0x800);
1065dd285b06SPaolo Bonzini         memory_region_add_subregion(memory, 0xfffe2000, &mpu->id_iomem_e20);
1066dd285b06SPaolo Bonzini     }
1067dd285b06SPaolo Bonzini }
1068dd285b06SPaolo Bonzini 
1069dd285b06SPaolo Bonzini /* MPUI Control (Dummy) */
1070dd285b06SPaolo Bonzini static uint64_t omap_mpui_read(void *opaque, hwaddr addr,
1071dd285b06SPaolo Bonzini                                unsigned size)
1072dd285b06SPaolo Bonzini {
1073dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1074dd285b06SPaolo Bonzini 
1075dd285b06SPaolo Bonzini     if (size != 4) {
1076dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
1077dd285b06SPaolo Bonzini     }
1078dd285b06SPaolo Bonzini 
1079dd285b06SPaolo Bonzini     switch (addr) {
1080dd285b06SPaolo Bonzini     case 0x00:	/* CTRL */
1081dd285b06SPaolo Bonzini         return s->mpui_ctrl;
1082dd285b06SPaolo Bonzini     case 0x04:	/* DEBUG_ADDR */
1083dd285b06SPaolo Bonzini         return 0x01ffffff;
1084dd285b06SPaolo Bonzini     case 0x08:	/* DEBUG_DATA */
1085dd285b06SPaolo Bonzini         return 0xffffffff;
1086dd285b06SPaolo Bonzini     case 0x0c:	/* DEBUG_FLAG */
1087dd285b06SPaolo Bonzini         return 0x00000800;
1088dd285b06SPaolo Bonzini     case 0x10:	/* STATUS */
1089dd285b06SPaolo Bonzini         return 0x00000000;
1090dd285b06SPaolo Bonzini 
1091dd285b06SPaolo Bonzini     /* Not in OMAP310 */
1092dd285b06SPaolo Bonzini     case 0x14:	/* DSP_STATUS */
1093dd285b06SPaolo Bonzini     case 0x18:	/* DSP_BOOT_CONFIG */
1094dd285b06SPaolo Bonzini         return 0x00000000;
1095dd285b06SPaolo Bonzini     case 0x1c:	/* DSP_MPUI_CONFIG */
1096dd285b06SPaolo Bonzini         return 0x0000ffff;
1097dd285b06SPaolo Bonzini     }
1098dd285b06SPaolo Bonzini 
1099dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1100dd285b06SPaolo Bonzini     return 0;
1101dd285b06SPaolo Bonzini }
1102dd285b06SPaolo Bonzini 
1103dd285b06SPaolo Bonzini static void omap_mpui_write(void *opaque, hwaddr addr,
1104dd285b06SPaolo Bonzini                             uint64_t value, unsigned size)
1105dd285b06SPaolo Bonzini {
1106dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1107dd285b06SPaolo Bonzini 
1108dd285b06SPaolo Bonzini     if (size != 4) {
110977a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
111077a8257eSStefan Weil         return;
1111dd285b06SPaolo Bonzini     }
1112dd285b06SPaolo Bonzini 
1113dd285b06SPaolo Bonzini     switch (addr) {
1114dd285b06SPaolo Bonzini     case 0x00:	/* CTRL */
1115dd285b06SPaolo Bonzini         s->mpui_ctrl = value & 0x007fffff;
1116dd285b06SPaolo Bonzini         break;
1117dd285b06SPaolo Bonzini 
1118dd285b06SPaolo Bonzini     case 0x04:	/* DEBUG_ADDR */
1119dd285b06SPaolo Bonzini     case 0x08:	/* DEBUG_DATA */
1120dd285b06SPaolo Bonzini     case 0x0c:	/* DEBUG_FLAG */
1121dd285b06SPaolo Bonzini     case 0x10:	/* STATUS */
1122dd285b06SPaolo Bonzini     /* Not in OMAP310 */
1123dd285b06SPaolo Bonzini     case 0x14:	/* DSP_STATUS */
1124dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
1125dd285b06SPaolo Bonzini         break;
1126dd285b06SPaolo Bonzini     case 0x18:	/* DSP_BOOT_CONFIG */
1127dd285b06SPaolo Bonzini     case 0x1c:	/* DSP_MPUI_CONFIG */
1128dd285b06SPaolo Bonzini         break;
1129dd285b06SPaolo Bonzini 
1130dd285b06SPaolo Bonzini     default:
1131dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1132dd285b06SPaolo Bonzini     }
1133dd285b06SPaolo Bonzini }
1134dd285b06SPaolo Bonzini 
1135dd285b06SPaolo Bonzini static const MemoryRegionOps omap_mpui_ops = {
1136dd285b06SPaolo Bonzini     .read = omap_mpui_read,
1137dd285b06SPaolo Bonzini     .write = omap_mpui_write,
1138dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1139dd285b06SPaolo Bonzini };
1140dd285b06SPaolo Bonzini 
1141dd285b06SPaolo Bonzini static void omap_mpui_reset(struct omap_mpu_state_s *s)
1142dd285b06SPaolo Bonzini {
1143dd285b06SPaolo Bonzini     s->mpui_ctrl = 0x0003ff1b;
1144dd285b06SPaolo Bonzini }
1145dd285b06SPaolo Bonzini 
1146dd285b06SPaolo Bonzini static void omap_mpui_init(MemoryRegion *memory, hwaddr base,
1147dd285b06SPaolo Bonzini                 struct omap_mpu_state_s *mpu)
1148dd285b06SPaolo Bonzini {
11492c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->mpui_iomem, NULL, &omap_mpui_ops, mpu,
1150dd285b06SPaolo Bonzini                           "omap-mpui", 0x100);
1151dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &mpu->mpui_iomem);
1152dd285b06SPaolo Bonzini 
1153dd285b06SPaolo Bonzini     omap_mpui_reset(mpu);
1154dd285b06SPaolo Bonzini }
1155dd285b06SPaolo Bonzini 
1156dd285b06SPaolo Bonzini /* TIPB Bridges */
1157dd285b06SPaolo Bonzini struct omap_tipb_bridge_s {
1158dd285b06SPaolo Bonzini     qemu_irq abort;
1159dd285b06SPaolo Bonzini     MemoryRegion iomem;
1160dd285b06SPaolo Bonzini 
1161dd285b06SPaolo Bonzini     int width_intr;
1162dd285b06SPaolo Bonzini     uint16_t control;
1163dd285b06SPaolo Bonzini     uint16_t alloc;
1164dd285b06SPaolo Bonzini     uint16_t buffer;
1165dd285b06SPaolo Bonzini     uint16_t enh_control;
1166dd285b06SPaolo Bonzini };
1167dd285b06SPaolo Bonzini 
1168dd285b06SPaolo Bonzini static uint64_t omap_tipb_bridge_read(void *opaque, hwaddr addr,
1169dd285b06SPaolo Bonzini                                       unsigned size)
1170dd285b06SPaolo Bonzini {
1171dd285b06SPaolo Bonzini     struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1172dd285b06SPaolo Bonzini 
1173dd285b06SPaolo Bonzini     if (size < 2) {
1174dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
1175dd285b06SPaolo Bonzini     }
1176dd285b06SPaolo Bonzini 
1177dd285b06SPaolo Bonzini     switch (addr) {
1178dd285b06SPaolo Bonzini     case 0x00:	/* TIPB_CNTL */
1179dd285b06SPaolo Bonzini         return s->control;
1180dd285b06SPaolo Bonzini     case 0x04:	/* TIPB_BUS_ALLOC */
1181dd285b06SPaolo Bonzini         return s->alloc;
1182dd285b06SPaolo Bonzini     case 0x08:	/* MPU_TIPB_CNTL */
1183dd285b06SPaolo Bonzini         return s->buffer;
1184dd285b06SPaolo Bonzini     case 0x0c:	/* ENHANCED_TIPB_CNTL */
1185dd285b06SPaolo Bonzini         return s->enh_control;
1186dd285b06SPaolo Bonzini     case 0x10:	/* ADDRESS_DBG */
1187dd285b06SPaolo Bonzini     case 0x14:	/* DATA_DEBUG_LOW */
1188dd285b06SPaolo Bonzini     case 0x18:	/* DATA_DEBUG_HIGH */
1189dd285b06SPaolo Bonzini         return 0xffff;
1190dd285b06SPaolo Bonzini     case 0x1c:	/* DEBUG_CNTR_SIG */
1191dd285b06SPaolo Bonzini         return 0x00f8;
1192dd285b06SPaolo Bonzini     }
1193dd285b06SPaolo Bonzini 
1194dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1195dd285b06SPaolo Bonzini     return 0;
1196dd285b06SPaolo Bonzini }
1197dd285b06SPaolo Bonzini 
1198dd285b06SPaolo Bonzini static void omap_tipb_bridge_write(void *opaque, hwaddr addr,
1199dd285b06SPaolo Bonzini                                    uint64_t value, unsigned size)
1200dd285b06SPaolo Bonzini {
1201dd285b06SPaolo Bonzini     struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1202dd285b06SPaolo Bonzini 
1203dd285b06SPaolo Bonzini     if (size < 2) {
120477a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
120577a8257eSStefan Weil         return;
1206dd285b06SPaolo Bonzini     }
1207dd285b06SPaolo Bonzini 
1208dd285b06SPaolo Bonzini     switch (addr) {
1209dd285b06SPaolo Bonzini     case 0x00:	/* TIPB_CNTL */
1210dd285b06SPaolo Bonzini         s->control = value & 0xffff;
1211dd285b06SPaolo Bonzini         break;
1212dd285b06SPaolo Bonzini 
1213dd285b06SPaolo Bonzini     case 0x04:	/* TIPB_BUS_ALLOC */
1214dd285b06SPaolo Bonzini         s->alloc = value & 0x003f;
1215dd285b06SPaolo Bonzini         break;
1216dd285b06SPaolo Bonzini 
1217dd285b06SPaolo Bonzini     case 0x08:	/* MPU_TIPB_CNTL */
1218dd285b06SPaolo Bonzini         s->buffer = value & 0x0003;
1219dd285b06SPaolo Bonzini         break;
1220dd285b06SPaolo Bonzini 
1221dd285b06SPaolo Bonzini     case 0x0c:	/* ENHANCED_TIPB_CNTL */
1222dd285b06SPaolo Bonzini         s->width_intr = !(value & 2);
1223dd285b06SPaolo Bonzini         s->enh_control = value & 0x000f;
1224dd285b06SPaolo Bonzini         break;
1225dd285b06SPaolo Bonzini 
1226dd285b06SPaolo Bonzini     case 0x10:	/* ADDRESS_DBG */
1227dd285b06SPaolo Bonzini     case 0x14:	/* DATA_DEBUG_LOW */
1228dd285b06SPaolo Bonzini     case 0x18:	/* DATA_DEBUG_HIGH */
1229dd285b06SPaolo Bonzini     case 0x1c:	/* DEBUG_CNTR_SIG */
1230dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
1231dd285b06SPaolo Bonzini         break;
1232dd285b06SPaolo Bonzini 
1233dd285b06SPaolo Bonzini     default:
1234dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1235dd285b06SPaolo Bonzini     }
1236dd285b06SPaolo Bonzini }
1237dd285b06SPaolo Bonzini 
1238dd285b06SPaolo Bonzini static const MemoryRegionOps omap_tipb_bridge_ops = {
1239dd285b06SPaolo Bonzini     .read = omap_tipb_bridge_read,
1240dd285b06SPaolo Bonzini     .write = omap_tipb_bridge_write,
1241dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1242dd285b06SPaolo Bonzini };
1243dd285b06SPaolo Bonzini 
1244dd285b06SPaolo Bonzini static void omap_tipb_bridge_reset(struct omap_tipb_bridge_s *s)
1245dd285b06SPaolo Bonzini {
1246dd285b06SPaolo Bonzini     s->control = 0xffff;
1247dd285b06SPaolo Bonzini     s->alloc = 0x0009;
1248dd285b06SPaolo Bonzini     s->buffer = 0x0000;
1249dd285b06SPaolo Bonzini     s->enh_control = 0x000f;
1250dd285b06SPaolo Bonzini }
1251dd285b06SPaolo Bonzini 
1252dd285b06SPaolo Bonzini static struct omap_tipb_bridge_s *omap_tipb_bridge_init(
1253dd285b06SPaolo Bonzini     MemoryRegion *memory, hwaddr base,
1254dd285b06SPaolo Bonzini     qemu_irq abort_irq, omap_clk clk)
1255dd285b06SPaolo Bonzini {
1256b45c03f5SMarkus Armbruster     struct omap_tipb_bridge_s *s = g_new0(struct omap_tipb_bridge_s, 1);
1257dd285b06SPaolo Bonzini 
1258dd285b06SPaolo Bonzini     s->abort = abort_irq;
1259dd285b06SPaolo Bonzini     omap_tipb_bridge_reset(s);
1260dd285b06SPaolo Bonzini 
12612c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_tipb_bridge_ops, s,
1262dd285b06SPaolo Bonzini                           "omap-tipb-bridge", 0x100);
1263dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &s->iomem);
1264dd285b06SPaolo Bonzini 
1265dd285b06SPaolo Bonzini     return s;
1266dd285b06SPaolo Bonzini }
1267dd285b06SPaolo Bonzini 
1268dd285b06SPaolo Bonzini /* Dummy Traffic Controller's Memory Interface */
1269dd285b06SPaolo Bonzini static uint64_t omap_tcmi_read(void *opaque, hwaddr addr,
1270dd285b06SPaolo Bonzini                                unsigned size)
1271dd285b06SPaolo Bonzini {
1272dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1273dd285b06SPaolo Bonzini     uint32_t ret;
1274dd285b06SPaolo Bonzini 
1275dd285b06SPaolo Bonzini     if (size != 4) {
1276dd285b06SPaolo Bonzini         return omap_badwidth_read32(opaque, addr);
1277dd285b06SPaolo Bonzini     }
1278dd285b06SPaolo Bonzini 
1279dd285b06SPaolo Bonzini     switch (addr) {
1280dd285b06SPaolo Bonzini     case 0x00:	/* IMIF_PRIO */
1281dd285b06SPaolo Bonzini     case 0x04:	/* EMIFS_PRIO */
1282dd285b06SPaolo Bonzini     case 0x08:	/* EMIFF_PRIO */
1283dd285b06SPaolo Bonzini     case 0x0c:	/* EMIFS_CONFIG */
1284dd285b06SPaolo Bonzini     case 0x10:	/* EMIFS_CS0_CONFIG */
1285dd285b06SPaolo Bonzini     case 0x14:	/* EMIFS_CS1_CONFIG */
1286dd285b06SPaolo Bonzini     case 0x18:	/* EMIFS_CS2_CONFIG */
1287dd285b06SPaolo Bonzini     case 0x1c:	/* EMIFS_CS3_CONFIG */
1288dd285b06SPaolo Bonzini     case 0x24:	/* EMIFF_MRS */
1289dd285b06SPaolo Bonzini     case 0x28:	/* TIMEOUT1 */
1290dd285b06SPaolo Bonzini     case 0x2c:	/* TIMEOUT2 */
1291dd285b06SPaolo Bonzini     case 0x30:	/* TIMEOUT3 */
1292dd285b06SPaolo Bonzini     case 0x3c:	/* EMIFF_SDRAM_CONFIG_2 */
1293dd285b06SPaolo Bonzini     case 0x40:	/* EMIFS_CFG_DYN_WAIT */
1294dd285b06SPaolo Bonzini         return s->tcmi_regs[addr >> 2];
1295dd285b06SPaolo Bonzini 
1296dd285b06SPaolo Bonzini     case 0x20:	/* EMIFF_SDRAM_CONFIG */
1297dd285b06SPaolo Bonzini         ret = s->tcmi_regs[addr >> 2];
1298dd285b06SPaolo Bonzini         s->tcmi_regs[addr >> 2] &= ~1; /* XXX: Clear SLRF on SDRAM access */
1299dd285b06SPaolo Bonzini         /* XXX: We can try using the VGA_DIRTY flag for this */
1300dd285b06SPaolo Bonzini         return ret;
1301dd285b06SPaolo Bonzini     }
1302dd285b06SPaolo Bonzini 
1303dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1304dd285b06SPaolo Bonzini     return 0;
1305dd285b06SPaolo Bonzini }
1306dd285b06SPaolo Bonzini 
1307dd285b06SPaolo Bonzini static void omap_tcmi_write(void *opaque, hwaddr addr,
1308dd285b06SPaolo Bonzini                             uint64_t value, unsigned size)
1309dd285b06SPaolo Bonzini {
1310dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1311dd285b06SPaolo Bonzini 
1312dd285b06SPaolo Bonzini     if (size != 4) {
131377a8257eSStefan Weil         omap_badwidth_write32(opaque, addr, value);
131477a8257eSStefan Weil         return;
1315dd285b06SPaolo Bonzini     }
1316dd285b06SPaolo Bonzini 
1317dd285b06SPaolo Bonzini     switch (addr) {
1318dd285b06SPaolo Bonzini     case 0x00:	/* IMIF_PRIO */
1319dd285b06SPaolo Bonzini     case 0x04:	/* EMIFS_PRIO */
1320dd285b06SPaolo Bonzini     case 0x08:	/* EMIFF_PRIO */
1321dd285b06SPaolo Bonzini     case 0x10:	/* EMIFS_CS0_CONFIG */
1322dd285b06SPaolo Bonzini     case 0x14:	/* EMIFS_CS1_CONFIG */
1323dd285b06SPaolo Bonzini     case 0x18:	/* EMIFS_CS2_CONFIG */
1324dd285b06SPaolo Bonzini     case 0x1c:	/* EMIFS_CS3_CONFIG */
1325dd285b06SPaolo Bonzini     case 0x20:	/* EMIFF_SDRAM_CONFIG */
1326dd285b06SPaolo Bonzini     case 0x24:	/* EMIFF_MRS */
1327dd285b06SPaolo Bonzini     case 0x28:	/* TIMEOUT1 */
1328dd285b06SPaolo Bonzini     case 0x2c:	/* TIMEOUT2 */
1329dd285b06SPaolo Bonzini     case 0x30:	/* TIMEOUT3 */
1330dd285b06SPaolo Bonzini     case 0x3c:	/* EMIFF_SDRAM_CONFIG_2 */
1331dd285b06SPaolo Bonzini     case 0x40:	/* EMIFS_CFG_DYN_WAIT */
1332dd285b06SPaolo Bonzini         s->tcmi_regs[addr >> 2] = value;
1333dd285b06SPaolo Bonzini         break;
1334dd285b06SPaolo Bonzini     case 0x0c:	/* EMIFS_CONFIG */
1335dd285b06SPaolo Bonzini         s->tcmi_regs[addr >> 2] = (value & 0xf) | (1 << 4);
1336dd285b06SPaolo Bonzini         break;
1337dd285b06SPaolo Bonzini 
1338dd285b06SPaolo Bonzini     default:
1339dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1340dd285b06SPaolo Bonzini     }
1341dd285b06SPaolo Bonzini }
1342dd285b06SPaolo Bonzini 
1343dd285b06SPaolo Bonzini static const MemoryRegionOps omap_tcmi_ops = {
1344dd285b06SPaolo Bonzini     .read = omap_tcmi_read,
1345dd285b06SPaolo Bonzini     .write = omap_tcmi_write,
1346dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1347dd285b06SPaolo Bonzini };
1348dd285b06SPaolo Bonzini 
1349dd285b06SPaolo Bonzini static void omap_tcmi_reset(struct omap_mpu_state_s *mpu)
1350dd285b06SPaolo Bonzini {
1351dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x00 >> 2] = 0x00000000;
1352dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x04 >> 2] = 0x00000000;
1353dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x08 >> 2] = 0x00000000;
1354dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x0c >> 2] = 0x00000010;
1355dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x10 >> 2] = 0x0010fffb;
1356dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x14 >> 2] = 0x0010fffb;
1357dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x18 >> 2] = 0x0010fffb;
1358dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x1c >> 2] = 0x0010fffb;
1359dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x20 >> 2] = 0x00618800;
1360dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x24 >> 2] = 0x00000037;
1361dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x28 >> 2] = 0x00000000;
1362dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x2c >> 2] = 0x00000000;
1363dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x30 >> 2] = 0x00000000;
1364dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x3c >> 2] = 0x00000003;
1365dd285b06SPaolo Bonzini     mpu->tcmi_regs[0x40 >> 2] = 0x00000000;
1366dd285b06SPaolo Bonzini }
1367dd285b06SPaolo Bonzini 
1368dd285b06SPaolo Bonzini static void omap_tcmi_init(MemoryRegion *memory, hwaddr base,
1369dd285b06SPaolo Bonzini                 struct omap_mpu_state_s *mpu)
1370dd285b06SPaolo Bonzini {
13712c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->tcmi_iomem, NULL, &omap_tcmi_ops, mpu,
1372dd285b06SPaolo Bonzini                           "omap-tcmi", 0x100);
1373dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &mpu->tcmi_iomem);
1374dd285b06SPaolo Bonzini     omap_tcmi_reset(mpu);
1375dd285b06SPaolo Bonzini }
1376dd285b06SPaolo Bonzini 
1377dd285b06SPaolo Bonzini /* Digital phase-locked loops control */
1378dd285b06SPaolo Bonzini struct dpll_ctl_s {
1379dd285b06SPaolo Bonzini     MemoryRegion iomem;
1380dd285b06SPaolo Bonzini     uint16_t mode;
1381dd285b06SPaolo Bonzini     omap_clk dpll;
1382dd285b06SPaolo Bonzini };
1383dd285b06SPaolo Bonzini 
1384dd285b06SPaolo Bonzini static uint64_t omap_dpll_read(void *opaque, hwaddr addr,
1385dd285b06SPaolo Bonzini                                unsigned size)
1386dd285b06SPaolo Bonzini {
1387dd285b06SPaolo Bonzini     struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1388dd285b06SPaolo Bonzini 
1389dd285b06SPaolo Bonzini     if (size != 2) {
1390dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
1391dd285b06SPaolo Bonzini     }
1392dd285b06SPaolo Bonzini 
1393dd285b06SPaolo Bonzini     if (addr == 0x00)	/* CTL_REG */
1394dd285b06SPaolo Bonzini         return s->mode;
1395dd285b06SPaolo Bonzini 
1396dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1397dd285b06SPaolo Bonzini     return 0;
1398dd285b06SPaolo Bonzini }
1399dd285b06SPaolo Bonzini 
1400dd285b06SPaolo Bonzini static void omap_dpll_write(void *opaque, hwaddr addr,
1401dd285b06SPaolo Bonzini                             uint64_t value, unsigned size)
1402dd285b06SPaolo Bonzini {
1403dd285b06SPaolo Bonzini     struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1404dd285b06SPaolo Bonzini     uint16_t diff;
1405dd285b06SPaolo Bonzini     static const int bypass_div[4] = { 1, 2, 4, 4 };
1406dd285b06SPaolo Bonzini     int div, mult;
1407dd285b06SPaolo Bonzini 
1408dd285b06SPaolo Bonzini     if (size != 2) {
140977a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
141077a8257eSStefan Weil         return;
1411dd285b06SPaolo Bonzini     }
1412dd285b06SPaolo Bonzini 
1413dd285b06SPaolo Bonzini     if (addr == 0x00) {	/* CTL_REG */
1414dd285b06SPaolo Bonzini         /* See omap_ulpd_pm_write() too */
1415dd285b06SPaolo Bonzini         diff = s->mode & value;
1416dd285b06SPaolo Bonzini         s->mode = value & 0x2fff;
1417dd285b06SPaolo Bonzini         if (diff & (0x3ff << 2)) {
1418dd285b06SPaolo Bonzini             if (value & (1 << 4)) {			/* PLL_ENABLE */
1419dd285b06SPaolo Bonzini                 div = ((value >> 5) & 3) + 1;		/* PLL_DIV */
1420dd285b06SPaolo Bonzini                 mult = MIN((value >> 7) & 0x1f, 1);	/* PLL_MULT */
1421dd285b06SPaolo Bonzini             } else {
1422dd285b06SPaolo Bonzini                 div = bypass_div[((value >> 2) & 3)];	/* BYPASS_DIV */
1423dd285b06SPaolo Bonzini                 mult = 1;
1424dd285b06SPaolo Bonzini             }
1425dd285b06SPaolo Bonzini             omap_clk_setrate(s->dpll, div, mult);
1426dd285b06SPaolo Bonzini         }
1427dd285b06SPaolo Bonzini 
1428dd285b06SPaolo Bonzini         /* Enter the desired mode.  */
1429dd285b06SPaolo Bonzini         s->mode = (s->mode & 0xfffe) | ((s->mode >> 4) & 1);
1430dd285b06SPaolo Bonzini 
1431dd285b06SPaolo Bonzini         /* Act as if the lock is restored.  */
1432dd285b06SPaolo Bonzini         s->mode |= 2;
1433dd285b06SPaolo Bonzini     } else {
1434dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1435dd285b06SPaolo Bonzini     }
1436dd285b06SPaolo Bonzini }
1437dd285b06SPaolo Bonzini 
1438dd285b06SPaolo Bonzini static const MemoryRegionOps omap_dpll_ops = {
1439dd285b06SPaolo Bonzini     .read = omap_dpll_read,
1440dd285b06SPaolo Bonzini     .write = omap_dpll_write,
1441dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1442dd285b06SPaolo Bonzini };
1443dd285b06SPaolo Bonzini 
1444dd285b06SPaolo Bonzini static void omap_dpll_reset(struct dpll_ctl_s *s)
1445dd285b06SPaolo Bonzini {
1446dd285b06SPaolo Bonzini     s->mode = 0x2002;
1447dd285b06SPaolo Bonzini     omap_clk_setrate(s->dpll, 1, 1);
1448dd285b06SPaolo Bonzini }
1449dd285b06SPaolo Bonzini 
1450dd285b06SPaolo Bonzini static struct dpll_ctl_s  *omap_dpll_init(MemoryRegion *memory,
1451dd285b06SPaolo Bonzini                            hwaddr base, omap_clk clk)
1452dd285b06SPaolo Bonzini {
1453dd285b06SPaolo Bonzini     struct dpll_ctl_s *s = g_malloc0(sizeof(*s));
14542c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_dpll_ops, s, "omap-dpll", 0x100);
1455dd285b06SPaolo Bonzini 
1456dd285b06SPaolo Bonzini     s->dpll = clk;
1457dd285b06SPaolo Bonzini     omap_dpll_reset(s);
1458dd285b06SPaolo Bonzini 
1459dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &s->iomem);
1460dd285b06SPaolo Bonzini     return s;
1461dd285b06SPaolo Bonzini }
1462dd285b06SPaolo Bonzini 
1463dd285b06SPaolo Bonzini /* MPU Clock/Reset/Power Mode Control */
1464dd285b06SPaolo Bonzini static uint64_t omap_clkm_read(void *opaque, hwaddr addr,
1465dd285b06SPaolo Bonzini                                unsigned size)
1466dd285b06SPaolo Bonzini {
1467dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1468dd285b06SPaolo Bonzini 
1469dd285b06SPaolo Bonzini     if (size != 2) {
1470dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
1471dd285b06SPaolo Bonzini     }
1472dd285b06SPaolo Bonzini 
1473dd285b06SPaolo Bonzini     switch (addr) {
1474dd285b06SPaolo Bonzini     case 0x00:	/* ARM_CKCTL */
1475dd285b06SPaolo Bonzini         return s->clkm.arm_ckctl;
1476dd285b06SPaolo Bonzini 
1477dd285b06SPaolo Bonzini     case 0x04:	/* ARM_IDLECT1 */
1478dd285b06SPaolo Bonzini         return s->clkm.arm_idlect1;
1479dd285b06SPaolo Bonzini 
1480dd285b06SPaolo Bonzini     case 0x08:	/* ARM_IDLECT2 */
1481dd285b06SPaolo Bonzini         return s->clkm.arm_idlect2;
1482dd285b06SPaolo Bonzini 
1483dd285b06SPaolo Bonzini     case 0x0c:	/* ARM_EWUPCT */
1484dd285b06SPaolo Bonzini         return s->clkm.arm_ewupct;
1485dd285b06SPaolo Bonzini 
1486dd285b06SPaolo Bonzini     case 0x10:	/* ARM_RSTCT1 */
1487dd285b06SPaolo Bonzini         return s->clkm.arm_rstct1;
1488dd285b06SPaolo Bonzini 
1489dd285b06SPaolo Bonzini     case 0x14:	/* ARM_RSTCT2 */
1490dd285b06SPaolo Bonzini         return s->clkm.arm_rstct2;
1491dd285b06SPaolo Bonzini 
1492dd285b06SPaolo Bonzini     case 0x18:	/* ARM_SYSST */
1493dd285b06SPaolo Bonzini         return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start;
1494dd285b06SPaolo Bonzini 
1495dd285b06SPaolo Bonzini     case 0x1c:	/* ARM_CKOUT1 */
1496dd285b06SPaolo Bonzini         return s->clkm.arm_ckout1;
1497dd285b06SPaolo Bonzini 
1498dd285b06SPaolo Bonzini     case 0x20:	/* ARM_CKOUT2 */
1499dd285b06SPaolo Bonzini         break;
1500dd285b06SPaolo Bonzini     }
1501dd285b06SPaolo Bonzini 
1502dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1503dd285b06SPaolo Bonzini     return 0;
1504dd285b06SPaolo Bonzini }
1505dd285b06SPaolo Bonzini 
1506dd285b06SPaolo Bonzini static inline void omap_clkm_ckctl_update(struct omap_mpu_state_s *s,
1507dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1508dd285b06SPaolo Bonzini {
1509dd285b06SPaolo Bonzini     omap_clk clk;
1510dd285b06SPaolo Bonzini 
1511dd285b06SPaolo Bonzini     if (diff & (1 << 14)) {				/* ARM_INTHCK_SEL */
1512dd285b06SPaolo Bonzini         if (value & (1 << 14))
1513dd285b06SPaolo Bonzini             /* Reserved */;
1514dd285b06SPaolo Bonzini         else {
1515dd285b06SPaolo Bonzini             clk = omap_findclk(s, "arminth_ck");
1516dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
1517dd285b06SPaolo Bonzini         }
1518dd285b06SPaolo Bonzini     }
1519dd285b06SPaolo Bonzini     if (diff & (1 << 12)) {				/* ARM_TIMXO */
1520dd285b06SPaolo Bonzini         clk = omap_findclk(s, "armtim_ck");
1521dd285b06SPaolo Bonzini         if (value & (1 << 12))
1522dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "clkin"));
1523dd285b06SPaolo Bonzini         else
1524dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
1525dd285b06SPaolo Bonzini     }
1526dd285b06SPaolo Bonzini     /* XXX: en_dspck */
1527dd285b06SPaolo Bonzini     if (diff & (3 << 10)) {				/* DSPMMUDIV */
1528dd285b06SPaolo Bonzini         clk = omap_findclk(s, "dspmmu_ck");
1529dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 10) & 3), 1);
1530dd285b06SPaolo Bonzini     }
1531dd285b06SPaolo Bonzini     if (diff & (3 << 8)) {				/* TCDIV */
1532dd285b06SPaolo Bonzini         clk = omap_findclk(s, "tc_ck");
1533dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 8) & 3), 1);
1534dd285b06SPaolo Bonzini     }
1535dd285b06SPaolo Bonzini     if (diff & (3 << 6)) {				/* DSPDIV */
1536dd285b06SPaolo Bonzini         clk = omap_findclk(s, "dsp_ck");
1537dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 6) & 3), 1);
1538dd285b06SPaolo Bonzini     }
1539dd285b06SPaolo Bonzini     if (diff & (3 << 4)) {				/* ARMDIV */
1540dd285b06SPaolo Bonzini         clk = omap_findclk(s, "arm_ck");
1541dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 4) & 3), 1);
1542dd285b06SPaolo Bonzini     }
1543dd285b06SPaolo Bonzini     if (diff & (3 << 2)) {				/* LCDDIV */
1544dd285b06SPaolo Bonzini         clk = omap_findclk(s, "lcd_ck");
1545dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 2) & 3), 1);
1546dd285b06SPaolo Bonzini     }
1547dd285b06SPaolo Bonzini     if (diff & (3 << 0)) {				/* PERDIV */
1548dd285b06SPaolo Bonzini         clk = omap_findclk(s, "armper_ck");
1549dd285b06SPaolo Bonzini         omap_clk_setrate(clk, 1 << ((value >> 0) & 3), 1);
1550dd285b06SPaolo Bonzini     }
1551dd285b06SPaolo Bonzini }
1552dd285b06SPaolo Bonzini 
1553dd285b06SPaolo Bonzini static inline void omap_clkm_idlect1_update(struct omap_mpu_state_s *s,
1554dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1555dd285b06SPaolo Bonzini {
1556dd285b06SPaolo Bonzini     omap_clk clk;
1557dd285b06SPaolo Bonzini 
1558dd285b06SPaolo Bonzini     if (value & (1 << 11)) {                            /* SETARM_IDLE */
1559c3affe56SAndreas Färber         cpu_interrupt(CPU(s->cpu), CPU_INTERRUPT_HALT);
1560dd285b06SPaolo Bonzini     }
1561cf83f140SEric Blake     if (!(value & (1 << 10))) {                         /* WKUP_MODE */
1562cf83f140SEric Blake         /* XXX: disable wakeup from IRQ */
1563cf83f140SEric Blake         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
1564cf83f140SEric Blake     }
1565dd285b06SPaolo Bonzini 
1566dd285b06SPaolo Bonzini #define SET_CANIDLE(clock, bit)				\
1567dd285b06SPaolo Bonzini     if (diff & (1 << bit)) {				\
1568dd285b06SPaolo Bonzini         clk = omap_findclk(s, clock);			\
1569dd285b06SPaolo Bonzini         omap_clk_canidle(clk, (value >> bit) & 1);	\
1570dd285b06SPaolo Bonzini     }
1571dd285b06SPaolo Bonzini     SET_CANIDLE("mpuwd_ck", 0)				/* IDLWDT_ARM */
1572dd285b06SPaolo Bonzini     SET_CANIDLE("armxor_ck", 1)				/* IDLXORP_ARM */
1573dd285b06SPaolo Bonzini     SET_CANIDLE("mpuper_ck", 2)				/* IDLPER_ARM */
1574dd285b06SPaolo Bonzini     SET_CANIDLE("lcd_ck", 3)				/* IDLLCD_ARM */
1575dd285b06SPaolo Bonzini     SET_CANIDLE("lb_ck", 4)				/* IDLLB_ARM */
1576dd285b06SPaolo Bonzini     SET_CANIDLE("hsab_ck", 5)				/* IDLHSAB_ARM */
1577dd285b06SPaolo Bonzini     SET_CANIDLE("tipb_ck", 6)				/* IDLIF_ARM */
1578dd285b06SPaolo Bonzini     SET_CANIDLE("dma_ck", 6)				/* IDLIF_ARM */
1579dd285b06SPaolo Bonzini     SET_CANIDLE("tc_ck", 6)				/* IDLIF_ARM */
1580dd285b06SPaolo Bonzini     SET_CANIDLE("dpll1", 7)				/* IDLDPLL_ARM */
1581dd285b06SPaolo Bonzini     SET_CANIDLE("dpll2", 7)				/* IDLDPLL_ARM */
1582dd285b06SPaolo Bonzini     SET_CANIDLE("dpll3", 7)				/* IDLDPLL_ARM */
1583dd285b06SPaolo Bonzini     SET_CANIDLE("mpui_ck", 8)				/* IDLAPI_ARM */
1584dd285b06SPaolo Bonzini     SET_CANIDLE("armtim_ck", 9)				/* IDLTIM_ARM */
1585dd285b06SPaolo Bonzini }
1586dd285b06SPaolo Bonzini 
1587dd285b06SPaolo Bonzini static inline void omap_clkm_idlect2_update(struct omap_mpu_state_s *s,
1588dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1589dd285b06SPaolo Bonzini {
1590dd285b06SPaolo Bonzini     omap_clk clk;
1591dd285b06SPaolo Bonzini 
1592dd285b06SPaolo Bonzini #define SET_ONOFF(clock, bit)				\
1593dd285b06SPaolo Bonzini     if (diff & (1 << bit)) {				\
1594dd285b06SPaolo Bonzini         clk = omap_findclk(s, clock);			\
1595dd285b06SPaolo Bonzini         omap_clk_onoff(clk, (value >> bit) & 1);	\
1596dd285b06SPaolo Bonzini     }
1597dd285b06SPaolo Bonzini     SET_ONOFF("mpuwd_ck", 0)				/* EN_WDTCK */
1598dd285b06SPaolo Bonzini     SET_ONOFF("armxor_ck", 1)				/* EN_XORPCK */
1599dd285b06SPaolo Bonzini     SET_ONOFF("mpuper_ck", 2)				/* EN_PERCK */
1600dd285b06SPaolo Bonzini     SET_ONOFF("lcd_ck", 3)				/* EN_LCDCK */
1601dd285b06SPaolo Bonzini     SET_ONOFF("lb_ck", 4)				/* EN_LBCK */
1602dd285b06SPaolo Bonzini     SET_ONOFF("hsab_ck", 5)				/* EN_HSABCK */
1603dd285b06SPaolo Bonzini     SET_ONOFF("mpui_ck", 6)				/* EN_APICK */
1604dd285b06SPaolo Bonzini     SET_ONOFF("armtim_ck", 7)				/* EN_TIMCK */
1605dd285b06SPaolo Bonzini     SET_CANIDLE("dma_ck", 8)				/* DMACK_REQ */
1606dd285b06SPaolo Bonzini     SET_ONOFF("arm_gpio_ck", 9)				/* EN_GPIOCK */
1607dd285b06SPaolo Bonzini     SET_ONOFF("lbfree_ck", 10)				/* EN_LBFREECK */
1608dd285b06SPaolo Bonzini }
1609dd285b06SPaolo Bonzini 
1610dd285b06SPaolo Bonzini static inline void omap_clkm_ckout1_update(struct omap_mpu_state_s *s,
1611dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1612dd285b06SPaolo Bonzini {
1613dd285b06SPaolo Bonzini     omap_clk clk;
1614dd285b06SPaolo Bonzini 
1615dd285b06SPaolo Bonzini     if (diff & (3 << 4)) {				/* TCLKOUT */
1616dd285b06SPaolo Bonzini         clk = omap_findclk(s, "tclk_out");
1617dd285b06SPaolo Bonzini         switch ((value >> 4) & 3) {
1618dd285b06SPaolo Bonzini         case 1:
1619dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_gen3"));
1620dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 1);
1621dd285b06SPaolo Bonzini             break;
1622dd285b06SPaolo Bonzini         case 2:
1623dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
1624dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 1);
1625dd285b06SPaolo Bonzini             break;
1626dd285b06SPaolo Bonzini         default:
1627dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 0);
1628dd285b06SPaolo Bonzini         }
1629dd285b06SPaolo Bonzini     }
1630dd285b06SPaolo Bonzini     if (diff & (3 << 2)) {				/* DCLKOUT */
1631dd285b06SPaolo Bonzini         clk = omap_findclk(s, "dclk_out");
1632dd285b06SPaolo Bonzini         switch ((value >> 2) & 3) {
1633dd285b06SPaolo Bonzini         case 0:
1634dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "dspmmu_ck"));
1635dd285b06SPaolo Bonzini             break;
1636dd285b06SPaolo Bonzini         case 1:
1637dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_gen2"));
1638dd285b06SPaolo Bonzini             break;
1639dd285b06SPaolo Bonzini         case 2:
1640dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "dsp_ck"));
1641dd285b06SPaolo Bonzini             break;
1642dd285b06SPaolo Bonzini         case 3:
1643dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
1644dd285b06SPaolo Bonzini             break;
1645dd285b06SPaolo Bonzini         }
1646dd285b06SPaolo Bonzini     }
1647dd285b06SPaolo Bonzini     if (diff & (3 << 0)) {				/* ACLKOUT */
1648dd285b06SPaolo Bonzini         clk = omap_findclk(s, "aclk_out");
1649dd285b06SPaolo Bonzini         switch ((value >> 0) & 3) {
1650dd285b06SPaolo Bonzini         case 1:
1651dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
1652dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 1);
1653dd285b06SPaolo Bonzini             break;
1654dd285b06SPaolo Bonzini         case 2:
1655dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "arm_ck"));
1656dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 1);
1657dd285b06SPaolo Bonzini             break;
1658dd285b06SPaolo Bonzini         case 3:
1659dd285b06SPaolo Bonzini             omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
1660dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 1);
1661dd285b06SPaolo Bonzini             break;
1662dd285b06SPaolo Bonzini         default:
1663dd285b06SPaolo Bonzini             omap_clk_onoff(clk, 0);
1664dd285b06SPaolo Bonzini         }
1665dd285b06SPaolo Bonzini     }
1666dd285b06SPaolo Bonzini }
1667dd285b06SPaolo Bonzini 
1668dd285b06SPaolo Bonzini static void omap_clkm_write(void *opaque, hwaddr addr,
1669dd285b06SPaolo Bonzini                             uint64_t value, unsigned size)
1670dd285b06SPaolo Bonzini {
1671dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1672dd285b06SPaolo Bonzini     uint16_t diff;
1673dd285b06SPaolo Bonzini     omap_clk clk;
1674dd285b06SPaolo Bonzini     static const char *clkschemename[8] = {
1675dd285b06SPaolo Bonzini         "fully synchronous", "fully asynchronous", "synchronous scalable",
1676dd285b06SPaolo Bonzini         "mix mode 1", "mix mode 2", "bypass mode", "mix mode 3", "mix mode 4",
1677dd285b06SPaolo Bonzini     };
1678dd285b06SPaolo Bonzini 
1679dd285b06SPaolo Bonzini     if (size != 2) {
168077a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
168177a8257eSStefan Weil         return;
1682dd285b06SPaolo Bonzini     }
1683dd285b06SPaolo Bonzini 
1684dd285b06SPaolo Bonzini     switch (addr) {
1685dd285b06SPaolo Bonzini     case 0x00:	/* ARM_CKCTL */
1686dd285b06SPaolo Bonzini         diff = s->clkm.arm_ckctl ^ value;
1687dd285b06SPaolo Bonzini         s->clkm.arm_ckctl = value & 0x7fff;
1688dd285b06SPaolo Bonzini         omap_clkm_ckctl_update(s, diff, value);
1689dd285b06SPaolo Bonzini         return;
1690dd285b06SPaolo Bonzini 
1691dd285b06SPaolo Bonzini     case 0x04:	/* ARM_IDLECT1 */
1692dd285b06SPaolo Bonzini         diff = s->clkm.arm_idlect1 ^ value;
1693dd285b06SPaolo Bonzini         s->clkm.arm_idlect1 = value & 0x0fff;
1694dd285b06SPaolo Bonzini         omap_clkm_idlect1_update(s, diff, value);
1695dd285b06SPaolo Bonzini         return;
1696dd285b06SPaolo Bonzini 
1697dd285b06SPaolo Bonzini     case 0x08:	/* ARM_IDLECT2 */
1698dd285b06SPaolo Bonzini         diff = s->clkm.arm_idlect2 ^ value;
1699dd285b06SPaolo Bonzini         s->clkm.arm_idlect2 = value & 0x07ff;
1700dd285b06SPaolo Bonzini         omap_clkm_idlect2_update(s, diff, value);
1701dd285b06SPaolo Bonzini         return;
1702dd285b06SPaolo Bonzini 
1703dd285b06SPaolo Bonzini     case 0x0c:	/* ARM_EWUPCT */
1704dd285b06SPaolo Bonzini         s->clkm.arm_ewupct = value & 0x003f;
1705dd285b06SPaolo Bonzini         return;
1706dd285b06SPaolo Bonzini 
1707dd285b06SPaolo Bonzini     case 0x10:	/* ARM_RSTCT1 */
1708dd285b06SPaolo Bonzini         diff = s->clkm.arm_rstct1 ^ value;
1709dd285b06SPaolo Bonzini         s->clkm.arm_rstct1 = value & 0x0007;
1710dd285b06SPaolo Bonzini         if (value & 9) {
1711cf83f140SEric Blake             qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
1712dd285b06SPaolo Bonzini             s->clkm.cold_start = 0xa;
1713dd285b06SPaolo Bonzini         }
1714dd285b06SPaolo Bonzini         if (diff & ~value & 4) {				/* DSP_RST */
1715dd285b06SPaolo Bonzini             omap_mpui_reset(s);
1716dd285b06SPaolo Bonzini             omap_tipb_bridge_reset(s->private_tipb);
1717dd285b06SPaolo Bonzini             omap_tipb_bridge_reset(s->public_tipb);
1718dd285b06SPaolo Bonzini         }
1719dd285b06SPaolo Bonzini         if (diff & 2) {						/* DSP_EN */
1720dd285b06SPaolo Bonzini             clk = omap_findclk(s, "dsp_ck");
1721dd285b06SPaolo Bonzini             omap_clk_canidle(clk, (~value >> 1) & 1);
1722dd285b06SPaolo Bonzini         }
1723dd285b06SPaolo Bonzini         return;
1724dd285b06SPaolo Bonzini 
1725dd285b06SPaolo Bonzini     case 0x14:	/* ARM_RSTCT2 */
1726dd285b06SPaolo Bonzini         s->clkm.arm_rstct2 = value & 0x0001;
1727dd285b06SPaolo Bonzini         return;
1728dd285b06SPaolo Bonzini 
1729dd285b06SPaolo Bonzini     case 0x18:	/* ARM_SYSST */
1730dd285b06SPaolo Bonzini         if ((s->clkm.clocking_scheme ^ (value >> 11)) & 7) {
1731dd285b06SPaolo Bonzini             s->clkm.clocking_scheme = (value >> 11) & 7;
1732a89f364aSAlistair Francis             printf("%s: clocking scheme set to %s\n", __func__,
1733dd285b06SPaolo Bonzini                    clkschemename[s->clkm.clocking_scheme]);
1734dd285b06SPaolo Bonzini         }
1735dd285b06SPaolo Bonzini         s->clkm.cold_start &= value & 0x3f;
1736dd285b06SPaolo Bonzini         return;
1737dd285b06SPaolo Bonzini 
1738dd285b06SPaolo Bonzini     case 0x1c:	/* ARM_CKOUT1 */
1739dd285b06SPaolo Bonzini         diff = s->clkm.arm_ckout1 ^ value;
1740dd285b06SPaolo Bonzini         s->clkm.arm_ckout1 = value & 0x003f;
1741dd285b06SPaolo Bonzini         omap_clkm_ckout1_update(s, diff, value);
1742dd285b06SPaolo Bonzini         return;
1743dd285b06SPaolo Bonzini 
1744dd285b06SPaolo Bonzini     case 0x20:	/* ARM_CKOUT2 */
1745dd285b06SPaolo Bonzini     default:
1746dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1747dd285b06SPaolo Bonzini     }
1748dd285b06SPaolo Bonzini }
1749dd285b06SPaolo Bonzini 
1750dd285b06SPaolo Bonzini static const MemoryRegionOps omap_clkm_ops = {
1751dd285b06SPaolo Bonzini     .read = omap_clkm_read,
1752dd285b06SPaolo Bonzini     .write = omap_clkm_write,
1753dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1754dd285b06SPaolo Bonzini };
1755dd285b06SPaolo Bonzini 
1756dd285b06SPaolo Bonzini static uint64_t omap_clkdsp_read(void *opaque, hwaddr addr,
1757dd285b06SPaolo Bonzini                                  unsigned size)
1758dd285b06SPaolo Bonzini {
1759dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1760259186a7SAndreas Färber     CPUState *cpu = CPU(s->cpu);
1761dd285b06SPaolo Bonzini 
1762dd285b06SPaolo Bonzini     if (size != 2) {
1763dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
1764dd285b06SPaolo Bonzini     }
1765dd285b06SPaolo Bonzini 
1766dd285b06SPaolo Bonzini     switch (addr) {
1767dd285b06SPaolo Bonzini     case 0x04:	/* DSP_IDLECT1 */
1768dd285b06SPaolo Bonzini         return s->clkm.dsp_idlect1;
1769dd285b06SPaolo Bonzini 
1770dd285b06SPaolo Bonzini     case 0x08:	/* DSP_IDLECT2 */
1771dd285b06SPaolo Bonzini         return s->clkm.dsp_idlect2;
1772dd285b06SPaolo Bonzini 
1773dd285b06SPaolo Bonzini     case 0x14:	/* DSP_RSTCT2 */
1774dd285b06SPaolo Bonzini         return s->clkm.dsp_rstct2;
1775dd285b06SPaolo Bonzini 
1776dd285b06SPaolo Bonzini     case 0x18:	/* DSP_SYSST */
1777259186a7SAndreas Färber         cpu = CPU(s->cpu);
1778dd285b06SPaolo Bonzini         return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start |
1779259186a7SAndreas Färber                 (cpu->halted << 6);      /* Quite useless... */
1780dd285b06SPaolo Bonzini     }
1781dd285b06SPaolo Bonzini 
1782dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
1783dd285b06SPaolo Bonzini     return 0;
1784dd285b06SPaolo Bonzini }
1785dd285b06SPaolo Bonzini 
1786dd285b06SPaolo Bonzini static inline void omap_clkdsp_idlect1_update(struct omap_mpu_state_s *s,
1787dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1788dd285b06SPaolo Bonzini {
1789dd285b06SPaolo Bonzini     omap_clk clk;
1790dd285b06SPaolo Bonzini 
1791dd285b06SPaolo Bonzini     SET_CANIDLE("dspxor_ck", 1);			/* IDLXORP_DSP */
1792dd285b06SPaolo Bonzini }
1793dd285b06SPaolo Bonzini 
1794dd285b06SPaolo Bonzini static inline void omap_clkdsp_idlect2_update(struct omap_mpu_state_s *s,
1795dd285b06SPaolo Bonzini                 uint16_t diff, uint16_t value)
1796dd285b06SPaolo Bonzini {
1797dd285b06SPaolo Bonzini     omap_clk clk;
1798dd285b06SPaolo Bonzini 
1799dd285b06SPaolo Bonzini     SET_ONOFF("dspxor_ck", 1);				/* EN_XORPCK */
1800dd285b06SPaolo Bonzini }
1801dd285b06SPaolo Bonzini 
1802dd285b06SPaolo Bonzini static void omap_clkdsp_write(void *opaque, hwaddr addr,
1803dd285b06SPaolo Bonzini                               uint64_t value, unsigned size)
1804dd285b06SPaolo Bonzini {
1805dd285b06SPaolo Bonzini     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1806dd285b06SPaolo Bonzini     uint16_t diff;
1807dd285b06SPaolo Bonzini 
1808dd285b06SPaolo Bonzini     if (size != 2) {
180977a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
181077a8257eSStefan Weil         return;
1811dd285b06SPaolo Bonzini     }
1812dd285b06SPaolo Bonzini 
1813dd285b06SPaolo Bonzini     switch (addr) {
1814dd285b06SPaolo Bonzini     case 0x04:	/* DSP_IDLECT1 */
1815dd285b06SPaolo Bonzini         diff = s->clkm.dsp_idlect1 ^ value;
1816dd285b06SPaolo Bonzini         s->clkm.dsp_idlect1 = value & 0x01f7;
1817dd285b06SPaolo Bonzini         omap_clkdsp_idlect1_update(s, diff, value);
1818dd285b06SPaolo Bonzini         break;
1819dd285b06SPaolo Bonzini 
1820dd285b06SPaolo Bonzini     case 0x08:	/* DSP_IDLECT2 */
1821dd285b06SPaolo Bonzini         s->clkm.dsp_idlect2 = value & 0x0037;
1822dd285b06SPaolo Bonzini         diff = s->clkm.dsp_idlect1 ^ value;
1823dd285b06SPaolo Bonzini         omap_clkdsp_idlect2_update(s, diff, value);
1824dd285b06SPaolo Bonzini         break;
1825dd285b06SPaolo Bonzini 
1826dd285b06SPaolo Bonzini     case 0x14:	/* DSP_RSTCT2 */
1827dd285b06SPaolo Bonzini         s->clkm.dsp_rstct2 = value & 0x0001;
1828dd285b06SPaolo Bonzini         break;
1829dd285b06SPaolo Bonzini 
1830dd285b06SPaolo Bonzini     case 0x18:	/* DSP_SYSST */
1831dd285b06SPaolo Bonzini         s->clkm.cold_start &= value & 0x3f;
1832dd285b06SPaolo Bonzini         break;
1833dd285b06SPaolo Bonzini 
1834dd285b06SPaolo Bonzini     default:
1835dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
1836dd285b06SPaolo Bonzini     }
1837dd285b06SPaolo Bonzini }
1838dd285b06SPaolo Bonzini 
1839dd285b06SPaolo Bonzini static const MemoryRegionOps omap_clkdsp_ops = {
1840dd285b06SPaolo Bonzini     .read = omap_clkdsp_read,
1841dd285b06SPaolo Bonzini     .write = omap_clkdsp_write,
1842dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
1843dd285b06SPaolo Bonzini };
1844dd285b06SPaolo Bonzini 
1845dd285b06SPaolo Bonzini static void omap_clkm_reset(struct omap_mpu_state_s *s)
1846dd285b06SPaolo Bonzini {
1847dd285b06SPaolo Bonzini     if (s->wdt && s->wdt->reset)
1848dd285b06SPaolo Bonzini         s->clkm.cold_start = 0x6;
1849dd285b06SPaolo Bonzini     s->clkm.clocking_scheme = 0;
1850dd285b06SPaolo Bonzini     omap_clkm_ckctl_update(s, ~0, 0x3000);
1851dd285b06SPaolo Bonzini     s->clkm.arm_ckctl = 0x3000;
1852dd285b06SPaolo Bonzini     omap_clkm_idlect1_update(s, s->clkm.arm_idlect1 ^ 0x0400, 0x0400);
1853dd285b06SPaolo Bonzini     s->clkm.arm_idlect1 = 0x0400;
1854dd285b06SPaolo Bonzini     omap_clkm_idlect2_update(s, s->clkm.arm_idlect2 ^ 0x0100, 0x0100);
1855dd285b06SPaolo Bonzini     s->clkm.arm_idlect2 = 0x0100;
1856dd285b06SPaolo Bonzini     s->clkm.arm_ewupct = 0x003f;
1857dd285b06SPaolo Bonzini     s->clkm.arm_rstct1 = 0x0000;
1858dd285b06SPaolo Bonzini     s->clkm.arm_rstct2 = 0x0000;
1859dd285b06SPaolo Bonzini     s->clkm.arm_ckout1 = 0x0015;
1860dd285b06SPaolo Bonzini     s->clkm.dpll1_mode = 0x2002;
1861dd285b06SPaolo Bonzini     omap_clkdsp_idlect1_update(s, s->clkm.dsp_idlect1 ^ 0x0040, 0x0040);
1862dd285b06SPaolo Bonzini     s->clkm.dsp_idlect1 = 0x0040;
1863dd285b06SPaolo Bonzini     omap_clkdsp_idlect2_update(s, ~0, 0x0000);
1864dd285b06SPaolo Bonzini     s->clkm.dsp_idlect2 = 0x0000;
1865dd285b06SPaolo Bonzini     s->clkm.dsp_rstct2 = 0x0000;
1866dd285b06SPaolo Bonzini }
1867dd285b06SPaolo Bonzini 
1868dd285b06SPaolo Bonzini static void omap_clkm_init(MemoryRegion *memory, hwaddr mpu_base,
1869dd285b06SPaolo Bonzini                 hwaddr dsp_base, struct omap_mpu_state_s *s)
1870dd285b06SPaolo Bonzini {
18712c9b15caSPaolo Bonzini     memory_region_init_io(&s->clkm_iomem, NULL, &omap_clkm_ops, s,
1872dd285b06SPaolo Bonzini                           "omap-clkm", 0x100);
18732c9b15caSPaolo Bonzini     memory_region_init_io(&s->clkdsp_iomem, NULL, &omap_clkdsp_ops, s,
1874dd285b06SPaolo Bonzini                           "omap-clkdsp", 0x1000);
1875dd285b06SPaolo Bonzini 
1876dd285b06SPaolo Bonzini     s->clkm.arm_idlect1 = 0x03ff;
1877dd285b06SPaolo Bonzini     s->clkm.arm_idlect2 = 0x0100;
1878dd285b06SPaolo Bonzini     s->clkm.dsp_idlect1 = 0x0002;
1879dd285b06SPaolo Bonzini     omap_clkm_reset(s);
1880dd285b06SPaolo Bonzini     s->clkm.cold_start = 0x3a;
1881dd285b06SPaolo Bonzini 
1882dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, mpu_base, &s->clkm_iomem);
1883dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, dsp_base, &s->clkdsp_iomem);
1884dd285b06SPaolo Bonzini }
1885dd285b06SPaolo Bonzini 
1886dd285b06SPaolo Bonzini /* MPU I/O */
1887dd285b06SPaolo Bonzini struct omap_mpuio_s {
1888dd285b06SPaolo Bonzini     qemu_irq irq;
1889dd285b06SPaolo Bonzini     qemu_irq kbd_irq;
1890dd285b06SPaolo Bonzini     qemu_irq *in;
1891dd285b06SPaolo Bonzini     qemu_irq handler[16];
1892dd285b06SPaolo Bonzini     qemu_irq wakeup;
1893dd285b06SPaolo Bonzini     MemoryRegion iomem;
1894dd285b06SPaolo Bonzini 
1895dd285b06SPaolo Bonzini     uint16_t inputs;
1896dd285b06SPaolo Bonzini     uint16_t outputs;
1897dd285b06SPaolo Bonzini     uint16_t dir;
1898dd285b06SPaolo Bonzini     uint16_t edge;
1899dd285b06SPaolo Bonzini     uint16_t mask;
1900dd285b06SPaolo Bonzini     uint16_t ints;
1901dd285b06SPaolo Bonzini 
1902dd285b06SPaolo Bonzini     uint16_t debounce;
1903dd285b06SPaolo Bonzini     uint16_t latch;
1904dd285b06SPaolo Bonzini     uint8_t event;
1905dd285b06SPaolo Bonzini 
1906dd285b06SPaolo Bonzini     uint8_t buttons[5];
1907dd285b06SPaolo Bonzini     uint8_t row_latch;
1908dd285b06SPaolo Bonzini     uint8_t cols;
1909dd285b06SPaolo Bonzini     int kbd_mask;
1910dd285b06SPaolo Bonzini     int clk;
1911dd285b06SPaolo Bonzini };
1912dd285b06SPaolo Bonzini 
1913dd285b06SPaolo Bonzini static void omap_mpuio_set(void *opaque, int line, int level)
1914dd285b06SPaolo Bonzini {
1915dd285b06SPaolo Bonzini     struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
1916dd285b06SPaolo Bonzini     uint16_t prev = s->inputs;
1917dd285b06SPaolo Bonzini 
1918dd285b06SPaolo Bonzini     if (level)
1919dd285b06SPaolo Bonzini         s->inputs |= 1 << line;
1920dd285b06SPaolo Bonzini     else
1921dd285b06SPaolo Bonzini         s->inputs &= ~(1 << line);
1922dd285b06SPaolo Bonzini 
1923dd285b06SPaolo Bonzini     if (((1 << line) & s->dir & ~s->mask) && s->clk) {
1924dd285b06SPaolo Bonzini         if ((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) {
1925dd285b06SPaolo Bonzini             s->ints |= 1 << line;
1926dd285b06SPaolo Bonzini             qemu_irq_raise(s->irq);
1927dd285b06SPaolo Bonzini             /* TODO: wakeup */
1928dd285b06SPaolo Bonzini         }
1929dd285b06SPaolo Bonzini         if ((s->event & (1 << 0)) &&		/* SET_GPIO_EVENT_MODE */
1930dd285b06SPaolo Bonzini                 (s->event >> 1) == line)	/* PIN_SELECT */
1931dd285b06SPaolo Bonzini             s->latch = s->inputs;
1932dd285b06SPaolo Bonzini     }
1933dd285b06SPaolo Bonzini }
1934dd285b06SPaolo Bonzini 
1935dd285b06SPaolo Bonzini static void omap_mpuio_kbd_update(struct omap_mpuio_s *s)
1936dd285b06SPaolo Bonzini {
1937dd285b06SPaolo Bonzini     int i;
1938dd285b06SPaolo Bonzini     uint8_t *row, rows = 0, cols = ~s->cols;
1939dd285b06SPaolo Bonzini 
1940dd285b06SPaolo Bonzini     for (row = s->buttons + 4, i = 1 << 4; i; row --, i >>= 1)
1941dd285b06SPaolo Bonzini         if (*row & cols)
1942dd285b06SPaolo Bonzini             rows |= i;
1943dd285b06SPaolo Bonzini 
1944dd285b06SPaolo Bonzini     qemu_set_irq(s->kbd_irq, rows && !s->kbd_mask && s->clk);
1945dd285b06SPaolo Bonzini     s->row_latch = ~rows;
1946dd285b06SPaolo Bonzini }
1947dd285b06SPaolo Bonzini 
1948dd285b06SPaolo Bonzini static uint64_t omap_mpuio_read(void *opaque, hwaddr addr,
1949dd285b06SPaolo Bonzini                                 unsigned size)
1950dd285b06SPaolo Bonzini {
1951dd285b06SPaolo Bonzini     struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
1952dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
1953dd285b06SPaolo Bonzini     uint16_t ret;
1954dd285b06SPaolo Bonzini 
1955dd285b06SPaolo Bonzini     if (size != 2) {
1956dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
1957dd285b06SPaolo Bonzini     }
1958dd285b06SPaolo Bonzini 
1959dd285b06SPaolo Bonzini     switch (offset) {
1960dd285b06SPaolo Bonzini     case 0x00:	/* INPUT_LATCH */
1961dd285b06SPaolo Bonzini         return s->inputs;
1962dd285b06SPaolo Bonzini 
1963dd285b06SPaolo Bonzini     case 0x04:	/* OUTPUT_REG */
1964dd285b06SPaolo Bonzini         return s->outputs;
1965dd285b06SPaolo Bonzini 
1966dd285b06SPaolo Bonzini     case 0x08:	/* IO_CNTL */
1967dd285b06SPaolo Bonzini         return s->dir;
1968dd285b06SPaolo Bonzini 
1969dd285b06SPaolo Bonzini     case 0x10:	/* KBR_LATCH */
1970dd285b06SPaolo Bonzini         return s->row_latch;
1971dd285b06SPaolo Bonzini 
1972dd285b06SPaolo Bonzini     case 0x14:	/* KBC_REG */
1973dd285b06SPaolo Bonzini         return s->cols;
1974dd285b06SPaolo Bonzini 
1975dd285b06SPaolo Bonzini     case 0x18:	/* GPIO_EVENT_MODE_REG */
1976dd285b06SPaolo Bonzini         return s->event;
1977dd285b06SPaolo Bonzini 
1978dd285b06SPaolo Bonzini     case 0x1c:	/* GPIO_INT_EDGE_REG */
1979dd285b06SPaolo Bonzini         return s->edge;
1980dd285b06SPaolo Bonzini 
1981dd285b06SPaolo Bonzini     case 0x20:	/* KBD_INT */
1982dd285b06SPaolo Bonzini         return (~s->row_latch & 0x1f) && !s->kbd_mask;
1983dd285b06SPaolo Bonzini 
1984dd285b06SPaolo Bonzini     case 0x24:	/* GPIO_INT */
1985dd285b06SPaolo Bonzini         ret = s->ints;
1986dd285b06SPaolo Bonzini         s->ints &= s->mask;
1987dd285b06SPaolo Bonzini         if (ret)
1988dd285b06SPaolo Bonzini             qemu_irq_lower(s->irq);
1989dd285b06SPaolo Bonzini         return ret;
1990dd285b06SPaolo Bonzini 
1991dd285b06SPaolo Bonzini     case 0x28:	/* KBD_MASKIT */
1992dd285b06SPaolo Bonzini         return s->kbd_mask;
1993dd285b06SPaolo Bonzini 
1994dd285b06SPaolo Bonzini     case 0x2c:	/* GPIO_MASKIT */
1995dd285b06SPaolo Bonzini         return s->mask;
1996dd285b06SPaolo Bonzini 
1997dd285b06SPaolo Bonzini     case 0x30:	/* GPIO_DEBOUNCING_REG */
1998dd285b06SPaolo Bonzini         return s->debounce;
1999dd285b06SPaolo Bonzini 
2000dd285b06SPaolo Bonzini     case 0x34:	/* GPIO_LATCH_REG */
2001dd285b06SPaolo Bonzini         return s->latch;
2002dd285b06SPaolo Bonzini     }
2003dd285b06SPaolo Bonzini 
2004dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
2005dd285b06SPaolo Bonzini     return 0;
2006dd285b06SPaolo Bonzini }
2007dd285b06SPaolo Bonzini 
2008dd285b06SPaolo Bonzini static void omap_mpuio_write(void *opaque, hwaddr addr,
2009dd285b06SPaolo Bonzini                              uint64_t value, unsigned size)
2010dd285b06SPaolo Bonzini {
2011dd285b06SPaolo Bonzini     struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2012dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2013dd285b06SPaolo Bonzini     uint16_t diff;
2014dd285b06SPaolo Bonzini     int ln;
2015dd285b06SPaolo Bonzini 
2016dd285b06SPaolo Bonzini     if (size != 2) {
201777a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
201877a8257eSStefan Weil         return;
2019dd285b06SPaolo Bonzini     }
2020dd285b06SPaolo Bonzini 
2021dd285b06SPaolo Bonzini     switch (offset) {
2022dd285b06SPaolo Bonzini     case 0x04:	/* OUTPUT_REG */
2023dd285b06SPaolo Bonzini         diff = (s->outputs ^ value) & ~s->dir;
2024dd285b06SPaolo Bonzini         s->outputs = value;
2025bd2a8884SStefan Hajnoczi         while ((ln = ctz32(diff)) != 32) {
2026dd285b06SPaolo Bonzini             if (s->handler[ln])
2027dd285b06SPaolo Bonzini                 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2028dd285b06SPaolo Bonzini             diff &= ~(1 << ln);
2029dd285b06SPaolo Bonzini         }
2030dd285b06SPaolo Bonzini         break;
2031dd285b06SPaolo Bonzini 
2032dd285b06SPaolo Bonzini     case 0x08:	/* IO_CNTL */
2033dd285b06SPaolo Bonzini         diff = s->outputs & (s->dir ^ value);
2034dd285b06SPaolo Bonzini         s->dir = value;
2035dd285b06SPaolo Bonzini 
2036dd285b06SPaolo Bonzini         value = s->outputs & ~s->dir;
2037bd2a8884SStefan Hajnoczi         while ((ln = ctz32(diff)) != 32) {
2038dd285b06SPaolo Bonzini             if (s->handler[ln])
2039dd285b06SPaolo Bonzini                 qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2040dd285b06SPaolo Bonzini             diff &= ~(1 << ln);
2041dd285b06SPaolo Bonzini         }
2042dd285b06SPaolo Bonzini         break;
2043dd285b06SPaolo Bonzini 
2044dd285b06SPaolo Bonzini     case 0x14:	/* KBC_REG */
2045dd285b06SPaolo Bonzini         s->cols = value;
2046dd285b06SPaolo Bonzini         omap_mpuio_kbd_update(s);
2047dd285b06SPaolo Bonzini         break;
2048dd285b06SPaolo Bonzini 
2049dd285b06SPaolo Bonzini     case 0x18:	/* GPIO_EVENT_MODE_REG */
2050dd285b06SPaolo Bonzini         s->event = value & 0x1f;
2051dd285b06SPaolo Bonzini         break;
2052dd285b06SPaolo Bonzini 
2053dd285b06SPaolo Bonzini     case 0x1c:	/* GPIO_INT_EDGE_REG */
2054dd285b06SPaolo Bonzini         s->edge = value;
2055dd285b06SPaolo Bonzini         break;
2056dd285b06SPaolo Bonzini 
2057dd285b06SPaolo Bonzini     case 0x28:	/* KBD_MASKIT */
2058dd285b06SPaolo Bonzini         s->kbd_mask = value & 1;
2059dd285b06SPaolo Bonzini         omap_mpuio_kbd_update(s);
2060dd285b06SPaolo Bonzini         break;
2061dd285b06SPaolo Bonzini 
2062dd285b06SPaolo Bonzini     case 0x2c:	/* GPIO_MASKIT */
2063dd285b06SPaolo Bonzini         s->mask = value;
2064dd285b06SPaolo Bonzini         break;
2065dd285b06SPaolo Bonzini 
2066dd285b06SPaolo Bonzini     case 0x30:	/* GPIO_DEBOUNCING_REG */
2067dd285b06SPaolo Bonzini         s->debounce = value & 0x1ff;
2068dd285b06SPaolo Bonzini         break;
2069dd285b06SPaolo Bonzini 
2070dd285b06SPaolo Bonzini     case 0x00:	/* INPUT_LATCH */
2071dd285b06SPaolo Bonzini     case 0x10:	/* KBR_LATCH */
2072dd285b06SPaolo Bonzini     case 0x20:	/* KBD_INT */
2073dd285b06SPaolo Bonzini     case 0x24:	/* GPIO_INT */
2074dd285b06SPaolo Bonzini     case 0x34:	/* GPIO_LATCH_REG */
2075dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
2076dd285b06SPaolo Bonzini         return;
2077dd285b06SPaolo Bonzini 
2078dd285b06SPaolo Bonzini     default:
2079dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
2080dd285b06SPaolo Bonzini         return;
2081dd285b06SPaolo Bonzini     }
2082dd285b06SPaolo Bonzini }
2083dd285b06SPaolo Bonzini 
2084dd285b06SPaolo Bonzini static const MemoryRegionOps omap_mpuio_ops  = {
2085dd285b06SPaolo Bonzini     .read = omap_mpuio_read,
2086dd285b06SPaolo Bonzini     .write = omap_mpuio_write,
2087dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
2088dd285b06SPaolo Bonzini };
2089dd285b06SPaolo Bonzini 
2090dd285b06SPaolo Bonzini static void omap_mpuio_reset(struct omap_mpuio_s *s)
2091dd285b06SPaolo Bonzini {
2092dd285b06SPaolo Bonzini     s->inputs = 0;
2093dd285b06SPaolo Bonzini     s->outputs = 0;
2094dd285b06SPaolo Bonzini     s->dir = ~0;
2095dd285b06SPaolo Bonzini     s->event = 0;
2096dd285b06SPaolo Bonzini     s->edge = 0;
2097dd285b06SPaolo Bonzini     s->kbd_mask = 0;
2098dd285b06SPaolo Bonzini     s->mask = 0;
2099dd285b06SPaolo Bonzini     s->debounce = 0;
2100dd285b06SPaolo Bonzini     s->latch = 0;
2101dd285b06SPaolo Bonzini     s->ints = 0;
2102dd285b06SPaolo Bonzini     s->row_latch = 0x1f;
2103dd285b06SPaolo Bonzini     s->clk = 1;
2104dd285b06SPaolo Bonzini }
2105dd285b06SPaolo Bonzini 
2106dd285b06SPaolo Bonzini static void omap_mpuio_onoff(void *opaque, int line, int on)
2107dd285b06SPaolo Bonzini {
2108dd285b06SPaolo Bonzini     struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2109dd285b06SPaolo Bonzini 
2110dd285b06SPaolo Bonzini     s->clk = on;
2111dd285b06SPaolo Bonzini     if (on)
2112dd285b06SPaolo Bonzini         omap_mpuio_kbd_update(s);
2113dd285b06SPaolo Bonzini }
2114dd285b06SPaolo Bonzini 
2115dd285b06SPaolo Bonzini static struct omap_mpuio_s *omap_mpuio_init(MemoryRegion *memory,
2116dd285b06SPaolo Bonzini                 hwaddr base,
2117dd285b06SPaolo Bonzini                 qemu_irq kbd_int, qemu_irq gpio_int, qemu_irq wakeup,
2118dd285b06SPaolo Bonzini                 omap_clk clk)
2119dd285b06SPaolo Bonzini {
2120b45c03f5SMarkus Armbruster     struct omap_mpuio_s *s = g_new0(struct omap_mpuio_s, 1);
2121dd285b06SPaolo Bonzini 
2122dd285b06SPaolo Bonzini     s->irq = gpio_int;
2123dd285b06SPaolo Bonzini     s->kbd_irq = kbd_int;
2124dd285b06SPaolo Bonzini     s->wakeup = wakeup;
2125dd285b06SPaolo Bonzini     s->in = qemu_allocate_irqs(omap_mpuio_set, s, 16);
2126dd285b06SPaolo Bonzini     omap_mpuio_reset(s);
2127dd285b06SPaolo Bonzini 
21282c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_mpuio_ops, s,
2129dd285b06SPaolo Bonzini                           "omap-mpuio", 0x800);
2130dd285b06SPaolo Bonzini     memory_region_add_subregion(memory, base, &s->iomem);
2131dd285b06SPaolo Bonzini 
2132f3c7d038SAndreas Färber     omap_clk_adduser(clk, qemu_allocate_irq(omap_mpuio_onoff, s, 0));
2133dd285b06SPaolo Bonzini 
2134dd285b06SPaolo Bonzini     return s;
2135dd285b06SPaolo Bonzini }
2136dd285b06SPaolo Bonzini 
2137dd285b06SPaolo Bonzini qemu_irq *omap_mpuio_in_get(struct omap_mpuio_s *s)
2138dd285b06SPaolo Bonzini {
2139dd285b06SPaolo Bonzini     return s->in;
2140dd285b06SPaolo Bonzini }
2141dd285b06SPaolo Bonzini 
2142dd285b06SPaolo Bonzini void omap_mpuio_out_set(struct omap_mpuio_s *s, int line, qemu_irq handler)
2143dd285b06SPaolo Bonzini {
2144dd285b06SPaolo Bonzini     if (line >= 16 || line < 0)
2145a89f364aSAlistair Francis         hw_error("%s: No GPIO line %i\n", __func__, line);
2146dd285b06SPaolo Bonzini     s->handler[line] = handler;
2147dd285b06SPaolo Bonzini }
2148dd285b06SPaolo Bonzini 
2149dd285b06SPaolo Bonzini void omap_mpuio_key(struct omap_mpuio_s *s, int row, int col, int down)
2150dd285b06SPaolo Bonzini {
2151dd285b06SPaolo Bonzini     if (row >= 5 || row < 0)
2152a89f364aSAlistair Francis         hw_error("%s: No key %i-%i\n", __func__, col, row);
2153dd285b06SPaolo Bonzini 
2154dd285b06SPaolo Bonzini     if (down)
2155dd285b06SPaolo Bonzini         s->buttons[row] |= 1 << col;
2156dd285b06SPaolo Bonzini     else
2157dd285b06SPaolo Bonzini         s->buttons[row] &= ~(1 << col);
2158dd285b06SPaolo Bonzini 
2159dd285b06SPaolo Bonzini     omap_mpuio_kbd_update(s);
2160dd285b06SPaolo Bonzini }
2161dd285b06SPaolo Bonzini 
2162dd285b06SPaolo Bonzini /* MicroWire Interface */
2163dd285b06SPaolo Bonzini struct omap_uwire_s {
2164dd285b06SPaolo Bonzini     MemoryRegion iomem;
2165dd285b06SPaolo Bonzini     qemu_irq txirq;
2166dd285b06SPaolo Bonzini     qemu_irq rxirq;
2167dd285b06SPaolo Bonzini     qemu_irq txdrq;
2168dd285b06SPaolo Bonzini 
2169dd285b06SPaolo Bonzini     uint16_t txbuf;
2170dd285b06SPaolo Bonzini     uint16_t rxbuf;
2171dd285b06SPaolo Bonzini     uint16_t control;
2172dd285b06SPaolo Bonzini     uint16_t setup[5];
2173dd285b06SPaolo Bonzini 
2174dd285b06SPaolo Bonzini     uWireSlave *chip[4];
2175dd285b06SPaolo Bonzini };
2176dd285b06SPaolo Bonzini 
2177dd285b06SPaolo Bonzini static void omap_uwire_transfer_start(struct omap_uwire_s *s)
2178dd285b06SPaolo Bonzini {
2179dd285b06SPaolo Bonzini     int chipselect = (s->control >> 10) & 3;		/* INDEX */
2180dd285b06SPaolo Bonzini     uWireSlave *slave = s->chip[chipselect];
2181dd285b06SPaolo Bonzini 
2182dd285b06SPaolo Bonzini     if ((s->control >> 5) & 0x1f) {			/* NB_BITS_WR */
2183dd285b06SPaolo Bonzini         if (s->control & (1 << 12))			/* CS_CMD */
2184dd285b06SPaolo Bonzini             if (slave && slave->send)
2185dd285b06SPaolo Bonzini                 slave->send(slave->opaque,
2186dd285b06SPaolo Bonzini                                 s->txbuf >> (16 - ((s->control >> 5) & 0x1f)));
2187dd285b06SPaolo Bonzini         s->control &= ~(1 << 14);			/* CSRB */
2188dd285b06SPaolo Bonzini         /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
2189dd285b06SPaolo Bonzini          * a DRQ.  When is the level IRQ supposed to be reset?  */
2190dd285b06SPaolo Bonzini     }
2191dd285b06SPaolo Bonzini 
2192dd285b06SPaolo Bonzini     if ((s->control >> 0) & 0x1f) {			/* NB_BITS_RD */
2193dd285b06SPaolo Bonzini         if (s->control & (1 << 12))			/* CS_CMD */
2194dd285b06SPaolo Bonzini             if (slave && slave->receive)
2195dd285b06SPaolo Bonzini                 s->rxbuf = slave->receive(slave->opaque);
2196dd285b06SPaolo Bonzini         s->control |= 1 << 15;				/* RDRB */
2197dd285b06SPaolo Bonzini         /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
2198dd285b06SPaolo Bonzini          * a DRQ.  When is the level IRQ supposed to be reset?  */
2199dd285b06SPaolo Bonzini     }
2200dd285b06SPaolo Bonzini }
2201dd285b06SPaolo Bonzini 
2202dd285b06SPaolo Bonzini static uint64_t omap_uwire_read(void *opaque, hwaddr addr,
2203dd285b06SPaolo Bonzini                                 unsigned size)
2204dd285b06SPaolo Bonzini {
2205dd285b06SPaolo Bonzini     struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
2206dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2207dd285b06SPaolo Bonzini 
2208dd285b06SPaolo Bonzini     if (size != 2) {
2209dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
2210dd285b06SPaolo Bonzini     }
2211dd285b06SPaolo Bonzini 
2212dd285b06SPaolo Bonzini     switch (offset) {
2213dd285b06SPaolo Bonzini     case 0x00:	/* RDR */
2214dd285b06SPaolo Bonzini         s->control &= ~(1 << 15);			/* RDRB */
2215dd285b06SPaolo Bonzini         return s->rxbuf;
2216dd285b06SPaolo Bonzini 
2217dd285b06SPaolo Bonzini     case 0x04:	/* CSR */
2218dd285b06SPaolo Bonzini         return s->control;
2219dd285b06SPaolo Bonzini 
2220dd285b06SPaolo Bonzini     case 0x08:	/* SR1 */
2221dd285b06SPaolo Bonzini         return s->setup[0];
2222dd285b06SPaolo Bonzini     case 0x0c:	/* SR2 */
2223dd285b06SPaolo Bonzini         return s->setup[1];
2224dd285b06SPaolo Bonzini     case 0x10:	/* SR3 */
2225dd285b06SPaolo Bonzini         return s->setup[2];
2226dd285b06SPaolo Bonzini     case 0x14:	/* SR4 */
2227dd285b06SPaolo Bonzini         return s->setup[3];
2228dd285b06SPaolo Bonzini     case 0x18:	/* SR5 */
2229dd285b06SPaolo Bonzini         return s->setup[4];
2230dd285b06SPaolo Bonzini     }
2231dd285b06SPaolo Bonzini 
2232dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
2233dd285b06SPaolo Bonzini     return 0;
2234dd285b06SPaolo Bonzini }
2235dd285b06SPaolo Bonzini 
2236dd285b06SPaolo Bonzini static void omap_uwire_write(void *opaque, hwaddr addr,
2237dd285b06SPaolo Bonzini                              uint64_t value, unsigned size)
2238dd285b06SPaolo Bonzini {
2239dd285b06SPaolo Bonzini     struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
2240dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2241dd285b06SPaolo Bonzini 
2242dd285b06SPaolo Bonzini     if (size != 2) {
224377a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
224477a8257eSStefan Weil         return;
2245dd285b06SPaolo Bonzini     }
2246dd285b06SPaolo Bonzini 
2247dd285b06SPaolo Bonzini     switch (offset) {
2248dd285b06SPaolo Bonzini     case 0x00:	/* TDR */
2249dd285b06SPaolo Bonzini         s->txbuf = value;				/* TD */
2250dd285b06SPaolo Bonzini         if ((s->setup[4] & (1 << 2)) &&			/* AUTO_TX_EN */
2251dd285b06SPaolo Bonzini                         ((s->setup[4] & (1 << 3)) ||	/* CS_TOGGLE_TX_EN */
2252dd285b06SPaolo Bonzini                          (s->control & (1 << 12)))) {	/* CS_CMD */
2253dd285b06SPaolo Bonzini             s->control |= 1 << 14;			/* CSRB */
2254dd285b06SPaolo Bonzini             omap_uwire_transfer_start(s);
2255dd285b06SPaolo Bonzini         }
2256dd285b06SPaolo Bonzini         break;
2257dd285b06SPaolo Bonzini 
2258dd285b06SPaolo Bonzini     case 0x04:	/* CSR */
2259dd285b06SPaolo Bonzini         s->control = value & 0x1fff;
2260dd285b06SPaolo Bonzini         if (value & (1 << 13))				/* START */
2261dd285b06SPaolo Bonzini             omap_uwire_transfer_start(s);
2262dd285b06SPaolo Bonzini         break;
2263dd285b06SPaolo Bonzini 
2264dd285b06SPaolo Bonzini     case 0x08:	/* SR1 */
2265dd285b06SPaolo Bonzini         s->setup[0] = value & 0x003f;
2266dd285b06SPaolo Bonzini         break;
2267dd285b06SPaolo Bonzini 
2268dd285b06SPaolo Bonzini     case 0x0c:	/* SR2 */
2269dd285b06SPaolo Bonzini         s->setup[1] = value & 0x0fc0;
2270dd285b06SPaolo Bonzini         break;
2271dd285b06SPaolo Bonzini 
2272dd285b06SPaolo Bonzini     case 0x10:	/* SR3 */
2273dd285b06SPaolo Bonzini         s->setup[2] = value & 0x0003;
2274dd285b06SPaolo Bonzini         break;
2275dd285b06SPaolo Bonzini 
2276dd285b06SPaolo Bonzini     case 0x14:	/* SR4 */
2277dd285b06SPaolo Bonzini         s->setup[3] = value & 0x0001;
2278dd285b06SPaolo Bonzini         break;
2279dd285b06SPaolo Bonzini 
2280dd285b06SPaolo Bonzini     case 0x18:	/* SR5 */
2281dd285b06SPaolo Bonzini         s->setup[4] = value & 0x000f;
2282dd285b06SPaolo Bonzini         break;
2283dd285b06SPaolo Bonzini 
2284dd285b06SPaolo Bonzini     default:
2285dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
2286dd285b06SPaolo Bonzini         return;
2287dd285b06SPaolo Bonzini     }
2288dd285b06SPaolo Bonzini }
2289dd285b06SPaolo Bonzini 
2290dd285b06SPaolo Bonzini static const MemoryRegionOps omap_uwire_ops = {
2291dd285b06SPaolo Bonzini     .read = omap_uwire_read,
2292dd285b06SPaolo Bonzini     .write = omap_uwire_write,
2293dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
2294dd285b06SPaolo Bonzini };
2295dd285b06SPaolo Bonzini 
2296dd285b06SPaolo Bonzini static void omap_uwire_reset(struct omap_uwire_s *s)
2297dd285b06SPaolo Bonzini {
2298dd285b06SPaolo Bonzini     s->control = 0;
2299dd285b06SPaolo Bonzini     s->setup[0] = 0;
2300dd285b06SPaolo Bonzini     s->setup[1] = 0;
2301dd285b06SPaolo Bonzini     s->setup[2] = 0;
2302dd285b06SPaolo Bonzini     s->setup[3] = 0;
2303dd285b06SPaolo Bonzini     s->setup[4] = 0;
2304dd285b06SPaolo Bonzini }
2305dd285b06SPaolo Bonzini 
2306dd285b06SPaolo Bonzini static struct omap_uwire_s *omap_uwire_init(MemoryRegion *system_memory,
2307dd285b06SPaolo Bonzini                                             hwaddr base,
2308dd285b06SPaolo Bonzini                                             qemu_irq txirq, qemu_irq rxirq,
2309dd285b06SPaolo Bonzini                                             qemu_irq dma,
2310dd285b06SPaolo Bonzini                                             omap_clk clk)
2311dd285b06SPaolo Bonzini {
2312b45c03f5SMarkus Armbruster     struct omap_uwire_s *s = g_new0(struct omap_uwire_s, 1);
2313dd285b06SPaolo Bonzini 
2314dd285b06SPaolo Bonzini     s->txirq = txirq;
2315dd285b06SPaolo Bonzini     s->rxirq = rxirq;
2316dd285b06SPaolo Bonzini     s->txdrq = dma;
2317dd285b06SPaolo Bonzini     omap_uwire_reset(s);
2318dd285b06SPaolo Bonzini 
23192c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_uwire_ops, s, "omap-uwire", 0x800);
2320dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
2321dd285b06SPaolo Bonzini 
2322dd285b06SPaolo Bonzini     return s;
2323dd285b06SPaolo Bonzini }
2324dd285b06SPaolo Bonzini 
2325dd285b06SPaolo Bonzini void omap_uwire_attach(struct omap_uwire_s *s,
2326dd285b06SPaolo Bonzini                 uWireSlave *slave, int chipselect)
2327dd285b06SPaolo Bonzini {
2328dd285b06SPaolo Bonzini     if (chipselect < 0 || chipselect > 3) {
2329c0dbca36SAlistair Francis         error_report("%s: Bad chipselect %i", __func__, chipselect);
2330dd285b06SPaolo Bonzini         exit(-1);
2331dd285b06SPaolo Bonzini     }
2332dd285b06SPaolo Bonzini 
2333dd285b06SPaolo Bonzini     s->chip[chipselect] = slave;
2334dd285b06SPaolo Bonzini }
2335dd285b06SPaolo Bonzini 
2336dd285b06SPaolo Bonzini /* Pseudonoise Pulse-Width Light Modulator */
2337dd285b06SPaolo Bonzini struct omap_pwl_s {
2338dd285b06SPaolo Bonzini     MemoryRegion iomem;
2339dd285b06SPaolo Bonzini     uint8_t output;
2340dd285b06SPaolo Bonzini     uint8_t level;
2341dd285b06SPaolo Bonzini     uint8_t enable;
2342dd285b06SPaolo Bonzini     int clk;
2343dd285b06SPaolo Bonzini };
2344dd285b06SPaolo Bonzini 
2345dd285b06SPaolo Bonzini static void omap_pwl_update(struct omap_pwl_s *s)
2346dd285b06SPaolo Bonzini {
2347dd285b06SPaolo Bonzini     int output = (s->clk && s->enable) ? s->level : 0;
2348dd285b06SPaolo Bonzini 
2349dd285b06SPaolo Bonzini     if (output != s->output) {
2350dd285b06SPaolo Bonzini         s->output = output;
2351a89f364aSAlistair Francis         printf("%s: Backlight now at %i/256\n", __func__, output);
2352dd285b06SPaolo Bonzini     }
2353dd285b06SPaolo Bonzini }
2354dd285b06SPaolo Bonzini 
2355dd285b06SPaolo Bonzini static uint64_t omap_pwl_read(void *opaque, hwaddr addr,
2356dd285b06SPaolo Bonzini                               unsigned size)
2357dd285b06SPaolo Bonzini {
2358dd285b06SPaolo Bonzini     struct omap_pwl_s *s = (struct omap_pwl_s *) opaque;
2359dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2360dd285b06SPaolo Bonzini 
2361dd285b06SPaolo Bonzini     if (size != 1) {
2362dd285b06SPaolo Bonzini         return omap_badwidth_read8(opaque, addr);
2363dd285b06SPaolo Bonzini     }
2364dd285b06SPaolo Bonzini 
2365dd285b06SPaolo Bonzini     switch (offset) {
2366dd285b06SPaolo Bonzini     case 0x00:	/* PWL_LEVEL */
2367dd285b06SPaolo Bonzini         return s->level;
2368dd285b06SPaolo Bonzini     case 0x04:	/* PWL_CTRL */
2369dd285b06SPaolo Bonzini         return s->enable;
2370dd285b06SPaolo Bonzini     }
2371dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
2372dd285b06SPaolo Bonzini     return 0;
2373dd285b06SPaolo Bonzini }
2374dd285b06SPaolo Bonzini 
2375dd285b06SPaolo Bonzini static void omap_pwl_write(void *opaque, hwaddr addr,
2376dd285b06SPaolo Bonzini                            uint64_t value, unsigned size)
2377dd285b06SPaolo Bonzini {
2378dd285b06SPaolo Bonzini     struct omap_pwl_s *s = (struct omap_pwl_s *) opaque;
2379dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2380dd285b06SPaolo Bonzini 
2381dd285b06SPaolo Bonzini     if (size != 1) {
238277a8257eSStefan Weil         omap_badwidth_write8(opaque, addr, value);
238377a8257eSStefan Weil         return;
2384dd285b06SPaolo Bonzini     }
2385dd285b06SPaolo Bonzini 
2386dd285b06SPaolo Bonzini     switch (offset) {
2387dd285b06SPaolo Bonzini     case 0x00:	/* PWL_LEVEL */
2388dd285b06SPaolo Bonzini         s->level = value;
2389dd285b06SPaolo Bonzini         omap_pwl_update(s);
2390dd285b06SPaolo Bonzini         break;
2391dd285b06SPaolo Bonzini     case 0x04:	/* PWL_CTRL */
2392dd285b06SPaolo Bonzini         s->enable = value & 1;
2393dd285b06SPaolo Bonzini         omap_pwl_update(s);
2394dd285b06SPaolo Bonzini         break;
2395dd285b06SPaolo Bonzini     default:
2396dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
2397dd285b06SPaolo Bonzini         return;
2398dd285b06SPaolo Bonzini     }
2399dd285b06SPaolo Bonzini }
2400dd285b06SPaolo Bonzini 
2401dd285b06SPaolo Bonzini static const MemoryRegionOps omap_pwl_ops = {
2402dd285b06SPaolo Bonzini     .read = omap_pwl_read,
2403dd285b06SPaolo Bonzini     .write = omap_pwl_write,
2404dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
2405dd285b06SPaolo Bonzini };
2406dd285b06SPaolo Bonzini 
2407dd285b06SPaolo Bonzini static void omap_pwl_reset(struct omap_pwl_s *s)
2408dd285b06SPaolo Bonzini {
2409dd285b06SPaolo Bonzini     s->output = 0;
2410dd285b06SPaolo Bonzini     s->level = 0;
2411dd285b06SPaolo Bonzini     s->enable = 0;
2412dd285b06SPaolo Bonzini     s->clk = 1;
2413dd285b06SPaolo Bonzini     omap_pwl_update(s);
2414dd285b06SPaolo Bonzini }
2415dd285b06SPaolo Bonzini 
2416dd285b06SPaolo Bonzini static void omap_pwl_clk_update(void *opaque, int line, int on)
2417dd285b06SPaolo Bonzini {
2418dd285b06SPaolo Bonzini     struct omap_pwl_s *s = (struct omap_pwl_s *) opaque;
2419dd285b06SPaolo Bonzini 
2420dd285b06SPaolo Bonzini     s->clk = on;
2421dd285b06SPaolo Bonzini     omap_pwl_update(s);
2422dd285b06SPaolo Bonzini }
2423dd285b06SPaolo Bonzini 
2424dd285b06SPaolo Bonzini static struct omap_pwl_s *omap_pwl_init(MemoryRegion *system_memory,
2425dd285b06SPaolo Bonzini                                         hwaddr base,
2426dd285b06SPaolo Bonzini                                         omap_clk clk)
2427dd285b06SPaolo Bonzini {
2428dd285b06SPaolo Bonzini     struct omap_pwl_s *s = g_malloc0(sizeof(*s));
2429dd285b06SPaolo Bonzini 
2430dd285b06SPaolo Bonzini     omap_pwl_reset(s);
2431dd285b06SPaolo Bonzini 
24322c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_pwl_ops, s,
2433dd285b06SPaolo Bonzini                           "omap-pwl", 0x800);
2434dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
2435dd285b06SPaolo Bonzini 
2436f3c7d038SAndreas Färber     omap_clk_adduser(clk, qemu_allocate_irq(omap_pwl_clk_update, s, 0));
2437dd285b06SPaolo Bonzini     return s;
2438dd285b06SPaolo Bonzini }
2439dd285b06SPaolo Bonzini 
2440dd285b06SPaolo Bonzini /* Pulse-Width Tone module */
2441dd285b06SPaolo Bonzini struct omap_pwt_s {
2442dd285b06SPaolo Bonzini     MemoryRegion iomem;
2443dd285b06SPaolo Bonzini     uint8_t frc;
2444dd285b06SPaolo Bonzini     uint8_t vrc;
2445dd285b06SPaolo Bonzini     uint8_t gcr;
2446dd285b06SPaolo Bonzini     omap_clk clk;
2447dd285b06SPaolo Bonzini };
2448dd285b06SPaolo Bonzini 
2449dd285b06SPaolo Bonzini static uint64_t omap_pwt_read(void *opaque, hwaddr addr,
2450dd285b06SPaolo Bonzini                               unsigned size)
2451dd285b06SPaolo Bonzini {
2452dd285b06SPaolo Bonzini     struct omap_pwt_s *s = (struct omap_pwt_s *) opaque;
2453dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2454dd285b06SPaolo Bonzini 
2455dd285b06SPaolo Bonzini     if (size != 1) {
2456dd285b06SPaolo Bonzini         return omap_badwidth_read8(opaque, addr);
2457dd285b06SPaolo Bonzini     }
2458dd285b06SPaolo Bonzini 
2459dd285b06SPaolo Bonzini     switch (offset) {
2460dd285b06SPaolo Bonzini     case 0x00:	/* FRC */
2461dd285b06SPaolo Bonzini         return s->frc;
2462dd285b06SPaolo Bonzini     case 0x04:	/* VCR */
2463dd285b06SPaolo Bonzini         return s->vrc;
2464dd285b06SPaolo Bonzini     case 0x08:	/* GCR */
2465dd285b06SPaolo Bonzini         return s->gcr;
2466dd285b06SPaolo Bonzini     }
2467dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
2468dd285b06SPaolo Bonzini     return 0;
2469dd285b06SPaolo Bonzini }
2470dd285b06SPaolo Bonzini 
2471dd285b06SPaolo Bonzini static void omap_pwt_write(void *opaque, hwaddr addr,
2472dd285b06SPaolo Bonzini                            uint64_t value, unsigned size)
2473dd285b06SPaolo Bonzini {
2474dd285b06SPaolo Bonzini     struct omap_pwt_s *s = (struct omap_pwt_s *) opaque;
2475dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2476dd285b06SPaolo Bonzini 
2477dd285b06SPaolo Bonzini     if (size != 1) {
247877a8257eSStefan Weil         omap_badwidth_write8(opaque, addr, value);
247977a8257eSStefan Weil         return;
2480dd285b06SPaolo Bonzini     }
2481dd285b06SPaolo Bonzini 
2482dd285b06SPaolo Bonzini     switch (offset) {
2483dd285b06SPaolo Bonzini     case 0x00:	/* FRC */
2484dd285b06SPaolo Bonzini         s->frc = value & 0x3f;
2485dd285b06SPaolo Bonzini         break;
2486dd285b06SPaolo Bonzini     case 0x04:	/* VRC */
2487dd285b06SPaolo Bonzini         if ((value ^ s->vrc) & 1) {
2488dd285b06SPaolo Bonzini             if (value & 1)
2489a89f364aSAlistair Francis                 printf("%s: %iHz buzz on\n", __func__, (int)
2490dd285b06SPaolo Bonzini                                 /* 1.5 MHz from a 12-MHz or 13-MHz PWT_CLK */
2491dd285b06SPaolo Bonzini                                 ((omap_clk_getrate(s->clk) >> 3) /
2492dd285b06SPaolo Bonzini                                  /* Pre-multiplexer divider */
2493dd285b06SPaolo Bonzini                                  ((s->gcr & 2) ? 1 : 154) /
2494dd285b06SPaolo Bonzini                                  /* Octave multiplexer */
2495dd285b06SPaolo Bonzini                                  (2 << (value & 3)) *
2496dd285b06SPaolo Bonzini                                  /* 101/107 divider */
2497dd285b06SPaolo Bonzini                                  ((value & (1 << 2)) ? 101 : 107) *
2498dd285b06SPaolo Bonzini                                  /*  49/55 divider */
2499dd285b06SPaolo Bonzini                                  ((value & (1 << 3)) ?  49 : 55) *
2500dd285b06SPaolo Bonzini                                  /*  50/63 divider */
2501dd285b06SPaolo Bonzini                                  ((value & (1 << 4)) ?  50 : 63) *
2502dd285b06SPaolo Bonzini                                  /*  80/127 divider */
2503dd285b06SPaolo Bonzini                                  ((value & (1 << 5)) ?  80 : 127) /
2504dd285b06SPaolo Bonzini                                  (107 * 55 * 63 * 127)));
2505dd285b06SPaolo Bonzini             else
2506a89f364aSAlistair Francis                 printf("%s: silence!\n", __func__);
2507dd285b06SPaolo Bonzini         }
2508dd285b06SPaolo Bonzini         s->vrc = value & 0x7f;
2509dd285b06SPaolo Bonzini         break;
2510dd285b06SPaolo Bonzini     case 0x08:	/* GCR */
2511dd285b06SPaolo Bonzini         s->gcr = value & 3;
2512dd285b06SPaolo Bonzini         break;
2513dd285b06SPaolo Bonzini     default:
2514dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
2515dd285b06SPaolo Bonzini         return;
2516dd285b06SPaolo Bonzini     }
2517dd285b06SPaolo Bonzini }
2518dd285b06SPaolo Bonzini 
2519dd285b06SPaolo Bonzini static const MemoryRegionOps omap_pwt_ops = {
2520dd285b06SPaolo Bonzini     .read =omap_pwt_read,
2521dd285b06SPaolo Bonzini     .write = omap_pwt_write,
2522dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
2523dd285b06SPaolo Bonzini };
2524dd285b06SPaolo Bonzini 
2525dd285b06SPaolo Bonzini static void omap_pwt_reset(struct omap_pwt_s *s)
2526dd285b06SPaolo Bonzini {
2527dd285b06SPaolo Bonzini     s->frc = 0;
2528dd285b06SPaolo Bonzini     s->vrc = 0;
2529dd285b06SPaolo Bonzini     s->gcr = 0;
2530dd285b06SPaolo Bonzini }
2531dd285b06SPaolo Bonzini 
2532dd285b06SPaolo Bonzini static struct omap_pwt_s *omap_pwt_init(MemoryRegion *system_memory,
2533dd285b06SPaolo Bonzini                                         hwaddr base,
2534dd285b06SPaolo Bonzini                                         omap_clk clk)
2535dd285b06SPaolo Bonzini {
2536dd285b06SPaolo Bonzini     struct omap_pwt_s *s = g_malloc0(sizeof(*s));
2537dd285b06SPaolo Bonzini     s->clk = clk;
2538dd285b06SPaolo Bonzini     omap_pwt_reset(s);
2539dd285b06SPaolo Bonzini 
25402c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_pwt_ops, s,
2541dd285b06SPaolo Bonzini                           "omap-pwt", 0x800);
2542dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
2543dd285b06SPaolo Bonzini     return s;
2544dd285b06SPaolo Bonzini }
2545dd285b06SPaolo Bonzini 
2546dd285b06SPaolo Bonzini /* Real-time Clock module */
2547dd285b06SPaolo Bonzini struct omap_rtc_s {
2548dd285b06SPaolo Bonzini     MemoryRegion iomem;
2549dd285b06SPaolo Bonzini     qemu_irq irq;
2550dd285b06SPaolo Bonzini     qemu_irq alarm;
2551dd285b06SPaolo Bonzini     QEMUTimer *clk;
2552dd285b06SPaolo Bonzini 
2553dd285b06SPaolo Bonzini     uint8_t interrupts;
2554dd285b06SPaolo Bonzini     uint8_t status;
2555dd285b06SPaolo Bonzini     int16_t comp_reg;
2556dd285b06SPaolo Bonzini     int running;
2557dd285b06SPaolo Bonzini     int pm_am;
2558dd285b06SPaolo Bonzini     int auto_comp;
2559dd285b06SPaolo Bonzini     int round;
2560dd285b06SPaolo Bonzini     struct tm alarm_tm;
2561dd285b06SPaolo Bonzini     time_t alarm_ti;
2562dd285b06SPaolo Bonzini 
2563dd285b06SPaolo Bonzini     struct tm current_tm;
2564dd285b06SPaolo Bonzini     time_t ti;
2565dd285b06SPaolo Bonzini     uint64_t tick;
2566dd285b06SPaolo Bonzini };
2567dd285b06SPaolo Bonzini 
2568dd285b06SPaolo Bonzini static void omap_rtc_interrupts_update(struct omap_rtc_s *s)
2569dd285b06SPaolo Bonzini {
2570dd285b06SPaolo Bonzini     /* s->alarm is level-triggered */
2571dd285b06SPaolo Bonzini     qemu_set_irq(s->alarm, (s->status >> 6) & 1);
2572dd285b06SPaolo Bonzini }
2573dd285b06SPaolo Bonzini 
2574dd285b06SPaolo Bonzini static void omap_rtc_alarm_update(struct omap_rtc_s *s)
2575dd285b06SPaolo Bonzini {
2576dd285b06SPaolo Bonzini     s->alarm_ti = mktimegm(&s->alarm_tm);
2577dd285b06SPaolo Bonzini     if (s->alarm_ti == -1)
2578a89f364aSAlistair Francis         printf("%s: conversion failed\n", __func__);
2579dd285b06SPaolo Bonzini }
2580dd285b06SPaolo Bonzini 
2581dd285b06SPaolo Bonzini static uint64_t omap_rtc_read(void *opaque, hwaddr addr,
2582dd285b06SPaolo Bonzini                               unsigned size)
2583dd285b06SPaolo Bonzini {
2584dd285b06SPaolo Bonzini     struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
2585dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2586dd285b06SPaolo Bonzini     uint8_t i;
2587dd285b06SPaolo Bonzini 
2588dd285b06SPaolo Bonzini     if (size != 1) {
2589dd285b06SPaolo Bonzini         return omap_badwidth_read8(opaque, addr);
2590dd285b06SPaolo Bonzini     }
2591dd285b06SPaolo Bonzini 
2592dd285b06SPaolo Bonzini     switch (offset) {
2593dd285b06SPaolo Bonzini     case 0x00:	/* SECONDS_REG */
2594dd285b06SPaolo Bonzini         return to_bcd(s->current_tm.tm_sec);
2595dd285b06SPaolo Bonzini 
2596dd285b06SPaolo Bonzini     case 0x04:	/* MINUTES_REG */
2597dd285b06SPaolo Bonzini         return to_bcd(s->current_tm.tm_min);
2598dd285b06SPaolo Bonzini 
2599dd285b06SPaolo Bonzini     case 0x08:	/* HOURS_REG */
2600dd285b06SPaolo Bonzini         if (s->pm_am)
2601dd285b06SPaolo Bonzini             return ((s->current_tm.tm_hour > 11) << 7) |
2602dd285b06SPaolo Bonzini                     to_bcd(((s->current_tm.tm_hour - 1) % 12) + 1);
2603dd285b06SPaolo Bonzini         else
2604dd285b06SPaolo Bonzini             return to_bcd(s->current_tm.tm_hour);
2605dd285b06SPaolo Bonzini 
2606dd285b06SPaolo Bonzini     case 0x0c:	/* DAYS_REG */
2607dd285b06SPaolo Bonzini         return to_bcd(s->current_tm.tm_mday);
2608dd285b06SPaolo Bonzini 
2609dd285b06SPaolo Bonzini     case 0x10:	/* MONTHS_REG */
2610dd285b06SPaolo Bonzini         return to_bcd(s->current_tm.tm_mon + 1);
2611dd285b06SPaolo Bonzini 
2612dd285b06SPaolo Bonzini     case 0x14:	/* YEARS_REG */
2613dd285b06SPaolo Bonzini         return to_bcd(s->current_tm.tm_year % 100);
2614dd285b06SPaolo Bonzini 
2615dd285b06SPaolo Bonzini     case 0x18:	/* WEEK_REG */
2616dd285b06SPaolo Bonzini         return s->current_tm.tm_wday;
2617dd285b06SPaolo Bonzini 
2618dd285b06SPaolo Bonzini     case 0x20:	/* ALARM_SECONDS_REG */
2619dd285b06SPaolo Bonzini         return to_bcd(s->alarm_tm.tm_sec);
2620dd285b06SPaolo Bonzini 
2621dd285b06SPaolo Bonzini     case 0x24:	/* ALARM_MINUTES_REG */
2622dd285b06SPaolo Bonzini         return to_bcd(s->alarm_tm.tm_min);
2623dd285b06SPaolo Bonzini 
2624dd285b06SPaolo Bonzini     case 0x28:	/* ALARM_HOURS_REG */
2625dd285b06SPaolo Bonzini         if (s->pm_am)
2626dd285b06SPaolo Bonzini             return ((s->alarm_tm.tm_hour > 11) << 7) |
2627dd285b06SPaolo Bonzini                     to_bcd(((s->alarm_tm.tm_hour - 1) % 12) + 1);
2628dd285b06SPaolo Bonzini         else
2629dd285b06SPaolo Bonzini             return to_bcd(s->alarm_tm.tm_hour);
2630dd285b06SPaolo Bonzini 
2631dd285b06SPaolo Bonzini     case 0x2c:	/* ALARM_DAYS_REG */
2632dd285b06SPaolo Bonzini         return to_bcd(s->alarm_tm.tm_mday);
2633dd285b06SPaolo Bonzini 
2634dd285b06SPaolo Bonzini     case 0x30:	/* ALARM_MONTHS_REG */
2635dd285b06SPaolo Bonzini         return to_bcd(s->alarm_tm.tm_mon + 1);
2636dd285b06SPaolo Bonzini 
2637dd285b06SPaolo Bonzini     case 0x34:	/* ALARM_YEARS_REG */
2638dd285b06SPaolo Bonzini         return to_bcd(s->alarm_tm.tm_year % 100);
2639dd285b06SPaolo Bonzini 
2640dd285b06SPaolo Bonzini     case 0x40:	/* RTC_CTRL_REG */
2641dd285b06SPaolo Bonzini         return (s->pm_am << 3) | (s->auto_comp << 2) |
2642dd285b06SPaolo Bonzini                 (s->round << 1) | s->running;
2643dd285b06SPaolo Bonzini 
2644dd285b06SPaolo Bonzini     case 0x44:	/* RTC_STATUS_REG */
2645dd285b06SPaolo Bonzini         i = s->status;
2646dd285b06SPaolo Bonzini         s->status &= ~0x3d;
2647dd285b06SPaolo Bonzini         return i;
2648dd285b06SPaolo Bonzini 
2649dd285b06SPaolo Bonzini     case 0x48:	/* RTC_INTERRUPTS_REG */
2650dd285b06SPaolo Bonzini         return s->interrupts;
2651dd285b06SPaolo Bonzini 
2652dd285b06SPaolo Bonzini     case 0x4c:	/* RTC_COMP_LSB_REG */
2653dd285b06SPaolo Bonzini         return ((uint16_t) s->comp_reg) & 0xff;
2654dd285b06SPaolo Bonzini 
2655dd285b06SPaolo Bonzini     case 0x50:	/* RTC_COMP_MSB_REG */
2656dd285b06SPaolo Bonzini         return ((uint16_t) s->comp_reg) >> 8;
2657dd285b06SPaolo Bonzini     }
2658dd285b06SPaolo Bonzini 
2659dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
2660dd285b06SPaolo Bonzini     return 0;
2661dd285b06SPaolo Bonzini }
2662dd285b06SPaolo Bonzini 
2663dd285b06SPaolo Bonzini static void omap_rtc_write(void *opaque, hwaddr addr,
2664dd285b06SPaolo Bonzini                            uint64_t value, unsigned size)
2665dd285b06SPaolo Bonzini {
2666dd285b06SPaolo Bonzini     struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
2667dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
2668dd285b06SPaolo Bonzini     struct tm new_tm;
2669dd285b06SPaolo Bonzini     time_t ti[2];
2670dd285b06SPaolo Bonzini 
2671dd285b06SPaolo Bonzini     if (size != 1) {
267277a8257eSStefan Weil         omap_badwidth_write8(opaque, addr, value);
267377a8257eSStefan Weil         return;
2674dd285b06SPaolo Bonzini     }
2675dd285b06SPaolo Bonzini 
2676dd285b06SPaolo Bonzini     switch (offset) {
2677dd285b06SPaolo Bonzini     case 0x00:	/* SECONDS_REG */
2678dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2679dd285b06SPaolo Bonzini         printf("RTC SEC_REG <-- %02x\n", value);
2680dd285b06SPaolo Bonzini #endif
2681dd285b06SPaolo Bonzini         s->ti -= s->current_tm.tm_sec;
2682dd285b06SPaolo Bonzini         s->ti += from_bcd(value);
2683dd285b06SPaolo Bonzini         return;
2684dd285b06SPaolo Bonzini 
2685dd285b06SPaolo Bonzini     case 0x04:	/* MINUTES_REG */
2686dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2687dd285b06SPaolo Bonzini         printf("RTC MIN_REG <-- %02x\n", value);
2688dd285b06SPaolo Bonzini #endif
2689dd285b06SPaolo Bonzini         s->ti -= s->current_tm.tm_min * 60;
2690dd285b06SPaolo Bonzini         s->ti += from_bcd(value) * 60;
2691dd285b06SPaolo Bonzini         return;
2692dd285b06SPaolo Bonzini 
2693dd285b06SPaolo Bonzini     case 0x08:	/* HOURS_REG */
2694dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2695dd285b06SPaolo Bonzini         printf("RTC HRS_REG <-- %02x\n", value);
2696dd285b06SPaolo Bonzini #endif
2697dd285b06SPaolo Bonzini         s->ti -= s->current_tm.tm_hour * 3600;
2698dd285b06SPaolo Bonzini         if (s->pm_am) {
2699dd285b06SPaolo Bonzini             s->ti += (from_bcd(value & 0x3f) & 12) * 3600;
2700dd285b06SPaolo Bonzini             s->ti += ((value >> 7) & 1) * 43200;
2701dd285b06SPaolo Bonzini         } else
2702dd285b06SPaolo Bonzini             s->ti += from_bcd(value & 0x3f) * 3600;
2703dd285b06SPaolo Bonzini         return;
2704dd285b06SPaolo Bonzini 
2705dd285b06SPaolo Bonzini     case 0x0c:	/* DAYS_REG */
2706dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2707dd285b06SPaolo Bonzini         printf("RTC DAY_REG <-- %02x\n", value);
2708dd285b06SPaolo Bonzini #endif
2709dd285b06SPaolo Bonzini         s->ti -= s->current_tm.tm_mday * 86400;
2710dd285b06SPaolo Bonzini         s->ti += from_bcd(value) * 86400;
2711dd285b06SPaolo Bonzini         return;
2712dd285b06SPaolo Bonzini 
2713dd285b06SPaolo Bonzini     case 0x10:	/* MONTHS_REG */
2714dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2715dd285b06SPaolo Bonzini         printf("RTC MTH_REG <-- %02x\n", value);
2716dd285b06SPaolo Bonzini #endif
2717dd285b06SPaolo Bonzini         memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
2718dd285b06SPaolo Bonzini         new_tm.tm_mon = from_bcd(value);
2719dd285b06SPaolo Bonzini         ti[0] = mktimegm(&s->current_tm);
2720dd285b06SPaolo Bonzini         ti[1] = mktimegm(&new_tm);
2721dd285b06SPaolo Bonzini 
2722dd285b06SPaolo Bonzini         if (ti[0] != -1 && ti[1] != -1) {
2723dd285b06SPaolo Bonzini             s->ti -= ti[0];
2724dd285b06SPaolo Bonzini             s->ti += ti[1];
2725dd285b06SPaolo Bonzini         } else {
2726dd285b06SPaolo Bonzini             /* A less accurate version */
2727dd285b06SPaolo Bonzini             s->ti -= s->current_tm.tm_mon * 2592000;
2728dd285b06SPaolo Bonzini             s->ti += from_bcd(value) * 2592000;
2729dd285b06SPaolo Bonzini         }
2730dd285b06SPaolo Bonzini         return;
2731dd285b06SPaolo Bonzini 
2732dd285b06SPaolo Bonzini     case 0x14:	/* YEARS_REG */
2733dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2734dd285b06SPaolo Bonzini         printf("RTC YRS_REG <-- %02x\n", value);
2735dd285b06SPaolo Bonzini #endif
2736dd285b06SPaolo Bonzini         memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
2737dd285b06SPaolo Bonzini         new_tm.tm_year += from_bcd(value) - (new_tm.tm_year % 100);
2738dd285b06SPaolo Bonzini         ti[0] = mktimegm(&s->current_tm);
2739dd285b06SPaolo Bonzini         ti[1] = mktimegm(&new_tm);
2740dd285b06SPaolo Bonzini 
2741dd285b06SPaolo Bonzini         if (ti[0] != -1 && ti[1] != -1) {
2742dd285b06SPaolo Bonzini             s->ti -= ti[0];
2743dd285b06SPaolo Bonzini             s->ti += ti[1];
2744dd285b06SPaolo Bonzini         } else {
2745dd285b06SPaolo Bonzini             /* A less accurate version */
27467e7e5858SPeter Maydell             s->ti -= (time_t)(s->current_tm.tm_year % 100) * 31536000;
27477e7e5858SPeter Maydell             s->ti += (time_t)from_bcd(value) * 31536000;
2748dd285b06SPaolo Bonzini         }
2749dd285b06SPaolo Bonzini         return;
2750dd285b06SPaolo Bonzini 
2751dd285b06SPaolo Bonzini     case 0x18:	/* WEEK_REG */
2752dd285b06SPaolo Bonzini         return;	/* Ignored */
2753dd285b06SPaolo Bonzini 
2754dd285b06SPaolo Bonzini     case 0x20:	/* ALARM_SECONDS_REG */
2755dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2756dd285b06SPaolo Bonzini         printf("ALM SEC_REG <-- %02x\n", value);
2757dd285b06SPaolo Bonzini #endif
2758dd285b06SPaolo Bonzini         s->alarm_tm.tm_sec = from_bcd(value);
2759dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2760dd285b06SPaolo Bonzini         return;
2761dd285b06SPaolo Bonzini 
2762dd285b06SPaolo Bonzini     case 0x24:	/* ALARM_MINUTES_REG */
2763dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2764dd285b06SPaolo Bonzini         printf("ALM MIN_REG <-- %02x\n", value);
2765dd285b06SPaolo Bonzini #endif
2766dd285b06SPaolo Bonzini         s->alarm_tm.tm_min = from_bcd(value);
2767dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2768dd285b06SPaolo Bonzini         return;
2769dd285b06SPaolo Bonzini 
2770dd285b06SPaolo Bonzini     case 0x28:	/* ALARM_HOURS_REG */
2771dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2772dd285b06SPaolo Bonzini         printf("ALM HRS_REG <-- %02x\n", value);
2773dd285b06SPaolo Bonzini #endif
2774dd285b06SPaolo Bonzini         if (s->pm_am)
2775dd285b06SPaolo Bonzini             s->alarm_tm.tm_hour =
2776dd285b06SPaolo Bonzini                     ((from_bcd(value & 0x3f)) % 12) +
2777dd285b06SPaolo Bonzini                     ((value >> 7) & 1) * 12;
2778dd285b06SPaolo Bonzini         else
2779dd285b06SPaolo Bonzini             s->alarm_tm.tm_hour = from_bcd(value);
2780dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2781dd285b06SPaolo Bonzini         return;
2782dd285b06SPaolo Bonzini 
2783dd285b06SPaolo Bonzini     case 0x2c:	/* ALARM_DAYS_REG */
2784dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2785dd285b06SPaolo Bonzini         printf("ALM DAY_REG <-- %02x\n", value);
2786dd285b06SPaolo Bonzini #endif
2787dd285b06SPaolo Bonzini         s->alarm_tm.tm_mday = from_bcd(value);
2788dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2789dd285b06SPaolo Bonzini         return;
2790dd285b06SPaolo Bonzini 
2791dd285b06SPaolo Bonzini     case 0x30:	/* ALARM_MONTHS_REG */
2792dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2793dd285b06SPaolo Bonzini         printf("ALM MON_REG <-- %02x\n", value);
2794dd285b06SPaolo Bonzini #endif
2795dd285b06SPaolo Bonzini         s->alarm_tm.tm_mon = from_bcd(value);
2796dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2797dd285b06SPaolo Bonzini         return;
2798dd285b06SPaolo Bonzini 
2799dd285b06SPaolo Bonzini     case 0x34:	/* ALARM_YEARS_REG */
2800dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2801dd285b06SPaolo Bonzini         printf("ALM YRS_REG <-- %02x\n", value);
2802dd285b06SPaolo Bonzini #endif
2803dd285b06SPaolo Bonzini         s->alarm_tm.tm_year = from_bcd(value);
2804dd285b06SPaolo Bonzini         omap_rtc_alarm_update(s);
2805dd285b06SPaolo Bonzini         return;
2806dd285b06SPaolo Bonzini 
2807dd285b06SPaolo Bonzini     case 0x40:	/* RTC_CTRL_REG */
2808dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2809dd285b06SPaolo Bonzini         printf("RTC CONTROL <-- %02x\n", value);
2810dd285b06SPaolo Bonzini #endif
2811dd285b06SPaolo Bonzini         s->pm_am = (value >> 3) & 1;
2812dd285b06SPaolo Bonzini         s->auto_comp = (value >> 2) & 1;
2813dd285b06SPaolo Bonzini         s->round = (value >> 1) & 1;
2814dd285b06SPaolo Bonzini         s->running = value & 1;
2815dd285b06SPaolo Bonzini         s->status &= 0xfd;
2816dd285b06SPaolo Bonzini         s->status |= s->running << 1;
2817dd285b06SPaolo Bonzini         return;
2818dd285b06SPaolo Bonzini 
2819dd285b06SPaolo Bonzini     case 0x44:	/* RTC_STATUS_REG */
2820dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2821dd285b06SPaolo Bonzini         printf("RTC STATUSL <-- %02x\n", value);
2822dd285b06SPaolo Bonzini #endif
2823dd285b06SPaolo Bonzini         s->status &= ~((value & 0xc0) ^ 0x80);
2824dd285b06SPaolo Bonzini         omap_rtc_interrupts_update(s);
2825dd285b06SPaolo Bonzini         return;
2826dd285b06SPaolo Bonzini 
2827dd285b06SPaolo Bonzini     case 0x48:	/* RTC_INTERRUPTS_REG */
2828dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2829dd285b06SPaolo Bonzini         printf("RTC INTRS <-- %02x\n", value);
2830dd285b06SPaolo Bonzini #endif
2831dd285b06SPaolo Bonzini         s->interrupts = value;
2832dd285b06SPaolo Bonzini         return;
2833dd285b06SPaolo Bonzini 
2834dd285b06SPaolo Bonzini     case 0x4c:	/* RTC_COMP_LSB_REG */
2835dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2836dd285b06SPaolo Bonzini         printf("RTC COMPLSB <-- %02x\n", value);
2837dd285b06SPaolo Bonzini #endif
2838dd285b06SPaolo Bonzini         s->comp_reg &= 0xff00;
2839dd285b06SPaolo Bonzini         s->comp_reg |= 0x00ff & value;
2840dd285b06SPaolo Bonzini         return;
2841dd285b06SPaolo Bonzini 
2842dd285b06SPaolo Bonzini     case 0x50:	/* RTC_COMP_MSB_REG */
2843dd285b06SPaolo Bonzini #ifdef ALMDEBUG
2844dd285b06SPaolo Bonzini         printf("RTC COMPMSB <-- %02x\n", value);
2845dd285b06SPaolo Bonzini #endif
2846dd285b06SPaolo Bonzini         s->comp_reg &= 0x00ff;
2847dd285b06SPaolo Bonzini         s->comp_reg |= 0xff00 & (value << 8);
2848dd285b06SPaolo Bonzini         return;
2849dd285b06SPaolo Bonzini 
2850dd285b06SPaolo Bonzini     default:
2851dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
2852dd285b06SPaolo Bonzini         return;
2853dd285b06SPaolo Bonzini     }
2854dd285b06SPaolo Bonzini }
2855dd285b06SPaolo Bonzini 
2856dd285b06SPaolo Bonzini static const MemoryRegionOps omap_rtc_ops = {
2857dd285b06SPaolo Bonzini     .read = omap_rtc_read,
2858dd285b06SPaolo Bonzini     .write = omap_rtc_write,
2859dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
2860dd285b06SPaolo Bonzini };
2861dd285b06SPaolo Bonzini 
2862dd285b06SPaolo Bonzini static void omap_rtc_tick(void *opaque)
2863dd285b06SPaolo Bonzini {
2864dd285b06SPaolo Bonzini     struct omap_rtc_s *s = opaque;
2865dd285b06SPaolo Bonzini 
2866dd285b06SPaolo Bonzini     if (s->round) {
2867dd285b06SPaolo Bonzini         /* Round to nearest full minute.  */
2868dd285b06SPaolo Bonzini         if (s->current_tm.tm_sec < 30)
2869dd285b06SPaolo Bonzini             s->ti -= s->current_tm.tm_sec;
2870dd285b06SPaolo Bonzini         else
2871dd285b06SPaolo Bonzini             s->ti += 60 - s->current_tm.tm_sec;
2872dd285b06SPaolo Bonzini 
2873dd285b06SPaolo Bonzini         s->round = 0;
2874dd285b06SPaolo Bonzini     }
2875dd285b06SPaolo Bonzini 
2876dd285b06SPaolo Bonzini     localtime_r(&s->ti, &s->current_tm);
2877dd285b06SPaolo Bonzini 
2878dd285b06SPaolo Bonzini     if ((s->interrupts & 0x08) && s->ti == s->alarm_ti) {
2879dd285b06SPaolo Bonzini         s->status |= 0x40;
2880dd285b06SPaolo Bonzini         omap_rtc_interrupts_update(s);
2881dd285b06SPaolo Bonzini     }
2882dd285b06SPaolo Bonzini 
2883dd285b06SPaolo Bonzini     if (s->interrupts & 0x04)
2884dd285b06SPaolo Bonzini         switch (s->interrupts & 3) {
2885dd285b06SPaolo Bonzini         case 0:
2886dd285b06SPaolo Bonzini             s->status |= 0x04;
2887dd285b06SPaolo Bonzini             qemu_irq_pulse(s->irq);
2888dd285b06SPaolo Bonzini             break;
2889dd285b06SPaolo Bonzini         case 1:
2890dd285b06SPaolo Bonzini             if (s->current_tm.tm_sec)
2891dd285b06SPaolo Bonzini                 break;
2892dd285b06SPaolo Bonzini             s->status |= 0x08;
2893dd285b06SPaolo Bonzini             qemu_irq_pulse(s->irq);
2894dd285b06SPaolo Bonzini             break;
2895dd285b06SPaolo Bonzini         case 2:
2896dd285b06SPaolo Bonzini             if (s->current_tm.tm_sec || s->current_tm.tm_min)
2897dd285b06SPaolo Bonzini                 break;
2898dd285b06SPaolo Bonzini             s->status |= 0x10;
2899dd285b06SPaolo Bonzini             qemu_irq_pulse(s->irq);
2900dd285b06SPaolo Bonzini             break;
2901dd285b06SPaolo Bonzini         case 3:
2902dd285b06SPaolo Bonzini             if (s->current_tm.tm_sec ||
2903dd285b06SPaolo Bonzini                             s->current_tm.tm_min || s->current_tm.tm_hour)
2904dd285b06SPaolo Bonzini                 break;
2905dd285b06SPaolo Bonzini             s->status |= 0x20;
2906dd285b06SPaolo Bonzini             qemu_irq_pulse(s->irq);
2907dd285b06SPaolo Bonzini             break;
2908dd285b06SPaolo Bonzini         }
2909dd285b06SPaolo Bonzini 
2910dd285b06SPaolo Bonzini     /* Move on */
2911dd285b06SPaolo Bonzini     if (s->running)
2912dd285b06SPaolo Bonzini         s->ti ++;
2913dd285b06SPaolo Bonzini     s->tick += 1000;
2914dd285b06SPaolo Bonzini 
2915dd285b06SPaolo Bonzini     /*
2916dd285b06SPaolo Bonzini      * Every full hour add a rough approximation of the compensation
2917dd285b06SPaolo Bonzini      * register to the 32kHz Timer (which drives the RTC) value.
2918dd285b06SPaolo Bonzini      */
2919dd285b06SPaolo Bonzini     if (s->auto_comp && !s->current_tm.tm_sec && !s->current_tm.tm_min)
2920dd285b06SPaolo Bonzini         s->tick += s->comp_reg * 1000 / 32768;
2921dd285b06SPaolo Bonzini 
2922bc72ad67SAlex Bligh     timer_mod(s->clk, s->tick);
2923dd285b06SPaolo Bonzini }
2924dd285b06SPaolo Bonzini 
2925dd285b06SPaolo Bonzini static void omap_rtc_reset(struct omap_rtc_s *s)
2926dd285b06SPaolo Bonzini {
2927dd285b06SPaolo Bonzini     struct tm tm;
2928dd285b06SPaolo Bonzini 
2929dd285b06SPaolo Bonzini     s->interrupts = 0;
2930dd285b06SPaolo Bonzini     s->comp_reg = 0;
2931dd285b06SPaolo Bonzini     s->running = 0;
2932dd285b06SPaolo Bonzini     s->pm_am = 0;
2933dd285b06SPaolo Bonzini     s->auto_comp = 0;
2934dd285b06SPaolo Bonzini     s->round = 0;
2935884f17c2SAlex Bligh     s->tick = qemu_clock_get_ms(rtc_clock);
2936dd285b06SPaolo Bonzini     memset(&s->alarm_tm, 0, sizeof(s->alarm_tm));
2937dd285b06SPaolo Bonzini     s->alarm_tm.tm_mday = 0x01;
2938dd285b06SPaolo Bonzini     s->status = 1 << 7;
2939dd285b06SPaolo Bonzini     qemu_get_timedate(&tm, 0);
2940dd285b06SPaolo Bonzini     s->ti = mktimegm(&tm);
2941dd285b06SPaolo Bonzini 
2942dd285b06SPaolo Bonzini     omap_rtc_alarm_update(s);
2943dd285b06SPaolo Bonzini     omap_rtc_tick(s);
2944dd285b06SPaolo Bonzini }
2945dd285b06SPaolo Bonzini 
2946dd285b06SPaolo Bonzini static struct omap_rtc_s *omap_rtc_init(MemoryRegion *system_memory,
2947dd285b06SPaolo Bonzini                                         hwaddr base,
2948dd285b06SPaolo Bonzini                                         qemu_irq timerirq, qemu_irq alarmirq,
2949dd285b06SPaolo Bonzini                                         omap_clk clk)
2950dd285b06SPaolo Bonzini {
2951b45c03f5SMarkus Armbruster     struct omap_rtc_s *s = g_new0(struct omap_rtc_s, 1);
2952dd285b06SPaolo Bonzini 
2953dd285b06SPaolo Bonzini     s->irq = timerirq;
2954dd285b06SPaolo Bonzini     s->alarm = alarmirq;
2955884f17c2SAlex Bligh     s->clk = timer_new_ms(rtc_clock, omap_rtc_tick, s);
2956dd285b06SPaolo Bonzini 
2957dd285b06SPaolo Bonzini     omap_rtc_reset(s);
2958dd285b06SPaolo Bonzini 
29592c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_rtc_ops, s,
2960dd285b06SPaolo Bonzini                           "omap-rtc", 0x800);
2961dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
2962dd285b06SPaolo Bonzini 
2963dd285b06SPaolo Bonzini     return s;
2964dd285b06SPaolo Bonzini }
2965dd285b06SPaolo Bonzini 
2966dd285b06SPaolo Bonzini /* Multi-channel Buffered Serial Port interfaces */
2967dd285b06SPaolo Bonzini struct omap_mcbsp_s {
2968dd285b06SPaolo Bonzini     MemoryRegion iomem;
2969dd285b06SPaolo Bonzini     qemu_irq txirq;
2970dd285b06SPaolo Bonzini     qemu_irq rxirq;
2971dd285b06SPaolo Bonzini     qemu_irq txdrq;
2972dd285b06SPaolo Bonzini     qemu_irq rxdrq;
2973dd285b06SPaolo Bonzini 
2974dd285b06SPaolo Bonzini     uint16_t spcr[2];
2975dd285b06SPaolo Bonzini     uint16_t rcr[2];
2976dd285b06SPaolo Bonzini     uint16_t xcr[2];
2977dd285b06SPaolo Bonzini     uint16_t srgr[2];
2978dd285b06SPaolo Bonzini     uint16_t mcr[2];
2979dd285b06SPaolo Bonzini     uint16_t pcr;
2980dd285b06SPaolo Bonzini     uint16_t rcer[8];
2981dd285b06SPaolo Bonzini     uint16_t xcer[8];
2982dd285b06SPaolo Bonzini     int tx_rate;
2983dd285b06SPaolo Bonzini     int rx_rate;
2984dd285b06SPaolo Bonzini     int tx_req;
2985dd285b06SPaolo Bonzini     int rx_req;
2986dd285b06SPaolo Bonzini 
2987dd285b06SPaolo Bonzini     I2SCodec *codec;
2988dd285b06SPaolo Bonzini     QEMUTimer *source_timer;
2989dd285b06SPaolo Bonzini     QEMUTimer *sink_timer;
2990dd285b06SPaolo Bonzini };
2991dd285b06SPaolo Bonzini 
2992dd285b06SPaolo Bonzini static void omap_mcbsp_intr_update(struct omap_mcbsp_s *s)
2993dd285b06SPaolo Bonzini {
2994dd285b06SPaolo Bonzini     int irq;
2995dd285b06SPaolo Bonzini 
2996dd285b06SPaolo Bonzini     switch ((s->spcr[0] >> 4) & 3) {			/* RINTM */
2997dd285b06SPaolo Bonzini     case 0:
2998dd285b06SPaolo Bonzini         irq = (s->spcr[0] >> 1) & 1;			/* RRDY */
2999dd285b06SPaolo Bonzini         break;
3000dd285b06SPaolo Bonzini     case 3:
3001dd285b06SPaolo Bonzini         irq = (s->spcr[0] >> 3) & 1;			/* RSYNCERR */
3002dd285b06SPaolo Bonzini         break;
3003dd285b06SPaolo Bonzini     default:
3004dd285b06SPaolo Bonzini         irq = 0;
3005dd285b06SPaolo Bonzini         break;
3006dd285b06SPaolo Bonzini     }
3007dd285b06SPaolo Bonzini 
3008dd285b06SPaolo Bonzini     if (irq)
3009dd285b06SPaolo Bonzini         qemu_irq_pulse(s->rxirq);
3010dd285b06SPaolo Bonzini 
3011dd285b06SPaolo Bonzini     switch ((s->spcr[1] >> 4) & 3) {			/* XINTM */
3012dd285b06SPaolo Bonzini     case 0:
3013dd285b06SPaolo Bonzini         irq = (s->spcr[1] >> 1) & 1;			/* XRDY */
3014dd285b06SPaolo Bonzini         break;
3015dd285b06SPaolo Bonzini     case 3:
3016dd285b06SPaolo Bonzini         irq = (s->spcr[1] >> 3) & 1;			/* XSYNCERR */
3017dd285b06SPaolo Bonzini         break;
3018dd285b06SPaolo Bonzini     default:
3019dd285b06SPaolo Bonzini         irq = 0;
3020dd285b06SPaolo Bonzini         break;
3021dd285b06SPaolo Bonzini     }
3022dd285b06SPaolo Bonzini 
3023dd285b06SPaolo Bonzini     if (irq)
3024dd285b06SPaolo Bonzini         qemu_irq_pulse(s->txirq);
3025dd285b06SPaolo Bonzini }
3026dd285b06SPaolo Bonzini 
3027dd285b06SPaolo Bonzini static void omap_mcbsp_rx_newdata(struct omap_mcbsp_s *s)
3028dd285b06SPaolo Bonzini {
3029dd285b06SPaolo Bonzini     if ((s->spcr[0] >> 1) & 1)				/* RRDY */
3030dd285b06SPaolo Bonzini         s->spcr[0] |= 1 << 2;				/* RFULL */
3031dd285b06SPaolo Bonzini     s->spcr[0] |= 1 << 1;				/* RRDY */
3032dd285b06SPaolo Bonzini     qemu_irq_raise(s->rxdrq);
3033dd285b06SPaolo Bonzini     omap_mcbsp_intr_update(s);
3034dd285b06SPaolo Bonzini }
3035dd285b06SPaolo Bonzini 
3036dd285b06SPaolo Bonzini static void omap_mcbsp_source_tick(void *opaque)
3037dd285b06SPaolo Bonzini {
3038dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3039dd285b06SPaolo Bonzini     static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3040dd285b06SPaolo Bonzini 
3041dd285b06SPaolo Bonzini     if (!s->rx_rate)
3042dd285b06SPaolo Bonzini         return;
3043dd285b06SPaolo Bonzini     if (s->rx_req)
3044a89f364aSAlistair Francis         printf("%s: Rx FIFO overrun\n", __func__);
3045dd285b06SPaolo Bonzini 
3046dd285b06SPaolo Bonzini     s->rx_req = s->rx_rate << bps[(s->rcr[0] >> 5) & 7];
3047dd285b06SPaolo Bonzini 
3048dd285b06SPaolo Bonzini     omap_mcbsp_rx_newdata(s);
3049bc72ad67SAlex Bligh     timer_mod(s->source_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
305073bcb24dSRutuja Shah                    NANOSECONDS_PER_SECOND);
3051dd285b06SPaolo Bonzini }
3052dd285b06SPaolo Bonzini 
3053dd285b06SPaolo Bonzini static void omap_mcbsp_rx_start(struct omap_mcbsp_s *s)
3054dd285b06SPaolo Bonzini {
3055dd285b06SPaolo Bonzini     if (!s->codec || !s->codec->rts)
3056dd285b06SPaolo Bonzini         omap_mcbsp_source_tick(s);
3057dd285b06SPaolo Bonzini     else if (s->codec->in.len) {
3058dd285b06SPaolo Bonzini         s->rx_req = s->codec->in.len;
3059dd285b06SPaolo Bonzini         omap_mcbsp_rx_newdata(s);
3060dd285b06SPaolo Bonzini     }
3061dd285b06SPaolo Bonzini }
3062dd285b06SPaolo Bonzini 
3063dd285b06SPaolo Bonzini static void omap_mcbsp_rx_stop(struct omap_mcbsp_s *s)
3064dd285b06SPaolo Bonzini {
3065bc72ad67SAlex Bligh     timer_del(s->source_timer);
3066dd285b06SPaolo Bonzini }
3067dd285b06SPaolo Bonzini 
3068dd285b06SPaolo Bonzini static void omap_mcbsp_rx_done(struct omap_mcbsp_s *s)
3069dd285b06SPaolo Bonzini {
3070dd285b06SPaolo Bonzini     s->spcr[0] &= ~(1 << 1);				/* RRDY */
3071dd285b06SPaolo Bonzini     qemu_irq_lower(s->rxdrq);
3072dd285b06SPaolo Bonzini     omap_mcbsp_intr_update(s);
3073dd285b06SPaolo Bonzini }
3074dd285b06SPaolo Bonzini 
3075dd285b06SPaolo Bonzini static void omap_mcbsp_tx_newdata(struct omap_mcbsp_s *s)
3076dd285b06SPaolo Bonzini {
3077dd285b06SPaolo Bonzini     s->spcr[1] |= 1 << 1;				/* XRDY */
3078dd285b06SPaolo Bonzini     qemu_irq_raise(s->txdrq);
3079dd285b06SPaolo Bonzini     omap_mcbsp_intr_update(s);
3080dd285b06SPaolo Bonzini }
3081dd285b06SPaolo Bonzini 
3082dd285b06SPaolo Bonzini static void omap_mcbsp_sink_tick(void *opaque)
3083dd285b06SPaolo Bonzini {
3084dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3085dd285b06SPaolo Bonzini     static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3086dd285b06SPaolo Bonzini 
3087dd285b06SPaolo Bonzini     if (!s->tx_rate)
3088dd285b06SPaolo Bonzini         return;
3089dd285b06SPaolo Bonzini     if (s->tx_req)
3090a89f364aSAlistair Francis         printf("%s: Tx FIFO underrun\n", __func__);
3091dd285b06SPaolo Bonzini 
3092dd285b06SPaolo Bonzini     s->tx_req = s->tx_rate << bps[(s->xcr[0] >> 5) & 7];
3093dd285b06SPaolo Bonzini 
3094dd285b06SPaolo Bonzini     omap_mcbsp_tx_newdata(s);
3095bc72ad67SAlex Bligh     timer_mod(s->sink_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
309673bcb24dSRutuja Shah                    NANOSECONDS_PER_SECOND);
3097dd285b06SPaolo Bonzini }
3098dd285b06SPaolo Bonzini 
3099dd285b06SPaolo Bonzini static void omap_mcbsp_tx_start(struct omap_mcbsp_s *s)
3100dd285b06SPaolo Bonzini {
3101dd285b06SPaolo Bonzini     if (!s->codec || !s->codec->cts)
3102dd285b06SPaolo Bonzini         omap_mcbsp_sink_tick(s);
3103dd285b06SPaolo Bonzini     else if (s->codec->out.size) {
3104dd285b06SPaolo Bonzini         s->tx_req = s->codec->out.size;
3105dd285b06SPaolo Bonzini         omap_mcbsp_tx_newdata(s);
3106dd285b06SPaolo Bonzini     }
3107dd285b06SPaolo Bonzini }
3108dd285b06SPaolo Bonzini 
3109dd285b06SPaolo Bonzini static void omap_mcbsp_tx_done(struct omap_mcbsp_s *s)
3110dd285b06SPaolo Bonzini {
3111dd285b06SPaolo Bonzini     s->spcr[1] &= ~(1 << 1);				/* XRDY */
3112dd285b06SPaolo Bonzini     qemu_irq_lower(s->txdrq);
3113dd285b06SPaolo Bonzini     omap_mcbsp_intr_update(s);
3114dd285b06SPaolo Bonzini     if (s->codec && s->codec->cts)
3115dd285b06SPaolo Bonzini         s->codec->tx_swallow(s->codec->opaque);
3116dd285b06SPaolo Bonzini }
3117dd285b06SPaolo Bonzini 
3118dd285b06SPaolo Bonzini static void omap_mcbsp_tx_stop(struct omap_mcbsp_s *s)
3119dd285b06SPaolo Bonzini {
3120dd285b06SPaolo Bonzini     s->tx_req = 0;
3121dd285b06SPaolo Bonzini     omap_mcbsp_tx_done(s);
3122bc72ad67SAlex Bligh     timer_del(s->sink_timer);
3123dd285b06SPaolo Bonzini }
3124dd285b06SPaolo Bonzini 
3125dd285b06SPaolo Bonzini static void omap_mcbsp_req_update(struct omap_mcbsp_s *s)
3126dd285b06SPaolo Bonzini {
3127dd285b06SPaolo Bonzini     int prev_rx_rate, prev_tx_rate;
3128dd285b06SPaolo Bonzini     int rx_rate = 0, tx_rate = 0;
3129dd285b06SPaolo Bonzini     int cpu_rate = 1500000;	/* XXX */
3130dd285b06SPaolo Bonzini 
3131dd285b06SPaolo Bonzini     /* TODO: check CLKSTP bit */
3132dd285b06SPaolo Bonzini     if (s->spcr[1] & (1 << 6)) {			/* GRST */
3133dd285b06SPaolo Bonzini         if (s->spcr[0] & (1 << 0)) {			/* RRST */
3134dd285b06SPaolo Bonzini             if ((s->srgr[1] & (1 << 13)) &&		/* CLKSM */
3135dd285b06SPaolo Bonzini                             (s->pcr & (1 << 8))) {	/* CLKRM */
3136dd285b06SPaolo Bonzini                 if (~s->pcr & (1 << 7))			/* SCLKME */
3137dd285b06SPaolo Bonzini                     rx_rate = cpu_rate /
3138dd285b06SPaolo Bonzini                             ((s->srgr[0] & 0xff) + 1);	/* CLKGDV */
3139dd285b06SPaolo Bonzini             } else
3140dd285b06SPaolo Bonzini                 if (s->codec)
3141dd285b06SPaolo Bonzini                     rx_rate = s->codec->rx_rate;
3142dd285b06SPaolo Bonzini         }
3143dd285b06SPaolo Bonzini 
3144dd285b06SPaolo Bonzini         if (s->spcr[1] & (1 << 0)) {			/* XRST */
3145dd285b06SPaolo Bonzini             if ((s->srgr[1] & (1 << 13)) &&		/* CLKSM */
3146dd285b06SPaolo Bonzini                             (s->pcr & (1 << 9))) {	/* CLKXM */
3147dd285b06SPaolo Bonzini                 if (~s->pcr & (1 << 7))			/* SCLKME */
3148dd285b06SPaolo Bonzini                     tx_rate = cpu_rate /
3149dd285b06SPaolo Bonzini                             ((s->srgr[0] & 0xff) + 1);	/* CLKGDV */
3150dd285b06SPaolo Bonzini             } else
3151dd285b06SPaolo Bonzini                 if (s->codec)
3152dd285b06SPaolo Bonzini                     tx_rate = s->codec->tx_rate;
3153dd285b06SPaolo Bonzini         }
3154dd285b06SPaolo Bonzini     }
3155dd285b06SPaolo Bonzini     prev_tx_rate = s->tx_rate;
3156dd285b06SPaolo Bonzini     prev_rx_rate = s->rx_rate;
3157dd285b06SPaolo Bonzini     s->tx_rate = tx_rate;
3158dd285b06SPaolo Bonzini     s->rx_rate = rx_rate;
3159dd285b06SPaolo Bonzini 
3160dd285b06SPaolo Bonzini     if (s->codec)
3161dd285b06SPaolo Bonzini         s->codec->set_rate(s->codec->opaque, rx_rate, tx_rate);
3162dd285b06SPaolo Bonzini 
3163dd285b06SPaolo Bonzini     if (!prev_tx_rate && tx_rate)
3164dd285b06SPaolo Bonzini         omap_mcbsp_tx_start(s);
3165dd285b06SPaolo Bonzini     else if (s->tx_rate && !tx_rate)
3166dd285b06SPaolo Bonzini         omap_mcbsp_tx_stop(s);
3167dd285b06SPaolo Bonzini 
3168dd285b06SPaolo Bonzini     if (!prev_rx_rate && rx_rate)
3169dd285b06SPaolo Bonzini         omap_mcbsp_rx_start(s);
3170dd285b06SPaolo Bonzini     else if (prev_tx_rate && !tx_rate)
3171dd285b06SPaolo Bonzini         omap_mcbsp_rx_stop(s);
3172dd285b06SPaolo Bonzini }
3173dd285b06SPaolo Bonzini 
3174dd285b06SPaolo Bonzini static uint64_t omap_mcbsp_read(void *opaque, hwaddr addr,
3175dd285b06SPaolo Bonzini                                 unsigned size)
3176dd285b06SPaolo Bonzini {
3177dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3178dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
3179dd285b06SPaolo Bonzini     uint16_t ret;
3180dd285b06SPaolo Bonzini 
3181dd285b06SPaolo Bonzini     if (size != 2) {
3182dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
3183dd285b06SPaolo Bonzini     }
3184dd285b06SPaolo Bonzini 
3185dd285b06SPaolo Bonzini     switch (offset) {
3186dd285b06SPaolo Bonzini     case 0x00:	/* DRR2 */
3187dd285b06SPaolo Bonzini         if (((s->rcr[0] >> 5) & 7) < 3)			/* RWDLEN1 */
3188dd285b06SPaolo Bonzini             return 0x0000;
3189dd285b06SPaolo Bonzini         /* Fall through.  */
3190dd285b06SPaolo Bonzini     case 0x02:	/* DRR1 */
3191dd285b06SPaolo Bonzini         if (s->rx_req < 2) {
3192a89f364aSAlistair Francis             printf("%s: Rx FIFO underrun\n", __func__);
3193dd285b06SPaolo Bonzini             omap_mcbsp_rx_done(s);
3194dd285b06SPaolo Bonzini         } else {
3195dd285b06SPaolo Bonzini             s->tx_req -= 2;
3196dd285b06SPaolo Bonzini             if (s->codec && s->codec->in.len >= 2) {
3197dd285b06SPaolo Bonzini                 ret = s->codec->in.fifo[s->codec->in.start ++] << 8;
3198dd285b06SPaolo Bonzini                 ret |= s->codec->in.fifo[s->codec->in.start ++];
3199dd285b06SPaolo Bonzini                 s->codec->in.len -= 2;
3200dd285b06SPaolo Bonzini             } else
3201dd285b06SPaolo Bonzini                 ret = 0x0000;
3202dd285b06SPaolo Bonzini             if (!s->tx_req)
3203dd285b06SPaolo Bonzini                 omap_mcbsp_rx_done(s);
3204dd285b06SPaolo Bonzini             return ret;
3205dd285b06SPaolo Bonzini         }
3206dd285b06SPaolo Bonzini         return 0x0000;
3207dd285b06SPaolo Bonzini 
3208dd285b06SPaolo Bonzini     case 0x04:	/* DXR2 */
3209dd285b06SPaolo Bonzini     case 0x06:	/* DXR1 */
3210dd285b06SPaolo Bonzini         return 0x0000;
3211dd285b06SPaolo Bonzini 
3212dd285b06SPaolo Bonzini     case 0x08:	/* SPCR2 */
3213dd285b06SPaolo Bonzini         return s->spcr[1];
3214dd285b06SPaolo Bonzini     case 0x0a:	/* SPCR1 */
3215dd285b06SPaolo Bonzini         return s->spcr[0];
3216dd285b06SPaolo Bonzini     case 0x0c:	/* RCR2 */
3217dd285b06SPaolo Bonzini         return s->rcr[1];
3218dd285b06SPaolo Bonzini     case 0x0e:	/* RCR1 */
3219dd285b06SPaolo Bonzini         return s->rcr[0];
3220dd285b06SPaolo Bonzini     case 0x10:	/* XCR2 */
3221dd285b06SPaolo Bonzini         return s->xcr[1];
3222dd285b06SPaolo Bonzini     case 0x12:	/* XCR1 */
3223dd285b06SPaolo Bonzini         return s->xcr[0];
3224dd285b06SPaolo Bonzini     case 0x14:	/* SRGR2 */
3225dd285b06SPaolo Bonzini         return s->srgr[1];
3226dd285b06SPaolo Bonzini     case 0x16:	/* SRGR1 */
3227dd285b06SPaolo Bonzini         return s->srgr[0];
3228dd285b06SPaolo Bonzini     case 0x18:	/* MCR2 */
3229dd285b06SPaolo Bonzini         return s->mcr[1];
3230dd285b06SPaolo Bonzini     case 0x1a:	/* MCR1 */
3231dd285b06SPaolo Bonzini         return s->mcr[0];
3232dd285b06SPaolo Bonzini     case 0x1c:	/* RCERA */
3233dd285b06SPaolo Bonzini         return s->rcer[0];
3234dd285b06SPaolo Bonzini     case 0x1e:	/* RCERB */
3235dd285b06SPaolo Bonzini         return s->rcer[1];
3236dd285b06SPaolo Bonzini     case 0x20:	/* XCERA */
3237dd285b06SPaolo Bonzini         return s->xcer[0];
3238dd285b06SPaolo Bonzini     case 0x22:	/* XCERB */
3239dd285b06SPaolo Bonzini         return s->xcer[1];
3240dd285b06SPaolo Bonzini     case 0x24:	/* PCR0 */
3241dd285b06SPaolo Bonzini         return s->pcr;
3242dd285b06SPaolo Bonzini     case 0x26:	/* RCERC */
3243dd285b06SPaolo Bonzini         return s->rcer[2];
3244dd285b06SPaolo Bonzini     case 0x28:	/* RCERD */
3245dd285b06SPaolo Bonzini         return s->rcer[3];
3246dd285b06SPaolo Bonzini     case 0x2a:	/* XCERC */
3247dd285b06SPaolo Bonzini         return s->xcer[2];
3248dd285b06SPaolo Bonzini     case 0x2c:	/* XCERD */
3249dd285b06SPaolo Bonzini         return s->xcer[3];
3250dd285b06SPaolo Bonzini     case 0x2e:	/* RCERE */
3251dd285b06SPaolo Bonzini         return s->rcer[4];
3252dd285b06SPaolo Bonzini     case 0x30:	/* RCERF */
3253dd285b06SPaolo Bonzini         return s->rcer[5];
3254dd285b06SPaolo Bonzini     case 0x32:	/* XCERE */
3255dd285b06SPaolo Bonzini         return s->xcer[4];
3256dd285b06SPaolo Bonzini     case 0x34:	/* XCERF */
3257dd285b06SPaolo Bonzini         return s->xcer[5];
3258dd285b06SPaolo Bonzini     case 0x36:	/* RCERG */
3259dd285b06SPaolo Bonzini         return s->rcer[6];
3260dd285b06SPaolo Bonzini     case 0x38:	/* RCERH */
3261dd285b06SPaolo Bonzini         return s->rcer[7];
3262dd285b06SPaolo Bonzini     case 0x3a:	/* XCERG */
3263dd285b06SPaolo Bonzini         return s->xcer[6];
3264dd285b06SPaolo Bonzini     case 0x3c:	/* XCERH */
3265dd285b06SPaolo Bonzini         return s->xcer[7];
3266dd285b06SPaolo Bonzini     }
3267dd285b06SPaolo Bonzini 
3268dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
3269dd285b06SPaolo Bonzini     return 0;
3270dd285b06SPaolo Bonzini }
3271dd285b06SPaolo Bonzini 
3272dd285b06SPaolo Bonzini static void omap_mcbsp_writeh(void *opaque, hwaddr addr,
3273dd285b06SPaolo Bonzini                 uint32_t value)
3274dd285b06SPaolo Bonzini {
3275dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3276dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
3277dd285b06SPaolo Bonzini 
3278dd285b06SPaolo Bonzini     switch (offset) {
3279dd285b06SPaolo Bonzini     case 0x00:	/* DRR2 */
3280dd285b06SPaolo Bonzini     case 0x02:	/* DRR1 */
3281dd285b06SPaolo Bonzini         OMAP_RO_REG(addr);
3282dd285b06SPaolo Bonzini         return;
3283dd285b06SPaolo Bonzini 
3284dd285b06SPaolo Bonzini     case 0x04:	/* DXR2 */
3285dd285b06SPaolo Bonzini         if (((s->xcr[0] >> 5) & 7) < 3)			/* XWDLEN1 */
3286dd285b06SPaolo Bonzini             return;
3287dd285b06SPaolo Bonzini         /* Fall through.  */
3288dd285b06SPaolo Bonzini     case 0x06:	/* DXR1 */
3289dd285b06SPaolo Bonzini         if (s->tx_req > 1) {
3290dd285b06SPaolo Bonzini             s->tx_req -= 2;
3291dd285b06SPaolo Bonzini             if (s->codec && s->codec->cts) {
3292dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] = (value >> 8) & 0xff;
3293dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] = (value >> 0) & 0xff;
3294dd285b06SPaolo Bonzini             }
3295dd285b06SPaolo Bonzini             if (s->tx_req < 2)
3296dd285b06SPaolo Bonzini                 omap_mcbsp_tx_done(s);
3297dd285b06SPaolo Bonzini         } else
3298a89f364aSAlistair Francis             printf("%s: Tx FIFO overrun\n", __func__);
3299dd285b06SPaolo Bonzini         return;
3300dd285b06SPaolo Bonzini 
3301dd285b06SPaolo Bonzini     case 0x08:	/* SPCR2 */
3302dd285b06SPaolo Bonzini         s->spcr[1] &= 0x0002;
3303dd285b06SPaolo Bonzini         s->spcr[1] |= 0x03f9 & value;
3304dd285b06SPaolo Bonzini         s->spcr[1] |= 0x0004 & (value << 2);		/* XEMPTY := XRST */
3305dd285b06SPaolo Bonzini         if (~value & 1)					/* XRST */
3306dd285b06SPaolo Bonzini             s->spcr[1] &= ~6;
3307dd285b06SPaolo Bonzini         omap_mcbsp_req_update(s);
3308dd285b06SPaolo Bonzini         return;
3309dd285b06SPaolo Bonzini     case 0x0a:	/* SPCR1 */
3310dd285b06SPaolo Bonzini         s->spcr[0] &= 0x0006;
3311dd285b06SPaolo Bonzini         s->spcr[0] |= 0xf8f9 & value;
3312dd285b06SPaolo Bonzini         if (value & (1 << 15))				/* DLB */
3313a89f364aSAlistair Francis             printf("%s: Digital Loopback mode enable attempt\n", __func__);
3314dd285b06SPaolo Bonzini         if (~value & 1) {				/* RRST */
3315dd285b06SPaolo Bonzini             s->spcr[0] &= ~6;
3316dd285b06SPaolo Bonzini             s->rx_req = 0;
3317dd285b06SPaolo Bonzini             omap_mcbsp_rx_done(s);
3318dd285b06SPaolo Bonzini         }
3319dd285b06SPaolo Bonzini         omap_mcbsp_req_update(s);
3320dd285b06SPaolo Bonzini         return;
3321dd285b06SPaolo Bonzini 
3322dd285b06SPaolo Bonzini     case 0x0c:	/* RCR2 */
3323dd285b06SPaolo Bonzini         s->rcr[1] = value & 0xffff;
3324dd285b06SPaolo Bonzini         return;
3325dd285b06SPaolo Bonzini     case 0x0e:	/* RCR1 */
3326dd285b06SPaolo Bonzini         s->rcr[0] = value & 0x7fe0;
3327dd285b06SPaolo Bonzini         return;
3328dd285b06SPaolo Bonzini     case 0x10:	/* XCR2 */
3329dd285b06SPaolo Bonzini         s->xcr[1] = value & 0xffff;
3330dd285b06SPaolo Bonzini         return;
3331dd285b06SPaolo Bonzini     case 0x12:	/* XCR1 */
3332dd285b06SPaolo Bonzini         s->xcr[0] = value & 0x7fe0;
3333dd285b06SPaolo Bonzini         return;
3334dd285b06SPaolo Bonzini     case 0x14:	/* SRGR2 */
3335dd285b06SPaolo Bonzini         s->srgr[1] = value & 0xffff;
3336dd285b06SPaolo Bonzini         omap_mcbsp_req_update(s);
3337dd285b06SPaolo Bonzini         return;
3338dd285b06SPaolo Bonzini     case 0x16:	/* SRGR1 */
3339dd285b06SPaolo Bonzini         s->srgr[0] = value & 0xffff;
3340dd285b06SPaolo Bonzini         omap_mcbsp_req_update(s);
3341dd285b06SPaolo Bonzini         return;
3342dd285b06SPaolo Bonzini     case 0x18:	/* MCR2 */
3343dd285b06SPaolo Bonzini         s->mcr[1] = value & 0x03e3;
3344dd285b06SPaolo Bonzini         if (value & 3)					/* XMCM */
3345c94a60cbSAlistair Francis             printf("%s: Tx channel selection mode enable attempt\n", __func__);
3346dd285b06SPaolo Bonzini         return;
3347dd285b06SPaolo Bonzini     case 0x1a:	/* MCR1 */
3348dd285b06SPaolo Bonzini         s->mcr[0] = value & 0x03e1;
3349dd285b06SPaolo Bonzini         if (value & 1)					/* RMCM */
3350c94a60cbSAlistair Francis             printf("%s: Rx channel selection mode enable attempt\n", __func__);
3351dd285b06SPaolo Bonzini         return;
3352dd285b06SPaolo Bonzini     case 0x1c:	/* RCERA */
3353dd285b06SPaolo Bonzini         s->rcer[0] = value & 0xffff;
3354dd285b06SPaolo Bonzini         return;
3355dd285b06SPaolo Bonzini     case 0x1e:	/* RCERB */
3356dd285b06SPaolo Bonzini         s->rcer[1] = value & 0xffff;
3357dd285b06SPaolo Bonzini         return;
3358dd285b06SPaolo Bonzini     case 0x20:	/* XCERA */
3359dd285b06SPaolo Bonzini         s->xcer[0] = value & 0xffff;
3360dd285b06SPaolo Bonzini         return;
3361dd285b06SPaolo Bonzini     case 0x22:	/* XCERB */
3362dd285b06SPaolo Bonzini         s->xcer[1] = value & 0xffff;
3363dd285b06SPaolo Bonzini         return;
3364dd285b06SPaolo Bonzini     case 0x24:	/* PCR0 */
3365dd285b06SPaolo Bonzini         s->pcr = value & 0x7faf;
3366dd285b06SPaolo Bonzini         return;
3367dd285b06SPaolo Bonzini     case 0x26:	/* RCERC */
3368dd285b06SPaolo Bonzini         s->rcer[2] = value & 0xffff;
3369dd285b06SPaolo Bonzini         return;
3370dd285b06SPaolo Bonzini     case 0x28:	/* RCERD */
3371dd285b06SPaolo Bonzini         s->rcer[3] = value & 0xffff;
3372dd285b06SPaolo Bonzini         return;
3373dd285b06SPaolo Bonzini     case 0x2a:	/* XCERC */
3374dd285b06SPaolo Bonzini         s->xcer[2] = value & 0xffff;
3375dd285b06SPaolo Bonzini         return;
3376dd285b06SPaolo Bonzini     case 0x2c:	/* XCERD */
3377dd285b06SPaolo Bonzini         s->xcer[3] = value & 0xffff;
3378dd285b06SPaolo Bonzini         return;
3379dd285b06SPaolo Bonzini     case 0x2e:	/* RCERE */
3380dd285b06SPaolo Bonzini         s->rcer[4] = value & 0xffff;
3381dd285b06SPaolo Bonzini         return;
3382dd285b06SPaolo Bonzini     case 0x30:	/* RCERF */
3383dd285b06SPaolo Bonzini         s->rcer[5] = value & 0xffff;
3384dd285b06SPaolo Bonzini         return;
3385dd285b06SPaolo Bonzini     case 0x32:	/* XCERE */
3386dd285b06SPaolo Bonzini         s->xcer[4] = value & 0xffff;
3387dd285b06SPaolo Bonzini         return;
3388dd285b06SPaolo Bonzini     case 0x34:	/* XCERF */
3389dd285b06SPaolo Bonzini         s->xcer[5] = value & 0xffff;
3390dd285b06SPaolo Bonzini         return;
3391dd285b06SPaolo Bonzini     case 0x36:	/* RCERG */
3392dd285b06SPaolo Bonzini         s->rcer[6] = value & 0xffff;
3393dd285b06SPaolo Bonzini         return;
3394dd285b06SPaolo Bonzini     case 0x38:	/* RCERH */
3395dd285b06SPaolo Bonzini         s->rcer[7] = value & 0xffff;
3396dd285b06SPaolo Bonzini         return;
3397dd285b06SPaolo Bonzini     case 0x3a:	/* XCERG */
3398dd285b06SPaolo Bonzini         s->xcer[6] = value & 0xffff;
3399dd285b06SPaolo Bonzini         return;
3400dd285b06SPaolo Bonzini     case 0x3c:	/* XCERH */
3401dd285b06SPaolo Bonzini         s->xcer[7] = value & 0xffff;
3402dd285b06SPaolo Bonzini         return;
3403dd285b06SPaolo Bonzini     }
3404dd285b06SPaolo Bonzini 
3405dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
3406dd285b06SPaolo Bonzini }
3407dd285b06SPaolo Bonzini 
3408dd285b06SPaolo Bonzini static void omap_mcbsp_writew(void *opaque, hwaddr addr,
3409dd285b06SPaolo Bonzini                 uint32_t value)
3410dd285b06SPaolo Bonzini {
3411dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3412dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
3413dd285b06SPaolo Bonzini 
3414dd285b06SPaolo Bonzini     if (offset == 0x04) {				/* DXR */
3415dd285b06SPaolo Bonzini         if (((s->xcr[0] >> 5) & 7) < 3)			/* XWDLEN1 */
3416dd285b06SPaolo Bonzini             return;
3417dd285b06SPaolo Bonzini         if (s->tx_req > 3) {
3418dd285b06SPaolo Bonzini             s->tx_req -= 4;
3419dd285b06SPaolo Bonzini             if (s->codec && s->codec->cts) {
3420dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] =
3421dd285b06SPaolo Bonzini                         (value >> 24) & 0xff;
3422dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] =
3423dd285b06SPaolo Bonzini                         (value >> 16) & 0xff;
3424dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] =
3425dd285b06SPaolo Bonzini                         (value >> 8) & 0xff;
3426dd285b06SPaolo Bonzini                 s->codec->out.fifo[s->codec->out.len ++] =
3427dd285b06SPaolo Bonzini                         (value >> 0) & 0xff;
3428dd285b06SPaolo Bonzini             }
3429dd285b06SPaolo Bonzini             if (s->tx_req < 4)
3430dd285b06SPaolo Bonzini                 omap_mcbsp_tx_done(s);
3431dd285b06SPaolo Bonzini         } else
3432a89f364aSAlistair Francis             printf("%s: Tx FIFO overrun\n", __func__);
3433dd285b06SPaolo Bonzini         return;
3434dd285b06SPaolo Bonzini     }
3435dd285b06SPaolo Bonzini 
3436dd285b06SPaolo Bonzini     omap_badwidth_write16(opaque, addr, value);
3437dd285b06SPaolo Bonzini }
3438dd285b06SPaolo Bonzini 
3439dd285b06SPaolo Bonzini static void omap_mcbsp_write(void *opaque, hwaddr addr,
3440dd285b06SPaolo Bonzini                              uint64_t value, unsigned size)
3441dd285b06SPaolo Bonzini {
3442dd285b06SPaolo Bonzini     switch (size) {
344377a8257eSStefan Weil     case 2:
344477a8257eSStefan Weil         omap_mcbsp_writeh(opaque, addr, value);
344577a8257eSStefan Weil         break;
344677a8257eSStefan Weil     case 4:
344777a8257eSStefan Weil         omap_mcbsp_writew(opaque, addr, value);
344877a8257eSStefan Weil         break;
344977a8257eSStefan Weil     default:
345077a8257eSStefan Weil         omap_badwidth_write16(opaque, addr, value);
3451dd285b06SPaolo Bonzini     }
3452dd285b06SPaolo Bonzini }
3453dd285b06SPaolo Bonzini 
3454dd285b06SPaolo Bonzini static const MemoryRegionOps omap_mcbsp_ops = {
3455dd285b06SPaolo Bonzini     .read = omap_mcbsp_read,
3456dd285b06SPaolo Bonzini     .write = omap_mcbsp_write,
3457dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
3458dd285b06SPaolo Bonzini };
3459dd285b06SPaolo Bonzini 
3460dd285b06SPaolo Bonzini static void omap_mcbsp_reset(struct omap_mcbsp_s *s)
3461dd285b06SPaolo Bonzini {
3462dd285b06SPaolo Bonzini     memset(&s->spcr, 0, sizeof(s->spcr));
3463dd285b06SPaolo Bonzini     memset(&s->rcr, 0, sizeof(s->rcr));
3464dd285b06SPaolo Bonzini     memset(&s->xcr, 0, sizeof(s->xcr));
3465dd285b06SPaolo Bonzini     s->srgr[0] = 0x0001;
3466dd285b06SPaolo Bonzini     s->srgr[1] = 0x2000;
3467dd285b06SPaolo Bonzini     memset(&s->mcr, 0, sizeof(s->mcr));
3468dd285b06SPaolo Bonzini     memset(&s->pcr, 0, sizeof(s->pcr));
3469dd285b06SPaolo Bonzini     memset(&s->rcer, 0, sizeof(s->rcer));
3470dd285b06SPaolo Bonzini     memset(&s->xcer, 0, sizeof(s->xcer));
3471dd285b06SPaolo Bonzini     s->tx_req = 0;
3472dd285b06SPaolo Bonzini     s->rx_req = 0;
3473dd285b06SPaolo Bonzini     s->tx_rate = 0;
3474dd285b06SPaolo Bonzini     s->rx_rate = 0;
3475bc72ad67SAlex Bligh     timer_del(s->source_timer);
3476bc72ad67SAlex Bligh     timer_del(s->sink_timer);
3477dd285b06SPaolo Bonzini }
3478dd285b06SPaolo Bonzini 
3479dd285b06SPaolo Bonzini static struct omap_mcbsp_s *omap_mcbsp_init(MemoryRegion *system_memory,
3480dd285b06SPaolo Bonzini                                             hwaddr base,
3481dd285b06SPaolo Bonzini                                             qemu_irq txirq, qemu_irq rxirq,
3482dd285b06SPaolo Bonzini                                             qemu_irq *dma, omap_clk clk)
3483dd285b06SPaolo Bonzini {
3484b45c03f5SMarkus Armbruster     struct omap_mcbsp_s *s = g_new0(struct omap_mcbsp_s, 1);
3485dd285b06SPaolo Bonzini 
3486dd285b06SPaolo Bonzini     s->txirq = txirq;
3487dd285b06SPaolo Bonzini     s->rxirq = rxirq;
3488dd285b06SPaolo Bonzini     s->txdrq = dma[0];
3489dd285b06SPaolo Bonzini     s->rxdrq = dma[1];
3490bc72ad67SAlex Bligh     s->sink_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_mcbsp_sink_tick, s);
3491bc72ad67SAlex Bligh     s->source_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, omap_mcbsp_source_tick, s);
3492dd285b06SPaolo Bonzini     omap_mcbsp_reset(s);
3493dd285b06SPaolo Bonzini 
34942c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_mcbsp_ops, s, "omap-mcbsp", 0x800);
3495dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
3496dd285b06SPaolo Bonzini 
3497dd285b06SPaolo Bonzini     return s;
3498dd285b06SPaolo Bonzini }
3499dd285b06SPaolo Bonzini 
3500dd285b06SPaolo Bonzini static void omap_mcbsp_i2s_swallow(void *opaque, int line, int level)
3501dd285b06SPaolo Bonzini {
3502dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3503dd285b06SPaolo Bonzini 
3504dd285b06SPaolo Bonzini     if (s->rx_rate) {
3505dd285b06SPaolo Bonzini         s->rx_req = s->codec->in.len;
3506dd285b06SPaolo Bonzini         omap_mcbsp_rx_newdata(s);
3507dd285b06SPaolo Bonzini     }
3508dd285b06SPaolo Bonzini }
3509dd285b06SPaolo Bonzini 
3510dd285b06SPaolo Bonzini static void omap_mcbsp_i2s_start(void *opaque, int line, int level)
3511dd285b06SPaolo Bonzini {
3512dd285b06SPaolo Bonzini     struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3513dd285b06SPaolo Bonzini 
3514dd285b06SPaolo Bonzini     if (s->tx_rate) {
3515dd285b06SPaolo Bonzini         s->tx_req = s->codec->out.size;
3516dd285b06SPaolo Bonzini         omap_mcbsp_tx_newdata(s);
3517dd285b06SPaolo Bonzini     }
3518dd285b06SPaolo Bonzini }
3519dd285b06SPaolo Bonzini 
3520dd285b06SPaolo Bonzini void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, I2SCodec *slave)
3521dd285b06SPaolo Bonzini {
3522dd285b06SPaolo Bonzini     s->codec = slave;
3523f3c7d038SAndreas Färber     slave->rx_swallow = qemu_allocate_irq(omap_mcbsp_i2s_swallow, s, 0);
3524f3c7d038SAndreas Färber     slave->tx_start = qemu_allocate_irq(omap_mcbsp_i2s_start, s, 0);
3525dd285b06SPaolo Bonzini }
3526dd285b06SPaolo Bonzini 
3527dd285b06SPaolo Bonzini /* LED Pulse Generators */
3528dd285b06SPaolo Bonzini struct omap_lpg_s {
3529dd285b06SPaolo Bonzini     MemoryRegion iomem;
3530dd285b06SPaolo Bonzini     QEMUTimer *tm;
3531dd285b06SPaolo Bonzini 
3532dd285b06SPaolo Bonzini     uint8_t control;
3533dd285b06SPaolo Bonzini     uint8_t power;
3534dd285b06SPaolo Bonzini     int64_t on;
3535dd285b06SPaolo Bonzini     int64_t period;
3536dd285b06SPaolo Bonzini     int clk;
3537dd285b06SPaolo Bonzini     int cycle;
3538dd285b06SPaolo Bonzini };
3539dd285b06SPaolo Bonzini 
3540dd285b06SPaolo Bonzini static void omap_lpg_tick(void *opaque)
3541dd285b06SPaolo Bonzini {
3542dd285b06SPaolo Bonzini     struct omap_lpg_s *s = opaque;
3543dd285b06SPaolo Bonzini 
3544dd285b06SPaolo Bonzini     if (s->cycle)
3545bc72ad67SAlex Bligh         timer_mod(s->tm, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + s->period - s->on);
3546dd285b06SPaolo Bonzini     else
3547bc72ad67SAlex Bligh         timer_mod(s->tm, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + s->on);
3548dd285b06SPaolo Bonzini 
3549dd285b06SPaolo Bonzini     s->cycle = !s->cycle;
3550a89f364aSAlistair Francis     printf("%s: LED is %s\n", __func__, s->cycle ? "on" : "off");
3551dd285b06SPaolo Bonzini }
3552dd285b06SPaolo Bonzini 
3553dd285b06SPaolo Bonzini static void omap_lpg_update(struct omap_lpg_s *s)
3554dd285b06SPaolo Bonzini {
3555dd285b06SPaolo Bonzini     int64_t on, period = 1, ticks = 1000;
3556dd285b06SPaolo Bonzini     static const int per[8] = { 1, 2, 4, 8, 12, 16, 20, 24 };
3557dd285b06SPaolo Bonzini 
3558dd285b06SPaolo Bonzini     if (~s->control & (1 << 6))					/* LPGRES */
3559dd285b06SPaolo Bonzini         on = 0;
3560dd285b06SPaolo Bonzini     else if (s->control & (1 << 7))				/* PERM_ON */
3561dd285b06SPaolo Bonzini         on = period;
3562dd285b06SPaolo Bonzini     else {
3563dd285b06SPaolo Bonzini         period = muldiv64(ticks, per[s->control & 7],		/* PERCTRL */
3564dd285b06SPaolo Bonzini                         256 / 32);
3565dd285b06SPaolo Bonzini         on = (s->clk && s->power) ? muldiv64(ticks,
3566dd285b06SPaolo Bonzini                         per[(s->control >> 3) & 7], 256) : 0;	/* ONCTRL */
3567dd285b06SPaolo Bonzini     }
3568dd285b06SPaolo Bonzini 
3569bc72ad67SAlex Bligh     timer_del(s->tm);
3570dd285b06SPaolo Bonzini     if (on == period && s->on < s->period)
3571a89f364aSAlistair Francis         printf("%s: LED is on\n", __func__);
3572dd285b06SPaolo Bonzini     else if (on == 0 && s->on)
3573a89f364aSAlistair Francis         printf("%s: LED is off\n", __func__);
3574dd285b06SPaolo Bonzini     else if (on && (on != s->on || period != s->period)) {
3575dd285b06SPaolo Bonzini         s->cycle = 0;
3576dd285b06SPaolo Bonzini         s->on = on;
3577dd285b06SPaolo Bonzini         s->period = period;
3578dd285b06SPaolo Bonzini         omap_lpg_tick(s);
3579dd285b06SPaolo Bonzini         return;
3580dd285b06SPaolo Bonzini     }
3581dd285b06SPaolo Bonzini 
3582dd285b06SPaolo Bonzini     s->on = on;
3583dd285b06SPaolo Bonzini     s->period = period;
3584dd285b06SPaolo Bonzini }
3585dd285b06SPaolo Bonzini 
3586dd285b06SPaolo Bonzini static void omap_lpg_reset(struct omap_lpg_s *s)
3587dd285b06SPaolo Bonzini {
3588dd285b06SPaolo Bonzini     s->control = 0x00;
3589dd285b06SPaolo Bonzini     s->power = 0x00;
3590dd285b06SPaolo Bonzini     s->clk = 1;
3591dd285b06SPaolo Bonzini     omap_lpg_update(s);
3592dd285b06SPaolo Bonzini }
3593dd285b06SPaolo Bonzini 
3594dd285b06SPaolo Bonzini static uint64_t omap_lpg_read(void *opaque, hwaddr addr,
3595dd285b06SPaolo Bonzini                               unsigned size)
3596dd285b06SPaolo Bonzini {
3597dd285b06SPaolo Bonzini     struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3598dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
3599dd285b06SPaolo Bonzini 
3600dd285b06SPaolo Bonzini     if (size != 1) {
3601dd285b06SPaolo Bonzini         return omap_badwidth_read8(opaque, addr);
3602dd285b06SPaolo Bonzini     }
3603dd285b06SPaolo Bonzini 
3604dd285b06SPaolo Bonzini     switch (offset) {
3605dd285b06SPaolo Bonzini     case 0x00:	/* LCR */
3606dd285b06SPaolo Bonzini         return s->control;
3607dd285b06SPaolo Bonzini 
3608dd285b06SPaolo Bonzini     case 0x04:	/* PMR */
3609dd285b06SPaolo Bonzini         return s->power;
3610dd285b06SPaolo Bonzini     }
3611dd285b06SPaolo Bonzini 
3612dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
3613dd285b06SPaolo Bonzini     return 0;
3614dd285b06SPaolo Bonzini }
3615dd285b06SPaolo Bonzini 
3616dd285b06SPaolo Bonzini static void omap_lpg_write(void *opaque, hwaddr addr,
3617dd285b06SPaolo Bonzini                            uint64_t value, unsigned size)
3618dd285b06SPaolo Bonzini {
3619dd285b06SPaolo Bonzini     struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3620dd285b06SPaolo Bonzini     int offset = addr & OMAP_MPUI_REG_MASK;
3621dd285b06SPaolo Bonzini 
3622dd285b06SPaolo Bonzini     if (size != 1) {
362377a8257eSStefan Weil         omap_badwidth_write8(opaque, addr, value);
362477a8257eSStefan Weil         return;
3625dd285b06SPaolo Bonzini     }
3626dd285b06SPaolo Bonzini 
3627dd285b06SPaolo Bonzini     switch (offset) {
3628dd285b06SPaolo Bonzini     case 0x00:	/* LCR */
3629dd285b06SPaolo Bonzini         if (~value & (1 << 6))					/* LPGRES */
3630dd285b06SPaolo Bonzini             omap_lpg_reset(s);
3631dd285b06SPaolo Bonzini         s->control = value & 0xff;
3632dd285b06SPaolo Bonzini         omap_lpg_update(s);
3633dd285b06SPaolo Bonzini         return;
3634dd285b06SPaolo Bonzini 
3635dd285b06SPaolo Bonzini     case 0x04:	/* PMR */
3636dd285b06SPaolo Bonzini         s->power = value & 0x01;
3637dd285b06SPaolo Bonzini         omap_lpg_update(s);
3638dd285b06SPaolo Bonzini         return;
3639dd285b06SPaolo Bonzini 
3640dd285b06SPaolo Bonzini     default:
3641dd285b06SPaolo Bonzini         OMAP_BAD_REG(addr);
3642dd285b06SPaolo Bonzini         return;
3643dd285b06SPaolo Bonzini     }
3644dd285b06SPaolo Bonzini }
3645dd285b06SPaolo Bonzini 
3646dd285b06SPaolo Bonzini static const MemoryRegionOps omap_lpg_ops = {
3647dd285b06SPaolo Bonzini     .read = omap_lpg_read,
3648dd285b06SPaolo Bonzini     .write = omap_lpg_write,
3649dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
3650dd285b06SPaolo Bonzini };
3651dd285b06SPaolo Bonzini 
3652dd285b06SPaolo Bonzini static void omap_lpg_clk_update(void *opaque, int line, int on)
3653dd285b06SPaolo Bonzini {
3654dd285b06SPaolo Bonzini     struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3655dd285b06SPaolo Bonzini 
3656dd285b06SPaolo Bonzini     s->clk = on;
3657dd285b06SPaolo Bonzini     omap_lpg_update(s);
3658dd285b06SPaolo Bonzini }
3659dd285b06SPaolo Bonzini 
3660dd285b06SPaolo Bonzini static struct omap_lpg_s *omap_lpg_init(MemoryRegion *system_memory,
3661dd285b06SPaolo Bonzini                                         hwaddr base, omap_clk clk)
3662dd285b06SPaolo Bonzini {
3663b45c03f5SMarkus Armbruster     struct omap_lpg_s *s = g_new0(struct omap_lpg_s, 1);
3664dd285b06SPaolo Bonzini 
3665bc72ad67SAlex Bligh     s->tm = timer_new_ms(QEMU_CLOCK_VIRTUAL, omap_lpg_tick, s);
3666dd285b06SPaolo Bonzini 
3667dd285b06SPaolo Bonzini     omap_lpg_reset(s);
3668dd285b06SPaolo Bonzini 
36692c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &omap_lpg_ops, s, "omap-lpg", 0x800);
3670dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, base, &s->iomem);
3671dd285b06SPaolo Bonzini 
3672f3c7d038SAndreas Färber     omap_clk_adduser(clk, qemu_allocate_irq(omap_lpg_clk_update, s, 0));
3673dd285b06SPaolo Bonzini 
3674dd285b06SPaolo Bonzini     return s;
3675dd285b06SPaolo Bonzini }
3676dd285b06SPaolo Bonzini 
3677dd285b06SPaolo Bonzini /* MPUI Peripheral Bridge configuration */
3678dd285b06SPaolo Bonzini static uint64_t omap_mpui_io_read(void *opaque, hwaddr addr,
3679dd285b06SPaolo Bonzini                                   unsigned size)
3680dd285b06SPaolo Bonzini {
3681dd285b06SPaolo Bonzini     if (size != 2) {
3682dd285b06SPaolo Bonzini         return omap_badwidth_read16(opaque, addr);
3683dd285b06SPaolo Bonzini     }
3684dd285b06SPaolo Bonzini 
3685dd285b06SPaolo Bonzini     if (addr == OMAP_MPUI_BASE)	/* CMR */
3686dd285b06SPaolo Bonzini         return 0xfe4d;
3687dd285b06SPaolo Bonzini 
3688dd285b06SPaolo Bonzini     OMAP_BAD_REG(addr);
3689dd285b06SPaolo Bonzini     return 0;
3690dd285b06SPaolo Bonzini }
3691dd285b06SPaolo Bonzini 
3692dd285b06SPaolo Bonzini static void omap_mpui_io_write(void *opaque, hwaddr addr,
3693dd285b06SPaolo Bonzini                                uint64_t value, unsigned size)
3694dd285b06SPaolo Bonzini {
3695dd285b06SPaolo Bonzini     /* FIXME: infinite loop */
3696dd285b06SPaolo Bonzini     omap_badwidth_write16(opaque, addr, value);
3697dd285b06SPaolo Bonzini }
3698dd285b06SPaolo Bonzini 
3699dd285b06SPaolo Bonzini static const MemoryRegionOps omap_mpui_io_ops = {
3700dd285b06SPaolo Bonzini     .read = omap_mpui_io_read,
3701dd285b06SPaolo Bonzini     .write = omap_mpui_io_write,
3702dd285b06SPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
3703dd285b06SPaolo Bonzini };
3704dd285b06SPaolo Bonzini 
3705dd285b06SPaolo Bonzini static void omap_setup_mpui_io(MemoryRegion *system_memory,
3706dd285b06SPaolo Bonzini                                struct omap_mpu_state_s *mpu)
3707dd285b06SPaolo Bonzini {
37082c9b15caSPaolo Bonzini     memory_region_init_io(&mpu->mpui_io_iomem, NULL, &omap_mpui_io_ops, mpu,
3709dd285b06SPaolo Bonzini                           "omap-mpui-io", 0x7fff);
3710dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, OMAP_MPUI_BASE,
3711dd285b06SPaolo Bonzini                                 &mpu->mpui_io_iomem);
3712dd285b06SPaolo Bonzini }
3713dd285b06SPaolo Bonzini 
3714dd285b06SPaolo Bonzini /* General chip reset */
3715dd285b06SPaolo Bonzini static void omap1_mpu_reset(void *opaque)
3716dd285b06SPaolo Bonzini {
3717dd285b06SPaolo Bonzini     struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
3718dd285b06SPaolo Bonzini 
3719dd285b06SPaolo Bonzini     omap_dma_reset(mpu->dma);
3720dd285b06SPaolo Bonzini     omap_mpu_timer_reset(mpu->timer[0]);
3721dd285b06SPaolo Bonzini     omap_mpu_timer_reset(mpu->timer[1]);
3722dd285b06SPaolo Bonzini     omap_mpu_timer_reset(mpu->timer[2]);
3723dd285b06SPaolo Bonzini     omap_wd_timer_reset(mpu->wdt);
3724dd285b06SPaolo Bonzini     omap_os_timer_reset(mpu->os_timer);
3725dd285b06SPaolo Bonzini     omap_lcdc_reset(mpu->lcd);
3726dd285b06SPaolo Bonzini     omap_ulpd_pm_reset(mpu);
3727dd285b06SPaolo Bonzini     omap_pin_cfg_reset(mpu);
3728dd285b06SPaolo Bonzini     omap_mpui_reset(mpu);
3729dd285b06SPaolo Bonzini     omap_tipb_bridge_reset(mpu->private_tipb);
3730dd285b06SPaolo Bonzini     omap_tipb_bridge_reset(mpu->public_tipb);
3731dd285b06SPaolo Bonzini     omap_dpll_reset(mpu->dpll[0]);
3732dd285b06SPaolo Bonzini     omap_dpll_reset(mpu->dpll[1]);
3733dd285b06SPaolo Bonzini     omap_dpll_reset(mpu->dpll[2]);
3734dd285b06SPaolo Bonzini     omap_uart_reset(mpu->uart[0]);
3735dd285b06SPaolo Bonzini     omap_uart_reset(mpu->uart[1]);
3736dd285b06SPaolo Bonzini     omap_uart_reset(mpu->uart[2]);
3737dd285b06SPaolo Bonzini     omap_mmc_reset(mpu->mmc);
3738dd285b06SPaolo Bonzini     omap_mpuio_reset(mpu->mpuio);
3739dd285b06SPaolo Bonzini     omap_uwire_reset(mpu->microwire);
3740dd285b06SPaolo Bonzini     omap_pwl_reset(mpu->pwl);
3741dd285b06SPaolo Bonzini     omap_pwt_reset(mpu->pwt);
3742dd285b06SPaolo Bonzini     omap_rtc_reset(mpu->rtc);
3743dd285b06SPaolo Bonzini     omap_mcbsp_reset(mpu->mcbsp1);
3744dd285b06SPaolo Bonzini     omap_mcbsp_reset(mpu->mcbsp2);
3745dd285b06SPaolo Bonzini     omap_mcbsp_reset(mpu->mcbsp3);
3746dd285b06SPaolo Bonzini     omap_lpg_reset(mpu->led[0]);
3747dd285b06SPaolo Bonzini     omap_lpg_reset(mpu->led[1]);
3748dd285b06SPaolo Bonzini     omap_clkm_reset(mpu);
3749dd285b06SPaolo Bonzini     cpu_reset(CPU(mpu->cpu));
3750dd285b06SPaolo Bonzini }
3751dd285b06SPaolo Bonzini 
3752dd285b06SPaolo Bonzini static const struct omap_map_s {
3753dd285b06SPaolo Bonzini     hwaddr phys_dsp;
3754dd285b06SPaolo Bonzini     hwaddr phys_mpu;
3755dd285b06SPaolo Bonzini     uint32_t size;
3756dd285b06SPaolo Bonzini     const char *name;
3757dd285b06SPaolo Bonzini } omap15xx_dsp_mm[] = {
3758dd285b06SPaolo Bonzini     /* Strobe 0 */
3759dd285b06SPaolo Bonzini     { 0xe1010000, 0xfffb0000, 0x800, "UART1 BT" },		/* CS0 */
3760dd285b06SPaolo Bonzini     { 0xe1010800, 0xfffb0800, 0x800, "UART2 COM" },		/* CS1 */
3761dd285b06SPaolo Bonzini     { 0xe1011800, 0xfffb1800, 0x800, "McBSP1 audio" },		/* CS3 */
3762dd285b06SPaolo Bonzini     { 0xe1012000, 0xfffb2000, 0x800, "MCSI2 communication" },	/* CS4 */
3763dd285b06SPaolo Bonzini     { 0xe1012800, 0xfffb2800, 0x800, "MCSI1 BT u-Law" },	/* CS5 */
3764dd285b06SPaolo Bonzini     { 0xe1013000, 0xfffb3000, 0x800, "uWire" },			/* CS6 */
3765dd285b06SPaolo Bonzini     { 0xe1013800, 0xfffb3800, 0x800, "I^2C" },			/* CS7 */
3766dd285b06SPaolo Bonzini     { 0xe1014000, 0xfffb4000, 0x800, "USB W2FC" },		/* CS8 */
3767dd285b06SPaolo Bonzini     { 0xe1014800, 0xfffb4800, 0x800, "RTC" },			/* CS9 */
3768dd285b06SPaolo Bonzini     { 0xe1015000, 0xfffb5000, 0x800, "MPUIO" },			/* CS10 */
3769dd285b06SPaolo Bonzini     { 0xe1015800, 0xfffb5800, 0x800, "PWL" },			/* CS11 */
3770dd285b06SPaolo Bonzini     { 0xe1016000, 0xfffb6000, 0x800, "PWT" },			/* CS12 */
3771dd285b06SPaolo Bonzini     { 0xe1017000, 0xfffb7000, 0x800, "McBSP3" },		/* CS14 */
3772dd285b06SPaolo Bonzini     { 0xe1017800, 0xfffb7800, 0x800, "MMC" },			/* CS15 */
3773dd285b06SPaolo Bonzini     { 0xe1019000, 0xfffb9000, 0x800, "32-kHz timer" },		/* CS18 */
3774dd285b06SPaolo Bonzini     { 0xe1019800, 0xfffb9800, 0x800, "UART3" },			/* CS19 */
3775dd285b06SPaolo Bonzini     { 0xe101c800, 0xfffbc800, 0x800, "TIPB switches" },		/* CS25 */
3776dd285b06SPaolo Bonzini     /* Strobe 1 */
3777dd285b06SPaolo Bonzini     { 0xe101e000, 0xfffce000, 0x800, "GPIOs" },			/* CS28 */
3778dd285b06SPaolo Bonzini 
3779dd285b06SPaolo Bonzini     { 0 }
3780dd285b06SPaolo Bonzini };
3781dd285b06SPaolo Bonzini 
3782dd285b06SPaolo Bonzini static void omap_setup_dsp_mapping(MemoryRegion *system_memory,
3783dd285b06SPaolo Bonzini                                    const struct omap_map_s *map)
3784dd285b06SPaolo Bonzini {
3785dd285b06SPaolo Bonzini     MemoryRegion *io;
3786dd285b06SPaolo Bonzini 
3787dd285b06SPaolo Bonzini     for (; map->phys_dsp; map ++) {
3788dd285b06SPaolo Bonzini         io = g_new(MemoryRegion, 1);
37892c9b15caSPaolo Bonzini         memory_region_init_alias(io, NULL, map->name,
3790dd285b06SPaolo Bonzini                                  system_memory, map->phys_mpu, map->size);
3791dd285b06SPaolo Bonzini         memory_region_add_subregion(system_memory, map->phys_dsp, io);
3792dd285b06SPaolo Bonzini     }
3793dd285b06SPaolo Bonzini }
3794dd285b06SPaolo Bonzini 
3795dd285b06SPaolo Bonzini void omap_mpu_wakeup(void *opaque, int irq, int req)
3796dd285b06SPaolo Bonzini {
3797dd285b06SPaolo Bonzini     struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
3798259186a7SAndreas Färber     CPUState *cpu = CPU(mpu->cpu);
3799dd285b06SPaolo Bonzini 
3800259186a7SAndreas Färber     if (cpu->halted) {
3801c3affe56SAndreas Färber         cpu_interrupt(cpu, CPU_INTERRUPT_EXITTB);
3802dd285b06SPaolo Bonzini     }
3803dd285b06SPaolo Bonzini }
3804dd285b06SPaolo Bonzini 
3805dd285b06SPaolo Bonzini static const struct dma_irq_map omap1_dma_irq_map[] = {
3806dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH0_6 },
3807dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH1_7 },
3808dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH2_8 },
3809dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH3 },
3810dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH4 },
3811dd285b06SPaolo Bonzini     { 0, OMAP_INT_DMA_CH5 },
3812dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH6 },
3813dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH7 },
3814dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH8 },
3815dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH9 },
3816dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH10 },
3817dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH11 },
3818dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH12 },
3819dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH13 },
3820dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH14 },
3821dd285b06SPaolo Bonzini     { 1, OMAP_INT_1610_DMA_CH15 }
3822dd285b06SPaolo Bonzini };
3823dd285b06SPaolo Bonzini 
3824dd285b06SPaolo Bonzini /* DMA ports for OMAP1 */
3825dd285b06SPaolo Bonzini static int omap_validate_emiff_addr(struct omap_mpu_state_s *s,
3826dd285b06SPaolo Bonzini                 hwaddr addr)
3827dd285b06SPaolo Bonzini {
3828dd285b06SPaolo Bonzini     return range_covers_byte(OMAP_EMIFF_BASE, s->sdram_size, addr);
3829dd285b06SPaolo Bonzini }
3830dd285b06SPaolo Bonzini 
3831dd285b06SPaolo Bonzini static int omap_validate_emifs_addr(struct omap_mpu_state_s *s,
3832dd285b06SPaolo Bonzini                 hwaddr addr)
3833dd285b06SPaolo Bonzini {
3834dd285b06SPaolo Bonzini     return range_covers_byte(OMAP_EMIFS_BASE, OMAP_EMIFF_BASE - OMAP_EMIFS_BASE,
3835dd285b06SPaolo Bonzini                              addr);
3836dd285b06SPaolo Bonzini }
3837dd285b06SPaolo Bonzini 
3838dd285b06SPaolo Bonzini static int omap_validate_imif_addr(struct omap_mpu_state_s *s,
3839dd285b06SPaolo Bonzini                 hwaddr addr)
3840dd285b06SPaolo Bonzini {
3841dd285b06SPaolo Bonzini     return range_covers_byte(OMAP_IMIF_BASE, s->sram_size, addr);
3842dd285b06SPaolo Bonzini }
3843dd285b06SPaolo Bonzini 
3844dd285b06SPaolo Bonzini static int omap_validate_tipb_addr(struct omap_mpu_state_s *s,
3845dd285b06SPaolo Bonzini                 hwaddr addr)
3846dd285b06SPaolo Bonzini {
3847dd285b06SPaolo Bonzini     return range_covers_byte(0xfffb0000, 0xffff0000 - 0xfffb0000, addr);
3848dd285b06SPaolo Bonzini }
3849dd285b06SPaolo Bonzini 
3850dd285b06SPaolo Bonzini static int omap_validate_local_addr(struct omap_mpu_state_s *s,
3851dd285b06SPaolo Bonzini                 hwaddr addr)
3852dd285b06SPaolo Bonzini {
3853dd285b06SPaolo Bonzini     return range_covers_byte(OMAP_LOCALBUS_BASE, 0x1000000, addr);
3854dd285b06SPaolo Bonzini }
3855dd285b06SPaolo Bonzini 
3856dd285b06SPaolo Bonzini static int omap_validate_tipb_mpui_addr(struct omap_mpu_state_s *s,
3857dd285b06SPaolo Bonzini                 hwaddr addr)
3858dd285b06SPaolo Bonzini {
3859dd285b06SPaolo Bonzini     return range_covers_byte(0xe1010000, 0xe1020004 - 0xe1010000, addr);
3860dd285b06SPaolo Bonzini }
3861dd285b06SPaolo Bonzini 
38624387b253SPhilippe Mathieu-Daudé struct omap_mpu_state_s *omap310_mpu_init(MemoryRegion *dram,
3863ba1ba5ccSIgor Mammedov                 const char *cpu_type)
3864dd285b06SPaolo Bonzini {
3865dd285b06SPaolo Bonzini     int i;
3866b45c03f5SMarkus Armbruster     struct omap_mpu_state_s *s = g_new0(struct omap_mpu_state_s, 1);
3867dd285b06SPaolo Bonzini     qemu_irq dma_irqs[6];
3868dd285b06SPaolo Bonzini     DriveInfo *dinfo;
3869dd285b06SPaolo Bonzini     SysBusDevice *busdev;
38704387b253SPhilippe Mathieu-Daudé     MemoryRegion *system_memory = get_system_memory();
3871dd285b06SPaolo Bonzini 
3872dd285b06SPaolo Bonzini     /* Core */
3873dd285b06SPaolo Bonzini     s->mpu_model = omap310;
3874ba1ba5ccSIgor Mammedov     s->cpu = ARM_CPU(cpu_create(cpu_type));
38754387b253SPhilippe Mathieu-Daudé     s->sdram_size = memory_region_size(dram);
3876dd285b06SPaolo Bonzini     s->sram_size = OMAP15XX_SRAM_SIZE;
3877dd285b06SPaolo Bonzini 
3878f3c7d038SAndreas Färber     s->wakeup = qemu_allocate_irq(omap_mpu_wakeup, s, 0);
3879dd285b06SPaolo Bonzini 
3880dd285b06SPaolo Bonzini     /* Clocks */
3881dd285b06SPaolo Bonzini     omap_clk_init(s);
3882dd285b06SPaolo Bonzini 
3883dd285b06SPaolo Bonzini     /* Memory-mapped stuff */
388498a99ce0SPeter Maydell     memory_region_init_ram(&s->imif_ram, NULL, "omap1.sram", s->sram_size,
3885f8ed85acSMarkus Armbruster                            &error_fatal);
3886dd285b06SPaolo Bonzini     memory_region_add_subregion(system_memory, OMAP_IMIF_BASE, &s->imif_ram);
3887dd285b06SPaolo Bonzini 
3888dd285b06SPaolo Bonzini     omap_clkm_init(system_memory, 0xfffece00, 0xe1008000, s);
3889dd285b06SPaolo Bonzini 
3890dd285b06SPaolo Bonzini     s->ih[0] = qdev_create(NULL, "omap-intc");
3891dd285b06SPaolo Bonzini     qdev_prop_set_uint32(s->ih[0], "size", 0x100);
3892bab592a2SMarc-André Lureau     omap_intc_set_iclk(OMAP_INTC(s->ih[0]), omap_findclk(s, "arminth_ck"));
3893dd285b06SPaolo Bonzini     qdev_init_nofail(s->ih[0]);
3894dd285b06SPaolo Bonzini     busdev = SYS_BUS_DEVICE(s->ih[0]);
3895437f0f10SPeter Maydell     sysbus_connect_irq(busdev, 0,
3896437f0f10SPeter Maydell                        qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ));
3897437f0f10SPeter Maydell     sysbus_connect_irq(busdev, 1,
3898437f0f10SPeter Maydell                        qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_FIQ));
3899dd285b06SPaolo Bonzini     sysbus_mmio_map(busdev, 0, 0xfffecb00);
3900dd285b06SPaolo Bonzini     s->ih[1] = qdev_create(NULL, "omap-intc");
3901dd285b06SPaolo Bonzini     qdev_prop_set_uint32(s->ih[1], "size", 0x800);
3902bab592a2SMarc-André Lureau     omap_intc_set_iclk(OMAP_INTC(s->ih[1]), omap_findclk(s, "arminth_ck"));
3903dd285b06SPaolo Bonzini     qdev_init_nofail(s->ih[1]);
3904dd285b06SPaolo Bonzini     busdev = SYS_BUS_DEVICE(s->ih[1]);
3905dd285b06SPaolo Bonzini     sysbus_connect_irq(busdev, 0,
3906dd285b06SPaolo Bonzini                        qdev_get_gpio_in(s->ih[0], OMAP_INT_15XX_IH2_IRQ));
3907dd285b06SPaolo Bonzini     /* The second interrupt controller's FIQ output is not wired up */
3908dd285b06SPaolo Bonzini     sysbus_mmio_map(busdev, 0, 0xfffe0000);
3909dd285b06SPaolo Bonzini 
3910dd285b06SPaolo Bonzini     for (i = 0; i < 6; i++) {
3911dd285b06SPaolo Bonzini         dma_irqs[i] = qdev_get_gpio_in(s->ih[omap1_dma_irq_map[i].ih],
3912dd285b06SPaolo Bonzini                                        omap1_dma_irq_map[i].intr);
3913dd285b06SPaolo Bonzini     }
3914dd285b06SPaolo Bonzini     s->dma = omap_dma_init(0xfffed800, dma_irqs, system_memory,
3915dd285b06SPaolo Bonzini                            qdev_get_gpio_in(s->ih[0], OMAP_INT_DMA_LCD),
3916dd285b06SPaolo Bonzini                            s, omap_findclk(s, "dma_ck"), omap_dma_3_1);
3917dd285b06SPaolo Bonzini 
3918dd285b06SPaolo Bonzini     s->port[emiff    ].addr_valid = omap_validate_emiff_addr;
3919dd285b06SPaolo Bonzini     s->port[emifs    ].addr_valid = omap_validate_emifs_addr;
3920dd285b06SPaolo Bonzini     s->port[imif     ].addr_valid = omap_validate_imif_addr;
3921dd285b06SPaolo Bonzini     s->port[tipb     ].addr_valid = omap_validate_tipb_addr;
3922dd285b06SPaolo Bonzini     s->port[local    ].addr_valid = omap_validate_local_addr;
3923dd285b06SPaolo Bonzini     s->port[tipb_mpui].addr_valid = omap_validate_tipb_mpui_addr;
3924dd285b06SPaolo Bonzini 
3925dd285b06SPaolo Bonzini     /* Register SDRAM and SRAM DMA ports for fast transfers.  */
39264387b253SPhilippe Mathieu-Daudé     soc_dma_port_add_mem(s->dma, memory_region_get_ram_ptr(dram),
3927dd285b06SPaolo Bonzini                          OMAP_EMIFF_BASE, s->sdram_size);
3928dd285b06SPaolo Bonzini     soc_dma_port_add_mem(s->dma, memory_region_get_ram_ptr(&s->imif_ram),
3929dd285b06SPaolo Bonzini                          OMAP_IMIF_BASE, s->sram_size);
3930dd285b06SPaolo Bonzini 
3931dd285b06SPaolo Bonzini     s->timer[0] = omap_mpu_timer_init(system_memory, 0xfffec500,
3932dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_TIMER1),
3933dd285b06SPaolo Bonzini                     omap_findclk(s, "mputim_ck"));
3934dd285b06SPaolo Bonzini     s->timer[1] = omap_mpu_timer_init(system_memory, 0xfffec600,
3935dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_TIMER2),
3936dd285b06SPaolo Bonzini                     omap_findclk(s, "mputim_ck"));
3937dd285b06SPaolo Bonzini     s->timer[2] = omap_mpu_timer_init(system_memory, 0xfffec700,
3938dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_TIMER3),
3939dd285b06SPaolo Bonzini                     omap_findclk(s, "mputim_ck"));
3940dd285b06SPaolo Bonzini 
3941dd285b06SPaolo Bonzini     s->wdt = omap_wd_timer_init(system_memory, 0xfffec800,
3942dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_WD_TIMER),
3943dd285b06SPaolo Bonzini                     omap_findclk(s, "armwdt_ck"));
3944dd285b06SPaolo Bonzini 
3945dd285b06SPaolo Bonzini     s->os_timer = omap_os_timer_init(system_memory, 0xfffb9000,
3946dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[1], OMAP_INT_OS_TIMER),
3947dd285b06SPaolo Bonzini                     omap_findclk(s, "clk32-kHz"));
3948dd285b06SPaolo Bonzini 
3949dd285b06SPaolo Bonzini     s->lcd = omap_lcdc_init(system_memory, 0xfffec000,
3950dd285b06SPaolo Bonzini                             qdev_get_gpio_in(s->ih[0], OMAP_INT_LCD_CTRL),
3951dd285b06SPaolo Bonzini                             omap_dma_get_lcdch(s->dma),
3952dd285b06SPaolo Bonzini                             omap_findclk(s, "lcd_ck"));
3953dd285b06SPaolo Bonzini 
3954dd285b06SPaolo Bonzini     omap_ulpd_pm_init(system_memory, 0xfffe0800, s);
3955dd285b06SPaolo Bonzini     omap_pin_cfg_init(system_memory, 0xfffe1000, s);
3956dd285b06SPaolo Bonzini     omap_id_init(system_memory, s);
3957dd285b06SPaolo Bonzini 
3958dd285b06SPaolo Bonzini     omap_mpui_init(system_memory, 0xfffec900, s);
3959dd285b06SPaolo Bonzini 
3960dd285b06SPaolo Bonzini     s->private_tipb = omap_tipb_bridge_init(system_memory, 0xfffeca00,
3961dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_BRIDGE_PRIV),
3962dd285b06SPaolo Bonzini                     omap_findclk(s, "tipb_ck"));
3963dd285b06SPaolo Bonzini     s->public_tipb = omap_tipb_bridge_init(system_memory, 0xfffed300,
3964dd285b06SPaolo Bonzini                     qdev_get_gpio_in(s->ih[0], OMAP_INT_BRIDGE_PUB),
3965dd285b06SPaolo Bonzini                     omap_findclk(s, "tipb_ck"));
3966dd285b06SPaolo Bonzini 
3967dd285b06SPaolo Bonzini     omap_tcmi_init(system_memory, 0xfffecc00, s);
3968dd285b06SPaolo Bonzini 
3969dd285b06SPaolo Bonzini     s->uart[0] = omap_uart_init(0xfffb0000,
3970dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_UART1),
3971dd285b06SPaolo Bonzini                     omap_findclk(s, "uart1_ck"),
3972dd285b06SPaolo Bonzini                     omap_findclk(s, "uart1_ck"),
3973dd285b06SPaolo Bonzini                     s->drq[OMAP_DMA_UART1_TX], s->drq[OMAP_DMA_UART1_RX],
3974dd285b06SPaolo Bonzini                     "uart1",
39759bca0edbSPeter Maydell                     serial_hd(0));
3976dd285b06SPaolo Bonzini     s->uart[1] = omap_uart_init(0xfffb0800,
3977dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_UART2),
3978dd285b06SPaolo Bonzini                     omap_findclk(s, "uart2_ck"),
3979dd285b06SPaolo Bonzini                     omap_findclk(s, "uart2_ck"),
3980dd285b06SPaolo Bonzini                     s->drq[OMAP_DMA_UART2_TX], s->drq[OMAP_DMA_UART2_RX],
3981dd285b06SPaolo Bonzini                     "uart2",
39829bca0edbSPeter Maydell                     serial_hd(0) ? serial_hd(1) : NULL);
3983dd285b06SPaolo Bonzini     s->uart[2] = omap_uart_init(0xfffb9800,
3984dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[0], OMAP_INT_UART3),
3985dd285b06SPaolo Bonzini                     omap_findclk(s, "uart3_ck"),
3986dd285b06SPaolo Bonzini                     omap_findclk(s, "uart3_ck"),
3987dd285b06SPaolo Bonzini                     s->drq[OMAP_DMA_UART3_TX], s->drq[OMAP_DMA_UART3_RX],
3988dd285b06SPaolo Bonzini                     "uart3",
39899bca0edbSPeter Maydell                     serial_hd(0) && serial_hd(1) ? serial_hd(2) : NULL);
3990dd285b06SPaolo Bonzini 
3991dd285b06SPaolo Bonzini     s->dpll[0] = omap_dpll_init(system_memory, 0xfffecf00,
3992dd285b06SPaolo Bonzini                                 omap_findclk(s, "dpll1"));
3993dd285b06SPaolo Bonzini     s->dpll[1] = omap_dpll_init(system_memory, 0xfffed000,
3994dd285b06SPaolo Bonzini                                 omap_findclk(s, "dpll2"));
3995dd285b06SPaolo Bonzini     s->dpll[2] = omap_dpll_init(system_memory, 0xfffed100,
3996dd285b06SPaolo Bonzini                                 omap_findclk(s, "dpll3"));
3997dd285b06SPaolo Bonzini 
3998dd285b06SPaolo Bonzini     dinfo = drive_get(IF_SD, 0, 0);
3999a82929a2SThomas Huth     if (!dinfo && !qtest_enabled()) {
4000a82929a2SThomas Huth         warn_report("missing SecureDigital device");
4001dd285b06SPaolo Bonzini     }
4002fa1d36dfSMarkus Armbruster     s->mmc = omap_mmc_init(0xfffb7800, system_memory,
4003a82929a2SThomas Huth                            dinfo ? blk_by_legacy_dinfo(dinfo) : NULL,
4004dd285b06SPaolo Bonzini                            qdev_get_gpio_in(s->ih[1], OMAP_INT_OQN),
4005dd285b06SPaolo Bonzini                            &s->drq[OMAP_DMA_MMC_TX],
4006dd285b06SPaolo Bonzini                     omap_findclk(s, "mmc_ck"));
4007dd285b06SPaolo Bonzini 
4008dd285b06SPaolo Bonzini     s->mpuio = omap_mpuio_init(system_memory, 0xfffb5000,
4009dd285b06SPaolo Bonzini                                qdev_get_gpio_in(s->ih[1], OMAP_INT_KEYBOARD),
4010dd285b06SPaolo Bonzini                                qdev_get_gpio_in(s->ih[1], OMAP_INT_MPUIO),
4011dd285b06SPaolo Bonzini                                s->wakeup, omap_findclk(s, "clk32-kHz"));
4012dd285b06SPaolo Bonzini 
4013dd285b06SPaolo Bonzini     s->gpio = qdev_create(NULL, "omap-gpio");
4014dd285b06SPaolo Bonzini     qdev_prop_set_int32(s->gpio, "mpu_model", s->mpu_model);
4015*ba2aba83SMarc-André Lureau     omap_gpio_set_clk(OMAP1_GPIO(s->gpio), omap_findclk(s, "arm_gpio_ck"));
4016dd285b06SPaolo Bonzini     qdev_init_nofail(s->gpio);
4017dd285b06SPaolo Bonzini     sysbus_connect_irq(SYS_BUS_DEVICE(s->gpio), 0,
4018dd285b06SPaolo Bonzini                        qdev_get_gpio_in(s->ih[0], OMAP_INT_GPIO_BANK1));
4019dd285b06SPaolo Bonzini     sysbus_mmio_map(SYS_BUS_DEVICE(s->gpio), 0, 0xfffce000);
4020dd285b06SPaolo Bonzini 
4021dd285b06SPaolo Bonzini     s->microwire = omap_uwire_init(system_memory, 0xfffb3000,
4022dd285b06SPaolo Bonzini                                    qdev_get_gpio_in(s->ih[1], OMAP_INT_uWireTX),
4023dd285b06SPaolo Bonzini                                    qdev_get_gpio_in(s->ih[1], OMAP_INT_uWireRX),
4024dd285b06SPaolo Bonzini                     s->drq[OMAP_DMA_UWIRE_TX], omap_findclk(s, "mpuper_ck"));
4025dd285b06SPaolo Bonzini 
4026dd285b06SPaolo Bonzini     s->pwl = omap_pwl_init(system_memory, 0xfffb5800,
4027dd285b06SPaolo Bonzini                            omap_findclk(s, "armxor_ck"));
4028dd285b06SPaolo Bonzini     s->pwt = omap_pwt_init(system_memory, 0xfffb6000,
4029dd285b06SPaolo Bonzini                            omap_findclk(s, "armxor_ck"));
4030dd285b06SPaolo Bonzini 
4031dd285b06SPaolo Bonzini     s->i2c[0] = qdev_create(NULL, "omap_i2c");
4032dd285b06SPaolo Bonzini     qdev_prop_set_uint8(s->i2c[0], "revision", 0x11);
40330fd20c53SMarc-André Lureau     omap_i2c_set_fclk(OMAP_I2C(s->i2c[0]), omap_findclk(s, "mpuper_ck"));
4034dd285b06SPaolo Bonzini     qdev_init_nofail(s->i2c[0]);
4035dd285b06SPaolo Bonzini     busdev = SYS_BUS_DEVICE(s->i2c[0]);
4036dd285b06SPaolo Bonzini     sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(s->ih[1], OMAP_INT_I2C));
4037dd285b06SPaolo Bonzini     sysbus_connect_irq(busdev, 1, s->drq[OMAP_DMA_I2C_TX]);
4038dd285b06SPaolo Bonzini     sysbus_connect_irq(busdev, 2, s->drq[OMAP_DMA_I2C_RX]);
4039dd285b06SPaolo Bonzini     sysbus_mmio_map(busdev, 0, 0xfffb3800);
4040dd285b06SPaolo Bonzini 
4041dd285b06SPaolo Bonzini     s->rtc = omap_rtc_init(system_memory, 0xfffb4800,
4042dd285b06SPaolo Bonzini                            qdev_get_gpio_in(s->ih[1], OMAP_INT_RTC_TIMER),
4043dd285b06SPaolo Bonzini                            qdev_get_gpio_in(s->ih[1], OMAP_INT_RTC_ALARM),
4044dd285b06SPaolo Bonzini                     omap_findclk(s, "clk32-kHz"));
4045dd285b06SPaolo Bonzini 
4046dd285b06SPaolo Bonzini     s->mcbsp1 = omap_mcbsp_init(system_memory, 0xfffb1800,
4047dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_McBSP1TX),
4048dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_McBSP1RX),
4049dd285b06SPaolo Bonzini                     &s->drq[OMAP_DMA_MCBSP1_TX], omap_findclk(s, "dspxor_ck"));
4050dd285b06SPaolo Bonzini     s->mcbsp2 = omap_mcbsp_init(system_memory, 0xfffb1000,
4051dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[0],
4052dd285b06SPaolo Bonzini                                                  OMAP_INT_310_McBSP2_TX),
4053dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[0],
4054dd285b06SPaolo Bonzini                                                  OMAP_INT_310_McBSP2_RX),
4055dd285b06SPaolo Bonzini                     &s->drq[OMAP_DMA_MCBSP2_TX], omap_findclk(s, "mpuper_ck"));
4056dd285b06SPaolo Bonzini     s->mcbsp3 = omap_mcbsp_init(system_memory, 0xfffb7000,
4057dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_McBSP3TX),
4058dd285b06SPaolo Bonzini                                 qdev_get_gpio_in(s->ih[1], OMAP_INT_McBSP3RX),
4059dd285b06SPaolo Bonzini                     &s->drq[OMAP_DMA_MCBSP3_TX], omap_findclk(s, "dspxor_ck"));
4060dd285b06SPaolo Bonzini 
4061dd285b06SPaolo Bonzini     s->led[0] = omap_lpg_init(system_memory,
4062dd285b06SPaolo Bonzini                               0xfffbd000, omap_findclk(s, "clk32-kHz"));
4063dd285b06SPaolo Bonzini     s->led[1] = omap_lpg_init(system_memory,
4064dd285b06SPaolo Bonzini                               0xfffbd800, omap_findclk(s, "clk32-kHz"));
4065dd285b06SPaolo Bonzini 
4066dd285b06SPaolo Bonzini     /* Register mappings not currenlty implemented:
4067dd285b06SPaolo Bonzini      * MCSI2 Comm	fffb2000 - fffb27ff (not mapped on OMAP310)
4068dd285b06SPaolo Bonzini      * MCSI1 Bluetooth	fffb2800 - fffb2fff (not mapped on OMAP310)
4069dd285b06SPaolo Bonzini      * USB W2FC		fffb4000 - fffb47ff
4070dd285b06SPaolo Bonzini      * Camera Interface	fffb6800 - fffb6fff
4071dd285b06SPaolo Bonzini      * USB Host		fffba000 - fffba7ff
4072dd285b06SPaolo Bonzini      * FAC		fffba800 - fffbafff
4073dd285b06SPaolo Bonzini      * HDQ/1-Wire	fffbc000 - fffbc7ff
4074dd285b06SPaolo Bonzini      * TIPB switches	fffbc800 - fffbcfff
4075dd285b06SPaolo Bonzini      * Mailbox		fffcf000 - fffcf7ff
4076dd285b06SPaolo Bonzini      * Local bus IF	fffec100 - fffec1ff
4077dd285b06SPaolo Bonzini      * Local bus MMU	fffec200 - fffec2ff
4078dd285b06SPaolo Bonzini      * DSP MMU		fffed200 - fffed2ff
4079dd285b06SPaolo Bonzini      */
4080dd285b06SPaolo Bonzini 
4081dd285b06SPaolo Bonzini     omap_setup_dsp_mapping(system_memory, omap15xx_dsp_mm);
4082dd285b06SPaolo Bonzini     omap_setup_mpui_io(system_memory, s);
4083dd285b06SPaolo Bonzini 
4084dd285b06SPaolo Bonzini     qemu_register_reset(omap1_mpu_reset, s);
4085dd285b06SPaolo Bonzini 
4086dd285b06SPaolo Bonzini     return s;
4087dd285b06SPaolo Bonzini }
4088