blob: af1deae2b15de186676fa65a2ac0bd718f8df765 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Jianguo Sunbbd11bd2017-10-23 19:17:50 +08002/*
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>
Jianguo Sunbbd11bd2017-10-23 19:17:50 +08009 */
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;
Shawn Guo58dfb242018-03-02 09:12:01 +080064 struct regulator *vpcie;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +080065};
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
Rob Herringc4a42ee2020-08-20 21:53:51 -0600125static int histb_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
126 int where, int size, u32 *val)
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800127{
Rob Herringc4a42ee2020-08-20 21:53:51 -0600128 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800129
Rob Herringc4a42ee2020-08-20 21:53:51 -0600130 if (PCI_SLOT(devfn)) {
131 *val = ~0;
132 return PCIBIOS_DEVICE_NOT_FOUND;
133 }
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800134
Rob Herringc4a42ee2020-08-20 21:53:51 -0600135 *val = dw_pcie_read_dbi(pci, where, size);
136 return PCIBIOS_SUCCESSFUL;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800137}
138
Rob Herringc4a42ee2020-08-20 21:53:51 -0600139static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
140 int where, int size, u32 val)
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800141{
Rob Herringc4a42ee2020-08-20 21:53:51 -0600142 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800143
Rob Herringc4a42ee2020-08-20 21:53:51 -0600144 if (PCI_SLOT(devfn))
145 return PCIBIOS_DEVICE_NOT_FOUND;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800146
Rob Herringc4a42ee2020-08-20 21:53:51 -0600147 dw_pcie_write_dbi(pci, where, size, val);
148 return PCIBIOS_SUCCESSFUL;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800149}
150
Rob Herringc4a42ee2020-08-20 21:53:51 -0600151static struct pci_ops histb_pci_ops = {
152 .read = histb_pcie_rd_own_conf,
153 .write = histb_pcie_wr_own_conf,
154};
155
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800156static 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_establish_link(struct pcie_port *pp)
173{
174 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
175 struct histb_pcie *hipcie = to_histb_pcie(pci);
176 u32 regval;
177
178 if (dw_pcie_link_up(pci)) {
179 dev_info(pci->dev, "Link already up\n");
180 return 0;
181 }
182
183 /* PCIe RC work mode */
184 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
185 regval &= ~PCIE_DEVICE_TYPE_MASK;
186 regval |= PCIE_WM_RC;
187 histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
188
189 /* setup root complex */
190 dw_pcie_setup_rc(pp);
191
192 /* assert LTSSM enable */
193 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
194 regval |= PCIE_APP_LTSSM_ENABLE;
195 histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
196
197 return dw_pcie_wait_for_link(pci);
198}
199
200static int histb_pcie_host_init(struct pcie_port *pp)
201{
Rob Herringc4a42ee2020-08-20 21:53:51 -0600202 pp->bridge->ops = &histb_pci_ops;
203
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800204 histb_pcie_establish_link(pp);
205
206 if (IS_ENABLED(CONFIG_PCI_MSI))
207 dw_pcie_msi_init(pp);
208
209 return 0;
210}
211
Julia Lawallb69f4ab2018-10-27 20:31:19 +0200212static const struct dw_pcie_host_ops histb_pcie_host_ops = {
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800213 .host_init = histb_pcie_host_init,
214};
215
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800216static void histb_pcie_host_disable(struct histb_pcie *hipcie)
217{
218 reset_control_assert(hipcie->soft_reset);
219 reset_control_assert(hipcie->sys_reset);
220 reset_control_assert(hipcie->bus_reset);
221
222 clk_disable_unprepare(hipcie->aux_clk);
223 clk_disable_unprepare(hipcie->pipe_clk);
224 clk_disable_unprepare(hipcie->sys_clk);
225 clk_disable_unprepare(hipcie->bus_clk);
226
227 if (gpio_is_valid(hipcie->reset_gpio))
228 gpio_set_value_cansleep(hipcie->reset_gpio, 0);
Shawn Guo58dfb242018-03-02 09:12:01 +0800229
230 if (hipcie->vpcie)
231 regulator_disable(hipcie->vpcie);
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800232}
233
234static int histb_pcie_host_enable(struct pcie_port *pp)
235{
236 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
237 struct histb_pcie *hipcie = to_histb_pcie(pci);
238 struct device *dev = pci->dev;
239 int ret;
240
241 /* power on PCIe device if have */
Shawn Guo58dfb242018-03-02 09:12:01 +0800242 if (hipcie->vpcie) {
243 ret = regulator_enable(hipcie->vpcie);
244 if (ret) {
245 dev_err(dev, "failed to enable regulator: %d\n", ret);
246 return ret;
247 }
248 }
249
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800250 if (gpio_is_valid(hipcie->reset_gpio))
251 gpio_set_value_cansleep(hipcie->reset_gpio, 1);
252
253 ret = clk_prepare_enable(hipcie->bus_clk);
254 if (ret) {
255 dev_err(dev, "cannot prepare/enable bus clk\n");
256 goto err_bus_clk;
257 }
258
259 ret = clk_prepare_enable(hipcie->sys_clk);
260 if (ret) {
261 dev_err(dev, "cannot prepare/enable sys clk\n");
262 goto err_sys_clk;
263 }
264
265 ret = clk_prepare_enable(hipcie->pipe_clk);
266 if (ret) {
267 dev_err(dev, "cannot prepare/enable pipe clk\n");
268 goto err_pipe_clk;
269 }
270
271 ret = clk_prepare_enable(hipcie->aux_clk);
272 if (ret) {
273 dev_err(dev, "cannot prepare/enable aux clk\n");
274 goto err_aux_clk;
275 }
276
277 reset_control_assert(hipcie->soft_reset);
278 reset_control_deassert(hipcie->soft_reset);
279
280 reset_control_assert(hipcie->sys_reset);
281 reset_control_deassert(hipcie->sys_reset);
282
283 reset_control_assert(hipcie->bus_reset);
284 reset_control_deassert(hipcie->bus_reset);
285
286 return 0;
287
288err_aux_clk:
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800289 clk_disable_unprepare(hipcie->pipe_clk);
Shawn Guodb0c25f2018-03-02 09:12:00 +0800290err_pipe_clk:
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800291 clk_disable_unprepare(hipcie->sys_clk);
Shawn Guodb0c25f2018-03-02 09:12:00 +0800292err_sys_clk:
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800293 clk_disable_unprepare(hipcie->bus_clk);
Shawn Guodb0c25f2018-03-02 09:12:00 +0800294err_bus_clk:
Shawn Guo58dfb242018-03-02 09:12:01 +0800295 if (hipcie->vpcie)
296 regulator_disable(hipcie->vpcie);
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800297
298 return ret;
299}
300
301static const struct dw_pcie_ops dw_pcie_ops = {
302 .read_dbi = histb_pcie_read_dbi,
303 .write_dbi = histb_pcie_write_dbi,
304 .link_up = histb_pcie_link_up,
305};
306
307static int histb_pcie_probe(struct platform_device *pdev)
308{
309 struct histb_pcie *hipcie;
310 struct dw_pcie *pci;
311 struct pcie_port *pp;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800312 struct device_node *np = pdev->dev.of_node;
313 struct device *dev = &pdev->dev;
314 enum of_gpio_flags of_flags;
315 unsigned long flag = GPIOF_DIR_OUT;
316 int ret;
317
318 hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
319 if (!hipcie)
320 return -ENOMEM;
321
322 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
323 if (!pci)
324 return -ENOMEM;
325
326 hipcie->pci = pci;
327 pp = &pci->pp;
328 pci->dev = dev;
329 pci->ops = &dw_pcie_ops;
330
Dejin Zheng936fa5c2020-07-09 00:40:13 +0800331 hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800332 if (IS_ERR(hipcie->ctrl)) {
333 dev_err(dev, "cannot get control reg base\n");
334 return PTR_ERR(hipcie->ctrl);
335 }
336
Dejin Zheng936fa5c2020-07-09 00:40:13 +0800337 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800338 if (IS_ERR(pci->dbi_base)) {
339 dev_err(dev, "cannot get rc-dbi base\n");
340 return PTR_ERR(pci->dbi_base);
341 }
342
Shawn Guo58dfb242018-03-02 09:12:01 +0800343 hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
344 if (IS_ERR(hipcie->vpcie)) {
Thierry Reding8f9e1642019-08-29 12:53:18 +0200345 if (PTR_ERR(hipcie->vpcie) != -ENODEV)
346 return PTR_ERR(hipcie->vpcie);
Shawn Guo58dfb242018-03-02 09:12:01 +0800347 hipcie->vpcie = NULL;
348 }
349
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800350 hipcie->reset_gpio = of_get_named_gpio_flags(np,
351 "reset-gpios", 0, &of_flags);
352 if (of_flags & OF_GPIO_ACTIVE_LOW)
353 flag |= GPIOF_ACTIVE_LOW;
354 if (gpio_is_valid(hipcie->reset_gpio)) {
355 ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
356 flag, "PCIe device power control");
357 if (ret) {
358 dev_err(dev, "unable to request gpio\n");
359 return ret;
360 }
361 }
362
363 hipcie->aux_clk = devm_clk_get(dev, "aux");
364 if (IS_ERR(hipcie->aux_clk)) {
365 dev_err(dev, "Failed to get PCIe aux clk\n");
366 return PTR_ERR(hipcie->aux_clk);
367 }
368
369 hipcie->pipe_clk = devm_clk_get(dev, "pipe");
370 if (IS_ERR(hipcie->pipe_clk)) {
371 dev_err(dev, "Failed to get PCIe pipe clk\n");
372 return PTR_ERR(hipcie->pipe_clk);
373 }
374
375 hipcie->sys_clk = devm_clk_get(dev, "sys");
376 if (IS_ERR(hipcie->sys_clk)) {
377 dev_err(dev, "Failed to get PCIEe sys clk\n");
378 return PTR_ERR(hipcie->sys_clk);
379 }
380
381 hipcie->bus_clk = devm_clk_get(dev, "bus");
382 if (IS_ERR(hipcie->bus_clk)) {
383 dev_err(dev, "Failed to get PCIe bus clk\n");
384 return PTR_ERR(hipcie->bus_clk);
385 }
386
387 hipcie->soft_reset = devm_reset_control_get(dev, "soft");
388 if (IS_ERR(hipcie->soft_reset)) {
389 dev_err(dev, "couldn't get soft reset\n");
390 return PTR_ERR(hipcie->soft_reset);
391 }
392
393 hipcie->sys_reset = devm_reset_control_get(dev, "sys");
394 if (IS_ERR(hipcie->sys_reset)) {
395 dev_err(dev, "couldn't get sys reset\n");
396 return PTR_ERR(hipcie->sys_reset);
397 }
398
399 hipcie->bus_reset = devm_reset_control_get(dev, "bus");
400 if (IS_ERR(hipcie->bus_reset)) {
401 dev_err(dev, "couldn't get bus reset\n");
402 return PTR_ERR(hipcie->bus_reset);
403 }
404
405 if (IS_ENABLED(CONFIG_PCI_MSI)) {
406 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
Krzysztof WilczyƄskicaecb052020-08-02 14:25:53 +0000407 if (pp->msi_irq < 0)
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800408 return pp->msi_irq;
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800409 }
410
411 hipcie->phy = devm_phy_get(dev, "phy");
412 if (IS_ERR(hipcie->phy)) {
413 dev_info(dev, "no pcie-phy found\n");
414 hipcie->phy = NULL;
415 /* fall through here!
416 * if no pcie-phy found, phy init
417 * should be done under boot!
418 */
419 } else {
420 phy_init(hipcie->phy);
421 }
422
Jianguo Sunbbd11bd2017-10-23 19:17:50 +0800423 pp->ops = &histb_pcie_host_ops;
424
425 platform_set_drvdata(pdev, hipcie);
426
427 ret = histb_pcie_host_enable(pp);
428 if (ret) {
429 dev_err(dev, "failed to enable host\n");
430 return ret;
431 }
432
433 ret = dw_pcie_host_init(pp);
434 if (ret) {
435 dev_err(dev, "failed to initialize host\n");
436 return ret;
437 }
438
439 return 0;
440}
441
442static int histb_pcie_remove(struct platform_device *pdev)
443{
444 struct histb_pcie *hipcie = platform_get_drvdata(pdev);
445
446 histb_pcie_host_disable(hipcie);
447
448 if (hipcie->phy)
449 phy_exit(hipcie->phy);
450
451 return 0;
452}
453
454static const struct of_device_id histb_pcie_of_match[] = {
455 { .compatible = "hisilicon,hi3798cv200-pcie", },
456 {},
457};
458MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
459
460static struct platform_driver histb_pcie_platform_driver = {
461 .probe = histb_pcie_probe,
462 .remove = histb_pcie_remove,
463 .driver = {
464 .name = "histb-pcie",
465 .of_match_table = histb_pcie_of_match,
466 },
467};
468module_platform_driver(histb_pcie_platform_driver);
469
470MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
471MODULE_LICENSE("GPL v2");