]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_ssc.c
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_ssc.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <machine/bus.h>
41
42 #include <arm/at91/at91_sscreg.h>
43
44 struct at91_ssc_softc
45 {
46         device_t dev;                   /* Myself */
47         void *intrhand;                 /* Interrupt handle */
48         struct resource *irq_res;       /* IRQ resource */
49         struct resource *mem_res;       /* Memory resource */
50         struct mtx sc_mtx;              /* basically a perimeter lock */
51         struct cdev *cdev;
52         int flags;
53 #define OPENED 1
54 };
55
56 static inline uint32_t
57 RD4(struct at91_ssc_softc *sc, bus_size_t off)
58 {
59         return bus_read_4(sc->mem_res, off);
60 }
61
62 static inline void
63 WR4(struct at91_ssc_softc *sc, bus_size_t off, uint32_t val)
64 {
65         bus_write_4(sc->mem_res, off, val);
66 }
67
68 #define AT91_SSC_LOCK(_sc)              mtx_lock(&(_sc)->sc_mtx)
69 #define AT91_SSC_UNLOCK(_sc)            mtx_unlock(&(_sc)->sc_mtx)
70 #define AT91_SSC_LOCK_INIT(_sc) \
71         mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \
72             "ssc", MTX_DEF)
73 #define AT91_SSC_LOCK_DESTROY(_sc)      mtx_destroy(&(_sc)->sc_mtx);
74 #define AT91_SSC_ASSERT_LOCKED(_sc)     mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
75 #define AT91_SSC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
76 #define CDEV2SOFTC(dev)         ((dev)->si_drv1)
77
78 static devclass_t at91_ssc_devclass;
79
80 /* bus entry points */
81
82 static int at91_ssc_probe(device_t dev);
83 static int at91_ssc_attach(device_t dev);
84 static int at91_ssc_detach(device_t dev);
85 static void at91_ssc_intr(void *);
86
87 /* helper routines */
88 static int at91_ssc_activate(device_t dev);
89 static void at91_ssc_deactivate(device_t dev);
90
91 /* cdev routines */
92 static d_open_t at91_ssc_open;
93 static d_close_t at91_ssc_close;
94 static d_read_t at91_ssc_read;
95 static d_write_t at91_ssc_write;
96
97 static struct cdevsw at91_ssc_cdevsw =
98 {
99         .d_version = D_VERSION,
100         .d_open = at91_ssc_open,
101         .d_close = at91_ssc_close,
102         .d_read = at91_ssc_read,
103         .d_write = at91_ssc_write,
104 };
105
106 static int
107 at91_ssc_probe(device_t dev)
108 {
109         device_set_desc(dev, "SSC");
110         return (0);
111 }
112
113 static int
114 at91_ssc_attach(device_t dev)
115 {
116         struct at91_ssc_softc *sc = device_get_softc(dev);
117         int err;
118
119         sc->dev = dev;
120         err = at91_ssc_activate(dev);
121         if (err)
122                 goto out;
123
124         AT91_SSC_LOCK_INIT(sc);
125
126         /*
127          * Activate the interrupt
128          */
129         err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
130             NULL, at91_ssc_intr, sc, &sc->intrhand);
131         if (err) {
132                 AT91_SSC_LOCK_DESTROY(sc);
133                 goto out;
134         }
135         sc->cdev = make_dev(&at91_ssc_cdevsw, device_get_unit(dev), UID_ROOT,
136             GID_WHEEL, 0600, "ssc%d", device_get_unit(dev));
137         if (sc->cdev == NULL) {
138                 err = ENOMEM;
139                 goto out;
140         }
141         sc->cdev->si_drv1 = sc;
142
143         // Init for TSC needs
144         WR4(sc, SSC_CR, SSC_CR_SWRST);
145         WR4(sc, SSC_CMR, 0);            // clock divider unused
146         WR4(sc, SSC_RCMR,
147             SSC_RCMR_CKS_RK | SSC_RCMR_CKO_NONE | SSC_RCMR_START_FALL_EDGE_RF);
148         WR4(sc, SSC_RFMR,
149             0x1f | SSC_RFMR_MSFBF | SSC_RFMR_FSOS_NONE);
150         WR4(sc, SSC_TCMR,
151             SSC_TCMR_CKS_TK | SSC_TCMR_CKO_NONE |  SSC_RCMR_START_CONT);
152         WR4(sc, SSC_TFMR,
153             0x1f | SSC_TFMR_DATDEF | SSC_TFMR_MSFBF | SSC_TFMR_FSOS_NEG_PULSE);
154
155 out:
156         if (err)
157                 at91_ssc_deactivate(dev);
158         return (err);
159 }
160
161 static int
162 at91_ssc_detach(device_t dev)
163 {
164         return (EBUSY); /* XXX */
165 }
166
167 static int
168 at91_ssc_activate(device_t dev)
169 {
170         struct at91_ssc_softc *sc;
171         int rid;
172
173         sc = device_get_softc(dev);
174         rid = 0;
175         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
176             RF_ACTIVE);
177         if (sc->mem_res == NULL)
178                 goto errout;
179         rid = 0;
180         sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
181             RF_ACTIVE);
182         if (sc->irq_res == NULL)
183                 goto errout;
184         return (0);
185 errout:
186         at91_ssc_deactivate(dev);
187         return (ENOMEM);
188 }
189
190 static void
191 at91_ssc_deactivate(device_t dev)
192 {
193         struct at91_ssc_softc *sc;
194
195         sc = device_get_softc(dev);
196         if (sc->intrhand)
197                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
198         sc->intrhand = NULL;
199         bus_generic_detach(sc->dev);
200         if (sc->mem_res)
201                 bus_release_resource(dev, SYS_RES_IOPORT,
202                     rman_get_rid(sc->mem_res), sc->mem_res);
203         sc->mem_res = NULL;
204         if (sc->irq_res)
205                 bus_release_resource(dev, SYS_RES_IRQ,
206                     rman_get_rid(sc->irq_res), sc->irq_res);
207         sc->irq_res = NULL;
208         return;
209 }
210
211 static void
212 at91_ssc_intr(void *xsc)
213 {
214         struct at91_ssc_softc *sc = xsc;
215         wakeup(sc);
216         return;
217 }
218
219 static int
220 at91_ssc_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
221 {
222         struct at91_ssc_softc *sc;
223
224         sc = CDEV2SOFTC(dev);
225         AT91_SSC_LOCK(sc);
226         if (!(sc->flags & OPENED)) {
227                 sc->flags |= OPENED;
228         }
229         AT91_SSC_UNLOCK(sc);
230         return (0);
231 }
232
233 static int
234 at91_ssc_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
235 {
236         struct at91_ssc_softc *sc;
237
238         sc = CDEV2SOFTC(dev);
239         AT91_SSC_LOCK(sc);
240         sc->flags &= ~OPENED;
241         AT91_SSC_UNLOCK(sc);
242         return (0);
243 }
244
245 static int
246 at91_ssc_read(struct cdev *dev, struct uio *uio, int flag)
247 {
248         return EIO;
249 }
250
251 static int
252 at91_ssc_write(struct cdev *dev, struct uio *uio, int flag)
253 {
254         return EIO;
255 }
256
257 static device_method_t at91_ssc_methods[] = {
258         /* Device interface */
259         DEVMETHOD(device_probe,         at91_ssc_probe),
260         DEVMETHOD(device_attach,        at91_ssc_attach),
261         DEVMETHOD(device_detach,        at91_ssc_detach),
262
263         { 0, 0 }
264 };
265
266 static driver_t at91_ssc_driver = {
267         "at91_ssc",
268         at91_ssc_methods,
269         sizeof(struct at91_ssc_softc),
270 };
271
272 DRIVER_MODULE(at91_ssc, atmelarm, at91_ssc_driver, at91_ssc_devclass, 0, 0);