Merge tag 'rproc-v5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/andersson...
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pcie-histb.c
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
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         int reset_gpio;
64         struct regulator *vpcie;
65 };
66
67 static u32 histb_pcie_readl(struct histb_pcie *histb_pcie, u32 reg)
68 {
69         return readl(histb_pcie->ctrl + reg);
70 }
71
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
77 static 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
91 static 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
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
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
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                 *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
139 static 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
151 static struct pci_ops histb_pci_ops = {
152         .read = histb_pcie_rd_own_conf,
153         .write = histb_pcie_wr_own_conf,
154 };
155
156 static 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
172 static 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
200 static int histb_pcie_host_init(struct pcie_port *pp)
201 {
202         pp->bridge->ops = &histb_pci_ops;
203
204         histb_pcie_establish_link(pp);
205         dw_pcie_msi_init(pp);
206
207         return 0;
208 }
209
210 static const struct dw_pcie_host_ops histb_pcie_host_ops = {
211         .host_init = histb_pcie_host_init,
212 };
213
214 static void histb_pcie_host_disable(struct histb_pcie *hipcie)
215 {
216         reset_control_assert(hipcie->soft_reset);
217         reset_control_assert(hipcie->sys_reset);
218         reset_control_assert(hipcie->bus_reset);
219
220         clk_disable_unprepare(hipcie->aux_clk);
221         clk_disable_unprepare(hipcie->pipe_clk);
222         clk_disable_unprepare(hipcie->sys_clk);
223         clk_disable_unprepare(hipcie->bus_clk);
224
225         if (gpio_is_valid(hipcie->reset_gpio))
226                 gpio_set_value_cansleep(hipcie->reset_gpio, 0);
227
228         if (hipcie->vpcie)
229                 regulator_disable(hipcie->vpcie);
230 }
231
232 static int histb_pcie_host_enable(struct pcie_port *pp)
233 {
234         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
235         struct histb_pcie *hipcie = to_histb_pcie(pci);
236         struct device *dev = pci->dev;
237         int ret;
238
239         /* power on PCIe device if have */
240         if (hipcie->vpcie) {
241                 ret = regulator_enable(hipcie->vpcie);
242                 if (ret) {
243                         dev_err(dev, "failed to enable regulator: %d\n", ret);
244                         return ret;
245                 }
246         }
247
248         if (gpio_is_valid(hipcie->reset_gpio))
249                 gpio_set_value_cansleep(hipcie->reset_gpio, 1);
250
251         ret = clk_prepare_enable(hipcie->bus_clk);
252         if (ret) {
253                 dev_err(dev, "cannot prepare/enable bus clk\n");
254                 goto err_bus_clk;
255         }
256
257         ret = clk_prepare_enable(hipcie->sys_clk);
258         if (ret) {
259                 dev_err(dev, "cannot prepare/enable sys clk\n");
260                 goto err_sys_clk;
261         }
262
263         ret = clk_prepare_enable(hipcie->pipe_clk);
264         if (ret) {
265                 dev_err(dev, "cannot prepare/enable pipe clk\n");
266                 goto err_pipe_clk;
267         }
268
269         ret = clk_prepare_enable(hipcie->aux_clk);
270         if (ret) {
271                 dev_err(dev, "cannot prepare/enable aux clk\n");
272                 goto err_aux_clk;
273         }
274
275         reset_control_assert(hipcie->soft_reset);
276         reset_control_deassert(hipcie->soft_reset);
277
278         reset_control_assert(hipcie->sys_reset);
279         reset_control_deassert(hipcie->sys_reset);
280
281         reset_control_assert(hipcie->bus_reset);
282         reset_control_deassert(hipcie->bus_reset);
283
284         return 0;
285
286 err_aux_clk:
287         clk_disable_unprepare(hipcie->pipe_clk);
288 err_pipe_clk:
289         clk_disable_unprepare(hipcie->sys_clk);
290 err_sys_clk:
291         clk_disable_unprepare(hipcie->bus_clk);
292 err_bus_clk:
293         if (hipcie->vpcie)
294                 regulator_disable(hipcie->vpcie);
295
296         return ret;
297 }
298
299 static const struct dw_pcie_ops dw_pcie_ops = {
300         .read_dbi = histb_pcie_read_dbi,
301         .write_dbi = histb_pcie_write_dbi,
302         .link_up = histb_pcie_link_up,
303 };
304
305 static int histb_pcie_probe(struct platform_device *pdev)
306 {
307         struct histb_pcie *hipcie;
308         struct dw_pcie *pci;
309         struct pcie_port *pp;
310         struct device_node *np = pdev->dev.of_node;
311         struct device *dev = &pdev->dev;
312         enum of_gpio_flags of_flags;
313         unsigned long flag = GPIOF_DIR_OUT;
314         int ret;
315
316         hipcie = devm_kzalloc(dev, sizeof(*hipcie), GFP_KERNEL);
317         if (!hipcie)
318                 return -ENOMEM;
319
320         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
321         if (!pci)
322                 return -ENOMEM;
323
324         hipcie->pci = pci;
325         pp = &pci->pp;
326         pci->dev = dev;
327         pci->ops = &dw_pcie_ops;
328
329         hipcie->ctrl = devm_platform_ioremap_resource_byname(pdev, "control");
330         if (IS_ERR(hipcie->ctrl)) {
331                 dev_err(dev, "cannot get control reg base\n");
332                 return PTR_ERR(hipcie->ctrl);
333         }
334
335         pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc-dbi");
336         if (IS_ERR(pci->dbi_base)) {
337                 dev_err(dev, "cannot get rc-dbi base\n");
338                 return PTR_ERR(pci->dbi_base);
339         }
340
341         hipcie->vpcie = devm_regulator_get_optional(dev, "vpcie");
342         if (IS_ERR(hipcie->vpcie)) {
343                 if (PTR_ERR(hipcie->vpcie) != -ENODEV)
344                         return PTR_ERR(hipcie->vpcie);
345                 hipcie->vpcie = NULL;
346         }
347
348         hipcie->reset_gpio = of_get_named_gpio_flags(np,
349                                 "reset-gpios", 0, &of_flags);
350         if (of_flags & OF_GPIO_ACTIVE_LOW)
351                 flag |= GPIOF_ACTIVE_LOW;
352         if (gpio_is_valid(hipcie->reset_gpio)) {
353                 ret = devm_gpio_request_one(dev, hipcie->reset_gpio,
354                                 flag, "PCIe device power control");
355                 if (ret) {
356                         dev_err(dev, "unable to request gpio\n");
357                         return ret;
358                 }
359         }
360
361         hipcie->aux_clk = devm_clk_get(dev, "aux");
362         if (IS_ERR(hipcie->aux_clk)) {
363                 dev_err(dev, "Failed to get PCIe aux clk\n");
364                 return PTR_ERR(hipcie->aux_clk);
365         }
366
367         hipcie->pipe_clk = devm_clk_get(dev, "pipe");
368         if (IS_ERR(hipcie->pipe_clk)) {
369                 dev_err(dev, "Failed to get PCIe pipe clk\n");
370                 return PTR_ERR(hipcie->pipe_clk);
371         }
372
373         hipcie->sys_clk = devm_clk_get(dev, "sys");
374         if (IS_ERR(hipcie->sys_clk)) {
375                 dev_err(dev, "Failed to get PCIEe sys clk\n");
376                 return PTR_ERR(hipcie->sys_clk);
377         }
378
379         hipcie->bus_clk = devm_clk_get(dev, "bus");
380         if (IS_ERR(hipcie->bus_clk)) {
381                 dev_err(dev, "Failed to get PCIe bus clk\n");
382                 return PTR_ERR(hipcie->bus_clk);
383         }
384
385         hipcie->soft_reset = devm_reset_control_get(dev, "soft");
386         if (IS_ERR(hipcie->soft_reset)) {
387                 dev_err(dev, "couldn't get soft reset\n");
388                 return PTR_ERR(hipcie->soft_reset);
389         }
390
391         hipcie->sys_reset = devm_reset_control_get(dev, "sys");
392         if (IS_ERR(hipcie->sys_reset)) {
393                 dev_err(dev, "couldn't get sys reset\n");
394                 return PTR_ERR(hipcie->sys_reset);
395         }
396
397         hipcie->bus_reset = devm_reset_control_get(dev, "bus");
398         if (IS_ERR(hipcie->bus_reset)) {
399                 dev_err(dev, "couldn't get bus reset\n");
400                 return PTR_ERR(hipcie->bus_reset);
401         }
402
403         if (IS_ENABLED(CONFIG_PCI_MSI)) {
404                 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
405                 if (pp->msi_irq < 0)
406                         return pp->msi_irq;
407         }
408
409         hipcie->phy = devm_phy_get(dev, "phy");
410         if (IS_ERR(hipcie->phy)) {
411                 dev_info(dev, "no pcie-phy found\n");
412                 hipcie->phy = NULL;
413                 /* fall through here!
414                  * if no pcie-phy found, phy init
415                  * should be done under boot!
416                  */
417         } else {
418                 phy_init(hipcie->phy);
419         }
420
421         pp->ops = &histb_pcie_host_ops;
422
423         platform_set_drvdata(pdev, hipcie);
424
425         ret = histb_pcie_host_enable(pp);
426         if (ret) {
427                 dev_err(dev, "failed to enable host\n");
428                 return ret;
429         }
430
431         ret = dw_pcie_host_init(pp);
432         if (ret) {
433                 dev_err(dev, "failed to initialize host\n");
434                 return ret;
435         }
436
437         return 0;
438 }
439
440 static int histb_pcie_remove(struct platform_device *pdev)
441 {
442         struct histb_pcie *hipcie = platform_get_drvdata(pdev);
443
444         histb_pcie_host_disable(hipcie);
445
446         if (hipcie->phy)
447                 phy_exit(hipcie->phy);
448
449         return 0;
450 }
451
452 static const struct of_device_id histb_pcie_of_match[] = {
453         { .compatible = "hisilicon,hi3798cv200-pcie", },
454         {},
455 };
456 MODULE_DEVICE_TABLE(of, histb_pcie_of_match);
457
458 static struct platform_driver histb_pcie_platform_driver = {
459         .probe  = histb_pcie_probe,
460         .remove = histb_pcie_remove,
461         .driver = {
462                 .name = "histb-pcie",
463                 .of_match_table = histb_pcie_of_match,
464         },
465 };
466 module_platform_driver(histb_pcie_platform_driver);
467
468 MODULE_DESCRIPTION("HiSilicon STB PCIe host controller driver");
469 MODULE_LICENSE("GPL v2");