xref: /openbmc/qemu/hw/arm/tosa.c (revision c63ca4ff)
1 /* vim:set shiftwidth=4 ts=4 et: */
2 /*
3  * PXA255 Sharp Zaurus SL-6000 PDA platform
4  *
5  * Copyright (c) 2008 Dmitry Baryshkov
6  *
7  * Code based on spitz platform by Andrzej Zaborowski <balrog@zabor.org>
8  * This code is licensed under the GNU GPL v2.
9  *
10  * Contributions after 2012-01-13 are licensed under the terms of the
11  * GNU GPL, version 2 or (at your option) any later version.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "sysemu/runstate.h"
17 #include "hw/arm/pxa.h"
18 #include "hw/arm/boot.h"
19 #include "hw/arm/sharpsl.h"
20 #include "hw/pcmcia.h"
21 #include "hw/boards.h"
22 #include "hw/display/tc6393xb.h"
23 #include "hw/i2c/i2c.h"
24 #include "hw/irq.h"
25 #include "hw/ssi/ssi.h"
26 #include "hw/sysbus.h"
27 #include "hw/misc/led.h"
28 #include "exec/address-spaces.h"
29 #include "qom/object.h"
30 
31 #define TOSA_RAM 0x04000000
32 #define TOSA_ROM 0x00800000
33 
34 #define TOSA_GPIO_USB_IN                (5)
35 #define TOSA_GPIO_nSD_DETECT            (9)
36 #define TOSA_GPIO_ON_RESET              (19)
37 #define TOSA_GPIO_CF_IRQ                (21)    /* CF slot0 Ready */
38 #define TOSA_GPIO_CF_CD                 (13)
39 #define TOSA_GPIO_TC6393XB_INT          (15)
40 #define TOSA_GPIO_JC_CF_IRQ             (36)    /* CF slot1 Ready */
41 
42 #define TOSA_SCOOP_GPIO_BASE            1
43 #define TOSA_GPIO_IR_POWERDWN           (TOSA_SCOOP_GPIO_BASE + 2)
44 #define TOSA_GPIO_SD_WP                 (TOSA_SCOOP_GPIO_BASE + 3)
45 #define TOSA_GPIO_PWR_ON                (TOSA_SCOOP_GPIO_BASE + 4)
46 
47 #define TOSA_SCOOP_JC_GPIO_BASE         1
48 #define TOSA_GPIO_BT_LED                (TOSA_SCOOP_JC_GPIO_BASE + 0)
49 #define TOSA_GPIO_NOTE_LED              (TOSA_SCOOP_JC_GPIO_BASE + 1)
50 #define TOSA_GPIO_CHRG_ERR_LED          (TOSA_SCOOP_JC_GPIO_BASE + 2)
51 #define TOSA_GPIO_TC6393XB_L3V_ON       (TOSA_SCOOP_JC_GPIO_BASE + 5)
52 #define TOSA_GPIO_WLAN_LED              (TOSA_SCOOP_JC_GPIO_BASE + 7)
53 
54 #define DAC_BASE 0x4e
55 #define DAC_CH1 0
56 #define DAC_CH2 1
57 
58 static void tosa_microdrive_attach(PXA2xxState *cpu)
59 {
60     PCMCIACardState *md;
61     DriveInfo *dinfo;
62 
63     dinfo = drive_get(IF_IDE, 0, 0);
64     if (!dinfo || dinfo->media_cd)
65         return;
66     md = dscm1xxxx_init(dinfo);
67     pxa2xx_pcmcia_attach(cpu->pcmcia[0], md);
68 }
69 
70 /*
71  * Encapsulation of some GPIO line behaviour for the Tosa board
72  *
73  * QEMU interface:
74  *  + named GPIO inputs "leds[0..3]": assert to light LEDs
75  *  + named GPIO input "reset": when asserted, resets the system
76  */
77 
78 #define TYPE_TOSA_MISC_GPIO "tosa-misc-gpio"
79 OBJECT_DECLARE_SIMPLE_TYPE(TosaMiscGPIOState, TOSA_MISC_GPIO)
80 
81 struct TosaMiscGPIOState {
82     SysBusDevice parent_obj;
83 };
84 
85 static void tosa_reset(void *opaque, int line, int level)
86 {
87     if (level) {
88         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
89     }
90 }
91 
92 static void tosa_misc_gpio_init(Object *obj)
93 {
94     DeviceState *dev = DEVICE(obj);
95 
96     qdev_init_gpio_in_named(dev, tosa_reset, "reset", 1);
97 }
98 
99 static void tosa_gpio_setup(PXA2xxState *cpu,
100                 DeviceState *scp0,
101                 DeviceState *scp1,
102                 TC6393xbState *tmio)
103 {
104     DeviceState *misc_gpio;
105     LEDState *led[4];
106 
107     misc_gpio = sysbus_create_simple(TYPE_TOSA_MISC_GPIO, -1, NULL);
108 
109     /* MMC/SD host */
110     pxa2xx_mmci_handlers(cpu->mmc,
111                     qdev_get_gpio_in(scp0, TOSA_GPIO_SD_WP),
112                     qemu_irq_invert(qdev_get_gpio_in(cpu->gpio, TOSA_GPIO_nSD_DETECT)));
113 
114     /* Handle reset */
115     qdev_connect_gpio_out(cpu->gpio, TOSA_GPIO_ON_RESET,
116                           qdev_get_gpio_in_named(misc_gpio, "reset", 0));
117 
118     /* PCMCIA signals: card's IRQ and Card-Detect */
119     pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[0],
120                         qdev_get_gpio_in(cpu->gpio, TOSA_GPIO_CF_IRQ),
121                         qdev_get_gpio_in(cpu->gpio, TOSA_GPIO_CF_CD));
122 
123     pxa2xx_pcmcia_set_irq_cb(cpu->pcmcia[1],
124                         qdev_get_gpio_in(cpu->gpio, TOSA_GPIO_JC_CF_IRQ),
125                         NULL);
126 
127     led[0] = led_create_simple(OBJECT(misc_gpio), GPIO_POLARITY_ACTIVE_HIGH,
128                                LED_COLOR_BLUE, "bluetooth");
129     led[1] = led_create_simple(OBJECT(misc_gpio), GPIO_POLARITY_ACTIVE_HIGH,
130                                LED_COLOR_GREEN, "note");
131     led[2] = led_create_simple(OBJECT(misc_gpio), GPIO_POLARITY_ACTIVE_HIGH,
132                                LED_COLOR_AMBER, "charger-error");
133     led[3] = led_create_simple(OBJECT(misc_gpio), GPIO_POLARITY_ACTIVE_HIGH,
134                                LED_COLOR_GREEN, "wlan");
135 
136     qdev_connect_gpio_out(scp1, TOSA_GPIO_BT_LED,
137                           qdev_get_gpio_in(DEVICE(led[0]), 0));
138     qdev_connect_gpio_out(scp1, TOSA_GPIO_NOTE_LED,
139                           qdev_get_gpio_in(DEVICE(led[1]), 0));
140     qdev_connect_gpio_out(scp1, TOSA_GPIO_CHRG_ERR_LED,
141                           qdev_get_gpio_in(DEVICE(led[2]), 0));
142     qdev_connect_gpio_out(scp1, TOSA_GPIO_WLAN_LED,
143                           qdev_get_gpio_in(DEVICE(led[3]), 0));
144 
145     qdev_connect_gpio_out(scp1, TOSA_GPIO_TC6393XB_L3V_ON, tc6393xb_l3v_get(tmio));
146 
147     /* UDC Vbus */
148     qemu_irq_raise(qdev_get_gpio_in(cpu->gpio, TOSA_GPIO_USB_IN));
149 }
150 
151 static uint32_t tosa_ssp_tansfer(SSIPeripheral *dev, uint32_t value)
152 {
153     fprintf(stderr, "TG: %u %02x\n", value >> 5, value & 0x1f);
154     return 0;
155 }
156 
157 static void tosa_ssp_realize(SSIPeripheral *dev, Error **errp)
158 {
159     /* Nothing to do.  */
160 }
161 
162 #define TYPE_TOSA_DAC "tosa_dac"
163 OBJECT_DECLARE_SIMPLE_TYPE(TosaDACState, TOSA_DAC)
164 
165 struct TosaDACState {
166     I2CSlave parent_obj;
167 
168     int len;
169     char buf[3];
170 };
171 
172 static int tosa_dac_send(I2CSlave *i2c, uint8_t data)
173 {
174     TosaDACState *s = TOSA_DAC(i2c);
175 
176     s->buf[s->len] = data;
177     if (s->len ++ > 2) {
178 #ifdef VERBOSE
179         fprintf(stderr, "%s: message too long (%i bytes)\n", __func__, s->len);
180 #endif
181         return 1;
182     }
183 
184     if (s->len == 2) {
185         fprintf(stderr, "dac: channel %d value 0x%02x\n",
186                 s->buf[0], s->buf[1]);
187     }
188 
189     return 0;
190 }
191 
192 static int tosa_dac_event(I2CSlave *i2c, enum i2c_event event)
193 {
194     TosaDACState *s = TOSA_DAC(i2c);
195 
196     s->len = 0;
197     switch (event) {
198     case I2C_START_SEND:
199         break;
200     case I2C_START_RECV:
201         printf("%s: recv not supported!!!\n", __func__);
202         break;
203     case I2C_FINISH:
204 #ifdef VERBOSE
205         if (s->len < 2)
206             printf("%s: message too short (%i bytes)\n", __func__, s->len);
207         if (s->len > 2)
208             printf("%s: message too long\n", __func__);
209 #endif
210         break;
211     default:
212         break;
213     }
214 
215     return 0;
216 }
217 
218 static uint8_t tosa_dac_recv(I2CSlave *s)
219 {
220     printf("%s: recv not supported!!!\n", __func__);
221     return 0xff;
222 }
223 
224 static void tosa_tg_init(PXA2xxState *cpu)
225 {
226     I2CBus *bus = pxa2xx_i2c_bus(cpu->i2c[0]);
227     i2c_slave_create_simple(bus, TYPE_TOSA_DAC, DAC_BASE);
228     ssi_create_peripheral(cpu->ssp[1], "tosa-ssp");
229 }
230 
231 
232 static struct arm_boot_info tosa_binfo = {
233     .loader_start = PXA2XX_SDRAM_BASE,
234     .ram_size = 0x04000000,
235 };
236 
237 static void tosa_init(MachineState *machine)
238 {
239     MemoryRegion *address_space_mem = get_system_memory();
240     MemoryRegion *rom = g_new(MemoryRegion, 1);
241     PXA2xxState *mpu;
242     TC6393xbState *tmio;
243     DeviceState *scp0, *scp1;
244 
245     mpu = pxa255_init(address_space_mem, tosa_binfo.ram_size);
246 
247     memory_region_init_rom(rom, NULL, "tosa.rom", TOSA_ROM, &error_fatal);
248     memory_region_add_subregion(address_space_mem, 0, rom);
249 
250     tmio = tc6393xb_init(address_space_mem, 0x10000000,
251             qdev_get_gpio_in(mpu->gpio, TOSA_GPIO_TC6393XB_INT));
252 
253     scp0 = sysbus_create_simple("scoop", 0x08800000, NULL);
254     scp1 = sysbus_create_simple("scoop", 0x14800040, NULL);
255 
256     tosa_gpio_setup(mpu, scp0, scp1, tmio);
257 
258     tosa_microdrive_attach(mpu);
259 
260     tosa_tg_init(mpu);
261 
262     tosa_binfo.board_id = 0x208;
263     arm_load_kernel(mpu->cpu, machine, &tosa_binfo);
264     sl_bootparam_write(SL_PXA_PARAM_BASE);
265 }
266 
267 static void tosapda_machine_init(MachineClass *mc)
268 {
269     mc->desc = "Sharp SL-6000 (Tosa) PDA (PXA255)";
270     mc->init = tosa_init;
271     mc->block_default_type = IF_IDE;
272     mc->ignore_memory_transaction_failures = true;
273 }
274 
275 DEFINE_MACHINE("tosa", tosapda_machine_init)
276 
277 static void tosa_dac_class_init(ObjectClass *klass, void *data)
278 {
279     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
280 
281     k->event = tosa_dac_event;
282     k->recv = tosa_dac_recv;
283     k->send = tosa_dac_send;
284 }
285 
286 static const TypeInfo tosa_dac_info = {
287     .name          = TYPE_TOSA_DAC,
288     .parent        = TYPE_I2C_SLAVE,
289     .instance_size = sizeof(TosaDACState),
290     .class_init    = tosa_dac_class_init,
291 };
292 
293 static void tosa_ssp_class_init(ObjectClass *klass, void *data)
294 {
295     SSIPeripheralClass *k = SSI_PERIPHERAL_CLASS(klass);
296 
297     k->realize = tosa_ssp_realize;
298     k->transfer = tosa_ssp_tansfer;
299 }
300 
301 static const TypeInfo tosa_ssp_info = {
302     .name          = "tosa-ssp",
303     .parent        = TYPE_SSI_PERIPHERAL,
304     .instance_size = sizeof(SSIPeripheral),
305     .class_init    = tosa_ssp_class_init,
306 };
307 
308 static const TypeInfo tosa_misc_gpio_info = {
309     .name          = TYPE_TOSA_MISC_GPIO,
310     .parent        = TYPE_SYS_BUS_DEVICE,
311     .instance_size = sizeof(TosaMiscGPIOState),
312     .instance_init = tosa_misc_gpio_init,
313     /*
314      * No class init required: device has no internal state so does not
315      * need to set up reset or vmstate, and has no realize method.
316      */
317 };
318 
319 static void tosa_register_types(void)
320 {
321     type_register_static(&tosa_dac_info);
322     type_register_static(&tosa_ssp_info);
323     type_register_static(&tosa_misc_gpio_info);
324 }
325 
326 type_init(tosa_register_types)
327