bcm2835_pwm.c revision 1.1 1 1.1 mlelstv /* $NetBSD: bcm2835_pwm.c,v 1.1 2015/11/21 07:41:29 mlelstv Exp $ */
2 1.1 mlelstv
3 1.1 mlelstv /*-
4 1.1 mlelstv * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 mlelstv * All rights reserved.
6 1.1 mlelstv *
7 1.1 mlelstv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mlelstv * by Michael van Elst
9 1.1 mlelstv *
10 1.1 mlelstv * Redistribution and use in source and binary forms, with or without
11 1.1 mlelstv * modification, are permitted provided that the following conditions
12 1.1 mlelstv * are met:
13 1.1 mlelstv * 1. Redistributions of source code must retain the above copyright
14 1.1 mlelstv * notice, this list of conditions and the following disclaimer.
15 1.1 mlelstv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mlelstv * notice, this list of conditions and the following disclaimer in the
17 1.1 mlelstv * documentation and/or other materials provided with the distribution.
18 1.1 mlelstv *
19 1.1 mlelstv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 mlelstv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 mlelstv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 mlelstv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 mlelstv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 mlelstv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 mlelstv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 mlelstv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 mlelstv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 mlelstv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 mlelstv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mlelstv */
31 1.1 mlelstv
32 1.1 mlelstv /*
33 1.1 mlelstv * Driver for BCM2835 Pulse Width Modulator
34 1.1 mlelstv *
35 1.1 mlelstv * Each channel can be allocated and used individually, but
36 1.1 mlelstv * for FIFO usage, both channels must to be requested.
37 1.1 mlelstv */
38 1.1 mlelstv
39 1.1 mlelstv #include <sys/cdefs.h>
40 1.1 mlelstv __KERNEL_RCSID(0, "$NetBSD: bcm2835_pwm.c,v 1.1 2015/11/21 07:41:29 mlelstv Exp $");
41 1.1 mlelstv
42 1.1 mlelstv #include "bcmdmac.h"
43 1.1 mlelstv
44 1.1 mlelstv #include <sys/param.h>
45 1.1 mlelstv #include <sys/systm.h>
46 1.1 mlelstv #include <sys/device.h>
47 1.1 mlelstv #include <sys/kernel.h>
48 1.1 mlelstv #include <sys/bus.h>
49 1.1 mlelstv #include <sys/atomic.h>
50 1.1 mlelstv #include <sys/intr.h>
51 1.1 mlelstv
52 1.1 mlelstv #include <arm/broadcom/bcm2835reg.h>
53 1.1 mlelstv #include <arm/broadcom/bcm_amba.h>
54 1.1 mlelstv
55 1.1 mlelstv #include <arm/broadcom/bcm2835_pwm.h>
56 1.1 mlelstv
57 1.1 mlelstv struct bcm_pwm_channel {
58 1.1 mlelstv struct bcm2835pwm_softc *sc;
59 1.1 mlelstv uint32_t ctlmask, stamask, gapomask;
60 1.1 mlelstv int rng, dat;
61 1.1 mlelstv bool inuse;
62 1.1 mlelstv uint32_t ctlsave, rngsave, datsave;
63 1.1 mlelstv };
64 1.1 mlelstv
65 1.1 mlelstv struct bcm2835pwm_softc {
66 1.1 mlelstv device_t sc_dev;
67 1.1 mlelstv
68 1.1 mlelstv bus_space_tag_t sc_iot;
69 1.1 mlelstv bus_space_handle_t sc_ioh;
70 1.1 mlelstv
71 1.1 mlelstv int sc_clockrate;
72 1.1 mlelstv struct bcm_pwm_channel sc_channels[2];
73 1.1 mlelstv kmutex_t sc_lock;
74 1.1 mlelstv
75 1.1 mlelstv uint32_t sc_physaddr;
76 1.1 mlelstv };
77 1.1 mlelstv
78 1.1 mlelstv #define PWM_WRITE(sc, reg, val) \
79 1.1 mlelstv bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
80 1.1 mlelstv #define PWM_READ(sc, reg) \
81 1.1 mlelstv bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
82 1.1 mlelstv
83 1.1 mlelstv static int bcmpwm_match(device_t, cfdata_t, void *);
84 1.1 mlelstv static void bcmpwm_attach(device_t, device_t, void *);
85 1.1 mlelstv static int bcmpwm_wait(struct bcm2835pwm_softc *);
86 1.1 mlelstv
87 1.1 mlelstv CFATTACH_DECL_NEW(bcmpwm_amba, sizeof(struct bcm2835pwm_softc),
88 1.1 mlelstv bcmpwm_match, bcmpwm_attach, NULL, NULL);
89 1.1 mlelstv
90 1.1 mlelstv /* ARGSUSED */
91 1.1 mlelstv static int
92 1.1 mlelstv bcmpwm_match(device_t parent, cfdata_t match, void *aux)
93 1.1 mlelstv {
94 1.1 mlelstv struct amba_attach_args *aaa = aux;
95 1.1 mlelstv
96 1.1 mlelstv if (strcmp(aaa->aaa_name, "bcmpwm") != 0)
97 1.1 mlelstv return 0;
98 1.1 mlelstv
99 1.1 mlelstv if (aaa->aaa_addr != BCM2835_PWM_BASE)
100 1.1 mlelstv return 0;
101 1.1 mlelstv
102 1.1 mlelstv return 1;
103 1.1 mlelstv }
104 1.1 mlelstv
105 1.1 mlelstv static void
106 1.1 mlelstv bcmpwm_attach(device_t parent, device_t self, void *aux)
107 1.1 mlelstv {
108 1.1 mlelstv struct bcm2835pwm_softc *sc = device_private(self);
109 1.1 mlelstv struct amba_attach_args *aaa = aux;
110 1.1 mlelstv const prop_dictionary_t cfg = device_properties(self);
111 1.1 mlelstv
112 1.1 mlelstv aprint_naive("\n");
113 1.1 mlelstv aprint_normal(": PWM\n");
114 1.1 mlelstv
115 1.1 mlelstv sc->sc_dev = self;
116 1.1 mlelstv sc->sc_iot = aaa->aaa_iot;
117 1.1 mlelstv sc->sc_physaddr = aaa->aaa_addr;
118 1.1 mlelstv
119 1.1 mlelstv if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_PWM_SIZE, 0,
120 1.1 mlelstv &sc->sc_ioh)) {
121 1.1 mlelstv aprint_error_dev(sc->sc_dev, "unable to map device\n");
122 1.1 mlelstv goto fail0;
123 1.1 mlelstv }
124 1.1 mlelstv
125 1.1 mlelstv prop_dictionary_get_uint32(cfg, "pwmclockrate", &sc->sc_clockrate);
126 1.1 mlelstv
127 1.1 mlelstv sc->sc_channels[0].sc = sc;
128 1.1 mlelstv sc->sc_channels[0].ctlmask = PWM_CTL_MSEN1 | PWM_CTL_USEF1 |
129 1.1 mlelstv PWM_CTL_POLA1 | PWM_CTL_SBIT1 |
130 1.1 mlelstv PWM_CTL_RPTL1 | PWM_CTL_MODE1 |
131 1.1 mlelstv PWM_CTL_PWEN1;
132 1.1 mlelstv sc->sc_channels[0].stamask = PWM_STA_STA1;
133 1.1 mlelstv sc->sc_channels[0].gapomask = PWM_STA_GAPO1;
134 1.1 mlelstv sc->sc_channels[0].rng = PWM_RNG1;
135 1.1 mlelstv sc->sc_channels[0].dat = PWM_DAT1;
136 1.1 mlelstv
137 1.1 mlelstv sc->sc_channels[1].sc = sc;
138 1.1 mlelstv sc->sc_channels[1].ctlmask = PWM_CTL_MSEN2 | PWM_CTL_USEF2 |
139 1.1 mlelstv PWM_CTL_POLA2 | PWM_CTL_SBIT2 |
140 1.1 mlelstv PWM_CTL_RPTL2 | PWM_CTL_MODE2 |
141 1.1 mlelstv PWM_CTL_PWEN2;
142 1.1 mlelstv sc->sc_channels[1].stamask = PWM_STA_STA2;
143 1.1 mlelstv sc->sc_channels[1].gapomask = PWM_STA_GAPO2;
144 1.1 mlelstv sc->sc_channels[1].rng = PWM_RNG2;
145 1.1 mlelstv sc->sc_channels[1].dat = PWM_DAT2;
146 1.1 mlelstv
147 1.1 mlelstv /* The PWM hardware can be used by vcaudio if the
148 1.1 mlelstv * analog output is selected
149 1.1 mlelstv */
150 1.1 mlelstv sc->sc_channels[0].inuse = false;
151 1.1 mlelstv sc->sc_channels[1].inuse = false;
152 1.1 mlelstv
153 1.1 mlelstv mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
154 1.1 mlelstv
155 1.1 mlelstv /* Success! */
156 1.1 mlelstv
157 1.1 mlelstv fail0: return;
158 1.1 mlelstv }
159 1.1 mlelstv
160 1.1 mlelstv struct bcm_pwm_channel *
161 1.1 mlelstv bcm_pwm_alloc(int num)
162 1.1 mlelstv {
163 1.1 mlelstv struct bcm2835pwm_softc *sc;
164 1.1 mlelstv device_t dev;
165 1.1 mlelstv struct bcm_pwm_channel *pwm;
166 1.1 mlelstv
167 1.1 mlelstv dev = device_find_by_driver_unit("bcmpwm", 0);
168 1.1 mlelstv if (dev == NULL)
169 1.1 mlelstv return NULL;
170 1.1 mlelstv sc = device_private(dev);
171 1.1 mlelstv
172 1.1 mlelstv if (num < 0 || num >= __arraycount(sc->sc_channels))
173 1.1 mlelstv return NULL;
174 1.1 mlelstv
175 1.1 mlelstv pwm = &sc->sc_channels[num];
176 1.1 mlelstv
177 1.1 mlelstv mutex_enter(&sc->sc_lock);
178 1.1 mlelstv if (pwm->inuse)
179 1.1 mlelstv pwm = NULL;
180 1.1 mlelstv else
181 1.1 mlelstv pwm->inuse = true;
182 1.1 mlelstv mutex_exit(&sc->sc_lock);
183 1.1 mlelstv
184 1.1 mlelstv if (pwm) {
185 1.1 mlelstv pwm->datsave = PWM_READ(pwm->sc, pwm->dat);
186 1.1 mlelstv pwm->ctlsave = PWM_READ(pwm->sc, PWM_CTL);
187 1.1 mlelstv pwm->rngsave = PWM_READ(pwm->sc, pwm->rng);
188 1.1 mlelstv }
189 1.1 mlelstv
190 1.1 mlelstv return pwm;
191 1.1 mlelstv }
192 1.1 mlelstv
193 1.1 mlelstv void
194 1.1 mlelstv bcm_pwm_free(struct bcm_pwm_channel *pwm)
195 1.1 mlelstv {
196 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
197 1.1 mlelstv
198 1.1 mlelstv KASSERT(pwm->inuse);
199 1.1 mlelstv
200 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->rng, pwm->rngsave);
201 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_CTL, pwm->ctlsave & ~PWM_CTL_WRITEZERO);
202 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->dat, pwm->datsave);
203 1.1 mlelstv
204 1.1 mlelstv mutex_enter(&sc->sc_lock);
205 1.1 mlelstv pwm->inuse = false;
206 1.1 mlelstv mutex_exit(&sc->sc_lock);
207 1.1 mlelstv }
208 1.1 mlelstv
209 1.1 mlelstv void
210 1.1 mlelstv bcm_pwm_control(struct bcm_pwm_channel *pwm, uint32_t ctl, uint32_t rng)
211 1.1 mlelstv {
212 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
213 1.1 mlelstv uint32_t w;
214 1.1 mlelstv
215 1.1 mlelstv KASSERT(pwm->inuse);
216 1.1 mlelstv
217 1.1 mlelstv /* set control bits like for channel 0
218 1.1 mlelstv * there are generic bit definitions that the caller can use.
219 1.1 mlelstv */
220 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_CTL);
221 1.1 mlelstv ctl = (w & ~pwm->ctlmask) | __SHIFTIN(ctl, pwm->ctlmask);
222 1.1 mlelstv
223 1.1 mlelstv /* when FIFO usage gets enabled but wasn't clear the FIFO */
224 1.1 mlelstv if ((w & (PWM_CTL_USEF1|PWM_CTL_USEF2)) == 0 &&
225 1.1 mlelstv (ctl & (PWM_CTL_USEF1|PWM_CTL_USEF2)) != 0)
226 1.1 mlelstv ctl |= PWM_CTL_CLRF1;
227 1.1 mlelstv
228 1.1 mlelstv PWM_WRITE(sc, pwm->rng, rng);
229 1.1 mlelstv PWM_WRITE(sc, PWM_CTL, ctl & ~PWM_CTL_WRITEZERO);
230 1.1 mlelstv }
231 1.1 mlelstv
232 1.1 mlelstv uint32_t
233 1.1 mlelstv bcm_pwm_status(struct bcm_pwm_channel *pwm)
234 1.1 mlelstv {
235 1.1 mlelstv uint32_t w;
236 1.1 mlelstv uint32_t common = PWM_STA_BERR | PWM_STA_RERR1 |
237 1.1 mlelstv PWM_STA_WERR1 | PWM_STA_EMPT1 | PWM_STA_FULL1;
238 1.1 mlelstv
239 1.1 mlelstv /* return status bits like for channel 0
240 1.1 mlelstv * there are generic bit definitions that the caller can use.
241 1.1 mlelstv *
242 1.1 mlelstv * The BERR bit is returned for both channels.
243 1.1 mlelstv */
244 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_STA);
245 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_STA, w &
246 1.1 mlelstv (pwm->stamask | pwm->gapomask | common));
247 1.1 mlelstv
248 1.1 mlelstv w = __SHIFTIN(__SHIFTOUT(w, pwm->stamask), PWM_STA_STA)
249 1.1 mlelstv | __SHIFTIN(__SHIFTOUT(w, pwm->gapomask), PWM_STA_GAPO)
250 1.1 mlelstv | (w & common);
251 1.1 mlelstv
252 1.1 mlelstv return w;
253 1.1 mlelstv }
254 1.1 mlelstv
255 1.1 mlelstv static int
256 1.1 mlelstv bcmpwm_wait(struct bcm2835pwm_softc *sc)
257 1.1 mlelstv {
258 1.1 mlelstv int i;
259 1.1 mlelstv uint32_t s;
260 1.1 mlelstv
261 1.1 mlelstv for (i=0; i<1000; ++i) {
262 1.1 mlelstv s = PWM_READ(sc, PWM_STA);
263 1.1 mlelstv if ((s & PWM_STA_FULL1) == 0)
264 1.1 mlelstv break;
265 1.1 mlelstv delay(1);
266 1.1 mlelstv }
267 1.1 mlelstv if (i >= 1000)
268 1.1 mlelstv return -1;
269 1.1 mlelstv
270 1.1 mlelstv return 0;
271 1.1 mlelstv }
272 1.1 mlelstv
273 1.1 mlelstv int
274 1.1 mlelstv bcm_pwm_write(struct bcm_pwm_channel *pwm, uint32_t *data1, uint32_t *data2,
275 1.1 mlelstv int len)
276 1.1 mlelstv {
277 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
278 1.1 mlelstv int n;
279 1.1 mlelstv uint32_t r;
280 1.1 mlelstv bool even = false;
281 1.1 mlelstv
282 1.1 mlelstv KASSERT(pwm->inuse);
283 1.1 mlelstv
284 1.1 mlelstv n = len;
285 1.1 mlelstv while (n > 0) {
286 1.1 mlelstv if (bcmpwm_wait(sc))
287 1.1 mlelstv break;
288 1.1 mlelstv r = even ? *data2++ : *data1++;
289 1.1 mlelstv PWM_WRITE(sc, PWM_FIFO, r);
290 1.1 mlelstv if (data2 != NULL)
291 1.1 mlelstv even = !even;
292 1.1 mlelstv --n;
293 1.1 mlelstv }
294 1.1 mlelstv
295 1.1 mlelstv return len - n;
296 1.1 mlelstv }
297 1.1 mlelstv
298 1.1 mlelstv void
299 1.1 mlelstv bcm_pwm_set(struct bcm_pwm_channel *pwm, uint32_t w)
300 1.1 mlelstv {
301 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
302 1.1 mlelstv
303 1.1 mlelstv PWM_WRITE(sc, pwm->dat, w);
304 1.1 mlelstv }
305 1.1 mlelstv
306 1.1 mlelstv int
307 1.1 mlelstv bcm_pwm_flush(struct bcm_pwm_channel *pwm)
308 1.1 mlelstv {
309 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
310 1.1 mlelstv
311 1.1 mlelstv return bcmpwm_wait(sc) ? EIO : 0;
312 1.1 mlelstv }
313 1.1 mlelstv
314 1.1 mlelstv void
315 1.1 mlelstv bcm_pwm_dma_enable(struct bcm_pwm_channel *pwm, bool enable)
316 1.1 mlelstv {
317 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
318 1.1 mlelstv uint32_t w;
319 1.1 mlelstv
320 1.1 mlelstv #if 0
321 1.1 mlelstv w = PWM_READ(sc, PWM_DMAC);
322 1.1 mlelstv if (enable)
323 1.1 mlelstv w |= PWM_DMAC_ENAB;
324 1.1 mlelstv else
325 1.1 mlelstv w &= ~PWM_DMAC_ENAB;
326 1.1 mlelstv #else
327 1.1 mlelstv w = (enable ? PWM_DMAC_ENAB : 0)
328 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_PANIC)
329 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_DREQ);
330 1.1 mlelstv #endif
331 1.1 mlelstv PWM_WRITE(sc, PWM_DMAC, w & ~PWM_DMAC_WRITEZERO);
332 1.1 mlelstv }
333 1.1 mlelstv
334 1.1 mlelstv uint32_t
335 1.1 mlelstv bcm_pwm_dma_address(struct bcm_pwm_channel *pwm)
336 1.1 mlelstv {
337 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
338 1.1 mlelstv
339 1.1 mlelstv return BCM2835_PERIPHERALS_TO_BUS(sc->sc_physaddr + PWM_FIFO);
340 1.1 mlelstv }
341 1.1 mlelstv
342