xref: /linux/drivers/dma/hsu/pci.c (revision bec897e0a796e3eb5116a2340505ca2da4cecb1b)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22b49e0c5SAndy Shevchenko /*
32b49e0c5SAndy Shevchenko  * PCI driver for the High Speed UART DMA
42b49e0c5SAndy Shevchenko  *
52b49e0c5SAndy Shevchenko  * Copyright (C) 2015 Intel Corporation
62b49e0c5SAndy Shevchenko  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
72b49e0c5SAndy Shevchenko  *
82b49e0c5SAndy Shevchenko  * Partially based on the bits found in drivers/tty/serial/mfd.c.
92b49e0c5SAndy Shevchenko  */
102b49e0c5SAndy Shevchenko 
112b49e0c5SAndy Shevchenko #include <linux/bitops.h>
122b49e0c5SAndy Shevchenko #include <linux/device.h>
132b49e0c5SAndy Shevchenko #include <linux/module.h>
142b49e0c5SAndy Shevchenko #include <linux/pci.h>
152b49e0c5SAndy Shevchenko 
162b49e0c5SAndy Shevchenko #include "hsu.h"
172b49e0c5SAndy Shevchenko 
182b49e0c5SAndy Shevchenko #define HSU_PCI_DMASR		0x00
192b49e0c5SAndy Shevchenko #define HSU_PCI_DMAISR		0x04
202b49e0c5SAndy Shevchenko 
212b49e0c5SAndy Shevchenko #define HSU_PCI_CHAN_OFFSET	0x100
222b49e0c5SAndy Shevchenko 
234831e0d9SAndy Shevchenko #define PCI_DEVICE_ID_INTEL_MFLD_HSU_DMA	0x081e
244831e0d9SAndy Shevchenko #define PCI_DEVICE_ID_INTEL_MRFLD_HSU_DMA	0x1192
254831e0d9SAndy Shevchenko 
262b49e0c5SAndy Shevchenko static irqreturn_t hsu_pci_irq(int irq, void *dev)
272b49e0c5SAndy Shevchenko {
282b49e0c5SAndy Shevchenko 	struct hsu_dma_chip *chip = dev;
292b49e0c5SAndy Shevchenko 	u32 dmaisr;
30c6f82787SChuah, Kim Tatt 	u32 status;
312b49e0c5SAndy Shevchenko 	unsigned short i;
32d2f5a731SAndy Shevchenko 	int ret = 0;
33c6f82787SChuah, Kim Tatt 	int err;
342b49e0c5SAndy Shevchenko 
352b49e0c5SAndy Shevchenko 	dmaisr = readl(chip->regs + HSU_PCI_DMAISR);
364c97ad99SHeikki Krogerus 	for (i = 0; i < chip->hsu->nr_channels; i++) {
37c6f82787SChuah, Kim Tatt 		if (dmaisr & 0x1) {
38c6f82787SChuah, Kim Tatt 			err = hsu_dma_get_status(chip, i, &status);
39c6f82787SChuah, Kim Tatt 			if (err > 0)
40d2f5a731SAndy Shevchenko 				ret |= 1;
41c6f82787SChuah, Kim Tatt 			else if (err == 0)
42c6f82787SChuah, Kim Tatt 				ret |= hsu_dma_do_irq(chip, i, status);
43c6f82787SChuah, Kim Tatt 		}
442b49e0c5SAndy Shevchenko 		dmaisr >>= 1;
452b49e0c5SAndy Shevchenko 	}
462b49e0c5SAndy Shevchenko 
47d2f5a731SAndy Shevchenko 	return IRQ_RETVAL(ret);
482b49e0c5SAndy Shevchenko }
492b49e0c5SAndy Shevchenko 
502b49e0c5SAndy Shevchenko static int hsu_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
512b49e0c5SAndy Shevchenko {
522b49e0c5SAndy Shevchenko 	struct hsu_dma_chip *chip;
532b49e0c5SAndy Shevchenko 	int ret;
542b49e0c5SAndy Shevchenko 
552b49e0c5SAndy Shevchenko 	ret = pcim_enable_device(pdev);
562b49e0c5SAndy Shevchenko 	if (ret)
572b49e0c5SAndy Shevchenko 		return ret;
582b49e0c5SAndy Shevchenko 
592b49e0c5SAndy Shevchenko 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
602b49e0c5SAndy Shevchenko 	if (ret) {
612b49e0c5SAndy Shevchenko 		dev_err(&pdev->dev, "I/O memory remapping failed\n");
622b49e0c5SAndy Shevchenko 		return ret;
632b49e0c5SAndy Shevchenko 	}
642b49e0c5SAndy Shevchenko 
652b49e0c5SAndy Shevchenko 	pci_set_master(pdev);
662b49e0c5SAndy Shevchenko 	pci_try_set_mwi(pdev);
672b49e0c5SAndy Shevchenko 
68*bec897e0SQing Wang 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
692b49e0c5SAndy Shevchenko 	if (ret)
702b49e0c5SAndy Shevchenko 		return ret;
712b49e0c5SAndy Shevchenko 
722b49e0c5SAndy Shevchenko 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
732b49e0c5SAndy Shevchenko 	if (!chip)
742b49e0c5SAndy Shevchenko 		return -ENOMEM;
752b49e0c5SAndy Shevchenko 
76e9bb8a9dSAndy Shevchenko 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
77e9bb8a9dSAndy Shevchenko 	if (ret < 0)
78e9bb8a9dSAndy Shevchenko 		return ret;
79e9bb8a9dSAndy Shevchenko 
802b49e0c5SAndy Shevchenko 	chip->dev = &pdev->dev;
812b49e0c5SAndy Shevchenko 	chip->regs = pcim_iomap_table(pdev)[0];
822b49e0c5SAndy Shevchenko 	chip->length = pci_resource_len(pdev, 0);
832b49e0c5SAndy Shevchenko 	chip->offset = HSU_PCI_CHAN_OFFSET;
84e9bb8a9dSAndy Shevchenko 	chip->irq = pci_irq_vector(pdev, 0);
852b49e0c5SAndy Shevchenko 
862b49e0c5SAndy Shevchenko 	ret = hsu_dma_probe(chip);
872b49e0c5SAndy Shevchenko 	if (ret)
882b49e0c5SAndy Shevchenko 		return ret;
892b49e0c5SAndy Shevchenko 
902b49e0c5SAndy Shevchenko 	ret = request_irq(chip->irq, hsu_pci_irq, 0, "hsu_dma_pci", chip);
912b49e0c5SAndy Shevchenko 	if (ret)
922b49e0c5SAndy Shevchenko 		goto err_register_irq;
932b49e0c5SAndy Shevchenko 
94035b73b2SFerry Toth 	/*
95035b73b2SFerry Toth 	 * On Intel Tangier B0 and Anniedale the interrupt line, disregarding
96035b73b2SFerry Toth 	 * to have different numbers, is shared between HSU DMA and UART IPs.
97035b73b2SFerry Toth 	 * Thus on such SoCs we are expecting that IRQ handler is called in
98035b73b2SFerry Toth 	 * UART driver only. Instead of handling the spurious interrupt
99035b73b2SFerry Toth 	 * from HSU DMA here and waste CPU time and delay HSU UART interrupt
100035b73b2SFerry Toth 	 * handling, disable the interrupt entirely.
101035b73b2SFerry Toth 	 */
102035b73b2SFerry Toth 	if (pdev->device == PCI_DEVICE_ID_INTEL_MRFLD_HSU_DMA)
103035b73b2SFerry Toth 		disable_irq_nosync(chip->irq);
104035b73b2SFerry Toth 
1052b49e0c5SAndy Shevchenko 	pci_set_drvdata(pdev, chip);
1062b49e0c5SAndy Shevchenko 
1072b49e0c5SAndy Shevchenko 	return 0;
1082b49e0c5SAndy Shevchenko 
1092b49e0c5SAndy Shevchenko err_register_irq:
1102b49e0c5SAndy Shevchenko 	hsu_dma_remove(chip);
1112b49e0c5SAndy Shevchenko 	return ret;
1122b49e0c5SAndy Shevchenko }
1132b49e0c5SAndy Shevchenko 
1142b49e0c5SAndy Shevchenko static void hsu_pci_remove(struct pci_dev *pdev)
1152b49e0c5SAndy Shevchenko {
1162b49e0c5SAndy Shevchenko 	struct hsu_dma_chip *chip = pci_get_drvdata(pdev);
1172b49e0c5SAndy Shevchenko 
1182b49e0c5SAndy Shevchenko 	free_irq(chip->irq, chip);
1192b49e0c5SAndy Shevchenko 	hsu_dma_remove(chip);
1202b49e0c5SAndy Shevchenko }
1212b49e0c5SAndy Shevchenko 
1222b49e0c5SAndy Shevchenko static const struct pci_device_id hsu_pci_id_table[] = {
1234831e0d9SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MFLD_HSU_DMA), 0 },
1244831e0d9SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MRFLD_HSU_DMA), 0 },
1252b49e0c5SAndy Shevchenko 	{ }
1262b49e0c5SAndy Shevchenko };
1272b49e0c5SAndy Shevchenko MODULE_DEVICE_TABLE(pci, hsu_pci_id_table);
1282b49e0c5SAndy Shevchenko 
1292b49e0c5SAndy Shevchenko static struct pci_driver hsu_pci_driver = {
1302b49e0c5SAndy Shevchenko 	.name		= "hsu_dma_pci",
1312b49e0c5SAndy Shevchenko 	.id_table	= hsu_pci_id_table,
1322b49e0c5SAndy Shevchenko 	.probe		= hsu_pci_probe,
1332b49e0c5SAndy Shevchenko 	.remove		= hsu_pci_remove,
1342b49e0c5SAndy Shevchenko };
1352b49e0c5SAndy Shevchenko 
1362b49e0c5SAndy Shevchenko module_pci_driver(hsu_pci_driver);
1372b49e0c5SAndy Shevchenko 
1382b49e0c5SAndy Shevchenko MODULE_LICENSE("GPL v2");
1392b49e0c5SAndy Shevchenko MODULE_DESCRIPTION("High Speed UART DMA PCI driver");
1402b49e0c5SAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
141