Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  cb710/core.c
0004  *
0005  *  Copyright by Michał Mirosław, 2008-2009
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/pci.h>
0010 #include <linux/spinlock.h>
0011 #include <linux/idr.h>
0012 #include <linux/cb710.h>
0013 #include <linux/gfp.h>
0014 
0015 static DEFINE_IDA(cb710_ida);
0016 
0017 void cb710_pci_update_config_reg(struct pci_dev *pdev,
0018     int reg, uint32_t mask, uint32_t xor)
0019 {
0020     u32 rval;
0021 
0022     pci_read_config_dword(pdev, reg, &rval);
0023     rval = (rval & mask) ^ xor;
0024     pci_write_config_dword(pdev, reg, rval);
0025 }
0026 EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
0027 
0028 /* Some magic writes based on Windows driver init code */
0029 static int cb710_pci_configure(struct pci_dev *pdev)
0030 {
0031     unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
0032     struct pci_dev *pdev0;
0033     u32 val;
0034 
0035     cb710_pci_update_config_reg(pdev, 0x48,
0036         ~0x000000FF, 0x0000003F);
0037 
0038     pci_read_config_dword(pdev, 0x48, &val);
0039     if (val & 0x80000000)
0040         return 0;
0041 
0042     pdev0 = pci_get_slot(pdev->bus, devfn);
0043     if (!pdev0)
0044         return -ENODEV;
0045 
0046     if (pdev0->vendor == PCI_VENDOR_ID_ENE
0047         && pdev0->device == PCI_DEVICE_ID_ENE_720) {
0048         cb710_pci_update_config_reg(pdev0, 0x8C,
0049             ~0x00F00000, 0x00100000);
0050         cb710_pci_update_config_reg(pdev0, 0xB0,
0051             ~0x08000000, 0x08000000);
0052     }
0053 
0054     cb710_pci_update_config_reg(pdev0, 0x8C,
0055         ~0x00000F00, 0x00000200);
0056     cb710_pci_update_config_reg(pdev0, 0x90,
0057         ~0x00060000, 0x00040000);
0058 
0059     pci_dev_put(pdev0);
0060 
0061     return 0;
0062 }
0063 
0064 static irqreturn_t cb710_irq_handler(int irq, void *data)
0065 {
0066     struct cb710_chip *chip = data;
0067     struct cb710_slot *slot = &chip->slot[0];
0068     irqreturn_t handled = IRQ_NONE;
0069     unsigned nr;
0070 
0071     spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
0072 
0073     for (nr = chip->slots; nr; ++slot, --nr) {
0074         cb710_irq_handler_t handler_func = slot->irq_handler;
0075         if (handler_func && handler_func(slot))
0076             handled = IRQ_HANDLED;
0077     }
0078 
0079     spin_unlock(&chip->irq_lock);
0080 
0081     return handled;
0082 }
0083 
0084 static void cb710_release_slot(struct device *dev)
0085 {
0086 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
0087     struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
0088     struct cb710_chip *chip = cb710_slot_to_chip(slot);
0089 
0090     /* slot struct can be freed now */
0091     atomic_dec(&chip->slot_refs_count);
0092 #endif
0093 }
0094 
0095 static int cb710_register_slot(struct cb710_chip *chip,
0096     unsigned slot_mask, unsigned io_offset, const char *name)
0097 {
0098     int nr = chip->slots;
0099     struct cb710_slot *slot = &chip->slot[nr];
0100     int err;
0101 
0102     dev_dbg(cb710_chip_dev(chip),
0103         "register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
0104         name, chip->platform_id, nr, slot_mask, io_offset);
0105 
0106     /* slot->irq_handler == NULL here; this needs to be
0107      * seen before platform_device_register() */
0108     ++chip->slots;
0109     smp_wmb();
0110 
0111     slot->iobase = chip->iobase + io_offset;
0112     slot->pdev.name = name;
0113     slot->pdev.id = chip->platform_id;
0114     slot->pdev.dev.parent = &chip->pdev->dev;
0115     slot->pdev.dev.release = cb710_release_slot;
0116 
0117     err = platform_device_register(&slot->pdev);
0118 
0119 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
0120     atomic_inc(&chip->slot_refs_count);
0121 #endif
0122 
0123     if (err) {
0124         /* device_initialize() called from platform_device_register()
0125          * wants this on error path */
0126         platform_device_put(&slot->pdev);
0127 
0128         /* slot->irq_handler == NULL here anyway, so no lock needed */
0129         --chip->slots;
0130         return err;
0131     }
0132 
0133     chip->slot_mask |= slot_mask;
0134 
0135     return 0;
0136 }
0137 
0138 static void cb710_unregister_slot(struct cb710_chip *chip,
0139     unsigned slot_mask)
0140 {
0141     int nr = chip->slots - 1;
0142 
0143     if (!(chip->slot_mask & slot_mask))
0144         return;
0145 
0146     platform_device_unregister(&chip->slot[nr].pdev);
0147 
0148     /* complementary to spin_unlock() in cb710_set_irq_handler() */
0149     smp_rmb();
0150     BUG_ON(chip->slot[nr].irq_handler != NULL);
0151 
0152     /* slot->irq_handler == NULL here, so no lock needed */
0153     --chip->slots;
0154     chip->slot_mask &= ~slot_mask;
0155 }
0156 
0157 void cb710_set_irq_handler(struct cb710_slot *slot,
0158     cb710_irq_handler_t handler)
0159 {
0160     struct cb710_chip *chip = cb710_slot_to_chip(slot);
0161     unsigned long flags;
0162 
0163     spin_lock_irqsave(&chip->irq_lock, flags);
0164     slot->irq_handler = handler;
0165     spin_unlock_irqrestore(&chip->irq_lock, flags);
0166 }
0167 EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
0168 
0169 static int __maybe_unused cb710_suspend(struct device *dev_d)
0170 {
0171     struct pci_dev *pdev = to_pci_dev(dev_d);
0172     struct cb710_chip *chip = pci_get_drvdata(pdev);
0173 
0174     devm_free_irq(&pdev->dev, pdev->irq, chip);
0175     return 0;
0176 }
0177 
0178 static int __maybe_unused cb710_resume(struct device *dev_d)
0179 {
0180     struct pci_dev *pdev = to_pci_dev(dev_d);
0181     struct cb710_chip *chip = pci_get_drvdata(pdev);
0182 
0183     return devm_request_irq(&pdev->dev, pdev->irq,
0184         cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
0185 }
0186 
0187 static int cb710_probe(struct pci_dev *pdev,
0188     const struct pci_device_id *ent)
0189 {
0190     struct cb710_chip *chip;
0191     u32 val;
0192     int err;
0193     int n = 0;
0194 
0195     err = cb710_pci_configure(pdev);
0196     if (err)
0197         return err;
0198 
0199     /* this is actually magic... */
0200     pci_read_config_dword(pdev, 0x48, &val);
0201     if (!(val & 0x80000000)) {
0202         pci_write_config_dword(pdev, 0x48, val|0x71000000);
0203         pci_read_config_dword(pdev, 0x48, &val);
0204     }
0205 
0206     dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
0207     if (!(val & 0x70000000))
0208         return -ENODEV;
0209     val = (val >> 28) & 7;
0210     if (val & CB710_SLOT_MMC)
0211         ++n;
0212     if (val & CB710_SLOT_MS)
0213         ++n;
0214     if (val & CB710_SLOT_SM)
0215         ++n;
0216 
0217     chip = devm_kzalloc(&pdev->dev, struct_size(chip, slot, n),
0218                 GFP_KERNEL);
0219     if (!chip)
0220         return -ENOMEM;
0221 
0222     err = pcim_enable_device(pdev);
0223     if (err)
0224         return err;
0225 
0226     err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
0227     if (err)
0228         return err;
0229 
0230     spin_lock_init(&chip->irq_lock);
0231     chip->pdev = pdev;
0232     chip->iobase = pcim_iomap_table(pdev)[0];
0233 
0234     pci_set_drvdata(pdev, chip);
0235 
0236     err = devm_request_irq(&pdev->dev, pdev->irq,
0237         cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
0238     if (err)
0239         return err;
0240 
0241     err = ida_alloc(&cb710_ida, GFP_KERNEL);
0242     if (err < 0)
0243         return err;
0244     chip->platform_id = err;
0245 
0246     dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
0247         chip->platform_id, chip->iobase, pdev->irq);
0248 
0249     if (val & CB710_SLOT_MMC) { /* MMC/SD slot */
0250         err = cb710_register_slot(chip,
0251             CB710_SLOT_MMC, 0x00, "cb710-mmc");
0252         if (err)
0253             return err;
0254     }
0255 
0256     if (val & CB710_SLOT_MS) {  /* MemoryStick slot */
0257         err = cb710_register_slot(chip,
0258             CB710_SLOT_MS, 0x40, "cb710-ms");
0259         if (err)
0260             goto unreg_mmc;
0261     }
0262 
0263     if (val & CB710_SLOT_SM) {  /* SmartMedia slot */
0264         err = cb710_register_slot(chip,
0265             CB710_SLOT_SM, 0x60, "cb710-sm");
0266         if (err)
0267             goto unreg_ms;
0268     }
0269 
0270     return 0;
0271 unreg_ms:
0272     cb710_unregister_slot(chip, CB710_SLOT_MS);
0273 unreg_mmc:
0274     cb710_unregister_slot(chip, CB710_SLOT_MMC);
0275 
0276 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
0277     BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
0278 #endif
0279     return err;
0280 }
0281 
0282 static void cb710_remove_one(struct pci_dev *pdev)
0283 {
0284     struct cb710_chip *chip = pci_get_drvdata(pdev);
0285 
0286     cb710_unregister_slot(chip, CB710_SLOT_SM);
0287     cb710_unregister_slot(chip, CB710_SLOT_MS);
0288     cb710_unregister_slot(chip, CB710_SLOT_MMC);
0289 #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
0290     BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
0291 #endif
0292 
0293     ida_free(&cb710_ida, chip->platform_id);
0294 }
0295 
0296 static const struct pci_device_id cb710_pci_tbl[] = {
0297     { PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
0298         PCI_ANY_ID, PCI_ANY_ID, },
0299     { 0, }
0300 };
0301 
0302 static SIMPLE_DEV_PM_OPS(cb710_pm_ops, cb710_suspend, cb710_resume);
0303 
0304 static struct pci_driver cb710_driver = {
0305     .name = KBUILD_MODNAME,
0306     .id_table = cb710_pci_tbl,
0307     .probe = cb710_probe,
0308     .remove = cb710_remove_one,
0309     .driver.pm = &cb710_pm_ops,
0310 };
0311 
0312 static int __init cb710_init_module(void)
0313 {
0314     return pci_register_driver(&cb710_driver);
0315 }
0316 
0317 static void __exit cb710_cleanup_module(void)
0318 {
0319     pci_unregister_driver(&cb710_driver);
0320     ida_destroy(&cb710_ida);
0321 }
0322 
0323 module_init(cb710_init_module);
0324 module_exit(cb710_cleanup_module);
0325 
0326 MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
0327 MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
0328 MODULE_LICENSE("GPL");
0329 MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);