c67x00-drv.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * c67x00-drv.c: Cypress C67X00 USB Common infrastructure
  3. *
  4. * Copyright (C) 2006-2008 Barco N.V.
  5. * Derived from the Cypress cy7c67200/300 ezusb linux driver and
  6. * based on multiple host controller drivers inside the linux kernel.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. * MA 02110-1301 USA.
  22. */
  23. /*
  24. * This file implements the common infrastructure for using the c67x00.
  25. * It is both the link between the platform configuration and subdrivers and
  26. * the link between the common hardware parts and the subdrivers (e.g.
  27. * interrupt handling).
  28. *
  29. * The c67x00 has 2 SIE's (serial interface engine) wich can be configured
  30. * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG).
  31. *
  32. * Depending on the platform configuration, the SIE's are created and
  33. * the corresponding subdriver is initialized (c67x00_probe_sie).
  34. */
  35. #include <linux/device.h>
  36. #include <linux/io.h>
  37. #include <linux/list.h>
  38. #include <linux/usb.h>
  39. #include <linux/usb/c67x00.h>
  40. #include "c67x00.h"
  41. static void c67x00_probe_sie(struct c67x00_sie *sie,
  42. struct c67x00_device *dev, int sie_num)
  43. {
  44. spin_lock_init(&sie->lock);
  45. sie->dev = dev;
  46. sie->sie_num = sie_num;
  47. sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
  48. switch (sie->mode) {
  49. case C67X00_SIE_UNUSED:
  50. dev_info(sie_dev(sie),
  51. "Not using SIE %d as requested\n", sie->sie_num);
  52. break;
  53. default:
  54. dev_err(sie_dev(sie),
  55. "Unsupported configuration: 0x%x for SIE %d\n",
  56. sie->mode, sie->sie_num);
  57. break;
  58. }
  59. }
  60. static void c67x00_remove_sie(struct c67x00_sie *sie)
  61. {
  62. }
  63. static irqreturn_t c67x00_irq(int irq, void *__dev)
  64. {
  65. struct c67x00_device *c67x00 = __dev;
  66. struct c67x00_sie *sie;
  67. u16 msg, int_status;
  68. int i, count = 8;
  69. int_status = c67x00_ll_hpi_status(c67x00);
  70. if (!int_status)
  71. return IRQ_NONE;
  72. while (int_status != 0 && (count-- >= 0)) {
  73. c67x00_ll_irq(c67x00, int_status);
  74. for (i = 0; i < C67X00_SIES; i++) {
  75. sie = &c67x00->sie[i];
  76. msg = 0;
  77. if (int_status & SIEMSG_FLG(i))
  78. msg = c67x00_ll_fetch_siemsg(c67x00, i);
  79. if (sie->irq)
  80. sie->irq(sie, int_status, msg);
  81. }
  82. int_status = c67x00_ll_hpi_status(c67x00);
  83. }
  84. if (int_status)
  85. dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
  86. "status = 0x%04x\n", int_status);
  87. return IRQ_HANDLED;
  88. }
  89. /* ------------------------------------------------------------------------- */
  90. static int __devinit c67x00_drv_probe(struct platform_device *pdev)
  91. {
  92. struct c67x00_device *c67x00;
  93. struct c67x00_platform_data *pdata;
  94. struct resource *res, *res2;
  95. int ret, i;
  96. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  97. if (!res)
  98. return -ENODEV;
  99. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  100. if (!res2)
  101. return -ENODEV;
  102. pdata = pdev->dev.platform_data;
  103. if (!pdata)
  104. return -ENODEV;
  105. c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
  106. if (!c67x00)
  107. return -ENOMEM;
  108. if (!request_mem_region(res->start, res->end - res->start + 1,
  109. pdev->name)) {
  110. dev_err(&pdev->dev, "Memory region busy\n");
  111. ret = -EBUSY;
  112. goto request_mem_failed;
  113. }
  114. c67x00->hpi.base = ioremap(res->start, res->end - res->start + 1);
  115. if (!c67x00->hpi.base) {
  116. dev_err(&pdev->dev, "Unable to map HPI registers\n");
  117. ret = -EIO;
  118. goto map_failed;
  119. }
  120. spin_lock_init(&c67x00->hpi.lock);
  121. c67x00->hpi.regstep = pdata->hpi_regstep;
  122. c67x00->pdata = pdev->dev.platform_data;
  123. c67x00->pdev = pdev;
  124. c67x00_ll_init(c67x00);
  125. c67x00_ll_hpi_reg_init(c67x00);
  126. ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
  127. if (ret) {
  128. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  129. goto request_irq_failed;
  130. }
  131. ret = c67x00_ll_reset(c67x00);
  132. if (ret) {
  133. dev_err(&pdev->dev, "Device reset failed\n");
  134. goto reset_failed;
  135. }
  136. for (i = 0; i < C67X00_SIES; i++)
  137. c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
  138. platform_set_drvdata(pdev, c67x00);
  139. return 0;
  140. reset_failed:
  141. free_irq(res2->start, c67x00);
  142. request_irq_failed:
  143. iounmap(c67x00->hpi.base);
  144. map_failed:
  145. release_mem_region(res->start, res->end - res->start + 1);
  146. request_mem_failed:
  147. kfree(c67x00);
  148. return ret;
  149. }
  150. static int __devexit c67x00_drv_remove(struct platform_device *pdev)
  151. {
  152. struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
  153. struct resource *res;
  154. int i;
  155. for (i = 0; i < C67X00_SIES; i++)
  156. c67x00_remove_sie(&c67x00->sie[i]);
  157. c67x00_ll_release(c67x00);
  158. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  159. if (res)
  160. free_irq(res->start, c67x00);
  161. iounmap(c67x00->hpi.base);
  162. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  163. if (res)
  164. release_mem_region(res->start, res->end - res->start + 1);
  165. kfree(c67x00);
  166. return 0;
  167. }
  168. static struct platform_driver c67x00_driver = {
  169. .probe = c67x00_drv_probe,
  170. .remove = __devexit_p(c67x00_drv_remove),
  171. .driver = {
  172. .owner = THIS_MODULE,
  173. .name = "c67x00",
  174. },
  175. };
  176. MODULE_ALIAS("platform:c67x00");
  177. static int __init c67x00_init(void)
  178. {
  179. return platform_driver_register(&c67x00_driver);
  180. }
  181. static void __exit c67x00_exit(void)
  182. {
  183. platform_driver_unregister(&c67x00_driver);
  184. }
  185. module_init(c67x00_init);
  186. module_exit(c67x00_exit);
  187. MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
  188. MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
  189. MODULE_LICENSE("GPL");