xref: /linux/drivers/pci/controller/dwc/pci-exynos.c (revision 2f2c7254931f41b5736e3ba12aaa9ac1bbeeeb92)
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 
54 struct exynos_pcie {
55 	struct dw_pcie			pci;
56 	struct clk_bulk_data		*clks;
57 	struct phy			*phy;
58 	struct regulator_bulk_data	supplies[2];
59 };
60 
exynos_pcie_writel(void __iomem * base,u32 val,u32 reg)61 static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
62 {
63 	writel(val, base + reg);
64 }
65 
exynos_pcie_readl(void __iomem * base,u32 reg)66 static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
67 {
68 	return readl(base + reg);
69 }
70 
exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie * ep,bool on)71 static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
72 {
73 	struct dw_pcie *pci = &ep->pci;
74 	u32 val;
75 
76 	val = exynos_pcie_readl(pci->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(pci->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
82 }
83 
exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie * ep,bool on)84 static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
85 {
86 	struct dw_pcie *pci = &ep->pci;
87 	u32 val;
88 
89 	val = exynos_pcie_readl(pci->elbi_base, PCIE_ELBI_SLV_ARMISC);
90 	if (on)
91 		val |= PCIE_ELBI_SLV_DBI_ENABLE;
92 	else
93 		val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
94 	exynos_pcie_writel(pci->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
95 }
96 
exynos_pcie_assert_core_reset(struct exynos_pcie * ep)97 static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
98 {
99 	struct dw_pcie *pci = &ep->pci;
100 	u32 val;
101 
102 	val = exynos_pcie_readl(pci->elbi_base, PCIE_CORE_RESET);
103 	val &= ~PCIE_CORE_RESET_ENABLE;
104 	exynos_pcie_writel(pci->elbi_base, val, PCIE_CORE_RESET);
105 	exynos_pcie_writel(pci->elbi_base, 0, PCIE_STICKY_RESET);
106 	exynos_pcie_writel(pci->elbi_base, 0, PCIE_NONSTICKY_RESET);
107 }
108 
exynos_pcie_deassert_core_reset(struct exynos_pcie * ep)109 static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
110 {
111 	struct dw_pcie *pci = &ep->pci;
112 	u32 val;
113 
114 	val = exynos_pcie_readl(pci->elbi_base, PCIE_CORE_RESET);
115 	val |= PCIE_CORE_RESET_ENABLE;
116 
117 	exynos_pcie_writel(pci->elbi_base, val, PCIE_CORE_RESET);
118 	exynos_pcie_writel(pci->elbi_base, 1, PCIE_STICKY_RESET);
119 	exynos_pcie_writel(pci->elbi_base, 1, PCIE_NONSTICKY_RESET);
120 	exynos_pcie_writel(pci->elbi_base, 1, PCIE_APP_INIT_RESET);
121 	exynos_pcie_writel(pci->elbi_base, 0, PCIE_APP_INIT_RESET);
122 }
123 
exynos_pcie_start_link(struct dw_pcie * pci)124 static int exynos_pcie_start_link(struct dw_pcie *pci)
125 {
126 	u32 val;
127 
128 	val = exynos_pcie_readl(pci->elbi_base, PCIE_SW_WAKE);
129 	val &= ~PCIE_BUS_EN;
130 	exynos_pcie_writel(pci->elbi_base, val, PCIE_SW_WAKE);
131 
132 	/* assert LTSSM enable */
133 	exynos_pcie_writel(pci->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
134 			  PCIE_APP_LTSSM_ENABLE);
135 	return 0;
136 }
137 
exynos_pcie_clear_irq_pulse(struct exynos_pcie * ep)138 static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
139 {
140 	struct dw_pcie *pci = &ep->pci;
141 
142 	u32 val = exynos_pcie_readl(pci->elbi_base, PCIE_IRQ_PULSE);
143 
144 	exynos_pcie_writel(pci->elbi_base, val, PCIE_IRQ_PULSE);
145 }
146 
exynos_pcie_irq_handler(int irq,void * arg)147 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
148 {
149 	struct exynos_pcie *ep = arg;
150 
151 	exynos_pcie_clear_irq_pulse(ep);
152 	return IRQ_HANDLED;
153 }
154 
exynos_pcie_enable_irq_pulse(struct exynos_pcie * ep)155 static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
156 {
157 	struct dw_pcie *pci = &ep->pci;
158 
159 	u32 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
160 		  IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
161 
162 	exynos_pcie_writel(pci->elbi_base, val, PCIE_IRQ_EN_PULSE);
163 	exynos_pcie_writel(pci->elbi_base, 0, PCIE_IRQ_EN_LEVEL);
164 	exynos_pcie_writel(pci->elbi_base, 0, PCIE_IRQ_EN_SPECIAL);
165 }
166 
exynos_pcie_read_dbi(struct dw_pcie * pci,void __iomem * base,u32 reg,size_t size)167 static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
168 				u32 reg, size_t size)
169 {
170 	struct exynos_pcie *ep = to_exynos_pcie(pci);
171 	u32 val;
172 
173 	exynos_pcie_sideband_dbi_r_mode(ep, true);
174 	dw_pcie_read(base + reg, size, &val);
175 	exynos_pcie_sideband_dbi_r_mode(ep, false);
176 	return val;
177 }
178 
exynos_pcie_write_dbi(struct dw_pcie * pci,void __iomem * base,u32 reg,size_t size,u32 val)179 static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
180 				  u32 reg, size_t size, u32 val)
181 {
182 	struct exynos_pcie *ep = to_exynos_pcie(pci);
183 
184 	exynos_pcie_sideband_dbi_w_mode(ep, true);
185 	dw_pcie_write(base + reg, size, val);
186 	exynos_pcie_sideband_dbi_w_mode(ep, false);
187 }
188 
exynos_pcie_rd_own_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)189 static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
190 				   int where, int size, u32 *val)
191 {
192 	struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
193 
194 	if (PCI_SLOT(devfn))
195 		return PCIBIOS_DEVICE_NOT_FOUND;
196 
197 	*val = dw_pcie_read_dbi(pci, where, size);
198 	return PCIBIOS_SUCCESSFUL;
199 }
200 
exynos_pcie_wr_own_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)201 static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
202 				   int where, int size, u32 val)
203 {
204 	struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
205 
206 	if (PCI_SLOT(devfn))
207 		return PCIBIOS_DEVICE_NOT_FOUND;
208 
209 	dw_pcie_write_dbi(pci, where, size, val);
210 	return PCIBIOS_SUCCESSFUL;
211 }
212 
213 static struct pci_ops exynos_pci_ops = {
214 	.read = exynos_pcie_rd_own_conf,
215 	.write = exynos_pcie_wr_own_conf,
216 };
217 
exynos_pcie_link_up(struct dw_pcie * pci)218 static bool exynos_pcie_link_up(struct dw_pcie *pci)
219 {
220 	u32 val = exynos_pcie_readl(pci->elbi_base, PCIE_ELBI_RDLH_LINKUP);
221 
222 	return val & PCIE_ELBI_XMLH_LINKUP;
223 }
224 
exynos_pcie_host_init(struct dw_pcie_rp * pp)225 static int exynos_pcie_host_init(struct dw_pcie_rp *pp)
226 {
227 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
228 	struct exynos_pcie *ep = to_exynos_pcie(pci);
229 
230 	pp->bridge->ops = &exynos_pci_ops;
231 
232 	exynos_pcie_assert_core_reset(ep);
233 
234 	phy_init(ep->phy);
235 	phy_power_on(ep->phy);
236 
237 	exynos_pcie_deassert_core_reset(ep);
238 	exynos_pcie_enable_irq_pulse(ep);
239 
240 	return 0;
241 }
242 
243 static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
244 	.init = exynos_pcie_host_init,
245 };
246 
exynos_add_pcie_port(struct exynos_pcie * ep,struct platform_device * pdev)247 static int exynos_add_pcie_port(struct exynos_pcie *ep,
248 				       struct platform_device *pdev)
249 {
250 	struct dw_pcie *pci = &ep->pci;
251 	struct dw_pcie_rp *pp = &pci->pp;
252 	struct device *dev = &pdev->dev;
253 	int ret;
254 
255 	pp->irq = platform_get_irq(pdev, 0);
256 	if (pp->irq < 0)
257 		return pp->irq;
258 
259 	ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
260 			       IRQF_SHARED, "exynos-pcie", ep);
261 	if (ret) {
262 		dev_err(dev, "failed to request irq\n");
263 		return ret;
264 	}
265 
266 	pp->ops = &exynos_pcie_host_ops;
267 	pp->msi_irq[0] = -ENODEV;
268 
269 	ret = dw_pcie_host_init(pp);
270 	if (ret) {
271 		dev_err(dev, "failed to initialize host\n");
272 		return ret;
273 	}
274 
275 	return 0;
276 }
277 
278 static const struct dw_pcie_ops dw_pcie_ops = {
279 	.read_dbi = exynos_pcie_read_dbi,
280 	.write_dbi = exynos_pcie_write_dbi,
281 	.link_up = exynos_pcie_link_up,
282 	.start_link = exynos_pcie_start_link,
283 };
284 
exynos_pcie_probe(struct platform_device * pdev)285 static int exynos_pcie_probe(struct platform_device *pdev)
286 {
287 	struct device *dev = &pdev->dev;
288 	struct exynos_pcie *ep;
289 	struct device_node *np = dev->of_node;
290 	int ret;
291 
292 	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
293 	if (!ep)
294 		return -ENOMEM;
295 
296 	ep->pci.dev = dev;
297 	ep->pci.ops = &dw_pcie_ops;
298 
299 	ep->phy = devm_of_phy_get(dev, np, NULL);
300 	if (IS_ERR(ep->phy))
301 		return PTR_ERR(ep->phy);
302 
303 	ret = devm_clk_bulk_get_all_enabled(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 
326 fail_probe:
327 	phy_exit(ep->phy);
328 	regulator_bulk_disable(ARRAY_SIZE(ep->supplies), ep->supplies);
329 
330 	return ret;
331 }
332 
exynos_pcie_remove(struct platform_device * pdev)333 static 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 
exynos_pcie_suspend_noirq(struct device * dev)344 static 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 
exynos_pcie_resume_noirq(struct device * dev)356 static 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 
374 static 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 
379 static const struct of_device_id exynos_pcie_of_match[] = {
380 	{ .compatible = "samsung,exynos5433-pcie", },
381 	{ },
382 };
383 
384 static 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 };
393 module_platform_driver(exynos_pcie_driver);
394 MODULE_DESCRIPTION("Samsung Exynos PCIe host controller driver");
395 MODULE_LICENSE("GPL v2");
396 MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
397