mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / arch / arm / mach-mxs / mach-mxs.c
blob98f6e2adb53eaf39722f4988f43ce9d96c86ba50
1 /*
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/clk-provider.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/gpio.h>
21 #include <linux/init.h>
22 #include <linux/irqchip/mxs.h>
23 #include <linux/reboot.h>
24 #include <linux/micrel_phy.h>
25 #include <linux/of_address.h>
26 #include <linux/of_platform.h>
27 #include <linux/phy.h>
28 #include <linux/pinctrl/consumer.h>
29 #include <linux/sys_soc.h>
30 #include <asm/mach/arch.h>
31 #include <asm/mach/map.h>
32 #include <asm/mach/time.h>
33 #include <asm/system_misc.h>
35 #include "pm.h"
37 /* MXS DIGCTL SAIF CLKMUX */
38 #define MXS_DIGCTL_SAIF_CLKMUX_DIRECT 0x0
39 #define MXS_DIGCTL_SAIF_CLKMUX_CROSSINPUT 0x1
40 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0 0x2
41 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR1 0x3
43 #define HW_DIGCTL_CHIPID 0x310
44 #define HW_DIGCTL_CHIPID_MASK (0xffff << 16)
45 #define HW_DIGCTL_REV_MASK 0xff
46 #define HW_DIGCTL_CHIPID_MX23 (0x3780 << 16)
47 #define HW_DIGCTL_CHIPID_MX28 (0x2800 << 16)
49 #define MXS_CHIP_REVISION_1_0 0x10
50 #define MXS_CHIP_REVISION_1_1 0x11
51 #define MXS_CHIP_REVISION_1_2 0x12
52 #define MXS_CHIP_REVISION_1_3 0x13
53 #define MXS_CHIP_REVISION_1_4 0x14
54 #define MXS_CHIP_REV_UNKNOWN 0xff
56 #define MXS_GPIO_NR(bank, nr) ((bank) * 32 + (nr))
58 #define MXS_SET_ADDR 0x4
59 #define MXS_CLR_ADDR 0x8
60 #define MXS_TOG_ADDR 0xc
62 static u32 chipid;
63 static u32 socid;
65 static void __iomem *reset_addr;
67 static inline void __mxs_setl(u32 mask, void __iomem *reg)
69 __raw_writel(mask, reg + MXS_SET_ADDR);
72 static inline void __mxs_clrl(u32 mask, void __iomem *reg)
74 __raw_writel(mask, reg + MXS_CLR_ADDR);
77 static inline void __mxs_togl(u32 mask, void __iomem *reg)
79 __raw_writel(mask, reg + MXS_TOG_ADDR);
82 #define OCOTP_WORD_OFFSET 0x20
83 #define OCOTP_WORD_COUNT 0x20
85 #define BM_OCOTP_CTRL_BUSY (1 << 8)
86 #define BM_OCOTP_CTRL_ERROR (1 << 9)
87 #define BM_OCOTP_CTRL_RD_BANK_OPEN (1 << 12)
89 static DEFINE_MUTEX(ocotp_mutex);
90 static u32 ocotp_words[OCOTP_WORD_COUNT];
92 static const u32 *mxs_get_ocotp(void)
94 struct device_node *np;
95 void __iomem *ocotp_base;
96 int timeout = 0x400;
97 size_t i;
98 static int once;
100 if (once)
101 return ocotp_words;
103 np = of_find_compatible_node(NULL, NULL, "fsl,ocotp");
104 ocotp_base = of_iomap(np, 0);
105 WARN_ON(!ocotp_base);
107 mutex_lock(&ocotp_mutex);
110 * clk_enable(hbus_clk) for ocotp can be skipped
111 * as it must be on when system is running.
114 /* try to clear ERROR bit */
115 __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
117 /* check both BUSY and ERROR cleared */
118 while ((__raw_readl(ocotp_base) &
119 (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
120 cpu_relax();
122 if (unlikely(!timeout))
123 goto error_unlock;
125 /* open OCOTP banks for read */
126 __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
128 /* approximately wait 32 hclk cycles */
129 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();
136 if (unlikely(!timeout))
137 goto error_unlock;
139 for (i = 0; i < OCOTP_WORD_COUNT; i++)
140 ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
141 i * 0x10);
143 /* close banks for power saving */
144 __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
146 once = 1;
148 mutex_unlock(&ocotp_mutex);
150 return ocotp_words;
152 error_unlock:
153 mutex_unlock(&ocotp_mutex);
154 pr_err("%s: timeout in reading OCOTP\n", __func__);
155 return NULL;
158 enum mac_oui {
159 OUI_FSL,
160 OUI_DENX,
161 OUI_CRYSTALFONTZ,
164 static void __init update_fec_mac_prop(enum mac_oui oui)
166 struct device_node *np, *from = NULL;
167 struct property *newmac;
168 const u32 *ocotp = mxs_get_ocotp();
169 u8 *macaddr;
170 u32 val;
171 int i;
173 for (i = 0; i < 2; i++) {
174 np = of_find_compatible_node(from, NULL, "fsl,imx28-fec");
175 if (!np)
176 return;
178 from = np;
180 if (of_get_property(np, "local-mac-address", NULL))
181 continue;
183 newmac = kzalloc(sizeof(*newmac) + 6, GFP_KERNEL);
184 if (!newmac)
185 return;
186 newmac->value = newmac + 1;
187 newmac->length = 6;
189 newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
190 if (!newmac->name) {
191 kfree(newmac);
192 return;
196 * OCOTP only stores the last 4 octets for each mac address,
197 * so hard-code OUI here.
199 macaddr = newmac->value;
200 switch (oui) {
201 case OUI_FSL:
202 macaddr[0] = 0x00;
203 macaddr[1] = 0x04;
204 macaddr[2] = 0x9f;
205 break;
206 case OUI_DENX:
207 macaddr[0] = 0xc0;
208 macaddr[1] = 0xe5;
209 macaddr[2] = 0x4e;
210 break;
211 case OUI_CRYSTALFONTZ:
212 macaddr[0] = 0x58;
213 macaddr[1] = 0xb9;
214 macaddr[2] = 0xe1;
215 break;
217 val = ocotp[i];
218 macaddr[3] = (val >> 16) & 0xff;
219 macaddr[4] = (val >> 8) & 0xff;
220 macaddr[5] = (val >> 0) & 0xff;
222 of_update_property(np, newmac);
226 static inline void enable_clk_enet_out(void)
228 struct clk *clk = clk_get_sys("enet_out", NULL);
230 if (!IS_ERR(clk))
231 clk_prepare_enable(clk);
234 static void __init imx28_evk_init(void)
236 update_fec_mac_prop(OUI_FSL);
238 mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
241 static int apx4devkit_phy_fixup(struct phy_device *phy)
243 phy->dev_flags |= MICREL_PHY_50MHZ_CLK;
244 return 0;
247 static void __init apx4devkit_init(void)
249 enable_clk_enet_out();
251 if (IS_BUILTIN(CONFIG_PHYLIB))
252 phy_register_fixup_for_uid(PHY_ID_KSZ8051, MICREL_PHY_ID_MASK,
253 apx4devkit_phy_fixup);
256 #define ENET0_MDC__GPIO_4_0 MXS_GPIO_NR(4, 0)
257 #define ENET0_MDIO__GPIO_4_1 MXS_GPIO_NR(4, 1)
258 #define ENET0_RX_EN__GPIO_4_2 MXS_GPIO_NR(4, 2)
259 #define ENET0_RXD0__GPIO_4_3 MXS_GPIO_NR(4, 3)
260 #define ENET0_RXD1__GPIO_4_4 MXS_GPIO_NR(4, 4)
261 #define ENET0_TX_EN__GPIO_4_6 MXS_GPIO_NR(4, 6)
262 #define ENET0_TXD0__GPIO_4_7 MXS_GPIO_NR(4, 7)
263 #define ENET0_TXD1__GPIO_4_8 MXS_GPIO_NR(4, 8)
264 #define ENET_CLK__GPIO_4_16 MXS_GPIO_NR(4, 16)
266 #define TX28_FEC_PHY_POWER MXS_GPIO_NR(3, 29)
267 #define TX28_FEC_PHY_RESET MXS_GPIO_NR(4, 13)
268 #define TX28_FEC_nINT MXS_GPIO_NR(4, 5)
270 static const struct gpio tx28_gpios[] __initconst = {
271 { ENET0_MDC__GPIO_4_0, GPIOF_OUT_INIT_LOW, "GPIO_4_0" },
272 { ENET0_MDIO__GPIO_4_1, GPIOF_OUT_INIT_LOW, "GPIO_4_1" },
273 { ENET0_RX_EN__GPIO_4_2, GPIOF_OUT_INIT_LOW, "GPIO_4_2" },
274 { ENET0_RXD0__GPIO_4_3, GPIOF_OUT_INIT_LOW, "GPIO_4_3" },
275 { ENET0_RXD1__GPIO_4_4, GPIOF_OUT_INIT_LOW, "GPIO_4_4" },
276 { ENET0_TX_EN__GPIO_4_6, GPIOF_OUT_INIT_LOW, "GPIO_4_6" },
277 { ENET0_TXD0__GPIO_4_7, GPIOF_OUT_INIT_LOW, "GPIO_4_7" },
278 { ENET0_TXD1__GPIO_4_8, GPIOF_OUT_INIT_LOW, "GPIO_4_8" },
279 { ENET_CLK__GPIO_4_16, GPIOF_OUT_INIT_LOW, "GPIO_4_16" },
280 { TX28_FEC_PHY_POWER, GPIOF_OUT_INIT_LOW, "fec-phy-power" },
281 { TX28_FEC_PHY_RESET, GPIOF_OUT_INIT_LOW, "fec-phy-reset" },
282 { TX28_FEC_nINT, GPIOF_DIR_IN, "fec-int" },
285 static void __init tx28_post_init(void)
287 struct device_node *np;
288 struct platform_device *pdev;
289 struct pinctrl *pctl;
290 int ret;
292 enable_clk_enet_out();
294 np = of_find_compatible_node(NULL, NULL, "fsl,imx28-fec");
295 pdev = of_find_device_by_node(np);
296 if (!pdev) {
297 pr_err("%s: failed to find fec device\n", __func__);
298 return;
301 pctl = pinctrl_get_select(&pdev->dev, "gpio_mode");
302 if (IS_ERR(pctl)) {
303 pr_err("%s: failed to get pinctrl state\n", __func__);
304 return;
307 ret = gpio_request_array(tx28_gpios, ARRAY_SIZE(tx28_gpios));
308 if (ret) {
309 pr_err("%s: failed to request gpios: %d\n", __func__, ret);
310 return;
313 /* Power up fec phy */
314 gpio_set_value(TX28_FEC_PHY_POWER, 1);
315 msleep(26); /* 25ms according to data sheet */
317 /* Mode strap pins */
318 gpio_set_value(ENET0_RX_EN__GPIO_4_2, 1);
319 gpio_set_value(ENET0_RXD0__GPIO_4_3, 1);
320 gpio_set_value(ENET0_RXD1__GPIO_4_4, 1);
322 udelay(100); /* minimum assertion time for nRST */
324 /* Deasserting FEC PHY RESET */
325 gpio_set_value(TX28_FEC_PHY_RESET, 1);
327 pinctrl_put(pctl);
330 static void __init crystalfontz_init(void)
332 update_fec_mac_prop(OUI_CRYSTALFONTZ);
335 static const char __init *mxs_get_soc_id(void)
337 struct device_node *np;
338 void __iomem *digctl_base;
340 np = of_find_compatible_node(NULL, NULL, "fsl,imx23-digctl");
341 digctl_base = of_iomap(np, 0);
342 WARN_ON(!digctl_base);
344 chipid = readl(digctl_base + HW_DIGCTL_CHIPID);
345 socid = chipid & HW_DIGCTL_CHIPID_MASK;
347 iounmap(digctl_base);
348 of_node_put(np);
350 switch (socid) {
351 case HW_DIGCTL_CHIPID_MX23:
352 return "i.MX23";
353 case HW_DIGCTL_CHIPID_MX28:
354 return "i.MX28";
355 default:
356 return "Unknown";
360 static u32 __init mxs_get_cpu_rev(void)
362 u32 rev = chipid & HW_DIGCTL_REV_MASK;
364 switch (socid) {
365 case HW_DIGCTL_CHIPID_MX23:
366 switch (rev) {
367 case 0x0:
368 return MXS_CHIP_REVISION_1_0;
369 case 0x1:
370 return MXS_CHIP_REVISION_1_1;
371 case 0x2:
372 return MXS_CHIP_REVISION_1_2;
373 case 0x3:
374 return MXS_CHIP_REVISION_1_3;
375 case 0x4:
376 return MXS_CHIP_REVISION_1_4;
377 default:
378 return MXS_CHIP_REV_UNKNOWN;
380 case HW_DIGCTL_CHIPID_MX28:
381 switch (rev) {
382 case 0x0:
383 return MXS_CHIP_REVISION_1_1;
384 case 0x1:
385 return MXS_CHIP_REVISION_1_2;
386 default:
387 return MXS_CHIP_REV_UNKNOWN;
389 default:
390 return MXS_CHIP_REV_UNKNOWN;
394 static const char __init *mxs_get_revision(void)
396 u32 rev = mxs_get_cpu_rev();
398 if (rev != MXS_CHIP_REV_UNKNOWN)
399 return kasprintf(GFP_KERNEL, "%d.%d", (rev >> 4) & 0xf,
400 rev & 0xf);
401 else
402 return kasprintf(GFP_KERNEL, "%s", "Unknown");
405 #define MX23_CLKCTRL_RESET_OFFSET 0x120
406 #define MX28_CLKCTRL_RESET_OFFSET 0x1e0
408 static int __init mxs_restart_init(void)
410 struct device_node *np;
412 np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
413 reset_addr = of_iomap(np, 0);
414 if (!reset_addr)
415 return -ENODEV;
417 if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
418 reset_addr += MX23_CLKCTRL_RESET_OFFSET;
419 else
420 reset_addr += MX28_CLKCTRL_RESET_OFFSET;
421 of_node_put(np);
423 return 0;
426 static void __init mxs_machine_init(void)
428 struct device_node *root;
429 struct device *parent;
430 struct soc_device *soc_dev;
431 struct soc_device_attribute *soc_dev_attr;
432 int ret;
434 soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
435 if (!soc_dev_attr)
436 return;
438 root = of_find_node_by_path("/");
439 ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
440 if (ret)
441 return;
443 soc_dev_attr->family = "Freescale MXS Family";
444 soc_dev_attr->soc_id = mxs_get_soc_id();
445 soc_dev_attr->revision = mxs_get_revision();
447 soc_dev = soc_device_register(soc_dev_attr);
448 if (IS_ERR(soc_dev)) {
449 kfree(soc_dev_attr->revision);
450 kfree(soc_dev_attr);
451 return;
454 parent = soc_device_to_device(soc_dev);
456 if (of_machine_is_compatible("fsl,imx28-evk"))
457 imx28_evk_init();
458 else if (of_machine_is_compatible("bluegiga,apx4devkit"))
459 apx4devkit_init();
460 else if (of_machine_is_compatible("crystalfontz,cfa10036"))
461 crystalfontz_init();
463 of_platform_populate(NULL, of_default_bus_match_table,
464 NULL, parent);
466 mxs_restart_init();
468 if (of_machine_is_compatible("karo,tx28"))
469 tx28_post_init();
472 #define MXS_CLKCTRL_RESET_CHIP (1 << 1)
475 * Reset the system. It is called by machine_restart().
477 static void mxs_restart(enum reboot_mode mode, const char *cmd)
479 if (reset_addr) {
480 /* reset the chip */
481 __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
483 pr_err("Failed to assert the chip reset\n");
485 /* Delay to allow the serial port to show the message */
486 mdelay(50);
489 /* We'll take a jump through zero as a poor second */
490 soft_restart(0);
493 static void __init mxs_timer_init(void)
495 if (of_machine_is_compatible("fsl,imx23"))
496 mx23_clocks_init();
497 else
498 mx28_clocks_init();
499 of_clk_init(NULL);
500 clocksource_of_init();
503 static const char *mxs_dt_compat[] __initdata = {
504 "fsl,imx28",
505 "fsl,imx23",
506 NULL,
509 DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
510 .handle_irq = icoll_handle_irq,
511 .init_time = mxs_timer_init,
512 .init_machine = mxs_machine_init,
513 .init_late = mxs_pm_init,
514 .dt_compat = mxs_dt_compat,
515 .restart = mxs_restart,
516 MACHINE_END