board.c 11 KB

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