]>
Commit | Line | Data |
---|---|---|
45361a4f TP |
1 | /* |
2 | * PCIe driver for Marvell Armada 370 and Armada XP SoCs | |
3 | * | |
4 | * This file is licensed under the terms of the GNU General Public | |
5 | * License version 2. This program is licensed "as is" without any | |
6 | * warranty of any kind, whether express or implied. | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/pci.h> | |
11 | #include <linux/clk.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/mbus.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/platform_device.h> | |
16 | #include <linux/of_address.h> | |
17 | #include <linux/of_pci.h> | |
18 | #include <linux/of_irq.h> | |
19 | #include <linux/of_platform.h> | |
20 | ||
21 | /* | |
22 | * PCIe unit register offsets. | |
23 | */ | |
24 | #define PCIE_DEV_ID_OFF 0x0000 | |
25 | #define PCIE_CMD_OFF 0x0004 | |
26 | #define PCIE_DEV_REV_OFF 0x0008 | |
27 | #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3)) | |
28 | #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3)) | |
29 | #define PCIE_HEADER_LOG_4_OFF 0x0128 | |
30 | #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4)) | |
31 | #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4)) | |
32 | #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4)) | |
33 | #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4)) | |
34 | #define PCIE_WIN5_CTRL_OFF 0x1880 | |
35 | #define PCIE_WIN5_BASE_OFF 0x1884 | |
36 | #define PCIE_WIN5_REMAP_OFF 0x188c | |
37 | #define PCIE_CONF_ADDR_OFF 0x18f8 | |
38 | #define PCIE_CONF_ADDR_EN 0x80000000 | |
39 | #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc)) | |
40 | #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16) | |
41 | #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11) | |
42 | #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8) | |
43 | #define PCIE_CONF_ADDR(bus, devfn, where) \ | |
44 | (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \ | |
45 | PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \ | |
46 | PCIE_CONF_ADDR_EN) | |
47 | #define PCIE_CONF_DATA_OFF 0x18fc | |
48 | #define PCIE_MASK_OFF 0x1910 | |
49 | #define PCIE_MASK_ENABLE_INTS 0x0f000000 | |
50 | #define PCIE_CTRL_OFF 0x1a00 | |
51 | #define PCIE_CTRL_X1_MODE 0x0001 | |
52 | #define PCIE_STAT_OFF 0x1a04 | |
53 | #define PCIE_STAT_BUS 0xff00 | |
f4ac9901 | 54 | #define PCIE_STAT_DEV 0x1f0000 |
45361a4f TP |
55 | #define PCIE_STAT_LINK_DOWN BIT(0) |
56 | #define PCIE_DEBUG_CTRL 0x1a60 | |
57 | #define PCIE_DEBUG_SOFT_RESET BIT(20) | |
58 | ||
59 | /* | |
60 | * This product ID is registered by Marvell, and used when the Marvell | |
61 | * SoC is not the root complex, but an endpoint on the PCIe bus. It is | |
62 | * therefore safe to re-use this PCI ID for our emulated PCI-to-PCI | |
63 | * bridge. | |
64 | */ | |
65 | #define MARVELL_EMULATED_PCI_PCI_BRIDGE_ID 0x7846 | |
66 | ||
67 | /* PCI configuration space of a PCI-to-PCI bridge */ | |
68 | struct mvebu_sw_pci_bridge { | |
69 | u16 vendor; | |
70 | u16 device; | |
71 | u16 command; | |
72 | u16 status; | |
73 | u16 class; | |
74 | u8 interface; | |
75 | u8 revision; | |
76 | u8 bist; | |
77 | u8 header_type; | |
78 | u8 latency_timer; | |
79 | u8 cache_line_size; | |
80 | u32 bar[2]; | |
81 | u8 primary_bus; | |
82 | u8 secondary_bus; | |
83 | u8 subordinate_bus; | |
84 | u8 secondary_latency_timer; | |
85 | u8 iobase; | |
86 | u8 iolimit; | |
87 | u16 secondary_status; | |
88 | u16 membase; | |
89 | u16 memlimit; | |
90 | u16 prefmembase; | |
91 | u16 prefmemlimit; | |
92 | u32 prefbaseupper; | |
93 | u32 preflimitupper; | |
94 | u16 iobaseupper; | |
95 | u16 iolimitupper; | |
96 | u8 cappointer; | |
97 | u8 reserved1; | |
98 | u16 reserved2; | |
99 | u32 romaddr; | |
100 | u8 intline; | |
101 | u8 intpin; | |
102 | u16 bridgectrl; | |
103 | }; | |
104 | ||
105 | struct mvebu_pcie_port; | |
106 | ||
107 | /* Structure representing all PCIe interfaces */ | |
108 | struct mvebu_pcie { | |
109 | struct platform_device *pdev; | |
110 | struct mvebu_pcie_port *ports; | |
111 | struct resource io; | |
112 | struct resource realio; | |
113 | struct resource mem; | |
114 | struct resource busn; | |
115 | int nports; | |
116 | }; | |
117 | ||
118 | /* Structure representing one PCIe interface */ | |
119 | struct mvebu_pcie_port { | |
120 | char *name; | |
121 | void __iomem *base; | |
122 | spinlock_t conf_lock; | |
123 | int haslink; | |
124 | u32 port; | |
125 | u32 lane; | |
126 | int devfn; | |
127 | struct clk *clk; | |
128 | struct mvebu_sw_pci_bridge bridge; | |
129 | struct device_node *dn; | |
130 | struct mvebu_pcie *pcie; | |
131 | phys_addr_t memwin_base; | |
132 | size_t memwin_size; | |
133 | phys_addr_t iowin_base; | |
134 | size_t iowin_size; | |
135 | }; | |
136 | ||
137 | static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port) | |
138 | { | |
139 | return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN); | |
140 | } | |
141 | ||
142 | static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr) | |
143 | { | |
144 | u32 stat; | |
145 | ||
146 | stat = readl(port->base + PCIE_STAT_OFF); | |
147 | stat &= ~PCIE_STAT_BUS; | |
148 | stat |= nr << 8; | |
149 | writel(stat, port->base + PCIE_STAT_OFF); | |
150 | } | |
151 | ||
f4ac9901 TP |
152 | static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr) |
153 | { | |
154 | u32 stat; | |
155 | ||
156 | stat = readl(port->base + PCIE_STAT_OFF); | |
157 | stat &= ~PCIE_STAT_DEV; | |
158 | stat |= nr << 16; | |
159 | writel(stat, port->base + PCIE_STAT_OFF); | |
160 | } | |
161 | ||
45361a4f TP |
162 | /* |
163 | * Setup PCIE BARs and Address Decode Wins: | |
164 | * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks | |
165 | * WIN[0-3] -> DRAM bank[0-3] | |
166 | */ | |
167 | static void __init mvebu_pcie_setup_wins(struct mvebu_pcie_port *port) | |
168 | { | |
169 | const struct mbus_dram_target_info *dram; | |
170 | u32 size; | |
171 | int i; | |
172 | ||
173 | dram = mv_mbus_dram_info(); | |
174 | ||
175 | /* First, disable and clear BARs and windows. */ | |
176 | for (i = 1; i < 3; i++) { | |
177 | writel(0, port->base + PCIE_BAR_CTRL_OFF(i)); | |
178 | writel(0, port->base + PCIE_BAR_LO_OFF(i)); | |
179 | writel(0, port->base + PCIE_BAR_HI_OFF(i)); | |
180 | } | |
181 | ||
182 | for (i = 0; i < 5; i++) { | |
183 | writel(0, port->base + PCIE_WIN04_CTRL_OFF(i)); | |
184 | writel(0, port->base + PCIE_WIN04_BASE_OFF(i)); | |
185 | writel(0, port->base + PCIE_WIN04_REMAP_OFF(i)); | |
186 | } | |
187 | ||
188 | writel(0, port->base + PCIE_WIN5_CTRL_OFF); | |
189 | writel(0, port->base + PCIE_WIN5_BASE_OFF); | |
190 | writel(0, port->base + PCIE_WIN5_REMAP_OFF); | |
191 | ||
192 | /* Setup windows for DDR banks. Count total DDR size on the fly. */ | |
193 | size = 0; | |
194 | for (i = 0; i < dram->num_cs; i++) { | |
195 | const struct mbus_dram_window *cs = dram->cs + i; | |
196 | ||
197 | writel(cs->base & 0xffff0000, | |
198 | port->base + PCIE_WIN04_BASE_OFF(i)); | |
199 | writel(0, port->base + PCIE_WIN04_REMAP_OFF(i)); | |
200 | writel(((cs->size - 1) & 0xffff0000) | | |
201 | (cs->mbus_attr << 8) | | |
202 | (dram->mbus_dram_target_id << 4) | 1, | |
203 | port->base + PCIE_WIN04_CTRL_OFF(i)); | |
204 | ||
205 | size += cs->size; | |
206 | } | |
207 | ||
208 | /* Round up 'size' to the nearest power of two. */ | |
209 | if ((size & (size - 1)) != 0) | |
210 | size = 1 << fls(size); | |
211 | ||
212 | /* Setup BAR[1] to all DRAM banks. */ | |
213 | writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1)); | |
214 | writel(0, port->base + PCIE_BAR_HI_OFF(1)); | |
215 | writel(((size - 1) & 0xffff0000) | 1, | |
216 | port->base + PCIE_BAR_CTRL_OFF(1)); | |
217 | } | |
218 | ||
219 | static void __init mvebu_pcie_setup_hw(struct mvebu_pcie_port *port) | |
220 | { | |
221 | u16 cmd; | |
222 | u32 mask; | |
223 | ||
224 | /* Point PCIe unit MBUS decode windows to DRAM space. */ | |
225 | mvebu_pcie_setup_wins(port); | |
226 | ||
227 | /* Master + slave enable. */ | |
228 | cmd = readw(port->base + PCIE_CMD_OFF); | |
229 | cmd |= PCI_COMMAND_IO; | |
230 | cmd |= PCI_COMMAND_MEMORY; | |
231 | cmd |= PCI_COMMAND_MASTER; | |
232 | writew(cmd, port->base + PCIE_CMD_OFF); | |
233 | ||
234 | /* Enable interrupt lines A-D. */ | |
235 | mask = readl(port->base + PCIE_MASK_OFF); | |
236 | mask |= PCIE_MASK_ENABLE_INTS; | |
237 | writel(mask, port->base + PCIE_MASK_OFF); | |
238 | } | |
239 | ||
240 | static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port, | |
241 | struct pci_bus *bus, | |
242 | u32 devfn, int where, int size, u32 *val) | |
243 | { | |
244 | writel(PCIE_CONF_ADDR(bus->number, devfn, where), | |
245 | port->base + PCIE_CONF_ADDR_OFF); | |
246 | ||
247 | *val = readl(port->base + PCIE_CONF_DATA_OFF); | |
248 | ||
249 | if (size == 1) | |
250 | *val = (*val >> (8 * (where & 3))) & 0xff; | |
251 | else if (size == 2) | |
252 | *val = (*val >> (8 * (where & 3))) & 0xffff; | |
253 | ||
254 | return PCIBIOS_SUCCESSFUL; | |
255 | } | |
256 | ||
257 | static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port, | |
258 | struct pci_bus *bus, | |
259 | u32 devfn, int where, int size, u32 val) | |
260 | { | |
261 | int ret = PCIBIOS_SUCCESSFUL; | |
262 | ||
263 | writel(PCIE_CONF_ADDR(bus->number, devfn, where), | |
264 | port->base + PCIE_CONF_ADDR_OFF); | |
265 | ||
266 | if (size == 4) | |
267 | writel(val, port->base + PCIE_CONF_DATA_OFF); | |
268 | else if (size == 2) | |
269 | writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3)); | |
270 | else if (size == 1) | |
271 | writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3)); | |
272 | else | |
273 | ret = PCIBIOS_BAD_REGISTER_NUMBER; | |
274 | ||
275 | return ret; | |
276 | } | |
277 | ||
278 | static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port) | |
279 | { | |
280 | phys_addr_t iobase; | |
281 | ||
282 | /* Are the new iobase/iolimit values invalid? */ | |
283 | if (port->bridge.iolimit < port->bridge.iobase || | |
284 | port->bridge.iolimitupper < port->bridge.iobaseupper) { | |
285 | ||
286 | /* If a window was configured, remove it */ | |
287 | if (port->iowin_base) { | |
288 | mvebu_mbus_del_window(port->iowin_base, | |
289 | port->iowin_size); | |
290 | port->iowin_base = 0; | |
291 | port->iowin_size = 0; | |
292 | } | |
293 | ||
294 | return; | |
295 | } | |
296 | ||
297 | /* | |
298 | * We read the PCI-to-PCI bridge emulated registers, and | |
299 | * calculate the base address and size of the address decoding | |
300 | * window to setup, according to the PCI-to-PCI bridge | |
301 | * specifications. iobase is the bus address, port->iowin_base | |
302 | * is the CPU address. | |
303 | */ | |
304 | iobase = ((port->bridge.iobase & 0xF0) << 8) | | |
305 | (port->bridge.iobaseupper << 16); | |
306 | port->iowin_base = port->pcie->io.start + iobase; | |
307 | port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) | | |
308 | (port->bridge.iolimitupper << 16)) - | |
309 | iobase); | |
310 | ||
311 | mvebu_mbus_add_window_remap_flags(port->name, port->iowin_base, | |
312 | port->iowin_size, | |
313 | iobase, | |
314 | MVEBU_MBUS_PCI_IO); | |
315 | ||
316 | pci_ioremap_io(iobase, port->iowin_base); | |
317 | } | |
318 | ||
319 | static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port) | |
320 | { | |
321 | /* Are the new membase/memlimit values invalid? */ | |
322 | if (port->bridge.memlimit < port->bridge.membase) { | |
323 | ||
324 | /* If a window was configured, remove it */ | |
325 | if (port->memwin_base) { | |
326 | mvebu_mbus_del_window(port->memwin_base, | |
327 | port->memwin_size); | |
328 | port->memwin_base = 0; | |
329 | port->memwin_size = 0; | |
330 | } | |
331 | ||
332 | return; | |
333 | } | |
334 | ||
335 | /* | |
336 | * We read the PCI-to-PCI bridge emulated registers, and | |
337 | * calculate the base address and size of the address decoding | |
338 | * window to setup, according to the PCI-to-PCI bridge | |
339 | * specifications. | |
340 | */ | |
341 | port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16); | |
342 | port->memwin_size = | |
343 | (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) - | |
344 | port->memwin_base; | |
345 | ||
346 | mvebu_mbus_add_window_remap_flags(port->name, port->memwin_base, | |
347 | port->memwin_size, | |
348 | MVEBU_MBUS_NO_REMAP, | |
349 | MVEBU_MBUS_PCI_MEM); | |
350 | } | |
351 | ||
352 | /* | |
353 | * Initialize the configuration space of the PCI-to-PCI bridge | |
354 | * associated with the given PCIe interface. | |
355 | */ | |
356 | static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port) | |
357 | { | |
358 | struct mvebu_sw_pci_bridge *bridge = &port->bridge; | |
359 | ||
360 | memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge)); | |
361 | ||
362 | bridge->status = PCI_STATUS_CAP_LIST; | |
363 | bridge->class = PCI_CLASS_BRIDGE_PCI; | |
364 | bridge->vendor = PCI_VENDOR_ID_MARVELL; | |
365 | bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID; | |
366 | bridge->header_type = PCI_HEADER_TYPE_BRIDGE; | |
367 | bridge->cache_line_size = 0x10; | |
368 | ||
369 | /* We support 32 bits I/O addressing */ | |
370 | bridge->iobase = PCI_IO_RANGE_TYPE_32; | |
371 | bridge->iolimit = PCI_IO_RANGE_TYPE_32; | |
372 | } | |
373 | ||
374 | /* | |
375 | * Read the configuration space of the PCI-to-PCI bridge associated to | |
376 | * the given PCIe interface. | |
377 | */ | |
378 | static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port, | |
379 | unsigned int where, int size, u32 *value) | |
380 | { | |
381 | struct mvebu_sw_pci_bridge *bridge = &port->bridge; | |
382 | ||
383 | switch (where & ~3) { | |
384 | case PCI_VENDOR_ID: | |
385 | *value = bridge->device << 16 | bridge->vendor; | |
386 | break; | |
387 | ||
388 | case PCI_COMMAND: | |
389 | *value = bridge->status << 16 | bridge->command; | |
390 | break; | |
391 | ||
392 | case PCI_CLASS_REVISION: | |
393 | *value = bridge->class << 16 | bridge->interface << 8 | | |
394 | bridge->revision; | |
395 | break; | |
396 | ||
397 | case PCI_CACHE_LINE_SIZE: | |
398 | *value = bridge->bist << 24 | bridge->header_type << 16 | | |
399 | bridge->latency_timer << 8 | bridge->cache_line_size; | |
400 | break; | |
401 | ||
402 | case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1: | |
403 | *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4]; | |
404 | break; | |
405 | ||
406 | case PCI_PRIMARY_BUS: | |
407 | *value = (bridge->secondary_latency_timer << 24 | | |
408 | bridge->subordinate_bus << 16 | | |
409 | bridge->secondary_bus << 8 | | |
410 | bridge->primary_bus); | |
411 | break; | |
412 | ||
413 | case PCI_IO_BASE: | |
414 | *value = (bridge->secondary_status << 16 | | |
415 | bridge->iolimit << 8 | | |
416 | bridge->iobase); | |
417 | break; | |
418 | ||
419 | case PCI_MEMORY_BASE: | |
420 | *value = (bridge->memlimit << 16 | bridge->membase); | |
421 | break; | |
422 | ||
423 | case PCI_PREF_MEMORY_BASE: | |
424 | *value = (bridge->prefmemlimit << 16 | bridge->prefmembase); | |
425 | break; | |
426 | ||
427 | case PCI_PREF_BASE_UPPER32: | |
428 | *value = bridge->prefbaseupper; | |
429 | break; | |
430 | ||
431 | case PCI_PREF_LIMIT_UPPER32: | |
432 | *value = bridge->preflimitupper; | |
433 | break; | |
434 | ||
435 | case PCI_IO_BASE_UPPER16: | |
436 | *value = (bridge->iolimitupper << 16 | bridge->iobaseupper); | |
437 | break; | |
438 | ||
439 | case PCI_ROM_ADDRESS1: | |
440 | *value = 0; | |
441 | break; | |
442 | ||
443 | default: | |
444 | *value = 0xffffffff; | |
445 | return PCIBIOS_BAD_REGISTER_NUMBER; | |
446 | } | |
447 | ||
448 | if (size == 2) | |
449 | *value = (*value >> (8 * (where & 3))) & 0xffff; | |
450 | else if (size == 1) | |
451 | *value = (*value >> (8 * (where & 3))) & 0xff; | |
452 | ||
453 | return PCIBIOS_SUCCESSFUL; | |
454 | } | |
455 | ||
456 | /* Write to the PCI-to-PCI bridge configuration space */ | |
457 | static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port, | |
458 | unsigned int where, int size, u32 value) | |
459 | { | |
460 | struct mvebu_sw_pci_bridge *bridge = &port->bridge; | |
461 | u32 mask, reg; | |
462 | int err; | |
463 | ||
464 | if (size == 4) | |
465 | mask = 0x0; | |
466 | else if (size == 2) | |
467 | mask = ~(0xffff << ((where & 3) * 8)); | |
468 | else if (size == 1) | |
469 | mask = ~(0xff << ((where & 3) * 8)); | |
470 | else | |
471 | return PCIBIOS_BAD_REGISTER_NUMBER; | |
472 | ||
473 | err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, ®); | |
474 | if (err) | |
475 | return err; | |
476 | ||
477 | value = (reg & mask) | value << ((where & 3) * 8); | |
478 | ||
479 | switch (where & ~3) { | |
480 | case PCI_COMMAND: | |
481 | bridge->command = value & 0xffff; | |
482 | bridge->status = value >> 16; | |
483 | break; | |
484 | ||
485 | case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1: | |
486 | bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value; | |
487 | break; | |
488 | ||
489 | case PCI_IO_BASE: | |
490 | /* | |
491 | * We also keep bit 1 set, it is a read-only bit that | |
492 | * indicates we support 32 bits addressing for the | |
493 | * I/O | |
494 | */ | |
495 | bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32; | |
496 | bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32; | |
497 | bridge->secondary_status = value >> 16; | |
498 | mvebu_pcie_handle_iobase_change(port); | |
499 | break; | |
500 | ||
501 | case PCI_MEMORY_BASE: | |
502 | bridge->membase = value & 0xffff; | |
503 | bridge->memlimit = value >> 16; | |
504 | mvebu_pcie_handle_membase_change(port); | |
505 | break; | |
506 | ||
507 | case PCI_PREF_MEMORY_BASE: | |
508 | bridge->prefmembase = value & 0xffff; | |
509 | bridge->prefmemlimit = value >> 16; | |
510 | break; | |
511 | ||
512 | case PCI_PREF_BASE_UPPER32: | |
513 | bridge->prefbaseupper = value; | |
514 | break; | |
515 | ||
516 | case PCI_PREF_LIMIT_UPPER32: | |
517 | bridge->preflimitupper = value; | |
518 | break; | |
519 | ||
520 | case PCI_IO_BASE_UPPER16: | |
521 | bridge->iobaseupper = value & 0xffff; | |
522 | bridge->iolimitupper = value >> 16; | |
523 | mvebu_pcie_handle_iobase_change(port); | |
524 | break; | |
525 | ||
526 | case PCI_PRIMARY_BUS: | |
527 | bridge->primary_bus = value & 0xff; | |
528 | bridge->secondary_bus = (value >> 8) & 0xff; | |
529 | bridge->subordinate_bus = (value >> 16) & 0xff; | |
530 | bridge->secondary_latency_timer = (value >> 24) & 0xff; | |
531 | mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus); | |
532 | break; | |
533 | ||
534 | default: | |
535 | break; | |
536 | } | |
537 | ||
538 | return PCIBIOS_SUCCESSFUL; | |
539 | } | |
540 | ||
541 | static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys) | |
542 | { | |
543 | return sys->private_data; | |
544 | } | |
545 | ||
546 | static struct mvebu_pcie_port * | |
547 | mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus, | |
548 | int devfn) | |
549 | { | |
550 | int i; | |
551 | ||
552 | for (i = 0; i < pcie->nports; i++) { | |
553 | struct mvebu_pcie_port *port = &pcie->ports[i]; | |
554 | if (bus->number == 0 && port->devfn == devfn) | |
555 | return port; | |
556 | if (bus->number != 0 && | |
197fc226 TP |
557 | bus->number >= port->bridge.secondary_bus && |
558 | bus->number <= port->bridge.subordinate_bus) | |
45361a4f TP |
559 | return port; |
560 | } | |
561 | ||
562 | return NULL; | |
563 | } | |
564 | ||
565 | /* PCI configuration space write function */ | |
566 | static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn, | |
567 | int where, int size, u32 val) | |
568 | { | |
569 | struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata); | |
570 | struct mvebu_pcie_port *port; | |
571 | unsigned long flags; | |
572 | int ret; | |
573 | ||
574 | port = mvebu_pcie_find_port(pcie, bus, devfn); | |
575 | if (!port) | |
576 | return PCIBIOS_DEVICE_NOT_FOUND; | |
577 | ||
578 | /* Access the emulated PCI-to-PCI bridge */ | |
579 | if (bus->number == 0) | |
580 | return mvebu_sw_pci_bridge_write(port, where, size, val); | |
581 | ||
197fc226 TP |
582 | if (!port->haslink) |
583 | return PCIBIOS_DEVICE_NOT_FOUND; | |
584 | ||
585 | /* | |
586 | * On the secondary bus, we don't want to expose any other | |
587 | * device than the device physically connected in the PCIe | |
588 | * slot, visible in slot 0. In slot 1, there's a special | |
589 | * Marvell device that only makes sense when the Armada is | |
590 | * used as a PCIe endpoint. | |
591 | */ | |
592 | if (bus->number == port->bridge.secondary_bus && | |
593 | PCI_SLOT(devfn) != 0) | |
45361a4f TP |
594 | return PCIBIOS_DEVICE_NOT_FOUND; |
595 | ||
596 | /* Access the real PCIe interface */ | |
597 | spin_lock_irqsave(&port->conf_lock, flags); | |
f4ac9901 | 598 | ret = mvebu_pcie_hw_wr_conf(port, bus, devfn, |
45361a4f TP |
599 | where, size, val); |
600 | spin_unlock_irqrestore(&port->conf_lock, flags); | |
601 | ||
602 | return ret; | |
603 | } | |
604 | ||
605 | /* PCI configuration space read function */ | |
606 | static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, | |
607 | int size, u32 *val) | |
608 | { | |
609 | struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata); | |
610 | struct mvebu_pcie_port *port; | |
611 | unsigned long flags; | |
612 | int ret; | |
613 | ||
614 | port = mvebu_pcie_find_port(pcie, bus, devfn); | |
615 | if (!port) { | |
616 | *val = 0xffffffff; | |
617 | return PCIBIOS_DEVICE_NOT_FOUND; | |
618 | } | |
619 | ||
620 | /* Access the emulated PCI-to-PCI bridge */ | |
621 | if (bus->number == 0) | |
622 | return mvebu_sw_pci_bridge_read(port, where, size, val); | |
623 | ||
197fc226 TP |
624 | if (!port->haslink) { |
625 | *val = 0xffffffff; | |
626 | return PCIBIOS_DEVICE_NOT_FOUND; | |
627 | } | |
628 | ||
629 | /* | |
630 | * On the secondary bus, we don't want to expose any other | |
631 | * device than the device physically connected in the PCIe | |
632 | * slot, visible in slot 0. In slot 1, there's a special | |
633 | * Marvell device that only makes sense when the Armada is | |
634 | * used as a PCIe endpoint. | |
635 | */ | |
636 | if (bus->number == port->bridge.secondary_bus && | |
637 | PCI_SLOT(devfn) != 0) { | |
45361a4f TP |
638 | *val = 0xffffffff; |
639 | return PCIBIOS_DEVICE_NOT_FOUND; | |
640 | } | |
641 | ||
642 | /* Access the real PCIe interface */ | |
643 | spin_lock_irqsave(&port->conf_lock, flags); | |
f4ac9901 | 644 | ret = mvebu_pcie_hw_rd_conf(port, bus, devfn, |
45361a4f TP |
645 | where, size, val); |
646 | spin_unlock_irqrestore(&port->conf_lock, flags); | |
647 | ||
648 | return ret; | |
649 | } | |
650 | ||
651 | static struct pci_ops mvebu_pcie_ops = { | |
652 | .read = mvebu_pcie_rd_conf, | |
653 | .write = mvebu_pcie_wr_conf, | |
654 | }; | |
655 | ||
656 | static int __init mvebu_pcie_setup(int nr, struct pci_sys_data *sys) | |
657 | { | |
658 | struct mvebu_pcie *pcie = sys_to_pcie(sys); | |
659 | int i; | |
660 | ||
661 | pci_add_resource_offset(&sys->resources, &pcie->realio, sys->io_offset); | |
662 | pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset); | |
663 | pci_add_resource(&sys->resources, &pcie->busn); | |
664 | ||
665 | for (i = 0; i < pcie->nports; i++) { | |
666 | struct mvebu_pcie_port *port = &pcie->ports[i]; | |
667 | mvebu_pcie_setup_hw(port); | |
668 | } | |
669 | ||
670 | return 1; | |
671 | } | |
672 | ||
673 | static int __init mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) | |
674 | { | |
675 | struct of_irq oirq; | |
676 | int ret; | |
677 | ||
678 | ret = of_irq_map_pci(dev, &oirq); | |
679 | if (ret) | |
680 | return ret; | |
681 | ||
682 | return irq_create_of_mapping(oirq.controller, oirq.specifier, | |
683 | oirq.size); | |
684 | } | |
685 | ||
686 | static struct pci_bus *mvebu_pcie_scan_bus(int nr, struct pci_sys_data *sys) | |
687 | { | |
688 | struct mvebu_pcie *pcie = sys_to_pcie(sys); | |
689 | struct pci_bus *bus; | |
690 | ||
691 | bus = pci_create_root_bus(&pcie->pdev->dev, sys->busnr, | |
692 | &mvebu_pcie_ops, sys, &sys->resources); | |
693 | if (!bus) | |
694 | return NULL; | |
695 | ||
696 | pci_scan_child_bus(bus); | |
697 | ||
698 | return bus; | |
699 | } | |
700 | ||
701 | resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev, | |
702 | const struct resource *res, | |
703 | resource_size_t start, | |
704 | resource_size_t size, | |
705 | resource_size_t align) | |
706 | { | |
707 | if (dev->bus->number != 0) | |
708 | return start; | |
709 | ||
710 | /* | |
711 | * On the PCI-to-PCI bridge side, the I/O windows must have at | |
712 | * least a 64 KB size and be aligned on their size, and the | |
713 | * memory windows must have at least a 1 MB size and be | |
714 | * aligned on their size | |
715 | */ | |
716 | if (res->flags & IORESOURCE_IO) | |
717 | return round_up(start, max((resource_size_t)SZ_64K, size)); | |
718 | else if (res->flags & IORESOURCE_MEM) | |
719 | return round_up(start, max((resource_size_t)SZ_1M, size)); | |
720 | else | |
721 | return start; | |
722 | } | |
723 | ||
724 | static void __init mvebu_pcie_enable(struct mvebu_pcie *pcie) | |
725 | { | |
726 | struct hw_pci hw; | |
727 | ||
728 | memset(&hw, 0, sizeof(hw)); | |
729 | ||
730 | hw.nr_controllers = 1; | |
731 | hw.private_data = (void **)&pcie; | |
732 | hw.setup = mvebu_pcie_setup; | |
733 | hw.scan = mvebu_pcie_scan_bus; | |
734 | hw.map_irq = mvebu_pcie_map_irq; | |
735 | hw.ops = &mvebu_pcie_ops; | |
736 | hw.align_resource = mvebu_pcie_align_resource; | |
737 | ||
738 | pci_common_init(&hw); | |
739 | } | |
740 | ||
741 | /* | |
742 | * Looks up the list of register addresses encoded into the reg = | |
743 | * <...> property for one that matches the given port/lane. Once | |
744 | * found, maps it. | |
745 | */ | |
746 | static void __iomem * __init | |
747 | mvebu_pcie_map_registers(struct platform_device *pdev, | |
748 | struct device_node *np, | |
749 | struct mvebu_pcie_port *port) | |
750 | { | |
751 | struct resource regs; | |
752 | int ret = 0; | |
753 | ||
754 | ret = of_address_to_resource(np, 0, ®s); | |
755 | if (ret) | |
756 | return NULL; | |
757 | ||
758 | return devm_request_and_ioremap(&pdev->dev, ®s); | |
759 | } | |
760 | ||
761 | static int __init mvebu_pcie_probe(struct platform_device *pdev) | |
762 | { | |
763 | struct mvebu_pcie *pcie; | |
764 | struct device_node *np = pdev->dev.of_node; | |
765 | struct of_pci_range range; | |
766 | struct of_pci_range_parser parser; | |
767 | struct device_node *child; | |
768 | int i, ret; | |
769 | ||
770 | pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie), | |
771 | GFP_KERNEL); | |
772 | if (!pcie) | |
773 | return -ENOMEM; | |
774 | ||
775 | pcie->pdev = pdev; | |
776 | ||
777 | if (of_pci_range_parser_init(&parser, np)) | |
778 | return -EINVAL; | |
779 | ||
780 | /* Get the I/O and memory ranges from DT */ | |
781 | for_each_of_pci_range(&parser, &range) { | |
782 | unsigned long restype = range.flags & IORESOURCE_TYPE_BITS; | |
783 | if (restype == IORESOURCE_IO) { | |
784 | of_pci_range_to_resource(&range, np, &pcie->io); | |
785 | of_pci_range_to_resource(&range, np, &pcie->realio); | |
786 | pcie->io.name = "I/O"; | |
787 | pcie->realio.start = max_t(resource_size_t, | |
788 | PCIBIOS_MIN_IO, | |
789 | range.pci_addr); | |
790 | pcie->realio.end = min_t(resource_size_t, | |
791 | IO_SPACE_LIMIT, | |
792 | range.pci_addr + range.size); | |
793 | } | |
794 | if (restype == IORESOURCE_MEM) { | |
795 | of_pci_range_to_resource(&range, np, &pcie->mem); | |
796 | pcie->mem.name = "MEM"; | |
797 | } | |
798 | } | |
799 | ||
800 | /* Get the bus range */ | |
801 | ret = of_pci_parse_bus_range(np, &pcie->busn); | |
802 | if (ret) { | |
803 | dev_err(&pdev->dev, "failed to parse bus-range property: %d\n", | |
804 | ret); | |
805 | return ret; | |
806 | } | |
807 | ||
808 | for_each_child_of_node(pdev->dev.of_node, child) { | |
809 | if (!of_device_is_available(child)) | |
810 | continue; | |
811 | pcie->nports++; | |
812 | } | |
813 | ||
814 | pcie->ports = devm_kzalloc(&pdev->dev, pcie->nports * | |
815 | sizeof(struct mvebu_pcie_port), | |
816 | GFP_KERNEL); | |
817 | if (!pcie->ports) | |
818 | return -ENOMEM; | |
819 | ||
820 | i = 0; | |
821 | for_each_child_of_node(pdev->dev.of_node, child) { | |
822 | struct mvebu_pcie_port *port = &pcie->ports[i]; | |
823 | ||
824 | if (!of_device_is_available(child)) | |
825 | continue; | |
826 | ||
827 | port->pcie = pcie; | |
828 | ||
829 | if (of_property_read_u32(child, "marvell,pcie-port", | |
830 | &port->port)) { | |
831 | dev_warn(&pdev->dev, | |
832 | "ignoring PCIe DT node, missing pcie-port property\n"); | |
833 | continue; | |
834 | } | |
835 | ||
836 | if (of_property_read_u32(child, "marvell,pcie-lane", | |
837 | &port->lane)) | |
838 | port->lane = 0; | |
839 | ||
840 | port->name = kasprintf(GFP_KERNEL, "pcie%d.%d", | |
841 | port->port, port->lane); | |
842 | ||
843 | port->devfn = of_pci_get_devfn(child); | |
844 | if (port->devfn < 0) | |
845 | continue; | |
846 | ||
847 | port->base = mvebu_pcie_map_registers(pdev, child, port); | |
848 | if (!port->base) { | |
849 | dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n", | |
850 | port->port, port->lane); | |
851 | continue; | |
852 | } | |
853 | ||
f4ac9901 TP |
854 | mvebu_pcie_set_local_dev_nr(port, 1); |
855 | ||
45361a4f TP |
856 | if (mvebu_pcie_link_up(port)) { |
857 | port->haslink = 1; | |
858 | dev_info(&pdev->dev, "PCIe%d.%d: link up\n", | |
859 | port->port, port->lane); | |
860 | } else { | |
861 | port->haslink = 0; | |
862 | dev_info(&pdev->dev, "PCIe%d.%d: link down\n", | |
863 | port->port, port->lane); | |
864 | } | |
865 | ||
866 | port->clk = of_clk_get_by_name(child, NULL); | |
3d9939c9 | 867 | if (IS_ERR(port->clk)) { |
45361a4f TP |
868 | dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n", |
869 | port->port, port->lane); | |
870 | iounmap(port->base); | |
871 | port->haslink = 0; | |
872 | continue; | |
873 | } | |
874 | ||
875 | port->dn = child; | |
876 | ||
877 | clk_prepare_enable(port->clk); | |
878 | spin_lock_init(&port->conf_lock); | |
879 | ||
880 | mvebu_sw_pci_bridge_init(port); | |
881 | ||
882 | i++; | |
883 | } | |
884 | ||
885 | mvebu_pcie_enable(pcie); | |
886 | ||
887 | return 0; | |
888 | } | |
889 | ||
890 | static const struct of_device_id mvebu_pcie_of_match_table[] = { | |
891 | { .compatible = "marvell,armada-xp-pcie", }, | |
892 | { .compatible = "marvell,armada-370-pcie", }, | |
893 | {}, | |
894 | }; | |
895 | MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table); | |
896 | ||
897 | static struct platform_driver mvebu_pcie_driver = { | |
898 | .driver = { | |
899 | .owner = THIS_MODULE, | |
900 | .name = "mvebu-pcie", | |
901 | .of_match_table = | |
902 | of_match_ptr(mvebu_pcie_of_match_table), | |
903 | }, | |
904 | }; | |
905 | ||
906 | static int __init mvebu_pcie_init(void) | |
907 | { | |
908 | return platform_driver_probe(&mvebu_pcie_driver, | |
909 | mvebu_pcie_probe); | |
910 | } | |
911 | ||
912 | subsys_initcall(mvebu_pcie_init); | |
913 | ||
914 | MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>"); | |
915 | MODULE_DESCRIPTION("Marvell EBU PCIe driver"); | |
916 | MODULE_LICENSE("GPLv2"); |