5decf4b51c6329ad8d82d94f121b32389ed76a32
[qemu/mini2440.git] / hw / mini2440.c
blob5decf4b51c6329ad8d82d94f121b32389ed76a32
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 struct mini2440_board_s {
39 struct s3c_state_s *cpu;
40 unsigned int ram;
41 struct ee24c08_s * eeprom;
42 const char * kernel;
43 SDState * mmc;
44 NANDFlashState *nand;
45 int bl_level;
49 * the 24c08 sits on 4 addresses on the bus, and uses the lower address bits
50 * to address the 256 byte "page" of the eeprom. We thus need to use 4 i2c_slaves
51 * and keep track of which one was used to set the read/write pointer into the data
53 struct ee24c08_s;
54 typedef struct ee24cxx_page_s {
55 i2c_slave i2c;
56 struct ee24c08_s * eeprom;
57 uint8_t page;
58 } ee24cxx_page_s;
59 typedef struct ee24c08_s {
60 /* that memory takes 4 addresses */
61 i2c_slave * slave[4];
62 uint16_t ptr;
63 uint16_t count;
64 uint8_t data[1024];
65 } ee24c08;
67 static void ee24c08_event(i2c_slave *i2c, enum i2c_event event)
69 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
71 if (!s->eeprom)
72 return;
74 s->eeprom->ptr = s->page * 256;
75 s->eeprom->count = 0;
78 static int ee24c08_tx(i2c_slave *i2c, uint8_t data)
80 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
82 if (!s->eeprom)
83 return 0;
84 if (s->eeprom->count++ == 0) {
85 /* first byte is address offset */
86 s->eeprom->ptr = (s->page * 256) + data;
87 } else {
88 mini2440_printf("write %04x=%02x\n", s->eeprom->ptr, data);
89 s->eeprom->data[s->eeprom->ptr] = data;
90 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
91 s->eeprom->count++;
93 return 0;
96 static int ee24c08_rx(i2c_slave *i2c)
98 ee24cxx_page_s *s = FROM_I2C_SLAVE(ee24cxx_page_s, i2c);
99 uint8_t res;
100 if (!s->eeprom)
101 return 0;
103 res = s->eeprom->data[s->eeprom->ptr];
105 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
106 s->eeprom->count++;
107 return res;
110 static void ee24c08_save(QEMUFile *f, void *opaque)
112 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
113 int i;
115 qemu_put_be16s(f, &s->ptr);
116 qemu_put_be16s(f, &s->count);
117 qemu_put_buffer(f, s->data, sizeof(s->data));
119 for (i = 0; i < 4; i++)
120 i2c_slave_save(f, s->slave[i]);
123 static int ee24c08_load(QEMUFile *f, void *opaque, int version_id)
125 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
126 int i;
128 qemu_get_be16s(f, &s->ptr);
129 qemu_get_be16s(f, &s->count);
130 qemu_get_buffer(f, s->data, sizeof(s->data));
132 for (i = 0; i < 4; i++)
133 i2c_slave_load(f, s->slave[i]);
134 return 0;
137 static void ee24c08_page_init(i2c_slave *i2c)
139 /* nothing to do here */
142 static I2CSlaveInfo ee24c08_info = {
143 .init = ee24c08_page_init,
144 .event = ee24c08_event,
145 .recv = ee24c08_rx,
146 .send = ee24c08_tx
149 static void ee24c08_register_devices(void)
151 i2c_register_slave("24C08", sizeof(ee24cxx_page_s), &ee24c08_info);
154 device_init(ee24c08_register_devices);
156 static ee24c08 * ee24c08_init(i2c_bus * bus)
158 ee24c08 *s = qemu_malloc(sizeof(ee24c08));
159 int i = 0;
161 printf("QEMU: %s\n", __FUNCTION__);
163 memset(s->data, 0xff, sizeof(s->data));
165 for (i = 0; i < 4; i++) {
166 DeviceState *dev = i2c_create_slave(bus, "24C08", 0x50 + i);
167 if (!dev) {
168 mini2440_printf("failed address %02x\n", 0x50+i);
170 s->slave[i] = I2C_SLAVE_FROM_QDEV(dev);
171 ee24cxx_page_s *ss = FROM_I2C_SLAVE(ee24cxx_page_s, s->slave[i]);
172 ss->page = i;
173 ss->eeprom = s;
175 register_savevm("ee24c08", -1, 0, ee24c08_save, ee24c08_load, s);
176 return s;
179 /* Handlers for output ports */
180 static void mini2440_bl_switch(void *opaque, int line, int level)
182 printf("QEMU: %s: LCD Backlight now %s (%d).\n", __FUNCTION__, level ? "on" : "off", level);
185 static void mini2440_bl_intensity(int line, int level, void *opaque)
187 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
189 if ((level >> 8) != s->bl_level) {
190 s->bl_level = level >> 8;
191 printf("%s: LCD Backlight now at %04x\n", __FUNCTION__, level);
195 static void mini2440_gpio_setup(struct mini2440_board_s *s)
197 /* set the "input" pin values */
198 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(13), 1);
199 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(14), 1);
200 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(15), 0);
202 s3c_gpio_out_set(s->cpu->io, MINI2440_GPIO_BACKLIGHT,
203 *qemu_allocate_irqs(mini2440_bl_switch, s, 1));
205 s3c_timers_cmp_handler_set(s->cpu->timers, 1, mini2440_bl_intensity, s);
207 /* Register the SD card pins to the lower SD driver */
208 sd_set_cb(s->mmc,
209 s3c_gpio_in_get(s->cpu->io)[MINI2440_GPIO_WP_SD],
210 qemu_irq_invert(s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_nSD_DETECT]));
214 #if 0
215 static void hexdump(const void* address, uint32_t len)
217 const unsigned char* p = address;
218 int i, j;
220 for (i = 0; i < len; i += 16) {
221 for (j = 0; j < 16 && i + j < len; j++)
222 fprintf(stderr, "%02x ", p[i + j]);
223 for (; j < 16; j++)
224 fprintf(stderr, " ");
225 fprintf(stderr, " ");
226 for (j = 0; j < 16 && i + j < len; j++)
227 fprintf(stderr, "%c", (p[i + j] < ' ' || p[i + j] > 0x7f) ? '.' : p[i + j]);
228 fprintf(stderr, "\n");
231 #endif
233 static int mini2440_load_from_nand(NANDFlashState *nand,
234 uint32_t nand_offset, uint32_t s3c_base_offset, uint32_t size)
236 uint8_t buffer[512];
237 uint32_t src = 0;
238 int page = 0;
239 uint32_t dst = 0;
241 if (!nand)
242 return 0;
244 for (page = 0; page < (size / 512); page++, src += 512 + 16, dst += 512) {
245 if (nand_readraw(nand, nand_offset + src, buffer, 512)) {
246 cpu_physical_memory_write(s3c_base_offset + dst, buffer, 512);
247 } else {
248 mini2440_printf("failed to load nand %d:%d\n",
249 nand_offset + src, 512 + 16);
250 return 0;
253 return (int) size;
256 static void mini2440_reset(void *opaque)
258 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
259 uint32_t image_size;
262 * Normally we would load 4 KB of nand to SRAM and jump there, but
263 * it is not working perfectly as expected, so we cheat and load
264 * it from nand directly relocated to 0x33f80000 and jump there
266 if (mini2440_load_from_nand(s->nand, 0, S3C_RAM_BASE | 0x03f80000, 256*1024)> 0) {
267 mini2440_printf("loaded default u-boot from NAND\n");
268 s->cpu->env->regs[15] = S3C_RAM_BASE | 0x03f80000; /* start address, u-boot already relocated */
270 #if 0 && defined(LATER)
271 if (mini2440_load_from_nand(s->nand, 0, S3C_SRAM_BASE_NANDBOOT, S3C_SRAM_SIZE) > 0) {
272 s->cpu->env->regs[15] = S3C_SRAM_BASE_NANDBOOT; /* start address, u-boot relocating code */
273 mini2440_printf("4KB SteppingStone loaded from NAND\n");
275 #endif
277 * if a u--boot is available as a file, we always use it
280 image_size = load_image("mini2440/u-boot.bin", qemu_get_ram_ptr(0x03f80000));
281 if (image_size < 0)
282 image_size = load_image("u-boot.bin", qemu_get_ram_ptr(0x03f80000));
283 if (image_size > 0) {
284 if (image_size & (512 -1)) /* round size to a NAND block size */
285 image_size = (image_size + 512) & ~(512-1);
286 mini2440_printf("loaded override u-boot (size %x)\n", image_size);
287 s->cpu->env->regs[15] = S3C_RAM_BASE | 0x03f80000; /* start address, u-boot already relocated */
291 * if a kernel was explicitly specified, we load it too
293 if (s->kernel) {
294 image_size = load_image(s->kernel, qemu_get_ram_ptr(0x02000000));
295 if (image_size > 0) {
296 if (image_size & (512 -1)) /* round size to a NAND block size */
297 image_size = (image_size + 512) & ~(512-1);
298 mini2440_printf("loaded %s (size %x)\n", s->kernel, image_size);
303 /* Typical touchscreen calibration values */
304 static const int mini2440_ts_scale[6] = {
305 0, (90 - 960) * 256 / 1021, -90 * 256 * 32,
306 (940 - 75) * 256 / 1021, 0, 75 * 256 * 32,
309 /* Board init. */
310 static struct mini2440_board_s *mini2440_init_common(int ram_size,
311 const char *kernel_filename, const char *cpu_model,
312 SDState *mmc)
314 struct mini2440_board_s *s = (struct mini2440_board_s *)
315 qemu_mallocz(sizeof(struct mini2440_board_s));
317 s->ram = 0x04000000;
318 s->kernel = kernel_filename;
319 s->mmc = mmc;
321 /* Setup CPU & memory */
322 if (ram_size < s->ram + S3C_SRAM_SIZE) {
323 mini2440_printf("This platform requires %i bytes of memory (not %d)\n",
324 s->ram + S3C_SRAM_SIZE, ram_size);
325 exit(1);
327 if (cpu_model && strcmp(cpu_model, "arm920t")) {
328 mini2440_printf("This platform requires an ARM920T core\n");
329 exit(2);
331 s->cpu = s3c24xx_init(S3C_CPU_2440, 12000000 /* 12 mhz */, s->ram, S3C_SRAM_BASE_NANDBOOT, s->mmc);
333 /* Setup peripherals */
334 mini2440_gpio_setup(s);
336 s->eeprom = ee24c08_init(s3c_i2c_bus(s->cpu->i2c));
339 NICInfo* nd;
340 nd = &nd_table[0];
341 if (!nd->model)
342 nd->model = "dm9000";
343 if (strcmp(nd->model, "dm9000") == 0) {
344 dm9000_init(nd, 0x20000000, 0x300, 0x304, s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_DM9000]);
348 s3c_adc_setscale(s->cpu->adc, mini2440_ts_scale);
350 /* Setup initial (reset) machine state */
351 qemu_register_reset(mini2440_reset, s);
353 return s;
357 static void mini2440_init(ram_addr_t ram_size,
358 const char *boot_device,
359 const char *kernel_filename,
360 const char *kernel_cmdline,
361 const char *initrd_filename,
362 const char *cpu_model)
364 struct mini2440_board_s *mini;
365 int sd_idx = drive_get_index(IF_SD, 0, 0);
366 SDState *sd = 0;
368 if (sd_idx >= 0)
369 sd = sd_init(drives_table[sd_idx].bdrv, 0);
371 mini = mini2440_init_common(ram_size,
372 kernel_filename, cpu_model, sd);
374 mini->nand = nand_init(NAND_MFR_SAMSUNG, 0x76);
375 mini->cpu->nand->reg(mini->cpu->nand, mini->nand);
377 mini2440_reset(mini);
380 QEMUMachine mini2440_machine = {
381 "mini2440",
382 "MINI2440 Chinese Samsung SoC dev board (S3C2440A)",
383 .init = mini2440_init,