board.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2017-2019 NXP
  4. *
  5. * Copyright 2019 Siemens AG
  6. *
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <env.h>
  12. #include <errno.h>
  13. #include <init.h>
  14. #include <log.h>
  15. #include <netdev.h>
  16. #include <env_internal.h>
  17. #include <fsl_esdhc_imx.h>
  18. #include <i2c.h>
  19. #include <led.h>
  20. #include <pca953x.h>
  21. #include <power-domain.h>
  22. #include <asm/gpio.h>
  23. #include <asm/arch/imx8-pins.h>
  24. #include <asm/arch/iomux.h>
  25. #include <asm/arch/sci/sci.h>
  26. #include <asm/arch/sys_proto.h>
  27. #ifndef CONFIG_SPL
  28. #include <asm/arch-imx8/clock.h>
  29. #endif
  30. #include <linux/delay.h>
  31. #include "../common/factoryset.h"
  32. #define GPIO_PAD_CTRL \
  33. ((SC_PAD_CONFIG_NORMAL << PADRING_CONFIG_SHIFT) | \
  34. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  35. (SC_PAD_28FDSOI_DSE_DV_HIGH << PADRING_DSE_SHIFT) | \
  36. (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
  37. #define ENET_NORMAL_PAD_CTRL \
  38. ((SC_PAD_CONFIG_NORMAL << PADRING_CONFIG_SHIFT) | \
  39. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  40. (SC_PAD_28FDSOI_DSE_18V_10MA << PADRING_DSE_SHIFT) | \
  41. (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
  42. #define UART_PAD_CTRL \
  43. ((SC_PAD_CONFIG_OUT_IN << PADRING_CONFIG_SHIFT) | \
  44. (SC_PAD_ISO_OFF << PADRING_LPCONFIG_SHIFT) | \
  45. (SC_PAD_28FDSOI_DSE_DV_HIGH << PADRING_DSE_SHIFT) | \
  46. (SC_PAD_28FDSOI_PS_PU << PADRING_PULL_SHIFT))
  47. static iomux_cfg_t uart2_pads[] = {
  48. SC_P_UART2_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
  49. SC_P_UART2_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
  50. };
  51. static void setup_iomux_uart(void)
  52. {
  53. imx8_iomux_setup_multiple_pads(uart2_pads, ARRAY_SIZE(uart2_pads));
  54. }
  55. int board_early_init_f(void)
  56. {
  57. /* Set UART clock root to 80 MHz */
  58. sc_pm_clock_rate_t rate = SC_80MHZ;
  59. int ret;
  60. ret = sc_pm_setup_uart(SC_R_UART_0, rate);
  61. ret |= sc_pm_setup_uart(SC_R_UART_2, rate);
  62. if (ret)
  63. return ret;
  64. setup_iomux_uart();
  65. return 0;
  66. }
  67. #define ENET_PHY_RESET IMX_GPIO_NR(0, 3)
  68. #define ENET_TEST_1 IMX_GPIO_NR(0, 8)
  69. #define ENET_TEST_2 IMX_GPIO_NR(0, 9)
  70. /*#define ETH_IO_TEST*/
  71. static iomux_cfg_t enet_reset[] = {
  72. SC_P_ESAI0_SCKT | MUX_MODE_ALT(4) | MUX_PAD_CTRL(GPIO_PAD_CTRL),
  73. #ifdef ETH_IO_TEST
  74. /* GPIO0.IO08 MODE3: TXD0 */
  75. SC_P_ESAI0_TX4_RX1 | MUX_MODE_ALT(4) |
  76. MUX_PAD_CTRL(ENET_NORMAL_PAD_CTRL),
  77. /* GPIO0.IO09 MODE3: TXD1 */
  78. SC_P_ESAI0_TX5_RX0 | MUX_MODE_ALT(4) |
  79. MUX_PAD_CTRL(ENET_NORMAL_PAD_CTRL),
  80. #endif
  81. };
  82. static void enet_device_phy_reset(void)
  83. {
  84. int ret = 0;
  85. imx8_iomux_setup_multiple_pads(enet_reset, ARRAY_SIZE(enet_reset));
  86. ret = gpio_request(ENET_PHY_RESET, "enet_phy_reset");
  87. if (!ret) {
  88. gpio_direction_output(ENET_PHY_RESET, 1);
  89. gpio_set_value(ENET_PHY_RESET, 0);
  90. /* SMSC9303 TRM chapter 14.5.2 */
  91. udelay(200);
  92. gpio_set_value(ENET_PHY_RESET, 1);
  93. } else {
  94. printf("ENET RESET failed!\n");
  95. }
  96. #ifdef ETH_IO_TEST
  97. ret = gpio_request(ENET_TEST_1, "enet_test1");
  98. if (!ret) {
  99. int i;
  100. printf("ENET TEST 1!\n");
  101. for (i = 0; i < 20; i++) {
  102. gpio_direction_output(ENET_TEST_1, 1);
  103. gpio_set_value(ENET_TEST_1, 0);
  104. udelay(50);
  105. gpio_set_value(ENET_TEST_1, 1);
  106. udelay(50);
  107. }
  108. gpio_free(ENET_TEST_1);
  109. } else {
  110. printf("GPIO for ENET TEST 1 failed!\n");
  111. }
  112. ret = gpio_request(ENET_TEST_2, "enet_test2");
  113. if (!ret) {
  114. int i;
  115. printf("ENET TEST 2!\n");
  116. for (i = 0; i < 20; i++) {
  117. gpio_direction_output(ENET_TEST_2, 1);
  118. gpio_set_value(ENET_TEST_2, 0);
  119. udelay(50);
  120. gpio_set_value(ENET_TEST_2, 1);
  121. udelay(50);
  122. }
  123. gpio_free(ENET_TEST_2);
  124. } else {
  125. printf("GPIO for ENET TEST 2 failed!\n");
  126. }
  127. #endif
  128. }
  129. int setup_gpr_fec(void)
  130. {
  131. sc_ipc_t ipc_handle = -1;
  132. sc_err_t err = 0;
  133. unsigned int test;
  134. /*
  135. * TX_CLK_SEL: it controls a mux between clock coming from the pad 50M
  136. * input pin and clock generated internally to connectivity subsystem
  137. * 0: internal clock
  138. * 1: external clock ---> your choice for RMII
  139. *
  140. * CLKDIV_SEL: it controls a div by 2 on the internal clock path à
  141. * it should be don’t care when using external clock
  142. * 0: non-divided clock
  143. * 1: clock divided by 2
  144. * 50_DISABLE or 125_DISABLE:
  145. * it’s used to disable the clock tree going outside the chip
  146. * when reference clock is generated internally.
  147. * It should be don’t care when reference clock is provided
  148. * externally.
  149. * 0: clock is enabled
  150. * 1: clock is disabled
  151. *
  152. * SC_C_TXCLK = 24,
  153. * SC_C_CLKDIV = 25,
  154. * SC_C_DISABLE_50 = 26,
  155. * SC_C_DISABLE_125 = 27,
  156. */
  157. err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, 1);
  158. if (err != SC_ERR_NONE)
  159. printf("Error in setting up SC_C %d\n\r", SC_C_TXCLK);
  160. sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
  161. debug("TEST SC_C %d-->%d\n\r", SC_C_TXCLK, test);
  162. err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_CLKDIV, 0);
  163. if (err != SC_ERR_NONE)
  164. printf("Error in setting up SC_C %d\n\r", SC_C_CLKDIV);
  165. sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_CLKDIV, &test);
  166. debug("TEST SC_C %d-->%d\n\r", SC_C_CLKDIV, test);
  167. err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_DISABLE_50, 0);
  168. if (err != SC_ERR_NONE)
  169. printf("Error in setting up SC_C %d\n\r", SC_C_DISABLE_50);
  170. sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
  171. debug("TEST SC_C %d-->%d\n\r", SC_C_DISABLE_50, test);
  172. err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_DISABLE_125, 1);
  173. if (err != SC_ERR_NONE)
  174. printf("Error in setting up SC_C %d\n\r", SC_C_DISABLE_125);
  175. sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_TXCLK, &test);
  176. debug("TEST SC_C %d-->%d\n\r", SC_C_DISABLE_125, test);
  177. err = sc_misc_set_control(ipc_handle, SC_R_ENET_1, SC_C_SEL_125, 1);
  178. if (err != SC_ERR_NONE)
  179. printf("Error in setting up SC_C %d\n\r", SC_C_SEL_125);
  180. sc_misc_get_control(ipc_handle, SC_R_ENET_1, SC_C_SEL_125, &test);
  181. debug("TEST SC_C %d-->%d\n\r", SC_C_SEL_125, test);
  182. return 0;
  183. }
  184. #if IS_ENABLED(CONFIG_FEC_MXC)
  185. #include <miiphy.h>
  186. int board_phy_config(struct phy_device *phydev)
  187. {
  188. if (phydev->drv->config)
  189. phydev->drv->config(phydev);
  190. return 0;
  191. }
  192. #endif
  193. static int setup_fec(void)
  194. {
  195. setup_gpr_fec();
  196. /* Reset ENET PHY */
  197. enet_device_phy_reset();
  198. return 0;
  199. }
  200. void reset_cpu(void)
  201. {
  202. }
  203. #ifndef CONFIG_SPL_BUILD
  204. /* LED's */
  205. static int board_led_init(void)
  206. {
  207. struct udevice *bus, *dev;
  208. u8 pca_led[2] = { 0x00, 0x00 };
  209. int ret;
  210. /* init all GPIO LED's */
  211. if (IS_ENABLED(CONFIG_LED))
  212. led_default_state();
  213. /* enable all leds on PCA9552 */
  214. ret = uclass_get_device_by_seq(UCLASS_I2C, PCA9552_1_I2C_BUS, &bus);
  215. if (ret) {
  216. printf("ERROR: I2C get %d\n", ret);
  217. return ret;
  218. }
  219. ret = dm_i2c_probe(bus, PCA9552_1_I2C_ADDR, 0, &dev);
  220. if (ret) {
  221. printf("ERROR: PCA9552 probe failed\n");
  222. return ret;
  223. }
  224. ret = dm_i2c_write(dev, 0x16, pca_led, sizeof(pca_led));
  225. if (ret) {
  226. printf("ERROR: PCA9552 write failed\n");
  227. return ret;
  228. }
  229. mdelay(1);
  230. return ret;
  231. }
  232. #endif /* !CONFIG_SPL_BUILD */
  233. int checkboard(void)
  234. {
  235. puts("Board: Capricorn\n");
  236. /*
  237. * Running build_info() doesn't work with current SCFW blob.
  238. * Uncomment below call when new blob is available.
  239. */
  240. /*build_info();*/
  241. print_bootinfo();
  242. return 0;
  243. }
  244. int board_init(void)
  245. {
  246. setup_fec();
  247. return 0;
  248. }
  249. #ifdef CONFIG_OF_BOARD_SETUP
  250. int ft_board_setup(void *blob, struct bd_info *bd)
  251. {
  252. return 0;
  253. }
  254. #endif
  255. int board_mmc_get_env_dev(int devno)
  256. {
  257. return devno;
  258. }
  259. static int check_mmc_autodetect(void)
  260. {
  261. char *autodetect_str = env_get("mmcautodetect");
  262. if (autodetect_str && (strcmp(autodetect_str, "yes") == 0))
  263. return 1;
  264. return 0;
  265. }
  266. /* This should be defined for each board */
  267. __weak int mmc_map_to_kernel_blk(int dev_no)
  268. {
  269. return dev_no;
  270. }
  271. void board_late_mmc_env_init(void)
  272. {
  273. char cmd[32];
  274. char mmcblk[32];
  275. u32 dev_no = mmc_get_env_dev();
  276. if (!check_mmc_autodetect())
  277. return;
  278. env_set_ulong("mmcdev", dev_no);
  279. /* Set mmcblk env */
  280. sprintf(mmcblk, "/dev/mmcblk%dp2 rootwait rw",
  281. mmc_map_to_kernel_blk(dev_no));
  282. env_set("mmcroot", mmcblk);
  283. sprintf(cmd, "mmc dev %d", dev_no);
  284. run_command(cmd, 0);
  285. }
  286. #ifndef CONFIG_SPL_BUILD
  287. int factoryset_read_eeprom(int i2c_addr);
  288. static int load_parameters_from_factoryset(void)
  289. {
  290. int ret;
  291. ret = factoryset_read_eeprom(EEPROM_I2C_ADDR);
  292. if (ret)
  293. return ret;
  294. return factoryset_env_set();
  295. }
  296. int board_late_init(void)
  297. {
  298. env_set("sec_boot", "no");
  299. #ifdef CONFIG_AHAB_BOOT
  300. env_set("sec_boot", "yes");
  301. #endif
  302. #ifdef CONFIG_ENV_IS_IN_MMC
  303. board_late_mmc_env_init();
  304. #endif
  305. /* Init LEDs */
  306. if (board_led_init())
  307. printf("I2C LED init failed\n");
  308. /* Set environment from factoryset */
  309. if (load_parameters_from_factoryset())
  310. printf("Loading factoryset parameters failed!\n");
  311. return 0;
  312. }
  313. /* Service button */
  314. #define MAX_PIN_NUMBER 128
  315. #define BOARD_DEFAULT_BUTTON_GPIO IMX_GPIO_NR(1, 31)
  316. unsigned char get_button_state(char * const envname, unsigned char def)
  317. {
  318. int button = 0;
  319. int gpio;
  320. char *ptr_env;
  321. /* If button is not found we take default */
  322. ptr_env = env_get(envname);
  323. if (!ptr_env) {
  324. printf("Using default: %u\n", def);
  325. gpio = def;
  326. } else {
  327. gpio = (unsigned char)simple_strtoul(ptr_env, NULL, 0);
  328. if (gpio > MAX_PIN_NUMBER)
  329. gpio = def;
  330. }
  331. gpio_request(gpio, "");
  332. gpio_direction_input(gpio);
  333. if (gpio_get_value(gpio))
  334. button = 1;
  335. else
  336. button = 0;
  337. gpio_free(gpio);
  338. return button;
  339. }
  340. /*
  341. * This command returns the status of the user button on
  342. * Input - none
  343. * Returns - 1 if button is held down
  344. * 0 if button is not held down
  345. */
  346. static int
  347. do_userbutton(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  348. {
  349. int button = 0;
  350. button = get_button_state("button_usr1", BOARD_DEFAULT_BUTTON_GPIO);
  351. if (argc > 1)
  352. printf("Button state: %u\n", button);
  353. return button;
  354. }
  355. U_BOOT_CMD(
  356. usrbutton, CONFIG_SYS_MAXARGS, 2, do_userbutton,
  357. "Return the status of user button",
  358. "[print]"
  359. );
  360. #define ERST IMX_GPIO_NR(0, 3)
  361. static int
  362. do_eth_reset(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  363. {
  364. gpio_request(ERST, "ERST");
  365. gpio_direction_output(ERST, 0);
  366. udelay(200);
  367. gpio_set_value(ERST, 1);
  368. return 0;
  369. }
  370. U_BOOT_CMD(
  371. switch_rst, CONFIG_SYS_MAXARGS, 2, do_eth_reset,
  372. "Reset eth phy",
  373. "[print]"
  374. );
  375. #endif /* ! CONFIG_SPL_BUILD */