1 /* $NetBSD: ehci_plb.c,v 1.3 2026/09/28 20:41:01 rkujawa Exp $ */ 2 3 /* 4 * Copyright (c) 2026 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Radoslaw Kujawa. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * EHCI behind the PLB-AHB bridge. 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: ehci_plb.c,v 1.3 2026/09/28 20:41:01 rkujawa Exp $"); 37 38 #include "opt_usb.h" 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/device.h> 43 #include <sys/extent.h> 44 45 #include <powerpc/ibm4xx/cpu.h> 46 #include <powerpc/ibm4xx/dev/plbvar.h> 47 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usbdi.h> 50 #include <dev/usb/usbdivar.h> 51 #include <dev/usb/usb_mem.h> 52 #include <dev/usb/ehcireg.h> 53 #include <dev/usb/ehcivar.h> 54 55 #define EHCI_PLB_SIZE 0x400 56 57 /* DesignWare implementation regs. */ 58 #define EHCI_DWC_INSNREG01 0x94 59 #define INSNREG01_THRESH_FULL 0x00800080 /* OUT/IN thresh: 1 packet */ 60 #define EHCI_DWC_INSNREG03 0x9c 61 #define INSNREG03_BMT 0x00000001 /* break transfer */ 62 63 #include "locators.h" 64 65 static int ehci_plb_match(device_t, cfdata_t, void *); 66 static void ehci_plb_attach(device_t, device_t, void *); 67 static void ehci_plb_deferred(device_t); 68 69 CFATTACH_DECL_NEW(ehci_plb, sizeof(struct ehci_softc), 70 ehci_plb_match, ehci_plb_attach, NULL, NULL); 71 72 static struct powerpc_bus_space ehci_plb_tag = { 73 _BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE, 74 0x00000000, 75 }; 76 static char ehci_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)] 77 __attribute__((aligned(8))); 78 79 /* 80 * PLB4xAHB AHB arbiter 81 */ 82 #define AHBARB_BASE 0xbffd2000 83 #define AHBARB_SIZE 0x100 84 #define AHBARB_PL5 0x10 /* AHB master #5 priority = EHCI */ 85 #define AHBARB_VERSION 0x90 86 #define AHBARB_VERSION_MAGIC 0x3230362a 87 #define AHBARB_PRIO_MAX 15 /* 0=disabled .. 15=highest */ 88 89 static struct powerpc_bus_space ehci_plb_arb_tag = { 90 _BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE, 91 0x00000000, 92 }; 93 static char ehci_arb_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)] 94 __attribute__((aligned(8))); 95 96 static int 97 ehci_plb_match(device_t parent, cfdata_t match, void *aux) 98 { 99 struct plb_attach_args *paa = aux; 100 101 if (strcmp(paa->plb_name, match->cf_name) != 0) 102 return 0; 103 104 if (match->cf_loc[PLBCF_ADDR] == PLBCF_ADDR_DEFAULT) 105 panic("ehci_plb_match: wildcard addr not allowed"); 106 if (match->cf_loc[PLBCF_IRQ] == PLBCF_IRQ_DEFAULT) 107 panic("ehci_plb_match: wildcard IRQ not allowed"); 108 109 paa->plb_addr = match->cf_loc[PLBCF_ADDR]; 110 paa->plb_irq = match->cf_loc[PLBCF_IRQ]; 111 return 1; 112 } 113 114 /* 115 * Raise the EHCI host controller's AHB arbitration priority to the maximum. 116 */ 117 static void 118 ehci_plb_set_arb_priority(device_t self) 119 { 120 bus_space_handle_t ioh; 121 122 ehci_plb_arb_tag.pbs_base = AHBARB_BASE; 123 ehci_plb_arb_tag.pbs_limit = AHBARB_BASE + AHBARB_SIZE; 124 if (bus_space_init(&ehci_plb_arb_tag, "ehciarb", ehci_arb_ex_storage, 125 sizeof(ehci_arb_ex_storage)) || 126 bus_space_map(&ehci_plb_arb_tag, AHBARB_BASE, AHBARB_SIZE, 0, &ioh)) { 127 aprint_error_dev(self, "can't map AHB arbiter\n"); 128 return; 129 } 130 131 if (bus_space_read_4(&ehci_plb_arb_tag, ioh, AHBARB_VERSION) == 132 AHBARB_VERSION_MAGIC) 133 bus_space_write_4(&ehci_plb_arb_tag, ioh, AHBARB_PL5, 134 AHBARB_PRIO_MAX); 135 else 136 aprint_error_dev(self, 137 "AHB arbiter version mismatch; EHCI priority unchanged\n"); 138 139 bus_space_unmap(&ehci_plb_arb_tag, ioh, AHBARB_SIZE); 140 } 141 142 static void 143 ehci_plb_attach(device_t parent, device_t self, void *aux) 144 { 145 struct ehci_softc *sc = device_private(self); 146 struct plb_attach_args *paa = aux; 147 148 sc->sc_dev = self; 149 sc->sc_bus.ub_hcpriv = sc; 150 /* Hardware snoop only. */ 151 sc->sc_bus.ub_dmatag = paa->plb_dmat; 152 sc->sc_bus.ub_revision = USBREV_2_0; 153 154 /* Handle lost completions, duh. */ 155 sc->sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND; 156 157 ehci_plb_tag.pbs_base = paa->plb_addr; 158 ehci_plb_tag.pbs_limit = paa->plb_addr + EHCI_PLB_SIZE; 159 sc->iot = &ehci_plb_tag; 160 161 if (bus_space_init(&ehci_plb_tag, "ehciplb", ehci_ex_storage, 162 sizeof(ehci_ex_storage)) || 163 bus_space_map(sc->iot, paa->plb_addr, EHCI_PLB_SIZE, 0, 164 &sc->ioh)) { 165 aprint_error(": can't map registers\n"); 166 return; 167 } 168 sc->sc_size = EHCI_PLB_SIZE; 169 170 aprint_normal(": EHCI USB controller\n"); 171 172 sc->sc_offs = bus_space_read_1(sc->iot, sc->ioh, EHCI_CAPLENGTH); 173 174 /* 175 * Another workaround: 440EPx errata USBH_3: 176 * bulk-in transfers can stall mid-burst... 177 */ 178 bus_space_write_4(sc->iot, sc->ioh, EHCI_DWC_INSNREG03, INSNREG03_BMT); 179 /* Errata USBH_5: buffer a whole packet before transfer starts. */ 180 bus_space_write_4(sc->iot, sc->ioh, EHCI_DWC_INSNREG01, 181 INSNREG01_THRESH_FULL); 182 #ifdef EHCI_DEBUG 183 aprint_normal_dev(self, "insnreg01 %#x insnreg03 %#x\n", 184 bus_space_read_4(sc->iot, sc->ioh, EHCI_DWC_INSNREG01), 185 bus_space_read_4(sc->iot, sc->ioh, EHCI_DWC_INSNREG03)); 186 #endif 187 188 /* Give the EHCI AHB master top arbitration priority (ERR4003 CHIP_16). */ 189 ehci_plb_set_arb_priority(self); 190 191 /* Disable interrupts, so we don't get any spurious ones. */ 192 bus_space_write_4(sc->iot, sc->ioh, sc->sc_offs + EHCI_USBINTR, 0); 193 194 intr_establish_xname(paa->plb_irq, IST_LEVEL, IPL_USB, ehci_intr, 195 sc, device_xname(self)); 196 197 /* Wait until the companion OHCI has attached. */ 198 config_defer(self, ehci_plb_deferred); 199 } 200 201 static void 202 ehci_plb_deferred(device_t self) 203 { 204 struct ehci_softc *sc = device_private(self); 205 device_t comp; 206 int error; 207 208 comp = device_find_by_xname("ohci0"); 209 if (comp != NULL) { 210 sc->sc_ncomp = 1; 211 sc->sc_comps[0] = comp; 212 } 213 214 error = ehci_init(sc); 215 if (error) { 216 aprint_error_dev(self, "init failed, error=%d\n", error); 217 return; 218 } 219 220 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, 221 CFARGS_NONE); 222 } 223