1 // SPDX-License-Identifier: GPL-2.0+
2 // PCI interface driver for Loongson SPI Support
3 // Copyright (C) 2023 Loongson Technology Corporation Limited
4 
5 #include <linux/mod_devicetable.h>
6 #include <linux/pci.h>
7 
8 #include "spi-loongson.h"
9 
loongson_spi_pci_register(struct pci_dev * pdev,const struct pci_device_id * ent)10 static int loongson_spi_pci_register(struct pci_dev *pdev,
11 			const struct pci_device_id *ent)
12 {
13 	int ret;
14 	void __iomem *reg_base;
15 	struct device *dev = &pdev->dev;
16 	int pci_bar = 0;
17 
18 	ret = pcim_enable_device(pdev);
19 	if (ret < 0)
20 		return dev_err_probe(dev, ret, "cannot enable pci device\n");
21 
22 	ret = pcim_iomap_regions(pdev, BIT(pci_bar), pci_name(pdev));
23 	if (ret)
24 		return dev_err_probe(dev, ret, "failed to request and remap memory\n");
25 
26 	reg_base = pcim_iomap_table(pdev)[pci_bar];
27 
28 	ret = loongson_spi_init_controller(dev, reg_base);
29 	if (ret)
30 		return dev_err_probe(dev, ret, "failed to initialize controller\n");
31 
32 	return 0;
33 }
34 
35 static struct pci_device_id loongson_spi_devices[] = {
36 	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, 0x7a0b) },
37 	{ PCI_DEVICE(PCI_VENDOR_ID_LOONGSON, 0x7a1b) },
38 	{ }
39 };
40 MODULE_DEVICE_TABLE(pci, loongson_spi_devices);
41 
42 static struct pci_driver loongson_spi_pci_driver = {
43 	.name       = "loongson-spi-pci",
44 	.id_table   = loongson_spi_devices,
45 	.probe      = loongson_spi_pci_register,
46 	.driver	= {
47 		.bus = &pci_bus_type,
48 		.pm = &loongson_spi_dev_pm_ops,
49 	},
50 };
51 module_pci_driver(loongson_spi_pci_driver);
52 
53 MODULE_DESCRIPTION("Loongson spi pci driver");
54 MODULE_LICENSE("GPL");
55 MODULE_IMPORT_NS(SPI_LOONGSON_CORE);
56