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$"); 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 67261410Sian if (!ofw_bus_status_okay(dev)) 68261410Sian return (ENXIO); 69261410Sian 70245365Srwatson if (ofw_bus_is_compatible(dev, "altera,jtag_uart-11_0")) { 71245365Srwatson device_set_desc(dev, "Altera JTAG UART"); 72245365Srwatson return (BUS_PROBE_DEFAULT); 73245365Srwatson } 74245365Srwatson return (ENXIO); 75239676Srwatson} 76239676Srwatson 77239676Srwatsonstatic int 78245365Srwatsonaltera_jtag_uart_fdt_attach(device_t dev) 79239676Srwatson{ 80239676Srwatson struct altera_jtag_uart_softc *sc; 81239676Srwatson int error; 82239676Srwatson 83239676Srwatson error = 0; 84239676Srwatson sc = device_get_softc(dev); 85239676Srwatson sc->ajus_dev = dev; 86239676Srwatson sc->ajus_unit = device_get_unit(dev); 87239676Srwatson sc->ajus_mem_rid = 0; 88239676Srwatson sc->ajus_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 89239676Srwatson &sc->ajus_mem_rid, RF_ACTIVE); 90239676Srwatson if (sc->ajus_mem_res == NULL) { 91239676Srwatson device_printf(dev, "couldn't map memory\n"); 92239676Srwatson error = ENXIO; 93239676Srwatson goto out; 94239676Srwatson } 95239676Srwatson 96239676Srwatson /* 97239676Srwatson * Interrupt support is optional -- if we can't allocate an IRQ, then 98239676Srwatson * we fall back on polling. 99239676Srwatson */ 100239676Srwatson sc->ajus_irq_rid = 0; 101239676Srwatson sc->ajus_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 102239676Srwatson &sc->ajus_irq_rid, RF_ACTIVE | RF_SHAREABLE); 103239676Srwatson if (sc->ajus_irq_res == NULL) 104239676Srwatson device_printf(dev, 105239676Srwatson "IRQ unavailable; selecting polled operation\n"); 106239676Srwatson error = altera_jtag_uart_attach(sc); 107239676Srwatsonout: 108239676Srwatson if (error) { 109239676Srwatson if (sc->ajus_irq_res != NULL) 110239676Srwatson bus_release_resource(dev, SYS_RES_IRQ, 111239676Srwatson sc->ajus_irq_rid, sc->ajus_irq_res); 112239676Srwatson if (sc->ajus_mem_res != NULL) 113239676Srwatson bus_release_resource(dev, SYS_RES_MEMORY, 114239676Srwatson sc->ajus_mem_rid, sc->ajus_mem_res); 115239676Srwatson } 116239676Srwatson return (error); 117239676Srwatson} 118239676Srwatson 119239676Srwatsonstatic int 120245365Srwatsonaltera_jtag_uart_fdt_detach(device_t dev) 121239676Srwatson{ 122239676Srwatson struct altera_jtag_uart_softc *sc; 123239676Srwatson 124239676Srwatson sc = device_get_softc(dev); 125239676Srwatson KASSERT(sc->ajus_mem_res != NULL, ("%s: resources not allocated", 126239676Srwatson __func__)); 127239676Srwatson 128239676Srwatson altera_jtag_uart_detach(sc); 129239676Srwatson bus_release_resource(dev, SYS_RES_IRQ, sc->ajus_irq_rid, 130239676Srwatson sc->ajus_irq_res); 131239676Srwatson bus_release_resource(dev, SYS_RES_MEMORY, sc->ajus_mem_rid, 132239676Srwatson sc->ajus_mem_res); 133239676Srwatson return (0); 134239676Srwatson} 135239676Srwatson 136245365Srwatsonstatic device_method_t altera_jtag_uart_fdt_methods[] = { 137245365Srwatson DEVMETHOD(device_probe, altera_jtag_uart_fdt_probe), 138245365Srwatson DEVMETHOD(device_attach, altera_jtag_uart_fdt_attach), 139245365Srwatson DEVMETHOD(device_detach, altera_jtag_uart_fdt_detach), 140239676Srwatson { 0, 0 } 141239676Srwatson}; 142239676Srwatson 143245365Srwatsonstatic driver_t altera_jtag_uart_fdt_driver = { 144239676Srwatson "altera_jtag_uart", 145245365Srwatson altera_jtag_uart_fdt_methods, 146239676Srwatson sizeof(struct altera_jtag_uart_softc), 147239676Srwatson}; 148239676Srwatson 149245365SrwatsonDRIVER_MODULE(altera_jtag_uart, simplebus, altera_jtag_uart_fdt_driver, 150239676Srwatson altera_jtag_uart_devclass, 0, 0); 151