]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/arm/at91/at91_ssc.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / arm / at91 / at91_ssc.c
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$");
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_read_t at91_ssc_read;
92 static d_write_t at91_ssc_write;
93
94 static struct cdevsw at91_ssc_cdevsw =
95 {
96         .d_version = D_VERSION,
97         .d_open = at91_ssc_open,
98         .d_close = at91_ssc_close,
99         .d_read = at91_ssc_read,
100         .d_write = at91_ssc_write,
101 };
102
103 static int
104 at91_ssc_probe(device_t dev)
105 {
106         device_set_desc(dev, "SSC");
107         return (0);
108 }
109
110 static int
111 at91_ssc_attach(device_t dev)
112 {
113         struct at91_ssc_softc *sc = device_get_softc(dev);
114         int err;
115
116         sc->dev = dev;
117         err = at91_ssc_activate(dev);
118         if (err)
119                 goto out;
120
121         AT91_SSC_LOCK_INIT(sc);
122
123         /*
124          * Activate the interrupt
125          */
126         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
127             NULL, at91_ssc_intr, sc, &sc->intrhand);
128         if (err) {
129                 AT91_SSC_LOCK_DESTROY(sc);
130                 goto out;
131         }
132         sc->cdev = make_dev(&at91_ssc_cdevsw, device_get_unit(dev), UID_ROOT,
133             GID_WHEEL, 0600, "ssc%d", device_get_unit(dev));
134         if (sc->cdev == NULL) {
135                 err = ENOMEM;
136                 goto out;
137         }
138         sc->cdev->si_drv1 = sc;
139
140         // Init for TSC needs
141         WR4(sc, SSC_CR, SSC_CR_SWRST);
142         WR4(sc, SSC_CMR, 0);            // clock divider unused
143         WR4(sc, SSC_RCMR,
144             SSC_RCMR_CKS_RK | SSC_RCMR_CKO_NONE | SSC_RCMR_START_FALL_EDGE_RF);
145         WR4(sc, SSC_RFMR,
146             0x1f | SSC_RFMR_MSFBF | SSC_RFMR_FSOS_NONE);
147         WR4(sc, SSC_TCMR,
148             SSC_TCMR_CKS_TK | SSC_TCMR_CKO_NONE |  SSC_RCMR_START_CONT);
149         WR4(sc, SSC_TFMR,
150             0x1f | SSC_TFMR_DATDEF | SSC_TFMR_MSFBF | SSC_TFMR_FSOS_NEG_PULSE);
151
152 out:;
153         if (err)
154                 at91_ssc_deactivate(dev);
155         return (err);
156 }
157
158 static int
159 at91_ssc_detach(device_t dev)
160 {
161         return (EBUSY); /* XXX */
162 }
163
164 static int
165 at91_ssc_activate(device_t dev)
166 {
167         struct at91_ssc_softc *sc;
168         int rid;
169
170         sc = device_get_softc(dev);
171         rid = 0;
172         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
173             RF_ACTIVE);
174         if (sc->mem_res == NULL)
175                 goto errout;
176         rid = 0;
177         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
178             RF_ACTIVE);
179         if (sc->irq_res == NULL)
180                 goto errout;
181         return (0);
182 errout:
183         at91_ssc_deactivate(dev);
184         return (ENOMEM);
185 }
186
187 static void
188 at91_ssc_deactivate(device_t dev)
189 {
190         struct at91_ssc_softc *sc;
191
192         sc = device_get_softc(dev);
193         if (sc->intrhand)
194                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
195         sc->intrhand = 0;
196         bus_generic_detach(sc->dev);
197         if (sc->mem_res)
198                 bus_release_resource(dev, SYS_RES_IOPORT,
199                     rman_get_rid(sc->mem_res), sc->mem_res);
200         sc->mem_res = 0;
201         if (sc->irq_res)
202                 bus_release_resource(dev, SYS_RES_IRQ,
203                     rman_get_rid(sc->irq_res), sc->irq_res);
204         sc->irq_res = 0;
205         return;
206 }
207
208 static void
209 at91_ssc_intr(void *xsc)
210 {
211         struct at91_ssc_softc *sc = xsc;
212 #if 0
213         uint32_t status;
214
215         /* Reading the status also clears the interrupt */
216         status = RD4(sc, SSC_SR);
217         if (status == 0)
218                 return;
219         AT91_SSC_LOCK(sc);
220         AT91_SSC_UNLOCK(sc);
221 #endif
222         wakeup(sc);
223         return;
224 }
225
226 static int 
227 at91_ssc_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
228 {
229         struct at91_ssc_softc *sc;
230
231         sc = CDEV2SOFTC(dev);
232         AT91_SSC_LOCK(sc);
233         if (!(sc->flags & OPENED)) {
234                 sc->flags |= OPENED;
235 #if 0
236         // Enable interrupts
237 #endif
238         }
239         AT91_SSC_UNLOCK(sc);
240         return (0);
241 }
242
243 static int
244 at91_ssc_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
245 {
246         struct at91_ssc_softc *sc;
247
248         sc = CDEV2SOFTC(dev);
249         AT91_SSC_LOCK(sc);
250         sc->flags &= ~OPENED;
251 #if 0
252         // Disable interrupts
253 #endif
254         AT91_SSC_UNLOCK(sc);
255         return (0);
256 }
257
258 static int
259 at91_ssc_read(struct cdev *dev, struct uio *uio, int flag)
260 {
261         return EIO;
262 }
263
264 static int
265 at91_ssc_write(struct cdev *dev, struct uio *uio, int flag)
266 {
267         return EIO;
268 }
269
270 static device_method_t at91_ssc_methods[] = {
271         /* Device interface */
272         DEVMETHOD(device_probe,         at91_ssc_probe),
273         DEVMETHOD(device_attach,        at91_ssc_attach),
274         DEVMETHOD(device_detach,        at91_ssc_detach),
275
276         { 0, 0 }
277 };
278
279 static driver_t at91_ssc_driver = {
280         "at91_ssc",
281         at91_ssc_methods,
282         sizeof(struct at91_ssc_softc),
283 };
284
285 DRIVER_MODULE(at91_ssc, atmelarm, at91_ssc_driver, at91_ssc_devclass, 0, 0);