]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * USB HOST XHCI Controller stack | |
3 | * | |
4 | * Based on xHCI host controller driver in linux-kernel | |
5 | * by Sarah Sharp. | |
6 | * | |
7 | * Copyright (C) 2008 Intel Corp. | |
8 | * Author: Sarah Sharp | |
9 | * | |
10 | * Copyright (C) 2013 Samsung Electronics Co.Ltd | |
11 | * Authors: Vivek Gautam <gautam.vivek@samsung.com> | |
12 | * Vikas Sajjan <vikas.sajjan@samsung.com> | |
13 | * | |
14 | * SPDX-License-Identifier: GPL-2.0+ | |
15 | */ | |
16 | ||
17 | /** | |
18 | * This file gives the xhci stack for usb3.0 looking into | |
19 | * xhci specification Rev1.0 (5/21/10). | |
20 | * The quirk devices support hasn't been given yet. | |
21 | */ | |
22 | ||
23 | #include <common.h> | |
24 | #include <dm.h> | |
25 | #include <asm/byteorder.h> | |
26 | #include <usb.h> | |
27 | #include <malloc.h> | |
28 | #include <watchdog.h> | |
29 | #include <asm/cache.h> | |
30 | #include <asm/unaligned.h> | |
31 | #include <linux/errno.h> | |
32 | #include "xhci.h" | |
33 | ||
34 | #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT | |
35 | #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 | |
36 | #endif | |
37 | ||
38 | static struct descriptor { | |
39 | struct usb_hub_descriptor hub; | |
40 | struct usb_device_descriptor device; | |
41 | struct usb_config_descriptor config; | |
42 | struct usb_interface_descriptor interface; | |
43 | struct usb_endpoint_descriptor endpoint; | |
44 | struct usb_ss_ep_comp_descriptor ep_companion; | |
45 | } __attribute__ ((packed)) descriptor = { | |
46 | { | |
47 | 0xc, /* bDescLength */ | |
48 | 0x2a, /* bDescriptorType: hub descriptor */ | |
49 | 2, /* bNrPorts -- runtime modified */ | |
50 | cpu_to_le16(0x8), /* wHubCharacteristics */ | |
51 | 10, /* bPwrOn2PwrGood */ | |
52 | 0, /* bHubCntrCurrent */ | |
53 | { /* Device removable */ | |
54 | } /* at most 7 ports! XXX */ | |
55 | }, | |
56 | { | |
57 | 0x12, /* bLength */ | |
58 | 1, /* bDescriptorType: UDESC_DEVICE */ | |
59 | cpu_to_le16(0x0300), /* bcdUSB: v3.0 */ | |
60 | 9, /* bDeviceClass: UDCLASS_HUB */ | |
61 | 0, /* bDeviceSubClass: UDSUBCLASS_HUB */ | |
62 | 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */ | |
63 | 9, /* bMaxPacketSize: 512 bytes 2^9 */ | |
64 | 0x0000, /* idVendor */ | |
65 | 0x0000, /* idProduct */ | |
66 | cpu_to_le16(0x0100), /* bcdDevice */ | |
67 | 1, /* iManufacturer */ | |
68 | 2, /* iProduct */ | |
69 | 0, /* iSerialNumber */ | |
70 | 1 /* bNumConfigurations: 1 */ | |
71 | }, | |
72 | { | |
73 | 0x9, | |
74 | 2, /* bDescriptorType: UDESC_CONFIG */ | |
75 | cpu_to_le16(0x1f), /* includes SS endpoint descriptor */ | |
76 | 1, /* bNumInterface */ | |
77 | 1, /* bConfigurationValue */ | |
78 | 0, /* iConfiguration */ | |
79 | 0x40, /* bmAttributes: UC_SELF_POWER */ | |
80 | 0 /* bMaxPower */ | |
81 | }, | |
82 | { | |
83 | 0x9, /* bLength */ | |
84 | 4, /* bDescriptorType: UDESC_INTERFACE */ | |
85 | 0, /* bInterfaceNumber */ | |
86 | 0, /* bAlternateSetting */ | |
87 | 1, /* bNumEndpoints */ | |
88 | 9, /* bInterfaceClass: UICLASS_HUB */ | |
89 | 0, /* bInterfaceSubClass: UISUBCLASS_HUB */ | |
90 | 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */ | |
91 | 0 /* iInterface */ | |
92 | }, | |
93 | { | |
94 | 0x7, /* bLength */ | |
95 | 5, /* bDescriptorType: UDESC_ENDPOINT */ | |
96 | 0x81, /* bEndpointAddress: IN endpoint 1 */ | |
97 | 3, /* bmAttributes: UE_INTERRUPT */ | |
98 | 8, /* wMaxPacketSize */ | |
99 | 255 /* bInterval */ | |
100 | }, | |
101 | { | |
102 | 0x06, /* ss_bLength */ | |
103 | 0x30, /* ss_bDescriptorType: SS EP Companion */ | |
104 | 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */ | |
105 | /* ss_bmAttributes: 1 packet per service interval */ | |
106 | 0x00, | |
107 | /* ss_wBytesPerInterval: 15 bits for max 15 ports */ | |
108 | cpu_to_le16(0x02), | |
109 | }, | |
110 | }; | |
111 | ||
112 | #ifndef CONFIG_DM_USB | |
113 | static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT]; | |
114 | #endif | |
115 | ||
116 | struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev) | |
117 | { | |
118 | #ifdef CONFIG_DM_USB | |
119 | struct udevice *dev; | |
120 | ||
121 | /* Find the USB controller */ | |
122 | for (dev = udev->dev; | |
123 | device_get_uclass_id(dev) != UCLASS_USB; | |
124 | dev = dev->parent) | |
125 | ; | |
126 | return dev_get_priv(dev); | |
127 | #else | |
128 | return udev->controller; | |
129 | #endif | |
130 | } | |
131 | ||
132 | /** | |
133 | * Waits for as per specified amount of time | |
134 | * for the "result" to match with "done" | |
135 | * | |
136 | * @param ptr pointer to the register to be read | |
137 | * @param mask mask for the value read | |
138 | * @param done value to be campared with result | |
139 | * @param usec time to wait till | |
140 | * @return 0 if handshake is success else < 0 on failure | |
141 | */ | |
142 | static int handshake(uint32_t volatile *ptr, uint32_t mask, | |
143 | uint32_t done, int usec) | |
144 | { | |
145 | uint32_t result; | |
146 | ||
147 | do { | |
148 | result = xhci_readl(ptr); | |
149 | if (result == ~(uint32_t)0) | |
150 | return -ENODEV; | |
151 | result &= mask; | |
152 | if (result == done) | |
153 | return 0; | |
154 | usec--; | |
155 | udelay(1); | |
156 | } while (usec > 0); | |
157 | ||
158 | return -ETIMEDOUT; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Set the run bit and wait for the host to be running. | |
163 | * | |
164 | * @param hcor pointer to host controller operation registers | |
165 | * @return status of the Handshake | |
166 | */ | |
167 | static int xhci_start(struct xhci_hcor *hcor) | |
168 | { | |
169 | u32 temp; | |
170 | int ret; | |
171 | ||
172 | puts("Starting the controller\n"); | |
173 | temp = xhci_readl(&hcor->or_usbcmd); | |
174 | temp |= (CMD_RUN); | |
175 | xhci_writel(&hcor->or_usbcmd, temp); | |
176 | ||
177 | /* | |
178 | * Wait for the HCHalted Status bit to be 0 to indicate the host is | |
179 | * running. | |
180 | */ | |
181 | ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC); | |
182 | if (ret) | |
183 | debug("Host took too long to start, " | |
184 | "waited %u microseconds.\n", | |
185 | XHCI_MAX_HALT_USEC); | |
186 | return ret; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Resets the XHCI Controller | |
191 | * | |
192 | * @param hcor pointer to host controller operation registers | |
193 | * @return -EBUSY if XHCI Controller is not halted else status of handshake | |
194 | */ | |
195 | static int xhci_reset(struct xhci_hcor *hcor) | |
196 | { | |
197 | u32 cmd; | |
198 | u32 state; | |
199 | int ret; | |
200 | ||
201 | /* Halting the Host first */ | |
202 | debug("// Halt the HC: %p\n", hcor); | |
203 | state = xhci_readl(&hcor->or_usbsts) & STS_HALT; | |
204 | if (!state) { | |
205 | cmd = xhci_readl(&hcor->or_usbcmd); | |
206 | cmd &= ~CMD_RUN; | |
207 | xhci_writel(&hcor->or_usbcmd, cmd); | |
208 | } | |
209 | ||
210 | ret = handshake(&hcor->or_usbsts, | |
211 | STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); | |
212 | if (ret) { | |
213 | printf("Host not halted after %u microseconds.\n", | |
214 | XHCI_MAX_HALT_USEC); | |
215 | return -EBUSY; | |
216 | } | |
217 | ||
218 | debug("// Reset the HC\n"); | |
219 | cmd = xhci_readl(&hcor->or_usbcmd); | |
220 | cmd |= CMD_RESET; | |
221 | xhci_writel(&hcor->or_usbcmd, cmd); | |
222 | ||
223 | ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC); | |
224 | if (ret) | |
225 | return ret; | |
226 | ||
227 | /* | |
228 | * xHCI cannot write to any doorbells or operational registers other | |
229 | * than status until the "Controller Not Ready" flag is cleared. | |
230 | */ | |
231 | return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC); | |
232 | } | |
233 | ||
234 | /** | |
235 | * Used for passing endpoint bitmasks between the core and HCDs. | |
236 | * Find the index for an endpoint given its descriptor. | |
237 | * Use the return value to right shift 1 for the bitmask. | |
238 | * | |
239 | * Index = (epnum * 2) + direction - 1, | |
240 | * where direction = 0 for OUT, 1 for IN. | |
241 | * For control endpoints, the IN index is used (OUT index is unused), so | |
242 | * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) | |
243 | * | |
244 | * @param desc USB enpdoint Descriptor | |
245 | * @return index of the Endpoint | |
246 | */ | |
247 | static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc) | |
248 | { | |
249 | unsigned int index; | |
250 | ||
251 | if (usb_endpoint_xfer_control(desc)) | |
252 | index = (unsigned int)(usb_endpoint_num(desc) * 2); | |
253 | else | |
254 | index = (unsigned int)((usb_endpoint_num(desc) * 2) - | |
255 | (usb_endpoint_dir_in(desc) ? 0 : 1)); | |
256 | ||
257 | return index; | |
258 | } | |
259 | ||
260 | /** | |
261 | * Issue a configure endpoint command or evaluate context command | |
262 | * and wait for it to finish. | |
263 | * | |
264 | * @param udev pointer to the Device Data Structure | |
265 | * @param ctx_change flag to indicate the Context has changed or NOT | |
266 | * @return 0 on success, -1 on failure | |
267 | */ | |
268 | static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change) | |
269 | { | |
270 | struct xhci_container_ctx *in_ctx; | |
271 | struct xhci_virt_device *virt_dev; | |
272 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
273 | union xhci_trb *event; | |
274 | ||
275 | virt_dev = ctrl->devs[udev->slot_id]; | |
276 | in_ctx = virt_dev->in_ctx; | |
277 | ||
278 | xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size); | |
279 | xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0, | |
280 | ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP); | |
281 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); | |
282 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) | |
283 | != udev->slot_id); | |
284 | ||
285 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { | |
286 | case COMP_SUCCESS: | |
287 | debug("Successful %s command\n", | |
288 | ctx_change ? "Evaluate Context" : "Configure Endpoint"); | |
289 | break; | |
290 | default: | |
291 | printf("ERROR: %s command returned completion code %d.\n", | |
292 | ctx_change ? "Evaluate Context" : "Configure Endpoint", | |
293 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); | |
294 | return -EINVAL; | |
295 | } | |
296 | ||
297 | xhci_acknowledge_event(ctrl); | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | /** | |
303 | * Configure the endpoint, programming the device contexts. | |
304 | * | |
305 | * @param udev pointer to the USB device structure | |
306 | * @return returns the status of the xhci_configure_endpoints | |
307 | */ | |
308 | static int xhci_set_configuration(struct usb_device *udev) | |
309 | { | |
310 | struct xhci_container_ctx *in_ctx; | |
311 | struct xhci_container_ctx *out_ctx; | |
312 | struct xhci_input_control_ctx *ctrl_ctx; | |
313 | struct xhci_slot_ctx *slot_ctx; | |
314 | struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM]; | |
315 | int cur_ep; | |
316 | int max_ep_flag = 0; | |
317 | int ep_index; | |
318 | unsigned int dir; | |
319 | unsigned int ep_type; | |
320 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
321 | int num_of_ep; | |
322 | int ep_flag = 0; | |
323 | u64 trb_64 = 0; | |
324 | int slot_id = udev->slot_id; | |
325 | struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; | |
326 | struct usb_interface *ifdesc; | |
327 | ||
328 | out_ctx = virt_dev->out_ctx; | |
329 | in_ctx = virt_dev->in_ctx; | |
330 | ||
331 | num_of_ep = udev->config.if_desc[0].no_of_ep; | |
332 | ifdesc = &udev->config.if_desc[0]; | |
333 | ||
334 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); | |
335 | /* Initialize the input context control */ | |
336 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); | |
337 | ctrl_ctx->drop_flags = 0; | |
338 | ||
339 | /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */ | |
340 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { | |
341 | ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]); | |
342 | ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1)); | |
343 | if (max_ep_flag < ep_flag) | |
344 | max_ep_flag = ep_flag; | |
345 | } | |
346 | ||
347 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); | |
348 | ||
349 | /* slot context */ | |
350 | xhci_slot_copy(ctrl, in_ctx, out_ctx); | |
351 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); | |
352 | slot_ctx->dev_info &= ~(LAST_CTX_MASK); | |
353 | slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0); | |
354 | ||
355 | xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0); | |
356 | ||
357 | /* filling up ep contexts */ | |
358 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { | |
359 | struct usb_endpoint_descriptor *endpt_desc = NULL; | |
360 | ||
361 | endpt_desc = &ifdesc->ep_desc[cur_ep]; | |
362 | trb_64 = 0; | |
363 | ||
364 | ep_index = xhci_get_ep_index(endpt_desc); | |
365 | ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); | |
366 | ||
367 | /* Allocate the ep rings */ | |
368 | virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true); | |
369 | if (!virt_dev->eps[ep_index].ring) | |
370 | return -ENOMEM; | |
371 | ||
372 | /*NOTE: ep_desc[0] actually represents EP1 and so on */ | |
373 | dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7); | |
374 | ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2)); | |
375 | ep_ctx[ep_index]->ep_info2 = | |
376 | cpu_to_le32(ep_type << EP_TYPE_SHIFT); | |
377 | ep_ctx[ep_index]->ep_info2 |= | |
378 | cpu_to_le32(MAX_PACKET | |
379 | (get_unaligned(&endpt_desc->wMaxPacketSize))); | |
380 | ||
381 | ep_ctx[ep_index]->ep_info2 |= | |
382 | cpu_to_le32(((0 & MAX_BURST_MASK) << MAX_BURST_SHIFT) | | |
383 | ((3 & ERROR_COUNT_MASK) << ERROR_COUNT_SHIFT)); | |
384 | ||
385 | trb_64 = (uintptr_t) | |
386 | virt_dev->eps[ep_index].ring->enqueue; | |
387 | ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 | | |
388 | virt_dev->eps[ep_index].ring->cycle_state); | |
389 | } | |
390 | ||
391 | return xhci_configure_endpoints(udev, false); | |
392 | } | |
393 | ||
394 | /** | |
395 | * Issue an Address Device command (which will issue a SetAddress request to | |
396 | * the device). | |
397 | * | |
398 | * @param udev pointer to the Device Data Structure | |
399 | * @return 0 if successful else error code on failure | |
400 | */ | |
401 | static int xhci_address_device(struct usb_device *udev, int root_portnr) | |
402 | { | |
403 | int ret = 0; | |
404 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
405 | struct xhci_slot_ctx *slot_ctx; | |
406 | struct xhci_input_control_ctx *ctrl_ctx; | |
407 | struct xhci_virt_device *virt_dev; | |
408 | int slot_id = udev->slot_id; | |
409 | union xhci_trb *event; | |
410 | ||
411 | virt_dev = ctrl->devs[slot_id]; | |
412 | ||
413 | /* | |
414 | * This is the first Set Address since device plug-in | |
415 | * so setting up the slot context. | |
416 | */ | |
417 | debug("Setting up addressable devices %p\n", ctrl->dcbaa); | |
418 | xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr); | |
419 | ||
420 | ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); | |
421 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); | |
422 | ctrl_ctx->drop_flags = 0; | |
423 | ||
424 | xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV); | |
425 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); | |
426 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id); | |
427 | ||
428 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { | |
429 | case COMP_CTX_STATE: | |
430 | case COMP_EBADSLT: | |
431 | printf("Setup ERROR: address device command for slot %d.\n", | |
432 | slot_id); | |
433 | ret = -EINVAL; | |
434 | break; | |
435 | case COMP_TX_ERR: | |
436 | puts("Device not responding to set address.\n"); | |
437 | ret = -EPROTO; | |
438 | break; | |
439 | case COMP_DEV_ERR: | |
440 | puts("ERROR: Incompatible device" | |
441 | "for address device command.\n"); | |
442 | ret = -ENODEV; | |
443 | break; | |
444 | case COMP_SUCCESS: | |
445 | debug("Successful Address Device command\n"); | |
446 | udev->status = 0; | |
447 | break; | |
448 | default: | |
449 | printf("ERROR: unexpected command completion code 0x%x.\n", | |
450 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); | |
451 | ret = -EINVAL; | |
452 | break; | |
453 | } | |
454 | ||
455 | xhci_acknowledge_event(ctrl); | |
456 | ||
457 | if (ret < 0) | |
458 | /* | |
459 | * TODO: Unsuccessful Address Device command shall leave the | |
460 | * slot in default state. So, issue Disable Slot command now. | |
461 | */ | |
462 | return ret; | |
463 | ||
464 | xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, | |
465 | virt_dev->out_ctx->size); | |
466 | slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx); | |
467 | ||
468 | debug("xHC internal address is: %d\n", | |
469 | le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Issue Enable slot command to the controller to allocate | |
476 | * device slot and assign the slot id. It fails if the xHC | |
477 | * ran out of device slots, the Enable Slot command timed out, | |
478 | * or allocating memory failed. | |
479 | * | |
480 | * @param udev pointer to the Device Data Structure | |
481 | * @return Returns 0 on succes else return error code on failure | |
482 | */ | |
483 | static int _xhci_alloc_device(struct usb_device *udev) | |
484 | { | |
485 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
486 | union xhci_trb *event; | |
487 | int ret; | |
488 | ||
489 | /* | |
490 | * Root hub will be first device to be initailized. | |
491 | * If this device is root-hub, don't do any xHC related | |
492 | * stuff. | |
493 | */ | |
494 | if (ctrl->rootdev == 0) { | |
495 | udev->speed = USB_SPEED_SUPER; | |
496 | return 0; | |
497 | } | |
498 | ||
499 | xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT); | |
500 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); | |
501 | BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)) | |
502 | != COMP_SUCCESS); | |
503 | ||
504 | udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)); | |
505 | ||
506 | xhci_acknowledge_event(ctrl); | |
507 | ||
508 | ret = xhci_alloc_virt_device(ctrl, udev->slot_id); | |
509 | if (ret < 0) { | |
510 | /* | |
511 | * TODO: Unsuccessful Address Device command shall leave | |
512 | * the slot in default. So, issue Disable Slot command now. | |
513 | */ | |
514 | puts("Could not allocate xHCI USB device data structures\n"); | |
515 | return ret; | |
516 | } | |
517 | ||
518 | return 0; | |
519 | } | |
520 | ||
521 | #ifndef CONFIG_DM_USB | |
522 | int usb_alloc_device(struct usb_device *udev) | |
523 | { | |
524 | return _xhci_alloc_device(udev); | |
525 | } | |
526 | #endif | |
527 | ||
528 | /* | |
529 | * Full speed devices may have a max packet size greater than 8 bytes, but the | |
530 | * USB core doesn't know that until it reads the first 8 bytes of the | |
531 | * descriptor. If the usb_device's max packet size changes after that point, | |
532 | * we need to issue an evaluate context command and wait on it. | |
533 | * | |
534 | * @param udev pointer to the Device Data Structure | |
535 | * @return returns the status of the xhci_configure_endpoints | |
536 | */ | |
537 | int xhci_check_maxpacket(struct usb_device *udev) | |
538 | { | |
539 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
540 | unsigned int slot_id = udev->slot_id; | |
541 | int ep_index = 0; /* control endpoint */ | |
542 | struct xhci_container_ctx *in_ctx; | |
543 | struct xhci_container_ctx *out_ctx; | |
544 | struct xhci_input_control_ctx *ctrl_ctx; | |
545 | struct xhci_ep_ctx *ep_ctx; | |
546 | int max_packet_size; | |
547 | int hw_max_packet_size; | |
548 | int ret = 0; | |
549 | struct usb_interface *ifdesc; | |
550 | ||
551 | ifdesc = &udev->config.if_desc[0]; | |
552 | ||
553 | out_ctx = ctrl->devs[slot_id]->out_ctx; | |
554 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); | |
555 | ||
556 | ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); | |
557 | hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); | |
558 | max_packet_size = usb_endpoint_maxp(&ifdesc->ep_desc[0]); | |
559 | if (hw_max_packet_size != max_packet_size) { | |
560 | debug("Max Packet Size for ep 0 changed.\n"); | |
561 | debug("Max packet size in usb_device = %d\n", max_packet_size); | |
562 | debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size); | |
563 | debug("Issuing evaluate context command.\n"); | |
564 | ||
565 | /* Set up the modified control endpoint 0 */ | |
566 | xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx, | |
567 | ctrl->devs[slot_id]->out_ctx, ep_index); | |
568 | in_ctx = ctrl->devs[slot_id]->in_ctx; | |
569 | ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); | |
570 | ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK); | |
571 | ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); | |
572 | ||
573 | /* | |
574 | * Set up the input context flags for the command | |
575 | * FIXME: This won't work if a non-default control endpoint | |
576 | * changes max packet sizes. | |
577 | */ | |
578 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); | |
579 | ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); | |
580 | ctrl_ctx->drop_flags = 0; | |
581 | ||
582 | ret = xhci_configure_endpoints(udev, true); | |
583 | } | |
584 | return ret; | |
585 | } | |
586 | ||
587 | /** | |
588 | * Clears the Change bits of the Port Status Register | |
589 | * | |
590 | * @param wValue request value | |
591 | * @param wIndex request index | |
592 | * @param addr address of posrt status register | |
593 | * @param port_status state of port status register | |
594 | * @return none | |
595 | */ | |
596 | static void xhci_clear_port_change_bit(u16 wValue, | |
597 | u16 wIndex, volatile uint32_t *addr, u32 port_status) | |
598 | { | |
599 | char *port_change_bit; | |
600 | u32 status; | |
601 | ||
602 | switch (wValue) { | |
603 | case USB_PORT_FEAT_C_RESET: | |
604 | status = PORT_RC; | |
605 | port_change_bit = "reset"; | |
606 | break; | |
607 | case USB_PORT_FEAT_C_CONNECTION: | |
608 | status = PORT_CSC; | |
609 | port_change_bit = "connect"; | |
610 | break; | |
611 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
612 | status = PORT_OCC; | |
613 | port_change_bit = "over-current"; | |
614 | break; | |
615 | case USB_PORT_FEAT_C_ENABLE: | |
616 | status = PORT_PEC; | |
617 | port_change_bit = "enable/disable"; | |
618 | break; | |
619 | case USB_PORT_FEAT_C_SUSPEND: | |
620 | status = PORT_PLC; | |
621 | port_change_bit = "suspend/resume"; | |
622 | break; | |
623 | default: | |
624 | /* Should never happen */ | |
625 | return; | |
626 | } | |
627 | ||
628 | /* Change bits are all write 1 to clear */ | |
629 | xhci_writel(addr, port_status | status); | |
630 | ||
631 | port_status = xhci_readl(addr); | |
632 | debug("clear port %s change, actual port %d status = 0x%x\n", | |
633 | port_change_bit, wIndex, port_status); | |
634 | } | |
635 | ||
636 | /** | |
637 | * Save Read Only (RO) bits and save read/write bits where | |
638 | * writing a 0 clears the bit and writing a 1 sets the bit (RWS). | |
639 | * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. | |
640 | * | |
641 | * @param state state of the Port Status and Control Regsiter | |
642 | * @return a value that would result in the port being in the | |
643 | * same state, if the value was written to the port | |
644 | * status control register. | |
645 | */ | |
646 | static u32 xhci_port_state_to_neutral(u32 state) | |
647 | { | |
648 | /* Save read-only status and port state */ | |
649 | return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS); | |
650 | } | |
651 | ||
652 | /** | |
653 | * Submits the Requests to the XHCI Host Controller | |
654 | * | |
655 | * @param udev pointer to the USB device structure | |
656 | * @param pipe contains the DIR_IN or OUT , devnum | |
657 | * @param buffer buffer to be read/written based on the request | |
658 | * @return returns 0 if successful else -1 on failure | |
659 | */ | |
660 | static int xhci_submit_root(struct usb_device *udev, unsigned long pipe, | |
661 | void *buffer, struct devrequest *req) | |
662 | { | |
663 | uint8_t tmpbuf[4]; | |
664 | u16 typeReq; | |
665 | void *srcptr = NULL; | |
666 | int len, srclen; | |
667 | uint32_t reg; | |
668 | volatile uint32_t *status_reg; | |
669 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
670 | struct xhci_hccr *hccr = ctrl->hccr; | |
671 | struct xhci_hcor *hcor = ctrl->hcor; | |
672 | int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1)); | |
673 | ||
674 | if ((req->requesttype & USB_RT_PORT) && | |
675 | le16_to_cpu(req->index) > max_ports) { | |
676 | printf("The request port(%d) exceeds maximum port number\n", | |
677 | le16_to_cpu(req->index) - 1); | |
678 | return -EINVAL; | |
679 | } | |
680 | ||
681 | status_reg = (volatile uint32_t *) | |
682 | (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc); | |
683 | srclen = 0; | |
684 | ||
685 | typeReq = req->request | req->requesttype << 8; | |
686 | ||
687 | switch (typeReq) { | |
688 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: | |
689 | switch (le16_to_cpu(req->value) >> 8) { | |
690 | case USB_DT_DEVICE: | |
691 | debug("USB_DT_DEVICE request\n"); | |
692 | srcptr = &descriptor.device; | |
693 | srclen = 0x12; | |
694 | break; | |
695 | case USB_DT_CONFIG: | |
696 | debug("USB_DT_CONFIG config\n"); | |
697 | srcptr = &descriptor.config; | |
698 | srclen = 0x19; | |
699 | break; | |
700 | case USB_DT_STRING: | |
701 | debug("USB_DT_STRING config\n"); | |
702 | switch (le16_to_cpu(req->value) & 0xff) { | |
703 | case 0: /* Language */ | |
704 | srcptr = "\4\3\11\4"; | |
705 | srclen = 4; | |
706 | break; | |
707 | case 1: /* Vendor String */ | |
708 | srcptr = "\16\3U\0-\0B\0o\0o\0t\0"; | |
709 | srclen = 14; | |
710 | break; | |
711 | case 2: /* Product Name */ | |
712 | srcptr = "\52\3X\0H\0C\0I\0 " | |
713 | "\0H\0o\0s\0t\0 " | |
714 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; | |
715 | srclen = 42; | |
716 | break; | |
717 | default: | |
718 | printf("unknown value DT_STRING %x\n", | |
719 | le16_to_cpu(req->value)); | |
720 | goto unknown; | |
721 | } | |
722 | break; | |
723 | default: | |
724 | printf("unknown value %x\n", le16_to_cpu(req->value)); | |
725 | goto unknown; | |
726 | } | |
727 | break; | |
728 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): | |
729 | switch (le16_to_cpu(req->value) >> 8) { | |
730 | case USB_DT_HUB: | |
731 | case USB_DT_SS_HUB: | |
732 | debug("USB_DT_HUB config\n"); | |
733 | srcptr = &descriptor.hub; | |
734 | srclen = 0x8; | |
735 | break; | |
736 | default: | |
737 | printf("unknown value %x\n", le16_to_cpu(req->value)); | |
738 | goto unknown; | |
739 | } | |
740 | break; | |
741 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): | |
742 | debug("USB_REQ_SET_ADDRESS\n"); | |
743 | ctrl->rootdev = le16_to_cpu(req->value); | |
744 | break; | |
745 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
746 | /* Do nothing */ | |
747 | break; | |
748 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): | |
749 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ | |
750 | tmpbuf[1] = 0; | |
751 | srcptr = tmpbuf; | |
752 | srclen = 2; | |
753 | break; | |
754 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): | |
755 | memset(tmpbuf, 0, 4); | |
756 | reg = xhci_readl(status_reg); | |
757 | if (reg & PORT_CONNECT) { | |
758 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; | |
759 | switch (reg & DEV_SPEED_MASK) { | |
760 | case XDEV_FS: | |
761 | debug("SPEED = FULLSPEED\n"); | |
762 | break; | |
763 | case XDEV_LS: | |
764 | debug("SPEED = LOWSPEED\n"); | |
765 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; | |
766 | break; | |
767 | case XDEV_HS: | |
768 | debug("SPEED = HIGHSPEED\n"); | |
769 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; | |
770 | break; | |
771 | case XDEV_SS: | |
772 | debug("SPEED = SUPERSPEED\n"); | |
773 | tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8; | |
774 | break; | |
775 | } | |
776 | } | |
777 | if (reg & PORT_PE) | |
778 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; | |
779 | if ((reg & PORT_PLS_MASK) == XDEV_U3) | |
780 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; | |
781 | if (reg & PORT_OC) | |
782 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; | |
783 | if (reg & PORT_RESET) | |
784 | tmpbuf[0] |= USB_PORT_STAT_RESET; | |
785 | if (reg & PORT_POWER) | |
786 | /* | |
787 | * XXX: This Port power bit (for USB 3.0 hub) | |
788 | * we are faking in USB 2.0 hub port status; | |
789 | * since there's a change in bit positions in | |
790 | * two: | |
791 | * USB 2.0 port status PP is at position[8] | |
792 | * USB 3.0 port status PP is at position[9] | |
793 | * So, we are still keeping it at position [8] | |
794 | */ | |
795 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; | |
796 | if (reg & PORT_CSC) | |
797 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; | |
798 | if (reg & PORT_PEC) | |
799 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; | |
800 | if (reg & PORT_OCC) | |
801 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; | |
802 | if (reg & PORT_RC) | |
803 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; | |
804 | ||
805 | srcptr = tmpbuf; | |
806 | srclen = 4; | |
807 | break; | |
808 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): | |
809 | reg = xhci_readl(status_reg); | |
810 | reg = xhci_port_state_to_neutral(reg); | |
811 | switch (le16_to_cpu(req->value)) { | |
812 | case USB_PORT_FEAT_ENABLE: | |
813 | reg |= PORT_PE; | |
814 | xhci_writel(status_reg, reg); | |
815 | break; | |
816 | case USB_PORT_FEAT_POWER: | |
817 | reg |= PORT_POWER; | |
818 | xhci_writel(status_reg, reg); | |
819 | break; | |
820 | case USB_PORT_FEAT_RESET: | |
821 | reg |= PORT_RESET; | |
822 | xhci_writel(status_reg, reg); | |
823 | break; | |
824 | default: | |
825 | printf("unknown feature %x\n", le16_to_cpu(req->value)); | |
826 | goto unknown; | |
827 | } | |
828 | break; | |
829 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): | |
830 | reg = xhci_readl(status_reg); | |
831 | reg = xhci_port_state_to_neutral(reg); | |
832 | switch (le16_to_cpu(req->value)) { | |
833 | case USB_PORT_FEAT_ENABLE: | |
834 | reg &= ~PORT_PE; | |
835 | break; | |
836 | case USB_PORT_FEAT_POWER: | |
837 | reg &= ~PORT_POWER; | |
838 | break; | |
839 | case USB_PORT_FEAT_C_RESET: | |
840 | case USB_PORT_FEAT_C_CONNECTION: | |
841 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
842 | case USB_PORT_FEAT_C_ENABLE: | |
843 | xhci_clear_port_change_bit((le16_to_cpu(req->value)), | |
844 | le16_to_cpu(req->index), | |
845 | status_reg, reg); | |
846 | break; | |
847 | default: | |
848 | printf("unknown feature %x\n", le16_to_cpu(req->value)); | |
849 | goto unknown; | |
850 | } | |
851 | xhci_writel(status_reg, reg); | |
852 | break; | |
853 | default: | |
854 | puts("Unknown request\n"); | |
855 | goto unknown; | |
856 | } | |
857 | ||
858 | debug("scrlen = %d\n req->length = %d\n", | |
859 | srclen, le16_to_cpu(req->length)); | |
860 | ||
861 | len = min(srclen, (int)le16_to_cpu(req->length)); | |
862 | ||
863 | if (srcptr != NULL && len > 0) | |
864 | memcpy(buffer, srcptr, len); | |
865 | else | |
866 | debug("Len is 0\n"); | |
867 | ||
868 | udev->act_len = len; | |
869 | udev->status = 0; | |
870 | ||
871 | return 0; | |
872 | ||
873 | unknown: | |
874 | udev->act_len = 0; | |
875 | udev->status = USB_ST_STALLED; | |
876 | ||
877 | return -ENODEV; | |
878 | } | |
879 | ||
880 | /** | |
881 | * Submits the INT request to XHCI Host cotroller | |
882 | * | |
883 | * @param udev pointer to the USB device | |
884 | * @param pipe contains the DIR_IN or OUT , devnum | |
885 | * @param buffer buffer to be read/written based on the request | |
886 | * @param length length of the buffer | |
887 | * @param interval interval of the interrupt | |
888 | * @return 0 | |
889 | */ | |
890 | static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe, | |
891 | void *buffer, int length, int interval) | |
892 | { | |
893 | /* | |
894 | * TODO: Not addressing any interrupt type transfer requests | |
895 | * Add support for it later. | |
896 | */ | |
897 | return -EINVAL; | |
898 | } | |
899 | ||
900 | /** | |
901 | * submit the BULK type of request to the USB Device | |
902 | * | |
903 | * @param udev pointer to the USB device | |
904 | * @param pipe contains the DIR_IN or OUT , devnum | |
905 | * @param buffer buffer to be read/written based on the request | |
906 | * @param length length of the buffer | |
907 | * @return returns 0 if successful else -1 on failure | |
908 | */ | |
909 | static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe, | |
910 | void *buffer, int length) | |
911 | { | |
912 | if (usb_pipetype(pipe) != PIPE_BULK) { | |
913 | printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); | |
914 | return -EINVAL; | |
915 | } | |
916 | ||
917 | return xhci_bulk_tx(udev, pipe, length, buffer); | |
918 | } | |
919 | ||
920 | /** | |
921 | * submit the control type of request to the Root hub/Device based on the devnum | |
922 | * | |
923 | * @param udev pointer to the USB device | |
924 | * @param pipe contains the DIR_IN or OUT , devnum | |
925 | * @param buffer buffer to be read/written based on the request | |
926 | * @param length length of the buffer | |
927 | * @param setup Request type | |
928 | * @param root_portnr Root port number that this device is on | |
929 | * @return returns 0 if successful else -1 on failure | |
930 | */ | |
931 | static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe, | |
932 | void *buffer, int length, | |
933 | struct devrequest *setup, int root_portnr) | |
934 | { | |
935 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); | |
936 | int ret = 0; | |
937 | ||
938 | if (usb_pipetype(pipe) != PIPE_CONTROL) { | |
939 | printf("non-control pipe (type=%lu)", usb_pipetype(pipe)); | |
940 | return -EINVAL; | |
941 | } | |
942 | ||
943 | if (usb_pipedevice(pipe) == ctrl->rootdev) | |
944 | return xhci_submit_root(udev, pipe, buffer, setup); | |
945 | ||
946 | if (setup->request == USB_REQ_SET_ADDRESS && | |
947 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) | |
948 | return xhci_address_device(udev, root_portnr); | |
949 | ||
950 | if (setup->request == USB_REQ_SET_CONFIGURATION && | |
951 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) { | |
952 | ret = xhci_set_configuration(udev); | |
953 | if (ret) { | |
954 | puts("Failed to configure xHCI endpoint\n"); | |
955 | return ret; | |
956 | } | |
957 | } | |
958 | ||
959 | return xhci_ctrl_tx(udev, pipe, setup, length, buffer); | |
960 | } | |
961 | ||
962 | static int xhci_lowlevel_init(struct xhci_ctrl *ctrl) | |
963 | { | |
964 | struct xhci_hccr *hccr; | |
965 | struct xhci_hcor *hcor; | |
966 | uint32_t val; | |
967 | uint32_t val2; | |
968 | uint32_t reg; | |
969 | ||
970 | hccr = ctrl->hccr; | |
971 | hcor = ctrl->hcor; | |
972 | /* | |
973 | * Program the Number of Device Slots Enabled field in the CONFIG | |
974 | * register with the max value of slots the HC can handle. | |
975 | */ | |
976 | val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK); | |
977 | val2 = xhci_readl(&hcor->or_config); | |
978 | val |= (val2 & ~HCS_SLOTS_MASK); | |
979 | xhci_writel(&hcor->or_config, val); | |
980 | ||
981 | /* initializing xhci data structures */ | |
982 | if (xhci_mem_init(ctrl, hccr, hcor) < 0) | |
983 | return -ENOMEM; | |
984 | ||
985 | reg = xhci_readl(&hccr->cr_hcsparams1); | |
986 | descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >> | |
987 | HCS_MAX_PORTS_SHIFT); | |
988 | printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); | |
989 | ||
990 | /* Port Indicators */ | |
991 | reg = xhci_readl(&hccr->cr_hccparams); | |
992 | if (HCS_INDICATOR(reg)) | |
993 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) | |
994 | | 0x80, &descriptor.hub.wHubCharacteristics); | |
995 | ||
996 | /* Port Power Control */ | |
997 | if (HCC_PPC(reg)) | |
998 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) | |
999 | | 0x01, &descriptor.hub.wHubCharacteristics); | |
1000 | ||
1001 | if (xhci_start(hcor)) { | |
1002 | xhci_reset(hcor); | |
1003 | return -ENODEV; | |
1004 | } | |
1005 | ||
1006 | /* Zero'ing IRQ control register and IRQ pending register */ | |
1007 | xhci_writel(&ctrl->ir_set->irq_control, 0x0); | |
1008 | xhci_writel(&ctrl->ir_set->irq_pending, 0x0); | |
1009 | ||
1010 | reg = HC_VERSION(xhci_readl(&hccr->cr_capbase)); | |
1011 | printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff); | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl) | |
1017 | { | |
1018 | u32 temp; | |
1019 | ||
1020 | xhci_reset(ctrl->hcor); | |
1021 | ||
1022 | debug("// Disabling event ring interrupts\n"); | |
1023 | temp = xhci_readl(&ctrl->hcor->or_usbsts); | |
1024 | xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT); | |
1025 | temp = xhci_readl(&ctrl->ir_set->irq_pending); | |
1026 | xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp)); | |
1027 | ||
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | #ifndef CONFIG_DM_USB | |
1032 | int submit_control_msg(struct usb_device *udev, unsigned long pipe, | |
1033 | void *buffer, int length, struct devrequest *setup) | |
1034 | { | |
1035 | struct usb_device *hop = udev; | |
1036 | ||
1037 | if (hop->parent) | |
1038 | while (hop->parent->parent) | |
1039 | hop = hop->parent; | |
1040 | ||
1041 | return _xhci_submit_control_msg(udev, pipe, buffer, length, setup, | |
1042 | hop->portnr); | |
1043 | } | |
1044 | ||
1045 | int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, | |
1046 | int length) | |
1047 | { | |
1048 | return _xhci_submit_bulk_msg(udev, pipe, buffer, length); | |
1049 | } | |
1050 | ||
1051 | int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, | |
1052 | int length, int interval) | |
1053 | { | |
1054 | return _xhci_submit_int_msg(udev, pipe, buffer, length, interval); | |
1055 | } | |
1056 | ||
1057 | /** | |
1058 | * Intialises the XHCI host controller | |
1059 | * and allocates the necessary data structures | |
1060 | * | |
1061 | * @param index index to the host controller data structure | |
1062 | * @return pointer to the intialised controller | |
1063 | */ | |
1064 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) | |
1065 | { | |
1066 | struct xhci_hccr *hccr; | |
1067 | struct xhci_hcor *hcor; | |
1068 | struct xhci_ctrl *ctrl; | |
1069 | int ret; | |
1070 | ||
1071 | *controller = NULL; | |
1072 | ||
1073 | if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0) | |
1074 | return -ENODEV; | |
1075 | ||
1076 | if (xhci_reset(hcor) != 0) | |
1077 | return -ENODEV; | |
1078 | ||
1079 | ctrl = &xhcic[index]; | |
1080 | ||
1081 | ctrl->hccr = hccr; | |
1082 | ctrl->hcor = hcor; | |
1083 | ||
1084 | ret = xhci_lowlevel_init(ctrl); | |
1085 | ||
1086 | if (ret) { | |
1087 | ctrl->hccr = NULL; | |
1088 | ctrl->hcor = NULL; | |
1089 | } else { | |
1090 | *controller = &xhcic[index]; | |
1091 | } | |
1092 | ||
1093 | return ret; | |
1094 | } | |
1095 | ||
1096 | /** | |
1097 | * Stops the XHCI host controller | |
1098 | * and cleans up all the related data structures | |
1099 | * | |
1100 | * @param index index to the host controller data structure | |
1101 | * @return none | |
1102 | */ | |
1103 | int usb_lowlevel_stop(int index) | |
1104 | { | |
1105 | struct xhci_ctrl *ctrl = (xhcic + index); | |
1106 | ||
1107 | if (ctrl->hcor) { | |
1108 | xhci_lowlevel_stop(ctrl); | |
1109 | xhci_hcd_stop(index); | |
1110 | xhci_cleanup(ctrl); | |
1111 | } | |
1112 | ||
1113 | return 0; | |
1114 | } | |
1115 | #endif /* CONFIG_DM_USB */ | |
1116 | ||
1117 | #ifdef CONFIG_DM_USB | |
1118 | ||
1119 | static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev, | |
1120 | unsigned long pipe, void *buffer, int length, | |
1121 | struct devrequest *setup) | |
1122 | { | |
1123 | struct usb_device *uhop; | |
1124 | struct udevice *hub; | |
1125 | int root_portnr = 0; | |
1126 | ||
1127 | debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__, | |
1128 | dev->name, udev, udev->dev->name, udev->portnr); | |
1129 | hub = udev->dev; | |
1130 | if (device_get_uclass_id(hub) == UCLASS_USB_HUB) { | |
1131 | /* Figure out our port number on the root hub */ | |
1132 | if (usb_hub_is_root_hub(hub)) { | |
1133 | root_portnr = udev->portnr; | |
1134 | } else { | |
1135 | while (!usb_hub_is_root_hub(hub->parent)) | |
1136 | hub = hub->parent; | |
1137 | uhop = dev_get_parent_priv(hub); | |
1138 | root_portnr = uhop->portnr; | |
1139 | } | |
1140 | } | |
1141 | /* | |
1142 | struct usb_device *hop = udev; | |
1143 | ||
1144 | if (hop->parent) | |
1145 | while (hop->parent->parent) | |
1146 | hop = hop->parent; | |
1147 | */ | |
1148 | return _xhci_submit_control_msg(udev, pipe, buffer, length, setup, | |
1149 | root_portnr); | |
1150 | } | |
1151 | ||
1152 | static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, | |
1153 | unsigned long pipe, void *buffer, int length) | |
1154 | { | |
1155 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); | |
1156 | return _xhci_submit_bulk_msg(udev, pipe, buffer, length); | |
1157 | } | |
1158 | ||
1159 | static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev, | |
1160 | unsigned long pipe, void *buffer, int length, | |
1161 | int interval) | |
1162 | { | |
1163 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); | |
1164 | return _xhci_submit_int_msg(udev, pipe, buffer, length, interval); | |
1165 | } | |
1166 | ||
1167 | static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev) | |
1168 | { | |
1169 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); | |
1170 | return _xhci_alloc_device(udev); | |
1171 | } | |
1172 | ||
1173 | static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev) | |
1174 | { | |
1175 | struct xhci_ctrl *ctrl = dev_get_priv(dev); | |
1176 | struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev); | |
1177 | struct xhci_virt_device *virt_dev; | |
1178 | struct xhci_input_control_ctx *ctrl_ctx; | |
1179 | struct xhci_container_ctx *out_ctx; | |
1180 | struct xhci_container_ctx *in_ctx; | |
1181 | struct xhci_slot_ctx *slot_ctx; | |
1182 | int slot_id = udev->slot_id; | |
1183 | unsigned think_time; | |
1184 | ||
1185 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); | |
1186 | ||
1187 | /* Ignore root hubs */ | |
1188 | if (usb_hub_is_root_hub(udev->dev)) | |
1189 | return 0; | |
1190 | ||
1191 | virt_dev = ctrl->devs[slot_id]; | |
1192 | BUG_ON(!virt_dev); | |
1193 | ||
1194 | out_ctx = virt_dev->out_ctx; | |
1195 | in_ctx = virt_dev->in_ctx; | |
1196 | ||
1197 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); | |
1198 | /* Initialize the input context control */ | |
1199 | ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG); | |
1200 | ctrl_ctx->drop_flags = 0; | |
1201 | ||
1202 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); | |
1203 | ||
1204 | /* slot context */ | |
1205 | xhci_slot_copy(ctrl, in_ctx, out_ctx); | |
1206 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); | |
1207 | ||
1208 | /* Update hub related fields */ | |
1209 | slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); | |
1210 | if (hub->tt.multi && udev->speed == USB_SPEED_HIGH) | |
1211 | slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); | |
1212 | slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild)); | |
1213 | /* | |
1214 | * Set TT think time - convert from ns to FS bit times. | |
1215 | * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns | |
1216 | * | |
1217 | * 0 = 8 FS bit times, 1 = 16 FS bit times, | |
1218 | * 2 = 24 FS bit times, 3 = 32 FS bit times. | |
1219 | * | |
1220 | * This field shall be 0 if the device is not a high-spped hub. | |
1221 | */ | |
1222 | think_time = hub->tt.think_time; | |
1223 | if (think_time != 0) | |
1224 | think_time = (think_time / 666) - 1; | |
1225 | if (udev->speed == USB_SPEED_HIGH) | |
1226 | slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time)); | |
1227 | ||
1228 | return xhci_configure_endpoints(udev, false); | |
1229 | } | |
1230 | ||
1231 | int xhci_register(struct udevice *dev, struct xhci_hccr *hccr, | |
1232 | struct xhci_hcor *hcor) | |
1233 | { | |
1234 | struct xhci_ctrl *ctrl = dev_get_priv(dev); | |
1235 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); | |
1236 | int ret; | |
1237 | ||
1238 | debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name, | |
1239 | ctrl, hccr, hcor); | |
1240 | ||
1241 | ctrl->dev = dev; | |
1242 | ||
1243 | /* | |
1244 | * XHCI needs to issue a Address device command to setup | |
1245 | * proper device context structures, before it can interact | |
1246 | * with the device. So a get_descriptor will fail before any | |
1247 | * of that is done for XHCI unlike EHCI. | |
1248 | */ | |
1249 | priv->desc_before_addr = false; | |
1250 | ||
1251 | ret = xhci_reset(hcor); | |
1252 | if (ret) | |
1253 | goto err; | |
1254 | ||
1255 | ctrl->hccr = hccr; | |
1256 | ctrl->hcor = hcor; | |
1257 | ret = xhci_lowlevel_init(ctrl); | |
1258 | if (ret) | |
1259 | goto err; | |
1260 | ||
1261 | return 0; | |
1262 | err: | |
1263 | free(ctrl); | |
1264 | debug("%s: failed, ret=%d\n", __func__, ret); | |
1265 | return ret; | |
1266 | } | |
1267 | ||
1268 | int xhci_deregister(struct udevice *dev) | |
1269 | { | |
1270 | struct xhci_ctrl *ctrl = dev_get_priv(dev); | |
1271 | ||
1272 | xhci_lowlevel_stop(ctrl); | |
1273 | xhci_cleanup(ctrl); | |
1274 | ||
1275 | return 0; | |
1276 | } | |
1277 | ||
1278 | struct dm_usb_ops xhci_usb_ops = { | |
1279 | .control = xhci_submit_control_msg, | |
1280 | .bulk = xhci_submit_bulk_msg, | |
1281 | .interrupt = xhci_submit_int_msg, | |
1282 | .alloc_device = xhci_alloc_device, | |
1283 | .update_hub_device = xhci_update_hub_device, | |
1284 | }; | |
1285 | ||
1286 | #endif |