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/ti/ti_i2c.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2011
    3  *      Ben Gray <ben.r.gray@gmail.com>.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 /**
   29  * Driver for the I2C module on the TI SoC.
   30  *
   31  * This driver is heavily based on the TWI driver for the AT91 (at91_twi.c).
   32  *
   33  * CAUTION: The I2Ci registers are limited to 16 bit and 8 bit data accesses,
   34  * 32 bit data access is not allowed and can corrupt register content.
   35  *
   36  * This driver currently doesn't use DMA for the transfer, although I hope to
   37  * incorporate that sometime in the future.  The idea being that for transaction
   38  * larger than a certain size the DMA engine is used, for anything less the
   39  * normal interrupt/fifo driven option is used.
   40  *
   41  *
   42  * WARNING: This driver uses mtx_sleep and interrupts to perform transactions,
   43  * which means you can't do a transaction during startup before the interrupts
   44  * have been enabled.  Hint - the freebsd function config_intrhook_establish().
   45  */
   46 
   47 #include <sys/cdefs.h>
   48 __FBSDID("$FreeBSD: releng/10.1/sys/arm/ti/ti_i2c.c 270243 2014-08-20 19:37:05Z loos $");
   49 
   50 #include <sys/param.h>
   51 #include <sys/systm.h>
   52 #include <sys/bus.h>
   53 #include <sys/conf.h>
   54 #include <sys/kernel.h>
   55 #include <sys/lock.h>
   56 #include <sys/mbuf.h>
   57 #include <sys/malloc.h>
   58 #include <sys/module.h>
   59 #include <sys/mutex.h>
   60 #include <sys/rman.h>
   61 #include <machine/bus.h>
   62 
   63 #include <dev/fdt/fdt_common.h>
   64 #include <dev/ofw/openfirm.h>
   65 #include <dev/ofw/ofw_bus.h>
   66 #include <dev/ofw/ofw_bus_subr.h>
   67 
   68 #include <arm/ti/ti_prcm.h>
   69 #include <arm/ti/ti_i2c.h>
   70 
   71 #include <dev/iicbus/iiconf.h>
   72 #include <dev/iicbus/iicbus.h>
   73 
   74 #include "iicbus_if.h"
   75 
   76 /**
   77  *      I2C device driver context, a pointer to this is stored in the device
   78  *      driver structure.
   79  */
   80 struct ti_i2c_softc
   81 {
   82         device_t                sc_dev;
   83         uint32_t                device_id;
   84         struct resource*        sc_irq_res;
   85         struct resource*        sc_mem_res;
   86         device_t                sc_iicbus;
   87 
   88         void*                   sc_irq_h;
   89 
   90         struct mtx              sc_mtx;
   91 
   92         volatile uint16_t       sc_stat_flags;  /* contains the status flags last IRQ */
   93 
   94         uint16_t                sc_rev;
   95 };
   96 
   97 struct ti_i2c_clock_config
   98 {
   99         int speed;
  100         int bitrate;
  101         uint8_t psc;            /* Fast/Standard mode prescale divider */
  102         uint8_t scll;           /* Fast/Standard mode SCL low time */
  103         uint8_t sclh;           /* Fast/Standard mode SCL high time */
  104         uint8_t hsscll;         /* High Speed mode SCL low time */
  105         uint8_t hssclh;         /* High Speed mode SCL high time */
  106 };
  107 
  108 static struct ti_i2c_clock_config ti_i2c_clock_configs[] = {
  109 
  110 #if defined(SOC_OMAP4)
  111         { IIC_SLOW,      100000, 23,  13,  15, 0,  0},
  112         { IIC_FAST,      400000,  9,   5,   7, 0,  0},
  113         { IIC_FASTEST,  3310000,  1, 113, 115, 7, 10},
  114 #elif defined(SOC_TI_AM335X)
  115         { IIC_SLOW,      100000,  3,  53,  55, 0,  0},
  116         { IIC_FAST,      400000,  3,   8,  10, 0,  0},
  117         { IIC_FASTEST,   400000,  3,   8,  10, 0,  0}, /* This might be higher */
  118 #else
  119 #error "TI I2C driver is not supported on this SoC"
  120 #endif
  121         { -1, 0 }
  122 };
  123 
  124 
  125 #define TI_I2C_REV1  0x003C      /* OMAP3 */
  126 #define TI_I2C_REV2  0x000A      /* OMAP4 */
  127 
  128 /**
  129  *      Locking macros used throughout the driver
  130  */
  131 #define TI_I2C_LOCK(_sc)             mtx_lock(&(_sc)->sc_mtx)
  132 #define TI_I2C_UNLOCK(_sc)           mtx_unlock(&(_sc)->sc_mtx)
  133 #define TI_I2C_LOCK_INIT(_sc) \
  134         mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
  135                  "ti_i2c", MTX_DEF)
  136 #define TI_I2C_LOCK_DESTROY(_sc)      mtx_destroy(&_sc->sc_mtx);
  137 #define TI_I2C_ASSERT_LOCKED(_sc)     mtx_assert(&_sc->sc_mtx, MA_OWNED);
  138 #define TI_I2C_ASSERT_UNLOCKED(_sc)   mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
  139 
  140 #ifdef DEBUG
  141 #define ti_i2c_dbg(_sc, fmt, args...) \
  142     device_printf((_sc)->sc_dev, fmt, ##args)
  143 #else
  144 #define ti_i2c_dbg(_sc, fmt, args...)
  145 #endif
  146 
  147 static devclass_t ti_i2c_devclass;
  148 
  149 /* bus entry points */
  150 
  151 static int ti_i2c_probe(device_t dev);
  152 static int ti_i2c_attach(device_t dev);
  153 static int ti_i2c_detach(device_t dev);
  154 static void ti_i2c_intr(void *);
  155 
  156 /* OFW routine */
  157 static phandle_t ti_i2c_get_node(device_t bus, device_t dev);
  158 
  159 /* helper routines */
  160 static int ti_i2c_activate(device_t dev);
  161 static void ti_i2c_deactivate(device_t dev);
  162 
  163 /**
  164  *      ti_i2c_read_2 - reads a 16-bit value from one of the I2C registers
  165  *      @sc: I2C device context
  166  *      @off: the byte offset within the register bank to read from.
  167  *
  168  *
  169  *      LOCKING:
  170  *      No locking required
  171  *
  172  *      RETURNS:
  173  *      16-bit value read from the register.
  174  */
  175 static inline uint16_t
  176 ti_i2c_read_2(struct ti_i2c_softc *sc, bus_size_t off)
  177 {
  178         return bus_read_2(sc->sc_mem_res, off);
  179 }
  180 
  181 /**
  182  *      ti_i2c_write_2 - writes a 16-bit value to one of the I2C registers
  183  *      @sc: I2C device context
  184  *      @off: the byte offset within the register bank to read from.
  185  *      @val: the value to write into the register
  186  *
  187  *      LOCKING:
  188  *      No locking required
  189  *
  190  *      RETURNS:
  191  *      16-bit value read from the register.
  192  */
  193 static inline void
  194 ti_i2c_write_2(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)
  195 {
  196         bus_write_2(sc->sc_mem_res, off, val);
  197 }
  198 
  199 /**
  200  *      ti_i2c_read_reg - reads a 16-bit value from one of the I2C registers
  201  *          take into account revision-dependent register offset
  202  *      @sc: I2C device context
  203  *      @off: the byte offset within the register bank to read from.
  204  *
  205  *
  206  *      LOCKING:
  207  *      No locking required
  208  *
  209  *      RETURNS:
  210  *      16-bit value read from the register.
  211  */
  212 static inline uint16_t
  213 ti_i2c_read_reg(struct ti_i2c_softc *sc, bus_size_t off)
  214 {
  215         /* XXXOMAP3: FIXME add registers mapping here */
  216         return bus_read_2(sc->sc_mem_res, off);
  217 }
  218 
  219 /**
  220  *      ti_i2c_write_reg - writes a 16-bit value to one of the I2C registers
  221  *          take into account revision-dependent register offset
  222  *      @sc: I2C device context
  223  *      @off: the byte offset within the register bank to read from.
  224  *      @val: the value to write into the register
  225  *
  226  *      LOCKING:
  227  *      No locking required
  228  *
  229  *      RETURNS:
  230  *      16-bit value read from the register.
  231  */
  232 static inline void
  233 ti_i2c_write_reg(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)
  234 {
  235         /* XXXOMAP3: FIXME add registers mapping here */
  236         bus_write_2(sc->sc_mem_res, off, val);
  237 }
  238 
  239 /**
  240  *      ti_i2c_set_intr_enable - writes the interrupt enable register
  241  *      @sc: I2C device context
  242  *      @ie: bitmask of the interrupts to enable
  243  *
  244  *      This function is needed as writing the I2C_IE register on the OMAP4 devices
  245  *      doesn't seem to actually enable the interrupt, rather you have to write
  246  *      through the I2C_IRQENABLE_CLR and I2C_IRQENABLE_SET registers.
  247  *
  248  *      LOCKING:
  249  *      No locking required
  250  *
  251  *      RETURNS:
  252  *      Nothing.
  253  */
  254 static inline void
  255 ti_i2c_set_intr_enable(struct ti_i2c_softc *sc, uint16_t ie)
  256 {
  257         /* XXXOMAP3: FIXME */
  258         ti_i2c_write_2(sc, I2C_REG_IRQENABLE_CLR, 0xffff);
  259         if (ie)
  260                 ti_i2c_write_2(sc, I2C_REG_IRQENABLE_SET, ie);
  261 }
  262 
  263 /**
  264  *      ti_i2c_reset - attach function for the driver
  265  *      @dev: i2c device handle
  266  *
  267  *
  268  *
  269  *      LOCKING:
  270  *      Called from timer context
  271  *
  272  *      RETURNS:
  273  *      EH_HANDLED or EH_NOT_HANDLED
  274  */
  275 static int
  276 ti_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
  277 {
  278         struct ti_i2c_softc *sc = device_get_softc(dev);
  279         struct ti_i2c_clock_config *clkcfg;
  280         uint16_t con_reg;
  281 
  282         clkcfg = ti_i2c_clock_configs;
  283         while (clkcfg->speed != -1) {
  284                 if (clkcfg->speed == speed)
  285                         break;
  286                 /* take slow if speed is unknown */
  287                 if ((speed == IIC_UNKNOWN) && (clkcfg->speed == IIC_SLOW))
  288                         break;
  289                 clkcfg++;
  290         }
  291         if (clkcfg->speed == -1)
  292                 return (EINVAL);
  293 
  294         TI_I2C_LOCK(sc);
  295 
  296         /* First disable the controller while changing the clocks */
  297         con_reg = ti_i2c_read_reg(sc, I2C_REG_CON);
  298         ti_i2c_write_reg(sc, I2C_REG_CON, 0x0000);
  299 
  300         /* Program the prescaler */
  301         ti_i2c_write_reg(sc, I2C_REG_PSC, clkcfg->psc);
  302 
  303         /* Set the bitrate */
  304         ti_i2c_write_reg(sc, I2C_REG_SCLL, clkcfg->scll | (clkcfg->hsscll<<8));
  305         ti_i2c_write_reg(sc, I2C_REG_SCLH, clkcfg->sclh | (clkcfg->hssclh<<8));
  306 
  307         /* Check if we are dealing with high speed mode */
  308         if ((clkcfg->hsscll + clkcfg->hssclh) > 0)
  309                 con_reg  = I2C_CON_OPMODE_HS;
  310         else
  311                 con_reg  = I2C_CON_OPMODE_STD;
  312 
  313         /* Enable the I2C module again */
  314         ti_i2c_write_reg(sc, I2C_REG_CON, I2C_CON_I2C_EN | con_reg);
  315 
  316         TI_I2C_UNLOCK(sc);
  317 
  318         return (IIC_ENOADDR);
  319 }
  320 
  321 /**
  322  *      ti_i2c_intr - interrupt handler for the I2C module
  323  *      @dev: i2c device handle
  324  *
  325  *
  326  *
  327  *      LOCKING:
  328  *      Called from timer context
  329  *
  330  *      RETURNS:
  331  *      EH_HANDLED or EH_NOT_HANDLED
  332  */
  333 static void
  334 ti_i2c_intr(void *arg)
  335 {
  336         struct ti_i2c_softc *sc = (struct ti_i2c_softc*) arg;
  337         uint16_t status;
  338 
  339         status = ti_i2c_read_reg(sc, I2C_REG_STAT);
  340         if (status == 0)
  341                 return;
  342 
  343         TI_I2C_LOCK(sc);
  344 
  345         /* save the flags */
  346         sc->sc_stat_flags |= status;
  347 
  348         /* clear the status flags */
  349         ti_i2c_write_reg(sc, I2C_REG_STAT, status);
  350 
  351         /* wakeup the process the started the transaction */
  352         wakeup(sc);
  353 
  354         TI_I2C_UNLOCK(sc);
  355 
  356         return;
  357 }
  358 
  359 /**
  360  *      ti_i2c_wait - waits for the specific event to occur
  361  *      @sc: i2c driver context
  362  *      @flags: the event(s) to wait on, this is a bitmask of the I2C_STAT_??? flags
  363  *      @statp: if not null will contain the status flags upon return
  364  *      @timo: the number of ticks to wait
  365  *
  366  *
  367  *
  368  *      LOCKING:
  369  *      The driver context must be locked before calling this function. Internally
  370  *      the function sleeps, releasing the lock as it does so, however the lock is
  371  *      always retaken before this function returns.
  372  *
  373  *      RETURNS:
  374  *      0 if the event(s) were tripped within timeout period
  375  *      EBUSY if timedout waiting for the events
  376  *      ENXIO if a NACK event was received
  377  */
  378 static int
  379 ti_i2c_wait(struct ti_i2c_softc *sc, uint16_t flags, uint16_t *statp, int timo)
  380 {
  381         int waittime = timo;
  382         int start_ticks = ticks;
  383         int rc;
  384 
  385         TI_I2C_ASSERT_LOCKED(sc);
  386 
  387         /* check if the condition has already occured, the interrupt routine will
  388          * clear the status flags.
  389          */
  390         if ((sc->sc_stat_flags & flags) == 0) {
  391 
  392                 /* condition(s) haven't occured so sleep on the IRQ */
  393                 while (waittime > 0) {
  394 
  395                         rc = mtx_sleep(sc, &sc->sc_mtx, 0, "I2Cwait", waittime);
  396                         if (rc == EWOULDBLOCK) {
  397                                 /* timed-out, simply break out of the loop */
  398                                 break;
  399                         } else {
  400                                 /* IRQ has been tripped, but need to sanity check we have the
  401                                  * right events in the status flag.
  402                                  */
  403                                 if ((sc->sc_stat_flags & flags) != 0)
  404                                         break;
  405 
  406                                 /* event hasn't been tripped so wait some more */
  407                                 waittime -= (ticks - start_ticks);
  408                                 start_ticks = ticks;
  409                         }
  410                 }
  411         }
  412 
  413         /* copy the actual status bits */
  414         if (statp != NULL)
  415                 *statp = sc->sc_stat_flags;
  416 
  417         /* return the status found */
  418         if ((sc->sc_stat_flags & flags) != 0)
  419                 rc = 0;
  420         else
  421                 rc = EBUSY;
  422 
  423         /* clear the flags set by the interrupt handler */
  424         sc->sc_stat_flags = 0;
  425 
  426         return (rc);
  427 }
  428 
  429 /**
  430  *      ti_i2c_wait_for_free_bus - waits for the bus to become free
  431  *      @sc: i2c driver context
  432  *      @timo: the time to wait for the bus to become free
  433  *
  434  *
  435  *
  436  *      LOCKING:
  437  *      The driver context must be locked before calling this function. Internally
  438  *      the function sleeps, releasing the lock as it does so, however the lock is
  439  *      always taken before this function returns.
  440  *
  441  *      RETURNS:
  442  *      0 if the event(s) were tripped within timeout period
  443  *      EBUSY if timedout waiting for the events
  444  *      ENXIO if a NACK event was received
  445  */
  446 static int
  447 ti_i2c_wait_for_free_bus(struct ti_i2c_softc *sc, int timo)
  448 {
  449         /* check if the bus is free, BB bit = 0 */
  450         if ((ti_i2c_read_reg(sc, I2C_REG_STAT) & I2C_STAT_BB) == 0)
  451                 return 0;
  452 
  453         /* enable bus free interrupts */
  454         ti_i2c_set_intr_enable(sc, I2C_IE_BF);
  455 
  456         /* wait for the bus free interrupt to be tripped */
  457         return ti_i2c_wait(sc, I2C_STAT_BF, NULL, timo);
  458 }
  459 
  460 /**
  461  *      ti_i2c_read_bytes - attempts to perform a read operation
  462  *      @sc: i2c driver context
  463  *      @buf: buffer to hold the received bytes
  464  *      @len: the number of bytes to read
  465  *
  466  *      This function assumes the slave address is already set
  467  *
  468  *      LOCKING:
  469  *      The context lock should be held before calling this function
  470  *
  471  *      RETURNS:
  472  *      0 on function succeeded
  473  *      EINVAL if invalid message is passed as an arg
  474  */
  475 static int
  476 ti_i2c_read_bytes(struct ti_i2c_softc *sc, uint8_t *buf, uint16_t len)
  477 {
  478         int      timo = (hz / 4);
  479         int      err = 0;
  480         uint16_t con_reg;
  481         uint16_t events;
  482         uint16_t status;
  483         uint32_t amount = 0;
  484         uint32_t sofar = 0;
  485         uint32_t i;
  486 
  487         /* wait for the bus to become free */
  488         err = ti_i2c_wait_for_free_bus(sc, timo);
  489         if (err != 0) {
  490                 device_printf(sc->sc_dev, "bus never freed\n");
  491                 return (err);
  492         }
  493 
  494         /* set the events to wait for */
  495         events = I2C_IE_RDR |   /* Receive draining interrupt */
  496                  I2C_IE_RRDY |  /* Receive Data Ready interrupt */
  497                  I2C_IE_ARDY |  /* Register Access Ready interrupt */
  498                  I2C_IE_NACK |  /* No Acknowledgment interrupt */
  499                  I2C_IE_AL;
  500 
  501         /* enable interrupts for the events we want */
  502         ti_i2c_set_intr_enable(sc, events);
  503 
  504         /* write the number of bytes to read */
  505         ti_i2c_write_reg(sc, I2C_REG_CNT, len);
  506 
  507         /* clear the write bit and initiate the read transaction. Setting the STT
  508          * (start) bit initiates the transfer.
  509          */
  510         con_reg = ti_i2c_read_reg(sc, I2C_REG_CON);
  511         con_reg &= ~I2C_CON_TRX;
  512         con_reg |=  I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  513         ti_i2c_write_reg(sc, I2C_REG_CON, con_reg);
  514 
  515         /* reading loop */
  516         while (1) {
  517 
  518                 /* wait for an event */
  519                 err = ti_i2c_wait(sc, events, &status, timo);
  520                 if (err != 0) {
  521                         break;
  522                 }
  523 
  524                 /* check for the error conditions */
  525                 if (status & I2C_STAT_NACK) {
  526                         /* no ACK from slave */
  527                         ti_i2c_dbg(sc, "NACK\n");
  528                         err = ENXIO;
  529                         break;
  530                 }
  531                 if (status & I2C_STAT_AL) {
  532                         /* arbitration lost */
  533                         ti_i2c_dbg(sc, "Arbitration lost\n");
  534                         err = ENXIO;
  535                         break;
  536                 }
  537 
  538                 /* check if we have finished */
  539                 if (status & I2C_STAT_ARDY) {
  540                         /* register access ready - transaction complete basically */
  541                         ti_i2c_dbg(sc, "ARDY transaction complete\n");
  542                         err = 0;
  543                         break;
  544                 }
  545 
  546                 /* read some data */
  547                 if (status & I2C_STAT_RDR) {
  548                         /* Receive draining interrupt - last data received */
  549                         ti_i2c_dbg(sc, "Receive draining interrupt\n");
  550 
  551                         /* get the number of bytes in the FIFO */
  552                         amount = ti_i2c_read_reg(sc, I2C_REG_BUFSTAT);
  553                         amount >>= 8;
  554                         amount &= 0x3f;
  555                 }
  556                 else if (status & I2C_STAT_RRDY) {
  557                         /* Receive data ready interrupt - enough data received */
  558                         ti_i2c_dbg(sc, "Receive data ready interrupt\n");
  559 
  560                         /* get the number of bytes in the FIFO */
  561                         amount = ti_i2c_read_reg(sc, I2C_REG_BUF);
  562                         amount >>= 8;
  563                         amount &= 0x3f;
  564                         amount += 1;
  565                 }
  566 
  567                 /* sanity check we haven't overwritten the array */
  568                 if ((sofar + amount) > len) {
  569                         ti_i2c_dbg(sc, "to many bytes to read\n");
  570                         amount = (len - sofar);
  571                 }
  572 
  573                 /* read the bytes from the fifo */
  574                 for (i = 0; i < amount; i++) {
  575                         buf[sofar++] = (uint8_t)(ti_i2c_read_reg(sc, I2C_REG_DATA) & 0xff);
  576                 }
  577 
  578                 /* attempt to clear the receive ready bits */
  579                 ti_i2c_write_reg(sc, I2C_REG_STAT, I2C_STAT_RDR | I2C_STAT_RRDY);
  580         }
  581 
  582         /* reset the registers regardless if there was an error or not */
  583         ti_i2c_set_intr_enable(sc, 0x0000);
  584         ti_i2c_write_reg(sc, I2C_REG_CON, I2C_CON_I2C_EN | I2C_CON_MST | I2C_CON_STP);
  585 
  586         return (err);
  587 }
  588 
  589 /**
  590  *      ti_i2c_write_bytes - attempts to perform a read operation
  591  *      @sc: i2c driver context
  592  *      @buf: buffer containing the bytes to write
  593  *      @len: the number of bytes to write
  594  *
  595  *      This function assumes the slave address is already set
  596  *
  597  *      LOCKING:
  598  *      The context lock should be held before calling this function
  599  *
  600  *      RETURNS:
  601  *      0 on function succeeded
  602  *      EINVAL if invalid message is passed as an arg
  603  */
  604 static int
  605 ti_i2c_write_bytes(struct ti_i2c_softc *sc, const uint8_t *buf, uint16_t len)
  606 {
  607         int      timo = (hz / 4);
  608         int      err = 0;
  609         uint16_t con_reg;
  610         uint16_t events;
  611         uint16_t status;
  612         uint32_t amount = 0;
  613         uint32_t sofar = 0;
  614         uint32_t i;
  615 
  616         /* wait for the bus to become free */
  617         err = ti_i2c_wait_for_free_bus(sc, timo);
  618         if (err != 0)
  619                 return (err);
  620 
  621         /* set the events to wait for */
  622         events = I2C_IE_XDR |   /* Transmit draining interrupt */
  623                  I2C_IE_XRDY |  /* Transmit Data Ready interrupt */
  624                  I2C_IE_ARDY |  /* Register Access Ready interrupt */
  625                  I2C_IE_NACK |  /* No Acknowledgment interrupt */
  626                  I2C_IE_AL;
  627 
  628         /* enable interrupts for the events we want*/
  629         ti_i2c_set_intr_enable(sc, events);
  630 
  631         /* write the number of bytes to write */
  632         ti_i2c_write_reg(sc, I2C_REG_CNT, len);
  633 
  634         /* set the write bit and initiate the write transaction. Setting the STT
  635          * (start) bit initiates the transfer.
  636          */
  637         con_reg = ti_i2c_read_reg(sc, I2C_REG_CON);
  638         con_reg |= I2C_CON_TRX | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP;
  639         ti_i2c_write_reg(sc, I2C_REG_CON, con_reg);
  640 
  641         /* writing loop */
  642         while (1) {
  643 
  644                 /* wait for an event */
  645                 err = ti_i2c_wait(sc, events, &status, timo);
  646                 if (err != 0) {
  647                         break;
  648                 }
  649 
  650                 /* check for the error conditions */
  651                 if (status & I2C_STAT_NACK) {
  652                         /* no ACK from slave */
  653                         ti_i2c_dbg(sc, "NACK\n");
  654                         err = ENXIO;
  655                         break;
  656                 }
  657                 if (status & I2C_STAT_AL) {
  658                         /* arbitration lost */
  659                         ti_i2c_dbg(sc, "Arbitration lost\n");
  660                         err = ENXIO;
  661                         break;
  662                 }
  663 
  664                 /* check if we have finished */
  665                 if (status & I2C_STAT_ARDY) {
  666                         /* register access ready - transaction complete basically */
  667                         ti_i2c_dbg(sc, "ARDY transaction complete\n");
  668                         err = 0;
  669                         break;
  670                 }
  671 
  672                 /* read some data */
  673                 if (status & I2C_STAT_XDR) {
  674                         /* Receive draining interrupt - last data received */
  675                         ti_i2c_dbg(sc, "Transmit draining interrupt\n");
  676 
  677                         /* get the number of bytes in the FIFO */
  678                         amount = ti_i2c_read_reg(sc, I2C_REG_BUFSTAT);
  679                         amount &= 0x3f;
  680                 }
  681                 else if (status & I2C_STAT_XRDY) {
  682                         /* Receive data ready interrupt - enough data received */
  683                         ti_i2c_dbg(sc, "Transmit data ready interrupt\n");
  684 
  685                         /* get the number of bytes in the FIFO */
  686                         amount = ti_i2c_read_reg(sc, I2C_REG_BUF);
  687                         amount &= 0x3f;
  688                         amount += 1;
  689                 }
  690 
  691                 /* sanity check we haven't overwritten the array */
  692                 if ((sofar + amount) > len) {
  693                         ti_i2c_dbg(sc, "to many bytes to write\n");
  694                         amount = (len - sofar);
  695                 }
  696 
  697                 /* write the bytes from the fifo */
  698                 for (i = 0; i < amount; i++) {
  699                         ti_i2c_write_reg(sc, I2C_REG_DATA, buf[sofar++]);
  700                 }
  701 
  702                 /* attempt to clear the transmit ready bits */
  703                 ti_i2c_write_reg(sc, I2C_REG_STAT, I2C_STAT_XDR | I2C_STAT_XRDY);
  704         }
  705 
  706         /* reset the registers regardless if there was an error or not */
  707         ti_i2c_set_intr_enable(sc, 0x0000);
  708         ti_i2c_write_reg(sc, I2C_REG_CON, I2C_CON_I2C_EN | I2C_CON_MST | I2C_CON_STP);
  709 
  710         return (err);
  711 }
  712 
  713 /**
  714  *      ti_i2c_transfer - called to perform the transfer
  715  *      @dev: i2c device handle
  716  *      @msgs: the messages to send/receive
  717  *      @nmsgs: the number of messages in the msgs array
  718  *
  719  *
  720  *      LOCKING:
  721  *      Internally locked
  722  *
  723  *      RETURNS:
  724  *      0 on function succeeded
  725  *      EINVAL if invalid message is passed as an arg
  726  */
  727 static int
  728 ti_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
  729 {
  730         struct ti_i2c_softc *sc = device_get_softc(dev);
  731         int err = 0;
  732         uint32_t i;
  733         uint16_t len;
  734         uint8_t *buf;
  735 
  736         TI_I2C_LOCK(sc);
  737 
  738         for (i = 0; i < nmsgs; i++) {
  739 
  740                 len = msgs[i].len;
  741                 buf = msgs[i].buf;
  742 
  743                 /* zero byte transfers aren't allowed */
  744                 if (len == 0 || buf == NULL) {
  745                         err = EINVAL;
  746                         goto out;
  747                 }
  748 
  749                 /* set the slave address */
  750                 ti_i2c_write_reg(sc, I2C_REG_SA, msgs[i].slave >> 1);
  751 
  752                 /* perform the read or write */
  753                 if (msgs[i].flags & IIC_M_RD) {
  754                         err = ti_i2c_read_bytes(sc, buf, len);
  755                 } else {
  756                         err = ti_i2c_write_bytes(sc, buf, len);
  757                 }
  758 
  759         }
  760 
  761 out:
  762         TI_I2C_UNLOCK(sc);
  763 
  764         return (err);
  765 }
  766 
  767 /**
  768  *      ti_i2c_callback - not sure about this one
  769  *      @dev: i2c device handle
  770  *
  771  *
  772  *
  773  *      LOCKING:
  774  *      Called from timer context
  775  *
  776  *      RETURNS:
  777  *      EH_HANDLED or EH_NOT_HANDLED
  778  */
  779 static int
  780 ti_i2c_callback(device_t dev, int index, caddr_t data)
  781 {
  782         int error = 0;
  783 
  784         switch (index) {
  785                 case IIC_REQUEST_BUS:
  786                         break;
  787 
  788                 case IIC_RELEASE_BUS:
  789                         break;
  790 
  791                 default:
  792                         error = EINVAL;
  793         }
  794 
  795         return (error);
  796 }
  797 
  798 /**
  799  *      ti_i2c_activate - initialises and activates an I2C bus
  800  *      @dev: i2c device handle
  801  *      @num: the number of the I2C controller to activate; 1, 2 or 3
  802  *
  803  *
  804  *      LOCKING:
  805  *      Assumed called in an atomic context.
  806  *
  807  *      RETURNS:
  808  *      nothing
  809  */
  810 static int
  811 ti_i2c_activate(device_t dev)
  812 {
  813         struct ti_i2c_softc *sc = (struct ti_i2c_softc*) device_get_softc(dev);
  814         unsigned int timeout = 0;
  815         uint16_t con_reg;
  816         int err;
  817         clk_ident_t clk;
  818 
  819         /*
  820          * The following sequence is taken from the OMAP3530 technical reference
  821          *
  822          * 1. Enable the functional and interface clocks (see Section 18.3.1.1.1).
  823          */
  824         clk = I2C0_CLK + sc->device_id;
  825         err = ti_prcm_clk_enable(clk);
  826         if (err)
  827                 return (err);
  828 
  829         /* There seems to be a bug in the I2C reset mechanism, for some reason you
  830          * need to disable the I2C module before issuing the reset and then enable
  831          * it again after to detect the reset done.
  832          *
  833          * I found this out by looking at the Linux driver implementation, thanks
  834          * linux guys!
  835          */
  836 
  837         /* Disable the I2C controller */
  838         ti_i2c_write_reg(sc, I2C_REG_CON, 0x0000);
  839 
  840         /* Issue a softreset to the controller */
  841         /* XXXOMAP3: FIXME */
  842         bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, 0x0002);
  843 
  844         /* Re-enable the module and then check for the reset done */
  845         ti_i2c_write_reg(sc, I2C_REG_CON, I2C_CON_I2C_EN);
  846 
  847         while ((ti_i2c_read_reg(sc, I2C_REG_SYSS) & 0x01) == 0x00) {
  848                 if (timeout++ > 100) {
  849                         return (EBUSY);
  850                 }
  851                 DELAY(100);
  852         }
  853 
  854         /* Disable the I2C controller once again, now that the reset has finished */
  855         ti_i2c_write_reg(sc, I2C_REG_CON, 0x0000);
  856 
  857         /* 2. Program the prescaler to obtain an approximately 12-MHz internal
  858          *    sampling clock (I2Ci_INTERNAL_CLK) by programming the corresponding
  859          *    value in the I2Ci.I2C_PSC[3:0] PSC field.
  860          *    This value depends on the frequency of the functional clock (I2Ci_FCLK).
  861          *    Because this frequency is 96MHz, the I2Ci.I2C_PSC[7:0] PSC field value
  862          *    is 0x7.
  863          */
  864 
  865         /* Program the prescaler to obtain an approximately 12-MHz internal
  866          * sampling clock.
  867          */
  868         ti_i2c_write_reg(sc, I2C_REG_PSC, 0x0017);
  869 
  870         /* 3. Program the I2Ci.I2C_SCLL[7:0] SCLL and I2Ci.I2C_SCLH[7:0] SCLH fields
  871          *    to obtain a bit rate of 100K bps or 400K bps. These values depend on
  872          *    the internal sampling clock frequency (see Table 18-12).
  873          */
  874 
  875         /* Set the bitrate to 100kbps */
  876         ti_i2c_write_reg(sc, I2C_REG_SCLL, 0x000d);
  877         ti_i2c_write_reg(sc, I2C_REG_SCLH, 0x000f);
  878 
  879         /* 4. (Optional) Program the I2Ci.I2C_SCLL[15:8] HSSCLL and
  880          *    I2Ci.I2C_SCLH[15:8] HSSCLH fields to obtain a bit rate of 400K bps or
  881          *    3.4M bps (for the second phase of HS mode). These values depend on the
  882          *    internal sampling clock frequency (see Table 18-12).
  883          *
  884          * 5. (Optional) If a bit rate of 3.4M bps is used and the bus line
  885          *    capacitance exceeds 45 pF, program the CONTROL.CONTROL_DEVCONF1[12]
  886          *    I2C1HSMASTER bit for I2C1, the CONTROL.CONTROL_DEVCONF1[13]
  887          *    I2C2HSMASTER bit for I2C2, or the CONTROL.CONTROL_DEVCONF1[14]
  888          *    I2C3HSMASTER bit for I2C3.
  889          */
  890 
  891         /* 6. Configure the Own Address of the I2C controller by storing it in the
  892          *    I2Ci.I2C_OA0 register. Up to four Own Addresses can be programmed in
  893          *    the I2Ci.I2C_OAi registers (with I = 0, 1, 2, 3) for each I2C
  894          *    controller.
  895          *
  896          * Note: For a 10-bit address, set the corresponding expand Own Address bit
  897          * in the I2Ci.I2C_CON register.
  898          */
  899 
  900         /* Driver currently always in single master mode so ignore this step */
  901 
  902         /* 7. Set the TX threshold (in transmitter mode) and the RX threshold (in
  903          *    receiver mode) by setting the I2Ci.I2C_BUF[5:0]XTRSH field to (TX
  904          *    threshold - 1) and the I2Ci.I2C_BUF[13:8]RTRSH field to (RX threshold
  905          *    - 1), where the TX and RX thresholds are greater than or equal to 1.
  906          */
  907 
  908         /* Set the FIFO buffer threshold, note I2C1 & I2C2 have 8 byte FIFO, whereas
  909          * I2C3 has 64 bytes.  Threshold set to 5 for now.
  910          */
  911         ti_i2c_write_reg(sc, I2C_REG_BUF, 0x0404);
  912 
  913         /*
  914          * 8. Take the I2C controller out of reset by setting the I2Ci.I2C_CON[15]
  915          *    I2C_EN bit to 1.
  916          */
  917         ti_i2c_write_reg(sc, I2C_REG_CON, I2C_CON_I2C_EN | I2C_CON_OPMODE_STD);
  918 
  919         /*
  920          * To initialize the I2C controller, perform the following steps:
  921          *
  922          * 1. Configure the I2Ci.I2C_CON register:
  923          *    · For master or slave mode, set the I2Ci.I2C_CON[10] MST bit (0: slave,
  924          *      1: master).
  925          *    · For transmitter or receiver mode, set the I2Ci.I2C_CON[9] TRX bit
  926          *      (0: receiver, 1: transmitter).
  927          */
  928         con_reg = ti_i2c_read_reg(sc, I2C_REG_CON);
  929         con_reg |= I2C_CON_MST;
  930         ti_i2c_write_reg(sc, I2C_REG_CON, con_reg);
  931 
  932         /* 2. If using an interrupt to transmit/receive data, set to 1 the
  933          *    corresponding bit in the I2Ci.I2C_IE register (the I2Ci.I2C_IE[4]
  934          *    XRDY_IE bit for the transmit interrupt, the I2Ci.I2C_IE[3] RRDY bit
  935          *    for the receive interrupt).
  936          */
  937         ti_i2c_set_intr_enable(sc, I2C_IE_XRDY | I2C_IE_RRDY);
  938 
  939         /* 3. If using DMA to receive/transmit data, set to 1 the corresponding bit
  940          *    in the I2Ci.I2C_BUF register (the I2Ci.I2C_BUF[15] RDMA_EN bit for the
  941          *    receive DMA channel, the I2Ci.I2C_BUF[7] XDMA_EN bit for the transmit
  942          *    DMA channel).
  943          */
  944 
  945         /* not using DMA for now, so ignore this */
  946 
  947         return (0);
  948 }
  949 
  950 /**
  951  *      ti_i2c_deactivate - deactivates the controller and releases resources
  952  *      @dev: i2c device handle
  953  *
  954  *
  955  *
  956  *      LOCKING:
  957  *      Assumed called in an atomic context.
  958  *
  959  *      RETURNS:
  960  *      nothing
  961  */
  962 static void
  963 ti_i2c_deactivate(device_t dev)
  964 {
  965         struct ti_i2c_softc *sc = device_get_softc(dev);
  966         clk_ident_t clk;
  967 
  968         /* Disable the controller - cancel all transactions */
  969         ti_i2c_write_reg(sc, I2C_REG_CON, 0x0000);
  970 
  971         /* Release the interrupt handler */
  972         if (sc->sc_irq_h) {
  973                 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
  974                 sc->sc_irq_h = 0;
  975         }
  976 
  977         bus_generic_detach(sc->sc_dev);
  978 
  979         /* Unmap the I2C controller registers */
  980         if (sc->sc_mem_res != 0) {
  981                 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_irq_res),
  982                                                          sc->sc_mem_res);
  983                 sc->sc_mem_res = NULL;
  984         }
  985 
  986         /* Release the IRQ resource */
  987         if (sc->sc_irq_res != NULL) {
  988                 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->sc_irq_res),
  989                                                          sc->sc_irq_res);
  990                 sc->sc_irq_res = NULL;
  991         }
  992 
  993         /* Finally disable the functional and interface clocks */
  994         clk = I2C0_CLK + sc->device_id;
  995         ti_prcm_clk_disable(clk);
  996 
  997         return;
  998 }
  999 
 1000 /**
 1001  *      ti_i2c_probe - probe function for the driver
 1002  *      @dev: i2c device handle
 1003  *
 1004  *
 1005  *
 1006  *      LOCKING:
 1007  *
 1008  *
 1009  *      RETURNS:
 1010  *      Always returns 0
 1011  */
 1012 static int
 1013 ti_i2c_probe(device_t dev)
 1014 {
 1015 
 1016         if (!ofw_bus_status_okay(dev))
 1017                 return (ENXIO);
 1018 
 1019         if (!ofw_bus_is_compatible(dev, "ti,i2c"))
 1020                 return (ENXIO);
 1021 
 1022         device_set_desc(dev, "TI I2C Controller");
 1023         return (0);
 1024 }
 1025 
 1026 /**
 1027  *      ti_i2c_attach - attach function for the driver
 1028  *      @dev: i2c device handle
 1029  *
 1030  *      Initialised driver data structures and activates the I2C controller.
 1031  *
 1032  *      LOCKING:
 1033  *
 1034  *
 1035  *      RETURNS:
 1036  *
 1037  */
 1038 static int
 1039 ti_i2c_attach(device_t dev)
 1040 {
 1041         struct ti_i2c_softc *sc = device_get_softc(dev);
 1042         phandle_t node;
 1043         pcell_t did;
 1044         int err;
 1045         int rid;
 1046 
 1047         sc->sc_dev = dev;
 1048 
 1049         /* Get the i2c device id from FDT */
 1050         node = ofw_bus_get_node(dev);
 1051         if ((OF_getprop(node, "i2c-device-id", &did, sizeof(did))) <= 0) {
 1052                 device_printf(dev, "missing i2c-device-id attribute in FDT\n");
 1053                 return (ENXIO);
 1054         }
 1055         sc->device_id = fdt32_to_cpu(did);
 1056 
 1057         TI_I2C_LOCK_INIT(sc);
 1058 
 1059         /* Get the memory resource for the register mapping */
 1060         rid = 0;
 1061         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
 1062                                                 RF_ACTIVE);
 1063         if (sc->sc_mem_res == NULL)
 1064                 panic("%s: Cannot map registers", device_get_name(dev));
 1065 
 1066         /* Allocate an IRQ resource for the MMC controller */
 1067         rid = 0;
 1068         sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
 1069                                                 RF_ACTIVE | RF_SHAREABLE);
 1070         if (sc->sc_irq_res == NULL) {
 1071                 err = ENOMEM;
 1072                 goto out;
 1073         }
 1074 
 1075         /* First we _must_ activate the H/W */
 1076         err = ti_i2c_activate(dev);
 1077         if (err) {
 1078                 device_printf(dev, "ti_i2c_activate failed\n");
 1079                 goto out;
 1080         }
 1081 
 1082         /* XXXOMAP3: FIXME get proper revision here */
 1083         /* Read the version number of the I2C module */
 1084         sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff;
 1085 
 1086         device_printf(dev, "I2C revision %d.%d\n", sc->sc_rev >> 4,
 1087             sc->sc_rev & 0xf);
 1088 
 1089         /* activate the interrupt */
 1090         err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
 1091                                 NULL, ti_i2c_intr, sc, &sc->sc_irq_h);
 1092         if (err)
 1093                 goto out;
 1094 
 1095         /* Attach to the iicbus */
 1096         if ((sc->sc_iicbus = device_add_child(dev, "iicbus", -1)) == NULL)
 1097                 device_printf(dev, "could not allocate iicbus instance\n");
 1098 
 1099         /* Probe and attach the iicbus */
 1100         bus_generic_attach(dev);
 1101 
 1102 out:
 1103         if (err) {
 1104                 ti_i2c_deactivate(dev);
 1105                 TI_I2C_LOCK_DESTROY(sc);
 1106         }
 1107 
 1108         return (err);
 1109 }
 1110 
 1111 /**
 1112  *      ti_i2c_detach - detach function for the driver
 1113  *      @dev: i2c device handle
 1114  *
 1115  *
 1116  *
 1117  *      LOCKING:
 1118  *
 1119  *
 1120  *      RETURNS:
 1121  *      Always returns 0
 1122  */
 1123 static int
 1124 ti_i2c_detach(device_t dev)
 1125 {
 1126         struct ti_i2c_softc *sc = device_get_softc(dev);
 1127         int rv;
 1128 
 1129         ti_i2c_deactivate(dev);
 1130 
 1131         if (sc->sc_iicbus && (rv = device_delete_child(dev, sc->sc_iicbus)) != 0)
 1132                 return (rv);
 1133 
 1134         TI_I2C_LOCK_DESTROY(sc);
 1135 
 1136         return (0);
 1137 }
 1138 
 1139 
 1140 static phandle_t
 1141 ti_i2c_get_node(device_t bus, device_t dev)
 1142 {
 1143         /* 
 1144          * Share controller node with iibus device
 1145          */
 1146         return ofw_bus_get_node(bus);
 1147 }
 1148 
 1149 static device_method_t ti_i2c_methods[] = {
 1150         /* Device interface */
 1151         DEVMETHOD(device_probe,         ti_i2c_probe),
 1152         DEVMETHOD(device_attach,        ti_i2c_attach),
 1153         DEVMETHOD(device_detach,        ti_i2c_detach),
 1154 
 1155         /* OFW methods */
 1156         DEVMETHOD(ofw_bus_get_node,     ti_i2c_get_node),
 1157 
 1158         /* iicbus interface */
 1159         DEVMETHOD(iicbus_callback,      ti_i2c_callback),
 1160         DEVMETHOD(iicbus_reset,         ti_i2c_reset),
 1161         DEVMETHOD(iicbus_transfer,      ti_i2c_transfer),
 1162         { 0, 0 }
 1163 };
 1164 
 1165 static driver_t ti_i2c_driver = {
 1166         "iichb",
 1167         ti_i2c_methods,
 1168         sizeof(struct ti_i2c_softc),
 1169 };
 1170 
 1171 DRIVER_MODULE(ti_iic, simplebus, ti_i2c_driver, ti_i2c_devclass, 0, 0);
 1172 DRIVER_MODULE(iicbus, ti_iic, iicbus_driver, iicbus_devclass, 0, 0);
 1173 
 1174 MODULE_DEPEND(ti_iic, ti_prcm, 1, 1, 1);
 1175 MODULE_DEPEND(ti_iic, iicbus, 1, 1, 1);

Cache object: 0e6cbb345969381ebcb7b366bfc11708


[ 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.