i2c-uniphier-f.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/i2c.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #define UNIPHIER_FI2C_CR 0x00 /* control register */
  22. #define UNIPHIER_FI2C_CR_MST BIT(3) /* master mode */
  23. #define UNIPHIER_FI2C_CR_STA BIT(2) /* start condition */
  24. #define UNIPHIER_FI2C_CR_STO BIT(1) /* stop condition */
  25. #define UNIPHIER_FI2C_CR_NACK BIT(0) /* do not return ACK */
  26. #define UNIPHIER_FI2C_DTTX 0x04 /* TX FIFO */
  27. #define UNIPHIER_FI2C_DTTX_CMD BIT(8) /* send command (slave addr) */
  28. #define UNIPHIER_FI2C_DTTX_RD BIT(0) /* read transaction */
  29. #define UNIPHIER_FI2C_DTRX 0x04 /* RX FIFO */
  30. #define UNIPHIER_FI2C_SLAD 0x0c /* slave address */
  31. #define UNIPHIER_FI2C_CYC 0x10 /* clock cycle control */
  32. #define UNIPHIER_FI2C_LCTL 0x14 /* clock low period control */
  33. #define UNIPHIER_FI2C_SSUT 0x18 /* restart/stop setup time control */
  34. #define UNIPHIER_FI2C_DSUT 0x1c /* data setup time control */
  35. #define UNIPHIER_FI2C_INT 0x20 /* interrupt status */
  36. #define UNIPHIER_FI2C_IE 0x24 /* interrupt enable */
  37. #define UNIPHIER_FI2C_IC 0x28 /* interrupt clear */
  38. #define UNIPHIER_FI2C_INT_TE BIT(9) /* TX FIFO empty */
  39. #define UNIPHIER_FI2C_INT_RF BIT(8) /* RX FIFO full */
  40. #define UNIPHIER_FI2C_INT_TC BIT(7) /* send complete (STOP) */
  41. #define UNIPHIER_FI2C_INT_RC BIT(6) /* receive complete (STOP) */
  42. #define UNIPHIER_FI2C_INT_TB BIT(5) /* sent specified bytes */
  43. #define UNIPHIER_FI2C_INT_RB BIT(4) /* received specified bytes */
  44. #define UNIPHIER_FI2C_INT_NA BIT(2) /* no ACK */
  45. #define UNIPHIER_FI2C_INT_AL BIT(1) /* arbitration lost */
  46. #define UNIPHIER_FI2C_SR 0x2c /* status register */
  47. #define UNIPHIER_FI2C_SR_DB BIT(12) /* device busy */
  48. #define UNIPHIER_FI2C_SR_STS BIT(11) /* stop condition detected */
  49. #define UNIPHIER_FI2C_SR_BB BIT(8) /* bus busy */
  50. #define UNIPHIER_FI2C_SR_RFF BIT(3) /* RX FIFO full */
  51. #define UNIPHIER_FI2C_SR_RNE BIT(2) /* RX FIFO not empty */
  52. #define UNIPHIER_FI2C_SR_TNF BIT(1) /* TX FIFO not full */
  53. #define UNIPHIER_FI2C_SR_TFE BIT(0) /* TX FIFO empty */
  54. #define UNIPHIER_FI2C_RST 0x34 /* reset control */
  55. #define UNIPHIER_FI2C_RST_TBRST BIT(2) /* clear TX FIFO */
  56. #define UNIPHIER_FI2C_RST_RBRST BIT(1) /* clear RX FIFO */
  57. #define UNIPHIER_FI2C_RST_RST BIT(0) /* forcible bus reset */
  58. #define UNIPHIER_FI2C_BM 0x38 /* bus monitor */
  59. #define UNIPHIER_FI2C_BM_SDAO BIT(3) /* output for SDA line */
  60. #define UNIPHIER_FI2C_BM_SDAS BIT(2) /* readback of SDA line */
  61. #define UNIPHIER_FI2C_BM_SCLO BIT(1) /* output for SCL line */
  62. #define UNIPHIER_FI2C_BM_SCLS BIT(0) /* readback of SCL line */
  63. #define UNIPHIER_FI2C_NOISE 0x3c /* noise filter control */
  64. #define UNIPHIER_FI2C_TBC 0x40 /* TX byte count setting */
  65. #define UNIPHIER_FI2C_RBC 0x44 /* RX byte count setting */
  66. #define UNIPHIER_FI2C_TBCM 0x48 /* TX byte count monitor */
  67. #define UNIPHIER_FI2C_RBCM 0x4c /* RX byte count monitor */
  68. #define UNIPHIER_FI2C_BRST 0x50 /* bus reset */
  69. #define UNIPHIER_FI2C_BRST_FOEN BIT(1) /* normal operation */
  70. #define UNIPHIER_FI2C_BRST_RSCL BIT(0) /* release SCL */
  71. #define UNIPHIER_FI2C_INT_FAULTS \
  72. (UNIPHIER_FI2C_INT_NA | UNIPHIER_FI2C_INT_AL)
  73. #define UNIPHIER_FI2C_INT_STOP \
  74. (UNIPHIER_FI2C_INT_TC | UNIPHIER_FI2C_INT_RC)
  75. #define UNIPHIER_FI2C_RD BIT(0)
  76. #define UNIPHIER_FI2C_STOP BIT(1)
  77. #define UNIPHIER_FI2C_MANUAL_NACK BIT(2)
  78. #define UNIPHIER_FI2C_BYTE_WISE BIT(3)
  79. #define UNIPHIER_FI2C_DEFER_STOP_COMP BIT(4)
  80. #define UNIPHIER_FI2C_DEFAULT_SPEED 100000
  81. #define UNIPHIER_FI2C_MAX_SPEED 400000
  82. #define UNIPHIER_FI2C_FIFO_SIZE 8
  83. struct uniphier_fi2c_priv {
  84. struct completion comp;
  85. struct i2c_adapter adap;
  86. void __iomem *membase;
  87. struct clk *clk;
  88. unsigned int len;
  89. u8 *buf;
  90. u32 enabled_irqs;
  91. int error;
  92. unsigned int flags;
  93. unsigned int busy_cnt;
  94. unsigned int clk_cycle;
  95. spinlock_t lock; /* IRQ synchronization */
  96. };
  97. static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
  98. bool first)
  99. {
  100. int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
  101. /*
  102. * TX-FIFO stores slave address in it for the first access.
  103. * Decrement the counter.
  104. */
  105. if (first)
  106. fifo_space--;
  107. while (priv->len) {
  108. if (fifo_space-- <= 0)
  109. break;
  110. dev_dbg(&priv->adap.dev, "write data: %02x\n", *priv->buf);
  111. writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
  112. priv->len--;
  113. }
  114. }
  115. static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
  116. {
  117. int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
  118. 1 : UNIPHIER_FI2C_FIFO_SIZE;
  119. while (priv->len) {
  120. if (fifo_left-- <= 0)
  121. break;
  122. *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
  123. dev_dbg(&priv->adap.dev, "read data: %02x\n", priv->buf[-1]);
  124. priv->len--;
  125. }
  126. }
  127. static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
  128. {
  129. writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
  130. }
  131. static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv,
  132. u32 mask)
  133. {
  134. writel(mask, priv->membase + UNIPHIER_FI2C_IC);
  135. }
  136. static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
  137. {
  138. dev_dbg(&priv->adap.dev, "stop condition\n");
  139. priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
  140. uniphier_fi2c_set_irqs(priv);
  141. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
  142. priv->membase + UNIPHIER_FI2C_CR);
  143. }
  144. static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
  145. {
  146. struct uniphier_fi2c_priv *priv = dev_id;
  147. u32 irq_status;
  148. spin_lock(&priv->lock);
  149. irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
  150. irq_status &= priv->enabled_irqs;
  151. dev_dbg(&priv->adap.dev,
  152. "interrupt: enabled_irqs=%04x, irq_status=%04x\n",
  153. priv->enabled_irqs, irq_status);
  154. if (irq_status & UNIPHIER_FI2C_INT_STOP)
  155. goto complete;
  156. if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
  157. dev_dbg(&priv->adap.dev, "arbitration lost\n");
  158. priv->error = -EAGAIN;
  159. goto complete;
  160. }
  161. if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
  162. dev_dbg(&priv->adap.dev, "could not get ACK\n");
  163. priv->error = -ENXIO;
  164. if (priv->flags & UNIPHIER_FI2C_RD) {
  165. /*
  166. * work around a hardware bug:
  167. * The receive-completed interrupt is never set even if
  168. * STOP condition is detected after the address phase
  169. * of read transaction fails to get ACK.
  170. * To avoid time-out error, we issue STOP here,
  171. * but do not wait for its completion.
  172. * It should be checked after exiting this handler.
  173. */
  174. uniphier_fi2c_stop(priv);
  175. priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
  176. goto complete;
  177. }
  178. goto stop;
  179. }
  180. if (irq_status & UNIPHIER_FI2C_INT_TE) {
  181. if (!priv->len)
  182. goto data_done;
  183. uniphier_fi2c_fill_txfifo(priv, false);
  184. goto handled;
  185. }
  186. if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
  187. uniphier_fi2c_drain_rxfifo(priv);
  188. /*
  189. * If the number of bytes to read is multiple of the FIFO size
  190. * (msg->len == 8, 16, 24, ...), the INT_RF bit is set a little
  191. * earlier than INT_RB. We wait for INT_RB to confirm the
  192. * completion of the current message.
  193. */
  194. if (!priv->len && (irq_status & UNIPHIER_FI2C_INT_RB))
  195. goto data_done;
  196. if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
  197. if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
  198. !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
  199. dev_dbg(&priv->adap.dev,
  200. "enable read byte count IRQ\n");
  201. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
  202. uniphier_fi2c_set_irqs(priv);
  203. priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
  204. }
  205. if (priv->len <= 1) {
  206. dev_dbg(&priv->adap.dev, "set NACK\n");
  207. writel(UNIPHIER_FI2C_CR_MST |
  208. UNIPHIER_FI2C_CR_NACK,
  209. priv->membase + UNIPHIER_FI2C_CR);
  210. }
  211. }
  212. goto handled;
  213. }
  214. spin_unlock(&priv->lock);
  215. return IRQ_NONE;
  216. data_done:
  217. if (priv->flags & UNIPHIER_FI2C_STOP) {
  218. stop:
  219. uniphier_fi2c_stop(priv);
  220. } else {
  221. complete:
  222. priv->enabled_irqs = 0;
  223. uniphier_fi2c_set_irqs(priv);
  224. complete(&priv->comp);
  225. }
  226. handled:
  227. /*
  228. * This controller makes a pause while any bit of the IRQ status is
  229. * asserted. Clear the asserted bit to kick the controller just before
  230. * exiting the handler.
  231. */
  232. uniphier_fi2c_clear_irqs(priv, irq_status);
  233. spin_unlock(&priv->lock);
  234. return IRQ_HANDLED;
  235. }
  236. static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr,
  237. bool repeat)
  238. {
  239. priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
  240. uniphier_fi2c_set_irqs(priv);
  241. /* do not use TX byte counter */
  242. writel(0, priv->membase + UNIPHIER_FI2C_TBC);
  243. /* set slave address */
  244. writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
  245. priv->membase + UNIPHIER_FI2C_DTTX);
  246. /*
  247. * First chunk of data. For a repeated START condition, do not write
  248. * data to the TX fifo here to avoid the timing issue.
  249. */
  250. if (!repeat)
  251. uniphier_fi2c_fill_txfifo(priv, true);
  252. }
  253. static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  254. {
  255. priv->flags |= UNIPHIER_FI2C_RD;
  256. if (likely(priv->len < 256)) {
  257. /*
  258. * If possible, use RX byte counter.
  259. * It can automatically handle NACK for the last byte.
  260. */
  261. writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
  262. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
  263. UNIPHIER_FI2C_INT_RB;
  264. } else {
  265. /*
  266. * The byte counter can not count over 256. In this case,
  267. * do not use it at all. Drain data when FIFO gets full,
  268. * but treat the last portion as a special case.
  269. */
  270. writel(0, priv->membase + UNIPHIER_FI2C_RBC);
  271. priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
  272. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
  273. }
  274. uniphier_fi2c_set_irqs(priv);
  275. /* set slave address with RD bit */
  276. writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
  277. priv->membase + UNIPHIER_FI2C_DTTX);
  278. }
  279. static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
  280. {
  281. writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
  282. }
  283. static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
  284. {
  285. writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
  286. priv->membase + UNIPHIER_FI2C_BRST);
  287. }
  288. static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
  289. {
  290. uniphier_fi2c_reset(priv);
  291. i2c_recover_bus(&priv->adap);
  292. }
  293. static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
  294. struct i2c_msg *msg, bool repeat,
  295. bool stop)
  296. {
  297. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  298. bool is_read = msg->flags & I2C_M_RD;
  299. unsigned long time_left, flags;
  300. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, repeat=%d, stop=%d\n",
  301. is_read ? "receive" : "transmit", msg->addr, msg->len,
  302. repeat, stop);
  303. priv->len = msg->len;
  304. priv->buf = msg->buf;
  305. priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
  306. priv->error = 0;
  307. priv->flags = 0;
  308. if (stop)
  309. priv->flags |= UNIPHIER_FI2C_STOP;
  310. reinit_completion(&priv->comp);
  311. uniphier_fi2c_clear_irqs(priv, U32_MAX);
  312. writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
  313. priv->membase + UNIPHIER_FI2C_RST); /* reset TX/RX FIFO */
  314. spin_lock_irqsave(&priv->lock, flags);
  315. if (is_read)
  316. uniphier_fi2c_rx_init(priv, msg->addr);
  317. else
  318. uniphier_fi2c_tx_init(priv, msg->addr, repeat);
  319. dev_dbg(&adap->dev, "start condition\n");
  320. /*
  321. * For a repeated START condition, writing a slave address to the FIFO
  322. * kicks the controller. So, the UNIPHIER_FI2C_CR register should be
  323. * written only for a non-repeated START condition.
  324. */
  325. if (!repeat)
  326. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
  327. priv->membase + UNIPHIER_FI2C_CR);
  328. spin_unlock_irqrestore(&priv->lock, flags);
  329. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  330. spin_lock_irqsave(&priv->lock, flags);
  331. priv->enabled_irqs = 0;
  332. uniphier_fi2c_set_irqs(priv);
  333. spin_unlock_irqrestore(&priv->lock, flags);
  334. if (!time_left) {
  335. dev_err(&adap->dev, "transaction timeout.\n");
  336. uniphier_fi2c_recover(priv);
  337. return -ETIMEDOUT;
  338. }
  339. dev_dbg(&adap->dev, "complete\n");
  340. if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
  341. u32 status;
  342. int ret;
  343. ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
  344. status,
  345. (status & UNIPHIER_FI2C_SR_STS) &&
  346. !(status & UNIPHIER_FI2C_SR_BB),
  347. 1, 20);
  348. if (ret) {
  349. dev_err(&adap->dev,
  350. "stop condition was not completed.\n");
  351. uniphier_fi2c_recover(priv);
  352. return ret;
  353. }
  354. }
  355. return priv->error;
  356. }
  357. static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
  358. {
  359. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  360. if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
  361. if (priv->busy_cnt++ > 3) {
  362. /*
  363. * If bus busy continues too long, it is probably
  364. * in a wrong state. Try bus recovery.
  365. */
  366. uniphier_fi2c_recover(priv);
  367. priv->busy_cnt = 0;
  368. }
  369. return -EAGAIN;
  370. }
  371. priv->busy_cnt = 0;
  372. return 0;
  373. }
  374. static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
  375. struct i2c_msg *msgs, int num)
  376. {
  377. struct i2c_msg *msg, *emsg = msgs + num;
  378. bool repeat = false;
  379. int ret;
  380. ret = uniphier_fi2c_check_bus_busy(adap);
  381. if (ret)
  382. return ret;
  383. for (msg = msgs; msg < emsg; msg++) {
  384. /* Emit STOP if it is the last message or I2C_M_STOP is set. */
  385. bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
  386. ret = uniphier_fi2c_master_xfer_one(adap, msg, repeat, stop);
  387. if (ret)
  388. return ret;
  389. repeat = !stop;
  390. }
  391. return num;
  392. }
  393. static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
  394. {
  395. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  396. }
  397. static const struct i2c_algorithm uniphier_fi2c_algo = {
  398. .master_xfer = uniphier_fi2c_master_xfer,
  399. .functionality = uniphier_fi2c_functionality,
  400. };
  401. static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
  402. {
  403. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  404. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  405. UNIPHIER_FI2C_BM_SCLS);
  406. }
  407. static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
  408. {
  409. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  410. writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
  411. priv->membase + UNIPHIER_FI2C_BRST);
  412. }
  413. static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
  414. {
  415. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  416. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  417. UNIPHIER_FI2C_BM_SDAS);
  418. }
  419. static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
  420. {
  421. uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
  422. }
  423. static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
  424. .recover_bus = i2c_generic_scl_recovery,
  425. .get_scl = uniphier_fi2c_get_scl,
  426. .set_scl = uniphier_fi2c_set_scl,
  427. .get_sda = uniphier_fi2c_get_sda,
  428. .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
  429. };
  430. static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv)
  431. {
  432. unsigned int cyc = priv->clk_cycle;
  433. u32 tmp;
  434. tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
  435. tmp |= UNIPHIER_FI2C_CR_MST;
  436. writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
  437. uniphier_fi2c_reset(priv);
  438. /*
  439. * Standard-mode: tLOW + tHIGH = 10 us
  440. * Fast-mode: tLOW + tHIGH = 2.5 us
  441. */
  442. writel(cyc, priv->membase + UNIPHIER_FI2C_CYC);
  443. /*
  444. * Standard-mode: tLOW = 4.7 us, tHIGH = 4.0 us, tBUF = 4.7 us
  445. * Fast-mode: tLOW = 1.3 us, tHIGH = 0.6 us, tBUF = 1.3 us
  446. * "tLow/tHIGH = 5/4" meets both.
  447. */
  448. writel(cyc * 5 / 9, priv->membase + UNIPHIER_FI2C_LCTL);
  449. /*
  450. * Standard-mode: tHD;STA = 4.0 us, tSU;STA = 4.7 us, tSU;STO = 4.0 us
  451. * Fast-mode: tHD;STA = 0.6 us, tSU;STA = 0.6 us, tSU;STO = 0.6 us
  452. */
  453. writel(cyc / 2, priv->membase + UNIPHIER_FI2C_SSUT);
  454. /*
  455. * Standard-mode: tSU;DAT = 250 ns
  456. * Fast-mode: tSU;DAT = 100 ns
  457. */
  458. writel(cyc / 16, priv->membase + UNIPHIER_FI2C_DSUT);
  459. uniphier_fi2c_prepare_operation(priv);
  460. }
  461. static int uniphier_fi2c_probe(struct platform_device *pdev)
  462. {
  463. struct device *dev = &pdev->dev;
  464. struct uniphier_fi2c_priv *priv;
  465. struct resource *regs;
  466. u32 bus_speed;
  467. unsigned long clk_rate;
  468. int irq, ret;
  469. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  470. if (!priv)
  471. return -ENOMEM;
  472. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  473. priv->membase = devm_ioremap_resource(dev, regs);
  474. if (IS_ERR(priv->membase))
  475. return PTR_ERR(priv->membase);
  476. irq = platform_get_irq(pdev, 0);
  477. if (irq < 0) {
  478. dev_err(dev, "failed to get IRQ number\n");
  479. return irq;
  480. }
  481. if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
  482. bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
  483. if (!bus_speed || bus_speed > UNIPHIER_FI2C_MAX_SPEED) {
  484. dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
  485. return -EINVAL;
  486. }
  487. priv->clk = devm_clk_get(dev, NULL);
  488. if (IS_ERR(priv->clk)) {
  489. dev_err(dev, "failed to get clock\n");
  490. return PTR_ERR(priv->clk);
  491. }
  492. ret = clk_prepare_enable(priv->clk);
  493. if (ret)
  494. return ret;
  495. clk_rate = clk_get_rate(priv->clk);
  496. if (!clk_rate) {
  497. dev_err(dev, "input clock rate should not be zero\n");
  498. ret = -EINVAL;
  499. goto disable_clk;
  500. }
  501. priv->clk_cycle = clk_rate / bus_speed;
  502. init_completion(&priv->comp);
  503. spin_lock_init(&priv->lock);
  504. priv->adap.owner = THIS_MODULE;
  505. priv->adap.algo = &uniphier_fi2c_algo;
  506. priv->adap.dev.parent = dev;
  507. priv->adap.dev.of_node = dev->of_node;
  508. strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
  509. priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
  510. i2c_set_adapdata(&priv->adap, priv);
  511. platform_set_drvdata(pdev, priv);
  512. uniphier_fi2c_hw_init(priv);
  513. ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
  514. pdev->name, priv);
  515. if (ret) {
  516. dev_err(dev, "failed to request irq %d\n", irq);
  517. goto disable_clk;
  518. }
  519. ret = i2c_add_adapter(&priv->adap);
  520. disable_clk:
  521. if (ret)
  522. clk_disable_unprepare(priv->clk);
  523. return ret;
  524. }
  525. static int uniphier_fi2c_remove(struct platform_device *pdev)
  526. {
  527. struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
  528. i2c_del_adapter(&priv->adap);
  529. clk_disable_unprepare(priv->clk);
  530. return 0;
  531. }
  532. static int __maybe_unused uniphier_fi2c_suspend(struct device *dev)
  533. {
  534. struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
  535. clk_disable_unprepare(priv->clk);
  536. return 0;
  537. }
  538. static int __maybe_unused uniphier_fi2c_resume(struct device *dev)
  539. {
  540. struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
  541. int ret;
  542. ret = clk_prepare_enable(priv->clk);
  543. if (ret)
  544. return ret;
  545. uniphier_fi2c_hw_init(priv);
  546. return 0;
  547. }
  548. static const struct dev_pm_ops uniphier_fi2c_pm_ops = {
  549. SET_SYSTEM_SLEEP_PM_OPS(uniphier_fi2c_suspend, uniphier_fi2c_resume)
  550. };
  551. static const struct of_device_id uniphier_fi2c_match[] = {
  552. { .compatible = "socionext,uniphier-fi2c" },
  553. { /* sentinel */ }
  554. };
  555. MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
  556. static struct platform_driver uniphier_fi2c_drv = {
  557. .probe = uniphier_fi2c_probe,
  558. .remove = uniphier_fi2c_remove,
  559. .driver = {
  560. .name = "uniphier-fi2c",
  561. .of_match_table = uniphier_fi2c_match,
  562. .pm = &uniphier_fi2c_pm_ops,
  563. },
  564. };
  565. module_platform_driver(uniphier_fi2c_drv);
  566. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  567. MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
  568. MODULE_LICENSE("GPL");