i2c-altera.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright Intel Corporation (C) 2017.
  4. *
  5. * Based on the i2c-axxia.c driver.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clkdev.h>
  9. #include <linux/err.h>
  10. #include <linux/i2c.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #define ALTR_I2C_TFR_CMD 0x00 /* Transfer Command register */
  18. #define ALTR_I2C_TFR_CMD_STA BIT(9) /* send START before byte */
  19. #define ALTR_I2C_TFR_CMD_STO BIT(8) /* send STOP after byte */
  20. #define ALTR_I2C_TFR_CMD_RW_D BIT(0) /* Direction of transfer */
  21. #define ALTR_I2C_RX_DATA 0x04 /* RX data FIFO register */
  22. #define ALTR_I2C_CTRL 0x08 /* Control register */
  23. #define ALTR_I2C_CTRL_RXT_SHFT 4 /* RX FIFO Threshold */
  24. #define ALTR_I2C_CTRL_TCT_SHFT 2 /* TFER CMD FIFO Threshold */
  25. #define ALTR_I2C_CTRL_BSPEED BIT(1) /* Bus Speed (1=Fast) */
  26. #define ALTR_I2C_CTRL_EN BIT(0) /* Enable Core (1=Enable) */
  27. #define ALTR_I2C_ISER 0x0C /* Interrupt Status Enable register */
  28. #define ALTR_I2C_ISER_RXOF_EN BIT(4) /* Enable RX OVERFLOW IRQ */
  29. #define ALTR_I2C_ISER_ARB_EN BIT(3) /* Enable ARB LOST IRQ */
  30. #define ALTR_I2C_ISER_NACK_EN BIT(2) /* Enable NACK DET IRQ */
  31. #define ALTR_I2C_ISER_RXRDY_EN BIT(1) /* Enable RX Ready IRQ */
  32. #define ALTR_I2C_ISER_TXRDY_EN BIT(0) /* Enable TX Ready IRQ */
  33. #define ALTR_I2C_ISR 0x10 /* Interrupt Status register */
  34. #define ALTR_I2C_ISR_RXOF BIT(4) /* RX OVERFLOW IRQ */
  35. #define ALTR_I2C_ISR_ARB BIT(3) /* ARB LOST IRQ */
  36. #define ALTR_I2C_ISR_NACK BIT(2) /* NACK DET IRQ */
  37. #define ALTR_I2C_ISR_RXRDY BIT(1) /* RX Ready IRQ */
  38. #define ALTR_I2C_ISR_TXRDY BIT(0) /* TX Ready IRQ */
  39. #define ALTR_I2C_STATUS 0x14 /* Status register */
  40. #define ALTR_I2C_STAT_CORE BIT(0) /* Core Status (0=idle) */
  41. #define ALTR_I2C_TC_FIFO_LVL 0x18 /* Transfer FIFO LVL register */
  42. #define ALTR_I2C_RX_FIFO_LVL 0x1C /* Receive FIFO LVL register */
  43. #define ALTR_I2C_SCL_LOW 0x20 /* SCL low count register */
  44. #define ALTR_I2C_SCL_HIGH 0x24 /* SCL high count register */
  45. #define ALTR_I2C_SDA_HOLD 0x28 /* SDA hold count register */
  46. #define ALTR_I2C_ALL_IRQ (ALTR_I2C_ISR_RXOF | ALTR_I2C_ISR_ARB | \
  47. ALTR_I2C_ISR_NACK | ALTR_I2C_ISR_RXRDY | \
  48. ALTR_I2C_ISR_TXRDY)
  49. #define ALTR_I2C_THRESHOLD 0 /* IRQ Threshold at 1 element */
  50. #define ALTR_I2C_DFLT_FIFO_SZ 4
  51. #define ALTR_I2C_TIMEOUT 100000 /* 100ms */
  52. #define ALTR_I2C_XFER_TIMEOUT (msecs_to_jiffies(250))
  53. /**
  54. * struct altr_i2c_dev - I2C device context
  55. * @base: pointer to register struct
  56. * @msg: pointer to current message
  57. * @msg_len: number of bytes transferred in msg
  58. * @msg_err: error code for completed message
  59. * @msg_complete: xfer completion object
  60. * @dev: device reference
  61. * @adapter: core i2c abstraction
  62. * @i2c_clk: clock reference for i2c input clock
  63. * @bus_clk_rate: current i2c bus clock rate
  64. * @buf: ptr to msg buffer for easier use.
  65. * @fifo_size: size of the FIFO passed in.
  66. * @isr_mask: cached copy of local ISR enables.
  67. * @isr_status: cached copy of local ISR status.
  68. * @isr_mutex: mutex for IRQ thread.
  69. */
  70. struct altr_i2c_dev {
  71. void __iomem *base;
  72. struct i2c_msg *msg;
  73. size_t msg_len;
  74. int msg_err;
  75. struct completion msg_complete;
  76. struct device *dev;
  77. struct i2c_adapter adapter;
  78. struct clk *i2c_clk;
  79. u32 bus_clk_rate;
  80. u8 *buf;
  81. u32 fifo_size;
  82. u32 isr_mask;
  83. u32 isr_status;
  84. struct mutex isr_mutex;
  85. };
  86. static void
  87. altr_i2c_int_enable(struct altr_i2c_dev *idev, u32 mask, bool enable)
  88. {
  89. u32 int_en;
  90. int_en = readl(idev->base + ALTR_I2C_ISER);
  91. if (enable)
  92. idev->isr_mask = int_en | mask;
  93. else
  94. idev->isr_mask = int_en & ~mask;
  95. writel(idev->isr_mask, idev->base + ALTR_I2C_ISER);
  96. }
  97. static void altr_i2c_int_clear(struct altr_i2c_dev *idev, u32 mask)
  98. {
  99. u32 int_en = readl(idev->base + ALTR_I2C_ISR);
  100. writel(int_en | mask, idev->base + ALTR_I2C_ISR);
  101. }
  102. static void altr_i2c_core_disable(struct altr_i2c_dev *idev)
  103. {
  104. u32 tmp = readl(idev->base + ALTR_I2C_CTRL);
  105. writel(tmp & ~ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL);
  106. }
  107. static void altr_i2c_core_enable(struct altr_i2c_dev *idev)
  108. {
  109. u32 tmp = readl(idev->base + ALTR_I2C_CTRL);
  110. writel(tmp | ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL);
  111. }
  112. static void altr_i2c_reset(struct altr_i2c_dev *idev)
  113. {
  114. altr_i2c_core_disable(idev);
  115. altr_i2c_core_enable(idev);
  116. }
  117. static inline void altr_i2c_stop(struct altr_i2c_dev *idev)
  118. {
  119. writel(ALTR_I2C_TFR_CMD_STO, idev->base + ALTR_I2C_TFR_CMD);
  120. }
  121. static void altr_i2c_init(struct altr_i2c_dev *idev)
  122. {
  123. u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate;
  124. u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
  125. u32 tmp = (ALTR_I2C_THRESHOLD << ALTR_I2C_CTRL_RXT_SHFT) |
  126. (ALTR_I2C_THRESHOLD << ALTR_I2C_CTRL_TCT_SHFT);
  127. u32 t_high, t_low;
  128. if (idev->bus_clk_rate <= I2C_MAX_STANDARD_MODE_FREQ) {
  129. tmp &= ~ALTR_I2C_CTRL_BSPEED;
  130. /* Standard mode SCL 50/50 */
  131. t_high = divisor * 1 / 2;
  132. t_low = divisor * 1 / 2;
  133. } else {
  134. tmp |= ALTR_I2C_CTRL_BSPEED;
  135. /* Fast mode SCL 33/66 */
  136. t_high = divisor * 1 / 3;
  137. t_low = divisor * 2 / 3;
  138. }
  139. writel(tmp, idev->base + ALTR_I2C_CTRL);
  140. dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
  141. idev->bus_clk_rate, clk_mhz, divisor);
  142. /* Reset controller */
  143. altr_i2c_reset(idev);
  144. /* SCL High Time */
  145. writel(t_high, idev->base + ALTR_I2C_SCL_HIGH);
  146. /* SCL Low Time */
  147. writel(t_low, idev->base + ALTR_I2C_SCL_LOW);
  148. /* SDA Hold Time, 300ns */
  149. writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);
  150. /* Mask all master interrupt bits */
  151. altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
  152. }
  153. /*
  154. * altr_i2c_transfer - On the last byte to be transmitted, send
  155. * a Stop bit on the last byte.
  156. */
  157. static void altr_i2c_transfer(struct altr_i2c_dev *idev, u32 data)
  158. {
  159. /* On the last byte to be transmitted, send STOP */
  160. if (idev->msg_len == 1)
  161. data |= ALTR_I2C_TFR_CMD_STO;
  162. if (idev->msg_len > 0)
  163. writel(data, idev->base + ALTR_I2C_TFR_CMD);
  164. }
  165. /*
  166. * altr_i2c_empty_rx_fifo - Fetch data from RX FIFO until end of
  167. * transfer. Send a Stop bit on the last byte.
  168. */
  169. static void altr_i2c_empty_rx_fifo(struct altr_i2c_dev *idev)
  170. {
  171. size_t rx_fifo_avail = readl(idev->base + ALTR_I2C_RX_FIFO_LVL);
  172. int bytes_to_transfer = min(rx_fifo_avail, idev->msg_len);
  173. while (bytes_to_transfer-- > 0) {
  174. *idev->buf++ = readl(idev->base + ALTR_I2C_RX_DATA);
  175. idev->msg_len--;
  176. altr_i2c_transfer(idev, 0);
  177. }
  178. }
  179. /*
  180. * altr_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
  181. */
  182. static int altr_i2c_fill_tx_fifo(struct altr_i2c_dev *idev)
  183. {
  184. size_t tx_fifo_avail = idev->fifo_size - readl(idev->base +
  185. ALTR_I2C_TC_FIFO_LVL);
  186. int bytes_to_transfer = min(tx_fifo_avail, idev->msg_len);
  187. int ret = idev->msg_len - bytes_to_transfer;
  188. while (bytes_to_transfer-- > 0) {
  189. altr_i2c_transfer(idev, *idev->buf++);
  190. idev->msg_len--;
  191. }
  192. return ret;
  193. }
  194. static irqreturn_t altr_i2c_isr_quick(int irq, void *_dev)
  195. {
  196. struct altr_i2c_dev *idev = _dev;
  197. irqreturn_t ret = IRQ_HANDLED;
  198. /* Read IRQ status but only interested in Enabled IRQs. */
  199. idev->isr_status = readl(idev->base + ALTR_I2C_ISR) & idev->isr_mask;
  200. if (idev->isr_status)
  201. ret = IRQ_WAKE_THREAD;
  202. return ret;
  203. }
  204. static irqreturn_t altr_i2c_isr(int irq, void *_dev)
  205. {
  206. int ret;
  207. bool read, finish = false;
  208. struct altr_i2c_dev *idev = _dev;
  209. u32 status = idev->isr_status;
  210. mutex_lock(&idev->isr_mutex);
  211. if (!idev->msg) {
  212. dev_warn(idev->dev, "unexpected interrupt\n");
  213. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  214. goto out;
  215. }
  216. read = (idev->msg->flags & I2C_M_RD) != 0;
  217. /* handle Lost Arbitration */
  218. if (unlikely(status & ALTR_I2C_ISR_ARB)) {
  219. altr_i2c_int_clear(idev, ALTR_I2C_ISR_ARB);
  220. idev->msg_err = -EAGAIN;
  221. finish = true;
  222. } else if (unlikely(status & ALTR_I2C_ISR_NACK)) {
  223. dev_dbg(idev->dev, "Could not get ACK\n");
  224. idev->msg_err = -ENXIO;
  225. altr_i2c_int_clear(idev, ALTR_I2C_ISR_NACK);
  226. altr_i2c_stop(idev);
  227. finish = true;
  228. } else if (read && unlikely(status & ALTR_I2C_ISR_RXOF)) {
  229. /* handle RX FIFO Overflow */
  230. altr_i2c_empty_rx_fifo(idev);
  231. altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY);
  232. altr_i2c_stop(idev);
  233. dev_err(idev->dev, "RX FIFO Overflow\n");
  234. finish = true;
  235. } else if (read && (status & ALTR_I2C_ISR_RXRDY)) {
  236. /* RX FIFO needs service? */
  237. altr_i2c_empty_rx_fifo(idev);
  238. altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY);
  239. if (!idev->msg_len)
  240. finish = true;
  241. } else if (!read && (status & ALTR_I2C_ISR_TXRDY)) {
  242. /* TX FIFO needs service? */
  243. altr_i2c_int_clear(idev, ALTR_I2C_ISR_TXRDY);
  244. if (idev->msg_len > 0)
  245. altr_i2c_fill_tx_fifo(idev);
  246. else
  247. finish = true;
  248. } else {
  249. dev_warn(idev->dev, "Unexpected interrupt: 0x%x\n", status);
  250. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  251. }
  252. if (finish) {
  253. /* Wait for the Core to finish */
  254. ret = readl_poll_timeout_atomic(idev->base + ALTR_I2C_STATUS,
  255. status,
  256. !(status & ALTR_I2C_STAT_CORE),
  257. 1, ALTR_I2C_TIMEOUT);
  258. if (ret)
  259. dev_err(idev->dev, "message timeout\n");
  260. altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
  261. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  262. complete(&idev->msg_complete);
  263. dev_dbg(idev->dev, "Message Complete\n");
  264. }
  265. out:
  266. mutex_unlock(&idev->isr_mutex);
  267. return IRQ_HANDLED;
  268. }
  269. static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
  270. {
  271. u32 imask = ALTR_I2C_ISR_RXOF | ALTR_I2C_ISR_ARB | ALTR_I2C_ISR_NACK;
  272. unsigned long time_left;
  273. u32 value;
  274. u8 addr = i2c_8bit_addr_from_msg(msg);
  275. mutex_lock(&idev->isr_mutex);
  276. idev->msg = msg;
  277. idev->msg_len = msg->len;
  278. idev->buf = msg->buf;
  279. idev->msg_err = 0;
  280. reinit_completion(&idev->msg_complete);
  281. altr_i2c_core_enable(idev);
  282. /* Make sure RX FIFO is empty */
  283. do {
  284. readl(idev->base + ALTR_I2C_RX_DATA);
  285. } while (readl(idev->base + ALTR_I2C_RX_FIFO_LVL));
  286. writel(ALTR_I2C_TFR_CMD_STA | addr, idev->base + ALTR_I2C_TFR_CMD);
  287. if ((msg->flags & I2C_M_RD) != 0) {
  288. imask |= ALTR_I2C_ISER_RXOF_EN | ALTR_I2C_ISER_RXRDY_EN;
  289. altr_i2c_int_enable(idev, imask, true);
  290. /* write the first byte to start the RX */
  291. altr_i2c_transfer(idev, 0);
  292. } else {
  293. imask |= ALTR_I2C_ISR_TXRDY;
  294. altr_i2c_int_enable(idev, imask, true);
  295. altr_i2c_fill_tx_fifo(idev);
  296. }
  297. mutex_unlock(&idev->isr_mutex);
  298. time_left = wait_for_completion_timeout(&idev->msg_complete,
  299. ALTR_I2C_XFER_TIMEOUT);
  300. mutex_lock(&idev->isr_mutex);
  301. altr_i2c_int_enable(idev, imask, false);
  302. value = readl(idev->base + ALTR_I2C_STATUS) & ALTR_I2C_STAT_CORE;
  303. if (value)
  304. dev_err(idev->dev, "Core Status not IDLE...\n");
  305. if (time_left == 0) {
  306. idev->msg_err = -ETIMEDOUT;
  307. dev_dbg(idev->dev, "Transaction timed out.\n");
  308. }
  309. altr_i2c_core_disable(idev);
  310. mutex_unlock(&idev->isr_mutex);
  311. return idev->msg_err;
  312. }
  313. static int
  314. altr_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  315. {
  316. struct altr_i2c_dev *idev = i2c_get_adapdata(adap);
  317. int i, ret;
  318. for (i = 0; i < num; i++) {
  319. ret = altr_i2c_xfer_msg(idev, msgs++);
  320. if (ret)
  321. return ret;
  322. }
  323. return num;
  324. }
  325. static u32 altr_i2c_func(struct i2c_adapter *adap)
  326. {
  327. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  328. }
  329. static const struct i2c_algorithm altr_i2c_algo = {
  330. .master_xfer = altr_i2c_xfer,
  331. .functionality = altr_i2c_func,
  332. };
  333. static int altr_i2c_probe(struct platform_device *pdev)
  334. {
  335. struct altr_i2c_dev *idev = NULL;
  336. int irq, ret;
  337. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  338. if (!idev)
  339. return -ENOMEM;
  340. idev->base = devm_platform_ioremap_resource(pdev, 0);
  341. if (IS_ERR(idev->base))
  342. return PTR_ERR(idev->base);
  343. irq = platform_get_irq(pdev, 0);
  344. if (irq < 0)
  345. return irq;
  346. idev->i2c_clk = devm_clk_get(&pdev->dev, NULL);
  347. if (IS_ERR(idev->i2c_clk)) {
  348. dev_err(&pdev->dev, "missing clock\n");
  349. return PTR_ERR(idev->i2c_clk);
  350. }
  351. idev->dev = &pdev->dev;
  352. init_completion(&idev->msg_complete);
  353. mutex_init(&idev->isr_mutex);
  354. ret = device_property_read_u32(idev->dev, "fifo-size",
  355. &idev->fifo_size);
  356. if (ret) {
  357. dev_err(&pdev->dev, "FIFO size set to default of %d\n",
  358. ALTR_I2C_DFLT_FIFO_SZ);
  359. idev->fifo_size = ALTR_I2C_DFLT_FIFO_SZ;
  360. }
  361. ret = device_property_read_u32(idev->dev, "clock-frequency",
  362. &idev->bus_clk_rate);
  363. if (ret) {
  364. dev_err(&pdev->dev, "Default to 100kHz\n");
  365. idev->bus_clk_rate = I2C_MAX_STANDARD_MODE_FREQ; /* default clock rate */
  366. }
  367. if (idev->bus_clk_rate > I2C_MAX_FAST_MODE_FREQ) {
  368. dev_err(&pdev->dev, "invalid clock-frequency %d\n",
  369. idev->bus_clk_rate);
  370. return -EINVAL;
  371. }
  372. ret = devm_request_threaded_irq(&pdev->dev, irq, altr_i2c_isr_quick,
  373. altr_i2c_isr, IRQF_ONESHOT,
  374. pdev->name, idev);
  375. if (ret) {
  376. dev_err(&pdev->dev, "failed to claim IRQ %d\n", irq);
  377. return ret;
  378. }
  379. ret = clk_prepare_enable(idev->i2c_clk);
  380. if (ret) {
  381. dev_err(&pdev->dev, "failed to enable clock\n");
  382. return ret;
  383. }
  384. mutex_lock(&idev->isr_mutex);
  385. altr_i2c_init(idev);
  386. mutex_unlock(&idev->isr_mutex);
  387. i2c_set_adapdata(&idev->adapter, idev);
  388. strscpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
  389. idev->adapter.owner = THIS_MODULE;
  390. idev->adapter.algo = &altr_i2c_algo;
  391. idev->adapter.dev.parent = &pdev->dev;
  392. idev->adapter.dev.of_node = pdev->dev.of_node;
  393. platform_set_drvdata(pdev, idev);
  394. ret = i2c_add_adapter(&idev->adapter);
  395. if (ret) {
  396. clk_disable_unprepare(idev->i2c_clk);
  397. return ret;
  398. }
  399. dev_info(&pdev->dev, "Altera SoftIP I2C Probe Complete\n");
  400. return 0;
  401. }
  402. static int altr_i2c_remove(struct platform_device *pdev)
  403. {
  404. struct altr_i2c_dev *idev = platform_get_drvdata(pdev);
  405. clk_disable_unprepare(idev->i2c_clk);
  406. i2c_del_adapter(&idev->adapter);
  407. return 0;
  408. }
  409. /* Match table for of_platform binding */
  410. static const struct of_device_id altr_i2c_of_match[] = {
  411. { .compatible = "altr,softip-i2c-v1.0" },
  412. {},
  413. };
  414. MODULE_DEVICE_TABLE(of, altr_i2c_of_match);
  415. static struct platform_driver altr_i2c_driver = {
  416. .probe = altr_i2c_probe,
  417. .remove = altr_i2c_remove,
  418. .driver = {
  419. .name = "altera-i2c",
  420. .of_match_table = altr_i2c_of_match,
  421. },
  422. };
  423. module_platform_driver(altr_i2c_driver);
  424. MODULE_DESCRIPTION("Altera Soft IP I2C bus driver");
  425. MODULE_AUTHOR("Thor Thayer <[email protected]>");
  426. MODULE_LICENSE("GPL v2");