pata_pxa.c 9.7 KB

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