i2c-pxa.c 25 KB

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