Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 2 | /* |
| 3 | * pci.c - DesignWare HS OTG Controller PCI driver |
| 4 | * |
| 5 | * Copyright (C) 2004-2013 Synopsys, Inc. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions, and the following disclaimer, |
| 12 | * without modification. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. The names of the above-listed copyright holders may not be used |
| 17 | * to endorse or promote products derived from this software without |
| 18 | * specific prior written permission. |
| 19 | * |
| 20 | * ALTERNATIVELY, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") as published by the Free Software |
| 22 | * Foundation; either version 2 of the License, or (at your option) any |
| 23 | * later version. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 26 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 27 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 29 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * Provides the initialization and cleanup entry points for the DWC_otg PCI |
| 40 | * driver |
| 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/module.h> |
| 44 | #include <linux/moduleparam.h> |
| 45 | #include <linux/spinlock.h> |
| 46 | #include <linux/interrupt.h> |
| 47 | #include <linux/io.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/pci.h> |
| 50 | #include <linux/usb.h> |
| 51 | |
| 52 | #include <linux/usb/hcd.h> |
| 53 | #include <linux/usb/ch11.h> |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 54 | #include <linux/platform_device.h> |
| 55 | #include <linux/usb/usb_phy_generic.h> |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 56 | |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 57 | #define PCI_PRODUCT_ID_HAPS_HSOTG 0xabc0 |
| 58 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 59 | static const char dwc2_driver_name[] = "dwc2-pci"; |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 60 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 61 | struct dwc2_pci_glue { |
| 62 | struct platform_device *dwc2; |
| 63 | struct platform_device *phy; |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 66 | /** |
Lee Jones | 81d708b | 2021-05-26 14:00:25 +0100 | [diff] [blame] | 67 | * dwc2_pci_remove() - Provides the cleanup entry points for the DWC_otg PCI |
Grigor Tovmasyan | 6fb914d | 2018-05-16 12:04:24 +0400 | [diff] [blame] | 68 | * driver |
| 69 | * |
| 70 | * @pci: The programming view of DWC_otg PCI |
| 71 | */ |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 72 | static void dwc2_pci_remove(struct pci_dev *pci) |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 73 | { |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 74 | struct dwc2_pci_glue *glue = pci_get_drvdata(pci); |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 75 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 76 | platform_device_unregister(glue->dwc2); |
| 77 | usb_phy_generic_unregister(glue->phy); |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 78 | pci_set_drvdata(pci, NULL); |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 79 | } |
| 80 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 81 | static int dwc2_pci_probe(struct pci_dev *pci, |
John Youn | 9da5197 | 2017-01-17 20:30:27 -0800 | [diff] [blame] | 82 | const struct pci_device_id *id) |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 83 | { |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 84 | struct resource res[2]; |
| 85 | struct platform_device *dwc2; |
| 86 | struct platform_device *phy; |
| 87 | int ret; |
| 88 | struct device *dev = &pci->dev; |
| 89 | struct dwc2_pci_glue *glue; |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 90 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 91 | ret = pcim_enable_device(pci); |
| 92 | if (ret) { |
| 93 | dev_err(dev, "failed to enable pci device\n"); |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 94 | return -ENODEV; |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 95 | } |
| 96 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 97 | pci_set_master(pci); |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 98 | |
Vardan Mikayelyan | a127ccc | 2018-01-19 14:45:38 +0400 | [diff] [blame] | 99 | phy = usb_phy_generic_register(); |
| 100 | if (IS_ERR(phy)) { |
| 101 | dev_err(dev, "error registering generic PHY (%ld)\n", |
| 102 | PTR_ERR(phy)); |
| 103 | return PTR_ERR(phy); |
| 104 | } |
| 105 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 106 | dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO); |
| 107 | if (!dwc2) { |
| 108 | dev_err(dev, "couldn't allocate dwc2 device\n"); |
Dan Carpenter | 3c135e8 | 2018-10-18 10:36:47 +0300 | [diff] [blame] | 109 | ret = -ENOMEM; |
Vardan Mikayelyan | ecd29da | 2018-01-19 14:46:30 +0400 | [diff] [blame] | 110 | goto err; |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res)); |
| 114 | |
| 115 | res[0].start = pci_resource_start(pci, 0); |
| 116 | res[0].end = pci_resource_end(pci, 0); |
| 117 | res[0].name = "dwc2"; |
| 118 | res[0].flags = IORESOURCE_MEM; |
| 119 | |
| 120 | res[1].start = pci->irq; |
| 121 | res[1].name = "dwc2"; |
| 122 | res[1].flags = IORESOURCE_IRQ; |
| 123 | |
| 124 | ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res)); |
| 125 | if (ret) { |
| 126 | dev_err(dev, "couldn't add resources to dwc2 device\n"); |
Vardan Mikayelyan | ecd29da | 2018-01-19 14:46:30 +0400 | [diff] [blame] | 127 | goto err; |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | dwc2->dev.parent = dev; |
| 131 | |
Vardan Mikayelyan | fb50aac | 2018-01-19 14:46:04 +0400 | [diff] [blame] | 132 | glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL); |
Wei Yongjun | fe70dce | 2018-03-28 13:41:13 +0000 | [diff] [blame] | 133 | if (!glue) { |
| 134 | ret = -ENOMEM; |
Vardan Mikayelyan | ecd29da | 2018-01-19 14:46:30 +0400 | [diff] [blame] | 135 | goto err; |
Wei Yongjun | fe70dce | 2018-03-28 13:41:13 +0000 | [diff] [blame] | 136 | } |
Vardan Mikayelyan | fb50aac | 2018-01-19 14:46:04 +0400 | [diff] [blame] | 137 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 138 | ret = platform_device_add(dwc2); |
| 139 | if (ret) { |
| 140 | dev_err(dev, "failed to register dwc2 device\n"); |
| 141 | goto err; |
| 142 | } |
| 143 | |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 144 | glue->phy = phy; |
| 145 | glue->dwc2 = dwc2; |
| 146 | pci_set_drvdata(pci, glue); |
| 147 | |
| 148 | return 0; |
| 149 | err: |
| 150 | usb_phy_generic_unregister(phy); |
| 151 | platform_device_put(dwc2); |
| 152 | return ret; |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jingoo Han | 41e043f | 2013-12-03 08:26:00 +0900 | [diff] [blame] | 155 | static const struct pci_device_id dwc2_pci_ids[] = { |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 156 | { |
| 157 | PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, PCI_PRODUCT_ID_HAPS_HSOTG), |
| 158 | }, |
Federico Vaga | 25f73e6 | 2013-05-13 19:54:12 +0200 | [diff] [blame] | 159 | { |
| 160 | PCI_DEVICE(PCI_VENDOR_ID_STMICRO, |
| 161 | PCI_DEVICE_ID_STMICRO_USB_OTG), |
| 162 | }, |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 163 | { /* end: all zeroes */ } |
| 164 | }; |
| 165 | MODULE_DEVICE_TABLE(pci, dwc2_pci_ids); |
| 166 | |
| 167 | static struct pci_driver dwc2_pci_driver = { |
| 168 | .name = dwc2_driver_name, |
| 169 | .id_table = dwc2_pci_ids, |
John Youn | 9024c49 | 2015-03-03 17:17:49 -0800 | [diff] [blame] | 170 | .probe = dwc2_pci_probe, |
| 171 | .remove = dwc2_pci_remove, |
Paul Zimmerman | 99882e3 | 2013-03-11 17:48:01 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | module_pci_driver(dwc2_pci_driver); |
| 175 | |
| 176 | MODULE_DESCRIPTION("DESIGNWARE HS OTG PCI Bus Glue"); |
| 177 | MODULE_AUTHOR("Synopsys, Inc."); |
| 178 | MODULE_LICENSE("Dual BSD/GPL"); |