Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 1 | /* |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 2 | * Copyright 2015,2016 Freescale Semiconductor, Inc. |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 3 | * |
| 4 | * FSL USB HOST xHCI Controller |
| 5 | * |
| 6 | * Author: Ramneek Mehresh<ramneek.mehresh@freescale.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <usb.h> |
Masahiro Yamada | 5d97dff | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 13 | #include <linux/errno.h> |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 14 | #include <linux/compat.h> |
| 15 | #include <linux/usb/xhci-fsl.h> |
| 16 | #include <linux/usb/dwc3.h> |
| 17 | #include "xhci.h" |
Sriram Dash | ef53b8c | 2016-06-13 09:58:36 +0530 | [diff] [blame] | 18 | #include <fsl_errata.h> |
| 19 | #include <fsl_usb.h> |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 20 | #include <dm.h> |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 21 | |
| 22 | /* Declare global data pointer */ |
| 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 25 | #ifndef CONFIG_DM_USB |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 26 | static struct fsl_xhci fsl_xhci; |
| 27 | unsigned long ctr_addr[] = FSL_USB_XHCI_ADDR; |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 28 | #else |
| 29 | struct xhci_fsl_priv { |
| 30 | struct xhci_ctrl xhci; |
| 31 | fdt_addr_t hcd_base; |
| 32 | struct fsl_xhci ctx; |
| 33 | }; |
| 34 | #endif |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 35 | |
| 36 | __weak int __board_usb_init(int index, enum usb_init_type init) |
| 37 | { |
| 38 | return 0; |
| 39 | } |
| 40 | |
Sriram Dash | ef53b8c | 2016-06-13 09:58:36 +0530 | [diff] [blame] | 41 | static int erratum_a008751(void) |
| 42 | { |
| 43 | #if defined(CONFIG_TARGET_LS2080AQDS) || defined(CONFIG_TARGET_LS2080ARDB) |
| 44 | u32 __iomem *scfg = (u32 __iomem *)SCFG_BASE; |
| 45 | writel(SCFG_USB3PRM1CR_INIT, scfg + SCFG_USB3PRM1CR / 4); |
| 46 | return 0; |
| 47 | #endif |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | static void fsl_apply_xhci_errata(void) |
| 52 | { |
| 53 | int ret; |
| 54 | if (has_erratum_a008751()) { |
| 55 | ret = erratum_a008751(); |
| 56 | if (ret != 0) |
| 57 | puts("Failed to apply erratum a008751\n"); |
| 58 | } |
| 59 | } |
| 60 | |
Sriram Dash | e915716 | 2016-08-22 17:55:15 +0530 | [diff] [blame] | 61 | static void fsl_xhci_set_beat_burst_length(struct dwc3 *dwc3_reg) |
| 62 | { |
| 63 | clrsetbits_le32(&dwc3_reg->g_sbuscfg0, USB3_ENABLE_BEAT_BURST_MASK, |
| 64 | USB3_ENABLE_BEAT_BURST); |
| 65 | setbits_le32(&dwc3_reg->g_sbuscfg1, USB3_SET_BEAT_BURST_LIMIT); |
| 66 | } |
| 67 | |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 68 | static int fsl_xhci_core_init(struct fsl_xhci *fsl_xhci) |
| 69 | { |
| 70 | int ret = 0; |
| 71 | |
| 72 | ret = dwc3_core_init(fsl_xhci->dwc3_reg); |
| 73 | if (ret) { |
| 74 | debug("%s:failed to initialize core\n", __func__); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | /* We are hard-coding DWC3 core to Host Mode */ |
| 79 | dwc3_set_mode(fsl_xhci->dwc3_reg, DWC3_GCTL_PRTCAP_HOST); |
| 80 | |
Nikhil Badola | 667f4dd | 2015-06-23 09:17:49 +0530 | [diff] [blame] | 81 | /* Set GFLADJ_30MHZ as 20h as per XHCI spec default value */ |
| 82 | dwc3_set_fladj(fsl_xhci->dwc3_reg, GFLADJ_30MHZ_DEFAULT); |
| 83 | |
Sriram Dash | e915716 | 2016-08-22 17:55:15 +0530 | [diff] [blame] | 84 | /* Change beat burst and outstanding pipelined transfers requests */ |
| 85 | fsl_xhci_set_beat_burst_length(fsl_xhci->dwc3_reg); |
| 86 | |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static int fsl_xhci_core_exit(struct fsl_xhci *fsl_xhci) |
| 91 | { |
| 92 | /* |
| 93 | * Currently fsl socs do not support PHY shutdown from |
| 94 | * sw. But this support may be added in future socs. |
| 95 | */ |
| 96 | return 0; |
| 97 | } |
| 98 | |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 99 | #ifdef CONFIG_DM_USB |
| 100 | static int xhci_fsl_probe(struct udevice *dev) |
| 101 | { |
| 102 | struct xhci_fsl_priv *priv = dev_get_priv(dev); |
| 103 | struct xhci_hccr *hccr; |
| 104 | struct xhci_hcor *hcor; |
| 105 | |
| 106 | int ret = 0; |
| 107 | |
| 108 | /* |
| 109 | * Get the base address for XHCI controller from the device node |
| 110 | */ |
| 111 | priv->hcd_base = dev_get_addr(dev); |
| 112 | if (priv->hcd_base == FDT_ADDR_T_NONE) { |
| 113 | debug("Can't get the XHCI register base address\n"); |
| 114 | return -ENXIO; |
| 115 | } |
| 116 | priv->ctx.hcd = (struct xhci_hccr *)priv->hcd_base; |
| 117 | priv->ctx.dwc3_reg = (struct dwc3 *)((char *)(priv->hcd_base) + |
| 118 | DWC3_REG_OFFSET); |
| 119 | |
| 120 | fsl_apply_xhci_errata(); |
| 121 | |
| 122 | ret = fsl_xhci_core_init(&priv->ctx); |
| 123 | if (ret < 0) { |
| 124 | puts("Failed to initialize xhci\n"); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | hccr = (struct xhci_hccr *)(priv->ctx.hcd); |
| 129 | hcor = (struct xhci_hcor *)((uintptr_t) hccr |
| 130 | + HC_LENGTH(xhci_readl(&hccr->cr_capbase))); |
| 131 | |
| 132 | debug("xhci-fsl: init hccr %lx and hcor %lx hc_length %lx\n", |
| 133 | (uintptr_t)hccr, (uintptr_t)hcor, |
| 134 | (uintptr_t)HC_LENGTH(xhci_readl(&hccr->cr_capbase))); |
| 135 | |
| 136 | return xhci_register(dev, hccr, hcor); |
| 137 | } |
| 138 | |
| 139 | static int xhci_fsl_remove(struct udevice *dev) |
| 140 | { |
| 141 | struct xhci_fsl_priv *priv = dev_get_priv(dev); |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 142 | |
| 143 | fsl_xhci_core_exit(&priv->ctx); |
| 144 | |
Masahiro Yamada | 8319aeb | 2016-09-06 22:17:35 +0900 | [diff] [blame] | 145 | return xhci_deregister(dev); |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static const struct udevice_id xhci_usb_ids[] = { |
| 149 | { .compatible = "fsl,layerscape-dwc3", }, |
| 150 | { } |
| 151 | }; |
| 152 | |
| 153 | U_BOOT_DRIVER(xhci_fsl) = { |
| 154 | .name = "xhci_fsl", |
| 155 | .id = UCLASS_USB, |
| 156 | .of_match = xhci_usb_ids, |
| 157 | .probe = xhci_fsl_probe, |
| 158 | .remove = xhci_fsl_remove, |
| 159 | .ops = &xhci_usb_ops, |
| 160 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 161 | .priv_auto_alloc_size = sizeof(struct xhci_fsl_priv), |
| 162 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 163 | }; |
| 164 | #else |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 165 | int xhci_hcd_init(int index, struct xhci_hccr **hccr, struct xhci_hcor **hcor) |
| 166 | { |
| 167 | struct fsl_xhci *ctx = &fsl_xhci; |
| 168 | int ret = 0; |
| 169 | |
| 170 | ctx->hcd = (struct xhci_hccr *)ctr_addr[index]; |
| 171 | ctx->dwc3_reg = (struct dwc3 *)((char *)(ctx->hcd) + DWC3_REG_OFFSET); |
| 172 | |
| 173 | ret = board_usb_init(index, USB_INIT_HOST); |
| 174 | if (ret != 0) { |
| 175 | puts("Failed to initialize board for USB\n"); |
| 176 | return ret; |
| 177 | } |
| 178 | |
Sriram Dash | ef53b8c | 2016-06-13 09:58:36 +0530 | [diff] [blame] | 179 | fsl_apply_xhci_errata(); |
| 180 | |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 181 | ret = fsl_xhci_core_init(ctx); |
| 182 | if (ret < 0) { |
| 183 | puts("Failed to initialize xhci\n"); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | *hccr = (struct xhci_hccr *)ctx->hcd; |
Nikhil Badola | 7e5a32f | 2015-06-23 09:17:32 +0530 | [diff] [blame] | 188 | *hcor = (struct xhci_hcor *)((uintptr_t) *hccr |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 189 | + HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase))); |
| 190 | |
Nikhil Badola | 7e5a32f | 2015-06-23 09:17:32 +0530 | [diff] [blame] | 191 | debug("fsl-xhci: init hccr %lx and hcor %lx hc_length %lx\n", |
| 192 | (uintptr_t)*hccr, (uintptr_t)*hcor, |
| 193 | (uintptr_t)HC_LENGTH(xhci_readl(&(*hccr)->cr_capbase))); |
Ramneek Mehresh | ba92ee0 | 2015-05-29 14:47:19 +0530 | [diff] [blame] | 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | void xhci_hcd_stop(int index) |
| 199 | { |
| 200 | struct fsl_xhci *ctx = &fsl_xhci; |
| 201 | |
| 202 | fsl_xhci_core_exit(ctx); |
| 203 | } |
Rajesh Bhagat | 707c866 | 2016-07-01 18:51:47 +0530 | [diff] [blame] | 204 | #endif |