1 /* $NetBSD: coram.c,v 1.12 2013/10/16 18:20:16 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2011 Jonathan A. Kollasch 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: coram.c,v 1.12 2013/10/16 18:20:16 christos Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/kmem.h> 36 #include <sys/mutex.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 40 #include <dev/dtv/dtvif.h> 41 42 #include <dev/pci/cx23885reg.h> 43 #include <dev/pci/coramvar.h> 44 45 #include <dev/pci/pcivar.h> 46 #include <dev/pci/pcireg.h> 47 #include <dev/pci/pcidevs.h> 48 #include <dev/i2c/i2cvar.h> 49 #include <dev/i2c/at24cxxvar.h> 50 51 #include <dev/i2c/cx24227var.h> 52 #include <dev/i2c/mt2131var.h> 53 54 /* #define CORAM_DEBUG */ 55 /* #define CORAM_ATTACH_I2C */ 56 57 static const struct coram_board coram_boards[] = { 58 { PCI_VENDOR_HAUPPAUGE, 0x7911, "Hauppauge HVR-1250" }, 59 }; 60 61 static int coram_match(device_t, cfdata_t, void *); 62 static void coram_attach(device_t, device_t, void *); 63 static int coram_detach(device_t, int); 64 static int coram_rescan(device_t, const char *, const int *); 65 static void coram_childdet(device_t, device_t); 66 static bool coram_resume(device_t, const pmf_qual_t *); 67 static int coram_intr(void *); 68 static const struct coram_board * coram_board_lookup(uint16_t, uint16_t); 69 70 static int coram_iic_exec(void *, i2c_op_t, i2c_addr_t, 71 const void *, size_t, void *, size_t, int); 72 static int coram_iic_acquire_bus(void *, int); 73 static void coram_iic_release_bus(void *, int); 74 static int coram_iic_read(struct coram_iic_softc *, i2c_op_t, i2c_addr_t, 75 const void *, size_t, void *, size_t, int); 76 static int coram_iic_write(struct coram_iic_softc *, i2c_op_t, i2c_addr_t, 77 const void *, size_t, void *, size_t, int); 78 79 static void coram_dtv_get_devinfo(void *, struct dvb_frontend_info *); 80 static int coram_dtv_open(void *, int); 81 static void coram_dtv_close(void *); 82 static int coram_dtv_set_tuner(void *, const struct dvb_frontend_parameters *); 83 static fe_status_t coram_dtv_get_status(void *); 84 static uint16_t coram_dtv_get_signal_strength(void *); 85 static uint16_t coram_dtv_get_snr(void *); 86 static int coram_dtv_start_transfer(void *, void (*)(void *, const struct dtv_payload *), void *); 87 static int coram_dtv_stop_transfer(void *); 88 89 static int coram_mpeg_attach(struct coram_softc *); 90 static int coram_mpeg_detach(struct coram_softc *, int); 91 static int coram_mpeg_reset(struct coram_softc *); 92 static void * coram_mpeg_malloc(struct coram_softc *, size_t); 93 static int coram_allocmem(struct coram_softc *, size_t, size_t, struct coram_dma *); 94 static void coram_mpeg_free(struct coram_softc *, void *); 95 static int coram_mpeg_halt(struct coram_softc *); 96 static int coram_freemem(struct coram_softc *, struct coram_dma *); 97 static int coram_mpeg_trigger(struct coram_softc *, void *); 98 static int coram_risc_buffer(struct coram_softc *, uint32_t, uint32_t); 99 static int coram_risc_field(struct coram_softc *, uint32_t *, uint32_t); 100 static int coram_sram_ch_setup(struct coram_softc *, struct coram_sram_ch *, uint32_t); 101 static int coram_mpeg_intr(struct coram_softc *); 102 103 CFATTACH_DECL2_NEW(coram, sizeof(struct coram_softc), 104 coram_match, coram_attach, coram_detach, NULL, 105 coram_rescan, coram_childdet); 106 107 #define CORAM_SRAM_CH6 0 108 109 #define CORAM_TS_PKTSIZE (188 * 8) 110 111 static struct coram_sram_ch coram_sram_chs[] = { 112 [CORAM_SRAM_CH6] = { 113 .csc_cmds= 0x10140, 114 .csc_iq = 0x10500, 115 .csc_iqsz = 0x40, 116 .csc_cdt = 0x10600, 117 .csc_cdtsz = 0x10, 118 .csc_fifo = 0x6000, 119 .csc_fifosz = 0x1000, 120 .csc_risc = 0x10800, 121 .csc_riscsz = 0x800, 122 .csc_ptr1 = DMA5_PTR1, 123 .csc_ptr2 = DMA5_PTR2, 124 .csc_cnt1 = DMA5_CNT1, 125 .csc_cnt2 = DMA5_CNT2, 126 }, 127 }; 128 129 static const struct dtv_hw_if coram_dtv_if = { 130 .get_devinfo = coram_dtv_get_devinfo, 131 .open = coram_dtv_open, 132 .close = coram_dtv_close, 133 .set_tuner = coram_dtv_set_tuner, 134 .get_status = coram_dtv_get_status, 135 .get_signal_strength = coram_dtv_get_signal_strength, 136 .get_snr = coram_dtv_get_snr, 137 .start_transfer = coram_dtv_start_transfer, 138 .stop_transfer = coram_dtv_stop_transfer, 139 }; 140 141 static int 142 coram_match(device_t parent, cfdata_t match, void *v) 143 { 144 const struct pci_attach_args *pa = v; 145 pcireg_t subid; 146 147 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CONEXANT) 148 return 0; 149 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_CONEXANT_CX23885) 150 return 0; 151 152 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 153 if (coram_board_lookup(PCI_VENDOR(subid), PCI_PRODUCT(subid)) == NULL) 154 return 0; 155 156 return 1; 157 } 158 159 static void 160 coram_attach(device_t parent, device_t self, void *aux) 161 { 162 struct coram_softc *sc = device_private(self); 163 const struct pci_attach_args *pa = aux; 164 pci_intr_handle_t ih; 165 pcireg_t reg; 166 const char *intrstr; 167 struct coram_iic_softc *cic; 168 uint32_t value; 169 int i; 170 #ifdef CORAM_ATTACH_I2C 171 struct i2cbus_attach_args iba; 172 #endif 173 174 sc->sc_dev = self; 175 176 pci_aprint_devinfo(pa, NULL); 177 178 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 179 sc->sc_board = coram_board_lookup(PCI_VENDOR(reg), PCI_PRODUCT(reg)); 180 KASSERT(sc->sc_board != NULL); 181 182 if (pci_mapreg_map(pa, CX23885_MMBASE, PCI_MAPREG_TYPE_MEM, 0, 183 &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems)) { 184 aprint_error_dev(self, "couldn't map memory space\n"); 185 return; 186 } 187 188 sc->sc_dmat = pa->pa_dmat; 189 sc->sc_pc = pa->pa_pc; 190 191 if (pci_intr_map(pa, &ih)) { 192 aprint_error_dev(self, "couldn't map interrupt\n"); 193 return; 194 } 195 intrstr = pci_intr_string(pa->pa_pc, ih); 196 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_VM, coram_intr, self); 197 if (sc->sc_ih == NULL) { 198 aprint_error_dev(self, "couldn't establish interrupt"); 199 if (intrstr != NULL) 200 aprint_error(" at %s", intrstr); 201 aprint_error("\n"); 202 return; 203 } 204 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 205 206 /* set master */ 207 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); 208 reg |= PCI_COMMAND_MASTER_ENABLE; 209 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg); 210 211 /* I2C */ 212 for(i = 0; i < I2C_NUM; i++) { 213 cic = &sc->sc_iic[i]; 214 215 cic->cic_sc = sc; 216 if (bus_space_subregion(sc->sc_memt, sc->sc_memh, 217 I2C_BASE + (I2C_SIZE * i), I2C_SIZE, &cic->cic_regh)) 218 panic("failed to subregion i2c"); 219 220 mutex_init(&cic->cic_busmutex, MUTEX_DRIVER, IPL_NONE); 221 cic->cic_i2c.ic_cookie = cic; 222 cic->cic_i2c.ic_acquire_bus = coram_iic_acquire_bus; 223 cic->cic_i2c.ic_release_bus = coram_iic_release_bus; 224 cic->cic_i2c.ic_exec = coram_iic_exec; 225 226 #ifdef CORAM_ATTACH_I2C 227 /* attach iic(4) */ 228 memset(&iba, 0, sizeof(iba)); 229 iba.iba_tag = &cic->cic_i2c; 230 iba.iba_type = I2C_TYPE_SMBUS; 231 cic->cic_i2cdev = config_found_ia(self, "i2cbus", &iba, 232 iicbus_print); 233 #endif 234 } 235 236 /* HVR1250 GPIO */ 237 value = bus_space_read_4(sc->sc_memt, sc->sc_memh, 0x110010); 238 #if 1 239 value &= ~0x00010001; 240 bus_space_write_4(sc->sc_memt, sc->sc_memh, 0x110010, value); 241 delay(5000); 242 #endif 243 value |= 0x00010001; 244 bus_space_write_4(sc->sc_memt, sc->sc_memh, 0x110010, value); 245 246 #if 0 247 int i; 248 uint8_t foo[256]; 249 uint8_t bar; 250 bar = 0; 251 // seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0, 256, foo, 256); 252 253 iic_acquire_bus(&sc->sc_i2c, I2C_F_POLL); 254 iic_exec(&sc->sc_i2c, I2C_OP_READ_WITH_STOP, 0x50, &bar, 1, foo, 256, 255 I2C_F_POLL); 256 iic_release_bus(&sc->sc_i2c, I2C_F_POLL); 257 258 printf("\n"); 259 for ( i = 0; i < 256; i++) { 260 if ( (i % 8) == 0 ) 261 printf("%02x: ", i); 262 263 printf("%02x", foo[i]); 264 265 if ( (i % 8) == 7 ) 266 printf("\n"); 267 else 268 printf(" "); 269 } 270 printf("\n"); 271 #endif 272 273 sc->sc_demod = cx24227_open(sc->sc_dev, &sc->sc_iic[0].cic_i2c, 0x19); 274 if (sc->sc_demod == NULL) 275 aprint_error_dev(self, "couldn't open cx24227\n"); 276 sc->sc_tuner = mt2131_open(sc->sc_dev, &sc->sc_iic[0].cic_i2c, 0x61); 277 if (sc->sc_tuner == NULL) 278 aprint_error_dev(self, "couldn't open mt2131\n"); 279 280 coram_mpeg_attach(sc); 281 282 if (!pmf_device_register(self, NULL, coram_resume)) 283 aprint_error_dev(self, "couldn't establish power handler\n"); 284 285 return; 286 } 287 288 static int 289 coram_detach(device_t self, int flags) 290 { 291 struct coram_softc *sc = device_private(self); 292 struct coram_iic_softc *cic; 293 unsigned int i; 294 int error; 295 296 error = coram_mpeg_detach(sc, flags); 297 if (error) 298 return error; 299 300 if (sc->sc_tuner) 301 mt2131_close(sc->sc_tuner); 302 if (sc->sc_demod) 303 cx24227_close(sc->sc_demod); 304 for (i = 0; i < I2C_NUM; i++) { 305 cic = &sc->sc_iic[i]; 306 if (cic->cic_i2cdev) 307 config_detach(cic->cic_i2cdev, flags); 308 mutex_destroy(&cic->cic_busmutex); 309 } 310 pmf_device_deregister(self); 311 312 if (sc->sc_mems) 313 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); 314 if (sc->sc_ih) 315 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 316 317 return 0; 318 } 319 320 static int 321 coram_rescan(device_t self, const char *ifattr, const int *locs) 322 { 323 struct coram_softc *sc = device_private(self); 324 struct dtv_attach_args daa; 325 326 daa.hw = &coram_dtv_if; 327 daa.priv = sc; 328 329 if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL) 330 sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus", 331 &daa, dtv_print); 332 333 return 0; 334 } 335 336 static void 337 coram_childdet(device_t self, device_t child) 338 { 339 struct coram_softc *sc = device_private(self); 340 struct coram_iic_softc *cic; 341 unsigned int i; 342 343 if (sc->sc_dtvdev == child) 344 sc->sc_dtvdev = NULL; 345 346 for (i = 0; i < I2C_NUM; i++) { 347 cic = &sc->sc_iic[i]; 348 if (cic->cic_i2cdev == child) 349 cic->cic_i2cdev = NULL; 350 } 351 } 352 353 static int 354 coram_intr(void *v) 355 { 356 device_t self = v; 357 struct coram_softc *sc; 358 uint32_t val; 359 360 sc = device_private(self); 361 362 val = bus_space_read_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSTAT ); 363 if (val == 0) 364 return 0; /* not ours */ 365 366 /* vid c */ 367 if (val & __BIT(2)) 368 coram_mpeg_intr(sc); 369 370 if (val & ~__BIT(2)) 371 printf("%s %08x\n", __func__, val); 372 373 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_STAT, val); 374 375 return 1; 376 } 377 378 static const struct coram_board * 379 coram_board_lookup(uint16_t vendor, uint16_t product) 380 { 381 unsigned int i; 382 383 for (i = 0; i < __arraycount(coram_boards); i++) { 384 if (coram_boards[i].vendor == vendor && 385 coram_boards[i].product == product) { 386 return &coram_boards[i]; 387 } 388 } 389 390 return NULL; 391 } 392 393 #define CXDTV_TS_RISCI2 (1 << 4) 394 #define CXDTV_TS_RISCI1 (1 << 0) 395 396 #define CXDTV_TS_RISCI (CXDTV_TS_RISCI1|CXDTV_TS_RISCI2) 397 398 static int 399 coram_mpeg_intr(struct coram_softc *sc) 400 { 401 struct dtv_payload payload; 402 uint32_t s, m, v; 403 int i; 404 405 s = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_INT_STAT); 406 m = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK); 407 408 if ((s & m) == 0) 409 return 0; 410 411 if ( (s & ~CXDTV_TS_RISCI) != 0 ) { 412 printf("%s: unexpected TS IS %08x\n", 413 device_xname(sc->sc_dev), s); 414 415 printf("cmds:\n"); 416 for(i = 0; i < 20; i++) 417 { 418 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, 0x10140 +(i*4)); 419 printf("%06x %08x\n", 0x10140+(i*4), v); 420 } 421 } 422 423 if (sc->sc_dtvsubmitcb == NULL) 424 goto done; 425 426 if ((s & CXDTV_TS_RISCI1) == CXDTV_TS_RISCI1) { 427 bus_dmamap_sync(sc->sc_dmat, sc->sc_dma->map, 428 0, CORAM_TS_PKTSIZE, 429 BUS_DMASYNC_POSTREAD); 430 payload.data = KERNADDR(sc->sc_dma); 431 payload.size = CORAM_TS_PKTSIZE; 432 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload); 433 } 434 435 if ((s & CXDTV_TS_RISCI2) == CXDTV_TS_RISCI2) { 436 bus_dmamap_sync(sc->sc_dmat, sc->sc_dma->map, 437 CORAM_TS_PKTSIZE, CORAM_TS_PKTSIZE, 438 BUS_DMASYNC_POSTREAD); 439 payload.data = (char *)(KERNADDR(sc->sc_dma)) + (uintptr_t)CORAM_TS_PKTSIZE; 440 payload.size = CORAM_TS_PKTSIZE; 441 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload); 442 } 443 444 done: 445 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_INT_STAT, s); 446 447 return 1; 448 } 449 450 static bool 451 coram_resume(device_t dv, const pmf_qual_t *qual) 452 { 453 return true; 454 } 455 456 static int 457 coram_iic_acquire_bus(void *cookie, int flags) 458 { 459 struct coram_iic_softc *cic; 460 461 cic = cookie; 462 463 if (flags & I2C_F_POLL) { 464 while (mutex_tryenter(&cic->cic_busmutex) == 0) 465 delay(50); 466 return 0; 467 } 468 469 mutex_enter(&cic->cic_busmutex); 470 471 return 0; 472 } 473 474 static void 475 coram_iic_release_bus(void *cookie, int flags) 476 { 477 struct coram_iic_softc *cic; 478 479 cic = cookie; 480 481 mutex_exit(&cic->cic_busmutex); 482 483 return; 484 } 485 486 /* I2C Bus */ 487 488 #define I2C_ADDR 0x0000 489 #define I2C_WDATA 0x0004 490 #define I2C_CTRL 0x0008 491 #define I2C_RDATA 0x000c 492 #define I2C_STAT 0x0010 493 494 #define I2C_EXTEND (1 << 3) 495 #define I2C_NOSTOP (1 << 4) 496 497 static int 498 coram_iic_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, 499 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags) 500 { 501 struct coram_iic_softc *cic; 502 int ret; 503 504 cic = cookie; 505 506 if(cmdlen) { 507 ret = coram_iic_write(cic, op, addr, cmdbuf, cmdlen, buf, len, flags); 508 if(ret) 509 return ret; 510 } 511 512 if(len) { 513 ret = coram_iic_read(cic, op, addr, cmdbuf, cmdlen, buf, len, flags); 514 if(ret) 515 return ret; 516 } 517 518 519 return 0; 520 521 } 522 523 static int 524 coram_iic_read(struct coram_iic_softc *cic, i2c_op_t op, i2c_addr_t addr, 525 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags) 526 { 527 uint8_t *rb; 528 uint32_t ctrl; 529 int bn; 530 531 rb = buf; 532 533 for ( bn = 0; bn < len; bn++) { 534 ctrl = (0x9d << 24) | (1 << 12) | (1 << 2) | 1; 535 if ( bn < len - 1 ) 536 ctrl |= I2C_NOSTOP | I2C_EXTEND; 537 538 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_ADDR, addr<<25); 539 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_CTRL, ctrl); 540 541 while((bus_space_read_4(cic->cic_sc->sc_memt, cic->cic_regh, 542 I2C_STAT) & 0x02)) { 543 delay(25); 544 } 545 if((bus_space_read_4(cic->cic_sc->sc_memt, cic->cic_regh, 546 I2C_STAT) & 0x01) == 0x00) { 547 // printf("%s %d no ack\n", __func__, bn); 548 return EIO; 549 } 550 551 rb[bn] = bus_space_read_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_RDATA); 552 553 } 554 555 return 0; 556 } 557 558 static int 559 coram_iic_write(struct coram_iic_softc *cic, i2c_op_t op, i2c_addr_t addr, 560 const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags) 561 { 562 const uint8_t *wb; 563 uint32_t wdata, addrreg, ctrl; 564 int bn; 565 566 wb = cmdbuf; 567 568 addrreg = (addr << 25) | wb[0]; 569 wdata = wb[0]; 570 ctrl = (0x9d << 24) | (1 << 12) | (1 << 2); 571 572 if ( cmdlen > 1 ) 573 ctrl |= I2C_NOSTOP | I2C_EXTEND; 574 else if (len) 575 ctrl |= I2C_NOSTOP; 576 577 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_ADDR, addrreg); 578 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_WDATA, wdata); 579 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_CTRL, ctrl); 580 581 while((bus_space_read_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_STAT) & 0x02)) { 582 delay(25); } 583 584 for ( bn = 1; bn < cmdlen; bn++) { 585 ctrl = (0x9d << 24) | (1 << 12) | (1 << 2); 586 wdata = wb[bn]; 587 588 if ( bn < cmdlen - 1 ) 589 ctrl |= I2C_NOSTOP | I2C_EXTEND; 590 else if (len) 591 ctrl |= I2C_NOSTOP; 592 593 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_ADDR, addrreg); 594 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_WDATA, wdata); 595 bus_space_write_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_CTRL, ctrl); 596 597 while((bus_space_read_4(cic->cic_sc->sc_memt, cic->cic_regh, I2C_STAT) & 0x02)) { 598 delay(25); } 599 } 600 601 return 0; 602 } 603 604 static int 605 coram_mpeg_attach(struct coram_softc *sc) 606 { 607 struct coram_sram_ch *ch; 608 609 ch = &coram_sram_chs[CORAM_SRAM_CH6]; 610 611 sc->sc_riscbufsz = ch->csc_riscsz; 612 sc->sc_riscbuf = kmem_alloc(ch->csc_riscsz, KM_SLEEP); 613 614 if ( sc->sc_riscbuf == NULL ) 615 panic("riscbuf null"); 616 617 coram_mpeg_reset(sc); 618 619 sc->sc_tsbuf = NULL; 620 621 coram_rescan(sc->sc_dev, NULL, NULL); 622 623 return (sc->sc_dtvdev != NULL); 624 } 625 626 static int 627 coram_mpeg_detach(struct coram_softc *sc, int flags) 628 { 629 struct coram_sram_ch *ch = &coram_sram_chs[CORAM_SRAM_CH6]; 630 int error; 631 632 if (sc->sc_dtvdev) { 633 error = config_detach(sc->sc_dtvdev, flags); 634 if (error) 635 return error; 636 } 637 if (sc->sc_riscbuf) { 638 kmem_free(sc->sc_riscbuf, ch->csc_riscsz); 639 } 640 641 return 0; 642 } 643 644 static void 645 coram_dtv_get_devinfo(void *cookie, struct dvb_frontend_info *info) 646 { 647 struct coram_softc *sc = cookie; 648 649 memset(info, 0, sizeof(*info)); 650 strlcpy(info->name, sc->sc_board->name, sizeof(info->name)); 651 info->type = FE_ATSC; 652 info->frequency_min = 54000000; 653 info->frequency_max = 858000000; 654 info->frequency_stepsize = 62500; 655 info->caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB; 656 } 657 658 static int 659 coram_dtv_open(void *cookie, int flags) 660 { 661 struct coram_softc *sc = cookie; 662 663 #ifdef CORAM_DEBUG 664 device_printf(sc->sc_dev, "%s\n", __func__); 665 #endif 666 667 //KASSERT(sc->sc_tsbuf == NULL); 668 669 if (sc->sc_tuner == NULL || sc->sc_demod == NULL) 670 return ENXIO; 671 672 coram_mpeg_reset(sc); 673 674 /* allocate two alternating DMA areas for MPEG TS packets */ 675 sc->sc_tsbuf = coram_mpeg_malloc(sc, CORAM_TS_PKTSIZE * 2); 676 677 if (sc->sc_tsbuf == NULL) 678 return ENOMEM; 679 680 return 0; 681 } 682 683 static void 684 coram_dtv_close(void *cookie) 685 { 686 struct coram_softc *sc = cookie; 687 688 #ifdef CORAM_DEBUG 689 device_printf(sc->sc_dev, "%s\n", __func__); 690 #endif 691 692 coram_mpeg_halt(sc); 693 694 if (sc->sc_tsbuf != NULL) { 695 coram_mpeg_free(sc, sc->sc_tsbuf); 696 sc->sc_tsbuf = NULL; 697 } 698 } 699 700 static int 701 coram_dtv_set_tuner(void *cookie, const struct dvb_frontend_parameters *params) 702 { 703 struct coram_softc *sc = cookie; 704 705 KASSERT(sc->sc_tuner != NULL); 706 mt2131_tune_dtv(sc->sc_tuner, params); 707 KASSERT(sc->sc_demod != NULL); 708 return cx24227_set_modulation(sc->sc_demod, params->u.vsb.modulation); 709 } 710 711 static fe_status_t 712 coram_dtv_get_status(void *cookie) 713 { 714 struct coram_softc *sc = cookie; 715 716 if (sc->sc_demod == NULL) 717 return ENXIO; 718 719 return cx24227_get_dtv_status(sc->sc_demod);; 720 } 721 722 static uint16_t 723 coram_dtv_get_signal_strength(void *cookie) 724 { 725 return 0; 726 } 727 728 static uint16_t 729 coram_dtv_get_snr(void *cookie) 730 { 731 return 0; 732 } 733 734 static int 735 coram_dtv_start_transfer(void *cookie, 736 void (*cb)(void *, const struct dtv_payload *), void *arg) 737 { 738 struct coram_softc *sc = cookie; 739 740 #ifdef CORAM_DEBUG 741 device_printf(sc->sc_dev, "%s\n", __func__); 742 #endif 743 744 sc->sc_dtvsubmitcb = cb; 745 sc->sc_dtvsubmitarg = arg; 746 747 coram_mpeg_trigger(sc, sc->sc_tsbuf); 748 749 return 0; 750 } 751 752 static int 753 coram_dtv_stop_transfer(void *cookie) 754 { 755 struct coram_softc *sc = cookie; 756 757 #ifdef CORAM_DEBUG 758 device_printf(sc->sc_dev, "%s\n", __func__); 759 #endif 760 761 coram_mpeg_halt(sc); 762 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK, 0); 763 764 sc->sc_dtvsubmitcb = NULL; 765 sc->sc_dtvsubmitarg = NULL; 766 767 return 0; 768 } 769 770 771 static int 772 coram_mpeg_reset(struct coram_softc *sc) 773 { 774 /* hold RISC in reset */ 775 bus_space_write_4(sc->sc_memt, sc->sc_memh, DEV_CNTRL2, 0); 776 777 /* disable fifo + risc */ 778 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_DMA_CTL, 0); 779 780 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK, 0); 781 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK, 0); 782 783 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_STAT, 0); 784 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_INT_STAT, 0); 785 786 memset(sc->sc_riscbuf, 0, sc->sc_riscbufsz); 787 788 return 0; 789 } 790 791 static void * 792 coram_mpeg_malloc(struct coram_softc *sc, size_t size) 793 { 794 struct coram_dma *p; 795 int err; 796 797 p = kmem_alloc(sizeof(struct coram_dma), KM_SLEEP); 798 if ( p == NULL ) 799 return NULL; 800 err = coram_allocmem(sc, size, 16, p); 801 if (err) { 802 kmem_free(p, sizeof(struct coram_dma)); 803 return NULL; 804 } 805 806 p->next = sc->sc_dma; 807 sc->sc_dma = p; 808 809 return KERNADDR(p); 810 } 811 812 static int 813 coram_allocmem(struct coram_softc *sc, size_t size, size_t align, 814 struct coram_dma *p) 815 { 816 int err; 817 818 p->size = size; 819 err = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0, 820 p->segs, sizeof(p->segs) / sizeof(p->segs[0]), 821 &p->nsegs, BUS_DMA_NOWAIT); 822 if (err) 823 return err; 824 err = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size, 825 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 826 if (err) 827 goto free; 828 err = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0, 829 BUS_DMA_NOWAIT, &p->map); 830 if (err) 831 goto unmap; 832 err = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL, 833 BUS_DMA_NOWAIT); 834 if (err) 835 goto destroy; 836 837 return 0; 838 destroy: 839 bus_dmamap_destroy(sc->sc_dmat, p->map); 840 unmap: 841 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 842 free: 843 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); 844 845 return err; 846 } 847 848 static int 849 coram_mpeg_halt(struct coram_softc *sc) 850 { 851 uint32_t v; 852 853 #ifdef CORAM_DEBUG 854 device_printf(sc->sc_dev, "%s\n", __func__); 855 #endif 856 857 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_DMA_CTL, 0); 858 859 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK); 860 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK, 861 v & __BIT(2)); 862 863 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK); 864 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK, 865 v & 0); 866 867 return 0; 868 } 869 870 static void 871 coram_mpeg_free(struct coram_softc *sc, void *addr) 872 { 873 struct coram_dma *p; 874 struct coram_dma **pp; 875 876 for (pp = &sc->sc_dma; (p = *pp) != NULL; pp = &p->next) 877 if (KERNADDR(p) == addr) { 878 coram_freemem(sc, p); 879 *pp = p->next; 880 kmem_free(p, sizeof(struct coram_dma)); 881 return; 882 } 883 884 printf("%s: %p is already free\n", device_xname(sc->sc_dev), addr); 885 return; 886 } 887 888 static int 889 coram_freemem(struct coram_softc *sc, struct coram_dma *p) 890 { 891 bus_dmamap_unload(sc->sc_dmat, p->map); 892 bus_dmamap_destroy(sc->sc_dmat, p->map); 893 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); 894 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs); 895 896 return 0; 897 } 898 899 static int 900 coram_mpeg_trigger(struct coram_softc *sc, void *buf) 901 { 902 struct coram_dma *p; 903 struct coram_sram_ch *ch; 904 uint32_t v; 905 906 ch = &coram_sram_chs[CORAM_SRAM_CH6]; 907 908 for (p = sc->sc_dma; p && KERNADDR(p) != buf; p = p->next) 909 continue; 910 if (p == NULL) { 911 printf("%s: coram_mpeg_trigger: bad addr %p\n", 912 device_xname(sc->sc_dev), buf); 913 return ENOENT; 914 } 915 916 /* disable fifo + risc */ 917 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_DMA_CTL, 0); 918 919 coram_risc_buffer(sc, CORAM_TS_PKTSIZE, 1); 920 coram_sram_ch_setup(sc, ch, CORAM_TS_PKTSIZE); 921 922 /* let me hope this bit is the same as on the 2388[0-3] */ 923 /* software reset */ 924 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_GEN_CTL, 0x0040); 925 delay (100*1000); 926 927 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_LNGTH, CORAM_TS_PKTSIZE); 928 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_HW_SOP_CTL, 0x47 << 16 | 188 << 4); 929 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_TS_CLK_EN, 1); 930 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_VLD_MISC, 0); 931 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_GEN_CTL, 12); 932 delay (100*1000); 933 934 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, PAD_CTRL); 935 v &= ~0x4; /* Clear TS2_SOP_OE */ 936 bus_space_write_4(sc->sc_memt, sc->sc_memh, PAD_CTRL, v); 937 938 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK); 939 v |= 0x111111; 940 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_INT_MSK, v); 941 942 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_DMA_CTL); 943 v |= 0x11; /* Enable RISC controller and FIFO */ 944 bus_space_write_4(sc->sc_memt, sc->sc_memh, VID_C_DMA_CTL, v); 945 946 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, DEV_CNTRL2); 947 v |= __BIT(5); /* Enable RISC controller */ 948 bus_space_write_4(sc->sc_memt, sc->sc_memh, DEV_CNTRL2, v); 949 950 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK); 951 v |= 0x001f00; 952 v |= 0x04; 953 bus_space_write_4(sc->sc_memt, sc->sc_memh, PCI_INT_MSK, v); 954 955 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_GEN_CTL); 956 #ifdef CORAM_DEBUG 957 printf("%s, %06x %08x\n", __func__, VID_C_GEN_CTL, v); 958 #endif 959 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_SOP_STATUS); 960 #ifdef CORAM_DEBUG 961 printf("%s, %06x %08x\n", __func__, VID_C_SOP_STATUS, v); 962 #endif 963 delay(100*1000); 964 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_GEN_CTL); 965 #ifdef CORAM_DEBUG 966 printf("%s, %06x %08x\n", __func__, VID_C_GEN_CTL, v); 967 #endif 968 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, VID_C_SOP_STATUS); 969 #ifdef CORAM_DEBUG 970 printf("%s, %06x %08x\n", __func__, VID_C_SOP_STATUS, v); 971 #endif 972 973 return 0; 974 } 975 976 static int 977 coram_risc_buffer(struct coram_softc *sc, uint32_t bpl, uint32_t lines) 978 { 979 uint32_t *rm; 980 uint32_t size; 981 982 size = 1 + (bpl * lines) / PAGE_SIZE + lines; 983 size += 2; 984 985 if (sc->sc_riscbuf == NULL) { 986 return ENOMEM; 987 } 988 989 rm = (uint32_t *)sc->sc_riscbuf; 990 coram_risc_field(sc, rm, bpl); 991 992 return 0; 993 } 994 995 static int 996 coram_risc_field(struct coram_softc *sc, uint32_t *rm, uint32_t bpl) 997 { 998 struct coram_dma *p; 999 1000 for (p = sc->sc_dma; p && KERNADDR(p) != sc->sc_tsbuf; p = p->next) 1001 continue; 1002 if (p == NULL) { 1003 printf("%s: coram_risc_field: bad addr %p\n", 1004 device_xname(sc->sc_dev), sc->sc_tsbuf); 1005 return ENOENT; 1006 } 1007 1008 memset(sc->sc_riscbuf, 0, sc->sc_riscbufsz); 1009 1010 rm = sc->sc_riscbuf; 1011 1012 /* htole32 will be done when program is copied to chip sram */ 1013 1014 /* XXX */ 1015 *(rm++) = (CX_RISC_SYNC|0); 1016 1017 *(rm++) = (CX_RISC_WRITE|CX_RISC_SOL|CX_RISC_EOL|CX_RISC_IRQ1|bpl); 1018 *(rm++) = (DMAADDR(p) + 0 * bpl); 1019 *(rm++) = 0; /* high dword */ 1020 1021 *(rm++) = (CX_RISC_WRITE|CX_RISC_SOL|CX_RISC_EOL|CX_RISC_IRQ2|bpl); 1022 *(rm++) = (DMAADDR(p) + 1 * bpl); 1023 *(rm++) = 0; 1024 1025 *(rm++) = (CX_RISC_JUMP|1); 1026 *(rm++) = (coram_sram_chs[CORAM_SRAM_CH6].csc_risc + 4); 1027 *(rm++) = 0; 1028 1029 return 0; 1030 } 1031 1032 static int 1033 coram_sram_ch_setup(struct coram_softc *sc, struct coram_sram_ch *csc, 1034 uint32_t bpl) 1035 { 1036 unsigned int i, lines; 1037 uint32_t cdt; 1038 1039 /* XXX why round? */ 1040 bpl = (bpl + 7) & ~7; 1041 cdt = csc->csc_cdt; 1042 lines = csc->csc_fifosz / bpl; 1043 #ifdef CORAM_DEBUG 1044 printf("%s %d lines\n", __func__, lines); 1045 #endif 1046 1047 /* fill in CDT */ 1048 for (i = 0; i < lines; i++) { 1049 #ifdef CORAM_DEBUG 1050 printf("CDT ent %08x, %08x\n", cdt + (16 * i), 1051 csc->csc_fifo + (bpl * i)); 1052 #endif 1053 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1054 cdt + (16 * i), csc->csc_fifo + (bpl * i)); 1055 } 1056 1057 /* copy program */ 1058 /* converts program to little endian as it goes into sram */ 1059 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 1060 csc->csc_risc, (void *)sc->sc_riscbuf, sc->sc_riscbufsz >> 2); 1061 1062 /* fill in CMDS */ 1063 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1064 csc->csc_cmds + CMDS_O_IRPC, csc->csc_risc); 1065 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1066 csc->csc_cmds + CMDS_O_IRPC + 4, 0); 1067 1068 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1069 csc->csc_cmds + CMDS_O_CDTB, csc->csc_cdt); 1070 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1071 csc->csc_cmds + CMDS_O_CDTS, (lines * 16) >> 3); /* XXX magic */ 1072 1073 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1074 csc->csc_cmds + CMDS_O_IQB, csc->csc_iq); 1075 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1076 csc->csc_cmds + CMDS_O_IQS, 1077 CMDS_IQS_ISRP | (csc->csc_iqsz >> 2) ); 1078 1079 /* zero rest of CMDS */ 1080 bus_space_set_region_4(sc->sc_memt, sc->sc_memh, 0x18, 0, 20); 1081 1082 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1083 csc->csc_ptr1, csc->csc_fifo); 1084 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1085 csc->csc_ptr2, cdt); 1086 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1087 csc->csc_cnt2, (lines * 16) >> 3); 1088 bus_space_write_4(sc->sc_memt, sc->sc_memh, 1089 csc->csc_cnt1, (bpl >> 3) - 1); 1090 1091 return 0; 1092 } 1093 1094 MODULE(MODULE_CLASS_DRIVER, coram, "cx24227,mt2131,pci"); 1095 1096 #ifdef _MODULE 1097 #include "ioconf.c" 1098 #endif 1099 1100 static int 1101 coram_modcmd(modcmd_t cmd, void *v) 1102 { 1103 int error = 0; 1104 1105 switch (cmd) { 1106 case MODULE_CMD_INIT: 1107 #ifdef _MODULE 1108 error = config_init_component(cfdriver_ioconf_coram, 1109 cfattach_ioconf_coram, cfdata_ioconf_coram); 1110 #endif 1111 return error; 1112 case MODULE_CMD_FINI: 1113 #ifdef _MODULE 1114 error = config_fini_component(cfdriver_ioconf_coram, 1115 cfattach_ioconf_coram, cfdata_ioconf_coram); 1116 #endif 1117 return error; 1118 default: 1119 return ENOTTY; 1120 } 1121 } 1122