Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.2
      1 /*	$NetBSD: oboe.c,v 1.2 2001/12/02 20:29:55 augustss Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jan Sparud.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Toshiba OBOE IrDA SIR/FIR driver.
     41  *
     42  * Based on information from the Linux driver, thus the magic hex numbers.
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/malloc.h>
     50 #include <sys/tty.h>
     51 #include <sys/vnode.h>
     52 #include <sys/poll.h>
     53 
     54 #include <dev/ir/ir.h>
     55 #include <dev/ir/irdaio.h>
     56 #include <dev/ir/irframevar.h>
     57 
     58 #include <dev/pci/pcidevs.h>
     59 #include <dev/pci/pcivar.h>
     60 
     61 #include <machine/bus.h>
     62 #include <machine/intr.h>
     63 #include <uvm/uvm_extern.h>
     64 
     65 #include <dev/pci/oboereg.h>
     66 
     67 int oboe_match(struct device *parent, struct cfdata *match, void *aux);
     68 void oboe_attach(struct device *parent, struct device *self, void *aux);
     69 int oboe_activate(struct device *self, enum devact act);
     70 int oboe_detach(struct device *self, int flags);
     71 
     72 int oboe_open(void *h, int flag, int mode, struct proc *p);
     73 int oboe_close(void *h, int flag, int mode, struct proc *p);
     74 int oboe_read(void *h, struct uio *uio, int flag);
     75 int oboe_write(void *h, struct uio *uio, int flag);
     76 int oboe_set_params(void *h, struct irda_params *params);
     77 int oboe_reset_params(void *h);
     78 int oboe_get_speeds(void *h, int *speeds);
     79 int oboe_get_turnarounds(void *h, int *times);
     80 int oboe_poll(void *h, int events, struct proc *p);
     81 
     82 #ifdef OBOE_DEBUG
     83 #define DPRINTF(x)	if (oboedebug) printf x
     84 int oboedebug = 1;
     85 #else
     86 #define DPRINTF(x)
     87 #endif
     88 
     89 struct oboe_dma;
     90 
     91 struct oboe_softc {
     92 	struct device		sc_dev;
     93 	struct device		*sc_child;
     94 	struct pci_attach_args	sc_pa;
     95 	pci_intr_handle_t *	sc_ih;
     96 	unsigned int		sc_revision;	/* PCI Revision ID */
     97 	/* I/O Base device */
     98 	bus_space_tag_t		sc_iot;
     99 	bus_space_handle_t	sc_ioh;
    100 	bus_dma_tag_t		sc_dmatag;
    101 	struct selinfo		sc_rsel;
    102 
    103 	int			sc_state;
    104 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    105 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    106 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    107 
    108 	int			sc_speeds;
    109 	int			sc_flags;
    110 	int			sc_speed;
    111 
    112 	struct oboe_dma		*sc_dmas;
    113 	struct OboeTaskFile *	sc_taskfile;    /* The taskfile   */
    114 	u_char *		sc_xmit_bufs[TX_SLOTS];
    115 	u_char *		sc_recv_bufs[RX_SLOTS];
    116 	void *			sc_xmit_stores[TX_SLOTS];
    117 	void *			sc_recv_stores[RX_SLOTS];
    118 	int			sc_txs; /* Current transmit slot number */
    119 	int			sc_rxs; /* Current receive slot number */
    120 	int			sc_saved; /* number of saved frames */
    121 	int			sc_lens[RX_SLOTS];
    122 
    123 	int			sc_txpending;
    124 
    125 	/* Statistics */
    126 	int			sc_txpackets;
    127 	int			sc_rxpackets;
    128 	int			sc_txerrors;
    129 	int			sc_rxerrors;
    130 };
    131 
    132 static int oboe_intr(void *handle);
    133 static int oboe_reset(struct oboe_softc *);
    134 
    135 struct oboe_dma {
    136 	bus_dmamap_t map;
    137 	caddr_t addr;
    138 	bus_dma_segment_t segs[1];
    139 	int nsegs;
    140 	size_t size;
    141 	struct oboe_dma *next;
    142 };
    143 
    144 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    145 #define KERNADDR(p) ((void *)((p)->addr))
    146 
    147 static int oboe_alloc_taskfile(struct oboe_softc *);
    148 static void oboe_init_taskfile(struct oboe_softc *);
    149 static void oboe_startchip(struct oboe_softc *);
    150 static void oboe_stopchip(struct oboe_softc *);
    151 static void oboe_setbaud(struct oboe_softc *, int);
    152 
    153 struct cfattach oboe_ca = {
    154 	sizeof(struct oboe_softc), oboe_match, oboe_attach,
    155 	oboe_detach, oboe_activate
    156 };
    157 
    158 struct irframe_methods oboe_methods = {
    159 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    160 	oboe_set_params, oboe_reset_params, oboe_get_speeds,
    161 	oboe_get_turnarounds
    162 };
    163 
    164 int
    165 oboe_match(struct device *parent, struct cfdata *match, void *aux)
    166 {
    167 	struct pci_attach_args *pa = aux;
    168 
    169 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    170 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE)
    171 		return (1);
    172 	return 0;
    173 }
    174 
    175 void
    176 oboe_attach(struct device *parent, struct device *self, void *aux)
    177 {
    178 	struct oboe_softc *sc = (struct oboe_softc *)self;
    179 	struct pci_attach_args *pa = aux;
    180 	pci_intr_handle_t ih;
    181 	struct ir_attach_args ia;
    182 	const char *intrstring;
    183 
    184 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    185 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    186 	       sc->sc_revision);
    187 
    188 	/* Map I/O registers. */
    189 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    190 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    191 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    192 		return;
    193 	}
    194 
    195 	sc->sc_dmatag = pa->pa_dmat;
    196 
    197 	ia.ia_type = IR_TYPE_IRFRAME;
    198 	ia.ia_methods = &oboe_methods;
    199 	ia.ia_handle = sc;
    200 
    201 	sc->sc_state = 0;
    202 	sc->sc_speed = IRDA_SPEED_9600;
    203 
    204 	/* Enable the device */
    205 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    206 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    207 	    PCI_COMMAND_MASTER_ENABLE);
    208 
    209 	/* Reset the device; bail out upon failure. */
    210 	if (oboe_reset(sc) != 0) {
    211 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    212 		return;
    213 	}
    214 	/* Map and establish the interrupt. */
    215 	if (pci_intr_map(pa, &ih)) {
    216 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    217 		return;
    218 	}
    219 	intrstring = pci_intr_string(pa->pa_pc, ih);
    220 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    221 	if (sc->sc_ih == NULL) {
    222 		printf("%s: couldn't establish interrupt",
    223 		    sc->sc_dev.dv_xname);
    224 		if (intrstring != NULL)
    225 			printf(" at %s", intrstring);
    226 		printf("\n");
    227 		return;
    228 	}
    229 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    230 
    231 	sc->sc_txs = 0;
    232 	sc->sc_rxs = 0;
    233 
    234 	sc->sc_speeds =
    235 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    236 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    237 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    238 
    239 	oboe_alloc_taskfile(sc);
    240 
    241 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    242 }
    243 
    244 int
    245 oboe_activate(struct device *self, enum devact act)
    246 {
    247 	struct oboe_softc *sc = (struct oboe_softc *)self;
    248 	int error;
    249 
    250 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    251 
    252 	switch (act) {
    253 	case DVACT_ACTIVATE:
    254 		return (EOPNOTSUPP);
    255 		break;
    256 
    257 	case DVACT_DEACTIVATE:
    258 		if (sc->sc_child != NULL)
    259 			error = config_deactivate(sc->sc_child);
    260 		else
    261 			error = 0;
    262 		break;
    263 	}
    264 	return (error);
    265 }
    266 
    267 int
    268 oboe_detach(struct device *self, int flags)
    269 {
    270 #if 0
    271 	struct oboe_softc *sc = (struct oboe_softc *)self;
    272 #endif
    273 	/* XXX needs reference counting for proper detach. */
    274 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    275 	return (0);
    276 }
    277 
    278 int
    279 oboe_open(void *h, int flag, int mode, struct proc *p)
    280 {
    281 	struct oboe_softc *sc = h;
    282 
    283 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    284 
    285 	sc->sc_state = 0;
    286 	sc->sc_saved = 0;
    287 	oboe_init_taskfile(sc);
    288 	oboe_startchip(sc);
    289 
    290 	return (0);
    291 }
    292 
    293 int
    294 oboe_close(void *h, int flag, int mode, struct proc *p)
    295 {
    296 	struct oboe_softc *sc = h;
    297 	int error = 0;
    298 	int s = splir();
    299 
    300 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    301 	/* Wait for output to drain */
    302 
    303 	if (sc->sc_txpending > 0) {
    304 		sc->sc_state |= OBOE_CLOSING;
    305 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    306 	}
    307 	splx(s);
    308 
    309 	oboe_stopchip(sc);
    310 	return (error);
    311 }
    312 
    313 int
    314 oboe_read(void *h, struct uio *uio, int flag)
    315 {
    316 	struct oboe_softc *sc = h;
    317 	int error = 0;
    318 	int s;
    319 	int slot;
    320 
    321 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    322 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    323 		 (long)uio->uio_offset));
    324 
    325 	s = splir();
    326 	while (sc->sc_saved == 0) {
    327 		if (flag & IO_NDELAY) {
    328 			splx(s);
    329 			return (EWOULDBLOCK);
    330 		}
    331 		sc->sc_state |= OBOE_RSLP;
    332 		DPRINTF(("oboe_read: sleep\n"));
    333 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    334 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    335 		if (error) {
    336 			sc->sc_state &= ~OBOE_RSLP;
    337 			break;
    338 		}
    339 	}
    340 
    341 	/* Do just one frame transfer per read */
    342 
    343 	if (!error) {
    344 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    345 		if (uio->uio_resid < sc->sc_lens[slot]) {
    346 			DPRINTF(("oboe_read: uio buffer smaller than frame size (%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    347 			error = EINVAL;
    348 		} else {
    349 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    350 				 sc->sc_lens[slot],
    351 				 sc->sc_recv_stores[slot]));
    352 			error = uiomove(sc->sc_recv_stores[slot],
    353 					sc->sc_lens[slot], uio);
    354 		}
    355 	}
    356 	sc->sc_saved--;
    357 	splx(s);
    358 
    359 	return (error);
    360 }
    361 
    362 int
    363 oboe_write(void *h, struct uio *uio, int flag)
    364 {
    365 	struct oboe_softc *sc = h;
    366 	int error = 0;
    367 	int s = splir();
    368 
    369 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    370 	while (sc->sc_txpending == TX_SLOTS) {
    371 		if (flag & IO_NDELAY) {
    372 			splx(s);
    373 			return (EWOULDBLOCK);
    374 		}
    375 		sc->sc_state |= OBOE_WSLP;
    376 		DPRINTF(("oboe_write: sleep\n"));
    377 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    378 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    379 		if (error) {
    380 			sc->sc_state &= ~OBOE_WSLP;
    381 			break;
    382 		}
    383 	}
    384 	if (error)
    385 		goto err;
    386 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    387 		DPRINTF(("oboe_write: slot overrun\n"));
    388 	}
    389 
    390 	sc->sc_taskfile->xmit[sc->sc_txs].len = uio->uio_resid;
    391 	error = uiomove(sc->sc_xmit_bufs[sc->sc_txs],
    392 			uio->uio_resid, uio);
    393 	if (error)
    394 		goto err;
    395 
    396 	OUTB(sc, 0, OBOE_RST);
    397 	OUTB(sc, 0x1e, OBOE_REG_11);
    398 
    399 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    400 
    401 	/* Need delay here??? */
    402 	delay(1000);
    403 
    404 	sc->sc_txpending++;
    405 	OUTB(sc, 0x80, OBOE_RST);
    406 	OUTB(sc, 1, OBOE_REG_9);
    407 	sc->sc_txs++;
    408 	sc->sc_txs %= TX_SLOTS;
    409 
    410  err:
    411 	splx(s);
    412 	return (error);
    413 }
    414 
    415 int
    416 oboe_set_params(void *h, struct irda_params *p)
    417 {
    418 	struct oboe_softc *sc = h;
    419 	int s = splir();
    420 
    421 	if (p->speed > 0) {
    422 		oboe_setbaud(sc, p->speed);
    423 	}
    424 
    425 	splx(s);
    426 
    427 	/* ignore ebofs and maxsize for now */
    428 	return (0);
    429 }
    430 
    431 int
    432 oboe_reset_params(void *h)
    433 {
    434 	struct oboe_softc *sc = h;
    435 	int s = splir();
    436 
    437 	oboe_setbaud(sc, 9600);
    438 
    439 	splx(s);
    440 
    441 	return (0);
    442 }
    443 
    444 int
    445 oboe_get_speeds(void *h, int *speeds)
    446 {
    447 	struct oboe_softc *sc = h;
    448 	int s = splir();
    449 	*speeds = sc->sc_speeds;
    450 	splx(s);
    451 	return (0);
    452 }
    453 
    454 int
    455 oboe_get_turnarounds(void *h, int *turnarounds)
    456 {
    457 #if 0
    458 	struct oboe_softc *sc = h;
    459 #endif
    460 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    461 	*turnarounds = 0x01; /* 10ms */
    462 	return (0);
    463 }
    464 
    465 int
    466 oboe_poll(void *h, int events, struct proc *p)
    467 {
    468 	struct oboe_softc *sc = h;
    469 	int revents = 0;
    470 	int s;
    471 
    472 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    473 
    474 	s = splir();
    475 	if (events & (POLLOUT | POLLWRNORM))
    476 		revents |= events & (POLLOUT | POLLWRNORM);
    477 	if (events & (POLLIN | POLLRDNORM)) {
    478 		if (sc->sc_saved > 0) {
    479 			DPRINTF(("%s: have data\n", __FUNCTION__));
    480 			revents |= events & (POLLIN | POLLRDNORM);
    481 		} else {
    482 			DPRINTF(("%s: recording select", __FUNCTION__));
    483 			selrecord(p, &sc->sc_rsel);
    484 		}
    485 	}
    486 	splx(s);
    487 
    488 	return (revents);
    489 }
    490 
    491 
    492 static int
    493 oboe_reset(struct oboe_softc *sc)
    494 {
    495 #if 0
    496 	OUTB(sc, 0x00, OBOE_RST);
    497 	OUTB(sc, 0x80, OBOE_RST);
    498 #endif
    499 	return 0;
    500 }
    501 
    502 static int
    503 oboe_intr(void *p)
    504 {
    505 	struct oboe_softc *sc = p;
    506 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    507 
    508 	if (!(irqstat & 0xf8))
    509 		return (0); /* Not for me? */
    510 
    511 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    512 
    513 	OUTB(sc, irqstat, OBOE_ISR);
    514 
    515 	if (irqstat & OBOE_ISR_RXDONE) {
    516 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    517 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    518 			if (sc->sc_saved == RX_SLOTS) {
    519 				DPRINTF(("oboe_intr: all buffers filled\n"));
    520 				return 0;
    521 			}
    522 
    523 			if (len > 2)
    524 				len -= 2; /* JSP: skip check sum? */
    525 
    526 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    527 				 sc->sc_recv_stores[sc->sc_rxs]));
    528 			bcopy(sc->sc_recv_bufs[sc->sc_rxs],
    529 			      sc->sc_recv_stores[sc->sc_rxs],
    530 			      len);
    531 			sc->sc_lens[sc->sc_rxs] = len;
    532 			sc->sc_saved++;
    533 #if 0
    534 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    535 				     len, &sc->sc_q);
    536 #endif
    537 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    538 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    539 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    540 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    541 		}
    542 		DPRINTF(("oboe_intr no more frames available\n"));
    543 		if (sc->sc_state & OBOE_RSLP) {
    544 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    545 			sc->sc_state &= ~OBOE_RSLP;
    546 			DPRINTF(("oboe_intr: waking up reader\n"));
    547 			wakeup(&sc->sc_rxs);
    548 		}
    549 		selwakeup(&sc->sc_rsel);
    550 		DPRINTF(("oboe_intr returning\n"));
    551 	}
    552 	if (irqstat & OBOE_ISR_TXDONE) {
    553 	        DPRINTF(("oboe_intr: write done\n"));
    554 		sc->sc_txpending--;
    555 		sc->sc_txpackets++;
    556 
    557 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    558 			wakeup(&sc->sc_state);
    559 			return 1;
    560 		}
    561 
    562 		if (sc->sc_state & OBOE_WSLP) {
    563 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    564 			sc->sc_state &= ~OBOE_WSLP;
    565 			DPRINTF(("oboe_intr: waking up writer\n"));
    566 			wakeup(&sc->sc_txs);
    567 		}
    568 	}
    569 	return (1);
    570 }
    571 
    572 
    573 static void
    574 oboe_init_taskfile(struct oboe_softc *sc)
    575 {
    576 	int i;
    577 	int s = splir();
    578 
    579 	for (i = 0; i < TX_SLOTS; ++i) {
    580 		sc->sc_taskfile->xmit[i].len = 0;
    581 		sc->sc_taskfile->xmit[i].control = 0x00;
    582 		sc->sc_taskfile->xmit[i].buffer =
    583 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    584 	}
    585 
    586 	for (i = 0; i < RX_SLOTS; ++i) {
    587 		sc->sc_taskfile->recv[i].len = 0;
    588 		sc->sc_taskfile->recv[i].control = 0x83;
    589 		sc->sc_taskfile->recv[i].buffer =
    590 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    591 	}
    592 
    593 	sc->sc_txpending = 0;
    594 
    595 	splx(s);
    596 }
    597 
    598 static int
    599 oboe_alloc_taskfile(struct oboe_softc *sc)
    600 {
    601 	int i;
    602 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    603 	if (addr == NULL) {
    604 		goto bad;
    605 	}
    606 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    607 	addr += sizeof (struct OboeTaskFile);
    608 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    609 
    610 	for (i = 0; i < TX_SLOTS; ++i) {
    611 		sc->sc_xmit_bufs[i] =
    612 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    613 		sc->sc_xmit_stores[i] =
    614 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    615 		if (sc->sc_xmit_bufs[i] == NULL ||
    616 		    sc->sc_xmit_stores[i] == NULL) {
    617 			goto bad;
    618 		}
    619 	}
    620 	for (i = 0; i < RX_SLOTS; ++i) {
    621 		sc->sc_recv_bufs[i] =
    622 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    623 		sc->sc_recv_stores[i] =
    624 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    625 		if (sc->sc_recv_bufs[i] == NULL ||
    626 		    sc->sc_recv_stores[i] == NULL) {
    627 			goto bad;
    628 		}
    629 	}
    630 
    631 	return 0;
    632 bad:
    633 	printf("oboe: malloc for buffers failed()\n");
    634 	return 1;
    635 }
    636 
    637 static void
    638 oboe_startchip (struct oboe_softc *sc) {
    639 	uint32_t physaddr;
    640 
    641 	OUTB(sc, 0, OBOE_LOCK);
    642 	OUTB(sc, 0, OBOE_RST);
    643 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    644 	OUTB(sc, 0xf0, OBOE_REG_D);
    645 	OUTB(sc, 0xff, OBOE_ISR);
    646 	OUTB(sc, 0x0f, OBOE_REG_1A);
    647 	OUTB(sc, 0xff, OBOE_REG_1B);
    648 
    649 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    650 
    651 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    652 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    653 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    654 
    655 	OUTB(sc, 0x0e, OBOE_REG_11);
    656 	OUTB(sc, 0x80, OBOE_RST);
    657 
    658 	oboe_setbaud(sc, 9600);
    659 
    660 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    661 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    662 		sc->sc_rxs = 0;
    663 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    664 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    665 		sc->sc_txs = 0;
    666 }
    667 
    668 static void
    669 oboe_stopchip (struct oboe_softc *sc) {
    670 	OUTB(sc, 0x0e, OBOE_REG_11);
    671 	OUTB(sc, 0x00, OBOE_RST);
    672 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    673 	OUTB(sc, 0xff, OBOE_TFP1);
    674 	OUTB(sc, 0xff, OBOE_TFP0);
    675 	OUTB(sc, 0x0f, OBOE_REG_1B);
    676 	OUTB(sc, 0xff, OBOE_REG_1A);
    677 	OUTB(sc, 0x00, OBOE_ISR); /* FIXME: should i do this to disable ints? */
    678 	OUTB(sc, 0x80, OBOE_RST);
    679 	OUTB(sc, 0x0e, OBOE_LOCK);
    680 }
    681 
    682 #define SPEEDCASE(speed, type, divisor) \
    683 case speed: \
    684 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    685 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    686 OUTB(sc, divisor, OBOE_UDIV); \
    687 break
    688 
    689 static void
    690 oboe_setbaud (struct oboe_softc *sc, int baud) {
    691 	int s;
    692 
    693 	DPRINTF(("oboe: setting baud to %d\n", baud));
    694 
    695 	s = splir();
    696 
    697 	switch (baud) {
    698 	SPEEDCASE(   2400, SIR, 0xbf);
    699 	SPEEDCASE(   9600, SIR, 0x2f);
    700 	SPEEDCASE(  19200, SIR, 0x17);
    701 	SPEEDCASE(  38400, SIR, 0x0b);
    702 	SPEEDCASE(  57600, SIR, 0x07);
    703 	SPEEDCASE( 115200, SIR, 0x03);
    704 	SPEEDCASE(1152000, MIR, 0x01);
    705 	SPEEDCASE(4000000, FIR, 0x00);
    706 	default:
    707 		printf("oboe: cannot set speed to %d\n", baud);
    708 		return;
    709 	}
    710 
    711 	splx(s);
    712 
    713 	OUTB(sc, 0x00, OBOE_RST);
    714 	OUTB(sc, 0x80, OBOE_RST);
    715 	OUTB(sc, 0x01, OBOE_REG_9);
    716 
    717 	sc->sc_speed = baud;
    718 }
    719