1 // SPDX-License-Identifier: GPL-2.0
2 /*-
3  * Copyright (c) 2007-2008, Juniper Networks, Inc.
4  * All rights reserved.
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <init.h>
11 #include <log.h>
12 #include <pci.h>
13 #include <usb.h>
14 #include <asm/io.h>
15 
16 #include "ehci.h"
17 
18 /* Information about a USB port */
19 struct ehci_pci_priv {
20 	struct ehci_ctrl ehci;
21 	struct phy phy;
22 };
23 
24 #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_init(struct udevice * dev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)25 static int ehci_pci_init(struct udevice *dev, struct ehci_hccr **ret_hccr,
26 			  struct ehci_hcor **ret_hcor)
27 {
28 	struct ehci_pci_priv *priv = dev_get_priv(dev);
29 	struct ehci_hccr *hccr;
30 	struct ehci_hcor *hcor;
31 	int ret;
32 	u32 cmd;
33 
34 	ret = ehci_setup_phy(dev, &priv->phy, 0);
35 	if (ret)
36 		return ret;
37 
38 	hccr = (struct ehci_hccr *)dm_pci_map_bar(dev,
39 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
40 	hcor = (struct ehci_hcor *)((uintptr_t) hccr +
41 			HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
42 
43 	debug("EHCI-PCI init hccr %#lx and hcor %#lx hc_length %d\n",
44 	      (ulong)hccr, (ulong)hcor,
45 	      (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
46 
47 	*ret_hccr = hccr;
48 	*ret_hcor = hcor;
49 
50 	/* enable busmaster */
51 	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
52 	cmd |= PCI_COMMAND_MASTER;
53 	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
54 
55 	return 0;
56 }
57 
58 #else
59 
60 #ifdef CONFIG_PCI_EHCI_DEVICE
61 static struct pci_device_id ehci_pci_ids[] = {
62 	/* Please add supported PCI EHCI controller ids here */
63 	{0x1033, 0x00E0},	/* NEC */
64 	{0x10B9, 0x5239},	/* ULI1575 PCI EHCI module ids */
65 	{0x12D8, 0x400F},	/* Pericom */
66 	{0, 0}
67 };
68 #endif
69 
ehci_pci_legacy_init(pci_dev_t pdev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)70 static void ehci_pci_legacy_init(pci_dev_t pdev, struct ehci_hccr **ret_hccr,
71 				 struct ehci_hcor **ret_hcor)
72 {
73 	struct ehci_hccr *hccr;
74 	struct ehci_hcor *hcor;
75 	u32 cmd;
76 
77 	hccr = (struct ehci_hccr *)pci_map_bar(pdev,
78 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
79 	hcor = (struct ehci_hcor *)((uintptr_t) hccr +
80 			HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
81 
82 	debug("EHCI-PCI init hccr 0x%x and hcor 0x%x hc_length %d\n",
83 	      (u32)hccr, (u32)hcor,
84 	      (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
85 
86 	*ret_hccr = hccr;
87 	*ret_hcor = hcor;
88 
89 	/* enable busmaster */
90 	pci_read_config_dword(pdev, PCI_COMMAND, &cmd);
91 	cmd |= PCI_COMMAND_MASTER;
92 	pci_write_config_dword(pdev, PCI_COMMAND, cmd);
93 }
94 
95 /*
96  * Create the appropriate control structures to manage
97  * a new EHCI host controller.
98  */
ehci_hcd_init(int index,enum usb_init_type init,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)99 int ehci_hcd_init(int index, enum usb_init_type init,
100 		struct ehci_hccr **ret_hccr, struct ehci_hcor **ret_hcor)
101 {
102 	pci_dev_t pdev;
103 
104 #ifdef CONFIG_PCI_EHCI_DEVICE
105 	pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVICE);
106 #else
107 	pdev = pci_find_class(PCI_CLASS_SERIAL_USB_EHCI, index);
108 #endif
109 	if (pdev < 0) {
110 		printf("EHCI host controller not found\n");
111 		return -1;
112 	}
113 	ehci_pci_legacy_init(pdev, ret_hccr, ret_hcor);
114 
115 	return 0;
116 }
117 
118 /*
119  * Destroy the appropriate control structures corresponding
120  * the the EHCI host controller.
121  */
ehci_hcd_stop(int index)122 int ehci_hcd_stop(int index)
123 {
124 	return 0;
125 }
126 #endif /* !CONFIG_IS_ENABLED(DM_USB) */
127 
128 #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_probe(struct udevice * dev)129 static int ehci_pci_probe(struct udevice *dev)
130 {
131 	struct ehci_hccr *hccr;
132 	struct ehci_hcor *hcor;
133 	int ret;
134 
135 	ret = ehci_pci_init(dev, &hccr, &hcor);
136 	if (ret)
137 		return ret;
138 
139 	return ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
140 }
141 
ehci_pci_remove(struct udevice * dev)142 static int ehci_pci_remove(struct udevice *dev)
143 {
144 	struct ehci_pci_priv *priv = dev_get_priv(dev);
145 	int ret;
146 
147 	ret = ehci_deregister(dev);
148 	if (ret)
149 		return ret;
150 
151 	return ehci_shutdown_phy(dev, &priv->phy);
152 }
153 
154 static const struct udevice_id ehci_pci_ids[] = {
155 	{ .compatible = "ehci-pci" },
156 	{ }
157 };
158 
159 U_BOOT_DRIVER(ehci_pci) = {
160 	.name	= "ehci_pci",
161 	.id	= UCLASS_USB,
162 	.probe = ehci_pci_probe,
163 	.remove = ehci_pci_remove,
164 	.of_match = ehci_pci_ids,
165 	.ops	= &ehci_usb_ops,
166 	.plat_auto	= sizeof(struct usb_plat),
167 	.priv_auto	= sizeof(struct ehci_pci_priv),
168 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
169 };
170 
171 static struct pci_device_id ehci_pci_supported[] = {
172 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_EHCI, ~0) },
173 	{},
174 };
175 
176 U_BOOT_PCI_DEVICE(ehci_pci, ehci_pci_supported);
177 
178 #endif /* CONFIG_IS_ENABLED(DM_USB) */
179