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
55239676Srwatson/*
56239676Srwatson * Nexus bus attachment for Altera JTAG UARTs.  Appropriate for most Altera
57239676Srwatson * FPGA SoC-style configurations in which the IP core will be exposed to the
58239676Srwatson * processor via a memory-mapped Avalon bus.
59239676Srwatson */
60239676Srwatsonstatic int
61239676Srwatsonaltera_jtag_uart_nexus_probe(device_t dev)
62239676Srwatson{
63239676Srwatson
64239676Srwatson	device_set_desc(dev, "Altera JTAG UART");
65257336Snwhitehorn	return (BUS_PROBE_NOWILDCARD);
66239676Srwatson}
67239676Srwatson
68239676Srwatsonstatic int
69239676Srwatsonaltera_jtag_uart_nexus_attach(device_t dev)
70239676Srwatson{
71239676Srwatson	struct altera_jtag_uart_softc *sc;
72239676Srwatson	int error;
73239676Srwatson
74239676Srwatson	error = 0;
75239676Srwatson	sc = device_get_softc(dev);
76239676Srwatson	sc->ajus_dev = dev;
77239676Srwatson	sc->ajus_unit = device_get_unit(dev);
78239676Srwatson	sc->ajus_mem_rid = 0;
79239676Srwatson	sc->ajus_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
80239676Srwatson	    &sc->ajus_mem_rid, RF_ACTIVE);
81239676Srwatson	if (sc->ajus_mem_res == NULL) {
82239676Srwatson		device_printf(dev, "couldn't map memory\n");
83239676Srwatson		error = ENXIO;
84239676Srwatson		goto out;
85239676Srwatson	}
86239676Srwatson
87239676Srwatson	/*
88239676Srwatson	 * Interrupt support is optional -- if we can't allocate an IRQ, then
89239676Srwatson	 * we fall back on polling.
90239676Srwatson	 */
91239676Srwatson	sc->ajus_irq_rid = 0;
92239676Srwatson	sc->ajus_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
93239676Srwatson	    &sc->ajus_irq_rid, RF_ACTIVE | RF_SHAREABLE);
94239676Srwatson	if (sc->ajus_irq_res == NULL)
95239676Srwatson		device_printf(dev,
96239676Srwatson		    "IRQ unavailable; selecting polled operation\n");
97239676Srwatson	error = altera_jtag_uart_attach(sc);
98239676Srwatsonout:
99239676Srwatson	if (error) {
100239676Srwatson		if (sc->ajus_irq_res != NULL)
101239676Srwatson			bus_release_resource(dev, SYS_RES_IRQ,
102239676Srwatson			    sc->ajus_irq_rid, sc->ajus_irq_res);
103239676Srwatson		if (sc->ajus_mem_res != NULL)
104239676Srwatson			bus_release_resource(dev, SYS_RES_MEMORY,
105239676Srwatson			    sc->ajus_mem_rid, sc->ajus_mem_res);
106239676Srwatson	}
107239676Srwatson	return (error);
108239676Srwatson}
109239676Srwatson
110239676Srwatsonstatic int
111239676Srwatsonaltera_jtag_uart_nexus_detach(device_t dev)
112239676Srwatson{
113239676Srwatson	struct altera_jtag_uart_softc *sc;
114239676Srwatson
115239676Srwatson	sc = device_get_softc(dev);
116239676Srwatson	KASSERT(sc->ajus_mem_res != NULL, ("%s: resources not allocated",
117239676Srwatson	    __func__));
118239676Srwatson
119239676Srwatson	altera_jtag_uart_detach(sc);
120239676Srwatson	bus_release_resource(dev, SYS_RES_IRQ, sc->ajus_irq_rid,
121239676Srwatson	    sc->ajus_irq_res);
122239676Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, sc->ajus_mem_rid,
123239676Srwatson	    sc->ajus_mem_res);
124239676Srwatson	return (0);
125239676Srwatson}
126239676Srwatson
127239676Srwatsonstatic device_method_t altera_jtag_uart_nexus_methods[] = {
128239676Srwatson	DEVMETHOD(device_probe,		altera_jtag_uart_nexus_probe),
129239676Srwatson	DEVMETHOD(device_attach,	altera_jtag_uart_nexus_attach),
130239676Srwatson	DEVMETHOD(device_detach,	altera_jtag_uart_nexus_detach),
131239676Srwatson	{ 0, 0 }
132239676Srwatson};
133239676Srwatson
134239676Srwatsonstatic driver_t altera_jtag_uart_nexus_driver = {
135239676Srwatson	"altera_jtag_uart",
136239676Srwatson	altera_jtag_uart_nexus_methods,
137239676Srwatson	sizeof(struct altera_jtag_uart_softc),
138239676Srwatson};
139239676Srwatson
140239676SrwatsonDRIVER_MODULE(altera_jtag_uart, nexus, altera_jtag_uart_nexus_driver,
141239676Srwatson    altera_jtag_uart_devclass, 0, 0);
142