15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
28e22978cSAlexander Shishkin /*
38e22978cSAlexander Shishkin * ci_hdrc_pci.c - MIPS USB IP core family device controller
48e22978cSAlexander Shishkin *
58e22978cSAlexander Shishkin * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
68e22978cSAlexander Shishkin *
78e22978cSAlexander Shishkin * Author: David Lopo
88e22978cSAlexander Shishkin */
98e22978cSAlexander Shishkin
108e22978cSAlexander Shishkin #include <linux/platform_device.h>
118e22978cSAlexander Shishkin #include <linux/module.h>
128e22978cSAlexander Shishkin #include <linux/pci.h>
138e22978cSAlexander Shishkin #include <linux/interrupt.h>
148e22978cSAlexander Shishkin #include <linux/usb/gadget.h>
158e22978cSAlexander Shishkin #include <linux/usb/chipidea.h>
16ba1aff67SAndy Shevchenko #include <linux/usb/usb_phy_generic.h>
178e22978cSAlexander Shishkin
188e22978cSAlexander Shishkin /* driver name */
198e22978cSAlexander Shishkin #define UDC_DRIVER_NAME "ci_hdrc_pci"
208e22978cSAlexander Shishkin
21ba1aff67SAndy Shevchenko struct ci_hdrc_pci {
22ba1aff67SAndy Shevchenko struct platform_device *ci;
23ba1aff67SAndy Shevchenko struct platform_device *phy;
24ba1aff67SAndy Shevchenko };
25ba1aff67SAndy Shevchenko
268e22978cSAlexander Shishkin /******************************************************************************
278e22978cSAlexander Shishkin * PCI block
288e22978cSAlexander Shishkin *****************************************************************************/
298e22978cSAlexander Shishkin static struct ci_hdrc_platform_data pci_platdata = {
308e22978cSAlexander Shishkin .name = UDC_DRIVER_NAME,
318e22978cSAlexander Shishkin .capoffset = DEF_CAPOFFSET,
328e22978cSAlexander Shishkin };
338e22978cSAlexander Shishkin
348e22978cSAlexander Shishkin static struct ci_hdrc_platform_data langwell_pci_platdata = {
358e22978cSAlexander Shishkin .name = UDC_DRIVER_NAME,
368e22978cSAlexander Shishkin .capoffset = 0,
378e22978cSAlexander Shishkin };
388e22978cSAlexander Shishkin
398e22978cSAlexander Shishkin static struct ci_hdrc_platform_data penwell_pci_platdata = {
408e22978cSAlexander Shishkin .name = UDC_DRIVER_NAME,
418e22978cSAlexander Shishkin .capoffset = 0,
428e22978cSAlexander Shishkin .power_budget = 200,
438e22978cSAlexander Shishkin };
448e22978cSAlexander Shishkin
458e22978cSAlexander Shishkin /**
468e22978cSAlexander Shishkin * ci_hdrc_pci_probe: PCI probe
478e22978cSAlexander Shishkin * @pdev: USB device controller being probed
488e22978cSAlexander Shishkin * @id: PCI hotplug ID connecting controller to UDC framework
498e22978cSAlexander Shishkin *
508e22978cSAlexander Shishkin * This function returns an error code
518e22978cSAlexander Shishkin * Allocates basic PCI resources for this USB device controller, and then
528e22978cSAlexander Shishkin * invokes the udc_probe() method to start the UDC associated with it
538e22978cSAlexander Shishkin */
ci_hdrc_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)548e22978cSAlexander Shishkin static int ci_hdrc_pci_probe(struct pci_dev *pdev,
558e22978cSAlexander Shishkin const struct pci_device_id *id)
568e22978cSAlexander Shishkin {
578e22978cSAlexander Shishkin struct ci_hdrc_platform_data *platdata = (void *)id->driver_data;
58ba1aff67SAndy Shevchenko struct ci_hdrc_pci *ci;
598e22978cSAlexander Shishkin struct resource res[3];
608e22978cSAlexander Shishkin int retval = 0, nres = 2;
618e22978cSAlexander Shishkin
628e22978cSAlexander Shishkin if (!platdata) {
638e22978cSAlexander Shishkin dev_err(&pdev->dev, "device doesn't provide driver data\n");
648e22978cSAlexander Shishkin return -ENODEV;
658e22978cSAlexander Shishkin }
668e22978cSAlexander Shishkin
67ba1aff67SAndy Shevchenko ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
68ba1aff67SAndy Shevchenko if (!ci)
69ba1aff67SAndy Shevchenko return -ENOMEM;
70ba1aff67SAndy Shevchenko
718e22978cSAlexander Shishkin retval = pcim_enable_device(pdev);
728e22978cSAlexander Shishkin if (retval)
738e22978cSAlexander Shishkin return retval;
748e22978cSAlexander Shishkin
758e22978cSAlexander Shishkin if (!pdev->irq) {
768e22978cSAlexander Shishkin dev_err(&pdev->dev, "No IRQ, check BIOS/PCI setup!");
778e22978cSAlexander Shishkin return -ENODEV;
788e22978cSAlexander Shishkin }
798e22978cSAlexander Shishkin
808e22978cSAlexander Shishkin pci_set_master(pdev);
818e22978cSAlexander Shishkin pci_try_set_mwi(pdev);
828e22978cSAlexander Shishkin
83ba1aff67SAndy Shevchenko /* register a nop PHY */
84ba1aff67SAndy Shevchenko ci->phy = usb_phy_generic_register();
858c0614caSWei Yongjun if (IS_ERR(ci->phy))
868c0614caSWei Yongjun return PTR_ERR(ci->phy);
87ba1aff67SAndy Shevchenko
888e22978cSAlexander Shishkin memset(res, 0, sizeof(res));
898e22978cSAlexander Shishkin res[0].start = pci_resource_start(pdev, 0);
908e22978cSAlexander Shishkin res[0].end = pci_resource_end(pdev, 0);
918e22978cSAlexander Shishkin res[0].flags = IORESOURCE_MEM;
928e22978cSAlexander Shishkin res[1].start = pdev->irq;
938e22978cSAlexander Shishkin res[1].flags = IORESOURCE_IRQ;
948e22978cSAlexander Shishkin
95ba1aff67SAndy Shevchenko ci->ci = ci_hdrc_add_device(&pdev->dev, res, nres, platdata);
96ba1aff67SAndy Shevchenko if (IS_ERR(ci->ci)) {
978e22978cSAlexander Shishkin dev_err(&pdev->dev, "ci_hdrc_add_device failed!\n");
98ba1aff67SAndy Shevchenko usb_phy_generic_unregister(ci->phy);
99ba1aff67SAndy Shevchenko return PTR_ERR(ci->ci);
1008e22978cSAlexander Shishkin }
1018e22978cSAlexander Shishkin
102ba1aff67SAndy Shevchenko pci_set_drvdata(pdev, ci);
1038e22978cSAlexander Shishkin
1048e22978cSAlexander Shishkin return 0;
1058e22978cSAlexander Shishkin }
1068e22978cSAlexander Shishkin
1078e22978cSAlexander Shishkin /**
1088e22978cSAlexander Shishkin * ci_hdrc_pci_remove: PCI remove
1098e22978cSAlexander Shishkin * @pdev: USB Device Controller being removed
1108e22978cSAlexander Shishkin *
1118e22978cSAlexander Shishkin * Reverses the effect of ci_hdrc_pci_probe(),
1128e22978cSAlexander Shishkin * first invoking the udc_remove() and then releases
1138e22978cSAlexander Shishkin * all PCI resources allocated for this USB device controller
1148e22978cSAlexander Shishkin */
ci_hdrc_pci_remove(struct pci_dev * pdev)1158e22978cSAlexander Shishkin static void ci_hdrc_pci_remove(struct pci_dev *pdev)
1168e22978cSAlexander Shishkin {
117ba1aff67SAndy Shevchenko struct ci_hdrc_pci *ci = pci_get_drvdata(pdev);
1188e22978cSAlexander Shishkin
119ba1aff67SAndy Shevchenko ci_hdrc_remove_device(ci->ci);
120ba1aff67SAndy Shevchenko usb_phy_generic_unregister(ci->phy);
1218e22978cSAlexander Shishkin }
1228e22978cSAlexander Shishkin
123*f2db5f20SLee Jones /*
1248e22978cSAlexander Shishkin * PCI device table
1258e22978cSAlexander Shishkin * PCI device structure
1268e22978cSAlexander Shishkin *
1278e22978cSAlexander Shishkin * Check "pci.h" for details
128518ca8d9SAndy Shevchenko *
129518ca8d9SAndy Shevchenko * Note: ehci-pci driver may try to probe the device first. You have to add an
130518ca8d9SAndy Shevchenko * ID to the bypass_pci_id_table in ehci-pci driver to prevent this.
1318e22978cSAlexander Shishkin */
1324e065b8bSJingoo Han static const struct pci_device_id ci_hdrc_pci_id_table[] = {
1338e22978cSAlexander Shishkin {
1348e22978cSAlexander Shishkin PCI_DEVICE(0x153F, 0x1004),
1358e22978cSAlexander Shishkin .driver_data = (kernel_ulong_t)&pci_platdata,
1368e22978cSAlexander Shishkin },
1378e22978cSAlexander Shishkin {
1388e22978cSAlexander Shishkin PCI_DEVICE(0x153F, 0x1006),
1398e22978cSAlexander Shishkin .driver_data = (kernel_ulong_t)&pci_platdata,
1408e22978cSAlexander Shishkin },
1418e22978cSAlexander Shishkin {
14246b95a1dSSergei Shtylyov PCI_VDEVICE(INTEL, 0x0811),
1438e22978cSAlexander Shishkin .driver_data = (kernel_ulong_t)&langwell_pci_platdata,
1448e22978cSAlexander Shishkin },
1458e22978cSAlexander Shishkin {
14646b95a1dSSergei Shtylyov PCI_VDEVICE(INTEL, 0x0829),
1478e22978cSAlexander Shishkin .driver_data = (kernel_ulong_t)&penwell_pci_platdata,
1488e22978cSAlexander Shishkin },
149a214339dSDavid Cohen {
150a214339dSDavid Cohen /* Intel Clovertrail */
15146b95a1dSSergei Shtylyov PCI_VDEVICE(INTEL, 0xe006),
152a214339dSDavid Cohen .driver_data = (kernel_ulong_t)&penwell_pci_platdata,
153a214339dSDavid Cohen },
154a214339dSDavid Cohen { 0 } /* end: all zeroes */
1558e22978cSAlexander Shishkin };
1568e22978cSAlexander Shishkin MODULE_DEVICE_TABLE(pci, ci_hdrc_pci_id_table);
1578e22978cSAlexander Shishkin
1588e22978cSAlexander Shishkin static struct pci_driver ci_hdrc_pci_driver = {
1598e22978cSAlexander Shishkin .name = UDC_DRIVER_NAME,
1608e22978cSAlexander Shishkin .id_table = ci_hdrc_pci_id_table,
1618e22978cSAlexander Shishkin .probe = ci_hdrc_pci_probe,
1628e22978cSAlexander Shishkin .remove = ci_hdrc_pci_remove,
1638e22978cSAlexander Shishkin };
1648e22978cSAlexander Shishkin
1658e22978cSAlexander Shishkin module_pci_driver(ci_hdrc_pci_driver);
1668e22978cSAlexander Shishkin
1678e22978cSAlexander Shishkin MODULE_AUTHOR("MIPS - David Lopo <dlopo@chipidea.mips.com>");
1688e22978cSAlexander Shishkin MODULE_DESCRIPTION("MIPS CI13XXX USB Peripheral Controller");
1698e22978cSAlexander Shishkin MODULE_LICENSE("GPL");
1708e22978cSAlexander Shishkin MODULE_ALIAS("platform:ci13xxx_pci");
171