18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci Mantis PCI bridge driver 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci Copyright (C) Manu Abraham (abraham.manu@gmail.com) 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci*/ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <asm/page.h> 118c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 128c2ecf20Sopenharmony_ci#include <linux/pci.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <asm/irq.h> 158c2ecf20Sopenharmony_ci#include <linux/signal.h> 168c2ecf20Sopenharmony_ci#include <linux/sched.h> 178c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <media/dmxdev.h> 208c2ecf20Sopenharmony_ci#include <media/dvbdev.h> 218c2ecf20Sopenharmony_ci#include <media/dvb_demux.h> 228c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h> 238c2ecf20Sopenharmony_ci#include <media/dvb_net.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include "mantis_common.h" 268c2ecf20Sopenharmony_ci#include "mantis_reg.h" 278c2ecf20Sopenharmony_ci#include "mantis_dma.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define RISC_WRITE (0x01 << 28) 308c2ecf20Sopenharmony_ci#define RISC_JUMP (0x07 << 28) 318c2ecf20Sopenharmony_ci#define RISC_IRQ (0x01 << 24) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define RISC_STATUS(status) ((((~status) & 0x0f) << 20) | ((status & 0x0f) << 16)) 348c2ecf20Sopenharmony_ci#define RISC_FLUSH(risc_pos) (risc_pos = 0) 358c2ecf20Sopenharmony_ci#define RISC_INSTR(risc_pos, opcode) (mantis->risc_cpu[risc_pos++] = cpu_to_le32(opcode)) 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define MANTIS_BUF_SIZE (64 * 1024) 388c2ecf20Sopenharmony_ci#define MANTIS_BLOCK_BYTES (MANTIS_BUF_SIZE / 4) 398c2ecf20Sopenharmony_ci#define MANTIS_DMA_TR_BYTES (2 * 1024) /* upper limit: 4095 bytes. */ 408c2ecf20Sopenharmony_ci#define MANTIS_BLOCK_COUNT (MANTIS_BUF_SIZE / MANTIS_BLOCK_BYTES) 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define MANTIS_DMA_TR_UNITS (MANTIS_BLOCK_BYTES / MANTIS_DMA_TR_BYTES) 438c2ecf20Sopenharmony_ci/* MANTIS_BUF_SIZE / MANTIS_DMA_TR_UNITS must not exceed MANTIS_RISC_SIZE (4k RISC cmd buffer) */ 448c2ecf20Sopenharmony_ci#define MANTIS_RISC_SIZE PAGE_SIZE /* RISC program must fit here. */ 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ciint mantis_dma_exit(struct mantis_pci *mantis) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci if (mantis->buf_cpu) { 498c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 508c2ecf20Sopenharmony_ci "DMA=0x%lx cpu=0x%p size=%d", 518c2ecf20Sopenharmony_ci (unsigned long) mantis->buf_dma, 528c2ecf20Sopenharmony_ci mantis->buf_cpu, 538c2ecf20Sopenharmony_ci MANTIS_BUF_SIZE); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci pci_free_consistent(mantis->pdev, MANTIS_BUF_SIZE, 568c2ecf20Sopenharmony_ci mantis->buf_cpu, mantis->buf_dma); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci mantis->buf_cpu = NULL; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci if (mantis->risc_cpu) { 618c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 628c2ecf20Sopenharmony_ci "RISC=0x%lx cpu=0x%p size=%lx", 638c2ecf20Sopenharmony_ci (unsigned long) mantis->risc_dma, 648c2ecf20Sopenharmony_ci mantis->risc_cpu, 658c2ecf20Sopenharmony_ci MANTIS_RISC_SIZE); 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci pci_free_consistent(mantis->pdev, MANTIS_RISC_SIZE, 688c2ecf20Sopenharmony_ci mantis->risc_cpu, mantis->risc_dma); 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci mantis->risc_cpu = NULL; 718c2ecf20Sopenharmony_ci } 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci return 0; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mantis_dma_exit); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic inline int mantis_alloc_buffers(struct mantis_pci *mantis) 788c2ecf20Sopenharmony_ci{ 798c2ecf20Sopenharmony_ci if (!mantis->buf_cpu) { 808c2ecf20Sopenharmony_ci mantis->buf_cpu = pci_alloc_consistent(mantis->pdev, 818c2ecf20Sopenharmony_ci MANTIS_BUF_SIZE, 828c2ecf20Sopenharmony_ci &mantis->buf_dma); 838c2ecf20Sopenharmony_ci if (!mantis->buf_cpu) { 848c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 858c2ecf20Sopenharmony_ci "DMA buffer allocation failed"); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci goto err; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 908c2ecf20Sopenharmony_ci "DMA=0x%lx cpu=0x%p size=%d", 918c2ecf20Sopenharmony_ci (unsigned long) mantis->buf_dma, 928c2ecf20Sopenharmony_ci mantis->buf_cpu, MANTIS_BUF_SIZE); 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci if (!mantis->risc_cpu) { 958c2ecf20Sopenharmony_ci mantis->risc_cpu = pci_alloc_consistent(mantis->pdev, 968c2ecf20Sopenharmony_ci MANTIS_RISC_SIZE, 978c2ecf20Sopenharmony_ci &mantis->risc_dma); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci if (!mantis->risc_cpu) { 1008c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 1018c2ecf20Sopenharmony_ci "RISC program allocation failed"); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci mantis_dma_exit(mantis); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci goto err; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, 1088c2ecf20Sopenharmony_ci "RISC=0x%lx cpu=0x%p size=%lx", 1098c2ecf20Sopenharmony_ci (unsigned long) mantis->risc_dma, 1108c2ecf20Sopenharmony_ci mantis->risc_cpu, MANTIS_RISC_SIZE); 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci return 0; 1148c2ecf20Sopenharmony_cierr: 1158c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, "Out of memory (?) ....."); 1168c2ecf20Sopenharmony_ci return -ENOMEM; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciint mantis_dma_init(struct mantis_pci *mantis) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci int err; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "Mantis DMA init"); 1248c2ecf20Sopenharmony_ci err = mantis_alloc_buffers(mantis); 1258c2ecf20Sopenharmony_ci if (err < 0) { 1268c2ecf20Sopenharmony_ci dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer"); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci /* Stop RISC Engine */ 1298c2ecf20Sopenharmony_ci mmwrite(0, MANTIS_DMA_CTL); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci return err; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci return 0; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mantis_dma_init); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic inline void mantis_risc_program(struct mantis_pci *mantis) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci u32 buf_pos = 0; 1418c2ecf20Sopenharmony_ci u32 line, step; 1428c2ecf20Sopenharmony_ci u32 risc_pos; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program"); 1458c2ecf20Sopenharmony_ci RISC_FLUSH(risc_pos); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u, bytes per DMA tr %u", 1488c2ecf20Sopenharmony_ci MANTIS_BLOCK_COUNT, MANTIS_BLOCK_BYTES, MANTIS_DMA_TR_BYTES); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci for (line = 0; line < MANTIS_BLOCK_COUNT; line++) { 1518c2ecf20Sopenharmony_ci for (step = 0; step < MANTIS_DMA_TR_UNITS; step++) { 1528c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d], step=[%d]", line, step); 1538c2ecf20Sopenharmony_ci if (step == 0) { 1548c2ecf20Sopenharmony_ci RISC_INSTR(risc_pos, RISC_WRITE | 1558c2ecf20Sopenharmony_ci RISC_IRQ | 1568c2ecf20Sopenharmony_ci RISC_STATUS(line) | 1578c2ecf20Sopenharmony_ci MANTIS_DMA_TR_BYTES); 1588c2ecf20Sopenharmony_ci } else { 1598c2ecf20Sopenharmony_ci RISC_INSTR(risc_pos, RISC_WRITE | MANTIS_DMA_TR_BYTES); 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci RISC_INSTR(risc_pos, mantis->buf_dma + buf_pos); 1628c2ecf20Sopenharmony_ci buf_pos += MANTIS_DMA_TR_BYTES; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci RISC_INSTR(risc_pos, RISC_JUMP); 1668c2ecf20Sopenharmony_ci RISC_INSTR(risc_pos, mantis->risc_dma); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_civoid mantis_dma_start(struct mantis_pci *mantis) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine"); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci mantis_risc_program(mantis); 1748c2ecf20Sopenharmony_ci mmwrite(mantis->risc_dma, MANTIS_RISC_START); 1758c2ecf20Sopenharmony_ci mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci mmwrite(0, MANTIS_DMA_CTL); 1788c2ecf20Sopenharmony_ci mantis->last_block = mantis->busy_block = 0; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci mantis_unmask_ints(mantis, MANTIS_INT_RISCI); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN 1838c2ecf20Sopenharmony_ci | MANTIS_RISC_EN, MANTIS_DMA_CTL); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_civoid mantis_dma_stop(struct mantis_pci *mantis) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine"); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN | 1948c2ecf20Sopenharmony_ci MANTIS_DCAP_EN | 1958c2ecf20Sopenharmony_ci MANTIS_RISC_EN)), MANTIS_DMA_CTL); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci mantis_mask_ints(mantis, MANTIS_INT_RISCI | MANTIS_INT_RISCEN); 2008c2ecf20Sopenharmony_ci} 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_civoid mantis_dma_xfer(struct tasklet_struct *t) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci struct mantis_pci *mantis = from_tasklet(mantis, t, tasklet); 2068c2ecf20Sopenharmony_ci struct mantis_hwconfig *config = mantis->hwconfig; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci while (mantis->last_block != mantis->busy_block) { 2098c2ecf20Sopenharmony_ci dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]", 2108c2ecf20Sopenharmony_ci mantis->last_block, mantis->busy_block); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter) 2138c2ecf20Sopenharmony_ci (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES); 2148c2ecf20Sopenharmony_ci mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci} 217