The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/arm/mv/twsi.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
    3  * All rights reserved.
    4  *
    5  * Developed by Semihalf.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of MARVELL nor the names of contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * Driver for the TWSI (aka I2C, aka IIC) bus controller found on Marvell
   34  * SoCs. Supports master operation only, and works in polling mode.
   35  *
   36  * Calls to DELAY() are needed per Application Note AN-179 "TWSI Software
   37  * Guidelines for Discovery(TM), Horizon (TM) and Feroceon(TM) Devices".
   38  */
   39 
   40 #include <sys/cdefs.h>
   41 __FBSDID("$FreeBSD: stable/10/sys/arm/mv/twsi.c 266152 2014-05-15 16:11:06Z ian $");
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/bus.h>
   46 #include <sys/kernel.h>
   47 #include <sys/module.h>
   48 #include <sys/resource.h>
   49 
   50 #include <machine/_inttypes.h>
   51 #include <machine/bus.h>
   52 #include <machine/resource.h>
   53 
   54 #include <sys/rman.h>
   55 
   56 #include <sys/lock.h>
   57 #include <sys/mutex.h>
   58 
   59 #include <dev/iicbus/iiconf.h>
   60 #include <dev/iicbus/iicbus.h>
   61 #include <dev/fdt/fdt_common.h>
   62 #include <dev/ofw/ofw_bus.h>
   63 #include <dev/ofw/ofw_bus_subr.h>
   64 
   65 #include <arm/mv/mvreg.h>
   66 #include <arm/mv/mvvar.h>
   67 
   68 #include "iicbus_if.h"
   69 
   70 #define MV_TWSI_NAME            "twsi"
   71 #define IICBUS_DEVNAME          "iicbus"
   72 
   73 #define TWSI_SLAVE_ADDR         0x00
   74 #define TWSI_EXT_SLAVE_ADDR     0x10
   75 #define TWSI_DATA               0x04
   76 
   77 #define TWSI_CONTROL            0x08
   78 #define TWSI_CONTROL_ACK        (1 << 2)
   79 #define TWSI_CONTROL_IFLG       (1 << 3)
   80 #define TWSI_CONTROL_STOP       (1 << 4)
   81 #define TWSI_CONTROL_START      (1 << 5)
   82 #define TWSI_CONTROL_TWSIEN     (1 << 6)
   83 #define TWSI_CONTROL_INTEN      (1 << 7)
   84 
   85 #define TWSI_STATUS                     0x0c
   86 #define TWSI_STATUS_START               0x08
   87 #define TWSI_STATUS_RPTD_START          0x10
   88 #define TWSI_STATUS_ADDR_W_ACK          0x18
   89 #define TWSI_STATUS_DATA_WR_ACK         0x28
   90 #define TWSI_STATUS_ADDR_R_ACK          0x40
   91 #define TWSI_STATUS_DATA_RD_ACK         0x50
   92 #define TWSI_STATUS_DATA_RD_NOACK       0x58
   93 
   94 #define TWSI_BAUD_RATE          0x0c
   95 #define TWSI_BAUD_RATE_PARAM(M,N)       ((((M) << 3) | ((N) & 0x7)) & 0x7f)
   96 #define TWSI_BAUD_RATE_RAW(C,M,N)       ((C)/((10*(M+1))<<(N+1)))
   97 #define TWSI_BAUD_RATE_SLOW             50000   /* 50kHz */
   98 #define TWSI_BAUD_RATE_FAST             100000  /* 100kHz */
   99 
  100 #define TWSI_SOFT_RESET         0x1c
  101 
  102 #define TWSI_DEBUG
  103 #undef TWSI_DEBUG
  104 
  105 #ifdef  TWSI_DEBUG
  106 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
  107 #else
  108 #define debugf(fmt, args...)
  109 #endif
  110 
  111 struct mv_twsi_softc {
  112         device_t        dev;
  113         struct resource *res[1];        /* SYS_RES_MEMORY */
  114         struct mtx      mutex;
  115         device_t        iicbus;
  116 };
  117 
  118 static struct mv_twsi_baud_rate {
  119         uint32_t        raw;
  120         int             param;
  121         int             m;
  122         int             n;
  123 } baud_rate[IIC_FASTEST + 1];
  124 
  125 static int mv_twsi_probe(device_t);
  126 static int mv_twsi_attach(device_t);
  127 static int mv_twsi_detach(device_t);
  128 
  129 static int mv_twsi_reset(device_t dev, u_char speed, u_char addr,
  130     u_char *oldaddr);
  131 static int mv_twsi_repeated_start(device_t dev, u_char slave, int timeout);
  132 static int mv_twsi_start(device_t dev, u_char slave, int timeout);
  133 static int mv_twsi_stop(device_t dev);
  134 static int mv_twsi_read(device_t dev, char *buf, int len, int *read, int last,
  135     int delay);
  136 static int mv_twsi_write(device_t dev, const char *buf, int len, int *sent,
  137     int timeout);
  138 
  139 static struct resource_spec res_spec[] = {
  140         { SYS_RES_MEMORY, 0, RF_ACTIVE },
  141         { -1, 0 }
  142 };
  143 
  144 static device_method_t mv_twsi_methods[] = {
  145         /* device interface */
  146         DEVMETHOD(device_probe,         mv_twsi_probe),
  147         DEVMETHOD(device_attach,        mv_twsi_attach),
  148         DEVMETHOD(device_detach,        mv_twsi_detach),
  149 
  150         /* iicbus interface */
  151         DEVMETHOD(iicbus_callback, iicbus_null_callback),
  152         DEVMETHOD(iicbus_repeated_start, mv_twsi_repeated_start),
  153         DEVMETHOD(iicbus_start,         mv_twsi_start),
  154         DEVMETHOD(iicbus_stop,          mv_twsi_stop),
  155         DEVMETHOD(iicbus_write,         mv_twsi_write),
  156         DEVMETHOD(iicbus_read,          mv_twsi_read),
  157         DEVMETHOD(iicbus_reset,         mv_twsi_reset),
  158         DEVMETHOD(iicbus_transfer,      iicbus_transfer_gen),
  159         { 0, 0 }
  160 };
  161 
  162 static devclass_t mv_twsi_devclass;
  163 
  164 static driver_t mv_twsi_driver = {
  165         MV_TWSI_NAME,
  166         mv_twsi_methods,
  167         sizeof(struct mv_twsi_softc),
  168 };
  169 
  170 DRIVER_MODULE(twsi, simplebus, mv_twsi_driver, mv_twsi_devclass, 0, 0);
  171 DRIVER_MODULE(iicbus, twsi, iicbus_driver, iicbus_devclass, 0, 0);
  172 MODULE_DEPEND(twsi, iicbus, 1, 1, 1);
  173 
  174 static __inline uint32_t
  175 TWSI_READ(struct mv_twsi_softc *sc, bus_size_t off)
  176 {
  177 
  178         return (bus_read_4(sc->res[0], off));
  179 }
  180 
  181 static __inline void
  182 TWSI_WRITE(struct mv_twsi_softc *sc, bus_size_t off, uint32_t val)
  183 {
  184 
  185         bus_write_4(sc->res[0], off, val);
  186 }
  187 
  188 static __inline void
  189 twsi_control_clear(struct mv_twsi_softc *sc, uint32_t mask)
  190 {
  191         uint32_t val;
  192 
  193         val = TWSI_READ(sc, TWSI_CONTROL);
  194         val &= ~mask;
  195         TWSI_WRITE(sc, TWSI_CONTROL, val);
  196 }
  197 
  198 static __inline void
  199 twsi_control_set(struct mv_twsi_softc *sc, uint32_t mask)
  200 {
  201         uint32_t val;
  202 
  203         val = TWSI_READ(sc, TWSI_CONTROL);
  204         val |= mask;
  205         TWSI_WRITE(sc, TWSI_CONTROL, val);
  206 }
  207 
  208 static __inline void
  209 twsi_clear_iflg(struct mv_twsi_softc *sc)
  210 {
  211 
  212         DELAY(1000);
  213         twsi_control_clear(sc, TWSI_CONTROL_IFLG);
  214         DELAY(1000);
  215 }
  216 
  217 
  218 /*
  219  * timeout given in us
  220  * returns
  221  *   0 on sucessfull mask change
  222  *   non-zero on timeout
  223  */
  224 static int
  225 twsi_poll_ctrl(struct mv_twsi_softc *sc, int timeout, uint32_t mask)
  226 {
  227 
  228         timeout /= 10;
  229         while (!(TWSI_READ(sc, TWSI_CONTROL) & mask)) {
  230                 DELAY(10);
  231                 if (--timeout < 0)
  232                         return (timeout);
  233         }
  234         return (0);
  235 }
  236 
  237 
  238 /*
  239  * 'timeout' is given in us. Note also that timeout handling is not exact --
  240  * twsi_locked_start() total wait can be more than 2 x timeout
  241  * (twsi_poll_ctrl() is called twice). 'mask' can be either TWSI_STATUS_START
  242  * or TWSI_STATUS_RPTD_START
  243  */
  244 static int
  245 twsi_locked_start(device_t dev, struct mv_twsi_softc *sc, int32_t mask,
  246     u_char slave, int timeout)
  247 {
  248         int read_access, iflg_set = 0;
  249         uint32_t status;
  250 
  251         mtx_assert(&sc->mutex, MA_OWNED);
  252 
  253         if (mask == TWSI_STATUS_RPTD_START)
  254                 /* read IFLG to know if it should be cleared later; from NBSD */
  255                 iflg_set = TWSI_READ(sc, TWSI_CONTROL) & TWSI_CONTROL_IFLG;
  256 
  257         twsi_control_set(sc, TWSI_CONTROL_START);
  258 
  259         if (mask == TWSI_STATUS_RPTD_START && iflg_set) {
  260                 debugf("IFLG set, clearing\n");
  261                 twsi_clear_iflg(sc);
  262         }
  263 
  264         /*
  265          * Without this delay we timeout checking IFLG if the timeout is 0.
  266          * NBSD driver always waits here too.
  267          */
  268         DELAY(1000);
  269 
  270         if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
  271                 debugf("timeout sending %sSTART condition\n",
  272                     mask == TWSI_STATUS_START ? "" : "repeated ");
  273                 return (IIC_ETIMEOUT);
  274         }
  275 
  276         status = TWSI_READ(sc, TWSI_STATUS);
  277         if (status != mask) {
  278                 debugf("wrong status (%02x) after sending %sSTART condition\n",
  279                     status, mask == TWSI_STATUS_START ? "" : "repeated ");
  280                 return (IIC_ESTATUS);
  281         }
  282 
  283         TWSI_WRITE(sc, TWSI_DATA, slave);
  284         DELAY(1000);
  285         twsi_clear_iflg(sc);
  286 
  287         if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
  288                 debugf("timeout sending slave address\n");
  289                 return (IIC_ETIMEOUT);
  290         }
  291         
  292         read_access = (slave & 0x1) ? 1 : 0;
  293         status = TWSI_READ(sc, TWSI_STATUS);
  294         if (status != (read_access ?
  295             TWSI_STATUS_ADDR_R_ACK : TWSI_STATUS_ADDR_W_ACK)) {
  296                 debugf("no ACK (status: %02x) after sending slave address\n",
  297                     status);
  298                 return (IIC_ENOACK);
  299         }
  300 
  301         return (IIC_NOERR);
  302 }
  303 
  304 static int
  305 mv_twsi_probe(device_t dev)
  306 {
  307 
  308         if (!ofw_bus_status_okay(dev))
  309                 return (ENXIO);
  310 
  311         if (!ofw_bus_is_compatible(dev, "mrvl,twsi"))
  312                 return (ENXIO);
  313 
  314         device_set_desc(dev, "Marvell Integrated I2C Bus Controller");
  315         return (BUS_PROBE_DEFAULT);
  316 }
  317 
  318 #define ABSSUB(a,b)     (((a) > (b)) ? (a) - (b) : (b) - (a))
  319 static void
  320 mv_twsi_cal_baud_rate(const uint32_t target, struct mv_twsi_baud_rate *rate)
  321 {
  322         uint32_t clk, cur, diff, diff0;
  323         int m, n, m0, n0;
  324 
  325         /* Calculate baud rate. */
  326         m0 = n0 = 4;    /* Default values on reset */
  327         diff0 = 0xffffffff;
  328         clk = get_tclk();
  329 
  330         for (n = 0; n < 8; n++) {
  331                 for (m = 0; m < 16; m++) {
  332                         cur = TWSI_BAUD_RATE_RAW(clk,m,n);
  333                         diff = ABSSUB(target, cur);
  334                         if (diff < diff0) {
  335                                 m0 = m;
  336                                 n0 = n;
  337                                 diff0 = diff;
  338                         }
  339                 }
  340         }
  341         rate->raw = TWSI_BAUD_RATE_RAW(clk, m0, n0);
  342         rate->param = TWSI_BAUD_RATE_PARAM(m0, n0);
  343         rate->m = m0;
  344         rate->n = n0;
  345 }
  346 
  347 static int
  348 mv_twsi_attach(device_t dev)
  349 {
  350         struct mv_twsi_softc *sc;
  351         phandle_t child, iicbusnode;
  352         device_t childdev;
  353         struct iicbus_ivar *devi;
  354         char dname[32]; /* 32 is taken from struct u_device */
  355         uint32_t paddr;
  356         int len, error;
  357 
  358         sc = device_get_softc(dev);
  359         sc->dev = dev;
  360         bzero(baud_rate, sizeof(baud_rate));
  361 
  362         mtx_init(&sc->mutex, device_get_nameunit(dev), MV_TWSI_NAME, MTX_DEF);
  363 
  364         /* Allocate IO resources */
  365         if (bus_alloc_resources(dev, res_spec, sc->res)) {
  366                 device_printf(dev, "could not allocate resources\n");
  367                 mv_twsi_detach(dev);
  368                 return (ENXIO);
  369         }
  370 
  371         mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_SLOW, &baud_rate[IIC_SLOW]);
  372         mv_twsi_cal_baud_rate(TWSI_BAUD_RATE_FAST, &baud_rate[IIC_FAST]);
  373         if (bootverbose)
  374                 device_printf(dev, "calculated baud rates are:\n"
  375                     " %" PRIu32 " kHz (M=%d, N=%d) for slow,\n"
  376                     " %" PRIu32 " kHz (M=%d, N=%d) for fast.\n",
  377                     baud_rate[IIC_SLOW].raw / 1000,
  378                     baud_rate[IIC_SLOW].m,
  379                     baud_rate[IIC_SLOW].n,
  380                     baud_rate[IIC_FAST].raw / 1000,
  381                     baud_rate[IIC_FAST].m,
  382                     baud_rate[IIC_FAST].n);
  383 
  384         sc->iicbus = device_add_child(dev, IICBUS_DEVNAME, -1);
  385         if (sc->iicbus == NULL) {
  386                 device_printf(dev, "could not add iicbus child\n");
  387                 mv_twsi_detach(dev);
  388                 return (ENXIO);
  389         }
  390         /* Attach iicbus. */
  391         bus_generic_attach(dev);
  392 
  393         iicbusnode = 0;
  394         /* Find iicbus as the child devices in the device tree. */
  395         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
  396             child = OF_peer(child)) {
  397                 len = OF_getproplen(child, "model");
  398                 if (len <= 0 || len > sizeof(dname) - 1)
  399                         continue;
  400                 error = OF_getprop(child, "model", &dname, len);
  401                 dname[len + 1] = '\0';
  402                 if (error == -1)
  403                         continue;
  404                 len = strlen(dname);
  405                 if (len == strlen(IICBUS_DEVNAME) &&
  406                     strncasecmp(dname, IICBUS_DEVNAME, len) == 0) {
  407                         iicbusnode = child;
  408                         break; 
  409                 }
  410         }
  411         if (iicbusnode == 0)
  412                 goto attach_end;
  413 
  414         /* Attach child devices onto iicbus. */
  415         for (child = OF_child(iicbusnode); child != 0; child = OF_peer(child)) {
  416                 /* Get slave address. */
  417                 error = OF_getprop(child, "i2c-address", &paddr, sizeof(paddr));
  418                 if (error == -1)
  419                         error = OF_getprop(child, "reg", &paddr, sizeof(paddr));
  420                 if (error == -1)
  421                         continue;
  422 
  423                 /* Get device driver name. */
  424                 len = OF_getproplen(child, "model");
  425                 if (len <= 0 || len > sizeof(dname) - 1)
  426                         continue;
  427                 OF_getprop(child, "model", &dname, len);
  428                 dname[len + 1] = '\0';
  429 
  430                 if (bootverbose)
  431                         device_printf(dev, "adding a device %s at %d.\n",
  432                             dname, fdt32_to_cpu(paddr));
  433                 childdev = BUS_ADD_CHILD(sc->iicbus, 0, dname, -1);
  434                 devi = IICBUS_IVAR(childdev);
  435                 devi->addr = fdt32_to_cpu(paddr);
  436         }
  437 
  438 attach_end:
  439         bus_generic_attach(sc->iicbus);
  440 
  441         return (0);
  442 }
  443 
  444 static int
  445 mv_twsi_detach(device_t dev)
  446 {
  447         struct mv_twsi_softc *sc;
  448         int rv;
  449 
  450         sc = device_get_softc(dev);
  451 
  452         if ((rv = bus_generic_detach(dev)) != 0)
  453                 return (rv);
  454 
  455         if (sc->iicbus != NULL)
  456                 if ((rv = device_delete_child(dev, sc->iicbus)) != 0)
  457                         return (rv);
  458 
  459         bus_release_resources(dev, res_spec, sc->res);
  460 
  461         mtx_destroy(&sc->mutex);
  462         return (0);
  463 }
  464 
  465 /*
  466  * Only slave mode supported, disregard [old]addr
  467  */
  468 static int
  469 mv_twsi_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
  470 {
  471         struct mv_twsi_softc *sc;
  472         uint32_t param;
  473 
  474         sc = device_get_softc(dev);
  475 
  476         switch (speed) {
  477         case IIC_SLOW:
  478         case IIC_FAST:
  479                 param = baud_rate[speed].param;
  480                 break;
  481         case IIC_FASTEST:
  482         case IIC_UNKNOWN:
  483         default:
  484                 param = baud_rate[IIC_FAST].param;
  485                 break;
  486         }
  487 
  488         mtx_lock(&sc->mutex);
  489         TWSI_WRITE(sc, TWSI_SOFT_RESET, 0x0);
  490         DELAY(2000);
  491         TWSI_WRITE(sc, TWSI_BAUD_RATE, param);
  492         TWSI_WRITE(sc, TWSI_CONTROL, TWSI_CONTROL_TWSIEN | TWSI_CONTROL_ACK);
  493         DELAY(1000);
  494         mtx_unlock(&sc->mutex);
  495 
  496         return (0);
  497 }
  498 
  499 /*
  500  * timeout is given in us
  501  */
  502 static int
  503 mv_twsi_repeated_start(device_t dev, u_char slave, int timeout)
  504 {
  505         struct mv_twsi_softc *sc;
  506         int rv;
  507 
  508         sc = device_get_softc(dev);
  509 
  510         mtx_lock(&sc->mutex);
  511         rv = twsi_locked_start(dev, sc, TWSI_STATUS_RPTD_START, slave,
  512             timeout);
  513         mtx_unlock(&sc->mutex);
  514 
  515         if (rv) {
  516                 mv_twsi_stop(dev);
  517                 return (rv);
  518         } else
  519                 return (IIC_NOERR);
  520 }
  521 
  522 /*
  523  * timeout is given in us
  524  */
  525 static int
  526 mv_twsi_start(device_t dev, u_char slave, int timeout)
  527 {
  528         struct mv_twsi_softc *sc;
  529         int rv;
  530 
  531         sc = device_get_softc(dev);
  532 
  533         mtx_lock(&sc->mutex);
  534         rv = twsi_locked_start(dev, sc, TWSI_STATUS_START, slave, timeout);
  535         mtx_unlock(&sc->mutex);
  536 
  537         if (rv) {
  538                 mv_twsi_stop(dev);
  539                 return (rv);
  540         } else
  541                 return (IIC_NOERR);
  542 }
  543 
  544 static int
  545 mv_twsi_stop(device_t dev)
  546 {
  547         struct mv_twsi_softc *sc;
  548 
  549         sc = device_get_softc(dev);
  550 
  551         mtx_lock(&sc->mutex);
  552         twsi_control_set(sc, TWSI_CONTROL_STOP);
  553         DELAY(1000);
  554         twsi_clear_iflg(sc);
  555         mtx_unlock(&sc->mutex);
  556 
  557         return (IIC_NOERR);
  558 }
  559 
  560 static int
  561 mv_twsi_read(device_t dev, char *buf, int len, int *read, int last, int delay)
  562 {
  563         struct mv_twsi_softc *sc;
  564         uint32_t status;
  565         int last_byte, rv;
  566 
  567         sc = device_get_softc(dev);
  568 
  569         mtx_lock(&sc->mutex);
  570         *read = 0;
  571         while (*read < len) {
  572                 /*
  573                  * Check if we are reading last byte of the last buffer,
  574                  * do not send ACK then, per I2C specs
  575                  */
  576                 last_byte = ((*read == len - 1) && last) ? 1 : 0;
  577                 if (last_byte)
  578                         twsi_control_clear(sc, TWSI_CONTROL_ACK);
  579                 else
  580                         twsi_control_set(sc, TWSI_CONTROL_ACK);
  581 
  582                 DELAY (1000);
  583                 twsi_clear_iflg(sc);
  584 
  585                 if (twsi_poll_ctrl(sc, delay, TWSI_CONTROL_IFLG)) {
  586                         debugf("timeout reading data\n");
  587                         rv = IIC_ETIMEOUT;
  588                         goto out;
  589                 }
  590 
  591                 status = TWSI_READ(sc, TWSI_STATUS);
  592                 if (status != (last_byte ?
  593                     TWSI_STATUS_DATA_RD_NOACK : TWSI_STATUS_DATA_RD_ACK)) {
  594                         debugf("wrong status (%02x) while reading\n", status);
  595                         rv = IIC_ESTATUS;
  596                         goto out;
  597                 }
  598 
  599                 *buf++ = TWSI_READ(sc, TWSI_DATA);
  600                 (*read)++;
  601         }
  602         rv = IIC_NOERR;
  603 out:
  604         mtx_unlock(&sc->mutex);
  605         return (rv);
  606 }
  607 
  608 static int
  609 mv_twsi_write(device_t dev, const char *buf, int len, int *sent, int timeout)
  610 {
  611         struct mv_twsi_softc *sc;
  612         uint32_t status;
  613         int rv;
  614 
  615         sc = device_get_softc(dev);
  616 
  617         mtx_lock(&sc->mutex);
  618         *sent = 0;
  619         while (*sent < len) {
  620                 TWSI_WRITE(sc, TWSI_DATA, *buf++);
  621 
  622                 twsi_clear_iflg(sc);
  623                 if (twsi_poll_ctrl(sc, timeout, TWSI_CONTROL_IFLG)) {
  624                         debugf("timeout writing data\n");
  625                         rv = IIC_ETIMEOUT;
  626                         goto out;
  627                 }
  628 
  629                 status = TWSI_READ(sc, TWSI_STATUS);
  630                 if (status != TWSI_STATUS_DATA_WR_ACK) {
  631                         debugf("wrong status (%02x) while writing\n", status);
  632                         rv = IIC_ESTATUS;
  633                         goto out;
  634                 }
  635                 (*sent)++;
  636         }
  637         rv = IIC_NOERR;
  638 out:
  639         mtx_unlock(&sc->mutex);
  640         return (rv);
  641 }

Cache object: 8e694d853f1ad86ab374b5aa62863710


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.