183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
22731b9a8SJean-Christophe PLAGNIOL-VILLARD /*-
32731b9a8SJean-Christophe PLAGNIOL-VILLARD * Copyright (c) 2007-2008, Juniper Networks, Inc.
42731b9a8SJean-Christophe PLAGNIOL-VILLARD * All rights reserved.
52731b9a8SJean-Christophe PLAGNIOL-VILLARD */
62731b9a8SJean-Christophe PLAGNIOL-VILLARD
72731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <common.h>
82b53b078SSimon Glass #include <dm.h>
97c38e90aSVincent Palatin #include <errno.h>
102731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <pci.h>
112731b9a8SJean-Christophe PLAGNIOL-VILLARD #include <usb.h>
122cb7b900SAlexey Brodkin #include <asm/io.h>
132731b9a8SJean-Christophe PLAGNIOL-VILLARD
142731b9a8SJean-Christophe PLAGNIOL-VILLARD #include "ehci.h"
152731b9a8SJean-Christophe PLAGNIOL-VILLARD
162b53b078SSimon Glass /* Information about a USB port */
172b53b078SSimon Glass struct ehci_pci_priv {
182b53b078SSimon Glass struct ehci_ctrl ehci;
191335e774SMarek Vasut struct phy phy;
202731b9a8SJean-Christophe PLAGNIOL-VILLARD };
212731b9a8SJean-Christophe PLAGNIOL-VILLARD
22*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_init(struct udevice * dev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)231335e774SMarek Vasut static int ehci_pci_init(struct udevice *dev, struct ehci_hccr **ret_hccr,
242b53b078SSimon Glass struct ehci_hcor **ret_hcor)
252731b9a8SJean-Christophe PLAGNIOL-VILLARD {
261335e774SMarek Vasut struct ehci_pci_priv *priv = dev_get_priv(dev);
27ae003d05SVincent Palatin struct ehci_hccr *hccr;
28ae003d05SVincent Palatin struct ehci_hcor *hcor;
291335e774SMarek Vasut int ret;
3009c5c164SSimon Glass u32 cmd;
312731b9a8SJean-Christophe PLAGNIOL-VILLARD
321335e774SMarek Vasut ret = ehci_setup_phy(dev, &priv->phy, 0);
331335e774SMarek Vasut if (ret)
341335e774SMarek Vasut return ret;
351335e774SMarek Vasut
3609c5c164SSimon Glass hccr = (struct ehci_hccr *)dm_pci_map_bar(dev,
37ae46d2a9SZhao Chenhui PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
3809c5c164SSimon Glass hcor = (struct ehci_hcor *)((uintptr_t) hccr +
39ae003d05SVincent Palatin HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
402731b9a8SJean-Christophe PLAGNIOL-VILLARD
41b11e2984SSimon Glass debug("EHCI-PCI init hccr %#lx and hcor %#lx hc_length %d\n",
42b11e2984SSimon Glass (ulong)hccr, (ulong)hcor,
4309c5c164SSimon Glass (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
44ae003d05SVincent Palatin
45ae003d05SVincent Palatin *ret_hccr = hccr;
46ae003d05SVincent Palatin *ret_hcor = hcor;
47af68c066SFlorian Fainelli
487c38e90aSVincent Palatin /* enable busmaster */
4909c5c164SSimon Glass dm_pci_read_config32(dev, PCI_COMMAND, &cmd);
507c38e90aSVincent Palatin cmd |= PCI_COMMAND_MASTER;
5109c5c164SSimon Glass dm_pci_write_config32(dev, PCI_COMMAND, cmd);
521335e774SMarek Vasut
531335e774SMarek Vasut return 0;
542b53b078SSimon Glass }
552b53b078SSimon Glass
5609c5c164SSimon Glass #else
572b53b078SSimon Glass
582b53b078SSimon Glass #ifdef CONFIG_PCI_EHCI_DEVICE
592b53b078SSimon Glass static struct pci_device_id ehci_pci_ids[] = {
602b53b078SSimon Glass /* Please add supported PCI EHCI controller ids here */
612b53b078SSimon Glass {0x1033, 0x00E0}, /* NEC */
622b53b078SSimon Glass {0x10B9, 0x5239}, /* ULI1575 PCI EHCI module ids */
632b53b078SSimon Glass {0x12D8, 0x400F}, /* Pericom */
642b53b078SSimon Glass {0, 0}
652b53b078SSimon Glass };
662b53b078SSimon Glass #endif
672b53b078SSimon Glass
ehci_pci_legacy_init(pci_dev_t pdev,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)6809c5c164SSimon Glass static void ehci_pci_legacy_init(pci_dev_t pdev, struct ehci_hccr **ret_hccr,
6909c5c164SSimon Glass struct ehci_hcor **ret_hcor)
7009c5c164SSimon Glass {
7109c5c164SSimon Glass struct ehci_hccr *hccr;
7209c5c164SSimon Glass struct ehci_hcor *hcor;
7309c5c164SSimon Glass u32 cmd;
7409c5c164SSimon Glass
7509c5c164SSimon Glass hccr = (struct ehci_hccr *)pci_map_bar(pdev,
7609c5c164SSimon Glass PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
7709c5c164SSimon Glass hcor = (struct ehci_hcor *)((uintptr_t) hccr +
7809c5c164SSimon Glass HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
7909c5c164SSimon Glass
8009c5c164SSimon Glass debug("EHCI-PCI init hccr 0x%x and hcor 0x%x hc_length %d\n",
8109c5c164SSimon Glass (u32)hccr, (u32)hcor,
8209c5c164SSimon Glass (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
8309c5c164SSimon Glass
8409c5c164SSimon Glass *ret_hccr = hccr;
8509c5c164SSimon Glass *ret_hcor = hcor;
8609c5c164SSimon Glass
8709c5c164SSimon Glass /* enable busmaster */
8809c5c164SSimon Glass pci_read_config_dword(pdev, PCI_COMMAND, &cmd);
8909c5c164SSimon Glass cmd |= PCI_COMMAND_MASTER;
9009c5c164SSimon Glass pci_write_config_dword(pdev, PCI_COMMAND, cmd);
9109c5c164SSimon Glass }
9209c5c164SSimon Glass
932b53b078SSimon Glass /*
942b53b078SSimon Glass * Create the appropriate control structures to manage
952b53b078SSimon Glass * a new EHCI host controller.
962b53b078SSimon Glass */
ehci_hcd_init(int index,enum usb_init_type init,struct ehci_hccr ** ret_hccr,struct ehci_hcor ** ret_hcor)972b53b078SSimon Glass int ehci_hcd_init(int index, enum usb_init_type init,
982b53b078SSimon Glass struct ehci_hccr **ret_hccr, struct ehci_hcor **ret_hcor)
992b53b078SSimon Glass {
1002b53b078SSimon Glass pci_dev_t pdev;
1012b53b078SSimon Glass
1022b53b078SSimon Glass #ifdef CONFIG_PCI_EHCI_DEVICE
1032b53b078SSimon Glass pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVICE);
1042b53b078SSimon Glass #else
1052b53b078SSimon Glass pdev = pci_find_class(PCI_CLASS_SERIAL_USB_EHCI, index);
1062b53b078SSimon Glass #endif
1072b53b078SSimon Glass if (pdev < 0) {
1082b53b078SSimon Glass printf("EHCI host controller not found\n");
1092b53b078SSimon Glass return -1;
1102b53b078SSimon Glass }
11109c5c164SSimon Glass ehci_pci_legacy_init(pdev, ret_hccr, ret_hcor);
1122b53b078SSimon Glass
1132731b9a8SJean-Christophe PLAGNIOL-VILLARD return 0;
1142731b9a8SJean-Christophe PLAGNIOL-VILLARD }
1152731b9a8SJean-Christophe PLAGNIOL-VILLARD
1162731b9a8SJean-Christophe PLAGNIOL-VILLARD /*
1172731b9a8SJean-Christophe PLAGNIOL-VILLARD * Destroy the appropriate control structures corresponding
1182731b9a8SJean-Christophe PLAGNIOL-VILLARD * the the EHCI host controller.
1192731b9a8SJean-Christophe PLAGNIOL-VILLARD */
ehci_hcd_stop(int index)120676ae068SLucas Stach int ehci_hcd_stop(int index)
1212731b9a8SJean-Christophe PLAGNIOL-VILLARD {
1222731b9a8SJean-Christophe PLAGNIOL-VILLARD return 0;
1232731b9a8SJean-Christophe PLAGNIOL-VILLARD }
124*fd09c205SSven Schwermer #endif /* !CONFIG_IS_ENABLED(DM_USB) */
1252b53b078SSimon Glass
126*fd09c205SSven Schwermer #if CONFIG_IS_ENABLED(DM_USB)
ehci_pci_probe(struct udevice * dev)1272b53b078SSimon Glass static int ehci_pci_probe(struct udevice *dev)
1282b53b078SSimon Glass {
1292b53b078SSimon Glass struct ehci_hccr *hccr;
1302b53b078SSimon Glass struct ehci_hcor *hcor;
1311335e774SMarek Vasut int ret;
1322b53b078SSimon Glass
1331335e774SMarek Vasut ret = ehci_pci_init(dev, &hccr, &hcor);
1341335e774SMarek Vasut if (ret)
1351335e774SMarek Vasut return ret;
1362b53b078SSimon Glass
1372b53b078SSimon Glass return ehci_register(dev, hccr, hcor, NULL, 0, USB_INIT_HOST);
1382b53b078SSimon Glass }
1392b53b078SSimon Glass
ehci_pci_remove(struct udevice * dev)1401335e774SMarek Vasut static int ehci_pci_remove(struct udevice *dev)
1411335e774SMarek Vasut {
1421335e774SMarek Vasut struct ehci_pci_priv *priv = dev_get_priv(dev);
1431335e774SMarek Vasut int ret;
1441335e774SMarek Vasut
1451335e774SMarek Vasut ret = ehci_deregister(dev);
1461335e774SMarek Vasut if (ret)
1471335e774SMarek Vasut return ret;
1481335e774SMarek Vasut
1491335e774SMarek Vasut return ehci_shutdown_phy(dev, &priv->phy);
1501335e774SMarek Vasut }
1511335e774SMarek Vasut
152907eed2cSSimon Glass static const struct udevice_id ehci_pci_ids[] = {
153907eed2cSSimon Glass { .compatible = "ehci-pci" },
154907eed2cSSimon Glass { }
155907eed2cSSimon Glass };
156907eed2cSSimon Glass
1572b53b078SSimon Glass U_BOOT_DRIVER(ehci_pci) = {
1582b53b078SSimon Glass .name = "ehci_pci",
1592b53b078SSimon Glass .id = UCLASS_USB,
1602b53b078SSimon Glass .probe = ehci_pci_probe,
1611335e774SMarek Vasut .remove = ehci_pci_remove,
162907eed2cSSimon Glass .of_match = ehci_pci_ids,
1632b53b078SSimon Glass .ops = &ehci_usb_ops,
1642b53b078SSimon Glass .platdata_auto_alloc_size = sizeof(struct usb_platdata),
1652b53b078SSimon Glass .priv_auto_alloc_size = sizeof(struct ehci_pci_priv),
1662b53b078SSimon Glass .flags = DM_FLAG_ALLOC_PRIV_DMA,
1672b53b078SSimon Glass };
1682b53b078SSimon Glass
1692b53b078SSimon Glass static struct pci_device_id ehci_pci_supported[] = {
1702b53b078SSimon Glass { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_EHCI, ~0) },
1712b53b078SSimon Glass {},
1722b53b078SSimon Glass };
1732b53b078SSimon Glass
1742b53b078SSimon Glass U_BOOT_PCI_DEVICE(ehci_pci, ehci_pci_supported);
1752b53b078SSimon Glass
176*fd09c205SSven Schwermer #endif /* CONFIG_IS_ENABLED(DM_USB) */
177