2 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * PATA driver for Samsung SoCs.
6 * Supports CF Interface in True IDE mode. Currently only PIO mode has been
7 * implemented; UDMA support has to be added.
10 * PATA driver for AT91SAM9260 Static Memory Controller
11 * PATA driver for Toshiba SCC controller
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/clk.h>
22 #include <linux/libata.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
26 #include <linux/platform_data/ata-samsung_cf.h>
27 #include <plat/regs-ata.h>
29 #define DRV_NAME "pata_samsung_cf"
30 #define DRV_VERSION "0.1"
39 * struct s3c_ide_info - S3C PATA instance.
40 * @clk: The clock resource for this controller.
41 * @ide_addr: The area mapped for the hardware registers.
42 * @sfr_addr: The area mapped for the special function registers.
43 * @irq: The IRQ number we are using.
44 * @cpu_type: The exact type of this controller.
45 * @fifo_status_reg: The ATA_FIFO_STATUS register offset.
49 void __iomem
*ide_addr
;
50 void __iomem
*sfr_addr
;
52 enum s3c_cpu_type cpu_type
;
53 unsigned int fifo_status_reg
;
56 static void pata_s3c_set_endian(void __iomem
*s3c_ide_regbase
, u8 mode
)
58 u32 reg
= readl(s3c_ide_regbase
+ S3C_ATA_CFG
);
59 reg
= mode
? (reg
& ~S3C_ATA_CFG_SWAP
) : (reg
| S3C_ATA_CFG_SWAP
);
60 writel(reg
, s3c_ide_regbase
+ S3C_ATA_CFG
);
63 static void pata_s3c_cfg_mode(void __iomem
*s3c_ide_sfrbase
)
65 /* Select true-ide as the internal operating mode */
66 writel(readl(s3c_ide_sfrbase
+ S3C_CFATA_MUX
) | S3C_CFATA_MUX_TRUEIDE
,
67 s3c_ide_sfrbase
+ S3C_CFATA_MUX
);
71 pata_s3c_setup_timing(struct s3c_ide_info
*info
, const struct ata_timing
*ata
)
78 piotime
= ((t2i
& 0xff) << 12) | ((t2
& 0xff) << 4) | (t1
& 0xf);
83 static void pata_s3c_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
85 struct s3c_ide_info
*info
= ap
->host
->private_data
;
86 struct ata_timing timing
;
88 ulong ata_cfg
= readl(info
->ide_addr
+ S3C_ATA_CFG
);
91 /* Enables IORDY if mode requires it */
92 if (ata_pio_need_iordy(adev
))
93 ata_cfg
|= S3C_ATA_CFG_IORDYEN
;
95 ata_cfg
&= ~S3C_ATA_CFG_IORDYEN
;
97 cycle_time
= (int)(1000000000UL / clk_get_rate(info
->clk
));
99 ata_timing_compute(adev
, adev
->pio_mode
, &timing
,
100 cycle_time
* 1000, 0);
102 piotime
= pata_s3c_setup_timing(info
, &timing
);
104 writel(ata_cfg
, info
->ide_addr
+ S3C_ATA_CFG
);
105 writel(piotime
, info
->ide_addr
+ S3C_ATA_PIO_TIME
);
109 * Waits until the IDE controller is able to perform next read/write
110 * operation to the disk. Needed for 64XX series boards only.
112 static int wait_for_host_ready(struct s3c_ide_info
*info
)
115 void __iomem
*fifo_reg
= info
->ide_addr
+ info
->fifo_status_reg
;
117 /* wait for maximum of 20 msec */
118 timeout
= jiffies
+ msecs_to_jiffies(20);
119 while (time_before(jiffies
, timeout
)) {
120 if ((readl(fifo_reg
) >> 28) == 0)
127 * Writes to one of the task file registers.
129 static void ata_outb(struct ata_host
*host
, u8 addr
, void __iomem
*reg
)
131 struct s3c_ide_info
*info
= host
->private_data
;
133 wait_for_host_ready(info
);
138 * Reads from one of the task file registers.
140 static u8
ata_inb(struct ata_host
*host
, void __iomem
*reg
)
142 struct s3c_ide_info
*info
= host
->private_data
;
145 wait_for_host_ready(info
);
147 wait_for_host_ready(info
);
148 temp
= readb(info
->ide_addr
+ S3C_ATA_PIO_RDATA
);
153 * pata_s3c_tf_load - send taskfile registers to host controller
155 static void pata_s3c_tf_load(struct ata_port
*ap
,
156 const struct ata_taskfile
*tf
)
158 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
159 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
161 if (tf
->ctl
!= ap
->last_ctl
) {
162 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
163 ap
->last_ctl
= tf
->ctl
;
167 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
168 ata_outb(ap
->host
, tf
->hob_feature
, ioaddr
->feature_addr
);
169 ata_outb(ap
->host
, tf
->hob_nsect
, ioaddr
->nsect_addr
);
170 ata_outb(ap
->host
, tf
->hob_lbal
, ioaddr
->lbal_addr
);
171 ata_outb(ap
->host
, tf
->hob_lbam
, ioaddr
->lbam_addr
);
172 ata_outb(ap
->host
, tf
->hob_lbah
, ioaddr
->lbah_addr
);
176 ata_outb(ap
->host
, tf
->feature
, ioaddr
->feature_addr
);
177 ata_outb(ap
->host
, tf
->nsect
, ioaddr
->nsect_addr
);
178 ata_outb(ap
->host
, tf
->lbal
, ioaddr
->lbal_addr
);
179 ata_outb(ap
->host
, tf
->lbam
, ioaddr
->lbam_addr
);
180 ata_outb(ap
->host
, tf
->lbah
, ioaddr
->lbah_addr
);
183 if (tf
->flags
& ATA_TFLAG_DEVICE
)
184 ata_outb(ap
->host
, tf
->device
, ioaddr
->device_addr
);
190 * pata_s3c_tf_read - input device's ATA taskfile shadow registers
192 static void pata_s3c_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
194 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
196 tf
->feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
197 tf
->nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
198 tf
->lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
199 tf
->lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
200 tf
->lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
201 tf
->device
= ata_inb(ap
->host
, ioaddr
->device_addr
);
203 if (tf
->flags
& ATA_TFLAG_LBA48
) {
204 ata_outb(ap
->host
, tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
205 tf
->hob_feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
206 tf
->hob_nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
207 tf
->hob_lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
208 tf
->hob_lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
209 tf
->hob_lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
210 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
211 ap
->last_ctl
= tf
->ctl
;
216 * pata_s3c_exec_command - issue ATA command to host controller
218 static void pata_s3c_exec_command(struct ata_port
*ap
,
219 const struct ata_taskfile
*tf
)
221 ata_outb(ap
->host
, tf
->command
, ap
->ioaddr
.command_addr
);
226 * pata_s3c_check_status - Read device status register
228 static u8
pata_s3c_check_status(struct ata_port
*ap
)
230 return ata_inb(ap
->host
, ap
->ioaddr
.status_addr
);
234 * pata_s3c_check_altstatus - Read alternate device status register
236 static u8
pata_s3c_check_altstatus(struct ata_port
*ap
)
238 return ata_inb(ap
->host
, ap
->ioaddr
.altstatus_addr
);
242 * pata_s3c_data_xfer - Transfer data by PIO
244 static unsigned int pata_s3c_data_xfer(struct ata_device
*dev
,
245 unsigned char *buf
, unsigned int buflen
, int rw
)
247 struct ata_port
*ap
= dev
->link
->ap
;
248 struct s3c_ide_info
*info
= ap
->host
->private_data
;
249 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
250 unsigned int words
= buflen
>> 1, i
;
251 u16
*data_ptr
= (u16
*)buf
;
253 /* Requires wait same as in ata_inb/ata_outb */
255 for (i
= 0; i
< words
; i
++, data_ptr
++) {
256 wait_for_host_ready(info
);
257 (void) readw(data_addr
);
258 wait_for_host_ready(info
);
259 *data_ptr
= readw(info
->ide_addr
260 + S3C_ATA_PIO_RDATA
);
263 for (i
= 0; i
< words
; i
++, data_ptr
++) {
264 wait_for_host_ready(info
);
265 writew(*data_ptr
, data_addr
);
269 dev_err(ap
->dev
, "unexpected trailing data\n");
275 * pata_s3c_dev_select - Select device on ATA bus
277 static void pata_s3c_dev_select(struct ata_port
*ap
, unsigned int device
)
279 u8 tmp
= ATA_DEVICE_OBS
;
284 ata_outb(ap
->host
, tmp
, ap
->ioaddr
.device_addr
);
289 * pata_s3c_devchk - PATA device presence detection
291 static unsigned int pata_s3c_devchk(struct ata_port
*ap
,
294 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
297 pata_s3c_dev_select(ap
, device
);
299 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
300 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
302 ata_outb(ap
->host
, 0xaa, ioaddr
->nsect_addr
);
303 ata_outb(ap
->host
, 0x55, ioaddr
->lbal_addr
);
305 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
306 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
308 nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
309 lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
311 if ((nsect
== 0x55) && (lbal
== 0xaa))
312 return 1; /* we found a device */
314 return 0; /* nothing found */
318 * pata_s3c_wait_after_reset - wait for devices to become ready after reset
320 static int pata_s3c_wait_after_reset(struct ata_link
*link
,
321 unsigned long deadline
)
325 ata_msleep(link
->ap
, ATA_WAIT_AFTER_RESET
);
327 /* always check readiness of the master device */
328 rc
= ata_sff_wait_ready(link
, deadline
);
329 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
330 * and TF status is 0xff, bail out on it too.
339 * pata_s3c_bus_softreset - PATA device software reset
341 static unsigned int pata_s3c_bus_softreset(struct ata_port
*ap
,
342 unsigned long deadline
)
344 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
346 /* software reset. causes dev0 to be selected */
347 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
349 ata_outb(ap
->host
, ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
351 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
352 ap
->last_ctl
= ap
->ctl
;
354 return pata_s3c_wait_after_reset(&ap
->link
, deadline
);
358 * pata_s3c_softreset - reset host port via ATA SRST
360 static int pata_s3c_softreset(struct ata_link
*link
, unsigned int *classes
,
361 unsigned long deadline
)
363 struct ata_port
*ap
= link
->ap
;
364 unsigned int devmask
= 0;
368 /* determine if device 0 is present */
369 if (pata_s3c_devchk(ap
, 0))
372 /* select device 0 again */
373 pata_s3c_dev_select(ap
, 0);
375 /* issue bus reset */
376 rc
= pata_s3c_bus_softreset(ap
, deadline
);
377 /* if link is occupied, -ENODEV too is an error */
378 if (rc
&& rc
!= -ENODEV
) {
379 ata_link_err(link
, "SRST failed (errno=%d)\n", rc
);
383 /* determine by signature whether we have ATA or ATAPI devices */
384 classes
[0] = ata_sff_dev_classify(&ap
->link
.device
[0],
385 devmask
& (1 << 0), &err
);
391 * pata_s3c_set_devctl - Write device control register
393 static void pata_s3c_set_devctl(struct ata_port
*ap
, u8 ctl
)
395 ata_outb(ap
->host
, ctl
, ap
->ioaddr
.ctl_addr
);
398 static struct scsi_host_template pata_s3c_sht
= {
399 ATA_PIO_SHT(DRV_NAME
),
402 static struct ata_port_operations pata_s3c_port_ops
= {
403 .inherits
= &ata_sff_port_ops
,
404 .sff_check_status
= pata_s3c_check_status
,
405 .sff_check_altstatus
= pata_s3c_check_altstatus
,
406 .sff_tf_load
= pata_s3c_tf_load
,
407 .sff_tf_read
= pata_s3c_tf_read
,
408 .sff_data_xfer
= pata_s3c_data_xfer
,
409 .sff_exec_command
= pata_s3c_exec_command
,
410 .sff_dev_select
= pata_s3c_dev_select
,
411 .sff_set_devctl
= pata_s3c_set_devctl
,
412 .softreset
= pata_s3c_softreset
,
413 .set_piomode
= pata_s3c_set_piomode
,
416 static struct ata_port_operations pata_s5p_port_ops
= {
417 .inherits
= &ata_sff_port_ops
,
418 .set_piomode
= pata_s3c_set_piomode
,
421 static void pata_s3c_enable(void __iomem
*s3c_ide_regbase
, bool state
)
423 u32 temp
= readl(s3c_ide_regbase
+ S3C_ATA_CTRL
);
424 temp
= state
? (temp
| 1) : (temp
& ~1);
425 writel(temp
, s3c_ide_regbase
+ S3C_ATA_CTRL
);
428 static irqreturn_t
pata_s3c_irq(int irq
, void *dev_instance
)
430 struct ata_host
*host
= dev_instance
;
431 struct s3c_ide_info
*info
= host
->private_data
;
434 reg
= readl(info
->ide_addr
+ S3C_ATA_IRQ
);
435 writel(reg
, info
->ide_addr
+ S3C_ATA_IRQ
);
437 return ata_sff_interrupt(irq
, dev_instance
);
440 static void pata_s3c_hwinit(struct s3c_ide_info
*info
,
441 struct s3c_ide_platdata
*pdata
)
443 switch (info
->cpu_type
) {
445 /* Configure as big endian */
446 pata_s3c_cfg_mode(info
->sfr_addr
);
447 pata_s3c_set_endian(info
->ide_addr
, 1);
448 pata_s3c_enable(info
->ide_addr
, true);
451 /* Remove IRQ Status */
452 writel(0x1f, info
->ide_addr
+ S3C_ATA_IRQ
);
453 writel(0x1b, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
457 pata_s3c_cfg_mode(info
->sfr_addr
);
461 /* Configure as little endian */
462 pata_s3c_set_endian(info
->ide_addr
, 0);
463 pata_s3c_enable(info
->ide_addr
, true);
466 /* Remove IRQ Status */
467 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ
);
468 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
476 static int __init
pata_s3c_probe(struct platform_device
*pdev
)
478 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
479 struct device
*dev
= &pdev
->dev
;
480 struct s3c_ide_info
*info
;
481 struct resource
*res
;
483 struct ata_host
*host
;
484 enum s3c_cpu_type cpu_type
;
487 cpu_type
= platform_get_device_id(pdev
)->driver_data
;
489 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
491 dev_err(dev
, "failed to allocate memory for device data\n");
495 info
->irq
= platform_get_irq(pdev
, 0);
497 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
499 dev_err(dev
, "failed to get mem resource\n");
503 if (!devm_request_mem_region(dev
, res
->start
,
504 resource_size(res
), DRV_NAME
)) {
505 dev_err(dev
, "error requesting register region\n");
509 info
->ide_addr
= devm_ioremap(dev
, res
->start
, resource_size(res
));
510 if (!info
->ide_addr
) {
511 dev_err(dev
, "failed to map IO base address\n");
515 info
->clk
= devm_clk_get(&pdev
->dev
, "cfcon");
516 if (IS_ERR(info
->clk
)) {
517 dev_err(dev
, "failed to get access to cf controller clock\n");
518 ret
= PTR_ERR(info
->clk
);
523 clk_enable(info
->clk
);
526 host
= ata_host_alloc(dev
, 1);
528 dev_err(dev
, "failed to allocate ide host\n");
534 ap
->pio_mask
= ATA_PIO4
;
536 if (cpu_type
== TYPE_S3C64XX
) {
537 ap
->ops
= &pata_s3c_port_ops
;
538 info
->sfr_addr
= info
->ide_addr
+ 0x1800;
539 info
->ide_addr
+= 0x1900;
540 info
->fifo_status_reg
= 0x94;
541 } else if (cpu_type
== TYPE_S5PC100
) {
542 ap
->ops
= &pata_s5p_port_ops
;
543 info
->sfr_addr
= info
->ide_addr
+ 0x1800;
544 info
->ide_addr
+= 0x1900;
545 info
->fifo_status_reg
= 0x84;
547 ap
->ops
= &pata_s5p_port_ops
;
548 info
->fifo_status_reg
= 0x84;
551 info
->cpu_type
= cpu_type
;
553 if (info
->irq
<= 0) {
554 ap
->flags
|= ATA_FLAG_PIO_POLLING
;
556 ata_port_desc(ap
, "no IRQ, using PIO polling\n");
559 ap
->ioaddr
.cmd_addr
= info
->ide_addr
+ S3C_ATA_CMD
;
560 ap
->ioaddr
.data_addr
= info
->ide_addr
+ S3C_ATA_PIO_DTR
;
561 ap
->ioaddr
.error_addr
= info
->ide_addr
+ S3C_ATA_PIO_FED
;
562 ap
->ioaddr
.feature_addr
= info
->ide_addr
+ S3C_ATA_PIO_FED
;
563 ap
->ioaddr
.nsect_addr
= info
->ide_addr
+ S3C_ATA_PIO_SCR
;
564 ap
->ioaddr
.lbal_addr
= info
->ide_addr
+ S3C_ATA_PIO_LLR
;
565 ap
->ioaddr
.lbam_addr
= info
->ide_addr
+ S3C_ATA_PIO_LMR
;
566 ap
->ioaddr
.lbah_addr
= info
->ide_addr
+ S3C_ATA_PIO_LHR
;
567 ap
->ioaddr
.device_addr
= info
->ide_addr
+ S3C_ATA_PIO_DVR
;
568 ap
->ioaddr
.status_addr
= info
->ide_addr
+ S3C_ATA_PIO_CSD
;
569 ap
->ioaddr
.command_addr
= info
->ide_addr
+ S3C_ATA_PIO_CSD
;
570 ap
->ioaddr
.altstatus_addr
= info
->ide_addr
+ S3C_ATA_PIO_DAD
;
571 ap
->ioaddr
.ctl_addr
= info
->ide_addr
+ S3C_ATA_PIO_DAD
;
573 ata_port_desc(ap
, "mmio cmd 0x%llx ",
574 (unsigned long long)res
->start
);
576 host
->private_data
= info
;
578 if (pdata
&& pdata
->setup_gpio
)
581 /* Set endianness and enable the interface */
582 pata_s3c_hwinit(info
, pdata
);
584 platform_set_drvdata(pdev
, host
);
586 return ata_host_activate(host
, info
->irq
,
587 info
->irq
? pata_s3c_irq
: NULL
,
591 clk_disable(info
->clk
);
595 static int __exit
pata_s3c_remove(struct platform_device
*pdev
)
597 struct ata_host
*host
= platform_get_drvdata(pdev
);
598 struct s3c_ide_info
*info
= host
->private_data
;
600 ata_host_detach(host
);
602 clk_disable(info
->clk
);
608 static int pata_s3c_suspend(struct device
*dev
)
610 struct platform_device
*pdev
= to_platform_device(dev
);
611 struct ata_host
*host
= platform_get_drvdata(pdev
);
613 return ata_host_suspend(host
, PMSG_SUSPEND
);
616 static int pata_s3c_resume(struct device
*dev
)
618 struct platform_device
*pdev
= to_platform_device(dev
);
619 struct ata_host
*host
= platform_get_drvdata(pdev
);
620 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
621 struct s3c_ide_info
*info
= host
->private_data
;
623 pata_s3c_hwinit(info
, pdata
);
624 ata_host_resume(host
);
629 static const struct dev_pm_ops pata_s3c_pm_ops
= {
630 .suspend
= pata_s3c_suspend
,
631 .resume
= pata_s3c_resume
,
635 /* driver device registration */
636 static struct platform_device_id pata_s3c_driver_ids
[] = {
638 .name
= "s3c64xx-pata",
639 .driver_data
= TYPE_S3C64XX
,
641 .name
= "s5pc100-pata",
642 .driver_data
= TYPE_S5PC100
,
644 .name
= "s5pv210-pata",
645 .driver_data
= TYPE_S5PV210
,
650 MODULE_DEVICE_TABLE(platform
, pata_s3c_driver_ids
);
652 static struct platform_driver pata_s3c_driver
= {
653 .remove
= __exit_p(pata_s3c_remove
),
654 .id_table
= pata_s3c_driver_ids
,
657 .owner
= THIS_MODULE
,
659 .pm
= &pata_s3c_pm_ops
,
664 module_platform_driver_probe(pata_s3c_driver
, pata_s3c_probe
);
666 MODULE_AUTHOR("Abhilash Kesavan, <a.kesavan@samsung.com>");
667 MODULE_DESCRIPTION("low-level driver for Samsung PATA controller");
668 MODULE_LICENSE("GPL");
669 MODULE_VERSION(DRV_VERSION
);