i2c-mpc.c 9.3 KB

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