powerpc/mpc5xxx: Use of_get_next_parent to simplify code
[linux/fpc-iii.git] / drivers / ata / pata_pxa.c
blobc36b3e6531d89f482a73299ea28c54b7da13a668
1 /*
2 * Generic PXA PATA driver
4 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
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.
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.
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.
21 #include <linux/kernel.h>
22 #include <linux/module.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>
31 #include <scsi/scsi_host.h>
33 #include <mach/pxa2xx-regs.h>
34 #include <linux/platform_data/ata-pxa.h>
35 #include <mach/dma.h>
37 #define DRV_NAME "pata_pxa"
38 #define DRV_VERSION "0.1"
40 struct pata_pxa_data {
41 uint32_t dma_channel;
42 struct pxa_dma_desc *dma_desc;
43 dma_addr_t dma_desc_addr;
44 uint32_t dma_desc_id;
46 /* DMA IO physical address */
47 uint32_t dma_io_addr;
48 /* PXA DREQ<0:2> pin selector */
49 uint32_t dma_dreq;
50 /* DMA DCSR register value */
51 uint32_t dma_dcsr;
53 struct completion dma_done;
57 * Setup the DMA descriptors. The size is transfer capped at 4k per descriptor,
58 * if the transfer is longer, it is split into multiple chained descriptors.
60 static void pxa_load_dmac(struct scatterlist *sg, struct ata_queued_cmd *qc)
62 struct pata_pxa_data *pd = qc->ap->private_data;
64 uint32_t cpu_len, seg_len;
65 dma_addr_t cpu_addr;
67 cpu_addr = sg_dma_address(sg);
68 cpu_len = sg_dma_len(sg);
70 do {
71 seg_len = (cpu_len > 0x1000) ? 0x1000 : cpu_len;
73 pd->dma_desc[pd->dma_desc_id].ddadr = pd->dma_desc_addr +
74 ((pd->dma_desc_id + 1) * sizeof(struct pxa_dma_desc));
76 pd->dma_desc[pd->dma_desc_id].dcmd = DCMD_BURST32 |
77 DCMD_WIDTH2 | (DCMD_LENGTH & seg_len);
79 if (qc->tf.flags & ATA_TFLAG_WRITE) {
80 pd->dma_desc[pd->dma_desc_id].dsadr = cpu_addr;
81 pd->dma_desc[pd->dma_desc_id].dtadr = pd->dma_io_addr;
82 pd->dma_desc[pd->dma_desc_id].dcmd |= DCMD_INCSRCADDR |
83 DCMD_FLOWTRG;
84 } else {
85 pd->dma_desc[pd->dma_desc_id].dsadr = pd->dma_io_addr;
86 pd->dma_desc[pd->dma_desc_id].dtadr = cpu_addr;
87 pd->dma_desc[pd->dma_desc_id].dcmd |= DCMD_INCTRGADDR |
88 DCMD_FLOWSRC;
91 cpu_len -= seg_len;
92 cpu_addr += seg_len;
93 pd->dma_desc_id++;
95 } while (cpu_len);
97 /* Should not happen */
98 if (seg_len & 0x1f)
99 DALGN |= (1 << pd->dma_dreq);
103 * Prepare taskfile for submission.
105 static void pxa_qc_prep(struct ata_queued_cmd *qc)
107 struct pata_pxa_data *pd = qc->ap->private_data;
108 int si = 0;
109 struct scatterlist *sg;
111 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
112 return;
114 pd->dma_desc_id = 0;
116 DCSR(pd->dma_channel) = 0;
117 DALGN &= ~(1 << pd->dma_dreq);
119 for_each_sg(qc->sg, sg, qc->n_elem, si)
120 pxa_load_dmac(sg, qc);
122 pd->dma_desc[pd->dma_desc_id - 1].ddadr = DDADR_STOP;
124 /* Fire IRQ only at the end of last block */
125 pd->dma_desc[pd->dma_desc_id - 1].dcmd |= DCMD_ENDIRQEN;
127 DDADR(pd->dma_channel) = pd->dma_desc_addr;
128 DRCMR(pd->dma_dreq) = DRCMR_MAPVLD | pd->dma_channel;
133 * Configure the DMA controller, load the DMA descriptors, but don't start the
134 * DMA controller yet. Only issue the ATA command.
136 static void pxa_bmdma_setup(struct ata_queued_cmd *qc)
138 qc->ap->ops->sff_exec_command(qc->ap, &qc->tf);
142 * Execute the DMA transfer.
144 static void pxa_bmdma_start(struct ata_queued_cmd *qc)
146 struct pata_pxa_data *pd = qc->ap->private_data;
147 init_completion(&pd->dma_done);
148 DCSR(pd->dma_channel) = DCSR_RUN;
152 * Wait until the DMA transfer completes, then stop the DMA controller.
154 static void pxa_bmdma_stop(struct ata_queued_cmd *qc)
156 struct pata_pxa_data *pd = qc->ap->private_data;
158 if ((DCSR(pd->dma_channel) & DCSR_RUN) &&
159 wait_for_completion_timeout(&pd->dma_done, HZ))
160 dev_err(qc->ap->dev, "Timeout waiting for DMA completion!");
162 DCSR(pd->dma_channel) = 0;
166 * Read DMA status. The bmdma_stop() will take care of properly finishing the
167 * DMA transfer so we always have DMA-complete interrupt here.
169 static unsigned char pxa_bmdma_status(struct ata_port *ap)
171 struct pata_pxa_data *pd = ap->private_data;
172 unsigned char ret = ATA_DMA_INTR;
174 if (pd->dma_dcsr & DCSR_BUSERR)
175 ret |= ATA_DMA_ERR;
177 return ret;
181 * No IRQ register present so we do nothing.
183 static void pxa_irq_clear(struct ata_port *ap)
188 * Check for ATAPI DMA. ATAPI DMA is unsupported by this driver. It's still
189 * unclear why ATAPI has DMA issues.
191 static int pxa_check_atapi_dma(struct ata_queued_cmd *qc)
193 return -EOPNOTSUPP;
196 static struct scsi_host_template pxa_ata_sht = {
197 ATA_BMDMA_SHT(DRV_NAME),
200 static struct ata_port_operations pxa_ata_port_ops = {
201 .inherits = &ata_bmdma_port_ops,
202 .cable_detect = ata_cable_40wire,
204 .bmdma_setup = pxa_bmdma_setup,
205 .bmdma_start = pxa_bmdma_start,
206 .bmdma_stop = pxa_bmdma_stop,
207 .bmdma_status = pxa_bmdma_status,
209 .check_atapi_dma = pxa_check_atapi_dma,
211 .sff_irq_clear = pxa_irq_clear,
213 .qc_prep = pxa_qc_prep,
217 * DMA interrupt handler.
219 static void pxa_ata_dma_irq(int dma, void *port)
221 struct ata_port *ap = port;
222 struct pata_pxa_data *pd = ap->private_data;
224 pd->dma_dcsr = DCSR(dma);
225 DCSR(dma) = pd->dma_dcsr;
227 if (pd->dma_dcsr & DCSR_STOPSTATE)
228 complete(&pd->dma_done);
231 static int pxa_ata_probe(struct platform_device *pdev)
233 struct ata_host *host;
234 struct ata_port *ap;
235 struct pata_pxa_data *data;
236 struct resource *cmd_res;
237 struct resource *ctl_res;
238 struct resource *dma_res;
239 struct resource *irq_res;
240 struct pata_pxa_pdata *pdata = dev_get_platdata(&pdev->dev);
241 int ret = 0;
244 * Resource validation, three resources are needed:
245 * - CMD port base address
246 * - CTL port base address
247 * - DMA port base address
248 * - IRQ pin
250 if (pdev->num_resources != 4) {
251 dev_err(&pdev->dev, "invalid number of resources\n");
252 return -EINVAL;
256 * CMD port base address
258 cmd_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
259 if (unlikely(cmd_res == NULL))
260 return -EINVAL;
263 * CTL port base address
265 ctl_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
266 if (unlikely(ctl_res == NULL))
267 return -EINVAL;
270 * DMA port base address
272 dma_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
273 if (unlikely(dma_res == NULL))
274 return -EINVAL;
277 * IRQ pin
279 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
280 if (unlikely(irq_res == NULL))
281 return -EINVAL;
284 * Allocate the host
286 host = ata_host_alloc(&pdev->dev, 1);
287 if (!host)
288 return -ENOMEM;
290 ap = host->ports[0];
291 ap->ops = &pxa_ata_port_ops;
292 ap->pio_mask = ATA_PIO4;
293 ap->mwdma_mask = ATA_MWDMA2;
295 ap->ioaddr.cmd_addr = devm_ioremap(&pdev->dev, cmd_res->start,
296 resource_size(cmd_res));
297 ap->ioaddr.ctl_addr = devm_ioremap(&pdev->dev, ctl_res->start,
298 resource_size(ctl_res));
299 ap->ioaddr.bmdma_addr = devm_ioremap(&pdev->dev, dma_res->start,
300 resource_size(dma_res));
303 * Adjust register offsets
305 ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
306 ap->ioaddr.data_addr = ap->ioaddr.cmd_addr +
307 (ATA_REG_DATA << pdata->reg_shift);
308 ap->ioaddr.error_addr = ap->ioaddr.cmd_addr +
309 (ATA_REG_ERR << pdata->reg_shift);
310 ap->ioaddr.feature_addr = ap->ioaddr.cmd_addr +
311 (ATA_REG_FEATURE << pdata->reg_shift);
312 ap->ioaddr.nsect_addr = ap->ioaddr.cmd_addr +
313 (ATA_REG_NSECT << pdata->reg_shift);
314 ap->ioaddr.lbal_addr = ap->ioaddr.cmd_addr +
315 (ATA_REG_LBAL << pdata->reg_shift);
316 ap->ioaddr.lbam_addr = ap->ioaddr.cmd_addr +
317 (ATA_REG_LBAM << pdata->reg_shift);
318 ap->ioaddr.lbah_addr = ap->ioaddr.cmd_addr +
319 (ATA_REG_LBAH << pdata->reg_shift);
320 ap->ioaddr.device_addr = ap->ioaddr.cmd_addr +
321 (ATA_REG_DEVICE << pdata->reg_shift);
322 ap->ioaddr.status_addr = ap->ioaddr.cmd_addr +
323 (ATA_REG_STATUS << pdata->reg_shift);
324 ap->ioaddr.command_addr = ap->ioaddr.cmd_addr +
325 (ATA_REG_CMD << pdata->reg_shift);
328 * Allocate and load driver's internal data structure
330 data = devm_kzalloc(&pdev->dev, sizeof(struct pata_pxa_data),
331 GFP_KERNEL);
332 if (!data)
333 return -ENOMEM;
335 ap->private_data = data;
336 data->dma_dreq = pdata->dma_dreq;
337 data->dma_io_addr = dma_res->start;
340 * Allocate space for the DMA descriptors
342 data->dma_desc = dmam_alloc_coherent(&pdev->dev, PAGE_SIZE,
343 &data->dma_desc_addr, GFP_KERNEL);
344 if (!data->dma_desc)
345 return -EINVAL;
348 * Request the DMA channel
350 data->dma_channel = pxa_request_dma(DRV_NAME, DMA_PRIO_LOW,
351 pxa_ata_dma_irq, ap);
352 if (data->dma_channel < 0)
353 return -EBUSY;
356 * Stop and clear the DMA channel
358 DCSR(data->dma_channel) = 0;
361 * Activate the ATA host
363 ret = ata_host_activate(host, irq_res->start, ata_sff_interrupt,
364 pdata->irq_flags, &pxa_ata_sht);
365 if (ret)
366 pxa_free_dma(data->dma_channel);
368 return ret;
371 static int pxa_ata_remove(struct platform_device *pdev)
373 struct ata_host *host = platform_get_drvdata(pdev);
374 struct pata_pxa_data *data = host->ports[0]->private_data;
376 pxa_free_dma(data->dma_channel);
378 ata_host_detach(host);
380 return 0;
383 static struct platform_driver pxa_ata_driver = {
384 .probe = pxa_ata_probe,
385 .remove = pxa_ata_remove,
386 .driver = {
387 .name = DRV_NAME,
391 module_platform_driver(pxa_ata_driver);
393 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
394 MODULE_DESCRIPTION("DMA-capable driver for PATA on PXA CPU");
395 MODULE_LICENSE("GPL");
396 MODULE_VERSION(DRV_VERSION);
397 MODULE_ALIAS("platform:" DRV_NAME);