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>
28 #define DRV_NAME "pata_samsung_cf"
29 #define DRV_VERSION "0.1"
31 #define S3C_CFATA_REG(x) (x)
32 #define S3C_CFATA_MUX S3C_CFATA_REG(0x0)
33 #define S3C_ATA_CTRL S3C_CFATA_REG(0x0)
34 #define S3C_ATA_CMD S3C_CFATA_REG(0x8)
35 #define S3C_ATA_IRQ S3C_CFATA_REG(0x10)
36 #define S3C_ATA_IRQ_MSK S3C_CFATA_REG(0x14)
37 #define S3C_ATA_CFG S3C_CFATA_REG(0x18)
39 #define S3C_ATA_PIO_TIME S3C_CFATA_REG(0x2c)
40 #define S3C_ATA_PIO_DTR S3C_CFATA_REG(0x54)
41 #define S3C_ATA_PIO_FED S3C_CFATA_REG(0x58)
42 #define S3C_ATA_PIO_SCR S3C_CFATA_REG(0x5c)
43 #define S3C_ATA_PIO_LLR S3C_CFATA_REG(0x60)
44 #define S3C_ATA_PIO_LMR S3C_CFATA_REG(0x64)
45 #define S3C_ATA_PIO_LHR S3C_CFATA_REG(0x68)
46 #define S3C_ATA_PIO_DVR S3C_CFATA_REG(0x6c)
47 #define S3C_ATA_PIO_CSD S3C_CFATA_REG(0x70)
48 #define S3C_ATA_PIO_DAD S3C_CFATA_REG(0x74)
49 #define S3C_ATA_PIO_RDATA S3C_CFATA_REG(0x7c)
51 #define S3C_CFATA_MUX_TRUEIDE 0x01
52 #define S3C_ATA_CFG_SWAP 0x40
53 #define S3C_ATA_CFG_IORDYEN 0x02
61 * struct s3c_ide_info - S3C PATA instance.
62 * @clk: The clock resource for this controller.
63 * @ide_addr: The area mapped for the hardware registers.
64 * @sfr_addr: The area mapped for the special function registers.
65 * @irq: The IRQ number we are using.
66 * @cpu_type: The exact type of this controller.
67 * @fifo_status_reg: The ATA_FIFO_STATUS register offset.
71 void __iomem
*ide_addr
;
72 void __iomem
*sfr_addr
;
74 enum s3c_cpu_type cpu_type
;
75 unsigned int fifo_status_reg
;
78 static void pata_s3c_set_endian(void __iomem
*s3c_ide_regbase
, u8 mode
)
80 u32 reg
= readl(s3c_ide_regbase
+ S3C_ATA_CFG
);
81 reg
= mode
? (reg
& ~S3C_ATA_CFG_SWAP
) : (reg
| S3C_ATA_CFG_SWAP
);
82 writel(reg
, s3c_ide_regbase
+ S3C_ATA_CFG
);
85 static void pata_s3c_cfg_mode(void __iomem
*s3c_ide_sfrbase
)
87 /* Select true-ide as the internal operating mode */
88 writel(readl(s3c_ide_sfrbase
+ S3C_CFATA_MUX
) | S3C_CFATA_MUX_TRUEIDE
,
89 s3c_ide_sfrbase
+ S3C_CFATA_MUX
);
93 pata_s3c_setup_timing(struct s3c_ide_info
*info
, const struct ata_timing
*ata
)
100 piotime
= ((t2i
& 0xff) << 12) | ((t2
& 0xff) << 4) | (t1
& 0xf);
105 static void pata_s3c_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
107 struct s3c_ide_info
*info
= ap
->host
->private_data
;
108 struct ata_timing timing
;
110 ulong ata_cfg
= readl(info
->ide_addr
+ S3C_ATA_CFG
);
113 /* Enables IORDY if mode requires it */
114 if (ata_pio_need_iordy(adev
))
115 ata_cfg
|= S3C_ATA_CFG_IORDYEN
;
117 ata_cfg
&= ~S3C_ATA_CFG_IORDYEN
;
119 cycle_time
= (int)(1000000000UL / clk_get_rate(info
->clk
));
121 ata_timing_compute(adev
, adev
->pio_mode
, &timing
,
122 cycle_time
* 1000, 0);
124 piotime
= pata_s3c_setup_timing(info
, &timing
);
126 writel(ata_cfg
, info
->ide_addr
+ S3C_ATA_CFG
);
127 writel(piotime
, info
->ide_addr
+ S3C_ATA_PIO_TIME
);
131 * Waits until the IDE controller is able to perform next read/write
132 * operation to the disk. Needed for 64XX series boards only.
134 static int wait_for_host_ready(struct s3c_ide_info
*info
)
137 void __iomem
*fifo_reg
= info
->ide_addr
+ info
->fifo_status_reg
;
139 /* wait for maximum of 20 msec */
140 timeout
= jiffies
+ msecs_to_jiffies(20);
141 while (time_before(jiffies
, timeout
)) {
142 if ((readl(fifo_reg
) >> 28) == 0)
149 * Writes to one of the task file registers.
151 static void ata_outb(struct ata_host
*host
, u8 addr
, void __iomem
*reg
)
153 struct s3c_ide_info
*info
= host
->private_data
;
155 wait_for_host_ready(info
);
160 * Reads from one of the task file registers.
162 static u8
ata_inb(struct ata_host
*host
, void __iomem
*reg
)
164 struct s3c_ide_info
*info
= host
->private_data
;
167 wait_for_host_ready(info
);
169 wait_for_host_ready(info
);
170 temp
= readb(info
->ide_addr
+ S3C_ATA_PIO_RDATA
);
175 * pata_s3c_tf_load - send taskfile registers to host controller
177 static void pata_s3c_tf_load(struct ata_port
*ap
,
178 const struct ata_taskfile
*tf
)
180 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
181 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
183 if (tf
->ctl
!= ap
->last_ctl
) {
184 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
185 ap
->last_ctl
= tf
->ctl
;
189 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
190 ata_outb(ap
->host
, tf
->hob_feature
, ioaddr
->feature_addr
);
191 ata_outb(ap
->host
, tf
->hob_nsect
, ioaddr
->nsect_addr
);
192 ata_outb(ap
->host
, tf
->hob_lbal
, ioaddr
->lbal_addr
);
193 ata_outb(ap
->host
, tf
->hob_lbam
, ioaddr
->lbam_addr
);
194 ata_outb(ap
->host
, tf
->hob_lbah
, ioaddr
->lbah_addr
);
198 ata_outb(ap
->host
, tf
->feature
, ioaddr
->feature_addr
);
199 ata_outb(ap
->host
, tf
->nsect
, ioaddr
->nsect_addr
);
200 ata_outb(ap
->host
, tf
->lbal
, ioaddr
->lbal_addr
);
201 ata_outb(ap
->host
, tf
->lbam
, ioaddr
->lbam_addr
);
202 ata_outb(ap
->host
, tf
->lbah
, ioaddr
->lbah_addr
);
205 if (tf
->flags
& ATA_TFLAG_DEVICE
)
206 ata_outb(ap
->host
, tf
->device
, ioaddr
->device_addr
);
212 * pata_s3c_tf_read - input device's ATA taskfile shadow registers
214 static void pata_s3c_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
216 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
218 tf
->feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
219 tf
->nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
220 tf
->lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
221 tf
->lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
222 tf
->lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
223 tf
->device
= ata_inb(ap
->host
, ioaddr
->device_addr
);
225 if (tf
->flags
& ATA_TFLAG_LBA48
) {
226 ata_outb(ap
->host
, tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
227 tf
->hob_feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
228 tf
->hob_nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
229 tf
->hob_lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
230 tf
->hob_lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
231 tf
->hob_lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
232 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
233 ap
->last_ctl
= tf
->ctl
;
238 * pata_s3c_exec_command - issue ATA command to host controller
240 static void pata_s3c_exec_command(struct ata_port
*ap
,
241 const struct ata_taskfile
*tf
)
243 ata_outb(ap
->host
, tf
->command
, ap
->ioaddr
.command_addr
);
248 * pata_s3c_check_status - Read device status register
250 static u8
pata_s3c_check_status(struct ata_port
*ap
)
252 return ata_inb(ap
->host
, ap
->ioaddr
.status_addr
);
256 * pata_s3c_check_altstatus - Read alternate device status register
258 static u8
pata_s3c_check_altstatus(struct ata_port
*ap
)
260 return ata_inb(ap
->host
, ap
->ioaddr
.altstatus_addr
);
264 * pata_s3c_data_xfer - Transfer data by PIO
266 static unsigned int pata_s3c_data_xfer(struct ata_queued_cmd
*qc
,
267 unsigned char *buf
, unsigned int buflen
, int rw
)
269 struct ata_port
*ap
= qc
->dev
->link
->ap
;
270 struct s3c_ide_info
*info
= ap
->host
->private_data
;
271 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
272 unsigned int words
= buflen
>> 1, i
;
273 u16
*data_ptr
= (u16
*)buf
;
275 /* Requires wait same as in ata_inb/ata_outb */
277 for (i
= 0; i
< words
; i
++, data_ptr
++) {
278 wait_for_host_ready(info
);
279 (void) readw(data_addr
);
280 wait_for_host_ready(info
);
281 *data_ptr
= readw(info
->ide_addr
282 + S3C_ATA_PIO_RDATA
);
285 for (i
= 0; i
< words
; i
++, data_ptr
++) {
286 wait_for_host_ready(info
);
287 writew(*data_ptr
, data_addr
);
291 dev_err(ap
->dev
, "unexpected trailing data\n");
297 * pata_s3c_dev_select - Select device on ATA bus
299 static void pata_s3c_dev_select(struct ata_port
*ap
, unsigned int device
)
301 u8 tmp
= ATA_DEVICE_OBS
;
306 ata_outb(ap
->host
, tmp
, ap
->ioaddr
.device_addr
);
311 * pata_s3c_devchk - PATA device presence detection
313 static unsigned int pata_s3c_devchk(struct ata_port
*ap
,
316 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
319 pata_s3c_dev_select(ap
, device
);
321 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
322 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
324 ata_outb(ap
->host
, 0xaa, ioaddr
->nsect_addr
);
325 ata_outb(ap
->host
, 0x55, ioaddr
->lbal_addr
);
327 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
328 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
330 nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
331 lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
333 if ((nsect
== 0x55) && (lbal
== 0xaa))
334 return 1; /* we found a device */
336 return 0; /* nothing found */
340 * pata_s3c_wait_after_reset - wait for devices to become ready after reset
342 static int pata_s3c_wait_after_reset(struct ata_link
*link
,
343 unsigned long deadline
)
347 ata_msleep(link
->ap
, ATA_WAIT_AFTER_RESET
);
349 /* always check readiness of the master device */
350 rc
= ata_sff_wait_ready(link
, deadline
);
351 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
352 * and TF status is 0xff, bail out on it too.
361 * pata_s3c_bus_softreset - PATA device software reset
363 static int pata_s3c_bus_softreset(struct ata_port
*ap
,
364 unsigned long deadline
)
366 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
368 /* software reset. causes dev0 to be selected */
369 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
371 ata_outb(ap
->host
, ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
373 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
374 ap
->last_ctl
= ap
->ctl
;
376 return pata_s3c_wait_after_reset(&ap
->link
, deadline
);
380 * pata_s3c_softreset - reset host port via ATA SRST
382 static int pata_s3c_softreset(struct ata_link
*link
, unsigned int *classes
,
383 unsigned long deadline
)
385 struct ata_port
*ap
= link
->ap
;
386 unsigned int devmask
= 0;
390 /* determine if device 0 is present */
391 if (pata_s3c_devchk(ap
, 0))
394 /* select device 0 again */
395 pata_s3c_dev_select(ap
, 0);
397 /* issue bus reset */
398 rc
= pata_s3c_bus_softreset(ap
, deadline
);
399 /* if link is occupied, -ENODEV too is an error */
400 if (rc
&& rc
!= -ENODEV
) {
401 ata_link_err(link
, "SRST failed (errno=%d)\n", rc
);
405 /* determine by signature whether we have ATA or ATAPI devices */
406 classes
[0] = ata_sff_dev_classify(&ap
->link
.device
[0],
407 devmask
& (1 << 0), &err
);
413 * pata_s3c_set_devctl - Write device control register
415 static void pata_s3c_set_devctl(struct ata_port
*ap
, u8 ctl
)
417 ata_outb(ap
->host
, ctl
, ap
->ioaddr
.ctl_addr
);
420 static struct scsi_host_template pata_s3c_sht
= {
421 ATA_PIO_SHT(DRV_NAME
),
424 static struct ata_port_operations pata_s3c_port_ops
= {
425 .inherits
= &ata_sff_port_ops
,
426 .sff_check_status
= pata_s3c_check_status
,
427 .sff_check_altstatus
= pata_s3c_check_altstatus
,
428 .sff_tf_load
= pata_s3c_tf_load
,
429 .sff_tf_read
= pata_s3c_tf_read
,
430 .sff_data_xfer
= pata_s3c_data_xfer
,
431 .sff_exec_command
= pata_s3c_exec_command
,
432 .sff_dev_select
= pata_s3c_dev_select
,
433 .sff_set_devctl
= pata_s3c_set_devctl
,
434 .softreset
= pata_s3c_softreset
,
435 .set_piomode
= pata_s3c_set_piomode
,
438 static struct ata_port_operations pata_s5p_port_ops
= {
439 .inherits
= &ata_sff_port_ops
,
440 .set_piomode
= pata_s3c_set_piomode
,
443 static void pata_s3c_enable(void __iomem
*s3c_ide_regbase
, bool state
)
445 u32 temp
= readl(s3c_ide_regbase
+ S3C_ATA_CTRL
);
446 temp
= state
? (temp
| 1) : (temp
& ~1);
447 writel(temp
, s3c_ide_regbase
+ S3C_ATA_CTRL
);
450 static irqreturn_t
pata_s3c_irq(int irq
, void *dev_instance
)
452 struct ata_host
*host
= dev_instance
;
453 struct s3c_ide_info
*info
= host
->private_data
;
456 reg
= readl(info
->ide_addr
+ S3C_ATA_IRQ
);
457 writel(reg
, info
->ide_addr
+ S3C_ATA_IRQ
);
459 return ata_sff_interrupt(irq
, dev_instance
);
462 static void pata_s3c_hwinit(struct s3c_ide_info
*info
,
463 struct s3c_ide_platdata
*pdata
)
465 switch (info
->cpu_type
) {
467 /* Configure as big endian */
468 pata_s3c_cfg_mode(info
->sfr_addr
);
469 pata_s3c_set_endian(info
->ide_addr
, 1);
470 pata_s3c_enable(info
->ide_addr
, true);
473 /* Remove IRQ Status */
474 writel(0x1f, info
->ide_addr
+ S3C_ATA_IRQ
);
475 writel(0x1b, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
479 /* Configure as little endian */
480 pata_s3c_set_endian(info
->ide_addr
, 0);
481 pata_s3c_enable(info
->ide_addr
, true);
484 /* Remove IRQ Status */
485 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ
);
486 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
494 static int __init
pata_s3c_probe(struct platform_device
*pdev
)
496 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
497 struct device
*dev
= &pdev
->dev
;
498 struct s3c_ide_info
*info
;
499 struct resource
*res
;
501 struct ata_host
*host
;
502 enum s3c_cpu_type cpu_type
;
505 cpu_type
= platform_get_device_id(pdev
)->driver_data
;
507 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
509 dev_err(dev
, "failed to allocate memory for device data\n");
513 info
->irq
= platform_get_irq(pdev
, 0);
515 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
517 info
->ide_addr
= devm_ioremap_resource(dev
, res
);
518 if (IS_ERR(info
->ide_addr
))
519 return PTR_ERR(info
->ide_addr
);
521 info
->clk
= devm_clk_get(&pdev
->dev
, "cfcon");
522 if (IS_ERR(info
->clk
)) {
523 dev_err(dev
, "failed to get access to cf controller clock\n");
524 ret
= PTR_ERR(info
->clk
);
529 clk_enable(info
->clk
);
532 host
= ata_host_alloc(dev
, 1);
534 dev_err(dev
, "failed to allocate ide host\n");
540 ap
->pio_mask
= ATA_PIO4
;
542 if (cpu_type
== TYPE_S3C64XX
) {
543 ap
->ops
= &pata_s3c_port_ops
;
544 info
->sfr_addr
= info
->ide_addr
+ 0x1800;
545 info
->ide_addr
+= 0x1900;
546 info
->fifo_status_reg
= 0x94;
548 ap
->ops
= &pata_s5p_port_ops
;
549 info
->fifo_status_reg
= 0x84;
552 info
->cpu_type
= cpu_type
;
554 if (info
->irq
<= 0) {
555 ap
->flags
|= ATA_FLAG_PIO_POLLING
;
557 ata_port_desc(ap
, "no IRQ, using PIO polling\n");
560 ap
->ioaddr
.cmd_addr
= info
->ide_addr
+ S3C_ATA_CMD
;
561 ap
->ioaddr
.data_addr
= info
->ide_addr
+ S3C_ATA_PIO_DTR
;
562 ap
->ioaddr
.error_addr
= info
->ide_addr
+ S3C_ATA_PIO_FED
;
563 ap
->ioaddr
.feature_addr
= info
->ide_addr
+ S3C_ATA_PIO_FED
;
564 ap
->ioaddr
.nsect_addr
= info
->ide_addr
+ S3C_ATA_PIO_SCR
;
565 ap
->ioaddr
.lbal_addr
= info
->ide_addr
+ S3C_ATA_PIO_LLR
;
566 ap
->ioaddr
.lbam_addr
= info
->ide_addr
+ S3C_ATA_PIO_LMR
;
567 ap
->ioaddr
.lbah_addr
= info
->ide_addr
+ S3C_ATA_PIO_LHR
;
568 ap
->ioaddr
.device_addr
= info
->ide_addr
+ S3C_ATA_PIO_DVR
;
569 ap
->ioaddr
.status_addr
= info
->ide_addr
+ S3C_ATA_PIO_CSD
;
570 ap
->ioaddr
.command_addr
= info
->ide_addr
+ S3C_ATA_PIO_CSD
;
571 ap
->ioaddr
.altstatus_addr
= info
->ide_addr
+ S3C_ATA_PIO_DAD
;
572 ap
->ioaddr
.ctl_addr
= info
->ide_addr
+ S3C_ATA_PIO_DAD
;
574 ata_port_desc(ap
, "mmio cmd 0x%llx ",
575 (unsigned long long)res
->start
);
577 host
->private_data
= info
;
579 if (pdata
&& pdata
->setup_gpio
)
582 /* Set endianness and enable the interface */
583 pata_s3c_hwinit(info
, pdata
);
585 ret
= ata_host_activate(host
, info
->irq
,
586 info
->irq
? pata_s3c_irq
: NULL
,
594 clk_disable(info
->clk
);
598 static int __exit
pata_s3c_remove(struct platform_device
*pdev
)
600 struct ata_host
*host
= platform_get_drvdata(pdev
);
601 struct s3c_ide_info
*info
= host
->private_data
;
603 ata_host_detach(host
);
605 clk_disable(info
->clk
);
610 #ifdef CONFIG_PM_SLEEP
611 static int pata_s3c_suspend(struct device
*dev
)
613 struct platform_device
*pdev
= to_platform_device(dev
);
614 struct ata_host
*host
= platform_get_drvdata(pdev
);
616 return ata_host_suspend(host
, PMSG_SUSPEND
);
619 static int pata_s3c_resume(struct device
*dev
)
621 struct platform_device
*pdev
= to_platform_device(dev
);
622 struct ata_host
*host
= platform_get_drvdata(pdev
);
623 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
624 struct s3c_ide_info
*info
= host
->private_data
;
626 pata_s3c_hwinit(info
, pdata
);
627 ata_host_resume(host
);
632 static const struct dev_pm_ops pata_s3c_pm_ops
= {
633 .suspend
= pata_s3c_suspend
,
634 .resume
= pata_s3c_resume
,
638 /* driver device registration */
639 static const struct platform_device_id pata_s3c_driver_ids
[] = {
641 .name
= "s3c64xx-pata",
642 .driver_data
= TYPE_S3C64XX
,
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 #ifdef CONFIG_PM_SLEEP
658 .pm
= &pata_s3c_pm_ops
,
663 module_platform_driver_probe(pata_s3c_driver
, pata_s3c_probe
);
665 MODULE_AUTHOR("Abhilash Kesavan, <a.kesavan@samsung.com>");
666 MODULE_DESCRIPTION("low-level driver for Samsung PATA controller");
667 MODULE_LICENSE("GPL");
668 MODULE_VERSION(DRV_VERSION
);