c67x00-drv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * c67x00-drv.c: Cypress C67X00 USB Common infrastructure
  4. *
  5. * Copyright (C) 2006-2008 Barco N.V.
  6. * Derived from the Cypress cy7c67200/300 ezusb linux driver and
  7. * based on multiple host controller drivers inside the linux kernel.
  8. */
  9. /*
  10. * This file implements the common infrastructure for using the c67x00.
  11. * It is both the link between the platform configuration and subdrivers and
  12. * the link between the common hardware parts and the subdrivers (e.g.
  13. * interrupt handling).
  14. *
  15. * The c67x00 has 2 SIE's (serial interface engine) which can be configured
  16. * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG).
  17. *
  18. * Depending on the platform configuration, the SIE's are created and
  19. * the corresponding subdriver is initialized (c67x00_probe_sie).
  20. */
  21. #include <linux/device.h>
  22. #include <linux/io.h>
  23. #include <linux/list.h>
  24. #include <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/c67x00.h>
  28. #include "c67x00.h"
  29. #include "c67x00-hcd.h"
  30. static void c67x00_probe_sie(struct c67x00_sie *sie,
  31. struct c67x00_device *dev, int sie_num)
  32. {
  33. spin_lock_init(&sie->lock);
  34. sie->dev = dev;
  35. sie->sie_num = sie_num;
  36. sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num);
  37. switch (sie->mode) {
  38. case C67X00_SIE_HOST:
  39. c67x00_hcd_probe(sie);
  40. break;
  41. case C67X00_SIE_UNUSED:
  42. dev_info(sie_dev(sie),
  43. "Not using SIE %d as requested\n", sie->sie_num);
  44. break;
  45. default:
  46. dev_err(sie_dev(sie),
  47. "Unsupported configuration: 0x%x for SIE %d\n",
  48. sie->mode, sie->sie_num);
  49. break;
  50. }
  51. }
  52. static void c67x00_remove_sie(struct c67x00_sie *sie)
  53. {
  54. switch (sie->mode) {
  55. case C67X00_SIE_HOST:
  56. c67x00_hcd_remove(sie);
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. static irqreturn_t c67x00_irq(int irq, void *__dev)
  63. {
  64. struct c67x00_device *c67x00 = __dev;
  65. struct c67x00_sie *sie;
  66. u16 msg, int_status;
  67. int i, count = 8;
  68. int_status = c67x00_ll_hpi_status(c67x00);
  69. if (!int_status)
  70. return IRQ_NONE;
  71. while (int_status != 0 && (count-- >= 0)) {
  72. c67x00_ll_irq(c67x00, int_status);
  73. for (i = 0; i < C67X00_SIES; i++) {
  74. sie = &c67x00->sie[i];
  75. msg = 0;
  76. if (int_status & SIEMSG_FLG(i))
  77. msg = c67x00_ll_fetch_siemsg(c67x00, i);
  78. if (sie->irq)
  79. sie->irq(sie, int_status, msg);
  80. }
  81. int_status = c67x00_ll_hpi_status(c67x00);
  82. }
  83. if (int_status)
  84. dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! "
  85. "status = 0x%04x\n", int_status);
  86. return IRQ_HANDLED;
  87. }
  88. /* ------------------------------------------------------------------------- */
  89. static int c67x00_drv_probe(struct platform_device *pdev)
  90. {
  91. struct c67x00_device *c67x00;
  92. struct c67x00_platform_data *pdata;
  93. struct resource *res, *res2;
  94. int ret, i;
  95. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  96. if (!res)
  97. return -ENODEV;
  98. res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  99. if (!res2)
  100. return -ENODEV;
  101. pdata = dev_get_platdata(&pdev->dev);
  102. if (!pdata)
  103. return -ENODEV;
  104. c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL);
  105. if (!c67x00)
  106. return -ENOMEM;
  107. if (!request_mem_region(res->start, resource_size(res),
  108. pdev->name)) {
  109. dev_err(&pdev->dev, "Memory region busy\n");
  110. ret = -EBUSY;
  111. goto request_mem_failed;
  112. }
  113. c67x00->hpi.base = ioremap(res->start, resource_size(res));
  114. if (!c67x00->hpi.base) {
  115. dev_err(&pdev->dev, "Unable to map HPI registers\n");
  116. ret = -EIO;
  117. goto map_failed;
  118. }
  119. spin_lock_init(&c67x00->hpi.lock);
  120. c67x00->hpi.regstep = pdata->hpi_regstep;
  121. c67x00->pdata = dev_get_platdata(&pdev->dev);
  122. c67x00->pdev = pdev;
  123. c67x00_ll_init(c67x00);
  124. c67x00_ll_hpi_reg_init(c67x00);
  125. ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00);
  126. if (ret) {
  127. dev_err(&pdev->dev, "Cannot claim IRQ\n");
  128. goto request_irq_failed;
  129. }
  130. ret = c67x00_ll_reset(c67x00);
  131. if (ret) {
  132. dev_err(&pdev->dev, "Device reset failed\n");
  133. goto reset_failed;
  134. }
  135. for (i = 0; i < C67X00_SIES; i++)
  136. c67x00_probe_sie(&c67x00->sie[i], c67x00, i);
  137. platform_set_drvdata(pdev, c67x00);
  138. return 0;
  139. reset_failed:
  140. free_irq(res2->start, c67x00);
  141. request_irq_failed:
  142. iounmap(c67x00->hpi.base);
  143. map_failed:
  144. release_mem_region(res->start, resource_size(res));
  145. request_mem_failed:
  146. kfree(c67x00);
  147. return ret;
  148. }
  149. static int c67x00_drv_remove(struct platform_device *pdev)
  150. {
  151. struct c67x00_device *c67x00 = platform_get_drvdata(pdev);
  152. struct resource *res;
  153. int i;
  154. for (i = 0; i < C67X00_SIES; i++)
  155. c67x00_remove_sie(&c67x00->sie[i]);
  156. c67x00_ll_release(c67x00);
  157. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  158. if (res)
  159. free_irq(res->start, c67x00);
  160. iounmap(c67x00->hpi.base);
  161. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  162. if (res)
  163. release_mem_region(res->start, resource_size(res));
  164. kfree(c67x00);
  165. return 0;
  166. }
  167. static struct platform_driver c67x00_driver = {
  168. .probe = c67x00_drv_probe,
  169. .remove = c67x00_drv_remove,
  170. .driver = {
  171. .name = "c67x00",
  172. },
  173. };
  174. module_platform_driver(c67x00_driver);
  175. MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely");
  176. MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver");
  177. MODULE_LICENSE("GPL");
  178. MODULE_ALIAS("platform:c67x00");