i2c-iop3xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* ------------------------------------------------------------------------- */
  2. /* i2c-iop3xx.c i2c driver algorithms for Intel XScale IOP3xx & IXP46x */
  3. /* ------------------------------------------------------------------------- */
  4. /* Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
  5. * <Peter dot Milne at D hyphen TACQ dot com>
  6. *
  7. * With acknowledgements to i2c-algo-ibm_ocp.c by
  8. * Ian DaSilva, MontaVista Software, Inc. idasilva@mvista.com
  9. *
  10. * And i2c-algo-pcf.c, which was created by Simon G. Vogl and Hans Berglund:
  11. *
  12. * Copyright (C) 1995-1997 Simon G. Vogl, 1998-2000 Hans Berglund
  13. *
  14. * And which acknowledged Kyösti Mälkki <kmalkki@cc.hut.fi>,
  15. * Frodo Looijaard <frodol@dds.nl>, Martin Bailey<mbailey@littlefeet-inc.com>
  16. *
  17. * Major cleanup by Deepak Saxena <dsaxena@plexity.net>, 01/2005:
  18. *
  19. * - Use driver model to pass per-chip info instead of hardcoding and #ifdefs
  20. * - Use ioremap/__raw_readl/__raw_writel instead of direct dereference
  21. * - Make it work with IXP46x chips
  22. * - Cleanup function names, coding style, etc
  23. *
  24. * - writing to slave address causes latchup on iop331.
  25. * fix: driver refuses to address self.
  26. *
  27. * This program is free software; you can redistribute it and/or modify
  28. * it under the terms of the GNU General Public License as published by
  29. * the Free Software Foundation, version 2.
  30. */
  31. #include <linux/interrupt.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/delay.h>
  35. #include <linux/slab.h>
  36. #include <linux/init.h>
  37. #include <linux/errno.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/i2c.h>
  40. #include <asm/io.h>
  41. #include "i2c-iop3xx.h"
  42. /* global unit counter */
  43. static int i2c_id;
  44. static inline unsigned char
  45. iic_cook_addr(struct i2c_msg *msg)
  46. {
  47. unsigned char addr;
  48. addr = (msg->addr << 1);
  49. if (msg->flags & I2C_M_RD)
  50. addr |= 1;
  51. /*
  52. * Read or Write?
  53. */
  54. if (msg->flags & I2C_M_REV_DIR_ADDR)
  55. addr ^= 1;
  56. return addr;
  57. }
  58. static void
  59. iop3xx_i2c_reset(struct i2c_algo_iop3xx_data *iop3xx_adap)
  60. {
  61. /* Follows devman 9.3 */
  62. __raw_writel(IOP3XX_ICR_UNIT_RESET, iop3xx_adap->ioaddr + CR_OFFSET);
  63. __raw_writel(IOP3XX_ISR_CLEARBITS, iop3xx_adap->ioaddr + SR_OFFSET);
  64. __raw_writel(0, iop3xx_adap->ioaddr + CR_OFFSET);
  65. }
  66. static void
  67. iop3xx_i2c_enable(struct i2c_algo_iop3xx_data *iop3xx_adap)
  68. {
  69. u32 cr = IOP3XX_ICR_GCD | IOP3XX_ICR_SCLEN | IOP3XX_ICR_UE;
  70. /*
  71. * Every time unit enable is asserted, GPOD needs to be cleared
  72. * on IOP3XX to avoid data corruption on the bus.
  73. */
  74. #if defined(CONFIG_ARCH_IOP32X) || defined(CONFIG_ARCH_IOP33X)
  75. if (iop3xx_adap->id == 0) {
  76. gpio_line_set(IOP3XX_GPIO_LINE(7), GPIO_LOW);
  77. gpio_line_set(IOP3XX_GPIO_LINE(6), GPIO_LOW);
  78. } else {
  79. gpio_line_set(IOP3XX_GPIO_LINE(5), GPIO_LOW);
  80. gpio_line_set(IOP3XX_GPIO_LINE(4), GPIO_LOW);
  81. }
  82. #endif
  83. /* NB SR bits not same position as CR IE bits :-( */
  84. iop3xx_adap->SR_enabled =
  85. IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD |
  86. IOP3XX_ISR_RXFULL | IOP3XX_ISR_TXEMPTY;
  87. cr |= IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
  88. IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE;
  89. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  90. }
  91. static void
  92. iop3xx_i2c_transaction_cleanup(struct i2c_algo_iop3xx_data *iop3xx_adap)
  93. {
  94. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  95. cr &= ~(IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE |
  96. IOP3XX_ICR_MSTOP | IOP3XX_ICR_SCLEN);
  97. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  98. }
  99. /*
  100. * NB: the handler has to clear the source of the interrupt!
  101. * Then it passes the SR flags of interest to BH via adap data
  102. */
  103. static irqreturn_t
  104. iop3xx_i2c_irq_handler(int this_irq, void *dev_id)
  105. {
  106. struct i2c_algo_iop3xx_data *iop3xx_adap = dev_id;
  107. u32 sr = __raw_readl(iop3xx_adap->ioaddr + SR_OFFSET);
  108. if ((sr &= iop3xx_adap->SR_enabled)) {
  109. __raw_writel(sr, iop3xx_adap->ioaddr + SR_OFFSET);
  110. iop3xx_adap->SR_received |= sr;
  111. wake_up_interruptible(&iop3xx_adap->waitq);
  112. }
  113. return IRQ_HANDLED;
  114. }
  115. /* check all error conditions, clear them , report most important */
  116. static int
  117. iop3xx_i2c_error(u32 sr)
  118. {
  119. int rc = 0;
  120. if ((sr & IOP3XX_ISR_BERRD)) {
  121. if ( !rc ) rc = -I2C_ERR_BERR;
  122. }
  123. if ((sr & IOP3XX_ISR_ALD)) {
  124. if ( !rc ) rc = -I2C_ERR_ALD;
  125. }
  126. return rc;
  127. }
  128. static inline u32
  129. iop3xx_i2c_get_srstat(struct i2c_algo_iop3xx_data *iop3xx_adap)
  130. {
  131. unsigned long flags;
  132. u32 sr;
  133. spin_lock_irqsave(&iop3xx_adap->lock, flags);
  134. sr = iop3xx_adap->SR_received;
  135. iop3xx_adap->SR_received = 0;
  136. spin_unlock_irqrestore(&iop3xx_adap->lock, flags);
  137. return sr;
  138. }
  139. /*
  140. * sleep until interrupted, then recover and analyse the SR
  141. * saved by handler
  142. */
  143. typedef int (* compare_func)(unsigned test, unsigned mask);
  144. /* returns 1 on correct comparison */
  145. static int
  146. iop3xx_i2c_wait_event(struct i2c_algo_iop3xx_data *iop3xx_adap,
  147. unsigned flags, unsigned* status,
  148. compare_func compare)
  149. {
  150. unsigned sr = 0;
  151. int interrupted;
  152. int done;
  153. int rc = 0;
  154. do {
  155. interrupted = wait_event_interruptible_timeout (
  156. iop3xx_adap->waitq,
  157. (done = compare( sr = iop3xx_i2c_get_srstat(iop3xx_adap) ,flags )),
  158. 1 * HZ;
  159. );
  160. if ((rc = iop3xx_i2c_error(sr)) < 0) {
  161. *status = sr;
  162. return rc;
  163. } else if (!interrupted) {
  164. *status = sr;
  165. return -ETIMEDOUT;
  166. }
  167. } while(!done);
  168. *status = sr;
  169. return 0;
  170. }
  171. /*
  172. * Concrete compare_funcs
  173. */
  174. static int
  175. all_bits_clear(unsigned test, unsigned mask)
  176. {
  177. return (test & mask) == 0;
  178. }
  179. static int
  180. any_bits_set(unsigned test, unsigned mask)
  181. {
  182. return (test & mask) != 0;
  183. }
  184. static int
  185. iop3xx_i2c_wait_tx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  186. {
  187. return iop3xx_i2c_wait_event(
  188. iop3xx_adap,
  189. IOP3XX_ISR_TXEMPTY | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
  190. status, any_bits_set);
  191. }
  192. static int
  193. iop3xx_i2c_wait_rx_done(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  194. {
  195. return iop3xx_i2c_wait_event(
  196. iop3xx_adap,
  197. IOP3XX_ISR_RXFULL | IOP3XX_ISR_ALD | IOP3XX_ISR_BERRD,
  198. status, any_bits_set);
  199. }
  200. static int
  201. iop3xx_i2c_wait_idle(struct i2c_algo_iop3xx_data *iop3xx_adap, int *status)
  202. {
  203. return iop3xx_i2c_wait_event(
  204. iop3xx_adap, IOP3XX_ISR_UNITBUSY, status, all_bits_clear);
  205. }
  206. static int
  207. iop3xx_i2c_send_target_addr(struct i2c_algo_iop3xx_data *iop3xx_adap,
  208. struct i2c_msg* msg)
  209. {
  210. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  211. int status;
  212. int rc;
  213. /* avoid writing to my slave address (hangs on 80331),
  214. * forbidden in Intel developer manual
  215. */
  216. if (msg->addr == MYSAR) {
  217. return -EBUSY;
  218. }
  219. __raw_writel(iic_cook_addr(msg), iop3xx_adap->ioaddr + DBR_OFFSET);
  220. cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
  221. cr |= IOP3XX_ICR_MSTART | IOP3XX_ICR_TBYTE;
  222. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  223. rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
  224. return rc;
  225. }
  226. static int
  227. iop3xx_i2c_write_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char byte,
  228. int stop)
  229. {
  230. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  231. int status;
  232. int rc = 0;
  233. __raw_writel(byte, iop3xx_adap->ioaddr + DBR_OFFSET);
  234. cr &= ~IOP3XX_ICR_MSTART;
  235. if (stop) {
  236. cr |= IOP3XX_ICR_MSTOP;
  237. } else {
  238. cr &= ~IOP3XX_ICR_MSTOP;
  239. }
  240. cr |= IOP3XX_ICR_TBYTE;
  241. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  242. rc = iop3xx_i2c_wait_tx_done(iop3xx_adap, &status);
  243. return rc;
  244. }
  245. static int
  246. iop3xx_i2c_read_byte(struct i2c_algo_iop3xx_data *iop3xx_adap, char* byte,
  247. int stop)
  248. {
  249. unsigned long cr = __raw_readl(iop3xx_adap->ioaddr + CR_OFFSET);
  250. int status;
  251. int rc = 0;
  252. cr &= ~IOP3XX_ICR_MSTART;
  253. if (stop) {
  254. cr |= IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK;
  255. } else {
  256. cr &= ~(IOP3XX_ICR_MSTOP | IOP3XX_ICR_NACK);
  257. }
  258. cr |= IOP3XX_ICR_TBYTE;
  259. __raw_writel(cr, iop3xx_adap->ioaddr + CR_OFFSET);
  260. rc = iop3xx_i2c_wait_rx_done(iop3xx_adap, &status);
  261. *byte = __raw_readl(iop3xx_adap->ioaddr + DBR_OFFSET);
  262. return rc;
  263. }
  264. static int
  265. iop3xx_i2c_writebytes(struct i2c_adapter *i2c_adap, const char *buf, int count)
  266. {
  267. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  268. int ii;
  269. int rc = 0;
  270. for (ii = 0; rc == 0 && ii != count; ++ii)
  271. rc = iop3xx_i2c_write_byte(iop3xx_adap, buf[ii], ii==count-1);
  272. return rc;
  273. }
  274. static int
  275. iop3xx_i2c_readbytes(struct i2c_adapter *i2c_adap, char *buf, int count)
  276. {
  277. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  278. int ii;
  279. int rc = 0;
  280. for (ii = 0; rc == 0 && ii != count; ++ii)
  281. rc = iop3xx_i2c_read_byte(iop3xx_adap, &buf[ii], ii==count-1);
  282. return rc;
  283. }
  284. /*
  285. * Description: This function implements combined transactions. Combined
  286. * transactions consist of combinations of reading and writing blocks of data.
  287. * FROM THE SAME ADDRESS
  288. * Each transfer (i.e. a read or a write) is separated by a repeated start
  289. * condition.
  290. */
  291. static int
  292. iop3xx_i2c_handle_msg(struct i2c_adapter *i2c_adap, struct i2c_msg* pmsg)
  293. {
  294. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  295. int rc;
  296. rc = iop3xx_i2c_send_target_addr(iop3xx_adap, pmsg);
  297. if (rc < 0) {
  298. return rc;
  299. }
  300. if ((pmsg->flags&I2C_M_RD)) {
  301. return iop3xx_i2c_readbytes(i2c_adap, pmsg->buf, pmsg->len);
  302. } else {
  303. return iop3xx_i2c_writebytes(i2c_adap, pmsg->buf, pmsg->len);
  304. }
  305. }
  306. /*
  307. * master_xfer() - main read/write entry
  308. */
  309. static int
  310. iop3xx_i2c_master_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs,
  311. int num)
  312. {
  313. struct i2c_algo_iop3xx_data *iop3xx_adap = i2c_adap->algo_data;
  314. int im = 0;
  315. int ret = 0;
  316. int status;
  317. iop3xx_i2c_wait_idle(iop3xx_adap, &status);
  318. iop3xx_i2c_reset(iop3xx_adap);
  319. iop3xx_i2c_enable(iop3xx_adap);
  320. for (im = 0; ret == 0 && im != num; im++) {
  321. ret = iop3xx_i2c_handle_msg(i2c_adap, &msgs[im]);
  322. }
  323. iop3xx_i2c_transaction_cleanup(iop3xx_adap);
  324. if(ret)
  325. return ret;
  326. return im;
  327. }
  328. static u32
  329. iop3xx_i2c_func(struct i2c_adapter *adap)
  330. {
  331. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  332. }
  333. static const struct i2c_algorithm iop3xx_i2c_algo = {
  334. .master_xfer = iop3xx_i2c_master_xfer,
  335. .functionality = iop3xx_i2c_func,
  336. };
  337. static int
  338. iop3xx_i2c_remove(struct platform_device *pdev)
  339. {
  340. struct i2c_adapter *padapter = platform_get_drvdata(pdev);
  341. struct i2c_algo_iop3xx_data *adapter_data =
  342. (struct i2c_algo_iop3xx_data *)padapter->algo_data;
  343. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  344. unsigned long cr = __raw_readl(adapter_data->ioaddr + CR_OFFSET);
  345. /*
  346. * Disable the actual HW unit
  347. */
  348. cr &= ~(IOP3XX_ICR_ALD_IE | IOP3XX_ICR_BERR_IE |
  349. IOP3XX_ICR_RXFULL_IE | IOP3XX_ICR_TXEMPTY_IE);
  350. __raw_writel(cr, adapter_data->ioaddr + CR_OFFSET);
  351. iounmap((void __iomem*)adapter_data->ioaddr);
  352. release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
  353. kfree(adapter_data);
  354. kfree(padapter);
  355. platform_set_drvdata(pdev, NULL);
  356. return 0;
  357. }
  358. static int
  359. iop3xx_i2c_probe(struct platform_device *pdev)
  360. {
  361. struct resource *res;
  362. int ret, irq;
  363. struct i2c_adapter *new_adapter;
  364. struct i2c_algo_iop3xx_data *adapter_data;
  365. new_adapter = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
  366. if (!new_adapter) {
  367. ret = -ENOMEM;
  368. goto out;
  369. }
  370. adapter_data = kzalloc(sizeof(struct i2c_algo_iop3xx_data), GFP_KERNEL);
  371. if (!adapter_data) {
  372. ret = -ENOMEM;
  373. goto free_adapter;
  374. }
  375. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  376. if (!res) {
  377. ret = -ENODEV;
  378. goto free_both;
  379. }
  380. if (!request_mem_region(res->start, IOP3XX_I2C_IO_SIZE, pdev->name)) {
  381. ret = -EBUSY;
  382. goto free_both;
  383. }
  384. /* set the adapter enumeration # */
  385. adapter_data->id = i2c_id++;
  386. adapter_data->ioaddr = (u32)ioremap(res->start, IOP3XX_I2C_IO_SIZE);
  387. if (!adapter_data->ioaddr) {
  388. ret = -ENOMEM;
  389. goto release_region;
  390. }
  391. irq = platform_get_irq(pdev, 0);
  392. if (irq < 0) {
  393. ret = -ENXIO;
  394. goto unmap;
  395. }
  396. ret = request_irq(irq, iop3xx_i2c_irq_handler, 0,
  397. pdev->name, adapter_data);
  398. if (ret) {
  399. ret = -EIO;
  400. goto unmap;
  401. }
  402. memcpy(new_adapter->name, pdev->name, strlen(pdev->name));
  403. new_adapter->id = I2C_HW_IOP3XX;
  404. new_adapter->owner = THIS_MODULE;
  405. new_adapter->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
  406. new_adapter->dev.parent = &pdev->dev;
  407. new_adapter->nr = pdev->id;
  408. /*
  409. * Default values...should these come in from board code?
  410. */
  411. new_adapter->timeout = 100;
  412. new_adapter->algo = &iop3xx_i2c_algo;
  413. init_waitqueue_head(&adapter_data->waitq);
  414. spin_lock_init(&adapter_data->lock);
  415. iop3xx_i2c_reset(adapter_data);
  416. iop3xx_i2c_enable(adapter_data);
  417. platform_set_drvdata(pdev, new_adapter);
  418. new_adapter->algo_data = adapter_data;
  419. i2c_add_numbered_adapter(new_adapter);
  420. return 0;
  421. unmap:
  422. iounmap((void __iomem*)adapter_data->ioaddr);
  423. release_region:
  424. release_mem_region(res->start, IOP3XX_I2C_IO_SIZE);
  425. free_both:
  426. kfree(adapter_data);
  427. free_adapter:
  428. kfree(new_adapter);
  429. out:
  430. return ret;
  431. }
  432. static struct platform_driver iop3xx_i2c_driver = {
  433. .probe = iop3xx_i2c_probe,
  434. .remove = iop3xx_i2c_remove,
  435. .driver = {
  436. .owner = THIS_MODULE,
  437. .name = "IOP3xx-I2C",
  438. },
  439. };
  440. static int __init
  441. i2c_iop3xx_init (void)
  442. {
  443. return platform_driver_register(&iop3xx_i2c_driver);
  444. }
  445. static void __exit
  446. i2c_iop3xx_exit (void)
  447. {
  448. platform_driver_unregister(&iop3xx_i2c_driver);
  449. return;
  450. }
  451. module_init (i2c_iop3xx_init);
  452. module_exit (i2c_iop3xx_exit);
  453. MODULE_AUTHOR("D-TACQ Solutions Ltd <www.d-tacq.com>");
  454. MODULE_DESCRIPTION("IOP3xx iic algorithm and driver");
  455. MODULE_LICENSE("GPL");
  456. MODULE_ALIAS("platform:IOP3xx-I2C");