summaryrefslogtreecommitdiff
path: root/drivers/usb/host/dwc3-octeon-glue.c
blob: 975f375e15ade5451acc358fb80ecd4869a3ed59 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// SPDX-License-Identifier: GPL-2.0
/*
 * Octeon family DWC3 specific glue layer
 *
 * Copyright (C) 2020 Stefan Roese <sr@denx.de>
 *
 * The low-level init code is based on the Linux driver octeon-usb.c by
 * David Daney <david.daney@cavium.com>, which is:
 * Copyright (C) 2010-2017 Cavium Networks
 */

#include <dm.h>
#include <errno.h>
#include <usb.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <dm/of_access.h>
#include <linux/bitfield.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/usb/dwc3.h>
#include <linux/usb/otg.h>
#include <mach/octeon-model.h>

DECLARE_GLOBAL_DATA_PTR;

#define CVMX_GPIO_BIT_CFGX(i)	(0x0001070000000900ull + ((i) * 8))
#define CVMX_GPIO_XBIT_CFGX(i)	(0x0001070000000900ull + \
				 ((i) & 31) * 8 - 8 * 16)

#define GPIO_BIT_CFG_TX_OE		BIT_ULL(0)
#define GPIO_BIT_CFG_OUTPUT_SEL		GENMASK_ULL(20, 16)

#define UCTL_CTL_UCTL_RST		BIT_ULL(0)
#define UCTL_CTL_UAHC_RST		BIT_ULL(1)
#define UCTL_CTL_UPHY_RST		BIT_ULL(2)
#define UCTL_CTL_DRD_MODE		BIT_ULL(3)
#define UCTL_CTL_SCLK_EN		BIT_ULL(4)
#define UCTL_CTL_HS_POWER_EN		BIT_ULL(12)
#define UCTL_CTL_SS_POWER_EN		BIT_ULL(14)
#define UCTL_CTL_H_CLKDIV_SEL		GENMASK_ULL(26, 24)
#define UCTL_CTL_H_CLKDIV_RST		BIT_ULL(28)
#define UCTL_CTL_H_CLK_EN		BIT_ULL(30)
#define UCTL_CTL_REF_CLK_FSEL		GENMASK_ULL(37, 32)
#define UCTL_CTL_REF_CLK_DIV2		BIT_ULL(38)
#define UCTL_CTL_REF_SSP_EN		BIT_ULL(39)
#define UCTL_CTL_MPLL_MULTIPLIER	GENMASK_ULL(46, 40)
#define UCTL_CTL_SSC_EN			BIT_ULL(59)
#define UCTL_CTL_REF_CLK_SEL		GENMASK_ULL(61, 60)

#define UCTL_HOST_CFG			0xe0
#define UCTL_HOST_CFG_PPC_ACTIVE_HIGH_EN BIT_ULL(24)
#define UCTL_HOST_CFG_PPC_EN		BIT_ULL(25)

#define UCTL_SHIM_CFG			0xe8
#define UCTL_SHIM_CFG_CSR_ENDIAN_MODE	GENMASK_ULL(1, 0)
#define UCTL_SHIM_CFG_DMA_ENDIAN_MODE	GENMASK_ULL(9, 8)

#define OCTEON_H_CLKDIV_SEL		8
#define OCTEON_MIN_H_CLK_RATE		150000000
#define OCTEON_MAX_H_CLK_RATE		300000000

#define CLOCK_50MHZ			50000000
#define CLOCK_100MHZ			100000000
#define CLOCK_125MHZ			125000000

static u8 clk_div[OCTEON_H_CLKDIV_SEL] = {1, 2, 4, 6, 8, 16, 24, 32};

static int dwc3_octeon_config_power(struct udevice *dev, void __iomem *base)
{
	u64 uctl_host_cfg;
	u64 gpio_bit;
	u32 gpio_pwr[3];
	int gpio, len, power_active_low;
	const struct device_node *node = dev_np(dev);
	int index = ((u64)base >> 24) & 1;
	void __iomem *gpio_bit_cfg;

	if (of_find_property(node, "power", &len)) {
		if (len == 12) {
			dev_read_u32_array(dev, "power", gpio_pwr, 3);
			power_active_low = gpio_pwr[2] & 0x01;
			gpio = gpio_pwr[1];
		} else if (len == 8) {
			dev_read_u32_array(dev, "power", gpio_pwr, 2);
			power_active_low = 0;
			gpio = gpio_pwr[1];
		} else {
			printf("dwc3 controller clock init failure\n");
			return -EINVAL;
		}

		gpio_bit_cfg = ioremap(CVMX_GPIO_BIT_CFGX(gpio), 0);

		if ((OCTEON_IS_MODEL(OCTEON_CN73XX) ||
		     OCTEON_IS_MODEL(OCTEON_CNF75XX)) && gpio <= 31) {
			gpio_bit = ioread64(gpio_bit_cfg);
			gpio_bit |= GPIO_BIT_CFG_TX_OE;
			gpio_bit &= ~GPIO_BIT_CFG_OUTPUT_SEL;
			gpio_bit |= FIELD_PREP(GPIO_BIT_CFG_OUTPUT_SEL,
					       index == 0 ? 0x14 : 0x15);
			iowrite64(gpio_bit, gpio_bit_cfg);
		} else if (gpio <= 15) {
			gpio_bit = ioread64(gpio_bit_cfg);
			gpio_bit |= GPIO_BIT_CFG_TX_OE;
			gpio_bit &= ~GPIO_BIT_CFG_OUTPUT_SEL;
			gpio_bit |= FIELD_PREP(GPIO_BIT_CFG_OUTPUT_SEL,
					       index == 0 ? 0x14 : 0x19);
			iowrite64(gpio_bit, gpio_bit_cfg);
		} else {
			gpio_bit_cfg = ioremap(CVMX_GPIO_XBIT_CFGX(gpio), 0);

			gpio_bit = ioread64(gpio_bit_cfg);
			gpio_bit |= GPIO_BIT_CFG_TX_OE;
			gpio_bit &= ~GPIO_BIT_CFG_OUTPUT_SEL;
			gpio_bit |= FIELD_PREP(GPIO_BIT_CFG_OUTPUT_SEL,
					       index == 0 ? 0x14 : 0x19);
			iowrite64(gpio_bit, gpio_bit_cfg);
		}

		/* Enable XHCI power control and set if active high or low. */
		uctl_host_cfg = ioread64(base + UCTL_HOST_CFG);
		uctl_host_cfg |= UCTL_HOST_CFG_PPC_EN;
		if (power_active_low)
			uctl_host_cfg &= ~UCTL_HOST_CFG_PPC_ACTIVE_HIGH_EN;
		else
			uctl_host_cfg |= UCTL_HOST_CFG_PPC_ACTIVE_HIGH_EN;
		iowrite64(uctl_host_cfg, base + UCTL_HOST_CFG);

		/* Wait for power to stabilize */
		mdelay(10);
	} else {
		/* Disable XHCI power control and set if active high. */
		uctl_host_cfg = ioread64(base + UCTL_HOST_CFG);
		uctl_host_cfg &= ~UCTL_HOST_CFG_PPC_EN;
		uctl_host_cfg &= ~UCTL_HOST_CFG_PPC_ACTIVE_HIGH_EN;
		iowrite64(uctl_host_cfg, base + UCTL_HOST_CFG);
		dev_warn(dev, "dwc3 controller clock init failure.\n");
	}

	return 0;
}

static int dwc3_octeon_clocks_start(struct udevice *dev, void __iomem *base)
{
	u64 uctl_ctl;
	int ref_clk_sel = 2;
	u64 div;
	u32 clock_rate;
	int mpll_mul;
	int i;
	u64 h_clk_rate;
	void __iomem *uctl_ctl_reg = base;
	const char *ss_clock_type;
	const char *hs_clock_type;

	i = dev_read_u32(dev, "refclk-frequency", &clock_rate);
	if (i) {
		printf("No UCTL \"refclk-frequency\"\n");
		return -EINVAL;
	}

	ss_clock_type = dev_read_string(dev, "refclk-type-ss");
	if (!ss_clock_type) {
		printf("No UCTL \"refclk-type-ss\"\n");
		return -EINVAL;
	}

	hs_clock_type = dev_read_string(dev, "refclk-type-hs");
	if (!hs_clock_type) {
		printf("No UCTL \"refclk-type-hs\"\n");
		return -EINVAL;
	}

	if (strcmp("dlmc_ref_clk0", ss_clock_type) == 0) {
		if (strcmp(hs_clock_type, "dlmc_ref_clk0") == 0) {
			ref_clk_sel = 0;
		} else if (strcmp(hs_clock_type, "pll_ref_clk") == 0) {
			ref_clk_sel = 2;
		} else {
			printf("Invalid HS clock type %s, using pll_ref_clk\n",
			       hs_clock_type);
		}
	} else if (strcmp(ss_clock_type, "dlmc_ref_clk1") == 0) {
		if (strcmp(hs_clock_type, "dlmc_ref_clk1") == 0) {
			ref_clk_sel = 1;
		} else if (strcmp(hs_clock_type, "pll_ref_clk") == 0) {
			ref_clk_sel = 3;
		} else {
			printf("Invalid HS clock type %s, using pll_ref_clk\n",
			       hs_clock_type);
			ref_clk_sel = 3;
		}
	} else {
		printf("Invalid SS clock type %s, using dlmc_ref_clk0\n",
		       ss_clock_type);
	}

	if ((ref_clk_sel == 0 || ref_clk_sel == 1) &&
	    clock_rate != CLOCK_100MHZ)
		printf("Invalid UCTL clock rate of %u\n", clock_rate);

	/*
	 * Step 1: Wait for all voltages to be stable...that surely
	 *         happened before this driver is started. SKIP
	 */

	/* Step 2: Select GPIO for overcurrent indication, if desired. SKIP */

	/* Step 3: Assert all resets. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl |= UCTL_CTL_UCTL_RST | UCTL_CTL_UAHC_RST | UCTL_CTL_UPHY_RST;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 4a: Reset the clock dividers. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl |= UCTL_CTL_H_CLKDIV_RST;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 4b: Select controller clock frequency. */
	for (div = ARRAY_SIZE(clk_div) - 1; div >= 0; div--) {
		h_clk_rate = gd->bus_clk / clk_div[div];
		if (h_clk_rate <= OCTEON_MAX_H_CLK_RATE &&
		    h_clk_rate >= OCTEON_MIN_H_CLK_RATE)
			break;
	}
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_H_CLKDIV_SEL;
	uctl_ctl |= FIELD_PREP(UCTL_CTL_H_CLKDIV_SEL, div);
	uctl_ctl |= UCTL_CTL_H_CLK_EN;
	iowrite64(uctl_ctl, uctl_ctl_reg);
	uctl_ctl = ioread64(uctl_ctl_reg);
	if (div != FIELD_GET(UCTL_CTL_H_CLKDIV_SEL, uctl_ctl) ||
	    !(uctl_ctl & UCTL_CTL_H_CLK_EN)) {
		printf("dwc3 controller clock init failure\n");
		return -EINVAL;
	}

	/* Step 4c: Deassert the controller clock divider reset. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_H_CLKDIV_RST;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 5a: Reference clock configuration. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_REF_CLK_SEL;
	uctl_ctl |= FIELD_PREP(UCTL_CTL_REF_CLK_SEL, ref_clk_sel);
	uctl_ctl &= ~UCTL_CTL_REF_CLK_FSEL;
	uctl_ctl |= FIELD_PREP(UCTL_CTL_REF_CLK_FSEL, 0x07);
	uctl_ctl &= ~UCTL_CTL_REF_CLK_DIV2;

	switch (clock_rate) {
	default:
		printf("Invalid ref_clk %u, using %u instead\n", CLOCK_100MHZ,
		       clock_rate);
		fallthrough;
	case CLOCK_100MHZ:
		mpll_mul = 0x19;
		if (ref_clk_sel < 2) {
			uctl_ctl &= ~UCTL_CTL_REF_CLK_FSEL;
			uctl_ctl |= FIELD_PREP(UCTL_CTL_REF_CLK_FSEL, 0x27);
		}
		break;
	case CLOCK_50MHZ:
		mpll_mul = 0x32;
		break;
	case CLOCK_125MHZ:
		mpll_mul = 0x28;
		break;
	}
	uctl_ctl &= ~UCTL_CTL_MPLL_MULTIPLIER;
	uctl_ctl |= FIELD_PREP(UCTL_CTL_MPLL_MULTIPLIER, mpll_mul);

	/* Step 5b: Configure and enable spread-spectrum for SuperSpeed. */
	uctl_ctl |= UCTL_CTL_SSC_EN;

	/* Step 5c: Enable SuperSpeed. */
	uctl_ctl |= UCTL_CTL_REF_SSP_EN;

	/* Step 5d: Configure PHYs. SKIP */

	/* Step 6a & 6b: Power up PHYs. */
	uctl_ctl |= UCTL_CTL_HS_POWER_EN;
	uctl_ctl |= UCTL_CTL_SS_POWER_EN;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 7: Wait 10 controller-clock cycles to take effect. */
	udelay(10);

	/* Step 8a: Deassert UCTL reset signal. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_UCTL_RST;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 8b: Wait 10 controller-clock cycles. */
	udelay(10);

	/* Step 8c: Setup power-power control. */
	if (dwc3_octeon_config_power(dev, base)) {
		printf("Error configuring power\n");
		return -EINVAL;
	}

	/* Step 8d: Deassert UAHC reset signal. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_UAHC_RST;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 8e: Wait 10 controller-clock cycles. */
	udelay(10);

	/* Step 9: Enable conditional coprocessor clock of UCTL. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl |= UCTL_CTL_SCLK_EN;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	/* Step 10: Set for host mode only. */
	uctl_ctl = ioread64(uctl_ctl_reg);
	uctl_ctl &= ~UCTL_CTL_DRD_MODE;
	iowrite64(uctl_ctl, uctl_ctl_reg);

	return 0;
}

static void dwc3_octeon_set_endian_mode(void __iomem *base)
{
	u64 shim_cfg;

	shim_cfg = ioread64(base + UCTL_SHIM_CFG);
	shim_cfg &= ~UCTL_SHIM_CFG_CSR_ENDIAN_MODE;
	shim_cfg |= FIELD_PREP(UCTL_SHIM_CFG_CSR_ENDIAN_MODE, 1);
	shim_cfg &= ~UCTL_SHIM_CFG_DMA_ENDIAN_MODE;
	shim_cfg |= FIELD_PREP(UCTL_SHIM_CFG_DMA_ENDIAN_MODE, 1);
	iowrite64(shim_cfg, base + UCTL_SHIM_CFG);
}

static void dwc3_octeon_phy_reset(void __iomem *base)
{
	u64 uctl_ctl;

	uctl_ctl = ioread64(base);
	uctl_ctl &= ~UCTL_CTL_UPHY_RST;
	iowrite64(uctl_ctl, base);
}

static int octeon_dwc3_glue_probe(struct udevice *dev)
{
	void __iomem *base;

	base = dev_remap_addr(dev);
	if (IS_ERR(base))
		return PTR_ERR(base);

	dwc3_octeon_clocks_start(dev, base);
	dwc3_octeon_set_endian_mode(base);
	dwc3_octeon_phy_reset(base);

	return 0;
}

static int octeon_dwc3_glue_bind(struct udevice *dev)
{
	ofnode node, dwc3_node;

	/* Find snps,dwc3 node from subnode */
	dwc3_node = ofnode_null();
	ofnode_for_each_subnode(node, dev_ofnode(dev)) {
		if (ofnode_device_is_compatible(node, "snps,dwc3"))
			dwc3_node = node;
	}

	if (!ofnode_valid(dwc3_node)) {
		printf("Can't find dwc3 subnode for %s\n", dev->name);
		return -ENODEV;
	}

	return dm_scan_fdt_dev(dev);
}

static const struct udevice_id octeon_dwc3_glue_ids[] = {
	{ .compatible = "cavium,octeon-7130-usb-uctl" },
	{ }
};

U_BOOT_DRIVER(dwc3_octeon_glue) = {
	.name = "dwc3_octeon_glue",
	.id = UCLASS_NOP,
	.of_match = octeon_dwc3_glue_ids,
	.probe = octeon_dwc3_glue_probe,
	.bind = octeon_dwc3_glue_bind,
	.flags = DM_FLAG_ALLOC_PRIV_DMA,
};