dma_sbus.c revision 227848
1/* $OpenBSD: dma_sbus.c,v 1.16 2008/06/26 05:42:18 ray Exp $ */ 2/* $NetBSD: dma_sbus.c,v 1.32 2008/04/28 20:23:57 martin Exp $ */ 3 4/*- 5 * Copyright (c) 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Paul Kranenburg. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33/*- 34 * Copyright (c) 1994 Peter Galbavy. All rights reserved. 35 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 */ 57 58#include <sys/cdefs.h> 59__FBSDID("$FreeBSD: head/sys/sparc64/sbus/dma_sbus.c 227848 2011-11-22 21:55:40Z marius $"); 60 61#include <sys/param.h> 62#include <sys/systm.h> 63#include <sys/bus.h> 64#include <sys/kernel.h> 65#include <sys/module.h> 66#include <sys/rman.h> 67 68#include <dev/ofw/ofw_bus.h> 69#include <dev/ofw/ofw_bus_subr.h> 70#include <dev/ofw/openfirm.h> 71 72#include <machine/bus.h> 73#include <machine/bus_common.h> 74#include <machine/resource.h> 75 76#include <sparc64/sbus/lsi64854reg.h> 77#include <sparc64/sbus/lsi64854var.h> 78#include <sparc64/sbus/ofw_sbus.h> 79#include <sparc64/sbus/sbusreg.h> 80#include <sparc64/sbus/sbusvar.h> 81 82struct dma_devinfo { 83 struct ofw_bus_devinfo ddi_obdinfo; 84 struct resource_list ddi_rl; 85}; 86 87struct dma_softc { 88 struct lsi64854_softc sc_lsi64854; /* base device */ 89 int sc_ign; 90 int sc_slot; 91}; 92 93static devclass_t dma_devclass; 94 95static device_probe_t dma_probe; 96static device_attach_t dma_attach; 97static bus_print_child_t dma_print_child; 98static bus_probe_nomatch_t dma_probe_nomatch; 99static bus_get_resource_list_t dma_get_resource_list; 100static ofw_bus_get_devinfo_t dma_get_devinfo; 101 102static struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *, 103 phandle_t); 104static void dma_destroy_dinfo(struct dma_devinfo *); 105static int dma_print_res(struct dma_devinfo *); 106 107static device_method_t dma_methods[] = { 108 /* Device interface */ 109 DEVMETHOD(device_probe, dma_probe), 110 DEVMETHOD(device_attach, dma_attach), 111 DEVMETHOD(device_shutdown, bus_generic_shutdown), 112 DEVMETHOD(device_suspend, bus_generic_suspend), 113 DEVMETHOD(device_resume, bus_generic_resume), 114 115 /* Bus interface */ 116 DEVMETHOD(bus_print_child, dma_print_child), 117 DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch), 118 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 119 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 120 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 121 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 122 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 123 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 124 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 125 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 126 DEVMETHOD(bus_get_resource_list, dma_get_resource_list), 127 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 128 129 /* ofw_bus interface */ 130 DEVMETHOD(ofw_bus_get_devinfo, dma_get_devinfo), 131 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 132 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 133 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 134 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 135 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 136 137 DEVMETHOD_END 138}; 139 140static driver_t dma_driver = { 141 "dma", 142 dma_methods, 143 sizeof(struct dma_softc), 144}; 145 146/* 147 * The probe order is handled by sbus(4) as we don't want the variants 148 * with children to be attached earlier than the stand-alone controllers 149 * in order to generally preserve the OFW device tree order. 150 */ 151EARLY_DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0, 152 BUS_PASS_DEFAULT); 153MODULE_DEPEND(dma, sbus, 1, 1, 1); 154MODULE_VERSION(dma, 1); 155 156static int 157dma_probe(device_t dev) 158{ 159 const char *name; 160 161 name = ofw_bus_get_name(dev); 162 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 || 163 strcmp(name, "ledma") == 0) { 164 device_set_desc_copy(dev, name); 165 return (0); 166 } 167 return (ENXIO); 168} 169 170static int 171dma_attach(device_t dev) 172{ 173 struct dma_softc *dsc; 174 struct lsi64854_softc *lsc; 175 struct dma_devinfo *ddi; 176 device_t cdev; 177 const char *name; 178 char *cabletype; 179 uint32_t csr; 180 phandle_t child, node; 181 int error, i; 182 183 dsc = device_get_softc(dev); 184 lsc = &dsc->sc_lsi64854; 185 186 name = ofw_bus_get_name(dev); 187 node = ofw_bus_get_node(dev); 188 dsc->sc_ign = sbus_get_ign(dev); 189 dsc->sc_slot = sbus_get_slot(dev); 190 191 i = 0; 192 lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, 193 RF_ACTIVE); 194 if (lsc->sc_res == NULL) { 195 device_printf(dev, "cannot allocate resources\n"); 196 return (ENXIO); 197 } 198 199 if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0) 200 lsc->sc_channel = L64854_CHANNEL_SCSI; 201 else if (strcmp(name, "ledma") == 0) { 202 /* 203 * Check to see which cable type is currently active and 204 * set the appropriate bit in the ledma csr so that it 205 * gets used. If we didn't netboot, the PROM won't have 206 * the "cable-selection" property; default to TP and then 207 * the user can change it via a "media" option to ifconfig. 208 */ 209 csr = L64854_GCSR(lsc); 210 if ((OF_getprop_alloc(node, "cable-selection", 1, 211 (void **)&cabletype)) == -1) { 212 /* assume TP if nothing there */ 213 csr |= E_TP_AUI; 214 } else { 215 if (strcmp(cabletype, "aui") == 0) 216 csr &= ~E_TP_AUI; 217 else 218 csr |= E_TP_AUI; 219 free(cabletype, M_OFWPROP); 220 } 221 L64854_SCSR(lsc, csr); 222 DELAY(20000); /* manual says we need a 20ms delay */ 223 lsc->sc_channel = L64854_CHANNEL_ENET; 224 } else { 225 device_printf(dev, "unsupported DMA channel\n"); 226 error = ENXIO; 227 goto fail_lres; 228 } 229 230 error = bus_dma_tag_create( 231 bus_get_dma_tag(dev), /* parent */ 232 1, 0, /* alignment, boundary */ 233 BUS_SPACE_MAXADDR, /* lowaddr */ 234 BUS_SPACE_MAXADDR, /* highaddr */ 235 NULL, NULL, /* filter, filterarg */ 236 BUS_SPACE_MAXSIZE, /* maxsize */ 237 BUS_SPACE_UNRESTRICTED, /* nsegments */ 238 BUS_SPACE_MAXSIZE, /* maxsegsize */ 239 0, /* flags */ 240 NULL, NULL, /* no locking */ 241 &lsc->sc_parent_dmat); 242 if (error != 0) { 243 device_printf(dev, "cannot allocate parent DMA tag\n"); 244 goto fail_lres; 245 } 246 247 i = sbus_get_burstsz(dev); 248 lsc->sc_burst = (i & SBUS_BURST_32) ? 32 : 249 (i & SBUS_BURST_16) ? 16 : 0; 250 lsc->sc_dev = dev; 251 252 /* Attach children. */ 253 i = 0; 254 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 255 if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL) 256 continue; 257 if (i != 0) { 258 device_printf(dev, 259 "<%s>: only one child per DMA channel supported\n", 260 ddi->ddi_obdinfo.obd_name); 261 dma_destroy_dinfo(ddi); 262 continue; 263 } 264 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) { 265 device_printf(dev, "<%s>: device_add_child failed\n", 266 ddi->ddi_obdinfo.obd_name); 267 dma_destroy_dinfo(ddi); 268 continue; 269 } 270 device_set_ivars(cdev, ddi); 271 i++; 272 } 273 return (bus_generic_attach(dev)); 274 275 fail_lres: 276 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lsc->sc_res), 277 lsc->sc_res); 278 return (error); 279} 280 281static struct dma_devinfo * 282dma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node) 283{ 284 struct dma_devinfo *ddi; 285 struct sbus_regs *reg; 286 uint32_t base, iv, *intr; 287 int i, nreg, nintr, slot, rslot; 288 289 ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO); 290 if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) { 291 free(ddi, M_DEVBUF); 292 return (NULL); 293 } 294 resource_list_init(&ddi->ddi_rl); 295 slot = -1; 296 nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 297 if (nreg == -1) { 298 device_printf(dev, "<%s>: incomplete\n", 299 ddi->ddi_obdinfo.obd_name); 300 goto fail; 301 } 302 for (i = 0; i < nreg; i++) { 303 base = reg[i].sbr_offset; 304 if (SBUS_ABS(base)) { 305 rslot = SBUS_ABS_TO_SLOT(base); 306 base = SBUS_ABS_TO_OFFSET(base); 307 } else 308 rslot = reg[i].sbr_slot; 309 if (slot != -1 && slot != rslot) { 310 device_printf(dev, "<%s>: multiple slots\n", 311 ddi->ddi_obdinfo.obd_name); 312 free(reg, M_OFWPROP); 313 goto fail; 314 } 315 slot = rslot; 316 317 resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base, 318 base + reg[i].sbr_size, reg[i].sbr_size); 319 } 320 free(reg, M_OFWPROP); 321 if (slot != dsc->sc_slot) { 322 device_printf(dev, "<%s>: parent and child slot do not match\n", 323 ddi->ddi_obdinfo.obd_name); 324 goto fail; 325 } 326 327 /* 328 * The `interrupts' property contains the SBus interrupt level. 329 */ 330 nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 331 (void **)&intr); 332 if (nintr != -1) { 333 for (i = 0; i < nintr; i++) { 334 iv = intr[i]; 335 /* 336 * SBus card devices need the slot number encoded into 337 * the vector as this is generally not done. 338 */ 339 if ((iv & INTMAP_OBIO_MASK) == 0) 340 iv |= slot << 3; 341 /* Set the IGN as appropriate. */ 342 iv |= dsc->sc_ign << INTMAP_IGN_SHIFT; 343 resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i, 344 iv, iv, 1); 345 } 346 free(intr, M_OFWPROP); 347 } 348 return (ddi); 349 350 fail: 351 dma_destroy_dinfo(ddi); 352 return (NULL); 353} 354 355static void 356dma_destroy_dinfo(struct dma_devinfo *dinfo) 357{ 358 359 resource_list_free(&dinfo->ddi_rl); 360 ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo); 361 free(dinfo, M_DEVBUF); 362} 363 364static int 365dma_print_child(device_t dev, device_t child) 366{ 367 int rv; 368 369 rv = bus_print_child_header(dev, child); 370 rv += dma_print_res(device_get_ivars(child)); 371 rv += bus_print_child_footer(dev, child); 372 return (rv); 373} 374 375static void 376dma_probe_nomatch(device_t dev, device_t child) 377{ 378 const char *type; 379 380 device_printf(dev, "<%s>", ofw_bus_get_name(child)); 381 dma_print_res(device_get_ivars(child)); 382 type = ofw_bus_get_type(child); 383 printf(" type %s (no driver attached)\n", 384 type != NULL ? type : "unknown"); 385} 386 387static struct resource_list * 388dma_get_resource_list(device_t dev, device_t child) 389{ 390 struct dma_devinfo *ddi; 391 392 ddi = device_get_ivars(child); 393 return (&ddi->ddi_rl); 394} 395 396static const struct ofw_bus_devinfo * 397dma_get_devinfo(device_t bus, device_t child) 398{ 399 struct dma_devinfo *ddi; 400 401 ddi = device_get_ivars(child); 402 return (&ddi->ddi_obdinfo); 403} 404 405static int 406dma_print_res(struct dma_devinfo *ddi) 407{ 408 int rv; 409 410 rv = 0; 411 rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY, 412 "%#lx"); 413 rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%ld"); 414 return (rv); 415} 416