-
Notifications
You must be signed in to change notification settings - Fork 1
/
hx170dec.c
616 lines (500 loc) · 16.6 KB
/
hx170dec.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
* Decoder device driver (kernel module)
*
* Copyright (C) 2009 Hantro Products Oy.
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
--------------------------------------------------------------------------------
--
-- Version control information, please leave untouched.
--
-- $RCSfile: hx170dec.c,v $
-- $Date: 2009/12/22 12:10:38 $
-- $Revision: 1.1 $
--
------------------------------------------------------------------------------*/
#include <linux/kernel.h>
#include <linux/module.h>
/* needed for __init,__exit directives */
#include <linux/init.h>
/* needed for remap_pfn_range
SetPageReserved
ClearPageReserved
*/
#include <linux/mm.h>
/* obviously, for kmalloc */
#include <linux/slab.h>
/* for struct file_operations, register_chrdev() */
#include <linux/fs.h>
/* standard error codes */
#include <linux/errno.h>
#include <linux/moduleparam.h>
/* request_irq(), free_irq() */
#include <linux/interrupt.h>
/* needed for virt_to_phys() */
#include <asm/io.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <linux/clk.h>
#include <uapi/asm-generic/signal.h>
#include <asm-generic/siginfo.h>
#include <uapi/asm-generic/siginfo.h>
#include <asm/irq.h>
#include <linux/irq.h>
#include <linux/version.h>
/* our own stuff */
#include "hx170dec.h"
/* module description */
MODULE_LICENSE("Proprietary");
MODULE_AUTHOR("Hantro Products Oy");
MODULE_DESCRIPTION("driver module for 8170/81990 Hantro decoder/pp");
/* Decoder interrupt register */
#define X170_INTERRUPT_REGISTER_DEC (1*4)
#define X170_INTERRUPT_REGISTER_PP (60*4)
/* Logic module base address */
#define HXDEC_LOGIC_MODULE0_BASE AT91SAM9G45_VDEC_BASE
#define VP_PB_INT_LT (NR_IRQS_LEGACY + AT91SAM9G45_ID_VDEC)
#define INT_EXPINT1 10
#define INT_EXPINT2 11
#define INT_EXPINT3 12
/* these could be module params in the future */
#define DEC_IO_BASE HXDEC_LOGIC_MODULE0_BASE
#define DEC_IO_SIZE ((100+1) * 4) /* bytes */
#define DEC_IRQ VP_PB_INT_LT
#define HX_DEC_INTERRUPT_BIT 0x100
#define HX_PP_INTERRUPT_BIT 0x100
static const int DecHwId[] = { 0x8190, 0x8170, 0x9170, 0x9190 };
static u32 hx_pp_instance = 0;
static u32 hx_dec_instance = 0;
unsigned long base_port = HXDEC_LOGIC_MODULE0_BASE;
int irq = DEC_IRQ;
/* module_param(name, type, perm) */
module_param(base_port, ulong, 0);
module_param(irq, int, 0);
/* and this is our MAJOR; use 0 for dynamic allocation (recommended)*/
static int hx170dec_major = 0;
/* here's all the must remember stuff */
typedef struct
{
char *buffer;
unsigned long iobaseaddr;
unsigned int iosize;
volatile u8 *hwregs;
int irq;
struct fasync_struct *async_queue_dec;
struct fasync_struct *async_queue_pp;
} hx170dec_t;
static hx170dec_t hx170dec_data; /* dynamic allocation? */
#ifdef HW_PERFORMANCE
static struct timeval end_time;
#endif
static int ReserveIO(void);
static void ReleaseIO(void);
static void ResetAsic(hx170dec_t * dev);
#ifdef HX170DEC_DEBUG
static void dump_regs(unsigned long data);
#endif
/* IRQ handler */
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
static irqreturn_t hx170dec_isr(int irq, void *dev_id, struct pt_regs *regs);
#else
static irqreturn_t hx170dec_isr(int irq, void *dev_id);
#endif
/*------------------------------------------------------------------------------
Function name : hx170dec_ioctl
Description : communication method to/from the user space
Return type : int
------------------------------------------------------------------------------*/
static long hx170dec_ioctl(struct file *filp,
unsigned int cmd, unsigned long arg)
{
int err = 0;
#ifdef HW_PERFORMANCE
struct timeval *end_time_arg;
#endif
PDEBUG("ioctl cmd 0x%08ux\n", cmd);
/*
* extract the type and number bitfields, and don't decode
* wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok()
*/
if(_IOC_TYPE(cmd) != HX170DEC_IOC_MAGIC)
return -ENOTTY;
if(_IOC_NR(cmd) > HX170DEC_IOC_MAXNR)
return -ENOTTY;
/*
* the direction is a bitmask, and VERIFY_WRITE catches R/W
* transfers. `Type' is user-oriented, while
* access_ok is kernel-oriented, so the concept of "read" and
* "write" is reversed
*/
if(_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void *) arg, _IOC_SIZE(cmd));
else if(_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void *) arg, _IOC_SIZE(cmd));
if(err)
return -EFAULT;
switch (cmd)
{
case HX170DEC_IOC_CLI:
disable_irq(hx170dec_data.irq);
break;
case HX170DEC_IOC_STI:
enable_irq(hx170dec_data.irq);
break;
case HX170DEC_IOCGHWOFFSET:
__put_user(hx170dec_data.iobaseaddr, (unsigned long *) arg);
break;
case HX170DEC_IOCGHWIOSIZE:
__put_user(hx170dec_data.iosize, (unsigned int *) arg);
break;
case HX170DEC_PP_INSTANCE:
filp->private_data = &hx_pp_instance;
break;
#ifdef HW_PERFORMANCE
case HX170DEC_HW_PERFORMANCE:
end_time_arg = (struct timeval *) arg;
end_time_arg->tv_sec = end_time.tv_sec;
end_time_arg->tv_usec = end_time.tv_usec;
break;
#endif
}
return 0;
}
/*------------------------------------------------------------------------------
Function name : hx170dec_open
Description : open method
Return type : int
------------------------------------------------------------------------------*/
static int hx170dec_open(struct inode *inode, struct file *filp)
{
filp->private_data = &hx_dec_instance;
PDEBUG("dev opened\n");
return 0;
}
/*------------------------------------------------------------------------------
Function name : hx170dec_fasync
Description : Method for signing up for a interrupt
Return type : int
------------------------------------------------------------------------------*/
static int hx170dec_fasync(int fd, struct file *filp, int mode)
{
hx170dec_t *dev = &hx170dec_data;
struct fasync_struct **async_queue;
/* select which interrupt this instance will sign up for */
if(((u32 *) filp->private_data) == &hx_dec_instance)
{
/* decoder */
PDEBUG("decoder fasync called %d %x %d %x\n",
fd, (u32) filp, mode, (u32) & dev->async_queue_dec);
async_queue = &dev->async_queue_dec;
}
else
{
/* pp */
PDEBUG("pp fasync called %d %x %d %x\n",
fd, (u32) filp, mode, (u32) & dev->async_queue_pp);
async_queue = &dev->async_queue_pp;
}
return fasync_helper(fd, filp, mode, async_queue);
}
/*------------------------------------------------------------------------------
Function name : hx170dec_release
Description : Release driver
Return type : int
------------------------------------------------------------------------------*/
static int hx170dec_release(struct inode *inode, struct file *filp)
{
/* hx170dec_t *dev = &hx170dec_data; */
if(filp->f_flags & FASYNC)
{
/* remove this filp from the asynchronusly notified filp's */
hx170dec_fasync(-1, filp, 0);
}
PDEBUG("dev closed\n");
return 0;
}
/* VFS methods */
static struct file_operations hx170dec_fops = {
open:hx170dec_open,
release:hx170dec_release,
unlocked_ioctl:hx170dec_ioctl,
fasync:hx170dec_fasync,
};
/*------------------------------------------------------------------------------
Function name : hx170dec_init
Description : Initialize the driver
Return type : int
------------------------------------------------------------------------------*/
int __init hx170dec_init(void)
{
int result;
PDEBUG("module init\n");
printk(KERN_INFO "hx170dec: dec/pp kernel module. %s \n", "$Revision: 1.9 $");
printk(KERN_INFO "hx170dec: supports 8170 and 8190 hardware \n");
printk(KERN_INFO "hx170dec: base_port=0x%08lx irq=%i\n", base_port, irq);
hx170dec_data.iobaseaddr = base_port;
hx170dec_data.iosize = DEC_IO_SIZE;
hx170dec_data.irq = irq;
hx170dec_data.async_queue_dec = NULL;
hx170dec_data.async_queue_pp = NULL;
result = register_chrdev(hx170dec_major, "hx170dec", &hx170dec_fops);
if(result < 0)
{
printk(KERN_INFO "hx170dec: unable to get major %d\n", hx170dec_major);
goto err;
}
else if(result != 0) /* this is for dynamic major */
{
hx170dec_major = result;
}
result = ReserveIO();
if(result < 0)
{
goto err;
}
ResetAsic(&hx170dec_data); /* reset hardware */
/* get the IRQ line */
if(irq > 0)
{
result = request_irq(irq, hx170dec_isr,
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
SA_INTERRUPT | SA_SHIRQ,
#else
IRQF_DISABLED | IRQF_SHARED | IRQF_TRIGGER_HIGH,
#endif
"hx170dec", (void *) &hx170dec_data);
if(result != 0)
{
if(result == -EINVAL)
{
printk(KERN_ERR "hx170dec: Bad irq number or handler\n");
}
else if(result == -EBUSY)
{
printk(KERN_ERR "hx170dec: IRQ <%d> busy, change your config\n",
hx170dec_data.irq);
}
ReleaseIO();
goto err;
}
}
else
{
printk(KERN_INFO "hx170dec: IRQ not in use!\n");
}
printk(KERN_INFO "hx170dec: module inserted. Major = %d\n", hx170dec_major);
return 0;
err:
printk(KERN_INFO "hx170dec: module not inserted\n");
unregister_chrdev(hx170dec_major, "hx170dec");
return result;
}
/*------------------------------------------------------------------------------
Function name : hx170dec_cleanup
Description : clean up
Return type : int
------------------------------------------------------------------------------*/
void __exit hx170dec_cleanup(void)
{
hx170dec_t *dev = (hx170dec_t *) & hx170dec_data;
/* clear dec IRQ */
writel(0, dev->hwregs + X170_INTERRUPT_REGISTER_DEC);
/* clear pp IRQ */
writel(0, dev->hwregs + X170_INTERRUPT_REGISTER_PP);
#ifdef HX170DEC_DEBUG
dump_regs((unsigned long) dev); /* dump the regs */
#endif
/* free the IRQ */
if(dev->irq != -1)
{
free_irq(dev->irq, (void *) dev);
}
ReleaseIO();
unregister_chrdev(hx170dec_major, "hx170dec");
printk(KERN_INFO "hx170dec: module removed\n");
return;
}
module_init(hx170dec_init);
module_exit(hx170dec_cleanup);
static int CheckHwId(hx170dec_t * dev)
{
long int hwid;
size_t numHw = sizeof(DecHwId) / sizeof(*DecHwId);
hwid = readl(dev->hwregs);
printk(KERN_INFO "hx170dec: HW ID=0x%08lx\n", hwid);
hwid = (hwid >> 16) & 0xFFFF; /* product version only */
while(numHw--)
{
if(hwid == DecHwId[numHw])
{
printk(KERN_INFO "hx170dec: Compatible HW found at 0x%08lx\n",
dev->iobaseaddr);
return 1;
}
}
printk(KERN_INFO "hx170dec: No Compatible HW found at 0x%08lx\n",
dev->iobaseaddr);
return 0;
}
/*------------------------------------------------------------------------------
Function name : ReserveIO
Description : IO reserve
Return type : int
------------------------------------------------------------------------------*/
static int ReserveIO(void)
{
clk_enable(clk_get(NULL, "vdec_clk"));
if(!request_mem_region
(hx170dec_data.iobaseaddr, hx170dec_data.iosize, "hx170dec"))
{
printk(KERN_INFO "hx170dec: failed to reserve HW regs\n");
return -EBUSY;
}
hx170dec_data.hwregs =
(volatile u8 *) ioremap_nocache(hx170dec_data.iobaseaddr,
hx170dec_data.iosize);
if(hx170dec_data.hwregs == NULL)
{
printk(KERN_INFO "hx170dec: failed to ioremap HW regs\n");
ReleaseIO();
return -EBUSY;
}
/* check for correct HW */
if(!CheckHwId(&hx170dec_data))
{
ReleaseIO();
return -EBUSY;
}
return 0;
}
/*------------------------------------------------------------------------------
Function name : releaseIO
Description : release
Return type : void
------------------------------------------------------------------------------*/
static void ReleaseIO(void)
{
if(hx170dec_data.hwregs)
iounmap((void *) hx170dec_data.hwregs);
release_mem_region(hx170dec_data.iobaseaddr, hx170dec_data.iosize);
clk_disable(clk_get(NULL, "vdec_clk"));
}
/*------------------------------------------------------------------------------
Function name : hx170dec_isr
Description : interrupt handler
Return type : irqreturn_t
------------------------------------------------------------------------------*/
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
irqreturn_t hx170dec_isr(int irq, void *dev_id, struct pt_regs *regs)
#else
irqreturn_t hx170dec_isr(int irq, void *dev_id)
#endif
{
unsigned int handled = 0;
hx170dec_t *dev = (hx170dec_t *) dev_id;
u32 irq_status_dec;
u32 irq_status_pp;
handled = 0;
/* interrupt status register read */
irq_status_dec = readl(dev->hwregs + X170_INTERRUPT_REGISTER_DEC);
irq_status_pp = readl(dev->hwregs + X170_INTERRUPT_REGISTER_PP);
if((irq_status_dec & HX_DEC_INTERRUPT_BIT) ||
(irq_status_pp & HX_PP_INTERRUPT_BIT))
{
if(irq_status_dec & HX_DEC_INTERRUPT_BIT)
{
#ifdef HW_PERFORMANCE
do_gettimeofday(&end_time);
#endif
/* clear dec IRQ */
writel(irq_status_dec & (~HX_DEC_INTERRUPT_BIT),
dev->hwregs + X170_INTERRUPT_REGISTER_DEC);
/* fasync kill for decoder instances */
if(dev->async_queue_dec != NULL)
{
kill_fasync(&dev->async_queue_dec, SIGIO, POLL_IN);
}
#if 0
else
{
printk(KERN_WARNING
"hx170dec: DEC IRQ received w/o anybody waiting for it!\n");
}
#endif
PDEBUG("decoder IRQ received!\n");
}
if(irq_status_pp & HX_PP_INTERRUPT_BIT)
{
#ifdef HW_PERFORMANCE
do_gettimeofday(&end_time);
#endif
/* clear pp IRQ */
writel(irq_status_pp & (~HX_PP_INTERRUPT_BIT),
dev->hwregs + X170_INTERRUPT_REGISTER_PP);
/* kill fasync for PP instances */
if(dev->async_queue_pp != NULL)
{
kill_fasync(&dev->async_queue_pp, SIGIO, POLL_IN);
}
#if 0
else
{
printk(KERN_WARNING
"hx170dec: PP IRQ received w/o anybody waiting for it!\n");
}
#endif
PDEBUG("pp IRQ received!\n");
}
handled = 1;
}
else
{
PDEBUG("IRQ received, but not x170's!\n");
}
return IRQ_RETVAL(handled);
}
/*------------------------------------------------------------------------------
Function name : ResetAsic
Description : reset asic
Return type :
------------------------------------------------------------------------------*/
void ResetAsic(hx170dec_t * dev)
{
int i;
writel(0, dev->hwregs + 0x04);
for(i = 4; i < dev->iosize; i += 4)
{
writel(0, dev->hwregs + i);
}
}
/*------------------------------------------------------------------------------
Function name : dump_regs
Description : Dump registers
Return type :
------------------------------------------------------------------------------*/
#ifdef HX170DEC_DEBUG
void dump_regs(unsigned long data)
{
hx170dec_t *dev = (hx170dec_t *) data;
int i;
PDEBUG("Reg Dump Start\n");
for(i = 0; i < dev->iosize; i += 4)
{
PDEBUG("\toffset %02X = %08X\n", i, readl(dev->hwregs + i));
}
PDEBUG("Reg Dump End\n");
}
#endif