···581581 This driver can also be built as a module. If so, the module582582 will be called i2c-octeon.583583584584+config I2C_XILINX585585+ tristate "Xilinx I2C Controller"586586+ depends on EXPERIMENTAL && HAS_IOMEM587587+ help588588+ If you say yes to this option, support will be included for the589589+ Xilinx I2C controller.590590+591591+ This driver can also be built as a module. If so, the module592592+ will be called xilinx_i2c.593593+584594comment "External I2C/SMBus adapter drivers"585595586596config I2C_PARPORT
···11+/*22+ * i2c-xiic.c33+ * Copyright (c) 2002-2007 Xilinx Inc.44+ * Copyright (c) 2009-2010 Intel Corporation55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope that it will be useful,1111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1313+ * GNU General Public License for more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program; if not, write to the Free Software1717+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.1818+ *1919+ *2020+ * This code was implemented by Mocean Laboratories AB when porting linux2121+ * to the automotive development board Russellville. The copyright holder2222+ * as seen in the header is Intel corporation.2323+ * Mocean Laboratories forked off the GNU/Linux platform work into a2424+ * separate company called Pelagicore AB, which commited the code to the2525+ * kernel.2626+ */2727+2828+/* Supports:2929+ * Xilinx IIC3030+ */3131+#include <linux/kernel.h>3232+#include <linux/module.h>3333+#include <linux/init.h>3434+#include <linux/errno.h>3535+#include <linux/platform_device.h>3636+#include <linux/i2c.h>3737+#include <linux/interrupt.h>3838+#include <linux/wait.h>3939+#include <linux/i2c-xiic.h>4040+#include <linux/io.h>4141+4242+#define DRIVER_NAME "xiic-i2c"4343+4444+enum xilinx_i2c_state {4545+ STATE_DONE,4646+ STATE_ERROR,4747+ STATE_START4848+};4949+5050+/**5151+ * struct xiic_i2c - Internal representation of the XIIC I2C bus5252+ * @base: Memory base of the HW registers5353+ * @wait: Wait queue for callers5454+ * @adap: Kernel adapter representation5555+ * @tx_msg: Messages from above to be sent5656+ * @lock: Mutual exclusion5757+ * @tx_pos: Current pos in TX message5858+ * @nmsgs: Number of messages in tx_msg5959+ * @state: See STATE_6060+ * @rx_msg: Current RX message6161+ * @rx_pos: Position within current RX message6262+ */6363+struct xiic_i2c {6464+ void __iomem *base;6565+ wait_queue_head_t wait;6666+ struct i2c_adapter adap;6767+ struct i2c_msg *tx_msg;6868+ spinlock_t lock;6969+ unsigned int tx_pos;7070+ unsigned int nmsgs;7171+ enum xilinx_i2c_state state;7272+ struct i2c_msg *rx_msg;7373+ int rx_pos;7474+};7575+7676+7777+#define XIIC_MSB_OFFSET 07878+#define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)7979+8080+/*8181+ * Register offsets in bytes from RegisterBase. Three is added to the8282+ * base offset to access LSB (IBM style) of the word8383+ */8484+#define XIIC_CR_REG_OFFSET (0x00+XIIC_REG_OFFSET) /* Control Register */8585+#define XIIC_SR_REG_OFFSET (0x04+XIIC_REG_OFFSET) /* Status Register */8686+#define XIIC_DTR_REG_OFFSET (0x08+XIIC_REG_OFFSET) /* Data Tx Register */8787+#define XIIC_DRR_REG_OFFSET (0x0C+XIIC_REG_OFFSET) /* Data Rx Register */8888+#define XIIC_ADR_REG_OFFSET (0x10+XIIC_REG_OFFSET) /* Address Register */8989+#define XIIC_TFO_REG_OFFSET (0x14+XIIC_REG_OFFSET) /* Tx FIFO Occupancy */9090+#define XIIC_RFO_REG_OFFSET (0x18+XIIC_REG_OFFSET) /* Rx FIFO Occupancy */9191+#define XIIC_TBA_REG_OFFSET (0x1C+XIIC_REG_OFFSET) /* 10 Bit Address reg */9292+#define XIIC_RFD_REG_OFFSET (0x20+XIIC_REG_OFFSET) /* Rx FIFO Depth reg */9393+#define XIIC_GPO_REG_OFFSET (0x24+XIIC_REG_OFFSET) /* Output Register */9494+9595+/* Control Register masks */9696+#define XIIC_CR_ENABLE_DEVICE_MASK 0x01 /* Device enable = 1 */9797+#define XIIC_CR_TX_FIFO_RESET_MASK 0x02 /* Transmit FIFO reset=1 */9898+#define XIIC_CR_MSMS_MASK 0x04 /* Master starts Txing=1 */9999+#define XIIC_CR_DIR_IS_TX_MASK 0x08 /* Dir of tx. Txing=1 */100100+#define XIIC_CR_NO_ACK_MASK 0x10 /* Tx Ack. NO ack = 1 */101101+#define XIIC_CR_REPEATED_START_MASK 0x20 /* Repeated start = 1 */102102+#define XIIC_CR_GENERAL_CALL_MASK 0x40 /* Gen Call enabled = 1 */103103+104104+/* Status Register masks */105105+#define XIIC_SR_GEN_CALL_MASK 0x01 /* 1=a mstr issued a GC */106106+#define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02 /* 1=when addr as slave */107107+#define XIIC_SR_BUS_BUSY_MASK 0x04 /* 1 = bus is busy */108108+#define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08 /* 1=Dir: mstr <-- slave */109109+#define XIIC_SR_TX_FIFO_FULL_MASK 0x10 /* 1 = Tx FIFO full */110110+#define XIIC_SR_RX_FIFO_FULL_MASK 0x20 /* 1 = Rx FIFO full */111111+#define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40 /* 1 = Rx FIFO empty */112112+#define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80 /* 1 = Tx FIFO empty */113113+114114+/* Interrupt Status Register masks Interrupt occurs when... */115115+#define XIIC_INTR_ARB_LOST_MASK 0x01 /* 1 = arbitration lost */116116+#define XIIC_INTR_TX_ERROR_MASK 0x02 /* 1=Tx error/msg complete */117117+#define XIIC_INTR_TX_EMPTY_MASK 0x04 /* 1 = Tx FIFO/reg empty */118118+#define XIIC_INTR_RX_FULL_MASK 0x08 /* 1=Rx FIFO/reg=OCY level */119119+#define XIIC_INTR_BNB_MASK 0x10 /* 1 = Bus not busy */120120+#define XIIC_INTR_AAS_MASK 0x20 /* 1 = when addr as slave */121121+#define XIIC_INTR_NAAS_MASK 0x40 /* 1 = not addr as slave */122122+#define XIIC_INTR_TX_HALF_MASK 0x80 /* 1 = TX FIFO half empty */123123+124124+/* The following constants specify the depth of the FIFOs */125125+#define IIC_RX_FIFO_DEPTH 16 /* Rx fifo capacity */126126+#define IIC_TX_FIFO_DEPTH 16 /* Tx fifo capacity */127127+128128+/* The following constants specify groups of interrupts that are typically129129+ * enabled or disables at the same time130130+ */131131+#define XIIC_TX_INTERRUPTS \132132+(XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)133133+134134+#define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)135135+136136+/* The following constants are used with the following macros to specify the137137+ * operation, a read or write operation.138138+ */139139+#define XIIC_READ_OPERATION 1140140+#define XIIC_WRITE_OPERATION 0141141+142142+/*143143+ * Tx Fifo upper bit masks.144144+ */145145+#define XIIC_TX_DYN_START_MASK 0x0100 /* 1 = Set dynamic start */146146+#define XIIC_TX_DYN_STOP_MASK 0x0200 /* 1 = Set dynamic stop */147147+148148+/*149149+ * The following constants define the register offsets for the Interrupt150150+ * registers. There are some holes in the memory map for reserved addresses151151+ * to allow other registers to be added and still match the memory map of the152152+ * interrupt controller registers153153+ */154154+#define XIIC_DGIER_OFFSET 0x1C /* Device Global Interrupt Enable Register */155155+#define XIIC_IISR_OFFSET 0x20 /* Interrupt Status Register */156156+#define XIIC_IIER_OFFSET 0x28 /* Interrupt Enable Register */157157+#define XIIC_RESETR_OFFSET 0x40 /* Reset Register */158158+159159+#define XIIC_RESET_MASK 0xAUL160160+161161+/*162162+ * The following constant is used for the device global interrupt enable163163+ * register, to enable all interrupts for the device, this is the only bit164164+ * in the register165165+ */166166+#define XIIC_GINTR_ENABLE_MASK 0x80000000UL167167+168168+#define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)169169+#define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)170170+171171+static void xiic_start_xfer(struct xiic_i2c *i2c);172172+static void __xiic_start_xfer(struct xiic_i2c *i2c);173173+174174+static inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)175175+{176176+ iowrite8(value, i2c->base + reg);177177+}178178+179179+static inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)180180+{181181+ return ioread8(i2c->base + reg);182182+}183183+184184+static inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)185185+{186186+ iowrite16(value, i2c->base + reg);187187+}188188+189189+static inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)190190+{191191+ iowrite32(value, i2c->base + reg);192192+}193193+194194+static inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)195195+{196196+ return ioread32(i2c->base + reg);197197+}198198+199199+static inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)200200+{201201+ u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);202202+ xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);203203+}204204+205205+static inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)206206+{207207+ u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);208208+ xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);209209+}210210+211211+static inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)212212+{213213+ u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);214214+ xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);215215+}216216+217217+static inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)218218+{219219+ xiic_irq_clr(i2c, mask);220220+ xiic_irq_en(i2c, mask);221221+}222222+223223+static void xiic_clear_rx_fifo(struct xiic_i2c *i2c)224224+{225225+ u8 sr;226226+ for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);227227+ !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);228228+ sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET))229229+ xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);230230+}231231+232232+static void xiic_reinit(struct xiic_i2c *i2c)233233+{234234+ xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);235235+236236+ /* Set receive Fifo depth to maximum (zero based). */237237+ xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);238238+239239+ /* Reset Tx Fifo. */240240+ xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);241241+242242+ /* Enable IIC Device, remove Tx Fifo reset & disable general call. */243243+ xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);244244+245245+ /* make sure RX fifo is empty */246246+ xiic_clear_rx_fifo(i2c);247247+248248+ /* Enable interrupts */249249+ xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);250250+251251+ xiic_irq_clr_en(i2c, XIIC_INTR_AAS_MASK | XIIC_INTR_ARB_LOST_MASK);252252+}253253+254254+static void xiic_deinit(struct xiic_i2c *i2c)255255+{256256+ u8 cr;257257+258258+ xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);259259+260260+ /* Disable IIC Device. */261261+ cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);262262+ xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);263263+}264264+265265+static void xiic_read_rx(struct xiic_i2c *i2c)266266+{267267+ u8 bytes_in_fifo;268268+ int i;269269+270270+ bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;271271+272272+ dev_dbg(i2c->adap.dev.parent, "%s entry, bytes in fifo: %d, msg: %d"273273+ ", SR: 0x%x, CR: 0x%x\n",274274+ __func__, bytes_in_fifo, xiic_rx_space(i2c),275275+ xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),276276+ xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));277277+278278+ if (bytes_in_fifo > xiic_rx_space(i2c))279279+ bytes_in_fifo = xiic_rx_space(i2c);280280+281281+ for (i = 0; i < bytes_in_fifo; i++)282282+ i2c->rx_msg->buf[i2c->rx_pos++] =283283+ xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);284284+285285+ xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET,286286+ (xiic_rx_space(i2c) > IIC_RX_FIFO_DEPTH) ?287287+ IIC_RX_FIFO_DEPTH - 1 : xiic_rx_space(i2c) - 1);288288+}289289+290290+static int xiic_tx_fifo_space(struct xiic_i2c *i2c)291291+{292292+ /* return the actual space left in the FIFO */293293+ return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;294294+}295295+296296+static void xiic_fill_tx_fifo(struct xiic_i2c *i2c)297297+{298298+ u8 fifo_space = xiic_tx_fifo_space(i2c);299299+ int len = xiic_tx_space(i2c);300300+301301+ len = (len > fifo_space) ? fifo_space : len;302302+303303+ dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",304304+ __func__, len, fifo_space);305305+306306+ while (len--) {307307+ u16 data = i2c->tx_msg->buf[i2c->tx_pos++];308308+ if ((xiic_tx_space(i2c) == 0) && (i2c->nmsgs == 1)) {309309+ /* last message in transfer -> STOP */310310+ data |= XIIC_TX_DYN_STOP_MASK;311311+ dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);312312+313313+ xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);314314+ } else315315+ xiic_setreg8(i2c, XIIC_DTR_REG_OFFSET, data);316316+ }317317+}318318+319319+static void xiic_wakeup(struct xiic_i2c *i2c, int code)320320+{321321+ i2c->tx_msg = NULL;322322+ i2c->rx_msg = NULL;323323+ i2c->nmsgs = 0;324324+ i2c->state = code;325325+ wake_up(&i2c->wait);326326+}327327+328328+static void xiic_process(struct xiic_i2c *i2c)329329+{330330+ u32 pend, isr, ier;331331+ u32 clr = 0;332332+333333+ /* Get the interrupt Status from the IPIF. There is no clearing of334334+ * interrupts in the IPIF. Interrupts must be cleared at the source.335335+ * To find which interrupts are pending; AND interrupts pending with336336+ * interrupts masked.337337+ */338338+ isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);339339+ ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);340340+ pend = isr & ier;341341+342342+ dev_dbg(i2c->adap.dev.parent, "%s entry, IER: 0x%x, ISR: 0x%x, "343343+ "pend: 0x%x, SR: 0x%x, msg: %p, nmsgs: %d\n",344344+ __func__, ier, isr, pend, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),345345+ i2c->tx_msg, i2c->nmsgs);346346+347347+ /* Do not processes a devices interrupts if the device has no348348+ * interrupts pending349349+ */350350+ if (!pend)351351+ return;352352+353353+ /* Service requesting interrupt */354354+ if ((pend & XIIC_INTR_ARB_LOST_MASK) ||355355+ ((pend & XIIC_INTR_TX_ERROR_MASK) &&356356+ !(pend & XIIC_INTR_RX_FULL_MASK))) {357357+ /* bus arbritration lost, or...358358+ * Transmit error _OR_ RX completed359359+ * if this happens when RX_FULL is not set360360+ * this is probably a TX error361361+ */362362+363363+ dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);364364+365365+ /* dynamic mode seem to suffer from problems if we just flushes366366+ * fifos and the next message is a TX with len 0 (only addr)367367+ * reset the IP instead of just flush fifos368368+ */369369+ xiic_reinit(i2c);370370+371371+ if (i2c->tx_msg)372372+ xiic_wakeup(i2c, STATE_ERROR);373373+374374+ } else if (pend & XIIC_INTR_RX_FULL_MASK) {375375+ /* Receive register/FIFO is full */376376+377377+ clr = XIIC_INTR_RX_FULL_MASK;378378+ if (!i2c->rx_msg) {379379+ dev_dbg(i2c->adap.dev.parent,380380+ "%s unexpexted RX IRQ\n", __func__);381381+ xiic_clear_rx_fifo(i2c);382382+ goto out;383383+ }384384+385385+ xiic_read_rx(i2c);386386+ if (xiic_rx_space(i2c) == 0) {387387+ /* this is the last part of the message */388388+ i2c->rx_msg = NULL;389389+390390+ /* also clear TX error if there (RX complete) */391391+ clr |= (isr & XIIC_INTR_TX_ERROR_MASK);392392+393393+ dev_dbg(i2c->adap.dev.parent,394394+ "%s end of message, nmsgs: %d\n",395395+ __func__, i2c->nmsgs);396396+397397+ /* send next message if this wasn't the last,398398+ * otherwise the transfer will be finialise when399399+ * receiving the bus not busy interrupt400400+ */401401+ if (i2c->nmsgs > 1) {402402+ i2c->nmsgs--;403403+ i2c->tx_msg++;404404+ dev_dbg(i2c->adap.dev.parent,405405+ "%s will start next...\n", __func__);406406+407407+ __xiic_start_xfer(i2c);408408+ }409409+ }410410+ } else if (pend & XIIC_INTR_BNB_MASK) {411411+ /* IIC bus has transitioned to not busy */412412+ clr = XIIC_INTR_BNB_MASK;413413+414414+ /* The bus is not busy, disable BusNotBusy interrupt */415415+ xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);416416+417417+ if (!i2c->tx_msg)418418+ goto out;419419+420420+ if ((i2c->nmsgs == 1) && !i2c->rx_msg &&421421+ xiic_tx_space(i2c) == 0)422422+ xiic_wakeup(i2c, STATE_DONE);423423+ else424424+ xiic_wakeup(i2c, STATE_ERROR);425425+426426+ } else if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {427427+ /* Transmit register/FIFO is empty or � empty */428428+429429+ clr = pend &430430+ (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK);431431+432432+ if (!i2c->tx_msg) {433433+ dev_dbg(i2c->adap.dev.parent,434434+ "%s unexpexted TX IRQ\n", __func__);435435+ goto out;436436+ }437437+438438+ xiic_fill_tx_fifo(i2c);439439+440440+ /* current message sent and there is space in the fifo */441441+ if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {442442+ dev_dbg(i2c->adap.dev.parent,443443+ "%s end of message sent, nmsgs: %d\n",444444+ __func__, i2c->nmsgs);445445+ if (i2c->nmsgs > 1) {446446+ i2c->nmsgs--;447447+ i2c->tx_msg++;448448+ __xiic_start_xfer(i2c);449449+ } else {450450+ xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);451451+452452+ dev_dbg(i2c->adap.dev.parent,453453+ "%s Got TX IRQ but no more to do...\n",454454+ __func__);455455+ }456456+ } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))457457+ /* current frame is sent and is last,458458+ * make sure to disable tx half459459+ */460460+ xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);461461+ } else {462462+ /* got IRQ which is not acked */463463+ dev_err(i2c->adap.dev.parent, "%s Got unexpected IRQ\n",464464+ __func__);465465+ clr = pend;466466+ }467467+out:468468+ dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);469469+470470+ xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);471471+}472472+473473+static int xiic_bus_busy(struct xiic_i2c *i2c)474474+{475475+ u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);476476+477477+ return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;478478+}479479+480480+static int xiic_busy(struct xiic_i2c *i2c)481481+{482482+ int tries = 3;483483+ int err;484484+485485+ if (i2c->tx_msg)486486+ return -EBUSY;487487+488488+ /* for instance if previous transfer was terminated due to TX error489489+ * it might be that the bus is on it's way to become available490490+ * give it at most 3 ms to wake491491+ */492492+ err = xiic_bus_busy(i2c);493493+ while (err && tries--) {494494+ mdelay(1);495495+ err = xiic_bus_busy(i2c);496496+ }497497+498498+ return err;499499+}500500+501501+static void xiic_start_recv(struct xiic_i2c *i2c)502502+{503503+ u8 rx_watermark;504504+ struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;505505+506506+ /* Clear and enable Rx full interrupt. */507507+ xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);508508+509509+ /* we want to get all but last byte, because the TX_ERROR IRQ is used510510+ * to inidicate error ACK on the address, and negative ack on the last511511+ * received byte, so to not mix them receive all but last.512512+ * In the case where there is only one byte to receive513513+ * we can check if ERROR and RX full is set at the same time514514+ */515515+ rx_watermark = msg->len;516516+ if (rx_watermark > IIC_RX_FIFO_DEPTH)517517+ rx_watermark = IIC_RX_FIFO_DEPTH;518518+ xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rx_watermark - 1);519519+520520+ if (!(msg->flags & I2C_M_NOSTART))521521+ /* write the address */522522+ xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,523523+ (msg->addr << 1) | XIIC_READ_OPERATION |524524+ XIIC_TX_DYN_START_MASK);525525+526526+ xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);527527+528528+ xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,529529+ msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));530530+ if (i2c->nmsgs == 1)531531+ /* very last, enable bus not busy as well */532532+ xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);533533+534534+ /* the message is tx:ed */535535+ i2c->tx_pos = msg->len;536536+}537537+538538+static void xiic_start_send(struct xiic_i2c *i2c)539539+{540540+ struct i2c_msg *msg = i2c->tx_msg;541541+542542+ xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);543543+544544+ dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d, "545545+ "ISR: 0x%x, CR: 0x%x\n",546546+ __func__, msg, msg->len, xiic_getreg32(i2c, XIIC_IISR_OFFSET),547547+ xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));548548+549549+ if (!(msg->flags & I2C_M_NOSTART)) {550550+ /* write the address */551551+ u16 data = ((msg->addr << 1) & 0xfe) | XIIC_WRITE_OPERATION |552552+ XIIC_TX_DYN_START_MASK;553553+ if ((i2c->nmsgs == 1) && msg->len == 0)554554+ /* no data and last message -> add STOP */555555+ data |= XIIC_TX_DYN_STOP_MASK;556556+557557+ xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);558558+ }559559+560560+ xiic_fill_tx_fifo(i2c);561561+562562+ /* Clear any pending Tx empty, Tx Error and then enable them. */563563+ xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |564564+ XIIC_INTR_BNB_MASK);565565+}566566+567567+static irqreturn_t xiic_isr(int irq, void *dev_id)568568+{569569+ struct xiic_i2c *i2c = dev_id;570570+571571+ spin_lock(&i2c->lock);572572+ /* disable interrupts globally */573573+ xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);574574+575575+ dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);576576+577577+ xiic_process(i2c);578578+579579+ xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);580580+ spin_unlock(&i2c->lock);581581+582582+ return IRQ_HANDLED;583583+}584584+585585+static void __xiic_start_xfer(struct xiic_i2c *i2c)586586+{587587+ int first = 1;588588+ int fifo_space = xiic_tx_fifo_space(i2c);589589+ dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",590590+ __func__, i2c->tx_msg, fifo_space);591591+592592+ if (!i2c->tx_msg)593593+ return;594594+595595+ i2c->rx_pos = 0;596596+ i2c->tx_pos = 0;597597+ i2c->state = STATE_START;598598+ while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {599599+ if (!first) {600600+ i2c->nmsgs--;601601+ i2c->tx_msg++;602602+ i2c->tx_pos = 0;603603+ } else604604+ first = 0;605605+606606+ if (i2c->tx_msg->flags & I2C_M_RD) {607607+ /* we dont date putting several reads in the FIFO */608608+ xiic_start_recv(i2c);609609+ return;610610+ } else {611611+ xiic_start_send(i2c);612612+ if (xiic_tx_space(i2c) != 0) {613613+ /* the message could not be completely sent */614614+ break;615615+ }616616+ }617617+618618+ fifo_space = xiic_tx_fifo_space(i2c);619619+ }620620+621621+ /* there are more messages or the current one could not be completely622622+ * put into the FIFO, also enable the half empty interrupt623623+ */624624+ if (i2c->nmsgs > 1 || xiic_tx_space(i2c))625625+ xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);626626+627627+}628628+629629+static void xiic_start_xfer(struct xiic_i2c *i2c)630630+{631631+ unsigned long flags;632632+633633+ spin_lock_irqsave(&i2c->lock, flags);634634+ xiic_reinit(i2c);635635+ /* disable interrupts globally */636636+ xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);637637+ spin_unlock_irqrestore(&i2c->lock, flags);638638+639639+ __xiic_start_xfer(i2c);640640+ xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);641641+}642642+643643+static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)644644+{645645+ struct xiic_i2c *i2c = i2c_get_adapdata(adap);646646+ int err;647647+648648+ dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,649649+ xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));650650+651651+ err = xiic_busy(i2c);652652+ if (err)653653+ return err;654654+655655+ i2c->tx_msg = msgs;656656+ i2c->nmsgs = num;657657+658658+ xiic_start_xfer(i2c);659659+660660+ if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||661661+ (i2c->state == STATE_DONE), HZ))662662+ return (i2c->state == STATE_DONE) ? num : -EIO;663663+ else {664664+ i2c->tx_msg = NULL;665665+ i2c->rx_msg = NULL;666666+ i2c->nmsgs = 0;667667+ return -ETIMEDOUT;668668+ }669669+}670670+671671+static u32 xiic_func(struct i2c_adapter *adap)672672+{673673+ return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;674674+}675675+676676+static const struct i2c_algorithm xiic_algorithm = {677677+ .master_xfer = xiic_xfer,678678+ .functionality = xiic_func,679679+};680680+681681+static struct i2c_adapter xiic_adapter = {682682+ .owner = THIS_MODULE,683683+ .name = DRIVER_NAME,684684+ .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,685685+ .algo = &xiic_algorithm,686686+};687687+688688+689689+static int __devinit xiic_i2c_probe(struct platform_device *pdev)690690+{691691+ struct xiic_i2c *i2c;692692+ struct xiic_i2c_platform_data *pdata;693693+ struct resource *res;694694+ int ret, irq;695695+ u8 i;696696+697697+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);698698+ if (!res)699699+ goto resource_missing;700700+701701+ irq = platform_get_irq(pdev, 0);702702+ if (irq < 0)703703+ goto resource_missing;704704+705705+ pdata = (struct xiic_i2c_platform_data *) pdev->dev.platform_data;706706+ if (!pdata)707707+ return -EINVAL;708708+709709+ i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);710710+ if (!i2c)711711+ return -ENOMEM;712712+713713+ if (!request_mem_region(res->start, resource_size(res), pdev->name)) {714714+ dev_err(&pdev->dev, "Memory region busy\n");715715+ ret = -EBUSY;716716+ goto request_mem_failed;717717+ }718718+719719+ i2c->base = ioremap(res->start, resource_size(res));720720+ if (!i2c->base) {721721+ dev_err(&pdev->dev, "Unable to map registers\n");722722+ ret = -EIO;723723+ goto map_failed;724724+ }725725+726726+ /* hook up driver to tree */727727+ platform_set_drvdata(pdev, i2c);728728+ i2c->adap = xiic_adapter;729729+ i2c_set_adapdata(&i2c->adap, i2c);730730+ i2c->adap.dev.parent = &pdev->dev;731731+732732+ xiic_reinit(i2c);733733+734734+ spin_lock_init(&i2c->lock);735735+ init_waitqueue_head(&i2c->wait);736736+ ret = request_irq(irq, xiic_isr, 0, pdev->name, i2c);737737+ if (ret) {738738+ dev_err(&pdev->dev, "Cannot claim IRQ\n");739739+ goto request_irq_failed;740740+ }741741+742742+ /* add i2c adapter to i2c tree */743743+ ret = i2c_add_adapter(&i2c->adap);744744+ if (ret) {745745+ dev_err(&pdev->dev, "Failed to add adapter\n");746746+ goto add_adapter_failed;747747+ }748748+749749+ /* add in known devices to the bus */750750+ for (i = 0; i < pdata->num_devices; i++)751751+ i2c_new_device(&i2c->adap, pdata->devices + i);752752+753753+ return 0;754754+755755+add_adapter_failed:756756+ free_irq(irq, i2c);757757+request_irq_failed:758758+ xiic_deinit(i2c);759759+ iounmap(i2c->base);760760+map_failed:761761+ release_mem_region(res->start, resource_size(res));762762+request_mem_failed:763763+ kfree(i2c);764764+765765+ return ret;766766+resource_missing:767767+ dev_err(&pdev->dev, "IRQ or Memory resource is missing\n");768768+ return -ENOENT;769769+}770770+771771+static int __devexit xiic_i2c_remove(struct platform_device* pdev)772772+{773773+ struct xiic_i2c *i2c = platform_get_drvdata(pdev);774774+ struct resource *res;775775+776776+ /* remove adapter & data */777777+ i2c_del_adapter(&i2c->adap);778778+779779+ xiic_deinit(i2c);780780+781781+ platform_set_drvdata(pdev, NULL);782782+783783+ free_irq(platform_get_irq(pdev, 0), i2c);784784+785785+ iounmap(i2c->base);786786+787787+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);788788+ if (res)789789+ release_mem_region(res->start, resource_size(res));790790+791791+ kfree(i2c);792792+793793+ return 0;794794+}795795+796796+797797+/* work with hotplug and coldplug */798798+MODULE_ALIAS("platform:"DRIVER_NAME);799799+800800+static struct platform_driver xiic_i2c_driver = {801801+ .probe = xiic_i2c_probe,802802+ .remove = __devexit_p(xiic_i2c_remove),803803+ .driver = {804804+ .owner = THIS_MODULE,805805+ .name = DRIVER_NAME,806806+ },807807+};808808+809809+static int __init xiic_i2c_init(void)810810+{811811+ return platform_driver_register(&xiic_i2c_driver);812812+}813813+814814+static void __exit xiic_i2c_exit(void)815815+{816816+ platform_driver_unregister(&xiic_i2c_driver);817817+}818818+819819+module_init(xiic_i2c_init);820820+module_exit(xiic_i2c_exit);821821+822822+MODULE_AUTHOR("info@mocean-labs.com");823823+MODULE_DESCRIPTION("Xilinx I2C bus driver");824824+MODULE_LICENSE("GPL v2");