usb_uhci.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. * (C) Copyright 2008, Daniel Hellström, daniel@gaisler.com
  24. * Added AMBA Plug&Play detection of GRUSB, modified interrupt handler.
  25. * Added cache flushes where needed.
  26. *
  27. * See file CREDITS for list of people who contributed to this
  28. * project.
  29. *
  30. * This program is free software; you can redistribute it and/or
  31. * modify it under the terms of the GNU General Public License as
  32. * published by the Free Software Foundation; either version 2 of
  33. * the License, or (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License
  41. * along with this program; if not, write to the Free Software
  42. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  43. * MA 02111-1307 USA
  44. *
  45. *
  46. */
  47. /**********************************************************************
  48. * How it works:
  49. * -------------
  50. * The framelist / Transfer descriptor / Queue Heads are similar like
  51. * in the linux usb_uhci.c.
  52. *
  53. * During initialization, the following skeleton is allocated in init_skel:
  54. *
  55. * framespecific | common chain
  56. *
  57. * framelist[]
  58. * [ 0 ]-----> TD ---------\
  59. * [ 1 ]-----> TD ----------> TD ------> QH -------> QH -------> QH ---> NULL
  60. * ... TD ---------/
  61. * [1023]-----> TD --------/
  62. *
  63. * ^^ ^^ ^^ ^^ ^^
  64. * 7 TDs for 1 TD for Start of Start of End Chain
  65. * INT (2-128ms) 1ms-INT CTRL Chain BULK Chain
  66. *
  67. *
  68. * Since this is a bootloader, the isochronous transfer descriptor have been removed.
  69. *
  70. * Interrupt Transfers.
  71. * --------------------
  72. * For Interrupt transfers USB_MAX_TEMP_INT_TD Transfer descriptor are available. They
  73. * will be inserted after the appropriate (depending the interval setting) skeleton TD.
  74. * If an interrupt has been detected the dev->irqhandler is called. The status and number
  75. * of transfered bytes is stored in dev->irq_status resp. dev->irq_act_len. If the
  76. * dev->irqhandler returns 0, the interrupt TD is removed and disabled. If an 1 is returned,
  77. * the interrupt TD will be reactivated.
  78. *
  79. * Control Transfers
  80. * -----------------
  81. * Control Transfers are issued by filling the tmp_td with the appropriate data and connect
  82. * them to the qh_cntrl queue header. Before other control/bulk transfers can be issued,
  83. * the programm has to wait for completion. This does not allows asynchronous data transfer.
  84. *
  85. * Bulk Transfers
  86. * --------------
  87. * Bulk Transfers are issued by filling the tmp_td with the appropriate data and connect
  88. * them to the qh_bulk queue header. Before other control/bulk transfers can be issued,
  89. * the programm has to wait for completion. This does not allows asynchronous data transfer.
  90. *
  91. *
  92. */
  93. #include <common.h>
  94. #include <ambapp.h>
  95. #include <asm/leon.h>
  96. #include <asm/leon3.h>
  97. #include <asm/processor.h>
  98. #ifdef CONFIG_USB_UHCI
  99. #include <usb.h>
  100. #include "usb_uhci.h"
  101. #define USB_MAX_TEMP_TD 128 /* number of temporary TDs for bulk and control transfers */
  102. #define USB_MAX_TEMP_INT_TD 32 /* number of temporary TDs for Interrupt transfers */
  103. extern int leon3_snooping_avail;
  104. /*
  105. #define out16r(address,data) (*(unsigned short *)(address) = \
  106. (unsigned short)( \
  107. (((unsigned short)(data)&0xff)<<8) | \
  108. (((unsigned short)(data)&0xff00)>>8) \
  109. ))
  110. */
  111. #define out16r(address,data) _out16r((unsigned int)(address), (unsigned short)(data))
  112. void _out16r(unsigned int address, unsigned short data)
  113. {
  114. unsigned short val = (unsigned short)((((unsigned short)(data) & 0xff)
  115. << 8) | (((unsigned short)(data)
  116. & 0xff00) >> 8));
  117. #ifdef UHCI_DEBUG_REGS
  118. printf("out16r(0x%lx,0x%04x = 0x%04x)\n", address, val, data);
  119. #endif
  120. *(unsigned short *)(address) = val;
  121. }
  122. #define out32r(address,data) _out32r((unsigned int)(address), (unsigned int)(data))
  123. void _out32r(unsigned int address, unsigned int data)
  124. {
  125. unsigned int val = (unsigned int)((((unsigned int)(data) & 0x000000ff)
  126. << 24) | (((unsigned int)(data) &
  127. 0x0000ff00) << 8) |
  128. (((unsigned int)(data) & 0x00ff0000)
  129. >> 8) | (((unsigned int)(data) &
  130. 0xff000000) >> 24));
  131. #ifdef UHCI_DEBUG_REGS
  132. printf("out32r(0x%lx,0x%lx = 0x%lx)\n", address, val, data);
  133. #endif
  134. *(unsigned int *)address = val;
  135. }
  136. #define in16r(address) _in16r((unsigned int)(address))
  137. unsigned short _in16r(unsigned int address)
  138. {
  139. unsigned short val = sparc_load_reg_cachemiss_word(address);
  140. val = ((val << 8) & 0xff00) | ((val >> 8) & 0xff);
  141. #ifdef UHCI_DEBUG_REGS
  142. printf("in16r(0x%lx): 0x%04x\n", address, val);
  143. #endif
  144. return val;
  145. }
  146. #define in32r(address) _in32r((unsigned int)(address))
  147. unsigned int _in32r(unsigned int address)
  148. {
  149. unsigned int val = sparc_load_reg_cachemiss(address);
  150. val =
  151. ((val << 24) & 0xff000000) | ((val << 8) & 0xff0000) | ((val >> 8) &
  152. 0xff00) |
  153. ((val >> 24) & 0xff);
  154. #ifdef UHCI_DEBUG_REGS
  155. printf("in32r(0x%lx): 0x%08x\n", address, val);
  156. #endif
  157. return val;
  158. }
  159. #define READ32(address) sparc_load_reg_cachemiss((unsigned int)(address))
  160. /*#define USB_UHCI_DEBUG*/
  161. #undef USB_UHCI_DEBUG
  162. void usb_show_td(int max);
  163. #ifdef USB_UHCI_DEBUG
  164. void grusb_show_regs(void);
  165. #define USB_UHCI_PRINTF(fmt,args...) printf (fmt ,##args)
  166. #else
  167. #define USB_UHCI_PRINTF(fmt,args...)
  168. #endif
  169. static int grusb_irq = -1; /* irq vector, if -1 uhci is stopped / reseted */
  170. unsigned int usb_base_addr; /* base address */
  171. static uhci_td_t td_int[8] __attribute__ ((aligned(16))); /* Interrupt Transfer descriptors */
  172. static uhci_qh_t qh_cntrl __attribute__ ((aligned(16))); /* control Queue Head */
  173. static uhci_qh_t qh_bulk __attribute__ ((aligned(16))); /* bulk Queue Head */
  174. static uhci_qh_t qh_end __attribute__ ((aligned(16))); /* end Queue Head */
  175. static uhci_td_t td_last __attribute__ ((aligned(16))); /* last TD (linked with end chain) */
  176. /* temporary tds */
  177. static uhci_td_t tmp_td[USB_MAX_TEMP_TD] __attribute__ ((aligned(16))); /* temporary bulk/control td's */
  178. static uhci_td_t tmp_int_td[USB_MAX_TEMP_INT_TD] __attribute__ ((aligned(16))); /* temporary interrupt td's */
  179. static unsigned long framelist[1024] __attribute__ ((aligned(0x1000))); /* frame list */
  180. static struct virt_root_hub rh; /* struct for root hub */
  181. /**********************************************************************
  182. * some forward decleration
  183. */
  184. int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
  185. void *buffer, int transfer_len,
  186. struct devrequest *setup);
  187. /* fill a td with the approproiate data. Link, status, info and buffer
  188. * are used by the USB controller itselfes, dev is used to identify the
  189. * "connected" device
  190. */
  191. void usb_fill_td(uhci_td_t * td, unsigned long link, unsigned long status,
  192. unsigned long info, unsigned long buffer, unsigned long dev)
  193. {
  194. td->link = swap_32(link);
  195. td->status = swap_32(status);
  196. if ((info & UHCI_PID) == 0)
  197. info |= USB_PID_OUT;
  198. td->info = swap_32(info);
  199. td->buffer = swap_32(buffer);
  200. td->dev_ptr = dev;
  201. }
  202. /* fill a qh with the approproiate data. Head and element are used by the USB controller
  203. * itselfes. As soon as a valid dev_ptr is filled, a td chain is connected to the qh.
  204. * Please note, that after completion of the td chain, the entry element is removed /
  205. * marked invalid by the USB controller.
  206. */
  207. void usb_fill_qh(uhci_qh_t * qh, unsigned long head, unsigned long element)
  208. {
  209. qh->head = swap_32(head);
  210. qh->element = swap_32(element);
  211. qh->dev_ptr = 0L;
  212. }
  213. /* get the status of a td->status
  214. */
  215. unsigned long usb_uhci_td_stat(unsigned long status)
  216. {
  217. unsigned long result = 0;
  218. result |= (status & TD_CTRL_NAK) ? USB_ST_NAK_REC : 0;
  219. result |= (status & TD_CTRL_STALLED) ? USB_ST_STALLED : 0;
  220. result |= (status & TD_CTRL_DBUFERR) ? USB_ST_BUF_ERR : 0;
  221. result |= (status & TD_CTRL_BABBLE) ? USB_ST_BABBLE_DET : 0;
  222. result |= (status & TD_CTRL_CRCTIMEO) ? USB_ST_CRC_ERR : 0;
  223. result |= (status & TD_CTRL_BITSTUFF) ? USB_ST_BIT_ERR : 0;
  224. result |= (status & TD_CTRL_ACTIVE) ? USB_ST_NOT_PROC : 0;
  225. return result;
  226. }
  227. /* get the status and the transfered len of a td chain.
  228. * called from the completion handler
  229. */
  230. int usb_get_td_status(uhci_td_t * td, struct usb_device *dev)
  231. {
  232. unsigned long temp, info;
  233. unsigned long stat;
  234. uhci_td_t *mytd = td;
  235. if (dev->devnum == rh.devnum)
  236. return 0;
  237. dev->act_len = 0;
  238. stat = 0;
  239. do {
  240. temp = swap_32((unsigned long)READ32(&mytd->status));
  241. stat = usb_uhci_td_stat(temp);
  242. info = swap_32((unsigned long)READ32(&mytd->info));
  243. if (((info & 0xff) != USB_PID_SETUP) && (((info >> 21) & 0x7ff) != 0x7ff) && (temp & 0x7FF) != 0x7ff) { /* if not setup and not null data pack */
  244. dev->act_len += (temp & 0x7FF) + 1; /* the transfered len is act_len + 1 */
  245. }
  246. if (stat) { /* status no ok */
  247. dev->status = stat;
  248. return -1;
  249. }
  250. temp = swap_32((unsigned long)READ32(&mytd->link));
  251. mytd = (uhci_td_t *) (temp & 0xfffffff0);
  252. } while ((temp & 0x1) == 0); /* process all TDs */
  253. dev->status = stat;
  254. return 0; /* Ok */
  255. }
  256. /*-------------------------------------------------------------------
  257. * LOW LEVEL STUFF
  258. * assembles QHs und TDs for control, bulk and iso
  259. *-------------------------------------------------------------------*/
  260. int dummy(void)
  261. {
  262. USB_UHCI_PRINTF("DUMMY\n");
  263. return 0;
  264. }
  265. /* Submits a control message. That is a Setup, Data and Status transfer.
  266. * Routine does not wait for completion.
  267. */
  268. int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  269. int transfer_len, struct devrequest *setup)
  270. {
  271. unsigned long destination, status;
  272. int maxsze = usb_maxpacket(dev, pipe);
  273. unsigned long dataptr;
  274. int len;
  275. int pktsze;
  276. int i = 0;
  277. if (!maxsze) {
  278. USB_UHCI_PRINTF
  279. ("uhci_submit_control_urb: pipesize for pipe %lx is zero\n",
  280. pipe);
  281. return -1;
  282. }
  283. if (((pipe >> 8) & 0x7f) == rh.devnum) {
  284. /* this is the root hub -> redirect it */
  285. return uhci_submit_rh_msg(dev, pipe, buffer, transfer_len,
  286. setup);
  287. }
  288. USB_UHCI_PRINTF("uhci_submit_control start len %x, maxsize %x\n",
  289. transfer_len, maxsze);
  290. /* The "pipe" thing contains the destination in bits 8--18 */
  291. destination = (pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; /* Setup stage */
  292. /* 3 errors */
  293. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
  294. /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD); */
  295. /* Build the TD for the control request, try forever, 8 bytes of data */
  296. usb_fill_td(&tmp_td[i], UHCI_PTR_TERM, status, destination | (7 << 21),
  297. (unsigned long)setup, (unsigned long)dev);
  298. #ifdef DEBUG_EXTRA
  299. {
  300. char *sp = (char *)setup;
  301. printf("SETUP to pipe %lx: %x %x %x %x %x %x %x %x\n", pipe,
  302. sp[0], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6], sp[7]);
  303. }
  304. #endif
  305. dataptr = (unsigned long)buffer;
  306. len = transfer_len;
  307. /* If direction is "send", change the frame from SETUP (0x2D)
  308. to OUT (0xE1). Else change it from SETUP to IN (0x69). */
  309. destination =
  310. (pipe & PIPE_DEVEP_MASK) | ((pipe & USB_DIR_IN) ==
  311. 0 ? USB_PID_OUT : USB_PID_IN);
  312. while (len > 0) {
  313. /* data stage */
  314. pktsze = len;
  315. i++;
  316. if (pktsze > maxsze)
  317. pktsze = maxsze;
  318. destination ^= 1 << TD_TOKEN_TOGGLE; /* toggle DATA0/1 */
  319. usb_fill_td(&tmp_td[i], UHCI_PTR_TERM, status, destination | ((pktsze - 1) << 21), dataptr, (unsigned long)dev); /* Status, pktsze bytes of data */
  320. tmp_td[i - 1].link = swap_32((unsigned long)&tmp_td[i]);
  321. dataptr += pktsze;
  322. len -= pktsze;
  323. }
  324. /* Build the final TD for control status */
  325. /* It's only IN if the pipe is out AND we aren't expecting data */
  326. destination &= ~UHCI_PID;
  327. if (((pipe & USB_DIR_IN) == 0) || (transfer_len == 0))
  328. destination |= USB_PID_IN;
  329. else
  330. destination |= USB_PID_OUT;
  331. destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
  332. i++;
  333. status &= ~TD_CTRL_SPD;
  334. /* no limit on errors on final packet , 0 bytes of data */
  335. usb_fill_td(&tmp_td[i], UHCI_PTR_TERM, status | TD_CTRL_IOC,
  336. destination | (UHCI_NULL_DATA_SIZE << 21), 0,
  337. (unsigned long)dev);
  338. tmp_td[i - 1].link = swap_32((unsigned long)&tmp_td[i]); /* queue status td */
  339. /* usb_show_td(i+1); */
  340. USB_UHCI_PRINTF("uhci_submit_control end (%d tmp_tds used)\n", i);
  341. /* first mark the control QH element terminated */
  342. qh_cntrl.element = 0xffffffffL;
  343. /* set qh active */
  344. qh_cntrl.dev_ptr = (unsigned long)dev;
  345. /* fill in tmp_td_chain */
  346. dummy();
  347. qh_cntrl.element = swap_32((unsigned long)&tmp_td[0]);
  348. return 0;
  349. }
  350. /*-------------------------------------------------------------------
  351. * Prepare TDs for bulk transfers.
  352. */
  353. int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  354. int transfer_len)
  355. {
  356. unsigned long destination, status, info;
  357. unsigned long dataptr;
  358. int maxsze = usb_maxpacket(dev, pipe);
  359. int len;
  360. int i = 0;
  361. if (transfer_len < 0) {
  362. printf("Negative transfer length in submit_bulk\n");
  363. return -1;
  364. }
  365. if (!maxsze)
  366. return -1;
  367. /* The "pipe" thing contains the destination in bits 8--18. */
  368. destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid(pipe);
  369. /* 3 errors */
  370. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
  371. /* ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27); */
  372. /* Build the TDs for the bulk request */
  373. len = transfer_len;
  374. dataptr = (unsigned long)buffer;
  375. do {
  376. int pktsze = len;
  377. if (pktsze > maxsze)
  378. pktsze = maxsze;
  379. /* pktsze bytes of data */
  380. info =
  381. destination | (((pktsze - 1) & UHCI_NULL_DATA_SIZE) << 21) |
  382. (usb_gettoggle
  383. (dev, usb_pipeendpoint(pipe),
  384. usb_pipeout(pipe)) << TD_TOKEN_TOGGLE);
  385. if ((len - pktsze) == 0)
  386. status |= TD_CTRL_IOC; /* last one generates INT */
  387. usb_fill_td(&tmp_td[i], UHCI_PTR_TERM, status, info, dataptr, (unsigned long)dev); /* Status, pktsze bytes of data */
  388. if (i > 0)
  389. tmp_td[i - 1].link = swap_32((unsigned long)&tmp_td[i]);
  390. i++;
  391. dataptr += pktsze;
  392. len -= pktsze;
  393. usb_dotoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  394. } while (len > 0);
  395. /* first mark the bulk QH element terminated */
  396. qh_bulk.element = 0xffffffffL;
  397. /* set qh active */
  398. qh_bulk.dev_ptr = (unsigned long)dev;
  399. /* fill in tmp_td_chain */
  400. qh_bulk.element = swap_32((unsigned long)&tmp_td[0]);
  401. return 0;
  402. }
  403. /* search a free interrupt td
  404. */
  405. uhci_td_t *uhci_alloc_int_td(void)
  406. {
  407. int i;
  408. for (i = 0; i < USB_MAX_TEMP_INT_TD; i++) {
  409. if (tmp_int_td[i].dev_ptr == 0) /* no device assigned -> free TD */
  410. return &tmp_int_td[i];
  411. }
  412. return NULL;
  413. }
  414. /*-------------------------------------------------------------------
  415. * submits USB interrupt (ie. polling ;-)
  416. */
  417. int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  418. int transfer_len, int interval)
  419. {
  420. int nint, n;
  421. unsigned long status, destination;
  422. unsigned long info, tmp;
  423. uhci_td_t *mytd;
  424. if (interval < 0 || interval >= 256)
  425. return -1;
  426. if (interval == 0)
  427. nint = 0;
  428. else {
  429. for (nint = 0, n = 1; nint <= 8; nint++, n += n) { /* round interval down to 2^n */
  430. if (interval < n) {
  431. interval = n / 2;
  432. break;
  433. }
  434. }
  435. nint--;
  436. }
  437. USB_UHCI_PRINTF("Rounded interval to %i, chain %i\n", interval, nint);
  438. mytd = uhci_alloc_int_td();
  439. if (mytd == NULL) {
  440. printf("No free INT TDs found\n");
  441. return -1;
  442. }
  443. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC | (3 << 27);
  444. /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
  445. */
  446. destination =
  447. (pipe & PIPE_DEVEP_MASK) | usb_packetid(pipe) |
  448. (((transfer_len - 1) & 0x7ff) << 21);
  449. info =
  450. destination |
  451. (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) <<
  452. TD_TOKEN_TOGGLE);
  453. tmp = swap_32(td_int[nint].link);
  454. usb_fill_td(mytd, tmp, status, info, (unsigned long)buffer,
  455. (unsigned long)dev);
  456. /* Link it */
  457. tmp = swap_32((unsigned long)mytd);
  458. td_int[nint].link = tmp;
  459. usb_dotoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
  460. return 0;
  461. }
  462. /**********************************************************************
  463. * Low Level functions
  464. */
  465. void reset_hc(void)
  466. {
  467. /* Global reset for 100ms */
  468. out16r(usb_base_addr + USBPORTSC1, 0x0204);
  469. out16r(usb_base_addr + USBPORTSC2, 0x0204);
  470. out16r(usb_base_addr + USBCMD, USBCMD_GRESET | USBCMD_RS);
  471. /* Turn off all interrupts */
  472. out16r(usb_base_addr + USBINTR, 0);
  473. wait_ms(50);
  474. out16r(usb_base_addr + USBCMD, 0);
  475. wait_ms(10);
  476. }
  477. void start_hc(void)
  478. {
  479. int timeout = 1000;
  480. while (in16r(usb_base_addr + USBCMD) & USBCMD_HCRESET) {
  481. if (!--timeout) {
  482. printf("USBCMD_HCRESET timed out!\n");
  483. break;
  484. }
  485. }
  486. /* Turn on all interrupts */
  487. out16r(usb_base_addr + USBINTR,
  488. USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP);
  489. /* Start at frame 0 */
  490. out16r(usb_base_addr + USBFRNUM, 0);
  491. /* set Framebuffer base address */
  492. out32r(usb_base_addr + USBFLBASEADD, (unsigned long)&framelist);
  493. /* Run and mark it configured with a 64-byte max packet */
  494. out16r(usb_base_addr + USBCMD, USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
  495. }
  496. /* Initialize the skeleton
  497. */
  498. void usb_init_skel(void)
  499. {
  500. unsigned long temp;
  501. int n;
  502. for (n = 0; n < USB_MAX_TEMP_INT_TD; n++)
  503. tmp_int_td[n].dev_ptr = 0L; /* no devices connected */
  504. /* last td */
  505. usb_fill_td(&td_last, UHCI_PTR_TERM, TD_CTRL_IOC, USB_PID_OUT, 0, 0L);
  506. /* usb_fill_td(&td_last,UHCI_PTR_TERM,0,0,0); */
  507. /* End Queue Header */
  508. usb_fill_qh(&qh_end, UHCI_PTR_TERM, (unsigned long)&td_last);
  509. /* Bulk Queue Header */
  510. temp = (unsigned long)&qh_end;
  511. usb_fill_qh(&qh_bulk, temp | UHCI_PTR_QH, UHCI_PTR_TERM);
  512. /* Control Queue Header */
  513. temp = (unsigned long)&qh_bulk;
  514. usb_fill_qh(&qh_cntrl, temp | UHCI_PTR_QH, UHCI_PTR_TERM);
  515. /* 1ms Interrupt td */
  516. temp = (unsigned long)&qh_cntrl;
  517. usb_fill_td(&td_int[0], temp | UHCI_PTR_QH, 0, USB_PID_OUT, 0, 0L);
  518. temp = (unsigned long)&td_int[0];
  519. for (n = 1; n < 8; n++)
  520. usb_fill_td(&td_int[n], temp, 0, USB_PID_OUT, 0, 0L);
  521. for (n = 0; n < 1024; n++) {
  522. /* link all framelist pointers to one of the interrupts */
  523. int m, o;
  524. if ((n & 127) == 127)
  525. framelist[n] = swap_32((unsigned long)&td_int[0]);
  526. else
  527. for (o = 1, m = 2; m <= 128; o++, m += m)
  528. if ((n & (m - 1)) == ((m - 1) / 2))
  529. framelist[n] =
  530. swap_32((unsigned long)&td_int[o]);
  531. }
  532. }
  533. /* check the common skeleton for completed transfers, and update the status
  534. * of the "connected" device. Called from the IRQ routine.
  535. */
  536. void usb_check_skel(void)
  537. {
  538. struct usb_device *dev;
  539. /* start with the control qh */
  540. if (qh_cntrl.dev_ptr != 0) { /* it's a device assigned check if this caused IRQ */
  541. dev = (struct usb_device *)qh_cntrl.dev_ptr;
  542. /* Flush cache now that hardware updated DATA and TDs/QHs */
  543. if (!leon3_snooping_avail)
  544. sparc_dcache_flush_all();
  545. usb_get_td_status(&tmp_td[0], dev); /* update status */
  546. if (!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
  547. qh_cntrl.dev_ptr = 0;
  548. }
  549. }
  550. /* now process the bulk */
  551. if (qh_bulk.dev_ptr != 0) { /* it's a device assigned check if this caused IRQ */
  552. dev = (struct usb_device *)qh_bulk.dev_ptr;
  553. /* Flush cache now that hardware updated DATA and TDs/QHs */
  554. if (!leon3_snooping_avail)
  555. sparc_dcache_flush_all();
  556. usb_get_td_status(&tmp_td[0], dev); /* update status */
  557. if (!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
  558. qh_bulk.dev_ptr = 0;
  559. }
  560. }
  561. }
  562. /* check the interrupt chain, ubdate the status of the appropriate device,
  563. * call the appropriate irqhandler and reactivate the TD if the irqhandler
  564. * returns with 1
  565. */
  566. void usb_check_int_chain(void)
  567. {
  568. int i, res;
  569. unsigned long link, status;
  570. struct usb_device *dev;
  571. uhci_td_t *td, *prevtd;
  572. for (i = 0; i < 8; i++) {
  573. prevtd = &td_int[i]; /* the first previous td is the skeleton td */
  574. link = swap_32(READ32(&td_int[i].link)) & 0xfffffff0; /* next in chain */
  575. td = (uhci_td_t *) link; /* assign it */
  576. /* all interrupt TDs are finally linked to the td_int[0].
  577. * so we process all until we find the td_int[0].
  578. * if int0 chain points to a QH, we're also done
  579. */
  580. while (((i > 0) && (link != (unsigned long)&td_int[0])) ||
  581. ((i == 0)
  582. && !(swap_32(READ32(&td->link)) & UHCI_PTR_QH))) {
  583. /* check if a device is assigned with this td */
  584. status = swap_32(READ32(&td->status));
  585. if ((td->dev_ptr != 0L) && !(status & TD_CTRL_ACTIVE)) {
  586. /* td is not active and a device is assigned -> call irqhandler */
  587. dev = (struct usb_device *)td->dev_ptr;
  588. dev->irq_act_len = ((status & 0x7FF) == 0x7FF) ? 0 : (status & 0x7FF) + 1; /* transfered length */
  589. dev->irq_status = usb_uhci_td_stat(status); /* get status */
  590. res = dev->irq_handle(dev); /* call irqhandler */
  591. if (res == 1) {
  592. /* reactivate */
  593. status |= TD_CTRL_ACTIVE;
  594. td->status = swap_32(status);
  595. prevtd = td; /* previous td = this td */
  596. } else {
  597. prevtd->link = READ32(&td->link); /* link previous td directly to the nex td -> unlinked */
  598. /* remove device pointer */
  599. td->dev_ptr = 0L;
  600. }
  601. } /* if we call the irq handler */
  602. link = swap_32(READ32(&td->link)) & 0xfffffff0; /* next in chain */
  603. td = (uhci_td_t *) link; /* assign it */
  604. } /* process all td in this int chain */
  605. } /* next interrupt chain */
  606. }
  607. /* usb interrupt service routine.
  608. */
  609. void handle_usb_interrupt(void)
  610. {
  611. unsigned short status;
  612. static int error = 0;
  613. /*
  614. * Read the interrupt status, and write it back to clear the
  615. * interrupt cause
  616. */
  617. status = in16r(usb_base_addr + USBSTS);
  618. if (!status) /* shared interrupt, not mine */
  619. return;
  620. if (status != 1) {
  621. /* remove host controller halted state */
  622. if ((status & (USBSTS_HCPE | USBSTS_HCH)) ==
  623. (USBSTS_HCPE | USBSTS_HCH)) {
  624. /* Stop due to bug in driver, or hardware */
  625. out16r(usb_base_addr + USBSTS, status);
  626. out16r(usb_base_addr + USBCMD,
  627. USBCMD_HCRESET | USBCMD_GRESET);
  628. printf
  629. ("GRUSB: HW detected error(s) in USB Descriptors (STS: 0x%x)\n",
  630. status);
  631. usb_show_td(8);
  632. return;
  633. } else if ((status & 0x20)
  634. && ((in16r(usb_base_addr + USBCMD) & USBCMD_RS) ==
  635. 0)) {
  636. if (error < 10) {
  637. out16r(usb_base_addr + USBCMD,
  638. USBCMD_RS | in16r(usb_base_addr +
  639. USBCMD));
  640. error++;
  641. }
  642. } else
  643. error = 0;
  644. }
  645. usb_check_int_chain(); /* call interrupt handlers for int tds */
  646. usb_check_skel(); /* call completion handler for common transfer routines */
  647. out16r(usb_base_addr + USBSTS, status);
  648. }
  649. /* init uhci
  650. */
  651. int usb_lowlevel_init(void)
  652. {
  653. unsigned char temp;
  654. ambapp_ahbdev ahbdev;
  655. /* Find GRUSB core using AMBA Plug&Play information */
  656. if (ambapp_ahbslv_first(VENDOR_GAISLER, GAISLER_UHCI, &ahbdev) != 1) {
  657. printf("USB UHCI: Failed to find GRUSB controller\n");
  658. return -1;
  659. }
  660. usb_base_addr = ahbdev.address[0];
  661. grusb_irq = ahbdev.irq;
  662. /*
  663. usb_base_addr = 0xfffa0000;
  664. grusb_irq = 10;
  665. */
  666. #ifdef USB_UHCI_DEBUG
  667. grusb_show_regs();
  668. #endif
  669. memset(td_int, 0, sizeof(td_int));
  670. memset(tmp_td, 0, sizeof(tmp_td));
  671. memset(tmp_int_td, 0, sizeof(tmp_int_td));
  672. memset(&qh_cntrl, 0, sizeof(qh_cntrl));
  673. memset(&qh_end, 0, sizeof(qh_end));
  674. memset(&td_last, 0, sizeof(td_last));
  675. irq_free_handler(grusb_irq);
  676. USB_UHCI_PRINTF("GRUSB: at 0x%lx irq %d\n", usb_base_addr, grusb_irq);
  677. rh.devnum = 0;
  678. usb_init_skel();
  679. reset_hc();
  680. start_hc();
  681. irq_install_handler(grusb_irq,
  682. (interrupt_handler_t *) handle_usb_interrupt, NULL);
  683. return 0;
  684. }
  685. /* stop uhci
  686. */
  687. int usb_lowlevel_stop(void)
  688. {
  689. if (grusb_irq == -1)
  690. return 1;
  691. irq_free_handler(grusb_irq);
  692. reset_hc();
  693. grusb_irq = -1;
  694. return 0;
  695. }
  696. /*******************************************************************************************
  697. * Virtual Root Hub
  698. * Since the uhci does not have a real HUB, we simulate one ;-)
  699. */
  700. #undef USB_RH_DEBUG
  701. #ifdef USB_RH_DEBUG
  702. #define USB_RH_PRINTF(fmt,args...) printf (fmt ,##args)
  703. static void usb_display_wValue(unsigned short wValue, unsigned short wIndex);
  704. static void usb_display_Req(unsigned short req);
  705. #else
  706. #define USB_RH_PRINTF(fmt,args...)
  707. static void usb_display_wValue(unsigned short wValue, unsigned short wIndex)
  708. {
  709. }
  710. static void usb_display_Req(unsigned short req)
  711. {
  712. }
  713. #endif
  714. static unsigned char root_hub_dev_des[] = {
  715. 0x12, /* __u8 bLength; */
  716. 0x01, /* __u8 bDescriptorType; Device */
  717. 0x00, /* __u16 bcdUSB; v1.0 */
  718. 0x01,
  719. 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
  720. 0x00, /* __u8 bDeviceSubClass; */
  721. 0x00, /* __u8 bDeviceProtocol; */
  722. 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
  723. 0x00, /* __u16 idVendor; */
  724. 0x00,
  725. 0x00, /* __u16 idProduct; */
  726. 0x00,
  727. 0x00, /* __u16 bcdDevice; */
  728. 0x00,
  729. 0x01, /* __u8 iManufacturer; */
  730. 0x00, /* __u8 iProduct; */
  731. 0x00, /* __u8 iSerialNumber; */
  732. 0x01 /* __u8 bNumConfigurations; */
  733. };
  734. /* Configuration descriptor */
  735. static unsigned char root_hub_config_des[] = {
  736. 0x09, /* __u8 bLength; */
  737. 0x02, /* __u8 bDescriptorType; Configuration */
  738. 0x19, /* __u16 wTotalLength; */
  739. 0x00,
  740. 0x01, /* __u8 bNumInterfaces; */
  741. 0x01, /* __u8 bConfigurationValue; */
  742. 0x00, /* __u8 iConfiguration; */
  743. 0x40, /* __u8 bmAttributes;
  744. Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
  745. 0x00, /* __u8 MaxPower; */
  746. /* interface */
  747. 0x09, /* __u8 if_bLength; */
  748. 0x04, /* __u8 if_bDescriptorType; Interface */
  749. 0x00, /* __u8 if_bInterfaceNumber; */
  750. 0x00, /* __u8 if_bAlternateSetting; */
  751. 0x01, /* __u8 if_bNumEndpoints; */
  752. 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
  753. 0x00, /* __u8 if_bInterfaceSubClass; */
  754. 0x00, /* __u8 if_bInterfaceProtocol; */
  755. 0x00, /* __u8 if_iInterface; */
  756. /* endpoint */
  757. 0x07, /* __u8 ep_bLength; */
  758. 0x05, /* __u8 ep_bDescriptorType; Endpoint */
  759. 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
  760. 0x03, /* __u8 ep_bmAttributes; Interrupt */
  761. 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
  762. 0x00,
  763. 0xff /* __u8 ep_bInterval; 255 ms */
  764. };
  765. static unsigned char root_hub_hub_des[] = {
  766. 0x09, /* __u8 bLength; */
  767. 0x29, /* __u8 bDescriptorType; Hub-descriptor */
  768. 0x02, /* __u8 bNbrPorts; */
  769. 0x00, /* __u16 wHubCharacteristics; */
  770. 0x00,
  771. 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
  772. 0x00, /* __u8 bHubContrCurrent; 0 mA */
  773. 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
  774. 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
  775. };
  776. static unsigned char root_hub_str_index0[] = {
  777. 0x04, /* __u8 bLength; */
  778. 0x03, /* __u8 bDescriptorType; String-descriptor */
  779. 0x09, /* __u8 lang ID */
  780. 0x04, /* __u8 lang ID */
  781. };
  782. static unsigned char root_hub_str_index1[] = {
  783. 28, /* __u8 bLength; */
  784. 0x03, /* __u8 bDescriptorType; String-descriptor */
  785. 'U', /* __u8 Unicode */
  786. 0, /* __u8 Unicode */
  787. 'H', /* __u8 Unicode */
  788. 0, /* __u8 Unicode */
  789. 'C', /* __u8 Unicode */
  790. 0, /* __u8 Unicode */
  791. 'I', /* __u8 Unicode */
  792. 0, /* __u8 Unicode */
  793. ' ', /* __u8 Unicode */
  794. 0, /* __u8 Unicode */
  795. 'R', /* __u8 Unicode */
  796. 0, /* __u8 Unicode */
  797. 'o', /* __u8 Unicode */
  798. 0, /* __u8 Unicode */
  799. 'o', /* __u8 Unicode */
  800. 0, /* __u8 Unicode */
  801. 't', /* __u8 Unicode */
  802. 0, /* __u8 Unicode */
  803. ' ', /* __u8 Unicode */
  804. 0, /* __u8 Unicode */
  805. 'H', /* __u8 Unicode */
  806. 0, /* __u8 Unicode */
  807. 'u', /* __u8 Unicode */
  808. 0, /* __u8 Unicode */
  809. 'b', /* __u8 Unicode */
  810. 0, /* __u8 Unicode */
  811. };
  812. /*
  813. * Root Hub Control Pipe (interrupt Pipes are not supported)
  814. */
  815. int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
  816. int transfer_len, struct devrequest *cmd)
  817. {
  818. void *data = buffer;
  819. int leni = transfer_len;
  820. int len = 0;
  821. int status = 0;
  822. int stat = 0;
  823. int i;
  824. unsigned short cstatus;
  825. unsigned short bmRType_bReq;
  826. unsigned short wValue;
  827. unsigned short wIndex;
  828. unsigned short wLength;
  829. if (usb_pipeint(pipe)) {
  830. printf("Root-Hub submit IRQ: NOT implemented\n");
  831. return 0;
  832. }
  833. bmRType_bReq = cmd->requesttype | cmd->request << 8;
  834. wValue = swap_16(cmd->value);
  835. wIndex = swap_16(cmd->index);
  836. wLength = swap_16(cmd->length);
  837. usb_display_Req(bmRType_bReq);
  838. for (i = 0; i < 8; i++)
  839. rh.c_p_r[i] = 0;
  840. USB_RH_PRINTF("Root-Hub: adr: %2x cmd(%1x): %02x%02x %04x %04x %04x\n",
  841. dev->devnum, 8, cmd->requesttype, cmd->request, wValue,
  842. wIndex, wLength);
  843. switch (bmRType_bReq) {
  844. /* Request Destination:
  845. without flags: Device,
  846. RH_INTERFACE: interface,
  847. RH_ENDPOINT: endpoint,
  848. RH_CLASS means HUB here,
  849. RH_OTHER | RH_CLASS almost ever means HUB_PORT here
  850. */
  851. case RH_GET_STATUS:
  852. *(unsigned short *)data = swap_16(1);
  853. len = 2;
  854. break;
  855. case RH_GET_STATUS | RH_INTERFACE:
  856. *(unsigned short *)data = swap_16(0);
  857. len = 2;
  858. break;
  859. case RH_GET_STATUS | RH_ENDPOINT:
  860. *(unsigned short *)data = swap_16(0);
  861. len = 2;
  862. break;
  863. case RH_GET_STATUS | RH_CLASS:
  864. *(unsigned long *)data = swap_32(0);
  865. len = 4;
  866. break; /* hub power ** */
  867. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  868. status = in16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1));
  869. cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
  870. ((status & USBPORTSC_PEC) >> (3 - 1)) |
  871. (rh.c_p_r[wIndex - 1] << (0 + 4));
  872. status = (status & USBPORTSC_CCS) | ((status & USBPORTSC_PE) >> (2 - 1)) | ((status & USBPORTSC_SUSP) >> (12 - 2)) | ((status & USBPORTSC_PR) >> (9 - 4)) | (1 << 8) | /* power on ** */
  873. ((status & USBPORTSC_LSDA) << (-8 + 9));
  874. *(unsigned short *)data = swap_16(status);
  875. *(unsigned short *)(data + 2) = swap_16(cstatus);
  876. len = 4;
  877. break;
  878. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  879. switch (wValue) {
  880. case (RH_ENDPOINT_STALL):
  881. len = 0;
  882. break;
  883. }
  884. break;
  885. case RH_CLEAR_FEATURE | RH_CLASS:
  886. switch (wValue) {
  887. case (RH_C_HUB_OVER_CURRENT):
  888. len = 0; /* hub power over current ** */
  889. break;
  890. }
  891. break;
  892. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  893. usb_display_wValue(wValue, wIndex);
  894. switch (wValue) {
  895. case (RH_PORT_ENABLE):
  896. status =
  897. in16r(usb_base_addr + USBPORTSC1 +
  898. 2 * (wIndex - 1));
  899. status = (status & 0xfff5) & ~USBPORTSC_PE;
  900. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  901. status);
  902. len = 0;
  903. break;
  904. case (RH_PORT_SUSPEND):
  905. status =
  906. in16r(usb_base_addr + USBPORTSC1 +
  907. 2 * (wIndex - 1));
  908. status = (status & 0xfff5) & ~USBPORTSC_SUSP;
  909. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  910. status);
  911. len = 0;
  912. break;
  913. case (RH_PORT_POWER):
  914. len = 0; /* port power ** */
  915. break;
  916. case (RH_C_PORT_CONNECTION):
  917. status =
  918. in16r(usb_base_addr + USBPORTSC1 +
  919. 2 * (wIndex - 1));
  920. status = (status & 0xfff5) | USBPORTSC_CSC;
  921. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  922. status);
  923. len = 0;
  924. break;
  925. case (RH_C_PORT_ENABLE):
  926. status =
  927. in16r(usb_base_addr + USBPORTSC1 +
  928. 2 * (wIndex - 1));
  929. status = (status & 0xfff5) | USBPORTSC_PEC;
  930. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  931. status);
  932. len = 0;
  933. break;
  934. case (RH_C_PORT_SUSPEND):
  935. /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
  936. len = 0;
  937. break;
  938. case (RH_C_PORT_OVER_CURRENT):
  939. len = 0;
  940. break;
  941. case (RH_C_PORT_RESET):
  942. rh.c_p_r[wIndex - 1] = 0;
  943. len = 0;
  944. break;
  945. }
  946. break;
  947. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  948. usb_display_wValue(wValue, wIndex);
  949. switch (wValue) {
  950. case (RH_PORT_SUSPEND):
  951. status =
  952. in16r(usb_base_addr + USBPORTSC1 +
  953. 2 * (wIndex - 1));
  954. status = (status & 0xfff5) | USBPORTSC_SUSP;
  955. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  956. status);
  957. len = 0;
  958. break;
  959. case (RH_PORT_RESET):
  960. status =
  961. in16r(usb_base_addr + USBPORTSC1 +
  962. 2 * (wIndex - 1));
  963. status = (status & 0xfff5) | USBPORTSC_PR;
  964. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  965. status);
  966. wait_ms(10);
  967. status = (status & 0xfff5) & ~USBPORTSC_PR;
  968. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  969. status);
  970. udelay(10);
  971. status = (status & 0xfff5) | USBPORTSC_PE;
  972. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  973. status);
  974. wait_ms(10);
  975. status = (status & 0xfff5) | 0xa;
  976. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  977. status);
  978. len = 0;
  979. break;
  980. case (RH_PORT_POWER):
  981. len = 0; /* port power ** */
  982. break;
  983. case (RH_PORT_ENABLE):
  984. status =
  985. in16r(usb_base_addr + USBPORTSC1 +
  986. 2 * (wIndex - 1));
  987. status = (status & 0xfff5) | USBPORTSC_PE;
  988. out16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1),
  989. status);
  990. len = 0;
  991. break;
  992. }
  993. break;
  994. case RH_SET_ADDRESS:
  995. rh.devnum = wValue;
  996. len = 0;
  997. break;
  998. case RH_GET_DESCRIPTOR:
  999. switch ((wValue & 0xff00) >> 8) {
  1000. case (0x01): /* device descriptor */
  1001. i = sizeof(root_hub_config_des);
  1002. status = i > wLength ? wLength : i;
  1003. len = leni > status ? status : leni;
  1004. memcpy(data, root_hub_dev_des, len);
  1005. break;
  1006. case (0x02): /* configuration descriptor */
  1007. i = sizeof(root_hub_config_des);
  1008. status = i > wLength ? wLength : i;
  1009. len = leni > status ? status : leni;
  1010. memcpy(data, root_hub_config_des, len);
  1011. break;
  1012. case (0x03): /*string descriptors */
  1013. if (wValue == 0x0300) {
  1014. i = sizeof(root_hub_str_index0);
  1015. status = i > wLength ? wLength : i;
  1016. len = leni > status ? status : leni;
  1017. memcpy(data, root_hub_str_index0, len);
  1018. break;
  1019. }
  1020. if (wValue == 0x0301) {
  1021. i = sizeof(root_hub_str_index1);
  1022. status = i > wLength ? wLength : i;
  1023. len = leni > status ? status : leni;
  1024. memcpy(data, root_hub_str_index1, len);
  1025. break;
  1026. }
  1027. stat = USB_ST_STALLED;
  1028. }
  1029. break;
  1030. case RH_GET_DESCRIPTOR | RH_CLASS:
  1031. root_hub_hub_des[2] = 2;
  1032. i = sizeof(root_hub_hub_des);
  1033. status = i > wLength ? wLength : i;
  1034. len = leni > status ? status : leni;
  1035. memcpy(data, root_hub_hub_des, len);
  1036. break;
  1037. case RH_GET_CONFIGURATION:
  1038. *(unsigned char *)data = 0x01;
  1039. len = 1;
  1040. break;
  1041. case RH_SET_CONFIGURATION:
  1042. len = 0;
  1043. break;
  1044. default:
  1045. stat = USB_ST_STALLED;
  1046. }
  1047. USB_RH_PRINTF("Root-Hub stat %lx port1: %x port2: %x\n\n", stat,
  1048. in16r(usb_base_addr + USBPORTSC1),
  1049. in16r(usb_base_addr + USBPORTSC2));
  1050. dev->act_len = len;
  1051. dev->status = stat;
  1052. return stat;
  1053. }
  1054. /********************************************************************************
  1055. * Some Debug Routines
  1056. */
  1057. #ifdef USB_RH_DEBUG
  1058. static void usb_display_Req(unsigned short req)
  1059. {
  1060. USB_RH_PRINTF("- Root-Hub Request: ");
  1061. switch (req) {
  1062. case RH_GET_STATUS:
  1063. USB_RH_PRINTF("Get Status ");
  1064. break;
  1065. case RH_GET_STATUS | RH_INTERFACE:
  1066. USB_RH_PRINTF("Get Status Interface ");
  1067. break;
  1068. case RH_GET_STATUS | RH_ENDPOINT:
  1069. USB_RH_PRINTF("Get Status Endpoint ");
  1070. break;
  1071. case RH_GET_STATUS | RH_CLASS:
  1072. USB_RH_PRINTF("Get Status Class");
  1073. break; /* hub power ** */
  1074. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  1075. USB_RH_PRINTF("Get Status Class Others");
  1076. break;
  1077. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  1078. USB_RH_PRINTF("Clear Feature Endpoint ");
  1079. break;
  1080. case RH_CLEAR_FEATURE | RH_CLASS:
  1081. USB_RH_PRINTF("Clear Feature Class ");
  1082. break;
  1083. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  1084. USB_RH_PRINTF("Clear Feature Other Class ");
  1085. break;
  1086. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  1087. USB_RH_PRINTF("Set Feature Other Class ");
  1088. break;
  1089. case RH_SET_ADDRESS:
  1090. USB_RH_PRINTF("Set Address ");
  1091. break;
  1092. case RH_GET_DESCRIPTOR:
  1093. USB_RH_PRINTF("Get Descriptor ");
  1094. break;
  1095. case RH_GET_DESCRIPTOR | RH_CLASS:
  1096. USB_RH_PRINTF("Get Descriptor Class ");
  1097. break;
  1098. case RH_GET_CONFIGURATION:
  1099. USB_RH_PRINTF("Get Configuration ");
  1100. break;
  1101. case RH_SET_CONFIGURATION:
  1102. USB_RH_PRINTF("Get Configuration ");
  1103. break;
  1104. default:
  1105. USB_RH_PRINTF("****UNKNOWN**** 0x%04X ", req);
  1106. }
  1107. USB_RH_PRINTF("\n");
  1108. }
  1109. static void usb_display_wValue(unsigned short wValue, unsigned short wIndex)
  1110. {
  1111. switch (wValue) {
  1112. case (RH_PORT_ENABLE):
  1113. USB_RH_PRINTF("Root-Hub: Enable Port %d\n", wIndex);
  1114. break;
  1115. case (RH_PORT_SUSPEND):
  1116. USB_RH_PRINTF("Root-Hub: Suspend Port %d\n", wIndex);
  1117. break;
  1118. case (RH_PORT_POWER):
  1119. USB_RH_PRINTF("Root-Hub: Port Power %d\n", wIndex);
  1120. break;
  1121. case (RH_C_PORT_CONNECTION):
  1122. USB_RH_PRINTF("Root-Hub: C Port Connection Port %d\n", wIndex);
  1123. break;
  1124. case (RH_C_PORT_ENABLE):
  1125. USB_RH_PRINTF("Root-Hub: C Port Enable Port %d\n", wIndex);
  1126. break;
  1127. case (RH_C_PORT_SUSPEND):
  1128. USB_RH_PRINTF("Root-Hub: C Port Suspend Port %d\n", wIndex);
  1129. break;
  1130. case (RH_C_PORT_OVER_CURRENT):
  1131. USB_RH_PRINTF("Root-Hub: C Port Over Current Port %d\n",
  1132. wIndex);
  1133. break;
  1134. case (RH_C_PORT_RESET):
  1135. USB_RH_PRINTF("Root-Hub: C Port reset Port %d\n", wIndex);
  1136. break;
  1137. default:
  1138. USB_RH_PRINTF("Root-Hub: unknown %x %x\n", wValue, wIndex);
  1139. break;
  1140. }
  1141. }
  1142. #endif
  1143. /*#ifdef USB_UHCI_DEBUG*/
  1144. static int usb_display_td(uhci_td_t * td)
  1145. {
  1146. unsigned long tmp;
  1147. int valid;
  1148. printf("TD at %p:\n", td);
  1149. tmp = swap_32(READ32(&td->link));
  1150. printf("Link points to 0x%08lX, %s first, %s, %s\n", tmp & 0xfffffff0,
  1151. ((tmp & 0x4) == 0x4) ? "Depth" : "Breath",
  1152. ((tmp & 0x2) == 0x2) ? "QH" : "TD",
  1153. ((tmp & 0x1) == 0x1) ? "invalid" : "valid");
  1154. valid = ((tmp & 0x1) == 0x0);
  1155. tmp = swap_32(READ32(&td->status));
  1156. printf
  1157. (" %s %ld Errors %s %s %s \n %s %s %s %s %s %s\n Len 0x%lX\n",
  1158. (((tmp >> 29) & 0x1) == 0x1) ? "SPD Enable" : "SPD Disable",
  1159. ((tmp >> 28) & 0x3),
  1160. (((tmp >> 26) & 0x1) == 0x1) ? "Low Speed" : "Full Speed",
  1161. (((tmp >> 25) & 0x1) == 0x1) ? "ISO " : "",
  1162. (((tmp >> 24) & 0x1) == 0x1) ? "IOC " : "",
  1163. (((tmp >> 23) & 0x1) == 0x1) ? "Active " : "Inactive ",
  1164. (((tmp >> 22) & 0x1) == 0x1) ? "Stalled" : "",
  1165. (((tmp >> 21) & 0x1) == 0x1) ? "Data Buffer Error" : "",
  1166. (((tmp >> 20) & 0x1) == 0x1) ? "Babble" : "",
  1167. (((tmp >> 19) & 0x1) == 0x1) ? "NAK" : "",
  1168. (((tmp >> 18) & 0x1) == 0x1) ? "Bitstuff Error" : "",
  1169. (tmp & 0x7ff));
  1170. tmp = swap_32(READ32(&td->info));
  1171. printf(" MaxLen 0x%lX\n", ((tmp >> 21) & 0x7FF));
  1172. printf(" %sEndpoint 0x%lX Dev Addr 0x%lX PID 0x%lX\n",
  1173. ((tmp >> 19) & 0x1) == 0x1 ? "TOGGLE " : "", ((tmp >> 15) & 0xF),
  1174. ((tmp >> 8) & 0x7F), tmp & 0xFF);
  1175. tmp = swap_32(READ32(&td->buffer));
  1176. printf(" Buffer 0x%08lX\n", tmp);
  1177. printf(" DEV %08lX\n", td->dev_ptr);
  1178. return valid;
  1179. }
  1180. void usb_show_td(int max)
  1181. {
  1182. int i;
  1183. if (max > 0) {
  1184. for (i = 0; i < max; i++) {
  1185. usb_display_td(&tmp_td[i]);
  1186. }
  1187. } else {
  1188. i = 0;
  1189. do {
  1190. printf("tmp_td[%d]\n", i);
  1191. } while (usb_display_td(&tmp_td[i++]));
  1192. }
  1193. }
  1194. void grusb_show_regs(void)
  1195. {
  1196. unsigned int tmp;
  1197. tmp = in16r(usb_base_addr + USBCMD);
  1198. printf(" USBCMD: 0x%04x\n", tmp);
  1199. tmp = in16r(usb_base_addr + USBSTS);
  1200. printf(" USBSTS: 0x%04x\n", tmp);
  1201. tmp = in16r(usb_base_addr + USBINTR);
  1202. printf(" USBINTR: 0x%04x\n", tmp);
  1203. tmp = in16r(usb_base_addr + USBFRNUM);
  1204. printf(" FRNUM: 0x%04x\n", tmp);
  1205. tmp = in32r(usb_base_addr + USBFLBASEADD);
  1206. printf(" FLBASEADD: 0x%08x\n", tmp);
  1207. tmp = in16r(usb_base_addr + USBSOF);
  1208. printf(" SOFMOD: 0x%04x\n", tmp);
  1209. tmp = in16r(usb_base_addr + USBPORTSC1);
  1210. printf(" PORTSC1: 0x%04x\n", tmp);
  1211. }
  1212. /*#endif*/
  1213. #endif /* CONFIG_USB_UHCI */
  1214. /* EOF */