i2c-nomadik.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2009 ST-Ericsson SA
  4. * Copyright (C) 2009 STMicroelectronics
  5. *
  6. * I2C master mode controller driver, used in Nomadik 8815
  7. * and Ux500 platforms.
  8. *
  9. * Author: Srinidhi Kasagar <[email protected]>
  10. * Author: Sachin Verma <[email protected]>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/amba/bus.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #define DRIVER_NAME "nmk-i2c"
  25. /* I2C Controller register offsets */
  26. #define I2C_CR (0x000)
  27. #define I2C_SCR (0x004)
  28. #define I2C_HSMCR (0x008)
  29. #define I2C_MCR (0x00C)
  30. #define I2C_TFR (0x010)
  31. #define I2C_SR (0x014)
  32. #define I2C_RFR (0x018)
  33. #define I2C_TFTR (0x01C)
  34. #define I2C_RFTR (0x020)
  35. #define I2C_DMAR (0x024)
  36. #define I2C_BRCR (0x028)
  37. #define I2C_IMSCR (0x02C)
  38. #define I2C_RISR (0x030)
  39. #define I2C_MISR (0x034)
  40. #define I2C_ICR (0x038)
  41. /* Control registers */
  42. #define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */
  43. #define I2C_CR_OM (0x3 << 1) /* Operating mode */
  44. #define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */
  45. #define I2C_CR_SM (0x3 << 4) /* Speed mode */
  46. #define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */
  47. #define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */
  48. #define I2C_CR_FRX (0x1 << 8) /* Flush Receive */
  49. #define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */
  50. #define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */
  51. #define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */
  52. #define I2C_CR_LM (0x1 << 12) /* Loopback mode */
  53. #define I2C_CR_FON (0x3 << 13) /* Filtering on */
  54. #define I2C_CR_FS (0x3 << 15) /* Force stop enable */
  55. /* Master controller (MCR) register */
  56. #define I2C_MCR_OP (0x1 << 0) /* Operation */
  57. #define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */
  58. #define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */
  59. #define I2C_MCR_SB (0x1 << 11) /* Extended address */
  60. #define I2C_MCR_AM (0x3 << 12) /* Address type */
  61. #define I2C_MCR_STOP (0x1 << 14) /* Stop condition */
  62. #define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */
  63. /* Status register (SR) */
  64. #define I2C_SR_OP (0x3 << 0) /* Operation */
  65. #define I2C_SR_STATUS (0x3 << 2) /* controller status */
  66. #define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */
  67. #define I2C_SR_TYPE (0x3 << 7) /* Receive type */
  68. #define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */
  69. /* Interrupt mask set/clear (IMSCR) bits */
  70. #define I2C_IT_TXFE (0x1 << 0)
  71. #define I2C_IT_TXFNE (0x1 << 1)
  72. #define I2C_IT_TXFF (0x1 << 2)
  73. #define I2C_IT_TXFOVR (0x1 << 3)
  74. #define I2C_IT_RXFE (0x1 << 4)
  75. #define I2C_IT_RXFNF (0x1 << 5)
  76. #define I2C_IT_RXFF (0x1 << 6)
  77. #define I2C_IT_RFSR (0x1 << 16)
  78. #define I2C_IT_RFSE (0x1 << 17)
  79. #define I2C_IT_WTSR (0x1 << 18)
  80. #define I2C_IT_MTD (0x1 << 19)
  81. #define I2C_IT_STD (0x1 << 20)
  82. #define I2C_IT_MAL (0x1 << 24)
  83. #define I2C_IT_BERR (0x1 << 25)
  84. #define I2C_IT_MTDWS (0x1 << 28)
  85. #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))
  86. /* some bits in ICR are reserved */
  87. #define I2C_CLEAR_ALL_INTS 0x131f007f
  88. /* first three msb bits are reserved */
  89. #define IRQ_MASK(mask) (mask & 0x1fffffff)
  90. /* maximum threshold value */
  91. #define MAX_I2C_FIFO_THRESHOLD 15
  92. enum i2c_freq_mode {
  93. I2C_FREQ_MODE_STANDARD, /* up to 100 Kb/s */
  94. I2C_FREQ_MODE_FAST, /* up to 400 Kb/s */
  95. I2C_FREQ_MODE_HIGH_SPEED, /* up to 3.4 Mb/s */
  96. I2C_FREQ_MODE_FAST_PLUS, /* up to 1 Mb/s */
  97. };
  98. /**
  99. * struct i2c_vendor_data - per-vendor variations
  100. * @has_mtdws: variant has the MTDWS bit
  101. * @fifodepth: variant FIFO depth
  102. */
  103. struct i2c_vendor_data {
  104. bool has_mtdws;
  105. u32 fifodepth;
  106. };
  107. enum i2c_status {
  108. I2C_NOP,
  109. I2C_ON_GOING,
  110. I2C_OK,
  111. I2C_ABORT
  112. };
  113. /* operation */
  114. enum i2c_operation {
  115. I2C_NO_OPERATION = 0xff,
  116. I2C_WRITE = 0x00,
  117. I2C_READ = 0x01
  118. };
  119. /**
  120. * struct i2c_nmk_client - client specific data
  121. * @slave_adr: 7-bit slave address
  122. * @count: no. bytes to be transferred
  123. * @buffer: client data buffer
  124. * @xfer_bytes: bytes transferred till now
  125. * @operation: current I2C operation
  126. */
  127. struct i2c_nmk_client {
  128. unsigned short slave_adr;
  129. unsigned long count;
  130. unsigned char *buffer;
  131. unsigned long xfer_bytes;
  132. enum i2c_operation operation;
  133. };
  134. /**
  135. * struct nmk_i2c_dev - private data structure of the controller.
  136. * @vendor: vendor data for this variant.
  137. * @adev: parent amba device.
  138. * @adap: corresponding I2C adapter.
  139. * @irq: interrupt line for the controller.
  140. * @virtbase: virtual io memory area.
  141. * @clk: hardware i2c block clock.
  142. * @cli: holder of client specific data.
  143. * @clk_freq: clock frequency for the operation mode
  144. * @tft: Tx FIFO Threshold in bytes
  145. * @rft: Rx FIFO Threshold in bytes
  146. * @timeout: Slave response timeout (ms)
  147. * @sm: speed mode
  148. * @stop: stop condition.
  149. * @xfer_complete: acknowledge completion for a I2C message.
  150. * @result: controller propogated result.
  151. */
  152. struct nmk_i2c_dev {
  153. struct i2c_vendor_data *vendor;
  154. struct amba_device *adev;
  155. struct i2c_adapter adap;
  156. int irq;
  157. void __iomem *virtbase;
  158. struct clk *clk;
  159. struct i2c_nmk_client cli;
  160. u32 clk_freq;
  161. unsigned char tft;
  162. unsigned char rft;
  163. int timeout;
  164. enum i2c_freq_mode sm;
  165. int stop;
  166. struct completion xfer_complete;
  167. int result;
  168. };
  169. /* controller's abort causes */
  170. static const char *abort_causes[] = {
  171. "no ack received after address transmission",
  172. "no ack received during data phase",
  173. "ack received after xmission of master code",
  174. "master lost arbitration",
  175. "slave restarts",
  176. "slave reset",
  177. "overflow, maxsize is 2047 bytes",
  178. };
  179. static inline void i2c_set_bit(void __iomem *reg, u32 mask)
  180. {
  181. writel(readl(reg) | mask, reg);
  182. }
  183. static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
  184. {
  185. writel(readl(reg) & ~mask, reg);
  186. }
  187. /**
  188. * flush_i2c_fifo() - This function flushes the I2C FIFO
  189. * @dev: private data of I2C Driver
  190. *
  191. * This function flushes the I2C Tx and Rx FIFOs. It returns
  192. * 0 on successful flushing of FIFO
  193. */
  194. static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
  195. {
  196. #define LOOP_ATTEMPTS 10
  197. int i;
  198. unsigned long timeout;
  199. /*
  200. * flush the transmit and receive FIFO. The flushing
  201. * operation takes several cycles before to be completed.
  202. * On the completion, the I2C internal logic clears these
  203. * bits, until then no one must access Tx, Rx FIFO and
  204. * should poll on these bits waiting for the completion.
  205. */
  206. writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
  207. for (i = 0; i < LOOP_ATTEMPTS; i++) {
  208. timeout = jiffies + dev->adap.timeout;
  209. while (!time_after(jiffies, timeout)) {
  210. if ((readl(dev->virtbase + I2C_CR) &
  211. (I2C_CR_FTX | I2C_CR_FRX)) == 0)
  212. return 0;
  213. }
  214. }
  215. dev_err(&dev->adev->dev,
  216. "flushing operation timed out giving up after %d attempts",
  217. LOOP_ATTEMPTS);
  218. return -ETIMEDOUT;
  219. }
  220. /**
  221. * disable_all_interrupts() - Disable all interrupts of this I2c Bus
  222. * @dev: private data of I2C Driver
  223. */
  224. static void disable_all_interrupts(struct nmk_i2c_dev *dev)
  225. {
  226. u32 mask = IRQ_MASK(0);
  227. writel(mask, dev->virtbase + I2C_IMSCR);
  228. }
  229. /**
  230. * clear_all_interrupts() - Clear all interrupts of I2C Controller
  231. * @dev: private data of I2C Driver
  232. */
  233. static void clear_all_interrupts(struct nmk_i2c_dev *dev)
  234. {
  235. u32 mask;
  236. mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
  237. writel(mask, dev->virtbase + I2C_ICR);
  238. }
  239. /**
  240. * init_hw() - initialize the I2C hardware
  241. * @dev: private data of I2C Driver
  242. */
  243. static int init_hw(struct nmk_i2c_dev *dev)
  244. {
  245. int stat;
  246. stat = flush_i2c_fifo(dev);
  247. if (stat)
  248. goto exit;
  249. /* disable the controller */
  250. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  251. disable_all_interrupts(dev);
  252. clear_all_interrupts(dev);
  253. dev->cli.operation = I2C_NO_OPERATION;
  254. exit:
  255. return stat;
  256. }
  257. /* enable peripheral, master mode operation */
  258. #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
  259. /**
  260. * load_i2c_mcr_reg() - load the MCR register
  261. * @dev: private data of controller
  262. * @flags: message flags
  263. */
  264. static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev, u16 flags)
  265. {
  266. u32 mcr = 0;
  267. unsigned short slave_adr_3msb_bits;
  268. mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
  269. if (unlikely(flags & I2C_M_TEN)) {
  270. /* 10-bit address transaction */
  271. mcr |= GEN_MASK(2, I2C_MCR_AM, 12);
  272. /*
  273. * Get the top 3 bits.
  274. * EA10 represents extended address in MCR. This includes
  275. * the extension (MSB bits) of the 7 bit address loaded
  276. * in A7
  277. */
  278. slave_adr_3msb_bits = (dev->cli.slave_adr >> 7) & 0x7;
  279. mcr |= GEN_MASK(slave_adr_3msb_bits, I2C_MCR_EA10, 8);
  280. } else {
  281. /* 7-bit address transaction */
  282. mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
  283. }
  284. /* start byte procedure not applied */
  285. mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
  286. /* check the operation, master read/write? */
  287. if (dev->cli.operation == I2C_WRITE)
  288. mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
  289. else
  290. mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
  291. /* stop or repeated start? */
  292. if (dev->stop)
  293. mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
  294. else
  295. mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
  296. mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
  297. return mcr;
  298. }
  299. /**
  300. * setup_i2c_controller() - setup the controller
  301. * @dev: private data of controller
  302. */
  303. static void setup_i2c_controller(struct nmk_i2c_dev *dev)
  304. {
  305. u32 brcr1, brcr2;
  306. u32 i2c_clk, div;
  307. u32 ns;
  308. u16 slsu;
  309. writel(0x0, dev->virtbase + I2C_CR);
  310. writel(0x0, dev->virtbase + I2C_HSMCR);
  311. writel(0x0, dev->virtbase + I2C_TFTR);
  312. writel(0x0, dev->virtbase + I2C_RFTR);
  313. writel(0x0, dev->virtbase + I2C_DMAR);
  314. i2c_clk = clk_get_rate(dev->clk);
  315. /*
  316. * set the slsu:
  317. *
  318. * slsu defines the data setup time after SCL clock
  319. * stretching in terms of i2c clk cycles + 1 (zero means
  320. * "wait one cycle"), the needed setup time for the three
  321. * modes are 250ns, 100ns, 10ns respectively.
  322. *
  323. * As the time for one cycle T in nanoseconds is
  324. * T = (1/f) * 1000000000 =>
  325. * slsu = cycles / (1000000000 / f) + 1
  326. */
  327. ns = DIV_ROUND_UP_ULL(1000000000ULL, i2c_clk);
  328. switch (dev->sm) {
  329. case I2C_FREQ_MODE_FAST:
  330. case I2C_FREQ_MODE_FAST_PLUS:
  331. slsu = DIV_ROUND_UP(100, ns); /* Fast */
  332. break;
  333. case I2C_FREQ_MODE_HIGH_SPEED:
  334. slsu = DIV_ROUND_UP(10, ns); /* High */
  335. break;
  336. case I2C_FREQ_MODE_STANDARD:
  337. default:
  338. slsu = DIV_ROUND_UP(250, ns); /* Standard */
  339. break;
  340. }
  341. slsu += 1;
  342. dev_dbg(&dev->adev->dev, "calculated SLSU = %04x\n", slsu);
  343. writel(slsu << 16, dev->virtbase + I2C_SCR);
  344. /*
  345. * The spec says, in case of std. mode the divider is
  346. * 2 whereas it is 3 for fast and fastplus mode of
  347. * operation. TODO - high speed support.
  348. */
  349. div = (dev->clk_freq > I2C_MAX_STANDARD_MODE_FREQ) ? 3 : 2;
  350. /*
  351. * generate the mask for baud rate counters. The controller
  352. * has two baud rate counters. One is used for High speed
  353. * operation, and the other is for std, fast mode, fast mode
  354. * plus operation. Currently we do not supprt high speed mode
  355. * so set brcr1 to 0.
  356. */
  357. brcr1 = 0 << 16;
  358. brcr2 = (i2c_clk/(dev->clk_freq * div)) & 0xffff;
  359. /* set the baud rate counter register */
  360. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  361. /*
  362. * set the speed mode. Currently we support
  363. * only standard and fast mode of operation
  364. * TODO - support for fast mode plus (up to 1Mb/s)
  365. * and high speed (up to 3.4 Mb/s)
  366. */
  367. if (dev->sm > I2C_FREQ_MODE_FAST) {
  368. dev_err(&dev->adev->dev,
  369. "do not support this mode defaulting to std. mode\n");
  370. brcr2 = i2c_clk / (I2C_MAX_STANDARD_MODE_FREQ * 2) & 0xffff;
  371. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  372. writel(I2C_FREQ_MODE_STANDARD << 4,
  373. dev->virtbase + I2C_CR);
  374. }
  375. writel(dev->sm << 4, dev->virtbase + I2C_CR);
  376. /* set the Tx and Rx FIFO threshold */
  377. writel(dev->tft, dev->virtbase + I2C_TFTR);
  378. writel(dev->rft, dev->virtbase + I2C_RFTR);
  379. }
  380. /**
  381. * read_i2c() - Read from I2C client device
  382. * @dev: private data of I2C Driver
  383. * @flags: message flags
  384. *
  385. * This function reads from i2c client device when controller is in
  386. * master mode. There is a completion timeout. If there is no transfer
  387. * before timeout error is returned.
  388. */
  389. static int read_i2c(struct nmk_i2c_dev *dev, u16 flags)
  390. {
  391. int status = 0;
  392. u32 mcr, irq_mask;
  393. unsigned long timeout;
  394. mcr = load_i2c_mcr_reg(dev, flags);
  395. writel(mcr, dev->virtbase + I2C_MCR);
  396. /* load the current CR value */
  397. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  398. dev->virtbase + I2C_CR);
  399. /* enable the controller */
  400. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  401. init_completion(&dev->xfer_complete);
  402. /* enable interrupts by setting the mask */
  403. irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
  404. I2C_IT_MAL | I2C_IT_BERR);
  405. if (dev->stop || !dev->vendor->has_mtdws)
  406. irq_mask |= I2C_IT_MTD;
  407. else
  408. irq_mask |= I2C_IT_MTDWS;
  409. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  410. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  411. dev->virtbase + I2C_IMSCR);
  412. timeout = wait_for_completion_timeout(
  413. &dev->xfer_complete, dev->adap.timeout);
  414. if (timeout == 0) {
  415. /* Controller timed out */
  416. dev_err(&dev->adev->dev, "read from slave 0x%x timed out\n",
  417. dev->cli.slave_adr);
  418. status = -ETIMEDOUT;
  419. }
  420. return status;
  421. }
  422. static void fill_tx_fifo(struct nmk_i2c_dev *dev, int no_bytes)
  423. {
  424. int count;
  425. for (count = (no_bytes - 2);
  426. (count > 0) &&
  427. (dev->cli.count != 0);
  428. count--) {
  429. /* write to the Tx FIFO */
  430. writeb(*dev->cli.buffer,
  431. dev->virtbase + I2C_TFR);
  432. dev->cli.buffer++;
  433. dev->cli.count--;
  434. dev->cli.xfer_bytes++;
  435. }
  436. }
  437. /**
  438. * write_i2c() - Write data to I2C client.
  439. * @dev: private data of I2C Driver
  440. * @flags: message flags
  441. *
  442. * This function writes data to I2C client
  443. */
  444. static int write_i2c(struct nmk_i2c_dev *dev, u16 flags)
  445. {
  446. u32 status = 0;
  447. u32 mcr, irq_mask;
  448. unsigned long timeout;
  449. mcr = load_i2c_mcr_reg(dev, flags);
  450. writel(mcr, dev->virtbase + I2C_MCR);
  451. /* load the current CR value */
  452. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  453. dev->virtbase + I2C_CR);
  454. /* enable the controller */
  455. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  456. init_completion(&dev->xfer_complete);
  457. /* enable interrupts by settings the masks */
  458. irq_mask = (I2C_IT_TXFOVR | I2C_IT_MAL | I2C_IT_BERR);
  459. /* Fill the TX FIFO with transmit data */
  460. fill_tx_fifo(dev, MAX_I2C_FIFO_THRESHOLD);
  461. if (dev->cli.count != 0)
  462. irq_mask |= I2C_IT_TXFNE;
  463. /*
  464. * check if we want to transfer a single or multiple bytes, if so
  465. * set the MTDWS bit (Master Transaction Done Without Stop)
  466. * to start repeated start operation
  467. */
  468. if (dev->stop || !dev->vendor->has_mtdws)
  469. irq_mask |= I2C_IT_MTD;
  470. else
  471. irq_mask |= I2C_IT_MTDWS;
  472. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  473. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  474. dev->virtbase + I2C_IMSCR);
  475. timeout = wait_for_completion_timeout(
  476. &dev->xfer_complete, dev->adap.timeout);
  477. if (timeout == 0) {
  478. /* Controller timed out */
  479. dev_err(&dev->adev->dev, "write to slave 0x%x timed out\n",
  480. dev->cli.slave_adr);
  481. status = -ETIMEDOUT;
  482. }
  483. return status;
  484. }
  485. /**
  486. * nmk_i2c_xfer_one() - transmit a single I2C message
  487. * @dev: device with a message encoded into it
  488. * @flags: message flags
  489. */
  490. static int nmk_i2c_xfer_one(struct nmk_i2c_dev *dev, u16 flags)
  491. {
  492. int status;
  493. if (flags & I2C_M_RD) {
  494. /* read operation */
  495. dev->cli.operation = I2C_READ;
  496. status = read_i2c(dev, flags);
  497. } else {
  498. /* write operation */
  499. dev->cli.operation = I2C_WRITE;
  500. status = write_i2c(dev, flags);
  501. }
  502. if (status || (dev->result)) {
  503. u32 i2c_sr;
  504. u32 cause;
  505. i2c_sr = readl(dev->virtbase + I2C_SR);
  506. /*
  507. * Check if the controller I2C operation status
  508. * is set to ABORT(11b).
  509. */
  510. if (((i2c_sr >> 2) & 0x3) == 0x3) {
  511. /* get the abort cause */
  512. cause = (i2c_sr >> 4) & 0x7;
  513. dev_err(&dev->adev->dev, "%s\n",
  514. cause >= ARRAY_SIZE(abort_causes) ?
  515. "unknown reason" :
  516. abort_causes[cause]);
  517. }
  518. (void) init_hw(dev);
  519. status = status ? status : dev->result;
  520. }
  521. return status;
  522. }
  523. /**
  524. * nmk_i2c_xfer() - I2C transfer function used by kernel framework
  525. * @i2c_adap: Adapter pointer to the controller
  526. * @msgs: Pointer to data to be written.
  527. * @num_msgs: Number of messages to be executed
  528. *
  529. * This is the function called by the generic kernel i2c_transfer()
  530. * or i2c_smbus...() API calls. Note that this code is protected by the
  531. * semaphore set in the kernel i2c_transfer() function.
  532. *
  533. * NOTE:
  534. * READ TRANSFER : We impose a restriction of the first message to be the
  535. * index message for any read transaction.
  536. * - a no index is coded as '0',
  537. * - 2byte big endian index is coded as '3'
  538. * !!! msg[0].buf holds the actual index.
  539. * This is compatible with generic messages of smbus emulator
  540. * that send a one byte index.
  541. * eg. a I2C transation to read 2 bytes from index 0
  542. * idx = 0;
  543. * msg[0].addr = client->addr;
  544. * msg[0].flags = 0x0;
  545. * msg[0].len = 1;
  546. * msg[0].buf = &idx;
  547. *
  548. * msg[1].addr = client->addr;
  549. * msg[1].flags = I2C_M_RD;
  550. * msg[1].len = 2;
  551. * msg[1].buf = rd_buff
  552. * i2c_transfer(adap, msg, 2);
  553. *
  554. * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
  555. * If you want to emulate an SMBUS write transaction put the
  556. * index as first byte(or first and second) in the payload.
  557. * eg. a I2C transation to write 2 bytes from index 1
  558. * wr_buff[0] = 0x1;
  559. * wr_buff[1] = 0x23;
  560. * wr_buff[2] = 0x46;
  561. * msg[0].flags = 0x0;
  562. * msg[0].len = 3;
  563. * msg[0].buf = wr_buff;
  564. * i2c_transfer(adap, msg, 1);
  565. *
  566. * To read or write a block of data (multiple bytes) using SMBUS emulation
  567. * please use the i2c_smbus_read_i2c_block_data()
  568. * or i2c_smbus_write_i2c_block_data() API
  569. */
  570. static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
  571. struct i2c_msg msgs[], int num_msgs)
  572. {
  573. int status = 0;
  574. int i;
  575. struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
  576. int j;
  577. pm_runtime_get_sync(&dev->adev->dev);
  578. /* Attempt three times to send the message queue */
  579. for (j = 0; j < 3; j++) {
  580. /* setup the i2c controller */
  581. setup_i2c_controller(dev);
  582. for (i = 0; i < num_msgs; i++) {
  583. dev->cli.slave_adr = msgs[i].addr;
  584. dev->cli.buffer = msgs[i].buf;
  585. dev->cli.count = msgs[i].len;
  586. dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
  587. dev->result = 0;
  588. status = nmk_i2c_xfer_one(dev, msgs[i].flags);
  589. if (status != 0)
  590. break;
  591. }
  592. if (status == 0)
  593. break;
  594. }
  595. pm_runtime_put_sync(&dev->adev->dev);
  596. /* return the no. messages processed */
  597. if (status)
  598. return status;
  599. else
  600. return num_msgs;
  601. }
  602. /**
  603. * disable_interrupts() - disable the interrupts
  604. * @dev: private data of controller
  605. * @irq: interrupt number
  606. */
  607. static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
  608. {
  609. irq = IRQ_MASK(irq);
  610. writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
  611. dev->virtbase + I2C_IMSCR);
  612. return 0;
  613. }
  614. /**
  615. * i2c_irq_handler() - interrupt routine
  616. * @irq: interrupt number
  617. * @arg: data passed to the handler
  618. *
  619. * This is the interrupt handler for the i2c driver. Currently
  620. * it handles the major interrupts like Rx & Tx FIFO management
  621. * interrupts, master transaction interrupts, arbitration and
  622. * bus error interrupts. The rest of the interrupts are treated as
  623. * unhandled.
  624. */
  625. static irqreturn_t i2c_irq_handler(int irq, void *arg)
  626. {
  627. struct nmk_i2c_dev *dev = arg;
  628. u32 tft, rft;
  629. u32 count;
  630. u32 misr, src;
  631. /* load Tx FIFO and Rx FIFO threshold values */
  632. tft = readl(dev->virtbase + I2C_TFTR);
  633. rft = readl(dev->virtbase + I2C_RFTR);
  634. /* read interrupt status register */
  635. misr = readl(dev->virtbase + I2C_MISR);
  636. src = __ffs(misr);
  637. switch ((1 << src)) {
  638. /* Transmit FIFO nearly empty interrupt */
  639. case I2C_IT_TXFNE:
  640. {
  641. if (dev->cli.operation == I2C_READ) {
  642. /*
  643. * in read operation why do we care for writing?
  644. * so disable the Transmit FIFO interrupt
  645. */
  646. disable_interrupts(dev, I2C_IT_TXFNE);
  647. } else {
  648. fill_tx_fifo(dev, (MAX_I2C_FIFO_THRESHOLD - tft));
  649. /*
  650. * if done, close the transfer by disabling the
  651. * corresponding TXFNE interrupt
  652. */
  653. if (dev->cli.count == 0)
  654. disable_interrupts(dev, I2C_IT_TXFNE);
  655. }
  656. }
  657. break;
  658. /*
  659. * Rx FIFO nearly full interrupt.
  660. * This is set when the numer of entries in Rx FIFO is
  661. * greater or equal than the threshold value programmed
  662. * in RFT
  663. */
  664. case I2C_IT_RXFNF:
  665. for (count = rft; count > 0; count--) {
  666. /* Read the Rx FIFO */
  667. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  668. dev->cli.buffer++;
  669. }
  670. dev->cli.count -= rft;
  671. dev->cli.xfer_bytes += rft;
  672. break;
  673. /* Rx FIFO full */
  674. case I2C_IT_RXFF:
  675. for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
  676. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  677. dev->cli.buffer++;
  678. }
  679. dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
  680. dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
  681. break;
  682. /* Master Transaction Done with/without stop */
  683. case I2C_IT_MTD:
  684. case I2C_IT_MTDWS:
  685. if (dev->cli.operation == I2C_READ) {
  686. while (!(readl(dev->virtbase + I2C_RISR)
  687. & I2C_IT_RXFE)) {
  688. if (dev->cli.count == 0)
  689. break;
  690. *dev->cli.buffer =
  691. readb(dev->virtbase + I2C_RFR);
  692. dev->cli.buffer++;
  693. dev->cli.count--;
  694. dev->cli.xfer_bytes++;
  695. }
  696. }
  697. disable_all_interrupts(dev);
  698. clear_all_interrupts(dev);
  699. if (dev->cli.count) {
  700. dev->result = -EIO;
  701. dev_err(&dev->adev->dev,
  702. "%lu bytes still remain to be xfered\n",
  703. dev->cli.count);
  704. (void) init_hw(dev);
  705. }
  706. complete(&dev->xfer_complete);
  707. break;
  708. /* Master Arbitration lost interrupt */
  709. case I2C_IT_MAL:
  710. dev->result = -EIO;
  711. (void) init_hw(dev);
  712. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
  713. complete(&dev->xfer_complete);
  714. break;
  715. /*
  716. * Bus Error interrupt.
  717. * This happens when an unexpected start/stop condition occurs
  718. * during the transaction.
  719. */
  720. case I2C_IT_BERR:
  721. dev->result = -EIO;
  722. /* get the status */
  723. if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
  724. (void) init_hw(dev);
  725. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
  726. complete(&dev->xfer_complete);
  727. break;
  728. /*
  729. * Tx FIFO overrun interrupt.
  730. * This is set when a write operation in Tx FIFO is performed and
  731. * the Tx FIFO is full.
  732. */
  733. case I2C_IT_TXFOVR:
  734. dev->result = -EIO;
  735. (void) init_hw(dev);
  736. dev_err(&dev->adev->dev, "Tx Fifo Over run\n");
  737. complete(&dev->xfer_complete);
  738. break;
  739. /* unhandled interrupts by this driver - TODO*/
  740. case I2C_IT_TXFE:
  741. case I2C_IT_TXFF:
  742. case I2C_IT_RXFE:
  743. case I2C_IT_RFSR:
  744. case I2C_IT_RFSE:
  745. case I2C_IT_WTSR:
  746. case I2C_IT_STD:
  747. dev_err(&dev->adev->dev, "unhandled Interrupt\n");
  748. break;
  749. default:
  750. dev_err(&dev->adev->dev, "spurious Interrupt..\n");
  751. break;
  752. }
  753. return IRQ_HANDLED;
  754. }
  755. #ifdef CONFIG_PM_SLEEP
  756. static int nmk_i2c_suspend_late(struct device *dev)
  757. {
  758. int ret;
  759. ret = pm_runtime_force_suspend(dev);
  760. if (ret)
  761. return ret;
  762. pinctrl_pm_select_sleep_state(dev);
  763. return 0;
  764. }
  765. static int nmk_i2c_resume_early(struct device *dev)
  766. {
  767. return pm_runtime_force_resume(dev);
  768. }
  769. #endif
  770. #ifdef CONFIG_PM
  771. static int nmk_i2c_runtime_suspend(struct device *dev)
  772. {
  773. struct amba_device *adev = to_amba_device(dev);
  774. struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
  775. clk_disable_unprepare(nmk_i2c->clk);
  776. pinctrl_pm_select_idle_state(dev);
  777. return 0;
  778. }
  779. static int nmk_i2c_runtime_resume(struct device *dev)
  780. {
  781. struct amba_device *adev = to_amba_device(dev);
  782. struct nmk_i2c_dev *nmk_i2c = amba_get_drvdata(adev);
  783. int ret;
  784. ret = clk_prepare_enable(nmk_i2c->clk);
  785. if (ret) {
  786. dev_err(dev, "can't prepare_enable clock\n");
  787. return ret;
  788. }
  789. pinctrl_pm_select_default_state(dev);
  790. ret = init_hw(nmk_i2c);
  791. if (ret) {
  792. clk_disable_unprepare(nmk_i2c->clk);
  793. pinctrl_pm_select_idle_state(dev);
  794. }
  795. return ret;
  796. }
  797. #endif
  798. static const struct dev_pm_ops nmk_i2c_pm = {
  799. SET_LATE_SYSTEM_SLEEP_PM_OPS(nmk_i2c_suspend_late, nmk_i2c_resume_early)
  800. SET_RUNTIME_PM_OPS(nmk_i2c_runtime_suspend,
  801. nmk_i2c_runtime_resume,
  802. NULL)
  803. };
  804. static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
  805. {
  806. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
  807. }
  808. static const struct i2c_algorithm nmk_i2c_algo = {
  809. .master_xfer = nmk_i2c_xfer,
  810. .functionality = nmk_i2c_functionality
  811. };
  812. static void nmk_i2c_of_probe(struct device_node *np,
  813. struct nmk_i2c_dev *nmk)
  814. {
  815. /* Default to 100 kHz if no frequency is given in the node */
  816. if (of_property_read_u32(np, "clock-frequency", &nmk->clk_freq))
  817. nmk->clk_freq = I2C_MAX_STANDARD_MODE_FREQ;
  818. /* This driver only supports 'standard' and 'fast' modes of operation. */
  819. if (nmk->clk_freq <= I2C_MAX_STANDARD_MODE_FREQ)
  820. nmk->sm = I2C_FREQ_MODE_STANDARD;
  821. else
  822. nmk->sm = I2C_FREQ_MODE_FAST;
  823. nmk->tft = 1; /* Tx FIFO threshold */
  824. nmk->rft = 8; /* Rx FIFO threshold */
  825. nmk->timeout = 200; /* Slave response timeout(ms) */
  826. }
  827. static int nmk_i2c_probe(struct amba_device *adev, const struct amba_id *id)
  828. {
  829. int ret = 0;
  830. struct device_node *np = adev->dev.of_node;
  831. struct nmk_i2c_dev *dev;
  832. struct i2c_adapter *adap;
  833. struct i2c_vendor_data *vendor = id->data;
  834. u32 max_fifo_threshold = (vendor->fifodepth / 2) - 1;
  835. dev = devm_kzalloc(&adev->dev, sizeof(*dev), GFP_KERNEL);
  836. if (!dev)
  837. return -ENOMEM;
  838. dev->vendor = vendor;
  839. dev->adev = adev;
  840. nmk_i2c_of_probe(np, dev);
  841. if (dev->tft > max_fifo_threshold) {
  842. dev_warn(&adev->dev, "requested TX FIFO threshold %u, adjusted down to %u\n",
  843. dev->tft, max_fifo_threshold);
  844. dev->tft = max_fifo_threshold;
  845. }
  846. if (dev->rft > max_fifo_threshold) {
  847. dev_warn(&adev->dev, "requested RX FIFO threshold %u, adjusted down to %u\n",
  848. dev->rft, max_fifo_threshold);
  849. dev->rft = max_fifo_threshold;
  850. }
  851. amba_set_drvdata(adev, dev);
  852. dev->virtbase = devm_ioremap(&adev->dev, adev->res.start,
  853. resource_size(&adev->res));
  854. if (!dev->virtbase)
  855. return -ENOMEM;
  856. dev->irq = adev->irq[0];
  857. ret = devm_request_irq(&adev->dev, dev->irq, i2c_irq_handler, 0,
  858. DRIVER_NAME, dev);
  859. if (ret) {
  860. dev_err(&adev->dev, "cannot claim the irq %d\n", dev->irq);
  861. return ret;
  862. }
  863. dev->clk = devm_clk_get_enabled(&adev->dev, NULL);
  864. if (IS_ERR(dev->clk)) {
  865. dev_err(&adev->dev, "could enable i2c clock\n");
  866. return PTR_ERR(dev->clk);
  867. }
  868. init_hw(dev);
  869. adap = &dev->adap;
  870. adap->dev.of_node = np;
  871. adap->dev.parent = &adev->dev;
  872. adap->owner = THIS_MODULE;
  873. adap->class = I2C_CLASS_DEPRECATED;
  874. adap->algo = &nmk_i2c_algo;
  875. adap->timeout = msecs_to_jiffies(dev->timeout);
  876. snprintf(adap->name, sizeof(adap->name),
  877. "Nomadik I2C at %pR", &adev->res);
  878. i2c_set_adapdata(adap, dev);
  879. dev_info(&adev->dev,
  880. "initialize %s on virtual base %p\n",
  881. adap->name, dev->virtbase);
  882. ret = i2c_add_adapter(adap);
  883. if (ret)
  884. return ret;
  885. pm_runtime_put(&adev->dev);
  886. return 0;
  887. }
  888. static void nmk_i2c_remove(struct amba_device *adev)
  889. {
  890. struct nmk_i2c_dev *dev = amba_get_drvdata(adev);
  891. i2c_del_adapter(&dev->adap);
  892. flush_i2c_fifo(dev);
  893. disable_all_interrupts(dev);
  894. clear_all_interrupts(dev);
  895. /* disable the controller */
  896. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  897. }
  898. static struct i2c_vendor_data vendor_stn8815 = {
  899. .has_mtdws = false,
  900. .fifodepth = 16, /* Guessed from TFTR/RFTR = 7 */
  901. };
  902. static struct i2c_vendor_data vendor_db8500 = {
  903. .has_mtdws = true,
  904. .fifodepth = 32, /* Guessed from TFTR/RFTR = 15 */
  905. };
  906. static const struct amba_id nmk_i2c_ids[] = {
  907. {
  908. .id = 0x00180024,
  909. .mask = 0x00ffffff,
  910. .data = &vendor_stn8815,
  911. },
  912. {
  913. .id = 0x00380024,
  914. .mask = 0x00ffffff,
  915. .data = &vendor_db8500,
  916. },
  917. {},
  918. };
  919. MODULE_DEVICE_TABLE(amba, nmk_i2c_ids);
  920. static struct amba_driver nmk_i2c_driver = {
  921. .drv = {
  922. .owner = THIS_MODULE,
  923. .name = DRIVER_NAME,
  924. .pm = &nmk_i2c_pm,
  925. },
  926. .id_table = nmk_i2c_ids,
  927. .probe = nmk_i2c_probe,
  928. .remove = nmk_i2c_remove,
  929. };
  930. static int __init nmk_i2c_init(void)
  931. {
  932. return amba_driver_register(&nmk_i2c_driver);
  933. }
  934. static void __exit nmk_i2c_exit(void)
  935. {
  936. amba_driver_unregister(&nmk_i2c_driver);
  937. }
  938. subsys_initcall(nmk_i2c_init);
  939. module_exit(nmk_i2c_exit);
  940. MODULE_AUTHOR("Sachin Verma");
  941. MODULE_AUTHOR("Srinidhi KASAGAR");
  942. MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
  943. MODULE_LICENSE("GPL");