i2c-xiic.c 22 KB

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