usb.c 6.1 KB

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