i2c-pxa.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. /*
  438. * We are protected by the adapter bus mutex.
  439. */
  440. static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
  441. {
  442. long timeout;
  443. int ret;
  444. /*
  445. * Wait for the bus to become free.
  446. */
  447. ret = i2c_pxa_wait_bus_not_busy(i2c);
  448. if (ret) {
  449. dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
  450. goto out;
  451. }
  452. /*
  453. * Set master mode.
  454. */
  455. ret = i2c_pxa_set_master(i2c);
  456. if (ret) {
  457. dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
  458. goto out;
  459. }
  460. spin_lock_irq(&i2c->lock);
  461. i2c->msg = msg;
  462. i2c->msg_num = num;
  463. i2c->msg_idx = 0;
  464. i2c->msg_ptr = 0;
  465. i2c->irqlogidx = 0;
  466. i2c_pxa_start_message(i2c);
  467. spin_unlock_irq(&i2c->lock);
  468. /*
  469. * The rest of the processing occurs in the interrupt handler.
  470. */
  471. timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
  472. /*
  473. * We place the return code in i2c->msg_idx.
  474. */
  475. ret = i2c->msg_idx;
  476. if (timeout == 0)
  477. i2c_pxa_scream_blue_murder(i2c, "timeout");
  478. out:
  479. return ret;
  480. }
  481. /*
  482. * i2c_pxa_master_complete - complete the message and wake up.
  483. */
  484. static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret)
  485. {
  486. i2c->msg_ptr = 0;
  487. i2c->msg = NULL;
  488. i2c->msg_idx ++;
  489. i2c->msg_num = 0;
  490. if (ret)
  491. i2c->msg_idx = ret;
  492. wake_up(&i2c->wait);
  493. }
  494. static void i2c_pxa_irq_txempty(struct pxa_i2c *i2c, u32 isr)
  495. {
  496. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  497. again:
  498. /*
  499. * If ISR_ALD is set, we lost arbitration.
  500. */
  501. if (isr & ISR_ALD) {
  502. /*
  503. * Do we need to do anything here? The PXA docs
  504. * are vague about what happens.
  505. */
  506. i2c_pxa_scream_blue_murder(i2c, "ALD set");
  507. /*
  508. * We ignore this error. We seem to see spurious ALDs
  509. * for seemingly no reason. If we handle them as I think
  510. * they should, we end up causing an I2C error, which
  511. * is painful for some systems.
  512. */
  513. return; /* ignore */
  514. }
  515. if (isr & ISR_BED) {
  516. int ret = BUS_ERROR;
  517. /*
  518. * I2C bus error - either the device NAK'd us, or
  519. * something more serious happened. If we were NAK'd
  520. * on the initial address phase, we can retry.
  521. */
  522. if (isr & ISR_ACKNAK) {
  523. if (i2c->msg_ptr == 0 && i2c->msg_idx == 0)
  524. ret = I2C_RETRY;
  525. else
  526. ret = XFER_NAKED;
  527. }
  528. i2c_pxa_master_complete(i2c, ret);
  529. } else if (isr & ISR_RWM) {
  530. /*
  531. * Read mode. We have just sent the address byte, and
  532. * now we must initiate the transfer.
  533. */
  534. if (i2c->msg_ptr == i2c->msg->len - 1 &&
  535. i2c->msg_idx == i2c->msg_num - 1)
  536. icr |= ICR_STOP | ICR_ACKNAK;
  537. icr |= ICR_ALDIE | ICR_TB;
  538. } else if (i2c->msg_ptr < i2c->msg->len) {
  539. /*
  540. * Write mode. Write the next data byte.
  541. */
  542. writel(i2c->msg->buf[i2c->msg_ptr++], _IDBR(i2c));
  543. icr |= ICR_ALDIE | ICR_TB;
  544. /*
  545. * If this is the last byte of the last message, send
  546. * a STOP.
  547. */
  548. if (i2c->msg_ptr == i2c->msg->len &&
  549. i2c->msg_idx == i2c->msg_num - 1)
  550. icr |= ICR_STOP;
  551. } else if (i2c->msg_idx < i2c->msg_num - 1) {
  552. /*
  553. * Next segment of the message.
  554. */
  555. i2c->msg_ptr = 0;
  556. i2c->msg_idx ++;
  557. i2c->msg++;
  558. /*
  559. * If we aren't doing a repeated start and address,
  560. * go back and try to send the next byte. Note that
  561. * we do not support switching the R/W direction here.
  562. */
  563. if (i2c->msg->flags & I2C_M_NOSTART)
  564. goto again;
  565. /*
  566. * Write the next address.
  567. */
  568. writel(i2c_pxa_addr_byte(i2c->msg), _IDBR(i2c));
  569. /*
  570. * And trigger a repeated start, and send the byte.
  571. */
  572. icr &= ~ICR_ALDIE;
  573. icr |= ICR_START | ICR_TB;
  574. } else {
  575. if (i2c->msg->len == 0) {
  576. /*
  577. * Device probes have a message length of zero
  578. * and need the bus to be reset before it can
  579. * be used again.
  580. */
  581. i2c_pxa_reset(i2c);
  582. }
  583. i2c_pxa_master_complete(i2c, 0);
  584. }
  585. i2c->icrlog[i2c->irqlogidx-1] = icr;
  586. writel(icr, _ICR(i2c));
  587. show_state(i2c);
  588. }
  589. static void i2c_pxa_irq_rxfull(struct pxa_i2c *i2c, u32 isr)
  590. {
  591. u32 icr = readl(_ICR(i2c)) & ~(ICR_START|ICR_STOP|ICR_ACKNAK|ICR_TB);
  592. /*
  593. * Read the byte.
  594. */
  595. i2c->msg->buf[i2c->msg_ptr++] = readl(_IDBR(i2c));
  596. if (i2c->msg_ptr < i2c->msg->len) {
  597. /*
  598. * If this is the last byte of the last
  599. * message, send a STOP.
  600. */
  601. if (i2c->msg_ptr == i2c->msg->len - 1)
  602. icr |= ICR_STOP | ICR_ACKNAK;
  603. icr |= ICR_ALDIE | ICR_TB;
  604. } else {
  605. i2c_pxa_master_complete(i2c, 0);
  606. }
  607. i2c->icrlog[i2c->irqlogidx-1] = icr;
  608. writel(icr, _ICR(i2c));
  609. }
  610. static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
  611. {
  612. struct pxa_i2c *i2c = dev_id;
  613. u32 isr = readl(_ISR(i2c));
  614. if (i2c_debug > 2 && 0) {
  615. dev_dbg(&i2c->adap.dev, "%s: ISR=%08x, ICR=%08x, IBMR=%02x\n",
  616. __func__, isr, readl(_ICR(i2c)), readl(_IBMR(i2c)));
  617. decode_ISR(isr);
  618. }
  619. if (i2c->irqlogidx < ARRAY_SIZE(i2c->isrlog))
  620. i2c->isrlog[i2c->irqlogidx++] = isr;
  621. show_state(i2c);
  622. /*
  623. * Always clear all pending IRQs.
  624. */
  625. writel(isr & (ISR_SSD|ISR_ALD|ISR_ITE|ISR_IRF|ISR_SAD|ISR_BED), _ISR(i2c));
  626. if (isr & ISR_SAD)
  627. i2c_pxa_slave_start(i2c, isr);
  628. if (isr & ISR_SSD)
  629. i2c_pxa_slave_stop(i2c);
  630. if (i2c_pxa_is_slavemode(i2c)) {
  631. if (isr & ISR_ITE)
  632. i2c_pxa_slave_txempty(i2c, isr);
  633. if (isr & ISR_IRF)
  634. i2c_pxa_slave_rxfull(i2c, isr);
  635. } else if (i2c->msg) {
  636. if (isr & ISR_ITE)
  637. i2c_pxa_irq_txempty(i2c, isr);
  638. if (isr & ISR_IRF)
  639. i2c_pxa_irq_rxfull(i2c, isr);
  640. } else {
  641. i2c_pxa_scream_blue_murder(i2c, "spurious irq");
  642. }
  643. return IRQ_HANDLED;
  644. }
  645. static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  646. {
  647. struct pxa_i2c *i2c = adap->algo_data;
  648. int ret, i;
  649. /* If the I2C controller is disabled we need to reset it (probably due
  650. to a suspend/resume destroying state). We do this here as we can then
  651. avoid worrying about resuming the controller before its users. */
  652. if (!(readl(_ICR(i2c)) & ICR_IUE))
  653. i2c_pxa_reset(i2c);
  654. for (i = adap->retries; i >= 0; i--) {
  655. ret = i2c_pxa_do_xfer(i2c, msgs, num);
  656. if (ret != I2C_RETRY)
  657. goto out;
  658. if (i2c_debug)
  659. dev_dbg(&adap->dev, "Retrying transmission\n");
  660. udelay(100);
  661. }
  662. i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
  663. ret = -EREMOTEIO;
  664. out:
  665. i2c_pxa_set_slave(i2c, ret);
  666. return ret;
  667. }
  668. static u32 i2c_pxa_functionality(struct i2c_adapter *adap)
  669. {
  670. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  671. }
  672. static const struct i2c_algorithm i2c_pxa_algorithm = {
  673. .master_xfer = i2c_pxa_xfer,
  674. .functionality = i2c_pxa_functionality,
  675. };
  676. static struct pxa_i2c i2c_pxa = {
  677. .lock = SPIN_LOCK_UNLOCKED,
  678. .adap = {
  679. .owner = THIS_MODULE,
  680. .algo = &i2c_pxa_algorithm,
  681. .name = "pxa2xx-i2c.0",
  682. .retries = 5,
  683. },
  684. };
  685. #define res_len(r) ((r)->end - (r)->start + 1)
  686. static int i2c_pxa_probe(struct platform_device *dev)
  687. {
  688. struct pxa_i2c *i2c = &i2c_pxa;
  689. struct resource *res;
  690. #ifdef CONFIG_I2C_PXA_SLAVE
  691. struct i2c_pxa_platform_data *plat = dev->dev.platform_data;
  692. #endif
  693. int ret;
  694. int irq;
  695. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  696. irq = platform_get_irq(dev, 0);
  697. if (res == NULL || irq < 0)
  698. return -ENODEV;
  699. if (!request_mem_region(res->start, res_len(res), res->name))
  700. return -ENOMEM;
  701. i2c = kmalloc(sizeof(struct pxa_i2c), GFP_KERNEL);
  702. if (!i2c) {
  703. ret = -ENOMEM;
  704. goto emalloc;
  705. }
  706. memcpy(i2c, &i2c_pxa, sizeof(struct pxa_i2c));
  707. init_waitqueue_head(&i2c->wait);
  708. i2c->adap.name[strlen(i2c->adap.name) - 1] = '0' + dev->id % 10;
  709. i2c->reg_base = ioremap(res->start, res_len(res));
  710. if (!i2c->reg_base) {
  711. ret = -EIO;
  712. goto eremap;
  713. }
  714. i2c->iobase = res->start;
  715. i2c->iosize = res_len(res);
  716. i2c->irq = irq;
  717. i2c->slave_addr = I2C_PXA_SLAVE_ADDR;
  718. #ifdef CONFIG_I2C_PXA_SLAVE
  719. if (plat) {
  720. i2c->slave_addr = plat->slave_addr;
  721. i2c->slave = plat->slave;
  722. }
  723. #endif
  724. switch (dev->id) {
  725. case 0:
  726. #ifdef CONFIG_PXA27x
  727. pxa_gpio_mode(GPIO117_I2CSCL_MD);
  728. pxa_gpio_mode(GPIO118_I2CSDA_MD);
  729. #endif
  730. pxa_set_cken(CKEN14_I2C, 1);
  731. break;
  732. #ifdef CONFIG_PXA27x
  733. case 1:
  734. local_irq_disable();
  735. PCFR |= PCFR_PI2CEN;
  736. local_irq_enable();
  737. pxa_set_cken(CKEN15_PWRI2C, 1);
  738. #endif
  739. }
  740. ret = request_irq(irq, i2c_pxa_handler, IRQF_DISABLED,
  741. i2c->adap.name, i2c);
  742. if (ret)
  743. goto ereqirq;
  744. i2c_pxa_reset(i2c);
  745. i2c->adap.algo_data = i2c;
  746. i2c->adap.dev.parent = &dev->dev;
  747. ret = i2c_add_adapter(&i2c->adap);
  748. if (ret < 0) {
  749. printk(KERN_INFO "I2C: Failed to add bus\n");
  750. goto eadapt;
  751. }
  752. platform_set_drvdata(dev, i2c);
  753. #ifdef CONFIG_I2C_PXA_SLAVE
  754. printk(KERN_INFO "I2C: %s: PXA I2C adapter, slave address %d\n",
  755. i2c->adap.dev.bus_id, i2c->slave_addr);
  756. #else
  757. printk(KERN_INFO "I2C: %s: PXA I2C adapter\n",
  758. i2c->adap.dev.bus_id);
  759. #endif
  760. return 0;
  761. eadapt:
  762. free_irq(irq, i2c);
  763. ereqirq:
  764. switch (dev->id) {
  765. case 0:
  766. pxa_set_cken(CKEN14_I2C, 0);
  767. break;
  768. #ifdef CONFIG_PXA27x
  769. case 1:
  770. pxa_set_cken(CKEN15_PWRI2C, 0);
  771. local_irq_disable();
  772. PCFR &= ~PCFR_PI2CEN;
  773. local_irq_enable();
  774. #endif
  775. }
  776. eremap:
  777. kfree(i2c);
  778. emalloc:
  779. release_mem_region(res->start, res_len(res));
  780. return ret;
  781. }
  782. static int i2c_pxa_remove(struct platform_device *dev)
  783. {
  784. struct pxa_i2c *i2c = platform_get_drvdata(dev);
  785. platform_set_drvdata(dev, NULL);
  786. i2c_del_adapter(&i2c->adap);
  787. free_irq(i2c->irq, i2c);
  788. switch (dev->id) {
  789. case 0:
  790. pxa_set_cken(CKEN14_I2C, 0);
  791. break;
  792. #ifdef CONFIG_PXA27x
  793. case 1:
  794. pxa_set_cken(CKEN15_PWRI2C, 0);
  795. local_irq_disable();
  796. PCFR &= ~PCFR_PI2CEN;
  797. local_irq_enable();
  798. #endif
  799. }
  800. release_mem_region(i2c->iobase, i2c->iosize);
  801. kfree(i2c);
  802. return 0;
  803. }
  804. static struct platform_driver i2c_pxa_driver = {
  805. .probe = i2c_pxa_probe,
  806. .remove = i2c_pxa_remove,
  807. .driver = {
  808. .name = "pxa2xx-i2c",
  809. },
  810. };
  811. static int __init i2c_adap_pxa_init(void)
  812. {
  813. return platform_driver_register(&i2c_pxa_driver);
  814. }
  815. static void i2c_adap_pxa_exit(void)
  816. {
  817. return platform_driver_unregister(&i2c_pxa_driver);
  818. }
  819. MODULE_LICENSE("GPL");
  820. module_init(i2c_adap_pxa_init);
  821. module_exit(i2c_adap_pxa_exit);