sama5d3xek.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (C) 2012 - 2013 Atmel Corporation
  3. * Bo Shen <voice.shen@atmel.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <mmc.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/sama5d3_smc.h>
  11. #include <asm/arch/at91_common.h>
  12. #include <asm/arch/at91_rstc.h>
  13. #include <asm/arch/gpio.h>
  14. #include <asm/arch/clk.h>
  15. #include <lcd.h>
  16. #include <linux/ctype.h>
  17. #include <atmel_hlcdc.h>
  18. #include <atmel_mci.h>
  19. #include <phy.h>
  20. #include <micrel.h>
  21. #include <net.h>
  22. #include <netdev.h>
  23. #include <spl.h>
  24. #include <asm/arch/atmel_mpddrc.h>
  25. #include <asm/arch/at91_wdt.h>
  26. #ifdef CONFIG_USB_GADGET_ATMEL_USBA
  27. #include <asm/arch/atmel_usba_udc.h>
  28. #endif
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /* ------------------------------------------------------------------------- */
  31. /*
  32. * Miscelaneous platform dependent initialisations
  33. */
  34. #ifdef CONFIG_NAND_ATMEL
  35. void sama5d3xek_nand_hw_init(void)
  36. {
  37. struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
  38. at91_periph_clk_enable(ATMEL_ID_SMC);
  39. /* Configure SMC CS3 for NAND/SmartMedia */
  40. writel(AT91_SMC_SETUP_NWE(2) | AT91_SMC_SETUP_NCS_WR(1) |
  41. AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(1),
  42. &smc->cs[3].setup);
  43. writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(5) |
  44. AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(5),
  45. &smc->cs[3].pulse);
  46. writel(AT91_SMC_CYCLE_NWE(8) | AT91_SMC_CYCLE_NRD(8),
  47. &smc->cs[3].cycle);
  48. writel(AT91_SMC_TIMINGS_TCLR(3) | AT91_SMC_TIMINGS_TADL(10) |
  49. AT91_SMC_TIMINGS_TAR(3) | AT91_SMC_TIMINGS_TRR(4) |
  50. AT91_SMC_TIMINGS_TWB(5) | AT91_SMC_TIMINGS_RBNSEL(3)|
  51. AT91_SMC_TIMINGS_NFSEL(1), &smc->cs[3].timings);
  52. writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
  53. AT91_SMC_MODE_EXNW_DISABLE |
  54. #ifdef CONFIG_SYS_NAND_DBW_16
  55. AT91_SMC_MODE_DBW_16 |
  56. #else /* CONFIG_SYS_NAND_DBW_8 */
  57. AT91_SMC_MODE_DBW_8 |
  58. #endif
  59. AT91_SMC_MODE_TDF_CYCLE(3),
  60. &smc->cs[3].mode);
  61. }
  62. #endif
  63. #ifndef CONFIG_SYS_NO_FLASH
  64. static void sama5d3xek_nor_hw_init(void)
  65. {
  66. struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
  67. at91_periph_clk_enable(ATMEL_ID_SMC);
  68. /* Configure SMC CS0 for NOR flash */
  69. writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
  70. AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(0),
  71. &smc->cs[0].setup);
  72. writel(AT91_SMC_PULSE_NWE(10) | AT91_SMC_PULSE_NCS_WR(11) |
  73. AT91_SMC_PULSE_NRD(10) | AT91_SMC_PULSE_NCS_RD(11),
  74. &smc->cs[0].pulse);
  75. writel(AT91_SMC_CYCLE_NWE(11) | AT91_SMC_CYCLE_NRD(14),
  76. &smc->cs[0].cycle);
  77. writel(AT91_SMC_TIMINGS_TCLR(0) | AT91_SMC_TIMINGS_TADL(0) |
  78. AT91_SMC_TIMINGS_TAR(0) | AT91_SMC_TIMINGS_TRR(0) |
  79. AT91_SMC_TIMINGS_TWB(0) | AT91_SMC_TIMINGS_RBNSEL(0)|
  80. AT91_SMC_TIMINGS_NFSEL(0), &smc->cs[0].timings);
  81. writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
  82. AT91_SMC_MODE_EXNW_DISABLE |
  83. AT91_SMC_MODE_DBW_16 |
  84. AT91_SMC_MODE_TDF_CYCLE(1),
  85. &smc->cs[0].mode);
  86. /* Address pin (A1 ~ A23) configuration */
  87. at91_set_a_periph(AT91_PIO_PORTE, 1, 0);
  88. at91_set_a_periph(AT91_PIO_PORTE, 2, 0);
  89. at91_set_a_periph(AT91_PIO_PORTE, 3, 0);
  90. at91_set_a_periph(AT91_PIO_PORTE, 4, 0);
  91. at91_set_a_periph(AT91_PIO_PORTE, 5, 0);
  92. at91_set_a_periph(AT91_PIO_PORTE, 6, 0);
  93. at91_set_a_periph(AT91_PIO_PORTE, 7, 0);
  94. at91_set_a_periph(AT91_PIO_PORTE, 8, 0);
  95. at91_set_a_periph(AT91_PIO_PORTE, 9, 0);
  96. at91_set_a_periph(AT91_PIO_PORTE, 10, 0);
  97. at91_set_a_periph(AT91_PIO_PORTE, 11, 0);
  98. at91_set_a_periph(AT91_PIO_PORTE, 12, 0);
  99. at91_set_a_periph(AT91_PIO_PORTE, 13, 0);
  100. at91_set_a_periph(AT91_PIO_PORTE, 14, 0);
  101. at91_set_a_periph(AT91_PIO_PORTE, 15, 0);
  102. at91_set_a_periph(AT91_PIO_PORTE, 16, 0);
  103. at91_set_a_periph(AT91_PIO_PORTE, 17, 0);
  104. at91_set_a_periph(AT91_PIO_PORTE, 18, 0);
  105. at91_set_a_periph(AT91_PIO_PORTE, 19, 0);
  106. at91_set_a_periph(AT91_PIO_PORTE, 20, 0);
  107. at91_set_a_periph(AT91_PIO_PORTE, 21, 0);
  108. at91_set_a_periph(AT91_PIO_PORTE, 22, 0);
  109. at91_set_a_periph(AT91_PIO_PORTE, 23, 0);
  110. /* CS0 pin configuration */
  111. at91_set_a_periph(AT91_PIO_PORTE, 26, 0);
  112. }
  113. #endif
  114. #ifdef CONFIG_CMD_USB
  115. static void sama5d3xek_usb_hw_init(void)
  116. {
  117. at91_set_pio_output(AT91_PIO_PORTD, 25, 0);
  118. at91_set_pio_output(AT91_PIO_PORTD, 26, 0);
  119. at91_set_pio_output(AT91_PIO_PORTD, 27, 0);
  120. }
  121. #endif
  122. #ifdef CONFIG_GENERIC_ATMEL_MCI
  123. static void sama5d3xek_mci_hw_init(void)
  124. {
  125. at91_mci_hw_init();
  126. at91_set_pio_output(AT91_PIO_PORTB, 10, 0); /* MCI0 Power */
  127. }
  128. #endif
  129. #ifdef CONFIG_LCD
  130. vidinfo_t panel_info = {
  131. .vl_col = 800,
  132. .vl_row = 480,
  133. .vl_clk = 24000000,
  134. .vl_bpix = LCD_BPP,
  135. .vl_tft = 1,
  136. .vl_hsync_len = 128,
  137. .vl_left_margin = 64,
  138. .vl_right_margin = 64,
  139. .vl_vsync_len = 2,
  140. .vl_upper_margin = 22,
  141. .vl_lower_margin = 21,
  142. .mmio = ATMEL_BASE_LCDC,
  143. };
  144. void lcd_enable(void)
  145. {
  146. }
  147. void lcd_disable(void)
  148. {
  149. }
  150. static void sama5d3xek_lcd_hw_init(void)
  151. {
  152. gd->fb_base = CONFIG_SAMA5D3_LCD_BASE;
  153. /* The higher 8 bit of LCD is board related */
  154. at91_set_c_periph(AT91_PIO_PORTC, 14, 0); /* LCDD16 */
  155. at91_set_c_periph(AT91_PIO_PORTC, 13, 0); /* LCDD17 */
  156. at91_set_c_periph(AT91_PIO_PORTC, 12, 0); /* LCDD18 */
  157. at91_set_c_periph(AT91_PIO_PORTC, 11, 0); /* LCDD19 */
  158. at91_set_c_periph(AT91_PIO_PORTC, 10, 0); /* LCDD20 */
  159. at91_set_c_periph(AT91_PIO_PORTC, 15, 0); /* LCDD21 */
  160. at91_set_c_periph(AT91_PIO_PORTE, 27, 0); /* LCDD22 */
  161. at91_set_c_periph(AT91_PIO_PORTE, 28, 0); /* LCDD23 */
  162. /* Configure lower 16 bit of LCD and enable clock */
  163. at91_lcd_hw_init();
  164. }
  165. #ifdef CONFIG_LCD_INFO
  166. #include <nand.h>
  167. #include <version.h>
  168. void lcd_show_board_info(void)
  169. {
  170. ulong dram_size;
  171. uint64_t nand_size;
  172. int i;
  173. char temp[32];
  174. lcd_printf("%s\n", U_BOOT_VERSION);
  175. lcd_printf("(C) 2013 ATMEL Corp\n");
  176. lcd_printf("at91@atmel.com\n");
  177. lcd_printf("%s CPU at %s MHz\n", get_cpu_name(),
  178. strmhz(temp, get_cpu_clk_rate()));
  179. dram_size = 0;
  180. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++)
  181. dram_size += gd->bd->bi_dram[i].size;
  182. nand_size = 0;
  183. #ifdef CONFIG_NAND_ATMEL
  184. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  185. nand_size += nand_info[i]->size;
  186. #endif
  187. lcd_printf("%ld MB SDRAM, %lld MB NAND\n",
  188. dram_size >> 20, nand_size >> 20);
  189. }
  190. #endif /* CONFIG_LCD_INFO */
  191. #endif /* CONFIG_LCD */
  192. int board_early_init_f(void)
  193. {
  194. at91_periph_clk_enable(ATMEL_ID_PIOA);
  195. at91_periph_clk_enable(ATMEL_ID_PIOB);
  196. at91_periph_clk_enable(ATMEL_ID_PIOC);
  197. at91_periph_clk_enable(ATMEL_ID_PIOD);
  198. at91_periph_clk_enable(ATMEL_ID_PIOE);
  199. at91_seriald_hw_init();
  200. return 0;
  201. }
  202. int board_init(void)
  203. {
  204. /* adress of boot parameters */
  205. gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
  206. #ifdef CONFIG_NAND_ATMEL
  207. sama5d3xek_nand_hw_init();
  208. #endif
  209. #ifndef CONFIG_SYS_NO_FLASH
  210. sama5d3xek_nor_hw_init();
  211. #endif
  212. #ifdef CONFIG_CMD_USB
  213. sama5d3xek_usb_hw_init();
  214. #endif
  215. #ifdef CONFIG_USB_GADGET_ATMEL_USBA
  216. at91_udp_hw_init();
  217. #endif
  218. #ifdef CONFIG_GENERIC_ATMEL_MCI
  219. sama5d3xek_mci_hw_init();
  220. #endif
  221. #ifdef CONFIG_ATMEL_SPI
  222. at91_spi0_hw_init(1 << 0);
  223. #endif
  224. #ifdef CONFIG_MACB
  225. if (has_emac())
  226. at91_macb_hw_init();
  227. if (has_gmac())
  228. at91_gmac_hw_init();
  229. #endif
  230. #ifdef CONFIG_LCD
  231. if (has_lcdc())
  232. sama5d3xek_lcd_hw_init();
  233. #endif
  234. return 0;
  235. }
  236. int dram_init(void)
  237. {
  238. gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
  239. CONFIG_SYS_SDRAM_SIZE);
  240. return 0;
  241. }
  242. int board_phy_config(struct phy_device *phydev)
  243. {
  244. /* board specific timings for GMAC */
  245. if (has_gmac()) {
  246. /* rx data delay */
  247. ksz9021_phy_extended_write(phydev,
  248. MII_KSZ9021_EXT_RGMII_RX_DATA_SKEW,
  249. 0x2222);
  250. /* tx data delay */
  251. ksz9021_phy_extended_write(phydev,
  252. MII_KSZ9021_EXT_RGMII_TX_DATA_SKEW,
  253. 0x2222);
  254. /* rx/tx clock delay */
  255. ksz9021_phy_extended_write(phydev,
  256. MII_KSZ9021_EXT_RGMII_CLOCK_SKEW,
  257. 0xf2f4);
  258. }
  259. /* always run the PHY's config routine */
  260. if (phydev->drv->config)
  261. return phydev->drv->config(phydev);
  262. return 0;
  263. }
  264. int board_eth_init(bd_t *bis)
  265. {
  266. int rc = 0;
  267. #ifdef CONFIG_MACB
  268. if (has_emac())
  269. rc = macb_eth_initialize(0, (void *)ATMEL_BASE_EMAC, 0x00);
  270. if (has_gmac())
  271. rc = macb_eth_initialize(0, (void *)ATMEL_BASE_GMAC, 0x00);
  272. #endif
  273. #ifdef CONFIG_USB_GADGET_ATMEL_USBA
  274. usba_udc_probe(&pdata);
  275. #ifdef CONFIG_USB_ETH_RNDIS
  276. usb_eth_initialize(bis);
  277. #endif
  278. #endif
  279. return rc;
  280. }
  281. #ifdef CONFIG_GENERIC_ATMEL_MCI
  282. int board_mmc_init(bd_t *bis)
  283. {
  284. int rc = 0;
  285. rc = atmel_mci_init((void *)ATMEL_BASE_MCI0);
  286. return rc;
  287. }
  288. #endif
  289. /* SPI chip select control */
  290. #ifdef CONFIG_ATMEL_SPI
  291. #include <spi.h>
  292. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  293. {
  294. return bus == 0 && cs < 4;
  295. }
  296. void spi_cs_activate(struct spi_slave *slave)
  297. {
  298. switch (slave->cs) {
  299. case 0:
  300. at91_set_pio_output(AT91_PIO_PORTD, 13, 0);
  301. case 1:
  302. at91_set_pio_output(AT91_PIO_PORTD, 14, 0);
  303. case 2:
  304. at91_set_pio_output(AT91_PIO_PORTD, 15, 0);
  305. case 3:
  306. at91_set_pio_output(AT91_PIO_PORTD, 16, 0);
  307. default:
  308. break;
  309. }
  310. }
  311. void spi_cs_deactivate(struct spi_slave *slave)
  312. {
  313. switch (slave->cs) {
  314. case 0:
  315. at91_set_pio_output(AT91_PIO_PORTD, 13, 1);
  316. case 1:
  317. at91_set_pio_output(AT91_PIO_PORTD, 14, 1);
  318. case 2:
  319. at91_set_pio_output(AT91_PIO_PORTD, 15, 1);
  320. case 3:
  321. at91_set_pio_output(AT91_PIO_PORTD, 16, 1);
  322. default:
  323. break;
  324. }
  325. }
  326. #endif /* CONFIG_ATMEL_SPI */
  327. #ifdef CONFIG_BOARD_LATE_INIT
  328. int board_late_init(void)
  329. {
  330. #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
  331. const int MAX_STR_LEN = 32;
  332. char name[MAX_STR_LEN], *p;
  333. int i;
  334. strncpy(name, get_cpu_name(), MAX_STR_LEN);
  335. for (i = 0, p = name; (*p) && (i < MAX_STR_LEN); p++, i++)
  336. *p = tolower(*p);
  337. strcat(name, "ek.dtb");
  338. setenv("dtb_name", name);
  339. #endif
  340. return 0;
  341. }
  342. #endif
  343. /* SPL */
  344. #ifdef CONFIG_SPL_BUILD
  345. void spl_board_init(void)
  346. {
  347. #ifdef CONFIG_SYS_USE_MMC
  348. sama5d3xek_mci_hw_init();
  349. #elif CONFIG_SYS_USE_NANDFLASH
  350. sama5d3xek_nand_hw_init();
  351. #elif CONFIG_SYS_USE_SERIALFLASH
  352. at91_spi0_hw_init(1 << 0);
  353. #endif
  354. }
  355. static void ddr2_conf(struct atmel_mpddrc_config *ddr2)
  356. {
  357. ddr2->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR2_SDRAM);
  358. ddr2->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
  359. ATMEL_MPDDRC_CR_NR_ROW_14 |
  360. ATMEL_MPDDRC_CR_CAS_DDR_CAS3 |
  361. ATMEL_MPDDRC_CR_ENRDM_ON |
  362. ATMEL_MPDDRC_CR_NB_8BANKS |
  363. ATMEL_MPDDRC_CR_NDQS_DISABLED |
  364. ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
  365. ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
  366. /*
  367. * As the DDR2-SDRAm device requires a refresh time is 7.8125us
  368. * when DDR run at 133MHz, so it needs (7.8125us * 133MHz / 10^9) clocks
  369. */
  370. ddr2->rtr = 0x411;
  371. ddr2->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
  372. 2 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
  373. 2 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
  374. 8 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
  375. 2 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
  376. 2 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
  377. 2 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
  378. 2 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
  379. ddr2->tpr1 = (2 << ATMEL_MPDDRC_TPR1_TXP_OFFSET |
  380. 200 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
  381. 28 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
  382. 26 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET);
  383. ddr2->tpr2 = (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET |
  384. 2 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
  385. 2 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
  386. 7 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
  387. 8 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET);
  388. }
  389. void mem_init(void)
  390. {
  391. struct atmel_mpddrc_config ddr2;
  392. ddr2_conf(&ddr2);
  393. /* Enable MPDDR clock */
  394. at91_periph_clk_enable(ATMEL_ID_MPDDRC);
  395. at91_system_clk_enable(AT91_PMC_DDR);
  396. /* DDRAM2 Controller initialize */
  397. ddr2_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddr2);
  398. }
  399. void at91_pmc_init(void)
  400. {
  401. u32 tmp;
  402. tmp = AT91_PMC_PLLAR_29 |
  403. AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
  404. AT91_PMC_PLLXR_MUL(43) |
  405. AT91_PMC_PLLXR_DIV(1);
  406. at91_plla_init(tmp);
  407. at91_pllicpr_init(AT91_PMC_IPLL_PLLA(0x3));
  408. tmp = AT91_PMC_MCKR_MDIV_4 |
  409. AT91_PMC_MCKR_CSS_PLLA;
  410. at91_mck_init(tmp);
  411. }
  412. #endif