1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Synopsys DesignWare PCIe host controller driver
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * https://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
9 */
10
11 #include <linux/delay.h>
12 #include <linux/of.h>
13 #include <linux/of_platform.h>
14 #include <linux/types.h>
15
16 #include "../../pci.h"
17 #include "pcie-designware.h"
18
19 /*
20 * These interfaces resemble the pci_find_*capability() interfaces, but these
21 * are for configuring host controllers, which are bridges *to* PCI devices but
22 * are not PCI devices themselves.
23 */
__dw_pcie_find_next_cap(struct dw_pcie * pci,u8 cap_ptr,u8 cap)24 static u8 __dw_pcie_find_next_cap(struct dw_pcie *pci, u8 cap_ptr,
25 u8 cap)
26 {
27 u8 cap_id, next_cap_ptr;
28 u16 reg;
29
30 if (!cap_ptr)
31 return 0;
32
33 reg = dw_pcie_readw_dbi(pci, cap_ptr);
34 cap_id = (reg & 0x00ff);
35
36 if (cap_id > PCI_CAP_ID_MAX)
37 return 0;
38
39 if (cap_id == cap)
40 return cap_ptr;
41
42 next_cap_ptr = (reg & 0xff00) >> 8;
43 return __dw_pcie_find_next_cap(pci, next_cap_ptr, cap);
44 }
45
dw_pcie_find_capability(struct dw_pcie * pci,u8 cap)46 u8 dw_pcie_find_capability(struct dw_pcie *pci, u8 cap)
47 {
48 u8 next_cap_ptr;
49 u16 reg;
50
51 reg = dw_pcie_readw_dbi(pci, PCI_CAPABILITY_LIST);
52 next_cap_ptr = (reg & 0x00ff);
53
54 return __dw_pcie_find_next_cap(pci, next_cap_ptr, cap);
55 }
56 EXPORT_SYMBOL_GPL(dw_pcie_find_capability);
57
dw_pcie_msi_capabilities(struct dw_pcie * pci)58 u16 dw_pcie_msi_capabilities(struct dw_pcie *pci)
59 {
60 u8 offset;
61
62 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
63 return dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
64 }
65
dw_pcie_find_next_ext_capability(struct dw_pcie * pci,u16 start,u8 cap)66 static u16 dw_pcie_find_next_ext_capability(struct dw_pcie *pci, u16 start,
67 u8 cap)
68 {
69 u32 header;
70 int ttl;
71 int pos = PCI_CFG_SPACE_SIZE;
72
73 /* minimum 8 bytes per capability */
74 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
75
76 if (start)
77 pos = start;
78
79 header = dw_pcie_readl_dbi(pci, pos);
80 /*
81 * If we have no capabilities, this is indicated by cap ID,
82 * cap version and next pointer all being 0.
83 */
84 if (header == 0)
85 return 0;
86
87 while (ttl-- > 0) {
88 if (PCI_EXT_CAP_ID(header) == cap && pos != start)
89 return pos;
90
91 pos = PCI_EXT_CAP_NEXT(header);
92 if (pos < PCI_CFG_SPACE_SIZE)
93 break;
94
95 header = dw_pcie_readl_dbi(pci, pos);
96 }
97
98 return 0;
99 }
100
dw_pcie_find_ext_capability(struct dw_pcie * pci,u8 cap)101 u16 dw_pcie_find_ext_capability(struct dw_pcie *pci, u8 cap)
102 {
103 return dw_pcie_find_next_ext_capability(pci, 0, cap);
104 }
105 EXPORT_SYMBOL_GPL(dw_pcie_find_ext_capability);
106
dw_pcie_read(void __iomem * addr,int size,u32 * val)107 int dw_pcie_read(void __iomem *addr, int size, u32 *val)
108 {
109 if (!IS_ALIGNED((uintptr_t)addr, size)) {
110 *val = 0;
111 return PCIBIOS_BAD_REGISTER_NUMBER;
112 }
113
114 if (size == 4) {
115 *val = readl(addr);
116 } else if (size == 2) {
117 *val = readw(addr);
118 } else if (size == 1) {
119 *val = readb(addr);
120 } else {
121 *val = 0;
122 return PCIBIOS_BAD_REGISTER_NUMBER;
123 }
124
125 return PCIBIOS_SUCCESSFUL;
126 }
127 EXPORT_SYMBOL_GPL(dw_pcie_read);
128
dw_pcie_write(void __iomem * addr,int size,u32 val)129 int dw_pcie_write(void __iomem *addr, int size, u32 val)
130 {
131 if (!IS_ALIGNED((uintptr_t)addr, size))
132 return PCIBIOS_BAD_REGISTER_NUMBER;
133
134 if (size == 4)
135 writel(val, addr);
136 else if (size == 2)
137 writew(val, addr);
138 else if (size == 1)
139 writeb(val, addr);
140 else
141 return PCIBIOS_BAD_REGISTER_NUMBER;
142
143 return PCIBIOS_SUCCESSFUL;
144 }
145 EXPORT_SYMBOL_GPL(dw_pcie_write);
146
dw_pcie_read_dbi(struct dw_pcie * pci,u32 reg,size_t size)147 u32 dw_pcie_read_dbi(struct dw_pcie *pci, u32 reg, size_t size)
148 {
149 int ret;
150 u32 val;
151
152 if (pci->ops && pci->ops->read_dbi)
153 return pci->ops->read_dbi(pci, pci->dbi_base, reg, size);
154
155 ret = dw_pcie_read(pci->dbi_base + reg, size, &val);
156 if (ret)
157 dev_err(pci->dev, "Read DBI address failed\n");
158
159 return val;
160 }
161 EXPORT_SYMBOL_GPL(dw_pcie_read_dbi);
162
dw_pcie_write_dbi(struct dw_pcie * pci,u32 reg,size_t size,u32 val)163 void dw_pcie_write_dbi(struct dw_pcie *pci, u32 reg, size_t size, u32 val)
164 {
165 int ret;
166
167 if (pci->ops && pci->ops->write_dbi) {
168 pci->ops->write_dbi(pci, pci->dbi_base, reg, size, val);
169 return;
170 }
171
172 ret = dw_pcie_write(pci->dbi_base + reg, size, val);
173 if (ret)
174 dev_err(pci->dev, "Write DBI address failed\n");
175 }
176 EXPORT_SYMBOL_GPL(dw_pcie_write_dbi);
177
dw_pcie_write_dbi2(struct dw_pcie * pci,u32 reg,size_t size,u32 val)178 void dw_pcie_write_dbi2(struct dw_pcie *pci, u32 reg, size_t size, u32 val)
179 {
180 int ret;
181
182 if (pci->ops && pci->ops->write_dbi2) {
183 pci->ops->write_dbi2(pci, pci->dbi_base2, reg, size, val);
184 return;
185 }
186
187 ret = dw_pcie_write(pci->dbi_base2 + reg, size, val);
188 if (ret)
189 dev_err(pci->dev, "write DBI address failed\n");
190 }
191
dw_pcie_readl_atu(struct dw_pcie * pci,u32 reg)192 static u32 dw_pcie_readl_atu(struct dw_pcie *pci, u32 reg)
193 {
194 int ret;
195 u32 val;
196
197 if (pci->ops && pci->ops->read_dbi)
198 return pci->ops->read_dbi(pci, pci->atu_base, reg, 4);
199
200 ret = dw_pcie_read(pci->atu_base + reg, 4, &val);
201 if (ret)
202 dev_err(pci->dev, "Read ATU address failed\n");
203
204 return val;
205 }
206
dw_pcie_writel_atu(struct dw_pcie * pci,u32 reg,u32 val)207 static void dw_pcie_writel_atu(struct dw_pcie *pci, u32 reg, u32 val)
208 {
209 int ret;
210
211 if (pci->ops && pci->ops->write_dbi) {
212 pci->ops->write_dbi(pci, pci->atu_base, reg, 4, val);
213 return;
214 }
215
216 ret = dw_pcie_write(pci->atu_base + reg, 4, val);
217 if (ret)
218 dev_err(pci->dev, "Write ATU address failed\n");
219 }
220
dw_pcie_readl_ob_unroll(struct dw_pcie * pci,u32 index,u32 reg)221 static u32 dw_pcie_readl_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg)
222 {
223 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
224
225 return dw_pcie_readl_atu(pci, offset + reg);
226 }
227
dw_pcie_writel_ob_unroll(struct dw_pcie * pci,u32 index,u32 reg,u32 val)228 static void dw_pcie_writel_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg,
229 u32 val)
230 {
231 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
232
233 dw_pcie_writel_atu(pci, offset + reg, val);
234 }
235
dw_pcie_enable_ecrc(u32 val)236 static inline u32 dw_pcie_enable_ecrc(u32 val)
237 {
238 /*
239 * DesignWare core version 4.90A has a design issue where the 'TD'
240 * bit in the Control register-1 of the ATU outbound region acts
241 * like an override for the ECRC setting, i.e., the presence of TLP
242 * Digest (ECRC) in the outgoing TLPs is solely determined by this
243 * bit. This is contrary to the PCIe spec which says that the
244 * enablement of the ECRC is solely determined by the AER
245 * registers.
246 *
247 * Because of this, even when the ECRC is enabled through AER
248 * registers, the transactions going through ATU won't have TLP
249 * Digest as there is no way the PCI core AER code could program
250 * the TD bit which is specific to the DesignWare core.
251 *
252 * The best way to handle this scenario is to program the TD bit
253 * always. It affects only the traffic from root port to downstream
254 * devices.
255 *
256 * At this point,
257 * When ECRC is enabled in AER registers, everything works normally
258 * When ECRC is NOT enabled in AER registers, then,
259 * on Root Port:- TLP Digest (DWord size) gets appended to each packet
260 * even through it is not required. Since downstream
261 * TLPs are mostly for configuration accesses and BAR
262 * accesses, they are not in critical path and won't
263 * have much negative effect on the performance.
264 * on End Point:- TLP Digest is received for some/all the packets coming
265 * from the root port. TLP Digest is ignored because,
266 * as per the PCIe Spec r5.0 v1.0 section 2.2.3
267 * "TLP Digest Rules", when an endpoint receives TLP
268 * Digest when its ECRC check functionality is disabled
269 * in AER registers, received TLP Digest is just ignored.
270 * Since there is no issue or error reported either side, best way to
271 * handle the scenario is to program TD bit by default.
272 */
273
274 return val | PCIE_ATU_TD;
275 }
276
dw_pcie_prog_outbound_atu_unroll(struct dw_pcie * pci,u8 func_no,int index,int type,u64 cpu_addr,u64 pci_addr,u64 size)277 static void dw_pcie_prog_outbound_atu_unroll(struct dw_pcie *pci, u8 func_no,
278 int index, int type,
279 u64 cpu_addr, u64 pci_addr,
280 u64 size)
281 {
282 u32 retries, val;
283 u64 limit_addr = cpu_addr + size - 1;
284
285 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
286 lower_32_bits(cpu_addr));
287 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
288 upper_32_bits(cpu_addr));
289 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_LIMIT,
290 lower_32_bits(limit_addr));
291 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_LIMIT,
292 upper_32_bits(limit_addr));
293 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
294 lower_32_bits(pci_addr));
295 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
296 upper_32_bits(pci_addr));
297 val = type | PCIE_ATU_FUNC_NUM(func_no);
298 if (upper_32_bits(limit_addr) > upper_32_bits(cpu_addr))
299 val |= PCIE_ATU_INCREASE_REGION_SIZE;
300 if (pci->version == 0x490A)
301 val = dw_pcie_enable_ecrc(val);
302 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, val);
303 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
304 PCIE_ATU_ENABLE);
305
306 /*
307 * Make sure ATU enable takes effect before any subsequent config
308 * and I/O accesses.
309 */
310 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
311 val = dw_pcie_readl_ob_unroll(pci, index,
312 PCIE_ATU_UNR_REGION_CTRL2);
313 if (val & PCIE_ATU_ENABLE)
314 return;
315
316 mdelay(LINK_WAIT_IATU);
317 }
318 dev_err(pci->dev, "Outbound iATU is not being enabled\n");
319 }
320
__dw_pcie_prog_outbound_atu(struct dw_pcie * pci,u8 func_no,int index,int type,u64 cpu_addr,u64 pci_addr,u64 size)321 static void __dw_pcie_prog_outbound_atu(struct dw_pcie *pci, u8 func_no,
322 int index, int type, u64 cpu_addr,
323 u64 pci_addr, u64 size)
324 {
325 u32 retries, val;
326 u64 limit_addr;
327
328 if (pci->ops && pci->ops->cpu_addr_fixup)
329 cpu_addr = pci->ops->cpu_addr_fixup(pci, cpu_addr);
330
331 if (pci->iatu_unroll_enabled) {
332 dw_pcie_prog_outbound_atu_unroll(pci, func_no, index, type,
333 cpu_addr, pci_addr, size);
334 return;
335 }
336
337 limit_addr = cpu_addr + size - 1;
338
339 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT,
340 PCIE_ATU_REGION_OUTBOUND | index);
341 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_BASE,
342 lower_32_bits(cpu_addr));
343 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_BASE,
344 upper_32_bits(cpu_addr));
345 dw_pcie_writel_dbi(pci, PCIE_ATU_LIMIT,
346 lower_32_bits(limit_addr));
347 if (pci->version >= 0x460A)
348 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_LIMIT,
349 upper_32_bits(limit_addr));
350 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET,
351 lower_32_bits(pci_addr));
352 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET,
353 upper_32_bits(pci_addr));
354 val = type | PCIE_ATU_FUNC_NUM(func_no);
355 if (upper_32_bits(limit_addr) > upper_32_bits(cpu_addr) &&
356 pci->version >= 0x460A)
357 val |= PCIE_ATU_INCREASE_REGION_SIZE;
358 if (pci->version == 0x490A)
359 val = dw_pcie_enable_ecrc(val);
360 dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, val);
361 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE);
362
363 /*
364 * Make sure ATU enable takes effect before any subsequent config
365 * and I/O accesses.
366 */
367 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
368 val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
369 if (val & PCIE_ATU_ENABLE)
370 return;
371
372 mdelay(LINK_WAIT_IATU);
373 }
374 dev_err(pci->dev, "Outbound iATU is not being enabled\n");
375 }
376
dw_pcie_prog_outbound_atu(struct dw_pcie * pci,int index,int type,u64 cpu_addr,u64 pci_addr,u64 size)377 void dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index, int type,
378 u64 cpu_addr, u64 pci_addr, u64 size)
379 {
380 __dw_pcie_prog_outbound_atu(pci, 0, index, type,
381 cpu_addr, pci_addr, size);
382 }
383
dw_pcie_prog_ep_outbound_atu(struct dw_pcie * pci,u8 func_no,int index,int type,u64 cpu_addr,u64 pci_addr,u64 size)384 void dw_pcie_prog_ep_outbound_atu(struct dw_pcie *pci, u8 func_no, int index,
385 int type, u64 cpu_addr, u64 pci_addr,
386 u64 size)
387 {
388 __dw_pcie_prog_outbound_atu(pci, func_no, index, type,
389 cpu_addr, pci_addr, size);
390 }
391
dw_pcie_readl_ib_unroll(struct dw_pcie * pci,u32 index,u32 reg)392 static u32 dw_pcie_readl_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg)
393 {
394 u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
395
396 return dw_pcie_readl_atu(pci, offset + reg);
397 }
398
dw_pcie_writel_ib_unroll(struct dw_pcie * pci,u32 index,u32 reg,u32 val)399 static void dw_pcie_writel_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg,
400 u32 val)
401 {
402 u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
403
404 dw_pcie_writel_atu(pci, offset + reg, val);
405 }
406
dw_pcie_prog_inbound_atu_unroll(struct dw_pcie * pci,u8 func_no,int index,int bar,u64 cpu_addr,enum dw_pcie_as_type as_type)407 static int dw_pcie_prog_inbound_atu_unroll(struct dw_pcie *pci, u8 func_no,
408 int index, int bar, u64 cpu_addr,
409 enum dw_pcie_as_type as_type)
410 {
411 int type;
412 u32 retries, val;
413
414 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
415 lower_32_bits(cpu_addr));
416 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
417 upper_32_bits(cpu_addr));
418
419 switch (as_type) {
420 case DW_PCIE_AS_MEM:
421 type = PCIE_ATU_TYPE_MEM;
422 break;
423 case DW_PCIE_AS_IO:
424 type = PCIE_ATU_TYPE_IO;
425 break;
426 default:
427 return -EINVAL;
428 }
429
430 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, type |
431 PCIE_ATU_FUNC_NUM(func_no));
432 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
433 PCIE_ATU_FUNC_NUM_MATCH_EN |
434 PCIE_ATU_ENABLE |
435 PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
436
437 /*
438 * Make sure ATU enable takes effect before any subsequent config
439 * and I/O accesses.
440 */
441 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
442 val = dw_pcie_readl_ib_unroll(pci, index,
443 PCIE_ATU_UNR_REGION_CTRL2);
444 if (val & PCIE_ATU_ENABLE)
445 return 0;
446
447 mdelay(LINK_WAIT_IATU);
448 }
449 dev_err(pci->dev, "Inbound iATU is not being enabled\n");
450
451 return -EBUSY;
452 }
453
dw_pcie_prog_inbound_atu(struct dw_pcie * pci,u8 func_no,int index,int bar,u64 cpu_addr,enum dw_pcie_as_type as_type)454 int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, u8 func_no, int index,
455 int bar, u64 cpu_addr,
456 enum dw_pcie_as_type as_type)
457 {
458 int type;
459 u32 retries, val;
460
461 if (pci->iatu_unroll_enabled)
462 return dw_pcie_prog_inbound_atu_unroll(pci, func_no, index, bar,
463 cpu_addr, as_type);
464
465 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_INBOUND |
466 index);
467 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, lower_32_bits(cpu_addr));
468 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET, upper_32_bits(cpu_addr));
469
470 switch (as_type) {
471 case DW_PCIE_AS_MEM:
472 type = PCIE_ATU_TYPE_MEM;
473 break;
474 case DW_PCIE_AS_IO:
475 type = PCIE_ATU_TYPE_IO;
476 break;
477 default:
478 return -EINVAL;
479 }
480
481 dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type |
482 PCIE_ATU_FUNC_NUM(func_no));
483 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE |
484 PCIE_ATU_FUNC_NUM_MATCH_EN |
485 PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
486
487 /*
488 * Make sure ATU enable takes effect before any subsequent config
489 * and I/O accesses.
490 */
491 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
492 val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
493 if (val & PCIE_ATU_ENABLE)
494 return 0;
495
496 mdelay(LINK_WAIT_IATU);
497 }
498 dev_err(pci->dev, "Inbound iATU is not being enabled\n");
499
500 return -EBUSY;
501 }
502
dw_pcie_disable_atu(struct dw_pcie * pci,int index,enum dw_pcie_region_type type)503 void dw_pcie_disable_atu(struct dw_pcie *pci, int index,
504 enum dw_pcie_region_type type)
505 {
506 u32 region;
507
508 switch (type) {
509 case DW_PCIE_REGION_INBOUND:
510 region = PCIE_ATU_REGION_INBOUND;
511 break;
512 case DW_PCIE_REGION_OUTBOUND:
513 region = PCIE_ATU_REGION_OUTBOUND;
514 break;
515 default:
516 return;
517 }
518
519 if (pci->iatu_unroll_enabled) {
520 if (region == PCIE_ATU_REGION_INBOUND) {
521 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
522 ~(u32)PCIE_ATU_ENABLE);
523 } else {
524 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
525 ~(u32)PCIE_ATU_ENABLE);
526 }
527 } else {
528 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, region | index);
529 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, ~(u32)PCIE_ATU_ENABLE);
530 }
531 }
532
dw_pcie_wait_for_link(struct dw_pcie * pci)533 int dw_pcie_wait_for_link(struct dw_pcie *pci)
534 {
535 int retries;
536
537 /* Check if the link is up or not */
538 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
539 if (dw_pcie_link_up(pci)) {
540 dev_info(pci->dev, "Link up\n");
541 return 0;
542 }
543 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
544 }
545
546 dev_info(pci->dev, "Phy link never came up\n");
547
548 return -ETIMEDOUT;
549 }
550 EXPORT_SYMBOL_GPL(dw_pcie_wait_for_link);
551
dw_pcie_link_up(struct dw_pcie * pci)552 int dw_pcie_link_up(struct dw_pcie *pci)
553 {
554 u32 val;
555
556 if (pci->ops && pci->ops->link_up)
557 return pci->ops->link_up(pci);
558
559 val = readl(pci->dbi_base + PCIE_PORT_DEBUG1);
560 return ((val & PCIE_PORT_DEBUG1_LINK_UP) &&
561 (!(val & PCIE_PORT_DEBUG1_LINK_IN_TRAINING)));
562 }
563 EXPORT_SYMBOL_GPL(dw_pcie_link_up);
564
dw_pcie_upconfig_setup(struct dw_pcie * pci)565 void dw_pcie_upconfig_setup(struct dw_pcie *pci)
566 {
567 u32 val;
568
569 val = dw_pcie_readl_dbi(pci, PCIE_PORT_MULTI_LANE_CTRL);
570 val |= PORT_MLTI_UPCFG_SUPPORT;
571 dw_pcie_writel_dbi(pci, PCIE_PORT_MULTI_LANE_CTRL, val);
572 }
573 EXPORT_SYMBOL_GPL(dw_pcie_upconfig_setup);
574
dw_pcie_link_set_max_speed(struct dw_pcie * pci,u32 link_gen)575 static void dw_pcie_link_set_max_speed(struct dw_pcie *pci, u32 link_gen)
576 {
577 u32 cap, ctrl2, link_speed;
578 u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
579
580 cap = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
581 ctrl2 = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCTL2);
582 ctrl2 &= ~PCI_EXP_LNKCTL2_TLS;
583
584 switch (pcie_link_speed[link_gen]) {
585 case PCIE_SPEED_2_5GT:
586 link_speed = PCI_EXP_LNKCTL2_TLS_2_5GT;
587 break;
588 case PCIE_SPEED_5_0GT:
589 link_speed = PCI_EXP_LNKCTL2_TLS_5_0GT;
590 break;
591 case PCIE_SPEED_8_0GT:
592 link_speed = PCI_EXP_LNKCTL2_TLS_8_0GT;
593 break;
594 case PCIE_SPEED_16_0GT:
595 link_speed = PCI_EXP_LNKCTL2_TLS_16_0GT;
596 break;
597 default:
598 /* Use hardware capability */
599 link_speed = FIELD_GET(PCI_EXP_LNKCAP_SLS, cap);
600 ctrl2 &= ~PCI_EXP_LNKCTL2_HASD;
601 break;
602 }
603
604 dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCTL2, ctrl2 | link_speed);
605
606 cap &= ~((u32)PCI_EXP_LNKCAP_SLS);
607 dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, cap | link_speed);
608
609 }
610
dw_pcie_iatu_unroll_enabled(struct dw_pcie * pci)611 static u8 dw_pcie_iatu_unroll_enabled(struct dw_pcie *pci)
612 {
613 u32 val;
614
615 val = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT);
616 if (val == 0xffffffff)
617 return 1;
618
619 return 0;
620 }
621
dw_pcie_iatu_detect_regions_unroll(struct dw_pcie * pci)622 static void dw_pcie_iatu_detect_regions_unroll(struct dw_pcie *pci)
623 {
624 int max_region, i, ob = 0, ib = 0;
625 u32 val;
626
627 max_region = min((int)pci->atu_size / 512, 256);
628
629 for (i = 0; i < max_region; i++) {
630 dw_pcie_writel_ob_unroll(pci, i, PCIE_ATU_UNR_LOWER_TARGET,
631 0x11110000);
632
633 val = dw_pcie_readl_ob_unroll(pci, i, PCIE_ATU_UNR_LOWER_TARGET);
634 if (val == 0x11110000)
635 ob++;
636 else
637 break;
638 }
639
640 for (i = 0; i < max_region; i++) {
641 dw_pcie_writel_ib_unroll(pci, i, PCIE_ATU_UNR_LOWER_TARGET,
642 0x11110000);
643
644 val = dw_pcie_readl_ib_unroll(pci, i, PCIE_ATU_UNR_LOWER_TARGET);
645 if (val == 0x11110000)
646 ib++;
647 else
648 break;
649 }
650 pci->num_ib_windows = ib;
651 pci->num_ob_windows = ob;
652 }
653
dw_pcie_iatu_detect_regions(struct dw_pcie * pci)654 static void dw_pcie_iatu_detect_regions(struct dw_pcie *pci)
655 {
656 int max_region, i, ob = 0, ib = 0;
657 u32 val;
658
659 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, 0xFF);
660 max_region = dw_pcie_readl_dbi(pci, PCIE_ATU_VIEWPORT) + 1;
661
662 for (i = 0; i < max_region; i++) {
663 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_OUTBOUND | i);
664 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, 0x11110000);
665 val = dw_pcie_readl_dbi(pci, PCIE_ATU_LOWER_TARGET);
666 if (val == 0x11110000)
667 ob++;
668 else
669 break;
670 }
671
672 for (i = 0; i < max_region; i++) {
673 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_INBOUND | i);
674 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, 0x11110000);
675 val = dw_pcie_readl_dbi(pci, PCIE_ATU_LOWER_TARGET);
676 if (val == 0x11110000)
677 ib++;
678 else
679 break;
680 }
681
682 pci->num_ib_windows = ib;
683 pci->num_ob_windows = ob;
684 }
685
dw_pcie_iatu_detect(struct dw_pcie * pci)686 void dw_pcie_iatu_detect(struct dw_pcie *pci)
687 {
688 struct device *dev = pci->dev;
689 struct platform_device *pdev = to_platform_device(dev);
690
691 if (pci->version >= 0x480A || (!pci->version &&
692 dw_pcie_iatu_unroll_enabled(pci))) {
693 pci->iatu_unroll_enabled = true;
694 if (!pci->atu_base) {
695 struct resource *res =
696 platform_get_resource_byname(pdev, IORESOURCE_MEM, "atu");
697 if (res) {
698 pci->atu_size = resource_size(res);
699 pci->atu_base = devm_ioremap_resource(dev, res);
700 }
701 if (!pci->atu_base || IS_ERR(pci->atu_base))
702 pci->atu_base = pci->dbi_base + DEFAULT_DBI_ATU_OFFSET;
703 }
704
705 if (!pci->atu_size)
706 /* Pick a minimal default, enough for 8 in and 8 out windows */
707 pci->atu_size = SZ_4K;
708
709 dw_pcie_iatu_detect_regions_unroll(pci);
710 } else
711 dw_pcie_iatu_detect_regions(pci);
712
713 dev_info(pci->dev, "iATU unroll: %s\n", pci->iatu_unroll_enabled ?
714 "enabled" : "disabled");
715
716 dev_info(pci->dev, "Detected iATU regions: %u outbound, %u inbound",
717 pci->num_ob_windows, pci->num_ib_windows);
718 }
719
dw_pcie_setup(struct dw_pcie * pci)720 void dw_pcie_setup(struct dw_pcie *pci)
721 {
722 u32 val;
723 struct device *dev = pci->dev;
724 struct device_node *np = dev->of_node;
725
726 if (pci->link_gen > 0)
727 dw_pcie_link_set_max_speed(pci, pci->link_gen);
728
729 /* Configure Gen1 N_FTS */
730 if (pci->n_fts[0]) {
731 val = dw_pcie_readl_dbi(pci, PCIE_PORT_AFR);
732 val &= ~(PORT_AFR_N_FTS_MASK | PORT_AFR_CC_N_FTS_MASK);
733 val |= PORT_AFR_N_FTS(pci->n_fts[0]);
734 val |= PORT_AFR_CC_N_FTS(pci->n_fts[0]);
735 dw_pcie_writel_dbi(pci, PCIE_PORT_AFR, val);
736 }
737
738 /* Configure Gen2+ N_FTS */
739 if (pci->n_fts[1]) {
740 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
741 val &= ~PORT_LOGIC_N_FTS_MASK;
742 val |= pci->n_fts[1];
743 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
744 }
745
746 val = dw_pcie_readl_dbi(pci, PCIE_PORT_LINK_CONTROL);
747 val &= ~PORT_LINK_FAST_LINK_MODE;
748 val |= PORT_LINK_DLL_LINK_EN;
749 dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val);
750
751 if (of_property_read_bool(np, "snps,enable-cdm-check")) {
752 val = dw_pcie_readl_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS);
753 val |= PCIE_PL_CHK_REG_CHK_REG_CONTINUOUS |
754 PCIE_PL_CHK_REG_CHK_REG_START;
755 dw_pcie_writel_dbi(pci, PCIE_PL_CHK_REG_CONTROL_STATUS, val);
756 }
757
758 of_property_read_u32(np, "num-lanes", &pci->num_lanes);
759 if (!pci->num_lanes) {
760 dev_dbg(pci->dev, "Using h/w default number of lanes\n");
761 return;
762 }
763
764 /* Set the number of lanes */
765 val &= ~PORT_LINK_FAST_LINK_MODE;
766 val &= ~PORT_LINK_MODE_MASK;
767 switch (pci->num_lanes) {
768 case 1:
769 val |= PORT_LINK_MODE_1_LANES;
770 break;
771 case 2:
772 val |= PORT_LINK_MODE_2_LANES;
773 break;
774 case 4:
775 val |= PORT_LINK_MODE_4_LANES;
776 break;
777 case 8:
778 val |= PORT_LINK_MODE_8_LANES;
779 break;
780 default:
781 dev_err(pci->dev, "num-lanes %u: invalid value\n", pci->num_lanes);
782 return;
783 }
784 dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val);
785
786 /* Set link width speed control register */
787 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
788 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
789 switch (pci->num_lanes) {
790 case 1:
791 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
792 break;
793 case 2:
794 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
795 break;
796 case 4:
797 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
798 break;
799 case 8:
800 val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
801 break;
802 }
803 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
804 }
805