2 * Luminary Micro Stellaris peripherals
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "hw/sysbus.h"
12 #include "hw/arm/arm.h"
13 #include "hw/devices.h"
14 #include "qemu/timer.h"
15 #include "hw/i2c/i2c.h"
17 #include "hw/boards.h"
18 #include "exec/address-spaces.h"
28 #define BP_OLED_I2C 0x01
29 #define BP_OLED_SSI 0x02
30 #define BP_GAMEPAD 0x04
32 typedef const struct {
42 } stellaris_board_info
;
44 /* General purpose timer module. */
46 #define TYPE_STELLARIS_GPTM "stellaris-gptm"
47 #define STELLARIS_GPTM(obj) \
48 OBJECT_CHECK(gptm_state, (obj), TYPE_STELLARIS_GPTM)
50 typedef struct gptm_state
{
51 SysBusDevice parent_obj
;
62 uint32_t match_prescale
[2];
65 struct gptm_state
*opaque
[2];
67 /* The timers have an alternate output used to trigger the ADC. */
72 static void gptm_update_irq(gptm_state
*s
)
75 level
= (s
->state
& s
->mask
) != 0;
76 qemu_set_irq(s
->irq
, level
);
79 static void gptm_stop(gptm_state
*s
, int n
)
81 timer_del(s
->timer
[n
]);
84 static void gptm_reload(gptm_state
*s
, int n
, int reset
)
88 tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
93 /* 32-bit CountDown. */
95 count
= s
->load
[0] | (s
->load
[1] << 16);
96 tick
+= (int64_t)count
* system_clock_scale
;
97 } else if (s
->config
== 1) {
98 /* 32-bit RTC. 1Hz tick. */
99 tick
+= get_ticks_per_sec();
100 } else if (s
->mode
[n
] == 0xa) {
101 /* PWM mode. Not implemented. */
103 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
106 timer_mod(s
->timer
[n
], tick
);
109 static void gptm_tick(void *opaque
)
111 gptm_state
**p
= (gptm_state
**)opaque
;
117 if (s
->config
== 0) {
119 if ((s
->control
& 0x20)) {
120 /* Output trigger. */
121 qemu_irq_pulse(s
->trigger
);
123 if (s
->mode
[0] & 1) {
128 gptm_reload(s
, 0, 0);
130 } else if (s
->config
== 1) {
134 match
= s
->match
[0] | (s
->match
[1] << 16);
140 gptm_reload(s
, 0, 0);
141 } else if (s
->mode
[n
] == 0xa) {
142 /* PWM mode. Not implemented. */
144 hw_error("TODO: 16-bit timer mode 0x%x\n", s
->mode
[n
]);
149 static uint64_t gptm_read(void *opaque
, hwaddr offset
,
152 gptm_state
*s
= (gptm_state
*)opaque
;
157 case 0x04: /* TAMR */
159 case 0x08: /* TBMR */
168 return s
->state
& s
->mask
;
171 case 0x28: /* TAILR */
172 return s
->load
[0] | ((s
->config
< 4) ? (s
->load
[1] << 16) : 0);
173 case 0x2c: /* TBILR */
175 case 0x30: /* TAMARCHR */
176 return s
->match
[0] | ((s
->config
< 4) ? (s
->match
[1] << 16) : 0);
177 case 0x34: /* TBMATCHR */
179 case 0x38: /* TAPR */
180 return s
->prescale
[0];
181 case 0x3c: /* TBPR */
182 return s
->prescale
[1];
183 case 0x40: /* TAPMR */
184 return s
->match_prescale
[0];
185 case 0x44: /* TBPMR */
186 return s
->match_prescale
[1];
191 hw_error("TODO: Timer value read\n");
193 hw_error("gptm_read: Bad offset 0x%x\n", (int)offset
);
198 static void gptm_write(void *opaque
, hwaddr offset
,
199 uint64_t value
, unsigned size
)
201 gptm_state
*s
= (gptm_state
*)opaque
;
204 /* The timers should be disabled before changing the configuration.
205 We take advantage of this and defer everything until the timer
211 case 0x04: /* TAMR */
214 case 0x08: /* TBMR */
220 /* TODO: Implement pause. */
221 if ((oldval
^ value
) & 1) {
223 gptm_reload(s
, 0, 1);
228 if (((oldval
^ value
) & 0x100) && s
->config
>= 4) {
230 gptm_reload(s
, 1, 1);
237 s
->mask
= value
& 0x77;
243 case 0x28: /* TAILR */
244 s
->load
[0] = value
& 0xffff;
246 s
->load
[1] = value
>> 16;
249 case 0x2c: /* TBILR */
250 s
->load
[1] = value
& 0xffff;
252 case 0x30: /* TAMARCHR */
253 s
->match
[0] = value
& 0xffff;
255 s
->match
[1] = value
>> 16;
258 case 0x34: /* TBMATCHR */
259 s
->match
[1] = value
>> 16;
261 case 0x38: /* TAPR */
262 s
->prescale
[0] = value
;
264 case 0x3c: /* TBPR */
265 s
->prescale
[1] = value
;
267 case 0x40: /* TAPMR */
268 s
->match_prescale
[0] = value
;
270 case 0x44: /* TBPMR */
271 s
->match_prescale
[0] = value
;
274 hw_error("gptm_write: Bad offset 0x%x\n", (int)offset
);
279 static const MemoryRegionOps gptm_ops
= {
282 .endianness
= DEVICE_NATIVE_ENDIAN
,
285 static const VMStateDescription vmstate_stellaris_gptm
= {
286 .name
= "stellaris_gptm",
288 .minimum_version_id
= 1,
289 .minimum_version_id_old
= 1,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_UINT32(config
, gptm_state
),
292 VMSTATE_UINT32_ARRAY(mode
, gptm_state
, 2),
293 VMSTATE_UINT32(control
, gptm_state
),
294 VMSTATE_UINT32(state
, gptm_state
),
295 VMSTATE_UINT32(mask
, gptm_state
),
297 VMSTATE_UINT32_ARRAY(load
, gptm_state
, 2),
298 VMSTATE_UINT32_ARRAY(match
, gptm_state
, 2),
299 VMSTATE_UINT32_ARRAY(prescale
, gptm_state
, 2),
300 VMSTATE_UINT32_ARRAY(match_prescale
, gptm_state
, 2),
301 VMSTATE_UINT32(rtc
, gptm_state
),
302 VMSTATE_INT64_ARRAY(tick
, gptm_state
, 2),
303 VMSTATE_TIMER_ARRAY(timer
, gptm_state
, 2),
304 VMSTATE_END_OF_LIST()
308 static int stellaris_gptm_init(SysBusDevice
*sbd
)
310 DeviceState
*dev
= DEVICE(sbd
);
311 gptm_state
*s
= STELLARIS_GPTM(dev
);
313 sysbus_init_irq(sbd
, &s
->irq
);
314 qdev_init_gpio_out(dev
, &s
->trigger
, 1);
316 memory_region_init_io(&s
->iomem
, OBJECT(s
), &gptm_ops
, s
,
318 sysbus_init_mmio(sbd
, &s
->iomem
);
320 s
->opaque
[0] = s
->opaque
[1] = s
;
321 s
->timer
[0] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[0]);
322 s
->timer
[1] = timer_new_ns(QEMU_CLOCK_VIRTUAL
, gptm_tick
, &s
->opaque
[1]);
323 vmstate_register(dev
, -1, &vmstate_stellaris_gptm
, s
);
328 /* System controller. */
347 stellaris_board_info
*board
;
350 static void ssys_update(ssys_state
*s
)
352 qemu_set_irq(s
->irq
, (s
->int_status
& s
->int_mask
) != 0);
355 static uint32_t pllcfg_sandstorm
[16] = {
357 0x1ae0, /* 1.8432 Mhz */
359 0xd573, /* 2.4576 Mhz */
360 0x37a6, /* 3.57954 Mhz */
361 0x1ae2, /* 3.6864 Mhz */
363 0x98bc, /* 4.906 Mhz */
364 0x935b, /* 4.9152 Mhz */
366 0x4dee, /* 5.12 Mhz */
368 0x75db, /* 6.144 Mhz */
369 0x1ae6, /* 7.3728 Mhz */
371 0x585b /* 8.192 Mhz */
374 static uint32_t pllcfg_fury
[16] = {
376 0x1b20, /* 1.8432 Mhz */
378 0xf42b, /* 2.4576 Mhz */
379 0x37e3, /* 3.57954 Mhz */
380 0x1b21, /* 3.6864 Mhz */
382 0x98ee, /* 4.906 Mhz */
383 0xd5b4, /* 4.9152 Mhz */
385 0x4e27, /* 5.12 Mhz */
387 0xec1c, /* 6.144 Mhz */
388 0x1b23, /* 7.3728 Mhz */
390 0xb11c /* 8.192 Mhz */
393 #define DID0_VER_MASK 0x70000000
394 #define DID0_VER_0 0x00000000
395 #define DID0_VER_1 0x10000000
397 #define DID0_CLASS_MASK 0x00FF0000
398 #define DID0_CLASS_SANDSTORM 0x00000000
399 #define DID0_CLASS_FURY 0x00010000
401 static int ssys_board_class(const ssys_state
*s
)
403 uint32_t did0
= s
->board
->did0
;
404 switch (did0
& DID0_VER_MASK
) {
406 return DID0_CLASS_SANDSTORM
;
408 switch (did0
& DID0_CLASS_MASK
) {
409 case DID0_CLASS_SANDSTORM
:
410 case DID0_CLASS_FURY
:
411 return did0
& DID0_CLASS_MASK
;
413 /* for unknown classes, fall through */
415 hw_error("ssys_board_class: Unknown class 0x%08x\n", did0
);
419 static uint64_t ssys_read(void *opaque
, hwaddr offset
,
422 ssys_state
*s
= (ssys_state
*)opaque
;
425 case 0x000: /* DID0 */
426 return s
->board
->did0
;
427 case 0x004: /* DID1 */
428 return s
->board
->did1
;
429 case 0x008: /* DC0 */
430 return s
->board
->dc0
;
431 case 0x010: /* DC1 */
432 return s
->board
->dc1
;
433 case 0x014: /* DC2 */
434 return s
->board
->dc2
;
435 case 0x018: /* DC3 */
436 return s
->board
->dc3
;
437 case 0x01c: /* DC4 */
438 return s
->board
->dc4
;
439 case 0x030: /* PBORCTL */
441 case 0x034: /* LDOPCTL */
443 case 0x040: /* SRCR0 */
445 case 0x044: /* SRCR1 */
447 case 0x048: /* SRCR2 */
449 case 0x050: /* RIS */
450 return s
->int_status
;
451 case 0x054: /* IMC */
453 case 0x058: /* MISC */
454 return s
->int_status
& s
->int_mask
;
455 case 0x05c: /* RESC */
457 case 0x060: /* RCC */
459 case 0x064: /* PLLCFG */
462 xtal
= (s
->rcc
>> 6) & 0xf;
463 switch (ssys_board_class(s
)) {
464 case DID0_CLASS_FURY
:
465 return pllcfg_fury
[xtal
];
466 case DID0_CLASS_SANDSTORM
:
467 return pllcfg_sandstorm
[xtal
];
469 hw_error("ssys_read: Unhandled class for PLLCFG read.\n");
473 case 0x070: /* RCC2 */
475 case 0x100: /* RCGC0 */
477 case 0x104: /* RCGC1 */
479 case 0x108: /* RCGC2 */
481 case 0x110: /* SCGC0 */
483 case 0x114: /* SCGC1 */
485 case 0x118: /* SCGC2 */
487 case 0x120: /* DCGC0 */
489 case 0x124: /* DCGC1 */
491 case 0x128: /* DCGC2 */
493 case 0x150: /* CLKVCLR */
495 case 0x160: /* LDOARST */
497 case 0x1e0: /* USER0 */
499 case 0x1e4: /* USER1 */
502 hw_error("ssys_read: Bad offset 0x%x\n", (int)offset
);
507 static bool ssys_use_rcc2(ssys_state
*s
)
509 return (s
->rcc2
>> 31) & 0x1;
513 * Caculate the sys. clock period in ms.
515 static void ssys_calculate_system_clock(ssys_state
*s
)
517 if (ssys_use_rcc2(s
)) {
518 system_clock_scale
= 5 * (((s
->rcc2
>> 23) & 0x3f) + 1);
520 system_clock_scale
= 5 * (((s
->rcc
>> 23) & 0xf) + 1);
524 static void ssys_write(void *opaque
, hwaddr offset
,
525 uint64_t value
, unsigned size
)
527 ssys_state
*s
= (ssys_state
*)opaque
;
530 case 0x030: /* PBORCTL */
531 s
->pborctl
= value
& 0xffff;
533 case 0x034: /* LDOPCTL */
534 s
->ldopctl
= value
& 0x1f;
536 case 0x040: /* SRCR0 */
537 case 0x044: /* SRCR1 */
538 case 0x048: /* SRCR2 */
539 fprintf(stderr
, "Peripheral reset not implemented\n");
541 case 0x054: /* IMC */
542 s
->int_mask
= value
& 0x7f;
544 case 0x058: /* MISC */
545 s
->int_status
&= ~value
;
547 case 0x05c: /* RESC */
548 s
->resc
= value
& 0x3f;
550 case 0x060: /* RCC */
551 if ((s
->rcc
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
553 s
->int_status
|= (1 << 6);
556 ssys_calculate_system_clock(s
);
558 case 0x070: /* RCC2 */
559 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
563 if ((s
->rcc2
& (1 << 13)) != 0 && (value
& (1 << 13)) == 0) {
565 s
->int_status
|= (1 << 6);
568 ssys_calculate_system_clock(s
);
570 case 0x100: /* RCGC0 */
573 case 0x104: /* RCGC1 */
576 case 0x108: /* RCGC2 */
579 case 0x110: /* SCGC0 */
582 case 0x114: /* SCGC1 */
585 case 0x118: /* SCGC2 */
588 case 0x120: /* DCGC0 */
591 case 0x124: /* DCGC1 */
594 case 0x128: /* DCGC2 */
597 case 0x150: /* CLKVCLR */
600 case 0x160: /* LDOARST */
604 hw_error("ssys_write: Bad offset 0x%x\n", (int)offset
);
609 static const MemoryRegionOps ssys_ops
= {
612 .endianness
= DEVICE_NATIVE_ENDIAN
,
615 static void ssys_reset(void *opaque
)
617 ssys_state
*s
= (ssys_state
*)opaque
;
622 if (ssys_board_class(s
) == DID0_CLASS_SANDSTORM
) {
625 s
->rcc2
= 0x07802810;
630 ssys_calculate_system_clock(s
);
633 static int stellaris_sys_post_load(void *opaque
, int version_id
)
635 ssys_state
*s
= opaque
;
637 ssys_calculate_system_clock(s
);
642 static const VMStateDescription vmstate_stellaris_sys
= {
643 .name
= "stellaris_sys",
645 .minimum_version_id
= 1,
646 .minimum_version_id_old
= 1,
647 .post_load
= stellaris_sys_post_load
,
648 .fields
= (VMStateField
[]) {
649 VMSTATE_UINT32(pborctl
, ssys_state
),
650 VMSTATE_UINT32(ldopctl
, ssys_state
),
651 VMSTATE_UINT32(int_mask
, ssys_state
),
652 VMSTATE_UINT32(int_status
, ssys_state
),
653 VMSTATE_UINT32(resc
, ssys_state
),
654 VMSTATE_UINT32(rcc
, ssys_state
),
655 VMSTATE_UINT32_V(rcc2
, ssys_state
, 2),
656 VMSTATE_UINT32_ARRAY(rcgc
, ssys_state
, 3),
657 VMSTATE_UINT32_ARRAY(scgc
, ssys_state
, 3),
658 VMSTATE_UINT32_ARRAY(dcgc
, ssys_state
, 3),
659 VMSTATE_UINT32(clkvclr
, ssys_state
),
660 VMSTATE_UINT32(ldoarst
, ssys_state
),
661 VMSTATE_END_OF_LIST()
665 static int stellaris_sys_init(uint32_t base
, qemu_irq irq
,
666 stellaris_board_info
* board
,
671 s
= (ssys_state
*)g_malloc0(sizeof(ssys_state
));
674 /* Most devices come preprogrammed with a MAC address in the user data. */
675 s
->user0
= macaddr
[0] | (macaddr
[1] << 8) | (macaddr
[2] << 16);
676 s
->user1
= macaddr
[3] | (macaddr
[4] << 8) | (macaddr
[5] << 16);
678 memory_region_init_io(&s
->iomem
, NULL
, &ssys_ops
, s
, "ssys", 0x00001000);
679 memory_region_add_subregion(get_system_memory(), base
, &s
->iomem
);
681 vmstate_register(NULL
, -1, &vmstate_stellaris_sys
, s
);
686 /* I2C controller. */
688 #define TYPE_STELLARIS_I2C "stellaris-i2c"
689 #define STELLARIS_I2C(obj) \
690 OBJECT_CHECK(stellaris_i2c_state, (obj), TYPE_STELLARIS_I2C)
693 SysBusDevice parent_obj
;
705 } stellaris_i2c_state
;
707 #define STELLARIS_I2C_MCS_BUSY 0x01
708 #define STELLARIS_I2C_MCS_ERROR 0x02
709 #define STELLARIS_I2C_MCS_ADRACK 0x04
710 #define STELLARIS_I2C_MCS_DATACK 0x08
711 #define STELLARIS_I2C_MCS_ARBLST 0x10
712 #define STELLARIS_I2C_MCS_IDLE 0x20
713 #define STELLARIS_I2C_MCS_BUSBSY 0x40
715 static uint64_t stellaris_i2c_read(void *opaque
, hwaddr offset
,
718 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
724 /* We don't emulate timing, so the controller is never busy. */
725 return s
->mcs
| STELLARIS_I2C_MCS_IDLE
;
728 case 0x0c: /* MTPR */
730 case 0x10: /* MIMR */
732 case 0x14: /* MRIS */
734 case 0x18: /* MMIS */
735 return s
->mris
& s
->mimr
;
739 hw_error("strllaris_i2c_read: Bad offset 0x%x\n", (int)offset
);
744 static void stellaris_i2c_update(stellaris_i2c_state
*s
)
748 level
= (s
->mris
& s
->mimr
) != 0;
749 qemu_set_irq(s
->irq
, level
);
752 static void stellaris_i2c_write(void *opaque
, hwaddr offset
,
753 uint64_t value
, unsigned size
)
755 stellaris_i2c_state
*s
= (stellaris_i2c_state
*)opaque
;
759 s
->msa
= value
& 0xff;
762 if ((s
->mcr
& 0x10) == 0) {
763 /* Disabled. Do nothing. */
766 /* Grab the bus if this is starting a transfer. */
767 if ((value
& 2) && (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
768 if (i2c_start_transfer(s
->bus
, s
->msa
>> 1, s
->msa
& 1)) {
769 s
->mcs
|= STELLARIS_I2C_MCS_ARBLST
;
771 s
->mcs
&= ~STELLARIS_I2C_MCS_ARBLST
;
772 s
->mcs
|= STELLARIS_I2C_MCS_BUSBSY
;
775 /* If we don't have the bus then indicate an error. */
776 if (!i2c_bus_busy(s
->bus
)
777 || (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
) == 0) {
778 s
->mcs
|= STELLARIS_I2C_MCS_ERROR
;
781 s
->mcs
&= ~STELLARIS_I2C_MCS_ERROR
;
783 /* Transfer a byte. */
784 /* TODO: Handle errors. */
787 s
->mdr
= i2c_recv(s
->bus
) & 0xff;
790 i2c_send(s
->bus
, s
->mdr
);
792 /* Raise an interrupt. */
796 /* Finish transfer. */
797 i2c_end_transfer(s
->bus
);
798 s
->mcs
&= ~STELLARIS_I2C_MCS_BUSBSY
;
802 s
->mdr
= value
& 0xff;
804 case 0x0c: /* MTPR */
805 s
->mtpr
= value
& 0xff;
807 case 0x10: /* MIMR */
810 case 0x1c: /* MICR */
816 "stellaris_i2c_write: Loopback not implemented\n");
819 "stellaris_i2c_write: Slave mode not implemented\n");
820 s
->mcr
= value
& 0x31;
823 hw_error("stellaris_i2c_write: Bad offset 0x%x\n",
826 stellaris_i2c_update(s
);
829 static void stellaris_i2c_reset(stellaris_i2c_state
*s
)
831 if (s
->mcs
& STELLARIS_I2C_MCS_BUSBSY
)
832 i2c_end_transfer(s
->bus
);
841 stellaris_i2c_update(s
);
844 static const MemoryRegionOps stellaris_i2c_ops
= {
845 .read
= stellaris_i2c_read
,
846 .write
= stellaris_i2c_write
,
847 .endianness
= DEVICE_NATIVE_ENDIAN
,
850 static const VMStateDescription vmstate_stellaris_i2c
= {
851 .name
= "stellaris_i2c",
853 .minimum_version_id
= 1,
854 .minimum_version_id_old
= 1,
855 .fields
= (VMStateField
[]) {
856 VMSTATE_UINT32(msa
, stellaris_i2c_state
),
857 VMSTATE_UINT32(mcs
, stellaris_i2c_state
),
858 VMSTATE_UINT32(mdr
, stellaris_i2c_state
),
859 VMSTATE_UINT32(mtpr
, stellaris_i2c_state
),
860 VMSTATE_UINT32(mimr
, stellaris_i2c_state
),
861 VMSTATE_UINT32(mris
, stellaris_i2c_state
),
862 VMSTATE_UINT32(mcr
, stellaris_i2c_state
),
863 VMSTATE_END_OF_LIST()
867 static int stellaris_i2c_init(SysBusDevice
*sbd
)
869 DeviceState
*dev
= DEVICE(sbd
);
870 stellaris_i2c_state
*s
= STELLARIS_I2C(dev
);
873 sysbus_init_irq(sbd
, &s
->irq
);
874 bus
= i2c_init_bus(dev
, "i2c");
877 memory_region_init_io(&s
->iomem
, OBJECT(s
), &stellaris_i2c_ops
, s
,
879 sysbus_init_mmio(sbd
, &s
->iomem
);
880 /* ??? For now we only implement the master interface. */
881 stellaris_i2c_reset(s
);
882 vmstate_register(dev
, -1, &vmstate_stellaris_i2c
, s
);
886 /* Analogue to Digital Converter. This is only partially implemented,
887 enough for applications that use a combined ADC and timer tick. */
889 #define STELLARIS_ADC_EM_CONTROLLER 0
890 #define STELLARIS_ADC_EM_COMP 1
891 #define STELLARIS_ADC_EM_EXTERNAL 4
892 #define STELLARIS_ADC_EM_TIMER 5
893 #define STELLARIS_ADC_EM_PWM0 6
894 #define STELLARIS_ADC_EM_PWM1 7
895 #define STELLARIS_ADC_EM_PWM2 8
897 #define STELLARIS_ADC_FIFO_EMPTY 0x0100
898 #define STELLARIS_ADC_FIFO_FULL 0x1000
900 #define TYPE_STELLARIS_ADC "stellaris-adc"
901 #define STELLARIS_ADC(obj) \
902 OBJECT_CHECK(stellaris_adc_state, (obj), TYPE_STELLARIS_ADC)
904 typedef struct StellarisADCState
{
905 SysBusDevice parent_obj
;
924 } stellaris_adc_state
;
926 static uint32_t stellaris_adc_fifo_read(stellaris_adc_state
*s
, int n
)
930 tail
= s
->fifo
[n
].state
& 0xf;
931 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_EMPTY
) {
934 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf) | ((tail
+ 1) & 0xf);
935 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_FULL
;
936 if (tail
+ 1 == ((s
->fifo
[n
].state
>> 4) & 0xf))
937 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_EMPTY
;
939 return s
->fifo
[n
].data
[tail
];
942 static void stellaris_adc_fifo_write(stellaris_adc_state
*s
, int n
,
947 /* TODO: Real hardware has limited size FIFOs. We have a full 16 entry
948 FIFO fir each sequencer. */
949 head
= (s
->fifo
[n
].state
>> 4) & 0xf;
950 if (s
->fifo
[n
].state
& STELLARIS_ADC_FIFO_FULL
) {
954 s
->fifo
[n
].data
[head
] = value
;
955 head
= (head
+ 1) & 0xf;
956 s
->fifo
[n
].state
&= ~STELLARIS_ADC_FIFO_EMPTY
;
957 s
->fifo
[n
].state
= (s
->fifo
[n
].state
& ~0xf0) | (head
<< 4);
958 if ((s
->fifo
[n
].state
& 0xf) == head
)
959 s
->fifo
[n
].state
|= STELLARIS_ADC_FIFO_FULL
;
962 static void stellaris_adc_update(stellaris_adc_state
*s
)
967 for (n
= 0; n
< 4; n
++) {
968 level
= (s
->ris
& s
->im
& (1 << n
)) != 0;
969 qemu_set_irq(s
->irq
[n
], level
);
973 static void stellaris_adc_trigger(void *opaque
, int irq
, int level
)
975 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
978 for (n
= 0; n
< 4; n
++) {
979 if ((s
->actss
& (1 << n
)) == 0) {
983 if (((s
->emux
>> (n
* 4)) & 0xff) != 5) {
987 /* Some applications use the ADC as a random number source, so introduce
988 some variation into the signal. */
989 s
->noise
= s
->noise
* 314159 + 1;
990 /* ??? actual inputs not implemented. Return an arbitrary value. */
991 stellaris_adc_fifo_write(s
, n
, 0x200 + ((s
->noise
>> 16) & 7));
993 stellaris_adc_update(s
);
997 static void stellaris_adc_reset(stellaris_adc_state
*s
)
1001 for (n
= 0; n
< 4; n
++) {
1004 s
->fifo
[n
].state
= STELLARIS_ADC_FIFO_EMPTY
;
1008 static uint64_t stellaris_adc_read(void *opaque
, hwaddr offset
,
1011 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1013 /* TODO: Implement this. */
1014 if (offset
>= 0x40 && offset
< 0xc0) {
1016 n
= (offset
- 0x40) >> 5;
1017 switch (offset
& 0x1f) {
1018 case 0x00: /* SSMUX */
1020 case 0x04: /* SSCTL */
1022 case 0x08: /* SSFIFO */
1023 return stellaris_adc_fifo_read(s
, n
);
1024 case 0x0c: /* SSFSTAT */
1025 return s
->fifo
[n
].state
;
1031 case 0x00: /* ACTSS */
1033 case 0x04: /* RIS */
1037 case 0x0c: /* ISC */
1038 return s
->ris
& s
->im
;
1039 case 0x10: /* OSTAT */
1041 case 0x14: /* EMUX */
1043 case 0x18: /* USTAT */
1045 case 0x20: /* SSPRI */
1047 case 0x30: /* SAC */
1050 hw_error("strllaris_adc_read: Bad offset 0x%x\n",
1056 static void stellaris_adc_write(void *opaque
, hwaddr offset
,
1057 uint64_t value
, unsigned size
)
1059 stellaris_adc_state
*s
= (stellaris_adc_state
*)opaque
;
1061 /* TODO: Implement this. */
1062 if (offset
>= 0x40 && offset
< 0xc0) {
1064 n
= (offset
- 0x40) >> 5;
1065 switch (offset
& 0x1f) {
1066 case 0x00: /* SSMUX */
1067 s
->ssmux
[n
] = value
& 0x33333333;
1069 case 0x04: /* SSCTL */
1071 hw_error("ADC: Unimplemented sequence %" PRIx64
"\n",
1074 s
->ssctl
[n
] = value
;
1081 case 0x00: /* ACTSS */
1082 s
->actss
= value
& 0xf;
1087 case 0x0c: /* ISC */
1090 case 0x10: /* OSTAT */
1093 case 0x14: /* EMUX */
1096 case 0x18: /* USTAT */
1099 case 0x20: /* SSPRI */
1102 case 0x28: /* PSSI */
1103 hw_error("Not implemented: ADC sample initiate\n");
1105 case 0x30: /* SAC */
1109 hw_error("stellaris_adc_write: Bad offset 0x%x\n", (int)offset
);
1111 stellaris_adc_update(s
);
1114 static const MemoryRegionOps stellaris_adc_ops
= {
1115 .read
= stellaris_adc_read
,
1116 .write
= stellaris_adc_write
,
1117 .endianness
= DEVICE_NATIVE_ENDIAN
,
1120 static const VMStateDescription vmstate_stellaris_adc
= {
1121 .name
= "stellaris_adc",
1123 .minimum_version_id
= 1,
1124 .minimum_version_id_old
= 1,
1125 .fields
= (VMStateField
[]) {
1126 VMSTATE_UINT32(actss
, stellaris_adc_state
),
1127 VMSTATE_UINT32(ris
, stellaris_adc_state
),
1128 VMSTATE_UINT32(im
, stellaris_adc_state
),
1129 VMSTATE_UINT32(emux
, stellaris_adc_state
),
1130 VMSTATE_UINT32(ostat
, stellaris_adc_state
),
1131 VMSTATE_UINT32(ustat
, stellaris_adc_state
),
1132 VMSTATE_UINT32(sspri
, stellaris_adc_state
),
1133 VMSTATE_UINT32(sac
, stellaris_adc_state
),
1134 VMSTATE_UINT32(fifo
[0].state
, stellaris_adc_state
),
1135 VMSTATE_UINT32_ARRAY(fifo
[0].data
, stellaris_adc_state
, 16),
1136 VMSTATE_UINT32(ssmux
[0], stellaris_adc_state
),
1137 VMSTATE_UINT32(ssctl
[0], stellaris_adc_state
),
1138 VMSTATE_UINT32(fifo
[1].state
, stellaris_adc_state
),
1139 VMSTATE_UINT32_ARRAY(fifo
[1].data
, stellaris_adc_state
, 16),
1140 VMSTATE_UINT32(ssmux
[1], stellaris_adc_state
),
1141 VMSTATE_UINT32(ssctl
[1], stellaris_adc_state
),
1142 VMSTATE_UINT32(fifo
[2].state
, stellaris_adc_state
),
1143 VMSTATE_UINT32_ARRAY(fifo
[2].data
, stellaris_adc_state
, 16),
1144 VMSTATE_UINT32(ssmux
[2], stellaris_adc_state
),
1145 VMSTATE_UINT32(ssctl
[2], stellaris_adc_state
),
1146 VMSTATE_UINT32(fifo
[3].state
, stellaris_adc_state
),
1147 VMSTATE_UINT32_ARRAY(fifo
[3].data
, stellaris_adc_state
, 16),
1148 VMSTATE_UINT32(ssmux
[3], stellaris_adc_state
),
1149 VMSTATE_UINT32(ssctl
[3], stellaris_adc_state
),
1150 VMSTATE_UINT32(noise
, stellaris_adc_state
),
1151 VMSTATE_END_OF_LIST()
1155 static int stellaris_adc_init(SysBusDevice
*sbd
)
1157 DeviceState
*dev
= DEVICE(sbd
);
1158 stellaris_adc_state
*s
= STELLARIS_ADC(dev
);
1161 for (n
= 0; n
< 4; n
++) {
1162 sysbus_init_irq(sbd
, &s
->irq
[n
]);
1165 memory_region_init_io(&s
->iomem
, OBJECT(s
), &stellaris_adc_ops
, s
,
1167 sysbus_init_mmio(sbd
, &s
->iomem
);
1168 stellaris_adc_reset(s
);
1169 qdev_init_gpio_in(dev
, stellaris_adc_trigger
, 1);
1170 vmstate_register(dev
, -1, &vmstate_stellaris_adc
, s
);
1175 static stellaris_board_info stellaris_boards
[] = {
1179 0x001f001f, /* dc0 */
1189 0x00ff007f, /* dc0 */
1194 BP_OLED_SSI
| BP_GAMEPAD
1198 static void stellaris_init(const char *kernel_filename
, const char *cpu_model
,
1199 stellaris_board_info
*board
)
1201 static const int uart_irq
[] = {5, 6, 33, 34};
1202 static const int timer_irq
[] = {19, 21, 23, 35};
1203 static const uint32_t gpio_addr
[7] =
1204 { 0x40004000, 0x40005000, 0x40006000, 0x40007000,
1205 0x40024000, 0x40025000, 0x40026000};
1206 static const int gpio_irq
[7] = {0, 1, 2, 3, 4, 30, 31};
1208 MemoryRegion
*address_space_mem
= get_system_memory();
1210 DeviceState
*gpio_dev
[7];
1211 qemu_irq gpio_in
[7][8];
1212 qemu_irq gpio_out
[7][8];
1221 flash_size
= ((board
->dc0
& 0xffff) + 1) << 1;
1222 sram_size
= (board
->dc0
>> 18) + 1;
1223 pic
= armv7m_init(address_space_mem
,
1224 flash_size
, sram_size
, kernel_filename
, cpu_model
);
1226 if (board
->dc1
& (1 << 16)) {
1227 dev
= sysbus_create_varargs(TYPE_STELLARIS_ADC
, 0x40038000,
1228 pic
[14], pic
[15], pic
[16], pic
[17], NULL
);
1229 adc
= qdev_get_gpio_in(dev
, 0);
1233 for (i
= 0; i
< 4; i
++) {
1234 if (board
->dc2
& (0x10000 << i
)) {
1235 dev
= sysbus_create_simple(TYPE_STELLARIS_GPTM
,
1236 0x40030000 + i
* 0x1000,
1238 /* TODO: This is incorrect, but we get away with it because
1239 the ADC output is only ever pulsed. */
1240 qdev_connect_gpio_out(dev
, 0, adc
);
1244 stellaris_sys_init(0x400fe000, pic
[28], board
, nd_table
[0].macaddr
.a
);
1246 for (i
= 0; i
< 7; i
++) {
1247 if (board
->dc4
& (1 << i
)) {
1248 gpio_dev
[i
] = sysbus_create_simple("pl061_luminary", gpio_addr
[i
],
1250 for (j
= 0; j
< 8; j
++) {
1251 gpio_in
[i
][j
] = qdev_get_gpio_in(gpio_dev
[i
], j
);
1252 gpio_out
[i
][j
] = NULL
;
1257 if (board
->dc2
& (1 << 12)) {
1258 dev
= sysbus_create_simple(TYPE_STELLARIS_I2C
, 0x40020000, pic
[8]);
1259 i2c
= (I2CBus
*)qdev_get_child_bus(dev
, "i2c");
1260 if (board
->peripherals
& BP_OLED_I2C
) {
1261 i2c_create_slave(i2c
, "ssd0303", 0x3d);
1265 for (i
= 0; i
< 4; i
++) {
1266 if (board
->dc2
& (1 << i
)) {
1267 sysbus_create_simple("pl011_luminary", 0x4000c000 + i
* 0x1000,
1271 if (board
->dc2
& (1 << 4)) {
1272 dev
= sysbus_create_simple("pl022", 0x40008000, pic
[7]);
1273 if (board
->peripherals
& BP_OLED_SSI
) {
1276 DeviceState
*ssddev
;
1278 /* Some boards have both an OLED controller and SD card connected to
1279 * the same SSI port, with the SD card chip select connected to a
1280 * GPIO pin. Technically the OLED chip select is connected to the
1281 * SSI Fss pin. We do not bother emulating that as both devices
1282 * should never be selected simultaneously, and our OLED controller
1283 * ignores stray 0xff commands that occur when deselecting the SD
1286 bus
= qdev_get_child_bus(dev
, "ssi");
1288 sddev
= ssi_create_slave(bus
, "ssi-sd");
1289 ssddev
= ssi_create_slave(bus
, "ssd0323");
1290 gpio_out
[GPIO_D
][0] = qemu_irq_split(qdev_get_gpio_in(sddev
, 0),
1291 qdev_get_gpio_in(ssddev
, 0));
1292 gpio_out
[GPIO_C
][7] = qdev_get_gpio_in(ssddev
, 1);
1294 /* Make sure the select pin is high. */
1295 qemu_irq_raise(gpio_out
[GPIO_D
][0]);
1298 if (board
->dc4
& (1 << 28)) {
1301 qemu_check_nic_model(&nd_table
[0], "stellaris");
1303 enet
= qdev_create(NULL
, "stellaris_enet");
1304 qdev_set_nic_properties(enet
, &nd_table
[0]);
1305 qdev_init_nofail(enet
);
1306 sysbus_mmio_map(SYS_BUS_DEVICE(enet
), 0, 0x40048000);
1307 sysbus_connect_irq(SYS_BUS_DEVICE(enet
), 0, pic
[42]);
1309 if (board
->peripherals
& BP_GAMEPAD
) {
1310 qemu_irq gpad_irq
[5];
1311 static const int gpad_keycode
[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
1313 gpad_irq
[0] = qemu_irq_invert(gpio_in
[GPIO_E
][0]); /* up */
1314 gpad_irq
[1] = qemu_irq_invert(gpio_in
[GPIO_E
][1]); /* down */
1315 gpad_irq
[2] = qemu_irq_invert(gpio_in
[GPIO_E
][2]); /* left */
1316 gpad_irq
[3] = qemu_irq_invert(gpio_in
[GPIO_E
][3]); /* right */
1317 gpad_irq
[4] = qemu_irq_invert(gpio_in
[GPIO_F
][1]); /* select */
1319 stellaris_gamepad_init(5, gpad_irq
, gpad_keycode
);
1321 for (i
= 0; i
< 7; i
++) {
1322 if (board
->dc4
& (1 << i
)) {
1323 for (j
= 0; j
< 8; j
++) {
1324 if (gpio_out
[i
][j
]) {
1325 qdev_connect_gpio_out(gpio_dev
[i
], j
, gpio_out
[i
][j
]);
1332 /* FIXME: Figure out how to generate these from stellaris_boards. */
1333 static void lm3s811evb_init(QEMUMachineInitArgs
*args
)
1335 const char *cpu_model
= args
->cpu_model
;
1336 const char *kernel_filename
= args
->kernel_filename
;
1337 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[0]);
1340 static void lm3s6965evb_init(QEMUMachineInitArgs
*args
)
1342 const char *cpu_model
= args
->cpu_model
;
1343 const char *kernel_filename
= args
->kernel_filename
;
1344 stellaris_init(kernel_filename
, cpu_model
, &stellaris_boards
[1]);
1347 static QEMUMachine lm3s811evb_machine
= {
1348 .name
= "lm3s811evb",
1349 .desc
= "Stellaris LM3S811EVB",
1350 .init
= lm3s811evb_init
,
1353 static QEMUMachine lm3s6965evb_machine
= {
1354 .name
= "lm3s6965evb",
1355 .desc
= "Stellaris LM3S6965EVB",
1356 .init
= lm3s6965evb_init
,
1359 static void stellaris_machine_init(void)
1361 qemu_register_machine(&lm3s811evb_machine
);
1362 qemu_register_machine(&lm3s6965evb_machine
);
1365 machine_init(stellaris_machine_init
);
1367 static void stellaris_i2c_class_init(ObjectClass
*klass
, void *data
)
1369 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1371 sdc
->init
= stellaris_i2c_init
;
1374 static const TypeInfo stellaris_i2c_info
= {
1375 .name
= TYPE_STELLARIS_I2C
,
1376 .parent
= TYPE_SYS_BUS_DEVICE
,
1377 .instance_size
= sizeof(stellaris_i2c_state
),
1378 .class_init
= stellaris_i2c_class_init
,
1381 static void stellaris_gptm_class_init(ObjectClass
*klass
, void *data
)
1383 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1385 sdc
->init
= stellaris_gptm_init
;
1388 static const TypeInfo stellaris_gptm_info
= {
1389 .name
= TYPE_STELLARIS_GPTM
,
1390 .parent
= TYPE_SYS_BUS_DEVICE
,
1391 .instance_size
= sizeof(gptm_state
),
1392 .class_init
= stellaris_gptm_class_init
,
1395 static void stellaris_adc_class_init(ObjectClass
*klass
, void *data
)
1397 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
1399 sdc
->init
= stellaris_adc_init
;
1402 static const TypeInfo stellaris_adc_info
= {
1403 .name
= TYPE_STELLARIS_ADC
,
1404 .parent
= TYPE_SYS_BUS_DEVICE
,
1405 .instance_size
= sizeof(stellaris_adc_state
),
1406 .class_init
= stellaris_adc_class_init
,
1409 static void stellaris_register_types(void)
1411 type_register_static(&stellaris_i2c_info
);
1412 type_register_static(&stellaris_gptm_info
);
1413 type_register_static(&stellaris_adc_info
);
1416 type_init(stellaris_register_types
)