pata_ixp4xx_cf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_port *ap, struct ata_device **error)
  27. {
  28. int i;
  29. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  30. struct ata_device *dev = &ap->device[i];
  31. if (ata_dev_enabled(dev)) {
  32. ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
  33. dev->pio_mode = XFER_PIO_0;
  34. dev->xfer_mode = XFER_PIO_0;
  35. dev->xfer_shift = ATA_SHIFT_PIO;
  36. dev->flags |= ATA_DFLAG_PIO;
  37. }
  38. }
  39. return 0;
  40. }
  41. static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  42. unsigned int buflen, int write_data)
  43. {
  44. unsigned int i;
  45. unsigned int words = buflen >> 1;
  46. u16 *buf16 = (u16 *) buf;
  47. struct ata_port *ap = adev->ap;
  48. void __iomem *mmio = ap->ioaddr.data_addr;
  49. struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
  50. /* set the expansion bus in 16bit mode and restore
  51. * 8 bit mode after the transaction.
  52. */
  53. *data->cs0_cfg &= ~(0x01);
  54. udelay(100);
  55. /* Transfer multiple of 2 bytes */
  56. if (write_data) {
  57. for (i = 0; i < words; i++)
  58. writew(buf16[i], mmio);
  59. } else {
  60. for (i = 0; i < words; i++)
  61. buf16[i] = readw(mmio);
  62. }
  63. /* Transfer trailing 1 byte, if any. */
  64. if (unlikely(buflen & 0x01)) {
  65. u16 align_buf[1] = { 0 };
  66. unsigned char *trailing_buf = buf + buflen - 1;
  67. if (write_data) {
  68. memcpy(align_buf, trailing_buf, 1);
  69. writew(align_buf[0], mmio);
  70. } else {
  71. align_buf[0] = readw(mmio);
  72. memcpy(trailing_buf, align_buf, 1);
  73. }
  74. }
  75. udelay(100);
  76. *data->cs0_cfg |= 0x01;
  77. }
  78. static struct scsi_host_template ixp4xx_sht = {
  79. .module = THIS_MODULE,
  80. .name = DRV_NAME,
  81. .ioctl = ata_scsi_ioctl,
  82. .queuecommand = ata_scsi_queuecmd,
  83. .can_queue = ATA_DEF_QUEUE,
  84. .this_id = ATA_SHT_THIS_ID,
  85. .sg_tablesize = LIBATA_MAX_PRD,
  86. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  87. .emulated = ATA_SHT_EMULATED,
  88. .use_clustering = ATA_SHT_USE_CLUSTERING,
  89. .proc_name = DRV_NAME,
  90. .dma_boundary = ATA_DMA_BOUNDARY,
  91. .slave_configure = ata_scsi_slave_config,
  92. .slave_destroy = ata_scsi_slave_destroy,
  93. .bios_param = ata_std_bios_param,
  94. };
  95. static struct ata_port_operations ixp4xx_port_ops = {
  96. .set_mode = ixp4xx_set_mode,
  97. .mode_filter = ata_pci_default_filter,
  98. .port_disable = ata_port_disable,
  99. .tf_load = ata_tf_load,
  100. .tf_read = ata_tf_read,
  101. .exec_command = ata_exec_command,
  102. .check_status = ata_check_status,
  103. .dev_select = ata_std_dev_select,
  104. .freeze = ata_bmdma_freeze,
  105. .thaw = ata_bmdma_thaw,
  106. .error_handler = ata_bmdma_error_handler,
  107. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  108. .qc_prep = ata_qc_prep,
  109. .qc_issue = ata_qc_issue_prot,
  110. .data_xfer = ixp4xx_mmio_data_xfer,
  111. .cable_detect = ata_cable_40wire,
  112. .irq_handler = ata_interrupt,
  113. .irq_clear = ata_bmdma_irq_clear,
  114. .irq_on = ata_irq_on,
  115. .irq_ack = ata_dummy_irq_ack,
  116. .port_start = ata_port_start,
  117. };
  118. static void ixp4xx_setup_port(struct ata_ioports *ioaddr,
  119. struct ixp4xx_pata_data *data)
  120. {
  121. ioaddr->cmd_addr = data->cs0;
  122. ioaddr->altstatus_addr = data->cs1 + 0x06;
  123. ioaddr->ctl_addr = data->cs1 + 0x06;
  124. ata_std_ports(ioaddr);
  125. #ifndef __ARMEB__
  126. /* adjust the addresses to handle the address swizzling of the
  127. * ixp4xx in little endian mode.
  128. */
  129. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  130. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  131. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  132. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  133. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  134. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  135. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  136. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  137. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  138. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  139. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  140. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  141. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  142. #endif
  143. }
  144. static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
  145. {
  146. unsigned int irq;
  147. struct resource *cs0, *cs1;
  148. struct ata_host *host;
  149. struct ata_port *ap;
  150. struct ixp4xx_pata_data *data = pdev->dev.platform_data;
  151. cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  153. if (!cs0 || !cs1)
  154. return -EINVAL;
  155. /* allocate host */
  156. host = ata_host_alloc(&pdev->dev, 1);
  157. if (!host)
  158. return -ENOMEM;
  159. /* acquire resources and fill host */
  160. pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  161. data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
  162. data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
  163. irq = platform_get_irq(pdev, 0);
  164. if (irq)
  165. set_irq_type(irq, IRQT_RISING);
  166. /* Setup expansion bus chip selects */
  167. *data->cs0_cfg = data->cs0_bits;
  168. *data->cs1_cfg = data->cs1_bits;
  169. ap = host->ports[0];
  170. ap->ops = &ixp4xx_port_ops;
  171. ap->pio_mask = 0x1f; /* PIO4 */
  172. ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
  173. ixp4xx_setup_port(&ap->ioaddr, data);
  174. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  175. /* activate host */
  176. return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht);
  177. }
  178. static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
  179. {
  180. struct ata_host *host = platform_get_drvdata(dev);
  181. ata_host_detach(host);
  182. return 0;
  183. }
  184. static struct platform_driver ixp4xx_pata_platform_driver = {
  185. .driver = {
  186. .name = DRV_NAME,
  187. .owner = THIS_MODULE,
  188. },
  189. .probe = ixp4xx_pata_probe,
  190. .remove = __devexit_p(ixp4xx_pata_remove),
  191. };
  192. static int __init ixp4xx_pata_init(void)
  193. {
  194. return platform_driver_register(&ixp4xx_pata_platform_driver);
  195. }
  196. static void __exit ixp4xx_pata_exit(void)
  197. {
  198. platform_driver_unregister(&ixp4xx_pata_platform_driver);
  199. }
  200. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  201. MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
  202. MODULE_LICENSE("GPL");
  203. MODULE_VERSION(DRV_VERSION);
  204. module_init(ixp4xx_pata_init);
  205. module_exit(ixp4xx_pata_exit);