Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com> |
| 4 | * |
| 5 | * Renesas RCar USB HOST xHCI Controller |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 13 | #include <usb.h> |
| 14 | #include <wait_bit.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 17 | |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 18 | #include <usb/xhci.h> |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 19 | #include "xhci-rcar-r8a779x_usb3_v3.h" |
| 20 | |
| 21 | /* Register Offset */ |
| 22 | #define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */ |
| 23 | #define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */ |
| 24 | |
| 25 | /* Register Settings */ |
| 26 | /* FW Download Control & Status */ |
| 27 | #define RCAR_USB3_DL_CTRL_ENABLE BIT(0) |
| 28 | #define RCAR_USB3_DL_CTRL_FW_SUCCESS BIT(4) |
| 29 | #define RCAR_USB3_DL_CTRL_FW_SET_DATA0 BIT(8) |
| 30 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 31 | struct rcar_xhci_plat { |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 32 | fdt_addr_t hcd_base; |
| 33 | struct clk clk; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * Contains pointers to register base addresses |
| 38 | * for the usb controller. |
| 39 | */ |
| 40 | struct rcar_xhci { |
| 41 | struct xhci_ctrl ctrl; /* Needs to come first in this struct! */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 42 | struct usb_plat usb_plat; |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 43 | struct xhci_hccr *hcd; |
| 44 | }; |
| 45 | |
| 46 | static int xhci_rcar_download_fw(struct rcar_xhci *ctx, const u32 *fw_data, |
| 47 | const size_t fw_array_size) |
| 48 | { |
| 49 | void __iomem *regs = (void __iomem *)ctx->hcd; |
| 50 | int i, ret; |
| 51 | |
| 52 | /* Download R-Car USB3.0 firmware */ |
| 53 | setbits_le32(regs + RCAR_USB3_DL_CTRL, RCAR_USB3_DL_CTRL_ENABLE); |
| 54 | |
| 55 | for (i = 0; i < fw_array_size; i++) { |
| 56 | writel(fw_data[i], regs + RCAR_USB3_FW_DATA0); |
| 57 | setbits_le32(regs + RCAR_USB3_DL_CTRL, |
| 58 | RCAR_USB3_DL_CTRL_FW_SET_DATA0); |
| 59 | |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 60 | ret = wait_for_bit_le32(regs + RCAR_USB3_DL_CTRL, |
| 61 | RCAR_USB3_DL_CTRL_FW_SET_DATA0, false, |
| 62 | 10, false); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 63 | if (ret) |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | clrbits_le32(regs + RCAR_USB3_DL_CTRL, RCAR_USB3_DL_CTRL_ENABLE); |
| 68 | |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 69 | ret = wait_for_bit_le32(regs + RCAR_USB3_DL_CTRL, |
| 70 | RCAR_USB3_DL_CTRL_FW_SUCCESS, true, |
| 71 | 10, false); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int xhci_rcar_probe(struct udevice *dev) |
| 77 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 78 | struct rcar_xhci_plat *plat = dev_get_plat(dev); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 79 | struct rcar_xhci *ctx = dev_get_priv(dev); |
| 80 | struct xhci_hcor *hcor; |
| 81 | int len, ret; |
| 82 | |
| 83 | ret = clk_get_by_index(dev, 0, &plat->clk); |
| 84 | if (ret < 0) { |
| 85 | dev_err(dev, "Failed to get USB3 clock\n"); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | ret = clk_enable(&plat->clk); |
| 90 | if (ret) { |
| 91 | dev_err(dev, "Failed to enable USB3 clock\n"); |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 92 | return ret; |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | ctx->hcd = (struct xhci_hccr *)plat->hcd_base; |
| 96 | len = HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase)); |
| 97 | hcor = (struct xhci_hcor *)((uintptr_t)ctx->hcd + len); |
| 98 | |
| 99 | ret = xhci_rcar_download_fw(ctx, firmware_r8a779x_usb3_v3, |
| 100 | ARRAY_SIZE(firmware_r8a779x_usb3_v3)); |
| 101 | if (ret) { |
| 102 | dev_err(dev, "Failed to download firmware\n"); |
| 103 | goto err_fw; |
| 104 | } |
| 105 | |
| 106 | ret = xhci_register(dev, ctx->hcd, hcor); |
| 107 | if (ret) { |
| 108 | dev_err(dev, "Failed to register xHCI\n"); |
| 109 | goto err_fw; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | |
| 114 | err_fw: |
| 115 | clk_disable(&plat->clk); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | static int xhci_rcar_deregister(struct udevice *dev) |
| 120 | { |
Matthias Blankertz | a06ad95 | 2018-05-22 15:24:48 +0200 | [diff] [blame] | 121 | int ret; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 122 | struct rcar_xhci_plat *plat = dev_get_plat(dev); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 123 | |
Matthias Blankertz | a06ad95 | 2018-05-22 15:24:48 +0200 | [diff] [blame] | 124 | ret = xhci_deregister(dev); |
| 125 | |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 126 | clk_disable(&plat->clk); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 127 | |
Matthias Blankertz | a06ad95 | 2018-05-22 15:24:48 +0200 | [diff] [blame] | 128 | return ret; |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 131 | static int xhci_rcar_of_to_plat(struct udevice *dev) |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 132 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 133 | struct rcar_xhci_plat *plat = dev_get_plat(dev); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 134 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 135 | plat->hcd_base = dev_read_addr(dev); |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 136 | if (plat->hcd_base == FDT_ADDR_T_NONE) { |
| 137 | debug("Can't get the XHCI register base address\n"); |
| 138 | return -ENXIO; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static const struct udevice_id xhci_rcar_ids[] = { |
Lad Prabhakar | 7f2d86d | 2020-09-21 18:24:09 +0100 | [diff] [blame] | 145 | { .compatible = "renesas,rcar-gen3-xhci" }, |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 146 | { .compatible = "renesas,xhci-r8a7795" }, |
| 147 | { .compatible = "renesas,xhci-r8a7796" }, |
Marek Vasut | f7da2c7 | 2018-02-26 10:35:15 +0100 | [diff] [blame] | 148 | { .compatible = "renesas,xhci-r8a77965" }, |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 149 | { } |
| 150 | }; |
| 151 | |
| 152 | U_BOOT_DRIVER(usb_xhci) = { |
| 153 | .name = "xhci_rcar", |
| 154 | .id = UCLASS_USB, |
| 155 | .probe = xhci_rcar_probe, |
| 156 | .remove = xhci_rcar_deregister, |
| 157 | .ops = &xhci_usb_ops, |
| 158 | .of_match = xhci_rcar_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 159 | .of_to_plat = xhci_rcar_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 160 | .plat_auto = sizeof(struct rcar_xhci_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 161 | .priv_auto = sizeof(struct rcar_xhci), |
Marek Vasut | 2425727 | 2017-10-15 15:01:29 +0200 | [diff] [blame] | 162 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 163 | }; |