11.1Sjmcneill/* $NetBSD: ohci_ahb.c,v 1.1 2026/01/09 22:54:30 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2024 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: ohci_ahb.c,v 1.1 2026/01/09 22:54:30 jmcneill Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill
371.1Sjmcneill#include <dev/usb/usb.h>
381.1Sjmcneill#include <dev/usb/usbdi.h>
391.1Sjmcneill#include <dev/usb/usbdivar.h>
401.1Sjmcneill#include <dev/usb/usb_mem.h>
411.1Sjmcneill#include <dev/usb/ohcireg.h>
421.1Sjmcneill#include <dev/usb/ohcivar.h>
431.1Sjmcneill
441.1Sjmcneill#include <machine/wii.h>
451.1Sjmcneill#include <machine/wiiu.h>
461.1Sjmcneill#include <machine/pio.h>
471.1Sjmcneill#include "ahb.h"
481.1Sjmcneill
491.1Sjmcneill#define	USB_CHICKENBITS		0x0d0400cc
501.1Sjmcneill#define	 OHCI_INTR_ENABLE	0x000e1800
511.1Sjmcneill
521.1Sjmcneillextern struct powerpc_bus_dma_tag wii_mem2_bus_dma_tag;
531.1Sjmcneill
541.1Sjmcneillstatic int	ohci_ahb_match(device_t, cfdata_t, void *);
551.1Sjmcneillstatic void	ohci_ahb_attach(device_t, device_t, void *);
561.1Sjmcneill
571.1SjmcneillCFATTACH_DECL_NEW(ohci_ahb, sizeof(struct ohci_softc),
581.1Sjmcneill	ohci_ahb_match, ohci_ahb_attach, NULL, NULL);
591.1Sjmcneill
601.1Sjmcneillstatic int
611.1Sjmcneillohci_ahb_match(device_t parent, cfdata_t cf, void *aux)
621.1Sjmcneill{
631.1Sjmcneill	struct ahb_attach_args *aaa = aux;
641.1Sjmcneill
651.1Sjmcneill	return wiiu_native || aaa->aaa_irq < 32;
661.1Sjmcneill}
671.1Sjmcneill
681.1Sjmcneillstatic void
691.1Sjmcneillohci_ahb_attach(device_t parent, device_t self, void *aux)
701.1Sjmcneill{
711.1Sjmcneill	struct ahb_attach_args *aaa = aux;
721.1Sjmcneill	struct ohci_softc *sc = device_private(self);
731.1Sjmcneill	int error;
741.1Sjmcneill
751.1Sjmcneill	sc->sc_dev = self;
761.1Sjmcneill	sc->sc_bus.ub_hcpriv = sc;
771.1Sjmcneill	sc->sc_bus.ub_dmatag = &wii_mem2_bus_dma_tag;
781.1Sjmcneill	sc->sc_flags = 0;
791.1Sjmcneill	sc->sc_size = 0x200;
801.1Sjmcneill	sc->iot = aaa->aaa_bst;
811.1Sjmcneill	error = bus_space_map(sc->iot, aaa->aaa_addr, sc->sc_size, 0, &sc->ioh);
821.1Sjmcneill	if (error != 0) {
831.1Sjmcneill		aprint_error(": couldn't map registers (%d)\n", error);
841.1Sjmcneill		return;
851.1Sjmcneill	}
861.1Sjmcneill
871.1Sjmcneill	aprint_naive("\n");
881.1Sjmcneill	aprint_normal(": OHCI\n");
891.1Sjmcneill
901.1Sjmcneill	ahb_claim_device(self,
911.1Sjmcneill	    device_unit(self) == 0 ? IOPOH0EN : IOPOH1EN);
921.1Sjmcneill
931.1Sjmcneill	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
941.1Sjmcneill	    OHCI_ALL_INTRS);
951.1Sjmcneill
961.1Sjmcneill	out32(USB_CHICKENBITS, in32(USB_CHICKENBITS) | OHCI_INTR_ENABLE);
971.1Sjmcneill
981.1Sjmcneill	error = ohci_init(sc);
991.1Sjmcneill	if (error != 0) {
1001.1Sjmcneill		aprint_error_dev(self, "init failed, error = %d\n", error);
1011.1Sjmcneill		return;
1021.1Sjmcneill	}
1031.1Sjmcneill
1041.1Sjmcneill	ahb_intr_establish(aaa->aaa_irq, IPL_USB, ohci_intr, sc,
1051.1Sjmcneill	    device_xname(self));
1061.1Sjmcneill
1071.1Sjmcneill	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint,
1081.1Sjmcneill	    CFARGS_NONE);
1091.1Sjmcneill}
110