blob: 00d6bb823113e0d86439de5a9bb713edc042d21e [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>
13#include <fdtdec.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020015#include <usb.h>
16#include <usb/ehci-ci.h>
17#include <usb/ulpi.h>
18#include <wait_bit.h>
19#include <asm/gpio.h>
20#include <asm/io.h>
21#include <linux/compat.h>
22#include "ehci.h"
23
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020024struct msm_ehci_priv {
25 struct ehci_ctrl ctrl; /* Needed by EHCI */
26 struct usb_ehci *ehci; /* Start of IP core*/
27 struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030028 struct phy phy;
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020029};
30
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020031static int msm_init_after_reset(struct ehci_ctrl *dev)
32{
33 struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
34 struct usb_ehci *ehci = p->ehci;
35
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030036 generic_phy_reset(&p->phy);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020037
38 /* set mode to host controller */
39 writel(CM_HOST, &ehci->usbmode);
40
41 return 0;
42}
43
44static const struct ehci_ops msm_ehci_ops = {
45 .init_after_reset = msm_init_after_reset
46};
47
48static int ehci_usb_probe(struct udevice *dev)
49{
50 struct msm_ehci_priv *p = dev_get_priv(dev);
51 struct usb_ehci *ehci = p->ehci;
Ramon Fried0683b272018-09-21 13:35:51 +030052 struct usb_platdata *plat = dev_get_platdata(dev);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020053 struct ehci_hccr *hccr;
54 struct ehci_hcor *hcor;
55 int ret;
56
57 hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
58 hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
59 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
60
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030061 ret = ehci_setup_phy(dev, &p->phy, 0);
62 if (ret)
63 return ret;
64
Ramon Fried0683b272018-09-21 13:35:51 +030065 ret = board_usb_init(0, plat->init_type);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020066 if (ret < 0)
67 return ret;
68
Ramon Fried0683b272018-09-21 13:35:51 +030069 return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0,
70 plat->init_type);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020071}
72
73static int ehci_usb_remove(struct udevice *dev)
74{
75 struct msm_ehci_priv *p = dev_get_priv(dev);
76 struct usb_ehci *ehci = p->ehci;
77 int ret;
78
79 ret = ehci_deregister(dev);
80 if (ret)
81 return ret;
82
83 /* Stop controller. */
84 clrbits_le32(&ehci->usbcmd, CMD_RUN);
85
Ramon Fried0ac0b6e2018-09-21 13:35:50 +030086 ret = ehci_shutdown_phy(dev, &p->phy);
87 if (ret)
88 return ret;
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020089
Ramon Friedcd8c3ae2018-09-21 13:35:43 +030090 ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020091 if (ret < 0)
92 return ret;
93
94 /* Reset controller */
95 setbits_le32(&ehci->usbcmd, CMD_RESET);
96
97 /* Wait for reset */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +010098 if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
Mateusz Kulikowski5a822112016-03-31 23:12:26 +020099 printf("Stuck on USB reset.\n");
100 return -ETIMEDOUT;
101 }
102
103 return 0;
104}
105
106static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
107{
108 struct msm_ehci_priv *priv = dev_get_priv(dev);
109
110 priv->ulpi_vp.port_num = 0;
Simon Glassa821c4a2017-05-17 17:18:05 -0600111 priv->ehci = (void *)devfdt_get_addr(dev);
Mateusz Kulikowski5a822112016-03-31 23:12:26 +0200112
113 if (priv->ehci == (void *)FDT_ADDR_T_NONE)
114 return -EINVAL;
115
116 /* Warning: this will not work if viewport address is > 64 bit due to
117 * ULPI design.
118 */
119 priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
120
121 return 0;
122}
123
124static const struct udevice_id ehci_usb_ids[] = {
125 { .compatible = "qcom,ehci-host", },
126 { }
127};
128
129U_BOOT_DRIVER(usb_ehci) = {
130 .name = "ehci_msm",
131 .id = UCLASS_USB,
132 .of_match = ehci_usb_ids,
133 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
134 .probe = ehci_usb_probe,
135 .remove = ehci_usb_remove,
136 .ops = &ehci_usb_ops,
137 .priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
Ramon Fried2df49232018-09-21 13:35:44 +0300138 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
Mateusz Kulikowski5a822112016-03-31 23:12:26 +0200139 .flags = DM_FLAG_ALLOC_PRIV_DMA,
140};