blob: 2ce310b0a02289c45eff97875e2b53b791f753f4 [file] [log] [blame]
Manu Abraham41e840b2009-12-02 21:57:10 -03001/*
2 Mantis PCI bridge driver
3
Manu Abraham8825a092009-12-15 09:13:49 -03004 Copyright (C) Manu Abraham (abraham.manu@gmail.com)
Manu Abraham41e840b2009-12-02 21:57:10 -03005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
Manu Abrahamb3b96142009-12-04 05:41:11 -030021#include <linux/kernel.h>
Manu Abraham41e840b2009-12-02 21:57:10 -030022#include <asm/page.h>
23#include <linux/vmalloc.h>
Manu Abrahamb3b96142009-12-04 05:41:11 -030024#include <linux/pci.h>
25
26#include <asm/irq.h>
27#include <linux/signal.h>
28#include <linux/sched.h>
29#include <linux/interrupt.h>
30
31#include "dmxdev.h"
32#include "dvbdev.h"
33#include "dvb_demux.h"
34#include "dvb_frontend.h"
35#include "dvb_net.h"
36
Manu Abraham41e840b2009-12-02 21:57:10 -030037#include "mantis_common.h"
Manu Abrahamb3b96142009-12-04 05:41:11 -030038#include "mantis_reg.h"
39#include "mantis_dma.h"
Manu Abraham41e840b2009-12-02 21:57:10 -030040
41#define RISC_WRITE (0x01 << 28)
42#define RISC_JUMP (0x07 << 28)
43#define RISC_IRQ (0x01 << 24)
44
45#define RISC_STATUS(status) ((((~status) & 0x0f) << 20) | ((status & 0x0f) << 16))
Marko Ristola79d06d42010-08-07 08:16:15 -030046#define RISC_FLUSH(risc_pos) (risc_pos = 0)
47#define RISC_INSTR(risc_pos, opcode) (mantis->risc_cpu[risc_pos++] = cpu_to_le32(opcode))
Manu Abraham41e840b2009-12-02 21:57:10 -030048
Manu Abrahamf5ae4f62009-12-15 08:47:21 -030049#define MANTIS_BUF_SIZE (64 * 1024)
Marko Ristola79d06d42010-08-07 08:16:15 -030050#define MANTIS_BLOCK_BYTES (MANTIS_BUF_SIZE / 4)
51#define MANTIS_DMA_TR_BYTES (2 * 1024) /* upper limit: 4095 bytes. */
52#define MANTIS_BLOCK_COUNT (MANTIS_BUF_SIZE / MANTIS_BLOCK_BYTES)
53
54#define MANTIS_DMA_TR_UNITS (MANTIS_BLOCK_BYTES / MANTIS_DMA_TR_BYTES)
55/* MANTIS_BUF_SIZE / MANTIS_DMA_TR_UNITS must not exceed MANTIS_RISC_SIZE (4k RISC cmd buffer) */
56#define MANTIS_RISC_SIZE PAGE_SIZE /* RISC program must fit here. */
Manu Abraham41e840b2009-12-02 21:57:10 -030057
58int mantis_dma_exit(struct mantis_pci *mantis)
59{
60 if (mantis->buf_cpu) {
Manu Abrahamb3b96142009-12-04 05:41:11 -030061 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -030062 "DMA=0x%lx cpu=0x%p size=%d",
63 (unsigned long) mantis->buf_dma,
64 mantis->buf_cpu,
65 MANTIS_BUF_SIZE);
66
67 pci_free_consistent(mantis->pdev, MANTIS_BUF_SIZE,
68 mantis->buf_cpu, mantis->buf_dma);
69
70 mantis->buf_cpu = NULL;
71 }
72 if (mantis->risc_cpu) {
Manu Abrahamb3b96142009-12-04 05:41:11 -030073 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -030074 "RISC=0x%lx cpu=0x%p size=%lx",
75 (unsigned long) mantis->risc_dma,
76 mantis->risc_cpu,
77 MANTIS_RISC_SIZE);
78
79 pci_free_consistent(mantis->pdev, MANTIS_RISC_SIZE,
80 mantis->risc_cpu, mantis->risc_dma);
81
82 mantis->risc_cpu = NULL;
83 }
84
85 return 0;
86}
Manu Abrahamb3b96142009-12-04 05:41:11 -030087EXPORT_SYMBOL_GPL(mantis_dma_exit);
Manu Abraham41e840b2009-12-02 21:57:10 -030088
89static inline int mantis_alloc_buffers(struct mantis_pci *mantis)
90{
91 if (!mantis->buf_cpu) {
92 mantis->buf_cpu = pci_alloc_consistent(mantis->pdev,
93 MANTIS_BUF_SIZE,
94 &mantis->buf_dma);
95 if (!mantis->buf_cpu) {
Manu Abrahamb3b96142009-12-04 05:41:11 -030096 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -030097 "DMA buffer allocation failed");
98
99 goto err;
100 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300101 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -0300102 "DMA=0x%lx cpu=0x%p size=%d",
103 (unsigned long) mantis->buf_dma,
104 mantis->buf_cpu, MANTIS_BUF_SIZE);
105 }
106 if (!mantis->risc_cpu) {
107 mantis->risc_cpu = pci_alloc_consistent(mantis->pdev,
108 MANTIS_RISC_SIZE,
109 &mantis->risc_dma);
110
111 if (!mantis->risc_cpu) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300112 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -0300113 "RISC program allocation failed");
114
115 mantis_dma_exit(mantis);
116
117 goto err;
118 }
Manu Abrahamb3b96142009-12-04 05:41:11 -0300119 dprintk(MANTIS_ERROR, 1,
Manu Abraham41e840b2009-12-02 21:57:10 -0300120 "RISC=0x%lx cpu=0x%p size=%lx",
121 (unsigned long) mantis->risc_dma,
122 mantis->risc_cpu, MANTIS_RISC_SIZE);
123 }
124
125 return 0;
126err:
Manu Abrahamb3b96142009-12-04 05:41:11 -0300127 dprintk(MANTIS_ERROR, 1, "Out of memory (?) .....");
Manu Abraham41e840b2009-12-02 21:57:10 -0300128 return -ENOMEM;
129}
130
Manu Abraham41e840b2009-12-02 21:57:10 -0300131int mantis_dma_init(struct mantis_pci *mantis)
132{
Fabio Estevam919db5c2015-08-10 14:11:41 -0300133 int err;
Manu Abraham41e840b2009-12-02 21:57:10 -0300134
Manu Abrahamb3b96142009-12-04 05:41:11 -0300135 dprintk(MANTIS_DEBUG, 1, "Mantis DMA init");
Fabio Estevam919db5c2015-08-10 14:11:41 -0300136 err = mantis_alloc_buffers(mantis);
137 if (err < 0) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300138 dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer");
Manu Abraham41e840b2009-12-02 21:57:10 -0300139
Manu Abraham3e978a82009-12-04 05:56:35 -0300140 /* Stop RISC Engine */
Manu Abraham41e840b2009-12-02 21:57:10 -0300141 mmwrite(0, MANTIS_DMA_CTL);
142
Mauro Carvalho Chehabb0c49e22015-08-11 10:30:24 -0300143 return err;
Manu Abraham41e840b2009-12-02 21:57:10 -0300144 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300145
146 return 0;
Manu Abraham41e840b2009-12-02 21:57:10 -0300147}
Manu Abrahamb3b96142009-12-04 05:41:11 -0300148EXPORT_SYMBOL_GPL(mantis_dma_init);
Manu Abraham41e840b2009-12-02 21:57:10 -0300149
Manu Abraham41e840b2009-12-02 21:57:10 -0300150static inline void mantis_risc_program(struct mantis_pci *mantis)
151{
152 u32 buf_pos = 0;
Marko Ristola79d06d42010-08-07 08:16:15 -0300153 u32 line, step;
154 u32 risc_pos;
Manu Abraham41e840b2009-12-02 21:57:10 -0300155
Manu Abrahamb3b96142009-12-04 05:41:11 -0300156 dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program");
Marko Ristola79d06d42010-08-07 08:16:15 -0300157 RISC_FLUSH(risc_pos);
Manu Abraham41e840b2009-12-02 21:57:10 -0300158
Marko Ristola79d06d42010-08-07 08:16:15 -0300159 dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u, bytes per DMA tr %u",
160 MANTIS_BLOCK_COUNT, MANTIS_BLOCK_BYTES, MANTIS_DMA_TR_BYTES);
Manu Abraham41e840b2009-12-02 21:57:10 -0300161
Marko Ristola79d06d42010-08-07 08:16:15 -0300162 for (line = 0; line < MANTIS_BLOCK_COUNT; line++) {
163 for (step = 0; step < MANTIS_DMA_TR_UNITS; step++) {
164 dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d], step=[%d]", line, step);
165 if (step == 0) {
166 RISC_INSTR(risc_pos, RISC_WRITE |
167 RISC_IRQ |
168 RISC_STATUS(line) |
169 MANTIS_DMA_TR_BYTES);
170 } else {
171 RISC_INSTR(risc_pos, RISC_WRITE | MANTIS_DMA_TR_BYTES);
172 }
173 RISC_INSTR(risc_pos, mantis->buf_dma + buf_pos);
174 buf_pos += MANTIS_DMA_TR_BYTES;
175 }
Manu Abraham41e840b2009-12-02 21:57:10 -0300176 }
Marko Ristola79d06d42010-08-07 08:16:15 -0300177 RISC_INSTR(risc_pos, RISC_JUMP);
178 RISC_INSTR(risc_pos, mantis->risc_dma);
Manu Abraham41e840b2009-12-02 21:57:10 -0300179}
180
181void mantis_dma_start(struct mantis_pci *mantis)
182{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300183 dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine");
Manu Abraham41e840b2009-12-02 21:57:10 -0300184
185 mantis_risc_program(mantis);
David Woodhouse41703682009-12-03 05:47:11 -0300186 mmwrite(mantis->risc_dma, MANTIS_RISC_START);
Sigmund Augdala0c59062009-12-04 05:13:21 -0300187 mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
Manu Abraham41e840b2009-12-02 21:57:10 -0300188
189 mmwrite(0, MANTIS_DMA_CTL);
Marko Ristola79d06d42010-08-07 08:16:15 -0300190 mantis->last_block = mantis->busy_block = 0;
Manu Abraham41e840b2009-12-02 21:57:10 -0300191
Jan Klötzkea96762d2015-06-06 16:58:13 -0300192 mantis_unmask_ints(mantis, MANTIS_INT_RISCI);
Manu Abraham41e840b2009-12-02 21:57:10 -0300193
194 mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN
195 | MANTIS_RISC_EN, MANTIS_DMA_CTL);
196
197}
198
199void mantis_dma_stop(struct mantis_pci *mantis)
200{
Manu Abrahamb3b96142009-12-04 05:41:11 -0300201 dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine");
Manu Abraham41e840b2009-12-02 21:57:10 -0300202
Sigmund Augdala0c59062009-12-04 05:13:21 -0300203 mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR);
Marko Ristolae4deee02009-12-03 05:39:22 -0300204
Manu Abraham41e840b2009-12-02 21:57:10 -0300205 mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN |
206 MANTIS_DCAP_EN |
207 MANTIS_RISC_EN)), MANTIS_DMA_CTL);
208
209 mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT);
210
Jan Klötzkea96762d2015-06-06 16:58:13 -0300211 mantis_mask_ints(mantis, MANTIS_INT_RISCI | MANTIS_INT_RISCEN);
Manu Abraham41e840b2009-12-02 21:57:10 -0300212}
213
214
215void mantis_dma_xfer(unsigned long data)
216{
217 struct mantis_pci *mantis = (struct mantis_pci *) data;
Manu Abraham33c79632009-12-03 05:44:38 -0300218 struct mantis_hwconfig *config = mantis->hwconfig;
Manu Abraham41e840b2009-12-02 21:57:10 -0300219
Marko Ristola79d06d42010-08-07 08:16:15 -0300220 while (mantis->last_block != mantis->busy_block) {
Manu Abrahamb3b96142009-12-04 05:41:11 -0300221 dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]",
Marko Ristola79d06d42010-08-07 08:16:15 -0300222 mantis->last_block, mantis->busy_block);
Manu Abraham41e840b2009-12-02 21:57:10 -0300223
Manu Abrahamf5ae4f62009-12-15 08:47:21 -0300224 (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
Manu Abraham41e840b2009-12-02 21:57:10 -0300225 (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES);
226 mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT;
227 }
228}