Home | History | Annotate | Line # | Download | only in ralink
      1  1.9  thorpej /*	$NetBSD: ralink_i2c.c,v 1.9 2025/09/15 13:23:02 thorpej Exp $	*/
      2  1.2     matt /*-
      3  1.2     matt  * Copyright (c) 2011 CradlePoint Technology, Inc.
      4  1.2     matt  * All rights reserved.
      5  1.2     matt  *
      6  1.2     matt  *
      7  1.2     matt  * Redistribution and use in source and binary forms, with or without
      8  1.2     matt  * modification, are permitted provided that the following conditions
      9  1.2     matt  * are met:
     10  1.2     matt  * 1. Redistributions of source code must retain the above copyright
     11  1.2     matt  *    notice, this list of conditions and the following disclaimer.
     12  1.2     matt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2     matt  *    notice, this list of conditions and the following disclaimer in the
     14  1.2     matt  *    documentation and/or other materials provided with the distribution.
     15  1.2     matt  *
     16  1.2     matt  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
     17  1.2     matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.2     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.2     matt  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
     20  1.2     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.2     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.2     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.2     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.2     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.2     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.2     matt  * POSSIBILITY OF SUCH DAMAGE.
     27  1.2     matt  */
     28  1.2     matt 
     29  1.2     matt /* ra_i2c.c - Ralink i2c 3052 driver */
     30  1.2     matt 
     31  1.2     matt #include <sys/cdefs.h>
     32  1.9  thorpej __KERNEL_RCSID(0, "$NetBSD: ralink_i2c.c,v 1.9 2025/09/15 13:23:02 thorpej Exp $");
     33  1.2     matt 
     34  1.2     matt #include <sys/param.h>
     35  1.2     matt #include <sys/bus.h>
     36  1.2     matt #include <sys/device.h>
     37  1.2     matt #include <sys/errno.h>
     38  1.2     matt #include <sys/kernel.h>
     39  1.2     matt #include <sys/proc.h>
     40  1.2     matt #include <sys/systm.h>
     41  1.2     matt 
     42  1.2     matt #include <dev/i2c/i2cvar.h>
     43  1.2     matt 
     44  1.2     matt #include <mips/ralink/ralink_var.h>
     45  1.2     matt #include <mips/ralink/ralink_reg.h>
     46  1.2     matt 
     47  1.2     matt #if 0
     48  1.2     matt /*
     49  1.2     matt  * Defined for the Ralink 3050, w/320MHz CPU:  milage may vary.
     50  1.2     matt  * Set the I2C clock to 100K bps (low speed) transfer rate.
     51  1.2     matt  * Value is based upon the forms defined in the Ralink reference document
     52  1.2     matt  * for RT3050, page 53.  JCL.
     53  1.2     matt  */
     54  1.2     matt #define CLKDIV_VALUE 533
     55  1.2     matt #endif
     56  1.2     matt 
     57  1.2     matt /*
     58  1.7    skrll  * Slow the I2C bus clock to 12.5 KHz to work around the misbehavior
     59  1.2     matt  * of the TI part.
     60  1.2     matt  */
     61  1.2     matt #define CLKDIV_VALUE 4264
     62  1.2     matt 
     63  1.2     matt #define i2c_busy_loop		(clkdiv*30)
     64  1.2     matt #define max_ee_busy_loop	(clkdiv*25)
     65  1.2     matt 
     66  1.2     matt 
     67  1.2     matt typedef struct ra_i2c_softc {
     68  1.2     matt 	device_t		sc_dev;
     69  1.2     matt 	struct i2c_controller	sc_i2c;
     70  1.2     matt 	bus_space_tag_t		sc_memt;
     71  1.2     matt 	bus_space_handle_t	sc_i2c_memh;
     72  1.2     matt 	bus_space_handle_t	sc_sy_memh;
     73  1.2     matt } ra_i2c_softc_t;
     74  1.2     matt 
     75  1.2     matt 
     76  1.3      chs static int  ra_i2c_match(device_t, cfdata_t, void *);
     77  1.3      chs static void ra_i2c_attach(device_t, device_t, void *);
     78  1.2     matt 
     79  1.2     matt /* RT3052 I2C functions */
     80  1.2     matt static int  i2c_write(ra_i2c_softc_t *, u_long, const u_char *, u_long);
     81  1.2     matt static int  i2c_read(ra_i2c_softc_t *, u_long, u_char *, u_long);
     82  1.2     matt #ifdef NOTYET
     83  1.2     matt static void i2c_write_stop(ra_i2c_softc_t *, u_long, u_char *);
     84  1.2     matt static void i2c_read_stop(ra_i2c_softc_t *, u_long, u_char *);
     85  1.2     matt #endif
     86  1.2     matt 
     87  1.2     matt /* i2c driver functions */
     88  1.2     matt int  ra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     89  1.2     matt 	void *, size_t, int);
     90  1.2     matt void ra_i2c_reset(ra_i2c_softc_t *);
     91  1.2     matt 
     92  1.2     matt CFATTACH_DECL_NEW(ri2c, sizeof(struct ra_i2c_softc),
     93  1.2     matt 	ra_i2c_match, ra_i2c_attach, NULL, NULL);
     94  1.2     matt 
     95  1.2     matt unsigned int clkdiv = CLKDIV_VALUE;
     96  1.2     matt 
     97  1.2     matt static inline void
     98  1.2     matt ra_i2c_busy_wait(ra_i2c_softc_t *sc)
     99  1.2     matt {
    100  1.2     matt 	for (int i=0; i < i2c_busy_loop; i++) {
    101  1.2     matt 		uint32_t r;
    102  1.2     matt 		r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    103  1.2     matt 			RA_I2C_STATUS);
    104  1.2     matt 		if ((r & I2C_STATUS_BUSY) == 0)
    105  1.2     matt 			break;
    106  1.2     matt 	}
    107  1.2     matt }
    108  1.2     matt 
    109  1.2     matt int
    110  1.2     matt ra_i2c_match(device_t parent, cfdata_t cf, void *aux)
    111  1.2     matt {
    112  1.2     matt 	return 1;
    113  1.2     matt }
    114  1.2     matt 
    115  1.2     matt 
    116  1.2     matt void
    117  1.2     matt ra_i2c_attach(device_t parent, device_t self, void *aux)
    118  1.2     matt {
    119  1.2     matt 	ra_i2c_softc_t * const sc = device_private(self);
    120  1.2     matt 	const struct mainbus_attach_args *ma = aux;
    121  1.2     matt 	uint32_t r;
    122  1.2     matt 	int error;
    123  1.2     matt 
    124  1.2     matt 	aprint_naive(": Ralink I2C controller\n");
    125  1.2     matt 	aprint_normal(": Ralink I2C controller\n");
    126  1.2     matt 
    127  1.2     matt 	/* save out bus space tag */
    128  1.2     matt 	sc->sc_memt = ma->ma_memt;
    129  1.2     matt 
    130  1.2     matt 	/* Map Sysctl registers */
    131  1.2     matt 	if ((error = bus_space_map(ma->ma_memt, RA_SYSCTL_BASE, 0x10000,
    132  1.2     matt 	    0, &sc->sc_sy_memh)) != 0) {
    133  1.2     matt 		aprint_error_dev(self, "unable to map Sysctl registers, "
    134  1.2     matt 			"error=%d\n", error);
    135  1.2     matt 		return;
    136  1.2     matt 	}
    137  1.2     matt 
    138  1.2     matt 	/* map the I2C registers */
    139  1.2     matt 	if ((error = bus_space_map(sc->sc_memt, RA_I2C_BASE, 0x100,
    140  1.2     matt 	    0, &sc->sc_i2c_memh)) != 0) {
    141  1.2     matt 		aprint_error_dev(self, "unable to map registers, "
    142  1.2     matt 			"error=%d\n", error);
    143  1.2     matt 		bus_space_unmap(ma->ma_memt, sc->sc_sy_memh, 0x10000);
    144  1.2     matt 		return;
    145  1.2     matt 	}
    146  1.2     matt 
    147  1.2     matt 	/*  Enable I2C block */
    148  1.2     matt 	r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh,
    149  1.2     matt 		RA_SYSCTL_GPIOMODE);
    150  1.2     matt 	r &= ~GPIOMODE_I2C;
    151  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh,
    152  1.2     matt 		RA_SYSCTL_GPIOMODE, r);
    153  1.2     matt 
    154  1.4  thorpej 	iic_tag_init(&sc->sc_i2c);
    155  1.2     matt 	sc->sc_i2c.ic_cookie = sc;
    156  1.2     matt 	sc->sc_i2c.ic_exec = ra_i2c_exec;
    157  1.2     matt 
    158  1.9  thorpej 	iicbus_attach(self, &sc->sc_i2c);
    159  1.2     matt }
    160  1.2     matt 
    161  1.2     matt 
    162  1.2     matt 
    163  1.2     matt /*
    164  1.2     matt  * I2C API
    165  1.2     matt  */
    166  1.2     matt 
    167  1.2     matt int
    168  1.2     matt ra_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    169  1.2     matt 	size_t cmdlen, void *buf, size_t len, int flags)
    170  1.2     matt {
    171  1.2     matt 	ra_i2c_softc_t * const sc = cookie;
    172  1.2     matt 
    173  1.2     matt 	/*
    174  1.2     matt 	 * Make sure we only pass a seven-bit device address,
    175  1.2     matt 	 * as per I2C standard.
    176  1.2     matt 	 */
    177  1.2     matt 	KASSERT(addr <= 127);
    178  1.2     matt 	if (addr > 127)
    179  1.2     matt 		return -1;
    180  1.2     matt 
    181  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    182  1.2     matt 		addr);
    183  1.2     matt 
    184  1.2     matt 	ra_i2c_reset(sc);
    185  1.2     matt 
    186  1.2     matt 	/*
    187  1.2     matt 	 * Process the requested operation.
    188  1.2     matt 	 * There are four I2C operations of interest:
    189  1.2     matt 	 *   - Write
    190  1.2     matt 	 *   - Write with stop
    191  1.2     matt 	 *   - Read
    192  1.2     matt 	 *   - Read with stop
    193  1.2     matt 	 * Because the I2C block on the Ralink part generates stop markers
    194  1.2     matt 	 * at approprite places, based upon the byte count, the read and write
    195  1.2     matt 	 * with stop operations will rarely be needed.  They are included here
    196  1.2     matt 	 * as placeholders, but haven't been implemented or tested.
    197  1.2     matt 	 */
    198  1.2     matt 	switch(op) {
    199  1.2     matt 	case  I2C_OP_WRITE:
    200  1.2     matt 		return i2c_write(sc, addr, cmdbuf, cmdlen);
    201  1.2     matt 		break;
    202  1.2     matt 	case I2C_OP_READ:
    203  1.2     matt 		return i2c_read(sc, addr, buf, len);
    204  1.2     matt 		break;
    205  1.2     matt #ifdef NOTYET
    206  1.2     matt 	case I2C_OP_WRITE_WITH_STOP:
    207  1.2     matt 		i2c_write_stop(sc, addr, buf);
    208  1.2     matt 		break;
    209  1.2     matt 	case I2C_OP_READ_WITH_STOP:
    210  1.2     matt 		i2c_read_stop(sc, addr, buf);
    211  1.2     matt 		break;
    212  1.2     matt #endif
    213  1.2     matt 	default:
    214  1.2     matt 		return -1;  /* Illegal operation, error return. */
    215  1.2     matt 	}
    216  1.2     matt 
    217  1.2     matt 	return 0;
    218  1.2     matt }
    219  1.2     matt 
    220  1.2     matt static int
    221  1.2     matt i2c_write(ra_i2c_softc_t *sc, u_long addr, const u_char *data,
    222  1.2     matt 	u_long nbytes)
    223  1.2     matt {
    224  1.2     matt 	uint32_t r;
    225  1.2     matt 	int i, j;
    226  1.2     matt 
    227  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    228  1.2     matt 		addr);
    229  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
    230  1.2     matt 		nbytes - 1);
    231  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
    232  1.2     matt 		I2C_OP_WRITE);
    233  1.2     matt 
    234  1.2     matt 	for (i=0; i < nbytes; i++) {
    235  1.2     matt 		for (j=0; j < max_ee_busy_loop; j++) {
    236  1.7    skrll 			r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    237  1.2     matt 				RA_I2C_STATUS);
    238  1.2     matt 			if ((r & I2C_STATUS_SDOEMPTY) != 0) {
    239  1.2     matt 				bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh,
    240  1.2     matt 					RA_I2C_DATAOUT, data[i]);
    241  1.2     matt 				break;
    242  1.2     matt 			}
    243  1.2     matt 		}
    244  1.2     matt #if 0
    245  1.2     matt 		if ((r & I2C_STATUS_ACKERR) != 0) {
    246  1.2     matt 			aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
    247  1.2     matt 				__func__);
    248  1.2     matt 			return EAGAIN;
    249  1.2     matt 		}
    250  1.2     matt #endif
    251  1.2     matt 		if (j == max_ee_busy_loop) {
    252  1.2     matt 			aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
    253  1.2     matt 				__func__);
    254  1.2     matt 			return EAGAIN;
    255  1.7    skrll 		}
    256  1.2     matt 	}
    257  1.2     matt 
    258  1.2     matt 	ra_i2c_busy_wait(sc);
    259  1.2     matt 
    260  1.2     matt 	return 0;
    261  1.2     matt }
    262  1.2     matt 
    263  1.2     matt 
    264  1.2     matt static int
    265  1.2     matt i2c_read(ra_i2c_softc_t *sc, u_long addr, u_char *data, u_long nbytes)
    266  1.2     matt {
    267  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    268  1.2     matt 		addr);
    269  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
    270  1.2     matt 		nbytes - 1);
    271  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
    272  1.2     matt 		I2C_OP_READ);
    273  1.2     matt 
    274  1.2     matt 	for (u_int i = 0; i < nbytes; i++) {
    275  1.2     matt 		u_long j;
    276  1.2     matt 		uint32_t r;
    277  1.2     matt 
    278  1.2     matt 		for (j=0; j < max_ee_busy_loop; j++) {
    279  1.7    skrll 			r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    280  1.2     matt 				RA_I2C_STATUS);
    281  1.2     matt 			if ((r & I2C_STATUS_DATARDY) != 0) {
    282  1.2     matt 				data[i] = bus_space_read_4(
    283  1.2     matt 						sc->sc_memt, sc->sc_i2c_memh,
    284  1.2     matt 						RA_I2C_DATAIN);
    285  1.2     matt 				break;
    286  1.2     matt 			}
    287  1.2     matt 		}
    288  1.2     matt #if 0
    289  1.2     matt 		if ((r & I2C_STATUS_ACKERR) != 0) {
    290  1.2     matt 			aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
    291  1.2     matt 				__func__);
    292  1.2     matt 			return EAGAIN;
    293  1.2     matt 		}
    294  1.2     matt #endif
    295  1.2     matt 		if (j == max_ee_busy_loop) {
    296  1.2     matt 			aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
    297  1.2     matt 				__func__);
    298  1.2     matt 			return EAGAIN;
    299  1.2     matt 		}
    300  1.2     matt 	}
    301  1.2     matt 
    302  1.2     matt 	ra_i2c_busy_wait(sc);
    303  1.2     matt 
    304  1.2     matt 	return 0;
    305  1.2     matt 
    306  1.2     matt }
    307  1.2     matt 
    308  1.2     matt 
    309  1.2     matt #ifdef NOTYET
    310  1.2     matt static void
    311  1.2     matt i2c_write_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
    312  1.2     matt {
    313  1.2     matt 	/* unimplemented */
    314  1.2     matt }
    315  1.2     matt 
    316  1.2     matt static void
    317  1.2     matt i2c_read_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
    318  1.2     matt {
    319  1.2     matt 	/* unimplemented */
    320  1.2     matt }
    321  1.2     matt #endif
    322  1.2     matt 
    323  1.2     matt void
    324  1.2     matt ra_i2c_reset(ra_i2c_softc_t *sc)
    325  1.2     matt {
    326  1.2     matt 	uint32_t r;
    327  1.2     matt 
    328  1.2     matt 	/* reset i2c block */
    329  1.2     matt 	r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST);
    330  1.7    skrll 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST,
    331  1.2     matt 		r | RST_I2C);
    332  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST, r);
    333  1.2     matt 
    334  1.2     matt 	r = I2C_CONFIG_ADDRLEN(I2C_CONFIG_ADDRLEN_8) |
    335  1.2     matt 	    I2C_CONFIG_DEVADLEN(I2C_CONFIG_DEVADLEN_7) |
    336  1.2     matt 	    I2C_CONFIG_ADDRDIS;
    337  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CONFIG, r);
    338  1.2     matt 
    339  1.2     matt 	/*
    340  1.2     matt 	 * Set the I2C clock divider.  Appears to be set to 200,000,
    341  1.2     matt 	 * which is strange, as I2C is 100K/400K/3.?M bps.
    342  1.2     matt 	 */
    343  1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CLKDIV,
    344  1.2     matt 		clkdiv);
    345  1.2     matt }
    346