xref: /freebsd/sys/contrib/vchiq/interface/vchiq_arm/vchiq_kmod.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2012-2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/malloc.h>
35 #include <sys/rman.h>
36 #include <sys/timeet.h>
37 #include <sys/timetc.h>
38 #include <sys/watchdog.h>
39 #include <machine/bus.h>
40 #include <machine/cpu.h>
41 #include <machine/frame.h>
42 #include <machine/intr.h>
43 
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <machine/bus.h>
50 #include <machine/fdt.h>
51 
52 #include "vchiq_arm.h"
53 #include "vchiq_2835.h"
54 
55 #define	VCHIQ_LOCK	do {		\
56 	mtx_lock(&bcm_vchiq_sc->lock);	\
57 } while(0)
58 
59 #define	VCHIQ_UNLOCK	do {		\
60 	mtx_unlock(&bcm_vchiq_sc->lock);	\
61 } while(0)
62 
63 #ifdef  DEBUG
64 #define dprintf(fmt, args...) printf(fmt, ##args)
65 #else
66 #define dprintf(fmt, args...)
67 #endif
68 
69 struct bcm_vchiq_softc {
70 	struct mtx		lock;
71 	struct resource *	mem_res;
72 	struct resource *	irq_res;
73 	void*			intr_hl;
74 	bus_space_tag_t		bst;
75 	bus_space_handle_t	bsh;
76 	int			regs_offset;
77 };
78 
79 static struct bcm_vchiq_softc *bcm_vchiq_sc = NULL;
80 
81 #define	BSD_DTB			1
82 #define	UPSTREAM_DTB		2
83 static struct ofw_compat_data compat_data[] = {
84 	{"broadcom,bcm2835-vchiq",	BSD_DTB},
85 	{"brcm,bcm2835-vchiq",		UPSTREAM_DTB},
86 	{"brcm,bcm2711-vchiq",		UPSTREAM_DTB},
87 	{NULL,				0}
88 };
89 
90 #define	vchiq_read_4(reg)		\
91     bus_space_read_4(bcm_vchiq_sc->bst, bcm_vchiq_sc->bsh, (reg) + \
92     bcm_vchiq_sc->regs_offset)
93 #define	vchiq_write_4(reg, val)		\
94     bus_space_write_4(bcm_vchiq_sc->bst, bcm_vchiq_sc->bsh, (reg) + \
95     bcm_vchiq_sc->regs_offset, val)
96 
97 /*
98  * Extern functions */
99 void vchiq_exit(void);
100 int vchiq_init(void);
101 
102 extern VCHIQ_STATE_T g_state;
103 extern int g_cache_line_size;
104 
105 static void
106 bcm_vchiq_intr(void *arg)
107 {
108 	VCHIQ_STATE_T *state = &g_state;
109 	unsigned int status;
110 
111 	/* Read (and clear) the doorbell */
112 	status = vchiq_read_4(0x40);
113 
114 	if (status & 0x4) {  /* Was the doorbell rung? */
115 		remote_event_pollall(state);
116 	}
117 }
118 
119 void
120 remote_event_signal(REMOTE_EVENT_T *event)
121 {
122 	event->fired = 1;
123 
124 	/* The test on the next line also ensures the write on the previous line
125 		has completed */
126 	if (event->armed) {
127 		/* trigger vc interrupt */
128 		dsb();
129 		vchiq_write_4(0x48, 0);
130 	}
131 }
132 
133 static int
134 bcm_vchiq_probe(device_t dev)
135 {
136 
137 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
138 		return (ENXIO);
139 
140 	device_set_desc(dev, "BCM2835 VCHIQ");
141 	return (BUS_PROBE_DEFAULT);
142 }
143 
144 static int
145 bcm_vchiq_attach(device_t dev)
146 {
147 	struct bcm_vchiq_softc *sc = device_get_softc(dev);
148 	phandle_t node;
149 	pcell_t cell;
150 	int rid = 0;
151 
152 	if (bcm_vchiq_sc != NULL)
153 		return (EINVAL);
154 
155 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
156 	if (sc->mem_res == NULL) {
157 		device_printf(dev, "could not allocate memory resource\n");
158 		return (ENXIO);
159 	}
160 
161 	sc->bst = rman_get_bustag(sc->mem_res);
162 	sc->bsh = rman_get_bushandle(sc->mem_res);
163 
164 	rid = 0;
165 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
166 	if (sc->irq_res == NULL) {
167 		device_printf(dev, "could not allocate interrupt resource\n");
168 		return (ENXIO);
169 	}
170 
171 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == UPSTREAM_DTB)
172 		sc->regs_offset = -0x40;
173 
174 	node = ofw_bus_get_node(dev);
175 	if ((OF_getencprop(node, "cache-line-size", &cell, sizeof(cell))) > 0)
176 		g_cache_line_size = cell;
177 
178 	vchiq_core_initialize();
179 
180 	/* Setup and enable the timer */
181 	if (bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
182 			NULL, bcm_vchiq_intr, sc,
183 			&sc->intr_hl) != 0) {
184 		bus_release_resource(dev, SYS_RES_IRQ, rid,
185 			sc->irq_res);
186 		device_printf(dev, "Unable to setup the clock irq handler.\n");
187 		return (ENXIO);
188 	}
189 
190 	mtx_init(&sc->lock, "vchiq", 0, MTX_DEF);
191 	bcm_vchiq_sc = sc;
192 
193 	vchiq_init();
194 
195 	bus_generic_probe(dev);
196 	bus_generic_attach(dev);
197 
198 	return (0);
199 }
200 
201 static int
202 bcm_vchiq_detach(device_t dev)
203 {
204 	struct bcm_vchiq_softc *sc = device_get_softc(dev);
205 
206 	vchiq_exit();
207 
208 	if (sc->intr_hl)
209                 bus_teardown_intr(dev, sc->irq_res, sc->intr_hl);
210 	bus_release_resource(dev, SYS_RES_IRQ, 0,
211 		sc->irq_res);
212 	bus_release_resource(dev, SYS_RES_MEMORY, 0,
213 		sc->mem_res);
214 
215 	mtx_destroy(&sc->lock);
216 
217 	return (0);
218 }
219 
220 
221 static device_method_t bcm_vchiq_methods[] = {
222 	DEVMETHOD(device_probe,		bcm_vchiq_probe),
223 	DEVMETHOD(device_attach,	bcm_vchiq_attach),
224 	DEVMETHOD(device_detach,	bcm_vchiq_detach),
225 
226         /* Bus interface */
227         DEVMETHOD(bus_add_child,        bus_generic_add_child),
228 
229 	{ 0, 0 }
230 };
231 
232 static driver_t bcm_vchiq_driver = {
233 	"vchiq",
234 	bcm_vchiq_methods,
235 	sizeof(struct bcm_vchiq_softc),
236 };
237 
238 DRIVER_MODULE(vchiq, simplebus, bcm_vchiq_driver, 0, 0);
239 MODULE_VERSION(vchiq, 1);
240