usbdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*USB 1.1,2.0 device*/
  2. #include <common.h>
  3. #include <asm/processor.h>
  4. #ifdef CONFIG_440_EP
  5. #include <usb.h>
  6. #include "usbdev.h"
  7. #include "vecnum.h"
  8. #define USB_DT_DEVICE 0x01
  9. #define USB_DT_CONFIG 0x02
  10. #define USB_DT_STRING 0x03
  11. #define USB_DT_INTERFACE 0x04
  12. #define USB_DT_ENDPOINT 0x05
  13. unsigned char set_value = -1;
  14. void process_endpoints(unsigned short usb2d0_intrin)
  15. {
  16. /*will hold the packet received */
  17. struct usb_device_descriptor usb_device_packet;
  18. struct usb_config_descriptor usb_config_packet;
  19. struct usb_string_descriptor usb_string_packet;
  20. struct devrequest setup_packet;
  21. unsigned int *setup_packet_pt;
  22. unsigned char *packet_pt;
  23. int temp, temp1;
  24. int i;
  25. /*printf("{USB device} - endpoint 0x%X \n", usb2d0_intrin); */
  26. /*set usb address, seems to not work unless it is done in the next
  27. interrupt, so that is why it is done this way */
  28. if (set_value != -1)
  29. *(unsigned char *)USB2D0_FADDR_8 = set_value;
  30. /*endpoint 1 */
  31. if (usb2d0_intrin & 0x01) {
  32. setup_packet_pt = (unsigned int *)&setup_packet;
  33. /*copy packet */
  34. setup_packet_pt[0] = *(unsigned int *)USB2D0_FIFO_0;
  35. setup_packet_pt[1] = *(unsigned int *)USB2D0_FIFO_0;
  36. temp = *(unsigned int *)USB2D0_FIFO_0;
  37. temp1 = *(unsigned int *)USB2D0_FIFO_0;
  38. /*do some swapping */
  39. setup_packet.value = swap_16(setup_packet.value);
  40. setup_packet.index = swap_16(setup_packet.index);
  41. setup_packet.length = swap_16(setup_packet.length);
  42. /*clear rx packet */
  43. *(unsigned short *)USB2D0_INCSR0_8 = 0x48;
  44. /*printf("0x%X 0x%X 0x%X 0x%X 0x%X 0x%X 0x%X\n", setup_packet.requesttype,
  45. setup_packet.request, setup_packet.value,
  46. setup_packet.index, setup_packet.length, temp, temp1 ); */
  47. switch (setup_packet.request) {
  48. case USB_REQ_GET_DESCRIPTOR:
  49. switch (setup_packet.value >> 8) {
  50. case USB_DT_DEVICE:
  51. /*create packet */
  52. usb_device_packet.bLength = 18;
  53. usb_device_packet.bDescriptorType =
  54. USB_DT_DEVICE;
  55. #ifdef USB_2_0_DEVICE
  56. usb_device_packet.bcdUSB = swap_16(0x200);
  57. #else
  58. usb_device_packet.bcdUSB = swap_16(0x110);
  59. #endif
  60. usb_device_packet.bDeviceClass = 0xff;
  61. usb_device_packet.bDeviceSubClass = 0;
  62. usb_device_packet.bDeviceProtocol = 0;
  63. usb_device_packet.bMaxPacketSize0 = 32;
  64. usb_device_packet.idVendor = swap_16(1);
  65. usb_device_packet.idProduct = swap_16(2);
  66. usb_device_packet.bcdDevice = swap_16(0x300);
  67. usb_device_packet.iManufacturer = 1;
  68. usb_device_packet.iProduct = 1;
  69. usb_device_packet.iSerialNumber = 1;
  70. usb_device_packet.bNumConfigurations = 1;
  71. /*put packet in fifo */
  72. packet_pt = (unsigned char *)&usb_device_packet;
  73. break;
  74. case USB_DT_CONFIG:
  75. /*create packet */
  76. usb_config_packet.bLength = 9;
  77. usb_config_packet.bDescriptorType =
  78. USB_DT_CONFIG;
  79. usb_config_packet.wTotalLength = swap_16(25);
  80. usb_config_packet.bNumInterfaces = 1;
  81. usb_config_packet.bConfigurationValue = 1;
  82. usb_config_packet.iConfiguration = 0;
  83. usb_config_packet.bmAttributes = 0x40;
  84. usb_config_packet.MaxPower = 0;
  85. /*put packet in fifo */
  86. packet_pt = (unsigned char *)&usb_config_packet;
  87. break;
  88. case USB_DT_STRING:
  89. /*create packet */
  90. usb_string_packet.bLength = 2;
  91. usb_string_packet.bDescriptorType =
  92. USB_DT_STRING;
  93. usb_string_packet.wData[0] = 0x0094;
  94. /*put packet in fifo */
  95. packet_pt = (unsigned char *)&usb_string_packet;
  96. break;
  97. }
  98. /*put packet in fifo */
  99. for (i = 0; i < (setup_packet.length); i++) {
  100. *(unsigned char *)USB2D0_FIFO_0 = packet_pt[i];
  101. }
  102. /*give tx command */
  103. *(unsigned short *)USB2D0_INCSR0_8 = 0x0a;
  104. break;
  105. case USB_REQ_SET_ADDRESS:
  106. /*copy usb address */
  107. set_value = setup_packet.value;
  108. break;
  109. }
  110. }
  111. }
  112. void process_other(unsigned char usb2d0_intrusb)
  113. {
  114. /*check for sof */
  115. if (usb2d0_intrusb & 0x08) {
  116. /*printf("{USB device} - sof detected\n"); */
  117. }
  118. /*check for reset */
  119. if (usb2d0_intrusb & 0x04) {
  120. /*printf("{USB device} - reset detected\n"); */
  121. /*copy usb address of zero, need to do this when usb reset */
  122. set_value = 0;
  123. }
  124. if (usb2d0_intrusb & 0x02) {
  125. /*printf("{USB device} - resume detected\n"); */
  126. }
  127. if (usb2d0_intrusb & 0x01) {
  128. /*printf("{USB device} - suspend detected\n"); */
  129. }
  130. }
  131. int usbInt(void)
  132. {
  133. /*Must read these 2 registers and use values to clear interrupts. If you
  134. do not read them then the interrupt will not be cleared. If you do not
  135. use the variable the optimizer will not do a read. */
  136. volatile unsigned short usb2d0_intrin =
  137. *(unsigned short *)USB2D0_INTRIN_16;
  138. volatile unsigned char usb2d0_intrusb =
  139. *(unsigned char *)USB2D0_INTRUSB_8;
  140. /*check if there was an endpoint interrupt */
  141. if (usb2d0_intrin != 0) {
  142. process_endpoints(usb2d0_intrin);
  143. }
  144. /*check for other interrupts */
  145. if (usb2d0_intrusb != 0) {
  146. process_other(usb2d0_intrusb);
  147. }
  148. return 0;
  149. }
  150. void usb_dev_init()
  151. {
  152. #ifdef USB_2_0_DEVICE
  153. printf("USB 2.0 Device init\n");
  154. /*select 2.0 device */
  155. mtsdr(sdr_usb0, 0x0); /* 2.0 */
  156. /*usb dev init */
  157. *(unsigned char *)USB2D0_POWER_8 = 0xa1; /* 2.0 */
  158. #else
  159. printf("USB 1.1 Device init\n");
  160. /*select 1.1 device */
  161. mtsdr(sdr_usb0, 0x2); /* 1.1 */
  162. /*usb dev init */
  163. *(unsigned char *)USB2D0_POWER_8 = 0xc0; /* 1.1 */
  164. #endif
  165. /*enable interrupts */
  166. *(unsigned char *)USB2D0_INTRUSBE_8 = 0x0f;
  167. irq_install_handler(VECNUM_USBDEV, (interrupt_handler_t *) usbInt,
  168. NULL);
  169. }
  170. #endif /*CONFIG_440_EP */