mach-mxs.c 13 KB

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