blob: 6093c8fb0b68629c79ae176b196d596552f08e76 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +05302/*
3 * (C) Copyright 2009
4 * Marvell Semiconductor <www.marvell.com>
5 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +05306 */
7
8#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +053011#include <asm/io.h>
12#include <usb.h>
Simon Glassc05ed002020-05-10 11:40:11 -060013#include <linux/delay.h>
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +053014#include "ehci.h"
Stefan Roesefe11ae22015-06-29 14:58:15 +020015#include <linux/mbus.h>
Lei Wena7efd712011-10-18 20:11:42 +053016#include <asm/arch/cpu.h>
Stefan Roesecd482252015-09-01 11:39:44 +020017#include <dm.h>
Albert ARIBAUD805ad7e2012-01-15 22:08:40 +000018
Trevor Woernerbb0fb4c2020-05-06 08:02:40 -040019#if defined(CONFIG_ARCH_KIRKWOOD)
Stefan Roese3dc23f72014-10-22 12:13:06 +020020#include <asm/arch/soc.h>
Trevor Woernerb16a3312020-05-06 08:02:38 -040021#elif defined(CONFIG_ARCH_ORION5X)
Albert ARIBAUD805ad7e2012-01-15 22:08:40 +000022#include <asm/arch/orion5x.h>
23#endif
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +053024
Albert ARIBAUD74d34422012-01-15 22:08:39 +000025DECLARE_GLOBAL_DATA_PTR;
26
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +053027#define USB_WINDOW_CTRL(i) (0x320 + ((i) << 4))
28#define USB_WINDOW_BASE(i) (0x324 + ((i) << 4))
29#define USB_TARGET_DRAM 0x0
30
Stefan Roesec6cfcc92016-07-18 17:24:56 +020031#define USB2_SBUSCFG_OFF 0x90
32
33#define USB_SBUSCFG_BAWR_OFF 0x6
34#define USB_SBUSCFG_BARD_OFF 0x3
35#define USB_SBUSCFG_AHBBRST_OFF 0x0
36
37#define USB_SBUSCFG_BAWR_ALIGN_64B 0x4
38#define USB_SBUSCFG_BARD_ALIGN_64B 0x4
39#define USB_SBUSCFG_AHBBRST_INCR16 0x7
40
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +053041/*
42 * USB 2.0 Bridge Address Decoding registers setup
43 */
Sven Schwermerfd09c202018-11-21 08:43:56 +010044#if CONFIG_IS_ENABLED(DM_USB)
Stefan Roesefe11ae22015-06-29 14:58:15 +020045
Stefan Roesecd482252015-09-01 11:39:44 +020046struct ehci_mvebu_priv {
47 struct ehci_ctrl ehci;
48 fdt_addr_t hcd_base;
49};
Stefan Roesefe11ae22015-06-29 14:58:15 +020050
Chris Packham515fe1e2022-11-05 17:23:57 +130051#define USB_TO_DRAM_TARGET_ID 0x2
52#define USB_TO_DRAM_ATTR_ID 0x0
53#define USB_DRAM_BASE 0x00000000
54#define USB_DRAM_SIZE 0xfff /* don't overrun u-boot source (was 0xffff) */
55
Stefan Roesefe11ae22015-06-29 14:58:15 +020056/*
57 * Once all the older Marvell SoC's (Orion, Kirkwood) are converted
58 * to the common mvebu archticture including the mbus setup, this
59 * will be the only function needed to configure the access windows
60 */
Chris Packham515fe1e2022-11-05 17:23:57 +130061static void usb_brg_adrdec_setup(struct udevice *dev, void *base)
Stefan Roesefe11ae22015-06-29 14:58:15 +020062{
63 const struct mbus_dram_target_info *dram;
64 int i;
65
66 dram = mvebu_mbus_dram_info();
67
68 for (i = 0; i < 4; i++) {
Stefan Roesecd482252015-09-01 11:39:44 +020069 writel(0, base + USB_WINDOW_CTRL(i));
70 writel(0, base + USB_WINDOW_BASE(i));
Stefan Roesefe11ae22015-06-29 14:58:15 +020071 }
72
Chris Packham515fe1e2022-11-05 17:23:57 +130073 if (device_is_compatible(dev, "marvell,ac5-ehci")) {
74 /*
75 * use decoding window to map dram address seen by usb to 0x0
76 */
Stefan Roesefe11ae22015-06-29 14:58:15 +020077
78 /* Write size, attributes and target id to control register */
Chris Packham515fe1e2022-11-05 17:23:57 +130079 writel((USB_DRAM_SIZE << 16) | (USB_TO_DRAM_ATTR_ID << 8) |
80 (USB_TO_DRAM_TARGET_ID << 4) | 1,
81 base + USB_WINDOW_CTRL(0));
Stefan Roesefe11ae22015-06-29 14:58:15 +020082
83 /* Write base address to base register */
Chris Packham515fe1e2022-11-05 17:23:57 +130084 writel(USB_DRAM_BASE, base + USB_WINDOW_BASE(0));
85
86 debug("## AC5 decoding windows, ctrl[%p]=0x%x, base[%p]=0x%x\n",
87 base + USB_WINDOW_CTRL(0), readl(base + USB_WINDOW_CTRL(0)),
88 base + USB_WINDOW_BASE(0), readl(base + USB_WINDOW_BASE(0)));
89 } else {
90 for (i = 0; i < dram->num_cs; i++) {
91 const struct mbus_dram_window *cs = dram->cs + i;
92
93 /* Write size, attributes and target id to control register */
94 writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
95 (dram->mbus_dram_target_id << 4) | 1,
96 base + USB_WINDOW_CTRL(i));
97
98 /* Write base address to base register */
99 writel(cs->base, base + USB_WINDOW_BASE(i));
100 }
Stefan Roesefe11ae22015-06-29 14:58:15 +0200101 }
102}
Stefan Roesecd482252015-09-01 11:39:44 +0200103
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200104static void marvell_ehci_powerup_fixup(struct ehci_ctrl *ctrl,
105 uint32_t *status_reg, uint32_t *reg)
106{
107 struct ehci_mvebu_priv *priv = ctrl->priv;
108
109 /*
110 * Set default value for reg SBUSCFG, which is Control for the AMBA
111 * system bus interface:
112 * BAWR = BARD = 4 : Align rd/wr bursts packets larger than 64 bytes
113 * AHBBRST = 7 : Align AHB burst for packets larger than 64 bytes
114 */
115 writel((USB_SBUSCFG_BAWR_ALIGN_64B << USB_SBUSCFG_BAWR_OFF) |
116 (USB_SBUSCFG_BARD_ALIGN_64B << USB_SBUSCFG_BARD_OFF) |
117 (USB_SBUSCFG_AHBBRST_INCR16 << USB_SBUSCFG_AHBBRST_OFF),
118 priv->hcd_base + USB2_SBUSCFG_OFF);
119
120 mdelay(50);
121}
122
123static struct ehci_ops marvell_ehci_ops = {
124 .powerup_fixup = NULL,
125};
126
Stefan Roesecd482252015-09-01 11:39:44 +0200127static int ehci_mvebu_probe(struct udevice *dev)
128{
129 struct ehci_mvebu_priv *priv = dev_get_priv(dev);
130 struct ehci_hccr *hccr;
131 struct ehci_hcor *hcor;
132
133 /*
134 * Get the base address for EHCI controller from the device node
135 */
Masahiro Yamada25484932020-07-17 14:36:48 +0900136 priv->hcd_base = dev_read_addr(dev);
Stefan Roesecd482252015-09-01 11:39:44 +0200137 if (priv->hcd_base == FDT_ADDR_T_NONE) {
138 debug("Can't get the EHCI register base address\n");
139 return -ENXIO;
140 }
141
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200142 /*
143 * For SoCs without hlock like Armada3700 we need to program the sbuscfg
144 * reg to guarantee AHB master's burst will not overrun or underrun
145 * the FIFO. Otherwise all USB2 write option will fail.
146 * Also, the address decoder doesn't need to get setup with this
147 * SoC, so don't call usb_brg_adrdec_setup().
148 */
Pali Roháraf6d0932022-02-14 11:34:24 +0100149 if (device_is_compatible(dev, "marvell,armada-3700-ehci"))
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200150 marvell_ehci_ops.powerup_fixup = marvell_ehci_powerup_fixup;
151 else
Chris Packham515fe1e2022-11-05 17:23:57 +1300152 usb_brg_adrdec_setup(dev, (void *)priv->hcd_base);
Stefan Roesecd482252015-09-01 11:39:44 +0200153
154 hccr = (struct ehci_hccr *)(priv->hcd_base + 0x100);
155 hcor = (struct ehci_hcor *)
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200156 ((uintptr_t)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Stefan Roesecd482252015-09-01 11:39:44 +0200157
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200158 debug("ehci-marvell: init hccr %lx and hcor %lx hc_length %ld\n",
159 (uintptr_t)hccr, (uintptr_t)hcor,
160 (uintptr_t)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
Stefan Roesecd482252015-09-01 11:39:44 +0200161
Chris Packham515fe1e2022-11-05 17:23:57 +1300162#define PHY_CALIB_OFFSET 0x808
163 /*
164 * Trigger calibration during each usb start/reset:
165 * BIT 13 to 0, and then to 1
166 */
167 if (device_is_compatible(dev, "marvell,ac5-ehci")) {
168 void *phy_calib_reg = (void *)(priv->hcd_base + PHY_CALIB_OFFSET);
169 u32 val = readl(phy_calib_reg) & (~BIT(13));
170
171 writel(val, phy_calib_reg);
172 writel(val | BIT(13), phy_calib_reg);
173 }
174
Stefan Roesec6cfcc92016-07-18 17:24:56 +0200175 return ehci_register(dev, hccr, hcor, &marvell_ehci_ops, 0,
176 USB_INIT_HOST);
Stefan Roesecd482252015-09-01 11:39:44 +0200177}
178
Stefan Roesecd482252015-09-01 11:39:44 +0200179static const struct udevice_id ehci_usb_ids[] = {
180 { .compatible = "marvell,orion-ehci", },
Pali Roháraf6d0932022-02-14 11:34:24 +0100181 { .compatible = "marvell,armada-3700-ehci", },
Chris Packham515fe1e2022-11-05 17:23:57 +1300182 { .compatible = "marvell,ac5-ehci", },
Stefan Roesecd482252015-09-01 11:39:44 +0200183 { }
184};
185
186U_BOOT_DRIVER(ehci_mvebu) = {
187 .name = "ehci_mvebu",
188 .id = UCLASS_USB,
189 .of_match = ehci_usb_ids,
190 .probe = ehci_mvebu_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900191 .remove = ehci_deregister,
Stefan Roesecd482252015-09-01 11:39:44 +0200192 .ops = &ehci_usb_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700193 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700194 .priv_auto = sizeof(struct ehci_mvebu_priv),
Stefan Roesecd482252015-09-01 11:39:44 +0200195 .flags = DM_FLAG_ALLOC_PRIV_DMA,
196};
197
Stefan Roesefe11ae22015-06-29 14:58:15 +0200198#else
Anton Schubert8a333712015-07-23 15:02:09 +0200199#define MVUSB_BASE(port) MVUSB0_BASE
200
201static void usb_brg_adrdec_setup(int index)
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530202{
203 int i;
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000204 u32 size, base, attrib;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530205
206 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
207
208 /* Enable DRAM bank */
209 switch (i) {
210 case 0:
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000211 attrib = MVUSB0_CPU_ATTR_DRAM_CS0;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530212 break;
213 case 1:
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000214 attrib = MVUSB0_CPU_ATTR_DRAM_CS1;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530215 break;
216 case 2:
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000217 attrib = MVUSB0_CPU_ATTR_DRAM_CS2;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530218 break;
219 case 3:
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000220 attrib = MVUSB0_CPU_ATTR_DRAM_CS3;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530221 break;
222 default:
223 /* invalide bank, disable access */
224 attrib = 0;
225 break;
226 }
227
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000228 size = gd->bd->bi_dram[i].size;
229 base = gd->bd->bi_dram[i].start;
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530230 if ((size) && (attrib))
Stefan Roese82b91432015-07-22 10:01:30 +0200231 writel(MVCPU_WIN_CTRL_DATA(size, USB_TARGET_DRAM,
232 attrib, MVCPU_WIN_ENABLE),
233 MVUSB0_BASE + USB_WINDOW_CTRL(i));
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530234 else
Stefan Roese82b91432015-07-22 10:01:30 +0200235 writel(MVCPU_WIN_DISABLE,
236 MVUSB0_BASE + USB_WINDOW_CTRL(i));
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530237
Stefan Roese82b91432015-07-22 10:01:30 +0200238 writel(base, MVUSB0_BASE + USB_WINDOW_BASE(i));
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530239 }
240}
241
242/*
243 * Create the appropriate control structures to manage
244 * a new EHCI host controller.
245 */
Troy Kisky127efc42013-10-10 15:27:57 -0700246int ehci_hcd_init(int index, enum usb_init_type init,
247 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530248{
Anton Schubert8a333712015-07-23 15:02:09 +0200249 usb_brg_adrdec_setup(index);
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530250
Anton Schubert8a333712015-07-23 15:02:09 +0200251 *hccr = (struct ehci_hccr *)(MVUSB_BASE(index) + 0x100);
Lucas Stach676ae062012-09-26 00:14:35 +0200252 *hcor = (struct ehci_hcor *)((uint32_t) *hccr
253 + HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530254
Albert ARIBAUD74d34422012-01-15 22:08:39 +0000255 debug("ehci-marvell: init hccr %x and hcor %x hc_length %d\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200256 (uint32_t)*hccr, (uint32_t)*hcor,
257 (uint32_t)HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530258
259 return 0;
260}
261
262/*
263 * Destroy the appropriate control structures corresponding
264 * the the EHCI host controller.
265 */
Lucas Stach676ae062012-09-26 00:14:35 +0200266int ehci_hcd_stop(int index)
Prafulla Wadaskar1d8937a2009-06-29 20:56:43 +0530267{
268 return 0;
269}
Stefan Roesecd482252015-09-01 11:39:44 +0200270
Sven Schwermerfd09c202018-11-21 08:43:56 +0100271#endif /* CONFIG_IS_ENABLED(DM_USB) */