i2c-mpc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
  4. * This is a combined i2c adapter and algorithm driver for the
  5. * MPC107/Tsi107 PowerPC northbridge and processors that include
  6. * the same I2C unit (8240, 8245, 85xx).
  7. *
  8. * Release 0.8
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_i2c.h>
  20. #include <asm/io.h>
  21. #include <linux/fsl_devices.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #define DRV_NAME "mpc-i2c"
  26. #define MPC_I2C_FDR 0x04
  27. #define MPC_I2C_CR 0x08
  28. #define MPC_I2C_SR 0x0c
  29. #define MPC_I2C_DR 0x10
  30. #define MPC_I2C_DFSRR 0x14
  31. #define CCR_MEN 0x80
  32. #define CCR_MIEN 0x40
  33. #define CCR_MSTA 0x20
  34. #define CCR_MTX 0x10
  35. #define CCR_TXAK 0x08
  36. #define CCR_RSTA 0x04
  37. #define CSR_MCF 0x80
  38. #define CSR_MAAS 0x40
  39. #define CSR_MBB 0x20
  40. #define CSR_MAL 0x10
  41. #define CSR_SRW 0x04
  42. #define CSR_MIF 0x02
  43. #define CSR_RXAK 0x01
  44. struct mpc_i2c {
  45. void __iomem *base;
  46. u32 interrupt;
  47. wait_queue_head_t queue;
  48. struct i2c_adapter adap;
  49. int irq;
  50. u32 flags;
  51. };
  52. static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
  53. {
  54. writeb(x, i2c->base + MPC_I2C_CR);
  55. }
  56. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id)
  57. {
  58. struct mpc_i2c *i2c = dev_id;
  59. if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
  60. /* Read again to allow register to stabilise */
  61. i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
  62. writeb(0, i2c->base + MPC_I2C_SR);
  63. wake_up_interruptible(&i2c->queue);
  64. }
  65. return IRQ_HANDLED;
  66. }
  67. /* Sometimes 9th clock pulse isn't generated, and slave doesn't release
  68. * the bus, because it wants to send ACK.
  69. * Following sequence of enabling/disabling and sending start/stop generates
  70. * the pulse, so it's all OK.
  71. */
  72. static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  73. {
  74. writeccr(i2c, 0);
  75. udelay(30);
  76. writeccr(i2c, CCR_MEN);
  77. udelay(30);
  78. writeccr(i2c, CCR_MSTA | CCR_MTX);
  79. udelay(30);
  80. writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
  81. udelay(30);
  82. writeccr(i2c, CCR_MEN);
  83. udelay(30);
  84. }
  85. static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
  86. {
  87. unsigned long orig_jiffies = jiffies;
  88. u32 x;
  89. int result = 0;
  90. if (i2c->irq == NO_IRQ)
  91. {
  92. while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
  93. schedule();
  94. if (time_after(jiffies, orig_jiffies + timeout)) {
  95. pr_debug("I2C: timeout\n");
  96. writeccr(i2c, 0);
  97. result = -EIO;
  98. break;
  99. }
  100. }
  101. x = readb(i2c->base + MPC_I2C_SR);
  102. writeb(0, i2c->base + MPC_I2C_SR);
  103. } else {
  104. /* Interrupt mode */
  105. result = wait_event_interruptible_timeout(i2c->queue,
  106. (i2c->interrupt & CSR_MIF), timeout * HZ);
  107. if (unlikely(result < 0)) {
  108. pr_debug("I2C: wait interrupted\n");
  109. writeccr(i2c, 0);
  110. } else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
  111. pr_debug("I2C: wait timeout\n");
  112. writeccr(i2c, 0);
  113. result = -ETIMEDOUT;
  114. }
  115. x = i2c->interrupt;
  116. i2c->interrupt = 0;
  117. }
  118. if (result < 0)
  119. return result;
  120. if (!(x & CSR_MCF)) {
  121. pr_debug("I2C: unfinished\n");
  122. return -EIO;
  123. }
  124. if (x & CSR_MAL) {
  125. pr_debug("I2C: MAL\n");
  126. return -EIO;
  127. }
  128. if (writing && (x & CSR_RXAK)) {
  129. pr_debug("I2C: No RXAK\n");
  130. /* generate stop */
  131. writeccr(i2c, CCR_MEN);
  132. return -EIO;
  133. }
  134. return 0;
  135. }
  136. static void mpc_i2c_setclock(struct mpc_i2c *i2c)
  137. {
  138. /* Set clock and filters */
  139. if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
  140. writeb(0x31, i2c->base + MPC_I2C_FDR);
  141. writeb(0x10, i2c->base + MPC_I2C_DFSRR);
  142. } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
  143. writeb(0x3f, i2c->base + MPC_I2C_FDR);
  144. else
  145. writel(0x1031, i2c->base + MPC_I2C_FDR);
  146. }
  147. static void mpc_i2c_start(struct mpc_i2c *i2c)
  148. {
  149. /* Clear arbitration */
  150. writeb(0, i2c->base + MPC_I2C_SR);
  151. /* Start with MEN */
  152. writeccr(i2c, CCR_MEN);
  153. }
  154. static void mpc_i2c_stop(struct mpc_i2c *i2c)
  155. {
  156. writeccr(i2c, CCR_MEN);
  157. }
  158. static int mpc_write(struct mpc_i2c *i2c, int target,
  159. const u8 * data, int length, int restart)
  160. {
  161. int i, result;
  162. unsigned timeout = i2c->adap.timeout;
  163. u32 flags = restart ? CCR_RSTA : 0;
  164. /* Start with MEN */
  165. if (!restart)
  166. writeccr(i2c, CCR_MEN);
  167. /* Start as master */
  168. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  169. /* Write target byte */
  170. writeb((target << 1), i2c->base + MPC_I2C_DR);
  171. result = i2c_wait(i2c, timeout, 1);
  172. if (result < 0)
  173. return result;
  174. for (i = 0; i < length; i++) {
  175. /* Write data byte */
  176. writeb(data[i], i2c->base + MPC_I2C_DR);
  177. result = i2c_wait(i2c, timeout, 1);
  178. if (result < 0)
  179. return result;
  180. }
  181. return 0;
  182. }
  183. static int mpc_read(struct mpc_i2c *i2c, int target,
  184. u8 * data, int length, int restart)
  185. {
  186. unsigned timeout = i2c->adap.timeout;
  187. int i, result;
  188. u32 flags = restart ? CCR_RSTA : 0;
  189. /* Start with MEN */
  190. if (!restart)
  191. writeccr(i2c, CCR_MEN);
  192. /* Switch to read - restart */
  193. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  194. /* Write target address byte - this time with the read flag set */
  195. writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
  196. result = i2c_wait(i2c, timeout, 1);
  197. if (result < 0)
  198. return result;
  199. if (length) {
  200. if (length == 1)
  201. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  202. else
  203. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
  204. /* Dummy read */
  205. readb(i2c->base + MPC_I2C_DR);
  206. }
  207. for (i = 0; i < length; i++) {
  208. result = i2c_wait(i2c, timeout, 0);
  209. if (result < 0)
  210. return result;
  211. /* Generate txack on next to last byte */
  212. if (i == length - 2)
  213. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  214. /* Generate stop on last byte */
  215. if (i == length - 1)
  216. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
  217. data[i] = readb(i2c->base + MPC_I2C_DR);
  218. }
  219. return length;
  220. }
  221. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  222. {
  223. struct i2c_msg *pmsg;
  224. int i;
  225. int ret = 0;
  226. unsigned long orig_jiffies = jiffies;
  227. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  228. mpc_i2c_start(i2c);
  229. /* Allow bus up to 1s to become not busy */
  230. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  231. if (signal_pending(current)) {
  232. pr_debug("I2C: Interrupted\n");
  233. writeccr(i2c, 0);
  234. return -EINTR;
  235. }
  236. if (time_after(jiffies, orig_jiffies + HZ)) {
  237. pr_debug("I2C: timeout\n");
  238. if (readb(i2c->base + MPC_I2C_SR) ==
  239. (CSR_MCF | CSR_MBB | CSR_RXAK))
  240. mpc_i2c_fixup(i2c);
  241. return -EIO;
  242. }
  243. schedule();
  244. }
  245. for (i = 0; ret >= 0 && i < num; i++) {
  246. pmsg = &msgs[i];
  247. pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  248. pmsg->flags & I2C_M_RD ? "read" : "write",
  249. pmsg->len, pmsg->addr, i + 1, num);
  250. if (pmsg->flags & I2C_M_RD)
  251. ret =
  252. mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  253. else
  254. ret =
  255. mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  256. }
  257. mpc_i2c_stop(i2c);
  258. return (ret < 0) ? ret : num;
  259. }
  260. static u32 mpc_functionality(struct i2c_adapter *adap)
  261. {
  262. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  263. }
  264. static const struct i2c_algorithm mpc_algo = {
  265. .master_xfer = mpc_xfer,
  266. .functionality = mpc_functionality,
  267. };
  268. static struct i2c_adapter mpc_ops = {
  269. .owner = THIS_MODULE,
  270. .name = "MPC adapter",
  271. .id = I2C_HW_MPC107,
  272. .algo = &mpc_algo,
  273. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  274. .timeout = 1,
  275. };
  276. static int __devinit fsl_i2c_probe(struct of_device *op, const struct of_device_id *match)
  277. {
  278. int result = 0;
  279. struct mpc_i2c *i2c;
  280. i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
  281. if (!i2c)
  282. return -ENOMEM;
  283. if (of_get_property(op->node, "dfsrr", NULL))
  284. i2c->flags |= FSL_I2C_DEV_SEPARATE_DFSRR;
  285. if (of_device_is_compatible(op->node, "fsl,mpc5200-i2c") ||
  286. of_device_is_compatible(op->node, "mpc5200-i2c"))
  287. i2c->flags |= FSL_I2C_DEV_CLOCK_5200;
  288. init_waitqueue_head(&i2c->queue);
  289. i2c->base = of_iomap(op->node, 0);
  290. if (!i2c->base) {
  291. printk(KERN_ERR "i2c-mpc - failed to map controller\n");
  292. result = -ENOMEM;
  293. goto fail_map;
  294. }
  295. i2c->irq = irq_of_parse_and_map(op->node, 0);
  296. if (i2c->irq != NO_IRQ) { /* i2c->irq = NO_IRQ implies polling */
  297. result = request_irq(i2c->irq, mpc_i2c_isr,
  298. IRQF_SHARED, "i2c-mpc", i2c);
  299. if (result < 0) {
  300. printk(KERN_ERR "i2c-mpc - failed to attach interrupt\n");
  301. goto fail_request;
  302. }
  303. }
  304. mpc_i2c_setclock(i2c);
  305. dev_set_drvdata(&op->dev, i2c);
  306. i2c->adap = mpc_ops;
  307. i2c_set_adapdata(&i2c->adap, i2c);
  308. i2c->adap.dev.parent = &op->dev;
  309. result = i2c_add_adapter(&i2c->adap);
  310. if (result < 0) {
  311. printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
  312. goto fail_add;
  313. }
  314. of_register_i2c_devices(&i2c->adap, op->node);
  315. return result;
  316. fail_add:
  317. dev_set_drvdata(&op->dev, NULL);
  318. free_irq(i2c->irq, i2c);
  319. fail_request:
  320. irq_dispose_mapping(i2c->irq);
  321. iounmap(i2c->base);
  322. fail_map:
  323. kfree(i2c);
  324. return result;
  325. };
  326. static int __devexit fsl_i2c_remove(struct of_device *op)
  327. {
  328. struct mpc_i2c *i2c = dev_get_drvdata(&op->dev);
  329. i2c_del_adapter(&i2c->adap);
  330. dev_set_drvdata(&op->dev, NULL);
  331. if (i2c->irq != NO_IRQ)
  332. free_irq(i2c->irq, i2c);
  333. irq_dispose_mapping(i2c->irq);
  334. iounmap(i2c->base);
  335. kfree(i2c);
  336. return 0;
  337. };
  338. static const struct of_device_id mpc_i2c_of_match[] = {
  339. {.compatible = "fsl-i2c",},
  340. {},
  341. };
  342. MODULE_DEVICE_TABLE(of, mpc_i2c_of_match);
  343. /* Structure for a device driver */
  344. static struct of_platform_driver mpc_i2c_driver = {
  345. .match_table = mpc_i2c_of_match,
  346. .probe = fsl_i2c_probe,
  347. .remove = __devexit_p(fsl_i2c_remove),
  348. .driver = {
  349. .owner = THIS_MODULE,
  350. .name = DRV_NAME,
  351. },
  352. };
  353. static int __init fsl_i2c_init(void)
  354. {
  355. int rv;
  356. rv = of_register_platform_driver(&mpc_i2c_driver);
  357. if (rv)
  358. printk(KERN_ERR DRV_NAME
  359. " of_register_platform_driver failed (%i)\n", rv);
  360. return rv;
  361. }
  362. static void __exit fsl_i2c_exit(void)
  363. {
  364. of_unregister_platform_driver(&mpc_i2c_driver);
  365. }
  366. module_init(fsl_i2c_init);
  367. module_exit(fsl_i2c_exit);
  368. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  369. MODULE_DESCRIPTION
  370. ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
  371. MODULE_LICENSE("GPL");