xref: /rk3399_rockchip-uboot/drivers/usb/host/ehci-pci.c (revision 1a4f6af8bfd44c8ae6e87a81ff125eed47042cc5)
12731b9a8SJean-Christophe PLAGNIOL-VILLARD /*-
22731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Copyright (c) 2007-2008, Juniper Networks, Inc.
32731b9a8SJean-Christophe PLAGNIOL-VILLARD  * All rights reserved.
42731b9a8SJean-Christophe PLAGNIOL-VILLARD  *
5e62b5266SSimon Glass  * SPDX-License-Identifier:	GPL-2.0
62731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
72731b9a8SJean-Christophe PLAGNIOL-VILLARD 
82731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
92b53b078SSimon Glass #include <dm.h>
107c38e90aSVincent Palatin #include <errno.h>
112731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
122731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
132cb7b900SAlexey Brodkin #include <asm/io.h>
142731b9a8SJean-Christophe PLAGNIOL-VILLARD 
152731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
172b53b078SSimon Glass /* Information about a USB port */
182b53b078SSimon Glass struct ehci_pci_priv {
192b53b078SSimon Glass 	struct ehci_ctrl ehci;
2097147d3eSMarek Vasut 	struct phy phy;
212731b9a8SJean-Christophe PLAGNIOL-VILLARD };
222731b9a8SJean-Christophe PLAGNIOL-VILLARD 
23*3739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_init(struct udevice * dev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)2497147d3eSMarek Vasut static int ehci_pci_init(struct udevice *dev, struct ehci_hccr **ret_hccr,
252b53b078SSimon Glass 			  struct ehci_hcor **ret_hcor)
262731b9a8SJean-Christophe PLAGNIOL-VILLARD {
2797147d3eSMarek Vasut 	struct ehci_pci_priv *priv = dev_get_priv(dev);
28ae003d05SVincent Palatin 	struct ehci_hccr *hccr;
29ae003d05SVincent Palatin 	struct ehci_hcor *hcor;
3097147d3eSMarek Vasut 	int ret;
3109c5c164SSimon Glass 	u32 cmd;
322731b9a8SJean-Christophe PLAGNIOL-VILLARD 
3397147d3eSMarek Vasut 	ret = ehci_setup_phy(dev, &priv->phy, 0);
3497147d3eSMarek Vasut 	if (ret)
3597147d3eSMarek Vasut 		return ret;
3697147d3eSMarek Vasut 
3709c5c164SSimon Glass 	hccr = (struct ehci_hccr *)dm_pci_map_bar(dev,
38ae46d2a9SZhao Chenhui 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
3909c5c164SSimon Glass 	hcor = (struct ehci_hcor *)((uintptr_t) hccr +
40ae003d05SVincent Palatin 			HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
412731b9a8SJean-Christophe PLAGNIOL-VILLARD 
42b11e2984SSimon Glass 	debug("EHCI-PCI init hccr %#lx and hcor %#lx hc_length %d\n",
43b11e2984SSimon Glass 	      (ulong)hccr, (ulong)hcor,
4409c5c164SSimon Glass 	      (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
45ae003d05SVincent Palatin 
46ae003d05SVincent Palatin 	*ret_hccr = hccr;
47ae003d05SVincent Palatin 	*ret_hcor = hcor;
48af68c066SFlorian Fainelli 
497c38e90aSVincent Palatin 	/* enable busmaster */
5009c5c164SSimon Glass 	dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
517c38e90aSVincent Palatin 	cmd |= PCI_COMMAND_MASTER;
5209c5c164SSimon Glass 	dm_pci_write_config32(dev, PCI_COMMAND, cmd);
5397147d3eSMarek Vasut 
5497147d3eSMarek Vasut 	return 0;
552b53b078SSimon Glass }
562b53b078SSimon Glass 
5709c5c164SSimon Glass #else
582b53b078SSimon Glass 
592b53b078SSimon Glass #ifdef CONFIG_PCI_EHCI_DEVICE
602b53b078SSimon Glass static struct pci_device_id ehci_pci_ids[] = {
612b53b078SSimon Glass 	/* Please add supported PCI EHCI controller ids here */
622b53b078SSimon Glass 	{0x1033, 0x00E0},	/* NEC */
632b53b078SSimon Glass 	{0x10B9, 0x5239},	/* ULI1575 PCI EHCI module ids */
642b53b078SSimon Glass 	{0x12D8, 0x400F},	/* Pericom */
652b53b078SSimon Glass 	{0, 0}
662b53b078SSimon Glass };
672b53b078SSimon Glass #endif
682b53b078SSimon Glass 
ehci_pci_legacy_init(pci_dev_t pdev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)6909c5c164SSimon Glass static void ehci_pci_legacy_init(pci_dev_t pdev, struct ehci_hccr **ret_hccr,
7009c5c164SSimon Glass 				 struct ehci_hcor **ret_hcor)
7109c5c164SSimon Glass {
7209c5c164SSimon Glass 	struct ehci_hccr *hccr;
7309c5c164SSimon Glass 	struct ehci_hcor *hcor;
7409c5c164SSimon Glass 	u32 cmd;
7509c5c164SSimon Glass 
7609c5c164SSimon Glass 	hccr = (struct ehci_hccr *)pci_map_bar(pdev,
7709c5c164SSimon Glass 			PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
7809c5c164SSimon Glass 	hcor = (struct ehci_hcor *)((uintptr_t) hccr +
7909c5c164SSimon Glass 			HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
8009c5c164SSimon Glass 
8109c5c164SSimon Glass 	debug("EHCI-PCI init hccr 0x%x and hcor 0x%x hc_length %d\n",
8209c5c164SSimon Glass 	      (u32)hccr, (u32)hcor,
8309c5c164SSimon Glass 	      (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
8409c5c164SSimon Glass 
8509c5c164SSimon Glass 	*ret_hccr = hccr;
8609c5c164SSimon Glass 	*ret_hcor = hcor;
8709c5c164SSimon Glass 
8809c5c164SSimon Glass 	/* enable busmaster */
8909c5c164SSimon Glass 	pci_read_config_dword(pdev, PCI_COMMAND, &cmd);
9009c5c164SSimon Glass 	cmd |= PCI_COMMAND_MASTER;
9109c5c164SSimon Glass 	pci_write_config_dword(pdev, PCI_COMMAND, cmd);
9209c5c164SSimon Glass }
9309c5c164SSimon Glass 
942b53b078SSimon Glass /*
952b53b078SSimon Glass  * Create the appropriate control structures to manage
962b53b078SSimon Glass  * a new EHCI host controller.
972b53b078SSimon Glass  */
ehci_hcd_init(int index,enum usb_init_type init,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)982b53b078SSimon Glass int ehci_hcd_init(int index, enum usb_init_type init,
992b53b078SSimon Glass 		struct ehci_hccr **ret_hccr, struct ehci_hcor **ret_hcor)
1002b53b078SSimon Glass {
1012b53b078SSimon Glass 	pci_dev_t pdev;
1022b53b078SSimon Glass 
1032b53b078SSimon Glass #ifdef CONFIG_PCI_EHCI_DEVICE
1042b53b078SSimon Glass 	pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVICE);
1052b53b078SSimon Glass #else
1062b53b078SSimon Glass 	pdev = pci_find_class(PCI_CLASS_SERIAL_USB_EHCI, index);
1072b53b078SSimon Glass #endif
1082b53b078SSimon Glass 	if (pdev < 0) {
1092b53b078SSimon Glass 		printf("EHCI host controller not found\n");
1102b53b078SSimon Glass 		return -1;
1112b53b078SSimon Glass 	}
11209c5c164SSimon Glass 	ehci_pci_legacy_init(pdev, ret_hccr, ret_hcor);
1132b53b078SSimon Glass 
1142731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
1152731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1162731b9a8SJean-Christophe PLAGNIOL-VILLARD 
1172731b9a8SJean-Christophe PLAGNIOL-VILLARD /*
1182731b9a8SJean-Christophe PLAGNIOL-VILLARD  * Destroy the appropriate control structures corresponding
1192731b9a8SJean-Christophe PLAGNIOL-VILLARD  * the the EHCI host controller.
1202731b9a8SJean-Christophe PLAGNIOL-VILLARD  */
ehci_hcd_stop(int index)121676ae068SLucas Stach int ehci_hcd_stop(int index)
1222731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1232731b9a8SJean-Christophe PLAGNIOL-VILLARD 	return 0;
1242731b9a8SJean-Christophe PLAGNIOL-VILLARD }
125*3739bf7eSSven Schwermer #endif /* !CONFIG_IS_ENABLED(DM_USB) */
1262b53b078SSimon Glass 
127*3739bf7eSSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_probe(struct udevice * dev)1282b53b078SSimon Glass static int ehci_pci_probe(struct udevice *dev)
1292b53b078SSimon Glass {
1302b53b078SSimon Glass 	struct ehci_hccr *hccr;
1312b53b078SSimon Glass 	struct ehci_hcor *hcor;
13297147d3eSMarek Vasut 	int ret;
1332b53b078SSimon Glass 
13497147d3eSMarek Vasut 	ret = ehci_pci_init(dev, &hccr, &hcor);
13597147d3eSMarek Vasut 	if (ret)
13697147d3eSMarek Vasut 		return ret;
1372b53b078SSimon Glass 
1382b53b078SSimon Glass 	return ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
1392b53b078SSimon Glass }
1402b53b078SSimon Glass 
ehci_pci_remove(struct udevice * dev)14197147d3eSMarek Vasut static int ehci_pci_remove(struct udevice *dev)
14297147d3eSMarek Vasut {
14397147d3eSMarek Vasut 	struct ehci_pci_priv *priv = dev_get_priv(dev);
14497147d3eSMarek Vasut 	int ret;
14597147d3eSMarek Vasut 
14697147d3eSMarek Vasut 	ret = ehci_deregister(dev);
14797147d3eSMarek Vasut 	if (ret)
14897147d3eSMarek Vasut 		return ret;
14997147d3eSMarek Vasut 
15097147d3eSMarek Vasut 	return ehci_shutdown_phy(dev, &priv->phy);
15197147d3eSMarek Vasut }
15297147d3eSMarek Vasut 
153907eed2cSSimon Glass static const struct udevice_id ehci_pci_ids[] = {
154907eed2cSSimon Glass 	{ .compatible = "ehci-pci" },
155907eed2cSSimon Glass 	{ }
156907eed2cSSimon Glass };
157907eed2cSSimon Glass 
1582b53b078SSimon Glass U_BOOT_DRIVER(ehci_pci) = {
1592b53b078SSimon Glass 	.name	= "ehci_pci",
1602b53b078SSimon Glass 	.id	= UCLASS_USB,
1612b53b078SSimon Glass 	.probe = ehci_pci_probe,
16297147d3eSMarek Vasut 	.remove = ehci_pci_remove,
163907eed2cSSimon Glass 	.of_match = ehci_pci_ids,
1642b53b078SSimon Glass 	.ops	= &ehci_usb_ops,
1652b53b078SSimon Glass 	.platdata_auto_alloc_size = sizeof(struct usb_platdata),
1662b53b078SSimon Glass 	.priv_auto_alloc_size = sizeof(struct ehci_pci_priv),
1672b53b078SSimon Glass 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
1682b53b078SSimon Glass };
1692b53b078SSimon Glass 
1702b53b078SSimon Glass static struct pci_device_id ehci_pci_supported[] = {
1712b53b078SSimon Glass 	{ PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_EHCI, ~0) },
1722b53b078SSimon Glass 	{},
1732b53b078SSimon Glass };
1742b53b078SSimon Glass 
1752b53b078SSimon Glass U_BOOT_PCI_DEVICE(ehci_pci, ehci_pci_supported);
1762b53b078SSimon Glass 
177*3739bf7eSSven Schwermer #endif /* CONFIG_IS_ENABLED(DM_USB) */
178