blob: dd92808ff7b37a19c5c4b9b980c164378d4960ef [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowski5a822112016-03-31 23:12:26 +02002/*
3 * Qualcomm EHCI driver
4 *
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6 *
7 * Based on Linux driver
Mateusz Kulikowski5a822112016-03-31 23:12:26 +02008 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020013#include <usb.h>
14#include <usb/ehci-ci.h>
15#include <usb/ulpi.h>
16#include <wait_bit.h>
17#include <asm/gpio.h>
18#include <asm/io.h>
19#include <linux/compat.h>
20#include "ehci.h"
21
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020022struct msm_ehci_priv {
23 struct ehci_ctrl ctrl; /* Needed by EHCI */
24 struct usb_ehci *ehci; /* Start of IP core*/
25 struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030026 struct phy phy;
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020027};
28
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020029static int msm_init_after_reset(struct ehci_ctrl *dev)
30{
31 struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
32 struct usb_ehci *ehci = p->ehci;
33
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030034 generic_phy_reset(&p->phy);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020035
36 /* set mode to host controller */
37 writel(CM_HOST, &ehci->usbmode);
38
39 return 0;
40}
41
42static const struct ehci_ops msm_ehci_ops = {
43 .init_after_reset = msm_init_after_reset
44};
45
46static int ehci_usb_probe(struct udevice *dev)
47{
48 struct msm_ehci_priv *p = dev_get_priv(dev);
49 struct usb_ehci *ehci = p->ehci;
Ramon Fried0683b272018-09-21 13:35:51 +030050 struct usb_platdata *plat = dev_get_platdata(dev);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020051 struct ehci_hccr *hccr;
52 struct ehci_hcor *hcor;
53 int ret;
54
55 hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
56 hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
57 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
58
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030059 ret = ehci_setup_phy(dev, &p->phy, 0);
60 if (ret)
61 return ret;
62
Ramon Fried0683b272018-09-21 13:35:51 +030063 ret = board_usb_init(0, plat->init_type);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020064 if (ret < 0)
65 return ret;
66
Ramon Fried0683b272018-09-21 13:35:51 +030067 return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0,
68 plat->init_type);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020069}
70
71static int ehci_usb_remove(struct udevice *dev)
72{
73 struct msm_ehci_priv *p = dev_get_priv(dev);
74 struct usb_ehci *ehci = p->ehci;
75 int ret;
76
77 ret = ehci_deregister(dev);
78 if (ret)
79 return ret;
80
81 /* Stop controller. */
82 clrbits_le32(&ehci->usbcmd, CMD_RUN);
83
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030084 ret = ehci_shutdown_phy(dev, &p->phy);
85 if (ret)
86 return ret;
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020087
Ramon Friedcd8c3ae2018-09-21 13:35:43 +030088 ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020089 if (ret < 0)
90 return ret;
91
92 /* Reset controller */
93 setbits_le32(&ehci->usbcmd, CMD_RESET);
94
95 /* Wait for reset */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +010096 if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020097 printf("Stuck on USB reset.\n");
98 return -ETIMEDOUT;
99 }
100
101 return 0;
102}
103
104static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
105{
106 struct msm_ehci_priv *priv = dev_get_priv(dev);
107
108 priv->ulpi_vp.port_num = 0;
Kever Yang2be11302020-03-04 08:59:49 +0800109 priv->ehci = dev_read_addr_ptr(dev);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +0200110
111 if (priv->ehci == (void *)FDT_ADDR_T_NONE)
112 return -EINVAL;
113
114 /* Warning: this will not work if viewport address is > 64 bit due to
115 * ULPI design.
116 */
117 priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
118
119 return 0;
120}
121
Ramon Fried24667042018-09-21 13:35:53 +0300122#if defined(CONFIG_CI_UDC)
123/* Little quirk that MSM needs with Chipidea controller
124 * Must reinit phy after reset
125 */
126void ci_init_after_reset(struct ehci_ctrl *ctrl)
127{
128 struct msm_ehci_priv *p = ctrl->priv;
129
130 generic_phy_reset(&p->phy);
131}
132#endif
133
Mateusz Kulikowski5a822112016-03-31 23:12:26 +0200134static const struct udevice_id ehci_usb_ids[] = {
135 { .compatible = "qcom,ehci-host", },
136 { }
137};
138
139U_BOOT_DRIVER(usb_ehci) = {
140 .name = "ehci_msm",
141 .id = UCLASS_USB,
142 .of_match = ehci_usb_ids,
143 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
144 .probe = ehci_usb_probe,
145 .remove = ehci_usb_remove,
146 .ops = &ehci_usb_ops,
147 .priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
Ramon Fried2df49232018-09-21 13:35:44 +0300148 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
Mateusz Kulikowski5a822112016-03-31 23:12:26 +0200149 .flags = DM_FLAG_ALLOC_PRIV_DMA,
150};