/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2011 Ben Gray <[email protected]>.4* Copyright (c) 2014 Luiz Otavio O Souza <[email protected]>.5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/**30* Driver for the I2C module on the TI SoC.31*32* This driver is heavily based on the TWI driver for the AT91 (at91_twi.c).33*34* CAUTION: The I2Ci registers are limited to 16 bit and 8 bit data accesses,35* 32 bit data access is not allowed and can corrupt register content.36*37* This driver currently doesn't use DMA for the transfer, although I hope to38* incorporate that sometime in the future. The idea being that for transaction39* larger than a certain size the DMA engine is used, for anything less the40* normal interrupt/fifo driven option is used.41*/4243#include <sys/param.h>44#include <sys/systm.h>45#include <sys/bus.h>46#include <sys/conf.h>47#include <sys/kernel.h>48#include <sys/lock.h>49#include <sys/mbuf.h>50#include <sys/malloc.h>51#include <sys/module.h>52#include <sys/mutex.h>53#include <sys/rman.h>54#include <sys/sysctl.h>55#include <machine/bus.h>5657#include <dev/ofw/openfirm.h>58#include <dev/ofw/ofw_bus.h>59#include <dev/ofw/ofw_bus_subr.h>6061#include <arm/ti/ti_cpuid.h>62#include <arm/ti/ti_sysc.h>63#include <arm/ti/ti_i2c.h>6465#include <dev/iicbus/iiconf.h>66#include <dev/iicbus/iicbus.h>6768#include "iicbus_if.h"6970/**71* I2C device driver context, a pointer to this is stored in the device72* driver structure.73*/74struct ti_i2c_softc75{76device_t sc_dev;77struct resource* sc_irq_res;78struct resource* sc_mem_res;79device_t sc_iicbus;8081void* sc_irq_h;8283struct mtx sc_mtx;8485struct iic_msg* sc_buffer;86int sc_bus_inuse;87int sc_buffer_pos;88int sc_error;89int sc_fifo_trsh;90int sc_timeout;9192uint16_t sc_con_reg;93uint16_t sc_rev;94};9596struct ti_i2c_clock_config97{98u_int frequency; /* Bus frequency in Hz */99uint8_t psc; /* Fast/Standard mode prescale divider */100uint8_t scll; /* Fast/Standard mode SCL low time */101uint8_t sclh; /* Fast/Standard mode SCL high time */102uint8_t hsscll; /* High Speed mode SCL low time */103uint8_t hssclh; /* High Speed mode SCL high time */104};105106#if defined(SOC_TI_AM335X)107/*108* AM335x i2c bus clock is 48MHZ / ((psc + 1) * (scll + 7 + sclh + 5))109* In all cases we prescale the clock to 24MHz as recommended in the manual.110*/111static struct ti_i2c_clock_config ti_am335x_i2c_clock_configs[] = {112{ 100000, 1, 111, 117, 0, 0},113{ 400000, 1, 23, 25, 0, 0},114{ 1000000, 1, 5, 7, 0, 0},115{ 0 /* Table terminator */ }116};117#endif118119/**120* Locking macros used throughout the driver121*/122#define TI_I2C_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)123#define TI_I2C_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)124#define TI_I2C_LOCK_INIT(_sc) \125mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \126"ti_i2c", MTX_DEF)127#define TI_I2C_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx)128#define TI_I2C_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)129#define TI_I2C_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED)130131#ifdef DEBUG132#define ti_i2c_dbg(_sc, fmt, args...) \133device_printf((_sc)->sc_dev, fmt, ##args)134#else135#define ti_i2c_dbg(_sc, fmt, args...)136#endif137138/**139* ti_i2c_read_2 - reads a 16-bit value from one of the I2C registers140* @sc: I2C device context141* @off: the byte offset within the register bank to read from.142*143*144* LOCKING:145* No locking required146*147* RETURNS:148* 16-bit value read from the register.149*/150static inline uint16_t151ti_i2c_read_2(struct ti_i2c_softc *sc, bus_size_t off)152{153154return (bus_read_2(sc->sc_mem_res, off));155}156157/**158* ti_i2c_write_2 - writes a 16-bit value to one of the I2C registers159* @sc: I2C device context160* @off: the byte offset within the register bank to read from.161* @val: the value to write into the register162*163* LOCKING:164* No locking required165*166* RETURNS:167* 16-bit value read from the register.168*/169static inline void170ti_i2c_write_2(struct ti_i2c_softc *sc, bus_size_t off, uint16_t val)171{172173bus_write_2(sc->sc_mem_res, off, val);174}175176static int177ti_i2c_transfer_intr(struct ti_i2c_softc* sc, uint16_t status)178{179int amount, done, i;180181done = 0;182amount = 0;183/* Check for the error conditions. */184if (status & I2C_STAT_NACK) {185/* No ACK from slave. */186ti_i2c_dbg(sc, "NACK\n");187ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_NACK);188sc->sc_error = ENXIO;189} else if (status & I2C_STAT_AL) {190/* Arbitration lost. */191ti_i2c_dbg(sc, "Arbitration lost\n");192ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_AL);193sc->sc_error = ENXIO;194}195196/* Check if we have finished. */197if (status & I2C_STAT_ARDY) {198/* Register access ready - transaction complete basically. */199ti_i2c_dbg(sc, "ARDY transaction complete\n");200if (sc->sc_error != 0 && sc->sc_buffer->flags & IIC_M_NOSTOP) {201ti_i2c_write_2(sc, I2C_REG_CON,202sc->sc_con_reg | I2C_CON_STP);203}204ti_i2c_write_2(sc, I2C_REG_STATUS,205I2C_STAT_ARDY | I2C_STAT_RDR | I2C_STAT_RRDY |206I2C_STAT_XDR | I2C_STAT_XRDY);207return (1);208}209210if (sc->sc_buffer->flags & IIC_M_RD) {211/* Read some data. */212if (status & I2C_STAT_RDR) {213/*214* Receive draining interrupt - last data received.215* The set FIFO threshold won't be reached to trigger216* RRDY.217*/218ti_i2c_dbg(sc, "Receive draining interrupt\n");219220/*221* Drain the FIFO. Read the pending data in the FIFO.222*/223amount = sc->sc_buffer->len - sc->sc_buffer_pos;224} else if (status & I2C_STAT_RRDY) {225/*226* Receive data ready interrupt - FIFO has reached the227* set threshold.228*/229ti_i2c_dbg(sc, "Receive data ready interrupt\n");230231amount = min(sc->sc_fifo_trsh,232sc->sc_buffer->len - sc->sc_buffer_pos);233}234235/* Read the bytes from the fifo. */236for (i = 0; i < amount; i++)237sc->sc_buffer->buf[sc->sc_buffer_pos++] =238(uint8_t)(ti_i2c_read_2(sc, I2C_REG_DATA) & 0xff);239240if (status & I2C_STAT_RDR)241ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RDR);242if (status & I2C_STAT_RRDY)243ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_RRDY);244245} else {246/* Write some data. */247if (status & I2C_STAT_XDR) {248/*249* Transmit draining interrupt - FIFO level is below250* the set threshold and the amount of data still to251* be transferred won't reach the set FIFO threshold.252*/253ti_i2c_dbg(sc, "Transmit draining interrupt\n");254255/*256* Drain the TX data. Write the pending data in the257* FIFO.258*/259amount = sc->sc_buffer->len - sc->sc_buffer_pos;260} else if (status & I2C_STAT_XRDY) {261/*262* Transmit data ready interrupt - the FIFO level263* is below the set threshold.264*/265ti_i2c_dbg(sc, "Transmit data ready interrupt\n");266267amount = min(sc->sc_fifo_trsh,268sc->sc_buffer->len - sc->sc_buffer_pos);269}270271/* Write the bytes from the fifo. */272for (i = 0; i < amount; i++)273ti_i2c_write_2(sc, I2C_REG_DATA,274sc->sc_buffer->buf[sc->sc_buffer_pos++]);275276if (status & I2C_STAT_XDR)277ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XDR);278if (status & I2C_STAT_XRDY)279ti_i2c_write_2(sc, I2C_REG_STATUS, I2C_STAT_XRDY);280}281282return (done);283}284285/**286* ti_i2c_intr - interrupt handler for the I2C module287* @dev: i2c device handle288*289*290*291* LOCKING:292* Called from timer context293*294* RETURNS:295* EH_HANDLED or EH_NOT_HANDLED296*/297static void298ti_i2c_intr(void *arg)299{300int done;301struct ti_i2c_softc *sc;302uint16_t events, status;303304sc = (struct ti_i2c_softc *)arg;305306TI_I2C_LOCK(sc);307308status = ti_i2c_read_2(sc, I2C_REG_STATUS);309if (status == 0) {310TI_I2C_UNLOCK(sc);311return;312}313314/* Save enabled interrupts. */315events = ti_i2c_read_2(sc, I2C_REG_IRQENABLE_SET);316317/* We only care about enabled interrupts. */318status &= events;319320done = 0;321322if (sc->sc_buffer != NULL)323done = ti_i2c_transfer_intr(sc, status);324else {325ti_i2c_dbg(sc, "Transfer interrupt without buffer\n");326sc->sc_error = EINVAL;327done = 1;328}329330if (done)331/* Wakeup the process that started the transaction. */332wakeup(sc);333334TI_I2C_UNLOCK(sc);335}336337/**338* ti_i2c_transfer - called to perform the transfer339* @dev: i2c device handle340* @msgs: the messages to send/receive341* @nmsgs: the number of messages in the msgs array342*343*344* LOCKING:345* Internally locked346*347* RETURNS:348* 0 on function succeeded349* EINVAL if invalid message is passed as an arg350*/351static int352ti_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)353{354int err, i, repstart, timeout;355struct ti_i2c_softc *sc;356uint16_t reg;357358sc = device_get_softc(dev);359TI_I2C_LOCK(sc);360361/* If the controller is busy wait until it is available. */362while (sc->sc_bus_inuse == 1)363mtx_sleep(sc, &sc->sc_mtx, 0, "i2cbuswait", 0);364365/* Now we have control over the I2C controller. */366sc->sc_bus_inuse = 1;367368err = 0;369repstart = 0;370for (i = 0; i < nmsgs; i++) {371sc->sc_buffer = &msgs[i];372sc->sc_buffer_pos = 0;373sc->sc_error = 0;374375/* Zero byte transfers aren't allowed. */376if (sc->sc_buffer == NULL || sc->sc_buffer->buf == NULL ||377sc->sc_buffer->len == 0) {378err = EINVAL;379break;380}381382/* Check if the i2c bus is free. */383if (repstart == 0) {384/*385* On repeated start we send the START condition while386* the bus _is_ busy.387*/388timeout = 0;389while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {390if (timeout++ > 100) {391err = EBUSY;392goto out;393}394DELAY(1000);395}396timeout = 0;397} else398repstart = 0;399400if (sc->sc_buffer->flags & IIC_M_NOSTOP)401repstart = 1;402403/* Set the slave address. */404ti_i2c_write_2(sc, I2C_REG_SA, msgs[i].slave >> 1);405406/* Write the data length. */407ti_i2c_write_2(sc, I2C_REG_CNT, sc->sc_buffer->len);408409/* Clear the RX and the TX FIFO. */410reg = ti_i2c_read_2(sc, I2C_REG_BUF);411reg |= I2C_BUF_RXFIFO_CLR | I2C_BUF_TXFIFO_CLR;412ti_i2c_write_2(sc, I2C_REG_BUF, reg);413414reg = sc->sc_con_reg | I2C_CON_STT;415if (repstart == 0)416reg |= I2C_CON_STP;417if ((sc->sc_buffer->flags & IIC_M_RD) == 0)418reg |= I2C_CON_TRX;419ti_i2c_write_2(sc, I2C_REG_CON, reg);420421/* Wait for an event. */422err = mtx_sleep(sc, &sc->sc_mtx, 0, "i2ciowait", sc->sc_timeout);423if (err == 0)424err = sc->sc_error;425426if (err)427break;428}429430out:431if (timeout == 0) {432while (ti_i2c_read_2(sc, I2C_REG_STATUS_RAW) & I2C_STAT_BB) {433if (timeout++ > 100)434break;435DELAY(1000);436}437}438/* Put the controller in master mode again. */439if ((ti_i2c_read_2(sc, I2C_REG_CON) & I2C_CON_MST) == 0)440ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);441442sc->sc_buffer = NULL;443sc->sc_bus_inuse = 0;444445/* Wake up the processes that are waiting for the bus. */446wakeup(sc);447448TI_I2C_UNLOCK(sc);449450return (err);451}452453static int454ti_i2c_reset(struct ti_i2c_softc *sc, u_char speed)455{456int timeout;457struct ti_i2c_clock_config *clkcfg;458u_int busfreq;459uint16_t fifo_trsh, reg, scll, sclh;460461switch (ti_chip()) {462#ifdef SOC_TI_AM335X463case CHIP_AM335X:464clkcfg = ti_am335x_i2c_clock_configs;465break;466#endif467default:468panic("Unknown TI SoC, unable to reset the i2c");469}470471/*472* If we haven't attached the bus yet, just init at the default slow473* speed. This lets us get the hardware initialized enough to attach474* the bus which is where the real speed configuration is handled. After475* the bus is attached, get the configured speed from it. Search the476* configuration table for the best speed we can do that doesn't exceed477* the requested speed.478*/479if (sc->sc_iicbus == NULL)480busfreq = 100000;481else482busfreq = IICBUS_GET_FREQUENCY(sc->sc_iicbus, speed);483for (;;) {484if (clkcfg[1].frequency == 0 || clkcfg[1].frequency > busfreq)485break;486clkcfg++;487}488489/*490* 23.1.4.3 - HS I2C Software Reset491* From OMAP4 TRM at page 4068.492*493* 1. Ensure that the module is disabled.494*/495sc->sc_con_reg = 0;496ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);497498/* 2. Issue a softreset to the controller. */499bus_write_2(sc->sc_mem_res, I2C_REG_SYSC, I2C_REG_SYSC_SRST);500501/*502* 3. Enable the module.503* The I2Ci.I2C_SYSS[0] RDONE bit is asserted only after the module504* is enabled by setting the I2Ci.I2C_CON[15] I2C_EN bit to 1.505*/506ti_i2c_write_2(sc, I2C_REG_CON, I2C_CON_I2C_EN);507508/* 4. Wait for the software reset to complete. */509timeout = 0;510while ((ti_i2c_read_2(sc, I2C_REG_SYSS) & I2C_SYSS_RDONE) == 0) {511if (timeout++ > 100)512return (EBUSY);513DELAY(100);514}515516/*517* Disable the I2C controller once again, now that the reset has518* finished.519*/520ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);521522/*523* The following sequence is taken from the OMAP4 TRM at page 4077.524*525* 1. Enable the functional and interface clocks (see Section526* 23.1.5.1.1.1.1). Done at ti_i2c_activate().527*528* 2. Program the prescaler to obtain an approximately 12MHz internal529* sampling clock (I2Ci_INTERNAL_CLK) by programming the530* corresponding value in the I2Ci.I2C_PSC[3:0] PSC field.531* This value depends on the frequency of the functional clock532* (I2Ci_FCLK). Because this frequency is 96MHz, the533* I2Ci.I2C_PSC[7:0] PSC field value is 0x7.534*/535ti_i2c_write_2(sc, I2C_REG_PSC, clkcfg->psc);536537/*538* 3. Program the I2Ci.I2C_SCLL[7:0] SCLL and I2Ci.I2C_SCLH[7:0] SCLH539* bit fields to obtain a bit rate of 100 Kbps, 400 Kbps or 1Mbps.540* These values depend on the internal sampling clock frequency541* (see Table 23-8).542*/543scll = clkcfg->scll & I2C_SCLL_MASK;544sclh = clkcfg->sclh & I2C_SCLH_MASK;545546/*547* 4. (Optional) Program the I2Ci.I2C_SCLL[15:8] HSSCLL and548* I2Ci.I2C_SCLH[15:8] HSSCLH fields to obtain a bit rate of549* 400K bps or 3.4M bps (for the second phase of HS mode). These550* values depend on the internal sampling clock frequency (see551* Table 23-8).552*553* 5. (Optional) If a bit rate of 3.4M bps is used and the bus line554* capacitance exceeds 45 pF, (see Section 18.4.8, PAD Functional555* Multiplexing and Configuration).556*/557558/* Write the selected bit rate. */559ti_i2c_write_2(sc, I2C_REG_SCLL, scll);560ti_i2c_write_2(sc, I2C_REG_SCLH, sclh);561562/*563* 6. Configure the Own Address of the I2C controller by storing it in564* the I2Ci.I2C_OA0 register. Up to four Own Addresses can be565* programmed in the I2Ci.I2C_OAi registers (where i = 0, 1, 2, 3)566* for each I2C controller.567*568* Note: For a 10-bit address, set the corresponding expand Own Address569* bit in the I2Ci.I2C_CON register.570*571* Driver currently always in single master mode so ignore this step.572*/573574/*575* 7. Set the TX threshold (in transmitter mode) and the RX threshold576* (in receiver mode) by setting the I2Ci.I2C_BUF[5:0]XTRSH field to577* (TX threshold - 1) and the I2Ci.I2C_BUF[13:8]RTRSH field to (RX578* threshold - 1), where the TX and RX thresholds are greater than579* or equal to 1.580*581* The threshold is set to 5 for now.582*/583fifo_trsh = (sc->sc_fifo_trsh - 1) & I2C_BUF_TRSH_MASK;584reg = fifo_trsh | (fifo_trsh << I2C_BUF_RXTRSH_SHIFT);585ti_i2c_write_2(sc, I2C_REG_BUF, reg);586587/*588* 8. Take the I2C controller out of reset by setting the589* I2Ci.I2C_CON[15] I2C_EN bit to 1.590*591* 23.1.5.1.1.1.2 - Initialize the I2C Controller592*593* To initialize the I2C controller, perform the following steps:594*595* 1. Configure the I2Ci.I2C_CON register:596* . For master or slave mode, set the I2Ci.I2C_CON[10] MST bit597* (0: slave, 1: master).598* . For transmitter or receiver mode, set the I2Ci.I2C_CON[9] TRX599* bit (0: receiver, 1: transmitter).600*/601602/* Enable the I2C controller in master mode. */603sc->sc_con_reg |= I2C_CON_I2C_EN | I2C_CON_MST;604ti_i2c_write_2(sc, I2C_REG_CON, sc->sc_con_reg);605606/*607* 2. If using an interrupt to transmit/receive data, set the608* corresponding bit in the I2Ci.I2C_IE register (the I2Ci.I2C_IE[4]609* XRDY_IE bit for the transmit interrupt, the I2Ci.I2C_IE[3] RRDY610* bit for the receive interrupt).611*/612613/* Set the interrupts we want to be notified. */614reg = I2C_IE_XDR | /* Transmit draining interrupt. */615I2C_IE_XRDY | /* Transmit Data Ready interrupt. */616I2C_IE_RDR | /* Receive draining interrupt. */617I2C_IE_RRDY | /* Receive Data Ready interrupt. */618I2C_IE_ARDY | /* Register Access Ready interrupt. */619I2C_IE_NACK | /* No Acknowledgment interrupt. */620I2C_IE_AL; /* Arbitration lost interrupt. */621622/* Enable the interrupts. */623ti_i2c_write_2(sc, I2C_REG_IRQENABLE_SET, reg);624625/*626* 3. If using DMA to receive/transmit data, set to 1 the corresponding627* bit in the I2Ci.I2C_BUF register (the I2Ci.I2C_BUF[15] RDMA_EN628* bit for the receive DMA channel, the I2Ci.I2C_BUF[7] XDMA_EN bit629* for the transmit DMA channel).630*631* Not using DMA for now, so ignore this.632*/633634return (0);635}636637static int638ti_i2c_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)639{640struct ti_i2c_softc *sc;641int err;642643sc = device_get_softc(dev);644TI_I2C_LOCK(sc);645err = ti_i2c_reset(sc, speed);646TI_I2C_UNLOCK(sc);647if (err)648return (err);649650return (IIC_ENOADDR);651}652653static int654ti_i2c_activate(device_t dev)655{656int err;657struct ti_i2c_softc *sc;658659sc = (struct ti_i2c_softc*)device_get_softc(dev);660661/*662* 1. Enable the functional and interface clocks (see Section663* 23.1.5.1.1.1.1).664*/665err = ti_sysc_clock_enable(device_get_parent(dev));666if (err)667return (err);668669return (ti_i2c_reset(sc, IIC_UNKNOWN));670}671672/**673* ti_i2c_deactivate - deactivates the controller and releases resources674* @dev: i2c device handle675*676*677*678* LOCKING:679* Assumed called in an atomic context.680*681* RETURNS:682* nothing683*/684static void685ti_i2c_deactivate(device_t dev)686{687struct ti_i2c_softc *sc = device_get_softc(dev);688689/* Disable the controller - cancel all transactions. */690ti_i2c_write_2(sc, I2C_REG_IRQENABLE_CLR, 0xffff);691ti_i2c_write_2(sc, I2C_REG_STATUS, 0xffff);692ti_i2c_write_2(sc, I2C_REG_CON, 0);693694/* Release the interrupt handler. */695if (sc->sc_irq_h != NULL) {696bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_h);697sc->sc_irq_h = NULL;698}699700/* Unmap the I2C controller registers. */701if (sc->sc_mem_res != NULL) {702bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);703sc->sc_mem_res = NULL;704}705706/* Release the IRQ resource. */707if (sc->sc_irq_res != NULL) {708bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);709sc->sc_irq_res = NULL;710}711712/* Finally disable the functional and interface clocks. */713ti_sysc_clock_disable(device_get_parent(dev));714}715716static int717ti_i2c_sysctl_clk(SYSCTL_HANDLER_ARGS)718{719int clk, psc, sclh, scll;720struct ti_i2c_softc *sc;721722sc = arg1;723724TI_I2C_LOCK(sc);725/* Get the system prescaler value. */726psc = (int)ti_i2c_read_2(sc, I2C_REG_PSC) + 1;727728/* Get the bitrate. */729scll = (int)ti_i2c_read_2(sc, I2C_REG_SCLL) & I2C_SCLL_MASK;730sclh = (int)ti_i2c_read_2(sc, I2C_REG_SCLH) & I2C_SCLH_MASK;731732clk = I2C_CLK / psc / (scll + 7 + sclh + 5);733TI_I2C_UNLOCK(sc);734735return (sysctl_handle_int(oidp, &clk, 0, req));736}737738static int739ti_i2c_sysctl_timeout(SYSCTL_HANDLER_ARGS)740{741struct ti_i2c_softc *sc;742unsigned int val;743int err;744745sc = arg1;746747/*748* MTX_DEF lock can't be held while doing uimove in749* sysctl_handle_int750*/751TI_I2C_LOCK(sc);752val = sc->sc_timeout;753TI_I2C_UNLOCK(sc);754755err = sysctl_handle_int(oidp, &val, 0, req);756/* Write request? */757if ((err == 0) && (req->newptr != NULL)) {758TI_I2C_LOCK(sc);759sc->sc_timeout = val;760TI_I2C_UNLOCK(sc);761}762763return (err);764}765766static int767ti_i2c_probe(device_t dev)768{769770if (!ofw_bus_status_okay(dev))771return (ENXIO);772if (!ofw_bus_is_compatible(dev, "ti,omap4-i2c"))773return (ENXIO);774device_set_desc(dev, "TI I2C Controller");775776return (0);777}778779static int780ti_i2c_attach(device_t dev)781{782int err, rid;783struct ti_i2c_softc *sc;784struct sysctl_ctx_list *ctx;785struct sysctl_oid_list *tree;786uint16_t fifosz;787788sc = device_get_softc(dev);789sc->sc_dev = dev;790791/* Get the memory resource for the register mapping. */792rid = 0;793sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,794RF_ACTIVE);795if (sc->sc_mem_res == NULL) {796device_printf(dev, "Cannot map registers.\n");797return (ENXIO);798}799800/* Allocate our IRQ resource. */801rid = 0;802sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,803RF_ACTIVE | RF_SHAREABLE);804if (sc->sc_irq_res == NULL) {805bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);806device_printf(dev, "Cannot allocate interrupt.\n");807return (ENXIO);808}809810TI_I2C_LOCK_INIT(sc);811812/* First of all, we _must_ activate the H/W. */813err = ti_i2c_activate(dev);814if (err) {815device_printf(dev, "ti_i2c_activate failed\n");816goto out;817}818819/* Read the version number of the I2C module */820sc->sc_rev = ti_i2c_read_2(sc, I2C_REG_REVNB_HI) & 0xff;821822/* Get the fifo size. */823fifosz = ti_i2c_read_2(sc, I2C_REG_BUFSTAT);824fifosz >>= I2C_BUFSTAT_FIFODEPTH_SHIFT;825fifosz &= I2C_BUFSTAT_FIFODEPTH_MASK;826827device_printf(dev, "I2C revision %d.%d FIFO size: %d bytes\n",828sc->sc_rev >> 4, sc->sc_rev & 0xf, 8 << fifosz);829830/* Set the FIFO threshold to 5 for now. */831sc->sc_fifo_trsh = 5;832833/* Set I2C bus timeout */834sc->sc_timeout = 5*hz;835836ctx = device_get_sysctl_ctx(dev);837tree = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));838SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_clock",839CTLFLAG_RD | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,840ti_i2c_sysctl_clk, "IU", "I2C bus clock");841842SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "i2c_timeout",843CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, sc, 0,844ti_i2c_sysctl_timeout, "IU", "I2C bus timeout (in ticks)");845846/* Activate the interrupt. */847err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,848NULL, ti_i2c_intr, sc, &sc->sc_irq_h);849if (err)850goto out;851852/* Attach the iicbus. */853if ((sc->sc_iicbus = device_add_child(dev, "iicbus",854DEVICE_UNIT_ANY)) == NULL) {855device_printf(dev, "could not allocate iicbus instance\n");856err = ENXIO;857goto out;858}859860/* Probe and attach the iicbus when interrupts are available. */861bus_delayed_attach_children(dev);862863out:864if (err) {865ti_i2c_deactivate(dev);866TI_I2C_LOCK_DESTROY(sc);867}868869return (err);870}871872static int873ti_i2c_detach(device_t dev)874{875struct ti_i2c_softc *sc;876int rv;877878sc = device_get_softc(dev);879880if ((rv = bus_generic_detach(dev)) != 0) {881device_printf(dev, "cannot detach child devices\n");882return (rv);883}884885ti_i2c_deactivate(dev);886TI_I2C_LOCK_DESTROY(sc);887888return (0);889}890891static phandle_t892ti_i2c_get_node(device_t bus, device_t dev)893{894895/* Share controller node with iibus device. */896return (ofw_bus_get_node(bus));897}898899static device_method_t ti_i2c_methods[] = {900/* Device interface */901DEVMETHOD(device_probe, ti_i2c_probe),902DEVMETHOD(device_attach, ti_i2c_attach),903DEVMETHOD(device_detach, ti_i2c_detach),904905/* Bus interface */906DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),907DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),908DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),909DEVMETHOD(bus_release_resource, bus_generic_release_resource),910DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),911DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),912DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),913DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),914DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),915916/* OFW methods */917DEVMETHOD(ofw_bus_get_node, ti_i2c_get_node),918919/* iicbus interface */920DEVMETHOD(iicbus_callback, iicbus_null_callback),921DEVMETHOD(iicbus_reset, ti_i2c_iicbus_reset),922DEVMETHOD(iicbus_transfer, ti_i2c_transfer),923924DEVMETHOD_END925};926927static driver_t ti_i2c_driver = {928"iichb",929ti_i2c_methods,930sizeof(struct ti_i2c_softc),931};932933DRIVER_MODULE(ti_iic, simplebus, ti_i2c_driver, 0, 0);934DRIVER_MODULE(iicbus, ti_iic, iicbus_driver, 0, 0);935936MODULE_DEPEND(ti_iic, ti_sysc, 1, 1, 1);937MODULE_DEPEND(ti_iic, iicbus, 1, 1, 1);938939940