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_bulk_data *clks;
58 struct phy *phy;
59 struct regulator_bulk_data supplies[2];
60};
61
62static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
63{
64 writel(val, base + reg);
65}
66
67static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
68{
69 return readl(base + reg);
70}
71
72static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
73{
74 u32 val;
75
76 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_AWMISC);
77 if (on)
78 val |= PCIE_ELBI_SLV_DBI_ENABLE;
79 else
80 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
81 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
82}
83
84static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
85{
86 u32 val;
87
88 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_ARMISC);
89 if (on)
90 val |= PCIE_ELBI_SLV_DBI_ENABLE;
91 else
92 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
93 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
94}
95
96static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
97{
98 u32 val;
99
100 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
101 val &= ~PCIE_CORE_RESET_ENABLE;
102 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
103 exynos_pcie_writel(ep->elbi_base, 0, PCIE_STICKY_RESET);
104 exynos_pcie_writel(ep->elbi_base, 0, PCIE_NONSTICKY_RESET);
105}
106
107static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
108{
109 u32 val;
110
111 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
112 val |= PCIE_CORE_RESET_ENABLE;
113
114 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
115 exynos_pcie_writel(ep->elbi_base, 1, PCIE_STICKY_RESET);
116 exynos_pcie_writel(ep->elbi_base, 1, PCIE_NONSTICKY_RESET);
117 exynos_pcie_writel(ep->elbi_base, 1, PCIE_APP_INIT_RESET);
118 exynos_pcie_writel(ep->elbi_base, 0, PCIE_APP_INIT_RESET);
119}
120
121static int exynos_pcie_start_link(struct dw_pcie *pci)
122{
123 struct exynos_pcie *ep = to_exynos_pcie(pci);
124 u32 val;
125
126 val = exynos_pcie_readl(ep->elbi_base, PCIE_SW_WAKE);
127 val &= ~PCIE_BUS_EN;
128 exynos_pcie_writel(ep->elbi_base, val, PCIE_SW_WAKE);
129
130 /* assert LTSSM enable */
131 exynos_pcie_writel(ep->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
132 PCIE_APP_LTSSM_ENABLE);
133 return 0;
134}
135
136static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
137{
138 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_IRQ_PULSE);
139
140 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_PULSE);
141}
142
143static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
144{
145 struct exynos_pcie *ep = arg;
146
147 exynos_pcie_clear_irq_pulse(ep);
148 return IRQ_HANDLED;
149}
150
151static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
152{
153 u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
154 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
155
156 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_EN_PULSE);
157 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
158 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
159}
160
161static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
162 u32 reg, size_t size)
163{
164 struct exynos_pcie *ep = to_exynos_pcie(pci);
165 u32 val;
166
167 exynos_pcie_sideband_dbi_r_mode(ep, true);
168 dw_pcie_read(base + reg, size, &val);
169 exynos_pcie_sideband_dbi_r_mode(ep, false);
170 return val;
171}
172
173static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
174 u32 reg, size_t size, u32 val)
175{
176 struct exynos_pcie *ep = to_exynos_pcie(pci);
177
178 exynos_pcie_sideband_dbi_w_mode(ep, true);
179 dw_pcie_write(base + reg, size, val);
180 exynos_pcie_sideband_dbi_w_mode(ep, false);
181}
182
183static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
184 int where, int size, u32 *val)
185{
186 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
187
188 if (PCI_SLOT(devfn))
189 return PCIBIOS_DEVICE_NOT_FOUND;
190
191 *val = dw_pcie_read_dbi(pci, where, size);
192 return PCIBIOS_SUCCESSFUL;
193}
194
195static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
196 int where, int size, u32 val)
197{
198 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
199
200 if (PCI_SLOT(devfn))
201 return PCIBIOS_DEVICE_NOT_FOUND;
202
203 dw_pcie_write_dbi(pci, where, size, val);
204 return PCIBIOS_SUCCESSFUL;
205}
206
207static struct pci_ops exynos_pci_ops = {
208 .read = exynos_pcie_rd_own_conf,
209 .write = exynos_pcie_wr_own_conf,
210};
211
212static int exynos_pcie_link_up(struct dw_pcie *pci)
213{
214 struct exynos_pcie *ep = to_exynos_pcie(pci);
215 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_RDLH_LINKUP);
216
217 return (val & PCIE_ELBI_XMLH_LINKUP);
218}
219
220static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
221{
222 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
223 struct exynos_pcie *ep = to_exynos_pcie(pci);
224
225 pp->bridge->ops = &exynos_pci_ops;
226
227 exynos_pcie_assert_core_reset(ep);
228
229 phy_init(ep->phy);
230 phy_power_on(ep->phy);
231
232 exynos_pcie_deassert_core_reset(ep);
233 exynos_pcie_enable_irq_pulse(ep);
234
235 return 0;
236}
237
238static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
239 .init = exynos_pcie_host_init,
240};
241
242static int exynos_add_pcie_port(struct exynos_pcie *ep,
243 struct platform_device *pdev)
244{
245 struct dw_pcie *pci = &ep->pci;
246 struct dw_pcie_rp *pp = &pci->pp;
247 struct device *dev = &pdev->dev;
248 int ret;
249
250 pp->irq = platform_get_irq(pdev, 0);
251 if (pp->irq < 0)
252 return pp->irq;
253
254 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
255 IRQF_SHARED, "exynos-pcie", ep);
256 if (ret) {
257 dev_err(dev, "failed to request irq\n");
258 return ret;
259 }
260
261 pp->ops = &exynos_pcie_host_ops;
262 pp->msi_irq[0] = -ENODEV;
263
264 ret = dw_pcie_host_init(pp);
265 if (ret) {
266 dev_err(dev, "failed to initialize host\n");
267 return ret;
268 }
269
270 return 0;
271}
272
273static const struct dw_pcie_ops dw_pcie_ops = {
274 .read_dbi = exynos_pcie_read_dbi,
275 .write_dbi = exynos_pcie_write_dbi,
276 .link_up = exynos_pcie_link_up,
277 .start_link = exynos_pcie_start_link,
278};
279
280static int exynos_pcie_probe(struct platform_device *pdev)
281{
282 struct device *dev = &pdev->dev;
283 struct exynos_pcie *ep;
284 struct device_node *np = dev->of_node;
285 int ret;
286
287 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
288 if (!ep)
289 return -ENOMEM;
290
291 ep->pci.dev = dev;
292 ep->pci.ops = &dw_pcie_ops;
293
294 ep->phy = devm_of_phy_get(dev, np, NULL);
295 if (IS_ERR(ep->phy))
296 return PTR_ERR(ep->phy);
297
298 /* External Local Bus interface (ELBI) registers */
299 ep->elbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
300 if (IS_ERR(ep->elbi_base))
301 return PTR_ERR(ep->elbi_base);
302
303 ret = devm_clk_bulk_get_all_enable(dev, &ep->clks);
304 if (ret < 0)
305 return ret;
306
307 ep->supplies[0].supply = "vdd18";
308 ep->supplies[1].supply = "vdd10";
309 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
310 ep->supplies);
311 if (ret)
312 return ret;
313
314 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
315 if (ret)
316 return ret;
317
318 platform_set_drvdata(pdev, ep);
319
320 ret = exynos_add_pcie_port(ep, pdev);
321 if (ret < 0)
322 goto fail_probe;
323
324 return 0;
325
326fail_probe:
327 phy_exit(ep->phy);
328 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
329
330 return ret;
331}
332
333static void exynos_pcie_remove(struct platform_device *pdev)
334{
335 struct exynos_pcie *ep = platform_get_drvdata(pdev);
336
337 dw_pcie_host_deinit(&ep->pci.pp);
338 exynos_pcie_assert_core_reset(ep);
339 phy_power_off(ep->phy);
340 phy_exit(ep->phy);
341 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
342}
343
344static int exynos_pcie_suspend_noirq(struct device *dev)
345{
346 struct exynos_pcie *ep = dev_get_drvdata(dev);
347
348 exynos_pcie_assert_core_reset(ep);
349 phy_power_off(ep->phy);
350 phy_exit(ep->phy);
351 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
352
353 return 0;
354}
355
356static int exynos_pcie_resume_noirq(struct device *dev)
357{
358 struct exynos_pcie *ep = dev_get_drvdata(dev);
359 struct dw_pcie *pci = &ep->pci;
360 struct dw_pcie_rp *pp = &pci->pp;
361 int ret;
362
363 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
364 if (ret)
365 return ret;
366
367 /* exynos_pcie_host_init controls ep->phy */
368 exynos_pcie_host_init(pp);
369 dw_pcie_setup_rc(pp);
370 exynos_pcie_start_link(pci);
371 return dw_pcie_wait_for_link(pci);
372}
373
374static const struct dev_pm_ops exynos_pcie_pm_ops = {
375 NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
376 exynos_pcie_resume_noirq)
377};
378
379static const struct of_device_id exynos_pcie_of_match[] = {
380 { .compatible = "samsung,exynos5433-pcie", },
381 { },
382};
383
384static struct platform_driver exynos_pcie_driver = {
385 .probe = exynos_pcie_probe,
386 .remove = exynos_pcie_remove,
387 .driver = {
388 .name = "exynos-pcie",
389 .of_match_table = exynos_pcie_of_match,
390 .pm = &exynos_pcie_pm_ops,
391 },
392};
393module_platform_driver(exynos_pcie_driver);
394MODULE_DESCRIPTION("Samsung Exynos PCIe host controller driver");
395MODULE_LICENSE("GPL v2");
396MODULE_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-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/of_device.h>
18#include <linux/pci.h>
19#include <linux/platform_device.h>
20#include <linux/phy/phy.h>
21#include <linux/regulator/consumer.h>
22
23#include "pcie-designware.h"
24
25#define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
26
27/* PCIe ELBI registers */
28#define PCIE_IRQ_PULSE 0x000
29#define IRQ_INTA_ASSERT BIT(0)
30#define IRQ_INTB_ASSERT BIT(2)
31#define IRQ_INTC_ASSERT BIT(4)
32#define IRQ_INTD_ASSERT BIT(6)
33#define PCIE_IRQ_LEVEL 0x004
34#define PCIE_IRQ_SPECIAL 0x008
35#define PCIE_IRQ_EN_PULSE 0x00c
36#define PCIE_IRQ_EN_LEVEL 0x010
37#define PCIE_IRQ_EN_SPECIAL 0x014
38#define PCIE_SW_WAKE 0x018
39#define PCIE_BUS_EN BIT(1)
40#define PCIE_CORE_RESET 0x01c
41#define PCIE_CORE_RESET_ENABLE BIT(0)
42#define PCIE_STICKY_RESET 0x020
43#define PCIE_NONSTICKY_RESET 0x024
44#define PCIE_APP_INIT_RESET 0x028
45#define PCIE_APP_LTSSM_ENABLE 0x02c
46#define PCIE_ELBI_RDLH_LINKUP 0x074
47#define PCIE_ELBI_XMLH_LINKUP BIT(4)
48#define PCIE_ELBI_LTSSM_ENABLE 0x1
49#define PCIE_ELBI_SLV_AWMISC 0x11c
50#define PCIE_ELBI_SLV_ARMISC 0x120
51#define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
52
53struct exynos_pcie {
54 struct dw_pcie pci;
55 void __iomem *elbi_base;
56 struct clk *clk;
57 struct clk *bus_clk;
58 struct phy *phy;
59 struct regulator_bulk_data supplies[2];
60};
61
62static int exynos_pcie_init_clk_resources(struct exynos_pcie *ep)
63{
64 struct device *dev = ep->pci.dev;
65 int ret;
66
67 ret = clk_prepare_enable(ep->clk);
68 if (ret) {
69 dev_err(dev, "cannot enable pcie rc clock");
70 return ret;
71 }
72
73 ret = clk_prepare_enable(ep->bus_clk);
74 if (ret) {
75 dev_err(dev, "cannot enable pcie bus clock");
76 goto err_bus_clk;
77 }
78
79 return 0;
80
81err_bus_clk:
82 clk_disable_unprepare(ep->clk);
83
84 return ret;
85}
86
87static void exynos_pcie_deinit_clk_resources(struct exynos_pcie *ep)
88{
89 clk_disable_unprepare(ep->bus_clk);
90 clk_disable_unprepare(ep->clk);
91}
92
93static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
94{
95 writel(val, base + reg);
96}
97
98static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
99{
100 return readl(base + reg);
101}
102
103static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
104{
105 u32 val;
106
107 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_AWMISC);
108 if (on)
109 val |= PCIE_ELBI_SLV_DBI_ENABLE;
110 else
111 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
112 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
113}
114
115static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
116{
117 u32 val;
118
119 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_SLV_ARMISC);
120 if (on)
121 val |= PCIE_ELBI_SLV_DBI_ENABLE;
122 else
123 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
124 exynos_pcie_writel(ep->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
125}
126
127static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
128{
129 u32 val;
130
131 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
132 val &= ~PCIE_CORE_RESET_ENABLE;
133 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
134 exynos_pcie_writel(ep->elbi_base, 0, PCIE_STICKY_RESET);
135 exynos_pcie_writel(ep->elbi_base, 0, PCIE_NONSTICKY_RESET);
136}
137
138static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
139{
140 u32 val;
141
142 val = exynos_pcie_readl(ep->elbi_base, PCIE_CORE_RESET);
143 val |= PCIE_CORE_RESET_ENABLE;
144
145 exynos_pcie_writel(ep->elbi_base, val, PCIE_CORE_RESET);
146 exynos_pcie_writel(ep->elbi_base, 1, PCIE_STICKY_RESET);
147 exynos_pcie_writel(ep->elbi_base, 1, PCIE_NONSTICKY_RESET);
148 exynos_pcie_writel(ep->elbi_base, 1, PCIE_APP_INIT_RESET);
149 exynos_pcie_writel(ep->elbi_base, 0, PCIE_APP_INIT_RESET);
150}
151
152static int exynos_pcie_start_link(struct dw_pcie *pci)
153{
154 struct exynos_pcie *ep = to_exynos_pcie(pci);
155 u32 val;
156
157 val = exynos_pcie_readl(ep->elbi_base, PCIE_SW_WAKE);
158 val &= ~PCIE_BUS_EN;
159 exynos_pcie_writel(ep->elbi_base, val, PCIE_SW_WAKE);
160
161 /* assert LTSSM enable */
162 exynos_pcie_writel(ep->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
163 PCIE_APP_LTSSM_ENABLE);
164 return 0;
165}
166
167static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
168{
169 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_IRQ_PULSE);
170
171 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_PULSE);
172}
173
174static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
175{
176 struct exynos_pcie *ep = arg;
177
178 exynos_pcie_clear_irq_pulse(ep);
179 return IRQ_HANDLED;
180}
181
182static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
183{
184 u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
185 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
186
187 exynos_pcie_writel(ep->elbi_base, val, PCIE_IRQ_EN_PULSE);
188 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
189 exynos_pcie_writel(ep->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
190}
191
192static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
193 u32 reg, size_t size)
194{
195 struct exynos_pcie *ep = to_exynos_pcie(pci);
196 u32 val;
197
198 exynos_pcie_sideband_dbi_r_mode(ep, true);
199 dw_pcie_read(base + reg, size, &val);
200 exynos_pcie_sideband_dbi_r_mode(ep, false);
201 return val;
202}
203
204static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
205 u32 reg, size_t size, u32 val)
206{
207 struct exynos_pcie *ep = to_exynos_pcie(pci);
208
209 exynos_pcie_sideband_dbi_w_mode(ep, true);
210 dw_pcie_write(base + reg, size, val);
211 exynos_pcie_sideband_dbi_w_mode(ep, false);
212}
213
214static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
215 int where, int size, u32 *val)
216{
217 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
218
219 if (PCI_SLOT(devfn)) {
220 *val = ~0;
221 return PCIBIOS_DEVICE_NOT_FOUND;
222 }
223
224 *val = dw_pcie_read_dbi(pci, where, size);
225 return PCIBIOS_SUCCESSFUL;
226}
227
228static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
229 int where, int size, u32 val)
230{
231 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
232
233 if (PCI_SLOT(devfn))
234 return PCIBIOS_DEVICE_NOT_FOUND;
235
236 dw_pcie_write_dbi(pci, where, size, val);
237 return PCIBIOS_SUCCESSFUL;
238}
239
240static struct pci_ops exynos_pci_ops = {
241 .read = exynos_pcie_rd_own_conf,
242 .write = exynos_pcie_wr_own_conf,
243};
244
245static int exynos_pcie_link_up(struct dw_pcie *pci)
246{
247 struct exynos_pcie *ep = to_exynos_pcie(pci);
248 u32 val = exynos_pcie_readl(ep->elbi_base, PCIE_ELBI_RDLH_LINKUP);
249
250 return (val & PCIE_ELBI_XMLH_LINKUP);
251}
252
253static int exynos_pcie_host_init(struct pcie_port *pp)
254{
255 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
256 struct exynos_pcie *ep = to_exynos_pcie(pci);
257
258 pp->bridge->ops = &exynos_pci_ops;
259
260 exynos_pcie_assert_core_reset(ep);
261
262 phy_reset(ep->phy);
263 phy_power_on(ep->phy);
264 phy_init(ep->phy);
265
266 exynos_pcie_deassert_core_reset(ep);
267 exynos_pcie_enable_irq_pulse(ep);
268
269 return 0;
270}
271
272static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
273 .host_init = exynos_pcie_host_init,
274};
275
276static int exynos_add_pcie_port(struct exynos_pcie *ep,
277 struct platform_device *pdev)
278{
279 struct dw_pcie *pci = &ep->pci;
280 struct pcie_port *pp = &pci->pp;
281 struct device *dev = &pdev->dev;
282 int ret;
283
284 pp->irq = platform_get_irq(pdev, 0);
285 if (pp->irq < 0)
286 return pp->irq;
287
288 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
289 IRQF_SHARED, "exynos-pcie", ep);
290 if (ret) {
291 dev_err(dev, "failed to request irq\n");
292 return ret;
293 }
294
295 pp->ops = &exynos_pcie_host_ops;
296 pp->msi_irq = -ENODEV;
297
298 ret = dw_pcie_host_init(pp);
299 if (ret) {
300 dev_err(dev, "failed to initialize host\n");
301 return ret;
302 }
303
304 return 0;
305}
306
307static const struct dw_pcie_ops dw_pcie_ops = {
308 .read_dbi = exynos_pcie_read_dbi,
309 .write_dbi = exynos_pcie_write_dbi,
310 .link_up = exynos_pcie_link_up,
311 .start_link = exynos_pcie_start_link,
312};
313
314static int exynos_pcie_probe(struct platform_device *pdev)
315{
316 struct device *dev = &pdev->dev;
317 struct exynos_pcie *ep;
318 struct device_node *np = dev->of_node;
319 int ret;
320
321 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
322 if (!ep)
323 return -ENOMEM;
324
325 ep->pci.dev = dev;
326 ep->pci.ops = &dw_pcie_ops;
327
328 ep->phy = devm_of_phy_get(dev, np, NULL);
329 if (IS_ERR(ep->phy))
330 return PTR_ERR(ep->phy);
331
332 /* External Local Bus interface (ELBI) registers */
333 ep->elbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
334 if (IS_ERR(ep->elbi_base))
335 return PTR_ERR(ep->elbi_base);
336
337 ep->clk = devm_clk_get(dev, "pcie");
338 if (IS_ERR(ep->clk)) {
339 dev_err(dev, "Failed to get pcie rc clock\n");
340 return PTR_ERR(ep->clk);
341 }
342
343 ep->bus_clk = devm_clk_get(dev, "pcie_bus");
344 if (IS_ERR(ep->bus_clk)) {
345 dev_err(dev, "Failed to get pcie bus clock\n");
346 return PTR_ERR(ep->bus_clk);
347 }
348
349 ep->supplies[0].supply = "vdd18";
350 ep->supplies[1].supply = "vdd10";
351 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ep->supplies),
352 ep->supplies);
353 if (ret)
354 return ret;
355
356 ret = exynos_pcie_init_clk_resources(ep);
357 if (ret)
358 return ret;
359
360 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
361 if (ret)
362 return ret;
363
364 platform_set_drvdata(pdev, ep);
365
366 ret = exynos_add_pcie_port(ep, pdev);
367 if (ret < 0)
368 goto fail_probe;
369
370 return 0;
371
372fail_probe:
373 phy_exit(ep->phy);
374 exynos_pcie_deinit_clk_resources(ep);
375 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
376
377 return ret;
378}
379
380static int __exit exynos_pcie_remove(struct platform_device *pdev)
381{
382 struct exynos_pcie *ep = platform_get_drvdata(pdev);
383
384 dw_pcie_host_deinit(&ep->pci.pp);
385 exynos_pcie_assert_core_reset(ep);
386 phy_power_off(ep->phy);
387 phy_exit(ep->phy);
388 exynos_pcie_deinit_clk_resources(ep);
389 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
390
391 return 0;
392}
393
394static int __maybe_unused exynos_pcie_suspend_noirq(struct device *dev)
395{
396 struct exynos_pcie *ep = dev_get_drvdata(dev);
397
398 exynos_pcie_assert_core_reset(ep);
399 phy_power_off(ep->phy);
400 phy_exit(ep->phy);
401 regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
402
403 return 0;
404}
405
406static int __maybe_unused exynos_pcie_resume_noirq(struct device *dev)
407{
408 struct exynos_pcie *ep = dev_get_drvdata(dev);
409 struct dw_pcie *pci = &ep->pci;
410 struct pcie_port *pp = &pci->pp;
411 int ret;
412
413 ret = regulator_bulk_enable(ARRAY_SIZE(ep->supplies), ep->supplies);
414 if (ret)
415 return ret;
416
417 /* exynos_pcie_host_init controls ep->phy */
418 exynos_pcie_host_init(pp);
419 dw_pcie_setup_rc(pp);
420 exynos_pcie_start_link(pci);
421 return dw_pcie_wait_for_link(pci);
422}
423
424static const struct dev_pm_ops exynos_pcie_pm_ops = {
425 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(exynos_pcie_suspend_noirq,
426 exynos_pcie_resume_noirq)
427};
428
429static const struct of_device_id exynos_pcie_of_match[] = {
430 { .compatible = "samsung,exynos5433-pcie", },
431 { },
432};
433
434static struct platform_driver exynos_pcie_driver = {
435 .probe = exynos_pcie_probe,
436 .remove = __exit_p(exynos_pcie_remove),
437 .driver = {
438 .name = "exynos-pcie",
439 .of_match_table = exynos_pcie_of_match,
440 .pm = &exynos_pcie_pm_ops,
441 },
442};
443module_platform_driver(exynos_pcie_driver);
444MODULE_LICENSE("GPL v2");
445MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);