ricoh_mmc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * ricoh_mmc.c - Dummy driver to disable the Rioch MMC controller.
  3. *
  4. * Copyright (C) 2007 Philip Langdale, All Rights Reserved.
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. */
  11. /*
  12. * This is a conceptually ridiculous driver, but it is required by the way
  13. * the Ricoh multi-function R5C832 works. This chip implements firewire
  14. * and four different memory card controllers. Two of those controllers are
  15. * an SDHCI controller and a proprietary MMC controller. The linux SDHCI
  16. * driver supports MMC cards but the chip detects MMC cards in hardware
  17. * and directs them to the MMC controller - so the SDHCI driver never sees
  18. * them. To get around this, we must disable the useless MMC controller.
  19. * At that point, the SDHCI controller will start seeing them. As a bonus,
  20. * a detection event occurs immediately, even if the MMC card is already
  21. * in the reader.
  22. *
  23. * The relevant registers live on the firewire function, so this is unavoidably
  24. * ugly. Such is life.
  25. */
  26. #include <linux/pci.h>
  27. #define DRIVER_NAME "ricoh-mmc"
  28. static const struct pci_device_id pci_ids[] __devinitdata = {
  29. {
  30. .vendor = PCI_VENDOR_ID_RICOH,
  31. .device = PCI_DEVICE_ID_RICOH_R5C843,
  32. .subvendor = PCI_ANY_ID,
  33. .subdevice = PCI_ANY_ID,
  34. },
  35. { /* end: all zeroes */ },
  36. };
  37. MODULE_DEVICE_TABLE(pci, pci_ids);
  38. static int ricoh_mmc_disable(struct pci_dev *fw_dev)
  39. {
  40. u8 write_enable;
  41. u8 disable;
  42. pci_read_config_byte(fw_dev, 0xCB, &disable);
  43. if (disable & 0x02) {
  44. printk(KERN_INFO DRIVER_NAME
  45. ": Controller already disabled. Nothing to do.\n");
  46. return -ENODEV;
  47. }
  48. pci_read_config_byte(fw_dev, 0xCA, &write_enable);
  49. pci_write_config_byte(fw_dev, 0xCA, 0x57);
  50. pci_write_config_byte(fw_dev, 0xCB, disable | 0x02);
  51. pci_write_config_byte(fw_dev, 0xCA, write_enable);
  52. printk(KERN_INFO DRIVER_NAME
  53. ": Controller is now disabled.\n");
  54. return 0;
  55. }
  56. static int ricoh_mmc_enable(struct pci_dev *fw_dev)
  57. {
  58. u8 write_enable;
  59. u8 disable;
  60. pci_read_config_byte(fw_dev, 0xCA, &write_enable);
  61. pci_read_config_byte(fw_dev, 0xCB, &disable);
  62. pci_write_config_byte(fw_dev, 0xCA, 0x57);
  63. pci_write_config_byte(fw_dev, 0xCB, disable & ~0x02);
  64. pci_write_config_byte(fw_dev, 0xCA, write_enable);
  65. printk(KERN_INFO DRIVER_NAME
  66. ": Controller is now re-enabled.\n");
  67. return 0;
  68. }
  69. static int __devinit ricoh_mmc_probe(struct pci_dev *pdev,
  70. const struct pci_device_id *ent)
  71. {
  72. u8 rev;
  73. struct pci_dev *fw_dev = NULL;
  74. BUG_ON(pdev == NULL);
  75. BUG_ON(ent == NULL);
  76. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
  77. printk(KERN_INFO DRIVER_NAME
  78. ": Ricoh MMC controller found at %s [%04x:%04x] (rev %x)\n",
  79. pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
  80. (int)rev);
  81. while ((fw_dev = pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5C832, fw_dev))) {
  82. if (PCI_SLOT(pdev->devfn) == PCI_SLOT(fw_dev->devfn) &&
  83. pdev->bus == fw_dev->bus) {
  84. if (ricoh_mmc_disable(fw_dev) != 0) {
  85. return -ENODEV;
  86. }
  87. pci_set_drvdata(pdev, fw_dev);
  88. break;
  89. }
  90. }
  91. if (pci_get_drvdata(pdev) == NULL) {
  92. printk(KERN_WARNING DRIVER_NAME
  93. ": Main firewire function not found. Cannot disable controller.\n");
  94. return -ENODEV;
  95. }
  96. return 0;
  97. }
  98. static void __devexit ricoh_mmc_remove(struct pci_dev *pdev)
  99. {
  100. struct pci_dev *fw_dev = NULL;
  101. fw_dev = pci_get_drvdata(pdev);
  102. BUG_ON(fw_dev == NULL);
  103. ricoh_mmc_enable(fw_dev);
  104. pci_set_drvdata(pdev, NULL);
  105. }
  106. static int ricoh_mmc_suspend (struct pci_dev *pdev, pm_message_t state)
  107. {
  108. struct pci_dev *fw_dev = NULL;
  109. fw_dev = pci_get_drvdata(pdev);
  110. BUG_ON(fw_dev == NULL);
  111. printk(KERN_INFO DRIVER_NAME ": Suspending.\n");
  112. ricoh_mmc_enable(fw_dev);
  113. return 0;
  114. }
  115. static int ricoh_mmc_resume (struct pci_dev *pdev)
  116. {
  117. struct pci_dev *fw_dev = NULL;
  118. fw_dev = pci_get_drvdata(pdev);
  119. BUG_ON(fw_dev == NULL);
  120. printk(KERN_INFO DRIVER_NAME ": Resuming.\n");
  121. ricoh_mmc_disable(fw_dev);
  122. return 0;
  123. }
  124. static struct pci_driver ricoh_mmc_driver = {
  125. .name = DRIVER_NAME,
  126. .id_table = pci_ids,
  127. .probe = ricoh_mmc_probe,
  128. .remove = __devexit_p(ricoh_mmc_remove),
  129. .suspend = ricoh_mmc_suspend,
  130. .resume = ricoh_mmc_resume,
  131. };
  132. /*****************************************************************************\
  133. * *
  134. * Driver init/exit *
  135. * *
  136. \*****************************************************************************/
  137. static int __init ricoh_mmc_drv_init(void)
  138. {
  139. printk(KERN_INFO DRIVER_NAME
  140. ": Ricoh MMC Controller disabling driver\n");
  141. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Philip Langdale\n");
  142. return pci_register_driver(&ricoh_mmc_driver);
  143. }
  144. static void __exit ricoh_mmc_drv_exit(void)
  145. {
  146. pci_unregister_driver(&ricoh_mmc_driver);
  147. }
  148. module_init(ricoh_mmc_drv_init);
  149. module_exit(ricoh_mmc_drv_exit);
  150. MODULE_AUTHOR("Philip Langdale <philipl@alumni.utexas.net>");
  151. MODULE_DESCRIPTION("Ricoh MMC Controller disabling driver");
  152. MODULE_LICENSE("GPL");