Home | History | Annotate | Line # | Download | only in samsung
exynos_i2c.c revision 1.9
      1  1.9    marty /*	$NetBSD: exynos_i2c.c,v 1.9 2015/12/30 04:30:27 marty Exp $ */
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.9    marty  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  reinoud  * All rights reserved.
      6  1.1  reinoud  *
      7  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1  reinoud  * modification, are permitted provided that the following conditions
      9  1.1  reinoud  * are met:
     10  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15  1.1  reinoud  *
     16  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  reinoud  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  reinoud  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  reinoud  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  reinoud  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  reinoud  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  reinoud  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  reinoud  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  reinoud  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  reinoud  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  reinoud  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  reinoud  *
     28  1.1  reinoud  */
     29  1.1  reinoud 
     30  1.1  reinoud #include "opt_exynos.h"
     31  1.1  reinoud #include "opt_arm_debug.h"
     32  1.1  reinoud 
     33  1.1  reinoud #include <sys/cdefs.h>
     34  1.9    marty __KERNEL_RCSID(0, "$NetBSD: exynos_i2c.c,v 1.9 2015/12/30 04:30:27 marty Exp $");
     35  1.1  reinoud 
     36  1.1  reinoud #include <sys/param.h>
     37  1.1  reinoud #include <sys/bus.h>
     38  1.1  reinoud #include <sys/device.h>
     39  1.1  reinoud #include <sys/intr.h>
     40  1.1  reinoud #include <sys/systm.h>
     41  1.9    marty #include <sys/kernel.h>
     42  1.1  reinoud #include <sys/kmem.h>
     43  1.1  reinoud 
     44  1.1  reinoud #include <arm/samsung/exynos_reg.h>
     45  1.9    marty #include <arm/samsung/exynos_var.h>
     46  1.1  reinoud #include <arm/samsung/exynos_intr.h>
     47  1.1  reinoud 
     48  1.1  reinoud #include <sys/gpio.h>
     49  1.1  reinoud #include <dev/gpio/gpiovar.h>
     50  1.1  reinoud 
     51  1.1  reinoud #include <dev/i2c/i2cvar.h>
     52  1.1  reinoud #include <dev/i2c/i2c_bitbang.h>
     53  1.1  reinoud 
     54  1.5    marty #include <dev/fdt/fdtvar.h>
     55  1.1  reinoud 
     56  1.5    marty struct exynos_i2c_softc {
     57  1.5    marty 	device_t		sc_dev;
     58  1.5    marty 	bus_space_tag_t		sc_bst;
     59  1.5    marty 	bus_space_handle_t	sc_bsh;
     60  1.5    marty 	void *			sc_ih;
     61  1.9    marty 	struct clk *		sc_clk;
     62  1.5    marty 
     63  1.9    marty 	struct fdtbus_pinctrl_pin  *sc_sda;
     64  1.9    marty 	struct fdtbus_pinctrl_pin  *sc_scl;
     65  1.5    marty 	bool			sc_sda_is_output;
     66  1.9    marty 
     67  1.5    marty 	struct i2c_controller 	sc_ic;
     68  1.5    marty 	kmutex_t		sc_lock;
     69  1.5    marty 	kcondvar_t		sc_cv;
     70  1.5    marty 	device_t		sc_i2cdev;
     71  1.1  reinoud };
     72  1.1  reinoud 
     73  1.5    marty static int	exynos_i2c_intr(void *);
     74  1.1  reinoud 
     75  1.5    marty static int	exynos_i2c_acquire_bus(void *, int);
     76  1.5    marty static void	exynos_i2c_release_bus(void *, int);
     77  1.1  reinoud 
     78  1.5    marty static int	exynos_i2c_send_start(void *, int);
     79  1.5    marty static int	exynos_i2c_send_stop(void *, int);
     80  1.5    marty static int	exynos_i2c_initiate_xfer(void *, i2c_addr_t, int);
     81  1.5    marty static int	exynos_i2c_read_byte(void *, uint8_t *, int);
     82  1.5    marty static int	exynos_i2c_write_byte(void *, uint8_t , int);
     83  1.1  reinoud 
     84  1.9    marty static int	exynos_i2c_wait(struct exynos_i2c_softc *, int);
     85  1.9    marty 
     86  1.1  reinoud 
     87  1.5    marty static int exynos_i2c_match(device_t, cfdata_t, void *);
     88  1.5    marty static void exynos_i2c_attach(device_t, device_t, void *);
     89  1.1  reinoud 
     90  1.9    marty static i2c_tag_t exynos_i2c_get_tag(device_t);
     91  1.9    marty 
     92  1.9    marty struct fdtbus_i2c_controller_func exynos_i2c_funcs = {
     93  1.9    marty 	.get_tag = exynos_i2c_get_tag
     94  1.9    marty };
     95  1.9    marty 
     96  1.5    marty CFATTACH_DECL_NEW(exynos_i2c, sizeof(struct exynos_i2c_softc),
     97  1.5    marty     exynos_i2c_match, exynos_i2c_attach, NULL, NULL);
     98  1.1  reinoud 
     99  1.8    marty #define I2C_WRITE(sc, reg, val) \
    100  1.9    marty     bus_space_write_1((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    101  1.8    marty #define I2C_READ(sc, reg) \
    102  1.9    marty     bus_space_read_1((sc)->sc_bst, (sc)->sc_bsh, (reg))
    103  1.8    marty 
    104  1.9    marty #define IICCON  0x00
    105  1.9    marty #define IICSTAT 0x04
    106  1.9    marty #define IICADD  0x08
    107  1.9    marty #define IICDS   0x0C
    108  1.9    marty 
    109  1.9    marty #define ACKENABLE  (1<<7)
    110  1.9    marty #define TXPRESCALE (1<<6)
    111  1.9    marty #define INTENABLE  (1<<5)
    112  1.9    marty #define IRQPEND    (1<<4)
    113  1.9    marty #define PRESCALE   (0x0f)
    114  1.9    marty 
    115  1.9    marty #define MODESELECT  (3<<6)
    116  1.9    marty #define BUSYSTART   (1<<5)
    117  1.9    marty #define BUSENABLE   (1<<4)
    118  1.9    marty #define ARBITRATION (1<<3)
    119  1.9    marty #define SLAVESTATUS (1<<2)
    120  1.9    marty #define ZEROSTATUS  (1<<1)
    121  1.9    marty #define LASTBIT     (1<<0)
    122  1.9    marty 
    123  1.9    marty #define READBIT     (1<<7)
    124  1.8    marty 
    125  1.1  reinoud static int
    126  1.5    marty exynos_i2c_match(device_t self, cfdata_t cf, void *aux)
    127  1.1  reinoud {
    128  1.5    marty 	const char * const compatible[] = { "samsung,s3c2440-i2c", NULL };
    129  1.5    marty 	struct fdt_attach_args * const faa = aux;
    130  1.1  reinoud 
    131  1.5    marty 	return of_match_compatible(faa->faa_phandle, compatible);
    132  1.1  reinoud }
    133  1.1  reinoud 
    134  1.1  reinoud static void
    135  1.5    marty exynos_i2c_attach(device_t parent, device_t self, void *aux)
    136  1.1  reinoud {
    137  1.5    marty         struct exynos_i2c_softc * const sc =  device_private(self);
    138  1.5    marty 	struct fdt_attach_args * const faa = aux;
    139  1.5    marty 	const int phandle = faa->faa_phandle;
    140  1.1  reinoud 	struct i2cbus_attach_args iba;
    141  1.5    marty 
    142  1.5    marty 	char intrstr[128];
    143  1.5    marty 	bus_addr_t addr;
    144  1.5    marty 	bus_size_t size;
    145  1.5    marty 	int error;
    146  1.5    marty 
    147  1.8    marty 	char result[64];
    148  1.8    marty 	int i2c_handle;
    149  1.8    marty 	int len;
    150  1.8    marty 	int handle;
    151  1.9    marty 	int func, pud, drv;
    152  1.8    marty 
    153  1.5    marty 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    154  1.5    marty 		aprint_error(": couldn't get registers\n");
    155  1.5    marty 		return;
    156  1.5    marty 	}
    157  1.5    marty 
    158  1.1  reinoud 
    159  1.1  reinoud 	sc->sc_dev  = self;
    160  1.5    marty 	sc->sc_bst = faa->faa_bst;
    161  1.5    marty 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    162  1.5    marty 	if (error) {
    163  1.5    marty 		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr,
    164  1.5    marty 			     error);
    165  1.1  reinoud 		return;
    166  1.1  reinoud 	}
    167  1.1  reinoud 
    168  1.5    marty 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    169  1.5    marty 	cv_init(&sc->sc_cv, device_xname(self));
    170  1.8    marty 	aprint_normal(" @ 0x%08x\n", (uint)addr);
    171  1.1  reinoud 
    172  1.5    marty 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    173  1.5    marty 		aprint_error_dev(self, "failed to decode interrupt\n");
    174  1.5    marty 		return;
    175  1.1  reinoud 	}
    176  1.5    marty 
    177  1.5    marty 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM,
    178  1.5    marty 	    FDT_INTR_MPSAFE, exynos_i2c_intr, sc);
    179  1.5    marty 	if (sc->sc_ih == NULL) {
    180  1.5    marty 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    181  1.5    marty 		    intrstr);
    182  1.5    marty 		return;
    183  1.5    marty 	}
    184  1.5    marty 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    185  1.5    marty 
    186  1.8    marty 	len = OF_getprop(phandle, "pinctrl-0", (char *)&handle,
    187  1.8    marty 			 sizeof(handle));
    188  1.8    marty 	if (len != sizeof(int)) {
    189  1.8    marty 		aprint_error_dev(self, "couldn't get pinctrl-0.\n");
    190  1.8    marty 		return;
    191  1.8    marty 	}
    192  1.8    marty 
    193  1.8    marty 	i2c_handle = fdtbus_get_phandle_from_native(be32toh(handle));
    194  1.8    marty 	len = OF_getprop(i2c_handle, "samsung,pins", result, sizeof(result));
    195  1.8    marty 	if (len <= 0) {
    196  1.8    marty 		aprint_error_dev(self, "couldn't get pins.\n");
    197  1.8    marty 		return;
    198  1.8    marty 	}
    199  1.8    marty 
    200  1.8    marty 	len = OF_getprop(i2c_handle, "samsung,pin-function",
    201  1.8    marty 			 &handle, sizeof(handle));
    202  1.8    marty 	if (len <= 0) {
    203  1.8    marty 		aprint_error_dev(self, "couldn't get pin-function.\n");
    204  1.8    marty 		return;
    205  1.8    marty 	} else
    206  1.8    marty 		func = be32toh(handle);
    207  1.8    marty 
    208  1.9    marty 	sc->sc_sda = fdtbus_pinctrl_acquire(phandle, &result[0]);
    209  1.9    marty 	if (sc->sc_sda == NULL) {
    210  1.9    marty 		printf("could not acquire sda gpio %s\n", &result[0]);
    211  1.9    marty 		return;
    212  1.9    marty 	}
    213  1.9    marty 
    214  1.9    marty 	sc->sc_scl = fdtbus_pinctrl_acquire(phandle, &result[7]);
    215  1.9    marty 	if (sc->sc_scl == NULL) {
    216  1.9    marty 		printf("could not acquire scl gpio %s\n", &result[7]);
    217  1.9    marty 		return;
    218  1.9    marty 	}
    219  1.8    marty 
    220  1.8    marty 	len = OF_getprop(i2c_handle, "samsung,pin-pud", &handle,
    221  1.8    marty 			 sizeof(&handle));
    222  1.8    marty 	if (len <= 0) {
    223  1.8    marty 		aprint_error_dev(self, "couldn't get pin-pud.\n");
    224  1.8    marty 		return;
    225  1.8    marty 	} else
    226  1.8    marty 		pud = be32toh(handle);
    227  1.8    marty 
    228  1.8    marty 	len = OF_getprop(i2c_handle, "samsung,pin-drv", &handle,
    229  1.8    marty 			 sizeof(&handle));
    230  1.8    marty 	if (len <= 0) {
    231  1.8    marty 		aprint_error_dev(self, "couldn't get pin-drv.\n");
    232  1.8    marty 		return;
    233  1.8    marty 	} else
    234  1.8    marty 		drv = be32toh(handle);
    235  1.8    marty 
    236  1.9    marty 	struct exynos_gpio_pin_cfg cfg;
    237  1.9    marty 	cfg.cfg = func;
    238  1.9    marty 	cfg.pud = pud;
    239  1.9    marty 	cfg.drv = drv;
    240  1.9    marty 	cfg.conpwd = 0;
    241  1.9    marty 	cfg.pudpwd = 0;
    242  1.9    marty 
    243  1.9    marty 	fdtbus_pinctrl_set_cfg(sc->sc_scl, &cfg);
    244  1.9    marty 	fdtbus_pinctrl_set_cfg(sc->sc_sda, &cfg);
    245  1.9    marty 
    246  1.9    marty 	sc->sc_ic.ic_cookie = sc;
    247  1.9    marty 	sc->sc_ic.ic_acquire_bus = exynos_i2c_acquire_bus;
    248  1.9    marty 	sc->sc_ic.ic_release_bus = exynos_i2c_release_bus;
    249  1.9    marty 	sc->sc_ic.ic_send_start  = exynos_i2c_send_start;
    250  1.9    marty 	sc->sc_ic.ic_send_stop   = exynos_i2c_send_stop;
    251  1.9    marty 	sc->sc_ic.ic_initiate_xfer = exynos_i2c_initiate_xfer;
    252  1.9    marty 	sc->sc_ic.ic_read_byte   = exynos_i2c_read_byte;
    253  1.9    marty 	sc->sc_ic.ic_write_byte  = exynos_i2c_write_byte;
    254  1.5    marty 
    255  1.5    marty 	sc->sc_i2cdev = config_found_ia(self, "i2cbus", &iba, iicbus_print);
    256  1.5    marty 
    257  1.1  reinoud }
    258  1.1  reinoud 
    259  1.9    marty static i2c_tag_t
    260  1.9    marty exynos_i2c_get_tag(device_t dev)
    261  1.1  reinoud {
    262  1.9    marty 	struct exynos_i2c_softc * const sc = device_private(dev);
    263  1.1  reinoud 
    264  1.9    marty 	return &sc->sc_ic;
    265  1.1  reinoud }
    266  1.1  reinoud 
    267  1.5    marty static int
    268  1.5    marty exynos_i2c_intr(void *priv)
    269  1.5    marty {
    270  1.5    marty 	struct exynos_i2c_softc * const sc = priv;
    271  1.5    marty 
    272  1.9    marty 	uint8_t istatus = I2C_READ(sc, IICCON);
    273  1.8    marty 	if (!(istatus & IRQPEND))
    274  1.8    marty 		return 0;
    275  1.8    marty 	istatus &= ~IRQPEND;
    276  1.9    marty 	I2C_WRITE(sc, IICCON, istatus);
    277  1.5    marty 
    278  1.5    marty 	mutex_enter(&sc->sc_lock);
    279  1.5    marty 	cv_broadcast(&sc->sc_cv);
    280  1.5    marty 	mutex_exit(&sc->sc_lock);
    281  1.5    marty 
    282  1.5    marty 	return 1;
    283  1.5    marty }
    284  1.1  reinoud 
    285  1.1  reinoud static int
    286  1.5    marty exynos_i2c_acquire_bus(void *cookie, int flags)
    287  1.1  reinoud {
    288  1.5    marty 	struct exynos_i2c_softc *i2c_sc = cookie;
    289  1.1  reinoud 
    290  1.5    marty 	mutex_enter(&i2c_sc->sc_lock);
    291  1.1  reinoud 	return 0;
    292  1.1  reinoud }
    293  1.1  reinoud 
    294  1.1  reinoud static void
    295  1.5    marty exynos_i2c_release_bus(void *cookie, int flags)
    296  1.1  reinoud {
    297  1.5    marty 	struct exynos_i2c_softc *i2c_sc = cookie;
    298  1.1  reinoud 
    299  1.5    marty 	mutex_exit(&i2c_sc->sc_lock);
    300  1.1  reinoud }
    301  1.1  reinoud 
    302  1.1  reinoud static int
    303  1.9    marty exynos_i2c_wait(struct exynos_i2c_softc *sc, int flags)
    304  1.9    marty {
    305  1.9    marty 	int error, retry;
    306  1.9    marty 	uint8_t stat = 0;
    307  1.9    marty 
    308  1.9    marty 	retry = (flags & I2C_F_POLL) ? 100000 : 100;
    309  1.9    marty 
    310  1.9    marty 	while (--retry > 0) {
    311  1.9    marty 		if ((flags & I2C_F_POLL) == 0) {
    312  1.9    marty 			error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
    313  1.9    marty 			    max(mstohz(10), 1));
    314  1.9    marty 			if (error) {
    315  1.9    marty 				return error;
    316  1.9    marty 			}
    317  1.9    marty 		}
    318  1.9    marty 		stat = I2C_READ(sc, IICSTAT);
    319  1.9    marty 		if (!(stat & BUSYSTART)) {
    320  1.9    marty 			break;
    321  1.9    marty 		}
    322  1.9    marty 		if (flags & I2C_F_POLL) {
    323  1.9    marty 			delay(10);
    324  1.9    marty 		}
    325  1.9    marty 	}
    326  1.9    marty 	if (retry == 0) {
    327  1.9    marty 		stat = I2C_READ(sc, IICSTAT);
    328  1.9    marty 		device_printf(sc->sc_dev, "timed out, status = %#x\n", stat);
    329  1.9    marty 		return ETIMEDOUT;
    330  1.9    marty 	}
    331  1.9    marty 
    332  1.9    marty 	return 0;
    333  1.9    marty }
    334  1.9    marty 
    335  1.9    marty 
    336  1.9    marty static int
    337  1.5    marty exynos_i2c_send_start(void *cookie, int flags)
    338  1.1  reinoud {
    339  1.9    marty 	struct exynos_i2c_softc *sc = cookie;
    340  1.9    marty 	I2C_WRITE(sc, IICSTAT, 0xF0);
    341  1.9    marty 	return 0;
    342  1.1  reinoud }
    343  1.1  reinoud 
    344  1.3    skrll static int
    345  1.5    marty exynos_i2c_send_stop(void *cookie, int flags)
    346  1.1  reinoud {
    347  1.9    marty 	struct exynos_i2c_softc *sc = cookie;
    348  1.9    marty 	I2C_WRITE(sc, IICSTAT, 0xD0);
    349  1.9    marty 	return 0;
    350  1.1  reinoud }
    351  1.1  reinoud 
    352  1.3    skrll static int
    353  1.5    marty exynos_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    354  1.1  reinoud {
    355  1.9    marty 	struct exynos_i2c_softc *sc = cookie;
    356  1.9    marty 	uint8_t byte = addr & 0x7f;
    357  1.9    marty 	if (flags & I2C_F_READ)
    358  1.9    marty 		byte |= READBIT;
    359  1.9    marty 	else
    360  1.9    marty 		byte &= ~READBIT;
    361  1.9    marty 	I2C_WRITE(sc, IICADD, addr);
    362  1.9    marty 	exynos_i2c_send_start(cookie, flags);
    363  1.9    marty 	exynos_i2c_write_byte(cookie, byte, flags);
    364  1.9    marty 	return exynos_i2c_wait(cookie, flags);
    365  1.1  reinoud }
    366  1.1  reinoud 
    367  1.1  reinoud static int
    368  1.5    marty exynos_i2c_read_byte(void *cookie, uint8_t *bytep, int flags)
    369  1.1  reinoud {
    370  1.9    marty 	struct exynos_i2c_softc *sc = cookie;
    371  1.9    marty 	int error = exynos_i2c_wait(sc, flags);
    372  1.9    marty 	if (error)
    373  1.9    marty 		return error;
    374  1.9    marty 	*bytep = I2C_READ(sc, IICDS) & 0xff;
    375  1.9    marty 	if (flags & I2C_F_STOP)
    376  1.9    marty 		exynos_i2c_send_stop(cookie, flags);
    377  1.9    marty 	return 0;
    378  1.1  reinoud }
    379  1.1  reinoud 
    380  1.3    skrll static int
    381  1.5    marty exynos_i2c_write_byte(void *cookie, uint8_t byte, int flags)
    382  1.1  reinoud {
    383  1.9    marty 	struct exynos_i2c_softc *sc = cookie;
    384  1.9    marty 	int error = exynos_i2c_wait(sc, flags);
    385  1.9    marty 	if (error)
    386  1.9    marty 		return error;
    387  1.9    marty 	I2C_WRITE(sc, IICDS, byte);
    388  1.9    marty 	return 0;
    389  1.1  reinoud }
    390