i2c-xiic.c 23 KB

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