1783f6d3dSÁlvaro Fernández Rojas // SPDX-License-Identifier: GPL-2.0-or-later
2783f6d3dSÁlvaro Fernández Rojas /*
3783f6d3dSÁlvaro Fernández Rojas * BCM6328 USBH PHY Controller Driver
4783f6d3dSÁlvaro Fernández Rojas *
5783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
6783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2015 Simon Arlott
7783f6d3dSÁlvaro Fernández Rojas *
8783f6d3dSÁlvaro Fernández Rojas * Derived from bcm963xx_4.12L.06B_consumer/kernel/linux/arch/mips/bcm963xx/setup.c:
9783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2002 Broadcom Corporation
10783f6d3dSÁlvaro Fernández Rojas *
11783f6d3dSÁlvaro Fernández Rojas * Derived from OpenWrt patches:
12783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2013 Jonas Gorski <jonas.gorski@gmail.com>
13783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2013 Florian Fainelli <f.fainelli@gmail.com>
14783f6d3dSÁlvaro Fernández Rojas * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
15783f6d3dSÁlvaro Fernández Rojas */
16783f6d3dSÁlvaro Fernández Rojas
17783f6d3dSÁlvaro Fernández Rojas #include <linux/clk.h>
18783f6d3dSÁlvaro Fernández Rojas #include <linux/io.h>
19783f6d3dSÁlvaro Fernández Rojas #include <linux/module.h>
20*7559e757SRob Herring #include <linux/of.h>
21783f6d3dSÁlvaro Fernández Rojas #include <linux/phy/phy.h>
22783f6d3dSÁlvaro Fernández Rojas #include <linux/platform_device.h>
23783f6d3dSÁlvaro Fernández Rojas #include <linux/reset.h>
24783f6d3dSÁlvaro Fernández Rojas
25783f6d3dSÁlvaro Fernández Rojas /* USBH control register offsets */
26783f6d3dSÁlvaro Fernández Rojas enum usbh_regs {
27783f6d3dSÁlvaro Fernández Rojas USBH_BRT_CONTROL1 = 0,
28783f6d3dSÁlvaro Fernández Rojas USBH_BRT_CONTROL2,
29783f6d3dSÁlvaro Fernández Rojas USBH_BRT_STATUS1,
30783f6d3dSÁlvaro Fernández Rojas USBH_BRT_STATUS2,
31783f6d3dSÁlvaro Fernández Rojas USBH_UTMI_CONTROL1,
32783f6d3dSÁlvaro Fernández Rojas #define USBH_UC1_DEV_MODE_SEL BIT(0)
33783f6d3dSÁlvaro Fernández Rojas USBH_TEST_PORT_CONTROL,
34783f6d3dSÁlvaro Fernández Rojas USBH_PLL_CONTROL1,
35783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_REFCLKSEL_SHIFT 0
36783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_REFCLKSEL_MASK (0x3 << USBH_PLLC_REFCLKSEL_SHIFT)
37783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_CLKSEL_SHIFT 2
38783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_CLKSEL_MASK (0x3 << USBH_PLLC_CLKSEL_MASK)
39783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_XTAL_PWRDWNB BIT(4)
40783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PLL_PWRDWNB BIT(5)
41783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PLL_CALEN BIT(6)
42783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PHYPLL_BYP BIT(7)
43783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PLL_RESET BIT(8)
44783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PLL_IDDQ_PWRDN BIT(9)
45783f6d3dSÁlvaro Fernández Rojas #define USBH_PLLC_PLL_PWRDN_DELAY BIT(10)
46783f6d3dSÁlvaro Fernández Rojas #define USBH_6318_PLLC_PLL_SUSPEND_EN BIT(27)
47783f6d3dSÁlvaro Fernández Rojas #define USBH_6318_PLLC_PHYPLL_BYP BIT(29)
48783f6d3dSÁlvaro Fernández Rojas #define USBH_6318_PLLC_PLL_RESET BIT(30)
49783f6d3dSÁlvaro Fernández Rojas #define USBH_6318_PLLC_PLL_IDDQ_PWRDN BIT(31)
50783f6d3dSÁlvaro Fernández Rojas USBH_SWAP_CONTROL,
51783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_OHCI_DATA_SWAP BIT(0)
52783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_OHCI_ENDIAN_SWAP BIT(1)
53783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_OHCI_LOGICAL_ADDR_EN BIT(2)
54783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_EHCI_DATA_SWAP BIT(3)
55783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_EHCI_ENDIAN_SWAP BIT(4)
56783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_EHCI_LOGICAL_ADDR_EN BIT(5)
57783f6d3dSÁlvaro Fernández Rojas #define USBH_SC_USB_DEVICE_SEL BIT(6)
58783f6d3dSÁlvaro Fernández Rojas USBH_GENERIC_CONTROL,
59783f6d3dSÁlvaro Fernández Rojas #define USBH_GC_PLL_SUSPEND_EN BIT(1)
60783f6d3dSÁlvaro Fernández Rojas USBH_FRAME_ADJUST_VALUE,
61783f6d3dSÁlvaro Fernández Rojas USBH_SETUP,
62783f6d3dSÁlvaro Fernández Rojas #define USBH_S_IOC BIT(4)
63783f6d3dSÁlvaro Fernández Rojas #define USBH_S_IPP BIT(5)
64783f6d3dSÁlvaro Fernández Rojas USBH_MDIO,
65783f6d3dSÁlvaro Fernández Rojas USBH_MDIO32,
66783f6d3dSÁlvaro Fernández Rojas USBH_USB_SIM_CONTROL,
67783f6d3dSÁlvaro Fernández Rojas #define USBH_USC_LADDR_SEL BIT(5)
68783f6d3dSÁlvaro Fernández Rojas
69783f6d3dSÁlvaro Fernández Rojas __USBH_ENUM_SIZE
70783f6d3dSÁlvaro Fernández Rojas };
71783f6d3dSÁlvaro Fernández Rojas
72783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy_variant {
73783f6d3dSÁlvaro Fernández Rojas /* Registers */
74783f6d3dSÁlvaro Fernández Rojas long regs[__USBH_ENUM_SIZE];
75783f6d3dSÁlvaro Fernández Rojas
76783f6d3dSÁlvaro Fernández Rojas /* PLLC bits to set/clear for power on */
77783f6d3dSÁlvaro Fernández Rojas u32 power_pllc_clr;
78783f6d3dSÁlvaro Fernández Rojas u32 power_pllc_set;
79783f6d3dSÁlvaro Fernández Rojas
80783f6d3dSÁlvaro Fernández Rojas /* Setup bits to set/clear for power on */
81783f6d3dSÁlvaro Fernández Rojas u32 setup_clr;
82783f6d3dSÁlvaro Fernández Rojas u32 setup_set;
83783f6d3dSÁlvaro Fernández Rojas
84783f6d3dSÁlvaro Fernández Rojas /* Swap Control bits to set */
85783f6d3dSÁlvaro Fernández Rojas u32 swapctl_dev_set;
86783f6d3dSÁlvaro Fernández Rojas
87783f6d3dSÁlvaro Fernández Rojas /* Test Port Control value to set if non-zero */
88783f6d3dSÁlvaro Fernández Rojas u32 tpc_val;
89783f6d3dSÁlvaro Fernández Rojas
90783f6d3dSÁlvaro Fernández Rojas /* USB Sim Control bits to set */
91783f6d3dSÁlvaro Fernández Rojas u32 usc_set;
92783f6d3dSÁlvaro Fernández Rojas
93783f6d3dSÁlvaro Fernández Rojas /* UTMI Control 1 bits to set */
94783f6d3dSÁlvaro Fernández Rojas u32 utmictl1_dev_set;
95783f6d3dSÁlvaro Fernández Rojas };
96783f6d3dSÁlvaro Fernández Rojas
97783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy {
98783f6d3dSÁlvaro Fernández Rojas void __iomem *base;
99783f6d3dSÁlvaro Fernández Rojas struct clk *usbh_clk;
100783f6d3dSÁlvaro Fernández Rojas struct clk *usb_ref_clk;
101783f6d3dSÁlvaro Fernández Rojas struct reset_control *reset;
102783f6d3dSÁlvaro Fernández Rojas const struct bcm63xx_usbh_phy_variant *variant;
103783f6d3dSÁlvaro Fernández Rojas bool device_mode;
104783f6d3dSÁlvaro Fernández Rojas };
105783f6d3dSÁlvaro Fernández Rojas
106783f6d3dSÁlvaro Fernández Rojas static const struct bcm63xx_usbh_phy_variant usbh_bcm6318 = {
107783f6d3dSÁlvaro Fernández Rojas .regs = {
108783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL1] = -1,
109783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL2] = -1,
110783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS1] = -1,
111783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS2] = -1,
112783f6d3dSÁlvaro Fernández Rojas [USBH_UTMI_CONTROL1] = 0x2c,
113783f6d3dSÁlvaro Fernández Rojas [USBH_TEST_PORT_CONTROL] = 0x1c,
114783f6d3dSÁlvaro Fernández Rojas [USBH_PLL_CONTROL1] = 0x04,
115783f6d3dSÁlvaro Fernández Rojas [USBH_SWAP_CONTROL] = 0x0c,
116783f6d3dSÁlvaro Fernández Rojas [USBH_GENERIC_CONTROL] = -1,
117783f6d3dSÁlvaro Fernández Rojas [USBH_FRAME_ADJUST_VALUE] = 0x08,
118783f6d3dSÁlvaro Fernández Rojas [USBH_SETUP] = 0x00,
119783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO] = 0x14,
120783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO32] = 0x18,
121783f6d3dSÁlvaro Fernández Rojas [USBH_USB_SIM_CONTROL] = 0x20,
122783f6d3dSÁlvaro Fernández Rojas },
123783f6d3dSÁlvaro Fernández Rojas .power_pllc_clr = USBH_6318_PLLC_PLL_IDDQ_PWRDN,
124783f6d3dSÁlvaro Fernández Rojas .power_pllc_set = USBH_6318_PLLC_PLL_SUSPEND_EN,
125783f6d3dSÁlvaro Fernández Rojas .setup_set = USBH_S_IOC,
126783f6d3dSÁlvaro Fernández Rojas .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
127783f6d3dSÁlvaro Fernández Rojas .usc_set = USBH_USC_LADDR_SEL,
128783f6d3dSÁlvaro Fernández Rojas .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
129783f6d3dSÁlvaro Fernández Rojas };
130783f6d3dSÁlvaro Fernández Rojas
131783f6d3dSÁlvaro Fernández Rojas static const struct bcm63xx_usbh_phy_variant usbh_bcm6328 = {
132783f6d3dSÁlvaro Fernández Rojas .regs = {
133783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL1] = 0x00,
134783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL2] = 0x04,
135783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS1] = 0x08,
136783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS2] = 0x0c,
137783f6d3dSÁlvaro Fernández Rojas [USBH_UTMI_CONTROL1] = 0x10,
138783f6d3dSÁlvaro Fernández Rojas [USBH_TEST_PORT_CONTROL] = 0x14,
139783f6d3dSÁlvaro Fernández Rojas [USBH_PLL_CONTROL1] = 0x18,
140783f6d3dSÁlvaro Fernández Rojas [USBH_SWAP_CONTROL] = 0x1c,
141783f6d3dSÁlvaro Fernández Rojas [USBH_GENERIC_CONTROL] = 0x20,
142783f6d3dSÁlvaro Fernández Rojas [USBH_FRAME_ADJUST_VALUE] = 0x24,
143783f6d3dSÁlvaro Fernández Rojas [USBH_SETUP] = 0x28,
144783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO] = 0x2c,
145783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO32] = 0x30,
146783f6d3dSÁlvaro Fernández Rojas [USBH_USB_SIM_CONTROL] = 0x34,
147783f6d3dSÁlvaro Fernández Rojas },
148783f6d3dSÁlvaro Fernández Rojas .setup_set = USBH_S_IOC,
149783f6d3dSÁlvaro Fernández Rojas .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
150783f6d3dSÁlvaro Fernández Rojas .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
151783f6d3dSÁlvaro Fernández Rojas };
152783f6d3dSÁlvaro Fernández Rojas
153783f6d3dSÁlvaro Fernández Rojas static const struct bcm63xx_usbh_phy_variant usbh_bcm6358 = {
154783f6d3dSÁlvaro Fernández Rojas .regs = {
155783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL1] = -1,
156783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL2] = -1,
157783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS1] = -1,
158783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS2] = -1,
159783f6d3dSÁlvaro Fernández Rojas [USBH_UTMI_CONTROL1] = -1,
160783f6d3dSÁlvaro Fernández Rojas [USBH_TEST_PORT_CONTROL] = 0x24,
161783f6d3dSÁlvaro Fernández Rojas [USBH_PLL_CONTROL1] = -1,
162783f6d3dSÁlvaro Fernández Rojas [USBH_SWAP_CONTROL] = 0x00,
163783f6d3dSÁlvaro Fernández Rojas [USBH_GENERIC_CONTROL] = -1,
164783f6d3dSÁlvaro Fernández Rojas [USBH_FRAME_ADJUST_VALUE] = -1,
165783f6d3dSÁlvaro Fernández Rojas [USBH_SETUP] = -1,
166783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO] = -1,
167783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO32] = -1,
168783f6d3dSÁlvaro Fernández Rojas [USBH_USB_SIM_CONTROL] = -1,
169783f6d3dSÁlvaro Fernández Rojas },
170783f6d3dSÁlvaro Fernández Rojas /*
171783f6d3dSÁlvaro Fernández Rojas * The magic value comes for the original vendor BSP
172783f6d3dSÁlvaro Fernández Rojas * and is needed for USB to work. Datasheet does not
173783f6d3dSÁlvaro Fernández Rojas * help, so the magic value is used as-is.
174783f6d3dSÁlvaro Fernández Rojas */
175783f6d3dSÁlvaro Fernández Rojas .tpc_val = 0x1c0020,
176783f6d3dSÁlvaro Fernández Rojas };
177783f6d3dSÁlvaro Fernández Rojas
178783f6d3dSÁlvaro Fernández Rojas static const struct bcm63xx_usbh_phy_variant usbh_bcm6368 = {
179783f6d3dSÁlvaro Fernández Rojas .regs = {
180783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL1] = 0x00,
181783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL2] = 0x04,
182783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS1] = 0x08,
183783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS2] = 0x0c,
184783f6d3dSÁlvaro Fernández Rojas [USBH_UTMI_CONTROL1] = 0x10,
185783f6d3dSÁlvaro Fernández Rojas [USBH_TEST_PORT_CONTROL] = 0x14,
186783f6d3dSÁlvaro Fernández Rojas [USBH_PLL_CONTROL1] = 0x18,
187783f6d3dSÁlvaro Fernández Rojas [USBH_SWAP_CONTROL] = 0x1c,
188783f6d3dSÁlvaro Fernández Rojas [USBH_GENERIC_CONTROL] = -1,
189783f6d3dSÁlvaro Fernández Rojas [USBH_FRAME_ADJUST_VALUE] = 0x24,
190783f6d3dSÁlvaro Fernández Rojas [USBH_SETUP] = 0x28,
191783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO] = 0x2c,
192783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO32] = 0x30,
193783f6d3dSÁlvaro Fernández Rojas [USBH_USB_SIM_CONTROL] = 0x34,
194783f6d3dSÁlvaro Fernández Rojas },
195783f6d3dSÁlvaro Fernández Rojas .power_pllc_clr = USBH_PLLC_PLL_IDDQ_PWRDN | USBH_PLLC_PLL_PWRDN_DELAY,
196783f6d3dSÁlvaro Fernández Rojas .setup_set = USBH_S_IOC,
197783f6d3dSÁlvaro Fernández Rojas .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
198783f6d3dSÁlvaro Fernández Rojas .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
199783f6d3dSÁlvaro Fernández Rojas };
200783f6d3dSÁlvaro Fernández Rojas
201783f6d3dSÁlvaro Fernández Rojas static const struct bcm63xx_usbh_phy_variant usbh_bcm63268 = {
202783f6d3dSÁlvaro Fernández Rojas .regs = {
203783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL1] = 0x00,
204783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_CONTROL2] = 0x04,
205783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS1] = 0x08,
206783f6d3dSÁlvaro Fernández Rojas [USBH_BRT_STATUS2] = 0x0c,
207783f6d3dSÁlvaro Fernández Rojas [USBH_UTMI_CONTROL1] = 0x10,
208783f6d3dSÁlvaro Fernández Rojas [USBH_TEST_PORT_CONTROL] = 0x14,
209783f6d3dSÁlvaro Fernández Rojas [USBH_PLL_CONTROL1] = 0x18,
210783f6d3dSÁlvaro Fernández Rojas [USBH_SWAP_CONTROL] = 0x1c,
211783f6d3dSÁlvaro Fernández Rojas [USBH_GENERIC_CONTROL] = 0x20,
212783f6d3dSÁlvaro Fernández Rojas [USBH_FRAME_ADJUST_VALUE] = 0x24,
213783f6d3dSÁlvaro Fernández Rojas [USBH_SETUP] = 0x28,
214783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO] = 0x2c,
215783f6d3dSÁlvaro Fernández Rojas [USBH_MDIO32] = 0x30,
216783f6d3dSÁlvaro Fernández Rojas [USBH_USB_SIM_CONTROL] = 0x34,
217783f6d3dSÁlvaro Fernández Rojas },
218783f6d3dSÁlvaro Fernández Rojas .power_pllc_clr = USBH_PLLC_PLL_IDDQ_PWRDN | USBH_PLLC_PLL_PWRDN_DELAY,
219783f6d3dSÁlvaro Fernández Rojas .setup_clr = USBH_S_IPP,
220783f6d3dSÁlvaro Fernández Rojas .setup_set = USBH_S_IOC,
221783f6d3dSÁlvaro Fernández Rojas .swapctl_dev_set = USBH_SC_USB_DEVICE_SEL,
222783f6d3dSÁlvaro Fernández Rojas .utmictl1_dev_set = USBH_UC1_DEV_MODE_SEL,
223783f6d3dSÁlvaro Fernández Rojas };
224783f6d3dSÁlvaro Fernández Rojas
usbh_has_reg(struct bcm63xx_usbh_phy * usbh,int reg)225783f6d3dSÁlvaro Fernández Rojas static inline bool usbh_has_reg(struct bcm63xx_usbh_phy *usbh, int reg)
226783f6d3dSÁlvaro Fernández Rojas {
227783f6d3dSÁlvaro Fernández Rojas return (usbh->variant->regs[reg] >= 0);
228783f6d3dSÁlvaro Fernández Rojas }
229783f6d3dSÁlvaro Fernández Rojas
usbh_readl(struct bcm63xx_usbh_phy * usbh,int reg)230783f6d3dSÁlvaro Fernández Rojas static inline u32 usbh_readl(struct bcm63xx_usbh_phy *usbh, int reg)
231783f6d3dSÁlvaro Fernández Rojas {
232783f6d3dSÁlvaro Fernández Rojas return __raw_readl(usbh->base + usbh->variant->regs[reg]);
233783f6d3dSÁlvaro Fernández Rojas }
234783f6d3dSÁlvaro Fernández Rojas
usbh_writel(struct bcm63xx_usbh_phy * usbh,int reg,u32 value)235783f6d3dSÁlvaro Fernández Rojas static inline void usbh_writel(struct bcm63xx_usbh_phy *usbh, int reg,
236783f6d3dSÁlvaro Fernández Rojas u32 value)
237783f6d3dSÁlvaro Fernández Rojas {
238783f6d3dSÁlvaro Fernández Rojas __raw_writel(value, usbh->base + usbh->variant->regs[reg]);
239783f6d3dSÁlvaro Fernández Rojas }
240783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_init(struct phy * phy)241783f6d3dSÁlvaro Fernández Rojas static int bcm63xx_usbh_phy_init(struct phy *phy)
242783f6d3dSÁlvaro Fernández Rojas {
243783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
244783f6d3dSÁlvaro Fernández Rojas int ret;
245783f6d3dSÁlvaro Fernández Rojas
246783f6d3dSÁlvaro Fernández Rojas ret = clk_prepare_enable(usbh->usbh_clk);
247783f6d3dSÁlvaro Fernández Rojas if (ret) {
248783f6d3dSÁlvaro Fernández Rojas dev_err(&phy->dev, "unable to enable usbh clock: %d\n", ret);
249783f6d3dSÁlvaro Fernández Rojas return ret;
250783f6d3dSÁlvaro Fernández Rojas }
251783f6d3dSÁlvaro Fernández Rojas
252783f6d3dSÁlvaro Fernández Rojas ret = clk_prepare_enable(usbh->usb_ref_clk);
253783f6d3dSÁlvaro Fernández Rojas if (ret) {
254783f6d3dSÁlvaro Fernández Rojas dev_err(&phy->dev, "unable to enable usb_ref clock: %d\n", ret);
255783f6d3dSÁlvaro Fernández Rojas clk_disable_unprepare(usbh->usbh_clk);
256783f6d3dSÁlvaro Fernández Rojas return ret;
257783f6d3dSÁlvaro Fernández Rojas }
258783f6d3dSÁlvaro Fernández Rojas
259783f6d3dSÁlvaro Fernández Rojas ret = reset_control_reset(usbh->reset);
260783f6d3dSÁlvaro Fernández Rojas if (ret) {
261783f6d3dSÁlvaro Fernández Rojas dev_err(&phy->dev, "unable to reset device: %d\n", ret);
262783f6d3dSÁlvaro Fernández Rojas clk_disable_unprepare(usbh->usb_ref_clk);
263783f6d3dSÁlvaro Fernández Rojas clk_disable_unprepare(usbh->usbh_clk);
264783f6d3dSÁlvaro Fernández Rojas return ret;
265783f6d3dSÁlvaro Fernández Rojas }
266783f6d3dSÁlvaro Fernández Rojas
267783f6d3dSÁlvaro Fernández Rojas /* Configure to work in native CPU endian */
268783f6d3dSÁlvaro Fernández Rojas if (usbh_has_reg(usbh, USBH_SWAP_CONTROL)) {
269783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_SWAP_CONTROL);
270783f6d3dSÁlvaro Fernández Rojas
271783f6d3dSÁlvaro Fernández Rojas val |= USBH_SC_EHCI_DATA_SWAP;
272783f6d3dSÁlvaro Fernández Rojas val &= ~USBH_SC_EHCI_ENDIAN_SWAP;
273783f6d3dSÁlvaro Fernández Rojas
274783f6d3dSÁlvaro Fernández Rojas val |= USBH_SC_OHCI_DATA_SWAP;
275783f6d3dSÁlvaro Fernández Rojas val &= ~USBH_SC_OHCI_ENDIAN_SWAP;
276783f6d3dSÁlvaro Fernández Rojas
277783f6d3dSÁlvaro Fernández Rojas if (usbh->device_mode && usbh->variant->swapctl_dev_set)
278783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->swapctl_dev_set;
279783f6d3dSÁlvaro Fernández Rojas
280783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_SWAP_CONTROL, val);
281783f6d3dSÁlvaro Fernández Rojas }
282783f6d3dSÁlvaro Fernández Rojas
283783f6d3dSÁlvaro Fernández Rojas if (usbh_has_reg(usbh, USBH_SETUP)) {
284783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_SETUP);
285783f6d3dSÁlvaro Fernández Rojas
286783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->setup_set;
287783f6d3dSÁlvaro Fernández Rojas val &= ~usbh->variant->setup_clr;
288783f6d3dSÁlvaro Fernández Rojas
289783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_SETUP, val);
290783f6d3dSÁlvaro Fernández Rojas }
291783f6d3dSÁlvaro Fernández Rojas
292783f6d3dSÁlvaro Fernández Rojas if (usbh_has_reg(usbh, USBH_USB_SIM_CONTROL)) {
293783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_USB_SIM_CONTROL);
294783f6d3dSÁlvaro Fernández Rojas
295783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->usc_set;
296783f6d3dSÁlvaro Fernández Rojas
297783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_USB_SIM_CONTROL, val);
298783f6d3dSÁlvaro Fernández Rojas }
299783f6d3dSÁlvaro Fernández Rojas
300783f6d3dSÁlvaro Fernández Rojas if (usbh->variant->tpc_val &&
301783f6d3dSÁlvaro Fernández Rojas usbh_has_reg(usbh, USBH_TEST_PORT_CONTROL))
302783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_TEST_PORT_CONTROL,
303783f6d3dSÁlvaro Fernández Rojas usbh->variant->tpc_val);
304783f6d3dSÁlvaro Fernández Rojas
305783f6d3dSÁlvaro Fernández Rojas if (usbh->device_mode &&
306783f6d3dSÁlvaro Fernández Rojas usbh_has_reg(usbh, USBH_UTMI_CONTROL1) &&
307783f6d3dSÁlvaro Fernández Rojas usbh->variant->utmictl1_dev_set) {
308783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_UTMI_CONTROL1);
309783f6d3dSÁlvaro Fernández Rojas
310783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->utmictl1_dev_set;
311783f6d3dSÁlvaro Fernández Rojas
312783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_UTMI_CONTROL1, val);
313783f6d3dSÁlvaro Fernández Rojas }
314783f6d3dSÁlvaro Fernández Rojas
315783f6d3dSÁlvaro Fernández Rojas return 0;
316783f6d3dSÁlvaro Fernández Rojas }
317783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_power_on(struct phy * phy)318783f6d3dSÁlvaro Fernández Rojas static int bcm63xx_usbh_phy_power_on(struct phy *phy)
319783f6d3dSÁlvaro Fernández Rojas {
320783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
321783f6d3dSÁlvaro Fernández Rojas
322783f6d3dSÁlvaro Fernández Rojas if (usbh_has_reg(usbh, USBH_PLL_CONTROL1)) {
323783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_PLL_CONTROL1);
324783f6d3dSÁlvaro Fernández Rojas
325783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->power_pllc_set;
326783f6d3dSÁlvaro Fernández Rojas val &= ~usbh->variant->power_pllc_clr;
327783f6d3dSÁlvaro Fernández Rojas
328783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_PLL_CONTROL1, val);
329783f6d3dSÁlvaro Fernández Rojas }
330783f6d3dSÁlvaro Fernández Rojas
331783f6d3dSÁlvaro Fernández Rojas return 0;
332783f6d3dSÁlvaro Fernández Rojas }
333783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_power_off(struct phy * phy)334783f6d3dSÁlvaro Fernández Rojas static int bcm63xx_usbh_phy_power_off(struct phy *phy)
335783f6d3dSÁlvaro Fernández Rojas {
336783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
337783f6d3dSÁlvaro Fernández Rojas
338783f6d3dSÁlvaro Fernández Rojas if (usbh_has_reg(usbh, USBH_PLL_CONTROL1)) {
339783f6d3dSÁlvaro Fernández Rojas u32 val = usbh_readl(usbh, USBH_PLL_CONTROL1);
340783f6d3dSÁlvaro Fernández Rojas
341783f6d3dSÁlvaro Fernández Rojas val &= ~usbh->variant->power_pllc_set;
342783f6d3dSÁlvaro Fernández Rojas val |= usbh->variant->power_pllc_clr;
343783f6d3dSÁlvaro Fernández Rojas
344783f6d3dSÁlvaro Fernández Rojas usbh_writel(usbh, USBH_PLL_CONTROL1, val);
345783f6d3dSÁlvaro Fernández Rojas }
346783f6d3dSÁlvaro Fernández Rojas
347783f6d3dSÁlvaro Fernández Rojas return 0;
348783f6d3dSÁlvaro Fernández Rojas }
349783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_exit(struct phy * phy)350783f6d3dSÁlvaro Fernández Rojas static int bcm63xx_usbh_phy_exit(struct phy *phy)
351783f6d3dSÁlvaro Fernández Rojas {
352783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh = phy_get_drvdata(phy);
353783f6d3dSÁlvaro Fernández Rojas
354783f6d3dSÁlvaro Fernández Rojas clk_disable_unprepare(usbh->usbh_clk);
355783f6d3dSÁlvaro Fernández Rojas clk_disable_unprepare(usbh->usb_ref_clk);
356783f6d3dSÁlvaro Fernández Rojas
357783f6d3dSÁlvaro Fernández Rojas return 0;
358783f6d3dSÁlvaro Fernández Rojas }
359783f6d3dSÁlvaro Fernández Rojas
360783f6d3dSÁlvaro Fernández Rojas static const struct phy_ops bcm63xx_usbh_phy_ops = {
361783f6d3dSÁlvaro Fernández Rojas .exit = bcm63xx_usbh_phy_exit,
362783f6d3dSÁlvaro Fernández Rojas .init = bcm63xx_usbh_phy_init,
363783f6d3dSÁlvaro Fernández Rojas .power_off = bcm63xx_usbh_phy_power_off,
364783f6d3dSÁlvaro Fernández Rojas .power_on = bcm63xx_usbh_phy_power_on,
365783f6d3dSÁlvaro Fernández Rojas .owner = THIS_MODULE,
366783f6d3dSÁlvaro Fernández Rojas };
367783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_xlate(struct device * dev,struct of_phandle_args * args)368783f6d3dSÁlvaro Fernández Rojas static struct phy *bcm63xx_usbh_phy_xlate(struct device *dev,
369783f6d3dSÁlvaro Fernández Rojas struct of_phandle_args *args)
370783f6d3dSÁlvaro Fernández Rojas {
371783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh = dev_get_drvdata(dev);
372783f6d3dSÁlvaro Fernández Rojas
373783f6d3dSÁlvaro Fernández Rojas usbh->device_mode = !!args->args[0];
374783f6d3dSÁlvaro Fernández Rojas
375783f6d3dSÁlvaro Fernández Rojas return of_phy_simple_xlate(dev, args);
376783f6d3dSÁlvaro Fernández Rojas }
377783f6d3dSÁlvaro Fernández Rojas
bcm63xx_usbh_phy_probe(struct platform_device * pdev)378783f6d3dSÁlvaro Fernández Rojas static int __init bcm63xx_usbh_phy_probe(struct platform_device *pdev)
379783f6d3dSÁlvaro Fernández Rojas {
380783f6d3dSÁlvaro Fernández Rojas struct device *dev = &pdev->dev;
381783f6d3dSÁlvaro Fernández Rojas struct bcm63xx_usbh_phy *usbh;
382783f6d3dSÁlvaro Fernández Rojas const struct bcm63xx_usbh_phy_variant *variant;
383783f6d3dSÁlvaro Fernández Rojas struct phy *phy;
384783f6d3dSÁlvaro Fernández Rojas struct phy_provider *phy_provider;
385783f6d3dSÁlvaro Fernández Rojas
386783f6d3dSÁlvaro Fernández Rojas usbh = devm_kzalloc(dev, sizeof(*usbh), GFP_KERNEL);
387783f6d3dSÁlvaro Fernández Rojas if (!usbh)
388783f6d3dSÁlvaro Fernández Rojas return -ENOMEM;
389783f6d3dSÁlvaro Fernández Rojas
390783f6d3dSÁlvaro Fernández Rojas variant = device_get_match_data(dev);
391783f6d3dSÁlvaro Fernández Rojas if (!variant)
392783f6d3dSÁlvaro Fernández Rojas return -EINVAL;
393783f6d3dSÁlvaro Fernández Rojas usbh->variant = variant;
394783f6d3dSÁlvaro Fernández Rojas
395783f6d3dSÁlvaro Fernández Rojas usbh->base = devm_platform_ioremap_resource(pdev, 0);
396783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(usbh->base))
397783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(usbh->base);
398783f6d3dSÁlvaro Fernández Rojas
399783f6d3dSÁlvaro Fernández Rojas usbh->reset = devm_reset_control_get_exclusive(dev, NULL);
400783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(usbh->reset)) {
401783f6d3dSÁlvaro Fernández Rojas if (PTR_ERR(usbh->reset) != -EPROBE_DEFER)
402783f6d3dSÁlvaro Fernández Rojas dev_err(dev, "failed to get reset\n");
403783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(usbh->reset);
404783f6d3dSÁlvaro Fernández Rojas }
405783f6d3dSÁlvaro Fernández Rojas
406783f6d3dSÁlvaro Fernández Rojas usbh->usbh_clk = devm_clk_get_optional(dev, "usbh");
407783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(usbh->usbh_clk))
408783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(usbh->usbh_clk);
409783f6d3dSÁlvaro Fernández Rojas
410783f6d3dSÁlvaro Fernández Rojas usbh->usb_ref_clk = devm_clk_get_optional(dev, "usb_ref");
411783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(usbh->usb_ref_clk))
412783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(usbh->usb_ref_clk);
413783f6d3dSÁlvaro Fernández Rojas
414783f6d3dSÁlvaro Fernández Rojas phy = devm_phy_create(dev, NULL, &bcm63xx_usbh_phy_ops);
415783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(phy)) {
416783f6d3dSÁlvaro Fernández Rojas dev_err(dev, "failed to create PHY\n");
417783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(phy);
418783f6d3dSÁlvaro Fernández Rojas }
419783f6d3dSÁlvaro Fernández Rojas
420783f6d3dSÁlvaro Fernández Rojas platform_set_drvdata(pdev, usbh);
421783f6d3dSÁlvaro Fernández Rojas phy_set_drvdata(phy, usbh);
422783f6d3dSÁlvaro Fernández Rojas
423783f6d3dSÁlvaro Fernández Rojas phy_provider = devm_of_phy_provider_register(dev,
424783f6d3dSÁlvaro Fernández Rojas bcm63xx_usbh_phy_xlate);
425783f6d3dSÁlvaro Fernández Rojas if (IS_ERR(phy_provider)) {
426783f6d3dSÁlvaro Fernández Rojas dev_err(dev, "failed to register PHY provider\n");
427783f6d3dSÁlvaro Fernández Rojas return PTR_ERR(phy_provider);
428783f6d3dSÁlvaro Fernández Rojas }
429783f6d3dSÁlvaro Fernández Rojas
430783f6d3dSÁlvaro Fernández Rojas dev_dbg(dev, "Registered BCM63xx USB PHY driver\n");
431783f6d3dSÁlvaro Fernández Rojas
432783f6d3dSÁlvaro Fernández Rojas return 0;
433783f6d3dSÁlvaro Fernández Rojas }
434783f6d3dSÁlvaro Fernández Rojas
435783f6d3dSÁlvaro Fernández Rojas static const struct of_device_id bcm63xx_usbh_phy_ids[] __initconst = {
436783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm6318-usbh-phy", .data = &usbh_bcm6318 },
437783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm6328-usbh-phy", .data = &usbh_bcm6328 },
438783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm6358-usbh-phy", .data = &usbh_bcm6358 },
439783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm6362-usbh-phy", .data = &usbh_bcm6368 },
440783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm6368-usbh-phy", .data = &usbh_bcm6368 },
441783f6d3dSÁlvaro Fernández Rojas { .compatible = "brcm,bcm63268-usbh-phy", .data = &usbh_bcm63268 },
442783f6d3dSÁlvaro Fernández Rojas { /* sentinel */ }
443783f6d3dSÁlvaro Fernández Rojas };
444783f6d3dSÁlvaro Fernández Rojas MODULE_DEVICE_TABLE(of, bcm63xx_usbh_phy_ids);
445783f6d3dSÁlvaro Fernández Rojas
446783f6d3dSÁlvaro Fernández Rojas static struct platform_driver bcm63xx_usbh_phy_driver __refdata = {
447783f6d3dSÁlvaro Fernández Rojas .driver = {
448783f6d3dSÁlvaro Fernández Rojas .name = "bcm63xx-usbh-phy",
449783f6d3dSÁlvaro Fernández Rojas .of_match_table = bcm63xx_usbh_phy_ids,
450783f6d3dSÁlvaro Fernández Rojas },
451783f6d3dSÁlvaro Fernández Rojas .probe = bcm63xx_usbh_phy_probe,
452783f6d3dSÁlvaro Fernández Rojas };
453783f6d3dSÁlvaro Fernández Rojas module_platform_driver(bcm63xx_usbh_phy_driver);
454783f6d3dSÁlvaro Fernández Rojas
455783f6d3dSÁlvaro Fernández Rojas MODULE_DESCRIPTION("BCM63xx USBH PHY driver");
456783f6d3dSÁlvaro Fernández Rojas MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
457783f6d3dSÁlvaro Fernández Rojas MODULE_AUTHOR("Simon Arlott");
458783f6d3dSÁlvaro Fernández Rojas MODULE_LICENSE("GPL");
459