mach-mxs.c 12 KB

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