1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Atari Falcon PATA controller driver
5 *
6 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
8 *
9 * Based on falconide.c:
10 *
11 * Created 12 Jul 1997 by Geert Uytterhoeven
12 */
13
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>
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26
27 #include <asm/setup.h>
28 #include <asm/atarihw.h>
29 #include <asm/atariints.h>
30 #include <asm/atari_stdma.h>
31
32 #define DRV_NAME "pata_falcon"
33 #define DRV_VERSION "0.1.0"
34
35 static int pata_falcon_swap_mask;
36
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)");
39
40 static const struct scsi_host_template pata_falcon_sht = {
41 ATA_PIO_SHT(DRV_NAME),
42 };
43
pata_falcon_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)44 static unsigned int pata_falcon_data_xfer(struct ata_queued_cmd *qc,
45 unsigned char *buf,
46 unsigned int buflen, int rw)
47 {
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;
53 bool swap = 1;
54
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);
58
59 /* Transfer multiple of 2 bytes */
60 if (rw == READ) {
61 if (swap)
62 raw_insw_swapw(data_addr, (u16 *)buf, words);
63 else
64 raw_insw(data_addr, (u16 *)buf, words);
65 } else {
66 if (swap)
67 raw_outsw_swapw(data_addr, (u16 *)buf, words);
68 else
69 raw_outsw(data_addr, (u16 *)buf, words);
70 }
71
72 /* Transfer trailing byte, if any. */
73 if (unlikely(buflen & 0x01)) {
74 unsigned char pad[2] = { };
75
76 /* Point buf to the tail of buffer */
77 buf += buflen - 1;
78
79 if (rw == READ) {
80 if (swap)
81 raw_insw_swapw(data_addr, (u16 *)pad, 1);
82 else
83 raw_insw(data_addr, (u16 *)pad, 1);
84 *buf = pad[0];
85 } else {
86 pad[0] = *buf;
87 if (swap)
88 raw_outsw_swapw(data_addr, (u16 *)pad, 1);
89 else
90 raw_outsw(data_addr, (u16 *)pad, 1);
91 }
92 words++;
93 }
94
95 return words << 1;
96 }
97
98 /*
99 * Provide our own set_mode() as we don't want to change anything that has
100 * already been configured..
101 */
pata_falcon_set_mode(struct ata_link * link,struct ata_device ** unused)102 static int pata_falcon_set_mode(struct ata_link *link,
103 struct ata_device **unused)
104 {
105 struct ata_device *dev;
106
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");
113 }
114 return 0;
115 }
116
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,
122 };
123
pata_falcon_init_one(struct platform_device * pdev)124 static int __init pata_falcon_init_one(struct platform_device *pdev)
125 {
126 struct resource *base_mem_res, *ctl_mem_res;
127 struct resource *base_res, *ctl_res, *irq_res;
128 struct ata_host *host;
129 struct ata_port *ap;
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 */
133
134 dev_info(&pdev->dev, "Atari Falcon and Q40/Q60 PATA controller\n");
135
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");
140 return -EBUSY;
141 }
142
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");
147 return -EBUSY;
148 }
149
150 base_mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 if (!base_mem_res)
152 return -ENODEV;
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");
156 return -EBUSY;
157 }
158
159 ctl_mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
160 if (!ctl_mem_res)
161 return -ENODEV;
162
163 /* allocate host */
164 host = ata_host_alloc(&pdev->dev, 1);
165 if (!host)
166 return -ENOMEM;
167 ap = host->ports[0];
168
169 ap->ops = &pata_falcon_ops;
170 ap->pio_mask = ATA_PIO4;
171 ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
172
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;
175
176 if (base_res) { /* only Q40 has IO resources */
177 io_offset = 0x10000;
178 reg_shift = 0;
179 base = (void __iomem *)base_res->start;
180 ctl_base = (void __iomem *)ctl_res->start;
181 } else {
182 base = (void __iomem *)base_mem_res->start;
183 ctl_base = (void __iomem *)ctl_mem_res->start;
184 }
185
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);
195
196 ap->ioaddr.altstatus_addr = ctl_base + io_offset;
197 ap->ioaddr.ctl_addr = ctl_base + io_offset;
198
199 ata_port_desc(ap, "cmd %px ctl %px data %px",
200 base, ctl_base, ap->ioaddr.data_addr);
201
202 if (pdev->id > 0)
203 mask_shift = 2;
204 ap->private_data = (void *)(uintptr_t)(pata_falcon_swap_mask >> mask_shift);
205
206 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
207 if (irq_res && irq_res->start > 0) {
208 irq = irq_res->start;
209 } else {
210 ap->flags |= ATA_FLAG_PIO_POLLING;
211 ata_port_desc(ap, "no IRQ, using PIO polling");
212 }
213
214 /* activate */
215 return ata_host_activate(host, irq, irq ? ata_sff_interrupt : NULL,
216 IRQF_SHARED, &pata_falcon_sht);
217 }
218
pata_falcon_remove_one(struct platform_device * pdev)219 static int __exit pata_falcon_remove_one(struct platform_device *pdev)
220 {
221 struct ata_host *host = platform_get_drvdata(pdev);
222
223 ata_host_detach(host);
224
225 return 0;
226 }
227
228 static struct platform_driver pata_falcon_driver = {
229 .remove = __exit_p(pata_falcon_remove_one),
230 .driver = {
231 .name = "atari-falcon-ide",
232 },
233 };
234
235 module_platform_driver_probe(pata_falcon_driver, pata_falcon_init_one);
236
237 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
238 MODULE_DESCRIPTION("low-level driver for Atari Falcon PATA");
239 MODULE_LICENSE("GPL v2");
240 MODULE_ALIAS("platform:atari-falcon-ide");
241 MODULE_VERSION(DRV_VERSION);
242