Statistics
| Branch: | Revision:

root / hw / omap1.c @ 7f132a21

History | View | Annotate | Download (112.8 kB)

1
/*
2
 * TI OMAP processors emulation.
3
 *
4
 * Copyright (C) 2006-2008 Andrzej Zaborowski  <balrog@zabor.org>
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as
8
 * published by the Free Software Foundation; either version 2 or
9
 * (at your option) version 3 of the License.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include "hw.h"
20
#include "arm-misc.h"
21
#include "omap.h"
22
#include "sysemu.h"
23
#include "qemu-timer.h"
24
#include "qemu-char.h"
25
#include "soc_dma.h"
26
/* We use pc-style serial ports.  */
27
#include "pc.h"
28

    
29
/* Should signal the TCMI/GPMC */
30
uint32_t omap_badwidth_read8(void *opaque, target_phys_addr_t addr)
31
{
32
    uint8_t ret;
33

    
34
    OMAP_8B_REG(addr);
35
    cpu_physical_memory_read(addr, (void *) &ret, 1);
36
    return ret;
37
}
38

    
39
void omap_badwidth_write8(void *opaque, target_phys_addr_t addr,
40
                uint32_t value)
41
{
42
    uint8_t val8 = value;
43

    
44
    OMAP_8B_REG(addr);
45
    cpu_physical_memory_write(addr, (void *) &val8, 1);
46
}
47

    
48
uint32_t omap_badwidth_read16(void *opaque, target_phys_addr_t addr)
49
{
50
    uint16_t ret;
51

    
52
    OMAP_16B_REG(addr);
53
    cpu_physical_memory_read(addr, (void *) &ret, 2);
54
    return ret;
55
}
56

    
57
void omap_badwidth_write16(void *opaque, target_phys_addr_t addr,
58
                uint32_t value)
59
{
60
    uint16_t val16 = value;
61

    
62
    OMAP_16B_REG(addr);
63
    cpu_physical_memory_write(addr, (void *) &val16, 2);
64
}
65

    
66
uint32_t omap_badwidth_read32(void *opaque, target_phys_addr_t addr)
67
{
68
    uint32_t ret;
69

    
70
    OMAP_32B_REG(addr);
71
    cpu_physical_memory_read(addr, (void *) &ret, 4);
72
    return ret;
73
}
74

    
75
void omap_badwidth_write32(void *opaque, target_phys_addr_t addr,
76
                uint32_t value)
77
{
78
    OMAP_32B_REG(addr);
79
    cpu_physical_memory_write(addr, (void *) &value, 4);
80
}
81

    
82
/* MPU OS timers */
83
struct omap_mpu_timer_s {
84
    qemu_irq irq;
85
    omap_clk clk;
86
    uint32_t val;
87
    int64_t time;
88
    QEMUTimer *timer;
89
    QEMUBH *tick;
90
    int64_t rate;
91
    int it_ena;
92

    
93
    int enable;
94
    int ptv;
95
    int ar;
96
    int st;
97
    uint32_t reset_val;
98
};
99

    
100
static inline uint32_t omap_timer_read(struct omap_mpu_timer_s *timer)
101
{
102
    uint64_t distance = qemu_get_clock(vm_clock) - timer->time;
103

    
104
    if (timer->st && timer->enable && timer->rate)
105
        return timer->val - muldiv64(distance >> (timer->ptv + 1),
106
                                     timer->rate, get_ticks_per_sec());
107
    else
108
        return timer->val;
109
}
110

    
111
static inline void omap_timer_sync(struct omap_mpu_timer_s *timer)
112
{
113
    timer->val = omap_timer_read(timer);
114
    timer->time = qemu_get_clock(vm_clock);
115
}
116

    
117
static inline void omap_timer_update(struct omap_mpu_timer_s *timer)
118
{
119
    int64_t expires;
120

    
121
    if (timer->enable && timer->st && timer->rate) {
122
        timer->val = timer->reset_val;        /* Should skip this on clk enable */
123
        expires = muldiv64((uint64_t) timer->val << (timer->ptv + 1),
124
                           get_ticks_per_sec(), timer->rate);
125

    
126
        /* If timer expiry would be sooner than in about 1 ms and
127
         * auto-reload isn't set, then fire immediately.  This is a hack
128
         * to make systems like PalmOS run in acceptable time.  PalmOS
129
         * sets the interval to a very low value and polls the status bit
130
         * in a busy loop when it wants to sleep just a couple of CPU
131
         * ticks.  */
132
        if (expires > (get_ticks_per_sec() >> 10) || timer->ar)
133
            qemu_mod_timer(timer->timer, timer->time + expires);
134
        else
135
            qemu_bh_schedule(timer->tick);
136
    } else
137
        qemu_del_timer(timer->timer);
138
}
139

    
140
static void omap_timer_fire(void *opaque)
141
{
142
    struct omap_mpu_timer_s *timer = opaque;
143

    
144
    if (!timer->ar) {
145
        timer->val = 0;
146
        timer->st = 0;
147
    }
148

    
149
    if (timer->it_ena)
150
        /* Edge-triggered irq */
151
        qemu_irq_pulse(timer->irq);
152
}
153

    
154
static void omap_timer_tick(void *opaque)
155
{
156
    struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
157

    
158
    omap_timer_sync(timer);
159
    omap_timer_fire(timer);
160
    omap_timer_update(timer);
161
}
162

    
163
static void omap_timer_clk_update(void *opaque, int line, int on)
164
{
165
    struct omap_mpu_timer_s *timer = (struct omap_mpu_timer_s *) opaque;
166

    
167
    omap_timer_sync(timer);
168
    timer->rate = on ? omap_clk_getrate(timer->clk) : 0;
169
    omap_timer_update(timer);
170
}
171

    
172
static void omap_timer_clk_setup(struct omap_mpu_timer_s *timer)
173
{
174
    omap_clk_adduser(timer->clk,
175
                    qemu_allocate_irqs(omap_timer_clk_update, timer, 1)[0]);
176
    timer->rate = omap_clk_getrate(timer->clk);
177
}
178

    
179
static uint32_t omap_mpu_timer_read(void *opaque, target_phys_addr_t addr)
180
{
181
    struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
182

    
183
    switch (addr) {
184
    case 0x00:        /* CNTL_TIMER */
185
        return (s->enable << 5) | (s->ptv << 2) | (s->ar << 1) | s->st;
186

    
187
    case 0x04:        /* LOAD_TIM */
188
        break;
189

    
190
    case 0x08:        /* READ_TIM */
191
        return omap_timer_read(s);
192
    }
193

    
194
    OMAP_BAD_REG(addr);
195
    return 0;
196
}
197

    
198
static void omap_mpu_timer_write(void *opaque, target_phys_addr_t addr,
199
                uint32_t value)
200
{
201
    struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *) opaque;
202

    
203
    switch (addr) {
204
    case 0x00:        /* CNTL_TIMER */
205
        omap_timer_sync(s);
206
        s->enable = (value >> 5) & 1;
207
        s->ptv = (value >> 2) & 7;
208
        s->ar = (value >> 1) & 1;
209
        s->st = value & 1;
210
        omap_timer_update(s);
211
        return;
212

    
213
    case 0x04:        /* LOAD_TIM */
214
        s->reset_val = value;
215
        return;
216

    
217
    case 0x08:        /* READ_TIM */
218
        OMAP_RO_REG(addr);
219
        break;
220

    
221
    default:
222
        OMAP_BAD_REG(addr);
223
    }
224
}
225

    
226
static CPUReadMemoryFunc * const omap_mpu_timer_readfn[] = {
227
    omap_badwidth_read32,
228
    omap_badwidth_read32,
229
    omap_mpu_timer_read,
230
};
231

    
232
static CPUWriteMemoryFunc * const omap_mpu_timer_writefn[] = {
233
    omap_badwidth_write32,
234
    omap_badwidth_write32,
235
    omap_mpu_timer_write,
236
};
237

    
238
static void omap_mpu_timer_reset(struct omap_mpu_timer_s *s)
239
{
240
    qemu_del_timer(s->timer);
241
    s->enable = 0;
242
    s->reset_val = 31337;
243
    s->val = 0;
244
    s->ptv = 0;
245
    s->ar = 0;
246
    s->st = 0;
247
    s->it_ena = 1;
248
}
249

    
250
struct omap_mpu_timer_s *omap_mpu_timer_init(target_phys_addr_t base,
251
                qemu_irq irq, omap_clk clk)
252
{
253
    int iomemtype;
254
    struct omap_mpu_timer_s *s = (struct omap_mpu_timer_s *)
255
            qemu_mallocz(sizeof(struct omap_mpu_timer_s));
256

    
257
    s->irq = irq;
258
    s->clk = clk;
259
    s->timer = qemu_new_timer(vm_clock, omap_timer_tick, s);
260
    s->tick = qemu_bh_new(omap_timer_fire, s);
261
    omap_mpu_timer_reset(s);
262
    omap_timer_clk_setup(s);
263

    
264
    iomemtype = cpu_register_io_memory(omap_mpu_timer_readfn,
265
                    omap_mpu_timer_writefn, s);
266
    cpu_register_physical_memory(base, 0x100, iomemtype);
267

    
268
    return s;
269
}
270

    
271
/* Watchdog timer */
272
struct omap_watchdog_timer_s {
273
    struct omap_mpu_timer_s timer;
274
    uint8_t last_wr;
275
    int mode;
276
    int free;
277
    int reset;
278
};
279

    
280
static uint32_t omap_wd_timer_read(void *opaque, target_phys_addr_t addr)
281
{
282
    struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
283

    
284
    switch (addr) {
285
    case 0x00:        /* CNTL_TIMER */
286
        return (s->timer.ptv << 9) | (s->timer.ar << 8) |
287
                (s->timer.st << 7) | (s->free << 1);
288

    
289
    case 0x04:        /* READ_TIMER */
290
        return omap_timer_read(&s->timer);
291

    
292
    case 0x08:        /* TIMER_MODE */
293
        return s->mode << 15;
294
    }
295

    
296
    OMAP_BAD_REG(addr);
297
    return 0;
298
}
299

    
300
static void omap_wd_timer_write(void *opaque, target_phys_addr_t addr,
301
                uint32_t value)
302
{
303
    struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *) opaque;
304

    
305
    switch (addr) {
306
    case 0x00:        /* CNTL_TIMER */
307
        omap_timer_sync(&s->timer);
308
        s->timer.ptv = (value >> 9) & 7;
309
        s->timer.ar = (value >> 8) & 1;
310
        s->timer.st = (value >> 7) & 1;
311
        s->free = (value >> 1) & 1;
312
        omap_timer_update(&s->timer);
313
        break;
314

    
315
    case 0x04:        /* LOAD_TIMER */
316
        s->timer.reset_val = value & 0xffff;
317
        break;
318

    
319
    case 0x08:        /* TIMER_MODE */
320
        if (!s->mode && ((value >> 15) & 1))
321
            omap_clk_get(s->timer.clk);
322
        s->mode |= (value >> 15) & 1;
323
        if (s->last_wr == 0xf5) {
324
            if ((value & 0xff) == 0xa0) {
325
                if (s->mode) {
326
                    s->mode = 0;
327
                    omap_clk_put(s->timer.clk);
328
                }
329
            } else {
330
                /* XXX: on T|E hardware somehow this has no effect,
331
                 * on Zire 71 it works as specified.  */
332
                s->reset = 1;
333
                qemu_system_reset_request();
334
            }
335
        }
336
        s->last_wr = value & 0xff;
337
        break;
338

    
339
    default:
340
        OMAP_BAD_REG(addr);
341
    }
342
}
343

    
344
static CPUReadMemoryFunc * const omap_wd_timer_readfn[] = {
345
    omap_badwidth_read16,
346
    omap_wd_timer_read,
347
    omap_badwidth_read16,
348
};
349

    
350
static CPUWriteMemoryFunc * const omap_wd_timer_writefn[] = {
351
    omap_badwidth_write16,
352
    omap_wd_timer_write,
353
    omap_badwidth_write16,
354
};
355

    
356
static void omap_wd_timer_reset(struct omap_watchdog_timer_s *s)
357
{
358
    qemu_del_timer(s->timer.timer);
359
    if (!s->mode)
360
        omap_clk_get(s->timer.clk);
361
    s->mode = 1;
362
    s->free = 1;
363
    s->reset = 0;
364
    s->timer.enable = 1;
365
    s->timer.it_ena = 1;
366
    s->timer.reset_val = 0xffff;
367
    s->timer.val = 0;
368
    s->timer.st = 0;
369
    s->timer.ptv = 0;
370
    s->timer.ar = 0;
371
    omap_timer_update(&s->timer);
372
}
373

    
374
struct omap_watchdog_timer_s *omap_wd_timer_init(target_phys_addr_t base,
375
                qemu_irq irq, omap_clk clk)
376
{
377
    int iomemtype;
378
    struct omap_watchdog_timer_s *s = (struct omap_watchdog_timer_s *)
379
            qemu_mallocz(sizeof(struct omap_watchdog_timer_s));
380

    
381
    s->timer.irq = irq;
382
    s->timer.clk = clk;
383
    s->timer.timer = qemu_new_timer(vm_clock, omap_timer_tick, &s->timer);
384
    omap_wd_timer_reset(s);
385
    omap_timer_clk_setup(&s->timer);
386

    
387
    iomemtype = cpu_register_io_memory(omap_wd_timer_readfn,
388
                    omap_wd_timer_writefn, s);
389
    cpu_register_physical_memory(base, 0x100, iomemtype);
390

    
391
    return s;
392
}
393

    
394
/* 32-kHz timer */
395
struct omap_32khz_timer_s {
396
    struct omap_mpu_timer_s timer;
397
};
398

    
399
static uint32_t omap_os_timer_read(void *opaque, target_phys_addr_t addr)
400
{
401
    struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
402
    int offset = addr & OMAP_MPUI_REG_MASK;
403

    
404
    switch (offset) {
405
    case 0x00:        /* TVR */
406
        return s->timer.reset_val;
407

    
408
    case 0x04:        /* TCR */
409
        return omap_timer_read(&s->timer);
410

    
411
    case 0x08:        /* CR */
412
        return (s->timer.ar << 3) | (s->timer.it_ena << 2) | s->timer.st;
413

    
414
    default:
415
        break;
416
    }
417
    OMAP_BAD_REG(addr);
418
    return 0;
419
}
420

    
421
static void omap_os_timer_write(void *opaque, target_phys_addr_t addr,
422
                uint32_t value)
423
{
424
    struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *) opaque;
425
    int offset = addr & OMAP_MPUI_REG_MASK;
426

    
427
    switch (offset) {
428
    case 0x00:        /* TVR */
429
        s->timer.reset_val = value & 0x00ffffff;
430
        break;
431

    
432
    case 0x04:        /* TCR */
433
        OMAP_RO_REG(addr);
434
        break;
435

    
436
    case 0x08:        /* CR */
437
        s->timer.ar = (value >> 3) & 1;
438
        s->timer.it_ena = (value >> 2) & 1;
439
        if (s->timer.st != (value & 1) || (value & 2)) {
440
            omap_timer_sync(&s->timer);
441
            s->timer.enable = value & 1;
442
            s->timer.st = value & 1;
443
            omap_timer_update(&s->timer);
444
        }
445
        break;
446

    
447
    default:
448
        OMAP_BAD_REG(addr);
449
    }
450
}
451

    
452
static CPUReadMemoryFunc * const omap_os_timer_readfn[] = {
453
    omap_badwidth_read32,
454
    omap_badwidth_read32,
455
    omap_os_timer_read,
456
};
457

    
458
static CPUWriteMemoryFunc * const omap_os_timer_writefn[] = {
459
    omap_badwidth_write32,
460
    omap_badwidth_write32,
461
    omap_os_timer_write,
462
};
463

    
464
static void omap_os_timer_reset(struct omap_32khz_timer_s *s)
465
{
466
    qemu_del_timer(s->timer.timer);
467
    s->timer.enable = 0;
468
    s->timer.it_ena = 0;
469
    s->timer.reset_val = 0x00ffffff;
470
    s->timer.val = 0;
471
    s->timer.st = 0;
472
    s->timer.ptv = 0;
473
    s->timer.ar = 1;
474
}
475

    
476
struct omap_32khz_timer_s *omap_os_timer_init(target_phys_addr_t base,
477
                qemu_irq irq, omap_clk clk)
478
{
479
    int iomemtype;
480
    struct omap_32khz_timer_s *s = (struct omap_32khz_timer_s *)
481
            qemu_mallocz(sizeof(struct omap_32khz_timer_s));
482

    
483
    s->timer.irq = irq;
484
    s->timer.clk = clk;
485
    s->timer.timer = qemu_new_timer(vm_clock, omap_timer_tick, &s->timer);
486
    omap_os_timer_reset(s);
487
    omap_timer_clk_setup(&s->timer);
488

    
489
    iomemtype = cpu_register_io_memory(omap_os_timer_readfn,
490
                    omap_os_timer_writefn, s);
491
    cpu_register_physical_memory(base, 0x800, iomemtype);
492

    
493
    return s;
494
}
495

    
496
/* Ultra Low-Power Device Module */
497
static uint32_t omap_ulpd_pm_read(void *opaque, target_phys_addr_t addr)
498
{
499
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
500
    uint16_t ret;
501

    
502
    switch (addr) {
503
    case 0x14:        /* IT_STATUS */
504
        ret = s->ulpd_pm_regs[addr >> 2];
505
        s->ulpd_pm_regs[addr >> 2] = 0;
506
        qemu_irq_lower(s->irq[1][OMAP_INT_GAUGE_32K]);
507
        return ret;
508

    
509
    case 0x18:        /* Reserved */
510
    case 0x1c:        /* Reserved */
511
    case 0x20:        /* Reserved */
512
    case 0x28:        /* Reserved */
513
    case 0x2c:        /* Reserved */
514
        OMAP_BAD_REG(addr);
515
    case 0x00:        /* COUNTER_32_LSB */
516
    case 0x04:        /* COUNTER_32_MSB */
517
    case 0x08:        /* COUNTER_HIGH_FREQ_LSB */
518
    case 0x0c:        /* COUNTER_HIGH_FREQ_MSB */
519
    case 0x10:        /* GAUGING_CTRL */
520
    case 0x24:        /* SETUP_ANALOG_CELL3_ULPD1 */
521
    case 0x30:        /* CLOCK_CTRL */
522
    case 0x34:        /* SOFT_REQ */
523
    case 0x38:        /* COUNTER_32_FIQ */
524
    case 0x3c:        /* DPLL_CTRL */
525
    case 0x40:        /* STATUS_REQ */
526
        /* XXX: check clk::usecount state for every clock */
527
    case 0x48:        /* LOCL_TIME */
528
    case 0x4c:        /* APLL_CTRL */
529
    case 0x50:        /* POWER_CTRL */
530
        return s->ulpd_pm_regs[addr >> 2];
531
    }
532

    
533
    OMAP_BAD_REG(addr);
534
    return 0;
535
}
536

    
537
static inline void omap_ulpd_clk_update(struct omap_mpu_state_s *s,
538
                uint16_t diff, uint16_t value)
539
{
540
    if (diff & (1 << 4))                                /* USB_MCLK_EN */
541
        omap_clk_onoff(omap_findclk(s, "usb_clk0"), (value >> 4) & 1);
542
    if (diff & (1 << 5))                                /* DIS_USB_PVCI_CLK */
543
        omap_clk_onoff(omap_findclk(s, "usb_w2fc_ck"), (~value >> 5) & 1);
544
}
545

    
546
static inline void omap_ulpd_req_update(struct omap_mpu_state_s *s,
547
                uint16_t diff, uint16_t value)
548
{
549
    if (diff & (1 << 0))                                /* SOFT_DPLL_REQ */
550
        omap_clk_canidle(omap_findclk(s, "dpll4"), (~value >> 0) & 1);
551
    if (diff & (1 << 1))                                /* SOFT_COM_REQ */
552
        omap_clk_canidle(omap_findclk(s, "com_mclk_out"), (~value >> 1) & 1);
553
    if (diff & (1 << 2))                                /* SOFT_SDW_REQ */
554
        omap_clk_canidle(omap_findclk(s, "bt_mclk_out"), (~value >> 2) & 1);
555
    if (diff & (1 << 3))                                /* SOFT_USB_REQ */
556
        omap_clk_canidle(omap_findclk(s, "usb_clk0"), (~value >> 3) & 1);
557
}
558

    
559
static void omap_ulpd_pm_write(void *opaque, target_phys_addr_t addr,
560
                uint32_t value)
561
{
562
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
563
    int64_t now, ticks;
564
    int div, mult;
565
    static const int bypass_div[4] = { 1, 2, 4, 4 };
566
    uint16_t diff;
567

    
568
    switch (addr) {
569
    case 0x00:        /* COUNTER_32_LSB */
570
    case 0x04:        /* COUNTER_32_MSB */
571
    case 0x08:        /* COUNTER_HIGH_FREQ_LSB */
572
    case 0x0c:        /* COUNTER_HIGH_FREQ_MSB */
573
    case 0x14:        /* IT_STATUS */
574
    case 0x40:        /* STATUS_REQ */
575
        OMAP_RO_REG(addr);
576
        break;
577

    
578
    case 0x10:        /* GAUGING_CTRL */
579
        /* Bits 0 and 1 seem to be confused in the OMAP 310 TRM */
580
        if ((s->ulpd_pm_regs[addr >> 2] ^ value) & 1) {
581
            now = qemu_get_clock(vm_clock);
582

    
583
            if (value & 1)
584
                s->ulpd_gauge_start = now;
585
            else {
586
                now -= s->ulpd_gauge_start;
587

    
588
                /* 32-kHz ticks */
589
                ticks = muldiv64(now, 32768, get_ticks_per_sec());
590
                s->ulpd_pm_regs[0x00 >> 2] = (ticks >>  0) & 0xffff;
591
                s->ulpd_pm_regs[0x04 >> 2] = (ticks >> 16) & 0xffff;
592
                if (ticks >> 32)        /* OVERFLOW_32K */
593
                    s->ulpd_pm_regs[0x14 >> 2] |= 1 << 2;
594

    
595
                /* High frequency ticks */
596
                ticks = muldiv64(now, 12000000, get_ticks_per_sec());
597
                s->ulpd_pm_regs[0x08 >> 2] = (ticks >>  0) & 0xffff;
598
                s->ulpd_pm_regs[0x0c >> 2] = (ticks >> 16) & 0xffff;
599
                if (ticks >> 32)        /* OVERFLOW_HI_FREQ */
600
                    s->ulpd_pm_regs[0x14 >> 2] |= 1 << 1;
601

    
602
                s->ulpd_pm_regs[0x14 >> 2] |= 1 << 0;        /* IT_GAUGING */
603
                qemu_irq_raise(s->irq[1][OMAP_INT_GAUGE_32K]);
604
            }
605
        }
606
        s->ulpd_pm_regs[addr >> 2] = value;
607
        break;
608

    
609
    case 0x18:        /* Reserved */
610
    case 0x1c:        /* Reserved */
611
    case 0x20:        /* Reserved */
612
    case 0x28:        /* Reserved */
613
    case 0x2c:        /* Reserved */
614
        OMAP_BAD_REG(addr);
615
    case 0x24:        /* SETUP_ANALOG_CELL3_ULPD1 */
616
    case 0x38:        /* COUNTER_32_FIQ */
617
    case 0x48:        /* LOCL_TIME */
618
    case 0x50:        /* POWER_CTRL */
619
        s->ulpd_pm_regs[addr >> 2] = value;
620
        break;
621

    
622
    case 0x30:        /* CLOCK_CTRL */
623
        diff = s->ulpd_pm_regs[addr >> 2] ^ value;
624
        s->ulpd_pm_regs[addr >> 2] = value & 0x3f;
625
        omap_ulpd_clk_update(s, diff, value);
626
        break;
627

    
628
    case 0x34:        /* SOFT_REQ */
629
        diff = s->ulpd_pm_regs[addr >> 2] ^ value;
630
        s->ulpd_pm_regs[addr >> 2] = value & 0x1f;
631
        omap_ulpd_req_update(s, diff, value);
632
        break;
633

    
634
    case 0x3c:        /* DPLL_CTRL */
635
        /* XXX: OMAP310 TRM claims bit 3 is PLL_ENABLE, and bit 4 is
636
         * omitted altogether, probably a typo.  */
637
        /* This register has identical semantics with DPLL(1:3) control
638
         * registers, see omap_dpll_write() */
639
        diff = s->ulpd_pm_regs[addr >> 2] & value;
640
        s->ulpd_pm_regs[addr >> 2] = value & 0x2fff;
641
        if (diff & (0x3ff << 2)) {
642
            if (value & (1 << 4)) {                        /* PLL_ENABLE */
643
                div = ((value >> 5) & 3) + 1;                /* PLL_DIV */
644
                mult = MIN((value >> 7) & 0x1f, 1);        /* PLL_MULT */
645
            } else {
646
                div = bypass_div[((value >> 2) & 3)];        /* BYPASS_DIV */
647
                mult = 1;
648
            }
649
            omap_clk_setrate(omap_findclk(s, "dpll4"), div, mult);
650
        }
651

    
652
        /* Enter the desired mode.  */
653
        s->ulpd_pm_regs[addr >> 2] =
654
                (s->ulpd_pm_regs[addr >> 2] & 0xfffe) |
655
                ((s->ulpd_pm_regs[addr >> 2] >> 4) & 1);
656

    
657
        /* Act as if the lock is restored.  */
658
        s->ulpd_pm_regs[addr >> 2] |= 2;
659
        break;
660

    
661
    case 0x4c:        /* APLL_CTRL */
662
        diff = s->ulpd_pm_regs[addr >> 2] & value;
663
        s->ulpd_pm_regs[addr >> 2] = value & 0xf;
664
        if (diff & (1 << 0))                                /* APLL_NDPLL_SWITCH */
665
            omap_clk_reparent(omap_findclk(s, "ck_48m"), omap_findclk(s,
666
                                    (value & (1 << 0)) ? "apll" : "dpll4"));
667
        break;
668

    
669
    default:
670
        OMAP_BAD_REG(addr);
671
    }
672
}
673

    
674
static CPUReadMemoryFunc * const omap_ulpd_pm_readfn[] = {
675
    omap_badwidth_read16,
676
    omap_ulpd_pm_read,
677
    omap_badwidth_read16,
678
};
679

    
680
static CPUWriteMemoryFunc * const omap_ulpd_pm_writefn[] = {
681
    omap_badwidth_write16,
682
    omap_ulpd_pm_write,
683
    omap_badwidth_write16,
684
};
685

    
686
static void omap_ulpd_pm_reset(struct omap_mpu_state_s *mpu)
687
{
688
    mpu->ulpd_pm_regs[0x00 >> 2] = 0x0001;
689
    mpu->ulpd_pm_regs[0x04 >> 2] = 0x0000;
690
    mpu->ulpd_pm_regs[0x08 >> 2] = 0x0001;
691
    mpu->ulpd_pm_regs[0x0c >> 2] = 0x0000;
692
    mpu->ulpd_pm_regs[0x10 >> 2] = 0x0000;
693
    mpu->ulpd_pm_regs[0x18 >> 2] = 0x01;
694
    mpu->ulpd_pm_regs[0x1c >> 2] = 0x01;
695
    mpu->ulpd_pm_regs[0x20 >> 2] = 0x01;
696
    mpu->ulpd_pm_regs[0x24 >> 2] = 0x03ff;
697
    mpu->ulpd_pm_regs[0x28 >> 2] = 0x01;
698
    mpu->ulpd_pm_regs[0x2c >> 2] = 0x01;
699
    omap_ulpd_clk_update(mpu, mpu->ulpd_pm_regs[0x30 >> 2], 0x0000);
700
    mpu->ulpd_pm_regs[0x30 >> 2] = 0x0000;
701
    omap_ulpd_req_update(mpu, mpu->ulpd_pm_regs[0x34 >> 2], 0x0000);
702
    mpu->ulpd_pm_regs[0x34 >> 2] = 0x0000;
703
    mpu->ulpd_pm_regs[0x38 >> 2] = 0x0001;
704
    mpu->ulpd_pm_regs[0x3c >> 2] = 0x2211;
705
    mpu->ulpd_pm_regs[0x40 >> 2] = 0x0000; /* FIXME: dump a real STATUS_REQ */
706
    mpu->ulpd_pm_regs[0x48 >> 2] = 0x960;
707
    mpu->ulpd_pm_regs[0x4c >> 2] = 0x08;
708
    mpu->ulpd_pm_regs[0x50 >> 2] = 0x08;
709
    omap_clk_setrate(omap_findclk(mpu, "dpll4"), 1, 4);
710
    omap_clk_reparent(omap_findclk(mpu, "ck_48m"), omap_findclk(mpu, "dpll4"));
711
}
712

    
713
static void omap_ulpd_pm_init(target_phys_addr_t base,
714
                struct omap_mpu_state_s *mpu)
715
{
716
    int iomemtype = cpu_register_io_memory(omap_ulpd_pm_readfn,
717
                    omap_ulpd_pm_writefn, mpu);
718

    
719
    cpu_register_physical_memory(base, 0x800, iomemtype);
720
    omap_ulpd_pm_reset(mpu);
721
}
722

    
723
/* OMAP Pin Configuration */
724
static uint32_t omap_pin_cfg_read(void *opaque, target_phys_addr_t addr)
725
{
726
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
727

    
728
    switch (addr) {
729
    case 0x00:        /* FUNC_MUX_CTRL_0 */
730
    case 0x04:        /* FUNC_MUX_CTRL_1 */
731
    case 0x08:        /* FUNC_MUX_CTRL_2 */
732
        return s->func_mux_ctrl[addr >> 2];
733

    
734
    case 0x0c:        /* COMP_MODE_CTRL_0 */
735
        return s->comp_mode_ctrl[0];
736

    
737
    case 0x10:        /* FUNC_MUX_CTRL_3 */
738
    case 0x14:        /* FUNC_MUX_CTRL_4 */
739
    case 0x18:        /* FUNC_MUX_CTRL_5 */
740
    case 0x1c:        /* FUNC_MUX_CTRL_6 */
741
    case 0x20:        /* FUNC_MUX_CTRL_7 */
742
    case 0x24:        /* FUNC_MUX_CTRL_8 */
743
    case 0x28:        /* FUNC_MUX_CTRL_9 */
744
    case 0x2c:        /* FUNC_MUX_CTRL_A */
745
    case 0x30:        /* FUNC_MUX_CTRL_B */
746
    case 0x34:        /* FUNC_MUX_CTRL_C */
747
    case 0x38:        /* FUNC_MUX_CTRL_D */
748
        return s->func_mux_ctrl[(addr >> 2) - 1];
749

    
750
    case 0x40:        /* PULL_DWN_CTRL_0 */
751
    case 0x44:        /* PULL_DWN_CTRL_1 */
752
    case 0x48:        /* PULL_DWN_CTRL_2 */
753
    case 0x4c:        /* PULL_DWN_CTRL_3 */
754
        return s->pull_dwn_ctrl[(addr & 0xf) >> 2];
755

    
756
    case 0x50:        /* GATE_INH_CTRL_0 */
757
        return s->gate_inh_ctrl[0];
758

    
759
    case 0x60:        /* VOLTAGE_CTRL_0 */
760
        return s->voltage_ctrl[0];
761

    
762
    case 0x70:        /* TEST_DBG_CTRL_0 */
763
        return s->test_dbg_ctrl[0];
764

    
765
    case 0x80:        /* MOD_CONF_CTRL_0 */
766
        return s->mod_conf_ctrl[0];
767
    }
768

    
769
    OMAP_BAD_REG(addr);
770
    return 0;
771
}
772

    
773
static inline void omap_pin_funcmux0_update(struct omap_mpu_state_s *s,
774
                uint32_t diff, uint32_t value)
775
{
776
    if (s->compat1509) {
777
        if (diff & (1 << 9))                        /* BLUETOOTH */
778
            omap_clk_onoff(omap_findclk(s, "bt_mclk_out"),
779
                            (~value >> 9) & 1);
780
        if (diff & (1 << 7))                        /* USB.CLKO */
781
            omap_clk_onoff(omap_findclk(s, "usb.clko"),
782
                            (value >> 7) & 1);
783
    }
784
}
785

    
786
static inline void omap_pin_funcmux1_update(struct omap_mpu_state_s *s,
787
                uint32_t diff, uint32_t value)
788
{
789
    if (s->compat1509) {
790
        if (diff & (1 << 31))                        /* MCBSP3_CLK_HIZ_DI */
791
            omap_clk_onoff(omap_findclk(s, "mcbsp3.clkx"),
792
                            (value >> 31) & 1);
793
        if (diff & (1 << 1))                        /* CLK32K */
794
            omap_clk_onoff(omap_findclk(s, "clk32k_out"),
795
                            (~value >> 1) & 1);
796
    }
797
}
798

    
799
static inline void omap_pin_modconf1_update(struct omap_mpu_state_s *s,
800
                uint32_t diff, uint32_t value)
801
{
802
    if (diff & (1 << 31))                        /* CONF_MOD_UART3_CLK_MODE_R */
803
         omap_clk_reparent(omap_findclk(s, "uart3_ck"),
804
                         omap_findclk(s, ((value >> 31) & 1) ?
805
                                 "ck_48m" : "armper_ck"));
806
    if (diff & (1 << 30))                        /* CONF_MOD_UART2_CLK_MODE_R */
807
         omap_clk_reparent(omap_findclk(s, "uart2_ck"),
808
                         omap_findclk(s, ((value >> 30) & 1) ?
809
                                 "ck_48m" : "armper_ck"));
810
    if (diff & (1 << 29))                        /* CONF_MOD_UART1_CLK_MODE_R */
811
         omap_clk_reparent(omap_findclk(s, "uart1_ck"),
812
                         omap_findclk(s, ((value >> 29) & 1) ?
813
                                 "ck_48m" : "armper_ck"));
814
    if (diff & (1 << 23))                        /* CONF_MOD_MMC_SD_CLK_REQ_R */
815
         omap_clk_reparent(omap_findclk(s, "mmc_ck"),
816
                         omap_findclk(s, ((value >> 23) & 1) ?
817
                                 "ck_48m" : "armper_ck"));
818
    if (diff & (1 << 12))                        /* CONF_MOD_COM_MCLK_12_48_S */
819
         omap_clk_reparent(omap_findclk(s, "com_mclk_out"),
820
                         omap_findclk(s, ((value >> 12) & 1) ?
821
                                 "ck_48m" : "armper_ck"));
822
    if (diff & (1 << 9))                        /* CONF_MOD_USB_HOST_HHC_UHO */
823
         omap_clk_onoff(omap_findclk(s, "usb_hhc_ck"), (value >> 9) & 1);
824
}
825

    
826
static void omap_pin_cfg_write(void *opaque, target_phys_addr_t addr,
827
                uint32_t value)
828
{
829
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
830
    uint32_t diff;
831

    
832
    switch (addr) {
833
    case 0x00:        /* FUNC_MUX_CTRL_0 */
834
        diff = s->func_mux_ctrl[addr >> 2] ^ value;
835
        s->func_mux_ctrl[addr >> 2] = value;
836
        omap_pin_funcmux0_update(s, diff, value);
837
        return;
838

    
839
    case 0x04:        /* FUNC_MUX_CTRL_1 */
840
        diff = s->func_mux_ctrl[addr >> 2] ^ value;
841
        s->func_mux_ctrl[addr >> 2] = value;
842
        omap_pin_funcmux1_update(s, diff, value);
843
        return;
844

    
845
    case 0x08:        /* FUNC_MUX_CTRL_2 */
846
        s->func_mux_ctrl[addr >> 2] = value;
847
        return;
848

    
849
    case 0x0c:        /* COMP_MODE_CTRL_0 */
850
        s->comp_mode_ctrl[0] = value;
851
        s->compat1509 = (value != 0x0000eaef);
852
        omap_pin_funcmux0_update(s, ~0, s->func_mux_ctrl[0]);
853
        omap_pin_funcmux1_update(s, ~0, s->func_mux_ctrl[1]);
854
        return;
855

    
856
    case 0x10:        /* FUNC_MUX_CTRL_3 */
857
    case 0x14:        /* FUNC_MUX_CTRL_4 */
858
    case 0x18:        /* FUNC_MUX_CTRL_5 */
859
    case 0x1c:        /* FUNC_MUX_CTRL_6 */
860
    case 0x20:        /* FUNC_MUX_CTRL_7 */
861
    case 0x24:        /* FUNC_MUX_CTRL_8 */
862
    case 0x28:        /* FUNC_MUX_CTRL_9 */
863
    case 0x2c:        /* FUNC_MUX_CTRL_A */
864
    case 0x30:        /* FUNC_MUX_CTRL_B */
865
    case 0x34:        /* FUNC_MUX_CTRL_C */
866
    case 0x38:        /* FUNC_MUX_CTRL_D */
867
        s->func_mux_ctrl[(addr >> 2) - 1] = value;
868
        return;
869

    
870
    case 0x40:        /* PULL_DWN_CTRL_0 */
871
    case 0x44:        /* PULL_DWN_CTRL_1 */
872
    case 0x48:        /* PULL_DWN_CTRL_2 */
873
    case 0x4c:        /* PULL_DWN_CTRL_3 */
874
        s->pull_dwn_ctrl[(addr & 0xf) >> 2] = value;
875
        return;
876

    
877
    case 0x50:        /* GATE_INH_CTRL_0 */
878
        s->gate_inh_ctrl[0] = value;
879
        return;
880

    
881
    case 0x60:        /* VOLTAGE_CTRL_0 */
882
        s->voltage_ctrl[0] = value;
883
        return;
884

    
885
    case 0x70:        /* TEST_DBG_CTRL_0 */
886
        s->test_dbg_ctrl[0] = value;
887
        return;
888

    
889
    case 0x80:        /* MOD_CONF_CTRL_0 */
890
        diff = s->mod_conf_ctrl[0] ^ value;
891
        s->mod_conf_ctrl[0] = value;
892
        omap_pin_modconf1_update(s, diff, value);
893
        return;
894

    
895
    default:
896
        OMAP_BAD_REG(addr);
897
    }
898
}
899

    
900
static CPUReadMemoryFunc * const omap_pin_cfg_readfn[] = {
901
    omap_badwidth_read32,
902
    omap_badwidth_read32,
903
    omap_pin_cfg_read,
904
};
905

    
906
static CPUWriteMemoryFunc * const omap_pin_cfg_writefn[] = {
907
    omap_badwidth_write32,
908
    omap_badwidth_write32,
909
    omap_pin_cfg_write,
910
};
911

    
912
static void omap_pin_cfg_reset(struct omap_mpu_state_s *mpu)
913
{
914
    /* Start in Compatibility Mode.  */
915
    mpu->compat1509 = 1;
916
    omap_pin_funcmux0_update(mpu, mpu->func_mux_ctrl[0], 0);
917
    omap_pin_funcmux1_update(mpu, mpu->func_mux_ctrl[1], 0);
918
    omap_pin_modconf1_update(mpu, mpu->mod_conf_ctrl[0], 0);
919
    memset(mpu->func_mux_ctrl, 0, sizeof(mpu->func_mux_ctrl));
920
    memset(mpu->comp_mode_ctrl, 0, sizeof(mpu->comp_mode_ctrl));
921
    memset(mpu->pull_dwn_ctrl, 0, sizeof(mpu->pull_dwn_ctrl));
922
    memset(mpu->gate_inh_ctrl, 0, sizeof(mpu->gate_inh_ctrl));
923
    memset(mpu->voltage_ctrl, 0, sizeof(mpu->voltage_ctrl));
924
    memset(mpu->test_dbg_ctrl, 0, sizeof(mpu->test_dbg_ctrl));
925
    memset(mpu->mod_conf_ctrl, 0, sizeof(mpu->mod_conf_ctrl));
926
}
927

    
928
static void omap_pin_cfg_init(target_phys_addr_t base,
929
                struct omap_mpu_state_s *mpu)
930
{
931
    int iomemtype = cpu_register_io_memory(omap_pin_cfg_readfn,
932
                    omap_pin_cfg_writefn, mpu);
933

    
934
    cpu_register_physical_memory(base, 0x800, iomemtype);
935
    omap_pin_cfg_reset(mpu);
936
}
937

    
938
/* Device Identification, Die Identification */
939
static uint32_t omap_id_read(void *opaque, target_phys_addr_t addr)
940
{
941
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
942

    
943
    switch (addr) {
944
    case 0xfffe1800:        /* DIE_ID_LSB */
945
        return 0xc9581f0e;
946
    case 0xfffe1804:        /* DIE_ID_MSB */
947
        return 0xa8858bfa;
948

    
949
    case 0xfffe2000:        /* PRODUCT_ID_LSB */
950
        return 0x00aaaafc;
951
    case 0xfffe2004:        /* PRODUCT_ID_MSB */
952
        return 0xcafeb574;
953

    
954
    case 0xfffed400:        /* JTAG_ID_LSB */
955
        switch (s->mpu_model) {
956
        case omap310:
957
            return 0x03310315;
958
        case omap1510:
959
            return 0x03310115;
960
        default:
961
            hw_error("%s: bad mpu model\n", __FUNCTION__);
962
        }
963
        break;
964

    
965
    case 0xfffed404:        /* JTAG_ID_MSB */
966
        switch (s->mpu_model) {
967
        case omap310:
968
            return 0xfb57402f;
969
        case omap1510:
970
            return 0xfb47002f;
971
        default:
972
            hw_error("%s: bad mpu model\n", __FUNCTION__);
973
        }
974
        break;
975
    }
976

    
977
    OMAP_BAD_REG(addr);
978
    return 0;
979
}
980

    
981
static void omap_id_write(void *opaque, target_phys_addr_t addr,
982
                uint32_t value)
983
{
984
    OMAP_BAD_REG(addr);
985
}
986

    
987
static CPUReadMemoryFunc * const omap_id_readfn[] = {
988
    omap_badwidth_read32,
989
    omap_badwidth_read32,
990
    omap_id_read,
991
};
992

    
993
static CPUWriteMemoryFunc * const omap_id_writefn[] = {
994
    omap_badwidth_write32,
995
    omap_badwidth_write32,
996
    omap_id_write,
997
};
998

    
999
static void omap_id_init(struct omap_mpu_state_s *mpu)
1000
{
1001
    int iomemtype = cpu_register_io_memory(omap_id_readfn,
1002
                    omap_id_writefn, mpu);
1003
    cpu_register_physical_memory_offset(0xfffe1800, 0x800, iomemtype, 0xfffe1800);
1004
    cpu_register_physical_memory_offset(0xfffed400, 0x100, iomemtype, 0xfffed400);
1005
    if (!cpu_is_omap15xx(mpu))
1006
        cpu_register_physical_memory_offset(0xfffe2000, 0x800, iomemtype, 0xfffe2000);
1007
}
1008

    
1009
/* MPUI Control (Dummy) */
1010
static uint32_t omap_mpui_read(void *opaque, target_phys_addr_t addr)
1011
{
1012
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1013

    
1014
    switch (addr) {
1015
    case 0x00:        /* CTRL */
1016
        return s->mpui_ctrl;
1017
    case 0x04:        /* DEBUG_ADDR */
1018
        return 0x01ffffff;
1019
    case 0x08:        /* DEBUG_DATA */
1020
        return 0xffffffff;
1021
    case 0x0c:        /* DEBUG_FLAG */
1022
        return 0x00000800;
1023
    case 0x10:        /* STATUS */
1024
        return 0x00000000;
1025

    
1026
    /* Not in OMAP310 */
1027
    case 0x14:        /* DSP_STATUS */
1028
    case 0x18:        /* DSP_BOOT_CONFIG */
1029
        return 0x00000000;
1030
    case 0x1c:        /* DSP_MPUI_CONFIG */
1031
        return 0x0000ffff;
1032
    }
1033

    
1034
    OMAP_BAD_REG(addr);
1035
    return 0;
1036
}
1037

    
1038
static void omap_mpui_write(void *opaque, target_phys_addr_t addr,
1039
                uint32_t value)
1040
{
1041
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1042

    
1043
    switch (addr) {
1044
    case 0x00:        /* CTRL */
1045
        s->mpui_ctrl = value & 0x007fffff;
1046
        break;
1047

    
1048
    case 0x04:        /* DEBUG_ADDR */
1049
    case 0x08:        /* DEBUG_DATA */
1050
    case 0x0c:        /* DEBUG_FLAG */
1051
    case 0x10:        /* STATUS */
1052
    /* Not in OMAP310 */
1053
    case 0x14:        /* DSP_STATUS */
1054
        OMAP_RO_REG(addr);
1055
    case 0x18:        /* DSP_BOOT_CONFIG */
1056
    case 0x1c:        /* DSP_MPUI_CONFIG */
1057
        break;
1058

    
1059
    default:
1060
        OMAP_BAD_REG(addr);
1061
    }
1062
}
1063

    
1064
static CPUReadMemoryFunc * const omap_mpui_readfn[] = {
1065
    omap_badwidth_read32,
1066
    omap_badwidth_read32,
1067
    omap_mpui_read,
1068
};
1069

    
1070
static CPUWriteMemoryFunc * const omap_mpui_writefn[] = {
1071
    omap_badwidth_write32,
1072
    omap_badwidth_write32,
1073
    omap_mpui_write,
1074
};
1075

    
1076
static void omap_mpui_reset(struct omap_mpu_state_s *s)
1077
{
1078
    s->mpui_ctrl = 0x0003ff1b;
1079
}
1080

    
1081
static void omap_mpui_init(target_phys_addr_t base,
1082
                struct omap_mpu_state_s *mpu)
1083
{
1084
    int iomemtype = cpu_register_io_memory(omap_mpui_readfn,
1085
                    omap_mpui_writefn, mpu);
1086

    
1087
    cpu_register_physical_memory(base, 0x100, iomemtype);
1088

    
1089
    omap_mpui_reset(mpu);
1090
}
1091

    
1092
/* TIPB Bridges */
1093
struct omap_tipb_bridge_s {
1094
    qemu_irq abort;
1095

    
1096
    int width_intr;
1097
    uint16_t control;
1098
    uint16_t alloc;
1099
    uint16_t buffer;
1100
    uint16_t enh_control;
1101
};
1102

    
1103
static uint32_t omap_tipb_bridge_read(void *opaque, target_phys_addr_t addr)
1104
{
1105
    struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1106

    
1107
    switch (addr) {
1108
    case 0x00:        /* TIPB_CNTL */
1109
        return s->control;
1110
    case 0x04:        /* TIPB_BUS_ALLOC */
1111
        return s->alloc;
1112
    case 0x08:        /* MPU_TIPB_CNTL */
1113
        return s->buffer;
1114
    case 0x0c:        /* ENHANCED_TIPB_CNTL */
1115
        return s->enh_control;
1116
    case 0x10:        /* ADDRESS_DBG */
1117
    case 0x14:        /* DATA_DEBUG_LOW */
1118
    case 0x18:        /* DATA_DEBUG_HIGH */
1119
        return 0xffff;
1120
    case 0x1c:        /* DEBUG_CNTR_SIG */
1121
        return 0x00f8;
1122
    }
1123

    
1124
    OMAP_BAD_REG(addr);
1125
    return 0;
1126
}
1127

    
1128
static void omap_tipb_bridge_write(void *opaque, target_phys_addr_t addr,
1129
                uint32_t value)
1130
{
1131
    struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *) opaque;
1132

    
1133
    switch (addr) {
1134
    case 0x00:        /* TIPB_CNTL */
1135
        s->control = value & 0xffff;
1136
        break;
1137

    
1138
    case 0x04:        /* TIPB_BUS_ALLOC */
1139
        s->alloc = value & 0x003f;
1140
        break;
1141

    
1142
    case 0x08:        /* MPU_TIPB_CNTL */
1143
        s->buffer = value & 0x0003;
1144
        break;
1145

    
1146
    case 0x0c:        /* ENHANCED_TIPB_CNTL */
1147
        s->width_intr = !(value & 2);
1148
        s->enh_control = value & 0x000f;
1149
        break;
1150

    
1151
    case 0x10:        /* ADDRESS_DBG */
1152
    case 0x14:        /* DATA_DEBUG_LOW */
1153
    case 0x18:        /* DATA_DEBUG_HIGH */
1154
    case 0x1c:        /* DEBUG_CNTR_SIG */
1155
        OMAP_RO_REG(addr);
1156
        break;
1157

    
1158
    default:
1159
        OMAP_BAD_REG(addr);
1160
    }
1161
}
1162

    
1163
static CPUReadMemoryFunc * const omap_tipb_bridge_readfn[] = {
1164
    omap_badwidth_read16,
1165
    omap_tipb_bridge_read,
1166
    omap_tipb_bridge_read,
1167
};
1168

    
1169
static CPUWriteMemoryFunc * const omap_tipb_bridge_writefn[] = {
1170
    omap_badwidth_write16,
1171
    omap_tipb_bridge_write,
1172
    omap_tipb_bridge_write,
1173
};
1174

    
1175
static void omap_tipb_bridge_reset(struct omap_tipb_bridge_s *s)
1176
{
1177
    s->control = 0xffff;
1178
    s->alloc = 0x0009;
1179
    s->buffer = 0x0000;
1180
    s->enh_control = 0x000f;
1181
}
1182

    
1183
struct omap_tipb_bridge_s *omap_tipb_bridge_init(target_phys_addr_t base,
1184
                qemu_irq abort_irq, omap_clk clk)
1185
{
1186
    int iomemtype;
1187
    struct omap_tipb_bridge_s *s = (struct omap_tipb_bridge_s *)
1188
            qemu_mallocz(sizeof(struct omap_tipb_bridge_s));
1189

    
1190
    s->abort = abort_irq;
1191
    omap_tipb_bridge_reset(s);
1192

    
1193
    iomemtype = cpu_register_io_memory(omap_tipb_bridge_readfn,
1194
                    omap_tipb_bridge_writefn, s);
1195
    cpu_register_physical_memory(base, 0x100, iomemtype);
1196

    
1197
    return s;
1198
}
1199

    
1200
/* Dummy Traffic Controller's Memory Interface */
1201
static uint32_t omap_tcmi_read(void *opaque, target_phys_addr_t addr)
1202
{
1203
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1204
    uint32_t ret;
1205

    
1206
    switch (addr) {
1207
    case 0x00:        /* IMIF_PRIO */
1208
    case 0x04:        /* EMIFS_PRIO */
1209
    case 0x08:        /* EMIFF_PRIO */
1210
    case 0x0c:        /* EMIFS_CONFIG */
1211
    case 0x10:        /* EMIFS_CS0_CONFIG */
1212
    case 0x14:        /* EMIFS_CS1_CONFIG */
1213
    case 0x18:        /* EMIFS_CS2_CONFIG */
1214
    case 0x1c:        /* EMIFS_CS3_CONFIG */
1215
    case 0x24:        /* EMIFF_MRS */
1216
    case 0x28:        /* TIMEOUT1 */
1217
    case 0x2c:        /* TIMEOUT2 */
1218
    case 0x30:        /* TIMEOUT3 */
1219
    case 0x3c:        /* EMIFF_SDRAM_CONFIG_2 */
1220
    case 0x40:        /* EMIFS_CFG_DYN_WAIT */
1221
        return s->tcmi_regs[addr >> 2];
1222

    
1223
    case 0x20:        /* EMIFF_SDRAM_CONFIG */
1224
        ret = s->tcmi_regs[addr >> 2];
1225
        s->tcmi_regs[addr >> 2] &= ~1; /* XXX: Clear SLRF on SDRAM access */
1226
        /* XXX: We can try using the VGA_DIRTY flag for this */
1227
        return ret;
1228
    }
1229

    
1230
    OMAP_BAD_REG(addr);
1231
    return 0;
1232
}
1233

    
1234
static void omap_tcmi_write(void *opaque, target_phys_addr_t addr,
1235
                uint32_t value)
1236
{
1237
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1238

    
1239
    switch (addr) {
1240
    case 0x00:        /* IMIF_PRIO */
1241
    case 0x04:        /* EMIFS_PRIO */
1242
    case 0x08:        /* EMIFF_PRIO */
1243
    case 0x10:        /* EMIFS_CS0_CONFIG */
1244
    case 0x14:        /* EMIFS_CS1_CONFIG */
1245
    case 0x18:        /* EMIFS_CS2_CONFIG */
1246
    case 0x1c:        /* EMIFS_CS3_CONFIG */
1247
    case 0x20:        /* EMIFF_SDRAM_CONFIG */
1248
    case 0x24:        /* EMIFF_MRS */
1249
    case 0x28:        /* TIMEOUT1 */
1250
    case 0x2c:        /* TIMEOUT2 */
1251
    case 0x30:        /* TIMEOUT3 */
1252
    case 0x3c:        /* EMIFF_SDRAM_CONFIG_2 */
1253
    case 0x40:        /* EMIFS_CFG_DYN_WAIT */
1254
        s->tcmi_regs[addr >> 2] = value;
1255
        break;
1256
    case 0x0c:        /* EMIFS_CONFIG */
1257
        s->tcmi_regs[addr >> 2] = (value & 0xf) | (1 << 4);
1258
        break;
1259

    
1260
    default:
1261
        OMAP_BAD_REG(addr);
1262
    }
1263
}
1264

    
1265
static CPUReadMemoryFunc * const omap_tcmi_readfn[] = {
1266
    omap_badwidth_read32,
1267
    omap_badwidth_read32,
1268
    omap_tcmi_read,
1269
};
1270

    
1271
static CPUWriteMemoryFunc * const omap_tcmi_writefn[] = {
1272
    omap_badwidth_write32,
1273
    omap_badwidth_write32,
1274
    omap_tcmi_write,
1275
};
1276

    
1277
static void omap_tcmi_reset(struct omap_mpu_state_s *mpu)
1278
{
1279
    mpu->tcmi_regs[0x00 >> 2] = 0x00000000;
1280
    mpu->tcmi_regs[0x04 >> 2] = 0x00000000;
1281
    mpu->tcmi_regs[0x08 >> 2] = 0x00000000;
1282
    mpu->tcmi_regs[0x0c >> 2] = 0x00000010;
1283
    mpu->tcmi_regs[0x10 >> 2] = 0x0010fffb;
1284
    mpu->tcmi_regs[0x14 >> 2] = 0x0010fffb;
1285
    mpu->tcmi_regs[0x18 >> 2] = 0x0010fffb;
1286
    mpu->tcmi_regs[0x1c >> 2] = 0x0010fffb;
1287
    mpu->tcmi_regs[0x20 >> 2] = 0x00618800;
1288
    mpu->tcmi_regs[0x24 >> 2] = 0x00000037;
1289
    mpu->tcmi_regs[0x28 >> 2] = 0x00000000;
1290
    mpu->tcmi_regs[0x2c >> 2] = 0x00000000;
1291
    mpu->tcmi_regs[0x30 >> 2] = 0x00000000;
1292
    mpu->tcmi_regs[0x3c >> 2] = 0x00000003;
1293
    mpu->tcmi_regs[0x40 >> 2] = 0x00000000;
1294
}
1295

    
1296
static void omap_tcmi_init(target_phys_addr_t base,
1297
                struct omap_mpu_state_s *mpu)
1298
{
1299
    int iomemtype = cpu_register_io_memory(omap_tcmi_readfn,
1300
                    omap_tcmi_writefn, mpu);
1301

    
1302
    cpu_register_physical_memory(base, 0x100, iomemtype);
1303
    omap_tcmi_reset(mpu);
1304
}
1305

    
1306
/* Digital phase-locked loops control */
1307
static uint32_t omap_dpll_read(void *opaque, target_phys_addr_t addr)
1308
{
1309
    struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1310

    
1311
    if (addr == 0x00)        /* CTL_REG */
1312
        return s->mode;
1313

    
1314
    OMAP_BAD_REG(addr);
1315
    return 0;
1316
}
1317

    
1318
static void omap_dpll_write(void *opaque, target_phys_addr_t addr,
1319
                uint32_t value)
1320
{
1321
    struct dpll_ctl_s *s = (struct dpll_ctl_s *) opaque;
1322
    uint16_t diff;
1323
    static const int bypass_div[4] = { 1, 2, 4, 4 };
1324
    int div, mult;
1325

    
1326
    if (addr == 0x00) {        /* CTL_REG */
1327
        /* See omap_ulpd_pm_write() too */
1328
        diff = s->mode & value;
1329
        s->mode = value & 0x2fff;
1330
        if (diff & (0x3ff << 2)) {
1331
            if (value & (1 << 4)) {                        /* PLL_ENABLE */
1332
                div = ((value >> 5) & 3) + 1;                /* PLL_DIV */
1333
                mult = MIN((value >> 7) & 0x1f, 1);        /* PLL_MULT */
1334
            } else {
1335
                div = bypass_div[((value >> 2) & 3)];        /* BYPASS_DIV */
1336
                mult = 1;
1337
            }
1338
            omap_clk_setrate(s->dpll, div, mult);
1339
        }
1340

    
1341
        /* Enter the desired mode.  */
1342
        s->mode = (s->mode & 0xfffe) | ((s->mode >> 4) & 1);
1343

    
1344
        /* Act as if the lock is restored.  */
1345
        s->mode |= 2;
1346
    } else {
1347
        OMAP_BAD_REG(addr);
1348
    }
1349
}
1350

    
1351
static CPUReadMemoryFunc * const omap_dpll_readfn[] = {
1352
    omap_badwidth_read16,
1353
    omap_dpll_read,
1354
    omap_badwidth_read16,
1355
};
1356

    
1357
static CPUWriteMemoryFunc * const omap_dpll_writefn[] = {
1358
    omap_badwidth_write16,
1359
    omap_dpll_write,
1360
    omap_badwidth_write16,
1361
};
1362

    
1363
static void omap_dpll_reset(struct dpll_ctl_s *s)
1364
{
1365
    s->mode = 0x2002;
1366
    omap_clk_setrate(s->dpll, 1, 1);
1367
}
1368

    
1369
static void omap_dpll_init(struct dpll_ctl_s *s, target_phys_addr_t base,
1370
                omap_clk clk)
1371
{
1372
    int iomemtype = cpu_register_io_memory(omap_dpll_readfn,
1373
                    omap_dpll_writefn, s);
1374

    
1375
    s->dpll = clk;
1376
    omap_dpll_reset(s);
1377

    
1378
    cpu_register_physical_memory(base, 0x100, iomemtype);
1379
}
1380

    
1381
/* UARTs */
1382
struct omap_uart_s {
1383
    target_phys_addr_t base;
1384
    SerialState *serial; /* TODO */
1385
    struct omap_target_agent_s *ta;
1386
    omap_clk fclk;
1387
    qemu_irq irq;
1388

    
1389
    uint8_t eblr;
1390
    uint8_t syscontrol;
1391
    uint8_t wkup;
1392
    uint8_t cfps;
1393
    uint8_t mdr[2];
1394
    uint8_t scr;
1395
    uint8_t clksel;
1396
};
1397

    
1398
void omap_uart_reset(struct omap_uart_s *s)
1399
{
1400
    s->eblr = 0x00;
1401
    s->syscontrol = 0;
1402
    s->wkup = 0x3f;
1403
    s->cfps = 0x69;
1404
    s->clksel = 0;
1405
}
1406

    
1407
struct omap_uart_s *omap_uart_init(target_phys_addr_t base,
1408
                qemu_irq irq, omap_clk fclk, omap_clk iclk,
1409
                qemu_irq txdma, qemu_irq rxdma, CharDriverState *chr)
1410
{
1411
    struct omap_uart_s *s = (struct omap_uart_s *)
1412
            qemu_mallocz(sizeof(struct omap_uart_s));
1413

    
1414
    s->base = base;
1415
    s->fclk = fclk;
1416
    s->irq = irq;
1417
#ifdef TARGET_WORDS_BIGENDIAN
1418
    s->serial = serial_mm_init(base, 2, irq, omap_clk_getrate(fclk)/16,
1419
                               chr ?: qemu_chr_open("null", "null", NULL), 1,
1420
                               1);
1421
#else
1422
    s->serial = serial_mm_init(base, 2, irq, omap_clk_getrate(fclk)/16,
1423
                               chr ?: qemu_chr_open("null", "null", NULL), 1,
1424
                               0);
1425
#endif
1426
    return s;
1427
}
1428

    
1429
static uint32_t omap_uart_read(void *opaque, target_phys_addr_t addr)
1430
{
1431
    struct omap_uart_s *s = (struct omap_uart_s *) opaque;
1432

    
1433
    addr &= 0xff;
1434
    switch (addr) {
1435
    case 0x20:        /* MDR1 */
1436
        return s->mdr[0];
1437
    case 0x24:        /* MDR2 */
1438
        return s->mdr[1];
1439
    case 0x40:        /* SCR */
1440
        return s->scr;
1441
    case 0x44:        /* SSR */
1442
        return 0x0;
1443
    case 0x48:        /* EBLR (OMAP2) */
1444
        return s->eblr;
1445
    case 0x4C:        /* OSC_12M_SEL (OMAP1) */
1446
        return s->clksel;
1447
    case 0x50:        /* MVR */
1448
        return 0x30;
1449
    case 0x54:        /* SYSC (OMAP2) */
1450
        return s->syscontrol;
1451
    case 0x58:        /* SYSS (OMAP2) */
1452
        return 1;
1453
    case 0x5c:        /* WER (OMAP2) */
1454
        return s->wkup;
1455
    case 0x60:        /* CFPS (OMAP2) */
1456
        return s->cfps;
1457
    }
1458

    
1459
    OMAP_BAD_REG(addr);
1460
    return 0;
1461
}
1462

    
1463
static void omap_uart_write(void *opaque, target_phys_addr_t addr,
1464
                uint32_t value)
1465
{
1466
    struct omap_uart_s *s = (struct omap_uart_s *) opaque;
1467

    
1468
    addr &= 0xff;
1469
    switch (addr) {
1470
    case 0x20:        /* MDR1 */
1471
        s->mdr[0] = value & 0x7f;
1472
        break;
1473
    case 0x24:        /* MDR2 */
1474
        s->mdr[1] = value & 0xff;
1475
        break;
1476
    case 0x40:        /* SCR */
1477
        s->scr = value & 0xff;
1478
        break;
1479
    case 0x48:        /* EBLR (OMAP2) */
1480
        s->eblr = value & 0xff;
1481
        break;
1482
    case 0x4C:        /* OSC_12M_SEL (OMAP1) */
1483
        s->clksel = value & 1;
1484
        break;
1485
    case 0x44:        /* SSR */
1486
    case 0x50:        /* MVR */
1487
    case 0x58:        /* SYSS (OMAP2) */
1488
        OMAP_RO_REG(addr);
1489
        break;
1490
    case 0x54:        /* SYSC (OMAP2) */
1491
        s->syscontrol = value & 0x1d;
1492
        if (value & 2)
1493
            omap_uart_reset(s);
1494
        break;
1495
    case 0x5c:        /* WER (OMAP2) */
1496
        s->wkup = value & 0x7f;
1497
        break;
1498
    case 0x60:        /* CFPS (OMAP2) */
1499
        s->cfps = value & 0xff;
1500
        break;
1501
    default:
1502
        OMAP_BAD_REG(addr);
1503
    }
1504
}
1505

    
1506
static CPUReadMemoryFunc * const omap_uart_readfn[] = {
1507
    omap_uart_read,
1508
    omap_uart_read,
1509
    omap_badwidth_read8,
1510
};
1511

    
1512
static CPUWriteMemoryFunc * const omap_uart_writefn[] = {
1513
    omap_uart_write,
1514
    omap_uart_write,
1515
    omap_badwidth_write8,
1516
};
1517

    
1518
struct omap_uart_s *omap2_uart_init(struct omap_target_agent_s *ta,
1519
                qemu_irq irq, omap_clk fclk, omap_clk iclk,
1520
                qemu_irq txdma, qemu_irq rxdma, CharDriverState *chr)
1521
{
1522
    target_phys_addr_t base = omap_l4_attach(ta, 0, 0);
1523
    struct omap_uart_s *s = omap_uart_init(base, irq,
1524
                    fclk, iclk, txdma, rxdma, chr);
1525
    int iomemtype = cpu_register_io_memory(omap_uart_readfn,
1526
                    omap_uart_writefn, s);
1527

    
1528
    s->ta = ta;
1529

    
1530
    cpu_register_physical_memory(base + 0x20, 0x100, iomemtype);
1531

    
1532
    return s;
1533
}
1534

    
1535
void omap_uart_attach(struct omap_uart_s *s, CharDriverState *chr)
1536
{
1537
    /* TODO: Should reuse or destroy current s->serial */
1538
#ifdef TARGET_WORDS_BIGENDIAN
1539
    s->serial = serial_mm_init(s->base, 2, s->irq,
1540
                               omap_clk_getrate(s->fclk) / 16,
1541
                               chr ?: qemu_chr_open("null", "null", NULL), 1,
1542
                               1);
1543
#else
1544
    s->serial = serial_mm_init(s->base, 2, s->irq,
1545
                               omap_clk_getrate(s->fclk) / 16,
1546
                               chr ?: qemu_chr_open("null", "null", NULL), 1,
1547
                               0);
1548
#endif
1549
}
1550

    
1551
/* MPU Clock/Reset/Power Mode Control */
1552
static uint32_t omap_clkm_read(void *opaque, target_phys_addr_t addr)
1553
{
1554
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1555

    
1556
    switch (addr) {
1557
    case 0x00:        /* ARM_CKCTL */
1558
        return s->clkm.arm_ckctl;
1559

    
1560
    case 0x04:        /* ARM_IDLECT1 */
1561
        return s->clkm.arm_idlect1;
1562

    
1563
    case 0x08:        /* ARM_IDLECT2 */
1564
        return s->clkm.arm_idlect2;
1565

    
1566
    case 0x0c:        /* ARM_EWUPCT */
1567
        return s->clkm.arm_ewupct;
1568

    
1569
    case 0x10:        /* ARM_RSTCT1 */
1570
        return s->clkm.arm_rstct1;
1571

    
1572
    case 0x14:        /* ARM_RSTCT2 */
1573
        return s->clkm.arm_rstct2;
1574

    
1575
    case 0x18:        /* ARM_SYSST */
1576
        return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start;
1577

    
1578
    case 0x1c:        /* ARM_CKOUT1 */
1579
        return s->clkm.arm_ckout1;
1580

    
1581
    case 0x20:        /* ARM_CKOUT2 */
1582
        break;
1583
    }
1584

    
1585
    OMAP_BAD_REG(addr);
1586
    return 0;
1587
}
1588

    
1589
static inline void omap_clkm_ckctl_update(struct omap_mpu_state_s *s,
1590
                uint16_t diff, uint16_t value)
1591
{
1592
    omap_clk clk;
1593

    
1594
    if (diff & (1 << 14)) {                                /* ARM_INTHCK_SEL */
1595
        if (value & (1 << 14))
1596
            /* Reserved */;
1597
        else {
1598
            clk = omap_findclk(s, "arminth_ck");
1599
            omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
1600
        }
1601
    }
1602
    if (diff & (1 << 12)) {                                /* ARM_TIMXO */
1603
        clk = omap_findclk(s, "armtim_ck");
1604
        if (value & (1 << 12))
1605
            omap_clk_reparent(clk, omap_findclk(s, "clkin"));
1606
        else
1607
            omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
1608
    }
1609
    /* XXX: en_dspck */
1610
    if (diff & (3 << 10)) {                                /* DSPMMUDIV */
1611
        clk = omap_findclk(s, "dspmmu_ck");
1612
        omap_clk_setrate(clk, 1 << ((value >> 10) & 3), 1);
1613
    }
1614
    if (diff & (3 << 8)) {                                /* TCDIV */
1615
        clk = omap_findclk(s, "tc_ck");
1616
        omap_clk_setrate(clk, 1 << ((value >> 8) & 3), 1);
1617
    }
1618
    if (diff & (3 << 6)) {                                /* DSPDIV */
1619
        clk = omap_findclk(s, "dsp_ck");
1620
        omap_clk_setrate(clk, 1 << ((value >> 6) & 3), 1);
1621
    }
1622
    if (diff & (3 << 4)) {                                /* ARMDIV */
1623
        clk = omap_findclk(s, "arm_ck");
1624
        omap_clk_setrate(clk, 1 << ((value >> 4) & 3), 1);
1625
    }
1626
    if (diff & (3 << 2)) {                                /* LCDDIV */
1627
        clk = omap_findclk(s, "lcd_ck");
1628
        omap_clk_setrate(clk, 1 << ((value >> 2) & 3), 1);
1629
    }
1630
    if (diff & (3 << 0)) {                                /* PERDIV */
1631
        clk = omap_findclk(s, "armper_ck");
1632
        omap_clk_setrate(clk, 1 << ((value >> 0) & 3), 1);
1633
    }
1634
}
1635

    
1636
static inline void omap_clkm_idlect1_update(struct omap_mpu_state_s *s,
1637
                uint16_t diff, uint16_t value)
1638
{
1639
    omap_clk clk;
1640

    
1641
    if (value & (1 << 11))                                /* SETARM_IDLE */
1642
        cpu_interrupt(s->env, CPU_INTERRUPT_HALT);
1643
    if (!(value & (1 << 10)))                                /* WKUP_MODE */
1644
        qemu_system_shutdown_request();        /* XXX: disable wakeup from IRQ */
1645

    
1646
#define SET_CANIDLE(clock, bit)                                \
1647
    if (diff & (1 << bit)) {                                \
1648
        clk = omap_findclk(s, clock);                        \
1649
        omap_clk_canidle(clk, (value >> bit) & 1);        \
1650
    }
1651
    SET_CANIDLE("mpuwd_ck", 0)                                /* IDLWDT_ARM */
1652
    SET_CANIDLE("armxor_ck", 1)                                /* IDLXORP_ARM */
1653
    SET_CANIDLE("mpuper_ck", 2)                                /* IDLPER_ARM */
1654
    SET_CANIDLE("lcd_ck", 3)                                /* IDLLCD_ARM */
1655
    SET_CANIDLE("lb_ck", 4)                                /* IDLLB_ARM */
1656
    SET_CANIDLE("hsab_ck", 5)                                /* IDLHSAB_ARM */
1657
    SET_CANIDLE("tipb_ck", 6)                                /* IDLIF_ARM */
1658
    SET_CANIDLE("dma_ck", 6)                                /* IDLIF_ARM */
1659
    SET_CANIDLE("tc_ck", 6)                                /* IDLIF_ARM */
1660
    SET_CANIDLE("dpll1", 7)                                /* IDLDPLL_ARM */
1661
    SET_CANIDLE("dpll2", 7)                                /* IDLDPLL_ARM */
1662
    SET_CANIDLE("dpll3", 7)                                /* IDLDPLL_ARM */
1663
    SET_CANIDLE("mpui_ck", 8)                                /* IDLAPI_ARM */
1664
    SET_CANIDLE("armtim_ck", 9)                                /* IDLTIM_ARM */
1665
}
1666

    
1667
static inline void omap_clkm_idlect2_update(struct omap_mpu_state_s *s,
1668
                uint16_t diff, uint16_t value)
1669
{
1670
    omap_clk clk;
1671

    
1672
#define SET_ONOFF(clock, bit)                                \
1673
    if (diff & (1 << bit)) {                                \
1674
        clk = omap_findclk(s, clock);                        \
1675
        omap_clk_onoff(clk, (value >> bit) & 1);        \
1676
    }
1677
    SET_ONOFF("mpuwd_ck", 0)                                /* EN_WDTCK */
1678
    SET_ONOFF("armxor_ck", 1)                                /* EN_XORPCK */
1679
    SET_ONOFF("mpuper_ck", 2)                                /* EN_PERCK */
1680
    SET_ONOFF("lcd_ck", 3)                                /* EN_LCDCK */
1681
    SET_ONOFF("lb_ck", 4)                                /* EN_LBCK */
1682
    SET_ONOFF("hsab_ck", 5)                                /* EN_HSABCK */
1683
    SET_ONOFF("mpui_ck", 6)                                /* EN_APICK */
1684
    SET_ONOFF("armtim_ck", 7)                                /* EN_TIMCK */
1685
    SET_CANIDLE("dma_ck", 8)                                /* DMACK_REQ */
1686
    SET_ONOFF("arm_gpio_ck", 9)                                /* EN_GPIOCK */
1687
    SET_ONOFF("lbfree_ck", 10)                                /* EN_LBFREECK */
1688
}
1689

    
1690
static inline void omap_clkm_ckout1_update(struct omap_mpu_state_s *s,
1691
                uint16_t diff, uint16_t value)
1692
{
1693
    omap_clk clk;
1694

    
1695
    if (diff & (3 << 4)) {                                /* TCLKOUT */
1696
        clk = omap_findclk(s, "tclk_out");
1697
        switch ((value >> 4) & 3) {
1698
        case 1:
1699
            omap_clk_reparent(clk, omap_findclk(s, "ck_gen3"));
1700
            omap_clk_onoff(clk, 1);
1701
            break;
1702
        case 2:
1703
            omap_clk_reparent(clk, omap_findclk(s, "tc_ck"));
1704
            omap_clk_onoff(clk, 1);
1705
            break;
1706
        default:
1707
            omap_clk_onoff(clk, 0);
1708
        }
1709
    }
1710
    if (diff & (3 << 2)) {                                /* DCLKOUT */
1711
        clk = omap_findclk(s, "dclk_out");
1712
        switch ((value >> 2) & 3) {
1713
        case 0:
1714
            omap_clk_reparent(clk, omap_findclk(s, "dspmmu_ck"));
1715
            break;
1716
        case 1:
1717
            omap_clk_reparent(clk, omap_findclk(s, "ck_gen2"));
1718
            break;
1719
        case 2:
1720
            omap_clk_reparent(clk, omap_findclk(s, "dsp_ck"));
1721
            break;
1722
        case 3:
1723
            omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
1724
            break;
1725
        }
1726
    }
1727
    if (diff & (3 << 0)) {                                /* ACLKOUT */
1728
        clk = omap_findclk(s, "aclk_out");
1729
        switch ((value >> 0) & 3) {
1730
        case 1:
1731
            omap_clk_reparent(clk, omap_findclk(s, "ck_gen1"));
1732
            omap_clk_onoff(clk, 1);
1733
            break;
1734
        case 2:
1735
            omap_clk_reparent(clk, omap_findclk(s, "arm_ck"));
1736
            omap_clk_onoff(clk, 1);
1737
            break;
1738
        case 3:
1739
            omap_clk_reparent(clk, omap_findclk(s, "ck_ref14"));
1740
            omap_clk_onoff(clk, 1);
1741
            break;
1742
        default:
1743
            omap_clk_onoff(clk, 0);
1744
        }
1745
    }
1746
}
1747

    
1748
static void omap_clkm_write(void *opaque, target_phys_addr_t addr,
1749
                uint32_t value)
1750
{
1751
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1752
    uint16_t diff;
1753
    omap_clk clk;
1754
    static const char *clkschemename[8] = {
1755
        "fully synchronous", "fully asynchronous", "synchronous scalable",
1756
        "mix mode 1", "mix mode 2", "bypass mode", "mix mode 3", "mix mode 4",
1757
    };
1758

    
1759
    switch (addr) {
1760
    case 0x00:        /* ARM_CKCTL */
1761
        diff = s->clkm.arm_ckctl ^ value;
1762
        s->clkm.arm_ckctl = value & 0x7fff;
1763
        omap_clkm_ckctl_update(s, diff, value);
1764
        return;
1765

    
1766
    case 0x04:        /* ARM_IDLECT1 */
1767
        diff = s->clkm.arm_idlect1 ^ value;
1768
        s->clkm.arm_idlect1 = value & 0x0fff;
1769
        omap_clkm_idlect1_update(s, diff, value);
1770
        return;
1771

    
1772
    case 0x08:        /* ARM_IDLECT2 */
1773
        diff = s->clkm.arm_idlect2 ^ value;
1774
        s->clkm.arm_idlect2 = value & 0x07ff;
1775
        omap_clkm_idlect2_update(s, diff, value);
1776
        return;
1777

    
1778
    case 0x0c:        /* ARM_EWUPCT */
1779
        s->clkm.arm_ewupct = value & 0x003f;
1780
        return;
1781

    
1782
    case 0x10:        /* ARM_RSTCT1 */
1783
        diff = s->clkm.arm_rstct1 ^ value;
1784
        s->clkm.arm_rstct1 = value & 0x0007;
1785
        if (value & 9) {
1786
            qemu_system_reset_request();
1787
            s->clkm.cold_start = 0xa;
1788
        }
1789
        if (diff & ~value & 4) {                                /* DSP_RST */
1790
            omap_mpui_reset(s);
1791
            omap_tipb_bridge_reset(s->private_tipb);
1792
            omap_tipb_bridge_reset(s->public_tipb);
1793
        }
1794
        if (diff & 2) {                                                /* DSP_EN */
1795
            clk = omap_findclk(s, "dsp_ck");
1796
            omap_clk_canidle(clk, (~value >> 1) & 1);
1797
        }
1798
        return;
1799

    
1800
    case 0x14:        /* ARM_RSTCT2 */
1801
        s->clkm.arm_rstct2 = value & 0x0001;
1802
        return;
1803

    
1804
    case 0x18:        /* ARM_SYSST */
1805
        if ((s->clkm.clocking_scheme ^ (value >> 11)) & 7) {
1806
            s->clkm.clocking_scheme = (value >> 11) & 7;
1807
            printf("%s: clocking scheme set to %s\n", __FUNCTION__,
1808
                            clkschemename[s->clkm.clocking_scheme]);
1809
        }
1810
        s->clkm.cold_start &= value & 0x3f;
1811
        return;
1812

    
1813
    case 0x1c:        /* ARM_CKOUT1 */
1814
        diff = s->clkm.arm_ckout1 ^ value;
1815
        s->clkm.arm_ckout1 = value & 0x003f;
1816
        omap_clkm_ckout1_update(s, diff, value);
1817
        return;
1818

    
1819
    case 0x20:        /* ARM_CKOUT2 */
1820
    default:
1821
        OMAP_BAD_REG(addr);
1822
    }
1823
}
1824

    
1825
static CPUReadMemoryFunc * const omap_clkm_readfn[] = {
1826
    omap_badwidth_read16,
1827
    omap_clkm_read,
1828
    omap_badwidth_read16,
1829
};
1830

    
1831
static CPUWriteMemoryFunc * const omap_clkm_writefn[] = {
1832
    omap_badwidth_write16,
1833
    omap_clkm_write,
1834
    omap_badwidth_write16,
1835
};
1836

    
1837
static uint32_t omap_clkdsp_read(void *opaque, target_phys_addr_t addr)
1838
{
1839
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1840

    
1841
    switch (addr) {
1842
    case 0x04:        /* DSP_IDLECT1 */
1843
        return s->clkm.dsp_idlect1;
1844

    
1845
    case 0x08:        /* DSP_IDLECT2 */
1846
        return s->clkm.dsp_idlect2;
1847

    
1848
    case 0x14:        /* DSP_RSTCT2 */
1849
        return s->clkm.dsp_rstct2;
1850

    
1851
    case 0x18:        /* DSP_SYSST */
1852
        return (s->clkm.clocking_scheme << 11) | s->clkm.cold_start |
1853
                (s->env->halted << 6);        /* Quite useless... */
1854
    }
1855

    
1856
    OMAP_BAD_REG(addr);
1857
    return 0;
1858
}
1859

    
1860
static inline void omap_clkdsp_idlect1_update(struct omap_mpu_state_s *s,
1861
                uint16_t diff, uint16_t value)
1862
{
1863
    omap_clk clk;
1864

    
1865
    SET_CANIDLE("dspxor_ck", 1);                        /* IDLXORP_DSP */
1866
}
1867

    
1868
static inline void omap_clkdsp_idlect2_update(struct omap_mpu_state_s *s,
1869
                uint16_t diff, uint16_t value)
1870
{
1871
    omap_clk clk;
1872

    
1873
    SET_ONOFF("dspxor_ck", 1);                                /* EN_XORPCK */
1874
}
1875

    
1876
static void omap_clkdsp_write(void *opaque, target_phys_addr_t addr,
1877
                uint32_t value)
1878
{
1879
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
1880
    uint16_t diff;
1881

    
1882
    switch (addr) {
1883
    case 0x04:        /* DSP_IDLECT1 */
1884
        diff = s->clkm.dsp_idlect1 ^ value;
1885
        s->clkm.dsp_idlect1 = value & 0x01f7;
1886
        omap_clkdsp_idlect1_update(s, diff, value);
1887
        break;
1888

    
1889
    case 0x08:        /* DSP_IDLECT2 */
1890
        s->clkm.dsp_idlect2 = value & 0x0037;
1891
        diff = s->clkm.dsp_idlect1 ^ value;
1892
        omap_clkdsp_idlect2_update(s, diff, value);
1893
        break;
1894

    
1895
    case 0x14:        /* DSP_RSTCT2 */
1896
        s->clkm.dsp_rstct2 = value & 0x0001;
1897
        break;
1898

    
1899
    case 0x18:        /* DSP_SYSST */
1900
        s->clkm.cold_start &= value & 0x3f;
1901
        break;
1902

    
1903
    default:
1904
        OMAP_BAD_REG(addr);
1905
    }
1906
}
1907

    
1908
static CPUReadMemoryFunc * const omap_clkdsp_readfn[] = {
1909
    omap_badwidth_read16,
1910
    omap_clkdsp_read,
1911
    omap_badwidth_read16,
1912
};
1913

    
1914
static CPUWriteMemoryFunc * const omap_clkdsp_writefn[] = {
1915
    omap_badwidth_write16,
1916
    omap_clkdsp_write,
1917
    omap_badwidth_write16,
1918
};
1919

    
1920
static void omap_clkm_reset(struct omap_mpu_state_s *s)
1921
{
1922
    if (s->wdt && s->wdt->reset)
1923
        s->clkm.cold_start = 0x6;
1924
    s->clkm.clocking_scheme = 0;
1925
    omap_clkm_ckctl_update(s, ~0, 0x3000);
1926
    s->clkm.arm_ckctl = 0x3000;
1927
    omap_clkm_idlect1_update(s, s->clkm.arm_idlect1 ^ 0x0400, 0x0400);
1928
    s->clkm.arm_idlect1 = 0x0400;
1929
    omap_clkm_idlect2_update(s, s->clkm.arm_idlect2 ^ 0x0100, 0x0100);
1930
    s->clkm.arm_idlect2 = 0x0100;
1931
    s->clkm.arm_ewupct = 0x003f;
1932
    s->clkm.arm_rstct1 = 0x0000;
1933
    s->clkm.arm_rstct2 = 0x0000;
1934
    s->clkm.arm_ckout1 = 0x0015;
1935
    s->clkm.dpll1_mode = 0x2002;
1936
    omap_clkdsp_idlect1_update(s, s->clkm.dsp_idlect1 ^ 0x0040, 0x0040);
1937
    s->clkm.dsp_idlect1 = 0x0040;
1938
    omap_clkdsp_idlect2_update(s, ~0, 0x0000);
1939
    s->clkm.dsp_idlect2 = 0x0000;
1940
    s->clkm.dsp_rstct2 = 0x0000;
1941
}
1942

    
1943
static void omap_clkm_init(target_phys_addr_t mpu_base,
1944
                target_phys_addr_t dsp_base, struct omap_mpu_state_s *s)
1945
{
1946
    int iomemtype[2] = {
1947
        cpu_register_io_memory(omap_clkm_readfn, omap_clkm_writefn, s),
1948
        cpu_register_io_memory(omap_clkdsp_readfn, omap_clkdsp_writefn, s),
1949
    };
1950

    
1951
    s->clkm.arm_idlect1 = 0x03ff;
1952
    s->clkm.arm_idlect2 = 0x0100;
1953
    s->clkm.dsp_idlect1 = 0x0002;
1954
    omap_clkm_reset(s);
1955
    s->clkm.cold_start = 0x3a;
1956

    
1957
    cpu_register_physical_memory(mpu_base, 0x100, iomemtype[0]);
1958
    cpu_register_physical_memory(dsp_base, 0x1000, iomemtype[1]);
1959
}
1960

    
1961
/* MPU I/O */
1962
struct omap_mpuio_s {
1963
    qemu_irq irq;
1964
    qemu_irq kbd_irq;
1965
    qemu_irq *in;
1966
    qemu_irq handler[16];
1967
    qemu_irq wakeup;
1968

    
1969
    uint16_t inputs;
1970
    uint16_t outputs;
1971
    uint16_t dir;
1972
    uint16_t edge;
1973
    uint16_t mask;
1974
    uint16_t ints;
1975

    
1976
    uint16_t debounce;
1977
    uint16_t latch;
1978
    uint8_t event;
1979

    
1980
    uint8_t buttons[5];
1981
    uint8_t row_latch;
1982
    uint8_t cols;
1983
    int kbd_mask;
1984
    int clk;
1985
};
1986

    
1987
static void omap_mpuio_set(void *opaque, int line, int level)
1988
{
1989
    struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
1990
    uint16_t prev = s->inputs;
1991

    
1992
    if (level)
1993
        s->inputs |= 1 << line;
1994
    else
1995
        s->inputs &= ~(1 << line);
1996

    
1997
    if (((1 << line) & s->dir & ~s->mask) && s->clk) {
1998
        if ((s->edge & s->inputs & ~prev) | (~s->edge & ~s->inputs & prev)) {
1999
            s->ints |= 1 << line;
2000
            qemu_irq_raise(s->irq);
2001
            /* TODO: wakeup */
2002
        }
2003
        if ((s->event & (1 << 0)) &&                /* SET_GPIO_EVENT_MODE */
2004
                (s->event >> 1) == line)        /* PIN_SELECT */
2005
            s->latch = s->inputs;
2006
    }
2007
}
2008

    
2009
static void omap_mpuio_kbd_update(struct omap_mpuio_s *s)
2010
{
2011
    int i;
2012
    uint8_t *row, rows = 0, cols = ~s->cols;
2013

    
2014
    for (row = s->buttons + 4, i = 1 << 4; i; row --, i >>= 1)
2015
        if (*row & cols)
2016
            rows |= i;
2017

    
2018
    qemu_set_irq(s->kbd_irq, rows && !s->kbd_mask && s->clk);
2019
    s->row_latch = ~rows;
2020
}
2021

    
2022
static uint32_t omap_mpuio_read(void *opaque, target_phys_addr_t addr)
2023
{
2024
    struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2025
    int offset = addr & OMAP_MPUI_REG_MASK;
2026
    uint16_t ret;
2027

    
2028
    switch (offset) {
2029
    case 0x00:        /* INPUT_LATCH */
2030
        return s->inputs;
2031

    
2032
    case 0x04:        /* OUTPUT_REG */
2033
        return s->outputs;
2034

    
2035
    case 0x08:        /* IO_CNTL */
2036
        return s->dir;
2037

    
2038
    case 0x10:        /* KBR_LATCH */
2039
        return s->row_latch;
2040

    
2041
    case 0x14:        /* KBC_REG */
2042
        return s->cols;
2043

    
2044
    case 0x18:        /* GPIO_EVENT_MODE_REG */
2045
        return s->event;
2046

    
2047
    case 0x1c:        /* GPIO_INT_EDGE_REG */
2048
        return s->edge;
2049

    
2050
    case 0x20:        /* KBD_INT */
2051
        return (~s->row_latch & 0x1f) && !s->kbd_mask;
2052

    
2053
    case 0x24:        /* GPIO_INT */
2054
        ret = s->ints;
2055
        s->ints &= s->mask;
2056
        if (ret)
2057
            qemu_irq_lower(s->irq);
2058
        return ret;
2059

    
2060
    case 0x28:        /* KBD_MASKIT */
2061
        return s->kbd_mask;
2062

    
2063
    case 0x2c:        /* GPIO_MASKIT */
2064
        return s->mask;
2065

    
2066
    case 0x30:        /* GPIO_DEBOUNCING_REG */
2067
        return s->debounce;
2068

    
2069
    case 0x34:        /* GPIO_LATCH_REG */
2070
        return s->latch;
2071
    }
2072

    
2073
    OMAP_BAD_REG(addr);
2074
    return 0;
2075
}
2076

    
2077
static void omap_mpuio_write(void *opaque, target_phys_addr_t addr,
2078
                uint32_t value)
2079
{
2080
    struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2081
    int offset = addr & OMAP_MPUI_REG_MASK;
2082
    uint16_t diff;
2083
    int ln;
2084

    
2085
    switch (offset) {
2086
    case 0x04:        /* OUTPUT_REG */
2087
        diff = (s->outputs ^ value) & ~s->dir;
2088
        s->outputs = value;
2089
        while ((ln = ffs(diff))) {
2090
            ln --;
2091
            if (s->handler[ln])
2092
                qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2093
            diff &= ~(1 << ln);
2094
        }
2095
        break;
2096

    
2097
    case 0x08:        /* IO_CNTL */
2098
        diff = s->outputs & (s->dir ^ value);
2099
        s->dir = value;
2100

    
2101
        value = s->outputs & ~s->dir;
2102
        while ((ln = ffs(diff))) {
2103
            ln --;
2104
            if (s->handler[ln])
2105
                qemu_set_irq(s->handler[ln], (value >> ln) & 1);
2106
            diff &= ~(1 << ln);
2107
        }
2108
        break;
2109

    
2110
    case 0x14:        /* KBC_REG */
2111
        s->cols = value;
2112
        omap_mpuio_kbd_update(s);
2113
        break;
2114

    
2115
    case 0x18:        /* GPIO_EVENT_MODE_REG */
2116
        s->event = value & 0x1f;
2117
        break;
2118

    
2119
    case 0x1c:        /* GPIO_INT_EDGE_REG */
2120
        s->edge = value;
2121
        break;
2122

    
2123
    case 0x28:        /* KBD_MASKIT */
2124
        s->kbd_mask = value & 1;
2125
        omap_mpuio_kbd_update(s);
2126
        break;
2127

    
2128
    case 0x2c:        /* GPIO_MASKIT */
2129
        s->mask = value;
2130
        break;
2131

    
2132
    case 0x30:        /* GPIO_DEBOUNCING_REG */
2133
        s->debounce = value & 0x1ff;
2134
        break;
2135

    
2136
    case 0x00:        /* INPUT_LATCH */
2137
    case 0x10:        /* KBR_LATCH */
2138
    case 0x20:        /* KBD_INT */
2139
    case 0x24:        /* GPIO_INT */
2140
    case 0x34:        /* GPIO_LATCH_REG */
2141
        OMAP_RO_REG(addr);
2142
        return;
2143

    
2144
    default:
2145
        OMAP_BAD_REG(addr);
2146
        return;
2147
    }
2148
}
2149

    
2150
static CPUReadMemoryFunc * const omap_mpuio_readfn[] = {
2151
    omap_badwidth_read16,
2152
    omap_mpuio_read,
2153
    omap_badwidth_read16,
2154
};
2155

    
2156
static CPUWriteMemoryFunc * const omap_mpuio_writefn[] = {
2157
    omap_badwidth_write16,
2158
    omap_mpuio_write,
2159
    omap_badwidth_write16,
2160
};
2161

    
2162
static void omap_mpuio_reset(struct omap_mpuio_s *s)
2163
{
2164
    s->inputs = 0;
2165
    s->outputs = 0;
2166
    s->dir = ~0;
2167
    s->event = 0;
2168
    s->edge = 0;
2169
    s->kbd_mask = 0;
2170
    s->mask = 0;
2171
    s->debounce = 0;
2172
    s->latch = 0;
2173
    s->ints = 0;
2174
    s->row_latch = 0x1f;
2175
    s->clk = 1;
2176
}
2177

    
2178
static void omap_mpuio_onoff(void *opaque, int line, int on)
2179
{
2180
    struct omap_mpuio_s *s = (struct omap_mpuio_s *) opaque;
2181

    
2182
    s->clk = on;
2183
    if (on)
2184
        omap_mpuio_kbd_update(s);
2185
}
2186

    
2187
struct omap_mpuio_s *omap_mpuio_init(target_phys_addr_t base,
2188
                qemu_irq kbd_int, qemu_irq gpio_int, qemu_irq wakeup,
2189
                omap_clk clk)
2190
{
2191
    int iomemtype;
2192
    struct omap_mpuio_s *s = (struct omap_mpuio_s *)
2193
            qemu_mallocz(sizeof(struct omap_mpuio_s));
2194

    
2195
    s->irq = gpio_int;
2196
    s->kbd_irq = kbd_int;
2197
    s->wakeup = wakeup;
2198
    s->in = qemu_allocate_irqs(omap_mpuio_set, s, 16);
2199
    omap_mpuio_reset(s);
2200

    
2201
    iomemtype = cpu_register_io_memory(omap_mpuio_readfn,
2202
                    omap_mpuio_writefn, s);
2203
    cpu_register_physical_memory(base, 0x800, iomemtype);
2204

    
2205
    omap_clk_adduser(clk, qemu_allocate_irqs(omap_mpuio_onoff, s, 1)[0]);
2206

    
2207
    return s;
2208
}
2209

    
2210
qemu_irq *omap_mpuio_in_get(struct omap_mpuio_s *s)
2211
{
2212
    return s->in;
2213
}
2214

    
2215
void omap_mpuio_out_set(struct omap_mpuio_s *s, int line, qemu_irq handler)
2216
{
2217
    if (line >= 16 || line < 0)
2218
        hw_error("%s: No GPIO line %i\n", __FUNCTION__, line);
2219
    s->handler[line] = handler;
2220
}
2221

    
2222
void omap_mpuio_key(struct omap_mpuio_s *s, int row, int col, int down)
2223
{
2224
    if (row >= 5 || row < 0)
2225
        hw_error("%s: No key %i-%i\n", __FUNCTION__, col, row);
2226

    
2227
    if (down)
2228
        s->buttons[row] |= 1 << col;
2229
    else
2230
        s->buttons[row] &= ~(1 << col);
2231

    
2232
    omap_mpuio_kbd_update(s);
2233
}
2234

    
2235
/* MicroWire Interface */
2236
struct omap_uwire_s {
2237
    qemu_irq txirq;
2238
    qemu_irq rxirq;
2239
    qemu_irq txdrq;
2240

    
2241
    uint16_t txbuf;
2242
    uint16_t rxbuf;
2243
    uint16_t control;
2244
    uint16_t setup[5];
2245

    
2246
    uWireSlave *chip[4];
2247
};
2248

    
2249
static void omap_uwire_transfer_start(struct omap_uwire_s *s)
2250
{
2251
    int chipselect = (s->control >> 10) & 3;                /* INDEX */
2252
    uWireSlave *slave = s->chip[chipselect];
2253

    
2254
    if ((s->control >> 5) & 0x1f) {                        /* NB_BITS_WR */
2255
        if (s->control & (1 << 12))                        /* CS_CMD */
2256
            if (slave && slave->send)
2257
                slave->send(slave->opaque,
2258
                                s->txbuf >> (16 - ((s->control >> 5) & 0x1f)));
2259
        s->control &= ~(1 << 14);                        /* CSRB */
2260
        /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
2261
         * a DRQ.  When is the level IRQ supposed to be reset?  */
2262
    }
2263

    
2264
    if ((s->control >> 0) & 0x1f) {                        /* NB_BITS_RD */
2265
        if (s->control & (1 << 12))                        /* CS_CMD */
2266
            if (slave && slave->receive)
2267
                s->rxbuf = slave->receive(slave->opaque);
2268
        s->control |= 1 << 15;                                /* RDRB */
2269
        /* TODO: depending on s->setup[4] bits [1:0] assert an IRQ or
2270
         * a DRQ.  When is the level IRQ supposed to be reset?  */
2271
    }
2272
}
2273

    
2274
static uint32_t omap_uwire_read(void *opaque, target_phys_addr_t addr)
2275
{
2276
    struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
2277
    int offset = addr & OMAP_MPUI_REG_MASK;
2278

    
2279
    switch (offset) {
2280
    case 0x00:        /* RDR */
2281
        s->control &= ~(1 << 15);                        /* RDRB */
2282
        return s->rxbuf;
2283

    
2284
    case 0x04:        /* CSR */
2285
        return s->control;
2286

    
2287
    case 0x08:        /* SR1 */
2288
        return s->setup[0];
2289
    case 0x0c:        /* SR2 */
2290
        return s->setup[1];
2291
    case 0x10:        /* SR3 */
2292
        return s->setup[2];
2293
    case 0x14:        /* SR4 */
2294
        return s->setup[3];
2295
    case 0x18:        /* SR5 */
2296
        return s->setup[4];
2297
    }
2298

    
2299
    OMAP_BAD_REG(addr);
2300
    return 0;
2301
}
2302

    
2303
static void omap_uwire_write(void *opaque, target_phys_addr_t addr,
2304
                uint32_t value)
2305
{
2306
    struct omap_uwire_s *s = (struct omap_uwire_s *) opaque;
2307
    int offset = addr & OMAP_MPUI_REG_MASK;
2308

    
2309
    switch (offset) {
2310
    case 0x00:        /* TDR */
2311
        s->txbuf = value;                                /* TD */
2312
        if ((s->setup[4] & (1 << 2)) &&                        /* AUTO_TX_EN */
2313
                        ((s->setup[4] & (1 << 3)) ||        /* CS_TOGGLE_TX_EN */
2314
                         (s->control & (1 << 12)))) {        /* CS_CMD */
2315
            s->control |= 1 << 14;                        /* CSRB */
2316
            omap_uwire_transfer_start(s);
2317
        }
2318
        break;
2319

    
2320
    case 0x04:        /* CSR */
2321
        s->control = value & 0x1fff;
2322
        if (value & (1 << 13))                                /* START */
2323
            omap_uwire_transfer_start(s);
2324
        break;
2325

    
2326
    case 0x08:        /* SR1 */
2327
        s->setup[0] = value & 0x003f;
2328
        break;
2329

    
2330
    case 0x0c:        /* SR2 */
2331
        s->setup[1] = value & 0x0fc0;
2332
        break;
2333

    
2334
    case 0x10:        /* SR3 */
2335
        s->setup[2] = value & 0x0003;
2336
        break;
2337

    
2338
    case 0x14:        /* SR4 */
2339
        s->setup[3] = value & 0x0001;
2340
        break;
2341

    
2342
    case 0x18:        /* SR5 */
2343
        s->setup[4] = value & 0x000f;
2344
        break;
2345

    
2346
    default:
2347
        OMAP_BAD_REG(addr);
2348
        return;
2349
    }
2350
}
2351

    
2352
static CPUReadMemoryFunc * const omap_uwire_readfn[] = {
2353
    omap_badwidth_read16,
2354
    omap_uwire_read,
2355
    omap_badwidth_read16,
2356
};
2357

    
2358
static CPUWriteMemoryFunc * const omap_uwire_writefn[] = {
2359
    omap_badwidth_write16,
2360
    omap_uwire_write,
2361
    omap_badwidth_write16,
2362
};
2363

    
2364
static void omap_uwire_reset(struct omap_uwire_s *s)
2365
{
2366
    s->control = 0;
2367
    s->setup[0] = 0;
2368
    s->setup[1] = 0;
2369
    s->setup[2] = 0;
2370
    s->setup[3] = 0;
2371
    s->setup[4] = 0;
2372
}
2373

    
2374
struct omap_uwire_s *omap_uwire_init(target_phys_addr_t base,
2375
                qemu_irq *irq, qemu_irq dma, omap_clk clk)
2376
{
2377
    int iomemtype;
2378
    struct omap_uwire_s *s = (struct omap_uwire_s *)
2379
            qemu_mallocz(sizeof(struct omap_uwire_s));
2380

    
2381
    s->txirq = irq[0];
2382
    s->rxirq = irq[1];
2383
    s->txdrq = dma;
2384
    omap_uwire_reset(s);
2385

    
2386
    iomemtype = cpu_register_io_memory(omap_uwire_readfn,
2387
                    omap_uwire_writefn, s);
2388
    cpu_register_physical_memory(base, 0x800, iomemtype);
2389

    
2390
    return s;
2391
}
2392

    
2393
void omap_uwire_attach(struct omap_uwire_s *s,
2394
                uWireSlave *slave, int chipselect)
2395
{
2396
    if (chipselect < 0 || chipselect > 3) {
2397
        fprintf(stderr, "%s: Bad chipselect %i\n", __FUNCTION__, chipselect);
2398
        exit(-1);
2399
    }
2400

    
2401
    s->chip[chipselect] = slave;
2402
}
2403

    
2404
/* Pseudonoise Pulse-Width Light Modulator */
2405
static void omap_pwl_update(struct omap_mpu_state_s *s)
2406
{
2407
    int output = (s->pwl.clk && s->pwl.enable) ? s->pwl.level : 0;
2408

    
2409
    if (output != s->pwl.output) {
2410
        s->pwl.output = output;
2411
        printf("%s: Backlight now at %i/256\n", __FUNCTION__, output);
2412
    }
2413
}
2414

    
2415
static uint32_t omap_pwl_read(void *opaque, target_phys_addr_t addr)
2416
{
2417
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2418
    int offset = addr & OMAP_MPUI_REG_MASK;
2419

    
2420
    switch (offset) {
2421
    case 0x00:        /* PWL_LEVEL */
2422
        return s->pwl.level;
2423
    case 0x04:        /* PWL_CTRL */
2424
        return s->pwl.enable;
2425
    }
2426
    OMAP_BAD_REG(addr);
2427
    return 0;
2428
}
2429

    
2430
static void omap_pwl_write(void *opaque, target_phys_addr_t addr,
2431
                uint32_t value)
2432
{
2433
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2434
    int offset = addr & OMAP_MPUI_REG_MASK;
2435

    
2436
    switch (offset) {
2437
    case 0x00:        /* PWL_LEVEL */
2438
        s->pwl.level = value;
2439
        omap_pwl_update(s);
2440
        break;
2441
    case 0x04:        /* PWL_CTRL */
2442
        s->pwl.enable = value & 1;
2443
        omap_pwl_update(s);
2444
        break;
2445
    default:
2446
        OMAP_BAD_REG(addr);
2447
        return;
2448
    }
2449
}
2450

    
2451
static CPUReadMemoryFunc * const omap_pwl_readfn[] = {
2452
    omap_pwl_read,
2453
    omap_badwidth_read8,
2454
    omap_badwidth_read8,
2455
};
2456

    
2457
static CPUWriteMemoryFunc * const omap_pwl_writefn[] = {
2458
    omap_pwl_write,
2459
    omap_badwidth_write8,
2460
    omap_badwidth_write8,
2461
};
2462

    
2463
static void omap_pwl_reset(struct omap_mpu_state_s *s)
2464
{
2465
    s->pwl.output = 0;
2466
    s->pwl.level = 0;
2467
    s->pwl.enable = 0;
2468
    s->pwl.clk = 1;
2469
    omap_pwl_update(s);
2470
}
2471

    
2472
static void omap_pwl_clk_update(void *opaque, int line, int on)
2473
{
2474
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2475

    
2476
    s->pwl.clk = on;
2477
    omap_pwl_update(s);
2478
}
2479

    
2480
static void omap_pwl_init(target_phys_addr_t base, struct omap_mpu_state_s *s,
2481
                omap_clk clk)
2482
{
2483
    int iomemtype;
2484

    
2485
    omap_pwl_reset(s);
2486

    
2487
    iomemtype = cpu_register_io_memory(omap_pwl_readfn,
2488
                    omap_pwl_writefn, s);
2489
    cpu_register_physical_memory(base, 0x800, iomemtype);
2490

    
2491
    omap_clk_adduser(clk, qemu_allocate_irqs(omap_pwl_clk_update, s, 1)[0]);
2492
}
2493

    
2494
/* Pulse-Width Tone module */
2495
static uint32_t omap_pwt_read(void *opaque, target_phys_addr_t addr)
2496
{
2497
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2498
    int offset = addr & OMAP_MPUI_REG_MASK;
2499

    
2500
    switch (offset) {
2501
    case 0x00:        /* FRC */
2502
        return s->pwt.frc;
2503
    case 0x04:        /* VCR */
2504
        return s->pwt.vrc;
2505
    case 0x08:        /* GCR */
2506
        return s->pwt.gcr;
2507
    }
2508
    OMAP_BAD_REG(addr);
2509
    return 0;
2510
}
2511

    
2512
static void omap_pwt_write(void *opaque, target_phys_addr_t addr,
2513
                uint32_t value)
2514
{
2515
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *) opaque;
2516
    int offset = addr & OMAP_MPUI_REG_MASK;
2517

    
2518
    switch (offset) {
2519
    case 0x00:        /* FRC */
2520
        s->pwt.frc = value & 0x3f;
2521
        break;
2522
    case 0x04:        /* VRC */
2523
        if ((value ^ s->pwt.vrc) & 1) {
2524
            if (value & 1)
2525
                printf("%s: %iHz buzz on\n", __FUNCTION__, (int)
2526
                                /* 1.5 MHz from a 12-MHz or 13-MHz PWT_CLK */
2527
                                ((omap_clk_getrate(s->pwt.clk) >> 3) /
2528
                                 /* Pre-multiplexer divider */
2529
                                 ((s->pwt.gcr & 2) ? 1 : 154) /
2530
                                 /* Octave multiplexer */
2531
                                 (2 << (value & 3)) *
2532
                                 /* 101/107 divider */
2533
                                 ((value & (1 << 2)) ? 101 : 107) *
2534
                                 /*  49/55 divider */
2535
                                 ((value & (1 << 3)) ?  49 : 55) *
2536
                                 /*  50/63 divider */
2537
                                 ((value & (1 << 4)) ?  50 : 63) *
2538
                                 /*  80/127 divider */
2539
                                 ((value & (1 << 5)) ?  80 : 127) /
2540
                                 (107 * 55 * 63 * 127)));
2541
            else
2542
                printf("%s: silence!\n", __FUNCTION__);
2543
        }
2544
        s->pwt.vrc = value & 0x7f;
2545
        break;
2546
    case 0x08:        /* GCR */
2547
        s->pwt.gcr = value & 3;
2548
        break;
2549
    default:
2550
        OMAP_BAD_REG(addr);
2551
        return;
2552
    }
2553
}
2554

    
2555
static CPUReadMemoryFunc * const omap_pwt_readfn[] = {
2556
    omap_pwt_read,
2557
    omap_badwidth_read8,
2558
    omap_badwidth_read8,
2559
};
2560

    
2561
static CPUWriteMemoryFunc * const omap_pwt_writefn[] = {
2562
    omap_pwt_write,
2563
    omap_badwidth_write8,
2564
    omap_badwidth_write8,
2565
};
2566

    
2567
static void omap_pwt_reset(struct omap_mpu_state_s *s)
2568
{
2569
    s->pwt.frc = 0;
2570
    s->pwt.vrc = 0;
2571
    s->pwt.gcr = 0;
2572
}
2573

    
2574
static void omap_pwt_init(target_phys_addr_t base, struct omap_mpu_state_s *s,
2575
                omap_clk clk)
2576
{
2577
    int iomemtype;
2578

    
2579
    s->pwt.clk = clk;
2580
    omap_pwt_reset(s);
2581

    
2582
    iomemtype = cpu_register_io_memory(omap_pwt_readfn,
2583
                    omap_pwt_writefn, s);
2584
    cpu_register_physical_memory(base, 0x800, iomemtype);
2585
}
2586

    
2587
/* Real-time Clock module */
2588
struct omap_rtc_s {
2589
    qemu_irq irq;
2590
    qemu_irq alarm;
2591
    QEMUTimer *clk;
2592

    
2593
    uint8_t interrupts;
2594
    uint8_t status;
2595
    int16_t comp_reg;
2596
    int running;
2597
    int pm_am;
2598
    int auto_comp;
2599
    int round;
2600
    struct tm alarm_tm;
2601
    time_t alarm_ti;
2602

    
2603
    struct tm current_tm;
2604
    time_t ti;
2605
    uint64_t tick;
2606
};
2607

    
2608
static void omap_rtc_interrupts_update(struct omap_rtc_s *s)
2609
{
2610
    /* s->alarm is level-triggered */
2611
    qemu_set_irq(s->alarm, (s->status >> 6) & 1);
2612
}
2613

    
2614
static void omap_rtc_alarm_update(struct omap_rtc_s *s)
2615
{
2616
    s->alarm_ti = mktimegm(&s->alarm_tm);
2617
    if (s->alarm_ti == -1)
2618
        printf("%s: conversion failed\n", __FUNCTION__);
2619
}
2620

    
2621
static uint32_t omap_rtc_read(void *opaque, target_phys_addr_t addr)
2622
{
2623
    struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
2624
    int offset = addr & OMAP_MPUI_REG_MASK;
2625
    uint8_t i;
2626

    
2627
    switch (offset) {
2628
    case 0x00:        /* SECONDS_REG */
2629
        return to_bcd(s->current_tm.tm_sec);
2630

    
2631
    case 0x04:        /* MINUTES_REG */
2632
        return to_bcd(s->current_tm.tm_min);
2633

    
2634
    case 0x08:        /* HOURS_REG */
2635
        if (s->pm_am)
2636
            return ((s->current_tm.tm_hour > 11) << 7) |
2637
                    to_bcd(((s->current_tm.tm_hour - 1) % 12) + 1);
2638
        else
2639
            return to_bcd(s->current_tm.tm_hour);
2640

    
2641
    case 0x0c:        /* DAYS_REG */
2642
        return to_bcd(s->current_tm.tm_mday);
2643

    
2644
    case 0x10:        /* MONTHS_REG */
2645
        return to_bcd(s->current_tm.tm_mon + 1);
2646

    
2647
    case 0x14:        /* YEARS_REG */
2648
        return to_bcd(s->current_tm.tm_year % 100);
2649

    
2650
    case 0x18:        /* WEEK_REG */
2651
        return s->current_tm.tm_wday;
2652

    
2653
    case 0x20:        /* ALARM_SECONDS_REG */
2654
        return to_bcd(s->alarm_tm.tm_sec);
2655

    
2656
    case 0x24:        /* ALARM_MINUTES_REG */
2657
        return to_bcd(s->alarm_tm.tm_min);
2658

    
2659
    case 0x28:        /* ALARM_HOURS_REG */
2660
        if (s->pm_am)
2661
            return ((s->alarm_tm.tm_hour > 11) << 7) |
2662
                    to_bcd(((s->alarm_tm.tm_hour - 1) % 12) + 1);
2663
        else
2664
            return to_bcd(s->alarm_tm.tm_hour);
2665

    
2666
    case 0x2c:        /* ALARM_DAYS_REG */
2667
        return to_bcd(s->alarm_tm.tm_mday);
2668

    
2669
    case 0x30:        /* ALARM_MONTHS_REG */
2670
        return to_bcd(s->alarm_tm.tm_mon + 1);
2671

    
2672
    case 0x34:        /* ALARM_YEARS_REG */
2673
        return to_bcd(s->alarm_tm.tm_year % 100);
2674

    
2675
    case 0x40:        /* RTC_CTRL_REG */
2676
        return (s->pm_am << 3) | (s->auto_comp << 2) |
2677
                (s->round << 1) | s->running;
2678

    
2679
    case 0x44:        /* RTC_STATUS_REG */
2680
        i = s->status;
2681
        s->status &= ~0x3d;
2682
        return i;
2683

    
2684
    case 0x48:        /* RTC_INTERRUPTS_REG */
2685
        return s->interrupts;
2686

    
2687
    case 0x4c:        /* RTC_COMP_LSB_REG */
2688
        return ((uint16_t) s->comp_reg) & 0xff;
2689

    
2690
    case 0x50:        /* RTC_COMP_MSB_REG */
2691
        return ((uint16_t) s->comp_reg) >> 8;
2692
    }
2693

    
2694
    OMAP_BAD_REG(addr);
2695
    return 0;
2696
}
2697

    
2698
static void omap_rtc_write(void *opaque, target_phys_addr_t addr,
2699
                uint32_t value)
2700
{
2701
    struct omap_rtc_s *s = (struct omap_rtc_s *) opaque;
2702
    int offset = addr & OMAP_MPUI_REG_MASK;
2703
    struct tm new_tm;
2704
    time_t ti[2];
2705

    
2706
    switch (offset) {
2707
    case 0x00:        /* SECONDS_REG */
2708
#ifdef ALMDEBUG
2709
        printf("RTC SEC_REG <-- %02x\n", value);
2710
#endif
2711
        s->ti -= s->current_tm.tm_sec;
2712
        s->ti += from_bcd(value);
2713
        return;
2714

    
2715
    case 0x04:        /* MINUTES_REG */
2716
#ifdef ALMDEBUG
2717
        printf("RTC MIN_REG <-- %02x\n", value);
2718
#endif
2719
        s->ti -= s->current_tm.tm_min * 60;
2720
        s->ti += from_bcd(value) * 60;
2721
        return;
2722

    
2723
    case 0x08:        /* HOURS_REG */
2724
#ifdef ALMDEBUG
2725
        printf("RTC HRS_REG <-- %02x\n", value);
2726
#endif
2727
        s->ti -= s->current_tm.tm_hour * 3600;
2728
        if (s->pm_am) {
2729
            s->ti += (from_bcd(value & 0x3f) & 12) * 3600;
2730
            s->ti += ((value >> 7) & 1) * 43200;
2731
        } else
2732
            s->ti += from_bcd(value & 0x3f) * 3600;
2733
        return;
2734

    
2735
    case 0x0c:        /* DAYS_REG */
2736
#ifdef ALMDEBUG
2737
        printf("RTC DAY_REG <-- %02x\n", value);
2738
#endif
2739
        s->ti -= s->current_tm.tm_mday * 86400;
2740
        s->ti += from_bcd(value) * 86400;
2741
        return;
2742

    
2743
    case 0x10:        /* MONTHS_REG */
2744
#ifdef ALMDEBUG
2745
        printf("RTC MTH_REG <-- %02x\n", value);
2746
#endif
2747
        memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
2748
        new_tm.tm_mon = from_bcd(value);
2749
        ti[0] = mktimegm(&s->current_tm);
2750
        ti[1] = mktimegm(&new_tm);
2751

    
2752
        if (ti[0] != -1 && ti[1] != -1) {
2753
            s->ti -= ti[0];
2754
            s->ti += ti[1];
2755
        } else {
2756
            /* A less accurate version */
2757
            s->ti -= s->current_tm.tm_mon * 2592000;
2758
            s->ti += from_bcd(value) * 2592000;
2759
        }
2760
        return;
2761

    
2762
    case 0x14:        /* YEARS_REG */
2763
#ifdef ALMDEBUG
2764
        printf("RTC YRS_REG <-- %02x\n", value);
2765
#endif
2766
        memcpy(&new_tm, &s->current_tm, sizeof(new_tm));
2767
        new_tm.tm_year += from_bcd(value) - (new_tm.tm_year % 100);
2768
        ti[0] = mktimegm(&s->current_tm);
2769
        ti[1] = mktimegm(&new_tm);
2770

    
2771
        if (ti[0] != -1 && ti[1] != -1) {
2772
            s->ti -= ti[0];
2773
            s->ti += ti[1];
2774
        } else {
2775
            /* A less accurate version */
2776
            s->ti -= (s->current_tm.tm_year % 100) * 31536000;
2777
            s->ti += from_bcd(value) * 31536000;
2778
        }
2779
        return;
2780

    
2781
    case 0x18:        /* WEEK_REG */
2782
        return;        /* Ignored */
2783

    
2784
    case 0x20:        /* ALARM_SECONDS_REG */
2785
#ifdef ALMDEBUG
2786
        printf("ALM SEC_REG <-- %02x\n", value);
2787
#endif
2788
        s->alarm_tm.tm_sec = from_bcd(value);
2789
        omap_rtc_alarm_update(s);
2790
        return;
2791

    
2792
    case 0x24:        /* ALARM_MINUTES_REG */
2793
#ifdef ALMDEBUG
2794
        printf("ALM MIN_REG <-- %02x\n", value);
2795
#endif
2796
        s->alarm_tm.tm_min = from_bcd(value);
2797
        omap_rtc_alarm_update(s);
2798
        return;
2799

    
2800
    case 0x28:        /* ALARM_HOURS_REG */
2801
#ifdef ALMDEBUG
2802
        printf("ALM HRS_REG <-- %02x\n", value);
2803
#endif
2804
        if (s->pm_am)
2805
            s->alarm_tm.tm_hour =
2806
                    ((from_bcd(value & 0x3f)) % 12) +
2807
                    ((value >> 7) & 1) * 12;
2808
        else
2809
            s->alarm_tm.tm_hour = from_bcd(value);
2810
        omap_rtc_alarm_update(s);
2811
        return;
2812

    
2813
    case 0x2c:        /* ALARM_DAYS_REG */
2814
#ifdef ALMDEBUG
2815
        printf("ALM DAY_REG <-- %02x\n", value);
2816
#endif
2817
        s->alarm_tm.tm_mday = from_bcd(value);
2818
        omap_rtc_alarm_update(s);
2819
        return;
2820

    
2821
    case 0x30:        /* ALARM_MONTHS_REG */
2822
#ifdef ALMDEBUG
2823
        printf("ALM MON_REG <-- %02x\n", value);
2824
#endif
2825
        s->alarm_tm.tm_mon = from_bcd(value);
2826
        omap_rtc_alarm_update(s);
2827
        return;
2828

    
2829
    case 0x34:        /* ALARM_YEARS_REG */
2830
#ifdef ALMDEBUG
2831
        printf("ALM YRS_REG <-- %02x\n", value);
2832
#endif
2833
        s->alarm_tm.tm_year = from_bcd(value);
2834
        omap_rtc_alarm_update(s);
2835
        return;
2836

    
2837
    case 0x40:        /* RTC_CTRL_REG */
2838
#ifdef ALMDEBUG
2839
        printf("RTC CONTROL <-- %02x\n", value);
2840
#endif
2841
        s->pm_am = (value >> 3) & 1;
2842
        s->auto_comp = (value >> 2) & 1;
2843
        s->round = (value >> 1) & 1;
2844
        s->running = value & 1;
2845
        s->status &= 0xfd;
2846
        s->status |= s->running << 1;
2847
        return;
2848

    
2849
    case 0x44:        /* RTC_STATUS_REG */
2850
#ifdef ALMDEBUG
2851
        printf("RTC STATUSL <-- %02x\n", value);
2852
#endif
2853
        s->status &= ~((value & 0xc0) ^ 0x80);
2854
        omap_rtc_interrupts_update(s);
2855
        return;
2856

    
2857
    case 0x48:        /* RTC_INTERRUPTS_REG */
2858
#ifdef ALMDEBUG
2859
        printf("RTC INTRS <-- %02x\n", value);
2860
#endif
2861
        s->interrupts = value;
2862
        return;
2863

    
2864
    case 0x4c:        /* RTC_COMP_LSB_REG */
2865
#ifdef ALMDEBUG
2866
        printf("RTC COMPLSB <-- %02x\n", value);
2867
#endif
2868
        s->comp_reg &= 0xff00;
2869
        s->comp_reg |= 0x00ff & value;
2870
        return;
2871

    
2872
    case 0x50:        /* RTC_COMP_MSB_REG */
2873
#ifdef ALMDEBUG
2874
        printf("RTC COMPMSB <-- %02x\n", value);
2875
#endif
2876
        s->comp_reg &= 0x00ff;
2877
        s->comp_reg |= 0xff00 & (value << 8);
2878
        return;
2879

    
2880
    default:
2881
        OMAP_BAD_REG(addr);
2882
        return;
2883
    }
2884
}
2885

    
2886
static CPUReadMemoryFunc * const omap_rtc_readfn[] = {
2887
    omap_rtc_read,
2888
    omap_badwidth_read8,
2889
    omap_badwidth_read8,
2890
};
2891

    
2892
static CPUWriteMemoryFunc * const omap_rtc_writefn[] = {
2893
    omap_rtc_write,
2894
    omap_badwidth_write8,
2895
    omap_badwidth_write8,
2896
};
2897

    
2898
static void omap_rtc_tick(void *opaque)
2899
{
2900
    struct omap_rtc_s *s = opaque;
2901

    
2902
    if (s->round) {
2903
        /* Round to nearest full minute.  */
2904
        if (s->current_tm.tm_sec < 30)
2905
            s->ti -= s->current_tm.tm_sec;
2906
        else
2907
            s->ti += 60 - s->current_tm.tm_sec;
2908

    
2909
        s->round = 0;
2910
    }
2911

    
2912
    memcpy(&s->current_tm, localtime(&s->ti), sizeof(s->current_tm));
2913

    
2914
    if ((s->interrupts & 0x08) && s->ti == s->alarm_ti) {
2915
        s->status |= 0x40;
2916
        omap_rtc_interrupts_update(s);
2917
    }
2918

    
2919
    if (s->interrupts & 0x04)
2920
        switch (s->interrupts & 3) {
2921
        case 0:
2922
            s->status |= 0x04;
2923
            qemu_irq_pulse(s->irq);
2924
            break;
2925
        case 1:
2926
            if (s->current_tm.tm_sec)
2927
                break;
2928
            s->status |= 0x08;
2929
            qemu_irq_pulse(s->irq);
2930
            break;
2931
        case 2:
2932
            if (s->current_tm.tm_sec || s->current_tm.tm_min)
2933
                break;
2934
            s->status |= 0x10;
2935
            qemu_irq_pulse(s->irq);
2936
            break;
2937
        case 3:
2938
            if (s->current_tm.tm_sec ||
2939
                            s->current_tm.tm_min || s->current_tm.tm_hour)
2940
                break;
2941
            s->status |= 0x20;
2942
            qemu_irq_pulse(s->irq);
2943
            break;
2944
        }
2945

    
2946
    /* Move on */
2947
    if (s->running)
2948
        s->ti ++;
2949
    s->tick += 1000;
2950

    
2951
    /*
2952
     * Every full hour add a rough approximation of the compensation
2953
     * register to the 32kHz Timer (which drives the RTC) value. 
2954
     */
2955
    if (s->auto_comp && !s->current_tm.tm_sec && !s->current_tm.tm_min)
2956
        s->tick += s->comp_reg * 1000 / 32768;
2957

    
2958
    qemu_mod_timer(s->clk, s->tick);
2959
}
2960

    
2961
static void omap_rtc_reset(struct omap_rtc_s *s)
2962
{
2963
    struct tm tm;
2964

    
2965
    s->interrupts = 0;
2966
    s->comp_reg = 0;
2967
    s->running = 0;
2968
    s->pm_am = 0;
2969
    s->auto_comp = 0;
2970
    s->round = 0;
2971
    s->tick = qemu_get_clock(rt_clock);
2972
    memset(&s->alarm_tm, 0, sizeof(s->alarm_tm));
2973
    s->alarm_tm.tm_mday = 0x01;
2974
    s->status = 1 << 7;
2975
    qemu_get_timedate(&tm, 0);
2976
    s->ti = mktimegm(&tm);
2977

    
2978
    omap_rtc_alarm_update(s);
2979
    omap_rtc_tick(s);
2980
}
2981

    
2982
struct omap_rtc_s *omap_rtc_init(target_phys_addr_t base,
2983
                qemu_irq *irq, omap_clk clk)
2984
{
2985
    int iomemtype;
2986
    struct omap_rtc_s *s = (struct omap_rtc_s *)
2987
            qemu_mallocz(sizeof(struct omap_rtc_s));
2988

    
2989
    s->irq = irq[0];
2990
    s->alarm = irq[1];
2991
    s->clk = qemu_new_timer(rt_clock, omap_rtc_tick, s);
2992

    
2993
    omap_rtc_reset(s);
2994

    
2995
    iomemtype = cpu_register_io_memory(omap_rtc_readfn,
2996
                    omap_rtc_writefn, s);
2997
    cpu_register_physical_memory(base, 0x800, iomemtype);
2998

    
2999
    return s;
3000
}
3001

    
3002
/* Multi-channel Buffered Serial Port interfaces */
3003
struct omap_mcbsp_s {
3004
    qemu_irq txirq;
3005
    qemu_irq rxirq;
3006
    qemu_irq txdrq;
3007
    qemu_irq rxdrq;
3008

    
3009
    uint16_t spcr[2];
3010
    uint16_t rcr[2];
3011
    uint16_t xcr[2];
3012
    uint16_t srgr[2];
3013
    uint16_t mcr[2];
3014
    uint16_t pcr;
3015
    uint16_t rcer[8];
3016
    uint16_t xcer[8];
3017
    int tx_rate;
3018
    int rx_rate;
3019
    int tx_req;
3020
    int rx_req;
3021

    
3022
    I2SCodec *codec;
3023
    QEMUTimer *source_timer;
3024
    QEMUTimer *sink_timer;
3025
};
3026

    
3027
static void omap_mcbsp_intr_update(struct omap_mcbsp_s *s)
3028
{
3029
    int irq;
3030

    
3031
    switch ((s->spcr[0] >> 4) & 3) {                        /* RINTM */
3032
    case 0:
3033
        irq = (s->spcr[0] >> 1) & 1;                        /* RRDY */
3034
        break;
3035
    case 3:
3036
        irq = (s->spcr[0] >> 3) & 1;                        /* RSYNCERR */
3037
        break;
3038
    default:
3039
        irq = 0;
3040
        break;
3041
    }
3042

    
3043
    if (irq)
3044
        qemu_irq_pulse(s->rxirq);
3045

    
3046
    switch ((s->spcr[1] >> 4) & 3) {                        /* XINTM */
3047
    case 0:
3048
        irq = (s->spcr[1] >> 1) & 1;                        /* XRDY */
3049
        break;
3050
    case 3:
3051
        irq = (s->spcr[1] >> 3) & 1;                        /* XSYNCERR */
3052
        break;
3053
    default:
3054
        irq = 0;
3055
        break;
3056
    }
3057

    
3058
    if (irq)
3059
        qemu_irq_pulse(s->txirq);
3060
}
3061

    
3062
static void omap_mcbsp_rx_newdata(struct omap_mcbsp_s *s)
3063
{
3064
    if ((s->spcr[0] >> 1) & 1)                                /* RRDY */
3065
        s->spcr[0] |= 1 << 2;                                /* RFULL */
3066
    s->spcr[0] |= 1 << 1;                                /* RRDY */
3067
    qemu_irq_raise(s->rxdrq);
3068
    omap_mcbsp_intr_update(s);
3069
}
3070

    
3071
static void omap_mcbsp_source_tick(void *opaque)
3072
{
3073
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3074
    static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3075

    
3076
    if (!s->rx_rate)
3077
        return;
3078
    if (s->rx_req)
3079
        printf("%s: Rx FIFO overrun\n", __FUNCTION__);
3080

    
3081
    s->rx_req = s->rx_rate << bps[(s->rcr[0] >> 5) & 7];
3082

    
3083
    omap_mcbsp_rx_newdata(s);
3084
    qemu_mod_timer(s->source_timer, qemu_get_clock(vm_clock) +
3085
                   get_ticks_per_sec());
3086
}
3087

    
3088
static void omap_mcbsp_rx_start(struct omap_mcbsp_s *s)
3089
{
3090
    if (!s->codec || !s->codec->rts)
3091
        omap_mcbsp_source_tick(s);
3092
    else if (s->codec->in.len) {
3093
        s->rx_req = s->codec->in.len;
3094
        omap_mcbsp_rx_newdata(s);
3095
    }
3096
}
3097

    
3098
static void omap_mcbsp_rx_stop(struct omap_mcbsp_s *s)
3099
{
3100
    qemu_del_timer(s->source_timer);
3101
}
3102

    
3103
static void omap_mcbsp_rx_done(struct omap_mcbsp_s *s)
3104
{
3105
    s->spcr[0] &= ~(1 << 1);                                /* RRDY */
3106
    qemu_irq_lower(s->rxdrq);
3107
    omap_mcbsp_intr_update(s);
3108
}
3109

    
3110
static void omap_mcbsp_tx_newdata(struct omap_mcbsp_s *s)
3111
{
3112
    s->spcr[1] |= 1 << 1;                                /* XRDY */
3113
    qemu_irq_raise(s->txdrq);
3114
    omap_mcbsp_intr_update(s);
3115
}
3116

    
3117
static void omap_mcbsp_sink_tick(void *opaque)
3118
{
3119
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3120
    static const int bps[8] = { 0, 1, 1, 2, 2, 2, -255, -255 };
3121

    
3122
    if (!s->tx_rate)
3123
        return;
3124
    if (s->tx_req)
3125
        printf("%s: Tx FIFO underrun\n", __FUNCTION__);
3126

    
3127
    s->tx_req = s->tx_rate << bps[(s->xcr[0] >> 5) & 7];
3128

    
3129
    omap_mcbsp_tx_newdata(s);
3130
    qemu_mod_timer(s->sink_timer, qemu_get_clock(vm_clock) +
3131
                   get_ticks_per_sec());
3132
}
3133

    
3134
static void omap_mcbsp_tx_start(struct omap_mcbsp_s *s)
3135
{
3136
    if (!s->codec || !s->codec->cts)
3137
        omap_mcbsp_sink_tick(s);
3138
    else if (s->codec->out.size) {
3139
        s->tx_req = s->codec->out.size;
3140
        omap_mcbsp_tx_newdata(s);
3141
    }
3142
}
3143

    
3144
static void omap_mcbsp_tx_done(struct omap_mcbsp_s *s)
3145
{
3146
    s->spcr[1] &= ~(1 << 1);                                /* XRDY */
3147
    qemu_irq_lower(s->txdrq);
3148
    omap_mcbsp_intr_update(s);
3149
    if (s->codec && s->codec->cts)
3150
        s->codec->tx_swallow(s->codec->opaque);
3151
}
3152

    
3153
static void omap_mcbsp_tx_stop(struct omap_mcbsp_s *s)
3154
{
3155
    s->tx_req = 0;
3156
    omap_mcbsp_tx_done(s);
3157
    qemu_del_timer(s->sink_timer);
3158
}
3159

    
3160
static void omap_mcbsp_req_update(struct omap_mcbsp_s *s)
3161
{
3162
    int prev_rx_rate, prev_tx_rate;
3163
    int rx_rate = 0, tx_rate = 0;
3164
    int cpu_rate = 1500000;        /* XXX */
3165

    
3166
    /* TODO: check CLKSTP bit */
3167
    if (s->spcr[1] & (1 << 6)) {                        /* GRST */
3168
        if (s->spcr[0] & (1 << 0)) {                        /* RRST */
3169
            if ((s->srgr[1] & (1 << 13)) &&                /* CLKSM */
3170
                            (s->pcr & (1 << 8))) {        /* CLKRM */
3171
                if (~s->pcr & (1 << 7))                        /* SCLKME */
3172
                    rx_rate = cpu_rate /
3173
                            ((s->srgr[0] & 0xff) + 1);        /* CLKGDV */
3174
            } else
3175
                if (s->codec)
3176
                    rx_rate = s->codec->rx_rate;
3177
        }
3178

    
3179
        if (s->spcr[1] & (1 << 0)) {                        /* XRST */
3180
            if ((s->srgr[1] & (1 << 13)) &&                /* CLKSM */
3181
                            (s->pcr & (1 << 9))) {        /* CLKXM */
3182
                if (~s->pcr & (1 << 7))                        /* SCLKME */
3183
                    tx_rate = cpu_rate /
3184
                            ((s->srgr[0] & 0xff) + 1);        /* CLKGDV */
3185
            } else
3186
                if (s->codec)
3187
                    tx_rate = s->codec->tx_rate;
3188
        }
3189
    }
3190
    prev_tx_rate = s->tx_rate;
3191
    prev_rx_rate = s->rx_rate;
3192
    s->tx_rate = tx_rate;
3193
    s->rx_rate = rx_rate;
3194

    
3195
    if (s->codec)
3196
        s->codec->set_rate(s->codec->opaque, rx_rate, tx_rate);
3197

    
3198
    if (!prev_tx_rate && tx_rate)
3199
        omap_mcbsp_tx_start(s);
3200
    else if (s->tx_rate && !tx_rate)
3201
        omap_mcbsp_tx_stop(s);
3202

    
3203
    if (!prev_rx_rate && rx_rate)
3204
        omap_mcbsp_rx_start(s);
3205
    else if (prev_tx_rate && !tx_rate)
3206
        omap_mcbsp_rx_stop(s);
3207
}
3208

    
3209
static uint32_t omap_mcbsp_read(void *opaque, target_phys_addr_t addr)
3210
{
3211
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3212
    int offset = addr & OMAP_MPUI_REG_MASK;
3213
    uint16_t ret;
3214

    
3215
    switch (offset) {
3216
    case 0x00:        /* DRR2 */
3217
        if (((s->rcr[0] >> 5) & 7) < 3)                        /* RWDLEN1 */
3218
            return 0x0000;
3219
        /* Fall through.  */
3220
    case 0x02:        /* DRR1 */
3221
        if (s->rx_req < 2) {
3222
            printf("%s: Rx FIFO underrun\n", __FUNCTION__);
3223
            omap_mcbsp_rx_done(s);
3224
        } else {
3225
            s->tx_req -= 2;
3226
            if (s->codec && s->codec->in.len >= 2) {
3227
                ret = s->codec->in.fifo[s->codec->in.start ++] << 8;
3228
                ret |= s->codec->in.fifo[s->codec->in.start ++];
3229
                s->codec->in.len -= 2;
3230
            } else
3231
                ret = 0x0000;
3232
            if (!s->tx_req)
3233
                omap_mcbsp_rx_done(s);
3234
            return ret;
3235
        }
3236
        return 0x0000;
3237

    
3238
    case 0x04:        /* DXR2 */
3239
    case 0x06:        /* DXR1 */
3240
        return 0x0000;
3241

    
3242
    case 0x08:        /* SPCR2 */
3243
        return s->spcr[1];
3244
    case 0x0a:        /* SPCR1 */
3245
        return s->spcr[0];
3246
    case 0x0c:        /* RCR2 */
3247
        return s->rcr[1];
3248
    case 0x0e:        /* RCR1 */
3249
        return s->rcr[0];
3250
    case 0x10:        /* XCR2 */
3251
        return s->xcr[1];
3252
    case 0x12:        /* XCR1 */
3253
        return s->xcr[0];
3254
    case 0x14:        /* SRGR2 */
3255
        return s->srgr[1];
3256
    case 0x16:        /* SRGR1 */
3257
        return s->srgr[0];
3258
    case 0x18:        /* MCR2 */
3259
        return s->mcr[1];
3260
    case 0x1a:        /* MCR1 */
3261
        return s->mcr[0];
3262
    case 0x1c:        /* RCERA */
3263
        return s->rcer[0];
3264
    case 0x1e:        /* RCERB */
3265
        return s->rcer[1];
3266
    case 0x20:        /* XCERA */
3267
        return s->xcer[0];
3268
    case 0x22:        /* XCERB */
3269
        return s->xcer[1];
3270
    case 0x24:        /* PCR0 */
3271
        return s->pcr;
3272
    case 0x26:        /* RCERC */
3273
        return s->rcer[2];
3274
    case 0x28:        /* RCERD */
3275
        return s->rcer[3];
3276
    case 0x2a:        /* XCERC */
3277
        return s->xcer[2];
3278
    case 0x2c:        /* XCERD */
3279
        return s->xcer[3];
3280
    case 0x2e:        /* RCERE */
3281
        return s->rcer[4];
3282
    case 0x30:        /* RCERF */
3283
        return s->rcer[5];
3284
    case 0x32:        /* XCERE */
3285
        return s->xcer[4];
3286
    case 0x34:        /* XCERF */
3287
        return s->xcer[5];
3288
    case 0x36:        /* RCERG */
3289
        return s->rcer[6];
3290
    case 0x38:        /* RCERH */
3291
        return s->rcer[7];
3292
    case 0x3a:        /* XCERG */
3293
        return s->xcer[6];
3294
    case 0x3c:        /* XCERH */
3295
        return s->xcer[7];
3296
    }
3297

    
3298
    OMAP_BAD_REG(addr);
3299
    return 0;
3300
}
3301

    
3302
static void omap_mcbsp_writeh(void *opaque, target_phys_addr_t addr,
3303
                uint32_t value)
3304
{
3305
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3306
    int offset = addr & OMAP_MPUI_REG_MASK;
3307

    
3308
    switch (offset) {
3309
    case 0x00:        /* DRR2 */
3310
    case 0x02:        /* DRR1 */
3311
        OMAP_RO_REG(addr);
3312
        return;
3313

    
3314
    case 0x04:        /* DXR2 */
3315
        if (((s->xcr[0] >> 5) & 7) < 3)                        /* XWDLEN1 */
3316
            return;
3317
        /* Fall through.  */
3318
    case 0x06:        /* DXR1 */
3319
        if (s->tx_req > 1) {
3320
            s->tx_req -= 2;
3321
            if (s->codec && s->codec->cts) {
3322
                s->codec->out.fifo[s->codec->out.len ++] = (value >> 8) & 0xff;
3323
                s->codec->out.fifo[s->codec->out.len ++] = (value >> 0) & 0xff;
3324
            }
3325
            if (s->tx_req < 2)
3326
                omap_mcbsp_tx_done(s);
3327
        } else
3328
            printf("%s: Tx FIFO overrun\n", __FUNCTION__);
3329
        return;
3330

    
3331
    case 0x08:        /* SPCR2 */
3332
        s->spcr[1] &= 0x0002;
3333
        s->spcr[1] |= 0x03f9 & value;
3334
        s->spcr[1] |= 0x0004 & (value << 2);                /* XEMPTY := XRST */
3335
        if (~value & 1)                                        /* XRST */
3336
            s->spcr[1] &= ~6;
3337
        omap_mcbsp_req_update(s);
3338
        return;
3339
    case 0x0a:        /* SPCR1 */
3340
        s->spcr[0] &= 0x0006;
3341
        s->spcr[0] |= 0xf8f9 & value;
3342
        if (value & (1 << 15))                                /* DLB */
3343
            printf("%s: Digital Loopback mode enable attempt\n", __FUNCTION__);
3344
        if (~value & 1) {                                /* RRST */
3345
            s->spcr[0] &= ~6;
3346
            s->rx_req = 0;
3347
            omap_mcbsp_rx_done(s);
3348
        }
3349
        omap_mcbsp_req_update(s);
3350
        return;
3351

    
3352
    case 0x0c:        /* RCR2 */
3353
        s->rcr[1] = value & 0xffff;
3354
        return;
3355
    case 0x0e:        /* RCR1 */
3356
        s->rcr[0] = value & 0x7fe0;
3357
        return;
3358
    case 0x10:        /* XCR2 */
3359
        s->xcr[1] = value & 0xffff;
3360
        return;
3361
    case 0x12:        /* XCR1 */
3362
        s->xcr[0] = value & 0x7fe0;
3363
        return;
3364
    case 0x14:        /* SRGR2 */
3365
        s->srgr[1] = value & 0xffff;
3366
        omap_mcbsp_req_update(s);
3367
        return;
3368
    case 0x16:        /* SRGR1 */
3369
        s->srgr[0] = value & 0xffff;
3370
        omap_mcbsp_req_update(s);
3371
        return;
3372
    case 0x18:        /* MCR2 */
3373
        s->mcr[1] = value & 0x03e3;
3374
        if (value & 3)                                        /* XMCM */
3375
            printf("%s: Tx channel selection mode enable attempt\n",
3376
                            __FUNCTION__);
3377
        return;
3378
    case 0x1a:        /* MCR1 */
3379
        s->mcr[0] = value & 0x03e1;
3380
        if (value & 1)                                        /* RMCM */
3381
            printf("%s: Rx channel selection mode enable attempt\n",
3382
                            __FUNCTION__);
3383
        return;
3384
    case 0x1c:        /* RCERA */
3385
        s->rcer[0] = value & 0xffff;
3386
        return;
3387
    case 0x1e:        /* RCERB */
3388
        s->rcer[1] = value & 0xffff;
3389
        return;
3390
    case 0x20:        /* XCERA */
3391
        s->xcer[0] = value & 0xffff;
3392
        return;
3393
    case 0x22:        /* XCERB */
3394
        s->xcer[1] = value & 0xffff;
3395
        return;
3396
    case 0x24:        /* PCR0 */
3397
        s->pcr = value & 0x7faf;
3398
        return;
3399
    case 0x26:        /* RCERC */
3400
        s->rcer[2] = value & 0xffff;
3401
        return;
3402
    case 0x28:        /* RCERD */
3403
        s->rcer[3] = value & 0xffff;
3404
        return;
3405
    case 0x2a:        /* XCERC */
3406
        s->xcer[2] = value & 0xffff;
3407
        return;
3408
    case 0x2c:        /* XCERD */
3409
        s->xcer[3] = value & 0xffff;
3410
        return;
3411
    case 0x2e:        /* RCERE */
3412
        s->rcer[4] = value & 0xffff;
3413
        return;
3414
    case 0x30:        /* RCERF */
3415
        s->rcer[5] = value & 0xffff;
3416
        return;
3417
    case 0x32:        /* XCERE */
3418
        s->xcer[4] = value & 0xffff;
3419
        return;
3420
    case 0x34:        /* XCERF */
3421
        s->xcer[5] = value & 0xffff;
3422
        return;
3423
    case 0x36:        /* RCERG */
3424
        s->rcer[6] = value & 0xffff;
3425
        return;
3426
    case 0x38:        /* RCERH */
3427
        s->rcer[7] = value & 0xffff;
3428
        return;
3429
    case 0x3a:        /* XCERG */
3430
        s->xcer[6] = value & 0xffff;
3431
        return;
3432
    case 0x3c:        /* XCERH */
3433
        s->xcer[7] = value & 0xffff;
3434
        return;
3435
    }
3436

    
3437
    OMAP_BAD_REG(addr);
3438
}
3439

    
3440
static void omap_mcbsp_writew(void *opaque, target_phys_addr_t addr,
3441
                uint32_t value)
3442
{
3443
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3444
    int offset = addr & OMAP_MPUI_REG_MASK;
3445

    
3446
    if (offset == 0x04) {                                /* DXR */
3447
        if (((s->xcr[0] >> 5) & 7) < 3)                        /* XWDLEN1 */
3448
            return;
3449
        if (s->tx_req > 3) {
3450
            s->tx_req -= 4;
3451
            if (s->codec && s->codec->cts) {
3452
                s->codec->out.fifo[s->codec->out.len ++] =
3453
                        (value >> 24) & 0xff;
3454
                s->codec->out.fifo[s->codec->out.len ++] =
3455
                        (value >> 16) & 0xff;
3456
                s->codec->out.fifo[s->codec->out.len ++] =
3457
                        (value >> 8) & 0xff;
3458
                s->codec->out.fifo[s->codec->out.len ++] =
3459
                        (value >> 0) & 0xff;
3460
            }
3461
            if (s->tx_req < 4)
3462
                omap_mcbsp_tx_done(s);
3463
        } else
3464
            printf("%s: Tx FIFO overrun\n", __FUNCTION__);
3465
        return;
3466
    }
3467

    
3468
    omap_badwidth_write16(opaque, addr, value);
3469
}
3470

    
3471
static CPUReadMemoryFunc * const omap_mcbsp_readfn[] = {
3472
    omap_badwidth_read16,
3473
    omap_mcbsp_read,
3474
    omap_badwidth_read16,
3475
};
3476

    
3477
static CPUWriteMemoryFunc * const omap_mcbsp_writefn[] = {
3478
    omap_badwidth_write16,
3479
    omap_mcbsp_writeh,
3480
    omap_mcbsp_writew,
3481
};
3482

    
3483
static void omap_mcbsp_reset(struct omap_mcbsp_s *s)
3484
{
3485
    memset(&s->spcr, 0, sizeof(s->spcr));
3486
    memset(&s->rcr, 0, sizeof(s->rcr));
3487
    memset(&s->xcr, 0, sizeof(s->xcr));
3488
    s->srgr[0] = 0x0001;
3489
    s->srgr[1] = 0x2000;
3490
    memset(&s->mcr, 0, sizeof(s->mcr));
3491
    memset(&s->pcr, 0, sizeof(s->pcr));
3492
    memset(&s->rcer, 0, sizeof(s->rcer));
3493
    memset(&s->xcer, 0, sizeof(s->xcer));
3494
    s->tx_req = 0;
3495
    s->rx_req = 0;
3496
    s->tx_rate = 0;
3497
    s->rx_rate = 0;
3498
    qemu_del_timer(s->source_timer);
3499
    qemu_del_timer(s->sink_timer);
3500
}
3501

    
3502
struct omap_mcbsp_s *omap_mcbsp_init(target_phys_addr_t base,
3503
                qemu_irq *irq, qemu_irq *dma, omap_clk clk)
3504
{
3505
    int iomemtype;
3506
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *)
3507
            qemu_mallocz(sizeof(struct omap_mcbsp_s));
3508

    
3509
    s->txirq = irq[0];
3510
    s->rxirq = irq[1];
3511
    s->txdrq = dma[0];
3512
    s->rxdrq = dma[1];
3513
    s->sink_timer = qemu_new_timer(vm_clock, omap_mcbsp_sink_tick, s);
3514
    s->source_timer = qemu_new_timer(vm_clock, omap_mcbsp_source_tick, s);
3515
    omap_mcbsp_reset(s);
3516

    
3517
    iomemtype = cpu_register_io_memory(omap_mcbsp_readfn,
3518
                    omap_mcbsp_writefn, s);
3519
    cpu_register_physical_memory(base, 0x800, iomemtype);
3520

    
3521
    return s;
3522
}
3523

    
3524
static void omap_mcbsp_i2s_swallow(void *opaque, int line, int level)
3525
{
3526
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3527

    
3528
    if (s->rx_rate) {
3529
        s->rx_req = s->codec->in.len;
3530
        omap_mcbsp_rx_newdata(s);
3531
    }
3532
}
3533

    
3534
static void omap_mcbsp_i2s_start(void *opaque, int line, int level)
3535
{
3536
    struct omap_mcbsp_s *s = (struct omap_mcbsp_s *) opaque;
3537

    
3538
    if (s->tx_rate) {
3539
        s->tx_req = s->codec->out.size;
3540
        omap_mcbsp_tx_newdata(s);
3541
    }
3542
}
3543

    
3544
void omap_mcbsp_i2s_attach(struct omap_mcbsp_s *s, I2SCodec *slave)
3545
{
3546
    s->codec = slave;
3547
    slave->rx_swallow = qemu_allocate_irqs(omap_mcbsp_i2s_swallow, s, 1)[0];
3548
    slave->tx_start = qemu_allocate_irqs(omap_mcbsp_i2s_start, s, 1)[0];
3549
}
3550

    
3551
/* LED Pulse Generators */
3552
struct omap_lpg_s {
3553
    QEMUTimer *tm;
3554

    
3555
    uint8_t control;
3556
    uint8_t power;
3557
    int64_t on;
3558
    int64_t period;
3559
    int clk;
3560
    int cycle;
3561
};
3562

    
3563
static void omap_lpg_tick(void *opaque)
3564
{
3565
    struct omap_lpg_s *s = opaque;
3566

    
3567
    if (s->cycle)
3568
        qemu_mod_timer(s->tm, qemu_get_clock(rt_clock) + s->period - s->on);
3569
    else
3570
        qemu_mod_timer(s->tm, qemu_get_clock(rt_clock) + s->on);
3571

    
3572
    s->cycle = !s->cycle;
3573
    printf("%s: LED is %s\n", __FUNCTION__, s->cycle ? "on" : "off");
3574
}
3575

    
3576
static void omap_lpg_update(struct omap_lpg_s *s)
3577
{
3578
    int64_t on, period = 1, ticks = 1000;
3579
    static const int per[8] = { 1, 2, 4, 8, 12, 16, 20, 24 };
3580

    
3581
    if (~s->control & (1 << 6))                                        /* LPGRES */
3582
        on = 0;
3583
    else if (s->control & (1 << 7))                                /* PERM_ON */
3584
        on = period;
3585
    else {
3586
        period = muldiv64(ticks, per[s->control & 7],                /* PERCTRL */
3587
                        256 / 32);
3588
        on = (s->clk && s->power) ? muldiv64(ticks,
3589
                        per[(s->control >> 3) & 7], 256) : 0;        /* ONCTRL */
3590
    }
3591

    
3592
    qemu_del_timer(s->tm);
3593
    if (on == period && s->on < s->period)
3594
        printf("%s: LED is on\n", __FUNCTION__);
3595
    else if (on == 0 && s->on)
3596
        printf("%s: LED is off\n", __FUNCTION__);
3597
    else if (on && (on != s->on || period != s->period)) {
3598
        s->cycle = 0;
3599
        s->on = on;
3600
        s->period = period;
3601
        omap_lpg_tick(s);
3602
        return;
3603
    }
3604

    
3605
    s->on = on;
3606
    s->period = period;
3607
}
3608

    
3609
static void omap_lpg_reset(struct omap_lpg_s *s)
3610
{
3611
    s->control = 0x00;
3612
    s->power = 0x00;
3613
    s->clk = 1;
3614
    omap_lpg_update(s);
3615
}
3616

    
3617
static uint32_t omap_lpg_read(void *opaque, target_phys_addr_t addr)
3618
{
3619
    struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3620
    int offset = addr & OMAP_MPUI_REG_MASK;
3621

    
3622
    switch (offset) {
3623
    case 0x00:        /* LCR */
3624
        return s->control;
3625

    
3626
    case 0x04:        /* PMR */
3627
        return s->power;
3628
    }
3629

    
3630
    OMAP_BAD_REG(addr);
3631
    return 0;
3632
}
3633

    
3634
static void omap_lpg_write(void *opaque, target_phys_addr_t addr,
3635
                uint32_t value)
3636
{
3637
    struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3638
    int offset = addr & OMAP_MPUI_REG_MASK;
3639

    
3640
    switch (offset) {
3641
    case 0x00:        /* LCR */
3642
        if (~value & (1 << 6))                                        /* LPGRES */
3643
            omap_lpg_reset(s);
3644
        s->control = value & 0xff;
3645
        omap_lpg_update(s);
3646
        return;
3647

    
3648
    case 0x04:        /* PMR */
3649
        s->power = value & 0x01;
3650
        omap_lpg_update(s);
3651
        return;
3652

    
3653
    default:
3654
        OMAP_BAD_REG(addr);
3655
        return;
3656
    }
3657
}
3658

    
3659
static CPUReadMemoryFunc * const omap_lpg_readfn[] = {
3660
    omap_lpg_read,
3661
    omap_badwidth_read8,
3662
    omap_badwidth_read8,
3663
};
3664

    
3665
static CPUWriteMemoryFunc * const omap_lpg_writefn[] = {
3666
    omap_lpg_write,
3667
    omap_badwidth_write8,
3668
    omap_badwidth_write8,
3669
};
3670

    
3671
static void omap_lpg_clk_update(void *opaque, int line, int on)
3672
{
3673
    struct omap_lpg_s *s = (struct omap_lpg_s *) opaque;
3674

    
3675
    s->clk = on;
3676
    omap_lpg_update(s);
3677
}
3678

    
3679
struct omap_lpg_s *omap_lpg_init(target_phys_addr_t base, omap_clk clk)
3680
{
3681
    int iomemtype;
3682
    struct omap_lpg_s *s = (struct omap_lpg_s *)
3683
            qemu_mallocz(sizeof(struct omap_lpg_s));
3684

    
3685
    s->tm = qemu_new_timer(rt_clock, omap_lpg_tick, s);
3686

    
3687
    omap_lpg_reset(s);
3688

    
3689
    iomemtype = cpu_register_io_memory(omap_lpg_readfn,
3690
                    omap_lpg_writefn, s);
3691
    cpu_register_physical_memory(base, 0x800, iomemtype);
3692

    
3693
    omap_clk_adduser(clk, qemu_allocate_irqs(omap_lpg_clk_update, s, 1)[0]);
3694

    
3695
    return s;
3696
}
3697

    
3698
/* MPUI Peripheral Bridge configuration */
3699
static uint32_t omap_mpui_io_read(void *opaque, target_phys_addr_t addr)
3700
{
3701
    if (addr == OMAP_MPUI_BASE)        /* CMR */
3702
        return 0xfe4d;
3703

    
3704
    OMAP_BAD_REG(addr);
3705
    return 0;
3706
}
3707

    
3708
static CPUReadMemoryFunc * const omap_mpui_io_readfn[] = {
3709
    omap_badwidth_read16,
3710
    omap_mpui_io_read,
3711
    omap_badwidth_read16,
3712
};
3713

    
3714
static CPUWriteMemoryFunc * const omap_mpui_io_writefn[] = {
3715
    omap_badwidth_write16,
3716
    omap_badwidth_write16,
3717
    omap_badwidth_write16,
3718
};
3719

    
3720
static void omap_setup_mpui_io(struct omap_mpu_state_s *mpu)
3721
{
3722
    int iomemtype = cpu_register_io_memory(omap_mpui_io_readfn,
3723
                    omap_mpui_io_writefn, mpu);
3724
    cpu_register_physical_memory(OMAP_MPUI_BASE, 0x7fff, iomemtype);
3725
}
3726

    
3727
/* General chip reset */
3728
static void omap1_mpu_reset(void *opaque)
3729
{
3730
    struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
3731

    
3732
    omap_inth_reset(mpu->ih[0]);
3733
    omap_inth_reset(mpu->ih[1]);
3734
    omap_dma_reset(mpu->dma);
3735
    omap_mpu_timer_reset(mpu->timer[0]);
3736
    omap_mpu_timer_reset(mpu->timer[1]);
3737
    omap_mpu_timer_reset(mpu->timer[2]);
3738
    omap_wd_timer_reset(mpu->wdt);
3739
    omap_os_timer_reset(mpu->os_timer);
3740
    omap_lcdc_reset(mpu->lcd);
3741
    omap_ulpd_pm_reset(mpu);
3742
    omap_pin_cfg_reset(mpu);
3743
    omap_mpui_reset(mpu);
3744
    omap_tipb_bridge_reset(mpu->private_tipb);
3745
    omap_tipb_bridge_reset(mpu->public_tipb);
3746
    omap_dpll_reset(&mpu->dpll[0]);
3747
    omap_dpll_reset(&mpu->dpll[1]);
3748
    omap_dpll_reset(&mpu->dpll[2]);
3749
    omap_uart_reset(mpu->uart[0]);
3750
    omap_uart_reset(mpu->uart[1]);
3751
    omap_uart_reset(mpu->uart[2]);
3752
    omap_mmc_reset(mpu->mmc);
3753
    omap_mpuio_reset(mpu->mpuio);
3754
    omap_gpio_reset(mpu->gpio);
3755
    omap_uwire_reset(mpu->microwire);
3756
    omap_pwl_reset(mpu);
3757
    omap_pwt_reset(mpu);
3758
    omap_i2c_reset(mpu->i2c[0]);
3759
    omap_rtc_reset(mpu->rtc);
3760
    omap_mcbsp_reset(mpu->mcbsp1);
3761
    omap_mcbsp_reset(mpu->mcbsp2);
3762
    omap_mcbsp_reset(mpu->mcbsp3);
3763
    omap_lpg_reset(mpu->led[0]);
3764
    omap_lpg_reset(mpu->led[1]);
3765
    omap_clkm_reset(mpu);
3766
    cpu_reset(mpu->env);
3767
}
3768

    
3769
static const struct omap_map_s {
3770
    target_phys_addr_t phys_dsp;
3771
    target_phys_addr_t phys_mpu;
3772
    uint32_t size;
3773
    const char *name;
3774
} omap15xx_dsp_mm[] = {
3775
    /* Strobe 0 */
3776
    { 0xe1010000, 0xfffb0000, 0x800, "UART1 BT" },                /* CS0 */
3777
    { 0xe1010800, 0xfffb0800, 0x800, "UART2 COM" },                /* CS1 */
3778
    { 0xe1011800, 0xfffb1800, 0x800, "McBSP1 audio" },                /* CS3 */
3779
    { 0xe1012000, 0xfffb2000, 0x800, "MCSI2 communication" },        /* CS4 */
3780
    { 0xe1012800, 0xfffb2800, 0x800, "MCSI1 BT u-Law" },        /* CS5 */
3781
    { 0xe1013000, 0xfffb3000, 0x800, "uWire" },                        /* CS6 */
3782
    { 0xe1013800, 0xfffb3800, 0x800, "I^2C" },                        /* CS7 */
3783
    { 0xe1014000, 0xfffb4000, 0x800, "USB W2FC" },                /* CS8 */
3784
    { 0xe1014800, 0xfffb4800, 0x800, "RTC" },                        /* CS9 */
3785
    { 0xe1015000, 0xfffb5000, 0x800, "MPUIO" },                        /* CS10 */
3786
    { 0xe1015800, 0xfffb5800, 0x800, "PWL" },                        /* CS11 */
3787
    { 0xe1016000, 0xfffb6000, 0x800, "PWT" },                        /* CS12 */
3788
    { 0xe1017000, 0xfffb7000, 0x800, "McBSP3" },                /* CS14 */
3789
    { 0xe1017800, 0xfffb7800, 0x800, "MMC" },                        /* CS15 */
3790
    { 0xe1019000, 0xfffb9000, 0x800, "32-kHz timer" },                /* CS18 */
3791
    { 0xe1019800, 0xfffb9800, 0x800, "UART3" },                        /* CS19 */
3792
    { 0xe101c800, 0xfffbc800, 0x800, "TIPB switches" },                /* CS25 */
3793
    /* Strobe 1 */
3794
    { 0xe101e000, 0xfffce000, 0x800, "GPIOs" },                        /* CS28 */
3795

    
3796
    { 0 }
3797
};
3798

    
3799
static void omap_setup_dsp_mapping(const struct omap_map_s *map)
3800
{
3801
    int io;
3802

    
3803
    for (; map->phys_dsp; map ++) {
3804
        io = cpu_get_physical_page_desc(map->phys_mpu);
3805

    
3806
        cpu_register_physical_memory(map->phys_dsp, map->size, io);
3807
    }
3808
}
3809

    
3810
void omap_mpu_wakeup(void *opaque, int irq, int req)
3811
{
3812
    struct omap_mpu_state_s *mpu = (struct omap_mpu_state_s *) opaque;
3813

    
3814
    if (mpu->env->halted)
3815
        cpu_interrupt(mpu->env, CPU_INTERRUPT_EXITTB);
3816
}
3817

    
3818
static const struct dma_irq_map omap1_dma_irq_map[] = {
3819
    { 0, OMAP_INT_DMA_CH0_6 },
3820
    { 0, OMAP_INT_DMA_CH1_7 },
3821
    { 0, OMAP_INT_DMA_CH2_8 },
3822
    { 0, OMAP_INT_DMA_CH3 },
3823
    { 0, OMAP_INT_DMA_CH4 },
3824
    { 0, OMAP_INT_DMA_CH5 },
3825
    { 1, OMAP_INT_1610_DMA_CH6 },
3826
    { 1, OMAP_INT_1610_DMA_CH7 },
3827
    { 1, OMAP_INT_1610_DMA_CH8 },
3828
    { 1, OMAP_INT_1610_DMA_CH9 },
3829
    { 1, OMAP_INT_1610_DMA_CH10 },
3830
    { 1, OMAP_INT_1610_DMA_CH11 },
3831
    { 1, OMAP_INT_1610_DMA_CH12 },
3832
    { 1, OMAP_INT_1610_DMA_CH13 },
3833
    { 1, OMAP_INT_1610_DMA_CH14 },
3834
    { 1, OMAP_INT_1610_DMA_CH15 }
3835
};
3836

    
3837
/* DMA ports for OMAP1 */
3838
static int omap_validate_emiff_addr(struct omap_mpu_state_s *s,
3839
                target_phys_addr_t addr)
3840
{
3841
    return addr >= OMAP_EMIFF_BASE && addr < OMAP_EMIFF_BASE + s->sdram_size;
3842
}
3843

    
3844
static int omap_validate_emifs_addr(struct omap_mpu_state_s *s,
3845
                target_phys_addr_t addr)
3846
{
3847
    return addr >= OMAP_EMIFS_BASE && addr < OMAP_EMIFF_BASE;
3848
}
3849

    
3850
static int omap_validate_imif_addr(struct omap_mpu_state_s *s,
3851
                target_phys_addr_t addr)
3852
{
3853
    return addr >= OMAP_IMIF_BASE && addr < OMAP_IMIF_BASE + s->sram_size;
3854
}
3855

    
3856
static int omap_validate_tipb_addr(struct omap_mpu_state_s *s,
3857
                target_phys_addr_t addr)
3858
{
3859
    return addr >= 0xfffb0000 && addr < 0xffff0000;
3860
}
3861

    
3862
static int omap_validate_local_addr(struct omap_mpu_state_s *s,
3863
                target_phys_addr_t addr)
3864
{
3865
    return addr >= OMAP_LOCALBUS_BASE && addr < OMAP_LOCALBUS_BASE + 0x1000000;
3866
}
3867

    
3868
static int omap_validate_tipb_mpui_addr(struct omap_mpu_state_s *s,
3869
                target_phys_addr_t addr)
3870
{
3871
    return addr >= 0xe1010000 && addr < 0xe1020004;
3872
}
3873

    
3874
struct omap_mpu_state_s *omap310_mpu_init(unsigned long sdram_size,
3875
                const char *core)
3876
{
3877
    int i;
3878
    struct omap_mpu_state_s *s = (struct omap_mpu_state_s *)
3879
            qemu_mallocz(sizeof(struct omap_mpu_state_s));
3880
    ram_addr_t imif_base, emiff_base;
3881
    qemu_irq *cpu_irq;
3882
    qemu_irq dma_irqs[6];
3883
    DriveInfo *dinfo;
3884

    
3885
    if (!core)
3886
        core = "ti925t";
3887

    
3888
    /* Core */
3889
    s->mpu_model = omap310;
3890
    s->env = cpu_init(core);
3891
    if (!s->env) {
3892
        fprintf(stderr, "Unable to find CPU definition\n");
3893
        exit(1);
3894
    }
3895
    s->sdram_size = sdram_size;
3896
    s->sram_size = OMAP15XX_SRAM_SIZE;
3897

    
3898
    s->wakeup = qemu_allocate_irqs(omap_mpu_wakeup, s, 1)[0];
3899

    
3900
    /* Clocks */
3901
    omap_clk_init(s);
3902

    
3903
    /* Memory-mapped stuff */
3904
    cpu_register_physical_memory(OMAP_EMIFF_BASE, s->sdram_size,
3905
                    (emiff_base = qemu_ram_alloc(s->sdram_size)) | IO_MEM_RAM);
3906
    cpu_register_physical_memory(OMAP_IMIF_BASE, s->sram_size,
3907
                    (imif_base = qemu_ram_alloc(s->sram_size)) | IO_MEM_RAM);
3908

    
3909
    omap_clkm_init(0xfffece00, 0xe1008000, s);
3910

    
3911
    cpu_irq = arm_pic_init_cpu(s->env);
3912
    s->ih[0] = omap_inth_init(0xfffecb00, 0x100, 1, &s->irq[0],
3913
                    cpu_irq[ARM_PIC_CPU_IRQ], cpu_irq[ARM_PIC_CPU_FIQ],
3914
                    omap_findclk(s, "arminth_ck"));
3915
    s->ih[1] = omap_inth_init(0xfffe0000, 0x800, 1, &s->irq[1],
3916
                    omap_inth_get_pin(s->ih[0], OMAP_INT_15XX_IH2_IRQ),
3917
                    NULL, omap_findclk(s, "arminth_ck"));
3918

    
3919
    for (i = 0; i < 6; i ++)
3920
        dma_irqs[i] =
3921
                s->irq[omap1_dma_irq_map[i].ih][omap1_dma_irq_map[i].intr];
3922
    s->dma = omap_dma_init(0xfffed800, dma_irqs, s->irq[0][OMAP_INT_DMA_LCD],
3923
                           s, omap_findclk(s, "dma_ck"), omap_dma_3_1);
3924

    
3925
    s->port[emiff    ].addr_valid = omap_validate_emiff_addr;
3926
    s->port[emifs    ].addr_valid = omap_validate_emifs_addr;
3927
    s->port[imif     ].addr_valid = omap_validate_imif_addr;
3928
    s->port[tipb     ].addr_valid = omap_validate_tipb_addr;
3929
    s->port[local    ].addr_valid = omap_validate_local_addr;
3930
    s->port[tipb_mpui].addr_valid = omap_validate_tipb_mpui_addr;
3931

    
3932
    /* Register SDRAM and SRAM DMA ports for fast transfers.  */
3933
    soc_dma_port_add_mem_ram(s->dma,
3934
                    emiff_base, OMAP_EMIFF_BASE, s->sdram_size);
3935
    soc_dma_port_add_mem_ram(s->dma,
3936
                    imif_base, OMAP_IMIF_BASE, s->sram_size);
3937

    
3938
    s->timer[0] = omap_mpu_timer_init(0xfffec500,
3939
                    s->irq[0][OMAP_INT_TIMER1],
3940
                    omap_findclk(s, "mputim_ck"));
3941
    s->timer[1] = omap_mpu_timer_init(0xfffec600,
3942
                    s->irq[0][OMAP_INT_TIMER2],
3943
                    omap_findclk(s, "mputim_ck"));
3944
    s->timer[2] = omap_mpu_timer_init(0xfffec700,
3945
                    s->irq[0][OMAP_INT_TIMER3],
3946
                    omap_findclk(s, "mputim_ck"));
3947

    
3948
    s->wdt = omap_wd_timer_init(0xfffec800,
3949
                    s->irq[0][OMAP_INT_WD_TIMER],
3950
                    omap_findclk(s, "armwdt_ck"));
3951

    
3952
    s->os_timer = omap_os_timer_init(0xfffb9000,
3953
                    s->irq[1][OMAP_INT_OS_TIMER],
3954
                    omap_findclk(s, "clk32-kHz"));
3955

    
3956
    s->lcd = omap_lcdc_init(0xfffec000, s->irq[0][OMAP_INT_LCD_CTRL],
3957
                    omap_dma_get_lcdch(s->dma), imif_base, emiff_base,
3958
                    omap_findclk(s, "lcd_ck"));
3959

    
3960
    omap_ulpd_pm_init(0xfffe0800, s);
3961
    omap_pin_cfg_init(0xfffe1000, s);
3962
    omap_id_init(s);
3963

    
3964
    omap_mpui_init(0xfffec900, s);
3965

    
3966
    s->private_tipb = omap_tipb_bridge_init(0xfffeca00,
3967
                    s->irq[0][OMAP_INT_BRIDGE_PRIV],
3968
                    omap_findclk(s, "tipb_ck"));
3969
    s->public_tipb = omap_tipb_bridge_init(0xfffed300,
3970
                    s->irq[0][OMAP_INT_BRIDGE_PUB],
3971
                    omap_findclk(s, "tipb_ck"));
3972

    
3973
    omap_tcmi_init(0xfffecc00, s);
3974

    
3975
    s->uart[0] = omap_uart_init(0xfffb0000, s->irq[1][OMAP_INT_UART1],
3976
                    omap_findclk(s, "uart1_ck"),
3977
                    omap_findclk(s, "uart1_ck"),
3978
                    s->drq[OMAP_DMA_UART1_TX], s->drq[OMAP_DMA_UART1_RX],
3979
                    serial_hds[0]);
3980
    s->uart[1] = omap_uart_init(0xfffb0800, s->irq[1][OMAP_INT_UART2],
3981
                    omap_findclk(s, "uart2_ck"),
3982
                    omap_findclk(s, "uart2_ck"),
3983
                    s->drq[OMAP_DMA_UART2_TX], s->drq[OMAP_DMA_UART2_RX],
3984
                    serial_hds[0] ? serial_hds[1] : NULL);
3985
    s->uart[2] = omap_uart_init(0xfffb9800, s->irq[0][OMAP_INT_UART3],
3986
                    omap_findclk(s, "uart3_ck"),
3987
                    omap_findclk(s, "uart3_ck"),
3988
                    s->drq[OMAP_DMA_UART3_TX], s->drq[OMAP_DMA_UART3_RX],
3989
                    serial_hds[0] && serial_hds[1] ? serial_hds[2] : NULL);
3990

    
3991
    omap_dpll_init(&s->dpll[0], 0xfffecf00, omap_findclk(s, "dpll1"));
3992
    omap_dpll_init(&s->dpll[1], 0xfffed000, omap_findclk(s, "dpll2"));
3993
    omap_dpll_init(&s->dpll[2], 0xfffed100, omap_findclk(s, "dpll3"));
3994

    
3995
    dinfo = drive_get(IF_SD, 0, 0);
3996
    if (!dinfo) {
3997
        fprintf(stderr, "qemu: missing SecureDigital device\n");
3998
        exit(1);
3999
    }
4000
    s->mmc = omap_mmc_init(0xfffb7800, dinfo->bdrv,
4001
                    s->irq[1][OMAP_INT_OQN], &s->drq[OMAP_DMA_MMC_TX],
4002
                    omap_findclk(s, "mmc_ck"));
4003

    
4004
    s->mpuio = omap_mpuio_init(0xfffb5000,
4005
                    s->irq[1][OMAP_INT_KEYBOARD], s->irq[1][OMAP_INT_MPUIO],
4006
                    s->wakeup, omap_findclk(s, "clk32-kHz"));
4007

    
4008
    s->gpio = omap_gpio_init(0xfffce000, s->irq[0][OMAP_INT_GPIO_BANK1],
4009
                    omap_findclk(s, "arm_gpio_ck"));
4010

    
4011
    s->microwire = omap_uwire_init(0xfffb3000, &s->irq[1][OMAP_INT_uWireTX],
4012
                    s->drq[OMAP_DMA_UWIRE_TX], omap_findclk(s, "mpuper_ck"));
4013

    
4014
    omap_pwl_init(0xfffb5800, s, omap_findclk(s, "armxor_ck"));
4015
    omap_pwt_init(0xfffb6000, s, omap_findclk(s, "armxor_ck"));
4016

    
4017
    s->i2c[0] = omap_i2c_init(0xfffb3800, s->irq[1][OMAP_INT_I2C],
4018
                    &s->drq[OMAP_DMA_I2C_RX], omap_findclk(s, "mpuper_ck"));
4019

    
4020
    s->rtc = omap_rtc_init(0xfffb4800, &s->irq[1][OMAP_INT_RTC_TIMER],
4021
                    omap_findclk(s, "clk32-kHz"));
4022

    
4023
    s->mcbsp1 = omap_mcbsp_init(0xfffb1800, &s->irq[1][OMAP_INT_McBSP1TX],
4024
                    &s->drq[OMAP_DMA_MCBSP1_TX], omap_findclk(s, "dspxor_ck"));
4025
    s->mcbsp2 = omap_mcbsp_init(0xfffb1000, &s->irq[0][OMAP_INT_310_McBSP2_TX],
4026
                    &s->drq[OMAP_DMA_MCBSP2_TX], omap_findclk(s, "mpuper_ck"));
4027
    s->mcbsp3 = omap_mcbsp_init(0xfffb7000, &s->irq[1][OMAP_INT_McBSP3TX],
4028
                    &s->drq[OMAP_DMA_MCBSP3_TX], omap_findclk(s, "dspxor_ck"));
4029

    
4030
    s->led[0] = omap_lpg_init(0xfffbd000, omap_findclk(s, "clk32-kHz"));
4031
    s->led[1] = omap_lpg_init(0xfffbd800, omap_findclk(s, "clk32-kHz"));
4032

    
4033
    /* Register mappings not currenlty implemented:
4034
     * MCSI2 Comm        fffb2000 - fffb27ff (not mapped on OMAP310)
4035
     * MCSI1 Bluetooth        fffb2800 - fffb2fff (not mapped on OMAP310)
4036
     * USB W2FC                fffb4000 - fffb47ff
4037
     * Camera Interface        fffb6800 - fffb6fff
4038
     * USB Host                fffba000 - fffba7ff
4039
     * FAC                fffba800 - fffbafff
4040
     * HDQ/1-Wire        fffbc000 - fffbc7ff
4041
     * TIPB switches        fffbc800 - fffbcfff
4042
     * Mailbox                fffcf000 - fffcf7ff
4043
     * Local bus IF        fffec100 - fffec1ff
4044
     * Local bus MMU        fffec200 - fffec2ff
4045
     * DSP MMU                fffed200 - fffed2ff
4046
     */
4047

    
4048
    omap_setup_dsp_mapping(omap15xx_dsp_mm);
4049
    omap_setup_mpui_io(s);
4050

    
4051
    qemu_register_reset(omap1_mpu_reset, s);
4052

    
4053
    return s;
4054
}