[S3C2440] Added a i2c register
[qemu/mini2440.git] / hw / mini2440.c
blob119e2bc9f31e827f4287ad5373391380fbbb35d0
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, "%s: " format, __FUNCTION__, ##__VA_ARGS__)
28 /* Wiring common to all revisions */
29 #define MINI2440_GPIO_BACKLIGHT S3C_GPB(1)
30 #define MINI2440_GPIO_LCD_RESET S3C_GPC(6)
31 #define MINI2440_GPIO_nSD_DETECT S3C_GPG(8)
32 #define MINI2440_IRQ_nSD_DETECT S3C_EINT(16)
33 #define MINI2440_IRQ_DM9000 S3C_EINT(7)
34 #define MINI2440_GPIO_DM9000 S3C_GPF(7)
36 #define MINI2440_GPIO_SDMMC_ON S3C_GPB(2)
37 #define MINI2440_GPIO_USB_PULLUP S3C_GPB(9)
38 #define MINI2440_GPIO_USB_ATTACH S3C_GPB(10)
40 struct mini2440_board_s {
41 struct s3c_state_s *cpu;
42 unsigned int ram;
43 struct ee24c08_s * eeprom;
44 const char * kernel;
45 SDState * mmc;
46 struct nand_flash_s *nand;
47 int bl_level;
51 * the 24c08 sits on 4 addresses on the bus, and uses the lower address bits
52 * to address the 256 byte "page" of the eeprom. We thus need to use 4 i2c_slaves
53 * and keep track of which one was used to set the read/write pointer into the data
55 struct ee24c08_s;
56 struct ee24c08_slave_s {
57 i2c_slave slave;
58 struct ee24c08_s * eeprom;
59 uint8_t page;
61 struct ee24c08_s {
62 // that memory takes 4 addresses
63 struct ee24c08_slave_s * slave[4];
64 uint16_t ptr;
65 uint16_t count;
66 uint8_t data[1024];
69 static void ee24c08_event(i2c_slave *i2c, enum i2c_event event)
71 struct ee24c08_slave_s *s = (struct ee24c08_slave_s *) i2c;
73 s->eeprom->ptr = s->page * 256;
74 s->eeprom->count = 0;
77 static int ee24c08_tx(i2c_slave *i2c, uint8_t data)
79 struct ee24c08_slave_s *s = (struct ee24c08_slave_s *) i2c;
80 if (s->eeprom->count++ == 0) {
81 /* first byte is address offset */
82 s->eeprom->ptr = (s->page * 256) + data;
83 } else {
84 printf("%s: write %04x=%02x\n", __FUNCTION__, s->eeprom->ptr, data);
85 s->eeprom->data[s->eeprom->ptr] = data;
86 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
87 s->eeprom->count++;
89 return 0;
92 static int ee24c08_rx(i2c_slave *i2c)
94 struct ee24c08_slave_s *s = (struct ee24c08_slave_s *) i2c;
95 uint8_t res = s->eeprom->data[s->eeprom->ptr];
97 s->eeprom->ptr = (s->eeprom->ptr & ~0xff) | ((s->eeprom->ptr + 1) & 0xff);
98 s->eeprom->count++;
99 return res;
102 static void ee24c08_save(QEMUFile *f, void *opaque)
104 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
105 int i;
107 qemu_put_be16s(f, &s->ptr);
108 qemu_put_be16s(f, &s->count);
109 qemu_put_buffer(f, s->data, sizeof(s->data));
111 for (i = 0; i < 4; i++)
112 i2c_slave_save(f, &s->slave[i]->slave);
115 static int ee24c08_load(QEMUFile *f, void *opaque, int version_id)
117 struct ee24c08_s *s = (struct ee24c08_s *) opaque;
118 int i;
120 qemu_get_be16s(f, &s->ptr);
121 qemu_get_be16s(f, &s->count);
122 qemu_get_buffer(f, s->data, sizeof(s->data));
124 for (i = 0; i < 4; i++)
125 i2c_slave_load(f, &s->slave[i]->slave);
126 return 0;
129 static struct ee24c08_s * ee24c08_init(i2c_bus *bus)
131 struct ee24c08_s *s = (struct ee24c08_s *)
132 qemu_mallocz(sizeof(struct ee24c08_s));
133 int i = 0;
134 memset(s->data, 0xff, sizeof(s->data));
136 for (i = 0; i < 4; i++) {
137 struct ee24c08_slave_s * ss = (struct ee24c08_slave_s *)
138 i2c_slave_init(bus, 0x50 + i, sizeof(struct ee24c08_slave_s));
139 ss->slave.event = ee24c08_event;
140 ss->slave.recv = ee24c08_rx;
141 ss->slave.send = ee24c08_tx;
142 ss->page = i;
143 ss->eeprom = s;
144 s->slave[i] = ss;
146 register_savevm("ee24c08", -1, 0, ee24c08_save, ee24c08_load, s);
147 return s;
151 /* Handlers for output ports */
152 static void mini2440_bl_switch(void *opaque, int line, int level)
154 printf("%s: LCD Backlight now %s.\n", __FUNCTION__, level ? "on" : "off");
157 static void mini2440_bl_intensity(int line, int level, void *opaque)
159 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
161 if ((level >> 8) != s->bl_level) {
162 s->bl_level = level >> 8;
163 printf("%s: LCD Backlight now at %04x\n", __FUNCTION__, s->bl_level);
167 static void mini2440_gpio_setup(struct mini2440_board_s *s)
169 /* set the "input" pin values */
170 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(13), 1);
171 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(14), 1);
172 s3c_gpio_set_dat(s->cpu->io, S3C_GPG(15), 0);
174 s3c_gpio_out_set(s->cpu->io, MINI2440_GPIO_BACKLIGHT,
175 *qemu_allocate_irqs(mini2440_bl_switch, s, 1));
177 s3c_timers_cmp_handler_set(s->cpu->timers, 1, mini2440_bl_intensity, s);
179 // this confuses the kernel, we will need a way to bridge this IRQ to the SD system
180 // right now without this, qemu will not know how to pass the SD card insert/remove
181 // properly to the kernel
182 // sd_set_cb(s->mmc, 0, s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_nSD_DETECT]);
185 #if 0
186 static void hexdump(const void* address, uint32_t len)
188 const unsigned char* p = address;
189 int i, j;
191 for (i = 0; i < len; i += 16) {
192 for (j = 0; j < 16 && i + j < len; j++)
193 fprintf(stderr, "%02x ", p[i + j]);
194 for (; j < 16; j++)
195 fprintf(stderr, " ");
196 fprintf(stderr, " ");
197 for (j = 0; j < 16 && i + j < len; j++)
198 fprintf(stderr, "%c", (p[i + j] < ' ' || p[i + j] > 0x7f) ? '.' : p[i + j]);
199 fprintf(stderr, "\n");
202 #endif
204 static void mini2440_reset(void *opaque)
206 struct mini2440_board_s *s = (struct mini2440_board_s *) opaque;
207 uint32_t image_size;
209 if (1) {
210 image_size = load_image("mini2440/u-boot.bin", phys_ram_base + 0x03f80000);
211 if (!image_size)
212 image_size = load_image("u-boot.bin", phys_ram_base + 0x03f80000);
213 if (image_size) {
214 if (image_size & (512 -1)) /* round size to a NAND block size */
215 image_size = (image_size + 512) & ~(512-1);
216 fprintf(stderr, "%s: loaded default u-boot (size %x)\n", __FUNCTION__, image_size);
217 s->cpu->env->regs[15] = S3C_RAM_BASE | 0x03f80000; // start address, u-boot already relocated
220 #if 0
222 * Performs Samsung S3C2440 bootup, but loading 4KB of the nand at the base of the RAM
223 * and jumping there
225 if (s->nand) {
226 uint32_t src = 0;
227 int page = 0;
228 uint8_t stone[S3C_SRAM_SIZE];
229 uint8_t * dst = stone;
231 fprintf(stderr, "%s: attempting boot from NAND\n", __FUNCTION__);
233 for (page = 0; page < (S3C_SRAM_SIZE / 512); page++, src += 512+16, dst += 512)
234 if (nand_readraw(s->nand, src, dst, 512) == 0) {
235 fprintf(stderr, "%s: failed to load nand %d:%d\n", __FUNCTION__, src, 512+16);
237 cpu_physical_memory_write(S3C_SRAM_BASE_NANDBOOT, stone, S3C_SRAM_SIZE);
238 s->cpu->env->regs[15] = S3C_SRAM_BASE_NANDBOOT; // start address, u-boot relocating code
239 fprintf(stderr, "%s: 4KB SteppingStone loaded from NAND\n", __FUNCTION__);
241 #endif
242 if (1) {
243 image_size = load_image(s->kernel, phys_ram_base + 0x02000000);
244 if (image_size) {
245 if (image_size & (512 -1)) /* round size to a NAND block size */
246 image_size = (image_size + 512) & ~(512-1);
247 fprintf(stderr, "%s: loaded %s (size %x)\n", __FUNCTION__, s->kernel, image_size);
250 if (0) {
251 image_size = load_image("/tftpboot/minimalist-image-mini2440.jffs2", phys_ram_base);
252 if (image_size) {
253 if (image_size & (512 -1)) /* round size to a NAND block size */
254 image_size = (image_size + 512) & ~(512-1);
255 fprintf(stderr, "%s: loaded jffs2 (size %x)\n", __FUNCTION__, image_size);
261 /* Typical touchscreen calibration values */
262 static const int mini2440_ts_scale[6] = {
263 0, (90 - 960) * 256 / 1021, -90 * 256 * 32,
264 (940 - 75) * 256 / 1021, 0, 75 * 256 * 32,
267 /* Board init. */
268 static struct mini2440_board_s *mini2440_init_common(int ram_size,
269 const char *kernel_filename, const char *cpu_model,
270 SDState *mmc)
272 struct mini2440_board_s *s = (struct mini2440_board_s *)
273 qemu_mallocz(sizeof(struct mini2440_board_s));
275 s->ram = 0x04000000;
276 s->kernel = kernel_filename;
277 s->mmc = mmc;
279 /* Setup CPU & memory */
280 if (ram_size < s->ram + S3C_SRAM_SIZE) {
281 fprintf(stderr, "This platform requires %i bytes of memory (not %d)\n",
282 s->ram + S3C_SRAM_SIZE, ram_size);
283 exit(1);
285 if (cpu_model && strcmp(cpu_model, "arm920t")) {
286 fprintf(stderr, "This platform requires an ARM920T core\n");
287 exit(2);
289 s->cpu = s3c24xx_init(S3C_CPU_2440, s->ram, S3C_SRAM_BASE_NANDBOOT, s->mmc);
291 /* Setup peripherals */
292 mini2440_gpio_setup(s);
294 s->eeprom = ee24c08_init(s3c_i2c_bus(s->cpu->i2c));
296 // if (usb_enabled)
297 // usb_device_attach(usb_bt_init(local_piconet));
300 NICInfo* nd;
301 nd = &nd_table[0];
302 if (!nd->model)
303 nd->model = "dm9000";
304 if (strcmp(nd->model, "dm9000") == 0) {
305 dm9000_init(nd, 0x20000000, 0x300, 0x304, s3c_gpio_in_get(s->cpu->io)[MINI2440_IRQ_DM9000]);
309 s3c_adc_setscale(s->cpu->adc, mini2440_ts_scale);
311 /* Setup initial (reset) machine state */
312 qemu_register_reset(mini2440_reset, s);
313 #if 0
314 arm_load_kernel(s->ram, kernel_filename, kernel_cmdline,
315 initrd_filename, 0x49e, S3C_RAM_BASE);
316 #endif
318 return s;
321 static void mini2440_init(ram_addr_t ram_size, int vga_ram_size,
322 const char *boot_device,
323 const char *kernel_filename, const char *kernel_cmdline,
324 const char *initrd_filename, const char *cpu_model)
326 struct mini2440_board_s *mini;
327 int sd_idx = drive_get_index(IF_SD, 0, 0);
328 SDState *sd = 0;
330 if (sd_idx >= 0)
331 sd = sd_init(drives_table[sd_idx].bdrv, 0);
333 mini = mini2440_init_common(ram_size,
334 kernel_filename, cpu_model, sd);
336 mini->nand = nand_init(NAND_MFR_SAMSUNG, 0x36);
337 mini->cpu->nand->reg(mini->cpu->nand, mini->nand);
339 mini2440_reset(mini);
342 QEMUMachine mini2440_machine = {
343 "mini2440",
344 "MINI2440 Chinese Samsung SoC dev board (S3C2440A)",
345 .init = mini2440_init,
346 .ram_require = (0x04000000 + S3C_SRAM_SIZE) | RAMSIZE_FIXED