ide_platform.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Platform IDE driver
  3. *
  4. * Copyright (C) 2007 MontaVista Software
  5. *
  6. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ide.h>
  17. #include <linux/ioport.h>
  18. #include <linux/module.h>
  19. #include <linux/pata_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. static struct {
  23. void __iomem *plat_ide_mapbase;
  24. void __iomem *plat_ide_alt_mapbase;
  25. ide_hwif_t *hwif;
  26. int index;
  27. } hwif_prop;
  28. static ide_hwif_t *__devinit plat_ide_locate_hwif(void __iomem *base,
  29. void __iomem *ctrl, struct pata_platform_info *pdata, int irq,
  30. int mmio)
  31. {
  32. unsigned long port = (unsigned long)base;
  33. ide_hwif_t *hwif;
  34. int index, i;
  35. for (index = 0; index < MAX_HWIFS; ++index) {
  36. hwif = ide_hwifs + index;
  37. if (hwif->io_ports[IDE_DATA_OFFSET] == port)
  38. goto found;
  39. }
  40. for (index = 0; index < MAX_HWIFS; ++index) {
  41. hwif = ide_hwifs + index;
  42. if (hwif->io_ports[IDE_DATA_OFFSET] == 0)
  43. goto found;
  44. }
  45. return NULL;
  46. found:
  47. hwif->hw.io_ports[IDE_DATA_OFFSET] = port;
  48. port += (1 << pdata->ioport_shift);
  49. for (i = IDE_ERROR_OFFSET; i <= IDE_STATUS_OFFSET;
  50. i++, port += (1 << pdata->ioport_shift))
  51. hwif->hw.io_ports[i] = port;
  52. hwif->hw.io_ports[IDE_CONTROL_OFFSET] = (unsigned long)ctrl;
  53. memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
  54. hwif->hw.irq = hwif->irq = irq;
  55. hwif->hw.dma = NO_DMA;
  56. hwif->chipset = hwif->hw.chipset = ide_generic;
  57. if (mmio) {
  58. hwif->mmio = 1;
  59. default_hwif_mmiops(hwif);
  60. }
  61. hwif_prop.hwif = hwif;
  62. hwif_prop.index = index;
  63. return hwif;
  64. }
  65. static int __devinit plat_ide_probe(struct platform_device *pdev)
  66. {
  67. struct resource *res_base, *res_alt, *res_irq;
  68. ide_hwif_t *hwif;
  69. struct pata_platform_info *pdata;
  70. int ret = 0;
  71. int mmio = 0;
  72. pdata = pdev->dev.platform_data;
  73. /* get a pointer to the register memory */
  74. res_base = platform_get_resource(pdev, IORESOURCE_IO, 0);
  75. res_alt = platform_get_resource(pdev, IORESOURCE_IO, 1);
  76. if (!res_base || !res_alt) {
  77. res_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  78. res_alt = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  79. if (!res_base || !res_alt) {
  80. ret = -ENOMEM;
  81. goto out;
  82. }
  83. mmio = 1;
  84. }
  85. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  86. if (!res_irq) {
  87. ret = -EINVAL;
  88. goto out;
  89. }
  90. if (mmio) {
  91. hwif_prop.plat_ide_mapbase = devm_ioremap(&pdev->dev,
  92. res_base->start, res_base->end - res_base->start + 1);
  93. hwif_prop.plat_ide_alt_mapbase = devm_ioremap(&pdev->dev,
  94. res_alt->start, res_alt->end - res_alt->start + 1);
  95. } else {
  96. hwif_prop.plat_ide_mapbase = devm_ioport_map(&pdev->dev,
  97. res_base->start, res_base->end - res_base->start + 1);
  98. hwif_prop.plat_ide_alt_mapbase = devm_ioport_map(&pdev->dev,
  99. res_alt->start, res_alt->end - res_alt->start + 1);
  100. }
  101. hwif = plat_ide_locate_hwif(hwif_prop.plat_ide_mapbase,
  102. hwif_prop.plat_ide_alt_mapbase, pdata, res_irq->start, mmio);
  103. if (!hwif) {
  104. ret = -ENODEV;
  105. goto out;
  106. }
  107. hwif->gendev.parent = &pdev->dev;
  108. hwif->noprobe = 0;
  109. probe_hwif_init(hwif);
  110. platform_set_drvdata(pdev, hwif);
  111. ide_proc_register_port(hwif);
  112. return 0;
  113. out:
  114. return ret;
  115. }
  116. static int __devexit plat_ide_remove(struct platform_device *pdev)
  117. {
  118. ide_hwif_t *hwif = pdev->dev.driver_data;
  119. if (hwif != hwif_prop.hwif) {
  120. dev_printk(KERN_DEBUG, &pdev->dev, "%s: hwif value error",
  121. pdev->name);
  122. } else {
  123. ide_unregister(hwif_prop.index);
  124. hwif_prop.index = 0;
  125. hwif_prop.hwif = NULL;
  126. }
  127. return 0;
  128. }
  129. static struct platform_driver platform_ide_driver = {
  130. .driver = {
  131. .name = "pata_platform",
  132. },
  133. .probe = plat_ide_probe,
  134. .remove = __devexit_p(plat_ide_remove),
  135. };
  136. static int __init platform_ide_init(void)
  137. {
  138. return platform_driver_register(&platform_ide_driver);
  139. }
  140. static void __exit platform_ide_exit(void)
  141. {
  142. platform_driver_unregister(&platform_ide_driver);
  143. }
  144. MODULE_DESCRIPTION("Platform IDE driver");
  145. MODULE_LICENSE("GPL");
  146. module_init(platform_ide_init);
  147. module_exit(platform_ide_exit);