1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Allwinner sun9i USB phy driver
5 * Copyright (C) 2014-2015 Chen-Yu Tsai <wens@csie.org>
7 * Based on phy-sun4i-usb.c from
8 * Hans de Goede <hdegoede@redhat.com>
11 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
14 #include <linux/clk.h>
15 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/phy/phy.h>
19 #include <linux/usb/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
23 #define SUNXI_AHB_INCR16_BURST_EN BIT(11)
24 #define SUNXI_AHB_INCR8_BURST_EN BIT(10)
25 #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
26 #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
27 #define SUNXI_ULPI_BYPASS_EN BIT(0)
29 /* usb1 HSIC specific bits */
30 #define SUNXI_EHCI_HS_FORCE BIT(20)
31 #define SUNXI_HSIC_CONNECT_DET BIT(17)
32 #define SUNXI_HSIC_CONNECT_INT BIT(16)
33 #define SUNXI_HSIC BIT(1)
35 struct sun9i_usb_phy
{
38 struct reset_control
*reset
;
41 enum usb_phy_interface type
;
44 static void sun9i_usb_phy_passby(struct sun9i_usb_phy
*phy
, int enable
)
48 bits
= SUNXI_AHB_INCR16_BURST_EN
| SUNXI_AHB_INCR8_BURST_EN
|
49 SUNXI_AHB_INCR4_BURST_EN
| SUNXI_AHB_INCRX_ALIGN_EN
|
52 if (phy
->type
== USBPHY_INTERFACE_MODE_HSIC
)
53 bits
|= SUNXI_HSIC
| SUNXI_EHCI_HS_FORCE
|
54 SUNXI_HSIC_CONNECT_DET
| SUNXI_HSIC_CONNECT_INT
;
56 reg_value
= readl(phy
->pmu
);
63 writel(reg_value
, phy
->pmu
);
66 static int sun9i_usb_phy_init(struct phy
*_phy
)
68 struct sun9i_usb_phy
*phy
= phy_get_drvdata(_phy
);
71 ret
= clk_prepare_enable(phy
->clk
);
75 ret
= clk_prepare_enable(phy
->hsic_clk
);
79 ret
= reset_control_deassert(phy
->reset
);
83 sun9i_usb_phy_passby(phy
, 1);
87 clk_disable_unprepare(phy
->hsic_clk
);
90 clk_disable_unprepare(phy
->clk
);
96 static int sun9i_usb_phy_exit(struct phy
*_phy
)
98 struct sun9i_usb_phy
*phy
= phy_get_drvdata(_phy
);
100 sun9i_usb_phy_passby(phy
, 0);
101 reset_control_assert(phy
->reset
);
102 clk_disable_unprepare(phy
->hsic_clk
);
103 clk_disable_unprepare(phy
->clk
);
108 static const struct phy_ops sun9i_usb_phy_ops
= {
109 .init
= sun9i_usb_phy_init
,
110 .exit
= sun9i_usb_phy_exit
,
111 .owner
= THIS_MODULE
,
114 static int sun9i_usb_phy_probe(struct platform_device
*pdev
)
116 struct sun9i_usb_phy
*phy
;
117 struct device
*dev
= &pdev
->dev
;
118 struct device_node
*np
= dev
->of_node
;
119 struct phy_provider
*phy_provider
;
120 struct resource
*res
;
122 phy
= devm_kzalloc(dev
, sizeof(*phy
), GFP_KERNEL
);
126 phy
->type
= of_usb_get_phy_mode(np
);
127 if (phy
->type
== USBPHY_INTERFACE_MODE_HSIC
) {
128 phy
->clk
= devm_clk_get(dev
, "hsic_480M");
129 if (IS_ERR(phy
->clk
)) {
130 dev_err(dev
, "failed to get hsic_480M clock\n");
131 return PTR_ERR(phy
->clk
);
134 phy
->hsic_clk
= devm_clk_get(dev
, "hsic_12M");
135 if (IS_ERR(phy
->hsic_clk
)) {
136 dev_err(dev
, "failed to get hsic_12M clock\n");
137 return PTR_ERR(phy
->hsic_clk
);
140 phy
->reset
= devm_reset_control_get(dev
, "hsic");
141 if (IS_ERR(phy
->reset
)) {
142 dev_err(dev
, "failed to get reset control\n");
143 return PTR_ERR(phy
->reset
);
146 phy
->clk
= devm_clk_get(dev
, "phy");
147 if (IS_ERR(phy
->clk
)) {
148 dev_err(dev
, "failed to get phy clock\n");
149 return PTR_ERR(phy
->clk
);
152 phy
->reset
= devm_reset_control_get(dev
, "phy");
153 if (IS_ERR(phy
->reset
)) {
154 dev_err(dev
, "failed to get reset control\n");
155 return PTR_ERR(phy
->reset
);
159 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
160 phy
->pmu
= devm_ioremap_resource(dev
, res
);
161 if (IS_ERR(phy
->pmu
))
162 return PTR_ERR(phy
->pmu
);
164 phy
->phy
= devm_phy_create(dev
, NULL
, &sun9i_usb_phy_ops
);
165 if (IS_ERR(phy
->phy
)) {
166 dev_err(dev
, "failed to create PHY\n");
167 return PTR_ERR(phy
->phy
);
170 phy_set_drvdata(phy
->phy
, phy
);
171 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
173 return PTR_ERR_OR_ZERO(phy_provider
);
176 static const struct of_device_id sun9i_usb_phy_of_match
[] = {
177 { .compatible
= "allwinner,sun9i-a80-usb-phy" },
180 MODULE_DEVICE_TABLE(of
, sun9i_usb_phy_of_match
);
182 static struct platform_driver sun9i_usb_phy_driver
= {
183 .probe
= sun9i_usb_phy_probe
,
185 .of_match_table
= sun9i_usb_phy_of_match
,
186 .name
= "sun9i-usb-phy",
189 module_platform_driver(sun9i_usb_phy_driver
);
191 MODULE_DESCRIPTION("Allwinner sun9i USB phy driver");
192 MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
193 MODULE_LICENSE("GPL");