pata_ixp4xx_cf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_link_for_each_dev(dev, link) {
  30. if (ata_dev_enabled(dev)) {
  31. ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
  32. dev->pio_mode = XFER_PIO_0;
  33. dev->xfer_mode = XFER_PIO_0;
  34. dev->xfer_shift = ATA_SHIFT_PIO;
  35. dev->flags |= ATA_DFLAG_PIO;
  36. }
  37. }
  38. return 0;
  39. }
  40. static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev,
  41. unsigned char *buf, unsigned int buflen, int rw)
  42. {
  43. unsigned int i;
  44. unsigned int words = buflen >> 1;
  45. u16 *buf16 = (u16 *) buf;
  46. struct ata_port *ap = dev->link->ap;
  47. void __iomem *mmio = ap->ioaddr.data_addr;
  48. struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
  49. /* set the expansion bus in 16bit mode and restore
  50. * 8 bit mode after the transaction.
  51. */
  52. *data->cs0_cfg &= ~(0x01);
  53. udelay(100);
  54. /* Transfer multiple of 2 bytes */
  55. if (rw == READ)
  56. for (i = 0; i < words; i++)
  57. buf16[i] = readw(mmio);
  58. else
  59. for (i = 0; i < words; i++)
  60. writew(buf16[i], mmio);
  61. /* Transfer trailing 1 byte, if any. */
  62. if (unlikely(buflen & 0x01)) {
  63. u16 align_buf[1] = { 0 };
  64. unsigned char *trailing_buf = buf + buflen - 1;
  65. if (rw == READ) {
  66. align_buf[0] = readw(mmio);
  67. memcpy(trailing_buf, align_buf, 1);
  68. } else {
  69. memcpy(align_buf, trailing_buf, 1);
  70. writew(align_buf[0], mmio);
  71. }
  72. words++;
  73. }
  74. udelay(100);
  75. *data->cs0_cfg |= 0x01;
  76. return words << 1;
  77. }
  78. static struct scsi_host_template ixp4xx_sht = {
  79. ATA_PIO_SHT(DRV_NAME),
  80. };
  81. static struct ata_port_operations ixp4xx_port_ops = {
  82. .inherits = &ata_sff_port_ops,
  83. .sff_data_xfer = ixp4xx_mmio_data_xfer,
  84. .cable_detect = ata_cable_40wire,
  85. .set_mode = ixp4xx_set_mode,
  86. };
  87. static void ixp4xx_setup_port(struct ata_port *ap,
  88. struct ixp4xx_pata_data *data,
  89. unsigned long raw_cs0, unsigned long raw_cs1)
  90. {
  91. struct ata_ioports *ioaddr = &ap->ioaddr;
  92. unsigned long raw_cmd = raw_cs0;
  93. unsigned long raw_ctl = raw_cs1 + 0x06;
  94. ioaddr->cmd_addr = data->cs0;
  95. ioaddr->altstatus_addr = data->cs1 + 0x06;
  96. ioaddr->ctl_addr = data->cs1 + 0x06;
  97. ata_sff_std_ports(ioaddr);
  98. #ifndef __ARMEB__
  99. /* adjust the addresses to handle the address swizzling of the
  100. * ixp4xx in little endian mode.
  101. */
  102. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  103. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  104. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  105. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  106. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  107. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  108. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  109. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  110. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  111. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  112. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  113. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  114. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  115. raw_cmd ^= 0x03;
  116. raw_ctl ^= 0x03;
  117. #endif
  118. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl);
  119. }
  120. static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
  121. {
  122. unsigned int irq;
  123. struct resource *cs0, *cs1;
  124. struct ata_host *host;
  125. struct ata_port *ap;
  126. struct ixp4xx_pata_data *data = pdev->dev.platform_data;
  127. cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  129. if (!cs0 || !cs1)
  130. return -EINVAL;
  131. /* allocate host */
  132. host = ata_host_alloc(&pdev->dev, 1);
  133. if (!host)
  134. return -ENOMEM;
  135. /* acquire resources and fill host */
  136. pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  137. data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
  138. data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
  139. if (!data->cs0 || !data->cs1)
  140. return -ENOMEM;
  141. irq = platform_get_irq(pdev, 0);
  142. if (irq)
  143. set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
  144. /* Setup expansion bus chip selects */
  145. *data->cs0_cfg = data->cs0_bits;
  146. *data->cs1_cfg = data->cs1_bits;
  147. ap = host->ports[0];
  148. ap->ops = &ixp4xx_port_ops;
  149. ap->pio_mask = 0x1f; /* PIO4 */
  150. ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
  151. ixp4xx_setup_port(ap, data, cs0->start, cs1->start);
  152. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  153. /* activate host */
  154. return ata_host_activate(host, irq, ata_sff_interrupt, 0, &ixp4xx_sht);
  155. }
  156. static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
  157. {
  158. struct ata_host *host = platform_get_drvdata(dev);
  159. ata_host_detach(host);
  160. return 0;
  161. }
  162. static struct platform_driver ixp4xx_pata_platform_driver = {
  163. .driver = {
  164. .name = DRV_NAME,
  165. .owner = THIS_MODULE,
  166. },
  167. .probe = ixp4xx_pata_probe,
  168. .remove = __devexit_p(ixp4xx_pata_remove),
  169. };
  170. static int __init ixp4xx_pata_init(void)
  171. {
  172. return platform_driver_register(&ixp4xx_pata_platform_driver);
  173. }
  174. static void __exit ixp4xx_pata_exit(void)
  175. {
  176. platform_driver_unregister(&ixp4xx_pata_platform_driver);
  177. }
  178. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  179. MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
  180. MODULE_LICENSE("GPL");
  181. MODULE_VERSION(DRV_VERSION);
  182. MODULE_ALIAS("platform:" DRV_NAME);
  183. module_init(ixp4xx_pata_init);
  184. module_exit(ixp4xx_pata_exit);