usb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. /*
  9. * USB test
  10. *
  11. * The USB controller is tested in the local loopback mode.
  12. * It is configured so that endpoint 0 operates as host and endpoint 1
  13. * operates as function endpoint. After that an IN token transaction
  14. * is performed.
  15. * Refer to MPC850 User Manual, Section 32.11.1 USB Host Controller
  16. * Initialization Example.
  17. */
  18. #include <post.h>
  19. #if CONFIG_POST & CONFIG_SYS_POST_USB
  20. #include <commproc.h>
  21. #include <command.h>
  22. #define TOUT_LOOP 100
  23. #define PROFF_USB ((uint)0x0000)
  24. #define CPM_USB_EP0_BASE 0x0a00
  25. #define CPM_USB_EP1_BASE 0x0a20
  26. #define CPM_USB_DT0_BASE 0x0a80
  27. #define CPM_USB_DT1_BASE 0x0a90
  28. #define CPM_USB_DR0_BASE 0x0aa0
  29. #define CPM_USB_DR1_BASE 0x0ab0
  30. #define CPM_USB_RX0_BASE 0x0b00
  31. #define CPM_USB_RX1_BASE 0x0b08
  32. #define CPM_USB_TX0_BASE 0x0b20
  33. #define CPM_USB_TX1_BASE 0x0b28
  34. #define USB_EXPECT(x) if (!(x)) goto Done;
  35. typedef struct usb_param {
  36. ushort ep0ptr;
  37. ushort ep1ptr;
  38. ushort ep2ptr;
  39. ushort ep3ptr;
  40. uint rstate;
  41. uint rptr;
  42. ushort frame_n;
  43. ushort rbcnt;
  44. ushort rtemp;
  45. } usb_param_t;
  46. typedef struct usb_param_block {
  47. ushort rbase;
  48. ushort tbase;
  49. uchar rfcr;
  50. uchar tfcr;
  51. ushort mrblr;
  52. ushort rbptr;
  53. ushort tbptr;
  54. uint tstate;
  55. uint tptr;
  56. ushort tcrc;
  57. ushort tbcnt;
  58. uint res[2];
  59. } usb_param_block_t;
  60. typedef struct usb {
  61. uchar usmod;
  62. uchar usadr;
  63. uchar uscom;
  64. uchar res1;
  65. ushort usep[4];
  66. uchar res2[4];
  67. ushort usber;
  68. uchar res3[2];
  69. ushort usbmr;
  70. uchar res4;
  71. uchar usbs;
  72. uchar res5[8];
  73. } usb_t;
  74. int usb_post_test (int flags)
  75. {
  76. int res = -1;
  77. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  78. volatile cpm8xx_t *cp = &(im->im_cpm);
  79. volatile usb_param_t *pram_ptr;
  80. uint dpram;
  81. ushort DPRAM;
  82. volatile cbd_t *tx;
  83. volatile cbd_t *rx;
  84. volatile usb_t *usbr;
  85. volatile usb_param_block_t *ep0;
  86. volatile usb_param_block_t *ep1;
  87. int j;
  88. pram_ptr = (usb_param_t *) & (im->im_cpm.cp_dparam[PROFF_USB]);
  89. dpram = (uint) im->im_cpm.cp_dpmem;
  90. DPRAM = dpram;
  91. tx = (cbd_t *) (dpram + CPM_USB_TX0_BASE);
  92. rx = (cbd_t *) (dpram + CPM_USB_RX0_BASE);
  93. ep0 = (usb_param_block_t *) (dpram + CPM_USB_EP0_BASE);
  94. ep1 = (usb_param_block_t *) (dpram + CPM_USB_EP1_BASE);
  95. usbr = (usb_t *) & (im->im_cpm.cp_scc[0]);
  96. /* 01 */
  97. im->im_ioport.iop_padir &= ~(ushort) 0x0200;
  98. im->im_ioport.iop_papar |= (ushort) 0x0200;
  99. cp->cp_sicr &= ~0x000000FF;
  100. cp->cp_sicr |= 0x00000018;
  101. cp->cp_brgc4 = 0x00010001;
  102. /* 02 */
  103. im->im_ioport.iop_padir &= ~(ushort) 0x0002;
  104. im->im_ioport.iop_padir &= ~(ushort) 0x0001;
  105. im->im_ioport.iop_papar |= (ushort) 0x0002;
  106. im->im_ioport.iop_papar |= (ushort) 0x0001;
  107. /* 03 */
  108. im->im_ioport.iop_pcdir &= ~(ushort) 0x0020;
  109. im->im_ioport.iop_pcdir &= ~(ushort) 0x0010;
  110. im->im_ioport.iop_pcpar &= ~(ushort) 0x0020;
  111. im->im_ioport.iop_pcpar &= ~(ushort) 0x0010;
  112. im->im_ioport.iop_pcso |= (ushort) 0x0020;
  113. im->im_ioport.iop_pcso |= (ushort) 0x0010;
  114. /* 04 */
  115. im->im_ioport.iop_pcdir |= (ushort) 0x0200;
  116. im->im_ioport.iop_pcdir |= (ushort) 0x0100;
  117. im->im_ioport.iop_pcpar |= (ushort) 0x0200;
  118. im->im_ioport.iop_pcpar |= (ushort) 0x0100;
  119. /* 05 */
  120. pram_ptr->frame_n = 0;
  121. /* 06 */
  122. pram_ptr->ep0ptr = DPRAM + CPM_USB_EP0_BASE;
  123. pram_ptr->ep1ptr = DPRAM + CPM_USB_EP1_BASE;
  124. /* 07-10 */
  125. tx[0].cbd_sc = 0xB800;
  126. tx[0].cbd_datlen = 3;
  127. tx[0].cbd_bufaddr = dpram + CPM_USB_DT0_BASE;
  128. tx[1].cbd_sc = 0xBC80;
  129. tx[1].cbd_datlen = 3;
  130. tx[1].cbd_bufaddr = dpram + CPM_USB_DT1_BASE;
  131. rx[0].cbd_sc = 0xA000;
  132. rx[0].cbd_datlen = 0;
  133. rx[0].cbd_bufaddr = dpram + CPM_USB_DR0_BASE;
  134. rx[1].cbd_sc = 0xA000;
  135. rx[1].cbd_datlen = 0;
  136. rx[1].cbd_bufaddr = dpram + CPM_USB_DR1_BASE;
  137. /* 11-12 */
  138. *(volatile int *) (dpram + CPM_USB_DT0_BASE) = 0x69856000;
  139. *(volatile int *) (dpram + CPM_USB_DT1_BASE) = 0xABCD1234;
  140. *(volatile int *) (dpram + CPM_USB_DR0_BASE) = 0;
  141. *(volatile int *) (dpram + CPM_USB_DR1_BASE) = 0;
  142. /* 13-16 */
  143. ep0->rbase = DPRAM + CPM_USB_RX0_BASE;
  144. ep0->tbase = DPRAM + CPM_USB_TX0_BASE;
  145. ep0->rfcr = 0x18;
  146. ep0->tfcr = 0x18;
  147. ep0->mrblr = 0x100;
  148. ep0->rbptr = DPRAM + CPM_USB_RX0_BASE;
  149. ep0->tbptr = DPRAM + CPM_USB_TX0_BASE;
  150. ep0->tstate = 0;
  151. /* 17-20 */
  152. ep1->rbase = DPRAM + CPM_USB_RX1_BASE;
  153. ep1->tbase = DPRAM + CPM_USB_TX1_BASE;
  154. ep1->rfcr = 0x18;
  155. ep1->tfcr = 0x18;
  156. ep1->mrblr = 0x100;
  157. ep1->rbptr = DPRAM + CPM_USB_RX1_BASE;
  158. ep1->tbptr = DPRAM + CPM_USB_TX1_BASE;
  159. ep1->tstate = 0;
  160. /* 21-24 */
  161. usbr->usep[0] = 0x0000;
  162. usbr->usep[1] = 0x1100;
  163. usbr->usep[2] = 0x2200;
  164. usbr->usep[3] = 0x3300;
  165. /* 25 */
  166. usbr->usmod = 0x06;
  167. /* 26 */
  168. usbr->usadr = 0x05;
  169. /* 27 */
  170. usbr->uscom = 0;
  171. /* 28 */
  172. usbr->usmod |= 0x01;
  173. udelay (1);
  174. /* 29-30 */
  175. usbr->uscom = 0x80;
  176. usbr->uscom = 0x81;
  177. /* Wait for the data packet to be transmitted */
  178. for (j = 0; j < TOUT_LOOP; j++) {
  179. if (tx[1].cbd_sc & (ushort) 0x8000)
  180. udelay (1);
  181. else
  182. break;
  183. }
  184. USB_EXPECT (j < TOUT_LOOP);
  185. USB_EXPECT (tx[0].cbd_sc == 0x3800);
  186. USB_EXPECT (tx[0].cbd_datlen == 3);
  187. USB_EXPECT (tx[1].cbd_sc == 0x3C80);
  188. USB_EXPECT (tx[1].cbd_datlen == 3);
  189. USB_EXPECT (rx[0].cbd_sc == 0x2C00);
  190. USB_EXPECT (rx[0].cbd_datlen == 5);
  191. USB_EXPECT (*(volatile int *) (dpram + CPM_USB_DR0_BASE) ==
  192. 0xABCD122B);
  193. USB_EXPECT (*(volatile char *) (dpram + CPM_USB_DR0_BASE + 4) == 0x42);
  194. res = 0;
  195. Done:
  196. return res;
  197. }
  198. #endif /* CONFIG_POST & CONFIG_SYS_POST_USB */