]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/nlm/xlp_pci.c
MFV r314565,314567,314570:
[FreeBSD/FreeBSD.git] / sys / mips / nlm / xlp_pci.c
1 /*-
2  * Copyright (c) 2003-2012 Broadcom Corporation
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  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/rman.h>
41 #include <sys/pciio.h>
42
43 #include <machine/bus.h>
44 #include <machine/md_var.h>
45 #include <machine/intr_machdep.h>
46 #include <machine/cpuregs.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51
52 #include <dev/pci/pcivar.h>
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pci_private.h>
55
56 #include <dev/uart/uart.h>
57 #include <dev/uart/uart_bus.h>
58 #include <dev/uart/uart_cpu.h>
59
60 #include <dev/ofw/openfirm.h>
61 #include <dev/ofw/ofw_bus.h>
62 #include <dev/ofw/ofw_bus_subr.h>
63
64 #include <mips/nlm/hal/haldefs.h>
65 #include <mips/nlm/interrupt.h>
66 #include <mips/nlm/hal/iomap.h>
67 #include <mips/nlm/hal/mips-extns.h>
68 #include <mips/nlm/hal/pic.h>
69 #include <mips/nlm/hal/bridge.h>
70 #include <mips/nlm/hal/gbu.h>
71 #include <mips/nlm/hal/pcibus.h>
72 #include <mips/nlm/hal/uart.h>
73 #include <mips/nlm/xlp.h>
74
75 #include "pcib_if.h"
76 #include <dev/pci/pcib_private.h>
77 #include "pci_if.h"
78
79 static int
80 xlp_pci_attach(device_t dev)
81 {
82         struct pci_devinfo *dinfo;
83         device_t pcib;
84         int maxslots, s, f, pcifunchigh, irq;
85         int busno, node, devoffset;
86         uint16_t devid;
87         uint8_t hdrtype;
88
89         /*
90          * The on-chip devices are on a bus that is almost, but not
91          * quite, completely like PCI. Add those things by hand.
92          */
93         pcib = device_get_parent(dev);
94         busno = pcib_get_bus(dev);
95         maxslots = PCIB_MAXSLOTS(pcib);
96         for (s = 0; s <= maxslots; s++) {
97                 pcifunchigh = 0;
98                 f = 0;
99                 hdrtype = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_HDRTYPE, 1);
100                 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
101                         continue;
102                 if (hdrtype & PCIM_MFDEV)
103                         pcifunchigh = PCI_FUNCMAX;
104                 node = s / 8;
105                 for (f = 0; f <= pcifunchigh; f++) {
106                         devoffset = XLP_HDR_OFFSET(node, 0, s % 8, f);
107                         if (!nlm_dev_exists(devoffset))
108                                 continue;
109
110                         /* Find if there is a desc for the SoC device */
111                         devid = PCIB_READ_CONFIG(pcib, busno, s, f, PCIR_DEVICE, 2);
112
113                         /* Skip devices that don't have a proper PCI header */
114                         switch (devid) {
115                         case PCI_DEVICE_ID_NLM_ICI:
116                         case PCI_DEVICE_ID_NLM_PIC:
117                         case PCI_DEVICE_ID_NLM_FMN:
118                         case PCI_DEVICE_ID_NLM_UART:
119                         case PCI_DEVICE_ID_NLM_I2C:
120                         case PCI_DEVICE_ID_NLM_NOR:
121                         case PCI_DEVICE_ID_NLM_MMC:
122                                 continue;
123                         case PCI_DEVICE_ID_NLM_EHCI:
124                                 irq = PIC_USB_IRQ(f);
125                                 PCIB_WRITE_CONFIG(pcib, busno, s, f,
126                                     XLP_PCI_DEVSCRATCH_REG0 << 2,
127                                     (1 << 8) | irq, 4);
128                         }
129                         dinfo = pci_read_device(pcib, dev, pcib_get_domain(dev),
130                             busno, s, f);
131                         pci_add_child(dev, dinfo);
132                 }
133         }
134         return (bus_generic_attach(dev));
135 }
136
137 static int
138 xlp_pci_probe(device_t dev)
139 {
140         device_t pcib;
141
142         pcib = device_get_parent(dev);
143         /*
144          * Only the top level bus has SoC devices, leave the rest to
145          * Generic PCI code
146          */
147         if (strcmp(device_get_nameunit(pcib), "pcib0") != 0)
148                 return (ENXIO);
149         device_set_desc(dev, "XLP SoCbus");
150         return (BUS_PROBE_DEFAULT);
151 }
152
153 static devclass_t pci_devclass;
154 static device_method_t xlp_pci_methods[] = {
155         /* Device interface */
156         DEVMETHOD(device_probe,         xlp_pci_probe),
157         DEVMETHOD(device_attach,        xlp_pci_attach),
158         DEVMETHOD(bus_rescan,           bus_null_rescan),
159         DEVMETHOD_END
160 };
161
162 DEFINE_CLASS_1(pci, xlp_pci_driver, xlp_pci_methods, sizeof(struct pci_softc),
163     pci_driver);
164 DRIVER_MODULE(xlp_pci, pcib, xlp_pci_driver, pci_devclass, 0, 0);
165
166 static int
167 xlp_pcib_probe(device_t dev)
168 {
169
170         if (ofw_bus_is_compatible(dev, "netlogic,xlp-pci")) {
171                 device_set_desc(dev, "XLP PCI bus");
172                 return (BUS_PROBE_DEFAULT);
173         }
174         return (ENXIO);
175 }
176
177 static int
178 xlp_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
179 {
180
181         switch (which) {
182         case PCIB_IVAR_DOMAIN:
183                 *result = 0;
184                 return (0);
185         case PCIB_IVAR_BUS:
186                 *result = 0;
187                 return (0);
188         }
189         return (ENOENT);
190 }
191
192 static int
193 xlp_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t result)
194 {
195         switch (which) {
196         case PCIB_IVAR_DOMAIN:
197                 return (EINVAL);
198         case PCIB_IVAR_BUS:
199                 return (EINVAL);
200         }
201         return (ENOENT);
202 }
203
204 static int
205 xlp_pcib_maxslots(device_t dev)
206 {
207
208         return (PCI_SLOTMAX);
209 }
210
211 static u_int32_t
212 xlp_pcib_read_config(device_t dev, u_int b, u_int s, u_int f,
213     u_int reg, int width)
214 {
215         uint32_t data = 0;
216         uint64_t cfgaddr;
217         int     regindex = reg/sizeof(uint32_t);
218
219         cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
220         if ((width == 2) && (reg & 1))
221                 return 0xFFFFFFFF;
222         else if ((width == 4) && (reg & 3))
223                 return 0xFFFFFFFF;
224
225         /*
226          * The intline and int pin of SoC devices are DOA, except
227          * for bridges (slot %8 == 1).
228          * use the values we stashed in a writable PCI scratch reg.
229          */
230         if (b == 0 && regindex == 0xf && s % 8 > 1)
231                 regindex = XLP_PCI_DEVSCRATCH_REG0;
232
233         data = nlm_read_pci_reg(cfgaddr, regindex);
234         if (width == 1)
235                 return ((data >> ((reg & 3) << 3)) & 0xff);
236         else if (width == 2)
237                 return ((data >> ((reg & 3) << 3)) & 0xffff);
238         else
239                 return (data);
240 }
241
242 static void
243 xlp_pcib_write_config(device_t dev, u_int b, u_int s, u_int f,
244     u_int reg, u_int32_t val, int width)
245 {
246         uint64_t cfgaddr;
247         uint32_t data = 0;
248         int     regindex = reg / sizeof(uint32_t);
249
250         cfgaddr = nlm_pcicfg_base(XLP_HDR_OFFSET(0, b, s, f));
251         if ((width == 2) && (reg & 1))
252                 return;
253         else if ((width == 4) && (reg & 3))
254                 return;
255
256         if (width == 1) {
257                 data = nlm_read_pci_reg(cfgaddr, regindex);
258                 data = (data & ~(0xff << ((reg & 3) << 3))) |
259                     (val << ((reg & 3) << 3));
260         } else if (width == 2) {
261                 data = nlm_read_pci_reg(cfgaddr, regindex);
262                 data = (data & ~(0xffff << ((reg & 3) << 3))) |
263                     (val << ((reg & 3) << 3));
264         } else {
265                 data = val;
266         }
267
268         /*
269          * use shadow reg for intpin/intline which are dead
270          */
271         if (b == 0 && regindex == 0xf && s % 8 > 1)
272                 regindex = XLP_PCI_DEVSCRATCH_REG0;
273         nlm_write_pci_reg(cfgaddr, regindex, data);
274 }
275
276 /*
277  * Enable byte swap in hardware when compiled big-endian.
278  * Programs a link's PCIe SWAP regions from the link's IO and MEM address
279  * ranges.
280  */
281 static void
282 xlp_pcib_hardware_swap_enable(int node, int link)
283 {
284 #if BYTE_ORDER == BIG_ENDIAN
285         uint64_t bbase, linkpcibase;
286         uint32_t bar;
287         int pcieoffset;
288
289         pcieoffset = XLP_IO_PCIE_OFFSET(node, link);
290         if (!nlm_dev_exists(pcieoffset))
291                 return;
292
293         bbase = nlm_get_bridge_regbase(node);
294         linkpcibase = nlm_pcicfg_base(pcieoffset);
295         bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEMEM_BASE0 + link);
296         nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_MEM_BASE, bar);
297
298         bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEMEM_LIMIT0 + link);
299         nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_MEM_LIM, bar | 0xFFF);
300
301         bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEIO_BASE0 + link);
302         nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_IO_BASE, bar);
303
304         bar = nlm_read_bridge_reg(bbase, BRIDGE_PCIEIO_LIMIT0 + link);
305         nlm_write_pci_reg(linkpcibase, PCIE_BYTE_SWAP_IO_LIM, bar | 0xFFF);
306 #endif
307 }
308
309 static int
310 xlp_pcib_attach(device_t dev)
311 {
312         int node, link;
313
314         /* enable hardware swap on all nodes/links */
315         for (node = 0; node < XLP_MAX_NODES; node++)
316                 for (link = 0; link < 4; link++)
317                         xlp_pcib_hardware_swap_enable(node, link);
318
319         device_add_child(dev, "pci", -1);
320         bus_generic_attach(dev);
321         return (0);
322 }
323
324 /*
325  * XLS PCIe can have upto 4 links, and each link has its on IRQ
326  * Find the link on which the device is on
327  */
328 static int
329 xlp_pcie_link(device_t pcib, device_t dev)
330 {
331         device_t parent, tmp;
332
333         /* find the lane on which the slot is connected to */
334         tmp = dev;
335         while (1) {
336                 parent = device_get_parent(tmp);
337                 if (parent == NULL || parent == pcib) {
338                         device_printf(dev, "Cannot find parent bus\n");
339                         return (-1);
340                 }
341                 if (strcmp(device_get_nameunit(parent), "pci0") == 0)
342                         break;
343                 tmp = parent;
344         }
345         return (pci_get_function(tmp));
346 }
347
348 static int
349 xlp_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
350 {
351         int i, link;
352
353         /*
354          * Each link has 32 MSIs that can be allocated, but for now
355          * we only support one device per link.
356          * msi_alloc() equivalent is needed when we start supporting
357          * bridges on the PCIe link.
358          */
359         link = xlp_pcie_link(pcib, dev);
360         if (link == -1)
361                 return (ENXIO);
362
363         /*
364          * encode the irq so that we know it is a MSI interrupt when we
365          * setup interrupts
366          */
367         for (i = 0; i < count; i++)
368                 irqs[i] = 64 + link * 32 + i;
369
370         return (0);
371 }
372
373 static int
374 xlp_release_msi(device_t pcib, device_t dev, int count, int *irqs)
375 {
376         return (0);
377 }
378
379 static int
380 xlp_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
381     uint32_t *data)
382 {
383         int link;
384
385         if (irq < 64) {
386                 device_printf(dev, "%s: map_msi for irq %d  - ignored",
387                     device_get_nameunit(pcib), irq);
388                 return (ENXIO);
389         }
390         link = (irq - 64) / 32;
391         *addr = MIPS_MSI_ADDR(0);
392         *data = MIPS_MSI_DATA(PIC_PCIE_IRQ(link));
393         return (0);
394 }
395
396 static void
397 bridge_pcie_ack(int irq, void *arg)
398 {
399         uint32_t node,reg;
400         uint64_t base;
401
402         node = nlm_nodeid();
403         reg = PCIE_MSI_STATUS;
404
405         switch (irq) {
406                 case PIC_PCIE_0_IRQ:
407                         base = nlm_pcicfg_base(XLP_IO_PCIE0_OFFSET(node));
408                         break;
409                 case PIC_PCIE_1_IRQ:
410                         base = nlm_pcicfg_base(XLP_IO_PCIE1_OFFSET(node));
411                         break;
412                 case PIC_PCIE_2_IRQ:
413                         base = nlm_pcicfg_base(XLP_IO_PCIE2_OFFSET(node));
414                         break;
415                 case PIC_PCIE_3_IRQ:
416                         base = nlm_pcicfg_base(XLP_IO_PCIE3_OFFSET(node));
417                         break;
418                 default:
419                         return;
420         }
421
422         nlm_write_pci_reg(base, reg, 0xFFFFFFFF);
423         return;
424 }
425
426 static int
427 mips_platform_pcib_setup_intr(device_t dev, device_t child,
428     struct resource *irq, int flags, driver_filter_t *filt,
429     driver_intr_t *intr, void *arg, void **cookiep)
430 {
431         int error = 0;
432         int xlpirq;
433
434         error = rman_activate_resource(irq);
435         if (error)
436                 return error;
437         if (rman_get_start(irq) != rman_get_end(irq)) {
438                 device_printf(dev, "Interrupt allocation %ju != %ju\n",
439                     rman_get_start(irq), rman_get_end(irq));
440                 return (EINVAL);
441         }
442         xlpirq = rman_get_start(irq);
443         if (xlpirq == 0)
444                 return (0);
445
446         if (strcmp(device_get_name(dev), "pcib") != 0)
447                 return (0);
448
449         /*
450          * temporary hack for MSI, we support just one device per
451          * link, and assign the link interrupt to the device interrupt
452          */
453         if (xlpirq >= 64) {
454                 int node, val, link;
455                 uint64_t base;
456
457                 xlpirq -= 64;
458                 if (xlpirq % 32 != 0)
459                         return (0);
460
461                 node = nlm_nodeid();
462                 link = xlpirq / 32;
463                 base = nlm_pcicfg_base(XLP_IO_PCIE_OFFSET(node,link));
464
465                 /* MSI Interrupt Vector enable at bridge's configuration */
466                 nlm_write_pci_reg(base, PCIE_MSI_EN, PCIE_MSI_VECTOR_INT_EN);
467
468                 val = nlm_read_pci_reg(base, PCIE_INT_EN0);
469                 /* MSI Interrupt enable at bridge's configuration */
470                 nlm_write_pci_reg(base, PCIE_INT_EN0,
471                     (val | PCIE_MSI_INT_EN));
472
473                 /* legacy interrupt disable at bridge */
474                 val = nlm_read_pci_reg(base, PCIE_BRIDGE_CMD);
475                 nlm_write_pci_reg(base, PCIE_BRIDGE_CMD,
476                     (val | PCIM_CMD_INTxDIS));
477
478                 /* MSI address update at bridge */
479                 nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_ADDRL,
480                     MSI_MIPS_ADDR_BASE);
481                 nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_ADDRH, 0);
482
483                 val = nlm_read_pci_reg(base, PCIE_BRIDGE_MSI_CAP);
484                 /* MSI capability enable at bridge */
485                 nlm_write_pci_reg(base, PCIE_BRIDGE_MSI_CAP,
486                     (val | (PCIM_MSICTRL_MSI_ENABLE << 16) |
487                         (PCIM_MSICTRL_MMC_32 << 16)));
488                 xlpirq = PIC_PCIE_IRQ(link);
489         }
490
491         /* if it is for real PCIe, we need to ack at bridge too */
492         if (xlpirq >= PIC_PCIE_IRQ(0) && xlpirq <= PIC_PCIE_IRQ(3))
493                 xlp_set_bus_ack(xlpirq, bridge_pcie_ack, NULL);
494         cpu_establish_hardintr(device_get_name(child), filt, intr, arg,
495             xlpirq, flags, cookiep);
496
497         return (0);
498 }
499
500 static int
501 mips_platform_pcib_teardown_intr(device_t dev, device_t child,
502     struct resource *irq, void *cookie)
503 {
504         if (strcmp(device_get_name(child), "pci") == 0) {
505                 /* if needed reprogram the pic to clear pcix related entry */
506                 device_printf(dev, "teardown intr\n");
507         }
508         return (bus_generic_teardown_intr(dev, child, irq, cookie));
509 }
510
511 static int
512 mips_pcib_route_interrupt(device_t bus, device_t dev, int pin)
513 {
514         int f, d;
515
516         /*
517          * Validate requested pin number.
518          */
519         if ((pin < 1) || (pin > 4))
520                 return (255);
521
522         if (pci_get_bus(dev) == 0 &&
523             pci_get_vendor(dev) == PCI_VENDOR_NETLOGIC) {
524                 f = pci_get_function(dev);
525                 d = pci_get_slot(dev) % 8;
526
527                 /*
528                  * For PCIe links, return link IRT, for other SoC devices
529                  * get the IRT from its PCIe header
530                  */
531                 if (d == 1)
532                         return (PIC_PCIE_IRQ(f));
533                 else
534                         return (255);   /* use intline, don't reroute */
535         } else {
536                 /* Regular PCI devices */
537                 return (PIC_PCIE_IRQ(xlp_pcie_link(bus, dev)));
538         }
539 }
540
541 static device_method_t xlp_pcib_methods[] = {
542         /* Device interface */
543         DEVMETHOD(device_probe, xlp_pcib_probe),
544         DEVMETHOD(device_attach, xlp_pcib_attach),
545
546         /* Bus interface */
547         DEVMETHOD(bus_read_ivar, xlp_pcib_read_ivar),
548         DEVMETHOD(bus_write_ivar, xlp_pcib_write_ivar),
549         DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
550         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
551         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
552         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
553         DEVMETHOD(bus_setup_intr, mips_platform_pcib_setup_intr),
554         DEVMETHOD(bus_teardown_intr, mips_platform_pcib_teardown_intr),
555
556         /* pcib interface */
557         DEVMETHOD(pcib_maxslots, xlp_pcib_maxslots),
558         DEVMETHOD(pcib_read_config, xlp_pcib_read_config),
559         DEVMETHOD(pcib_write_config, xlp_pcib_write_config),
560         DEVMETHOD(pcib_route_interrupt, mips_pcib_route_interrupt),
561         DEVMETHOD(pcib_request_feature, pcib_request_feature_allow),
562
563         DEVMETHOD(pcib_alloc_msi, xlp_alloc_msi),
564         DEVMETHOD(pcib_release_msi, xlp_release_msi),
565         DEVMETHOD(pcib_map_msi, xlp_map_msi),
566
567         DEVMETHOD_END
568 };
569
570 static driver_t xlp_pcib_driver = {
571         "pcib",
572         xlp_pcib_methods,
573         1, /* no softc */
574 };
575
576 static devclass_t pcib_devclass;
577 DRIVER_MODULE(xlp_pcib, simplebus, xlp_pcib_driver, pcib_devclass, 0, 0);