183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2cc30b780SBo Shen /*
3cc30b780SBo Shen * (C) Copyright 2012
4cc30b780SBo Shen * Atmel Semiconductor <www.atmel.com>
5cc30b780SBo Shen * Written-by: Bo Shen <voice.shen@atmel.com>
6cc30b780SBo Shen */
7cc30b780SBo Shen
8cc30b780SBo Shen #include <common.h>
917b68b5aSWenyou Yang #include <clk.h>
1017b68b5aSWenyou Yang #include <dm.h>
11cc30b780SBo Shen #include <usb.h>
12cc30b780SBo Shen #include <asm/io.h>
13cc30b780SBo Shen #include <asm/arch/clk.h>
14cc30b780SBo Shen
15cc30b780SBo Shen #include "ehci.h"
16cc30b780SBo Shen
17*fd09c205SSven Schwermer #if !CONFIG_IS_ENABLED(DM_USB)
1817b68b5aSWenyou Yang
ehci_hcd_init(int index,enum usb_init_type init,struct ehci_hccr ** hccr,struct ehci_hcor ** hcor)19127efc4fSTroy Kisky int ehci_hcd_init(int index, enum usb_init_type init,
20127efc4fSTroy Kisky struct ehci_hccr **hccr, struct ehci_hcor **hcor)
21cc30b780SBo Shen {
22cc30b780SBo Shen /* Enable UTMI PLL */
23b55b5960SWenyou Yang if (at91_upll_clk_enable())
24cc30b780SBo Shen return -1;
25cc30b780SBo Shen
26cc30b780SBo Shen /* Enable USB Host clock */
2797b2043dSBo Shen at91_periph_clk_enable(ATMEL_ID_UHPHS);
28cc30b780SBo Shen
29676ae068SLucas Stach *hccr = (struct ehci_hccr *)ATMEL_BASE_EHCI;
30676ae068SLucas Stach *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
31676ae068SLucas Stach HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
32cc30b780SBo Shen
33cc30b780SBo Shen return 0;
34cc30b780SBo Shen }
35cc30b780SBo Shen
ehci_hcd_stop(int index)36676ae068SLucas Stach int ehci_hcd_stop(int index)
37cc30b780SBo Shen {
38cc30b780SBo Shen /* Disable USB Host Clock */
3997b2043dSBo Shen at91_periph_clk_disable(ATMEL_ID_UHPHS);
40cc30b780SBo Shen
41cc30b780SBo Shen /* Disable UTMI PLL */
42b55b5960SWenyou Yang if (at91_upll_clk_disable())
43cc30b780SBo Shen return -1;
44cc30b780SBo Shen
45cc30b780SBo Shen return 0;
46cc30b780SBo Shen }
4717b68b5aSWenyou Yang
4817b68b5aSWenyou Yang #else
4917b68b5aSWenyou Yang
5017b68b5aSWenyou Yang struct ehci_atmel_priv {
5117b68b5aSWenyou Yang struct ehci_ctrl ehci;
5217b68b5aSWenyou Yang };
5317b68b5aSWenyou Yang
ehci_atmel_enable_clk(struct udevice * dev)5417b68b5aSWenyou Yang static int ehci_atmel_enable_clk(struct udevice *dev)
5517b68b5aSWenyou Yang {
5617b68b5aSWenyou Yang struct clk clk;
5717b68b5aSWenyou Yang int ret;
5817b68b5aSWenyou Yang
5917b68b5aSWenyou Yang ret = clk_get_by_index(dev, 0, &clk);
6017b68b5aSWenyou Yang if (ret)
6117b68b5aSWenyou Yang return ret;
6217b68b5aSWenyou Yang
6317b68b5aSWenyou Yang ret = clk_enable(&clk);
6417b68b5aSWenyou Yang if (ret)
6517b68b5aSWenyou Yang return ret;
6617b68b5aSWenyou Yang
6717b68b5aSWenyou Yang ret = clk_get_by_index(dev, 1, &clk);
6817b68b5aSWenyou Yang if (ret)
6917b68b5aSWenyou Yang return -EINVAL;
7017b68b5aSWenyou Yang
7117b68b5aSWenyou Yang ret = clk_enable(&clk);
7217b68b5aSWenyou Yang if (ret)
7317b68b5aSWenyou Yang return ret;
7417b68b5aSWenyou Yang
7517b68b5aSWenyou Yang clk_free(&clk);
7617b68b5aSWenyou Yang
7717b68b5aSWenyou Yang return 0;
7817b68b5aSWenyou Yang }
7917b68b5aSWenyou Yang
ehci_atmel_probe(struct udevice * dev)8017b68b5aSWenyou Yang static int ehci_atmel_probe(struct udevice *dev)
8117b68b5aSWenyou Yang {
8217b68b5aSWenyou Yang struct ehci_hccr *hccr;
8317b68b5aSWenyou Yang struct ehci_hcor *hcor;
8417b68b5aSWenyou Yang fdt_addr_t hcd_base;
8517b68b5aSWenyou Yang int ret;
8617b68b5aSWenyou Yang
8717b68b5aSWenyou Yang ret = ehci_atmel_enable_clk(dev);
8817b68b5aSWenyou Yang if (ret) {
8917b68b5aSWenyou Yang debug("Failed to enable USB Host clock\n");
9017b68b5aSWenyou Yang return ret;
9117b68b5aSWenyou Yang }
9217b68b5aSWenyou Yang
9317b68b5aSWenyou Yang /*
9417b68b5aSWenyou Yang * Get the base address for EHCI controller from the device node
9517b68b5aSWenyou Yang */
96a821c4afSSimon Glass hcd_base = devfdt_get_addr(dev);
9717b68b5aSWenyou Yang if (hcd_base == FDT_ADDR_T_NONE) {
9817b68b5aSWenyou Yang debug("Can't get the EHCI register base address\n");
9917b68b5aSWenyou Yang return -ENXIO;
10017b68b5aSWenyou Yang }
10117b68b5aSWenyou Yang
10217b68b5aSWenyou Yang hccr = (struct ehci_hccr *)hcd_base;
10317b68b5aSWenyou Yang hcor = (struct ehci_hcor *)
10417b68b5aSWenyou Yang ((u32)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
10517b68b5aSWenyou Yang
10617b68b5aSWenyou Yang debug("echi-atmel: init hccr %x and hcor %x hc_length %d\n",
10717b68b5aSWenyou Yang (u32)hccr, (u32)hcor,
10817b68b5aSWenyou Yang (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
10917b68b5aSWenyou Yang
11017b68b5aSWenyou Yang return ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
11117b68b5aSWenyou Yang }
11217b68b5aSWenyou Yang
11317b68b5aSWenyou Yang static const struct udevice_id ehci_usb_ids[] = {
11417b68b5aSWenyou Yang { .compatible = "atmel,at91sam9g45-ehci", },
11517b68b5aSWenyou Yang { }
11617b68b5aSWenyou Yang };
11717b68b5aSWenyou Yang
11817b68b5aSWenyou Yang U_BOOT_DRIVER(ehci_atmel) = {
11917b68b5aSWenyou Yang .name = "ehci_atmel",
12017b68b5aSWenyou Yang .id = UCLASS_USB,
12117b68b5aSWenyou Yang .of_match = ehci_usb_ids,
12217b68b5aSWenyou Yang .probe = ehci_atmel_probe,
12340527342SMasahiro Yamada .remove = ehci_deregister,
12417b68b5aSWenyou Yang .ops = &ehci_usb_ops,
12517b68b5aSWenyou Yang .platdata_auto_alloc_size = sizeof(struct usb_platdata),
12617b68b5aSWenyou Yang .priv_auto_alloc_size = sizeof(struct ehci_atmel_priv),
12717b68b5aSWenyou Yang .flags = DM_FLAG_ALLOC_PRIV_DMA,
12817b68b5aSWenyou Yang };
12917b68b5aSWenyou Yang
13017b68b5aSWenyou Yang #endif
131