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