i2c-pxa.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /*
  2. * i2c_adap_pxa.c
  3. *
  4. * I2C adapter for the PXA I2C bus access.
  5. *
  6. * Copyright (C) 2002 Intrinsyc Software Inc.
  7. * Copyright (C) 2004-2005 Deep Blue Solutions Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * History:
  14. * Apr 2002: Initial version [CS]
  15. * Jun 2002: Properly seperated algo/adap [FB]
  16. * Jan 2003: Fixed several bugs concerning interrupt handling [Kai-Uwe Bloem]
  17. * Jan 2003: added limited signal handling [Kai-Uwe Bloem]
  18. * Sep 2004: Major rework to ensure efficient bus handling [RMK]
  19. * Dec 2004: Added support for PXA27x and slave device probing [Liam Girdwood]
  20. * Feb 2005: Rework slave mode handling [RMK]
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/i2c.h>
  25. #include <linux/i2c-id.h>
  26. #include <linux/init.h>
  27. #include <linux/time.h>
  28. #include <linux/sched.h>
  29. #include <linux/delay.h>
  30. #include <linux/errno.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/i2c-pxa.h>
  33. #include <linux/platform_device.h>
  34. #include <asm/hardware.h>
  35. #include <asm/irq.h>
  36. #include <asm/io.h>
  37. #include <asm/arch/i2c.h>
  38. #include <asm/arch/pxa-regs.h>
  39. struct pxa_i2c {
  40. spinlock_t lock;
  41. wait_queue_head_t wait;
  42. struct i2c_msg *msg;
  43. unsigned int msg_num;
  44. unsigned int msg_idx;
  45. unsigned int msg_ptr;
  46. unsigned int slave_addr;
  47. struct i2c_adapter adap;
  48. #ifdef CONFIG_I2C_PXA_SLAVE
  49. struct i2c_slave_client *slave;
  50. #endif
  51. unsigned int irqlogidx;
  52. u32 isrlog[32];
  53. u32 icrlog[32];
  54. void __iomem *reg_base;
  55. unsigned long iobase;
  56. unsigned long iosize;
  57. int irq;
  58. };
  59. #define _IBMR(i2c) ((i2c)->reg_base + 0)
  60. #define _IDBR(i2c) ((i2c)->reg_base + 8)
  61. #define _ICR(i2c) ((i2c)->reg_base + 0x10)
  62. #define _ISR(i2c) ((i2c)->reg_base + 0x18)
  63. #define _ISAR(i2c) ((i2c)->reg_base + 0x20)
  64. /*
  65. * I2C Slave mode address
  66. */
  67. #define I2C_PXA_SLAVE_ADDR 0x1
  68. #ifdef DEBUG
  69. struct bits {
  70. u32 mask;
  71. const char *set;
  72. const char *unset;
  73. };
  74. #define BIT(m, s, u) { .mask = m, .set = s, .unset = u }
  75. static inline void
  76. decode_bits(const char *prefix, const struct bits *bits, int num, u32 val)
  77. {
  78. printk("%s %08x: ", prefix, val);
  79. while (num--) {
  80. const char *str = val & bits->mask ? bits->set : bits->unset;
  81. if (str)
  82. printk("%s ", str);
  83. bits++;
  84. }
  85. }
  86. static const struct bits isr_bits[] = {
  87. BIT(ISR_RWM, "RX", "TX"),
  88. BIT(ISR_ACKNAK, "NAK", "ACK"),
  89. BIT(ISR_UB, "Bsy", "Rdy"),
  90. BIT(ISR_IBB, "BusBsy", "BusRdy"),
  91. BIT(ISR_SSD, "SlaveStop", NULL),
  92. BIT(ISR_ALD, "ALD", NULL),
  93. BIT(ISR_ITE, "TxEmpty", NULL),
  94. BIT(ISR_IRF, "RxFull", NULL),
  95. BIT(ISR_GCAD, "GenCall", NULL),
  96. BIT(ISR_SAD, "SlaveAddr", NULL),
  97. BIT(ISR_BED, "BusErr", NULL),
  98. };
  99. static void decode_ISR(unsigned int val)
  100. {
  101. decode_bits(KERN_DEBUG "ISR", isr_bits, ARRAY_SIZE(isr_bits), val);
  102. printk("\n");
  103. }
  104. static const struct bits icr_bits[] = {
  105. BIT(ICR_START, "START", NULL),
  106. BIT(ICR_STOP, "STOP", NULL),
  107. BIT(ICR_ACKNAK, "ACKNAK", NULL),
  108. BIT(ICR_TB, "TB", NULL),
  109. BIT(ICR_MA, "MA", NULL),
  110. BIT(ICR_SCLE, "SCLE", "scle"),
  111. BIT(ICR_IUE, "IUE", "iue"),
  112. BIT(ICR_GCD, "GCD", NULL),
  113. BIT(ICR_ITEIE, "ITEIE", NULL),
  114. BIT(ICR_IRFIE, "IRFIE", NULL),
  115. BIT(ICR_BEIE, "BEIE", NULL),
  116. BIT(ICR_SSDIE, "SSDIE", NULL),
  117. BIT(ICR_ALDIE, "ALDIE", NULL),
  118. BIT(ICR_SADIE, "SADIE", NULL),
  119. BIT(ICR_UR, "UR", "ur"),
  120. };
  121. static void decode_ICR(unsigned int val)
  122. {
  123. decode_bits(KERN_DEBUG "ICR", icr_bits, ARRAY_SIZE(icr_bits), val);
  124. printk("\n");
  125. }
  126. static unsigned int i2c_debug = DEBUG;
  127. static void i2c_pxa_show_state(struct pxa_i2c *i2c, int lno, const char *fname)
  128. {
  129. dev_dbg(&i2c->adap.dev, "state:%s:%d: ISR=%08x, ICR=%08x, IBMR=%02x\n", fname, lno,
  130. readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  131. }
  132. #define show_state(i2c) i2c_pxa_show_state(i2c, __LINE__, __FUNCTION__)
  133. #else
  134. #define i2c_debug 0
  135. #define show_state(i2c) do { } while (0)
  136. #define decode_ISR(val) do { } while (0)
  137. #define decode_ICR(val) do { } while (0)
  138. #endif
  139. #define eedbg(lvl, x...) do { if ((lvl) < 1) { printk(KERN_DEBUG "" x); } } while(0)
  140. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
  141. static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
  142. {
  143. unsigned int i;
  144. printk("i2c: error: %s\n", why);
  145. printk("i2c: msg_num: %d msg_idx: %d msg_ptr: %d\n",
  146. i2c->msg_num, i2c->msg_idx, i2c->msg_ptr);
  147. printk("i2c: ICR: %08x ISR: %08x\n"
  148. "i2c: log: ", readl(_ICR(i2c)), readl(_ISR(i2c)));
  149. for (i = 0; i < i2c->irqlogidx; i++)
  150. printk("[%08x:%08x] ", i2c->isrlog[i], i2c->icrlog[i]);
  151. printk("\n");
  152. }
  153. static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
  154. {
  155. return !(readl(_ICR(i2c)) & ICR_SCLE);
  156. }
  157. static void i2c_pxa_abort(struct pxa_i2c *i2c)
  158. {
  159. unsigned long timeout = jiffies + HZ/4;
  160. if (i2c_pxa_is_slavemode(i2c)) {
  161. dev_dbg(&i2c->adap.dev, "%s: called in slave mode\n", __func__);
  162. return;
  163. }
  164. while (time_before(jiffies, timeout) && (readl(_IBMR(i2c)) & 0x1) == 0) {
  165. unsigned long icr = readl(_ICR(i2c));
  166. icr &= ~ICR_START;
  167. icr |= ICR_ACKNAK | ICR_STOP | ICR_TB;
  168. writel(icr, _ICR(i2c));
  169. show_state(i2c);
  170. msleep(1);
  171. }
  172. writel(readl(_ICR(i2c)) & ~(ICR_MA | ICR_START | ICR_STOP),
  173. _ICR(i2c));
  174. }
  175. static int i2c_pxa_wait_bus_not_busy(struct pxa_i2c *i2c)
  176. {
  177. int timeout = DEF_TIMEOUT;
  178. while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
  179. if ((readl(_ISR(i2c)) & ISR_SAD) != 0)
  180. timeout += 4;
  181. msleep(2);
  182. show_state(i2c);
  183. }
  184. if (timeout <= 0)
  185. show_state(i2c);
  186. return timeout <= 0 ? I2C_RETRY : 0;
  187. }
  188. static int i2c_pxa_wait_master(struct pxa_i2c *i2c)
  189. {
  190. unsigned long timeout = jiffies + HZ*4;
  191. while (time_before(jiffies, timeout)) {
  192. if (i2c_debug > 1)
  193. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  194. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  195. if (readl(_ISR(i2c)) & ISR_SAD) {
  196. if (i2c_debug > 0)
  197. dev_dbg(&i2c->adap.dev, "%s: Slave detected\n", __func__);
  198. goto out;
  199. }
  200. /* wait for unit and bus being not busy, and we also do a
  201. * quick check of the i2c lines themselves to ensure they've
  202. * gone high...
  203. */
  204. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) == 0 && readl(_IBMR(i2c)) == 3) {
  205. if (i2c_debug > 0)
  206. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  207. return 1;
  208. }
  209. msleep(1);
  210. }
  211. if (i2c_debug > 0)
  212. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  213. out:
  214. return 0;
  215. }
  216. static int i2c_pxa_set_master(struct pxa_i2c *i2c)
  217. {
  218. if (i2c_debug)
  219. dev_dbg(&i2c->adap.dev, "setting to bus master\n");
  220. if ((readl(_ISR(i2c)) & (ISR_UB | ISR_IBB)) != 0) {
  221. dev_dbg(&i2c->adap.dev, "%s: unit is busy\n", __func__);
  222. if (!i2c_pxa_wait_master(i2c)) {
  223. dev_dbg(&i2c->adap.dev, "%s: error: unit busy\n", __func__);
  224. return I2C_RETRY;
  225. }
  226. }
  227. writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
  228. return 0;
  229. }
  230. #ifdef CONFIG_I2C_PXA_SLAVE
  231. static int i2c_pxa_wait_slave(struct pxa_i2c *i2c)
  232. {
  233. unsigned long timeout = jiffies + HZ*1;
  234. /* wait for stop */
  235. show_state(i2c);
  236. while (time_before(jiffies, timeout)) {
  237. if (i2c_debug > 1)
  238. dev_dbg(&i2c->adap.dev, "%s: %ld: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  239. __func__, (long)jiffies, readl(_ISR(i2c)), readl(_ICR(i2c)), readl(_IBMR(i2c)));
  240. if ((readl(_ISR(i2c)) & (ISR_UB|ISR_IBB)) == 0 ||
  241. (readl(_ISR(i2c)) & ISR_SAD) != 0 ||
  242. (readl(_ICR(i2c)) & ICR_SCLE) == 0) {
  243. if (i2c_debug > 1)
  244. dev_dbg(&i2c->adap.dev, "%s: done\n", __func__);
  245. return 1;
  246. }
  247. msleep(1);
  248. }
  249. if (i2c_debug > 0)
  250. dev_dbg(&i2c->adap.dev, "%s: did not free\n", __func__);
  251. return 0;
  252. }
  253. /*
  254. * clear the hold on the bus, and take of anything else
  255. * that has been configured
  256. */
  257. static void i2c_pxa_set_slave(struct pxa_i2c *i2c, int errcode)
  258. {
  259. show_state(i2c);
  260. if (errcode < 0) {
  261. udelay(100); /* simple delay */
  262. } else {
  263. /* we need to wait for the stop condition to end */
  264. /* if we where in stop, then clear... */
  265. if (readl(_ICR(i2c)) & ICR_STOP) {
  266. udelay(100);
  267. writel(readl(_ICR(i2c)) & ~ICR_STOP, _ICR(i2c));
  268. }
  269. if (!i2c_pxa_wait_slave(i2c)) {
  270. dev_err(&i2c->adap.dev, "%s: wait timedout\n",
  271. __func__);
  272. return;
  273. }
  274. }
  275. writel(readl(_ICR(i2c)) & ~(ICR_STOP|ICR_ACKNAK|ICR_MA), _ICR(i2c));
  276. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  277. if (i2c_debug) {
  278. dev_dbg(&i2c->adap.dev, "ICR now %08x, ISR %08x\n", readl(_ICR(i2c)), readl(_ISR(i2c)));
  279. decode_ICR(readl(_ICR(i2c)));
  280. }
  281. }
  282. #else
  283. #define i2c_pxa_set_slave(i2c, err) do { } while (0)
  284. #endif
  285. static void i2c_pxa_reset(struct pxa_i2c *i2c)
  286. {
  287. pr_debug("Resetting I2C Controller Unit\n");
  288. /* abort any transfer currently under way */
  289. i2c_pxa_abort(i2c);
  290. /* reset according to 9.8 */
  291. writel(ICR_UR, _ICR(i2c));
  292. writel(I2C_ISR_INIT, _ISR(i2c));
  293. writel(readl(_ICR(i2c)) & ~ICR_UR, _ICR(i2c));
  294. writel(i2c->slave_addr, _ISAR(i2c));
  295. /* set control register values */
  296. writel(I2C_ICR_INIT, _ICR(i2c));
  297. #ifdef CONFIG_I2C_PXA_SLAVE
  298. dev_info(&i2c->adap.dev, "Enabling slave mode\n");
  299. writel(readl(_ICR(i2c)) | ICR_SADIE | ICR_ALDIE | ICR_SSDIE, _ICR(i2c));
  300. #endif
  301. i2c_pxa_set_slave(i2c, 0);
  302. /* enable unit */
  303. writel(readl(_ICR(i2c)) | ICR_IUE, _ICR(i2c));
  304. udelay(100);
  305. }
  306. #ifdef CONFIG_I2C_PXA_SLAVE
  307. /*
  308. * PXA I2C Slave mode
  309. */
  310. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  311. {
  312. if (isr & ISR_BED) {
  313. /* what should we do here? */
  314. } else {
  315. int ret = 0;
  316. if (i2c->slave != NULL)
  317. ret = i2c->slave->read(i2c->slave->data);
  318. writel(ret, _IDBR(i2c));
  319. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c)); /* allow next byte */
  320. }
  321. }
  322. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  323. {
  324. unsigned int byte = readl(_IDBR(i2c));
  325. if (i2c->slave != NULL)
  326. i2c->slave->write(i2c->slave->data, byte);
  327. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  328. }
  329. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  330. {
  331. int timeout;
  332. if (i2c_debug > 0)
  333. dev_dbg(&i2c->adap.dev, "SAD, mode is slave-%cx\n",
  334. (isr & ISR_RWM) ? 'r' : 't');
  335. if (i2c->slave != NULL)
  336. i2c->slave->event(i2c->slave->data,
  337. (isr & ISR_RWM) ? I2C_SLAVE_EVENT_START_READ : I2C_SLAVE_EVENT_START_WRITE);
  338. /*
  339. * slave could interrupt in the middle of us generating a
  340. * start condition... if this happens, we'd better back off
  341. * and stop holding the poor thing up
  342. */
  343. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  344. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  345. timeout = 0x10000;
  346. while (1) {
  347. if ((readl(_IBMR(i2c)) & 2) == 2)
  348. break;
  349. timeout--;
  350. if (timeout <= 0) {
  351. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  352. break;
  353. }
  354. }
  355. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  356. }
  357. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  358. {
  359. if (i2c_debug > 2)
  360. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop)\n");
  361. if (i2c->slave != NULL)
  362. i2c->slave->event(i2c->slave->data, I2C_SLAVE_EVENT_STOP);
  363. if (i2c_debug > 2)
  364. dev_dbg(&i2c->adap.dev, "ISR: SSD (Slave Stop) acked\n");
  365. /*
  366. * If we have a master-mode message waiting,
  367. * kick it off now that the slave has completed.
  368. */
  369. if (i2c->msg)
  370. i2c_pxa_master_complete(i2c, I2C_RETRY);
  371. }
  372. #else
  373. static void i2c_pxa_slave_txempty(struct pxa_i2c *i2c, u32 isr)
  374. {
  375. if (isr & ISR_BED) {
  376. /* what should we do here? */
  377. } else {
  378. writel(0, _IDBR(i2c));
  379. writel(readl(_ICR(i2c)) | ICR_TB, _ICR(i2c));
  380. }
  381. }
  382. static void i2c_pxa_slave_rxfull(struct pxa_i2c *i2c, u32 isr)
  383. {
  384. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  385. }
  386. static void i2c_pxa_slave_start(struct pxa_i2c *i2c, u32 isr)
  387. {
  388. int timeout;
  389. /*
  390. * slave could interrupt in the middle of us generating a
  391. * start condition... if this happens, we'd better back off
  392. * and stop holding the poor thing up
  393. */
  394. writel(readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP), _ICR(i2c));
  395. writel(readl(_ICR(i2c)) | ICR_TB | ICR_ACKNAK, _ICR(i2c));
  396. timeout = 0x10000;
  397. while (1) {
  398. if ((readl(_IBMR(i2c)) & 2) == 2)
  399. break;
  400. timeout--;
  401. if (timeout <= 0) {
  402. dev_err(&i2c->adap.dev, "timeout waiting for SCL high\n");
  403. break;
  404. }
  405. }
  406. writel(readl(_ICR(i2c)) & ~ICR_SCLE, _ICR(i2c));
  407. }
  408. static void i2c_pxa_slave_stop(struct pxa_i2c *i2c)
  409. {
  410. if (i2c->msg)
  411. i2c_pxa_master_complete(i2c, I2C_RETRY);
  412. }
  413. #endif
  414. /*
  415. * PXA I2C Master mode
  416. */
  417. static inline unsigned int i2c_pxa_addr_byte(struct i2c_msg *msg)
  418. {
  419. unsigned int addr = (msg->addr & 0x7f) << 1;
  420. if (msg->flags & I2C_M_RD)
  421. addr |= 1;
  422. return addr;
  423. }
  424. static inline void i2c_pxa_start_message(struct pxa_i2c *i2c)
  425. {
  426. u32 icr;
  427. /*
  428. * Step 1: target slave address into IDBR
  429. */
  430. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  431. /*
  432. * Step 2: initiate the write.
  433. */
  434. icr = readl(_ICR(i2c)) & ~(ICR_STOP | ICR_ALDIE);
  435. writel(icr | ICR_START | ICR_TB, _ICR(i2c));
  436. }
  437. static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
  438. {
  439. u32 icr;
  440. /*
  441. * Clear the STOP and ACK flags
  442. */
  443. icr = readl(_ICR(i2c));
  444. icr &= ~(ICR_STOP | ICR_ACKNAK);
  445. writel(icr, _ICR(i2c));
  446. }
  447. /*
  448. * We are protected by the adapter bus mutex.
  449. */
  450. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  451. {
  452. long timeout;
  453. int ret;
  454. /*
  455. * Wait for the bus to become free.
  456. */
  457. ret = i2c_pxa_wait_bus_not_busy(i2c);
  458. if (ret) {
  459. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  460. goto out;
  461. }
  462. /*
  463. * Set master mode.
  464. */
  465. ret = i2c_pxa_set_master(i2c);
  466. if (ret) {
  467. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  468. goto out;
  469. }
  470. spin_lock_irq(&i2c->lock);
  471. i2c->msg = msg;
  472. i2c->msg_num = num;
  473. i2c->msg_idx = 0;
  474. i2c->msg_ptr = 0;
  475. i2c->irqlogidx = 0;
  476. i2c_pxa_start_message(i2c);
  477. spin_unlock_irq(&i2c->lock);
  478. /*
  479. * The rest of the processing occurs in the interrupt handler.
  480. */
  481. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  482. i2c_pxa_stop_message(i2c);
  483. /*
  484. * We place the return code in i2c->msg_idx.
  485. */
  486. ret = i2c->msg_idx;
  487. if (timeout == 0)
  488. i2c_pxa_scream_blue_murder(i2c, "timeout");
  489. out:
  490. return ret;
  491. }
  492. /*
  493. * i2c_pxa_master_complete - complete the message and wake up.
  494. */
  495. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  496. {
  497. i2c->msg_ptr = 0;
  498. i2c->msg = NULL;
  499. i2c->msg_idx ++;
  500. i2c->msg_num = 0;
  501. if (ret)
  502. i2c->msg_idx = ret;
  503. wake_up(&i2c->wait);
  504. }
  505. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  506. {
  507. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  508. again:
  509. /*
  510. * If ISR_ALD is set, we lost arbitration.
  511. */
  512. if (isr & ISR_ALD) {
  513. /*
  514. * Do we need to do anything here? The PXA docs
  515. * are vague about what happens.
  516. */
  517. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  518. /*
  519. * We ignore this error. We seem to see spurious ALDs
  520. * for seemingly no reason. If we handle them as I think
  521. * they should, we end up causing an I2C error, which
  522. * is painful for some systems.
  523. */
  524. return; /* ignore */
  525. }
  526. if (isr & ISR_BED) {
  527. int ret = BUS_ERROR;
  528. /*
  529. * I2C bus error - either the device NAK'd us, or
  530. * something more serious happened. If we were NAK'd
  531. * on the initial address phase, we can retry.
  532. */
  533. if (isr & ISR_ACKNAK) {
  534. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  535. ret = I2C_RETRY;
  536. else
  537. ret = XFER_NAKED;
  538. }
  539. i2c_pxa_master_complete(i2c, ret);
  540. } else if (isr & ISR_RWM) {
  541. /*
  542. * Read mode. We have just sent the address byte, and
  543. * now we must initiate the transfer.
  544. */
  545. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  546. i2c->msg_idx == i2c->msg_num - 1)
  547. icr |= ICR_STOP | ICR_ACKNAK;
  548. icr |= ICR_ALDIE | ICR_TB;
  549. } else if (i2c->msg_ptr < i2c->msg->len) {
  550. /*
  551. * Write mode. Write the next data byte.
  552. */
  553. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  554. icr |= ICR_ALDIE | ICR_TB;
  555. /*
  556. * If this is the last byte of the last message, send
  557. * a STOP.
  558. */
  559. if (i2c->msg_ptr == i2c->msg->len &&
  560. i2c->msg_idx == i2c->msg_num - 1)
  561. icr |= ICR_STOP;
  562. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  563. /*
  564. * Next segment of the message.
  565. */
  566. i2c->msg_ptr = 0;
  567. i2c->msg_idx ++;
  568. i2c->msg++;
  569. /*
  570. * If we aren't doing a repeated start and address,
  571. * go back and try to send the next byte. Note that
  572. * we do not support switching the R/W direction here.
  573. */
  574. if (i2c->msg->flags & I2C_M_NOSTART)
  575. goto again;
  576. /*
  577. * Write the next address.
  578. */
  579. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  580. /*
  581. * And trigger a repeated start, and send the byte.
  582. */
  583. icr &= ~ICR_ALDIE;
  584. icr |= ICR_START | ICR_TB;
  585. } else {
  586. if (i2c->msg->len == 0) {
  587. /*
  588. * Device probes have a message length of zero
  589. * and need the bus to be reset before it can
  590. * be used again.
  591. */
  592. i2c_pxa_reset(i2c);
  593. }
  594. i2c_pxa_master_complete(i2c, 0);
  595. }
  596. i2c->icrlog[i2c->irqlogidx-1] = icr;
  597. writel(icr, _ICR(i2c));
  598. show_state(i2c);
  599. }
  600. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  601. {
  602. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  603. /*
  604. * Read the byte.
  605. */
  606. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  607. if (i2c->msg_ptr < i2c->msg->len) {
  608. /*
  609. * If this is the last byte of the last
  610. * message, send a STOP.
  611. */
  612. if (i2c->msg_ptr == i2c->msg->len - 1)
  613. icr |= ICR_STOP | ICR_ACKNAK;
  614. icr |= ICR_ALDIE | ICR_TB;
  615. } else {
  616. i2c_pxa_master_complete(i2c, 0);
  617. }
  618. i2c->icrlog[i2c->irqlogidx-1] = icr;
  619. writel(icr, _ICR(i2c));
  620. }
  621. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  622. {
  623. struct pxa_i2c *i2c = dev_id;
  624. u32 isr = readl(_ISR(i2c));
  625. if (i2c_debug > 2 && 0) {
  626. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  627. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  628. decode_ISR(isr);
  629. }
  630. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  631. i2c->isrlog[i2c->irqlogidx++] = isr;
  632. show_state(i2c);
  633. /*
  634. * Always clear all pending IRQs.
  635. */
  636. writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
  637. if (isr & ISR_SAD)
  638. i2c_pxa_slave_start(i2c, isr);
  639. if (isr & ISR_SSD)
  640. i2c_pxa_slave_stop(i2c);
  641. if (i2c_pxa_is_slavemode(i2c)) {
  642. if (isr & ISR_ITE)
  643. i2c_pxa_slave_txempty(i2c, isr);
  644. if (isr & ISR_IRF)
  645. i2c_pxa_slave_rxfull(i2c, isr);
  646. } else if (i2c->msg) {
  647. if (isr & ISR_ITE)
  648. i2c_pxa_irq_txempty(i2c, isr);
  649. if (isr & ISR_IRF)
  650. i2c_pxa_irq_rxfull(i2c, isr);
  651. } else {
  652. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  653. }
  654. return IRQ_HANDLED;
  655. }
  656. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  657. {
  658. struct pxa_i2c *i2c = adap->algo_data;
  659. int ret, i;
  660. /* If the I2C controller is disabled we need to reset it (probably due
  661. to a suspend/resume destroying state). We do this here as we can then
  662. avoid worrying about resuming the controller before its users. */
  663. if (!(readl(_ICR(i2c)) & ICR_IUE))
  664. i2c_pxa_reset(i2c);
  665. for (i = adap->retries; i >= 0; i--) {
  666. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  667. if (ret != I2C_RETRY)
  668. goto out;
  669. if (i2c_debug)
  670. dev_dbg(&adap->dev, "Retrying transmission\n");
  671. udelay(100);
  672. }
  673. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  674. ret = -EREMOTEIO;
  675. out:
  676. i2c_pxa_set_slave(i2c, ret);
  677. return ret;
  678. }
  679. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  680. {
  681. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  682. }
  683. static const struct i2c_algorithm i2c_pxa_algorithm = {
  684. .master_xfer = i2c_pxa_xfer,
  685. .functionality = i2c_pxa_functionality,
  686. };
  687. static struct pxa_i2c i2c_pxa = {
  688. .lock = __SPIN_LOCK_UNLOCKED(i2c_pxa.lock),
  689. .adap = {
  690. .owner = THIS_MODULE,
  691. .algo = &i2c_pxa_algorithm,
  692. .name = "pxa2xx-i2c.0",
  693. .retries = 5,
  694. },
  695. };
  696. #define res_len(r) ((r)->end - (r)->start + 1)
  697. static int i2c_pxa_probe(struct platform_device *dev)
  698. {
  699. struct pxa_i2c *i2c = &i2c_pxa;
  700. struct resource *res;
  701. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  702. int ret;
  703. int irq;
  704. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  705. irq = platform_get_irq(dev, 0);
  706. if (res == NULL || irq < 0)
  707. return -ENODEV;
  708. if (!request_mem_region(res->start, res_len(res), res->name))
  709. return -ENOMEM;
  710. i2c = kmalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
  711. if (!i2c) {
  712. ret = -ENOMEM;
  713. goto emalloc;
  714. }
  715. memcpy(i2c, &i2c_pxa, sizeof(struct pxa_i2c));
  716. init_waitqueue_head(&i2c->wait);
  717. i2c->adap.name[strlen(i2c->adap.name) - 1] = '0' + dev->id % 10;
  718. i2c->reg_base = ioremap(res->start, res_len(res));
  719. if (!i2c->reg_base) {
  720. ret = -EIO;
  721. goto eremap;
  722. }
  723. i2c->iobase = res->start;
  724. i2c->iosize = res_len(res);
  725. i2c->irq = irq;
  726. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  727. #ifdef CONFIG_I2C_PXA_SLAVE
  728. if (plat) {
  729. i2c->slave_addr = plat->slave_addr;
  730. i2c->slave = plat->slave;
  731. }
  732. #endif
  733. switch (dev->id) {
  734. case 0:
  735. #ifdef CONFIG_PXA27x
  736. pxa_gpio_mode(GPIO117_I2CSCL_MD);
  737. pxa_gpio_mode(GPIO118_I2CSDA_MD);
  738. #endif
  739. pxa_set_cken(CKEN_I2C, 1);
  740. break;
  741. #ifdef CONFIG_PXA27x
  742. case 1:
  743. local_irq_disable();
  744. PCFR |= PCFR_PI2CEN;
  745. local_irq_enable();
  746. pxa_set_cken(CKEN_PWRI2C, 1);
  747. #endif
  748. }
  749. ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
  750. i2c->adap.name, i2c);
  751. if (ret)
  752. goto ereqirq;
  753. i2c_pxa_reset(i2c);
  754. i2c->adap.algo_data = i2c;
  755. i2c->adap.dev.parent = &dev->dev;
  756. if (plat) {
  757. i2c->adap.class = plat->class;
  758. }
  759. ret = i2c_add_adapter(&i2c->adap);
  760. if (ret < 0) {
  761. printk(KERN_INFO "I2C: Failed to add bus\n");
  762. goto eadapt;
  763. }
  764. platform_set_drvdata(dev, i2c);
  765. #ifdef CONFIG_I2C_PXA_SLAVE
  766. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  767. i2c->adap.dev.bus_id, i2c->slave_addr);
  768. #else
  769. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  770. i2c->adap.dev.bus_id);
  771. #endif
  772. return 0;
  773. eadapt:
  774. free_irq(irq, i2c);
  775. ereqirq:
  776. switch (dev->id) {
  777. case 0:
  778. pxa_set_cken(CKEN_I2C, 0);
  779. break;
  780. #ifdef CONFIG_PXA27x
  781. case 1:
  782. pxa_set_cken(CKEN_PWRI2C, 0);
  783. local_irq_disable();
  784. PCFR &= ~PCFR_PI2CEN;
  785. local_irq_enable();
  786. #endif
  787. }
  788. eremap:
  789. kfree(i2c);
  790. emalloc:
  791. release_mem_region(res->start, res_len(res));
  792. return ret;
  793. }
  794. static int i2c_pxa_remove(struct platform_device *dev)
  795. {
  796. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  797. platform_set_drvdata(dev, NULL);
  798. i2c_del_adapter(&i2c->adap);
  799. free_irq(i2c->irq, i2c);
  800. switch (dev->id) {
  801. case 0:
  802. pxa_set_cken(CKEN_I2C, 0);
  803. break;
  804. #ifdef CONFIG_PXA27x
  805. case 1:
  806. pxa_set_cken(CKEN_PWRI2C, 0);
  807. local_irq_disable();
  808. PCFR &= ~PCFR_PI2CEN;
  809. local_irq_enable();
  810. #endif
  811. }
  812. release_mem_region(i2c->iobase, i2c->iosize);
  813. kfree(i2c);
  814. return 0;
  815. }
  816. static struct platform_driver i2c_pxa_driver = {
  817. .probe = i2c_pxa_probe,
  818. .remove = i2c_pxa_remove,
  819. .driver = {
  820. .name = "pxa2xx-i2c",
  821. },
  822. };
  823. static int __init i2c_adap_pxa_init(void)
  824. {
  825. return platform_driver_register(&i2c_pxa_driver);
  826. }
  827. static void i2c_adap_pxa_exit(void)
  828. {
  829. return platform_driver_unregister(&i2c_pxa_driver);
  830. }
  831. MODULE_LICENSE("GPL");
  832. module_init(i2c_adap_pxa_init);
  833. module_exit(i2c_adap_pxa_exit);