1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * This software was developed by SRI International and the University of
8  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9  * ("CTSRD"), as part of the DARPA CRASH research programme.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/condvar.h>
37 #include <sys/conf.h>
38 #include <sys/bio.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/rman.h>
45 #include <sys/systm.h>
46 #include <sys/taskqueue.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 
51 #include <geom/geom_disk.h>
52 
53 #include <dev/altera/jtag_uart/altera_jtag_uart.h>
54 
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 
60 /*
61  * FDT bus attachment for Altera JTAG UARTs.
62  */
63 static int
64 altera_jtag_uart_fdt_probe(device_t dev)
65 {
66 
67 	if (!ofw_bus_status_okay(dev))
68 		return (ENXIO);
69 
70 	if (ofw_bus_is_compatible(dev, "altera,jtag_uart-11_0")) {
71 		device_set_desc(dev, "Altera JTAG UART");
72 		return (BUS_PROBE_DEFAULT);
73 	}
74 	return (ENXIO);
75 }
76 
77 static int
78 altera_jtag_uart_fdt_attach(device_t dev)
79 {
80 	struct altera_jtag_uart_softc *sc;
81 	int error;
82 
83 	error = 0;
84 	sc = device_get_softc(dev);
85 	sc->ajus_dev = dev;
86 	sc->ajus_unit = device_get_unit(dev);
87 	sc->ajus_mem_rid = 0;
88 	sc->ajus_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
89 	    &sc->ajus_mem_rid, RF_ACTIVE);
90 	if (sc->ajus_mem_res == NULL) {
91 		device_printf(dev, "couldn't map memory\n");
92 		error = ENXIO;
93 		goto out;
94 	}
95 
96 	/*
97 	 * Interrupt support is optional -- if we can't allocate an IRQ, then
98 	 * we fall back on polling.
99 	 */
100 	sc->ajus_irq_rid = 0;
101 	sc->ajus_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
102 	    &sc->ajus_irq_rid, RF_ACTIVE | RF_SHAREABLE);
103 	if (sc->ajus_irq_res == NULL)
104 		device_printf(dev,
105 		    "IRQ unavailable; selecting polled operation\n");
106 	error = altera_jtag_uart_attach(sc);
107 out:
108 	if (error) {
109 		if (sc->ajus_irq_res != NULL)
110 			bus_release_resource(dev, SYS_RES_IRQ,
111 			    sc->ajus_irq_rid, sc->ajus_irq_res);
112 		if (sc->ajus_mem_res != NULL)
113 			bus_release_resource(dev, SYS_RES_MEMORY,
114 			    sc->ajus_mem_rid, sc->ajus_mem_res);
115 	}
116 	return (error);
117 }
118 
119 static int
120 altera_jtag_uart_fdt_detach(device_t dev)
121 {
122 	struct altera_jtag_uart_softc *sc;
123 
124 	sc = device_get_softc(dev);
125 	KASSERT(sc->ajus_mem_res != NULL, ("%s: resources not allocated",
126 	    __func__));
127 
128 	altera_jtag_uart_detach(sc);
129 	bus_release_resource(dev, SYS_RES_IRQ, sc->ajus_irq_rid,
130 	    sc->ajus_irq_res);
131 	bus_release_resource(dev, SYS_RES_MEMORY, sc->ajus_mem_rid,
132 	    sc->ajus_mem_res);
133 	return (0);
134 }
135 
136 static device_method_t altera_jtag_uart_fdt_methods[] = {
137 	DEVMETHOD(device_probe,		altera_jtag_uart_fdt_probe),
138 	DEVMETHOD(device_attach,	altera_jtag_uart_fdt_attach),
139 	DEVMETHOD(device_detach,	altera_jtag_uart_fdt_detach),
140 	{ 0, 0 }
141 };
142 
143 static driver_t altera_jtag_uart_fdt_driver = {
144 	"altera_jtag_uart",
145 	altera_jtag_uart_fdt_methods,
146 	sizeof(struct altera_jtag_uart_softc),
147 };
148 
149 DRIVER_MODULE(altera_jtag_uart, simplebus, altera_jtag_uart_fdt_driver, 0, 0);
150