1*bb5dc3a9Sjsg /* $OpenBSD: bpp.c,v 1.7 2024/04/14 03:23:13 jsg Exp $ */
28260c660Smiod /* $NetBSD: bpp.c,v 1.25 2005/12/11 12:23:44 christos Exp $ */
38260c660Smiod
48260c660Smiod /*-
58260c660Smiod * Copyright (c) 1998 The NetBSD Foundation, Inc.
68260c660Smiod * All rights reserved.
78260c660Smiod *
88260c660Smiod * This code is derived from software contributed to The NetBSD Foundation
98260c660Smiod * by Paul Kranenburg.
108260c660Smiod *
118260c660Smiod * Redistribution and use in source and binary forms, with or without
128260c660Smiod * modification, are permitted provided that the following conditions
138260c660Smiod * are met:
148260c660Smiod * 1. Redistributions of source code must retain the above copyright
158260c660Smiod * notice, this list of conditions and the following disclaimer.
168260c660Smiod * 2. Redistributions in binary form must reproduce the above copyright
178260c660Smiod * notice, this list of conditions and the following disclaimer in the
188260c660Smiod * documentation and/or other materials provided with the distribution.
198260c660Smiod *
208260c660Smiod * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
218260c660Smiod * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
228260c660Smiod * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
238260c660Smiod * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
248260c660Smiod * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
258260c660Smiod * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
268260c660Smiod * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
278260c660Smiod * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
288260c660Smiod * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
298260c660Smiod * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
308260c660Smiod * POSSIBILITY OF SUCH DAMAGE.
318260c660Smiod */
328260c660Smiod
338260c660Smiod #include <sys/param.h>
348260c660Smiod #include <sys/ioctl.h>
358260c660Smiod #include <sys/systm.h>
368260c660Smiod #include <sys/kernel.h>
378260c660Smiod #include <sys/errno.h>
388260c660Smiod #include <sys/device.h>
398260c660Smiod #include <sys/malloc.h>
408260c660Smiod #include <sys/proc.h>
418260c660Smiod #include <sys/vnode.h>
428260c660Smiod #include <sys/conf.h>
438260c660Smiod
448260c660Smiod #include <machine/autoconf.h>
458260c660Smiod #include <machine/bus.h>
468260c660Smiod #include <machine/conf.h>
478260c660Smiod #include <machine/intr.h>
488260c660Smiod
498260c660Smiod #include <dev/ic/lsi64854reg.h>
508260c660Smiod #include <dev/ic/lsi64854var.h>
518260c660Smiod
528260c660Smiod #include <dev/sbus/sbusvar.h>
538260c660Smiod #include <dev/sbus/bppreg.h>
548260c660Smiod
558260c660Smiod #define splbpp() spltty() /* XXX */
568260c660Smiod
578260c660Smiod #ifdef DEBUG
588260c660Smiod #define DPRINTF(x) do { if (bppdebug) printf x ; } while (0)
598260c660Smiod int bppdebug = 1;
608260c660Smiod #else
618260c660Smiod #define DPRINTF(x)
628260c660Smiod #endif
638260c660Smiod
648260c660Smiod #if 0
658260c660Smiod struct bpp_param {
668260c660Smiod int bpp_dss; /* data setup to strobe */
678260c660Smiod int bpp_dsw; /* data strobe width */
688260c660Smiod int bpp_outputpins; /* Select/Autofeed/Init pins */
698260c660Smiod int bpp_inputpins; /* Error/Select/Paperout pins */
708260c660Smiod };
718260c660Smiod #endif
728260c660Smiod
738260c660Smiod struct hwstate {
748260c660Smiod u_int16_t hw_hcr; /* Hardware config register */
758260c660Smiod u_int16_t hw_ocr; /* Operation config register */
768260c660Smiod u_int8_t hw_tcr; /* Transfer Control register */
778260c660Smiod u_int8_t hw_or; /* Output register */
788260c660Smiod u_int16_t hw_irq; /* IRQ; polarity bits only */
798260c660Smiod };
808260c660Smiod
818260c660Smiod struct bpp_softc {
828260c660Smiod struct lsi64854_softc sc_lsi64854; /* base device */
838260c660Smiod
848260c660Smiod size_t sc_bufsz; /* temp buffer */
858260c660Smiod caddr_t sc_buf;
868260c660Smiod
878260c660Smiod int sc_error; /* bottom-half error */
888260c660Smiod int sc_flags;
898260c660Smiod #define BPP_LOCKED 0x01 /* DMA in progress */
908260c660Smiod #define BPP_WANT 0x02 /* Waiting for DMA */
918260c660Smiod
928260c660Smiod /* Hardware state */
938260c660Smiod struct hwstate sc_hwstate;
948260c660Smiod };
958260c660Smiod
968260c660Smiod int bppmatch(struct device *, void *, void *);
978260c660Smiod void bppattach(struct device *, struct device *, void *);
988260c660Smiod int bppintr (void *);
998260c660Smiod void bpp_setparams(struct bpp_softc *, struct hwstate *);
1008260c660Smiod
1018260c660Smiod const struct cfattach bpp_ca = {
1028260c660Smiod sizeof(struct bpp_softc), bppmatch, bppattach
1038260c660Smiod };
1048260c660Smiod
1058260c660Smiod struct cfdriver bpp_cd = {
1068260c660Smiod NULL, "bpp", DV_DULL
1078260c660Smiod };
1088260c660Smiod
1098260c660Smiod #define BPPUNIT(dev) (minor(dev))
1108260c660Smiod
1118260c660Smiod int
bppmatch(struct device * parent,void * vcf,void * aux)1128260c660Smiod bppmatch(struct device *parent, void *vcf, void *aux)
1138260c660Smiod {
1148260c660Smiod struct sbus_attach_args *sa = aux;
1158260c660Smiod
1168260c660Smiod return (strcmp("SUNW,bpp", sa->sa_name) == 0);
1178260c660Smiod }
1188260c660Smiod
1198260c660Smiod void
bppattach(struct device * parent,struct device * self,void * aux)1208260c660Smiod bppattach(struct device *parent, struct device *self, void *aux)
1218260c660Smiod {
1228260c660Smiod struct sbus_attach_args *sa = aux;
1238260c660Smiod struct bpp_softc *dsc = (void *)self;
1248260c660Smiod struct lsi64854_softc *sc = &dsc->sc_lsi64854;
1258260c660Smiod int burst, sbusburst;
1268260c660Smiod int node;
1278260c660Smiod
1288260c660Smiod node = sa->sa_node;
1298260c660Smiod
1308260c660Smiod sc->sc_bustag = sa->sa_bustag;
1318260c660Smiod sc->sc_dmatag = sa->sa_dmatag;
1328260c660Smiod
1338260c660Smiod /* Map device registers */
1348260c660Smiod if (sa->sa_npromvaddrs != 0) {
1358260c660Smiod if (sbus_bus_map(sa->sa_bustag, 0, sa->sa_promvaddrs[0],
1368260c660Smiod sa->sa_size, /* ???? */
1378260c660Smiod BUS_SPACE_MAP_PROMADDRESS, 0, &sc->sc_regs) != 0) {
138bd89844bSmiod printf(": cannot map registers\n");
1398260c660Smiod return;
1408260c660Smiod }
1418260c660Smiod } else if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
1428260c660Smiod sa->sa_size, 0, 0, &sc->sc_regs) != 0) {
143bd89844bSmiod printf(": cannot map registers\n");
1448260c660Smiod return;
1458260c660Smiod }
1468260c660Smiod
1478260c660Smiod /* Check for the interrupt property */
1488260c660Smiod if (sa->sa_nintr == 0) {
1498260c660Smiod printf(": no interrupt property\n");
1508260c660Smiod return;
1518260c660Smiod }
1528260c660Smiod
1538260c660Smiod /*
1548260c660Smiod * Get transfer burst size from PROM and plug it into the
1558260c660Smiod * controller registers. This is needed on the Sun4m; do
1568260c660Smiod * others need it too?
1578260c660Smiod */
1588260c660Smiod sbusburst = ((struct sbus_softc *)parent)->sc_burst;
1598260c660Smiod if (sbusburst == 0)
1608260c660Smiod sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
1618260c660Smiod
1628260c660Smiod burst = getpropint(node, "burst-sizes", -1);
1638260c660Smiod if (burst == -1)
1648260c660Smiod /* take SBus burst sizes */
1658260c660Smiod burst = sbusburst;
1668260c660Smiod
1678260c660Smiod /* Clamp at parent's burst sizes */
1688260c660Smiod burst &= sbusburst;
1698260c660Smiod sc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
1708260c660Smiod (burst & SBUS_BURST_16) ? 16 : 0;
1718260c660Smiod
1728260c660Smiod /* Initialize the DMA channel */
1738260c660Smiod sc->sc_channel = L64854_CHANNEL_PP;
1748260c660Smiod if (lsi64854_attach(sc) != 0)
1758260c660Smiod return;
1768260c660Smiod
1778260c660Smiod /* Establish interrupt handler */
1788260c660Smiod sc->sc_intrchain = bppintr;
1798260c660Smiod sc->sc_intrchainarg = dsc;
1808260c660Smiod (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY, 0,
1818260c660Smiod bppintr, sc, self->dv_xname);
1828260c660Smiod
1838260c660Smiod /* Allocate buffer XXX - should actually use dmamap_uio() */
1848260c660Smiod dsc->sc_bufsz = 1024;
1858260c660Smiod dsc->sc_buf = malloc(dsc->sc_bufsz, M_DEVBUF, M_NOWAIT);
1868260c660Smiod
1878260c660Smiod /* XXX read default state */
1888260c660Smiod {
1898260c660Smiod bus_space_handle_t h = sc->sc_regs;
1908260c660Smiod struct hwstate *hw = &dsc->sc_hwstate;
1918260c660Smiod int ack_rate = sa->sa_frequency/1000000;
1928260c660Smiod
1938260c660Smiod hw->hw_hcr = bus_space_read_2(sc->sc_bustag, h, L64854_REG_HCR);
1948260c660Smiod hw->hw_ocr = bus_space_read_2(sc->sc_bustag, h, L64854_REG_OCR);
1958260c660Smiod hw->hw_tcr = bus_space_read_1(sc->sc_bustag, h, L64854_REG_TCR);
1968260c660Smiod hw->hw_or = bus_space_read_1(sc->sc_bustag, h, L64854_REG_OR);
1978260c660Smiod
1988260c660Smiod DPRINTF(("bpp: hcr %x ocr %x tcr %x or %x\n",
1998260c660Smiod hw->hw_hcr, hw->hw_ocr, hw->hw_tcr, hw->hw_or));
2008260c660Smiod /* Set these to sane values */
2018260c660Smiod hw->hw_hcr = ((ack_rate<<BPP_HCR_DSS_SHFT)&BPP_HCR_DSS_MASK)
2028260c660Smiod | ((ack_rate<<BPP_HCR_DSW_SHFT)&BPP_HCR_DSW_MASK);
2038260c660Smiod hw->hw_ocr |= BPP_OCR_ACK_OP;
2048260c660Smiod }
2058260c660Smiod }
2068260c660Smiod
2078260c660Smiod void
bpp_setparams(struct bpp_softc * sc,struct hwstate * hw)2088260c660Smiod bpp_setparams(struct bpp_softc *sc, struct hwstate *hw)
2098260c660Smiod {
2108260c660Smiod u_int16_t irq;
2118260c660Smiod bus_space_tag_t t = sc->sc_lsi64854.sc_bustag;
2128260c660Smiod bus_space_handle_t h = sc->sc_lsi64854.sc_regs;
2138260c660Smiod
2148260c660Smiod bus_space_write_2(t, h, L64854_REG_HCR, hw->hw_hcr);
2158260c660Smiod bus_space_write_2(t, h, L64854_REG_OCR, hw->hw_ocr);
2168260c660Smiod bus_space_write_1(t, h, L64854_REG_TCR, hw->hw_tcr);
2178260c660Smiod bus_space_write_1(t, h, L64854_REG_OR, hw->hw_or);
2188260c660Smiod
2198260c660Smiod /* Only change IRP settings in interrupt status register */
2208260c660Smiod irq = bus_space_read_2(t, h, L64854_REG_ICR);
2218260c660Smiod irq &= ~BPP_ALLIRP;
2228260c660Smiod irq |= (hw->hw_irq & BPP_ALLIRP);
2238260c660Smiod bus_space_write_2(t, h, L64854_REG_ICR, irq);
2248260c660Smiod DPRINTF(("bpp_setparams: hcr %x ocr %x tcr %x or %x, irq %x\n",
2258260c660Smiod hw->hw_hcr, hw->hw_ocr, hw->hw_tcr, hw->hw_or, irq));
2268260c660Smiod }
2278260c660Smiod
2288260c660Smiod int
bppopen(dev_t dev,int flags,int mode,struct proc * p)2298260c660Smiod bppopen(dev_t dev, int flags, int mode, struct proc *p)
2308260c660Smiod {
2318260c660Smiod int unit = BPPUNIT(dev);
2328260c660Smiod struct bpp_softc *sc;
2338260c660Smiod struct lsi64854_softc *lsi;
2348260c660Smiod u_int16_t irq;
2358260c660Smiod int s;
2368260c660Smiod
2378260c660Smiod if (unit >= bpp_cd.cd_ndevs)
2388260c660Smiod return (ENXIO);
2398260c660Smiod if ((sc = bpp_cd.cd_devs[unit]) == NULL)
2408260c660Smiod return (ENXIO);
2418260c660Smiod
2428260c660Smiod lsi = &sc->sc_lsi64854;
2438260c660Smiod
2448260c660Smiod /* Set default parameters */
2458260c660Smiod s = splbpp();
2468260c660Smiod bpp_setparams(sc, &sc->sc_hwstate);
2478260c660Smiod splx(s);
2488260c660Smiod
2498260c660Smiod /* Enable interrupts */
2508260c660Smiod irq = BPP_ERR_IRQ_EN;
2518260c660Smiod irq |= sc->sc_hwstate.hw_irq;
2528260c660Smiod bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR, irq);
2538260c660Smiod return (0);
2548260c660Smiod }
2558260c660Smiod
2568260c660Smiod int
bppclose(dev_t dev,int flags,int mode,struct proc * p)2578260c660Smiod bppclose(dev_t dev, int flags, int mode, struct proc *p)
2588260c660Smiod {
2598260c660Smiod struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
2608260c660Smiod struct lsi64854_softc *lsi = &sc->sc_lsi64854;
2618260c660Smiod u_int16_t irq;
2628260c660Smiod
2638260c660Smiod /* Turn off all interrupt enables */
2648260c660Smiod irq = sc->sc_hwstate.hw_irq | BPP_ALLIRQ;
2658260c660Smiod irq &= ~BPP_ALLEN;
2668260c660Smiod bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR, irq);
2678260c660Smiod
2688260c660Smiod sc->sc_flags = 0;
2698260c660Smiod return (0);
2708260c660Smiod }
2718260c660Smiod
2728260c660Smiod int
bppwrite(dev_t dev,struct uio * uio,int flags)2738260c660Smiod bppwrite(dev_t dev, struct uio *uio, int flags)
2748260c660Smiod {
2758260c660Smiod struct bpp_softc *sc = bpp_cd.cd_devs[BPPUNIT(dev)];
2768260c660Smiod struct lsi64854_softc *lsi = &sc->sc_lsi64854;
2778260c660Smiod int error = 0;
2788260c660Smiod int s;
2798260c660Smiod
2808260c660Smiod /*
2818260c660Smiod * Wait until the DMA engine is free.
2828260c660Smiod */
2838260c660Smiod s = splbpp();
2848260c660Smiod while ((sc->sc_flags & BPP_LOCKED) != 0) {
2858260c660Smiod if ((flags & IO_NDELAY) != 0) {
2868260c660Smiod splx(s);
2878260c660Smiod return (EWOULDBLOCK);
2888260c660Smiod }
2898260c660Smiod
2908260c660Smiod sc->sc_flags |= BPP_WANT;
29103604742Smpi error = tsleep_nsec(sc->sc_buf, PZERO | PCATCH, "bppwrite",
29203604742Smpi INFSLP);
2938260c660Smiod if (error != 0) {
2948260c660Smiod splx(s);
2958260c660Smiod return (error);
2968260c660Smiod }
2978260c660Smiod }
2988260c660Smiod sc->sc_flags |= BPP_LOCKED;
2998260c660Smiod splx(s);
3008260c660Smiod
3018260c660Smiod /*
3028260c660Smiod * Move data from user space into our private buffer
3038260c660Smiod * and start DMA.
3048260c660Smiod */
3058260c660Smiod while (uio->uio_resid > 0) {
3068260c660Smiod caddr_t bp = sc->sc_buf;
307661211aeSstefan size_t len = ulmin(sc->sc_bufsz, uio->uio_resid);
3088260c660Smiod
309661211aeSstefan if ((error = uiomove(bp, len, uio)) != 0)
3108260c660Smiod break;
3118260c660Smiod
3128260c660Smiod while (len > 0) {
3138260c660Smiod u_int8_t tcr;
3148260c660Smiod size_t size = len;
3158260c660Smiod DMA_SETUP(lsi, &bp, &len, 0, &size);
3168260c660Smiod
3178260c660Smiod #ifdef DEBUG
3188260c660Smiod if (bppdebug) {
319661211aeSstefan size_t i;
3208260c660Smiod printf("bpp: writing %ld : ", len);
321*bb5dc3a9Sjsg for (i=0; i<len; i++)
322*bb5dc3a9Sjsg printf("%c(0x%x)", bp[i], bp[i]);
3238260c660Smiod printf("\n");
3248260c660Smiod }
3258260c660Smiod #endif
3268260c660Smiod
3278260c660Smiod /* Clear direction control bit */
3288260c660Smiod tcr = bus_space_read_1(lsi->sc_bustag, lsi->sc_regs,
3298260c660Smiod L64854_REG_TCR);
3308260c660Smiod tcr &= ~BPP_TCR_DIR;
3318260c660Smiod bus_space_write_1(lsi->sc_bustag, lsi->sc_regs,
3328260c660Smiod L64854_REG_TCR, tcr);
3338260c660Smiod
3348260c660Smiod /* Enable DMA */
3358260c660Smiod s = splbpp();
3368260c660Smiod DMA_GO(lsi);
33703604742Smpi error = tsleep_nsec(sc, PZERO | PCATCH, "bppdma",
33803604742Smpi INFSLP);
3398260c660Smiod splx(s);
3408260c660Smiod if (error != 0)
3418260c660Smiod goto out;
3428260c660Smiod
3438260c660Smiod /* Bail out if bottom half reported an error */
3448260c660Smiod if ((error = sc->sc_error) != 0)
3458260c660Smiod goto out;
3468260c660Smiod
3478260c660Smiod /*
3488260c660Smiod * DMA_INTR() does this part.
3498260c660Smiod *
3508260c660Smiod * len -= size;
3518260c660Smiod */
3528260c660Smiod }
3538260c660Smiod }
3548260c660Smiod
3558260c660Smiod out:
3568260c660Smiod DPRINTF(("bpp done %x\n", error));
3578260c660Smiod s = splbpp();
3588260c660Smiod sc->sc_flags &= ~BPP_LOCKED;
3598260c660Smiod if ((sc->sc_flags & BPP_WANT) != 0) {
3608260c660Smiod sc->sc_flags &= ~BPP_WANT;
3618260c660Smiod wakeup(sc->sc_buf);
3628260c660Smiod }
3638260c660Smiod splx(s);
3648260c660Smiod return (error);
3658260c660Smiod }
3668260c660Smiod
3678260c660Smiod int
bppioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)3688260c660Smiod bppioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
3698260c660Smiod {
3708260c660Smiod int error = 0;
3718260c660Smiod
3728260c660Smiod switch(cmd) {
3738260c660Smiod default:
3748260c660Smiod error = ENODEV;
3758260c660Smiod break;
3768260c660Smiod }
3778260c660Smiod
3788260c660Smiod return (error);
3798260c660Smiod }
3808260c660Smiod
3818260c660Smiod int
bppintr(void * arg)3828260c660Smiod bppintr(void *arg)
3838260c660Smiod {
3848260c660Smiod struct bpp_softc *sc = arg;
3858260c660Smiod struct lsi64854_softc *lsi = &sc->sc_lsi64854;
3868260c660Smiod u_int16_t irq;
3878260c660Smiod
3888260c660Smiod /* First handle any possible DMA interrupts */
3898260c660Smiod if (DMA_INTR(lsi) == -1)
3908260c660Smiod sc->sc_error = 1;
3918260c660Smiod
3928260c660Smiod irq = bus_space_read_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR);
3938260c660Smiod /* Ack all interrupts */
3948260c660Smiod bus_space_write_2(lsi->sc_bustag, lsi->sc_regs, L64854_REG_ICR,
3958260c660Smiod irq | BPP_ALLIRQ);
3968260c660Smiod
3978260c660Smiod DPRINTF(("bpp_intr: %x\n", irq));
3988260c660Smiod /* Did our device interrupt? */
3998260c660Smiod if ((irq & BPP_ALLIRQ) == 0)
4008260c660Smiod return (0);
4018260c660Smiod
4028260c660Smiod if ((sc->sc_flags & BPP_LOCKED) != 0)
4038260c660Smiod wakeup(sc);
4048260c660Smiod else if ((sc->sc_flags & BPP_WANT) != 0) {
4058260c660Smiod sc->sc_flags &= ~BPP_WANT;
4068260c660Smiod wakeup(sc->sc_buf);
4078260c660Smiod }
4088260c660Smiod return (1);
4098260c660Smiod }
410