1 // SPDX-License-Identifier: GPL-2.0
3 * c8sectpfe-core.c - C8SECTPFE STi DVB driver
5 * Copyright (c) STMicroelectronics 2015
7 * Author:Peter Bennett <peter.bennett@st.com>
8 * Peter Griffin <peter.griffin@linaro.org>
11 #include <linux/atomic.h>
12 #include <linux/clk.h>
13 #include <linux/completion.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dvb/dmx.h>
18 #include <linux/dvb/frontend.h>
19 #include <linux/errno.h>
20 #include <linux/firmware.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_platform.h>
27 #include <linux/platform_device.h>
28 #include <linux/usb.h>
29 #include <linux/slab.h>
30 #include <linux/time.h>
31 #include <linux/version.h>
32 #include <linux/wait.h>
33 #include <linux/pinctrl/pinctrl.h>
35 #include "c8sectpfe-core.h"
36 #include "c8sectpfe-common.h"
37 #include "c8sectpfe-debugfs.h"
38 #include <media/dmxdev.h>
39 #include <media/dvb_demux.h>
40 #include <media/dvb_frontend.h>
41 #include <media/dvb_net.h>
43 #define FIRMWARE_MEMDMA "pti_memdma_h407.elf"
44 MODULE_FIRMWARE(FIRMWARE_MEMDMA
);
46 #define PID_TABLE_SIZE 1024
49 static int load_c8sectpfe_fw(struct c8sectpfei
*fei
);
51 #define TS_PKT_SIZE 188
52 #define HEADER_SIZE (4)
53 #define PACKET_SIZE (TS_PKT_SIZE+HEADER_SIZE)
55 #define FEI_ALIGNMENT (32)
56 /* hw requires minimum of 8*PACKET_SIZE and padded to 8byte boundary */
57 #define FEI_BUFFER_SIZE (8*PACKET_SIZE*340)
61 static void c8sectpfe_timer_interrupt(struct timer_list
*t
)
63 struct c8sectpfei
*fei
= from_timer(fei
, t
, timer
);
64 struct channel_info
*channel
;
67 /* iterate through input block channels */
68 for (chan_num
= 0; chan_num
< fei
->tsin_count
; chan_num
++) {
69 channel
= fei
->channel_data
[chan_num
];
71 /* is this descriptor initialised and TP enabled */
72 if (channel
->irec
&& readl(channel
->irec
+ DMA_PRDS_TPENABLE
))
73 tasklet_schedule(&channel
->tsklet
);
76 fei
->timer
.expires
= jiffies
+ msecs_to_jiffies(POLL_MSECS
);
77 add_timer(&fei
->timer
);
80 static void channel_swdemux_tsklet(unsigned long data
)
82 struct channel_info
*channel
= (struct channel_info
*)data
;
83 struct c8sectpfei
*fei
;
85 int pos
, num_packets
, n
, size
;
88 if (unlikely(!channel
|| !channel
->irec
))
93 wp
= readl(channel
->irec
+ DMA_PRDS_BUSWP_TP(0));
94 rp
= readl(channel
->irec
+ DMA_PRDS_BUSRP_TP(0));
96 pos
= rp
- channel
->back_buffer_busaddr
;
100 wp
= channel
->back_buffer_busaddr
+ FEI_BUFFER_SIZE
;
103 num_packets
= size
/ PACKET_SIZE
;
105 /* manage cache so data is visible to CPU */
106 dma_sync_single_for_cpu(fei
->dev
,
111 buf
= (u8
*) channel
->back_buffer_aligned
;
114 "chan=%d channel=%p num_packets = %d, buf = %p, pos = 0x%x\n\trp=0x%lx, wp=0x%lx\n",
115 channel
->tsin_id
, channel
, num_packets
, buf
, pos
, rp
, wp
);
117 for (n
= 0; n
< num_packets
; n
++) {
118 dvb_dmx_swfilter_packets(
120 demux
[channel
->demux_mapping
].dvb_demux
,
126 /* advance the read pointer */
127 if (wp
== (channel
->back_buffer_busaddr
+ FEI_BUFFER_SIZE
))
128 writel(channel
->back_buffer_busaddr
, channel
->irec
+
129 DMA_PRDS_BUSRP_TP(0));
131 writel(wp
, channel
->irec
+ DMA_PRDS_BUSRP_TP(0));
134 static int c8sectpfe_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
136 struct dvb_demux
*demux
= dvbdmxfeed
->demux
;
137 struct stdemux
*stdemux
= (struct stdemux
*)demux
->priv
;
138 struct c8sectpfei
*fei
= stdemux
->c8sectpfei
;
139 struct channel_info
*channel
;
141 unsigned long *bitmap
;
144 switch (dvbdmxfeed
->type
) {
150 dev_err(fei
->dev
, "%s:%d Error bailing\n"
151 , __func__
, __LINE__
);
155 if (dvbdmxfeed
->type
== DMX_TYPE_TS
) {
156 switch (dvbdmxfeed
->pes_type
) {
159 case DMX_PES_TELETEXT
:
164 dev_err(fei
->dev
, "%s:%d Error bailing\n"
165 , __func__
, __LINE__
);
170 if (!atomic_read(&fei
->fw_loaded
)) {
171 ret
= load_c8sectpfe_fw(fei
);
176 mutex_lock(&fei
->lock
);
178 channel
= fei
->channel_data
[stdemux
->tsin_index
];
180 bitmap
= (unsigned long *) channel
->pid_buffer_aligned
;
182 /* 8192 is a special PID */
183 if (dvbdmxfeed
->pid
== 8192) {
184 tmp
= readl(fei
->io
+ C8SECTPFE_IB_PID_SET(channel
->tsin_id
));
185 tmp
&= ~C8SECTPFE_PID_ENABLE
;
186 writel(tmp
, fei
->io
+ C8SECTPFE_IB_PID_SET(channel
->tsin_id
));
189 bitmap_set(bitmap
, dvbdmxfeed
->pid
, 1);
192 /* manage cache so PID bitmap is visible to HW */
193 dma_sync_single_for_device(fei
->dev
,
194 channel
->pid_buffer_busaddr
,
200 if (fei
->global_feed_count
== 0) {
201 fei
->timer
.expires
= jiffies
+
202 msecs_to_jiffies(msecs_to_jiffies(POLL_MSECS
));
204 add_timer(&fei
->timer
);
207 if (stdemux
->running_feed_count
== 0) {
209 dev_dbg(fei
->dev
, "Starting channel=%p\n", channel
);
211 tasklet_init(&channel
->tsklet
, channel_swdemux_tsklet
,
212 (unsigned long) channel
);
214 /* Reset the internal inputblock sram pointers */
215 writel(channel
->fifo
,
216 fei
->io
+ C8SECTPFE_IB_BUFF_STRT(channel
->tsin_id
));
217 writel(channel
->fifo
+ FIFO_LEN
- 1,
218 fei
->io
+ C8SECTPFE_IB_BUFF_END(channel
->tsin_id
));
220 writel(channel
->fifo
,
221 fei
->io
+ C8SECTPFE_IB_READ_PNT(channel
->tsin_id
));
222 writel(channel
->fifo
,
223 fei
->io
+ C8SECTPFE_IB_WRT_PNT(channel
->tsin_id
));
226 /* reset read / write memdma ptrs for this channel */
227 writel(channel
->back_buffer_busaddr
, channel
->irec
+
228 DMA_PRDS_BUSBASE_TP(0));
230 tmp
= channel
->back_buffer_busaddr
+ FEI_BUFFER_SIZE
- 1;
231 writel(tmp
, channel
->irec
+ DMA_PRDS_BUSTOP_TP(0));
233 writel(channel
->back_buffer_busaddr
, channel
->irec
+
234 DMA_PRDS_BUSWP_TP(0));
236 /* Issue a reset and enable InputBlock */
237 writel(C8SECTPFE_SYS_ENABLE
| C8SECTPFE_SYS_RESET
238 , fei
->io
+ C8SECTPFE_IB_SYS(channel
->tsin_id
));
240 /* and enable the tp */
241 writel(0x1, channel
->irec
+ DMA_PRDS_TPENABLE
);
243 dev_dbg(fei
->dev
, "%s:%d Starting DMA feed on stdemux=%p\n"
244 , __func__
, __LINE__
, stdemux
);
247 stdemux
->running_feed_count
++;
248 fei
->global_feed_count
++;
250 mutex_unlock(&fei
->lock
);
255 static int c8sectpfe_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
258 struct dvb_demux
*demux
= dvbdmxfeed
->demux
;
259 struct stdemux
*stdemux
= (struct stdemux
*)demux
->priv
;
260 struct c8sectpfei
*fei
= stdemux
->c8sectpfei
;
261 struct channel_info
*channel
;
265 unsigned long *bitmap
;
267 if (!atomic_read(&fei
->fw_loaded
)) {
268 ret
= load_c8sectpfe_fw(fei
);
273 mutex_lock(&fei
->lock
);
275 channel
= fei
->channel_data
[stdemux
->tsin_index
];
277 bitmap
= (unsigned long *) channel
->pid_buffer_aligned
;
279 if (dvbdmxfeed
->pid
== 8192) {
280 tmp
= readl(fei
->io
+ C8SECTPFE_IB_PID_SET(channel
->tsin_id
));
281 tmp
|= C8SECTPFE_PID_ENABLE
;
282 writel(tmp
, fei
->io
+ C8SECTPFE_IB_PID_SET(channel
->tsin_id
));
284 bitmap_clear(bitmap
, dvbdmxfeed
->pid
, 1);
287 /* manage cache so data is visible to HW */
288 dma_sync_single_for_device(fei
->dev
,
289 channel
->pid_buffer_busaddr
,
293 if (--stdemux
->running_feed_count
== 0) {
295 channel
= fei
->channel_data
[stdemux
->tsin_index
];
297 /* TP re-configuration on page 168 of functional spec */
299 /* disable IB (prevents more TS data going to memdma) */
300 writel(0, fei
->io
+ C8SECTPFE_IB_SYS(channel
->tsin_id
));
302 /* disable this channels descriptor */
303 writel(0, channel
->irec
+ DMA_PRDS_TPENABLE
);
305 tasklet_disable(&channel
->tsklet
);
307 /* now request memdma channel goes idle */
308 idlereq
= (1 << channel
->tsin_id
) | IDLEREQ
;
309 writel(idlereq
, fei
->io
+ DMA_IDLE_REQ
);
311 /* wait for idle irq handler to signal completion */
312 ret
= wait_for_completion_timeout(&channel
->idle_completion
,
313 msecs_to_jiffies(100));
317 "Timeout waiting for idle irq on tsin%d\n",
320 reinit_completion(&channel
->idle_completion
);
322 /* reset read / write ptrs for this channel */
324 writel(channel
->back_buffer_busaddr
,
325 channel
->irec
+ DMA_PRDS_BUSBASE_TP(0));
327 tmp
= channel
->back_buffer_busaddr
+ FEI_BUFFER_SIZE
- 1;
328 writel(tmp
, channel
->irec
+ DMA_PRDS_BUSTOP_TP(0));
330 writel(channel
->back_buffer_busaddr
,
331 channel
->irec
+ DMA_PRDS_BUSWP_TP(0));
334 "%s:%d stopping DMA feed on stdemux=%p channel=%d\n",
335 __func__
, __LINE__
, stdemux
, channel
->tsin_id
);
337 /* turn off all PIDS in the bitmap */
338 memset((void *)channel
->pid_buffer_aligned
339 , 0x00, PID_TABLE_SIZE
);
341 /* manage cache so data is visible to HW */
342 dma_sync_single_for_device(fei
->dev
,
343 channel
->pid_buffer_busaddr
,
350 if (--fei
->global_feed_count
== 0) {
351 dev_dbg(fei
->dev
, "%s:%d global_feed_count=%d\n"
352 , __func__
, __LINE__
, fei
->global_feed_count
);
354 del_timer(&fei
->timer
);
357 mutex_unlock(&fei
->lock
);
362 static struct channel_info
*find_channel(struct c8sectpfei
*fei
, int tsin_num
)
366 for (i
= 0; i
< C8SECTPFE_MAX_TSIN_CHAN
; i
++) {
367 if (!fei
->channel_data
[i
])
370 if (fei
->channel_data
[i
]->tsin_id
== tsin_num
)
371 return fei
->channel_data
[i
];
377 static void c8sectpfe_getconfig(struct c8sectpfei
*fei
)
379 struct c8sectpfe_hw
*hw
= &fei
->hw_stats
;
381 hw
->num_ib
= readl(fei
->io
+ SYS_CFG_NUM_IB
);
382 hw
->num_mib
= readl(fei
->io
+ SYS_CFG_NUM_MIB
);
383 hw
->num_swts
= readl(fei
->io
+ SYS_CFG_NUM_SWTS
);
384 hw
->num_tsout
= readl(fei
->io
+ SYS_CFG_NUM_TSOUT
);
385 hw
->num_ccsc
= readl(fei
->io
+ SYS_CFG_NUM_CCSC
);
386 hw
->num_ram
= readl(fei
->io
+ SYS_CFG_NUM_RAM
);
387 hw
->num_tp
= readl(fei
->io
+ SYS_CFG_NUM_TP
);
389 dev_info(fei
->dev
, "C8SECTPFE hw supports the following:\n");
390 dev_info(fei
->dev
, "Input Blocks: %d\n", hw
->num_ib
);
391 dev_info(fei
->dev
, "Merged Input Blocks: %d\n", hw
->num_mib
);
392 dev_info(fei
->dev
, "Software Transport Stream Inputs: %d\n"
394 dev_info(fei
->dev
, "Transport Stream Output: %d\n", hw
->num_tsout
);
395 dev_info(fei
->dev
, "Cable Card Converter: %d\n", hw
->num_ccsc
);
396 dev_info(fei
->dev
, "RAMs supported by C8SECTPFE: %d\n", hw
->num_ram
);
397 dev_info(fei
->dev
, "Tango TPs supported by C8SECTPFE: %d\n"
401 static irqreturn_t
c8sectpfe_idle_irq_handler(int irq
, void *priv
)
403 struct c8sectpfei
*fei
= priv
;
404 struct channel_info
*chan
;
406 unsigned long tmp
= readl(fei
->io
+ DMA_IDLE_REQ
);
408 /* page 168 of functional spec: Clear the idle request
409 by writing 0 to the C8SECTPFE_DMA_IDLE_REQ register. */
411 /* signal idle completion */
412 for_each_set_bit(bit
, &tmp
, fei
->hw_stats
.num_ib
) {
414 chan
= find_channel(fei
, bit
);
417 complete(&chan
->idle_completion
);
420 writel(0, fei
->io
+ DMA_IDLE_REQ
);
426 static void free_input_block(struct c8sectpfei
*fei
, struct channel_info
*tsin
)
431 if (tsin
->back_buffer_busaddr
)
432 if (!dma_mapping_error(fei
->dev
, tsin
->back_buffer_busaddr
))
433 dma_unmap_single(fei
->dev
, tsin
->back_buffer_busaddr
,
434 FEI_BUFFER_SIZE
, DMA_BIDIRECTIONAL
);
436 kfree(tsin
->back_buffer_start
);
438 if (tsin
->pid_buffer_busaddr
)
439 if (!dma_mapping_error(fei
->dev
, tsin
->pid_buffer_busaddr
))
440 dma_unmap_single(fei
->dev
, tsin
->pid_buffer_busaddr
,
441 PID_TABLE_SIZE
, DMA_BIDIRECTIONAL
);
443 kfree(tsin
->pid_buffer_start
);
448 static int configure_memdma_and_inputblock(struct c8sectpfei
*fei
,
449 struct channel_info
*tsin
)
453 char tsin_pin_name
[MAX_NAME
];
458 dev_dbg(fei
->dev
, "%s:%d Configuring channel=%p tsin=%d\n"
459 , __func__
, __LINE__
, tsin
, tsin
->tsin_id
);
461 init_completion(&tsin
->idle_completion
);
463 tsin
->back_buffer_start
= kzalloc(FEI_BUFFER_SIZE
+
464 FEI_ALIGNMENT
, GFP_KERNEL
);
466 if (!tsin
->back_buffer_start
) {
471 /* Ensure backbuffer is 32byte aligned */
472 tsin
->back_buffer_aligned
= tsin
->back_buffer_start
475 tsin
->back_buffer_aligned
= (void *)
476 (((uintptr_t) tsin
->back_buffer_aligned
) & ~0x1F);
478 tsin
->back_buffer_busaddr
= dma_map_single(fei
->dev
,
479 (void *)tsin
->back_buffer_aligned
,
483 if (dma_mapping_error(fei
->dev
, tsin
->back_buffer_busaddr
)) {
484 dev_err(fei
->dev
, "failed to map back_buffer\n");
490 * The pid buffer can be configured (in hw) for byte or bit
491 * per pid. By powers of deduction we conclude stih407 family
492 * is configured (at SoC design stage) for bit per pid.
494 tsin
->pid_buffer_start
= kzalloc(2048, GFP_KERNEL
);
496 if (!tsin
->pid_buffer_start
) {
502 * PID buffer needs to be aligned to size of the pid table
503 * which at bit per pid is 1024 bytes (8192 pids / 8).
504 * PIDF_BASE register enforces this alignment when writing
508 tsin
->pid_buffer_aligned
= tsin
->pid_buffer_start
+
511 tsin
->pid_buffer_aligned
= (void *)
512 (((uintptr_t) tsin
->pid_buffer_aligned
) & ~0x3ff);
514 tsin
->pid_buffer_busaddr
= dma_map_single(fei
->dev
,
515 tsin
->pid_buffer_aligned
,
519 if (dma_mapping_error(fei
->dev
, tsin
->pid_buffer_busaddr
)) {
520 dev_err(fei
->dev
, "failed to map pid_bitmap\n");
525 /* manage cache so pid bitmap is visible to HW */
526 dma_sync_single_for_device(fei
->dev
,
527 tsin
->pid_buffer_busaddr
,
531 snprintf(tsin_pin_name
, MAX_NAME
, "tsin%d-%s", tsin
->tsin_id
,
532 (tsin
->serial_not_parallel
? "serial" : "parallel"));
534 tsin
->pstate
= pinctrl_lookup_state(fei
->pinctrl
, tsin_pin_name
);
535 if (IS_ERR(tsin
->pstate
)) {
536 dev_err(fei
->dev
, "%s: pinctrl_lookup_state couldn't find %s state\n"
537 , __func__
, tsin_pin_name
);
538 ret
= PTR_ERR(tsin
->pstate
);
542 ret
= pinctrl_select_state(fei
->pinctrl
, tsin
->pstate
);
545 dev_err(fei
->dev
, "%s: pinctrl_select_state failed\n"
550 /* Enable this input block */
551 tmp
= readl(fei
->io
+ SYS_INPUT_CLKEN
);
552 tmp
|= BIT(tsin
->tsin_id
);
553 writel(tmp
, fei
->io
+ SYS_INPUT_CLKEN
);
555 if (tsin
->serial_not_parallel
)
556 tmp
|= C8SECTPFE_SERIAL_NOT_PARALLEL
;
558 if (tsin
->invert_ts_clk
)
559 tmp
|= C8SECTPFE_INVERT_TSCLK
;
561 if (tsin
->async_not_sync
)
562 tmp
|= C8SECTPFE_ASYNC_NOT_SYNC
;
564 tmp
|= C8SECTPFE_ALIGN_BYTE_SOP
| C8SECTPFE_BYTE_ENDIANNESS_MSB
;
566 writel(tmp
, fei
->io
+ C8SECTPFE_IB_IP_FMT_CFG(tsin
->tsin_id
));
568 writel(C8SECTPFE_SYNC(0x9) |
569 C8SECTPFE_DROP(0x9) |
570 C8SECTPFE_TOKEN(0x47),
571 fei
->io
+ C8SECTPFE_IB_SYNCLCKDRP_CFG(tsin
->tsin_id
));
573 writel(TS_PKT_SIZE
, fei
->io
+ C8SECTPFE_IB_PKT_LEN(tsin
->tsin_id
));
575 /* Place the FIFO's at the end of the irec descriptors */
577 tsin
->fifo
= (tsin
->tsin_id
* FIFO_LEN
);
579 writel(tsin
->fifo
, fei
->io
+ C8SECTPFE_IB_BUFF_STRT(tsin
->tsin_id
));
580 writel(tsin
->fifo
+ FIFO_LEN
- 1,
581 fei
->io
+ C8SECTPFE_IB_BUFF_END(tsin
->tsin_id
));
583 writel(tsin
->fifo
, fei
->io
+ C8SECTPFE_IB_READ_PNT(tsin
->tsin_id
));
584 writel(tsin
->fifo
, fei
->io
+ C8SECTPFE_IB_WRT_PNT(tsin
->tsin_id
));
586 writel(tsin
->pid_buffer_busaddr
,
587 fei
->io
+ PIDF_BASE(tsin
->tsin_id
));
589 dev_dbg(fei
->dev
, "chan=%d PIDF_BASE=0x%x pid_bus_addr=%pad\n",
590 tsin
->tsin_id
, readl(fei
->io
+ PIDF_BASE(tsin
->tsin_id
)),
591 &tsin
->pid_buffer_busaddr
);
593 /* Configure and enable HW PID filtering */
596 * The PID value is created by assembling the first 8 bytes of
597 * the TS packet into a 64-bit word in big-endian format. A
598 * slice of that 64-bit word is taken from
599 * (PID_OFFSET+PID_NUM_BITS-1) to PID_OFFSET.
601 tmp
= (C8SECTPFE_PID_ENABLE
| C8SECTPFE_PID_NUMBITS(13)
602 | C8SECTPFE_PID_OFFSET(40));
604 writel(tmp
, fei
->io
+ C8SECTPFE_IB_PID_SET(tsin
->tsin_id
));
606 dev_dbg(fei
->dev
, "chan=%d setting wp: %d, rp: %d, buf: %d-%d\n",
608 readl(fei
->io
+ C8SECTPFE_IB_WRT_PNT(tsin
->tsin_id
)),
609 readl(fei
->io
+ C8SECTPFE_IB_READ_PNT(tsin
->tsin_id
)),
610 readl(fei
->io
+ C8SECTPFE_IB_BUFF_STRT(tsin
->tsin_id
)),
611 readl(fei
->io
+ C8SECTPFE_IB_BUFF_END(tsin
->tsin_id
)));
613 /* Get base addpress of pointer record block from DMEM */
614 tsin
->irec
= fei
->io
+ DMA_MEMDMA_OFFSET
+ DMA_DMEM_OFFSET
+
615 readl(fei
->io
+ DMA_PTRREC_BASE
);
617 /* fill out pointer record data structure */
619 /* advance pointer record block to our channel */
620 tsin
->irec
+= (tsin
->tsin_id
* DMA_PRDS_SIZE
);
622 writel(tsin
->fifo
, tsin
->irec
+ DMA_PRDS_MEMBASE
);
624 writel(tsin
->fifo
+ FIFO_LEN
- 1, tsin
->irec
+ DMA_PRDS_MEMTOP
);
626 writel((188 + 7)&~7, tsin
->irec
+ DMA_PRDS_PKTSIZE
);
628 writel(0x1, tsin
->irec
+ DMA_PRDS_TPENABLE
);
630 /* read/write pointers with physical bus address */
632 writel(tsin
->back_buffer_busaddr
, tsin
->irec
+ DMA_PRDS_BUSBASE_TP(0));
634 tmp
= tsin
->back_buffer_busaddr
+ FEI_BUFFER_SIZE
- 1;
635 writel(tmp
, tsin
->irec
+ DMA_PRDS_BUSTOP_TP(0));
637 writel(tsin
->back_buffer_busaddr
, tsin
->irec
+ DMA_PRDS_BUSWP_TP(0));
638 writel(tsin
->back_buffer_busaddr
, tsin
->irec
+ DMA_PRDS_BUSRP_TP(0));
640 /* initialize tasklet */
641 tasklet_init(&tsin
->tsklet
, channel_swdemux_tsklet
,
642 (unsigned long) tsin
);
647 free_input_block(fei
, tsin
);
651 static irqreturn_t
c8sectpfe_error_irq_handler(int irq
, void *priv
)
653 struct c8sectpfei
*fei
= priv
;
655 dev_err(fei
->dev
, "%s: error handling not yet implemented\n"
659 * TODO FIXME we should detect some error conditions here
660 * and ideally so something about them!
666 static int c8sectpfe_probe(struct platform_device
*pdev
)
668 struct device
*dev
= &pdev
->dev
;
669 struct device_node
*child
, *np
= dev
->of_node
;
670 struct c8sectpfei
*fei
;
671 struct resource
*res
;
673 struct channel_info
*tsin
;
675 /* Allocate the c8sectpfei structure */
676 fei
= devm_kzalloc(dev
, sizeof(struct c8sectpfei
), GFP_KERNEL
);
682 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "c8sectpfe");
683 fei
->io
= devm_ioremap_resource(dev
, res
);
685 return PTR_ERR(fei
->io
);
687 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
689 fei
->sram
= devm_ioremap_resource(dev
, res
);
690 if (IS_ERR(fei
->sram
))
691 return PTR_ERR(fei
->sram
);
693 fei
->sram_size
= resource_size(res
);
695 fei
->idle_irq
= platform_get_irq_byname(pdev
, "c8sectpfe-idle-irq");
696 if (fei
->idle_irq
< 0) {
697 dev_err(dev
, "Can't get c8sectpfe-idle-irq\n");
698 return fei
->idle_irq
;
701 fei
->error_irq
= platform_get_irq_byname(pdev
, "c8sectpfe-error-irq");
702 if (fei
->error_irq
< 0) {
703 dev_err(dev
, "Can't get c8sectpfe-error-irq\n");
704 return fei
->error_irq
;
707 platform_set_drvdata(pdev
, fei
);
709 fei
->c8sectpfeclk
= devm_clk_get(dev
, "c8sectpfe");
710 if (IS_ERR(fei
->c8sectpfeclk
)) {
711 dev_err(dev
, "c8sectpfe clk not found\n");
712 return PTR_ERR(fei
->c8sectpfeclk
);
715 ret
= clk_prepare_enable(fei
->c8sectpfeclk
);
717 dev_err(dev
, "Failed to enable c8sectpfe clock\n");
721 /* to save power disable all IP's (on by default) */
722 writel(0, fei
->io
+ SYS_INPUT_CLKEN
);
724 /* Enable memdma clock */
725 writel(MEMDMAENABLE
, fei
->io
+ SYS_OTHER_CLKEN
);
727 /* clear internal sram */
728 memset_io(fei
->sram
, 0x0, fei
->sram_size
);
730 c8sectpfe_getconfig(fei
);
732 ret
= devm_request_irq(dev
, fei
->idle_irq
, c8sectpfe_idle_irq_handler
,
733 0, "c8sectpfe-idle-irq", fei
);
735 dev_err(dev
, "Can't register c8sectpfe-idle-irq IRQ.\n");
736 goto err_clk_disable
;
739 ret
= devm_request_irq(dev
, fei
->error_irq
,
740 c8sectpfe_error_irq_handler
, 0,
741 "c8sectpfe-error-irq", fei
);
743 dev_err(dev
, "Can't register c8sectpfe-error-irq IRQ.\n");
744 goto err_clk_disable
;
747 fei
->tsin_count
= of_get_child_count(np
);
749 if (fei
->tsin_count
> C8SECTPFE_MAX_TSIN_CHAN
||
750 fei
->tsin_count
> fei
->hw_stats
.num_ib
) {
752 dev_err(dev
, "More tsin declared than exist on SoC!\n");
754 goto err_clk_disable
;
757 fei
->pinctrl
= devm_pinctrl_get(dev
);
759 if (IS_ERR(fei
->pinctrl
)) {
760 dev_err(dev
, "Error getting tsin pins\n");
761 ret
= PTR_ERR(fei
->pinctrl
);
762 goto err_clk_disable
;
765 for_each_child_of_node(np
, child
) {
766 struct device_node
*i2c_bus
;
768 fei
->channel_data
[index
] = devm_kzalloc(dev
,
769 sizeof(struct channel_info
),
772 if (!fei
->channel_data
[index
]) {
774 goto err_clk_disable
;
777 tsin
= fei
->channel_data
[index
];
781 ret
= of_property_read_u32(child
, "tsin-num", &tsin
->tsin_id
);
783 dev_err(&pdev
->dev
, "No tsin_num found\n");
784 goto err_clk_disable
;
787 /* sanity check value */
788 if (tsin
->tsin_id
> fei
->hw_stats
.num_ib
) {
790 "tsin-num %d specified greater than number\n\tof input block hw in SoC! (%d)",
791 tsin
->tsin_id
, fei
->hw_stats
.num_ib
);
793 goto err_clk_disable
;
796 tsin
->invert_ts_clk
= of_property_read_bool(child
,
799 tsin
->serial_not_parallel
= of_property_read_bool(child
,
800 "serial-not-parallel");
802 tsin
->async_not_sync
= of_property_read_bool(child
,
805 ret
= of_property_read_u32(child
, "dvb-card",
808 dev_err(&pdev
->dev
, "No dvb-card found\n");
809 goto err_clk_disable
;
812 i2c_bus
= of_parse_phandle(child
, "i2c-bus", 0);
814 dev_err(&pdev
->dev
, "No i2c-bus found\n");
816 goto err_clk_disable
;
819 of_find_i2c_adapter_by_node(i2c_bus
);
820 if (!tsin
->i2c_adapter
) {
821 dev_err(&pdev
->dev
, "No i2c adapter found\n");
822 of_node_put(i2c_bus
);
824 goto err_clk_disable
;
826 of_node_put(i2c_bus
);
828 tsin
->rst_gpio
= of_get_named_gpio(child
, "reset-gpios", 0);
830 ret
= gpio_is_valid(tsin
->rst_gpio
);
833 "reset gpio for tsin%d not valid (gpio=%d)\n",
834 tsin
->tsin_id
, tsin
->rst_gpio
);
835 goto err_clk_disable
;
838 ret
= devm_gpio_request_one(dev
, tsin
->rst_gpio
,
839 GPIOF_OUT_INIT_LOW
, "NIM reset");
840 if (ret
&& ret
!= -EBUSY
) {
841 dev_err(dev
, "Can't request tsin%d reset gpio\n"
842 , fei
->channel_data
[index
]->tsin_id
);
843 goto err_clk_disable
;
847 /* toggle reset lines */
848 gpio_direction_output(tsin
->rst_gpio
, 0);
849 usleep_range(3500, 5000);
850 gpio_direction_output(tsin
->rst_gpio
, 1);
851 usleep_range(3000, 5000);
854 tsin
->demux_mapping
= index
;
857 "channel=%p n=%d tsin_num=%d, invert-ts-clk=%d\n\tserial-not-parallel=%d pkt-clk-valid=%d dvb-card=%d\n",
858 fei
->channel_data
[index
], index
,
859 tsin
->tsin_id
, tsin
->invert_ts_clk
,
860 tsin
->serial_not_parallel
, tsin
->async_not_sync
,
866 /* Setup timer interrupt */
867 timer_setup(&fei
->timer
, c8sectpfe_timer_interrupt
, 0);
869 mutex_init(&fei
->lock
);
871 /* Get the configuration information about the tuners */
872 ret
= c8sectpfe_tuner_register_frontend(&fei
->c8sectpfe
[0],
874 c8sectpfe_start_feed
,
875 c8sectpfe_stop_feed
);
877 dev_err(dev
, "c8sectpfe_tuner_register_frontend failed (%d)\n",
879 goto err_clk_disable
;
882 c8sectpfe_debugfs_init(fei
);
887 clk_disable_unprepare(fei
->c8sectpfeclk
);
891 static int c8sectpfe_remove(struct platform_device
*pdev
)
893 struct c8sectpfei
*fei
= platform_get_drvdata(pdev
);
894 struct channel_info
*channel
;
897 wait_for_completion(&fei
->fw_ack
);
899 c8sectpfe_tuner_unregister_frontend(fei
->c8sectpfe
[0], fei
);
902 * Now loop through and un-configure each of the InputBlock resources
904 for (i
= 0; i
< fei
->tsin_count
; i
++) {
905 channel
= fei
->channel_data
[i
];
906 free_input_block(fei
, channel
);
909 c8sectpfe_debugfs_exit(fei
);
911 dev_info(fei
->dev
, "Stopping memdma SLIM core\n");
912 if (readl(fei
->io
+ DMA_CPU_RUN
))
913 writel(0x0, fei
->io
+ DMA_CPU_RUN
);
915 /* unclock all internal IP's */
916 if (readl(fei
->io
+ SYS_INPUT_CLKEN
))
917 writel(0, fei
->io
+ SYS_INPUT_CLKEN
);
919 if (readl(fei
->io
+ SYS_OTHER_CLKEN
))
920 writel(0, fei
->io
+ SYS_OTHER_CLKEN
);
922 if (fei
->c8sectpfeclk
)
923 clk_disable_unprepare(fei
->c8sectpfeclk
);
929 static int configure_channels(struct c8sectpfei
*fei
)
932 struct channel_info
*tsin
;
933 struct device_node
*child
, *np
= fei
->dev
->of_node
;
935 /* iterate round each tsin and configure memdma descriptor and IB hw */
936 for_each_child_of_node(np
, child
) {
938 tsin
= fei
->channel_data
[index
];
940 ret
= configure_memdma_and_inputblock(fei
,
941 fei
->channel_data
[index
]);
945 "configure_memdma_and_inputblock failed\n");
954 for (index
= 0; index
< fei
->tsin_count
; index
++) {
955 tsin
= fei
->channel_data
[index
];
956 free_input_block(fei
, tsin
);
962 c8sectpfe_elf_sanity_check(struct c8sectpfei
*fei
, const struct firmware
*fw
)
964 struct elf32_hdr
*ehdr
;
968 dev_err(fei
->dev
, "failed to load %s\n", FIRMWARE_MEMDMA
);
972 if (fw
->size
< sizeof(struct elf32_hdr
)) {
973 dev_err(fei
->dev
, "Image is too small\n");
977 ehdr
= (struct elf32_hdr
*)fw
->data
;
979 /* We only support ELF32 at this point */
980 class = ehdr
->e_ident
[EI_CLASS
];
981 if (class != ELFCLASS32
) {
982 dev_err(fei
->dev
, "Unsupported class: %d\n", class);
986 if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
987 dev_err(fei
->dev
, "Unsupported firmware endianness\n");
991 if (fw
->size
< ehdr
->e_shoff
+ sizeof(struct elf32_shdr
)) {
992 dev_err(fei
->dev
, "Image is too small\n");
996 if (memcmp(ehdr
->e_ident
, ELFMAG
, SELFMAG
)) {
997 dev_err(fei
->dev
, "Image is corrupted (bad magic)\n");
1001 /* Check ELF magic */
1002 ehdr
= (Elf32_Ehdr
*)fw
->data
;
1003 if (ehdr
->e_ident
[EI_MAG0
] != ELFMAG0
||
1004 ehdr
->e_ident
[EI_MAG1
] != ELFMAG1
||
1005 ehdr
->e_ident
[EI_MAG2
] != ELFMAG2
||
1006 ehdr
->e_ident
[EI_MAG3
] != ELFMAG3
) {
1007 dev_err(fei
->dev
, "Invalid ELF magic\n");
1011 if (ehdr
->e_type
!= ET_EXEC
) {
1012 dev_err(fei
->dev
, "Unsupported ELF header type\n");
1016 if (ehdr
->e_phoff
> fw
->size
) {
1017 dev_err(fei
->dev
, "Firmware size is too small\n");
1025 static void load_imem_segment(struct c8sectpfei
*fei
, Elf32_Phdr
*phdr
,
1026 const struct firmware
*fw
, u8 __iomem
*dest
,
1029 const u8
*imem_src
= fw
->data
+ phdr
->p_offset
;
1033 * For IMEM segments, the segment contains 24-bit
1034 * instructions which must be padded to 32-bit
1035 * instructions before being written. The written
1036 * segment is padded with NOP instructions.
1040 "Loading IMEM segment %d 0x%08x\n\t (0x%x bytes) -> 0x%p (0x%x bytes)\n",
1042 phdr
->p_paddr
, phdr
->p_filesz
,
1043 dest
, phdr
->p_memsz
+ phdr
->p_memsz
/ 3);
1045 for (i
= 0; i
< phdr
->p_filesz
; i
++) {
1047 writeb(readb((void __iomem
*)imem_src
), (void __iomem
*)dest
);
1049 /* Every 3 bytes, add an additional
1050 * padding zero in destination */
1053 writeb(0x00, (void __iomem
*)dest
);
1061 static void load_dmem_segment(struct c8sectpfei
*fei
, Elf32_Phdr
*phdr
,
1062 const struct firmware
*fw
, u8 __iomem
*dst
, int seg_num
)
1065 * For DMEM segments copy the segment data from the ELF
1066 * file and pad segment with zeroes
1070 "Loading DMEM segment %d 0x%08x\n\t(0x%x bytes) -> 0x%p (0x%x bytes)\n",
1071 seg_num
, phdr
->p_paddr
, phdr
->p_filesz
,
1072 dst
, phdr
->p_memsz
);
1074 memcpy((void __force
*)dst
, (void *)fw
->data
+ phdr
->p_offset
,
1077 memset((void __force
*)dst
+ phdr
->p_filesz
, 0,
1078 phdr
->p_memsz
- phdr
->p_filesz
);
1081 static int load_slim_core_fw(const struct firmware
*fw
, struct c8sectpfei
*fei
)
1091 ehdr
= (Elf32_Ehdr
*)fw
->data
;
1092 phdr
= (Elf32_Phdr
*)(fw
->data
+ ehdr
->e_phoff
);
1094 /* go through the available ELF segments */
1095 for (i
= 0; i
< ehdr
->e_phnum
; i
++, phdr
++) {
1097 /* Only consider LOAD segments */
1098 if (phdr
->p_type
!= PT_LOAD
)
1102 * Check segment is contained within the fw->data buffer
1104 if (phdr
->p_offset
+ phdr
->p_filesz
> fw
->size
) {
1106 "Segment %d is outside of firmware file\n", i
);
1112 * MEMDMA IMEM has executable flag set, otherwise load
1113 * this segment into DMEM.
1117 if (phdr
->p_flags
& PF_X
) {
1118 dst
= (u8 __iomem
*) fei
->io
+ DMA_MEMDMA_IMEM
;
1120 * The Slim ELF file uses 32-bit word addressing for
1123 dst
+= (phdr
->p_paddr
& 0xFFFFF) * sizeof(unsigned int);
1124 load_imem_segment(fei
, phdr
, fw
, dst
, i
);
1126 dst
= (u8 __iomem
*) fei
->io
+ DMA_MEMDMA_DMEM
;
1128 * The Slim ELF file uses 32-bit word addressing for
1131 dst
+= (phdr
->p_paddr
& 0xFFFFF) * sizeof(unsigned int);
1132 load_dmem_segment(fei
, phdr
, fw
, dst
, i
);
1136 release_firmware(fw
);
1140 static int load_c8sectpfe_fw(struct c8sectpfei
*fei
)
1142 const struct firmware
*fw
;
1145 dev_info(fei
->dev
, "Loading firmware: %s\n", FIRMWARE_MEMDMA
);
1147 err
= request_firmware(&fw
, FIRMWARE_MEMDMA
, fei
->dev
);
1151 err
= c8sectpfe_elf_sanity_check(fei
, fw
);
1153 dev_err(fei
->dev
, "c8sectpfe_elf_sanity_check failed err=(%d)\n"
1155 release_firmware(fw
);
1159 err
= load_slim_core_fw(fw
, fei
);
1161 dev_err(fei
->dev
, "load_slim_core_fw failed err=(%d)\n", err
);
1165 /* now the firmware is loaded configure the input blocks */
1166 err
= configure_channels(fei
);
1168 dev_err(fei
->dev
, "configure_channels failed err=(%d)\n", err
);
1173 * STBus target port can access IMEM and DMEM ports
1174 * without waiting for CPU
1176 writel(0x1, fei
->io
+ DMA_PER_STBUS_SYNC
);
1178 dev_info(fei
->dev
, "Boot the memdma SLIM core\n");
1179 writel(0x1, fei
->io
+ DMA_CPU_RUN
);
1181 atomic_set(&fei
->fw_loaded
, 1);
1186 static const struct of_device_id c8sectpfe_match
[] = {
1187 { .compatible
= "st,stih407-c8sectpfe" },
1190 MODULE_DEVICE_TABLE(of
, c8sectpfe_match
);
1192 static struct platform_driver c8sectpfe_driver
= {
1194 .name
= "c8sectpfe",
1195 .of_match_table
= of_match_ptr(c8sectpfe_match
),
1197 .probe
= c8sectpfe_probe
,
1198 .remove
= c8sectpfe_remove
,
1201 module_platform_driver(c8sectpfe_driver
);
1203 MODULE_AUTHOR("Peter Bennett <peter.bennett@st.com>");
1204 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
1205 MODULE_DESCRIPTION("C8SECTPFE STi DVB Driver");
1206 MODULE_LICENSE("GPL");