1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Amiga Gayle PATA controller driver
5 *
6 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
8 *
9 * Based on gayle.c:
10 *
11 * Created 12 Jul 1997 by Geert Uytterhoeven
12 */
13
14 #include <linux/ata.h>
15 #include <linux/blkdev.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/libata.h>
20 #include <linux/mm.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/zorro.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_host.h>
26
27 #include <asm/amigahw.h>
28 #include <asm/amigaints.h>
29 #include <asm/amigayle.h>
30 #include <asm/setup.h>
31
32 #define DRV_NAME "pata_gayle"
33 #define DRV_VERSION "0.1.0"
34
35 #define GAYLE_CONTROL 0x101a
36
37 static const struct scsi_host_template pata_gayle_sht = {
38 ATA_PIO_SHT(DRV_NAME),
39 };
40
41 /* FIXME: is this needed? */
pata_gayle_data_xfer(struct ata_queued_cmd * qc,unsigned char * buf,unsigned int buflen,int rw)42 static unsigned int pata_gayle_data_xfer(struct ata_queued_cmd *qc,
43 unsigned char *buf,
44 unsigned int buflen, int rw)
45 {
46 struct ata_device *dev = qc->dev;
47 struct ata_port *ap = dev->link->ap;
48 void __iomem *data_addr = ap->ioaddr.data_addr;
49 unsigned int words = buflen >> 1;
50
51 /* Transfer multiple of 2 bytes */
52 if (rw == READ)
53 raw_insw((u16 *)data_addr, (u16 *)buf, words);
54 else
55 raw_outsw((u16 *)data_addr, (u16 *)buf, words);
56
57 /* Transfer trailing byte, if any. */
58 if (unlikely(buflen & 0x01)) {
59 unsigned char pad[2] = { };
60
61 /* Point buf to the tail of buffer */
62 buf += buflen - 1;
63
64 if (rw == READ) {
65 raw_insw((u16 *)data_addr, (u16 *)pad, 1);
66 *buf = pad[0];
67 } else {
68 pad[0] = *buf;
69 raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
70 }
71 words++;
72 }
73
74 return words << 1;
75 }
76
77 /*
78 * Provide our own set_mode() as we don't want to change anything that has
79 * already been configured..
80 */
pata_gayle_set_mode(struct ata_link * link,struct ata_device ** unused)81 static int pata_gayle_set_mode(struct ata_link *link,
82 struct ata_device **unused)
83 {
84 struct ata_device *dev;
85
86 ata_for_each_dev(dev, link, ENABLED) {
87 /* We don't really care */
88 dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
89 dev->xfer_shift = ATA_SHIFT_PIO;
90 dev->flags |= ATA_DFLAG_PIO;
91 ata_dev_info(dev, "configured for PIO\n");
92 }
93 return 0;
94 }
95
pata_gayle_irq_check(struct ata_port * ap)96 static bool pata_gayle_irq_check(struct ata_port *ap)
97 {
98 u8 ch;
99
100 ch = z_readb((unsigned long)ap->private_data);
101
102 return !!(ch & GAYLE_IRQ_IDE);
103 }
104
pata_gayle_irq_clear(struct ata_port * ap)105 static void pata_gayle_irq_clear(struct ata_port *ap)
106 {
107 (void)z_readb((unsigned long)ap->ioaddr.status_addr);
108 z_writeb(0x7c, (unsigned long)ap->private_data);
109 }
110
111 static struct ata_port_operations pata_gayle_a1200_ops = {
112 .inherits = &ata_sff_port_ops,
113 .sff_data_xfer = pata_gayle_data_xfer,
114 .sff_irq_check = pata_gayle_irq_check,
115 .sff_irq_clear = pata_gayle_irq_clear,
116 .cable_detect = ata_cable_unknown,
117 .set_mode = pata_gayle_set_mode,
118 };
119
120 static struct ata_port_operations pata_gayle_a4000_ops = {
121 .inherits = &ata_sff_port_ops,
122 .sff_data_xfer = pata_gayle_data_xfer,
123 .cable_detect = ata_cable_unknown,
124 .set_mode = pata_gayle_set_mode,
125 };
126
pata_gayle_init_one(struct platform_device * pdev)127 static int __init pata_gayle_init_one(struct platform_device *pdev)
128 {
129 struct resource *res;
130 struct gayle_ide_platform_data *pdata;
131 struct ata_host *host;
132 struct ata_port *ap;
133 void __iomem *base;
134 int ret;
135
136 pdata = dev_get_platdata(&pdev->dev);
137
138 dev_info(&pdev->dev, "Amiga Gayle IDE controller (A%u style)\n",
139 pdata->explicit_ack ? 1200 : 4000);
140
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 if (!res)
143 return -ENODEV;
144
145 if (!devm_request_mem_region(&pdev->dev, res->start,
146 resource_size(res), DRV_NAME)) {
147 pr_err(DRV_NAME ": resources busy\n");
148 return -EBUSY;
149 }
150
151 /* allocate host */
152 host = ata_host_alloc(&pdev->dev, 1);
153 if (!host)
154 return -ENOMEM;
155
156 ap = host->ports[0];
157
158 if (pdata->explicit_ack)
159 ap->ops = &pata_gayle_a1200_ops;
160 else
161 ap->ops = &pata_gayle_a4000_ops;
162
163 ap->pio_mask = ATA_PIO4;
164 ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
165
166 base = ZTWO_VADDR(pdata->base);
167 ap->ioaddr.data_addr = base;
168 ap->ioaddr.error_addr = base + 2 + 1 * 4;
169 ap->ioaddr.feature_addr = base + 2 + 1 * 4;
170 ap->ioaddr.nsect_addr = base + 2 + 2 * 4;
171 ap->ioaddr.lbal_addr = base + 2 + 3 * 4;
172 ap->ioaddr.lbam_addr = base + 2 + 4 * 4;
173 ap->ioaddr.lbah_addr = base + 2 + 5 * 4;
174 ap->ioaddr.device_addr = base + 2 + 6 * 4;
175 ap->ioaddr.status_addr = base + 2 + 7 * 4;
176 ap->ioaddr.command_addr = base + 2 + 7 * 4;
177
178 ap->ioaddr.altstatus_addr = base + GAYLE_CONTROL;
179 ap->ioaddr.ctl_addr = base + GAYLE_CONTROL;
180
181 ap->private_data = (void *)ZTWO_VADDR(pdata->irqport);
182
183 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", pdata->base,
184 pdata->base + GAYLE_CONTROL);
185
186 ret = ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
187 IRQF_SHARED, &pata_gayle_sht);
188 if (ret)
189 return ret;
190
191 platform_set_drvdata(pdev, host);
192
193 return 0;
194 }
195
pata_gayle_remove_one(struct platform_device * pdev)196 static int __exit pata_gayle_remove_one(struct platform_device *pdev)
197 {
198 struct ata_host *host = platform_get_drvdata(pdev);
199
200 ata_host_detach(host);
201
202 return 0;
203 }
204
205 static struct platform_driver pata_gayle_driver = {
206 .remove = __exit_p(pata_gayle_remove_one),
207 .driver = {
208 .name = "amiga-gayle-ide",
209 },
210 };
211
212 module_platform_driver_probe(pata_gayle_driver, pata_gayle_init_one);
213
214 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
215 MODULE_DESCRIPTION("low-level driver for Amiga Gayle PATA");
216 MODULE_LICENSE("GPL v2");
217 MODULE_ALIAS("platform:amiga-gayle-ide");
218 MODULE_VERSION(DRV_VERSION);
219