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