1219567Smarius/* $OpenBSD: dma_sbus.c,v 1.16 2008/06/26 05:42:18 ray Exp $ */ 2219567Smarius/* $NetBSD: dma_sbus.c,v 1.32 2008/04/28 20:23:57 martin 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 * 20146392Smarius * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21146392Smarius * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22146392Smarius * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23146392Smarius * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24146392Smarius * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25146392Smarius * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26146392Smarius * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27146392Smarius * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28146392Smarius * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29146392Smarius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30146392Smarius * POSSIBILITY OF SUCH DAMAGE. 31146392Smarius */ 32146392Smarius 33146392Smarius/*- 34146392Smarius * Copyright (c) 1994 Peter Galbavy. All rights reserved. 35146392Smarius * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>. All rights reserved. 36146392Smarius * 37146392Smarius * Redistribution and use in source and binary forms, with or without 38146392Smarius * modification, are permitted provided that the following conditions 39146392Smarius * are met: 40146392Smarius * 1. Redistributions of source code must retain the above copyright 41146392Smarius * notice, this list of conditions and the following disclaimer. 42146392Smarius * 2. Redistributions in binary form must reproduce the above copyright 43146392Smarius * notice, this list of conditions and the following disclaimer in the 44146392Smarius * documentation and/or other materials provided with the distribution. 45146392Smarius * 46146392Smarius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47146392Smarius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48146392Smarius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49146392Smarius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50146392Smarius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51146392Smarius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52146392Smarius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53146392Smarius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54146392Smarius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55146392Smarius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56146392Smarius */ 57146392Smarius 58146392Smarius#include <sys/cdefs.h> 59146392Smarius__FBSDID("$FreeBSD$"); 60146392Smarius 61146392Smarius#include <sys/param.h> 62146392Smarius#include <sys/systm.h> 63146392Smarius#include <sys/bus.h> 64146392Smarius#include <sys/kernel.h> 65146392Smarius#include <sys/module.h> 66146392Smarius#include <sys/rman.h> 67146392Smarius 68146392Smarius#include <dev/ofw/ofw_bus.h> 69152684Smarius#include <dev/ofw/ofw_bus_subr.h> 70146392Smarius#include <dev/ofw/openfirm.h> 71146392Smarius 72146392Smarius#include <machine/bus.h> 73146392Smarius#include <machine/bus_common.h> 74146392Smarius#include <machine/resource.h> 75146392Smarius 76146392Smarius#include <sparc64/sbus/lsi64854reg.h> 77146392Smarius#include <sparc64/sbus/lsi64854var.h> 78146392Smarius#include <sparc64/sbus/ofw_sbus.h> 79146392Smarius#include <sparc64/sbus/sbusreg.h> 80146392Smarius#include <sparc64/sbus/sbusvar.h> 81146392Smarius 82146392Smariusstruct dma_devinfo { 83152684Smarius struct ofw_bus_devinfo ddi_obdinfo; 84146392Smarius struct resource_list ddi_rl; 85146392Smarius}; 86146392Smarius 87146392Smariusstruct dma_softc { 88146392Smarius struct lsi64854_softc sc_lsi64854; /* base device */ 89146392Smarius int sc_ign; 90146392Smarius int sc_slot; 91146392Smarius}; 92146392Smarius 93146392Smariusstatic devclass_t dma_devclass; 94146392Smarius 95146392Smariusstatic device_probe_t dma_probe; 96146392Smariusstatic device_attach_t dma_attach; 97146392Smariusstatic bus_print_child_t dma_print_child; 98146392Smariusstatic bus_probe_nomatch_t dma_probe_nomatch; 99146392Smariusstatic bus_get_resource_list_t dma_get_resource_list; 100152684Smariusstatic ofw_bus_get_devinfo_t dma_get_devinfo; 101146392Smarius 102152684Smariusstatic struct dma_devinfo *dma_setup_dinfo(device_t, struct dma_softc *, 103152684Smarius phandle_t); 104146392Smariusstatic void dma_destroy_dinfo(struct dma_devinfo *); 105152684Smariusstatic int dma_print_res(struct dma_devinfo *); 106146392Smarius 107146392Smariusstatic device_method_t dma_methods[] = { 108166147Smarius /* Device interface */ 109146392Smarius DEVMETHOD(device_probe, dma_probe), 110146392Smarius DEVMETHOD(device_attach, dma_attach), 111154870Smarius DEVMETHOD(device_shutdown, bus_generic_shutdown), 112154870Smarius DEVMETHOD(device_suspend, bus_generic_suspend), 113154870Smarius DEVMETHOD(device_resume, bus_generic_resume), 114146392Smarius 115146392Smarius /* Bus interface */ 116146392Smarius DEVMETHOD(bus_print_child, dma_print_child), 117146392Smarius DEVMETHOD(bus_probe_nomatch, dma_probe_nomatch), 118146392Smarius DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 119146392Smarius DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 120146392Smarius DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 121225931Smarius DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 122190099Smarius DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 123190099Smarius DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 124190099Smarius DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 125190099Smarius DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 126146392Smarius DEVMETHOD(bus_get_resource_list, dma_get_resource_list), 127190114Smarius DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 128146392Smarius 129146392Smarius /* ofw_bus interface */ 130152684Smarius DEVMETHOD(ofw_bus_get_devinfo, dma_get_devinfo), 131152684Smarius DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 132152684Smarius DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 133152684Smarius DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 134152684Smarius DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 135152684Smarius DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 136146392Smarius 137227848Smarius DEVMETHOD_END 138146392Smarius}; 139146392Smarius 140146392Smariusstatic driver_t dma_driver = { 141146392Smarius "dma", 142146392Smarius dma_methods, 143146392Smarius sizeof(struct dma_softc), 144146392Smarius}; 145146392Smarius 146200874Smarius/* 147200874Smarius * The probe order is handled by sbus(4) as we don't want the variants 148200874Smarius * with children to be attached earlier than the stand-alone controllers 149200874Smarius * in order to generally preserve the OFW device tree order. 150200874Smarius */ 151200874SmariusEARLY_DRIVER_MODULE(dma, sbus, dma_driver, dma_devclass, 0, 0, 152200874Smarius BUS_PASS_DEFAULT); 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; 181182876Smarius int error, i; 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 191182876Smarius i = 0; 192182876Smarius lsc->sc_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, 193146392Smarius RF_ACTIVE); 194146392Smarius if (lsc->sc_res == NULL) { 195146392Smarius device_printf(dev, "cannot allocate resources\n"); 196146392Smarius return (ENXIO); 197146392Smarius } 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; 219300173Sgonzo OF_prop_free(cabletype); 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( 231166147Smarius bus_get_dma_tag(dev), /* parent */ 232166147Smarius 1, 0, /* alignment, boundary */ 233146392Smarius BUS_SPACE_MAXADDR, /* lowaddr */ 234146392Smarius BUS_SPACE_MAXADDR, /* highaddr */ 235146392Smarius NULL, NULL, /* filter, filterarg */ 236226948Smarius BUS_SPACE_MAXSIZE, /* maxsize */ 237226948Smarius BUS_SPACE_UNRESTRICTED, /* nsegments */ 238226948Smarius BUS_SPACE_MAXSIZE, /* 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 247182876Smarius i = sbus_get_burstsz(dev); 248182876Smarius lsc->sc_burst = (i & SBUS_BURST_32) ? 32 : 249182876Smarius (i & SBUS_BURST_16) ? 16 : 0; 250146392Smarius lsc->sc_dev = dev; 251146392Smarius 252146392Smarius /* Attach children. */ 253182876Smarius i = 0; 254146392Smarius for (child = OF_child(node); child != 0; child = OF_peer(child)) { 255152684Smarius if ((ddi = dma_setup_dinfo(dev, dsc, child)) == NULL) 256146392Smarius continue; 257182876Smarius if (i != 0) { 258152684Smarius device_printf(dev, 259152684Smarius "<%s>: only one child per DMA channel supported\n", 260152684Smarius ddi->ddi_obdinfo.obd_name); 261146392Smarius dma_destroy_dinfo(ddi); 262146392Smarius continue; 263146392Smarius } 264146392Smarius if ((cdev = device_add_child(dev, NULL, -1)) == NULL) { 265146392Smarius device_printf(dev, "<%s>: device_add_child failed\n", 266152684Smarius ddi->ddi_obdinfo.obd_name); 267146392Smarius dma_destroy_dinfo(ddi); 268146392Smarius continue; 269146392Smarius } 270146392Smarius device_set_ivars(cdev, ddi); 271182876Smarius i++; 272146392Smarius } 273152684Smarius return (bus_generic_attach(dev)); 274146392Smarius 275146392Smarius fail_lres: 276182876Smarius bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(lsc->sc_res), 277182876Smarius lsc->sc_res); 278146392Smarius return (error); 279146392Smarius} 280146392Smarius 281146392Smariusstatic struct dma_devinfo * 282152684Smariusdma_setup_dinfo(device_t dev, struct dma_softc *dsc, phandle_t node) 283146392Smarius{ 284146392Smarius struct dma_devinfo *ddi; 285146392Smarius struct sbus_regs *reg; 286146392Smarius uint32_t base, iv, *intr; 287146392Smarius int i, nreg, nintr, slot, rslot; 288146392Smarius 289146392Smarius ddi = malloc(sizeof(*ddi), M_DEVBUF, M_WAITOK | M_ZERO); 290152684Smarius if (ofw_bus_gen_setup_devinfo(&ddi->ddi_obdinfo, node) != 0) { 291152684Smarius free(ddi, M_DEVBUF); 292146392Smarius return (NULL); 293152684Smarius } 294146392Smarius resource_list_init(&ddi->ddi_rl); 295146392Smarius slot = -1; 296146392Smarius nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)®); 297146392Smarius if (nreg == -1) { 298152684Smarius device_printf(dev, "<%s>: incomplete\n", 299152684Smarius ddi->ddi_obdinfo.obd_name); 300152684Smarius goto fail; 301146392Smarius } 302146392Smarius for (i = 0; i < nreg; i++) { 303146392Smarius base = reg[i].sbr_offset; 304146392Smarius if (SBUS_ABS(base)) { 305146392Smarius rslot = SBUS_ABS_TO_SLOT(base); 306146392Smarius base = SBUS_ABS_TO_OFFSET(base); 307146392Smarius } else 308146392Smarius rslot = reg[i].sbr_slot; 309146392Smarius if (slot != -1 && slot != rslot) { 310152684Smarius device_printf(dev, "<%s>: multiple slots\n", 311152684Smarius ddi->ddi_obdinfo.obd_name); 312300173Sgonzo OF_prop_free(reg); 313152684Smarius goto fail; 314146392Smarius } 315146392Smarius slot = rslot; 316146392Smarius 317146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_MEMORY, i, base, 318146392Smarius base + reg[i].sbr_size, reg[i].sbr_size); 319146392Smarius } 320300173Sgonzo OF_prop_free(reg); 321146392Smarius if (slot != dsc->sc_slot) { 322146392Smarius device_printf(dev, "<%s>: parent and child slot do not match\n", 323152684Smarius ddi->ddi_obdinfo.obd_name); 324152684Smarius goto fail; 325146392Smarius } 326146392Smarius 327146392Smarius /* 328146392Smarius * The `interrupts' property contains the SBus interrupt level. 329146392Smarius */ 330146392Smarius nintr = OF_getprop_alloc(node, "interrupts", sizeof(*intr), 331146392Smarius (void **)&intr); 332146392Smarius if (nintr != -1) { 333146392Smarius for (i = 0; i < nintr; i++) { 334146392Smarius iv = intr[i]; 335146392Smarius /* 336146392Smarius * SBus card devices need the slot number encoded into 337146392Smarius * the vector as this is generally not done. 338146392Smarius */ 339146392Smarius if ((iv & INTMAP_OBIO_MASK) == 0) 340146392Smarius iv |= slot << 3; 341146392Smarius /* Set the IGN as appropriate. */ 342146392Smarius iv |= dsc->sc_ign << INTMAP_IGN_SHIFT; 343146392Smarius resource_list_add(&ddi->ddi_rl, SYS_RES_IRQ, i, 344146392Smarius iv, iv, 1); 345146392Smarius } 346300173Sgonzo OF_prop_free(intr); 347146392Smarius } 348146392Smarius return (ddi); 349152684Smarius 350152684Smarius fail: 351152684Smarius dma_destroy_dinfo(ddi); 352152684Smarius return (NULL); 353146392Smarius} 354146392Smarius 355146392Smariusstatic void 356146392Smariusdma_destroy_dinfo(struct dma_devinfo *dinfo) 357146392Smarius{ 358146392Smarius 359146392Smarius resource_list_free(&dinfo->ddi_rl); 360152684Smarius ofw_bus_gen_destroy_devinfo(&dinfo->ddi_obdinfo); 361146392Smarius free(dinfo, M_DEVBUF); 362146392Smarius} 363146392Smarius 364146392Smariusstatic int 365146392Smariusdma_print_child(device_t dev, device_t child) 366146392Smarius{ 367146392Smarius int rv; 368146392Smarius 369146392Smarius rv = bus_print_child_header(dev, child); 370152684Smarius rv += dma_print_res(device_get_ivars(child)); 371146392Smarius rv += bus_print_child_footer(dev, child); 372146392Smarius return (rv); 373146392Smarius} 374146392Smarius 375146392Smariusstatic void 376146392Smariusdma_probe_nomatch(device_t dev, device_t child) 377146392Smarius{ 378152684Smarius const char *type; 379146392Smarius 380152684Smarius device_printf(dev, "<%s>", ofw_bus_get_name(child)); 381152684Smarius dma_print_res(device_get_ivars(child)); 382152684Smarius type = ofw_bus_get_type(child); 383152684Smarius printf(" type %s (no driver attached)\n", 384152684Smarius type != NULL ? type : "unknown"); 385146392Smarius} 386146392Smarius 387146392Smariusstatic struct resource_list * 388146392Smariusdma_get_resource_list(device_t dev, device_t child) 389146392Smarius{ 390146392Smarius struct dma_devinfo *ddi; 391146392Smarius 392146392Smarius ddi = device_get_ivars(child); 393146392Smarius return (&ddi->ddi_rl); 394146392Smarius} 395146392Smarius 396152684Smariusstatic const struct ofw_bus_devinfo * 397152684Smariusdma_get_devinfo(device_t bus, device_t child) 398146392Smarius{ 399152684Smarius struct dma_devinfo *ddi; 400146392Smarius 401152684Smarius ddi = device_get_ivars(child); 402152684Smarius return (&ddi->ddi_obdinfo); 403146392Smarius} 404146392Smarius 405152684Smariusstatic int 406152684Smariusdma_print_res(struct dma_devinfo *ddi) 407146392Smarius{ 408152684Smarius int rv; 409146392Smarius 410152684Smarius rv = 0; 411152684Smarius rv += resource_list_print_type(&ddi->ddi_rl, "mem", SYS_RES_MEMORY, 412297199Sjhibbits "%#jx"); 413297199Sjhibbits rv += resource_list_print_type(&ddi->ddi_rl, "irq", SYS_RES_IRQ, "%jd"); 414152684Smarius return (rv); 415146392Smarius} 416