Merge branch 'u-boot-samsung/master' into 'u-boot-arm/master'
[oweals/u-boot.git] / board / BuS / vl_ma2sc / vl_ma2sc.c
1 /*
2  * (C) Copyright 2009-2012
3  * Jens Scharsig  <esw@bus-elekronik.de>
4  * BuS Elektronik GmbH & Co. KG
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <config.h>
10 #include <common.h>
11 #include <asm/sizes.h>
12 #include <asm/io.h>
13 #include <asm/arch/hardware.h>
14 #include <asm/arch/clk.h>
15 #include <asm/arch/at91_matrix.h>
16 #include <asm/arch/at91sam9_smc.h>
17 #include <asm/arch/at91_pmc.h>
18 #include <asm/arch/at91_pio.h>
19 #include <asm/arch/at91sam9263.h>
20 #include <asm/arch/gpio.h>
21 #include <asm/arch/at91_common.h>
22 #include <lcd.h>
23 #include <i2c.h>
24 #include <atmel_lcdc.h>
25 #if defined(CONFIG_RESET_PHY_R) && defined(CONFIG_MACB)
26 #include <net.h>
27 #endif
28 #include <netdev.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #ifdef CONFIG_CMD_NAND
33 static void vl_ma2sc_nand_hw_init(void)
34 {
35         unsigned long csa;
36         at91_smc_t      *smc    = (at91_smc_t *) ATMEL_BASE_SMC0;
37         at91_matrix_t   *matrix = (at91_matrix_t *) ATMEL_BASE_MATRIX;
38         at91_pmc_t      *pmc    = (at91_pmc_t *) ATMEL_BASE_PMC;
39
40         at91_set_pio_output(AT91_PIO_PORTA, 13, 1);     /* CAN_TX -> H */
41         at91_set_pio_output(AT91_PIO_PORTA, 12, 1);     /* CAN_STB -> H */
42         at91_set_pio_output(AT91_PIO_PORTA, 11, 1);     /* CAN_EN -> H */
43
44         /* Enable CS3 */
45         csa = readl(&matrix->csa[0]) | AT91_MATRIX_CSA_EBI_CS3A;
46         writel(csa, &matrix->csa[0]);
47
48         /* Configure SMC CS3 for NAND/SmartMedia */
49         writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
50                 AT91_SMC_SETUP_NRD(1) | AT91_SMC_SETUP_NCS_RD(0),
51                 &smc->cs[3].setup);
52
53         writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(3) |
54                 AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(3),
55                 &smc->cs[3].pulse);
56
57         writel(AT91_SMC_CYCLE_NWE(5) | AT91_SMC_CYCLE_NRD(5),
58                 &smc->cs[3].cycle);
59         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
60                 AT91_SMC_MODE_DBW_8 |
61                 AT91_SMC_MODE_TDF_CYCLE(2),
62                 &smc->cs[3].mode);
63         writel((1 << ATMEL_ID_PIOB) | (1 << ATMEL_ID_PIOCDE),
64                 &pmc->pcer);
65
66         /* Configure RDY/BSY */
67 #ifdef CONFIG_SYS_NAND_READY_PIN
68         at91_set_pio_input(CONFIG_SYS_NAND_READY_PIN, 1);
69 #endif
70         /* Enable NandFlash */
71         at91_set_pio_output(CONFIG_SYS_NAND_ENABLE_PIN, 1);
72 }
73 #endif
74
75 #ifdef CONFIG_MACB
76 static void vl_ma2sc_macb_hw_init(void)
77 {
78         at91_pmc_t      *pmc    = (at91_pmc_t *) ATMEL_BASE_PMC;
79
80         /* Enable clock */
81         writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
82
83         at91_phy_reset();
84
85         at91_macb_hw_init();
86 }
87 #endif
88
89 #ifdef CONFIG_LCD
90 vidinfo_t panel_info = {
91         .vl_col =               320,
92         .vl_row =               240,
93         .vl_clk =               6500000,
94         .vl_sync =              ATMEL_LCDC_INVDVAL_INVERTED |
95                                 ATMEL_LCDC_INVLINE_INVERTED |
96                                 ATMEL_LCDC_INVVD_INVERTED   |
97                                 ATMEL_LCDC_INVFRAME_INVERTED,
98         .vl_bpix =              (ATMEL_LCDC_PIXELSIZE_8 >> 5),
99         .vl_tft =               1,
100         .vl_hsync_len =         5,      /* Horiz Sync Pulse Width */
101         .vl_left_margin =       68,     /* horiz back porch */
102         .vl_right_margin =      20,     /* horiz front porch */
103         .vl_vsync_len =         2,      /* vert Sync Pulse Width */
104         .vl_upper_margin =      18,     /* vert back porch */
105         .vl_lower_margin =      4,      /* vert front porch */
106         .mmio =                 ATMEL_BASE_LCDC,
107 };
108
109 void lcd_enable(void)
110 {
111 }
112
113 void lcd_disable(void)
114 {
115 }
116
117 static void vl_ma2sc_lcd_hw_init(void)
118 {
119         at91_pmc_t      *pmc    = (at91_pmc_t *) ATMEL_BASE_PMC;
120
121         at91_set_a_periph(AT91_PIO_PORTC, 0, 0);        /* LCDVSYNC */
122         at91_set_a_periph(AT91_PIO_PORTC, 1, 0);        /* LCDHSYNC */
123         at91_set_a_periph(AT91_PIO_PORTC, 2, 0);        /* LCDDOTCK */
124         at91_set_a_periph(AT91_PIO_PORTC, 3, 0);        /* LCDDEN */
125         at91_set_b_periph(AT91_PIO_PORTB, 9, 0);        /* LCDCC */
126
127         at91_set_a_periph(AT91_PIO_PORTC, 4, 0);        /* LCDD0 */
128         at91_set_a_periph(AT91_PIO_PORTC, 5, 0);        /* LCDD1 */
129         at91_set_a_periph(AT91_PIO_PORTC, 6, 0);        /* LCDD2 */
130         at91_set_a_periph(AT91_PIO_PORTC, 7, 0);        /* LCDD3 */
131         at91_set_a_periph(AT91_PIO_PORTC, 8, 0);        /* LCDD4 */
132         at91_set_a_periph(AT91_PIO_PORTC, 9, 0);        /* LCDD5 */
133         at91_set_a_periph(AT91_PIO_PORTC, 10, 0);       /* LCDD6 */
134         at91_set_a_periph(AT91_PIO_PORTC, 11, 0);       /* LCDD7 */
135
136         at91_set_a_periph(AT91_PIO_PORTC, 13, 0);       /* LCDD9 */
137         at91_set_a_periph(AT91_PIO_PORTC, 14, 0);       /* LCDD10 */
138         at91_set_a_periph(AT91_PIO_PORTC, 15, 0);       /* LCDD11 */
139         at91_set_a_periph(AT91_PIO_PORTC, 16, 0);       /* LCDD12 */
140         at91_set_b_periph(AT91_PIO_PORTC, 12, 0);       /* LCDD13 */
141         at91_set_a_periph(AT91_PIO_PORTC, 18, 0);       /* LCDD14 */
142         at91_set_a_periph(AT91_PIO_PORTC, 19, 0);       /* LCDD15 */
143
144         at91_set_a_periph(AT91_PIO_PORTC, 20, 0);       /* LCDD26 */
145         at91_set_a_periph(AT91_PIO_PORTC, 21, 0);       /* LCDD17 */
146         at91_set_a_periph(AT91_PIO_PORTC, 22, 0);       /* LCDD18 */
147         at91_set_a_periph(AT91_PIO_PORTC, 23, 0);       /* LCDD19 */
148         at91_set_a_periph(AT91_PIO_PORTC, 24, 0);       /* LCDD20 */
149         at91_set_b_periph(AT91_PIO_PORTC, 17, 0);       /* LCDD21 */
150         at91_set_a_periph(AT91_PIO_PORTC, 26, 0);       /* LCDD22 */
151         at91_set_a_periph(AT91_PIO_PORTC, 27, 0);       /* LCDD23 */
152
153         at91_set_pio_output(AT91_PIO_PORTE, 0, 0);      /* LCD QXH */
154
155         at91_set_pio_output(AT91_PIO_PORTE, 2, 0);      /* LCD SHUT */
156         at91_set_pio_output(AT91_PIO_PORTE, 3, 1);      /* LCD TopBottom */
157         at91_set_pio_output(AT91_PIO_PORTE, 4, 0);      /* LCD REV */
158         at91_set_pio_output(AT91_PIO_PORTE, 5, 1);      /* LCD RightLeft */
159         at91_set_pio_output(AT91_PIO_PORTE, 6, 0);      /* LCD Color Mode CM */
160         at91_set_pio_output(AT91_PIO_PORTE, 7, 0);      /* LCD BGR */
161
162         at91_set_pio_output(AT91_PIO_PORTB, 9, 0);      /* LCD CC */
163
164         writel(1 << ATMEL_ID_LCDC, &pmc->pcer);
165         gd->fb_base = ATMEL_BASE_SRAM0;
166 }
167 #endif /* Config LCD */
168
169 #ifdef CONFIG_BOARD_EARLY_INIT_F
170 int board_early_init_f(void)
171 {
172         struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
173
174         /* Enable clocks for all PIOs */
175         writel((1 << ATMEL_ID_PIOA) | (1 << ATMEL_ID_PIOB) |
176                 (1 << ATMEL_ID_PIOCDE),
177                 &pmc->pcer);
178
179         at91_seriald_hw_init();
180
181         return 0;
182 }
183 #endif
184
185 int board_init(void)
186 {
187         at91_smc_t      *smc    = (at91_smc_t *) ATMEL_BASE_SMC0;
188         at91_pio_t      *pio = (at91_pio_t *) ATMEL_BASE_PIO;
189         u32             pin;
190
191         pin = 0x1F000001;
192         writel(pin, &pio->pioa.idr);
193         writel(pin, &pio->pioa.pudr);
194         writel(pin, &pio->pioa.per);
195         writel(pin, &pio->pioa.oer);
196         writel(pin, &pio->pioa.sodr);
197         writel((1 << 25), &pio->pioa.codr);
198
199         pin = 0x1F000100;
200         writel(pin, &pio->piob.idr);
201         writel(pin, &pio->piob.pudr);
202         writel(pin, &pio->piob.per);
203         writel(pin, &pio->piob.oer);
204         writel(pin, &pio->piob.codr);
205         writel((1 << 24), &pio->piob.sodr);
206
207         pin = 0x40000000;                       /* Pullup DRxD enbable */
208         writel(pin, &pio->pioc.puer);
209
210         pin = 0x0000000F;                       /* HWversion als Input */
211         writel(pin, &pio->piod.idr);
212         writel(pin, &pio->piod.puer);
213         writel(pin, &pio->piod.per);
214         writel(pin, &pio->piod.odr);
215         writel(pin, &pio->piod.owdr);
216
217         gd->bd->bi_arch_number = MACH_TYPE_VL_MA2SC;
218         /* adress of boot parameters */
219         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
220
221         writel(CONFIG_SYS_SMC0_MODE0_VAL, &smc->cs[0].setup);
222         writel(CONFIG_SYS_SMC0_CYCLE0_VAL, &smc->cs[0].cycle);
223         writel(CONFIG_SYS_SMC0_PULSE0_VAL, &smc->cs[0].pulse);
224         writel(CONFIG_SYS_SMC0_SETUP0_VAL, &smc->cs[0].setup);
225
226 #ifdef CONFIG_CMD_NAND
227         vl_ma2sc_nand_hw_init();
228 #endif
229 #ifdef CONFIG_MACB
230         vl_ma2sc_macb_hw_init();
231 #endif
232 #ifdef CONFIG_USB_OHCI_NEW
233         at91_uhp_hw_init();
234 #endif
235 #ifdef CONFIG_LCD
236         vl_ma2sc_lcd_hw_init();
237 #endif
238         return 0;
239 }
240
241 #ifdef CONFIG_MISC_INIT_R
242 int misc_init_r(void)
243 {
244         uchar   buffer[8];
245         at91_pio_t      *pio = (at91_pio_t *) ATMEL_BASE_PIO;
246         u32             pin;
247
248         buffer[0] = 0x04;
249         buffer[1] = 0x00;
250         if (i2c_write(0x68, 0x0E, 1, buffer, 2) != 0)
251                 puts("error reseting rtc clock\n\0");
252
253         /* read hardware version */
254
255         pin = (readl(&pio->piod.pdsr) & 0x0F) + 0x44;
256         printf("Board: revision %c\n", pin);
257         buffer[0] = pin;
258         buffer[1] = 0;
259         setenv("revision", (char *) buffer);
260
261         pin = 0x40000000;                       /* Pullup DRxD enbable */
262         writel(pin, &pio->pioc.puer);
263         return 0;
264 }
265 #endif
266
267 int dram_init(void)
268 {
269         gd->ram_size = get_ram_size((long *) CONFIG_SYS_SDRAM_BASE,
270                         CONFIG_SYS_SDRAM_SIZE);
271         return 0;
272 }
273
274 #ifdef CONFIG_RESET_PHY_R
275 void reset_phy(void)
276 {
277 #ifdef CONFIG_MACB
278         /*
279          * Initialize ethernet HW addr prior to starting Linux,
280          * needed for nfsroot
281          */
282         eth_init(gd->bd);
283 #endif
284 }
285 #endif
286
287 int board_eth_init(bd_t *bis)
288 {
289         int rc = 0;
290 #ifdef CONFIG_MACB
291         rc = macb_eth_initialize(0, (void *) ATMEL_BASE_EMAC, 0x01);
292 #endif
293         return rc;
294 }
295
296 #ifdef CONFIG_SYS_I2C_SOFT
297 void i2c_init_board(void)
298 {
299         u32 pin;
300
301         at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
302         at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
303         u8 sda = (1<<4);
304         u8 scl = (1<<5);
305
306         writel(1 << ATMEL_ID_PIOB, &pmc->pcer);
307         pin = sda | scl;
308         writel(pin, &pio->piob.idr);    /* Disable Interupt */
309         writel(pin, &pio->piob.pudr);
310         writel(pin, &pio->piob.per);
311         writel(pin, &pio->piob.oer);
312         writel(pin, &pio->piob.sodr);
313 }
314 #endif
315
316 void watchdog_reset(void)
317 {
318         at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
319         u32     pin = 0x1;      /* PA0 */
320
321         if ((readl(&pio->pioa.odsr) & pin) > 0)
322                 writel(pin, &pio->pioa.codr);
323         else
324                 writel(pin, &pio->pioa.sodr);
325 }
326
327 void enable_caches(void)
328 {
329 #ifndef CONFIG_SYS_DCACHE_OFF
330         dcache_enable();
331 #endif
332 }
333
334 /*---------------------------------------------------------------------------*/
335
336 int do_ledtest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
337 {
338         int rcode = 1;
339         int row;
340         int col;
341         u32 pinz;
342         u32 pins;
343         at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
344
345         at91_set_pio_output(AT91_PIO_PORTB, 8, 0);      /* LCD DIM */
346
347         pins = 0x1F000000;
348         writel(pins, &pio->pioa.idr);
349         writel(pins, &pio->pioa.pudr);
350         writel(pins, &pio->pioa.per);
351         writel(pins, &pio->pioa.oer);
352         writel(pins, &pio->pioa.sodr);
353
354         pinz = 0x1F000000;
355         writel(pinz, &pio->piob.idr);
356         writel(pinz, &pio->piob.pudr);
357         writel(pinz, &pio->piob.per);
358         writel(pinz, &pio->piob.oer);
359         writel(pinz, &pio->piob.sodr);
360
361         for (row = 0; row < 5; row++) {
362                 for (col = 0; col < 5; col++) {
363                         writel((0x01000000 << col), &pio->piob.sodr);
364                         writel((0x01000000 << row), &pio->pioa.codr);
365                         printf("LED Test %d x %d\n", row, col);
366                         udelay(1000000);
367                         writel(pinz, &pio->piob.codr);
368                         writel(pins, &pio->pioa.sodr);
369                 }
370         }
371         return rcode;
372 }
373
374 void poweroff(void)
375 {
376         watchdog_reset();
377         at91_set_pio_output(AT91_PIO_PORTA, 13, 1);     /* CAN_TX -> H */
378         udelay(100);
379         at91_set_pio_output(AT91_PIO_PORTA, 12, 0);     /* CAN_STB -> L */
380         udelay(100);
381         at91_set_pio_output(AT91_PIO_PORTA, 11, 0);     /* CAN_EN -> L */
382         udelay(100);
383         while (1)
384                 watchdog_reset();
385 }
386
387 int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc,  char * const argv[])
388 {
389         int rcode = 1;
390         poweroff();
391         return rcode;
392 }
393
394 int do_beep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
395 {
396         int i;
397         u32 freq;
398         u32 durate;
399         int rcode = 1;
400
401         freq = 1000;
402         durate = 2;
403         switch (argc) {
404         case 3:
405                 durate = simple_strtoul(argv[2], NULL, 10);
406         case 2:
407                 freq = simple_strtoul(argv[1], NULL, 10);
408         case 1:
409                 break;
410         default:
411                 cmd_usage(cmdtp);
412                 rcode = 1;
413                 break;
414         }
415         durate = durate * freq;
416         freq = 500000 / freq;
417         for (i = 0; i < durate; i++) {
418                 at91_set_pio_output(AT91_PIO_PORTB, 29, 1);     /* Sound On*/
419                 udelay(freq);
420                 at91_set_pio_output(AT91_PIO_PORTB, 29, 0);     /* Sound Off*/
421                 udelay(freq);
422         }
423         at91_set_pio_output(AT91_PIO_PORTB, 29, 0);     /* Sound Off*/
424         return rcode;
425 }
426
427 int do_keytest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
428 {
429         int rcode = 1;
430         int row;
431         u32 col;
432         u32 pinz;
433         u32 pins;
434         at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
435         at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
436
437         writel((1 << ATMEL_ID_PIOA), &pmc->pcer);
438
439         pins = 0x001F0000;
440         writel(pins, &pio->pioa.idr);
441         writel(pins, &pio->pioa.pudr);
442         writel(pins, &pio->pioa.per);
443         writel(pins, &pio->pioa.odr);
444
445         pinz = 0x000F0000;
446         writel(pinz, &pio->piob.idr);
447         writel(pinz, &pio->piob.pudr);
448         writel(pinz, &pio->piob.per);
449         writel(pinz, &pio->piob.oer);
450         writel(pinz, &pio->piob.codr);
451
452         while (1) {
453                 col = 0;
454                 for (row = 0; row < 4; row++) {
455                         writel((0x00010000 << row), &pio->piob.sodr);
456                         udelay(10000);
457                         col <<= 4;
458                         col |= ((readl(&pio->pioa.pdsr) >> 16) & 0xF) ^ 0xF ;
459                         writel(pinz, &pio->piob.codr);
460                 }
461                 printf("Matix: ");
462                 for (row = 0; row < 16; row++) {
463                         printf("%1.1d", col & 1);
464                         col >>= 1;
465                 }
466                 printf(" SP %d\r ",
467                         1 ^ (1 & (readl(&pio->piob.pdsr) >> 20)));
468                 if ((1 & (readl(&pio->pioa.pdsr) >> 1)) == 0) {
469                         /* SHUTDOWN */
470                         row = 0;
471                         while (row < 1000) {
472                                 if ((1 & (readl(&pio->pioa.pdsr) >> 1)) == 0)
473                                         row++;
474                                 udelay(100);
475                         }
476                         udelay(100000);
477                         row = 0;
478                         while (row < 1000) {
479                                 if ((1 & (readl(&pio->pioa.pdsr) >> 1)) > 0) {
480                                         row++;
481                                         udelay(1000);
482                                 }
483                         }
484                         poweroff();
485                         while (1)
486                                 ;
487                 }
488         }
489         return rcode;
490 }
491
492 /*****************************************************************************/
493
494 U_BOOT_CMD(
495         ledtest,        1,      0,      do_ledtest,
496         "test ledmatrix",
497         "\n"
498         );
499
500 U_BOOT_CMD(
501         keytest,        1,      0,      do_keytest,
502         "test keymatix and special keys, poweroff on pressing ON key",
503         "\n"
504         );
505
506 U_BOOT_CMD(
507         poweroff,       1,      0,      do_poweroff,
508         "power off",
509         "\n"
510         );
511
512 U_BOOT_CMD(
513         beep,   3,      0,      do_beep,
514         "[freq [duration]]",
515         "freq frequence of beep\nduration duration of beep\n"
516         );
517
518 /*****************************************************************************/