c67x00-drv.c 5.7 KB

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