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.
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 ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD: releng/6.3/sys/arm/at91/at91_ssc.c 160481 2006-07-18 21:56:02Z imp $");
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/conf.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/rman.h>
37 #include <machine/bus.h>
38
39 #include <arm/at91/at91_sscreg.h>
40
41 struct at91_ssc_softc
42 {
43 device_t dev; /* Myself */
44 void *intrhand; /* Interrupt handle */
45 struct resource *irq_res; /* IRQ resource */
46 struct resource *mem_res; /* Memory resource */
47 struct mtx sc_mtx; /* basically a perimeter lock */
48 struct cdev *cdev;
49 int flags;
50 #define OPENED 1
51 };
52
53 static inline uint32_t
54 RD4(struct at91_ssc_softc *sc, bus_size_t off)
55 {
56 return bus_read_4(sc->mem_res, off);
57 }
58
59 static inline void
60 WR4(struct at91_ssc_softc *sc, bus_size_t off, uint32_t val)
61 {
62 bus_write_4(sc->mem_res, off, val);
63 }
64
65 #define AT91_SSC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
66 #define AT91_SSC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
67 #define AT91_SSC_LOCK_INIT(_sc) \
68 mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \
69 "ssc", MTX_DEF)
70 #define AT91_SSC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
71 #define AT91_SSC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
72 #define AT91_SSC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
73 #define CDEV2SOFTC(dev) ((dev)->si_drv1)
74
75 static devclass_t at91_ssc_devclass;
76
77 /* bus entry points */
78
79 static int at91_ssc_probe(device_t dev);
80 static int at91_ssc_attach(device_t dev);
81 static int at91_ssc_detach(device_t dev);
82 static void at91_ssc_intr(void *);
83
84 /* helper routines */
85 static int at91_ssc_activate(device_t dev);
86 static void at91_ssc_deactivate(device_t dev);
87
88 /* cdev routines */
89 static d_open_t at91_ssc_open;
90 static d_close_t at91_ssc_close;
91 static d_ioctl_t at91_ssc_ioctl;
92
93 static struct cdevsw at91_ssc_cdevsw =
94 {
95 .d_version = D_VERSION,
96 .d_open = at91_ssc_open,
97 .d_close = at91_ssc_close,
98 .d_ioctl = at91_ssc_ioctl
99 };
100
101 static int
102 at91_ssc_probe(device_t dev)
103 {
104 device_set_desc(dev, "SSC");
105 return (0);
106 }
107
108 static int
109 at91_ssc_attach(device_t dev)
110 {
111 struct at91_ssc_softc *sc = device_get_softc(dev);
112 int err;
113
114 sc->dev = dev;
115 err = at91_ssc_activate(dev);
116 if (err)
117 goto out;
118
119 AT91_SSC_LOCK_INIT(sc);
120
121 /*
122 * Activate the interrupt
123 */
124 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
125 at91_ssc_intr, sc, &sc->intrhand);
126 if (err) {
127 AT91_SSC_LOCK_DESTROY(sc);
128 goto out;
129 }
130 sc->cdev = make_dev(&at91_ssc_cdevsw, device_get_unit(dev), UID_ROOT,
131 GID_WHEEL, 0600, "ssc%d", device_get_unit(dev));
132 if (sc->cdev == NULL) {
133 err = ENOMEM;
134 goto out;
135 }
136 sc->cdev->si_drv1 = sc;
137 out:;
138 if (err)
139 at91_ssc_deactivate(dev);
140 return (err);
141 }
142
143 static int
144 at91_ssc_detach(device_t dev)
145 {
146 return (EBUSY); /* XXX */
147 }
148
149 static int
150 at91_ssc_activate(device_t dev)
151 {
152 struct at91_ssc_softc *sc;
153 int rid;
154
155 sc = device_get_softc(dev);
156 rid = 0;
157 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
158 RF_ACTIVE);
159 if (sc->mem_res == NULL)
160 goto errout;
161 rid = 0;
162 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
163 RF_ACTIVE);
164 if (sc->mem_res == NULL)
165 goto errout;
166 return (0);
167 errout:
168 at91_ssc_deactivate(dev);
169 return (ENOMEM);
170 }
171
172 static void
173 at91_ssc_deactivate(device_t dev)
174 {
175 struct at91_ssc_softc *sc;
176
177 sc = device_get_softc(dev);
178 if (sc->intrhand)
179 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
180 sc->intrhand = 0;
181 bus_generic_detach(sc->dev);
182 if (sc->mem_res)
183 bus_release_resource(dev, SYS_RES_IOPORT,
184 rman_get_rid(sc->mem_res), sc->mem_res);
185 sc->mem_res = 0;
186 if (sc->irq_res)
187 bus_release_resource(dev, SYS_RES_IRQ,
188 rman_get_rid(sc->irq_res), sc->irq_res);
189 sc->irq_res = 0;
190 return;
191 }
192
193 static void
194 at91_ssc_intr(void *xsc)
195 {
196 struct at91_ssc_softc *sc = xsc;
197 #if 0
198 uint32_t status;
199
200 /* Reading the status also clears the interrupt */
201 status = RD4(sc, SSC_SR);
202 if (status == 0)
203 return;
204 AT91_SSC_LOCK(sc);
205 AT91_SSC_UNLOCK(sc);
206 #endif
207 wakeup(sc);
208 return;
209 }
210
211 static int
212 at91_ssc_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
213 {
214 struct at91_ssc_softc *sc;
215
216 sc = CDEV2SOFTC(dev);
217 AT91_SSC_LOCK(sc);
218 if (!(sc->flags & OPENED)) {
219 sc->flags |= OPENED;
220 #if 0
221 // Enable interrupts
222 #endif
223 }
224 AT91_SSC_UNLOCK(sc);
225 return (0);
226 }
227
228 static int
229 at91_ssc_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
230 {
231 struct at91_ssc_softc *sc;
232
233 sc = CDEV2SOFTC(dev);
234 AT91_SSC_LOCK(sc);
235 sc->flags &= ~OPENED;
236 #if 0
237 // Disable interrupts
238 #endif
239 AT91_SSC_UNLOCK(sc);
240 return (0);
241 }
242
243 static int
244 at91_ssc_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
245 struct thread *td)
246 {
247 return (ENXIO);
248 }
249
250 static device_method_t at91_ssc_methods[] = {
251 /* Device interface */
252 DEVMETHOD(device_probe, at91_ssc_probe),
253 DEVMETHOD(device_attach, at91_ssc_attach),
254 DEVMETHOD(device_detach, at91_ssc_detach),
255
256 { 0, 0 }
257 };
258
259 static driver_t at91_ssc_driver = {
260 "at91_ssc",
261 at91_ssc_methods,
262 sizeof(struct at91_ssc_softc),
263 };
264
265 DRIVER_MODULE(at91_ssc, atmelarm, at91_ssc_driver, at91_ssc_devclass, 0, 0);
Cache object: 6ed71b97d648541727ed234feb3764ef
|