cxdtv.c revision 1.2 1 /* $NetBSD: cxdtv.c,v 1.2 2011/07/14 23:47:45 jmcneill 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: cxdtv.c,v 1.2 2011/07/14 23:47:45 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcidevs.h>
44 #include <dev/i2c/i2cvar.h>
45 #include <dev/i2c/i2c_bitbang.h>
46
47 #include <dev/i2c/tvpllvar.h>
48 #include <dev/i2c/tvpll_tuners.h>
49
50 #include <dev/i2c/nxt2kvar.h>
51 #include <dev/i2c/lg3303var.h>
52
53 #include <dev/pci/cxdtvreg.h>
54 #include <dev/pci/cxdtvvar.h>
55 #include <dev/pci/cxdtv_boards.h>
56
57 #include <dev/dtv/dtvif.h>
58
59 #define CXDTV_MMBASE 0x10
60
61 #define CXDTV_SRAM_CH_MPEG 0
62 #define CXDTV_TS_PKTSIZE (188 * 8)
63
64
65 static int cxdtv_match(struct device *, struct cfdata *, void *);
66 static void cxdtv_attach(struct device *, struct device *, void *);
67 static int cxdtv_detach(struct device *, int);
68 static int cxdtv_intr(void *);
69
70 static bool cxdtv_resume(device_t, const pmf_qual_t *);
71
72 static int cxdtv_iic_acquire_bus(void *, int);
73 static void cxdtv_iic_release_bus(void *, int);
74 static int cxdtv_iic_send_start(void *, int);
75 static int cxdtv_iic_send_stop(void *, int);
76 static int cxdtv_iic_initiate_xfer(void *, i2c_addr_t, int);
77 static int cxdtv_iic_read_byte(void *, uint8_t *, int);
78 static int cxdtv_iic_write_byte(void *, uint8_t, int);
79
80 static void cxdtv_i2cbb_set_bits(void *, uint32_t);
81 static void cxdtv_i2cbb_set_dir(void *, uint32_t);
82 static uint32_t cxdtv_i2cbb_read_bits(void *);
83
84 static int cxdtv_sram_ch_setup(struct cxdtv_softc *,
85 struct cxdtv_sram_ch *, uint32_t);
86 static int cxdtv_allocmem(struct cxdtv_softc *, size_t, size_t,
87 struct cxdtv_dma *);
88 static int cxdtv_freemem(struct cxdtv_softc *, struct cxdtv_dma *);
89 static int cxdtv_risc_buffer(struct cxdtv_softc *, uint32_t, uint32_t);
90 static int cxdtv_risc_field(struct cxdtv_softc *, uint32_t *, uint32_t);
91
92 static int cxdtv_mpeg_attach(struct cxdtv_softc *);
93 static int cxdtv_mpeg_intr(struct cxdtv_softc *);
94 static int cxdtv_mpeg_reset(struct cxdtv_softc *);
95
96 static int cxdtv_mpeg_trigger(struct cxdtv_softc *, void *);
97 static int cxdtv_mpeg_halt(struct cxdtv_softc *);
98 static void * cxdtv_mpeg_malloc(struct cxdtv_softc *, size_t);
99 static void cxdtv_mpeg_free(struct cxdtv_softc *, void *);
100
101 static void cxdtv_card_init_hd5500(struct cxdtv_softc *);
102 static void cxdtv_card_init_hdtvwonder(struct cxdtv_softc *);
103
104 const struct i2c_bitbang_ops cxdtv_i2cbb_ops = {
105 cxdtv_i2cbb_set_bits,
106 cxdtv_i2cbb_set_dir,
107 cxdtv_i2cbb_read_bits,
108 { CXDTV_I2C_C_DATACONTROL_SDA, CXDTV_I2C_C_DATACONTROL_SCL, 0, 0 }
109 };
110
111 /* Maybe make this dynamically allocated. */
112 static struct cxdtv_sram_ch cxdtv_sram_chs[] = {
113 [CXDTV_SRAM_CH_MPEG] = {
114 .csc_cmds = 0x180200, /* CMDS for ch. 28 */
115 .csc_iq = 0x180340, /* after last CMDS */
116 .csc_iqsz = 0x40, /* 16 dwords */
117 .csc_cdt = 0x180380, /* after iq */
118 .csc_cdtsz = 0x40, /* cluster discriptor space */
119 .csc_fifo = 0x180400, /* after cdt */
120 .csc_fifosz = 0x001C00, /* let's just align this up */
121 .csc_risc = 0x182000, /* after fifo */
122 .csc_riscsz = 0x6000, /* room for dma programs */
123 .csc_ptr1 = CXDTV_DMA28_PTR1,
124 .csc_ptr2 = CXDTV_DMA28_PTR2,
125 .csc_cnt1 = CXDTV_DMA28_CNT1,
126 .csc_cnt2 = CXDTV_DMA28_CNT2,
127 },
128 };
129
130 CFATTACH_DECL_NEW(cxdtv, sizeof(struct cxdtv_softc),
131 cxdtv_match, cxdtv_attach, cxdtv_detach, NULL);
132
133 static int
134 cxdtv_match(device_t parent, cfdata_t match, void *aux)
135 {
136 const struct pci_attach_args *pa;
137
138 pa = aux;
139
140 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CONEXANT)
141 return 0;
142
143 switch (PCI_PRODUCT(pa->pa_id)) {
144 case PCI_PRODUCT_CONEXANT_CX2388XMPEG:
145 return 1;
146 }
147
148 /* XXX only match supported boards */
149
150 return 0;
151 }
152
153 static void
154 cxdtv_attach(device_t parent, device_t self, void *aux)
155 {
156 struct cxdtv_softc *sc;
157 const struct pci_attach_args *pa = aux;
158 pci_intr_handle_t ih;
159 pcireg_t reg;
160 const char *intrstr;
161 char devinfo[76];
162 struct i2cbus_attach_args iba;
163
164 sc = device_private(self);
165
166 sc->sc_dev = self;
167
168 aprint_naive("\n");
169
170 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
171
172 sc->sc_vendor = PCI_VENDOR(reg);
173 sc->sc_product = PCI_PRODUCT(reg);
174
175 sc->sc_board = cxdtv_board_lookup(sc->sc_vendor, sc->sc_product);
176
177 if (sc->sc_board == NULL) {
178 aprint_error_dev(self ,"unsupported device 0x%08x\n", reg);
179 return;
180 }
181
182 pci_devinfo(reg, pa->pa_class, 0, devinfo, sizeof(devinfo));
183 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
184
185 if (pci_mapreg_map(pa, CXDTV_MMBASE, PCI_MAPREG_TYPE_MEM, 0,
186 &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems)) {
187 aprint_error_dev(self, "couldn't map memory space\n");
188 return;
189 }
190
191 sc->sc_dmat = pa->pa_dmat;
192
193 if (pci_intr_map(pa, &ih)) {
194 aprint_error_dev(self, "couldn't map interrupt\n");
195 return;
196 }
197 intrstr = pci_intr_string(pa->pa_pc, ih);
198 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_VM, cxdtv_intr, sc);
199 if (sc->sc_ih == NULL) {
200 aprint_error_dev(self, "couldn't establish interrupt");
201 if (intrstr != NULL)
202 aprint_error(" at %s", intrstr);
203 aprint_error("\n");
204 return;
205 }
206 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
207
208 /* set master */
209 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
210 reg |= PCI_COMMAND_MASTER_ENABLE;
211 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
212
213 mutex_init(&sc->sc_delaylock, MUTEX_DEFAULT, IPL_NONE);
214 cv_init(&sc->sc_delaycv, "cxdtvwait");
215
216 mutex_init(&sc->sc_i2c_buslock, MUTEX_DRIVER, IPL_NONE);
217 sc->sc_i2c.ic_cookie = sc;
218 sc->sc_i2c.ic_exec = NULL;
219 sc->sc_i2c.ic_acquire_bus = cxdtv_iic_acquire_bus;
220 sc->sc_i2c.ic_release_bus = cxdtv_iic_release_bus;
221 sc->sc_i2c.ic_send_start = cxdtv_iic_send_start;
222 sc->sc_i2c.ic_send_stop = cxdtv_iic_send_stop;
223 sc->sc_i2c.ic_initiate_xfer = cxdtv_iic_initiate_xfer;
224 sc->sc_i2c.ic_read_byte = cxdtv_iic_read_byte;
225 sc->sc_i2c.ic_write_byte = cxdtv_iic_write_byte;
226
227 #if notyet
228 /* enable i2c compatible software mode */
229 val = bus_space_read_4(sc->sc_memt, sc->sc_memh,
230 CXDTV_I2C_C_DATACONTROL);
231 val = CXDTV_I2C_C_DATACONTROL_SCL | CXDTV_I2C_C_DATACONTROL_SDA;
232 bus_space_write_4(sc->sc_memt, sc->sc_memh,
233 CXDTV_I2C_C_DATACONTROL, val);
234 #endif
235
236 cxdtv_mpeg_attach(sc);
237
238 /* attach other devices to iic(4) */
239 memset(&iba, 0, sizeof(iba));
240 iba.iba_tag = &sc->sc_i2c;
241 config_found_ia(self, "i2cbus", &iba, iicbus_print);
242
243 if (!pmf_device_register(self, NULL, cxdtv_resume))
244 aprint_error_dev(self, "couldn't establish power handler\n");
245
246 return;
247 }
248
249 static int
250 cxdtv_detach(device_t self, int flags)
251 {
252 return EBUSY;
253 }
254
255 static bool
256 cxdtv_resume(device_t dv, const pmf_qual_t *qual)
257 {
258 struct cxdtv_softc *sc;
259 sc = device_private(dv);
260
261 /* XXX revisit */
262
263 aprint_debug_dev(dv, "%s\n", __func__);
264
265 return true;
266 }
267
268 static int
269 cxdtv_intr(void *intarg)
270 {
271 struct cxdtv_softc *sc = intarg;
272 uint32_t val;
273
274 val = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MSTAT);
275 if (val == 0) {
276 return 0; /* not ours */
277 }
278
279 if (val & CXT_PI_TS_INT) {
280 cxdtv_mpeg_intr(sc);
281 }
282
283 if (val & ~CXT_PI_TS_INT) {
284 device_printf(sc->sc_dev, "%s, %08x\n", __func__, val);
285 }
286
287 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_STAT, val);
288
289 return 1;
290 }
291
292 /* I2C interface */
293
294 static void
295 cxdtv_i2cbb_set_bits(void *cookie, uint32_t bits)
296 {
297 struct cxdtv_softc *sc = cookie;
298 uint32_t value;
299
300 bus_space_write_4(sc->sc_memt, sc->sc_memh,
301 CXDTV_I2C_C_DATACONTROL, bits);
302 value = bus_space_read_4(sc->sc_memt, sc->sc_memh,
303 CXDTV_I2C_C_DATACONTROL);
304
305 return;
306 }
307
308 static void
309 cxdtv_i2cbb_set_dir(void *cookie, uint32_t bits)
310 {
311 return;
312 }
313
314 static uint32_t
315 cxdtv_i2cbb_read_bits(void *cookie)
316 {
317 struct cxdtv_softc *sc = cookie;
318 uint32_t value;
319
320 value = bus_space_read_4(sc->sc_memt, sc->sc_memh,
321 CXDTV_I2C_C_DATACONTROL);
322
323 return value;
324 }
325
326 static int
327 cxdtv_iic_acquire_bus(void *cookie, int flags)
328 {
329 struct cxdtv_softc *sc = cookie;
330
331 mutex_enter(&sc->sc_i2c_buslock);
332
333 return 0;
334 }
335
336 static void
337 cxdtv_iic_release_bus(void *cookie, int flags)
338 {
339 struct cxdtv_softc *sc = cookie;
340
341 mutex_exit(&sc->sc_i2c_buslock);
342
343 return;
344 }
345
346 static int
347 cxdtv_iic_send_start(void *cookie, int flags)
348 {
349 return i2c_bitbang_send_start(cookie, flags, &cxdtv_i2cbb_ops);
350 }
351
352 static int
353 cxdtv_iic_send_stop(void *cookie, int flags)
354 {
355 return i2c_bitbang_send_stop(cookie, flags, &cxdtv_i2cbb_ops);
356 }
357
358 static int
359 cxdtv_iic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
360 {
361 return i2c_bitbang_initiate_xfer(cookie, addr, flags, &cxdtv_i2cbb_ops);
362 }
363
364 static int
365 cxdtv_iic_read_byte(void *cookie, uint8_t *data, int flags)
366 {
367 return i2c_bitbang_read_byte(cookie, data, flags, &cxdtv_i2cbb_ops);
368 }
369
370 static int
371 cxdtv_iic_write_byte(void *cookie, uint8_t data, int flags)
372 {
373 return i2c_bitbang_write_byte(cookie, data, flags, &cxdtv_i2cbb_ops);
374 }
375
376 /* MPEG TS Port */
377
378 static void cxdtv_dtv_get_devinfo(void *, struct dvb_frontend_info *);
379 static int cxdtv_dtv_open(void *, int);
380 static void cxdtv_dtv_close(void *);
381 static int cxdtv_dtv_set_tuner(void *, const struct dvb_frontend_parameters *);
382 static fe_status_t cxdtv_dtv_get_status(void *);
383 static uint16_t cxdtv_dtv_get_signal_strength(void *);
384 static uint16_t cxdtv_dtv_get_snr(void *);
385 static int cxdtv_dtv_start_transfer(void *);
386 static int cxdtv_dtv_stop_transfer(void *);
387
388 static const struct dtv_hw_if cxdtv_dtv_if = {
389 .get_devinfo = cxdtv_dtv_get_devinfo,
390 .open = cxdtv_dtv_open,
391 .close = cxdtv_dtv_close,
392 .set_tuner = cxdtv_dtv_set_tuner,
393 .get_status = cxdtv_dtv_get_status,
394 .get_signal_strength = cxdtv_dtv_get_signal_strength,
395 .get_snr = cxdtv_dtv_get_snr,
396 .start_transfer = cxdtv_dtv_start_transfer,
397 .stop_transfer = cxdtv_dtv_stop_transfer,
398 };
399
400 int
401 cxdtv_mpeg_attach(struct cxdtv_softc *sc)
402 {
403 struct dtv_attach_args daa;
404 struct cxdtv_sram_ch *ch;
405
406 CX_DPRINTF(("cxdtv_mpeg_attach\n"));
407
408 ch = &cxdtv_sram_chs[CXDTV_SRAM_CH_MPEG];
409
410 sc->sc_riscbufsz = ch->csc_riscsz;
411 sc->sc_riscbuf = kmem_alloc(ch->csc_riscsz, KM_SLEEP);
412
413 if ( sc->sc_riscbuf == NULL )
414 panic("riscbuf null");
415
416 aprint_debug_dev(sc->sc_dev, "attaching frontend...\n");
417
418 switch(sc->sc_vendor) {
419 case PCI_VENDOR_ATI:
420 cxdtv_card_init_hdtvwonder(sc);
421 break;
422 case PCI_VENDOR_PCHDTV:
423 if (sc->sc_product == PCI_PRODUCT_PCHDTV_HD5500) {
424 cxdtv_card_init_hd5500(sc);
425 }
426 break;
427 }
428
429 KASSERT(sc->sc_tuner == NULL);
430 KASSERT(sc->sc_demod == NULL);
431
432 switch(sc->sc_board->cb_demod) {
433 case CXDTV_DEMOD_NXT2004:
434 sc->sc_demod = nxt2k_open(sc->sc_dev, &sc->sc_i2c, 0x0a, 0);
435 break;
436 case CXDTV_DEMOD_LG3303:
437 sc->sc_demod = lg3303_open(sc->sc_dev, &sc->sc_i2c, 0x59,
438 LG3303_CFG_SERIAL_INPUT);
439 break;
440 default:
441 break;
442 }
443
444 switch(sc->sc_board->cb_tuner) {
445 case CXDTV_TUNER_PLL:
446 if (sc->sc_vendor == PCI_VENDOR_ATI)
447 sc->sc_tuner = tvpll_open(sc->sc_dev, &sc->sc_i2c, 0x61, &tvpll_tuv1236d_pll);
448 if (sc->sc_vendor == PCI_VENDOR_PCHDTV)
449 sc->sc_tuner = tvpll_open(sc->sc_dev, &sc->sc_i2c, 0x61, &tvpll_tdvs_h06xf_pll);
450 break;
451 default:
452 break;
453 }
454
455 KASSERT(sc->sc_tuner != NULL);
456 KASSERT(sc->sc_demod != NULL);
457
458 daa.hw = &cxdtv_dtv_if;
459 daa.priv = sc;
460
461 sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus", &daa, dtv_print);
462
463 return (sc->sc_dtvdev != NULL);
464 }
465
466 static void
467 cxdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
468 {
469 memset(info, 0, sizeof(*info));
470 strlcpy(info->name, "CX23880", sizeof(info->name));
471 info->type = FE_ATSC;
472 info->frequency_min = 54000000;
473 info->frequency_max = 858000000;
474 info->frequency_stepsize = 62500;
475 info->caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB;
476 }
477
478 static int
479 cxdtv_dtv_open(void *priv, int flags)
480 {
481 struct cxdtv_softc *sc = priv;
482
483 KASSERT(sc->sc_tsbuf == NULL);
484
485 cxdtv_mpeg_reset(sc);
486
487 printf("sc_dma %p\n", sc->sc_dma);
488 printf("sc_tsbuf %p\n", sc->sc_tsbuf);
489 /* allocate two alternating DMA areas for MPEG TS packets */
490 sc->sc_tsbuf = cxdtv_mpeg_malloc(sc, CXDTV_TS_PKTSIZE * 2);
491 printf("sc_dma %p\n", sc->sc_dma);
492 printf("sc_tsbuf %p\n", sc->sc_tsbuf);
493
494 if (sc->sc_tsbuf == NULL)
495 return EIO;
496
497 return 0;
498 }
499
500 static void
501 cxdtv_dtv_close(void *priv)
502 {
503 struct cxdtv_softc *sc = priv;
504
505 cxdtv_dtv_stop_transfer(sc);
506
507 if (sc->sc_tsbuf != NULL) {
508 cxdtv_mpeg_free(sc, sc->sc_tsbuf);
509 sc->sc_tsbuf = NULL;
510 }
511 }
512
513 static int
514 cxdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
515 {
516 struct cxdtv_softc *sc = priv;
517 int error = -1;
518
519 switch(sc->sc_board->cb_tuner) {
520 case CXDTV_TUNER_PLL:
521 error = tvpll_tune_dtv(sc->sc_tuner, params);
522 }
523 if (error)
524 goto bad;
525
526 switch(sc->sc_board->cb_demod) {
527 case CXDTV_DEMOD_NXT2004:
528 error = nxt2k_set_modulation(sc->sc_demod, params->u.vsb.modulation);
529 break;
530 case CXDTV_DEMOD_LG3303:
531 error = lg3303_set_modulation(sc->sc_demod, params->u.vsb.modulation);
532 break;
533 default:
534 break;
535 }
536
537 bad:
538 return error;
539 }
540
541 static fe_status_t
542 cxdtv_dtv_get_status(void *priv)
543 {
544 struct cxdtv_softc *sc = priv;
545
546 switch(sc->sc_board->cb_demod) {
547 case CXDTV_DEMOD_NXT2004:
548 return nxt2k_get_dtv_status(sc->sc_demod);
549 case CXDTV_DEMOD_LG3303:
550 return lg3303_get_dtv_status(sc->sc_demod);
551 default:
552 return 0;
553 }
554 }
555
556 static uint16_t
557 cxdtv_dtv_get_signal_strength(void *priv)
558 {
559 return 27;
560 }
561
562 static uint16_t
563 cxdtv_dtv_get_snr(void *priv)
564 {
565 return 42;
566 }
567
568 static int
569 cxdtv_dtv_start_transfer(void *priv)
570 {
571 struct cxdtv_softc *sc = priv;
572
573 printf("sc_dma %p\n", sc->sc_dma);
574 printf("sc_tsbuf %p\n", sc->sc_tsbuf);
575 /* allocate two alternating DMA areas for MPEG TS packets */
576 sc->sc_tsbuf = cxdtv_mpeg_malloc(sc, CXDTV_TS_PKTSIZE * 2);
577 printf("sc_dma %p\n", sc->sc_dma);
578 printf("sc_tsbuf %p\n", sc->sc_tsbuf);
579
580 printf("KERNADDR %p, DMAADDR %016lx\n", KERNADDR(sc->sc_dma), DMAADDR(sc->sc_dma));
581
582 cxdtv_mpeg_trigger(sc, sc->sc_tsbuf);
583
584 return 0;
585 }
586
587 static int
588 cxdtv_dtv_stop_transfer(void *priv)
589 {
590 struct cxdtv_softc *sc = priv;
591
592 cxdtv_mpeg_halt(sc);
593 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK, 0);
594
595 return 0;
596 }
597
598 int
599 cxdtv_mpeg_reset(struct cxdtv_softc *sc)
600 {
601 struct cxdtv_sram_ch *ch;
602 uint32_t v;
603
604 CX_DPRINTF(("cxdtv_mpeg_reset\n"));
605
606 ch = &cxdtv_sram_chs[CXDTV_SRAM_CH_MPEG];
607 v = (uint32_t)-1;
608
609 /* shutdown */
610 /* hold RISC in reset */
611 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_DEV_CNTRL2, 0);
612 /* disable FIFO and RISC */
613 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_DMA_CNTRL, 0);
614 /* mask off all interrupts */
615 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK, 0);
616 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK, 0);
617
618 /* clear interrupts */
619 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_STAT, v);
620 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_STAT, v);
621
622 memset(sc->sc_riscbuf, 0, sc->sc_riscbufsz);
623
624 /* XXX magic */
625 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PDMA_STHRSH, 0x0707);
626 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PDMA_DTHRSH, 0x0707);
627
628 /* reset external components*/
629 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_SRST_IO, 0);
630 mutex_enter(&sc->sc_delaylock);
631 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, MAX(1, mstohz(1)));
632 mutex_exit(&sc->sc_delaylock);
633 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_SRST_IO, 1);
634
635 /* let error interrupts happen */
636 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK);
637 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK,
638 v | 0x00fc00); /* XXX magic */
639
640 return 0;
641 }
642
643 static int
644 cxdtv_risc_buffer(struct cxdtv_softc *sc, uint32_t bpl, uint32_t lines)
645 {
646 uint32_t *rm;
647 uint32_t size;
648
649 CX_DPRINTF(("cxdtv_risc_buffer: bpl=0x%x\n", bpl));
650
651 size = 1 + (bpl * lines) / PAGE_SIZE + lines;
652 size += 2;
653
654 device_printf(sc->sc_dev, "%s: est. inst. %d\n", __func__, size);
655
656 size *= 8;
657 device_printf(sc->sc_dev, "%s: est. qword %d\n", __func__, size);
658
659 if (sc->sc_riscbuf == NULL) {
660 device_printf(sc->sc_dev, "not enough memory for RISC\n");
661 return ENOMEM;
662 }
663
664 rm = (uint32_t *)sc->sc_riscbuf;
665 cxdtv_risc_field(sc, rm, bpl);
666
667 return 0;
668 }
669
670 static int
671 cxdtv_risc_field(struct cxdtv_softc *sc, uint32_t *rm, uint32_t bpl)
672 {
673 struct cxdtv_dma *p;
674
675 CX_DPRINTF(("cxdtv_risc_field: bpl=0x%x\n", bpl));
676
677 for (p = sc->sc_dma; p && KERNADDR(p) != sc->sc_tsbuf; p = p->next)
678 continue;
679 if (p == NULL) {
680 device_printf(sc->sc_dev, "cxdtv_risc_field: bad addr %p\n",
681 sc->sc_tsbuf);
682 return ENOENT;
683 }
684
685 memset(sc->sc_riscbuf, 0, sc->sc_riscbufsz);
686
687 rm = sc->sc_riscbuf;
688
689 /* htole32 will be done when program is copied to chip sram */
690
691 /* XXX */
692 *(rm++) = (CX_RISC_SYNC|0);
693
694 *(rm++) = (CX_RISC_WRITE|CX_RISC_SOL|CX_RISC_EOL|CX_RISC_IRQ1|bpl);
695 *(rm++) = (DMAADDR(p) + 0 * bpl);
696
697 *(rm++) = (CX_RISC_WRITE|CX_RISC_SOL|CX_RISC_EOL|CX_RISC_IRQ2|bpl);
698 *(rm++) = (DMAADDR(p) + 1 * bpl);
699
700 *(rm++) = (CX_RISC_JUMP|1);
701 *(rm++) = (cxdtv_sram_chs[CXDTV_SRAM_CH_MPEG].csc_risc + 4);
702
703 return 0;
704 }
705
706 static int
707 cxdtv_sram_ch_setup(struct cxdtv_softc *sc, struct cxdtv_sram_ch *csc,
708 uint32_t bpl)
709 {
710 unsigned int i, lines;
711 uint32_t cdt;
712
713 CX_DPRINTF(("cxdtv_sram_ch_setup: bpl=0x%x\n", bpl));
714
715 /* XXX why round? */
716 bpl = (bpl + 7) & ~7;
717 CX_DPRINTF(("cxdtv_sram_ch_setup: bpl=0x%x\n", bpl));
718 cdt = csc->csc_cdt;
719 lines = csc->csc_fifosz / bpl;
720 device_printf(sc->sc_dev, "%s %d lines\n", __func__, lines);
721
722 /* fill in CDT */
723 for (i = 0; i < lines; i++) {
724 CX_DPRINTF(("CDT ent %08x, %08x\n", cdt + (16 * i),
725 csc->csc_fifo + (bpl * i)));
726 bus_space_write_4(sc->sc_memt, sc->sc_memh,
727 cdt + (16 * i),
728 csc->csc_fifo + (bpl * i));
729 }
730
731 /* copy DMA program */
732
733 /* converts program to little endian as it goes into SRAM */
734 bus_space_write_region_4(sc->sc_memt, sc->sc_memh,
735 csc->csc_risc, (void *)sc->sc_riscbuf, sc->sc_riscbufsz >> 2);
736
737 /* fill in CMDS */
738 bus_space_write_4(sc->sc_memt, sc->sc_memh,
739 csc->csc_cmds + CX_CMDS_O_IRPC, csc->csc_risc);
740
741 bus_space_write_4(sc->sc_memt, sc->sc_memh,
742 csc->csc_cmds + CX_CMDS_O_CDTB, csc->csc_cdt);
743 bus_space_write_4(sc->sc_memt, sc->sc_memh,
744 csc->csc_cmds + CX_CMDS_O_CDTS, (lines * 16) >> 3); /* XXX magic */
745
746 bus_space_write_4(sc->sc_memt, sc->sc_memh,
747 csc->csc_cmds + CX_CMDS_O_IQB, csc->csc_iq);
748 bus_space_write_4(sc->sc_memt, sc->sc_memh,
749 csc->csc_cmds + CX_CMDS_O_IQS,
750 CX_CMDS_IQS_ISRP | (csc->csc_iqsz >> 2) );
751
752 /* zero rest of CMDS */
753 bus_space_set_region_4(sc->sc_memt, sc->sc_memh, 0x14, 0, 0x2c/4);
754
755 bus_space_write_4(sc->sc_memt, sc->sc_memh,
756 csc->csc_cnt1, (bpl >> 3) - 1);
757
758 bus_space_write_4(sc->sc_memt, sc->sc_memh,
759 csc->csc_ptr2, cdt);
760 bus_space_write_4(sc->sc_memt, sc->sc_memh,
761 csc->csc_cnt2, (lines * 16) >> 3);
762
763 return 0;
764 }
765
766 int
767 cxdtv_mpeg_trigger(struct cxdtv_softc *sc, void *buf)
768 {
769 struct cxdtv_dma *p;
770 struct cxdtv_sram_ch *ch;
771 uint32_t v;
772 uint32_t offset;
773
774 ch = &cxdtv_sram_chs[CXDTV_SRAM_CH_MPEG];
775
776 for (p = sc->sc_dma; p && KERNADDR(p) != buf; p = p->next)
777 continue;
778 if (p == NULL) {
779 device_printf(sc->sc_dev, "cxdtv_mpeg_trigger: bad addr %p\n",
780 buf);
781 return ENOENT;
782 }
783
784 CX_DPRINTF(("cxdtv_mpeg_trigger: buf=%p\n", buf));
785
786 cxdtv_risc_buffer(sc, CXDTV_TS_PKTSIZE, 1);
787 cxdtv_sram_ch_setup(sc, ch, CXDTV_TS_PKTSIZE);
788
789 /* software reset */
790 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_GEN_CONTROL,
791 0x40);
792 mutex_enter(&sc->sc_delaylock);
793 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, mstohz(100));
794 mutex_exit(&sc->sc_delaylock);
795
796 /* serial MPEG port on HD5500 */
797 switch(sc->sc_vendor) {
798 case PCI_VENDOR_ATI:
799 /* both ATI boards with DTV are the same */
800 /* parallel MPEG port */
801 bus_space_write_4(sc->sc_memt, sc->sc_memh,
802 CXDTV_PINMUX_IO, 0x80); /* XXX bit defines */
803 break;
804 case PCI_VENDOR_PCHDTV:
805 if (sc->sc_product == PCI_PRODUCT_PCHDTV_HD5500) {
806 /* serial MPEG port */
807 bus_space_write_4(sc->sc_memt, sc->sc_memh,
808 CXDTV_PINMUX_IO, 0x00); /* XXX bit defines */
809 bus_space_write_4(sc->sc_memt, sc->sc_memh,
810 CXDTV_TS_GEN_CONTROL, 0x08);
811 /* byte-width start-of-packet */
812 bus_space_write_4(sc->sc_memt, sc->sc_memh,
813 CXDTV_TS_SOP_STATUS, 1 << 13);
814 }
815 break;
816 default:
817 break;
818 }
819
820 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_LNGTH,
821 CXDTV_TS_PKTSIZE);
822
823 /* Configure for standard MPEG TS, 1 good to sync */
824 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_HW_SOP_CONTROL,
825 0x47 << 16 | 188 << 4 | 1);
826
827 offset = CXDTV_TS_GEN_CONTROL;
828 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, offset);
829 printf("%06x %08x\n", offset, v);
830
831 #if 0
832 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_GEN_CONTROL, 0x00);
833 mutex_enter(&sc->sc_delaylock);
834 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, mstohz(100));
835 mutex_exit(&sc->sc_delaylock);
836 #endif
837
838 /* zero counter */
839 bus_space_write_4(sc->sc_memt, sc->sc_memh,
840 CXDTV_TS_GP_CNT_CNTRL, 0x03);
841
842 /* enable bad packet interrupt */
843 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_BD_PKT_STATUS,
844 0x1000);
845
846 /* enable overflow counter */
847 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_FIFO_OVFL_STAT,
848 0x1000);
849
850 /* unmask TS interrupt */
851 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK);
852 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK,
853 v | CXT_PI_TS_INT);
854
855 /* unmask all TS interrupts */
856 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK);
857 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK,
858 v | 0x1f1011);
859
860 /* enable RISC DMA engine */
861 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_DEV_CNTRL2);
862 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_DEV_CNTRL2,
863 v | CXDTV_DEV_CNTRL2_RUN_RISC);
864
865 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_DMA_CNTRL);
866 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_DMA_CNTRL,
867 v | CXDTV_TS_RISC_EN | CXDTV_TS_FIFO_EN);
868
869 #if 0
870 mutex_enter(&sc->sc_delaylock);
871 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, mstohz(1000));
872 mutex_exit(&sc->sc_delaylock);
873
874 for(offset = 0x33c040; offset < 0x33c064; offset += 4) {
875 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, offset);
876 printf("%06x %08x\n", offset, v);
877 }
878 for(offset = 0x200070; offset < 0x200080; offset += 4) {
879 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, offset);
880 printf("%06x %08x\n", offset, v);
881 }
882 #endif
883
884 return 0;
885 }
886
887 int
888 cxdtv_mpeg_halt(struct cxdtv_softc *sc)
889 {
890 uint32_t v;
891
892 CX_DPRINTF(("cxdtv_mpeg_halt\n"));
893
894 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_DMA_CNTRL);
895 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_DMA_CNTRL,
896 v & ~(CXDTV_TS_RISC_EN|CXDTV_TS_FIFO_EN));
897
898 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK);
899 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_PCI_INT_MASK,
900 v & ~CXT_PI_TS_INT);
901
902 v = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK);
903 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK,
904 v & ~0x1f1011);
905
906 return 0;
907 }
908
909 int
910 cxdtv_mpeg_intr(struct cxdtv_softc *sc)
911 {
912 struct dtv_payload payload;
913 uint32_t s, m;
914
915 s = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_STAT);
916 m = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_MASK);
917 if ((s & m) == 0)
918 return 0;
919
920 if ( (s & ~CXDTV_TS_RISCI) != 0 )
921 device_printf(sc->sc_dev, "unexpected TS IS %08x\n", s);
922
923 if ((s & CXDTV_TS_RISCI1) == CXDTV_TS_RISCI1) {
924 bus_dmamap_sync(sc->sc_dmat, sc->sc_dma->map,
925 0, CXDTV_TS_PKTSIZE,
926 BUS_DMASYNC_POSTREAD);
927 payload.data = KERNADDR(sc->sc_dma);
928 payload.size = CXDTV_TS_PKTSIZE;
929 dtv_submit_payload(sc->sc_dtvdev, &payload);
930 }
931
932 if ((s & CXDTV_TS_RISCI2) == CXDTV_TS_RISCI2) {
933 bus_dmamap_sync(sc->sc_dmat, sc->sc_dma->map,
934 CXDTV_TS_PKTSIZE, CXDTV_TS_PKTSIZE,
935 BUS_DMASYNC_POSTREAD);
936 payload.data = (char *)(KERNADDR(sc->sc_dma)) + (uintptr_t)CXDTV_TS_PKTSIZE;
937 payload.size = CXDTV_TS_PKTSIZE;
938 dtv_submit_payload(sc->sc_dtvdev, &payload);
939 }
940
941 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_TS_INT_STAT, s);
942
943 return 1;
944 }
945
946 static int
947 cxdtv_allocmem(struct cxdtv_softc *sc, size_t size, size_t align,
948 struct cxdtv_dma *p)
949 {
950 int err;
951
952 p->size = size;
953 err = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
954 p->segs, __arraycount(p->segs),
955 &p->nsegs, BUS_DMA_NOWAIT);
956 if (err)
957 return err;
958 err = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
959 &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
960 if (err)
961 goto free;
962 err = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
963 BUS_DMA_NOWAIT, &p->map);
964 if (err)
965 goto unmap;
966 err = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
967 BUS_DMA_NOWAIT);
968 if (err)
969 goto destroy;
970
971 return 0;
972
973 destroy:
974 bus_dmamap_destroy(sc->sc_dmat, p->map);
975 unmap:
976 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
977 free:
978 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
979
980 return err;
981 }
982
983 static int
984 cxdtv_freemem(struct cxdtv_softc *sc, struct cxdtv_dma *p)
985 {
986
987 bus_dmamap_unload(sc->sc_dmat, p->map);
988 bus_dmamap_destroy(sc->sc_dmat, p->map);
989 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
990 bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
991
992 return 0;
993 }
994
995 void *
996 cxdtv_mpeg_malloc(struct cxdtv_softc *sc, size_t size)
997 {
998 struct cxdtv_dma *p;
999 int err;
1000
1001 p = kmem_alloc(sizeof(*p), KM_SLEEP);
1002 if (p == NULL) {
1003 return NULL;
1004 }
1005
1006 err = cxdtv_allocmem(sc, size, 16, p);
1007 if (err) {
1008 kmem_free(p, sizeof(*p));
1009 device_printf(sc->sc_dev, "not enough memory\n");
1010 return NULL;
1011 }
1012
1013 p->next = sc->sc_dma;
1014 sc->sc_dma = p;
1015
1016 return KERNADDR(p);
1017 }
1018
1019 static void
1020 cxdtv_mpeg_free(struct cxdtv_softc *sc, void *addr)
1021 {
1022 struct cxdtv_dma *p;
1023 struct cxdtv_dma **pp;
1024
1025 for (pp = &sc->sc_dma; (p = *pp) != NULL; pp = &p->next) {
1026 if (KERNADDR(p) == addr) {
1027 cxdtv_freemem(sc, p);
1028 *pp = p->next;
1029 kmem_free(p, sizeof(*p));
1030 return;
1031 }
1032 }
1033
1034 device_printf(sc->sc_dev, "%p is already free\n", addr);
1035
1036 return;
1037 }
1038
1039
1040 /* ATI HDTV Wonder */
1041 static void
1042 cxdtv_card_init_hdtvwonder(struct cxdtv_softc *sc)
1043 {
1044 int i, x;
1045 i2c_addr_t na;
1046 uint8_t nb[5][2] = {
1047 {0x10, 0x12}, {0x13, 0x04}, {0x16, 0x00},
1048 {0x14, 0x04}, {0x17, 0x00}
1049 };
1050
1051 /* prepare TUV1236D/TU1236F NIM */
1052
1053 na = 0x0a; /* Nxt2004 address */
1054 x = 0;
1055
1056 iic_acquire_bus(&sc->sc_i2c, I2C_F_POLL);
1057
1058 for(i = 0; i < 5; i++)
1059 x |= iic_exec(&sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, na,
1060 nb[i], 2, NULL, 0, I2C_F_POLL);
1061
1062 iic_release_bus(&sc->sc_i2c, I2C_F_POLL);
1063
1064 if (x)
1065 aprint_error_dev(sc->sc_dev, "HDTV Wonder tuner init failed");
1066 }
1067
1068 /* pcHDTV HD5500 */
1069 static void
1070 cxdtv_card_init_hd5500(struct cxdtv_softc *sc)
1071 {
1072 uint32_t val;
1073
1074 /* hardware (demod) reset */
1075 val = bus_space_read_4(sc->sc_memt, sc->sc_memh, CXDTV_GP0_IO);
1076
1077 val &= ~1;
1078 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_GP0_IO, val);
1079 mutex_enter(&sc->sc_delaylock);
1080 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, mstohz(10));
1081 mutex_exit(&sc->sc_delaylock);
1082
1083 val |= 1;
1084 bus_space_write_4(sc->sc_memt, sc->sc_memh, CXDTV_GP0_IO, val);
1085 mutex_enter(&sc->sc_delaylock);
1086 cv_timedwait(&sc->sc_delaycv, &sc->sc_delaylock, mstohz(15));
1087 mutex_exit(&sc->sc_delaylock);
1088 }
1089
1090 MODULE(MODULE_CLASS_DRIVER, cxdtv, "dtv,tvpll,nxt2k,lg3303");
1091
1092 #ifdef _MODULE
1093 #include "ioconf.c"
1094 #endif
1095
1096 static int
1097 cxdtv_modcmd(modcmd_t cmd, void *opaque)
1098 {
1099 switch (cmd) {
1100 case MODULE_CMD_INIT:
1101 #ifdef _MODULE
1102 return config_init_component(cfdriver_ioconf_cxdtv,
1103 cfattach_ioconf_cxdtv, cfdata_ioconf_cxdtv);
1104 #else
1105 return 0;
1106 #endif
1107 case MODULE_CMD_FINI:
1108 #ifdef _MODULE
1109 return config_fini_component(cfdriver_ioconf_cxdtv,
1110 cfattach_ioconf_cxdtv, cfdata_ioconf_cxdtv);
1111 #else
1112 return 0;
1113 #endif
1114 default:
1115 return ENOTTY;
1116 }
1117 }
1118