mach-mxs.c 11 KB

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