i2c-designware.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Synopsys DesignWare I2C adapter driver (master only).
  3. *
  4. * Based on the TI DAVINCI I2C adapter driver.
  5. *
  6. * Copyright (C) 2006 Texas Instruments.
  7. * Copyright (C) 2007 MontaVista Software Inc.
  8. * Copyright (C) 2009 Provigent Ltd.
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. * ----------------------------------------------------------------------------
  26. *
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <linux/i2c.h>
  32. #include <linux/clk.h>
  33. #include <linux/errno.h>
  34. #include <linux/sched.h>
  35. #include <linux/err.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/io.h>
  39. #include <linux/slab.h>
  40. #include <linux/swab.h>
  41. /*
  42. * Registers offset
  43. */
  44. #define DW_IC_CON 0x0
  45. #define DW_IC_TAR 0x4
  46. #define DW_IC_DATA_CMD 0x10
  47. #define DW_IC_SS_SCL_HCNT 0x14
  48. #define DW_IC_SS_SCL_LCNT 0x18
  49. #define DW_IC_FS_SCL_HCNT 0x1c
  50. #define DW_IC_FS_SCL_LCNT 0x20
  51. #define DW_IC_INTR_STAT 0x2c
  52. #define DW_IC_INTR_MASK 0x30
  53. #define DW_IC_RAW_INTR_STAT 0x34
  54. #define DW_IC_RX_TL 0x38
  55. #define DW_IC_TX_TL 0x3c
  56. #define DW_IC_CLR_INTR 0x40
  57. #define DW_IC_CLR_RX_UNDER 0x44
  58. #define DW_IC_CLR_RX_OVER 0x48
  59. #define DW_IC_CLR_TX_OVER 0x4c
  60. #define DW_IC_CLR_RD_REQ 0x50
  61. #define DW_IC_CLR_TX_ABRT 0x54
  62. #define DW_IC_CLR_RX_DONE 0x58
  63. #define DW_IC_CLR_ACTIVITY 0x5c
  64. #define DW_IC_CLR_STOP_DET 0x60
  65. #define DW_IC_CLR_START_DET 0x64
  66. #define DW_IC_CLR_GEN_CALL 0x68
  67. #define DW_IC_ENABLE 0x6c
  68. #define DW_IC_STATUS 0x70
  69. #define DW_IC_TXFLR 0x74
  70. #define DW_IC_RXFLR 0x78
  71. #define DW_IC_COMP_PARAM_1 0xf4
  72. #define DW_IC_COMP_TYPE 0xfc
  73. #define DW_IC_TX_ABRT_SOURCE 0x80
  74. #define DW_IC_CON_MASTER 0x1
  75. #define DW_IC_CON_SPEED_STD 0x2
  76. #define DW_IC_CON_SPEED_FAST 0x4
  77. #define DW_IC_CON_10BITADDR_MASTER 0x10
  78. #define DW_IC_CON_RESTART_EN 0x20
  79. #define DW_IC_CON_SLAVE_DISABLE 0x40
  80. #define DW_IC_INTR_RX_UNDER 0x001
  81. #define DW_IC_INTR_RX_OVER 0x002
  82. #define DW_IC_INTR_RX_FULL 0x004
  83. #define DW_IC_INTR_TX_OVER 0x008
  84. #define DW_IC_INTR_TX_EMPTY 0x010
  85. #define DW_IC_INTR_RD_REQ 0x020
  86. #define DW_IC_INTR_TX_ABRT 0x040
  87. #define DW_IC_INTR_RX_DONE 0x080
  88. #define DW_IC_INTR_ACTIVITY 0x100
  89. #define DW_IC_INTR_STOP_DET 0x200
  90. #define DW_IC_INTR_START_DET 0x400
  91. #define DW_IC_INTR_GEN_CALL 0x800
  92. #define DW_IC_INTR_DEFAULT_MASK (DW_IC_INTR_RX_FULL | \
  93. DW_IC_INTR_TX_EMPTY | \
  94. DW_IC_INTR_TX_ABRT | \
  95. DW_IC_INTR_STOP_DET)
  96. #define DW_IC_STATUS_ACTIVITY 0x1
  97. #define DW_IC_ERR_TX_ABRT 0x1
  98. /*
  99. * status codes
  100. */
  101. #define STATUS_IDLE 0x0
  102. #define STATUS_WRITE_IN_PROGRESS 0x1
  103. #define STATUS_READ_IN_PROGRESS 0x2
  104. #define TIMEOUT 20 /* ms */
  105. /*
  106. * hardware abort codes from the DW_IC_TX_ABRT_SOURCE register
  107. *
  108. * only expected abort codes are listed here
  109. * refer to the datasheet for the full list
  110. */
  111. #define ABRT_7B_ADDR_NOACK 0
  112. #define ABRT_10ADDR1_NOACK 1
  113. #define ABRT_10ADDR2_NOACK 2
  114. #define ABRT_TXDATA_NOACK 3
  115. #define ABRT_GCALL_NOACK 4
  116. #define ABRT_GCALL_READ 5
  117. #define ABRT_SBYTE_ACKDET 7
  118. #define ABRT_SBYTE_NORSTRT 9
  119. #define ABRT_10B_RD_NORSTRT 10
  120. #define ABRT_MASTER_DIS 11
  121. #define ARB_LOST 12
  122. #define DW_IC_TX_ABRT_7B_ADDR_NOACK (1UL << ABRT_7B_ADDR_NOACK)
  123. #define DW_IC_TX_ABRT_10ADDR1_NOACK (1UL << ABRT_10ADDR1_NOACK)
  124. #define DW_IC_TX_ABRT_10ADDR2_NOACK (1UL << ABRT_10ADDR2_NOACK)
  125. #define DW_IC_TX_ABRT_TXDATA_NOACK (1UL << ABRT_TXDATA_NOACK)
  126. #define DW_IC_TX_ABRT_GCALL_NOACK (1UL << ABRT_GCALL_NOACK)
  127. #define DW_IC_TX_ABRT_GCALL_READ (1UL << ABRT_GCALL_READ)
  128. #define DW_IC_TX_ABRT_SBYTE_ACKDET (1UL << ABRT_SBYTE_ACKDET)
  129. #define DW_IC_TX_ABRT_SBYTE_NORSTRT (1UL << ABRT_SBYTE_NORSTRT)
  130. #define DW_IC_TX_ABRT_10B_RD_NORSTRT (1UL << ABRT_10B_RD_NORSTRT)
  131. #define DW_IC_TX_ABRT_MASTER_DIS (1UL << ABRT_MASTER_DIS)
  132. #define DW_IC_TX_ARB_LOST (1UL << ARB_LOST)
  133. #define DW_IC_TX_ABRT_NOACK (DW_IC_TX_ABRT_7B_ADDR_NOACK | \
  134. DW_IC_TX_ABRT_10ADDR1_NOACK | \
  135. DW_IC_TX_ABRT_10ADDR2_NOACK | \
  136. DW_IC_TX_ABRT_TXDATA_NOACK | \
  137. DW_IC_TX_ABRT_GCALL_NOACK)
  138. static char *abort_sources[] = {
  139. [ABRT_7B_ADDR_NOACK] =
  140. "slave address not acknowledged (7bit mode)",
  141. [ABRT_10ADDR1_NOACK] =
  142. "first address byte not acknowledged (10bit mode)",
  143. [ABRT_10ADDR2_NOACK] =
  144. "second address byte not acknowledged (10bit mode)",
  145. [ABRT_TXDATA_NOACK] =
  146. "data not acknowledged",
  147. [ABRT_GCALL_NOACK] =
  148. "no acknowledgement for a general call",
  149. [ABRT_GCALL_READ] =
  150. "read after general call",
  151. [ABRT_SBYTE_ACKDET] =
  152. "start byte acknowledged",
  153. [ABRT_SBYTE_NORSTRT] =
  154. "trying to send start byte when restart is disabled",
  155. [ABRT_10B_RD_NORSTRT] =
  156. "trying to read when restart is disabled (10bit mode)",
  157. [ABRT_MASTER_DIS] =
  158. "trying to use disabled adapter",
  159. [ARB_LOST] =
  160. "lost arbitration",
  161. };
  162. /**
  163. * struct dw_i2c_dev - private i2c-designware data
  164. * @dev: driver model device node
  165. * @base: IO registers pointer
  166. * @cmd_complete: tx completion indicator
  167. * @lock: protect this struct and IO registers
  168. * @clk: input reference clock
  169. * @cmd_err: run time hadware error code
  170. * @msgs: points to an array of messages currently being transferred
  171. * @msgs_num: the number of elements in msgs
  172. * @msg_write_idx: the element index of the current tx message in the msgs
  173. * array
  174. * @tx_buf_len: the length of the current tx buffer
  175. * @tx_buf: the current tx buffer
  176. * @msg_read_idx: the element index of the current rx message in the msgs
  177. * array
  178. * @rx_buf_len: the length of the current rx buffer
  179. * @rx_buf: the current rx buffer
  180. * @msg_err: error status of the current transfer
  181. * @status: i2c master status, one of STATUS_*
  182. * @abort_source: copy of the TX_ABRT_SOURCE register
  183. * @irq: interrupt number for the i2c master
  184. * @swab: true if the instantiated IP is of different endianess
  185. * @adapter: i2c subsystem adapter node
  186. * @tx_fifo_depth: depth of the hardware tx fifo
  187. * @rx_fifo_depth: depth of the hardware rx fifo
  188. */
  189. struct dw_i2c_dev {
  190. struct device *dev;
  191. void __iomem *base;
  192. struct completion cmd_complete;
  193. struct mutex lock;
  194. struct clk *clk;
  195. int cmd_err;
  196. struct i2c_msg *msgs;
  197. int msgs_num;
  198. int msg_write_idx;
  199. u32 tx_buf_len;
  200. u8 *tx_buf;
  201. int msg_read_idx;
  202. u32 rx_buf_len;
  203. u8 *rx_buf;
  204. int msg_err;
  205. unsigned int status;
  206. u32 abort_source;
  207. int irq;
  208. int swab;
  209. struct i2c_adapter adapter;
  210. unsigned int tx_fifo_depth;
  211. unsigned int rx_fifo_depth;
  212. };
  213. static u32 dw_readl(struct dw_i2c_dev *dev, int offset)
  214. {
  215. u32 value = readl(dev->base + offset);
  216. if (dev->swab)
  217. return swab32(value);
  218. else
  219. return value;
  220. }
  221. static void dw_writel(struct dw_i2c_dev *dev, u32 b, int offset)
  222. {
  223. if (dev->swab)
  224. b = swab32(b);
  225. writel(b, dev->base + offset);
  226. }
  227. static u32
  228. i2c_dw_scl_hcnt(u32 ic_clk, u32 tSYMBOL, u32 tf, int cond, int offset)
  229. {
  230. /*
  231. * DesignWare I2C core doesn't seem to have solid strategy to meet
  232. * the tHD;STA timing spec. Configuring _HCNT based on tHIGH spec
  233. * will result in violation of the tHD;STA spec.
  234. */
  235. if (cond)
  236. /*
  237. * Conditional expression:
  238. *
  239. * IC_[FS]S_SCL_HCNT + (1+4+3) >= IC_CLK * tHIGH
  240. *
  241. * This is based on the DW manuals, and represents an ideal
  242. * configuration. The resulting I2C bus speed will be
  243. * faster than any of the others.
  244. *
  245. * If your hardware is free from tHD;STA issue, try this one.
  246. */
  247. return (ic_clk * tSYMBOL + 5000) / 10000 - 8 + offset;
  248. else
  249. /*
  250. * Conditional expression:
  251. *
  252. * IC_[FS]S_SCL_HCNT + 3 >= IC_CLK * (tHD;STA + tf)
  253. *
  254. * This is just experimental rule; the tHD;STA period turned
  255. * out to be proportinal to (_HCNT + 3). With this setting,
  256. * we could meet both tHIGH and tHD;STA timing specs.
  257. *
  258. * If unsure, you'd better to take this alternative.
  259. *
  260. * The reason why we need to take into account "tf" here,
  261. * is the same as described in i2c_dw_scl_lcnt().
  262. */
  263. return (ic_clk * (tSYMBOL + tf) + 5000) / 10000 - 3 + offset;
  264. }
  265. static u32 i2c_dw_scl_lcnt(u32 ic_clk, u32 tLOW, u32 tf, int offset)
  266. {
  267. /*
  268. * Conditional expression:
  269. *
  270. * IC_[FS]S_SCL_LCNT + 1 >= IC_CLK * (tLOW + tf)
  271. *
  272. * DW I2C core starts counting the SCL CNTs for the LOW period
  273. * of the SCL clock (tLOW) as soon as it pulls the SCL line.
  274. * In order to meet the tLOW timing spec, we need to take into
  275. * account the fall time of SCL signal (tf). Default tf value
  276. * should be 0.3 us, for safety.
  277. */
  278. return ((ic_clk * (tLOW + tf) + 5000) / 10000) - 1 + offset;
  279. }
  280. /**
  281. * i2c_dw_init() - initialize the designware i2c master hardware
  282. * @dev: device private data
  283. *
  284. * This functions configures and enables the I2C master.
  285. * This function is called during I2C init function, and in case of timeout at
  286. * run time.
  287. */
  288. static int i2c_dw_init(struct dw_i2c_dev *dev)
  289. {
  290. u32 input_clock_khz = clk_get_rate(dev->clk) / 1000;
  291. u32 ic_con, hcnt, lcnt;
  292. u32 reg;
  293. /* Configure register endianess access */
  294. reg = dw_readl(dev, DW_IC_COMP_TYPE);
  295. if (reg == ___constant_swab32(DW_IC_COMP_TYPE_VALUE)) {
  296. dev->swab = 1;
  297. reg = DW_IC_COMP_TYPE_VALUE;
  298. }
  299. if (reg != DW_IC_COMP_TYPE_VALUE) {
  300. dev_err(dev->dev, "Unknown Synopsys component type: "
  301. "0x%08x\n", reg);
  302. return -ENODEV;
  303. }
  304. /* Disable the adapter */
  305. dw_writel(dev, 0, DW_IC_ENABLE);
  306. /* set standard and fast speed deviders for high/low periods */
  307. /* Standard-mode */
  308. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  309. 40, /* tHD;STA = tHIGH = 4.0 us */
  310. 3, /* tf = 0.3 us */
  311. 0, /* 0: DW default, 1: Ideal */
  312. 0); /* No offset */
  313. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  314. 47, /* tLOW = 4.7 us */
  315. 3, /* tf = 0.3 us */
  316. 0); /* No offset */
  317. dw_writel(dev, hcnt, DW_IC_SS_SCL_HCNT);
  318. dw_writel(dev, lcnt, DW_IC_SS_SCL_LCNT);
  319. dev_dbg(dev->dev, "Standard-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  320. /* Fast-mode */
  321. hcnt = i2c_dw_scl_hcnt(input_clock_khz,
  322. 6, /* tHD;STA = tHIGH = 0.6 us */
  323. 3, /* tf = 0.3 us */
  324. 0, /* 0: DW default, 1: Ideal */
  325. 0); /* No offset */
  326. lcnt = i2c_dw_scl_lcnt(input_clock_khz,
  327. 13, /* tLOW = 1.3 us */
  328. 3, /* tf = 0.3 us */
  329. 0); /* No offset */
  330. dw_writel(dev, hcnt, DW_IC_FS_SCL_HCNT);
  331. dw_writel(dev, lcnt, DW_IC_FS_SCL_LCNT);
  332. dev_dbg(dev->dev, "Fast-mode HCNT:LCNT = %d:%d\n", hcnt, lcnt);
  333. /* Configure Tx/Rx FIFO threshold levels */
  334. dw_writel(dev, dev->tx_fifo_depth - 1, DW_IC_TX_TL);
  335. dw_writel(dev, 0, DW_IC_RX_TL);
  336. /* configure the i2c master */
  337. ic_con = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
  338. DW_IC_CON_RESTART_EN | DW_IC_CON_SPEED_FAST;
  339. dw_writel(dev, ic_con, DW_IC_CON);
  340. return 0;
  341. }
  342. /*
  343. * Waiting for bus not busy
  344. */
  345. static int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)
  346. {
  347. int timeout = TIMEOUT;
  348. while (dw_readl(dev, DW_IC_STATUS) & DW_IC_STATUS_ACTIVITY) {
  349. if (timeout <= 0) {
  350. dev_warn(dev->dev, "timeout waiting for bus ready\n");
  351. return -ETIMEDOUT;
  352. }
  353. timeout--;
  354. mdelay(1);
  355. }
  356. return 0;
  357. }
  358. static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
  359. {
  360. struct i2c_msg *msgs = dev->msgs;
  361. u32 ic_con;
  362. /* Disable the adapter */
  363. dw_writel(dev, 0, DW_IC_ENABLE);
  364. /* set the slave (target) address */
  365. dw_writel(dev, msgs[dev->msg_write_idx].addr, DW_IC_TAR);
  366. /* if the slave address is ten bit address, enable 10BITADDR */
  367. ic_con = dw_readl(dev, DW_IC_CON);
  368. if (msgs[dev->msg_write_idx].flags & I2C_M_TEN)
  369. ic_con |= DW_IC_CON_10BITADDR_MASTER;
  370. else
  371. ic_con &= ~DW_IC_CON_10BITADDR_MASTER;
  372. dw_writel(dev, ic_con, DW_IC_CON);
  373. /* Enable the adapter */
  374. dw_writel(dev, 1, DW_IC_ENABLE);
  375. /* Enable interrupts */
  376. dw_writel(dev, DW_IC_INTR_DEFAULT_MASK, DW_IC_INTR_MASK);
  377. }
  378. /*
  379. * Initiate (and continue) low level master read/write transaction.
  380. * This function is only called from i2c_dw_isr, and pumping i2c_msg
  381. * messages into the tx buffer. Even if the size of i2c_msg data is
  382. * longer than the size of the tx buffer, it handles everything.
  383. */
  384. static void
  385. i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
  386. {
  387. struct i2c_msg *msgs = dev->msgs;
  388. u32 intr_mask;
  389. int tx_limit, rx_limit;
  390. u32 addr = msgs[dev->msg_write_idx].addr;
  391. u32 buf_len = dev->tx_buf_len;
  392. u8 *buf = dev->tx_buf;
  393. intr_mask = DW_IC_INTR_DEFAULT_MASK;
  394. for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
  395. /*
  396. * if target address has changed, we need to
  397. * reprogram the target address in the i2c
  398. * adapter when we are done with this transfer
  399. */
  400. if (msgs[dev->msg_write_idx].addr != addr) {
  401. dev_err(dev->dev,
  402. "%s: invalid target address\n", __func__);
  403. dev->msg_err = -EINVAL;
  404. break;
  405. }
  406. if (msgs[dev->msg_write_idx].len == 0) {
  407. dev_err(dev->dev,
  408. "%s: invalid message length\n", __func__);
  409. dev->msg_err = -EINVAL;
  410. break;
  411. }
  412. if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
  413. /* new i2c_msg */
  414. buf = msgs[dev->msg_write_idx].buf;
  415. buf_len = msgs[dev->msg_write_idx].len;
  416. }
  417. tx_limit = dev->tx_fifo_depth - dw_readl(dev, DW_IC_TXFLR);
  418. rx_limit = dev->rx_fifo_depth - dw_readl(dev, DW_IC_RXFLR);
  419. while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
  420. if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
  421. dw_writel(dev, 0x100, DW_IC_DATA_CMD);
  422. rx_limit--;
  423. } else
  424. dw_writel(dev, *buf++, DW_IC_DATA_CMD);
  425. tx_limit--; buf_len--;
  426. }
  427. dev->tx_buf = buf;
  428. dev->tx_buf_len = buf_len;
  429. if (buf_len > 0) {
  430. /* more bytes to be written */
  431. dev->status |= STATUS_WRITE_IN_PROGRESS;
  432. break;
  433. } else
  434. dev->status &= ~STATUS_WRITE_IN_PROGRESS;
  435. }
  436. /*
  437. * If i2c_msg index search is completed, we don't need TX_EMPTY
  438. * interrupt any more.
  439. */
  440. if (dev->msg_write_idx == dev->msgs_num)
  441. intr_mask &= ~DW_IC_INTR_TX_EMPTY;
  442. if (dev->msg_err)
  443. intr_mask = 0;
  444. dw_writel(dev, intr_mask, DW_IC_INTR_MASK);
  445. }
  446. static void
  447. i2c_dw_read(struct dw_i2c_dev *dev)
  448. {
  449. struct i2c_msg *msgs = dev->msgs;
  450. int rx_valid;
  451. for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
  452. u32 len;
  453. u8 *buf;
  454. if (!(msgs[dev->msg_read_idx].flags & I2C_M_RD))
  455. continue;
  456. if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
  457. len = msgs[dev->msg_read_idx].len;
  458. buf = msgs[dev->msg_read_idx].buf;
  459. } else {
  460. len = dev->rx_buf_len;
  461. buf = dev->rx_buf;
  462. }
  463. rx_valid = dw_readl(dev, DW_IC_RXFLR);
  464. for (; len > 0 && rx_valid > 0; len--, rx_valid--)
  465. *buf++ = dw_readl(dev, DW_IC_DATA_CMD);
  466. if (len > 0) {
  467. dev->status |= STATUS_READ_IN_PROGRESS;
  468. dev->rx_buf_len = len;
  469. dev->rx_buf = buf;
  470. return;
  471. } else
  472. dev->status &= ~STATUS_READ_IN_PROGRESS;
  473. }
  474. }
  475. static int i2c_dw_handle_tx_abort(struct dw_i2c_dev *dev)
  476. {
  477. unsigned long abort_source = dev->abort_source;
  478. int i;
  479. if (abort_source & DW_IC_TX_ABRT_NOACK) {
  480. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  481. dev_dbg(dev->dev,
  482. "%s: %s\n", __func__, abort_sources[i]);
  483. return -EREMOTEIO;
  484. }
  485. for_each_set_bit(i, &abort_source, ARRAY_SIZE(abort_sources))
  486. dev_err(dev->dev, "%s: %s\n", __func__, abort_sources[i]);
  487. if (abort_source & DW_IC_TX_ARB_LOST)
  488. return -EAGAIN;
  489. else if (abort_source & DW_IC_TX_ABRT_GCALL_READ)
  490. return -EINVAL; /* wrong msgs[] data */
  491. else
  492. return -EIO;
  493. }
  494. /*
  495. * Prepare controller for a transaction and call i2c_dw_xfer_msg
  496. */
  497. static int
  498. i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  499. {
  500. struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
  501. int ret;
  502. dev_dbg(dev->dev, "%s: msgs: %d\n", __func__, num);
  503. mutex_lock(&dev->lock);
  504. INIT_COMPLETION(dev->cmd_complete);
  505. dev->msgs = msgs;
  506. dev->msgs_num = num;
  507. dev->cmd_err = 0;
  508. dev->msg_write_idx = 0;
  509. dev->msg_read_idx = 0;
  510. dev->msg_err = 0;
  511. dev->status = STATUS_IDLE;
  512. dev->abort_source = 0;
  513. ret = i2c_dw_wait_bus_not_busy(dev);
  514. if (ret < 0)
  515. goto done;
  516. /* start the transfers */
  517. i2c_dw_xfer_init(dev);
  518. /* wait for tx to complete */
  519. ret = wait_for_completion_interruptible_timeout(&dev->cmd_complete, HZ);
  520. if (ret == 0) {
  521. dev_err(dev->dev, "controller timed out\n");
  522. i2c_dw_init(dev);
  523. ret = -ETIMEDOUT;
  524. goto done;
  525. } else if (ret < 0)
  526. goto done;
  527. if (dev->msg_err) {
  528. ret = dev->msg_err;
  529. goto done;
  530. }
  531. /* no error */
  532. if (likely(!dev->cmd_err)) {
  533. /* Disable the adapter */
  534. dw_writel(dev, 0, DW_IC_ENABLE);
  535. ret = num;
  536. goto done;
  537. }
  538. /* We have an error */
  539. if (dev->cmd_err == DW_IC_ERR_TX_ABRT) {
  540. ret = i2c_dw_handle_tx_abort(dev);
  541. goto done;
  542. }
  543. ret = -EIO;
  544. done:
  545. mutex_unlock(&dev->lock);
  546. return ret;
  547. }
  548. static u32 i2c_dw_func(struct i2c_adapter *adap)
  549. {
  550. return I2C_FUNC_I2C |
  551. I2C_FUNC_10BIT_ADDR |
  552. I2C_FUNC_SMBUS_BYTE |
  553. I2C_FUNC_SMBUS_BYTE_DATA |
  554. I2C_FUNC_SMBUS_WORD_DATA |
  555. I2C_FUNC_SMBUS_I2C_BLOCK;
  556. }
  557. static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
  558. {
  559. u32 stat;
  560. /*
  561. * The IC_INTR_STAT register just indicates "enabled" interrupts.
  562. * Ths unmasked raw version of interrupt status bits are available
  563. * in the IC_RAW_INTR_STAT register.
  564. *
  565. * That is,
  566. * stat = readl(IC_INTR_STAT);
  567. * equals to,
  568. * stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
  569. *
  570. * The raw version might be useful for debugging purposes.
  571. */
  572. stat = dw_readl(dev, DW_IC_INTR_STAT);
  573. /*
  574. * Do not use the IC_CLR_INTR register to clear interrupts, or
  575. * you'll miss some interrupts, triggered during the period from
  576. * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
  577. *
  578. * Instead, use the separately-prepared IC_CLR_* registers.
  579. */
  580. if (stat & DW_IC_INTR_RX_UNDER)
  581. dw_readl(dev, DW_IC_CLR_RX_UNDER);
  582. if (stat & DW_IC_INTR_RX_OVER)
  583. dw_readl(dev, DW_IC_CLR_RX_OVER);
  584. if (stat & DW_IC_INTR_TX_OVER)
  585. dw_readl(dev, DW_IC_CLR_TX_OVER);
  586. if (stat & DW_IC_INTR_RD_REQ)
  587. dw_readl(dev, DW_IC_CLR_RD_REQ);
  588. if (stat & DW_IC_INTR_TX_ABRT) {
  589. /*
  590. * The IC_TX_ABRT_SOURCE register is cleared whenever
  591. * the IC_CLR_TX_ABRT is read. Preserve it beforehand.
  592. */
  593. dev->abort_source = dw_readl(dev, DW_IC_TX_ABRT_SOURCE);
  594. dw_readl(dev, DW_IC_CLR_TX_ABRT);
  595. }
  596. if (stat & DW_IC_INTR_RX_DONE)
  597. dw_readl(dev, DW_IC_CLR_RX_DONE);
  598. if (stat & DW_IC_INTR_ACTIVITY)
  599. dw_readl(dev, DW_IC_CLR_ACTIVITY);
  600. if (stat & DW_IC_INTR_STOP_DET)
  601. dw_readl(dev, DW_IC_CLR_STOP_DET);
  602. if (stat & DW_IC_INTR_START_DET)
  603. dw_readl(dev, DW_IC_CLR_START_DET);
  604. if (stat & DW_IC_INTR_GEN_CALL)
  605. dw_readl(dev, DW_IC_CLR_GEN_CALL);
  606. return stat;
  607. }
  608. /*
  609. * Interrupt service routine. This gets called whenever an I2C interrupt
  610. * occurs.
  611. */
  612. static irqreturn_t i2c_dw_isr(int this_irq, void *dev_id)
  613. {
  614. struct dw_i2c_dev *dev = dev_id;
  615. u32 stat;
  616. stat = i2c_dw_read_clear_intrbits(dev);
  617. dev_dbg(dev->dev, "%s: stat=0x%x\n", __func__, stat);
  618. if (stat & DW_IC_INTR_TX_ABRT) {
  619. dev->cmd_err |= DW_IC_ERR_TX_ABRT;
  620. dev->status = STATUS_IDLE;
  621. /*
  622. * Anytime TX_ABRT is set, the contents of the tx/rx
  623. * buffers are flushed. Make sure to skip them.
  624. */
  625. dw_writel(dev, 0, DW_IC_INTR_MASK);
  626. goto tx_aborted;
  627. }
  628. if (stat & DW_IC_INTR_RX_FULL)
  629. i2c_dw_read(dev);
  630. if (stat & DW_IC_INTR_TX_EMPTY)
  631. i2c_dw_xfer_msg(dev);
  632. /*
  633. * No need to modify or disable the interrupt mask here.
  634. * i2c_dw_xfer_msg() will take care of it according to
  635. * the current transmit status.
  636. */
  637. tx_aborted:
  638. if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
  639. complete(&dev->cmd_complete);
  640. return IRQ_HANDLED;
  641. }
  642. static struct i2c_algorithm i2c_dw_algo = {
  643. .master_xfer = i2c_dw_xfer,
  644. .functionality = i2c_dw_func,
  645. };
  646. static int __devinit dw_i2c_probe(struct platform_device *pdev)
  647. {
  648. struct dw_i2c_dev *dev;
  649. struct i2c_adapter *adap;
  650. struct resource *mem, *ioarea;
  651. int irq, r;
  652. u32 reg;
  653. /* NOTE: driver uses the static register mapping */
  654. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  655. if (!mem) {
  656. dev_err(&pdev->dev, "no mem resource?\n");
  657. return -EINVAL;
  658. }
  659. irq = platform_get_irq(pdev, 0);
  660. if (irq < 0) {
  661. dev_err(&pdev->dev, "no irq resource?\n");
  662. return irq; /* -ENXIO */
  663. }
  664. ioarea = request_mem_region(mem->start, resource_size(mem),
  665. pdev->name);
  666. if (!ioarea) {
  667. dev_err(&pdev->dev, "I2C region already claimed\n");
  668. return -EBUSY;
  669. }
  670. dev = kzalloc(sizeof(struct dw_i2c_dev), GFP_KERNEL);
  671. if (!dev) {
  672. r = -ENOMEM;
  673. goto err_release_region;
  674. }
  675. init_completion(&dev->cmd_complete);
  676. mutex_init(&dev->lock);
  677. dev->dev = get_device(&pdev->dev);
  678. dev->irq = irq;
  679. platform_set_drvdata(pdev, dev);
  680. dev->clk = clk_get(&pdev->dev, NULL);
  681. if (IS_ERR(dev->clk)) {
  682. r = -ENODEV;
  683. goto err_free_mem;
  684. }
  685. clk_enable(dev->clk);
  686. dev->base = ioremap(mem->start, resource_size(mem));
  687. if (dev->base == NULL) {
  688. dev_err(&pdev->dev, "failure mapping io resources\n");
  689. r = -EBUSY;
  690. goto err_unuse_clocks;
  691. }
  692. r = i2c_dw_init(dev);
  693. if (r)
  694. goto err_unuse_clocks;
  695. reg = dw_readl(dev, DW_IC_COMP_PARAM_1);
  696. dev->tx_fifo_depth = ((reg >> 16) & 0xff) + 1;
  697. dev->rx_fifo_depth = ((reg >> 8) & 0xff) + 1;
  698. dw_writel(dev, 0, DW_IC_INTR_MASK); /* disable IRQ */
  699. r = request_irq(dev->irq, i2c_dw_isr, IRQF_DISABLED, pdev->name, dev);
  700. if (r) {
  701. dev_err(&pdev->dev, "failure requesting irq %i\n", dev->irq);
  702. goto err_iounmap;
  703. }
  704. adap = &dev->adapter;
  705. i2c_set_adapdata(adap, dev);
  706. adap->owner = THIS_MODULE;
  707. adap->class = I2C_CLASS_HWMON;
  708. strlcpy(adap->name, "Synopsys DesignWare I2C adapter",
  709. sizeof(adap->name));
  710. adap->algo = &i2c_dw_algo;
  711. adap->dev.parent = &pdev->dev;
  712. adap->nr = pdev->id;
  713. r = i2c_add_numbered_adapter(adap);
  714. if (r) {
  715. dev_err(&pdev->dev, "failure adding adapter\n");
  716. goto err_free_irq;
  717. }
  718. return 0;
  719. err_free_irq:
  720. free_irq(dev->irq, dev);
  721. err_iounmap:
  722. iounmap(dev->base);
  723. err_unuse_clocks:
  724. clk_disable(dev->clk);
  725. clk_put(dev->clk);
  726. dev->clk = NULL;
  727. err_free_mem:
  728. platform_set_drvdata(pdev, NULL);
  729. put_device(&pdev->dev);
  730. kfree(dev);
  731. err_release_region:
  732. release_mem_region(mem->start, resource_size(mem));
  733. return r;
  734. }
  735. static int __devexit dw_i2c_remove(struct platform_device *pdev)
  736. {
  737. struct dw_i2c_dev *dev = platform_get_drvdata(pdev);
  738. struct resource *mem;
  739. platform_set_drvdata(pdev, NULL);
  740. i2c_del_adapter(&dev->adapter);
  741. put_device(&pdev->dev);
  742. clk_disable(dev->clk);
  743. clk_put(dev->clk);
  744. dev->clk = NULL;
  745. dw_writel(dev, 0, DW_IC_ENABLE);
  746. free_irq(dev->irq, dev);
  747. kfree(dev);
  748. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  749. release_mem_region(mem->start, resource_size(mem));
  750. return 0;
  751. }
  752. /* work with hotplug and coldplug */
  753. MODULE_ALIAS("platform:i2c_designware");
  754. static struct platform_driver dw_i2c_driver = {
  755. .remove = __devexit_p(dw_i2c_remove),
  756. .driver = {
  757. .name = "i2c_designware",
  758. .owner = THIS_MODULE,
  759. },
  760. };
  761. static int __init dw_i2c_init_driver(void)
  762. {
  763. return platform_driver_probe(&dw_i2c_driver, dw_i2c_probe);
  764. }
  765. module_init(dw_i2c_init_driver);
  766. static void __exit dw_i2c_exit_driver(void)
  767. {
  768. platform_driver_unregister(&dw_i2c_driver);
  769. }
  770. module_exit(dw_i2c_exit_driver);
  771. MODULE_AUTHOR("Baruch Siach <baruch@tkos.co.il>");
  772. MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter");
  773. MODULE_LICENSE("GPL");