isp1760-core.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the NXP ISP1760 chip
  4. *
  5. * Copyright 2014 Laurent Pinchart
  6. * Copyright 2007 Sebastian Siewior
  7. *
  8. * Contacts:
  9. * Sebastian Siewior <bigeasy@linutronix.de>
  10. * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include "isp1760-core.h"
  20. #include "isp1760-hcd.h"
  21. #include "isp1760-regs.h"
  22. #include "isp1760-udc.h"
  23. static void isp1760_init_core(struct isp1760_device *isp)
  24. {
  25. u32 otgctrl;
  26. u32 hwmode;
  27. /* Low-level chip reset */
  28. if (isp->rst_gpio) {
  29. gpiod_set_value_cansleep(isp->rst_gpio, 1);
  30. msleep(50);
  31. gpiod_set_value_cansleep(isp->rst_gpio, 0);
  32. }
  33. /*
  34. * Reset the host controller, including the CPU interface
  35. * configuration.
  36. */
  37. isp1760_write32(isp->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
  38. msleep(100);
  39. /* Setup HW Mode Control: This assumes a level active-low interrupt */
  40. hwmode = HW_DATA_BUS_32BIT;
  41. if (isp->devflags & ISP1760_FLAG_BUS_WIDTH_16)
  42. hwmode &= ~HW_DATA_BUS_32BIT;
  43. if (isp->devflags & ISP1760_FLAG_ANALOG_OC)
  44. hwmode |= HW_ANA_DIGI_OC;
  45. if (isp->devflags & ISP1760_FLAG_DACK_POL_HIGH)
  46. hwmode |= HW_DACK_POL_HIGH;
  47. if (isp->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
  48. hwmode |= HW_DREQ_POL_HIGH;
  49. if (isp->devflags & ISP1760_FLAG_INTR_POL_HIGH)
  50. hwmode |= HW_INTR_HIGH_ACT;
  51. if (isp->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
  52. hwmode |= HW_INTR_EDGE_TRIG;
  53. /*
  54. * The ISP1761 has a dedicated DC IRQ line but supports sharing the HC
  55. * IRQ line for both the host and device controllers. Hardcode IRQ
  56. * sharing for now and disable the DC interrupts globally to avoid
  57. * spurious interrupts during HCD registration.
  58. */
  59. if (isp->devflags & ISP1760_FLAG_ISP1761) {
  60. isp1760_write32(isp->regs, DC_MODE, 0);
  61. hwmode |= HW_COMN_IRQ;
  62. }
  63. /*
  64. * We have to set this first in case we're in 16-bit mode.
  65. * Write it twice to ensure correct upper bits if switching
  66. * to 16-bit mode.
  67. */
  68. isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
  69. isp1760_write32(isp->regs, HC_HW_MODE_CTRL, hwmode);
  70. /*
  71. * PORT 1 Control register of the ISP1760 is the OTG control register
  72. * on ISP1761.
  73. *
  74. * TODO: Really support OTG. For now we configure port 1 in device mode
  75. * when OTG is requested.
  76. */
  77. if ((isp->devflags & ISP1760_FLAG_ISP1761) &&
  78. (isp->devflags & ISP1760_FLAG_OTG_EN))
  79. otgctrl = ((HW_DM_PULLDOWN | HW_DP_PULLDOWN) << 16)
  80. | HW_OTG_DISABLE;
  81. else
  82. otgctrl = (HW_SW_SEL_HC_DC << 16)
  83. | (HW_VBUS_DRV | HW_SEL_CP_EXT);
  84. isp1760_write32(isp->regs, HC_PORT1_CTRL, otgctrl);
  85. dev_info(isp->dev, "bus width: %u, oc: %s\n",
  86. isp->devflags & ISP1760_FLAG_BUS_WIDTH_16 ? 16 : 32,
  87. isp->devflags & ISP1760_FLAG_ANALOG_OC ? "analog" : "digital");
  88. }
  89. void isp1760_set_pullup(struct isp1760_device *isp, bool enable)
  90. {
  91. isp1760_write32(isp->regs, HW_OTG_CTRL_SET,
  92. enable ? HW_DP_PULLUP : HW_DP_PULLUP << 16);
  93. }
  94. int isp1760_register(struct resource *mem, int irq, unsigned long irqflags,
  95. struct device *dev, unsigned int devflags)
  96. {
  97. struct isp1760_device *isp;
  98. bool udc_disabled = !(devflags & ISP1760_FLAG_ISP1761);
  99. int ret;
  100. /*
  101. * If neither the HCD not the UDC is enabled return an error, as no
  102. * device would be registered.
  103. */
  104. if ((!IS_ENABLED(CONFIG_USB_ISP1760_HCD) || usb_disabled()) &&
  105. (!IS_ENABLED(CONFIG_USB_ISP1761_UDC) || udc_disabled))
  106. return -ENODEV;
  107. /* prevent usb-core allocating DMA pages */
  108. dev->dma_mask = NULL;
  109. isp = devm_kzalloc(dev, sizeof(*isp), GFP_KERNEL);
  110. if (!isp)
  111. return -ENOMEM;
  112. isp->dev = dev;
  113. isp->devflags = devflags;
  114. isp->rst_gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
  115. if (IS_ERR(isp->rst_gpio))
  116. return PTR_ERR(isp->rst_gpio);
  117. isp->regs = devm_ioremap_resource(dev, mem);
  118. if (IS_ERR(isp->regs))
  119. return PTR_ERR(isp->regs);
  120. isp1760_init_core(isp);
  121. if (IS_ENABLED(CONFIG_USB_ISP1760_HCD) && !usb_disabled()) {
  122. ret = isp1760_hcd_register(&isp->hcd, isp->regs, mem, irq,
  123. irqflags | IRQF_SHARED, dev);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. if (IS_ENABLED(CONFIG_USB_ISP1761_UDC) && !udc_disabled) {
  128. ret = isp1760_udc_register(isp, irq, irqflags);
  129. if (ret < 0) {
  130. isp1760_hcd_unregister(&isp->hcd);
  131. return ret;
  132. }
  133. }
  134. dev_set_drvdata(dev, isp);
  135. return 0;
  136. }
  137. void isp1760_unregister(struct device *dev)
  138. {
  139. struct isp1760_device *isp = dev_get_drvdata(dev);
  140. isp1760_udc_unregister(isp);
  141. isp1760_hcd_unregister(&isp->hcd);
  142. }
  143. MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
  144. MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
  145. MODULE_LICENSE("GPL v2");