1 // SPDX-License-Identifier: GPL-2.0
4 * Atari Falcon PATA controller driver
6 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
9 * Based on falconide.c:
11 * Created 12 Jul 1997 by Geert Uytterhoeven
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/blkdev.h>
18 #include <linux/delay.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <linux/ata.h>
22 #include <linux/libata.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
27 #include <asm/setup.h>
28 #include <asm/atarihw.h>
29 #include <asm/atariints.h>
30 #include <asm/atari_stdma.h>
32 #define DRV_NAME "pata_falcon"
33 #define DRV_VERSION "0.1.0"
35 static int pata_falcon_swap_mask
;
37 module_param_named(data_swab
, pata_falcon_swap_mask
, int, 0444);
38 MODULE_PARM_DESC(data_swab
, "Data byte swap enable/disable bitmap (0x1==drive1, 0x2==drive2, 0x4==drive3, 0x8==drive4, default==0)");
40 static const struct scsi_host_template pata_falcon_sht
= {
41 ATA_PIO_SHT(DRV_NAME
),
44 static unsigned int pata_falcon_data_xfer(struct ata_queued_cmd
*qc
,
46 unsigned int buflen
, int rw
)
48 struct ata_device
*dev
= qc
->dev
;
49 struct ata_port
*ap
= dev
->link
->ap
;
50 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
51 unsigned int words
= buflen
>> 1;
52 struct scsi_cmnd
*cmd
= qc
->scsicmd
;
55 if (dev
->class == ATA_DEV_ATA
&& cmd
&&
56 !blk_rq_is_passthrough(scsi_cmd_to_rq(cmd
)))
57 swap
= (uintptr_t)ap
->private_data
& BIT(dev
->devno
);
59 /* Transfer multiple of 2 bytes */
62 raw_insw_swapw(data_addr
, (u16
*)buf
, words
);
64 raw_insw(data_addr
, (u16
*)buf
, words
);
67 raw_outsw_swapw(data_addr
, (u16
*)buf
, words
);
69 raw_outsw(data_addr
, (u16
*)buf
, words
);
72 /* Transfer trailing byte, if any. */
73 if (unlikely(buflen
& 0x01)) {
74 unsigned char pad
[2] = { };
76 /* Point buf to the tail of buffer */
81 raw_insw_swapw(data_addr
, (u16
*)pad
, 1);
83 raw_insw(data_addr
, (u16
*)pad
, 1);
88 raw_outsw_swapw(data_addr
, (u16
*)pad
, 1);
90 raw_outsw(data_addr
, (u16
*)pad
, 1);
99 * Provide our own set_mode() as we don't want to change anything that has
100 * already been configured..
102 static int pata_falcon_set_mode(struct ata_link
*link
,
103 struct ata_device
**unused
)
105 struct ata_device
*dev
;
107 ata_for_each_dev(dev
, link
, ENABLED
) {
108 /* We don't really care */
109 dev
->pio_mode
= dev
->xfer_mode
= XFER_PIO_0
;
110 dev
->xfer_shift
= ATA_SHIFT_PIO
;
111 dev
->flags
|= ATA_DFLAG_PIO
;
112 ata_dev_info(dev
, "configured for PIO\n");
117 static struct ata_port_operations pata_falcon_ops
= {
118 .inherits
= &ata_sff_port_ops
,
119 .sff_data_xfer
= pata_falcon_data_xfer
,
120 .cable_detect
= ata_cable_unknown
,
121 .set_mode
= pata_falcon_set_mode
,
124 static int pata_falcon_init_one(struct platform_device
*pdev
)
126 struct resource
*base_mem_res
, *ctl_mem_res
;
127 struct resource
*base_res
, *ctl_res
, *irq_res
;
128 struct ata_host
*host
;
130 void __iomem
*base
, *ctl_base
;
131 int mask_shift
= 0; /* Q40 & Falcon default */
132 int irq
= 0, io_offset
= 1, reg_shift
= 2; /* Falcon defaults */
134 dev_info(&pdev
->dev
, "Atari Falcon and Q40/Q60 PATA controller\n");
136 base_res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
137 if (base_res
&& !devm_request_region(&pdev
->dev
, base_res
->start
,
138 resource_size(base_res
), DRV_NAME
)) {
139 dev_err(&pdev
->dev
, "resources busy\n");
143 ctl_res
= platform_get_resource(pdev
, IORESOURCE_IO
, 1);
144 if (ctl_res
&& !devm_request_region(&pdev
->dev
, ctl_res
->start
,
145 resource_size(ctl_res
), DRV_NAME
)) {
146 dev_err(&pdev
->dev
, "resources busy\n");
150 base_mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
153 if (!devm_request_mem_region(&pdev
->dev
, base_mem_res
->start
,
154 resource_size(base_mem_res
), DRV_NAME
)) {
155 dev_err(&pdev
->dev
, "resources busy\n");
159 ctl_mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
164 host
= ata_host_alloc(&pdev
->dev
, 1);
169 ap
->ops
= &pata_falcon_ops
;
170 ap
->pio_mask
= ATA_PIO4
;
171 ap
->flags
|= ATA_FLAG_SLAVE_POSS
| ATA_FLAG_NO_IORDY
;
173 /* N.B. this assumes data_addr will be used for word-sized I/O only */
174 ap
->ioaddr
.data_addr
= (void __iomem
*)base_mem_res
->start
;
176 if (base_res
) { /* only Q40 has IO resources */
179 base
= (void __iomem
*)base_res
->start
;
180 ctl_base
= (void __iomem
*)ctl_res
->start
;
182 base
= (void __iomem
*)base_mem_res
->start
;
183 ctl_base
= (void __iomem
*)ctl_mem_res
->start
;
186 ap
->ioaddr
.error_addr
= base
+ io_offset
+ (1 << reg_shift
);
187 ap
->ioaddr
.feature_addr
= base
+ io_offset
+ (1 << reg_shift
);
188 ap
->ioaddr
.nsect_addr
= base
+ io_offset
+ (2 << reg_shift
);
189 ap
->ioaddr
.lbal_addr
= base
+ io_offset
+ (3 << reg_shift
);
190 ap
->ioaddr
.lbam_addr
= base
+ io_offset
+ (4 << reg_shift
);
191 ap
->ioaddr
.lbah_addr
= base
+ io_offset
+ (5 << reg_shift
);
192 ap
->ioaddr
.device_addr
= base
+ io_offset
+ (6 << reg_shift
);
193 ap
->ioaddr
.status_addr
= base
+ io_offset
+ (7 << reg_shift
);
194 ap
->ioaddr
.command_addr
= base
+ io_offset
+ (7 << reg_shift
);
196 ap
->ioaddr
.altstatus_addr
= ctl_base
+ io_offset
;
197 ap
->ioaddr
.ctl_addr
= ctl_base
+ io_offset
;
199 ata_port_desc(ap
, "cmd %px ctl %px data %px",
200 base
, ctl_base
, ap
->ioaddr
.data_addr
);
204 ap
->private_data
= (void *)(uintptr_t)(pata_falcon_swap_mask
>> mask_shift
);
206 irq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
207 if (irq_res
&& irq_res
->start
> 0) {
208 irq
= irq_res
->start
;
210 ap
->flags
|= ATA_FLAG_PIO_POLLING
;
211 ata_port_desc(ap
, "no IRQ, using PIO polling");
215 return ata_host_activate(host
, irq
, irq
? ata_sff_interrupt
: NULL
,
216 IRQF_SHARED
, &pata_falcon_sht
);
219 static void pata_falcon_remove_one(struct platform_device
*pdev
)
221 struct ata_host
*host
= platform_get_drvdata(pdev
);
223 ata_host_detach(host
);
226 static struct platform_driver pata_falcon_driver
= {
227 .probe
= pata_falcon_init_one
,
228 .remove
= pata_falcon_remove_one
,
230 .name
= "atari-falcon-ide",
234 module_platform_driver(pata_falcon_driver
);
236 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
237 MODULE_DESCRIPTION("low-level driver for Atari Falcon PATA");
238 MODULE_LICENSE("GPL v2");
239 MODULE_ALIAS("platform:atari-falcon-ide");
240 MODULE_VERSION(DRV_VERSION
);