Linux Audio

Check our new training course

Loading...
v6.13.7
  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);
 
 
 
 
v5.4
  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 *		http://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	struct resource *res;
 88
 89	ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
 90	if (!ep->mem_res)
 91		return -ENOMEM;
 92
 93	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 94	ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
 95	if (IS_ERR(ep->mem_res->elbi_base))
 96		return PTR_ERR(ep->mem_res->elbi_base);
 97
 98	return 0;
 99}
100
101static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
102{
103	struct dw_pcie *pci = ep->pci;
104	struct device *dev = pci->dev;
105
106	ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
107	if (!ep->clk_res)
108		return -ENOMEM;
109
110	ep->clk_res->clk = devm_clk_get(dev, "pcie");
111	if (IS_ERR(ep->clk_res->clk)) {
112		dev_err(dev, "Failed to get pcie rc clock\n");
113		return PTR_ERR(ep->clk_res->clk);
114	}
115
116	ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
117	if (IS_ERR(ep->clk_res->bus_clk)) {
118		dev_err(dev, "Failed to get pcie bus clock\n");
119		return PTR_ERR(ep->clk_res->bus_clk);
120	}
121
122	return 0;
123}
124
125static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
126{
127	struct dw_pcie *pci = ep->pci;
128	struct device *dev = pci->dev;
129	int ret;
130
131	ret = clk_prepare_enable(ep->clk_res->clk);
132	if (ret) {
133		dev_err(dev, "cannot enable pcie rc clock");
134		return ret;
135	}
136
137	ret = clk_prepare_enable(ep->clk_res->bus_clk);
138	if (ret) {
139		dev_err(dev, "cannot enable pcie bus clock");
140		goto err_bus_clk;
141	}
142
143	return 0;
144
145err_bus_clk:
146	clk_disable_unprepare(ep->clk_res->clk);
147
148	return ret;
149}
150
151static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
152{
153	clk_disable_unprepare(ep->clk_res->bus_clk);
154	clk_disable_unprepare(ep->clk_res->clk);
155}
156
157static const struct exynos_pcie_ops exynos5440_pcie_ops = {
158	.get_mem_resources	= exynos5440_pcie_get_mem_resources,
159	.get_clk_resources	= exynos5440_pcie_get_clk_resources,
160	.init_clk_resources	= exynos5440_pcie_init_clk_resources,
161	.deinit_clk_resources	= exynos5440_pcie_deinit_clk_resources,
162};
163
164static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
165{
166	writel(val, base + reg);
167}
168
169static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
170{
171	return readl(base + reg);
172}
173
174static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
175{
176	u32 val;
177
178	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
179	if (on)
180		val |= PCIE_ELBI_SLV_DBI_ENABLE;
181	else
182		val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
183	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
184}
185
186static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
187{
188	u32 val;
189
190	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
191	if (on)
192		val |= PCIE_ELBI_SLV_DBI_ENABLE;
193	else
194		val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
195	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
196}
197
198static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
199{
200	u32 val;
201
202	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
203	val &= ~PCIE_CORE_RESET_ENABLE;
204	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
205	exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
206	exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
207	exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
208}
209
210static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
211{
212	u32 val;
213
214	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
215	val |= PCIE_CORE_RESET_ENABLE;
216
217	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
218	exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
219	exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
220	exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
221	exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
222}
223
224static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
225{
226	struct dw_pcie *pci = ep->pci;
227	struct device *dev = pci->dev;
228
229	if (ep->reset_gpio >= 0)
230		devm_gpio_request_one(dev, ep->reset_gpio,
231				GPIOF_OUT_INIT_HIGH, "RESET");
232}
233
234static int exynos_pcie_establish_link(struct exynos_pcie *ep)
235{
236	struct dw_pcie *pci = ep->pci;
237	struct pcie_port *pp = &pci->pp;
238	struct device *dev = pci->dev;
239
240	if (dw_pcie_link_up(pci)) {
241		dev_err(dev, "Link already up\n");
242		return 0;
243	}
244
245	exynos_pcie_assert_core_reset(ep);
246
247	phy_reset(ep->phy);
248
249	exynos_pcie_writel(ep->mem_res->elbi_base, 1,
250			PCIE_PWR_RESET);
251
252	phy_power_on(ep->phy);
253	phy_init(ep->phy);
254
255	exynos_pcie_deassert_core_reset(ep);
256	dw_pcie_setup_rc(pp);
257	exynos_pcie_assert_reset(ep);
258
259	/* assert LTSSM enable */
260	exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
261			  PCIE_APP_LTSSM_ENABLE);
262
263	/* check if the link is up or not */
264	if (!dw_pcie_wait_for_link(pci))
265		return 0;
266
267	phy_power_off(ep->phy);
268	return -ETIMEDOUT;
269}
270
271static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
272{
273	u32 val;
274
275	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
276	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
277}
278
279static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
280{
281	u32 val;
282
283	/* enable INTX interrupt */
284	val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
285		IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
286	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
287}
288
289static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
290{
291	struct exynos_pcie *ep = arg;
292
293	exynos_pcie_clear_irq_pulse(ep);
294	return IRQ_HANDLED;
295}
296
297static void exynos_pcie_msi_init(struct exynos_pcie *ep)
298{
299	struct dw_pcie *pci = ep->pci;
300	struct pcie_port *pp = &pci->pp;
301	u32 val;
302
303	dw_pcie_msi_init(pp);
304
305	/* enable MSI interrupt */
306	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
307	val |= IRQ_MSI_ENABLE;
308	exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
309}
310
311static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
312{
313	exynos_pcie_enable_irq_pulse(ep);
314
315	if (IS_ENABLED(CONFIG_PCI_MSI))
316		exynos_pcie_msi_init(ep);
 
317}
318
319static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
320				u32 reg, size_t size)
321{
322	struct exynos_pcie *ep = to_exynos_pcie(pci);
323	u32 val;
324
325	exynos_pcie_sideband_dbi_r_mode(ep, true);
326	dw_pcie_read(base + reg, size, &val);
327	exynos_pcie_sideband_dbi_r_mode(ep, false);
328	return val;
329}
330
331static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
332				  u32 reg, size_t size, u32 val)
333{
334	struct exynos_pcie *ep = to_exynos_pcie(pci);
335
336	exynos_pcie_sideband_dbi_w_mode(ep, true);
337	dw_pcie_write(base + reg, size, val);
338	exynos_pcie_sideband_dbi_w_mode(ep, false);
339}
340
341static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
342				u32 *val)
343{
344	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
345	struct exynos_pcie *ep = to_exynos_pcie(pci);
346	int ret;
 
347
348	exynos_pcie_sideband_dbi_r_mode(ep, true);
349	ret = dw_pcie_read(pci->dbi_base + where, size, val);
350	exynos_pcie_sideband_dbi_r_mode(ep, false);
351	return ret;
352}
353
354static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
355				u32 val)
356{
357	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
358	struct exynos_pcie *ep = to_exynos_pcie(pci);
359	int ret;
 
360
361	exynos_pcie_sideband_dbi_w_mode(ep, true);
362	ret = dw_pcie_write(pci->dbi_base + where, size, val);
363	exynos_pcie_sideband_dbi_w_mode(ep, false);
364	return ret;
365}
366
 
 
 
 
 
367static int exynos_pcie_link_up(struct dw_pcie *pci)
368{
369	struct exynos_pcie *ep = to_exynos_pcie(pci);
370	u32 val;
371
372	val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
373	if (val == PCIE_ELBI_LTSSM_ENABLE)
374		return 1;
375
376	return 0;
377}
378
379static int exynos_pcie_host_init(struct pcie_port *pp)
380{
381	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
382	struct exynos_pcie *ep = to_exynos_pcie(pci);
383
384	exynos_pcie_establish_link(ep);
385	exynos_pcie_enable_interrupts(ep);
 
 
 
 
 
 
 
386
387	return 0;
388}
389
390static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
391	.rd_own_conf = exynos_pcie_rd_own_conf,
392	.wr_own_conf = exynos_pcie_wr_own_conf,
393	.host_init = exynos_pcie_host_init,
394};
395
396static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
397				       struct platform_device *pdev)
398{
399	struct dw_pcie *pci = ep->pci;
400	struct pcie_port *pp = &pci->pp;
401	struct device *dev = &pdev->dev;
402	int ret;
403
404	pp->irq = platform_get_irq(pdev, 1);
405	if (pp->irq < 0) {
406		dev_err(dev, "failed to get irq\n");
407		return pp->irq;
408	}
409	ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
410				IRQF_SHARED, "exynos-pcie", ep);
411	if (ret) {
412		dev_err(dev, "failed to request irq\n");
413		return ret;
414	}
415
416	if (IS_ENABLED(CONFIG_PCI_MSI)) {
417		pp->msi_irq = platform_get_irq(pdev, 0);
418		if (pp->msi_irq < 0) {
419			dev_err(dev, "failed to get msi irq\n");
420			return pp->msi_irq;
421		}
422	}
423
424	pp->ops = &exynos_pcie_host_ops;
 
425
426	ret = dw_pcie_host_init(pp);
427	if (ret) {
428		dev_err(dev, "failed to initialize host\n");
429		return ret;
430	}
431
432	return 0;
433}
434
435static const struct dw_pcie_ops dw_pcie_ops = {
436	.read_dbi = exynos_pcie_read_dbi,
437	.write_dbi = exynos_pcie_write_dbi,
438	.link_up = exynos_pcie_link_up,
 
439};
440
441static int __init exynos_pcie_probe(struct platform_device *pdev)
442{
443	struct device *dev = &pdev->dev;
444	struct dw_pcie *pci;
445	struct exynos_pcie *ep;
446	struct device_node *np = dev->of_node;
447	int ret;
448
449	ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
450	if (!ep)
451		return -ENOMEM;
452
453	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
454	if (!pci)
455		return -ENOMEM;
456
457	pci->dev = dev;
458	pci->ops = &dw_pcie_ops;
 
459
460	ep->pci = pci;
461	ep->ops = (const struct exynos_pcie_ops *)
462		of_device_get_match_data(dev);
 
463
464	ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
 
 
465
466	ep->phy = devm_of_phy_get(dev, np, NULL);
467	if (IS_ERR(ep->phy)) {
468		if (PTR_ERR(ep->phy) != -ENODEV)
469			return PTR_ERR(ep->phy);
 
 
470
471		ep->phy = NULL;
472	}
473
474	if (ep->ops && ep->ops->get_mem_resources) {
475		ret = ep->ops->get_mem_resources(pdev, ep);
476		if (ret)
477			return ret;
478	}
479
480	if (ep->ops && ep->ops->get_clk_resources &&
481			ep->ops->init_clk_resources) {
482		ret = ep->ops->get_clk_resources(ep);
483		if (ret)
484			return ret;
485		ret = ep->ops->init_clk_resources(ep);
486		if (ret)
487			return ret;
488	}
489
490	platform_set_drvdata(pdev, ep);
491
492	ret = exynos_add_pcie_port(ep, pdev);
493	if (ret < 0)
494		goto fail_probe;
495
496	return 0;
497
498fail_probe:
499	phy_exit(ep->phy);
 
500
501	if (ep->ops && ep->ops->deinit_clk_resources)
502		ep->ops->deinit_clk_resources(ep);
503	return ret;
504}
505
506static int __exit exynos_pcie_remove(struct platform_device *pdev)
507{
508	struct exynos_pcie *ep = platform_get_drvdata(pdev);
509
510	if (ep->ops && ep->ops->deinit_clk_resources)
511		ep->ops->deinit_clk_resources(ep);
 
 
 
 
 
 
 
 
 
 
 
 
 
512
513	return 0;
514}
515
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516static const struct of_device_id exynos_pcie_of_match[] = {
517	{
518		.compatible = "samsung,exynos5440-pcie",
519		.data = &exynos5440_pcie_ops
520	},
521	{},
522};
523
524static struct platform_driver exynos_pcie_driver = {
525	.remove		= __exit_p(exynos_pcie_remove),
 
526	.driver = {
527		.name	= "exynos-pcie",
528		.of_match_table = exynos_pcie_of_match,
 
529	},
530};
531
532/* Exynos PCIe driver does not allow module unload */
533
534static int __init exynos_pcie_init(void)
535{
536	return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
537}
538subsys_initcall(exynos_pcie_init);