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