i2c-mpc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <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 MPC_I2C_ADDR 0x00
  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 MPC_I2C_REGION 0x20
  32. #define CCR_MEN 0x80
  33. #define CCR_MIEN 0x40
  34. #define CCR_MSTA 0x20
  35. #define CCR_MTX 0x10
  36. #define CCR_TXAK 0x08
  37. #define CCR_RSTA 0x04
  38. #define CSR_MCF 0x80
  39. #define CSR_MAAS 0x40
  40. #define CSR_MBB 0x20
  41. #define CSR_MAL 0x10
  42. #define CSR_SRW 0x04
  43. #define CSR_MIF 0x02
  44. #define CSR_RXAK 0x01
  45. struct mpc_i2c {
  46. void __iomem *base;
  47. u32 interrupt;
  48. wait_queue_head_t queue;
  49. struct i2c_adapter adap;
  50. int irq;
  51. u32 flags;
  52. };
  53. static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
  54. {
  55. writeb(x, i2c->base + MPC_I2C_CR);
  56. }
  57. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
  58. {
  59. struct mpc_i2c *i2c = dev_id;
  60. if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
  61. /* Read again to allow register to stabilise */
  62. i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
  63. writeb(0, i2c->base + MPC_I2C_SR);
  64. wake_up_interruptible(&i2c->queue);
  65. }
  66. return IRQ_HANDLED;
  67. }
  68. static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
  69. {
  70. unsigned long orig_jiffies = jiffies;
  71. u32 x;
  72. int result = 0;
  73. if (i2c->irq == 0)
  74. {
  75. while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
  76. schedule();
  77. if (time_after(jiffies, orig_jiffies + timeout)) {
  78. pr_debug("I2C: timeout\n");
  79. result = -EIO;
  80. break;
  81. }
  82. }
  83. x = readb(i2c->base + MPC_I2C_SR);
  84. writeb(0, i2c->base + MPC_I2C_SR);
  85. } else {
  86. /* Interrupt mode */
  87. result = wait_event_interruptible_timeout(i2c->queue,
  88. (i2c->interrupt & CSR_MIF), timeout * HZ);
  89. if (unlikely(result < 0))
  90. pr_debug("I2C: wait interrupted\n");
  91. else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
  92. pr_debug("I2C: wait timeout\n");
  93. result = -ETIMEDOUT;
  94. }
  95. x = i2c->interrupt;
  96. i2c->interrupt = 0;
  97. }
  98. if (result < 0)
  99. return result;
  100. if (!(x & CSR_MCF)) {
  101. pr_debug("I2C: unfinished\n");
  102. return -EIO;
  103. }
  104. if (x & CSR_MAL) {
  105. pr_debug("I2C: MAL\n");
  106. return -EIO;
  107. }
  108. if (writing && (x & CSR_RXAK)) {
  109. pr_debug("I2C: No RXAK\n");
  110. /* generate stop */
  111. writeccr(i2c, CCR_MEN);
  112. return -EIO;
  113. }
  114. return 0;
  115. }
  116. static void mpc_i2c_setclock(struct mpc_i2c *i2c)
  117. {
  118. /* Set clock and filters */
  119. if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
  120. writeb(0x31, i2c->base + MPC_I2C_FDR);
  121. writeb(0x10, i2c->base + MPC_I2C_DFSRR);
  122. } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
  123. writeb(0x3f, i2c->base + MPC_I2C_FDR);
  124. else
  125. writel(0x1031, i2c->base + MPC_I2C_FDR);
  126. }
  127. static void mpc_i2c_start(struct mpc_i2c *i2c)
  128. {
  129. /* Clear arbitration */
  130. writeb(0, i2c->base + MPC_I2C_SR);
  131. /* Start with MEN */
  132. writeccr(i2c, CCR_MEN);
  133. }
  134. static void mpc_i2c_stop(struct mpc_i2c *i2c)
  135. {
  136. writeccr(i2c, CCR_MEN);
  137. }
  138. static int mpc_write(struct mpc_i2c *i2c, int target,
  139. const u8 * data, int length, int restart)
  140. {
  141. int i;
  142. unsigned timeout = i2c->adap.timeout;
  143. u32 flags = restart ? CCR_RSTA : 0;
  144. /* Start with MEN */
  145. if (!restart)
  146. writeccr(i2c, CCR_MEN);
  147. /* Start as master */
  148. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  149. /* Write target byte */
  150. writeb((target << 1), i2c->base + MPC_I2C_DR);
  151. if (i2c_wait(i2c, timeout, 1) < 0)
  152. return -1;
  153. for (i = 0; i < length; i++) {
  154. /* Write data byte */
  155. writeb(data[i], i2c->base + MPC_I2C_DR);
  156. if (i2c_wait(i2c, timeout, 1) < 0)
  157. return -1;
  158. }
  159. return 0;
  160. }
  161. static int mpc_read(struct mpc_i2c *i2c, int target,
  162. u8 * data, int length, int restart)
  163. {
  164. unsigned timeout = i2c->adap.timeout;
  165. int i;
  166. u32 flags = restart ? CCR_RSTA : 0;
  167. /* Start with MEN */
  168. if (!restart)
  169. writeccr(i2c, CCR_MEN);
  170. /* Switch to read - restart */
  171. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  172. /* Write target address byte - this time with the read flag set */
  173. writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
  174. if (i2c_wait(i2c, timeout, 1) < 0)
  175. return -1;
  176. if (length) {
  177. if (length == 1)
  178. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  179. else
  180. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
  181. /* Dummy read */
  182. readb(i2c->base + MPC_I2C_DR);
  183. }
  184. for (i = 0; i < length; i++) {
  185. if (i2c_wait(i2c, timeout, 0) < 0)
  186. return -1;
  187. /* Generate txack on next to last byte */
  188. if (i == length - 2)
  189. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  190. /* Generate stop on last byte */
  191. if (i == length - 1)
  192. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
  193. data[i] = readb(i2c->base + MPC_I2C_DR);
  194. }
  195. return length;
  196. }
  197. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  198. {
  199. struct i2c_msg *pmsg;
  200. int i;
  201. int ret = 0;
  202. unsigned long orig_jiffies = jiffies;
  203. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  204. mpc_i2c_start(i2c);
  205. /* Allow bus up to 1s to become not busy */
  206. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  207. if (signal_pending(current)) {
  208. pr_debug("I2C: Interrupted\n");
  209. return -EINTR;
  210. }
  211. if (time_after(jiffies, orig_jiffies + HZ)) {
  212. pr_debug("I2C: timeout\n");
  213. return -EIO;
  214. }
  215. schedule();
  216. }
  217. for (i = 0; ret >= 0 && i < num; i++) {
  218. pmsg = &msgs[i];
  219. pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  220. pmsg->flags & I2C_M_RD ? "read" : "write",
  221. pmsg->len, pmsg->addr, i + 1, num);
  222. if (pmsg->flags & I2C_M_RD)
  223. ret =
  224. mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  225. else
  226. ret =
  227. mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  228. }
  229. mpc_i2c_stop(i2c);
  230. return (ret < 0) ? ret : num;
  231. }
  232. static u32 mpc_functionality(struct i2c_adapter *adap)
  233. {
  234. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  235. }
  236. static struct i2c_algorithm mpc_algo = {
  237. .name = "MPC algorithm",
  238. .id = I2C_ALGO_MPC107,
  239. .master_xfer = mpc_xfer,
  240. .functionality = mpc_functionality,
  241. };
  242. static struct i2c_adapter mpc_ops = {
  243. .owner = THIS_MODULE,
  244. .name = "MPC adapter",
  245. .id = I2C_ALGO_MPC107 | I2C_HW_MPC107,
  246. .algo = &mpc_algo,
  247. .class = I2C_CLASS_HWMON,
  248. .timeout = 1,
  249. .retries = 1
  250. };
  251. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  252. MODULE_DESCRIPTION
  253. ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
  254. MODULE_LICENSE("GPL");