i2c-pxa.c 24 KB

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