i2c-mpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #ifdef CONFIG_FSL_OCP
  22. #include <asm/ocp.h>
  23. #define FSL_I2C_DEV_SEPARATE_DFSRR FS_I2C_SEPARATE_DFSRR
  24. #define FSL_I2C_DEV_CLOCK_5200 FS_I2C_CLOCK_5200
  25. #else
  26. #include <linux/fsl_devices.h>
  27. #endif
  28. #include <linux/i2c.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #define MPC_I2C_ADDR 0x00
  32. #define MPC_I2C_FDR 0x04
  33. #define MPC_I2C_CR 0x08
  34. #define MPC_I2C_SR 0x0c
  35. #define MPC_I2C_DR 0x10
  36. #define MPC_I2C_DFSRR 0x14
  37. #define MPC_I2C_REGION 0x20
  38. #define CCR_MEN 0x80
  39. #define CCR_MIEN 0x40
  40. #define CCR_MSTA 0x20
  41. #define CCR_MTX 0x10
  42. #define CCR_TXAK 0x08
  43. #define CCR_RSTA 0x04
  44. #define CSR_MCF 0x80
  45. #define CSR_MAAS 0x40
  46. #define CSR_MBB 0x20
  47. #define CSR_MAL 0x10
  48. #define CSR_SRW 0x04
  49. #define CSR_MIF 0x02
  50. #define CSR_RXAK 0x01
  51. struct mpc_i2c {
  52. void __iomem *base;
  53. u32 interrupt;
  54. wait_queue_head_t queue;
  55. struct i2c_adapter adap;
  56. int irq;
  57. u32 flags;
  58. };
  59. static __inline__ void writeccr(struct mpc_i2c *i2c, u32 x)
  60. {
  61. writeb(x, i2c->base + MPC_I2C_CR);
  62. }
  63. static irqreturn_t mpc_i2c_isr(int irq, void *dev_id, struct pt_regs *regs)
  64. {
  65. struct mpc_i2c *i2c = dev_id;
  66. if (readb(i2c->base + MPC_I2C_SR) & CSR_MIF) {
  67. /* Read again to allow register to stabilise */
  68. i2c->interrupt = readb(i2c->base + MPC_I2C_SR);
  69. writeb(0, i2c->base + MPC_I2C_SR);
  70. wake_up_interruptible(&i2c->queue);
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
  75. {
  76. unsigned long orig_jiffies = jiffies;
  77. u32 x;
  78. int result = 0;
  79. if (i2c->irq == 0)
  80. {
  81. while (!(readb(i2c->base + MPC_I2C_SR) & CSR_MIF)) {
  82. schedule();
  83. if (time_after(jiffies, orig_jiffies + timeout)) {
  84. pr_debug("I2C: timeout\n");
  85. result = -EIO;
  86. break;
  87. }
  88. }
  89. x = readb(i2c->base + MPC_I2C_SR);
  90. writeb(0, i2c->base + MPC_I2C_SR);
  91. } else {
  92. /* Interrupt mode */
  93. result = wait_event_interruptible_timeout(i2c->queue,
  94. (i2c->interrupt & CSR_MIF), timeout * HZ);
  95. if (unlikely(result < 0))
  96. pr_debug("I2C: wait interrupted\n");
  97. else if (unlikely(!(i2c->interrupt & CSR_MIF))) {
  98. pr_debug("I2C: wait timeout\n");
  99. result = -ETIMEDOUT;
  100. }
  101. x = i2c->interrupt;
  102. i2c->interrupt = 0;
  103. }
  104. if (result < 0)
  105. return result;
  106. if (!(x & CSR_MCF)) {
  107. pr_debug("I2C: unfinished\n");
  108. return -EIO;
  109. }
  110. if (x & CSR_MAL) {
  111. pr_debug("I2C: MAL\n");
  112. return -EIO;
  113. }
  114. if (writing && (x & CSR_RXAK)) {
  115. pr_debug("I2C: No RXAK\n");
  116. /* generate stop */
  117. writeccr(i2c, CCR_MEN);
  118. return -EIO;
  119. }
  120. return 0;
  121. }
  122. static void mpc_i2c_setclock(struct mpc_i2c *i2c)
  123. {
  124. /* Set clock and filters */
  125. if (i2c->flags & FSL_I2C_DEV_SEPARATE_DFSRR) {
  126. writeb(0x31, i2c->base + MPC_I2C_FDR);
  127. writeb(0x10, i2c->base + MPC_I2C_DFSRR);
  128. } else if (i2c->flags & FSL_I2C_DEV_CLOCK_5200)
  129. writeb(0x3f, i2c->base + MPC_I2C_FDR);
  130. else
  131. writel(0x1031, i2c->base + MPC_I2C_FDR);
  132. }
  133. static void mpc_i2c_start(struct mpc_i2c *i2c)
  134. {
  135. /* Clear arbitration */
  136. writeb(0, i2c->base + MPC_I2C_SR);
  137. /* Start with MEN */
  138. writeccr(i2c, CCR_MEN);
  139. }
  140. static void mpc_i2c_stop(struct mpc_i2c *i2c)
  141. {
  142. writeccr(i2c, CCR_MEN);
  143. }
  144. static int mpc_write(struct mpc_i2c *i2c, int target,
  145. const u8 * data, int length, int restart)
  146. {
  147. int i;
  148. unsigned timeout = i2c->adap.timeout;
  149. u32 flags = restart ? CCR_RSTA : 0;
  150. /* Start with MEN */
  151. if (!restart)
  152. writeccr(i2c, CCR_MEN);
  153. /* Start as master */
  154. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  155. /* Write target byte */
  156. writeb((target << 1), i2c->base + MPC_I2C_DR);
  157. if (i2c_wait(i2c, timeout, 1) < 0)
  158. return -1;
  159. for (i = 0; i < length; i++) {
  160. /* Write data byte */
  161. writeb(data[i], i2c->base + MPC_I2C_DR);
  162. if (i2c_wait(i2c, timeout, 1) < 0)
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. static int mpc_read(struct mpc_i2c *i2c, int target,
  168. u8 * data, int length, int restart)
  169. {
  170. unsigned timeout = i2c->adap.timeout;
  171. int i;
  172. u32 flags = restart ? CCR_RSTA : 0;
  173. /* Start with MEN */
  174. if (!restart)
  175. writeccr(i2c, CCR_MEN);
  176. /* Switch to read - restart */
  177. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_MTX | flags);
  178. /* Write target address byte - this time with the read flag set */
  179. writeb((target << 1) | 1, i2c->base + MPC_I2C_DR);
  180. if (i2c_wait(i2c, timeout, 1) < 0)
  181. return -1;
  182. if (length) {
  183. if (length == 1)
  184. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  185. else
  186. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA);
  187. /* Dummy read */
  188. readb(i2c->base + MPC_I2C_DR);
  189. }
  190. for (i = 0; i < length; i++) {
  191. if (i2c_wait(i2c, timeout, 0) < 0)
  192. return -1;
  193. /* Generate txack on next to last byte */
  194. if (i == length - 2)
  195. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_MSTA | CCR_TXAK);
  196. /* Generate stop on last byte */
  197. if (i == length - 1)
  198. writeccr(i2c, CCR_MIEN | CCR_MEN | CCR_TXAK);
  199. data[i] = readb(i2c->base + MPC_I2C_DR);
  200. }
  201. return length;
  202. }
  203. static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  204. {
  205. struct i2c_msg *pmsg;
  206. int i;
  207. int ret = 0;
  208. unsigned long orig_jiffies = jiffies;
  209. struct mpc_i2c *i2c = i2c_get_adapdata(adap);
  210. mpc_i2c_start(i2c);
  211. /* Allow bus up to 1s to become not busy */
  212. while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
  213. if (signal_pending(current)) {
  214. pr_debug("I2C: Interrupted\n");
  215. return -EINTR;
  216. }
  217. if (time_after(jiffies, orig_jiffies + HZ)) {
  218. pr_debug("I2C: timeout\n");
  219. return -EIO;
  220. }
  221. schedule();
  222. }
  223. for (i = 0; ret >= 0 && i < num; i++) {
  224. pmsg = &msgs[i];
  225. pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
  226. pmsg->flags & I2C_M_RD ? "read" : "write",
  227. pmsg->len, pmsg->addr, i + 1, num);
  228. if (pmsg->flags & I2C_M_RD)
  229. ret =
  230. mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  231. else
  232. ret =
  233. mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
  234. }
  235. mpc_i2c_stop(i2c);
  236. return (ret < 0) ? ret : num;
  237. }
  238. static u32 mpc_functionality(struct i2c_adapter *adap)
  239. {
  240. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  241. }
  242. static struct i2c_algorithm mpc_algo = {
  243. .name = "MPC algorithm",
  244. .id = I2C_ALGO_MPC107,
  245. .master_xfer = mpc_xfer,
  246. .functionality = mpc_functionality,
  247. };
  248. static struct i2c_adapter mpc_ops = {
  249. .owner = THIS_MODULE,
  250. .name = "MPC adapter",
  251. .id = I2C_ALGO_MPC107 | I2C_HW_MPC107,
  252. .algo = &mpc_algo,
  253. .class = I2C_CLASS_HWMON,
  254. .timeout = 1,
  255. .retries = 1
  256. };
  257. #ifdef CONFIG_FSL_OCP
  258. static int __devinit mpc_i2c_probe(struct ocp_device *ocp)
  259. {
  260. int result = 0;
  261. struct mpc_i2c *i2c;
  262. if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
  263. return -ENOMEM;
  264. }
  265. memset(i2c, 0, sizeof(*i2c));
  266. i2c->irq = ocp->def->irq;
  267. i2c->flags = ((struct ocp_fs_i2c_data *)ocp->def->additions)->flags;
  268. init_waitqueue_head(&i2c->queue);
  269. if (!request_mem_region(ocp->def->paddr, MPC_I2C_REGION, "i2c-mpc")) {
  270. printk(KERN_ERR "i2c-mpc - resource unavailable\n");
  271. return -ENODEV;
  272. }
  273. i2c->base = ioremap(ocp->def->paddr, MPC_I2C_REGION);
  274. if (!i2c->base) {
  275. printk(KERN_ERR "i2c-mpc - failed to map controller\n");
  276. result = -ENOMEM;
  277. goto fail_map;
  278. }
  279. if (i2c->irq != OCP_IRQ_NA)
  280. {
  281. if ((result = request_irq(ocp->def->irq, mpc_i2c_isr,
  282. 0, "i2c-mpc", i2c)) < 0) {
  283. printk(KERN_ERR
  284. "i2c-mpc - failed to attach interrupt\n");
  285. goto fail_irq;
  286. }
  287. } else
  288. i2c->irq = 0;
  289. i2c->adap = mpc_ops;
  290. i2c_set_adapdata(&i2c->adap, i2c);
  291. if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
  292. printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
  293. goto fail_add;
  294. }
  295. mpc_i2c_setclock(i2c);
  296. ocp_set_drvdata(ocp, i2c);
  297. return result;
  298. fail_add:
  299. if (ocp->def->irq != OCP_IRQ_NA)
  300. free_irq(ocp->def->irq, 0);
  301. fail_irq:
  302. iounmap(i2c->base);
  303. fail_map:
  304. release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
  305. kfree(i2c);
  306. return result;
  307. }
  308. static void __devexit mpc_i2c_remove(struct ocp_device *ocp)
  309. {
  310. struct mpc_i2c *i2c = ocp_get_drvdata(ocp);
  311. ocp_set_drvdata(ocp, NULL);
  312. i2c_del_adapter(&i2c->adap);
  313. if (ocp->def->irq != OCP_IRQ_NA)
  314. free_irq(i2c->irq, i2c);
  315. iounmap(i2c->base);
  316. release_mem_region(ocp->def->paddr, MPC_I2C_REGION);
  317. kfree(i2c);
  318. }
  319. static struct ocp_device_id mpc_iic_ids[] __devinitdata = {
  320. {.vendor = OCP_VENDOR_FREESCALE,.function = OCP_FUNC_IIC},
  321. {.vendor = OCP_VENDOR_INVALID}
  322. };
  323. MODULE_DEVICE_TABLE(ocp, mpc_iic_ids);
  324. static struct ocp_driver mpc_iic_driver = {
  325. .name = "iic",
  326. .id_table = mpc_iic_ids,
  327. .probe = mpc_i2c_probe,
  328. .remove = __devexit_p(mpc_i2c_remove)
  329. };
  330. static int __init iic_init(void)
  331. {
  332. return ocp_register_driver(&mpc_iic_driver);
  333. }
  334. static void __exit iic_exit(void)
  335. {
  336. ocp_unregister_driver(&mpc_iic_driver);
  337. }
  338. module_init(iic_init);
  339. module_exit(iic_exit);
  340. #else
  341. static int fsl_i2c_probe(struct device *device)
  342. {
  343. int result = 0;
  344. struct mpc_i2c *i2c;
  345. struct platform_device *pdev = to_platform_device(device);
  346. struct fsl_i2c_platform_data *pdata;
  347. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. pdata = (struct fsl_i2c_platform_data *) pdev->dev.platform_data;
  349. if (!(i2c = kmalloc(sizeof(*i2c), GFP_KERNEL))) {
  350. return -ENOMEM;
  351. }
  352. memset(i2c, 0, sizeof(*i2c));
  353. i2c->irq = platform_get_irq(pdev, 0);
  354. i2c->flags = pdata->device_flags;
  355. init_waitqueue_head(&i2c->queue);
  356. i2c->base = ioremap((phys_addr_t)r->start, MPC_I2C_REGION);
  357. if (!i2c->base) {
  358. printk(KERN_ERR "i2c-mpc - failed to map controller\n");
  359. result = -ENOMEM;
  360. goto fail_map;
  361. }
  362. if (i2c->irq != 0)
  363. if ((result = request_irq(i2c->irq, mpc_i2c_isr,
  364. 0, "fsl-i2c", i2c)) < 0) {
  365. printk(KERN_ERR
  366. "i2c-mpc - failed to attach interrupt\n");
  367. goto fail_irq;
  368. }
  369. i2c->adap = mpc_ops;
  370. i2c_set_adapdata(&i2c->adap, i2c);
  371. i2c->adap.dev.parent = &pdev->dev;
  372. if ((result = i2c_add_adapter(&i2c->adap)) < 0) {
  373. printk(KERN_ERR "i2c-mpc - failed to add adapter\n");
  374. goto fail_add;
  375. }
  376. mpc_i2c_setclock(i2c);
  377. dev_set_drvdata(device, i2c);
  378. return result;
  379. fail_add:
  380. if (i2c->irq != 0)
  381. free_irq(i2c->irq, NULL);
  382. fail_irq:
  383. iounmap(i2c->base);
  384. fail_map:
  385. kfree(i2c);
  386. return result;
  387. };
  388. static int fsl_i2c_remove(struct device *device)
  389. {
  390. struct mpc_i2c *i2c = dev_get_drvdata(device);
  391. dev_set_drvdata(device, NULL);
  392. i2c_del_adapter(&i2c->adap);
  393. if (i2c->irq != 0)
  394. free_irq(i2c->irq, i2c);
  395. iounmap(i2c->base);
  396. kfree(i2c);
  397. return 0;
  398. };
  399. /* Structure for a device driver */
  400. static struct device_driver fsl_i2c_driver = {
  401. .name = "fsl-i2c",
  402. .bus = &platform_bus_type,
  403. .probe = fsl_i2c_probe,
  404. .remove = fsl_i2c_remove,
  405. };
  406. static int __init fsl_i2c_init(void)
  407. {
  408. return driver_register(&fsl_i2c_driver);
  409. }
  410. static void __exit fsl_i2c_exit(void)
  411. {
  412. driver_unregister(&fsl_i2c_driver);
  413. }
  414. module_init(fsl_i2c_init);
  415. module_exit(fsl_i2c_exit);
  416. #endif /* CONFIG_FSL_OCP */
  417. MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
  418. MODULE_DESCRIPTION
  419. ("I2C-Bus adapter for MPC107 bridge and MPC824x/85xx/52xx processors");
  420. MODULE_LICENSE("GPL");