mach-mxs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. static void __init crystalfontz_init(void)
  221. {
  222. update_fec_mac_prop(OUI_CRYSTALFONTZ);
  223. }
  224. static void __init duckbill_init(void)
  225. {
  226. update_fec_mac_prop(OUI_I2SE);
  227. }
  228. static void __init m28cu3_init(void)
  229. {
  230. update_fec_mac_prop(OUI_DENX);
  231. }
  232. static const char __init *mxs_get_soc_id(void)
  233. {
  234. struct device_node *np;
  235. void __iomem *digctl_base;
  236. np = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
  237. digctl_base = of_iomap(np, 0);
  238. WARN_ON(!digctl_base);
  239. chipid = readl(digctl_base + HW_DIGCTL_CHIPID);
  240. socid = chipid & HW_DIGCTL_CHIPID_MASK;
  241. iounmap(digctl_base);
  242. of_node_put(np);
  243. switch (socid) {
  244. case HW_DIGCTL_CHIPID_MX23:
  245. return "i.MX23";
  246. case HW_DIGCTL_CHIPID_MX28:
  247. return "i.MX28";
  248. default:
  249. return "Unknown";
  250. }
  251. }
  252. static u32 __init mxs_get_cpu_rev(void)
  253. {
  254. u32 rev = chipid & HW_DIGCTL_REV_MASK;
  255. switch (socid) {
  256. case HW_DIGCTL_CHIPID_MX23:
  257. switch (rev) {
  258. case 0x0:
  259. return MXS_CHIP_REVISION_1_0;
  260. case 0x1:
  261. return MXS_CHIP_REVISION_1_1;
  262. case 0x2:
  263. return MXS_CHIP_REVISION_1_2;
  264. case 0x3:
  265. return MXS_CHIP_REVISION_1_3;
  266. case 0x4:
  267. return MXS_CHIP_REVISION_1_4;
  268. default:
  269. return MXS_CHIP_REV_UNKNOWN;
  270. }
  271. case HW_DIGCTL_CHIPID_MX28:
  272. switch (rev) {
  273. case 0x0:
  274. return MXS_CHIP_REVISION_1_1;
  275. case 0x1:
  276. return MXS_CHIP_REVISION_1_2;
  277. default:
  278. return MXS_CHIP_REV_UNKNOWN;
  279. }
  280. default:
  281. return MXS_CHIP_REV_UNKNOWN;
  282. }
  283. }
  284. static const char __init *mxs_get_revision(void)
  285. {
  286. u32 rev = mxs_get_cpu_rev();
  287. if (rev != MXS_CHIP_REV_UNKNOWN)
  288. return kasprintf(GFP_KERNEL, "%d.%d", (rev >> 4) & 0xf,
  289. rev & 0xf);
  290. else
  291. return kasprintf(GFP_KERNEL, "%s", "Unknown");
  292. }
  293. #define MX23_CLKCTRL_RESET_OFFSET 0x120
  294. #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
  295. static int __init mxs_restart_init(void)
  296. {
  297. struct device_node *np;
  298. np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
  299. reset_addr = of_iomap(np, 0);
  300. if (!reset_addr)
  301. return -ENODEV;
  302. if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
  303. reset_addr += MX23_CLKCTRL_RESET_OFFSET;
  304. else
  305. reset_addr += MX28_CLKCTRL_RESET_OFFSET;
  306. of_node_put(np);
  307. return 0;
  308. }
  309. static void __init eukrea_mbmx283lc_init(void)
  310. {
  311. mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
  312. }
  313. static void __init mxs_machine_init(void)
  314. {
  315. struct device_node *root;
  316. struct device *parent;
  317. struct soc_device *soc_dev;
  318. struct soc_device_attribute *soc_dev_attr;
  319. int ret;
  320. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  321. if (!soc_dev_attr)
  322. return;
  323. root = of_find_node_by_path("/");
  324. ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
  325. if (ret)
  326. return;
  327. soc_dev_attr->family = "Freescale MXS Family";
  328. soc_dev_attr->soc_id = mxs_get_soc_id();
  329. soc_dev_attr->revision = mxs_get_revision();
  330. soc_dev = soc_device_register(soc_dev_attr);
  331. if (IS_ERR(soc_dev)) {
  332. kfree(soc_dev_attr->revision);
  333. kfree(soc_dev_attr);
  334. return;
  335. }
  336. parent = soc_device_to_device(soc_dev);
  337. if (of_machine_is_compatible("fsl,imx28-evk"))
  338. imx28_evk_init();
  339. if (of_machine_is_compatible("armadeus,imx28-apf28"))
  340. imx28_apf28_init();
  341. else if (of_machine_is_compatible("bluegiga,apx4devkit"))
  342. apx4devkit_init();
  343. else if (of_machine_is_compatible("crystalfontz,cfa10036"))
  344. crystalfontz_init();
  345. else if (of_machine_is_compatible("eukrea,mbmx283lc"))
  346. eukrea_mbmx283lc_init();
  347. else if (of_machine_is_compatible("i2se,duckbill"))
  348. duckbill_init();
  349. else if (of_machine_is_compatible("msr,m28cu3"))
  350. m28cu3_init();
  351. of_platform_default_populate(NULL, NULL, parent);
  352. mxs_restart_init();
  353. }
  354. #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
  355. /*
  356. * Reset the system. It is called by machine_restart().
  357. */
  358. static void mxs_restart(enum reboot_mode mode, const char *cmd)
  359. {
  360. if (reset_addr) {
  361. /* reset the chip */
  362. __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
  363. pr_err("Failed to assert the chip reset\n");
  364. /* Delay to allow the serial port to show the message */
  365. mdelay(50);
  366. }
  367. /* We'll take a jump through zero as a poor second */
  368. soft_restart(0);
  369. }
  370. static const char *const mxs_dt_compat[] __initconst = {
  371. "fsl,imx28",
  372. "fsl,imx23",
  373. NULL,
  374. };
  375. DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
  376. .handle_irq = icoll_handle_irq,
  377. .init_machine = mxs_machine_init,
  378. .init_late = mxs_pm_init,
  379. .dt_compat = mxs_dt_compat,
  380. .restart = mxs_restart,
  381. MACHINE_END