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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
// SPDX-License-Identifier: GPL-2.0
/*
* c8sectpfe-core.c - C8SECTPFE STi DVB driver
*
* Copyright (c) STMicroelectronics 2015
*
* Author:Peter Bennett <peter.bennett@st.com>
* Peter Griffin <peter.griffin@linaro.org>
*
*/
#include <linux/atomic.h>
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/frontend.h>
#include <linux/errno.h>
#include <linux/firmware.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/version.h>
#include <linux/wait.h>
#include <linux/pinctrl/pinctrl.h>
#include "c8sectpfe-core.h"
#include "c8sectpfe-common.h"
#include "c8sectpfe-debugfs.h"
#include <media/dmxdev.h>
#include <media/dvb_demux.h>
#include <media/dvb_frontend.h>
#include <media/dvb_net.h>
#define FIRMWARE_MEMDMA "pti_memdma_h407.elf"
MODULE_FIRMWARE(FIRMWARE_MEMDMA);
#define PID_TABLE_SIZE 1024
#define POLL_MSECS 50
static int load_c8sectpfe_fw(struct c8sectpfei *fei);
#define TS_PKT_SIZE 188
#define HEADER_SIZE (4)
#define PACKET_SIZE (TS_PKT_SIZE+HEADER_SIZE)
#define FEI_ALIGNMENT (32)
/* hw requires minimum of 8*PACKET_SIZE and padded to 8byte boundary */
#define FEI_BUFFER_SIZE (8*PACKET_SIZE*340)
#define FIFO_LEN 1024
static void c8sectpfe_timer_interrupt(struct timer_list *t)
{
struct c8sectpfei *fei = from_timer(fei, t, timer);
struct channel_info *channel;
int chan_num;
/* iterate through input block channels */
for (chan_num = 0; chan_num < fei->tsin_count; chan_num++) {
channel = fei->channel_data[chan_num];
/* is this descriptor initialised and TP enabled */
if (channel->irec && readl(channel->irec + DMA_PRDS_TPENABLE))
tasklet_schedule(&channel->tsklet);
}
fei->timer.expires = jiffies + msecs_to_jiffies(POLL_MSECS);
add_timer(&fei->timer);
}
static void channel_swdemux_tsklet(struct tasklet_struct *t)
{
struct channel_info *channel = from_tasklet(channel, t, tsklet);
struct c8sectpfei *fei;
unsigned long wp, rp;
int pos, num_packets, n, size;
u8 *buf;
if (unlikely(!channel || !channel->irec))
return;
fei = channel->fei;
wp = readl(channel->irec + DMA_PRDS_BUSWP_TP(0));
rp = readl(channel->irec + DMA_PRDS_BUSRP_TP(0));
pos = rp - channel->back_buffer_busaddr;
/* has it wrapped */
if (wp < rp)
wp = channel->back_buffer_busaddr + FEI_BUFFER_SIZE;
size = wp - rp;
num_packets = size / PACKET_SIZE;
/* manage cache so data is visible to CPU */
dma_sync_single_for_cpu(fei->dev,
rp,
size,
DMA_FROM_DEVICE);
buf = (u8 *) channel->back_buffer_aligned;
dev_dbg(fei->dev,
"chan=%d channel=%p num_packets = %d, buf = %p, pos = 0x%x\n\trp=0x%lx, wp=0x%lx\n",
channel->tsin_id, channel, num_packets, buf, pos, rp, wp);
for (n = 0; n < num_packets; n++) {
dvb_dmx_swfilter_packets(
&fei->c8sectpfe[0]->
demux[channel->demux_mapping].dvb_demux,
&buf[pos], 1);
pos += PACKET_SIZE;
}
/* advance the read pointer */
if (wp == (channel->back_buffer_busaddr + FEI_BUFFER_SIZE))
writel(channel->back_buffer_busaddr, channel->irec +
DMA_PRDS_BUSRP_TP(0));
else
writel(wp, channel->irec + DMA_PRDS_BUSRP_TP(0));
}
static int c8sectpfe_start_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux *demux = dvbdmxfeed->demux;
struct stdemux *stdemux = (struct stdemux *)demux->priv;
struct c8sectpfei *fei = stdemux->c8sectpfei;
struct channel_info *channel;
u32 tmp;
unsigned long *bitmap;
int ret;
switch (dvbdmxfeed->type) {
case DMX_TYPE_TS:
break;
case DMX_TYPE_SEC:
break;
default:
dev_err(fei->dev, "%s:%d Error bailing\n"
, __func__, __LINE__);
return -EINVAL;
}
if (dvbdmxfeed->type == DMX_TYPE_TS) {
switch (dvbdmxfeed->pes_type) {
case DMX_PES_VIDEO:
case DMX_PES_AUDIO:
case DMX_PES_TELETEXT:
case DMX_PES_PCR:
case DMX_PES_OTHER:
break;
default:
dev_err(fei->dev, "%s:%d Error bailing\n"
, __func__, __LINE__);
return -EINVAL;
}
}
if (!atomic_read(&fei->fw_loaded)) {
ret = load_c8sectpfe_fw(fei);
if (ret)
return ret;
}
mutex_lock(&fei->lock);
channel = fei->channel_data[stdemux->tsin_index];
bitmap = (unsigned long *) channel->pid_buffer_aligned;
/* 8192 is a special PID */
if (dvbdmxfeed->pid == 8192) {
tmp = readl(fei->io + C8SECTPFE_IB_PID_SET(channel->tsin_id));
tmp &= ~C8SECTPFE_PID_ENABLE;
writel(tmp, fei->io + C8SECTPFE_IB_PID_SET(channel->tsin_id));
} else {
bitmap_set(bitmap, dvbdmxfeed->pid, 1);
}
/* manage cache so PID bitmap is visible to HW */
dma_sync_single_for_device(fei->dev,
channel->pid_buffer_busaddr,
PID_TABLE_SIZE,
DMA_TO_DEVICE);
channel->active = 1;
if (fei->global_feed_count == 0) {
fei->timer.expires = jiffies +
msecs_to_jiffies(msecs_to_jiffies(POLL_MSECS));
add_timer(&fei->timer);
}
if (stdemux->running_feed_count == 0) {
dev_dbg(fei->dev, "Starting channel=%p\n", channel);
tasklet_setup(&channel->tsklet, channel_swdemux_tsklet);
/* Reset the internal inputblock sram pointers */
writel(channel->fifo,
fei->io + C8SECTPFE_IB_BUFF_STRT(channel->tsin_id));
writel(channel->fifo + FIFO_LEN - 1,
fei->io + C8SECTPFE_IB_BUFF_END(channel->tsin_id));
writel(channel->fifo,
fei->io + C8SECTPFE_IB_READ_PNT(channel->tsin_id));
writel(channel->fifo,
fei->io + C8SECTPFE_IB_WRT_PNT(channel->tsin_id));
/* reset read / write memdma ptrs for this channel */
writel(channel->back_buffer_busaddr, channel->irec +
DMA_PRDS_BUSBASE_TP(0));
tmp = channel->back_buffer_busaddr + FEI_BUFFER_SIZE - 1;
writel(tmp, channel->irec + DMA_PRDS_BUSTOP_TP(0));
writel(channel->back_buffer_busaddr, channel->irec +
DMA_PRDS_BUSWP_TP(0));
/* Issue a reset and enable InputBlock */
writel(C8SECTPFE_SYS_ENABLE | C8SECTPFE_SYS_RESET
, fei->io + C8SECTPFE_IB_SYS(channel->tsin_id));
/* and enable the tp */
writel(0x1, channel->irec + DMA_PRDS_TPENABLE);
dev_dbg(fei->dev, "%s:%d Starting DMA feed on stdemux=%p\n"
, __func__, __LINE__, stdemux);
}
stdemux->running_feed_count++;
fei->global_feed_count++;
mutex_unlock(&fei->lock);
return 0;
}
static int c8sectpfe_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
{
struct dvb_demux *demux = dvbdmxfeed->demux;
struct stdemux *stdemux = (struct stdemux *)demux->priv;
struct c8sectpfei *fei = stdemux->c8sectpfei;
struct channel_info *channel;
int idlereq;
u32 tmp;
int ret;
unsigned long *bitmap;
if (!atomic_read(&fei->fw_loaded)) {
ret = load_c8sectpfe_fw(fei);
if (ret)
return ret;
}
mutex_lock(&fei->lock);
channel = fei->channel_data[stdemux->tsin_index];
bitmap = (unsigned long *) channel->pid_buffer_aligned;
if (dvbdmxfeed->pid == 8192) {
tmp = readl(fei->io + C8SECTPFE_IB_PID_SET(channel->tsin_id));
tmp |= C8SECTPFE_PID_ENABLE;
writel(tmp, fei->io + C8SECTPFE_IB_PID_SET(channel->tsin_id));
} else {
bitmap_clear(bitmap, dvbdmxfeed->pid, 1);
}
/* manage cache so data is visible to HW */
dma_sync_single_for_device(fei->dev,
channel->pid_buffer_busaddr,
PID_TABLE_SIZE,
DMA_TO_DEVICE);
if (--stdemux->running_feed_count == 0) {
channel = fei->channel_data[stdemux->tsin_index];
/* TP re-configuration on page 168 of functional spec */
/* disable IB (prevents more TS data going to memdma) */
writel(0, fei->io + C8SECTPFE_IB_SYS(channel->tsin_id));
/* disable this channels descriptor */
writel(0, channel->irec + DMA_PRDS_TPENABLE);
tasklet_disable(&channel->tsklet);
/* now request memdma channel goes idle */
idlereq = (1 << channel->tsin_id) | IDLEREQ;
writel(idlereq, fei->io + DMA_IDLE_REQ);
/* wait for idle irq handler to signal completion */
ret = wait_for_completion_timeout(&channel->idle_completion,
msecs_to_jiffies(100));
if (ret == 0)
dev_warn(fei->dev,
"Timeout waiting for idle irq on tsin%d\n",
channel->tsin_id);
reinit_completion(&channel->idle_completion);
/* reset read / write ptrs for this channel */
writel(channel->back_buffer_busaddr,
channel->irec + DMA_PRDS_BUSBASE_TP(0));
tmp = channel->back_buffer_busaddr + FEI_BUFFER_SIZE - 1;
writel(tmp, channel->irec + DMA_PRDS_BUSTOP_TP(0));
writel(channel->back_buffer_busaddr,
channel->irec + DMA_PRDS_BUSWP_TP(0));
dev_dbg(fei->dev,
"%s:%d stopping DMA feed on stdemux=%p channel=%d\n",
__func__, __LINE__, stdemux, channel->tsin_id);
/* turn off all PIDS in the bitmap */
memset((void *)channel->pid_buffer_aligned
, 0x00, PID_TABLE_SIZE);
/* manage cache so data is visible to HW */
dma_sync_single_for_device(fei->dev,
channel->pid_buffer_busaddr,
PID_TABLE_SIZE,
DMA_TO_DEVICE);
channel->active = 0;
}
if (--fei->global_feed_count == 0) {
dev_dbg(fei->dev, "%s:%d global_feed_count=%d\n"
, __func__, __LINE__, fei->global_feed_count);
del_timer(&fei->timer);
}
mutex_unlock(&fei->lock);
return 0;
}
static struct channel_info *find_channel(struct c8sectpfei *fei, int tsin_num)
{
int i;
for (i = 0; i < C8SECTPFE_MAX_TSIN_CHAN; i++) {
if (!fei->channel_data[i])
continue;
if (fei->channel_data[i]->tsin_id == tsin_num)
return fei->channel_data[i];
}
return NULL;
}
static void c8sectpfe_getconfig(struct c8sectpfei *fei)
{
struct c8sectpfe_hw *hw = &fei->hw_stats;
hw->num_ib = readl(fei->io + SYS_CFG_NUM_IB);
hw->num_mib = readl(fei->io + SYS_CFG_NUM_MIB);
hw->num_swts = readl(fei->io + SYS_CFG_NUM_SWTS);
hw->num_tsout = readl(fei->io + SYS_CFG_NUM_TSOUT);
hw->num_ccsc = readl(fei->io + SYS_CFG_NUM_CCSC);
hw->num_ram = readl(fei->io + SYS_CFG_NUM_RAM);
hw->num_tp = readl(fei->io + SYS_CFG_NUM_TP);
dev_info(fei->dev, "C8SECTPFE hw supports the following:\n");
dev_info(fei->dev, "Input Blocks: %d\n", hw->num_ib);
dev_info(fei->dev, "Merged Input Blocks: %d\n", hw->num_mib);
dev_info(fei->dev, "Software Transport Stream Inputs: %d\n"
, hw->num_swts);
dev_info(fei->dev, "Transport Stream Output: %d\n", hw->num_tsout);
dev_info(fei->dev, "Cable Card Converter: %d\n", hw->num_ccsc);
dev_info(fei->dev, "RAMs supported by C8SECTPFE: %d\n", hw->num_ram);
dev_info(fei->dev, "Tango TPs supported by C8SECTPFE: %d\n"
, hw->num_tp);
}
static irqreturn_t c8sectpfe_idle_irq_handler(int irq, void *priv)
{
struct c8sectpfei *fei = priv;
struct channel_info *chan;
int bit;
unsigned long tmp = readl(fei->io + DMA_IDLE_REQ);
/* page 168 of functional spec: Clear the idle request
by writing 0 to the C8SECTPFE_DMA_IDLE_REQ register. */
/* signal idle completion */
for_each_set_bit(bit, &tmp, fei->hw_stats.num_ib) {
chan = find_channel(fei, bit);
if (chan)
complete(&chan->idle_completion);
}
writel(0, fei->io + DMA_IDLE_REQ);
return IRQ_HANDLED;
}
static void free_input_block(struct c8sectpfei *fei, struct channel_info *tsin)
{
if (!fei || !tsin)
return;
if (tsin->back_buffer_busaddr)
if (!dma_mapping_error(fei->dev, tsin->back_buffer_busaddr))
dma_unmap_single(fei->dev, tsin->back_buffer_busaddr,
FEI_BUFFER_SIZE, DMA_BIDIRECTIONAL);
kfree(tsin->back_buffer_start);
if (tsin->pid_buffer_busaddr)
if (!dma_mapping_error(fei->dev, tsin->pid_buffer_busaddr))
dma_unmap_single(fei->dev, tsin->pid_buffer_busaddr,
PID_TABLE_SIZE, DMA_BIDIRECTIONAL);
kfree(tsin->pid_buffer_start);
}
#define MAX_NAME 20
static int configure_memdma_and_inputblock(struct c8sectpfei *fei,
struct channel_info *tsin)
{
int ret;
u32 tmp;
char tsin_pin_name[MAX_NAME];
if (!fei || !tsin)
return -EINVAL;
dev_dbg(fei->dev, "%s:%d Configuring channel=%p tsin=%d\n"
, __func__, __LINE__, tsin, tsin->tsin_id);
init_completion(&tsin->idle_completion);
tsin->back_buffer_start = kzalloc(FEI_BUFFER_SIZE +
FEI_ALIGNMENT, GFP_KERNEL);
if (!tsin->back_buffer_start) {
ret = -ENOMEM;
goto err_unmap;
}
/* Ensure backbuffer is 32byte aligned */
tsin->back_buffer_aligned = tsin->back_buffer_start
+ FEI_ALIGNMENT;
tsin->back_buffer_aligned = (void *)
(((uintptr_t) tsin->back_buffer_aligned) & ~0x1F);
tsin->back_buffer_busaddr = dma_map_single(fei->dev,
(void *)tsin->back_buffer_aligned,
FEI_BUFFER_SIZE,
DMA_BIDIRECTIONAL);
if (dma_mapping_error(fei->dev, tsin->back_buffer_busaddr)) {
dev_err(fei->dev, "failed to map back_buffer\n");
ret = -EFAULT;
goto err_unmap;
}
/*
* The pid buffer can be configured (in hw) for byte or bit
* per pid. By powers of deduction we conclude stih407 family
* is configured (at SoC design stage) for bit per pid.
*/
tsin->pid_buffer_start = kzalloc(2048, GFP_KERNEL);
if (!tsin->pid_buffer_start) {
ret = -ENOMEM;
goto err_unmap;
}
/*
* PID buffer needs to be aligned to size of the pid table
* which at bit per pid is 1024 bytes (8192 pids / 8).
* PIDF_BASE register enforces this alignment when writing
* the register.
*/
tsin->pid_buffer_aligned = tsin->pid_buffer_start +
PID_TABLE_SIZE;
tsin->pid_buffer_aligned = (void *)
(((uintptr_t) tsin->pid_buffer_aligned) & ~0x3ff);
tsin->pid_buffer_busaddr = dma_map_single(fei->dev,
tsin->pid_buffer_aligned,
PID_TABLE_SIZE,
DMA_BIDIRECTIONAL);
if (dma_mapping_error(fei->dev, tsin->pid_buffer_busaddr)) {
dev_err(fei->dev, "failed to map pid_bitmap\n");
ret = -EFAULT;
goto err_unmap;
}
/* manage cache so pid bitmap is visible to HW */
dma_sync_single_for_device(fei->dev,
tsin->pid_buffer_busaddr,
PID_TABLE_SIZE,
DMA_TO_DEVICE);
snprintf(tsin_pin_name, MAX_NAME, "tsin%d-%s", tsin->tsin_id,
(tsin->serial_not_parallel ? "serial" : "parallel"));
tsin->pstate = pinctrl_lookup_state(fei->pinctrl, tsin_pin_name);
if (IS_ERR(tsin->pstate)) {
dev_err(fei->dev, "%s: pinctrl_lookup_state couldn't find %s state\n"
, __func__, tsin_pin_name);
ret = PTR_ERR(tsin->pstate);
goto err_unmap;
}
ret = pinctrl_select_state(fei->pinctrl, tsin->pstate);
if (ret) {
dev_err(fei->dev, "%s: pinctrl_select_state failed\n"
, __func__);
goto err_unmap;
}
/* Enable this input block */
tmp = readl(fei->io + SYS_INPUT_CLKEN);
tmp |= BIT(tsin->tsin_id);
writel(tmp, fei->io + SYS_INPUT_CLKEN);
if (tsin->serial_not_parallel)
tmp |= C8SECTPFE_SERIAL_NOT_PARALLEL;
if (tsin->invert_ts_clk)
tmp |= C8SECTPFE_INVERT_TSCLK;
if (tsin->async_not_sync)
tmp |= C8SECTPFE_ASYNC_NOT_SYNC;
tmp |= C8SECTPFE_ALIGN_BYTE_SOP | C8SECTPFE_BYTE_ENDIANNESS_MSB;
writel(tmp, fei->io + C8SECTPFE_IB_IP_FMT_CFG(tsin->tsin_id));
writel(C8SECTPFE_SYNC(0x9) |
C8SECTPFE_DROP(0x9) |
C8SECTPFE_TOKEN(0x47),
fei->io + C8SECTPFE_IB_SYNCLCKDRP_CFG(tsin->tsin_id));
writel(TS_PKT_SIZE, fei->io + C8SECTPFE_IB_PKT_LEN(tsin->tsin_id));
/* Place the FIFO's at the end of the irec descriptors */
tsin->fifo = (tsin->tsin_id * FIFO_LEN);
writel(tsin->fifo, fei->io + C8SECTPFE_IB_BUFF_STRT(tsin->tsin_id));
writel(tsin->fifo + FIFO_LEN - 1,
fei->io + C8SECTPFE_IB_BUFF_END(tsin->tsin_id));
writel(tsin->fifo, fei->io + C8SECTPFE_IB_READ_PNT(tsin->tsin_id));
writel(tsin->fifo, fei->io + C8SECTPFE_IB_WRT_PNT(tsin->tsin_id));
writel(tsin->pid_buffer_busaddr,
fei->io + PIDF_BASE(tsin->tsin_id));
dev_dbg(fei->dev, "chan=%d PIDF_BASE=0x%x pid_bus_addr=%pad\n",
tsin->tsin_id, readl(fei->io + PIDF_BASE(tsin->tsin_id)),
&tsin->pid_buffer_busaddr);
/* Configure and enable HW PID filtering */
/*
* The PID value is created by assembling the first 8 bytes of
* the TS packet into a 64-bit word in big-endian format. A
* slice of that 64-bit word is taken from
* (PID_OFFSET+PID_NUM_BITS-1) to PID_OFFSET.
*/
tmp = (C8SECTPFE_PID_ENABLE | C8SECTPFE_PID_NUMBITS(13)
| C8SECTPFE_PID_OFFSET(40));
writel(tmp, fei->io + C8SECTPFE_IB_PID_SET(tsin->tsin_id));
dev_dbg(fei->dev, "chan=%d setting wp: %d, rp: %d, buf: %d-%d\n",
tsin->tsin_id,
readl(fei->io + C8SECTPFE_IB_WRT_PNT(tsin->tsin_id)),
readl(fei->io + C8SECTPFE_IB_READ_PNT(tsin->tsin_id)),
readl(fei->io + C8SECTPFE_IB_BUFF_STRT(tsin->tsin_id)),
readl(fei->io + C8SECTPFE_IB_BUFF_END(tsin->tsin_id)));
/* Get base addpress of pointer record block from DMEM */
tsin->irec = fei->io + DMA_MEMDMA_OFFSET + DMA_DMEM_OFFSET +
readl(fei->io + DMA_PTRREC_BASE);
/* fill out pointer record data structure */
/* advance pointer record block to our channel */
tsin->irec += (tsin->tsin_id * DMA_PRDS_SIZE);
writel(tsin->fifo, tsin->irec + DMA_PRDS_MEMBASE);
writel(tsin->fifo + FIFO_LEN - 1, tsin->irec + DMA_PRDS_MEMTOP);
writel((188 + 7)&~7, tsin->irec + DMA_PRDS_PKTSIZE);
writel(0x1, tsin->irec + DMA_PRDS_TPENABLE);
/* read/write pointers with physical bus address */
writel(tsin->back_buffer_busaddr, tsin->irec + DMA_PRDS_BUSBASE_TP(0));
tmp = tsin->back_buffer_busaddr + FEI_BUFFER_SIZE - 1;
writel(tmp, tsin->irec + DMA_PRDS_BUSTOP_TP(0));
writel(tsin->back_buffer_busaddr, tsin->irec + DMA_PRDS_BUSWP_TP(0));
writel(tsin->back_buffer_busaddr, tsin->irec + DMA_PRDS_BUSRP_TP(0));
/* initialize tasklet */
tasklet_setup(&tsin->tsklet, channel_swdemux_tsklet);
return 0;
err_unmap:
free_input_block(fei, tsin);
return ret;
}
static irqreturn_t c8sectpfe_error_irq_handler(int irq, void *priv)
{
struct c8sectpfei *fei = priv;
dev_err(fei->dev, "%s: error handling not yet implemented\n"
, __func__);
/*
* TODO FIXME we should detect some error conditions here
* and ideally so something about them!
*/
return IRQ_HANDLED;
}
static int c8sectpfe_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct device_node *child, *np = dev->of_node;
struct c8sectpfei *fei;
struct resource *res;
int ret, index = 0;
struct channel_info *tsin;
/* Allocate the c8sectpfei structure */
fei = devm_kzalloc(dev, sizeof(struct c8sectpfei), GFP_KERNEL);
if (!fei)
return -ENOMEM;
fei->dev = dev;
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "c8sectpfe");
fei->io = devm_ioremap_resource(dev, res);
if (IS_ERR(fei->io))
return PTR_ERR(fei->io);
res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
"c8sectpfe-ram");
fei->sram = devm_ioremap_resource(dev, res);
if (IS_ERR(fei->sram))
return PTR_ERR(fei->sram);
fei->sram_size = resource_size(res);
fei->idle_irq = platform_get_irq_byname(pdev, "c8sectpfe-idle-irq");
if (fei->idle_irq < 0)
return fei->idle_irq;
fei->error_irq = platform_get_irq_byname(pdev, "c8sectpfe-error-irq");
if (fei->error_irq < 0)
return fei->error_irq;
platform_set_drvdata(pdev, fei);
fei->c8sectpfeclk = devm_clk_get(dev, "c8sectpfe");
if (IS_ERR(fei->c8sectpfeclk)) {
dev_err(dev, "c8sectpfe clk not found\n");
return PTR_ERR(fei->c8sectpfeclk);
}
ret = clk_prepare_enable(fei->c8sectpfeclk);
if (ret) {
dev_err(dev, "Failed to enable c8sectpfe clock\n");
return ret;
}
/* to save power disable all IP's (on by default) */
writel(0, fei->io + SYS_INPUT_CLKEN);
/* Enable memdma clock */
writel(MEMDMAENABLE, fei->io + SYS_OTHER_CLKEN);
/* clear internal sram */
memset_io(fei->sram, 0x0, fei->sram_size);
c8sectpfe_getconfig(fei);
ret = devm_request_irq(dev, fei->idle_irq, c8sectpfe_idle_irq_handler,
0, "c8sectpfe-idle-irq", fei);
if (ret) {
dev_err(dev, "Can't register c8sectpfe-idle-irq IRQ.\n");
goto err_clk_disable;
}
ret = devm_request_irq(dev, fei->error_irq,
c8sectpfe_error_irq_handler, 0,
"c8sectpfe-error-irq", fei);
if (ret) {
dev_err(dev, "Can't register c8sectpfe-error-irq IRQ.\n");
goto err_clk_disable;
}
fei->tsin_count = of_get_child_count(np);
if (fei->tsin_count > C8SECTPFE_MAX_TSIN_CHAN ||
fei->tsin_count > fei->hw_stats.num_ib) {
dev_err(dev, "More tsin declared than exist on SoC!\n");
ret = -EINVAL;
goto err_clk_disable;
}
fei->pinctrl = devm_pinctrl_get(dev);
if (IS_ERR(fei->pinctrl)) {
dev_err(dev, "Error getting tsin pins\n");
ret = PTR_ERR(fei->pinctrl);
goto err_clk_disable;
}
for_each_child_of_node(np, child) {
struct device_node *i2c_bus;
fei->channel_data[index] = devm_kzalloc(dev,
sizeof(struct channel_info),
GFP_KERNEL);
if (!fei->channel_data[index]) {
ret = -ENOMEM;
goto err_clk_disable;
}
tsin = fei->channel_data[index];
tsin->fei = fei;
ret = of_property_read_u32(child, "tsin-num", &tsin->tsin_id);
if (ret) {
dev_err(&pdev->dev, "No tsin_num found\n");
goto err_clk_disable;
}
/* sanity check value */
if (tsin->tsin_id > fei->hw_stats.num_ib) {
dev_err(&pdev->dev,
"tsin-num %d specified greater than number\n\tof input block hw in SoC! (%d)",
tsin->tsin_id, fei->hw_stats.num_ib);
ret = -EINVAL;
goto err_clk_disable;
}
tsin->invert_ts_clk = of_property_read_bool(child,
"invert-ts-clk");
tsin->serial_not_parallel = of_property_read_bool(child,
"serial-not-parallel");
tsin->async_not_sync = of_property_read_bool(child,
"async-not-sync");
ret = of_property_read_u32(child, "dvb-card",
&tsin->dvb_card);
if (ret) {
dev_err(&pdev->dev, "No dvb-card found\n");
goto err_clk_disable;
}
i2c_bus = of_parse_phandle(child, "i2c-bus", 0);
if (!i2c_bus) {
dev_err(&pdev->dev, "No i2c-bus found\n");
ret = -ENODEV;
goto err_clk_disable;
}
tsin->i2c_adapter =
of_find_i2c_adapter_by_node(i2c_bus);
if (!tsin->i2c_adapter) {
dev_err(&pdev->dev, "No i2c adapter found\n");
of_node_put(i2c_bus);
ret = -ENODEV;
goto err_clk_disable;
}
of_node_put(i2c_bus);
tsin->rst_gpio = of_get_named_gpio(child, "reset-gpios", 0);
ret = gpio_is_valid(tsin->rst_gpio);
if (!ret) {
dev_err(dev,
"reset gpio for tsin%d not valid (gpio=%d)\n",
tsin->tsin_id, tsin->rst_gpio);
goto err_clk_disable;
}
ret = devm_gpio_request_one(dev, tsin->rst_gpio,
GPIOF_OUT_INIT_LOW, "NIM reset");
if (ret && ret != -EBUSY) {
dev_err(dev, "Can't request tsin%d reset gpio\n"
, fei->channel_data[index]->tsin_id);
goto err_clk_disable;
}
if (!ret) {
/* toggle reset lines */
gpio_direction_output(tsin->rst_gpio, 0);
usleep_range(3500, 5000);
gpio_direction_output(tsin->rst_gpio, 1);
usleep_range(3000, 5000);
}
tsin->demux_mapping = index;
dev_dbg(fei->dev,
"channel=%p n=%d tsin_num=%d, invert-ts-clk=%d\n\tserial-not-parallel=%d pkt-clk-valid=%d dvb-card=%d\n",
fei->channel_data[index], index,
tsin->tsin_id, tsin->invert_ts_clk,
tsin->serial_not_parallel, tsin->async_not_sync,
tsin->dvb_card);
index++;
}
/* Setup timer interrupt */
timer_setup(&fei->timer, c8sectpfe_timer_interrupt, 0);
mutex_init(&fei->lock);
/* Get the configuration information about the tuners */
ret = c8sectpfe_tuner_register_frontend(&fei->c8sectpfe[0],
(void *)fei,
c8sectpfe_start_feed,
c8sectpfe_stop_feed);
if (ret) {
dev_err(dev, "c8sectpfe_tuner_register_frontend failed (%d)\n",
ret);
goto err_clk_disable;
}
c8sectpfe_debugfs_init(fei);
return 0;
err_clk_disable:
clk_disable_unprepare(fei->c8sectpfeclk);
return ret;
}
static int c8sectpfe_remove(struct platform_device *pdev)
{
struct c8sectpfei *fei = platform_get_drvdata(pdev);
struct channel_info *channel;
int i;
wait_for_completion(&fei->fw_ack);
c8sectpfe_tuner_unregister_frontend(fei->c8sectpfe[0], fei);
/*
* Now loop through and un-configure each of the InputBlock resources
*/
for (i = 0; i < fei->tsin_count; i++) {
channel = fei->channel_data[i];
free_input_block(fei, channel);
}
c8sectpfe_debugfs_exit(fei);
dev_info(fei->dev, "Stopping memdma SLIM core\n");
if (readl(fei->io + DMA_CPU_RUN))
writel(0x0, fei->io + DMA_CPU_RUN);
/* unclock all internal IP's */
if (readl(fei->io + SYS_INPUT_CLKEN))
writel(0, fei->io + SYS_INPUT_CLKEN);
if (readl(fei->io + SYS_OTHER_CLKEN))
writel(0, fei->io + SYS_OTHER_CLKEN);
if (fei->c8sectpfeclk)
clk_disable_unprepare(fei->c8sectpfeclk);
return 0;
}
static int configure_channels(struct c8sectpfei *fei)
{
int index = 0, ret;
struct channel_info *tsin;
struct device_node *child, *np = fei->dev->of_node;
/* iterate round each tsin and configure memdma descriptor and IB hw */
for_each_child_of_node(np, child) {
tsin = fei->channel_data[index];
ret = configure_memdma_and_inputblock(fei,
fei->channel_data[index]);
if (ret) {
dev_err(fei->dev,
"configure_memdma_and_inputblock failed\n");
goto err_unmap;
}
index++;
}
return 0;
err_unmap:
for (index = 0; index < fei->tsin_count; index++) {
tsin = fei->channel_data[index];
free_input_block(fei, tsin);
}
return ret;
}
static int
c8sectpfe_elf_sanity_check(struct c8sectpfei *fei, const struct firmware *fw)
{
struct elf32_hdr *ehdr;
char class;
if (!fw) {
dev_err(fei->dev, "failed to load %s\n", FIRMWARE_MEMDMA);
return -EINVAL;
}
if (fw->size < sizeof(struct elf32_hdr)) {
dev_err(fei->dev, "Image is too small\n");
return -EINVAL;
}
ehdr = (struct elf32_hdr *)fw->data;
/* We only support ELF32 at this point */
class = ehdr->e_ident[EI_CLASS];
if (class != ELFCLASS32) {
dev_err(fei->dev, "Unsupported class: %d\n", class);
return -EINVAL;
}
if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
dev_err(fei->dev, "Unsupported firmware endianness\n");
return -EINVAL;
}
if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
dev_err(fei->dev, "Image is too small\n");
return -EINVAL;
}
if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
dev_err(fei->dev, "Image is corrupted (bad magic)\n");
return -EINVAL;
}
/* Check ELF magic */
ehdr = (Elf32_Ehdr *)fw->data;
if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
ehdr->e_ident[EI_MAG3] != ELFMAG3) {
dev_err(fei->dev, "Invalid ELF magic\n");
return -EINVAL;
}
if (ehdr->e_type != ET_EXEC) {
dev_err(fei->dev, "Unsupported ELF header type\n");
return -EINVAL;
}
if (ehdr->e_phoff > fw->size) {
dev_err(fei->dev, "Firmware size is too small\n");
return -EINVAL;
}
return 0;
}
static void load_imem_segment(struct c8sectpfei *fei, Elf32_Phdr *phdr,
const struct firmware *fw, u8 __iomem *dest,
int seg_num)
{
const u8 *imem_src = fw->data + phdr->p_offset;
int i;
/*
* For IMEM segments, the segment contains 24-bit
* instructions which must be padded to 32-bit
* instructions before being written. The written
* segment is padded with NOP instructions.
*/
dev_dbg(fei->dev,
"Loading IMEM segment %d 0x%08x\n\t (0x%x bytes) -> 0x%p (0x%x bytes)\n",
seg_num,
phdr->p_paddr, phdr->p_filesz,
dest, phdr->p_memsz + phdr->p_memsz / 3);
for (i = 0; i < phdr->p_filesz; i++) {
writeb(readb((void __iomem *)imem_src), (void __iomem *)dest);
/* Every 3 bytes, add an additional
* padding zero in destination */
if (i % 3 == 2) {
dest++;
writeb(0x00, (void __iomem *)dest);
}
dest++;
imem_src++;
}
}
static void load_dmem_segment(struct c8sectpfei *fei, Elf32_Phdr *phdr,
const struct firmware *fw, u8 __iomem *dst, int seg_num)
{
/*
* For DMEM segments copy the segment data from the ELF
* file and pad segment with zeroes
*/
dev_dbg(fei->dev,
"Loading DMEM segment %d 0x%08x\n\t(0x%x bytes) -> 0x%p (0x%x bytes)\n",
seg_num, phdr->p_paddr, phdr->p_filesz,
dst, phdr->p_memsz);
memcpy((void __force *)dst, (void *)fw->data + phdr->p_offset,
phdr->p_filesz);
memset((void __force *)dst + phdr->p_filesz, 0,
phdr->p_memsz - phdr->p_filesz);
}
static int load_slim_core_fw(const struct firmware *fw, struct c8sectpfei *fei)
{
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
u8 __iomem *dst;
int err = 0, i;
if (!fw || !fei)
return -EINVAL;
ehdr = (Elf32_Ehdr *)fw->data;
phdr = (Elf32_Phdr *)(fw->data + ehdr->e_phoff);
/* go through the available ELF segments */
for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
/* Only consider LOAD segments */
if (phdr->p_type != PT_LOAD)
continue;
/*
* Check segment is contained within the fw->data buffer
*/
if (phdr->p_offset + phdr->p_filesz > fw->size) {
dev_err(fei->dev,
"Segment %d is outside of firmware file\n", i);
err = -EINVAL;
break;
}
/*
* MEMDMA IMEM has executable flag set, otherwise load
* this segment into DMEM.
*
*/
if (phdr->p_flags & PF_X) {
dst = (u8 __iomem *) fei->io + DMA_MEMDMA_IMEM;
/*
* The Slim ELF file uses 32-bit word addressing for
* load offsets.
*/
dst += (phdr->p_paddr & 0xFFFFF) * sizeof(unsigned int);
load_imem_segment(fei, phdr, fw, dst, i);
} else {
dst = (u8 __iomem *) fei->io + DMA_MEMDMA_DMEM;
/*
* The Slim ELF file uses 32-bit word addressing for
* load offsets.
*/
dst += (phdr->p_paddr & 0xFFFFF) * sizeof(unsigned int);
load_dmem_segment(fei, phdr, fw, dst, i);
}
}
release_firmware(fw);
return err;
}
static int load_c8sectpfe_fw(struct c8sectpfei *fei)
{
const struct firmware *fw;
int err;
dev_info(fei->dev, "Loading firmware: %s\n", FIRMWARE_MEMDMA);
err = request_firmware(&fw, FIRMWARE_MEMDMA, fei->dev);
if (err)
return err;
err = c8sectpfe_elf_sanity_check(fei, fw);
if (err) {
dev_err(fei->dev, "c8sectpfe_elf_sanity_check failed err=(%d)\n"
, err);
release_firmware(fw);
return err;
}
err = load_slim_core_fw(fw, fei);
if (err) {
dev_err(fei->dev, "load_slim_core_fw failed err=(%d)\n", err);
return err;
}
/* now the firmware is loaded configure the input blocks */
err = configure_channels(fei);
if (err) {
dev_err(fei->dev, "configure_channels failed err=(%d)\n", err);
return err;
}
/*
* STBus target port can access IMEM and DMEM ports
* without waiting for CPU
*/
writel(0x1, fei->io + DMA_PER_STBUS_SYNC);
dev_info(fei->dev, "Boot the memdma SLIM core\n");
writel(0x1, fei->io + DMA_CPU_RUN);
atomic_set(&fei->fw_loaded, 1);
return 0;
}
static const struct of_device_id c8sectpfe_match[] = {
{ .compatible = "st,stih407-c8sectpfe" },
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, c8sectpfe_match);
static struct platform_driver c8sectpfe_driver = {
.driver = {
.name = "c8sectpfe",
.of_match_table = of_match_ptr(c8sectpfe_match),
},
.probe = c8sectpfe_probe,
.remove = c8sectpfe_remove,
};
module_platform_driver(c8sectpfe_driver);
MODULE_AUTHOR("Peter Bennett <peter.bennett@st.com>");
MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
MODULE_DESCRIPTION("C8SECTPFE STi DVB Driver");
MODULE_LICENSE("GPL");
|