blob: 233fe8a882649dfc4197b8ac58ff1dd0bdaed6e9 [file] [log] [blame]
Sean Crossbb389192013-09-26 11:24:47 +08001/*
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>
Lucas Stachd1dc9742014-03-28 17:52:59 +010028#include <linux/interrupt.h>
Sean Crossbb389192013-09-26 11:24:47 +080029
30#include "pcie-designware.h"
31
32#define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
33
34struct imx6_pcie {
35 int reset_gpio;
Lucas Stach57526132014-03-28 17:52:55 +010036 struct clk *pcie_bus;
37 struct clk *pcie_phy;
38 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080039 struct pcie_port pp;
40 struct regmap *iomuxc_gpr;
41 void __iomem *mem_base;
42};
43
Marek Vasutfa33a6d2013-12-12 22:50:02 +010044/* PCIe Root Complex registers (memory-mapped) */
45#define PCIE_RC_LCR 0x7c
46#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
47#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
48#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
49
Sean Crossbb389192013-09-26 11:24:47 +080050/* PCIe Port Logic registers (memory-mapped) */
51#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020052#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
53#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
54#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080055#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
56#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010057#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
58#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080059
60#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
61#define PCIE_PHY_CTRL_DATA_LOC 0
62#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
63#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
64#define PCIE_PHY_CTRL_WR_LOC 18
65#define PCIE_PHY_CTRL_RD_LOC 19
66
67#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
68#define PCIE_PHY_STAT_ACK_LOC 16
69
Marek Vasutfa33a6d2013-12-12 22:50:02 +010070#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
71#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
72
Sean Crossbb389192013-09-26 11:24:47 +080073/* PHY registers (not memory-mapped) */
74#define PCIE_PHY_RX_ASIC_OUT 0x100D
75
76#define PHY_RX_OVRD_IN_LO 0x1005
77#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
78#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
79
80static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
81{
82 u32 val;
83 u32 max_iterations = 10;
84 u32 wait_counter = 0;
85
86 do {
87 val = readl(dbi_base + PCIE_PHY_STAT);
88 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
89 wait_counter++;
90
91 if (val == exp_val)
92 return 0;
93
94 udelay(1);
95 } while (wait_counter < max_iterations);
96
97 return -ETIMEDOUT;
98}
99
100static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
101{
102 u32 val;
103 int ret;
104
105 val = addr << PCIE_PHY_CTRL_DATA_LOC;
106 writel(val, dbi_base + PCIE_PHY_CTRL);
107
108 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
109 writel(val, dbi_base + PCIE_PHY_CTRL);
110
111 ret = pcie_phy_poll_ack(dbi_base, 1);
112 if (ret)
113 return ret;
114
115 val = addr << PCIE_PHY_CTRL_DATA_LOC;
116 writel(val, dbi_base + PCIE_PHY_CTRL);
117
118 ret = pcie_phy_poll_ack(dbi_base, 0);
119 if (ret)
120 return ret;
121
122 return 0;
123}
124
125/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
126static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
127{
128 u32 val, phy_ctl;
129 int ret;
130
131 ret = pcie_phy_wait_ack(dbi_base, addr);
132 if (ret)
133 return ret;
134
135 /* assert Read signal */
136 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
137 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
138
139 ret = pcie_phy_poll_ack(dbi_base, 1);
140 if (ret)
141 return ret;
142
143 val = readl(dbi_base + PCIE_PHY_STAT);
144 *data = val & 0xffff;
145
146 /* deassert Read signal */
147 writel(0x00, dbi_base + PCIE_PHY_CTRL);
148
149 ret = pcie_phy_poll_ack(dbi_base, 0);
150 if (ret)
151 return ret;
152
153 return 0;
154}
155
156static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
157{
158 u32 var;
159 int ret;
160
161 /* write addr */
162 /* cap addr */
163 ret = pcie_phy_wait_ack(dbi_base, addr);
164 if (ret)
165 return ret;
166
167 var = data << PCIE_PHY_CTRL_DATA_LOC;
168 writel(var, dbi_base + PCIE_PHY_CTRL);
169
170 /* capture data */
171 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
172 writel(var, dbi_base + PCIE_PHY_CTRL);
173
174 ret = pcie_phy_poll_ack(dbi_base, 1);
175 if (ret)
176 return ret;
177
178 /* deassert cap data */
179 var = data << PCIE_PHY_CTRL_DATA_LOC;
180 writel(var, dbi_base + PCIE_PHY_CTRL);
181
182 /* wait for ack de-assertion */
183 ret = pcie_phy_poll_ack(dbi_base, 0);
184 if (ret)
185 return ret;
186
187 /* assert wr signal */
188 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
189 writel(var, dbi_base + PCIE_PHY_CTRL);
190
191 /* wait for ack */
192 ret = pcie_phy_poll_ack(dbi_base, 1);
193 if (ret)
194 return ret;
195
196 /* deassert wr signal */
197 var = data << PCIE_PHY_CTRL_DATA_LOC;
198 writel(var, dbi_base + PCIE_PHY_CTRL);
199
200 /* wait for ack de-assertion */
201 ret = pcie_phy_poll_ack(dbi_base, 0);
202 if (ret)
203 return ret;
204
205 writel(0x0, dbi_base + PCIE_PHY_CTRL);
206
207 return 0;
208}
209
210/* Added for PCI abort handling */
211static int imx6q_pcie_abort_handler(unsigned long addr,
212 unsigned int fsr, struct pt_regs *regs)
213{
Sean Crossbb389192013-09-26 11:24:47 +0800214 return 0;
215}
216
217static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
218{
219 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200220 u32 val, gpr1, gpr12;
221
222 /*
223 * If the bootloader already enabled the link we need some special
224 * handling to get the core back into a state where it is safe to
225 * touch it for configuration. As there is no dedicated reset signal
226 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
227 * state before completely disabling LTSSM, which is a prerequisite
228 * for core configuration.
229 *
230 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
231 * indication that the bootloader activated the link.
232 */
233 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
234 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
235
236 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
237 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
238 val = readl(pp->dbi_base + PCIE_PL_PFLR);
239 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
240 val |= PCIE_PL_PFLR_FORCE_LINK;
241 writel(val, pp->dbi_base + PCIE_PL_PFLR);
242
243 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
244 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
245 }
Sean Crossbb389192013-09-26 11:24:47 +0800246
247 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
248 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800249 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
250 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
251
Sean Crossbb389192013-09-26 11:24:47 +0800252 return 0;
253}
254
255static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
256{
257 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
258 int ret;
259
Lucas Stach57526132014-03-28 17:52:55 +0100260 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800261 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100262 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
263 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800264 }
265
Lucas Stach57526132014-03-28 17:52:55 +0100266 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800267 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100268 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
269 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800270 }
271
Lucas Stach57526132014-03-28 17:52:55 +0100272 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800273 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100274 dev_err(pp->dev, "unable to enable pcie clock\n");
275 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800276 }
277
278 /* allow the clocks to stabilize */
279 usleep_range(200, 500);
280
Tim Harvey3fce0e82014-08-07 23:36:40 -0700281 /* power up core phy and enable ref clock */
282 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
283 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
284 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
285 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
286
Richard Zhubc9ef772013-12-12 22:50:03 +0100287 /* Some boards don't have PCIe reset GPIO. */
288 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
289 gpio_set_value(imx6_pcie->reset_gpio, 0);
290 msleep(100);
291 gpio_set_value(imx6_pcie->reset_gpio, 1);
292 }
Sean Crossbb389192013-09-26 11:24:47 +0800293 return 0;
294
Lucas Stach57526132014-03-28 17:52:55 +0100295err_pcie:
296 clk_disable_unprepare(imx6_pcie->pcie_bus);
297err_pcie_bus:
298 clk_disable_unprepare(imx6_pcie->pcie_phy);
299err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800300 return ret;
301
302}
303
304static void imx6_pcie_init_phy(struct pcie_port *pp)
305{
306 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
307
308 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
309 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
310
311 /* configure constant input signal to the pcie ctrl and phy */
312 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
313 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
314 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
315 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
316
317 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
318 IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
319 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
320 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
321 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
322 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
323 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
324 IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
325 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
326 IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
327}
328
Marek Vasut66a60f92013-12-12 22:50:01 +0100329static int imx6_pcie_wait_for_link(struct pcie_port *pp)
330{
331 int count = 200;
332
333 while (!dw_pcie_link_up(pp)) {
334 usleep_range(100, 1000);
335 if (--count)
336 continue;
337
338 dev_err(pp->dev, "phy link never came up\n");
339 dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
340 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
341 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
342 return -EINVAL;
343 }
344
345 return 0;
346}
347
Lucas Stachd1dc9742014-03-28 17:52:59 +0100348static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
349{
350 struct pcie_port *pp = arg;
351
352 return dw_handle_msi_irq(pp);
353}
354
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100355static int imx6_pcie_start_link(struct pcie_port *pp)
356{
357 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
358 uint32_t tmp;
359 int ret, count;
360
361 /*
362 * Force Gen1 operation when starting the link. In case the link is
363 * started in Gen2 mode, there is a possibility the devices on the
364 * bus will not be detected at all. This happens with PCIe switches.
365 */
366 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
367 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
368 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
369 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
370
371 /* Start LTSSM. */
372 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
373 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
374
375 ret = imx6_pcie_wait_for_link(pp);
376 if (ret)
377 return ret;
378
379 /* Allow Gen2 mode after the link is up. */
380 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
381 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
382 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
383 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
384
385 /*
386 * Start Directed Speed Change so the best possible speed both link
387 * partners support can be negotiated.
388 */
389 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
390 tmp |= PORT_LOGIC_SPEED_CHANGE;
391 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
392
393 count = 200;
394 while (count--) {
395 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
396 /* Test if the speed change finished. */
397 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
398 break;
399 usleep_range(100, 1000);
400 }
401
402 /* Make sure link training is finished as well! */
403 if (count)
404 ret = imx6_pcie_wait_for_link(pp);
405 else
406 ret = -EINVAL;
407
408 if (ret) {
409 dev_err(pp->dev, "Failed to bring link up!\n");
410 } else {
411 tmp = readl(pp->dbi_base + 0x80);
412 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
413 }
414
415 return ret;
416}
417
Sean Crossbb389192013-09-26 11:24:47 +0800418static void imx6_pcie_host_init(struct pcie_port *pp)
419{
Sean Crossbb389192013-09-26 11:24:47 +0800420 imx6_pcie_assert_core_reset(pp);
421
422 imx6_pcie_init_phy(pp);
423
424 imx6_pcie_deassert_core_reset(pp);
425
426 dw_pcie_setup_rc(pp);
427
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100428 imx6_pcie_start_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100429
430 if (IS_ENABLED(CONFIG_PCI_MSI))
431 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800432}
433
Marek Vasut982aa232013-12-12 22:50:00 +0100434static void imx6_pcie_reset_phy(struct pcie_port *pp)
435{
436 uint32_t temp;
437
438 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
439 temp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
440 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
441 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, temp);
442
443 usleep_range(2000, 3000);
444
445 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &temp);
446 temp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
447 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
448 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, temp);
449}
450
Sean Crossbb389192013-09-26 11:24:47 +0800451static int imx6_pcie_link_up(struct pcie_port *pp)
452{
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700453 u32 rc, debug_r0, rx_valid;
454 int count = 5;
Sean Crossbb389192013-09-26 11:24:47 +0800455
Marek Vasut7f9f40c2013-12-12 22:49:59 +0100456 /*
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700457 * Test if the PHY reports that the link is up and also that the LTSSM
458 * training finished. There are three possible states of the link when
459 * this code is called:
460 * 1) The link is DOWN (unlikely)
461 * The link didn't come up yet for some reason. This usually means
462 * we have a real problem somewhere. Reset the PHY and exit. This
463 * state calls for inspection of the DEBUG registers.
464 * 2) The link is UP, but still in LTSSM training
465 * Wait for the training to finish, which should take a very short
466 * time. If the training does not finish, we have a problem and we
467 * need to inspect the DEBUG registers. If the training does finish,
468 * the link is up and operating correctly.
469 * 3) The link is UP and no longer in LTSSM training
470 * The link is up and operating correctly.
Marek Vasut7f9f40c2013-12-12 22:49:59 +0100471 */
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700472 while (1) {
473 rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
474 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP))
475 break;
476 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
477 return 1;
478 if (!count--)
479 break;
480 dev_dbg(pp->dev, "Link is up, but still in training\n");
481 /*
482 * Wait a little bit, then re-check if the link finished
483 * the training.
484 */
485 usleep_range(1000, 2000);
486 }
Sean Crossbb389192013-09-26 11:24:47 +0800487 /*
488 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
489 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
490 * If (MAC/LTSSM.state == Recovery.RcvrLock)
491 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
492 * to gen2 is stuck
493 */
494 pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700495 debug_r0 = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0);
Sean Crossbb389192013-09-26 11:24:47 +0800496
497 if (rx_valid & 0x01)
498 return 0;
499
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700500 if ((debug_r0 & 0x3f) != 0x0d)
Sean Crossbb389192013-09-26 11:24:47 +0800501 return 0;
502
503 dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700504 dev_dbg(pp->dev, "debug_r0=%08x debug_r1=%08x\n", debug_r0, rc);
Sean Crossbb389192013-09-26 11:24:47 +0800505
Marek Vasut982aa232013-12-12 22:50:00 +0100506 imx6_pcie_reset_phy(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800507
508 return 0;
509}
510
511static struct pcie_host_ops imx6_pcie_host_ops = {
512 .link_up = imx6_pcie_link_up,
513 .host_init = imx6_pcie_host_init,
514};
515
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530516static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800517 struct platform_device *pdev)
518{
519 int ret;
520
Lucas Stachd1dc9742014-03-28 17:52:59 +0100521 if (IS_ENABLED(CONFIG_PCI_MSI)) {
522 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
523 if (pp->msi_irq <= 0) {
524 dev_err(&pdev->dev, "failed to get MSI irq\n");
525 return -ENODEV;
526 }
527
528 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
529 imx6_pcie_msi_handler,
530 IRQF_SHARED, "mx6-pcie-msi", pp);
531 if (ret) {
532 dev_err(&pdev->dev, "failed to request MSI irq\n");
533 return -ENODEV;
534 }
535 }
536
Sean Crossbb389192013-09-26 11:24:47 +0800537 pp->root_bus_nr = -1;
538 pp->ops = &imx6_pcie_host_ops;
539
Sean Crossbb389192013-09-26 11:24:47 +0800540 ret = dw_pcie_host_init(pp);
541 if (ret) {
542 dev_err(&pdev->dev, "failed to initialize host\n");
543 return ret;
544 }
545
546 return 0;
547}
548
549static int __init imx6_pcie_probe(struct platform_device *pdev)
550{
551 struct imx6_pcie *imx6_pcie;
552 struct pcie_port *pp;
553 struct device_node *np = pdev->dev.of_node;
554 struct resource *dbi_base;
555 int ret;
556
557 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
558 if (!imx6_pcie)
559 return -ENOMEM;
560
561 pp = &imx6_pcie->pp;
562 pp->dev = &pdev->dev;
563
564 /* Added for PCI abort handling */
565 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
566 "imprecise external abort");
567
568 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800569 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200570 if (IS_ERR(pp->dbi_base))
571 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800572
573 /* Fetch GPIOs */
574 imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Marek Vasutc28f8a12013-12-12 22:49:58 +0100575 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
576 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
577 GPIOF_OUT_INIT_LOW, "PCIe reset");
578 if (ret) {
579 dev_err(&pdev->dev, "unable to get reset gpio\n");
580 return ret;
581 }
Sean Crossbb389192013-09-26 11:24:47 +0800582 }
583
Sean Crossbb389192013-09-26 11:24:47 +0800584 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100585 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
586 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800587 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100588 "pcie_phy clock source missing or invalid\n");
589 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800590 }
591
Lucas Stach57526132014-03-28 17:52:55 +0100592 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
593 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800594 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100595 "pcie_bus clock source missing or invalid\n");
596 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800597 }
598
Lucas Stach57526132014-03-28 17:52:55 +0100599 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
600 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800601 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100602 "pcie clock source missing or invalid\n");
603 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800604 }
605
606 /* Grab GPR config register range */
607 imx6_pcie->iomuxc_gpr =
608 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
609 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
610 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200611 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800612 }
613
614 ret = imx6_add_pcie_port(pp, pdev);
615 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200616 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800617
618 platform_set_drvdata(pdev, imx6_pcie);
619 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800620}
621
Lucas Stach3e3e4062014-07-31 20:16:05 +0200622static void imx6_pcie_shutdown(struct platform_device *pdev)
623{
624 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
625
626 /* bring down link, so bootloader gets clean state in case of reboot */
627 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
628}
629
Sean Crossbb389192013-09-26 11:24:47 +0800630static const struct of_device_id imx6_pcie_of_match[] = {
631 { .compatible = "fsl,imx6q-pcie", },
632 {},
633};
634MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
635
636static struct platform_driver imx6_pcie_driver = {
637 .driver = {
638 .name = "imx6q-pcie",
639 .owner = THIS_MODULE,
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530640 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800641 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200642 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800643};
644
645/* Freescale PCIe driver does not allow module unload */
646
647static int __init imx6_pcie_init(void)
648{
649 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
650}
Lucas Stach61da50d2014-09-05 09:36:48 -0600651module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800652
653MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
654MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
655MODULE_LICENSE("GPL v2");