bcm2835_pwm.c revision 1.2 1 1.2 skrll /* $NetBSD: bcm2835_pwm.c,v 1.2 2016/02/02 13:55:50 skrll 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.2 skrll __KERNEL_RCSID(0, "$NetBSD: bcm2835_pwm.c,v 1.2 2016/02/02 13:55:50 skrll 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.2 skrll bus_addr_t sc_iob;
71 1.1 mlelstv
72 1.1 mlelstv int sc_clockrate;
73 1.1 mlelstv struct bcm_pwm_channel sc_channels[2];
74 1.1 mlelstv kmutex_t sc_lock;
75 1.1 mlelstv };
76 1.1 mlelstv
77 1.1 mlelstv #define PWM_WRITE(sc, reg, val) \
78 1.1 mlelstv bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
79 1.1 mlelstv #define PWM_READ(sc, reg) \
80 1.1 mlelstv bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
81 1.1 mlelstv
82 1.1 mlelstv static int bcmpwm_match(device_t, cfdata_t, void *);
83 1.1 mlelstv static void bcmpwm_attach(device_t, device_t, void *);
84 1.1 mlelstv static int bcmpwm_wait(struct bcm2835pwm_softc *);
85 1.1 mlelstv
86 1.1 mlelstv CFATTACH_DECL_NEW(bcmpwm_amba, sizeof(struct bcm2835pwm_softc),
87 1.1 mlelstv bcmpwm_match, bcmpwm_attach, NULL, NULL);
88 1.1 mlelstv
89 1.1 mlelstv /* ARGSUSED */
90 1.1 mlelstv static int
91 1.1 mlelstv bcmpwm_match(device_t parent, cfdata_t match, void *aux)
92 1.1 mlelstv {
93 1.1 mlelstv struct amba_attach_args *aaa = aux;
94 1.1 mlelstv
95 1.1 mlelstv if (strcmp(aaa->aaa_name, "bcmpwm") != 0)
96 1.1 mlelstv return 0;
97 1.1 mlelstv
98 1.1 mlelstv if (aaa->aaa_addr != BCM2835_PWM_BASE)
99 1.1 mlelstv return 0;
100 1.1 mlelstv
101 1.1 mlelstv return 1;
102 1.1 mlelstv }
103 1.1 mlelstv
104 1.1 mlelstv static void
105 1.1 mlelstv bcmpwm_attach(device_t parent, device_t self, void *aux)
106 1.1 mlelstv {
107 1.1 mlelstv struct bcm2835pwm_softc *sc = device_private(self);
108 1.1 mlelstv struct amba_attach_args *aaa = aux;
109 1.1 mlelstv const prop_dictionary_t cfg = device_properties(self);
110 1.1 mlelstv
111 1.1 mlelstv aprint_naive("\n");
112 1.1 mlelstv aprint_normal(": PWM\n");
113 1.1 mlelstv
114 1.1 mlelstv sc->sc_dev = self;
115 1.1 mlelstv sc->sc_iot = aaa->aaa_iot;
116 1.2 skrll sc->sc_iob = aaa->aaa_addr;
117 1.1 mlelstv
118 1.1 mlelstv if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_PWM_SIZE, 0,
119 1.1 mlelstv &sc->sc_ioh)) {
120 1.1 mlelstv aprint_error_dev(sc->sc_dev, "unable to map device\n");
121 1.1 mlelstv goto fail0;
122 1.1 mlelstv }
123 1.1 mlelstv
124 1.1 mlelstv prop_dictionary_get_uint32(cfg, "pwmclockrate", &sc->sc_clockrate);
125 1.1 mlelstv
126 1.1 mlelstv sc->sc_channels[0].sc = sc;
127 1.1 mlelstv sc->sc_channels[0].ctlmask = PWM_CTL_MSEN1 | PWM_CTL_USEF1 |
128 1.1 mlelstv PWM_CTL_POLA1 | PWM_CTL_SBIT1 |
129 1.1 mlelstv PWM_CTL_RPTL1 | PWM_CTL_MODE1 |
130 1.1 mlelstv PWM_CTL_PWEN1;
131 1.1 mlelstv sc->sc_channels[0].stamask = PWM_STA_STA1;
132 1.1 mlelstv sc->sc_channels[0].gapomask = PWM_STA_GAPO1;
133 1.1 mlelstv sc->sc_channels[0].rng = PWM_RNG1;
134 1.1 mlelstv sc->sc_channels[0].dat = PWM_DAT1;
135 1.1 mlelstv
136 1.1 mlelstv sc->sc_channels[1].sc = sc;
137 1.1 mlelstv sc->sc_channels[1].ctlmask = PWM_CTL_MSEN2 | PWM_CTL_USEF2 |
138 1.1 mlelstv PWM_CTL_POLA2 | PWM_CTL_SBIT2 |
139 1.1 mlelstv PWM_CTL_RPTL2 | PWM_CTL_MODE2 |
140 1.1 mlelstv PWM_CTL_PWEN2;
141 1.1 mlelstv sc->sc_channels[1].stamask = PWM_STA_STA2;
142 1.1 mlelstv sc->sc_channels[1].gapomask = PWM_STA_GAPO2;
143 1.1 mlelstv sc->sc_channels[1].rng = PWM_RNG2;
144 1.1 mlelstv sc->sc_channels[1].dat = PWM_DAT2;
145 1.1 mlelstv
146 1.1 mlelstv /* The PWM hardware can be used by vcaudio if the
147 1.1 mlelstv * analog output is selected
148 1.1 mlelstv */
149 1.1 mlelstv sc->sc_channels[0].inuse = false;
150 1.1 mlelstv sc->sc_channels[1].inuse = false;
151 1.1 mlelstv
152 1.1 mlelstv mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
153 1.1 mlelstv
154 1.1 mlelstv /* Success! */
155 1.1 mlelstv
156 1.1 mlelstv fail0: return;
157 1.1 mlelstv }
158 1.1 mlelstv
159 1.1 mlelstv struct bcm_pwm_channel *
160 1.1 mlelstv bcm_pwm_alloc(int num)
161 1.1 mlelstv {
162 1.1 mlelstv struct bcm2835pwm_softc *sc;
163 1.1 mlelstv device_t dev;
164 1.1 mlelstv struct bcm_pwm_channel *pwm;
165 1.1 mlelstv
166 1.1 mlelstv dev = device_find_by_driver_unit("bcmpwm", 0);
167 1.1 mlelstv if (dev == NULL)
168 1.1 mlelstv return NULL;
169 1.1 mlelstv sc = device_private(dev);
170 1.1 mlelstv
171 1.1 mlelstv if (num < 0 || num >= __arraycount(sc->sc_channels))
172 1.1 mlelstv return NULL;
173 1.1 mlelstv
174 1.1 mlelstv pwm = &sc->sc_channels[num];
175 1.1 mlelstv
176 1.1 mlelstv mutex_enter(&sc->sc_lock);
177 1.1 mlelstv if (pwm->inuse)
178 1.1 mlelstv pwm = NULL;
179 1.1 mlelstv else
180 1.1 mlelstv pwm->inuse = true;
181 1.1 mlelstv mutex_exit(&sc->sc_lock);
182 1.1 mlelstv
183 1.1 mlelstv if (pwm) {
184 1.1 mlelstv pwm->datsave = PWM_READ(pwm->sc, pwm->dat);
185 1.1 mlelstv pwm->ctlsave = PWM_READ(pwm->sc, PWM_CTL);
186 1.1 mlelstv pwm->rngsave = PWM_READ(pwm->sc, pwm->rng);
187 1.1 mlelstv }
188 1.1 mlelstv
189 1.1 mlelstv return pwm;
190 1.1 mlelstv }
191 1.1 mlelstv
192 1.1 mlelstv void
193 1.1 mlelstv bcm_pwm_free(struct bcm_pwm_channel *pwm)
194 1.1 mlelstv {
195 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
196 1.1 mlelstv
197 1.1 mlelstv KASSERT(pwm->inuse);
198 1.1 mlelstv
199 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->rng, pwm->rngsave);
200 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_CTL, pwm->ctlsave & ~PWM_CTL_WRITEZERO);
201 1.1 mlelstv PWM_WRITE(pwm->sc, pwm->dat, pwm->datsave);
202 1.1 mlelstv
203 1.1 mlelstv mutex_enter(&sc->sc_lock);
204 1.1 mlelstv pwm->inuse = false;
205 1.1 mlelstv mutex_exit(&sc->sc_lock);
206 1.1 mlelstv }
207 1.1 mlelstv
208 1.1 mlelstv void
209 1.1 mlelstv bcm_pwm_control(struct bcm_pwm_channel *pwm, uint32_t ctl, uint32_t rng)
210 1.1 mlelstv {
211 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
212 1.1 mlelstv uint32_t w;
213 1.1 mlelstv
214 1.1 mlelstv KASSERT(pwm->inuse);
215 1.1 mlelstv
216 1.1 mlelstv /* set control bits like for channel 0
217 1.1 mlelstv * there are generic bit definitions that the caller can use.
218 1.1 mlelstv */
219 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_CTL);
220 1.1 mlelstv ctl = (w & ~pwm->ctlmask) | __SHIFTIN(ctl, pwm->ctlmask);
221 1.1 mlelstv
222 1.1 mlelstv /* when FIFO usage gets enabled but wasn't clear the FIFO */
223 1.1 mlelstv if ((w & (PWM_CTL_USEF1|PWM_CTL_USEF2)) == 0 &&
224 1.1 mlelstv (ctl & (PWM_CTL_USEF1|PWM_CTL_USEF2)) != 0)
225 1.1 mlelstv ctl |= PWM_CTL_CLRF1;
226 1.1 mlelstv
227 1.1 mlelstv PWM_WRITE(sc, pwm->rng, rng);
228 1.1 mlelstv PWM_WRITE(sc, PWM_CTL, ctl & ~PWM_CTL_WRITEZERO);
229 1.1 mlelstv }
230 1.1 mlelstv
231 1.1 mlelstv uint32_t
232 1.1 mlelstv bcm_pwm_status(struct bcm_pwm_channel *pwm)
233 1.1 mlelstv {
234 1.1 mlelstv uint32_t w;
235 1.1 mlelstv uint32_t common = PWM_STA_BERR | PWM_STA_RERR1 |
236 1.1 mlelstv PWM_STA_WERR1 | PWM_STA_EMPT1 | PWM_STA_FULL1;
237 1.1 mlelstv
238 1.1 mlelstv /* return status bits like for channel 0
239 1.1 mlelstv * there are generic bit definitions that the caller can use.
240 1.1 mlelstv *
241 1.1 mlelstv * The BERR bit is returned for both channels.
242 1.1 mlelstv */
243 1.1 mlelstv w = PWM_READ(pwm->sc, PWM_STA);
244 1.1 mlelstv PWM_WRITE(pwm->sc, PWM_STA, w &
245 1.1 mlelstv (pwm->stamask | pwm->gapomask | common));
246 1.1 mlelstv
247 1.1 mlelstv w = __SHIFTIN(__SHIFTOUT(w, pwm->stamask), PWM_STA_STA)
248 1.1 mlelstv | __SHIFTIN(__SHIFTOUT(w, pwm->gapomask), PWM_STA_GAPO)
249 1.1 mlelstv | (w & common);
250 1.1 mlelstv
251 1.1 mlelstv return w;
252 1.1 mlelstv }
253 1.1 mlelstv
254 1.1 mlelstv static int
255 1.1 mlelstv bcmpwm_wait(struct bcm2835pwm_softc *sc)
256 1.1 mlelstv {
257 1.1 mlelstv int i;
258 1.1 mlelstv uint32_t s;
259 1.1 mlelstv
260 1.1 mlelstv for (i=0; i<1000; ++i) {
261 1.1 mlelstv s = PWM_READ(sc, PWM_STA);
262 1.1 mlelstv if ((s & PWM_STA_FULL1) == 0)
263 1.1 mlelstv break;
264 1.1 mlelstv delay(1);
265 1.1 mlelstv }
266 1.1 mlelstv if (i >= 1000)
267 1.1 mlelstv return -1;
268 1.1 mlelstv
269 1.1 mlelstv return 0;
270 1.1 mlelstv }
271 1.1 mlelstv
272 1.1 mlelstv int
273 1.1 mlelstv bcm_pwm_write(struct bcm_pwm_channel *pwm, uint32_t *data1, uint32_t *data2,
274 1.1 mlelstv int len)
275 1.1 mlelstv {
276 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
277 1.1 mlelstv int n;
278 1.1 mlelstv uint32_t r;
279 1.1 mlelstv bool even = false;
280 1.1 mlelstv
281 1.1 mlelstv KASSERT(pwm->inuse);
282 1.1 mlelstv
283 1.1 mlelstv n = len;
284 1.1 mlelstv while (n > 0) {
285 1.1 mlelstv if (bcmpwm_wait(sc))
286 1.1 mlelstv break;
287 1.1 mlelstv r = even ? *data2++ : *data1++;
288 1.1 mlelstv PWM_WRITE(sc, PWM_FIFO, r);
289 1.1 mlelstv if (data2 != NULL)
290 1.1 mlelstv even = !even;
291 1.1 mlelstv --n;
292 1.1 mlelstv }
293 1.1 mlelstv
294 1.1 mlelstv return len - n;
295 1.1 mlelstv }
296 1.1 mlelstv
297 1.1 mlelstv void
298 1.1 mlelstv bcm_pwm_set(struct bcm_pwm_channel *pwm, uint32_t w)
299 1.1 mlelstv {
300 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
301 1.1 mlelstv
302 1.1 mlelstv PWM_WRITE(sc, pwm->dat, w);
303 1.1 mlelstv }
304 1.1 mlelstv
305 1.1 mlelstv int
306 1.1 mlelstv bcm_pwm_flush(struct bcm_pwm_channel *pwm)
307 1.1 mlelstv {
308 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
309 1.1 mlelstv
310 1.1 mlelstv return bcmpwm_wait(sc) ? EIO : 0;
311 1.1 mlelstv }
312 1.1 mlelstv
313 1.1 mlelstv void
314 1.1 mlelstv bcm_pwm_dma_enable(struct bcm_pwm_channel *pwm, bool enable)
315 1.1 mlelstv {
316 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
317 1.1 mlelstv uint32_t w;
318 1.1 mlelstv
319 1.1 mlelstv #if 0
320 1.1 mlelstv w = PWM_READ(sc, PWM_DMAC);
321 1.1 mlelstv if (enable)
322 1.1 mlelstv w |= PWM_DMAC_ENAB;
323 1.1 mlelstv else
324 1.1 mlelstv w &= ~PWM_DMAC_ENAB;
325 1.1 mlelstv #else
326 1.1 mlelstv w = (enable ? PWM_DMAC_ENAB : 0)
327 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_PANIC)
328 1.1 mlelstv | __SHIFTIN(7, PWM_DMAC_DREQ);
329 1.1 mlelstv #endif
330 1.1 mlelstv PWM_WRITE(sc, PWM_DMAC, w & ~PWM_DMAC_WRITEZERO);
331 1.1 mlelstv }
332 1.1 mlelstv
333 1.1 mlelstv uint32_t
334 1.1 mlelstv bcm_pwm_dma_address(struct bcm_pwm_channel *pwm)
335 1.1 mlelstv {
336 1.1 mlelstv struct bcm2835pwm_softc *sc = pwm->sc;
337 1.1 mlelstv
338 1.2 skrll return sc->sc_iob + PWM_FIFO;
339 1.1 mlelstv }
340 1.1 mlelstv
341