i2c-pxa.c 25 KB

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