xref: /linux/drivers/dma/hsu/pci.c (revision e9bb8a9df316a2480d316af7b242f40cba3b69b6)
12b49e0c5SAndy Shevchenko /*
22b49e0c5SAndy Shevchenko  * PCI driver for the High Speed UART DMA
32b49e0c5SAndy Shevchenko  *
42b49e0c5SAndy Shevchenko  * Copyright (C) 2015 Intel Corporation
52b49e0c5SAndy Shevchenko  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
62b49e0c5SAndy Shevchenko  *
72b49e0c5SAndy Shevchenko  * Partially based on the bits found in drivers/tty/serial/mfd.c.
82b49e0c5SAndy Shevchenko  *
92b49e0c5SAndy Shevchenko  * This program is free software; you can redistribute it and/or modify
102b49e0c5SAndy Shevchenko  * it under the terms of the GNU General Public License version 2 as
112b49e0c5SAndy Shevchenko  * published by the Free Software Foundation.
122b49e0c5SAndy Shevchenko  */
132b49e0c5SAndy Shevchenko 
142b49e0c5SAndy Shevchenko #include <linux/bitops.h>
152b49e0c5SAndy Shevchenko #include <linux/device.h>
162b49e0c5SAndy Shevchenko #include <linux/module.h>
172b49e0c5SAndy Shevchenko #include <linux/pci.h>
182b49e0c5SAndy Shevchenko 
192b49e0c5SAndy Shevchenko #include "hsu.h"
202b49e0c5SAndy Shevchenko 
212b49e0c5SAndy Shevchenko #define HSU_PCI_DMASR		0x00
222b49e0c5SAndy Shevchenko #define HSU_PCI_DMAISR		0x04
232b49e0c5SAndy Shevchenko 
242b49e0c5SAndy Shevchenko #define HSU_PCI_CHAN_OFFSET	0x100
252b49e0c5SAndy 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 
682b49e0c5SAndy Shevchenko 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
692b49e0c5SAndy Shevchenko 	if (ret)
702b49e0c5SAndy Shevchenko 		return ret;
712b49e0c5SAndy Shevchenko 
722b49e0c5SAndy Shevchenko 	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
732b49e0c5SAndy Shevchenko 	if (ret)
742b49e0c5SAndy Shevchenko 		return ret;
752b49e0c5SAndy Shevchenko 
762b49e0c5SAndy Shevchenko 	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
772b49e0c5SAndy Shevchenko 	if (!chip)
782b49e0c5SAndy Shevchenko 		return -ENOMEM;
792b49e0c5SAndy Shevchenko 
80*e9bb8a9dSAndy Shevchenko 	ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
81*e9bb8a9dSAndy Shevchenko 	if (ret < 0)
82*e9bb8a9dSAndy Shevchenko 		return ret;
83*e9bb8a9dSAndy Shevchenko 
842b49e0c5SAndy Shevchenko 	chip->dev = &pdev->dev;
852b49e0c5SAndy Shevchenko 	chip->regs = pcim_iomap_table(pdev)[0];
862b49e0c5SAndy Shevchenko 	chip->length = pci_resource_len(pdev, 0);
872b49e0c5SAndy Shevchenko 	chip->offset = HSU_PCI_CHAN_OFFSET;
88*e9bb8a9dSAndy Shevchenko 	chip->irq = pci_irq_vector(pdev, 0);
892b49e0c5SAndy Shevchenko 
902b49e0c5SAndy Shevchenko 	ret = hsu_dma_probe(chip);
912b49e0c5SAndy Shevchenko 	if (ret)
922b49e0c5SAndy Shevchenko 		return ret;
932b49e0c5SAndy Shevchenko 
942b49e0c5SAndy Shevchenko 	ret = request_irq(chip->irq, hsu_pci_irq, 0, "hsu_dma_pci", chip);
952b49e0c5SAndy Shevchenko 	if (ret)
962b49e0c5SAndy Shevchenko 		goto err_register_irq;
972b49e0c5SAndy Shevchenko 
982b49e0c5SAndy Shevchenko 	pci_set_drvdata(pdev, chip);
992b49e0c5SAndy Shevchenko 
1002b49e0c5SAndy Shevchenko 	return 0;
1012b49e0c5SAndy Shevchenko 
1022b49e0c5SAndy Shevchenko err_register_irq:
1032b49e0c5SAndy Shevchenko 	hsu_dma_remove(chip);
1042b49e0c5SAndy Shevchenko 	return ret;
1052b49e0c5SAndy Shevchenko }
1062b49e0c5SAndy Shevchenko 
1072b49e0c5SAndy Shevchenko static void hsu_pci_remove(struct pci_dev *pdev)
1082b49e0c5SAndy Shevchenko {
1092b49e0c5SAndy Shevchenko 	struct hsu_dma_chip *chip = pci_get_drvdata(pdev);
1102b49e0c5SAndy Shevchenko 
1112b49e0c5SAndy Shevchenko 	free_irq(chip->irq, chip);
1122b49e0c5SAndy Shevchenko 	hsu_dma_remove(chip);
1132b49e0c5SAndy Shevchenko }
1142b49e0c5SAndy Shevchenko 
1152b49e0c5SAndy Shevchenko static const struct pci_device_id hsu_pci_id_table[] = {
1162b49e0c5SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, 0x081e), 0 },
117594eb4a4SAndy Shevchenko 	{ PCI_VDEVICE(INTEL, 0x1192), 0 },
1182b49e0c5SAndy Shevchenko 	{ }
1192b49e0c5SAndy Shevchenko };
1202b49e0c5SAndy Shevchenko MODULE_DEVICE_TABLE(pci, hsu_pci_id_table);
1212b49e0c5SAndy Shevchenko 
1222b49e0c5SAndy Shevchenko static struct pci_driver hsu_pci_driver = {
1232b49e0c5SAndy Shevchenko 	.name		= "hsu_dma_pci",
1242b49e0c5SAndy Shevchenko 	.id_table	= hsu_pci_id_table,
1252b49e0c5SAndy Shevchenko 	.probe		= hsu_pci_probe,
1262b49e0c5SAndy Shevchenko 	.remove		= hsu_pci_remove,
1272b49e0c5SAndy Shevchenko };
1282b49e0c5SAndy Shevchenko 
1292b49e0c5SAndy Shevchenko module_pci_driver(hsu_pci_driver);
1302b49e0c5SAndy Shevchenko 
1312b49e0c5SAndy Shevchenko MODULE_LICENSE("GPL v2");
1322b49e0c5SAndy Shevchenko MODULE_DESCRIPTION("High Speed UART DMA PCI driver");
1332b49e0c5SAndy Shevchenko MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
134