2012-08-15 06:31:32 +00:00
|
|
|
/*-
|
2014-08-20 17:02:37 +00:00
|
|
|
* Copyright (c) 2011 Ben Gray <ben.r.gray@gmail.com>.
|
|
|
|
* Copyright (c) 2014 Luiz Otavio O Souza <loos@freebsd.org>.
|
2012-08-15 06:31:32 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Driver for the I2C module on the TI SoC.
|
|
|
|
*
|
|
|
|
* This driver is heavily based on the TWI driver for the AT91 (at91_twi.c).
|
|
|
|
*
|
|
|
|
* CAUTION: The I2Ci registers are limited to 16 bit and 8 bit data accesses,
|
|
|
|
* 32 bit data access is not allowed and can corrupt register content.
|
|
|
|
*
|
|
|
|
* This driver currently doesn't use DMA for the transfer, although I hope to
|
|
|
|
* incorporate that sometime in the future. The idea being that for transaction
|
|
|
|
* larger than a certain size the DMA engine is used, for anything less the
|
|
|
|
* normal interrupt/fifo driven option is used.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* WARNING: This driver uses mtx_sleep and interrupts to perform transactions,
|
|
|
|
* which means you can't do a transaction during startup before the interrupts
|
|
|
|
* have been enabled. Hint - the freebsd function config_intrhook_establish().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/rman.h>
|
2014-08-20 17:02:37 +00:00
|
|
|
#include <sys/sysctl.h>
|
2012-08-15 06:31:32 +00:00
|
|
|
#include <machine/bus.h>
|
|
|
|
|
|
|
|
#include <dev/ofw/openfirm.h>
|
|
|
|
#include <dev/ofw/ofw_bus.h>
|
|
|
|
#include <dev/ofw/ofw_bus_subr.h>
|
|
|
|
|
2014-05-17 18:52:20 +00:00
|
|
|
#include <arm/ti/ti_cpuid.h>
|
2012-08-15 06:31:32 +00:00
|
|
|
#include <arm/ti/ti_prcm.h>
|
|
|
|
#include <arm/ti/ti_i2c.h>
|
|
|
|
|
|
|
|
#include <dev/iicbus/iiconf.h>
|
|
|
|
#include <dev/iicbus/iicbus.h>
|
|
|
|
|
|
|
|
#include "iicbus_if.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I2C device driver context, a pointer to this is stored in the device
|
|
|
|
* driver structure.
|
|
|
|
*/
|
|
|
|
struct ti_i2c_softc
|
|
|
|
{
|
|
|
|
device_t sc_dev;
|
|
|
|
uint32_t device_id;
|
|
|
|
struct resource* sc_irq_res;
|
|
|
|
struct resource* sc_mem_res;
|
|
|
|
device_t sc_iicbus;
|
|
|
|
|
|
|
|
void* sc_irq_h;
|
|
|
|
|
|
|
|
struct mtx sc_mtx;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
struct iic_msg* sc_buffer;
|
|
|
|
int sc_bus_inuse;
|
|
|
|
int sc_buffer_pos;
|
|
|
|
int sc_error;
|
|
|
|
int sc_fifo_trsh;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
uint16_t sc_con_reg;
|
2012-08-15 06:31:32 +00:00
|
|
|
uint16_t sc_rev;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ti_i2c_clock_config
|
|
|
|
{
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
u_int frequency; /* Bus frequency in Hz */
|
2012-08-15 06:31:32 +00:00
|
|
|
uint8_t psc; /* Fast/Standard mode prescale divider */
|
|
|
|
uint8_t scll; /* Fast/Standard mode SCL low time */
|
|
|
|
uint8_t sclh; /* Fast/Standard mode SCL high time */
|
|
|
|
uint8_t hsscll; /* High Speed mode SCL low time */
|
|
|
|
uint8_t hssclh; /* High Speed mode SCL high time */
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(SOC_OMAP4)
|
2014-11-18 03:26:52 +00:00
|
|
|
/*
|
|
|
|
* OMAP4 i2c bus clock is 96MHz / ((psc + 1) * (scll + 7 + sclh + 5)).
|
|
|
|
* The prescaler values for 100KHz and 400KHz modes come from the table in the
|
|
|
|
* OMAP4 TRM. The table doesn't list 1MHz; these values should give that speed.
|
|
|
|
*/
|
2014-05-17 18:52:20 +00:00
|
|
|
static struct ti_i2c_clock_config ti_omap4_i2c_clock_configs[] = {
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
{ 100000, 23, 13, 15, 0, 0},
|
|
|
|
{ 400000, 9, 5, 7, 0, 0},
|
2014-12-22 00:50:01 +00:00
|
|
|
{ 1000000, 3, 5, 7, 0, 0},
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
/* { 3200000, 1, 113, 115, 7, 10}, - HS mode */
|
|
|
|
{ 0 /* Table terminator */ }
|
2014-05-17 18:52:20 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(SOC_TI_AM335X)
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
2014-11-18 03:26:52 +00:00
|
|
|
* AM335x i2c bus clock is 48MHZ / ((psc + 1) * (scll + 7 + sclh + 5))
|
|
|
|
* In all cases we prescale the clock to 24MHz as recommended in the manual.
|
2014-08-20 17:02:37 +00:00
|
|
|
*/
|
2014-05-17 18:52:20 +00:00
|
|
|
static struct ti_i2c_clock_config ti_am335x_i2c_clock_configs[] = {
|
2014-11-18 03:26:52 +00:00
|
|
|
{ 100000, 1, 111, 117, 0, 0},
|
|
|
|
{ 400000, 1, 23, 25, 0, 0},
|
|
|
|
{ 1000000, 1, 5, 7, 0, 0},
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
{ 0 /* Table terminator */ }
|
2012-08-15 06:31:32 +00:00
|
|
|
};
|
2014-05-17 18:52:20 +00:00
|
|
|
#endif
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Locking macros used throughout the driver
|
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
#define TI_I2C_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
|
|
|
|
#define TI_I2C_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
|
|
|
|
#define TI_I2C_LOCK_INIT(_sc) \
|
|
|
|
mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \
|
|
|
|
"ti_i2c", MTX_DEF)
|
|
|
|
#define TI_I2C_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx)
|
|
|
|
#define TI_I2C_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
|
|
|
|
#define TI_I2C_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-08-20 17:02:37 +00:00
|
|
|
#define ti_i2c_dbg(_sc, fmt, args...) \
|
|
|
|
device_printf((_sc)->sc_dev, fmt, ##args)
|
2012-08-15 06:31:32 +00:00
|
|
|
#else
|
2014-08-20 17:02:37 +00:00
|
|
|
#define ti_i2c_dbg(_sc, fmt, args...)
|
2012-08-15 06:31:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ti_i2c_read_2 - reads a 16-bit value from one of the I2C registers
|
|
|
|
* @sc: I2C device context
|
|
|
|
* @off: the byte offset within the register bank to read from.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* No locking required
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 16-bit value read from the register.
|
|
|
|
*/
|
|
|
|
static inline uint16_t
|
|
|
|
ti_i2c_read_2(struct ti_i2c_softc *sc, bus_size_t off)
|
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
|
|
|
|
return (bus_read_2(sc->sc_mem_res, off));
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ti_i2c_write_2 - writes a 16-bit value to one of the I2C registers
|
|
|
|
* @sc: I2C device context
|
|
|
|
* @off: the byte offset within the register bank to read from.
|
|
|
|
* @val: the value to write into the register
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* No locking required
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 16-bit value read from the register.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
ti_i2c_write_2(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)
|
|
|
|
{
|
|
|
|
|
|
|
|
bus_write_2(sc->sc_mem_res, off, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-08-20 17:02:37 +00:00
|
|
|
ti_i2c_transfer_intr(struct ti_i2c_softc* sc, uint16_t status)
|
2012-08-15 06:31:32 +00:00
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
int amount, done, i;
|
|
|
|
|
|
|
|
done = 0;
|
|
|
|
amount = 0;
|
|
|
|
/* Check for the error conditions. */
|
|
|
|
if (status & I2C_STAT_NACK) {
|
|
|
|
/* No ACK from slave. */
|
|
|
|
ti_i2c_dbg(sc, "NACK\n");
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_NACK);
|
|
|
|
sc->sc_error = ENXIO;
|
|
|
|
} else if (status & I2C_STAT_AL) {
|
|
|
|
/* Arbitration lost. */
|
|
|
|
ti_i2c_dbg(sc, "Arbitration lost\n");
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_AL);
|
|
|
|
sc->sc_error = ENXIO;
|
2014-05-17 18:52:20 +00:00
|
|
|
}
|
2014-08-20 17:02:37 +00:00
|
|
|
|
|
|
|
/* Check if we have finished. */
|
|
|
|
if (status & I2C_STAT_ARDY) {
|
|
|
|
/* Register access ready - transaction complete basically. */
|
|
|
|
ti_i2c_dbg(sc, "ARDY transaction complete\n");
|
|
|
|
if (sc->sc_error != 0 && sc->sc_buffer->flags & IIC_M_NOSTOP) {
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON,
|
|
|
|
sc->sc_con_reg | I2C_CON_STP);
|
|
|
|
}
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS,
|
|
|
|
I2C_STAT_ARDY | I2C_STAT_RDR | I2C_STAT_RRDY |
|
|
|
|
I2C_STAT_XDR | I2C_STAT_XRDY);
|
|
|
|
return (1);
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (sc->sc_buffer->flags & IIC_M_RD) {
|
|
|
|
/* Read some data. */
|
|
|
|
if (status & I2C_STAT_RDR) {
|
|
|
|
/*
|
|
|
|
* Receive draining interrupt - last data received.
|
|
|
|
* The set FIFO threshold wont be reached to trigger
|
|
|
|
* RRDY.
|
|
|
|
*/
|
|
|
|
ti_i2c_dbg(sc, "Receive draining interrupt\n");
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* Drain the FIFO. Read the pending data in the FIFO.
|
|
|
|
*/
|
|
|
|
amount = sc->sc_buffer->len - sc->sc_buffer_pos;
|
|
|
|
} else if (status & I2C_STAT_RRDY) {
|
|
|
|
/*
|
|
|
|
* Receive data ready interrupt - FIFO has reached the
|
|
|
|
* set threshold.
|
|
|
|
*/
|
|
|
|
ti_i2c_dbg(sc, "Receive data ready interrupt\n");
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
amount = min(sc->sc_fifo_trsh,
|
|
|
|
sc->sc_buffer->len - sc->sc_buffer_pos);
|
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Read the bytes from the fifo. */
|
|
|
|
for (i = 0; i < amount; i++)
|
|
|
|
sc->sc_buffer->buf[sc->sc_buffer_pos++] =
|
|
|
|
(uint8_t)(ti_i2c_read_2(sc, I2C_REG_DATA) & 0xff);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (status & I2C_STAT_RDR)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RDR);
|
|
|
|
if (status & I2C_STAT_RRDY)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RRDY);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
} else {
|
|
|
|
/* Write some data. */
|
|
|
|
if (status & I2C_STAT_XDR) {
|
|
|
|
/*
|
|
|
|
* Transmit draining interrupt - FIFO level is below
|
|
|
|
* the set threshold and the amount of data still to
|
|
|
|
* be transferred wont reach the set FIFO threshold.
|
|
|
|
*/
|
|
|
|
ti_i2c_dbg(sc, "Transmit draining interrupt\n");
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* Drain the TX data. Write the pending data in the
|
|
|
|
* FIFO.
|
|
|
|
*/
|
|
|
|
amount = sc->sc_buffer->len - sc->sc_buffer_pos;
|
|
|
|
} else if (status & I2C_STAT_XRDY) {
|
|
|
|
/*
|
|
|
|
* Transmit data ready interrupt - the FIFO level
|
|
|
|
* is below the set threshold.
|
|
|
|
*/
|
|
|
|
ti_i2c_dbg(sc, "Transmit data ready interrupt\n");
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
amount = min(sc->sc_fifo_trsh,
|
|
|
|
sc->sc_buffer->len - sc->sc_buffer_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the bytes from the fifo. */
|
|
|
|
for (i = 0; i < amount; i++)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_DATA,
|
|
|
|
sc->sc_buffer->buf[sc->sc_buffer_pos++]);
|
|
|
|
|
|
|
|
if (status & I2C_STAT_XDR)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XDR);
|
|
|
|
if (status & I2C_STAT_XRDY)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XRDY);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (done);
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ti_i2c_intr - interrupt handler for the I2C module
|
|
|
|
* @dev: i2c device handle
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* Called from timer context
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* EH_HANDLED or EH_NOT_HANDLED
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ti_i2c_intr(void *arg)
|
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
int done;
|
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
uint16_t events, status;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc = (struct ti_i2c_softc *)arg;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
TI_I2C_LOCK(sc);
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
status = ti_i2c_read_2(sc, I2C_REG_STATUS);
|
|
|
|
if (status == 0) {
|
|
|
|
TI_I2C_UNLOCK(sc);
|
|
|
|
return;
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Save enabled interrupts. */
|
|
|
|
events = ti_i2c_read_2(sc, I2C_REG_IRQENABLE_SET);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* We only care about enabled interrupts. */
|
|
|
|
status &= events;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
done = 0;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (sc->sc_buffer != NULL)
|
|
|
|
done = ti_i2c_transfer_intr(sc, status);
|
|
|
|
else {
|
|
|
|
ti_i2c_dbg(sc, "Transfer interrupt without buffer\n");
|
|
|
|
sc->sc_error = EINVAL;
|
|
|
|
done = 1;
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (done)
|
|
|
|
/* Wakeup the process that started the transaction. */
|
|
|
|
wakeup(sc);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
TI_I2C_UNLOCK(sc);
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-20 17:02:37 +00:00
|
|
|
* ti_i2c_transfer - called to perform the transfer
|
|
|
|
* @dev: i2c device handle
|
|
|
|
* @msgs: the messages to send/receive
|
|
|
|
* @nmsgs: the number of messages in the msgs array
|
2012-08-15 06:31:32 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* LOCKING:
|
2014-08-20 17:02:37 +00:00
|
|
|
* Internally locked
|
2012-08-15 06:31:32 +00:00
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* 0 on function succeeded
|
|
|
|
* EINVAL if invalid message is passed as an arg
|
|
|
|
*/
|
|
|
|
static int
|
2014-08-20 17:02:37 +00:00
|
|
|
ti_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
|
2012-08-15 06:31:32 +00:00
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
int err, i, repstart, timeout;
|
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
uint16_t reg;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc = device_get_softc(dev);
|
|
|
|
TI_I2C_LOCK(sc);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* If the controller is busy wait until it is available. */
|
|
|
|
while (sc->sc_bus_inuse == 1)
|
2014-10-18 18:27:24 +00:00
|
|
|
mtx_sleep(sc, &sc->sc_mtx, 0, "i2cbuswait", 0);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Now we have control over the I2C controller. */
|
|
|
|
sc->sc_bus_inuse = 1;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
err = 0;
|
|
|
|
repstart = 0;
|
|
|
|
for (i = 0; i < nmsgs; i++) {
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc->sc_buffer = &msgs[i];
|
|
|
|
sc->sc_buffer_pos = 0;
|
|
|
|
sc->sc_error = 0;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Zero byte transfers aren't allowed. */
|
|
|
|
if (sc->sc_buffer == NULL || sc->sc_buffer->buf == NULL ||
|
|
|
|
sc->sc_buffer->len == 0) {
|
|
|
|
err = EINVAL;
|
2012-08-15 06:31:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Check if the i2c bus is free. */
|
|
|
|
if (repstart == 0) {
|
|
|
|
/*
|
|
|
|
* On repeated start we send the START condition while
|
|
|
|
* the bus _is_ busy.
|
|
|
|
*/
|
|
|
|
timeout = 0;
|
|
|
|
while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
|
|
|
|
if (timeout++ > 100) {
|
|
|
|
err = EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
DELAY(1000);
|
|
|
|
}
|
|
|
|
timeout = 0;
|
|
|
|
} else
|
|
|
|
repstart = 0;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (sc->sc_buffer->flags & IIC_M_NOSTOP)
|
|
|
|
repstart = 1;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Set the slave address. */
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_SA, msgs[i].slave >> 1);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Write the data length. */
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CNT, sc->sc_buffer->len);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Clear the RX and the TX FIFO. */
|
|
|
|
reg = ti_i2c_read_2(sc, I2C_REG_BUF);
|
|
|
|
reg |= I2C_BUF_RXFIFO_CLR | I2C_BUF_TXFIFO_CLR;
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_BUF, reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
reg = sc->sc_con_reg | I2C_CON_STT;
|
|
|
|
if (repstart == 0)
|
|
|
|
reg |= I2C_CON_STP;
|
|
|
|
if ((sc->sc_buffer->flags & IIC_M_RD) == 0)
|
|
|
|
reg |= I2C_CON_TRX;
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Wait for an event. */
|
|
|
|
err = mtx_sleep(sc, &sc->sc_mtx, 0, "i2ciowait", hz);
|
|
|
|
if (err == 0)
|
|
|
|
err = sc->sc_error;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
if (err)
|
|
|
|
break;
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
out:
|
|
|
|
if (timeout == 0) {
|
|
|
|
while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {
|
|
|
|
if (timeout++ > 100)
|
|
|
|
break;
|
|
|
|
DELAY(1000);
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
2014-08-20 17:02:37 +00:00
|
|
|
}
|
|
|
|
/* Put the controller in master mode again. */
|
|
|
|
if ((ti_i2c_read_2(sc, I2C_REG_CON) & I2C_CON_MST) == 0)
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc->sc_buffer = NULL;
|
|
|
|
sc->sc_bus_inuse = 0;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Wake up the processes that are waiting for the bus. */
|
|
|
|
wakeup(sc);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
TI_I2C_UNLOCK(sc);
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-08-20 17:02:37 +00:00
|
|
|
ti_i2c_reset(struct ti_i2c_softc *sc, u_char speed)
|
2012-08-15 06:31:32 +00:00
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
int timeout;
|
|
|
|
struct ti_i2c_clock_config *clkcfg;
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
u_int busfreq;
|
2014-08-20 17:02:37 +00:00
|
|
|
uint16_t fifo_trsh, reg, scll, sclh;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
switch (ti_chip()) {
|
|
|
|
#ifdef SOC_OMAP4
|
|
|
|
case CHIP_OMAP_4:
|
|
|
|
clkcfg = ti_omap4_i2c_clock_configs;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef SOC_TI_AM335X
|
|
|
|
case CHIP_AM335X:
|
|
|
|
clkcfg = ti_am335x_i2c_clock_configs;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
panic("Unknown Ti SoC, unable to reset the i2c");
|
|
|
|
}
|
Allow i2c bus speed to be configured via hints, FDT data, and sysctl.
The current support for controlling i2c bus speed is an inconsistant mess.
There are 4 symbolic speed values defined, UNKNOWN, SLOW, FAST, FASTEST.
It seems to be universally assumed that SLOW means the standard 100KHz
rate from the original spec. Nothing ever calls iicbus_reset() with a
speed of FAST, although some drivers would treat it as the 400KHz standard
speed. Mostly iicbus_reset() is called with the speed set to UNKNOWN or
FASTEST, and there's really no telling what any individual driver will do
with those.
The speed of an i2c bus is limited by the speed of the slowest device on
the bus. This means that generally the bus speed needs to be configured
based on the board/system and the components within it. Historically for
i2c we've configured with device hints. Newer systems use FDT data and it
documents a clock-frequency property for i2c busses. Hobbyists and
developers are likely to want on the fly changes. These changes provide
all 3 methods, but do not require any existing drivers to change to use
the new facilities.
This adds an iicbus method, iicbus_get_frequency(dev, speed) that gets the
frequency for the requested symbolic speed. If the symbolic speed is SLOW
or if there is no speed configured for the bus, the returned value is
100KHz, always. Otherwise, if bus speed is configured by hints, fdt,
tunable, or sysctl, that speed is returned. It also adds a helper
function, iicbus_init_frequency() that any bus driver subclassed from
iicbus can initialize the frequency from some other source of info.
Initial driver implementations are provided for Freescale and TI.
Differential Revision: https://reviews.freebsd.org/D1174
PR: 195009
2014-11-18 01:54:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we haven't attached the bus yet, just init at the default slow
|
|
|
|
* speed. This lets us get the hardware initialized enough to attach
|
|
|
|
* the bus which is where the real speed configuration is handled. After
|
|
|
|
* the bus is attached, get the configured speed from it. Search the
|
|
|
|
* configuration table for the best speed we can do that doesn't exceed
|
|
|
|
* the requested speed.
|
|
|
|
*/
|
|
|
|
if (sc->sc_iicbus == NULL)
|
|
|
|
busfreq = 100000;
|
|
|
|
else
|
|
|
|
busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);
|
|
|
|
for (;;) {
|
|
|
|
if (clkcfg[1].frequency == 0 || clkcfg[1].frequency > busfreq)
|
2014-08-20 17:02:37 +00:00
|
|
|
break;
|
|
|
|
clkcfg++;
|
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* 23.1.4.3 - HS I2C Software Reset
|
|
|
|
* From OMAP4 TRM at page 4068.
|
2012-08-15 06:31:32 +00:00
|
|
|
*
|
2014-08-20 17:02:37 +00:00
|
|
|
* 1. Ensure that the module is disabled.
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
sc->sc_con_reg = 0;
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* 2. Issue a softreset to the controller. */
|
|
|
|
bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, I2C_REG_SYSC_SRST);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* 3. Enable the module.
|
|
|
|
* The I2Ci.I2C_SYSS[0] RDONE bit is asserted only after the module
|
|
|
|
* is enabled by setting the I2Ci.I2C_CON[15] I2C_EN bit to 1.
|
|
|
|
*/
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, I2C_CON_I2C_EN);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* 4. Wait for the software reset to complete. */
|
|
|
|
timeout = 0;
|
|
|
|
while ((ti_i2c_read_2(sc, I2C_REG_SYSS) & I2C_SYSS_RDONE) == 0) {
|
|
|
|
if (timeout++ > 100)
|
2012-08-15 06:31:32 +00:00
|
|
|
return (EBUSY);
|
|
|
|
DELAY(100);
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* Disable the I2C controller once again, now that the reset has
|
|
|
|
* finished.
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* The following sequence is taken from the OMAP4 TRM at page 4077.
|
|
|
|
*
|
|
|
|
* 1. Enable the functional and interface clocks (see Section
|
|
|
|
* 23.1.5.1.1.1.1). Done at ti_i2c_activate().
|
|
|
|
*
|
|
|
|
* 2. Program the prescaler to obtain an approximately 12MHz internal
|
|
|
|
* sampling clock (I2Ci_INTERNAL_CLK) by programming the
|
|
|
|
* corresponding value in the I2Ci.I2C_PSC[3:0] PSC field.
|
|
|
|
* This value depends on the frequency of the functional clock
|
|
|
|
* (I2Ci_FCLK). Because this frequency is 96MHz, the
|
|
|
|
* I2Ci.I2C_PSC[7:0] PSC field value is 0x7.
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
ti_i2c_write_2(sc, I2C_REG_PSC, clkcfg->psc);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* 3. Program the I2Ci.I2C_SCLL[7:0] SCLL and I2Ci.I2C_SCLH[7:0] SCLH
|
|
|
|
* bit fields to obtain a bit rate of 100 Kbps, 400 Kbps or 1Mbps.
|
|
|
|
* These values depend on the internal sampling clock frequency
|
|
|
|
* (see Table 23-8).
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
scll = clkcfg->scll & I2C_SCLL_MASK;
|
|
|
|
sclh = clkcfg->sclh & I2C_SCLH_MASK;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* 4. (Optional) Program the I2Ci.I2C_SCLL[15:8] HSSCLL and
|
|
|
|
* I2Ci.I2C_SCLH[15:8] HSSCLH fields to obtain a bit rate of
|
|
|
|
* 400K bps or 3.4M bps (for the second phase of HS mode). These
|
|
|
|
* values depend on the internal sampling clock frequency (see
|
|
|
|
* Table 23-8).
|
2012-08-15 06:31:32 +00:00
|
|
|
*
|
|
|
|
* 5. (Optional) If a bit rate of 3.4M bps is used and the bus line
|
2014-08-20 17:02:37 +00:00
|
|
|
* capacitance exceeds 45 pF, (see Section 18.4.8, PAD Functional
|
|
|
|
* Multiplexing and Configuration).
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
switch (ti_chip()) {
|
|
|
|
#ifdef SOC_OMAP4
|
|
|
|
case CHIP_OMAP_4:
|
|
|
|
if ((clkcfg->hsscll + clkcfg->hssclh) > 0) {
|
|
|
|
scll |= clkcfg->hsscll << I2C_HSSCLL_SHIFT;
|
|
|
|
sclh |= clkcfg->hssclh << I2C_HSSCLH_SHIFT;
|
|
|
|
sc->sc_con_reg |= I2C_CON_OPMODE_HS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Write the selected bit rate. */
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_SCLL, scll);
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_SCLH, sclh);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/*
|
|
|
|
* 6. Configure the Own Address of the I2C controller by storing it in
|
|
|
|
* the I2Ci.I2C_OA0 register. Up to four Own Addresses can be
|
|
|
|
* programmed in the I2Ci.I2C_OAi registers (where i = 0, 1, 2, 3)
|
|
|
|
* for each I2C controller.
|
|
|
|
*
|
|
|
|
* Note: For a 10-bit address, set the corresponding expand Own Address
|
|
|
|
* bit in the I2Ci.I2C_CON register.
|
|
|
|
*
|
|
|
|
* Driver currently always in single master mode so ignore this step.
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-08-20 17:02:37 +00:00
|
|
|
* 7. Set the TX threshold (in transmitter mode) and the RX threshold
|
|
|
|
* (in receiver mode) by setting the I2Ci.I2C_BUF[5:0]XTRSH field to
|
|
|
|
* (TX threshold - 1) and the I2Ci.I2C_BUF[13:8]RTRSH field to (RX
|
|
|
|
* threshold - 1), where the TX and RX thresholds are greater than
|
|
|
|
* or equal to 1.
|
|
|
|
*
|
|
|
|
* The threshold is set to 5 for now.
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
2014-08-20 17:02:37 +00:00
|
|
|
fifo_trsh = (sc->sc_fifo_trsh - 1) & I2C_BUF_TRSH_MASK;
|
|
|
|
reg = fifo_trsh | (fifo_trsh << I2C_BUF_RXTRSH_SHIFT);
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_BUF, reg);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
/*
|
2014-08-20 17:02:37 +00:00
|
|
|
* 8. Take the I2C controller out of reset by setting the
|
|
|
|
* I2Ci.I2C_CON[15] I2C_EN bit to 1.
|
|
|
|
*
|
|
|
|
* 23.1.5.1.1.1.2 - Initialize the I2C Controller
|
|
|
|
*
|
2012-08-15 06:31:32 +00:00
|
|
|
* To initialize the I2C controller, perform the following steps:
|
|
|
|
*
|
|
|
|
* 1. Configure the I2Ci.I2C_CON register:
|
2014-08-20 17:02:37 +00:00
|
|
|
* . For master or slave mode, set the I2Ci.I2C_CON[10] MST bit
|
|
|
|
* (0: slave, 1: master).
|
|
|
|
* . For transmitter or receiver mode, set the I2Ci.I2C_CON[9] TRX
|
|
|
|
* bit (0: receiver, 1: transmitter).
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Enable the I2C controller in master mode. */
|
|
|
|
sc->sc_con_reg |= I2C_CON_I2C_EN | I2C_CON_MST;
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 2. If using an interrupt to transmit/receive data, set the
|
2012-08-15 06:31:32 +00:00
|
|
|
* corresponding bit in the I2Ci.I2C_IE register (the I2Ci.I2C_IE[4]
|
2014-08-20 17:02:37 +00:00
|
|
|
* XRDY_IE bit for the transmit interrupt, the I2Ci.I2C_IE[3] RRDY
|
|
|
|
* bit for the receive interrupt).
|
2012-08-15 06:31:32 +00:00
|
|
|
*/
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Set the interrupts we want to be notified. */
|
|
|
|
reg = I2C_IE_XDR | /* Transmit draining interrupt. */
|
|
|
|
I2C_IE_XRDY | /* Transmit Data Ready interrupt. */
|
|
|
|
I2C_IE_RDR | /* Receive draining interrupt. */
|
|
|
|
I2C_IE_RRDY | /* Receive Data Ready interrupt. */
|
|
|
|
I2C_IE_ARDY | /* Register Access Ready interrupt. */
|
|
|
|
I2C_IE_NACK | /* No Acknowledgment interrupt. */
|
|
|
|
I2C_IE_AL; /* Arbitration lost interrupt. */
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Enable the interrupts. */
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_IRQENABLE_SET, reg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 3. If using DMA to receive/transmit data, set to 1 the corresponding
|
|
|
|
* bit in the I2Ci.I2C_BUF register (the I2Ci.I2C_BUF[15] RDMA_EN
|
|
|
|
* bit for the receive DMA channel, the I2Ci.I2C_BUF[7] XDMA_EN bit
|
|
|
|
* for the transmit DMA channel).
|
|
|
|
*
|
|
|
|
* Not using DMA for now, so ignore this.
|
|
|
|
*/
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
static int
|
|
|
|
ti_i2c_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
|
|
|
|
{
|
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
TI_I2C_LOCK(sc);
|
|
|
|
err = ti_i2c_reset(sc, speed);
|
|
|
|
TI_I2C_UNLOCK(sc);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
|
|
|
|
return (IIC_ENOADDR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ti_i2c_activate(device_t dev)
|
|
|
|
{
|
|
|
|
clk_ident_t clk;
|
|
|
|
int err;
|
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
|
|
|
|
sc = (struct ti_i2c_softc*)device_get_softc(dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 1. Enable the functional and interface clocks (see Section
|
|
|
|
* 23.1.5.1.1.1.1).
|
|
|
|
*/
|
|
|
|
clk = I2C0_CLK + sc->device_id;
|
|
|
|
err = ti_prcm_clk_enable(clk);
|
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
|
|
|
|
return (ti_i2c_reset(sc, IIC_UNKNOWN));
|
|
|
|
}
|
|
|
|
|
2012-08-15 06:31:32 +00:00
|
|
|
/**
|
|
|
|
* ti_i2c_deactivate - deactivates the controller and releases resources
|
|
|
|
* @dev: i2c device handle
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* LOCKING:
|
|
|
|
* Assumed called in an atomic context.
|
|
|
|
*
|
|
|
|
* RETURNS:
|
|
|
|
* nothing
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ti_i2c_deactivate(device_t dev)
|
|
|
|
{
|
|
|
|
struct ti_i2c_softc *sc = device_get_softc(dev);
|
|
|
|
clk_ident_t clk;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Disable the controller - cancel all transactions. */
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_IRQENABLE_CLR, 0xffff);
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_STATUS, 0xffff);
|
|
|
|
ti_i2c_write_2(sc, I2C_REG_CON, 0);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Release the interrupt handler. */
|
|
|
|
if (sc->sc_irq_h != NULL) {
|
2012-08-15 06:31:32 +00:00
|
|
|
bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);
|
2014-08-20 17:02:37 +00:00
|
|
|
sc->sc_irq_h = NULL;
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bus_generic_detach(sc->sc_dev);
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Unmap the I2C controller registers. */
|
|
|
|
if (sc->sc_mem_res != NULL) {
|
|
|
|
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
|
2012-08-15 06:31:32 +00:00
|
|
|
sc->sc_mem_res = NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Release the IRQ resource. */
|
2012-08-15 06:31:32 +00:00
|
|
|
if (sc->sc_irq_res != NULL) {
|
2014-08-20 17:02:37 +00:00
|
|
|
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
|
2012-08-15 06:31:32 +00:00
|
|
|
sc->sc_irq_res = NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Finally disable the functional and interface clocks. */
|
2012-08-15 06:31:32 +00:00
|
|
|
clk = I2C0_CLK + sc->device_id;
|
|
|
|
ti_prcm_clk_disable(clk);
|
2014-08-20 17:02:37 +00:00
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
static int
|
|
|
|
ti_i2c_sysctl_clk(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
device_t dev;
|
|
|
|
int clk, psc, sclh, scll;
|
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
|
|
|
|
dev = (device_t)arg1;
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
|
|
|
|
TI_I2C_LOCK(sc);
|
|
|
|
/* Get the system prescaler value. */
|
|
|
|
psc = (int)ti_i2c_read_2(sc, I2C_REG_PSC) + 1;
|
|
|
|
|
|
|
|
/* Get the bitrate. */
|
|
|
|
scll = (int)ti_i2c_read_2(sc, I2C_REG_SCLL) & I2C_SCLL_MASK;
|
|
|
|
sclh = (int)ti_i2c_read_2(sc, I2C_REG_SCLH) & I2C_SCLH_MASK;
|
|
|
|
|
|
|
|
clk = I2C_CLK / psc / (scll + 7 + sclh + 5);
|
|
|
|
TI_I2C_UNLOCK(sc);
|
|
|
|
|
|
|
|
return (sysctl_handle_int(oidp, &clk, 0, req));
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ti_i2c_probe(device_t dev)
|
|
|
|
{
|
2014-02-02 19:17:28 +00:00
|
|
|
|
|
|
|
if (!ofw_bus_status_okay(dev))
|
|
|
|
return (ENXIO);
|
2012-08-15 06:31:32 +00:00
|
|
|
if (!ofw_bus_is_compatible(dev, "ti,i2c"))
|
|
|
|
return (ENXIO);
|
|
|
|
device_set_desc(dev, "TI I2C Controller");
|
2014-08-20 17:02:37 +00:00
|
|
|
|
2012-08-15 06:31:32 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ti_i2c_attach(device_t dev)
|
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
int err, rid;
|
2012-08-15 06:31:32 +00:00
|
|
|
phandle_t node;
|
2014-08-20 17:02:37 +00:00
|
|
|
struct ti_i2c_softc *sc;
|
|
|
|
struct sysctl_ctx_list *ctx;
|
|
|
|
struct sysctl_oid_list *tree;
|
|
|
|
uint16_t fifosz;
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc = device_get_softc(dev);
|
2012-08-15 06:31:32 +00:00
|
|
|
sc->sc_dev = dev;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Get the i2c device id from FDT. */
|
2012-08-15 06:31:32 +00:00
|
|
|
node = ofw_bus_get_node(dev);
|
2014-08-20 17:02:37 +00:00
|
|
|
if ((OF_getencprop(node, "i2c-device-id", &sc->device_id,
|
|
|
|
sizeof(sc->device_id))) <= 0) {
|
2012-08-15 06:31:32 +00:00
|
|
|
device_printf(dev, "missing i2c-device-id attribute in FDT\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Get the memory resource for the register mapping. */
|
2012-08-15 06:31:32 +00:00
|
|
|
rid = 0;
|
|
|
|
sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
|
2014-08-20 17:02:37 +00:00
|
|
|
RF_ACTIVE);
|
|
|
|
if (sc->sc_mem_res == NULL) {
|
|
|
|
device_printf(dev, "Cannot map registers.\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Allocate our IRQ resource. */
|
2012-08-15 06:31:32 +00:00
|
|
|
rid = 0;
|
|
|
|
sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
|
2014-08-20 17:02:37 +00:00
|
|
|
RF_ACTIVE | RF_SHAREABLE);
|
2012-08-15 06:31:32 +00:00
|
|
|
if (sc->sc_irq_res == NULL) {
|
2014-08-20 17:02:37 +00:00
|
|
|
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
|
|
|
|
device_printf(dev, "Cannot allocate interrupt.\n");
|
|
|
|
return (ENXIO);
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
TI_I2C_LOCK_INIT(sc);
|
|
|
|
|
|
|
|
/* First of all, we _must_ activate the H/W. */
|
2012-08-15 06:31:32 +00:00
|
|
|
err = ti_i2c_activate(dev);
|
|
|
|
if (err) {
|
|
|
|
device_printf(dev, "ti_i2c_activate failed\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-12-09 11:51:17 +00:00
|
|
|
/* Read the version number of the I2C module */
|
|
|
|
sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Get the fifo size. */
|
|
|
|
fifosz = ti_i2c_read_2(sc, I2C_REG_BUFSTAT);
|
|
|
|
fifosz >>= I2C_BUFSTAT_FIFODEPTH_SHIFT;
|
|
|
|
fifosz &= I2C_BUFSTAT_FIFODEPTH_MASK;
|
|
|
|
|
|
|
|
device_printf(dev, "I2C revision %d.%d FIFO size: %d bytes\n",
|
|
|
|
sc->sc_rev >> 4, sc->sc_rev & 0xf, 8 << fifosz);
|
|
|
|
|
|
|
|
/* Set the FIFO threshold to 5 for now. */
|
|
|
|
sc->sc_fifo_trsh = 5;
|
|
|
|
|
|
|
|
ctx = device_get_sysctl_ctx(dev);
|
|
|
|
tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
|
|
|
|
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_clock",
|
|
|
|
CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_MPSAFE, dev, 0,
|
|
|
|
ti_i2c_sysctl_clk, "IU", "I2C bus clock");
|
2013-12-09 11:51:17 +00:00
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Activate the interrupt. */
|
2012-08-15 06:31:32 +00:00
|
|
|
err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
|
2014-08-20 17:02:37 +00:00
|
|
|
NULL, ti_i2c_intr, sc, &sc->sc_irq_h);
|
2012-08-15 06:31:32 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
/* Attach the iicbus. */
|
|
|
|
if ((sc->sc_iicbus = device_add_child(dev, "iicbus", -1)) == NULL) {
|
2012-08-15 06:31:32 +00:00
|
|
|
device_printf(dev, "could not allocate iicbus instance\n");
|
2014-08-20 17:02:37 +00:00
|
|
|
err = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
/* Probe and attach the iicbus */
|
|
|
|
bus_generic_attach(dev);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (err) {
|
|
|
|
ti_i2c_deactivate(dev);
|
|
|
|
TI_I2C_LOCK_DESTROY(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ti_i2c_detach(device_t dev)
|
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
struct ti_i2c_softc *sc;
|
2012-08-15 06:31:32 +00:00
|
|
|
int rv;
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
sc = device_get_softc(dev);
|
2012-08-15 06:31:32 +00:00
|
|
|
ti_i2c_deactivate(dev);
|
|
|
|
TI_I2C_LOCK_DESTROY(sc);
|
2014-08-20 17:02:37 +00:00
|
|
|
if (sc->sc_iicbus &&
|
|
|
|
(rv = device_delete_child(dev, sc->sc_iicbus)) != 0)
|
|
|
|
return (rv);
|
2012-08-15 06:31:32 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static phandle_t
|
|
|
|
ti_i2c_get_node(device_t bus, device_t dev)
|
|
|
|
{
|
2014-08-20 17:02:37 +00:00
|
|
|
|
|
|
|
/* Share controller node with iibus device. */
|
|
|
|
return (ofw_bus_get_node(bus));
|
2012-08-15 06:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static device_method_t ti_i2c_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, ti_i2c_probe),
|
|
|
|
DEVMETHOD(device_attach, ti_i2c_attach),
|
|
|
|
DEVMETHOD(device_detach, ti_i2c_detach),
|
|
|
|
|
|
|
|
/* OFW methods */
|
|
|
|
DEVMETHOD(ofw_bus_get_node, ti_i2c_get_node),
|
|
|
|
|
|
|
|
/* iicbus interface */
|
2014-12-27 20:06:16 +00:00
|
|
|
DEVMETHOD(iicbus_callback, iicbus_null_callback),
|
2014-08-20 17:02:37 +00:00
|
|
|
DEVMETHOD(iicbus_reset, ti_i2c_iicbus_reset),
|
2012-08-15 06:31:32 +00:00
|
|
|
DEVMETHOD(iicbus_transfer, ti_i2c_transfer),
|
2014-08-20 17:02:37 +00:00
|
|
|
|
|
|
|
DEVMETHOD_END
|
2012-08-15 06:31:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static driver_t ti_i2c_driver = {
|
|
|
|
"iichb",
|
|
|
|
ti_i2c_methods,
|
|
|
|
sizeof(struct ti_i2c_softc),
|
|
|
|
};
|
|
|
|
|
2014-08-20 17:02:37 +00:00
|
|
|
static devclass_t ti_i2c_devclass;
|
|
|
|
|
2012-08-15 06:31:32 +00:00
|
|
|
DRIVER_MODULE(ti_iic, simplebus, ti_i2c_driver, ti_i2c_devclass, 0, 0);
|
|
|
|
DRIVER_MODULE(iicbus, ti_iic, iicbus_driver, iicbus_devclass, 0, 0);
|
|
|
|
|
|
|
|
MODULE_DEPEND(ti_iic, ti_prcm, 1, 1, 1);
|
|
|
|
MODULE_DEPEND(ti_iic, iicbus, 1, 1, 1);
|