i2c-pxa.c 22 KB

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