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/mod_devicetable.h>
21 #include <linux/init.h>
22 #include <linux/clk.h>
23 #include <linux/libata.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include <linux/platform_data/ata-samsung_cf.h>
29 #define DRV_NAME "pata_samsung_cf"
30 #define DRV_VERSION "0.1"
32 #define S3C_CFATA_REG(x) (x)
33 #define S3C_CFATA_MUX S3C_CFATA_REG(0x0)
34 #define S3C_ATA_CTRL S3C_CFATA_REG(0x0)
35 #define S3C_ATA_CMD S3C_CFATA_REG(0x8)
36 #define S3C_ATA_IRQ S3C_CFATA_REG(0x10)
37 #define S3C_ATA_IRQ_MSK S3C_CFATA_REG(0x14)
38 #define S3C_ATA_CFG S3C_CFATA_REG(0x18)
40 #define S3C_ATA_PIO_TIME S3C_CFATA_REG(0x2c)
41 #define S3C_ATA_PIO_DTR S3C_CFATA_REG(0x54)
42 #define S3C_ATA_PIO_FED S3C_CFATA_REG(0x58)
43 #define S3C_ATA_PIO_SCR S3C_CFATA_REG(0x5c)
44 #define S3C_ATA_PIO_LLR S3C_CFATA_REG(0x60)
45 #define S3C_ATA_PIO_LMR S3C_CFATA_REG(0x64)
46 #define S3C_ATA_PIO_LHR S3C_CFATA_REG(0x68)
47 #define S3C_ATA_PIO_DVR S3C_CFATA_REG(0x6c)
48 #define S3C_ATA_PIO_CSD S3C_CFATA_REG(0x70)
49 #define S3C_ATA_PIO_DAD S3C_CFATA_REG(0x74)
50 #define S3C_ATA_PIO_RDATA S3C_CFATA_REG(0x7c)
52 #define S3C_CFATA_MUX_TRUEIDE 0x01
53 #define S3C_ATA_CFG_SWAP 0x40
54 #define S3C_ATA_CFG_IORDYEN 0x02
62 * struct s3c_ide_info - S3C PATA instance.
63 * @clk: The clock resource for this controller.
64 * @ide_addr: The area mapped for the hardware registers.
65 * @sfr_addr: The area mapped for the special function registers.
66 * @irq: The IRQ number we are using.
67 * @cpu_type: The exact type of this controller.
68 * @fifo_status_reg: The ATA_FIFO_STATUS register offset.
72 void __iomem
*ide_addr
;
73 void __iomem
*sfr_addr
;
75 enum s3c_cpu_type cpu_type
;
76 unsigned int fifo_status_reg
;
79 static void pata_s3c_set_endian(void __iomem
*s3c_ide_regbase
, u8 mode
)
81 u32 reg
= readl(s3c_ide_regbase
+ S3C_ATA_CFG
);
82 reg
= mode
? (reg
& ~S3C_ATA_CFG_SWAP
) : (reg
| S3C_ATA_CFG_SWAP
);
83 writel(reg
, s3c_ide_regbase
+ S3C_ATA_CFG
);
86 static void pata_s3c_cfg_mode(void __iomem
*s3c_ide_sfrbase
)
88 /* Select true-ide as the internal operating mode */
89 writel(readl(s3c_ide_sfrbase
+ S3C_CFATA_MUX
) | S3C_CFATA_MUX_TRUEIDE
,
90 s3c_ide_sfrbase
+ S3C_CFATA_MUX
);
94 pata_s3c_setup_timing(struct s3c_ide_info
*info
, const struct ata_timing
*ata
)
101 piotime
= ((t2i
& 0xff) << 12) | ((t2
& 0xff) << 4) | (t1
& 0xf);
106 static void pata_s3c_set_piomode(struct ata_port
*ap
, struct ata_device
*adev
)
108 struct s3c_ide_info
*info
= ap
->host
->private_data
;
109 struct ata_timing timing
;
111 ulong ata_cfg
= readl(info
->ide_addr
+ S3C_ATA_CFG
);
114 /* Enables IORDY if mode requires it */
115 if (ata_pio_need_iordy(adev
))
116 ata_cfg
|= S3C_ATA_CFG_IORDYEN
;
118 ata_cfg
&= ~S3C_ATA_CFG_IORDYEN
;
120 cycle_time
= (int)(1000000000UL / clk_get_rate(info
->clk
));
122 ata_timing_compute(adev
, adev
->pio_mode
, &timing
,
123 cycle_time
* 1000, 0);
125 piotime
= pata_s3c_setup_timing(info
, &timing
);
127 writel(ata_cfg
, info
->ide_addr
+ S3C_ATA_CFG
);
128 writel(piotime
, info
->ide_addr
+ S3C_ATA_PIO_TIME
);
132 * Waits until the IDE controller is able to perform next read/write
133 * operation to the disk. Needed for 64XX series boards only.
135 static int wait_for_host_ready(struct s3c_ide_info
*info
)
138 void __iomem
*fifo_reg
= info
->ide_addr
+ info
->fifo_status_reg
;
140 /* wait for maximum of 20 msec */
141 timeout
= jiffies
+ msecs_to_jiffies(20);
142 while (time_before(jiffies
, timeout
)) {
143 if ((readl(fifo_reg
) >> 28) == 0)
150 * Writes to one of the task file registers.
152 static void ata_outb(struct ata_host
*host
, u8 addr
, void __iomem
*reg
)
154 struct s3c_ide_info
*info
= host
->private_data
;
156 wait_for_host_ready(info
);
161 * Reads from one of the task file registers.
163 static u8
ata_inb(struct ata_host
*host
, void __iomem
*reg
)
165 struct s3c_ide_info
*info
= host
->private_data
;
168 wait_for_host_ready(info
);
170 wait_for_host_ready(info
);
171 temp
= readb(info
->ide_addr
+ S3C_ATA_PIO_RDATA
);
176 * pata_s3c_tf_load - send taskfile registers to host controller
178 static void pata_s3c_tf_load(struct ata_port
*ap
,
179 const struct ata_taskfile
*tf
)
181 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
182 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
184 if (tf
->ctl
!= ap
->last_ctl
) {
185 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
186 ap
->last_ctl
= tf
->ctl
;
190 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
191 ata_outb(ap
->host
, tf
->hob_feature
, ioaddr
->feature_addr
);
192 ata_outb(ap
->host
, tf
->hob_nsect
, ioaddr
->nsect_addr
);
193 ata_outb(ap
->host
, tf
->hob_lbal
, ioaddr
->lbal_addr
);
194 ata_outb(ap
->host
, tf
->hob_lbam
, ioaddr
->lbam_addr
);
195 ata_outb(ap
->host
, tf
->hob_lbah
, ioaddr
->lbah_addr
);
199 ata_outb(ap
->host
, tf
->feature
, ioaddr
->feature_addr
);
200 ata_outb(ap
->host
, tf
->nsect
, ioaddr
->nsect_addr
);
201 ata_outb(ap
->host
, tf
->lbal
, ioaddr
->lbal_addr
);
202 ata_outb(ap
->host
, tf
->lbam
, ioaddr
->lbam_addr
);
203 ata_outb(ap
->host
, tf
->lbah
, ioaddr
->lbah_addr
);
206 if (tf
->flags
& ATA_TFLAG_DEVICE
)
207 ata_outb(ap
->host
, tf
->device
, ioaddr
->device_addr
);
213 * pata_s3c_tf_read - input device's ATA taskfile shadow registers
215 static void pata_s3c_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
217 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
219 tf
->feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
220 tf
->nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
221 tf
->lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
222 tf
->lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
223 tf
->lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
224 tf
->device
= ata_inb(ap
->host
, ioaddr
->device_addr
);
226 if (tf
->flags
& ATA_TFLAG_LBA48
) {
227 ata_outb(ap
->host
, tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
228 tf
->hob_feature
= ata_inb(ap
->host
, ioaddr
->error_addr
);
229 tf
->hob_nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
230 tf
->hob_lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
231 tf
->hob_lbam
= ata_inb(ap
->host
, ioaddr
->lbam_addr
);
232 tf
->hob_lbah
= ata_inb(ap
->host
, ioaddr
->lbah_addr
);
233 ata_outb(ap
->host
, tf
->ctl
, ioaddr
->ctl_addr
);
234 ap
->last_ctl
= tf
->ctl
;
239 * pata_s3c_exec_command - issue ATA command to host controller
241 static void pata_s3c_exec_command(struct ata_port
*ap
,
242 const struct ata_taskfile
*tf
)
244 ata_outb(ap
->host
, tf
->command
, ap
->ioaddr
.command_addr
);
249 * pata_s3c_check_status - Read device status register
251 static u8
pata_s3c_check_status(struct ata_port
*ap
)
253 return ata_inb(ap
->host
, ap
->ioaddr
.status_addr
);
257 * pata_s3c_check_altstatus - Read alternate device status register
259 static u8
pata_s3c_check_altstatus(struct ata_port
*ap
)
261 return ata_inb(ap
->host
, ap
->ioaddr
.altstatus_addr
);
265 * pata_s3c_data_xfer - Transfer data by PIO
267 static unsigned int pata_s3c_data_xfer(struct ata_queued_cmd
*qc
,
268 unsigned char *buf
, unsigned int buflen
, int rw
)
270 struct ata_port
*ap
= qc
->dev
->link
->ap
;
271 struct s3c_ide_info
*info
= ap
->host
->private_data
;
272 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
273 unsigned int words
= buflen
>> 1, i
;
274 u16
*data_ptr
= (u16
*)buf
;
276 /* Requires wait same as in ata_inb/ata_outb */
278 for (i
= 0; i
< words
; i
++, data_ptr
++) {
279 wait_for_host_ready(info
);
280 (void) readw(data_addr
);
281 wait_for_host_ready(info
);
282 *data_ptr
= readw(info
->ide_addr
283 + S3C_ATA_PIO_RDATA
);
286 for (i
= 0; i
< words
; i
++, data_ptr
++) {
287 wait_for_host_ready(info
);
288 writew(*data_ptr
, data_addr
);
292 dev_err(ap
->dev
, "unexpected trailing data\n");
298 * pata_s3c_dev_select - Select device on ATA bus
300 static void pata_s3c_dev_select(struct ata_port
*ap
, unsigned int device
)
302 u8 tmp
= ATA_DEVICE_OBS
;
307 ata_outb(ap
->host
, tmp
, ap
->ioaddr
.device_addr
);
312 * pata_s3c_devchk - PATA device presence detection
314 static unsigned int pata_s3c_devchk(struct ata_port
*ap
,
317 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
320 pata_s3c_dev_select(ap
, device
);
322 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
323 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
325 ata_outb(ap
->host
, 0xaa, ioaddr
->nsect_addr
);
326 ata_outb(ap
->host
, 0x55, ioaddr
->lbal_addr
);
328 ata_outb(ap
->host
, 0x55, ioaddr
->nsect_addr
);
329 ata_outb(ap
->host
, 0xaa, ioaddr
->lbal_addr
);
331 nsect
= ata_inb(ap
->host
, ioaddr
->nsect_addr
);
332 lbal
= ata_inb(ap
->host
, ioaddr
->lbal_addr
);
334 if ((nsect
== 0x55) && (lbal
== 0xaa))
335 return 1; /* we found a device */
337 return 0; /* nothing found */
341 * pata_s3c_wait_after_reset - wait for devices to become ready after reset
343 static int pata_s3c_wait_after_reset(struct ata_link
*link
,
344 unsigned long deadline
)
348 ata_msleep(link
->ap
, ATA_WAIT_AFTER_RESET
);
350 /* always check readiness of the master device */
351 rc
= ata_sff_wait_ready(link
, deadline
);
352 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
353 * and TF status is 0xff, bail out on it too.
362 * pata_s3c_bus_softreset - PATA device software reset
364 static int pata_s3c_bus_softreset(struct ata_port
*ap
,
365 unsigned long deadline
)
367 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
369 /* software reset. causes dev0 to be selected */
370 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
372 ata_outb(ap
->host
, ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
374 ata_outb(ap
->host
, ap
->ctl
, ioaddr
->ctl_addr
);
375 ap
->last_ctl
= ap
->ctl
;
377 return pata_s3c_wait_after_reset(&ap
->link
, deadline
);
381 * pata_s3c_softreset - reset host port via ATA SRST
383 static int pata_s3c_softreset(struct ata_link
*link
, unsigned int *classes
,
384 unsigned long deadline
)
386 struct ata_port
*ap
= link
->ap
;
387 unsigned int devmask
= 0;
391 /* determine if device 0 is present */
392 if (pata_s3c_devchk(ap
, 0))
395 /* select device 0 again */
396 pata_s3c_dev_select(ap
, 0);
398 /* issue bus reset */
399 rc
= pata_s3c_bus_softreset(ap
, deadline
);
400 /* if link is occupied, -ENODEV too is an error */
401 if (rc
&& rc
!= -ENODEV
) {
402 ata_link_err(link
, "SRST failed (errno=%d)\n", rc
);
406 /* determine by signature whether we have ATA or ATAPI devices */
407 classes
[0] = ata_sff_dev_classify(&ap
->link
.device
[0],
408 devmask
& (1 << 0), &err
);
414 * pata_s3c_set_devctl - Write device control register
416 static void pata_s3c_set_devctl(struct ata_port
*ap
, u8 ctl
)
418 ata_outb(ap
->host
, ctl
, ap
->ioaddr
.ctl_addr
);
421 static struct scsi_host_template pata_s3c_sht
= {
422 ATA_PIO_SHT(DRV_NAME
),
425 static struct ata_port_operations pata_s3c_port_ops
= {
426 .inherits
= &ata_sff_port_ops
,
427 .sff_check_status
= pata_s3c_check_status
,
428 .sff_check_altstatus
= pata_s3c_check_altstatus
,
429 .sff_tf_load
= pata_s3c_tf_load
,
430 .sff_tf_read
= pata_s3c_tf_read
,
431 .sff_data_xfer
= pata_s3c_data_xfer
,
432 .sff_exec_command
= pata_s3c_exec_command
,
433 .sff_dev_select
= pata_s3c_dev_select
,
434 .sff_set_devctl
= pata_s3c_set_devctl
,
435 .softreset
= pata_s3c_softreset
,
436 .set_piomode
= pata_s3c_set_piomode
,
439 static struct ata_port_operations pata_s5p_port_ops
= {
440 .inherits
= &ata_sff_port_ops
,
441 .set_piomode
= pata_s3c_set_piomode
,
444 static void pata_s3c_enable(void __iomem
*s3c_ide_regbase
, bool state
)
446 u32 temp
= readl(s3c_ide_regbase
+ S3C_ATA_CTRL
);
447 temp
= state
? (temp
| 1) : (temp
& ~1);
448 writel(temp
, s3c_ide_regbase
+ S3C_ATA_CTRL
);
451 static irqreturn_t
pata_s3c_irq(int irq
, void *dev_instance
)
453 struct ata_host
*host
= dev_instance
;
454 struct s3c_ide_info
*info
= host
->private_data
;
457 reg
= readl(info
->ide_addr
+ S3C_ATA_IRQ
);
458 writel(reg
, info
->ide_addr
+ S3C_ATA_IRQ
);
460 return ata_sff_interrupt(irq
, dev_instance
);
463 static void pata_s3c_hwinit(struct s3c_ide_info
*info
,
464 struct s3c_ide_platdata
*pdata
)
466 switch (info
->cpu_type
) {
468 /* Configure as big endian */
469 pata_s3c_cfg_mode(info
->sfr_addr
);
470 pata_s3c_set_endian(info
->ide_addr
, 1);
471 pata_s3c_enable(info
->ide_addr
, true);
474 /* Remove IRQ Status */
475 writel(0x1f, info
->ide_addr
+ S3C_ATA_IRQ
);
476 writel(0x1b, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
480 /* Configure as little endian */
481 pata_s3c_set_endian(info
->ide_addr
, 0);
482 pata_s3c_enable(info
->ide_addr
, true);
485 /* Remove IRQ Status */
486 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ
);
487 writel(0x3f, info
->ide_addr
+ S3C_ATA_IRQ_MSK
);
495 static int __init
pata_s3c_probe(struct platform_device
*pdev
)
497 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
498 struct device
*dev
= &pdev
->dev
;
499 struct s3c_ide_info
*info
;
500 struct resource
*res
;
502 struct ata_host
*host
;
503 enum s3c_cpu_type cpu_type
;
506 cpu_type
= platform_get_device_id(pdev
)->driver_data
;
508 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
512 info
->irq
= platform_get_irq(pdev
, 0);
514 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
516 info
->ide_addr
= devm_ioremap_resource(dev
, res
);
517 if (IS_ERR(info
->ide_addr
))
518 return PTR_ERR(info
->ide_addr
);
520 info
->clk
= devm_clk_get(&pdev
->dev
, "cfcon");
521 if (IS_ERR(info
->clk
)) {
522 dev_err(dev
, "failed to get access to cf controller clock\n");
523 ret
= PTR_ERR(info
->clk
);
528 clk_enable(info
->clk
);
531 host
= ata_host_alloc(dev
, 1);
533 dev_err(dev
, "failed to allocate ide host\n");
539 ap
->pio_mask
= ATA_PIO4
;
541 if (cpu_type
== TYPE_S3C64XX
) {
542 ap
->ops
= &pata_s3c_port_ops
;
543 info
->sfr_addr
= info
->ide_addr
+ 0x1800;
544 info
->ide_addr
+= 0x1900;
545 info
->fifo_status_reg
= 0x94;
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 ret
= ata_host_activate(host
, info
->irq
,
585 info
->irq
? pata_s3c_irq
: NULL
,
593 clk_disable(info
->clk
);
597 static int __exit
pata_s3c_remove(struct platform_device
*pdev
)
599 struct ata_host
*host
= platform_get_drvdata(pdev
);
600 struct s3c_ide_info
*info
= host
->private_data
;
602 ata_host_detach(host
);
604 clk_disable(info
->clk
);
609 #ifdef CONFIG_PM_SLEEP
610 static int pata_s3c_suspend(struct device
*dev
)
612 struct platform_device
*pdev
= to_platform_device(dev
);
613 struct ata_host
*host
= platform_get_drvdata(pdev
);
615 return ata_host_suspend(host
, PMSG_SUSPEND
);
618 static int pata_s3c_resume(struct device
*dev
)
620 struct platform_device
*pdev
= to_platform_device(dev
);
621 struct ata_host
*host
= platform_get_drvdata(pdev
);
622 struct s3c_ide_platdata
*pdata
= dev_get_platdata(&pdev
->dev
);
623 struct s3c_ide_info
*info
= host
->private_data
;
625 pata_s3c_hwinit(info
, pdata
);
626 ata_host_resume(host
);
631 static const struct dev_pm_ops pata_s3c_pm_ops
= {
632 .suspend
= pata_s3c_suspend
,
633 .resume
= pata_s3c_resume
,
637 /* driver device registration */
638 static const struct platform_device_id pata_s3c_driver_ids
[] = {
640 .name
= "s3c64xx-pata",
641 .driver_data
= TYPE_S3C64XX
,
643 .name
= "s5pv210-pata",
644 .driver_data
= TYPE_S5PV210
,
649 MODULE_DEVICE_TABLE(platform
, pata_s3c_driver_ids
);
651 static struct platform_driver pata_s3c_driver
= {
652 .remove
= __exit_p(pata_s3c_remove
),
653 .id_table
= pata_s3c_driver_ids
,
656 #ifdef CONFIG_PM_SLEEP
657 .pm
= &pata_s3c_pm_ops
,
662 module_platform_driver_probe(pata_s3c_driver
, pata_s3c_probe
);
664 MODULE_AUTHOR("Abhilash Kesavan, <a.kesavan@samsung.com>");
665 MODULE_DESCRIPTION("low-level driver for Samsung PATA controller");
666 MODULE_LICENSE("GPL");
667 MODULE_VERSION(DRV_VERSION
);