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/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.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
52 struct 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 struct gpio_desc *reset_gpio;
64 struct regulator *vpcie;
65 };
66
histb_pcie_readl(struct histb_pcie * histb_pcie,u32 reg)67 static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
68 {
69 return readl(histb_pcie->ctrl + reg);
70 }
71
histb_pcie_writel(struct histb_pcie * histb_pcie,u32 reg,u32 val)72 static void histb_pcie_writel(struct histb_pcie *histb_pcie, u32 reg, u32 val)
73 {
74 writel(val, histb_pcie->ctrl + reg);
75 }
76
histb_pcie_dbi_w_mode(struct dw_pcie_rp * pp,bool enable)77 static void histb_pcie_dbi_w_mode(struct dw_pcie_rp *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
histb_pcie_dbi_r_mode(struct dw_pcie_rp * pp,bool enable)91 static void histb_pcie_dbi_r_mode(struct dw_pcie_rp *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
histb_pcie_read_dbi(struct dw_pcie * pci,void __iomem * base,u32 reg,size_t size)105 static 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
histb_pcie_write_dbi(struct dw_pcie * pci,void __iomem * base,u32 reg,size_t size,u32 val)117 static 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
histb_pcie_rd_own_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)125 static 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 return PCIBIOS_DEVICE_NOT_FOUND;
132
133 *val = dw_pcie_read_dbi(pci, where, size);
134 return PCIBIOS_SUCCESSFUL;
135 }
136
histb_pcie_wr_own_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)137 static int histb_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
138 int where, int size, u32 val)
139 {
140 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
141
142 if (PCI_SLOT(devfn))
143 return PCIBIOS_DEVICE_NOT_FOUND;
144
145 dw_pcie_write_dbi(pci, where, size, val);
146 return PCIBIOS_SUCCESSFUL;
147 }
148
149 static struct pci_ops histb_pci_ops = {
150 .read = histb_pcie_rd_own_conf,
151 .write = histb_pcie_wr_own_conf,
152 };
153
histb_pcie_link_up(struct dw_pcie * pci)154 static bool histb_pcie_link_up(struct dw_pcie *pci)
155 {
156 struct histb_pcie *hipcie = to_histb_pcie(pci);
157 u32 regval;
158 u32 status;
159
160 regval = histb_pcie_readl(hipcie, PCIE_SYS_STAT0);
161 status = histb_pcie_readl(hipcie, PCIE_SYS_STAT4);
162 status &= PCIE_LTSSM_STATE_MASK;
163 return ((regval & PCIE_XMLH_LINK_UP) && (regval & PCIE_RDLH_LINK_UP) &&
164 (status == PCIE_LTSSM_STATE_ACTIVE));
165 }
166
histb_pcie_start_link(struct dw_pcie * pci)167 static int histb_pcie_start_link(struct dw_pcie *pci)
168 {
169 struct histb_pcie *hipcie = to_histb_pcie(pci);
170 u32 regval;
171
172 /* assert LTSSM enable */
173 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL7);
174 regval |= PCIE_APP_LTSSM_ENABLE;
175 histb_pcie_writel(hipcie, PCIE_SYS_CTRL7, regval);
176
177 return 0;
178 }
179
histb_pcie_host_init(struct dw_pcie_rp * pp)180 static int histb_pcie_host_init(struct dw_pcie_rp *pp)
181 {
182 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
183 struct histb_pcie *hipcie = to_histb_pcie(pci);
184 u32 regval;
185
186 pp->bridge->ops = &histb_pci_ops;
187
188 /* PCIe RC work mode */
189 regval = histb_pcie_readl(hipcie, PCIE_SYS_CTRL0);
190 regval &= ~PCIE_DEVICE_TYPE_MASK;
191 regval |= PCIE_WM_RC;
192 histb_pcie_writel(hipcie, PCIE_SYS_CTRL0, regval);
193
194 return 0;
195 }
196
197 static const struct dw_pcie_host_ops histb_pcie_host_ops = {
198 .init = histb_pcie_host_init,
199 };
200
histb_pcie_host_disable(struct histb_pcie * hipcie)201 static void histb_pcie_host_disable(struct histb_pcie *hipcie)
202 {
203 reset_control_assert(hipcie->soft_reset);
204 reset_control_assert(hipcie->sys_reset);
205 reset_control_assert(hipcie->bus_reset);
206
207 clk_disable_unprepare(hipcie->aux_clk);
208 clk_disable_unprepare(hipcie->pipe_clk);
209 clk_disable_unprepare(hipcie->sys_clk);
210 clk_disable_unprepare(hipcie->bus_clk);
211
212 if (hipcie->reset_gpio)
213 gpiod_set_value_cansleep(hipcie->reset_gpio, 1);
214
215 if (hipcie->vpcie)
216 regulator_disable(hipcie->vpcie);
217 }
218
histb_pcie_host_enable(struct dw_pcie_rp * pp)219 static int histb_pcie_host_enable(struct dw_pcie_rp *pp)
220 {
221 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
222 struct histb_pcie *hipcie = to_histb_pcie(pci);
223 struct device *dev = pci->dev;
224 int ret;
225
226 /* power on PCIe device if have */
227 if (hipcie->vpcie) {
228 ret = regulator_enable(hipcie->vpcie);
229 if (ret) {
230 dev_err(dev, "failed to enable regulator: %d\n", ret);
231 return ret;
232 }
233 }
234
235 if (hipcie->reset_gpio)
236 gpiod_set_value_cansleep(hipcie->reset_gpio, 0);
237
238 ret = clk_prepare_enable(hipcie->bus_clk);
239 if (ret) {
240 dev_err(dev, "cannot prepare/enable bus clk\n");
241 goto err_bus_clk;
242 }
243
244 ret = clk_prepare_enable(hipcie->sys_clk);
245 if (ret) {
246 dev_err(dev, "cannot prepare/enable sys clk\n");
247 goto err_sys_clk;
248 }
249
250 ret = clk_prepare_enable(hipcie->pipe_clk);
251 if (ret) {
252 dev_err(dev, "cannot prepare/enable pipe clk\n");
253 goto err_pipe_clk;
254 }
255
256 ret = clk_prepare_enable(hipcie->aux_clk);
257 if (ret) {
258 dev_err(dev, "cannot prepare/enable aux clk\n");
259 goto err_aux_clk;
260 }
261
262 reset_control_assert(hipcie->soft_reset);
263 reset_control_deassert(hipcie->soft_reset);
264
265 reset_control_assert(hipcie->sys_reset);
266 reset_control_deassert(hipcie->sys_reset);
267
268 reset_control_assert(hipcie->bus_reset);
269 reset_control_deassert(hipcie->bus_reset);
270
271 return 0;
272
273 err_aux_clk:
274 clk_disable_unprepare(hipcie->pipe_clk);
275 err_pipe_clk:
276 clk_disable_unprepare(hipcie->sys_clk);
277 err_sys_clk:
278 clk_disable_unprepare(hipcie->bus_clk);
279 err_bus_clk:
280 if (hipcie->vpcie)
281 regulator_disable(hipcie->vpcie);
282
283 return ret;
284 }
285
286 static const struct dw_pcie_ops dw_pcie_ops = {
287 .read_dbi = histb_pcie_read_dbi,
288 .write_dbi = histb_pcie_write_dbi,
289 .link_up = histb_pcie_link_up,
290 .start_link = histb_pcie_start_link,
291 };
292
histb_pcie_probe(struct platform_device * pdev)293 static int histb_pcie_probe(struct platform_device *pdev)
294 {
295 struct histb_pcie *hipcie;
296 struct dw_pcie *pci;
297 struct dw_pcie_rp *pp;
298 struct device *dev = &pdev->dev;
299 int ret;
300
301 hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
302 if (!hipcie)
303 return -ENOMEM;
304
305 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
306 if (!pci)
307 return -ENOMEM;
308
309 hipcie->pci = pci;
310 pp = &pci->pp;
311 pci->dev = dev;
312 pci->ops = &dw_pcie_ops;
313
314 hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
315 if (IS_ERR(hipcie->ctrl)) {
316 dev_err(dev, "cannot get control reg base\n");
317 return PTR_ERR(hipcie->ctrl);
318 }
319
320 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
321 if (IS_ERR(pci->dbi_base)) {
322 dev_err(dev, "cannot get rc-dbi base\n");
323 return PTR_ERR(pci->dbi_base);
324 }
325
326 hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
327 if (IS_ERR(hipcie->vpcie)) {
328 if (PTR_ERR(hipcie->vpcie) != -ENODEV)
329 return PTR_ERR(hipcie->vpcie);
330 hipcie->vpcie = NULL;
331 }
332
333 hipcie->reset_gpio = devm_gpiod_get_optional(dev, "reset",
334 GPIOD_OUT_HIGH);
335 ret = PTR_ERR_OR_ZERO(hipcie->reset_gpio);
336 if (ret) {
337 dev_err(dev, "unable to request reset gpio: %d\n", ret);
338 return ret;
339 }
340
341 ret = gpiod_set_consumer_name(hipcie->reset_gpio,
342 "PCIe device power control");
343 if (ret) {
344 dev_err(dev, "unable to set reset gpio name: %d\n", ret);
345 return ret;
346 }
347
348 hipcie->aux_clk = devm_clk_get(dev, "aux");
349 if (IS_ERR(hipcie->aux_clk)) {
350 dev_err(dev, "Failed to get PCIe aux clk\n");
351 return PTR_ERR(hipcie->aux_clk);
352 }
353
354 hipcie->pipe_clk = devm_clk_get(dev, "pipe");
355 if (IS_ERR(hipcie->pipe_clk)) {
356 dev_err(dev, "Failed to get PCIe pipe clk\n");
357 return PTR_ERR(hipcie->pipe_clk);
358 }
359
360 hipcie->sys_clk = devm_clk_get(dev, "sys");
361 if (IS_ERR(hipcie->sys_clk)) {
362 dev_err(dev, "Failed to get PCIEe sys clk\n");
363 return PTR_ERR(hipcie->sys_clk);
364 }
365
366 hipcie->bus_clk = devm_clk_get(dev, "bus");
367 if (IS_ERR(hipcie->bus_clk)) {
368 dev_err(dev, "Failed to get PCIe bus clk\n");
369 return PTR_ERR(hipcie->bus_clk);
370 }
371
372 hipcie->soft_reset = devm_reset_control_get(dev, "soft");
373 if (IS_ERR(hipcie->soft_reset)) {
374 dev_err(dev, "couldn't get soft reset\n");
375 return PTR_ERR(hipcie->soft_reset);
376 }
377
378 hipcie->sys_reset = devm_reset_control_get(dev, "sys");
379 if (IS_ERR(hipcie->sys_reset)) {
380 dev_err(dev, "couldn't get sys reset\n");
381 return PTR_ERR(hipcie->sys_reset);
382 }
383
384 hipcie->bus_reset = devm_reset_control_get(dev, "bus");
385 if (IS_ERR(hipcie->bus_reset)) {
386 dev_err(dev, "couldn't get bus reset\n");
387 return PTR_ERR(hipcie->bus_reset);
388 }
389
390 hipcie->phy = devm_phy_get(dev, "phy");
391 if (IS_ERR(hipcie->phy)) {
392 dev_info(dev, "no pcie-phy found\n");
393 hipcie->phy = NULL;
394 /* fall through here!
395 * if no pcie-phy found, phy init
396 * should be done under boot!
397 */
398 } else {
399 phy_init(hipcie->phy);
400 }
401
402 pp->ops = &histb_pcie_host_ops;
403
404 platform_set_drvdata(pdev, hipcie);
405
406 ret = histb_pcie_host_enable(pp);
407 if (ret) {
408 dev_err(dev, "failed to enable host\n");
409 goto err_exit_phy;
410 }
411
412 ret = dw_pcie_host_init(pp);
413 if (ret) {
414 dev_err(dev, "failed to initialize host\n");
415 goto err_exit_phy;
416 }
417
418 return 0;
419
420 err_exit_phy:
421 phy_exit(hipcie->phy);
422
423 return ret;
424 }
425
histb_pcie_remove(struct platform_device * pdev)426 static void histb_pcie_remove(struct platform_device *pdev)
427 {
428 struct histb_pcie *hipcie = platform_get_drvdata(pdev);
429
430 histb_pcie_host_disable(hipcie);
431
432 phy_exit(hipcie->phy);
433 }
434
435 static const struct of_device_id histb_pcie_of_match[] = {
436 { .compatible = "hisilicon,hi3798cv200-pcie", },
437 {},
438 };
439 MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
440
441 static struct platform_driver histb_pcie_platform_driver = {
442 .probe = histb_pcie_probe,
443 .remove = histb_pcie_remove,
444 .driver = {
445 .name = "histb-pcie",
446 .of_match_table = histb_pcie_of_match,
447 },
448 };
449 module_platform_driver(histb_pcie_platform_driver);
450
451 MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
452