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)
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/init.h>
24 #include <linux/blkdev.h>
25 #include <linux/ata.h>
26 #include <linux/libata.h>
27 #include <linux/platform_device.h>
28 #include <linux/gpio.h>
29 #include <linux/slab.h>
30 #include <linux/completion.h>
32 #include <scsi/scsi_host.h>
34 #include <mach/pxa2xx-regs.h>
35 #include <mach/pata_pxa.h>
38 #define DRV_NAME "pata_pxa"
39 #define DRV_VERSION "0.1"
41 struct pata_pxa_data
{
43 struct pxa_dma_desc
*dma_desc
;
44 dma_addr_t dma_desc_addr
;
47 /* DMA IO physical address */
49 /* PXA DREQ<0:2> pin selector */
51 /* DMA DCSR register value */
54 struct completion dma_done
;
58 * Setup the DMA descriptors. The size is transfer capped at 4k per descriptor,
59 * if the transfer is longer, it is split into multiple chained descriptors.
61 static void pxa_load_dmac(struct scatterlist
*sg
, struct ata_queued_cmd
*qc
)
63 struct pata_pxa_data
*pd
= qc
->ap
->private_data
;
65 uint32_t cpu_len
, seg_len
;
68 cpu_addr
= sg_dma_address(sg
);
69 cpu_len
= sg_dma_len(sg
);
72 seg_len
= (cpu_len
> 0x1000) ? 0x1000 : cpu_len
;
74 pd
->dma_desc
[pd
->dma_desc_id
].ddadr
= pd
->dma_desc_addr
+
75 ((pd
->dma_desc_id
+ 1) * sizeof(struct pxa_dma_desc
));
77 pd
->dma_desc
[pd
->dma_desc_id
].dcmd
= DCMD_BURST32
|
78 DCMD_WIDTH2
| (DCMD_LENGTH
& seg_len
);
80 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
81 pd
->dma_desc
[pd
->dma_desc_id
].dsadr
= cpu_addr
;
82 pd
->dma_desc
[pd
->dma_desc_id
].dtadr
= pd
->dma_io_addr
;
83 pd
->dma_desc
[pd
->dma_desc_id
].dcmd
|= DCMD_INCSRCADDR
|
86 pd
->dma_desc
[pd
->dma_desc_id
].dsadr
= pd
->dma_io_addr
;
87 pd
->dma_desc
[pd
->dma_desc_id
].dtadr
= cpu_addr
;
88 pd
->dma_desc
[pd
->dma_desc_id
].dcmd
|= DCMD_INCTRGADDR
|
98 /* Should not happen */
100 DALGN
|= (1 << pd
->dma_dreq
);
104 * Prepare taskfile for submission.
106 static void pxa_qc_prep(struct ata_queued_cmd
*qc
)
108 struct pata_pxa_data
*pd
= qc
->ap
->private_data
;
110 struct scatterlist
*sg
;
112 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
117 DCSR(pd
->dma_channel
) = 0;
118 DALGN
&= ~(1 << pd
->dma_dreq
);
120 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
)
121 pxa_load_dmac(sg
, qc
);
123 pd
->dma_desc
[pd
->dma_desc_id
- 1].ddadr
= DDADR_STOP
;
125 /* Fire IRQ only at the end of last block */
126 pd
->dma_desc
[pd
->dma_desc_id
- 1].dcmd
|= DCMD_ENDIRQEN
;
128 DDADR(pd
->dma_channel
) = pd
->dma_desc_addr
;
129 DRCMR(pd
->dma_dreq
) = DRCMR_MAPVLD
| pd
->dma_channel
;
134 * Configure the DMA controller, load the DMA descriptors, but don't start the
135 * DMA controller yet. Only issue the ATA command.
137 static void pxa_bmdma_setup(struct ata_queued_cmd
*qc
)
139 qc
->ap
->ops
->sff_exec_command(qc
->ap
, &qc
->tf
);
143 * Execute the DMA transfer.
145 static void pxa_bmdma_start(struct ata_queued_cmd
*qc
)
147 struct pata_pxa_data
*pd
= qc
->ap
->private_data
;
148 init_completion(&pd
->dma_done
);
149 DCSR(pd
->dma_channel
) = DCSR_RUN
;
153 * Wait until the DMA transfer completes, then stop the DMA controller.
155 static void pxa_bmdma_stop(struct ata_queued_cmd
*qc
)
157 struct pata_pxa_data
*pd
= qc
->ap
->private_data
;
159 if ((DCSR(pd
->dma_channel
) & DCSR_RUN
) &&
160 wait_for_completion_timeout(&pd
->dma_done
, HZ
))
161 dev_err(qc
->ap
->dev
, "Timeout waiting for DMA completion!");
163 DCSR(pd
->dma_channel
) = 0;
167 * Read DMA status. The bmdma_stop() will take care of properly finishing the
168 * DMA transfer so we always have DMA-complete interrupt here.
170 static unsigned char pxa_bmdma_status(struct ata_port
*ap
)
172 struct pata_pxa_data
*pd
= ap
->private_data
;
173 unsigned char ret
= ATA_DMA_INTR
;
175 if (pd
->dma_dcsr
& DCSR_BUSERR
)
182 * No IRQ register present so we do nothing.
184 static void pxa_irq_clear(struct ata_port
*ap
)
189 * Check for ATAPI DMA. ATAPI DMA is unsupported by this driver. It's still
190 * unclear why ATAPI has DMA issues.
192 static int pxa_check_atapi_dma(struct ata_queued_cmd
*qc
)
197 static struct scsi_host_template pxa_ata_sht
= {
198 ATA_BMDMA_SHT(DRV_NAME
),
201 static struct ata_port_operations pxa_ata_port_ops
= {
202 .inherits
= &ata_bmdma_port_ops
,
203 .cable_detect
= ata_cable_40wire
,
205 .bmdma_setup
= pxa_bmdma_setup
,
206 .bmdma_start
= pxa_bmdma_start
,
207 .bmdma_stop
= pxa_bmdma_stop
,
208 .bmdma_status
= pxa_bmdma_status
,
210 .check_atapi_dma
= pxa_check_atapi_dma
,
212 .sff_irq_clear
= pxa_irq_clear
,
214 .qc_prep
= pxa_qc_prep
,
218 * DMA interrupt handler.
220 static void pxa_ata_dma_irq(int dma
, void *port
)
222 struct ata_port
*ap
= port
;
223 struct pata_pxa_data
*pd
= ap
->private_data
;
225 pd
->dma_dcsr
= DCSR(dma
);
226 DCSR(dma
) = pd
->dma_dcsr
;
228 if (pd
->dma_dcsr
& DCSR_STOPSTATE
)
229 complete(&pd
->dma_done
);
232 static int __devinit
pxa_ata_probe(struct platform_device
*pdev
)
234 struct ata_host
*host
;
236 struct pata_pxa_data
*data
;
237 struct resource
*cmd_res
;
238 struct resource
*ctl_res
;
239 struct resource
*dma_res
;
240 struct resource
*irq_res
;
241 struct pata_pxa_pdata
*pdata
= pdev
->dev
.platform_data
;
245 * Resource validation, three resources are needed:
246 * - CMD port base address
247 * - CTL port base address
248 * - DMA port base address
251 if (pdev
->num_resources
!= 4) {
252 dev_err(&pdev
->dev
, "invalid number of resources\n");
257 * CMD port base address
259 cmd_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
260 if (unlikely(cmd_res
== NULL
))
264 * CTL port base address
266 ctl_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
267 if (unlikely(ctl_res
== NULL
))
271 * DMA port base address
273 dma_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
274 if (unlikely(dma_res
== NULL
))
280 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
281 if (unlikely(irq_res
== NULL
))
287 host
= ata_host_alloc(&pdev
->dev
, 1);
292 ap
->ops
= &pxa_ata_port_ops
;
293 ap
->pio_mask
= ATA_PIO4
;
294 ap
->mwdma_mask
= ATA_MWDMA2
;
295 ap
->flags
= ATA_FLAG_MMIO
;
297 ap
->ioaddr
.cmd_addr
= devm_ioremap(&pdev
->dev
, cmd_res
->start
,
298 resource_size(cmd_res
));
299 ap
->ioaddr
.ctl_addr
= devm_ioremap(&pdev
->dev
, ctl_res
->start
,
300 resource_size(ctl_res
));
301 ap
->ioaddr
.bmdma_addr
= devm_ioremap(&pdev
->dev
, dma_res
->start
,
302 resource_size(dma_res
));
305 * Adjust register offsets
307 ap
->ioaddr
.altstatus_addr
= ap
->ioaddr
.ctl_addr
;
308 ap
->ioaddr
.data_addr
= ap
->ioaddr
.cmd_addr
+
309 (ATA_REG_DATA
<< pdata
->reg_shift
);
310 ap
->ioaddr
.error_addr
= ap
->ioaddr
.cmd_addr
+
311 (ATA_REG_ERR
<< pdata
->reg_shift
);
312 ap
->ioaddr
.feature_addr
= ap
->ioaddr
.cmd_addr
+
313 (ATA_REG_FEATURE
<< pdata
->reg_shift
);
314 ap
->ioaddr
.nsect_addr
= ap
->ioaddr
.cmd_addr
+
315 (ATA_REG_NSECT
<< pdata
->reg_shift
);
316 ap
->ioaddr
.lbal_addr
= ap
->ioaddr
.cmd_addr
+
317 (ATA_REG_LBAL
<< pdata
->reg_shift
);
318 ap
->ioaddr
.lbam_addr
= ap
->ioaddr
.cmd_addr
+
319 (ATA_REG_LBAM
<< pdata
->reg_shift
);
320 ap
->ioaddr
.lbah_addr
= ap
->ioaddr
.cmd_addr
+
321 (ATA_REG_LBAH
<< pdata
->reg_shift
);
322 ap
->ioaddr
.device_addr
= ap
->ioaddr
.cmd_addr
+
323 (ATA_REG_DEVICE
<< pdata
->reg_shift
);
324 ap
->ioaddr
.status_addr
= ap
->ioaddr
.cmd_addr
+
325 (ATA_REG_STATUS
<< pdata
->reg_shift
);
326 ap
->ioaddr
.command_addr
= ap
->ioaddr
.cmd_addr
+
327 (ATA_REG_CMD
<< pdata
->reg_shift
);
330 * Allocate and load driver's internal data structure
332 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct pata_pxa_data
),
337 ap
->private_data
= data
;
338 data
->dma_dreq
= pdata
->dma_dreq
;
339 data
->dma_io_addr
= dma_res
->start
;
342 * Allocate space for the DMA descriptors
344 data
->dma_desc
= dmam_alloc_coherent(&pdev
->dev
, PAGE_SIZE
,
345 &data
->dma_desc_addr
, GFP_KERNEL
);
350 * Request the DMA channel
352 data
->dma_channel
= pxa_request_dma(DRV_NAME
, DMA_PRIO_LOW
,
353 pxa_ata_dma_irq
, ap
);
354 if (data
->dma_channel
< 0)
358 * Stop and clear the DMA channel
360 DCSR(data
->dma_channel
) = 0;
363 * Activate the ATA host
365 ret
= ata_host_activate(host
, irq_res
->start
, ata_sff_interrupt
,
366 pdata
->irq_flags
, &pxa_ata_sht
);
368 pxa_free_dma(data
->dma_channel
);
373 static int __devexit
pxa_ata_remove(struct platform_device
*pdev
)
375 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
376 struct pata_pxa_data
*data
= host
->ports
[0]->private_data
;
378 pxa_free_dma(data
->dma_channel
);
380 ata_host_detach(host
);
385 static struct platform_driver pxa_ata_driver
= {
386 .probe
= pxa_ata_probe
,
387 .remove
= __devexit_p(pxa_ata_remove
),
390 .owner
= THIS_MODULE
,
394 static int __init
pxa_ata_init(void)
396 return platform_driver_register(&pxa_ata_driver
);
399 static void __exit
pxa_ata_exit(void)
401 platform_driver_unregister(&pxa_ata_driver
);
404 module_init(pxa_ata_init
);
405 module_exit(pxa_ata_exit
);
407 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
408 MODULE_DESCRIPTION("DMA-capable driver for PATA on PXA CPU");
409 MODULE_LICENSE("GPL");
410 MODULE_VERSION(DRV_VERSION
);
411 MODULE_ALIAS("platform:" DRV_NAME
);