ide_platform.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Platform IDE driver
  4. *
  5. * Copyright (C) 2007 MontaVista Software
  6. *
  7. * Maintainer: Kumar Gala <galak@kernel.crashing.org>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/ide.h>
  13. #include <linux/ioport.h>
  14. #include <linux/module.h>
  15. #include <linux/ata_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. static void plat_ide_setup_ports(struct ide_hw *hw, void __iomem *base,
  20. void __iomem *ctrl,
  21. struct pata_platform_info *pdata, int irq)
  22. {
  23. unsigned long port = (unsigned long)base;
  24. int i;
  25. hw->io_ports.data_addr = port;
  26. port += (1 << pdata->ioport_shift);
  27. for (i = 1; i <= 7;
  28. i++, port += (1 << pdata->ioport_shift))
  29. hw->io_ports_array[i] = port;
  30. hw->io_ports.ctl_addr = (unsigned long)ctrl;
  31. hw->irq = irq;
  32. }
  33. static const struct ide_port_info platform_ide_port_info = {
  34. .host_flags = IDE_HFLAG_NO_DMA,
  35. .chipset = ide_generic,
  36. };
  37. static int plat_ide_probe(struct platform_device *pdev)
  38. {
  39. struct resource *res_base, *res_alt, *res_irq;
  40. void __iomem *base, *alt_base;
  41. struct pata_platform_info *pdata;
  42. struct ide_host *host;
  43. int ret = 0, mmio = 0;
  44. struct ide_hw hw, *hws[] = { &hw };
  45. struct ide_port_info d = platform_ide_port_info;
  46. pdata = dev_get_platdata(&pdev->dev);
  47. /* get a pointer to the register memory */
  48. res_base = platform_get_resource(pdev, IORESOURCE_IO, 0);
  49. res_alt = platform_get_resource(pdev, IORESOURCE_IO, 1);
  50. if (!res_base || !res_alt) {
  51. res_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  52. res_alt = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  53. if (!res_base || !res_alt) {
  54. ret = -ENOMEM;
  55. goto out;
  56. }
  57. mmio = 1;
  58. }
  59. res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  60. if (!res_irq) {
  61. ret = -EINVAL;
  62. goto out;
  63. }
  64. if (mmio) {
  65. base = devm_ioremap(&pdev->dev,
  66. res_base->start, resource_size(res_base));
  67. alt_base = devm_ioremap(&pdev->dev,
  68. res_alt->start, resource_size(res_alt));
  69. } else {
  70. base = devm_ioport_map(&pdev->dev,
  71. res_base->start, resource_size(res_base));
  72. alt_base = devm_ioport_map(&pdev->dev,
  73. res_alt->start, resource_size(res_alt));
  74. }
  75. memset(&hw, 0, sizeof(hw));
  76. plat_ide_setup_ports(&hw, base, alt_base, pdata, res_irq->start);
  77. hw.dev = &pdev->dev;
  78. d.irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
  79. if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
  80. d.irq_flags |= IRQF_SHARED;
  81. if (mmio)
  82. d.host_flags |= IDE_HFLAG_MMIO;
  83. ret = ide_host_add(&d, hws, 1, &host);
  84. if (ret)
  85. goto out;
  86. platform_set_drvdata(pdev, host);
  87. return 0;
  88. out:
  89. return ret;
  90. }
  91. static int plat_ide_remove(struct platform_device *pdev)
  92. {
  93. struct ide_host *host = dev_get_drvdata(&pdev->dev);
  94. ide_host_remove(host);
  95. return 0;
  96. }
  97. static struct platform_driver platform_ide_driver = {
  98. .driver = {
  99. .name = "pata_platform",
  100. },
  101. .probe = plat_ide_probe,
  102. .remove = plat_ide_remove,
  103. };
  104. module_platform_driver(platform_ide_driver);
  105. MODULE_DESCRIPTION("Platform IDE driver");
  106. MODULE_LICENSE("GPL");
  107. MODULE_ALIAS("platform:pata_platform");