xref: /openbmc/linux/drivers/usb/dwc3/dwc3-octeon.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d9216d3eSLadislav Michl // SPDX-License-Identifier: GPL-2.0
2976f82e8SLadislav Michl /*
3d9216d3eSLadislav Michl  * DWC3 glue for Cavium Octeon III SOCs.
4976f82e8SLadislav Michl  *
5976f82e8SLadislav Michl  * Copyright (C) 2010-2017 Cavium Networks
6d9216d3eSLadislav Michl  * Copyright (C) 2023 RACOM s.r.o.
7976f82e8SLadislav Michl  */
8976f82e8SLadislav Michl 
9976f82e8SLadislav Michl #include <linux/bitfield.h>
10976f82e8SLadislav Michl #include <linux/bits.h>
11976f82e8SLadislav Michl #include <linux/device.h>
12976f82e8SLadislav Michl #include <linux/delay.h>
13976f82e8SLadislav Michl #include <linux/io.h>
14976f82e8SLadislav Michl #include <linux/module.h>
15976f82e8SLadislav Michl #include <linux/mutex.h>
16*ac2224a4SLinus Torvalds #include <linux/of.h>
17976f82e8SLadislav Michl #include <linux/of_platform.h>
18*ac2224a4SLinus Torvalds #include <linux/platform_device.h>
19976f82e8SLadislav Michl 
20976f82e8SLadislav Michl /*
21976f82e8SLadislav Michl  * USB Control Register
22976f82e8SLadislav Michl  */
23976f82e8SLadislav Michl #define USBDRD_UCTL_CTL				0x00
24976f82e8SLadislav Michl /* BIST fast-clear mode select. A BIST run with this bit set
25976f82e8SLadislav Michl  * clears all entries in USBH RAMs to 0x0.
26976f82e8SLadislav Michl  */
2741784066SLadislav Michl # define USBDRD_UCTL_CTL_CLEAR_BIST		BIT_ULL(63)
28976f82e8SLadislav Michl /* 1 = Start BIST and cleared by hardware */
2941784066SLadislav Michl # define USBDRD_UCTL_CTL_START_BIST		BIT_ULL(62)
30976f82e8SLadislav Michl /* Reference clock select for SuperSpeed and HighSpeed PLLs:
31976f82e8SLadislav Michl  *	0x0 = Both PLLs use DLMC_REF_CLK0 for reference clock
32976f82e8SLadislav Michl  *	0x1 = Both PLLs use DLMC_REF_CLK1 for reference clock
33976f82e8SLadislav Michl  *	0x2 = SuperSpeed PLL uses DLMC_REF_CLK0 for reference clock &
34976f82e8SLadislav Michl  *	      HighSpeed PLL uses PLL_REF_CLK for reference clck
35976f82e8SLadislav Michl  *	0x3 = SuperSpeed PLL uses DLMC_REF_CLK1 for reference clock &
36976f82e8SLadislav Michl  *	      HighSpeed PLL uses PLL_REF_CLK for reference clck
37976f82e8SLadislav Michl  */
3841784066SLadislav Michl # define USBDRD_UCTL_CTL_REF_CLK_SEL		GENMASK_ULL(61, 60)
39976f82e8SLadislav Michl /* 1 = Spread-spectrum clock enable, 0 = SS clock disable */
4041784066SLadislav Michl # define USBDRD_UCTL_CTL_SSC_EN			BIT_ULL(59)
41976f82e8SLadislav Michl /* Spread-spectrum clock modulation range:
42976f82e8SLadislav Michl  *	0x0 = -4980 ppm downspread
43976f82e8SLadislav Michl  *	0x1 = -4492 ppm downspread
44976f82e8SLadislav Michl  *	0x2 = -4003 ppm downspread
45976f82e8SLadislav Michl  *	0x3 - 0x7 = Reserved
46976f82e8SLadislav Michl  */
4741784066SLadislav Michl # define USBDRD_UCTL_CTL_SSC_RANGE		GENMASK_ULL(58, 56)
48976f82e8SLadislav Michl /* Enable non-standard oscillator frequencies:
49976f82e8SLadislav Michl  *	[55:53] = modules -1
50976f82e8SLadislav Michl  *	[52:47] = 2's complement push amount, 0 = Feature disabled
51976f82e8SLadislav Michl  */
5241784066SLadislav Michl # define USBDRD_UCTL_CTL_SSC_REF_CLK_SEL	GENMASK_ULL(55, 47)
53976f82e8SLadislav Michl /* Reference clock multiplier for non-standard frequencies:
54976f82e8SLadislav Michl  *	0x19 = 100MHz on DLMC_REF_CLK* if REF_CLK_SEL = 0x0 or 0x1
55976f82e8SLadislav Michl  *	0x28 = 125MHz on DLMC_REF_CLK* if REF_CLK_SEL = 0x0 or 0x1
56976f82e8SLadislav Michl  *	0x32 =  50MHz on DLMC_REF_CLK* if REF_CLK_SEL = 0x0 or 0x1
57976f82e8SLadislav Michl  *	Other Values = Reserved
58976f82e8SLadislav Michl  */
5941784066SLadislav Michl # define USBDRD_UCTL_CTL_MPLL_MULTIPLIER	GENMASK_ULL(46, 40)
60976f82e8SLadislav Michl /* Enable reference clock to prescaler for SuperSpeed functionality.
61976f82e8SLadislav Michl  * Should always be set to "1"
62976f82e8SLadislav Michl  */
6341784066SLadislav Michl # define USBDRD_UCTL_CTL_REF_SSP_EN		BIT_ULL(39)
64976f82e8SLadislav Michl /* Divide the reference clock by 2 before entering the
65976f82e8SLadislav Michl  * REF_CLK_FSEL divider:
66976f82e8SLadislav Michl  *	If REF_CLK_SEL = 0x0 or 0x1, then only 0x0 is legal
67976f82e8SLadislav Michl  *	If REF_CLK_SEL = 0x2 or 0x3, then:
68976f82e8SLadislav Michl  *		0x1 = DLMC_REF_CLK* is 125MHz
69976f82e8SLadislav Michl  *		0x0 = DLMC_REF_CLK* is another supported frequency
70976f82e8SLadislav Michl  */
7141784066SLadislav Michl # define USBDRD_UCTL_CTL_REF_CLK_DIV2		BIT_ULL(38)
72976f82e8SLadislav Michl /* Select reference clock freqnuency for both PLL blocks:
73976f82e8SLadislav Michl  *	0x27 = REF_CLK_SEL is 0x0 or 0x1
74976f82e8SLadislav Michl  *	0x07 = REF_CLK_SEL is 0x2 or 0x3
75976f82e8SLadislav Michl  */
7641784066SLadislav Michl # define USBDRD_UCTL_CTL_REF_CLK_FSEL		GENMASK_ULL(37, 32)
77976f82e8SLadislav Michl /* Controller clock enable. */
7841784066SLadislav Michl # define USBDRD_UCTL_CTL_H_CLK_EN		BIT_ULL(30)
79976f82e8SLadislav Michl /* Select bypass input to controller clock divider:
80976f82e8SLadislav Michl  *	0x0 = Use divided coprocessor clock from H_CLKDIV
81976f82e8SLadislav Michl  *	0x1 = Use clock from GPIO pins
82976f82e8SLadislav Michl  */
8341784066SLadislav Michl # define USBDRD_UCTL_CTL_H_CLK_BYP_SEL		BIT_ULL(29)
84976f82e8SLadislav Michl /* Reset controller clock divider. */
8541784066SLadislav Michl # define USBDRD_UCTL_CTL_H_CLKDIV_RST		BIT_ULL(28)
86976f82e8SLadislav Michl /* Clock divider select:
87976f82e8SLadislav Michl  *	0x0 = divide by 1
88976f82e8SLadislav Michl  *	0x1 = divide by 2
89976f82e8SLadislav Michl  *	0x2 = divide by 4
90976f82e8SLadislav Michl  *	0x3 = divide by 6
91976f82e8SLadislav Michl  *	0x4 = divide by 8
92976f82e8SLadislav Michl  *	0x5 = divide by 16
93976f82e8SLadislav Michl  *	0x6 = divide by 24
94976f82e8SLadislav Michl  *	0x7 = divide by 32
95976f82e8SLadislav Michl  */
9641784066SLadislav Michl # define USBDRD_UCTL_CTL_H_CLKDIV_SEL		GENMASK_ULL(26, 24)
97976f82e8SLadislav Michl /* USB3 port permanently attached: 0x0 = No, 0x1 = Yes */
9841784066SLadislav Michl # define USBDRD_UCTL_CTL_USB3_PORT_PERM_ATTACH	BIT_ULL(21)
99976f82e8SLadislav Michl /* USB2 port permanently attached: 0x0 = No, 0x1 = Yes */
10041784066SLadislav Michl # define USBDRD_UCTL_CTL_USB2_PORT_PERM_ATTACH	BIT_ULL(20)
101976f82e8SLadislav Michl /* Disable SuperSpeed PHY: 0x0 = No, 0x1 = Yes */
10241784066SLadislav Michl # define USBDRD_UCTL_CTL_USB3_PORT_DISABLE	BIT_ULL(18)
103976f82e8SLadislav Michl /* Disable HighSpeed PHY: 0x0 = No, 0x1 = Yes */
10441784066SLadislav Michl # define USBDRD_UCTL_CTL_USB2_PORT_DISABLE	BIT_ULL(16)
105976f82e8SLadislav Michl /* Enable PHY SuperSpeed block power: 0x0 = No, 0x1 = Yes */
10641784066SLadislav Michl # define USBDRD_UCTL_CTL_SS_POWER_EN		BIT_ULL(14)
107976f82e8SLadislav Michl /* Enable PHY HighSpeed block power: 0x0 = No, 0x1 = Yes */
10841784066SLadislav Michl # define USBDRD_UCTL_CTL_HS_POWER_EN		BIT_ULL(12)
109976f82e8SLadislav Michl /* Enable USB UCTL interface clock: 0xx = No, 0x1 = Yes */
11041784066SLadislav Michl # define USBDRD_UCTL_CTL_CSCLK_EN		BIT_ULL(4)
111976f82e8SLadislav Michl /* Controller mode: 0x0 = Host, 0x1 = Device */
11241784066SLadislav Michl # define USBDRD_UCTL_CTL_DRD_MODE		BIT_ULL(3)
113976f82e8SLadislav Michl /* PHY reset */
11441784066SLadislav Michl # define USBDRD_UCTL_CTL_UPHY_RST		BIT_ULL(2)
115976f82e8SLadislav Michl /* Software reset UAHC */
11641784066SLadislav Michl # define USBDRD_UCTL_CTL_UAHC_RST		BIT_ULL(1)
117976f82e8SLadislav Michl /* Software resets UCTL */
11841784066SLadislav Michl # define USBDRD_UCTL_CTL_UCTL_RST		BIT_ULL(0)
119976f82e8SLadislav Michl 
120976f82e8SLadislav Michl #define USBDRD_UCTL_BIST_STATUS			0x08
121976f82e8SLadislav Michl #define USBDRD_UCTL_SPARE0			0x10
122976f82e8SLadislav Michl #define USBDRD_UCTL_INTSTAT			0x30
123976f82e8SLadislav Michl #define USBDRD_UCTL_PORT_CFG_HS(port)		(0x40 + (0x20 * port))
124976f82e8SLadislav Michl #define USBDRD_UCTL_PORT_CFG_SS(port)		(0x48 + (0x20 * port))
125976f82e8SLadislav Michl #define USBDRD_UCTL_PORT_CR_DBG_CFG(port)	(0x50 + (0x20 * port))
126976f82e8SLadislav Michl #define USBDRD_UCTL_PORT_CR_DBG_STATUS(port)	(0x58 + (0x20 * port))
127976f82e8SLadislav Michl 
128976f82e8SLadislav Michl /*
129976f82e8SLadislav Michl  * UCTL Configuration Register
130976f82e8SLadislav Michl  */
131976f82e8SLadislav Michl #define USBDRD_UCTL_HOST_CFG			0xe0
132976f82e8SLadislav Michl /* Indicates minimum value of all received BELT values */
13341784066SLadislav Michl # define USBDRD_UCTL_HOST_CFG_HOST_CURRENT_BELT	GENMASK_ULL(59, 48)
134976f82e8SLadislav Michl /* HS jitter adjustment */
13541784066SLadislav Michl # define USBDRD_UCTL_HOST_CFG_FLA		GENMASK_ULL(37, 32)
136976f82e8SLadislav Michl /* Bus-master enable: 0x0 = Disabled (stall DMAs), 0x1 = enabled */
13741784066SLadislav Michl # define USBDRD_UCTL_HOST_CFG_BME		BIT_ULL(28)
138976f82e8SLadislav Michl /* Overcurrent protection enable: 0x0 = unavailable, 0x1 = available */
13941784066SLadislav Michl # define USBDRD_UCTL_HOST_OCI_EN		BIT_ULL(27)
140976f82e8SLadislav Michl /* Overcurrent sene selection:
141976f82e8SLadislav Michl  *	0x0 = Overcurrent indication from off-chip is active-low
142976f82e8SLadislav Michl  *	0x1 = Overcurrent indication from off-chip is active-high
143976f82e8SLadislav Michl  */
14441784066SLadislav Michl # define USBDRD_UCTL_HOST_OCI_ACTIVE_HIGH_EN	BIT_ULL(26)
145976f82e8SLadislav Michl /* Port power control enable: 0x0 = unavailable, 0x1 = available */
14641784066SLadislav Michl # define USBDRD_UCTL_HOST_PPC_EN		BIT_ULL(25)
147976f82e8SLadislav Michl /* Port power control sense selection:
148976f82e8SLadislav Michl  *	0x0 = Port power to off-chip is active-low
149976f82e8SLadislav Michl  *	0x1 = Port power to off-chip is active-high
150976f82e8SLadislav Michl  */
15141784066SLadislav Michl # define USBDRD_UCTL_HOST_PPC_ACTIVE_HIGH_EN	BIT_ULL(24)
152976f82e8SLadislav Michl 
153976f82e8SLadislav Michl /*
154976f82e8SLadislav Michl  * UCTL Shim Features Register
155976f82e8SLadislav Michl  */
156976f82e8SLadislav Michl #define USBDRD_UCTL_SHIM_CFG			0xe8
157976f82e8SLadislav Michl /* Out-of-bound UAHC register access: 0 = read, 1 = write */
15841784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_XS_NCB_OOB_WRN	BIT_ULL(63)
159976f82e8SLadislav Michl /* SRCID error log for out-of-bound UAHC register access:
160976f82e8SLadislav Michl  *	[59:58] = chipID
161976f82e8SLadislav Michl  *	[57] = Request source: 0 = core, 1 = NCB-device
162976f82e8SLadislav Michl  *	[56:51] = Core/NCB-device number, [56] always 0 for NCB devices
163976f82e8SLadislav Michl  *	[50:48] = SubID
164976f82e8SLadislav Michl  */
16541784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_XS_NCB_OOB_OSRC	GENMASK_ULL(59, 48)
166976f82e8SLadislav Michl /* Error log for bad UAHC DMA access: 0 = Read log, 1 = Write log */
16741784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_XM_BAD_DMA_WRN	BIT_ULL(47)
168976f82e8SLadislav Michl /* Encoded error type for bad UAHC DMA */
16941784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_XM_BAD_DMA_TYPE	GENMASK_ULL(43, 40)
170976f82e8SLadislav Michl /* Select the IOI read command used by DMA accesses */
17141784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_DMA_READ_CMD	BIT_ULL(12)
172976f82e8SLadislav Michl /* Select endian format for DMA accesses to the L2C:
173976f82e8SLadislav Michl  *	0x0 = Little endian
174976f82e8SLadislav Michl  *	0x1 = Big endian
175976f82e8SLadislav Michl  *	0x2 = Reserved
176976f82e8SLadislav Michl  *	0x3 = Reserved
177976f82e8SLadislav Michl  */
17841784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_DMA_ENDIAN_MODE	GENMASK_ULL(9, 8)
179976f82e8SLadislav Michl /* Select endian format for IOI CSR access to UAHC:
180976f82e8SLadislav Michl  *	0x0 = Little endian
181976f82e8SLadislav Michl  *	0x1 = Big endian
182976f82e8SLadislav Michl  *	0x2 = Reserved
183976f82e8SLadislav Michl  *	0x3 = Reserved
184976f82e8SLadislav Michl  */
18541784066SLadislav Michl # define USBDRD_UCTL_SHIM_CFG_CSR_ENDIAN_MODE	GENMASK_ULL(1, 0)
186976f82e8SLadislav Michl 
187976f82e8SLadislav Michl #define USBDRD_UCTL_ECC				0xf0
188976f82e8SLadislav Michl #define USBDRD_UCTL_SPARE1			0xf8
189976f82e8SLadislav Michl 
190976f82e8SLadislav Michl struct dwc3_octeon {
191976f82e8SLadislav Michl 	struct device *dev;
192976f82e8SLadislav Michl 	void __iomem *base;
193976f82e8SLadislav Michl };
194976f82e8SLadislav Michl 
195c6110163SLadislav Michl #define DWC3_GPIO_POWER_NONE	(-1)
196c6110163SLadislav Michl 
197976f82e8SLadislav Michl #ifdef CONFIG_CAVIUM_OCTEON_SOC
198976f82e8SLadislav Michl #include <asm/octeon/octeon.h>
dwc3_octeon_readq(void __iomem * addr)199976f82e8SLadislav Michl static inline uint64_t dwc3_octeon_readq(void __iomem *addr)
200976f82e8SLadislav Michl {
201976f82e8SLadislav Michl 	return cvmx_readq_csr(addr);
202976f82e8SLadislav Michl }
203976f82e8SLadislav Michl 
dwc3_octeon_writeq(void __iomem * base,uint64_t val)204976f82e8SLadislav Michl static inline void dwc3_octeon_writeq(void __iomem *base, uint64_t val)
205976f82e8SLadislav Michl {
206976f82e8SLadislav Michl 	cvmx_writeq_csr(base, val);
207976f82e8SLadislav Michl }
208976f82e8SLadislav Michl 
dwc3_octeon_config_gpio(int index,int gpio)209976f82e8SLadislav Michl static void dwc3_octeon_config_gpio(int index, int gpio)
210976f82e8SLadislav Michl {
211976f82e8SLadislav Michl 	union cvmx_gpio_bit_cfgx gpio_bit;
212976f82e8SLadislav Michl 
213976f82e8SLadislav Michl 	if ((OCTEON_IS_MODEL(OCTEON_CN73XX) ||
214976f82e8SLadislav Michl 	    OCTEON_IS_MODEL(OCTEON_CNF75XX))
215976f82e8SLadislav Michl 	    && gpio <= 31) {
216976f82e8SLadislav Michl 		gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(gpio));
217976f82e8SLadislav Michl 		gpio_bit.s.tx_oe = 1;
218976f82e8SLadislav Michl 		gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x15);
219976f82e8SLadislav Michl 		cvmx_write_csr(CVMX_GPIO_BIT_CFGX(gpio), gpio_bit.u64);
220976f82e8SLadislav Michl 	} else if (gpio <= 15) {
221976f82e8SLadislav Michl 		gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(gpio));
222976f82e8SLadislav Michl 		gpio_bit.s.tx_oe = 1;
223976f82e8SLadislav Michl 		gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x19);
224976f82e8SLadislav Michl 		cvmx_write_csr(CVMX_GPIO_BIT_CFGX(gpio), gpio_bit.u64);
225976f82e8SLadislav Michl 	} else {
226976f82e8SLadislav Michl 		gpio_bit.u64 = cvmx_read_csr(CVMX_GPIO_XBIT_CFGX(gpio));
227976f82e8SLadislav Michl 		gpio_bit.s.tx_oe = 1;
228976f82e8SLadislav Michl 		gpio_bit.s.output_sel = (index == 0 ? 0x14 : 0x19);
229976f82e8SLadislav Michl 		cvmx_write_csr(CVMX_GPIO_XBIT_CFGX(gpio), gpio_bit.u64);
230976f82e8SLadislav Michl 	}
231976f82e8SLadislav Michl }
232976f82e8SLadislav Michl #else
dwc3_octeon_readq(void __iomem * addr)233976f82e8SLadislav Michl static inline uint64_t dwc3_octeon_readq(void __iomem *addr)
234976f82e8SLadislav Michl {
235976f82e8SLadislav Michl 	return 0;
236976f82e8SLadislav Michl }
237976f82e8SLadislav Michl 
dwc3_octeon_writeq(void __iomem * base,uint64_t val)238976f82e8SLadislav Michl static inline void dwc3_octeon_writeq(void __iomem *base, uint64_t val) { }
239976f82e8SLadislav Michl 
dwc3_octeon_config_gpio(int index,int gpio)240976f82e8SLadislav Michl static inline void dwc3_octeon_config_gpio(int index, int gpio) { }
241976f82e8SLadislav Michl 
octeon_get_io_clock_rate(void)242976f82e8SLadislav Michl static uint64_t octeon_get_io_clock_rate(void)
243976f82e8SLadislav Michl {
244976f82e8SLadislav Michl 	return 150000000;
245976f82e8SLadislav Michl }
246976f82e8SLadislav Michl #endif
247976f82e8SLadislav Michl 
dwc3_octeon_get_divider(void)248976f82e8SLadislav Michl static int dwc3_octeon_get_divider(void)
249976f82e8SLadislav Michl {
250976f82e8SLadislav Michl 	static const uint8_t clk_div[] = { 1, 2, 4, 6, 8, 16, 24, 32 };
251976f82e8SLadislav Michl 	int div = 0;
252976f82e8SLadislav Michl 
253976f82e8SLadislav Michl 	while (div < ARRAY_SIZE(clk_div)) {
254976f82e8SLadislav Michl 		uint64_t rate = octeon_get_io_clock_rate() / clk_div[div];
255976f82e8SLadislav Michl 		if (rate <= 300000000 && rate >= 150000000)
256fb57f829SLadislav Michl 			return div;
257976f82e8SLadislav Michl 		div++;
258976f82e8SLadislav Michl 	}
259976f82e8SLadislav Michl 
260fb57f829SLadislav Michl 	return -EINVAL;
261976f82e8SLadislav Michl }
262976f82e8SLadislav Michl 
dwc3_octeon_setup(struct dwc3_octeon * octeon,int ref_clk_sel,int ref_clk_fsel,int mpll_mul,int power_gpio,int power_active_low)263c6110163SLadislav Michl static int dwc3_octeon_setup(struct dwc3_octeon *octeon,
264dc0092ceSLadislav Michl 			     int ref_clk_sel, int ref_clk_fsel, int mpll_mul,
265c6110163SLadislav Michl 			     int power_gpio, int power_active_low)
266976f82e8SLadislav Michl {
267976f82e8SLadislav Michl 	u64 val;
268dc0092ceSLadislav Michl 	int div;
26923f87bcaSLadislav Michl 	struct device *dev = octeon->dev;
27023f87bcaSLadislav Michl 	void __iomem *uctl_ctl_reg = octeon->base + USBDRD_UCTL_CTL;
271c6110163SLadislav Michl 	void __iomem *uctl_host_cfg_reg = octeon->base + USBDRD_UCTL_HOST_CFG;
272976f82e8SLadislav Michl 
273976f82e8SLadislav Michl 	/*
274976f82e8SLadislav Michl 	 * Step 1: Wait for all voltages to be stable...that surely
275976f82e8SLadislav Michl 	 *         happened before starting the kernel. SKIP
276976f82e8SLadislav Michl 	 */
277976f82e8SLadislav Michl 
278976f82e8SLadislav Michl 	/* Step 2: Select GPIO for overcurrent indication, if desired. SKIP */
279976f82e8SLadislav Michl 
280976f82e8SLadislav Michl 	/* Step 3: Assert all resets. */
281976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
282976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_UPHY_RST |
283976f82e8SLadislav Michl 	       USBDRD_UCTL_CTL_UAHC_RST |
284976f82e8SLadislav Michl 	       USBDRD_UCTL_CTL_UCTL_RST;
285976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
286976f82e8SLadislav Michl 
287976f82e8SLadislav Michl 	/* Step 4a: Reset the clock dividers. */
288976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
289976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_H_CLKDIV_RST;
290976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
291976f82e8SLadislav Michl 
292976f82e8SLadislav Michl 	/* Step 4b: Select controller clock frequency. */
293976f82e8SLadislav Michl 	div = dwc3_octeon_get_divider();
294fb57f829SLadislav Michl 	if (div < 0) {
295fb57f829SLadislav Michl 		dev_err(dev, "clock divider invalid\n");
296fb57f829SLadislav Michl 		return div;
297fb57f829SLadislav Michl 	}
298976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
299976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_H_CLKDIV_SEL;
300976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_CTL_H_CLKDIV_SEL, div);
301976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_H_CLK_EN;
302976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
303976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
304976f82e8SLadislav Michl 	if ((div != FIELD_GET(USBDRD_UCTL_CTL_H_CLKDIV_SEL, val)) ||
305976f82e8SLadislav Michl 	    (!(FIELD_GET(USBDRD_UCTL_CTL_H_CLK_EN, val)))) {
30654026474SLadislav Michl 		dev_err(dev, "clock init failure (UCTL_CTL=%016llx)\n", val);
307976f82e8SLadislav Michl 		return -EINVAL;
308976f82e8SLadislav Michl 	}
309976f82e8SLadislav Michl 
310976f82e8SLadislav Michl 	/* Step 4c: Deassert the controller clock divider reset. */
311976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_H_CLKDIV_RST;
312976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
313976f82e8SLadislav Michl 
314976f82e8SLadislav Michl 	/* Step 5a: Reference clock configuration. */
315976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
316976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_REF_CLK_DIV2;
317976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_REF_CLK_SEL;
318976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_CTL_REF_CLK_SEL, ref_clk_sel);
319976f82e8SLadislav Michl 
320976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_REF_CLK_FSEL;
321976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_CTL_REF_CLK_FSEL, ref_clk_fsel);
322976f82e8SLadislav Michl 
323976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_MPLL_MULTIPLIER;
324976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_CTL_MPLL_MULTIPLIER, mpll_mul);
325976f82e8SLadislav Michl 
326976f82e8SLadislav Michl 	/* Step 5b: Configure and enable spread-spectrum for SuperSpeed. */
327976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_SSC_EN;
328976f82e8SLadislav Michl 
329976f82e8SLadislav Michl 	/* Step 5c: Enable SuperSpeed. */
330976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_REF_SSP_EN;
331976f82e8SLadislav Michl 
332976f82e8SLadislav Michl 	/* Step 5d: Configure PHYs. SKIP */
333976f82e8SLadislav Michl 
334976f82e8SLadislav Michl 	/* Step 6a & 6b: Power up PHYs. */
335976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_HS_POWER_EN;
336976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_SS_POWER_EN;
337976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
338976f82e8SLadislav Michl 
339976f82e8SLadislav Michl 	/* Step 7: Wait 10 controller-clock cycles to take effect. */
340976f82e8SLadislav Michl 	udelay(10);
341976f82e8SLadislav Michl 
342976f82e8SLadislav Michl 	/* Step 8a: Deassert UCTL reset signal. */
343976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
344976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_UCTL_RST;
345976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
346976f82e8SLadislav Michl 
347976f82e8SLadislav Michl 	/* Step 8b: Wait 10 controller-clock cycles. */
348976f82e8SLadislav Michl 	udelay(10);
349976f82e8SLadislav Michl 
35023f87bcaSLadislav Michl 	/* Step 8c: Setup power control. */
351c6110163SLadislav Michl 	val = dwc3_octeon_readq(uctl_host_cfg_reg);
352c6110163SLadislav Michl 	val |= USBDRD_UCTL_HOST_PPC_EN;
353c6110163SLadislav Michl 	if (power_gpio == DWC3_GPIO_POWER_NONE) {
354c6110163SLadislav Michl 		val &= ~USBDRD_UCTL_HOST_PPC_EN;
355c6110163SLadislav Michl 	} else {
356c6110163SLadislav Michl 		val |= USBDRD_UCTL_HOST_PPC_EN;
357c6110163SLadislav Michl 		dwc3_octeon_config_gpio(((__force uintptr_t)octeon->base >> 24) & 1,
358c6110163SLadislav Michl 					power_gpio);
359c6110163SLadislav Michl 		dev_dbg(dev, "power control is using gpio%d\n", power_gpio);
360c6110163SLadislav Michl 	}
361c6110163SLadislav Michl 	if (power_active_low)
362c6110163SLadislav Michl 		val &= ~USBDRD_UCTL_HOST_PPC_ACTIVE_HIGH_EN;
363c6110163SLadislav Michl 	else
364c6110163SLadislav Michl 		val |= USBDRD_UCTL_HOST_PPC_ACTIVE_HIGH_EN;
365c6110163SLadislav Michl 	dwc3_octeon_writeq(uctl_host_cfg_reg, val);
366976f82e8SLadislav Michl 
367976f82e8SLadislav Michl 	/* Step 8d: Deassert UAHC reset signal. */
368976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
369976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_UAHC_RST;
370976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
371976f82e8SLadislav Michl 
372976f82e8SLadislav Michl 	/* Step 8e: Wait 10 controller-clock cycles. */
373976f82e8SLadislav Michl 	udelay(10);
374976f82e8SLadislav Michl 
375976f82e8SLadislav Michl 	/* Step 9: Enable conditional coprocessor clock of UCTL. */
376976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
377976f82e8SLadislav Michl 	val |= USBDRD_UCTL_CTL_CSCLK_EN;
378976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
379976f82e8SLadislav Michl 
380976f82e8SLadislav Michl 	/*Step 10: Set for host mode only. */
381976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
382976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_DRD_MODE;
383976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
384976f82e8SLadislav Michl 
385976f82e8SLadislav Michl 	return 0;
386976f82e8SLadislav Michl }
387976f82e8SLadislav Michl 
dwc3_octeon_set_endian_mode(struct dwc3_octeon * octeon)38823f87bcaSLadislav Michl static void dwc3_octeon_set_endian_mode(struct dwc3_octeon *octeon)
389976f82e8SLadislav Michl {
390976f82e8SLadislav Michl 	u64 val;
39123f87bcaSLadislav Michl 	void __iomem *uctl_shim_cfg_reg = octeon->base + USBDRD_UCTL_SHIM_CFG;
392976f82e8SLadislav Michl 
393976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_shim_cfg_reg);
394976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_SHIM_CFG_DMA_ENDIAN_MODE;
395976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_SHIM_CFG_CSR_ENDIAN_MODE;
396976f82e8SLadislav Michl #ifdef __BIG_ENDIAN
397976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_SHIM_CFG_DMA_ENDIAN_MODE, 1);
398976f82e8SLadislav Michl 	val |= FIELD_PREP(USBDRD_UCTL_SHIM_CFG_CSR_ENDIAN_MODE, 1);
399976f82e8SLadislav Michl #endif
400976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_shim_cfg_reg, val);
401976f82e8SLadislav Michl }
402976f82e8SLadislav Michl 
dwc3_octeon_phy_reset(struct dwc3_octeon * octeon)40323f87bcaSLadislav Michl static void dwc3_octeon_phy_reset(struct dwc3_octeon *octeon)
404976f82e8SLadislav Michl {
405976f82e8SLadislav Michl 	u64 val;
40623f87bcaSLadislav Michl 	void __iomem *uctl_ctl_reg = octeon->base + USBDRD_UCTL_CTL;
407976f82e8SLadislav Michl 
408976f82e8SLadislav Michl 	val = dwc3_octeon_readq(uctl_ctl_reg);
409976f82e8SLadislav Michl 	val &= ~USBDRD_UCTL_CTL_UPHY_RST;
410976f82e8SLadislav Michl 	dwc3_octeon_writeq(uctl_ctl_reg, val);
411976f82e8SLadislav Michl }
412976f82e8SLadislav Michl 
dwc3_octeon_probe(struct platform_device * pdev)413976f82e8SLadislav Michl static int dwc3_octeon_probe(struct platform_device *pdev)
414976f82e8SLadislav Michl {
415976f82e8SLadislav Michl 	struct device *dev = &pdev->dev;
416976f82e8SLadislav Michl 	struct device_node *node = dev->of_node;
417976f82e8SLadislav Michl 	struct dwc3_octeon *octeon;
418dc0092ceSLadislav Michl 	const char *hs_clock_type, *ss_clock_type;
419dc0092ceSLadislav Michl 	int ref_clk_sel, ref_clk_fsel, mpll_mul;
420c6110163SLadislav Michl 	int power_active_low, power_gpio;
421c6110163SLadislav Michl 	int err, len;
422dc0092ceSLadislav Michl 	u32 clock_rate;
423dc0092ceSLadislav Michl 
424dc0092ceSLadislav Michl 	if (of_property_read_u32(node, "refclk-frequency", &clock_rate)) {
425dc0092ceSLadislav Michl 		dev_err(dev, "No UCTL \"refclk-frequency\"\n");
426dc0092ceSLadislav Michl 		return -EINVAL;
427dc0092ceSLadislav Michl 	}
428dc0092ceSLadislav Michl 	if (of_property_read_string(node, "refclk-type-ss", &ss_clock_type)) {
429dc0092ceSLadislav Michl 		dev_err(dev, "No UCTL \"refclk-type-ss\"\n");
430dc0092ceSLadislav Michl 		return -EINVAL;
431dc0092ceSLadislav Michl 	}
432dc0092ceSLadislav Michl 	if (of_property_read_string(node, "refclk-type-hs", &hs_clock_type)) {
433dc0092ceSLadislav Michl 		dev_err(dev, "No UCTL \"refclk-type-hs\"\n");
434dc0092ceSLadislav Michl 		return -EINVAL;
435dc0092ceSLadislav Michl 	}
436dc0092ceSLadislav Michl 
437dc0092ceSLadislav Michl 	ref_clk_sel = 2;
438dc0092ceSLadislav Michl 	if (strcmp("dlmc_ref_clk0", ss_clock_type) == 0) {
439dc0092ceSLadislav Michl 		if (strcmp(hs_clock_type, "dlmc_ref_clk0") == 0)
440dc0092ceSLadislav Michl 			ref_clk_sel = 0;
441dc0092ceSLadislav Michl 		else if (strcmp(hs_clock_type, "pll_ref_clk"))
442dc0092ceSLadislav Michl 			dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
443dc0092ceSLadislav Michl 				 hs_clock_type);
444dc0092ceSLadislav Michl 	} else if (strcmp(ss_clock_type, "dlmc_ref_clk1") == 0) {
445dc0092ceSLadislav Michl 		if (strcmp(hs_clock_type, "dlmc_ref_clk1") == 0) {
446dc0092ceSLadislav Michl 			ref_clk_sel = 1;
447dc0092ceSLadislav Michl 		} else {
448dc0092ceSLadislav Michl 			ref_clk_sel = 3;
449dc0092ceSLadislav Michl 			if (strcmp(hs_clock_type, "pll_ref_clk"))
450dc0092ceSLadislav Michl 				dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
451dc0092ceSLadislav Michl 					 hs_clock_type);
452dc0092ceSLadislav Michl 		}
453dc0092ceSLadislav Michl 	} else {
454dc0092ceSLadislav Michl 		dev_warn(dev, "Invalid SS clock type %s, using dlmc_ref_clk0 instead\n",
455dc0092ceSLadislav Michl 			 ss_clock_type);
456dc0092ceSLadislav Michl 	}
457dc0092ceSLadislav Michl 
458dc0092ceSLadislav Michl 	ref_clk_fsel = 0x07;
459dc0092ceSLadislav Michl 	switch (clock_rate) {
460dc0092ceSLadislav Michl 	default:
461dc0092ceSLadislav Michl 		dev_warn(dev, "Invalid ref_clk %u, using 100000000 instead\n",
462dc0092ceSLadislav Michl 			 clock_rate);
463dc0092ceSLadislav Michl 		fallthrough;
464dc0092ceSLadislav Michl 	case 100000000:
465dc0092ceSLadislav Michl 		mpll_mul = 0x19;
466dc0092ceSLadislav Michl 		if (ref_clk_sel < 2)
467dc0092ceSLadislav Michl 			ref_clk_fsel = 0x27;
468dc0092ceSLadislav Michl 		break;
469dc0092ceSLadislav Michl 	case 50000000:
470dc0092ceSLadislav Michl 		mpll_mul = 0x32;
471dc0092ceSLadislav Michl 		break;
472dc0092ceSLadislav Michl 	case 125000000:
473dc0092ceSLadislav Michl 		mpll_mul = 0x28;
474dc0092ceSLadislav Michl 		break;
475dc0092ceSLadislav Michl 	}
476c6110163SLadislav Michl 
477c6110163SLadislav Michl 	power_gpio = DWC3_GPIO_POWER_NONE;
478c6110163SLadislav Michl 	power_active_low = 0;
479c6110163SLadislav Michl 	if (of_find_property(node, "power", &len)) {
480c6110163SLadislav Michl 		u32 gpio_pwr[3];
481c6110163SLadislav Michl 
482c6110163SLadislav Michl 		switch (len) {
483c6110163SLadislav Michl 		case 8:
484c6110163SLadislav Michl 			of_property_read_u32_array(node, "power", gpio_pwr, 2);
485c6110163SLadislav Michl 			break;
486c6110163SLadislav Michl 		case 12:
487c6110163SLadislav Michl 			of_property_read_u32_array(node, "power", gpio_pwr, 3);
488c6110163SLadislav Michl 			power_active_low = gpio_pwr[2] & 0x01;
489c6110163SLadislav Michl 			break;
490c6110163SLadislav Michl 		default:
491c6110163SLadislav Michl 			dev_err(dev, "invalid power configuration\n");
492c6110163SLadislav Michl 			return -EINVAL;
493c6110163SLadislav Michl 		}
494c6110163SLadislav Michl 		power_gpio = gpio_pwr[1];
495c6110163SLadislav Michl 	}
496976f82e8SLadislav Michl 
497976f82e8SLadislav Michl 	octeon = devm_kzalloc(dev, sizeof(*octeon), GFP_KERNEL);
498976f82e8SLadislav Michl 	if (!octeon)
499976f82e8SLadislav Michl 		return -ENOMEM;
500976f82e8SLadislav Michl 
501976f82e8SLadislav Michl 	octeon->dev = dev;
502976f82e8SLadislav Michl 	octeon->base = devm_platform_ioremap_resource(pdev, 0);
503976f82e8SLadislav Michl 	if (IS_ERR(octeon->base))
504976f82e8SLadislav Michl 		return PTR_ERR(octeon->base);
505976f82e8SLadislav Michl 
506dc0092ceSLadislav Michl 	err = dwc3_octeon_setup(octeon, ref_clk_sel, ref_clk_fsel, mpll_mul,
507dc0092ceSLadislav Michl 				power_gpio, power_active_low);
508976f82e8SLadislav Michl 	if (err)
509976f82e8SLadislav Michl 		return err;
510976f82e8SLadislav Michl 
51123f87bcaSLadislav Michl 	dwc3_octeon_set_endian_mode(octeon);
51223f87bcaSLadislav Michl 	dwc3_octeon_phy_reset(octeon);
513976f82e8SLadislav Michl 
514976f82e8SLadislav Michl 	platform_set_drvdata(pdev, octeon);
515976f82e8SLadislav Michl 
516976f82e8SLadislav Michl 	return of_platform_populate(node, NULL, NULL, dev);
517976f82e8SLadislav Michl }
518976f82e8SLadislav Michl 
dwc3_octeon_remove(struct platform_device * pdev)519976f82e8SLadislav Michl static void dwc3_octeon_remove(struct platform_device *pdev)
520976f82e8SLadislav Michl {
521976f82e8SLadislav Michl 	struct dwc3_octeon *octeon = platform_get_drvdata(pdev);
522976f82e8SLadislav Michl 
523976f82e8SLadislav Michl 	of_platform_depopulate(octeon->dev);
524976f82e8SLadislav Michl }
525976f82e8SLadislav Michl 
526976f82e8SLadislav Michl static const struct of_device_id dwc3_octeon_of_match[] = {
527976f82e8SLadislav Michl 	{ .compatible = "cavium,octeon-7130-usb-uctl" },
528976f82e8SLadislav Michl 	{ },
529976f82e8SLadislav Michl };
530976f82e8SLadislav Michl MODULE_DEVICE_TABLE(of, dwc3_octeon_of_match);
531976f82e8SLadislav Michl 
532976f82e8SLadislav Michl static struct platform_driver dwc3_octeon_driver = {
533976f82e8SLadislav Michl 	.probe		= dwc3_octeon_probe,
534976f82e8SLadislav Michl 	.remove_new	= dwc3_octeon_remove,
535976f82e8SLadislav Michl 	.driver		= {
536976f82e8SLadislav Michl 		.name	= "dwc3-octeon",
537976f82e8SLadislav Michl 		.of_match_table = dwc3_octeon_of_match,
538976f82e8SLadislav Michl 	},
539976f82e8SLadislav Michl };
540976f82e8SLadislav Michl module_platform_driver(dwc3_octeon_driver);
541976f82e8SLadislav Michl 
542976f82e8SLadislav Michl MODULE_ALIAS("platform:dwc3-octeon");
543d9216d3eSLadislav Michl MODULE_AUTHOR("Ladislav Michl <ladis@linux-mips.org>");
544976f82e8SLadislav Michl MODULE_LICENSE("GPL");
545976f82e8SLadislav Michl MODULE_DESCRIPTION("DesignWare USB3 OCTEON III Glue Layer");
546