2 * Copyright (C) 2014 Roman Byshko
4 * Roman Byshko <rbyshko@gmail.com>
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/arch/clock.h>
18 #define SUNXI_USB1_IO_BASE 0x01c14000
19 #define SUNXI_USB2_IO_BASE 0x01c1c000
21 #define SUNXI_USB_PMU_IRQ_ENABLE 0x800
22 #define SUNXI_USB_CSR 0x01c13404
23 #define SUNXI_USB_PASSBY_EN 1
25 #define SUNXI_EHCI_AHB_ICHR8_EN (1 << 10)
26 #define SUNXI_EHCI_AHB_INCR4_BURST_EN (1 << 9)
27 #define SUNXI_EHCI_AHB_INCRX_ALIGN_EN (1 << 8)
28 #define SUNXI_EHCI_ULPI_BYPASS_EN (1 << 0)
30 static struct sunxi_ehci_hcd {
38 } sunxi_echi_hcd[] = {
40 .usb_rst_mask = CCM_USB_CTRL_PHY1_RST,
41 .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI0,
42 .gpio_vbus = CONFIG_SUNXI_USB_VBUS0_GPIO,
43 .csr = (void *)SUNXI_USB_CSR,
47 #if (CONFIG_USB_MAX_CONTROLLER_COUNT > 1)
49 .usb_rst_mask = CCM_USB_CTRL_PHY2_RST,
50 .ahb_clk_mask = 1 << AHB_GATE_OFFSET_USB_EHCI1,
51 .gpio_vbus = CONFIG_SUNXI_USB_VBUS1_GPIO,
52 .csr = (void *)SUNXI_USB_CSR,
59 static int enabled_hcd_count;
61 static void *get_io_base(int hcd_id)
64 return (void *)SUNXI_USB1_IO_BASE;
66 return (void *)SUNXI_USB2_IO_BASE;
71 static void usb_phy_write(struct sunxi_ehci_hcd *sunxi_ehci, int addr,
74 int j = 0, usbc_bit = 0;
75 void *dest = sunxi_ehci->csr;
77 usbc_bit = 1 << (sunxi_ehci->id * 2);
78 for (j = 0; j < len; j++) {
79 /* set the bit address to be written */
80 clrbits_le32(dest, 0xff << 8);
81 setbits_le32(dest, (addr + j) << 8);
83 clrbits_le32(dest, usbc_bit);
86 setbits_le32(dest, 1 << 7);
88 clrbits_le32(dest, 1 << 7);
90 setbits_le32(dest, usbc_bit);
92 clrbits_le32(dest, usbc_bit);
98 static void sunxi_usb_phy_init(struct sunxi_ehci_hcd *sunxi_ehci)
100 /* The following comments are machine
101 * translated from Chinese, you have been warned!
104 /* adjust PHY's magnitude and rate */
105 usb_phy_write(sunxi_ehci, 0x20, 0x14, 5);
107 /* threshold adjustment disconnect */
108 #ifdef CONFIG_MACH_SUN4I
109 usb_phy_write(sunxi_ehci, 0x2a, 3, 2);
111 usb_phy_write(sunxi_ehci, 0x2a, 2, 2);
117 static void sunxi_usb_passby(struct sunxi_ehci_hcd *sunxi_ehci, int enable)
119 unsigned long bits = 0;
120 void *addr = get_io_base(sunxi_ehci->id) + SUNXI_USB_PMU_IRQ_ENABLE;
122 bits = SUNXI_EHCI_AHB_ICHR8_EN |
123 SUNXI_EHCI_AHB_INCR4_BURST_EN |
124 SUNXI_EHCI_AHB_INCRX_ALIGN_EN |
125 SUNXI_EHCI_ULPI_BYPASS_EN;
128 setbits_le32(addr, bits);
130 clrbits_le32(addr, bits);
135 static void sunxi_ehci_enable(struct sunxi_ehci_hcd *sunxi_ehci)
137 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
139 setbits_le32(&ccm->usb_clk_cfg, sunxi_ehci->usb_rst_mask);
140 setbits_le32(&ccm->ahb_gate0, sunxi_ehci->ahb_clk_mask);
142 sunxi_usb_phy_init(sunxi_ehci);
144 sunxi_usb_passby(sunxi_ehci, SUNXI_USB_PASSBY_EN);
146 gpio_direction_output(sunxi_ehci->gpio_vbus, 1);
149 static void sunxi_ehci_disable(struct sunxi_ehci_hcd *sunxi_ehci)
151 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
153 gpio_direction_output(sunxi_ehci->gpio_vbus, 0);
155 sunxi_usb_passby(sunxi_ehci, !SUNXI_USB_PASSBY_EN);
157 clrbits_le32(&ccm->ahb_gate0, sunxi_ehci->ahb_clk_mask);
158 clrbits_le32(&ccm->usb_clk_cfg, sunxi_ehci->usb_rst_mask);
161 int ehci_hcd_init(int index, enum usb_init_type init, struct ehci_hccr **hccr,
162 struct ehci_hcor **hcor)
164 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
165 struct sunxi_ehci_hcd *sunxi_ehci = &sunxi_echi_hcd[index];
168 /* enable common PHY only once */
170 setbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);
172 err = gpio_request(sunxi_ehci->gpio_vbus, "ehci_vbus");
176 sunxi_ehci_enable(sunxi_ehci);
178 *hccr = get_io_base(sunxi_ehci->id);
180 *hcor = (struct ehci_hcor *)((uint32_t) *hccr
181 + HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
183 debug("sunxi-ehci: init hccr %x and hcor %x hc_length %d\n",
184 (uint32_t)*hccr, (uint32_t)*hcor,
185 (uint32_t)HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
192 int ehci_hcd_stop(int index)
194 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
195 struct sunxi_ehci_hcd *sunxi_ehci = &sunxi_echi_hcd[index];
198 sunxi_ehci_disable(sunxi_ehci);
200 err = gpio_free(sunxi_ehci->gpio_vbus);
204 /* disable common PHY only once, for the last enabled hcd */
205 if (enabled_hcd_count == 1)
206 clrbits_le32(&ccm->usb_clk_cfg, CCM_USB_CTRL_PHYGATE);