dma_sbus.c revision 182062
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 182062 2008-08-23 15:20:33Z 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[] = { 116166147Smarius /* 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); 153182062SmariusMODULE_DEPEND(dma, sbus, 1, 1, 1); 154182062SmariusMODULE_VERSION(dma, 1); 155146392Smarius 156146392Smariusstatic int 157146392Smariusdma_probe(device_t dev) 158146392Smarius{ 159146392Smarius const char *name; 160146392Smarius 161146392Smarius name = ofw_bus_get_name(dev); 162146392Smarius if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0 || 163146392Smarius strcmp(name, "ledma") == 0) { 164166147Smarius device_set_desc_copy(dev, name); 165166147Smarius return (0); 166146392Smarius } 167166147Smarius return (ENXIO); 168146392Smarius} 169146392Smarius 170146392Smariusstatic int 171146392Smariusdma_attach(device_t dev) 172146392Smarius{ 173146392Smarius struct dma_softc *dsc; 174146392Smarius struct lsi64854_softc *lsc; 175146392Smarius struct dma_devinfo *ddi; 176146392Smarius device_t cdev; 177146392Smarius const char *name; 178152684Smarius char *cabletype; 179146392Smarius uint32_t csr; 180146392Smarius phandle_t child, node; 181146392Smarius int error, burst, children; 182146392Smarius 183146392Smarius dsc = device_get_softc(dev); 184146392Smarius lsc = &dsc->sc_lsi64854; 185146392Smarius 186146392Smarius name = ofw_bus_get_name(dev); 187146392Smarius node = ofw_bus_get_node(dev); 188146392Smarius dsc->sc_ign = sbus_get_ign(dev); 189146392Smarius dsc->sc_slot = sbus_get_slot(dev); 190146392Smarius 191146392Smarius lsc->sc_rid = 0; 192146392Smarius lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &lsc->sc_rid, 193146392Smarius RF_ACTIVE); 194146392Smarius if (lsc->sc_res == NULL) { 195146392Smarius device_printf(dev, "cannot allocate resources\n"); 196146392Smarius return (ENXIO); 197146392Smarius } 198146392Smarius lsc->sc_regt = rman_get_bustag(lsc->sc_res); 199146392Smarius lsc->sc_regh = rman_get_bushandle(lsc->sc_res); 200146392Smarius 201146392Smarius if (strcmp(name, "espdma") == 0 || strcmp(name, "dma") == 0) 202146392Smarius lsc->sc_channel = L64854_CHANNEL_SCSI; 203146392Smarius else if (strcmp(name, "ledma") == 0) { 204146392Smarius /* 205146392Smarius * Check to see which cable type is currently active and 206146392Smarius * set the appropriate bit in the ledma csr so that it 207146392Smarius * gets used. If we didn't netboot, the PROM won't have 208146392Smarius * the "cable-selection" property; default to TP and then 209146392Smarius * the user can change it via a "media" option to ifconfig. 210146392Smarius */ 211146392Smarius csr = L64854_GCSR(lsc); 212146392Smarius if ((OF_getprop_alloc(node, "cable-selection", 1, 213146392Smarius (void **)&cabletype)) == -1) { 214146392Smarius /* assume TP if nothing there */ 215146392Smarius csr |= E_TP_AUI; 216146392Smarius } else { 217146392Smarius if (strcmp(cabletype, "aui") == 0) 218146392Smarius csr &= ~E_TP_AUI; 219146392Smarius else 220146392Smarius csr |= E_TP_AUI; 221146392Smarius free(cabletype, M_OFWPROP); 222146392Smarius } 223146392Smarius L64854_SCSR(lsc, csr); 224146392Smarius DELAY(20000); /* manual says we need a 20ms delay */ 225146392Smarius lsc->sc_channel = L64854_CHANNEL_ENET; 226146392Smarius } else { 227146392Smarius device_printf(dev, "unsupported DMA channel\n"); 228146392Smarius error = ENXIO; 229146392Smarius goto fail_lres; 230146392Smarius } 231146392Smarius 232146392Smarius error = bus_dma_tag_create( 233166147Smarius bus_get_dma_tag(dev), /* parent */ 234166147Smarius 1, 0, /* alignment, boundary */ 235146392Smarius BUS_SPACE_MAXADDR, /* lowaddr */ 236146392Smarius BUS_SPACE_MAXADDR, /* highaddr */ 237146392Smarius NULL, NULL, /* filter, filterarg */ 238146392Smarius BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 239146392Smarius 0, /* nsegments */ 240146392Smarius BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 241146392Smarius 0, /* flags */ 242146392Smarius NULL, NULL, /* no locking */ 243146392Smarius &lsc->sc_parent_dmat); 244146392Smarius if (error != 0) { 245146392Smarius device_printf(dev, "cannot allocate parent DMA tag\n"); 246146392Smarius goto fail_lres; 247146392Smarius } 248146392Smarius 249146392Smarius burst = sbus_get_burstsz(dev); 250146392Smarius lsc->sc_burst = (burst & SBUS_BURST_32) ? 32 : 251146392Smarius (burst & SBUS_BURST_16) ? 16 : 0; 252146392Smarius lsc->sc_dev = dev; 253146392Smarius 254146392Smarius error = lsi64854_attach(lsc); 255146392Smarius if (error != 0) { 256146392Smarius device_printf(dev, "lsi64854_attach failed\n"); 257146392Smarius goto fail_lpdma; 258146392Smarius } 259146392Smarius 260146392Smarius /* Attach children. */ 261146392Smarius children = 0; 262146392Smarius for (child = OF_child(node); child != 0; child = OF_peer(child)) { 263152684Smarius if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL) 264146392Smarius continue; 265146392Smarius if (children != 0) { 266152684Smarius device_printf(dev, 267152684Smarius "<%s>: only one child per DMA channel supported\n", 268152684Smarius ddi->ddi_obdinfo.obd_name); 269146392Smarius dma_destroy_dinfo(ddi); 270146392Smarius continue; 271146392Smarius } 272146392Smarius if ((cdev = device_add_child(dev, NULL, -1)) == NULL) { 273146392Smarius device_printf(dev, "<%s>: device_add_child failed\n", 274152684Smarius ddi->ddi_obdinfo.obd_name); 275146392Smarius dma_destroy_dinfo(ddi); 276146392Smarius continue; 277146392Smarius } 278146392Smarius device_set_ivars(cdev, ddi); 279146392Smarius children++; 280146392Smarius } 281152684Smarius return (bus_generic_attach(dev)); 282146392Smarius 283146392Smarius fail_lpdma: 284146392Smarius bus_dma_tag_destroy(lsc->sc_parent_dmat); 285146392Smarius fail_lres: 286146392Smarius bus_release_resource(dev, SYS_RES_MEMORY, lsc->sc_rid, lsc->sc_res); 287146392Smarius return (error); 288146392Smarius} 289146392Smarius 290146392Smariusstatic struct dma_devinfo * 291152684Smariusdma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node) 292146392Smarius{ 293146392Smarius struct dma_devinfo *ddi; 294146392Smarius struct sbus_regs *reg; 295146392Smarius uint32_t base, iv, *intr; 296146392Smarius int i, nreg, nintr, slot, rslot; 297146392Smarius 298146392Smarius ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO); 299152684Smarius if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) { 300152684Smarius free(ddi, M_DEVBUF); 301146392Smarius return (NULL); 302152684Smarius } 303146392Smarius resource_list_init(&ddi->ddi_rl); 304146392Smarius slot = -1; 305146392Smarius nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 306146392Smarius if (nreg == -1) { 307152684Smarius device_printf(dev, "<%s>: incomplete\n", 308152684Smarius ddi->ddi_obdinfo.obd_name); 309152684Smarius goto fail; 310146392Smarius } 311146392Smarius for (i = 0; i < nreg; i++) { 312146392Smarius base = reg[i].sbr_offset; 313146392Smarius if (SBUS_ABS(base)) { 314146392Smarius rslot = SBUS_ABS_TO_SLOT(base); 315146392Smarius base = SBUS_ABS_TO_OFFSET(base); 316146392Smarius } else 317146392Smarius rslot = reg[i].sbr_slot; 318146392Smarius if (slot != -1 && slot != rslot) { 319152684Smarius device_printf(dev, "<%s>: multiple slots\n", 320152684Smarius ddi->ddi_obdinfo.obd_name); 321146392Smarius free(reg, M_OFWPROP); 322152684Smarius goto fail; 323146392Smarius } 324146392Smarius slot = rslot; 325146392Smarius 326146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base, 327146392Smarius base + reg[i].sbr_size, reg[i].sbr_size); 328146392Smarius } 329146392Smarius free(reg, M_OFWPROP); 330146392Smarius if (slot != dsc->sc_slot) { 331146392Smarius device_printf(dev, "<%s>: parent and child slot do not match\n", 332152684Smarius ddi->ddi_obdinfo.obd_name); 333152684Smarius goto fail; 334146392Smarius } 335146392Smarius 336146392Smarius /* 337146392Smarius * The `interrupts' property contains the SBus interrupt level. 338146392Smarius */ 339146392Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 340146392Smarius (void **)&intr); 341146392Smarius if (nintr != -1) { 342146392Smarius for (i = 0; i < nintr; i++) { 343146392Smarius iv = intr[i]; 344146392Smarius /* 345146392Smarius * SBus card devices need the slot number encoded into 346146392Smarius * the vector as this is generally not done. 347146392Smarius */ 348146392Smarius if ((iv & INTMAP_OBIO_MASK) == 0) 349146392Smarius iv |= slot << 3; 350146392Smarius /* Set the IGN as appropriate. */ 351146392Smarius iv |= dsc->sc_ign << INTMAP_IGN_SHIFT; 352146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i, 353146392Smarius iv, iv, 1); 354146392Smarius } 355146392Smarius free(intr, M_OFWPROP); 356146392Smarius } 357146392Smarius return (ddi); 358152684Smarius 359152684Smarius fail: 360152684Smarius dma_destroy_dinfo(ddi); 361152684Smarius return (NULL); 362146392Smarius} 363146392Smarius 364146392Smariusstatic void 365146392Smariusdma_destroy_dinfo(struct dma_devinfo *dinfo) 366146392Smarius{ 367146392Smarius 368146392Smarius resource_list_free(&dinfo->ddi_rl); 369152684Smarius ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo); 370146392Smarius free(dinfo, M_DEVBUF); 371146392Smarius} 372146392Smarius 373146392Smariusstatic int 374146392Smariusdma_print_child(device_t dev, device_t child) 375146392Smarius{ 376146392Smarius int rv; 377146392Smarius 378146392Smarius rv = bus_print_child_header(dev, child); 379152684Smarius rv += dma_print_res(device_get_ivars(child)); 380146392Smarius rv += bus_print_child_footer(dev, child); 381146392Smarius return (rv); 382146392Smarius} 383146392Smarius 384146392Smariusstatic void 385146392Smariusdma_probe_nomatch(device_t dev, device_t child) 386146392Smarius{ 387152684Smarius const char *type; 388146392Smarius 389152684Smarius device_printf(dev, "<%s>", ofw_bus_get_name(child)); 390152684Smarius dma_print_res(device_get_ivars(child)); 391152684Smarius type = ofw_bus_get_type(child); 392152684Smarius printf(" type %s (no driver attached)\n", 393152684Smarius type != NULL ? type : "unknown"); 394146392Smarius} 395146392Smarius 396146392Smariusstatic struct resource_list * 397146392Smariusdma_get_resource_list(device_t dev, device_t child) 398146392Smarius{ 399146392Smarius struct dma_devinfo *ddi; 400146392Smarius 401146392Smarius ddi = device_get_ivars(child); 402146392Smarius return (&ddi->ddi_rl); 403146392Smarius} 404146392Smarius 405152684Smariusstatic const struct ofw_bus_devinfo * 406152684Smariusdma_get_devinfo(device_t bus, device_t child) 407146392Smarius{ 408152684Smarius struct dma_devinfo *ddi; 409146392Smarius 410152684Smarius ddi = device_get_ivars(child); 411152684Smarius return (&ddi->ddi_obdinfo); 412146392Smarius} 413146392Smarius 414152684Smariusstatic int 415152684Smariusdma_print_res(struct dma_devinfo *ddi) 416146392Smarius{ 417152684Smarius int rv; 418146392Smarius 419152684Smarius rv = 0; 420152684Smarius rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY, 421152684Smarius "%#lx"); 422152684Smarius rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%ld"); 423152684Smarius return (rv); 424146392Smarius} 425