i2c-pxa.c 25 KB

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