i2c-xiic.c 24 KB

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