Home | History | Annotate | Line # | Download | only in i2c
i2c_bitbang.c revision 1.1.18.4
      1  1.1.18.4     yamt /*	$NetBSD: i2c_bitbang.c,v 1.1.18.4 2007/12/07 17:29:48 yamt Exp $	*/
      2       1.1  thorpej 
      3       1.1  thorpej /*
      4       1.1  thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5       1.1  thorpej  * All rights reserved.
      6       1.1  thorpej  *
      7       1.1  thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8       1.1  thorpej  *
      9       1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10       1.1  thorpej  * modification, are permitted provided that the following conditions
     11       1.1  thorpej  * are met:
     12       1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13       1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14       1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16       1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17       1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18       1.1  thorpej  *    must display the following acknowledgement:
     19       1.1  thorpej  *      This product includes software developed for the NetBSD Project by
     20       1.1  thorpej  *      Wasabi Systems, Inc.
     21       1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22       1.1  thorpej  *    or promote products derived from this software without specific prior
     23       1.1  thorpej  *    written permission.
     24       1.1  thorpej  *
     25       1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26       1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27       1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28       1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29       1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30       1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31       1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32       1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33       1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34       1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35       1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36       1.1  thorpej  */
     37       1.1  thorpej 
     38       1.1  thorpej /*
     39       1.1  thorpej  * Common module for bit-bang'ing an I2C bus.
     40       1.1  thorpej  */
     41       1.1  thorpej 
     42       1.1  thorpej #include <sys/param.h>
     43       1.1  thorpej 
     44       1.1  thorpej #include <dev/i2c/i2cvar.h>
     45       1.1  thorpej #include <dev/i2c/i2c_bitbang.h>
     46       1.1  thorpej 
     47  1.1.18.1     yamt #define	SETBITS(x)	ops->ibo_set_bits(v, (x))
     48       1.1  thorpej #define	DIR(x)		ops->ibo_set_dir(v, (x))
     49       1.1  thorpej #define	READ		ops->ibo_read_bits(v)
     50       1.1  thorpej 
     51       1.1  thorpej #define	SDA		ops->ibo_bits[I2C_BIT_SDA]	/* i2c signal */
     52       1.1  thorpej #define	SCL		ops->ibo_bits[I2C_BIT_SCL]	/* i2c signal */
     53       1.1  thorpej #define	OUTPUT		ops->ibo_bits[I2C_BIT_OUTPUT]	/* SDA is output */
     54       1.1  thorpej #define	INPUT		ops->ibo_bits[I2C_BIT_INPUT]	/* SDA is input */
     55       1.1  thorpej 
     56  1.1.18.3     yamt #ifndef SCL_BAIL_COUNT
     57  1.1.18.3     yamt #define SCL_BAIL_COUNT 1000
     58  1.1.18.3     yamt #endif
     59  1.1.18.3     yamt 
     60  1.1.18.3     yamt static inline int i2c_wait_for_scl(void *, i2c_bitbang_ops_t);
     61  1.1.18.3     yamt 
     62  1.1.18.3     yamt static inline int
     63  1.1.18.3     yamt i2c_wait_for_scl(void *v, i2c_bitbang_ops_t ops)
     64  1.1.18.3     yamt {
     65  1.1.18.3     yamt 	int bail = 0;
     66  1.1.18.3     yamt 
     67  1.1.18.4     yamt 	DIR(INPUT);
     68  1.1.18.4     yamt 
     69  1.1.18.3     yamt 	while (((READ & SCL) == 0) && (bail < SCL_BAIL_COUNT)) {
     70  1.1.18.3     yamt 
     71  1.1.18.3     yamt 		delay(1);
     72  1.1.18.3     yamt 		bail++;
     73  1.1.18.3     yamt 	}
     74  1.1.18.3     yamt 	if (bail == SCL_BAIL_COUNT) {
     75  1.1.18.3     yamt 
     76  1.1.18.3     yamt 		i2c_bitbang_send_stop(v, 0, ops);
     77  1.1.18.3     yamt 		return EIO;
     78  1.1.18.3     yamt 	}
     79  1.1.18.3     yamt 	return 0;
     80  1.1.18.3     yamt }
     81  1.1.18.3     yamt 
     82       1.1  thorpej /*ARGSUSED*/
     83       1.1  thorpej int
     84       1.1  thorpej i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops)
     85       1.1  thorpej {
     86       1.1  thorpej 
     87       1.1  thorpej 	DIR(OUTPUT);
     88  1.1.18.1     yamt 	SETBITS(SDA | SCL);
     89       1.1  thorpej 	delay(5);		/* bus free time (4.7 uS) */
     90  1.1.18.1     yamt 	SETBITS(      SCL);
     91  1.1.18.4     yamt 
     92  1.1.18.3     yamt 	if (i2c_wait_for_scl(v, ops) != 0)
     93  1.1.18.3     yamt 		return EIO;
     94       1.1  thorpej 	delay(4);		/* start hold time (4.0 uS) */
     95  1.1.18.4     yamt 
     96  1.1.18.4     yamt 	DIR(OUTPUT);
     97  1.1.18.1     yamt 	SETBITS(        0);
     98       1.1  thorpej 	delay(5);		/* clock low time (4.7 uS) */
     99       1.1  thorpej 
    100       1.1  thorpej 	return (0);
    101       1.1  thorpej }
    102       1.1  thorpej 
    103       1.1  thorpej /*ARGSUSED*/
    104       1.1  thorpej int
    105       1.1  thorpej i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops)
    106       1.1  thorpej {
    107       1.1  thorpej 
    108       1.1  thorpej 	DIR(OUTPUT);
    109  1.1.18.1     yamt 	SETBITS(      SCL);
    110       1.1  thorpej 	delay(4);		/* stop setup time (4.0 uS) */
    111  1.1.18.1     yamt 	SETBITS(SDA | SCL);
    112       1.1  thorpej 
    113       1.1  thorpej 	return (0);
    114       1.1  thorpej }
    115       1.1  thorpej 
    116       1.1  thorpej int
    117       1.1  thorpej i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
    118       1.1  thorpej     i2c_bitbang_ops_t ops)
    119       1.1  thorpej {
    120       1.1  thorpej 
    121  1.1.18.2     yamt 	if (addr < 0x80) {
    122  1.1.18.2     yamt 		uint8_t i2caddr;
    123       1.1  thorpej 
    124  1.1.18.2     yamt 		/* disallow the 10-bit address prefix */
    125  1.1.18.2     yamt 		if ((addr & 0x78) == 0x78)
    126  1.1.18.2     yamt 			return EINVAL;
    127  1.1.18.2     yamt 		i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
    128  1.1.18.2     yamt 		(void) i2c_bitbang_send_start(v, flags, ops);
    129  1.1.18.2     yamt 
    130  1.1.18.2     yamt 		return (i2c_bitbang_write_byte(v, i2caddr,
    131  1.1.18.2     yamt 			    flags & ~I2C_F_STOP, ops));
    132  1.1.18.2     yamt 
    133  1.1.18.2     yamt 	} else if (addr < 0x400) {
    134  1.1.18.2     yamt 		uint16_t	i2caddr;
    135  1.1.18.2     yamt 		int		rv;
    136  1.1.18.2     yamt 
    137  1.1.18.2     yamt 		i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0) |
    138  1.1.18.2     yamt 		    0xf000;
    139  1.1.18.2     yamt 
    140  1.1.18.2     yamt 		(void) i2c_bitbang_send_start(v, flags, ops);
    141  1.1.18.2     yamt 		rv = i2c_bitbang_write_byte(v, i2caddr >> 8,
    142  1.1.18.2     yamt 		    flags & ~I2C_F_STOP, ops);
    143  1.1.18.2     yamt 		/* did a slave ack the 10-bit prefix? */
    144  1.1.18.2     yamt 		if (rv != 0)
    145  1.1.18.2     yamt 			return rv;
    146  1.1.18.2     yamt 
    147  1.1.18.2     yamt 		/* send the lower 7-bits (+ read/write mode) */
    148  1.1.18.2     yamt 		return (i2c_bitbang_write_byte(v, i2caddr & 0xff,
    149  1.1.18.2     yamt 			    flags & ~I2C_F_STOP, ops));
    150       1.1  thorpej 
    151  1.1.18.2     yamt 	} else
    152  1.1.18.2     yamt 		return EINVAL;
    153       1.1  thorpej }
    154       1.1  thorpej 
    155       1.1  thorpej int
    156       1.1  thorpej i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags,
    157       1.1  thorpej     i2c_bitbang_ops_t ops)
    158       1.1  thorpej {
    159       1.1  thorpej 	int i;
    160       1.1  thorpej 	uint8_t val = 0;
    161       1.1  thorpej 	uint32_t bit;
    162       1.1  thorpej 
    163  1.1.18.4     yamt 	DIR(OUTPUT);
    164  1.1.18.1     yamt 	SETBITS(SDA      );
    165       1.1  thorpej 
    166       1.1  thorpej 	for (i = 0; i < 8; i++) {
    167       1.1  thorpej 		val <<= 1;
    168  1.1.18.4     yamt 
    169  1.1.18.4     yamt 		DIR(OUTPUT);
    170  1.1.18.1     yamt 		SETBITS(SDA | SCL);
    171  1.1.18.4     yamt 
    172  1.1.18.3     yamt 		if (i2c_wait_for_scl(v, ops) != 0)
    173  1.1.18.3     yamt 			return EIO;
    174       1.1  thorpej 		delay(4);	/* clock high time (4.0 uS) */
    175  1.1.18.4     yamt 
    176  1.1.18.4     yamt 		DIR(INPUT);
    177       1.1  thorpej 		if (READ & SDA)
    178       1.1  thorpej 			val |= 1;
    179  1.1.18.4     yamt 
    180  1.1.18.4     yamt 		DIR(OUTPUT);
    181  1.1.18.1     yamt 		SETBITS(SDA      );
    182       1.1  thorpej 		delay(5);	/* clock low time (4.7 uS) */
    183       1.1  thorpej 	}
    184       1.1  thorpej 
    185       1.1  thorpej 	bit = (flags & I2C_F_LAST) ? SDA : 0;
    186  1.1.18.4     yamt 
    187       1.1  thorpej 	DIR(OUTPUT);
    188  1.1.18.1     yamt 	SETBITS(bit      );
    189       1.1  thorpej 	delay(1);	/* data setup time (250 nS) */
    190  1.1.18.1     yamt 	SETBITS(bit | SCL);
    191  1.1.18.4     yamt 
    192  1.1.18.3     yamt 	if (i2c_wait_for_scl(v, ops) != 0)
    193  1.1.18.3     yamt 		return EIO;
    194       1.1  thorpej 	delay(4);	/* clock high time (4.0 uS) */
    195  1.1.18.4     yamt 
    196  1.1.18.4     yamt 	DIR(OUTPUT);
    197  1.1.18.1     yamt 	SETBITS(bit      );
    198       1.1  thorpej 	delay(5);	/* clock low time (4.7 uS) */
    199       1.1  thorpej 
    200       1.1  thorpej 	DIR(INPUT);
    201  1.1.18.1     yamt 	SETBITS(SDA      );
    202       1.1  thorpej 	delay(5);
    203       1.1  thorpej 
    204       1.1  thorpej 	if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST))
    205       1.1  thorpej 		(void) i2c_bitbang_send_stop(v, flags, ops);
    206       1.1  thorpej 
    207       1.1  thorpej 	*valp = val;
    208       1.1  thorpej 	return (0);
    209       1.1  thorpej }
    210       1.1  thorpej 
    211       1.1  thorpej int
    212       1.1  thorpej i2c_bitbang_write_byte(void *v, uint8_t val, int flags,
    213       1.1  thorpej     i2c_bitbang_ops_t ops)
    214       1.1  thorpej {
    215       1.1  thorpej 	uint32_t bit;
    216       1.1  thorpej 	uint8_t mask;
    217       1.1  thorpej 	int error;
    218       1.1  thorpej 
    219       1.1  thorpej 	for (mask = 0x80; mask != 0; mask >>= 1) {
    220       1.1  thorpej 		bit = (val & mask) ? SDA : 0;
    221  1.1.18.4     yamt 
    222  1.1.18.4     yamt 		DIR(OUTPUT);
    223  1.1.18.1     yamt 		SETBITS(bit      );
    224       1.1  thorpej 		delay(1);	/* data setup time (250 nS) */
    225  1.1.18.1     yamt 		SETBITS(bit | SCL);
    226  1.1.18.4     yamt 
    227  1.1.18.3     yamt 		if (i2c_wait_for_scl(v, ops))
    228  1.1.18.3     yamt 			return EIO;
    229       1.1  thorpej 		delay(4);	/* clock high time (4.0 uS) */
    230  1.1.18.4     yamt 
    231  1.1.18.4     yamt 		DIR(OUTPUT);
    232  1.1.18.1     yamt 		SETBITS(bit      );
    233       1.1  thorpej 		delay(5);	/* clock low time (4.7 uS) */
    234       1.1  thorpej 	}
    235       1.1  thorpej 
    236  1.1.18.4     yamt 	DIR(OUTPUT);
    237  1.1.18.1     yamt 	SETBITS(SDA      );
    238       1.1  thorpej 	delay(5);
    239  1.1.18.1     yamt 	SETBITS(SDA | SCL);
    240  1.1.18.4     yamt 
    241  1.1.18.3     yamt 	if (i2c_wait_for_scl(v, ops) != 0)
    242  1.1.18.3     yamt 		return EIO;
    243       1.1  thorpej 	delay(4);
    244  1.1.18.4     yamt 
    245  1.1.18.4     yamt 	DIR(INPUT);
    246       1.1  thorpej 	error = (READ & SDA) ? EIO : 0;
    247  1.1.18.4     yamt 
    248  1.1.18.4     yamt 	DIR(OUTPUT);
    249  1.1.18.1     yamt 	SETBITS(SDA      );
    250       1.1  thorpej 	delay(5);
    251       1.1  thorpej 
    252       1.1  thorpej 	if (flags & I2C_F_STOP)
    253       1.1  thorpej 		(void) i2c_bitbang_send_stop(v, flags, ops);
    254       1.1  thorpej 
    255       1.1  thorpej 	return (error);
    256       1.1  thorpej }
    257