blob: d86584b847d48bd17b848ddba168c81116ae5b43 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasute1cc60c2017-10-15 15:01:29 +02002/*
3 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
4 *
5 * Renesas RCar USB HOST xHCI Controller
Marek Vasute1cc60c2017-10-15 15:01:29 +02006 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <fdtdec.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Marek Vasute1cc60c2017-10-15 15:01:29 +020013#include <usb.h>
14#include <wait_bit.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Marek Vasute1cc60c2017-10-15 15:01:29 +020016
Jean-Jacques Hiblot1708a122019-09-11 11:33:46 +020017#include <usb/xhci.h>
Marek Vasute1cc60c2017-10-15 15:01:29 +020018#include "xhci-rcar-r8a779x_usb3_v3.h"
19
20/* Register Offset */
21#define RCAR_USB3_DL_CTRL 0x250 /* FW Download Control & Status */
22#define RCAR_USB3_FW_DATA0 0x258 /* FW Data0 */
23
24/* Register Settings */
25/* FW Download Control & Status */
26#define RCAR_USB3_DL_CTRL_ENABLE BIT(0)
27#define RCAR_USB3_DL_CTRL_FW_SUCCESS BIT(4)
28#define RCAR_USB3_DL_CTRL_FW_SET_DATA0 BIT(8)
29
30struct rcar_xhci_platdata {
31 fdt_addr_t hcd_base;
32 struct clk clk;
33};
34
35/**
36 * Contains pointers to register base addresses
37 * for the usb controller.
38 */
39struct rcar_xhci {
40 struct xhci_ctrl ctrl; /* Needs to come first in this struct! */
41 struct usb_platdata usb_plat;
42 struct xhci_hccr *hcd;
43};
44
45static int xhci_rcar_download_fw(struct rcar_xhci *ctx, const u32 *fw_data,
46 const size_t fw_array_size)
47{
48 void __iomem *regs = (void __iomem *)ctx->hcd;
49 int i, ret;
50
51 /* Download R-Car USB3.0 firmware */
52 setbits_le32(regs + RCAR_USB3_DL_CTRL, RCAR_USB3_DL_CTRL_ENABLE);
53
54 for (i = 0; i < fw_array_size; i++) {
55 writel(fw_data[i], regs + RCAR_USB3_FW_DATA0);
56 setbits_le32(regs + RCAR_USB3_DL_CTRL,
57 RCAR_USB3_DL_CTRL_FW_SET_DATA0);
58
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +010059 ret = wait_for_bit_le32(regs + RCAR_USB3_DL_CTRL,
60 RCAR_USB3_DL_CTRL_FW_SET_DATA0, false,
61 10, false);
Marek Vasute1cc60c2017-10-15 15:01:29 +020062 if (ret)
63 break;
64 }
65
66 clrbits_le32(regs + RCAR_USB3_DL_CTRL, RCAR_USB3_DL_CTRL_ENABLE);
67
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +010068 ret = wait_for_bit_le32(regs + RCAR_USB3_DL_CTRL,
69 RCAR_USB3_DL_CTRL_FW_SUCCESS, true,
70 10, false);
Marek Vasute1cc60c2017-10-15 15:01:29 +020071
72 return ret;
73}
74
75static int xhci_rcar_probe(struct udevice *dev)
76{
77 struct rcar_xhci_platdata *plat = dev_get_platdata(dev);
78 struct rcar_xhci *ctx = dev_get_priv(dev);
79 struct xhci_hcor *hcor;
80 int len, ret;
81
82 ret = clk_get_by_index(dev, 0, &plat->clk);
83 if (ret < 0) {
84 dev_err(dev, "Failed to get USB3 clock\n");
85 return ret;
86 }
87
88 ret = clk_enable(&plat->clk);
89 if (ret) {
90 dev_err(dev, "Failed to enable USB3 clock\n");
91 goto err_clk;
92 }
93
94 ctx->hcd = (struct xhci_hccr *)plat->hcd_base;
95 len = HC_LENGTH(xhci_readl(&ctx->hcd->cr_capbase));
96 hcor = (struct xhci_hcor *)((uintptr_t)ctx->hcd + len);
97
98 ret = xhci_rcar_download_fw(ctx, firmware_r8a779x_usb3_v3,
99 ARRAY_SIZE(firmware_r8a779x_usb3_v3));
100 if (ret) {
101 dev_err(dev, "Failed to download firmware\n");
102 goto err_fw;
103 }
104
105 ret = xhci_register(dev, ctx->hcd, hcor);
106 if (ret) {
107 dev_err(dev, "Failed to register xHCI\n");
108 goto err_fw;
109 }
110
111 return 0;
112
113err_fw:
114 clk_disable(&plat->clk);
115err_clk:
116 clk_free(&plat->clk);
117 return ret;
118}
119
120static int xhci_rcar_deregister(struct udevice *dev)
121{
Matthias Blankertz3f484222018-05-22 15:24:48 +0200122 int ret;
Marek Vasute1cc60c2017-10-15 15:01:29 +0200123 struct rcar_xhci_platdata *plat = dev_get_platdata(dev);
124
Matthias Blankertz3f484222018-05-22 15:24:48 +0200125 ret = xhci_deregister(dev);
126
Marek Vasute1cc60c2017-10-15 15:01:29 +0200127 clk_disable(&plat->clk);
128 clk_free(&plat->clk);
129
Matthias Blankertz3f484222018-05-22 15:24:48 +0200130 return ret;
Marek Vasute1cc60c2017-10-15 15:01:29 +0200131}
132
133static int xhci_rcar_ofdata_to_platdata(struct udevice *dev)
134{
135 struct rcar_xhci_platdata *plat = dev_get_platdata(dev);
136
137 plat->hcd_base = devfdt_get_addr(dev);
138 if (plat->hcd_base == FDT_ADDR_T_NONE) {
139 debug("Can't get the XHCI register base address\n");
140 return -ENXIO;
141 }
142
143 return 0;
144}
145
146static const struct udevice_id xhci_rcar_ids[] = {
147 { .compatible = "renesas,xhci-r8a7795" },
148 { .compatible = "renesas,xhci-r8a7796" },
Marek Vasut4f0533f2018-02-26 10:35:15 +0100149 { .compatible = "renesas,xhci-r8a77965" },
Marek Vasute1cc60c2017-10-15 15:01:29 +0200150 { }
151};
152
153U_BOOT_DRIVER(usb_xhci) = {
154 .name = "xhci_rcar",
155 .id = UCLASS_USB,
156 .probe = xhci_rcar_probe,
157 .remove = xhci_rcar_deregister,
158 .ops = &xhci_usb_ops,
159 .of_match = xhci_rcar_ids,
160 .ofdata_to_platdata = xhci_rcar_ofdata_to_platdata,
161 .platdata_auto_alloc_size = sizeof(struct rcar_xhci_platdata),
162 .priv_auto_alloc_size = sizeof(struct rcar_xhci),
163 .flags = DM_FLAG_ALLOC_PRIV_DMA,
164};