[PATCH] dvb: b2c2/flexcop driver refactoring part 2: add modular Flexcop driver
[safe/jmp/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 = 0;
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 #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
30 static int debug = 0;
31 module_param(debug, int, 0644);
32 MODULE_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
38 struct 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,
48  * this is logic is required */
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 */
55
56         struct flexcop_device *fc_dev;
57 };
58
59 static int lastwreg,lastwval,lastrreg,lastrval;
60
61 static flexcop_ibi_value flexcop_pci_read_ibi_reg (struct flexcop_device *fc, flexcop_ibi_register r)
62 {
63         struct flexcop_pci *fc_pci = fc->bus_specific;
64         flexcop_ibi_value v;
65         v.raw = readl(fc_pci->io_mem + r);
66
67         if (lastrreg != r || lastrval != v.raw) {
68                 lastrreg = r; lastrval = v.raw;
69                 deb_reg("new rd: %3x: %08x\n",r,v.raw);
70         }
71
72         return v;
73 }
74
75 static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register r, flexcop_ibi_value v)
76 {
77         struct flexcop_pci *fc_pci = fc->bus_specific;
78
79         if (lastwreg != r || lastwval != v.raw) {
80                 lastwreg = r; lastwval = v.raw;
81                 deb_reg("new wr: %3x: %08x\n",r,v.raw);
82         }
83
84         writel(v.raw, fc_pci->io_mem + r);
85         return 0;
86 }
87
88 /* When PID filtering is turned on, we use the timer IRQ, because small amounts
89  * of data need to be passed to the user space instantly as well. When PID
90  * filtering is turned off, we use the page-change-IRQ */
91 static irqreturn_t flexcop_pci_irq(int irq, void *dev_id, struct pt_regs *regs)
92 {
93         struct flexcop_pci *fc_pci = dev_id;
94         struct flexcop_device *fc = fc_pci->fc_dev;
95         flexcop_ibi_value v = fc->read_ibi_reg(fc,irq_20c);
96
97         deb_irq("irq: %08x cur_addr: %08x (%d), our addrs. 1: %08x 2: %08x; 0x000: "
98                         "%08x, 0x00c: %08x\n",v.raw,
99                         fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2,
100                         fc_pci->active_dma1_addr,
101                         fc_pci->dma[0].dma_addr0,fc_pci->dma[0].dma_addr1,
102                         fc->read_ibi_reg(fc,dma1_000).raw,
103                         fc->read_ibi_reg(fc,dma1_00c).raw);
104
105         if (v.irq_20c.DMA1_IRQ_Status == 1) {
106                 if (fc_pci->active_dma1_addr == 0)
107                         flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr0,fc_pci->dma[0].size / 188);
108                 else
109                         flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr1,fc_pci->dma[0].size / 188);
110
111                 deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
112                 fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
113         } else if (v.irq_20c.DMA1_Timer_Status == 1) {
114                 /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
115                  * complete TS packets when reading from the DMA memory */
116                 dma_addr_t cur_addr =
117                         fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
118                 u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
119
120                 /* buffer end was reached, restarted from the beginning
121                  * pass the data from last_cur_pos to the buffer end to the demux
122                  */
123                 if (cur_pos < fc_pci->last_dma1_cur_pos) {
124                         flexcop_pass_dmx_data(fc_pci->fc_dev,
125                                         fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
126                                         (fc_pci->dma[0].size*2 - 1) - fc_pci->last_dma1_cur_pos);
127                         fc_pci->last_dma1_cur_pos = 0;
128                 }
129
130                 if (cur_pos > fc_pci->last_dma1_cur_pos) {
131                         flexcop_pass_dmx_data(fc_pci->fc_dev,
132                                         fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
133                                         cur_pos - fc_pci->last_dma1_cur_pos);
134                 }
135
136                 fc_pci->last_dma1_cur_pos = cur_pos;
137         }
138
139 /* packet count would be ideal for hw filtering, but it isn't working. Either
140  * the data book is wrong, or I'm unable to read it correctly */
141
142 /*      if (v.irq_20c.DMA1_Size_IRQ_Status == 1) { packet counter */
143
144         return IRQ_HANDLED;
145 }
146
147 static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
148 {
149         struct flexcop_pci *fc_pci = fc->bus_specific;
150         if (onoff) {
151                 flexcop_dma_config(fc,&fc_pci->dma[0],FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1);
152                 flexcop_dma_config(fc,&fc_pci->dma[1],FC_DMA_2,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1);
153                 flexcop_dma_config_timer(fc,FC_DMA_1,1);
154
155                 if (fc_pci->fc_dev->pid_filtering) {
156                         fc_pci->last_dma1_cur_pos = 0;
157                         flexcop_dma_control_timer_irq(fc,FC_DMA_1,1);
158                 } else {
159                         fc_pci->active_dma1_addr = 0;
160                         flexcop_dma_control_size_irq(fc,FC_DMA_1,1);
161                 }
162
163 /*              flexcop_dma_config_packet_count(fc,FC_DMA_1,0xc0);
164                 flexcop_dma_control_packet_irq(fc,FC_DMA_1,1); */
165
166                 deb_irq("irqs enabled\n");
167         } else {
168                 if (fc_pci->fc_dev->pid_filtering)
169                         flexcop_dma_control_timer_irq(fc,FC_DMA_1,0);
170                 else
171                         flexcop_dma_control_size_irq(fc,FC_DMA_1,0);
172
173 //              flexcop_dma_control_packet_irq(fc,FC_DMA_1,0);
174                 deb_irq("irqs disabled\n");
175         }
176
177         return 0;
178 }
179
180 static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
181 {
182         int ret;
183         if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[0],FC_DEFAULT_DMA1_BUFSIZE)) != 0)
184                 return ret;
185
186         if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[1],FC_DEFAULT_DMA2_BUFSIZE)) != 0)
187                 goto dma1_free;
188
189         flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
190         flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_CAO   | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
191
192         fc_pci->init_state |= FC_PCI_DMA_INIT;
193         goto success;
194 dma1_free:
195         flexcop_dma_free(&fc_pci->dma[0]);
196
197 success:
198         return ret;
199 }
200
201 static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
202 {
203         if (fc_pci->init_state & FC_PCI_DMA_INIT) {
204                 flexcop_dma_free(&fc_pci->dma[0]);
205                 flexcop_dma_free(&fc_pci->dma[1]);
206         }
207         fc_pci->init_state &= ~FC_PCI_DMA_INIT;
208 }
209
210 static int flexcop_pci_init(struct flexcop_pci *fc_pci)
211 {
212         int ret;
213         u8 card_rev;
214
215         pci_read_config_byte(fc_pci->pdev, PCI_CLASS_REVISION, &card_rev);
216         info("card revision %x", card_rev);
217
218         if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
219                 return ret;
220
221         pci_set_master(fc_pci->pdev);
222
223         /* enable interrupts */
224         // pci_write_config_dword(pdev, 0x6c, 0x8000);
225
226         if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
227                 goto err_pci_disable_device;
228
229         fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
230
231         if (!fc_pci->io_mem) {
232                 err("cannot map io memory\n");
233                 ret = -EIO;
234                 goto err_pci_release_regions;
235         }
236
237         pci_set_drvdata(fc_pci->pdev, fc_pci);
238
239         if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_irq,
240                                         SA_SHIRQ, DRIVER_NAME, fc_pci)) != 0)
241                 goto err_pci_iounmap;
242
243         fc_pci->init_state |= FC_PCI_INIT;
244         goto success;
245
246 err_pci_iounmap:
247         pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
248         pci_set_drvdata(fc_pci->pdev, NULL);
249 err_pci_release_regions:
250         pci_release_regions(fc_pci->pdev);
251 err_pci_disable_device:
252         pci_disable_device(fc_pci->pdev);
253
254 success:
255         return ret;
256 }
257
258 static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
259 {
260         if (fc_pci->init_state & FC_PCI_INIT) {
261                 free_irq(fc_pci->pdev->irq, fc_pci);
262                 pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
263                 pci_set_drvdata(fc_pci->pdev, NULL);
264                 pci_release_regions(fc_pci->pdev);
265                 pci_disable_device(fc_pci->pdev);
266         }
267         fc_pci->init_state &= ~FC_PCI_INIT;
268 }
269
270
271 static int flexcop_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
272 {
273         struct flexcop_device *fc;
274         struct flexcop_pci *fc_pci;
275         int ret = -ENOMEM;
276
277         if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
278                 err("out of memory\n");
279                 return -ENOMEM;
280         }
281
282 /* general flexcop init */
283         fc_pci = fc->bus_specific;
284         fc_pci->fc_dev = fc;
285
286         fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
287         fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
288         fc->i2c_request = flexcop_i2c_request;
289         fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
290
291         fc->stream_control = flexcop_pci_stream_control;
292
293         fc->pid_filtering = enable_pid_filtering;
294         fc->bus_type = FC_PCI;
295
296         fc->dev = &pdev->dev;
297
298 /* bus specific part */
299         fc_pci->pdev = pdev;
300         if ((ret = flexcop_pci_init(fc_pci)) != 0)
301                 goto err_kfree;
302
303 /* init flexcop */
304         if ((ret = flexcop_device_initialize(fc)) != 0)
305                 goto err_pci_exit;
306
307 /* init dma */
308         if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
309                 goto err_fc_exit;
310
311         goto success;
312 err_fc_exit:
313         flexcop_device_exit(fc);
314 err_pci_exit:
315         flexcop_pci_exit(fc_pci);
316 err_kfree:
317         flexcop_device_kfree(fc);
318 success:
319         return ret;
320 }
321
322 /* in theory every _exit function should be called exactly two times,
323  * here and in the bail-out-part of the _init-function
324  */
325 static void flexcop_pci_remove(struct pci_dev *pdev)
326 {
327         struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
328
329         flexcop_pci_dma_exit(fc_pci);
330         flexcop_device_exit(fc_pci->fc_dev);
331         flexcop_pci_exit(fc_pci);
332         flexcop_device_kfree(fc_pci->fc_dev);
333 }
334
335 static struct pci_device_id flexcop_pci_tbl[] = {
336         { PCI_DEVICE(0x13d0, 0x2103) },
337 /*      { PCI_DEVICE(0x13d0, 0x2200) }, PCI FlexCopIII ? */
338         { },
339 };
340
341 MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
342
343 static struct pci_driver flexcop_pci_driver = {
344         .name = "Technisat/B2C2 FlexCop II/IIb/III PCI",
345         .id_table = flexcop_pci_tbl,
346         .probe = flexcop_pci_probe,
347         .remove = flexcop_pci_remove,
348 };
349
350 static int __init flexcop_pci_module_init(void)
351 {
352         return pci_register_driver(&flexcop_pci_driver);
353 }
354
355 static void __exit flexcop_pci_module_exit(void)
356 {
357         pci_unregister_driver(&flexcop_pci_driver);
358 }
359
360 module_init(flexcop_pci_module_init);
361 module_exit(flexcop_pci_module_exit);
362
363 MODULE_AUTHOR(DRIVER_AUTHOR);
364 MODULE_DESCRIPTION(DRIVER_NAME);
365 MODULE_LICENSE("GPL");