]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/ti/ti_pruss.c
MFH @ r323558.
[FreeBSD/FreeBSD.git] / sys / arm / ti / ti_pruss.c
1 /*-
2  * Copyright (c) 2013 Rui Paulo <rpaulo@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
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/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/malloc.h>
36 #include <sys/rman.h>
37 #include <sys/event.h>
38 #include <sys/selinfo.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/ofw/openfirm.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <machine/bus.h>
49
50 #include <arm/ti/ti_prcm.h>
51 #include <arm/ti/ti_pruss.h>
52
53 #ifdef DEBUG
54 #define DPRINTF(fmt, ...)       do {    \
55         printf("%s: ", __func__);       \
56         printf(fmt, __VA_ARGS__);       \
57 } while (0)
58 #else
59 #define DPRINTF(fmt, ...)
60 #endif
61
62 static device_probe_t           ti_pruss_probe;
63 static device_attach_t          ti_pruss_attach;
64 static device_detach_t          ti_pruss_detach;
65 static void                     ti_pruss_intr(void *);
66 static d_open_t                 ti_pruss_open;
67 static d_mmap_t                 ti_pruss_mmap;
68 static void                     ti_pruss_kq_read_detach(struct knote *);
69 static int                      ti_pruss_kq_read_event(struct knote *, long);
70 static d_kqfilter_t             ti_pruss_kqfilter;
71
72 #define TI_PRUSS_IRQS           8
73
74 struct ti_pruss_softc {
75         struct mtx              sc_mtx;
76         struct resource         *sc_mem_res;
77         struct resource         *sc_irq_res[TI_PRUSS_IRQS];
78         void                    *sc_intr[TI_PRUSS_IRQS];
79         bus_space_tag_t         sc_bt;
80         bus_space_handle_t      sc_bh;
81         struct cdev             *sc_pdev;
82         struct selinfo          sc_selinfo;
83 };
84
85 static struct cdevsw ti_pruss_cdevsw = {
86         .d_version =    D_VERSION,
87         .d_name =       "ti_pruss",
88         .d_open =       ti_pruss_open,
89         .d_mmap =       ti_pruss_mmap,
90         .d_kqfilter =   ti_pruss_kqfilter,
91 };
92
93 static device_method_t ti_pruss_methods[] = {
94         DEVMETHOD(device_probe,         ti_pruss_probe),
95         DEVMETHOD(device_attach,        ti_pruss_attach),
96         DEVMETHOD(device_detach,        ti_pruss_detach),
97
98         DEVMETHOD_END
99 };
100
101 static driver_t ti_pruss_driver = {
102         "ti_pruss",
103         ti_pruss_methods,
104         sizeof(struct ti_pruss_softc)
105 };
106
107 static devclass_t ti_pruss_devclass;
108
109 DRIVER_MODULE(ti_pruss, simplebus, ti_pruss_driver, ti_pruss_devclass, 0, 0);
110
111 static struct resource_spec ti_pruss_irq_spec[] = {
112         { SYS_RES_IRQ,      0,  RF_ACTIVE },
113         { SYS_RES_IRQ,      1,  RF_ACTIVE },
114         { SYS_RES_IRQ,      2,  RF_ACTIVE },
115         { SYS_RES_IRQ,      3,  RF_ACTIVE },
116         { SYS_RES_IRQ,      4,  RF_ACTIVE },
117         { SYS_RES_IRQ,      5,  RF_ACTIVE },
118         { SYS_RES_IRQ,      6,  RF_ACTIVE },
119         { SYS_RES_IRQ,      7,  RF_ACTIVE },
120         { -1,               0,  0 }
121 };
122 CTASSERT(TI_PRUSS_IRQS == nitems(ti_pruss_irq_spec) - 1);
123
124 static struct ti_pruss_irq_arg {
125         int                    irq;
126         struct ti_pruss_softc *sc;
127 } ti_pruss_irq_args[TI_PRUSS_IRQS];
128
129 static __inline uint32_t
130 ti_pruss_reg_read(struct ti_pruss_softc *sc, uint32_t reg)
131 {
132         return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
133 }
134
135 static __inline void
136 ti_pruss_reg_write(struct ti_pruss_softc *sc, uint32_t reg, uint32_t val)
137 {
138         bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
139 }
140
141 static int
142 ti_pruss_probe(device_t dev)
143 {
144
145         if (!ofw_bus_status_okay(dev))
146                 return (ENXIO);
147
148         if (ofw_bus_is_compatible(dev, "ti,pruss-v1") ||
149             ofw_bus_is_compatible(dev, "ti,pruss-v2")) {
150                 device_set_desc(dev, "TI Programmable Realtime Unit Subsystem");
151                 return (BUS_PROBE_DEFAULT);
152         }
153
154         return (ENXIO);
155 }
156
157 static int
158 ti_pruss_attach(device_t dev)
159 {
160         struct ti_pruss_softc *sc;
161         int rid, i;
162
163         if (ti_prcm_clk_enable(PRUSS_CLK) != 0) {
164                 device_printf(dev, "could not enable PRUSS clock\n");
165                 return (ENXIO);
166         }
167         sc = device_get_softc(dev);
168         rid = 0;
169         mtx_init(&sc->sc_mtx, "TI PRUSS", NULL, MTX_DEF);
170         knlist_init_mtx(&sc->sc_selinfo.si_note, &sc->sc_mtx);
171         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
172             RF_ACTIVE);
173         if (sc->sc_mem_res == NULL) {
174                 device_printf(dev, "could not allocate memory resource\n");
175                 return (ENXIO);
176         }
177         sc->sc_bt = rman_get_bustag(sc->sc_mem_res);
178         sc->sc_bh = rman_get_bushandle(sc->sc_mem_res);
179         if (bus_alloc_resources(dev, ti_pruss_irq_spec, sc->sc_irq_res) != 0) {
180                 device_printf(dev, "could not allocate interrupt resource\n");
181                 ti_pruss_detach(dev);
182                 return (ENXIO);
183         }
184         for (i = 0; i < TI_PRUSS_IRQS; i++) {
185                 ti_pruss_irq_args[i].irq = i;
186                 ti_pruss_irq_args[i].sc = sc;
187                 if (bus_setup_intr(dev, sc->sc_irq_res[i],
188                     INTR_MPSAFE | INTR_TYPE_MISC,
189                     NULL, ti_pruss_intr, &ti_pruss_irq_args[i],
190                     &sc->sc_intr[i]) != 0) {
191                         device_printf(dev,
192                             "unable to setup the interrupt handler\n");
193                         ti_pruss_detach(dev);
194                         return (ENXIO);
195                 }
196         }
197         if (ti_pruss_reg_read(sc, PRUSS_AM18XX_INTC) == PRUSS_AM18XX_REV)
198                 device_printf(dev, "AM18xx PRU-ICSS\n");
199         else if (ti_pruss_reg_read(sc, PRUSS_AM33XX_INTC) == PRUSS_AM33XX_REV)
200                 device_printf(dev, "AM33xx PRU-ICSS\n");
201
202         sc->sc_pdev = make_dev(&ti_pruss_cdevsw, 0, UID_ROOT, GID_WHEEL,
203             0600, "pruss%d", device_get_unit(dev));
204         sc->sc_pdev->si_drv1 = dev;
205
206         return (0);
207 }
208
209 static int
210 ti_pruss_detach(device_t dev)
211 {
212         struct ti_pruss_softc *sc;
213         int i;
214
215         sc = device_get_softc(dev);
216         for (i = 0; i < TI_PRUSS_IRQS; i++) {
217                 if (sc->sc_intr[i])
218                         bus_teardown_intr(dev, sc->sc_irq_res[i], sc->sc_intr[i]);
219                 if (sc->sc_irq_res[i])
220                         bus_release_resource(dev, SYS_RES_IRQ,
221                             rman_get_rid(sc->sc_irq_res[i]),
222                             sc->sc_irq_res[i]);
223         }
224         knlist_clear(&sc->sc_selinfo.si_note, 0);
225         knlist_destroy(&sc->sc_selinfo.si_note);
226         mtx_destroy(&sc->sc_mtx);
227         if (sc->sc_mem_res)
228                 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res),
229                     sc->sc_mem_res);
230         if (sc->sc_pdev)
231                 destroy_dev(sc->sc_pdev);
232
233         return (0);
234 }
235
236 static void
237 ti_pruss_intr(void *arg)
238 {
239         int val;
240         struct ti_pruss_irq_arg *iap = arg;
241         struct ti_pruss_softc *sc = iap->sc;
242         /*
243          * Interrupts pr1_host_intr[0:7] are mapped to 
244          * Host-2 to Host-9 of PRU-ICSS IRQ-controller.
245          */
246         const int pru_int = iap->irq + 2;
247         const int pru_int_mask = (1 << pru_int);
248
249         val = ti_pruss_reg_read(sc, PRUSS_AM33XX_INTC + PRUSS_INTC_HIER);
250         DPRINTF("interrupt %p, %d", sc, pru_int);
251         if (!(val & pru_int_mask))
252                 return;
253         ti_pruss_reg_write(sc, PRUSS_AM33XX_INTC + PRUSS_INTC_HIDISR, 
254             pru_int);
255         KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, pru_int);
256 }
257
258 static int
259 ti_pruss_open(struct cdev *cdev __unused, int oflags __unused,
260     int devtype __unused, struct thread *td __unused)
261 {
262         return (0);
263 }
264
265 static int
266 ti_pruss_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr,
267     int nprot, vm_memattr_t *memattr)
268 {
269         device_t dev = cdev->si_drv1;
270         struct ti_pruss_softc *sc = device_get_softc(dev);
271
272         if (offset > rman_get_size(sc->sc_mem_res))
273                 return (-1);
274         *paddr = rman_get_start(sc->sc_mem_res) + offset;
275         *memattr = VM_MEMATTR_UNCACHEABLE;
276
277         return (0);
278 }
279
280 static struct filterops ti_pruss_kq_read = {
281         .f_isfd = 1,
282         .f_detach = ti_pruss_kq_read_detach,
283         .f_event = ti_pruss_kq_read_event,
284 };
285
286 static void
287 ti_pruss_kq_read_detach(struct knote *kn)
288 {
289         struct ti_pruss_softc *sc = kn->kn_hook;
290
291         knlist_remove(&sc->sc_selinfo.si_note, kn, 0);
292 }
293
294 static int
295 ti_pruss_kq_read_event(struct knote *kn, long hint)
296 {
297         kn->kn_data = hint;
298
299         return (hint);
300 }
301
302 static int
303 ti_pruss_kqfilter(struct cdev *cdev, struct knote *kn)
304 {
305         device_t dev = cdev->si_drv1;
306         struct ti_pruss_softc *sc = device_get_softc(dev);
307
308         switch (kn->kn_filter) {
309         case EVFILT_READ:
310                 kn->kn_hook = sc;
311                 kn->kn_fop = &ti_pruss_kq_read;
312                 knlist_add(&sc->sc_selinfo.si_note, kn, 0);
313                 break;
314         default:
315                 return (EINVAL);
316         }
317
318         return (0);
319 }