1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/* $OpenBSD: blinkenlights.c,v 1.2 2007/04/21 15:48:22 gwk Exp $ */
/*
* Copyright (c) 2007 Gordon Willem Klok <gwk@openbsd,org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/kthread.h>
#include <sys/timeout.h>
#include <dev/ofw/openfirm.h>
#include <machine/bus.h>
#include <machine/autoconf.h>
#include <macppc/dev/dbdma.h>
#include <macppc/dev/i2sreg.h>
struct blinkenlights_softc {
struct device sc_dev;
int sc_node;
int sc_intr;
uint32_t sc_freq;
int sc_dmasts;
u_char *sc_reg;
dbdma_regmap_t *sc_dma;
bus_dma_tag_t sc_dmat;
bus_dmamap_t sc_bufmap;
bus_dma_segment_t sc_bufseg[1];
dbdma_t sc_dbdma;
dbdma_command_t *sc_dmacmd;
uint32_t *sc_buf;
uint32_t *sc_bufpos;
struct timeout sc_tmo;
};
int blinkenlights_match(struct device *, void *, void *);
void blinkenlights_attach(struct device *, struct device *, void *);
int blinkenlights_intr(void *);
void blinkenlights_startdma(struct blinkenlights_softc *);
void blinkenlights_deferred(void *);
void blinkenlights_theosDOT(void *);
void blinkenlights_timeout(void *);
void blinkenlights_pwm(struct blinkenlights_softc *, u_char *, int);
extern void keylargo_fcr_enable(int, u_int32_t);
extern void keylargo_fcr_disable(int, u_int32_t);
struct cfattach blinkenlights_ca = {
sizeof(struct blinkenlights_softc), blinkenlights_match,
blinkenlights_attach
};
struct cfdriver blinkenlights_cd = {
NULL, "blinkenlights", DV_DULL
};
#define BL_BUFSZ PAGE_SIZE
#define BL_DBDMA_CMDS 2
int
blinkenlights_match(struct device *parent, void *arg, void *aux)
{
struct confargs *ca = aux;
int soundbus, soundchip, error;
char compat[32];
if (strcmp(ca->ca_name, "i2s") != 0)
return 0;
if ((soundbus = OF_child(ca->ca_node)) == 0)
return 0;
if ((soundchip = OF_child(soundbus)) == 0)
return 0;
error = OF_getprop(soundchip, "virtual", compat, sizeof(compat));
if (error == -1)
error = OF_getprop(soundchip, "lightshow", compat,
sizeof(compat));
if (error == -1)
return 0;
/* we require at least 4 registers */
if (ca->ca_nreg / sizeof(int) < 4)
return 0;
/* we require at least 3 interrupts */
if (ca->ca_nintr / sizeof(int) < 6)
return 0;
return 1;
}
void
blinkenlights_attach(struct device *parent, struct device *self, void *aux)
{
struct blinkenlights_softc *sc = (struct blinkenlights_softc *)self;
struct confargs *ca = aux;
int nseg, error, intr[6];
u_int32_t reg[4];
sc->sc_node = OF_child(ca->ca_node);
OF_getprop(sc->sc_node, "reg", reg, sizeof(reg));
ca->ca_reg[0] += ca->ca_baseaddr;
ca->ca_reg[2] += ca->ca_baseaddr;
if ((sc->sc_reg = mapiodev(ca->ca_reg[0], ca->ca_reg[1])) == NULL) {
printf(": cannot map registers\n");
return;
}
sc->sc_dmat = ca->ca_dmat;
if ((sc->sc_dma = mapiodev(ca->ca_reg[2], ca->ca_reg[3])) == NULL) {
printf(": cannot map DMA registers\n");
goto nodma;
}
if ((sc->sc_dbdma = dbdma_alloc(sc->sc_dmat, BL_DBDMA_CMDS)) == NULL) {
printf(": cannot alloc DMA descriptors\n");
goto nodbdma;
}
sc->sc_dmacmd = sc->sc_dbdma->d_addr;
if ((error = bus_dmamem_alloc(sc->sc_dmat, BL_BUFSZ, 0, 0,
sc->sc_bufseg, 1, &nseg, BUS_DMA_NOWAIT))) {
printf(": cannot allocate DMA mem (%d)\n", error);
goto nodmamem;
}
if ((error = bus_dmamem_map(sc->sc_dmat, sc->sc_bufseg, nseg,
BL_BUFSZ, (caddr_t *)&sc->sc_buf, BUS_DMA_NOWAIT))) {
printf(": cannot map DMA mem (%d)\n", error);
goto nodmamap;
}
sc->sc_bufpos = sc->sc_buf;
if ((error = bus_dmamap_create(sc->sc_dmat, BL_BUFSZ, 1, BL_BUFSZ, 0,
BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->sc_bufmap))) {
printf(": cannot create DMA map (%d)\n", error);
goto nodmacreate;
}
if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_bufmap, sc->sc_buf,
BL_BUFSZ, NULL, BUS_DMA_NOWAIT))) {
printf(": cannot load DMA map (%d)\n", error);
goto nodmaload;
}
/* XXX: Should probably extract this from the clock data
* property of the soundchip node */
sc->sc_freq = 44100;
OF_getprop(sc->sc_node, "interrupts", intr, sizeof(intr));
sc->sc_intr = intr[2];
printf(": irq %d\n", sc->sc_intr);
mac_intr_establish(parent, sc->sc_intr, intr[3] ? IST_LEVEL :
IST_EDGE, IPL_AUDIO, blinkenlights_intr, sc, "blinkenlights");
out32rb(sc->sc_reg + I2S_INT, I2S_INT_CLKSTOPPEND);
keylargo_fcr_disable(I2SClockOffset, I2S0CLKEN);
for (error = 0; error < 1000; error++) {
if (in32rb(sc->sc_reg + I2S_INT) & I2S_INT_CLKSTOPPEND) {
error = 0;
break;
}
delay(1);
}
if (error) {
printf("%s: i2s timeout\n", sc->sc_dev.dv_xname);
goto nodmaload;
}
out32rb(sc->sc_reg + I2S_FORMAT, CLKSRC_VS);
keylargo_fcr_enable(I2SClockOffset, I2S0CLKEN);
kthread_create_deferred(blinkenlights_deferred, sc);
timeout_set(&sc->sc_tmo, blinkenlights_timeout, sc);
return;
nodmaload:
bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufmap);
nodmacreate:
bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_buf, BL_BUFSZ);
nodmamap:
bus_dmamem_free(sc->sc_dmat, sc->sc_bufseg, nseg);
nodmamem:
dbdma_free(sc->sc_dbdma);
nodbdma:
unmapiodev((void *)sc->sc_dma, ca->ca_reg[3]);
nodma:
unmapiodev(sc->sc_reg, ca->ca_reg[1]);
}
void
blinkenlights_deferred(void *v)
{
kthread_create(blinkenlights_theosDOT, v, NULL, "blinkenlights");
}
void
blinkenlights_theosDOT(void *v)
{
struct blinkenlights_softc *sc = (struct blinkenlights_softc *)v;
u_char leds[16];
int offset, i, s, speed;
while (1) {
speed = (averunnable.ldavg[0] / 100) / 2;
for (offset = 8; offset < 16; offset++) {
s = offset - 2;
if (s < 8) {
for (i = 1; i <= (8 - s); i++)
leds[i - 1] = 0x7f / i;
s += 8 - s;
}
for (i = offset; i >= s; i--)
leds[i] = 0xff / (offset + 1 - s);
blinkenlights_pwm(sc, leds, speed);
bzero(leds, sizeof(leds));
}
for (offset = 7; offset >= 0; offset--) {
s = offset + 2;
if (s > 7) {
for (i = 1; i <= (s - 7); i++)
leds[15 - (i - 1)] = (0xff / (s - 7)) / i;
s-= s - 7;
}
for (i = offset; i <= s; i++)
leds[i] = 0xff / (offset + 1) - s;
blinkenlights_pwm(sc, leds, speed);
bzero(leds, sizeof(leds));
}
}
}
void
blinkenlights_pwm(struct blinkenlights_softc *sc, u_char *leds, int msecs)
{
uint32_t *p;
int s, l, k, nsamp;
uint32_t freq[16] = {0};
p = sc->sc_bufpos;
nsamp = sc->sc_freq / 1000 * msecs;
for (k = 1; k < nsamp;) {
if (p - sc->sc_buf < BL_BUFSZ / sizeof(uint32_t)) {
s = 0;
for (l = 0; l < 16; l++) {
s >>= 1;
if (leds[l] &&
((k - freq[l]) == (0xff / leds[l]))) {
s |= 1 << 15;
freq[l] = k;
}
}
*p = (s << 17) | (s >> 15);
p++;
k++;
sc->sc_bufpos++;
} else {
blinkenlights_startdma(sc);
while (sc->sc_dmasts)
tsleep(sc->sc_buf, PWAIT, "blinken", 0);
sc->sc_bufpos = p = sc->sc_buf;
}
}
}
void
blinkenlights_startdma(struct blinkenlights_softc *sc)
{
dbdma_command_t *cmdp = sc->sc_dmacmd;
sc->sc_dmasts = 1;
timeout_add(&sc->sc_tmo, 250);
DBDMA_BUILD(cmdp, DBDMA_CMD_OUT_LAST, 0,
sc->sc_bufmap->dm_segs[0].ds_len,
sc->sc_bufmap->dm_segs[0].ds_addr, DBDMA_INT_ALWAYS,
DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
cmdp++;
DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0, DBDMA_INT_NEVER,
DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
dbdma_start(sc->sc_dma, sc->sc_dbdma);
}
void
blinkenlights_timeout(void *v)
{
struct blinkenlights_softc *sc = (struct blinkenlights_softc *)v;
dbdma_reset(sc->sc_dma);
timeout_del(&sc->sc_tmo);
sc->sc_dmasts = 0;
wakeup(sc->sc_buf);
}
int
blinkenlights_intr(void *v)
{
struct blinkenlights_softc *sc = (struct blinkenlights_softc *)v;
int status;
dbdma_command_t *cmd;
cmd = sc->sc_dmacmd;
status = dbdma_ld16(&cmd->d_status);
if (sc->sc_dmasts) {
sc->sc_dmasts = 0;
timeout_del(&sc->sc_tmo);
wakeup(sc->sc_buf);
return (1);
}
return (0);
}
|