Home | History | Annotate | Line # | Download | only in ralink
ralink_i2c.c revision 1.5.8.1
      1  1.5.8.1  thorpej /*	$NetBSD: ralink_i2c.c,v 1.5.8.1 2021/08/04 02:44:16 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.5.8.1  thorpej __KERNEL_RCSID(0, "$NetBSD: ralink_i2c.c,v 1.5.8.1 2021/08/04 02:44:16 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/malloc.h>
     40      1.2     matt #include <sys/proc.h>
     41      1.2     matt #include <sys/systm.h>
     42      1.2     matt 
     43      1.2     matt #include <dev/i2c/i2cvar.h>
     44      1.2     matt 
     45      1.2     matt #include <mips/ralink/ralink_var.h>
     46      1.2     matt #include <mips/ralink/ralink_reg.h>
     47      1.2     matt 
     48      1.2     matt #if 0
     49      1.2     matt /*
     50      1.2     matt  * Defined for the Ralink 3050, w/320MHz CPU:  milage may vary.
     51      1.2     matt  * Set the I2C clock to 100K bps (low speed) transfer rate.
     52      1.2     matt  * Value is based upon the forms defined in the Ralink reference document
     53      1.2     matt  * for RT3050, page 53.  JCL.
     54      1.2     matt  */
     55      1.2     matt #define CLKDIV_VALUE 533
     56      1.2     matt #endif
     57      1.2     matt 
     58      1.2     matt /*
     59      1.2     matt  * Slow the I2C bus clock to 12.5 KHz to work around the misbehavior
     60      1.2     matt  * of the TI part.
     61      1.2     matt  */
     62      1.2     matt #define CLKDIV_VALUE 4264
     63      1.2     matt 
     64      1.2     matt #define i2c_busy_loop		(clkdiv*30)
     65      1.2     matt #define max_ee_busy_loop	(clkdiv*25)
     66      1.2     matt 
     67      1.2     matt 
     68      1.2     matt typedef struct ra_i2c_softc {
     69      1.2     matt 	device_t		sc_dev;
     70      1.2     matt 	struct i2c_controller	sc_i2c;
     71      1.2     matt 	bus_space_tag_t		sc_memt;
     72      1.2     matt 	bus_space_handle_t	sc_i2c_memh;
     73      1.2     matt 	bus_space_handle_t	sc_sy_memh;
     74      1.2     matt } ra_i2c_softc_t;
     75      1.2     matt 
     76      1.2     matt 
     77      1.3      chs static int  ra_i2c_match(device_t, cfdata_t, void *);
     78      1.3      chs static void ra_i2c_attach(device_t, device_t, void *);
     79      1.2     matt 
     80      1.2     matt /* RT3052 I2C functions */
     81      1.2     matt static int  i2c_write(ra_i2c_softc_t *, u_long, const u_char *, u_long);
     82      1.2     matt static int  i2c_read(ra_i2c_softc_t *, u_long, u_char *, u_long);
     83      1.2     matt #ifdef NOTYET
     84      1.2     matt static void i2c_write_stop(ra_i2c_softc_t *, u_long, u_char *);
     85      1.2     matt static void i2c_read_stop(ra_i2c_softc_t *, u_long, u_char *);
     86      1.2     matt #endif
     87      1.2     matt 
     88      1.2     matt /* i2c driver functions */
     89      1.2     matt int  ra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
     90      1.2     matt 	void *, size_t, int);
     91      1.2     matt void ra_i2c_reset(ra_i2c_softc_t *);
     92      1.2     matt 
     93      1.2     matt CFATTACH_DECL_NEW(ri2c, sizeof(struct ra_i2c_softc),
     94      1.2     matt 	ra_i2c_match, ra_i2c_attach, NULL, NULL);
     95      1.2     matt 
     96      1.2     matt unsigned int clkdiv = CLKDIV_VALUE;
     97      1.2     matt 
     98      1.2     matt static inline void
     99      1.2     matt ra_i2c_busy_wait(ra_i2c_softc_t *sc)
    100      1.2     matt {
    101      1.2     matt 	for (int i=0; i < i2c_busy_loop; i++) {
    102      1.2     matt 		uint32_t r;
    103      1.2     matt 		r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    104      1.2     matt 			RA_I2C_STATUS);
    105      1.2     matt 		if ((r & I2C_STATUS_BUSY) == 0)
    106      1.2     matt 			break;
    107      1.2     matt 	}
    108      1.2     matt }
    109      1.2     matt 
    110      1.2     matt int
    111      1.2     matt ra_i2c_match(device_t parent, cfdata_t cf, void *aux)
    112      1.2     matt {
    113      1.2     matt 	return 1;
    114      1.2     matt }
    115      1.2     matt 
    116      1.2     matt 
    117      1.2     matt void
    118      1.2     matt ra_i2c_attach(device_t parent, device_t self, void *aux)
    119      1.2     matt {
    120      1.2     matt 	ra_i2c_softc_t * const sc = device_private(self);
    121      1.2     matt 	const struct mainbus_attach_args *ma = aux;
    122      1.2     matt 	struct i2cbus_attach_args iba;
    123      1.2     matt 	uint32_t r;
    124      1.2     matt 	int error;
    125      1.2     matt 
    126      1.2     matt 	aprint_naive(": Ralink I2C controller\n");
    127      1.2     matt 	aprint_normal(": Ralink I2C controller\n");
    128      1.2     matt 
    129      1.2     matt 	/* save out bus space tag */
    130      1.2     matt 	sc->sc_memt = ma->ma_memt;
    131      1.2     matt 
    132      1.2     matt 	/* Map Sysctl registers */
    133      1.2     matt 	if ((error = bus_space_map(ma->ma_memt, RA_SYSCTL_BASE, 0x10000,
    134      1.2     matt 	    0, &sc->sc_sy_memh)) != 0) {
    135      1.2     matt 		aprint_error_dev(self, "unable to map Sysctl registers, "
    136      1.2     matt 			"error=%d\n", error);
    137      1.2     matt 		return;
    138      1.2     matt 	}
    139      1.2     matt 
    140      1.2     matt 	/* map the I2C registers */
    141      1.2     matt 	if ((error = bus_space_map(sc->sc_memt, RA_I2C_BASE, 0x100,
    142      1.2     matt 	    0, &sc->sc_i2c_memh)) != 0) {
    143      1.2     matt 		aprint_error_dev(self, "unable to map registers, "
    144      1.2     matt 			"error=%d\n", error);
    145      1.2     matt 		bus_space_unmap(ma->ma_memt, sc->sc_sy_memh, 0x10000);
    146      1.2     matt 		return;
    147      1.2     matt 	}
    148      1.2     matt 
    149      1.2     matt 	/*  Enable I2C block */
    150      1.2     matt 	r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh,
    151      1.2     matt 		RA_SYSCTL_GPIOMODE);
    152      1.2     matt 	r &= ~GPIOMODE_I2C;
    153      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh,
    154      1.2     matt 		RA_SYSCTL_GPIOMODE, r);
    155      1.2     matt 
    156      1.4  thorpej 	iic_tag_init(&sc->sc_i2c);
    157      1.2     matt 	sc->sc_i2c.ic_cookie = sc;
    158      1.2     matt 	sc->sc_i2c.ic_exec = ra_i2c_exec;
    159      1.2     matt 
    160      1.2     matt 	memset(&iba, 0, sizeof(iba));
    161      1.2     matt 	iba.iba_tag = &sc->sc_i2c;
    162  1.5.8.1  thorpej 	config_found(self, &iba, iicbus_print, CFARGS_NONE);
    163      1.2     matt }
    164      1.2     matt 
    165      1.2     matt 
    166      1.2     matt 
    167      1.2     matt /*
    168      1.2     matt  * I2C API
    169      1.2     matt  */
    170      1.2     matt 
    171      1.2     matt int
    172      1.2     matt ra_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
    173      1.2     matt 	size_t cmdlen, void *buf, size_t len, int flags)
    174      1.2     matt {
    175      1.2     matt 	ra_i2c_softc_t * const sc = cookie;
    176      1.2     matt 
    177      1.2     matt 	/*
    178      1.2     matt 	 * Make sure we only pass a seven-bit device address,
    179      1.2     matt 	 * as per I2C standard.
    180      1.2     matt 	 */
    181      1.2     matt 	KASSERT(addr <= 127);
    182      1.2     matt 	if (addr > 127)
    183      1.2     matt 		return -1;
    184      1.2     matt 
    185      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    186      1.2     matt 		addr);
    187      1.2     matt 
    188      1.2     matt 	ra_i2c_reset(sc);
    189      1.2     matt 
    190      1.2     matt 	/*
    191      1.2     matt 	 * Process the requested operation.
    192      1.2     matt 	 * There are four I2C operations of interest:
    193      1.2     matt 	 *   - Write
    194      1.2     matt 	 *   - Write with stop
    195      1.2     matt 	 *   - Read
    196      1.2     matt 	 *   - Read with stop
    197      1.2     matt 	 * Because the I2C block on the Ralink part generates stop markers
    198      1.2     matt 	 * at approprite places, based upon the byte count, the read and write
    199      1.2     matt 	 * with stop operations will rarely be needed.  They are included here
    200      1.2     matt 	 * as placeholders, but haven't been implemented or tested.
    201      1.2     matt 	 */
    202      1.2     matt 	switch(op) {
    203      1.2     matt 	case  I2C_OP_WRITE:
    204      1.2     matt 		return i2c_write(sc, addr, cmdbuf, cmdlen);
    205      1.2     matt 		break;
    206      1.2     matt 	case I2C_OP_READ:
    207      1.2     matt 		return i2c_read(sc, addr, buf, len);
    208      1.2     matt 		break;
    209      1.2     matt #ifdef NOTYET
    210      1.2     matt 	case I2C_OP_WRITE_WITH_STOP:
    211      1.2     matt 		i2c_write_stop(sc, addr, buf);
    212      1.2     matt 		break;
    213      1.2     matt 	case I2C_OP_READ_WITH_STOP:
    214      1.2     matt 		i2c_read_stop(sc, addr, buf);
    215      1.2     matt 		break;
    216      1.2     matt #endif
    217      1.2     matt 	default:
    218      1.2     matt 		return -1;  /* Illegal operation, error return. */
    219      1.2     matt 	}
    220      1.2     matt 
    221      1.2     matt 	return 0;
    222      1.2     matt }
    223      1.2     matt 
    224      1.2     matt static int
    225      1.2     matt i2c_write(ra_i2c_softc_t *sc, u_long addr, const u_char *data,
    226      1.2     matt 	u_long nbytes)
    227      1.2     matt {
    228      1.2     matt 	uint32_t r;
    229      1.2     matt 	int i, j;
    230      1.2     matt 
    231      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    232      1.2     matt 		addr);
    233      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
    234      1.2     matt 		nbytes - 1);
    235      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
    236      1.2     matt 		I2C_OP_WRITE);
    237      1.2     matt 
    238      1.2     matt 	for (i=0; i < nbytes; i++) {
    239      1.2     matt 		for (j=0; j < max_ee_busy_loop; j++) {
    240      1.2     matt 			r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    241      1.2     matt 				RA_I2C_STATUS);
    242      1.2     matt 			if ((r & I2C_STATUS_SDOEMPTY) != 0) {
    243      1.2     matt 				bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh,
    244      1.2     matt 					RA_I2C_DATAOUT, data[i]);
    245      1.2     matt 				break;
    246      1.2     matt 			}
    247      1.2     matt 		}
    248      1.2     matt #if 0
    249      1.2     matt 		if ((r & I2C_STATUS_ACKERR) != 0) {
    250      1.2     matt 			aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
    251      1.2     matt 				__func__);
    252      1.2     matt 			return EAGAIN;
    253      1.2     matt 		}
    254      1.2     matt #endif
    255      1.2     matt 		if (j == max_ee_busy_loop) {
    256      1.2     matt 			aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
    257      1.2     matt 				__func__);
    258      1.2     matt 			return EAGAIN;
    259      1.2     matt 		}
    260      1.2     matt 	}
    261      1.2     matt 
    262      1.2     matt 	ra_i2c_busy_wait(sc);
    263      1.2     matt 
    264      1.2     matt 	return 0;
    265      1.2     matt }
    266      1.2     matt 
    267      1.2     matt 
    268      1.2     matt static int
    269      1.2     matt i2c_read(ra_i2c_softc_t *sc, u_long addr, u_char *data, u_long nbytes)
    270      1.2     matt {
    271      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_DEVADDR,
    272      1.2     matt 		addr);
    273      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_BYTECNT,
    274      1.2     matt 		nbytes - 1);
    275      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_STARTXFR,
    276      1.2     matt 		I2C_OP_READ);
    277      1.2     matt 
    278      1.2     matt 	for (u_int i = 0; i < nbytes; i++) {
    279      1.2     matt 		u_long j;
    280      1.2     matt 		uint32_t r;
    281      1.2     matt 
    282      1.2     matt 		for (j=0; j < max_ee_busy_loop; j++) {
    283      1.2     matt 			r = bus_space_read_4(sc->sc_memt, sc->sc_i2c_memh,
    284      1.2     matt 				RA_I2C_STATUS);
    285      1.2     matt 			if ((r & I2C_STATUS_DATARDY) != 0) {
    286      1.2     matt 				data[i] = bus_space_read_4(
    287      1.2     matt 						sc->sc_memt, sc->sc_i2c_memh,
    288      1.2     matt 						RA_I2C_DATAIN);
    289      1.2     matt 				break;
    290      1.2     matt 			}
    291      1.2     matt 		}
    292      1.2     matt #if 0
    293      1.2     matt 		if ((r & I2C_STATUS_ACKERR) != 0) {
    294      1.2     matt 			aprint_error_dev(sc->sc_dev, "ACK error in %s\n",
    295      1.2     matt 				__func__);
    296      1.2     matt 			return EAGAIN;
    297      1.2     matt 		}
    298      1.2     matt #endif
    299      1.2     matt 		if (j == max_ee_busy_loop) {
    300      1.2     matt 			aprint_error_dev(sc->sc_dev, "timeout error in %s\n",
    301      1.2     matt 				__func__);
    302      1.2     matt 			return EAGAIN;
    303      1.2     matt 		}
    304      1.2     matt 	}
    305      1.2     matt 
    306      1.2     matt 	ra_i2c_busy_wait(sc);
    307      1.2     matt 
    308      1.2     matt 	return 0;
    309      1.2     matt 
    310      1.2     matt }
    311      1.2     matt 
    312      1.2     matt 
    313      1.2     matt #ifdef NOTYET
    314      1.2     matt static void
    315      1.2     matt i2c_write_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
    316      1.2     matt {
    317      1.2     matt 	/* unimplemented */
    318      1.2     matt }
    319      1.2     matt 
    320      1.2     matt static void
    321      1.2     matt i2c_read_stop(ra_i2c_softc_t *sc, u_long address, u_char *data)
    322      1.2     matt {
    323      1.2     matt 	/* unimplemented */
    324      1.2     matt }
    325      1.2     matt #endif
    326      1.2     matt 
    327      1.2     matt void
    328      1.2     matt ra_i2c_reset(ra_i2c_softc_t *sc)
    329      1.2     matt {
    330      1.2     matt 	uint32_t r;
    331      1.2     matt 
    332      1.2     matt 	/* reset i2c block */
    333      1.2     matt 	r = bus_space_read_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST);
    334      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST,
    335      1.2     matt 		r | RST_I2C);
    336      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_sy_memh, RA_SYSCTL_RST, r);
    337      1.2     matt 
    338      1.2     matt 	r = I2C_CONFIG_ADDRLEN(I2C_CONFIG_ADDRLEN_8) |
    339      1.2     matt 	    I2C_CONFIG_DEVADLEN(I2C_CONFIG_DEVADLEN_7) |
    340      1.2     matt 	    I2C_CONFIG_ADDRDIS;
    341      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CONFIG, r);
    342      1.2     matt 
    343      1.2     matt 	/*
    344      1.2     matt 	 * Set the I2C clock divider.  Appears to be set to 200,000,
    345      1.2     matt 	 * which is strange, as I2C is 100K/400K/3.?M bps.
    346      1.2     matt 	 */
    347      1.2     matt 	bus_space_write_4(sc->sc_memt, sc->sc_i2c_memh, RA_I2C_CLKDIV,
    348      1.2     matt 		clkdiv);
    349      1.2     matt }
    350