perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / sti / c8sectpfe / c8sectpfe-core.c
blob3c05b3dc49ec0878912074754d8d772e1a933101
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
23 #include <linux/io.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
47 #define POLL_MSECS 50
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)
59 #define FIFO_LEN 1024
61 static void c8sectpfe_timer_interrupt(struct timer_list *t)
63 struct c8sectpfei *fei = from_timer(fei, t, timer);
64 struct channel_info *channel;
65 int chan_num;
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;
84 unsigned long wp, rp;
85 int pos, num_packets, n, size;
86 u8 *buf;
88 if (unlikely(!channel || !channel->irec))
89 return;
91 fei = channel->fei;
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;
98 /* has it wrapped */
99 if (wp < rp)
100 wp = channel->back_buffer_busaddr + FEI_BUFFER_SIZE;
102 size = wp - rp;
103 num_packets = size / PACKET_SIZE;
105 /* manage cache so data is visible to CPU */
106 dma_sync_single_for_cpu(fei->dev,
108 size,
109 DMA_FROM_DEVICE);
111 buf = (u8 *) channel->back_buffer_aligned;
113 dev_dbg(fei->dev,
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(
119 &fei->c8sectpfe[0]->
120 demux[channel->demux_mapping].dvb_demux,
121 &buf[pos], 1);
123 pos += PACKET_SIZE;
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));
130 else
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;
140 u32 tmp;
141 unsigned long *bitmap;
142 int ret;
144 switch (dvbdmxfeed->type) {
145 case DMX_TYPE_TS:
146 break;
147 case DMX_TYPE_SEC:
148 break;
149 default:
150 dev_err(fei->dev, "%s:%d Error bailing\n"
151 , __func__, __LINE__);
152 return -EINVAL;
155 if (dvbdmxfeed->type == DMX_TYPE_TS) {
156 switch (dvbdmxfeed->pes_type) {
157 case DMX_PES_VIDEO:
158 case DMX_PES_AUDIO:
159 case DMX_PES_TELETEXT:
160 case DMX_PES_PCR:
161 case DMX_PES_OTHER:
162 break;
163 default:
164 dev_err(fei->dev, "%s:%d Error bailing\n"
165 , __func__, __LINE__);
166 return -EINVAL;
170 if (!atomic_read(&fei->fw_loaded)) {
171 ret = load_c8sectpfe_fw(fei);
172 if (ret)
173 return ret;
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));
188 } else {
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,
195 PID_TABLE_SIZE,
196 DMA_TO_DEVICE);
198 channel->active = 1;
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);
252 return 0;
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;
262 int idlereq;
263 u32 tmp;
264 int ret;
265 unsigned long *bitmap;
267 if (!atomic_read(&fei->fw_loaded)) {
268 ret = load_c8sectpfe_fw(fei);
269 if (ret)
270 return ret;
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));
283 } else {
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,
290 PID_TABLE_SIZE,
291 DMA_TO_DEVICE);
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));
315 if (ret == 0)
316 dev_warn(fei->dev,
317 "Timeout waiting for idle irq on tsin%d\n",
318 channel->tsin_id);
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));
333 dev_dbg(fei->dev,
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,
344 PID_TABLE_SIZE,
345 DMA_TO_DEVICE);
347 channel->active = 0;
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);
359 return 0;
362 static struct channel_info *find_channel(struct c8sectpfei *fei, int tsin_num)
364 int i;
366 for (i = 0; i < C8SECTPFE_MAX_TSIN_CHAN; i++) {
367 if (!fei->channel_data[i])
368 continue;
370 if (fei->channel_data[i]->tsin_id == tsin_num)
371 return fei->channel_data[i];
374 return NULL;
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"
393 , hw->num_swts);
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"
398 , hw->num_tp);
401 static irqreturn_t c8sectpfe_idle_irq_handler(int irq, void *priv)
403 struct c8sectpfei *fei = priv;
404 struct channel_info *chan;
405 int bit;
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);
416 if (chan)
417 complete(&chan->idle_completion);
420 writel(0, fei->io + DMA_IDLE_REQ);
422 return IRQ_HANDLED;
426 static void free_input_block(struct c8sectpfei *fei, struct channel_info *tsin)
428 if (!fei || !tsin)
429 return;
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);
446 #define MAX_NAME 20
448 static int configure_memdma_and_inputblock(struct c8sectpfei *fei,
449 struct channel_info *tsin)
451 int ret;
452 u32 tmp;
453 char tsin_pin_name[MAX_NAME];
455 if (!fei || !tsin)
456 return -EINVAL;
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) {
467 ret = -ENOMEM;
468 goto err_unmap;
471 /* Ensure backbuffer is 32byte aligned */
472 tsin->back_buffer_aligned = tsin->back_buffer_start
473 + FEI_ALIGNMENT;
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,
480 FEI_BUFFER_SIZE,
481 DMA_BIDIRECTIONAL);
483 if (dma_mapping_error(fei->dev, tsin->back_buffer_busaddr)) {
484 dev_err(fei->dev, "failed to map back_buffer\n");
485 ret = -EFAULT;
486 goto err_unmap;
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) {
497 ret = -ENOMEM;
498 goto err_unmap;
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
505 * the register.
508 tsin->pid_buffer_aligned = tsin->pid_buffer_start +
509 PID_TABLE_SIZE;
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,
516 PID_TABLE_SIZE,
517 DMA_BIDIRECTIONAL);
519 if (dma_mapping_error(fei->dev, tsin->pid_buffer_busaddr)) {
520 dev_err(fei->dev, "failed to map pid_bitmap\n");
521 ret = -EFAULT;
522 goto err_unmap;
525 /* manage cache so pid bitmap is visible to HW */
526 dma_sync_single_for_device(fei->dev,
527 tsin->pid_buffer_busaddr,
528 PID_TABLE_SIZE,
529 DMA_TO_DEVICE);
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);
539 goto err_unmap;
542 ret = pinctrl_select_state(fei->pinctrl, tsin->pstate);
544 if (ret) {
545 dev_err(fei->dev, "%s: pinctrl_select_state failed\n"
546 , __func__);
547 goto err_unmap;
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",
607 tsin->tsin_id,
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);
644 return 0;
646 err_unmap:
647 free_input_block(fei, tsin);
648 return ret;
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"
656 , __func__);
659 * TODO FIXME we should detect some error conditions here
660 * and ideally so something about them!
663 return IRQ_HANDLED;
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;
672 int ret, index = 0;
673 struct channel_info *tsin;
675 /* Allocate the c8sectpfei structure */
676 fei = devm_kzalloc(dev, sizeof(struct c8sectpfei), GFP_KERNEL);
677 if (!fei)
678 return -ENOMEM;
680 fei->dev = dev;
682 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "c8sectpfe");
683 fei->io = devm_ioremap_resource(dev, res);
684 if (IS_ERR(fei->io))
685 return PTR_ERR(fei->io);
687 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
688 "c8sectpfe-ram");
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);
716 if (ret) {
717 dev_err(dev, "Failed to enable c8sectpfe clock\n");
718 return ret;
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);
734 if (ret) {
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);
742 if (ret) {
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");
753 ret = -EINVAL;
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),
770 GFP_KERNEL);
772 if (!fei->channel_data[index]) {
773 ret = -ENOMEM;
774 goto err_clk_disable;
777 tsin = fei->channel_data[index];
779 tsin->fei = fei;
781 ret = of_property_read_u32(child, "tsin-num", &tsin->tsin_id);
782 if (ret) {
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) {
789 dev_err(&pdev->dev,
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);
792 ret = -EINVAL;
793 goto err_clk_disable;
796 tsin->invert_ts_clk = of_property_read_bool(child,
797 "invert-ts-clk");
799 tsin->serial_not_parallel = of_property_read_bool(child,
800 "serial-not-parallel");
802 tsin->async_not_sync = of_property_read_bool(child,
803 "async-not-sync");
805 ret = of_property_read_u32(child, "dvb-card",
806 &tsin->dvb_card);
807 if (ret) {
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);
813 if (!i2c_bus) {
814 dev_err(&pdev->dev, "No i2c-bus found\n");
815 ret = -ENODEV;
816 goto err_clk_disable;
818 tsin->i2c_adapter =
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);
823 ret = -ENODEV;
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);
831 if (!ret) {
832 dev_err(dev,
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;
846 if (!ret) {
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;
856 dev_dbg(fei->dev,
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,
861 tsin->dvb_card);
863 index++;
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],
873 (void *)fei,
874 c8sectpfe_start_feed,
875 c8sectpfe_stop_feed);
876 if (ret) {
877 dev_err(dev, "c8sectpfe_tuner_register_frontend failed (%d)\n",
878 ret);
879 goto err_clk_disable;
882 c8sectpfe_debugfs_init(fei);
884 return 0;
886 err_clk_disable:
887 clk_disable_unprepare(fei->c8sectpfeclk);
888 return ret;
891 static int c8sectpfe_remove(struct platform_device *pdev)
893 struct c8sectpfei *fei = platform_get_drvdata(pdev);
894 struct channel_info *channel;
895 int i;
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);
925 return 0;
929 static int configure_channels(struct c8sectpfei *fei)
931 int index = 0, ret;
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]);
943 if (ret) {
944 dev_err(fei->dev,
945 "configure_memdma_and_inputblock failed\n");
946 goto err_unmap;
948 index++;
951 return 0;
953 err_unmap:
954 for (index = 0; index < fei->tsin_count; index++) {
955 tsin = fei->channel_data[index];
956 free_input_block(fei, tsin);
958 return ret;
961 static int
962 c8sectpfe_elf_sanity_check(struct c8sectpfei *fei, const struct firmware *fw)
964 struct elf32_hdr *ehdr;
965 char class;
967 if (!fw) {
968 dev_err(fei->dev, "failed to load %s\n", FIRMWARE_MEMDMA);
969 return -EINVAL;
972 if (fw->size < sizeof(struct elf32_hdr)) {
973 dev_err(fei->dev, "Image is too small\n");
974 return -EINVAL;
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);
983 return -EINVAL;
986 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
987 dev_err(fei->dev, "Unsupported firmware endianness\n");
988 return -EINVAL;
991 if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
992 dev_err(fei->dev, "Image is too small\n");
993 return -EINVAL;
996 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
997 dev_err(fei->dev, "Image is corrupted (bad magic)\n");
998 return -EINVAL;
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");
1008 return -EINVAL;
1011 if (ehdr->e_type != ET_EXEC) {
1012 dev_err(fei->dev, "Unsupported ELF header type\n");
1013 return -EINVAL;
1016 if (ehdr->e_phoff > fw->size) {
1017 dev_err(fei->dev, "Firmware size is too small\n");
1018 return -EINVAL;
1021 return 0;
1025 static void load_imem_segment(struct c8sectpfei *fei, Elf32_Phdr *phdr,
1026 const struct firmware *fw, u8 __iomem *dest,
1027 int seg_num)
1029 const u8 *imem_src = fw->data + phdr->p_offset;
1030 int i;
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.
1039 dev_dbg(fei->dev,
1040 "Loading IMEM segment %d 0x%08x\n\t (0x%x bytes) -> 0x%p (0x%x bytes)\n",
1041 seg_num,
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 */
1051 if (i % 3 == 2) {
1052 dest++;
1053 writeb(0x00, (void __iomem *)dest);
1056 dest++;
1057 imem_src++;
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
1069 dev_dbg(fei->dev,
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,
1075 phdr->p_filesz);
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)
1083 Elf32_Ehdr *ehdr;
1084 Elf32_Phdr *phdr;
1085 u8 __iomem *dst;
1086 int err = 0, i;
1088 if (!fw || !fei)
1089 return -EINVAL;
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)
1099 continue;
1102 * Check segment is contained within the fw->data buffer
1104 if (phdr->p_offset + phdr->p_filesz > fw->size) {
1105 dev_err(fei->dev,
1106 "Segment %d is outside of firmware file\n", i);
1107 err = -EINVAL;
1108 break;
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
1121 * load offsets.
1123 dst += (phdr->p_paddr & 0xFFFFF) * sizeof(unsigned int);
1124 load_imem_segment(fei, phdr, fw, dst, i);
1125 } else {
1126 dst = (u8 __iomem *) fei->io + DMA_MEMDMA_DMEM;
1128 * The Slim ELF file uses 32-bit word addressing for
1129 * load offsets.
1131 dst += (phdr->p_paddr & 0xFFFFF) * sizeof(unsigned int);
1132 load_dmem_segment(fei, phdr, fw, dst, i);
1136 release_firmware(fw);
1137 return err;
1140 static int load_c8sectpfe_fw(struct c8sectpfei *fei)
1142 const struct firmware *fw;
1143 int err;
1145 dev_info(fei->dev, "Loading firmware: %s\n", FIRMWARE_MEMDMA);
1147 err = request_firmware(&fw, FIRMWARE_MEMDMA, fei->dev);
1148 if (err)
1149 return err;
1151 err = c8sectpfe_elf_sanity_check(fei, fw);
1152 if (err) {
1153 dev_err(fei->dev, "c8sectpfe_elf_sanity_check failed err=(%d)\n"
1154 , err);
1155 release_firmware(fw);
1156 return err;
1159 err = load_slim_core_fw(fw, fei);
1160 if (err) {
1161 dev_err(fei->dev, "load_slim_core_fw failed err=(%d)\n", err);
1162 return err;
1165 /* now the firmware is loaded configure the input blocks */
1166 err = configure_channels(fei);
1167 if (err) {
1168 dev_err(fei->dev, "configure_channels failed err=(%d)\n", err);
1169 return 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);
1183 return 0;
1186 static const struct of_device_id c8sectpfe_match[] = {
1187 { .compatible = "st,stih407-c8sectpfe" },
1188 { /* sentinel */ },
1190 MODULE_DEVICE_TABLE(of, c8sectpfe_match);
1192 static struct platform_driver c8sectpfe_driver = {
1193 .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");