pata_ixp4xx_cf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * ixp4xx PATA/Compact Flash driver
  3. * Copyright (c) 2006 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. The interrupt line is optional, if not
  10. * specified the driver will run in polling mode.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/libata.h>
  20. #include <linux/irq.h>
  21. #include <linux/platform_device.h>
  22. #include <scsi/scsi_host.h>
  23. #define DRV_NAME "pata_ixp4xx_cf"
  24. #define DRV_VERSION "0.1.2"
  25. static int ixp4xx_set_mode(struct ata_port *ap, struct ata_device **error)
  26. {
  27. int i;
  28. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  29. struct ata_device *dev = &ap->device[i];
  30. if (ata_dev_ready(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 void ixp4xx_phy_reset(struct ata_port *ap)
  41. {
  42. ap->cbl = ATA_CBL_PATA40;
  43. ata_port_probe(ap);
  44. ata_bus_reset(ap);
  45. }
  46. static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  47. unsigned int buflen, int write_data)
  48. {
  49. unsigned int i;
  50. unsigned int words = buflen >> 1;
  51. u16 *buf16 = (u16 *) buf;
  52. struct ata_port *ap = adev->ap;
  53. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  54. struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
  55. /* set the expansion bus in 16bit mode and restore
  56. * 8 bit mode after the transaction.
  57. */
  58. *data->cs0_cfg &= ~(0x01);
  59. udelay(100);
  60. /* Transfer multiple of 2 bytes */
  61. if (write_data) {
  62. for (i = 0; i < words; i++)
  63. writew(buf16[i], mmio);
  64. } else {
  65. for (i = 0; i < words; i++)
  66. buf16[i] = readw(mmio);
  67. }
  68. /* Transfer trailing 1 byte, if any. */
  69. if (unlikely(buflen & 0x01)) {
  70. u16 align_buf[1] = { 0 };
  71. unsigned char *trailing_buf = buf + buflen - 1;
  72. if (write_data) {
  73. memcpy(align_buf, trailing_buf, 1);
  74. writew(align_buf[0], mmio);
  75. } else {
  76. align_buf[0] = readw(mmio);
  77. memcpy(trailing_buf, align_buf, 1);
  78. }
  79. }
  80. udelay(100);
  81. *data->cs0_cfg |= 0x01;
  82. }
  83. static void ixp4xx_irq_clear(struct ata_port *ap)
  84. {
  85. }
  86. static struct scsi_host_template ixp4xx_sht = {
  87. .module = THIS_MODULE,
  88. .name = DRV_NAME,
  89. .ioctl = ata_scsi_ioctl,
  90. .queuecommand = ata_scsi_queuecmd,
  91. .can_queue = ATA_DEF_QUEUE,
  92. .this_id = ATA_SHT_THIS_ID,
  93. .sg_tablesize = LIBATA_MAX_PRD,
  94. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  95. .emulated = ATA_SHT_EMULATED,
  96. .use_clustering = ATA_SHT_USE_CLUSTERING,
  97. .proc_name = DRV_NAME,
  98. .dma_boundary = ATA_DMA_BOUNDARY,
  99. .slave_configure = ata_scsi_slave_config,
  100. .slave_destroy = ata_scsi_slave_destroy,
  101. .bios_param = ata_std_bios_param,
  102. };
  103. static struct ata_port_operations ixp4xx_port_ops = {
  104. .set_mode = ixp4xx_set_mode,
  105. .mode_filter = ata_pci_default_filter,
  106. .port_disable = ata_port_disable,
  107. .tf_load = ata_tf_load,
  108. .tf_read = ata_tf_read,
  109. .check_status = ata_check_status,
  110. .exec_command = ata_exec_command,
  111. .dev_select = ata_std_dev_select,
  112. .qc_prep = ata_qc_prep,
  113. .qc_issue = ata_qc_issue_prot,
  114. .eng_timeout = ata_eng_timeout,
  115. .data_xfer = ixp4xx_mmio_data_xfer,
  116. .irq_handler = ata_interrupt,
  117. .irq_clear = ixp4xx_irq_clear,
  118. .irq_on = ata_irq_on,
  119. .irq_ack = ata_irq_ack,
  120. .port_start = ata_port_start,
  121. .phy_reset = ixp4xx_phy_reset,
  122. };
  123. static void ixp4xx_setup_port(struct ata_ioports *ioaddr,
  124. struct ixp4xx_pata_data *data)
  125. {
  126. ioaddr->cmd_addr = data->cs0;
  127. ioaddr->altstatus_addr = data->cs1 + 0x06;
  128. ioaddr->ctl_addr = data->cs1 + 0x06;
  129. ata_std_ports(ioaddr);
  130. #ifndef __ARMEB__
  131. /* adjust the addresses to handle the address swizzling of the
  132. * ixp4xx in little endian mode.
  133. */
  134. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  135. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  136. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  137. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  138. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  139. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  140. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  141. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  142. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  143. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  144. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  145. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  146. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  147. #endif
  148. }
  149. static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
  150. {
  151. int ret;
  152. unsigned int irq;
  153. struct resource *cs0, *cs1;
  154. struct ata_probe_ent ae;
  155. struct ixp4xx_pata_data *data = pdev->dev.platform_data;
  156. cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  158. if (!cs0 || !cs1)
  159. return -EINVAL;
  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_HIGH);
  166. /* Setup expansion bus chip selects */
  167. *data->cs0_cfg = data->cs0_bits;
  168. *data->cs1_cfg = data->cs1_bits;
  169. memset(&ae, 0, sizeof(struct ata_probe_ent));
  170. INIT_LIST_HEAD(&ae.node);
  171. ae.dev = &pdev->dev;
  172. ae.port_ops = &ixp4xx_port_ops;
  173. ae.sht = &ixp4xx_sht;
  174. ae.n_ports = 1;
  175. ae.pio_mask = 0x1f; /* PIO4 */
  176. ae.irq = irq;
  177. ae.irq_flags = 0;
  178. ae.port_flags = ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY
  179. | ATA_FLAG_NO_ATAPI | ATA_FLAG_SRST;
  180. /* run in polling mode if no irq has been assigned */
  181. if (!irq)
  182. ae.port_flags |= ATA_FLAG_PIO_POLLING;
  183. ixp4xx_setup_port(&ae.port[0], data);
  184. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  185. ret = ata_device_add(&ae);
  186. if (ret == 0)
  187. return -ENODEV;
  188. return 0;
  189. }
  190. static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
  191. {
  192. struct ata_host *host = platform_get_drvdata(dev);
  193. ata_host_detach(host);
  194. return 0;
  195. }
  196. static struct platform_driver ixp4xx_pata_platform_driver = {
  197. .driver = {
  198. .name = DRV_NAME,
  199. .owner = THIS_MODULE,
  200. },
  201. .probe = ixp4xx_pata_probe,
  202. .remove = __devexit_p(ixp4xx_pata_remove),
  203. };
  204. static int __init ixp4xx_pata_init(void)
  205. {
  206. return platform_driver_register(&ixp4xx_pata_platform_driver);
  207. }
  208. static void __exit ixp4xx_pata_exit(void)
  209. {
  210. platform_driver_unregister(&ixp4xx_pata_platform_driver);
  211. }
  212. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  213. MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
  214. MODULE_LICENSE("GPL");
  215. MODULE_VERSION(DRV_VERSION);
  216. module_init(ixp4xx_pata_init);
  217. module_exit(ixp4xx_pata_exit);