pata_pxa.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/completion.h>
  29. #include <scsi/scsi_host.h>
  30. #include <mach/pxa2xx-regs.h>
  31. #include <linux/platform_data/ata-pxa.h>
  32. #include <mach/dma.h>
  33. #define DRV_NAME "pata_pxa"
  34. #define DRV_VERSION "0.1"
  35. struct pata_pxa_data {
  36. uint32_t dma_channel;
  37. struct pxa_dma_desc *dma_desc;
  38. dma_addr_t dma_desc_addr;
  39. uint32_t dma_desc_id;
  40. /* DMA IO physical address */
  41. uint32_t dma_io_addr;
  42. /* PXA DREQ<0:2> pin selector */
  43. uint32_t dma_dreq;
  44. /* DMA DCSR register value */
  45. uint32_t dma_dcsr;
  46. struct completion dma_done;
  47. };
  48. /*
  49. * Setup the DMA descriptors. The size is transfer capped at 4k per descriptor,
  50. * if the transfer is longer, it is split into multiple chained descriptors.
  51. */
  52. static void pxa_load_dmac(struct scatterlist *sg, struct ata_queued_cmd *qc)
  53. {
  54. struct pata_pxa_data *pd = qc->ap->private_data;
  55. uint32_t cpu_len, seg_len;
  56. dma_addr_t cpu_addr;
  57. cpu_addr = sg_dma_address(sg);
  58. cpu_len = sg_dma_len(sg);
  59. do {
  60. seg_len = (cpu_len > 0x1000) ? 0x1000 : cpu_len;
  61. pd->dma_desc[pd->dma_desc_id].ddadr = pd->dma_desc_addr +
  62. ((pd->dma_desc_id + 1) * sizeof(struct pxa_dma_desc));
  63. pd->dma_desc[pd->dma_desc_id].dcmd = DCMD_BURST32 |
  64. DCMD_WIDTH2 | (DCMD_LENGTH & seg_len);
  65. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  66. pd->dma_desc[pd->dma_desc_id].dsadr = cpu_addr;
  67. pd->dma_desc[pd->dma_desc_id].dtadr = pd->dma_io_addr;
  68. pd->dma_desc[pd->dma_desc_id].dcmd |= DCMD_INCSRCADDR |
  69. DCMD_FLOWTRG;
  70. } else {
  71. pd->dma_desc[pd->dma_desc_id].dsadr = pd->dma_io_addr;
  72. pd->dma_desc[pd->dma_desc_id].dtadr = cpu_addr;
  73. pd->dma_desc[pd->dma_desc_id].dcmd |= DCMD_INCTRGADDR |
  74. DCMD_FLOWSRC;
  75. }
  76. cpu_len -= seg_len;
  77. cpu_addr += seg_len;
  78. pd->dma_desc_id++;
  79. } while (cpu_len);
  80. /* Should not happen */
  81. if (seg_len & 0x1f)
  82. DALGN |= (1 << pd->dma_dreq);
  83. }
  84. /*
  85. * Prepare taskfile for submission.
  86. */
  87. static void pxa_qc_prep(struct ata_queued_cmd *qc)
  88. {
  89. struct pata_pxa_data *pd = qc->ap->private_data;
  90. int si = 0;
  91. struct scatterlist *sg;
  92. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  93. return;
  94. pd->dma_desc_id = 0;
  95. DCSR(pd->dma_channel) = 0;
  96. DALGN &= ~(1 << pd->dma_dreq);
  97. for_each_sg(qc->sg, sg, qc->n_elem, si)
  98. pxa_load_dmac(sg, qc);
  99. pd->dma_desc[pd->dma_desc_id - 1].ddadr = DDADR_STOP;
  100. /* Fire IRQ only at the end of last block */
  101. pd->dma_desc[pd->dma_desc_id - 1].dcmd |= DCMD_ENDIRQEN;
  102. DDADR(pd->dma_channel) = pd->dma_desc_addr;
  103. DRCMR(pd->dma_dreq) = DRCMR_MAPVLD | pd->dma_channel;
  104. }
  105. /*
  106. * Configure the DMA controller, load the DMA descriptors, but don't start the
  107. * DMA controller yet. Only issue the ATA command.
  108. */
  109. static void pxa_bmdma_setup(struct ata_queued_cmd *qc)
  110. {
  111. qc->ap->ops->sff_exec_command(qc->ap, &qc->tf);
  112. }
  113. /*
  114. * Execute the DMA transfer.
  115. */
  116. static void pxa_bmdma_start(struct ata_queued_cmd *qc)
  117. {
  118. struct pata_pxa_data *pd = qc->ap->private_data;
  119. init_completion(&pd->dma_done);
  120. DCSR(pd->dma_channel) = DCSR_RUN;
  121. }
  122. /*
  123. * Wait until the DMA transfer completes, then stop the DMA controller.
  124. */
  125. static void pxa_bmdma_stop(struct ata_queued_cmd *qc)
  126. {
  127. struct pata_pxa_data *pd = qc->ap->private_data;
  128. if ((DCSR(pd->dma_channel) & DCSR_RUN) &&
  129. wait_for_completion_timeout(&pd->dma_done, HZ))
  130. dev_err(qc->ap->dev, "Timeout waiting for DMA completion!");
  131. DCSR(pd->dma_channel) = 0;
  132. }
  133. /*
  134. * Read DMA status. The bmdma_stop() will take care of properly finishing the
  135. * DMA transfer so we always have DMA-complete interrupt here.
  136. */
  137. static unsigned char pxa_bmdma_status(struct ata_port *ap)
  138. {
  139. struct pata_pxa_data *pd = ap->private_data;
  140. unsigned char ret = ATA_DMA_INTR;
  141. if (pd->dma_dcsr & DCSR_BUSERR)
  142. ret |= ATA_DMA_ERR;
  143. return ret;
  144. }
  145. /*
  146. * No IRQ register present so we do nothing.
  147. */
  148. static void pxa_irq_clear(struct ata_port *ap)
  149. {
  150. }
  151. /*
  152. * Check for ATAPI DMA. ATAPI DMA is unsupported by this driver. It's still
  153. * unclear why ATAPI has DMA issues.
  154. */
  155. static int pxa_check_atapi_dma(struct ata_queued_cmd *qc)
  156. {
  157. return -EOPNOTSUPP;
  158. }
  159. static struct scsi_host_template pxa_ata_sht = {
  160. ATA_BMDMA_SHT(DRV_NAME),
  161. };
  162. static struct ata_port_operations pxa_ata_port_ops = {
  163. .inherits = &ata_bmdma_port_ops,
  164. .cable_detect = ata_cable_40wire,
  165. .bmdma_setup = pxa_bmdma_setup,
  166. .bmdma_start = pxa_bmdma_start,
  167. .bmdma_stop = pxa_bmdma_stop,
  168. .bmdma_status = pxa_bmdma_status,
  169. .check_atapi_dma = pxa_check_atapi_dma,
  170. .sff_irq_clear = pxa_irq_clear,
  171. .qc_prep = pxa_qc_prep,
  172. };
  173. /*
  174. * DMA interrupt handler.
  175. */
  176. static void pxa_ata_dma_irq(int dma, void *port)
  177. {
  178. struct ata_port *ap = port;
  179. struct pata_pxa_data *pd = ap->private_data;
  180. pd->dma_dcsr = DCSR(dma);
  181. DCSR(dma) = pd->dma_dcsr;
  182. if (pd->dma_dcsr & DCSR_STOPSTATE)
  183. complete(&pd->dma_done);
  184. }
  185. static int pxa_ata_probe(struct platform_device *pdev)
  186. {
  187. struct ata_host *host;
  188. struct ata_port *ap;
  189. struct pata_pxa_data *data;
  190. struct resource *cmd_res;
  191. struct resource *ctl_res;
  192. struct resource *dma_res;
  193. struct resource *irq_res;
  194. struct pata_pxa_pdata *pdata = dev_get_platdata(&pdev->dev);
  195. int ret = 0;
  196. /*
  197. * Resource validation, three resources are needed:
  198. * - CMD port base address
  199. * - CTL port base address
  200. * - DMA port base address
  201. * - IRQ pin
  202. */
  203. if (pdev->num_resources != 4) {
  204. dev_err(&pdev->dev, "invalid number of resources\n");
  205. return -EINVAL;
  206. }
  207. /*
  208. * CMD port base address
  209. */
  210. cmd_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  211. if (unlikely(cmd_res == NULL))
  212. return -EINVAL;
  213. /*
  214. * CTL port base address
  215. */
  216. ctl_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  217. if (unlikely(ctl_res == NULL))
  218. return -EINVAL;
  219. /*
  220. * DMA port base address
  221. */
  222. dma_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  223. if (unlikely(dma_res == NULL))
  224. return -EINVAL;
  225. /*
  226. * IRQ pin
  227. */
  228. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  229. if (unlikely(irq_res == NULL))
  230. return -EINVAL;
  231. /*
  232. * Allocate the host
  233. */
  234. host = ata_host_alloc(&pdev->dev, 1);
  235. if (!host)
  236. return -ENOMEM;
  237. ap = host->ports[0];
  238. ap->ops = &pxa_ata_port_ops;
  239. ap->pio_mask = ATA_PIO4;
  240. ap->mwdma_mask = ATA_MWDMA2;
  241. ap->ioaddr.cmd_addr = devm_ioremap(&pdev->dev, cmd_res->start,
  242. resource_size(cmd_res));
  243. ap->ioaddr.ctl_addr = devm_ioremap(&pdev->dev, ctl_res->start,
  244. resource_size(ctl_res));
  245. ap->ioaddr.bmdma_addr = devm_ioremap(&pdev->dev, dma_res->start,
  246. resource_size(dma_res));
  247. /*
  248. * Adjust register offsets
  249. */
  250. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  251. ap->ioaddr.data_addr = ap->ioaddr.cmd_addr +
  252. (ATA_REG_DATA << pdata->reg_shift);
  253. ap->ioaddr.error_addr = ap->ioaddr.cmd_addr +
  254. (ATA_REG_ERR << pdata->reg_shift);
  255. ap->ioaddr.feature_addr = ap->ioaddr.cmd_addr +
  256. (ATA_REG_FEATURE << pdata->reg_shift);
  257. ap->ioaddr.nsect_addr = ap->ioaddr.cmd_addr +
  258. (ATA_REG_NSECT << pdata->reg_shift);
  259. ap->ioaddr.lbal_addr = ap->ioaddr.cmd_addr +
  260. (ATA_REG_LBAL << pdata->reg_shift);
  261. ap->ioaddr.lbam_addr = ap->ioaddr.cmd_addr +
  262. (ATA_REG_LBAM << pdata->reg_shift);
  263. ap->ioaddr.lbah_addr = ap->ioaddr.cmd_addr +
  264. (ATA_REG_LBAH << pdata->reg_shift);
  265. ap->ioaddr.device_addr = ap->ioaddr.cmd_addr +
  266. (ATA_REG_DEVICE << pdata->reg_shift);
  267. ap->ioaddr.status_addr = ap->ioaddr.cmd_addr +
  268. (ATA_REG_STATUS << pdata->reg_shift);
  269. ap->ioaddr.command_addr = ap->ioaddr.cmd_addr +
  270. (ATA_REG_CMD << pdata->reg_shift);
  271. /*
  272. * Allocate and load driver's internal data structure
  273. */
  274. data = devm_kzalloc(&pdev->dev, sizeof(struct pata_pxa_data),
  275. GFP_KERNEL);
  276. if (!data)
  277. return -ENOMEM;
  278. ap->private_data = data;
  279. data->dma_dreq = pdata->dma_dreq;
  280. data->dma_io_addr = dma_res->start;
  281. /*
  282. * Allocate space for the DMA descriptors
  283. */
  284. data->dma_desc = dmam_alloc_coherent(&pdev->dev, PAGE_SIZE,
  285. &data->dma_desc_addr, GFP_KERNEL);
  286. if (!data->dma_desc)
  287. return -EINVAL;
  288. /*
  289. * Request the DMA channel
  290. */
  291. data->dma_channel = pxa_request_dma(DRV_NAME, DMA_PRIO_LOW,
  292. pxa_ata_dma_irq, ap);
  293. if (data->dma_channel < 0)
  294. return -EBUSY;
  295. /*
  296. * Stop and clear the DMA channel
  297. */
  298. DCSR(data->dma_channel) = 0;
  299. /*
  300. * Activate the ATA host
  301. */
  302. ret = ata_host_activate(host, irq_res->start, ata_sff_interrupt,
  303. pdata->irq_flags, &pxa_ata_sht);
  304. if (ret)
  305. pxa_free_dma(data->dma_channel);
  306. return ret;
  307. }
  308. static int pxa_ata_remove(struct platform_device *pdev)
  309. {
  310. struct ata_host *host = platform_get_drvdata(pdev);
  311. struct pata_pxa_data *data = host->ports[0]->private_data;
  312. pxa_free_dma(data->dma_channel);
  313. ata_host_detach(host);
  314. return 0;
  315. }
  316. static struct platform_driver pxa_ata_driver = {
  317. .probe = pxa_ata_probe,
  318. .remove = pxa_ata_remove,
  319. .driver = {
  320. .name = DRV_NAME,
  321. },
  322. };
  323. module_platform_driver(pxa_ata_driver);
  324. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  325. MODULE_DESCRIPTION("DMA-capable driver for PATA on PXA CPU");
  326. MODULE_LICENSE("GPL");
  327. MODULE_VERSION(DRV_VERSION);
  328. MODULE_ALIAS("platform:" DRV_NAME);