blob: f9083d9a64ac6d6be08ca3e3db2152ce6114cd51 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shencc30b782012-06-27 21:58:20 +00002/*
3 * (C) Copyright 2012
4 * Atmel Semiconductor <www.atmel.com>
5 * Written-by: Bo Shen <voice.shen@atmel.com>
Bo Shencc30b782012-06-27 21:58:20 +00006 */
7
8#include <common.h>
Wenyou Yang17b68b52016-08-05 08:57:35 +08009#include <clk.h>
10#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Bo Shencc30b782012-06-27 21:58:20 +000013#include <usb.h>
14#include <asm/io.h>
Bo Shencc30b782012-06-27 21:58:20 +000015#include <asm/arch/clk.h>
16
17#include "ehci.h"
Bo Shencc30b782012-06-27 21:58:20 +000018
Sven Schwermerfd09c202018-11-21 08:43:56 +010019#if !CONFIG_IS_ENABLED(DM_USB)
Wenyou Yang17b68b52016-08-05 08:57:35 +080020
Troy Kisky127efc42013-10-10 15:27:57 -070021int ehci_hcd_init(int index, enum usb_init_type init,
22 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Bo Shencc30b782012-06-27 21:58:20 +000023{
Bo Shencc30b782012-06-27 21:58:20 +000024 /* Enable UTMI PLL */
Wenyou Yangb55b5962016-02-02 11:11:53 +080025 if (at91_upll_clk_enable())
26 return -1;
Bo Shencc30b782012-06-27 21:58:20 +000027
28 /* Enable USB Host clock */
Bo Shen97b20432014-08-06 17:24:57 +080029 at91_periph_clk_enable(ATMEL_ID_UHPHS);
Bo Shencc30b782012-06-27 21:58:20 +000030
Lucas Stach676ae062012-09-26 00:14:35 +020031 *hccr = (struct ehci_hccr *)ATMEL_BASE_EHCI;
32 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
33 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
Bo Shencc30b782012-06-27 21:58:20 +000034
35 return 0;
36}
37
Lucas Stach676ae062012-09-26 00:14:35 +020038int ehci_hcd_stop(int index)
Bo Shencc30b782012-06-27 21:58:20 +000039{
Bo Shencc30b782012-06-27 21:58:20 +000040 /* Disable USB Host Clock */
Bo Shen97b20432014-08-06 17:24:57 +080041 at91_periph_clk_disable(ATMEL_ID_UHPHS);
Bo Shencc30b782012-06-27 21:58:20 +000042
Bo Shencc30b782012-06-27 21:58:20 +000043 /* Disable UTMI PLL */
Wenyou Yangb55b5962016-02-02 11:11:53 +080044 if (at91_upll_clk_disable())
45 return -1;
Bo Shencc30b782012-06-27 21:58:20 +000046
47 return 0;
48}
Wenyou Yang17b68b52016-08-05 08:57:35 +080049
50#else
51
52struct ehci_atmel_priv {
53 struct ehci_ctrl ehci;
54};
55
56static int ehci_atmel_enable_clk(struct udevice *dev)
57{
Wenyou Yang17b68b52016-08-05 08:57:35 +080058 struct clk clk;
Wenyou Yang17b68b52016-08-05 08:57:35 +080059 int ret;
60
61 ret = clk_get_by_index(dev, 0, &clk);
62 if (ret)
63 return ret;
64
65 ret = clk_enable(&clk);
66 if (ret)
67 return ret;
68
69 ret = clk_get_by_index(dev, 1, &clk);
70 if (ret)
71 return -EINVAL;
72
Wenyou Yang17b68b52016-08-05 08:57:35 +080073 ret = clk_enable(&clk);
74 if (ret)
75 return ret;
76
77 clk_free(&clk);
78
79 return 0;
80}
81
82static int ehci_atmel_probe(struct udevice *dev)
83{
84 struct ehci_hccr *hccr;
85 struct ehci_hcor *hcor;
86 fdt_addr_t hcd_base;
87 int ret;
88
89 ret = ehci_atmel_enable_clk(dev);
90 if (ret) {
91 debug("Failed to enable USB Host clock\n");
92 return ret;
93 }
94
95 /*
96 * Get the base address for EHCI controller from the device node
97 */
Simon Glassa821c4a2017-05-17 17:18:05 -060098 hcd_base = devfdt_get_addr(dev);
Wenyou Yang17b68b52016-08-05 08:57:35 +080099 if (hcd_base == FDT_ADDR_T_NONE) {
100 debug("Can't get the EHCI register base address\n");
101 return -ENXIO;
102 }
103
104 hccr = (struct ehci_hccr *)hcd_base;
105 hcor = (struct ehci_hcor *)
106 ((u32)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
107
108 debug("echi-atmel: init hccr %x and hcor %x hc_length %d\n",
109 (u32)hccr, (u32)hcor,
110 (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
111
112 return ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
113}
114
Wenyou Yang17b68b52016-08-05 08:57:35 +0800115static const struct udevice_id ehci_usb_ids[] = {
116 { .compatible = "atmel,at91sam9g45-ehci", },
117 { }
118};
119
120U_BOOT_DRIVER(ehci_atmel) = {
121 .name = "ehci_atmel",
122 .id = UCLASS_USB,
123 .of_match = ehci_usb_ids,
124 .probe = ehci_atmel_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900125 .remove = ehci_deregister,
Wenyou Yang17b68b52016-08-05 08:57:35 +0800126 .ops = &ehci_usb_ops,
127 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
128 .priv_auto_alloc_size = sizeof(struct ehci_atmel_priv),
129 .flags = DM_FLAG_ALLOC_PRIV_DMA,
130};
131
132#endif