more cleanups
[qemu] / hw / beagle.c
1 /*
2  * Beagle board emulation. http://beagleboard.org/
3  * 
4  * Copyright (C) 2008 yajin(yajin@vm-kernel.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 or
9  * (at your option) version 3 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21
22 #include "qemu-common.h"
23 #include "sysemu.h"
24 #include "omap.h"
25 #include "arm-misc.h"
26 #include "irq.h"
27 #include "console.h"
28 #include "boards.h"
29 #include "i2c.h"
30 #include "devices.h"
31 #include "flash.h"
32 #include "hw.h"
33 #include "block.h"
34
35 #define BEAGLE_NAND_CS       0
36 #define BEAGLE_NAND_PAGESIZE 0x800
37
38 /* Beagle board support */
39 struct beagle_s {
40     struct omap_mpu_state_s *cpu;
41     
42     struct nand_flash_s *nand;
43     struct omap3_lcd_panel_s *lcd_panel;
44     i2c_bus *i2c;
45     struct twl4030_s *twl4030;
46 };
47
48 static struct arm_boot_info beagle_binfo = {
49     .ram_size = 0x08000000,
50 };
51
52 static void beagle_nand_pread(struct nand_flash_s *nand,
53                               uint64_t addr,
54                               uint8_t *data,
55                               uint32_t len)
56 {
57     uint16_t x;
58     uint32_t i;
59     
60     if ((len&1) || (addr&1)) {
61         fprintf(stderr, "%s: read byte length and address must be even (x16 device!)\n",
62                 __FUNCTION__);
63         exit(-1);
64     }
65     /* send command: reset */
66     nand_setpins(nand, 1, 0, 0, 1, 0);
67     nand_setio(nand, 0xff);
68     while (len) {
69         /* send command: read page (cycle1) */
70         nand_setpins(nand, 1, 0, 0, 1, 0);
71         nand_setio(nand, 0);
72         /* send address */
73         nand_setpins(nand, 0, 1, 0, 1, 0);
74         nand_setio(nand, (uint32_t)((addr >> 1) & 0xff));
75         nand_setio(nand, (uint32_t)((addr >> 9) & 0x3));
76         nand_setio(nand, (uint32_t)((addr >> 11) & 0xff));
77         nand_setio(nand, (uint32_t)((addr >> 19) & 0xff));
78         nand_setio(nand, (uint32_t)((addr >> 27) & 0x1));
79         /* send command: read page (cycle2) */
80         nand_setpins(nand, 1, 0, 0, 1, 0);
81         nand_setio(nand, 0x30);
82         /* read page data */
83         nand_setpins(nand, 0, 0, 0, 1, 0);
84         for (i = (BEAGLE_NAND_PAGESIZE / 2) - (addr & 0x3ff); i && len; i--) {
85             x = nand_getio(nand);
86             *(data++) = (uint8_t)(x & 0xff);
87             *(data++) = (uint8_t)((x >> 8) & 0xff);
88             len -= 2;
89             addr += 2;
90         }
91     }
92 }
93
94 static void beagle_init(ram_addr_t ram_size, int vga_ram_size,
95                 const char *boot_device,
96                 const char *kernel_filename, const char *kernel_cmdline,
97                 const char *initrd_filename, const char *cpu_model)
98 {
99     struct beagle_s *s = (struct beagle_s *) qemu_mallocz(sizeof(*s));
100     int sdram_size = beagle_binfo.ram_size;
101     int sdindex = drive_get_index(IF_SD, 0, 0);
102     
103     if (sdindex == -1) {
104         fprintf(stderr, "qemu: missing SecureDigital device\n");
105         exit(1);
106     }
107     
108         if (ram_size < sdram_size +  OMAP3530_SRAM_SIZE) {
109         fprintf(stderr, "This architecture uses %i bytes of memory\n",
110                         sdram_size + OMAP3530_SRAM_SIZE);
111         exit(1);
112     }
113         s->cpu = omap3530_mpu_init(sdram_size, NULL);
114     
115     if (serial_hds[0])
116         omap_uart_attach(s->cpu->uart[2], serial_hds[0]);
117
118         s->nand = nand_init(NAND_MFR_MICRON, 0xba); /* MT29F2G16ABC */
119         nand_setpins(s->nand, 0, 0, 0, 1, 0); /* no write-protect */
120     omap_gpmc_attach(s->cpu->gpmc, BEAGLE_NAND_CS, 0, NULL, NULL, s, s->nand);
121     omap3_mmc_attach(s->cpu->omap3_mmc[0], drives_table[sdindex].bdrv);
122
123     s->i2c = omap_i2c_bus(s->cpu->i2c[0]);
124     s->twl4030 = twl4030_init(s->i2c, s->cpu->irq[0][OMAP_INT_35XX_SYS_NIRQ]);
125
126         s->lcd_panel = omap3_lcd_panel_init();
127         omap3_lcd_panel_attach(s->cpu->dss, 0, s->lcd_panel);
128
129     if (!omap3_mmc_boot(s->cpu) 
130         && !omap3_nand_boot(s->cpu, s->nand, beagle_nand_pread)) {
131         fprintf(stderr, "%s: boot from MMC and NAND failed\n",
132                 __FUNCTION__);
133         exit(-1);
134     }
135 }
136
137 QEMUMachine beagle_machine = {
138     .name = "beagle",
139     .desc =     "Beagle board (OMAP3530)",
140     .init =     beagle_init,
141     .ram_require =     (0x08000000 +  OMAP3530_SRAM_SIZE) | RAMSIZE_FIXED,
142 };
143