15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2197ba5f4SPaul Zimmerman /*
3197ba5f4SPaul Zimmerman * pci.c - DesignWare HS OTG Controller PCI driver
4197ba5f4SPaul Zimmerman *
5197ba5f4SPaul Zimmerman * Copyright (C) 2004-2013 Synopsys, Inc.
6197ba5f4SPaul Zimmerman */
7197ba5f4SPaul Zimmerman
8197ba5f4SPaul Zimmerman /*
9197ba5f4SPaul Zimmerman * Provides the initialization and cleanup entry points for the DWC_otg PCI
10197ba5f4SPaul Zimmerman * driver
11197ba5f4SPaul Zimmerman */
12197ba5f4SPaul Zimmerman #include <linux/kernel.h>
13197ba5f4SPaul Zimmerman #include <linux/module.h>
14197ba5f4SPaul Zimmerman #include <linux/moduleparam.h>
15197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
16197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
17197ba5f4SPaul Zimmerman #include <linux/io.h>
18197ba5f4SPaul Zimmerman #include <linux/slab.h>
19197ba5f4SPaul Zimmerman #include <linux/pci.h>
20197ba5f4SPaul Zimmerman #include <linux/usb.h>
21197ba5f4SPaul Zimmerman
22197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
23197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
249024c495SJohn Youn #include <linux/platform_device.h>
259024c495SJohn Youn #include <linux/usb/usb_phy_generic.h>
26197ba5f4SPaul Zimmerman
27*e16d5f14SYinbo Zhu #include "core.h"
28197ba5f4SPaul Zimmerman
299024c495SJohn Youn static const char dwc2_driver_name[] = "dwc2-pci";
30197ba5f4SPaul Zimmerman
319024c495SJohn Youn struct dwc2_pci_glue {
329024c495SJohn Youn struct platform_device *dwc2;
339024c495SJohn Youn struct platform_device *phy;
34197ba5f4SPaul Zimmerman };
35197ba5f4SPaul Zimmerman
366fb914d7SGrigor Tovmasyan /**
3781d708bcSLee Jones * dwc2_pci_remove() - Provides the cleanup entry points for the DWC_otg PCI
386fb914d7SGrigor Tovmasyan * driver
396fb914d7SGrigor Tovmasyan *
406fb914d7SGrigor Tovmasyan * @pci: The programming view of DWC_otg PCI
416fb914d7SGrigor Tovmasyan */
dwc2_pci_remove(struct pci_dev * pci)429024c495SJohn Youn static void dwc2_pci_remove(struct pci_dev *pci)
43197ba5f4SPaul Zimmerman {
449024c495SJohn Youn struct dwc2_pci_glue *glue = pci_get_drvdata(pci);
45197ba5f4SPaul Zimmerman
469024c495SJohn Youn platform_device_unregister(glue->dwc2);
479024c495SJohn Youn usb_phy_generic_unregister(glue->phy);
489024c495SJohn Youn pci_set_drvdata(pci, NULL);
49197ba5f4SPaul Zimmerman }
50197ba5f4SPaul Zimmerman
dwc2_pci_probe(struct pci_dev * pci,const struct pci_device_id * id)519024c495SJohn Youn static int dwc2_pci_probe(struct pci_dev *pci,
52197ba5f4SPaul Zimmerman const struct pci_device_id *id)
53197ba5f4SPaul Zimmerman {
549024c495SJohn Youn struct resource res[2];
559024c495SJohn Youn struct platform_device *dwc2;
569024c495SJohn Youn struct platform_device *phy;
579024c495SJohn Youn int ret;
589024c495SJohn Youn struct device *dev = &pci->dev;
599024c495SJohn Youn struct dwc2_pci_glue *glue;
60197ba5f4SPaul Zimmerman
619024c495SJohn Youn ret = pcim_enable_device(pci);
629024c495SJohn Youn if (ret) {
639024c495SJohn Youn dev_err(dev, "failed to enable pci device\n");
64197ba5f4SPaul Zimmerman return -ENODEV;
65197ba5f4SPaul Zimmerman }
66197ba5f4SPaul Zimmerman
679024c495SJohn Youn pci_set_master(pci);
68197ba5f4SPaul Zimmerman
69a127cccdSVardan Mikayelyan phy = usb_phy_generic_register();
70a127cccdSVardan Mikayelyan if (IS_ERR(phy)) {
71a127cccdSVardan Mikayelyan dev_err(dev, "error registering generic PHY (%ld)\n",
72a127cccdSVardan Mikayelyan PTR_ERR(phy));
73a127cccdSVardan Mikayelyan return PTR_ERR(phy);
74a127cccdSVardan Mikayelyan }
75a127cccdSVardan Mikayelyan
769024c495SJohn Youn dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
779024c495SJohn Youn if (!dwc2) {
789024c495SJohn Youn dev_err(dev, "couldn't allocate dwc2 device\n");
793c135e89SDan Carpenter ret = -ENOMEM;
80ecd29dabSVardan Mikayelyan goto err;
819024c495SJohn Youn }
829024c495SJohn Youn
839024c495SJohn Youn memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
849024c495SJohn Youn
859024c495SJohn Youn res[0].start = pci_resource_start(pci, 0);
869024c495SJohn Youn res[0].end = pci_resource_end(pci, 0);
879024c495SJohn Youn res[0].name = "dwc2";
889024c495SJohn Youn res[0].flags = IORESOURCE_MEM;
899024c495SJohn Youn
909024c495SJohn Youn res[1].start = pci->irq;
919024c495SJohn Youn res[1].name = "dwc2";
929024c495SJohn Youn res[1].flags = IORESOURCE_IRQ;
939024c495SJohn Youn
949024c495SJohn Youn ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
959024c495SJohn Youn if (ret) {
969024c495SJohn Youn dev_err(dev, "couldn't add resources to dwc2 device\n");
97ecd29dabSVardan Mikayelyan goto err;
989024c495SJohn Youn }
999024c495SJohn Youn
1009024c495SJohn Youn dwc2->dev.parent = dev;
1019024c495SJohn Youn
102fb50aacdSVardan Mikayelyan glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
103fe70dce9SWei Yongjun if (!glue) {
104fe70dce9SWei Yongjun ret = -ENOMEM;
105ecd29dabSVardan Mikayelyan goto err;
106fe70dce9SWei Yongjun }
107fb50aacdSVardan Mikayelyan
1089024c495SJohn Youn ret = platform_device_add(dwc2);
1099024c495SJohn Youn if (ret) {
1109024c495SJohn Youn dev_err(dev, "failed to register dwc2 device\n");
1119024c495SJohn Youn goto err;
1129024c495SJohn Youn }
1139024c495SJohn Youn
1149024c495SJohn Youn glue->phy = phy;
1159024c495SJohn Youn glue->dwc2 = dwc2;
1169024c495SJohn Youn pci_set_drvdata(pci, glue);
1179024c495SJohn Youn
1189024c495SJohn Youn return 0;
1199024c495SJohn Youn err:
1209024c495SJohn Youn usb_phy_generic_unregister(phy);
1219024c495SJohn Youn platform_device_put(dwc2);
1229024c495SJohn Youn return ret;
123197ba5f4SPaul Zimmerman }
124197ba5f4SPaul Zimmerman
125197ba5f4SPaul Zimmerman static struct pci_driver dwc2_pci_driver = {
126197ba5f4SPaul Zimmerman .name = dwc2_driver_name,
127197ba5f4SPaul Zimmerman .id_table = dwc2_pci_ids,
1289024c495SJohn Youn .probe = dwc2_pci_probe,
1299024c495SJohn Youn .remove = dwc2_pci_remove,
130197ba5f4SPaul Zimmerman };
131197ba5f4SPaul Zimmerman
132197ba5f4SPaul Zimmerman module_pci_driver(dwc2_pci_driver);
133197ba5f4SPaul Zimmerman
134197ba5f4SPaul Zimmerman MODULE_DESCRIPTION("DESIGNWARE HS OTG PCI Bus Glue");
135197ba5f4SPaul Zimmerman MODULE_AUTHOR("Synopsys, Inc.");
136197ba5f4SPaul Zimmerman MODULE_LICENSE("Dual BSD/GPL");
137