i2c-pxa.c 27 KB

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