c67x00-drv.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include "c67x00-hcd.h"
  42. static void c67x00_probe_sie(struct c67x00_sie *sie,
  43. struct c67x00_device *dev, int sie_num)
  44. {
  45. spin_lock_init(&sie->lock);
  46. sie->dev = dev;
  47. sie->sie_num = sie_num;
  48. sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
  49. switch (sie->mode) {
  50. case C67X00_SIE_HOST:
  51. c67x00_hcd_probe(sie);
  52. break;
  53. case C67X00_SIE_UNUSED:
  54. dev_info(sie_dev(sie),
  55. "Not using SIE %d as requested\n", sie->sie_num);
  56. break;
  57. default:
  58. dev_err(sie_dev(sie),
  59. "Unsupported configuration: 0x%x for SIE %d\n",
  60. sie->mode, sie->sie_num);
  61. break;
  62. }
  63. }
  64. static void c67x00_remove_sie(struct c67x00_sie *sie)
  65. {
  66. switch (sie->mode) {
  67. case C67X00_SIE_HOST:
  68. c67x00_hcd_remove(sie);
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. static irqreturn_t c67x00_irq(int irq, void *__dev)
  75. {
  76. struct c67x00_device *c67x00 = __dev;
  77. struct c67x00_sie *sie;
  78. u16 msg, int_status;
  79. int i, count = 8;
  80. int_status = c67x00_ll_hpi_status(c67x00);
  81. if (!int_status)
  82. return IRQ_NONE;
  83. while (int_status != 0 && (count-- >= 0)) {
  84. c67x00_ll_irq(c67x00, int_status);
  85. for (i = 0; i < C67X00_SIES; i++) {
  86. sie = &c67x00->sie[i];
  87. msg = 0;
  88. if (int_status & SIEMSG_FLG(i))
  89. msg = c67x00_ll_fetch_siemsg(c67x00, i);
  90. if (sie->irq)
  91. sie->irq(sie, int_status, msg);
  92. }
  93. int_status = c67x00_ll_hpi_status(c67x00);
  94. }
  95. if (int_status)
  96. dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
  97. "status = 0x%04x\n", int_status);
  98. return IRQ_HANDLED;
  99. }
  100. /* ------------------------------------------------------------------------- */
  101. static int __devinit c67x00_drv_probe(struct platform_device *pdev)
  102. {
  103. struct c67x00_device *c67x00;
  104. struct c67x00_platform_data *pdata;
  105. struct resource *res, *res2;
  106. int ret, i;
  107. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  108. if (!res)
  109. return -ENODEV;
  110. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  111. if (!res2)
  112. return -ENODEV;
  113. pdata = pdev->dev.platform_data;
  114. if (!pdata)
  115. return -ENODEV;
  116. c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
  117. if (!c67x00)
  118. return -ENOMEM;
  119. if (!request_mem_region(res->start, res->end - res->start + 1,
  120. pdev->name)) {
  121. dev_err(&pdev->dev, "Memory region busy\n");
  122. ret = -EBUSY;
  123. goto request_mem_failed;
  124. }
  125. c67x00->hpi.base = ioremap(res->start, res->end - res->start + 1);
  126. if (!c67x00->hpi.base) {
  127. dev_err(&pdev->dev, "Unable to map HPI registers\n");
  128. ret = -EIO;
  129. goto map_failed;
  130. }
  131. spin_lock_init(&c67x00->hpi.lock);
  132. c67x00->hpi.regstep = pdata->hpi_regstep;
  133. c67x00->pdata = pdev->dev.platform_data;
  134. c67x00->pdev = pdev;
  135. c67x00_ll_init(c67x00);
  136. c67x00_ll_hpi_reg_init(c67x00);
  137. ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
  138. if (ret) {
  139. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  140. goto request_irq_failed;
  141. }
  142. ret = c67x00_ll_reset(c67x00);
  143. if (ret) {
  144. dev_err(&pdev->dev, "Device reset failed\n");
  145. goto reset_failed;
  146. }
  147. for (i = 0; i < C67X00_SIES; i++)
  148. c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
  149. platform_set_drvdata(pdev, c67x00);
  150. return 0;
  151. reset_failed:
  152. free_irq(res2->start, c67x00);
  153. request_irq_failed:
  154. iounmap(c67x00->hpi.base);
  155. map_failed:
  156. release_mem_region(res->start, res->end - res->start + 1);
  157. request_mem_failed:
  158. kfree(c67x00);
  159. return ret;
  160. }
  161. static int __devexit c67x00_drv_remove(struct platform_device *pdev)
  162. {
  163. struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
  164. struct resource *res;
  165. int i;
  166. for (i = 0; i < C67X00_SIES; i++)
  167. c67x00_remove_sie(&c67x00->sie[i]);
  168. c67x00_ll_release(c67x00);
  169. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  170. if (res)
  171. free_irq(res->start, c67x00);
  172. iounmap(c67x00->hpi.base);
  173. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  174. if (res)
  175. release_mem_region(res->start, res->end - res->start + 1);
  176. kfree(c67x00);
  177. return 0;
  178. }
  179. static struct platform_driver c67x00_driver = {
  180. .probe = c67x00_drv_probe,
  181. .remove = __devexit_p(c67x00_drv_remove),
  182. .driver = {
  183. .owner = THIS_MODULE,
  184. .name = "c67x00",
  185. },
  186. };
  187. MODULE_ALIAS("platform:c67x00");
  188. static int __init c67x00_init(void)
  189. {
  190. return platform_driver_register(&c67x00_driver);
  191. }
  192. static void __exit c67x00_exit(void)
  193. {
  194. platform_driver_unregister(&c67x00_driver);
  195. }
  196. module_init(c67x00_init);
  197. module_exit(c67x00_exit);
  198. MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
  199. MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
  200. MODULE_LICENSE("GPL");