mach-mxs.c 12 KB

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