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/dev/bktr/bktr_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) 1998, 2001 Nicolas Souchu
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 /*
   31  * I2C support for the bti2c chipset.
   32  *
   33  * From brooktree848.c <fsmp@freefall.org>
   34  */
   35 
   36 #include "opt_bktr.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/kernel.h>
   40 #include <sys/malloc.h>
   41 #include <sys/systm.h>
   42 #include <sys/module.h>
   43 #include <sys/bus.h>
   44 #include <sys/uio.h>
   45 
   46 #if __FreeBSD_version < 500014
   47 #include <sys/select.h>
   48 #else
   49 #include <sys/selinfo.h>
   50 #endif
   51 
   52 #if (__FreeBSD_version < 500000)
   53 #include <machine/clock.h>              /* for DELAY */
   54 #include <pci/pcivar.h>
   55 #include <pci/pcireg.h>
   56 #else
   57 #include <dev/pci/pcivar.h>
   58 #include <dev/pci/pcireg.h>
   59 #endif
   60 
   61 #include <machine/bus_memio.h>          /* for bus space */
   62 #include <machine/bus.h>
   63 #include <sys/bus.h>
   64 
   65 #include <dev/bktr/ioctl_meteor.h>
   66 #include <dev/bktr/ioctl_bt848.h>       /* extensions to ioctl_meteor.h */
   67 #include <dev/bktr/bktr_reg.h>
   68 #include <dev/bktr/bktr_i2c.h>
   69 
   70 #include <dev/smbus/smbconf.h>
   71 #include <dev/iicbus/iiconf.h>
   72 
   73 #define I2C_DELAY       40
   74 
   75 /* Compilation is void if BKTR_USE_FREEBSD_SMBUS is not
   76  * defined. This allows bktr owners to have smbus active for there
   77  * motherboard and still use their bktr without smbus.
   78  */
   79 #if defined(BKTR_USE_FREEBSD_SMBUS)
   80 
   81 #define BTI2C_DEBUG(x)  if (bti2c_debug) (x)
   82 static int bti2c_debug = 0;
   83 
   84 /*
   85  * Call this to pass the address of the bktr device to the
   86  * bti2c_i2c layer and initialize all the I2C bus architecture
   87  */
   88 int bt848_i2c_attach(device_t dev)
   89 {
   90         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
   91         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
   92 
   93         sc->smbus = device_add_child(dev, "smbus", -1);
   94         sc->iicbb = device_add_child(dev, "iicbb", -1);
   95 
   96         if (!sc->iicbb || !sc->smbus)
   97                 return ENXIO;
   98 
   99         bus_generic_attach(dev);
  100 
  101         return (0);
  102 };
  103 
  104 int bt848_i2c_detach(device_t dev)
  105 {
  106         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
  107         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
  108         int error = 0;
  109 
  110         if ((error = bus_generic_detach(dev)))
  111                 goto error;
  112 
  113         if (sc->iicbb && (error = device_delete_child(dev, sc->iicbb)))
  114                 goto error;
  115 
  116         if (sc->smbus && (error = device_delete_child(dev, sc->smbus)))
  117                 goto error;
  118 
  119 error:
  120         return (error);
  121 }
  122 
  123 int bti2c_smb_callback(device_t dev, int index, caddr_t *data)
  124 {
  125         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
  126         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
  127         int error = 0;
  128 
  129         /* test each time if we already have/haven't the iicbus
  130          * to avoid deadlocks
  131          */
  132         switch (index) {
  133         case SMB_REQUEST_BUS:
  134                 /* XXX test & set */
  135                 if (!sc->bus_owned) {
  136                         sc->bus_owned = 1;
  137                 } else
  138                         error = EWOULDBLOCK;
  139                 break;
  140 
  141         case SMB_RELEASE_BUS:
  142                 /* XXX test & set */
  143                 if (sc->bus_owned) {
  144                         sc->bus_owned = 0;
  145                 } else
  146                         error = EINVAL;
  147                 break;
  148 
  149         default:
  150                 error = EINVAL;
  151         }
  152 
  153         return (error);
  154 }
  155 
  156 int bti2c_iic_callback(device_t dev, int index, caddr_t *data)
  157 {
  158         struct bktr_softc *bktr_sc = (struct bktr_softc *)device_get_softc(dev);
  159         struct bktr_i2c_softc *sc = &bktr_sc->i2c_sc;
  160         int error = 0;
  161 
  162         /* test each time if we already have/haven't the smbus
  163          * to avoid deadlocks
  164          */
  165         switch (index) {
  166         case IIC_REQUEST_BUS:
  167                 /* XXX test & set */
  168                 if (!sc->bus_owned) {
  169                         sc->bus_owned = 1;
  170                 } else
  171                         error = EWOULDBLOCK;
  172                 break;
  173 
  174         case IIC_RELEASE_BUS:
  175                 /* XXX test & set */
  176                 if (sc->bus_owned) {
  177                         sc->bus_owned = 0;
  178                 } else
  179                         error = EINVAL;
  180                 break;
  181 
  182         default:
  183                 error = EINVAL;
  184         }
  185 
  186         return (error);
  187 }
  188 
  189 int bti2c_iic_reset(device_t dev, u_char speed, u_char addr, u_char * oldaddr)
  190 {
  191         if (oldaddr)
  192                 *oldaddr = 0;                   /* XXX */
  193 
  194         return (IIC_ENOADDR);
  195 }
  196 
  197 void bti2c_iic_setsda(device_t dev, int val)
  198 {
  199         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  200         int clock;
  201 
  202         clock = INL(sc, BKTR_I2C_DATA_CTL) & 0x2;
  203 
  204         if (val)
  205                 OUTL(sc, BKTR_I2C_DATA_CTL, clock | 1);
  206         else
  207                 OUTL(sc, BKTR_I2C_DATA_CTL, clock);
  208 
  209         DELAY(I2C_DELAY);
  210 
  211         return;
  212 }
  213 
  214 void bti2c_iic_setscl(device_t dev, int val)
  215 {
  216         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  217         int data;
  218 
  219         data = INL(sc, BKTR_I2C_DATA_CTL) & 0x1;
  220 
  221         if (val)
  222                 OUTL(sc, BKTR_I2C_DATA_CTL, 0x2 | data);
  223         else
  224                 OUTL(sc, BKTR_I2C_DATA_CTL, data);
  225 
  226         DELAY(I2C_DELAY);
  227 
  228         return;
  229 }
  230 
  231 int
  232 bti2c_iic_getsda(device_t dev)
  233 {
  234         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  235 
  236         return (INL(sc,BKTR_I2C_DATA_CTL) & 0x1);
  237 }
  238 
  239 int
  240 bti2c_iic_getscl(device_t dev)
  241 {
  242         return (0);
  243 }
  244 
  245 static int
  246 bti2c_write(struct bktr_softc *sc, u_long data)
  247 {
  248         u_long          x;
  249 
  250         /* clear status bits */
  251         OUTL(sc, BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
  252 
  253         BTI2C_DEBUG(printf("w%lx", data));
  254 
  255         /* write the address and data */
  256         OUTL(sc, BKTR_I2C_DATA_CTL, data);
  257 
  258         /* wait for completion */
  259         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
  260                 if ( INL(sc, BKTR_INT_STAT) & BT848_INT_I2CDONE )
  261                         break;
  262         }
  263 
  264         /* check for ACK */
  265         if ( !x || !( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK) ) {
  266                 BTI2C_DEBUG(printf("%c%c", (!x)?'+':'-',
  267                         (!( INL(sc, BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
  268                 return (SMB_ENOACK);
  269         }
  270         BTI2C_DEBUG(printf("+"));
  271 
  272         /* return OK */
  273         return( 0 );
  274 }
  275 
  276 int
  277 bti2c_smb_writeb(device_t dev, u_char slave, char cmd, char byte)
  278 {
  279         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  280         u_long data;
  281 
  282         data = ((slave & 0xff) << 24) | ((byte & 0xff) << 16) | (u_char)cmd;
  283 
  284         return (bti2c_write(sc, data));
  285 }
  286 
  287 /*
  288  * byte1 becomes low byte of word
  289  * byte2 becomes high byte of word
  290  */
  291 int
  292 bti2c_smb_writew(device_t dev, u_char slave, char cmd, short word)
  293 {
  294         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  295         u_long data;
  296         char low, high;
  297 
  298         low = (char)(word & 0xff);
  299         high = (char)((word & 0xff00) >> 8);
  300 
  301         data = ((slave & 0xff) << 24) | ((low & 0xff) << 16) |
  302                 ((high & 0xff) << 8) | BT848_DATA_CTL_I2CW3B | (u_char)cmd;
  303 
  304         return (bti2c_write(sc, data));
  305 }
  306 
  307 /*
  308  * The Bt878 and Bt879 differed on the treatment of i2c commands
  309  */
  310 int
  311 bti2c_smb_readb(device_t dev, u_char slave, char cmd, char *byte)
  312 {
  313         struct bktr_softc *sc  = (struct bktr_softc *)device_get_softc(dev);
  314         u_long          x;
  315 
  316         /* clear status bits */
  317         OUTL(sc,BKTR_INT_STAT, (BT848_INT_RACK | BT848_INT_I2CDONE));
  318 
  319         OUTL(sc,BKTR_I2C_DATA_CTL, ((slave & 0xff) << 24) | (u_char)cmd);;
  320 
  321         BTI2C_DEBUG(printf("r%lx/", (u_long)(((slave & 0xff) << 24) | (u_char)cmd)));
  322 
  323         /* wait for completion */
  324         for ( x = 0x7fffffff; x; --x ) {        /* safety valve */
  325                 if ( INL(sc,BKTR_INT_STAT) & BT848_INT_I2CDONE )
  326                         break;
  327         }
  328 
  329         /* check for ACK */
  330         if ( !x || !(INL(sc,BKTR_INT_STAT) & BT848_INT_RACK) ) {
  331                 BTI2C_DEBUG(printf("r%c%c", (!x)?'+':'-',
  332                         (!( INL(sc,BKTR_INT_STAT) & BT848_INT_RACK))?'+':'-'));
  333                 return (SMB_ENOACK);
  334         }
  335 
  336         *byte = (char)((INL(sc,BKTR_I2C_DATA_CTL) >> 8) & 0xff);
  337         BTI2C_DEBUG(printf("r%x+", *byte));
  338 
  339         return (0);
  340 }
  341 
  342 #endif /* defined(BKTR_USE_FREEBSD_SMBUS) */

Cache object: 145878e9b0df503f88f2e00b73b62610


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