blob: b7ca5d423a9fb014de2caf6e45b4dc1839d34b0d [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 Goedeaadd97f2015-06-14 17:29:53 +020047 int gpio_id_det;
Hans de Goedea1441982015-01-07 15:08:43 +010048 int id;
Hans de Goedee8204022015-04-27 16:57:54 +020049 int init_count;
50 int power_on_count;
Hans de Goede86979092015-04-27 14:54:47 +020051} sunxi_usb_phy[] = {
Hans de Goedea1441982015-01-07 15:08:43 +010052 {
Hans de Goedee7b852a2015-01-07 15:26:06 +010053 .usb_rst_mask = CCM_USB_CTRL_PHY0_RST | CCM_USB_CTRL_PHY0_CLK,
Hans de Goedee7b852a2015-01-07 15:26:06 +010054 .id = 0,
55 },
56 {
Hans de Goedea1441982015-01-07 15:08:43 +010057 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST | CCM_USB_CTRL_PHY1_CLK,
Hans de Goedea1441982015-01-07 15:08:43 +010058 .id = 1,
59 },
Hans de Goede1168e092015-04-27 16:50:04 +020060#if CONFIG_SUNXI_USB_PHYS >= 3
Hans de Goedea1441982015-01-07 15:08:43 +010061 {
62 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST | CCM_USB_CTRL_PHY2_CLK,
Hans de Goedea1441982015-01-07 15:08:43 +010063 .id = 2,
64 }
65#endif
66};
67
Hans de Goedea1441982015-01-07 15:08:43 +010068static int get_vbus_gpio(int index)
69{
70 switch (index) {
Hans de Goedee7b852a2015-01-07 15:26:06 +010071 case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_PIN);
Hans de Goedea1441982015-01-07 15:08:43 +010072 case 1: return sunxi_name_to_gpio(CONFIG_USB1_VBUS_PIN);
73 case 2: return sunxi_name_to_gpio(CONFIG_USB2_VBUS_PIN);
74 }
Hans de Goede494f5d02015-04-22 17:39:59 +020075 return -EINVAL;
Hans de Goedea1441982015-01-07 15:08:43 +010076}
77
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +010078static int get_vbus_detect_gpio(int index)
79{
80 switch (index) {
81 case 0: return sunxi_name_to_gpio(CONFIG_USB0_VBUS_DET);
82 }
Hans de Goede494f5d02015-04-22 17:39:59 +020083 return -EINVAL;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +010084}
85
Hans de Goedeaadd97f2015-06-14 17:29:53 +020086static int get_id_detect_gpio(int index)
87{
88 switch (index) {
89 case 0: return sunxi_name_to_gpio(CONFIG_USB0_ID_DET);
90 }
91 return -EINVAL;
92}
93
Hans de Goede86979092015-04-27 14:54:47 +020094static void usb_phy_write(struct sunxi_usb_phy *phy, int addr,
Hans de Goedea1441982015-01-07 15:08:43 +010095 int data, int len)
96{
97 int j = 0, usbc_bit = 0;
Hans de Goede1f4e1d12015-04-27 14:36:23 +020098 void *dest = (void *)SUNXI_USB0_BASE + SUNXI_USB_CSR;
Hans de Goedea1441982015-01-07 15:08:43 +010099
Vishnu Patekar3702f142015-03-01 23:47:48 +0530100#ifdef CONFIG_MACH_SUN8I_A33
101 /* CSR needs to be explicitly initialized to 0 on A33 */
102 writel(0, dest);
103#endif
104
Hans de Goede86979092015-04-27 14:54:47 +0200105 usbc_bit = 1 << (phy->id * 2);
Hans de Goedea1441982015-01-07 15:08:43 +0100106 for (j = 0; j < len; j++) {
107 /* set the bit address to be written */
108 clrbits_le32(dest, 0xff << 8);
109 setbits_le32(dest, (addr + j) << 8);
110
111 clrbits_le32(dest, usbc_bit);
112 /* set data bit */
113 if (data & 0x1)
114 setbits_le32(dest, 1 << 7);
115 else
116 clrbits_le32(dest, 1 << 7);
117
118 setbits_le32(dest, usbc_bit);
119
120 clrbits_le32(dest, usbc_bit);
121
122 data >>= 1;
123 }
124}
125
Hans de Goede86979092015-04-27 14:54:47 +0200126static void sunxi_usb_phy_config(struct sunxi_usb_phy *phy)
Hans de Goedea1441982015-01-07 15:08:43 +0100127{
128 /* The following comments are machine
129 * translated from Chinese, you have been warned!
130 */
131
Hans de Goedee7b852a2015-01-07 15:26:06 +0100132 /* Regulation 45 ohms */
Hans de Goede86979092015-04-27 14:54:47 +0200133 if (phy->id == 0)
134 usb_phy_write(phy, 0x0c, 0x01, 1);
Hans de Goedee7b852a2015-01-07 15:26:06 +0100135
Hans de Goedea1441982015-01-07 15:08:43 +0100136 /* adjust PHY's magnitude and rate */
Hans de Goede86979092015-04-27 14:54:47 +0200137 usb_phy_write(phy, 0x20, 0x14, 5);
Hans de Goedea1441982015-01-07 15:08:43 +0100138
139 /* threshold adjustment disconnect */
Hans de Goede9395a0e2015-05-31 19:26:54 +0200140#if defined CONFIG_MACH_SUN5I || defined CONFIG_MACH_SUN7I
Hans de Goede86979092015-04-27 14:54:47 +0200141 usb_phy_write(phy, 0x2a, 2, 2);
Hans de Goede9395a0e2015-05-31 19:26:54 +0200142#else
143 usb_phy_write(phy, 0x2a, 3, 2);
Hans de Goedea1441982015-01-07 15:08:43 +0100144#endif
145
146 return;
147}
148
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200149static void sunxi_usb_phy_passby(int index, int enable)
Hans de Goedea1441982015-01-07 15:08:43 +0100150{
151 unsigned long bits = 0;
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200152 void *addr;
153
154 if (index == 1)
155 addr = (void *)SUNXI_USB1_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
156 else
157 addr = (void *)SUNXI_USB2_BASE + SUNXI_USB_PMU_IRQ_ENABLE;
Hans de Goedea1441982015-01-07 15:08:43 +0100158
159 bits = SUNXI_EHCI_AHB_ICHR8_EN |
160 SUNXI_EHCI_AHB_INCR4_BURST_EN |
161 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
162 SUNXI_EHCI_ULPI_BYPASS_EN;
163
164 if (enable)
165 setbits_le32(addr, bits);
166 else
167 clrbits_le32(addr, bits);
168
169 return;
170}
171
Hans de Goede86979092015-04-27 14:54:47 +0200172void sunxi_usb_phy_enable_squelch_detect(int index, int enable)
Hans de Goedeab721ad2015-03-27 20:54:25 +0100173{
Hans de Goede86979092015-04-27 14:54:47 +0200174 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedeab721ad2015-03-27 20:54:25 +0100175
Hans de Goede86979092015-04-27 14:54:47 +0200176 usb_phy_write(phy, 0x3c, enable ? 0 : 2, 2);
Hans de Goedeab721ad2015-03-27 20:54:25 +0100177}
178
Hans de Goede86979092015-04-27 14:54:47 +0200179void sunxi_usb_phy_init(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100180{
Hans de Goede86979092015-04-27 14:54:47 +0200181 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100182 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
183
Hans de Goedee8204022015-04-27 16:57:54 +0200184 phy->init_count++;
185 if (phy->init_count != 1)
186 return;
187
Hans de Goede86979092015-04-27 14:54:47 +0200188 setbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
Hans de Goedea1441982015-01-07 15:08:43 +0100189
Hans de Goede86979092015-04-27 14:54:47 +0200190 sunxi_usb_phy_config(phy);
Hans de Goedea1441982015-01-07 15:08:43 +0100191
Hans de Goede86979092015-04-27 14:54:47 +0200192 if (phy->id != 0)
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200193 sunxi_usb_phy_passby(index, SUNXI_USB_PASSBY_EN);
Hans de Goedea1441982015-01-07 15:08:43 +0100194}
195
Hans de Goede86979092015-04-27 14:54:47 +0200196void sunxi_usb_phy_exit(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100197{
Hans de Goede86979092015-04-27 14:54:47 +0200198 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100199 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
200
Hans de Goedee8204022015-04-27 16:57:54 +0200201 phy->init_count--;
202 if (phy->init_count != 0)
203 return;
204
Hans de Goede86979092015-04-27 14:54:47 +0200205 if (phy->id != 0)
Hans de Goede1f4e1d12015-04-27 14:36:23 +0200206 sunxi_usb_phy_passby(index, !SUNXI_USB_PASSBY_EN);
Hans de Goedea1441982015-01-07 15:08:43 +0100207
Hans de Goede86979092015-04-27 14:54:47 +0200208 clrbits_le32(&ccm->usb_clk_cfg, phy->usb_rst_mask);
Hans de Goedea1441982015-01-07 15:08:43 +0100209}
210
Hans de Goede86979092015-04-27 14:54:47 +0200211void sunxi_usb_phy_power_on(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100212{
Hans de Goede86979092015-04-27 14:54:47 +0200213 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100214
Hans de Goedee8204022015-04-27 16:57:54 +0200215 phy->power_on_count++;
216 if (phy->power_on_count != 1)
217 return;
218
Hans de Goede86979092015-04-27 14:54:47 +0200219 if (phy->gpio_vbus >= 0)
220 gpio_set_value(phy->gpio_vbus, 1);
Hans de Goedea1441982015-01-07 15:08:43 +0100221}
222
Hans de Goede86979092015-04-27 14:54:47 +0200223void sunxi_usb_phy_power_off(int index)
Hans de Goedea1441982015-01-07 15:08:43 +0100224{
Hans de Goede86979092015-04-27 14:54:47 +0200225 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goedea1441982015-01-07 15:08:43 +0100226
Hans de Goedee8204022015-04-27 16:57:54 +0200227 phy->power_on_count--;
228 if (phy->power_on_count != 0)
229 return;
230
Hans de Goede86979092015-04-27 14:54:47 +0200231 if (phy->gpio_vbus >= 0)
232 gpio_set_value(phy->gpio_vbus, 0);
Hans de Goedea1441982015-01-07 15:08:43 +0100233}
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100234
Hans de Goede39c119d2015-07-08 16:44:22 +0200235int sunxi_usb_phy_power_is_on(int index)
236{
237 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
238
239 return phy->power_on_count > 0;
240}
241
Hans de Goede86979092015-04-27 14:54:47 +0200242int sunxi_usb_phy_vbus_detect(int index)
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100243{
Hans de Goede86979092015-04-27 14:54:47 +0200244 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
Hans de Goede444af392015-03-27 21:46:00 +0100245 int err, retries = 3;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100246
Hans de Goede78c2e642015-06-18 18:21:33 +0200247 if (phy->gpio_vbus_det < 0)
Hans de Goede86979092015-04-27 14:54:47 +0200248 return phy->gpio_vbus_det;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100249
Hans de Goede86979092015-04-27 14:54:47 +0200250 err = gpio_get_value(phy->gpio_vbus_det);
Hans de Goede444af392015-03-27 21:46:00 +0100251 /*
252 * Vbus may have been provided by the board and just been turned of
253 * some milliseconds ago on reset, what we're measuring then is a
254 * residual charge on Vbus, sleep a bit and try again.
255 */
256 while (err > 0 && retries--) {
257 mdelay(100);
Hans de Goede86979092015-04-27 14:54:47 +0200258 err = gpio_get_value(phy->gpio_vbus_det);
Hans de Goede444af392015-03-27 21:46:00 +0100259 }
260
261 return err;
Paul Kocialkowski7e9b7de2015-03-22 18:07:12 +0100262}
Hans de Goede1168e092015-04-27 16:50:04 +0200263
Hans de Goedeaadd97f2015-06-14 17:29:53 +0200264int sunxi_usb_phy_id_detect(int index)
265{
266 struct sunxi_usb_phy *phy = &sunxi_usb_phy[index];
267
268 if (phy->gpio_id_det < 0)
269 return phy->gpio_id_det;
270
271 return gpio_get_value(phy->gpio_id_det);
272}
273
Hans de Goede1168e092015-04-27 16:50:04 +0200274int sunxi_usb_phy_probe(void)
275{
276 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
277 struct sunxi_usb_phy *phy;
278 int i, ret = 0;
279
280 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
281 phy = &sunxi_usb_phy[i];
282
283 phy->gpio_vbus = get_vbus_gpio(i);
284 if (phy->gpio_vbus >= 0) {
285 ret = gpio_request(phy->gpio_vbus, "usb_vbus");
286 if (ret)
287 return ret;
288 ret = gpio_direction_output(phy->gpio_vbus, 0);
289 if (ret)
290 return ret;
291 }
292
293 phy->gpio_vbus_det = get_vbus_detect_gpio(i);
294 if (phy->gpio_vbus_det >= 0) {
295 ret = gpio_request(phy->gpio_vbus_det, "usb_vbus_det");
296 if (ret)
297 return ret;
298 ret = gpio_direction_input(phy->gpio_vbus_det);
299 if (ret)
300 return ret;
301 }
Hans de Goedeaadd97f2015-06-14 17:29:53 +0200302
303 phy->gpio_id_det = get_id_detect_gpio(i);
304 if (phy->gpio_id_det >= 0) {
305 ret = gpio_request(phy->gpio_id_det, "usb_id_det");
306 if (ret)
307 return ret;
308 ret = gpio_direction_input(phy->gpio_id_det);
309 if (ret)
310 return ret;
311 sunxi_gpio_set_pull(phy->gpio_id_det,
312 SUNXI_GPIO_PULL_UP);
313 }
Hans de Goede1168e092015-04-27 16:50:04 +0200314 }
315
316 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
317
318 return 0;
319}
320
321int sunxi_usb_phy_remove(void)
322{
323 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
324 struct sunxi_usb_phy *phy;
325 int i;
326
327 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
328
329 for (i = 0; i < CONFIG_SUNXI_USB_PHYS; i++) {
330 phy = &sunxi_usb_phy[i];
331
332 if (phy->gpio_vbus >= 0)
333 gpio_free(phy->gpio_vbus);
334
335 if (phy->gpio_vbus_det >= 0)
336 gpio_free(phy->gpio_vbus_det);
Hans de Goedeaadd97f2015-06-14 17:29:53 +0200337
338 if (phy->gpio_id_det >= 0)
339 gpio_free(phy->gpio_id_det);
Hans de Goede1168e092015-04-27 16:50:04 +0200340 }
341
342 return 0;
343}