i2c-rcar.c 26 KB

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