core.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cb710/core.c
  4. *
  5. * Copyright by Michał Mirosław, 2008-2009
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/idr.h>
  12. #include <linux/cb710.h>
  13. #include <linux/gfp.h>
  14. static DEFINE_IDA(cb710_ida);
  15. void cb710_pci_update_config_reg(struct pci_dev *pdev,
  16. int reg, uint32_t mask, uint32_t xor)
  17. {
  18. u32 rval;
  19. pci_read_config_dword(pdev, reg, &rval);
  20. rval = (rval & mask) ^ xor;
  21. pci_write_config_dword(pdev, reg, rval);
  22. }
  23. EXPORT_SYMBOL_GPL(cb710_pci_update_config_reg);
  24. /* Some magic writes based on Windows driver init code */
  25. static int cb710_pci_configure(struct pci_dev *pdev)
  26. {
  27. unsigned int devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
  28. struct pci_dev *pdev0;
  29. u32 val;
  30. cb710_pci_update_config_reg(pdev, 0x48,
  31. ~0x000000FF, 0x0000003F);
  32. pci_read_config_dword(pdev, 0x48, &val);
  33. if (val & 0x80000000)
  34. return 0;
  35. pdev0 = pci_get_slot(pdev->bus, devfn);
  36. if (!pdev0)
  37. return -ENODEV;
  38. if (pdev0->vendor == PCI_VENDOR_ID_ENE
  39. && pdev0->device == PCI_DEVICE_ID_ENE_720) {
  40. cb710_pci_update_config_reg(pdev0, 0x8C,
  41. ~0x00F00000, 0x00100000);
  42. cb710_pci_update_config_reg(pdev0, 0xB0,
  43. ~0x08000000, 0x08000000);
  44. }
  45. cb710_pci_update_config_reg(pdev0, 0x8C,
  46. ~0x00000F00, 0x00000200);
  47. cb710_pci_update_config_reg(pdev0, 0x90,
  48. ~0x00060000, 0x00040000);
  49. pci_dev_put(pdev0);
  50. return 0;
  51. }
  52. static irqreturn_t cb710_irq_handler(int irq, void *data)
  53. {
  54. struct cb710_chip *chip = data;
  55. struct cb710_slot *slot = &chip->slot[0];
  56. irqreturn_t handled = IRQ_NONE;
  57. unsigned nr;
  58. spin_lock(&chip->irq_lock); /* incl. smp_rmb() */
  59. for (nr = chip->slots; nr; ++slot, --nr) {
  60. cb710_irq_handler_t handler_func = slot->irq_handler;
  61. if (handler_func && handler_func(slot))
  62. handled = IRQ_HANDLED;
  63. }
  64. spin_unlock(&chip->irq_lock);
  65. return handled;
  66. }
  67. static void cb710_release_slot(struct device *dev)
  68. {
  69. #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
  70. struct cb710_slot *slot = cb710_pdev_to_slot(to_platform_device(dev));
  71. struct cb710_chip *chip = cb710_slot_to_chip(slot);
  72. /* slot struct can be freed now */
  73. atomic_dec(&chip->slot_refs_count);
  74. #endif
  75. }
  76. static int cb710_register_slot(struct cb710_chip *chip,
  77. unsigned slot_mask, unsigned io_offset, const char *name)
  78. {
  79. int nr = chip->slots;
  80. struct cb710_slot *slot = &chip->slot[nr];
  81. int err;
  82. dev_dbg(cb710_chip_dev(chip),
  83. "register: %s.%d; slot %d; mask %d; IO offset: 0x%02X\n",
  84. name, chip->platform_id, nr, slot_mask, io_offset);
  85. /* slot->irq_handler == NULL here; this needs to be
  86. * seen before platform_device_register() */
  87. ++chip->slots;
  88. smp_wmb();
  89. slot->iobase = chip->iobase + io_offset;
  90. slot->pdev.name = name;
  91. slot->pdev.id = chip->platform_id;
  92. slot->pdev.dev.parent = &chip->pdev->dev;
  93. slot->pdev.dev.release = cb710_release_slot;
  94. err = platform_device_register(&slot->pdev);
  95. #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
  96. atomic_inc(&chip->slot_refs_count);
  97. #endif
  98. if (err) {
  99. /* device_initialize() called from platform_device_register()
  100. * wants this on error path */
  101. platform_device_put(&slot->pdev);
  102. /* slot->irq_handler == NULL here anyway, so no lock needed */
  103. --chip->slots;
  104. return err;
  105. }
  106. chip->slot_mask |= slot_mask;
  107. return 0;
  108. }
  109. static void cb710_unregister_slot(struct cb710_chip *chip,
  110. unsigned slot_mask)
  111. {
  112. int nr = chip->slots - 1;
  113. if (!(chip->slot_mask & slot_mask))
  114. return;
  115. platform_device_unregister(&chip->slot[nr].pdev);
  116. /* complementary to spin_unlock() in cb710_set_irq_handler() */
  117. smp_rmb();
  118. BUG_ON(chip->slot[nr].irq_handler != NULL);
  119. /* slot->irq_handler == NULL here, so no lock needed */
  120. --chip->slots;
  121. chip->slot_mask &= ~slot_mask;
  122. }
  123. void cb710_set_irq_handler(struct cb710_slot *slot,
  124. cb710_irq_handler_t handler)
  125. {
  126. struct cb710_chip *chip = cb710_slot_to_chip(slot);
  127. unsigned long flags;
  128. spin_lock_irqsave(&chip->irq_lock, flags);
  129. slot->irq_handler = handler;
  130. spin_unlock_irqrestore(&chip->irq_lock, flags);
  131. }
  132. EXPORT_SYMBOL_GPL(cb710_set_irq_handler);
  133. static int __maybe_unused cb710_suspend(struct device *dev_d)
  134. {
  135. struct pci_dev *pdev = to_pci_dev(dev_d);
  136. struct cb710_chip *chip = pci_get_drvdata(pdev);
  137. devm_free_irq(&pdev->dev, pdev->irq, chip);
  138. return 0;
  139. }
  140. static int __maybe_unused cb710_resume(struct device *dev_d)
  141. {
  142. struct pci_dev *pdev = to_pci_dev(dev_d);
  143. struct cb710_chip *chip = pci_get_drvdata(pdev);
  144. return devm_request_irq(&pdev->dev, pdev->irq,
  145. cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
  146. }
  147. static int cb710_probe(struct pci_dev *pdev,
  148. const struct pci_device_id *ent)
  149. {
  150. struct cb710_chip *chip;
  151. u32 val;
  152. int err;
  153. int n = 0;
  154. err = cb710_pci_configure(pdev);
  155. if (err)
  156. return err;
  157. /* this is actually magic... */
  158. pci_read_config_dword(pdev, 0x48, &val);
  159. if (!(val & 0x80000000)) {
  160. pci_write_config_dword(pdev, 0x48, val|0x71000000);
  161. pci_read_config_dword(pdev, 0x48, &val);
  162. }
  163. dev_dbg(&pdev->dev, "PCI config[0x48] = 0x%08X\n", val);
  164. if (!(val & 0x70000000))
  165. return -ENODEV;
  166. val = (val >> 28) & 7;
  167. if (val & CB710_SLOT_MMC)
  168. ++n;
  169. if (val & CB710_SLOT_MS)
  170. ++n;
  171. if (val & CB710_SLOT_SM)
  172. ++n;
  173. chip = devm_kzalloc(&pdev->dev, struct_size(chip, slot, n),
  174. GFP_KERNEL);
  175. if (!chip)
  176. return -ENOMEM;
  177. err = pcim_enable_device(pdev);
  178. if (err)
  179. return err;
  180. err = pcim_iomap_regions(pdev, 0x0001, KBUILD_MODNAME);
  181. if (err)
  182. return err;
  183. spin_lock_init(&chip->irq_lock);
  184. chip->pdev = pdev;
  185. chip->iobase = pcim_iomap_table(pdev)[0];
  186. pci_set_drvdata(pdev, chip);
  187. err = devm_request_irq(&pdev->dev, pdev->irq,
  188. cb710_irq_handler, IRQF_SHARED, KBUILD_MODNAME, chip);
  189. if (err)
  190. return err;
  191. err = ida_alloc(&cb710_ida, GFP_KERNEL);
  192. if (err < 0)
  193. return err;
  194. chip->platform_id = err;
  195. dev_info(&pdev->dev, "id %d, IO 0x%p, IRQ %d\n",
  196. chip->platform_id, chip->iobase, pdev->irq);
  197. if (val & CB710_SLOT_MMC) { /* MMC/SD slot */
  198. err = cb710_register_slot(chip,
  199. CB710_SLOT_MMC, 0x00, "cb710-mmc");
  200. if (err)
  201. return err;
  202. }
  203. if (val & CB710_SLOT_MS) { /* MemoryStick slot */
  204. err = cb710_register_slot(chip,
  205. CB710_SLOT_MS, 0x40, "cb710-ms");
  206. if (err)
  207. goto unreg_mmc;
  208. }
  209. if (val & CB710_SLOT_SM) { /* SmartMedia slot */
  210. err = cb710_register_slot(chip,
  211. CB710_SLOT_SM, 0x60, "cb710-sm");
  212. if (err)
  213. goto unreg_ms;
  214. }
  215. return 0;
  216. unreg_ms:
  217. cb710_unregister_slot(chip, CB710_SLOT_MS);
  218. unreg_mmc:
  219. cb710_unregister_slot(chip, CB710_SLOT_MMC);
  220. #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
  221. BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
  222. #endif
  223. return err;
  224. }
  225. static void cb710_remove_one(struct pci_dev *pdev)
  226. {
  227. struct cb710_chip *chip = pci_get_drvdata(pdev);
  228. cb710_unregister_slot(chip, CB710_SLOT_SM);
  229. cb710_unregister_slot(chip, CB710_SLOT_MS);
  230. cb710_unregister_slot(chip, CB710_SLOT_MMC);
  231. #ifdef CONFIG_CB710_DEBUG_ASSUMPTIONS
  232. BUG_ON(atomic_read(&chip->slot_refs_count) != 0);
  233. #endif
  234. ida_free(&cb710_ida, chip->platform_id);
  235. }
  236. static const struct pci_device_id cb710_pci_tbl[] = {
  237. { PCI_VENDOR_ID_ENE, PCI_DEVICE_ID_ENE_CB710_FLASH,
  238. PCI_ANY_ID, PCI_ANY_ID, },
  239. { 0, }
  240. };
  241. static SIMPLE_DEV_PM_OPS(cb710_pm_ops, cb710_suspend, cb710_resume);
  242. static struct pci_driver cb710_driver = {
  243. .name = KBUILD_MODNAME,
  244. .id_table = cb710_pci_tbl,
  245. .probe = cb710_probe,
  246. .remove = cb710_remove_one,
  247. .driver.pm = &cb710_pm_ops,
  248. };
  249. static int __init cb710_init_module(void)
  250. {
  251. return pci_register_driver(&cb710_driver);
  252. }
  253. static void __exit cb710_cleanup_module(void)
  254. {
  255. pci_unregister_driver(&cb710_driver);
  256. ida_destroy(&cb710_ida);
  257. }
  258. module_init(cb710_init_module);
  259. module_exit(cb710_cleanup_module);
  260. MODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>");
  261. MODULE_DESCRIPTION("ENE CB710 memory card reader driver");
  262. MODULE_LICENSE("GPL");
  263. MODULE_DEVICE_TABLE(pci, cb710_pci_tbl);