pata_ixp4xx_cf.c 6.6 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.1ac1"
  25. static int ixp4xx_set_mode(struct ata_port *ap, struct ata_device *adev)
  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_enabled(dev)) {
  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. }
  37. return 0;
  38. }
  39. static void ixp4xx_phy_reset(struct ata_port *ap)
  40. {
  41. ap->cbl = ATA_CBL_PATA40;
  42. ata_port_probe(ap);
  43. ata_bus_reset(ap);
  44. }
  45. static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
  46. unsigned int buflen, int write_data)
  47. {
  48. unsigned int i;
  49. unsigned int words = buflen >> 1;
  50. u16 *buf16 = (u16 *) buf;
  51. struct ata_port *ap = adev->ap;
  52. void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
  53. struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
  54. /* set the expansion bus in 16bit mode and restore
  55. * 8 bit mode after the transaction.
  56. */
  57. *data->cs0_cfg &= ~(0x01);
  58. udelay(100);
  59. /* Transfer multiple of 2 bytes */
  60. if (write_data) {
  61. for (i = 0; i < words; i++)
  62. writew(buf16[i], mmio);
  63. } else {
  64. for (i = 0; i < words; i++)
  65. buf16[i] = readw(mmio);
  66. }
  67. /* Transfer trailing 1 byte, if any. */
  68. if (unlikely(buflen & 0x01)) {
  69. u16 align_buf[1] = { 0 };
  70. unsigned char *trailing_buf = buf + buflen - 1;
  71. if (write_data) {
  72. memcpy(align_buf, trailing_buf, 1);
  73. writew(align_buf[0], mmio);
  74. } else {
  75. align_buf[0] = readw(mmio);
  76. memcpy(trailing_buf, align_buf, 1);
  77. }
  78. }
  79. udelay(100);
  80. *data->cs0_cfg |= 0x01;
  81. }
  82. static void ixp4xx_irq_clear(struct ata_port *ap)
  83. {
  84. }
  85. static struct scsi_host_template ixp4xx_sht = {
  86. .module = THIS_MODULE,
  87. .name = DRV_NAME,
  88. .ioctl = ata_scsi_ioctl,
  89. .queuecommand = ata_scsi_queuecmd,
  90. .can_queue = ATA_DEF_QUEUE,
  91. .this_id = ATA_SHT_THIS_ID,
  92. .sg_tablesize = LIBATA_MAX_PRD,
  93. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  94. .emulated = ATA_SHT_EMULATED,
  95. .use_clustering = ATA_SHT_USE_CLUSTERING,
  96. .proc_name = DRV_NAME,
  97. .dma_boundary = ATA_DMA_BOUNDARY,
  98. .slave_configure = ata_scsi_slave_config,
  99. .slave_destroy = ata_scsi_slave_destroy,
  100. .bios_param = ata_std_bios_param,
  101. };
  102. static struct ata_port_operations ixp4xx_port_ops = {
  103. .set_mode = ixp4xx_set_mode,
  104. .mode_filter = ata_pci_default_filter,
  105. .port_disable = ata_port_disable,
  106. .tf_load = ata_tf_load,
  107. .tf_read = ata_tf_read,
  108. .check_status = ata_check_status,
  109. .exec_command = ata_exec_command,
  110. .dev_select = ata_std_dev_select,
  111. .qc_prep = ata_qc_prep,
  112. .qc_issue = ata_qc_issue_prot,
  113. .eng_timeout = ata_eng_timeout,
  114. .data_xfer = ixp4xx_mmio_data_xfer,
  115. .irq_handler = ata_interrupt,
  116. .irq_clear = ixp4xx_irq_clear,
  117. .irq_on = ata_irq_on,
  118. .irq_ack = ata_irq_ack,
  119. .port_start = ata_port_start,
  120. .phy_reset = ixp4xx_phy_reset,
  121. };
  122. static void ixp4xx_setup_port(struct ata_ioports *ioaddr,
  123. struct ixp4xx_pata_data *data)
  124. {
  125. ioaddr->cmd_addr = data->cs0;
  126. ioaddr->altstatus_addr = data->cs1 + 0x06;
  127. ioaddr->ctl_addr = data->cs1 + 0x06;
  128. ata_std_ports(ioaddr);
  129. #ifndef __ARMEB__
  130. /* adjust the addresses to handle the address swizzling of the
  131. * ixp4xx in little endian mode.
  132. */
  133. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  134. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  135. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  136. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  137. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  138. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  139. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  140. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  141. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  142. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  143. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  144. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  145. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  146. #endif
  147. }
  148. static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
  149. {
  150. int ret;
  151. unsigned int irq;
  152. struct resource *cs0, *cs1;
  153. struct ata_probe_ent ae;
  154. struct ixp4xx_pata_data *data = pdev->dev.platform_data;
  155. cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  157. if (!cs0 || !cs1)
  158. return -EINVAL;
  159. pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
  160. data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
  161. data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
  162. irq = platform_get_irq(pdev, 0);
  163. if (irq)
  164. set_irq_type(irq, IRQT_HIGH);
  165. /* Setup expansion bus chip selects */
  166. *data->cs0_cfg = data->cs0_bits;
  167. *data->cs1_cfg = data->cs1_bits;
  168. memset(&ae, 0, sizeof(struct ata_probe_ent));
  169. INIT_LIST_HEAD(&ae.node);
  170. ae.dev = &pdev->dev;
  171. ae.port_ops = &ixp4xx_port_ops;
  172. ae.sht = &ixp4xx_sht;
  173. ae.n_ports = 1;
  174. ae.pio_mask = 0x1f; /* PIO4 */
  175. ae.irq = irq;
  176. ae.irq_flags = 0;
  177. ae.port_flags = ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY
  178. | ATA_FLAG_NO_ATAPI | ATA_FLAG_SRST;
  179. /* run in polling mode if no irq has been assigned */
  180. if (!irq)
  181. ae.port_flags |= ATA_FLAG_PIO_POLLING;
  182. ixp4xx_setup_port(&ae.port[0], data);
  183. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  184. ret = ata_device_add(&ae);
  185. if (ret == 0)
  186. return -ENODEV;
  187. return 0;
  188. }
  189. static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
  190. {
  191. struct ata_host *host = platform_get_drvdata(dev);
  192. ata_host_detach(host);
  193. platform_set_drvdata(dev, NULL);
  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);