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