Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: ehci_plb.c,v 1.2 2026/06/19 18:55:23 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.2 2026/06/19 18:55:23 rkujawa Exp $");
     37 
     38 #include "opt_ppc4xx.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 #ifdef PPC4XX_L2CACHE
     48 #include <powerpc/ibm4xx/ibm4xx_460ex_l2.h>
     49 #endif
     50 
     51 #include <dev/usb/usb.h>
     52 #include <dev/usb/usbdi.h>
     53 #include <dev/usb/usbdivar.h>
     54 #include <dev/usb/usb_mem.h>
     55 #include <dev/usb/ehcireg.h>
     56 #include <dev/usb/ehcivar.h>
     57 
     58 #define	EHCI_PLB_SIZE	0x400
     59 
     60 #include "locators.h"
     61 
     62 static int	ehci_plb_match(device_t, cfdata_t, void *);
     63 static void	ehci_plb_attach(device_t, device_t, void *);
     64 static void	ehci_plb_deferred(device_t);
     65 
     66 CFATTACH_DECL_NEW(ehci_plb, sizeof(struct ehci_softc),
     67     ehci_plb_match, ehci_plb_attach, NULL, NULL);
     68 
     69 static struct powerpc_bus_space ehci_plb_tag = {
     70 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
     71 	0x00000000,
     72 };
     73 static char ehci_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)]
     74     __attribute__((aligned(8)));
     75 
     76 /*
     77  * PLB4xAHB AHB arbiter
     78  */
     79 #define	AHBARB_BASE		0xbffd2000
     80 #define	AHBARB_SIZE		0x100
     81 #define	AHBARB_PL5		0x10		/* AHB master #5 priority = EHCI */
     82 #define	AHBARB_VERSION		0x90
     83 #define	AHBARB_VERSION_MAGIC	0x3230362a
     84 #define	AHBARB_PRIO_MAX		15		/* 0=disabled .. 15=highest */
     85 
     86 static struct powerpc_bus_space ehci_plb_arb_tag = {
     87 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
     88 	0x00000000,
     89 };
     90 static char ehci_arb_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)]
     91     __attribute__((aligned(8)));
     92 
     93 static int
     94 ehci_plb_match(device_t parent, cfdata_t match, void *aux)
     95 {
     96 	struct plb_attach_args *paa = aux;
     97 
     98 	if (strcmp(paa->plb_name, match->cf_name) != 0)
     99 		return 0;
    100 
    101 	if (match->cf_loc[PLBCF_ADDR] == PLBCF_ADDR_DEFAULT)
    102 		panic("ehci_plb_match: wildcard addr not allowed");
    103 	if (match->cf_loc[PLBCF_IRQ] == PLBCF_IRQ_DEFAULT)
    104 		panic("ehci_plb_match: wildcard IRQ not allowed");
    105 
    106 	paa->plb_addr = match->cf_loc[PLBCF_ADDR];
    107 	paa->plb_irq = match->cf_loc[PLBCF_IRQ];
    108 	return 1;
    109 }
    110 
    111 /*
    112  * Raise the EHCI host controller's AHB arbitration priority to the maximum.
    113  */
    114 static void
    115 ehci_plb_set_arb_priority(device_t self)
    116 {
    117 	bus_space_handle_t ioh;
    118 
    119 	ehci_plb_arb_tag.pbs_base = AHBARB_BASE;
    120 	ehci_plb_arb_tag.pbs_limit = AHBARB_BASE + AHBARB_SIZE;
    121 	if (bus_space_init(&ehci_plb_arb_tag, "ehciarb", ehci_arb_ex_storage,
    122 	      sizeof(ehci_arb_ex_storage)) ||
    123 	    bus_space_map(&ehci_plb_arb_tag, AHBARB_BASE, AHBARB_SIZE, 0, &ioh)) {
    124 		aprint_error_dev(self, "can't map AHB arbiter\n");
    125 		return;
    126 	}
    127 
    128 	if (bus_space_read_4(&ehci_plb_arb_tag, ioh, AHBARB_VERSION) ==
    129 	    AHBARB_VERSION_MAGIC)
    130 		bus_space_write_4(&ehci_plb_arb_tag, ioh, AHBARB_PL5,
    131 		    AHBARB_PRIO_MAX);
    132 	else
    133 		aprint_error_dev(self,
    134 		    "AHB arbiter version mismatch; EHCI priority unchanged\n");
    135 
    136 	bus_space_unmap(&ehci_plb_arb_tag, ioh, AHBARB_SIZE);
    137 }
    138 
    139 static void
    140 ehci_plb_attach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct ehci_softc *sc = device_private(self);
    143 	struct plb_attach_args *paa = aux;
    144 
    145 	sc->sc_dev = self;
    146 	sc->sc_bus.ub_hcpriv = sc;
    147 #ifdef PPC4XX_L2CACHE
    148 	/* USB DMA needs software L2 invalidation (hardware snoop misses it). */
    149 	sc->sc_bus.ub_dmatag = ibm4xx_460ex_l2_dmatag();
    150 #else
    151 	sc->sc_bus.ub_dmatag = paa->plb_dmat;
    152 #endif
    153 	sc->sc_bus.ub_revision = USBREV_2_0;
    154 
    155 	ehci_plb_tag.pbs_base = paa->plb_addr;
    156 	ehci_plb_tag.pbs_limit = paa->plb_addr + EHCI_PLB_SIZE;
    157 	sc->iot = &ehci_plb_tag;
    158 
    159 	if (bus_space_init(&ehci_plb_tag, "ehciplb", ehci_ex_storage,
    160 	      sizeof(ehci_ex_storage)) ||
    161 	    bus_space_map(sc->iot, paa->plb_addr, EHCI_PLB_SIZE, 0,
    162 	      &sc->ioh)) {
    163 		aprint_error(": can't map registers\n");
    164 		return;
    165 	}
    166 	sc->sc_size = EHCI_PLB_SIZE;
    167 
    168 	aprint_normal(": EHCI USB controller\n");
    169 
    170 	sc->sc_offs = bus_space_read_1(sc->iot, sc->ioh, EHCI_CAPLENGTH);
    171 
    172 	/* Give the EHCI AHB master top arbitration priority (ERR4003 CHIP_16). */
    173 	ehci_plb_set_arb_priority(self);
    174 
    175 	/* Disable interrupts, so we don't get any spurious ones. */
    176 	bus_space_write_4(sc->iot, sc->ioh, sc->sc_offs + EHCI_USBINTR, 0);
    177 
    178 	intr_establish_xname(paa->plb_irq, IST_LEVEL, IPL_USB, ehci_intr,
    179 	    sc, device_xname(self));
    180 
    181 	/* Wait until the companion OHCI has attached. */
    182 	config_defer(self, ehci_plb_deferred);
    183 }
    184 
    185 static void
    186 ehci_plb_deferred(device_t self)
    187 {
    188 	struct ehci_softc *sc = device_private(self);
    189 	device_t comp;
    190 	int error;
    191 
    192 	comp = device_find_by_xname("ohci0");
    193 	if (comp != NULL) {
    194 		sc->sc_ncomp = 1;
    195 		sc->sc_comps[0] = comp;
    196 	}
    197 
    198 	error = ehci_init(sc);
    199 	if (error) {
    200 		aprint_error_dev(self, "init failed, error=%d\n", error);
    201 		return;
    202 	}
    203 
    204 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint,
    205 	    CFARGS_NONE);
    206 }
    207