d73bf850fb4f4c081f5ab140f7c6963ee8c32d39
[openwrt/staging/dedeckeh.git] / target / linux / bmips / files / drivers / pci / controller / pcie-bcm6318.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * BCM6318 PCIe Controller Driver
4 *
5 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
6 * Copyright (C) 2015 Jonas Gorski <jonas.gorski@gmail.com>
7 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
8 */
9
10 #include <linux/clk.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/of_gpio.h>
16 #include <linux/of_irq.h>
17 #include <linux/of_pci.h>
18 #include <linux/of_platform.h>
19 #include <linux/pci.h>
20 #include <linux/reset.h>
21 #include <linux/types.h>
22 #include <linux/version.h>
23 #include <linux/vmalloc.h>
24
25 #include "../pci.h"
26
27 #define PCIE_BUS_BRIDGE 0
28 #define PCIE_BUS_DEVICE 1
29
30 #define PCIE_SPECIFIC_REG 0x188
31 #define SPECIFIC_ENDIAN_MODE_BAR1_SHIFT 0
32 #define SPECIFIC_ENDIAN_MODE_BAR1_MASK (0x3 << SPECIFIC_ENDIAN_MODE_BAR1_SHIFT)
33 #define SPECIFIC_ENDIAN_MODE_BAR2_SHIFT 2
34 #define SPECIFIC_ENDIAN_MODE_BAR2_MASK (0x3 << SPECIFIC_ENDIAN_MODE_BAR1_SHIFT)
35 #define SPECIFIC_ENDIAN_MODE_BAR3_SHIFT 4
36 #define SPECIFIC_ENDIAN_MODE_BAR3_MASK (0x3 << SPECIFIC_ENDIAN_MODE_BAR1_SHIFT)
37 #define SPECIFIC_ENDIAN_MODE_WORD_ALIGN 0
38 #define SPECIFIC_ENDIAN_MODE_HALFWORD_ALIGN 1
39 #define SPECIFIC_ENDIAN_MODE_BYTE_ALIGN 2
40
41 #define PCIE_CONFIG2_REG 0x408
42 #define CONFIG2_BAR1_SIZE_EN 1
43 #define CONFIG2_BAR1_SIZE_MASK 0xf
44
45 #define PCIE_IDVAL3_REG 0x43c
46 #define IDVAL3_CLASS_CODE_MASK 0xffffff
47 #define IDVAL3_SUBCLASS_SHIFT 8
48 #define IDVAL3_CLASS_SHIFT 16
49
50 #define PCIE_DLSTATUS_REG 0x1048
51 #define DLSTATUS_PHYLINKUP (1 << 13)
52
53 #define PCIE_CPU_2_PCIE_MEM_WIN0_LO_REG 0x400c
54 #define C2P_MEM_WIN_ENDIAN_MODE_MASK 0x3
55 #define C2P_MEM_WIN_ENDIAN_NO_SWAP 0
56 #define C2P_MEM_WIN_ENDIAN_HALF_WORD_SWAP 1
57 #define C2P_MEM_WIN_ENDIAN_HALF_BYTE_SWAP 2
58 #define C2P_MEM_WIN_BASE_ADDR_SHIFT 20
59 #define C2P_MEM_WIN_BASE_ADDR_MASK (0xfff << C2P_MEM_WIN_BASE_ADDR_SHIFT)
60
61 #define PCIE_RC_BAR1_CONFIG_LO_REG 0x402c
62 #define RC_BAR_CFG_LO_SIZE_256MB 0xd
63 #define RC_BAR_CFG_LO_MATCH_ADDR_SHIFT 20
64 #define RC_BAR_CFG_LO_MATCH_ADDR_MASK (0xfff << RC_BAR_CFG_LO_MATCH_ADDR_SHIFT)
65
66 #define PCIE_CPU_2_PCIE_MEM_WIN0_BASELIMIT_REG 0x4070
67 #define C2P_BASELIMIT_LIMIT_SHIFT 20
68 #define C2P_BASELIMIT_LIMIT_MASK (0xfff << C2P_BASELIMIT_LIMIT_SHIFT)
69 #define C2P_BASELIMIT_BASE_SHIFT 4
70 #define C2P_BASELIMIT_BASE_MASK (0xfff << C2P_BASELIMIT_BASE_SHIFT)
71
72 #define PCIE_UBUS_BAR1_CFG_REMAP_REG 0x4088
73 #define BAR1_CFG_REMAP_OFFSET_SHIFT 20
74 #define BAR1_CFG_REMAP_OFFSET_MASK (0xfff << BAR1_CFG_REMAP_OFFSET_SHIFT)
75 #define BAR1_CFG_REMAP_ACCESS_EN 1
76
77 #define PCIE_HARD_DEBUG_REG 0x4204
78 #define HARD_DEBUG_SERDES_IDDQ (1 << 23)
79
80 #define PCIE_CPU_INT1_MASK_CLEAR_REG 0x830c
81 #define CPU_INT_PCIE_ERR_ATTN_CPU (1 << 0)
82 #define CPU_INT_PCIE_INTA (1 << 1)
83 #define CPU_INT_PCIE_INTB (1 << 2)
84 #define CPU_INT_PCIE_INTC (1 << 3)
85 #define CPU_INT_PCIE_INTD (1 << 4)
86 #define CPU_INT_PCIE_INTR (1 << 5)
87 #define CPU_INT_PCIE_NMI (1 << 6)
88 #define CPU_INT_PCIE_UBUS (1 << 7)
89 #define CPU_INT_IPI (1 << 8)
90
91 #define PCIE_EXT_CFG_INDEX_REG 0x8400
92 #define EXT_CFG_FUNC_NUM_SHIFT 12
93 #define EXT_CFG_FUNC_NUM_MASK (0x7 << EXT_CFG_FUNC_NUM_SHIFT)
94 #define EXT_CFG_DEV_NUM_SHIFT 15
95 #define EXT_CFG_DEV_NUM_MASK (0xf << EXT_CFG_DEV_NUM_SHIFT)
96 #define EXT_CFG_BUS_NUM_SHIFT 20
97 #define EXT_CFG_BUS_NUM_MASK (0xff << EXT_CFG_BUS_NUM_SHIFT)
98
99 #define PCIE_DEVICE_OFFSET 0x9000
100
101 struct bcm6318_pcie {
102 void __iomem *base;
103 int irq;
104 struct clk *clk;
105 struct clk *clk25;
106 struct clk *clk_ubus;
107 struct reset_control *reset;
108 struct reset_control *reset_ext;
109 struct reset_control *reset_core;
110 struct reset_control *reset_hard;
111 };
112
113 static struct bcm6318_pcie bcm6318_pcie;
114
115 extern int bmips_pci_irq;
116
117 /*
118 * swizzle 32bits data to return only the needed part
119 */
120 static int postprocess_read(u32 data, int where, unsigned int size)
121 {
122 u32 ret = 0;
123
124 switch (size) {
125 case 1:
126 ret = (data >> ((where & 3) << 3)) & 0xff;
127 break;
128 case 2:
129 ret = (data >> ((where & 3) << 3)) & 0xffff;
130 break;
131 case 4:
132 ret = data;
133 break;
134 }
135
136 return ret;
137 }
138
139 static int preprocess_write(u32 orig_data, u32 val, int where,
140 unsigned int size)
141 {
142 u32 ret = 0;
143
144 switch (size) {
145 case 1:
146 ret = (orig_data & ~(0xff << ((where & 3) << 3))) |
147 (val << ((where & 3) << 3));
148 break;
149 case 2:
150 ret = (orig_data & ~(0xffff << ((where & 3) << 3))) |
151 (val << ((where & 3) << 3));
152 break;
153 case 4:
154 ret = val;
155 break;
156 }
157
158 return ret;
159 }
160
161 static int bcm6318_pcie_can_access(struct pci_bus *bus, int devfn)
162 {
163 struct bcm6318_pcie *priv = &bcm6318_pcie;
164
165 switch (bus->number) {
166 case PCIE_BUS_BRIDGE:
167 return PCI_SLOT(devfn) == 0;
168 case PCIE_BUS_DEVICE:
169 if (PCI_SLOT(devfn) == 0)
170 return __raw_readl(priv->base + PCIE_DLSTATUS_REG)
171 & DLSTATUS_PHYLINKUP;
172 fallthrough;
173 default:
174 return false;
175 }
176 }
177
178 static int bcm6318_pcie_read(struct pci_bus *bus, unsigned int devfn,
179 int where, int size, u32 *val)
180 {
181 struct bcm6318_pcie *priv = &bcm6318_pcie;
182 u32 data;
183 u32 reg = where & ~3;
184
185 if (!bcm6318_pcie_can_access(bus, devfn))
186 return PCIBIOS_DEVICE_NOT_FOUND;
187
188 if (bus->number == PCIE_BUS_DEVICE)
189 reg += PCIE_DEVICE_OFFSET;
190
191 data = __raw_readl(priv->base + reg);
192 *val = postprocess_read(data, where, size);
193
194 return PCIBIOS_SUCCESSFUL;
195 }
196
197 static int bcm6318_pcie_write(struct pci_bus *bus, unsigned int devfn,
198 int where, int size, u32 val)
199 {
200 struct bcm6318_pcie *priv = &bcm6318_pcie;
201 u32 data;
202 u32 reg = where & ~3;
203
204 if (!bcm6318_pcie_can_access(bus, devfn))
205 return PCIBIOS_DEVICE_NOT_FOUND;
206
207 if (bus->number == PCIE_BUS_DEVICE)
208 reg += PCIE_DEVICE_OFFSET;
209
210 data = __raw_readl(priv->base + reg);
211 data = preprocess_write(data, val, where, size);
212 __raw_writel(data, priv->base + reg);
213
214 return PCIBIOS_SUCCESSFUL;
215 }
216
217 static struct pci_ops bcm6318_pcie_ops = {
218 .read = bcm6318_pcie_read,
219 .write = bcm6318_pcie_write,
220 };
221
222 static struct resource bcm6318_pcie_io_resource;
223 static struct resource bcm6318_pcie_mem_resource;
224 static struct resource bcm6318_pcie_busn_resource;
225
226 static struct pci_controller bcm6318_pcie_controller = {
227 .pci_ops = &bcm6318_pcie_ops,
228 .io_resource = &bcm6318_pcie_io_resource,
229 .mem_resource = &bcm6318_pcie_mem_resource,
230 };
231
232 static void bcm6318_pcie_reset(struct bcm6318_pcie *priv)
233 {
234 u32 val;
235
236 reset_control_deassert(priv->reset_hard);
237
238 reset_control_assert(priv->reset);
239 reset_control_assert(priv->reset_core);
240 reset_control_assert(priv->reset_ext);
241 mdelay(10);
242
243 reset_control_deassert(priv->reset_ext);
244 mdelay(10);
245
246 reset_control_deassert(priv->reset);
247 mdelay(10);
248
249 val = __raw_readl(priv->base + PCIE_HARD_DEBUG_REG);
250 val &= ~HARD_DEBUG_SERDES_IDDQ;
251 __raw_writel(val, priv->base + PCIE_HARD_DEBUG_REG);
252 mdelay(10);
253
254 reset_control_deassert(priv->reset_core);
255 mdelay(200);
256 }
257
258 static void bcm6318_pcie_setup(struct bcm6318_pcie *priv)
259 {
260 u32 val;
261
262 __raw_writel(CPU_INT_PCIE_INTA | CPU_INT_PCIE_INTB |
263 CPU_INT_PCIE_INTC | CPU_INT_PCIE_INTD,
264 priv->base + PCIE_CPU_INT1_MASK_CLEAR_REG);
265
266 val = bcm6318_pcie_mem_resource.end & C2P_BASELIMIT_LIMIT_MASK;
267 val |= (bcm6318_pcie_mem_resource.start >> C2P_BASELIMIT_LIMIT_SHIFT)
268 << C2P_BASELIMIT_BASE_SHIFT;
269 __raw_writel(val, priv->base + PCIE_CPU_2_PCIE_MEM_WIN0_BASELIMIT_REG);
270
271 /* setup class code as bridge */
272 val = __raw_readl(priv->base + PCIE_IDVAL3_REG);
273 val &= ~IDVAL3_CLASS_CODE_MASK;
274 val |= (PCI_CLASS_BRIDGE_PCI << IDVAL3_SUBCLASS_SHIFT);
275 __raw_writel(val, priv->base + PCIE_IDVAL3_REG);
276
277 /* disable bar1 size */
278 val = __raw_readl(priv->base + PCIE_CONFIG2_REG);
279 val &= ~CONFIG2_BAR1_SIZE_MASK;
280 __raw_writel(val, priv->base + PCIE_CONFIG2_REG);
281
282 /* set bar0 to little endian */
283 val = __raw_readl(priv->base + PCIE_CPU_2_PCIE_MEM_WIN0_LO_REG);
284 val |= bcm6318_pcie_mem_resource.start & C2P_MEM_WIN_BASE_ADDR_MASK;
285 val |= C2P_MEM_WIN_ENDIAN_HALF_BYTE_SWAP;
286 __raw_writel(val, priv->base + PCIE_CPU_2_PCIE_MEM_WIN0_LO_REG);
287
288 __raw_writel(SPECIFIC_ENDIAN_MODE_BYTE_ALIGN,
289 priv->base + PCIE_SPECIFIC_REG);
290
291 __raw_writel(RC_BAR_CFG_LO_SIZE_256MB,
292 priv->base + PCIE_RC_BAR1_CONFIG_LO_REG);
293
294 __raw_writel(BAR1_CFG_REMAP_ACCESS_EN,
295 priv->base + PCIE_UBUS_BAR1_CFG_REMAP_REG);
296
297 __raw_writel(PCIE_BUS_DEVICE << EXT_CFG_BUS_NUM_SHIFT,
298 priv->base + PCIE_EXT_CFG_INDEX_REG);
299 }
300
301 static int bcm6318_pcie_probe(struct platform_device *pdev)
302 {
303 struct device *dev = &pdev->dev;
304 struct device_node *np = dev->of_node;
305 struct bcm6318_pcie *priv = &bcm6318_pcie;
306 struct resource *res;
307 int ret;
308 LIST_HEAD(resources);
309
310 of_pci_check_probe_only();
311
312 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313 priv->base = devm_ioremap_resource(dev, res);
314 if (IS_ERR(priv->base))
315 return PTR_ERR(priv->base);
316
317 priv->irq = platform_get_irq(pdev, 0);
318 if (!priv->irq)
319 return -ENODEV;
320
321 bmips_pci_irq = priv->irq;
322
323 priv->reset = devm_reset_control_get(dev, "pcie");
324 if (IS_ERR(priv->reset))
325 return PTR_ERR(priv->reset);
326
327 priv->reset_ext = devm_reset_control_get(dev, "pcie-ext");
328 if (IS_ERR(priv->reset_ext))
329 return PTR_ERR(priv->reset_ext);
330
331 priv->reset_core = devm_reset_control_get(dev, "pcie-core");
332 if (IS_ERR(priv->reset_core))
333 return PTR_ERR(priv->reset_core);
334
335 priv->reset_hard = devm_reset_control_get(dev, "pcie-hard");
336 if (IS_ERR(priv->reset_hard))
337 return PTR_ERR(priv->reset_hard);
338
339 priv->clk = devm_clk_get(dev, "pcie");
340 if (IS_ERR(priv->clk))
341 return PTR_ERR(priv->clk);
342
343 priv->clk25 = devm_clk_get(dev, "pcie25");
344 if (IS_ERR(priv->clk25))
345 return PTR_ERR(priv->clk25);
346
347 priv->clk_ubus = devm_clk_get(dev, "pcie-ubus");
348 if (IS_ERR(priv->clk_ubus))
349 return PTR_ERR(priv->clk_ubus);
350
351 ret = clk_prepare_enable(priv->clk);
352 if (ret) {
353 dev_err(dev, "could not enable clock\n");
354 return ret;
355 }
356
357 ret = clk_prepare_enable(priv->clk25);
358 if (ret) {
359 dev_err(dev, "could not enable clock\n");
360 return ret;
361 }
362
363 ret = clk_prepare_enable(priv->clk_ubus);
364 if (ret) {
365 dev_err(dev, "could not enable clock\n");
366 return ret;
367 }
368
369 pci_load_of_ranges(&bcm6318_pcie_controller, np);
370 if (!bcm6318_pcie_mem_resource.start)
371 return -EINVAL;
372
373 of_pci_parse_bus_range(np, &bcm6318_pcie_busn_resource);
374 pci_add_resource(&resources, &bcm6318_pcie_busn_resource);
375
376 bcm6318_pcie_reset(priv);
377 bcm6318_pcie_setup(priv);
378
379 register_pci_controller(&bcm6318_pcie_controller);
380
381 return 0;
382 }
383
384 static const struct of_device_id bcm6318_pcie_of_match[] = {
385 { .compatible = "brcm,bcm6318-pcie", },
386 { /* sentinel */ }
387 };
388
389 static struct platform_driver bcm6318_pcie_driver = {
390 .probe = bcm6318_pcie_probe,
391 .driver = {
392 .name = "bcm6318-pcie",
393 .of_match_table = bcm6318_pcie_of_match,
394 },
395 };
396
397 int __init bcm6318_pcie_init(void)
398 {
399 int ret = platform_driver_register(&bcm6318_pcie_driver);
400 if (ret)
401 pr_err("pci-bcm6318: Error registering platform driver!\n");
402 return ret;
403 }
404 late_initcall_sync(bcm6318_pcie_init);