2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
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:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/clk.h>
14 #include <linux/clk/mxs.h>
15 #include <linux/clkdev.h>
16 #include <linux/clocksource.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/reboot.h>
23 #include <linux/micrel_phy.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/phy.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/sys_soc.h>
29 #include <asm/mach/arch.h>
30 #include <asm/mach/map.h>
31 #include <asm/mach/time.h>
32 #include <asm/system_misc.h>
36 /* MXS DIGCTL SAIF CLKMUX */
37 #define MXS_DIGCTL_SAIF_CLKMUX_DIRECT 0x0
38 #define MXS_DIGCTL_SAIF_CLKMUX_CROSSINPUT 0x1
39 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0 0x2
40 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR1 0x3
42 #define HW_DIGCTL_CHIPID 0x310
43 #define HW_DIGCTL_CHIPID_MASK (0xffff << 16)
44 #define HW_DIGCTL_REV_MASK 0xff
45 #define HW_DIGCTL_CHIPID_MX23 (0x3780 << 16)
46 #define HW_DIGCTL_CHIPID_MX28 (0x2800 << 16)
48 #define MXS_CHIP_REVISION_1_0 0x10
49 #define MXS_CHIP_REVISION_1_1 0x11
50 #define MXS_CHIP_REVISION_1_2 0x12
51 #define MXS_CHIP_REVISION_1_3 0x13
52 #define MXS_CHIP_REVISION_1_4 0x14
53 #define MXS_CHIP_REV_UNKNOWN 0xff
55 #define MXS_GPIO_NR(bank, nr) ((bank) * 32 + (nr))
57 #define MXS_SET_ADDR 0x4
58 #define MXS_CLR_ADDR 0x8
59 #define MXS_TOG_ADDR 0xc
64 static inline void __mxs_setl(u32 mask
, void __iomem
*reg
)
66 __raw_writel(mask
, reg
+ MXS_SET_ADDR
);
69 static inline void __mxs_clrl(u32 mask
, void __iomem
*reg
)
71 __raw_writel(mask
, reg
+ MXS_CLR_ADDR
);
74 static inline void __mxs_togl(u32 mask
, void __iomem
*reg
)
76 __raw_writel(mask
, reg
+ MXS_TOG_ADDR
);
79 #define OCOTP_WORD_OFFSET 0x20
80 #define OCOTP_WORD_COUNT 0x20
82 #define BM_OCOTP_CTRL_BUSY (1 << 8)
83 #define BM_OCOTP_CTRL_ERROR (1 << 9)
84 #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
86 static DEFINE_MUTEX(ocotp_mutex
);
87 static u32 ocotp_words
[OCOTP_WORD_COUNT
];
89 static const u32
*mxs_get_ocotp(void)
91 struct device_node
*np
;
92 void __iomem
*ocotp_base
;
100 np
= of_find_compatible_node(NULL
, NULL
, "fsl,ocotp");
101 ocotp_base
= of_iomap(np
, 0);
102 WARN_ON(!ocotp_base
);
104 mutex_lock(&ocotp_mutex
);
107 * clk_enable(hbus_clk) for ocotp can be skipped
108 * as it must be on when system is running.
111 /* try to clear ERROR bit */
112 __mxs_clrl(BM_OCOTP_CTRL_ERROR
, ocotp_base
);
114 /* check both BUSY and ERROR cleared */
115 while ((__raw_readl(ocotp_base
) &
116 (BM_OCOTP_CTRL_BUSY
| BM_OCOTP_CTRL_ERROR
)) && --timeout
)
119 if (unlikely(!timeout
))
122 /* open OCOTP banks for read */
123 __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN
, ocotp_base
);
125 /* approximately wait 32 hclk cycles */
128 /* poll BUSY bit becoming cleared */
130 while ((__raw_readl(ocotp_base
) & BM_OCOTP_CTRL_BUSY
) && --timeout
)
133 if (unlikely(!timeout
))
136 for (i
= 0; i
< OCOTP_WORD_COUNT
; i
++)
137 ocotp_words
[i
] = __raw_readl(ocotp_base
+ OCOTP_WORD_OFFSET
+
140 /* close banks for power saving */
141 __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN
, ocotp_base
);
145 mutex_unlock(&ocotp_mutex
);
150 mutex_unlock(&ocotp_mutex
);
151 pr_err("%s: timeout in reading OCOTP\n", __func__
);
161 static void __init
update_fec_mac_prop(enum mac_oui oui
)
163 struct device_node
*np
, *from
= NULL
;
164 struct property
*newmac
;
165 const u32
*ocotp
= mxs_get_ocotp();
170 for (i
= 0; i
< 2; i
++) {
171 np
= of_find_compatible_node(from
, NULL
, "fsl,imx28-fec");
177 if (of_get_property(np
, "local-mac-address", NULL
))
180 newmac
= kzalloc(sizeof(*newmac
) + 6, GFP_KERNEL
);
183 newmac
->value
= newmac
+ 1;
186 newmac
->name
= kstrdup("local-mac-address", GFP_KERNEL
);
193 * OCOTP only stores the last 4 octets for each mac address,
194 * so hard-code OUI here.
196 macaddr
= newmac
->value
;
208 case OUI_CRYSTALFONTZ
:
215 macaddr
[3] = (val
>> 16) & 0xff;
216 macaddr
[4] = (val
>> 8) & 0xff;
217 macaddr
[5] = (val
>> 0) & 0xff;
219 of_update_property(np
, newmac
);
223 static inline void enable_clk_enet_out(void)
225 struct clk
*clk
= clk_get_sys("enet_out", NULL
);
228 clk_prepare_enable(clk
);
231 static void __init
imx28_evk_init(void)
233 update_fec_mac_prop(OUI_FSL
);
235 mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0
);
238 static int apx4devkit_phy_fixup(struct phy_device
*phy
)
240 phy
->dev_flags
|= MICREL_PHY_50MHZ_CLK
;
244 static void __init
apx4devkit_init(void)
246 enable_clk_enet_out();
248 if (IS_BUILTIN(CONFIG_PHYLIB
))
249 phy_register_fixup_for_uid(PHY_ID_KSZ8051
, MICREL_PHY_ID_MASK
,
250 apx4devkit_phy_fixup
);
253 #define ENET0_MDC__GPIO_4_0 MXS_GPIO_NR(4, 0)
254 #define ENET0_MDIO__GPIO_4_1 MXS_GPIO_NR(4, 1)
255 #define ENET0_RX_EN__GPIO_4_2 MXS_GPIO_NR(4, 2)
256 #define ENET0_RXD0__GPIO_4_3 MXS_GPIO_NR(4, 3)
257 #define ENET0_RXD1__GPIO_4_4 MXS_GPIO_NR(4, 4)
258 #define ENET0_TX_EN__GPIO_4_6 MXS_GPIO_NR(4, 6)
259 #define ENET0_TXD0__GPIO_4_7 MXS_GPIO_NR(4, 7)
260 #define ENET0_TXD1__GPIO_4_8 MXS_GPIO_NR(4, 8)
261 #define ENET_CLK__GPIO_4_16 MXS_GPIO_NR(4, 16)
263 #define TX28_FEC_PHY_POWER MXS_GPIO_NR(3, 29)
264 #define TX28_FEC_PHY_RESET MXS_GPIO_NR(4, 13)
265 #define TX28_FEC_nINT MXS_GPIO_NR(4, 5)
267 static const struct gpio tx28_gpios
[] __initconst
= {
268 { ENET0_MDC__GPIO_4_0
, GPIOF_OUT_INIT_LOW
, "GPIO_4_0" },
269 { ENET0_MDIO__GPIO_4_1
, GPIOF_OUT_INIT_LOW
, "GPIO_4_1" },
270 { ENET0_RX_EN__GPIO_4_2
, GPIOF_OUT_INIT_LOW
, "GPIO_4_2" },
271 { ENET0_RXD0__GPIO_4_3
, GPIOF_OUT_INIT_LOW
, "GPIO_4_3" },
272 { ENET0_RXD1__GPIO_4_4
, GPIOF_OUT_INIT_LOW
, "GPIO_4_4" },
273 { ENET0_TX_EN__GPIO_4_6
, GPIOF_OUT_INIT_LOW
, "GPIO_4_6" },
274 { ENET0_TXD0__GPIO_4_7
, GPIOF_OUT_INIT_LOW
, "GPIO_4_7" },
275 { ENET0_TXD1__GPIO_4_8
, GPIOF_OUT_INIT_LOW
, "GPIO_4_8" },
276 { ENET_CLK__GPIO_4_16
, GPIOF_OUT_INIT_LOW
, "GPIO_4_16" },
277 { TX28_FEC_PHY_POWER
, GPIOF_OUT_INIT_LOW
, "fec-phy-power" },
278 { TX28_FEC_PHY_RESET
, GPIOF_OUT_INIT_LOW
, "fec-phy-reset" },
279 { TX28_FEC_nINT
, GPIOF_DIR_IN
, "fec-int" },
282 static void __init
tx28_post_init(void)
284 struct device_node
*np
;
285 struct platform_device
*pdev
;
286 struct pinctrl
*pctl
;
289 enable_clk_enet_out();
291 np
= of_find_compatible_node(NULL
, NULL
, "fsl,imx28-fec");
292 pdev
= of_find_device_by_node(np
);
294 pr_err("%s: failed to find fec device\n", __func__
);
298 pctl
= pinctrl_get_select(&pdev
->dev
, "gpio_mode");
300 pr_err("%s: failed to get pinctrl state\n", __func__
);
304 ret
= gpio_request_array(tx28_gpios
, ARRAY_SIZE(tx28_gpios
));
306 pr_err("%s: failed to request gpios: %d\n", __func__
, ret
);
310 /* Power up fec phy */
311 gpio_set_value(TX28_FEC_PHY_POWER
, 1);
312 msleep(26); /* 25ms according to data sheet */
314 /* Mode strap pins */
315 gpio_set_value(ENET0_RX_EN__GPIO_4_2
, 1);
316 gpio_set_value(ENET0_RXD0__GPIO_4_3
, 1);
317 gpio_set_value(ENET0_RXD1__GPIO_4_4
, 1);
319 udelay(100); /* minimum assertion time for nRST */
321 /* Deasserting FEC PHY RESET */
322 gpio_set_value(TX28_FEC_PHY_RESET
, 1);
327 static void __init
crystalfontz_init(void)
329 update_fec_mac_prop(OUI_CRYSTALFONTZ
);
332 static const char __init
*mxs_get_soc_id(void)
334 struct device_node
*np
;
335 void __iomem
*digctl_base
;
337 np
= of_find_compatible_node(NULL
, NULL
, "fsl,imx23-digctl");
338 digctl_base
= of_iomap(np
, 0);
339 WARN_ON(!digctl_base
);
341 chipid
= readl(digctl_base
+ HW_DIGCTL_CHIPID
);
342 socid
= chipid
& HW_DIGCTL_CHIPID_MASK
;
344 iounmap(digctl_base
);
348 case HW_DIGCTL_CHIPID_MX23
:
350 case HW_DIGCTL_CHIPID_MX28
:
357 static u32 __init
mxs_get_cpu_rev(void)
359 u32 rev
= chipid
& HW_DIGCTL_REV_MASK
;
362 case HW_DIGCTL_CHIPID_MX23
:
365 return MXS_CHIP_REVISION_1_0
;
367 return MXS_CHIP_REVISION_1_1
;
369 return MXS_CHIP_REVISION_1_2
;
371 return MXS_CHIP_REVISION_1_3
;
373 return MXS_CHIP_REVISION_1_4
;
375 return MXS_CHIP_REV_UNKNOWN
;
377 case HW_DIGCTL_CHIPID_MX28
:
380 return MXS_CHIP_REVISION_1_1
;
382 return MXS_CHIP_REVISION_1_2
;
384 return MXS_CHIP_REV_UNKNOWN
;
387 return MXS_CHIP_REV_UNKNOWN
;
391 static const char __init
*mxs_get_revision(void)
393 u32 rev
= mxs_get_cpu_rev();
395 if (rev
!= MXS_CHIP_REV_UNKNOWN
)
396 return kasprintf(GFP_KERNEL
, "TO%d.%d", (rev
>> 4) & 0xf,
399 return kasprintf(GFP_KERNEL
, "%s", "Unknown");
402 static void __init
mxs_machine_init(void)
404 struct device_node
*root
;
405 struct device
*parent
;
406 struct soc_device
*soc_dev
;
407 struct soc_device_attribute
*soc_dev_attr
;
410 soc_dev_attr
= kzalloc(sizeof(*soc_dev_attr
), GFP_KERNEL
);
414 root
= of_find_node_by_path("/");
415 ret
= of_property_read_string(root
, "model", &soc_dev_attr
->machine
);
419 soc_dev_attr
->family
= "Freescale MXS Family";
420 soc_dev_attr
->soc_id
= mxs_get_soc_id();
421 soc_dev_attr
->revision
= mxs_get_revision();
423 soc_dev
= soc_device_register(soc_dev_attr
);
424 if (IS_ERR(soc_dev
)) {
425 kfree(soc_dev_attr
->revision
);
430 parent
= soc_device_to_device(soc_dev
);
432 if (of_machine_is_compatible("fsl,imx28-evk"))
434 else if (of_machine_is_compatible("bluegiga,apx4devkit"))
436 else if (of_machine_is_compatible("crystalfontz,cfa10037") ||
437 of_machine_is_compatible("crystalfontz,cfa10049") ||
438 of_machine_is_compatible("crystalfontz,cfa10055") ||
439 of_machine_is_compatible("crystalfontz,cfa10057"))
442 of_platform_populate(NULL
, of_default_bus_match_table
,
445 if (of_machine_is_compatible("karo,tx28"))
449 #define MX23_CLKCTRL_RESET_OFFSET 0x120
450 #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
451 #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
454 * Reset the system. It is called by machine_restart().
456 static void mxs_restart(enum reboot_mode mode
, const char *cmd
)
458 struct device_node
*np
;
459 void __iomem
*reset_addr
;
461 np
= of_find_compatible_node(NULL
, NULL
, "fsl,clkctrl");
462 reset_addr
= of_iomap(np
, 0);
466 if (of_device_is_compatible(np
, "fsl,imx23-clkctrl"))
467 reset_addr
+= MX23_CLKCTRL_RESET_OFFSET
;
469 reset_addr
+= MX28_CLKCTRL_RESET_OFFSET
;
472 __mxs_setl(MXS_CLKCTRL_RESET_CHIP
, reset_addr
);
474 pr_err("Failed to assert the chip reset\n");
476 /* Delay to allow the serial port to show the message */
480 /* We'll take a jump through zero as a poor second */
484 static void __init
mxs_timer_init(void)
486 if (of_machine_is_compatible("fsl,imx23"))
490 clocksource_of_init();
493 static const char *mxs_dt_compat
[] __initdata
= {
499 DT_MACHINE_START(MXS
, "Freescale MXS (Device Tree)")
500 .handle_irq
= icoll_handle_irq
,
501 .init_time
= mxs_timer_init
,
502 .init_machine
= mxs_machine_init
,
503 .init_late
= mxs_pm_init
,
504 .dt_compat
= mxs_dt_compat
,
505 .restart
= mxs_restart
,