mach-mxs.c 10 KB

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