| 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, addr: 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, addr: 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, addr: 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(addr: 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(addr: 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(dev: &pdev->dev, size: sizeof(*priv), GFP_KERNEL); |
| 110 | if (!priv) |
| 111 | return -ENOMEM; |
| 112 | |
| 113 | ret = ida_alloc(ida: &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 = pcim_request_all_regions(pdev, DRV_NAME_ALCOR_PCI); |
| 125 | if (ret) { |
| 126 | dev_err(&pdev->dev, "Cannot request region\n" ); |
| 127 | ret = -EBUSY; |
| 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_free_ida; |
| 135 | } |
| 136 | |
| 137 | priv->iobase = pcim_iomap(pdev, bar, maxlen: 0); |
| 138 | if (!priv->iobase) { |
| 139 | ret = -ENOMEM; |
| 140 | goto error_free_ida; |
| 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(dev: priv->dev, AU6601_SDMA_MASK); |
| 148 | if (ret) { |
| 149 | dev_err(priv->dev, "Failed to set DMA mask\n" ); |
| 150 | goto error_free_ida; |
| 151 | } |
| 152 | |
| 153 | pci_set_master(dev: pdev); |
| 154 | pci_set_drvdata(pdev, data: 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(parent: &pdev->dev, id: priv->id, cells: alcor_pci_cells, |
| 161 | ARRAY_SIZE(alcor_pci_cells), NULL, irq_base: 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(dev: pdev); |
| 171 | pci_set_drvdata(pdev, NULL); |
| 172 | error_free_ida: |
| 173 | ida_free(&alcor_pci_idr, id: priv->id); |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | static void alcor_pci_remove(struct pci_dev *pdev) |
| 178 | { |
| 179 | struct alcor_pci_priv *priv; |
| 180 | |
| 181 | priv = pci_get_drvdata(pdev); |
| 182 | |
| 183 | mfd_remove_devices(parent: &pdev->dev); |
| 184 | |
| 185 | ida_free(&alcor_pci_idr, id: priv->id); |
| 186 | |
| 187 | pci_clear_master(dev: pdev); |
| 188 | pci_set_drvdata(pdev, NULL); |
| 189 | } |
| 190 | |
| 191 | #ifdef CONFIG_PM_SLEEP |
| 192 | static int alcor_suspend(struct device *dev) |
| 193 | { |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int alcor_resume(struct device *dev) |
| 198 | { |
| 199 | struct alcor_pci_priv *priv = dev_get_drvdata(dev); |
| 200 | |
| 201 | pci_disable_link_state(pdev: priv->pdev, |
| 202 | PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1); |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | #endif /* CONFIG_PM_SLEEP */ |
| 207 | |
| 208 | static SIMPLE_DEV_PM_OPS(alcor_pci_pm_ops, alcor_suspend, alcor_resume); |
| 209 | |
| 210 | static struct pci_driver alcor_driver = { |
| 211 | .name = DRV_NAME_ALCOR_PCI, |
| 212 | .id_table = pci_ids, |
| 213 | .probe = alcor_pci_probe, |
| 214 | .remove = alcor_pci_remove, |
| 215 | .driver = { |
| 216 | .pm = &alcor_pci_pm_ops |
| 217 | }, |
| 218 | }; |
| 219 | |
| 220 | module_pci_driver(alcor_driver); |
| 221 | |
| 222 | MODULE_AUTHOR("Oleksij Rempel <linux@rempel-privat.de>" ); |
| 223 | MODULE_DESCRIPTION("PCI driver for Alcor Micro AU6601 Secure Digital Host Controller Interface" ); |
| 224 | MODULE_LICENSE("GPL" ); |
| 225 | |