i2c-pxa.c 25 KB

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