forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipu_idmac.c
1797 lines (1494 loc) · 45.3 KB
/
ipu_idmac.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
/*
* Copyright (C) 2008
* Guennadi Liakhovetski, DENX Software Engineering, <[email protected]>
*
* Copyright (C) 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/dma-mapping.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/clk.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <mach/ipu.h>
#include "../dmaengine.h"
#include "ipu_intern.h"
#define FS_VF_IN_VALID 0x00000002
#define FS_ENC_IN_VALID 0x00000001
static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
bool wait_for_stop);
/*
* There can be only one, we could allocate it dynamically, but then we'd have
* to add an extra parameter to some functions, and use something as ugly as
* struct ipu *ipu = to_ipu(to_idmac(ichan->dma_chan.device));
* in the ISR
*/
static struct ipu ipu_data;
#define to_ipu(id) container_of(id, struct ipu, idmac)
static u32 __idmac_read_icreg(struct ipu *ipu, unsigned long reg)
{
return __raw_readl(ipu->reg_ic + reg);
}
#define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
static void __idmac_write_icreg(struct ipu *ipu, u32 value, unsigned long reg)
{
__raw_writel(value, ipu->reg_ic + reg);
}
#define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
static u32 idmac_read_ipureg(struct ipu *ipu, unsigned long reg)
{
return __raw_readl(ipu->reg_ipu + reg);
}
static void idmac_write_ipureg(struct ipu *ipu, u32 value, unsigned long reg)
{
__raw_writel(value, ipu->reg_ipu + reg);
}
/*****************************************************************************
* IPU / IC common functions
*/
static void dump_idmac_reg(struct ipu *ipu)
{
dev_dbg(ipu->dev, "IDMAC_CONF 0x%x, IC_CONF 0x%x, IDMAC_CHA_EN 0x%x, "
"IDMAC_CHA_PRI 0x%x, IDMAC_CHA_BUSY 0x%x\n",
idmac_read_icreg(ipu, IDMAC_CONF),
idmac_read_icreg(ipu, IC_CONF),
idmac_read_icreg(ipu, IDMAC_CHA_EN),
idmac_read_icreg(ipu, IDMAC_CHA_PRI),
idmac_read_icreg(ipu, IDMAC_CHA_BUSY));
dev_dbg(ipu->dev, "BUF0_RDY 0x%x, BUF1_RDY 0x%x, CUR_BUF 0x%x, "
"DB_MODE 0x%x, TASKS_STAT 0x%x\n",
idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
idmac_read_ipureg(ipu, IPU_CHA_CUR_BUF),
idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL),
idmac_read_ipureg(ipu, IPU_TASKS_STAT));
}
static uint32_t bytes_per_pixel(enum pixel_fmt fmt)
{
switch (fmt) {
case IPU_PIX_FMT_GENERIC: /* generic data */
case IPU_PIX_FMT_RGB332:
case IPU_PIX_FMT_YUV420P:
case IPU_PIX_FMT_YUV422P:
default:
return 1;
case IPU_PIX_FMT_RGB565:
case IPU_PIX_FMT_YUYV:
case IPU_PIX_FMT_UYVY:
return 2;
case IPU_PIX_FMT_BGR24:
case IPU_PIX_FMT_RGB24:
return 3;
case IPU_PIX_FMT_GENERIC_32: /* generic data */
case IPU_PIX_FMT_BGR32:
case IPU_PIX_FMT_RGB32:
case IPU_PIX_FMT_ABGR32:
return 4;
}
}
/* Enable direct write to memory by the Camera Sensor Interface */
static void ipu_ic_enable_task(struct ipu *ipu, enum ipu_channel channel)
{
uint32_t ic_conf, mask;
switch (channel) {
case IDMAC_IC_0:
mask = IC_CONF_PRPENC_EN;
break;
case IDMAC_IC_7:
mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
break;
default:
return;
}
ic_conf = idmac_read_icreg(ipu, IC_CONF) | mask;
idmac_write_icreg(ipu, ic_conf, IC_CONF);
}
/* Called under spin_lock_irqsave(&ipu_data.lock) */
static void ipu_ic_disable_task(struct ipu *ipu, enum ipu_channel channel)
{
uint32_t ic_conf, mask;
switch (channel) {
case IDMAC_IC_0:
mask = IC_CONF_PRPENC_EN;
break;
case IDMAC_IC_7:
mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
break;
default:
return;
}
ic_conf = idmac_read_icreg(ipu, IC_CONF) & ~mask;
idmac_write_icreg(ipu, ic_conf, IC_CONF);
}
static uint32_t ipu_channel_status(struct ipu *ipu, enum ipu_channel channel)
{
uint32_t stat = TASK_STAT_IDLE;
uint32_t task_stat_reg = idmac_read_ipureg(ipu, IPU_TASKS_STAT);
switch (channel) {
case IDMAC_IC_7:
stat = (task_stat_reg & TSTAT_CSI2MEM_MASK) >>
TSTAT_CSI2MEM_OFFSET;
break;
case IDMAC_IC_0:
case IDMAC_SDC_0:
case IDMAC_SDC_1:
default:
break;
}
return stat;
}
struct chan_param_mem_planar {
/* Word 0 */
u32 xv:10;
u32 yv:10;
u32 xb:12;
u32 yb:12;
u32 res1:2;
u32 nsb:1;
u32 lnpb:6;
u32 ubo_l:11;
u32 ubo_h:15;
u32 vbo_l:17;
u32 vbo_h:9;
u32 res2:3;
u32 fw:12;
u32 fh_l:8;
u32 fh_h:4;
u32 res3:28;
/* Word 1 */
u32 eba0;
u32 eba1;
u32 bpp:3;
u32 sl:14;
u32 pfs:3;
u32 bam:3;
u32 res4:2;
u32 npb:6;
u32 res5:1;
u32 sat:2;
u32 res6:30;
} __attribute__ ((packed));
struct chan_param_mem_interleaved {
/* Word 0 */
u32 xv:10;
u32 yv:10;
u32 xb:12;
u32 yb:12;
u32 sce:1;
u32 res1:1;
u32 nsb:1;
u32 lnpb:6;
u32 sx:10;
u32 sy_l:1;
u32 sy_h:9;
u32 ns:10;
u32 sm:10;
u32 sdx_l:3;
u32 sdx_h:2;
u32 sdy:5;
u32 sdrx:1;
u32 sdry:1;
u32 sdr1:1;
u32 res2:2;
u32 fw:12;
u32 fh_l:8;
u32 fh_h:4;
u32 res3:28;
/* Word 1 */
u32 eba0;
u32 eba1;
u32 bpp:3;
u32 sl:14;
u32 pfs:3;
u32 bam:3;
u32 res4:2;
u32 npb:6;
u32 res5:1;
u32 sat:2;
u32 scc:1;
u32 ofs0:5;
u32 ofs1:5;
u32 ofs2:5;
u32 ofs3:5;
u32 wid0:3;
u32 wid1:3;
u32 wid2:3;
u32 wid3:3;
u32 dec_sel:1;
u32 res6:28;
} __attribute__ ((packed));
union chan_param_mem {
struct chan_param_mem_planar pp;
struct chan_param_mem_interleaved ip;
};
static void ipu_ch_param_set_plane_offset(union chan_param_mem *params,
u32 u_offset, u32 v_offset)
{
params->pp.ubo_l = u_offset & 0x7ff;
params->pp.ubo_h = u_offset >> 11;
params->pp.vbo_l = v_offset & 0x1ffff;
params->pp.vbo_h = v_offset >> 17;
}
static void ipu_ch_param_set_size(union chan_param_mem *params,
uint32_t pixel_fmt, uint16_t width,
uint16_t height, uint16_t stride)
{
u32 u_offset;
u32 v_offset;
params->pp.fw = width - 1;
params->pp.fh_l = height - 1;
params->pp.fh_h = (height - 1) >> 8;
params->pp.sl = stride - 1;
switch (pixel_fmt) {
case IPU_PIX_FMT_GENERIC:
/*Represents 8-bit Generic data */
params->pp.bpp = 3;
params->pp.pfs = 7;
params->pp.npb = 31;
params->pp.sat = 2; /* SAT = use 32-bit access */
break;
case IPU_PIX_FMT_GENERIC_32:
/*Represents 32-bit Generic data */
params->pp.bpp = 0;
params->pp.pfs = 7;
params->pp.npb = 7;
params->pp.sat = 2; /* SAT = use 32-bit access */
break;
case IPU_PIX_FMT_RGB565:
params->ip.bpp = 2;
params->ip.pfs = 4;
params->ip.npb = 15;
params->ip.sat = 2; /* SAT = 32-bit access */
params->ip.ofs0 = 0; /* Red bit offset */
params->ip.ofs1 = 5; /* Green bit offset */
params->ip.ofs2 = 11; /* Blue bit offset */
params->ip.ofs3 = 16; /* Alpha bit offset */
params->ip.wid0 = 4; /* Red bit width - 1 */
params->ip.wid1 = 5; /* Green bit width - 1 */
params->ip.wid2 = 4; /* Blue bit width - 1 */
break;
case IPU_PIX_FMT_BGR24:
params->ip.bpp = 1; /* 24 BPP & RGB PFS */
params->ip.pfs = 4;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
params->ip.ofs0 = 0; /* Red bit offset */
params->ip.ofs1 = 8; /* Green bit offset */
params->ip.ofs2 = 16; /* Blue bit offset */
params->ip.ofs3 = 24; /* Alpha bit offset */
params->ip.wid0 = 7; /* Red bit width - 1 */
params->ip.wid1 = 7; /* Green bit width - 1 */
params->ip.wid2 = 7; /* Blue bit width - 1 */
break;
case IPU_PIX_FMT_RGB24:
params->ip.bpp = 1; /* 24 BPP & RGB PFS */
params->ip.pfs = 4;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
params->ip.ofs0 = 16; /* Red bit offset */
params->ip.ofs1 = 8; /* Green bit offset */
params->ip.ofs2 = 0; /* Blue bit offset */
params->ip.ofs3 = 24; /* Alpha bit offset */
params->ip.wid0 = 7; /* Red bit width - 1 */
params->ip.wid1 = 7; /* Green bit width - 1 */
params->ip.wid2 = 7; /* Blue bit width - 1 */
break;
case IPU_PIX_FMT_BGRA32:
case IPU_PIX_FMT_BGR32:
case IPU_PIX_FMT_ABGR32:
params->ip.bpp = 0;
params->ip.pfs = 4;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
params->ip.ofs0 = 8; /* Red bit offset */
params->ip.ofs1 = 16; /* Green bit offset */
params->ip.ofs2 = 24; /* Blue bit offset */
params->ip.ofs3 = 0; /* Alpha bit offset */
params->ip.wid0 = 7; /* Red bit width - 1 */
params->ip.wid1 = 7; /* Green bit width - 1 */
params->ip.wid2 = 7; /* Blue bit width - 1 */
params->ip.wid3 = 7; /* Alpha bit width - 1 */
break;
case IPU_PIX_FMT_RGBA32:
case IPU_PIX_FMT_RGB32:
params->ip.bpp = 0;
params->ip.pfs = 4;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
params->ip.ofs0 = 24; /* Red bit offset */
params->ip.ofs1 = 16; /* Green bit offset */
params->ip.ofs2 = 8; /* Blue bit offset */
params->ip.ofs3 = 0; /* Alpha bit offset */
params->ip.wid0 = 7; /* Red bit width - 1 */
params->ip.wid1 = 7; /* Green bit width - 1 */
params->ip.wid2 = 7; /* Blue bit width - 1 */
params->ip.wid3 = 7; /* Alpha bit width - 1 */
break;
case IPU_PIX_FMT_UYVY:
params->ip.bpp = 2;
params->ip.pfs = 6;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
break;
case IPU_PIX_FMT_YUV420P2:
case IPU_PIX_FMT_YUV420P:
params->ip.bpp = 3;
params->ip.pfs = 3;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
u_offset = stride * height;
v_offset = u_offset + u_offset / 4;
ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
break;
case IPU_PIX_FMT_YVU422P:
params->ip.bpp = 3;
params->ip.pfs = 2;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
v_offset = stride * height;
u_offset = v_offset + v_offset / 2;
ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
break;
case IPU_PIX_FMT_YUV422P:
params->ip.bpp = 3;
params->ip.pfs = 2;
params->ip.npb = 7;
params->ip.sat = 2; /* SAT = 32-bit access */
u_offset = stride * height;
v_offset = u_offset + u_offset / 2;
ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
break;
default:
dev_err(ipu_data.dev,
"mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
break;
}
params->pp.nsb = 1;
}
static void ipu_ch_param_set_buffer(union chan_param_mem *params,
dma_addr_t buf0, dma_addr_t buf1)
{
params->pp.eba0 = buf0;
params->pp.eba1 = buf1;
}
static void ipu_ch_param_set_rotation(union chan_param_mem *params,
enum ipu_rotate_mode rotate)
{
params->pp.bam = rotate;
}
static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
uint32_t num_words)
{
for (; num_words > 0; num_words--) {
dev_dbg(ipu_data.dev,
"write param mem - addr = 0x%08X, data = 0x%08X\n",
addr, *data);
idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
addr++;
if ((addr & 0x7) == 5) {
addr &= ~0x7; /* set to word 0 */
addr += 8; /* increment to next row */
}
}
}
static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
uint32_t *resize_coeff,
uint32_t *downsize_coeff)
{
uint32_t temp_size;
uint32_t temp_downsize;
*resize_coeff = 1 << 13;
*downsize_coeff = 1 << 13;
/* Cannot downsize more than 8:1 */
if (out_size << 3 < in_size)
return -EINVAL;
/* compute downsizing coefficient */
temp_downsize = 0;
temp_size = in_size;
while (temp_size >= out_size * 2 && temp_downsize < 2) {
temp_size >>= 1;
temp_downsize++;
}
*downsize_coeff = temp_downsize;
/*
* compute resizing coefficient using the following formula:
* resize_coeff = M*(SI -1)/(SO - 1)
* where M = 2^13, SI - input size, SO - output size
*/
*resize_coeff = (8192L * (temp_size - 1)) / (out_size - 1);
if (*resize_coeff >= 16384L) {
dev_err(ipu_data.dev, "Warning! Overflow on resize coeff.\n");
*resize_coeff = 0x3FFF;
}
dev_dbg(ipu_data.dev, "resizing from %u -> %u pixels, "
"downsize=%u, resize=%u.%lu (reg=%u)\n", in_size, out_size,
*downsize_coeff, *resize_coeff >= 8192L ? 1 : 0,
((*resize_coeff & 0x1FFF) * 10000L) / 8192L, *resize_coeff);
return 0;
}
static enum ipu_color_space format_to_colorspace(enum pixel_fmt fmt)
{
switch (fmt) {
case IPU_PIX_FMT_RGB565:
case IPU_PIX_FMT_BGR24:
case IPU_PIX_FMT_RGB24:
case IPU_PIX_FMT_BGR32:
case IPU_PIX_FMT_RGB32:
return IPU_COLORSPACE_RGB;
default:
return IPU_COLORSPACE_YCBCR;
}
}
static int ipu_ic_init_prpenc(struct ipu *ipu,
union ipu_channel_param *params, bool src_is_csi)
{
uint32_t reg, ic_conf;
uint32_t downsize_coeff, resize_coeff;
enum ipu_color_space in_fmt, out_fmt;
/* Setup vertical resizing */
calc_resize_coeffs(params->video.in_height,
params->video.out_height,
&resize_coeff, &downsize_coeff);
reg = (downsize_coeff << 30) | (resize_coeff << 16);
/* Setup horizontal resizing */
calc_resize_coeffs(params->video.in_width,
params->video.out_width,
&resize_coeff, &downsize_coeff);
reg |= (downsize_coeff << 14) | resize_coeff;
/* Setup color space conversion */
in_fmt = format_to_colorspace(params->video.in_pixel_fmt);
out_fmt = format_to_colorspace(params->video.out_pixel_fmt);
/*
* Colourspace conversion unsupported yet - see _init_csc() in
* Freescale sources
*/
if (in_fmt != out_fmt) {
dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
return -EOPNOTSUPP;
}
idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
ic_conf = idmac_read_icreg(ipu, IC_CONF);
if (src_is_csi)
ic_conf &= ~IC_CONF_RWS_EN;
else
ic_conf |= IC_CONF_RWS_EN;
idmac_write_icreg(ipu, ic_conf, IC_CONF);
return 0;
}
static uint32_t dma_param_addr(uint32_t dma_ch)
{
/* Channel Parameter Memory */
return 0x10000 | (dma_ch << 4);
}
static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
bool prio)
{
u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
if (prio)
reg |= 1UL << channel;
else
reg &= ~(1UL << channel);
idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
dump_idmac_reg(ipu);
}
static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
{
uint32_t mask;
switch (channel) {
case IDMAC_IC_0:
case IDMAC_IC_7:
mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
break;
case IDMAC_SDC_0:
case IDMAC_SDC_1:
mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
break;
default:
mask = 0;
break;
}
return mask;
}
/**
* ipu_enable_channel() - enable an IPU channel.
* @idmac: IPU DMAC context.
* @ichan: IDMAC channel.
* @return: 0 on success or negative error code on failure.
*/
static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
{
struct ipu *ipu = to_ipu(idmac);
enum ipu_channel channel = ichan->dma_chan.chan_id;
uint32_t reg;
unsigned long flags;
spin_lock_irqsave(&ipu->lock, flags);
/* Reset to buffer 0 */
idmac_write_ipureg(ipu, 1UL << channel, IPU_CHA_CUR_BUF);
ichan->active_buffer = 0;
ichan->status = IPU_CHANNEL_ENABLED;
switch (channel) {
case IDMAC_SDC_0:
case IDMAC_SDC_1:
case IDMAC_IC_7:
ipu_channel_set_priority(ipu, channel, true);
default:
break;
}
reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
ipu_ic_enable_task(ipu, channel);
spin_unlock_irqrestore(&ipu->lock, flags);
return 0;
}
/**
* ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
* @ichan: IDMAC channel.
* @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
* @width: width of buffer in pixels.
* @height: height of buffer in pixels.
* @stride: stride length of buffer in pixels.
* @rot_mode: rotation mode of buffer. A rotation setting other than
* IPU_ROTATE_VERT_FLIP should only be used for input buffers of
* rotation channels.
* @phyaddr_0: buffer 0 physical address.
* @phyaddr_1: buffer 1 physical address. Setting this to a value other than
* NULL enables double buffering mode.
* @return: 0 on success or negative error code on failure.
*/
static int ipu_init_channel_buffer(struct idmac_channel *ichan,
enum pixel_fmt pixel_fmt,
uint16_t width, uint16_t height,
uint32_t stride,
enum ipu_rotate_mode rot_mode,
dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
{
enum ipu_channel channel = ichan->dma_chan.chan_id;
struct idmac *idmac = to_idmac(ichan->dma_chan.device);
struct ipu *ipu = to_ipu(idmac);
union chan_param_mem params = {};
unsigned long flags;
uint32_t reg;
uint32_t stride_bytes;
stride_bytes = stride * bytes_per_pixel(pixel_fmt);
if (stride_bytes % 4) {
dev_err(ipu->dev,
"Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
stride, stride_bytes);
return -EINVAL;
}
/* IC channel's stride must be a multiple of 8 pixels */
if ((channel <= IDMAC_IC_13) && (stride % 8)) {
dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
return -EINVAL;
}
/* Build parameter memory data for DMA channel */
ipu_ch_param_set_size(¶ms, pixel_fmt, width, height, stride_bytes);
ipu_ch_param_set_buffer(¶ms, phyaddr_0, phyaddr_1);
ipu_ch_param_set_rotation(¶ms, rot_mode);
spin_lock_irqsave(&ipu->lock, flags);
ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)¶ms, 10);
reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
if (phyaddr_1)
reg |= 1UL << channel;
else
reg &= ~(1UL << channel);
idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
ichan->status = IPU_CHANNEL_READY;
spin_unlock_irqrestore(&ipu->lock, flags);
return 0;
}
/**
* ipu_select_buffer() - mark a channel's buffer as ready.
* @channel: channel ID.
* @buffer_n: buffer number to mark ready.
*/
static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
{
/* No locking - this is a write-one-to-set register, cleared by IPU */
if (buffer_n == 0)
/* Mark buffer 0 as ready. */
idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
else
/* Mark buffer 1 as ready. */
idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
}
/**
* ipu_update_channel_buffer() - update physical address of a channel buffer.
* @ichan: IDMAC channel.
* @buffer_n: buffer number to update.
* 0 or 1 are the only valid values.
* @phyaddr: buffer physical address.
*/
/* Called under spin_lock(_irqsave)(&ichan->lock) */
static void ipu_update_channel_buffer(struct idmac_channel *ichan,
int buffer_n, dma_addr_t phyaddr)
{
enum ipu_channel channel = ichan->dma_chan.chan_id;
uint32_t reg;
unsigned long flags;
spin_lock_irqsave(&ipu_data.lock, flags);
if (buffer_n == 0) {
reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
if (reg & (1UL << channel)) {
ipu_ic_disable_task(&ipu_data, channel);
ichan->status = IPU_CHANNEL_READY;
}
/* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
0x0008UL, IPU_IMA_ADDR);
idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
} else {
reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
if (reg & (1UL << channel)) {
ipu_ic_disable_task(&ipu_data, channel);
ichan->status = IPU_CHANNEL_READY;
}
/* Check if double-buffering is already enabled */
reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
if (!(reg & (1UL << channel)))
idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
IPU_CHA_DB_MODE_SEL);
/* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
0x0009UL, IPU_IMA_ADDR);
idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
}
spin_unlock_irqrestore(&ipu_data.lock, flags);
}
/* Called under spin_lock_irqsave(&ichan->lock) */
static int ipu_submit_buffer(struct idmac_channel *ichan,
struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
{
unsigned int chan_id = ichan->dma_chan.chan_id;
struct device *dev = &ichan->dma_chan.dev->device;
if (async_tx_test_ack(&desc->txd))
return -EINTR;
/*
* On first invocation this shouldn't be necessary, the call to
* ipu_init_channel_buffer() above will set addresses for us, so we
* could make it conditional on status >= IPU_CHANNEL_ENABLED, but
* doing it again shouldn't hurt either.
*/
ipu_update_channel_buffer(ichan, buf_idx, sg_dma_address(sg));
ipu_select_buffer(chan_id, buf_idx);
dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
sg, chan_id, buf_idx);
return 0;
}
/* Called under spin_lock_irqsave(&ichan->lock) */
static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
struct idmac_tx_desc *desc)
{
struct scatterlist *sg;
int i, ret = 0;
for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
if (!ichan->sg[i]) {
ichan->sg[i] = sg;
ret = ipu_submit_buffer(ichan, desc, sg, i);
if (ret < 0)
return ret;
sg = sg_next(sg);
}
}
return ret;
}
static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
{
struct idmac_tx_desc *desc = to_tx_desc(tx);
struct idmac_channel *ichan = to_idmac_chan(tx->chan);
struct idmac *idmac = to_idmac(tx->chan->device);
struct ipu *ipu = to_ipu(idmac);
struct device *dev = &ichan->dma_chan.dev->device;
dma_cookie_t cookie;
unsigned long flags;
int ret;
/* Sanity check */
if (!list_empty(&desc->list)) {
/* The descriptor doesn't belong to client */
dev_err(dev, "Descriptor %p not prepared!\n", tx);
return -EBUSY;
}
mutex_lock(&ichan->chan_mutex);
async_tx_clear_ack(tx);
if (ichan->status < IPU_CHANNEL_READY) {
struct idmac_video_param *video = &ichan->params.video;
/*
* Initial buffer assignment - the first two sg-entries from
* the descriptor will end up in the IDMAC buffers
*/
dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
sg_dma_address(&desc->sg[1]);
WARN_ON(ichan->sg[0] || ichan->sg[1]);
cookie = ipu_init_channel_buffer(ichan,
video->out_pixel_fmt,
video->out_width,
video->out_height,
video->out_stride,
IPU_ROTATE_NONE,
sg_dma_address(&desc->sg[0]),
dma_1);
if (cookie < 0)
goto out;
}
dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
cookie = dma_cookie_assign(tx);
/* ipu->lock can be taken under ichan->lock, but not v.v. */
spin_lock_irqsave(&ichan->lock, flags);
list_add_tail(&desc->list, &ichan->queue);
/* submit_buffers() atomically verifies and fills empty sg slots */
ret = ipu_submit_channel_buffers(ichan, desc);
spin_unlock_irqrestore(&ichan->lock, flags);
if (ret < 0) {
cookie = ret;
goto dequeue;
}
if (ichan->status < IPU_CHANNEL_ENABLED) {
ret = ipu_enable_channel(idmac, ichan);
if (ret < 0) {
cookie = ret;
goto dequeue;
}
}
dump_idmac_reg(ipu);
dequeue:
if (cookie < 0) {
spin_lock_irqsave(&ichan->lock, flags);
list_del_init(&desc->list);
spin_unlock_irqrestore(&ichan->lock, flags);
tx->cookie = cookie;
ichan->dma_chan.cookie = cookie;
}
out:
mutex_unlock(&ichan->chan_mutex);
return cookie;
}
/* Called with ichan->chan_mutex held */
static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
{
struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
struct idmac *idmac = to_idmac(ichan->dma_chan.device);
if (!desc)
return -ENOMEM;
/* No interrupts, just disable the tasklet for a moment */
tasklet_disable(&to_ipu(idmac)->tasklet);
ichan->n_tx_desc = n;
ichan->desc = desc;
INIT_LIST_HEAD(&ichan->queue);
INIT_LIST_HEAD(&ichan->free_list);
while (n--) {
struct dma_async_tx_descriptor *txd = &desc->txd;
memset(txd, 0, sizeof(*txd));
dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
txd->tx_submit = idmac_tx_submit;
list_add(&desc->list, &ichan->free_list);
desc++;
}
tasklet_enable(&to_ipu(idmac)->tasklet);
return 0;
}
/**
* ipu_init_channel() - initialize an IPU channel.
* @idmac: IPU DMAC context.
* @ichan: pointer to the channel object.
* @return 0 on success or negative error code on failure.
*/
static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
{
union ipu_channel_param *params = &ichan->params;
uint32_t ipu_conf;
enum ipu_channel channel = ichan->dma_chan.chan_id;
unsigned long flags;
uint32_t reg;
struct ipu *ipu = to_ipu(idmac);
int ret = 0, n_desc = 0;
dev_dbg(ipu->dev, "init channel = %d\n", channel);
if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
channel != IDMAC_IC_7)
return -EINVAL;
spin_lock_irqsave(&ipu->lock, flags);
switch (channel) {
case IDMAC_IC_7:
n_desc = 16;
reg = idmac_read_icreg(ipu, IC_CONF);
idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
break;
case IDMAC_IC_0:
n_desc = 16;
reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
ret = ipu_ic_init_prpenc(ipu, params, true);
break;
case IDMAC_SDC_0:
case IDMAC_SDC_1:
n_desc = 4;
default:
break;
}
ipu->channel_init_mask |= 1L << channel;
/* Enable IPU sub module */
ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
ipu_channel_conf_mask(channel);
idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
spin_unlock_irqrestore(&ipu->lock, flags);
if (n_desc && !ichan->desc)
ret = idmac_desc_alloc(ichan, n_desc);
dump_idmac_reg(ipu);