forked from torvalds/linux
/
sunxi_cedar.c
1114 lines (973 loc) · 32 KB
/
sunxi_cedar.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* drivers\media\video\sunxi\sunxi_cedar.c
* (C) Copyright 2007-2011
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
* huangxin <huangxin@allwinnertech.com>
*
* some simple description for this code
*
* 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.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/preempt.h>
#include <linux/cdev.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/rmap.h>
#include <linux/wait.h>
#include <linux/semaphore.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <mach/hardware.h>
#include <asm/system.h>
#include <asm/siginfo.h>
#include <asm/signal.h>
#include <mach/system.h>
#include <mach/clock.h>
#include "sunxi_cedar.h"
#define DRV_VERSION "0.01alpha"
#undef USE_CEDAR_ENGINE
#ifndef CEDARDEV_MAJOR
#define CEDARDEV_MAJOR (150)
#endif
#ifndef CEDARDEV_MINOR
#define CEDARDEV_MINOR (0)
#endif
//#define CEDAR_DEBUG
#define CONFIG_SW_SYSMEM_RESERVED_BASE 0x43000000
#define CONFIG_SW_SYSMEM_RESERVED_SIZE 75776
int g_dev_major = CEDARDEV_MAJOR;
int g_dev_minor = CEDARDEV_MINOR;
module_param(g_dev_major, int, S_IRUGO);//S_IRUGO represent that g_dev_major can be read,but canot be write
module_param(g_dev_minor, int, S_IRUGO);
#define VE_IRQ_NO (SW_INT_IRQNO_VE)
struct clk *ve_moduleclk = NULL;
struct clk *ve_pll4clk = NULL;
struct clk *ahb_veclk = NULL;
struct clk *dram_veclk = NULL;
struct clk *avs_moduleclk = NULL;
struct clk *hosc_clk = NULL;
static unsigned long pll4clk_rate = 720000000;
#ifdef CONFIG_CMA
static void *ve_start_virt;
#endif
extern unsigned long ve_start;
extern unsigned long ve_size;
extern int flush_clean_user_range(long start, long end);
struct iomap_para{
volatile char* regs_macc;
volatile char* regs_avs;
};
static DECLARE_WAIT_QUEUE_HEAD(wait_ve);
struct cedar_dev {
struct cdev cdev; /* char device struct */
struct device *dev; /* ptr to class device struct */
struct class *class; /* class for auto create device node */
struct semaphore sem; /* mutual exclusion semaphore */
wait_queue_head_t wq; /* wait queue for poll ops */
struct iomap_para iomap_addrs; /* io remap addrs */
struct timer_list cedar_engine_timer;
struct timer_list cedar_engine_timer_rel;
u32 irq; /* cedar video engine irq number */
u32 irq_flag; /* flag of video engine irq generated */
u32 irq_value; /* value of video engine irq */
u32 irq_has_enable;
u32 ref_count;
};
struct cedar_dev *cedar_devp;
u32 int_sta=0,int_value;
/*
* Video engine interrupt service routine
* To wake up ve wait queue
*/
static irqreturn_t VideoEngineInterupt(int irq, void *dev)
{
unsigned int ve_int_ctrl_reg;
volatile int val;
int modual_sel;
struct iomap_para addrs = cedar_devp->iomap_addrs;
modual_sel = readl(addrs.regs_macc + 0);
modual_sel &= 0xf;
/* estimate Which video format */
switch (modual_sel)
{
case 0: //mpeg124
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0x100 + 0x14);
break;
case 1: //h264
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0x200 + 0x20);
break;
case 2: //vc1
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0x300 + 0x24);
break;
case 3: //rmvb
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0x400 + 0x14);
break;
case 0xa: //isp
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0xa00 + 0x08);
break;
case 0xb: //avc enc
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0xb00 + 0x14);
break;
default:
ve_int_ctrl_reg = (unsigned int)(addrs.regs_macc + 0x100 + 0x14);
printk("macc modual sel not defined!\n");
break;
}
//disable interrupt
if(modual_sel == 0) {
val = readl(ve_int_ctrl_reg);
writel(val & (~0x7c), ve_int_ctrl_reg);
} else {
val = readl(ve_int_ctrl_reg);
writel(val & (~0xf), ve_int_ctrl_reg);
}
cedar_devp->irq_value = 1; //hx modify 2011-8-1 16:08:47
cedar_devp->irq_flag = 1;
//any interrupt will wake up wait queue
wake_up_interruptible(&wait_ve); //ioctl
return IRQ_HANDLED;
}
/*
* poll operateion for wait for ve irq
*/
unsigned int cedardev_poll(struct file *filp, struct poll_table_struct *wait)
{
int mask = 0;
struct cedar_dev *devp = filp->private_data;
poll_wait(filp, &devp->wq, wait);
if (devp->irq_flag == 1) {
devp->irq_flag = 0;
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
static int clk_status = 0;
static LIST_HEAD(run_task_list);
static LIST_HEAD(del_task_list);
static spinlock_t cedar_spin_lock;
#define CEDAR_RUN_LIST_NONULL -1
#define CEDAR_NONBLOCK_TASK 0 //非阻塞
#define CEDAR_BLOCK_TASK 1
#define CLK_REL_TIME 10000 //10秒
#define TIMER_CIRCLE 50 //50毫秒
#define TASK_INIT 0x00
#define TASK_TIMEOUT 0x55
#define TASK_RELEASE 0xaa
#define SIG_CEDAR 35
int enable_cedar_hw_clk(void)
{
unsigned long flags;
int res = -EFAULT;
spin_lock_irqsave(&cedar_spin_lock, flags);
if (clk_status == 1)
goto out;
clk_status = 1;
if(0 != clk_enable(ahb_veclk)){
printk("ahb_veclk failed; \n");
goto out;
}
if(0 != clk_enable(ve_moduleclk)){
printk("ve_moduleclk failed; \n");
goto out3;
}
if(0 != clk_enable(dram_veclk)){
printk("dram_veclk failed; \n");
goto out2;
}
if(0 != clk_enable(avs_moduleclk)){
printk("ve_moduleclk failed; \n");
goto out1;
}
#ifdef CEDAR_DEBUG
printk("%s,%d\n",__func__,__LINE__);
#endif
res = 0;
goto out;
out1:
clk_disable(dram_veclk);
out2:
clk_disable(ve_moduleclk);
out3:
clk_disable(ahb_veclk);
out:
spin_unlock_irqrestore(&cedar_spin_lock, flags);
return res;
}
int disable_cedar_hw_clk(void)
{
unsigned long flags;
spin_lock_irqsave(&cedar_spin_lock, flags);
if (clk_status == 0)
goto out;
clk_status = 0;
clk_disable(dram_veclk);
clk_disable(ve_moduleclk);
clk_disable(ahb_veclk);
clk_disable(avs_moduleclk);
#ifdef CEDAR_DEBUG
printk("%s,%d\n",__func__,__LINE__);
#endif
out:
spin_unlock_irqrestore(&cedar_spin_lock, flags);
return 0;
}
void cedardev_insert_task(struct cedarv_engine_task* new_task)
{
struct cedarv_engine_task *task_entry;
unsigned long flags;
spin_lock_irqsave(&cedar_spin_lock, flags);
if(list_empty(&run_task_list))
new_task->is_first_task = 1;
/*遍历run_task_list链表,如果插入的任务优先级比链表节点中的任务优先级高,并且当前插入任务不是第一个插入的任务。
*那么就将优先级高的任务放于前面,队列中的任务采取从高到底的优先级队列排队。
*/
list_for_each_entry(task_entry, &run_task_list, list) {
if ((task_entry->is_first_task == 0) && (task_entry->running == 0) && (task_entry->t.task_prio < new_task->t.task_prio)) {
break;
}
}
list_add(&new_task->list, task_entry->list.prev);
#ifdef CEDAR_DEBUG
printk("%s,%d, TASK_ID:",__func__,__LINE__);
list_for_each_entry(task_entry, &run_task_list, list) {
printk("%d!", task_entry->t.ID);
}
printk("\n");
#endif
/*每次插入一个任务,就将当前的计时器时间重置为系统当前的jiffies*/
mod_timer(&cedar_devp->cedar_engine_timer, jiffies + 0);
spin_unlock_irqrestore(&cedar_spin_lock, flags);
}
int cedardev_del_task(int task_id)
{
struct cedarv_engine_task *task_entry;
unsigned long flags;
spin_lock_irqsave(&cedar_spin_lock, flags);
/*遍历run_task_list链表
*如果找到对应的id号,那么就将run_task_list链表中的任务移到del_task_list链表的表头。
*/
list_for_each_entry(task_entry, &run_task_list, list) {
if (task_entry->t.ID == task_id && task_entry->status != TASK_RELEASE) {
task_entry->status = TASK_RELEASE;
spin_unlock_irqrestore(&cedar_spin_lock, flags);
mod_timer(&cedar_devp->cedar_engine_timer, jiffies + 0);
return 0;
}
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
//找不到对应 ID
return -1;
}
int cedardev_check_delay(int check_prio)
{
struct cedarv_engine_task *task_entry;
int timeout_total = 0;
unsigned long flags;
/*获取总的等待时间*/
spin_lock_irqsave(&cedar_spin_lock, flags);
list_for_each_entry(task_entry, &run_task_list, list) {
if ((task_entry->t.task_prio >= check_prio) || (task_entry->running == 1) || (task_entry->is_first_task == 1))
timeout_total = timeout_total + task_entry->t.frametime;
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
#ifdef CEDAR_DEBUG
printk("%s,%d,%d\n", __func__, __LINE__, timeout_total);
#endif
return timeout_total;
}
static void cedar_engine_for_timer_rel(unsigned long arg)
{
unsigned long flags;
spin_lock_irqsave(&cedar_spin_lock, flags);
if(list_empty(&run_task_list)){
disable_cedar_hw_clk();
} else {
printk("Warring: cedar engine timeout for clk disable, but task left, something wrong?\n");
mod_timer( &cedar_devp->cedar_engine_timer, jiffies + msecs_to_jiffies(TIMER_CIRCLE));
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
}
static void cedar_engine_for_events(unsigned long arg)
{
struct cedarv_engine_task *task_entry, *task_entry_tmp;
struct siginfo info;
unsigned long flags;
spin_lock_irqsave(&cedar_spin_lock, flags);
list_for_each_entry_safe(task_entry, task_entry_tmp, &run_task_list, list) {
mod_timer(&cedar_devp->cedar_engine_timer_rel, jiffies + msecs_to_jiffies(CLK_REL_TIME));
if (task_entry->status == TASK_RELEASE ||
time_after(jiffies, task_entry->t.timeout)) {
if (task_entry->status == TASK_INIT)
task_entry->status = TASK_TIMEOUT;
list_move(&task_entry->list, &del_task_list);
}
}
list_for_each_entry_safe(task_entry, task_entry_tmp, &del_task_list, list) {
info.si_signo = SIG_CEDAR;
info.si_code = task_entry->t.ID;
if (task_entry->status == TASK_TIMEOUT){//表示任务timeout删除
info.si_errno = TASK_TIMEOUT;
send_sig_info(SIG_CEDAR, &info, task_entry->task_handle);
}else if(task_entry->status == TASK_RELEASE){//表示任务正常运行完毕删除
info.si_errno = TASK_RELEASE;
send_sig_info(SIG_CEDAR, &info, task_entry->task_handle);
}
list_del(&task_entry->list);
kfree(task_entry);
}
/*激活链表中的task*/
if(!list_empty(&run_task_list)){
task_entry = list_entry(run_task_list.next, struct cedarv_engine_task, list);
if(task_entry->running == 0){
task_entry->running = 1;
info.si_signo = SIG_CEDAR;
info.si_code = task_entry->t.ID;
info.si_errno = TASK_INIT; //任务已经启动
send_sig_info(SIG_CEDAR, &info, task_entry->task_handle);
}
mod_timer( &cedar_devp->cedar_engine_timer, jiffies + msecs_to_jiffies(TIMER_CIRCLE));
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
}
static unsigned int g_ctx_reg0;
static void save_context(void)
{
if (SUNXI_VER_A10A == sw_get_ic_ver() ||
SUNXI_VER_A13A == sw_get_ic_ver())
g_ctx_reg0 = readl(0xf1c20e00);
}
static void restore_context(void)
{
if (SUNXI_VER_A10A == sw_get_ic_ver() ||
SUNXI_VER_A13A == sw_get_ic_ver())
writel(g_ctx_reg0, 0xf1c20e00);
}
static long __set_ve_freq (int arg)
{
/*
** Although the Allwinner sun7i driver sources indicate that the VE
** clock can go up to 500MHz, very simple JPEG and MPEG decoding
** tests show it can't run reliably at even 408MHz. Keeping the
** sun4i max setting seems best until more information is available.
*/
int max_rate = 320000000;
int min_rate = 100000000;
int arg_rate = arg * 1000000; /* arg_rate is specified in MHz */
int divisor;
if (arg_rate > max_rate)
arg_rate = max_rate;
if (arg_rate < min_rate)
arg_rate = min_rate;
/*
** compute integer divisor of pll4clk_rate so that:
** ve_moduleclk >= arg_rate
**
** clamp divisor so that:
** min_rate <= ve_moduleclk <= max_rate
** 1 <= divisor <= 8
*/
divisor = pll4clk_rate / arg_rate;
if (divisor == 0)
divisor = 1;
else if (pll4clk_rate / divisor < min_rate && divisor > 1)
divisor--;
else if (pll4clk_rate / divisor > max_rate)
divisor++;
/* VE PLL divisor can't be > 8 */
if (divisor > 8)
divisor = 8;
if (clk_set_rate(ve_moduleclk, pll4clk_rate / divisor) == -1) {
printk("IOCTL_SET_VE_FREQ: error setting clock; pll4clk_rate = %lu, requested = %lu\n", pll4clk_rate, pll4clk_rate / divisor);
return -EFAULT;
}
#ifdef CEDAR_DEBUG
printk("IOCTL_SET_VE_FREQ: pll4clk_rate = %lu, divisor = %d, arg_rate= %d, ve_moduleclk = %lu\n", pll4clk_rate, divisor, arg_rate, clk_get_rate(ve_moduleclk));
#endif
return 0;
}
/*
* ioctl function
* including : wait video engine done,
* AVS Counter control,
* Physical memory control,
* module clock/freq control.
* cedar engine
*/
long cedardev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
long ret = 0;
unsigned int v;
int ve_timeout = 0;
struct cedar_dev *devp;
#ifdef USE_CEDAR_ENGINE
int rel_taskid = 0;
struct __cedarv_task task_ret;
struct cedarv_engine_task *task_ptr = NULL;
#endif
unsigned long flags;
devp = filp->private_data;
switch (cmd)
{
case IOCTL_ENGINE_REQ:
#ifdef USE_CEDAR_ENGINE
if(copy_from_user(&task_ret, (void __user*)arg, sizeof(struct __cedarv_task))){
printk("IOCTL_ENGINE_REQ copy_from_user fail\n");
return -EFAULT;
}
spin_lock_irqsave(&cedar_spin_lock, flags);
/*如果task为非阻塞状态,请求者可以立即返回*/
if(!list_empty(&run_task_list) && ( task_ret.block_mode == CEDAR_NONBLOCK_TASK)){
spin_unlock_irqrestore(&cedar_spin_lock, flags);
return CEDAR_RUN_LIST_NONULL; //run_task_list里面有任务,返回-1
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
/*如果task为阻塞状态,将task插入run_task_list链表中*/
task_ptr = kmalloc(sizeof(struct cedarv_engine_task), GFP_KERNEL);
if(!task_ptr){
printk("get mem for IOCTL_ENGINE_REQ\n");
return PTR_ERR(task_ptr);
}
task_ptr->task_handle = current;
task_ptr->t.ID = task_ret.ID;
task_ptr->t.timeout = jiffies + msecs_to_jiffies(1000*task_ret.timeout);//ms to jiffies
task_ptr->t.frametime = task_ret.frametime;
task_ptr->t.task_prio = task_ret.task_prio;
task_ptr->running = 0;
task_ptr->is_first_task = 0;
task_ptr->status = TASK_INIT;
cedardev_insert_task(task_ptr);
enable_cedar_hw_clk();
return task_ptr->is_first_task;//插入run_task_list链表中的任务是第一个任务,返回1,不是第一个任务返回0. hx modify 2011-7-28 16:59:16!!!
#else
enable_cedar_hw_clk();
cedar_devp->ref_count++;
break;
#endif
case IOCTL_ENGINE_REL:
#ifdef USE_CEDAR_ENGINE
rel_taskid = (int)arg;
/*
* 利用任务的id号进行任务的删除操作。返回值意义:找不到对应ID,返回-1;找到对应ID,返回0。
*/
ret = cedardev_del_task(rel_taskid);
#else
disable_cedar_hw_clk();
cedar_devp->ref_count--;
#endif
return ret;
case IOCTL_ENGINE_CHECK_DELAY:
{
struct cedarv_engine_task_info task_info;
/*从用户空间中获取要查询的任务优先级,通过任务优先级,统计需要等待的总时间total_time.
* 在这个接口中,同时也给用户传递了当前任务的frametime(这样做可以减少接口,但是用户空间要多设置一个空的frametime值)
*对于当前task的frametime,也可以用额外的接口获取,但是这样做,frametime和total_time就处于不同接口中。好处???
*/
if(copy_from_user(&task_info, (void __user*)arg, sizeof(struct cedarv_engine_task_info))){
printk("IOCTL_ENGINE_CHECK_DELAY copy_from_user fail\n");
return -EFAULT;
}
task_info.total_time = cedardev_check_delay(task_info.task_prio);//task_info.task_prio是传递过来的优先级
#ifdef CEDAR_DEBUG
printk("%s,%d,%d\n", __func__, __LINE__, task_info.total_time);
#endif
task_info.frametime = 0;
spin_lock_irqsave(&cedar_spin_lock, flags);
if(!list_empty(&run_task_list)){
/*获取run_task_list链表中的第一个任务,也就是当前运行的任务,通过当前运行的任务获取frametime时间*/
struct cedarv_engine_task *task_entry;
#ifdef CEDAR_DEBUG
printk("%s,%d\n",__func__,__LINE__);
#endif
task_entry = list_entry(run_task_list.next, struct cedarv_engine_task, list);
if(task_entry->running == 1)
task_info.frametime = task_entry->t.frametime;
#ifdef CEDAR_DEBUG
printk("%s,%d,%d\n",__func__,__LINE__,task_info.frametime);
#endif
}
spin_unlock_irqrestore(&cedar_spin_lock, flags);
/*
*将任务优先级,total_time,frametime拷贝到用户空间。任务优先级还是用户设置的值,total_time是需要等待的总时间,
*frametime是当前任务的运行时间.其实当前任务的信息最好用另一个接口实现.减少耦合度和接口的拓展性.
*/
if (copy_to_user((void *)arg, &task_info, sizeof(struct cedarv_engine_task_info))){
printk("IOCTL_ENGINE_CHECK_DELAY copy_to_user fail\n");
return -EFAULT;
}
}
break;
case IOCTL_WAIT_VE:
//wait_event_interruptible(wait_ve, cedar_devp->irq_flag);
ve_timeout = (int)arg;
cedar_devp->irq_value = 0;
spin_lock_irqsave(&cedar_spin_lock, flags);
if(cedar_devp->irq_flag)
cedar_devp->irq_value = 1;
spin_unlock_irqrestore(&cedar_spin_lock, flags);
wait_event_interruptible_timeout(wait_ve, cedar_devp->irq_flag, ve_timeout*HZ);
//printk("%s,%d,ve_timeout:%d,cedar_devp->irq_value:%d\n", __func__, __LINE__, ve_timeout, cedar_devp->irq_value);
cedar_devp->irq_flag = 0;
/*返回1,表示中断返回,返回0,表示timeout返回*/
return cedar_devp->irq_value;
case IOCTL_ENABLE_VE:
clk_enable(ve_moduleclk);
break;
case IOCTL_DISABLE_VE:
clk_disable(ve_moduleclk);
break;
case IOCTL_RESET_VE:
clk_disable(dram_veclk);
clk_reset(ve_moduleclk, 1);
clk_reset(ve_moduleclk, 0);
clk_enable(dram_veclk);
break;
case IOCTL_SET_VE_FREQ:
return __set_ve_freq((int) arg);
case IOCTL_GETVALUE_AVS2:
/* Return AVS1 counter value */
return readl(cedar_devp->iomap_addrs.regs_avs + 0x88);
case IOCTL_ADJUST_AVS2:
{
int arg_s = (int)arg;
int temp;
save_context();
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x8c);
temp = v & 0xffff0000;
temp =temp + temp*arg_s/100;
temp = temp > (244<<16) ? (244<<16) : temp;
temp = temp < (234<<16) ? (234<<16) : temp;
v = (temp & 0xffff0000) | (v&0x0000ffff);
#ifdef CEDAR_DEBUG
printk("Kernel AVS ADJUST Print: 0x%x\n", v);
#endif
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x8c);
restore_context();
break;
}
case IOCTL_ADJUST_AVS2_ABS:
{
int arg_s = (int)arg;
int v_dst;
switch(arg_s){
case -2:
v_dst = 234;
break;
case -1:
v_dst = 236;
break;
case 1:
v_dst = 242;
break;
case 2:
v_dst = 244;
break;
default:
v_dst = 239;
break;
}
save_context();
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x8c);
v = (v_dst<<16) | (v&0x0000ffff);
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x8c);
restore_context();
break;
}
case IOCTL_CONFIG_AVS2:
save_context();
/* Set AVS counter divisor */
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x8c);
v = 239 << 16 | (v & 0xffff);
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x8c);
/* Enable AVS_CNT1 and Pause it */
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x80);
v |= 1 << 9 | 1 << 1;
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x80);
/* Set AVS_CNT1 init value as zero */
writel(0, cedar_devp->iomap_addrs.regs_avs + 0x88);
restore_context();
break;
case IOCTL_RESET_AVS2:
/* Set AVS_CNT1 init value as zero */
save_context();
writel(0, cedar_devp->iomap_addrs.regs_avs + 0x88);
restore_context();
break;
case IOCTL_PAUSE_AVS2:
/* Pause AVS_CNT1 */
save_context();
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x80);
v |= 1 << 9;
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x80);
restore_context();
break;
case IOCTL_START_AVS2:
/* Start AVS_CNT1 : do not pause */
save_context();
v = readl(cedar_devp->iomap_addrs.regs_avs + 0x80);
v &= ~(1 << 9);
writel(v, cedar_devp->iomap_addrs.regs_avs + 0x80);
restore_context();
break;
case IOCTL_GET_ENV_INFO:
{
struct cedarv_env_infomation env_info;
env_info.phymem_start = (unsigned int)phys_to_virt(ve_start);
env_info.phymem_total_size = ve_size;
env_info.address_macc = (unsigned int)cedar_devp->iomap_addrs.regs_macc;
if (copy_to_user((char *)arg, &env_info, sizeof(struct cedarv_env_infomation)))
return -EFAULT;
}
break;
case IOCTL_GET_IC_VER:
{
if (SUNXI_VER_A10A == sw_get_ic_ver() ||
SUNXI_VER_A13A == sw_get_ic_ver()) {
return 0x0A10000A;
} else if (SUNXI_VER_A10B == sw_get_ic_ver() ||
SUNXI_VER_A10C == sw_get_ic_ver() ||
SUNXI_VER_A13B == sw_get_ic_ver() ||
SUNXI_VER_A20 == sw_get_ic_ver()) {
return 0x0A10000B;
}else{
printk("IC_VER get error:%s,%d\n", __func__, __LINE__);
return -EFAULT;
}
}
case IOCTL_FLUSH_CACHE:
{
struct cedarv_cache_range cache_range;
if(copy_from_user(&cache_range, (void __user*)arg, sizeof(struct cedarv_cache_range))){
printk("IOCTL_FLUSH_CACHE copy_from_user fail\n");
return -EFAULT;
}
flush_clean_user_range(cache_range.start, cache_range.end);
}
break;
case IOCTL_SET_REFCOUNT:
cedar_devp->ref_count = (int)arg;
break;
case IOCTL_READ_REG:
{
struct cedarv_regop reg_para;
if(copy_from_user(®_para, (void __user*)arg, sizeof(struct cedarv_regop)))
return -EFAULT;
return readl(reg_para.addr);
}
case IOCTL_WRITE_REG:
{
struct cedarv_regop reg_para;
if(copy_from_user(®_para, (void __user*)arg, sizeof(struct cedarv_regop)))
return -EFAULT;
writel(reg_para.value, reg_para.addr);
break;
}
default:
break;
}
return ret;
}
static int cedardev_open(struct inode *inode, struct file *filp)
{
struct cedar_dev *devp;
devp = container_of(inode->i_cdev, struct cedar_dev, cdev);
filp->private_data = devp;
if (down_interruptible(&devp->sem)) {
return -ERESTARTSYS;
}
/* init other resource here */
devp->irq_flag = 0;
up(&devp->sem);
nonseekable_open(inode, filp);
return 0;
}
static int cedardev_release(struct inode *inode, struct file *filp)
{
struct cedar_dev *devp;
devp = filp->private_data;
if (down_interruptible(&devp->sem)) {
return -ERESTARTSYS;
}
/* release other resource here */
devp->irq_flag = 1;
up(&devp->sem);
return 0;
}
void cedardev_vma_open(struct vm_area_struct *vma)
{
}
void cedardev_vma_close(struct vm_area_struct *vma)
{
}
static struct vm_operations_struct cedardev_remap_vm_ops = {
.open = cedardev_vma_open,
.close = cedardev_vma_close,
};
static int cedardev_mmap(struct file *filp, struct vm_area_struct *vma)
{
unsigned long temp_pfn;
unsigned int VAddr;
struct iomap_para addrs;
unsigned int io_ram = 0;
VAddr = vma->vm_pgoff << 12;
addrs = cedar_devp->iomap_addrs;
if (VAddr == (unsigned int)addrs.regs_macc) {
temp_pfn = MACC_REGS_BASE >> 12;
io_ram = 1;
} else {
temp_pfn = (__pa(vma->vm_pgoff << 12))>>12;
io_ram = 0;
}
if (io_ram == 0) {
/* Set reserved and I/O flag for the area. */
vma->vm_flags |= VM_RESERVED | VM_IO;
/* Select uncached access. */
//vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
if (remap_pfn_range(vma, vma->vm_start, temp_pfn,
vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
return -EAGAIN;
}
} else {
/* Set reserved and I/O flag for the area. */
vma->vm_flags |= VM_RESERVED | VM_IO;
/* Select uncached access. */
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
if (io_remap_pfn_range(vma, vma->vm_start, temp_pfn,
vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
return -EAGAIN;
}
}
vma->vm_ops = &cedardev_remap_vm_ops;
cedardev_vma_open(vma);
return 0;
}
static int snd_sw_cedar_suspend(struct platform_device *pdev,pm_message_t state)
{
disable_cedar_hw_clk();
return 0;
}
static int snd_sw_cedar_resume(struct platform_device *pdev)
{
if(cedar_devp->ref_count == 0){
return 0;
}
enable_cedar_hw_clk();
return 0;
}
static struct file_operations cedardev_fops = {
.owner = THIS_MODULE,
.mmap = cedardev_mmap,
.poll = cedardev_poll,
.open = cedardev_open,
.release = cedardev_release,
.llseek = no_llseek,
.unlocked_ioctl = cedardev_ioctl,
};
/*data relating*/
static struct platform_device sw_device_cedar = {
.name = "sunxi-cedar",
};
/*method relating*/
static struct platform_driver sw_cedar_driver = {
#ifdef CONFIG_PM
.suspend = snd_sw_cedar_suspend,
.resume = snd_sw_cedar_resume,
#endif
.driver = {
.name = "sunxi-cedar",
},
};
static int __init cedardev_init(void)
{
int ret = 0;
int err = 0;
int devno;
unsigned int val;
dev_t dev = 0;
#ifdef CONFIG_CMA
/* If having CMA enabled, just rely on CMA for memory allocation */
resource_size_t pa;
ve_size = 80 * SZ_1M;
ve_start_virt = dma_alloc_coherent(NULL, ve_size, &pa,
GFP_KERNEL | GFP_DMA);
if (!ve_start_virt) {
printk(KERN_NOTICE "cedar: failed to allocate memory buffer\n");
return -ENODEV;
}
ve_start = pa;
if (ve_start + ve_size > SW_PA_SDRAM_START + SZ_256M) {
printk(KERN_NOTICE "cedar: buffer is above 256MB limit\n");
dma_free_coherent(NULL, ve_size, ve_start_virt, ve_start);
ve_start_virt = 0;
ve_size = 0;
return -ENODEV;
}
#else
if (ve_size == 0) {
printk("[cedar dev]: not installed! ve_mem_reserve=0\n");
return -ENODEV;
}
#endif
printk("[cedar dev]: install start!!!\n");
if((platform_device_register(&sw_device_cedar))<0)
return err;
if ((err = platform_driver_register(&sw_cedar_driver)) < 0)
return err;
/*register or alloc the device number.*/
if (g_dev_major) {
dev = MKDEV(g_dev_major, g_dev_minor);
ret = register_chrdev_region(dev, 1, "cedar_dev");
} else {
ret = alloc_chrdev_region(&dev, g_dev_minor, 1, "cedar_dev");
g_dev_major = MAJOR(dev);
g_dev_minor = MINOR(dev);
}
if (ret < 0) {
printk(KERN_WARNING "cedar_dev: can't get major %d\n", g_dev_major);
return ret;
}
spin_lock_init(&cedar_spin_lock);
cedar_devp = kmalloc(sizeof(struct cedar_dev), GFP_KERNEL);
if (cedar_devp == NULL) {
printk("malloc mem for cedar device err\n");
return -ENOMEM;
}
memset(cedar_devp, 0, sizeof(struct cedar_dev));
cedar_devp->irq = VE_IRQ_NO;
sema_init(&cedar_devp->sem, 1);
init_waitqueue_head(&cedar_devp->wq);
memset(&cedar_devp->iomap_addrs, 0, sizeof(struct iomap_para));
ret = request_irq(VE_IRQ_NO, VideoEngineInterupt, 0, "cedar_dev", NULL);
if (ret < 0) {
printk("request irq err\n");
return -EINVAL;
}
/* map for macc io space */
cedar_devp->iomap_addrs.regs_macc = ioremap(MACC_REGS_BASE, 4096);
if (!cedar_devp->iomap_addrs.regs_macc){
printk("cannot map region for macc");
}
cedar_devp->iomap_addrs.regs_avs = ioremap(AVS_REGS_BASE, 1024);