i2c-xiic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * i2c-xiic.c
  3. * Copyright (c) 2002-2007 Xilinx Inc.
  4. * Copyright (c) 2009-2010 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. *
  16. * This code was implemented by Mocean Laboratories AB when porting linux
  17. * to the automotive development board Russellville. The copyright holder
  18. * as seen in the header is Intel corporation.
  19. * Mocean Laboratories forked off the GNU/Linux platform work into a
  20. * separate company called Pelagicore AB, which committed the code to the
  21. * kernel.
  22. */
  23. /* Supports:
  24. * Xilinx IIC
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/err.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/i2c.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/wait.h>
  35. #include <linux/i2c-xiic.h>
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/of.h>
  39. #define DRIVER_NAME "xiic-i2c"
  40. enum xilinx_i2c_state {
  41. STATE_DONE,
  42. STATE_ERROR,
  43. STATE_START
  44. };
  45. enum xiic_endian {
  46. LITTLE,
  47. BIG
  48. };
  49. /**
  50. * struct xiic_i2c - Internal representation of the XIIC I2C bus
  51. * @base: Memory base of the HW registers
  52. * @wait: Wait queue for callers
  53. * @adap: Kernel adapter representation
  54. * @tx_msg: Messages from above to be sent
  55. * @lock: Mutual exclusion
  56. * @tx_pos: Current pos in TX message
  57. * @nmsgs: Number of messages in tx_msg
  58. * @state: See STATE_
  59. * @rx_msg: Current RX message
  60. * @rx_pos: Position within current RX message
  61. * @endianness: big/little-endian byte order
  62. */
  63. struct xiic_i2c {
  64. void __iomem *base;
  65. wait_queue_head_t wait;
  66. struct i2c_adapter adap;
  67. struct i2c_msg *tx_msg;
  68. spinlock_t lock;
  69. unsigned int tx_pos;
  70. unsigned int nmsgs;
  71. enum xilinx_i2c_state state;
  72. struct i2c_msg *rx_msg;
  73. int rx_pos;
  74. enum xiic_endian endianness;
  75. };
  76. #define XIIC_MSB_OFFSET 0
  77. #define XIIC_REG_OFFSET (0x100+XIIC_MSB_OFFSET)
  78. /*
  79. * Register offsets in bytes from RegisterBase. Three is added to the
  80. * base offset to access LSB (IBM style) of the word
  81. */
  82. #define XIIC_CR_REG_OFFSET (0x00+XIIC_REG_OFFSET) /* Control Register */
  83. #define XIIC_SR_REG_OFFSET (0x04+XIIC_REG_OFFSET) /* Status Register */
  84. #define XIIC_DTR_REG_OFFSET (0x08+XIIC_REG_OFFSET) /* Data Tx Register */
  85. #define XIIC_DRR_REG_OFFSET (0x0C+XIIC_REG_OFFSET) /* Data Rx Register */
  86. #define XIIC_ADR_REG_OFFSET (0x10+XIIC_REG_OFFSET) /* Address Register */
  87. #define XIIC_TFO_REG_OFFSET (0x14+XIIC_REG_OFFSET) /* Tx FIFO Occupancy */
  88. #define XIIC_RFO_REG_OFFSET (0x18+XIIC_REG_OFFSET) /* Rx FIFO Occupancy */
  89. #define XIIC_TBA_REG_OFFSET (0x1C+XIIC_REG_OFFSET) /* 10 Bit Address reg */
  90. #define XIIC_RFD_REG_OFFSET (0x20+XIIC_REG_OFFSET) /* Rx FIFO Depth reg */
  91. #define XIIC_GPO_REG_OFFSET (0x24+XIIC_REG_OFFSET) /* Output Register */
  92. /* Control Register masks */
  93. #define XIIC_CR_ENABLE_DEVICE_MASK 0x01 /* Device enable = 1 */
  94. #define XIIC_CR_TX_FIFO_RESET_MASK 0x02 /* Transmit FIFO reset=1 */
  95. #define XIIC_CR_MSMS_MASK 0x04 /* Master starts Txing=1 */
  96. #define XIIC_CR_DIR_IS_TX_MASK 0x08 /* Dir of tx. Txing=1 */
  97. #define XIIC_CR_NO_ACK_MASK 0x10 /* Tx Ack. NO ack = 1 */
  98. #define XIIC_CR_REPEATED_START_MASK 0x20 /* Repeated start = 1 */
  99. #define XIIC_CR_GENERAL_CALL_MASK 0x40 /* Gen Call enabled = 1 */
  100. /* Status Register masks */
  101. #define XIIC_SR_GEN_CALL_MASK 0x01 /* 1=a mstr issued a GC */
  102. #define XIIC_SR_ADDR_AS_SLAVE_MASK 0x02 /* 1=when addr as slave */
  103. #define XIIC_SR_BUS_BUSY_MASK 0x04 /* 1 = bus is busy */
  104. #define XIIC_SR_MSTR_RDING_SLAVE_MASK 0x08 /* 1=Dir: mstr <-- slave */
  105. #define XIIC_SR_TX_FIFO_FULL_MASK 0x10 /* 1 = Tx FIFO full */
  106. #define XIIC_SR_RX_FIFO_FULL_MASK 0x20 /* 1 = Rx FIFO full */
  107. #define XIIC_SR_RX_FIFO_EMPTY_MASK 0x40 /* 1 = Rx FIFO empty */
  108. #define XIIC_SR_TX_FIFO_EMPTY_MASK 0x80 /* 1 = Tx FIFO empty */
  109. /* Interrupt Status Register masks Interrupt occurs when... */
  110. #define XIIC_INTR_ARB_LOST_MASK 0x01 /* 1 = arbitration lost */
  111. #define XIIC_INTR_TX_ERROR_MASK 0x02 /* 1=Tx error/msg complete */
  112. #define XIIC_INTR_TX_EMPTY_MASK 0x04 /* 1 = Tx FIFO/reg empty */
  113. #define XIIC_INTR_RX_FULL_MASK 0x08 /* 1=Rx FIFO/reg=OCY level */
  114. #define XIIC_INTR_BNB_MASK 0x10 /* 1 = Bus not busy */
  115. #define XIIC_INTR_AAS_MASK 0x20 /* 1 = when addr as slave */
  116. #define XIIC_INTR_NAAS_MASK 0x40 /* 1 = not addr as slave */
  117. #define XIIC_INTR_TX_HALF_MASK 0x80 /* 1 = TX FIFO half empty */
  118. /* The following constants specify the depth of the FIFOs */
  119. #define IIC_RX_FIFO_DEPTH 16 /* Rx fifo capacity */
  120. #define IIC_TX_FIFO_DEPTH 16 /* Tx fifo capacity */
  121. /* The following constants specify groups of interrupts that are typically
  122. * enabled or disables at the same time
  123. */
  124. #define XIIC_TX_INTERRUPTS \
  125. (XIIC_INTR_TX_ERROR_MASK | XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)
  126. #define XIIC_TX_RX_INTERRUPTS (XIIC_INTR_RX_FULL_MASK | XIIC_TX_INTERRUPTS)
  127. /* The following constants are used with the following macros to specify the
  128. * operation, a read or write operation.
  129. */
  130. #define XIIC_READ_OPERATION 1
  131. #define XIIC_WRITE_OPERATION 0
  132. /*
  133. * Tx Fifo upper bit masks.
  134. */
  135. #define XIIC_TX_DYN_START_MASK 0x0100 /* 1 = Set dynamic start */
  136. #define XIIC_TX_DYN_STOP_MASK 0x0200 /* 1 = Set dynamic stop */
  137. /*
  138. * The following constants define the register offsets for the Interrupt
  139. * registers. There are some holes in the memory map for reserved addresses
  140. * to allow other registers to be added and still match the memory map of the
  141. * interrupt controller registers
  142. */
  143. #define XIIC_DGIER_OFFSET 0x1C /* Device Global Interrupt Enable Register */
  144. #define XIIC_IISR_OFFSET 0x20 /* Interrupt Status Register */
  145. #define XIIC_IIER_OFFSET 0x28 /* Interrupt Enable Register */
  146. #define XIIC_RESETR_OFFSET 0x40 /* Reset Register */
  147. #define XIIC_RESET_MASK 0xAUL
  148. /*
  149. * The following constant is used for the device global interrupt enable
  150. * register, to enable all interrupts for the device, this is the only bit
  151. * in the register
  152. */
  153. #define XIIC_GINTR_ENABLE_MASK 0x80000000UL
  154. #define xiic_tx_space(i2c) ((i2c)->tx_msg->len - (i2c)->tx_pos)
  155. #define xiic_rx_space(i2c) ((i2c)->rx_msg->len - (i2c)->rx_pos)
  156. static void xiic_start_xfer(struct xiic_i2c *i2c);
  157. static void __xiic_start_xfer(struct xiic_i2c *i2c);
  158. /*
  159. * For the register read and write functions, a little-endian and big-endian
  160. * version are necessary. Endianness is detected during the probe function.
  161. * Only the least significant byte [doublet] of the register are ever
  162. * accessed. This requires an offset of 3 [2] from the base address for
  163. * big-endian systems.
  164. */
  165. static inline void xiic_setreg8(struct xiic_i2c *i2c, int reg, u8 value)
  166. {
  167. if (i2c->endianness == LITTLE)
  168. iowrite8(value, i2c->base + reg);
  169. else
  170. iowrite8(value, i2c->base + reg + 3);
  171. }
  172. static inline u8 xiic_getreg8(struct xiic_i2c *i2c, int reg)
  173. {
  174. u8 ret;
  175. if (i2c->endianness == LITTLE)
  176. ret = ioread8(i2c->base + reg);
  177. else
  178. ret = ioread8(i2c->base + reg + 3);
  179. return ret;
  180. }
  181. static inline void xiic_setreg16(struct xiic_i2c *i2c, int reg, u16 value)
  182. {
  183. if (i2c->endianness == LITTLE)
  184. iowrite16(value, i2c->base + reg);
  185. else
  186. iowrite16be(value, i2c->base + reg + 2);
  187. }
  188. static inline void xiic_setreg32(struct xiic_i2c *i2c, int reg, int value)
  189. {
  190. if (i2c->endianness == LITTLE)
  191. iowrite32(value, i2c->base + reg);
  192. else
  193. iowrite32be(value, i2c->base + reg);
  194. }
  195. static inline int xiic_getreg32(struct xiic_i2c *i2c, int reg)
  196. {
  197. u32 ret;
  198. if (i2c->endianness == LITTLE)
  199. ret = ioread32(i2c->base + reg);
  200. else
  201. ret = ioread32be(i2c->base + reg);
  202. return ret;
  203. }
  204. static inline void xiic_irq_dis(struct xiic_i2c *i2c, u32 mask)
  205. {
  206. u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  207. xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier & ~mask);
  208. }
  209. static inline void xiic_irq_en(struct xiic_i2c *i2c, u32 mask)
  210. {
  211. u32 ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  212. xiic_setreg32(i2c, XIIC_IIER_OFFSET, ier | mask);
  213. }
  214. static inline void xiic_irq_clr(struct xiic_i2c *i2c, u32 mask)
  215. {
  216. u32 isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  217. xiic_setreg32(i2c, XIIC_IISR_OFFSET, isr & mask);
  218. }
  219. static inline void xiic_irq_clr_en(struct xiic_i2c *i2c, u32 mask)
  220. {
  221. xiic_irq_clr(i2c, mask);
  222. xiic_irq_en(i2c, mask);
  223. }
  224. static void xiic_clear_rx_fifo(struct xiic_i2c *i2c)
  225. {
  226. u8 sr;
  227. for (sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
  228. !(sr & XIIC_SR_RX_FIFO_EMPTY_MASK);
  229. sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET))
  230. xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
  231. }
  232. static void xiic_reinit(struct xiic_i2c *i2c)
  233. {
  234. xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
  235. /* Set receive Fifo depth to maximum (zero based). */
  236. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, IIC_RX_FIFO_DEPTH - 1);
  237. /* Reset Tx Fifo. */
  238. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
  239. /* Enable IIC Device, remove Tx Fifo reset & disable general call. */
  240. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_ENABLE_DEVICE_MASK);
  241. /* make sure RX fifo is empty */
  242. xiic_clear_rx_fifo(i2c);
  243. /* Enable interrupts */
  244. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  245. xiic_irq_clr_en(i2c, XIIC_INTR_ARB_LOST_MASK);
  246. }
  247. static void xiic_deinit(struct xiic_i2c *i2c)
  248. {
  249. u8 cr;
  250. xiic_setreg32(i2c, XIIC_RESETR_OFFSET, XIIC_RESET_MASK);
  251. /* Disable IIC Device. */
  252. cr = xiic_getreg8(i2c, XIIC_CR_REG_OFFSET);
  253. xiic_setreg8(i2c, XIIC_CR_REG_OFFSET, cr & ~XIIC_CR_ENABLE_DEVICE_MASK);
  254. }
  255. static void xiic_read_rx(struct xiic_i2c *i2c)
  256. {
  257. u8 bytes_in_fifo;
  258. int i;
  259. bytes_in_fifo = xiic_getreg8(i2c, XIIC_RFO_REG_OFFSET) + 1;
  260. dev_dbg(i2c->adap.dev.parent,
  261. "%s entry, bytes in fifo: %d, msg: %d, SR: 0x%x, CR: 0x%x\n",
  262. __func__, bytes_in_fifo, xiic_rx_space(i2c),
  263. xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
  264. xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
  265. if (bytes_in_fifo > xiic_rx_space(i2c))
  266. bytes_in_fifo = xiic_rx_space(i2c);
  267. for (i = 0; i < bytes_in_fifo; i++)
  268. i2c->rx_msg->buf[i2c->rx_pos++] =
  269. xiic_getreg8(i2c, XIIC_DRR_REG_OFFSET);
  270. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET,
  271. (xiic_rx_space(i2c) > IIC_RX_FIFO_DEPTH) ?
  272. IIC_RX_FIFO_DEPTH - 1 : xiic_rx_space(i2c) - 1);
  273. }
  274. static int xiic_tx_fifo_space(struct xiic_i2c *i2c)
  275. {
  276. /* return the actual space left in the FIFO */
  277. return IIC_TX_FIFO_DEPTH - xiic_getreg8(i2c, XIIC_TFO_REG_OFFSET) - 1;
  278. }
  279. static void xiic_fill_tx_fifo(struct xiic_i2c *i2c)
  280. {
  281. u8 fifo_space = xiic_tx_fifo_space(i2c);
  282. int len = xiic_tx_space(i2c);
  283. len = (len > fifo_space) ? fifo_space : len;
  284. dev_dbg(i2c->adap.dev.parent, "%s entry, len: %d, fifo space: %d\n",
  285. __func__, len, fifo_space);
  286. while (len--) {
  287. u16 data = i2c->tx_msg->buf[i2c->tx_pos++];
  288. if ((xiic_tx_space(i2c) == 0) && (i2c->nmsgs == 1)) {
  289. /* last message in transfer -> STOP */
  290. data |= XIIC_TX_DYN_STOP_MASK;
  291. dev_dbg(i2c->adap.dev.parent, "%s TX STOP\n", __func__);
  292. }
  293. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
  294. }
  295. }
  296. static void xiic_wakeup(struct xiic_i2c *i2c, int code)
  297. {
  298. i2c->tx_msg = NULL;
  299. i2c->rx_msg = NULL;
  300. i2c->nmsgs = 0;
  301. i2c->state = code;
  302. wake_up(&i2c->wait);
  303. }
  304. static irqreturn_t xiic_process(int irq, void *dev_id)
  305. {
  306. struct xiic_i2c *i2c = dev_id;
  307. u32 pend, isr, ier;
  308. u32 clr = 0;
  309. /* Get the interrupt Status from the IPIF. There is no clearing of
  310. * interrupts in the IPIF. Interrupts must be cleared at the source.
  311. * To find which interrupts are pending; AND interrupts pending with
  312. * interrupts masked.
  313. */
  314. spin_lock(&i2c->lock);
  315. isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  316. ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  317. pend = isr & ier;
  318. dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n",
  319. __func__, ier, isr, pend);
  320. dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n",
  321. __func__, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
  322. i2c->tx_msg, i2c->nmsgs);
  323. /* Service requesting interrupt */
  324. if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
  325. ((pend & XIIC_INTR_TX_ERROR_MASK) &&
  326. !(pend & XIIC_INTR_RX_FULL_MASK))) {
  327. /* bus arbritration lost, or...
  328. * Transmit error _OR_ RX completed
  329. * if this happens when RX_FULL is not set
  330. * this is probably a TX error
  331. */
  332. dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
  333. /* dynamic mode seem to suffer from problems if we just flushes
  334. * fifos and the next message is a TX with len 0 (only addr)
  335. * reset the IP instead of just flush fifos
  336. */
  337. xiic_reinit(i2c);
  338. if (i2c->rx_msg)
  339. xiic_wakeup(i2c, STATE_ERROR);
  340. if (i2c->tx_msg)
  341. xiic_wakeup(i2c, STATE_ERROR);
  342. }
  343. if (pend & XIIC_INTR_RX_FULL_MASK) {
  344. /* Receive register/FIFO is full */
  345. clr |= XIIC_INTR_RX_FULL_MASK;
  346. if (!i2c->rx_msg) {
  347. dev_dbg(i2c->adap.dev.parent,
  348. "%s unexpexted RX IRQ\n", __func__);
  349. xiic_clear_rx_fifo(i2c);
  350. goto out;
  351. }
  352. xiic_read_rx(i2c);
  353. if (xiic_rx_space(i2c) == 0) {
  354. /* this is the last part of the message */
  355. i2c->rx_msg = NULL;
  356. /* also clear TX error if there (RX complete) */
  357. clr |= (isr & XIIC_INTR_TX_ERROR_MASK);
  358. dev_dbg(i2c->adap.dev.parent,
  359. "%s end of message, nmsgs: %d\n",
  360. __func__, i2c->nmsgs);
  361. /* send next message if this wasn't the last,
  362. * otherwise the transfer will be finialise when
  363. * receiving the bus not busy interrupt
  364. */
  365. if (i2c->nmsgs > 1) {
  366. i2c->nmsgs--;
  367. i2c->tx_msg++;
  368. dev_dbg(i2c->adap.dev.parent,
  369. "%s will start next...\n", __func__);
  370. __xiic_start_xfer(i2c);
  371. }
  372. }
  373. }
  374. if (pend & XIIC_INTR_BNB_MASK) {
  375. /* IIC bus has transitioned to not busy */
  376. clr |= XIIC_INTR_BNB_MASK;
  377. /* The bus is not busy, disable BusNotBusy interrupt */
  378. xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
  379. if (!i2c->tx_msg)
  380. goto out;
  381. if ((i2c->nmsgs == 1) && !i2c->rx_msg &&
  382. xiic_tx_space(i2c) == 0)
  383. xiic_wakeup(i2c, STATE_DONE);
  384. else
  385. xiic_wakeup(i2c, STATE_ERROR);
  386. }
  387. if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
  388. /* Transmit register/FIFO is empty or ½ empty */
  389. clr |= (pend &
  390. (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK));
  391. if (!i2c->tx_msg) {
  392. dev_dbg(i2c->adap.dev.parent,
  393. "%s unexpexted TX IRQ\n", __func__);
  394. goto out;
  395. }
  396. xiic_fill_tx_fifo(i2c);
  397. /* current message sent and there is space in the fifo */
  398. if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
  399. dev_dbg(i2c->adap.dev.parent,
  400. "%s end of message sent, nmsgs: %d\n",
  401. __func__, i2c->nmsgs);
  402. if (i2c->nmsgs > 1) {
  403. i2c->nmsgs--;
  404. i2c->tx_msg++;
  405. __xiic_start_xfer(i2c);
  406. } else {
  407. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  408. dev_dbg(i2c->adap.dev.parent,
  409. "%s Got TX IRQ but no more to do...\n",
  410. __func__);
  411. }
  412. } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
  413. /* current frame is sent and is last,
  414. * make sure to disable tx half
  415. */
  416. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  417. }
  418. out:
  419. dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
  420. xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
  421. spin_unlock(&i2c->lock);
  422. return IRQ_HANDLED;
  423. }
  424. static int xiic_bus_busy(struct xiic_i2c *i2c)
  425. {
  426. u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
  427. return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
  428. }
  429. static int xiic_busy(struct xiic_i2c *i2c)
  430. {
  431. int tries = 3;
  432. int err;
  433. if (i2c->tx_msg)
  434. return -EBUSY;
  435. /* for instance if previous transfer was terminated due to TX error
  436. * it might be that the bus is on it's way to become available
  437. * give it at most 3 ms to wake
  438. */
  439. err = xiic_bus_busy(i2c);
  440. while (err && tries--) {
  441. msleep(1);
  442. err = xiic_bus_busy(i2c);
  443. }
  444. return err;
  445. }
  446. static void xiic_start_recv(struct xiic_i2c *i2c)
  447. {
  448. u8 rx_watermark;
  449. struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
  450. unsigned long flags;
  451. /* Clear and enable Rx full interrupt. */
  452. xiic_irq_clr_en(i2c, XIIC_INTR_RX_FULL_MASK | XIIC_INTR_TX_ERROR_MASK);
  453. /* we want to get all but last byte, because the TX_ERROR IRQ is used
  454. * to inidicate error ACK on the address, and negative ack on the last
  455. * received byte, so to not mix them receive all but last.
  456. * In the case where there is only one byte to receive
  457. * we can check if ERROR and RX full is set at the same time
  458. */
  459. rx_watermark = msg->len;
  460. if (rx_watermark > IIC_RX_FIFO_DEPTH)
  461. rx_watermark = IIC_RX_FIFO_DEPTH;
  462. xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rx_watermark - 1);
  463. local_irq_save(flags);
  464. if (!(msg->flags & I2C_M_NOSTART))
  465. /* write the address */
  466. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  467. (msg->addr << 1) | XIIC_READ_OPERATION |
  468. XIIC_TX_DYN_START_MASK);
  469. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  470. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  471. msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));
  472. local_irq_restore(flags);
  473. if (i2c->nmsgs == 1)
  474. /* very last, enable bus not busy as well */
  475. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  476. /* the message is tx:ed */
  477. i2c->tx_pos = msg->len;
  478. }
  479. static void xiic_start_send(struct xiic_i2c *i2c)
  480. {
  481. struct i2c_msg *msg = i2c->tx_msg;
  482. xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
  483. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d",
  484. __func__, msg, msg->len);
  485. dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
  486. __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
  487. xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
  488. if (!(msg->flags & I2C_M_NOSTART)) {
  489. /* write the address */
  490. u16 data = ((msg->addr << 1) & 0xfe) | XIIC_WRITE_OPERATION |
  491. XIIC_TX_DYN_START_MASK;
  492. if ((i2c->nmsgs == 1) && msg->len == 0)
  493. /* no data and last message -> add STOP */
  494. data |= XIIC_TX_DYN_STOP_MASK;
  495. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
  496. }
  497. xiic_fill_tx_fifo(i2c);
  498. /* Clear any pending Tx empty, Tx Error and then enable them. */
  499. xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |
  500. XIIC_INTR_BNB_MASK);
  501. }
  502. static irqreturn_t xiic_isr(int irq, void *dev_id)
  503. {
  504. struct xiic_i2c *i2c = dev_id;
  505. u32 pend, isr, ier;
  506. irqreturn_t ret = IRQ_NONE;
  507. /* Do not processes a devices interrupts if the device has no
  508. * interrupts pending
  509. */
  510. dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);
  511. isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  512. ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  513. pend = isr & ier;
  514. if (pend)
  515. ret = IRQ_WAKE_THREAD;
  516. return ret;
  517. }
  518. static void __xiic_start_xfer(struct xiic_i2c *i2c)
  519. {
  520. int first = 1;
  521. int fifo_space = xiic_tx_fifo_space(i2c);
  522. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
  523. __func__, i2c->tx_msg, fifo_space);
  524. if (!i2c->tx_msg)
  525. return;
  526. i2c->rx_pos = 0;
  527. i2c->tx_pos = 0;
  528. i2c->state = STATE_START;
  529. while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {
  530. if (!first) {
  531. i2c->nmsgs--;
  532. i2c->tx_msg++;
  533. i2c->tx_pos = 0;
  534. } else
  535. first = 0;
  536. if (i2c->tx_msg->flags & I2C_M_RD) {
  537. /* we dont date putting several reads in the FIFO */
  538. xiic_start_recv(i2c);
  539. return;
  540. } else {
  541. xiic_start_send(i2c);
  542. if (xiic_tx_space(i2c) != 0) {
  543. /* the message could not be completely sent */
  544. break;
  545. }
  546. }
  547. fifo_space = xiic_tx_fifo_space(i2c);
  548. }
  549. /* there are more messages or the current one could not be completely
  550. * put into the FIFO, also enable the half empty interrupt
  551. */
  552. if (i2c->nmsgs > 1 || xiic_tx_space(i2c))
  553. xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);
  554. }
  555. static void xiic_start_xfer(struct xiic_i2c *i2c)
  556. {
  557. spin_lock(&i2c->lock);
  558. xiic_reinit(i2c);
  559. __xiic_start_xfer(i2c);
  560. spin_unlock(&i2c->lock);
  561. }
  562. static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  563. {
  564. struct xiic_i2c *i2c = i2c_get_adapdata(adap);
  565. int err;
  566. dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
  567. xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
  568. err = xiic_busy(i2c);
  569. if (err)
  570. return err;
  571. i2c->tx_msg = msgs;
  572. i2c->nmsgs = num;
  573. xiic_start_xfer(i2c);
  574. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  575. (i2c->state == STATE_DONE), HZ))
  576. return (i2c->state == STATE_DONE) ? num : -EIO;
  577. else {
  578. i2c->tx_msg = NULL;
  579. i2c->rx_msg = NULL;
  580. i2c->nmsgs = 0;
  581. return -ETIMEDOUT;
  582. }
  583. }
  584. static u32 xiic_func(struct i2c_adapter *adap)
  585. {
  586. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  587. }
  588. static const struct i2c_algorithm xiic_algorithm = {
  589. .master_xfer = xiic_xfer,
  590. .functionality = xiic_func,
  591. };
  592. static struct i2c_adapter xiic_adapter = {
  593. .owner = THIS_MODULE,
  594. .name = DRIVER_NAME,
  595. .class = I2C_CLASS_DEPRECATED,
  596. .algo = &xiic_algorithm,
  597. };
  598. static int xiic_i2c_probe(struct platform_device *pdev)
  599. {
  600. struct xiic_i2c *i2c;
  601. struct xiic_i2c_platform_data *pdata;
  602. struct resource *res;
  603. int ret, irq;
  604. u8 i;
  605. u32 sr;
  606. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  607. if (!i2c)
  608. return -ENOMEM;
  609. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  610. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  611. if (IS_ERR(i2c->base))
  612. return PTR_ERR(i2c->base);
  613. irq = platform_get_irq(pdev, 0);
  614. if (irq < 0)
  615. return irq;
  616. pdata = dev_get_platdata(&pdev->dev);
  617. /* hook up driver to tree */
  618. platform_set_drvdata(pdev, i2c);
  619. i2c->adap = xiic_adapter;
  620. i2c_set_adapdata(&i2c->adap, i2c);
  621. i2c->adap.dev.parent = &pdev->dev;
  622. i2c->adap.dev.of_node = pdev->dev.of_node;
  623. spin_lock_init(&i2c->lock);
  624. init_waitqueue_head(&i2c->wait);
  625. ret = devm_request_threaded_irq(&pdev->dev, irq, xiic_isr,
  626. xiic_process, IRQF_ONESHOT,
  627. pdev->name, i2c);
  628. if (ret < 0) {
  629. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  630. return ret;
  631. }
  632. /*
  633. * Detect endianness
  634. * Try to reset the TX FIFO. Then check the EMPTY flag. If it is not
  635. * set, assume that the endianness was wrong and swap.
  636. */
  637. i2c->endianness = LITTLE;
  638. xiic_setreg32(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
  639. /* Reset is cleared in xiic_reinit */
  640. sr = xiic_getreg32(i2c, XIIC_SR_REG_OFFSET);
  641. if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK))
  642. i2c->endianness = BIG;
  643. xiic_reinit(i2c);
  644. /* add i2c adapter to i2c tree */
  645. ret = i2c_add_adapter(&i2c->adap);
  646. if (ret) {
  647. dev_err(&pdev->dev, "Failed to add adapter\n");
  648. xiic_deinit(i2c);
  649. return ret;
  650. }
  651. if (pdata) {
  652. /* add in known devices to the bus */
  653. for (i = 0; i < pdata->num_devices; i++)
  654. i2c_new_device(&i2c->adap, pdata->devices + i);
  655. }
  656. return 0;
  657. }
  658. static int xiic_i2c_remove(struct platform_device *pdev)
  659. {
  660. struct xiic_i2c *i2c = platform_get_drvdata(pdev);
  661. /* remove adapter & data */
  662. i2c_del_adapter(&i2c->adap);
  663. xiic_deinit(i2c);
  664. return 0;
  665. }
  666. #if defined(CONFIG_OF)
  667. static const struct of_device_id xiic_of_match[] = {
  668. { .compatible = "xlnx,xps-iic-2.00.a", },
  669. {},
  670. };
  671. MODULE_DEVICE_TABLE(of, xiic_of_match);
  672. #endif
  673. static struct platform_driver xiic_i2c_driver = {
  674. .probe = xiic_i2c_probe,
  675. .remove = xiic_i2c_remove,
  676. .driver = {
  677. .name = DRIVER_NAME,
  678. .of_match_table = of_match_ptr(xiic_of_match),
  679. },
  680. };
  681. module_platform_driver(xiic_i2c_driver);
  682. MODULE_AUTHOR("info@mocean-labs.com");
  683. MODULE_DESCRIPTION("Xilinx I2C bus driver");
  684. MODULE_LICENSE("GPL v2");
  685. MODULE_ALIAS("platform:"DRIVER_NAME);