780f4ffae166e4b0a627e4d26081bbb277085fef
[qemu/mini2440.git] / hw / mini2440.c
blob780f4ffae166e4b0a627e4d26081bbb277085fef
1 /*
2 * mini2440 development board support
4 * Copyright Michel Pollet <buserror@gmail.com>
6 * This code is licensed under the GNU GPL v2.
7 */
9 #include "hw.h"
10 #include "s3c.h"
11 #include "arm-misc.h"
12 #include "sysemu.h"
13 #include "i2c.h"
14 #include "qemu-timer.h"
15 #include "devices.h"
16 #include "audio/audio.h"
17 #include "boards.h"
18 #include "console.h"
19 #include "usb.h"
20 #include "net.h"
21 #include "sd.h"
22 #include "dm9000.h"
23 #include "eeprom24c0x.h"
25 #define mini2440_printf(format, ...) \
26 fprintf(stderr, "QEMU %s: " format, __FUNCTION__, ##__VA_ARGS__)
28 #define MINI2440_GPIO_BACKLIGHT S3C_GPG(4)
29 #define MINI2440_GPIO_LCD_RESET S3C_GPC(6)
30 #define MINI2440_GPIO_nSD_DETECT S3C_GPG(8)
31 #define MINI2440_GPIO_WP_SD S3C_GPH(8)
32 #define MINI2440_GPIO_DM9000 S3C_GPF(7)
33 #define MINI2440_GPIO_USB_PULLUP S3C_GPC(5)
35 #define MINI2440_IRQ_nSD_DETECT S3C_EINT(16)
36 #define MINI2440_IRQ_DM9000 S3C_EINT(7)
38 #define BOOT_NONE 0
39 #define BOOT_NOR 1
40 #define BOOT_NAND 2
42 struct mini2440_board_s {
43 struct s3c_state_s *cpu;
44 unsigned int ram;
45 struct ee24c08_s * eeprom;
46 const char * kernel;
47 SDState * mmc;
48 NANDFlashState *nand;
49 int bl_level;
50 int boot_mode;
54 * the 24c08 sits on 4 addresses on the bus, and uses the lower address bits
55 * to address the 256 byte "page" of the eeprom. We thus need to use 4 i2c_slaves
56 * and keep track of which one was used to set the read/write pointer into the data
58 struct ee24c08_s;
59 typedef struct ee24cxx_page_s {
60 i2c_slave i2c;
61 struct ee24c08_s * eeprom;
62 uint8_t page;
63 } ee24cxx_page_s;
64 typedef struct ee24c08_s {
65 /* that memory takes 4 addresses */
66 i2c_slave * slave[4];
67 uint16_t ptr;
68 uint16_t count;
69 uint8_t data[1024];
70 } ee24c08;
72 static void ee24c08_event(i2c_slave *i2c, enum i2c_event event)
74 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
76 if (!s->eeprom)
77 return;
79 s->eeprom->ptr = s->page * 256;
80 s->eeprom->count = 0;
83 static int ee24c08_tx(i2c_slave *i2c, uint8_t data)
85 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
87 if (!s->eeprom)
88 return 0;
89 if (s->eeprom->count++ == 0) {
90 /* first byte is address offset */
91 s->eeprom->ptr = (s->page * 256) + data;
92 } else {
93 mini2440_printf("write %04x=%02x\n", s->eeprom->ptr, data);
94 s->eeprom->data[s->eeprom->ptr] = data;
95 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
96 s->eeprom->count++;
98 return 0;
101 static int ee24c08_rx(i2c_slave *i2c)
103 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
104 uint8_t res;
105 if (!s->eeprom)
106 return 0;
108 res = s->eeprom->data[s->eeprom->ptr];
110 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
111 s->eeprom->count++;
112 return res;
115 static void ee24c08_save(QEMUFile *f, void *opaque)
117 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
118 int i;
120 qemu_put_be16s(f, &s->ptr);
121 qemu_put_be16s(f, &s->count);
122 qemu_put_buffer(f, s->data, sizeof(s->data));
124 for (i = 0; i < 4; i++)
125 i2c_slave_save(f, s->slave[i]);
128 static int ee24c08_load(QEMUFile *f, void *opaque, int version_id)
130 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
131 int i;
133 qemu_get_be16s(f, &s->ptr);
134 qemu_get_be16s(f, &s->count);
135 qemu_get_buffer(f, s->data, sizeof(s->data));
137 for (i = 0; i < 4; i++)
138 i2c_slave_load(f, s->slave[i]);
139 return 0;
142 static void ee24c08_page_init(i2c_slave *i2c)
144 /* nothing to do here */
147 static I2CSlaveInfo ee24c08_info = {
148 .init = ee24c08_page_init,
149 .event = ee24c08_event,
150 .recv = ee24c08_rx,
151 .send = ee24c08_tx
154 static void ee24c08_register_devices(void)
156 i2c_register_slave("24C08", sizeof(ee24cxx_page_s), &ee24c08_info);
159 device_init(ee24c08_register_devices);
161 static ee24c08 * ee24c08_init(i2c_bus * bus)
163 ee24c08 *s = qemu_malloc(sizeof(ee24c08));
164 int i = 0;
166 printf("QEMU: %s\n", __FUNCTION__);
168 memset(s->data, 0xff, sizeof(s->data));
170 for (i = 0; i < 4; i++) {
171 DeviceState *dev = i2c_create_slave(bus, "24C08", 0x50 + i);
172 if (!dev) {
173 mini2440_printf("failed address %02x\n", 0x50+i);
175 s->slave[i] = I2C_SLAVE_FROM_QDEV(dev);
176 ee24cxx_page_s *ss = FROM_I2C_SLAVE(ee24cxx_page_s, s->slave[i]);
177 ss->page = i;
178 ss->eeprom = s;
180 register_savevm("ee24c08", -1, 0, ee24c08_save, ee24c08_load, s);
181 return s;
184 /* Handlers for output ports */
185 static void mini2440_bl_switch(void *opaque, int line, int level)
187 printf("QEMU: %s: LCD Backlight now %s (%d).\n", __FUNCTION__, level ? "on" : "off", level);
190 static void mini2440_bl_intensity(int line, int level, void *opaque)
192 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
194 if ((level >> 8) != s->bl_level) {
195 s->bl_level = level >> 8;
196 printf("%s: LCD Backlight now at %04x\n", __FUNCTION__, level);
200 static void mini2440_gpio_setup(struct mini2440_board_s *s)
202 /* set the "input" pin values */
203 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(13), 1);
204 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(14), 1);
205 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(15), 0);
207 s3c_gpio_out_set(s->cpu->io, MINI2440_GPIO_BACKLIGHT,
208 *qemu_allocate_irqs(mini2440_bl_switch, s, 1));
210 s3c_timers_cmp_handler_set(s->cpu->timers, 1, mini2440_bl_intensity, s);
212 /* Register the SD card pins to the lower SD driver */
213 sd_set_cb(s->mmc,
214 s3c_gpio_in_get(s->cpu->io)[MINI2440_GPIO_WP_SD],
215 qemu_irq_invert(s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_nSD_DETECT]));
219 #if 0
220 static void hexdump(const void* address, uint32_t len)
222 const unsigned char* p = address;
223 int i, j;
225 for (i = 0; i < len; i += 16) {
226 for (j = 0; j < 16 && i + j < len; j++)
227 fprintf(stderr, "%02x ", p[i + j]);
228 for (; j < 16; j++)
229 fprintf(stderr, " ");
230 fprintf(stderr, " ");
231 for (j = 0; j < 16 && i + j < len; j++)
232 fprintf(stderr, "%c", (p[i + j] < ' ' || p[i + j] > 0x7f) ? '.' : p[i + j]);
233 fprintf(stderr, "\n");
236 #endif
238 static int mini2440_load_from_nand(NANDFlashState *nand,
239 uint32_t nand_offset, uint32_t s3c_base_offset, uint32_t size)
241 uint8_t buffer[512];
242 uint32_t src = 0;
243 int page = 0;
244 uint32_t dst = 0;
246 if (!nand)
247 return 0;
249 for (page = 0; page < (size / 512); page++, src += 512 + 16, dst += 512) {
250 if (nand_readraw(nand, nand_offset + src, buffer, 512)) {
251 cpu_physical_memory_write(s3c_base_offset + dst, buffer, 512);
252 } else {
253 mini2440_printf("failed to load nand %d:%d\n",
254 nand_offset + src, 512 + 16);
255 return 0;
258 return (int) size;
261 static void mini2440_reset(void *opaque)
263 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
264 int32_t image_size;
266 s->cpu->env->regs[15] = 0;
268 if (s->boot_mode == BOOT_NAND) {
270 * Normally we would load 4 KB of nand to SRAM and jump there, but
271 * it is not working perfectly as expected, so we cheat and load
272 * it from nand directly relocated to 0x33f80000 and jump there
274 if (mini2440_load_from_nand(s->nand, 0, S3C_RAM_BASE | 0x03f80000, 256*1024)> 0) {
275 mini2440_printf("loaded default u-boot from NAND\n");
276 s->cpu->env->regs[15] = S3C_RAM_BASE | 0x03f80000; /* start address, u-boot already relocated */
278 #if 0 && defined(LATER)
279 if (mini2440_load_from_nand(s->nand, 0, S3C_SRAM_BASE_NANDBOOT, S3C_SRAM_SIZE) > 0) {
280 s->cpu->env->regs[15] = S3C_SRAM_BASE_NANDBOOT; /* start address, u-boot relocating code */
281 mini2440_printf("4KB SteppingStone loaded from NAND\n");
283 #endif
286 * if a u--boot is available as a file, we always use it
289 image_size = load_image("mini2440/u-boot.bin", qemu_get_ram_ptr(0x03f80000));
290 if (image_size < 0)
291 image_size = load_image("u-boot.bin", qemu_get_ram_ptr(0x03f80000));
292 if (image_size > 0) {
293 if (image_size & (512 -1)) /* round size to a NAND block size */
294 image_size = (image_size + 512) & ~(512-1);
295 mini2440_printf("loaded override u-boot (size %x)\n", image_size);
296 s->cpu->env->regs[15] = S3C_RAM_BASE | 0x03f80000; /* start address, u-boot already relocated */
301 * if a kernel was explicitly specified, we load it too
303 if (s->kernel) {
304 image_size = load_image(s->kernel, qemu_get_ram_ptr(0x02000000));
305 if (image_size > 0) {
306 if (image_size & (512 -1)) /* round size to a NAND block size */
307 image_size = (image_size + 512) & ~(512-1);
308 mini2440_printf("loaded %s (size %x)\n", s->kernel, image_size);
314 /* Typical touchscreen calibration values */
315 static const int mini2440_ts_scale[6] = {
316 0, (90 - 960) * 256 / 1021, -90 * 256 * 32,
317 (940 - 75) * 256 / 1021, 0, 75 * 256 * 32,
320 /* Board init. */
321 static struct mini2440_board_s *mini2440_init_common(struct mini2440_board_s *s,
322 int ram_size,
323 const char *kernel_filename, const char *cpu_model,
324 SDState *mmc)
326 uint32_t sram_base;
328 s->ram = 0x04000000;
329 s->kernel = kernel_filename;
330 s->mmc = mmc;
332 /* Setup CPU & memory */
333 if (ram_size < s->ram + S3C_SRAM_SIZE) {
334 mini2440_printf("This platform requires %i bytes of memory (not %d)\n",
335 s->ram + S3C_SRAM_SIZE, ram_size);
336 exit(1);
338 if (cpu_model && strcmp(cpu_model, "arm920t")) {
339 mini2440_printf("This platform requires an ARM920T core\n");
340 exit(2);
343 if (s->boot_mode == BOOT_NOR)
344 sram_base = S3C_SRAM_BASE_NORBOOT;
345 else
346 sram_base = S3C_SRAM_BASE_NANDBOOT;
348 s->cpu = s3c24xx_init(S3C_CPU_2440, 12000000 /* 12 mhz */, s->ram, sram_base, s->mmc);
350 /* Setup peripherals */
351 mini2440_gpio_setup(s);
353 s->eeprom = ee24c08_init(s3c_i2c_bus(s->cpu->i2c));
356 NICInfo* nd;
357 nd = &nd_table[0];
358 if (!nd->model)
359 nd->model = "dm9000";
360 if (strcmp(nd->model, "dm9000") == 0) {
361 dm9000_init(nd, 0x20000000, 0x300, 0x304, s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_DM9000]);
365 s3c_adc_setscale(s->cpu->adc, mini2440_ts_scale);
367 /* Setup initial (reset) machine state */
368 qemu_register_reset(mini2440_reset, s);
370 return s;
373 #define FLASH_NOR_SIZE (2*1024*1024)
375 static void mini2440_init(ram_addr_t ram_size,
376 const char *boot_device,
377 const char *kernel_filename,
378 const char *kernel_cmdline,
379 const char *initrd_filename,
380 const char *cpu_model)
382 struct mini2440_board_s *mini = (struct mini2440_board_s *)
383 qemu_mallocz(sizeof(struct mini2440_board_s));
384 int sd_idx = drive_get_index(IF_SD, 0, 0);
385 int nor_idx = drive_get_index(IF_PFLASH, 0, 0);
386 int nand_idx = drive_get_index(IF_MTD, 0, 0);
387 SDState *sd = 0;
388 ram_addr_t phys_flash;
390 /* Check the boot mode */
391 if (nand_idx == 0)
392 mini->boot_mode = BOOT_NAND;
393 if (nor_idx == 0)
394 mini->boot_mode = BOOT_NOR;
396 printf("Boot mode: %s\n", mini->boot_mode == BOOT_NOR ? "NOR": "NAND");
398 /* Flash NOR init */
399 if (mini->boot_mode == BOOT_NOR) {
401 int nor_size = bdrv_getlength(drives_table[nor_idx].bdrv);
402 if (nor_size > FLASH_NOR_SIZE)
403 printf("file too big (2MBytes)\n");
404 printf("Register parallel flash %d size 0x%x '%s'\n",
405 nor_idx, nor_size,
406 bdrv_get_device_name(drives_table[nor_idx].bdrv));
410 phys_flash = qemu_ram_alloc(FLASH_NOR_SIZE);
411 pflash_cfi02_register(0, phys_flash,
412 nor_idx != -1 ? drives_table[nor_idx].bdrv : NULL, (64 * 1024),
413 FLASH_NOR_SIZE >> 16,
414 1, 2, 0x0000, 0x0000, 0x0000, 0x0000,
415 0x555, 0x2aa);
418 /* SD init */
419 if (sd_idx >= 0)
420 sd = sd_init(drives_table[sd_idx].bdrv, 0);
422 mini = mini2440_init_common(mini, ram_size,
423 kernel_filename, cpu_model, sd);
425 mini->nand = nand_init(NAND_MFR_SAMSUNG, 0x76);
426 mini->cpu->nand->reg(mini->cpu->nand, mini->nand);
428 mini2440_reset(mini);
431 QEMUMachine mini2440_machine = {
432 "mini2440",
433 "MINI2440 Chinese Samsung SoC dev board (S3C2440A)",
434 .init = mini2440_init,