xref: /freebsd/sys/dev/altera/pio/pio.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1 /*-
2  * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * Altera PIO (Parallel IO) device driver
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/rman.h>
42 #include <sys/timeet.h>
43 #include <sys/timetc.h>
44 
45 #include <dev/fdt/fdt_common.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 
50 #include <machine/bus.h>
51 #include <machine/fdt.h>
52 #include <machine/cpu.h>
53 
54 #include <dev/altera/pio/pio.h>
55 #include "pio_if.h"
56 
57 #define READ4(_sc, _reg) bus_read_4((_sc)->res[0], _reg)
58 #define READ2(_sc, _reg) bus_read_2((_sc)->res[0], _reg)
59 #define READ1(_sc, _reg) bus_read_1((_sc)->res[0], _reg)
60 #define WRITE4(_sc, _reg, _val) bus_write_4((_sc)->res[0], _reg, _val)
61 #define WRITE2(_sc, _reg, _val) bus_write_2((_sc)->res[0], _reg, _val)
62 #define WRITE1(_sc, _reg, _val) bus_write_1((_sc)->res[0], _reg, _val)
63 
64 struct pio_softc {
65 	struct resource		*res[2];
66 	bus_space_tag_t		bst;
67 	bus_space_handle_t	bsh;
68 	device_t		dev;
69 	void			*ih;
70 };
71 
72 static struct resource_spec pio_spec[] = {
73 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
74 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
75 	{ -1, 0 }
76 };
77 
78 static int
79 pio_setup_irq(device_t dev, void *intr_handler, void *ih_user)
80 {
81 	struct pio_softc *sc;
82 
83 	sc = device_get_softc(dev);
84 
85 	/* Setup interrupt handlers */
86 	if (bus_setup_intr(sc->dev, sc->res[1], INTR_TYPE_BIO | INTR_MPSAFE,
87 		NULL, intr_handler, ih_user, &sc->ih)) {
88 		device_printf(sc->dev, "Unable to setup intr\n");
89 		return (1);
90 	}
91 
92 	return (0);
93 }
94 
95 static int
96 pio_teardown_irq(device_t dev)
97 {
98 	struct pio_softc *sc;
99 
100 	sc = device_get_softc(dev);
101 
102 	bus_teardown_intr(sc->dev, sc->res[1], sc->ih);
103 
104 	return (0);
105 }
106 
107 static int
108 pio_read(device_t dev)
109 {
110 	struct pio_softc *sc;
111 
112 	sc = device_get_softc(dev);
113 
114 	return (READ4(sc, PIO_DATA));
115 }
116 
117 static int
118 pio_set(device_t dev, int bit, int enable)
119 {
120 	struct pio_softc *sc;
121 
122 	sc = device_get_softc(dev);
123 
124 	if (enable)
125 		WRITE4(sc, PIO_OUTSET, bit);
126 	else
127 		WRITE4(sc, PIO_OUTCLR, bit);
128 
129 	return (0);
130 }
131 
132 static int
133 pio_configure(device_t dev, int dir, int mask)
134 {
135 	struct pio_softc *sc;
136 
137 	sc = device_get_softc(dev);
138 
139 	WRITE4(sc, PIO_INT_MASK, mask);
140 	WRITE4(sc, PIO_DIR, dir);
141 
142 	return (0);
143 }
144 
145 static int
146 pio_probe(device_t dev)
147 {
148 
149 	if (!ofw_bus_status_okay(dev))
150 		return (ENXIO);
151 
152 	if (!ofw_bus_is_compatible(dev, "altr,pio"))
153 		return (ENXIO);
154 
155 	device_set_desc(dev, "Altera PIO");
156 	return (BUS_PROBE_DEFAULT);
157 }
158 
159 static int
160 pio_attach(device_t dev)
161 {
162 	struct pio_softc *sc;
163 	struct fdt_ic *fic;
164 	phandle_t node;
165 
166 	sc = device_get_softc(dev);
167 	sc->dev = dev;
168 
169 	if (bus_alloc_resources(dev, pio_spec, sc->res)) {
170 		device_printf(dev, "could not allocate resources\n");
171 		return (ENXIO);
172 	}
173 
174 	/* Memory interface */
175 	sc->bst = rman_get_bustag(sc->res[0]);
176 	sc->bsh = rman_get_bushandle(sc->res[0]);
177 
178 	if ((node = ofw_bus_get_node(sc->dev)) == -1)
179 		return (ENXIO);
180 
181 	fic = malloc(sizeof(*fic), M_DEVBUF, M_WAITOK|M_ZERO);
182 	fic->iph = node;
183 	fic->dev = dev;
184 	SLIST_INSERT_HEAD(&fdt_ic_list_head, fic, fdt_ics);
185 
186 	return (0);
187 }
188 
189 static device_method_t pio_methods[] = {
190 	DEVMETHOD(device_probe,		pio_probe),
191 	DEVMETHOD(device_attach,	pio_attach),
192 
193 	/* pio_if.m */
194 	DEVMETHOD(pio_read,		pio_read),
195 	DEVMETHOD(pio_configure,	pio_configure),
196 	DEVMETHOD(pio_set,		pio_set),
197 	DEVMETHOD(pio_setup_irq,	pio_setup_irq),
198 	DEVMETHOD(pio_teardown_irq,	pio_teardown_irq),
199 	DEVMETHOD_END
200 };
201 
202 static driver_t pio_driver = {
203 	"altera_pio",
204 	pio_methods,
205 	sizeof(struct pio_softc),
206 };
207 
208 DRIVER_MODULE(altera_pio, simplebus, pio_driver, 0, 0);
209