i2c-xiic.c 25 KB

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