pata_rb532_cf.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * A low-level PATA driver to handle a Compact Flash connected on the
  4. * Mikrotik's RouterBoard 532 board.
  5. *
  6. * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
  7. * Copyright (C) 2008 Florian Fainelli <[email protected]>
  8. *
  9. * This file was based on: drivers/ata/pata_ixp4xx_cf.c
  10. * Copyright (C) 2006-07 Tower Technologies
  11. * Author: Alessandro Zummo <[email protected]>
  12. *
  13. * Also was based on the driver for Linux 2.4.xx published by Mikrotik for
  14. * their RouterBoard 1xx and 5xx series devices. The original Mikrotik code
  15. * seems not to have a license.
  16. */
  17. #include <linux/gfp.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/libata.h>
  26. #include <scsi/scsi_host.h>
  27. #include <asm/mach-rc32434/rb.h>
  28. #define DRV_NAME "pata-rb532-cf"
  29. #define DRV_VERSION "0.1.0"
  30. #define DRV_DESC "PATA driver for RouterBOARD 532 Compact Flash"
  31. #define RB500_CF_MAXPORTS 1
  32. #define RB500_CF_IO_DELAY 400
  33. #define RB500_CF_REG_BASE 0x0800
  34. #define RB500_CF_REG_ERR 0x080D
  35. #define RB500_CF_REG_CTRL 0x080E
  36. /* 32bit buffered data register offset */
  37. #define RB500_CF_REG_DBUF32 0x0C00
  38. struct rb532_cf_info {
  39. void __iomem *iobase;
  40. struct gpio_desc *gpio_line;
  41. unsigned int irq;
  42. };
  43. /* ------------------------------------------------------------------------ */
  44. static irqreturn_t rb532_pata_irq_handler(int irq, void *dev_instance)
  45. {
  46. struct ata_host *ah = dev_instance;
  47. struct rb532_cf_info *info = ah->private_data;
  48. if (gpiod_get_value(info->gpio_line)) {
  49. irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_LOW);
  50. ata_sff_interrupt(info->irq, dev_instance);
  51. } else {
  52. irq_set_irq_type(info->irq, IRQ_TYPE_LEVEL_HIGH);
  53. }
  54. return IRQ_HANDLED;
  55. }
  56. static struct ata_port_operations rb532_pata_port_ops = {
  57. .inherits = &ata_sff_port_ops,
  58. .sff_data_xfer = ata_sff_data_xfer32,
  59. };
  60. /* ------------------------------------------------------------------------ */
  61. static struct scsi_host_template rb532_pata_sht = {
  62. ATA_PIO_SHT(DRV_NAME),
  63. };
  64. /* ------------------------------------------------------------------------ */
  65. static void rb532_pata_setup_ports(struct ata_host *ah)
  66. {
  67. struct rb532_cf_info *info = ah->private_data;
  68. struct ata_port *ap;
  69. ap = ah->ports[0];
  70. ap->ops = &rb532_pata_port_ops;
  71. ap->pio_mask = ATA_PIO4;
  72. ap->ioaddr.cmd_addr = info->iobase + RB500_CF_REG_BASE;
  73. ap->ioaddr.ctl_addr = info->iobase + RB500_CF_REG_CTRL;
  74. ap->ioaddr.altstatus_addr = info->iobase + RB500_CF_REG_CTRL;
  75. ata_sff_std_ports(&ap->ioaddr);
  76. ap->ioaddr.data_addr = info->iobase + RB500_CF_REG_DBUF32;
  77. ap->ioaddr.error_addr = info->iobase + RB500_CF_REG_ERR;
  78. }
  79. static int rb532_pata_driver_probe(struct platform_device *pdev)
  80. {
  81. int irq;
  82. struct gpio_desc *gpiod;
  83. struct resource *res;
  84. struct ata_host *ah;
  85. struct rb532_cf_info *info;
  86. int ret;
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. if (!res) {
  89. dev_err(&pdev->dev, "no IOMEM resource found\n");
  90. return -EINVAL;
  91. }
  92. irq = platform_get_irq(pdev, 0);
  93. if (irq < 0)
  94. return irq;
  95. if (!irq)
  96. return -EINVAL;
  97. gpiod = devm_gpiod_get(&pdev->dev, NULL, GPIOD_IN);
  98. if (IS_ERR(gpiod)) {
  99. dev_err(&pdev->dev, "no GPIO found for irq%d\n", irq);
  100. return PTR_ERR(gpiod);
  101. }
  102. gpiod_set_consumer_name(gpiod, DRV_NAME);
  103. /* allocate host */
  104. ah = ata_host_alloc(&pdev->dev, RB500_CF_MAXPORTS);
  105. if (!ah)
  106. return -ENOMEM;
  107. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  108. if (!info)
  109. return -ENOMEM;
  110. ah->private_data = info;
  111. info->gpio_line = gpiod;
  112. info->irq = irq;
  113. info->iobase = devm_ioremap(&pdev->dev, res->start,
  114. resource_size(res));
  115. if (!info->iobase)
  116. return -ENOMEM;
  117. rb532_pata_setup_ports(ah);
  118. ret = ata_host_activate(ah, irq, rb532_pata_irq_handler,
  119. IRQF_TRIGGER_LOW, &rb532_pata_sht);
  120. if (ret)
  121. return ret;
  122. return 0;
  123. }
  124. static int rb532_pata_driver_remove(struct platform_device *pdev)
  125. {
  126. struct ata_host *ah = platform_get_drvdata(pdev);
  127. ata_host_detach(ah);
  128. return 0;
  129. }
  130. static struct platform_driver rb532_pata_platform_driver = {
  131. .probe = rb532_pata_driver_probe,
  132. .remove = rb532_pata_driver_remove,
  133. .driver = {
  134. .name = DRV_NAME,
  135. },
  136. };
  137. #define DRV_INFO DRV_DESC " version " DRV_VERSION
  138. module_platform_driver(rb532_pata_platform_driver);
  139. MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
  140. MODULE_AUTHOR("Florian Fainelli <[email protected]>");
  141. MODULE_DESCRIPTION(DRV_DESC);
  142. MODULE_VERSION(DRV_VERSION);
  143. MODULE_LICENSE("GPL");
  144. MODULE_ALIAS("platform:" DRV_NAME);