core.c 8.3 KB

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