dh_imx6.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * DHCOM DH-iMX6 PDK board support
  4. *
  5. * Copyright (C) 2017 Marek Vasut <marex@denx.de>
  6. */
  7. #include <common.h>
  8. #include <asm/arch/clock.h>
  9. #include <asm/arch/crm_regs.h>
  10. #include <asm/arch/imx-regs.h>
  11. #include <asm/arch/iomux.h>
  12. #include <asm/arch/mx6-pins.h>
  13. #include <asm/arch/sys_proto.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <asm/mach-imx/boot_mode.h>
  17. #include <asm/mach-imx/iomux-v3.h>
  18. #include <asm/mach-imx/mxc_i2c.h>
  19. #include <asm/mach-imx/sata.h>
  20. #include <environment.h>
  21. #include <errno.h>
  22. #include <fsl_esdhc.h>
  23. #include <fuse.h>
  24. #include <i2c.h>
  25. #include <miiphy.h>
  26. #include <mmc.h>
  27. #include <net.h>
  28. #include <netdev.h>
  29. #include <usb.h>
  30. #include <usb/ehci-ci.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #define I2C_PAD_CTRL \
  33. (PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED | PAD_CTL_DSE_40ohm | \
  34. PAD_CTL_HYS | PAD_CTL_ODE | PAD_CTL_SRE_FAST)
  35. #define EEPROM_I2C_ADDRESS 0x50
  36. #define PC MUX_PAD_CTRL(I2C_PAD_CTRL)
  37. static struct i2c_pads_info dh6sdl_i2c_pad_info0 = {
  38. .scl = {
  39. .i2c_mode = MX6DL_PAD_EIM_D21__I2C1_SCL | PC,
  40. .gpio_mode = MX6DL_PAD_EIM_D21__GPIO3_IO21 | PC,
  41. .gp = IMX_GPIO_NR(3, 21)
  42. },
  43. .sda = {
  44. .i2c_mode = MX6DL_PAD_EIM_D28__I2C1_SDA | PC,
  45. .gpio_mode = MX6DL_PAD_EIM_D28__GPIO3_IO28 | PC,
  46. .gp = IMX_GPIO_NR(3, 28)
  47. }
  48. };
  49. static struct i2c_pads_info dh6sdl_i2c_pad_info1 = {
  50. .scl = {
  51. .i2c_mode = MX6DL_PAD_KEY_COL3__I2C2_SCL | PC,
  52. .gpio_mode = MX6DL_PAD_KEY_COL3__GPIO4_IO12 | PC,
  53. .gp = IMX_GPIO_NR(4, 12)
  54. },
  55. .sda = {
  56. .i2c_mode = MX6DL_PAD_KEY_ROW3__I2C2_SDA | PC,
  57. .gpio_mode = MX6DL_PAD_KEY_ROW3__GPIO4_IO13 | PC,
  58. .gp = IMX_GPIO_NR(4, 13)
  59. }
  60. };
  61. static struct i2c_pads_info dh6sdl_i2c_pad_info2 = {
  62. .scl = {
  63. .i2c_mode = MX6DL_PAD_GPIO_3__I2C3_SCL | PC,
  64. .gpio_mode = MX6DL_PAD_GPIO_3__GPIO1_IO03 | PC,
  65. .gp = IMX_GPIO_NR(1, 3)
  66. },
  67. .sda = {
  68. .i2c_mode = MX6DL_PAD_GPIO_6__I2C3_SDA | PC,
  69. .gpio_mode = MX6DL_PAD_GPIO_6__GPIO1_IO06 | PC,
  70. .gp = IMX_GPIO_NR(1, 6)
  71. }
  72. };
  73. static struct i2c_pads_info dh6dq_i2c_pad_info0 = {
  74. .scl = {
  75. .i2c_mode = MX6Q_PAD_EIM_D21__I2C1_SCL | PC,
  76. .gpio_mode = MX6Q_PAD_EIM_D21__GPIO3_IO21 | PC,
  77. .gp = IMX_GPIO_NR(3, 21)
  78. },
  79. .sda = {
  80. .i2c_mode = MX6Q_PAD_EIM_D28__I2C1_SDA | PC,
  81. .gpio_mode = MX6Q_PAD_EIM_D28__GPIO3_IO28 | PC,
  82. .gp = IMX_GPIO_NR(3, 28)
  83. }
  84. };
  85. static struct i2c_pads_info dh6dq_i2c_pad_info1 = {
  86. .scl = {
  87. .i2c_mode = MX6Q_PAD_KEY_COL3__I2C2_SCL | PC,
  88. .gpio_mode = MX6Q_PAD_KEY_COL3__GPIO4_IO12 | PC,
  89. .gp = IMX_GPIO_NR(4, 12)
  90. },
  91. .sda = {
  92. .i2c_mode = MX6Q_PAD_KEY_ROW3__I2C2_SDA | PC,
  93. .gpio_mode = MX6Q_PAD_KEY_ROW3__GPIO4_IO13 | PC,
  94. .gp = IMX_GPIO_NR(4, 13)
  95. }
  96. };
  97. static struct i2c_pads_info dh6dq_i2c_pad_info2 = {
  98. .scl = {
  99. .i2c_mode = MX6Q_PAD_GPIO_3__I2C3_SCL | PC,
  100. .gpio_mode = MX6Q_PAD_GPIO_3__GPIO1_IO03 | PC,
  101. .gp = IMX_GPIO_NR(1, 3)
  102. },
  103. .sda = {
  104. .i2c_mode = MX6Q_PAD_GPIO_6__I2C3_SDA | PC,
  105. .gpio_mode = MX6Q_PAD_GPIO_6__GPIO1_IO06 | PC,
  106. .gp = IMX_GPIO_NR(1, 6)
  107. }
  108. };
  109. int dram_init(void)
  110. {
  111. gd->ram_size = imx_ddr_size();
  112. return 0;
  113. }
  114. /*
  115. * Do not overwrite the console
  116. * Use always serial for U-Boot console
  117. */
  118. int overwrite_console(void)
  119. {
  120. return 1;
  121. }
  122. #ifdef CONFIG_FEC_MXC
  123. static void eth_phy_reset(void)
  124. {
  125. /* Reset PHY */
  126. gpio_direction_output(IMX_GPIO_NR(5, 0) , 0);
  127. udelay(500);
  128. gpio_set_value(IMX_GPIO_NR(5, 0), 1);
  129. /* Enable VIO */
  130. gpio_direction_output(IMX_GPIO_NR(1, 7) , 0);
  131. /*
  132. * KSZ9021 PHY needs at least 10 mSec after PHY reset
  133. * is released to stabilize
  134. */
  135. mdelay(10);
  136. }
  137. static int setup_fec_clock(void)
  138. {
  139. struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
  140. /* set gpr1[21] to select anatop clock */
  141. clrsetbits_le32(&iomuxc_regs->gpr[1], 0x1 << 21, 0x1 << 21);
  142. return enable_fec_anatop_clock(0, ENET_50MHZ);
  143. }
  144. int board_eth_init(bd_t *bis)
  145. {
  146. uint32_t base = IMX_FEC_BASE;
  147. struct mii_dev *bus = NULL;
  148. struct phy_device *phydev = NULL;
  149. setup_fec_clock();
  150. eth_phy_reset();
  151. bus = fec_get_miibus(base, -1);
  152. if (!bus)
  153. return -EINVAL;
  154. /* Scan PHY 0 */
  155. phydev = phy_find_by_mask(bus, 0xf, PHY_INTERFACE_MODE_RGMII);
  156. if (!phydev) {
  157. printf("Ethernet PHY not found!\n");
  158. return -EINVAL;
  159. }
  160. return fec_probe(bis, -1, base, bus, phydev);
  161. }
  162. #endif
  163. #ifdef CONFIG_FSL_ESDHC
  164. #define USDHC2_CD_GPIO IMX_GPIO_NR(6, 16)
  165. #define USDHC3_CD_GPIO IMX_GPIO_NR(7, 8)
  166. static struct fsl_esdhc_cfg usdhc_cfg[3] = {
  167. { USDHC2_BASE_ADDR },
  168. { USDHC3_BASE_ADDR },
  169. { USDHC4_BASE_ADDR },
  170. };
  171. int board_mmc_getcd(struct mmc *mmc)
  172. {
  173. struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv;
  174. switch (cfg->esdhc_base) {
  175. case USDHC2_BASE_ADDR:
  176. return gpio_get_value(USDHC2_CD_GPIO);
  177. case USDHC3_BASE_ADDR:
  178. return !gpio_get_value(USDHC3_CD_GPIO);
  179. case USDHC4_BASE_ADDR:
  180. return 1; /* eMMC/uSDHC4 is always present */
  181. }
  182. return 0;
  183. }
  184. int board_mmc_init(bd_t *bis)
  185. {
  186. int i, ret;
  187. /*
  188. * According to the board_mmc_init() the following map is done:
  189. * (U-Boot device node) (Physical Port)
  190. * mmc0 SD interface
  191. * mmc1 micro SD
  192. * mmc2 eMMC
  193. */
  194. gpio_direction_input(USDHC2_CD_GPIO);
  195. gpio_direction_input(USDHC3_CD_GPIO);
  196. usdhc_cfg[0].sdhc_clk = mxc_get_clock(MXC_ESDHC2_CLK);
  197. usdhc_cfg[1].sdhc_clk = mxc_get_clock(MXC_ESDHC3_CLK);
  198. usdhc_cfg[2].sdhc_clk = mxc_get_clock(MXC_ESDHC4_CLK);
  199. for (i = 0; i < CONFIG_SYS_FSL_USDHC_NUM; i++) {
  200. ret = fsl_esdhc_initialize(bis, &usdhc_cfg[i]);
  201. if (ret)
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. #endif
  207. #ifdef CONFIG_USB_EHCI_MX6
  208. static void setup_usb(void)
  209. {
  210. /*
  211. * Set daisy chain for otg_pin_id on MX6Q.
  212. * For MX6DL, this bit is reserved.
  213. */
  214. imx_iomux_set_gpr_register(1, 13, 1, 0);
  215. }
  216. int board_usb_phy_mode(int port)
  217. {
  218. if (port == 1)
  219. return USB_INIT_HOST;
  220. else
  221. return USB_INIT_DEVICE;
  222. }
  223. int board_ehci_power(int port, int on)
  224. {
  225. switch (port) {
  226. case 0:
  227. break;
  228. case 1:
  229. gpio_direction_output(IMX_GPIO_NR(3, 31), !!on);
  230. break;
  231. default:
  232. printf("MXC USB port %d not yet supported\n", port);
  233. return -EINVAL;
  234. }
  235. return 0;
  236. }
  237. #endif
  238. static int setup_dhcom_mac_from_fuse(void)
  239. {
  240. unsigned char enetaddr[6];
  241. int ret;
  242. ret = eth_env_get_enetaddr("ethaddr", enetaddr);
  243. if (ret) /* ethaddr is already set */
  244. return 0;
  245. imx_get_mac_from_fuse(0, enetaddr);
  246. if (is_valid_ethaddr(enetaddr)) {
  247. eth_env_set_enetaddr("ethaddr", enetaddr);
  248. return 0;
  249. }
  250. ret = i2c_set_bus_num(2);
  251. if (ret) {
  252. printf("Error switching I2C bus!\n");
  253. return ret;
  254. }
  255. ret = i2c_read(EEPROM_I2C_ADDRESS, 0xfa, 0x1, enetaddr, 0x6);
  256. if (ret) {
  257. printf("Error reading configuration EEPROM!\n");
  258. return ret;
  259. }
  260. if (is_valid_ethaddr(enetaddr))
  261. eth_env_set_enetaddr("ethaddr", enetaddr);
  262. return 0;
  263. }
  264. int board_early_init_f(void)
  265. {
  266. #ifdef CONFIG_USB_EHCI_MX6
  267. setup_usb();
  268. #endif
  269. return 0;
  270. }
  271. #ifdef CONFIG_MXC_SPI
  272. int board_spi_cs_gpio(unsigned bus, unsigned cs)
  273. {
  274. if (bus == 0 && cs == 0)
  275. return IMX_GPIO_NR(2, 30);
  276. else
  277. return -1;
  278. }
  279. #endif
  280. int board_init(void)
  281. {
  282. struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
  283. /* address of boot parameters */
  284. gd->bd->bi_boot_params = PHYS_SDRAM + 0x100;
  285. /* Enable eim_slow clocks */
  286. setbits_le32(&mxc_ccm->CCGR6, 0x1 << MXC_CCM_CCGR6_EMI_SLOW_OFFSET);
  287. #ifdef CONFIG_SYS_I2C_MXC
  288. if (is_mx6dq()) {
  289. setup_i2c(0, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6dq_i2c_pad_info0);
  290. setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6dq_i2c_pad_info1);
  291. setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6dq_i2c_pad_info2);
  292. } else {
  293. setup_i2c(0, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6sdl_i2c_pad_info0);
  294. setup_i2c(1, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6sdl_i2c_pad_info1);
  295. setup_i2c(2, CONFIG_SYS_I2C_SPEED, 0x7f, &dh6sdl_i2c_pad_info2);
  296. }
  297. #endif
  298. #ifdef CONFIG_SATA
  299. setup_sata();
  300. #endif
  301. setup_dhcom_mac_from_fuse();
  302. return 0;
  303. }
  304. #ifdef CONFIG_CMD_BMODE
  305. static const struct boot_mode board_boot_modes[] = {
  306. /* 4 bit bus width */
  307. {"sd2", MAKE_CFGVAL(0x40, 0x28, 0x00, 0x00)},
  308. {"sd3", MAKE_CFGVAL(0x40, 0x30, 0x00, 0x00)},
  309. /* 8 bit bus width */
  310. {"emmc", MAKE_CFGVAL(0x40, 0x38, 0x00, 0x00)},
  311. {NULL, 0},
  312. };
  313. #endif
  314. #define HW_CODE_BIT_0 IMX_GPIO_NR(2, 19)
  315. #define HW_CODE_BIT_1 IMX_GPIO_NR(6, 6)
  316. #define HW_CODE_BIT_2 IMX_GPIO_NR(2, 16)
  317. static int board_get_hwcode(void)
  318. {
  319. int hw_code;
  320. gpio_direction_input(HW_CODE_BIT_0);
  321. gpio_direction_input(HW_CODE_BIT_1);
  322. gpio_direction_input(HW_CODE_BIT_2);
  323. /* HW 100 + HW 200 = 00b; HW 300 = 01b */
  324. hw_code = ((gpio_get_value(HW_CODE_BIT_2) << 2) |
  325. (gpio_get_value(HW_CODE_BIT_1) << 1) |
  326. gpio_get_value(HW_CODE_BIT_0)) + 2;
  327. return hw_code;
  328. }
  329. int board_late_init(void)
  330. {
  331. u32 hw_code;
  332. char buf[16];
  333. hw_code = board_get_hwcode();
  334. switch (get_cpu_type()) {
  335. case MXC_CPU_MX6SOLO:
  336. snprintf(buf, sizeof(buf), "imx6s-dhcom%1d", hw_code);
  337. break;
  338. case MXC_CPU_MX6DL:
  339. snprintf(buf, sizeof(buf), "imx6dl-dhcom%1d", hw_code);
  340. break;
  341. case MXC_CPU_MX6D:
  342. snprintf(buf, sizeof(buf), "imx6d-dhcom%1d", hw_code);
  343. break;
  344. case MXC_CPU_MX6Q:
  345. snprintf(buf, sizeof(buf), "imx6q-dhcom%1d", hw_code);
  346. break;
  347. default:
  348. snprintf(buf, sizeof(buf), "UNKNOWN%1d", hw_code);
  349. break;
  350. }
  351. env_set("dhcom", buf);
  352. #ifdef CONFIG_CMD_BMODE
  353. add_board_boot_modes(board_boot_modes);
  354. #endif
  355. return 0;
  356. }
  357. int checkboard(void)
  358. {
  359. puts("Board: DHCOM i.MX6\n");
  360. return 0;
  361. }