i2c-xiic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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_AAS_MASK | 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 void xiic_process(struct xiic_i2c *i2c)
  305. {
  306. u32 pend, isr, ier;
  307. u32 clr = 0;
  308. /* Get the interrupt Status from the IPIF. There is no clearing of
  309. * interrupts in the IPIF. Interrupts must be cleared at the source.
  310. * To find which interrupts are pending; AND interrupts pending with
  311. * interrupts masked.
  312. */
  313. isr = xiic_getreg32(i2c, XIIC_IISR_OFFSET);
  314. ier = xiic_getreg32(i2c, XIIC_IIER_OFFSET);
  315. pend = isr & ier;
  316. dev_dbg(i2c->adap.dev.parent, "%s: IER: 0x%x, ISR: 0x%x, pend: 0x%x\n",
  317. __func__, ier, isr, pend);
  318. dev_dbg(i2c->adap.dev.parent, "%s: SR: 0x%x, msg: %p, nmsgs: %d\n",
  319. __func__, xiic_getreg8(i2c, XIIC_SR_REG_OFFSET),
  320. i2c->tx_msg, i2c->nmsgs);
  321. /* Do not processes a devices interrupts if the device has no
  322. * interrupts pending
  323. */
  324. if (!pend)
  325. return;
  326. /* Service requesting interrupt */
  327. if ((pend & XIIC_INTR_ARB_LOST_MASK) ||
  328. ((pend & XIIC_INTR_TX_ERROR_MASK) &&
  329. !(pend & XIIC_INTR_RX_FULL_MASK))) {
  330. /* bus arbritration lost, or...
  331. * Transmit error _OR_ RX completed
  332. * if this happens when RX_FULL is not set
  333. * this is probably a TX error
  334. */
  335. dev_dbg(i2c->adap.dev.parent, "%s error\n", __func__);
  336. /* dynamic mode seem to suffer from problems if we just flushes
  337. * fifos and the next message is a TX with len 0 (only addr)
  338. * reset the IP instead of just flush fifos
  339. */
  340. xiic_reinit(i2c);
  341. if (i2c->tx_msg)
  342. xiic_wakeup(i2c, STATE_ERROR);
  343. } else 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. } else if (pend & XIIC_INTR_BNB_MASK) {
  374. /* IIC bus has transitioned to not busy */
  375. clr = XIIC_INTR_BNB_MASK;
  376. /* The bus is not busy, disable BusNotBusy interrupt */
  377. xiic_irq_dis(i2c, XIIC_INTR_BNB_MASK);
  378. if (!i2c->tx_msg)
  379. goto out;
  380. if ((i2c->nmsgs == 1) && !i2c->rx_msg &&
  381. xiic_tx_space(i2c) == 0)
  382. xiic_wakeup(i2c, STATE_DONE);
  383. else
  384. xiic_wakeup(i2c, STATE_ERROR);
  385. } else if (pend & (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK)) {
  386. /* Transmit register/FIFO is empty or ½ empty */
  387. clr = pend &
  388. (XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_HALF_MASK);
  389. if (!i2c->tx_msg) {
  390. dev_dbg(i2c->adap.dev.parent,
  391. "%s unexpexted TX IRQ\n", __func__);
  392. goto out;
  393. }
  394. xiic_fill_tx_fifo(i2c);
  395. /* current message sent and there is space in the fifo */
  396. if (!xiic_tx_space(i2c) && xiic_tx_fifo_space(i2c) >= 2) {
  397. dev_dbg(i2c->adap.dev.parent,
  398. "%s end of message sent, nmsgs: %d\n",
  399. __func__, i2c->nmsgs);
  400. if (i2c->nmsgs > 1) {
  401. i2c->nmsgs--;
  402. i2c->tx_msg++;
  403. __xiic_start_xfer(i2c);
  404. } else {
  405. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  406. dev_dbg(i2c->adap.dev.parent,
  407. "%s Got TX IRQ but no more to do...\n",
  408. __func__);
  409. }
  410. } else if (!xiic_tx_space(i2c) && (i2c->nmsgs == 1))
  411. /* current frame is sent and is last,
  412. * make sure to disable tx half
  413. */
  414. xiic_irq_dis(i2c, XIIC_INTR_TX_HALF_MASK);
  415. } else {
  416. /* got IRQ which is not acked */
  417. dev_err(i2c->adap.dev.parent, "%s Got unexpected IRQ\n",
  418. __func__);
  419. clr = pend;
  420. }
  421. out:
  422. dev_dbg(i2c->adap.dev.parent, "%s clr: 0x%x\n", __func__, clr);
  423. xiic_setreg32(i2c, XIIC_IISR_OFFSET, clr);
  424. }
  425. static int xiic_bus_busy(struct xiic_i2c *i2c)
  426. {
  427. u8 sr = xiic_getreg8(i2c, XIIC_SR_REG_OFFSET);
  428. return (sr & XIIC_SR_BUS_BUSY_MASK) ? -EBUSY : 0;
  429. }
  430. static int xiic_busy(struct xiic_i2c *i2c)
  431. {
  432. int tries = 3;
  433. int err;
  434. if (i2c->tx_msg)
  435. return -EBUSY;
  436. /* for instance if previous transfer was terminated due to TX error
  437. * it might be that the bus is on it's way to become available
  438. * give it at most 3 ms to wake
  439. */
  440. err = xiic_bus_busy(i2c);
  441. while (err && tries--) {
  442. mdelay(1);
  443. err = xiic_bus_busy(i2c);
  444. }
  445. return err;
  446. }
  447. static void xiic_start_recv(struct xiic_i2c *i2c)
  448. {
  449. u8 rx_watermark;
  450. struct i2c_msg *msg = i2c->rx_msg = i2c->tx_msg;
  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. if (!(msg->flags & I2C_M_NOSTART))
  464. /* write the address */
  465. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  466. (msg->addr << 1) | XIIC_READ_OPERATION |
  467. XIIC_TX_DYN_START_MASK);
  468. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  469. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET,
  470. msg->len | ((i2c->nmsgs == 1) ? XIIC_TX_DYN_STOP_MASK : 0));
  471. if (i2c->nmsgs == 1)
  472. /* very last, enable bus not busy as well */
  473. xiic_irq_clr_en(i2c, XIIC_INTR_BNB_MASK);
  474. /* the message is tx:ed */
  475. i2c->tx_pos = msg->len;
  476. }
  477. static void xiic_start_send(struct xiic_i2c *i2c)
  478. {
  479. struct i2c_msg *msg = i2c->tx_msg;
  480. xiic_irq_clr(i2c, XIIC_INTR_TX_ERROR_MASK);
  481. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, len: %d",
  482. __func__, msg, msg->len);
  483. dev_dbg(i2c->adap.dev.parent, "%s entry, ISR: 0x%x, CR: 0x%x\n",
  484. __func__, xiic_getreg32(i2c, XIIC_IISR_OFFSET),
  485. xiic_getreg8(i2c, XIIC_CR_REG_OFFSET));
  486. if (!(msg->flags & I2C_M_NOSTART)) {
  487. /* write the address */
  488. u16 data = ((msg->addr << 1) & 0xfe) | XIIC_WRITE_OPERATION |
  489. XIIC_TX_DYN_START_MASK;
  490. if ((i2c->nmsgs == 1) && msg->len == 0)
  491. /* no data and last message -> add STOP */
  492. data |= XIIC_TX_DYN_STOP_MASK;
  493. xiic_setreg16(i2c, XIIC_DTR_REG_OFFSET, data);
  494. }
  495. xiic_fill_tx_fifo(i2c);
  496. /* Clear any pending Tx empty, Tx Error and then enable them. */
  497. xiic_irq_clr_en(i2c, XIIC_INTR_TX_EMPTY_MASK | XIIC_INTR_TX_ERROR_MASK |
  498. XIIC_INTR_BNB_MASK);
  499. }
  500. static irqreturn_t xiic_isr(int irq, void *dev_id)
  501. {
  502. struct xiic_i2c *i2c = dev_id;
  503. spin_lock(&i2c->lock);
  504. /* disable interrupts globally */
  505. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);
  506. dev_dbg(i2c->adap.dev.parent, "%s entry\n", __func__);
  507. xiic_process(i2c);
  508. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  509. spin_unlock(&i2c->lock);
  510. return IRQ_HANDLED;
  511. }
  512. static void __xiic_start_xfer(struct xiic_i2c *i2c)
  513. {
  514. int first = 1;
  515. int fifo_space = xiic_tx_fifo_space(i2c);
  516. dev_dbg(i2c->adap.dev.parent, "%s entry, msg: %p, fifos space: %d\n",
  517. __func__, i2c->tx_msg, fifo_space);
  518. if (!i2c->tx_msg)
  519. return;
  520. i2c->rx_pos = 0;
  521. i2c->tx_pos = 0;
  522. i2c->state = STATE_START;
  523. while ((fifo_space >= 2) && (first || (i2c->nmsgs > 1))) {
  524. if (!first) {
  525. i2c->nmsgs--;
  526. i2c->tx_msg++;
  527. i2c->tx_pos = 0;
  528. } else
  529. first = 0;
  530. if (i2c->tx_msg->flags & I2C_M_RD) {
  531. /* we dont date putting several reads in the FIFO */
  532. xiic_start_recv(i2c);
  533. return;
  534. } else {
  535. xiic_start_send(i2c);
  536. if (xiic_tx_space(i2c) != 0) {
  537. /* the message could not be completely sent */
  538. break;
  539. }
  540. }
  541. fifo_space = xiic_tx_fifo_space(i2c);
  542. }
  543. /* there are more messages or the current one could not be completely
  544. * put into the FIFO, also enable the half empty interrupt
  545. */
  546. if (i2c->nmsgs > 1 || xiic_tx_space(i2c))
  547. xiic_irq_clr_en(i2c, XIIC_INTR_TX_HALF_MASK);
  548. }
  549. static void xiic_start_xfer(struct xiic_i2c *i2c)
  550. {
  551. unsigned long flags;
  552. spin_lock_irqsave(&i2c->lock, flags);
  553. xiic_reinit(i2c);
  554. /* disable interrupts globally */
  555. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, 0);
  556. spin_unlock_irqrestore(&i2c->lock, flags);
  557. __xiic_start_xfer(i2c);
  558. xiic_setreg32(i2c, XIIC_DGIER_OFFSET, XIIC_GINTR_ENABLE_MASK);
  559. }
  560. static int xiic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  561. {
  562. struct xiic_i2c *i2c = i2c_get_adapdata(adap);
  563. int err;
  564. dev_dbg(adap->dev.parent, "%s entry SR: 0x%x\n", __func__,
  565. xiic_getreg8(i2c, XIIC_SR_REG_OFFSET));
  566. err = xiic_busy(i2c);
  567. if (err)
  568. return err;
  569. i2c->tx_msg = msgs;
  570. i2c->nmsgs = num;
  571. xiic_start_xfer(i2c);
  572. if (wait_event_timeout(i2c->wait, (i2c->state == STATE_ERROR) ||
  573. (i2c->state == STATE_DONE), HZ))
  574. return (i2c->state == STATE_DONE) ? num : -EIO;
  575. else {
  576. i2c->tx_msg = NULL;
  577. i2c->rx_msg = NULL;
  578. i2c->nmsgs = 0;
  579. return -ETIMEDOUT;
  580. }
  581. }
  582. static u32 xiic_func(struct i2c_adapter *adap)
  583. {
  584. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  585. }
  586. static const struct i2c_algorithm xiic_algorithm = {
  587. .master_xfer = xiic_xfer,
  588. .functionality = xiic_func,
  589. };
  590. static struct i2c_adapter xiic_adapter = {
  591. .owner = THIS_MODULE,
  592. .name = DRIVER_NAME,
  593. .class = I2C_CLASS_DEPRECATED,
  594. .algo = &xiic_algorithm,
  595. };
  596. static int xiic_i2c_probe(struct platform_device *pdev)
  597. {
  598. struct xiic_i2c *i2c;
  599. struct xiic_i2c_platform_data *pdata;
  600. struct resource *res;
  601. int ret, irq;
  602. u8 i;
  603. u32 sr;
  604. i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
  605. if (!i2c)
  606. return -ENOMEM;
  607. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  608. i2c->base = devm_ioremap_resource(&pdev->dev, res);
  609. if (IS_ERR(i2c->base))
  610. return PTR_ERR(i2c->base);
  611. irq = platform_get_irq(pdev, 0);
  612. if (irq < 0)
  613. return irq;
  614. pdata = dev_get_platdata(&pdev->dev);
  615. /* hook up driver to tree */
  616. platform_set_drvdata(pdev, i2c);
  617. i2c->adap = xiic_adapter;
  618. i2c_set_adapdata(&i2c->adap, i2c);
  619. i2c->adap.dev.parent = &pdev->dev;
  620. i2c->adap.dev.of_node = pdev->dev.of_node;
  621. spin_lock_init(&i2c->lock);
  622. init_waitqueue_head(&i2c->wait);
  623. ret = devm_request_irq(&pdev->dev, irq, xiic_isr, 0, pdev->name, i2c);
  624. if (ret < 0) {
  625. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  626. return ret;
  627. }
  628. /*
  629. * Detect endianness
  630. * Try to reset the TX FIFO. Then check the EMPTY flag. If it is not
  631. * set, assume that the endianness was wrong and swap.
  632. */
  633. i2c->endianness = LITTLE;
  634. xiic_setreg32(i2c, XIIC_CR_REG_OFFSET, XIIC_CR_TX_FIFO_RESET_MASK);
  635. /* Reset is cleared in xiic_reinit */
  636. sr = xiic_getreg32(i2c, XIIC_SR_REG_OFFSET);
  637. if (!(sr & XIIC_SR_TX_FIFO_EMPTY_MASK))
  638. i2c->endianness = BIG;
  639. xiic_reinit(i2c);
  640. /* add i2c adapter to i2c tree */
  641. ret = i2c_add_adapter(&i2c->adap);
  642. if (ret) {
  643. dev_err(&pdev->dev, "Failed to add adapter\n");
  644. xiic_deinit(i2c);
  645. return ret;
  646. }
  647. if (pdata) {
  648. /* add in known devices to the bus */
  649. for (i = 0; i < pdata->num_devices; i++)
  650. i2c_new_device(&i2c->adap, pdata->devices + i);
  651. }
  652. return 0;
  653. }
  654. static int xiic_i2c_remove(struct platform_device *pdev)
  655. {
  656. struct xiic_i2c *i2c = platform_get_drvdata(pdev);
  657. /* remove adapter & data */
  658. i2c_del_adapter(&i2c->adap);
  659. xiic_deinit(i2c);
  660. return 0;
  661. }
  662. #if defined(CONFIG_OF)
  663. static const struct of_device_id xiic_of_match[] = {
  664. { .compatible = "xlnx,xps-iic-2.00.a", },
  665. {},
  666. };
  667. MODULE_DEVICE_TABLE(of, xiic_of_match);
  668. #endif
  669. static struct platform_driver xiic_i2c_driver = {
  670. .probe = xiic_i2c_probe,
  671. .remove = xiic_i2c_remove,
  672. .driver = {
  673. .name = DRIVER_NAME,
  674. .of_match_table = of_match_ptr(xiic_of_match),
  675. },
  676. };
  677. module_platform_driver(xiic_i2c_driver);
  678. MODULE_AUTHOR("info@mocean-labs.com");
  679. MODULE_DESCRIPTION("Xilinx I2C bus driver");
  680. MODULE_LICENSE("GPL v2");
  681. MODULE_ALIAS("platform:"DRIVER_NAME);