dma_sbus.c revision 155067
1146392Smarius/* $OpenBSD: dma_sbus.c,v 1.12 2005/03/03 01:41:45 miod Exp $ */ 2146392Smarius/* $NetBSD: dma_sbus.c,v 1.5 2000/07/09 20:57:42 pk Exp $ */ 3146392Smarius 4146392Smarius/*- 5146392Smarius * Copyright (c) 1998 The NetBSD Foundation, Inc. 6146392Smarius * All rights reserved. 7146392Smarius * 8146392Smarius * This code is derived from software contributed to The NetBSD Foundation 9146392Smarius * by Paul Kranenburg. 10146392Smarius * 11146392Smarius * Redistribution and use in source and binary forms, with or without 12146392Smarius * modification, are permitted provided that the following conditions 13146392Smarius * are met: 14146392Smarius * 1. Redistributions of source code must retain the above copyright 15146392Smarius * notice, this list of conditions and the following disclaimer. 16146392Smarius * 2. Redistributions in binary form must reproduce the above copyright 17146392Smarius * notice, this list of conditions and the following disclaimer in the 18146392Smarius * documentation and/or other materials provided with the distribution. 19146392Smarius * 3. All advertising materials mentioning features or use of this software 20146392Smarius * must display the following acknowledgement: 21146392Smarius * This product includes software developed by the NetBSD 22146392Smarius * Foundation, Inc. and its contributors. 23146392Smarius * 4. Neither the name of The NetBSD Foundation nor the names of its 24146392Smarius * contributors may be used to endorse or promote products derived 25146392Smarius * from this software without specific prior written permission. 26146392Smarius * 27146392Smarius * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28146392Smarius * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29146392Smarius * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30146392Smarius * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31146392Smarius * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32146392Smarius * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33146392Smarius * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34146392Smarius * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35146392Smarius * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36146392Smarius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37146392Smarius * POSSIBILITY OF SUCH DAMAGE. 38146392Smarius */ 39146392Smarius 40146392Smarius/*- 41146392Smarius * Copyright (c) 1994 Peter Galbavy. All rights reserved. 42146392Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved. 43146392Smarius * 44146392Smarius * Redistribution and use in source and binary forms, with or without 45146392Smarius * modification, are permitted provided that the following conditions 46146392Smarius * are met: 47146392Smarius * 1. Redistributions of source code must retain the above copyright 48146392Smarius * notice, this list of conditions and the following disclaimer. 49146392Smarius * 2. Redistributions in binary form must reproduce the above copyright 50146392Smarius * notice, this list of conditions and the following disclaimer in the 51146392Smarius * documentation and/or other materials provided with the distribution. 52146392Smarius * 53146392Smarius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 54146392Smarius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 55146392Smarius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 56146392Smarius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 57146392Smarius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 58146392Smarius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 59146392Smarius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 60146392Smarius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 61146392Smarius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 62146392Smarius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 63146392Smarius */ 64146392Smarius 65146392Smarius#include <sys/cdefs.h> 66146392Smarius__FBSDID("$FreeBSD: head/sys/sparc64/sbus/dma_sbus.c 155067 2006-01-30 21:43:14Z marius $"); 67146392Smarius 68146392Smarius#include <sys/param.h> 69146392Smarius#include <sys/systm.h> 70146392Smarius#include <sys/bus.h> 71146392Smarius#include <sys/kernel.h> 72146392Smarius#include <sys/module.h> 73146392Smarius#include <sys/resource.h> 74146392Smarius#include <sys/rman.h> 75146392Smarius 76146392Smarius#include <dev/ofw/ofw_bus.h> 77152684Smarius#include <dev/ofw/ofw_bus_subr.h> 78146392Smarius#include <dev/ofw/openfirm.h> 79146392Smarius 80146392Smarius#include <machine/bus.h> 81146392Smarius#include <machine/bus_common.h> 82146392Smarius#include <machine/resource.h> 83146392Smarius 84146392Smarius#include <sparc64/sbus/lsi64854reg.h> 85146392Smarius#include <sparc64/sbus/lsi64854var.h> 86146392Smarius#include <sparc64/sbus/ofw_sbus.h> 87146392Smarius#include <sparc64/sbus/sbusreg.h> 88146392Smarius#include <sparc64/sbus/sbusvar.h> 89146392Smarius 90146392Smariusstruct dma_devinfo { 91152684Smarius struct ofw_bus_devinfo ddi_obdinfo; 92146392Smarius struct resource_list ddi_rl; 93146392Smarius}; 94146392Smarius 95146392Smariusstruct dma_softc { 96146392Smarius struct lsi64854_softc sc_lsi64854; /* base device */ 97146392Smarius int sc_ign; 98146392Smarius int sc_slot; 99146392Smarius}; 100146392Smarius 101146392Smariusstatic devclass_t dma_devclass; 102146392Smarius 103146392Smariusstatic device_probe_t dma_probe; 104146392Smariusstatic device_attach_t dma_attach; 105146392Smariusstatic bus_print_child_t dma_print_child; 106146392Smariusstatic bus_probe_nomatch_t dma_probe_nomatch; 107146392Smariusstatic bus_get_resource_list_t dma_get_resource_list; 108152684Smariusstatic ofw_bus_get_devinfo_t dma_get_devinfo; 109146392Smarius 110152684Smariusstatic struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *, 111152684Smarius phandle_t); 112146392Smariusstatic void dma_destroy_dinfo(struct dma_devinfo *); 113152684Smariusstatic int dma_print_res(struct dma_devinfo *); 114146392Smarius 115146392Smariusstatic device_method_t dma_methods[] = { 116146392Smarius /* Device interface */ 117146392Smarius DEVMETHOD(device_probe, dma_probe), 118146392Smarius DEVMETHOD(device_attach, dma_attach), 119154870Smarius DEVMETHOD(device_shutdown, bus_generic_shutdown), 120154870Smarius DEVMETHOD(device_suspend, bus_generic_suspend), 121154870Smarius DEVMETHOD(device_resume, bus_generic_resume), 122146392Smarius 123146392Smarius /* Bus interface */ 124146392Smarius DEVMETHOD(bus_print_child, dma_print_child), 125146392Smarius DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch), 126155067Smarius DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 127146392Smarius DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 128146392Smarius DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 129146392Smarius DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 130146392Smarius DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 131146392Smarius DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 132146392Smarius DEVMETHOD(bus_get_resource_list, dma_get_resource_list), 133146392Smarius DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 134146392Smarius 135146392Smarius /* ofw_bus interface */ 136152684Smarius DEVMETHOD(ofw_bus_get_devinfo, dma_get_devinfo), 137152684Smarius DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 138152684Smarius DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 139152684Smarius DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 140152684Smarius DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 141152684Smarius DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 142146392Smarius 143146392Smarius { 0, 0 } 144146392Smarius}; 145146392Smarius 146146392Smariusstatic driver_t dma_driver = { 147146392Smarius "dma", 148146392Smarius dma_methods, 149146392Smarius sizeof(struct dma_softc), 150146392Smarius}; 151146392Smarius 152146392SmariusDRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0); 153146392Smarius 154146392Smariusstatic int 155146392Smariusdma_probe(device_t dev) 156146392Smarius{ 157146392Smarius const char *name; 158146392Smarius 159146392Smarius name = ofw_bus_get_name(dev); 160146392Smarius if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 || 161146392Smarius strcmp(name, "ledma") == 0) { 162146392Smarius device_set_desc_copy(dev, name); 163146392Smarius return (0); 164146392Smarius } 165146392Smarius return (ENXIO); 166146392Smarius} 167146392Smarius 168146392Smariusstatic int 169146392Smariusdma_attach(device_t dev) 170146392Smarius{ 171146392Smarius struct dma_softc *dsc; 172146392Smarius struct lsi64854_softc *lsc; 173146392Smarius struct dma_devinfo *ddi; 174146392Smarius device_t cdev; 175146392Smarius const char *name; 176152684Smarius char *cabletype; 177146392Smarius uint32_t csr; 178146392Smarius phandle_t child, node; 179146392Smarius int error, burst, children; 180146392Smarius 181146392Smarius dsc = device_get_softc(dev); 182146392Smarius lsc = &dsc->sc_lsi64854; 183146392Smarius 184146392Smarius name = ofw_bus_get_name(dev); 185146392Smarius node = ofw_bus_get_node(dev); 186146392Smarius dsc->sc_ign = sbus_get_ign(dev); 187146392Smarius dsc->sc_slot = sbus_get_slot(dev); 188146392Smarius 189146392Smarius lsc->sc_rid = 0; 190146392Smarius lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &lsc->sc_rid, 191146392Smarius RF_ACTIVE); 192146392Smarius if (lsc->sc_res == NULL) { 193146392Smarius device_printf(dev, "cannot allocate resources\n"); 194146392Smarius return (ENXIO); 195146392Smarius } 196146392Smarius lsc->sc_regt = rman_get_bustag(lsc->sc_res); 197146392Smarius lsc->sc_regh = rman_get_bushandle(lsc->sc_res); 198146392Smarius 199146392Smarius if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0) 200146392Smarius lsc->sc_channel = L64854_CHANNEL_SCSI; 201146392Smarius else if (strcmp(name, "ledma") == 0) { 202146392Smarius /* 203146392Smarius * Check to see which cable type is currently active and 204146392Smarius * set the appropriate bit in the ledma csr so that it 205146392Smarius * gets used. If we didn't netboot, the PROM won't have 206146392Smarius * the "cable-selection" property; default to TP and then 207146392Smarius * the user can change it via a "media" option to ifconfig. 208146392Smarius */ 209146392Smarius csr = L64854_GCSR(lsc); 210146392Smarius if ((OF_getprop_alloc(node, "cable-selection", 1, 211146392Smarius (void **)&cabletype)) == -1) { 212146392Smarius /* assume TP if nothing there */ 213146392Smarius csr |= E_TP_AUI; 214146392Smarius } else { 215146392Smarius if (strcmp(cabletype, "aui") == 0) 216146392Smarius csr &= ~E_TP_AUI; 217146392Smarius else 218146392Smarius csr |= E_TP_AUI; 219146392Smarius free(cabletype, M_OFWPROP); 220146392Smarius } 221146392Smarius L64854_SCSR(lsc, csr); 222146392Smarius DELAY(20000); /* manual says we need a 20ms delay */ 223146392Smarius lsc->sc_channel = L64854_CHANNEL_ENET; 224146392Smarius } else { 225146392Smarius device_printf(dev, "unsupported DMA channel\n"); 226146392Smarius error = ENXIO; 227146392Smarius goto fail_lres; 228146392Smarius } 229146392Smarius 230146392Smarius error = bus_dma_tag_create( 231146392Smarius NULL, /* parent */ 232146392Smarius PAGE_SIZE, 0, /* alignment, boundary */ 233146392Smarius BUS_SPACE_MAXADDR, /* lowaddr */ 234146392Smarius BUS_SPACE_MAXADDR, /* highaddr */ 235146392Smarius NULL, NULL, /* filter, filterarg */ 236146392Smarius BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 237146392Smarius 0, /* nsegments */ 238146392Smarius BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 239146392Smarius 0, /* flags */ 240146392Smarius NULL, NULL, /* no locking */ 241146392Smarius &lsc->sc_parent_dmat); 242146392Smarius if (error != 0) { 243146392Smarius device_printf(dev, "cannot allocate parent DMA tag\n"); 244146392Smarius goto fail_lres; 245146392Smarius } 246146392Smarius 247146392Smarius burst = sbus_get_burstsz(dev); 248146392Smarius lsc->sc_burst = (burst & SBUS_BURST_32) ? 32 : 249146392Smarius (burst & SBUS_BURST_16) ? 16 : 0; 250146392Smarius lsc->sc_dev = dev; 251146392Smarius 252146392Smarius error = lsi64854_attach(lsc); 253146392Smarius if (error != 0) { 254146392Smarius device_printf(dev, "lsi64854_attach failed\n"); 255146392Smarius goto fail_lpdma; 256146392Smarius } 257146392Smarius 258146392Smarius /* Attach children. */ 259146392Smarius children = 0; 260146392Smarius for (child = OF_child(node); child != 0; child = OF_peer(child)) { 261152684Smarius if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL) 262146392Smarius continue; 263146392Smarius if (children != 0) { 264152684Smarius device_printf(dev, 265152684Smarius "<%s>: only one child per DMA channel supported\n", 266152684Smarius ddi->ddi_obdinfo.obd_name); 267146392Smarius dma_destroy_dinfo(ddi); 268146392Smarius continue; 269146392Smarius } 270146392Smarius if ((cdev = device_add_child(dev, NULL, -1)) == NULL) { 271146392Smarius device_printf(dev, "<%s>: device_add_child failed\n", 272152684Smarius ddi->ddi_obdinfo.obd_name); 273146392Smarius dma_destroy_dinfo(ddi); 274146392Smarius continue; 275146392Smarius } 276146392Smarius device_set_ivars(cdev, ddi); 277146392Smarius children++; 278146392Smarius } 279152684Smarius return (bus_generic_attach(dev)); 280146392Smarius 281146392Smarius fail_lpdma: 282146392Smarius bus_dma_tag_destroy(lsc->sc_parent_dmat); 283146392Smarius fail_lres: 284146392Smarius bus_release_resource(dev, SYS_RES_MEMORY, lsc->sc_rid, lsc->sc_res); 285146392Smarius return (error); 286146392Smarius} 287146392Smarius 288146392Smariusstatic struct dma_devinfo * 289152684Smariusdma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node) 290146392Smarius{ 291146392Smarius struct dma_devinfo *ddi; 292146392Smarius struct sbus_regs *reg; 293146392Smarius uint32_t base, iv, *intr; 294146392Smarius int i, nreg, nintr, slot, rslot; 295146392Smarius 296146392Smarius ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO); 297152684Smarius if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) { 298152684Smarius free(ddi, M_DEVBUF); 299146392Smarius return (NULL); 300152684Smarius } 301146392Smarius resource_list_init(&ddi->ddi_rl); 302146392Smarius slot = -1; 303146392Smarius nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 304146392Smarius if (nreg == -1) { 305152684Smarius device_printf(dev, "<%s>: incomplete\n", 306152684Smarius ddi->ddi_obdinfo.obd_name); 307152684Smarius goto fail; 308146392Smarius } 309146392Smarius for (i = 0; i < nreg; i++) { 310146392Smarius base = reg[i].sbr_offset; 311146392Smarius if (SBUS_ABS(base)) { 312146392Smarius rslot = SBUS_ABS_TO_SLOT(base); 313146392Smarius base = SBUS_ABS_TO_OFFSET(base); 314146392Smarius } else 315146392Smarius rslot = reg[i].sbr_slot; 316146392Smarius if (slot != -1 && slot != rslot) { 317152684Smarius device_printf(dev, "<%s>: multiple slots\n", 318152684Smarius ddi->ddi_obdinfo.obd_name); 319146392Smarius free(reg, M_OFWPROP); 320152684Smarius goto fail; 321146392Smarius } 322146392Smarius slot = rslot; 323146392Smarius 324146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base, 325146392Smarius base + reg[i].sbr_size, reg[i].sbr_size); 326146392Smarius } 327146392Smarius free(reg, M_OFWPROP); 328146392Smarius if (slot != dsc->sc_slot) { 329146392Smarius device_printf(dev, "<%s>: parent and child slot do not match\n", 330152684Smarius ddi->ddi_obdinfo.obd_name); 331152684Smarius goto fail; 332146392Smarius } 333146392Smarius 334146392Smarius /* 335146392Smarius * The `interrupts' property contains the SBus interrupt level. 336146392Smarius */ 337146392Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 338146392Smarius (void **)&intr); 339146392Smarius if (nintr != -1) { 340146392Smarius for (i = 0; i < nintr; i++) { 341146392Smarius iv = intr[i]; 342146392Smarius /* 343146392Smarius * SBus card devices need the slot number encoded into 344146392Smarius * the vector as this is generally not done. 345146392Smarius */ 346146392Smarius if ((iv & INTMAP_OBIO_MASK) == 0) 347146392Smarius iv |= slot << 3; 348146392Smarius /* Set the IGN as appropriate. */ 349146392Smarius iv |= dsc->sc_ign << INTMAP_IGN_SHIFT; 350146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i, 351146392Smarius iv, iv, 1); 352146392Smarius } 353146392Smarius free(intr, M_OFWPROP); 354146392Smarius } 355146392Smarius return (ddi); 356152684Smarius 357152684Smarius fail: 358152684Smarius dma_destroy_dinfo(ddi); 359152684Smarius return (NULL); 360146392Smarius} 361146392Smarius 362146392Smariusstatic void 363146392Smariusdma_destroy_dinfo(struct dma_devinfo *dinfo) 364146392Smarius{ 365146392Smarius 366146392Smarius resource_list_free(&dinfo->ddi_rl); 367152684Smarius ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo); 368146392Smarius free(dinfo, M_DEVBUF); 369146392Smarius} 370146392Smarius 371146392Smariusstatic int 372146392Smariusdma_print_child(device_t dev, device_t child) 373146392Smarius{ 374146392Smarius int rv; 375146392Smarius 376146392Smarius rv = bus_print_child_header(dev, child); 377152684Smarius rv += dma_print_res(device_get_ivars(child)); 378146392Smarius rv += bus_print_child_footer(dev, child); 379146392Smarius return (rv); 380146392Smarius} 381146392Smarius 382146392Smariusstatic void 383146392Smariusdma_probe_nomatch(device_t dev, device_t child) 384146392Smarius{ 385152684Smarius const char *type; 386146392Smarius 387152684Smarius device_printf(dev, "<%s>", ofw_bus_get_name(child)); 388152684Smarius dma_print_res(device_get_ivars(child)); 389152684Smarius type = ofw_bus_get_type(child); 390152684Smarius printf(" type %s (no driver attached)\n", 391152684Smarius type != NULL ? type : "unknown"); 392146392Smarius} 393146392Smarius 394146392Smariusstatic struct resource_list * 395146392Smariusdma_get_resource_list(device_t dev, device_t child) 396146392Smarius{ 397146392Smarius struct dma_devinfo *ddi; 398146392Smarius 399146392Smarius ddi = device_get_ivars(child); 400146392Smarius return (&ddi->ddi_rl); 401146392Smarius} 402146392Smarius 403152684Smariusstatic const struct ofw_bus_devinfo * 404152684Smariusdma_get_devinfo(device_t bus, device_t child) 405146392Smarius{ 406152684Smarius struct dma_devinfo *ddi; 407146392Smarius 408152684Smarius ddi = device_get_ivars(child); 409152684Smarius return (&ddi->ddi_obdinfo); 410146392Smarius} 411146392Smarius 412152684Smariusstatic int 413152684Smariusdma_print_res(struct dma_devinfo *ddi) 414146392Smarius{ 415152684Smarius int rv; 416146392Smarius 417152684Smarius rv = 0; 418152684Smarius rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY, 419152684Smarius "%#lx"); 420152684Smarius rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%ld"); 421152684Smarius return (rv); 422146392Smarius} 423