xref: /linux/drivers/misc/cardreader/alcor_pci.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Oleksij Rempel <linux@rempel-privat.de>
4  *
5  * Driver for Alcor Micro AU6601 and AU6621 controllers
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/irq.h>
12 #include <linux/mfd/core.h>
13 #include <linux/module.h>
14 #include <linux/pci.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm.h>
17 
18 #include <linux/alcor_pci.h>
19 
20 static DEFINE_IDA(alcor_pci_idr);
21 
22 static struct mfd_cell alcor_pci_cells[] = {
23 	[ALCOR_SD_CARD] = {
24 		.name = DRV_NAME_ALCOR_PCI_SDMMC,
25 	},
26 	[ALCOR_MS_CARD] = {
27 		.name = DRV_NAME_ALCOR_PCI_MS,
28 	},
29 };
30 
31 static const struct alcor_dev_cfg alcor_cfg = {
32 	.dma = 0,
33 };
34 
35 static const struct alcor_dev_cfg au6621_cfg = {
36 	.dma = 1,
37 };
38 
39 static const struct alcor_dev_cfg au6625_cfg = {
40 	.dma = 0,
41 };
42 
43 static const struct pci_device_id pci_ids[] = {
44 	{ PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6601),
45 		.driver_data = (kernel_ulong_t)&alcor_cfg },
46 	{ PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6621),
47 		.driver_data = (kernel_ulong_t)&au6621_cfg },
48 	{ PCI_DEVICE(PCI_ID_ALCOR_MICRO, PCI_ID_AU6625),
49 		.driver_data = (kernel_ulong_t)&au6625_cfg },
50 	{},
51 };
52 MODULE_DEVICE_TABLE(pci, pci_ids);
53 
54 void alcor_write8(struct alcor_pci_priv *priv, u8 val, unsigned int addr)
55 {
56 	writeb(val, priv->iobase + addr);
57 }
58 EXPORT_SYMBOL_GPL(alcor_write8);
59 
60 void alcor_write16(struct alcor_pci_priv *priv, u16 val, unsigned int addr)
61 {
62 	writew(val, priv->iobase + addr);
63 }
64 EXPORT_SYMBOL_GPL(alcor_write16);
65 
66 void alcor_write32(struct alcor_pci_priv *priv, u32 val, unsigned int addr)
67 {
68 	writel(val, priv->iobase + addr);
69 }
70 EXPORT_SYMBOL_GPL(alcor_write32);
71 
72 void alcor_write32be(struct alcor_pci_priv *priv, u32 val, unsigned int addr)
73 {
74 	iowrite32be(val, priv->iobase + addr);
75 }
76 EXPORT_SYMBOL_GPL(alcor_write32be);
77 
78 u8 alcor_read8(struct alcor_pci_priv *priv, unsigned int addr)
79 {
80 	return readb(priv->iobase + addr);
81 }
82 EXPORT_SYMBOL_GPL(alcor_read8);
83 
84 u32 alcor_read32(struct alcor_pci_priv *priv, unsigned int addr)
85 {
86 	return readl(priv->iobase + addr);
87 }
88 EXPORT_SYMBOL_GPL(alcor_read32);
89 
90 u32 alcor_read32be(struct alcor_pci_priv *priv, unsigned int addr)
91 {
92 	return ioread32be(priv->iobase + addr);
93 }
94 EXPORT_SYMBOL_GPL(alcor_read32be);
95 
96 static int alcor_pci_probe(struct pci_dev *pdev,
97 			   const struct pci_device_id *ent)
98 {
99 	struct alcor_dev_cfg *cfg;
100 	struct alcor_pci_priv *priv;
101 	int ret, i, bar = 0;
102 
103 	cfg = (void *)ent->driver_data;
104 
105 	ret = pcim_enable_device(pdev);
106 	if (ret)
107 		return ret;
108 
109 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
110 	if (!priv)
111 		return -ENOMEM;
112 
113 	ret = ida_alloc(&alcor_pci_idr, GFP_KERNEL);
114 	if (ret < 0)
115 		return ret;
116 	priv->id = ret;
117 
118 	priv->pdev = pdev;
119 	priv->parent_pdev = pdev->bus->self;
120 	priv->dev = &pdev->dev;
121 	priv->cfg = cfg;
122 	priv->irq = pdev->irq;
123 
124 	ret = pci_request_regions(pdev, DRV_NAME_ALCOR_PCI);
125 	if (ret) {
126 		dev_err(&pdev->dev, "Cannot request region\n");
127 		ret = -ENOMEM;
128 		goto error_free_ida;
129 	}
130 
131 	if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) {
132 		dev_err(&pdev->dev, "BAR %d is not iomem. Aborting.\n", bar);
133 		ret = -ENODEV;
134 		goto error_release_regions;
135 	}
136 
137 	priv->iobase = pcim_iomap(pdev, bar, 0);
138 	if (!priv->iobase) {
139 		ret = -ENOMEM;
140 		goto error_release_regions;
141 	}
142 
143 	/* make sure irqs are disabled */
144 	alcor_write32(priv, 0, AU6601_REG_INT_ENABLE);
145 	alcor_write32(priv, 0, AU6601_MS_INT_ENABLE);
146 
147 	ret = dma_set_mask_and_coherent(priv->dev, AU6601_SDMA_MASK);
148 	if (ret) {
149 		dev_err(priv->dev, "Failed to set DMA mask\n");
150 		goto error_release_regions;
151 	}
152 
153 	pci_set_master(pdev);
154 	pci_set_drvdata(pdev, priv);
155 
156 	for (i = 0; i < ARRAY_SIZE(alcor_pci_cells); i++) {
157 		alcor_pci_cells[i].platform_data = priv;
158 		alcor_pci_cells[i].pdata_size = sizeof(*priv);
159 	}
160 	ret = mfd_add_devices(&pdev->dev, priv->id, alcor_pci_cells,
161 			ARRAY_SIZE(alcor_pci_cells), NULL, 0, NULL);
162 	if (ret < 0)
163 		goto error_clear_drvdata;
164 
165 	pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
166 
167 	return 0;
168 
169 error_clear_drvdata:
170 	pci_clear_master(pdev);
171 	pci_set_drvdata(pdev, NULL);
172 error_release_regions:
173 	pci_release_regions(pdev);
174 error_free_ida:
175 	ida_free(&alcor_pci_idr, priv->id);
176 	return ret;
177 }
178 
179 static void alcor_pci_remove(struct pci_dev *pdev)
180 {
181 	struct alcor_pci_priv *priv;
182 
183 	priv = pci_get_drvdata(pdev);
184 
185 	mfd_remove_devices(&pdev->dev);
186 
187 	ida_free(&alcor_pci_idr, priv->id);
188 
189 	pci_release_regions(pdev);
190 	pci_clear_master(pdev);
191 	pci_set_drvdata(pdev, NULL);
192 }
193 
194 #ifdef CONFIG_PM_SLEEP
195 static int alcor_suspend(struct device *dev)
196 {
197 	return 0;
198 }
199 
200 static int alcor_resume(struct device *dev)
201 {
202 	struct alcor_pci_priv *priv = dev_get_drvdata(dev);
203 
204 	pci_disable_link_state(priv->pdev,
205 			       PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
206 
207 	return 0;
208 }
209 #endif /* CONFIG_PM_SLEEP */
210 
211 static SIMPLE_DEV_PM_OPS(alcor_pci_pm_ops, alcor_suspend, alcor_resume);
212 
213 static struct pci_driver alcor_driver = {
214 	.name	=	DRV_NAME_ALCOR_PCI,
215 	.id_table =	pci_ids,
216 	.probe	=	alcor_pci_probe,
217 	.remove =	alcor_pci_remove,
218 	.driver	=	{
219 		.pm	= &alcor_pci_pm_ops
220 	},
221 };
222 
223 module_pci_driver(alcor_driver);
224 
225 MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>");
226 MODULE_DESCRIPTION("PCI driver for Alcor Micro AU6601 Secure Digital Host Controller Interface");
227 MODULE_LICENSE("GPL");
228