1 // SPDX-License-Identifier: GPL-2.0+
3 * ti_usb_phy.c - USB3 and USB3 PHY programming for dwc3
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 * Taken from Linux Kernel v3.16 (drivers/phy/phy-ti-pipe3.c and
10 * drivers/phy/phy-omap-usb2.c) and ported to uboot.
12 * "commit 56042e : phy: ti-pipe3: Fix suspend/resume and module reload" for
15 * "commit eb82a3 : phy: omap-usb2: Balance pm_runtime_enable() on probe failure
16 * and remove" for phy-omap-usb2.c
21 #include <ti-usb-phy-uboot.h>
22 #include <dm/device_compat.h>
23 #include <dm/devres.h>
24 #include <linux/delay.h>
25 #include <linux/ioport.h>
27 #include <asm/arch/sys_proto.h>
30 #include "linux-compat.h"
32 #define PLL_STATUS 0x00000004
33 #define PLL_GO 0x00000008
34 #define PLL_CONFIGURATION1 0x0000000C
35 #define PLL_CONFIGURATION2 0x00000010
36 #define PLL_CONFIGURATION3 0x00000014
37 #define PLL_CONFIGURATION4 0x00000020
39 #define PLL_REGM_MASK 0x001FFE00
40 #define PLL_REGM_SHIFT 0x9
41 #define PLL_REGM_F_MASK 0x0003FFFF
42 #define PLL_REGM_F_SHIFT 0x0
43 #define PLL_REGN_MASK 0x000001FE
44 #define PLL_REGN_SHIFT 0x1
45 #define PLL_SELFREQDCO_MASK 0x0000000E
46 #define PLL_SELFREQDCO_SHIFT 0x1
47 #define PLL_SD_MASK 0x0003FC00
48 #define PLL_SD_SHIFT 10
49 #define SET_PLL_GO 0x1
50 #define PLL_LDOPWDN BIT(15)
51 #define PLL_TICOPWDN BIT(16)
55 #define OMAP_CTRL_DEV_PHY_PD BIT(0)
56 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK 0x003FC000
57 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT 0xE
59 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK 0xFFC00000
60 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT 0x16
62 #define OMAP_CTRL_USB3_PHY_TX_RX_POWERON 0x3
63 #define OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF 0x0
65 #define OMAP_CTRL_USB2_PHY_PD BIT(28)
67 #define AM437X_CTRL_USB2_PHY_PD BIT(0)
68 #define AM437X_CTRL_USB2_OTG_PD BIT(1)
69 #define AM437X_CTRL_USB2_OTGVDET_EN BIT(19)
70 #define AM437X_CTRL_USB2_OTGSESSEND_EN BIT(20)
72 static LIST_HEAD(ti_usb_phy_list);
73 typedef unsigned int u32;
75 struct usb3_dpll_params {
83 struct usb3_dpll_map {
85 struct usb3_dpll_params params;
86 struct usb3_dpll_map *dpll_map;
90 void __iomem *pll_ctrl_base;
91 void __iomem *usb2_phy_power;
92 void __iomem *usb3_phy_power;
93 struct usb3_dpll_map *dpll_map;
94 struct list_head list;
98 static struct usb3_dpll_map dpll_map_usb[] = {
99 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
100 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
101 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
102 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
103 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
104 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
105 { }, /* Terminator */
108 static inline unsigned int ti_usb3_readl(void __iomem *base, u32 offset)
110 return readl(base + offset);
113 static inline void ti_usb3_writel(void __iomem *base, u32 offset, u32 value)
115 writel(value, base + offset);
118 #ifndef CONFIG_AM43XX
119 static struct usb3_dpll_params *ti_usb3_get_dpll_params(struct ti_usb_phy *phy)
122 struct usb3_dpll_map *dpll_map = phy->dpll_map;
124 rate = get_sys_clk_freq();
126 for (; dpll_map->rate; dpll_map++) {
127 if (rate == dpll_map->rate)
128 return &dpll_map->params;
131 dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
136 static int ti_usb3_dpll_wait_lock(struct ti_usb_phy *phy)
140 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_STATUS);
148 static int ti_usb3_dpll_program(struct ti_usb_phy *phy)
151 struct usb3_dpll_params *dpll_params;
153 if (!phy->pll_ctrl_base)
156 dpll_params = ti_usb3_get_dpll_params(phy);
160 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
161 val &= ~PLL_REGN_MASK;
162 val |= dpll_params->n << PLL_REGN_SHIFT;
163 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
165 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
166 val &= ~PLL_SELFREQDCO_MASK;
167 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
168 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
170 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
171 val &= ~PLL_REGM_MASK;
172 val |= dpll_params->m << PLL_REGM_SHIFT;
173 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
175 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
176 val &= ~PLL_REGM_F_MASK;
177 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
178 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
180 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
182 val |= dpll_params->sd << PLL_SD_SHIFT;
183 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
185 ti_usb3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
187 return ti_usb3_dpll_wait_lock(phy);
191 void ti_usb2_phy_power(struct ti_usb_phy *phy, int on)
195 val = readl(phy->usb2_phy_power);
198 #if defined(CONFIG_DRA7XX)
200 val &= ~OMAP_CTRL_USB2_PHY_PD;
202 val &= ~OMAP_CTRL_DEV_PHY_PD;
203 #elif defined(CONFIG_AM43XX)
204 val &= ~(AM437X_CTRL_USB2_PHY_PD |
205 AM437X_CTRL_USB2_OTG_PD);
206 val |= (AM437X_CTRL_USB2_OTGVDET_EN |
207 AM437X_CTRL_USB2_OTGSESSEND_EN);
210 #if defined(CONFIG_DRA7XX)
212 val |= OMAP_CTRL_USB2_PHY_PD;
214 val |= OMAP_CTRL_DEV_PHY_PD;
216 #elif defined(CONFIG_AM43XX)
217 val &= ~(AM437X_CTRL_USB2_OTGVDET_EN |
218 AM437X_CTRL_USB2_OTGSESSEND_EN);
219 val |= (AM437X_CTRL_USB2_PHY_PD |
220 AM437X_CTRL_USB2_OTG_PD);
223 writel(val, phy->usb2_phy_power);
226 #ifndef CONFIG_AM43XX
227 void ti_usb3_phy_power(struct ti_usb_phy *phy, int on)
231 rate = get_sys_clk_freq();
234 if (!phy->usb3_phy_power)
237 val = readl(phy->usb3_phy_power);
239 val &= ~(OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK |
240 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK);
241 val |= (OMAP_CTRL_USB3_PHY_TX_RX_POWERON) <<
242 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
244 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT;
246 val &= ~OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK;
247 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
248 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
250 writel(val, phy->usb3_phy_power);
255 * ti_usb_phy_uboot_init - usb phy uboot initialization code
256 * @dev: struct ti_usb_phy_device containing initialization data
258 * Entry point for ti usb phy driver. This driver handles initialization
259 * of both usb2 phy and usb3 phy. Pointer to ti_usb_phy_device should be
260 * passed containing base address and other initialization data.
261 * Returns '0' on success and a negative value on failure.
263 * Generally called from board_usb_init() implemented in board file.
265 int ti_usb_phy_uboot_init(struct ti_usb_phy_device *dev)
267 struct ti_usb_phy *phy;
269 phy = devm_kzalloc(NULL, sizeof(*phy), GFP_KERNEL);
271 dev_err(NULL, "unable to alloc mem for TI USB3 PHY\n");
275 phy->dpll_map = dpll_map_usb;
276 phy->index = dev->index;
277 phy->pll_ctrl_base = dev->pll_ctrl_base;
278 phy->usb2_phy_power = dev->usb2_phy_power;
279 phy->usb3_phy_power = dev->usb3_phy_power;
281 #ifndef CONFIG_AM43XX
282 ti_usb3_dpll_program(phy);
283 ti_usb3_phy_power(phy, 1);
285 ti_usb2_phy_power(phy, 1);
287 list_add_tail(&phy->list, &ti_usb_phy_list);
293 * ti_usb_phy_uboot_exit - usb phy uboot cleanup code
294 * @index: index of this controller
296 * Performs cleanup of memory allocated in ti_usb_phy_uboot_init.
297 * index of _this_ controller should be passed and should match with
298 * the index passed in ti_usb_phy_device during init.
300 * Generally called from board file.
302 void ti_usb_phy_uboot_exit(int index)
304 struct ti_usb_phy *phy = NULL;
306 list_for_each_entry(phy, &ti_usb_phy_list, list) {
307 if (phy->index != index)
310 ti_usb2_phy_power(phy, 0);
311 #ifndef CONFIG_AM43XX
312 ti_usb3_phy_power(phy, 0);
314 list_del(&phy->list);