pata_ixp4xx_cf.c 8.8 KB

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