1 /* $NetBSD: ralink_mainbus.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $ */ 2 /*- 3 * Copyright (c) 2011 CradlePoint Technology, Inc. 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: ralink_mainbus.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $"); 31 32 #include "locators.h" 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/device.h> 36 37 #include <mips/cache.h> 38 #include <mips/cpuregs.h> 39 40 #include <mips/ralink/ralink_reg.h> 41 #include <mips/ralink/ralink_var.h> 42 43 typedef struct mainbus_softc { 44 device_t sc_dev; 45 bus_space_tag_t sc_memt; 46 bus_dma_tag_t sc_dmat; 47 } mainbus_softc_t; 48 49 static int mainbus_match(device_t, cfdata_t, void *); 50 static void mainbus_attach(device_t, device_t, void *); 51 static int mainbus_search(device_t, cfdata_t, const int *, void *); 52 static int mainbus_print(void *, const char *); 53 static int mainbus_find(device_t, cfdata_t, const int *, void *); 54 static void mainbus_attach_critical(struct mainbus_softc *); 55 56 57 CFATTACH_DECL_NEW(mainbus, sizeof(struct mainbus_softc), 58 mainbus_match, mainbus_attach, NULL, NULL); 59 60 static bool mainbus_found; 61 62 /* 63 * critical devices, if found, will be attached in the order listed here 64 */ 65 static const struct { 66 const char *name; 67 bool required; 68 } critical_devs[] = { 69 { .name = "cpu0", .required = true }, 70 { .name = "rwdog0", .required = false }, 71 { .name = "rgpio0", .required = true }, 72 { .name = "ri2c0", .required = false }, 73 { .name = "com0", .required = false }, 74 }; 75 76 77 static int 78 mainbus_match(device_t parent, cfdata_t match, void *aux) 79 { 80 if (mainbus_found) 81 return 0; 82 return 1; 83 } 84 85 static void 86 mainbus_attach(device_t parent, device_t self, void *aux) 87 { 88 mainbus_softc_t * const sc = device_private(self); 89 struct mainbus_attach_args ma; 90 union { 91 char buf[9]; 92 uint32_t id[2]; 93 } xid; 94 95 mainbus_found = true; 96 97 sc->sc_dev = self; 98 sc->sc_memt = &ra_bus_memt; 99 sc->sc_dmat = &ra_bus_dmat; 100 101 xid.id[0] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID0); 102 xid.id[1] = bus_space_read_4(sc->sc_memt, ra_sysctl_bsh, RA_SYSCTL_ID1); 103 xid.buf[8] = '\0'; 104 if (xid.buf[6] == ' ') { 105 xid.buf[6] = '\0'; 106 } else if (xid.buf[7] == ' ') { 107 xid.buf[7] = '\0'; 108 } 109 const char * const manuf = xid.buf[0] == 'M' ? "Mediatek" : "Ralink"; 110 111 aprint_naive(": %s %s System Bus\n", manuf, xid.buf); 112 aprint_normal(": %s %s System Bus\n", manuf, xid.buf); 113 114 ma.ma_name = NULL; 115 ma.ma_memt = sc->sc_memt; 116 ma.ma_dmat = sc->sc_dmat; 117 118 /* attach critical devices */ 119 mainbus_attach_critical(sc); 120 121 /* attach other devices */ 122 config_search(self, &ma, 123 CFARGS(.search = mainbus_search)); 124 } 125 126 static int 127 mainbus_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 128 { 129 struct mainbus_attach_args *ma; 130 131 ma = aux; 132 ma->ma_addr = cf->cf_loc[MAINBUSCF_ADDR]; 133 134 if (config_probe(parent, cf, aux)) 135 config_attach(parent, cf, aux, mainbus_print, CFARGS_NONE); 136 else 137 mainbus_print(aux, cf->cf_name); 138 return 0; 139 } 140 141 int 142 mainbus_print(void *aux, const char *pnp) 143 { 144 struct mainbus_attach_args *ma; 145 146 if (pnp) 147 aprint_normal("%s unconfigured\n", pnp); 148 149 ma = aux; 150 if (ma->ma_addr != MAINBUSCF_ADDR_DEFAULT) 151 aprint_normal(" addr 0x%llx", ma->ma_addr); 152 153 return UNCONF; 154 } 155 156 static int 157 mainbus_find(device_t parent, cfdata_t cf, const int *ldesc, void *aux) 158 { 159 struct mainbus_attach_args * const ma = aux; 160 char devname[16]; 161 162 snprintf(devname, sizeof(devname), "%s%d", cf->cf_name, cf->cf_unit); 163 if (strcmp(ma->ma_name, devname) != 0) 164 return 0; 165 166 return config_match(parent, cf, ma); 167 } 168 169 static void 170 mainbus_attach_critical(struct mainbus_softc *sc) 171 { 172 struct mainbus_attach_args ma; 173 cfdata_t cf; 174 size_t i; 175 176 for (i = 0; i < __arraycount(critical_devs); i++) { 177 ma.ma_name = critical_devs[i].name; 178 ma.ma_memt = sc->sc_memt; 179 ma.ma_dmat = sc->sc_dmat; 180 181 cf = config_search(sc->sc_dev, &ma, 182 CFARGS(.submatch = mainbus_find)); 183 if (cf == NULL && critical_devs[i].required) 184 panic("%s: failed to find %s", 185 __func__, critical_devs[i].name); 186 187 ma.ma_addr = cf->cf_loc[MAINBUSCF_ADDR]; 188 config_attach(sc->sc_dev, cf, &ma, mainbus_print, CFARGS_NONE); 189 } 190 } 191