i2c-cadence.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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) || id->bus_hold_flag)
  334. ctrl_reg |= CDNS_I2C_CR_HOLD;
  335. else
  336. ctrl_reg = ctrl_reg & ~CDNS_I2C_CR_HOLD;
  337. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  338. /* Clear the interrupts in interrupt status register */
  339. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  340. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  341. /*
  342. * The no. of bytes to receive is checked against the limit of
  343. * max transfer size. Set transfer size register with no of bytes
  344. * receive if it is less than transfer size and transfer size if
  345. * it is more. Enable the interrupts.
  346. */
  347. if (id->recv_count > CDNS_I2C_TRANSFER_SIZE) {
  348. cdns_i2c_writereg(CDNS_I2C_TRANSFER_SIZE,
  349. CDNS_I2C_XFER_SIZE_OFFSET);
  350. id->curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
  351. } else {
  352. cdns_i2c_writereg(id->recv_count, CDNS_I2C_XFER_SIZE_OFFSET);
  353. }
  354. /* Clear the bus hold flag if bytes to receive is less than FIFO size */
  355. if (!id->bus_hold_flag &&
  356. ((id->p_msg->flags & I2C_M_RECV_LEN) != I2C_M_RECV_LEN) &&
  357. (id->recv_count <= CDNS_I2C_FIFO_DEPTH))
  358. cdns_i2c_clear_bus_hold(id);
  359. /* Set the slave address in address register - triggers operation */
  360. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  361. CDNS_I2C_ADDR_OFFSET);
  362. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  363. }
  364. /**
  365. * cdns_i2c_msend - Prepare and start a master send operation
  366. * @id: pointer to the i2c device
  367. */
  368. static void cdns_i2c_msend(struct cdns_i2c *id)
  369. {
  370. unsigned int avail_bytes;
  371. unsigned int bytes_to_send;
  372. unsigned int ctrl_reg;
  373. unsigned int isr_status;
  374. id->p_recv_buf = NULL;
  375. id->p_send_buf = id->p_msg->buf;
  376. id->send_count = id->p_msg->len;
  377. /* Set the controller in Master transmit mode and clear the FIFO. */
  378. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  379. ctrl_reg &= ~CDNS_I2C_CR_RW;
  380. ctrl_reg |= CDNS_I2C_CR_CLR_FIFO;
  381. /*
  382. * Check for the message size against FIFO depth and set the
  383. * 'hold bus' bit if it is greater than FIFO depth.
  384. */
  385. if ((id->send_count > CDNS_I2C_FIFO_DEPTH) || id->bus_hold_flag)
  386. ctrl_reg |= CDNS_I2C_CR_HOLD;
  387. else
  388. ctrl_reg = ctrl_reg & ~CDNS_I2C_CR_HOLD;
  389. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  390. /* Clear the interrupts in interrupt status register. */
  391. isr_status = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  392. cdns_i2c_writereg(isr_status, CDNS_I2C_ISR_OFFSET);
  393. /*
  394. * Calculate the space available in FIFO. Check the message length
  395. * against the space available, and fill the FIFO accordingly.
  396. * Enable the interrupts.
  397. */
  398. avail_bytes = CDNS_I2C_FIFO_DEPTH -
  399. cdns_i2c_readreg(CDNS_I2C_XFER_SIZE_OFFSET);
  400. if (id->send_count > avail_bytes)
  401. bytes_to_send = avail_bytes;
  402. else
  403. bytes_to_send = id->send_count;
  404. while (bytes_to_send--) {
  405. cdns_i2c_writereg((*(id->p_send_buf)++), CDNS_I2C_DATA_OFFSET);
  406. id->send_count--;
  407. }
  408. /*
  409. * Clear the bus hold flag if there is no more data
  410. * and if it is the last message.
  411. */
  412. if (!id->bus_hold_flag && !id->send_count)
  413. cdns_i2c_clear_bus_hold(id);
  414. /* Set the slave address in address register - triggers operation. */
  415. cdns_i2c_writereg(id->p_msg->addr & CDNS_I2C_ADDR_MASK,
  416. CDNS_I2C_ADDR_OFFSET);
  417. cdns_i2c_writereg(CDNS_I2C_ENABLED_INTR_MASK, CDNS_I2C_IER_OFFSET);
  418. }
  419. /**
  420. * cdns_i2c_master_reset - Reset the interface
  421. * @adap: pointer to the i2c adapter driver instance
  422. *
  423. * This function cleanup the fifos, clear the hold bit and status
  424. * and disable the interrupts.
  425. */
  426. static void cdns_i2c_master_reset(struct i2c_adapter *adap)
  427. {
  428. struct cdns_i2c *id = adap->algo_data;
  429. u32 regval;
  430. /* Disable the interrupts */
  431. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK, CDNS_I2C_IDR_OFFSET);
  432. /* Clear the hold bit and fifos */
  433. regval = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  434. regval &= ~CDNS_I2C_CR_HOLD;
  435. regval |= CDNS_I2C_CR_CLR_FIFO;
  436. cdns_i2c_writereg(regval, CDNS_I2C_CR_OFFSET);
  437. /* Update the transfercount register to zero */
  438. cdns_i2c_writereg(0, CDNS_I2C_XFER_SIZE_OFFSET);
  439. /* Clear the interupt status register */
  440. regval = cdns_i2c_readreg(CDNS_I2C_ISR_OFFSET);
  441. cdns_i2c_writereg(regval, CDNS_I2C_ISR_OFFSET);
  442. /* Clear the status register */
  443. regval = cdns_i2c_readreg(CDNS_I2C_SR_OFFSET);
  444. cdns_i2c_writereg(regval, CDNS_I2C_SR_OFFSET);
  445. }
  446. static int cdns_i2c_process_msg(struct cdns_i2c *id, struct i2c_msg *msg,
  447. struct i2c_adapter *adap)
  448. {
  449. unsigned long time_left;
  450. u32 reg;
  451. id->p_msg = msg;
  452. id->err_status = 0;
  453. reinit_completion(&id->xfer_done);
  454. /* Check for the TEN Bit mode on each msg */
  455. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  456. if (msg->flags & I2C_M_TEN) {
  457. if (reg & CDNS_I2C_CR_NEA)
  458. cdns_i2c_writereg(reg & ~CDNS_I2C_CR_NEA,
  459. CDNS_I2C_CR_OFFSET);
  460. } else {
  461. if (!(reg & CDNS_I2C_CR_NEA))
  462. cdns_i2c_writereg(reg | CDNS_I2C_CR_NEA,
  463. CDNS_I2C_CR_OFFSET);
  464. }
  465. /* Check for the R/W flag on each msg */
  466. if (msg->flags & I2C_M_RD)
  467. cdns_i2c_mrecv(id);
  468. else
  469. cdns_i2c_msend(id);
  470. /* Wait for the signal of completion */
  471. time_left = wait_for_completion_timeout(&id->xfer_done, adap->timeout);
  472. if (time_left == 0) {
  473. cdns_i2c_master_reset(adap);
  474. dev_err(id->adap.dev.parent,
  475. "timeout waiting on completion\n");
  476. return -ETIMEDOUT;
  477. }
  478. cdns_i2c_writereg(CDNS_I2C_IXR_ALL_INTR_MASK,
  479. CDNS_I2C_IDR_OFFSET);
  480. /* If it is bus arbitration error, try again */
  481. if (id->err_status & CDNS_I2C_IXR_ARB_LOST)
  482. return -EAGAIN;
  483. return 0;
  484. }
  485. /**
  486. * cdns_i2c_master_xfer - The main i2c transfer function
  487. * @adap: pointer to the i2c adapter driver instance
  488. * @msgs: pointer to the i2c message structure
  489. * @num: the number of messages to transfer
  490. *
  491. * Initiates the send/recv activity based on the transfer message received.
  492. *
  493. * Return: number of msgs processed on success, negative error otherwise
  494. */
  495. static int cdns_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  496. int num)
  497. {
  498. int ret, count;
  499. u32 reg;
  500. struct cdns_i2c *id = adap->algo_data;
  501. bool hold_quirk;
  502. /* Check if the bus is free */
  503. if (cdns_i2c_readreg(CDNS_I2C_SR_OFFSET) & CDNS_I2C_SR_BA)
  504. return -EAGAIN;
  505. hold_quirk = !!(id->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
  506. /*
  507. * Set the flag to one when multiple messages are to be
  508. * processed with a repeated start.
  509. */
  510. if (num > 1) {
  511. /*
  512. * This controller does not give completion interrupt after a
  513. * master receive message if HOLD bit is set (repeated start),
  514. * resulting in SW timeout. Hence, if a receive message is
  515. * followed by any other message, an error is returned
  516. * indicating that this sequence is not supported.
  517. */
  518. for (count = 0; (count < num - 1 && hold_quirk); count++) {
  519. if (msgs[count].flags & I2C_M_RD) {
  520. dev_warn(adap->dev.parent,
  521. "Can't do repeated start after a receive message\n");
  522. return -EOPNOTSUPP;
  523. }
  524. }
  525. id->bus_hold_flag = 1;
  526. reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  527. reg |= CDNS_I2C_CR_HOLD;
  528. cdns_i2c_writereg(reg, CDNS_I2C_CR_OFFSET);
  529. } else {
  530. id->bus_hold_flag = 0;
  531. }
  532. /* Process the msg one by one */
  533. for (count = 0; count < num; count++, msgs++) {
  534. if (count == (num - 1))
  535. id->bus_hold_flag = 0;
  536. ret = cdns_i2c_process_msg(id, msgs, adap);
  537. if (ret)
  538. return ret;
  539. /* Report the other error interrupts to application */
  540. if (id->err_status) {
  541. cdns_i2c_master_reset(adap);
  542. if (id->err_status & CDNS_I2C_IXR_NACK)
  543. return -ENXIO;
  544. return -EIO;
  545. }
  546. }
  547. return num;
  548. }
  549. /**
  550. * cdns_i2c_func - Returns the supported features of the I2C driver
  551. * @adap: pointer to the i2c adapter structure
  552. *
  553. * Return: 32 bit value, each bit corresponding to a feature
  554. */
  555. static u32 cdns_i2c_func(struct i2c_adapter *adap)
  556. {
  557. return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR |
  558. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK) |
  559. I2C_FUNC_SMBUS_BLOCK_DATA;
  560. }
  561. static const struct i2c_algorithm cdns_i2c_algo = {
  562. .master_xfer = cdns_i2c_master_xfer,
  563. .functionality = cdns_i2c_func,
  564. };
  565. /**
  566. * cdns_i2c_calc_divs - Calculate clock dividers
  567. * @f: I2C clock frequency
  568. * @input_clk: Input clock frequency
  569. * @a: First divider (return value)
  570. * @b: Second divider (return value)
  571. *
  572. * f is used as input and output variable. As input it is used as target I2C
  573. * frequency. On function exit f holds the actually resulting I2C frequency.
  574. *
  575. * Return: 0 on success, negative errno otherwise.
  576. */
  577. static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
  578. unsigned int *a, unsigned int *b)
  579. {
  580. unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
  581. unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
  582. unsigned int last_error, current_error;
  583. /* calculate (divisor_a+1) x (divisor_b+1) */
  584. temp = input_clk / (22 * fscl);
  585. /*
  586. * If the calculated value is negative or 0, the fscl input is out of
  587. * range. Return error.
  588. */
  589. if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
  590. return -EINVAL;
  591. last_error = -1;
  592. for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
  593. div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
  594. if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
  595. continue;
  596. div_b--;
  597. actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
  598. if (actual_fscl > fscl)
  599. continue;
  600. current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
  601. (fscl - actual_fscl));
  602. if (last_error > current_error) {
  603. calc_div_a = div_a;
  604. calc_div_b = div_b;
  605. best_fscl = actual_fscl;
  606. last_error = current_error;
  607. }
  608. }
  609. *a = calc_div_a;
  610. *b = calc_div_b;
  611. *f = best_fscl;
  612. return 0;
  613. }
  614. /**
  615. * cdns_i2c_setclk - This function sets the serial clock rate for the I2C device
  616. * @clk_in: I2C clock input frequency in Hz
  617. * @id: Pointer to the I2C device structure
  618. *
  619. * The device must be idle rather than busy transferring data before setting
  620. * these device options.
  621. * The data rate is set by values in the control register.
  622. * The formula for determining the correct register values is
  623. * Fscl = Fpclk/(22 x (divisor_a+1) x (divisor_b+1))
  624. * See the hardware data sheet for a full explanation of setting the serial
  625. * clock rate. The clock can not be faster than the input clock divide by 22.
  626. * The two most common clock rates are 100KHz and 400KHz.
  627. *
  628. * Return: 0 on success, negative error otherwise
  629. */
  630. static int cdns_i2c_setclk(unsigned long clk_in, struct cdns_i2c *id)
  631. {
  632. unsigned int div_a, div_b;
  633. unsigned int ctrl_reg;
  634. int ret = 0;
  635. unsigned long fscl = id->i2c_clk;
  636. ret = cdns_i2c_calc_divs(&fscl, clk_in, &div_a, &div_b);
  637. if (ret)
  638. return ret;
  639. ctrl_reg = cdns_i2c_readreg(CDNS_I2C_CR_OFFSET);
  640. ctrl_reg &= ~(CDNS_I2C_CR_DIVA_MASK | CDNS_I2C_CR_DIVB_MASK);
  641. ctrl_reg |= ((div_a << CDNS_I2C_CR_DIVA_SHIFT) |
  642. (div_b << CDNS_I2C_CR_DIVB_SHIFT));
  643. cdns_i2c_writereg(ctrl_reg, CDNS_I2C_CR_OFFSET);
  644. return 0;
  645. }
  646. /**
  647. * cdns_i2c_clk_notifier_cb - Clock rate change callback
  648. * @nb: Pointer to notifier block
  649. * @event: Notification reason
  650. * @data: Pointer to notification data object
  651. *
  652. * This function is called when the cdns_i2c input clock frequency changes.
  653. * The callback checks whether a valid bus frequency can be generated after the
  654. * change. If so, the change is acknowledged, otherwise the change is aborted.
  655. * New dividers are written to the HW in the pre- or post change notification
  656. * depending on the scaling direction.
  657. *
  658. * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK
  659. * to acknowedge the change, NOTIFY_DONE if the notification is
  660. * considered irrelevant.
  661. */
  662. static int cdns_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long
  663. event, void *data)
  664. {
  665. struct clk_notifier_data *ndata = data;
  666. struct cdns_i2c *id = to_cdns_i2c(nb);
  667. if (id->suspended)
  668. return NOTIFY_OK;
  669. switch (event) {
  670. case PRE_RATE_CHANGE:
  671. {
  672. unsigned long input_clk = ndata->new_rate;
  673. unsigned long fscl = id->i2c_clk;
  674. unsigned int div_a, div_b;
  675. int ret;
  676. ret = cdns_i2c_calc_divs(&fscl, input_clk, &div_a, &div_b);
  677. if (ret) {
  678. dev_warn(id->adap.dev.parent,
  679. "clock rate change rejected\n");
  680. return NOTIFY_STOP;
  681. }
  682. /* scale up */
  683. if (ndata->new_rate > ndata->old_rate)
  684. cdns_i2c_setclk(ndata->new_rate, id);
  685. return NOTIFY_OK;
  686. }
  687. case POST_RATE_CHANGE:
  688. id->input_clk = ndata->new_rate;
  689. /* scale down */
  690. if (ndata->new_rate < ndata->old_rate)
  691. cdns_i2c_setclk(ndata->new_rate, id);
  692. return NOTIFY_OK;
  693. case ABORT_RATE_CHANGE:
  694. /* scale up */
  695. if (ndata->new_rate > ndata->old_rate)
  696. cdns_i2c_setclk(ndata->old_rate, id);
  697. return NOTIFY_OK;
  698. default:
  699. return NOTIFY_DONE;
  700. }
  701. }
  702. /**
  703. * cdns_i2c_suspend - Suspend method for the driver
  704. * @_dev: Address of the platform_device structure
  705. *
  706. * Put the driver into low power mode.
  707. *
  708. * Return: 0 always
  709. */
  710. static int __maybe_unused cdns_i2c_suspend(struct device *_dev)
  711. {
  712. struct platform_device *pdev = container_of(_dev,
  713. struct platform_device, dev);
  714. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  715. clk_disable(xi2c->clk);
  716. xi2c->suspended = 1;
  717. return 0;
  718. }
  719. /**
  720. * cdns_i2c_resume - Resume from suspend
  721. * @_dev: Address of the platform_device structure
  722. *
  723. * Resume operation after suspend.
  724. *
  725. * Return: 0 on success and error value on error
  726. */
  727. static int __maybe_unused cdns_i2c_resume(struct device *_dev)
  728. {
  729. struct platform_device *pdev = container_of(_dev,
  730. struct platform_device, dev);
  731. struct cdns_i2c *xi2c = platform_get_drvdata(pdev);
  732. int ret;
  733. ret = clk_enable(xi2c->clk);
  734. if (ret) {
  735. dev_err(_dev, "Cannot enable clock.\n");
  736. return ret;
  737. }
  738. xi2c->suspended = 0;
  739. return 0;
  740. }
  741. static SIMPLE_DEV_PM_OPS(cdns_i2c_dev_pm_ops, cdns_i2c_suspend,
  742. cdns_i2c_resume);
  743. static const struct cdns_platform_data r1p10_i2c_def = {
  744. .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
  745. };
  746. static const struct of_device_id cdns_i2c_of_match[] = {
  747. { .compatible = "cdns,i2c-r1p10", .data = &r1p10_i2c_def },
  748. { .compatible = "cdns,i2c-r1p14",},
  749. { /* end of table */ }
  750. };
  751. MODULE_DEVICE_TABLE(of, cdns_i2c_of_match);
  752. /**
  753. * cdns_i2c_probe - Platform registration call
  754. * @pdev: Handle to the platform device structure
  755. *
  756. * This function does all the memory allocation and registration for the i2c
  757. * device. User can modify the address mode to 10 bit address mode using the
  758. * ioctl call with option I2C_TENBIT.
  759. *
  760. * Return: 0 on success, negative error otherwise
  761. */
  762. static int cdns_i2c_probe(struct platform_device *pdev)
  763. {
  764. struct resource *r_mem;
  765. struct cdns_i2c *id;
  766. int ret;
  767. const struct of_device_id *match;
  768. id = devm_kzalloc(&pdev->dev, sizeof(*id), GFP_KERNEL);
  769. if (!id)
  770. return -ENOMEM;
  771. platform_set_drvdata(pdev, id);
  772. match = of_match_node(cdns_i2c_of_match, pdev->dev.of_node);
  773. if (match && match->data) {
  774. const struct cdns_platform_data *data = match->data;
  775. id->quirks = data->quirks;
  776. }
  777. r_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  778. id->membase = devm_ioremap_resource(&pdev->dev, r_mem);
  779. if (IS_ERR(id->membase))
  780. return PTR_ERR(id->membase);
  781. id->irq = platform_get_irq(pdev, 0);
  782. id->adap.owner = THIS_MODULE;
  783. id->adap.dev.of_node = pdev->dev.of_node;
  784. id->adap.algo = &cdns_i2c_algo;
  785. id->adap.timeout = CDNS_I2C_TIMEOUT;
  786. id->adap.retries = 3; /* Default retry value. */
  787. id->adap.algo_data = id;
  788. id->adap.dev.parent = &pdev->dev;
  789. init_completion(&id->xfer_done);
  790. snprintf(id->adap.name, sizeof(id->adap.name),
  791. "Cadence I2C at %08lx", (unsigned long)r_mem->start);
  792. id->clk = devm_clk_get(&pdev->dev, NULL);
  793. if (IS_ERR(id->clk)) {
  794. dev_err(&pdev->dev, "input clock not found.\n");
  795. return PTR_ERR(id->clk);
  796. }
  797. ret = clk_prepare_enable(id->clk);
  798. if (ret) {
  799. dev_err(&pdev->dev, "Unable to enable clock.\n");
  800. return ret;
  801. }
  802. id->clk_rate_change_nb.notifier_call = cdns_i2c_clk_notifier_cb;
  803. if (clk_notifier_register(id->clk, &id->clk_rate_change_nb))
  804. dev_warn(&pdev->dev, "Unable to register clock notifier.\n");
  805. id->input_clk = clk_get_rate(id->clk);
  806. ret = of_property_read_u32(pdev->dev.of_node, "clock-frequency",
  807. &id->i2c_clk);
  808. if (ret || (id->i2c_clk > CDNS_I2C_SPEED_MAX))
  809. id->i2c_clk = CDNS_I2C_SPEED_DEFAULT;
  810. cdns_i2c_writereg(CDNS_I2C_CR_ACK_EN | CDNS_I2C_CR_NEA | CDNS_I2C_CR_MS,
  811. CDNS_I2C_CR_OFFSET);
  812. ret = cdns_i2c_setclk(id->input_clk, id);
  813. if (ret) {
  814. dev_err(&pdev->dev, "invalid SCL clock: %u Hz\n", id->i2c_clk);
  815. ret = -EINVAL;
  816. goto err_clk_dis;
  817. }
  818. ret = devm_request_irq(&pdev->dev, id->irq, cdns_i2c_isr, 0,
  819. DRIVER_NAME, id);
  820. if (ret) {
  821. dev_err(&pdev->dev, "cannot get irq %d\n", id->irq);
  822. goto err_clk_dis;
  823. }
  824. ret = i2c_add_adapter(&id->adap);
  825. if (ret < 0) {
  826. dev_err(&pdev->dev, "reg adap failed: %d\n", ret);
  827. goto err_clk_dis;
  828. }
  829. /*
  830. * Cadence I2C controller has a bug wherein it generates
  831. * invalid read transaction after HW timeout in master receiver mode.
  832. * HW timeout is not used by this driver and the interrupt is disabled.
  833. * But the feature itself cannot be disabled. Hence maximum value
  834. * is written to this register to reduce the chances of error.
  835. */
  836. cdns_i2c_writereg(CDNS_I2C_TIMEOUT_MAX, CDNS_I2C_TIME_OUT_OFFSET);
  837. dev_info(&pdev->dev, "%u kHz mmio %08lx irq %d\n",
  838. id->i2c_clk / 1000, (unsigned long)r_mem->start, id->irq);
  839. return 0;
  840. err_clk_dis:
  841. clk_disable_unprepare(id->clk);
  842. return ret;
  843. }
  844. /**
  845. * cdns_i2c_remove - Unregister the device after releasing the resources
  846. * @pdev: Handle to the platform device structure
  847. *
  848. * This function frees all the resources allocated to the device.
  849. *
  850. * Return: 0 always
  851. */
  852. static int cdns_i2c_remove(struct platform_device *pdev)
  853. {
  854. struct cdns_i2c *id = platform_get_drvdata(pdev);
  855. i2c_del_adapter(&id->adap);
  856. clk_notifier_unregister(id->clk, &id->clk_rate_change_nb);
  857. clk_disable_unprepare(id->clk);
  858. return 0;
  859. }
  860. static struct platform_driver cdns_i2c_drv = {
  861. .driver = {
  862. .name = DRIVER_NAME,
  863. .of_match_table = cdns_i2c_of_match,
  864. .pm = &cdns_i2c_dev_pm_ops,
  865. },
  866. .probe = cdns_i2c_probe,
  867. .remove = cdns_i2c_remove,
  868. };
  869. module_platform_driver(cdns_i2c_drv);
  870. MODULE_AUTHOR("Xilinx Inc.");
  871. MODULE_DESCRIPTION("Cadence I2C bus driver");
  872. MODULE_LICENSE("GPL");