usb_uhci.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * Part of this code has been derived from linux:
  3. * Universal Host Controller Interface driver for USB (take II).
  4. *
  5. * (c) 1999-2001 Georg Acher, acher@in.tum.de (executive slave) (base guitar)
  6. * Deti Fliegl, deti@fliegl.de (executive slave) (lead voice)
  7. * Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader)
  8. * Roman Weissgaerber, weissg@vienna.at (virt root hub) (studio porter)
  9. * (c) 2000 Yggdrasil Computing, Inc. (port of new PCI interface support
  10. * from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  11. * (C) 2000 David Brownell, david-b@pacbell.net (usb-ohci.c)
  12. *
  13. * HW-initalization based on material of
  14. *
  15. * (C) Copyright 1999 Linus Torvalds
  16. * (C) Copyright 1999 Johannes Erdfelt
  17. * (C) Copyright 1999 Randy Dunlap
  18. * (C) Copyright 1999 Gregory P. Smith
  19. *
  20. *
  21. * Adapted for U-Boot:
  22. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  23. *
  24. * SPDX-License-Identifier: GPL-2.0+
  25. */
  26. /**********************************************************************
  27. * How it works:
  28. * -------------
  29. * The framelist / Transfer descriptor / Queue Heads are similar like
  30. * in the linux usb_uhci.c.
  31. *
  32. * During initialization, the following skeleton is allocated in init_skel:
  33. *
  34. * framespecific | common chain
  35. *
  36. * framelist[]
  37. * [ 0 ]-----> TD ---------\
  38. * [ 1 ]-----> TD ----------> TD ------> QH -------> QH -------> QH ---> NULL
  39. * ... TD ---------/
  40. * [1023]-----> TD --------/
  41. *
  42. * ^^ ^^ ^^ ^^ ^^
  43. * 7 TDs for 1 TD for Start of Start of End Chain
  44. * INT (2-128ms) 1ms-INT CTRL Chain BULK Chain
  45. *
  46. *
  47. * Since this is a bootloader, the isochronous transfer descriptor have been removed.
  48. *
  49. * Interrupt Transfers.
  50. * --------------------
  51. * For Interrupt transfers USB_MAX_TEMP_INT_TD Transfer descriptor are available. They
  52. * will be inserted after the appropriate (depending the interval setting) skeleton TD.
  53. * If an interrupt has been detected the dev->irqhandler is called. The status and number
  54. * of transferred bytes is stored in dev->irq_status resp. dev->irq_act_len. If the
  55. * dev->irqhandler returns 0, the interrupt TD is removed and disabled. If an 1 is returned,
  56. * the interrupt TD will be reactivated.
  57. *
  58. * Control Transfers
  59. * -----------------
  60. * Control Transfers are issued by filling the tmp_td with the appropriate data and connect
  61. * them to the qh_cntrl queue header. Before other control/bulk transfers can be issued,
  62. * the programm has to wait for completion. This does not allows asynchronous data transfer.
  63. *
  64. * Bulk Transfers
  65. * --------------
  66. * Bulk Transfers are issued by filling the tmp_td with the appropriate data and connect
  67. * them to the qh_bulk queue header. Before other control/bulk transfers can be issued,
  68. * the programm has to wait for completion. This does not allows asynchronous data transfer.
  69. *
  70. *
  71. */
  72. #include <common.h>
  73. #include <pci.h>
  74. #ifdef CONFIG_USB_UHCI
  75. #include <usb.h>
  76. #include "usb_uhci.h"
  77. #define USB_MAX_TEMP_TD 128 /* number of temporary TDs for bulk and control transfers */
  78. #define USB_MAX_TEMP_INT_TD 32 /* number of temporary TDs for Interrupt transfers */
  79. #undef USB_UHCI_DEBUG
  80. #ifdef USB_UHCI_DEBUG
  81. #define USB_UHCI_PRINTF(fmt,args...) printf (fmt ,##args)
  82. #else
  83. #define USB_UHCI_PRINTF(fmt,args...)
  84. #endif
  85. static int irqvec = -1; /* irq vector, if -1 uhci is stopped / reseted */
  86. unsigned int usb_base_addr; /* base address */
  87. static uhci_td_t td_int[8]; /* Interrupt Transfer descriptors */
  88. static uhci_qh_t qh_cntrl; /* control Queue Head */
  89. static uhci_qh_t qh_bulk; /* bulk Queue Head */
  90. static uhci_qh_t qh_end; /* end Queue Head */
  91. static uhci_td_t td_last; /* last TD (linked with end chain) */
  92. /* temporary tds */
  93. static uhci_td_t tmp_td[USB_MAX_TEMP_TD]; /* temporary bulk/control td's */
  94. static uhci_td_t tmp_int_td[USB_MAX_TEMP_INT_TD]; /* temporary interrupt td's */
  95. static unsigned long framelist[1024] __attribute__ ((aligned (0x1000))); /* frame list */
  96. static struct virt_root_hub rh; /* struct for root hub */
  97. /**********************************************************************
  98. * some forward decleration
  99. */
  100. int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  101. void *buffer, int transfer_len,struct devrequest *setup);
  102. /* fill a td with the approproiate data. Link, status, info and buffer
  103. * are used by the USB controller itselfes, dev is used to identify the
  104. * "connected" device
  105. */
  106. void usb_fill_td(uhci_td_t* td,unsigned long link,unsigned long status,
  107. unsigned long info, unsigned long buffer, unsigned long dev)
  108. {
  109. td->link=swap_32(link);
  110. td->status=swap_32(status);
  111. td->info=swap_32(info);
  112. td->buffer=swap_32(buffer);
  113. td->dev_ptr=dev;
  114. }
  115. /* fill a qh with the approproiate data. Head and element are used by the USB controller
  116. * itselfes. As soon as a valid dev_ptr is filled, a td chain is connected to the qh.
  117. * Please note, that after completion of the td chain, the entry element is removed /
  118. * marked invalid by the USB controller.
  119. */
  120. void usb_fill_qh(uhci_qh_t* qh,unsigned long head,unsigned long element)
  121. {
  122. qh->head=swap_32(head);
  123. qh->element=swap_32(element);
  124. qh->dev_ptr=0L;
  125. }
  126. /* get the status of a td->status
  127. */
  128. unsigned long usb_uhci_td_stat(unsigned long status)
  129. {
  130. unsigned long result=0;
  131. result |= (status & TD_CTRL_NAK) ? USB_ST_NAK_REC : 0;
  132. result |= (status & TD_CTRL_STALLED) ? USB_ST_STALLED : 0;
  133. result |= (status & TD_CTRL_DBUFERR) ? USB_ST_BUF_ERR : 0;
  134. result |= (status & TD_CTRL_BABBLE) ? USB_ST_BABBLE_DET : 0;
  135. result |= (status & TD_CTRL_CRCTIMEO) ? USB_ST_CRC_ERR : 0;
  136. result |= (status & TD_CTRL_BITSTUFF) ? USB_ST_BIT_ERR : 0;
  137. result |= (status & TD_CTRL_ACTIVE) ? USB_ST_NOT_PROC : 0;
  138. return result;
  139. }
  140. /* get the status and the transferred len of a td chain.
  141. * called from the completion handler
  142. */
  143. int usb_get_td_status(uhci_td_t *td,struct usb_device *dev)
  144. {
  145. unsigned long temp,info;
  146. unsigned long stat;
  147. uhci_td_t *mytd=td;
  148. if(dev->devnum==rh.devnum)
  149. return 0;
  150. dev->act_len=0;
  151. stat=0;
  152. do {
  153. temp=swap_32((unsigned long)mytd->status);
  154. stat=usb_uhci_td_stat(temp);
  155. info=swap_32((unsigned long)mytd->info);
  156. if(((info & 0xff)!= USB_PID_SETUP) &&
  157. (((info >> 21) & 0x7ff)!= 0x7ff) &&
  158. (temp & 0x7FF)!=0x7ff)
  159. { /* if not setup and not null data pack */
  160. dev->act_len+=(temp & 0x7FF) + 1; /* the transferred len is act_len + 1 */
  161. }
  162. if(stat) { /* status no ok */
  163. dev->status=stat;
  164. return -1;
  165. }
  166. temp=swap_32((unsigned long)mytd->link);
  167. mytd=(uhci_td_t *)(temp & 0xfffffff0);
  168. }while((temp & 0x1)==0); /* process all TDs */
  169. dev->status=stat;
  170. return 0; /* Ok */
  171. }
  172. /*-------------------------------------------------------------------
  173. * LOW LEVEL STUFF
  174. * assembles QHs und TDs for control, bulk and iso
  175. *-------------------------------------------------------------------*/
  176. /* Submits a control message. That is a Setup, Data and Status transfer.
  177. * Routine does not wait for completion.
  178. */
  179. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  180. int transfer_len,struct devrequest *setup)
  181. {
  182. unsigned long destination, status;
  183. int maxsze = usb_maxpacket(dev, pipe);
  184. unsigned long dataptr;
  185. int len;
  186. int pktsze;
  187. int i=0;
  188. if (!maxsze) {
  189. USB_UHCI_PRINTF("uhci_submit_control_urb: pipesize for pipe %lx is zero\n", pipe);
  190. return -1;
  191. }
  192. if(((pipe>>8)&0x7f)==rh.devnum) {
  193. /* this is the root hub -> redirect it */
  194. return uhci_submit_rh_msg(dev,pipe,buffer,transfer_len,setup);
  195. }
  196. USB_UHCI_PRINTF("uhci_submit_control start len %x, maxsize %x\n",transfer_len,maxsze);
  197. /* The "pipe" thing contains the destination in bits 8--18 */
  198. destination = (pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; /* Setup stage */
  199. /* 3 errors */
  200. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
  201. /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD); */
  202. /* Build the TD for the control request, try forever, 8 bytes of data */
  203. usb_fill_td(&tmp_td[i],UHCI_PTR_TERM ,status, destination | (7 << 21),(unsigned long)setup,(unsigned long)dev);
  204. #if 0
  205. {
  206. char *sp=(char *)setup;
  207. printf("SETUP to pipe %lx: %x %x %x %x %x %x %x %x\n", pipe,
  208. sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
  209. }
  210. #endif
  211. dataptr = (unsigned long)buffer;
  212. len=transfer_len;
  213. /* If direction is "send", change the frame from SETUP (0x2D)
  214. to OUT (0xE1). Else change it from SETUP to IN (0x69). */
  215. destination = (pipe & PIPE_DEVEP_MASK) | ((pipe & USB_DIR_IN)==0 ? USB_PID_OUT : USB_PID_IN);
  216. while (len > 0) {
  217. /* data stage */
  218. pktsze = len;
  219. i++;
  220. if (pktsze > maxsze)
  221. pktsze = maxsze;
  222. destination ^= 1 << TD_TOKEN_TOGGLE; /* toggle DATA0/1 */
  223. usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, destination | ((pktsze - 1) << 21),dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */
  224. tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]);
  225. dataptr += pktsze;
  226. len -= pktsze;
  227. }
  228. /* Build the final TD for control status */
  229. /* It's only IN if the pipe is out AND we aren't expecting data */
  230. destination &= ~UHCI_PID;
  231. if (((pipe & USB_DIR_IN)==0) || (transfer_len == 0))
  232. destination |= USB_PID_IN;
  233. else
  234. destination |= USB_PID_OUT;
  235. destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
  236. i++;
  237. status &=~TD_CTRL_SPD;
  238. /* no limit on errors on final packet , 0 bytes of data */
  239. usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),0,(unsigned long)dev);
  240. tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]); /* queue status td */
  241. /* usb_show_td(i+1);*/
  242. USB_UHCI_PRINTF("uhci_submit_control end (%d tmp_tds used)\n",i);
  243. /* first mark the control QH element terminated */
  244. qh_cntrl.element=0xffffffffL;
  245. /* set qh active */
  246. qh_cntrl.dev_ptr=(unsigned long)dev;
  247. /* fill in tmp_td_chain */
  248. qh_cntrl.element=swap_32((unsigned long)&tmp_td[0]);
  249. return 0;
  250. }
  251. /*-------------------------------------------------------------------
  252. * Prepare TDs for bulk transfers.
  253. */
  254. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len)
  255. {
  256. unsigned long destination, status,info;
  257. unsigned long dataptr;
  258. int maxsze = usb_maxpacket(dev, pipe);
  259. int len;
  260. int i=0;
  261. if(transfer_len < 0) {
  262. printf("Negative transfer length in submit_bulk\n");
  263. return -1;
  264. }
  265. if (!maxsze)
  266. return -1;
  267. /* The "pipe" thing contains the destination in bits 8--18. */
  268. destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
  269. /* 3 errors */
  270. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
  271. /* ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27); */
  272. /* Build the TDs for the bulk request */
  273. len = transfer_len;
  274. dataptr = (unsigned long)buffer;
  275. do {
  276. int pktsze = len;
  277. if (pktsze > maxsze)
  278. pktsze = maxsze;
  279. /* pktsze bytes of data */
  280. info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
  281. (usb_gettoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
  282. if((len-pktsze)==0)
  283. status |= TD_CTRL_IOC; /* last one generates INT */
  284. usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, info,dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */
  285. if(i>0)
  286. tmp_td[i-1].link=swap_32((unsigned long)&tmp_td[i]);
  287. i++;
  288. dataptr += pktsze;
  289. len -= pktsze;
  290. usb_dotoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
  291. } while (len > 0);
  292. /* first mark the bulk QH element terminated */
  293. qh_bulk.element=0xffffffffL;
  294. /* set qh active */
  295. qh_bulk.dev_ptr=(unsigned long)dev;
  296. /* fill in tmp_td_chain */
  297. qh_bulk.element=swap_32((unsigned long)&tmp_td[0]);
  298. return 0;
  299. }
  300. /* search a free interrupt td
  301. */
  302. uhci_td_t *uhci_alloc_int_td(void)
  303. {
  304. int i;
  305. for(i=0;i<USB_MAX_TEMP_INT_TD;i++) {
  306. if(tmp_int_td[i].dev_ptr==0) /* no device assigned -> free TD */
  307. return &tmp_int_td[i];
  308. }
  309. return NULL;
  310. }
  311. #if 0
  312. void uhci_show_temp_int_td(void)
  313. {
  314. int i;
  315. for(i=0;i<USB_MAX_TEMP_INT_TD;i++) {
  316. if((tmp_int_td[i].dev_ptr&0x01)!=0x1L) /* no device assigned -> free TD */
  317. printf("temp_td %d is assigned to dev %lx\n",i,tmp_int_td[i].dev_ptr);
  318. }
  319. printf("all others temp_tds are free\n");
  320. }
  321. #endif
  322. /*-------------------------------------------------------------------
  323. * submits USB interrupt (ie. polling ;-)
  324. */
  325. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len, int interval)
  326. {
  327. int nint, n;
  328. unsigned long status, destination;
  329. unsigned long info,tmp;
  330. uhci_td_t *mytd;
  331. if (interval < 0 || interval >= 256)
  332. return -1;
  333. if (interval == 0)
  334. nint = 0;
  335. else {
  336. for (nint = 0, n = 1; nint <= 8; nint++, n += n) /* round interval down to 2^n */
  337. {
  338. if(interval < n) {
  339. interval = n / 2;
  340. break;
  341. }
  342. }
  343. nint--;
  344. }
  345. USB_UHCI_PRINTF("Rounded interval to %i, chain %i\n", interval, nint);
  346. mytd=uhci_alloc_int_td();
  347. if(mytd==NULL) {
  348. printf("No free INT TDs found\n");
  349. return -1;
  350. }
  351. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC | (3 << 27);
  352. /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
  353. */
  354. destination =(pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe) | (((transfer_len - 1) & 0x7ff) << 21);
  355. info = destination | (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << TD_TOKEN_TOGGLE);
  356. tmp = swap_32(td_int[nint].link);
  357. usb_fill_td(mytd,tmp,status, info,(unsigned long)buffer,(unsigned long)dev);
  358. /* Link it */
  359. tmp = swap_32((unsigned long)mytd);
  360. td_int[nint].link=tmp;
  361. usb_dotoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
  362. return 0;
  363. }
  364. /**********************************************************************
  365. * Low Level functions
  366. */
  367. void reset_hc(void)
  368. {
  369. /* Global reset for 100ms */
  370. out16r( usb_base_addr + USBPORTSC1,0x0204);
  371. out16r( usb_base_addr + USBPORTSC2,0x0204);
  372. out16r( usb_base_addr + USBCMD,USBCMD_GRESET | USBCMD_RS);
  373. /* Turn off all interrupts */
  374. out16r(usb_base_addr + USBINTR,0);
  375. mdelay(50);
  376. out16r( usb_base_addr + USBCMD,0);
  377. mdelay(10);
  378. }
  379. void start_hc(void)
  380. {
  381. int timeout = 1000;
  382. while(in16r(usb_base_addr + USBCMD) & USBCMD_HCRESET) {
  383. if (!--timeout) {
  384. printf("USBCMD_HCRESET timed out!\n");
  385. break;
  386. }
  387. }
  388. /* Turn on all interrupts */
  389. out16r(usb_base_addr + USBINTR,USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP);
  390. /* Start at frame 0 */
  391. out16r(usb_base_addr + USBFRNUM,0);
  392. /* set Framebuffer base address */
  393. out32r(usb_base_addr+USBFLBASEADD,(unsigned long)&framelist);
  394. /* Run and mark it configured with a 64-byte max packet */
  395. out16r(usb_base_addr + USBCMD,USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  396. }
  397. /* Initialize the skeleton
  398. */
  399. void usb_init_skel(void)
  400. {
  401. unsigned long temp;
  402. int n;
  403. for(n=0;n<USB_MAX_TEMP_INT_TD;n++)
  404. tmp_int_td[n].dev_ptr=0L; /* no devices connected */
  405. /* last td */
  406. usb_fill_td(&td_last,UHCI_PTR_TERM,TD_CTRL_IOC ,0,0,0L);
  407. /* usb_fill_td(&td_last,UHCI_PTR_TERM,0,0,0); */
  408. /* End Queue Header */
  409. usb_fill_qh(&qh_end,UHCI_PTR_TERM,(unsigned long)&td_last);
  410. /* Bulk Queue Header */
  411. temp=(unsigned long)&qh_end;
  412. usb_fill_qh(&qh_bulk,temp | UHCI_PTR_QH,UHCI_PTR_TERM);
  413. /* Control Queue Header */
  414. temp=(unsigned long)&qh_bulk;
  415. usb_fill_qh(&qh_cntrl, temp | UHCI_PTR_QH,UHCI_PTR_TERM);
  416. /* 1ms Interrupt td */
  417. temp=(unsigned long)&qh_cntrl;
  418. usb_fill_td(&td_int[0],temp | UHCI_PTR_QH,0,0,0,0L);
  419. temp=(unsigned long)&td_int[0];
  420. for(n=1; n<8; n++)
  421. usb_fill_td(&td_int[n],temp,0,0,0,0L);
  422. for (n = 0; n < 1024; n++) {
  423. /* link all framelist pointers to one of the interrupts */
  424. int m, o;
  425. if ((n&127)==127)
  426. framelist[n]= swap_32((unsigned long)&td_int[0]);
  427. else
  428. for (o = 1, m = 2; m <= 128; o++, m += m)
  429. if ((n & (m - 1)) == ((m - 1) / 2))
  430. framelist[n]= swap_32((unsigned long)&td_int[o]);
  431. }
  432. }
  433. /* check the common skeleton for completed transfers, and update the status
  434. * of the "connected" device. Called from the IRQ routine.
  435. */
  436. void usb_check_skel(void)
  437. {
  438. struct usb_device *dev;
  439. /* start with the control qh */
  440. if(qh_cntrl.dev_ptr!=0) /* it's a device assigned check if this caused IRQ */
  441. {
  442. dev=(struct usb_device *)qh_cntrl.dev_ptr;
  443. usb_get_td_status(&tmp_td[0],dev); /* update status */
  444. if(!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
  445. qh_cntrl.dev_ptr=0;
  446. }
  447. }
  448. /* now process the bulk */
  449. if(qh_bulk.dev_ptr!=0) /* it's a device assigned check if this caused IRQ */
  450. {
  451. dev=(struct usb_device *)qh_bulk.dev_ptr;
  452. usb_get_td_status(&tmp_td[0],dev); /* update status */
  453. if(!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
  454. qh_bulk.dev_ptr=0;
  455. }
  456. }
  457. }
  458. /* check the interrupt chain, ubdate the status of the appropriate device,
  459. * call the appropriate irqhandler and reactivate the TD if the irqhandler
  460. * returns with 1
  461. */
  462. void usb_check_int_chain(void)
  463. {
  464. int i,res;
  465. unsigned long link,status;
  466. struct usb_device *dev;
  467. uhci_td_t *td,*prevtd;
  468. for(i=0;i<8;i++) {
  469. prevtd = &td_int[i]; /* the first previous td is the skeleton td */
  470. link=swap_32(td_int[i].link) & 0xfffffff0; /* next in chain */
  471. td=(uhci_td_t *)link; /* assign it */
  472. /* all interrupt TDs are finally linked to the td_int[0].
  473. * so we process all until we find the td_int[0].
  474. * if int0 chain points to a QH, we're also done
  475. */
  476. while(((i>0) && (link != (unsigned long)&td_int[0])) ||
  477. ((i==0) && !(swap_32(td->link) & UHCI_PTR_QH)))
  478. {
  479. /* check if a device is assigned with this td */
  480. status=swap_32(td->status);
  481. if((td->dev_ptr!=0L) && !(status & TD_CTRL_ACTIVE)) {
  482. /* td is not active and a device is assigned -> call irqhandler */
  483. dev=(struct usb_device *)td->dev_ptr;
  484. dev->irq_act_len=((status & 0x7FF)==0x7FF) ? 0 : (status & 0x7FF) + 1; /* transferred length */
  485. dev->irq_status=usb_uhci_td_stat(status); /* get status */
  486. res=dev->irq_handle(dev); /* call irqhandler */
  487. if(res==1) {
  488. /* reactivate */
  489. status|=TD_CTRL_ACTIVE;
  490. td->status=swap_32(status);
  491. prevtd=td; /* previous td = this td */
  492. }
  493. else {
  494. prevtd->link=td->link; /* link previous td directly to the nex td -> unlinked */
  495. /* remove device pointer */
  496. td->dev_ptr=0L;
  497. }
  498. } /* if we call the irq handler */
  499. link=swap_32(td->link) & 0xfffffff0; /* next in chain */
  500. td=(uhci_td_t *)link; /* assign it */
  501. } /* process all td in this int chain */
  502. } /* next interrupt chain */
  503. }
  504. /* usb interrupt service routine.
  505. */
  506. void handle_usb_interrupt(void)
  507. {
  508. unsigned short status;
  509. /*
  510. * Read the interrupt status, and write it back to clear the
  511. * interrupt cause
  512. */
  513. status = in16r(usb_base_addr + USBSTS);
  514. if (!status) /* shared interrupt, not mine */
  515. return;
  516. if (status != 1) {
  517. /* remove host controller halted state */
  518. if ((status&0x20) && ((in16r(usb_base_addr+USBCMD) && USBCMD_RS)==0)) {
  519. out16r(usb_base_addr + USBCMD, USBCMD_RS | in16r(usb_base_addr + USBCMD));
  520. }
  521. }
  522. usb_check_int_chain(); /* call interrupt handlers for int tds */
  523. usb_check_skel(); /* call completion handler for common transfer routines */
  524. out16r(usb_base_addr+USBSTS,status);
  525. }
  526. /* init uhci
  527. */
  528. int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
  529. {
  530. unsigned char temp;
  531. int busdevfunc;
  532. busdevfunc=pci_find_device(USB_UHCI_VEND_ID,USB_UHCI_DEV_ID,0); /* get PCI Device ID */
  533. if(busdevfunc==-1) {
  534. printf("Error USB UHCI (%04X,%04X) not found\n",USB_UHCI_VEND_ID,USB_UHCI_DEV_ID);
  535. return -1;
  536. }
  537. pci_read_config_byte(busdevfunc,PCI_INTERRUPT_LINE,&temp);
  538. irqvec = temp;
  539. irq_free_handler(irqvec);
  540. USB_UHCI_PRINTF("Interrupt Line = %d, is %d\n",irqvec);
  541. pci_read_config_byte(busdevfunc,PCI_INTERRUPT_PIN,&temp);
  542. USB_UHCI_PRINTF("Interrupt Pin = %ld\n",temp);
  543. pci_read_config_dword(busdevfunc,PCI_BASE_ADDRESS_4,&usb_base_addr);
  544. USB_UHCI_PRINTF("IO Base Address = 0x%lx\n",usb_base_addr);
  545. usb_base_addr&=0xFFFFFFF0;
  546. usb_base_addr+=CONFIG_SYS_ISA_IO_BASE_ADDRESS;
  547. rh.devnum = 0;
  548. usb_init_skel();
  549. reset_hc();
  550. start_hc();
  551. irq_install_handler(irqvec, (interrupt_handler_t *)handle_usb_interrupt, NULL);
  552. return 0;
  553. }
  554. /* stop uhci
  555. */
  556. int usb_lowlevel_stop(int index)
  557. {
  558. if(irqvec==-1)
  559. return 1;
  560. irq_free_handler(irqvec);
  561. reset_hc();
  562. irqvec = -1;
  563. return 0;
  564. }
  565. /*******************************************************************************************
  566. * Virtual Root Hub
  567. * Since the uhci does not have a real HUB, we simulate one ;-)
  568. */
  569. #undef USB_RH_DEBUG
  570. #ifdef USB_RH_DEBUG
  571. #define USB_RH_PRINTF(fmt,args...) printf (fmt ,##args)
  572. static void usb_display_wValue(unsigned short wValue,unsigned short wIndex);
  573. static void usb_display_Req(unsigned short req);
  574. #else
  575. #define USB_RH_PRINTF(fmt,args...)
  576. static void usb_display_wValue(unsigned short wValue,unsigned short wIndex) {}
  577. static void usb_display_Req(unsigned short req) {}
  578. #endif
  579. #define WANT_USB_ROOT_HUB_HUB_DES
  580. #include <usbroothubdes.h>
  581. #undef WANT_USB_ROOT_HUB_HUB_DES
  582. /*
  583. * Root Hub Control Pipe (interrupt Pipes are not supported)
  584. */
  585. int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len,struct devrequest *cmd)
  586. {
  587. void *data = buffer;
  588. int leni = transfer_len;
  589. int len = 0;
  590. int status = 0;
  591. int stat = 0;
  592. int i;
  593. unsigned short cstatus;
  594. unsigned short bmRType_bReq;
  595. unsigned short wValue;
  596. unsigned short wIndex;
  597. unsigned short wLength;
  598. if (usb_pipeint(pipe)) {
  599. printf("Root-Hub submit IRQ: NOT implemented\n");
  600. #if 0
  601. uhci->rh.urb = urb;
  602. uhci->rh.send = 1;
  603. uhci->rh.interval = urb->interval;
  604. rh_init_int_timer (urb);
  605. #endif
  606. return 0;
  607. }
  608. bmRType_bReq = cmd->requesttype | cmd->request << 8;
  609. wValue = swap_16(cmd->value);
  610. wIndex = swap_16(cmd->index);
  611. wLength = swap_16(cmd->length);
  612. usb_display_Req(bmRType_bReq);
  613. for (i = 0; i < 8; i++)
  614. rh.c_p_r[i] = 0;
  615. USB_RH_PRINTF("Root-Hub: adr: %2x cmd(%1x): %02x%02x %04x %04x %04x\n",
  616. dev->devnum, 8, cmd->requesttype,cmd->request, wValue, wIndex, wLength);
  617. switch (bmRType_bReq) {
  618. /* Request Destination:
  619. without flags: Device,
  620. RH_INTERFACE: interface,
  621. RH_ENDPOINT: endpoint,
  622. RH_CLASS means HUB here,
  623. RH_OTHER | RH_CLASS almost ever means HUB_PORT here
  624. */
  625. case RH_GET_STATUS:
  626. *(unsigned short *) data = swap_16(1);
  627. len=2;
  628. break;
  629. case RH_GET_STATUS | RH_INTERFACE:
  630. *(unsigned short *) data = swap_16(0);
  631. len=2;
  632. break;
  633. case RH_GET_STATUS | RH_ENDPOINT:
  634. *(unsigned short *) data = swap_16(0);
  635. len=2;
  636. break;
  637. case RH_GET_STATUS | RH_CLASS:
  638. *(unsigned long *) data = swap_32(0);
  639. len=4;
  640. break; /* hub power ** */
  641. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  642. status = in16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1));
  643. cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
  644. ((status & USBPORTSC_PEC) >> (3 - 1)) |
  645. (rh.c_p_r[wIndex - 1] << (0 + 4));
  646. status = (status & USBPORTSC_CCS) |
  647. ((status & USBPORTSC_PE) >> (2 - 1)) |
  648. ((status & USBPORTSC_SUSP) >> (12 - 2)) |
  649. ((status & USBPORTSC_PR) >> (9 - 4)) |
  650. (1 << 8) | /* power on ** */
  651. ((status & USBPORTSC_LSDA) << (-8 + 9));
  652. *(unsigned short *) data = swap_16(status);
  653. *(unsigned short *) (data + 2) = swap_16(cstatus);
  654. len=4;
  655. break;
  656. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  657. switch (wValue) {
  658. case (RH_ENDPOINT_STALL):
  659. len=0;
  660. break;
  661. }
  662. break;
  663. case RH_CLEAR_FEATURE | RH_CLASS:
  664. switch (wValue) {
  665. case (RH_C_HUB_OVER_CURRENT):
  666. len=0; /* hub power over current ** */
  667. break;
  668. }
  669. break;
  670. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  671. usb_display_wValue(wValue,wIndex);
  672. switch (wValue) {
  673. case (RH_PORT_ENABLE):
  674. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  675. status = (status & 0xfff5) & ~USBPORTSC_PE;
  676. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  677. len=0;
  678. break;
  679. case (RH_PORT_SUSPEND):
  680. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  681. status = (status & 0xfff5) & ~USBPORTSC_SUSP;
  682. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  683. len=0;
  684. break;
  685. case (RH_PORT_POWER):
  686. len=0; /* port power ** */
  687. break;
  688. case (RH_C_PORT_CONNECTION):
  689. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  690. status = (status & 0xfff5) | USBPORTSC_CSC;
  691. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  692. len=0;
  693. break;
  694. case (RH_C_PORT_ENABLE):
  695. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  696. status = (status & 0xfff5) | USBPORTSC_PEC;
  697. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  698. len=0;
  699. break;
  700. case (RH_C_PORT_SUSPEND):
  701. /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
  702. len=0;
  703. break;
  704. case (RH_C_PORT_OVER_CURRENT):
  705. len=0;
  706. break;
  707. case (RH_C_PORT_RESET):
  708. rh.c_p_r[wIndex - 1] = 0;
  709. len=0;
  710. break;
  711. }
  712. break;
  713. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  714. usb_display_wValue(wValue,wIndex);
  715. switch (wValue) {
  716. case (RH_PORT_SUSPEND):
  717. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  718. status = (status & 0xfff5) | USBPORTSC_SUSP;
  719. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  720. len=0;
  721. break;
  722. case (RH_PORT_RESET):
  723. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  724. status = (status & 0xfff5) | USBPORTSC_PR;
  725. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  726. mdelay(10);
  727. status = (status & 0xfff5) & ~USBPORTSC_PR;
  728. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  729. udelay(10);
  730. status = (status & 0xfff5) | USBPORTSC_PE;
  731. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  732. mdelay(10);
  733. status = (status & 0xfff5) | 0xa;
  734. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  735. len=0;
  736. break;
  737. case (RH_PORT_POWER):
  738. len=0; /* port power ** */
  739. break;
  740. case (RH_PORT_ENABLE):
  741. status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
  742. status = (status & 0xfff5) | USBPORTSC_PE;
  743. out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
  744. len=0;
  745. break;
  746. }
  747. break;
  748. case RH_SET_ADDRESS:
  749. rh.devnum = wValue;
  750. len=0;
  751. break;
  752. case RH_GET_DESCRIPTOR:
  753. switch ((wValue & 0xff00) >> 8) {
  754. case (0x01): /* device descriptor */
  755. i=sizeof(root_hub_config_des);
  756. status=i > wLength ? wLength : i;
  757. len = leni > status ? status : leni;
  758. memcpy (data, root_hub_dev_des, len);
  759. break;
  760. case (0x02): /* configuration descriptor */
  761. i=sizeof(root_hub_config_des);
  762. status=i > wLength ? wLength : i;
  763. len = leni > status ? status : leni;
  764. memcpy (data, root_hub_config_des, len);
  765. break;
  766. case (0x03): /*string descriptors */
  767. if(wValue==0x0300) {
  768. i=sizeof(root_hub_str_index0);
  769. status = i > wLength ? wLength : i;
  770. len = leni > status ? status : leni;
  771. memcpy (data, root_hub_str_index0, len);
  772. break;
  773. }
  774. if(wValue==0x0301) {
  775. i=sizeof(root_hub_str_index1);
  776. status = i > wLength ? wLength : i;
  777. len = leni > status ? status : leni;
  778. memcpy (data, root_hub_str_index1, len);
  779. break;
  780. }
  781. stat = USB_ST_STALLED;
  782. }
  783. break;
  784. case RH_GET_DESCRIPTOR | RH_CLASS:
  785. root_hub_hub_des[2] = 2;
  786. i=sizeof(root_hub_hub_des);
  787. status= i > wLength ? wLength : i;
  788. len = leni > status ? status : leni;
  789. memcpy (data, root_hub_hub_des, len);
  790. break;
  791. case RH_GET_CONFIGURATION:
  792. *(unsigned char *) data = 0x01;
  793. len = 1;
  794. break;
  795. case RH_SET_CONFIGURATION:
  796. len=0;
  797. break;
  798. default:
  799. stat = USB_ST_STALLED;
  800. }
  801. USB_RH_PRINTF("Root-Hub stat %lx port1: %x port2: %x\n\n",stat,
  802. in16r(usb_base_addr + USBPORTSC1), in16r(usb_base_addr + USBPORTSC2));
  803. dev->act_len=len;
  804. dev->status=stat;
  805. return stat;
  806. }
  807. /********************************************************************************
  808. * Some Debug Routines
  809. */
  810. #ifdef USB_RH_DEBUG
  811. static void usb_display_Req(unsigned short req)
  812. {
  813. USB_RH_PRINTF("- Root-Hub Request: ");
  814. switch (req) {
  815. case RH_GET_STATUS:
  816. USB_RH_PRINTF("Get Status ");
  817. break;
  818. case RH_GET_STATUS | RH_INTERFACE:
  819. USB_RH_PRINTF("Get Status Interface ");
  820. break;
  821. case RH_GET_STATUS | RH_ENDPOINT:
  822. USB_RH_PRINTF("Get Status Endpoint ");
  823. break;
  824. case RH_GET_STATUS | RH_CLASS:
  825. USB_RH_PRINTF("Get Status Class");
  826. break; /* hub power ** */
  827. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  828. USB_RH_PRINTF("Get Status Class Others");
  829. break;
  830. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  831. USB_RH_PRINTF("Clear Feature Endpoint ");
  832. break;
  833. case RH_CLEAR_FEATURE | RH_CLASS:
  834. USB_RH_PRINTF("Clear Feature Class ");
  835. break;
  836. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  837. USB_RH_PRINTF("Clear Feature Other Class ");
  838. break;
  839. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  840. USB_RH_PRINTF("Set Feature Other Class ");
  841. break;
  842. case RH_SET_ADDRESS:
  843. USB_RH_PRINTF("Set Address ");
  844. break;
  845. case RH_GET_DESCRIPTOR:
  846. USB_RH_PRINTF("Get Descriptor ");
  847. break;
  848. case RH_GET_DESCRIPTOR | RH_CLASS:
  849. USB_RH_PRINTF("Get Descriptor Class ");
  850. break;
  851. case RH_GET_CONFIGURATION:
  852. USB_RH_PRINTF("Get Configuration ");
  853. break;
  854. case RH_SET_CONFIGURATION:
  855. USB_RH_PRINTF("Get Configuration ");
  856. break;
  857. default:
  858. USB_RH_PRINTF("****UNKNOWN**** 0x%04X ",req);
  859. }
  860. USB_RH_PRINTF("\n");
  861. }
  862. static void usb_display_wValue(unsigned short wValue,unsigned short wIndex)
  863. {
  864. switch (wValue) {
  865. case (RH_PORT_ENABLE):
  866. USB_RH_PRINTF("Root-Hub: Enable Port %d\n",wIndex);
  867. break;
  868. case (RH_PORT_SUSPEND):
  869. USB_RH_PRINTF("Root-Hub: Suspend Port %d\n",wIndex);
  870. break;
  871. case (RH_PORT_POWER):
  872. USB_RH_PRINTF("Root-Hub: Port Power %d\n",wIndex);
  873. break;
  874. case (RH_C_PORT_CONNECTION):
  875. USB_RH_PRINTF("Root-Hub: C Port Connection Port %d\n",wIndex);
  876. break;
  877. case (RH_C_PORT_ENABLE):
  878. USB_RH_PRINTF("Root-Hub: C Port Enable Port %d\n",wIndex);
  879. break;
  880. case (RH_C_PORT_SUSPEND):
  881. USB_RH_PRINTF("Root-Hub: C Port Suspend Port %d\n",wIndex);
  882. break;
  883. case (RH_C_PORT_OVER_CURRENT):
  884. USB_RH_PRINTF("Root-Hub: C Port Over Current Port %d\n",wIndex);
  885. break;
  886. case (RH_C_PORT_RESET):
  887. USB_RH_PRINTF("Root-Hub: C Port reset Port %d\n",wIndex);
  888. break;
  889. default:
  890. USB_RH_PRINTF("Root-Hub: unknown %x %x\n",wValue,wIndex);
  891. break;
  892. }
  893. }
  894. #endif
  895. #ifdef USB_UHCI_DEBUG
  896. static int usb_display_td(uhci_td_t *td)
  897. {
  898. unsigned long tmp;
  899. int valid;
  900. printf("TD at %p:\n",td);
  901. tmp=swap_32(td->link);
  902. printf("Link points to 0x%08lX, %s first, %s, %s\n",tmp&0xfffffff0,
  903. ((tmp & 0x4)==0x4) ? "Depth" : "Breath",
  904. ((tmp & 0x2)==0x2) ? "QH" : "TD",
  905. ((tmp & 0x1)==0x1) ? "invalid" : "valid");
  906. valid=((tmp & 0x1)==0x0);
  907. tmp=swap_32(td->status);
  908. printf(" %s %ld Errors %s %s %s \n %s %s %s %s %s %s\n Len 0x%lX\n",
  909. (((tmp>>29)&0x1)==0x1) ? "SPD Enable" : "SPD Disable",
  910. ((tmp>>28)&0x3),
  911. (((tmp>>26)&0x1)==0x1) ? "Low Speed" : "Full Speed",
  912. (((tmp>>25)&0x1)==0x1) ? "ISO " : "",
  913. (((tmp>>24)&0x1)==0x1) ? "IOC " : "",
  914. (((tmp>>23)&0x1)==0x1) ? "Active " : "Inactive ",
  915. (((tmp>>22)&0x1)==0x1) ? "Stalled" : "",
  916. (((tmp>>21)&0x1)==0x1) ? "Data Buffer Error" : "",
  917. (((tmp>>20)&0x1)==0x1) ? "Babble" : "",
  918. (((tmp>>19)&0x1)==0x1) ? "NAK" : "",
  919. (((tmp>>18)&0x1)==0x1) ? "Bitstuff Error" : "",
  920. (tmp&0x7ff));
  921. tmp=swap_32(td->info);
  922. printf(" MaxLen 0x%lX\n",((tmp>>21)&0x7FF));
  923. printf(" %s Endpoint 0x%lX Dev Addr 0x%lX PID 0x%lX\n",((tmp>>19)&0x1)==0x1 ? "TOGGLE" : "",
  924. ((tmp>>15)&0xF),((tmp>>8)&0x7F),tmp&0xFF);
  925. tmp=swap_32(td->buffer);
  926. printf(" Buffer 0x%08lX\n",tmp);
  927. printf(" DEV %08lX\n",td->dev_ptr);
  928. return valid;
  929. }
  930. void usb_show_td(int max)
  931. {
  932. int i;
  933. if(max>0) {
  934. for(i=0;i<max;i++) {
  935. usb_display_td(&tmp_td[i]);
  936. }
  937. }
  938. else {
  939. i=0;
  940. do {
  941. printf("tmp_td[%d]\n",i);
  942. }while(usb_display_td(&tmp_td[i++]));
  943. }
  944. }
  945. #endif
  946. #endif /* CONFIG_USB_UHCI */
  947. /* EOF */