i2c-pxa.c 27 KB

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