Merge branch 'master' of /home/nchip/public_html/qemu into garage-push
[qemu] / hw / omap2.c
1 /*
2  * TI OMAP processors emulation.
3  *
4  * Copyright (C) 2007-2008 Nokia Corporation
5  * Written by Andrzej Zaborowski <andrew@openedhand.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 or
10  * (at your option) version 3 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include "hw.h"
22 #include "arm-misc.h"
23 #include "omap.h"
24 #include "sysemu.h"
25 #include "qemu-timer.h"
26 #include "qemu-char.h"
27 #include "flash.h"
28 #include "soc_dma.h"
29 #include "audio/audio.h"
30
31 /* GP timers */
32 struct omap_gp_timer_s {
33     qemu_irq irq;
34     qemu_irq wkup;
35     qemu_irq in;
36     qemu_irq out;
37     omap_clk clk;
38     QEMUTimer *timer;
39     QEMUTimer *match;
40     struct omap_target_agent_s *ta;
41
42     int in_val;
43     int out_val;
44     int64_t time;
45     int64_t rate;
46     int64_t ticks_per_sec;
47
48     int16_t config;
49     int status;
50     int it_ena;
51     int wu_ena;
52     int enable;
53     int inout;
54     int capt2;
55     int pt;
56     enum {
57         gpt_trigger_none, gpt_trigger_overflow, gpt_trigger_both
58     } trigger;
59     enum {
60         gpt_capture_none, gpt_capture_rising,
61         gpt_capture_falling, gpt_capture_both
62     } capture;
63     int scpwm;
64     int ce;
65     int pre;
66     int ptv;
67     int ar;
68     int st;
69     int posted;
70     uint32_t val;
71     uint32_t load_val;
72     uint32_t capture_val[2];
73     uint32_t match_val;
74     int capt_num;
75
76     uint16_t writeh;    /* LSB */
77     uint16_t readh;     /* MSB */
78 };
79
80 #define GPT_TCAR_IT     (1 << 2)
81 #define GPT_OVF_IT      (1 << 1)
82 #define GPT_MAT_IT      (1 << 0)
83
84 /*if the clock source of gptimer changes, rate must be regenerated*/
85 void omap_gp_timer_change_clk(struct omap_gp_timer_s *timer)
86 {
87     timer->rate = omap_clk_getrate(timer->clk);
88 }
89
90 static inline void omap_gp_timer_intr(struct omap_gp_timer_s *timer, int it)
91 {
92     if (timer->it_ena & it) {
93         if (!timer->status)
94             qemu_irq_raise(timer->irq);
95
96         timer->status |= it;
97         /* Or are the status bits set even when masked?
98          * i.e. is masking applied before or after the status register?  */
99     }
100
101     if (timer->wu_ena & it)
102         qemu_irq_pulse(timer->wkup);
103 }
104
105 static inline void omap_gp_timer_out(struct omap_gp_timer_s *timer, int level)
106 {
107     if (!timer->inout && timer->out_val != level) {
108         timer->out_val = level;
109         qemu_set_irq(timer->out, level);
110     }
111 }
112
113 static inline uint32_t omap_gp_timer_read(struct omap_gp_timer_s *timer)
114 {
115     uint64_t distance, rate;
116
117     if (timer->st && timer->rate) {
118         distance = qemu_get_clock(vm_clock) - timer->time;
119         
120         /*if ticks_per_sec is bigger than 32bit we cannot use muldiv64*/
121         if (timer->ticks_per_sec > 0xffffffff) {
122             distance /= ticks_per_sec / 1000; /*distance ms*/
123             rate = timer->rate >> (timer->pre ? timer->ptv + 1 : 0);
124             distance = muldiv64(distance, rate, 1000);
125         } else
126             distance = muldiv64(distance, timer->rate, timer->ticks_per_sec);
127
128         if (distance >= 0xffffffff - timer->val)
129             return 0xffffffff;
130         else
131             return timer->val + distance;
132     } else
133         return timer->val;
134 }
135
136 static inline void omap_gp_timer_sync(struct omap_gp_timer_s *timer)
137 {
138     if (timer->st) {
139         timer->val = omap_gp_timer_read(timer);
140         timer->time = qemu_get_clock(vm_clock);
141     }
142 }
143
144 static inline void omap_gp_timer_update(struct omap_gp_timer_s *timer)
145 {
146     int64_t expires, matches, rate;
147
148     if (timer->st && timer->rate) {
149         if (timer->ticks_per_sec > 0xffffffff) {
150             rate = timer->rate >> (timer->pre ? timer->ptv + 1 : 0); /*1s -> rate ticks*/
151             expires = muldiv64(0x100000000ll - timer->val, ticks_per_sec, rate);
152         } else
153             expires = muldiv64(0x100000000ll - timer->val,
154                         timer->ticks_per_sec, timer->rate);
155         qemu_mod_timer(timer->timer, timer->time + expires);
156
157         if (timer->ce && timer->match_val >= timer->val) {
158             if (timer->ticks_per_sec > 0xffffffff) {
159                 rate = timer->rate >> (timer->pre ? timer->ptv + 1 : 0); /*1s -> rate ticks*/
160                 matches = muldiv64(timer->match_val - timer->val, ticks_per_sec, rate);
161             } else
162                 matches = muldiv64(timer->match_val - timer->val,
163                             timer->ticks_per_sec, timer->rate);
164             qemu_mod_timer(timer->match, timer->time + matches);
165         } else
166             qemu_del_timer(timer->match);
167     } else {
168         qemu_del_timer(timer->timer);
169         qemu_del_timer(timer->match);
170         omap_gp_timer_out(timer, timer->scpwm);
171     }
172 }
173
174 static inline void omap_gp_timer_trigger(struct omap_gp_timer_s *timer)
175 {
176     if (timer->pt)
177         /* TODO in overflow-and-match mode if the first event to
178          * occur is the match, don't toggle.  */
179         omap_gp_timer_out(timer, !timer->out_val);
180     else
181         /* TODO inverted pulse on timer->out_val == 1?  */
182         qemu_irq_pulse(timer->out);
183 }
184
185 static void omap_gp_timer_tick(void *opaque)
186 {
187     struct omap_gp_timer_s *timer = (struct omap_gp_timer_s *) opaque;
188
189     if (!timer->ar) {
190         timer->st = 0;
191         timer->val = 0;
192     } else {
193         timer->val = timer->load_val;
194         timer->time = qemu_get_clock(vm_clock);
195     }
196
197     if (timer->trigger == gpt_trigger_overflow ||
198                     timer->trigger == gpt_trigger_both)
199         omap_gp_timer_trigger(timer);
200
201     omap_gp_timer_intr(timer, GPT_OVF_IT);
202     omap_gp_timer_update(timer);
203 }
204
205 static void omap_gp_timer_match(void *opaque)
206 {
207     struct omap_gp_timer_s *timer = (struct omap_gp_timer_s *) opaque;
208
209     if (timer->trigger == gpt_trigger_both)
210         omap_gp_timer_trigger(timer);
211
212     omap_gp_timer_intr(timer, GPT_MAT_IT);
213 }
214
215 static void omap_gp_timer_input(void *opaque, int line, int on)
216 {
217     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *) opaque;
218     int trigger;
219
220     switch (s->capture) {
221     default:
222     case gpt_capture_none:
223         trigger = 0;
224         break;
225     case gpt_capture_rising:
226         trigger = !s->in_val && on;
227         break;
228     case gpt_capture_falling:
229         trigger = s->in_val && !on;
230         break;
231     case gpt_capture_both:
232         trigger = (s->in_val == !on);
233         break;
234     }
235     s->in_val = on;
236
237     if (s->inout && trigger && s->capt_num < 2) {
238         s->capture_val[s->capt_num] = omap_gp_timer_read(s);
239
240         if (s->capt2 == s->capt_num ++)
241             omap_gp_timer_intr(s, GPT_TCAR_IT);
242     }
243 }
244
245 static void omap_gp_timer_clk_update(void *opaque, int line, int on)
246 {
247     struct omap_gp_timer_s *timer = (struct omap_gp_timer_s *) opaque;
248
249     omap_gp_timer_sync(timer);
250     timer->rate = on ? omap_clk_getrate(timer->clk) : 0;
251     omap_gp_timer_update(timer);
252 }
253
254 static void omap_gp_timer_clk_setup(struct omap_gp_timer_s *timer)
255 {
256     omap_clk_adduser(timer->clk,
257                     qemu_allocate_irqs(omap_gp_timer_clk_update, timer, 1)[0]);
258     timer->rate = omap_clk_getrate(timer->clk);
259     //fprintf(stderr, "omap gptimer clk rate 0x%llx\n", timer->rate);
260 }
261
262 static void omap_gp_timer_reset(struct omap_gp_timer_s *s)
263 {
264     s->config = 0x000;
265     s->status = 0;
266     s->it_ena = 0;
267     s->wu_ena = 0;
268     s->inout = 0;
269     s->capt2 = 0;
270     s->capt_num = 0;
271     s->pt = 0;
272     s->trigger = gpt_trigger_none;
273     s->capture = gpt_capture_none;
274     s->scpwm = 0;
275     s->ce = 0;
276     s->pre = 0;
277     s->ptv = 0;
278     s->ar = 0;
279     s->st = 0;
280     s->posted = 1;
281     s->val = 0x00000000;
282     s->load_val = 0x00000000;
283     s->capture_val[0] = 0x00000000;
284     s->capture_val[1] = 0x00000000;
285     s->match_val = 0x00000000;
286     omap_gp_timer_update(s);
287 }
288
289 static uint32_t omap_gp_timer_readw(void *opaque, target_phys_addr_t addr)
290 {
291     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *) opaque;
292
293     switch (addr) {
294     case 0x00:  /* TIDR */
295         return 0x21;
296
297     case 0x10:  /* TIOCP_CFG */
298         return s->config;
299
300     case 0x14:  /* TISTAT */
301         /* ??? When's this bit reset? */
302         return 1;                                               /* RESETDONE */
303
304     case 0x18:  /* TISR */
305         return s->status;
306
307     case 0x1c:  /* TIER */
308         return s->it_ena;
309
310     case 0x20:  /* TWER */
311         return s->wu_ena;
312
313     case 0x24:  /* TCLR */
314         return (s->inout << 14) |
315                 (s->capt2 << 13) |
316                 (s->pt << 12) |
317                 (s->trigger << 10) |
318                 (s->capture << 8) |
319                 (s->scpwm << 7) |
320                 (s->ce << 6) |
321                 (s->pre << 5) |
322                 (s->ptv << 2) |
323                 (s->ar << 1) |
324                 (s->st << 0);
325
326     case 0x28:  /* TCRR */
327         return omap_gp_timer_read(s);
328
329     case 0x2c:  /* TLDR */
330         return s->load_val;
331
332     case 0x30:  /* TTGR */
333         return 0xffffffff;
334
335     case 0x34:  /* TWPS */
336         return 0x00000000;      /* No posted writes pending.  */
337
338     case 0x38:  /* TMAR */
339         return s->match_val;
340
341     case 0x3c:  /* TCAR1 */
342         return s->capture_val[0];
343
344     case 0x40:  /* TSICR */
345         return s->posted << 2;
346
347     case 0x44:  /* TCAR2 */
348         return s->capture_val[1];
349     }
350
351     OMAP_BAD_REG(addr);
352     return 0;
353 }
354
355 static uint32_t omap_gp_timer_readh(void *opaque, target_phys_addr_t addr)
356 {
357     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *) opaque;
358     uint32_t ret;
359
360     if (addr & 2)
361         return s->readh;
362     else {
363         ret = omap_gp_timer_readw(opaque, addr);
364         s->readh = ret >> 16;
365         return ret & 0xffff;
366     }
367 }
368
369 static CPUReadMemoryFunc *omap_gp_timer_readfn[] = {
370     omap_badwidth_read32,
371     omap_gp_timer_readh,
372     omap_gp_timer_readw,
373 };
374
375 static void omap_gp_timer_write(void *opaque, target_phys_addr_t addr,
376                 uint32_t value)
377 {
378     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *) opaque;
379
380     switch (addr) {
381     case 0x00:  /* TIDR */
382     case 0x14:  /* TISTAT */
383     case 0x34:  /* TWPS */
384     case 0x3c:  /* TCAR1 */
385     case 0x44:  /* TCAR2 */
386         OMAP_RO_REG(addr);
387         break;
388
389     case 0x10:  /* TIOCP_CFG */
390         s->config = value & 0x33d;
391         if (((value >> 3) & 3) == 3)                            /* IDLEMODE */
392             fprintf(stderr, "%s: illegal IDLEMODE value in TIOCP_CFG\n",
393                             __FUNCTION__);
394         if (value & 2)                                          /* SOFTRESET */
395             omap_gp_timer_reset(s);
396         break;
397
398     case 0x18:  /* TISR */
399         if (value & GPT_TCAR_IT)
400             s->capt_num = 0;
401         if (s->status && !(s->status &= ~value))
402             qemu_irq_lower(s->irq);
403         break;
404
405     case 0x1c:  /* TIER */
406         s->it_ena = value & 7;
407         break;
408
409     case 0x20:  /* TWER */
410         s->wu_ena = value & 7;
411         break;
412
413     case 0x24:  /* TCLR */
414         omap_gp_timer_sync(s);
415         s->inout = (value >> 14) & 1;
416         s->capt2 = (value >> 13) & 1;
417         s->pt = (value >> 12) & 1;
418         s->trigger = (value >> 10) & 3;
419         if (s->capture == gpt_capture_none &&
420                         ((value >> 8) & 3) != gpt_capture_none)
421             s->capt_num = 0;
422         s->capture = (value >> 8) & 3;
423         s->scpwm = (value >> 7) & 1;
424         s->ce = (value >> 6) & 1;
425         s->pre = (value >> 5) & 1;
426         s->ptv = (value >> 2) & 7;
427         s->ar = (value >> 1) & 1;
428         s->st = (value >> 0) & 1;
429         if (s->inout && s->trigger != gpt_trigger_none)
430             fprintf(stderr, "%s: GP timer pin must be an output "
431                             "for this trigger mode\n", __FUNCTION__);
432         if (!s->inout && s->capture != gpt_capture_none)
433             fprintf(stderr, "%s: GP timer pin must be an input "
434                             "for this capture mode\n", __FUNCTION__);
435         if (s->trigger == gpt_trigger_none)
436             omap_gp_timer_out(s, s->scpwm);
437         /* TODO: make sure this doesn't overflow 32-bits */
438         s->ticks_per_sec = ticks_per_sec << (s->pre ? s->ptv + 1 : 0);
439         omap_gp_timer_update(s);
440         break;
441
442     case 0x28:  /* TCRR */
443         s->time = qemu_get_clock(vm_clock);
444         s->val = value;
445         omap_gp_timer_update(s);
446         break;
447
448     case 0x2c:  /* TLDR */
449         s->load_val = value;
450         break;
451
452     case 0x30:  /* TTGR */
453         s->time = qemu_get_clock(vm_clock);
454         s->val = s->load_val;
455         omap_gp_timer_update(s);
456         break;
457
458     case 0x38:  /* TMAR */
459         omap_gp_timer_sync(s);
460         s->match_val = value;
461         omap_gp_timer_update(s);
462         break;
463
464     case 0x40:  /* TSICR */
465         s->posted = (value >> 2) & 1;
466         if (value & 2)  /* How much exactly are we supposed to reset? */
467             omap_gp_timer_reset(s);
468         break;
469
470     default:
471         OMAP_BAD_REG(addr);
472     }
473 }
474
475 static void omap_gp_timer_writeh(void *opaque, target_phys_addr_t addr,
476                 uint32_t value)
477 {
478     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *) opaque;
479
480     if (addr & 2)
481         return omap_gp_timer_write(opaque, addr, (value << 16) | s->writeh);
482     else
483         s->writeh = (uint16_t) value;
484 }
485
486 static CPUWriteMemoryFunc *omap_gp_timer_writefn[] = {
487     omap_badwidth_write32,
488     omap_gp_timer_writeh,
489     omap_gp_timer_write,
490 };
491
492 static void omap_gp_timer_save_state(QEMUFile *f, void *opaque)
493 {
494     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *)opaque;
495     
496     qemu_put_timer(f, s->timer);
497     qemu_put_timer(f, s->match);
498     qemu_put_sbe32(f, s->in_val);
499     qemu_put_sbe32(f, s->out_val);
500     qemu_put_sbe64(f, s->time);
501     qemu_put_sbe64(f, s->rate);
502     qemu_put_sbe64(f, s->ticks_per_sec);
503     qemu_put_sbe16(f, s->config);
504     qemu_put_sbe32(f, s->status);
505     qemu_put_sbe32(f, s->it_ena);
506     qemu_put_sbe32(f, s->wu_ena);
507     qemu_put_sbe32(f, s->enable);
508     qemu_put_sbe32(f, s->inout);
509     qemu_put_sbe32(f, s->capt2);
510     qemu_put_sbe32(f, s->pt);
511     qemu_put_sbe32(f, s->trigger);
512     qemu_put_sbe32(f, s->capture);
513     qemu_put_sbe32(f, s->scpwm);
514     qemu_put_sbe32(f, s->ce);
515     qemu_put_sbe32(f, s->pre);
516     qemu_put_sbe32(f, s->ptv);
517     qemu_put_sbe32(f, s->ar);
518     qemu_put_sbe32(f, s->st);
519     qemu_put_sbe32(f, s->posted);
520     qemu_put_be32(f, s->val);
521     qemu_put_be32(f, s->load_val);
522     qemu_put_be32(f, s->capture_val[0]);
523     qemu_put_be32(f, s->capture_val[1]);
524     qemu_put_be32(f, s->match_val);
525     qemu_put_sbe32(f, s->capt_num);
526     qemu_put_be16(f, s->writeh);
527     qemu_put_be16(f, s->readh);
528 }
529
530 static int omap_gp_timer_load_state(QEMUFile *f, void *opaque, int version_id)
531 {
532     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *)opaque;
533     
534     if (version_id)
535         return -EINVAL;
536     
537     qemu_get_timer(f, s->timer);
538     qemu_get_timer(f, s->match);
539     s->in_val = qemu_get_sbe32(f);
540     s->out_val = qemu_get_sbe32(f);
541     s->time = qemu_get_sbe64(f);
542     s->rate = qemu_get_sbe64(f);
543     s->ticks_per_sec = qemu_get_sbe64(f);
544     s->config = qemu_get_sbe16(f);
545     s->status = qemu_get_sbe32(f);
546     s->it_ena = qemu_get_sbe32(f);
547     s->wu_ena = qemu_get_sbe32(f);
548     s->enable = qemu_get_sbe32(f);
549     s->inout = qemu_get_sbe32(f);
550     s->capt2 = qemu_get_sbe32(f);
551     s->pt = qemu_get_sbe32(f);
552     s->trigger = qemu_get_sbe32(f);
553     s->capture = qemu_get_sbe32(f);
554     s->scpwm = qemu_get_sbe32(f);
555     s->ce = qemu_get_sbe32(f);
556     s->pre = qemu_get_sbe32(f);
557     s->ptv = qemu_get_sbe32(f);
558     s->ar = qemu_get_sbe32(f);
559     s->st = qemu_get_sbe32(f);
560     s->posted = qemu_get_sbe32(f);
561     s->val = qemu_get_be32(f);
562     s->load_val = qemu_get_be32(f);
563     s->capture_val[0] = qemu_get_be32(f);
564     s->capture_val[1] = qemu_get_be32(f);
565     s->match_val = qemu_get_be32(f);
566     s->capt_num = qemu_get_sbe32(f);
567     s->writeh = qemu_get_be16(f);
568     s->readh = qemu_get_be16(f);
569     
570     omap_gp_timer_update(s);
571     
572     return 0;
573 }
574
575 struct omap_gp_timer_s *omap_gp_timer_init(struct omap_target_agent_s *ta,
576                 qemu_irq irq, omap_clk fclk, omap_clk iclk)
577 {
578     int iomemtype;
579     struct omap_gp_timer_s *s = (struct omap_gp_timer_s *)
580             qemu_mallocz(sizeof(struct omap_gp_timer_s));
581
582     s->ta = ta;
583     s->irq = irq;
584     s->clk = fclk;
585     s->timer = qemu_new_timer(vm_clock, omap_gp_timer_tick, s);
586     s->match = qemu_new_timer(vm_clock, omap_gp_timer_match, s);
587     s->in = qemu_allocate_irqs(omap_gp_timer_input, s, 1)[0];
588     omap_gp_timer_reset(s);
589     omap_gp_timer_clk_setup(s);
590
591     iomemtype = l4_register_io_memory(0, omap_gp_timer_readfn,
592                     omap_gp_timer_writefn, s);
593     omap_l4_attach(ta, 0, iomemtype);
594
595     register_savevm("omap_gp_timer", (ta->base >> 12) & 0xfffff, 0,
596                     omap_gp_timer_save_state, omap_gp_timer_load_state, s);
597     return s;
598 }
599
600 /* 32-kHz Sync Timer of the OMAP2 */
601 static uint32_t omap_synctimer_read(struct omap_synctimer_s *s) {
602     return muldiv64(qemu_get_clock(vm_clock), 0x8000, ticks_per_sec);
603 }
604
605 static void omap_synctimer_reset(struct omap_synctimer_s *s)
606 {
607     s->val = omap_synctimer_read(s);
608 }
609
610 static uint32_t omap_synctimer_readw(void *opaque, target_phys_addr_t addr)
611 {
612     struct omap_synctimer_s *s = (struct omap_synctimer_s *) opaque;
613     
614     switch (addr) {
615     case 0x00:  /* 32KSYNCNT_REV */
616         return 0x21;
617     case 0x10:  /* CR */
618         return omap_synctimer_read(s) - s->val;
619     }
620
621     OMAP_BAD_REG(addr);
622     return 0;
623 }
624
625 static uint32_t omap3_synctimer_readw(void *opaque, target_phys_addr_t addr)
626 {
627     struct omap_synctimer_s *s = (struct omap_synctimer_s *)opaque;
628     return (addr == 0x04) 
629         ? s->sysconfig 
630         : omap_synctimer_readw(opaque, addr);
631 }
632
633 static uint32_t omap_synctimer_readh(void *opaque, target_phys_addr_t addr)
634 {
635     struct omap_synctimer_s *s = (struct omap_synctimer_s *) opaque;
636     uint32_t ret;
637
638     if (addr & 2)
639         return s->readh;
640
641     ret = omap_synctimer_readw(opaque, addr);
642     s->readh = ret >> 16;
643     return ret & 0xffff;
644 }
645
646 static uint32_t omap3_synctimer_readh(void *opaque, target_phys_addr_t addr)
647 {
648     struct omap_synctimer_s *s = (struct omap_synctimer_s *) opaque;
649     uint32_t ret;
650     
651     if (addr & 2)
652         return s->readh;
653     
654     ret = omap3_synctimer_readw(opaque, addr);
655     s->readh = ret >> 16;
656     return ret & 0xffff;
657 }
658
659 static CPUReadMemoryFunc *omap_synctimer_readfn[] = {
660     omap_badwidth_read32,
661     omap_synctimer_readh,
662     omap_synctimer_readw,
663 };
664
665 static CPUReadMemoryFunc *omap3_synctimer_readfn[] = {
666     omap_badwidth_read32,
667     omap3_synctimer_readh,
668     omap3_synctimer_readw,
669 };
670
671 static void omap_synctimer_write(void *opaque, target_phys_addr_t addr,
672                 uint32_t value)
673 {
674     OMAP_BAD_REG(addr);
675 }
676
677 static void omap3_synctimer_write(void *opaque, target_phys_addr_t addr,
678                 uint32_t value)
679 {
680     struct omap_synctimer_s *s = (struct omap_synctimer_s *)opaque;
681     if (addr == 0x04) /* SYSCONFIG */
682         s->sysconfig = value & 0x0c;
683     else
684         OMAP_BAD_REG(addr);
685 }
686
687 static CPUWriteMemoryFunc *omap_synctimer_writefn[] = {
688     omap_badwidth_write32,
689     omap_synctimer_write,
690     omap_synctimer_write,
691 };
692
693 static CPUWriteMemoryFunc *omap3_synctimer_writefn[] = {
694     omap_badwidth_write32,
695     omap3_synctimer_write,
696     omap3_synctimer_write,
697 };
698
699 static void omap_synctimer_save_state(QEMUFile *f, void *opaque)
700 {
701     struct omap_synctimer_s *s = (struct omap_synctimer_s *)opaque;
702     
703     qemu_put_be32(f, s->val);
704     qemu_put_be16(f, s->readh);
705     qemu_put_be32(f, s->sysconfig);
706 }
707
708 static int omap_synctimer_load_state(QEMUFile *f, void *opaque, int version_id)
709 {
710     struct omap_synctimer_s *s = (struct omap_synctimer_s *)opaque;
711     
712     if (version_id)
713         return -EINVAL;
714     
715     s->val = qemu_get_be32(f);
716     s->readh = qemu_get_be16(f);
717     s->sysconfig = qemu_get_be32(f);
718     
719     omap_synctimer_reset(s);
720     
721     return 0;
722 }
723
724 void omap_synctimer_init(struct omap_target_agent_s *ta,
725                 struct omap_mpu_state_s *mpu, omap_clk fclk, omap_clk iclk)
726 {
727     struct omap_synctimer_s *s = &mpu->synctimer;
728
729     omap_synctimer_reset(s);
730     if (cpu_class_omap3(mpu))
731         omap_l4_attach(ta, 0, l4_register_io_memory(0,
732                     omap3_synctimer_readfn, omap3_synctimer_writefn, s));
733     else
734         omap_l4_attach(ta, 0, l4_register_io_memory(0,
735                     omap_synctimer_readfn, omap_synctimer_writefn, s));
736     
737     register_savevm("omap_synctimer", -1, 0,
738                     omap_synctimer_save_state, omap_synctimer_load_state, s);
739 }
740
741 /* General-Purpose Interface of OMAP2 */
742 struct omap2_gpio_s {
743     qemu_irq irq[2];
744     qemu_irq wkup;
745     qemu_irq *in;
746     qemu_irq handler[32];
747
748     uint8_t revision;
749     uint8_t config[2];
750     uint32_t inputs;
751     uint32_t outputs;
752     uint32_t dir;
753     uint32_t level[2];
754     uint32_t edge[2];
755     uint32_t mask[2];
756     uint32_t wumask;
757     uint32_t ints[2];
758     uint32_t debounce;
759     uint8_t delay;
760 };
761
762 static inline void omap_gpio_module_int_update(struct omap2_gpio_s *s,
763                 int line)
764 {
765     qemu_set_irq(s->irq[line], s->ints[line] & s->mask[line]);
766 }
767
768 static void omap_gpio_module_wake(struct omap2_gpio_s *s, int line)
769 {
770     if (!(s->config[0] & (1 << 2)))                     /* ENAWAKEUP */
771         return;
772     if (!(s->config[0] & (3 << 3)))                     /* Force Idle */
773         return;
774     if (!(s->wumask & (1 << line)))
775         return;
776
777     qemu_irq_raise(s->wkup);
778 }
779
780 static inline void omap_gpio_module_out_update(struct omap2_gpio_s *s,
781                 uint32_t diff)
782 {
783     int ln;
784
785     s->outputs ^= diff;
786     diff &= ~s->dir;
787     while ((ln = ffs(diff))) {
788         ln --;
789         qemu_set_irq(s->handler[ln], (s->outputs >> ln) & 1);
790         diff &= ~(1 << ln);
791     }
792 }
793
794 static void omap_gpio_module_level_update(struct omap2_gpio_s *s, int line)
795 {
796     s->ints[line] |= s->dir &
797             ((s->inputs & s->level[1]) | (~s->inputs & s->level[0]));
798     omap_gpio_module_int_update(s, line);
799 }
800
801 static inline void omap_gpio_module_int(struct omap2_gpio_s *s, int line)
802 {
803     s->ints[0] |= 1 << line;
804     omap_gpio_module_int_update(s, 0);
805     s->ints[1] |= 1 << line;
806     omap_gpio_module_int_update(s, 1);
807     omap_gpio_module_wake(s, line);
808 }
809
810 static void omap_gpio_module_set(void *opaque, int line, int level)
811 {
812     struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
813
814     if (level) {
815         if (s->dir & (1 << line) & ((~s->inputs & s->edge[0]) | s->level[1]))
816             omap_gpio_module_int(s, line);
817         s->inputs |= 1 << line;
818     } else {
819         if (s->dir & (1 << line) & ((s->inputs & s->edge[1]) | s->level[0]))
820             omap_gpio_module_int(s, line);
821         s->inputs &= ~(1 << line);
822     }
823 }
824
825 static void omap_gpio_module_reset(struct omap2_gpio_s *s)
826 {
827     s->config[0] = 0;
828     s->config[1] = 2;
829     s->ints[0] = 0;
830     s->ints[1] = 0;
831     s->mask[0] = 0;
832     s->mask[1] = 0;
833     s->wumask = 0;
834     s->dir = ~0;
835     s->level[0] = 0;
836     s->level[1] = 0;
837     s->edge[0] = 0;
838     s->edge[1] = 0;
839     s->debounce = 0;
840     s->delay = 0;
841 }
842
843 static uint32_t omap_gpio_module_read(void *opaque, target_phys_addr_t addr)
844 {
845     struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
846
847     switch (addr) {
848     case 0x00:  /* GPIO_REVISION */
849         return s->revision;
850
851     case 0x10:  /* GPIO_SYSCONFIG */
852         return s->config[0];
853
854     case 0x14:  /* GPIO_SYSSTATUS */
855         return 0x01;
856
857     case 0x18:  /* GPIO_IRQSTATUS1 */
858         return s->ints[0];
859
860     case 0x1c:  /* GPIO_IRQENABLE1 */
861     case 0x60:  /* GPIO_CLEARIRQENABLE1 */
862     case 0x64:  /* GPIO_SETIRQENABLE1 */
863         return s->mask[0];
864
865     case 0x20:  /* GPIO_WAKEUPENABLE */
866     case 0x80:  /* GPIO_CLEARWKUENA */
867     case 0x84:  /* GPIO_SETWKUENA */
868         return s->wumask;
869
870     case 0x28:  /* GPIO_IRQSTATUS2 */
871         return s->ints[1];
872
873     case 0x2c:  /* GPIO_IRQENABLE2 */
874     case 0x70:  /* GPIO_CLEARIRQENABLE2 */
875     case 0x74:  /* GPIO_SETIREQNEABLE2 */
876         return s->mask[1];
877
878     case 0x30:  /* GPIO_CTRL */
879         return s->config[1];
880
881     case 0x34:  /* GPIO_OE */
882         return s->dir;
883
884     case 0x38:  /* GPIO_DATAIN */
885         return s->inputs;
886
887     case 0x3c:  /* GPIO_DATAOUT */
888     case 0x90:  /* GPIO_CLEARDATAOUT */
889     case 0x94:  /* GPIO_SETDATAOUT */
890         return s->outputs;
891
892     case 0x40:  /* GPIO_LEVELDETECT0 */
893         return s->level[0];
894
895     case 0x44:  /* GPIO_LEVELDETECT1 */
896         return s->level[1];
897
898     case 0x48:  /* GPIO_RISINGDETECT */
899         return s->edge[0];
900
901     case 0x4c:  /* GPIO_FALLINGDETECT */
902         return s->edge[1];
903
904     case 0x50:  /* GPIO_DEBOUNCENABLE */
905         return s->debounce;
906
907     case 0x54:  /* GPIO_DEBOUNCINGTIME */
908         return s->delay;
909     }
910
911     OMAP_BAD_REG(addr);
912     return 0;
913 }
914
915 static void omap_gpio_module_write(void *opaque, target_phys_addr_t addr,
916                 uint32_t value)
917 {
918     struct omap2_gpio_s *s = (struct omap2_gpio_s *) opaque;
919     uint32_t diff;
920     int ln;
921
922     switch (addr) {
923     case 0x00:  /* GPIO_REVISION */
924     case 0x14:  /* GPIO_SYSSTATUS */
925     case 0x38:  /* GPIO_DATAIN */
926         OMAP_RO_REGV(addr, value);
927         break;
928
929     case 0x10:  /* GPIO_SYSCONFIG */
930         if (((value >> 3) & 3) == 3)
931             fprintf(stderr, "%s: bad IDLEMODE value\n", __FUNCTION__);
932         if (value & 2)
933             omap_gpio_module_reset(s);
934         s->config[0] = value & 0x1d;
935         break;
936
937     case 0x18:  /* GPIO_IRQSTATUS1 */
938         if (s->ints[0] & value) {
939             s->ints[0] &= ~value;
940             omap_gpio_module_level_update(s, 0);
941         }
942         break;
943
944     case 0x1c:  /* GPIO_IRQENABLE1 */
945         s->mask[0] = value;
946         omap_gpio_module_int_update(s, 0);
947         break;
948
949     case 0x20:  /* GPIO_WAKEUPENABLE */
950         s->wumask = value;
951         break;
952
953     case 0x28:  /* GPIO_IRQSTATUS2 */
954         if (s->ints[1] & value) {
955             s->ints[1] &= ~value;
956             omap_gpio_module_level_update(s, 1);
957         }
958         break;
959
960     case 0x2c:  /* GPIO_IRQENABLE2 */
961         s->mask[1] = value;
962         omap_gpio_module_int_update(s, 1);
963         break;
964
965     case 0x30:  /* GPIO_CTRL */
966         s->config[1] = value & 7;
967         break;
968
969     case 0x34:  /* GPIO_OE */
970         diff = s->outputs & (s->dir ^ value);
971         s->dir = value;
972
973         value = s->outputs & ~s->dir;
974         while ((ln = ffs(diff))) {
975             diff &= ~(1 <<-- ln);
976             qemu_set_irq(s->handler[ln], (value >> ln) & 1);
977         }
978
979         omap_gpio_module_level_update(s, 0);
980         omap_gpio_module_level_update(s, 1);
981         break;
982
983     case 0x3c:  /* GPIO_DATAOUT */
984         omap_gpio_module_out_update(s, s->outputs ^ value);
985         break;
986
987     case 0x40:  /* GPIO_LEVELDETECT0 */
988         s->level[0] = value;
989         omap_gpio_module_level_update(s, 0);
990         omap_gpio_module_level_update(s, 1);
991         break;
992
993     case 0x44:  /* GPIO_LEVELDETECT1 */
994         s->level[1] = value;
995         omap_gpio_module_level_update(s, 0);
996         omap_gpio_module_level_update(s, 1);
997         break;
998
999     case 0x48:  /* GPIO_RISINGDETECT */
1000         s->edge[0] = value;
1001         break;
1002
1003     case 0x4c:  /* GPIO_FALLINGDETECT */
1004         s->edge[1] = value;
1005         break;
1006
1007     case 0x50:  /* GPIO_DEBOUNCENABLE */
1008         s->debounce = value;
1009         break;
1010
1011     case 0x54:  /* GPIO_DEBOUNCINGTIME */
1012         s->delay = value;
1013         break;
1014
1015     case 0x60:  /* GPIO_CLEARIRQENABLE1 */
1016         s->mask[0] &= ~value;
1017         omap_gpio_module_int_update(s, 0);
1018         break;
1019
1020     case 0x64:  /* GPIO_SETIRQENABLE1 */
1021         s->mask[0] |= value;
1022         omap_gpio_module_int_update(s, 0);
1023         break;
1024
1025     case 0x70:  /* GPIO_CLEARIRQENABLE2 */
1026         s->mask[1] &= ~value;
1027         omap_gpio_module_int_update(s, 1);
1028         break;
1029
1030     case 0x74:  /* GPIO_SETIREQNEABLE2 */
1031         s->mask[1] |= value;
1032         omap_gpio_module_int_update(s, 1);
1033         break;
1034
1035     case 0x80:  /* GPIO_CLEARWKUENA */
1036         s->wumask &= ~value;
1037         break;
1038
1039     case 0x84:  /* GPIO_SETWKUENA */
1040         s->wumask |= value;
1041         break;
1042
1043     case 0x90:  /* GPIO_CLEARDATAOUT */
1044         omap_gpio_module_out_update(s, s->outputs & value);
1045         break;
1046
1047     case 0x94:  /* GPIO_SETDATAOUT */
1048         omap_gpio_module_out_update(s, ~s->outputs & value);
1049         break;
1050
1051     default:
1052         OMAP_BAD_REGV(addr, value);
1053         return;
1054     }
1055 }
1056
1057 static uint32_t omap_gpio_module_readp(void *opaque, target_phys_addr_t addr)
1058 {
1059     return omap_gpio_module_readp(opaque, addr) >> ((addr & 3) << 3);
1060 }
1061
1062 static void omap_gpio_module_writep(void *opaque, target_phys_addr_t addr,
1063                 uint32_t value)
1064 {
1065     uint32_t cur = 0;
1066     uint32_t mask = 0xffff;
1067
1068     switch (addr & ~3) {
1069     case 0x00:  /* GPIO_REVISION */
1070     case 0x14:  /* GPIO_SYSSTATUS */
1071     case 0x38:  /* GPIO_DATAIN */
1072         OMAP_RO_REG(addr);
1073         break;
1074
1075     case 0x10:  /* GPIO_SYSCONFIG */
1076     case 0x1c:  /* GPIO_IRQENABLE1 */
1077     case 0x20:  /* GPIO_WAKEUPENABLE */
1078     case 0x2c:  /* GPIO_IRQENABLE2 */
1079     case 0x30:  /* GPIO_CTRL */
1080     case 0x34:  /* GPIO_OE */
1081     case 0x3c:  /* GPIO_DATAOUT */
1082     case 0x40:  /* GPIO_LEVELDETECT0 */
1083     case 0x44:  /* GPIO_LEVELDETECT1 */
1084     case 0x48:  /* GPIO_RISINGDETECT */
1085     case 0x4c:  /* GPIO_FALLINGDETECT */
1086     case 0x50:  /* GPIO_DEBOUNCENABLE */
1087     case 0x54:  /* GPIO_DEBOUNCINGTIME */
1088         cur = omap_gpio_module_read(opaque, addr & ~3) &
1089                 ~(mask << ((addr & 3) << 3));
1090
1091         /* Fall through.  */
1092     case 0x18:  /* GPIO_IRQSTATUS1 */
1093     case 0x28:  /* GPIO_IRQSTATUS2 */
1094     case 0x60:  /* GPIO_CLEARIRQENABLE1 */
1095     case 0x64:  /* GPIO_SETIRQENABLE1 */
1096     case 0x70:  /* GPIO_CLEARIRQENABLE2 */
1097     case 0x74:  /* GPIO_SETIREQNEABLE2 */
1098     case 0x80:  /* GPIO_CLEARWKUENA */
1099     case 0x84:  /* GPIO_SETWKUENA */
1100     case 0x90:  /* GPIO_CLEARDATAOUT */
1101     case 0x94:  /* GPIO_SETDATAOUT */
1102         value <<= (addr & 3) << 3;
1103         omap_gpio_module_write(opaque, addr, cur | value);
1104         break;
1105
1106     default:
1107         OMAP_BAD_REG(addr);
1108         return;
1109     }
1110 }
1111
1112 static CPUReadMemoryFunc *omap_gpio_module_readfn[] = {
1113     omap_gpio_module_readp,
1114     omap_gpio_module_readp,
1115     omap_gpio_module_read,
1116 };
1117
1118 static CPUWriteMemoryFunc *omap_gpio_module_writefn[] = {
1119     omap_gpio_module_writep,
1120     omap_gpio_module_writep,
1121     omap_gpio_module_write,
1122 };
1123
1124 static void omap_gpio_module_init(struct omap_mpu_state_s *mpu,
1125                 struct omap2_gpio_s *s,
1126                 struct omap_target_agent_s *ta, int region,
1127                 qemu_irq mpu_irq, qemu_irq dsp_irq, qemu_irq wkup_irq,
1128                 omap_clk fclk, omap_clk iclk)
1129 {
1130     int iomemtype;
1131
1132     s->revision = cpu_class_omap3(mpu) ? 0x25 : 0x18;
1133     s->irq[0] = mpu_irq;
1134     s->irq[1] = dsp_irq;
1135     s->wkup = wkup_irq;
1136     s->in = qemu_allocate_irqs(omap_gpio_module_set, s, 32);
1137
1138     iomemtype = l4_register_io_memory(0, omap_gpio_module_readfn,
1139                     omap_gpio_module_writefn, s);
1140     omap_l4_attach(ta, region, iomemtype);
1141 }
1142
1143 struct omap_gpif_s {
1144     struct omap2_gpio_s module[6];
1145     int modules;
1146
1147     int autoidle;
1148     int gpo;
1149 };
1150
1151 static void omap_gpif_save_state(QEMUFile *f, void *opaque)
1152 {
1153     struct omap_gpif_s *s = (struct omap_gpif_s *)opaque;
1154     int i, j;
1155     
1156     qemu_put_sbe32(f, s->autoidle);
1157     qemu_put_sbe32(f, s->gpo);
1158     qemu_put_sbe32(f, s->modules);
1159     for (i = 0; i < s->modules; i++) {
1160         for (j = 0; j < 2; j++) {
1161             qemu_put_byte(f, s->module[i].config[j]);
1162             qemu_put_be32(f, s->module[i].level[j]);
1163             qemu_put_be32(f, s->module[i].edge[j]);
1164             qemu_put_be32(f, s->module[i].mask[j]);
1165             qemu_put_be32(f, s->module[i].ints[j]);
1166         }
1167         qemu_put_be32(f, s->module[i].inputs);
1168         qemu_put_be32(f, s->module[i].outputs);
1169         qemu_put_be32(f, s->module[i].dir);
1170         qemu_put_be32(f, s->module[i].wumask);
1171         qemu_put_be32(f, s->module[i].debounce);
1172         qemu_put_byte(f, s->module[i].delay);
1173     }
1174 }
1175
1176 static int omap_gpif_load_state(QEMUFile *f, void *opaque, int version_id)
1177 {
1178     struct omap_gpif_s *s = (struct omap_gpif_s *)opaque;
1179     int i, j;
1180
1181     if (version_id)
1182         return -EINVAL;
1183     
1184     s->autoidle = qemu_get_sbe32(f);
1185     s->gpo = qemu_get_sbe32(f);
1186     if (qemu_get_sbe32(f) != s->modules)
1187         return -EINVAL;
1188     for (i = 0; i < s->modules; i++) {
1189         for (j = 0; j < 2; j++) {
1190             s->module[i].config[j] = qemu_get_byte(f);
1191             s->module[i].level[j] = qemu_get_be32(f);
1192             s->module[i].edge[j] = qemu_get_be32(f);
1193             s->module[i].mask[j] = qemu_get_be32(f);
1194             s->module[i].ints[j] = qemu_get_be32(f);
1195         }
1196         s->module[i].inputs = qemu_get_be32(f);
1197         s->module[i].outputs = qemu_get_be32(f);
1198         s->module[i].dir = qemu_get_be32(f);
1199         s->module[i].wumask = qemu_get_be32(f);
1200         s->module[i].debounce = qemu_get_be32(f);
1201         s->module[i].delay = qemu_get_byte(f);
1202
1203         omap_gpio_module_level_update(&s->module[i], 0);
1204         omap_gpio_module_level_update(&s->module[i], 1);
1205     }
1206     
1207     return 0;
1208 }
1209
1210 static void omap_gpif_reset(struct omap_gpif_s *s)
1211 {
1212     int i;
1213
1214     for (i = 0; i < s->modules; i ++)
1215         omap_gpio_module_reset(s->module + i);
1216
1217     s->autoidle = 0;
1218     s->gpo = 0;
1219 }
1220
1221 static uint32_t omap_gpif_top_read(void *opaque, target_phys_addr_t addr)
1222 {
1223     struct omap_gpif_s *s = (struct omap_gpif_s *) opaque;
1224
1225     switch (addr) {
1226     case 0x00:  /* IPGENERICOCPSPL_REVISION */
1227         return 0x18;
1228
1229     case 0x10:  /* IPGENERICOCPSPL_SYSCONFIG */
1230         return s->autoidle;
1231
1232     case 0x14:  /* IPGENERICOCPSPL_SYSSTATUS */
1233         return 0x01;
1234
1235     case 0x18:  /* IPGENERICOCPSPL_IRQSTATUS */
1236         return 0x00;
1237
1238     case 0x40:  /* IPGENERICOCPSPL_GPO */
1239         return s->gpo;
1240
1241     case 0x50:  /* IPGENERICOCPSPL_GPI */
1242         return 0x00;
1243     }
1244
1245     OMAP_BAD_REG(addr);
1246     return 0;
1247 }
1248
1249 static void omap_gpif_top_write(void *opaque, target_phys_addr_t addr,
1250                 uint32_t value)
1251 {
1252     struct omap_gpif_s *s = (struct omap_gpif_s *) opaque;
1253
1254     switch (addr) {
1255     case 0x00:  /* IPGENERICOCPSPL_REVISION */
1256     case 0x14:  /* IPGENERICOCPSPL_SYSSTATUS */
1257     case 0x18:  /* IPGENERICOCPSPL_IRQSTATUS */
1258     case 0x50:  /* IPGENERICOCPSPL_GPI */
1259         OMAP_RO_REG(addr);
1260         break;
1261
1262     case 0x10:  /* IPGENERICOCPSPL_SYSCONFIG */
1263         if (value & (1 << 1))                                   /* SOFTRESET */
1264             omap_gpif_reset(s);
1265         s->autoidle = value & 1;
1266         break;
1267
1268     case 0x40:  /* IPGENERICOCPSPL_GPO */
1269         s->gpo = value & 1;
1270         break;
1271
1272     default:
1273         OMAP_BAD_REG(addr);
1274         return;
1275     }
1276 }
1277
1278 static CPUReadMemoryFunc *omap_gpif_top_readfn[] = {
1279     omap_gpif_top_read,
1280     omap_gpif_top_read,
1281     omap_gpif_top_read,
1282 };
1283
1284 static CPUWriteMemoryFunc *omap_gpif_top_writefn[] = {
1285     omap_gpif_top_write,
1286     omap_gpif_top_write,
1287     omap_gpif_top_write,
1288 };
1289
1290 struct omap_gpif_s *omap2_gpio_init(struct omap_mpu_state_s *mpu,
1291                 struct omap_target_agent_s *ta,
1292                 qemu_irq *irq, omap_clk *fclk, omap_clk iclk, int modules)
1293 {
1294     int iomemtype, i;
1295     struct omap_gpif_s *s = (struct omap_gpif_s *)
1296             qemu_mallocz(sizeof(struct omap_gpif_s));
1297     int region[4] = { 0, 2, 4, 5 };
1298
1299     s->modules = modules;
1300     for (i = 0; i < modules; i ++)
1301         omap_gpio_module_init(mpu, s->module + i, ta, region[i],
1302                         irq[i], 0, 0, fclk[i], iclk);
1303
1304     omap_gpif_reset(s);
1305
1306     iomemtype = l4_register_io_memory(0, omap_gpif_top_readfn,
1307                     omap_gpif_top_writefn, s);
1308     omap_l4_attach(ta, 1, iomemtype);
1309
1310     return s;
1311 }
1312
1313 struct omap_gpif_s *omap3_gpif_init()
1314 {
1315     struct omap_gpif_s *s = (struct omap_gpif_s *)
1316         qemu_mallocz(sizeof(struct omap_gpif_s));
1317     omap_gpif_reset(s);
1318     
1319     register_savevm("omap_gpif", -1, 0,
1320                     omap_gpif_save_state, omap_gpif_load_state, s);
1321     return s;
1322 }
1323
1324 void omap3_gpio_init(struct omap_mpu_state_s *mpu,
1325                      struct omap_gpif_s *s,struct omap_target_agent_s *ta,
1326                      qemu_irq irq, omap_clk *fclk, omap_clk iclk, int module_index)
1327 {
1328     s->modules++;
1329     omap_gpio_module_init(mpu, s->module + module_index, ta, 0,
1330                           irq, 0, 0, NULL,NULL);
1331 }
1332
1333 qemu_irq *omap2_gpio_in_get(struct omap_gpif_s *s, int start)
1334 {
1335     if (start >= s->modules * 32 || start < 0)
1336         hw_error("%s: No GPIO line %i\n", __FUNCTION__, start);
1337     return s->module[start >> 5].in + (start & 31);
1338 }
1339
1340 void omap2_gpio_out_set(struct omap_gpif_s *s, int line, qemu_irq handler)
1341 {
1342     if (line >= s->modules * 32 || line < 0)
1343         hw_error("%s: No GPIO line %i\n", __FUNCTION__, line);
1344     s->module[line >> 5].handler[line & 31] = handler;
1345 }
1346
1347 /* Enhanced Audio Controller (CODEC only) */
1348 struct omap_eac_s {
1349     qemu_irq irq;
1350
1351     uint16_t sysconfig;
1352     uint8_t config[4];
1353     uint8_t control;
1354     uint8_t address;
1355     uint16_t data;
1356     uint8_t vtol;
1357     uint8_t vtsl;
1358     uint16_t mixer;
1359     uint16_t gain[4];
1360     uint8_t att;
1361     uint16_t max[7];
1362
1363     struct {
1364         qemu_irq txdrq;
1365         qemu_irq rxdrq;
1366         uint32_t (*txrx)(void *opaque, uint32_t, int);
1367         void *opaque;
1368
1369 #define EAC_BUF_LEN 1024
1370         uint32_t rxbuf[EAC_BUF_LEN];
1371         int rxoff;
1372         int rxlen;
1373         int rxavail;
1374         uint32_t txbuf[EAC_BUF_LEN];
1375         int txlen;
1376         int txavail;
1377
1378         int enable;
1379         int rate;
1380
1381         uint16_t config[4];
1382
1383         /* These need to be moved to the actual codec */
1384         QEMUSoundCard card;
1385         SWVoiceIn *in_voice;
1386         SWVoiceOut *out_voice;
1387         int hw_enable;
1388     } codec;
1389
1390     struct {
1391         uint8_t control;
1392         uint16_t config;
1393     } modem, bt;
1394 };
1395
1396 static inline void omap_eac_interrupt_update(struct omap_eac_s *s)
1397 {
1398     qemu_set_irq(s->irq, (s->codec.config[1] >> 14) & 1);       /* AURDI */
1399 }
1400
1401 static inline void omap_eac_in_dmarequest_update(struct omap_eac_s *s)
1402 {
1403     qemu_set_irq(s->codec.rxdrq, (s->codec.rxavail || s->codec.rxlen) &&
1404                     ((s->codec.config[1] >> 12) & 1));          /* DMAREN */
1405 }
1406
1407 static inline void omap_eac_out_dmarequest_update(struct omap_eac_s *s)
1408 {
1409     qemu_set_irq(s->codec.txdrq, s->codec.txlen < s->codec.txavail &&
1410                     ((s->codec.config[1] >> 11) & 1));          /* DMAWEN */
1411 }
1412
1413 static inline void omap_eac_in_refill(struct omap_eac_s *s)
1414 {
1415     int left = MIN(EAC_BUF_LEN - s->codec.rxlen, s->codec.rxavail) << 2;
1416     int start = ((s->codec.rxoff + s->codec.rxlen) & (EAC_BUF_LEN - 1)) << 2;
1417     int leftwrap = MIN(left, (EAC_BUF_LEN << 2) - start);
1418     int recv = 1;
1419     uint8_t *buf = (uint8_t *) s->codec.rxbuf + start;
1420
1421     left -= leftwrap;
1422     start = 0;
1423     while (leftwrap && (recv = AUD_read(s->codec.in_voice, buf + start,
1424                                     leftwrap)) > 0) {   /* Be defensive */
1425         start += recv;
1426         leftwrap -= recv;
1427     }
1428     if (recv <= 0)
1429         s->codec.rxavail = 0;
1430     else
1431         s->codec.rxavail -= start >> 2;
1432     s->codec.rxlen += start >> 2;
1433
1434     if (recv > 0 && left > 0) {
1435         start = 0;
1436         while (left && (recv = AUD_read(s->codec.in_voice,
1437                                         (uint8_t *) s->codec.rxbuf + start,
1438                                         left)) > 0) {   /* Be defensive */
1439             start += recv;
1440             left -= recv;
1441         }
1442         if (recv <= 0)
1443             s->codec.rxavail = 0;
1444         else
1445             s->codec.rxavail -= start >> 2;
1446         s->codec.rxlen += start >> 2;
1447     }
1448 }
1449
1450 static inline void omap_eac_out_empty(struct omap_eac_s *s)
1451 {
1452     int left = s->codec.txlen << 2;
1453     int start = 0;
1454     int sent = 1;
1455
1456     while (left && (sent = AUD_write(s->codec.out_voice,
1457                                     (uint8_t *) s->codec.txbuf + start,
1458                                     left)) > 0) {       /* Be defensive */
1459         start += sent;
1460         left -= sent;
1461     }
1462
1463     if (!sent) {
1464         s->codec.txavail = 0;
1465         omap_eac_out_dmarequest_update(s);
1466     }
1467
1468     if (start)
1469         s->codec.txlen = 0;
1470 }
1471
1472 static void omap_eac_in_cb(void *opaque, int avail_b)
1473 {
1474     struct omap_eac_s *s = (struct omap_eac_s *) opaque;
1475
1476     s->codec.rxavail = avail_b >> 2;
1477     omap_eac_in_refill(s);
1478     /* TODO: possibly discard current buffer if overrun */
1479     omap_eac_in_dmarequest_update(s);
1480 }
1481
1482 static void omap_eac_out_cb(void *opaque, int free_b)
1483 {
1484     struct omap_eac_s *s = (struct omap_eac_s *) opaque;
1485
1486     s->codec.txavail = free_b >> 2;
1487     if (s->codec.txlen)
1488         omap_eac_out_empty(s);
1489     else
1490         omap_eac_out_dmarequest_update(s);
1491 }
1492
1493 static void omap_eac_enable_update(struct omap_eac_s *s)
1494 {
1495     s->codec.enable = !(s->codec.config[1] & 1) &&              /* EACPWD */
1496             (s->codec.config[1] & 2) &&                         /* AUDEN */
1497             s->codec.hw_enable;
1498 }
1499
1500 static const int omap_eac_fsint[4] = {
1501     8000,
1502     11025,
1503     22050,
1504     44100,
1505 };
1506
1507 static const int omap_eac_fsint2[8] = {
1508     8000,
1509     11025,
1510     22050,
1511     44100,
1512     48000,
1513     0, 0, 0,
1514 };
1515
1516 static const int omap_eac_fsint3[16] = {
1517     8000,
1518     11025,
1519     16000,
1520     22050,
1521     24000,
1522     32000,
1523     44100,
1524     48000,
1525     0, 0, 0, 0, 0, 0, 0, 0,
1526 };
1527
1528 static void omap_eac_rate_update(struct omap_eac_s *s)
1529 {
1530     int fsint[3];
1531
1532     fsint[2] = (s->codec.config[3] >> 9) & 0xf;
1533     fsint[1] = (s->codec.config[2] >> 0) & 0x7;
1534     fsint[0] = (s->codec.config[0] >> 6) & 0x3;
1535     if (fsint[2] < 0xf)
1536         s->codec.rate = omap_eac_fsint3[fsint[2]];
1537     else if (fsint[1] < 0x7)
1538         s->codec.rate = omap_eac_fsint2[fsint[1]];
1539     else
1540         s->codec.rate = omap_eac_fsint[fsint[0]];
1541 }
1542
1543 static void omap_eac_volume_update(struct omap_eac_s *s)
1544 {
1545     /* TODO */
1546 }
1547
1548 static void omap_eac_format_update(struct omap_eac_s *s)
1549 {
1550     struct audsettings fmt;
1551
1552     /* The hardware buffers at most one sample */
1553     if (s->codec.rxlen)
1554         s->codec.rxlen = 1;
1555
1556     if (s->codec.in_voice) {
1557         AUD_set_active_in(s->codec.in_voice, 0);
1558         AUD_close_in(&s->codec.card, s->codec.in_voice);
1559         s->codec.in_voice = 0;
1560     }
1561     if (s->codec.out_voice) {
1562         omap_eac_out_empty(s);
1563         AUD_set_active_out(s->codec.out_voice, 0);
1564         AUD_close_out(&s->codec.card, s->codec.out_voice);
1565         s->codec.out_voice = 0;
1566         s->codec.txavail = 0;
1567     }
1568     /* Discard what couldn't be written */
1569     s->codec.txlen = 0;
1570
1571     omap_eac_enable_update(s);
1572     if (!s->codec.enable)
1573         return;
1574
1575     omap_eac_rate_update(s);
1576     fmt.endianness = ((s->codec.config[0] >> 8) & 1);           /* LI_BI */
1577     fmt.nchannels = ((s->codec.config[0] >> 10) & 1) ? 2 : 1;   /* MN_ST */
1578     fmt.freq = s->codec.rate;
1579     /* TODO: signedness possibly depends on the CODEC hardware - or
1580      * does I2S specify it?  */
1581     /* All register writes are 16 bits so we we store 16-bit samples
1582      * in the buffers regardless of AGCFR[B8_16] value.  */
1583     fmt.fmt = AUD_FMT_U16;
1584
1585     s->codec.in_voice = AUD_open_in(&s->codec.card, s->codec.in_voice,
1586                     "eac.codec.in", s, omap_eac_in_cb, &fmt);
1587     s->codec.out_voice = AUD_open_out(&s->codec.card, s->codec.out_voice,
1588                     "eac.codec.out", s, omap_eac_out_cb, &fmt);
1589
1590     omap_eac_volume_update(s);
1591
1592     AUD_set_active_in(s->codec.in_voice, 1);
1593     AUD_set_active_out(s->codec.out_voice, 1);
1594 }
1595
1596 static void omap_eac_reset(struct omap_eac_s *s)
1597 {
1598     s->sysconfig = 0;
1599     s->config[0] = 0x0c;
1600     s->config[1] = 0x09;
1601     s->config[2] = 0xab;
1602     s->config[3] = 0x03;
1603     s->control = 0x00;
1604     s->address = 0x00;
1605     s->data = 0x0000;
1606     s->vtol = 0x00;
1607     s->vtsl = 0x00;
1608     s->mixer = 0x0000;
1609     s->gain[0] = 0xe7e7;
1610     s->gain[1] = 0x6767;
1611     s->gain[2] = 0x6767;
1612     s->gain[3] = 0x6767;
1613     s->att = 0xce;
1614     s->max[0] = 0;
1615     s->max[1] = 0;
1616     s->max[2] = 0;
1617     s->max[3] = 0;
1618     s->max[4] = 0;
1619     s->max[5] = 0;
1620     s->max[6] = 0;
1621
1622     s->modem.control = 0x00;
1623     s->modem.config = 0x0000;
1624     s->bt.control = 0x00;
1625     s->bt.config = 0x0000;
1626     s->codec.config[0] = 0x0649;
1627     s->codec.config[1] = 0x0000;
1628     s->codec.config[2] = 0x0007;
1629     s->codec.config[3] = 0x1ffc;
1630     s->codec.rxoff = 0;
1631     s->codec.rxlen = 0;
1632     s->codec.txlen = 0;
1633     s->codec.rxavail = 0;
1634     s->codec.txavail = 0;
1635
1636     omap_eac_format_update(s);
1637     omap_eac_interrupt_update(s);
1638 }
1639
1640 static uint32_t omap_eac_read(void *opaque, target_phys_addr_t addr)
1641 {
1642     struct omap_eac_s *s = (struct omap_eac_s *) opaque;
1643     uint32_t ret;
1644
1645     switch (addr) {
1646     case 0x000: /* CPCFR1 */
1647         return s->config[0];
1648     case 0x004: /* CPCFR2 */
1649         return s->config[1];
1650     case 0x008: /* CPCFR3 */
1651         return s->config[2];
1652     case 0x00c: /* CPCFR4 */
1653         return s->config[3];
1654
1655     case 0x010: /* CPTCTL */
1656         return s->control | ((s->codec.rxavail + s->codec.rxlen > 0) << 7) |
1657                 ((s->codec.txlen < s->codec.txavail) << 5);
1658
1659     case 0x014: /* CPTTADR */
1660         return s->address;
1661     case 0x018: /* CPTDATL */
1662         return s->data & 0xff;
1663     case 0x01c: /* CPTDATH */
1664         return s->data >> 8;
1665     case 0x020: /* CPTVSLL */
1666         return s->vtol;
1667     case 0x024: /* CPTVSLH */
1668         return s->vtsl | (3 << 5);      /* CRDY1 | CRDY2 */
1669     case 0x040: /* MPCTR */
1670         return s->modem.control;
1671     case 0x044: /* MPMCCFR */
1672         return s->modem.config;
1673     case 0x060: /* BPCTR */
1674         return s->bt.control;
1675     case 0x064: /* BPMCCFR */
1676         return s->bt.config;
1677     case 0x080: /* AMSCFR */
1678         return s->mixer;
1679     case 0x084: /* AMVCTR */
1680         return s->gain[0];
1681     case 0x088: /* AM1VCTR */
1682         return s->gain[1];
1683     case 0x08c: /* AM2VCTR */
1684         return s->gain[2];
1685     case 0x090: /* AM3VCTR */
1686         return s->gain[3];
1687     case 0x094: /* ASTCTR */
1688         return s->att;
1689     case 0x098: /* APD1LCR */
1690         return s->max[0];
1691     case 0x09c: /* APD1RCR */
1692         return s->max[1];
1693     case 0x0a0: /* APD2LCR */
1694         return s->max[2];
1695     case 0x0a4: /* APD2RCR */
1696         return s->max[3];
1697     case 0x0a8: /* APD3LCR */
1698         return s->max[4];
1699     case 0x0ac: /* APD3RCR */
1700         return s->max[5];
1701     case 0x0b0: /* APD4R */
1702         return s->max[6];
1703     case 0x0b4: /* ADWR */
1704         /* This should be write-only?  Docs list it as read-only.  */
1705         return 0x0000;
1706     case 0x0b8: /* ADRDR */
1707         if (likely(s->codec.rxlen > 1)) {
1708             ret = s->codec.rxbuf[s->codec.rxoff ++];
1709             s->codec.rxlen --;
1710             s->codec.rxoff &= EAC_BUF_LEN - 1;
1711             return ret;
1712         } else if (s->codec.rxlen) {
1713             ret = s->codec.rxbuf[s->codec.rxoff ++];
1714             s->codec.rxlen --;
1715             s->codec.rxoff &= EAC_BUF_LEN - 1;
1716             if (s->codec.rxavail)
1717                 omap_eac_in_refill(s);
1718             omap_eac_in_dmarequest_update(s);
1719             return ret;
1720         }
1721         return 0x0000;
1722     case 0x0bc: /* AGCFR */
1723         return s->codec.config[0];
1724     case 0x0c0: /* AGCTR */
1725         return s->codec.config[1] | ((s->codec.config[1] & 2) << 14);
1726     case 0x0c4: /* AGCFR2 */
1727         return s->codec.config[2];
1728     case 0x0c8: /* AGCFR3 */
1729         return s->codec.config[3];
1730     case 0x0cc: /* MBPDMACTR */
1731     case 0x0d0: /* MPDDMARR */
1732     case 0x0d8: /* MPUDMARR */
1733     case 0x0e4: /* BPDDMARR */
1734     case 0x0ec: /* BPUDMARR */
1735         return 0x0000;
1736
1737     case 0x100: /* VERSION_NUMBER */
1738         return 0x0010;
1739
1740     case 0x104: /* SYSCONFIG */
1741         return s->sysconfig;
1742
1743     case 0x108: /* SYSSTATUS */
1744         return 1 | 0xe;                                 /* RESETDONE | stuff */
1745     }
1746
1747     OMAP_BAD_REG(addr);
1748     return 0;
1749 }
1750
1751 static void omap_eac_write(void *opaque, target_phys_addr_t addr,
1752                 uint32_t value)
1753 {
1754     struct omap_eac_s *s = (struct omap_eac_s *) opaque;
1755
1756     switch (addr) {
1757     case 0x098: /* APD1LCR */
1758     case 0x09c: /* APD1RCR */
1759     case 0x0a0: /* APD2LCR */
1760     case 0x0a4: /* APD2RCR */
1761     case 0x0a8: /* APD3LCR */
1762     case 0x0ac: /* APD3RCR */
1763     case 0x0b0: /* APD4R */
1764     case 0x0b8: /* ADRDR */
1765     case 0x0d0: /* MPDDMARR */
1766     case 0x0d8: /* MPUDMARR */
1767     case 0x0e4: /* BPDDMARR */
1768     case 0x0ec: /* BPUDMARR */
1769     case 0x100: /* VERSION_NUMBER */
1770     case 0x108: /* SYSSTATUS */
1771         OMAP_RO_REG(addr);
1772         return;
1773
1774     case 0x000: /* CPCFR1 */
1775         s->config[0] = value & 0xff;
1776         omap_eac_format_update(s);
1777         break;
1778     case 0x004: /* CPCFR2 */
1779         s->config[1] = value & 0xff;
1780         omap_eac_format_update(s);
1781         break;
1782     case 0x008: /* CPCFR3 */
1783         s->config[2] = value & 0xff;
1784         omap_eac_format_update(s);
1785         break;
1786     case 0x00c: /* CPCFR4 */
1787         s->config[3] = value & 0xff;
1788         omap_eac_format_update(s);
1789         break;
1790
1791     case 0x010: /* CPTCTL */
1792         /* Assuming TXF and TXE bits are read-only... */
1793         s->control = value & 0x5f;
1794         omap_eac_interrupt_update(s);
1795         break;
1796
1797     case 0x014: /* CPTTADR */
1798         s->address = value & 0xff;
1799         break;
1800     case 0x018: /* CPTDATL */
1801         s->data &= 0xff00;
1802         s->data |= value & 0xff;
1803         break;
1804     case 0x01c: /* CPTDATH */
1805         s->data &= 0x00ff;
1806         s->data |= value << 8;
1807         break;
1808     case 0x020: /* CPTVSLL */
1809         s->vtol = value & 0xf8;
1810         break;
1811     case 0x024: /* CPTVSLH */
1812         s->vtsl = value & 0x9f;
1813         break;
1814     case 0x040: /* MPCTR */
1815         s->modem.control = value & 0x8f;
1816         break;
1817     case 0x044: /* MPMCCFR */
1818         s->modem.config = value & 0x7fff;
1819         break;
1820     case 0x060: /* BPCTR */
1821         s->bt.control = value & 0x8f;
1822         break;
1823     case 0x064: /* BPMCCFR */
1824         s->bt.config = value & 0x7fff;
1825         break;
1826     case 0x080: /* AMSCFR */
1827         s->mixer = value & 0x0fff;
1828         break;
1829     case 0x084: /* AMVCTR */
1830         s->gain[0] = value & 0xffff;
1831         break;
1832     case 0x088: /* AM1VCTR */
1833         s->gain[1] = value & 0xff7f;
1834         break;
1835     case 0x08c: /* AM2VCTR */
1836         s->gain[2] = value & 0xff7f;
1837         break;
1838     case 0x090: /* AM3VCTR */
1839         s->gain[3] = value & 0xff7f;
1840         break;
1841     case 0x094: /* ASTCTR */
1842         s->att = value & 0xff;
1843         break;
1844
1845     case 0x0b4: /* ADWR */
1846         s->codec.txbuf[s->codec.txlen ++] = value;
1847         if (unlikely(s->codec.txlen == EAC_BUF_LEN ||
1848                                 s->codec.txlen == s->codec.txavail)) {
1849             if (s->codec.txavail)
1850                 omap_eac_out_empty(s);
1851             /* Discard what couldn't be written */
1852             s->codec.txlen = 0;
1853         }
1854         break;
1855
1856     case 0x0bc: /* AGCFR */
1857         s->codec.config[0] = value & 0x07ff;
1858         omap_eac_format_update(s);
1859         break;
1860     case 0x0c0: /* AGCTR */
1861         s->codec.config[1] = value & 0x780f;
1862         omap_eac_format_update(s);
1863         break;
1864     case 0x0c4: /* AGCFR2 */
1865         s->codec.config[2] = value & 0x003f;
1866         omap_eac_format_update(s);
1867         break;
1868     case 0x0c8: /* AGCFR3 */
1869         s->codec.config[3] = value & 0xffff;
1870         omap_eac_format_update(s);
1871         break;
1872     case 0x0cc: /* MBPDMACTR */
1873     case 0x0d4: /* MPDDMAWR */
1874     case 0x0e0: /* MPUDMAWR */
1875     case 0x0e8: /* BPDDMAWR */
1876     case 0x0f0: /* BPUDMAWR */
1877         break;
1878
1879     case 0x104: /* SYSCONFIG */
1880         if (value & (1 << 1))                           /* SOFTRESET */
1881             omap_eac_reset(s);
1882         s->sysconfig = value & 0x31d;
1883         break;
1884
1885     default:
1886         OMAP_BAD_REG(addr);
1887         return;
1888     }
1889 }
1890
1891 static CPUReadMemoryFunc *omap_eac_readfn[] = {
1892     omap_badwidth_read16,
1893     omap_eac_read,
1894     omap_badwidth_read16,
1895 };
1896
1897 static CPUWriteMemoryFunc *omap_eac_writefn[] = {
1898     omap_badwidth_write16,
1899     omap_eac_write,
1900     omap_badwidth_write16,
1901 };
1902
1903 struct omap_eac_s *omap_eac_init(struct omap_target_agent_s *ta,
1904                 qemu_irq irq, qemu_irq *drq, omap_clk fclk, omap_clk iclk)
1905 {
1906     int iomemtype;
1907     struct omap_eac_s *s = (struct omap_eac_s *)
1908             qemu_mallocz(sizeof(struct omap_eac_s));
1909
1910     s->irq = irq;
1911     s->codec.rxdrq = *drq ++;
1912     s->codec.txdrq = *drq ++;
1913     omap_eac_reset(s);
1914
1915 #ifdef HAS_AUDIO
1916     AUD_register_card("OMAP EAC", &s->codec.card);
1917
1918     iomemtype = cpu_register_io_memory(0, omap_eac_readfn,
1919                     omap_eac_writefn, s);
1920     omap_l4_attach(ta, 0, iomemtype);
1921 #endif
1922
1923     return s;
1924 }
1925
1926 /* STI/XTI (emulation interface) console - reverse engineered only */
1927 struct omap_sti_s {
1928     qemu_irq irq;
1929     CharDriverState *chr;
1930
1931     uint32_t sysconfig;
1932     uint32_t systest;
1933     uint32_t irqst;
1934     uint32_t irqen;
1935     uint32_t clkcontrol;
1936     uint32_t serial_config;
1937 };
1938
1939 #define STI_TRACE_CONSOLE_CHANNEL       239
1940 #define STI_TRACE_CONTROL_CHANNEL       253
1941
1942 static inline void omap_sti_interrupt_update(struct omap_sti_s *s)
1943 {
1944     qemu_set_irq(s->irq, s->irqst & s->irqen);
1945 }
1946
1947 static void omap_sti_reset(struct omap_sti_s *s)
1948 {
1949     s->sysconfig = 0;
1950     s->irqst = 0;
1951     s->irqen = 0;
1952     s->clkcontrol = 0;
1953     s->serial_config = 0;
1954
1955     omap_sti_interrupt_update(s);
1956 }
1957
1958 static uint32_t omap_sti_read(void *opaque, target_phys_addr_t addr)
1959 {
1960     struct omap_sti_s *s = (struct omap_sti_s *) opaque;
1961
1962     switch (addr) {
1963     case 0x00:  /* STI_REVISION */
1964         return 0x10;
1965
1966     case 0x10:  /* STI_SYSCONFIG */
1967         return s->sysconfig;
1968
1969     case 0x14:  /* STI_SYSSTATUS / STI_RX_STATUS / XTI_SYSSTATUS */
1970         return 0x00;
1971
1972     case 0x18:  /* STI_IRQSTATUS */
1973         return s->irqst;
1974
1975     case 0x1c:  /* STI_IRQSETEN / STI_IRQCLREN */
1976         return s->irqen;
1977
1978     case 0x24:  /* STI_ER / STI_DR / XTI_TRACESELECT */
1979     case 0x28:  /* STI_RX_DR / XTI_RXDATA */
1980         /* TODO */
1981         return 0;
1982
1983     case 0x2c:  /* STI_CLK_CTRL / XTI_SCLKCRTL */
1984         return s->clkcontrol;
1985
1986     case 0x30:  /* STI_SERIAL_CFG / XTI_SCONFIG */
1987         return s->serial_config;
1988     }
1989
1990     OMAP_BAD_REG(addr);
1991     return 0;
1992 }
1993
1994 static void omap_sti_write(void *opaque, target_phys_addr_t addr,
1995                 uint32_t value)
1996 {
1997     struct omap_sti_s *s = (struct omap_sti_s *) opaque;
1998
1999     switch (addr) {
2000     case 0x00:  /* STI_REVISION */
2001     case 0x14:  /* STI_SYSSTATUS / STI_RX_STATUS / XTI_SYSSTATUS */
2002         OMAP_RO_REG(addr);
2003         return;
2004
2005     case 0x10:  /* STI_SYSCONFIG */
2006         if (value & (1 << 1))                           /* SOFTRESET */
2007             omap_sti_reset(s);
2008         s->sysconfig = value & 0xfe;
2009         break;
2010
2011     case 0x18:  /* STI_IRQSTATUS */
2012         s->irqst &= ~value;
2013         omap_sti_interrupt_update(s);
2014         break;
2015
2016     case 0x1c:  /* STI_IRQSETEN / STI_IRQCLREN */
2017         s->irqen = value & 0xffff;
2018         omap_sti_interrupt_update(s);
2019         break;
2020
2021     case 0x2c:  /* STI_CLK_CTRL / XTI_SCLKCRTL */
2022         s->clkcontrol = value & 0xff;
2023         break;
2024
2025     case 0x30:  /* STI_SERIAL_CFG / XTI_SCONFIG */
2026         s->serial_config = value & 0xff;
2027         break;
2028
2029     case 0x24:  /* STI_ER / STI_DR / XTI_TRACESELECT */
2030     case 0x28:  /* STI_RX_DR / XTI_RXDATA */
2031         /* TODO */
2032         return;
2033
2034     default:
2035         OMAP_BAD_REG(addr);
2036         return;
2037     }
2038 }
2039
2040 static CPUReadMemoryFunc *omap_sti_readfn[] = {
2041     omap_badwidth_read32,
2042     omap_badwidth_read32,
2043     omap_sti_read,
2044 };
2045
2046 static CPUWriteMemoryFunc *omap_sti_writefn[] = {
2047     omap_badwidth_write32,
2048     omap_badwidth_write32,
2049     omap_sti_write,
2050 };
2051
2052 static uint32_t omap_sti_fifo_read(void *opaque, target_phys_addr_t addr)
2053 {
2054     OMAP_BAD_REG(addr);
2055     return 0;
2056 }
2057
2058 static void omap_sti_fifo_write(void *opaque, target_phys_addr_t addr,
2059                 uint32_t value)
2060 {
2061     struct omap_sti_s *s = (struct omap_sti_s *) opaque;
2062     int ch = addr >> 6;
2063     uint8_t byte = value;
2064
2065     if (ch == STI_TRACE_CONTROL_CHANNEL) {
2066         /* Flush channel <i>value</i>.  */
2067         qemu_chr_write(s->chr, (const uint8_t *) "\r", 1);
2068     } else if (ch == STI_TRACE_CONSOLE_CHANNEL || 1) {
2069         if (value == 0xc0 || value == 0xc3) {
2070             /* Open channel <i>ch</i>.  */
2071         } else if (value == 0x00)
2072             qemu_chr_write(s->chr, (const uint8_t *) "\n", 1);
2073         else
2074             qemu_chr_write(s->chr, &byte, 1);
2075     }
2076 }
2077
2078 static CPUReadMemoryFunc *omap_sti_fifo_readfn[] = {
2079     omap_sti_fifo_read,
2080     omap_badwidth_read8,
2081     omap_badwidth_read8,
2082 };
2083
2084 static CPUWriteMemoryFunc *omap_sti_fifo_writefn[] = {
2085     omap_sti_fifo_write,
2086     omap_badwidth_write8,
2087     omap_badwidth_write8,
2088 };
2089
2090 static struct omap_sti_s *omap_sti_init(struct omap_target_agent_s *ta,
2091                 target_phys_addr_t channel_base, qemu_irq irq, omap_clk clk,
2092                 CharDriverState *chr)
2093 {
2094     int iomemtype;
2095     struct omap_sti_s *s = (struct omap_sti_s *)
2096             qemu_mallocz(sizeof(struct omap_sti_s));
2097
2098     s->irq = irq;
2099     omap_sti_reset(s);
2100
2101     s->chr = chr ?: qemu_chr_open("null", "null", NULL);
2102
2103     iomemtype = l4_register_io_memory(0, omap_sti_readfn,
2104                     omap_sti_writefn, s);
2105     omap_l4_attach(ta, 0, iomemtype);
2106
2107     iomemtype = cpu_register_io_memory(0, omap_sti_fifo_readfn,
2108                     omap_sti_fifo_writefn, s);
2109     cpu_register_physical_memory(channel_base, 0x10000, iomemtype);
2110
2111     return s;
2112 }
2113
2114 /* L4 Interconnect */
2115 #ifdef L4_MUX_HACK
2116 static int omap_l4_io_entries;
2117 static int omap_cpu_io_entry;
2118 static struct omap_l4_entry {
2119         CPUReadMemoryFunc **mem_read;
2120         CPUWriteMemoryFunc **mem_write;
2121         void *opaque;
2122 } *omap_l4_io_entry;
2123 static CPUReadMemoryFunc **omap_l4_io_readb_fn;
2124 static CPUReadMemoryFunc **omap_l4_io_readh_fn;
2125 static CPUReadMemoryFunc **omap_l4_io_readw_fn;
2126 static CPUWriteMemoryFunc **omap_l4_io_writeb_fn;
2127 static CPUWriteMemoryFunc **omap_l4_io_writeh_fn;
2128 static CPUWriteMemoryFunc **omap_l4_io_writew_fn;
2129 static void **omap_l4_io_opaque;
2130
2131 int l4_register_io_memory(int io_index, CPUReadMemoryFunc **mem_read,
2132                 CPUWriteMemoryFunc **mem_write, void *opaque)
2133 {
2134     omap_l4_io_entry[omap_l4_io_entries].mem_read = mem_read;
2135     omap_l4_io_entry[omap_l4_io_entries].mem_write = mem_write;
2136     omap_l4_io_entry[omap_l4_io_entries].opaque = opaque;
2137
2138     return omap_l4_io_entries ++;
2139 }
2140
2141 static uint32_t omap_l4_io_readb(void *opaque, target_phys_addr_t addr)
2142 {
2143     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2144
2145     return omap_l4_io_readb_fn[i](omap_l4_io_opaque[i], addr);
2146 }
2147
2148 static uint32_t omap_l4_io_readh(void *opaque, target_phys_addr_t addr)
2149 {
2150     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2151
2152     return omap_l4_io_readh_fn[i](omap_l4_io_opaque[i], addr);
2153 }
2154
2155 static uint32_t omap_l4_io_readw(void *opaque, target_phys_addr_t addr)
2156 {
2157     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2158
2159     return omap_l4_io_readw_fn[i](omap_l4_io_opaque[i], addr);
2160 }
2161
2162 static void omap_l4_io_writeb(void *opaque, target_phys_addr_t addr,
2163                 uint32_t value)
2164 {
2165     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2166
2167     return omap_l4_io_writeb_fn[i](omap_l4_io_opaque[i], addr, value);
2168 }
2169
2170 static void omap_l4_io_writeh(void *opaque, target_phys_addr_t addr,
2171                 uint32_t value)
2172 {
2173     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2174
2175     return omap_l4_io_writeh_fn[i](omap_l4_io_opaque[i], addr, value);
2176 }
2177
2178 static void omap_l4_io_writew(void *opaque, target_phys_addr_t addr,
2179                 uint32_t value)
2180 {
2181     unsigned int i = (addr - OMAP2_L4_BASE) >> TARGET_PAGE_BITS;
2182
2183     return omap_l4_io_writew_fn[i](omap_l4_io_opaque[i], addr, value);
2184 }
2185
2186 static CPUReadMemoryFunc *omap_l4_io_readfn[] = {
2187     omap_l4_io_readb,
2188     omap_l4_io_readh,
2189     omap_l4_io_readw,
2190 };
2191
2192 static CPUWriteMemoryFunc *omap_l4_io_writefn[] = {
2193     omap_l4_io_writeb,
2194     omap_l4_io_writeh,
2195     omap_l4_io_writew,
2196 };
2197 #endif
2198
2199 struct omap_l4_s *omap_l4_init(target_phys_addr_t base, int ta_num)
2200 {
2201     struct omap_l4_s *bus = qemu_mallocz(
2202                     sizeof(*bus) + ta_num * sizeof(*bus->ta));
2203
2204     bus->ta_num = ta_num;
2205     bus->base = base;
2206
2207 #ifdef L4_MUX_HACK
2208     omap_l4_io_entries = 1;
2209     omap_l4_io_entry = qemu_mallocz(125 * sizeof(*omap_l4_io_entry));
2210
2211     omap_cpu_io_entry =
2212             cpu_register_io_memory(0, omap_l4_io_readfn,
2213                             omap_l4_io_writefn, bus);
2214 # define L4_PAGES       (0xb4000 / TARGET_PAGE_SIZE)
2215     omap_l4_io_readb_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2216     omap_l4_io_readh_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2217     omap_l4_io_readw_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2218     omap_l4_io_writeb_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2219     omap_l4_io_writeh_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2220     omap_l4_io_writew_fn = qemu_mallocz(sizeof(void *) * L4_PAGES);
2221     omap_l4_io_opaque = qemu_mallocz(sizeof(void *) * L4_PAGES);
2222 #endif
2223
2224     return bus;
2225 }
2226
2227 static uint32_t omap_l4ta_read(void *opaque, target_phys_addr_t addr)
2228 {
2229     struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
2230
2231     switch (addr) {
2232     case 0x00:  /* COMPONENT */
2233         return s->component;
2234
2235     case 0x20:  /* AGENT_CONTROL */
2236         return s->control;
2237
2238     case 0x28:  /* AGENT_STATUS */
2239         return s->status;
2240     }
2241
2242     OMAP_BAD_REG(addr);
2243     return 0;
2244 }
2245
2246 static void omap_l4ta_write(void *opaque, target_phys_addr_t addr,
2247                 uint32_t value)
2248 {
2249     struct omap_target_agent_s *s = (struct omap_target_agent_s *) opaque;
2250
2251     switch (addr) {
2252     case 0x00:  /* COMPONENT */
2253     case 0x28:  /* AGENT_STATUS */
2254         OMAP_RO_REG(addr);
2255         break;
2256
2257     case 0x20:  /* AGENT_CONTROL */
2258         s->control = value & 0x01000700;
2259         if (value & 1)                                  /* OCP_RESET */
2260             s->status &= ~1;                            /* REQ_TIMEOUT */
2261         break;
2262
2263     default:
2264         OMAP_BAD_REG(addr);
2265     }
2266 }
2267
2268 static CPUReadMemoryFunc *omap_l4ta_readfn[] = {
2269     omap_badwidth_read16,
2270     omap_l4ta_read,
2271     omap_badwidth_read16,
2272 };
2273
2274 static CPUWriteMemoryFunc *omap_l4ta_writefn[] = {
2275     omap_badwidth_write32,
2276     omap_badwidth_write32,
2277     omap_l4ta_write,
2278 };
2279
2280 #define L4TA(n)         (n)
2281 #define L4TAO(n)        ((n) + 39)
2282
2283 static struct omap_l4_region_s omap_l4_region[125] = {
2284     [  1] = { 0x40800,  0x800, 32          }, /* Initiator agent */
2285     [  2] = { 0x41000, 0x1000, 32          }, /* Link agent */
2286     [  0] = { 0x40000,  0x800, 32          }, /* Address and protection */
2287     [  3] = { 0x00000, 0x1000, 32 | 16 | 8 }, /* System Control and Pinout */
2288     [  4] = { 0x01000, 0x1000, 32 | 16 | 8 }, /* L4TAO1 */
2289     [  5] = { 0x04000, 0x1000, 32 | 16     }, /* 32K Timer */
2290     [  6] = { 0x05000, 0x1000, 32 | 16 | 8 }, /* L4TAO2 */
2291     [  7] = { 0x08000,  0x800, 32          }, /* PRCM Region A */
2292     [  8] = { 0x08800,  0x800, 32          }, /* PRCM Region B */
2293     [  9] = { 0x09000, 0x1000, 32 | 16 | 8 }, /* L4TAO */
2294     [ 10] = { 0x12000, 0x1000, 32 | 16 | 8 }, /* Test (BCM) */
2295     [ 11] = { 0x13000, 0x1000, 32 | 16 | 8 }, /* L4TA1 */
2296     [ 12] = { 0x14000, 0x1000, 32          }, /* Test/emulation (TAP) */
2297     [ 13] = { 0x15000, 0x1000, 32 | 16 | 8 }, /* L4TA2 */
2298     [ 14] = { 0x18000, 0x1000, 32 | 16 | 8 }, /* GPIO1 */
2299     [ 16] = { 0x1a000, 0x1000, 32 | 16 | 8 }, /* GPIO2 */
2300     [ 18] = { 0x1c000, 0x1000, 32 | 16 | 8 }, /* GPIO3 */
2301     [ 19] = { 0x1e000, 0x1000, 32 | 16 | 8 }, /* GPIO4 */
2302     [ 15] = { 0x19000, 0x1000, 32 | 16 | 8 }, /* Quad GPIO TOP */
2303     [ 17] = { 0x1b000, 0x1000, 32 | 16 | 8 }, /* L4TA3 */
2304     [ 20] = { 0x20000, 0x1000, 32 | 16 | 8 }, /* WD Timer 1 (Secure) */
2305     [ 22] = { 0x22000, 0x1000, 32 | 16 | 8 }, /* WD Timer 2 (OMAP) */
2306     [ 21] = { 0x21000, 0x1000, 32 | 16 | 8 }, /* Dual WD timer TOP */
2307     [ 23] = { 0x23000, 0x1000, 32 | 16 | 8 }, /* L4TA4 */
2308     [ 24] = { 0x28000, 0x1000, 32 | 16 | 8 }, /* GP Timer 1 */
2309     [ 25] = { 0x29000, 0x1000, 32 | 16 | 8 }, /* L4TA7 */
2310     [ 26] = { 0x48000, 0x2000, 32 | 16 | 8 }, /* Emulation (ARM11ETB) */
2311     [ 27] = { 0x4a000, 0x1000, 32 | 16 | 8 }, /* L4TA9 */
2312     [ 28] = { 0x50000,  0x400, 32 | 16 | 8 }, /* Display top */
2313     [ 29] = { 0x50400,  0x400, 32 | 16 | 8 }, /* Display control */
2314     [ 30] = { 0x50800,  0x400, 32 | 16 | 8 }, /* Display RFBI */
2315     [ 31] = { 0x50c00,  0x400, 32 | 16 | 8 }, /* Display encoder */
2316     [ 32] = { 0x51000, 0x1000, 32 | 16 | 8 }, /* L4TA10 */
2317     [ 33] = { 0x52000,  0x400, 32 | 16 | 8 }, /* Camera top */
2318     [ 34] = { 0x52400,  0x400, 32 | 16 | 8 }, /* Camera core */
2319     [ 35] = { 0x52800,  0x400, 32 | 16 | 8 }, /* Camera DMA */
2320     [ 36] = { 0x52c00,  0x400, 32 | 16 | 8 }, /* Camera MMU */
2321     [ 37] = { 0x53000, 0x1000, 32 | 16 | 8 }, /* L4TA11 */
2322     [ 38] = { 0x56000, 0x1000, 32 | 16 | 8 }, /* sDMA */
2323     [ 39] = { 0x57000, 0x1000, 32 | 16 | 8 }, /* L4TA12 */
2324     [ 40] = { 0x58000, 0x1000, 32 | 16 | 8 }, /* SSI top */
2325     [ 41] = { 0x59000, 0x1000, 32 | 16 | 8 }, /* SSI GDD */
2326     [ 42] = { 0x5a000, 0x1000, 32 | 16 | 8 }, /* SSI Port1 */
2327     [ 43] = { 0x5b000, 0x1000, 32 | 16 | 8 }, /* SSI Port2 */
2328     [ 44] = { 0x5c000, 0x1000, 32 | 16 | 8 }, /* L4TA13 */
2329     [ 45] = { 0x5e000, 0x1000, 32 | 16 | 8 }, /* USB OTG */
2330     [ 46] = { 0x5f000, 0x1000, 32 | 16 | 8 }, /* L4TAO4 */
2331     [ 47] = { 0x60000, 0x1000, 32 | 16 | 8 }, /* Emulation (WIN_TRACER1SDRC) */
2332     [ 48] = { 0x61000, 0x1000, 32 | 16 | 8 }, /* L4TA14 */
2333     [ 49] = { 0x62000, 0x1000, 32 | 16 | 8 }, /* Emulation (WIN_TRACER2GPMC) */
2334     [ 50] = { 0x63000, 0x1000, 32 | 16 | 8 }, /* L4TA15 */
2335     [ 51] = { 0x64000, 0x1000, 32 | 16 | 8 }, /* Emulation (WIN_TRACER3OCM) */
2336     [ 52] = { 0x65000, 0x1000, 32 | 16 | 8 }, /* L4TA16 */
2337     [ 53] = { 0x66000,  0x300, 32 | 16 | 8 }, /* Emulation (WIN_TRACER4L4) */
2338     [ 54] = { 0x67000, 0x1000, 32 | 16 | 8 }, /* L4TA17 */
2339     [ 55] = { 0x68000, 0x1000, 32 | 16 | 8 }, /* Emulation (XTI) */
2340     [ 56] = { 0x69000, 0x1000, 32 | 16 | 8 }, /* L4TA18 */
2341     [ 57] = { 0x6a000, 0x1000,      16 | 8 }, /* UART1 */
2342     [ 58] = { 0x6b000, 0x1000, 32 | 16 | 8 }, /* L4TA19 */
2343     [ 59] = { 0x6c000, 0x1000,      16 | 8 }, /* UART2 */
2344     [ 60] = { 0x6d000, 0x1000, 32 | 16 | 8 }, /* L4TA20 */
2345     [ 61] = { 0x6e000, 0x1000,      16 | 8 }, /* UART3 */
2346     [ 62] = { 0x6f000, 0x1000, 32 | 16 | 8 }, /* L4TA21 */
2347     [ 63] = { 0x70000, 0x1000,      16     }, /* I2C1 */
2348     [ 64] = { 0x71000, 0x1000, 32 | 16 | 8 }, /* L4TAO5 */
2349     [ 65] = { 0x72000, 0x1000,      16     }, /* I2C2 */
2350     [ 66] = { 0x73000, 0x1000, 32 | 16 | 8 }, /* L4TAO6 */
2351     [ 67] = { 0x74000, 0x1000,      16     }, /* McBSP1 */
2352     [ 68] = { 0x75000, 0x1000, 32 | 16 | 8 }, /* L4TAO7 */
2353     [ 69] = { 0x76000, 0x1000,      16     }, /* McBSP2 */
2354     [ 70] = { 0x77000, 0x1000, 32 | 16 | 8 }, /* L4TAO8 */
2355     [ 71] = { 0x24000, 0x1000, 32 | 16 | 8 }, /* WD Timer 3 (DSP) */
2356     [ 72] = { 0x25000, 0x1000, 32 | 16 | 8 }, /* L4TA5 */
2357     [ 73] = { 0x26000, 0x1000, 32 | 16 | 8 }, /* WD Timer 4 (IVA) */
2358     [ 74] = { 0x27000, 0x1000, 32 | 16 | 8 }, /* L4TA6 */
2359     [ 75] = { 0x2a000, 0x1000, 32 | 16 | 8 }, /* GP Timer 2 */
2360     [ 76] = { 0x2b000, 0x1000, 32 | 16 | 8 }, /* L4TA8 */
2361     [ 77] = { 0x78000, 0x1000, 32 | 16 | 8 }, /* GP Timer 3 */
2362     [ 78] = { 0x79000, 0x1000, 32 | 16 | 8 }, /* L4TA22 */
2363     [ 79] = { 0x7a000, 0x1000, 32 | 16 | 8 }, /* GP Timer 4 */
2364     [ 80] = { 0x7b000, 0x1000, 32 | 16 | 8 }, /* L4TA23 */
2365     [ 81] = { 0x7c000, 0x1000, 32 | 16 | 8 }, /* GP Timer 5 */
2366     [ 82] = { 0x7d000, 0x1000, 32 | 16 | 8 }, /* L4TA24 */
2367     [ 83] = { 0x7e000, 0x1000, 32 | 16 | 8 }, /* GP Timer 6 */
2368     [ 84] = { 0x7f000, 0x1000, 32 | 16 | 8 }, /* L4TA25 */
2369     [ 85] = { 0x80000, 0x1000, 32 | 16 | 8 }, /* GP Timer 7 */
2370     [ 86] = { 0x81000, 0x1000, 32 | 16 | 8 }, /* L4TA26 */
2371     [ 87] = { 0x82000, 0x1000, 32 | 16 | 8 }, /* GP Timer 8 */
2372     [ 88] = { 0x83000, 0x1000, 32 | 16 | 8 }, /* L4TA27 */
2373     [ 89] = { 0x84000, 0x1000, 32 | 16 | 8 }, /* GP Timer 9 */
2374     [ 90] = { 0x85000, 0x1000, 32 | 16 | 8 }, /* L4TA28 */
2375     [ 91] = { 0x86000, 0x1000, 32 | 16 | 8 }, /* GP Timer 10 */
2376     [ 92] = { 0x87000, 0x1000, 32 | 16 | 8 }, /* L4TA29 */
2377     [ 93] = { 0x88000, 0x1000, 32 | 16 | 8 }, /* GP Timer 11 */
2378     [ 94] = { 0x89000, 0x1000, 32 | 16 | 8 }, /* L4TA30 */
2379     [ 95] = { 0x8a000, 0x1000, 32 | 16 | 8 }, /* GP Timer 12 */
2380     [ 96] = { 0x8b000, 0x1000, 32 | 16 | 8 }, /* L4TA31 */
2381     [ 97] = { 0x90000, 0x1000,      16     }, /* EAC */
2382     [ 98] = { 0x91000, 0x1000, 32 | 16 | 8 }, /* L4TA32 */
2383     [ 99] = { 0x92000, 0x1000,      16     }, /* FAC */
2384     [100] = { 0x93000, 0x1000, 32 | 16 | 8 }, /* L4TA33 */
2385     [101] = { 0x94000, 0x1000, 32 | 16 | 8 }, /* IPC (MAILBOX) */
2386     [102] = { 0x95000, 0x1000, 32 | 16 | 8 }, /* L4TA34 */
2387     [103] = { 0x98000, 0x1000, 32 | 16 | 8 }, /* SPI1 */
2388     [104] = { 0x99000, 0x1000, 32 | 16 | 8 }, /* L4TA35 */
2389     [105] = { 0x9a000, 0x1000, 32 | 16 | 8 }, /* SPI2 */
2390     [106] = { 0x9b000, 0x1000, 32 | 16 | 8 }, /* L4TA36 */
2391     [107] = { 0x9c000, 0x1000,      16 | 8 }, /* MMC SDIO */
2392     [108] = { 0x9d000, 0x1000, 32 | 16 | 8 }, /* L4TAO9 */
2393     [109] = { 0x9e000, 0x1000, 32 | 16 | 8 }, /* MS_PRO */
2394     [110] = { 0x9f000, 0x1000, 32 | 16 | 8 }, /* L4TAO10 */
2395     [111] = { 0xa0000, 0x1000, 32          }, /* RNG */
2396     [112] = { 0xa1000, 0x1000, 32 | 16 | 8 }, /* L4TAO11 */
2397     [113] = { 0xa2000, 0x1000, 32          }, /* DES3DES */
2398     [114] = { 0xa3000, 0x1000, 32 | 16 | 8 }, /* L4TAO12 */
2399     [115] = { 0xa4000, 0x1000, 32          }, /* SHA1MD5 */
2400     [116] = { 0xa5000, 0x1000, 32 | 16 | 8 }, /* L4TAO13 */
2401     [117] = { 0xa6000, 0x1000, 32          }, /* AES */
2402     [118] = { 0xa7000, 0x1000, 32 | 16 | 8 }, /* L4TA37 */
2403     [119] = { 0xa8000, 0x2000, 32          }, /* PKA */
2404     [120] = { 0xaa000, 0x1000, 32 | 16 | 8 }, /* L4TA38 */
2405     [121] = { 0xb0000, 0x1000, 32          }, /* MG */
2406     [122] = { 0xb1000, 0x1000, 32 | 16 | 8 },
2407     [123] = { 0xb2000, 0x1000, 32          }, /* HDQ/1-Wire */
2408     [124] = { 0xb3000, 0x1000, 32 | 16 | 8 }, /* L4TA39 */
2409 };
2410
2411 static struct omap_l4_agent_info_s omap_l4_agent_info[54] = {
2412     { 0,           0, 3, 2 }, /* L4IA initiatior agent */
2413     { L4TAO(1),    3, 2, 1 }, /* Control and pinout module */
2414     { L4TAO(2),    5, 2, 1 }, /* 32K timer */
2415     { L4TAO(3),    7, 3, 2 }, /* PRCM */
2416     { L4TA(1),    10, 2, 1 }, /* BCM */
2417     { L4TA(2),    12, 2, 1 }, /* Test JTAG */
2418     { L4TA(3),    14, 6, 3 }, /* Quad GPIO */
2419     { L4TA(4),    20, 4, 3 }, /* WD timer 1/2 */
2420     { L4TA(7),    24, 2, 1 }, /* GP timer 1 */
2421     { L4TA(9),    26, 2, 1 }, /* ATM11 ETB */
2422     { L4TA(10),   28, 5, 4 }, /* Display subsystem */
2423     { L4TA(11),   33, 5, 4 }, /* Camera subsystem */
2424     { L4TA(12),   38, 2, 1 }, /* sDMA */
2425     { L4TA(13),   40, 5, 4 }, /* SSI */
2426     { L4TAO(4),   45, 2, 1 }, /* USB */
2427     { L4TA(14),   47, 2, 1 }, /* Win Tracer1 */
2428     { L4TA(15),   49, 2, 1 }, /* Win Tracer2 */
2429     { L4TA(16),   51, 2, 1 }, /* Win Tracer3 */
2430     { L4TA(17),   53, 2, 1 }, /* Win Tracer4 */
2431     { L4TA(18),   55, 2, 1 }, /* XTI */
2432     { L4TA(19),   57, 2, 1 }, /* UART1 */
2433     { L4TA(20),   59, 2, 1 }, /* UART2 */
2434     { L4TA(21),   61, 2, 1 }, /* UART3 */
2435     { L4TAO(5),   63, 2, 1 }, /* I2C1 */
2436     { L4TAO(6),   65, 2, 1 }, /* I2C2 */
2437     { L4TAO(7),   67, 2, 1 }, /* McBSP1 */
2438     { L4TAO(8),   69, 2, 1 }, /* McBSP2 */
2439     { L4TA(5),    71, 2, 1 }, /* WD Timer 3 (DSP) */
2440     { L4TA(6),    73, 2, 1 }, /* WD Timer 4 (IVA) */
2441     { L4TA(8),    75, 2, 1 }, /* GP Timer 2 */
2442     { L4TA(22),   77, 2, 1 }, /* GP Timer 3 */
2443     { L4TA(23),   79, 2, 1 }, /* GP Timer 4 */
2444     { L4TA(24),   81, 2, 1 }, /* GP Timer 5 */
2445     { L4TA(25),   83, 2, 1 }, /* GP Timer 6 */
2446     { L4TA(26),   85, 2, 1 }, /* GP Timer 7 */
2447     { L4TA(27),   87, 2, 1 }, /* GP Timer 8 */
2448     { L4TA(28),   89, 2, 1 }, /* GP Timer 9 */
2449     { L4TA(29),   91, 2, 1 }, /* GP Timer 10 */
2450     { L4TA(30),   93, 2, 1 }, /* GP Timer 11 */
2451     { L4TA(31),   95, 2, 1 }, /* GP Timer 12 */
2452     { L4TA(32),   97, 2, 1 }, /* EAC */
2453     { L4TA(33),   99, 2, 1 }, /* FAC */
2454     { L4TA(34),  101, 2, 1 }, /* IPC */
2455     { L4TA(35),  103, 2, 1 }, /* SPI1 */
2456     { L4TA(36),  105, 2, 1 }, /* SPI2 */
2457     { L4TAO(9),  107, 2, 1 }, /* MMC SDIO */
2458     { L4TAO(10), 109, 2, 1 },
2459     { L4TAO(11), 111, 2, 1 }, /* RNG */
2460     { L4TAO(12), 113, 2, 1 }, /* DES3DES */
2461     { L4TAO(13), 115, 2, 1 }, /* SHA1MD5 */
2462     { L4TA(37),  117, 2, 1 }, /* AES */
2463     { L4TA(38),  119, 2, 1 }, /* PKA */
2464     { -1,        121, 2, 1 },
2465     { L4TA(39),  123, 2, 1 }, /* HDQ/1-Wire */
2466 };
2467
2468 #define omap_l4ta(bus, cs)      omap_l4ta_get(bus, L4TA(cs))
2469 #define omap_l4tao(bus, cs)     omap_l4ta_get(bus, L4TAO(cs))
2470
2471 static struct omap_target_agent_s *omap_l4ta_get(struct omap_l4_s *bus, int cs)
2472 {
2473     int i, iomemtype;
2474     struct omap_target_agent_s *ta = 0;
2475     struct omap_l4_agent_info_s *info = 0;
2476
2477     for (i = 0; i < bus->ta_num; i ++)
2478         if (omap_l4_agent_info[i].ta == cs) {
2479             ta = &bus->ta[i];
2480             info = &omap_l4_agent_info[i];
2481             break;
2482         }
2483     if (!ta) {
2484         fprintf(stderr, "%s: bad target agent (%i)\n", __FUNCTION__, cs);
2485         exit(-1);
2486     }
2487
2488     ta->bus = bus;
2489     ta->start = &omap_l4_region[info->region];
2490     ta->regions = info->regions;
2491
2492     ta->component = ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
2493     ta->status = 0x00000000;
2494     ta->control = 0x00000200;   /* XXX 01000200 for L4TAO */
2495
2496     iomemtype = l4_register_io_memory(0, omap_l4ta_readfn,
2497                     omap_l4ta_writefn, ta);
2498     ta->base = omap_l4_attach(ta, info->ta_region, iomemtype);
2499
2500     return ta;
2501 }
2502
2503 target_phys_addr_t omap_l4_attach(struct omap_target_agent_s *ta, int region,
2504                 int iotype)
2505 {
2506     target_phys_addr_t base;
2507     ssize_t size;
2508 #ifdef L4_MUX_HACK
2509     int i;
2510 #endif
2511
2512     if (region < 0 || region >= ta->regions) {
2513         fprintf(stderr, "%s: bad io region (%i)\n", __FUNCTION__, region);
2514         exit(-1);
2515     }
2516
2517     base = ta->bus->base + ta->start[region].offset;
2518     size = ta->start[region].size;
2519     if (iotype) {
2520 #ifndef L4_MUX_HACK
2521         cpu_register_physical_memory(base, size, iotype);
2522 #else
2523         cpu_register_physical_memory(base, size, omap_cpu_io_entry);
2524         i = (base - ta->bus->base) / TARGET_PAGE_SIZE;
2525         for (; size > 0; size -= TARGET_PAGE_SIZE, i ++) {
2526             omap_l4_io_readb_fn[i] = omap_l4_io_entry[iotype].mem_read[0];
2527             omap_l4_io_readh_fn[i] = omap_l4_io_entry[iotype].mem_read[1];
2528             omap_l4_io_readw_fn[i] = omap_l4_io_entry[iotype].mem_read[2];
2529             omap_l4_io_writeb_fn[i] = omap_l4_io_entry[iotype].mem_write[0];
2530             omap_l4_io_writeh_fn[i] = omap_l4_io_entry[iotype].mem_write[1];
2531             omap_l4_io_writew_fn[i] = omap_l4_io_entry[iotype].mem_write[2];
2532             omap_l4_io_opaque[i] = omap_l4_io_entry[iotype].opaque;
2533         }
2534 #endif
2535     }
2536
2537     return base;
2538 }
2539
2540 target_phys_addr_t omap_l4_base(struct omap_target_agent_s *ta, int region)
2541 {
2542     return ta->bus->base + ta->start[region].offset;
2543 }
2544
2545 uint32_t omap_l4_size(struct omap_target_agent_s *ta, int region)
2546 {
2547     return ta->start[region].size;
2548 }
2549
2550 /* TEST-Chip-level TAP */
2551 static uint32_t omap_tap_read(void *opaque, target_phys_addr_t addr)
2552 {
2553     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2554
2555     switch (addr) {
2556     case 0x204: /* IDCODE_reg */
2557         switch (s->mpu_model) {
2558         case omap2420:
2559         case omap2422:
2560         case omap2423:
2561             return 0x5b5d902f;  /* ES 2.2 */
2562         case omap2430:
2563             return 0x5b68a02f;  /* ES 2.2 */
2564         case omap3430:
2565             return 0x1b7ae02f;  /* ES 2 */
2566         case omap3530:
2567             return 0x3b7ae02f;  /* ES 3.0 */
2568         default:
2569             hw_error("%s: Bad mpu model\n", __FUNCTION__);
2570         }
2571
2572     case 0x208: /* PRODUCTION_ID_reg for OMAP2 */
2573     case 0x210: /* PRODUCTION_ID_reg for OMAP3 */
2574         switch (s->mpu_model) {
2575         case omap2420:
2576             return 0x000254f0;  /* POP ESHS2.1.1 in N91/93/95, ES2 in N800 */
2577         case omap2422:
2578             return 0x000400f0;
2579         case omap2423:
2580             return 0x000800f0;
2581         case omap2430:
2582             return 0x000000f0;
2583         case omap3430:
2584             return 0x000000f0;
2585         case omap3530:
2586             return 0x000f00f0;
2587         default:
2588             hw_error("%s: Bad mpu model\n", __FUNCTION__);
2589         }
2590
2591     case 0x20c:
2592         switch (s->mpu_model) {
2593         case omap2420:
2594         case omap2422:
2595         case omap2423:
2596             return 0xcafeb5d9;  /* ES 2.2 */
2597         case omap2430:
2598             return 0xcafeb68a;  /* ES 2.2 */
2599         case omap3430:
2600         case omap3530:
2601             return 0xcafeb7ae;  /* ES 2 */
2602         default:
2603             hw_error("%s: Bad mpu model\n", __FUNCTION__);
2604         }
2605
2606     case 0x218: /* DIE_ID_reg */
2607         return ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
2608     case 0x21c: /* DIE_ID_reg */
2609         return 0x54 << 24;
2610     case 0x220: /* DIE_ID_reg */
2611         return ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
2612     case 0x224: /* DIE_ID_reg */
2613         return ('Q' << 24) | ('E' << 16) | ('M' << 8) | ('U' << 0);
2614     }
2615
2616     OMAP_BAD_REG(addr);
2617     return 0;
2618 }
2619
2620 static void omap_tap_write(void *opaque, target_phys_addr_t addr,
2621                 uint32_t value)
2622 {
2623     OMAP_BAD_REG(addr);
2624 }
2625
2626 static CPUReadMemoryFunc *omap_tap_readfn[] = {
2627     omap_badwidth_read32,
2628     omap_badwidth_read32,
2629     omap_tap_read,
2630 };
2631
2632 static CPUWriteMemoryFunc *omap_tap_writefn[] = {
2633     omap_badwidth_write32,
2634     omap_badwidth_write32,
2635     omap_tap_write,
2636 };
2637
2638 void omap_tap_init(struct omap_target_agent_s *ta,
2639                 struct omap_mpu_state_s *mpu)
2640 {
2641     omap_l4_attach(ta, 0, l4_register_io_memory(0,
2642                             omap_tap_readfn, omap_tap_writefn, mpu));
2643 }
2644
2645 /* Power, Reset, and Clock Management */
2646 struct omap_prcm_s {
2647     qemu_irq irq[3];
2648     struct omap_mpu_state_s *mpu;
2649
2650     uint32_t irqst[3];
2651     uint32_t irqen[3];
2652
2653     uint32_t sysconfig;
2654     uint32_t voltctrl;
2655     uint32_t scratch[20];
2656
2657     uint32_t clksrc[1];
2658     uint32_t clkout[1];
2659     uint32_t clkemul[1];
2660     uint32_t clkpol[1];
2661     uint32_t clksel[8];
2662     uint32_t clken[12];
2663     uint32_t clkctrl[4];
2664     uint32_t clkidle[7];
2665     uint32_t setuptime[2];
2666
2667     uint32_t wkup[3];
2668     uint32_t wken[3];
2669     uint32_t wkst[3];
2670     uint32_t rst[4];
2671     uint32_t rstctrl[1];
2672     uint32_t power[4];
2673     uint32_t rsttime_wkup;
2674
2675     uint32_t ev;
2676     uint32_t evtime[2];
2677
2678     int dpll_lock, apll_lock[2];
2679 };
2680
2681 static void omap_prcm_int_update(struct omap_prcm_s *s, int dom)
2682 {
2683     qemu_set_irq(s->irq[dom], s->irqst[dom] & s->irqen[dom]);
2684     /* XXX or is the mask applied before PRCM_IRQSTATUS_* ? */
2685 }
2686
2687 static uint32_t omap_prcm_read(void *opaque, target_phys_addr_t addr)
2688 {
2689     struct omap_prcm_s *s = (struct omap_prcm_s *) opaque;
2690     uint32_t ret;
2691
2692     switch (addr) {
2693     case 0x000: /* PRCM_REVISION */
2694         return 0x10;
2695
2696     case 0x010: /* PRCM_SYSCONFIG */
2697         return s->sysconfig;
2698
2699     case 0x018: /* PRCM_IRQSTATUS_MPU */
2700         return s->irqst[0];
2701
2702     case 0x01c: /* PRCM_IRQENABLE_MPU */
2703         return s->irqen[0];
2704
2705     case 0x050: /* PRCM_VOLTCTRL */
2706         return s->voltctrl;
2707     case 0x054: /* PRCM_VOLTST */
2708         return s->voltctrl & 3;
2709
2710     case 0x060: /* PRCM_CLKSRC_CTRL */
2711         return s->clksrc[0];
2712     case 0x070: /* PRCM_CLKOUT_CTRL */
2713         return s->clkout[0];
2714     case 0x078: /* PRCM_CLKEMUL_CTRL */
2715         return s->clkemul[0];
2716     case 0x080: /* PRCM_CLKCFG_CTRL */
2717     case 0x084: /* PRCM_CLKCFG_STATUS */
2718         return 0;
2719
2720     case 0x090: /* PRCM_VOLTSETUP */
2721         return s->setuptime[0];
2722
2723     case 0x094: /* PRCM_CLKSSETUP */
2724         return s->setuptime[1];
2725
2726     case 0x098: /* PRCM_POLCTRL */
2727         return s->clkpol[0];
2728
2729     case 0x0b0: /* GENERAL_PURPOSE1 */
2730     case 0x0b4: /* GENERAL_PURPOSE2 */
2731     case 0x0b8: /* GENERAL_PURPOSE3 */
2732     case 0x0bc: /* GENERAL_PURPOSE4 */
2733     case 0x0c0: /* GENERAL_PURPOSE5 */
2734     case 0x0c4: /* GENERAL_PURPOSE6 */
2735     case 0x0c8: /* GENERAL_PURPOSE7 */
2736     case 0x0cc: /* GENERAL_PURPOSE8 */
2737     case 0x0d0: /* GENERAL_PURPOSE9 */
2738     case 0x0d4: /* GENERAL_PURPOSE10 */
2739     case 0x0d8: /* GENERAL_PURPOSE11 */
2740     case 0x0dc: /* GENERAL_PURPOSE12 */
2741     case 0x0e0: /* GENERAL_PURPOSE13 */
2742     case 0x0e4: /* GENERAL_PURPOSE14 */
2743     case 0x0e8: /* GENERAL_PURPOSE15 */
2744     case 0x0ec: /* GENERAL_PURPOSE16 */
2745     case 0x0f0: /* GENERAL_PURPOSE17 */
2746     case 0x0f4: /* GENERAL_PURPOSE18 */
2747     case 0x0f8: /* GENERAL_PURPOSE19 */
2748     case 0x0fc: /* GENERAL_PURPOSE20 */
2749         return s->scratch[(addr - 0xb0) >> 2];
2750
2751     case 0x140: /* CM_CLKSEL_MPU */
2752         return s->clksel[0];
2753     case 0x148: /* CM_CLKSTCTRL_MPU */
2754         return s->clkctrl[0];
2755
2756     case 0x158: /* RM_RSTST_MPU */
2757         return s->rst[0];
2758     case 0x1c8: /* PM_WKDEP_MPU */
2759         return s->wkup[0];
2760     case 0x1d4: /* PM_EVGENCTRL_MPU */
2761         return s->ev;
2762     case 0x1d8: /* PM_EVEGENONTIM_MPU */
2763         return s->evtime[0];
2764     case 0x1dc: /* PM_EVEGENOFFTIM_MPU */
2765         return s->evtime[1];
2766     case 0x1e0: /* PM_PWSTCTRL_MPU */
2767         return s->power[0];
2768     case 0x1e4: /* PM_PWSTST_MPU */
2769         return 0;
2770
2771     case 0x200: /* CM_FCLKEN1_CORE */
2772         return s->clken[0];
2773     case 0x204: /* CM_FCLKEN2_CORE */
2774         return s->clken[1];
2775     case 0x210: /* CM_ICLKEN1_CORE */
2776         return s->clken[2];
2777     case 0x214: /* CM_ICLKEN2_CORE */
2778         return s->clken[3];
2779     case 0x21c: /* CM_ICLKEN4_CORE */
2780         return s->clken[4];
2781
2782     case 0x220: /* CM_IDLEST1_CORE */
2783         /* TODO: check the actual iclk status */
2784         return 0x7ffffff9;
2785     case 0x224: /* CM_IDLEST2_CORE */
2786         /* TODO: check the actual iclk status */
2787         return 0x00000007;
2788     case 0x22c: /* CM_IDLEST4_CORE */
2789         /* TODO: check the actual iclk status */
2790         return 0x0000001f;
2791
2792     case 0x230: /* CM_AUTOIDLE1_CORE */
2793         return s->clkidle[0];
2794     case 0x234: /* CM_AUTOIDLE2_CORE */
2795         return s->clkidle[1];
2796     case 0x238: /* CM_AUTOIDLE3_CORE */
2797         return s->clkidle[2];
2798     case 0x23c: /* CM_AUTOIDLE4_CORE */
2799         return s->clkidle[3];
2800
2801     case 0x240: /* CM_CLKSEL1_CORE */
2802         return s->clksel[1];
2803     case 0x244: /* CM_CLKSEL2_CORE */
2804         return s->clksel[2];
2805
2806     case 0x248: /* CM_CLKSTCTRL_CORE */
2807         return s->clkctrl[1];
2808
2809     case 0x2a0: /* PM_WKEN1_CORE */
2810         return s->wken[0];
2811     case 0x2a4: /* PM_WKEN2_CORE */
2812         return s->wken[1];
2813
2814     case 0x2b0: /* PM_WKST1_CORE */
2815         return s->wkst[0];
2816     case 0x2b4: /* PM_WKST2_CORE */
2817         return s->wkst[1];
2818     case 0x2c8: /* PM_WKDEP_CORE */
2819         return 0x1e;
2820
2821     case 0x2e0: /* PM_PWSTCTRL_CORE */
2822         return s->power[1];
2823     case 0x2e4: /* PM_PWSTST_CORE */
2824         return 0x000030 | (s->power[1] & 0xfc00);
2825
2826     case 0x300: /* CM_FCLKEN_GFX */
2827         return s->clken[5];
2828     case 0x310: /* CM_ICLKEN_GFX */
2829         return s->clken[6];
2830     case 0x320: /* CM_IDLEST_GFX */
2831         /* TODO: check the actual iclk status */
2832         return 0x00000001;
2833     case 0x340: /* CM_CLKSEL_GFX */
2834         return s->clksel[3];
2835     case 0x348: /* CM_CLKSTCTRL_GFX */
2836         return s->clkctrl[2];
2837     case 0x350: /* RM_RSTCTRL_GFX */
2838         return s->rstctrl[0];
2839     case 0x358: /* RM_RSTST_GFX */
2840         return s->rst[1];
2841     case 0x3c8: /* PM_WKDEP_GFX */
2842         return s->wkup[1];
2843
2844     case 0x3e0: /* PM_PWSTCTRL_GFX */
2845         return s->power[2];
2846     case 0x3e4: /* PM_PWSTST_GFX */
2847         return s->power[2] & 3;
2848
2849     case 0x400: /* CM_FCLKEN_WKUP */
2850         return s->clken[7];
2851     case 0x410: /* CM_ICLKEN_WKUP */
2852         return s->clken[8];
2853     case 0x420: /* CM_IDLEST_WKUP */
2854         /* TODO: check the actual iclk status */
2855         return 0x0000003f;
2856     case 0x430: /* CM_AUTOIDLE_WKUP */
2857         return s->clkidle[4];
2858     case 0x440: /* CM_CLKSEL_WKUP */
2859         return s->clksel[4];
2860     case 0x450: /* RM_RSTCTRL_WKUP */
2861         return 0;
2862     case 0x454: /* RM_RSTTIME_WKUP */
2863         return s->rsttime_wkup;
2864     case 0x458: /* RM_RSTST_WKUP */
2865         return s->rst[2];
2866     case 0x4a0: /* PM_WKEN_WKUP */
2867         return s->wken[2];
2868     case 0x4b0: /* PM_WKST_WKUP */
2869         return s->wkst[2];
2870
2871     case 0x500: /* CM_CLKEN_PLL */
2872         return s->clken[9];
2873     case 0x520: /* CM_IDLEST_CKGEN */
2874         ret = 0x0000070 | (s->apll_lock[0] << 9) | (s->apll_lock[1] << 8);
2875         if (!(s->clksel[6] & 3))
2876             /* Core uses 32-kHz clock */
2877             ret |= 3 << 0;
2878         else if (!s->dpll_lock)
2879             /* DPLL not locked, core uses ref_clk */
2880             ret |= 1 << 0;
2881         else
2882             /* Core uses DPLL */
2883             ret |= 2 << 0;
2884         return ret;
2885     case 0x530: /* CM_AUTOIDLE_PLL */
2886         return s->clkidle[5];
2887     case 0x540: /* CM_CLKSEL1_PLL */
2888         return s->clksel[5];
2889     case 0x544: /* CM_CLKSEL2_PLL */
2890         return s->clksel[6];
2891
2892     case 0x800: /* CM_FCLKEN_DSP */
2893         return s->clken[10];
2894     case 0x810: /* CM_ICLKEN_DSP */
2895         return s->clken[11];
2896     case 0x820: /* CM_IDLEST_DSP */
2897         /* TODO: check the actual iclk status */
2898         return 0x00000103;
2899     case 0x830: /* CM_AUTOIDLE_DSP */
2900         return s->clkidle[6];
2901     case 0x840: /* CM_CLKSEL_DSP */
2902         return s->clksel[7];
2903     case 0x848: /* CM_CLKSTCTRL_DSP */
2904         return s->clkctrl[3];
2905     case 0x850: /* RM_RSTCTRL_DSP */
2906         return 0;
2907     case 0x858: /* RM_RSTST_DSP */
2908         return s->rst[3];
2909     case 0x8c8: /* PM_WKDEP_DSP */
2910         return s->wkup[2];
2911     case 0x8e0: /* PM_PWSTCTRL_DSP */
2912         return s->power[3];
2913     case 0x8e4: /* PM_PWSTST_DSP */
2914         return 0x008030 | (s->power[3] & 0x3003);
2915
2916     case 0x8f0: /* PRCM_IRQSTATUS_DSP */
2917         return s->irqst[1];
2918     case 0x8f4: /* PRCM_IRQENABLE_DSP */
2919         return s->irqen[1];
2920
2921     case 0x8f8: /* PRCM_IRQSTATUS_IVA */
2922         return s->irqst[2];
2923     case 0x8fc: /* PRCM_IRQENABLE_IVA */
2924         return s->irqen[2];
2925     }
2926
2927     OMAP_BAD_REG(addr);
2928     return 0;
2929 }
2930
2931 static void omap_prcm_apll_update(struct omap_prcm_s *s)
2932 {
2933     int mode[2];
2934
2935     mode[0] = (s->clken[9] >> 6) & 3;
2936     s->apll_lock[0] = (mode[0] == 3);
2937     mode[1] = (s->clken[9] >> 2) & 3;
2938     s->apll_lock[1] = (mode[1] == 3);
2939     /* TODO: update clocks */
2940
2941     if (mode[0] == 1 || mode[0] == 2 || mode[1] == 1 || mode[2] == 2)
2942         fprintf(stderr, "%s: bad EN_54M_PLL or bad EN_96M_PLL\n",
2943                         __FUNCTION__);
2944 }
2945
2946 static void omap_prcm_dpll_update(struct omap_prcm_s *s)
2947 {
2948     omap_clk dpll = omap_findclk(s->mpu, "dpll");
2949     omap_clk dpll_x2 = omap_findclk(s->mpu, "dpll");
2950     omap_clk core = omap_findclk(s->mpu, "core_clk");
2951     int mode = (s->clken[9] >> 0) & 3;
2952     int mult, div;
2953
2954     mult = (s->clksel[5] >> 12) & 0x3ff;
2955     div = (s->clksel[5] >> 8) & 0xf;
2956     if (mult == 0 || mult == 1)
2957         mode = 1;       /* Bypass */
2958
2959     s->dpll_lock = 0;
2960     switch (mode) {
2961     case 0:
2962         fprintf(stderr, "%s: bad EN_DPLL\n", __FUNCTION__);
2963         break;
2964     case 1:     /* Low-power bypass mode (Default) */
2965     case 2:     /* Fast-relock bypass mode */
2966         omap_clk_setrate(dpll, 1, 1);
2967         omap_clk_setrate(dpll_x2, 1, 1);
2968         break;
2969     case 3:     /* Lock mode */
2970         s->dpll_lock = 1; /* After 20 FINT cycles (ref_clk / (div + 1)).  */
2971
2972         omap_clk_setrate(dpll, div + 1, mult);
2973         omap_clk_setrate(dpll_x2, div + 1, mult * 2);
2974         break;
2975     }
2976
2977     switch ((s->clksel[6] >> 0) & 3) {
2978     case 0:
2979         omap_clk_reparent(core, omap_findclk(s->mpu, "clk32-kHz"));
2980         break;
2981     case 1:
2982         omap_clk_reparent(core, dpll);
2983         break;
2984     case 2:
2985         /* Default */
2986         omap_clk_reparent(core, dpll_x2);
2987         break;
2988     case 3:
2989         fprintf(stderr, "%s: bad CORE_CLK_SRC\n", __FUNCTION__);
2990         break;
2991     }
2992 }
2993
2994 static void omap_prcm_write(void *opaque, target_phys_addr_t addr,
2995                 uint32_t value)
2996 {
2997     struct omap_prcm_s *s = (struct omap_prcm_s *) opaque;
2998
2999     switch (addr) {
3000     case 0x000: /* PRCM_REVISION */
3001     case 0x054: /* PRCM_VOLTST */
3002     case 0x084: /* PRCM_CLKCFG_STATUS */
3003     case 0x1e4: /* PM_PWSTST_MPU */
3004     case 0x220: /* CM_IDLEST1_CORE */
3005     case 0x224: /* CM_IDLEST2_CORE */
3006     case 0x22c: /* CM_IDLEST4_CORE */
3007     case 0x2c8: /* PM_WKDEP_CORE */
3008     case 0x2e4: /* PM_PWSTST_CORE */
3009     case 0x320: /* CM_IDLEST_GFX */
3010     case 0x3e4: /* PM_PWSTST_GFX */
3011     case 0x420: /* CM_IDLEST_WKUP */
3012     case 0x520: /* CM_IDLEST_CKGEN */
3013     case 0x820: /* CM_IDLEST_DSP */
3014     case 0x8e4: /* PM_PWSTST_DSP */
3015         OMAP_RO_REG(addr);
3016         return;
3017
3018     case 0x010: /* PRCM_SYSCONFIG */
3019         s->sysconfig = value & 1;
3020         break;
3021
3022     case 0x018: /* PRCM_IRQSTATUS_MPU */
3023         s->irqst[0] &= ~value;
3024         omap_prcm_int_update(s, 0);
3025         break;
3026     case 0x01c: /* PRCM_IRQENABLE_MPU */
3027         s->irqen[0] = value & 0x3f;
3028         omap_prcm_int_update(s, 0);
3029         break;
3030
3031     case 0x050: /* PRCM_VOLTCTRL */
3032         s->voltctrl = value & 0xf1c3;
3033         break;
3034
3035     case 0x060: /* PRCM_CLKSRC_CTRL */
3036         s->clksrc[0] = value & 0xdb;
3037         /* TODO update clocks */
3038         break;
3039
3040     case 0x070: /* PRCM_CLKOUT_CTRL */
3041         s->clkout[0] = value & 0xbbbb;
3042         /* TODO update clocks */
3043         break;
3044
3045     case 0x078: /* PRCM_CLKEMUL_CTRL */
3046         s->clkemul[0] = value & 1;
3047         /* TODO update clocks */
3048         break;
3049
3050     case 0x080: /* PRCM_CLKCFG_CTRL */
3051         break;
3052
3053     case 0x090: /* PRCM_VOLTSETUP */
3054         s->setuptime[0] = value & 0xffff;
3055         break;
3056     case 0x094: /* PRCM_CLKSSETUP */
3057         s->setuptime[1] = value & 0xffff;
3058         break;
3059
3060     case 0x098: /* PRCM_POLCTRL */
3061         s->clkpol[0] = value & 0x701;
3062         break;
3063
3064     case 0x0b0: /* GENERAL_PURPOSE1 */
3065     case 0x0b4: /* GENERAL_PURPOSE2 */
3066     case 0x0b8: /* GENERAL_PURPOSE3 */
3067     case 0x0bc: /* GENERAL_PURPOSE4 */
3068     case 0x0c0: /* GENERAL_PURPOSE5 */
3069     case 0x0c4: /* GENERAL_PURPOSE6 */
3070     case 0x0c8: /* GENERAL_PURPOSE7 */
3071     case 0x0cc: /* GENERAL_PURPOSE8 */
3072     case 0x0d0: /* GENERAL_PURPOSE9 */
3073     case 0x0d4: /* GENERAL_PURPOSE10 */
3074     case 0x0d8: /* GENERAL_PURPOSE11 */
3075     case 0x0dc: /* GENERAL_PURPOSE12 */
3076     case 0x0e0: /* GENERAL_PURPOSE13 */
3077     case 0x0e4: /* GENERAL_PURPOSE14 */
3078     case 0x0e8: /* GENERAL_PURPOSE15 */
3079     case 0x0ec: /* GENERAL_PURPOSE16 */
3080     case 0x0f0: /* GENERAL_PURPOSE17 */
3081     case 0x0f4: /* GENERAL_PURPOSE18 */
3082     case 0x0f8: /* GENERAL_PURPOSE19 */
3083     case 0x0fc: /* GENERAL_PURPOSE20 */
3084         s->scratch[(addr - 0xb0) >> 2] = value;
3085         break;
3086
3087     case 0x140: /* CM_CLKSEL_MPU */
3088         s->clksel[0] = value & 0x1f;
3089         /* TODO update clocks */
3090         break;
3091     case 0x148: /* CM_CLKSTCTRL_MPU */
3092         s->clkctrl[0] = value & 0x1f;
3093         break;
3094
3095     case 0x158: /* RM_RSTST_MPU */
3096         s->rst[0] &= ~value;
3097         break;
3098     case 0x1c8: /* PM_WKDEP_MPU */
3099         s->wkup[0] = value & 0x15;
3100         break;
3101
3102     case 0x1d4: /* PM_EVGENCTRL_MPU */
3103         s->ev = value & 0x1f;
3104         break;
3105     case 0x1d8: /* PM_EVEGENONTIM_MPU */
3106         s->evtime[0] = value;
3107         break;
3108     case 0x1dc: /* PM_EVEGENOFFTIM_MPU */
3109         s->evtime[1] = value;
3110         break;
3111
3112     case 0x1e0: /* PM_PWSTCTRL_MPU */
3113         s->power[0] = value & 0xc0f;
3114         break;
3115
3116     case 0x200: /* CM_FCLKEN1_CORE */
3117         s->clken[0] = value & 0xbfffffff;
3118         /* TODO update clocks */
3119         /* The EN_EAC bit only gets/puts func_96m_clk.  */
3120         break;
3121     case 0x204: /* CM_FCLKEN2_CORE */
3122         s->clken[1] = value & 0x00000007;
3123         /* TODO update clocks */
3124         break;
3125     case 0x210: /* CM_ICLKEN1_CORE */
3126         s->clken[2] = value & 0xfffffff9;
3127         /* TODO update clocks */
3128         /* The EN_EAC bit only gets/puts core_l4_iclk.  */
3129         break;
3130     case 0x214: /* CM_ICLKEN2_CORE */
3131         s->clken[3] = value & 0x00000007;
3132         /* TODO update clocks */
3133         break;
3134     case 0x21c: /* CM_ICLKEN4_CORE */
3135         s->clken[4] = value & 0x0000001f;
3136         /* TODO update clocks */
3137         break;
3138
3139     case 0x230: /* CM_AUTOIDLE1_CORE */
3140         s->clkidle[0] = value & 0xfffffff9;
3141         /* TODO update clocks */
3142         break;
3143     case 0x234: /* CM_AUTOIDLE2_CORE */
3144         s->clkidle[1] = value & 0x00000007;
3145         /* TODO update clocks */
3146         break;
3147     case 0x238: /* CM_AUTOIDLE3_CORE */
3148         s->clkidle[2] = value & 0x00000007;
3149         /* TODO update clocks */
3150         break;
3151     case 0x23c: /* CM_AUTOIDLE4_CORE */
3152         s->clkidle[3] = value & 0x0000001f;
3153         /* TODO update clocks */
3154         break;
3155
3156     case 0x240: /* CM_CLKSEL1_CORE */
3157         s->clksel[1] = value & 0x0fffbf7f;
3158         /* TODO update clocks */
3159         break;
3160
3161     case 0x244: /* CM_CLKSEL2_CORE */
3162         s->clksel[2] = value & 0x00fffffc;
3163         /* TODO update clocks */
3164         break;
3165
3166     case 0x248: /* CM_CLKSTCTRL_CORE */
3167         s->clkctrl[1] = value & 0x7;
3168         break;
3169
3170     case 0x2a0: /* PM_WKEN1_CORE */
3171         s->wken[0] = value & 0x04667ff8;
3172         break;
3173     case 0x2a4: /* PM_WKEN2_CORE */
3174         s->wken[1] = value & 0x00000005;
3175         break;
3176
3177     case 0x2b0: /* PM_WKST1_CORE */
3178         s->wkst[0] &= ~value;
3179         break;
3180     case 0x2b4: /* PM_WKST2_CORE */
3181         s->wkst[1] &= ~value;
3182         break;
3183
3184     case 0x2e0: /* PM_PWSTCTRL_CORE */
3185         s->power[1] = (value & 0x00fc3f) | (1 << 2);
3186         break;
3187
3188     case 0x300: /* CM_FCLKEN_GFX */
3189         s->clken[5] = value & 6;
3190         /* TODO update clocks */
3191         break;
3192     case 0x310: /* CM_ICLKEN_GFX */
3193         s->clken[6] = value & 1;
3194         /* TODO update clocks */
3195         break;
3196     case 0x340: /* CM_CLKSEL_GFX */
3197         s->clksel[3] = value & 7;
3198         /* TODO update clocks */
3199         break;
3200     case 0x348: /* CM_CLKSTCTRL_GFX */
3201         s->clkctrl[2] = value & 1;
3202         break;
3203     case 0x350: /* RM_RSTCTRL_GFX */
3204         s->rstctrl[0] = value & 1;
3205         /* TODO: reset */
3206         break;
3207     case 0x358: /* RM_RSTST_GFX */
3208         s->rst[1] &= ~value;
3209         break;
3210     case 0x3c8: /* PM_WKDEP_GFX */
3211         s->wkup[1] = value & 0x13;
3212         break;
3213     case 0x3e0: /* PM_PWSTCTRL_GFX */
3214         s->power[2] = (value & 0x00c0f) | (3 << 2);
3215         break;
3216
3217     case 0x400: /* CM_FCLKEN_WKUP */
3218         s->clken[7] = value & 0xd;
3219         /* TODO update clocks */
3220         break;
3221     case 0x410: /* CM_ICLKEN_WKUP */
3222         s->clken[8] = value & 0x3f;
3223         /* TODO update clocks */
3224         break;
3225     case 0x430: /* CM_AUTOIDLE_WKUP */
3226         s->clkidle[4] = value & 0x0000003f;
3227         /* TODO update clocks */
3228         break;
3229     case 0x440: /* CM_CLKSEL_WKUP */
3230         s->clksel[4] = value & 3;
3231         /* TODO update clocks */
3232         break;
3233     case 0x450: /* RM_RSTCTRL_WKUP */
3234         /* TODO: reset */
3235         if (value & 2)
3236             qemu_system_reset_request();
3237         break;
3238     case 0x454: /* RM_RSTTIME_WKUP */
3239         s->rsttime_wkup = value & 0x1fff;
3240         break;
3241     case 0x458: /* RM_RSTST_WKUP */
3242         s->rst[2] &= ~value;
3243         break;
3244     case 0x4a0: /* PM_WKEN_WKUP */
3245         s->wken[2] = value & 0x00000005;
3246         break;
3247     case 0x4b0: /* PM_WKST_WKUP */
3248         s->wkst[2] &= ~value;
3249         break;
3250
3251     case 0x500: /* CM_CLKEN_PLL */
3252         if (value & 0xffffff30)
3253             fprintf(stderr, "%s: write 0s in CM_CLKEN_PLL for "
3254                             "future compatiblity\n", __FUNCTION__);
3255         if ((s->clken[9] ^ value) & 0xcc) {
3256             s->clken[9] &= ~0xcc;
3257             s->clken[9] |= value & 0xcc;
3258             omap_prcm_apll_update(s);
3259         }
3260         if ((s->clken[9] ^ value) & 3) {
3261             s->clken[9] &= ~3;
3262             s->clken[9] |= value & 3;
3263             omap_prcm_dpll_update(s);
3264         }
3265         break;
3266     case 0x530: /* CM_AUTOIDLE_PLL */
3267         s->clkidle[5] = value & 0x000000cf;
3268         /* TODO update clocks */
3269         break;
3270     case 0x540: /* CM_CLKSEL1_PLL */
3271         if (value & 0xfc4000d7)
3272             fprintf(stderr, "%s: write 0s in CM_CLKSEL1_PLL for "
3273                             "future compatiblity\n", __FUNCTION__);
3274         if ((s->clksel[5] ^ value) & 0x003fff00) {
3275             s->clksel[5] = value & 0x03bfff28;
3276             omap_prcm_dpll_update(s);
3277         }
3278         /* TODO update the other clocks */
3279
3280         s->clksel[5] = value & 0x03bfff28;
3281         break;
3282     case 0x544: /* CM_CLKSEL2_PLL */
3283         if (value & ~3)
3284             fprintf(stderr, "%s: write 0s in CM_CLKSEL2_PLL[31:2] for "
3285                             "future compatiblity\n", __FUNCTION__);
3286         if (s->clksel[6] != (value & 3)) {
3287             s->clksel[6] = value & 3;
3288             omap_prcm_dpll_update(s);
3289         }
3290         break;
3291
3292     case 0x800: /* CM_FCLKEN_DSP */
3293         s->clken[10] = value & 0x501;
3294         /* TODO update clocks */
3295         break;
3296     case 0x810: /* CM_ICLKEN_DSP */
3297         s->clken[11] = value & 0x2;
3298         /* TODO update clocks */
3299         break;
3300     case 0x830: /* CM_AUTOIDLE_DSP */
3301         s->clkidle[6] = value & 0x2;
3302         /* TODO update clocks */
3303         break;
3304     case 0x840: /* CM_CLKSEL_DSP */
3305         s->clksel[7] = value & 0x3fff;
3306         /* TODO update clocks */
3307         break;
3308     case 0x848: /* CM_CLKSTCTRL_DSP */
3309         s->clkctrl[3] = value & 0x101;
3310         break;
3311     case 0x850: /* RM_RSTCTRL_DSP */
3312         /* TODO: reset */
3313         break;
3314     case 0x858: /* RM_RSTST_DSP */
3315         s->rst[3] &= ~value;
3316         break;
3317     case 0x8c8: /* PM_WKDEP_DSP */
3318         s->wkup[2] = value & 0x13;
3319         break;
3320     case 0x8e0: /* PM_PWSTCTRL_DSP */
3321         s->power[3] = (value & 0x03017) | (3 << 2);
3322         break;
3323
3324     case 0x8f0: /* PRCM_IRQSTATUS_DSP */
3325         s->irqst[1] &= ~value;
3326         omap_prcm_int_update(s, 1);
3327         break;
3328     case 0x8f4: /* PRCM_IRQENABLE_DSP */
3329         s->irqen[1] = value & 0x7;
3330         omap_prcm_int_update(s, 1);
3331         break;
3332
3333     case 0x8f8: /* PRCM_IRQSTATUS_IVA */
3334         s->irqst[2] &= ~value;
3335         omap_prcm_int_update(s, 2);
3336         break;
3337     case 0x8fc: /* PRCM_IRQENABLE_IVA */
3338         s->irqen[2] = value & 0x7;
3339         omap_prcm_int_update(s, 2);
3340         break;
3341
3342     default:
3343         OMAP_BAD_REG(addr);
3344         return;
3345     }
3346 }
3347
3348 static CPUReadMemoryFunc *omap_prcm_readfn[] = {
3349     omap_badwidth_read32,
3350     omap_badwidth_read32,
3351     omap_prcm_read,
3352 };
3353
3354 static CPUWriteMemoryFunc *omap_prcm_writefn[] = {
3355     omap_badwidth_write32,
3356     omap_badwidth_write32,
3357     omap_prcm_write,
3358 };
3359
3360 static void omap_prcm_reset(struct omap_prcm_s *s)
3361 {
3362     s->sysconfig = 0;
3363     s->irqst[0] = 0;
3364     s->irqst[1] = 0;
3365     s->irqst[2] = 0;
3366     s->irqen[0] = 0;
3367     s->irqen[1] = 0;
3368     s->irqen[2] = 0;
3369     s->voltctrl = 0x1040;
3370     s->ev = 0x14;
3371     s->evtime[0] = 0;
3372     s->evtime[1] = 0;
3373     s->clkctrl[0] = 0;
3374     s->clkctrl[1] = 0;
3375     s->clkctrl[2] = 0;
3376     s->clkctrl[3] = 0;
3377     s->clken[1] = 7;
3378     s->clken[3] = 7;
3379     s->clken[4] = 0;
3380     s->clken[5] = 0;
3381     s->clken[6] = 0;
3382     s->clken[7] = 0xc;
3383     s->clken[8] = 0x3e;
3384     s->clken[9] = 0x0d;
3385     s->clken[10] = 0;
3386     s->clken[11] = 0;
3387     s->clkidle[0] = 0;
3388     s->clkidle[2] = 7;
3389     s->clkidle[3] = 0;
3390     s->clkidle[4] = 0;
3391     s->clkidle[5] = 0x0c;
3392     s->clkidle[6] = 0;
3393     s->clksel[0] = 0x01;
3394     s->clksel[1] = 0x02100121;
3395     s->clksel[2] = 0x00000000;
3396     s->clksel[3] = 0x01;
3397     s->clksel[4] = 0;
3398     s->clksel[7] = 0x0121;
3399     s->wkup[0] = 0x15;
3400     s->wkup[1] = 0x13;
3401     s->wkup[2] = 0x13;
3402     s->wken[0] = 0x04667ff8;
3403     s->wken[1] = 0x00000005;
3404     s->wken[2] = 5;
3405     s->wkst[0] = 0;
3406     s->wkst[1] = 0;
3407     s->wkst[2] = 0;
3408     s->power[0] = 0x00c;
3409     s->power[1] = 4;
3410     s->power[2] = 0x0000c;
3411     s->power[3] = 0x14;
3412     s->rstctrl[0] = 1;
3413     s->rst[3] = 1;
3414     omap_prcm_apll_update(s);
3415     omap_prcm_dpll_update(s);
3416 }
3417
3418 static void omap_prcm_coldreset(struct omap_prcm_s *s)
3419 {
3420     s->setuptime[0] = 0;
3421     s->setuptime[1] = 0;
3422     memset(&s->scratch, 0, sizeof(s->scratch));
3423     s->rst[0] = 0x01;
3424     s->rst[1] = 0x00;
3425     s->rst[2] = 0x01;
3426     s->clken[0] = 0;
3427     s->clken[2] = 0;
3428     s->clkidle[1] = 0;
3429     s->clksel[5] = 0;
3430     s->clksel[6] = 2;
3431     s->clksrc[0] = 0x43;
3432     s->clkout[0] = 0x0303;
3433     s->clkemul[0] = 0;
3434     s->clkpol[0] = 0x100;
3435     s->rsttime_wkup = 0x1002;
3436
3437     omap_prcm_reset(s);
3438 }
3439
3440 struct omap_prcm_s *omap_prcm_init(struct omap_target_agent_s *ta,
3441                 qemu_irq mpu_int, qemu_irq dsp_int, qemu_irq iva_int,
3442                 struct omap_mpu_state_s *mpu)
3443 {
3444     int iomemtype;
3445     struct omap_prcm_s *s = (struct omap_prcm_s *)
3446             qemu_mallocz(sizeof(struct omap_prcm_s));
3447
3448     s->irq[0] = mpu_int;
3449     s->irq[1] = dsp_int;
3450     s->irq[2] = iva_int;
3451     s->mpu = mpu;
3452     omap_prcm_coldreset(s);
3453
3454     iomemtype = l4_register_io_memory(0, omap_prcm_readfn,
3455                     omap_prcm_writefn, s);
3456     omap_l4_attach(ta, 0, iomemtype);
3457     omap_l4_attach(ta, 1, iomemtype);
3458
3459     return s;
3460 }
3461
3462 /* System and Pinout control */
3463 struct omap_sysctl_s {
3464     struct omap_mpu_state_s *mpu;
3465
3466     uint32_t sysconfig;
3467     uint32_t devconfig;
3468     uint32_t psaconfig;
3469     uint32_t padconf[0x45];
3470     uint8_t obs;
3471     uint32_t msuspendmux[5];
3472 };
3473
3474 static uint32_t omap_sysctl_read8(void *opaque, target_phys_addr_t addr)
3475 {
3476
3477     struct omap_sysctl_s *s = (struct omap_sysctl_s *) opaque;
3478     int pad_offset, byte_offset;
3479     int value;
3480
3481     switch (addr) {
3482     case 0x030 ... 0x140:       /* CONTROL_PADCONF - only used in the POP */
3483         pad_offset = (addr - 0x30) >> 2;
3484         byte_offset = (addr - 0x30) & (4 - 1);
3485
3486         value = s->padconf[pad_offset];
3487         value = (value >> (byte_offset * 8)) & 0xff;
3488
3489         return value;
3490
3491     default:
3492         break;
3493     }
3494
3495     OMAP_BAD_REG(addr);
3496     return 0;
3497 }
3498
3499 static uint32_t omap_sysctl_read(void *opaque, target_phys_addr_t addr)
3500 {
3501     struct omap_sysctl_s *s = (struct omap_sysctl_s *) opaque;
3502
3503     switch (addr) {
3504     case 0x000: /* CONTROL_REVISION */
3505         return 0x20;
3506
3507     case 0x010: /* CONTROL_SYSCONFIG */
3508         return s->sysconfig;
3509
3510     case 0x030 ... 0x140:       /* CONTROL_PADCONF - only used in the POP */
3511         return s->padconf[(addr - 0x30) >> 2];
3512
3513     case 0x270: /* CONTROL_DEBOBS */
3514         return s->obs;
3515
3516     case 0x274: /* CONTROL_DEVCONF */
3517         return s->devconfig;
3518
3519     case 0x28c: /* CONTROL_EMU_SUPPORT */
3520         return 0;
3521
3522     case 0x290: /* CONTROL_MSUSPENDMUX_0 */
3523         return s->msuspendmux[0];
3524     case 0x294: /* CONTROL_MSUSPENDMUX_1 */
3525         return s->msuspendmux[1];
3526     case 0x298: /* CONTROL_MSUSPENDMUX_2 */
3527         return s->msuspendmux[2];
3528     case 0x29c: /* CONTROL_MSUSPENDMUX_3 */
3529         return s->msuspendmux[3];
3530     case 0x2a0: /* CONTROL_MSUSPENDMUX_4 */
3531         return s->msuspendmux[4];
3532     case 0x2a4: /* CONTROL_MSUSPENDMUX_5 */
3533         return 0;
3534
3535     case 0x2b8: /* CONTROL_PSA_CTRL */
3536         return s->psaconfig;
3537     case 0x2bc: /* CONTROL_PSA_CMD */
3538     case 0x2c0: /* CONTROL_PSA_VALUE */
3539         return 0;
3540
3541     case 0x2b0: /* CONTROL_SEC_CTRL */
3542         return 0x800000f1;
3543     case 0x2d0: /* CONTROL_SEC_EMU */
3544         return 0x80000015;
3545     case 0x2d4: /* CONTROL_SEC_TAP */
3546         return 0x8000007f;
3547     case 0x2b4: /* CONTROL_SEC_TEST */
3548     case 0x2f0: /* CONTROL_SEC_STATUS */
3549     case 0x2f4: /* CONTROL_SEC_ERR_STATUS */
3550         /* Secure mode is not present on general-pusrpose device.  Outside
3551          * secure mode these values cannot be read or written.  */
3552         return 0;
3553
3554     case 0x2d8: /* CONTROL_OCM_RAM_PERM */
3555         return 0xff;
3556     case 0x2dc: /* CONTROL_OCM_PUB_RAM_ADD */
3557     case 0x2e0: /* CONTROL_EXT_SEC_RAM_START_ADD */
3558     case 0x2e4: /* CONTROL_EXT_SEC_RAM_STOP_ADD */
3559         /* No secure mode so no Extended Secure RAM present.  */
3560         return 0;
3561
3562     case 0x2f8: /* CONTROL_STATUS */
3563         /* Device Type => General-purpose */
3564         return 0x0300;
3565     case 0x2fc: /* CONTROL_GENERAL_PURPOSE_STATUS */
3566
3567     case 0x300: /* CONTROL_RPUB_KEY_H_0 */
3568     case 0x304: /* CONTROL_RPUB_KEY_H_1 */
3569     case 0x308: /* CONTROL_RPUB_KEY_H_2 */
3570     case 0x30c: /* CONTROL_RPUB_KEY_H_3 */
3571         return 0xdecafbad;
3572
3573     case 0x310: /* CONTROL_RAND_KEY_0 */
3574     case 0x314: /* CONTROL_RAND_KEY_1 */
3575     case 0x318: /* CONTROL_RAND_KEY_2 */
3576     case 0x31c: /* CONTROL_RAND_KEY_3 */
3577     case 0x320: /* CONTROL_CUST_KEY_0 */
3578     case 0x324: /* CONTROL_CUST_KEY_1 */
3579     case 0x330: /* CONTROL_TEST_KEY_0 */
3580     case 0x334: /* CONTROL_TEST_KEY_1 */
3581     case 0x338: /* CONTROL_TEST_KEY_2 */
3582     case 0x33c: /* CONTROL_TEST_KEY_3 */
3583     case 0x340: /* CONTROL_TEST_KEY_4 */
3584     case 0x344: /* CONTROL_TEST_KEY_5 */
3585     case 0x348: /* CONTROL_TEST_KEY_6 */
3586     case 0x34c: /* CONTROL_TEST_KEY_7 */
3587     case 0x350: /* CONTROL_TEST_KEY_8 */
3588     case 0x354: /* CONTROL_TEST_KEY_9 */
3589         /* Can only be accessed in secure mode and when C_FieldAccEnable
3590          * bit is set in CONTROL_SEC_CTRL.
3591          * TODO: otherwise an interconnect access error is generated.  */
3592         return 0;
3593     }
3594
3595     OMAP_BAD_REG(addr);
3596     return 0;
3597 }
3598
3599 static void omap_sysctl_write8(void *opaque, target_phys_addr_t addr,
3600                 uint32_t value)
3601 {
3602     struct omap_sysctl_s *s = (struct omap_sysctl_s *) opaque;
3603     int pad_offset, byte_offset;
3604     int prev_value;
3605
3606     switch (addr) {
3607     case 0x030 ... 0x140:       /* CONTROL_PADCONF - only used in the POP */
3608         pad_offset = (addr - 0x30) >> 2;
3609         byte_offset = (addr - 0x30) & (4 - 1);
3610
3611         prev_value = s->padconf[pad_offset];
3612         prev_value &= ~(0xff << (byte_offset * 8));
3613         prev_value |= ((value & 0x1f1f1f1f) << (byte_offset * 8)) & 0x1f1f1f1f;
3614         s->padconf[pad_offset] = prev_value;
3615         break;
3616
3617     default:
3618         OMAP_BAD_REG(addr);
3619         break;
3620     }
3621 }
3622
3623 static void omap_sysctl_write(void *opaque, target_phys_addr_t addr,
3624                 uint32_t value)
3625 {
3626     struct omap_sysctl_s *s = (struct omap_sysctl_s *) opaque;
3627
3628     switch (addr) {
3629     case 0x000: /* CONTROL_REVISION */
3630     case 0x2a4: /* CONTROL_MSUSPENDMUX_5 */
3631     case 0x2c0: /* CONTROL_PSA_VALUE */
3632     case 0x2f8: /* CONTROL_STATUS */
3633     case 0x2fc: /* CONTROL_GENERAL_PURPOSE_STATUS */
3634     case 0x300: /* CONTROL_RPUB_KEY_H_0 */
3635     case 0x304: /* CONTROL_RPUB_KEY_H_1 */
3636     case 0x308: /* CONTROL_RPUB_KEY_H_2 */
3637     case 0x30c: /* CONTROL_RPUB_KEY_H_3 */
3638     case 0x310: /* CONTROL_RAND_KEY_0 */
3639     case 0x314: /* CONTROL_RAND_KEY_1 */
3640     case 0x318: /* CONTROL_RAND_KEY_2 */
3641     case 0x31c: /* CONTROL_RAND_KEY_3 */
3642     case 0x320: /* CONTROL_CUST_KEY_0 */
3643     case 0x324: /* CONTROL_CUST_KEY_1 */
3644     case 0x330: /* CONTROL_TEST_KEY_0 */
3645     case 0x334: /* CONTROL_TEST_KEY_1 */
3646     case 0x338: /* CONTROL_TEST_KEY_2 */
3647     case 0x33c: /* CONTROL_TEST_KEY_3 */
3648     case 0x340: /* CONTROL_TEST_KEY_4 */
3649     case 0x344: /* CONTROL_TEST_KEY_5 */
3650     case 0x348: /* CONTROL_TEST_KEY_6 */
3651     case 0x34c: /* CONTROL_TEST_KEY_7 */
3652     case 0x350: /* CONTROL_TEST_KEY_8 */
3653     case 0x354: /* CONTROL_TEST_KEY_9 */
3654         OMAP_RO_REG(addr);
3655         return;
3656
3657     case 0x010: /* CONTROL_SYSCONFIG */
3658         s->sysconfig = value & 0x1e;
3659         break;
3660
3661     case 0x030 ... 0x140:       /* CONTROL_PADCONF - only used in the POP */
3662         /* XXX: should check constant bits */
3663         s->padconf[(addr - 0x30) >> 2] = value & 0x1f1f1f1f;
3664         break;
3665
3666     case 0x270: /* CONTROL_DEBOBS */
3667         s->obs = value & 0xff;
3668         break;
3669
3670     case 0x274: /* CONTROL_DEVCONF */
3671         s->devconfig = value & 0xffffc7ff;
3672         break;
3673
3674     case 0x28c: /* CONTROL_EMU_SUPPORT */
3675         break;
3676
3677     case 0x290: /* CONTROL_MSUSPENDMUX_0 */
3678         s->msuspendmux[0] = value & 0x3fffffff;
3679         break;
3680     case 0x294: /* CONTROL_MSUSPENDMUX_1 */
3681         s->msuspendmux[1] = value & 0x3fffffff;
3682         break;
3683     case 0x298: /* CONTROL_MSUSPENDMUX_2 */
3684         s->msuspendmux[2] = value & 0x3fffffff;
3685         break;
3686     case 0x29c: /* CONTROL_MSUSPENDMUX_3 */
3687         s->msuspendmux[3] = value & 0x3fffffff;
3688         break;
3689     case 0x2a0: /* CONTROL_MSUSPENDMUX_4 */
3690         s->msuspendmux[4] = value & 0x3fffffff;
3691         break;
3692
3693     case 0x2b8: /* CONTROL_PSA_CTRL */
3694         s->psaconfig = value & 0x1c;
3695         s->psaconfig |= (value & 0x20) ? 2 : 1;
3696         break;
3697     case 0x2bc: /* CONTROL_PSA_CMD */
3698         break;
3699
3700     case 0x2b0: /* CONTROL_SEC_CTRL */
3701     case 0x2b4: /* CONTROL_SEC_TEST */
3702     case 0x2d0: /* CONTROL_SEC_EMU */
3703     case 0x2d4: /* CONTROL_SEC_TAP */
3704     case 0x2d8: /* CONTROL_OCM_RAM_PERM */
3705     case 0x2dc: /* CONTROL_OCM_PUB_RAM_ADD */
3706     case 0x2e0: /* CONTROL_EXT_SEC_RAM_START_ADD */
3707     case 0x2e4: /* CONTROL_EXT_SEC_RAM_STOP_ADD */
3708     case 0x2f0: /* CONTROL_SEC_STATUS */
3709     case 0x2f4: /* CONTROL_SEC_ERR_STATUS */
3710         break;
3711
3712     default:
3713         OMAP_BAD_REG(addr);
3714         return;
3715     }
3716 }
3717
3718 static CPUReadMemoryFunc *omap_sysctl_readfn[] = {
3719     omap_sysctl_read8,
3720     omap_badwidth_read32,       /* TODO */
3721     omap_sysctl_read,
3722 };
3723
3724 static CPUWriteMemoryFunc *omap_sysctl_writefn[] = {
3725     omap_sysctl_write8,
3726     omap_badwidth_write32,      /* TODO */
3727     omap_sysctl_write,
3728 };
3729
3730 static void omap_sysctl_reset(struct omap_sysctl_s *s)
3731 {
3732     /* (power-on reset) */
3733     s->sysconfig = 0;
3734     s->obs = 0;
3735     s->devconfig = 0x0c000000;
3736     s->msuspendmux[0] = 0x00000000;
3737     s->msuspendmux[1] = 0x00000000;
3738     s->msuspendmux[2] = 0x00000000;
3739     s->msuspendmux[3] = 0x00000000;
3740     s->msuspendmux[4] = 0x00000000;
3741     s->psaconfig = 1;
3742
3743     s->padconf[0x00] = 0x000f0f0f;
3744     s->padconf[0x01] = 0x00000000;
3745     s->padconf[0x02] = 0x00000000;
3746     s->padconf[0x03] = 0x00000000;
3747     s->padconf[0x04] = 0x00000000;
3748     s->padconf[0x05] = 0x00000000;
3749     s->padconf[0x06] = 0x00000000;
3750     s->padconf[0x07] = 0x00000000;
3751     s->padconf[0x08] = 0x08080800;
3752     s->padconf[0x09] = 0x08080808;
3753     s->padconf[0x0a] = 0x08080808;
3754     s->padconf[0x0b] = 0x08080808;
3755     s->padconf[0x0c] = 0x08080808;
3756     s->padconf[0x0d] = 0x08080800;
3757     s->padconf[0x0e] = 0x08080808;
3758     s->padconf[0x0f] = 0x08080808;
3759     s->padconf[0x10] = 0x18181808;      /* | 0x07070700 if SBoot3 */
3760     s->padconf[0x11] = 0x18181818;      /* | 0x07070707 if SBoot3 */
3761     s->padconf[0x12] = 0x18181818;      /* | 0x07070707 if SBoot3 */
3762     s->padconf[0x13] = 0x18181818;      /* | 0x07070707 if SBoot3 */
3763     s->padconf[0x14] = 0x18181818;      /* | 0x00070707 if SBoot3 */
3764     s->padconf[0x15] = 0x18181818;
3765     s->padconf[0x16] = 0x18181818;      /* | 0x07000000 if SBoot3 */
3766     s->padconf[0x17] = 0x1f001f00;
3767     s->padconf[0x18] = 0x1f1f1f1f;
3768     s->padconf[0x19] = 0x00000000;
3769     s->padconf[0x1a] = 0x1f180000;
3770     s->padconf[0x1b] = 0x00001f1f;
3771     s->padconf[0x1c] = 0x1f001f00;
3772     s->padconf[0x1d] = 0x00000000;
3773     s->padconf[0x1e] = 0x00000000;
3774     s->padconf[0x1f] = 0x08000000;
3775     s->padconf[0x20] = 0x08080808;
3776     s->padconf[0x21] = 0x08080808;
3777     s->padconf[0x22] = 0x0f080808;
3778     s->padconf[0x23] = 0x0f0f0f0f;
3779     s->padconf[0x24] = 0x000f0f0f;
3780     s->padconf[0x25] = 0x1f1f1f0f;
3781     s->padconf[0x26] = 0x080f0f1f;
3782     s->padconf[0x27] = 0x070f1808;
3783     s->padconf[0x28] = 0x0f070707;
3784     s->padconf[0x29] = 0x000f0f1f;
3785     s->padconf[0x2a] = 0x0f0f0f1f;
3786     s->padconf[0x2b] = 0x08000000;
3787     s->padconf[0x2c] = 0x0000001f;
3788     s->padconf[0x2d] = 0x0f0f1f00;
3789     s->padconf[0x2e] = 0x1f1f0f0f;
3790     s->padconf[0x2f] = 0x0f1f1f1f;
3791     s->padconf[0x30] = 0x0f0f0f0f;
3792     s->padconf[0x31] = 0x0f1f0f1f;
3793     s->padconf[0x32] = 0x0f0f0f0f;
3794     s->padconf[0x33] = 0x0f1f0f1f;
3795     s->padconf[0x34] = 0x1f1f0f0f;
3796     s->padconf[0x35] = 0x0f0f1f1f;
3797     s->padconf[0x36] = 0x0f0f1f0f;
3798     s->padconf[0x37] = 0x0f0f0f0f;
3799     s->padconf[0x38] = 0x1f18180f;
3800     s->padconf[0x39] = 0x1f1f1f1f;
3801     s->padconf[0x3a] = 0x00001f1f;
3802     s->padconf[0x3b] = 0x00000000;
3803     s->padconf[0x3c] = 0x00000000;
3804     s->padconf[0x3d] = 0x0f0f0f0f;
3805     s->padconf[0x3e] = 0x18000f0f;
3806     s->padconf[0x3f] = 0x00070000;
3807     s->padconf[0x40] = 0x00000707;
3808     s->padconf[0x41] = 0x0f1f0700;
3809     s->padconf[0x42] = 0x1f1f070f;
3810     s->padconf[0x43] = 0x0008081f;
3811     s->padconf[0x44] = 0x00000800;
3812 }
3813
3814 struct omap_sysctl_s *omap_sysctl_init(struct omap_target_agent_s *ta,
3815                 omap_clk iclk, struct omap_mpu_state_s *mpu)
3816 {
3817     int iomemtype;
3818     struct omap_sysctl_s *s = (struct omap_sysctl_s *)
3819             qemu_mallocz(sizeof(struct omap_sysctl_s));
3820
3821     s->mpu = mpu;
3822     omap_sysctl_reset(s);
3823
3824     iomemtype = l4_register_io_memory(0, omap_sysctl_readfn,
3825                     omap_sysctl_writefn, s);
3826     omap_l4_attach(ta, 0, iomemtype);
3827
3828     return s;
3829 }
3830
3831 /* SDRAM Controller Subsystem */
3832 struct omap_sdrc_s {
3833     uint8_t config;
3834     uint32_t cscfg;
3835     uint32_t sharing;
3836     uint32_t dlla_ctrl;
3837     uint32_t power_reg;
3838     struct {
3839         uint32_t mcfg;
3840         uint32_t mr;
3841         uint32_t emr2;
3842         uint32_t actim_ctrla;
3843         uint32_t actim_ctrlb;
3844         uint32_t rfr_ctrl;
3845         uint32_t manual;
3846     } cs[2];
3847 };
3848
3849 static void omap_sdrc_save_state(QEMUFile *f, void *opaque)
3850 {
3851     struct omap_sdrc_s *s = (struct omap_sdrc_s *)opaque;
3852     int i;
3853     
3854     qemu_put_byte(f, s->config);
3855     qemu_put_be32(f, s->cscfg);
3856     qemu_put_be32(f, s->sharing);
3857     qemu_put_be32(f, s->dlla_ctrl);
3858     qemu_put_be32(f, s->power_reg);
3859     for (i = 0; i < 2; i++) {
3860         qemu_put_be32(f, s->cs[i].mcfg);
3861         qemu_put_be32(f, s->cs[i].mr);
3862         qemu_put_be32(f, s->cs[i].emr2);
3863         qemu_put_be32(f, s->cs[i].actim_ctrla);
3864         qemu_put_be32(f, s->cs[i].actim_ctrlb);
3865         qemu_put_be32(f, s->cs[i].rfr_ctrl);
3866         qemu_put_be32(f, s->cs[i].manual);
3867     }
3868 }
3869
3870 static int omap_sdrc_load_state(QEMUFile *f, void *opaque, int version_id)
3871 {
3872     struct omap_sdrc_s *s = (struct omap_sdrc_s *)opaque;
3873     int i;
3874     
3875     if (version_id)
3876         return -EINVAL;
3877     
3878     s->config = qemu_get_byte(f);
3879     s->cscfg = qemu_get_be32(f);
3880     s->sharing = qemu_get_be32(f);
3881     s->dlla_ctrl = qemu_get_be32(f);
3882     s->power_reg = qemu_get_be32(f);
3883     for (i = 0; i < 2; i++) {
3884         s->cs[i].mcfg = qemu_get_be32(f);
3885         s->cs[i].mr = qemu_get_be32(f);
3886         s->cs[i].emr2 = qemu_get_be32(f);
3887         s->cs[i].actim_ctrla = qemu_get_be32(f);
3888         s->cs[i].actim_ctrlb = qemu_get_be32(f);
3889         s->cs[i].rfr_ctrl = qemu_get_be32(f);
3890         s->cs[i].manual = qemu_get_be32(f);
3891     }
3892     
3893     return 0;
3894 }
3895
3896 static void omap_sdrc_reset(struct omap_sdrc_s *s)
3897 {
3898     s->config    = 0x10;
3899     s->cscfg     = 0x4;
3900     s->sharing   = 0; // TODO: copy from system control module
3901     s->dlla_ctrl = 0;
3902     s->power_reg = 0x85;
3903     s->cs[0].mcfg        = s->cs[1].mcfg        = 0; // TODO: copy from system control module!
3904     s->cs[0].mr          = s->cs[1].mr          = 0x0024;
3905     s->cs[0].emr2        = s->cs[1].emr2        = 0;
3906     s->cs[0].actim_ctrla = s->cs[1].actim_ctrla = 0;
3907     s->cs[0].actim_ctrlb = s->cs[1].actim_ctrlb = 0;
3908     s->cs[0].rfr_ctrl    = s->cs[1].rfr_ctrl    = 0;
3909     s->cs[0].manual      = s->cs[1].manual      = 0;
3910 }
3911
3912 static uint32_t omap_sdrc_read(void *opaque, target_phys_addr_t addr)
3913 {
3914     struct omap_sdrc_s *s = (struct omap_sdrc_s *) opaque;
3915     int cs = 0;
3916
3917     switch (addr) {
3918     case 0x00:  /* SDRC_REVISION */
3919         return 0x20;
3920
3921     case 0x10:  /* SDRC_SYSCONFIG */
3922         return s->config;
3923
3924     case 0x14:  /* SDRC_SYSSTATUS */
3925         return 1; /* RESETDONE */
3926
3927     case 0x40:  /* SDRC_CS_CFG */
3928         return s->cscfg;
3929
3930     case 0x44:  /* SDRC_SHARING */
3931         return s->sharing;
3932             
3933     case 0x48:  /* SDRC_ERR_ADDR */
3934         return 0;
3935
3936     case 0x4c:  /* SDRC_ERR_TYPE */
3937         return 0x8;
3938             
3939     case 0x60:  /* SDRC_DLLA_SCTRL */
3940         return s->dlla_ctrl;
3941         
3942     case 0x64:  /* SDRC_DLLA_STATUS */
3943         return ~(s->dlla_ctrl & 0x4);
3944
3945     case 0x68:  /* SDRC_DLLB_CTRL */
3946     case 0x6c:  /* SDRC_DLLB_STATUS */
3947         return 0x00;
3948     
3949     case 0x70:  /* SDRC_POWER */
3950         return s->power_reg;
3951
3952     case 0xb0 ... 0xd8:
3953         cs = 1;
3954         addr -= 0x30;
3955         /* fall through */
3956     case 0x80 ... 0xa8:
3957         switch (addr & 0x3f) {
3958         case 0x00: /* SDRC_MCFG_x */
3959             return s->cs[cs].mcfg;
3960         case 0x04: /* SDRC_MR_x */
3961             return s->cs[cs].mr;
3962         case 0x08: /* SDRC_EMR1_x */
3963             return 0x00;
3964         case 0x0c: /* SDRC_EMR2_x */
3965             return s->cs[cs].emr2;
3966         case 0x10: /* SDRC_EMR3_x */
3967             return 0x00;
3968         case 0x14:
3969             if (cs)
3970                 return s->cs[1].actim_ctrla; /* SDRC_ACTIM_CTRLA_1 */
3971             return 0x00;                     /* SDRC_DCDL1_CTRL */
3972         case 0x18:
3973             if (cs)
3974                 return s->cs[1].actim_ctrlb; /* SDRC_ACTIM_CTRLB_1 */
3975             return 0x00;                     /* SDRC_DCDL2_CTRL */
3976         case 0x1c:
3977             if (!cs)
3978                 return s->cs[0].actim_ctrla; /* SDRC_ACTIM_CTRLA_0 */
3979             break;
3980         case 0x20:
3981             if (!cs)
3982                 return s->cs[0].actim_ctrlb; /* SDRC_ACTIM_CTRLB_0 */
3983             break;
3984         case 0x24: /* SDRC_RFR_CTRL_x */
3985             return s->cs[cs].rfr_ctrl;
3986         case 0x28: /* SDRC_MANUAL_x */
3987             return s->cs[cs].manual;
3988         default:
3989             break;
3990         }
3991         addr += cs * 0x30; // restore address to get correct error messages
3992         break;
3993
3994     default:
3995         break;
3996     }
3997
3998     OMAP_BAD_REG(addr);
3999     return 0;
4000 }
4001
4002 static void omap_sdrc_write(void *opaque, target_phys_addr_t addr,
4003                 uint32_t value)
4004 {
4005     struct omap_sdrc_s *s = (struct omap_sdrc_s *) opaque;
4006     int cs = 0;
4007
4008     switch (addr) {
4009     case 0x00:  /* SDRC_REVISION */
4010     case 0x14:  /* SDRC_SYSSTATUS */
4011     case 0x48:  /* SDRC_ERR_ADDR */
4012     case 0x64:  /* SDRC_DLLA_STATUS */
4013     case 0x6c:  /* SDRC_DLLB_STATUS */
4014         OMAP_RO_REGV(addr, value);
4015         break;
4016
4017     case 0x10:  /* SDRC_SYSCONFIG */
4018         /* ignore invalid idle mode settings */
4019         /*if ((value >> 3) != 0x2)
4020             fprintf(stderr, "%s: bad SDRAM idle mode %i for SDRC_SYSCONFIG (full value 0x%08x)\n",
4021                             __FUNCTION__, value >> 3, value);*/
4022         if (value & 2)
4023             omap_sdrc_reset(s);
4024         s->config = value & 0x18;
4025         break;
4026
4027     case 0x40:  /* SDRC_CS_CFG */
4028         s->cscfg = value & 0x30f;
4029         break;
4030
4031     case 0x44:  /* SDRC_SHARING */
4032         if (!(s->sharing & 0x40000000)) /* LOCK */
4033             s->sharing = value & 0x40007f00;
4034         break;
4035
4036     case 0x4c:  /* SDRC_ERR_TYPE */
4037         OMAP_BAD_REGV(addr, value);
4038         break;
4039
4040     case 0x60:  /* SDRC_DLLA_CTRL */
4041         s->dlla_ctrl = value & 0xffff00fe;
4042         break;
4043
4044     case 0x68:  /* SDRC_DLLB_CTRL */
4045         /* silently ignore */
4046         /*OMAP_BAD_REGV(addr, value);*/
4047         break;
4048         
4049     case 0x70:  /* SDRC_POWER_REG */
4050         s->power_reg = value & 0x04fffffd;
4051         break;
4052
4053     case 0xb0 ... 0xd8:
4054         cs = 1;
4055         addr -= 0x30;
4056         /* fall through */
4057     case 0x80 ... 0xa8:
4058         switch (addr & 0x3f) {
4059         case 0x00: /* SDRC_MCFG_x */
4060             if (!(s->cs[cs].mcfg & 0x40000000)) { /* LOCKSTATUS */
4061                 if (value & 0x00080000) /* ADDRMUXLEGACY */
4062                     s->cs[cs].mcfg = value & 0x477bffdf;
4063                 else
4064                     s->cs[cs].mcfg = value & 0x41fbffdf; // ????
4065             }
4066             break;
4067         case 0x04: /* SDRC_MR_x */
4068             s->cs[cs].mr = value & 0xfff;
4069             break;
4070         case 0x08: /* SDRC_EMR1_x */
4071             break;
4072         case 0x0c: /* SDRC_EMR2_x */
4073             s->cs[cs].emr2 = value & 0xfff;
4074             break;
4075         case 0x10: /* SDRC_EMR3_x */
4076             break;
4077         case 0x14:
4078             if (cs)
4079                 s->cs[1].actim_ctrla = value & 0xffffffdf; /* SDRC_ACTIM_CTRLA_1 */
4080             break;                                         /* SDRC_DCDL1_CTRL */
4081         case 0x18:
4082             if (cs)
4083                 s->cs[1].actim_ctrlb = value & 0x000377ff; /* SDRC_ACTIM_CTRLB_1 */
4084             break;                                         /* SDRC_DCDL2_CTRL */
4085         case 0x1c:
4086             if (!cs)
4087                 s->cs[0].actim_ctrla = value & 0xffffffdf; /* SDRC_ACTIM_CTRLA_0 */
4088             else
4089                 OMAP_BAD_REGV(addr + 0x30, value);
4090             break;
4091         case 0x20:
4092             if (!cs)
4093                 s->cs[0].actim_ctrlb = value & 0x000377ff; /* SDRC_ACTIM_CTRLB_0 */
4094             else
4095                 OMAP_BAD_REGV(addr + 0x30, value);
4096             break;
4097         case 0x24: /* SDRC_RFR_CTRL_x */
4098             s->cs[cs].rfr_ctrl = value & 0x00ffff03;
4099             break;
4100         case 0x28: /* SDRC_MANUAL_x */
4101             s->cs[cs].manual = value & 0xffff000f;
4102             break;
4103         default:
4104             OMAP_BAD_REGV(addr + cs * 0x30, value);
4105             break;
4106         }
4107         break;
4108
4109     default:
4110         OMAP_BAD_REGV(addr, value);
4111         break;
4112     }
4113 }
4114
4115 static CPUReadMemoryFunc *omap_sdrc_readfn[] = {
4116     omap_badwidth_read32,
4117     omap_badwidth_read32,
4118     omap_sdrc_read,
4119 };
4120
4121 static CPUWriteMemoryFunc *omap_sdrc_writefn[] = {
4122     omap_badwidth_write32,
4123     omap_badwidth_write32,
4124     omap_sdrc_write,
4125 };
4126
4127 struct omap_sdrc_s *omap_sdrc_init(target_phys_addr_t base)
4128 {
4129     int iomemtype;
4130     struct omap_sdrc_s *s = (struct omap_sdrc_s *)
4131             qemu_mallocz(sizeof(struct omap_sdrc_s));
4132
4133     omap_sdrc_reset(s);
4134
4135     iomemtype = cpu_register_io_memory(0, omap_sdrc_readfn,
4136                     omap_sdrc_writefn, s);
4137     cpu_register_physical_memory(base, 0x1000, iomemtype);
4138
4139     register_savevm("omap_sdrc", -1, 0,
4140                     omap_sdrc_save_state, omap_sdrc_load_state, s);
4141     return s;
4142 }
4143
4144 /* General-Purpose Memory Controller */
4145 struct omap_gpmc_s {
4146     qemu_irq irq;
4147
4148     uint8_t revision;
4149     uint8_t sysconfig;
4150     uint16_t irqst;
4151     uint16_t irqen;
4152     uint16_t timeout;
4153     uint16_t config;
4154     uint32_t prefconfig[2];
4155     int prefcontrol;
4156     int preffifo;
4157     int prefcount;
4158     struct omap_gpmc_cs_file_s {
4159         uint32_t config[7];
4160         target_phys_addr_t base;
4161         size_t size;
4162         int iomemtype;
4163         void (*base_update)(void *opaque, target_phys_addr_t new);
4164         void (*unmap)(void *opaque);
4165         void *opaque;
4166     } cs_file[8];
4167     int ecc_cs;
4168     int ecc_ptr;
4169     uint32_t ecc_cfg;
4170     ECCState ecc[9];
4171 };
4172
4173 static void omap_gpmc_int_update(struct omap_gpmc_s *s)
4174 {
4175     qemu_set_irq(s->irq, s->irqen & s->irqst);
4176 }
4177
4178 static void omap_gpmc_cs_map(struct omap_gpmc_cs_file_s *f, int base, int mask)
4179 {
4180     /* TODO: check for overlapping regions and report access errors */
4181     if ((mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf) ||
4182                     (base < 0 || base >= 0x40) ||
4183                     (base & 0x0f & ~mask)) {
4184         fprintf(stderr, "%s: wrong cs address mapping/decoding!\n",
4185                         __FUNCTION__);
4186         return;
4187     }
4188
4189     if (!f->opaque)
4190         return;
4191
4192     f->base = base << 24;
4193     f->size = (0x0fffffff & ~(mask << 24)) + 1;
4194     /* TODO: rather than setting the size of the mapping (which should be
4195      * constant), the mask should cause wrapping of the address space, so
4196      * that the same memory becomes accessible at every <i>size</i> bytes
4197      * starting from <i>base</i>.  */
4198     if (f->iomemtype)
4199         cpu_register_physical_memory(f->base, f->size, f->iomemtype);
4200
4201     if (f->base_update)
4202         f->base_update(f->opaque, f->base);
4203 }
4204
4205 static void omap_gpmc_cs_unmap(struct omap_gpmc_cs_file_s *f)
4206 {
4207     if (f->size) {
4208         if (f->unmap)
4209             f->unmap(f->opaque);
4210         if (f->iomemtype)
4211             cpu_register_physical_memory(f->base, f->size, IO_MEM_UNASSIGNED);
4212         f->base = 0;
4213         f->size = 0;
4214     }
4215 }
4216
4217 static void omap_gpmc_reset(struct omap_gpmc_s *s)
4218 {
4219     int i;
4220
4221     s->sysconfig = 0;
4222     s->irqst = 0;
4223     s->irqen = 0;
4224     omap_gpmc_int_update(s);
4225     s->timeout = 0;
4226     s->config = 0xa00;
4227     s->prefconfig[0] = 0x00004000;
4228     s->prefconfig[1] = 0x00000000;
4229     s->prefcontrol = 0;
4230     s->preffifo = 0;
4231     s->prefcount = 0;
4232     for (i = 0; i < 8; i ++) {
4233         if (s->cs_file[i].config[6] & (1 << 6))                 /* CSVALID */
4234             omap_gpmc_cs_unmap(s->cs_file + i);
4235         s->cs_file[i].config[0] = i ? 1 << 12 : 0;
4236         s->cs_file[i].config[1] = 0x101001;
4237         s->cs_file[i].config[2] = 0x020201;
4238         s->cs_file[i].config[3] = 0x10031003;
4239         s->cs_file[i].config[4] = 0x10f1111;
4240         s->cs_file[i].config[5] = 0;
4241         s->cs_file[i].config[6] = 0xf00 | (i ? 0 : 1 << 6);
4242         if (s->cs_file[i].config[6] & (1 << 6))                 /* CSVALID */
4243             omap_gpmc_cs_map(&s->cs_file[i],
4244                             s->cs_file[i].config[6] & 0x3f,     /* MASKADDR */
4245                         (s->cs_file[i].config[6] >> 8 & 0xf));  /* BASEADDR */
4246     }
4247     omap_gpmc_cs_map(s->cs_file, 0, 0xf);
4248     s->ecc_cs = 0;
4249     s->ecc_ptr = 0;
4250     s->ecc_cfg = 0x3fcff000;
4251     for (i = 0; i < 9; i ++)
4252         ecc_reset(&s->ecc[i]);
4253 }
4254
4255 static void omap_gpmc_save_state(QEMUFile *f, void *opaque)
4256 {
4257     struct omap_gpmc_s *s = (struct omap_gpmc_s *)opaque;
4258     int i, j;
4259     
4260     qemu_put_byte(f, s->sysconfig);
4261     qemu_put_be16(f, s->irqst);
4262     qemu_put_be16(f, s->irqen);
4263     qemu_put_be16(f, s->timeout);
4264     qemu_put_be16(f, s->config);
4265     qemu_put_be32(f, s->prefconfig[0]);
4266     qemu_put_be32(f, s->prefconfig[1]);
4267     qemu_put_sbe32(f, s->prefcontrol);
4268     qemu_put_sbe32(f, s->preffifo);
4269     qemu_put_sbe32(f, s->prefcount);
4270     for (i = 0; i < 8; i++)
4271         for (j = 0; j < 7; j++)
4272             qemu_put_be32(f, s->cs_file[i].config[j]);
4273     qemu_put_sbe32(f, s->ecc_cs);
4274     qemu_put_sbe32(f, s->ecc_ptr);
4275     qemu_put_be32(f, s->ecc_cfg);
4276     for (i = 0; i < 9; i++) {
4277         qemu_put_byte(f, s->ecc[i].cp);
4278         qemu_put_be16(f, s->ecc[i].lp[0]);
4279         qemu_put_be16(f, s->ecc[i].lp[1]);
4280         qemu_put_be16(f, s->ecc[i].count);
4281     }
4282 }
4283
4284 static int omap_gpmc_load_state(QEMUFile *f, void *opaque, int version_id)
4285 {
4286     struct omap_gpmc_s *s = (struct omap_gpmc_s *)opaque;
4287     int i, j;
4288     
4289     if (version_id)
4290         return -EINVAL;
4291     
4292     s->sysconfig = qemu_get_byte(f);
4293     s->irqst = qemu_get_be16(f);
4294     s->irqen = qemu_get_be16(f);
4295     s->timeout = qemu_get_be16(f);
4296     s->config = qemu_get_be16(f);
4297     s->prefconfig[0] = qemu_get_be32(f);
4298     s->prefconfig[1] = qemu_get_be32(f);
4299     s->prefcontrol = qemu_get_sbe32(f);
4300     s->preffifo = qemu_get_sbe32(f);
4301     s->prefcount = qemu_get_sbe32(f);
4302     for (i = 0; i < 8; i++) {
4303         for (j = 0; j < 7; j++)
4304             s->cs_file[i].config[j] = qemu_get_be32(f);
4305         if (s->cs_file[i].config[6] & (1 << 6)) /* CSVALID */
4306             omap_gpmc_cs_map(&s->cs_file[i],
4307                              s->cs_file[i].config[6] & 0x3f, /* MASKADDR */
4308                              (s->cs_file[i].config[6] >> 8 & 0xf));     /* BASE */
4309     }        
4310     s->ecc_cs = qemu_get_sbe32(f);
4311     s->ecc_ptr = qemu_get_sbe32(f);
4312     s->ecc_cfg = qemu_get_be32(f);
4313     for (i = 0; i < 9; i++) {
4314         s->ecc[i].cp = qemu_get_byte(f);
4315         s->ecc[i].lp[0] = qemu_get_be16(f);
4316         s->ecc[i].lp[1] = qemu_get_be16(f);
4317         s->ecc[i].count = qemu_get_be16(f);
4318     }
4319     
4320     omap_gpmc_int_update(s);
4321     
4322     return 0;
4323 }
4324
4325 static uint32_t omap_gpmc_read(void *opaque, target_phys_addr_t addr)
4326 {
4327     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4328     int cs;
4329     struct omap_gpmc_cs_file_s *f;
4330     uint32_t x1, x2, x3, x4;
4331
4332     switch (addr) {
4333     case 0x000: /* GPMC_REVISION */
4334         return s->revision;
4335
4336     case 0x010: /* GPMC_SYSCONFIG */
4337         return s->sysconfig;
4338
4339     case 0x014: /* GPMC_SYSSTATUS */
4340         return 1;                                               /* RESETDONE */
4341
4342     case 0x018: /* GPMC_IRQSTATUS */
4343         return s->irqst;
4344
4345     case 0x01c: /* GPMC_IRQENABLE */
4346         return s->irqen;
4347
4348     case 0x040: /* GPMC_TIMEOUT_CONTROL */
4349         return s->timeout;
4350
4351     case 0x044: /* GPMC_ERR_ADDRESS */
4352     case 0x048: /* GPMC_ERR_TYPE */
4353         return 0;
4354
4355     case 0x050: /* GPMC_CONFIG */
4356         return s->config;
4357
4358     case 0x054: /* GPMC_STATUS */
4359         return 0x001;
4360
4361     case 0x060 ... 0x1d4:
4362         cs = (addr - 0x060) / 0x30;
4363         addr -= cs * 0x30;
4364         f = s->cs_file + cs;
4365         switch (addr) {
4366             case 0x60:  /* GPMC_CONFIG1 */
4367                 return f->config[0];
4368             case 0x64:  /* GPMC_CONFIG2 */
4369                 return f->config[1];
4370             case 0x68:  /* GPMC_CONFIG3 */
4371                 return f->config[2];
4372             case 0x6c:  /* GPMC_CONFIG4 */
4373                 return f->config[3];
4374             case 0x70:  /* GPMC_CONFIG5 */
4375                 return f->config[4];
4376             case 0x74:  /* GPMC_CONFIG6 */
4377                 return f->config[5];
4378             case 0x78:  /* GPMC_CONFIG7 */
4379                 return f->config[6];
4380             case 0x84:  /* GPMC_NAND_DATA */
4381                 if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4382                     && f->opaque) {
4383                     nand_setpins(f->opaque, 0, 0, 0, 1, 0);
4384                     switch (((f->config[0] >> 12) & 3)) {
4385                         case 0: /* 8bit */
4386                             x1 = nand_getio(f->opaque);
4387                             x2 = nand_getio(f->opaque);
4388                             x3 = nand_getio(f->opaque);
4389                             x4 = nand_getio(f->opaque);
4390                             return (x4 << 24) | (x3 << 16) | (x2 << 8) | x1;
4391                         case 1: /* 16bit */
4392                             x1 = nand_getio(f->opaque);
4393                             x2 = nand_getio(f->opaque);
4394                             return (x2 << 16) | x1;
4395                         default:
4396                             return 0;
4397                     }
4398                 }
4399                 return 0;
4400             default:
4401                 break;
4402         }
4403         break;
4404
4405     case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
4406         return s->prefconfig[0];
4407     case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
4408         return s->prefconfig[1];
4409     case 0x1ec: /* GPMC_PREFETCH_CONTROL */
4410         return s->prefcontrol;
4411     case 0x1f0: /* GPMC_PREFETCH_STATUS */
4412         return (s->preffifo << 24) |
4413                 ((s->preffifo >
4414                   ((s->prefconfig[0] >> 8) & 0x7f) ? 1 : 0) << 16) |
4415                 s->prefcount;
4416
4417     case 0x1f4: /* GPMC_ECC_CONFIG */
4418         return s->ecc_cs;
4419     case 0x1f8: /* GPMC_ECC_CONTROL */
4420         return s->ecc_ptr;
4421     case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
4422         return s->ecc_cfg;
4423     case 0x200 ... 0x220:       /* GPMC_ECC_RESULT */
4424         cs = (addr & 0x1f) >> 2;
4425         /* TODO: check correctness */
4426         return
4427                 ((s->ecc[cs].cp    &  0x07) <<  0) |
4428                 ((s->ecc[cs].cp    &  0x38) << 13) |
4429                 ((s->ecc[cs].lp[0] & 0x1ff) <<  3) |
4430                 ((s->ecc[cs].lp[1] & 0x1ff) << 19);
4431
4432     case 0x230: /* GPMC_TESTMODE_CTRL */
4433         return 0;
4434     case 0x234: /* GPMC_PSA_LSB */
4435     case 0x238: /* GPMC_PSA_MSB */
4436         return 0x00000000;
4437     }
4438
4439     OMAP_BAD_REG(addr);
4440     return 0;
4441 }
4442
4443 static uint32_t omap_gpmc_read8(void *opaque, target_phys_addr_t addr)
4444 {
4445     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4446     int cs;
4447     struct omap_gpmc_cs_file_s *f;
4448     
4449     switch (addr) {
4450         case 0x060 ... 0x1d4:
4451             cs = (addr - 0x060) / 0x30;
4452             addr -= cs * 0x30;
4453             f = s->cs_file + cs;
4454             switch (addr) {
4455                 case 0x84 ... 0x87:     /* GPMC_NAND_DATA */
4456                     if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4457                         && f->opaque) {
4458                         nand_setpins(f->opaque, 0, 0, 0, 1, 0);
4459                         switch (((f->config[0] >> 12) & 3)) {
4460                             case 0: /* 8bit */
4461                                 return nand_getio(f->opaque);
4462                             case 1: /* 16bit */
4463                                 /* reading 8bits from a 16bit device?! */
4464                                 return nand_getio(f->opaque);
4465                             default:
4466                                 return 0;
4467                         }
4468                     }
4469                     return 0;
4470                 default:
4471                     break;
4472             }
4473             break;
4474         default:
4475             break;
4476     }
4477     OMAP_BAD_REG(addr);
4478     return 0;
4479 }
4480
4481 static uint32_t omap_gpmc_read16(void *opaque, target_phys_addr_t addr)
4482 {
4483     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4484     int cs;
4485     struct omap_gpmc_cs_file_s *f;
4486     uint32_t x1, x2;
4487     
4488     switch (addr) {
4489         case 0x060 ... 0x1d4:
4490             cs = (addr - 0x060) / 0x30;
4491             addr -= cs * 0x30;
4492             f = s->cs_file + cs;
4493             switch (addr) {
4494                 case 0x84:      /* GPMC_NAND_DATA */
4495                 case 0x86:
4496                     if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4497                         && f->opaque) {
4498                         nand_setpins(f->opaque, 0, 0, 0, 1, 0);
4499                         switch (((f->config[0] >> 12) & 3)) {
4500                             case 0: /* 8bit */
4501                                 x1 = nand_getio(f->opaque);
4502                                 x2 = nand_getio(f->opaque);
4503                                 return (x2 << 8) | x1;
4504                             case 1: /* 16bit */
4505                                 return nand_getio(f->opaque);
4506                             default:
4507                                 return 0;
4508                         }
4509                     }
4510                     return 0;
4511                 default:
4512                     break;
4513             }
4514             break;
4515         default:
4516             break;
4517     }
4518     OMAP_BAD_REG(addr);
4519     return 0;
4520 }
4521
4522 static void omap_gpmc_write(void *opaque, target_phys_addr_t addr,
4523                 uint32_t value)
4524 {
4525     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4526     int cs;
4527     struct omap_gpmc_cs_file_s *f;
4528
4529     switch (addr) {
4530     case 0x000: /* GPMC_REVISION */
4531     case 0x014: /* GPMC_SYSSTATUS */
4532     case 0x054: /* GPMC_STATUS */
4533     case 0x1f0: /* GPMC_PREFETCH_STATUS */
4534     case 0x200 ... 0x220:       /* GPMC_ECC_RESULT */
4535     case 0x234: /* GPMC_PSA_LSB */
4536     case 0x238: /* GPMC_PSA_MSB */
4537         OMAP_RO_REGV(addr, value);
4538         break;
4539
4540     case 0x010: /* GPMC_SYSCONFIG */
4541         if ((value >> 3) == 0x3)
4542             fprintf(stderr, "%s: bad SDRAM idle mode %i\n",
4543                             __FUNCTION__, value >> 3);
4544         if (value & 2)
4545             omap_gpmc_reset(s);
4546         s->sysconfig = value & 0x19;
4547         break;
4548
4549     case 0x018: /* GPMC_IRQSTATUS */
4550         s->irqen = ~value;
4551         omap_gpmc_int_update(s);
4552         break;
4553
4554     case 0x01c: /* GPMC_IRQENABLE */
4555         s->irqen = value & 0xf03;
4556         omap_gpmc_int_update(s);
4557         break;
4558
4559     case 0x040: /* GPMC_TIMEOUT_CONTROL */
4560         s->timeout = value & 0x1ff1;
4561         break;
4562
4563     case 0x044: /* GPMC_ERR_ADDRESS */
4564     case 0x048: /* GPMC_ERR_TYPE */
4565         break;
4566
4567     case 0x050: /* GPMC_CONFIG */
4568         s->config = value & 0xf13;
4569         break;
4570
4571     case 0x060 ... 0x1d4:
4572         cs = (addr - 0x060) / 0x30;
4573         addr -= cs * 0x30;
4574         f = s->cs_file + cs;
4575         switch (addr) {
4576             case 0x60:  /* GPMC_CONFIG1 */
4577                 f->config[0] = value & 0xffef3e13;
4578                 break;
4579             case 0x64:  /* GPMC_CONFIG2 */
4580                 f->config[1] = value & 0x001f1f8f;
4581                 break;
4582             case 0x68:  /* GPMC_CONFIG3 */
4583                 f->config[2] = value & 0x001f1f8f;
4584                 break;
4585             case 0x6c:  /* GPMC_CONFIG4 */
4586                 f->config[3] = value & 0x1f8f1f8f;
4587                 break;
4588             case 0x70:  /* GPMC_CONFIG5 */
4589                 f->config[4] = value & 0x0f1f1f1f;
4590                 break;
4591             case 0x74:  /* GPMC_CONFIG6 */
4592                 f->config[5] = value & 0x00000fcf;
4593                 break;
4594             case 0x78:  /* GPMC_CONFIG7 */
4595                 if ((f->config[6] ^ value) & 0xf7f) {
4596                     if (f->config[6] & (1 << 6))                /* CSVALID */
4597                         omap_gpmc_cs_unmap(f);
4598                     if (value & (1 << 6))                       /* CSVALID */
4599                         omap_gpmc_cs_map(f, value & 0x3f,       /* MASKADDR */
4600                                         (value >> 8 & 0xf));    /* BASEADDR */
4601                 }
4602                 f->config[6] = value & 0x00000f7f;
4603                 break;
4604             case 0x7c:  /* GPMC_NAND_COMMAND */
4605             case 0x80:  /* GPMC_NAND_ADDRESS */
4606             case 0x84:  /* GPMC_NAND_DATA */
4607                 if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4608                     && f->opaque) {
4609                     switch (addr) {
4610                         case 0x7c: nand_setpins(f->opaque, 1, 0, 0, 1, 0); break; /* CLE */
4611                         case 0x80: nand_setpins(f->opaque, 0, 1, 0, 1, 0); break; /* ALE */
4612                         case 0x84: nand_setpins(f->opaque, 0, 0, 0, 1, 0); break;
4613                         default: break;
4614                     }
4615                     switch (((f->config[0] >> 12) & 3)) {
4616                         case 0: /* 8bit */
4617                             nand_setio(f->opaque, value & 0xff);
4618                             nand_setio(f->opaque, (value >> 8) & 0xff);
4619                             nand_setio(f->opaque, (value >> 16) & 0xff);
4620                             nand_setio(f->opaque, (value >> 24) & 0xff);
4621                             break;
4622                         case 1: /* 16bit */
4623                             nand_setio(f->opaque, value & 0xffff);
4624                             nand_setio(f->opaque, (value >> 16) & 0xffff);
4625                             break;
4626                         default:
4627                             break;
4628                     }
4629                 }
4630                 break;
4631             default:
4632                 goto bad_reg;
4633         }
4634         break;
4635
4636     case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */
4637         s->prefconfig[0] = value & 0x7f8f7fbf;
4638         /* TODO: update interrupts, fifos, dmas */
4639         break;
4640
4641     case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */
4642         s->prefconfig[1] = value & 0x3fff;
4643         break;
4644
4645     case 0x1ec: /* GPMC_PREFETCH_CONTROL */
4646         s->prefcontrol = value & 1;
4647         if (s->prefcontrol) {
4648             if (s->prefconfig[0] & 1)
4649                 s->preffifo = 0x40;
4650             else
4651                 s->preffifo = 0x00;
4652         }
4653         /* TODO: start */
4654         break;
4655
4656     case 0x1f4: /* GPMC_ECC_CONFIG */
4657         s->ecc_cs = 0x8f;
4658         break;
4659     case 0x1f8: /* GPMC_ECC_CONTROL */
4660         if (value & (1 << 8))
4661             for (cs = 0; cs < 9; cs ++)
4662                 ecc_reset(&s->ecc[cs]);
4663         s->ecc_ptr = value & 0xf;
4664         if (s->ecc_ptr == 0 || s->ecc_ptr > 9) {
4665             s->ecc_ptr = 0;
4666             s->ecc_cs &= ~1;
4667         }
4668         break;
4669     case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */
4670         s->ecc_cfg = value & 0x3fcff1ff;
4671         break;
4672     case 0x230: /* GPMC_TESTMODE_CTRL */
4673         if (value & 7)
4674             fprintf(stderr, "%s: test mode enable attempt\n", __FUNCTION__);
4675         break;
4676
4677     default:
4678     bad_reg:
4679         OMAP_BAD_REGV(addr, value);
4680         return;
4681     }
4682 }
4683
4684 static void omap_gpmc_write8(void *opaque, target_phys_addr_t addr,
4685                              uint32_t value)
4686 {
4687     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4688     int cs;
4689     struct omap_gpmc_cs_file_s *f;
4690
4691     switch (addr) {
4692         case 0x060 ... 0x1d4:
4693             cs = (addr - 0x060) / 0x30;
4694             addr -= cs * 0x30;
4695             f = s->cs_file + cs;
4696             switch (addr) {
4697                 case 0x7c ... 0x7f:     /* GPMC_NAND_COMMAND */
4698                 case 0x80 ... 0x83:     /* GPMC_NAND_ADDRESS */
4699                 case 0x84 ... 0x87:     /* GPMC_NAND_DATA */
4700                     if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4701                         && f->opaque) {
4702                         switch (addr) {
4703                             case 0x7c ... 0x7f:
4704                                 nand_setpins(f->opaque, 1, 0, 0, 1, 0); /* CLE */
4705                                 break;
4706                             case 0x80 ... 0x83:
4707                                 nand_setpins(f->opaque, 0, 1, 0, 1, 0); /* ALE */
4708                                 break;
4709                             case 0x84 ... 0x87:
4710                                 nand_setpins(f->opaque, 0, 0, 0, 1, 0);
4711                                 break;
4712                             default:
4713                                 break;
4714                         }
4715                         switch (((f->config[0] >> 12) & 3)) {
4716                             case 0: /* 8bit */
4717                                 nand_setio(f->opaque, value & 0xff);
4718                                 break;
4719                             case 1: /* 16bit */
4720                                 /* writing to a 16bit device with 8bit access!? */
4721                                 nand_setio(f->opaque, value & 0xffff);
4722                                 break;
4723                             default:
4724                                 break;
4725                         }
4726                     }
4727                     break;
4728                 default:
4729                     goto bad_reg;
4730             }
4731             break;
4732         default:
4733         bad_reg:
4734             OMAP_BAD_REGV(addr, value);
4735             return;
4736     }
4737 }
4738
4739 static void omap_gpmc_write16(void *opaque, target_phys_addr_t addr,
4740                               uint32_t value)
4741 {
4742     struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque;
4743     int cs;
4744     struct omap_gpmc_cs_file_s *f;
4745     
4746     switch (addr) {
4747         case 0x060 ... 0x1d4:
4748             cs = (addr - 0x060) / 0x30;
4749             addr -= cs * 0x30;
4750             f = s->cs_file + cs;
4751             switch (addr) {
4752                 case 0x7c:      /* GPMC_NAND_COMMAND */
4753                 case 0x7e:
4754                 case 0x80:      /* GPMC_NAND_ADDRESS */
4755                 case 0x82:
4756                 case 0x84:      /* GPMC_NAND_DATA */
4757                 case 0x86:
4758                     if (((f->config[0] >> 10) & 3) == 2 /* NAND device type ? */
4759                         && f->opaque) {
4760                         switch (addr) {
4761                             case 0x7c:
4762                             case 0x7e: 
4763                                 nand_setpins(f->opaque, 1, 0, 0, 1, 0); /* CLE */
4764                                 break;
4765                             case 0x80:
4766                             case 0x82:
4767                                 nand_setpins(f->opaque, 0, 1, 0, 1, 0); /* ALE */
4768                                 break;
4769                             case 0x84:
4770                             case 0x86:
4771                                 nand_setpins(f->opaque, 0, 0, 0, 1, 0);
4772                                 break;
4773                             default:
4774                                 break;
4775                         }
4776                         switch (((f->config[0] >> 12) & 3)) {
4777                             case 0: /* 8bit */
4778                                 nand_setio(f->opaque, value & 0xff);
4779                                 nand_setio(f->opaque, (value >> 8) & 0xff);
4780                                 break;
4781                             case 1: /* 16bit */
4782                                 nand_setio(f->opaque, value & 0xffff);
4783                                 break;
4784                             default:
4785                                 break;
4786                         }
4787                     }
4788                     break;
4789                 default:
4790                     goto bad_reg;
4791             }
4792             break;
4793         default:
4794         bad_reg:
4795             OMAP_BAD_REGV(addr, value);
4796             return;
4797     }
4798 }
4799
4800 static CPUReadMemoryFunc *omap_gpmc_readfn[] = {
4801     omap_gpmc_read8,
4802     omap_gpmc_read16,
4803     omap_gpmc_read,
4804 };
4805
4806 static CPUWriteMemoryFunc *omap_gpmc_writefn[] = {
4807     omap_gpmc_write8,
4808     omap_gpmc_write16,
4809     omap_gpmc_write,
4810 };
4811
4812 struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu,
4813                                    target_phys_addr_t base, qemu_irq irq)
4814 {
4815     int iomemtype;
4816     struct omap_gpmc_s *s = (struct omap_gpmc_s *)
4817             qemu_mallocz(sizeof(struct omap_gpmc_s));
4818
4819     s->revision = cpu_class_omap3(mpu) ? 0x50 : 0x20;
4820     omap_gpmc_reset(s);
4821
4822     iomemtype = cpu_register_io_memory(0, omap_gpmc_readfn,
4823                     omap_gpmc_writefn, s);
4824     cpu_register_physical_memory(base, 0x1000, iomemtype);
4825
4826     register_savevm("omap_gpmc", -1, 0,
4827                     omap_gpmc_save_state, omap_gpmc_load_state, s);
4828     return s;
4829 }
4830
4831 void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, int iomemtype,
4832                 void (*base_upd)(void *opaque, target_phys_addr_t new),
4833                 void (*unmap)(void *opaque), void *opaque,
4834                 int devicetype)
4835 {
4836     struct omap_gpmc_cs_file_s *f;
4837
4838     if (cs < 0 || cs >= 8) {
4839         fprintf(stderr, "%s: bad chip-select %i\n", __FUNCTION__, cs);
4840         exit(-1);
4841     }
4842     f = &s->cs_file[cs];
4843
4844     f->iomemtype = iomemtype;
4845     f->base_update = base_upd;
4846     f->unmap = unmap;
4847     f->opaque = opaque;
4848
4849     f->config[0] &= ~(0x3 << 10);
4850     f->config[0] |= (devicetype & 3) << 10;
4851     if (devicetype == 2) { /* NAND */
4852         f->config[0] &= ~(0x3 << 12);
4853         if (nand_getbuswidth(f->opaque) == 16)
4854             f->config[0] |= 1 << 12;
4855     }
4856     
4857     if (f->config[6] & (1 << 6))                                /* CSVALID */
4858         omap_gpmc_cs_map(f, f->config[6] & 0x3f,                /* MASKADDR */
4859                         (f->config[6] >> 8 & 0xf));             /* BASEADDR */
4860 }
4861
4862 /* General chip reset */
4863 static void omap2_mpu_reset(void *opaque)
4864 {
4865     struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
4866
4867     omap_inth_reset(mpu->ih[0]);
4868     omap_dma_reset(mpu->dma);
4869     omap_prcm_reset(mpu->prcm);
4870     omap_sysctl_reset(mpu->sysc);
4871     omap_gp_timer_reset(mpu->gptimer[0]);
4872     omap_gp_timer_reset(mpu->gptimer[1]);
4873     omap_gp_timer_reset(mpu->gptimer[2]);
4874     omap_gp_timer_reset(mpu->gptimer[3]);
4875     omap_gp_timer_reset(mpu->gptimer[4]);
4876     omap_gp_timer_reset(mpu->gptimer[5]);
4877     omap_gp_timer_reset(mpu->gptimer[6]);
4878     omap_gp_timer_reset(mpu->gptimer[7]);
4879     omap_gp_timer_reset(mpu->gptimer[8]);
4880     omap_gp_timer_reset(mpu->gptimer[9]);
4881     omap_gp_timer_reset(mpu->gptimer[10]);
4882     omap_gp_timer_reset(mpu->gptimer[11]);
4883     omap_synctimer_reset(&mpu->synctimer);
4884     omap_sdrc_reset(mpu->sdrc);
4885     omap_gpmc_reset(mpu->gpmc);
4886     omap_dss_reset(mpu->dss);
4887     omap_uart_reset(mpu->uart[0]);
4888     omap_uart_reset(mpu->uart[1]);
4889     omap_uart_reset(mpu->uart[2]);
4890     omap_mmc_reset(mpu->mmc);
4891     omap_gpif_reset(mpu->gpif);
4892     omap_mcspi_reset(mpu->mcspi[0]);
4893     omap_mcspi_reset(mpu->mcspi[1]);
4894     omap_i2c_reset(mpu->i2c[0]);
4895     omap_i2c_reset(mpu->i2c[1]);
4896     cpu_reset(mpu->env);
4897 }
4898
4899 static int omap2_validate_addr(struct omap_mpu_state_s *s,
4900                 target_phys_addr_t addr)
4901 {
4902     return 1;
4903 }
4904
4905 static const struct dma_irq_map omap2_dma_irq_map[] = {
4906     { 0, OMAP_INT_24XX_SDMA_IRQ0 },
4907     { 0, OMAP_INT_24XX_SDMA_IRQ1 },
4908     { 0, OMAP_INT_24XX_SDMA_IRQ2 },
4909     { 0, OMAP_INT_24XX_SDMA_IRQ3 },
4910 };
4911
4912 struct omap_mpu_state_s *omap2420_mpu_init(unsigned long sdram_size,
4913                 const char *core)
4914 {
4915     struct omap_mpu_state_s *s = (struct omap_mpu_state_s *)
4916             qemu_mallocz(sizeof(struct omap_mpu_state_s));
4917     ram_addr_t sram_base, q2_base;
4918     qemu_irq *cpu_irq;
4919     qemu_irq dma_irqs[4];
4920     omap_clk gpio_clks[4];
4921     int sdindex;
4922     int i;
4923
4924     /* Core */
4925     s->mpu_model = omap2420;
4926     s->env = cpu_init(core ?: "arm1136-r2");
4927     if (!s->env) {
4928         fprintf(stderr, "Unable to find CPU definition\n");
4929         exit(1);
4930     }
4931     s->sdram_size = sdram_size;
4932     s->sram_size = OMAP242X_SRAM_SIZE;
4933
4934     s->wakeup = qemu_allocate_irqs(omap_mpu_wakeup, s, 1)[0];
4935
4936     /* Clocks */
4937     omap_clk_init(s);
4938
4939     /* Memory-mapped stuff */
4940     cpu_register_physical_memory(OMAP2_Q2_BASE, s->sdram_size,
4941                     (q2_base = qemu_ram_alloc(s->sdram_size)) | IO_MEM_RAM);
4942     cpu_register_physical_memory(OMAP2_SRAM_BASE, s->sram_size,
4943                     (sram_base = qemu_ram_alloc(s->sram_size)) | IO_MEM_RAM);
4944
4945     s->l4 = omap_l4_init(OMAP2_L4_BASE, 54);
4946
4947     /* Actually mapped at any 2K boundary in the ARM11 private-peripheral if */
4948     cpu_irq = arm_pic_init_cpu(s->env);
4949     s->ih[0] = omap2_inth_init(s,
4950                     0x480fe000, 0x1000, 3, &s->irq[0],
4951                     cpu_irq[ARM_PIC_CPU_IRQ], cpu_irq[ARM_PIC_CPU_FIQ],
4952                     omap_findclk(s, "mpu_intc_fclk"),
4953                     omap_findclk(s, "mpu_intc_iclk"));
4954
4955     s->prcm = omap_prcm_init(omap_l4tao(s->l4, 3),
4956                     s->irq[0][OMAP_INT_24XX_PRCM_MPU_IRQ], NULL, NULL, s);
4957
4958     s->sysc = omap_sysctl_init(omap_l4tao(s->l4, 1),
4959                     omap_findclk(s, "omapctrl_iclk"), s);
4960
4961     for (i = 0; i < 4; i ++)
4962         dma_irqs[i] =
4963                 s->irq[omap2_dma_irq_map[i].ih][omap2_dma_irq_map[i].intr];
4964     s->dma = omap_dma4_init(0x48056000, dma_irqs, s, 256, 32,
4965                     omap_findclk(s, "sdma_iclk"),
4966                     omap_findclk(s, "sdma_fclk"));
4967     s->port->addr_valid = omap2_validate_addr;
4968
4969     /* Register SDRAM and SRAM ports for fast DMA transfers.  */
4970     soc_dma_port_add_mem_ram(s->dma, q2_base, OMAP2_Q2_BASE, s->sdram_size);
4971     soc_dma_port_add_mem_ram(s->dma, sram_base, OMAP2_SRAM_BASE, s->sram_size);
4972
4973     s->uart[0] = omap2_uart_init(omap_l4ta(s->l4, 19),
4974                     s->irq[0][OMAP_INT_24XX_UART1_IRQ],
4975                     omap_findclk(s, "uart1_fclk"),
4976                     omap_findclk(s, "uart1_iclk"),
4977                     s->drq[OMAP24XX_DMA_UART1_TX],
4978                     s->drq[OMAP24XX_DMA_UART1_RX], serial_hds[0]);
4979     s->uart[1] = omap2_uart_init(omap_l4ta(s->l4, 20),
4980                     s->irq[0][OMAP_INT_24XX_UART2_IRQ],
4981                     omap_findclk(s, "uart2_fclk"),
4982                     omap_findclk(s, "uart2_iclk"),
4983                     s->drq[OMAP24XX_DMA_UART2_TX],
4984                     s->drq[OMAP24XX_DMA_UART2_RX],
4985                     serial_hds[0] ? serial_hds[1] : 0);
4986     s->uart[2] = omap2_uart_init(omap_l4ta(s->l4, 21),
4987                     s->irq[0][OMAP_INT_24XX_UART3_IRQ],
4988                     omap_findclk(s, "uart3_fclk"),
4989                     omap_findclk(s, "uart3_iclk"),
4990                     s->drq[OMAP24XX_DMA_UART3_TX],
4991                     s->drq[OMAP24XX_DMA_UART3_RX],
4992                     serial_hds[0] && serial_hds[1] ? serial_hds[2] : 0);
4993
4994     s->gptimer[0] = omap_gp_timer_init(omap_l4ta(s->l4, 7),
4995                     s->irq[0][OMAP_INT_24XX_GPTIMER1],
4996                     omap_findclk(s, "wu_gpt1_clk"),
4997                     omap_findclk(s, "wu_l4_iclk"));
4998     s->gptimer[1] = omap_gp_timer_init(omap_l4ta(s->l4, 8),
4999                     s->irq[0][OMAP_INT_24XX_GPTIMER2],
5000                     omap_findclk(s, "core_gpt2_clk"),
5001                     omap_findclk(s, "core_l4_iclk"));
5002     s->gptimer[2] = omap_gp_timer_init(omap_l4ta(s->l4, 22),
5003                     s->irq[0][OMAP_INT_24XX_GPTIMER3],
5004                     omap_findclk(s, "core_gpt3_clk"),
5005                     omap_findclk(s, "core_l4_iclk"));
5006     s->gptimer[3] = omap_gp_timer_init(omap_l4ta(s->l4, 23),
5007                     s->irq[0][OMAP_INT_24XX_GPTIMER4],
5008                     omap_findclk(s, "core_gpt4_clk"),
5009                     omap_findclk(s, "core_l4_iclk"));
5010     s->gptimer[4] = omap_gp_timer_init(omap_l4ta(s->l4, 24),
5011                     s->irq[0][OMAP_INT_24XX_GPTIMER5],
5012                     omap_findclk(s, "core_gpt5_clk"),
5013                     omap_findclk(s, "core_l4_iclk"));
5014     s->gptimer[5] = omap_gp_timer_init(omap_l4ta(s->l4, 25),
5015                     s->irq[0][OMAP_INT_24XX_GPTIMER6],
5016                     omap_findclk(s, "core_gpt6_clk"),
5017                     omap_findclk(s, "core_l4_iclk"));
5018     s->gptimer[6] = omap_gp_timer_init(omap_l4ta(s->l4, 26),
5019                     s->irq[0][OMAP_INT_24XX_GPTIMER7],
5020                     omap_findclk(s, "core_gpt7_clk"),
5021                     omap_findclk(s, "core_l4_iclk"));
5022     s->gptimer[7] = omap_gp_timer_init(omap_l4ta(s->l4, 27),
5023                     s->irq[0][OMAP_INT_24XX_GPTIMER8],
5024                     omap_findclk(s, "core_gpt8_clk"),
5025                     omap_findclk(s, "core_l4_iclk"));
5026     s->gptimer[8] = omap_gp_timer_init(omap_l4ta(s->l4, 28),
5027                     s->irq[0][OMAP_INT_24XX_GPTIMER9],
5028                     omap_findclk(s, "core_gpt9_clk"),
5029                     omap_findclk(s, "core_l4_iclk"));
5030     s->gptimer[9] = omap_gp_timer_init(omap_l4ta(s->l4, 29),
5031                     s->irq[0][OMAP_INT_24XX_GPTIMER10],
5032                     omap_findclk(s, "core_gpt10_clk"),
5033                     omap_findclk(s, "core_l4_iclk"));
5034     s->gptimer[10] = omap_gp_timer_init(omap_l4ta(s->l4, 30),
5035                     s->irq[0][OMAP_INT_24XX_GPTIMER11],
5036                     omap_findclk(s, "core_gpt11_clk"),
5037                     omap_findclk(s, "core_l4_iclk"));
5038     s->gptimer[11] = omap_gp_timer_init(omap_l4ta(s->l4, 31),
5039                     s->irq[0][OMAP_INT_24XX_GPTIMER12],
5040                     omap_findclk(s, "core_gpt12_clk"),
5041                     omap_findclk(s, "core_l4_iclk"));
5042
5043     omap_tap_init(omap_l4ta(s->l4, 2), s);
5044
5045     omap_synctimer_init(omap_l4tao(s->l4, 2), s,
5046                     omap_findclk(s, "clk32-kHz"),
5047                     omap_findclk(s, "core_l4_iclk"));
5048
5049     s->i2c[0] = omap2_i2c_init(omap_l4tao(s->l4, 5),
5050                     s->irq[0][OMAP_INT_24XX_I2C1_IRQ],
5051                     &s->drq[OMAP24XX_DMA_I2C1_TX],
5052                     omap_findclk(s, "i2c1.fclk"),
5053                     omap_findclk(s, "i2c1.iclk"));
5054     s->i2c[1] = omap2_i2c_init(omap_l4tao(s->l4, 6),
5055                     s->irq[0][OMAP_INT_24XX_I2C2_IRQ],
5056                     &s->drq[OMAP24XX_DMA_I2C2_TX],
5057                     omap_findclk(s, "i2c2.fclk"),
5058                     omap_findclk(s, "i2c2.iclk"));
5059
5060     gpio_clks[0] = omap_findclk(s, "gpio1_dbclk");
5061     gpio_clks[1] = omap_findclk(s, "gpio2_dbclk");
5062     gpio_clks[2] = omap_findclk(s, "gpio3_dbclk");
5063     gpio_clks[3] = omap_findclk(s, "gpio4_dbclk");
5064     s->gpif = omap2_gpio_init(s, omap_l4ta(s->l4, 3),
5065                     &s->irq[0][OMAP_INT_24XX_GPIO_BANK1],
5066                     gpio_clks, omap_findclk(s, "gpio_iclk"), 4);
5067
5068     s->sdrc = omap_sdrc_init(0x68009000);
5069     s->gpmc = omap_gpmc_init(s, 0x6800a000, s->irq[0][OMAP_INT_24XX_GPMC_IRQ]);
5070
5071     sdindex = drive_get_index(IF_SD, 0, 0);
5072     if (sdindex == -1) {
5073         fprintf(stderr, "qemu: missing SecureDigital device\n");
5074         exit(1);
5075     }
5076     s->mmc = omap2_mmc_init(omap_l4tao(s->l4, 9), drives_table[sdindex].bdrv,
5077                     s->irq[0][OMAP_INT_24XX_MMC_IRQ],
5078                     &s->drq[OMAP24XX_DMA_MMC1_TX],
5079                     omap_findclk(s, "mmc_fclk"), omap_findclk(s, "mmc_iclk"));
5080
5081     s->mcspi[0] = omap_mcspi_init(omap_l4ta(s->l4, 35), s, 4,
5082                     s->irq[0][OMAP_INT_24XX_MCSPI1_IRQ],
5083                     &s->drq[OMAP24XX_DMA_SPI1_TX0],
5084                     omap_findclk(s, "spi1_fclk"),
5085                     omap_findclk(s, "spi1_iclk"));
5086     s->mcspi[1] = omap_mcspi_init(omap_l4ta(s->l4, 36), s, 2,
5087                     s->irq[0][OMAP_INT_24XX_MCSPI2_IRQ],
5088                     &s->drq[OMAP24XX_DMA_SPI2_TX0],
5089                     omap_findclk(s, "spi2_fclk"),
5090                     omap_findclk(s, "spi2_iclk"));
5091
5092     s->dss = omap_dss_init(s, omap_l4ta(s->l4, 10),
5093                     /* XXX wire M_IRQ_25, D_L2_IRQ_30 and I_IRQ_13 together */
5094                     s->irq[0][OMAP_INT_24XX_DSS_IRQ], s->drq[OMAP24XX_DMA_DSS],
5095                     omap_findclk(s, "dss_clk1"), omap_findclk(s, "dss_clk2"),
5096                     omap_findclk(s, "dss_54m_clk"),
5097                     omap_findclk(s, "dss_l3_iclk"),
5098                     omap_findclk(s, "dss_l4_iclk"));
5099
5100     omap_sti_init(omap_l4ta(s->l4, 18), 0x54000000,
5101                     s->irq[0][OMAP_INT_24XX_STI], omap_findclk(s, "emul_ck"),
5102                     serial_hds[0] && serial_hds[1] && serial_hds[2] ?
5103                     serial_hds[3] : 0);
5104
5105     s->eac = omap_eac_init(omap_l4ta(s->l4, 32),
5106                     s->irq[0][OMAP_INT_24XX_EAC_IRQ],
5107                     /* Ten consecutive lines */
5108                     &s->drq[OMAP24XX_DMA_EAC_AC_RD],
5109                     omap_findclk(s, "func_96m_clk"),
5110                     omap_findclk(s, "core_l4_iclk"));
5111
5112     /* All register mappings (includin those not currenlty implemented):
5113      * SystemControlMod 48000000 - 48000fff
5114      * SystemControlL4  48001000 - 48001fff
5115      * 32kHz Timer Mod  48004000 - 48004fff
5116      * 32kHz Timer L4   48005000 - 48005fff
5117      * PRCM ModA        48008000 - 480087ff
5118      * PRCM ModB        48008800 - 48008fff
5119      * PRCM L4          48009000 - 48009fff
5120      * TEST-BCM Mod     48012000 - 48012fff
5121      * TEST-BCM L4      48013000 - 48013fff
5122      * TEST-TAP Mod     48014000 - 48014fff
5123      * TEST-TAP L4      48015000 - 48015fff
5124      * GPIO1 Mod        48018000 - 48018fff
5125      * GPIO Top         48019000 - 48019fff
5126      * GPIO2 Mod        4801a000 - 4801afff
5127      * GPIO L4          4801b000 - 4801bfff
5128      * GPIO3 Mod        4801c000 - 4801cfff
5129      * GPIO4 Mod        4801e000 - 4801efff
5130      * WDTIMER1 Mod     48020000 - 48010fff
5131      * WDTIMER Top      48021000 - 48011fff
5132      * WDTIMER2 Mod     48022000 - 48012fff
5133      * WDTIMER L4       48023000 - 48013fff
5134      * WDTIMER3 Mod     48024000 - 48014fff
5135      * WDTIMER3 L4      48025000 - 48015fff
5136      * WDTIMER4 Mod     48026000 - 48016fff
5137      * WDTIMER4 L4      48027000 - 48017fff
5138      * GPTIMER1 Mod     48028000 - 48018fff
5139      * GPTIMER1 L4      48029000 - 48019fff
5140      * GPTIMER2 Mod     4802a000 - 4801afff
5141      * GPTIMER2 L4      4802b000 - 4801bfff
5142      * L4-Config AP     48040000 - 480407ff
5143      * L4-Config IP     48040800 - 48040fff
5144      * L4-Config LA     48041000 - 48041fff
5145      * ARM11ETB Mod     48048000 - 48049fff
5146      * ARM11ETB L4      4804a000 - 4804afff
5147      * DISPLAY Top      48050000 - 480503ff
5148      * DISPLAY DISPC    48050400 - 480507ff
5149      * DISPLAY RFBI     48050800 - 48050bff
5150      * DISPLAY VENC     48050c00 - 48050fff
5151      * DISPLAY L4       48051000 - 48051fff
5152      * CAMERA Top       48052000 - 480523ff
5153      * CAMERA core      48052400 - 480527ff
5154      * CAMERA DMA       48052800 - 48052bff
5155      * CAMERA MMU       48052c00 - 48052fff
5156      * CAMERA L4        48053000 - 48053fff
5157      * SDMA Mod         48056000 - 48056fff
5158      * SDMA L4          48057000 - 48057fff
5159      * SSI Top          48058000 - 48058fff
5160      * SSI GDD          48059000 - 48059fff
5161      * SSI Port1        4805a000 - 4805afff
5162      * SSI Port2        4805b000 - 4805bfff
5163      * SSI L4           4805c000 - 4805cfff
5164      * USB Mod          4805e000 - 480fefff
5165      * USB L4           4805f000 - 480fffff
5166      * WIN_TRACER1 Mod  48060000 - 48060fff
5167      * WIN_TRACER1 L4   48061000 - 48061fff
5168      * WIN_TRACER2 Mod  48062000 - 48062fff
5169      * WIN_TRACER2 L4   48063000 - 48063fff
5170      * WIN_TRACER3 Mod  48064000 - 48064fff
5171      * WIN_TRACER3 L4   48065000 - 48065fff
5172      * WIN_TRACER4 Top  48066000 - 480660ff
5173      * WIN_TRACER4 ETT  48066100 - 480661ff
5174      * WIN_TRACER4 WT   48066200 - 480662ff
5175      * WIN_TRACER4 L4   48067000 - 48067fff
5176      * XTI Mod          48068000 - 48068fff
5177      * XTI L4           48069000 - 48069fff
5178      * UART1 Mod        4806a000 - 4806afff
5179      * UART1 L4         4806b000 - 4806bfff
5180      * UART2 Mod        4806c000 - 4806cfff
5181      * UART2 L4         4806d000 - 4806dfff
5182      * UART3 Mod        4806e000 - 4806efff
5183      * UART3 L4         4806f000 - 4806ffff
5184      * I2C1 Mod         48070000 - 48070fff
5185      * I2C1 L4          48071000 - 48071fff
5186      * I2C2 Mod         48072000 - 48072fff
5187      * I2C2 L4          48073000 - 48073fff
5188      * McBSP1 Mod       48074000 - 48074fff
5189      * McBSP1 L4        48075000 - 48075fff
5190      * McBSP2 Mod       48076000 - 48076fff
5191      * McBSP2 L4        48077000 - 48077fff
5192      * GPTIMER3 Mod     48078000 - 48078fff
5193      * GPTIMER3 L4      48079000 - 48079fff
5194      * GPTIMER4 Mod     4807a000 - 4807afff
5195      * GPTIMER4 L4      4807b000 - 4807bfff
5196      * GPTIMER5 Mod     4807c000 - 4807cfff
5197      * GPTIMER5 L4      4807d000 - 4807dfff
5198      * GPTIMER6 Mod     4807e000 - 4807efff
5199      * GPTIMER6 L4      4807f000 - 4807ffff
5200      * GPTIMER7 Mod     48080000 - 48080fff
5201      * GPTIMER7 L4      48081000 - 48081fff
5202      * GPTIMER8 Mod     48082000 - 48082fff
5203      * GPTIMER8 L4      48083000 - 48083fff
5204      * GPTIMER9 Mod     48084000 - 48084fff
5205      * GPTIMER9 L4      48085000 - 48085fff
5206      * GPTIMER10 Mod    48086000 - 48086fff
5207      * GPTIMER10 L4     48087000 - 48087fff
5208      * GPTIMER11 Mod    48088000 - 48088fff
5209      * GPTIMER11 L4     48089000 - 48089fff
5210      * GPTIMER12 Mod    4808a000 - 4808afff
5211      * GPTIMER12 L4     4808b000 - 4808bfff
5212      * EAC Mod          48090000 - 48090fff
5213      * EAC L4           48091000 - 48091fff
5214      * FAC Mod          48092000 - 48092fff
5215      * FAC L4           48093000 - 48093fff
5216      * MAILBOX Mod      48094000 - 48094fff
5217      * MAILBOX L4       48095000 - 48095fff
5218      * SPI1 Mod         48098000 - 48098fff
5219      * SPI1 L4          48099000 - 48099fff
5220      * SPI2 Mod         4809a000 - 4809afff
5221      * SPI2 L4          4809b000 - 4809bfff
5222      * MMC/SDIO Mod     4809c000 - 4809cfff
5223      * MMC/SDIO L4      4809d000 - 4809dfff
5224      * MS_PRO Mod       4809e000 - 4809efff
5225      * MS_PRO L4        4809f000 - 4809ffff
5226      * RNG Mod          480a0000 - 480a0fff
5227      * RNG L4           480a1000 - 480a1fff
5228      * DES3DES Mod      480a2000 - 480a2fff
5229      * DES3DES L4       480a3000 - 480a3fff
5230      * SHA1MD5 Mod      480a4000 - 480a4fff
5231      * SHA1MD5 L4       480a5000 - 480a5fff
5232      * AES Mod          480a6000 - 480a6fff
5233      * AES L4           480a7000 - 480a7fff
5234      * PKA Mod          480a8000 - 480a9fff
5235      * PKA L4           480aa000 - 480aafff
5236      * MG Mod           480b0000 - 480b0fff
5237      * MG L4            480b1000 - 480b1fff
5238      * HDQ/1-wire Mod   480b2000 - 480b2fff
5239      * HDQ/1-wire L4    480b3000 - 480b3fff
5240      * MPU interrupt    480fe000 - 480fefff
5241      * STI channel base 54000000 - 5400ffff
5242      * IVA RAM          5c000000 - 5c01ffff
5243      * IVA ROM          5c020000 - 5c027fff
5244      * IMG_BUF_A        5c040000 - 5c040fff
5245      * IMG_BUF_B        5c042000 - 5c042fff
5246      * VLCDS            5c048000 - 5c0487ff
5247      * IMX_COEF         5c049000 - 5c04afff
5248      * IMX_CMD          5c051000 - 5c051fff
5249      * VLCDQ            5c053000 - 5c0533ff
5250      * VLCDH            5c054000 - 5c054fff
5251      * SEQ_CMD          5c055000 - 5c055fff
5252      * IMX_REG          5c056000 - 5c0560ff
5253      * VLCD_REG         5c056100 - 5c0561ff
5254      * SEQ_REG          5c056200 - 5c0562ff
5255      * IMG_BUF_REG      5c056300 - 5c0563ff
5256      * SEQIRQ_REG       5c056400 - 5c0564ff
5257      * OCP_REG          5c060000 - 5c060fff
5258      * SYSC_REG         5c070000 - 5c070fff
5259      * MMU_REG          5d000000 - 5d000fff
5260      * sDMA R           68000400 - 680005ff
5261      * sDMA W           68000600 - 680007ff
5262      * Display Control  68000800 - 680009ff
5263      * DSP subsystem    68000a00 - 68000bff
5264      * MPU subsystem    68000c00 - 68000dff
5265      * IVA subsystem    68001000 - 680011ff
5266      * USB              68001200 - 680013ff
5267      * Camera           68001400 - 680015ff
5268      * VLYNQ (firewall) 68001800 - 68001bff
5269      * VLYNQ            68001e00 - 68001fff
5270      * SSI              68002000 - 680021ff
5271      * L4               68002400 - 680025ff
5272      * DSP (firewall)   68002800 - 68002bff
5273      * DSP subsystem    68002e00 - 68002fff
5274      * IVA (firewall)   68003000 - 680033ff
5275      * IVA              68003600 - 680037ff
5276      * GFX              68003a00 - 68003bff
5277      * CMDWR emulation  68003c00 - 68003dff
5278      * SMS              68004000 - 680041ff
5279      * OCM              68004200 - 680043ff
5280      * GPMC             68004400 - 680045ff
5281      * RAM (firewall)   68005000 - 680053ff
5282      * RAM (err login)  68005400 - 680057ff
5283      * ROM (firewall)   68005800 - 68005bff
5284      * ROM (err login)  68005c00 - 68005fff
5285      * GPMC (firewall)  68006000 - 680063ff
5286      * GPMC (err login) 68006400 - 680067ff
5287      * SMS (err login)  68006c00 - 68006fff
5288      * SMS registers    68008000 - 68008fff
5289      * SDRC registers   68009000 - 68009fff
5290      * GPMC registers   6800a000   6800afff
5291      */
5292
5293     qemu_register_reset(omap2_mpu_reset, 0, s);
5294
5295     return s;
5296 }