pata_pxa.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Generic PXA PATA driver
  3. *
  4. * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; see the file COPYING. If not, write to
  18. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/ata.h>
  24. #include <linux/libata.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/gpio.h>
  28. #include <linux/slab.h>
  29. #include <linux/completion.h>
  30. #include <scsi/scsi_host.h>
  31. #include <linux/platform_data/ata-pxa.h>
  32. #define DRV_NAME "pata_pxa"
  33. #define DRV_VERSION "0.1"
  34. struct pata_pxa_data {
  35. struct dma_chan *dma_chan;
  36. dma_cookie_t dma_cookie;
  37. struct completion dma_done;
  38. };
  39. /*
  40. * DMA interrupt handler.
  41. */
  42. static void pxa_ata_dma_irq(void *d)
  43. {
  44. struct pata_pxa_data *pd = d;
  45. enum dma_status status;
  46. status = dmaengine_tx_status(pd->dma_chan, pd->dma_cookie, NULL);
  47. if (status == DMA_ERROR || status == DMA_COMPLETE)
  48. complete(&pd->dma_done);
  49. }
  50. /*
  51. * Prepare taskfile for submission.
  52. */
  53. static void pxa_qc_prep(struct ata_queued_cmd *qc)
  54. {
  55. struct pata_pxa_data *pd = qc->ap->private_data;
  56. struct dma_async_tx_descriptor *tx;
  57. enum dma_transfer_direction dir;
  58. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  59. return;
  60. dir = (qc->dma_dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM);
  61. tx = dmaengine_prep_slave_sg(pd->dma_chan, qc->sg, qc->n_elem, dir,
  62. DMA_PREP_INTERRUPT);
  63. if (!tx) {
  64. ata_dev_err(qc->dev, "prep_slave_sg() failed\n");
  65. return;
  66. }
  67. tx->callback = pxa_ata_dma_irq;
  68. tx->callback_param = pd;
  69. pd->dma_cookie = dmaengine_submit(tx);
  70. }
  71. /*
  72. * Configure the DMA controller, load the DMA descriptors, but don't start the
  73. * DMA controller yet. Only issue the ATA command.
  74. */
  75. static void pxa_bmdma_setup(struct ata_queued_cmd *qc)
  76. {
  77. qc->ap->ops->sff_exec_command(qc->ap, &qc->tf);
  78. }
  79. /*
  80. * Execute the DMA transfer.
  81. */
  82. static void pxa_bmdma_start(struct ata_queued_cmd *qc)
  83. {
  84. struct pata_pxa_data *pd = qc->ap->private_data;
  85. init_completion(&pd->dma_done);
  86. dma_async_issue_pending(pd->dma_chan);
  87. }
  88. /*
  89. * Wait until the DMA transfer completes, then stop the DMA controller.
  90. */
  91. static void pxa_bmdma_stop(struct ata_queued_cmd *qc)
  92. {
  93. struct pata_pxa_data *pd = qc->ap->private_data;
  94. enum dma_status status;
  95. status = dmaengine_tx_status(pd->dma_chan, pd->dma_cookie, NULL);
  96. if (status != DMA_ERROR && status != DMA_COMPLETE &&
  97. wait_for_completion_timeout(&pd->dma_done, HZ))
  98. ata_dev_err(qc->dev, "Timeout waiting for DMA completion!");
  99. dmaengine_terminate_all(pd->dma_chan);
  100. }
  101. /*
  102. * Read DMA status. The bmdma_stop() will take care of properly finishing the
  103. * DMA transfer so we always have DMA-complete interrupt here.
  104. */
  105. static unsigned char pxa_bmdma_status(struct ata_port *ap)
  106. {
  107. struct pata_pxa_data *pd = ap->private_data;
  108. unsigned char ret = ATA_DMA_INTR;
  109. struct dma_tx_state state;
  110. enum dma_status status;
  111. status = dmaengine_tx_status(pd->dma_chan, pd->dma_cookie, &state);
  112. if (status != DMA_COMPLETE)
  113. ret |= ATA_DMA_ERR;
  114. return ret;
  115. }
  116. /*
  117. * No IRQ register present so we do nothing.
  118. */
  119. static void pxa_irq_clear(struct ata_port *ap)
  120. {
  121. }
  122. /*
  123. * Check for ATAPI DMA. ATAPI DMA is unsupported by this driver. It's still
  124. * unclear why ATAPI has DMA issues.
  125. */
  126. static int pxa_check_atapi_dma(struct ata_queued_cmd *qc)
  127. {
  128. return -EOPNOTSUPP;
  129. }
  130. static struct scsi_host_template pxa_ata_sht = {
  131. ATA_BMDMA_SHT(DRV_NAME),
  132. };
  133. static struct ata_port_operations pxa_ata_port_ops = {
  134. .inherits = &ata_bmdma_port_ops,
  135. .cable_detect = ata_cable_40wire,
  136. .bmdma_setup = pxa_bmdma_setup,
  137. .bmdma_start = pxa_bmdma_start,
  138. .bmdma_stop = pxa_bmdma_stop,
  139. .bmdma_status = pxa_bmdma_status,
  140. .check_atapi_dma = pxa_check_atapi_dma,
  141. .sff_irq_clear = pxa_irq_clear,
  142. .qc_prep = pxa_qc_prep,
  143. };
  144. static int pxa_ata_probe(struct platform_device *pdev)
  145. {
  146. struct ata_host *host;
  147. struct ata_port *ap;
  148. struct pata_pxa_data *data;
  149. struct resource *cmd_res;
  150. struct resource *ctl_res;
  151. struct resource *dma_res;
  152. struct resource *irq_res;
  153. struct pata_pxa_pdata *pdata = dev_get_platdata(&pdev->dev);
  154. struct dma_slave_config config;
  155. int ret = 0;
  156. /*
  157. * Resource validation, three resources are needed:
  158. * - CMD port base address
  159. * - CTL port base address
  160. * - DMA port base address
  161. * - IRQ pin
  162. */
  163. if (pdev->num_resources != 4) {
  164. dev_err(&pdev->dev, "invalid number of resources\n");
  165. return -EINVAL;
  166. }
  167. /*
  168. * CMD port base address
  169. */
  170. cmd_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (unlikely(cmd_res == NULL))
  172. return -EINVAL;
  173. /*
  174. * CTL port base address
  175. */
  176. ctl_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  177. if (unlikely(ctl_res == NULL))
  178. return -EINVAL;
  179. /*
  180. * DMA port base address
  181. */
  182. dma_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  183. if (unlikely(dma_res == NULL))
  184. return -EINVAL;
  185. /*
  186. * IRQ pin
  187. */
  188. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  189. if (unlikely(irq_res == NULL))
  190. return -EINVAL;
  191. /*
  192. * Allocate the host
  193. */
  194. host = ata_host_alloc(&pdev->dev, 1);
  195. if (!host)
  196. return -ENOMEM;
  197. ap = host->ports[0];
  198. ap->ops = &pxa_ata_port_ops;
  199. ap->pio_mask = ATA_PIO4;
  200. ap->mwdma_mask = ATA_MWDMA2;
  201. ap->ioaddr.cmd_addr = devm_ioremap(&pdev->dev, cmd_res->start,
  202. resource_size(cmd_res));
  203. ap->ioaddr.ctl_addr = devm_ioremap(&pdev->dev, ctl_res->start,
  204. resource_size(ctl_res));
  205. ap->ioaddr.bmdma_addr = devm_ioremap(&pdev->dev, dma_res->start,
  206. resource_size(dma_res));
  207. /*
  208. * Adjust register offsets
  209. */
  210. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  211. ap->ioaddr.data_addr = ap->ioaddr.cmd_addr +
  212. (ATA_REG_DATA << pdata->reg_shift);
  213. ap->ioaddr.error_addr = ap->ioaddr.cmd_addr +
  214. (ATA_REG_ERR << pdata->reg_shift);
  215. ap->ioaddr.feature_addr = ap->ioaddr.cmd_addr +
  216. (ATA_REG_FEATURE << pdata->reg_shift);
  217. ap->ioaddr.nsect_addr = ap->ioaddr.cmd_addr +
  218. (ATA_REG_NSECT << pdata->reg_shift);
  219. ap->ioaddr.lbal_addr = ap->ioaddr.cmd_addr +
  220. (ATA_REG_LBAL << pdata->reg_shift);
  221. ap->ioaddr.lbam_addr = ap->ioaddr.cmd_addr +
  222. (ATA_REG_LBAM << pdata->reg_shift);
  223. ap->ioaddr.lbah_addr = ap->ioaddr.cmd_addr +
  224. (ATA_REG_LBAH << pdata->reg_shift);
  225. ap->ioaddr.device_addr = ap->ioaddr.cmd_addr +
  226. (ATA_REG_DEVICE << pdata->reg_shift);
  227. ap->ioaddr.status_addr = ap->ioaddr.cmd_addr +
  228. (ATA_REG_STATUS << pdata->reg_shift);
  229. ap->ioaddr.command_addr = ap->ioaddr.cmd_addr +
  230. (ATA_REG_CMD << pdata->reg_shift);
  231. /*
  232. * Allocate and load driver's internal data structure
  233. */
  234. data = devm_kzalloc(&pdev->dev, sizeof(struct pata_pxa_data),
  235. GFP_KERNEL);
  236. if (!data)
  237. return -ENOMEM;
  238. ap->private_data = data;
  239. memset(&config, 0, sizeof(config));
  240. config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  241. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  242. config.src_addr = dma_res->start;
  243. config.dst_addr = dma_res->start;
  244. config.src_maxburst = 32;
  245. config.dst_maxburst = 32;
  246. /*
  247. * Request the DMA channel
  248. */
  249. data->dma_chan =
  250. dma_request_slave_channel(&pdev->dev, "data");
  251. if (!data->dma_chan)
  252. return -EBUSY;
  253. ret = dmaengine_slave_config(data->dma_chan, &config);
  254. if (ret < 0) {
  255. dev_err(&pdev->dev, "dma configuration failed: %d\n", ret);
  256. return ret;
  257. }
  258. /*
  259. * Activate the ATA host
  260. */
  261. ret = ata_host_activate(host, irq_res->start, ata_sff_interrupt,
  262. pdata->irq_flags, &pxa_ata_sht);
  263. if (ret)
  264. dma_release_channel(data->dma_chan);
  265. return ret;
  266. }
  267. static int pxa_ata_remove(struct platform_device *pdev)
  268. {
  269. struct ata_host *host = platform_get_drvdata(pdev);
  270. struct pata_pxa_data *data = host->ports[0]->private_data;
  271. dma_release_channel(data->dma_chan);
  272. ata_host_detach(host);
  273. return 0;
  274. }
  275. static struct platform_driver pxa_ata_driver = {
  276. .probe = pxa_ata_probe,
  277. .remove = pxa_ata_remove,
  278. .driver = {
  279. .name = DRV_NAME,
  280. },
  281. };
  282. module_platform_driver(pxa_ata_driver);
  283. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  284. MODULE_DESCRIPTION("DMA-capable driver for PATA on PXA CPU");
  285. MODULE_LICENSE("GPL");
  286. MODULE_VERSION(DRV_VERSION);
  287. MODULE_ALIAS("platform:" DRV_NAME);