mach-mxs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/mxs.h>
  21. #include <linux/reboot.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 <linux/sys_soc.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 HW_DIGCTL_CHIPID 0x310
  39. #define HW_DIGCTL_CHIPID_MASK (0xffff << 16)
  40. #define HW_DIGCTL_REV_MASK 0xff
  41. #define HW_DIGCTL_CHIPID_MX23 (0x3780 << 16)
  42. #define HW_DIGCTL_CHIPID_MX28 (0x2800 << 16)
  43. #define MXS_CHIP_REVISION_1_0 0x10
  44. #define MXS_CHIP_REVISION_1_1 0x11
  45. #define MXS_CHIP_REVISION_1_2 0x12
  46. #define MXS_CHIP_REVISION_1_3 0x13
  47. #define MXS_CHIP_REVISION_1_4 0x14
  48. #define MXS_CHIP_REV_UNKNOWN 0xff
  49. #define MXS_GPIO_NR(bank, nr) ((bank) * 32 + (nr))
  50. #define MXS_SET_ADDR 0x4
  51. #define MXS_CLR_ADDR 0x8
  52. #define MXS_TOG_ADDR 0xc
  53. static u32 chipid;
  54. static u32 socid;
  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. };
  127. static void __init update_fec_mac_prop(enum mac_oui oui)
  128. {
  129. struct device_node *np, *from = NULL;
  130. struct property *newmac;
  131. const u32 *ocotp = mxs_get_ocotp();
  132. u8 *macaddr;
  133. u32 val;
  134. int i;
  135. for (i = 0; i < 2; i++) {
  136. np = of_find_compatible_node(from, NULL, "fsl,imx28-fec");
  137. if (!np)
  138. return;
  139. from = np;
  140. if (of_get_property(np, "local-mac-address", NULL))
  141. continue;
  142. newmac = kzalloc(sizeof(*newmac) + 6, GFP_KERNEL);
  143. if (!newmac)
  144. return;
  145. newmac->value = newmac + 1;
  146. newmac->length = 6;
  147. newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
  148. if (!newmac->name) {
  149. kfree(newmac);
  150. return;
  151. }
  152. /*
  153. * OCOTP only stores the last 4 octets for each mac address,
  154. * so hard-code OUI here.
  155. */
  156. macaddr = newmac->value;
  157. switch (oui) {
  158. case OUI_FSL:
  159. macaddr[0] = 0x00;
  160. macaddr[1] = 0x04;
  161. macaddr[2] = 0x9f;
  162. break;
  163. case OUI_DENX:
  164. macaddr[0] = 0xc0;
  165. macaddr[1] = 0xe5;
  166. macaddr[2] = 0x4e;
  167. break;
  168. case OUI_CRYSTALFONTZ:
  169. macaddr[0] = 0x58;
  170. macaddr[1] = 0xb9;
  171. macaddr[2] = 0xe1;
  172. break;
  173. }
  174. val = ocotp[i];
  175. macaddr[3] = (val >> 16) & 0xff;
  176. macaddr[4] = (val >> 8) & 0xff;
  177. macaddr[5] = (val >> 0) & 0xff;
  178. of_update_property(np, newmac);
  179. }
  180. }
  181. static inline void enable_clk_enet_out(void)
  182. {
  183. struct clk *clk = clk_get_sys("enet_out", NULL);
  184. if (!IS_ERR(clk))
  185. clk_prepare_enable(clk);
  186. }
  187. static void __init imx28_evk_init(void)
  188. {
  189. update_fec_mac_prop(OUI_FSL);
  190. mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
  191. }
  192. static int apx4devkit_phy_fixup(struct phy_device *phy)
  193. {
  194. phy->dev_flags |= MICREL_PHY_50MHZ_CLK;
  195. return 0;
  196. }
  197. static void __init apx4devkit_init(void)
  198. {
  199. enable_clk_enet_out();
  200. if (IS_BUILTIN(CONFIG_PHYLIB))
  201. phy_register_fixup_for_uid(PHY_ID_KSZ8051, MICREL_PHY_ID_MASK,
  202. apx4devkit_phy_fixup);
  203. }
  204. #define ENET0_MDC__GPIO_4_0 MXS_GPIO_NR(4, 0)
  205. #define ENET0_MDIO__GPIO_4_1 MXS_GPIO_NR(4, 1)
  206. #define ENET0_RX_EN__GPIO_4_2 MXS_GPIO_NR(4, 2)
  207. #define ENET0_RXD0__GPIO_4_3 MXS_GPIO_NR(4, 3)
  208. #define ENET0_RXD1__GPIO_4_4 MXS_GPIO_NR(4, 4)
  209. #define ENET0_TX_EN__GPIO_4_6 MXS_GPIO_NR(4, 6)
  210. #define ENET0_TXD0__GPIO_4_7 MXS_GPIO_NR(4, 7)
  211. #define ENET0_TXD1__GPIO_4_8 MXS_GPIO_NR(4, 8)
  212. #define ENET_CLK__GPIO_4_16 MXS_GPIO_NR(4, 16)
  213. #define TX28_FEC_PHY_POWER MXS_GPIO_NR(3, 29)
  214. #define TX28_FEC_PHY_RESET MXS_GPIO_NR(4, 13)
  215. #define TX28_FEC_nINT MXS_GPIO_NR(4, 5)
  216. static const struct gpio tx28_gpios[] __initconst = {
  217. { ENET0_MDC__GPIO_4_0, GPIOF_OUT_INIT_LOW, "GPIO_4_0" },
  218. { ENET0_MDIO__GPIO_4_1, GPIOF_OUT_INIT_LOW, "GPIO_4_1" },
  219. { ENET0_RX_EN__GPIO_4_2, GPIOF_OUT_INIT_LOW, "GPIO_4_2" },
  220. { ENET0_RXD0__GPIO_4_3, GPIOF_OUT_INIT_LOW, "GPIO_4_3" },
  221. { ENET0_RXD1__GPIO_4_4, GPIOF_OUT_INIT_LOW, "GPIO_4_4" },
  222. { ENET0_TX_EN__GPIO_4_6, GPIOF_OUT_INIT_LOW, "GPIO_4_6" },
  223. { ENET0_TXD0__GPIO_4_7, GPIOF_OUT_INIT_LOW, "GPIO_4_7" },
  224. { ENET0_TXD1__GPIO_4_8, GPIOF_OUT_INIT_LOW, "GPIO_4_8" },
  225. { ENET_CLK__GPIO_4_16, GPIOF_OUT_INIT_LOW, "GPIO_4_16" },
  226. { TX28_FEC_PHY_POWER, GPIOF_OUT_INIT_LOW, "fec-phy-power" },
  227. { TX28_FEC_PHY_RESET, GPIOF_OUT_INIT_LOW, "fec-phy-reset" },
  228. { TX28_FEC_nINT, GPIOF_DIR_IN, "fec-int" },
  229. };
  230. static void __init tx28_post_init(void)
  231. {
  232. struct device_node *np;
  233. struct platform_device *pdev;
  234. struct pinctrl *pctl;
  235. int ret;
  236. enable_clk_enet_out();
  237. np = of_find_compatible_node(NULL, NULL, "fsl,imx28-fec");
  238. pdev = of_find_device_by_node(np);
  239. if (!pdev) {
  240. pr_err("%s: failed to find fec device\n", __func__);
  241. return;
  242. }
  243. pctl = pinctrl_get_select(&pdev->dev, "gpio_mode");
  244. if (IS_ERR(pctl)) {
  245. pr_err("%s: failed to get pinctrl state\n", __func__);
  246. return;
  247. }
  248. ret = gpio_request_array(tx28_gpios, ARRAY_SIZE(tx28_gpios));
  249. if (ret) {
  250. pr_err("%s: failed to request gpios: %d\n", __func__, ret);
  251. return;
  252. }
  253. /* Power up fec phy */
  254. gpio_set_value(TX28_FEC_PHY_POWER, 1);
  255. msleep(26); /* 25ms according to data sheet */
  256. /* Mode strap pins */
  257. gpio_set_value(ENET0_RX_EN__GPIO_4_2, 1);
  258. gpio_set_value(ENET0_RXD0__GPIO_4_3, 1);
  259. gpio_set_value(ENET0_RXD1__GPIO_4_4, 1);
  260. udelay(100); /* minimum assertion time for nRST */
  261. /* Deasserting FEC PHY RESET */
  262. gpio_set_value(TX28_FEC_PHY_RESET, 1);
  263. pinctrl_put(pctl);
  264. }
  265. static void __init crystalfontz_init(void)
  266. {
  267. update_fec_mac_prop(OUI_CRYSTALFONTZ);
  268. }
  269. static const char __init *mxs_get_soc_id(void)
  270. {
  271. struct device_node *np;
  272. void __iomem *digctl_base;
  273. np = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
  274. digctl_base = of_iomap(np, 0);
  275. WARN_ON(!digctl_base);
  276. chipid = readl(digctl_base + HW_DIGCTL_CHIPID);
  277. socid = chipid & HW_DIGCTL_CHIPID_MASK;
  278. iounmap(digctl_base);
  279. of_node_put(np);
  280. switch (socid) {
  281. case HW_DIGCTL_CHIPID_MX23:
  282. return "i.MX23";
  283. case HW_DIGCTL_CHIPID_MX28:
  284. return "i.MX28";
  285. default:
  286. return "Unknown";
  287. }
  288. }
  289. static u32 __init mxs_get_cpu_rev(void)
  290. {
  291. u32 rev = chipid & HW_DIGCTL_REV_MASK;
  292. switch (socid) {
  293. case HW_DIGCTL_CHIPID_MX23:
  294. switch (rev) {
  295. case 0x0:
  296. return MXS_CHIP_REVISION_1_0;
  297. case 0x1:
  298. return MXS_CHIP_REVISION_1_1;
  299. case 0x2:
  300. return MXS_CHIP_REVISION_1_2;
  301. case 0x3:
  302. return MXS_CHIP_REVISION_1_3;
  303. case 0x4:
  304. return MXS_CHIP_REVISION_1_4;
  305. default:
  306. return MXS_CHIP_REV_UNKNOWN;
  307. }
  308. case HW_DIGCTL_CHIPID_MX28:
  309. switch (rev) {
  310. case 0x0:
  311. return MXS_CHIP_REVISION_1_1;
  312. case 0x1:
  313. return MXS_CHIP_REVISION_1_2;
  314. default:
  315. return MXS_CHIP_REV_UNKNOWN;
  316. }
  317. default:
  318. return MXS_CHIP_REV_UNKNOWN;
  319. }
  320. }
  321. static const char __init *mxs_get_revision(void)
  322. {
  323. u32 rev = mxs_get_cpu_rev();
  324. if (rev != MXS_CHIP_REV_UNKNOWN)
  325. return kasprintf(GFP_KERNEL, "TO%d.%d", (rev >> 4) & 0xf,
  326. rev & 0xf);
  327. else
  328. return kasprintf(GFP_KERNEL, "%s", "Unknown");
  329. }
  330. static void __init mxs_machine_init(void)
  331. {
  332. struct device_node *root;
  333. struct device *parent;
  334. struct soc_device *soc_dev;
  335. struct soc_device_attribute *soc_dev_attr;
  336. int ret;
  337. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  338. if (!soc_dev_attr)
  339. return;
  340. root = of_find_node_by_path("/");
  341. ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
  342. if (ret)
  343. return;
  344. soc_dev_attr->family = "Freescale MXS Family";
  345. soc_dev_attr->soc_id = mxs_get_soc_id();
  346. soc_dev_attr->revision = mxs_get_revision();
  347. soc_dev = soc_device_register(soc_dev_attr);
  348. if (IS_ERR(soc_dev)) {
  349. kfree(soc_dev_attr->revision);
  350. kfree(soc_dev_attr);
  351. return;
  352. }
  353. parent = soc_device_to_device(soc_dev);
  354. if (of_machine_is_compatible("fsl,imx28-evk"))
  355. imx28_evk_init();
  356. else if (of_machine_is_compatible("bluegiga,apx4devkit"))
  357. apx4devkit_init();
  358. else if (of_machine_is_compatible("crystalfontz,cfa10037") ||
  359. of_machine_is_compatible("crystalfontz,cfa10049") ||
  360. of_machine_is_compatible("crystalfontz,cfa10055") ||
  361. of_machine_is_compatible("crystalfontz,cfa10057"))
  362. crystalfontz_init();
  363. of_platform_populate(NULL, of_default_bus_match_table,
  364. NULL, parent);
  365. if (of_machine_is_compatible("karo,tx28"))
  366. tx28_post_init();
  367. }
  368. #define MX23_CLKCTRL_RESET_OFFSET 0x120
  369. #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
  370. #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
  371. /*
  372. * Reset the system. It is called by machine_restart().
  373. */
  374. static void mxs_restart(enum reboot_mode mode, const char *cmd)
  375. {
  376. struct device_node *np;
  377. void __iomem *reset_addr;
  378. np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
  379. reset_addr = of_iomap(np, 0);
  380. if (!reset_addr)
  381. goto soft;
  382. if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
  383. reset_addr += MX23_CLKCTRL_RESET_OFFSET;
  384. else
  385. reset_addr += MX28_CLKCTRL_RESET_OFFSET;
  386. /* reset the chip */
  387. __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
  388. pr_err("Failed to assert the chip reset\n");
  389. /* Delay to allow the serial port to show the message */
  390. mdelay(50);
  391. soft:
  392. /* We'll take a jump through zero as a poor second */
  393. soft_restart(0);
  394. }
  395. static void __init mxs_timer_init(void)
  396. {
  397. if (of_machine_is_compatible("fsl,imx23"))
  398. mx23_clocks_init();
  399. else
  400. mx28_clocks_init();
  401. clocksource_of_init();
  402. }
  403. static const char *mxs_dt_compat[] __initdata = {
  404. "fsl,imx28",
  405. "fsl,imx23",
  406. NULL,
  407. };
  408. DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
  409. .handle_irq = icoll_handle_irq,
  410. .init_time = mxs_timer_init,
  411. .init_machine = mxs_machine_init,
  412. .init_late = mxs_pm_init,
  413. .dt_compat = mxs_dt_compat,
  414. .restart = mxs_restart,
  415. MACHINE_END