c67x00-drv.c 5.9 KB

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