spic.c revision 69873
169873Snsayer/*
269873Snsayer * Copyright (c) 2000  Nick Sayer
369873Snsayer * All rights reserved.
469873Snsayer *
569873Snsayer * Redistribution and use in source and binary forms, with or without
669873Snsayer * modification, are permitted provided that the following conditions
769873Snsayer * are met:
869873Snsayer * 1. Redistributions of source code must retain the above copyright
969873Snsayer *    notice, this list of conditions and the following disclaimer.
1069873Snsayer * 2. Redistributions in binary form must reproduce the above copyright
1169873Snsayer *    notice, this list of conditions and the following disclaimer in the
1269873Snsayer *    documentation and/or other materials provided with the distribution.
1369873Snsayer *
1469873Snsayer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1569873Snsayer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1669873Snsayer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1769873Snsayer * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1869873Snsayer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1969873Snsayer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2069873Snsayer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2169873Snsayer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2269873Snsayer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2369873Snsayer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2469873Snsayer * SUCH DAMAGE.
2569873Snsayer *
2669873Snsayer * spic -- the Sony Programmable I/O Controller
2769873Snsayer *
2869873Snsayer * This device exists on most recent Sony laptops. It is the means by which
2969873Snsayer * you can watch the Jog Dial and some other functions.
3069873Snsayer *
3169873Snsayer * At the moment, this driver merely tries to turn the jog dial into a
3269873Snsayer * device that moused can park on, with the intent of supplying a Z axis
3369873Snsayer * and mouse button out of the jog dial. I suspect that this device will
3469873Snsayer * end up having to support at least 2 different minor devices: One to be
3569873Snsayer * the jog wheel device for moused to camp out on and the other to perform
3669873Snsayer * all of the other miscelaneous functions of this device. But for now,
3769873Snsayer * the jog wheel is all you get.
3869873Snsayer *
3969873Snsayer * At the moment, the data sent back by the device is rather primitive.
4069873Snsayer * It sends a single character per event:
4169873Snsayer * u = up, d = down -- that's the jog button
4269873Snsayer * l = left, r = right -- that's the dial.
4369873Snsayer * "left" and "right" are rather caprecious. They actually represent
4469873Snsayer * ccw and cw, respectively
4569873Snsayer *
4669873Snsayer * What documentation exists is thanks to Andrew Tridge, and his page at
4769873Snsayer * http://samba.org/picturebook/ Special thanks also to Ian Dowse, who
4869873Snsayer * also provided sample code upon which this driver was based.
4969873Snsayer *
5069873Snsayer * $FreeBSD: head/sys/i386/isa/spic.c 69873 2000-12-11 19:41:48Z nsayer $
5169873Snsayer */
5269873Snsayer
5369873Snsayer#include <sys/param.h>
5469873Snsayer#include <sys/systm.h>
5569873Snsayer#include <sys/kernel.h>
5669873Snsayer#include <sys/bus.h>
5769873Snsayer#include <machine/bus.h>
5869873Snsayer#include <sys/rman.h>
5969873Snsayer#include <machine/resource.h>
6069873Snsayer#include <isa/isavar.h>
6169873Snsayer#include <sys/poll.h>
6269873Snsayer#include <machine/pci_cfgreg.h>
6369873Snsayer#include <machine/clock.h>
6469873Snsayer#include <sys/tty.h>
6569873Snsayer#include <sys/conf.h>
6669873Snsayer#include <sys/fcntl.h>
6769873Snsayer#include <sys/dkstat.h>
6869873Snsayer#include <sys/malloc.h>
6969873Snsayer#include <sys/sysctl.h>
7069873Snsayer#include <sys/uio.h>
7169873Snsayer
7269873Snsayer#include <i386/isa/spicreg.h>
7369873Snsayer
7469873Snsayerstatic int spic_pollrate;
7569873Snsayer
7669873SnsayerSYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
7769873Snsayer;
7869873Snsayer
7969873Snsayerdevclass_t spic_devclass;
8069873Snsayer
8169873Snsayerstatic d_open_t		spicopen;
8269873Snsayerstatic d_close_t	spicclose;
8369873Snsayerstatic d_read_t		spicread;
8469873Snsayerstatic d_ioctl_t	spicioctl;
8569873Snsayerstatic d_poll_t		spicpoll;
8669873Snsayer
8769873Snsayerstatic struct cdevsw spic_cdevsw = {
8869873Snsayer        /* open */      spicopen,
8969873Snsayer        /* close */     spicclose,
9069873Snsayer        /* read */      spicread,
9169873Snsayer        /* write */     nowrite,
9269873Snsayer        /* ioctl */     spicioctl,
9369873Snsayer        /* poll */      spicpoll,
9469873Snsayer        /* mmap */      nommap,
9569873Snsayer        /* strategy */  nostrategy,
9669873Snsayer        /* name */      "spic",
9769873Snsayer        /* maj */       CDEV_MAJOR,
9869873Snsayer        /* dump */      nodump,
9969873Snsayer        /* psize */     nopsize,
10069873Snsayer        /* flags */     0,
10169873Snsayer        /* bmaj */      -1
10269873Snsayer};
10369873Snsayer
10469873Snsayer#define SCBUFLEN 128
10569873Snsayer
10669873Snsayerstruct spic_softc {
10769873Snsayer	u_short sc_port_addr;
10869873Snsayer	u_char sc_intr;
10969873Snsayer	struct resource *sc_port_res,*sc_intr_res;
11069873Snsayer	int	sc_port_rid,sc_intr_rid;
11169873Snsayer	int sc_opened;
11269873Snsayer	int sc_sleeping;
11369873Snsayer	int sc_buttonlast;
11469873Snsayer	struct callout_handle sc_timeout_ch;
11569873Snsayer	device_t sc_dev;
11669873Snsayer	struct selinfo sc_rsel;
11769873Snsayer	u_char sc_buf[SCBUFLEN];
11869873Snsayer	int sc_count;
11969873Snsayer};
12069873Snsayer
12169873Snsayerstatic void
12269873Snsayerwrite_port1(struct spic_softc *sc, u_char val)
12369873Snsayer{
12469873Snsayer	DELAY(10);
12569873Snsayer	outb(sc->sc_port_addr, val);
12669873Snsayer}
12769873Snsayer
12869873Snsayerstatic void
12969873Snsayerwrite_port2(struct spic_softc *sc, u_char val)
13069873Snsayer{
13169873Snsayer	DELAY(10);
13269873Snsayer	outb(sc->sc_port_addr + 4, val);
13369873Snsayer}
13469873Snsayer
13569873Snsayerstatic u_char
13669873Snsayerread_port1(struct spic_softc *sc)
13769873Snsayer{
13869873Snsayer	DELAY(10);
13969873Snsayer	return inb(sc->sc_port_addr);
14069873Snsayer}
14169873Snsayer
14269873Snsayerstatic u_char
14369873Snsayerread_port2(struct spic_softc *sc)
14469873Snsayer{
14569873Snsayer	DELAY(10);
14669873Snsayer	return inb(sc->sc_port_addr + 4);
14769873Snsayer}
14869873Snsayer
14969873Snsayerstatic void
15069873Snsayerbusy_wait(struct spic_softc *sc)
15169873Snsayer{
15269873Snsayer	int i=0;
15369873Snsayer
15469873Snsayer	while(read_port2(sc) & 2) {
15569873Snsayer		DELAY(10);
15669873Snsayer		if (i++>10000) {
15769873Snsayer			printf("spic busy wait abort\n");
15869873Snsayer			return;
15969873Snsayer		}
16069873Snsayer	}
16169873Snsayer}
16269873Snsayer
16369873Snsayerstatic u_char
16469873Snsayerspic_call1(struct spic_softc *sc, u_char dev) {
16569873Snsayer	busy_wait(sc);
16669873Snsayer	write_port2(sc, dev);
16769873Snsayer	read_port2(sc);
16869873Snsayer	return read_port1(sc);
16969873Snsayer}
17069873Snsayer
17169873Snsayerstatic u_char
17269873Snsayerspic_call2(struct spic_softc *sc, u_char dev, u_char fn)
17369873Snsayer{
17469873Snsayer	busy_wait(sc);
17569873Snsayer	write_port2(sc, dev);
17669873Snsayer	busy_wait(sc);
17769873Snsayer	write_port1(sc, fn);
17869873Snsayer	return read_port1(sc);
17969873Snsayer}
18069873Snsayer
18169873Snsayerstatic int
18269873Snsayerspic_probe(device_t dev)
18369873Snsayer{
18469873Snsayer        struct spic_softc *sc;
18569873Snsayer	u_char t, spic_irq;
18669873Snsayer
18769873Snsayer        sc = device_get_softc(dev);
18869873Snsayer
18969873Snsayer	/*
19069873Snsayer	 * We can only have 1 of these. Attempting to probe for a unit 1
19169873Snsayer	 * will destroy the work we did for unit 0
19269873Snsayer	 */
19369873Snsayer	if (device_get_unit(dev))
19469873Snsayer		return ENXIO;
19569873Snsayer
19669873Snsayer	bzero(sc, sizeof(struct spic_softc));
19769873Snsayer
19869873Snsayer	if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
19969873Snsayer		&sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
20069873Snsayer		device_printf(dev,"Couldn't map I/O\n");
20169873Snsayer		return ENXIO;
20269873Snsayer	}
20369873Snsayer	sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
20469873Snsayer
20569873Snsayer#ifdef notyet
20669873Snsayer	if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
20769873Snsayer		&sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
20869873Snsayer		device_printf(dev,"Couldn't map IRQ\n");
20969873Snsayer		bus_release_resource(dev, SYS_RES_IOPORT,
21069873Snsayer			sc->sc_port_rid, sc->sc_port_res);
21169873Snsayer		return ENXIO;
21269873Snsayer	}
21369873Snsayer	sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
21469873Snsayer
21569873Snsayer	switch (sc->sc_intr) {
21669873Snsayer		case 0: spic_irq = 3; break;
21769873Snsayer		case 5: spic_irq = 0; break;
21869873Snsayer		case 0xa: spic_irq = 1; break;
21969873Snsayer		case 0xb: spic_irq = 2; break;
22069873Snsayer		default: device_printf(dev,"Invalid IRQ\n");
22169873Snsayer			bus_release_resource(dev, SYS_RES_IOPORT,
22269873Snsayer				sc->sc_port_rid, sc->sc_port_res);
22369873Snsayer			bus_release_resource(dev, SYS_RES_IRQ,
22469873Snsayer				sc->sc_intr_rid, sc->sc_intr_res);
22569873Snsayer			return ENXIO;
22669873Snsayer	}
22769873Snsayer#else
22869873Snsayer	spic_irq = 3;
22969873Snsayer#endif
23069873Snsayer
23169873Snsayer#if 0
23269873Snsayer	if (sc->sc_port_addr != 0x10A0) {
23369873Snsayer		bus_release_resource(dev, SYS_RES_IOPORT,
23469873Snsayer			sc->sc_port_rid, sc->sc_port_res);
23569873Snsayer		bus_release_resource(dev, SYS_RES_IRQ,
23669873Snsayer			sc->sc_intr_rid, sc->sc_intr_res);
23769873Snsayer		return ENXIO;
23869873Snsayer	}
23969873Snsayer#endif
24069873Snsayer
24169873Snsayer	/* PIIX4 chipset at least? */
24269873Snsayer	if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) !=
24369873Snsayer		PIIX4_DEVID)
24469873Snsayer		return ENXIO;
24569873Snsayer
24669873Snsayer	/*
24769873Snsayer	 * This is an ugly hack. It is necessary until ACPI works correctly.
24869873Snsayer	 *
24969873Snsayer	 * The SPIC consists of 2 registers. They are mapped onto I/O by the
25069873Snsayer	 * PIIX4's General Device 10 function. There is also an interrupt
25169873Snsayer	 * control port at a somewhat magic location, but this first pass is
25269873Snsayer	 * polled.
25369873Snsayer	 *
25469873Snsayer	 * So the first thing we need to do is map the G10 space in.
25569873Snsayer	 *
25669873Snsayer	 */
25769873Snsayer
25869873Snsayer	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
25969873Snsayer		sc->sc_port_addr, 2);
26069873Snsayer	t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
26169873Snsayer	t &= 0xf0;
26269873Snsayer	t |= 4;
26369873Snsayer	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
26469873Snsayer	outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
26569873Snsayer	t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
26669873Snsayer	t &= 0x1f;
26769873Snsayer	t |= 0xc0;
26869873Snsayer	pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
26969873Snsayer
27069873Snsayer	/*
27169873Snsayer	 * XXX: Should try and see if there's anything actually there.
27269873Snsayer	 */
27369873Snsayer
27469873Snsayer	device_set_desc(dev, "Sony Programmable I/O Controller");
27569873Snsayer
27669873Snsayer	return 0;
27769873Snsayer}
27869873Snsayer
27969873Snsayerstatic int
28069873Snsayerspic_attach(device_t dev)
28169873Snsayer{
28269873Snsayer        struct spic_softc *sc;
28369873Snsayer
28469873Snsayer        sc = device_get_softc(dev);
28569873Snsayer
28669873Snsayer	sc->sc_dev = dev;
28769873Snsayer
28869873Snsayer	spic_pollrate = (hz/50); /* Every 50th of a second */
28969873Snsayer
29069873Snsayer	spic_call1(sc, 0x82);
29169873Snsayer	spic_call2(sc, 0x81, 0xff);
29269873Snsayer	spic_call1(sc, 0x92);
29369873Snsayer
29469873Snsayer        /* There can be only one */
29569873Snsayer        make_dev(&spic_cdevsw, 0, 0, 0, 0600, "jogdial");
29669873Snsayer
29769873Snsayer	return 0;
29869873Snsayer}
29969873Snsayer
30069873Snsayerstatic void
30169873Snsayerspictimeout(void *arg)
30269873Snsayer{
30369873Snsayer        struct spic_softc *sc = arg;
30469873Snsayer	u_char b, event, param;
30569873Snsayer	int j;
30669873Snsayer
30769873Snsayer        if (!sc->sc_opened) {
30869873Snsayer                device_printf(sc->sc_dev, "timeout called while closed!\n");
30969873Snsayer                return;
31069873Snsayer        }
31169873Snsayer
31269873Snsayer	event = read_port2(sc);
31369873Snsayer	param = read_port1(sc);
31469873Snsayer
31569873Snsayer	if ((event != 4) && (!(event & 0x1)))
31669873Snsayer		switch(event) {
31769873Snsayer			case 0x10: /* jog wheel event */
31869873Snsayer				b = !!(param & 0x40);
31969873Snsayer				if (b != sc->sc_buttonlast) {
32069873Snsayer					sc->sc_buttonlast = b;
32169873Snsayer					sc->sc_buf[sc->sc_count++] =
32269873Snsayer						b?'d':'u';
32369873Snsayer				}
32469873Snsayer				j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
32569873Snsayer				if (j<0)
32669873Snsayer					while(j++!=0) {
32769873Snsayer						sc->sc_buf[sc->sc_count++] =
32869873Snsayer							'l';
32969873Snsayer					}
33069873Snsayer				else if (j>0)
33169873Snsayer					while(j--!=0) {
33269873Snsayer						sc->sc_buf[sc->sc_count++] =
33369873Snsayer							'r';
33469873Snsayer					}
33569873Snsayer				break;
33669873Snsayer			case 0x60: /* Capture button */
33769873Snsayer				printf("Capture button event: %x\n",param);
33869873Snsayer				break;
33969873Snsayer			case 0x30: /* Lid switch */
34069873Snsayer				printf("Lid switch event: %x\n",param);
34169873Snsayer				break;
34269873Snsayer			default:
34369873Snsayer				printf("Unknown event: %x %x\n", event, param);
34469873Snsayer				break;
34569873Snsayer		}
34669873Snsayer	else {
34769873Snsayer		/* No event. Wait some more */
34869873Snsayer        	sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
34969873Snsayer		return;
35069873Snsayer	}
35169873Snsayer
35269873Snsayer	if (sc->sc_count) {
35369873Snsayer		if (sc->sc_sleeping) {
35469873Snsayer			sc->sc_sleeping = 0;
35569873Snsayer			wakeup((caddr_t) sc);
35669873Snsayer		}
35769873Snsayer		selwakeup(&sc->sc_rsel);
35869873Snsayer	}
35969873Snsayer
36069873Snsayer	spic_call2(sc, 0x81, 0xff); /* Clear event */
36169873Snsayer
36269873Snsayer        sc->sc_timeout_ch = timeout(spictimeout, sc, spic_pollrate);
36369873Snsayer}
36469873Snsayer
36569873Snsayerstatic int
36669873Snsayerspicopen(dev_t dev, int flag, int fmt, struct proc *p)
36769873Snsayer{
36869873Snsayer        struct spic_softc *sc;
36969873Snsayer
37069873Snsayer        sc = devclass_get_softc(spic_devclass, 0);
37169873Snsayer
37269873Snsayer        if (sc->sc_opened)
37369873Snsayer                return EBUSY;
37469873Snsayer
37569873Snsayer	sc->sc_opened++;
37669873Snsayer	sc->sc_count=0;
37769873Snsayer
37869873Snsayer        /* Start the polling */
37969873Snsayer        timeout(spictimeout, sc, spic_pollrate);
38069873Snsayer        return 0;
38169873Snsayer}
38269873Snsayer
38369873Snsayerstatic int
38469873Snsayerspicclose(dev_t dev, int flag, int fmt, struct proc *p)
38569873Snsayer{
38669873Snsayer        struct spic_softc *sc;
38769873Snsayer
38869873Snsayer        sc = devclass_get_softc(spic_devclass, 0);
38969873Snsayer
39069873Snsayer        /* Stop polling */
39169873Snsayer        untimeout(spictimeout, sc, sc->sc_timeout_ch);
39269873Snsayer        sc->sc_opened = 0;
39369873Snsayer        return 0;
39469873Snsayer}
39569873Snsayer
39669873Snsayerstatic int
39769873Snsayerspicread(dev_t dev, struct uio *uio, int flag)
39869873Snsayer{
39969873Snsayer        struct spic_softc *sc;
40069873Snsayer	int l, s, error;
40169873Snsayer	u_char buf[SCBUFLEN];
40269873Snsayer
40369873Snsayer        sc = devclass_get_softc(spic_devclass, 0);
40469873Snsayer
40569873Snsayer	if (uio->uio_resid <= 0) /* What kind of a read is this?! */
40669873Snsayer		return 0;
40769873Snsayer
40869873Snsayer	s = spltty();
40969873Snsayer	while (!(sc->sc_count)) {
41069873Snsayer		sc->sc_sleeping=1;
41169873Snsayer		error = tsleep((caddr_t) sc, PZERO | PCATCH, "jogrea", 0);
41269873Snsayer		sc->sc_sleeping=0;
41369873Snsayer		if (error) {
41469873Snsayer			splx(s);
41569873Snsayer			return error;
41669873Snsayer		}
41769873Snsayer	}
41869873Snsayer	splx(s);
41969873Snsayer
42069873Snsayer	s = spltty();
42169873Snsayer	l = min(uio->uio_resid, sc->sc_count);
42269873Snsayer	bcopy(sc->sc_buf, buf, l);
42369873Snsayer	sc->sc_count -= l;
42469873Snsayer	bcopy(sc->sc_buf + l, sc->sc_buf, l);
42569873Snsayer	splx(s);
42669873Snsayer	return uiomove(buf, l, uio);
42769873Snsayer
42869873Snsayer}
42969873Snsayer
43069873Snsayerstatic int
43169873Snsayerspicioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
43269873Snsayer{
43369873Snsayer        struct spic_softc *sc;
43469873Snsayer
43569873Snsayer        sc = devclass_get_softc(spic_devclass, 0);
43669873Snsayer
43769873Snsayer	return EIO;
43869873Snsayer}
43969873Snsayer
44069873Snsayerstatic int
44169873Snsayerspicpoll(dev_t dev, int events, struct proc *p)
44269873Snsayer{
44369873Snsayer        struct spic_softc *sc;
44469873Snsayer        int revents = 0, s;
44569873Snsayer
44669873Snsayer        sc = devclass_get_softc(spic_devclass, 0);
44769873Snsayer        s = spltty();
44869873Snsayer        if (events & (POLLIN | POLLRDNORM)) {
44969873Snsayer                if (sc->sc_count)
45069873Snsayer                        revents |= events & (POLLIN | POLLRDNORM);
45169873Snsayer                else
45269873Snsayer                        selrecord(p, &sc->sc_rsel); /* Who shall we wake? */
45369873Snsayer        }
45469873Snsayer        splx(s);
45569873Snsayer
45669873Snsayer        return revents;
45769873Snsayer}
45869873Snsayer
45969873Snsayer
46069873Snsayerstatic device_method_t spic_methods[] = {
46169873Snsayer	DEVMETHOD(device_probe,		spic_probe),
46269873Snsayer	DEVMETHOD(device_attach,	spic_attach),
46369873Snsayer
46469873Snsayer	{ 0, 0 }
46569873Snsayer};
46669873Snsayer
46769873Snsayerstatic driver_t spic_driver = {
46869873Snsayer	"spic",
46969873Snsayer	spic_methods,
47069873Snsayer	sizeof(struct spic_softc),
47169873Snsayer};
47269873Snsayer
47369873SnsayerDRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
47469873Snsayer
475