1 /* $NetBSD: opb.c,v 1.30 2026/06/14 00:02:35 rkujawa Exp $ */ 2 3 /* 4 * Copyright 2001,2002 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 3. All advertising materials mentioning features or use of this software 50 * must display the following acknowledgement: 51 * This product includes software developed by Christopher G. Demetriou 52 * for the NetBSD Project. 53 * 4. The name of the author may not be used to endorse or promote products 54 * derived from this software without specific prior written permission 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 57 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 58 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 59 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 60 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 61 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 62 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 63 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 64 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 65 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: opb.c,v 1.30 2026/06/14 00:02:35 rkujawa Exp $"); 70 71 #include "locators.h" 72 73 #ifdef _KERNEL_OPT 74 #include "opt_emac.h" 75 #endif 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/device.h> 80 #include <sys/extent.h> 81 #include <sys/cpu.h> 82 83 #include <powerpc/ibm4xx/cpu.h> 84 #include <powerpc/ibm4xx/dcr4xx.h> 85 86 #include <powerpc/ibm4xx/dev/opbreg.h> 87 #include <powerpc/ibm4xx/dev/opbvar.h> 88 #include <powerpc/ibm4xx/dev/plbvar.h> 89 #include <powerpc/ibm4xx/dev/rgmiireg.h> 90 #include <powerpc/ibm4xx/dev/zmiireg.h> 91 92 #include <powerpc/spr.h> 93 #include <powerpc/ibm4xx/spr.h> 94 95 #include <powerpc/ibm4xx/ibm405gp.h> 96 #include <powerpc/ibm4xx/amcc405ex.h> 97 #include <powerpc/ibm4xx/amcc460ex.h> 98 99 static int opb_get_frequency_405gp(void); 100 static int opb_get_frequency_405ex(void); 101 static int opb_get_frequency_boardprop(void); 102 103 104 /* 105 * The devices on the On-chip Peripheral Bus to the 405GP/EX CPU. 106 */ 107 const struct opb_dev { 108 int pvr; 109 const char *name; 110 bus_addr_t addr; 111 int instance; 112 int irq; 113 int flags; 114 } opb_devs [] = { 115 /* IBM405GP */ 116 { IBM405GP, "com", IBM405GP_UART0_BASE, 0, 0, 0 }, 117 { IBM405GP, "com", IBM405GP_UART1_BASE, 1, 1, 0 }, 118 { IBM405GP, "emac", IBM405GP_EMAC0_BASE, 0, 15, 0 }, 119 { IBM405GP, "opbgpio", IBM405GP_GPIO0_BASE, 0, -1, 0 }, 120 { IBM405GP, "gpiic",IBM405GP_IIC0_BASE, 0, 2, 0 }, 121 { IBM405GP, "wdog", -1, 0, -1, 0 }, 122 123 /* IBM405GPR */ 124 { IBM405GPR, "com", IBM405GP_UART0_BASE, 0, 0, 0 }, 125 { IBM405GPR, "com", IBM405GP_UART1_BASE, 1, 1, 0 }, 126 { IBM405GPR, "emac", IBM405GP_EMAC0_BASE, 0, 15, 0 }, 127 { IBM405GPR, "opbgpio", IBM405GP_GPIO0_BASE, 0, -1, 0 }, 128 { IBM405GPR, "gpiic",IBM405GP_IIC0_BASE, 0, 2, 0 }, 129 { IBM405GPR, "wdog", -1, 0, -1, 0 }, 130 131 /* AMCC405EX */ 132 { AMCC405EX, "gpt", AMCC405EX_GPT0_BASE, 0, -1, 0 }, 133 { AMCC405EX, "com", AMCC405EX_UART0_BASE, 0, 26, 0 }, 134 { AMCC405EX, "com", AMCC405EX_UART1_BASE, 1, 1, 0 }, 135 { AMCC405EX, "gpiic",AMCC405EX_IIC0_BASE, 0, 2, 0 }, 136 { AMCC405EX, "gpiic",AMCC405EX_IIC1_BASE, 1, 7, 0 }, 137 { AMCC405EX, "scp", AMCC405EX_SCP0_BASE, 0, 8, 0 }, /* SPI */ 138 { AMCC405EX, "opbgpio", AMCC405EX_GPIO0_BASE, -1, -1, 0 }, 139 { AMCC405EX, "emac", AMCC405EX_EMAC0_BASE, 0, 24, 140 OPB_FLAGS_EMAC_GBE | OPB_FLAGS_EMAC_STACV2 | OPB_FLAGS_EMAC_HT256 |\ 141 OPB_FLAGS_EMAC_RMII_RGMII }, 142 { AMCC405EX, "emac", AMCC405EX_EMAC1_BASE, 1, 25, 143 OPB_FLAGS_EMAC_GBE | OPB_FLAGS_EMAC_STACV2 | OPB_FLAGS_EMAC_HT256 |\ 144 OPB_FLAGS_EMAC_RMII_RGMII }, 145 { AMCC405EX, "wdog", -1, 0, -1, 0 }, 146 147 /* AMCC460EX */ 148 { AMCC460EX, "wdog", -1, 0, -1, 0 }, 149 { AMCC460EX, "com", AMCC460EX_UART0_BASE, 0, AMCC460EX_UART0_IRQ, 0 }, 150 { AMCC460EX, "com", AMCC460EX_UART1_BASE, 1, AMCC460EX_UART1_IRQ, 0 }, 151 { AMCC460EX, "gpiic",AMCC460EX_IIC0_BASE, 0, AMCC460EX_IIC0_IRQ, 0 }, 152 { AMCC460EX, "gpiic",AMCC460EX_IIC1_BASE, 1, AMCC460EX_IIC1_IRQ, 0 }, 153 { AMCC460EX, "opbgpio", AMCC460EX_GPIO0_BASE, -1, -1, 0 }, 154 { AMCC460EX, "emac", AMCC460EX_EMAC0_BASE, 0, AMCC460EX_EMAC0_IRQ, 155 OPB_FLAGS_EMAC_GBE | OPB_FLAGS_EMAC_STACV2 | OPB_FLAGS_EMAC_HT256 |\ 156 OPB_FLAGS_EMAC_RMII_RGMII | OPB_FLAGS_EMAC_ETHCFG_ECS |\ 157 OPB_FLAGS_EMAC_TAH }, 158 { AMCC460EX, "emac", AMCC460EX_EMAC1_BASE, 1, AMCC460EX_EMAC1_IRQ, 159 OPB_FLAGS_EMAC_GBE | OPB_FLAGS_EMAC_STACV2 | OPB_FLAGS_EMAC_HT256 |\ 160 OPB_FLAGS_EMAC_RMII_RGMII | OPB_FLAGS_EMAC_ETHCFG_ECS |\ 161 OPB_FLAGS_EMAC_TAH }, 162 163 { 0, NULL } 164 }; 165 166 int (*opb_get_frequency)(void); 167 const struct opb_param { 168 int pvr; 169 bus_addr_t base; 170 bus_addr_t limit; 171 int (*opb_get_frequency)(void); 172 bus_addr_t zmii_base; 173 bus_addr_t rgmii_base; 174 } opb_params[] = { 175 { IBM405GP, 176 IBM405GP_IP_BASE, IBM405GP_IP_BASE + OPBREG_SIZE, 177 opb_get_frequency_405gp, 178 0, 0 }, 179 { IBM405GPR, 180 IBM405GP_IP_BASE, IBM405GP_IP_BASE + OPBREG_SIZE, 181 opb_get_frequency_405gp, 182 0, 0 }, 183 { AMCC405EX, 184 AMCC405EX_OPB_BASE, AMCC405EX_OPB_BASE + OPBREG_SIZE, 185 opb_get_frequency_405ex, 186 0, AMCC405EX_RGMIIB0_BASE}, 187 { AMCC460EX, 188 AMCC460EX_OPB_BASE, AMCC460EX_OPB_BASE + 2 * OPBREG_SIZE, 189 opb_get_frequency_boardprop, 190 0, AMCC460EX_RGMII0_BASE}, 191 192 { 0 } 193 }; 194 195 static int opb_match(device_t, cfdata_t, void *); 196 static void opb_attach(device_t, device_t, void *); 197 static int opb_submatch(device_t, cfdata_t, const int *, void *); 198 static int opb_print(void *, const char *); 199 200 CFATTACH_DECL_NEW(opb, sizeof(struct opb_softc), 201 opb_match, opb_attach, NULL, NULL); 202 203 static struct powerpc_bus_space opb_tag = { 204 _BUS_SPACE_BIG_ENDIAN|_BUS_SPACE_MEM_TYPE, 205 0x00000000, 206 }; 207 static char ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)] 208 __attribute__((aligned(8))); 209 static int opb_tag_init_done; 210 211 /* 212 * Probe for the opb; always succeeds. 213 */ 214 static int 215 opb_match(device_t parent, cfdata_t cf, void *aux) 216 { 217 struct opb_attach_args *oaa = aux; 218 219 /* match only opb devices */ 220 if (strcmp(oaa->opb_name, cf->cf_name) != 0) 221 return 0; 222 223 return 1; 224 } 225 226 static int 227 opb_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 228 { 229 struct opb_attach_args *oaa = aux; 230 231 if (cf->cf_loc[OPBCF_ADDR] != OPBCF_ADDR_DEFAULT && 232 cf->cf_loc[OPBCF_ADDR] != oaa->opb_addr) 233 return 0; 234 235 return config_match(parent, cf, aux); 236 } 237 238 /* 239 * Attach the on-chip peripheral bus. 240 */ 241 static void 242 opb_attach(device_t parent, device_t self, void *aux) 243 { 244 struct opb_softc *sc = device_private(self); 245 struct plb_attach_args *paa = aux; 246 struct opb_attach_args oaa; 247 int i, pvr; 248 249 aprint_naive("\n"); 250 aprint_normal("\n"); 251 pvr = mfpvr() >> 16; 252 253 sc->sc_dev = self; 254 sc->sc_iot = opb_get_bus_space_tag(); 255 256 for (i = 0; opb_params[i].pvr != 0 && opb_params[i].pvr != pvr; i++) 257 ; 258 if (opb_params[i].pvr == 0) 259 panic("opb_get_bus_space_tag: no params for this CPU!"); 260 opb_get_frequency = opb_params[i].opb_get_frequency; 261 /* 262 * The MII bridge handles are used unconditionally 263 * so these must succeed! 264 */ 265 #ifdef EMAC_ZMII_PHY 266 if (opb_params[i].zmii_base != 0 && 267 bus_space_map(sc->sc_iot, opb_params[i].zmii_base, ZMII0_SIZE, 268 0, &sc->sc_zmiih)) 269 panic("opb_attach: can't map ZMII bridge"); 270 #endif 271 #ifdef EMAC_RGMII_PHY 272 if (opb_params[i].rgmii_base != 0 && 273 bus_space_map(sc->sc_iot, opb_params[i].rgmii_base, RGMII0_SIZE, 274 0, &sc->sc_rgmiih)) 275 panic("opb_attach: can't map RGMII bridge"); 276 #endif 277 278 for (i = 0; opb_devs[i].name != NULL; i++) { 279 if (opb_devs[i].pvr != pvr) 280 continue; 281 oaa.opb_name = opb_devs[i].name; 282 oaa.opb_addr = opb_devs[i].addr; 283 oaa.opb_instance = opb_devs[i].instance; 284 oaa.opb_irq = opb_devs[i].irq; 285 oaa.opb_bt = sc->sc_iot; 286 oaa.opb_dmat = paa->plb_dmat; 287 oaa.opb_flags = opb_devs[i].flags; 288 289 config_found(self, &oaa, opb_print, 290 CFARGS(.submatch = opb_submatch)); 291 } 292 } 293 294 static int 295 opb_print(void *aux, const char *pnp) 296 { 297 struct opb_attach_args *oaa = aux; 298 299 if (pnp) 300 aprint_normal("%s at %s", oaa->opb_name, pnp); 301 302 if (oaa->opb_addr != OPBCF_ADDR_DEFAULT) 303 aprint_normal(" addr 0x%08lx", oaa->opb_addr); 304 if (oaa->opb_irq != OPBCF_IRQ_DEFAULT) 305 aprint_normal(" irq %d", oaa->opb_irq); 306 307 return UNCONF; 308 } 309 310 bus_space_tag_t 311 opb_get_bus_space_tag(void) 312 { 313 int i, pvr; 314 315 if (!opb_tag_init_done) { 316 pvr = mfpvr() >> 16; 317 318 for (i = 0; opb_params[i].pvr != 0 && opb_params[i].pvr != pvr; 319 i++) 320 ; 321 if (opb_params[i].pvr == 0) 322 panic("opb_get_bus_space_tag: no params for this CPU!"); 323 324 opb_tag.pbs_base = opb_params[i].base; 325 opb_tag.pbs_limit = opb_params[i].limit; 326 327 if (bus_space_init(&opb_tag, "opbtag", 328 ex_storage, sizeof(ex_storage))) 329 panic("opb_attach: Failed to initialise opb_tag"); 330 opb_tag_init_done = 1; 331 } 332 333 return &opb_tag; 334 } 335 336 static int 337 opb_get_frequency_405gp(void) 338 { 339 prop_number_t pn; 340 uint32_t pllmr; 341 unsigned int processor_freq, plb_freq, opb_freq; 342 343 pn = prop_dictionary_get(board_properties, "processor-frequency"); 344 KASSERT(pn != NULL); 345 processor_freq = (unsigned int) prop_number_integer_value(pn); 346 pllmr = mfdcr(DCR_CPC0_PLLMR); 347 plb_freq = processor_freq / CPC0_PLLMR_CBDV(pllmr); 348 opb_freq = plb_freq / CPC0_PLLMR_OPDV(pllmr); 349 350 return opb_freq; 351 } 352 353 static int 354 opb_get_frequency_405ex(void) 355 { 356 prop_number_t pn; 357 unsigned int processor_freq, plb_freq, opb_freq; 358 359 pn = prop_dictionary_get(board_properties, "processor-frequency"); 360 KASSERT(pn != NULL); 361 processor_freq = (unsigned int) prop_number_integer_value(pn); 362 plb_freq = processor_freq / CPR0_PLBDV0(mfcpr(DCR_CPR0_PLBD)); 363 opb_freq = plb_freq / CPR0_OPBDV0(mfcpr(DCR_CPR0_OPBD)); 364 365 return opb_freq; 366 } 367 368 static int 369 opb_get_frequency_boardprop(void) 370 { 371 prop_number_t pn; 372 373 pn = prop_dictionary_get(board_properties, "opb-frequency"); 374 KASSERT(pn != NULL); 375 376 return (int)prop_number_integer_value(pn); 377 } 378