misc 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
37 #define GPMC_NOR             0
38 #define GPMC_NAND           1
39 #define GPMC_MDOC           2
40 #define GPMC_ONENAND    3
41 #define MMC_NAND            4
42 #define MMC_ONENAND     5
43
44
45 #define TST_DEVICE              0x0
46 #define EMU_DEVICE              0x1
47 #define HS_DEVICE               0x2
48 #define GP_DEVICE               0x3
49
50 #define DEBUG_BEAGLE
51
52 #ifdef DEBUG_BEAGLE
53 #define BEAGLE_DEBUG(...)    do { fprintf(stderr, __VA_ARGS__); } while(0)
54 #else
55 #define BEAGLE_DEBUG(x)    
56 #endif
57
58 /* Beagle board support */
59 struct beagle_s {
60     struct omap_mpu_state_s *cpu;
61     
62     struct nand_bflash_s *nand;
63     struct omap3_lcd_panel_s *lcd_panel;
64     i2c_bus *i2c;
65     struct twl4030_s *twl4030;
66 };
67
68
69
70 static struct arm_boot_info beagle_binfo = {
71     .ram_size = 0x08000000,
72 };
73
74
75 static uint32_t beagle_nand_read16(void *opaque, target_phys_addr_t addr)
76 {
77         struct beagle_s *s = (struct beagle_s *) opaque;
78     //BEAGLE_DEBUG("beagle_nand_read16 offset %x\n",addr);
79
80         switch (addr)
81         {
82                 case 0x7C: /*NAND_COMMAND*/
83                 case 0x80: /*NAND_ADDRESS*/
84                         OMAP_BAD_REG(addr);
85                         break;
86                 case 0x84: /*NAND_DATA*/
87                         return nandb_read_data16(s->nand);
88                         break;
89                 default:
90                         OMAP_BAD_REG(addr);
91                         break;
92         }
93     return 0;
94 }
95
96 static void beagle_nand_write16(void *opaque, target_phys_addr_t addr,
97                 uint32_t value)
98 {
99         struct beagle_s *s = (struct beagle_s *) opaque;
100     switch (addr)
101         {
102                 case 0x7C: /*NAND_COMMAND*/
103                         nandb_write_command(s->nand,value);
104                         break;
105                 case 0x80: /*NAND_ADDRESS*/
106                         nandb_write_address(s->nand,value);
107                         break;
108                 case 0x84: /*NAND_DATA*/
109                         nandb_write_data16(s->nand,value);
110                         break;
111                 default:
112                         OMAP_BAD_REG(addr);
113                         break;
114         }
115 }
116
117
118 static CPUReadMemoryFunc *beagle_nand_readfn[] = {
119         beagle_nand_read16,
120         beagle_nand_read16,
121         omap_badwidth_read32,
122 };
123
124 static CPUWriteMemoryFunc *beagle_nand_writefn[] = {
125         beagle_nand_write16,
126         beagle_nand_write16,
127         omap_badwidth_write32,
128 };
129
130 static void beagle_nand_setup(struct beagle_s *s)
131 {
132         //int iomemtype;
133         
134         /*MT29F2G16ABC*/
135         s->nand = nandb_init(NAND_MFR_MICRON,0xba);
136         /*wp=1, no write protect!!! */
137         //nand_set_wp(s->nand, 1);
138
139     omap_gpmc_attach(s->cpu->gpmc, BEAGLE_NAND_CS, 0, NULL, NULL, s,
140                      beagle_nand_readfn, beagle_nand_writefn);
141
142     omap3_set_mem_type(s->cpu,GPMC_NAND);
143 }
144
145 static int beagle_nand_read_page(struct beagle_s *s,uint8_t *buf, uint16_t page_addr)
146 {
147         uint16_t *p;
148         int i;
149
150         p=(uint16_t *)buf;
151
152         /*send command 0x0*/
153         beagle_nand_write16(s,0x7C,0);
154         /*send page address */
155         beagle_nand_write16(s,0x80,page_addr&0xff);
156         beagle_nand_write16(s,0x80,(page_addr>>8)&0x7);
157         beagle_nand_write16(s,0x80,(page_addr>>11)&0xff);
158         beagle_nand_write16(s,0x80,(page_addr>>19)&0xff);
159         beagle_nand_write16(s,0x80,(page_addr>>27)&0xff);
160         /*send command 0x30*/
161         beagle_nand_write16(s,0x7C,0x30);
162
163         for (i=0;i<0x800/2;i++)
164         {
165                 *p++ = beagle_nand_read16(s,0x84);
166         }
167         return 1;
168 }
169
170 /*read the xloader from NAND Flash into internal RAM*/
171 static int beagle_boot_from_nand(struct beagle_s *s)
172 {
173         uint32_t        loadaddr, len;
174         uint8_t nand_page[0x800],*load_dest;
175         uint32_t nand_pages,i;
176
177         /* The first two words(8 bytes) in first nand flash page have special meaning.
178                 First word:x-loader len
179                 Second word: x-load address in internal ram */
180         beagle_nand_read_page(s,nand_page,0);
181         len = *((uint32_t*)nand_page);
182         loadaddr =  *((uint32_t*)(nand_page+4));
183         if ((len==0)||(loadaddr==0)||(len==0xffffffff)||(loadaddr==0xffffffff))
184                 return (-1);
185
186         /*put the first page into internal ram*/
187         load_dest = phys_ram_base +beagle_binfo.ram_size;
188         load_dest += loadaddr-OMAP3_SRAM_BASE;
189         
190         BEAGLE_DEBUG("load_dest %x phys_ram_base %x \n",(unsigned)load_dest,(unsigned)phys_ram_base);
191         
192         memcpy(load_dest,nand_page+8,0x800-8);
193         load_dest += 0x800-8;
194
195         nand_pages = len/0x800;
196         if (len%0x800!=0)
197                 nand_pages++;
198
199         for (i=1;i<nand_pages;i++)
200         {
201                 beagle_nand_read_page(s,nand_page,i*0x800);
202                 memcpy(load_dest,nand_page,0x800);
203                 load_dest += 0x800;
204         }
205         s->cpu->env->regs[15] = loadaddr;
206         return 0;
207
208 }
209
210 static int beagle_rom_emu(struct beagle_s *s)
211 {
212     if (!omap3_mmc_boot(s->cpu))
213         if (beagle_boot_from_nand(s) < 0)
214             return -1;
215         return 0;
216 }
217
218 static void beagle_dss_setup(struct beagle_s *s)
219 {
220         s->lcd_panel = omap3_lcd_panel_init();
221         omap3_lcd_panel_attach(s->cpu->dss, 0, s->lcd_panel);
222 }
223
224 //static void beagle_mmc_cs_cb(void *opaque, int line, int level)
225 //{
226 //    /* TODO: this seems to actually be connected to the menelaus, to
227 //     * which also both MMC slots connect.  */
228 //    omap_mmc_enable((struct omap_mmc_s *) opaque, !level);
229 //
230 //    printf("%s: MMC slot %i active\n", __FUNCTION__, level + 1);
231 //}
232
233 static void beagle_i2c_setup(struct beagle_s *s)
234 {
235     /* Attach the CPU on one end of our I2C bus.  */
236     s->i2c = omap_i2c_bus(s->cpu->i2c[0]);
237
238     s->twl4030 = twl4030_init(s->i2c, s->cpu->irq[0][OMAP_INT_35XX_SYS_NIRQ]);
239 }
240
241
242 static void beagle_init(ram_addr_t ram_size, int vga_ram_size,
243                 const char *boot_device,
244                 const char *kernel_filename, const char *kernel_cmdline,
245                 const char *initrd_filename, const char *cpu_model)
246 {
247     struct beagle_s *s = (struct beagle_s *) qemu_mallocz(sizeof(*s));
248     int sdram_size = beagle_binfo.ram_size;
249
250         if (ram_size < sdram_size +  OMAP3530_SRAM_SIZE) {
251         fprintf(stderr, "This architecture uses %i bytes of memory\n",
252                         sdram_size + OMAP3530_SRAM_SIZE);
253         exit(1);
254     }
255         s->cpu = omap3530_mpu_init(sdram_size, NULL);
256         beagle_nand_setup(s);
257         beagle_i2c_setup(s);
258         beagle_dss_setup(s);
259         omap3_set_device_type(s->cpu,GP_DEVICE);
260     if (beagle_rom_emu(s) < 0) {
261                 fprintf(stderr,"boot from MMC and nand failed \n");
262                 exit(-1);
263         }
264 }
265
266
267
268 QEMUMachine beagle_machine = {
269     .name = "beagle",
270     .desc =     "Beagle board (OMAP3530)",
271     .init =     beagle_init,
272     .ram_require =     (0x08000000 +  OMAP3530_SRAM_SIZE) | RAMSIZE_FIXED,
273 };
274