Merge tag 'iwlwifi-next-for-kalle-2014-12-30' of https://git.kernel.org/pub/scm/linux...
[cascardo/linux.git] / arch / arm / mach-imx / mach-imx6q.c
1 /*
2  * Copyright 2011-2013 Freescale Semiconductor, Inc.
3  * Copyright 2011 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/clkdev.h>
15 #include <linux/cpu.h>
16 #include <linux/delay.h>
17 #include <linux/export.h>
18 #include <linux/init.h>
19 #include <linux/io.h>
20 #include <linux/irq.h>
21 #include <linux/irqchip.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_platform.h>
26 #include <linux/pm_opp.h>
27 #include <linux/pci.h>
28 #include <linux/phy.h>
29 #include <linux/reboot.h>
30 #include <linux/regmap.h>
31 #include <linux/micrel_phy.h>
32 #include <linux/mfd/syscon.h>
33 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
34 #include <linux/fec.h>
35 #include <linux/netdevice.h>
36 #include <asm/mach/arch.h>
37 #include <asm/mach/map.h>
38 #include <asm/system_misc.h>
39
40 #include "common.h"
41 #include "cpuidle.h"
42 #include "hardware.h"
43
44 static struct fec_platform_data fec_pdata;
45
46 static void imx6q_fec_sleep_enable(int enabled)
47 {
48         struct regmap *gpr;
49
50         gpr = syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
51         if (!IS_ERR(gpr)) {
52                 if (enabled)
53                         regmap_update_bits(gpr, IOMUXC_GPR13,
54                                            IMX6Q_GPR13_ENET_STOP_REQ,
55                                            IMX6Q_GPR13_ENET_STOP_REQ);
56
57                 else
58                         regmap_update_bits(gpr, IOMUXC_GPR13,
59                                            IMX6Q_GPR13_ENET_STOP_REQ, 0);
60         } else
61                 pr_err("failed to find fsl,imx6q-iomux-gpr regmap\n");
62 }
63
64 static void __init imx6q_enet_plt_init(void)
65 {
66         struct device_node *np;
67
68         np = of_find_node_by_path("/soc/aips-bus@02100000/ethernet@02188000");
69         if (np && of_get_property(np, "fsl,magic-packet", NULL))
70                 fec_pdata.sleep_mode_enable = imx6q_fec_sleep_enable;
71 }
72
73 /* For imx6q sabrelite board: set KSZ9021RN RGMII pad skew */
74 static int ksz9021rn_phy_fixup(struct phy_device *phydev)
75 {
76         if (IS_BUILTIN(CONFIG_PHYLIB)) {
77                 /* min rx data delay */
78                 phy_write(phydev, MICREL_KSZ9021_EXTREG_CTRL,
79                         0x8000 | MICREL_KSZ9021_RGMII_RX_DATA_PAD_SCEW);
80                 phy_write(phydev, MICREL_KSZ9021_EXTREG_DATA_WRITE, 0x0000);
81
82                 /* max rx/tx clock delay, min rx/tx control delay */
83                 phy_write(phydev, MICREL_KSZ9021_EXTREG_CTRL,
84                         0x8000 | MICREL_KSZ9021_RGMII_CLK_CTRL_PAD_SCEW);
85                 phy_write(phydev, MICREL_KSZ9021_EXTREG_DATA_WRITE, 0xf0f0);
86                 phy_write(phydev, MICREL_KSZ9021_EXTREG_CTRL,
87                         MICREL_KSZ9021_RGMII_CLK_CTRL_PAD_SCEW);
88         }
89
90         return 0;
91 }
92
93 static void mmd_write_reg(struct phy_device *dev, int device, int reg, int val)
94 {
95         phy_write(dev, 0x0d, device);
96         phy_write(dev, 0x0e, reg);
97         phy_write(dev, 0x0d, (1 << 14) | device);
98         phy_write(dev, 0x0e, val);
99 }
100
101 static int ksz9031rn_phy_fixup(struct phy_device *dev)
102 {
103         /*
104          * min rx data delay, max rx/tx clock delay,
105          * min rx/tx control delay
106          */
107         mmd_write_reg(dev, 2, 4, 0);
108         mmd_write_reg(dev, 2, 5, 0);
109         mmd_write_reg(dev, 2, 8, 0x003ff);
110
111         return 0;
112 }
113
114 /*
115  * fixup for PLX PEX8909 bridge to configure GPIO1-7 as output High
116  * as they are used for slots1-7 PERST#
117  */
118 static void ventana_pciesw_early_fixup(struct pci_dev *dev)
119 {
120         u32 dw;
121
122         if (!of_machine_is_compatible("gw,ventana"))
123                 return;
124
125         if (dev->devfn != 0)
126                 return;
127
128         pci_read_config_dword(dev, 0x62c, &dw);
129         dw |= 0xaaa8; // GPIO1-7 outputs
130         pci_write_config_dword(dev, 0x62c, dw);
131
132         pci_read_config_dword(dev, 0x644, &dw);
133         dw |= 0xfe;   // GPIO1-7 output high
134         pci_write_config_dword(dev, 0x644, dw);
135
136         msleep(100);
137 }
138 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_PLX, 0x8609, ventana_pciesw_early_fixup);
139 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_PLX, 0x8606, ventana_pciesw_early_fixup);
140 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_PLX, 0x8604, ventana_pciesw_early_fixup);
141
142 static int ar8031_phy_fixup(struct phy_device *dev)
143 {
144         u16 val;
145
146         /* To enable AR8031 output a 125MHz clk from CLK_25M */
147         phy_write(dev, 0xd, 0x7);
148         phy_write(dev, 0xe, 0x8016);
149         phy_write(dev, 0xd, 0x4007);
150
151         val = phy_read(dev, 0xe);
152         val &= 0xffe3;
153         val |= 0x18;
154         phy_write(dev, 0xe, val);
155
156         /* introduce tx clock delay */
157         phy_write(dev, 0x1d, 0x5);
158         val = phy_read(dev, 0x1e);
159         val |= 0x0100;
160         phy_write(dev, 0x1e, val);
161
162         return 0;
163 }
164
165 #define PHY_ID_AR8031   0x004dd074
166
167 static int ar8035_phy_fixup(struct phy_device *dev)
168 {
169         u16 val;
170
171         /* Ar803x phy SmartEEE feature cause link status generates glitch,
172          * which cause ethernet link down/up issue, so disable SmartEEE
173          */
174         phy_write(dev, 0xd, 0x3);
175         phy_write(dev, 0xe, 0x805d);
176         phy_write(dev, 0xd, 0x4003);
177
178         val = phy_read(dev, 0xe);
179         phy_write(dev, 0xe, val & ~(1 << 8));
180
181         /*
182          * Enable 125MHz clock from CLK_25M on the AR8031.  This
183          * is fed in to the IMX6 on the ENET_REF_CLK (V22) pad.
184          * Also, introduce a tx clock delay.
185          *
186          * This is the same as is the AR8031 fixup.
187          */
188         ar8031_phy_fixup(dev);
189
190         /*check phy power*/
191         val = phy_read(dev, 0x0);
192         if (val & BMCR_PDOWN)
193                 phy_write(dev, 0x0, val & ~BMCR_PDOWN);
194
195         return 0;
196 }
197
198 #define PHY_ID_AR8035 0x004dd072
199
200 static void __init imx6q_enet_phy_init(void)
201 {
202         if (IS_BUILTIN(CONFIG_PHYLIB)) {
203                 phy_register_fixup_for_uid(PHY_ID_KSZ9021, MICREL_PHY_ID_MASK,
204                                 ksz9021rn_phy_fixup);
205                 phy_register_fixup_for_uid(PHY_ID_KSZ9031, MICREL_PHY_ID_MASK,
206                                 ksz9031rn_phy_fixup);
207                 phy_register_fixup_for_uid(PHY_ID_AR8031, 0xffffffff,
208                                 ar8031_phy_fixup);
209                 phy_register_fixup_for_uid(PHY_ID_AR8035, 0xffffffef,
210                                 ar8035_phy_fixup);
211         }
212 }
213
214 static void __init imx6q_1588_init(void)
215 {
216         struct device_node *np;
217         struct clk *ptp_clk;
218         struct clk *enet_ref;
219         struct regmap *gpr;
220         u32 clksel;
221
222         np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-fec");
223         if (!np) {
224                 pr_warn("%s: failed to find fec node\n", __func__);
225                 return;
226         }
227
228         ptp_clk = of_clk_get(np, 2);
229         if (IS_ERR(ptp_clk)) {
230                 pr_warn("%s: failed to get ptp clock\n", __func__);
231                 goto put_node;
232         }
233
234         enet_ref = clk_get_sys(NULL, "enet_ref");
235         if (IS_ERR(enet_ref)) {
236                 pr_warn("%s: failed to get enet clock\n", __func__);
237                 goto put_ptp_clk;
238         }
239
240         /*
241          * If enet_ref from ANATOP/CCM is the PTP clock source, we need to
242          * set bit IOMUXC_GPR1[21].  Or the PTP clock must be from pad
243          * (external OSC), and we need to clear the bit.
244          */
245         clksel = ptp_clk == enet_ref ? IMX6Q_GPR1_ENET_CLK_SEL_ANATOP :
246                                        IMX6Q_GPR1_ENET_CLK_SEL_PAD;
247         gpr = syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
248         if (!IS_ERR(gpr))
249                 regmap_update_bits(gpr, IOMUXC_GPR1,
250                                 IMX6Q_GPR1_ENET_CLK_SEL_MASK,
251                                 clksel);
252         else
253                 pr_err("failed to find fsl,imx6q-iomux-gpr regmap\n");
254
255         clk_put(enet_ref);
256 put_ptp_clk:
257         clk_put(ptp_clk);
258 put_node:
259         of_node_put(np);
260 }
261
262 static void __init imx6q_axi_init(void)
263 {
264         struct regmap *gpr;
265         unsigned int mask;
266
267         gpr = syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
268         if (!IS_ERR(gpr)) {
269                 /*
270                  * Enable the cacheable attribute of VPU and IPU
271                  * AXI transactions.
272                  */
273                 mask = IMX6Q_GPR4_VPU_WR_CACHE_SEL |
274                         IMX6Q_GPR4_VPU_RD_CACHE_SEL |
275                         IMX6Q_GPR4_VPU_P_WR_CACHE_VAL |
276                         IMX6Q_GPR4_VPU_P_RD_CACHE_VAL_MASK |
277                         IMX6Q_GPR4_IPU_WR_CACHE_CTL |
278                         IMX6Q_GPR4_IPU_RD_CACHE_CTL;
279                 regmap_update_bits(gpr, IOMUXC_GPR4, mask, mask);
280
281                 /* Increase IPU read QoS priority */
282                 regmap_update_bits(gpr, IOMUXC_GPR6,
283                                 IMX6Q_GPR6_IPU1_ID00_RD_QOS_MASK |
284                                 IMX6Q_GPR6_IPU1_ID01_RD_QOS_MASK,
285                                 (0xf << 16) | (0x7 << 20));
286                 regmap_update_bits(gpr, IOMUXC_GPR7,
287                                 IMX6Q_GPR7_IPU2_ID00_RD_QOS_MASK |
288                                 IMX6Q_GPR7_IPU2_ID01_RD_QOS_MASK,
289                                 (0xf << 16) | (0x7 << 20));
290         } else {
291                 pr_warn("failed to find fsl,imx6q-iomuxc-gpr regmap\n");
292         }
293 }
294
295 /* Add auxdata to pass platform data */
296 static const struct of_dev_auxdata imx6q_auxdata_lookup[] __initconst = {
297         OF_DEV_AUXDATA("fsl,imx6q-fec", 0x02188000, NULL, &fec_pdata),
298         { /* sentinel */ }
299 };
300
301 static void __init imx6q_init_machine(void)
302 {
303         struct device *parent;
304
305         imx_print_silicon_rev(cpu_is_imx6dl() ? "i.MX6DL" : "i.MX6Q",
306                               imx_get_soc_revision());
307
308         parent = imx_soc_device_init();
309         if (parent == NULL)
310                 pr_warn("failed to initialize soc device\n");
311
312         imx6q_enet_phy_init();
313
314         of_platform_populate(NULL, of_default_bus_match_table,
315                              imx6q_auxdata_lookup, parent);
316
317         imx_anatop_init();
318         cpu_is_imx6q() ?  imx6q_pm_init() : imx6dl_pm_init();
319         imx6q_1588_init();
320         imx6q_enet_plt_init();
321         imx6q_axi_init();
322 }
323
324 #define OCOTP_CFG3                      0x440
325 #define OCOTP_CFG3_SPEED_SHIFT          16
326 #define OCOTP_CFG3_SPEED_1P2GHZ         0x3
327 #define OCOTP_CFG3_SPEED_996MHZ         0x2
328 #define OCOTP_CFG3_SPEED_852MHZ         0x1
329
330 static void __init imx6q_opp_check_speed_grading(struct device *cpu_dev)
331 {
332         struct device_node *np;
333         void __iomem *base;
334         u32 val;
335
336         np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ocotp");
337         if (!np) {
338                 pr_warn("failed to find ocotp node\n");
339                 return;
340         }
341
342         base = of_iomap(np, 0);
343         if (!base) {
344                 pr_warn("failed to map ocotp\n");
345                 goto put_node;
346         }
347
348         /*
349          * SPEED_GRADING[1:0] defines the max speed of ARM:
350          * 2b'11: 1200000000Hz;
351          * 2b'10: 996000000Hz;
352          * 2b'01: 852000000Hz; -- i.MX6Q Only, exclusive with 996MHz.
353          * 2b'00: 792000000Hz;
354          * We need to set the max speed of ARM according to fuse map.
355          */
356         val = readl_relaxed(base + OCOTP_CFG3);
357         val >>= OCOTP_CFG3_SPEED_SHIFT;
358         val &= 0x3;
359
360         if ((val != OCOTP_CFG3_SPEED_1P2GHZ) && cpu_is_imx6q())
361                 if (dev_pm_opp_disable(cpu_dev, 1200000000))
362                         pr_warn("failed to disable 1.2 GHz OPP\n");
363         if (val < OCOTP_CFG3_SPEED_996MHZ)
364                 if (dev_pm_opp_disable(cpu_dev, 996000000))
365                         pr_warn("failed to disable 996 MHz OPP\n");
366         if (cpu_is_imx6q()) {
367                 if (val != OCOTP_CFG3_SPEED_852MHZ)
368                         if (dev_pm_opp_disable(cpu_dev, 852000000))
369                                 pr_warn("failed to disable 852 MHz OPP\n");
370         }
371
372 put_node:
373         of_node_put(np);
374 }
375
376 static void __init imx6q_opp_init(void)
377 {
378         struct device_node *np;
379         struct device *cpu_dev = get_cpu_device(0);
380
381         if (!cpu_dev) {
382                 pr_warn("failed to get cpu0 device\n");
383                 return;
384         }
385         np = of_node_get(cpu_dev->of_node);
386         if (!np) {
387                 pr_warn("failed to find cpu0 node\n");
388                 return;
389         }
390
391         if (of_init_opp_table(cpu_dev)) {
392                 pr_warn("failed to init OPP table\n");
393                 goto put_node;
394         }
395
396         imx6q_opp_check_speed_grading(cpu_dev);
397
398 put_node:
399         of_node_put(np);
400 }
401
402 static struct platform_device imx6q_cpufreq_pdev = {
403         .name = "imx6q-cpufreq",
404 };
405
406 static void __init imx6q_init_late(void)
407 {
408         /*
409          * WAIT mode is broken on TO 1.0 and 1.1, so there is no point
410          * to run cpuidle on them.
411          */
412         if (imx_get_soc_revision() > IMX_CHIP_REVISION_1_1)
413                 imx6q_cpuidle_init();
414
415         if (IS_ENABLED(CONFIG_ARM_IMX6Q_CPUFREQ)) {
416                 imx6q_opp_init();
417                 platform_device_register(&imx6q_cpufreq_pdev);
418         }
419 }
420
421 static void __init imx6q_map_io(void)
422 {
423         debug_ll_io_init();
424         imx_scu_map_io();
425 }
426
427 static void __init imx6q_init_irq(void)
428 {
429         imx_init_revision_from_anatop();
430         imx_init_l2cache();
431         imx_src_init();
432         imx_gpc_init();
433         irqchip_init();
434 }
435
436 static const char * const imx6q_dt_compat[] __initconst = {
437         "fsl,imx6dl",
438         "fsl,imx6q",
439         NULL,
440 };
441
442 DT_MACHINE_START(IMX6Q, "Freescale i.MX6 Quad/DualLite (Device Tree)")
443         .smp            = smp_ops(imx_smp_ops),
444         .map_io         = imx6q_map_io,
445         .init_irq       = imx6q_init_irq,
446         .init_machine   = imx6q_init_machine,
447         .init_late      = imx6q_init_late,
448         .dt_compat      = imx6q_dt_compat,
449 MACHINE_END