i2c-pxa.c 27 KB

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