i2c-pxa.c 21 KB

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