i2c-cadence.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * I2C bus driver for the Cadence I2C controller.
  3. *
  4. * Copyright (C) 2009 - 2014 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation;
  9. * either version 2 of the License, or (at your option) any
  10. * later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of.h>
  20. /* Register offsets for the I2C device. */
  21. #define CDNS_I2C_CR_OFFSET 0x00 /* Control Register, RW */
  22. #define CDNS_I2C_SR_OFFSET 0x04 /* Status Register, RO */
  23. #define CDNS_I2C_ADDR_OFFSET 0x08 /* I2C Address Register, RW */
  24. #define CDNS_I2C_DATA_OFFSET 0x0C /* I2C Data Register, RW */
  25. #define CDNS_I2C_ISR_OFFSET 0x10 /* IRQ Status Register, RW */
  26. #define CDNS_I2C_XFER_SIZE_OFFSET 0x14 /* Transfer Size Register, RW */
  27. #define CDNS_I2C_TIME_OUT_OFFSET 0x1C /* Time Out Register, RW */
  28. #define CDNS_I2C_IER_OFFSET 0x24 /* IRQ Enable Register, WO */
  29. #define CDNS_I2C_IDR_OFFSET 0x28 /* IRQ Disable Register, WO */
  30. /* Control Register Bit mask definitions */
  31. #define CDNS_I2C_CR_HOLD BIT(4) /* Hold Bus bit */
  32. #define CDNS_I2C_CR_ACK_EN BIT(3)
  33. #define CDNS_I2C_CR_NEA BIT(2)
  34. #define CDNS_I2C_CR_MS BIT(1)
  35. /* Read or Write Master transfer 0 = Transmitter, 1 = Receiver */
  36. #define CDNS_I2C_CR_RW BIT(0)
  37. /* 1 = Auto init FIFO to zeroes */
  38. #define CDNS_I2C_CR_CLR_FIFO BIT(6)
  39. #define CDNS_I2C_CR_DIVA_SHIFT 14
  40. #define CDNS_I2C_CR_DIVA_MASK (3 << CDNS_I2C_CR_DIVA_SHIFT)
  41. #define CDNS_I2C_CR_DIVB_SHIFT 8
  42. #define CDNS_I2C_CR_DIVB_MASK (0x3f << CDNS_I2C_CR_DIVB_SHIFT)
  43. /* Status Register Bit mask definitions */
  44. #define CDNS_I2C_SR_BA BIT(8)
  45. #define CDNS_I2C_SR_RXDV BIT(5)
  46. /*
  47. * I2C Address Register Bit mask definitions
  48. * Normal addressing mode uses [6:0] bits. Extended addressing mode uses [9:0]
  49. * bits. A write access to this register always initiates a transfer if the I2C
  50. * is in master mode.
  51. */
  52. #define CDNS_I2C_ADDR_MASK 0x000003FF /* I2C Address Mask */
  53. /*
  54. * I2C Interrupt Registers Bit mask definitions
  55. * All the four interrupt registers (Status/Mask/Enable/Disable) have the same
  56. * bit definitions.
  57. */
  58. #define CDNS_I2C_IXR_ARB_LOST BIT(9)
  59. #define CDNS_I2C_IXR_RX_UNF BIT(7)
  60. #define CDNS_I2C_IXR_TX_OVF BIT(6)
  61. #define CDNS_I2C_IXR_RX_OVF BIT(5)
  62. #define CDNS_I2C_IXR_SLV_RDY BIT(4)
  63. #define CDNS_I2C_IXR_TO BIT(3)
  64. #define CDNS_I2C_IXR_NACK BIT(2)
  65. #define CDNS_I2C_IXR_DATA BIT(1)
  66. #define CDNS_I2C_IXR_COMP BIT(0)
  67. #define CDNS_I2C_IXR_ALL_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  68. CDNS_I2C_IXR_RX_UNF | \
  69. CDNS_I2C_IXR_TX_OVF | \
  70. CDNS_I2C_IXR_RX_OVF | \
  71. CDNS_I2C_IXR_SLV_RDY | \
  72. CDNS_I2C_IXR_TO | \
  73. CDNS_I2C_IXR_NACK | \
  74. CDNS_I2C_IXR_DATA | \
  75. CDNS_I2C_IXR_COMP)
  76. #define CDNS_I2C_IXR_ERR_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  77. CDNS_I2C_IXR_RX_UNF | \
  78. CDNS_I2C_IXR_TX_OVF | \
  79. CDNS_I2C_IXR_RX_OVF | \
  80. CDNS_I2C_IXR_NACK)
  81. #define CDNS_I2C_ENABLED_INTR_MASK (CDNS_I2C_IXR_ARB_LOST | \
  82. CDNS_I2C_IXR_RX_UNF | \
  83. CDNS_I2C_IXR_TX_OVF | \
  84. CDNS_I2C_IXR_RX_OVF | \
  85. CDNS_I2C_IXR_NACK | \
  86. CDNS_I2C_IXR_DATA | \
  87. CDNS_I2C_IXR_COMP)
  88. #define CDNS_I2C_TIMEOUT msecs_to_jiffies(1000)
  89. #define CDNS_I2C_FIFO_DEPTH 16
  90. /* FIFO depth at which the DATA interrupt occurs */
  91. #define CDNS_I2C_DATA_INTR_DEPTH (CDNS_I2C_FIFO_DEPTH - 2)
  92. #define CDNS_I2C_MAX_TRANSFER_SIZE 255
  93. /* Transfer size in multiples of data interrupt depth */
  94. #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_MAX_TRANSFER_SIZE - 3)
  95. #define DRIVER_NAME "cdns-i2c"
  96. #define CDNS_I2C_SPEED_MAX 400000
  97. #define CDNS_I2C_SPEED_DEFAULT 100000
  98. #define CDNS_I2C_DIVA_MAX 4
  99. #define CDNS_I2C_DIVB_MAX 64
  100. #define CDNS_I2C_TIMEOUT_MAX 0xFF
  101. #define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
  102. #define cdns_i2c_readreg(offset) readl_relaxed(id->membase + offset)
  103. #define cdns_i2c_writereg(val, offset) writel_relaxed(val, id->membase + offset)
  104. /**
  105. * struct cdns_i2c - I2C device private data structure
  106. * @membase: Base address of the I2C device
  107. * @adap: I2C adapter instance
  108. * @p_msg: Message pointer
  109. * @err_status: Error status in Interrupt Status Register
  110. * @xfer_done: Transfer complete status
  111. * @p_send_buf: Pointer to transmit buffer
  112. * @p_recv_buf: Pointer to receive buffer
  113. * @suspended: Flag holding the device's PM status
  114. * @send_count: Number of bytes still expected to send
  115. * @recv_count: Number of bytes still expected to receive
  116. * @curr_recv_count: Number of bytes to be received in current transfer
  117. * @irq: IRQ number
  118. * @input_clk: Input clock to I2C controller
  119. * @i2c_clk: Maximum I2C clock speed
  120. * @bus_hold_flag: Flag used in repeated start for clearing HOLD bit
  121. * @clk: Pointer to struct clk
  122. * @clk_rate_change_nb: Notifier block for clock rate changes
  123. * @quirks: flag for broken hold bit usage in r1p10
  124. */
  125. struct cdns_i2c {
  126. void __iomem *membase;
  127. struct i2c_adapter adap;
  128. struct i2c_msg *p_msg;
  129. int err_status;
  130. struct completion xfer_done;
  131. unsigned char *p_send_buf;
  132. unsigned char *p_recv_buf;
  133. u8 suspended;
  134. unsigned int send_count;
  135. unsigned int recv_count;
  136. unsigned int curr_recv_count;
  137. int irq;
  138. unsigned long input_clk;
  139. unsigned int i2c_clk;
  140. unsigned int bus_hold_flag;
  141. struct clk *clk;
  142. struct notifier_block clk_rate_change_nb;
  143. u32 quirks;
  144. };
  145. struct cdns_platform_data {
  146. u32 quirks;
  147. };
  148. #define to_cdns_i2c(_nb) container_of(_nb, struct cdns_i2c, \
  149. clk_rate_change_nb)
  150. /**
  151. * cdns_i2c_clear_bus_hold() - Clear bus hold bit
  152. * @id: Pointer to driver data struct
  153. *
  154. * Helper to clear the controller's bus hold bit.
  155. */
  156. static void cdns_i2c_clear_bus_hold(struct cdns_i2c *id)
  157. {
  158. u32 reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  159. if (reg & CDNS_I2C_CR_HOLD)
  160. cdns_i2c_writereg(reg & ~CDNS_I2C_CR_HOLD, CDNS_I2C_CR_OFFSET);
  161. }
  162. static inline bool cdns_is_holdquirk(struct cdns_i2c *id, bool hold_wrkaround)
  163. {
  164. return (hold_wrkaround &&
  165. (id->curr_recv_count == CDNS_I2C_FIFO_DEPTH + 1));
  166. }
  167. /**
  168. * cdns_i2c_isr - Interrupt handler for the I2C device
  169. * @irq: irq number for the I2C device
  170. * @ptr: void pointer to cdns_i2c structure
  171. *
  172. * This function handles the data interrupt, transfer complete interrupt and
  173. * the error interrupts of the I2C device.
  174. *
  175. * Return: IRQ_HANDLED always
  176. */
  177. static irqreturn_t cdns_i2c_isr(int irq, void *ptr)
  178. {
  179. unsigned int isr_status, avail_bytes, updatetx;
  180. unsigned int bytes_to_send;
  181. bool hold_quirk;
  182. struct cdns_i2c *id = ptr;
  183. /* Signal completion only after everything is updated */
  184. int done_flag = 0;
  185. irqreturn_t status = IRQ_NONE;
  186. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  187. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  188. /* Handling nack and arbitration lost interrupt */
  189. if (isr_status & (CDNS_I2C_IXR_NACK | CDNS_I2C_IXR_ARB_LOST)) {
  190. done_flag = 1;
  191. status = IRQ_HANDLED;
  192. }
  193. /*
  194. * Check if transfer size register needs to be updated again for a
  195. * large data receive operation.
  196. */
  197. updatetx = 0;
  198. if (id->recv_count > id->curr_recv_count)
  199. updatetx = 1;
  200. hold_quirk = (id->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
  201. /* When receiving, handle data interrupt and completion interrupt */
  202. if (id->p_recv_buf &&
  203. ((isr_status & CDNS_I2C_IXR_COMP) ||
  204. (isr_status & CDNS_I2C_IXR_DATA))) {
  205. /* Read data if receive data valid is set */
  206. while (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) &
  207. CDNS_I2C_SR_RXDV) {
  208. /*
  209. * Clear hold bit that was set for FIFO control if
  210. * RX data left is less than FIFO depth, unless
  211. * repeated start is selected.
  212. */
  213. if ((id->recv_count < CDNS_I2C_FIFO_DEPTH) &&
  214. !id->bus_hold_flag)
  215. cdns_i2c_clear_bus_hold(id);
  216. *(id->p_recv_buf)++ =
  217. cdns_i2c_readreg(CDNS_I2C_DATA_OFFSET);
  218. id->recv_count--;
  219. id->curr_recv_count--;
  220. if (cdns_is_holdquirk(id, hold_quirk))
  221. break;
  222. }
  223. /*
  224. * The controller sends NACK to the slave when transfer size
  225. * register reaches zero without considering the HOLD bit.
  226. * This workaround is implemented for large data transfers to
  227. * maintain transfer size non-zero while performing a large
  228. * receive operation.
  229. */
  230. if (cdns_is_holdquirk(id, hold_quirk)) {
  231. /* wait while fifo is full */
  232. while (cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET) !=
  233. (id->curr_recv_count - CDNS_I2C_FIFO_DEPTH))
  234. ;
  235. /*
  236. * Check number of bytes to be received against maximum
  237. * transfer size and update register accordingly.
  238. */
  239. if (((int)(id->recv_count) - CDNS_I2C_FIFO_DEPTH) >
  240. CDNS_I2C_TRANSFER_SIZE) {
  241. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  242. CDNS_I2C_XFER_SIZE_OFFSET);
  243. id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
  244. CDNS_I2C_FIFO_DEPTH;
  245. } else {
  246. cdns_i2c_writereg(id->recv_count -
  247. CDNS_I2C_FIFO_DEPTH,
  248. CDNS_I2C_XFER_SIZE_OFFSET);
  249. id->curr_recv_count = id->recv_count;
  250. }
  251. } else if (id->recv_count && !hold_quirk &&
  252. !id->curr_recv_count) {
  253. /* Set the slave address in address register*/
  254. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  255. CDNS_I2C_ADDR_OFFSET);
  256. if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
  257. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  258. CDNS_I2C_XFER_SIZE_OFFSET);
  259. id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
  260. } else {
  261. cdns_i2c_writereg(id->recv_count,
  262. CDNS_I2C_XFER_SIZE_OFFSET);
  263. id->curr_recv_count = id->recv_count;
  264. }
  265. }
  266. /* Clear hold (if not repeated start) and signal completion */
  267. if ((isr_status & CDNS_I2C_IXR_COMP) && !id->recv_count) {
  268. if (!id->bus_hold_flag)
  269. cdns_i2c_clear_bus_hold(id);
  270. done_flag = 1;
  271. }
  272. status = IRQ_HANDLED;
  273. }
  274. /* When sending, handle transfer complete interrupt */
  275. if ((isr_status & CDNS_I2C_IXR_COMP) && !id->p_recv_buf) {
  276. /*
  277. * If there is more data to be sent, calculate the
  278. * space available in FIFO and fill with that many bytes.
  279. */
  280. if (id->send_count) {
  281. avail_bytes = CDNS_I2C_FIFO_DEPTH -
  282. cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  283. if (id->send_count > avail_bytes)
  284. bytes_to_send = avail_bytes;
  285. else
  286. bytes_to_send = id->send_count;
  287. while (bytes_to_send--) {
  288. cdns_i2c_writereg(
  289. (*(id->p_send_buf)++),
  290. CDNS_I2C_DATA_OFFSET);
  291. id->send_count--;
  292. }
  293. } else {
  294. /*
  295. * Signal the completion of transaction and
  296. * clear the hold bus bit if there are no
  297. * further messages to be processed.
  298. */
  299. done_flag = 1;
  300. }
  301. if (!id->send_count && !id->bus_hold_flag)
  302. cdns_i2c_clear_bus_hold(id);
  303. status = IRQ_HANDLED;
  304. }
  305. /* Update the status for errors */
  306. id->err_status = isr_status & CDNS_I2C_IXR_ERR_INTR_MASK;
  307. if (id->err_status)
  308. status = IRQ_HANDLED;
  309. if (done_flag)
  310. complete(&id->xfer_done);
  311. return status;
  312. }
  313. /**
  314. * cdns_i2c_mrecv - Prepare and start a master receive operation
  315. * @id: pointer to the i2c device structure
  316. */
  317. static void cdns_i2c_mrecv(struct cdns_i2c *id)
  318. {
  319. unsigned int ctrl_reg;
  320. unsigned int isr_status;
  321. id->p_recv_buf = id->p_msg->buf;
  322. id->recv_count = id->p_msg->len;
  323. /* Put the controller in master receive mode and clear the FIFO */
  324. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  325. ctrl_reg |= CDNS_I2C_CR_RW | CDNS_I2C_CR_CLR_FIFO;
  326. if (id->p_msg->flags & I2C_M_RECV_LEN)
  327. id->recv_count = I2C_SMBUS_BLOCK_MAX + 1;
  328. id->curr_recv_count = id->recv_count;
  329. /*
  330. * Check for the message size against FIFO depth and set the
  331. * 'hold bus' bit if it is greater than FIFO depth.
  332. */
  333. if (id->recv_count > CDNS_I2C_FIFO_DEPTH)
  334. ctrl_reg |= CDNS_I2C_CR_HOLD;
  335. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  336. /* Clear the interrupts in interrupt status register */
  337. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  338. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  339. /*
  340. * The no. of bytes to receive is checked against the limit of
  341. * max transfer size. Set transfer size register with no of bytes
  342. * receive if it is less than transfer size and transfer size if
  343. * it is more. Enable the interrupts.
  344. */
  345. if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
  346. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  347. CDNS_I2C_XFER_SIZE_OFFSET);
  348. id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
  349. } else {
  350. cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
  351. }
  352. /* Clear the bus hold flag if bytes to receive is less than FIFO size */
  353. if (!id->bus_hold_flag &&
  354. ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
  355. (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
  356. cdns_i2c_clear_bus_hold(id);
  357. /* Set the slave address in address register - triggers operation */
  358. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  359. CDNS_I2C_ADDR_OFFSET);
  360. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  361. }
  362. /**
  363. * cdns_i2c_msend - Prepare and start a master send operation
  364. * @id: pointer to the i2c device
  365. */
  366. static void cdns_i2c_msend(struct cdns_i2c *id)
  367. {
  368. unsigned int avail_bytes;
  369. unsigned int bytes_to_send;
  370. unsigned int ctrl_reg;
  371. unsigned int isr_status;
  372. id->p_recv_buf = NULL;
  373. id->p_send_buf = id->p_msg->buf;
  374. id->send_count = id->p_msg->len;
  375. /* Set the controller in Master transmit mode and clear the FIFO. */
  376. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  377. ctrl_reg &= ~CDNS_I2C_CR_RW;
  378. ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
  379. /*
  380. * Check for the message size against FIFO depth and set the
  381. * 'hold bus' bit if it is greater than FIFO depth.
  382. */
  383. if (id->send_count > CDNS_I2C_FIFO_DEPTH)
  384. ctrl_reg |= CDNS_I2C_CR_HOLD;
  385. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  386. /* Clear the interrupts in interrupt status register. */
  387. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  388. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  389. /*
  390. * Calculate the space available in FIFO. Check the message length
  391. * against the space available, and fill the FIFO accordingly.
  392. * Enable the interrupts.
  393. */
  394. avail_bytes = CDNS_I2C_FIFO_DEPTH -
  395. cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  396. if (id->send_count > avail_bytes)
  397. bytes_to_send = avail_bytes;
  398. else
  399. bytes_to_send = id->send_count;
  400. while (bytes_to_send--) {
  401. cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
  402. id->send_count--;
  403. }
  404. /*
  405. * Clear the bus hold flag if there is no more data
  406. * and if it is the last message.
  407. */
  408. if (!id->bus_hold_flag && !id->send_count)
  409. cdns_i2c_clear_bus_hold(id);
  410. /* Set the slave address in address register - triggers operation. */
  411. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  412. CDNS_I2C_ADDR_OFFSET);
  413. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  414. }
  415. /**
  416. * cdns_i2c_master_reset - Reset the interface
  417. * @adap: pointer to the i2c adapter driver instance
  418. *
  419. * This function cleanup the fifos, clear the hold bit and status
  420. * and disable the interrupts.
  421. */
  422. static void cdns_i2c_master_reset(struct i2c_adapter *adap)
  423. {
  424. struct cdns_i2c *id = adap->algo_data;
  425. u32 regval;
  426. /* Disable the interrupts */
  427. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
  428. /* Clear the hold bit and fifos */
  429. regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  430. regval &= ~CDNS_I2C_CR_HOLD;
  431. regval |= CDNS_I2C_CR_CLR_FIFO;
  432. cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
  433. /* Update the transfercount register to zero */
  434. cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
  435. /* Clear the interupt status register */
  436. regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  437. cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
  438. /* Clear the status register */
  439. regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
  440. cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
  441. }
  442. static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
  443. struct i2c_adapter *adap)
  444. {
  445. unsigned long time_left;
  446. u32 reg;
  447. id->p_msg = msg;
  448. id->err_status = 0;
  449. reinit_completion(&id->xfer_done);
  450. /* Check for the TEN Bit mode on each msg */
  451. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  452. if (msg->flags & I2C_M_TEN) {
  453. if (reg & CDNS_I2C_CR_NEA)
  454. cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
  455. CDNS_I2C_CR_OFFSET);
  456. } else {
  457. if (!(reg & CDNS_I2C_CR_NEA))
  458. cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
  459. CDNS_I2C_CR_OFFSET);
  460. }
  461. /* Check for the R/W flag on each msg */
  462. if (msg->flags & I2C_M_RD)
  463. cdns_i2c_mrecv(id);
  464. else
  465. cdns_i2c_msend(id);
  466. /* Wait for the signal of completion */
  467. time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
  468. if (time_left == 0) {
  469. cdns_i2c_master_reset(adap);
  470. dev_err(id->adap.dev.parent,
  471. "timeout waiting on completion\n");
  472. return -ETIMEDOUT;
  473. }
  474. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
  475. CDNS_I2C_IDR_OFFSET);
  476. /* If it is bus arbitration error, try again */
  477. if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
  478. return -EAGAIN;
  479. return 0;
  480. }
  481. /**
  482. * cdns_i2c_master_xfer - The main i2c transfer function
  483. * @adap: pointer to the i2c adapter driver instance
  484. * @msgs: pointer to the i2c message structure
  485. * @num: the number of messages to transfer
  486. *
  487. * Initiates the send/recv activity based on the transfer message received.
  488. *
  489. * Return: number of msgs processed on success, negative error otherwise
  490. */
  491. static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  492. int num)
  493. {
  494. int ret, count;
  495. u32 reg;
  496. struct cdns_i2c *id = adap->algo_data;
  497. bool hold_quirk;
  498. /* Check if the bus is free */
  499. if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA)
  500. return -EAGAIN;
  501. hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
  502. /*
  503. * Set the flag to one when multiple messages are to be
  504. * processed with a repeated start.
  505. */
  506. if (num > 1) {
  507. /*
  508. * This controller does not give completion interrupt after a
  509. * master receive message if HOLD bit is set (repeated start),
  510. * resulting in SW timeout. Hence, if a receive message is
  511. * followed by any other message, an error is returned
  512. * indicating that this sequence is not supported.
  513. */
  514. for (count = 0; (count < num - 1 && hold_quirk); count++) {
  515. if (msgs[count].flags & I2C_M_RD) {
  516. dev_warn(adap->dev.parent,
  517. "Can't do repeated start after a receive message\n");
  518. return -EOPNOTSUPP;
  519. }
  520. }
  521. id->bus_hold_flag = 1;
  522. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  523. reg |= CDNS_I2C_CR_HOLD;
  524. cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
  525. } else {
  526. id->bus_hold_flag = 0;
  527. }
  528. /* Process the msg one by one */
  529. for (count = 0; count < num; count++, msgs++) {
  530. if (count == (num - 1))
  531. id->bus_hold_flag = 0;
  532. ret = cdns_i2c_process_msg(id, msgs, adap);
  533. if (ret)
  534. return ret;
  535. /* Report the other error interrupts to application */
  536. if (id->err_status) {
  537. cdns_i2c_master_reset(adap);
  538. if (id->err_status & CDNS_I2C_IXR_NACK)
  539. return -ENXIO;
  540. return -EIO;
  541. }
  542. }
  543. return num;
  544. }
  545. /**
  546. * cdns_i2c_func - Returns the supported features of the I2C driver
  547. * @adap: pointer to the i2c adapter structure
  548. *
  549. * Return: 32 bit value, each bit corresponding to a feature
  550. */
  551. static u32 cdns_i2c_func(struct i2c_adapter *adap)
  552. {
  553. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
  554. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  555. I2C_FUNC_SMBUS_BLOCK_DATA;
  556. }
  557. static const struct i2c_algorithm cdns_i2c_algo = {
  558. .master_xfer = cdns_i2c_master_xfer,
  559. .functionality = cdns_i2c_func,
  560. };
  561. /**
  562. * cdns_i2c_calc_divs - Calculate clock dividers
  563. * @f: I2C clock frequency
  564. * @input_clk: Input clock frequency
  565. * @a: First divider (return value)
  566. * @b: Second divider (return value)
  567. *
  568. * f is used as input and output variable. As input it is used as target I2C
  569. * frequency. On function exit f holds the actually resulting I2C frequency.
  570. *
  571. * Return: 0 on success, negative errno otherwise.
  572. */
  573. static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
  574. unsigned int *a, unsigned int *b)
  575. {
  576. unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
  577. unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
  578. unsigned int last_error, current_error;
  579. /* calculate (divisor_a+1) x (divisor_b+1) */
  580. temp = input_clk / (22 * fscl);
  581. /*
  582. * If the calculated value is negative or 0, the fscl input is out of
  583. * range. Return error.
  584. */
  585. if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
  586. return -EINVAL;
  587. last_error = -1;
  588. for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
  589. div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
  590. if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
  591. continue;
  592. div_b--;
  593. actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
  594. if (actual_fscl > fscl)
  595. continue;
  596. current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
  597. (fscl - actual_fscl));
  598. if (last_error > current_error) {
  599. calc_div_a = div_a;
  600. calc_div_b = div_b;
  601. best_fscl = actual_fscl;
  602. last_error = current_error;
  603. }
  604. }
  605. *a = calc_div_a;
  606. *b = calc_div_b;
  607. *f = best_fscl;
  608. return 0;
  609. }
  610. /**
  611. * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
  612. * @clk_in: I2C clock input frequency in Hz
  613. * @id: Pointer to the I2C device structure
  614. *
  615. * The device must be idle rather than busy transferring data before setting
  616. * these device options.
  617. * The data rate is set by values in the control register.
  618. * The formula for determining the correct register values is
  619. * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
  620. * See the hardware data sheet for a full explanation of setting the serial
  621. * clock rate. The clock can not be faster than the input clock divide by 22.
  622. * The two most common clock rates are 100KHz and 400KHz.
  623. *
  624. * Return: 0 on success, negative error otherwise
  625. */
  626. static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
  627. {
  628. unsigned int div_a, div_b;
  629. unsigned int ctrl_reg;
  630. int ret = 0;
  631. unsigned long fscl = id->i2c_clk;
  632. ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
  633. if (ret)
  634. return ret;
  635. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  636. ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
  637. ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
  638. (div_b << CDNS_I2C_CR_DIVB_SHIFT));
  639. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  640. return 0;
  641. }
  642. /**
  643. * cdns_i2c_clk_notifier_cb - Clock rate change callback
  644. * @nb: Pointer to notifier block
  645. * @event: Notification reason
  646. * @data: Pointer to notification data object
  647. *
  648. * This function is called when the cdns_i2c input clock frequency changes.
  649. * The callback checks whether a valid bus frequency can be generated after the
  650. * change. If so, the change is acknowledged, otherwise the change is aborted.
  651. * New dividers are written to the HW in the pre- or post change notification
  652. * depending on the scaling direction.
  653. *
  654. * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
  655. * to acknowedge the change, NOTIFY_DONE if the notification is
  656. * considered irrelevant.
  657. */
  658. static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
  659. event, void *data)
  660. {
  661. struct clk_notifier_data *ndata = data;
  662. struct cdns_i2c *id = to_cdns_i2c(nb);
  663. if (id->suspended)
  664. return NOTIFY_OK;
  665. switch (event) {
  666. case PRE_RATE_CHANGE:
  667. {
  668. unsigned long input_clk = ndata->new_rate;
  669. unsigned long fscl = id->i2c_clk;
  670. unsigned int div_a, div_b;
  671. int ret;
  672. ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
  673. if (ret) {
  674. dev_warn(id->adap.dev.parent,
  675. "clock rate change rejected\n");
  676. return NOTIFY_STOP;
  677. }
  678. /* scale up */
  679. if (ndata->new_rate > ndata->old_rate)
  680. cdns_i2c_setclk(ndata->new_rate, id);
  681. return NOTIFY_OK;
  682. }
  683. case POST_RATE_CHANGE:
  684. id->input_clk = ndata->new_rate;
  685. /* scale down */
  686. if (ndata->new_rate < ndata->old_rate)
  687. cdns_i2c_setclk(ndata->new_rate, id);
  688. return NOTIFY_OK;
  689. case ABORT_RATE_CHANGE:
  690. /* scale up */
  691. if (ndata->new_rate > ndata->old_rate)
  692. cdns_i2c_setclk(ndata->old_rate, id);
  693. return NOTIFY_OK;
  694. default:
  695. return NOTIFY_DONE;
  696. }
  697. }
  698. /**
  699. * cdns_i2c_suspend - Suspend method for the driver
  700. * @_dev: Address of the platform_device structure
  701. *
  702. * Put the driver into low power mode.
  703. *
  704. * Return: 0 always
  705. */
  706. static int __maybe_unused cdns_i2c_suspend(struct device *_dev)
  707. {
  708. struct platform_device *pdev = container_of(_dev,
  709. struct platform_device, dev);
  710. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  711. clk_disable(xi2c->clk);
  712. xi2c->suspended = 1;
  713. return 0;
  714. }
  715. /**
  716. * cdns_i2c_resume - Resume from suspend
  717. * @_dev: Address of the platform_device structure
  718. *
  719. * Resume operation after suspend.
  720. *
  721. * Return: 0 on success and error value on error
  722. */
  723. static int __maybe_unused cdns_i2c_resume(struct device *_dev)
  724. {
  725. struct platform_device *pdev = container_of(_dev,
  726. struct platform_device, dev);
  727. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  728. int ret;
  729. ret = clk_enable(xi2c->clk);
  730. if (ret) {
  731. dev_err(_dev, "Cannot enable clock.\n");
  732. return ret;
  733. }
  734. xi2c->suspended = 0;
  735. return 0;
  736. }
  737. static SIMPLE_DEV_PM_OPS(cdns_i2c_dev_pm_ops, cdns_i2c_suspend,
  738. cdns_i2c_resume);
  739. static const struct cdns_platform_data r1p10_i2c_def = {
  740. .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
  741. };
  742. static const struct of_device_id cdns_i2c_of_match[] = {
  743. { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
  744. { .compatible = "cdns,i2c-r1p14",},
  745. { /* end of table */ }
  746. };
  747. MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
  748. /**
  749. * cdns_i2c_probe - Platform registration call
  750. * @pdev: Handle to the platform device structure
  751. *
  752. * This function does all the memory allocation and registration for the i2c
  753. * device. User can modify the address mode to 10 bit address mode using the
  754. * ioctl call with option I2C_TENBIT.
  755. *
  756. * Return: 0 on success, negative error otherwise
  757. */
  758. static int cdns_i2c_probe(struct platform_device *pdev)
  759. {
  760. struct resource *r_mem;
  761. struct cdns_i2c *id;
  762. int ret;
  763. const struct of_device_id *match;
  764. id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
  765. if (!id)
  766. return -ENOMEM;
  767. platform_set_drvdata(pdev, id);
  768. match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
  769. if (match && match->data) {
  770. const struct cdns_platform_data *data = match->data;
  771. id->quirks = data->quirks;
  772. }
  773. r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  774. id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
  775. if (IS_ERR(id->membase))
  776. return PTR_ERR(id->membase);
  777. id->irq = platform_get_irq(pdev, 0);
  778. id->adap.owner = THIS_MODULE;
  779. id->adap.dev.of_node = pdev->dev.of_node;
  780. id->adap.algo = &cdns_i2c_algo;
  781. id->adap.timeout = CDNS_I2C_TIMEOUT;
  782. id->adap.retries = 3; /* Default retry value. */
  783. id->adap.algo_data = id;
  784. id->adap.dev.parent = &pdev->dev;
  785. init_completion(&id->xfer_done);
  786. snprintf(id->adap.name, sizeof(id->adap.name),
  787. "Cadence I2C at %08lx", (unsigned long)r_mem->start);
  788. id->clk = devm_clk_get(&pdev->dev, NULL);
  789. if (IS_ERR(id->clk)) {
  790. dev_err(&pdev->dev, "input clock not found.\n");
  791. return PTR_ERR(id->clk);
  792. }
  793. ret = clk_prepare_enable(id->clk);
  794. if (ret) {
  795. dev_err(&pdev->dev, "Unable to enable clock.\n");
  796. return ret;
  797. }
  798. id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
  799. if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
  800. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  801. id->input_clk = clk_get_rate(id->clk);
  802. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  803. &id->i2c_clk);
  804. if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
  805. id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
  806. cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS,
  807. CDNS_I2C_CR_OFFSET);
  808. ret = cdns_i2c_setclk(id->input_clk, id);
  809. if (ret) {
  810. dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
  811. ret = -EINVAL;
  812. goto err_clk_dis;
  813. }
  814. ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
  815. DRIVER_NAME, id);
  816. if (ret) {
  817. dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
  818. goto err_clk_dis;
  819. }
  820. ret = i2c_add_adapter(&id->adap);
  821. if (ret < 0) {
  822. dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
  823. goto err_clk_dis;
  824. }
  825. /*
  826. * Cadence I2C controller has a bug wherein it generates
  827. * invalid read transaction after HW timeout in master receiver mode.
  828. * HW timeout is not used by this driver and the interrupt is disabled.
  829. * But the feature itself cannot be disabled. Hence maximum value
  830. * is written to this register to reduce the chances of error.
  831. */
  832. cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
  833. dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
  834. id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
  835. return 0;
  836. err_clk_dis:
  837. clk_disable_unprepare(id->clk);
  838. return ret;
  839. }
  840. /**
  841. * cdns_i2c_remove - Unregister the device after releasing the resources
  842. * @pdev: Handle to the platform device structure
  843. *
  844. * This function frees all the resources allocated to the device.
  845. *
  846. * Return: 0 always
  847. */
  848. static int cdns_i2c_remove(struct platform_device *pdev)
  849. {
  850. struct cdns_i2c *id = platform_get_drvdata(pdev);
  851. i2c_del_adapter(&id->adap);
  852. clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
  853. clk_disable_unprepare(id->clk);
  854. return 0;
  855. }
  856. static struct platform_driver cdns_i2c_drv = {
  857. .driver = {
  858. .name = DRIVER_NAME,
  859. .of_match_table = cdns_i2c_of_match,
  860. .pm = &cdns_i2c_dev_pm_ops,
  861. },
  862. .probe = cdns_i2c_probe,
  863. .remove = cdns_i2c_remove,
  864. };
  865. module_platform_driver(cdns_i2c_drv);
  866. MODULE_AUTHOR("Xilinx Inc.");
  867. MODULE_DESCRIPTION("Cadence I2C bus driver");
  868. MODULE_LICENSE("GPL");