i2c-altera.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. * @isr_mutex: mutex for IRQ thread.
  81. */
  82. struct altr_i2c_dev {
  83. void __iomem *base;
  84. struct i2c_msg *msg;
  85. size_t msg_len;
  86. int msg_err;
  87. struct completion msg_complete;
  88. struct device *dev;
  89. struct i2c_adapter adapter;
  90. struct clk *i2c_clk;
  91. u32 bus_clk_rate;
  92. u8 *buf;
  93. u32 fifo_size;
  94. u32 isr_mask;
  95. u32 isr_status;
  96. spinlock_t lock; /* IRQ synchronization */
  97. struct mutex isr_mutex;
  98. };
  99. static void
  100. altr_i2c_int_enable(struct altr_i2c_dev *idev, u32 mask, bool enable)
  101. {
  102. unsigned long flags;
  103. u32 int_en;
  104. spin_lock_irqsave(&idev->lock, flags);
  105. int_en = readl(idev->base + ALTR_I2C_ISER);
  106. if (enable)
  107. idev->isr_mask = int_en | mask;
  108. else
  109. idev->isr_mask = int_en & ~mask;
  110. writel(idev->isr_mask, idev->base + ALTR_I2C_ISER);
  111. spin_unlock_irqrestore(&idev->lock, flags);
  112. }
  113. static void altr_i2c_int_clear(struct altr_i2c_dev *idev, u32 mask)
  114. {
  115. u32 int_en = readl(idev->base + ALTR_I2C_ISR);
  116. writel(int_en | mask, idev->base + ALTR_I2C_ISR);
  117. }
  118. static void altr_i2c_core_disable(struct altr_i2c_dev *idev)
  119. {
  120. u32 tmp = readl(idev->base + ALTR_I2C_CTRL);
  121. writel(tmp & ~ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL);
  122. }
  123. static void altr_i2c_core_enable(struct altr_i2c_dev *idev)
  124. {
  125. u32 tmp = readl(idev->base + ALTR_I2C_CTRL);
  126. writel(tmp | ALTR_I2C_CTRL_EN, idev->base + ALTR_I2C_CTRL);
  127. }
  128. static void altr_i2c_reset(struct altr_i2c_dev *idev)
  129. {
  130. altr_i2c_core_disable(idev);
  131. altr_i2c_core_enable(idev);
  132. }
  133. static inline void altr_i2c_stop(struct altr_i2c_dev *idev)
  134. {
  135. writel(ALTR_I2C_TFR_CMD_STO, idev->base + ALTR_I2C_TFR_CMD);
  136. }
  137. static void altr_i2c_init(struct altr_i2c_dev *idev)
  138. {
  139. u32 divisor = clk_get_rate(idev->i2c_clk) / idev->bus_clk_rate;
  140. u32 clk_mhz = clk_get_rate(idev->i2c_clk) / 1000000;
  141. u32 tmp = (ALTR_I2C_THRESHOLD << ALTR_I2C_CTRL_RXT_SHFT) |
  142. (ALTR_I2C_THRESHOLD << ALTR_I2C_CTRL_TCT_SHFT);
  143. u32 t_high, t_low;
  144. if (idev->bus_clk_rate <= 100000) {
  145. tmp &= ~ALTR_I2C_CTRL_BSPEED;
  146. /* Standard mode SCL 50/50 */
  147. t_high = divisor * 1 / 2;
  148. t_low = divisor * 1 / 2;
  149. } else {
  150. tmp |= ALTR_I2C_CTRL_BSPEED;
  151. /* Fast mode SCL 33/66 */
  152. t_high = divisor * 1 / 3;
  153. t_low = divisor * 2 / 3;
  154. }
  155. writel(tmp, idev->base + ALTR_I2C_CTRL);
  156. dev_dbg(idev->dev, "rate=%uHz per_clk=%uMHz -> ratio=1:%u\n",
  157. idev->bus_clk_rate, clk_mhz, divisor);
  158. /* Reset controller */
  159. altr_i2c_reset(idev);
  160. /* SCL High Time */
  161. writel(t_high, idev->base + ALTR_I2C_SCL_HIGH);
  162. /* SCL Low Time */
  163. writel(t_low, idev->base + ALTR_I2C_SCL_LOW);
  164. /* SDA Hold Time, 300ns */
  165. writel(3 * clk_mhz / 10, idev->base + ALTR_I2C_SDA_HOLD);
  166. /* Mask all master interrupt bits */
  167. altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
  168. }
  169. /**
  170. * altr_i2c_transfer - On the last byte to be transmitted, send
  171. * a Stop bit on the last byte.
  172. */
  173. static void altr_i2c_transfer(struct altr_i2c_dev *idev, u32 data)
  174. {
  175. /* On the last byte to be transmitted, send STOP */
  176. if (idev->msg_len == 1)
  177. data |= ALTR_I2C_TFR_CMD_STO;
  178. if (idev->msg_len > 0)
  179. writel(data, idev->base + ALTR_I2C_TFR_CMD);
  180. }
  181. /**
  182. * altr_i2c_empty_rx_fifo - Fetch data from RX FIFO until end of
  183. * transfer. Send a Stop bit on the last byte.
  184. */
  185. static void altr_i2c_empty_rx_fifo(struct altr_i2c_dev *idev)
  186. {
  187. size_t rx_fifo_avail = readl(idev->base + ALTR_I2C_RX_FIFO_LVL);
  188. int bytes_to_transfer = min(rx_fifo_avail, idev->msg_len);
  189. while (bytes_to_transfer-- > 0) {
  190. *idev->buf++ = readl(idev->base + ALTR_I2C_RX_DATA);
  191. idev->msg_len--;
  192. altr_i2c_transfer(idev, 0);
  193. }
  194. }
  195. /**
  196. * altr_i2c_fill_tx_fifo - Fill TX FIFO from current message buffer.
  197. * @return: Number of bytes left to transfer.
  198. */
  199. static int altr_i2c_fill_tx_fifo(struct altr_i2c_dev *idev)
  200. {
  201. size_t tx_fifo_avail = idev->fifo_size - readl(idev->base +
  202. ALTR_I2C_TC_FIFO_LVL);
  203. int bytes_to_transfer = min(tx_fifo_avail, idev->msg_len);
  204. int ret = idev->msg_len - bytes_to_transfer;
  205. while (bytes_to_transfer-- > 0) {
  206. altr_i2c_transfer(idev, *idev->buf++);
  207. idev->msg_len--;
  208. }
  209. return ret;
  210. }
  211. static irqreturn_t altr_i2c_isr_quick(int irq, void *_dev)
  212. {
  213. struct altr_i2c_dev *idev = _dev;
  214. irqreturn_t ret = IRQ_HANDLED;
  215. /* Read IRQ status but only interested in Enabled IRQs. */
  216. idev->isr_status = readl(idev->base + ALTR_I2C_ISR) & idev->isr_mask;
  217. if (idev->isr_status)
  218. ret = IRQ_WAKE_THREAD;
  219. return ret;
  220. }
  221. static irqreturn_t altr_i2c_isr(int irq, void *_dev)
  222. {
  223. int ret;
  224. bool read, finish = false;
  225. struct altr_i2c_dev *idev = _dev;
  226. u32 status = idev->isr_status;
  227. mutex_lock(&idev->isr_mutex);
  228. if (!idev->msg) {
  229. dev_warn(idev->dev, "unexpected interrupt\n");
  230. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  231. goto out;
  232. }
  233. read = (idev->msg->flags & I2C_M_RD) != 0;
  234. /* handle Lost Arbitration */
  235. if (unlikely(status & ALTR_I2C_ISR_ARB)) {
  236. altr_i2c_int_clear(idev, ALTR_I2C_ISR_ARB);
  237. idev->msg_err = -EAGAIN;
  238. finish = true;
  239. } else if (unlikely(status & ALTR_I2C_ISR_NACK)) {
  240. dev_dbg(idev->dev, "Could not get ACK\n");
  241. idev->msg_err = -ENXIO;
  242. altr_i2c_int_clear(idev, ALTR_I2C_ISR_NACK);
  243. altr_i2c_stop(idev);
  244. finish = true;
  245. } else if (read && unlikely(status & ALTR_I2C_ISR_RXOF)) {
  246. /* handle RX FIFO Overflow */
  247. altr_i2c_empty_rx_fifo(idev);
  248. altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY);
  249. altr_i2c_stop(idev);
  250. dev_err(idev->dev, "RX FIFO Overflow\n");
  251. finish = true;
  252. } else if (read && (status & ALTR_I2C_ISR_RXRDY)) {
  253. /* RX FIFO needs service? */
  254. altr_i2c_empty_rx_fifo(idev);
  255. altr_i2c_int_clear(idev, ALTR_I2C_ISR_RXRDY);
  256. if (!idev->msg_len)
  257. finish = true;
  258. } else if (!read && (status & ALTR_I2C_ISR_TXRDY)) {
  259. /* TX FIFO needs service? */
  260. altr_i2c_int_clear(idev, ALTR_I2C_ISR_TXRDY);
  261. if (idev->msg_len > 0)
  262. altr_i2c_fill_tx_fifo(idev);
  263. else
  264. finish = true;
  265. } else {
  266. dev_warn(idev->dev, "Unexpected interrupt: 0x%x\n", status);
  267. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  268. }
  269. if (finish) {
  270. /* Wait for the Core to finish */
  271. ret = readl_poll_timeout_atomic(idev->base + ALTR_I2C_STATUS,
  272. status,
  273. !(status & ALTR_I2C_STAT_CORE),
  274. 1, ALTR_I2C_TIMEOUT);
  275. if (ret)
  276. dev_err(idev->dev, "message timeout\n");
  277. altr_i2c_int_enable(idev, ALTR_I2C_ALL_IRQ, false);
  278. altr_i2c_int_clear(idev, ALTR_I2C_ALL_IRQ);
  279. complete(&idev->msg_complete);
  280. dev_dbg(idev->dev, "Message Complete\n");
  281. }
  282. out:
  283. mutex_unlock(&idev->isr_mutex);
  284. return IRQ_HANDLED;
  285. }
  286. static int altr_i2c_xfer_msg(struct altr_i2c_dev *idev, struct i2c_msg *msg)
  287. {
  288. u32 imask = ALTR_I2C_ISR_RXOF | ALTR_I2C_ISR_ARB | ALTR_I2C_ISR_NACK;
  289. unsigned long time_left;
  290. u32 value;
  291. u8 addr = i2c_8bit_addr_from_msg(msg);
  292. mutex_lock(&idev->isr_mutex);
  293. idev->msg = msg;
  294. idev->msg_len = msg->len;
  295. idev->buf = msg->buf;
  296. idev->msg_err = 0;
  297. reinit_completion(&idev->msg_complete);
  298. altr_i2c_core_enable(idev);
  299. /* Make sure RX FIFO is empty */
  300. do {
  301. readl(idev->base + ALTR_I2C_RX_DATA);
  302. } while (readl(idev->base + ALTR_I2C_RX_FIFO_LVL));
  303. writel(ALTR_I2C_TFR_CMD_STA | addr, idev->base + ALTR_I2C_TFR_CMD);
  304. if ((msg->flags & I2C_M_RD) != 0) {
  305. imask |= ALTR_I2C_ISER_RXOF_EN | ALTR_I2C_ISER_RXRDY_EN;
  306. altr_i2c_int_enable(idev, imask, true);
  307. /* write the first byte to start the RX */
  308. altr_i2c_transfer(idev, 0);
  309. } else {
  310. imask |= ALTR_I2C_ISR_TXRDY;
  311. altr_i2c_int_enable(idev, imask, true);
  312. altr_i2c_fill_tx_fifo(idev);
  313. }
  314. mutex_unlock(&idev->isr_mutex);
  315. time_left = wait_for_completion_timeout(&idev->msg_complete,
  316. ALTR_I2C_XFER_TIMEOUT);
  317. altr_i2c_int_enable(idev, imask, false);
  318. value = readl(idev->base + ALTR_I2C_STATUS) & ALTR_I2C_STAT_CORE;
  319. if (value)
  320. dev_err(idev->dev, "Core Status not IDLE...\n");
  321. if (time_left == 0) {
  322. idev->msg_err = -ETIMEDOUT;
  323. dev_dbg(idev->dev, "Transaction timed out.\n");
  324. }
  325. altr_i2c_core_disable(idev);
  326. return idev->msg_err;
  327. }
  328. static int
  329. altr_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  330. {
  331. struct altr_i2c_dev *idev = i2c_get_adapdata(adap);
  332. int i, ret;
  333. for (i = 0; i < num; i++) {
  334. ret = altr_i2c_xfer_msg(idev, msgs++);
  335. if (ret)
  336. return ret;
  337. }
  338. return num;
  339. }
  340. static u32 altr_i2c_func(struct i2c_adapter *adap)
  341. {
  342. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  343. }
  344. static const struct i2c_algorithm altr_i2c_algo = {
  345. .master_xfer = altr_i2c_xfer,
  346. .functionality = altr_i2c_func,
  347. };
  348. static int altr_i2c_probe(struct platform_device *pdev)
  349. {
  350. struct altr_i2c_dev *idev = NULL;
  351. struct resource *res;
  352. int irq, ret;
  353. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  354. if (!idev)
  355. return -ENOMEM;
  356. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  357. idev->base = devm_ioremap_resource(&pdev->dev, res);
  358. if (IS_ERR(idev->base))
  359. return PTR_ERR(idev->base);
  360. irq = platform_get_irq(pdev, 0);
  361. if (irq < 0) {
  362. dev_err(&pdev->dev, "missing interrupt resource\n");
  363. return irq;
  364. }
  365. idev->i2c_clk = devm_clk_get(&pdev->dev, NULL);
  366. if (IS_ERR(idev->i2c_clk)) {
  367. dev_err(&pdev->dev, "missing clock\n");
  368. return PTR_ERR(idev->i2c_clk);
  369. }
  370. idev->dev = &pdev->dev;
  371. init_completion(&idev->msg_complete);
  372. spin_lock_init(&idev->lock);
  373. mutex_init(&idev->isr_mutex);
  374. ret = device_property_read_u32(idev->dev, "fifo-size",
  375. &idev->fifo_size);
  376. if (ret) {
  377. dev_err(&pdev->dev, "FIFO size set to default of %d\n",
  378. ALTR_I2C_DFLT_FIFO_SZ);
  379. idev->fifo_size = ALTR_I2C_DFLT_FIFO_SZ;
  380. }
  381. ret = device_property_read_u32(idev->dev, "clock-frequency",
  382. &idev->bus_clk_rate);
  383. if (ret) {
  384. dev_err(&pdev->dev, "Default to 100kHz\n");
  385. idev->bus_clk_rate = 100000; /* default clock rate */
  386. }
  387. if (idev->bus_clk_rate > 400000) {
  388. dev_err(&pdev->dev, "invalid clock-frequency %d\n",
  389. idev->bus_clk_rate);
  390. return -EINVAL;
  391. }
  392. ret = devm_request_threaded_irq(&pdev->dev, irq, altr_i2c_isr_quick,
  393. altr_i2c_isr, IRQF_ONESHOT,
  394. pdev->name, idev);
  395. if (ret) {
  396. dev_err(&pdev->dev, "failed to claim IRQ %d\n", irq);
  397. return ret;
  398. }
  399. ret = clk_prepare_enable(idev->i2c_clk);
  400. if (ret) {
  401. dev_err(&pdev->dev, "failed to enable clock\n");
  402. return ret;
  403. }
  404. altr_i2c_init(idev);
  405. i2c_set_adapdata(&idev->adapter, idev);
  406. strlcpy(idev->adapter.name, pdev->name, sizeof(idev->adapter.name));
  407. idev->adapter.owner = THIS_MODULE;
  408. idev->adapter.algo = &altr_i2c_algo;
  409. idev->adapter.dev.parent = &pdev->dev;
  410. idev->adapter.dev.of_node = pdev->dev.of_node;
  411. platform_set_drvdata(pdev, idev);
  412. ret = i2c_add_adapter(&idev->adapter);
  413. if (ret) {
  414. clk_disable_unprepare(idev->i2c_clk);
  415. return ret;
  416. }
  417. dev_info(&pdev->dev, "Altera SoftIP I2C Probe Complete\n");
  418. return 0;
  419. }
  420. static int altr_i2c_remove(struct platform_device *pdev)
  421. {
  422. struct altr_i2c_dev *idev = platform_get_drvdata(pdev);
  423. clk_disable_unprepare(idev->i2c_clk);
  424. i2c_del_adapter(&idev->adapter);
  425. return 0;
  426. }
  427. /* Match table for of_platform binding */
  428. static const struct of_device_id altr_i2c_of_match[] = {
  429. { .compatible = "altr,softip-i2c-v1.0" },
  430. {},
  431. };
  432. MODULE_DEVICE_TABLE(of, altr_i2c_of_match);
  433. static struct platform_driver altr_i2c_driver = {
  434. .probe = altr_i2c_probe,
  435. .remove = altr_i2c_remove,
  436. .driver = {
  437. .name = "altera-i2c",
  438. .of_match_table = altr_i2c_of_match,
  439. },
  440. };
  441. module_platform_driver(altr_i2c_driver);
  442. MODULE_DESCRIPTION("Altera Soft IP I2C bus driver");
  443. MODULE_AUTHOR("Thor Thayer <thor.thayer@linux.intel.com>");
  444. MODULE_LICENSE("GPL v2");