]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/versatile/versatile_pci.c
MFC r257199, r257200, r257217:
[FreeBSD/stable/10.git] / sys / arm / versatile / versatile_pci.c
1 /*
2  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37 #include <sys/watchdog.h>
38 #include <machine/bus.h>
39 #include <machine/cpu.h>
40 #include <machine/intr.h>
41
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcireg.h>
44
45 #include <dev/pci/pcib_private.h>
46 #include "pcib_if.h"
47
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/openfirm.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52
53 #include <machine/bus.h>
54 #include <machine/fdt.h>
55
56 #include <arm/versatile/versatile_pci_bus_space.h>
57
58 #define MEM_SYS         0
59 #define MEM_CORE        1
60 #define MEM_BASE        2
61 #define MEM_CONF_BASE   3
62 #define MEM_REGIONS     4
63
64 #define SYS_PCICTL              0x00
65
66 #define PCI_CORE_IMAP0          0x00
67 #define PCI_CORE_IMAP1          0x04
68 #define PCI_CORE_IMAP2          0x08
69 #define PCI_CORE_SELFID         0x0C
70 #define PCI_CORE_SMAP0          0x10
71 #define PCI_CORE_SMAP1          0x14
72 #define PCI_CORE_SMAP2          0x18
73
74 #define VERSATILE_PCI_DEV       0x030010ee
75 #define VERSATILE_PCI_CLASS     0x0b400000
76
77 #define PCI_IO_WINDOW           0x44000000
78 #define PCI_IO_SIZE             0x0c000000
79 #define PCI_NPREFETCH_WINDOW    0x50000000
80 #define PCI_NPREFETCH_SIZE      0x10000000
81 #define PCI_PREFETCH_WINDOW     0x60000000
82 #define PCI_PREFETCH_SIZE       0x10000000
83
84 #define VERSATILE_PCI_IRQ_START 27
85 #define VERSATILE_PCI_IRQ_END   30
86
87 #ifdef DEBUG
88 #define dprintf(fmt, args...) do { printf("%s(): ", __func__);   \
89     printf(fmt,##args); } while (0)
90 #else
91 #define dprintf(fmt, args...)
92 #endif
93
94
95 #define versatile_pci_sys_read_4(reg)   \
96         bus_read_4(sc->mem_res[MEM_SYS], (reg))
97 #define versatile_pci_sys_write_4(reg, val)     \
98         bus_write_4(sc->mem_res[MEM_SYS], (reg), (val))
99
100 #define versatile_pci_core_read_4(reg)  \
101         bus_read_4(sc->mem_res[MEM_CORE], (reg))
102 #define versatile_pci_core_write_4(reg, val)    \
103         bus_write_4(sc->mem_res[MEM_CORE], (reg), (val))
104
105 #define versatile_pci_read_4(reg)       \
106         bus_read_4(sc->mem_res[MEM_BASE], (reg))
107 #define versatile_pci_write_4(reg, val) \
108         bus_write_4(sc->mem_res[MEM_BASE], (reg), (val))
109
110 #define versatile_pci_conf_read_4(reg)  \
111         bus_read_4(sc->mem_res[MEM_CONF_BASE], (reg))
112 #define versatile_pci_conf_write_4(reg, val)    \
113         bus_write_4(sc->mem_res[MEM_CONF_BASE], (reg), (val))
114 #define versatile_pci_conf_write_2(reg, val)    \
115         bus_write_2(sc->mem_res[MEM_CONF_BASE], (reg), (val))
116 #define versatile_pci_conf_write_1(reg, val)    \
117         bus_write_1(sc->mem_res[MEM_CONF_BASE], (reg), (val))
118
119 struct versatile_pci_softc {
120         struct resource*        mem_res[MEM_REGIONS];
121         struct resource*        irq_res;
122         void*                   intr_hl;
123
124         int                     pcib_slot;
125
126         /* Bus part */
127         int                     busno;
128         struct rman             io_rman;
129         struct rman             irq_rman;
130         struct rman             mem_rman;
131
132         struct mtx              mtx;
133 };
134
135 static struct resource_spec versatile_pci_mem_spec[] = {
136         { SYS_RES_MEMORY, 0, RF_ACTIVE },
137         { SYS_RES_MEMORY, 1, RF_ACTIVE },
138         { SYS_RES_MEMORY, 2, RF_ACTIVE },
139         { SYS_RES_MEMORY, 3, RF_ACTIVE },
140         { -1, 0, 0 }
141 };
142
143 static int
144 versatile_pci_probe(device_t dev)
145 {
146
147         if (ofw_bus_is_compatible(dev, "versatile,pci")) {
148                 device_set_desc(dev, "Versatile PCI controller");
149                 return (BUS_PROBE_DEFAULT);
150         }
151
152         return (ENXIO);
153 }
154
155 static int
156 versatile_pci_attach(device_t dev)
157 {
158         struct versatile_pci_softc *sc = device_get_softc(dev);
159         int err;
160         int slot;
161         uint32_t vendordev_id, class_id;
162         uint32_t val;
163
164         /* Request memory resources */
165         err = bus_alloc_resources(dev, versatile_pci_mem_spec,
166                 sc->mem_res);
167         if (err) {
168                 device_printf(dev, "Error: could not allocate memory resources\n");
169                 return (ENXIO);
170         }
171
172         /*
173          * Setup memory windows
174          */
175         versatile_pci_core_write_4(PCI_CORE_IMAP0, (PCI_IO_WINDOW >> 28));
176         versatile_pci_core_write_4(PCI_CORE_IMAP1, (PCI_NPREFETCH_WINDOW >> 28));
177         versatile_pci_core_write_4(PCI_CORE_IMAP2, (PCI_PREFETCH_WINDOW >> 28));
178
179         /*
180          * XXX: this is SDRAM offset >> 28
181          * Unused as of QEMU 1.5
182          */
183         versatile_pci_core_write_4(PCI_CORE_SMAP0, (PCI_IO_WINDOW >> 28));
184         versatile_pci_core_write_4(PCI_CORE_SMAP1, (PCI_NPREFETCH_WINDOW >> 28));
185         versatile_pci_core_write_4(PCI_CORE_SMAP2, (PCI_NPREFETCH_WINDOW >> 28));
186
187         versatile_pci_sys_write_4(SYS_PCICTL, 1);
188
189         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
190                 vendordev_id = versatile_pci_read_4((slot << 11) + PCIR_DEVVENDOR);
191                 class_id = versatile_pci_read_4((slot << 11) + PCIR_REVID);
192                 if ((vendordev_id == VERSATILE_PCI_DEV) &&
193                     (class_id == VERSATILE_PCI_CLASS))
194                         break;
195         }
196
197         if (slot == (PCI_SLOTMAX + 1)) {
198                 bus_release_resources(dev, versatile_pci_mem_spec,
199                     sc->mem_res);
200                 device_printf(dev, "Versatile PCI core not found\n");
201                 return (ENXIO);
202         }
203
204         sc->pcib_slot = slot;
205         device_printf(dev, "PCI core at slot #%d\n", slot);
206
207         versatile_pci_core_write_4(PCI_CORE_SELFID, slot);
208         val = versatile_pci_conf_read_4((slot << 11) + PCIR_COMMAND);
209         val |= (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN | PCIM_CMD_MWRICEN);
210         versatile_pci_conf_write_4((slot << 11) + PCIR_COMMAND, val);
211
212         /* Again SDRAM start >> 28  */
213         versatile_pci_write_4((slot << 11) + PCIR_BAR(0), 0);
214         versatile_pci_write_4((slot << 11) + PCIR_BAR(1), 0);
215         versatile_pci_write_4((slot << 11) + PCIR_BAR(2), 0);
216
217         /* Prepare resource managers */
218         sc->mem_rman.rm_type = RMAN_ARRAY;
219         sc->mem_rman.rm_descr = "versatile PCI memory window";
220         if (rman_init(&sc->mem_rman) != 0 || 
221             rman_manage_region(&sc->mem_rman, PCI_NPREFETCH_WINDOW, 
222                 PCI_NPREFETCH_WINDOW + PCI_NPREFETCH_SIZE - 1) != 0) {
223                 panic("versatile_pci_attach: failed to set up memory rman");
224         }
225
226         bootverbose = 1;
227         sc->io_rman.rm_type = RMAN_ARRAY;
228         sc->io_rman.rm_descr = "versatile PCI IO window";
229         if (rman_init(&sc->io_rman) != 0 || 
230             rman_manage_region(&sc->io_rman, PCI_IO_WINDOW, 
231                 PCI_IO_WINDOW + PCI_IO_SIZE - 1) != 0) {
232                 panic("versatile_pci_attach: failed to set up I/O rman");
233         }
234
235         sc->irq_rman.rm_type = RMAN_ARRAY;
236         sc->irq_rman.rm_descr = "versatile PCI IRQs";
237         if (rman_init(&sc->irq_rman) != 0 ||
238             rman_manage_region(&sc->irq_rman, VERSATILE_PCI_IRQ_START, 
239                 VERSATILE_PCI_IRQ_END) != 0) {
240                 panic("versatile_pci_attach: failed to set up IRQ rman");
241         }
242
243         mtx_init(&sc->mtx, device_get_nameunit(dev), "versatilepci",
244                         MTX_SPIN);
245
246         val = versatile_pci_conf_read_4((12 << 11) + PCIR_COMMAND);
247
248         for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
249                 vendordev_id = versatile_pci_read_4((slot << 11) + PCIR_DEVVENDOR);
250                 class_id = versatile_pci_read_4((slot << 11) + PCIR_REVID);
251
252                 if (slot == sc->pcib_slot)
253                         continue;
254
255                 if ((vendordev_id == 0xffffffff) &&
256                     (class_id == 0xffffffff))
257                         continue;
258
259                 val = versatile_pci_conf_read_4((slot << 11) + PCIR_COMMAND);
260                 val |= PCIM_CMD_MEMEN | PCIM_CMD_PORTEN;
261                 versatile_pci_conf_write_4((slot << 11) + PCIR_COMMAND, val);
262         }
263
264         device_add_child(dev, "pci", 0);
265         return (bus_generic_attach(dev));
266 }
267
268 static int
269 versatile_pci_read_ivar(device_t dev, device_t child, int which,
270     uintptr_t *result)
271 {
272         struct versatile_pci_softc *sc = device_get_softc(dev);
273
274         switch (which) {
275         case PCIB_IVAR_DOMAIN:
276                 *result = 0;
277                 return (0);
278         case PCIB_IVAR_BUS:
279                 *result = sc->busno;
280                 return (0);
281         }
282
283         return (ENOENT);
284 }
285
286 static int
287 versatile_pci_write_ivar(device_t dev, device_t child, int which,
288     uintptr_t result)
289 {
290         struct versatile_pci_softc * sc = device_get_softc(dev);
291
292         switch (which) {
293         case PCIB_IVAR_BUS:
294                 sc->busno = result;
295                 return (0);
296         }
297
298         return (ENOENT);
299 }
300
301 static struct resource *
302 versatile_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
303     u_long start, u_long end, u_long count, u_int flags)
304 {
305
306         struct versatile_pci_softc *sc = device_get_softc(bus);
307         struct resource *rv;
308         struct rman *rm;
309
310         dprintf("Alloc resources %d, %08lx..%08lx, %ld\n", type, start, end, count);
311
312         switch (type) {
313         case SYS_RES_IOPORT:
314                 rm = &sc->io_rman;
315                 break;
316         case SYS_RES_IRQ:
317                 rm = &sc->irq_rman;
318                 break;
319         case SYS_RES_MEMORY:
320                 rm = &sc->mem_rman;
321                 break;
322         default:
323                 return (NULL);
324         }
325
326         rv = rman_reserve_resource(rm, start, end, count, flags, child);
327
328         if (rv == NULL)
329                 return (NULL);
330
331         rman_set_rid(rv, *rid);
332
333         if (flags & RF_ACTIVE) {
334                 if (bus_activate_resource(child, type, *rid, rv)) {
335                         rman_release_resource(rv);
336                         return (NULL);
337                 }
338         }
339         return (rv);
340 }
341
342 static int
343 versatile_pci_activate_resource(device_t bus, device_t child, int type, int rid,
344     struct resource *r)
345 {
346         vm_offset_t vaddr;
347         int res;
348
349         switch(type) {
350         case SYS_RES_MEMORY:
351         case SYS_RES_IOPORT:
352                 vaddr = (vm_offset_t)pmap_mapdev(rman_get_start(r),
353                                 rman_get_size(r));
354                 rman_set_bushandle(r, vaddr);
355                 rman_set_bustag(r, versatile_bus_space_pcimem);
356                 res = rman_activate_resource(r);
357                 break;
358         case SYS_RES_IRQ:
359                 res = (BUS_ACTIVATE_RESOURCE(device_get_parent(bus),
360                     child, type, rid, r));
361                 break;
362         default:
363                 res = ENXIO;
364                 break;
365         }
366
367         return (res);
368 }
369
370 static int
371 versatile_pci_setup_intr(device_t bus, device_t child, struct resource *ires,
372             int flags, driver_filter_t *filt, driver_intr_t *handler,
373             void *arg, void **cookiep)
374 {
375
376         return BUS_SETUP_INTR(device_get_parent(bus), bus, ires, flags,
377             filt, handler, arg, cookiep);
378 }
379
380 static int
381 versatile_pci_teardown_intr(device_t dev, device_t child, struct resource *ires,
382     void *cookie)
383 {
384
385         return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, ires, cookie);
386 }
387
388
389
390 static int
391 versatile_pci_maxslots(device_t dev)
392 {
393
394         return (PCI_SLOTMAX);
395 }
396
397 static int
398 versatile_pci_route_interrupt(device_t pcib, device_t device, int pin)
399 {
400
401         return (27 + ((pci_get_slot(device) + pin - 1) & 3));
402 }
403
404 static uint32_t
405 versatile_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func,
406     u_int reg, int bytes)
407 {
408         struct versatile_pci_softc *sc = device_get_softc(dev);
409         uint32_t data;
410         uint32_t shift, mask;
411         uint32_t addr;
412
413         if (sc->pcib_slot == slot) {
414                 switch (bytes) {
415                         case 4: 
416                                 return (0xffffffff);
417                                 break;
418                         case 2:
419                                 return (0xffff);
420                                 break;
421                         case 1:
422                                 return (0xff);
423                                 break;
424                 }
425         }
426
427         addr = (bus << 16) | (slot << 11) | (func << 8) | (reg & ~3);
428
429         /* register access is 32-bit aligned */
430         shift = (reg & 3) * 8;
431
432         /* Create a mask based on the width, post-shift */
433         if (bytes == 2)
434                 mask = 0xffff;
435         else if (bytes == 1)
436                 mask = 0xff;
437         else
438                 mask = 0xffffffff;
439
440         dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot, 
441             func, reg, bytes);
442
443         mtx_lock_spin(&sc->mtx);
444         data = versatile_pci_conf_read_4(addr);
445         mtx_unlock_spin(&sc->mtx);
446
447         /* get request bytes from 32-bit word */
448         data = (data >> shift) & mask;
449
450         dprintf("%s: read 0x%x\n", __func__, data);
451
452         return (data);
453 }
454
455 static void
456 versatile_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func,
457     u_int reg, uint32_t data, int bytes)
458 {
459
460         struct versatile_pci_softc *sc = device_get_softc(dev);
461         uint32_t addr;
462
463         dprintf("%s: tag (%x, %x, %x) reg %d(%d)\n", __func__, bus, slot,
464             func, reg, bytes);
465
466         if (sc->pcib_slot == slot)
467                 return;
468
469         addr = (bus << 16) | (slot << 11) | (func << 8) | reg;
470         mtx_lock_spin(&sc->mtx);
471         switch (bytes) {
472                 case 4: 
473                         versatile_pci_conf_write_4(addr, data);
474                         break;
475                 case 2:
476                         versatile_pci_conf_write_2(addr, data);
477                         break;
478                 case 1:
479                         versatile_pci_conf_write_1(addr, data);
480                         break;
481         }
482         mtx_unlock_spin(&sc->mtx);
483 }
484
485 static device_method_t versatile_pci_methods[] = {
486         DEVMETHOD(device_probe,         versatile_pci_probe),
487         DEVMETHOD(device_attach,        versatile_pci_attach),
488
489         /* Bus interface */
490         DEVMETHOD(bus_read_ivar,        versatile_pci_read_ivar),
491         DEVMETHOD(bus_write_ivar,       versatile_pci_write_ivar),
492         DEVMETHOD(bus_alloc_resource,   versatile_pci_alloc_resource),
493         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
494         DEVMETHOD(bus_activate_resource, versatile_pci_activate_resource),
495         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
496         DEVMETHOD(bus_setup_intr,       versatile_pci_setup_intr),
497         DEVMETHOD(bus_teardown_intr,    versatile_pci_teardown_intr),
498
499         /* pcib interface */
500         DEVMETHOD(pcib_maxslots,        versatile_pci_maxslots),
501         DEVMETHOD(pcib_read_config,     versatile_pci_read_config),
502         DEVMETHOD(pcib_write_config,    versatile_pci_write_config),
503         DEVMETHOD(pcib_route_interrupt, versatile_pci_route_interrupt),
504
505         DEVMETHOD_END
506 };
507
508 static driver_t versatile_pci_driver = {
509         "pcib",
510         versatile_pci_methods,
511         sizeof(struct versatile_pci_softc),
512 };
513
514 static devclass_t versatile_pci_devclass;
515
516 DRIVER_MODULE(versatile_pci, simplebus, versatile_pci_driver, versatile_pci_devclass, 0, 0);