1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014, Xilinx, Inc
4  *
5  * USB Low level initialization(Specific to zynq)
6  */
7 
8 #include <dm.h>
9 #include <usb.h>
10 #include <asm/arch/hardware.h>
11 #include <asm/arch/sys_proto.h>
12 #include <asm/io.h>
13 #include <usb/ehci-ci.h>
14 #include <usb/ulpi.h>
15 
16 #include "ehci.h"
17 
18 struct zynq_ehci_priv {
19 	struct ehci_ctrl ehcictrl;
20 	struct usb_ehci *ehci;
21 };
22 
ehci_zynq_of_to_plat(struct udevice * dev)23 static int ehci_zynq_of_to_plat(struct udevice *dev)
24 {
25 	struct zynq_ehci_priv *priv = dev_get_priv(dev);
26 
27 	priv->ehci = dev_read_addr_ptr(dev);
28 	if (!priv->ehci)
29 		return -EINVAL;
30 
31 	return 0;
32 }
33 
ehci_zynq_probe(struct udevice * dev)34 static int ehci_zynq_probe(struct udevice *dev)
35 {
36 	struct usb_plat *plat = dev_get_plat(dev);
37 	struct zynq_ehci_priv *priv = dev_get_priv(dev);
38 	struct ehci_hccr *hccr;
39 	struct ehci_hcor *hcor;
40 	struct ulpi_viewport ulpi_vp;
41 	/* Used for writing the ULPI data address */
42 	struct ulpi_regs *ulpi = (struct ulpi_regs *)0;
43 	int ret;
44 
45 	hccr = (struct ehci_hccr *)((uint32_t)&priv->ehci->caplength);
46 	hcor = (struct ehci_hcor *)((uint32_t) hccr +
47 			HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
48 
49 	ulpi_vp.viewport_addr = (u32)&priv->ehci->ulpi_viewpoint;
50 	ulpi_vp.port_num = 0;
51 
52 	ret = ulpi_init(&ulpi_vp);
53 	if (ret) {
54 		puts("zynq ULPI viewport init failed\n");
55 		return -1;
56 	}
57 
58 	/* ULPI set flags */
59 	ulpi_write(&ulpi_vp, &ulpi->otg_ctrl,
60 		   ULPI_OTG_DP_PULLDOWN | ULPI_OTG_DM_PULLDOWN |
61 		   ULPI_OTG_EXTVBUSIND);
62 	ulpi_write(&ulpi_vp, &ulpi->function_ctrl,
63 		   ULPI_FC_FULL_SPEED | ULPI_FC_OPMODE_NORMAL |
64 		   ULPI_FC_SUSPENDM);
65 	ulpi_write(&ulpi_vp, &ulpi->iface_ctrl, 0);
66 
67 	/* Set VBus */
68 	ulpi_write(&ulpi_vp, &ulpi->otg_ctrl_set,
69 		   ULPI_OTG_DRVVBUS | ULPI_OTG_DRVVBUS_EXT);
70 
71 	return ehci_register(dev, hccr, hcor, NULL, 0, plat->init_type);
72 }
73 
74 static const struct udevice_id ehci_zynq_ids[] = {
75 	{ .compatible = "xlnx,zynq-usb-2.20a" },
76 	{ }
77 };
78 
79 U_BOOT_DRIVER(ehci_zynq) = {
80 	.name	= "ehci_zynq",
81 	.id	= UCLASS_USB,
82 	.of_match = ehci_zynq_ids,
83 	.of_to_plat = ehci_zynq_of_to_plat,
84 	.probe = ehci_zynq_probe,
85 	.remove = ehci_deregister,
86 	.ops	= &ehci_usb_ops,
87 	.plat_auto	= sizeof(struct usb_plat),
88 	.priv_auto	= sizeof(struct zynq_ehci_priv),
89 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
90 };
91