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 <linux/ioport.h>
24 #include <asm/arch/sys_proto.h>
27 #include "linux-compat.h"
29 #define PLL_STATUS 0x00000004
30 #define PLL_GO 0x00000008
31 #define PLL_CONFIGURATION1 0x0000000C
32 #define PLL_CONFIGURATION2 0x00000010
33 #define PLL_CONFIGURATION3 0x00000014
34 #define PLL_CONFIGURATION4 0x00000020
36 #define PLL_REGM_MASK 0x001FFE00
37 #define PLL_REGM_SHIFT 0x9
38 #define PLL_REGM_F_MASK 0x0003FFFF
39 #define PLL_REGM_F_SHIFT 0x0
40 #define PLL_REGN_MASK 0x000001FE
41 #define PLL_REGN_SHIFT 0x1
42 #define PLL_SELFREQDCO_MASK 0x0000000E
43 #define PLL_SELFREQDCO_SHIFT 0x1
44 #define PLL_SD_MASK 0x0003FC00
45 #define PLL_SD_SHIFT 10
46 #define SET_PLL_GO 0x1
47 #define PLL_LDOPWDN BIT(15)
48 #define PLL_TICOPWDN BIT(16)
52 #define OMAP_CTRL_DEV_PHY_PD BIT(0)
53 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK 0x003FC000
54 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT 0xE
56 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK 0xFFC00000
57 #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT 0x16
59 #define OMAP_CTRL_USB3_PHY_TX_RX_POWERON 0x3
60 #define OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF 0x0
62 #define OMAP_CTRL_USB2_PHY_PD BIT(28)
64 #define AM437X_CTRL_USB2_PHY_PD BIT(0)
65 #define AM437X_CTRL_USB2_OTG_PD BIT(1)
66 #define AM437X_CTRL_USB2_OTGVDET_EN BIT(19)
67 #define AM437X_CTRL_USB2_OTGSESSEND_EN BIT(20)
69 static LIST_HEAD(ti_usb_phy_list);
70 typedef unsigned int u32;
72 struct usb3_dpll_params {
80 struct usb3_dpll_map {
82 struct usb3_dpll_params params;
83 struct usb3_dpll_map *dpll_map;
87 void __iomem *pll_ctrl_base;
88 void __iomem *usb2_phy_power;
89 void __iomem *usb3_phy_power;
90 struct usb3_dpll_map *dpll_map;
91 struct list_head list;
95 static struct usb3_dpll_map dpll_map_usb[] = {
96 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
97 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
98 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
99 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
100 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
101 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
102 { }, /* Terminator */
105 static inline unsigned int ti_usb3_readl(void __iomem *base, u32 offset)
107 return readl(base + offset);
110 static inline void ti_usb3_writel(void __iomem *base, u32 offset, u32 value)
112 writel(value, base + offset);
115 #ifndef CONFIG_AM43XX
116 static struct usb3_dpll_params *ti_usb3_get_dpll_params(struct ti_usb_phy *phy)
119 struct usb3_dpll_map *dpll_map = phy->dpll_map;
121 rate = get_sys_clk_freq();
123 for (; dpll_map->rate; dpll_map++) {
124 if (rate == dpll_map->rate)
125 return &dpll_map->params;
128 dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
133 static int ti_usb3_dpll_wait_lock(struct ti_usb_phy *phy)
137 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_STATUS);
145 static int ti_usb3_dpll_program(struct ti_usb_phy *phy)
148 struct usb3_dpll_params *dpll_params;
150 if (!phy->pll_ctrl_base)
153 dpll_params = ti_usb3_get_dpll_params(phy);
157 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
158 val &= ~PLL_REGN_MASK;
159 val |= dpll_params->n << PLL_REGN_SHIFT;
160 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
162 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
163 val &= ~PLL_SELFREQDCO_MASK;
164 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
165 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
167 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
168 val &= ~PLL_REGM_MASK;
169 val |= dpll_params->m << PLL_REGM_SHIFT;
170 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
172 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
173 val &= ~PLL_REGM_F_MASK;
174 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
175 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
177 val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
179 val |= dpll_params->sd << PLL_SD_SHIFT;
180 ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
182 ti_usb3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
184 return ti_usb3_dpll_wait_lock(phy);
188 void ti_usb2_phy_power(struct ti_usb_phy *phy, int on)
192 val = readl(phy->usb2_phy_power);
195 #if defined(CONFIG_DRA7XX)
197 val &= ~OMAP_CTRL_USB2_PHY_PD;
199 val &= ~OMAP_CTRL_DEV_PHY_PD;
200 #elif defined(CONFIG_AM43XX)
201 val &= ~(AM437X_CTRL_USB2_PHY_PD |
202 AM437X_CTRL_USB2_OTG_PD);
203 val |= (AM437X_CTRL_USB2_OTGVDET_EN |
204 AM437X_CTRL_USB2_OTGSESSEND_EN);
207 #if defined(CONFIG_DRA7XX)
209 val |= OMAP_CTRL_USB2_PHY_PD;
211 val |= OMAP_CTRL_DEV_PHY_PD;
213 #elif defined(CONFIG_AM43XX)
214 val &= ~(AM437X_CTRL_USB2_OTGVDET_EN |
215 AM437X_CTRL_USB2_OTGSESSEND_EN);
216 val |= (AM437X_CTRL_USB2_PHY_PD |
217 AM437X_CTRL_USB2_OTG_PD);
220 writel(val, phy->usb2_phy_power);
223 #ifndef CONFIG_AM43XX
224 void ti_usb3_phy_power(struct ti_usb_phy *phy, int on)
228 rate = get_sys_clk_freq();
231 if (!phy->usb3_phy_power)
234 val = readl(phy->usb3_phy_power);
236 val &= ~(OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK |
237 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK);
238 val |= (OMAP_CTRL_USB3_PHY_TX_RX_POWERON) <<
239 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
241 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT;
243 val &= ~OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK;
244 val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
245 OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
247 writel(val, phy->usb3_phy_power);
252 * ti_usb_phy_uboot_init - usb phy uboot initialization code
253 * @dev: struct ti_usb_phy_device containing initialization data
255 * Entry point for ti usb phy driver. This driver handles initialization
256 * of both usb2 phy and usb3 phy. Pointer to ti_usb_phy_device should be
257 * passed containing base address and other initialization data.
258 * Returns '0' on success and a negative value on failure.
260 * Generally called from board_usb_init() implemented in board file.
262 int ti_usb_phy_uboot_init(struct ti_usb_phy_device *dev)
264 struct ti_usb_phy *phy;
266 phy = devm_kzalloc(NULL, sizeof(*phy), GFP_KERNEL);
268 dev_err(NULL, "unable to alloc mem for TI USB3 PHY\n");
272 phy->dpll_map = dpll_map_usb;
273 phy->index = dev->index;
274 phy->pll_ctrl_base = dev->pll_ctrl_base;
275 phy->usb2_phy_power = dev->usb2_phy_power;
276 phy->usb3_phy_power = dev->usb3_phy_power;
278 #ifndef CONFIG_AM43XX
279 ti_usb3_dpll_program(phy);
280 ti_usb3_phy_power(phy, 1);
282 ti_usb2_phy_power(phy, 1);
284 list_add_tail(&phy->list, &ti_usb_phy_list);
290 * ti_usb_phy_uboot_exit - usb phy uboot cleanup code
291 * @index: index of this controller
293 * Performs cleanup of memory allocated in ti_usb_phy_uboot_init.
294 * index of _this_ controller should be passed and should match with
295 * the index passed in ti_usb_phy_device during init.
297 * Generally called from board file.
299 void ti_usb_phy_uboot_exit(int index)
301 struct ti_usb_phy *phy = NULL;
303 list_for_each_entry(phy, &ti_usb_phy_list, list) {
304 if (phy->index != index)
307 ti_usb2_phy_power(phy, 0);
308 #ifndef CONFIG_AM43XX
309 ti_usb3_phy_power(phy, 0);
311 list_del(&phy->list);