mach-mxs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clk/mxs.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/can/platform/flexcan.h>
  17. #include <linux/delay.h>
  18. #include <linux/err.h>
  19. #include <linux/gpio.h>
  20. #include <linux/init.h>
  21. #include <linux/irqchip.h>
  22. #include <linux/irqchip/mxs.h>
  23. #include <linux/micrel_phy.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/phy.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <asm/mach/arch.h>
  29. #include <asm/mach/map.h>
  30. #include <asm/mach/time.h>
  31. #include <asm/system_misc.h>
  32. /* MXS DIGCTL SAIF CLKMUX */
  33. #define MXS_DIGCTL_SAIF_CLKMUX_DIRECT 0x0
  34. #define MXS_DIGCTL_SAIF_CLKMUX_CROSSINPUT 0x1
  35. #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0 0x2
  36. #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR1 0x3
  37. #define MXS_GPIO_NR(bank, nr) ((bank) * 32 + (nr))
  38. #define MXS_SET_ADDR 0x4
  39. #define MXS_CLR_ADDR 0x8
  40. #define MXS_TOG_ADDR 0xc
  41. static inline void __mxs_setl(u32 mask, void __iomem *reg)
  42. {
  43. __raw_writel(mask, reg + MXS_SET_ADDR);
  44. }
  45. static inline void __mxs_clrl(u32 mask, void __iomem *reg)
  46. {
  47. __raw_writel(mask, reg + MXS_CLR_ADDR);
  48. }
  49. static inline void __mxs_togl(u32 mask, void __iomem *reg)
  50. {
  51. __raw_writel(mask, reg + MXS_TOG_ADDR);
  52. }
  53. /*
  54. * MX28EVK_FLEXCAN_SWITCH is shared between both flexcan controllers
  55. */
  56. #define MX28EVK_FLEXCAN_SWITCH MXS_GPIO_NR(2, 13)
  57. static int flexcan0_en, flexcan1_en;
  58. static void mx28evk_flexcan_switch(void)
  59. {
  60. if (flexcan0_en || flexcan1_en)
  61. gpio_set_value(MX28EVK_FLEXCAN_SWITCH, 1);
  62. else
  63. gpio_set_value(MX28EVK_FLEXCAN_SWITCH, 0);
  64. }
  65. static void mx28evk_flexcan0_switch(int enable)
  66. {
  67. flexcan0_en = enable;
  68. mx28evk_flexcan_switch();
  69. }
  70. static void mx28evk_flexcan1_switch(int enable)
  71. {
  72. flexcan1_en = enable;
  73. mx28evk_flexcan_switch();
  74. }
  75. static struct flexcan_platform_data flexcan_pdata[2];
  76. static struct of_dev_auxdata mxs_auxdata_lookup[] __initdata = {
  77. OF_DEV_AUXDATA("fsl,imx28-flexcan", 0x80032000, NULL, &flexcan_pdata[0]),
  78. OF_DEV_AUXDATA("fsl,imx28-flexcan", 0x80034000, NULL, &flexcan_pdata[1]),
  79. { /* sentinel */ }
  80. };
  81. #define OCOTP_WORD_OFFSET 0x20
  82. #define OCOTP_WORD_COUNT 0x20
  83. #define BM_OCOTP_CTRL_BUSY (1 << 8)
  84. #define BM_OCOTP_CTRL_ERROR (1 << 9)
  85. #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
  86. static DEFINE_MUTEX(ocotp_mutex);
  87. static u32 ocotp_words[OCOTP_WORD_COUNT];
  88. static const u32 *mxs_get_ocotp(void)
  89. {
  90. struct device_node *np;
  91. void __iomem *ocotp_base;
  92. int timeout = 0x400;
  93. size_t i;
  94. static int once;
  95. if (once)
  96. return ocotp_words;
  97. np = of_find_compatible_node(NULL, NULL, "fsl,ocotp");
  98. ocotp_base = of_iomap(np, 0);
  99. WARN_ON(!ocotp_base);
  100. mutex_lock(&ocotp_mutex);
  101. /*
  102. * clk_enable(hbus_clk) for ocotp can be skipped
  103. * as it must be on when system is running.
  104. */
  105. /* try to clear ERROR bit */
  106. __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
  107. /* check both BUSY and ERROR cleared */
  108. while ((__raw_readl(ocotp_base) &
  109. (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
  110. cpu_relax();
  111. if (unlikely(!timeout))
  112. goto error_unlock;
  113. /* open OCOTP banks for read */
  114. __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  115. /* approximately wait 32 hclk cycles */
  116. udelay(1);
  117. /* poll BUSY bit becoming cleared */
  118. timeout = 0x400;
  119. while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
  120. cpu_relax();
  121. if (unlikely(!timeout))
  122. goto error_unlock;
  123. for (i = 0; i < OCOTP_WORD_COUNT; i++)
  124. ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
  125. i * 0x10);
  126. /* close banks for power saving */
  127. __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
  128. once = 1;
  129. mutex_unlock(&ocotp_mutex);
  130. return ocotp_words;
  131. error_unlock:
  132. mutex_unlock(&ocotp_mutex);
  133. pr_err("%s: timeout in reading OCOTP\n", __func__);
  134. return NULL;
  135. }
  136. enum mac_oui {
  137. OUI_FSL,
  138. OUI_DENX,
  139. OUI_CRYSTALFONTZ,
  140. };
  141. static void __init update_fec_mac_prop(enum mac_oui oui)
  142. {
  143. struct device_node *np, *from = NULL;
  144. struct property *newmac;
  145. const u32 *ocotp = mxs_get_ocotp();
  146. u8 *macaddr;
  147. u32 val;
  148. int i;
  149. for (i = 0; i < 2; i++) {
  150. np = of_find_compatible_node(from, NULL, "fsl,imx28-fec");
  151. if (!np)
  152. return;
  153. from = np;
  154. if (of_get_property(np, "local-mac-address", NULL))
  155. continue;
  156. newmac = kzalloc(sizeof(*newmac) + 6, GFP_KERNEL);
  157. if (!newmac)
  158. return;
  159. newmac->value = newmac + 1;
  160. newmac->length = 6;
  161. newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
  162. if (!newmac->name) {
  163. kfree(newmac);
  164. return;
  165. }
  166. /*
  167. * OCOTP only stores the last 4 octets for each mac address,
  168. * so hard-code OUI here.
  169. */
  170. macaddr = newmac->value;
  171. switch (oui) {
  172. case OUI_FSL:
  173. macaddr[0] = 0x00;
  174. macaddr[1] = 0x04;
  175. macaddr[2] = 0x9f;
  176. break;
  177. case OUI_DENX:
  178. macaddr[0] = 0xc0;
  179. macaddr[1] = 0xe5;
  180. macaddr[2] = 0x4e;
  181. break;
  182. case OUI_CRYSTALFONTZ:
  183. macaddr[0] = 0x58;
  184. macaddr[1] = 0xb9;
  185. macaddr[2] = 0xe1;
  186. break;
  187. }
  188. val = ocotp[i];
  189. macaddr[3] = (val >> 16) & 0xff;
  190. macaddr[4] = (val >> 8) & 0xff;
  191. macaddr[5] = (val >> 0) & 0xff;
  192. of_update_property(np, newmac);
  193. }
  194. }
  195. static inline void enable_clk_enet_out(void)
  196. {
  197. struct clk *clk = clk_get_sys("enet_out", NULL);
  198. if (!IS_ERR(clk))
  199. clk_prepare_enable(clk);
  200. }
  201. static void __init imx28_evk_init(void)
  202. {
  203. update_fec_mac_prop(OUI_FSL);
  204. mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
  205. }
  206. static void __init imx28_evk_post_init(void)
  207. {
  208. if (!gpio_request_one(MX28EVK_FLEXCAN_SWITCH, GPIOF_DIR_OUT,
  209. "flexcan-switch")) {
  210. flexcan_pdata[0].transceiver_switch = mx28evk_flexcan0_switch;
  211. flexcan_pdata[1].transceiver_switch = mx28evk_flexcan1_switch;
  212. }
  213. }
  214. static int apx4devkit_phy_fixup(struct phy_device *phy)
  215. {
  216. phy->dev_flags |= MICREL_PHY_50MHZ_CLK;
  217. return 0;
  218. }
  219. static void __init apx4devkit_init(void)
  220. {
  221. enable_clk_enet_out();
  222. if (IS_BUILTIN(CONFIG_PHYLIB))
  223. phy_register_fixup_for_uid(PHY_ID_KSZ8051, MICREL_PHY_ID_MASK,
  224. apx4devkit_phy_fixup);
  225. }
  226. #define ENET0_MDC__GPIO_4_0 MXS_GPIO_NR(4, 0)
  227. #define ENET0_MDIO__GPIO_4_1 MXS_GPIO_NR(4, 1)
  228. #define ENET0_RX_EN__GPIO_4_2 MXS_GPIO_NR(4, 2)
  229. #define ENET0_RXD0__GPIO_4_3 MXS_GPIO_NR(4, 3)
  230. #define ENET0_RXD1__GPIO_4_4 MXS_GPIO_NR(4, 4)
  231. #define ENET0_TX_EN__GPIO_4_6 MXS_GPIO_NR(4, 6)
  232. #define ENET0_TXD0__GPIO_4_7 MXS_GPIO_NR(4, 7)
  233. #define ENET0_TXD1__GPIO_4_8 MXS_GPIO_NR(4, 8)
  234. #define ENET_CLK__GPIO_4_16 MXS_GPIO_NR(4, 16)
  235. #define TX28_FEC_PHY_POWER MXS_GPIO_NR(3, 29)
  236. #define TX28_FEC_PHY_RESET MXS_GPIO_NR(4, 13)
  237. #define TX28_FEC_nINT MXS_GPIO_NR(4, 5)
  238. static const struct gpio tx28_gpios[] __initconst = {
  239. { ENET0_MDC__GPIO_4_0, GPIOF_OUT_INIT_LOW, "GPIO_4_0" },
  240. { ENET0_MDIO__GPIO_4_1, GPIOF_OUT_INIT_LOW, "GPIO_4_1" },
  241. { ENET0_RX_EN__GPIO_4_2, GPIOF_OUT_INIT_LOW, "GPIO_4_2" },
  242. { ENET0_RXD0__GPIO_4_3, GPIOF_OUT_INIT_LOW, "GPIO_4_3" },
  243. { ENET0_RXD1__GPIO_4_4, GPIOF_OUT_INIT_LOW, "GPIO_4_4" },
  244. { ENET0_TX_EN__GPIO_4_6, GPIOF_OUT_INIT_LOW, "GPIO_4_6" },
  245. { ENET0_TXD0__GPIO_4_7, GPIOF_OUT_INIT_LOW, "GPIO_4_7" },
  246. { ENET0_TXD1__GPIO_4_8, GPIOF_OUT_INIT_LOW, "GPIO_4_8" },
  247. { ENET_CLK__GPIO_4_16, GPIOF_OUT_INIT_LOW, "GPIO_4_16" },
  248. { TX28_FEC_PHY_POWER, GPIOF_OUT_INIT_LOW, "fec-phy-power" },
  249. { TX28_FEC_PHY_RESET, GPIOF_OUT_INIT_LOW, "fec-phy-reset" },
  250. { TX28_FEC_nINT, GPIOF_DIR_IN, "fec-int" },
  251. };
  252. static void __init tx28_post_init(void)
  253. {
  254. struct device_node *np;
  255. struct platform_device *pdev;
  256. struct pinctrl *pctl;
  257. int ret;
  258. enable_clk_enet_out();
  259. np = of_find_compatible_node(NULL, NULL, "fsl,imx28-fec");
  260. pdev = of_find_device_by_node(np);
  261. if (!pdev) {
  262. pr_err("%s: failed to find fec device\n", __func__);
  263. return;
  264. }
  265. pctl = pinctrl_get_select(&pdev->dev, "gpio_mode");
  266. if (IS_ERR(pctl)) {
  267. pr_err("%s: failed to get pinctrl state\n", __func__);
  268. return;
  269. }
  270. ret = gpio_request_array(tx28_gpios, ARRAY_SIZE(tx28_gpios));
  271. if (ret) {
  272. pr_err("%s: failed to request gpios: %d\n", __func__, ret);
  273. return;
  274. }
  275. /* Power up fec phy */
  276. gpio_set_value(TX28_FEC_PHY_POWER, 1);
  277. msleep(26); /* 25ms according to data sheet */
  278. /* Mode strap pins */
  279. gpio_set_value(ENET0_RX_EN__GPIO_4_2, 1);
  280. gpio_set_value(ENET0_RXD0__GPIO_4_3, 1);
  281. gpio_set_value(ENET0_RXD1__GPIO_4_4, 1);
  282. udelay(100); /* minimum assertion time for nRST */
  283. /* Deasserting FEC PHY RESET */
  284. gpio_set_value(TX28_FEC_PHY_RESET, 1);
  285. pinctrl_put(pctl);
  286. }
  287. static void __init cfa10049_init(void)
  288. {
  289. update_fec_mac_prop(OUI_CRYSTALFONTZ);
  290. }
  291. static void __init cfa10037_init(void)
  292. {
  293. update_fec_mac_prop(OUI_CRYSTALFONTZ);
  294. }
  295. static void __init mxs_machine_init(void)
  296. {
  297. if (of_machine_is_compatible("fsl,imx28-evk"))
  298. imx28_evk_init();
  299. else if (of_machine_is_compatible("bluegiga,apx4devkit"))
  300. apx4devkit_init();
  301. else if (of_machine_is_compatible("crystalfontz,cfa10037"))
  302. cfa10037_init();
  303. else if (of_machine_is_compatible("crystalfontz,cfa10049"))
  304. cfa10049_init();
  305. of_platform_populate(NULL, of_default_bus_match_table,
  306. mxs_auxdata_lookup, NULL);
  307. if (of_machine_is_compatible("karo,tx28"))
  308. tx28_post_init();
  309. if (of_machine_is_compatible("fsl,imx28-evk"))
  310. imx28_evk_post_init();
  311. }
  312. #define MX23_CLKCTRL_RESET_OFFSET 0x120
  313. #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
  314. #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
  315. /*
  316. * Reset the system. It is called by machine_restart().
  317. */
  318. static void mxs_restart(char mode, const char *cmd)
  319. {
  320. struct device_node *np;
  321. void __iomem *reset_addr;
  322. np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
  323. reset_addr = of_iomap(np, 0);
  324. if (!reset_addr)
  325. goto soft;
  326. if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
  327. reset_addr += MX23_CLKCTRL_RESET_OFFSET;
  328. else
  329. reset_addr += MX28_CLKCTRL_RESET_OFFSET;
  330. /* reset the chip */
  331. __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
  332. pr_err("Failed to assert the chip reset\n");
  333. /* Delay to allow the serial port to show the message */
  334. mdelay(50);
  335. soft:
  336. /* We'll take a jump through zero as a poor second */
  337. soft_restart(0);
  338. }
  339. static void __init mxs_timer_init(void)
  340. {
  341. if (of_machine_is_compatible("fsl,imx23"))
  342. mx23_clocks_init();
  343. else
  344. mx28_clocks_init();
  345. clocksource_of_init();
  346. }
  347. static const char *mxs_dt_compat[] __initdata = {
  348. "fsl,imx28",
  349. "fsl,imx23",
  350. NULL,
  351. };
  352. DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
  353. .map_io = debug_ll_io_init,
  354. .init_irq = irqchip_init,
  355. .handle_irq = icoll_handle_irq,
  356. .init_time = mxs_timer_init,
  357. .init_machine = mxs_machine_init,
  358. .dt_compat = mxs_dt_compat,
  359. .restart = mxs_restart,
  360. MACHINE_END