1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* Copyright 2019 NXP */ 3 4 #include <linux/module.h> 5 #include <linux/of.h> 6 #include <linux/fsl/ptp_qoriq.h> 7 8 #include "enetc.h" 9 10 int enetc_phc_index = -1; 11 EXPORT_SYMBOL(enetc_phc_index); 12 13 static struct ptp_clock_info enetc_ptp_caps = { 14 .owner = THIS_MODULE, 15 .name = "ENETC PTP clock", 16 .max_adj = 512000, 17 .n_alarm = 0, 18 .n_ext_ts = 2, 19 .n_per_out = 0, 20 .n_pins = 0, 21 .pps = 1, 22 .adjfine = ptp_qoriq_adjfine, 23 .adjtime = ptp_qoriq_adjtime, 24 .gettime64 = ptp_qoriq_gettime, 25 .settime64 = ptp_qoriq_settime, 26 .enable = ptp_qoriq_enable, 27 }; 28 29 static int enetc_ptp_probe(struct pci_dev *pdev, 30 const struct pci_device_id *ent) 31 { 32 struct ptp_qoriq *ptp_qoriq; 33 void __iomem *base; 34 int err, len, n; 35 36 if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) { 37 dev_info(&pdev->dev, "device is disabled, skipping\n"); 38 return -ENODEV; 39 } 40 41 err = pci_enable_device_mem(pdev); 42 if (err) 43 return dev_err_probe(&pdev->dev, err, "device enable failed\n"); 44 45 /* set up for high or low dma */ 46 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 47 if (err) { 48 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 49 if (err) { 50 dev_err(&pdev->dev, 51 "DMA configuration failed: 0x%x\n", err); 52 goto err_dma; 53 } 54 } 55 56 err = pci_request_mem_regions(pdev, KBUILD_MODNAME); 57 if (err) { 58 dev_err(&pdev->dev, "pci_request_regions failed err=%d\n", err); 59 goto err_pci_mem_reg; 60 } 61 62 pci_set_master(pdev); 63 64 ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL); 65 if (!ptp_qoriq) { 66 err = -ENOMEM; 67 goto err_alloc_ptp; 68 } 69 70 len = pci_resource_len(pdev, ENETC_BAR_REGS); 71 72 base = ioremap(pci_resource_start(pdev, ENETC_BAR_REGS), len); 73 if (!base) { 74 err = -ENXIO; 75 dev_err(&pdev->dev, "ioremap() failed\n"); 76 goto err_ioremap; 77 } 78 79 /* Allocate 1 interrupt */ 80 n = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSIX); 81 if (n != 1) { 82 err = -EPERM; 83 goto err_irq_vectors; 84 } 85 86 ptp_qoriq->irq = pci_irq_vector(pdev, 0); 87 88 err = request_irq(ptp_qoriq->irq, ptp_qoriq_isr, 0, DRIVER, ptp_qoriq); 89 if (err) { 90 dev_err(&pdev->dev, "request_irq() failed!\n"); 91 goto err_irq; 92 } 93 94 ptp_qoriq->dev = &pdev->dev; 95 96 err = ptp_qoriq_init(ptp_qoriq, base, &enetc_ptp_caps); 97 if (err) 98 goto err_no_clock; 99 100 enetc_phc_index = ptp_qoriq->phc_index; 101 pci_set_drvdata(pdev, ptp_qoriq); 102 103 return 0; 104 105 err_no_clock: 106 free_irq(ptp_qoriq->irq, ptp_qoriq); 107 err_irq: 108 pci_free_irq_vectors(pdev); 109 err_irq_vectors: 110 iounmap(base); 111 err_ioremap: 112 kfree(ptp_qoriq); 113 err_alloc_ptp: 114 pci_release_mem_regions(pdev); 115 err_pci_mem_reg: 116 err_dma: 117 pci_disable_device(pdev); 118 119 return err; 120 } 121 122 static void enetc_ptp_remove(struct pci_dev *pdev) 123 { 124 struct ptp_qoriq *ptp_qoriq = pci_get_drvdata(pdev); 125 126 enetc_phc_index = -1; 127 ptp_qoriq_free(ptp_qoriq); 128 pci_free_irq_vectors(pdev); 129 kfree(ptp_qoriq); 130 131 pci_release_mem_regions(pdev); 132 pci_disable_device(pdev); 133 } 134 135 static const struct pci_device_id enetc_ptp_id_table[] = { 136 { PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_PTP) }, 137 { 0, } /* End of table. */ 138 }; 139 MODULE_DEVICE_TABLE(pci, enetc_ptp_id_table); 140 141 static struct pci_driver enetc_ptp_driver = { 142 .name = KBUILD_MODNAME, 143 .id_table = enetc_ptp_id_table, 144 .probe = enetc_ptp_probe, 145 .remove = enetc_ptp_remove, 146 }; 147 module_pci_driver(enetc_ptp_driver); 148 149 MODULE_DESCRIPTION("ENETC PTP clock driver"); 150 MODULE_LICENSE("Dual BSD/GPL"); 151