1176bddee1cc0a18d233f8553c2aa7f4cae62741
[cascardo/linux.git] / drivers / pci / host / pci-imx6.c
1 /*
2  * PCIe host controller driver for Freescale i.MX6 SoCs
3  *
4  * Copyright (C) 2013 Kosagi
5  *              http://www.kosagi.com
6  *
7  * Author: Sean Cross <xobs@kosagi.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20 #include <linux/module.h>
21 #include <linux/of_gpio.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/regmap.h>
25 #include <linux/resource.h>
26 #include <linux/signal.h>
27 #include <linux/types.h>
28
29 #include "pcie-designware.h"
30
31 #define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
32
33 struct imx6_pcie {
34         int                     reset_gpio;
35         int                     power_on_gpio;
36         int                     wake_up_gpio;
37         int                     disable_gpio;
38         struct clk              *lvds_gate;
39         struct clk              *sata_ref_100m;
40         struct clk              *pcie_ref_125m;
41         struct clk              *pcie_axi;
42         struct pcie_port        pp;
43         struct regmap           *iomuxc_gpr;
44         void __iomem            *mem_base;
45 };
46
47 /* PCIe Port Logic registers (memory-mapped) */
48 #define PL_OFFSET 0x700
49 #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
50 #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
51
52 #define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
53 #define PCIE_PHY_CTRL_DATA_LOC 0
54 #define PCIE_PHY_CTRL_CAP_ADR_LOC 16
55 #define PCIE_PHY_CTRL_CAP_DAT_LOC 17
56 #define PCIE_PHY_CTRL_WR_LOC 18
57 #define PCIE_PHY_CTRL_RD_LOC 19
58
59 #define PCIE_PHY_STAT (PL_OFFSET + 0x110)
60 #define PCIE_PHY_STAT_ACK_LOC 16
61
62 /* PHY registers (not memory-mapped) */
63 #define PCIE_PHY_RX_ASIC_OUT 0x100D
64
65 #define PHY_RX_OVRD_IN_LO 0x1005
66 #define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
67 #define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
68
69 static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
70 {
71         u32 val;
72         u32 max_iterations = 10;
73         u32 wait_counter = 0;
74
75         do {
76                 val = readl(dbi_base + PCIE_PHY_STAT);
77                 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
78                 wait_counter++;
79
80                 if (val == exp_val)
81                         return 0;
82
83                 udelay(1);
84         } while (wait_counter < max_iterations);
85
86         return -ETIMEDOUT;
87 }
88
89 static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
90 {
91         u32 val;
92         int ret;
93
94         val = addr << PCIE_PHY_CTRL_DATA_LOC;
95         writel(val, dbi_base + PCIE_PHY_CTRL);
96
97         val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
98         writel(val, dbi_base + PCIE_PHY_CTRL);
99
100         ret = pcie_phy_poll_ack(dbi_base, 1);
101         if (ret)
102                 return ret;
103
104         val = addr << PCIE_PHY_CTRL_DATA_LOC;
105         writel(val, dbi_base + PCIE_PHY_CTRL);
106
107         ret = pcie_phy_poll_ack(dbi_base, 0);
108         if (ret)
109                 return ret;
110
111         return 0;
112 }
113
114 /* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
115 static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
116 {
117         u32 val, phy_ctl;
118         int ret;
119
120         ret = pcie_phy_wait_ack(dbi_base, addr);
121         if (ret)
122                 return ret;
123
124         /* assert Read signal */
125         phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
126         writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
127
128         ret = pcie_phy_poll_ack(dbi_base, 1);
129         if (ret)
130                 return ret;
131
132         val = readl(dbi_base + PCIE_PHY_STAT);
133         *data = val & 0xffff;
134
135         /* deassert Read signal */
136         writel(0x00, dbi_base + PCIE_PHY_CTRL);
137
138         ret = pcie_phy_poll_ack(dbi_base, 0);
139         if (ret)
140                 return ret;
141
142         return 0;
143 }
144
145 static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
146 {
147         u32 var;
148         int ret;
149
150         /* write addr */
151         /* cap addr */
152         ret = pcie_phy_wait_ack(dbi_base, addr);
153         if (ret)
154                 return ret;
155
156         var = data << PCIE_PHY_CTRL_DATA_LOC;
157         writel(var, dbi_base + PCIE_PHY_CTRL);
158
159         /* capture data */
160         var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
161         writel(var, dbi_base + PCIE_PHY_CTRL);
162
163         ret = pcie_phy_poll_ack(dbi_base, 1);
164         if (ret)
165                 return ret;
166
167         /* deassert cap data */
168         var = data << PCIE_PHY_CTRL_DATA_LOC;
169         writel(var, dbi_base + PCIE_PHY_CTRL);
170
171         /* wait for ack de-assertion */
172         ret = pcie_phy_poll_ack(dbi_base, 0);
173         if (ret)
174                 return ret;
175
176         /* assert wr signal */
177         var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
178         writel(var, dbi_base + PCIE_PHY_CTRL);
179
180         /* wait for ack */
181         ret = pcie_phy_poll_ack(dbi_base, 1);
182         if (ret)
183                 return ret;
184
185         /* deassert wr signal */
186         var = data << PCIE_PHY_CTRL_DATA_LOC;
187         writel(var, dbi_base + PCIE_PHY_CTRL);
188
189         /* wait for ack de-assertion */
190         ret = pcie_phy_poll_ack(dbi_base, 0);
191         if (ret)
192                 return ret;
193
194         writel(0x0, dbi_base + PCIE_PHY_CTRL);
195
196         return 0;
197 }
198
199 /*  Added for PCI abort handling */
200 static int imx6q_pcie_abort_handler(unsigned long addr,
201                 unsigned int fsr, struct pt_regs *regs)
202 {
203         return 0;
204 }
205
206 static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
207 {
208         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
209
210         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
211                         IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
212         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
213                         IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
214         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
215                         IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
216
217         /* Some boards don't have PCIe reset GPIO. */
218         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
219                 gpio_set_value(imx6_pcie->reset_gpio, 0);
220                 msleep(100);
221                 gpio_set_value(imx6_pcie->reset_gpio, 1);
222         }
223
224         return 0;
225 }
226
227 static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
228 {
229         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
230         int ret;
231
232         if (gpio_is_valid(imx6_pcie->power_on_gpio))
233                 gpio_set_value(imx6_pcie->power_on_gpio, 1);
234
235         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
236                         IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
237         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
238                         IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
239
240         ret = clk_prepare_enable(imx6_pcie->sata_ref_100m);
241         if (ret) {
242                 dev_err(pp->dev, "unable to enable sata_ref_100m\n");
243                 goto err_sata_ref;
244         }
245
246         ret = clk_prepare_enable(imx6_pcie->pcie_ref_125m);
247         if (ret) {
248                 dev_err(pp->dev, "unable to enable pcie_ref_125m\n");
249                 goto err_pcie_ref;
250         }
251
252         ret = clk_prepare_enable(imx6_pcie->lvds_gate);
253         if (ret) {
254                 dev_err(pp->dev, "unable to enable lvds_gate\n");
255                 goto err_lvds_gate;
256         }
257
258         ret = clk_prepare_enable(imx6_pcie->pcie_axi);
259         if (ret) {
260                 dev_err(pp->dev, "unable to enable pcie_axi\n");
261                 goto err_pcie_axi;
262         }
263
264         /* allow the clocks to stabilize */
265         usleep_range(200, 500);
266
267         return 0;
268
269 err_pcie_axi:
270         clk_disable_unprepare(imx6_pcie->lvds_gate);
271 err_lvds_gate:
272         clk_disable_unprepare(imx6_pcie->pcie_ref_125m);
273 err_pcie_ref:
274         clk_disable_unprepare(imx6_pcie->sata_ref_100m);
275 err_sata_ref:
276         return ret;
277
278 }
279
280 static void imx6_pcie_init_phy(struct pcie_port *pp)
281 {
282         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
283
284         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
285                         IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
286
287         /* configure constant input signal to the pcie ctrl and phy */
288         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
289                         IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
290         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
291                         IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
292
293         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
294                         IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
295         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
296                         IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
297         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
298                         IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
299         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
300                         IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
301         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
302                         IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
303 }
304
305 static void imx6_pcie_host_init(struct pcie_port *pp)
306 {
307         int count = 0;
308         struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
309
310         imx6_pcie_assert_core_reset(pp);
311
312         imx6_pcie_init_phy(pp);
313
314         imx6_pcie_deassert_core_reset(pp);
315
316         dw_pcie_setup_rc(pp);
317
318         regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
319                         IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
320
321         while (!dw_pcie_link_up(pp)) {
322                 usleep_range(100, 1000);
323                 count++;
324                 if (count >= 200) {
325                         dev_err(pp->dev, "phy link never came up\n");
326                         dev_dbg(pp->dev,
327                                 "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
328                                 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
329                                 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
330                         break;
331                 }
332         }
333
334         return;
335 }
336
337 static int imx6_pcie_link_up(struct pcie_port *pp)
338 {
339         u32 rc, ltssm, rx_valid, temp;
340
341         /* link is debug bit 36, debug register 1 starts at bit 32 */
342         rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & (0x1 << (36 - 32));
343         if (rc)
344                 return -EAGAIN;
345
346         /*
347          * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
348          * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
349          * If (MAC/LTSSM.state == Recovery.RcvrLock)
350          * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
351          * to gen2 is stuck
352          */
353         pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
354         ltssm = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0) & 0x3F;
355
356         if (rx_valid & 0x01)
357                 return 0;
358
359         if (ltssm != 0x0d)
360                 return 0;
361
362         dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
363
364         pcie_phy_read(pp->dbi_base,
365                 PHY_RX_OVRD_IN_LO, &temp);
366         temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN
367                 | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
368         pcie_phy_write(pp->dbi_base,
369                 PHY_RX_OVRD_IN_LO, temp);
370
371         usleep_range(2000, 3000);
372
373         pcie_phy_read(pp->dbi_base,
374                 PHY_RX_OVRD_IN_LO, &temp);
375         temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN
376                 | PHY_RX_OVRD_IN_LO_RX_PLL_EN);
377         pcie_phy_write(pp->dbi_base,
378                 PHY_RX_OVRD_IN_LO, temp);
379
380         return 0;
381 }
382
383 static struct pcie_host_ops imx6_pcie_host_ops = {
384         .link_up = imx6_pcie_link_up,
385         .host_init = imx6_pcie_host_init,
386 };
387
388 static int imx6_add_pcie_port(struct pcie_port *pp,
389                         struct platform_device *pdev)
390 {
391         int ret;
392
393         pp->irq = platform_get_irq(pdev, 0);
394         if (!pp->irq) {
395                 dev_err(&pdev->dev, "failed to get irq\n");
396                 return -ENODEV;
397         }
398
399         pp->root_bus_nr = -1;
400         pp->ops = &imx6_pcie_host_ops;
401
402         spin_lock_init(&pp->conf_lock);
403         ret = dw_pcie_host_init(pp);
404         if (ret) {
405                 dev_err(&pdev->dev, "failed to initialize host\n");
406                 return ret;
407         }
408
409         return 0;
410 }
411
412 static int __init imx6_pcie_probe(struct platform_device *pdev)
413 {
414         struct imx6_pcie *imx6_pcie;
415         struct pcie_port *pp;
416         struct device_node *np = pdev->dev.of_node;
417         struct resource *dbi_base;
418         int ret;
419
420         imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
421         if (!imx6_pcie)
422                 return -ENOMEM;
423
424         pp = &imx6_pcie->pp;
425         pp->dev = &pdev->dev;
426
427         /* Added for PCI abort handling */
428         hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
429                 "imprecise external abort");
430
431         dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
432         pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
433         if (IS_ERR(pp->dbi_base))
434                 return PTR_ERR(pp->dbi_base);
435
436         /* Fetch GPIOs */
437         imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
438         if (gpio_is_valid(imx6_pcie->reset_gpio)) {
439                 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
440                                             GPIOF_OUT_INIT_LOW, "PCIe reset");
441                 if (ret) {
442                         dev_err(&pdev->dev, "unable to get reset gpio\n");
443                         return ret;
444                 }
445         }
446
447         imx6_pcie->power_on_gpio = of_get_named_gpio(np, "power-on-gpio", 0);
448         if (gpio_is_valid(imx6_pcie->power_on_gpio)) {
449                 ret = devm_gpio_request_one(&pdev->dev,
450                                         imx6_pcie->power_on_gpio,
451                                         GPIOF_OUT_INIT_LOW,
452                                         "PCIe power enable");
453                 if (ret) {
454                         dev_err(&pdev->dev, "unable to get power-on gpio\n");
455                         return ret;
456                 }
457         }
458
459         imx6_pcie->wake_up_gpio = of_get_named_gpio(np, "wake-up-gpio", 0);
460         if (gpio_is_valid(imx6_pcie->wake_up_gpio)) {
461                 ret = devm_gpio_request_one(&pdev->dev,
462                                         imx6_pcie->wake_up_gpio,
463                                         GPIOF_IN,
464                                         "PCIe wake up");
465                 if (ret) {
466                         dev_err(&pdev->dev, "unable to get wake-up gpio\n");
467                         return ret;
468                 }
469         }
470
471         imx6_pcie->disable_gpio = of_get_named_gpio(np, "disable-gpio", 0);
472         if (gpio_is_valid(imx6_pcie->disable_gpio)) {
473                 ret = devm_gpio_request_one(&pdev->dev,
474                                         imx6_pcie->disable_gpio,
475                                         GPIOF_OUT_INIT_HIGH,
476                                         "PCIe disable endpoint");
477                 if (ret) {
478                         dev_err(&pdev->dev, "unable to get disable-ep gpio\n");
479                         return ret;
480                 }
481         }
482
483         /* Fetch clocks */
484         imx6_pcie->lvds_gate = devm_clk_get(&pdev->dev, "lvds_gate");
485         if (IS_ERR(imx6_pcie->lvds_gate)) {
486                 dev_err(&pdev->dev,
487                         "lvds_gate clock select missing or invalid\n");
488                 return PTR_ERR(imx6_pcie->lvds_gate);
489         }
490
491         imx6_pcie->sata_ref_100m = devm_clk_get(&pdev->dev, "sata_ref_100m");
492         if (IS_ERR(imx6_pcie->sata_ref_100m)) {
493                 dev_err(&pdev->dev,
494                         "sata_ref_100m clock source missing or invalid\n");
495                 return PTR_ERR(imx6_pcie->sata_ref_100m);
496         }
497
498         imx6_pcie->pcie_ref_125m = devm_clk_get(&pdev->dev, "pcie_ref_125m");
499         if (IS_ERR(imx6_pcie->pcie_ref_125m)) {
500                 dev_err(&pdev->dev,
501                         "pcie_ref_125m clock source missing or invalid\n");
502                 return PTR_ERR(imx6_pcie->pcie_ref_125m);
503         }
504
505         imx6_pcie->pcie_axi = devm_clk_get(&pdev->dev, "pcie_axi");
506         if (IS_ERR(imx6_pcie->pcie_axi)) {
507                 dev_err(&pdev->dev,
508                         "pcie_axi clock source missing or invalid\n");
509                 return PTR_ERR(imx6_pcie->pcie_axi);
510         }
511
512         /* Grab GPR config register range */
513         imx6_pcie->iomuxc_gpr =
514                  syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
515         if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
516                 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
517                 return PTR_ERR(imx6_pcie->iomuxc_gpr);
518         }
519
520         ret = imx6_add_pcie_port(pp, pdev);
521         if (ret < 0)
522                 return ret;
523
524         platform_set_drvdata(pdev, imx6_pcie);
525         return 0;
526 }
527
528 static const struct of_device_id imx6_pcie_of_match[] = {
529         { .compatible = "fsl,imx6q-pcie", },
530         {},
531 };
532 MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
533
534 static struct platform_driver imx6_pcie_driver = {
535         .driver = {
536                 .name   = "imx6q-pcie",
537                 .owner  = THIS_MODULE,
538                 .of_match_table = imx6_pcie_of_match,
539         },
540 };
541
542 /* Freescale PCIe driver does not allow module unload */
543
544 static int __init imx6_pcie_init(void)
545 {
546         return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
547 }
548 fs_initcall(imx6_pcie_init);
549
550 MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
551 MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
552 MODULE_LICENSE("GPL v2");