ehci_controller.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * ehci_controller.c
  3. *
  4. * Created on: Mar 9, 2013
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2012, hathach (tinyusb.org)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tiny usb stack.
  35. */
  36. //--------------------------------------------------------------------+
  37. // INCLUDE
  38. //--------------------------------------------------------------------+
  39. #include "unity.h"
  40. #include "tusb_option.h"
  41. #include "errors.h"
  42. #include "binary.h"
  43. #include "hal.h"
  44. #include "ehci.h"
  45. //--------------------------------------------------------------------+
  46. // MACRO CONSTANT TYPEDEF
  47. //--------------------------------------------------------------------+
  48. ehci_data_t ehci_data;
  49. LPC_USB0_Type lpc_usb0;
  50. LPC_USB1_Type lpc_usb1;
  51. //--------------------------------------------------------------------+
  52. // IMPLEMENTATION
  53. //--------------------------------------------------------------------+
  54. bool complete_all_qtd_in_list(ehci_qhd_t *head)
  55. {
  56. ehci_qhd_t *p_qhd = head;
  57. do
  58. {
  59. if ( !p_qhd->qtd_overlay.halted )
  60. {
  61. while(!p_qhd->qtd_overlay.next.terminate)
  62. {
  63. ehci_qtd_t* p_qtd = (ehci_qtd_t*) align32(p_qhd->qtd_overlay.next.address);
  64. p_qtd->active = 0;
  65. p_qhd->qtd_overlay = *p_qtd;
  66. }
  67. }
  68. if (!p_qhd->next.terminate)
  69. {
  70. p_qhd = (ehci_qhd_t*) align32(p_qhd->next.address);
  71. }
  72. else
  73. {
  74. break;
  75. }
  76. }while(p_qhd != head); // stop if loop around
  77. return true;
  78. }
  79. void ehci_controller_run(uint8_t hostid)
  80. {
  81. //------------- Async List -------------//
  82. ehci_registers_t* const regs = get_operational_register(hostid);
  83. complete_all_qtd_in_list((ehci_qhd_t*) regs->async_list_base);
  84. //------------- Period List -------------//
  85. complete_all_qtd_in_list( get_period_head(hostid) );
  86. regs->usb_sts = EHCI_INT_MASK_NXP_ASYNC | EHCI_INT_MASK_NXP_PERIODIC;
  87. hcd_isr(hostid);
  88. }
  89. void ehci_controller_run_error(uint8_t hostid)
  90. {
  91. //------------- Async List -------------//
  92. ehci_registers_t* const regs = get_operational_register(hostid);
  93. ehci_qhd_t *p_qhd = (ehci_qhd_t*) regs->async_list_base;
  94. do
  95. {
  96. if ( !p_qhd->qtd_overlay.halted )
  97. {
  98. if(!p_qhd->qtd_overlay.next.terminate)
  99. {
  100. ehci_qtd_t* p_qtd = (ehci_qtd_t*) align32(p_qhd->qtd_overlay.next.address);
  101. p_qtd->active = 0;
  102. p_qtd->babble_err = p_qtd->buffer_err = p_qtd->xact_err = 1;
  103. p_qhd->qtd_overlay = *p_qtd;
  104. }
  105. }
  106. p_qhd = (ehci_qhd_t*) align32(p_qhd->next.address);
  107. }while(p_qhd != get_async_head(hostid)); // stop if loop around
  108. //------------- Period List -------------//
  109. regs->usb_sts = EHCI_INT_MASK_ERROR;
  110. hcd_isr(hostid);
  111. }
  112. void ehci_controller_device_plug(uint8_t hostid, tusb_speed_t speed)
  113. {
  114. ehci_registers_t* const regs = get_operational_register(hostid);
  115. regs->usb_sts_bit.port_change_detect = 1;
  116. regs->portsc_bit.connect_status_change = 1;
  117. regs->portsc_bit.current_connect_status = 1;
  118. regs->portsc_bit.nxp_port_speed = speed;
  119. }
  120. void ehci_controller_device_unplug(uint8_t hostid)
  121. {
  122. ehci_registers_t* const regs = get_operational_register(hostid);
  123. regs->usb_sts_bit.port_change_detect = 1;
  124. regs->portsc_bit.connect_status_change = 1;
  125. regs->portsc_bit.current_connect_status = 0;
  126. }