ricoh_mmc.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 __devinit ricoh_mmc_probe(struct pci_dev *pdev,
  39. const struct pci_device_id *ent)
  40. {
  41. u8 rev;
  42. struct pci_dev *fw_dev = NULL;
  43. BUG_ON(pdev == NULL);
  44. BUG_ON(ent == NULL);
  45. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &rev);
  46. printk(KERN_INFO DRIVER_NAME
  47. ": Ricoh MMC controller found at %s [%04x:%04x] (rev %x)\n",
  48. pci_name(pdev), (int)pdev->vendor, (int)pdev->device,
  49. (int)rev);
  50. while ((fw_dev = pci_get_device(PCI_VENDOR_ID_RICOH, PCI_DEVICE_ID_RICOH_R5C832, fw_dev))) {
  51. if (PCI_SLOT(pdev->devfn) == PCI_SLOT(fw_dev->devfn) &&
  52. pdev->bus == fw_dev->bus) {
  53. u8 write_enable;
  54. u8 disable;
  55. pci_read_config_byte(fw_dev, 0xCB, &disable);
  56. if (disable & 0x02) {
  57. printk(KERN_INFO DRIVER_NAME
  58. ": Controller already disabled. Nothing to do.\n");
  59. return -ENODEV;
  60. }
  61. pci_read_config_byte(fw_dev, 0xCA, &write_enable);
  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. pci_set_drvdata(pdev, fw_dev);
  66. printk(KERN_INFO DRIVER_NAME
  67. ": Controller is now disabled.\n");
  68. break;
  69. }
  70. }
  71. if (pci_get_drvdata(pdev) == NULL) {
  72. printk(KERN_WARNING DRIVER_NAME
  73. ": Main firewire function not found. Cannot disable controller.\n");
  74. return -ENODEV;
  75. }
  76. return 0;
  77. }
  78. static void __devexit ricoh_mmc_remove(struct pci_dev *pdev)
  79. {
  80. u8 write_enable;
  81. u8 disable;
  82. struct pci_dev *fw_dev = NULL;
  83. fw_dev = pci_get_drvdata(pdev);
  84. BUG_ON(fw_dev == NULL);
  85. pci_read_config_byte(fw_dev, 0xCA, &write_enable);
  86. pci_read_config_byte(fw_dev, 0xCB, &disable);
  87. pci_write_config_byte(fw_dev, 0xCA, 0x57);
  88. pci_write_config_byte(fw_dev, 0xCB, disable & ~0x02);
  89. pci_write_config_byte(fw_dev, 0xCA, write_enable);
  90. printk(KERN_INFO DRIVER_NAME
  91. ": Controller is now re-enabled.\n");
  92. pci_set_drvdata(pdev, NULL);
  93. }
  94. static struct pci_driver ricoh_mmc_driver = {
  95. .name = DRIVER_NAME,
  96. .id_table = pci_ids,
  97. .probe = ricoh_mmc_probe,
  98. .remove = __devexit_p(ricoh_mmc_remove),
  99. };
  100. /*****************************************************************************\
  101. * *
  102. * Driver init/exit *
  103. * *
  104. \*****************************************************************************/
  105. static int __init ricoh_mmc_drv_init(void)
  106. {
  107. printk(KERN_INFO DRIVER_NAME
  108. ": Ricoh MMC Controller disabling driver\n");
  109. printk(KERN_INFO DRIVER_NAME ": Copyright(c) Philip Langdale\n");
  110. return pci_register_driver(&ricoh_mmc_driver);
  111. }
  112. static void __exit ricoh_mmc_drv_exit(void)
  113. {
  114. pci_unregister_driver(&ricoh_mmc_driver);
  115. }
  116. module_init(ricoh_mmc_drv_init);
  117. module_exit(ricoh_mmc_drv_exit);
  118. MODULE_AUTHOR("Philip Langdale <philipl@alumni.utexas.net>");
  119. MODULE_DESCRIPTION("Ricoh MMC Controller disabling driver");
  120. MODULE_LICENSE("GPL");