pata_ixp4xx_cf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ixp4xx PATA/Compact Flash driver
  4. * Copyright (C) 2006-07 Tower Technologies
  5. * Author: Alessandro Zummo <[email protected]>
  6. *
  7. * An ATA driver to handle a Compact Flash connected
  8. * to the ixp4xx expansion bus in TrueIDE mode. The CF
  9. * must have it chip selects connected to two CS lines
  10. * on the ixp4xx. In the irq is not available, you might
  11. * want to modify both this driver and libata to run in
  12. * polling mode.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/module.h>
  17. #include <linux/libata.h>
  18. #include <linux/irq.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <scsi/scsi_host.h>
  22. #define DRV_NAME "pata_ixp4xx_cf"
  23. #define DRV_VERSION "1.0"
  24. struct ixp4xx_pata {
  25. struct ata_host *host;
  26. struct regmap *rmap;
  27. u32 cmd_csreg;
  28. void __iomem *cmd;
  29. void __iomem *ctl;
  30. };
  31. #define IXP4XX_EXP_TIMING_STRIDE 0x04
  32. /* The timings for the chipselect is in bits 29..16 */
  33. #define IXP4XX_EXP_T1_T5_MASK GENMASK(29, 16)
  34. #define IXP4XX_EXP_PIO_0_8 0x0a470000
  35. #define IXP4XX_EXP_PIO_1_8 0x06430000
  36. #define IXP4XX_EXP_PIO_2_8 0x02410000
  37. #define IXP4XX_EXP_PIO_3_8 0x00820000
  38. #define IXP4XX_EXP_PIO_4_8 0x00400000
  39. #define IXP4XX_EXP_PIO_0_16 0x29640000
  40. #define IXP4XX_EXP_PIO_1_16 0x05030000
  41. #define IXP4XX_EXP_PIO_2_16 0x00b20000
  42. #define IXP4XX_EXP_PIO_3_16 0x00820000
  43. #define IXP4XX_EXP_PIO_4_16 0x00400000
  44. #define IXP4XX_EXP_BW_MASK (BIT(6)|BIT(0))
  45. #define IXP4XX_EXP_BYTE_RD16 BIT(6) /* Byte reads on half-word devices */
  46. #define IXP4XX_EXP_BYTE_EN BIT(0) /* Use 8bit data bus if set */
  47. static void ixp4xx_set_8bit_timing(struct ixp4xx_pata *ixpp, u8 pio_mode)
  48. {
  49. switch (pio_mode) {
  50. case XFER_PIO_0:
  51. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  52. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_0_8);
  53. break;
  54. case XFER_PIO_1:
  55. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  56. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_1_8);
  57. break;
  58. case XFER_PIO_2:
  59. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  60. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_2_8);
  61. break;
  62. case XFER_PIO_3:
  63. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  64. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_3_8);
  65. break;
  66. case XFER_PIO_4:
  67. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  68. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_4_8);
  69. break;
  70. default:
  71. break;
  72. }
  73. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  74. IXP4XX_EXP_BW_MASK, IXP4XX_EXP_BYTE_RD16|IXP4XX_EXP_BYTE_EN);
  75. }
  76. static void ixp4xx_set_16bit_timing(struct ixp4xx_pata *ixpp, u8 pio_mode)
  77. {
  78. switch (pio_mode){
  79. case XFER_PIO_0:
  80. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  81. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_0_16);
  82. break;
  83. case XFER_PIO_1:
  84. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  85. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_1_16);
  86. break;
  87. case XFER_PIO_2:
  88. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  89. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_2_16);
  90. break;
  91. case XFER_PIO_3:
  92. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  93. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_3_16);
  94. break;
  95. case XFER_PIO_4:
  96. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  97. IXP4XX_EXP_T1_T5_MASK, IXP4XX_EXP_PIO_4_16);
  98. break;
  99. default:
  100. break;
  101. }
  102. regmap_update_bits(ixpp->rmap, ixpp->cmd_csreg,
  103. IXP4XX_EXP_BW_MASK, IXP4XX_EXP_BYTE_RD16);
  104. }
  105. /* This sets up the timing on the chipselect CMD accordingly */
  106. static void ixp4xx_set_piomode(struct ata_port *ap, struct ata_device *adev)
  107. {
  108. struct ixp4xx_pata *ixpp = ap->host->private_data;
  109. ata_dev_info(adev, "configured for PIO%d 8bit\n",
  110. adev->pio_mode - XFER_PIO_0);
  111. ixp4xx_set_8bit_timing(ixpp, adev->pio_mode);
  112. }
  113. static unsigned int ixp4xx_mmio_data_xfer(struct ata_queued_cmd *qc,
  114. unsigned char *buf, unsigned int buflen, int rw)
  115. {
  116. unsigned int i;
  117. unsigned int words = buflen >> 1;
  118. u16 *buf16 = (u16 *) buf;
  119. struct ata_device *adev = qc->dev;
  120. struct ata_port *ap = qc->dev->link->ap;
  121. void __iomem *mmio = ap->ioaddr.data_addr;
  122. struct ixp4xx_pata *ixpp = ap->host->private_data;
  123. unsigned long flags;
  124. ata_dev_dbg(adev, "%s %d bytes\n", (rw == READ) ? "READ" : "WRITE",
  125. buflen);
  126. spin_lock_irqsave(ap->lock, flags);
  127. /* set the expansion bus in 16bit mode and restore
  128. * 8 bit mode after the transaction.
  129. */
  130. ixp4xx_set_16bit_timing(ixpp, adev->pio_mode);
  131. udelay(5);
  132. /* Transfer multiple of 2 bytes */
  133. if (rw == READ)
  134. for (i = 0; i < words; i++)
  135. buf16[i] = readw(mmio);
  136. else
  137. for (i = 0; i < words; i++)
  138. writew(buf16[i], mmio);
  139. /* Transfer trailing 1 byte, if any. */
  140. if (unlikely(buflen & 0x01)) {
  141. u16 align_buf[1] = { 0 };
  142. unsigned char *trailing_buf = buf + buflen - 1;
  143. if (rw == READ) {
  144. align_buf[0] = readw(mmio);
  145. memcpy(trailing_buf, align_buf, 1);
  146. } else {
  147. memcpy(align_buf, trailing_buf, 1);
  148. writew(align_buf[0], mmio);
  149. }
  150. words++;
  151. }
  152. ixp4xx_set_8bit_timing(ixpp, adev->pio_mode);
  153. udelay(5);
  154. spin_unlock_irqrestore(ap->lock, flags);
  155. return words << 1;
  156. }
  157. static struct scsi_host_template ixp4xx_sht = {
  158. ATA_PIO_SHT(DRV_NAME),
  159. };
  160. static struct ata_port_operations ixp4xx_port_ops = {
  161. .inherits = &ata_sff_port_ops,
  162. .sff_data_xfer = ixp4xx_mmio_data_xfer,
  163. .cable_detect = ata_cable_40wire,
  164. .set_piomode = ixp4xx_set_piomode,
  165. };
  166. static struct ata_port_info ixp4xx_port_info = {
  167. .flags = ATA_FLAG_NO_ATAPI,
  168. .pio_mask = ATA_PIO4,
  169. .port_ops = &ixp4xx_port_ops,
  170. };
  171. static void ixp4xx_setup_port(struct ata_port *ap,
  172. struct ixp4xx_pata *ixpp,
  173. unsigned long raw_cmd, unsigned long raw_ctl)
  174. {
  175. struct ata_ioports *ioaddr = &ap->ioaddr;
  176. raw_ctl += 0x06;
  177. ioaddr->cmd_addr = ixpp->cmd;
  178. ioaddr->altstatus_addr = ixpp->ctl + 0x06;
  179. ioaddr->ctl_addr = ixpp->ctl + 0x06;
  180. ata_sff_std_ports(ioaddr);
  181. if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
  182. /* adjust the addresses to handle the address swizzling of the
  183. * ixp4xx in little endian mode.
  184. */
  185. *(unsigned long *)&ioaddr->data_addr ^= 0x02;
  186. *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
  187. *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
  188. *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
  189. *(unsigned long *)&ioaddr->error_addr ^= 0x03;
  190. *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
  191. *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
  192. *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
  193. *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
  194. *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
  195. *(unsigned long *)&ioaddr->device_addr ^= 0x03;
  196. *(unsigned long *)&ioaddr->status_addr ^= 0x03;
  197. *(unsigned long *)&ioaddr->command_addr ^= 0x03;
  198. raw_cmd ^= 0x03;
  199. raw_ctl ^= 0x03;
  200. }
  201. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl);
  202. }
  203. static int ixp4xx_pata_probe(struct platform_device *pdev)
  204. {
  205. struct resource *cmd, *ctl;
  206. struct ata_port_info pi = ixp4xx_port_info;
  207. const struct ata_port_info *ppi[] = { &pi, NULL };
  208. struct device *dev = &pdev->dev;
  209. struct device_node *np = dev->of_node;
  210. struct ixp4xx_pata *ixpp;
  211. u32 csindex;
  212. int ret;
  213. int irq;
  214. cmd = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  215. ctl = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  216. if (!cmd || !ctl)
  217. return -EINVAL;
  218. ixpp = devm_kzalloc(dev, sizeof(*ixpp), GFP_KERNEL);
  219. if (!ixpp)
  220. return -ENOMEM;
  221. ixpp->rmap = syscon_node_to_regmap(np->parent);
  222. if (IS_ERR(ixpp->rmap))
  223. return dev_err_probe(dev, PTR_ERR(ixpp->rmap), "no regmap\n");
  224. /* Inspect our address to figure out what chipselect the CMD is on */
  225. ret = of_property_read_u32_index(np, "reg", 0, &csindex);
  226. if (ret)
  227. return dev_err_probe(dev, ret, "can't inspect CMD address\n");
  228. dev_info(dev, "using CS%d for PIO timing configuration\n", csindex);
  229. ixpp->cmd_csreg = csindex * IXP4XX_EXP_TIMING_STRIDE;
  230. ixpp->host = ata_host_alloc_pinfo(dev, ppi, 1);
  231. if (!ixpp->host)
  232. return -ENOMEM;
  233. ixpp->host->private_data = ixpp;
  234. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  235. if (ret)
  236. return ret;
  237. ixpp->cmd = devm_ioremap_resource(dev, cmd);
  238. ixpp->ctl = devm_ioremap_resource(dev, ctl);
  239. if (IS_ERR(ixpp->cmd) || IS_ERR(ixpp->ctl))
  240. return -ENOMEM;
  241. irq = platform_get_irq(pdev, 0);
  242. if (irq > 0)
  243. irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
  244. else if (irq < 0)
  245. return irq;
  246. else
  247. return -EINVAL;
  248. /* Just one port to set up */
  249. ixp4xx_setup_port(ixpp->host->ports[0], ixpp, cmd->start, ctl->start);
  250. ata_print_version_once(dev, DRV_VERSION);
  251. return ata_host_activate(ixpp->host, irq, ata_sff_interrupt, 0, &ixp4xx_sht);
  252. }
  253. static const struct of_device_id ixp4xx_pata_of_match[] = {
  254. { .compatible = "intel,ixp4xx-compact-flash", },
  255. { /* sentinel */ }
  256. };
  257. static struct platform_driver ixp4xx_pata_platform_driver = {
  258. .driver = {
  259. .name = DRV_NAME,
  260. .of_match_table = ixp4xx_pata_of_match,
  261. },
  262. .probe = ixp4xx_pata_probe,
  263. .remove = ata_platform_remove_one,
  264. };
  265. module_platform_driver(ixp4xx_pata_platform_driver);
  266. MODULE_AUTHOR("Alessandro Zummo <[email protected]>");
  267. MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
  268. MODULE_LICENSE("GPL");
  269. MODULE_VERSION(DRV_VERSION);
  270. MODULE_ALIAS("platform:" DRV_NAME);