Home | History | Annotate | Line # | Download | only in dev
ki2c.c revision 1.1
      1  1.1  grant /*	$NetBSD: ki2c.c,v 1.1 2003/12/27 02:19:34 grant Exp $	*/
      2  1.1  grant /*	Id: ki2c.c,v 1.7 2002/10/05 09:56:05 tsubai Exp	*/
      3  1.1  grant 
      4  1.1  grant /*-
      5  1.1  grant  * Copyright (c) 2001 Tsubai Masanari.  All rights reserved.
      6  1.1  grant  *
      7  1.1  grant  * Redistribution and use in source and binary forms, with or without
      8  1.1  grant  * modification, are permitted provided that the following conditions
      9  1.1  grant  * are met:
     10  1.1  grant  * 1. Redistributions of source code must retain the above copyright
     11  1.1  grant  *    notice, this list of conditions and the following disclaimer.
     12  1.1  grant  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  grant  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  grant  *    documentation and/or other materials provided with the distribution.
     15  1.1  grant  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  grant  *    derived from this software without specific prior written permission.
     17  1.1  grant  *
     18  1.1  grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  grant  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  grant  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  grant  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  grant  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  grant  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  grant  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  grant  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  grant  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  1.1  grant  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  grant  */
     29  1.1  grant 
     30  1.1  grant #include <sys/param.h>
     31  1.1  grant #include <sys/device.h>
     32  1.1  grant #include <sys/systm.h>
     33  1.1  grant 
     34  1.1  grant #include <dev/ofw/openfirm.h>
     35  1.1  grant #include <uvm/uvm_extern.h>
     36  1.1  grant #include <machine/autoconf.h>
     37  1.1  grant 
     38  1.1  grant /* Keywest I2C Register offsets */
     39  1.1  grant #define MODE	0
     40  1.1  grant #define CONTROL	1
     41  1.1  grant #define STATUS	2
     42  1.1  grant #define ISR	3
     43  1.1  grant #define IER	4
     44  1.1  grant #define ADDR	5
     45  1.1  grant #define SUBADDR	6
     46  1.1  grant #define DATA	7
     47  1.1  grant 
     48  1.1  grant /* MODE */
     49  1.1  grant #define I2C_SPEED	0x03	/* Speed mask */
     50  1.1  grant #define  I2C_100kHz	0x00
     51  1.1  grant #define  I2C_50kHz	0x01
     52  1.1  grant #define  I2C_25kHz	0x02
     53  1.1  grant #define I2C_MODE	0x0c	/* Mode mask */
     54  1.1  grant #define  I2C_DUMBMODE	0x00	/*  Dumb mode */
     55  1.1  grant #define  I2C_STDMODE	0x04	/*  Standard mode */
     56  1.1  grant #define  I2C_STDSUBMODE	0x08	/*  Standard mode + sub address */
     57  1.1  grant #define  I2C_COMBMODE	0x0c	/*  Combined mode */
     58  1.1  grant #define I2C_PORT	0xf0	/* Port mask */
     59  1.1  grant 
     60  1.1  grant /* CONTROL */
     61  1.1  grant #define I2C_CT_AAK	0x01	/* Send AAK */
     62  1.1  grant #define I2C_CT_ADDR	0x02	/* Send address(es) */
     63  1.1  grant #define I2C_CT_STOP	0x04	/* Send STOP */
     64  1.1  grant #define I2C_CT_START	0x08	/* Send START */
     65  1.1  grant 
     66  1.1  grant /* STATUS */
     67  1.1  grant #define I2C_ST_BUSY	0x01	/* Busy */
     68  1.1  grant #define I2C_ST_LASTAAK	0x02	/* Last AAK */
     69  1.1  grant #define I2C_ST_LASTRW	0x04	/* Last R/W */
     70  1.1  grant #define I2C_ST_SDA	0x08	/* SDA */
     71  1.1  grant #define I2C_ST_SCL	0x10	/* SCL */
     72  1.1  grant 
     73  1.1  grant /* ISR/IER */
     74  1.1  grant #define I2C_INT_DATA	0x01	/* Data byte sent/received */
     75  1.1  grant #define I2C_INT_ADDR	0x02	/* Address sent */
     76  1.1  grant #define I2C_INT_STOP	0x04	/* STOP condition sent */
     77  1.1  grant #define I2C_INT_START	0x08	/* START condition sent */
     78  1.1  grant 
     79  1.1  grant /* I2C flags */
     80  1.1  grant #define I2C_BUSY	0x01
     81  1.1  grant #define I2C_READING	0x02
     82  1.1  grant #define I2C_ERROR	0x04
     83  1.1  grant 
     84  1.1  grant struct ki2c_softc {
     85  1.1  grant 	struct device sc_dev;
     86  1.1  grant 	u_char *sc_reg;
     87  1.1  grant 	int sc_regstep;
     88  1.1  grant 
     89  1.1  grant 	int sc_flags;
     90  1.1  grant 	u_char *sc_data;
     91  1.1  grant 	int sc_resid;
     92  1.1  grant };
     93  1.1  grant 
     94  1.1  grant int ki2c_match(struct device *, struct cfdata *, void *);
     95  1.1  grant void ki2c_attach(struct device *, struct device *, void *);
     96  1.1  grant inline u_int ki2c_readreg(struct ki2c_softc *, int);
     97  1.1  grant inline void ki2c_writereg(struct ki2c_softc *, int, u_int);
     98  1.1  grant u_int ki2c_getmode(struct ki2c_softc *);
     99  1.1  grant void ki2c_setmode(struct ki2c_softc *, u_int);
    100  1.1  grant u_int ki2c_getspeed(struct ki2c_softc *);
    101  1.1  grant void ki2c_setspeed(struct ki2c_softc *, u_int);
    102  1.1  grant int ki2c_intr(struct ki2c_softc *);
    103  1.1  grant int ki2c_poll(struct ki2c_softc *, int);
    104  1.1  grant int ki2c_start(struct ki2c_softc *, int, int, void *, int);
    105  1.1  grant int ki2c_read(struct ki2c_softc *, int, int, void *, int);
    106  1.1  grant int ki2c_write(struct ki2c_softc *, int, int, const void *, int);
    107  1.1  grant 
    108  1.1  grant struct cfattach ki2c_ca = {
    109  1.1  grant 	"ki2c", {}, sizeof(struct ki2c_softc), ki2c_match, ki2c_attach
    110  1.1  grant };
    111  1.1  grant 
    112  1.1  grant int
    113  1.1  grant ki2c_match(parent, match, aux)
    114  1.1  grant 	struct device *parent;
    115  1.1  grant 	struct cfdata *match;
    116  1.1  grant 	void *aux;
    117  1.1  grant {
    118  1.1  grant 	struct confargs *ca = aux;
    119  1.1  grant 
    120  1.1  grant 	if (strcmp(ca->ca_name, "i2c") == 0)
    121  1.1  grant 		return 1;
    122  1.1  grant 
    123  1.1  grant 	return 0;
    124  1.1  grant }
    125  1.1  grant 
    126  1.1  grant void
    127  1.1  grant ki2c_attach(parent, self, aux)
    128  1.1  grant 	struct device *parent;
    129  1.1  grant 	struct device *self;
    130  1.1  grant 	void *aux;
    131  1.1  grant {
    132  1.1  grant 	struct ki2c_softc *sc = (struct ki2c_softc *)self;
    133  1.1  grant 	struct confargs *ca = aux;
    134  1.1  grant 	int node = ca->ca_node;
    135  1.1  grant 	int rate;
    136  1.1  grant 
    137  1.1  grant 	ca->ca_reg[0] += ca->ca_baseaddr;
    138  1.1  grant 
    139  1.1  grant 	if (OF_getprop(node, "AAPL,i2c-rate", &rate, 4) != 4) {
    140  1.1  grant 		printf(": cannot get i2c-rate\n");
    141  1.1  grant 		return;
    142  1.1  grant 	}
    143  1.1  grant 	if (OF_getprop(node, "AAPL,address", &sc->sc_reg, 4) != 4) {
    144  1.1  grant 		printf(": unable to find i2c address\n");
    145  1.1  grant 		return;
    146  1.1  grant 	}
    147  1.1  grant 	if (OF_getprop(node, "AAPL,address-step", &sc->sc_regstep, 4) != 4) {
    148  1.1  grant 		printf(": unable to find i2c address step\n");
    149  1.1  grant 		return;
    150  1.1  grant 	}
    151  1.1  grant 
    152  1.1  grant 	printf("\n");
    153  1.1  grant 
    154  1.1  grant 	ki2c_writereg(sc, STATUS, 0);
    155  1.1  grant 	ki2c_writereg(sc, ISR, 0);
    156  1.1  grant 	ki2c_writereg(sc, IER, 0);
    157  1.1  grant 
    158  1.1  grant 	ki2c_setmode(sc, I2C_STDSUBMODE);
    159  1.1  grant 	ki2c_setspeed(sc, I2C_100kHz);		/* XXX rate */
    160  1.1  grant }
    161  1.1  grant 
    162  1.1  grant u_int
    163  1.1  grant ki2c_readreg(sc, reg)
    164  1.1  grant 	struct ki2c_softc *sc;
    165  1.1  grant 	int reg;
    166  1.1  grant {
    167  1.1  grant 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    168  1.1  grant 
    169  1.1  grant 	return *addr;
    170  1.1  grant }
    171  1.1  grant 
    172  1.1  grant void
    173  1.1  grant ki2c_writereg(sc, reg, val)
    174  1.1  grant 	struct ki2c_softc *sc;
    175  1.1  grant 	int reg;
    176  1.1  grant 	u_int val;
    177  1.1  grant {
    178  1.1  grant 	u_char *addr = sc->sc_reg + sc->sc_regstep * reg;
    179  1.1  grant 
    180  1.1  grant 	*addr = val;
    181  1.1  grant 	asm volatile ("eieio");
    182  1.1  grant 	delay(10);
    183  1.1  grant }
    184  1.1  grant 
    185  1.1  grant u_int
    186  1.1  grant ki2c_getmode(sc)
    187  1.1  grant 	struct ki2c_softc *sc;
    188  1.1  grant {
    189  1.1  grant 	return ki2c_readreg(sc, MODE) & I2C_MODE;
    190  1.1  grant }
    191  1.1  grant 
    192  1.1  grant void
    193  1.1  grant ki2c_setmode(sc, mode)
    194  1.1  grant 	struct ki2c_softc *sc;
    195  1.1  grant 	u_int mode;
    196  1.1  grant {
    197  1.1  grant 	u_int x;
    198  1.1  grant 
    199  1.1  grant 	KASSERT((mode & ~I2C_MODE) == 0);
    200  1.1  grant 	x = ki2c_readreg(sc, MODE);
    201  1.1  grant 	x &= ~I2C_MODE;
    202  1.1  grant 	x |= mode;
    203  1.1  grant 	ki2c_writereg(sc, MODE, x);
    204  1.1  grant }
    205  1.1  grant 
    206  1.1  grant u_int
    207  1.1  grant ki2c_getspeed(sc)
    208  1.1  grant 	struct ki2c_softc *sc;
    209  1.1  grant {
    210  1.1  grant 	return ki2c_readreg(sc, MODE) & I2C_SPEED;
    211  1.1  grant }
    212  1.1  grant 
    213  1.1  grant void
    214  1.1  grant ki2c_setspeed(sc, speed)
    215  1.1  grant 	struct ki2c_softc *sc;
    216  1.1  grant 	u_int speed;
    217  1.1  grant {
    218  1.1  grant 	u_int x;
    219  1.1  grant 
    220  1.1  grant 	KASSERT((speed & ~I2C_SPEED) == 0);
    221  1.1  grant 	x = ki2c_readreg(sc, MODE);
    222  1.1  grant 	x &= ~I2C_SPEED;
    223  1.1  grant 	x |= speed;
    224  1.1  grant 	ki2c_writereg(sc, MODE, x);
    225  1.1  grant }
    226  1.1  grant 
    227  1.1  grant int
    228  1.1  grant ki2c_intr(sc)
    229  1.1  grant 	struct ki2c_softc *sc;
    230  1.1  grant {
    231  1.1  grant 	u_int isr, x;
    232  1.1  grant 
    233  1.1  grant 	isr = ki2c_readreg(sc, ISR);
    234  1.1  grant 
    235  1.1  grant 	if (isr & I2C_INT_ADDR) {
    236  1.1  grant #if 0
    237  1.1  grant 		if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    238  1.1  grant 			/* No slave responded. */
    239  1.1  grant 			sc->sc_flags |= I2C_ERROR;
    240  1.1  grant 			goto out;
    241  1.1  grant 		}
    242  1.1  grant #endif
    243  1.1  grant 
    244  1.1  grant 		if (sc->sc_flags & I2C_READING) {
    245  1.1  grant 			if (sc->sc_resid > 1) {
    246  1.1  grant 				x = ki2c_readreg(sc, CONTROL);
    247  1.1  grant 				x |= I2C_CT_AAK;
    248  1.1  grant 				ki2c_writereg(sc, CONTROL, x);
    249  1.1  grant 			}
    250  1.1  grant 		} else {
    251  1.1  grant 			ki2c_writereg(sc, DATA, *sc->sc_data++);
    252  1.1  grant 			sc->sc_resid--;
    253  1.1  grant 		}
    254  1.1  grant 	}
    255  1.1  grant 
    256  1.1  grant 	if (isr & I2C_INT_DATA) {
    257  1.1  grant 		if (sc->sc_flags & I2C_READING) {
    258  1.1  grant 			*sc->sc_data++ = ki2c_readreg(sc, DATA);
    259  1.1  grant 			sc->sc_resid--;
    260  1.1  grant 
    261  1.1  grant 			if (sc->sc_resid == 0) {	/* Completed */
    262  1.1  grant 				ki2c_writereg(sc, CONTROL, 0);
    263  1.1  grant 				goto out;
    264  1.1  grant 			}
    265  1.1  grant 		} else {
    266  1.1  grant #if 0
    267  1.1  grant 			if ((ki2c_readreg(sc, STATUS) & I2C_ST_LASTAAK) == 0) {
    268  1.1  grant 				/* No slave responded. */
    269  1.1  grant 				sc->sc_flags |= I2C_ERROR;
    270  1.1  grant 				goto out;
    271  1.1  grant 			}
    272  1.1  grant #endif
    273  1.1  grant 
    274  1.1  grant 			if (sc->sc_resid == 0) {
    275  1.1  grant 				x = ki2c_readreg(sc, CONTROL) | I2C_CT_STOP;
    276  1.1  grant 				ki2c_writereg(sc, CONTROL, x);
    277  1.1  grant 			} else {
    278  1.1  grant 				ki2c_writereg(sc, DATA, *sc->sc_data++);
    279  1.1  grant 				sc->sc_resid--;
    280  1.1  grant 			}
    281  1.1  grant 		}
    282  1.1  grant 	}
    283  1.1  grant 
    284  1.1  grant out:
    285  1.1  grant 	if (isr & I2C_INT_STOP) {
    286  1.1  grant 		ki2c_writereg(sc, CONTROL, 0);
    287  1.1  grant 		sc->sc_flags &= ~I2C_BUSY;
    288  1.1  grant 	}
    289  1.1  grant 
    290  1.1  grant 	ki2c_writereg(sc, ISR, isr);
    291  1.1  grant 
    292  1.1  grant 	return 1;
    293  1.1  grant }
    294  1.1  grant 
    295  1.1  grant int
    296  1.1  grant ki2c_poll(sc, timo)
    297  1.1  grant 	struct ki2c_softc *sc;
    298  1.1  grant 	int timo;
    299  1.1  grant {
    300  1.1  grant 	while (sc->sc_flags & I2C_BUSY) {
    301  1.1  grant 		if (ki2c_readreg(sc, ISR))
    302  1.1  grant 			ki2c_intr(sc);
    303  1.1  grant 		timo -= 100;
    304  1.1  grant 		if (timo < 0) {
    305  1.1  grant 			printf("i2c_poll: timeout\n");
    306  1.1  grant 			return -1;
    307  1.1  grant 		}
    308  1.1  grant 		delay(100);
    309  1.1  grant 	}
    310  1.1  grant 	return 0;
    311  1.1  grant }
    312  1.1  grant 
    313  1.1  grant int
    314  1.1  grant ki2c_start(sc, addr, subaddr, data, len)
    315  1.1  grant 	struct ki2c_softc *sc;
    316  1.1  grant 	int addr, subaddr, len;
    317  1.1  grant 	void *data;
    318  1.1  grant {
    319  1.1  grant 	int rw = (sc->sc_flags & I2C_READING) ? 1 : 0;
    320  1.1  grant 	int timo, x;
    321  1.1  grant 
    322  1.1  grant 	KASSERT((addr & 1) == 0);
    323  1.1  grant 
    324  1.1  grant 	sc->sc_data = data;
    325  1.1  grant 	sc->sc_resid = len;
    326  1.1  grant 	sc->sc_flags |= I2C_BUSY;
    327  1.1  grant 
    328  1.1  grant 	timo = 1000 + len * 200;
    329  1.1  grant 
    330  1.1  grant 	/* XXX TAS3001 sometimes takes 50ms to finish writing registers. */
    331  1.1  grant 	/* if (addr == 0x68) */
    332  1.1  grant 		timo += 100000;
    333  1.1  grant 
    334  1.1  grant 	ki2c_writereg(sc, ADDR, addr | rw);
    335  1.1  grant 	ki2c_writereg(sc, SUBADDR, subaddr);
    336  1.1  grant 
    337  1.1  grant 	x = ki2c_readreg(sc, CONTROL) | I2C_CT_ADDR;
    338  1.1  grant 	ki2c_writereg(sc, CONTROL, x);
    339  1.1  grant 
    340  1.1  grant 	if (ki2c_poll(sc, timo))
    341  1.1  grant 		return -1;
    342  1.1  grant 	if (sc->sc_flags & I2C_ERROR) {
    343  1.1  grant 		printf("I2C_ERROR\n");
    344  1.1  grant 		return -1;
    345  1.1  grant 	}
    346  1.1  grant 	return 0;
    347  1.1  grant }
    348  1.1  grant 
    349  1.1  grant int
    350  1.1  grant ki2c_read(sc, addr, subaddr, data, len)
    351  1.1  grant 	struct ki2c_softc *sc;
    352  1.1  grant 	int addr, subaddr, len;
    353  1.1  grant 	void *data;
    354  1.1  grant {
    355  1.1  grant 	sc->sc_flags = I2C_READING;
    356  1.1  grant 	return ki2c_start(sc, addr, subaddr, data, len);
    357  1.1  grant }
    358  1.1  grant 
    359  1.1  grant int
    360  1.1  grant ki2c_write(sc, addr, subaddr, data, len)
    361  1.1  grant 	struct ki2c_softc *sc;
    362  1.1  grant 	int addr, subaddr, len;
    363  1.1  grant 	const void *data;
    364  1.1  grant {
    365  1.1  grant 	sc->sc_flags = 0;
    366  1.1  grant 	return ki2c_start(sc, addr, subaddr, (void *)data, len);
    367  1.1  grant }
    368