blob: c4cbeeaab84bcb5afc08f4b8148b8e1e1e660d6e [file] [log] [blame]
Patrick Georgi6615ef32010-08-13 09:18:58 +00001/*
2 * This file is part of the libpayload project.
3 *
Nico Huber90292652013-06-13 14:37:15 +02004 * Copyright (C) 2013 secunet Security Networks AG
Patrick Georgi6615ef32010-08-13 09:18:58 +00005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Julius Werner1f864342013-09-03 17:15:31 -070030//#define USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +000031
Nico Huber90292652013-06-13 14:37:15 +020032#include <usb/usb.h>
33#include "generic_hub.h"
Patrick Georgi6615ef32010-08-13 09:18:58 +000034#include "xhci_private.h"
35#include "xhci.h"
36
Nico Huber90292652013-06-13 14:37:15 +020037static int
38xhci_rh_hub_status_changed(usbdev_t *const dev)
Patrick Georgi6615ef32010-08-13 09:18:58 +000039{
Nico Huber90292652013-06-13 14:37:15 +020040 xhci_t *const xhci = XHCI_INST(dev->controller);
41 const int changed = !!(xhci->opreg->usbsts & USBSTS_PCD);
42 if (changed)
43 xhci->opreg->usbsts =
44 (xhci->opreg->usbsts & USBSTS_PRSRV_MASK) | USBSTS_PCD;
45 return changed;
Patrick Georgi6615ef32010-08-13 09:18:58 +000046}
47
48static int
Nico Huber90292652013-06-13 14:37:15 +020049xhci_rh_port_status_changed(usbdev_t *const dev, const int port)
Patrick Georgi6615ef32010-08-13 09:18:58 +000050{
Nico Huber90292652013-06-13 14:37:15 +020051 xhci_t *const xhci = XHCI_INST(dev->controller);
52 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
53
54 const int changed = !!(*portsc & PORTSC_CSC);
55 /* always clear all the status change bits */
56 *portsc = (*portsc & PORTSC_RW_MASK) | 0x00ef0000;
57 return changed;
58}
59
60static int
61xhci_rh_port_connected(usbdev_t *const dev, const int port)
62{
63 xhci_t *const xhci = XHCI_INST(dev->controller);
64 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
65
66 return *portsc & PORTSC_CCS;
67}
68
69static int
70xhci_rh_port_in_reset(usbdev_t *const dev, const int port)
71{
72 xhci_t *const xhci = XHCI_INST(dev->controller);
73 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
74
75 return !!(*portsc & PORTSC_PR);
76}
77
78static int
79xhci_rh_port_enabled(usbdev_t *const dev, const int port)
80{
81 xhci_t *const xhci = XHCI_INST(dev->controller);
82 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
83
84 return !!(*portsc & PORTSC_PED);
85}
86
Julius Wernere00ba212013-09-24 20:03:54 -070087static usb_speed
Nico Huber90292652013-06-13 14:37:15 +020088xhci_rh_port_speed(usbdev_t *const dev, const int port)
89{
90 xhci_t *const xhci = XHCI_INST(dev->controller);
91 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
92
93 if (*portsc & PORTSC_PED) {
94 return ((*portsc & PORTSC_PORT_SPEED_MASK)
95 >> PORTSC_PORT_SPEED_START)
96 - 1;
97 } else {
Patrick Georgi6615ef32010-08-13 09:18:58 +000098 return -1;
Patrick Georgi6615ef32010-08-13 09:18:58 +000099 }
Patrick Georgi6615ef32010-08-13 09:18:58 +0000100}
101
Nico Huber90292652013-06-13 14:37:15 +0200102static int
103xhci_rh_start_port_reset(usbdev_t *const dev, const int port)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000104{
Nico Huber90292652013-06-13 14:37:15 +0200105 xhci_t *const xhci = XHCI_INST(dev->controller);
106 volatile u32 *const portsc = &xhci->opreg->prs[port - 1].portsc;
107
108 *portsc = (*portsc & PORTSC_RW_MASK) | PORTSC_PR;
109 return 0;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000110}
111
Nico Huber90292652013-06-13 14:37:15 +0200112static const generic_hub_ops_t xhci_rh_ops = {
113 .hub_status_changed = xhci_rh_hub_status_changed,
114 .port_status_changed = xhci_rh_port_status_changed,
115 .port_connected = xhci_rh_port_connected,
116 .port_in_reset = xhci_rh_port_in_reset,
117 .port_enabled = xhci_rh_port_enabled,
118 .port_speed = xhci_rh_port_speed,
119 .enable_port = NULL,
120 .disable_port = NULL,
121 .start_port_reset = xhci_rh_start_port_reset,
122 .reset_port = generic_hub_rh_resetport,
123};
Patrick Georgi6615ef32010-08-13 09:18:58 +0000124
125void
126xhci_rh_init (usbdev_t *dev)
127{
Patrick Georgi6615ef32010-08-13 09:18:58 +0000128 /* we can set them here because a root hub _really_ shouldn't
129 appear elsewhere */
130 dev->address = 0;
131 dev->hub = -1;
132 dev->port = -1;
133
Nico Huber90292652013-06-13 14:37:15 +0200134 const int num_ports = /* TODO: maybe we need to read extended caps */
135 (XHCI_INST(dev->controller)->capreg->hcsparams1 >> 24) & 0xff;
136 generic_hub_init(dev, num_ports, &xhci_rh_ops);
137
138 usb_debug("xHCI: root hub init done\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000139}