drivers/net: caif: fix wrong rtnl_is_locked() usage
[cascardo/linux.git] / arch / arm / mach-mxs / mach-mxs.c
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
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.h>
22 #include <linux/irqchip/mxs.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 <asm/mach/arch.h>
29 #include <asm/mach/map.h>
30 #include <asm/mach/time.h>
31 #include <asm/system_misc.h>
32
33 #include "pm.h"
34
35 /* MXS DIGCTL SAIF CLKMUX */
36 #define MXS_DIGCTL_SAIF_CLKMUX_DIRECT           0x0
37 #define MXS_DIGCTL_SAIF_CLKMUX_CROSSINPUT       0x1
38 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0         0x2
39 #define MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR1         0x3
40
41 #define MXS_GPIO_NR(bank, nr)   ((bank) * 32 + (nr))
42
43 #define MXS_SET_ADDR            0x4
44 #define MXS_CLR_ADDR            0x8
45 #define MXS_TOG_ADDR            0xc
46
47 static inline void __mxs_setl(u32 mask, void __iomem *reg)
48 {
49         __raw_writel(mask, reg + MXS_SET_ADDR);
50 }
51
52 static inline void __mxs_clrl(u32 mask, void __iomem *reg)
53 {
54         __raw_writel(mask, reg + MXS_CLR_ADDR);
55 }
56
57 static inline void __mxs_togl(u32 mask, void __iomem *reg)
58 {
59         __raw_writel(mask, reg + MXS_TOG_ADDR);
60 }
61
62 #define OCOTP_WORD_OFFSET               0x20
63 #define OCOTP_WORD_COUNT                0x20
64
65 #define BM_OCOTP_CTRL_BUSY              (1 << 8)
66 #define BM_OCOTP_CTRL_ERROR             (1 << 9)
67 #define BM_OCOTP_CTRL_RD_BANK_OPEN      (1 << 12)
68
69 static DEFINE_MUTEX(ocotp_mutex);
70 static u32 ocotp_words[OCOTP_WORD_COUNT];
71
72 static const u32 *mxs_get_ocotp(void)
73 {
74         struct device_node *np;
75         void __iomem *ocotp_base;
76         int timeout = 0x400;
77         size_t i;
78         static int once;
79
80         if (once)
81                 return ocotp_words;
82
83         np = of_find_compatible_node(NULL, NULL, "fsl,ocotp");
84         ocotp_base = of_iomap(np, 0);
85         WARN_ON(!ocotp_base);
86
87         mutex_lock(&ocotp_mutex);
88
89         /*
90          * clk_enable(hbus_clk) for ocotp can be skipped
91          * as it must be on when system is running.
92          */
93
94         /* try to clear ERROR bit */
95         __mxs_clrl(BM_OCOTP_CTRL_ERROR, ocotp_base);
96
97         /* check both BUSY and ERROR cleared */
98         while ((__raw_readl(ocotp_base) &
99                 (BM_OCOTP_CTRL_BUSY | BM_OCOTP_CTRL_ERROR)) && --timeout)
100                 cpu_relax();
101
102         if (unlikely(!timeout))
103                 goto error_unlock;
104
105         /* open OCOTP banks for read */
106         __mxs_setl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
107
108         /* approximately wait 32 hclk cycles */
109         udelay(1);
110
111         /* poll BUSY bit becoming cleared */
112         timeout = 0x400;
113         while ((__raw_readl(ocotp_base) & BM_OCOTP_CTRL_BUSY) && --timeout)
114                 cpu_relax();
115
116         if (unlikely(!timeout))
117                 goto error_unlock;
118
119         for (i = 0; i < OCOTP_WORD_COUNT; i++)
120                 ocotp_words[i] = __raw_readl(ocotp_base + OCOTP_WORD_OFFSET +
121                                                 i * 0x10);
122
123         /* close banks for power saving */
124         __mxs_clrl(BM_OCOTP_CTRL_RD_BANK_OPEN, ocotp_base);
125
126         once = 1;
127
128         mutex_unlock(&ocotp_mutex);
129
130         return ocotp_words;
131
132 error_unlock:
133         mutex_unlock(&ocotp_mutex);
134         pr_err("%s: timeout in reading OCOTP\n", __func__);
135         return NULL;
136 }
137
138 enum mac_oui {
139         OUI_FSL,
140         OUI_DENX,
141         OUI_CRYSTALFONTZ,
142 };
143
144 static void __init update_fec_mac_prop(enum mac_oui oui)
145 {
146         struct device_node *np, *from = NULL;
147         struct property *newmac;
148         const u32 *ocotp = mxs_get_ocotp();
149         u8 *macaddr;
150         u32 val;
151         int i;
152
153         for (i = 0; i < 2; i++) {
154                 np = of_find_compatible_node(from, NULL, "fsl,imx28-fec");
155                 if (!np)
156                         return;
157
158                 from = np;
159
160                 if (of_get_property(np, "local-mac-address", NULL))
161                         continue;
162
163                 newmac = kzalloc(sizeof(*newmac) + 6, GFP_KERNEL);
164                 if (!newmac)
165                         return;
166                 newmac->value = newmac + 1;
167                 newmac->length = 6;
168
169                 newmac->name = kstrdup("local-mac-address", GFP_KERNEL);
170                 if (!newmac->name) {
171                         kfree(newmac);
172                         return;
173                 }
174
175                 /*
176                  * OCOTP only stores the last 4 octets for each mac address,
177                  * so hard-code OUI here.
178                  */
179                 macaddr = newmac->value;
180                 switch (oui) {
181                 case OUI_FSL:
182                         macaddr[0] = 0x00;
183                         macaddr[1] = 0x04;
184                         macaddr[2] = 0x9f;
185                         break;
186                 case OUI_DENX:
187                         macaddr[0] = 0xc0;
188                         macaddr[1] = 0xe5;
189                         macaddr[2] = 0x4e;
190                         break;
191                 case OUI_CRYSTALFONTZ:
192                         macaddr[0] = 0x58;
193                         macaddr[1] = 0xb9;
194                         macaddr[2] = 0xe1;
195                         break;
196                 }
197                 val = ocotp[i];
198                 macaddr[3] = (val >> 16) & 0xff;
199                 macaddr[4] = (val >> 8) & 0xff;
200                 macaddr[5] = (val >> 0) & 0xff;
201
202                 of_update_property(np, newmac);
203         }
204 }
205
206 static inline void enable_clk_enet_out(void)
207 {
208         struct clk *clk = clk_get_sys("enet_out", NULL);
209
210         if (!IS_ERR(clk))
211                 clk_prepare_enable(clk);
212 }
213
214 static void __init imx28_evk_init(void)
215 {
216         update_fec_mac_prop(OUI_FSL);
217
218         mxs_saif_clkmux_select(MXS_DIGCTL_SAIF_CLKMUX_EXTMSTR0);
219 }
220
221 static int apx4devkit_phy_fixup(struct phy_device *phy)
222 {
223         phy->dev_flags |= MICREL_PHY_50MHZ_CLK;
224         return 0;
225 }
226
227 static void __init apx4devkit_init(void)
228 {
229         enable_clk_enet_out();
230
231         if (IS_BUILTIN(CONFIG_PHYLIB))
232                 phy_register_fixup_for_uid(PHY_ID_KSZ8051, MICREL_PHY_ID_MASK,
233                                            apx4devkit_phy_fixup);
234 }
235
236 #define ENET0_MDC__GPIO_4_0     MXS_GPIO_NR(4, 0)
237 #define ENET0_MDIO__GPIO_4_1    MXS_GPIO_NR(4, 1)
238 #define ENET0_RX_EN__GPIO_4_2   MXS_GPIO_NR(4, 2)
239 #define ENET0_RXD0__GPIO_4_3    MXS_GPIO_NR(4, 3)
240 #define ENET0_RXD1__GPIO_4_4    MXS_GPIO_NR(4, 4)
241 #define ENET0_TX_EN__GPIO_4_6   MXS_GPIO_NR(4, 6)
242 #define ENET0_TXD0__GPIO_4_7    MXS_GPIO_NR(4, 7)
243 #define ENET0_TXD1__GPIO_4_8    MXS_GPIO_NR(4, 8)
244 #define ENET_CLK__GPIO_4_16     MXS_GPIO_NR(4, 16)
245
246 #define TX28_FEC_PHY_POWER      MXS_GPIO_NR(3, 29)
247 #define TX28_FEC_PHY_RESET      MXS_GPIO_NR(4, 13)
248 #define TX28_FEC_nINT           MXS_GPIO_NR(4, 5)
249
250 static const struct gpio tx28_gpios[] __initconst = {
251         { ENET0_MDC__GPIO_4_0, GPIOF_OUT_INIT_LOW, "GPIO_4_0" },
252         { ENET0_MDIO__GPIO_4_1, GPIOF_OUT_INIT_LOW, "GPIO_4_1" },
253         { ENET0_RX_EN__GPIO_4_2, GPIOF_OUT_INIT_LOW, "GPIO_4_2" },
254         { ENET0_RXD0__GPIO_4_3, GPIOF_OUT_INIT_LOW, "GPIO_4_3" },
255         { ENET0_RXD1__GPIO_4_4, GPIOF_OUT_INIT_LOW, "GPIO_4_4" },
256         { ENET0_TX_EN__GPIO_4_6, GPIOF_OUT_INIT_LOW, "GPIO_4_6" },
257         { ENET0_TXD0__GPIO_4_7, GPIOF_OUT_INIT_LOW, "GPIO_4_7" },
258         { ENET0_TXD1__GPIO_4_8, GPIOF_OUT_INIT_LOW, "GPIO_4_8" },
259         { ENET_CLK__GPIO_4_16, GPIOF_OUT_INIT_LOW, "GPIO_4_16" },
260         { TX28_FEC_PHY_POWER, GPIOF_OUT_INIT_LOW, "fec-phy-power" },
261         { TX28_FEC_PHY_RESET, GPIOF_OUT_INIT_LOW, "fec-phy-reset" },
262         { TX28_FEC_nINT, GPIOF_DIR_IN, "fec-int" },
263 };
264
265 static void __init tx28_post_init(void)
266 {
267         struct device_node *np;
268         struct platform_device *pdev;
269         struct pinctrl *pctl;
270         int ret;
271
272         enable_clk_enet_out();
273
274         np = of_find_compatible_node(NULL, NULL, "fsl,imx28-fec");
275         pdev = of_find_device_by_node(np);
276         if (!pdev) {
277                 pr_err("%s: failed to find fec device\n", __func__);
278                 return;
279         }
280
281         pctl = pinctrl_get_select(&pdev->dev, "gpio_mode");
282         if (IS_ERR(pctl)) {
283                 pr_err("%s: failed to get pinctrl state\n", __func__);
284                 return;
285         }
286
287         ret = gpio_request_array(tx28_gpios, ARRAY_SIZE(tx28_gpios));
288         if (ret) {
289                 pr_err("%s: failed to request gpios: %d\n", __func__, ret);
290                 return;
291         }
292
293         /* Power up fec phy */
294         gpio_set_value(TX28_FEC_PHY_POWER, 1);
295         msleep(26); /* 25ms according to data sheet */
296
297         /* Mode strap pins */
298         gpio_set_value(ENET0_RX_EN__GPIO_4_2, 1);
299         gpio_set_value(ENET0_RXD0__GPIO_4_3, 1);
300         gpio_set_value(ENET0_RXD1__GPIO_4_4, 1);
301
302         udelay(100); /* minimum assertion time for nRST */
303
304         /* Deasserting FEC PHY RESET */
305         gpio_set_value(TX28_FEC_PHY_RESET, 1);
306
307         pinctrl_put(pctl);
308 }
309
310 static void __init cfa10049_init(void)
311 {
312         update_fec_mac_prop(OUI_CRYSTALFONTZ);
313 }
314
315 static void __init cfa10037_init(void)
316 {
317         update_fec_mac_prop(OUI_CRYSTALFONTZ);
318 }
319
320 static void __init mxs_machine_init(void)
321 {
322         if (of_machine_is_compatible("fsl,imx28-evk"))
323                 imx28_evk_init();
324         else if (of_machine_is_compatible("bluegiga,apx4devkit"))
325                 apx4devkit_init();
326         else if (of_machine_is_compatible("crystalfontz,cfa10037"))
327                 cfa10037_init();
328         else if (of_machine_is_compatible("crystalfontz,cfa10049"))
329                 cfa10049_init();
330
331         of_platform_populate(NULL, of_default_bus_match_table,
332                              NULL, NULL);
333
334         if (of_machine_is_compatible("karo,tx28"))
335                 tx28_post_init();
336 }
337
338 #define MX23_CLKCTRL_RESET_OFFSET       0x120
339 #define MX28_CLKCTRL_RESET_OFFSET       0x1e0
340 #define MXS_CLKCTRL_RESET_CHIP          (1 << 1)
341
342 /*
343  * Reset the system. It is called by machine_restart().
344  */
345 static void mxs_restart(char mode, const char *cmd)
346 {
347         struct device_node *np;
348         void __iomem *reset_addr;
349
350         np = of_find_compatible_node(NULL, NULL, "fsl,clkctrl");
351         reset_addr = of_iomap(np, 0);
352         if (!reset_addr)
353                 goto soft;
354
355         if (of_device_is_compatible(np, "fsl,imx23-clkctrl"))
356                 reset_addr += MX23_CLKCTRL_RESET_OFFSET;
357         else
358                 reset_addr += MX28_CLKCTRL_RESET_OFFSET;
359
360         /* reset the chip */
361         __mxs_setl(MXS_CLKCTRL_RESET_CHIP, reset_addr);
362
363         pr_err("Failed to assert the chip reset\n");
364
365         /* Delay to allow the serial port to show the message */
366         mdelay(50);
367
368 soft:
369         /* We'll take a jump through zero as a poor second */
370         soft_restart(0);
371 }
372
373 static void __init mxs_timer_init(void)
374 {
375         if (of_machine_is_compatible("fsl,imx23"))
376                 mx23_clocks_init();
377         else
378                 mx28_clocks_init();
379         clocksource_of_init();
380 }
381
382 static const char *mxs_dt_compat[] __initdata = {
383         "fsl,imx28",
384         "fsl,imx23",
385         NULL,
386 };
387
388 DT_MACHINE_START(MXS, "Freescale MXS (Device Tree)")
389         .map_io         = debug_ll_io_init,
390         .init_irq       = irqchip_init,
391         .handle_irq     = icoll_handle_irq,
392         .init_time      = mxs_timer_init,
393         .init_machine   = mxs_machine_init,
394         .init_late      = mxs_pm_init,
395         .dt_compat      = mxs_dt_compat,
396         .restart        = mxs_restart,
397 MACHINE_END