i2c-pxa.c 25 KB

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