]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/dvb/b2c2/flexcop-pci.c
[PATCH] dvb: flexcop: add big endian register definitions
[mirror_ubuntu-artful-kernel.git] / drivers / media / dvb / b2c2 / flexcop-pci.c
CommitLineData
2add87a9
JS
1/*
2 * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
3 *
4 * flexcop-pci.c - covers the PCI part including DMA transfers.
5 *
6 * see flexcop.c for copyright information.
7 */
8
9#define FC_LOG_PREFIX "flexcop-pci"
10#include "flexcop-common.h"
11
c4ee3fd4 12static int enable_pid_filtering = 1;
2add87a9
JS
13module_param(enable_pid_filtering, int, 0444);
14MODULE_PARM_DESC(enable_pid_filtering, "enable hardware pid filtering: supported values: 0 (fullts), 1");
15
16#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
17#define dprintk(level,args...) \
18 do { if ((debug & level)) printk(args); } while (0)
19#define DEBSTATUS ""
20#else
21#define dprintk(level,args...)
22#define DEBSTATUS " (debugging is not enabled)"
23#endif
24
25#define deb_info(args...) dprintk(0x01,args)
26#define deb_reg(args...) dprintk(0x02,args)
27#define deb_ts(args...) dprintk(0x04,args)
28#define deb_irq(args...) dprintk(0x08,args)
29
30static int debug = 0;
31module_param(debug, int, 0644);
32MODULE_PARM_DESC(debug, "set debug level (1=info,2=regs,4=TS,8=irqdma (|-able))." DEBSTATUS);
33
34#define DRIVER_VERSION "0.1"
35#define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV PCI Driver"
36#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>"
37
38struct flexcop_pci {
39 struct pci_dev *pdev;
40
41#define FC_PCI_INIT 0x01
42#define FC_PCI_DMA_INIT 0x02
43 int init_state;
44
45 void __iomem *io_mem;
46 u32 irq;
47/* buffersize (at least for DMA1, need to be % 188 == 0,
c4ee3fd4 48 * this logic is required */
2add87a9
JS
49#define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
50#define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
51 struct flexcop_dma dma[2];
52
53 int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
54 u32 last_dma1_cur_pos; /* position of the pointer last time the timer/packet irq occured */
c4ee3fd4 55 int count;
2add87a9 56
4853f16a
JS
57 spinlock_t irq_lock;
58
2add87a9
JS
59 struct flexcop_device *fc_dev;
60};
61
62static int lastwreg,lastwval,lastrreg,lastrval;
63
64static flexcop_ibi_value flexcop_pci_read_ibi_reg (struct flexcop_device *fc, flexcop_ibi_register r)
65{
66 struct flexcop_pci *fc_pci = fc->bus_specific;
67 flexcop_ibi_value v;
68 v.raw = readl(fc_pci->io_mem + r);
69
70 if (lastrreg != r || lastrval != v.raw) {
71 lastrreg = r; lastrval = v.raw;
72 deb_reg("new rd: %3x: %08x\n",r,v.raw);
73 }
74
75 return v;
76}
77
78static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register r, flexcop_ibi_value v)
79{
80 struct flexcop_pci *fc_pci = fc->bus_specific;
81
82 if (lastwreg != r || lastwval != v.raw) {
83 lastwreg = r; lastwval = v.raw;
84 deb_reg("new wr: %3x: %08x\n",r,v.raw);
85 }
86
87 writel(v.raw, fc_pci->io_mem + r);
88 return 0;
89}
90
91/* When PID filtering is turned on, we use the timer IRQ, because small amounts
92 * of data need to be passed to the user space instantly as well. When PID
93 * filtering is turned off, we use the page-change-IRQ */
94static irqreturn_t flexcop_pci_irq(int irq, void *dev_id, struct pt_regs *regs)
95{
96 struct flexcop_pci *fc_pci = dev_id;
97 struct flexcop_device *fc = fc_pci->fc_dev;
98 flexcop_ibi_value v = fc->read_ibi_reg(fc,irq_20c);
4853f16a
JS
99 irqreturn_t ret = IRQ_HANDLED;
100
101 spin_lock_irq(&fc_pci->irq_lock);
2add87a9 102
2add87a9
JS
103 if (v.irq_20c.DMA1_IRQ_Status == 1) {
104 if (fc_pci->active_dma1_addr == 0)
105 flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr0,fc_pci->dma[0].size / 188);
106 else
107 flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr1,fc_pci->dma[0].size / 188);
108
109 deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
110 fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
111 } else if (v.irq_20c.DMA1_Timer_Status == 1) {
112 /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
113 * complete TS packets when reading from the DMA memory */
114 dma_addr_t cur_addr =
115 fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
116 u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
117
c4ee3fd4
JS
118 deb_irq("irq: %08x cur_addr: %08x: cur_pos: %08x, last_cur_pos: %08x ",
119 v.raw,cur_addr,cur_pos,fc_pci->last_dma1_cur_pos);
120
2add87a9
JS
121 /* buffer end was reached, restarted from the beginning
122 * pass the data from last_cur_pos to the buffer end to the demux
123 */
124 if (cur_pos < fc_pci->last_dma1_cur_pos) {
c4ee3fd4 125 deb_irq(" end was reached: passing %d bytes ",(fc_pci->dma[0].size*2 - 1) - fc_pci->last_dma1_cur_pos);
2add87a9
JS
126 flexcop_pass_dmx_data(fc_pci->fc_dev,
127 fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
c4ee3fd4 128 (fc_pci->dma[0].size*2) - fc_pci->last_dma1_cur_pos);
2add87a9 129 fc_pci->last_dma1_cur_pos = 0;
c4ee3fd4 130 fc_pci->count = 0;
2add87a9
JS
131 }
132
133 if (cur_pos > fc_pci->last_dma1_cur_pos) {
c4ee3fd4 134 deb_irq(" passing %d bytes ",cur_pos - fc_pci->last_dma1_cur_pos);
2add87a9
JS
135 flexcop_pass_dmx_data(fc_pci->fc_dev,
136 fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
137 cur_pos - fc_pci->last_dma1_cur_pos);
138 }
c4ee3fd4 139 deb_irq("\n");
2add87a9
JS
140
141 fc_pci->last_dma1_cur_pos = cur_pos;
4853f16a
JS
142 } else
143 ret = IRQ_NONE;
144
145 spin_unlock_irq(&fc_pci->irq_lock);
2add87a9
JS
146
147/* packet count would be ideal for hw filtering, but it isn't working. Either
148 * the data book is wrong, or I'm unable to read it correctly */
149
150/* if (v.irq_20c.DMA1_Size_IRQ_Status == 1) { packet counter */
151
4853f16a 152 return ret;
2add87a9
JS
153}
154
155static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
156{
157 struct flexcop_pci *fc_pci = fc->bus_specific;
158 if (onoff) {
159 flexcop_dma_config(fc,&fc_pci->dma[0],FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1);
160 flexcop_dma_config(fc,&fc_pci->dma[1],FC_DMA_2,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1);
161 flexcop_dma_config_timer(fc,FC_DMA_1,1);
162
163 if (fc_pci->fc_dev->pid_filtering) {
164 fc_pci->last_dma1_cur_pos = 0;
165 flexcop_dma_control_timer_irq(fc,FC_DMA_1,1);
166 } else {
167 fc_pci->active_dma1_addr = 0;
168 flexcop_dma_control_size_irq(fc,FC_DMA_1,1);
169 }
170
171/* flexcop_dma_config_packet_count(fc,FC_DMA_1,0xc0);
172 flexcop_dma_control_packet_irq(fc,FC_DMA_1,1); */
173
174 deb_irq("irqs enabled\n");
175 } else {
176 if (fc_pci->fc_dev->pid_filtering)
177 flexcop_dma_control_timer_irq(fc,FC_DMA_1,0);
178 else
179 flexcop_dma_control_size_irq(fc,FC_DMA_1,0);
180
181// flexcop_dma_control_packet_irq(fc,FC_DMA_1,0);
182 deb_irq("irqs disabled\n");
183 }
184
185 return 0;
186}
187
188static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
189{
190 int ret;
191 if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[0],FC_DEFAULT_DMA1_BUFSIZE)) != 0)
192 return ret;
193
194 if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[1],FC_DEFAULT_DMA2_BUFSIZE)) != 0)
195 goto dma1_free;
196
197 flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
198 flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
199
200 fc_pci->init_state |= FC_PCI_DMA_INIT;
201 goto success;
202dma1_free:
203 flexcop_dma_free(&fc_pci->dma[0]);
204
205success:
206 return ret;
207}
208
209static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
210{
211 if (fc_pci->init_state & FC_PCI_DMA_INIT) {
212 flexcop_dma_free(&fc_pci->dma[0]);
213 flexcop_dma_free(&fc_pci->dma[1]);
214 }
215 fc_pci->init_state &= ~FC_PCI_DMA_INIT;
216}
217
218static int flexcop_pci_init(struct flexcop_pci *fc_pci)
219{
220 int ret;
221 u8 card_rev;
222
223 pci_read_config_byte(fc_pci->pdev, PCI_CLASS_REVISION, &card_rev);
224 info("card revision %x", card_rev);
225
226 if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
227 return ret;
228
229 pci_set_master(fc_pci->pdev);
230
231 /* enable interrupts */
232 // pci_write_config_dword(pdev, 0x6c, 0x8000);
233
234 if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
235 goto err_pci_disable_device;
236
237 fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
238
239 if (!fc_pci->io_mem) {
240 err("cannot map io memory\n");
241 ret = -EIO;
242 goto err_pci_release_regions;
243 }
244
245 pci_set_drvdata(fc_pci->pdev, fc_pci);
246
247 if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_irq,
248 SA_SHIRQ, DRIVER_NAME, fc_pci)) != 0)
249 goto err_pci_iounmap;
250
4853f16a
JS
251 spin_lock_init(&fc_pci->irq_lock);
252
2add87a9
JS
253 fc_pci->init_state |= FC_PCI_INIT;
254 goto success;
255
256err_pci_iounmap:
257 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
258 pci_set_drvdata(fc_pci->pdev, NULL);
259err_pci_release_regions:
260 pci_release_regions(fc_pci->pdev);
261err_pci_disable_device:
262 pci_disable_device(fc_pci->pdev);
263
264success:
265 return ret;
266}
267
268static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
269{
270 if (fc_pci->init_state & FC_PCI_INIT) {
271 free_irq(fc_pci->pdev->irq, fc_pci);
272 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
273 pci_set_drvdata(fc_pci->pdev, NULL);
274 pci_release_regions(fc_pci->pdev);
275 pci_disable_device(fc_pci->pdev);
276 }
277 fc_pci->init_state &= ~FC_PCI_INIT;
278}
279
280
281static int flexcop_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
282{
283 struct flexcop_device *fc;
284 struct flexcop_pci *fc_pci;
285 int ret = -ENOMEM;
286
287 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
288 err("out of memory\n");
289 return -ENOMEM;
290 }
291
292/* general flexcop init */
293 fc_pci = fc->bus_specific;
294 fc_pci->fc_dev = fc;
295
296 fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
297 fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
298 fc->i2c_request = flexcop_i2c_request;
299 fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
300
301 fc->stream_control = flexcop_pci_stream_control;
302
c4ee3fd4
JS
303 if (enable_pid_filtering)
304 info("will use the HW PID filter.");
305 else
306 info("will pass the complete TS to the demuxer.");
307
2add87a9
JS
308 fc->pid_filtering = enable_pid_filtering;
309 fc->bus_type = FC_PCI;
310
311 fc->dev = &pdev->dev;
59a7ad6c 312 fc->owner = THIS_MODULE;
2add87a9
JS
313
314/* bus specific part */
315 fc_pci->pdev = pdev;
316 if ((ret = flexcop_pci_init(fc_pci)) != 0)
317 goto err_kfree;
318
319/* init flexcop */
320 if ((ret = flexcop_device_initialize(fc)) != 0)
321 goto err_pci_exit;
322
323/* init dma */
324 if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
325 goto err_fc_exit;
326
327 goto success;
328err_fc_exit:
329 flexcop_device_exit(fc);
330err_pci_exit:
331 flexcop_pci_exit(fc_pci);
332err_kfree:
333 flexcop_device_kfree(fc);
334success:
335 return ret;
336}
337
338/* in theory every _exit function should be called exactly two times,
339 * here and in the bail-out-part of the _init-function
340 */
341static void flexcop_pci_remove(struct pci_dev *pdev)
342{
343 struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
344
345 flexcop_pci_dma_exit(fc_pci);
346 flexcop_device_exit(fc_pci->fc_dev);
347 flexcop_pci_exit(fc_pci);
348 flexcop_device_kfree(fc_pci->fc_dev);
349}
350
351static struct pci_device_id flexcop_pci_tbl[] = {
352 { PCI_DEVICE(0x13d0, 0x2103) },
353/* { PCI_DEVICE(0x13d0, 0x2200) }, PCI FlexCopIII ? */
354 { },
355};
356
357MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
358
359static struct pci_driver flexcop_pci_driver = {
360 .name = "Technisat/B2C2 FlexCop II/IIb/III PCI",
361 .id_table = flexcop_pci_tbl,
362 .probe = flexcop_pci_probe,
363 .remove = flexcop_pci_remove,
364};
365
366static int __init flexcop_pci_module_init(void)
367{
368 return pci_register_driver(&flexcop_pci_driver);
369}
370
371static void __exit flexcop_pci_module_exit(void)
372{
373 pci_unregister_driver(&flexcop_pci_driver);
374}
375
376module_init(flexcop_pci_module_init);
377module_exit(flexcop_pci_module_exit);
378
379MODULE_AUTHOR(DRIVER_AUTHOR);
380MODULE_DESCRIPTION(DRIVER_NAME);
381MODULE_LICENSE("GPL");