blob: b07d67ff3f82f18b1c57a153ff5e3e21f3675fa3 [file] [log] [blame]
Hans de Goedea1441982015-01-07 15:08:43 +01001/*
Hans de Goede86979092015-04-27 14:54:47 +02002 * Sunxi usb-phy code
Hans de Goedea1441982015-01-07 15:08:43 +01003 *
Hans de Goede86979092015-04-27 14:54:47 +02004 * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
5 * Copyright (C) 2014 Roman Byshko <rbyshko@gmail.com>
Hans de Goedea1441982015-01-07 15:08:43 +01006 *
7 * Based on code from
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
Hans de Goede7e5aabd2015-04-27 11:44:22 +020013#include <common.h>
Hans de Goedea1441982015-01-07 15:08:43 +010014#include <asm/arch/clock.h>
15#include <asm/arch/cpu.h>
Hans de Goede26a90052015-04-27 15:05:10 +020016#include <asm/arch/usb_phy.h>
Hans de Goedea1441982015-01-07 15:08:43 +010017#include <asm/gpio.h>
18#include <asm/io.h>
Hans de Goede494f5d02015-04-22 17:39:59 +020019#include <errno.h>
Hans de Goedee573b3c2015-01-11 19:33:35 +010020#ifdef CONFIG_AXP152_POWER
21#include <axp152.h>
22#endif
23#ifdef CONFIG_AXP209_POWER
24#include <axp209.h>
25#endif
26#ifdef CONFIG_AXP221_POWER
27#include <axp221.h>
28#endif
Hans de Goedea1441982015-01-07 15:08:43 +010029
30#define SUNXI_USB_PMU_IRQ_ENABLE 0x800
Vishnu Patekar3702f142015-03-01 23:47:48 +053031#ifdef CONFIG_MACH_SUN8I_A33
32#define SUNXI_USB_CSR 0x410
33#else
Hans de Goedea1441982015-01-07 15:08:43 +010034#define SUNXI_USB_CSR 0x404
Vishnu Patekar3702f142015-03-01 23:47:48 +053035#endif
Hans de Goedea1441982015-01-07 15:08:43 +010036#define SUNXI_USB_PASSBY_EN 1
37
38#define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10)
39#define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9)
40#define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8)
41#define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0)
42
Hans de Goede86979092015-04-27 14:54:47 +020043static struct sunxi_usb_phy {
Hans de Goedea1441982015-01-07 15:08:43 +010044 int usb_rst_mask;
Hans de Goedea1441982015-01-07 15:08:43 +010045 int gpio_vbus;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +010046 int gpio_vbus_det;
Hans de Goedea1441982015-01-07 15:08:43 +010047 int id;
Hans de Goedee8204022015-04-27 16:57:54 +020048 int init_count;
49 int power_on_count;
Hans de Goede86979092015-04-27 14:54:47 +020050} sunxi_usb_phy[] = {
Hans de Goedea1441982015-01-07 15:08:43 +010051 {
Hans de Goedee7b852a2015-01-07 15:26:06 +010052 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
Hans de Goedee7b852a2015-01-07 15:26:06 +010053 .id = 0,
54 },
55 {
Hans de Goedea1441982015-01-07 15:08:43 +010056 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
Hans de Goedea1441982015-01-07 15:08:43 +010057 .id = 1,
58 },
Hans de Goede1168e092015-04-27 16:50:04 +020059#if CONFIG_SUNXI_USB_PHYS >= 3
Hans de Goedea1441982015-01-07 15:08:43 +010060 {
61 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
Hans de Goedea1441982015-01-07 15:08:43 +010062 .id = 2,
63 }
64#endif
65};
66
Hans de Goedea1441982015-01-07 15:08:43 +010067static int get_vbus_gpio(int index)
68{
69 switch (index) {
Hans de Goedee7b852a2015-01-07 15:26:06 +010070 case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
Hans de Goedea1441982015-01-07 15:08:43 +010071 case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
72 case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
73 }
Hans de Goede494f5d02015-04-22 17:39:59 +020074 return -EINVAL;
Hans de Goedea1441982015-01-07 15:08:43 +010075}
76
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +010077static int get_vbus_detect_gpio(int index)
78{
79 switch (index) {
80 case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
81 }
Hans de Goede494f5d02015-04-22 17:39:59 +020082 return -EINVAL;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +010083}
84
Hans de Goede86979092015-04-27 14:54:47 +020085static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
Hans de Goedea1441982015-01-07 15:08:43 +010086 int data, int len)
87{
88 int j = 0, usbc_bit = 0;
Hans de Goede1f4e1d12015-04-27 14:36:23 +020089 void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
Hans de Goedea1441982015-01-07 15:08:43 +010090
Vishnu Patekar3702f142015-03-01 23:47:48 +053091#ifdef CONFIG_MACH_SUN8I_A33
92 /* CSR needs to be explicitly initialized to 0 on A33 */
93 writel(0, dest);
94#endif
95
Hans de Goede86979092015-04-27 14:54:47 +020096 usbc_bit = 1 << (phy->id * 2);
Hans de Goedea1441982015-01-07 15:08:43 +010097 for (j = 0; j < len; j++) {
98 /* set the bit address to be written */
99 clrbits_le32(dest, 0xff << 8);
100 setbits_le32(dest, (addr + j) << 8);
101
102 clrbits_le32(dest, usbc_bit);
103 /* set data bit */
104 if (data & 0x1)
105 setbits_le32(dest, 1 << 7);
106 else
107 clrbits_le32(dest, 1 << 7);
108
109 setbits_le32(dest, usbc_bit);
110
111 clrbits_le32(dest, usbc_bit);
112
113 data >>= 1;
114 }
115}
116
Hans de Goede86979092015-04-27 14:54:47 +0200117static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
Hans de Goedea1441982015-01-07 15:08:43 +0100118{
119 /* The following comments are machine
120 * translated from Chinese, you have been warned!
121 */
122
Hans de Goedee7b852a2015-01-07 15:26:06 +0100123 /* Regulation 45 ohms */
Hans de Goede86979092015-04-27 14:54:47 +0200124 if (phy->id == 0)
125 usb_phy_write(phy, 0x0c, 0x01, 1);
Hans de Goedee7b852a2015-01-07 15:26:06 +0100126
Hans de Goedea1441982015-01-07 15:08:43 +0100127 /* adjust PHY's magnitude and rate */
Hans de Goede86979092015-04-27 14:54:47 +0200128 usb_phy_write(phy, 0x20, 0x14, 5);
Hans de Goedea1441982015-01-07 15:08:43 +0100129
130 /* threshold adjustment disconnect */
Hans de Goede9395a0e2015-05-31 19:26:54 +0200131#if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
Hans de Goede86979092015-04-27 14:54:47 +0200132 usb_phy_write(phy, 0x2a, 2, 2);
Hans de Goede9395a0e2015-05-31 19:26:54 +0200133#else
134 usb_phy_write(phy, 0x2a, 3, 2);
Hans de Goedea1441982015-01-07 15:08:43 +0100135#endif
136
137 return;
138}
139
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200140static void sunxi_usb_phy_passby(int index, int enable)
Hans de Goedea1441982015-01-07 15:08:43 +0100141{
142 unsigned long bits = 0;
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200143 void *addr;
144
145 if (index == 1)
146 addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
147 else
148 addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
Hans de Goedea1441982015-01-07 15:08:43 +0100149
150 bits = SUNXI_EHCI_AHB_ICHR8_EN |
151 SUNXI_EHCI_AHB_INCR4_BURST_EN |
152 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
153 SUNXI_EHCI_ULPI_BYPASS_EN;
154
155 if (enable)
156 setbits_le32(addr, bits);
157 else
158 clrbits_le32(addr, bits);
159
160 return;
161}
162
Hans de Goede86979092015-04-27 14:54:47 +0200163void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
Hans de Goedeab721ad2015-03-27 20:54:25 +0100164{
Hans de Goede86979092015-04-27 14:54:47 +0200165 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedeab721ad2015-03-27 20:54:25 +0100166
Hans de Goede86979092015-04-27 14:54:47 +0200167 usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
Hans de Goedeab721ad2015-03-27 20:54:25 +0100168}
169
Hans de Goede86979092015-04-27 14:54:47 +0200170void sunxi_usb_phy_init(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100171{
Hans de Goede86979092015-04-27 14:54:47 +0200172 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100173 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
174
Hans de Goedee8204022015-04-27 16:57:54 +0200175 phy->init_count++;
176 if (phy->init_count != 1)
177 return;
178
Hans de Goede86979092015-04-27 14:54:47 +0200179 setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
Hans de Goedea1441982015-01-07 15:08:43 +0100180
Hans de Goede86979092015-04-27 14:54:47 +0200181 sunxi_usb_phy_config(phy);
Hans de Goedea1441982015-01-07 15:08:43 +0100182
Hans de Goede86979092015-04-27 14:54:47 +0200183 if (phy->id != 0)
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200184 sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
Hans de Goedea1441982015-01-07 15:08:43 +0100185}
186
Hans de Goede86979092015-04-27 14:54:47 +0200187void sunxi_usb_phy_exit(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100188{
Hans de Goede86979092015-04-27 14:54:47 +0200189 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100190 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
191
Hans de Goedee8204022015-04-27 16:57:54 +0200192 phy->init_count--;
193 if (phy->init_count != 0)
194 return;
195
Hans de Goede86979092015-04-27 14:54:47 +0200196 if (phy->id != 0)
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200197 sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
Hans de Goedea1441982015-01-07 15:08:43 +0100198
Hans de Goede86979092015-04-27 14:54:47 +0200199 clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
Hans de Goedea1441982015-01-07 15:08:43 +0100200}
201
Hans de Goede86979092015-04-27 14:54:47 +0200202void sunxi_usb_phy_power_on(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100203{
Hans de Goede86979092015-04-27 14:54:47 +0200204 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100205
Hans de Goedee8204022015-04-27 16:57:54 +0200206 phy->power_on_count++;
207 if (phy->power_on_count != 1)
208 return;
209
Hans de Goede86979092015-04-27 14:54:47 +0200210 if (phy->gpio_vbus >= 0)
211 gpio_set_value(phy->gpio_vbus, 1);
Hans de Goedea1441982015-01-07 15:08:43 +0100212}
213
Hans de Goede86979092015-04-27 14:54:47 +0200214void sunxi_usb_phy_power_off(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100215{
Hans de Goede86979092015-04-27 14:54:47 +0200216 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100217
Hans de Goedee8204022015-04-27 16:57:54 +0200218 phy->power_on_count--;
219 if (phy->power_on_count != 0)
220 return;
221
Hans de Goede86979092015-04-27 14:54:47 +0200222 if (phy->gpio_vbus >= 0)
223 gpio_set_value(phy->gpio_vbus, 0);
Hans de Goedea1441982015-01-07 15:08:43 +0100224}
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100225
Hans de Goede86979092015-04-27 14:54:47 +0200226int sunxi_usb_phy_vbus_detect(int index)
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100227{
Hans de Goede86979092015-04-27 14:54:47 +0200228 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goede444af392015-03-27 21:46:00 +0100229 int err, retries = 3;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100230
Hans de Goede86979092015-04-27 14:54:47 +0200231 if (phy->gpio_vbus_det < 0) {
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100232 eprintf("Error: invalid vbus detection pin\n");
Hans de Goede86979092015-04-27 14:54:47 +0200233 return phy->gpio_vbus_det;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100234 }
235
Hans de Goede86979092015-04-27 14:54:47 +0200236 err = gpio_get_value(phy->gpio_vbus_det);
Hans de Goede444af392015-03-27 21:46:00 +0100237 /*
238 * Vbus may have been provided by the board and just been turned of
239 * some milliseconds ago on reset, what we're measuring then is a
240 * residual charge on Vbus, sleep a bit and try again.
241 */
242 while (err > 0 && retries--) {
243 mdelay(100);
Hans de Goede86979092015-04-27 14:54:47 +0200244 err = gpio_get_value(phy->gpio_vbus_det);
Hans de Goede444af392015-03-27 21:46:00 +0100245 }
246
247 return err;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100248}
Hans de Goede1168e092015-04-27 16:50:04 +0200249
250int sunxi_usb_phy_probe(void)
251{
252 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
253 struct sunxi_usb_phy *phy;
254 int i, ret = 0;
255
256 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
257 phy = &sunxi_usb_phy[i];
258
259 phy->gpio_vbus = get_vbus_gpio(i);
260 if (phy->gpio_vbus >= 0) {
261 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
262 if (ret)
263 return ret;
264 ret = gpio_direction_output(phy->gpio_vbus, 0);
265 if (ret)
266 return ret;
267 }
268
269 phy->gpio_vbus_det = get_vbus_detect_gpio(i);
270 if (phy->gpio_vbus_det >= 0) {
271 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
272 if (ret)
273 return ret;
274 ret = gpio_direction_input(phy->gpio_vbus_det);
275 if (ret)
276 return ret;
277 }
278 }
279
280 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
281
282 return 0;
283}
284
285int sunxi_usb_phy_remove(void)
286{
287 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
288 struct sunxi_usb_phy *phy;
289 int i;
290
291 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
292
293 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
294 phy = &sunxi_usb_phy[i];
295
296 if (phy->gpio_vbus >= 0)
297 gpio_free(phy->gpio_vbus);
298
299 if (phy->gpio_vbus_det >= 0)
300 gpio_free(phy->gpio_vbus_det);
301 }
302
303 return 0;
304}