i2c-pxa.c 27 KB

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