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