blob: 0e03af279259b735ade878bf96be3963270c1b15 [file] [log] [blame]
Jingoo Han340cba62013-06-21 16:24:54 +09001/*
Jingoo Han4b1ced82013-07-31 17:14:10 +09002 * Synopsys Designware PCIe host controller driver
Jingoo Han340cba62013-06-21 16:24:54 +09003 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
7 * Author: Jingoo Han <jg1.han@samsung.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
Joao Pinto886bc5c2016-03-10 14:44:35 -060014#include <linux/delay.h>
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +053015#include <linux/of.h>
16#include <linux/types.h>
Jingoo Han340cba62013-06-21 16:24:54 +090017
Jingoo Han4b1ced82013-07-31 17:14:10 +090018#include "pcie-designware.h"
Jingoo Han340cba62013-06-21 16:24:54 +090019
Joao Pintodac29e62016-03-10 14:44:44 -060020/* PCIe Port Logic registers */
21#define PLR_OFFSET 0x700
22#define PCIE_PHY_DEBUG_R1 (PLR_OFFSET + 0x2c)
Jisheng Zhang01c07672016-08-17 15:57:37 -050023#define PCIE_PHY_DEBUG_R1_LINK_UP (0x1 << 4)
24#define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (0x1 << 29)
Joao Pintodac29e62016-03-10 14:44:44 -060025
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +053026int dw_pcie_read(void __iomem *addr, int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +090027{
Gabriele Paolonib6b18f52015-10-08 14:27:53 -050028 if ((uintptr_t)addr & (size - 1)) {
29 *val = 0;
30 return PCIBIOS_BAD_REGISTER_NUMBER;
31 }
32
Kishon Vijay Abraham I314fc852017-02-15 18:48:16 +053033 if (size == 4) {
Gabriele Paolonic003ca92015-10-08 14:27:43 -050034 *val = readl(addr);
Kishon Vijay Abraham I314fc852017-02-15 18:48:16 +053035 } else if (size == 2) {
Gabriele Paoloni4c458522015-10-08 14:27:48 -050036 *val = readw(addr);
Kishon Vijay Abraham I314fc852017-02-15 18:48:16 +053037 } else if (size == 1) {
Gabriele Paoloni4c458522015-10-08 14:27:48 -050038 *val = readb(addr);
Kishon Vijay Abraham I314fc852017-02-15 18:48:16 +053039 } else {
Gabriele Paolonic003ca92015-10-08 14:27:43 -050040 *val = 0;
Jingoo Han340cba62013-06-21 16:24:54 +090041 return PCIBIOS_BAD_REGISTER_NUMBER;
Gabriele Paolonic003ca92015-10-08 14:27:43 -050042 }
Jingoo Han340cba62013-06-21 16:24:54 +090043
44 return PCIBIOS_SUCCESSFUL;
45}
46
Kishon Vijay Abraham I19ce01cc2017-02-15 18:48:12 +053047int dw_pcie_write(void __iomem *addr, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +090048{
Gabriele Paolonib6b18f52015-10-08 14:27:53 -050049 if ((uintptr_t)addr & (size - 1))
50 return PCIBIOS_BAD_REGISTER_NUMBER;
51
Jingoo Han340cba62013-06-21 16:24:54 +090052 if (size == 4)
53 writel(val, addr);
54 else if (size == 2)
Gabriele Paoloni4c458522015-10-08 14:27:48 -050055 writew(val, addr);
Jingoo Han340cba62013-06-21 16:24:54 +090056 else if (size == 1)
Gabriele Paoloni4c458522015-10-08 14:27:48 -050057 writeb(val, addr);
Jingoo Han340cba62013-06-21 16:24:54 +090058 else
59 return PCIBIOS_BAD_REGISTER_NUMBER;
60
61 return PCIBIOS_SUCCESSFUL;
62}
63
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +053064u32 __dw_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
65 size_t size)
Jingoo Han340cba62013-06-21 16:24:54 +090066{
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +053067 int ret;
68 u32 val;
Bjorn Helgaas446fc232016-08-17 14:17:58 -050069
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +053070 if (pci->ops->read_dbi)
71 return pci->ops->read_dbi(pci, base, reg, size);
72
73 ret = dw_pcie_read(base + reg, size, &val);
74 if (ret)
75 dev_err(pci->dev, "read DBI address failed\n");
76
77 return val;
Jingoo Han340cba62013-06-21 16:24:54 +090078}
79
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +053080void __dw_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base, u32 reg,
81 size_t size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +090082{
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +053083 int ret;
84
85 if (pci->ops->write_dbi) {
86 pci->ops->write_dbi(pci, base, reg, size, val);
87 return;
88 }
89
90 ret = dw_pcie_write(base + reg, size, val);
91 if (ret)
92 dev_err(pci->dev, "write DBI address failed\n");
Jingoo Han340cba62013-06-21 16:24:54 +090093}
94
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +053095static u32 dw_pcie_readl_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg)
Joao Pintoa0601a42016-08-10 11:02:39 +010096{
97 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
98
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053099 return dw_pcie_readl_dbi(pci, offset + reg);
Joao Pintoa0601a42016-08-10 11:02:39 +0100100}
101
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530102static void dw_pcie_writel_ob_unroll(struct dw_pcie *pci, u32 index, u32 reg,
103 u32 val)
Joao Pintoa0601a42016-08-10 11:02:39 +0100104{
105 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
106
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530107 dw_pcie_writel_dbi(pci, offset + reg, val);
Joao Pintoa0601a42016-08-10 11:02:39 +0100108}
109
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530110void dw_pcie_prog_outbound_atu_unroll(struct dw_pcie *pci, int index, int type,
111 u64 cpu_addr, u64 pci_addr, u32 size)
112{
113 u32 retries, val;
114
115 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
116 lower_32_bits(cpu_addr));
117 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
118 upper_32_bits(cpu_addr));
119 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
120 lower_32_bits(cpu_addr + size - 1));
121 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
122 lower_32_bits(pci_addr));
123 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
124 upper_32_bits(pci_addr));
125 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
126 type);
127 dw_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
128 PCIE_ATU_ENABLE);
129
130 /*
131 * Make sure ATU enable takes effect before any subsequent config
132 * and I/O accesses.
133 */
134 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
135 val = dw_pcie_readl_ob_unroll(pci, index,
136 PCIE_ATU_UNR_REGION_CTRL2);
137 if (val & PCIE_ATU_ENABLE)
138 return;
139
140 usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
141 }
142 dev_err(pci->dev, "outbound iATU is not being enabled\n");
143}
144
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530145void dw_pcie_prog_outbound_atu(struct dw_pcie *pci, int index, int type,
146 u64 cpu_addr, u64 pci_addr, u32 size)
Jisheng Zhang63503c82015-04-30 16:22:28 +0800147{
Joao Pintod8bbeb32016-08-17 13:26:07 -0500148 u32 retries, val;
Stanimir Varbanov17209df2015-12-18 14:38:55 +0200149
Kishon Vijay Abraham Ia6600832017-03-13 19:13:22 +0530150 if (pci->ops->cpu_addr_fixup)
151 cpu_addr = pci->ops->cpu_addr_fixup(cpu_addr);
152
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530153 if (pci->iatu_unroll_enabled) {
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530154 dw_pcie_prog_outbound_atu_unroll(pci, index, type, cpu_addr,
155 pci_addr, size);
156 return;
Joao Pintoa0601a42016-08-10 11:02:39 +0100157 }
Stanimir Varbanov17209df2015-12-18 14:38:55 +0200158
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530159 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT,
160 PCIE_ATU_REGION_OUTBOUND | index);
161 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_BASE,
162 lower_32_bits(cpu_addr));
163 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_BASE,
164 upper_32_bits(cpu_addr));
165 dw_pcie_writel_dbi(pci, PCIE_ATU_LIMIT,
166 lower_32_bits(cpu_addr + size - 1));
167 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET,
168 lower_32_bits(pci_addr));
169 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET,
170 upper_32_bits(pci_addr));
171 dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type);
172 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE);
173
Stanimir Varbanov17209df2015-12-18 14:38:55 +0200174 /*
175 * Make sure ATU enable takes effect before any subsequent config
176 * and I/O accesses.
177 */
Joao Pintod8bbeb32016-08-17 13:26:07 -0500178 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530179 val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
Joao Pintod8bbeb32016-08-17 13:26:07 -0500180 if (val == PCIE_ATU_ENABLE)
181 return;
182
183 usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
184 }
Kishon Vijay Abraham Iedd45e32017-03-13 19:13:27 +0530185 dev_err(pci->dev, "outbound iATU is not being enabled\n");
Jisheng Zhang63503c82015-04-30 16:22:28 +0800186}
187
Kishon Vijay Abraham If8aed6e2017-03-27 15:15:05 +0530188static u32 dw_pcie_readl_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg)
189{
190 u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
191
192 return dw_pcie_readl_dbi(pci, offset + reg);
193}
194
195static void dw_pcie_writel_ib_unroll(struct dw_pcie *pci, u32 index, u32 reg,
196 u32 val)
197{
198 u32 offset = PCIE_GET_ATU_INB_UNR_REG_OFFSET(index);
199
200 dw_pcie_writel_dbi(pci, offset + reg, val);
201}
202
203int dw_pcie_prog_inbound_atu_unroll(struct dw_pcie *pci, int index, int bar,
204 u64 cpu_addr, enum dw_pcie_as_type as_type)
205{
206 int type;
207 u32 retries, val;
208
209 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
210 lower_32_bits(cpu_addr));
211 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
212 upper_32_bits(cpu_addr));
213
214 switch (as_type) {
215 case DW_PCIE_AS_MEM:
216 type = PCIE_ATU_TYPE_MEM;
217 break;
218 case DW_PCIE_AS_IO:
219 type = PCIE_ATU_TYPE_IO;
220 break;
221 default:
222 return -EINVAL;
223 }
224
225 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1, type);
226 dw_pcie_writel_ib_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
227 PCIE_ATU_ENABLE |
228 PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
229
230 /*
231 * Make sure ATU enable takes effect before any subsequent config
232 * and I/O accesses.
233 */
234 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
235 val = dw_pcie_readl_ib_unroll(pci, index,
236 PCIE_ATU_UNR_REGION_CTRL2);
237 if (val & PCIE_ATU_ENABLE)
238 return 0;
239
240 usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
241 }
242 dev_err(pci->dev, "inbound iATU is not being enabled\n");
243
244 return -EBUSY;
245}
246
247int dw_pcie_prog_inbound_atu(struct dw_pcie *pci, int index, int bar,
248 u64 cpu_addr, enum dw_pcie_as_type as_type)
249{
250 int type;
251 u32 retries, val;
252
253 if (pci->iatu_unroll_enabled)
254 return dw_pcie_prog_inbound_atu_unroll(pci, index, bar,
255 cpu_addr, as_type);
256
257 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, PCIE_ATU_REGION_INBOUND |
258 index);
259 dw_pcie_writel_dbi(pci, PCIE_ATU_LOWER_TARGET, lower_32_bits(cpu_addr));
260 dw_pcie_writel_dbi(pci, PCIE_ATU_UPPER_TARGET, upper_32_bits(cpu_addr));
261
262 switch (as_type) {
263 case DW_PCIE_AS_MEM:
264 type = PCIE_ATU_TYPE_MEM;
265 break;
266 case DW_PCIE_AS_IO:
267 type = PCIE_ATU_TYPE_IO;
268 break;
269 default:
270 return -EINVAL;
271 }
272
273 dw_pcie_writel_dbi(pci, PCIE_ATU_CR1, type);
274 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, PCIE_ATU_ENABLE
275 | PCIE_ATU_BAR_MODE_ENABLE | (bar << 8));
276
277 /*
278 * Make sure ATU enable takes effect before any subsequent config
279 * and I/O accesses.
280 */
281 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
282 val = dw_pcie_readl_dbi(pci, PCIE_ATU_CR2);
283 if (val & PCIE_ATU_ENABLE)
284 return 0;
285
286 usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
287 }
288 dev_err(pci->dev, "inbound iATU is not being enabled\n");
289
290 return -EBUSY;
291}
292
293void dw_pcie_disable_atu(struct dw_pcie *pci, int index,
294 enum dw_pcie_region_type type)
295{
296 int region;
297
298 switch (type) {
299 case DW_PCIE_REGION_INBOUND:
300 region = PCIE_ATU_REGION_INBOUND;
301 break;
302 case DW_PCIE_REGION_OUTBOUND:
303 region = PCIE_ATU_REGION_OUTBOUND;
304 break;
305 default:
306 return;
307 }
308
309 dw_pcie_writel_dbi(pci, PCIE_ATU_VIEWPORT, region | index);
310 dw_pcie_writel_dbi(pci, PCIE_ATU_CR2, ~PCIE_ATU_ENABLE);
311}
312
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530313int dw_pcie_wait_for_link(struct dw_pcie *pci)
Joao Pinto886bc5c2016-03-10 14:44:35 -0600314{
315 int retries;
316
317 /* check if the link is up or not */
318 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530319 if (dw_pcie_link_up(pci)) {
320 dev_info(pci->dev, "link up\n");
Joao Pinto886bc5c2016-03-10 14:44:35 -0600321 return 0;
322 }
323 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
324 }
325
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530326 dev_err(pci->dev, "phy link never came up\n");
Joao Pinto886bc5c2016-03-10 14:44:35 -0600327
328 return -ETIMEDOUT;
329}
330
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530331int dw_pcie_link_up(struct dw_pcie *pci)
Jingoo Han340cba62013-06-21 16:24:54 +0900332{
Joao Pintodac29e62016-03-10 14:44:44 -0600333 u32 val;
334
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530335 if (pci->ops->link_up)
336 return pci->ops->link_up(pci);
Bjorn Helgaas116a4892016-01-05 15:48:11 -0600337
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530338 val = readl(pci->dbi_base + PCIE_PHY_DEBUG_R1);
Jisheng Zhang01c07672016-08-17 15:57:37 -0500339 return ((val & PCIE_PHY_DEBUG_R1_LINK_UP) &&
340 (!(val & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING)));
Jingoo Han340cba62013-06-21 16:24:54 +0900341}
342
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530343void dw_pcie_setup(struct dw_pcie *pci)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900344{
Kishon Vijay Abraham I5f334db2017-02-15 18:48:15 +0530345 int ret;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900346 u32 val;
Kishon Vijay Abraham Ifeb85d92017-02-15 18:48:17 +0530347 u32 lanes;
Kishon Vijay Abraham I5f334db2017-02-15 18:48:15 +0530348 struct device *dev = pci->dev;
349 struct device_node *np = dev->of_node;
350
351 ret = of_property_read_u32(np, "num-lanes", &lanes);
352 if (ret)
353 lanes = 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900354
Mohit Kumar66c5c342014-04-14 14:22:54 -0600355 /* set the number of lanes */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530356 val = dw_pcie_readl_dbi(pci, PCIE_PORT_LINK_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900357 val &= ~PORT_LINK_MODE_MASK;
Kishon Vijay Abraham I5f334db2017-02-15 18:48:15 +0530358 switch (lanes) {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900359 case 1:
360 val |= PORT_LINK_MODE_1_LANES;
361 break;
362 case 2:
363 val |= PORT_LINK_MODE_2_LANES;
364 break;
365 case 4:
366 val |= PORT_LINK_MODE_4_LANES;
367 break;
Zhou Wang5b0f0732015-05-13 14:44:34 +0800368 case 8:
369 val |= PORT_LINK_MODE_8_LANES;
370 break;
Gabriele Paoloni907fce02015-09-29 00:03:10 +0800371 default:
Kishon Vijay Abraham I5f334db2017-02-15 18:48:15 +0530372 dev_err(pci->dev, "num-lanes %u: invalid value\n", lanes);
Gabriele Paoloni907fce02015-09-29 00:03:10 +0800373 return;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900374 }
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530375 dw_pcie_writel_dbi(pci, PCIE_PORT_LINK_CONTROL, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900376
377 /* set link width speed control register */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530378 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900379 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
Kishon Vijay Abraham I5f334db2017-02-15 18:48:15 +0530380 switch (lanes) {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900381 case 1:
382 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
383 break;
384 case 2:
385 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
386 break;
387 case 4:
388 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
389 break;
Zhou Wang5b0f0732015-05-13 14:44:34 +0800390 case 8:
391 val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
392 break;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900393 }
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530394 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900395}