i2c-pxa.c 22 KB

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