PageRenderTime 445ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/i2c/busses/i2c-nomadik.c

https://bitbucket.org/abioy/linux
C | 960 lines | 576 code | 137 blank | 247 comment | 55 complexity | 203c125b8f021734c24331389126f355 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Copyright (C) 2009 ST-Ericsson
  3. * Copyright (C) 2009 STMicroelectronics
  4. *
  5. * I2C master mode controller driver, used in Nomadik 8815
  6. * and Ux500 platforms.
  7. *
  8. * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
  9. * Author: Sachin Verma <sachin.verma@st.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2, as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/i2c.h>
  22. #include <linux/err.h>
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <plat/i2c.h>
  26. #define DRIVER_NAME "nmk-i2c"
  27. /* I2C Controller register offsets */
  28. #define I2C_CR (0x000)
  29. #define I2C_SCR (0x004)
  30. #define I2C_HSMCR (0x008)
  31. #define I2C_MCR (0x00C)
  32. #define I2C_TFR (0x010)
  33. #define I2C_SR (0x014)
  34. #define I2C_RFR (0x018)
  35. #define I2C_TFTR (0x01C)
  36. #define I2C_RFTR (0x020)
  37. #define I2C_DMAR (0x024)
  38. #define I2C_BRCR (0x028)
  39. #define I2C_IMSCR (0x02C)
  40. #define I2C_RISR (0x030)
  41. #define I2C_MISR (0x034)
  42. #define I2C_ICR (0x038)
  43. /* Control registers */
  44. #define I2C_CR_PE (0x1 << 0) /* Peripheral Enable */
  45. #define I2C_CR_OM (0x3 << 1) /* Operating mode */
  46. #define I2C_CR_SAM (0x1 << 3) /* Slave addressing mode */
  47. #define I2C_CR_SM (0x3 << 4) /* Speed mode */
  48. #define I2C_CR_SGCM (0x1 << 6) /* Slave general call mode */
  49. #define I2C_CR_FTX (0x1 << 7) /* Flush Transmit */
  50. #define I2C_CR_FRX (0x1 << 8) /* Flush Receive */
  51. #define I2C_CR_DMA_TX_EN (0x1 << 9) /* DMA Tx enable */
  52. #define I2C_CR_DMA_RX_EN (0x1 << 10) /* DMA Rx Enable */
  53. #define I2C_CR_DMA_SLE (0x1 << 11) /* DMA sync. logic enable */
  54. #define I2C_CR_LM (0x1 << 12) /* Loopback mode */
  55. #define I2C_CR_FON (0x3 << 13) /* Filtering on */
  56. #define I2C_CR_FS (0x3 << 15) /* Force stop enable */
  57. /* Master controller (MCR) register */
  58. #define I2C_MCR_OP (0x1 << 0) /* Operation */
  59. #define I2C_MCR_A7 (0x7f << 1) /* 7-bit address */
  60. #define I2C_MCR_EA10 (0x7 << 8) /* 10-bit Extended address */
  61. #define I2C_MCR_SB (0x1 << 11) /* Extended address */
  62. #define I2C_MCR_AM (0x3 << 12) /* Address type */
  63. #define I2C_MCR_STOP (0x1 << 14) /* Stop condition */
  64. #define I2C_MCR_LENGTH (0x7ff << 15) /* Transaction length */
  65. /* Status register (SR) */
  66. #define I2C_SR_OP (0x3 << 0) /* Operation */
  67. #define I2C_SR_STATUS (0x3 << 2) /* controller status */
  68. #define I2C_SR_CAUSE (0x7 << 4) /* Abort cause */
  69. #define I2C_SR_TYPE (0x3 << 7) /* Receive type */
  70. #define I2C_SR_LENGTH (0x7ff << 9) /* Transfer length */
  71. /* Interrupt mask set/clear (IMSCR) bits */
  72. #define I2C_IT_TXFE (0x1 << 0)
  73. #define I2C_IT_TXFNE (0x1 << 1)
  74. #define I2C_IT_TXFF (0x1 << 2)
  75. #define I2C_IT_TXFOVR (0x1 << 3)
  76. #define I2C_IT_RXFE (0x1 << 4)
  77. #define I2C_IT_RXFNF (0x1 << 5)
  78. #define I2C_IT_RXFF (0x1 << 6)
  79. #define I2C_IT_RFSR (0x1 << 16)
  80. #define I2C_IT_RFSE (0x1 << 17)
  81. #define I2C_IT_WTSR (0x1 << 18)
  82. #define I2C_IT_MTD (0x1 << 19)
  83. #define I2C_IT_STD (0x1 << 20)
  84. #define I2C_IT_MAL (0x1 << 24)
  85. #define I2C_IT_BERR (0x1 << 25)
  86. #define I2C_IT_MTDWS (0x1 << 28)
  87. #define GEN_MASK(val, mask, sb) (((val) << (sb)) & (mask))
  88. /* some bits in ICR are reserved */
  89. #define I2C_CLEAR_ALL_INTS 0x131f007f
  90. /* first three msb bits are reserved */
  91. #define IRQ_MASK(mask) (mask & 0x1fffffff)
  92. /* maximum threshold value */
  93. #define MAX_I2C_FIFO_THRESHOLD 15
  94. enum i2c_status {
  95. I2C_NOP,
  96. I2C_ON_GOING,
  97. I2C_OK,
  98. I2C_ABORT
  99. };
  100. /* operation */
  101. enum i2c_operation {
  102. I2C_NO_OPERATION = 0xff,
  103. I2C_WRITE = 0x00,
  104. I2C_READ = 0x01
  105. };
  106. /* controller response timeout in ms */
  107. #define I2C_TIMEOUT_MS 500
  108. /**
  109. * struct i2c_nmk_client - client specific data
  110. * @slave_adr: 7-bit slave address
  111. * @count: no. bytes to be transfered
  112. * @buffer: client data buffer
  113. * @xfer_bytes: bytes transfered till now
  114. * @operation: current I2C operation
  115. */
  116. struct i2c_nmk_client {
  117. unsigned short slave_adr;
  118. unsigned long count;
  119. unsigned char *buffer;
  120. unsigned long xfer_bytes;
  121. enum i2c_operation operation;
  122. };
  123. /**
  124. * struct nmk_i2c_dev - private data structure of the controller
  125. * @pdev: parent platform device
  126. * @adap: corresponding I2C adapter
  127. * @irq: interrupt line for the controller
  128. * @virtbase: virtual io memory area
  129. * @clk: hardware i2c block clock
  130. * @cfg: machine provided controller configuration
  131. * @cli: holder of client specific data
  132. * @stop: stop condition
  133. * @xfer_complete: acknowledge completion for a I2C message
  134. * @result: controller propogated result
  135. */
  136. struct nmk_i2c_dev {
  137. struct platform_device *pdev;
  138. struct i2c_adapter adap;
  139. int irq;
  140. void __iomem *virtbase;
  141. struct clk *clk;
  142. struct nmk_i2c_controller cfg;
  143. struct i2c_nmk_client cli;
  144. int stop;
  145. struct completion xfer_complete;
  146. int result;
  147. };
  148. /* controller's abort causes */
  149. static const char *abort_causes[] = {
  150. "no ack received after address transmission",
  151. "no ack received during data phase",
  152. "ack received after xmission of master code",
  153. "master lost arbitration",
  154. "slave restarts",
  155. "slave reset",
  156. "overflow, maxsize is 2047 bytes",
  157. };
  158. static inline void i2c_set_bit(void __iomem *reg, u32 mask)
  159. {
  160. writel(readl(reg) | mask, reg);
  161. }
  162. static inline void i2c_clr_bit(void __iomem *reg, u32 mask)
  163. {
  164. writel(readl(reg) & ~mask, reg);
  165. }
  166. /**
  167. * flush_i2c_fifo() - This function flushes the I2C FIFO
  168. * @dev: private data of I2C Driver
  169. *
  170. * This function flushes the I2C Tx and Rx FIFOs. It returns
  171. * 0 on successful flushing of FIFO
  172. */
  173. static int flush_i2c_fifo(struct nmk_i2c_dev *dev)
  174. {
  175. #define LOOP_ATTEMPTS 10
  176. int i;
  177. unsigned long timeout;
  178. /*
  179. * flush the transmit and receive FIFO. The flushing
  180. * operation takes several cycles before to be completed.
  181. * On the completion, the I2C internal logic clears these
  182. * bits, until then no one must access Tx, Rx FIFO and
  183. * should poll on these bits waiting for the completion.
  184. */
  185. writel((I2C_CR_FTX | I2C_CR_FRX), dev->virtbase + I2C_CR);
  186. for (i = 0; i < LOOP_ATTEMPTS; i++) {
  187. timeout = jiffies + msecs_to_jiffies(I2C_TIMEOUT_MS);
  188. while (!time_after(jiffies, timeout)) {
  189. if ((readl(dev->virtbase + I2C_CR) &
  190. (I2C_CR_FTX | I2C_CR_FRX)) == 0)
  191. return 0;
  192. }
  193. }
  194. dev_err(&dev->pdev->dev, "flushing operation timed out "
  195. "giving up after %d attempts", LOOP_ATTEMPTS);
  196. return -ETIMEDOUT;
  197. }
  198. /**
  199. * disable_all_interrupts() - Disable all interrupts of this I2c Bus
  200. * @dev: private data of I2C Driver
  201. */
  202. static void disable_all_interrupts(struct nmk_i2c_dev *dev)
  203. {
  204. u32 mask = IRQ_MASK(0);
  205. writel(mask, dev->virtbase + I2C_IMSCR);
  206. }
  207. /**
  208. * clear_all_interrupts() - Clear all interrupts of I2C Controller
  209. * @dev: private data of I2C Driver
  210. */
  211. static void clear_all_interrupts(struct nmk_i2c_dev *dev)
  212. {
  213. u32 mask;
  214. mask = IRQ_MASK(I2C_CLEAR_ALL_INTS);
  215. writel(mask, dev->virtbase + I2C_ICR);
  216. }
  217. /**
  218. * init_hw() - initialize the I2C hardware
  219. * @dev: private data of I2C Driver
  220. */
  221. static int init_hw(struct nmk_i2c_dev *dev)
  222. {
  223. int stat;
  224. stat = flush_i2c_fifo(dev);
  225. if (stat)
  226. return stat;
  227. /* disable the controller */
  228. i2c_clr_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  229. disable_all_interrupts(dev);
  230. clear_all_interrupts(dev);
  231. dev->cli.operation = I2C_NO_OPERATION;
  232. return 0;
  233. }
  234. /* enable peripheral, master mode operation */
  235. #define DEFAULT_I2C_REG_CR ((1 << 1) | I2C_CR_PE)
  236. /**
  237. * load_i2c_mcr_reg() - load the MCR register
  238. * @dev: private data of controller
  239. */
  240. static u32 load_i2c_mcr_reg(struct nmk_i2c_dev *dev)
  241. {
  242. u32 mcr = 0;
  243. /* 7-bit address transaction */
  244. mcr |= GEN_MASK(1, I2C_MCR_AM, 12);
  245. mcr |= GEN_MASK(dev->cli.slave_adr, I2C_MCR_A7, 1);
  246. /* start byte procedure not applied */
  247. mcr |= GEN_MASK(0, I2C_MCR_SB, 11);
  248. /* check the operation, master read/write? */
  249. if (dev->cli.operation == I2C_WRITE)
  250. mcr |= GEN_MASK(I2C_WRITE, I2C_MCR_OP, 0);
  251. else
  252. mcr |= GEN_MASK(I2C_READ, I2C_MCR_OP, 0);
  253. /* stop or repeated start? */
  254. if (dev->stop)
  255. mcr |= GEN_MASK(1, I2C_MCR_STOP, 14);
  256. else
  257. mcr &= ~(GEN_MASK(1, I2C_MCR_STOP, 14));
  258. mcr |= GEN_MASK(dev->cli.count, I2C_MCR_LENGTH, 15);
  259. return mcr;
  260. }
  261. /**
  262. * setup_i2c_controller() - setup the controller
  263. * @dev: private data of controller
  264. */
  265. static void setup_i2c_controller(struct nmk_i2c_dev *dev)
  266. {
  267. u32 brcr1, brcr2;
  268. u32 i2c_clk, div;
  269. writel(0x0, dev->virtbase + I2C_CR);
  270. writel(0x0, dev->virtbase + I2C_HSMCR);
  271. writel(0x0, dev->virtbase + I2C_TFTR);
  272. writel(0x0, dev->virtbase + I2C_RFTR);
  273. writel(0x0, dev->virtbase + I2C_DMAR);
  274. /*
  275. * set the slsu:
  276. *
  277. * slsu defines the data setup time after SCL clock
  278. * stretching in terms of i2c clk cycles. The
  279. * needed setup time for the three modes are 250ns,
  280. * 100ns, 10ns repectively thus leading to the values
  281. * of 14, 6, 2 for a 48 MHz i2c clk.
  282. */
  283. writel(dev->cfg.slsu << 16, dev->virtbase + I2C_SCR);
  284. i2c_clk = clk_get_rate(dev->clk);
  285. /* fallback to std. mode if machine has not provided it */
  286. if (dev->cfg.clk_freq == 0)
  287. dev->cfg.clk_freq = 100000;
  288. /*
  289. * The spec says, in case of std. mode the divider is
  290. * 2 whereas it is 3 for fast and fastplus mode of
  291. * operation. TODO - high speed support.
  292. */
  293. div = (dev->cfg.clk_freq > 100000) ? 3 : 2;
  294. /*
  295. * generate the mask for baud rate counters. The controller
  296. * has two baud rate counters. One is used for High speed
  297. * operation, and the other is for std, fast mode, fast mode
  298. * plus operation. Currently we do not supprt high speed mode
  299. * so set brcr1 to 0.
  300. */
  301. brcr1 = 0 << 16;
  302. brcr2 = (i2c_clk/(dev->cfg.clk_freq * div)) & 0xffff;
  303. /* set the baud rate counter register */
  304. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  305. /*
  306. * set the speed mode. Currently we support
  307. * only standard and fast mode of operation
  308. * TODO - support for fast mode plus (upto 1Mb/s)
  309. * and high speed (up to 3.4 Mb/s)
  310. */
  311. if (dev->cfg.sm > I2C_FREQ_MODE_FAST) {
  312. dev_err(&dev->pdev->dev, "do not support this mode "
  313. "defaulting to std. mode\n");
  314. brcr2 = i2c_clk/(100000 * 2) & 0xffff;
  315. writel((brcr1 | brcr2), dev->virtbase + I2C_BRCR);
  316. writel(I2C_FREQ_MODE_STANDARD << 4,
  317. dev->virtbase + I2C_CR);
  318. }
  319. writel(dev->cfg.sm << 4, dev->virtbase + I2C_CR);
  320. /* set the Tx and Rx FIFO threshold */
  321. writel(dev->cfg.tft, dev->virtbase + I2C_TFTR);
  322. writel(dev->cfg.rft, dev->virtbase + I2C_RFTR);
  323. }
  324. /**
  325. * read_i2c() - Read from I2C client device
  326. * @dev: private data of I2C Driver
  327. *
  328. * This function reads from i2c client device when controller is in
  329. * master mode. There is a completion timeout. If there is no transfer
  330. * before timeout error is returned.
  331. */
  332. static int read_i2c(struct nmk_i2c_dev *dev)
  333. {
  334. u32 status = 0;
  335. u32 mcr;
  336. u32 irq_mask = 0;
  337. int timeout;
  338. mcr = load_i2c_mcr_reg(dev);
  339. writel(mcr, dev->virtbase + I2C_MCR);
  340. /* load the current CR value */
  341. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  342. dev->virtbase + I2C_CR);
  343. /* enable the controller */
  344. i2c_set_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  345. init_completion(&dev->xfer_complete);
  346. /* enable interrupts by setting the mask */
  347. irq_mask = (I2C_IT_RXFNF | I2C_IT_RXFF |
  348. I2C_IT_MAL | I2C_IT_BERR);
  349. if (dev->stop)
  350. irq_mask |= I2C_IT_MTD;
  351. else
  352. irq_mask |= I2C_IT_MTDWS;
  353. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  354. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  355. dev->virtbase + I2C_IMSCR);
  356. timeout = wait_for_completion_interruptible_timeout(
  357. &dev->xfer_complete, msecs_to_jiffies(I2C_TIMEOUT_MS));
  358. if (timeout < 0) {
  359. dev_err(&dev->pdev->dev,
  360. "wait_for_completion_interruptible_timeout"
  361. "returned %d waiting for event\n", timeout);
  362. status = timeout;
  363. }
  364. if (timeout == 0) {
  365. /* controler has timedout, re-init the h/w */
  366. dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n");
  367. (void) init_hw(dev);
  368. status = -ETIMEDOUT;
  369. }
  370. return status;
  371. }
  372. /**
  373. * write_i2c() - Write data to I2C client.
  374. * @dev: private data of I2C Driver
  375. *
  376. * This function writes data to I2C client
  377. */
  378. static int write_i2c(struct nmk_i2c_dev *dev)
  379. {
  380. u32 status = 0;
  381. u32 mcr;
  382. u32 irq_mask = 0;
  383. int timeout;
  384. mcr = load_i2c_mcr_reg(dev);
  385. writel(mcr, dev->virtbase + I2C_MCR);
  386. /* load the current CR value */
  387. writel(readl(dev->virtbase + I2C_CR) | DEFAULT_I2C_REG_CR,
  388. dev->virtbase + I2C_CR);
  389. /* enable the controller */
  390. i2c_set_bit(dev->virtbase + I2C_CR , I2C_CR_PE);
  391. init_completion(&dev->xfer_complete);
  392. /* enable interrupts by settings the masks */
  393. irq_mask = (I2C_IT_TXFNE | I2C_IT_TXFOVR |
  394. I2C_IT_MAL | I2C_IT_BERR);
  395. /*
  396. * check if we want to transfer a single or multiple bytes, if so
  397. * set the MTDWS bit (Master Transaction Done Without Stop)
  398. * to start repeated start operation
  399. */
  400. if (dev->stop)
  401. irq_mask |= I2C_IT_MTD;
  402. else
  403. irq_mask |= I2C_IT_MTDWS;
  404. irq_mask = I2C_CLEAR_ALL_INTS & IRQ_MASK(irq_mask);
  405. writel(readl(dev->virtbase + I2C_IMSCR) | irq_mask,
  406. dev->virtbase + I2C_IMSCR);
  407. timeout = wait_for_completion_interruptible_timeout(
  408. &dev->xfer_complete, msecs_to_jiffies(I2C_TIMEOUT_MS));
  409. if (timeout < 0) {
  410. dev_err(&dev->pdev->dev,
  411. "wait_for_completion_interruptible_timeout"
  412. "returned %d waiting for event\n", timeout);
  413. status = timeout;
  414. }
  415. if (timeout == 0) {
  416. /* controler has timedout, re-init the h/w */
  417. dev_err(&dev->pdev->dev, "controller timed out, re-init h/w\n");
  418. (void) init_hw(dev);
  419. status = -ETIMEDOUT;
  420. }
  421. return status;
  422. }
  423. /**
  424. * nmk_i2c_xfer() - I2C transfer function used by kernel framework
  425. * @i2c_adap - Adapter pointer to the controller
  426. * @msgs[] - Pointer to data to be written.
  427. * @num_msgs - Number of messages to be executed
  428. *
  429. * This is the function called by the generic kernel i2c_transfer()
  430. * or i2c_smbus...() API calls. Note that this code is protected by the
  431. * semaphore set in the kernel i2c_transfer() function.
  432. *
  433. * NOTE:
  434. * READ TRANSFER : We impose a restriction of the first message to be the
  435. * index message for any read transaction.
  436. * - a no index is coded as '0',
  437. * - 2byte big endian index is coded as '3'
  438. * !!! msg[0].buf holds the actual index.
  439. * This is compatible with generic messages of smbus emulator
  440. * that send a one byte index.
  441. * eg. a I2C transation to read 2 bytes from index 0
  442. * idx = 0;
  443. * msg[0].addr = client->addr;
  444. * msg[0].flags = 0x0;
  445. * msg[0].len = 1;
  446. * msg[0].buf = &idx;
  447. *
  448. * msg[1].addr = client->addr;
  449. * msg[1].flags = I2C_M_RD;
  450. * msg[1].len = 2;
  451. * msg[1].buf = rd_buff
  452. * i2c_transfer(adap, msg, 2);
  453. *
  454. * WRITE TRANSFER : The I2C standard interface interprets all data as payload.
  455. * If you want to emulate an SMBUS write transaction put the
  456. * index as first byte(or first and second) in the payload.
  457. * eg. a I2C transation to write 2 bytes from index 1
  458. * wr_buff[0] = 0x1;
  459. * wr_buff[1] = 0x23;
  460. * wr_buff[2] = 0x46;
  461. * msg[0].flags = 0x0;
  462. * msg[0].len = 3;
  463. * msg[0].buf = wr_buff;
  464. * i2c_transfer(adap, msg, 1);
  465. *
  466. * To read or write a block of data (multiple bytes) using SMBUS emulation
  467. * please use the i2c_smbus_read_i2c_block_data()
  468. * or i2c_smbus_write_i2c_block_data() API
  469. */
  470. static int nmk_i2c_xfer(struct i2c_adapter *i2c_adap,
  471. struct i2c_msg msgs[], int num_msgs)
  472. {
  473. int status;
  474. int i;
  475. u32 cause;
  476. struct nmk_i2c_dev *dev = i2c_get_adapdata(i2c_adap);
  477. status = init_hw(dev);
  478. if (status)
  479. return status;
  480. /* setup the i2c controller */
  481. setup_i2c_controller(dev);
  482. for (i = 0; i < num_msgs; i++) {
  483. if (unlikely(msgs[i].flags & I2C_M_TEN)) {
  484. dev_err(&dev->pdev->dev, "10 bit addressing"
  485. "not supported\n");
  486. return -EINVAL;
  487. }
  488. dev->cli.slave_adr = msgs[i].addr;
  489. dev->cli.buffer = msgs[i].buf;
  490. dev->cli.count = msgs[i].len;
  491. dev->stop = (i < (num_msgs - 1)) ? 0 : 1;
  492. dev->result = 0;
  493. if (msgs[i].flags & I2C_M_RD) {
  494. /* it is a read operation */
  495. dev->cli.operation = I2C_READ;
  496. status = read_i2c(dev);
  497. } else {
  498. /* write operation */
  499. dev->cli.operation = I2C_WRITE;
  500. status = write_i2c(dev);
  501. }
  502. if (status || (dev->result)) {
  503. /* get the abort cause */
  504. cause = (readl(dev->virtbase + I2C_SR) >> 4) & 0x7;
  505. dev_err(&dev->pdev->dev, "error during I2C"
  506. "message xfer: %d\n", cause);
  507. dev_err(&dev->pdev->dev, "%s\n",
  508. cause >= ARRAY_SIZE(abort_causes)
  509. ? "unknown reason" : abort_causes[cause]);
  510. return status;
  511. }
  512. mdelay(1);
  513. }
  514. /* return the no. messages processed */
  515. if (status)
  516. return status;
  517. else
  518. return num_msgs;
  519. }
  520. /**
  521. * disable_interrupts() - disable the interrupts
  522. * @dev: private data of controller
  523. */
  524. static int disable_interrupts(struct nmk_i2c_dev *dev, u32 irq)
  525. {
  526. irq = IRQ_MASK(irq);
  527. writel(readl(dev->virtbase + I2C_IMSCR) & ~(I2C_CLEAR_ALL_INTS & irq),
  528. dev->virtbase + I2C_IMSCR);
  529. return 0;
  530. }
  531. /**
  532. * i2c_irq_handler() - interrupt routine
  533. * @irq: interrupt number
  534. * @arg: data passed to the handler
  535. *
  536. * This is the interrupt handler for the i2c driver. Currently
  537. * it handles the major interrupts like Rx & Tx FIFO management
  538. * interrupts, master transaction interrupts, arbitration and
  539. * bus error interrupts. The rest of the interrupts are treated as
  540. * unhandled.
  541. */
  542. static irqreturn_t i2c_irq_handler(int irq, void *arg)
  543. {
  544. struct nmk_i2c_dev *dev = arg;
  545. u32 tft, rft;
  546. u32 count;
  547. u32 misr;
  548. u32 src = 0;
  549. /* load Tx FIFO and Rx FIFO threshold values */
  550. tft = readl(dev->virtbase + I2C_TFTR);
  551. rft = readl(dev->virtbase + I2C_RFTR);
  552. /* read interrupt status register */
  553. misr = readl(dev->virtbase + I2C_MISR);
  554. src = __ffs(misr);
  555. switch ((1 << src)) {
  556. /* Transmit FIFO nearly empty interrupt */
  557. case I2C_IT_TXFNE:
  558. {
  559. if (dev->cli.operation == I2C_READ) {
  560. /*
  561. * in read operation why do we care for writing?
  562. * so disable the Transmit FIFO interrupt
  563. */
  564. disable_interrupts(dev, I2C_IT_TXFNE);
  565. } else {
  566. for (count = (MAX_I2C_FIFO_THRESHOLD - tft - 2);
  567. (count > 0) &&
  568. (dev->cli.count != 0);
  569. count--) {
  570. /* write to the Tx FIFO */
  571. writeb(*dev->cli.buffer,
  572. dev->virtbase + I2C_TFR);
  573. dev->cli.buffer++;
  574. dev->cli.count--;
  575. dev->cli.xfer_bytes++;
  576. }
  577. /*
  578. * if done, close the transfer by disabling the
  579. * corresponding TXFNE interrupt
  580. */
  581. if (dev->cli.count == 0)
  582. disable_interrupts(dev, I2C_IT_TXFNE);
  583. }
  584. }
  585. break;
  586. /*
  587. * Rx FIFO nearly full interrupt.
  588. * This is set when the numer of entries in Rx FIFO is
  589. * greater or equal than the threshold value programmed
  590. * in RFT
  591. */
  592. case I2C_IT_RXFNF:
  593. for (count = rft; count > 0; count--) {
  594. /* Read the Rx FIFO */
  595. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  596. dev->cli.buffer++;
  597. }
  598. dev->cli.count -= rft;
  599. dev->cli.xfer_bytes += rft;
  600. break;
  601. /* Rx FIFO full */
  602. case I2C_IT_RXFF:
  603. for (count = MAX_I2C_FIFO_THRESHOLD; count > 0; count--) {
  604. *dev->cli.buffer = readb(dev->virtbase + I2C_RFR);
  605. dev->cli.buffer++;
  606. }
  607. dev->cli.count -= MAX_I2C_FIFO_THRESHOLD;
  608. dev->cli.xfer_bytes += MAX_I2C_FIFO_THRESHOLD;
  609. break;
  610. /* Master Transaction Done with/without stop */
  611. case I2C_IT_MTD:
  612. case I2C_IT_MTDWS:
  613. if (dev->cli.operation == I2C_READ) {
  614. while (!readl(dev->virtbase + I2C_RISR) & I2C_IT_RXFE) {
  615. if (dev->cli.count == 0)
  616. break;
  617. *dev->cli.buffer =
  618. readb(dev->virtbase + I2C_RFR);
  619. dev->cli.buffer++;
  620. dev->cli.count--;
  621. dev->cli.xfer_bytes++;
  622. }
  623. }
  624. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTD);
  625. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MTDWS);
  626. disable_interrupts(dev,
  627. (I2C_IT_TXFNE | I2C_IT_TXFE | I2C_IT_TXFF
  628. | I2C_IT_TXFOVR | I2C_IT_RXFNF
  629. | I2C_IT_RXFF | I2C_IT_RXFE));
  630. if (dev->cli.count) {
  631. dev->result = -1;
  632. dev_err(&dev->pdev->dev, "%lu bytes still remain to be"
  633. "xfered\n", dev->cli.count);
  634. (void) init_hw(dev);
  635. }
  636. complete(&dev->xfer_complete);
  637. break;
  638. /* Master Arbitration lost interrupt */
  639. case I2C_IT_MAL:
  640. dev->result = -1;
  641. (void) init_hw(dev);
  642. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_MAL);
  643. complete(&dev->xfer_complete);
  644. break;
  645. /*
  646. * Bus Error interrupt.
  647. * This happens when an unexpected start/stop condition occurs
  648. * during the transaction.
  649. */
  650. case I2C_IT_BERR:
  651. dev->result = -1;
  652. /* get the status */
  653. if (((readl(dev->virtbase + I2C_SR) >> 2) & 0x3) == I2C_ABORT)
  654. (void) init_hw(dev);
  655. i2c_set_bit(dev->virtbase + I2C_ICR, I2C_IT_BERR);
  656. complete(&dev->xfer_complete);
  657. break;
  658. /*
  659. * Tx FIFO overrun interrupt.
  660. * This is set when a write operation in Tx FIFO is performed and
  661. * the Tx FIFO is full.
  662. */
  663. case I2C_IT_TXFOVR:
  664. dev->result = -1;
  665. (void) init_hw(dev);
  666. dev_err(&dev->pdev->dev, "Tx Fifo Over run\n");
  667. complete(&dev->xfer_complete);
  668. break;
  669. /* unhandled interrupts by this driver - TODO*/
  670. case I2C_IT_TXFE:
  671. case I2C_IT_TXFF:
  672. case I2C_IT_RXFE:
  673. case I2C_IT_RFSR:
  674. case I2C_IT_RFSE:
  675. case I2C_IT_WTSR:
  676. case I2C_IT_STD:
  677. dev_err(&dev->pdev->dev, "unhandled Interrupt\n");
  678. break;
  679. default:
  680. dev_err(&dev->pdev->dev, "spurious Interrupt..\n");
  681. break;
  682. }
  683. return IRQ_HANDLED;
  684. }
  685. static unsigned int nmk_i2c_functionality(struct i2c_adapter *adap)
  686. {
  687. return I2C_FUNC_I2C
  688. | I2C_FUNC_SMBUS_BYTE_DATA
  689. | I2C_FUNC_SMBUS_WORD_DATA
  690. | I2C_FUNC_SMBUS_I2C_BLOCK;
  691. }
  692. static const struct i2c_algorithm nmk_i2c_algo = {
  693. .master_xfer = nmk_i2c_xfer,
  694. .functionality = nmk_i2c_functionality
  695. };
  696. static int __devinit nmk_i2c_probe(struct platform_device *pdev)
  697. {
  698. int ret = 0;
  699. struct resource *res;
  700. struct nmk_i2c_controller *pdata =
  701. pdev->dev.platform_data;
  702. struct nmk_i2c_dev *dev;
  703. struct i2c_adapter *adap;
  704. dev = kzalloc(sizeof(struct nmk_i2c_dev), GFP_KERNEL);
  705. if (!dev) {
  706. dev_err(&pdev->dev, "cannot allocate memory\n");
  707. ret = -ENOMEM;
  708. goto err_no_mem;
  709. }
  710. dev->pdev = pdev;
  711. platform_set_drvdata(pdev, dev);
  712. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  713. if (!res) {
  714. ret = -ENOENT;
  715. goto err_no_resource;
  716. }
  717. if (request_mem_region(res->start, resource_size(res),
  718. DRIVER_NAME "I/O region") == NULL) {
  719. ret = -EBUSY;
  720. goto err_no_region;
  721. }
  722. dev->virtbase = ioremap(res->start, resource_size(res));
  723. if (!dev->virtbase) {
  724. ret = -ENOMEM;
  725. goto err_no_ioremap;
  726. }
  727. dev->irq = platform_get_irq(pdev, 0);
  728. ret = request_irq(dev->irq, i2c_irq_handler, IRQF_DISABLED,
  729. DRIVER_NAME, dev);
  730. if (ret) {
  731. dev_err(&pdev->dev, "cannot claim the irq %d\n", dev->irq);
  732. goto err_irq;
  733. }
  734. dev->clk = clk_get(&pdev->dev, NULL);
  735. if (IS_ERR(dev->clk)) {
  736. dev_err(&pdev->dev, "could not get i2c clock\n");
  737. ret = PTR_ERR(dev->clk);
  738. goto err_no_clk;
  739. }
  740. clk_enable(dev->clk);
  741. adap = &dev->adap;
  742. adap->dev.parent = &pdev->dev;
  743. adap->owner = THIS_MODULE;
  744. adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  745. adap->algo = &nmk_i2c_algo;
  746. /* fetch the controller id */
  747. adap->nr = pdev->id;
  748. /* fetch the controller configuration from machine */
  749. dev->cfg.clk_freq = pdata->clk_freq;
  750. dev->cfg.slsu = pdata->slsu;
  751. dev->cfg.tft = pdata->tft;
  752. dev->cfg.rft = pdata->rft;
  753. dev->cfg.sm = pdata->sm;
  754. i2c_set_adapdata(adap, dev);
  755. ret = init_hw(dev);
  756. if (ret != 0) {
  757. dev_err(&pdev->dev, "error in initializing i2c hardware\n");
  758. goto err_init_hw;
  759. }
  760. dev_dbg(&pdev->dev, "initialize I2C%d bus on virtual "
  761. "base %p\n", pdev->id, dev->virtbase);
  762. ret = i2c_add_numbered_adapter(adap);
  763. if (ret) {
  764. dev_err(&pdev->dev, "failed to add adapter\n");
  765. goto err_add_adap;
  766. }
  767. return 0;
  768. err_init_hw:
  769. clk_disable(dev->clk);
  770. err_add_adap:
  771. clk_put(dev->clk);
  772. err_no_clk:
  773. free_irq(dev->irq, dev);
  774. err_irq:
  775. iounmap(dev->virtbase);
  776. err_no_ioremap:
  777. release_mem_region(res->start, resource_size(res));
  778. err_no_region:
  779. platform_set_drvdata(pdev, NULL);
  780. err_no_resource:
  781. kfree(dev);
  782. err_no_mem:
  783. return ret;
  784. }
  785. static int __devexit nmk_i2c_remove(struct platform_device *pdev)
  786. {
  787. struct nmk_i2c_dev *dev = platform_get_drvdata(pdev);
  788. i2c_del_adapter(&dev->adap);
  789. flush_i2c_fifo(dev);
  790. disable_all_interrupts(dev);
  791. clear_all_interrupts(dev);
  792. /* disable the controller */
  793. i2c_clr_bit(dev->virtbase + I2C_CR, I2C_CR_PE);
  794. free_irq(dev->irq, dev);
  795. iounmap(dev->virtbase);
  796. clk_disable(dev->clk);
  797. clk_put(dev->clk);
  798. platform_set_drvdata(pdev, NULL);
  799. kfree(dev);
  800. return 0;
  801. }
  802. static struct platform_driver nmk_i2c_driver = {
  803. .driver = {
  804. .owner = THIS_MODULE,
  805. .name = DRIVER_NAME,
  806. },
  807. .probe = nmk_i2c_probe,
  808. .remove = __devexit_p(nmk_i2c_remove),
  809. };
  810. static int __init nmk_i2c_init(void)
  811. {
  812. return platform_driver_register(&nmk_i2c_driver);
  813. }
  814. static void __exit nmk_i2c_exit(void)
  815. {
  816. platform_driver_unregister(&nmk_i2c_driver);
  817. }
  818. subsys_initcall(nmk_i2c_init);
  819. module_exit(nmk_i2c_exit);
  820. MODULE_AUTHOR("Sachin Verma, Srinidhi KASAGAR");
  821. MODULE_DESCRIPTION("Nomadik/Ux500 I2C driver");
  822. MODULE_LICENSE("GPL");
  823. MODULE_ALIAS("platform:" DRIVER_NAME);