]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/broadcom/bcm2835/bcm2838_pci.c
MFV r365599: import fix for a libexecinfo warning at higher WARNS
[FreeBSD/FreeBSD.git] / sys / arm / broadcom / bcm2835 / bcm2838_pci.c
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2020 Dr Robert Harvey Crowston <crowston@protonmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  *
19  * $FreeBSD$
20  *
21  */
22
23 /*
24  * BCM2838-compatible PCI-express controller.
25  *
26  * Broadcom likes to give the same chip lots of different names. The name of
27  * this driver is taken from the Raspberry Pi 4 Broadcom 2838 chip.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/proc.h>
40 #include <sys/rman.h>
41 #include <sys/intr.h>
42 #include <sys/mutex.h>
43
44 #include <dev/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <dev/pci/pci_host_generic.h>
49 #include <dev/pci/pci_host_generic_fdt.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcib_private.h>
53
54 #include <machine/bus.h>
55 #include <machine/intr.h>
56
57 #include "pcib_if.h"
58 #include "msi_if.h"
59
60 extern struct bus_space memmap_bus;
61
62 #define BUS_SPACE_3G_MAXADDR    0xc0000000
63 #define PCI_ID_VAL3             0x43c
64 #define CLASS_SHIFT             0x10
65 #define SUBCLASS_SHIFT          0x8
66
67 #define REG_CONTROLLER_HW_REV                   0x406c
68 #define REG_BRIDGE_CTRL                         0x9210
69 #define BRIDGE_DISABLE_FLAG     0x1
70 #define BRIDGE_RESET_FLAG       0x2
71 #define REG_BRIDGE_SERDES_MODE                  0x4204
72 #define REG_BRIDGE_CONFIG                       0x4008
73 #define REG_BRIDGE_MEM_WINDOW_LOW               0x4034
74 #define REG_BRIDGE_MEM_WINDOW_HIGH              0x4038
75 #define REG_BRIDGE_MEM_WINDOW_1                 0x403c
76 #define REG_BRIDGE_GISB_WINDOW                  0x402c
77 #define REG_BRIDGE_STATE                        0x4068
78 #define REG_BRIDGE_LINK_STATE                   0x00bc
79 #define REG_BRIDGE_BUS_WINDOW_LOW               0x400c
80 #define REG_BRIDGE_BUS_WINDOW_HIGH              0x4010
81 #define REG_BRIDGE_CPU_WINDOW_LOW               0x4070
82 #define REG_BRIDGE_CPU_WINDOW_START_HIGH        0x4080
83 #define REG_BRIDGE_CPU_WINDOW_END_HIGH          0x4084
84
85 #define REG_MSI_ADDR_LOW                        0x4044
86 #define REG_MSI_ADDR_HIGH                       0x4048
87 #define REG_MSI_CONFIG                          0x404c
88 #define REG_MSI_CLR                             0x4508
89 #define REG_MSI_MASK_CLR                        0x4514
90 #define REG_MSI_RAISED                          0x4500
91 #define REG_MSI_EOI                             0x4060
92 #define NUM_MSI                 32
93
94 #define REG_EP_CONFIG_CHOICE                    0x9000
95 #define REG_EP_CONFIG_DATA                      0x8000
96
97 /*
98  * These values were obtained from runtime inspection of a Linux system using a
99  * JTAG. The very limited documentation I have obtained from Broadcom does not
100  * explain how to compute them.
101  */
102 #define REG_VALUE_4GB_WINDOW    0x11
103 #define REG_VALUE_4GB_CONFIG    0x88003000
104 #define REG_VALUE_MSI_CONFIG    0xffe06540
105
106 struct bcm_pcib_irqsrc {
107         struct intr_irqsrc      isrc;
108         u_int                   irq;
109         bool                    allocated;
110 };
111
112 struct bcm_pcib_softc {
113         struct generic_pcie_fdt_softc   base;
114         device_t                        dev;
115         struct mtx                      config_mtx;
116         struct mtx                      msi_mtx;
117         struct resource                 *msi_irq_res;
118         void                            *msi_intr_cookie;
119         struct bcm_pcib_irqsrc          *msi_isrcs;
120         pci_addr_t                      msi_addr;
121 };
122
123 static struct ofw_compat_data compat_data[] = {
124         {"brcm,bcm2711-pcie",                   1},
125         {"brcm,bcm7211-pcie",                   1},
126         {"brcm,bcm7445-pcie",                   1},
127         {NULL,                                  0}
128 };
129
130 static int
131 bcm_pcib_probe(device_t dev)
132 {
133
134         if (!ofw_bus_status_okay(dev))
135                 return (ENXIO);
136
137         if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
138                 return (ENXIO);
139
140         device_set_desc(dev,
141             "BCM2838-compatible PCI-express controller");
142         return (BUS_PROBE_DEFAULT);
143 }
144
145 static void
146 bcm_pcib_set_reg(struct bcm_pcib_softc *sc, uint32_t reg, uint32_t val)
147 {
148
149         bus_space_write_4(sc->base.base.bst, sc->base.base.bsh, reg,
150             htole32(val));
151 }
152
153 static uint32_t
154 bcm_pcib_read_reg(struct bcm_pcib_softc *sc, uint32_t reg)
155 {
156
157         return (le32toh(bus_space_read_4(sc->base.base.bst, sc->base.base.bsh,
158             reg)));
159 }
160
161 static void
162 bcm_pcib_reset_controller(struct bcm_pcib_softc *sc)
163 {
164         uint32_t val;
165
166         val = bcm_pcib_read_reg(sc, REG_BRIDGE_CTRL);
167         val = val | BRIDGE_RESET_FLAG | BRIDGE_DISABLE_FLAG;
168         bcm_pcib_set_reg(sc, REG_BRIDGE_CTRL, val);
169
170         DELAY(100);
171
172         val = bcm_pcib_read_reg(sc, REG_BRIDGE_CTRL);
173         val = val & ~BRIDGE_RESET_FLAG;
174         bcm_pcib_set_reg(sc, REG_BRIDGE_CTRL, val);
175
176         DELAY(100);
177
178         bcm_pcib_set_reg(sc, REG_BRIDGE_SERDES_MODE, 0);
179
180         DELAY(100);
181 }
182
183 static void
184 bcm_pcib_enable_controller(struct bcm_pcib_softc *sc)
185 {
186         uint32_t val;
187
188         val = bcm_pcib_read_reg(sc, REG_BRIDGE_CTRL);
189         val = val & ~BRIDGE_DISABLE_FLAG;
190         bcm_pcib_set_reg(sc, REG_BRIDGE_CTRL, val);
191
192         DELAY(100);
193 }
194
195 static int
196 bcm_pcib_check_ranges(device_t dev)
197 {
198         struct bcm_pcib_softc *sc;
199         struct pcie_range *ranges;
200         int error = 0, i;
201
202         sc = device_get_softc(dev);
203         ranges = &sc->base.base.ranges[0];
204
205         /* The first range needs to be non-zero. */
206         if (ranges[0].size == 0) {
207                 device_printf(dev, "error: first outbound memory range "
208                     "(pci addr: 0x%jx, cpu addr: 0x%jx) has zero size.\n",
209                     ranges[0].pci_base, ranges[0].phys_base);
210                 error = ENXIO;
211         }
212
213         /*
214          * The controller can actually handle three distinct ranges, but we
215          * only implement support for one.
216          */
217         for (i = 1; (bootverbose || error) && i < MAX_RANGES_TUPLES; ++i) {
218                 if (ranges[i].size > 0)
219                         device_printf(dev,
220                             "note: outbound memory range %d (pci addr: 0x%jx, "
221                             "cpu addr: 0x%jx, size: 0x%jx) will be ignored.\n",
222                             i, ranges[i].pci_base, ranges[i].phys_base,
223                             ranges[i].size);
224         }
225
226         return (error);
227 }
228
229 static const char *
230 bcm_pcib_link_state_string(uint32_t mode)
231 {
232
233         switch(mode & PCIEM_LINK_STA_SPEED) {
234         case 0:
235                 return ("not up");
236         case 1:
237                 return ("2.5 GT/s");
238         case 2:
239                 return ("5.0 GT/s");
240         case 4:
241                 return ("8.0 GT/s");
242         default:
243                 return ("unknown");
244         }
245 }
246
247 static bus_addr_t
248 bcm_get_offset_and_prepare_config(struct bcm_pcib_softc *sc, u_int bus,
249     u_int slot, u_int func, u_int reg)
250 {
251         /*
252          * Config for an end point is only available through a narrow window for
253          * one end point at a time. We first tell the controller which end point
254          * we want, then access it through the window.
255          */
256         uint32_t func_index;
257
258         if (bus == 0 && slot == 0 && func == 0)
259                 /*
260                  * Special case for root device; its config is always available
261                  * through the zero-offset.
262                  */
263                 return (reg);
264
265         /* Tell the controller to show us the config in question. */
266         func_index = PCIE_ADDR_OFFSET(bus, slot, func, 0);
267         bcm_pcib_set_reg(sc, REG_EP_CONFIG_CHOICE, func_index);
268
269         return (REG_EP_CONFIG_DATA + reg);
270 }
271
272 static bool
273 bcm_pcib_is_valid_quad(struct bcm_pcib_softc *sc, u_int bus, u_int slot,
274     u_int func, u_int reg)
275 {
276
277         if ((bus < sc->base.base.bus_start) || (bus > sc->base.base.bus_end))
278                 return (false);
279         if ((slot > PCI_SLOTMAX) || (func > PCI_FUNCMAX) || (reg > PCIE_REGMAX))
280                 return (false);
281
282         if (bus == 0 && slot == 0 && func == 0)
283                 return (true);
284         if (bus == 0)
285                 /*
286                  * Probing other slots and funcs on bus 0 will lock up the
287                  * memory controller.
288                  */
289                 return (false);
290
291         return (true);
292 }
293
294 static uint32_t
295 bcm_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
296     int bytes)
297 {
298         struct bcm_pcib_softc *sc;
299         bus_space_handle_t h;
300         bus_space_tag_t t;
301         bus_addr_t offset;
302         uint32_t data;
303
304         sc = device_get_softc(dev);
305         if (!bcm_pcib_is_valid_quad(sc, bus, slot, func, reg))
306                 return (~0U);
307
308         mtx_lock(&sc->config_mtx);
309         offset = bcm_get_offset_and_prepare_config(sc, bus, slot, func, reg);
310
311         t = sc->base.base.bst;
312         h = sc->base.base.bsh;
313
314         switch (bytes) {
315         case 1:
316                 data = bus_space_read_1(t, h, offset);
317                 break;
318         case 2:
319                 data = le16toh(bus_space_read_2(t, h, offset));
320                 break;
321         case 4:
322                 data = le32toh(bus_space_read_4(t, h, offset));
323                 break;
324         default:
325                 data = ~0U;
326                 break;
327         }
328
329         mtx_unlock(&sc->config_mtx);
330         return (data);
331 }
332
333 static void
334 bcm_pcib_write_config(device_t dev, u_int bus, u_int slot,
335     u_int func, u_int reg, uint32_t val, int bytes)
336 {
337         struct bcm_pcib_softc *sc;
338         bus_space_handle_t h;
339         bus_space_tag_t t;
340         uint32_t offset;
341
342         sc = device_get_softc(dev);
343         if (!bcm_pcib_is_valid_quad(sc, bus, slot, func, reg))
344                 return;
345
346         mtx_lock(&sc->config_mtx);
347         offset = bcm_get_offset_and_prepare_config(sc, bus, slot, func, reg);
348
349         t = sc->base.base.bst;
350         h = sc->base.base.bsh;
351
352         switch (bytes) {
353         case 1:
354                 bus_space_write_1(t, h, offset, val);
355                 break;
356         case 2:
357                 bus_space_write_2(t, h, offset, htole16(val));
358                 break;
359         case 4:
360                 bus_space_write_4(t, h, offset, htole32(val));
361                 break;
362         default:
363                 break;
364         }
365
366         mtx_unlock(&sc->config_mtx);
367 }
368
369 static void
370 bcm_pcib_msi_intr_process(struct bcm_pcib_softc *sc, uint32_t interrupt_bitmap,
371     struct trapframe *tf)
372 {
373         struct bcm_pcib_irqsrc *irqsrc;
374         uint32_t bit, irq;
375
376         while ((bit = ffs(interrupt_bitmap))) {
377                 irq = bit - 1;
378
379                 /* Acknowledge interrupt. */
380                 bcm_pcib_set_reg(sc, REG_MSI_CLR, 1 << irq);
381
382                 /* Send EOI. */
383                 bcm_pcib_set_reg(sc, REG_MSI_EOI, 1);
384
385                 /* Despatch to handler. */
386                 irqsrc = &sc->msi_isrcs[irq];
387                 if (intr_isrc_dispatch(&irqsrc->isrc, tf))
388                         device_printf(sc->dev,
389                             "note: unexpected interrupt (%d) triggered.\n",
390                             irq);
391
392                 /* Done with this interrupt. */
393                 interrupt_bitmap = interrupt_bitmap & ~(1 << irq);
394         }
395 }
396
397 static int
398 bcm_pcib_msi_intr(void *arg)
399 {
400         struct bcm_pcib_softc *sc;
401         struct trapframe *tf;
402         uint32_t interrupt_bitmap;
403
404         sc = (struct bcm_pcib_softc *) arg;
405         tf = curthread->td_intr_frame;
406
407         while ((interrupt_bitmap = bcm_pcib_read_reg(sc, REG_MSI_RAISED)))
408                 bcm_pcib_msi_intr_process(sc, interrupt_bitmap, tf);
409
410         return (FILTER_HANDLED);
411 }
412
413 static int
414 bcm_pcib_alloc_msi(device_t dev, device_t child, int count, int maxcount,
415     device_t *pic, struct intr_irqsrc **srcs)
416 {
417         struct bcm_pcib_softc *sc;
418         int first_int, i;
419
420         sc = device_get_softc(dev);
421         mtx_lock(&sc->msi_mtx);
422
423         /* Find a continguous region of free message-signalled interrupts. */
424         for (first_int = 0; first_int + count < NUM_MSI; ) {
425                 for (i = first_int; i < first_int + count; ++i) {
426                         if (sc->msi_isrcs[i].allocated)
427                                 goto next;
428                 }
429                 goto found;
430 next:
431                 first_int = i + 1;
432         }
433
434         /* No appropriate region available. */
435         mtx_unlock(&sc->msi_mtx);
436         device_printf(dev, "warning: failed to allocate %d MSI messages.\n",
437             count);
438         return (ENXIO);
439
440 found:
441         /* Mark the messages as in use. */
442         for (i = 0; i < count; ++i) {
443                 sc->msi_isrcs[i + first_int].allocated = true;
444                 srcs[i] = &(sc->msi_isrcs[i + first_int].isrc);
445         }
446
447         mtx_unlock(&sc->msi_mtx);
448         *pic = device_get_parent(dev);
449
450         return (0);
451 }
452
453 static int
454 bcm_pcib_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
455     uint64_t *addr, uint32_t *data)
456 {
457         struct bcm_pcib_softc *sc;
458         struct bcm_pcib_irqsrc *msi_msg;
459
460         sc = device_get_softc(dev);
461         msi_msg = (struct bcm_pcib_irqsrc *) isrc;
462
463         *addr = sc->msi_addr;
464         *data = (REG_VALUE_MSI_CONFIG & 0xffff) | msi_msg->irq;
465         return (0);
466 }
467
468 static int
469 bcm_pcib_release_msi(device_t dev, device_t child, int count,
470     struct intr_irqsrc **isrc)
471 {
472         struct bcm_pcib_softc *sc;
473         struct bcm_pcib_irqsrc *msi_isrc;
474         int i;
475
476         sc = device_get_softc(dev);
477         mtx_lock(&sc->msi_mtx);
478
479         for (i = 0; i < count; i++) {
480                 msi_isrc = (struct bcm_pcib_irqsrc *) isrc[i];
481                 msi_isrc->allocated = false;
482         }
483
484         mtx_unlock(&sc->msi_mtx);
485         return (0);
486 }
487
488 static int
489 bcm_pcib_msi_attach(device_t dev)
490 {
491         struct bcm_pcib_softc *sc;
492         phandle_t node, xref;
493         char const *bcm_name;
494         int i, rid;
495
496         sc = device_get_softc(dev);
497         sc->msi_addr = 0xffffffffc;
498
499         /* Clear any pending interrupts. */
500         bcm_pcib_set_reg(sc, REG_MSI_CLR, 0xffffffff);
501
502         rid = 1;
503         sc->msi_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
504             RF_ACTIVE);
505         if (sc->msi_irq_res == NULL) {
506                 device_printf(dev, "could not allocate MSI irq resource.\n");
507                 return (ENXIO);
508         }
509
510         sc->msi_isrcs = malloc(sizeof(*sc->msi_isrcs) * NUM_MSI, M_DEVBUF,
511             M_WAITOK | M_ZERO);
512
513         int error = bus_setup_intr(dev, sc->msi_irq_res, INTR_TYPE_BIO |
514             INTR_MPSAFE, bcm_pcib_msi_intr, NULL, sc, &sc->msi_intr_cookie);
515         if (error) {
516                 device_printf(dev, "error: failed to setup MSI handler.\n");
517                 return (ENXIO);
518         }
519
520         bcm_name = device_get_nameunit(dev);
521         for (i = 0; i < NUM_MSI; i++) {
522                 sc->msi_isrcs[i].irq = i;
523                 error = intr_isrc_register(&sc->msi_isrcs[i].isrc, dev, 0,
524                     "%s,%u", bcm_name, i);
525                 if (error) {
526                         device_printf(dev,
527                         "error: failed to register interrupt %d.\n", i);
528                         return (ENXIO);
529                 }
530         }
531
532         node = ofw_bus_get_node(dev);
533         xref = OF_xref_from_node(node);
534         OF_device_register_xref(xref, dev);
535
536         error = intr_msi_register(dev, xref);
537         if (error)
538                 return (ENXIO);
539
540         mtx_init(&sc->msi_mtx, "bcm_pcib: msi_mtx", NULL, MTX_DEF);
541
542         bcm_pcib_set_reg(sc, REG_MSI_MASK_CLR, 0xffffffff);
543         bcm_pcib_set_reg(sc, REG_MSI_ADDR_LOW, (sc->msi_addr & 0xffffffff) | 1);
544         bcm_pcib_set_reg(sc, REG_MSI_ADDR_HIGH, (sc->msi_addr >> 32));
545         bcm_pcib_set_reg(sc, REG_MSI_CONFIG, REG_VALUE_MSI_CONFIG);
546
547         return (0);
548 }
549
550 static void
551 bcm_pcib_relocate_bridge_window(device_t dev)
552 {
553         /*
554          * In principle an out-of-bounds bridge window could be automatically
555          * adjusted at resource-activation time to lie within the bus address
556          * space by pcib_grow_window(), but that is not possible because the
557          * out-of-bounds resource allocation fails at allocation time. Instead,
558          * we will just fix up the window on the controller here, before it is
559          * re-discovered by pcib_probe_windows().
560          */
561
562         struct bcm_pcib_softc *sc;
563         pci_addr_t base, size, new_base, new_limit;
564         uint16_t val;
565
566         sc = device_get_softc(dev);
567
568         val = bcm_pcib_read_config(dev, 0, 0, 0, PCIR_MEMBASE_1, 2);
569         base = PCI_PPBMEMBASE(0, val);
570
571         val = bcm_pcib_read_config(dev, 0, 0, 0, PCIR_MEMLIMIT_1, 2);
572         size = PCI_PPBMEMLIMIT(0, val) - base;
573
574         new_base = sc->base.base.ranges[0].pci_base;
575         val = (uint16_t) (new_base >> 16);
576         bcm_pcib_write_config(dev, 0, 0, 0, PCIR_MEMBASE_1, val, 2);
577
578         new_limit = new_base + size;
579         val = (uint16_t) (new_limit >> 16);
580         bcm_pcib_write_config(dev, 0, 0, 0, PCIR_MEMLIMIT_1, val, 2);
581 }
582
583 static uint32_t
584 encode_cpu_window_low(pci_addr_t phys_base, bus_size_t size)
585 {
586
587         return (((phys_base >> 0x10) & 0xfff0) |
588             ((phys_base + size - 1) & 0xfff00000));
589 }
590
591 static uint32_t
592 encode_cpu_window_start_high(pci_addr_t phys_base)
593 {
594
595         return ((phys_base >> 0x20) & 0xff);
596 }
597
598 static uint32_t
599 encode_cpu_window_end_high(pci_addr_t phys_base, bus_size_t size)
600 {
601
602         return (((phys_base + size - 1) >> 0x20) & 0xff);
603 }
604
605 static int
606 bcm_pcib_attach(device_t dev)
607 {
608         struct bcm_pcib_softc *sc;
609         pci_addr_t phys_base, pci_base;
610         bus_size_t size;
611         uint32_t hardware_rev, bridge_state, link_state;
612         int error, tries;
613
614         sc = device_get_softc(dev);
615         sc->dev = dev;
616
617         error = pci_host_generic_setup_fdt(dev);
618         if (error)
619                 return (error);
620
621         error = bcm_pcib_check_ranges(dev);
622         if (error)
623                 return (error);
624
625         mtx_init(&sc->config_mtx, "bcm_pcib: config_mtx", NULL, MTX_DEF);
626
627         bcm_pcib_reset_controller(sc);
628
629         hardware_rev = bcm_pcib_read_reg(sc, REG_CONTROLLER_HW_REV) & 0xffff;
630         device_printf(dev, "hardware identifies as revision 0x%x.\n",
631             hardware_rev);
632
633         /*
634          * Set PCI->CPU memory window. This encodes the inbound window showing
635          * up to 4 GiB of system memory to the controller, with zero offset.
636          * Thus, from the perspective of a device on the PCI-E bus, there is a
637          * 1:1 map from PCI-E bus addresses to system memory addresses. However,
638          * a hardware limitation means that the controller can only perform DMA
639          * on the lower 3 GiB of system memory.
640          */
641         bcm_pcib_set_reg(sc, REG_BRIDGE_MEM_WINDOW_LOW, REG_VALUE_4GB_WINDOW);
642         bcm_pcib_set_reg(sc, REG_BRIDGE_MEM_WINDOW_HIGH, 0);
643         bcm_pcib_set_reg(sc, REG_BRIDGE_CONFIG, REG_VALUE_4GB_CONFIG);
644         bcm_pcib_set_reg(sc, REG_BRIDGE_GISB_WINDOW, 0);
645         bcm_pcib_set_reg(sc, REG_BRIDGE_MEM_WINDOW_1, 0);
646
647         bcm_pcib_enable_controller(sc);
648
649         /* Wait for controller to start. */
650         for(tries = 0; ; ++tries) {
651                 bridge_state = bcm_pcib_read_reg(sc, REG_BRIDGE_STATE);
652
653                 if ((bridge_state & 0x30) == 0x30)
654                         /* Controller ready. */
655                         break;
656
657                 if (tries > 100) {
658                         device_printf(dev,
659                             "error: controller failed to start.\n");
660                         return (ENXIO);
661                 }
662
663                 DELAY(1000);
664         }
665
666         link_state = bcm_pcib_read_reg(sc, REG_BRIDGE_LINK_STATE) >> 0x10;
667         if (!link_state) {
668                 device_printf(dev, "error: controller started but link is not "
669                     "up.\n");
670                 return (ENXIO);
671         }
672         if (bootverbose)
673                 device_printf(dev, "note: reported link speed is %s.\n",
674                     bcm_pcib_link_state_string(link_state));
675
676         /*
677          * Set the CPU->PCI memory window. The map in this direction is not 1:1.
678          * Addresses seen by the CPU need to be adjusted to make sense to the
679          * controller as they pass through the window.
680          */
681         pci_base  = sc->base.base.ranges[0].pci_base;
682         phys_base = sc->base.base.ranges[0].phys_base;
683         size      = sc->base.base.ranges[0].size;
684
685         bcm_pcib_set_reg(sc, REG_BRIDGE_BUS_WINDOW_LOW, pci_base & 0xffffffff);
686         bcm_pcib_set_reg(sc, REG_BRIDGE_BUS_WINDOW_HIGH, pci_base >> 32);
687
688         bcm_pcib_set_reg(sc, REG_BRIDGE_CPU_WINDOW_LOW,
689             encode_cpu_window_low(phys_base, size));
690         bcm_pcib_set_reg(sc, REG_BRIDGE_CPU_WINDOW_START_HIGH,
691             encode_cpu_window_start_high(phys_base));
692         bcm_pcib_set_reg(sc, REG_BRIDGE_CPU_WINDOW_END_HIGH,
693             encode_cpu_window_end_high(phys_base, size));
694
695         /*
696          * The controller starts up declaring itself an endpoint; readvertise it
697          * as a bridge.
698          */
699         bcm_pcib_set_reg(sc, PCI_ID_VAL3,
700             PCIC_BRIDGE << CLASS_SHIFT | PCIS_BRIDGE_PCI << SUBCLASS_SHIFT);
701
702         bcm_pcib_set_reg(sc, REG_BRIDGE_SERDES_MODE, 0x2);
703         DELAY(100);
704
705         bcm_pcib_relocate_bridge_window(dev);
706
707         /* Configure interrupts. */
708         error = bcm_pcib_msi_attach(dev);
709         if (error)
710                 return (error);
711
712         /* Done. */
713         device_add_child(dev, "pci", -1);
714         return (bus_generic_attach(dev));
715 }
716
717 /*
718  * Device method table.
719  */
720 static device_method_t bcm_pcib_methods[] = {
721         /* Device interface. */
722         DEVMETHOD(device_probe,                 bcm_pcib_probe),
723         DEVMETHOD(device_attach,                bcm_pcib_attach),
724
725         /* PCIB interface. */
726         DEVMETHOD(pcib_read_config,             bcm_pcib_read_config),
727         DEVMETHOD(pcib_write_config,            bcm_pcib_write_config),
728
729         /* MSI interface. */
730         DEVMETHOD(msi_alloc_msi,                bcm_pcib_alloc_msi),
731         DEVMETHOD(msi_release_msi,              bcm_pcib_release_msi),
732         DEVMETHOD(msi_map_msi,                  bcm_pcib_map_msi),
733
734         DEVMETHOD_END
735 };
736
737 DEFINE_CLASS_1(pcib, bcm_pcib_driver, bcm_pcib_methods,
738     sizeof(struct bcm_pcib_softc), generic_pcie_fdt_driver);
739
740 static devclass_t bcm_pcib_devclass;
741 DRIVER_MODULE(bcm_pcib, simplebus, bcm_pcib_driver, bcm_pcib_devclass, 0, 0);