mach-mxs.c 9.3 KB

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