core.c 8.3 KB

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