i2c-altera.c 14 KB

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