i2c-uniphier-f.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. {
  238. priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
  239. uniphier_fi2c_set_irqs(priv);
  240. /* do not use TX byte counter */
  241. writel(0, priv->membase + UNIPHIER_FI2C_TBC);
  242. /* set slave address */
  243. writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
  244. priv->membase + UNIPHIER_FI2C_DTTX);
  245. /* first chunk of data */
  246. uniphier_fi2c_fill_txfifo(priv, true);
  247. }
  248. static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
  249. {
  250. priv->flags |= UNIPHIER_FI2C_RD;
  251. if (likely(priv->len < 256)) {
  252. /*
  253. * If possible, use RX byte counter.
  254. * It can automatically handle NACK for the last byte.
  255. */
  256. writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
  257. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
  258. UNIPHIER_FI2C_INT_RB;
  259. } else {
  260. /*
  261. * The byte counter can not count over 256. In this case,
  262. * do not use it at all. Drain data when FIFO gets full,
  263. * but treat the last portion as a special case.
  264. */
  265. writel(0, priv->membase + UNIPHIER_FI2C_RBC);
  266. priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
  267. priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
  268. }
  269. uniphier_fi2c_set_irqs(priv);
  270. /* set slave address with RD bit */
  271. writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
  272. priv->membase + UNIPHIER_FI2C_DTTX);
  273. }
  274. static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
  275. {
  276. writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
  277. }
  278. static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
  279. {
  280. writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
  281. priv->membase + UNIPHIER_FI2C_BRST);
  282. }
  283. static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
  284. {
  285. uniphier_fi2c_reset(priv);
  286. i2c_recover_bus(&priv->adap);
  287. }
  288. static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
  289. struct i2c_msg *msg, bool repeat,
  290. bool stop)
  291. {
  292. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  293. bool is_read = msg->flags & I2C_M_RD;
  294. unsigned long time_left, flags;
  295. dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, repeat=%d, stop=%d\n",
  296. is_read ? "receive" : "transmit", msg->addr, msg->len,
  297. repeat, stop);
  298. priv->len = msg->len;
  299. priv->buf = msg->buf;
  300. priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
  301. priv->error = 0;
  302. priv->flags = 0;
  303. if (stop)
  304. priv->flags |= UNIPHIER_FI2C_STOP;
  305. reinit_completion(&priv->comp);
  306. uniphier_fi2c_clear_irqs(priv, U32_MAX);
  307. writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
  308. priv->membase + UNIPHIER_FI2C_RST); /* reset TX/RX FIFO */
  309. spin_lock_irqsave(&priv->lock, flags);
  310. if (is_read)
  311. uniphier_fi2c_rx_init(priv, msg->addr);
  312. else
  313. uniphier_fi2c_tx_init(priv, msg->addr);
  314. dev_dbg(&adap->dev, "start condition\n");
  315. /*
  316. * For a repeated START condition, writing a slave address to the FIFO
  317. * kicks the controller. So, the UNIPHIER_FI2C_CR register should be
  318. * written only for a non-repeated START condition.
  319. */
  320. if (!repeat)
  321. writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
  322. priv->membase + UNIPHIER_FI2C_CR);
  323. spin_unlock_irqrestore(&priv->lock, flags);
  324. time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
  325. spin_lock_irqsave(&priv->lock, flags);
  326. priv->enabled_irqs = 0;
  327. uniphier_fi2c_set_irqs(priv);
  328. spin_unlock_irqrestore(&priv->lock, flags);
  329. if (!time_left) {
  330. dev_err(&adap->dev, "transaction timeout.\n");
  331. uniphier_fi2c_recover(priv);
  332. return -ETIMEDOUT;
  333. }
  334. dev_dbg(&adap->dev, "complete\n");
  335. if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
  336. u32 status;
  337. int ret;
  338. ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
  339. status,
  340. (status & UNIPHIER_FI2C_SR_STS) &&
  341. !(status & UNIPHIER_FI2C_SR_BB),
  342. 1, 20);
  343. if (ret) {
  344. dev_err(&adap->dev,
  345. "stop condition was not completed.\n");
  346. uniphier_fi2c_recover(priv);
  347. return ret;
  348. }
  349. }
  350. return priv->error;
  351. }
  352. static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
  353. {
  354. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  355. if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
  356. if (priv->busy_cnt++ > 3) {
  357. /*
  358. * If bus busy continues too long, it is probably
  359. * in a wrong state. Try bus recovery.
  360. */
  361. uniphier_fi2c_recover(priv);
  362. priv->busy_cnt = 0;
  363. }
  364. return -EAGAIN;
  365. }
  366. priv->busy_cnt = 0;
  367. return 0;
  368. }
  369. static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
  370. struct i2c_msg *msgs, int num)
  371. {
  372. struct i2c_msg *msg, *emsg = msgs + num;
  373. bool repeat = false;
  374. int ret;
  375. ret = uniphier_fi2c_check_bus_busy(adap);
  376. if (ret)
  377. return ret;
  378. for (msg = msgs; msg < emsg; msg++) {
  379. /* Emit STOP if it is the last message or I2C_M_STOP is set. */
  380. bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
  381. ret = uniphier_fi2c_master_xfer_one(adap, msg, repeat, stop);
  382. if (ret)
  383. return ret;
  384. repeat = !stop;
  385. }
  386. return num;
  387. }
  388. static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
  389. {
  390. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  391. }
  392. static const struct i2c_algorithm uniphier_fi2c_algo = {
  393. .master_xfer = uniphier_fi2c_master_xfer,
  394. .functionality = uniphier_fi2c_functionality,
  395. };
  396. static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
  397. {
  398. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  399. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  400. UNIPHIER_FI2C_BM_SCLS);
  401. }
  402. static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
  403. {
  404. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  405. writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
  406. priv->membase + UNIPHIER_FI2C_BRST);
  407. }
  408. static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
  409. {
  410. struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
  411. return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
  412. UNIPHIER_FI2C_BM_SDAS);
  413. }
  414. static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
  415. {
  416. uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
  417. }
  418. static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
  419. .recover_bus = i2c_generic_scl_recovery,
  420. .get_scl = uniphier_fi2c_get_scl,
  421. .set_scl = uniphier_fi2c_set_scl,
  422. .get_sda = uniphier_fi2c_get_sda,
  423. .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
  424. };
  425. static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv)
  426. {
  427. unsigned int cyc = priv->clk_cycle;
  428. u32 tmp;
  429. tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
  430. tmp |= UNIPHIER_FI2C_CR_MST;
  431. writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
  432. uniphier_fi2c_reset(priv);
  433. /*
  434. * Standard-mode: tLOW + tHIGH = 10 us
  435. * Fast-mode: tLOW + tHIGH = 2.5 us
  436. */
  437. writel(cyc, priv->membase + UNIPHIER_FI2C_CYC);
  438. /*
  439. * Standard-mode: tLOW = 4.7 us, tHIGH = 4.0 us, tBUF = 4.7 us
  440. * Fast-mode: tLOW = 1.3 us, tHIGH = 0.6 us, tBUF = 1.3 us
  441. * "tLow/tHIGH = 5/4" meets both.
  442. */
  443. writel(cyc * 5 / 9, priv->membase + UNIPHIER_FI2C_LCTL);
  444. /*
  445. * Standard-mode: tHD;STA = 4.0 us, tSU;STA = 4.7 us, tSU;STO = 4.0 us
  446. * Fast-mode: tHD;STA = 0.6 us, tSU;STA = 0.6 us, tSU;STO = 0.6 us
  447. */
  448. writel(cyc / 2, priv->membase + UNIPHIER_FI2C_SSUT);
  449. /*
  450. * Standard-mode: tSU;DAT = 250 ns
  451. * Fast-mode: tSU;DAT = 100 ns
  452. */
  453. writel(cyc / 16, priv->membase + UNIPHIER_FI2C_DSUT);
  454. uniphier_fi2c_prepare_operation(priv);
  455. }
  456. static int uniphier_fi2c_probe(struct platform_device *pdev)
  457. {
  458. struct device *dev = &pdev->dev;
  459. struct uniphier_fi2c_priv *priv;
  460. struct resource *regs;
  461. u32 bus_speed;
  462. unsigned long clk_rate;
  463. int irq, ret;
  464. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  465. if (!priv)
  466. return -ENOMEM;
  467. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  468. priv->membase = devm_ioremap_resource(dev, regs);
  469. if (IS_ERR(priv->membase))
  470. return PTR_ERR(priv->membase);
  471. irq = platform_get_irq(pdev, 0);
  472. if (irq < 0) {
  473. dev_err(dev, "failed to get IRQ number\n");
  474. return irq;
  475. }
  476. if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
  477. bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
  478. if (!bus_speed || bus_speed > UNIPHIER_FI2C_MAX_SPEED) {
  479. dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
  480. return -EINVAL;
  481. }
  482. priv->clk = devm_clk_get(dev, NULL);
  483. if (IS_ERR(priv->clk)) {
  484. dev_err(dev, "failed to get clock\n");
  485. return PTR_ERR(priv->clk);
  486. }
  487. ret = clk_prepare_enable(priv->clk);
  488. if (ret)
  489. return ret;
  490. clk_rate = clk_get_rate(priv->clk);
  491. if (!clk_rate) {
  492. dev_err(dev, "input clock rate should not be zero\n");
  493. ret = -EINVAL;
  494. goto disable_clk;
  495. }
  496. priv->clk_cycle = clk_rate / bus_speed;
  497. init_completion(&priv->comp);
  498. spin_lock_init(&priv->lock);
  499. priv->adap.owner = THIS_MODULE;
  500. priv->adap.algo = &uniphier_fi2c_algo;
  501. priv->adap.dev.parent = dev;
  502. priv->adap.dev.of_node = dev->of_node;
  503. strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
  504. priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
  505. i2c_set_adapdata(&priv->adap, priv);
  506. platform_set_drvdata(pdev, priv);
  507. uniphier_fi2c_hw_init(priv);
  508. ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
  509. pdev->name, priv);
  510. if (ret) {
  511. dev_err(dev, "failed to request irq %d\n", irq);
  512. goto disable_clk;
  513. }
  514. ret = i2c_add_adapter(&priv->adap);
  515. disable_clk:
  516. if (ret)
  517. clk_disable_unprepare(priv->clk);
  518. return ret;
  519. }
  520. static int uniphier_fi2c_remove(struct platform_device *pdev)
  521. {
  522. struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
  523. i2c_del_adapter(&priv->adap);
  524. clk_disable_unprepare(priv->clk);
  525. return 0;
  526. }
  527. static int __maybe_unused uniphier_fi2c_suspend(struct device *dev)
  528. {
  529. struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
  530. clk_disable_unprepare(priv->clk);
  531. return 0;
  532. }
  533. static int __maybe_unused uniphier_fi2c_resume(struct device *dev)
  534. {
  535. struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
  536. int ret;
  537. ret = clk_prepare_enable(priv->clk);
  538. if (ret)
  539. return ret;
  540. uniphier_fi2c_hw_init(priv);
  541. return 0;
  542. }
  543. static const struct dev_pm_ops uniphier_fi2c_pm_ops = {
  544. SET_SYSTEM_SLEEP_PM_OPS(uniphier_fi2c_suspend, uniphier_fi2c_resume)
  545. };
  546. static const struct of_device_id uniphier_fi2c_match[] = {
  547. { .compatible = "socionext,uniphier-fi2c" },
  548. { /* sentinel */ }
  549. };
  550. MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
  551. static struct platform_driver uniphier_fi2c_drv = {
  552. .probe = uniphier_fi2c_probe,
  553. .remove = uniphier_fi2c_remove,
  554. .driver = {
  555. .name = "uniphier-fi2c",
  556. .of_match_table = uniphier_fi2c_match,
  557. .pm = &uniphier_fi2c_pm_ops,
  558. },
  559. };
  560. module_platform_driver(uniphier_fi2c_drv);
  561. MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
  562. MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
  563. MODULE_LICENSE("GPL");