Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCIe host controller driver for Samsung Exynos SoCs
4 *
5 * Copyright (C) 2013-2020 Samsung Electronics Co., Ltd.
6 * https://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
9 * Jaehoon Chung <jh80.chung@samsung.com>
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/pci.h>
18#include <linux/platform_device.h>
19#include <linux/phy/phy.h>
20#include <linux/regulator/consumer.h>
21#include <linux/mod_devicetable.h>
22#include <linux/module.h>
23
24#include "pcie-designware.h"
25
26#define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
27
28/* PCIe ELBI registers */
29#define PCIE_IRQ_PULSE 0x000
30#define IRQ_INTA_ASSERT BIT(0)
31#define IRQ_INTB_ASSERT BIT(2)
32#define IRQ_INTC_ASSERT BIT(4)
33#define IRQ_INTD_ASSERT BIT(6)
34#define PCIE_IRQ_LEVEL 0x004
35#define PCIE_IRQ_SPECIAL 0x008
36#define PCIE_IRQ_EN_PULSE 0x00c
37#define PCIE_IRQ_EN_LEVEL 0x010
38#define PCIE_IRQ_EN_SPECIAL 0x014
39#define PCIE_SW_WAKE 0x018
40#define PCIE_BUS_EN BIT(1)
41#define PCIE_CORE_RESET 0x01c
42#define PCIE_CORE_RESET_ENABLE BIT(0)
43#define PCIE_STICKY_RESET 0x020
44#define PCIE_NONSTICKY_RESET 0x024
45#define PCIE_APP_INIT_RESET 0x028
46#define PCIE_APP_LTSSM_ENABLE 0x02c
47#define PCIE_ELBI_RDLH_LINKUP 0x074
48#define PCIE_ELBI_XMLH_LINKUP BIT(4)
49#define PCIE_ELBI_LTSSM_ENABLE 0x1
50#define PCIE_ELBI_SLV_AWMISC 0x11c
51#define PCIE_ELBI_SLV_ARMISC 0x120
52#define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
53
54struct exynos_pcie {
55 struct dw_pcie pci;
56 void __iomem *elbi_base;
57 struct clk *clk;
58 struct clk *bus_clk;
59 struct phy *phy;
60 struct regulator_bulk_data supplies[2];
61};
62
63static int exynos_pcie_init_clk_resources(struct exynos_pcie *ep)
64{
65 struct device *dev = ep->pci.dev;
66 int ret;
67
68 ret = clk_prepare_enable(ep->clk);
69 if (ret) {
70 dev_err(dev, "cannot enable pcie rc clock");
71 return ret;
72 }
73
74 ret = clk_prepare_enable(ep->bus_clk);
75 if (ret) {
76 dev_err(dev, "cannot enable pcie bus clock");
77 goto err_bus_clk;
78 }
79
80 return 0;
81
82err_bus_clk:
83 clk_disable_unprepare(ep->clk);
84
85 return ret;
86}
87
88static void exynos_pcie_deinit_clk_resources(struct exynos_pcie *ep)
89{
90 clk_disable_unprepare(ep->bus_clk);
91 clk_disable_unprepare(ep->clk);
92}
93
94static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
95{
96 writel(val, base + reg);
97}
98
99static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
100{
101 return readl(base + reg);
102}
103
104static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
105{
106 u32 val;
107
108 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_AWMISC);
109 if (on)
110 val |= PCIE_ELBI_SLV_DBI_ENABLE;
111 else
112 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
113 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
114}
115
116static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
117{
118 u32 val;
119
120 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_ARMISC);
121 if (on)
122 val |= PCIE_ELBI_SLV_DBI_ENABLE;
123 else
124 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
125 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
126}
127
128static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
129{
130 u32 val;
131
132 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
133 val &= ~PCIE_CORE_RESET_ENABLE;
134 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
135 exynos_pcie_writel(ep->elbi_base, 0, PCIE_STICKY_RESET);
136 exynos_pcie_writel(ep->elbi_base, 0, PCIE_NONSTICKY_RESET);
137}
138
139static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
140{
141 u32 val;
142
143 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
144 val |= PCIE_CORE_RESET_ENABLE;
145
146 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
147 exynos_pcie_writel(ep->elbi_base, 1, PCIE_STICKY_RESET);
148 exynos_pcie_writel(ep->elbi_base, 1, PCIE_NONSTICKY_RESET);
149 exynos_pcie_writel(ep->elbi_base, 1, PCIE_APP_INIT_RESET);
150 exynos_pcie_writel(ep->elbi_base, 0, PCIE_APP_INIT_RESET);
151}
152
153static int exynos_pcie_start_link(struct dw_pcie *pci)
154{
155 struct exynos_pcie *ep = to_exynos_pcie(pci);
156 u32 val;
157
158 val = exynos_pcie_readl(ep->elbi_base, PCIE_SW_WAKE);
159 val &= ~PCIE_BUS_EN;
160 exynos_pcie_writel(ep->elbi_base, val, PCIE_SW_WAKE);
161
162 /* assert LTSSM enable */
163 exynos_pcie_writel(ep->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
164 PCIE_APP_LTSSM_ENABLE);
165 return 0;
166}
167
168static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
169{
170 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_IRQ_PULSE);
171
172 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_PULSE);
173}
174
175static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
176{
177 struct exynos_pcie *ep = arg;
178
179 exynos_pcie_clear_irq_pulse(ep);
180 return IRQ_HANDLED;
181}
182
183static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
184{
185 u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
186 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
187
188 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_EN_PULSE);
189 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
190 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
191}
192
193static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
194 u32 reg, size_t size)
195{
196 struct exynos_pcie *ep = to_exynos_pcie(pci);
197 u32 val;
198
199 exynos_pcie_sideband_dbi_r_mode(ep, true);
200 dw_pcie_read(base + reg, size, &val);
201 exynos_pcie_sideband_dbi_r_mode(ep, false);
202 return val;
203}
204
205static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
206 u32 reg, size_t size, u32 val)
207{
208 struct exynos_pcie *ep = to_exynos_pcie(pci);
209
210 exynos_pcie_sideband_dbi_w_mode(ep, true);
211 dw_pcie_write(base + reg, size, val);
212 exynos_pcie_sideband_dbi_w_mode(ep, false);
213}
214
215static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
216 int where, int size, u32 *val)
217{
218 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
219
220 if (PCI_SLOT(devfn))
221 return PCIBIOS_DEVICE_NOT_FOUND;
222
223 *val = dw_pcie_read_dbi(pci, where, size);
224 return PCIBIOS_SUCCESSFUL;
225}
226
227static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
228 int where, int size, u32 val)
229{
230 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
231
232 if (PCI_SLOT(devfn))
233 return PCIBIOS_DEVICE_NOT_FOUND;
234
235 dw_pcie_write_dbi(pci, where, size, val);
236 return PCIBIOS_SUCCESSFUL;
237}
238
239static struct pci_ops exynos_pci_ops = {
240 .read = exynos_pcie_rd_own_conf,
241 .write = exynos_pcie_wr_own_conf,
242};
243
244static int exynos_pcie_link_up(struct dw_pcie *pci)
245{
246 struct exynos_pcie *ep = to_exynos_pcie(pci);
247 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_RDLH_LINKUP);
248
249 return (val & PCIE_ELBI_XMLH_LINKUP);
250}
251
252static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
253{
254 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
255 struct exynos_pcie *ep = to_exynos_pcie(pci);
256
257 pp->bridge->ops = &exynos_pci_ops;
258
259 exynos_pcie_assert_core_reset(ep);
260
261 phy_init(ep->phy);
262 phy_power_on(ep->phy);
263
264 exynos_pcie_deassert_core_reset(ep);
265 exynos_pcie_enable_irq_pulse(ep);
266
267 return 0;
268}
269
270static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
271 .init = exynos_pcie_host_init,
272};
273
274static int exynos_add_pcie_port(struct exynos_pcie *ep,
275 struct platform_device *pdev)
276{
277 struct dw_pcie *pci = &ep->pci;
278 struct dw_pcie_rp *pp = &pci->pp;
279 struct device *dev = &pdev->dev;
280 int ret;
281
282 pp->irq = platform_get_irq(pdev, 0);
283 if (pp->irq < 0)
284 return pp->irq;
285
286 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
287 IRQF_SHARED, "exynos-pcie", ep);
288 if (ret) {
289 dev_err(dev, "failed to request irq\n");
290 return ret;
291 }
292
293 pp->ops = &exynos_pcie_host_ops;
294 pp->msi_irq[0] = -ENODEV;
295
296 ret = dw_pcie_host_init(pp);
297 if (ret) {
298 dev_err(dev, "failed to initialize host\n");
299 return ret;
300 }
301
302 return 0;
303}
304
305static const struct dw_pcie_ops dw_pcie_ops = {
306 .read_dbi = exynos_pcie_read_dbi,
307 .write_dbi = exynos_pcie_write_dbi,
308 .link_up = exynos_pcie_link_up,
309 .start_link = exynos_pcie_start_link,
310};
311
312static int exynos_pcie_probe(struct platform_device *pdev)
313{
314 struct device *dev = &pdev->dev;
315 struct exynos_pcie *ep;
316 struct device_node *np = dev->of_node;
317 int ret;
318
319 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
320 if (!ep)
321 return -ENOMEM;
322
323 ep->pci.dev = dev;
324 ep->pci.ops = &dw_pcie_ops;
325
326 ep->phy = devm_of_phy_get(dev, np, NULL);
327 if (IS_ERR(ep->phy))
328 return PTR_ERR(ep->phy);
329
330 /* External Local Bus interface (ELBI) registers */
331 ep->elbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
332 if (IS_ERR(ep->elbi_base))
333 return PTR_ERR(ep->elbi_base);
334
335 ep->clk = devm_clk_get(dev, "pcie");
336 if (IS_ERR(ep->clk)) {
337 dev_err(dev, "Failed to get pcie rc clock\n");
338 return PTR_ERR(ep->clk);
339 }
340
341 ep->bus_clk = devm_clk_get(dev, "pcie_bus");
342 if (IS_ERR(ep->bus_clk)) {
343 dev_err(dev, "Failed to get pcie bus clock\n");
344 return PTR_ERR(ep->bus_clk);
345 }
346
347 ep->supplies[0].supply = "vdd18";
348 ep->supplies[1].supply = "vdd10";
349 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
350 ep->supplies);
351 if (ret)
352 return ret;
353
354 ret = exynos_pcie_init_clk_resources(ep);
355 if (ret)
356 return ret;
357
358 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
359 if (ret)
360 return ret;
361
362 platform_set_drvdata(pdev, ep);
363
364 ret = exynos_add_pcie_port(ep, pdev);
365 if (ret < 0)
366 goto fail_probe;
367
368 return 0;
369
370fail_probe:
371 phy_exit(ep->phy);
372 exynos_pcie_deinit_clk_resources(ep);
373 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
374
375 return ret;
376}
377
378static void exynos_pcie_remove(struct platform_device *pdev)
379{
380 struct exynos_pcie *ep = platform_get_drvdata(pdev);
381
382 dw_pcie_host_deinit(&ep->pci.pp);
383 exynos_pcie_assert_core_reset(ep);
384 phy_power_off(ep->phy);
385 phy_exit(ep->phy);
386 exynos_pcie_deinit_clk_resources(ep);
387 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
388}
389
390static int exynos_pcie_suspend_noirq(struct device *dev)
391{
392 struct exynos_pcie *ep = dev_get_drvdata(dev);
393
394 exynos_pcie_assert_core_reset(ep);
395 phy_power_off(ep->phy);
396 phy_exit(ep->phy);
397 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
398
399 return 0;
400}
401
402static int exynos_pcie_resume_noirq(struct device *dev)
403{
404 struct exynos_pcie *ep = dev_get_drvdata(dev);
405 struct dw_pcie *pci = &ep->pci;
406 struct dw_pcie_rp *pp = &pci->pp;
407 int ret;
408
409 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
410 if (ret)
411 return ret;
412
413 /* exynos_pcie_host_init controls ep->phy */
414 exynos_pcie_host_init(pp);
415 dw_pcie_setup_rc(pp);
416 exynos_pcie_start_link(pci);
417 return dw_pcie_wait_for_link(pci);
418}
419
420static const struct dev_pm_ops exynos_pcie_pm_ops = {
421 NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
422 exynos_pcie_resume_noirq)
423};
424
425static const struct of_device_id exynos_pcie_of_match[] = {
426 { .compatible = "samsung,exynos5433-pcie", },
427 { },
428};
429
430static struct platform_driver exynos_pcie_driver = {
431 .probe = exynos_pcie_probe,
432 .remove_new = exynos_pcie_remove,
433 .driver = {
434 .name = "exynos-pcie",
435 .of_match_table = exynos_pcie_of_match,
436 .pm = &exynos_pcie_pm_ops,
437 },
438};
439module_platform_driver(exynos_pcie_driver);
440MODULE_LICENSE("GPL v2");
441MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * PCIe host controller driver for Samsung Exynos SoCs
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * https://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
9 */
10
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/of_device.h>
18#include <linux/of_gpio.h>
19#include <linux/pci.h>
20#include <linux/platform_device.h>
21#include <linux/phy/phy.h>
22#include <linux/resource.h>
23#include <linux/signal.h>
24#include <linux/types.h>
25
26#include "pcie-designware.h"
27
28#define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
29
30/* PCIe ELBI registers */
31#define PCIE_IRQ_PULSE 0x000
32#define IRQ_INTA_ASSERT BIT(0)
33#define IRQ_INTB_ASSERT BIT(2)
34#define IRQ_INTC_ASSERT BIT(4)
35#define IRQ_INTD_ASSERT BIT(6)
36#define PCIE_IRQ_LEVEL 0x004
37#define PCIE_IRQ_SPECIAL 0x008
38#define PCIE_IRQ_EN_PULSE 0x00c
39#define PCIE_IRQ_EN_LEVEL 0x010
40#define IRQ_MSI_ENABLE BIT(2)
41#define PCIE_IRQ_EN_SPECIAL 0x014
42#define PCIE_PWR_RESET 0x018
43#define PCIE_CORE_RESET 0x01c
44#define PCIE_CORE_RESET_ENABLE BIT(0)
45#define PCIE_STICKY_RESET 0x020
46#define PCIE_NONSTICKY_RESET 0x024
47#define PCIE_APP_INIT_RESET 0x028
48#define PCIE_APP_LTSSM_ENABLE 0x02c
49#define PCIE_ELBI_RDLH_LINKUP 0x064
50#define PCIE_ELBI_LTSSM_ENABLE 0x1
51#define PCIE_ELBI_SLV_AWMISC 0x11c
52#define PCIE_ELBI_SLV_ARMISC 0x120
53#define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
54
55struct exynos_pcie_mem_res {
56 void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
57};
58
59struct exynos_pcie_clk_res {
60 struct clk *clk;
61 struct clk *bus_clk;
62};
63
64struct exynos_pcie {
65 struct dw_pcie *pci;
66 struct exynos_pcie_mem_res *mem_res;
67 struct exynos_pcie_clk_res *clk_res;
68 const struct exynos_pcie_ops *ops;
69 int reset_gpio;
70
71 struct phy *phy;
72};
73
74struct exynos_pcie_ops {
75 int (*get_mem_resources)(struct platform_device *pdev,
76 struct exynos_pcie *ep);
77 int (*get_clk_resources)(struct exynos_pcie *ep);
78 int (*init_clk_resources)(struct exynos_pcie *ep);
79 void (*deinit_clk_resources)(struct exynos_pcie *ep);
80};
81
82static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
83 struct exynos_pcie *ep)
84{
85 struct dw_pcie *pci = ep->pci;
86 struct device *dev = pci->dev;
87
88 ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
89 if (!ep->mem_res)
90 return -ENOMEM;
91
92 ep->mem_res->elbi_base = devm_platform_ioremap_resource(pdev, 0);
93 if (IS_ERR(ep->mem_res->elbi_base))
94 return PTR_ERR(ep->mem_res->elbi_base);
95
96 return 0;
97}
98
99static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
100{
101 struct dw_pcie *pci = ep->pci;
102 struct device *dev = pci->dev;
103
104 ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
105 if (!ep->clk_res)
106 return -ENOMEM;
107
108 ep->clk_res->clk = devm_clk_get(dev, "pcie");
109 if (IS_ERR(ep->clk_res->clk)) {
110 dev_err(dev, "Failed to get pcie rc clock\n");
111 return PTR_ERR(ep->clk_res->clk);
112 }
113
114 ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
115 if (IS_ERR(ep->clk_res->bus_clk)) {
116 dev_err(dev, "Failed to get pcie bus clock\n");
117 return PTR_ERR(ep->clk_res->bus_clk);
118 }
119
120 return 0;
121}
122
123static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
124{
125 struct dw_pcie *pci = ep->pci;
126 struct device *dev = pci->dev;
127 int ret;
128
129 ret = clk_prepare_enable(ep->clk_res->clk);
130 if (ret) {
131 dev_err(dev, "cannot enable pcie rc clock");
132 return ret;
133 }
134
135 ret = clk_prepare_enable(ep->clk_res->bus_clk);
136 if (ret) {
137 dev_err(dev, "cannot enable pcie bus clock");
138 goto err_bus_clk;
139 }
140
141 return 0;
142
143err_bus_clk:
144 clk_disable_unprepare(ep->clk_res->clk);
145
146 return ret;
147}
148
149static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
150{
151 clk_disable_unprepare(ep->clk_res->bus_clk);
152 clk_disable_unprepare(ep->clk_res->clk);
153}
154
155static const struct exynos_pcie_ops exynos5440_pcie_ops = {
156 .get_mem_resources = exynos5440_pcie_get_mem_resources,
157 .get_clk_resources = exynos5440_pcie_get_clk_resources,
158 .init_clk_resources = exynos5440_pcie_init_clk_resources,
159 .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources,
160};
161
162static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
163{
164 writel(val, base + reg);
165}
166
167static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
168{
169 return readl(base + reg);
170}
171
172static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
173{
174 u32 val;
175
176 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
177 if (on)
178 val |= PCIE_ELBI_SLV_DBI_ENABLE;
179 else
180 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
181 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
182}
183
184static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
185{
186 u32 val;
187
188 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
189 if (on)
190 val |= PCIE_ELBI_SLV_DBI_ENABLE;
191 else
192 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
193 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
194}
195
196static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
197{
198 u32 val;
199
200 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
201 val &= ~PCIE_CORE_RESET_ENABLE;
202 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
203 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
204 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
205 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
206}
207
208static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
209{
210 u32 val;
211
212 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
213 val |= PCIE_CORE_RESET_ENABLE;
214
215 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
216 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
217 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
218 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
219 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
220}
221
222static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
223{
224 struct dw_pcie *pci = ep->pci;
225 struct device *dev = pci->dev;
226
227 if (ep->reset_gpio >= 0)
228 devm_gpio_request_one(dev, ep->reset_gpio,
229 GPIOF_OUT_INIT_HIGH, "RESET");
230}
231
232static int exynos_pcie_establish_link(struct exynos_pcie *ep)
233{
234 struct dw_pcie *pci = ep->pci;
235 struct pcie_port *pp = &pci->pp;
236 struct device *dev = pci->dev;
237
238 if (dw_pcie_link_up(pci)) {
239 dev_err(dev, "Link already up\n");
240 return 0;
241 }
242
243 exynos_pcie_assert_core_reset(ep);
244
245 phy_reset(ep->phy);
246
247 exynos_pcie_writel(ep->mem_res->elbi_base, 1,
248 PCIE_PWR_RESET);
249
250 phy_power_on(ep->phy);
251 phy_init(ep->phy);
252
253 exynos_pcie_deassert_core_reset(ep);
254 dw_pcie_setup_rc(pp);
255 exynos_pcie_assert_reset(ep);
256
257 /* assert LTSSM enable */
258 exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
259 PCIE_APP_LTSSM_ENABLE);
260
261 /* check if the link is up or not */
262 if (!dw_pcie_wait_for_link(pci))
263 return 0;
264
265 phy_power_off(ep->phy);
266 return -ETIMEDOUT;
267}
268
269static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
270{
271 u32 val;
272
273 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
274 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
275}
276
277static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
278{
279 u32 val;
280
281 /* enable INTX interrupt */
282 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
283 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
284 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
285}
286
287static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
288{
289 struct exynos_pcie *ep = arg;
290
291 exynos_pcie_clear_irq_pulse(ep);
292 return IRQ_HANDLED;
293}
294
295static void exynos_pcie_msi_init(struct exynos_pcie *ep)
296{
297 struct dw_pcie *pci = ep->pci;
298 struct pcie_port *pp = &pci->pp;
299 u32 val;
300
301 dw_pcie_msi_init(pp);
302
303 /* enable MSI interrupt */
304 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
305 val |= IRQ_MSI_ENABLE;
306 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
307}
308
309static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
310{
311 exynos_pcie_enable_irq_pulse(ep);
312
313 if (IS_ENABLED(CONFIG_PCI_MSI))
314 exynos_pcie_msi_init(ep);
315}
316
317static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
318 u32 reg, size_t size)
319{
320 struct exynos_pcie *ep = to_exynos_pcie(pci);
321 u32 val;
322
323 exynos_pcie_sideband_dbi_r_mode(ep, true);
324 dw_pcie_read(base + reg, size, &val);
325 exynos_pcie_sideband_dbi_r_mode(ep, false);
326 return val;
327}
328
329static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
330 u32 reg, size_t size, u32 val)
331{
332 struct exynos_pcie *ep = to_exynos_pcie(pci);
333
334 exynos_pcie_sideband_dbi_w_mode(ep, true);
335 dw_pcie_write(base + reg, size, val);
336 exynos_pcie_sideband_dbi_w_mode(ep, false);
337}
338
339static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
340 u32 *val)
341{
342 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
343 struct exynos_pcie *ep = to_exynos_pcie(pci);
344 int ret;
345
346 exynos_pcie_sideband_dbi_r_mode(ep, true);
347 ret = dw_pcie_read(pci->dbi_base + where, size, val);
348 exynos_pcie_sideband_dbi_r_mode(ep, false);
349 return ret;
350}
351
352static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
353 u32 val)
354{
355 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
356 struct exynos_pcie *ep = to_exynos_pcie(pci);
357 int ret;
358
359 exynos_pcie_sideband_dbi_w_mode(ep, true);
360 ret = dw_pcie_write(pci->dbi_base + where, size, val);
361 exynos_pcie_sideband_dbi_w_mode(ep, false);
362 return ret;
363}
364
365static int exynos_pcie_link_up(struct dw_pcie *pci)
366{
367 struct exynos_pcie *ep = to_exynos_pcie(pci);
368 u32 val;
369
370 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
371 if (val == PCIE_ELBI_LTSSM_ENABLE)
372 return 1;
373
374 return 0;
375}
376
377static int exynos_pcie_host_init(struct pcie_port *pp)
378{
379 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
380 struct exynos_pcie *ep = to_exynos_pcie(pci);
381
382 exynos_pcie_establish_link(ep);
383 exynos_pcie_enable_interrupts(ep);
384
385 return 0;
386}
387
388static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
389 .rd_own_conf = exynos_pcie_rd_own_conf,
390 .wr_own_conf = exynos_pcie_wr_own_conf,
391 .host_init = exynos_pcie_host_init,
392};
393
394static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
395 struct platform_device *pdev)
396{
397 struct dw_pcie *pci = ep->pci;
398 struct pcie_port *pp = &pci->pp;
399 struct device *dev = &pdev->dev;
400 int ret;
401
402 pp->irq = platform_get_irq(pdev, 1);
403 if (pp->irq < 0)
404 return pp->irq;
405
406 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
407 IRQF_SHARED, "exynos-pcie", ep);
408 if (ret) {
409 dev_err(dev, "failed to request irq\n");
410 return ret;
411 }
412
413 if (IS_ENABLED(CONFIG_PCI_MSI)) {
414 pp->msi_irq = platform_get_irq(pdev, 0);
415 if (pp->msi_irq < 0)
416 return pp->msi_irq;
417 }
418
419 pp->ops = &exynos_pcie_host_ops;
420
421 ret = dw_pcie_host_init(pp);
422 if (ret) {
423 dev_err(dev, "failed to initialize host\n");
424 return ret;
425 }
426
427 return 0;
428}
429
430static const struct dw_pcie_ops dw_pcie_ops = {
431 .read_dbi = exynos_pcie_read_dbi,
432 .write_dbi = exynos_pcie_write_dbi,
433 .link_up = exynos_pcie_link_up,
434};
435
436static int __init exynos_pcie_probe(struct platform_device *pdev)
437{
438 struct device *dev = &pdev->dev;
439 struct dw_pcie *pci;
440 struct exynos_pcie *ep;
441 struct device_node *np = dev->of_node;
442 int ret;
443
444 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
445 if (!ep)
446 return -ENOMEM;
447
448 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
449 if (!pci)
450 return -ENOMEM;
451
452 pci->dev = dev;
453 pci->ops = &dw_pcie_ops;
454
455 ep->pci = pci;
456 ep->ops = (const struct exynos_pcie_ops *)
457 of_device_get_match_data(dev);
458
459 ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
460
461 ep->phy = devm_of_phy_get(dev, np, NULL);
462 if (IS_ERR(ep->phy)) {
463 if (PTR_ERR(ep->phy) != -ENODEV)
464 return PTR_ERR(ep->phy);
465
466 ep->phy = NULL;
467 }
468
469 if (ep->ops && ep->ops->get_mem_resources) {
470 ret = ep->ops->get_mem_resources(pdev, ep);
471 if (ret)
472 return ret;
473 }
474
475 if (ep->ops && ep->ops->get_clk_resources &&
476 ep->ops->init_clk_resources) {
477 ret = ep->ops->get_clk_resources(ep);
478 if (ret)
479 return ret;
480 ret = ep->ops->init_clk_resources(ep);
481 if (ret)
482 return ret;
483 }
484
485 platform_set_drvdata(pdev, ep);
486
487 ret = exynos_add_pcie_port(ep, pdev);
488 if (ret < 0)
489 goto fail_probe;
490
491 return 0;
492
493fail_probe:
494 phy_exit(ep->phy);
495
496 if (ep->ops && ep->ops->deinit_clk_resources)
497 ep->ops->deinit_clk_resources(ep);
498 return ret;
499}
500
501static int __exit exynos_pcie_remove(struct platform_device *pdev)
502{
503 struct exynos_pcie *ep = platform_get_drvdata(pdev);
504
505 if (ep->ops && ep->ops->deinit_clk_resources)
506 ep->ops->deinit_clk_resources(ep);
507
508 return 0;
509}
510
511static const struct of_device_id exynos_pcie_of_match[] = {
512 {
513 .compatible = "samsung,exynos5440-pcie",
514 .data = &exynos5440_pcie_ops
515 },
516 {},
517};
518
519static struct platform_driver exynos_pcie_driver = {
520 .remove = __exit_p(exynos_pcie_remove),
521 .driver = {
522 .name = "exynos-pcie",
523 .of_match_table = exynos_pcie_of_match,
524 },
525};
526
527/* Exynos PCIe driver does not allow module unload */
528
529static int __init exynos_pcie_init(void)
530{
531 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
532}
533subsys_initcall(exynos_pcie_init);