mach-mxs.c 12 KB

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