Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / media / dvb / b2c2 / flexcop-pci.c
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
12 static int enable_pid_filtering = 1;
13 module_param(enable_pid_filtering, int, 0444);
14 MODULE_PARM_DESC(enable_pid_filtering, "enable hardware pid filtering: supported values: 0 (fullts), 1");
15
16 static int irq_chk_intv;
17 module_param(irq_chk_intv, int, 0644);
18 MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ watchdog (currently just debugging).");
19
20 #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)
33 #define deb_chk(args...)   dprintk(0x10,args)
34
35 static int debug = 0;
36 module_param(debug, int, 0644);
37 MODULE_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
43 struct 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,
53  * this logic is required */
54 #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 */
60         int count;
61
62         spinlock_t irq_lock;
63
64         unsigned long last_irq;
65
66         struct work_struct irq_check_work;
67
68         struct flexcop_device *fc_dev;
69 };
70
71 static int lastwreg,lastwval,lastrreg,lastrval;
72
73 static 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
87 static 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
100 static void flexcop_pci_irq_check_work(void *data)
101 {
102         struct flexcop_pci *fc_pci = data;
103         struct flexcop_device *fc = fc_pci->fc_dev;
104
105         flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
106
107         flexcop_dump_reg(fc_pci->fc_dev,dma1_000,4);
108
109         if (v.sram_dest_reg_714.net_ovflow_error)
110                 deb_chk("sram net_ovflow_error\n");
111         if (v.sram_dest_reg_714.media_ovflow_error)
112                 deb_chk("sram media_ovflow_error\n");
113         if (v.sram_dest_reg_714.cai_ovflow_error)
114                 deb_chk("sram cai_ovflow_error\n");
115         if (v.sram_dest_reg_714.cai_ovflow_error)
116                 deb_chk("sram cai_ovflow_error\n");
117
118         schedule_delayed_work(&fc_pci->irq_check_work,
119                         msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
120 }
121
122 /* When PID filtering is turned on, we use the timer IRQ, because small amounts
123  * of data need to be passed to the user space instantly as well. When PID
124  * filtering is turned off, we use the page-change-IRQ */
125 static irqreturn_t flexcop_pci_isr(int irq, void *dev_id, struct pt_regs *regs)
126 {
127         struct flexcop_pci *fc_pci = dev_id;
128         struct flexcop_device *fc = fc_pci->fc_dev;
129         flexcop_ibi_value v;
130         irqreturn_t ret = IRQ_HANDLED;
131
132         spin_lock_irq(&fc_pci->irq_lock);
133
134         v = fc->read_ibi_reg(fc,irq_20c);
135
136    /* errors */
137         if (v.irq_20c.Data_receiver_error)
138                 deb_chk("data receiver error\n");
139         if (v.irq_20c.Continuity_error_flag)
140                 deb_chk("Contunuity error flag is set\n");
141         if (v.irq_20c.LLC_SNAP_FLAG_set)
142                 deb_chk("LLC_SNAP_FLAG_set is set\n");
143         if (v.irq_20c.Transport_Error)
144                 deb_chk("Transport error\n");
145
146         if ((fc_pci->count % 1000) == 0)
147                 deb_chk("%d valid irq took place so far\n",fc_pci->count);
148
149         if (v.irq_20c.DMA1_IRQ_Status == 1) {
150                 if (fc_pci->active_dma1_addr == 0)
151                         flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr0,fc_pci->dma[0].size / 188);
152                 else
153                         flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr1,fc_pci->dma[0].size / 188);
154
155                 deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
156                 fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
157         } else if (v.irq_20c.DMA1_Timer_Status == 1) {
158                 /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
159                  * complete TS packets when reading from the DMA memory */
160                 dma_addr_t cur_addr =
161                         fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
162                 u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
163
164                 deb_irq("%u irq: %08x cur_addr: %08x: cur_pos: %08x, last_cur_pos: %08x ",
165                                 jiffies_to_usecs(jiffies - fc_pci->last_irq),v.raw,cur_addr,cur_pos,fc_pci->last_dma1_cur_pos);
166                 fc_pci->last_irq = jiffies;
167
168                 /* buffer end was reached, restarted from the beginning
169                  * pass the data from last_cur_pos to the buffer end to the demux
170                  */
171                 if (cur_pos < fc_pci->last_dma1_cur_pos) {
172                         deb_irq(" end was reached: passing %d bytes ",(fc_pci->dma[0].size*2 - 1) - fc_pci->last_dma1_cur_pos);
173                         flexcop_pass_dmx_data(fc_pci->fc_dev,
174                                         fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
175                                         (fc_pci->dma[0].size*2) - fc_pci->last_dma1_cur_pos);
176                         fc_pci->last_dma1_cur_pos = 0;
177                 }
178
179                 if (cur_pos > fc_pci->last_dma1_cur_pos) {
180                         deb_irq(" passing %d bytes ",cur_pos - fc_pci->last_dma1_cur_pos);
181                         flexcop_pass_dmx_data(fc_pci->fc_dev,
182                                         fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
183                                         cur_pos - fc_pci->last_dma1_cur_pos);
184                 }
185                 deb_irq("\n");
186
187                 fc_pci->last_dma1_cur_pos = cur_pos;
188                 fc_pci->count++;
189         } else {
190                 deb_irq("isr for flexcop called, apparently without reason (%08x)\n",v.raw);
191                 ret = IRQ_NONE;
192         }
193
194         spin_unlock_irq(&fc_pci->irq_lock);
195
196         return ret;
197 }
198
199 static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
200 {
201         struct flexcop_pci *fc_pci = fc->bus_specific;
202         if (onoff) {
203                 flexcop_dma_config(fc,&fc_pci->dma[0],FC_DMA_1);
204                 flexcop_dma_config(fc,&fc_pci->dma[1],FC_DMA_2);
205
206                 flexcop_dma_config_timer(fc,FC_DMA_1,0);
207
208                 flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,1);
209                 deb_irq("DMA xfer enabled\n");
210
211                 fc_pci->last_dma1_cur_pos = 0;
212                 flexcop_dma_control_timer_irq(fc,FC_DMA_1,1);
213                 deb_irq("IRQ enabled\n");
214
215 //              fc_pci->active_dma1_addr = 0;
216 //              flexcop_dma_control_size_irq(fc,FC_DMA_1,1);
217
218                 if (irq_chk_intv > 0)
219                         schedule_delayed_work(&fc_pci->irq_check_work,
220                                         msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
221         } else {
222                 if (irq_chk_intv > 0)
223                         cancel_delayed_work(&fc_pci->irq_check_work);
224
225                 flexcop_dma_control_timer_irq(fc,FC_DMA_1,0);
226                 deb_irq("IRQ disabled\n");
227
228 //              flexcop_dma_control_size_irq(fc,FC_DMA_1,0);
229
230                 flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,0);
231                 deb_irq("DMA xfer disabled\n");
232         }
233
234         return 0;
235 }
236
237 static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
238 {
239         int ret;
240         if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[0],FC_DEFAULT_DMA1_BUFSIZE)) != 0)
241                 return ret;
242
243         if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[1],FC_DEFAULT_DMA2_BUFSIZE)) != 0)
244                 goto dma1_free;
245
246         flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
247         flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_CAO   | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
248
249         fc_pci->init_state |= FC_PCI_DMA_INIT;
250
251         goto success;
252 dma1_free:
253         flexcop_dma_free(&fc_pci->dma[0]);
254
255 success:
256         return ret;
257 }
258
259 static 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
268 static 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
297         if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
298                                         SA_SHIRQ, DRIVER_NAME, fc_pci)) != 0)
299                 goto err_pci_iounmap;
300
301         spin_lock_init(&fc_pci->irq_lock);
302
303         fc_pci->init_state |= FC_PCI_INIT;
304         goto success;
305
306 err_pci_iounmap:
307         pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
308         pci_set_drvdata(fc_pci->pdev, NULL);
309 err_pci_release_regions:
310         pci_release_regions(fc_pci->pdev);
311 err_pci_disable_device:
312         pci_disable_device(fc_pci->pdev);
313
314 success:
315         return ret;
316 }
317
318 static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
319 {
320         if (fc_pci->init_state & FC_PCI_INIT) {
321                 free_irq(fc_pci->pdev->irq, fc_pci);
322                 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
323                 pci_set_drvdata(fc_pci->pdev, NULL);
324                 pci_release_regions(fc_pci->pdev);
325                 pci_disable_device(fc_pci->pdev);
326         }
327         fc_pci->init_state &= ~FC_PCI_INIT;
328 }
329
330
331 static int flexcop_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
332 {
333         struct flexcop_device *fc;
334         struct flexcop_pci *fc_pci;
335         int ret = -ENOMEM;
336
337         if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
338                 err("out of memory\n");
339                 return -ENOMEM;
340         }
341
342 /* general flexcop init */
343         fc_pci = fc->bus_specific;
344         fc_pci->fc_dev = fc;
345
346         fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
347         fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
348         fc->i2c_request = flexcop_i2c_request;
349         fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
350
351         fc->stream_control = flexcop_pci_stream_control;
352
353         if (enable_pid_filtering)
354                 info("will use the HW PID filter.");
355         else
356                 info("will pass the complete TS to the demuxer.");
357
358         fc->pid_filtering = enable_pid_filtering;
359         fc->bus_type = FC_PCI;
360
361         fc->dev = &pdev->dev;
362         fc->owner = THIS_MODULE;
363
364 /* bus specific part */
365         fc_pci->pdev = pdev;
366         if ((ret = flexcop_pci_init(fc_pci)) != 0)
367                 goto err_kfree;
368
369 /* init flexcop */
370         if ((ret = flexcop_device_initialize(fc)) != 0)
371                 goto err_pci_exit;
372
373 /* init dma */
374         if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
375                 goto err_fc_exit;
376
377         INIT_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work, fc_pci);
378
379         goto success;
380 err_fc_exit:
381         flexcop_device_exit(fc);
382 err_pci_exit:
383         flexcop_pci_exit(fc_pci);
384 err_kfree:
385         flexcop_device_kfree(fc);
386 success:
387         return ret;
388 }
389
390 /* in theory every _exit function should be called exactly two times,
391  * here and in the bail-out-part of the _init-function
392  */
393 static void flexcop_pci_remove(struct pci_dev *pdev)
394 {
395         struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
396
397         flexcop_pci_dma_exit(fc_pci);
398         flexcop_device_exit(fc_pci->fc_dev);
399         flexcop_pci_exit(fc_pci);
400         flexcop_device_kfree(fc_pci->fc_dev);
401 }
402
403 static struct pci_device_id flexcop_pci_tbl[] = {
404         { PCI_DEVICE(0x13d0, 0x2103) },
405 /*      { PCI_DEVICE(0x13d0, 0x2200) }, ? */
406         { },
407 };
408
409 MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
410
411 static struct pci_driver flexcop_pci_driver = {
412         .name     = "b2c2_flexcop_pci",
413         .id_table = flexcop_pci_tbl,
414         .probe    = flexcop_pci_probe,
415         .remove   = flexcop_pci_remove,
416 };
417
418 static int __init flexcop_pci_module_init(void)
419 {
420         return pci_register_driver(&flexcop_pci_driver);
421 }
422
423 static void __exit flexcop_pci_module_exit(void)
424 {
425         pci_unregister_driver(&flexcop_pci_driver);
426 }
427
428 module_init(flexcop_pci_module_init);
429 module_exit(flexcop_pci_module_exit);
430
431 MODULE_AUTHOR(DRIVER_AUTHOR);
432 MODULE_DESCRIPTION(DRIVER_NAME);
433 MODULE_LICENSE("GPL");