blob: 6e166801505d23636014d2a120d479538933a2d7 [file] [log] [blame]
Johannes Stezenbach2add87a2005-05-16 21:54:10 -07001/*
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
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -070012static int enable_pid_filtering = 1;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070013module_param(enable_pid_filtering, int, 0444);
14MODULE_PARM_DESC(enable_pid_filtering, "enable hardware pid filtering: supported values: 0 (fullts), 1");
15
Patrick Boettcher64221be2005-07-07 17:57:49 -070016static int irq_chk_intv;
17module_param(irq_chk_intv, int, 0644);
18MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ watchdog (currently just debugging).");
19
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070020#ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
21#define dprintk(level,args...) \
22 do { if ((debug & level)) printk(args); } while (0)
23#define DEBSTATUS ""
24#else
25#define dprintk(level,args...)
26#define DEBSTATUS " (debugging is not enabled)"
27#endif
28
29#define deb_info(args...) dprintk(0x01,args)
30#define deb_reg(args...) dprintk(0x02,args)
31#define deb_ts(args...) dprintk(0x04,args)
32#define deb_irq(args...) dprintk(0x08,args)
Patrick Boettcher64221be2005-07-07 17:57:49 -070033#define deb_chk(args...) dprintk(0x10,args)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070034
35static int debug = 0;
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug, "set debug level (1=info,2=regs,4=TS,8=irqdma (|-able))." DEBSTATUS);
38
39#define DRIVER_VERSION "0.1"
40#define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV PCI Driver"
41#define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>"
42
43struct flexcop_pci {
44 struct pci_dev *pdev;
45
46#define FC_PCI_INIT 0x01
47#define FC_PCI_DMA_INIT 0x02
48 int init_state;
49
50 void __iomem *io_mem;
51 u32 irq;
52/* buffersize (at least for DMA1, need to be % 188 == 0,
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -070053 * this logic is required */
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070054#define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
55#define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
56 struct flexcop_dma dma[2];
57
58 int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
59 u32 last_dma1_cur_pos; /* position of the pointer last time the timer/packet irq occured */
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -070060 int count;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070061
Johannes Stezenbach4853f162005-05-16 21:54:15 -070062 spinlock_t irq_lock;
63
Patrick Boettcher64221be2005-07-07 17:57:49 -070064 unsigned long last_irq;
65
David Howellsc4028952006-11-22 14:57:56 +000066 struct delayed_work irq_check_work;
Patrick Boettcher64221be2005-07-07 17:57:49 -070067
Johannes Stezenbach2add87a2005-05-16 21:54:10 -070068 struct flexcop_device *fc_dev;
69};
70
71static int lastwreg,lastwval,lastrreg,lastrval;
72
73static flexcop_ibi_value flexcop_pci_read_ibi_reg (struct flexcop_device *fc, flexcop_ibi_register r)
74{
75 struct flexcop_pci *fc_pci = fc->bus_specific;
76 flexcop_ibi_value v;
77 v.raw = readl(fc_pci->io_mem + r);
78
79 if (lastrreg != r || lastrval != v.raw) {
80 lastrreg = r; lastrval = v.raw;
81 deb_reg("new rd: %3x: %08x\n",r,v.raw);
82 }
83
84 return v;
85}
86
87static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register r, flexcop_ibi_value v)
88{
89 struct flexcop_pci *fc_pci = fc->bus_specific;
90
91 if (lastwreg != r || lastwval != v.raw) {
92 lastwreg = r; lastwval = v.raw;
93 deb_reg("new wr: %3x: %08x\n",r,v.raw);
94 }
95
96 writel(v.raw, fc_pci->io_mem + r);
97 return 0;
98}
99
David Howellsc4028952006-11-22 14:57:56 +0000100static void flexcop_pci_irq_check_work(struct work_struct *work)
Patrick Boettcher64221be2005-07-07 17:57:49 -0700101{
David Howellsc4028952006-11-22 14:57:56 +0000102 struct flexcop_pci *fc_pci =
103 container_of(work, struct flexcop_pci, irq_check_work.work);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700104 struct flexcop_device *fc = fc_pci->fc_dev;
105
106 flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
107
108 flexcop_dump_reg(fc_pci->fc_dev,dma1_000,4);
109
110 if (v.sram_dest_reg_714.net_ovflow_error)
111 deb_chk("sram net_ovflow_error\n");
112 if (v.sram_dest_reg_714.media_ovflow_error)
113 deb_chk("sram media_ovflow_error\n");
114 if (v.sram_dest_reg_714.cai_ovflow_error)
115 deb_chk("sram cai_ovflow_error\n");
116 if (v.sram_dest_reg_714.cai_ovflow_error)
117 deb_chk("sram cai_ovflow_error\n");
118
119 schedule_delayed_work(&fc_pci->irq_check_work,
120 msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
121}
122
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700123/* When PID filtering is turned on, we use the timer IRQ, because small amounts
124 * of data need to be passed to the user space instantly as well. When PID
125 * filtering is turned off, we use the page-change-IRQ */
David Howells7d12e782006-10-05 14:55:46 +0100126static irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700127{
128 struct flexcop_pci *fc_pci = dev_id;
129 struct flexcop_device *fc = fc_pci->fc_dev;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700130 flexcop_ibi_value v;
Johannes Stezenbach4853f162005-05-16 21:54:15 -0700131 irqreturn_t ret = IRQ_HANDLED;
132
133 spin_lock_irq(&fc_pci->irq_lock);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700134
Patrick Boettcher64221be2005-07-07 17:57:49 -0700135 v = fc->read_ibi_reg(fc,irq_20c);
136
137 /* errors */
138 if (v.irq_20c.Data_receiver_error)
139 deb_chk("data receiver error\n");
140 if (v.irq_20c.Continuity_error_flag)
141 deb_chk("Contunuity error flag is set\n");
142 if (v.irq_20c.LLC_SNAP_FLAG_set)
143 deb_chk("LLC_SNAP_FLAG_set is set\n");
144 if (v.irq_20c.Transport_Error)
145 deb_chk("Transport error\n");
146
147 if ((fc_pci->count % 1000) == 0)
148 deb_chk("%d valid irq took place so far\n",fc_pci->count);
149
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700150 if (v.irq_20c.DMA1_IRQ_Status == 1) {
151 if (fc_pci->active_dma1_addr == 0)
152 flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr0,fc_pci->dma[0].size / 188);
153 else
154 flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr1,fc_pci->dma[0].size / 188);
155
156 deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
157 fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
158 } else if (v.irq_20c.DMA1_Timer_Status == 1) {
159 /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
160 * complete TS packets when reading from the DMA memory */
161 dma_addr_t cur_addr =
162 fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
163 u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
164
Randy Dunlap608268b2006-01-23 10:01:59 -0200165 deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, last_cur_pos: %08x ",
166 jiffies_to_usecs(jiffies - fc_pci->last_irq),
167 v.raw, (unsigned long long)cur_addr, cur_pos,
168 fc_pci->last_dma1_cur_pos);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700169 fc_pci->last_irq = jiffies;
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700170
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700171 /* buffer end was reached, restarted from the beginning
172 * pass the data from last_cur_pos to the buffer end to the demux
173 */
174 if (cur_pos < fc_pci->last_dma1_cur_pos) {
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700175 deb_irq(" end was reached: passing %d bytes ",(fc_pci->dma[0].size*2 - 1) - fc_pci->last_dma1_cur_pos);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700176 flexcop_pass_dmx_data(fc_pci->fc_dev,
177 fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700178 (fc_pci->dma[0].size*2) - fc_pci->last_dma1_cur_pos);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700179 fc_pci->last_dma1_cur_pos = 0;
180 }
181
182 if (cur_pos > fc_pci->last_dma1_cur_pos) {
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700183 deb_irq(" passing %d bytes ",cur_pos - fc_pci->last_dma1_cur_pos);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700184 flexcop_pass_dmx_data(fc_pci->fc_dev,
185 fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
186 cur_pos - fc_pci->last_dma1_cur_pos);
187 }
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700188 deb_irq("\n");
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700189
190 fc_pci->last_dma1_cur_pos = cur_pos;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700191 fc_pci->count++;
192 } else {
193 deb_irq("isr for flexcop called, apparently without reason (%08x)\n",v.raw);
Johannes Stezenbach4853f162005-05-16 21:54:15 -0700194 ret = IRQ_NONE;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700195 }
Johannes Stezenbach4853f162005-05-16 21:54:15 -0700196
197 spin_unlock_irq(&fc_pci->irq_lock);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700198
Johannes Stezenbach4853f162005-05-16 21:54:15 -0700199 return ret;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700200}
201
202static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
203{
204 struct flexcop_pci *fc_pci = fc->bus_specific;
205 if (onoff) {
Patrick Boettcher64221be2005-07-07 17:57:49 -0700206 flexcop_dma_config(fc,&fc_pci->dma[0],FC_DMA_1);
207 flexcop_dma_config(fc,&fc_pci->dma[1],FC_DMA_2);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700208
Patrick Boettcher64221be2005-07-07 17:57:49 -0700209 flexcop_dma_config_timer(fc,FC_DMA_1,0);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700210
Patrick Boettcher64221be2005-07-07 17:57:49 -0700211 flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,1);
212 deb_irq("DMA xfer enabled\n");
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700213
Patrick Boettcher64221be2005-07-07 17:57:49 -0700214 fc_pci->last_dma1_cur_pos = 0;
215 flexcop_dma_control_timer_irq(fc,FC_DMA_1,1);
216 deb_irq("IRQ enabled\n");
217
218// fc_pci->active_dma1_addr = 0;
219// flexcop_dma_control_size_irq(fc,FC_DMA_1,1);
220
221 if (irq_chk_intv > 0)
222 schedule_delayed_work(&fc_pci->irq_check_work,
223 msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700224 } else {
Patrick Boettcher64221be2005-07-07 17:57:49 -0700225 if (irq_chk_intv > 0)
226 cancel_delayed_work(&fc_pci->irq_check_work);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700227
Patrick Boettcher64221be2005-07-07 17:57:49 -0700228 flexcop_dma_control_timer_irq(fc,FC_DMA_1,0);
229 deb_irq("IRQ disabled\n");
230
231// flexcop_dma_control_size_irq(fc,FC_DMA_1,0);
232
233 flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,0);
234 deb_irq("DMA xfer disabled\n");
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700235 }
236
237 return 0;
238}
239
240static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
241{
242 int ret;
243 if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[0],FC_DEFAULT_DMA1_BUFSIZE)) != 0)
244 return ret;
245
Trent Piepho83977032006-05-12 20:36:24 -0300246 if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[1],FC_DEFAULT_DMA2_BUFSIZE)) != 0) {
247 flexcop_dma_free(&fc_pci->dma[0]);
248 return ret;
249 }
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700250
251 flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
252 flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
253
254 fc_pci->init_state |= FC_PCI_DMA_INIT;
Patrick Boettcher64221be2005-07-07 17:57:49 -0700255
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700256 return ret;
257}
258
259static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
260{
261 if (fc_pci->init_state & FC_PCI_DMA_INIT) {
262 flexcop_dma_free(&fc_pci->dma[0]);
263 flexcop_dma_free(&fc_pci->dma[1]);
264 }
265 fc_pci->init_state &= ~FC_PCI_DMA_INIT;
266}
267
268static int flexcop_pci_init(struct flexcop_pci *fc_pci)
269{
270 int ret;
271 u8 card_rev;
272
273 pci_read_config_byte(fc_pci->pdev, PCI_CLASS_REVISION, &card_rev);
274 info("card revision %x", card_rev);
275
276 if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
277 return ret;
278
279 pci_set_master(fc_pci->pdev);
280
281 /* enable interrupts */
282 // pci_write_config_dword(pdev, 0x6c, 0x8000);
283
284 if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
285 goto err_pci_disable_device;
286
287 fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
288
289 if (!fc_pci->io_mem) {
290 err("cannot map io memory\n");
291 ret = -EIO;
292 goto err_pci_release_regions;
293 }
294
295 pci_set_drvdata(fc_pci->pdev, fc_pci);
296
Patrick Boettcher64221be2005-07-07 17:57:49 -0700297 if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
Thomas Gleixner8076fe32006-07-01 19:29:37 -0700298 IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700299 goto err_pci_iounmap;
300
Johannes Stezenbach4853f162005-05-16 21:54:15 -0700301 spin_lock_init(&fc_pci->irq_lock);
302
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700303 fc_pci->init_state |= FC_PCI_INIT;
Trent Piepho83977032006-05-12 20:36:24 -0300304 return ret;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700305
306err_pci_iounmap:
307 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
308 pci_set_drvdata(fc_pci->pdev, NULL);
309err_pci_release_regions:
310 pci_release_regions(fc_pci->pdev);
311err_pci_disable_device:
312 pci_disable_device(fc_pci->pdev);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700313 return ret;
314}
315
316static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
317{
318 if (fc_pci->init_state & FC_PCI_INIT) {
319 free_irq(fc_pci->pdev->irq, fc_pci);
320 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
321 pci_set_drvdata(fc_pci->pdev, NULL);
322 pci_release_regions(fc_pci->pdev);
323 pci_disable_device(fc_pci->pdev);
324 }
325 fc_pci->init_state &= ~FC_PCI_INIT;
326}
327
328
329static int flexcop_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
330{
331 struct flexcop_device *fc;
332 struct flexcop_pci *fc_pci;
333 int ret = -ENOMEM;
334
335 if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
336 err("out of memory\n");
337 return -ENOMEM;
338 }
339
340/* general flexcop init */
341 fc_pci = fc->bus_specific;
342 fc_pci->fc_dev = fc;
343
344 fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
345 fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
346 fc->i2c_request = flexcop_i2c_request;
347 fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
348
349 fc->stream_control = flexcop_pci_stream_control;
350
Johannes Stezenbachc4ee3fd2005-05-16 21:54:15 -0700351 if (enable_pid_filtering)
352 info("will use the HW PID filter.");
353 else
354 info("will pass the complete TS to the demuxer.");
355
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700356 fc->pid_filtering = enable_pid_filtering;
357 fc->bus_type = FC_PCI;
358
359 fc->dev = &pdev->dev;
Johannes Stezenbach59a7ad62005-05-16 21:54:16 -0700360 fc->owner = THIS_MODULE;
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700361
362/* bus specific part */
363 fc_pci->pdev = pdev;
364 if ((ret = flexcop_pci_init(fc_pci)) != 0)
365 goto err_kfree;
366
367/* init flexcop */
368 if ((ret = flexcop_device_initialize(fc)) != 0)
369 goto err_pci_exit;
370
371/* init dma */
372 if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
373 goto err_fc_exit;
374
David Howellsc4028952006-11-22 14:57:56 +0000375 INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
Patrick Boettcher64221be2005-07-07 17:57:49 -0700376
Trent Piepho83977032006-05-12 20:36:24 -0300377 return ret;
378
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700379err_fc_exit:
380 flexcop_device_exit(fc);
381err_pci_exit:
382 flexcop_pci_exit(fc_pci);
383err_kfree:
384 flexcop_device_kfree(fc);
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700385 return ret;
386}
387
388/* in theory every _exit function should be called exactly two times,
389 * here and in the bail-out-part of the _init-function
390 */
391static void flexcop_pci_remove(struct pci_dev *pdev)
392{
393 struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
394
395 flexcop_pci_dma_exit(fc_pci);
396 flexcop_device_exit(fc_pci->fc_dev);
397 flexcop_pci_exit(fc_pci);
398 flexcop_device_kfree(fc_pci->fc_dev);
399}
400
401static struct pci_device_id flexcop_pci_tbl[] = {
402 { PCI_DEVICE(0x13d0, 0x2103) },
Patrick Boettcher64221be2005-07-07 17:57:49 -0700403/* { PCI_DEVICE(0x13d0, 0x2200) }, ? */
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700404 { },
405};
406
407MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
408
409static struct pci_driver flexcop_pci_driver = {
Patrick Boettcher63b5c1c2005-07-07 17:58:30 -0700410 .name = "b2c2_flexcop_pci",
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700411 .id_table = flexcop_pci_tbl,
Patrick Boettcher64221be2005-07-07 17:57:49 -0700412 .probe = flexcop_pci_probe,
413 .remove = flexcop_pci_remove,
Johannes Stezenbach2add87a2005-05-16 21:54:10 -0700414};
415
416static int __init flexcop_pci_module_init(void)
417{
418 return pci_register_driver(&flexcop_pci_driver);
419}
420
421static void __exit flexcop_pci_module_exit(void)
422{
423 pci_unregister_driver(&flexcop_pci_driver);
424}
425
426module_init(flexcop_pci_module_init);
427module_exit(flexcop_pci_module_exit);
428
429MODULE_AUTHOR(DRIVER_AUTHOR);
430MODULE_DESCRIPTION(DRIVER_NAME);
431MODULE_LICENSE("GPL");