1 /* $NetBSD: dwctwo_plb.c,v 1.7 2021/04/24 23:36:46 thorpej Exp $ */ 2 /* 3 * Copyright (c) 2013 KIYOHARA Takashi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: dwctwo_plb.c,v 1.7 2021/04/24 23:36:46 thorpej Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/device.h> 34 #include <sys/errno.h> 35 #include <sys/extent.h> 36 37 #include <powerpc/ibm4xx/cpu.h> 38 #include <powerpc/ibm4xx/dev/plbvar.h> 39 40 #include <dev/usb/usb.h> 41 #include <dev/usb/usbdi.h> 42 #include <dev/usb/usbdivar.h> 43 #include <dev/usb/usb_mem.h> 44 45 #include <dwc2/dwc2.h> 46 #include <dwc2/dwc2var.h> 47 #include "dwc2_core.h" 48 49 #include "locators.h" 50 51 #define DWCTWO_SIZE 0x40000 52 53 static int dwctwo_plb_match(device_t, cfdata_t, void *); 54 static void dwctwo_plb_attach(device_t, device_t, void *); 55 56 static void dwctwo_plb_deferred(device_t); 57 58 CFATTACH_DECL_NEW(dwctwo_plb, sizeof(struct dwc2_softc), 59 dwctwo_plb_match, dwctwo_plb_attach, NULL, NULL); 60 61 static struct powerpc_bus_space dwctwo_tag = { 62 _BUS_SPACE_LITTLE_ENDIAN | _BUS_SPACE_MEM_TYPE, 63 0x00000000, 64 0x00000000, 65 DWCTWO_SIZE 66 }; 67 static char ex_storage[EXTENT_FIXED_STORAGE_SIZE(8)] 68 __attribute__((aligned(8))); 69 70 71 static int 72 dwctwo_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("dwctwo_plb_match: wildcard addr not allowed"); 81 if (match->cf_loc[PLBCF_IRQ] == PLBCF_IRQ_DEFAULT) 82 panic("dwctwo_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 dwctwo_plb_attach(device_t parent, device_t self, void *aux) 91 { 92 struct dwc2_softc *sc = device_private(self); 93 struct plb_attach_args *paa = aux; 94 prop_dictionary_t dict = device_properties(self); 95 uint32_t srst0; 96 97 sc->sc_dev = self; 98 99 /* get core parameters */ 100 if (!prop_dictionary_get_uint32(dict, "params", 101 (uint32_t *)&sc->sc_params)) { 102 aprint_error("struct dwc2_core_params not found\n"); 103 return; 104 } 105 106 dwctwo_tag.pbs_base = paa->plb_addr; 107 dwctwo_tag.pbs_limit += paa->plb_addr; 108 if (bus_space_init(&dwctwo_tag, "dwctwotag", ex_storage, 109 sizeof(ex_storage))) 110 panic("dwctwo_attach: Failed to initialise opb_tag"); 111 sc->sc_iot = &dwctwo_tag; 112 bus_space_map(sc->sc_iot, paa->plb_addr, DWCTWO_SIZE, 0, &sc->sc_ioh); 113 sc->sc_bus.ub_dmatag = paa->plb_dmat; 114 115 intr_establish_xname(paa->plb_irq, IST_LEVEL, IPL_VM, dwc2_intr, sc, 116 device_xname(self)); 117 118 /* Enable the USB interface. */ 119 mtsdr(DCR_SDR0_PFC1, mfsdr(DCR_SDR0_PFC1) | SDR0_PFC1_USBEN); 120 srst0 = mfsdr(DCR_SDR0_SRST0); 121 mtsdr(DCR_SDR0_SRST0, srst0 | SDR0_SRST0_UPRST | SDR0_SRST0_AHB); 122 delay(200 * 1000); /* XXXX */ 123 mtsdr(DCR_SDR0_SRST0, srst0); 124 125 config_defer(self, dwctwo_plb_deferred); 126 } 127 128 static void 129 dwctwo_plb_deferred(device_t self) 130 { 131 struct dwc2_softc *sc = device_private(self); 132 int error; 133 134 error = dwc2_init(sc); 135 if (error != 0) { 136 aprint_error_dev(self, "couldn't initialize host, error=%d\n", 137 error); 138 return; 139 } 140 sc->sc_child = config_found(sc->sc_dev, &sc->sc_bus, usbctlprint, 141 CFARG_EOL); 142 } 143