board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * board.c
  4. *
  5. * Common board functions for AM33XX based boards
  6. *
  7. * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <debug_uart.h>
  12. #include <errno.h>
  13. #include <ns16550.h>
  14. #include <spl.h>
  15. #include <asm/arch/cpu.h>
  16. #include <asm/arch/hardware.h>
  17. #include <asm/arch/omap.h>
  18. #include <asm/arch/ddr_defs.h>
  19. #include <asm/arch/clock.h>
  20. #include <asm/arch/gpio.h>
  21. #include <asm/arch/mem.h>
  22. #include <asm/arch/mmc_host_def.h>
  23. #include <asm/arch/sys_proto.h>
  24. #include <asm/io.h>
  25. #include <asm/emif.h>
  26. #include <asm/gpio.h>
  27. #include <asm/omap_common.h>
  28. #include <i2c.h>
  29. #include <miiphy.h>
  30. #include <cpsw.h>
  31. #include <linux/errno.h>
  32. #include <linux/compiler.h>
  33. #include <linux/usb/ch9.h>
  34. #include <linux/usb/gadget.h>
  35. #include <linux/usb/musb.h>
  36. #include <asm/omap_musb.h>
  37. #include <asm/davinci_rtc.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. int dram_init(void)
  40. {
  41. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  42. sdram_init();
  43. #endif
  44. /* dram_init must store complete ramsize in gd->ram_size */
  45. gd->ram_size = get_ram_size(
  46. (void *)CONFIG_SYS_SDRAM_BASE,
  47. CONFIG_MAX_RAM_BANK_SIZE);
  48. return 0;
  49. }
  50. int dram_init_banksize(void)
  51. {
  52. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  53. gd->bd->bi_dram[0].size = gd->ram_size;
  54. return 0;
  55. }
  56. #if !CONFIG_IS_ENABLED(OF_CONTROL)
  57. static const struct ns16550_platdata am33xx_serial[] = {
  58. { .base = CONFIG_SYS_NS16550_COM1, .reg_shift = 2,
  59. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  60. # ifdef CONFIG_SYS_NS16550_COM2
  61. { .base = CONFIG_SYS_NS16550_COM2, .reg_shift = 2,
  62. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  63. # ifdef CONFIG_SYS_NS16550_COM3
  64. { .base = CONFIG_SYS_NS16550_COM3, .reg_shift = 2,
  65. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  66. { .base = CONFIG_SYS_NS16550_COM4, .reg_shift = 2,
  67. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  68. { .base = CONFIG_SYS_NS16550_COM5, .reg_shift = 2,
  69. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  70. { .base = CONFIG_SYS_NS16550_COM6, .reg_shift = 2,
  71. .clock = CONFIG_SYS_NS16550_CLK, .fcr = UART_FCR_DEFVAL, },
  72. # endif
  73. # endif
  74. };
  75. U_BOOT_DEVICES(am33xx_uarts) = {
  76. { "ns16550_serial", &am33xx_serial[0] },
  77. # ifdef CONFIG_SYS_NS16550_COM2
  78. { "ns16550_serial", &am33xx_serial[1] },
  79. # ifdef CONFIG_SYS_NS16550_COM3
  80. { "ns16550_serial", &am33xx_serial[2] },
  81. { "ns16550_serial", &am33xx_serial[3] },
  82. { "ns16550_serial", &am33xx_serial[4] },
  83. { "ns16550_serial", &am33xx_serial[5] },
  84. # endif
  85. # endif
  86. };
  87. #ifdef CONFIG_DM_GPIO
  88. static const struct omap_gpio_platdata am33xx_gpio[] = {
  89. { 0, AM33XX_GPIO0_BASE },
  90. { 1, AM33XX_GPIO1_BASE },
  91. { 2, AM33XX_GPIO2_BASE },
  92. { 3, AM33XX_GPIO3_BASE },
  93. #ifdef CONFIG_AM43XX
  94. { 4, AM33XX_GPIO4_BASE },
  95. { 5, AM33XX_GPIO5_BASE },
  96. #endif
  97. };
  98. U_BOOT_DEVICES(am33xx_gpios) = {
  99. { "gpio_omap", &am33xx_gpio[0] },
  100. { "gpio_omap", &am33xx_gpio[1] },
  101. { "gpio_omap", &am33xx_gpio[2] },
  102. { "gpio_omap", &am33xx_gpio[3] },
  103. #ifdef CONFIG_AM43XX
  104. { "gpio_omap", &am33xx_gpio[4] },
  105. { "gpio_omap", &am33xx_gpio[5] },
  106. #endif
  107. };
  108. #endif
  109. #endif
  110. #ifndef CONFIG_DM_GPIO
  111. static const struct gpio_bank gpio_bank_am33xx[] = {
  112. { (void *)AM33XX_GPIO0_BASE },
  113. { (void *)AM33XX_GPIO1_BASE },
  114. { (void *)AM33XX_GPIO2_BASE },
  115. { (void *)AM33XX_GPIO3_BASE },
  116. #ifdef CONFIG_AM43XX
  117. { (void *)AM33XX_GPIO4_BASE },
  118. { (void *)AM33XX_GPIO5_BASE },
  119. #endif
  120. };
  121. const struct gpio_bank *const omap_gpio_bank = gpio_bank_am33xx;
  122. #endif
  123. #if defined(CONFIG_MMC_OMAP_HS)
  124. int cpu_mmc_init(bd_t *bis)
  125. {
  126. int ret;
  127. ret = omap_mmc_init(0, 0, 0, -1, -1);
  128. if (ret)
  129. return ret;
  130. return omap_mmc_init(1, 0, 0, -1, -1);
  131. }
  132. #endif
  133. /*
  134. * RTC only with DDR in self-refresh mode magic value, checked against during
  135. * boot to see if we have a valid config. This should be in sync with the value
  136. * that will be in drivers/soc/ti/pm33xx.c.
  137. */
  138. #define RTC_MAGIC_VAL 0x8cd0
  139. /* Board type field bit shift for RTC only with DDR in self-refresh mode */
  140. #define RTC_BOARD_TYPE_SHIFT 16
  141. /* AM33XX has two MUSB controllers which can be host or gadget */
  142. #if (defined(CONFIG_USB_MUSB_GADGET) || defined(CONFIG_USB_MUSB_HOST)) && \
  143. (defined(CONFIG_AM335X_USB0) || defined(CONFIG_AM335X_USB1)) && \
  144. (!defined(CONFIG_DM_USB))
  145. static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  146. /* USB 2.0 PHY Control */
  147. #define CM_PHY_PWRDN (1 << 0)
  148. #define CM_PHY_OTG_PWRDN (1 << 1)
  149. #define OTGVDET_EN (1 << 19)
  150. #define OTGSESSENDEN (1 << 20)
  151. static void am33xx_usb_set_phy_power(u8 on, u32 *reg_addr)
  152. {
  153. if (on) {
  154. clrsetbits_le32(reg_addr, CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
  155. OTGVDET_EN | OTGSESSENDEN);
  156. } else {
  157. clrsetbits_le32(reg_addr, 0, CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
  158. }
  159. }
  160. static struct musb_hdrc_config musb_config = {
  161. .multipoint = 1,
  162. .dyn_fifo = 1,
  163. .num_eps = 16,
  164. .ram_bits = 12,
  165. };
  166. #ifdef CONFIG_AM335X_USB0
  167. static void am33xx_otg0_set_phy_power(struct udevice *dev, u8 on)
  168. {
  169. am33xx_usb_set_phy_power(on, &cdev->usb_ctrl0);
  170. }
  171. struct omap_musb_board_data otg0_board_data = {
  172. .set_phy_power = am33xx_otg0_set_phy_power,
  173. };
  174. static struct musb_hdrc_platform_data otg0_plat = {
  175. .mode = CONFIG_AM335X_USB0_MODE,
  176. .config = &musb_config,
  177. .power = 50,
  178. .platform_ops = &musb_dsps_ops,
  179. .board_data = &otg0_board_data,
  180. };
  181. #endif
  182. #ifdef CONFIG_AM335X_USB1
  183. static void am33xx_otg1_set_phy_power(struct udevice *dev, u8 on)
  184. {
  185. am33xx_usb_set_phy_power(on, &cdev->usb_ctrl1);
  186. }
  187. struct omap_musb_board_data otg1_board_data = {
  188. .set_phy_power = am33xx_otg1_set_phy_power,
  189. };
  190. static struct musb_hdrc_platform_data otg1_plat = {
  191. .mode = CONFIG_AM335X_USB1_MODE,
  192. .config = &musb_config,
  193. .power = 50,
  194. .platform_ops = &musb_dsps_ops,
  195. .board_data = &otg1_board_data,
  196. };
  197. #endif
  198. int arch_misc_init(void)
  199. {
  200. #ifdef CONFIG_AM335X_USB0
  201. musb_register(&otg0_plat, &otg0_board_data,
  202. (void *)USB0_OTG_BASE);
  203. #endif
  204. #ifdef CONFIG_AM335X_USB1
  205. musb_register(&otg1_plat, &otg1_board_data,
  206. (void *)USB1_OTG_BASE);
  207. #endif
  208. return 0;
  209. }
  210. #else /* CONFIG_USB_MUSB_* && CONFIG_AM335X_USB* && !CONFIG_DM_USB */
  211. int arch_misc_init(void)
  212. {
  213. struct udevice *dev;
  214. int ret;
  215. ret = uclass_first_device(UCLASS_MISC, &dev);
  216. if (ret || !dev)
  217. return ret;
  218. #if defined(CONFIG_DM_ETH) && defined(CONFIG_USB_ETHER)
  219. ret = usb_ether_init();
  220. if (ret) {
  221. pr_err("USB ether init failed\n");
  222. return ret;
  223. }
  224. #endif
  225. return 0;
  226. }
  227. #endif /* CONFIG_USB_MUSB_* && CONFIG_AM335X_USB* && !CONFIG_DM_USB */
  228. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  229. #if defined(CONFIG_SPL_AM33XX_ENABLE_RTC32K_OSC) || \
  230. (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RTC_DDR_SUPPORT))
  231. static void rtc32k_unlock(struct davinci_rtc *rtc)
  232. {
  233. /*
  234. * Unlock the RTC's registers. For more details please see the
  235. * RTC_SS section of the TRM. In order to unlock we need to
  236. * write these specific values (keys) in this order.
  237. */
  238. writel(RTC_KICK0R_WE, &rtc->kick0r);
  239. writel(RTC_KICK1R_WE, &rtc->kick1r);
  240. }
  241. #endif
  242. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RTC_DDR_SUPPORT)
  243. /*
  244. * Write contents of the RTC_SCRATCH1 register based on board type
  245. * Two things are passed
  246. * on. First 16 bits (0:15) are written with RTC_MAGIC value. Once the
  247. * control gets to kernel, kernel reads the scratchpad register and gets to
  248. * know that bootloader has rtc_only support.
  249. *
  250. * Second important thing is the board type (16:31). This is needed in the
  251. * rtc_only boot where in we want to avoid costly i2c reads to eeprom to
  252. * identify the board type and we go ahead and copy the board strings to
  253. * am43xx_board_name.
  254. */
  255. void update_rtc_magic(void)
  256. {
  257. struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
  258. u32 magic = RTC_MAGIC_VAL;
  259. magic |= (rtc_only_get_board_type() << RTC_BOARD_TYPE_SHIFT);
  260. rtc32k_unlock(rtc);
  261. /* write magic */
  262. writel(magic, &rtc->scratch1);
  263. }
  264. #endif
  265. /*
  266. * In the case of non-SPL based booting we'll want to call these
  267. * functions a tiny bit later as it will require gd to be set and cleared
  268. * and that's not true in s_init in this case so we cannot do it there.
  269. */
  270. int board_early_init_f(void)
  271. {
  272. prcm_init();
  273. set_mux_conf_regs();
  274. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RTC_DDR_SUPPORT)
  275. update_rtc_magic();
  276. #endif
  277. return 0;
  278. }
  279. /*
  280. * This function is the place to do per-board things such as ramp up the
  281. * MPU clock frequency.
  282. */
  283. __weak void am33xx_spl_board_init(void)
  284. {
  285. }
  286. #if defined(CONFIG_SPL_AM33XX_ENABLE_RTC32K_OSC)
  287. static void rtc32k_enable(void)
  288. {
  289. struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
  290. rtc32k_unlock(rtc);
  291. /* Enable the RTC 32K OSC by setting bits 3 and 6. */
  292. writel((1 << 3) | (1 << 6), &rtc->osc);
  293. }
  294. #endif
  295. static void uart_soft_reset(void)
  296. {
  297. struct uart_sys *uart_base = (struct uart_sys *)DEFAULT_UART_BASE;
  298. u32 regval;
  299. regval = readl(&uart_base->uartsyscfg);
  300. regval |= UART_RESET;
  301. writel(regval, &uart_base->uartsyscfg);
  302. while ((readl(&uart_base->uartsyssts) &
  303. UART_CLK_RUNNING_MASK) != UART_CLK_RUNNING_MASK)
  304. ;
  305. /* Disable smart idle */
  306. regval = readl(&uart_base->uartsyscfg);
  307. regval |= UART_SMART_IDLE_EN;
  308. writel(regval, &uart_base->uartsyscfg);
  309. }
  310. static void watchdog_disable(void)
  311. {
  312. struct wd_timer *wdtimer = (struct wd_timer *)WDT_BASE;
  313. writel(0xAAAA, &wdtimer->wdtwspr);
  314. while (readl(&wdtimer->wdtwwps) != 0x0)
  315. ;
  316. writel(0x5555, &wdtimer->wdtwspr);
  317. while (readl(&wdtimer->wdtwwps) != 0x0)
  318. ;
  319. }
  320. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RTC_DDR_SUPPORT)
  321. /*
  322. * Check if we are executing rtc-only + DDR mode, and resume from it if needed
  323. */
  324. static void rtc_only(void)
  325. {
  326. struct davinci_rtc *rtc = (struct davinci_rtc *)RTC_BASE;
  327. struct prm_device_inst *prm_device =
  328. (struct prm_device_inst *)PRM_DEVICE_INST;
  329. u32 scratch1;
  330. void (*resume_func)(void);
  331. scratch1 = readl(&rtc->scratch1);
  332. /*
  333. * Check RTC scratch against RTC_MAGIC_VAL, RTC_MAGIC_VAL is only
  334. * written to this register when we want to wake up from RTC only
  335. * with DDR in self-refresh mode. Contents of the RTC_SCRATCH1:
  336. * bits 0-15: RTC_MAGIC_VAL
  337. * bits 16-31: board type (needed for sdram_init)
  338. */
  339. if ((scratch1 & 0xffff) != RTC_MAGIC_VAL)
  340. return;
  341. rtc32k_unlock(rtc);
  342. /* Clear RTC magic */
  343. writel(0, &rtc->scratch1);
  344. /*
  345. * Update board type based on value stored on RTC_SCRATCH1, this
  346. * is done so that we don't need to read the board type from eeprom
  347. * over i2c bus which is expensive
  348. */
  349. rtc_only_update_board_type(scratch1 >> RTC_BOARD_TYPE_SHIFT);
  350. /*
  351. * Enable EMIF_DEVOFF in PRCM_PRM_EMIF_CTRL to indicate to EMIF we
  352. * are resuming from self-refresh. This avoids an unnecessary re-init
  353. * of the DDR. The re-init takes time and we would need to wait for
  354. * it to complete before accessing DDR to avoid L3 NOC errors.
  355. */
  356. writel(EMIF_CTRL_DEVOFF, &prm_device->emif_ctrl);
  357. rtc_only_prcm_init();
  358. sdram_init();
  359. /* Disable EMIF_DEVOFF for normal operation and to exit self-refresh */
  360. writel(0, &prm_device->emif_ctrl);
  361. resume_func = (void *)readl(&rtc->scratch0);
  362. if (resume_func)
  363. resume_func();
  364. }
  365. #endif
  366. void s_init(void)
  367. {
  368. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RTC_DDR_SUPPORT)
  369. rtc_only();
  370. #endif
  371. }
  372. void early_system_init(void)
  373. {
  374. /*
  375. * The ROM will only have set up sufficient pinmux to allow for the
  376. * first 4KiB NOR to be read, we must finish doing what we know of
  377. * the NOR mux in this space in order to continue.
  378. */
  379. #ifdef CONFIG_NOR_BOOT
  380. enable_norboot_pin_mux();
  381. #endif
  382. watchdog_disable();
  383. set_uart_mux_conf();
  384. setup_early_clocks();
  385. uart_soft_reset();
  386. #ifdef CONFIG_SPL_BUILD
  387. /*
  388. * Save the boot parameters passed from romcode.
  389. * We cannot delay the saving further than this,
  390. * to prevent overwrites.
  391. */
  392. save_omap_boot_params();
  393. #endif
  394. #ifdef CONFIG_DEBUG_UART_OMAP
  395. debug_uart_init();
  396. #endif
  397. #ifdef CONFIG_TI_I2C_BOARD_DETECT
  398. do_board_detect();
  399. #endif
  400. #ifdef CONFIG_SPL_BUILD
  401. spl_early_init();
  402. #endif
  403. #if defined(CONFIG_SPL_AM33XX_ENABLE_RTC32K_OSC)
  404. /* Enable RTC32K clock */
  405. rtc32k_enable();
  406. #endif
  407. }
  408. #ifdef CONFIG_SPL_BUILD
  409. void board_init_f(ulong dummy)
  410. {
  411. hw_data_init();
  412. early_system_init();
  413. board_early_init_f();
  414. sdram_init();
  415. /* dram_init must store complete ramsize in gd->ram_size */
  416. gd->ram_size = get_ram_size(
  417. (void *)CONFIG_SYS_SDRAM_BASE,
  418. CONFIG_MAX_RAM_BANK_SIZE);
  419. }
  420. #endif
  421. #endif
  422. int arch_cpu_init_dm(void)
  423. {
  424. hw_data_init();
  425. #ifndef CONFIG_SKIP_LOWLEVEL_INIT
  426. early_system_init();
  427. #endif
  428. return 0;
  429. }