1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright 2012 Linaro Ltd.
8 #include <linux/clk/mxs.h>
9 #include <linux/clkdev.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/gpio.h>
13 #include <linux/init.h>
14 #include <linux/irqchip/mxs.h>
15 #include <linux/reboot.h>
16 #include <linux/micrel_phy.h>
17 #include <linux/of_address.h>
18 #include <linux/of_platform.h>
19 #include <linux/phy.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/sys_soc.h>
22 #include <asm/mach/arch.h>
23 #include <asm/mach/map.h>
24 #include <asm/mach/time.h>
25 #include <asm/system_misc.h>
29 /* MXS DIGCTL SAIF CLKMUX */
30 #define MXS_DIGCTL_SAIF_CLKMUX_DIRECT 0x0
31 #define MXS_DIGCTL_SAIF_CLKMUX_CROSSINPUT 0x1
32 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0 0x2
33 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR1 0x3
35 #define HW_DIGCTL_CHIPID 0x310
36 #define HW_DIGCTL_CHIPID_MASK (0xffff << 16)
37 #define HW_DIGCTL_REV_MASK 0xff
38 #define HW_DIGCTL_CHIPID_MX23 (0x3780 << 16)
39 #define HW_DIGCTL_CHIPID_MX28 (0x2800 << 16)
41 #define MXS_CHIP_REVISION_1_0 0x10
42 #define MXS_CHIP_REVISION_1_1 0x11
43 #define MXS_CHIP_REVISION_1_2 0x12
44 #define MXS_CHIP_REVISION_1_3 0x13
45 #define MXS_CHIP_REVISION_1_4 0x14
46 #define MXS_CHIP_REV_UNKNOWN 0xff
48 #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
57 static void __iomem *reset_addr;
59 static inline void __mxs_setl(u32 mask, void __iomem *reg)
61 __raw_writel(mask, reg + MXS_SET_ADDR);
64 static inline void __mxs_clrl(u32 mask, void __iomem *reg)
66 __raw_writel(mask, reg + MXS_CLR_ADDR);
69 static inline void __mxs_togl(u32 mask, void __iomem *reg)
71 __raw_writel(mask, reg + MXS_TOG_ADDR);
74 #define OCOTP_WORD_OFFSET 0x20
75 #define OCOTP_WORD_COUNT 0x20
77 #define BM_OCOTP_CTRL_BUSY (1 << 8)
78 #define BM_OCOTP_CTRL_ERROR (1 << 9)
79 #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
81 static DEFINE_MUTEX(ocotp_mutex);
82 static u32 ocotp_words[OCOTP_WORD_COUNT];
84 static const u32 *mxs_get_ocotp(void)
86 struct device_node *np;
87 void __iomem *ocotp_base;
95 np = of_find_compatible_node(NULL, NULL, "fsl,ocotp");
96 ocotp_base = of_iomap(np, 0);
99 mutex_lock(&ocotp_mutex);
102 * clk_enable(hbus_clk) for ocotp can be skipped
103 * as it must be on when system is running.
106 /* try to clear ERROR bit */
107 __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
109 /* check both BUSY and ERROR cleared */
110 while ((__raw_readl(ocotp_base) &
111 (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
114 if (unlikely(!timeout))
117 /* open OCOTP banks for read */
118 __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
120 /* approximately wait 32 hclk cycles */
123 /* poll BUSY bit becoming cleared */
125 while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
128 if (unlikely(!timeout))
131 for (i = 0; i < OCOTP_WORD_COUNT; i++)
132 ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
135 /* close banks for power saving */
136 __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
140 mutex_unlock(&ocotp_mutex);
145 mutex_unlock(&ocotp_mutex);
146 pr_err("%s: timeout in reading OCOTP\n", __func__);
158 static void __init update_fec_mac_prop(enum mac_oui oui)
160 struct device_node *np, *from = NULL;
161 struct property *newmac;
162 const u32 *ocotp = mxs_get_ocotp();
167 for (i = 0; i < 2; i++) {
168 np = of_find_compatible_node(from, NULL, "fsl,imx28-fec");
174 if (of_get_property(np, "local-mac-address", NULL))
177 newmac = kzalloc(sizeof(*newmac) + 6, GFP_KERNEL);
180 newmac->value = newmac + 1;
183 newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
190 * OCOTP only stores the last 4 octets for each mac address,
191 * so hard-code OUI here.
193 macaddr = newmac->value;
205 case OUI_CRYSTALFONTZ:
222 macaddr[3] = (val >> 16) & 0xff;
223 macaddr[4] = (val >> 8) & 0xff;
224 macaddr[5] = (val >> 0) & 0xff;
226 of_update_property(np, newmac);
230 static inline void enable_clk_enet_out(void)
232 struct clk *clk = clk_get_sys("enet_out", NULL);
235 clk_prepare_enable(clk);
238 static void __init imx28_evk_init(void)
240 update_fec_mac_prop(OUI_FSL);
242 mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
245 static void __init imx28_apf28_init(void)
247 update_fec_mac_prop(OUI_ARMADEUS);
250 static int apx4devkit_phy_fixup(struct phy_device *phy)
252 phy->dev_flags |= MICREL_PHY_50MHZ_CLK;
256 static void __init apx4devkit_init(void)
258 enable_clk_enet_out();
260 if (IS_BUILTIN(CONFIG_PHYLIB))
261 phy_register_fixup_for_uid(PHY_ID_KSZ8051, MICREL_PHY_ID_MASK,
262 apx4devkit_phy_fixup);
265 static void __init crystalfontz_init(void)
267 update_fec_mac_prop(OUI_CRYSTALFONTZ);
270 static void __init duckbill_init(void)
272 update_fec_mac_prop(OUI_I2SE);
275 static void __init m28cu3_init(void)
277 update_fec_mac_prop(OUI_DENX);
280 static const char __init *mxs_get_soc_id(void)
282 struct device_node *np;
283 void __iomem *digctl_base;
285 np = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
286 digctl_base = of_iomap(np, 0);
287 WARN_ON(!digctl_base);
289 chipid = readl(digctl_base + HW_DIGCTL_CHIPID);
290 socid = chipid & HW_DIGCTL_CHIPID_MASK;
292 iounmap(digctl_base);
296 case HW_DIGCTL_CHIPID_MX23:
298 case HW_DIGCTL_CHIPID_MX28:
305 static u32 __init mxs_get_cpu_rev(void)
307 u32 rev = chipid & HW_DIGCTL_REV_MASK;
310 case HW_DIGCTL_CHIPID_MX23:
313 return MXS_CHIP_REVISION_1_0;
315 return MXS_CHIP_REVISION_1_1;
317 return MXS_CHIP_REVISION_1_2;
319 return MXS_CHIP_REVISION_1_3;
321 return MXS_CHIP_REVISION_1_4;
323 return MXS_CHIP_REV_UNKNOWN;
325 case HW_DIGCTL_CHIPID_MX28:
328 return MXS_CHIP_REVISION_1_1;
330 return MXS_CHIP_REVISION_1_2;
332 return MXS_CHIP_REV_UNKNOWN;
335 return MXS_CHIP_REV_UNKNOWN;
339 static const char __init *mxs_get_revision(void)
341 u32 rev = mxs_get_cpu_rev();
343 if (rev != MXS_CHIP_REV_UNKNOWN)
344 return kasprintf(GFP_KERNEL, "%d.%d", (rev >> 4) & 0xf,
347 return kasprintf(GFP_KERNEL, "%s", "Unknown");
350 #define MX23_CLKCTRL_RESET_OFFSET 0x120
351 #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
353 static int __init mxs_restart_init(void)
355 struct device_node *np;
357 np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
358 reset_addr = of_iomap(np, 0);
362 if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
363 reset_addr += MX23_CLKCTRL_RESET_OFFSET;
365 reset_addr += MX28_CLKCTRL_RESET_OFFSET;
371 static void __init eukrea_mbmx283lc_init(void)
373 mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
376 static void __init mxs_machine_init(void)
378 struct device_node *root;
379 struct device *parent;
380 struct soc_device *soc_dev;
381 struct soc_device_attribute *soc_dev_attr;
384 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
388 root = of_find_node_by_path("/");
389 ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
395 soc_dev_attr->family = "Freescale MXS Family";
396 soc_dev_attr->soc_id = mxs_get_soc_id();
397 soc_dev_attr->revision = mxs_get_revision();
399 soc_dev = soc_device_register(soc_dev_attr);
400 if (IS_ERR(soc_dev)) {
401 kfree(soc_dev_attr->revision);
406 parent = soc_device_to_device(soc_dev);
408 if (of_machine_is_compatible("fsl,imx28-evk"))
410 if (of_machine_is_compatible("armadeus,imx28-apf28"))
412 else if (of_machine_is_compatible("bluegiga,apx4devkit"))
414 else if (of_machine_is_compatible("crystalfontz,cfa10036"))
416 else if (of_machine_is_compatible("eukrea,mbmx283lc"))
417 eukrea_mbmx283lc_init();
418 else if (of_machine_is_compatible("i2se,duckbill") ||
419 of_machine_is_compatible("i2se,duckbill-2"))
421 else if (of_machine_is_compatible("msr,m28cu3"))
424 of_platform_default_populate(NULL, NULL, parent);
429 #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
432 * Reset the system. It is called by machine_restart().
434 static void mxs_restart(enum reboot_mode mode, const char *cmd)
438 __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
440 pr_err("Failed to assert the chip reset\n");
442 /* Delay to allow the serial port to show the message */
446 /* We'll take a jump through zero as a poor second */
450 static const char *const mxs_dt_compat[] __initconst = {
456 DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
457 .handle_irq = icoll_handle_irq,
458 .init_machine = mxs_machine_init,
459 .init_late = mxs_pm_init,
460 .dt_compat = mxs_dt_compat,
461 .restart = mxs_restart,