Home | History | Annotate | Line # | Download | only in dev
ehci_plb.c revision 1.1
      1 /*	$NetBSD: ehci_plb.c,v 1.1 2026/06/14 00:02:35 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.1 2026/06/14 00:02:35 rkujawa Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/extent.h>
     42 
     43 #include <powerpc/ibm4xx/cpu.h>
     44 #include <powerpc/ibm4xx/dev/plbvar.h>
     45 
     46 #include <dev/usb/usb.h>
     47 #include <dev/usb/usbdi.h>
     48 #include <dev/usb/usbdivar.h>
     49 #include <dev/usb/usb_mem.h>
     50 #include <dev/usb/ehcireg.h>
     51 #include <dev/usb/ehcivar.h>
     52 
     53 #define	EHCI_PLB_SIZE	0x400
     54 
     55 #include "locators.h"
     56 
     57 static int	ehci_plb_match(device_t, cfdata_t, void *);
     58 static void	ehci_plb_attach(device_t, device_t, void *);
     59 static void	ehci_plb_deferred(device_t);
     60 
     61 CFATTACH_DECL_NEW(ehci_plb, sizeof(struct ehci_softc),
     62     ehci_plb_match, ehci_plb_attach, NULL, NULL);
     63 
     64 static struct powerpc_bus_space ehci_plb_tag = {
     65 	_BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE,
     66 	0x00000000,
     67 };
     68 static char ehci_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)]
     69     __attribute__((aligned(8)));
     70 
     71 static int
     72 ehci_plb_match(device_t parent, cfdata_t match, void *aux)
     73 {
     74 	struct plb_attach_args *paa = aux;
     75 
     76 	if (strcmp(paa->plb_name, match->cf_name) != 0)
     77 		return 0;
     78 
     79 	if (match->cf_loc[PLBCF_ADDR] == PLBCF_ADDR_DEFAULT)
     80 		panic("ehci_plb_match: wildcard addr not allowed");
     81 	if (match->cf_loc[PLBCF_IRQ] == PLBCF_IRQ_DEFAULT)
     82 		panic("ehci_plb_match: wildcard IRQ not allowed");
     83 
     84 	paa->plb_addr = match->cf_loc[PLBCF_ADDR];
     85 	paa->plb_irq = match->cf_loc[PLBCF_IRQ];
     86 	return 1;
     87 }
     88 
     89 static void
     90 ehci_plb_attach(device_t parent, device_t self, void *aux)
     91 {
     92 	struct ehci_softc *sc = device_private(self);
     93 	struct plb_attach_args *paa = aux;
     94 
     95 	sc->sc_dev = self;
     96 	sc->sc_bus.ub_hcpriv = sc;
     97 	sc->sc_bus.ub_dmatag = paa->plb_dmat;
     98 	sc->sc_bus.ub_revision = USBREV_2_0;
     99 
    100 	ehci_plb_tag.pbs_base = paa->plb_addr;
    101 	ehci_plb_tag.pbs_limit = paa->plb_addr + EHCI_PLB_SIZE;
    102 	sc->iot = &ehci_plb_tag;
    103 
    104 	if (bus_space_init(&ehci_plb_tag, "ehciplb", ehci_ex_storage,
    105 	      sizeof(ehci_ex_storage)) ||
    106 	    bus_space_map(sc->iot, paa->plb_addr, EHCI_PLB_SIZE, 0,
    107 	      &sc->ioh)) {
    108 		aprint_error(": can't map registers\n");
    109 		return;
    110 	}
    111 	sc->sc_size = EHCI_PLB_SIZE;
    112 
    113 	aprint_normal(": EHCI USB controller\n");
    114 
    115 	sc->sc_offs = bus_space_read_1(sc->iot, sc->ioh, EHCI_CAPLENGTH);
    116 
    117 	/* Disable interrupts, so we don't get any spurious ones. */
    118 	bus_space_write_4(sc->iot, sc->ioh, sc->sc_offs + EHCI_USBINTR, 0);
    119 
    120 	intr_establish_xname(paa->plb_irq, IST_LEVEL, IPL_USB, ehci_intr,
    121 	    sc, device_xname(self));
    122 
    123 	/* Wait until the companion OHCI has attached. */
    124 	config_defer(self, ehci_plb_deferred);
    125 }
    126 
    127 static void
    128 ehci_plb_deferred(device_t self)
    129 {
    130 	struct ehci_softc *sc = device_private(self);
    131 	device_t comp;
    132 	int error;
    133 
    134 	comp = device_find_by_xname("ohci0");
    135 	if (comp != NULL) {
    136 		sc->sc_ncomp = 1;
    137 		sc->sc_comps[0] = comp;
    138 	}
    139 
    140 	error = ehci_init(sc);
    141 	if (error) {
    142 		aprint_error_dev(self, "init failed, error=%d\n", error);
    143 		return;
    144 	}
    145 
    146 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint,
    147 	    CFARGS_NONE);
    148 }
    149