-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharmdsp.c
414 lines (344 loc) · 9.14 KB
/
armdsp.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
armdsp - a simple framework for developing dsp programs for the TI OMAP
Copyright (C) 2010 Pace Willisson <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/module.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/cacheflush.h>
#include <asm/atomic.h>
#include <mach/common.h>
#include "armdsp.h"
#include "regs-omap-l138.h"
/* the arm kernel thinks it's a uniprocessor, so normal wmb() is a nop */
#define armdsp_wmb() do { dsb(); } while (0)
#define armdsp_rmb() do { dmb(); } while (0)
#define ARMDSP_NDEV 2
static dev_t armdsp_dev;
static struct cdev armdsp_cdev;
struct armdsp {
int irq;
int have_irq;
wait_queue_head_t wait;
unsigned long pending;
uint32_t chipint_mask;
};
static struct armdsp armdsp[ARMDSP_NDEV];
static void *armdsp_comm;
static struct armdsp_trgbuf *trgbuf;
#define SYSCFG0_ADDR 0x01c14000
#define HOST1CFG_OFFSET 0x44
#define CHIPSIG 0x174
#define CHIPSIG_CLR 0x178
#define SICR 0x24
#define PSC0_ADDR 0x01c10000
/* from arch/arm/mach/psc.h in ti's linux, but missing from hawkboard */
/* PSC register offsets */
#define EPCPR 0x070
#define PTCMD 0x120
#define PTSTAT 0x128
#define PDSTAT 0x200
#define PDCTL1 0x304
#define MDSTAT 0x800
#define MDCTL 0xA00
#define MDSTAT_STATE_MASK 0x1f
#define MDSTAT15 (MDSTAT + 4 * 15)
#define MDCTL15 (MDCTL + 4 * 15)
#define MDCTL_STATE_MASK 0x07
#define MDCTL_ENABLE 0x03
#define MDCTL_LRESET (1<<8)
/*
* for these, addr must be in the range IO_PHYS..(IO_PHYS+IO_SIZE)
* which is 0x01c00000 and 0x02000000
* or modules "EDMA3 CC" to "McBSP1 FIFO Data"
*
* outside this range, must use ioremap/iounmap
*/
#define armdsp_readphys(addr) (readl(IO_ADDRESS(addr)))
#define armdsp_writephys(val,addr) (writel(val,IO_ADDRESS(addr)))
static void
armdsp_reset (void)
{
uint32_t val;
/* assert active low reset */
val = armdsp_readphys (PSC0_ADDR + MDCTL15);
armdsp_writephys (val & ~MDCTL_LRESET, PSC0_ADDR + MDCTL15);
}
static ssize_t
armdsp_read (struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
unsigned int minor = iminor (filp->f_path.dentry->d_inode);
struct armdsp *dp = &armdsp[minor];
uint32_t length;
int ret;
switch (minor) {
case 0:
if (trgbuf->owner != ARMDSP_TRGBUF_OWNER_ARM) {
if (filp->f_flags & O_NONBLOCK)
return (-EAGAIN);
ret = wait_event_interruptible
(dp->wait,
trgbuf->owner == ARMDSP_TRGBUF_OWNER_ARM);
if (ret < 0)
return (ret);
}
armdsp_rmb ();
length = trgbuf->length;
if (length > count || length > sizeof trgbuf->buf)
return (-EINVAL);
if (copy_to_user (buf, trgbuf->buf, length))
return (-EFAULT);
return (length);
case 1:
while (test_and_clear_bit (0, &dp->pending) == 0) {
if (filp->f_flags & O_NONBLOCK)
return (-EAGAIN);
ret = wait_event_interruptible
(dp->wait, test_bit (0, &dp->pending));
if (ret < 0)
return (ret);
}
return (0);
}
return (-EINVAL);
}
static ssize_t
armdsp_write (struct file *filp, const char __user *buf, size_t count,
loff_t *f_pos)
{
unsigned int minor = iminor (filp->f_path.dentry->d_inode);
switch (minor) {
case 0:
if (count > sizeof trgbuf->buf)
return (-EINVAL);
if (copy_from_user (trgbuf->buf, buf, count))
return (-EFAULT);
trgbuf->length = count;
/* flushes cache and WB */
clean_dcache_area (trgbuf,
&trgbuf->buf[count] - (uint8_t *)trgbuf);
trgbuf->owner = ARMDSP_TRGBUF_OWNER_DSP;
clean_dcache_area (&trgbuf->owner, sizeof trgbuf->owner);
return (count);
}
return (-EINVAL);
}
static int
armdsp_ioctl (struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
unsigned int minor = iminor (filp->f_path.dentry->d_inode);
int err = 0;
uint32_t val;
struct armdsp_physio physarg;
void __user *argp = (void __user *)arg;
uint32_t __iomem *iop;
switch(cmd) {
case ARMDSP_IOCSTOP:
if (minor != 0) {
err = -ENOTTY;
break;
}
armdsp_reset ();
break;
case ARMDSP_IOCSTART:
if (minor != 0) {
err = -ENOTTY;
break;
}
flush_cache_all ();
val = armdsp_readphys (PSC0_ADDR + MDSTAT15);
if ((val & MDSTAT_STATE_MASK) != MDCTL_ENABLE) {
/* turn on dsp power */
val = armdsp_readphys (PSC0_ADDR + MDCTL15);
val = (val & ~MDCTL_STATE_MASK) | MDCTL_ENABLE;
armdsp_writephys (val, PSC0_ADDR + MDCTL15);
val = armdsp_readphys (PSC0_ADDR + PTCMD);
armdsp_writephys (val | 2, PSC0_ADDR + PTCMD);
while ((armdsp_readphys (PSC0_ADDR + PTSTAT) & 2) != 0)
;
}
/* write pointer to vector table */
armdsp_writephys (ARMDSP_COMM_PHYS + ARMDSP_COMM_VECS,
SYSCFG0_ADDR + HOST1CFG_OFFSET);
/* un-assert reset */
val = armdsp_readphys (PSC0_ADDR + MDCTL15);
armdsp_writephys (val | MDCTL_LRESET, PSC0_ADDR + MDCTL15);
break;
case ARMDSP_IOCWMB:
case ARMDSP_IOCRMB:
flush_cache_all ();
break;
case ARMDSP_IOCREADP:
if (copy_from_user (&physarg, argp, sizeof physarg)) {
err = -EFAULT;
break;
}
iop = ioremap (physarg.physaddr, sizeof *iop);
if (! iop) {
err = -EFAULT;
break;
}
physarg.val = *iop;
iounmap (iop);
if (copy_to_user (argp, &physarg, sizeof physarg)) {
err = -EFAULT;
break;
}
break;
case ARMDSP_IOCWRITEP:
if (copy_from_user (&physarg, argp, sizeof physarg)) {
err = -EFAULT;
break;
}
iop = ioremap (physarg.physaddr, sizeof *iop);
if (! iop) {
err = -EFAULT;
break;
}
*iop = physarg.val;
iounmap (iop);
break;
default:
err = -ENOTTY;
break;
}
return (err);
}
static unsigned int
armdsp_poll (struct file *filp, poll_table *wait)
{
unsigned int minor = iminor (filp->f_path.dentry->d_inode);
struct armdsp *dp = &armdsp[minor];
unsigned int mask;
poll_wait (filp, &dp->wait, wait);
mask = 0;
switch (minor) {
case 0:
if (trgbuf->owner == ARMDSP_TRGBUF_OWNER_ARM)
mask |= POLLIN | POLLRDNORM;
break;
case 1:
if (test_bit (0, &dp->pending))
mask |= POLLIN | POLLRDNORM;
break;
}
mask |= POLLOUT | POLLWRNORM;
return (mask);
}
static struct file_operations armdsp_fops = {
.owner = THIS_MODULE,
.read = armdsp_read,
.write = armdsp_write,
.ioctl = armdsp_ioctl,
.poll = armdsp_poll,
};
static void armdsp_cleanup (void);
static irqreturn_t
armdsp_irq (int irq, void *dev_id)
{
unsigned int minor = irq - IRQ_DA8XX_CHIPINT0;
struct armdsp *dp;
if (minor > ARMDSP_NDEV)
return (IRQ_NONE);
dp = &armdsp[minor];
armdsp_writephys (dp->chipint_mask, SYSCFG0_ADDR + CHIPSIG_CLR);
writel (dp->irq, davinci_soc_info.intc_base + SICR);
set_bit (0, &dp->pending);
armdsp_wmb ();
wake_up (&dp->wait);
return (IRQ_HANDLED);
}
static int armdsp_need_cdev_del;
static int __init
armdsp_init (void)
{
unsigned int minor;
struct armdsp *dp;
int ret = 0;
armdsp_reset ();
for (minor = 0; minor < ARMDSP_NDEV; minor++) {
dp = &armdsp[minor];
init_waitqueue_head (&dp->wait);
dp->irq = IRQ_DA8XX_CHIPINT0 + minor;
dp->chipint_mask = 1 << minor;
armdsp_writephys (dp->chipint_mask, SYSCFG0_ADDR + CHIPSIG_CLR);
writel (dp->irq, davinci_soc_info.intc_base + SICR);
}
armdsp_comm = ioremap (ARMDSP_COMM_PHYS, ARMDSP_COMM_SIZE);
if (armdsp_comm == NULL) {
ret = -ENOMEM;
goto cleanup;
}
trgbuf = armdsp_comm + ARMDSP_COMM_TRGBUF;
memset (trgbuf, 0, sizeof *trgbuf);
ret = alloc_chrdev_region (&armdsp_dev, 0, ARMDSP_NDEV, "armdsp");
if (ret)
goto cleanup;
cdev_init(&armdsp_cdev, &armdsp_fops);
armdsp_cdev.owner = THIS_MODULE;
armdsp_cdev.ops = &armdsp_fops;
ret = cdev_add (&armdsp_cdev, armdsp_dev, ARMDSP_NDEV);
if (ret)
goto cleanup;
armdsp_need_cdev_del = 1;
for (minor = 0; minor < ARMDSP_NDEV; minor++) {
dp = &armdsp[minor];
ret = request_irq (dp->irq, armdsp_irq,
IRQF_DISABLED, "armdsp", &armdsp_cdev);
if (ret)
goto cleanup;
dp->have_irq = 1;
}
return (0);
cleanup:
armdsp_cleanup ();
return (ret);
}
static void __exit
armdsp_exit(void)
{
armdsp_cleanup ();
}
static void
armdsp_cleanup (void)
{
unsigned int minor;
struct armdsp *dp;
armdsp_reset ();
for (minor = 0; minor < ARMDSP_NDEV; minor++) {
dp = &armdsp[minor];
if (dp->have_irq) {
armdsp_writephys (dp->chipint_mask,
SYSCFG0_ADDR + CHIPSIG_CLR);
writel (dp->irq, davinci_soc_info.intc_base + SICR);
free_irq (dp->irq, &armdsp_cdev);
}
}
if (armdsp_need_cdev_del)
cdev_del (&armdsp_cdev);
if (armdsp_dev)
unregister_chrdev_region (armdsp_dev, ARMDSP_NDEV);
if (armdsp_comm)
iounmap (armdsp_comm);
}
module_init(armdsp_init);
module_exit(armdsp_exit);
MODULE_AUTHOR("Pace Willisson <[email protected]>");
MODULE_LICENSE("GPL");