summaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/ehci-bcmbca.c
blob: 542cd41ad0e6cc28a18caa97bc178db8170a7a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2019 Broadcom Ltd.
 */

#include <common.h>
#include <dm/ofnode.h>
#include <asm/io.h>
#include <dm.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include "ehci.h"
#include <dm/device-internal.h>

#if defined(CONFIG_BCMBCA_PMC)
#include "pmc_drv.h"
#endif

#define MDIO_USB2   0

/* USB Host contorl regs */
typedef struct usb_ctrl{
	uint32_t setup;
#define USBH_IPP                (1<<5)
#define USBH_IOC                (1<<4)
#define USBH_STRAP_IPP_SEL      (1<<25)
#define USB2_OC_DISABLE_PORT0   (1<<28)
#define USB2_OC_DISABLE_PORT1   (1<<29)
#define USB3_OC_DISABLE_PORT0   (1<<30)
#define USB3_OC_DISABLE_PORT1   (1<<31)
	uint32_t pll_ctl;
	uint32_t fladj_value;
	uint32_t bridge_ctl;
#define USB_BRCTL_OHCI_MEM_REQ_DIS (1<<16)
	uint32_t spare1;
	uint32_t mdio;
	uint32_t mdio2;
	uint32_t test_port_control;
	uint32_t usb_simctl;
#define USBH_OHCI_MEM_REQ_DIS   (1<<1)
	uint32_t usb_testctl;
	uint32_t usb_testmon;
	uint32_t utmi_ctl_1;
	uint32_t utmi_ctl_2;
	uint32_t usb_pm;
#define XHC_SOFT_RESETB         (1<<22)
#define USB_PWRDWN              (1<<31)
	uint32_t usb_pm_status;
	uint32_t spare3;
	uint32_t pll_ldo_ctl;
	uint32_t pll_ldo_pllbias;
	uint32_t pll_afe_bg_cntl;
	uint32_t afe_usbio_tst;
	uint32_t pll_ndiv_frac;
	uint32_t tp_diag;
	uint32_t ahb_capture_fifo;
	uint32_t spare4;
	uint32_t usb30_ctl1;
#define PHY3_PLL_SEQ_START      (1<<4)
	uint32_t usb30_ctl2;
	uint32_t usb30_ctl3;
	uint32_t usb30_ctl4;
	uint32_t usb30_pctl;
	uint32_t usb30_ctl5;
	uint32_t spare5;
	uint32_t spare6;
	uint32_t spare7;
	uint32_t unsused1[3];
	uint32_t usb_device_ctl1;
	uint32_t usb_device_ctl2;
	uint32_t unsused2[22];
	uint32_t usb20_id;
	uint32_t usb30_id;
	uint32_t bdc_coreid;
	uint32_t usb_revid;
} usb_ctrl;

struct bcmbca_usb_ctrl {
	volatile struct usb_ctrl *regp;
};

struct bcmbca_ehci {
	struct ehci_ctrl ctrl;
};

static void usb_mdio_write(volatile uint32_t *mdio, uint32_t reg, uint32_t val, int mode)
{
	uint32_t data;
	data = (reg << 16) | val | mode;
	*mdio = data;
	data |= (1 << 25);
	*mdio = data;
	mdelay(1);
	data &= ~(1 << 25);
	*mdio = data;
}

static void usb2_eye_fix(volatile struct usb_ctrl *regp)
{
	/* Updating USB 2.0 PHY registers */
	usb_mdio_write((void *)&regp->mdio, 0x1f, 0x80a0, MDIO_USB2);
	usb_mdio_write((void *)&regp->mdio, 0x0a, 0xc6a0, MDIO_USB2);
}

#if defined (CONFIG_BCM63138)
static void bcm63138B0_manual_usb_ldo_start(volatile struct usb_ctrl *regp)
{
	regp->pll_ctl &= ~(1 << 30); /*pll_resetb=0*/
	regp->utmi_ctl_1 = 0; 
	regp->pll_ldo_ctl = 4; /*ldo_ctl=core_rdy */
	regp->pll_ctl |= ( 1 << 31); /*pll_iddq=1*/
	mdelay(10);
	regp->pll_ctl &= ~( 1 << 31); /*pll_iddq=0*/
	regp->pll_ldo_ctl |= 1; /*ldo_ctl.AFE_LDO_PWRDWNB=1*/
	regp->pll_ldo_ctl |= 2; /*ldo_ctl.AFE_BG_PWRDWNB=1*/
	mdelay(1);
	regp->utmi_ctl_1 = 0x00020002;/* utmi_resetb &ref_clk_sel=0; */ 
	regp->pll_ctl |= ( 1 << 30); /*pll_resetb=1*/
	mdelay(10);
}    
#endif

static int ehci_usb_probe (struct udevice *dev)
{
	struct ehci_hccr *hccr;
	struct ehci_hcor *hcor;
	int ret;
	struct resource res;
	struct udevice *ctrl_dev;

	uclass_get_device_by_driver(UCLASS_NOP, DM_GET_DRIVER(ctrl_bcmbca_drv), &ctrl_dev);

	ret = dev_read_resource_byname (dev, "usb-ehci", &res);
	if (ret)
	{
		dev_err(dev, "can't get usb-ehci register for usb (ret=%d)\n", ret);
		return ret;
	}

	hccr = devm_ioremap (dev, res.start, resource_size(&res));
	hcor = (struct ehci_hcor *)((uint64_t)hccr +
			HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
	debug("dev %p usb %d hccr %p hcor %p\n", dev, dev->seq, hccr, hcor);

	ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);

	return 0;
}

static int ehci_usb_remove(struct udevice *dev)
{
	int ret;
	struct udevice *next_devp = dev;
	struct udevice *ctrl_dev;

	debug ("remove bcmbca ehci dev %p seq %d\n", dev, dev->seq);
	ret = ehci_deregister(dev);

	if (ret)
	{
		printf("failed to deregister bcmbca ehci seq %d\n", dev->seq);
		return ret;
	}
	ret = uclass_find_next_device (&next_devp);
	if (!ret && !next_devp) {
		uclass_get_device_by_driver(UCLASS_NOP, DM_GET_DRIVER(ctrl_bcmbca_drv), &ctrl_dev);
		if (ctrl_dev)
			device_remove(ctrl_dev, DM_REMOVE_NORMAL);
	}

	return 0;
}


static const struct udevice_id ehci_usb_ids[] = {
	{ .compatible = "brcm,bcmbca-ehci" },
	{ }
};

U_BOOT_DRIVER(ehci_bcmbca) = {
	.name	= "ehci-bcmbca",
	.id	= UCLASS_USB,
	.of_match = ehci_usb_ids,
	.probe = ehci_usb_probe,
	.remove = ehci_usb_remove,
	.ops	= &ehci_usb_ops,
	.priv_auto_alloc_size = sizeof(struct bcmbca_ehci),
	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
};

/* definition of the dummy driver for usb ctrl */
static int bcmbca_usb_ctrl_probe (struct udevice *dev)
{
	int ret;
	uint32_t val;
	struct resource res;
	volatile struct usb_ctrl *regp;
	struct bcmbca_usb_ctrl *priv = dev_get_priv (dev);

	debug("bcmbca_usb_ctrl_probe called....\n");

	//usb-ctrl drive probe function is expected to be called once

#if defined(CONFIG_BCMBCA_PMC)
	// Power On USB, EHCI with index 0 has to be the first USB device 
#if defined(CONFIG_BCM47622) || defined(CONFIG_BCM6756) 
	if (PowerOnDevice(PMB_ADDR_USB31_20))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Up USB Host\n");
		return -1;
	}
#elif defined(CONFIG_BCM6846) || defined(CONFIG_BCM6878)
	if (PowerOnDevice(PMB_ADDR_USB20_2X))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Up USB Host\n");
		return -1;
	}
#else
	if (PowerOnDevice(PMB_ADDR_USB30_2X))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Up USB Host\n");
		return -1;
	}
#endif
	mdelay(1);
#endif
	priv->regp = NULL;
	ret = dev_read_resource_byname (dev, "usb-ctrl", &res);
	if (ret)
	{
		dev_err(dev, "can't get usb-ctrl register (ret=%d)\n", ret);
		return ret;
	}
	regp = devm_ioremap (dev, res.start, resource_size(&res));
	priv->regp = regp;
	debug("usb-ctrl reg %p\n", priv->regp);

	/* adjust over current & port power polarity */
	regp->setup |= (USBH_IOC);
	if (dev_read_bool(dev, "pwrflt-bias-pull-up"))
		regp->setup &= ~(USBH_IOC);

	/*overide strap for IPP*/
	val = regp->setup;
	val &= ~(USBH_STRAP_IPP_SEL);
	val |= (USBH_IPP);
	regp->setup = val;

	if (dev_read_bool(dev, "pwron-bias-pull-up"))
		regp->setup &= ~(USBH_IPP);

#if defined (CONFIG_BCM63138)
	mdelay(300);
	bcm63138B0_manual_usb_ldo_start(regp);
#endif
	/*enable USB PHYs*/
	mdelay(1);
	regp->usb_pm &= ~(USB_PWRDWN);
	mdelay(300);
	regp->usb_pm &= ~XHC_SOFT_RESETB;
	/*adjust the default AFE settings for better eye diagrams */
	usb2_eye_fix(regp);

	/*initialize EHCI & OHCI settings*/
	/* no swap for data & desciptors */    
	regp->bridge_ctl &= ~(0xf); /*clear lower 4 bits */

	/* reset host controllers for possible fake overcurrent indications */ 
	val = regp->usb_pm;
	regp->usb_pm = 0;
	regp->usb_pm = val;
	mdelay(1);

	return 0;
}

static int bcmbca_usb_ctrl_remove(struct udevice *dev)
{
	debug("bcmbca_usb_ctrl_remove called\n");
#if defined(CONFIG_BCMBCA_PMC)
#if defined(CONFIG_BCM47622) || defined(CONFIG_BCM6756) 
	if (PowerOffDevice(PMB_ADDR_USB31_20, 0))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Down USB Host\n");
		return -1;
	}
#elif defined(CONFIG_BCM6846) || defined(CONFIG_BCM6878)
	if (PowerOffDevice(PMB_ADDR_USB20_2X, 0))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Down USB Host\n");
		return -1;
	}
#else
	if (PowerOffDevice(PMB_ADDR_USB30_2X, 0))  // replace with pmc_usb call later on
	{
		printf("Failed to Power Down USB Host\n");
		return -1;
	}
#endif
#endif

	return 0;
}

static const struct udevice_id bcmbca_usb_ctrl_ids[] = {
	{ .compatible = "brcm,bcmbca-usb-ctrl" },
	{ }
};

U_BOOT_DRIVER(ctrl_bcmbca_drv) = {
	.name = "bcmbca-usbctrl",
	.id = UCLASS_NOP,
	.of_match = bcmbca_usb_ctrl_ids,
	.probe = bcmbca_usb_ctrl_probe,
	.remove = bcmbca_usb_ctrl_remove,
	.priv_auto_alloc_size = sizeof(struct bcmbca_usb_ctrl),
};