Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * PCIe host controller driver for HiSilicon STB SoCs
  4 *
  5 * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
  6 *
  7 * Authors: Ruqiang Ju <juruqiang@hisilicon.com>
  8 *          Jianguo Sun <sunjianguo1@huawei.com>
  9 */
 10
 11#include <linux/clk.h>
 12#include <linux/delay.h>
 13#include <linux/interrupt.h>
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/of.h>
 17#include <linux/of_gpio.h>
 18#include <linux/pci.h>
 19#include <linux/phy/phy.h>
 20#include <linux/platform_device.h>
 21#include <linux/resource.h>
 22#include <linux/reset.h>
 23
 24#include "pcie-designware.h"
 25
 26#define to_histb_pcie(x)	dev_get_drvdata((x)->dev)
 27
 28#define PCIE_SYS_CTRL0			0x0000
 29#define PCIE_SYS_CTRL1			0x0004
 30#define PCIE_SYS_CTRL7			0x001C
 31#define PCIE_SYS_CTRL13			0x0034
 32#define PCIE_SYS_CTRL15			0x003C
 33#define PCIE_SYS_CTRL16			0x0040
 34#define PCIE_SYS_CTRL17			0x0044
 35
 36#define PCIE_SYS_STAT0			0x0100
 37#define PCIE_SYS_STAT4			0x0110
 38
 39#define PCIE_RDLH_LINK_UP		BIT(5)
 40#define PCIE_XMLH_LINK_UP		BIT(15)
 41#define PCIE_ELBI_SLV_DBI_ENABLE	BIT(21)
 42#define PCIE_APP_LTSSM_ENABLE		BIT(11)
 43
 44#define PCIE_DEVICE_TYPE_MASK		GENMASK(31, 28)
 45#define PCIE_WM_EP			0
 46#define PCIE_WM_LEGACY			BIT(1)
 47#define PCIE_WM_RC			BIT(30)
 48
 49#define PCIE_LTSSM_STATE_MASK		GENMASK(5, 0)
 50#define PCIE_LTSSM_STATE_ACTIVE		0x11
 51
 52struct histb_pcie {
 53	struct dw_pcie *pci;
 54	struct clk *aux_clk;
 55	struct clk *pipe_clk;
 56	struct clk *sys_clk;
 57	struct clk *bus_clk;
 58	struct phy *phy;
 59	struct reset_control *soft_reset;
 60	struct reset_control *sys_reset;
 61	struct reset_control *bus_reset;
 62	void __iomem *ctrl;
 63	int reset_gpio;
 64	struct regulator *vpcie;
 65};
 66
 67static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
 68{
 69	return readl(histb_pcie->ctrl + reg);
 70}
 71
 72static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
 73{
 74	writel(val, histb_pcie->ctrl + reg);
 75}
 76
 77static void histb_pcie_dbi_w_mode(struct pcie_port *pp, bool enable)
 78{
 79	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 80	struct histb_pcie *hipcie = to_histb_pcie(pci);
 81	u32 val;
 82
 83	val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
 84	if (enable)
 85		val |= PCIE_ELBI_SLV_DBI_ENABLE;
 86	else
 87		val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
 88	histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, val);
 89}
 90
 91static void histb_pcie_dbi_r_mode(struct pcie_port *pp, bool enable)
 92{
 93	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
 94	struct histb_pcie *hipcie = to_histb_pcie(pci);
 95	u32 val;
 96
 97	val = histb_pcie_readl(hipcie, PCIE_SYS_CTRL1);
 98	if (enable)
 99		val |= PCIE_ELBI_SLV_DBI_ENABLE;
100	else
101		val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
102	histb_pcie_writel(hipcie, PCIE_SYS_CTRL1, val);
103}
104
105static u32 histb_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
106			       u32 reg, size_t size)
107{
108	u32 val;
109
110	histb_pcie_dbi_r_mode(&pci->pp, true);
111	dw_pcie_read(base + reg, size, &val);
112	histb_pcie_dbi_r_mode(&pci->pp, false);
113
114	return val;
115}
116
117static void histb_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
118				 u32 reg, size_t size, u32 val)
119{
120	histb_pcie_dbi_w_mode(&pci->pp, true);
121	dw_pcie_write(base + reg, size, val);
122	histb_pcie_dbi_w_mode(&pci->pp, false);
123}
124
125static int histb_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
126				  int where, int size, u32 *val)
127{
128	struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
129
130	if (PCI_SLOT(devfn)) {
131		*val = ~0;
132		return PCIBIOS_DEVICE_NOT_FOUND;
133	}
134
135	*val = dw_pcie_read_dbi(pci, where, size);
136	return PCIBIOS_SUCCESSFUL;
137}
138
139static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
140				  int where, int size, u32 val)
141{
142	struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
143
144	if (PCI_SLOT(devfn))
145		return PCIBIOS_DEVICE_NOT_FOUND;
146
147	dw_pcie_write_dbi(pci, where, size, val);
148	return PCIBIOS_SUCCESSFUL;
149}
150
151static struct pci_ops histb_pci_ops = {
152	.read = histb_pcie_rd_own_conf,
153	.write = histb_pcie_wr_own_conf,
154};
155
156static int histb_pcie_link_up(struct dw_pcie *pci)
157{
158	struct histb_pcie *hipcie = to_histb_pcie(pci);
159	u32 regval;
160	u32 status;
161
162	regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
163	status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
164	status &= PCIE_LTSSM_STATE_MASK;
165	if ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
166	    (status == PCIE_LTSSM_STATE_ACTIVE))
167		return 1;
168
169	return 0;
170}
171
172static int histb_pcie_start_link(struct dw_pcie *pci)
173{
174	struct histb_pcie *hipcie = to_histb_pcie(pci);
175	u32 regval;
176
177	/* assert LTSSM enable */
178	regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
179	regval |= PCIE_APP_LTSSM_ENABLE;
180	histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
181
182	return 0;
183}
184
185static int histb_pcie_host_init(struct pcie_port *pp)
186{
187	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
188	struct histb_pcie *hipcie = to_histb_pcie(pci);
189	u32 regval;
190
191	pp->bridge->ops = &histb_pci_ops;
192
193	/* PCIe RC work mode */
194	regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
195	regval &= ~PCIE_DEVICE_TYPE_MASK;
196	regval |= PCIE_WM_RC;
197	histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
198
199	return 0;
200}
201
202static const struct dw_pcie_host_ops histb_pcie_host_ops = {
203	.host_init = histb_pcie_host_init,
204};
205
206static void histb_pcie_host_disable(struct histb_pcie *hipcie)
207{
208	reset_control_assert(hipcie->soft_reset);
209	reset_control_assert(hipcie->sys_reset);
210	reset_control_assert(hipcie->bus_reset);
211
212	clk_disable_unprepare(hipcie->aux_clk);
213	clk_disable_unprepare(hipcie->pipe_clk);
214	clk_disable_unprepare(hipcie->sys_clk);
215	clk_disable_unprepare(hipcie->bus_clk);
216
217	if (gpio_is_valid(hipcie->reset_gpio))
218		gpio_set_value_cansleep(hipcie->reset_gpio, 0);
219
220	if (hipcie->vpcie)
221		regulator_disable(hipcie->vpcie);
222}
223
224static int histb_pcie_host_enable(struct pcie_port *pp)
225{
226	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
227	struct histb_pcie *hipcie = to_histb_pcie(pci);
228	struct device *dev = pci->dev;
229	int ret;
230
231	/* power on PCIe device if have */
232	if (hipcie->vpcie) {
233		ret = regulator_enable(hipcie->vpcie);
234		if (ret) {
235			dev_err(dev, "failed to enable regulator: %d\n", ret);
236			return ret;
237		}
238	}
239
240	if (gpio_is_valid(hipcie->reset_gpio))
241		gpio_set_value_cansleep(hipcie->reset_gpio, 1);
242
243	ret = clk_prepare_enable(hipcie->bus_clk);
244	if (ret) {
245		dev_err(dev, "cannot prepare/enable bus clk\n");
246		goto err_bus_clk;
247	}
248
249	ret = clk_prepare_enable(hipcie->sys_clk);
250	if (ret) {
251		dev_err(dev, "cannot prepare/enable sys clk\n");
252		goto err_sys_clk;
253	}
254
255	ret = clk_prepare_enable(hipcie->pipe_clk);
256	if (ret) {
257		dev_err(dev, "cannot prepare/enable pipe clk\n");
258		goto err_pipe_clk;
259	}
260
261	ret = clk_prepare_enable(hipcie->aux_clk);
262	if (ret) {
263		dev_err(dev, "cannot prepare/enable aux clk\n");
264		goto err_aux_clk;
265	}
266
267	reset_control_assert(hipcie->soft_reset);
268	reset_control_deassert(hipcie->soft_reset);
269
270	reset_control_assert(hipcie->sys_reset);
271	reset_control_deassert(hipcie->sys_reset);
272
273	reset_control_assert(hipcie->bus_reset);
274	reset_control_deassert(hipcie->bus_reset);
275
276	return 0;
277
278err_aux_clk:
279	clk_disable_unprepare(hipcie->pipe_clk);
280err_pipe_clk:
281	clk_disable_unprepare(hipcie->sys_clk);
282err_sys_clk:
283	clk_disable_unprepare(hipcie->bus_clk);
284err_bus_clk:
285	if (hipcie->vpcie)
286		regulator_disable(hipcie->vpcie);
287
288	return ret;
289}
290
291static const struct dw_pcie_ops dw_pcie_ops = {
292	.read_dbi = histb_pcie_read_dbi,
293	.write_dbi = histb_pcie_write_dbi,
294	.link_up = histb_pcie_link_up,
295	.start_link = histb_pcie_start_link,
296};
297
298static int histb_pcie_probe(struct platform_device *pdev)
299{
300	struct histb_pcie *hipcie;
301	struct dw_pcie *pci;
302	struct pcie_port *pp;
303	struct device_node *np = pdev->dev.of_node;
304	struct device *dev = &pdev->dev;
305	enum of_gpio_flags of_flags;
306	unsigned long flag = GPIOF_DIR_OUT;
307	int ret;
308
309	hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
310	if (!hipcie)
311		return -ENOMEM;
312
313	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
314	if (!pci)
315		return -ENOMEM;
316
317	hipcie->pci = pci;
318	pp = &pci->pp;
319	pci->dev = dev;
320	pci->ops = &dw_pcie_ops;
321
322	hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
323	if (IS_ERR(hipcie->ctrl)) {
324		dev_err(dev, "cannot get control reg base\n");
325		return PTR_ERR(hipcie->ctrl);
326	}
327
328	pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
329	if (IS_ERR(pci->dbi_base)) {
330		dev_err(dev, "cannot get rc-dbi base\n");
331		return PTR_ERR(pci->dbi_base);
332	}
333
334	hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
335	if (IS_ERR(hipcie->vpcie)) {
336		if (PTR_ERR(hipcie->vpcie) != -ENODEV)
337			return PTR_ERR(hipcie->vpcie);
338		hipcie->vpcie = NULL;
339	}
340
341	hipcie->reset_gpio = of_get_named_gpio_flags(np,
342				"reset-gpios", 0, &of_flags);
343	if (of_flags & OF_GPIO_ACTIVE_LOW)
344		flag |= GPIOF_ACTIVE_LOW;
345	if (gpio_is_valid(hipcie->reset_gpio)) {
346		ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
347				flag, "PCIe device power control");
348		if (ret) {
349			dev_err(dev, "unable to request gpio\n");
350			return ret;
351		}
352	}
353
354	hipcie->aux_clk = devm_clk_get(dev, "aux");
355	if (IS_ERR(hipcie->aux_clk)) {
356		dev_err(dev, "Failed to get PCIe aux clk\n");
357		return PTR_ERR(hipcie->aux_clk);
358	}
359
360	hipcie->pipe_clk = devm_clk_get(dev, "pipe");
361	if (IS_ERR(hipcie->pipe_clk)) {
362		dev_err(dev, "Failed to get PCIe pipe clk\n");
363		return PTR_ERR(hipcie->pipe_clk);
364	}
365
366	hipcie->sys_clk = devm_clk_get(dev, "sys");
367	if (IS_ERR(hipcie->sys_clk)) {
368		dev_err(dev, "Failed to get PCIEe sys clk\n");
369		return PTR_ERR(hipcie->sys_clk);
370	}
371
372	hipcie->bus_clk = devm_clk_get(dev, "bus");
373	if (IS_ERR(hipcie->bus_clk)) {
374		dev_err(dev, "Failed to get PCIe bus clk\n");
375		return PTR_ERR(hipcie->bus_clk);
376	}
377
378	hipcie->soft_reset = devm_reset_control_get(dev, "soft");
379	if (IS_ERR(hipcie->soft_reset)) {
380		dev_err(dev, "couldn't get soft reset\n");
381		return PTR_ERR(hipcie->soft_reset);
382	}
383
384	hipcie->sys_reset = devm_reset_control_get(dev, "sys");
385	if (IS_ERR(hipcie->sys_reset)) {
386		dev_err(dev, "couldn't get sys reset\n");
387		return PTR_ERR(hipcie->sys_reset);
388	}
389
390	hipcie->bus_reset = devm_reset_control_get(dev, "bus");
391	if (IS_ERR(hipcie->bus_reset)) {
392		dev_err(dev, "couldn't get bus reset\n");
393		return PTR_ERR(hipcie->bus_reset);
394	}
395
396	hipcie->phy = devm_phy_get(dev, "phy");
397	if (IS_ERR(hipcie->phy)) {
398		dev_info(dev, "no pcie-phy found\n");
399		hipcie->phy = NULL;
400		/* fall through here!
401		 * if no pcie-phy found, phy init
402		 * should be done under boot!
403		 */
404	} else {
405		phy_init(hipcie->phy);
406	}
407
408	pp->ops = &histb_pcie_host_ops;
409
410	platform_set_drvdata(pdev, hipcie);
411
412	ret = histb_pcie_host_enable(pp);
413	if (ret) {
414		dev_err(dev, "failed to enable host\n");
415		return ret;
416	}
417
418	ret = dw_pcie_host_init(pp);
419	if (ret) {
420		dev_err(dev, "failed to initialize host\n");
421		return ret;
422	}
423
424	return 0;
425}
426
427static int histb_pcie_remove(struct platform_device *pdev)
428{
429	struct histb_pcie *hipcie = platform_get_drvdata(pdev);
430
431	histb_pcie_host_disable(hipcie);
432
433	if (hipcie->phy)
434		phy_exit(hipcie->phy);
435
436	return 0;
437}
438
439static const struct of_device_id histb_pcie_of_match[] = {
440	{ .compatible = "hisilicon,hi3798cv200-pcie", },
441	{},
442};
443MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
444
445static struct platform_driver histb_pcie_platform_driver = {
446	.probe	= histb_pcie_probe,
447	.remove	= histb_pcie_remove,
448	.driver = {
449		.name = "histb-pcie",
450		.of_match_table = histb_pcie_of_match,
451	},
452};
453module_platform_driver(histb_pcie_platform_driver);
454
455MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
456MODULE_LICENSE("GPL v2");