i2c-rcar.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Renesas R-Car I2C unit
  4. *
  5. * Copyright (C) 2014-15 Wolfram Sang <wsa@sang-engineering.com>
  6. * Copyright (C) 2011-2015 Renesas Electronics Corporation
  7. *
  8. * Copyright (C) 2012-14 Renesas Solutions Corp.
  9. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  10. *
  11. * This file is based on the drivers/i2c/busses/i2c-sh7760.c
  12. * (c) 2005-2008 MSC Vertriebsges.m.b.H, Manuel Lauss <mlau@msc-ge.com>
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/i2c.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/reset.h>
  29. #include <linux/slab.h>
  30. /* register offsets */
  31. #define ICSCR 0x00 /* slave ctrl */
  32. #define ICMCR 0x04 /* master ctrl */
  33. #define ICSSR 0x08 /* slave status */
  34. #define ICMSR 0x0C /* master status */
  35. #define ICSIER 0x10 /* slave irq enable */
  36. #define ICMIER 0x14 /* master irq enable */
  37. #define ICCCR 0x18 /* clock dividers */
  38. #define ICSAR 0x1C /* slave address */
  39. #define ICMAR 0x20 /* master address */
  40. #define ICRXTX 0x24 /* data port */
  41. #define ICDMAER 0x3c /* DMA enable */
  42. #define ICFBSCR 0x38 /* first bit setup cycle */
  43. /* ICSCR */
  44. #define SDBS (1 << 3) /* slave data buffer select */
  45. #define SIE (1 << 2) /* slave interface enable */
  46. #define GCAE (1 << 1) /* general call address enable */
  47. #define FNA (1 << 0) /* forced non acknowledgment */
  48. /* ICMCR */
  49. #define MDBS (1 << 7) /* non-fifo mode switch */
  50. #define FSCL (1 << 6) /* override SCL pin */
  51. #define FSDA (1 << 5) /* override SDA pin */
  52. #define OBPC (1 << 4) /* override pins */
  53. #define MIE (1 << 3) /* master if enable */
  54. #define TSBE (1 << 2)
  55. #define FSB (1 << 1) /* force stop bit */
  56. #define ESG (1 << 0) /* enable start bit gen */
  57. /* ICSSR (also for ICSIER) */
  58. #define GCAR (1 << 6) /* general call received */
  59. #define STM (1 << 5) /* slave transmit mode */
  60. #define SSR (1 << 4) /* stop received */
  61. #define SDE (1 << 3) /* slave data empty */
  62. #define SDT (1 << 2) /* slave data transmitted */
  63. #define SDR (1 << 1) /* slave data received */
  64. #define SAR (1 << 0) /* slave addr received */
  65. /* ICMSR (also for ICMIE) */
  66. #define MNR (1 << 6) /* nack received */
  67. #define MAL (1 << 5) /* arbitration lost */
  68. #define MST (1 << 4) /* sent a stop */
  69. #define MDE (1 << 3)
  70. #define MDT (1 << 2)
  71. #define MDR (1 << 1)
  72. #define MAT (1 << 0) /* slave addr xfer done */
  73. /* ICDMAER */
  74. #define RSDMAE (1 << 3) /* DMA Slave Received Enable */
  75. #define TSDMAE (1 << 2) /* DMA Slave Transmitted Enable */
  76. #define RMDMAE (1 << 1) /* DMA Master Received Enable */
  77. #define TMDMAE (1 << 0) /* DMA Master Transmitted Enable */
  78. /* ICFBSCR */
  79. #define TCYC06 0x04 /* 6*Tcyc delay 1st bit between SDA and SCL */
  80. #define TCYC17 0x0f /* 17*Tcyc delay 1st bit between SDA and SCL */
  81. #define RCAR_BUS_PHASE_START (MDBS | MIE | ESG)
  82. #define RCAR_BUS_PHASE_DATA (MDBS | MIE)
  83. #define RCAR_BUS_MASK_DATA (~(ESG | FSB) & 0xFF)
  84. #define RCAR_BUS_PHASE_STOP (MDBS | MIE | FSB)
  85. #define RCAR_IRQ_SEND (MNR | MAL | MST | MAT | MDE)
  86. #define RCAR_IRQ_RECV (MNR | MAL | MST | MAT | MDR)
  87. #define RCAR_IRQ_STOP (MST)
  88. #define RCAR_IRQ_ACK_SEND (~(MAT | MDE) & 0x7F)
  89. #define RCAR_IRQ_ACK_RECV (~(MAT | MDR) & 0x7F)
  90. #define ID_LAST_MSG (1 << 0)
  91. #define ID_FIRST_MSG (1 << 1)
  92. #define ID_DONE (1 << 2)
  93. #define ID_ARBLOST (1 << 3)
  94. #define ID_NACK (1 << 4)
  95. /* persistent flags */
  96. #define ID_P_REP_AFTER_RD BIT(29)
  97. #define ID_P_NO_RXDMA BIT(30) /* HW forbids RXDMA sometimes */
  98. #define ID_P_PM_BLOCKED BIT(31)
  99. #define ID_P_MASK GENMASK(31, 29)
  100. enum rcar_i2c_type {
  101. I2C_RCAR_GEN1,
  102. I2C_RCAR_GEN2,
  103. I2C_RCAR_GEN3,
  104. };
  105. struct rcar_i2c_priv {
  106. u32 flags;
  107. void __iomem *io;
  108. struct i2c_adapter adap;
  109. struct i2c_msg *msg;
  110. int msgs_left;
  111. struct clk *clk;
  112. wait_queue_head_t wait;
  113. int pos;
  114. u32 icccr;
  115. u8 recovery_icmcr; /* protected by adapter lock */
  116. enum rcar_i2c_type devtype;
  117. struct i2c_client *slave;
  118. struct resource *res;
  119. struct dma_chan *dma_tx;
  120. struct dma_chan *dma_rx;
  121. struct scatterlist sg;
  122. enum dma_data_direction dma_direction;
  123. struct reset_control *rstc;
  124. int irq;
  125. };
  126. #define rcar_i2c_priv_to_dev(p) ((p)->adap.dev.parent)
  127. #define rcar_i2c_is_recv(p) ((p)->msg->flags & I2C_M_RD)
  128. #define LOOP_TIMEOUT 1024
  129. static void rcar_i2c_write(struct rcar_i2c_priv *priv, int reg, u32 val)
  130. {
  131. writel(val, priv->io + reg);
  132. }
  133. static u32 rcar_i2c_read(struct rcar_i2c_priv *priv, int reg)
  134. {
  135. return readl(priv->io + reg);
  136. }
  137. static int rcar_i2c_get_scl(struct i2c_adapter *adap)
  138. {
  139. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  140. return !!(rcar_i2c_read(priv, ICMCR) & FSCL);
  141. };
  142. static void rcar_i2c_set_scl(struct i2c_adapter *adap, int val)
  143. {
  144. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  145. if (val)
  146. priv->recovery_icmcr |= FSCL;
  147. else
  148. priv->recovery_icmcr &= ~FSCL;
  149. rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
  150. };
  151. static void rcar_i2c_set_sda(struct i2c_adapter *adap, int val)
  152. {
  153. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  154. if (val)
  155. priv->recovery_icmcr |= FSDA;
  156. else
  157. priv->recovery_icmcr &= ~FSDA;
  158. rcar_i2c_write(priv, ICMCR, priv->recovery_icmcr);
  159. };
  160. static int rcar_i2c_get_bus_free(struct i2c_adapter *adap)
  161. {
  162. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  163. return !(rcar_i2c_read(priv, ICMCR) & FSDA);
  164. };
  165. static struct i2c_bus_recovery_info rcar_i2c_bri = {
  166. .get_scl = rcar_i2c_get_scl,
  167. .set_scl = rcar_i2c_set_scl,
  168. .set_sda = rcar_i2c_set_sda,
  169. .get_bus_free = rcar_i2c_get_bus_free,
  170. .recover_bus = i2c_generic_scl_recovery,
  171. };
  172. static void rcar_i2c_init(struct rcar_i2c_priv *priv)
  173. {
  174. /* reset master mode */
  175. rcar_i2c_write(priv, ICMIER, 0);
  176. rcar_i2c_write(priv, ICMCR, MDBS);
  177. rcar_i2c_write(priv, ICMSR, 0);
  178. /* start clock */
  179. rcar_i2c_write(priv, ICCCR, priv->icccr);
  180. }
  181. static int rcar_i2c_bus_barrier(struct rcar_i2c_priv *priv)
  182. {
  183. int i;
  184. for (i = 0; i < LOOP_TIMEOUT; i++) {
  185. /* make sure that bus is not busy */
  186. if (!(rcar_i2c_read(priv, ICMCR) & FSDA))
  187. return 0;
  188. udelay(1);
  189. }
  190. /* Waiting did not help, try to recover */
  191. priv->recovery_icmcr = MDBS | OBPC | FSDA | FSCL;
  192. return i2c_recover_bus(&priv->adap);
  193. }
  194. static int rcar_i2c_clock_calculate(struct rcar_i2c_priv *priv, struct i2c_timings *t)
  195. {
  196. u32 scgd, cdf, round, ick, sum, scl, cdf_width;
  197. unsigned long rate;
  198. struct device *dev = rcar_i2c_priv_to_dev(priv);
  199. /* Fall back to previously used values if not supplied */
  200. t->bus_freq_hz = t->bus_freq_hz ?: 100000;
  201. t->scl_fall_ns = t->scl_fall_ns ?: 35;
  202. t->scl_rise_ns = t->scl_rise_ns ?: 200;
  203. t->scl_int_delay_ns = t->scl_int_delay_ns ?: 50;
  204. switch (priv->devtype) {
  205. case I2C_RCAR_GEN1:
  206. cdf_width = 2;
  207. break;
  208. case I2C_RCAR_GEN2:
  209. case I2C_RCAR_GEN3:
  210. cdf_width = 3;
  211. break;
  212. default:
  213. dev_err(dev, "device type error\n");
  214. return -EIO;
  215. }
  216. /*
  217. * calculate SCL clock
  218. * see
  219. * ICCCR
  220. *
  221. * ick = clkp / (1 + CDF)
  222. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  223. *
  224. * ick : I2C internal clock < 20 MHz
  225. * ticf : I2C SCL falling time
  226. * tr : I2C SCL rising time
  227. * intd : LSI internal delay
  228. * clkp : peripheral_clk
  229. * F[] : integer up-valuation
  230. */
  231. rate = clk_get_rate(priv->clk);
  232. cdf = rate / 20000000;
  233. if (cdf >= 1U << cdf_width) {
  234. dev_err(dev, "Input clock %lu too high\n", rate);
  235. return -EIO;
  236. }
  237. ick = rate / (cdf + 1);
  238. /*
  239. * it is impossible to calculate large scale
  240. * number on u32. separate it
  241. *
  242. * F[(ticf + tr + intd) * ick] with sum = (ticf + tr + intd)
  243. * = F[sum * ick / 1000000000]
  244. * = F[(ick / 1000000) * sum / 1000]
  245. */
  246. sum = t->scl_fall_ns + t->scl_rise_ns + t->scl_int_delay_ns;
  247. round = (ick + 500000) / 1000000 * sum;
  248. round = (round + 500) / 1000;
  249. /*
  250. * SCL = ick / (20 + SCGD * 8 + F[(ticf + tr + intd) * ick])
  251. *
  252. * Calculation result (= SCL) should be less than
  253. * bus_speed for hardware safety
  254. *
  255. * We could use something along the lines of
  256. * div = ick / (bus_speed + 1) + 1;
  257. * scgd = (div - 20 - round + 7) / 8;
  258. * scl = ick / (20 + (scgd * 8) + round);
  259. * (not fully verified) but that would get pretty involved
  260. */
  261. for (scgd = 0; scgd < 0x40; scgd++) {
  262. scl = ick / (20 + (scgd * 8) + round);
  263. if (scl <= t->bus_freq_hz)
  264. goto scgd_find;
  265. }
  266. dev_err(dev, "it is impossible to calculate best SCL\n");
  267. return -EIO;
  268. scgd_find:
  269. dev_dbg(dev, "clk %d/%d(%lu), round %u, CDF:0x%x, SCGD: 0x%x\n",
  270. scl, t->bus_freq_hz, clk_get_rate(priv->clk), round, cdf, scgd);
  271. /* keep icccr value */
  272. priv->icccr = scgd << cdf_width | cdf;
  273. return 0;
  274. }
  275. static void rcar_i2c_prepare_msg(struct rcar_i2c_priv *priv)
  276. {
  277. int read = !!rcar_i2c_is_recv(priv);
  278. priv->pos = 0;
  279. if (priv->msgs_left == 1)
  280. priv->flags |= ID_LAST_MSG;
  281. rcar_i2c_write(priv, ICMAR, i2c_8bit_addr_from_msg(priv->msg));
  282. /*
  283. * We don't have a test case but the HW engineers say that the write order
  284. * of ICMSR and ICMCR depends on whether we issue START or REP_START. Since
  285. * it didn't cause a drawback for me, let's rather be safe than sorry.
  286. */
  287. if (priv->flags & ID_FIRST_MSG) {
  288. rcar_i2c_write(priv, ICMSR, 0);
  289. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  290. } else {
  291. if (priv->flags & ID_P_REP_AFTER_RD)
  292. priv->flags &= ~ID_P_REP_AFTER_RD;
  293. else
  294. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  295. rcar_i2c_write(priv, ICMSR, 0);
  296. }
  297. rcar_i2c_write(priv, ICMIER, read ? RCAR_IRQ_RECV : RCAR_IRQ_SEND);
  298. }
  299. static void rcar_i2c_next_msg(struct rcar_i2c_priv *priv)
  300. {
  301. priv->msg++;
  302. priv->msgs_left--;
  303. priv->flags &= ID_P_MASK;
  304. rcar_i2c_prepare_msg(priv);
  305. }
  306. /*
  307. * interrupt functions
  308. */
  309. static void rcar_i2c_dma_unmap(struct rcar_i2c_priv *priv)
  310. {
  311. struct dma_chan *chan = priv->dma_direction == DMA_FROM_DEVICE
  312. ? priv->dma_rx : priv->dma_tx;
  313. /* Disable DMA Master Received/Transmitted */
  314. rcar_i2c_write(priv, ICDMAER, 0);
  315. /* Reset default delay */
  316. rcar_i2c_write(priv, ICFBSCR, TCYC06);
  317. dma_unmap_single(chan->device->dev, sg_dma_address(&priv->sg),
  318. sg_dma_len(&priv->sg), priv->dma_direction);
  319. /* Gen3 can only do one RXDMA per transfer and we just completed it */
  320. if (priv->devtype == I2C_RCAR_GEN3 &&
  321. priv->dma_direction == DMA_FROM_DEVICE)
  322. priv->flags |= ID_P_NO_RXDMA;
  323. priv->dma_direction = DMA_NONE;
  324. }
  325. static void rcar_i2c_cleanup_dma(struct rcar_i2c_priv *priv)
  326. {
  327. if (priv->dma_direction == DMA_NONE)
  328. return;
  329. else if (priv->dma_direction == DMA_FROM_DEVICE)
  330. dmaengine_terminate_all(priv->dma_rx);
  331. else if (priv->dma_direction == DMA_TO_DEVICE)
  332. dmaengine_terminate_all(priv->dma_tx);
  333. rcar_i2c_dma_unmap(priv);
  334. }
  335. static void rcar_i2c_dma_callback(void *data)
  336. {
  337. struct rcar_i2c_priv *priv = data;
  338. priv->pos += sg_dma_len(&priv->sg);
  339. rcar_i2c_dma_unmap(priv);
  340. }
  341. static void rcar_i2c_dma(struct rcar_i2c_priv *priv)
  342. {
  343. struct device *dev = rcar_i2c_priv_to_dev(priv);
  344. struct i2c_msg *msg = priv->msg;
  345. bool read = msg->flags & I2C_M_RD;
  346. enum dma_data_direction dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  347. struct dma_chan *chan = read ? priv->dma_rx : priv->dma_tx;
  348. struct dma_async_tx_descriptor *txdesc;
  349. dma_addr_t dma_addr;
  350. dma_cookie_t cookie;
  351. unsigned char *buf;
  352. int len;
  353. /* Do various checks to see if DMA is feasible at all */
  354. if (IS_ERR(chan) || msg->len < 8 || !(msg->flags & I2C_M_DMA_SAFE) ||
  355. (read && priv->flags & ID_P_NO_RXDMA))
  356. return;
  357. if (read) {
  358. /*
  359. * The last two bytes needs to be fetched using PIO in
  360. * order for the STOP phase to work.
  361. */
  362. buf = priv->msg->buf;
  363. len = priv->msg->len - 2;
  364. } else {
  365. /*
  366. * First byte in message was sent using PIO.
  367. */
  368. buf = priv->msg->buf + 1;
  369. len = priv->msg->len - 1;
  370. }
  371. dma_addr = dma_map_single(chan->device->dev, buf, len, dir);
  372. if (dma_mapping_error(chan->device->dev, dma_addr)) {
  373. dev_dbg(dev, "dma map failed, using PIO\n");
  374. return;
  375. }
  376. sg_dma_len(&priv->sg) = len;
  377. sg_dma_address(&priv->sg) = dma_addr;
  378. priv->dma_direction = dir;
  379. txdesc = dmaengine_prep_slave_sg(chan, &priv->sg, 1,
  380. read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
  381. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  382. if (!txdesc) {
  383. dev_dbg(dev, "dma prep slave sg failed, using PIO\n");
  384. rcar_i2c_cleanup_dma(priv);
  385. return;
  386. }
  387. txdesc->callback = rcar_i2c_dma_callback;
  388. txdesc->callback_param = priv;
  389. cookie = dmaengine_submit(txdesc);
  390. if (dma_submit_error(cookie)) {
  391. dev_dbg(dev, "submitting dma failed, using PIO\n");
  392. rcar_i2c_cleanup_dma(priv);
  393. return;
  394. }
  395. /* Set delay for DMA operations */
  396. rcar_i2c_write(priv, ICFBSCR, TCYC17);
  397. /* Enable DMA Master Received/Transmitted */
  398. if (read)
  399. rcar_i2c_write(priv, ICDMAER, RMDMAE);
  400. else
  401. rcar_i2c_write(priv, ICDMAER, TMDMAE);
  402. dma_async_issue_pending(chan);
  403. }
  404. static void rcar_i2c_irq_send(struct rcar_i2c_priv *priv, u32 msr)
  405. {
  406. struct i2c_msg *msg = priv->msg;
  407. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  408. if (!(msr & MDE))
  409. return;
  410. if (priv->pos < msg->len) {
  411. /*
  412. * Prepare next data to ICRXTX register.
  413. * This data will go to _SHIFT_ register.
  414. *
  415. * *
  416. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  417. */
  418. rcar_i2c_write(priv, ICRXTX, msg->buf[priv->pos]);
  419. priv->pos++;
  420. /*
  421. * Try to use DMA to transmit the rest of the data if
  422. * address transfer phase just finished.
  423. */
  424. if (msr & MAT)
  425. rcar_i2c_dma(priv);
  426. } else {
  427. /*
  428. * The last data was pushed to ICRXTX on _PREV_ empty irq.
  429. * It is on _SHIFT_ register, and will sent to I2C bus.
  430. *
  431. * *
  432. * [ICRXTX] -> [SHIFT] -> [I2C bus]
  433. */
  434. if (priv->flags & ID_LAST_MSG) {
  435. /*
  436. * If current msg is the _LAST_ msg,
  437. * prepare stop condition here.
  438. * ID_DONE will be set on STOP irq.
  439. */
  440. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  441. } else {
  442. rcar_i2c_next_msg(priv);
  443. return;
  444. }
  445. }
  446. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_SEND);
  447. }
  448. static void rcar_i2c_irq_recv(struct rcar_i2c_priv *priv, u32 msr)
  449. {
  450. struct i2c_msg *msg = priv->msg;
  451. /* FIXME: sometimes, unknown interrupt happened. Do nothing */
  452. if (!(msr & MDR))
  453. return;
  454. if (msr & MAT) {
  455. /*
  456. * Address transfer phase finished, but no data at this point.
  457. * Try to use DMA to receive data.
  458. */
  459. rcar_i2c_dma(priv);
  460. } else if (priv->pos < msg->len) {
  461. /* get received data */
  462. msg->buf[priv->pos] = rcar_i2c_read(priv, ICRXTX);
  463. priv->pos++;
  464. }
  465. /* If next received data is the _LAST_, go to new phase. */
  466. if (priv->pos + 1 == msg->len) {
  467. if (priv->flags & ID_LAST_MSG) {
  468. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_STOP);
  469. } else {
  470. rcar_i2c_write(priv, ICMCR, RCAR_BUS_PHASE_START);
  471. priv->flags |= ID_P_REP_AFTER_RD;
  472. }
  473. }
  474. if (priv->pos == msg->len && !(priv->flags & ID_LAST_MSG))
  475. rcar_i2c_next_msg(priv);
  476. else
  477. rcar_i2c_write(priv, ICMSR, RCAR_IRQ_ACK_RECV);
  478. }
  479. static bool rcar_i2c_slave_irq(struct rcar_i2c_priv *priv)
  480. {
  481. u32 ssr_raw, ssr_filtered;
  482. u8 value;
  483. ssr_raw = rcar_i2c_read(priv, ICSSR) & 0xff;
  484. ssr_filtered = ssr_raw & rcar_i2c_read(priv, ICSIER);
  485. if (!ssr_filtered)
  486. return false;
  487. /* address detected */
  488. if (ssr_filtered & SAR) {
  489. /* read or write request */
  490. if (ssr_raw & STM) {
  491. i2c_slave_event(priv->slave, I2C_SLAVE_READ_REQUESTED, &value);
  492. rcar_i2c_write(priv, ICRXTX, value);
  493. rcar_i2c_write(priv, ICSIER, SDE | SSR | SAR);
  494. } else {
  495. i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_REQUESTED, &value);
  496. rcar_i2c_read(priv, ICRXTX); /* dummy read */
  497. rcar_i2c_write(priv, ICSIER, SDR | SSR | SAR);
  498. }
  499. /* Clear SSR, too, because of old STOPs to other clients than us */
  500. rcar_i2c_write(priv, ICSSR, ~(SAR | SSR) & 0xff);
  501. }
  502. /* master sent stop */
  503. if (ssr_filtered & SSR) {
  504. i2c_slave_event(priv->slave, I2C_SLAVE_STOP, &value);
  505. rcar_i2c_write(priv, ICSCR, SIE | SDBS); /* clear our NACK */
  506. rcar_i2c_write(priv, ICSIER, SAR);
  507. rcar_i2c_write(priv, ICSSR, ~SSR & 0xff);
  508. }
  509. /* master wants to write to us */
  510. if (ssr_filtered & SDR) {
  511. int ret;
  512. value = rcar_i2c_read(priv, ICRXTX);
  513. ret = i2c_slave_event(priv->slave, I2C_SLAVE_WRITE_RECEIVED, &value);
  514. /* Send NACK in case of error */
  515. rcar_i2c_write(priv, ICSCR, SIE | SDBS | (ret < 0 ? FNA : 0));
  516. rcar_i2c_write(priv, ICSSR, ~SDR & 0xff);
  517. }
  518. /* master wants to read from us */
  519. if (ssr_filtered & SDE) {
  520. i2c_slave_event(priv->slave, I2C_SLAVE_READ_PROCESSED, &value);
  521. rcar_i2c_write(priv, ICRXTX, value);
  522. rcar_i2c_write(priv, ICSSR, ~SDE & 0xff);
  523. }
  524. return true;
  525. }
  526. static irqreturn_t rcar_i2c_irq(int irq, void *ptr)
  527. {
  528. struct rcar_i2c_priv *priv = ptr;
  529. u32 msr, val;
  530. /* Clear START or STOP immediately, except for REPSTART after read */
  531. if (likely(!(priv->flags & ID_P_REP_AFTER_RD))) {
  532. val = rcar_i2c_read(priv, ICMCR);
  533. rcar_i2c_write(priv, ICMCR, val & RCAR_BUS_MASK_DATA);
  534. }
  535. msr = rcar_i2c_read(priv, ICMSR);
  536. /* Only handle interrupts that are currently enabled */
  537. msr &= rcar_i2c_read(priv, ICMIER);
  538. if (!msr) {
  539. if (rcar_i2c_slave_irq(priv))
  540. return IRQ_HANDLED;
  541. return IRQ_NONE;
  542. }
  543. /* Arbitration lost */
  544. if (msr & MAL) {
  545. priv->flags |= ID_DONE | ID_ARBLOST;
  546. goto out;
  547. }
  548. /* Nack */
  549. if (msr & MNR) {
  550. /* HW automatically sends STOP after received NACK */
  551. rcar_i2c_write(priv, ICMIER, RCAR_IRQ_STOP);
  552. priv->flags |= ID_NACK;
  553. goto out;
  554. }
  555. /* Stop */
  556. if (msr & MST) {
  557. priv->msgs_left--; /* The last message also made it */
  558. priv->flags |= ID_DONE;
  559. goto out;
  560. }
  561. if (rcar_i2c_is_recv(priv))
  562. rcar_i2c_irq_recv(priv, msr);
  563. else
  564. rcar_i2c_irq_send(priv, msr);
  565. out:
  566. if (priv->flags & ID_DONE) {
  567. rcar_i2c_write(priv, ICMIER, 0);
  568. rcar_i2c_write(priv, ICMSR, 0);
  569. wake_up(&priv->wait);
  570. }
  571. return IRQ_HANDLED;
  572. }
  573. static struct dma_chan *rcar_i2c_request_dma_chan(struct device *dev,
  574. enum dma_transfer_direction dir,
  575. dma_addr_t port_addr)
  576. {
  577. struct dma_chan *chan;
  578. struct dma_slave_config cfg;
  579. char *chan_name = dir == DMA_MEM_TO_DEV ? "tx" : "rx";
  580. int ret;
  581. chan = dma_request_chan(dev, chan_name);
  582. if (IS_ERR(chan)) {
  583. dev_dbg(dev, "request_channel failed for %s (%ld)\n",
  584. chan_name, PTR_ERR(chan));
  585. return chan;
  586. }
  587. memset(&cfg, 0, sizeof(cfg));
  588. cfg.direction = dir;
  589. if (dir == DMA_MEM_TO_DEV) {
  590. cfg.dst_addr = port_addr;
  591. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  592. } else {
  593. cfg.src_addr = port_addr;
  594. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  595. }
  596. ret = dmaengine_slave_config(chan, &cfg);
  597. if (ret) {
  598. dev_dbg(dev, "slave_config failed for %s (%d)\n",
  599. chan_name, ret);
  600. dma_release_channel(chan);
  601. return ERR_PTR(ret);
  602. }
  603. dev_dbg(dev, "got DMA channel for %s\n", chan_name);
  604. return chan;
  605. }
  606. static void rcar_i2c_request_dma(struct rcar_i2c_priv *priv,
  607. struct i2c_msg *msg)
  608. {
  609. struct device *dev = rcar_i2c_priv_to_dev(priv);
  610. bool read;
  611. struct dma_chan *chan;
  612. enum dma_transfer_direction dir;
  613. read = msg->flags & I2C_M_RD;
  614. chan = read ? priv->dma_rx : priv->dma_tx;
  615. if (PTR_ERR(chan) != -EPROBE_DEFER)
  616. return;
  617. dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  618. chan = rcar_i2c_request_dma_chan(dev, dir, priv->res->start + ICRXTX);
  619. if (read)
  620. priv->dma_rx = chan;
  621. else
  622. priv->dma_tx = chan;
  623. }
  624. static void rcar_i2c_release_dma(struct rcar_i2c_priv *priv)
  625. {
  626. if (!IS_ERR(priv->dma_tx)) {
  627. dma_release_channel(priv->dma_tx);
  628. priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  629. }
  630. if (!IS_ERR(priv->dma_rx)) {
  631. dma_release_channel(priv->dma_rx);
  632. priv->dma_rx = ERR_PTR(-EPROBE_DEFER);
  633. }
  634. }
  635. /* I2C is a special case, we need to poll the status of a reset */
  636. static int rcar_i2c_do_reset(struct rcar_i2c_priv *priv)
  637. {
  638. int i, ret;
  639. ret = reset_control_reset(priv->rstc);
  640. if (ret)
  641. return ret;
  642. for (i = 0; i < LOOP_TIMEOUT; i++) {
  643. ret = reset_control_status(priv->rstc);
  644. if (ret == 0)
  645. return 0;
  646. udelay(1);
  647. }
  648. return -ETIMEDOUT;
  649. }
  650. static int rcar_i2c_master_xfer(struct i2c_adapter *adap,
  651. struct i2c_msg *msgs,
  652. int num)
  653. {
  654. struct rcar_i2c_priv *priv = i2c_get_adapdata(adap);
  655. struct device *dev = rcar_i2c_priv_to_dev(priv);
  656. int i, ret;
  657. long time_left;
  658. pm_runtime_get_sync(dev);
  659. /* Check bus state before init otherwise bus busy info will be lost */
  660. ret = rcar_i2c_bus_barrier(priv);
  661. if (ret < 0)
  662. goto out;
  663. /* Gen3 needs a reset before allowing RXDMA once */
  664. if (priv->devtype == I2C_RCAR_GEN3) {
  665. priv->flags |= ID_P_NO_RXDMA;
  666. if (!IS_ERR(priv->rstc)) {
  667. ret = rcar_i2c_do_reset(priv);
  668. if (ret == 0)
  669. priv->flags &= ~ID_P_NO_RXDMA;
  670. }
  671. }
  672. rcar_i2c_init(priv);
  673. for (i = 0; i < num; i++)
  674. rcar_i2c_request_dma(priv, msgs + i);
  675. /* init first message */
  676. priv->msg = msgs;
  677. priv->msgs_left = num;
  678. priv->flags = (priv->flags & ID_P_MASK) | ID_FIRST_MSG;
  679. rcar_i2c_prepare_msg(priv);
  680. time_left = wait_event_timeout(priv->wait, priv->flags & ID_DONE,
  681. num * adap->timeout);
  682. /* cleanup DMA if it couldn't complete properly due to an error */
  683. if (priv->dma_direction != DMA_NONE)
  684. rcar_i2c_cleanup_dma(priv);
  685. if (!time_left) {
  686. rcar_i2c_init(priv);
  687. ret = -ETIMEDOUT;
  688. } else if (priv->flags & ID_NACK) {
  689. ret = -ENXIO;
  690. } else if (priv->flags & ID_ARBLOST) {
  691. ret = -EAGAIN;
  692. } else {
  693. ret = num - priv->msgs_left; /* The number of transfer */
  694. }
  695. out:
  696. pm_runtime_put(dev);
  697. if (ret < 0 && ret != -ENXIO)
  698. dev_err(dev, "error %d : %x\n", ret, priv->flags);
  699. return ret;
  700. }
  701. static int rcar_reg_slave(struct i2c_client *slave)
  702. {
  703. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  704. if (priv->slave)
  705. return -EBUSY;
  706. if (slave->flags & I2C_CLIENT_TEN)
  707. return -EAFNOSUPPORT;
  708. /* Keep device active for slave address detection logic */
  709. pm_runtime_get_sync(rcar_i2c_priv_to_dev(priv));
  710. priv->slave = slave;
  711. rcar_i2c_write(priv, ICSAR, slave->addr);
  712. rcar_i2c_write(priv, ICSSR, 0);
  713. rcar_i2c_write(priv, ICSIER, SAR);
  714. rcar_i2c_write(priv, ICSCR, SIE | SDBS);
  715. return 0;
  716. }
  717. static int rcar_unreg_slave(struct i2c_client *slave)
  718. {
  719. struct rcar_i2c_priv *priv = i2c_get_adapdata(slave->adapter);
  720. WARN_ON(!priv->slave);
  721. /* ensure no irq is running before clearing ptr */
  722. disable_irq(priv->irq);
  723. rcar_i2c_write(priv, ICSIER, 0);
  724. rcar_i2c_write(priv, ICSSR, 0);
  725. enable_irq(priv->irq);
  726. rcar_i2c_write(priv, ICSCR, SDBS);
  727. rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
  728. priv->slave = NULL;
  729. pm_runtime_put(rcar_i2c_priv_to_dev(priv));
  730. return 0;
  731. }
  732. static u32 rcar_i2c_func(struct i2c_adapter *adap)
  733. {
  734. /*
  735. * This HW can't do:
  736. * I2C_SMBUS_QUICK (setting FSB during START didn't work)
  737. * I2C_M_NOSTART (automatically sends address after START)
  738. * I2C_M_IGNORE_NAK (automatically sends STOP after NAK)
  739. */
  740. return I2C_FUNC_I2C | I2C_FUNC_SLAVE |
  741. (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
  742. }
  743. static const struct i2c_algorithm rcar_i2c_algo = {
  744. .master_xfer = rcar_i2c_master_xfer,
  745. .functionality = rcar_i2c_func,
  746. .reg_slave = rcar_reg_slave,
  747. .unreg_slave = rcar_unreg_slave,
  748. };
  749. static const struct i2c_adapter_quirks rcar_i2c_quirks = {
  750. .flags = I2C_AQ_NO_ZERO_LEN,
  751. };
  752. static const struct of_device_id rcar_i2c_dt_ids[] = {
  753. { .compatible = "renesas,i2c-r8a7778", .data = (void *)I2C_RCAR_GEN1 },
  754. { .compatible = "renesas,i2c-r8a7779", .data = (void *)I2C_RCAR_GEN1 },
  755. { .compatible = "renesas,i2c-r8a7790", .data = (void *)I2C_RCAR_GEN2 },
  756. { .compatible = "renesas,i2c-r8a7791", .data = (void *)I2C_RCAR_GEN2 },
  757. { .compatible = "renesas,i2c-r8a7792", .data = (void *)I2C_RCAR_GEN2 },
  758. { .compatible = "renesas,i2c-r8a7793", .data = (void *)I2C_RCAR_GEN2 },
  759. { .compatible = "renesas,i2c-r8a7794", .data = (void *)I2C_RCAR_GEN2 },
  760. { .compatible = "renesas,i2c-r8a7795", .data = (void *)I2C_RCAR_GEN3 },
  761. { .compatible = "renesas,i2c-r8a7796", .data = (void *)I2C_RCAR_GEN3 },
  762. { .compatible = "renesas,i2c-rcar", .data = (void *)I2C_RCAR_GEN1 }, /* Deprecated */
  763. { .compatible = "renesas,rcar-gen1-i2c", .data = (void *)I2C_RCAR_GEN1 },
  764. { .compatible = "renesas,rcar-gen2-i2c", .data = (void *)I2C_RCAR_GEN2 },
  765. { .compatible = "renesas,rcar-gen3-i2c", .data = (void *)I2C_RCAR_GEN3 },
  766. {},
  767. };
  768. MODULE_DEVICE_TABLE(of, rcar_i2c_dt_ids);
  769. static int rcar_i2c_probe(struct platform_device *pdev)
  770. {
  771. struct rcar_i2c_priv *priv;
  772. struct i2c_adapter *adap;
  773. struct device *dev = &pdev->dev;
  774. struct i2c_timings i2c_t;
  775. int ret;
  776. priv = devm_kzalloc(dev, sizeof(struct rcar_i2c_priv), GFP_KERNEL);
  777. if (!priv)
  778. return -ENOMEM;
  779. priv->clk = devm_clk_get(dev, NULL);
  780. if (IS_ERR(priv->clk)) {
  781. dev_err(dev, "cannot get clock\n");
  782. return PTR_ERR(priv->clk);
  783. }
  784. priv->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  785. priv->io = devm_ioremap_resource(dev, priv->res);
  786. if (IS_ERR(priv->io))
  787. return PTR_ERR(priv->io);
  788. priv->devtype = (enum rcar_i2c_type)of_device_get_match_data(dev);
  789. init_waitqueue_head(&priv->wait);
  790. adap = &priv->adap;
  791. adap->nr = pdev->id;
  792. adap->algo = &rcar_i2c_algo;
  793. adap->class = I2C_CLASS_DEPRECATED;
  794. adap->retries = 3;
  795. adap->dev.parent = dev;
  796. adap->dev.of_node = dev->of_node;
  797. adap->bus_recovery_info = &rcar_i2c_bri;
  798. adap->quirks = &rcar_i2c_quirks;
  799. i2c_set_adapdata(adap, priv);
  800. strlcpy(adap->name, pdev->name, sizeof(adap->name));
  801. i2c_parse_fw_timings(dev, &i2c_t, false);
  802. /* Init DMA */
  803. sg_init_table(&priv->sg, 1);
  804. priv->dma_direction = DMA_NONE;
  805. priv->dma_rx = priv->dma_tx = ERR_PTR(-EPROBE_DEFER);
  806. /* Activate device for clock calculation */
  807. pm_runtime_enable(dev);
  808. pm_runtime_get_sync(dev);
  809. ret = rcar_i2c_clock_calculate(priv, &i2c_t);
  810. if (ret < 0)
  811. goto out_pm_put;
  812. rcar_i2c_write(priv, ICSAR, 0); /* Gen2: must be 0 if not using slave */
  813. if (priv->devtype == I2C_RCAR_GEN3) {
  814. priv->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  815. if (!IS_ERR(priv->rstc)) {
  816. ret = reset_control_status(priv->rstc);
  817. if (ret < 0)
  818. priv->rstc = ERR_PTR(-ENOTSUPP);
  819. }
  820. }
  821. /* Stay always active when multi-master to keep arbitration working */
  822. if (of_property_read_bool(dev->of_node, "multi-master"))
  823. priv->flags |= ID_P_PM_BLOCKED;
  824. else
  825. pm_runtime_put(dev);
  826. priv->irq = platform_get_irq(pdev, 0);
  827. ret = devm_request_irq(dev, priv->irq, rcar_i2c_irq, 0, dev_name(dev), priv);
  828. if (ret < 0) {
  829. dev_err(dev, "cannot get irq %d\n", priv->irq);
  830. goto out_pm_disable;
  831. }
  832. platform_set_drvdata(pdev, priv);
  833. ret = i2c_add_numbered_adapter(adap);
  834. if (ret < 0)
  835. goto out_pm_disable;
  836. dev_info(dev, "probed\n");
  837. return 0;
  838. out_pm_put:
  839. pm_runtime_put(dev);
  840. out_pm_disable:
  841. pm_runtime_disable(dev);
  842. return ret;
  843. }
  844. static int rcar_i2c_remove(struct platform_device *pdev)
  845. {
  846. struct rcar_i2c_priv *priv = platform_get_drvdata(pdev);
  847. struct device *dev = &pdev->dev;
  848. i2c_del_adapter(&priv->adap);
  849. rcar_i2c_release_dma(priv);
  850. if (priv->flags & ID_P_PM_BLOCKED)
  851. pm_runtime_put(dev);
  852. pm_runtime_disable(dev);
  853. return 0;
  854. }
  855. static struct platform_driver rcar_i2c_driver = {
  856. .driver = {
  857. .name = "i2c-rcar",
  858. .of_match_table = rcar_i2c_dt_ids,
  859. },
  860. .probe = rcar_i2c_probe,
  861. .remove = rcar_i2c_remove,
  862. };
  863. module_platform_driver(rcar_i2c_driver);
  864. MODULE_LICENSE("GPL v2");
  865. MODULE_DESCRIPTION("Renesas R-Car I2C bus driver");
  866. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");