Deleted Added
sdiff udiff text old ( 225882 ) new ( 234281 )
full compact
1/*-
2 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
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.

--- 10 unchanged lines hidden (view full) ---

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: head/sys/arm/at91/at91_pio.c 225882 2011-09-30 04:55:23Z kevlo $");
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/lock.h>
35#include <sys/mbuf.h>

--- 11 unchanged lines hidden (view full) ---

47{
48 device_t dev; /* Myself */
49 void *intrhand; /* Interrupt handle */
50 struct resource *irq_res; /* IRQ resource */
51 struct resource *mem_res; /* Memory resource */
52 struct mtx sc_mtx; /* basically a perimeter lock */
53 struct cdev *cdev;
54 int flags;
55#define OPENED 1
56};
57
58static inline uint32_t
59RD4(struct at91_pio_softc *sc, bus_size_t off)
60{
61 return (bus_read_4(sc->mem_res, off));
62}
63
64static inline void
65WR4(struct at91_pio_softc *sc, bus_size_t off, uint32_t val)
66{
67 bus_write_4(sc->mem_res, off, val);
68}
69
70#define AT91_PIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
71#define AT91_PIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
72#define AT91_PIO_LOCK_INIT(_sc) \
73 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
74 "pio", MTX_SPIN)
75#define AT91_PIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
76#define AT91_PIO_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
77#define AT91_PIO_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
78#define CDEV2SOFTC(dev) ((dev)->si_drv1)
79
80static devclass_t at91_pio_devclass;
81
82/* bus entry points */
83
84static int at91_pio_probe(device_t dev);
85static int at91_pio_attach(device_t dev);
86static int at91_pio_detach(device_t dev);

--- 40 unchanged lines hidden (view full) ---

127 }
128 device_set_desc(dev, name);
129 return (0);
130}
131
132static int
133at91_pio_attach(device_t dev)
134{
135 struct at91_pio_softc *sc = device_get_softc(dev);
136 int err;
137
138 sc->dev = dev;
139 err = at91_pio_activate(dev);
140 if (err)
141 goto out;
142
143 device_printf(dev, "ABSR: %#x OSR: %#x PSR:%#x ODSR: %#x\n",
144 RD4(sc, PIO_ABSR), RD4(sc, PIO_OSR), RD4(sc, PIO_PSR),
145 RD4(sc, PIO_ODSR));
146 AT91_PIO_LOCK_INIT(sc);
147
148 /*
149 * Activate the interrupt, but disable all interrupts in the hardware
150 */
151 WR4(sc, PIO_IDR, 0xffffffff);
152 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
153 at91_pio_intr, NULL, sc, &sc->intrhand);
154 if (err) {
155 AT91_PIO_LOCK_DESTROY(sc);
156 goto out;
157 }

--- 8 unchanged lines hidden (view full) ---

166 if (err)
167 at91_pio_deactivate(dev);
168 return (err);
169}
170
171static int
172at91_pio_detach(device_t dev)
173{
174 return (EBUSY); /* XXX */
175}
176
177static int
178at91_pio_activate(device_t dev)
179{
180 struct at91_pio_softc *sc;
181 int rid;

--- 28 unchanged lines hidden (view full) ---

210 if (sc->mem_res)
211 bus_release_resource(dev, SYS_RES_IOPORT,
212 rman_get_rid(sc->mem_res), sc->mem_res);
213 sc->mem_res = 0;
214 if (sc->irq_res)
215 bus_release_resource(dev, SYS_RES_IRQ,
216 rman_get_rid(sc->irq_res), sc->irq_res);
217 sc->irq_res = 0;
218 return;
219}
220
221static int
222at91_pio_intr(void *xsc)
223{
224 struct at91_pio_softc *sc = xsc;
225#if 0
226 uint32_t status;
227
228 /* Reading the status also clears the interrupt */
229 status = RD4(sc, PIO_SR);
230 if (status == 0)
231 return;
232 AT91_PIO_LOCK(sc);
233 AT91_PIO_UNLOCK(sc);
234#endif
235 wakeup(sc);
236 return (FILTER_HANDLED);
237}
238
239static int
240at91_pio_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
241{
242 struct at91_pio_softc *sc;
243
244 sc = CDEV2SOFTC(dev);
245 AT91_PIO_LOCK(sc);
246 if (!(sc->flags & OPENED)) {
247 sc->flags |= OPENED;
248#if 0
249 // Enable interrupts
250#endif
251 }
252 AT91_PIO_UNLOCK(sc);
253 return (0);
254}
255
256static int
257at91_pio_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
258{
259 struct at91_pio_softc *sc;
260
261 sc = CDEV2SOFTC(dev);
262 AT91_PIO_LOCK(sc);
263 sc->flags &= ~OPENED;
264#if 0
265 // Disable interrupts
266#endif
267 AT91_PIO_UNLOCK(sc);
268 return (0);
269}
270
271static int
272at91_pio_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
273 struct thread *td)
274{
275 return (ENXIO);
276}
277
278/*
279 * The following functions are called early in the boot process, so
280 * don't use bus_space, as that isn't yet available when we need to use
281 * them.
282 */
283void
284at91_pio_use_periph_a(uint32_t pio, uint32_t periph_a_mask, int use_pullup)
285{
286 uint32_t *PIO = (uint32_t *)(AT91_BASE + pio);
287
288 PIO[PIO_ASR / 4] = periph_a_mask;
289 PIO[PIO_PDR / 4] = periph_a_mask;
290 if (use_pullup)

--- 73 unchanged lines hidden (view full) ---

364at91_pio_gpio_set_deglitch(uint32_t pio, uint32_t data_mask, int use_deglitch)
365{
366 uint32_t *PIO = (uint32_t *)(AT91_BASE + pio);
367
368 if (use_deglitch)
369 PIO[PIO_IFER / 4] = data_mask;
370 else
371 PIO[PIO_IFDR / 4] = data_mask;
372 return;
373}
374
375void
376at91_pio_gpio_set_interrupt(uint32_t pio, uint32_t data_mask,
377 int enable_interrupt)
378{
379 uint32_t *PIO = (uint32_t *)(AT91_BASE + pio);
380
381 if (enable_interrupt)
382 PIO[PIO_IER / 4] = data_mask;
383 else
384 PIO[PIO_IDR / 4] = data_mask;
385 return;
386}
387
388uint32_t
389at91_pio_gpio_clear_interrupt(uint32_t pio)
390{
391 uint32_t *PIO = (uint32_t *)(AT91_BASE + pio);
392 /* reading this register will clear the interrupts */
393 return (PIO[PIO_ISR / 4]);
394}
395
396static device_method_t at91_pio_methods[] = {
397 /* Device interface */
398 DEVMETHOD(device_probe, at91_pio_probe),
399 DEVMETHOD(device_attach, at91_pio_attach),
400 DEVMETHOD(device_detach, at91_pio_detach),
401
402 { 0, 0 }
403};
404
405static driver_t at91_pio_driver = {
406 "at91_pio",
407 at91_pio_methods,
408 sizeof(struct at91_pio_softc),
409};
410
411DRIVER_MODULE(at91_pio, atmelarm, at91_pio_driver, at91_pio_devclass, 0, 0);