i2c-mpc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/sched.h>
  18. #include <linux/init.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <asm/io.h>
  22. #include <linux/fsl_devices.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #define MPC_I2C_ADDR 0x00
  27. #define MPC_I2C_FDR 0x04
  28. #define MPC_I2C_CR 0x08
  29. #define MPC_I2C_SR 0x0c
  30. #define MPC_I2C_DR 0x10
  31. #define MPC_I2C_DFSRR 0x14
  32. #define MPC_I2C_REGION 0x20
  33. #define CCR_MEN 0x80
  34. #define CCR_MIEN 0x40
  35. #define CCR_MSTA 0x20
  36. #define CCR_MTX 0x10
  37. #define CCR_TXAK 0x08
  38. #define CCR_RSTA 0x04
  39. #define CSR_MCF 0x80
  40. #define CSR_MAAS 0x40
  41. #define CSR_MBB 0x20
  42. #define CSR_MAL 0x10
  43. #define CSR_SRW 0x04
  44. #define CSR_MIF 0x02
  45. #define CSR_RXAK 0x01
  46. struct mpc_i2c {
  47. void __iomem *base;
  48. u32 interrupt;
  49. wait_queue_head_t queue;
  50. struct i2c_adapter adap;
  51. int irq;
  52. u32 flags;
  53. };
  54. static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
  55. {
  56. writeb(x, i2c->base + MPC_I2C_CR);
  57. }
  58. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
  59. {
  60. struct mpc_i2c *i2c = dev_id;
  61. if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
  62. /* Read again to allow register to stabilise */
  63. i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
  64. writeb(0, i2c->base + MPC_I2C_SR);
  65. wake_up_interruptible(&i2c->queue);
  66. }
  67. return IRQ_HANDLED;
  68. }
  69. static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
  70. {
  71. unsigned long orig_jiffies = jiffies;
  72. u32 x;
  73. int result = 0;
  74. if (i2c->irq == 0)
  75. {
  76. while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
  77. schedule();
  78. if (time_after(jiffies, orig_jiffies + timeout)) {
  79. pr_debug("I2C: timeout\n");
  80. result = -EIO;
  81. break;
  82. }
  83. }
  84. x = readb(i2c->base + MPC_I2C_SR);
  85. writeb(0, i2c->base + MPC_I2C_SR);
  86. } else {
  87. /* Interrupt mode */
  88. result = wait_event_interruptible_timeout(i2c->queue,
  89. (i2c->interrupt & CSR_MIF), timeout * HZ);
  90. if (unlikely(result < 0))
  91. pr_debug("I2C: wait interrupted\n");
  92. else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
  93. pr_debug("I2C: wait timeout\n");
  94. result = -ETIMEDOUT;
  95. }
  96. x = i2c->interrupt;
  97. i2c->interrupt = 0;
  98. }
  99. if (result < 0)
  100. return result;
  101. if (!(x & CSR_MCF)) {
  102. pr_debug("I2C: unfinished\n");
  103. return -EIO;
  104. }
  105. if (x & CSR_MAL) {
  106. pr_debug("I2C: MAL\n");
  107. return -EIO;
  108. }
  109. if (writing && (x & CSR_RXAK)) {
  110. pr_debug("I2C: No RXAK\n");
  111. /* generate stop */
  112. writeccr(i2c, CCR_MEN);
  113. return -EIO;
  114. }
  115. return 0;
  116. }
  117. static void mpc_i2c_setclock(struct mpc_i2c *i2c)
  118. {
  119. /* Set clock and filters */
  120. if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
  121. writeb(0x31, i2c->base + MPC_I2C_FDR);
  122. writeb(0x10, i2c->base + MPC_I2C_DFSRR);
  123. } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
  124. writeb(0x3f, i2c->base + MPC_I2C_FDR);
  125. else
  126. writel(0x1031, i2c->base + MPC_I2C_FDR);
  127. }
  128. static void mpc_i2c_start(struct mpc_i2c *i2c)
  129. {
  130. /* Clear arbitration */
  131. writeb(0, i2c->base + MPC_I2C_SR);
  132. /* Start with MEN */
  133. writeccr(i2c, CCR_MEN);
  134. }
  135. static void mpc_i2c_stop(struct mpc_i2c *i2c)
  136. {
  137. writeccr(i2c, CCR_MEN);
  138. }
  139. static int mpc_write(struct mpc_i2c *i2c, int target,
  140. const u8 * data, int length, int restart)
  141. {
  142. int i;
  143. unsigned timeout = i2c->adap.timeout;
  144. u32 flags = restart ? CCR_RSTA : 0;
  145. /* Start with MEN */
  146. if (!restart)
  147. writeccr(i2c, CCR_MEN);
  148. /* Start as master */
  149. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  150. /* Write target byte */
  151. writeb((target << 1), i2c->base + MPC_I2C_DR);
  152. if (i2c_wait(i2c, timeout, 1) < 0)
  153. return -1;
  154. for (i = 0; i < length; i++) {
  155. /* Write data byte */
  156. writeb(data[i], i2c->base + MPC_I2C_DR);
  157. if (i2c_wait(i2c, timeout, 1) < 0)
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. static int mpc_read(struct mpc_i2c *i2c, int target,
  163. u8 * data, int length, int restart)
  164. {
  165. unsigned timeout = i2c->adap.timeout;
  166. int i;
  167. u32 flags = restart ? CCR_RSTA : 0;
  168. /* Start with MEN */
  169. if (!restart)
  170. writeccr(i2c, CCR_MEN);
  171. /* Switch to read - restart */
  172. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  173. /* Write target address byte - this time with the read flag set */
  174. writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
  175. if (i2c_wait(i2c, timeout, 1) < 0)
  176. return -1;
  177. if (length) {
  178. if (length == 1)
  179. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  180. else
  181. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
  182. /* Dummy read */
  183. readb(i2c->base + MPC_I2C_DR);
  184. }
  185. for (i = 0; i < length; i++) {
  186. if (i2c_wait(i2c, timeout, 0) < 0)
  187. return -1;
  188. /* Generate txack on next to last byte */
  189. if (i == length - 2)
  190. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  191. /* Generate stop on last byte */
  192. if (i == length - 1)
  193. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
  194. data[i] = readb(i2c->base + MPC_I2C_DR);
  195. }
  196. return length;
  197. }
  198. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  199. {
  200. struct i2c_msg *pmsg;
  201. int i;
  202. int ret = 0;
  203. unsigned long orig_jiffies = jiffies;
  204. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  205. mpc_i2c_start(i2c);
  206. /* Allow bus up to 1s to become not busy */
  207. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  208. if (signal_pending(current)) {
  209. pr_debug("I2C: Interrupted\n");
  210. return -EINTR;
  211. }
  212. if (time_after(jiffies, orig_jiffies + HZ)) {
  213. pr_debug("I2C: timeout\n");
  214. return -EIO;
  215. }
  216. schedule();
  217. }
  218. for (i = 0; ret >= 0 && i < num; i++) {
  219. pmsg = &msgs[i];
  220. pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  221. pmsg->flags & I2C_M_RD ? "read" : "write",
  222. pmsg->len, pmsg->addr, i + 1, num);
  223. if (pmsg->flags & I2C_M_RD)
  224. ret =
  225. mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  226. else
  227. ret =
  228. mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  229. }
  230. mpc_i2c_stop(i2c);
  231. return (ret < 0) ? ret : num;
  232. }
  233. static u32 mpc_functionality(struct i2c_adapter *adap)
  234. {
  235. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  236. }
  237. static struct i2c_algorithm mpc_algo = {
  238. .master_xfer = mpc_xfer,
  239. .functionality = mpc_functionality,
  240. };
  241. static struct i2c_adapter mpc_ops = {
  242. .owner = THIS_MODULE,
  243. .name = "MPC adapter",
  244. .id = I2C_HW_MPC107,
  245. .algo = &mpc_algo,
  246. .class = I2C_CLASS_HWMON,
  247. .timeout = 1,
  248. .retries = 1
  249. };
  250. static int fsl_i2c_probe(struct platform_device *pdev)
  251. {
  252. int result = 0;
  253. struct mpc_i2c *i2c;
  254. struct fsl_i2c_platform_data *pdata;
  255. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  256. pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
  257. if (!(i2c = kzalloc(sizeof(*i2c), GFP_KERNEL))) {
  258. return -ENOMEM;
  259. }
  260. i2c->irq = platform_get_irq(pdev, 0);
  261. if (i2c->irq < 0) {
  262. result = -ENXIO;
  263. goto fail_get_irq;
  264. }
  265. i2c->flags = pdata->device_flags;
  266. init_waitqueue_head(&i2c->queue);
  267. i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
  268. if (!i2c->base) {
  269. printk(KERN_ERR "i2c-mpc - failed to map controller\n");
  270. result = -ENOMEM;
  271. goto fail_map;
  272. }
  273. if (i2c->irq != 0)
  274. if ((result = request_irq(i2c->irq, mpc_i2c_isr,
  275. SA_SHIRQ, "i2c-mpc", i2c)) < 0) {
  276. printk(KERN_ERR
  277. "i2c-mpc - failed to attach interrupt\n");
  278. goto fail_irq;
  279. }
  280. mpc_i2c_setclock(i2c);
  281. platform_set_drvdata(pdev, i2c);
  282. i2c->adap = mpc_ops;
  283. i2c_set_adapdata(&i2c->adap, i2c);
  284. i2c->adap.dev.parent = &pdev->dev;
  285. if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
  286. printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
  287. goto fail_add;
  288. }
  289. return result;
  290. fail_add:
  291. if (i2c->irq != 0)
  292. free_irq(i2c->irq, NULL);
  293. fail_irq:
  294. iounmap(i2c->base);
  295. fail_map:
  296. fail_get_irq:
  297. kfree(i2c);
  298. return result;
  299. };
  300. static int fsl_i2c_remove(struct platform_device *pdev)
  301. {
  302. struct mpc_i2c *i2c = platform_get_drvdata(pdev);
  303. i2c_del_adapter(&i2c->adap);
  304. platform_set_drvdata(pdev, NULL);
  305. if (i2c->irq != 0)
  306. free_irq(i2c->irq, i2c);
  307. iounmap(i2c->base);
  308. kfree(i2c);
  309. return 0;
  310. };
  311. /* Structure for a device driver */
  312. static struct platform_driver fsl_i2c_driver = {
  313. .probe = fsl_i2c_probe,
  314. .remove = fsl_i2c_remove,
  315. .driver = {
  316. .owner = THIS_MODULE,
  317. .name = "fsl-i2c",
  318. },
  319. };
  320. static int __init fsl_i2c_init(void)
  321. {
  322. return platform_driver_register(&fsl_i2c_driver);
  323. }
  324. static void __exit fsl_i2c_exit(void)
  325. {
  326. platform_driver_unregister(&fsl_i2c_driver);
  327. }
  328. module_init(fsl_i2c_init);
  329. module_exit(fsl_i2c_exit);
  330. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  331. MODULE_DESCRIPTION
  332. ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
  333. MODULE_LICENSE("GPL");