i2c-rcar.c 25 KB

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