pata_ixp4xx_cf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ixp4xx PATA/Compact Flash driver
  4. * Copyright (C) 2006-07 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * An ATA driver to handle a Compact Flash connected
  8. * to the ixp4xx expansion bus in TrueIDE mode. The CF
  9. * must have it chip selects connected to two CS lines
  10. * on the ixp4xx. In the irq is not available, you might
  11. * want to modify both this driver and libata to run in
  12. * polling mode.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/libata.h>
  17. #include <linux/irq.h>
  18. #include <linux/platform_device.h>
  19. #include <scsi/scsi_host.h>
  20. #define DRV_NAME "pata_ixp4xx_cf"
  21. #define DRV_VERSION "0.2"
  22. static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error)
  23. {
  24. struct ata_device *dev;
  25. ata_for_each_dev(dev, link, ENABLED) {
  26. ata_dev_info(dev, "configured for PIO0\n");
  27. dev->pio_mode = XFER_PIO_0;
  28. dev->xfer_mode = XFER_PIO_0;
  29. dev->xfer_shift = ATA_SHIFT_PIO;
  30. dev->flags |= ATA_DFLAG_PIO;
  31. }
  32. return 0;
  33. }
  34. static unsigned int ixp4xx_mmio_data_xfer(struct ata_queued_cmd *qc,
  35. unsigned char *buf, unsigned int buflen, int rw)
  36. {
  37. unsigned int i;
  38. unsigned int words = buflen >> 1;
  39. u16 *buf16 = (u16 *) buf;
  40. struct ata_port *ap = qc->dev->link->ap;
  41. void __iomem *mmio = ap->ioaddr.data_addr;
  42. struct ixp4xx_pata_data *data = dev_get_platdata(ap->host->dev);
  43. /* set the expansion bus in 16bit mode and restore
  44. * 8 bit mode after the transaction.
  45. */
  46. *data->cs0_cfg &= ~(0x01);
  47. udelay(100);
  48. /* Transfer multiple of 2 bytes */
  49. if (rw == READ)
  50. for (i = 0; i < words; i++)
  51. buf16[i] = readw(mmio);
  52. else
  53. for (i = 0; i < words; i++)
  54. writew(buf16[i], mmio);
  55. /* Transfer trailing 1 byte, if any. */
  56. if (unlikely(buflen & 0x01)) {
  57. u16 align_buf[1] = { 0 };
  58. unsigned char *trailing_buf = buf + buflen - 1;
  59. if (rw == READ) {
  60. align_buf[0] = readw(mmio);
  61. memcpy(trailing_buf, align_buf, 1);
  62. } else {
  63. memcpy(align_buf, trailing_buf, 1);
  64. writew(align_buf[0], mmio);
  65. }
  66. words++;
  67. }
  68. udelay(100);
  69. *data->cs0_cfg |= 0x01;
  70. return words << 1;
  71. }
  72. static struct scsi_host_template ixp4xx_sht = {
  73. ATA_PIO_SHT(DRV_NAME),
  74. };
  75. static struct ata_port_operations ixp4xx_port_ops = {
  76. .inherits = &ata_sff_port_ops,
  77. .sff_data_xfer = ixp4xx_mmio_data_xfer,
  78. .cable_detect = ata_cable_40wire,
  79. .set_mode = ixp4xx_set_mode,
  80. };
  81. static void ixp4xx_setup_port(struct ata_port *ap,
  82. struct ixp4xx_pata_data *data,
  83. unsigned long raw_cs0, unsigned long raw_cs1)
  84. {
  85. struct ata_ioports *ioaddr = &ap->ioaddr;
  86. unsigned long raw_cmd = raw_cs0;
  87. unsigned long raw_ctl = raw_cs1 + 0x06;
  88. ioaddr->cmd_addr = data->cs0;
  89. ioaddr->altstatus_addr = data->cs1 + 0x06;
  90. ioaddr->ctl_addr = data->cs1 + 0x06;
  91. ata_sff_std_ports(ioaddr);
  92. #ifndef __ARMEB__
  93. /* adjust the addresses to handle the address swizzling of the
  94. * ixp4xx in little endian mode.
  95. */
  96. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  97. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  98. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  99. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  100. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  101. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  102. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  103. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  104. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  105. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  106. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  107. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  108. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  109. raw_cmd ^= 0x03;
  110. raw_ctl ^= 0x03;
  111. #endif
  112. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl);
  113. }
  114. static int ixp4xx_pata_probe(struct platform_device *pdev)
  115. {
  116. unsigned int irq;
  117. struct resource *cs0, *cs1;
  118. struct ata_host *host;
  119. struct ata_port *ap;
  120. struct ixp4xx_pata_data *data = dev_get_platdata(&pdev->dev);
  121. int ret;
  122. cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  123. cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  124. if (!cs0 || !cs1)
  125. return -EINVAL;
  126. /* allocate host */
  127. host = ata_host_alloc(&pdev->dev, 1);
  128. if (!host)
  129. return -ENOMEM;
  130. /* acquire resources and fill host */
  131. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  132. if (ret)
  133. return ret;
  134. data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
  135. data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
  136. if (!data->cs0 || !data->cs1)
  137. return -ENOMEM;
  138. irq = platform_get_irq(pdev, 0);
  139. if (irq > 0)
  140. irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
  141. else if (irq < 0)
  142. return irq;
  143. else
  144. return -EINVAL;
  145. /* Setup expansion bus chip selects */
  146. *data->cs0_cfg = data->cs0_bits;
  147. *data->cs1_cfg = data->cs1_bits;
  148. ap = host->ports[0];
  149. ap->ops = &ixp4xx_port_ops;
  150. ap->pio_mask = ATA_PIO4;
  151. ap->flags |= ATA_FLAG_NO_ATAPI;
  152. ixp4xx_setup_port(ap, data, cs0->start, cs1->start);
  153. ata_print_version_once(&pdev->dev, DRV_VERSION);
  154. /* activate host */
  155. return ata_host_activate(host, irq, ata_sff_interrupt, 0, &ixp4xx_sht);
  156. }
  157. static struct platform_driver ixp4xx_pata_platform_driver = {
  158. .driver = {
  159. .name = DRV_NAME,
  160. },
  161. .probe = ixp4xx_pata_probe,
  162. .remove = ata_platform_remove_one,
  163. };
  164. module_platform_driver(ixp4xx_pata_platform_driver);
  165. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  166. MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
  167. MODULE_LICENSE("GPL");
  168. MODULE_VERSION(DRV_VERSION);
  169. MODULE_ALIAS("platform:" DRV_NAME);