pata_ixp4xx_cf.c 5.4 KB

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