altera_jtag_uart_fdt.c revision 245365
1239676Srwatson/*- 2239676Srwatson * Copyright (c) 2012 Robert N. M. Watson 3239676Srwatson * All rights reserved. 4239676Srwatson * 5239676Srwatson * This software was developed by SRI International and the University of 6239676Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7239676Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme. 8239676Srwatson * 9239676Srwatson * Redistribution and use in source and binary forms, with or without 10239676Srwatson * modification, are permitted provided that the following conditions 11239676Srwatson * are met: 12239676Srwatson * 1. Redistributions of source code must retain the above copyright 13239676Srwatson * notice, this list of conditions and the following disclaimer. 14239676Srwatson * 2. Redistributions in binary form must reproduce the above copyright 15239676Srwatson * notice, this list of conditions and the following disclaimer in the 16239676Srwatson * documentation and/or other materials provided with the distribution. 17239676Srwatson * 18239676Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19239676Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20239676Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21239676Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22239676Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23239676Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24239676Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25239676Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26239676Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27239676Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28239676Srwatson * SUCH DAMAGE. 29239676Srwatson */ 30239676Srwatson 31239676Srwatson#include <sys/cdefs.h> 32239676Srwatson__FBSDID("$FreeBSD: head/sys/dev/altera/jtag_uart/altera_jtag_uart_fdt.c 245365 2013-01-13 15:08:17Z rwatson $"); 33239676Srwatson 34239676Srwatson#include <sys/param.h> 35239676Srwatson#include <sys/bus.h> 36239676Srwatson#include <sys/condvar.h> 37239676Srwatson#include <sys/conf.h> 38239676Srwatson#include <sys/bio.h> 39239676Srwatson#include <sys/kernel.h> 40239676Srwatson#include <sys/lock.h> 41239676Srwatson#include <sys/malloc.h> 42239676Srwatson#include <sys/module.h> 43239676Srwatson#include <sys/mutex.h> 44239676Srwatson#include <sys/rman.h> 45239676Srwatson#include <sys/systm.h> 46239676Srwatson#include <sys/taskqueue.h> 47239676Srwatson 48239676Srwatson#include <machine/bus.h> 49239676Srwatson#include <machine/resource.h> 50239676Srwatson 51239676Srwatson#include <geom/geom_disk.h> 52239676Srwatson 53239676Srwatson#include <dev/altera/jtag_uart/altera_jtag_uart.h> 54239676Srwatson 55245365Srwatson#include <dev/fdt/fdt_common.h> 56245365Srwatson#include <dev/ofw/openfirm.h> 57245365Srwatson#include <dev/ofw/ofw_bus.h> 58245365Srwatson#include <dev/ofw/ofw_bus_subr.h> 59245365Srwatson 60239676Srwatson/* 61245365Srwatson * FDT bus attachment for Altera JTAG UARTs. 62239676Srwatson */ 63239676Srwatsonstatic int 64245365Srwatsonaltera_jtag_uart_fdt_probe(device_t dev) 65239676Srwatson{ 66239676Srwatson 67245365Srwatson if (ofw_bus_is_compatible(dev, "altera,jtag_uart-11_0")) { 68245365Srwatson device_set_desc(dev, "Altera JTAG UART"); 69245365Srwatson return (BUS_PROBE_DEFAULT); 70245365Srwatson } 71245365Srwatson return (ENXIO); 72239676Srwatson} 73239676Srwatson 74239676Srwatsonstatic int 75245365Srwatsonaltera_jtag_uart_fdt_attach(device_t dev) 76239676Srwatson{ 77239676Srwatson struct altera_jtag_uart_softc *sc; 78239676Srwatson int error; 79239676Srwatson 80239676Srwatson error = 0; 81239676Srwatson sc = device_get_softc(dev); 82239676Srwatson sc->ajus_dev = dev; 83239676Srwatson sc->ajus_unit = device_get_unit(dev); 84239676Srwatson sc->ajus_mem_rid = 0; 85239676Srwatson sc->ajus_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 86239676Srwatson &sc->ajus_mem_rid, RF_ACTIVE); 87239676Srwatson if (sc->ajus_mem_res == NULL) { 88239676Srwatson device_printf(dev, "couldn't map memory\n"); 89239676Srwatson error = ENXIO; 90239676Srwatson goto out; 91239676Srwatson } 92239676Srwatson 93239676Srwatson /* 94239676Srwatson * Interrupt support is optional -- if we can't allocate an IRQ, then 95239676Srwatson * we fall back on polling. 96239676Srwatson */ 97239676Srwatson sc->ajus_irq_rid = 0; 98239676Srwatson sc->ajus_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 99239676Srwatson &sc->ajus_irq_rid, RF_ACTIVE | RF_SHAREABLE); 100239676Srwatson if (sc->ajus_irq_res == NULL) 101239676Srwatson device_printf(dev, 102239676Srwatson "IRQ unavailable; selecting polled operation\n"); 103239676Srwatson error = altera_jtag_uart_attach(sc); 104239676Srwatsonout: 105239676Srwatson if (error) { 106239676Srwatson if (sc->ajus_irq_res != NULL) 107239676Srwatson bus_release_resource(dev, SYS_RES_IRQ, 108239676Srwatson sc->ajus_irq_rid, sc->ajus_irq_res); 109239676Srwatson if (sc->ajus_mem_res != NULL) 110239676Srwatson bus_release_resource(dev, SYS_RES_MEMORY, 111239676Srwatson sc->ajus_mem_rid, sc->ajus_mem_res); 112239676Srwatson } 113239676Srwatson return (error); 114239676Srwatson} 115239676Srwatson 116239676Srwatsonstatic int 117245365Srwatsonaltera_jtag_uart_fdt_detach(device_t dev) 118239676Srwatson{ 119239676Srwatson struct altera_jtag_uart_softc *sc; 120239676Srwatson 121239676Srwatson sc = device_get_softc(dev); 122239676Srwatson KASSERT(sc->ajus_mem_res != NULL, ("%s: resources not allocated", 123239676Srwatson __func__)); 124239676Srwatson 125239676Srwatson altera_jtag_uart_detach(sc); 126239676Srwatson bus_release_resource(dev, SYS_RES_IRQ, sc->ajus_irq_rid, 127239676Srwatson sc->ajus_irq_res); 128239676Srwatson bus_release_resource(dev, SYS_RES_MEMORY, sc->ajus_mem_rid, 129239676Srwatson sc->ajus_mem_res); 130239676Srwatson return (0); 131239676Srwatson} 132239676Srwatson 133245365Srwatsonstatic device_method_t altera_jtag_uart_fdt_methods[] = { 134245365Srwatson DEVMETHOD(device_probe, altera_jtag_uart_fdt_probe), 135245365Srwatson DEVMETHOD(device_attach, altera_jtag_uart_fdt_attach), 136245365Srwatson DEVMETHOD(device_detach, altera_jtag_uart_fdt_detach), 137239676Srwatson { 0, 0 } 138239676Srwatson}; 139239676Srwatson 140245365Srwatsonstatic driver_t altera_jtag_uart_fdt_driver = { 141239676Srwatson "altera_jtag_uart", 142245365Srwatson altera_jtag_uart_fdt_methods, 143239676Srwatson sizeof(struct altera_jtag_uart_softc), 144239676Srwatson}; 145239676Srwatson 146239676Srwatsonstatic devclass_t altera_jtag_uart_devclass; 147239676Srwatson 148245365SrwatsonDRIVER_MODULE(altera_jtag_uart, simplebus, altera_jtag_uart_fdt_driver, 149239676Srwatson altera_jtag_uart_devclass, 0, 0); 150