1 // SPDX-License-Identifier: GPL-2.0
4 * Buddha, Catweasel and X-Surf PATA controller driver
6 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
11 * Copyright (C) 1997, 2001 by Geert Uytterhoeven and others
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>
21 #include <linux/mod_devicetable.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/zorro.h>
25 #include <scsi/scsi_cmnd.h>
26 #include <scsi/scsi_host.h>
28 #include <asm/amigahw.h>
29 #include <asm/amigaints.h>
30 #include <asm/setup.h>
32 #define DRV_NAME "pata_buddha"
33 #define DRV_VERSION "0.1.1"
35 #define BUDDHA_BASE1 0x800
36 #define BUDDHA_BASE2 0xa00
37 #define BUDDHA_BASE3 0xc00
38 #define XSURF_BASE1 0xb000 /* 2.5" interface */
39 #define XSURF_BASE2 0xd000 /* 3.5" interface */
40 #define BUDDHA_CONTROL 0x11a
41 #define BUDDHA_IRQ 0xf00
42 #define XSURF_IRQ 0x7e
43 #define BUDDHA_IRQ_MR 0xfc0 /* master interrupt enable */
51 static unsigned int buddha_bases[3] = {
52 BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
55 static unsigned int xsurf_bases[2] = {
56 XSURF_BASE1, XSURF_BASE2
59 static const struct scsi_host_template pata_buddha_sht = {
60 ATA_PIO_SHT(DRV_NAME),
63 /* FIXME: is this needed? */
64 static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
66 unsigned int buflen, int rw)
68 struct ata_device *dev = qc->dev;
69 struct ata_port *ap = dev->link->ap;
70 void __iomem *data_addr = ap->ioaddr.data_addr;
71 unsigned int words = buflen >> 1;
73 /* Transfer multiple of 2 bytes */
75 raw_insw((u16 *)data_addr, (u16 *)buf, words);
77 raw_outsw((u16 *)data_addr, (u16 *)buf, words);
79 /* Transfer trailing byte, if any. */
80 if (unlikely(buflen & 0x01)) {
81 unsigned char pad[2] = { };
83 /* Point buf to the tail of buffer */
87 raw_insw((u16 *)data_addr, (u16 *)pad, 1);
91 raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
100 * Provide our own set_mode() as we don't want to change anything that has
101 * already been configured..
103 static int pata_buddha_set_mode(struct ata_link *link,
104 struct ata_device **unused)
106 struct ata_device *dev;
108 ata_for_each_dev(dev, link, ENABLED) {
109 /* We don't really care */
110 dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
111 dev->xfer_shift = ATA_SHIFT_PIO;
112 dev->flags |= ATA_DFLAG_PIO;
113 ata_dev_info(dev, "configured for PIO\n");
118 static bool pata_buddha_irq_check(struct ata_port *ap)
122 ch = z_readb((unsigned long)ap->private_data);
124 return !!(ch & 0x80);
127 static void pata_xsurf_irq_clear(struct ata_port *ap)
129 z_writeb(0, (unsigned long)ap->private_data);
132 static struct ata_port_operations pata_buddha_ops = {
133 .inherits = &ata_sff_port_ops,
134 .sff_data_xfer = pata_buddha_data_xfer,
135 .sff_irq_check = pata_buddha_irq_check,
136 .cable_detect = ata_cable_unknown,
137 .set_mode = pata_buddha_set_mode,
140 static struct ata_port_operations pata_xsurf_ops = {
141 .inherits = &ata_sff_port_ops,
142 .sff_data_xfer = pata_buddha_data_xfer,
143 .sff_irq_check = pata_buddha_irq_check,
144 .sff_irq_clear = pata_xsurf_irq_clear,
145 .cable_detect = ata_cable_unknown,
146 .set_mode = pata_buddha_set_mode,
149 static int pata_buddha_probe(struct zorro_dev *z,
150 const struct zorro_device_id *ent)
152 static const char * const board_name[] = {
153 "Buddha", "Catweasel", "X-Surf"
155 struct ata_host *host;
156 void __iomem *buddha_board;
158 unsigned int type = ent->driver_data;
159 unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
163 dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
165 board = z->resource.start;
167 if (type != BOARD_XSURF) {
168 if (!devm_request_mem_region(&z->dev,
169 board + BUDDHA_BASE1,
173 if (!devm_request_mem_region(&z->dev,
177 if (!devm_request_mem_region(&z->dev,
183 /* Workaround for X-Surf: Save drvdata in case zorro8390 has set it */
184 if (type == BOARD_XSURF)
185 old_drvdata = dev_get_drvdata(&z->dev);
188 host = ata_host_alloc(&z->dev, nr_ports);
189 if (type == BOARD_XSURF)
190 dev_set_drvdata(&z->dev, old_drvdata);
194 buddha_board = ZTWO_VADDR(board);
196 /* enable the board IRQ on Buddha/Catweasel */
197 if (type != BOARD_XSURF)
198 z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
200 for (i = 0; i < nr_ports; i++) {
201 struct ata_port *ap = host->ports[i];
202 void __iomem *base, *irqport;
203 unsigned long ctl = 0;
205 if (type != BOARD_XSURF) {
206 ap->ops = &pata_buddha_ops;
207 base = buddha_board + buddha_bases[i];
208 ctl = BUDDHA_CONTROL;
209 irqport = buddha_board + BUDDHA_IRQ + i * 0x40;
211 ap->ops = &pata_xsurf_ops;
212 base = buddha_board + xsurf_bases[i];
213 /* X-Surf has no CS1* (Control/AltStat) */
214 irqport = buddha_board + XSURF_IRQ;
217 ap->pio_mask = ATA_PIO4;
218 ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
220 ap->ioaddr.data_addr = base;
221 ap->ioaddr.error_addr = base + 2 + 1 * 4;
222 ap->ioaddr.feature_addr = base + 2 + 1 * 4;
223 ap->ioaddr.nsect_addr = base + 2 + 2 * 4;
224 ap->ioaddr.lbal_addr = base + 2 + 3 * 4;
225 ap->ioaddr.lbam_addr = base + 2 + 4 * 4;
226 ap->ioaddr.lbah_addr = base + 2 + 5 * 4;
227 ap->ioaddr.device_addr = base + 2 + 6 * 4;
228 ap->ioaddr.status_addr = base + 2 + 7 * 4;
229 ap->ioaddr.command_addr = base + 2 + 7 * 4;
232 ap->ioaddr.altstatus_addr = base + ctl;
233 ap->ioaddr.ctl_addr = base + ctl;
236 ap->private_data = (void *)irqport;
238 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
239 ctl ? board + buddha_bases[i] + ctl : 0);
242 ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
243 IRQF_SHARED, &pata_buddha_sht);
248 static void pata_buddha_remove(struct zorro_dev *z)
250 struct ata_host *host = dev_get_drvdata(&z->dev);
252 ata_host_detach(host);
255 static const struct zorro_device_id pata_buddha_zorro_tbl[] = {
256 { ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA, BOARD_BUDDHA},
257 { ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL, BOARD_CATWEASEL},
260 MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
262 static struct zorro_driver pata_buddha_driver = {
263 .name = "pata_buddha",
264 .id_table = pata_buddha_zorro_tbl,
265 .probe = pata_buddha_probe,
266 .remove = pata_buddha_remove,
270 * We cannot have a modalias for X-Surf boards, as it competes with the
271 * zorro8390 network driver. As a stopgap measure until we have proper
272 * MFD support for this board, we manually attach to it late after Zorro
273 * has enumerated its boards.
275 static int __init pata_buddha_late_init(void)
277 struct zorro_dev *z = NULL;
279 /* Auto-bind to regular boards */
280 zorro_register_driver(&pata_buddha_driver);
282 /* Manually bind to all X-Surf boards */
283 while ((z = zorro_find_device(ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, z))) {
284 static struct zorro_device_id xsurf_ent = {
285 ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, BOARD_XSURF
288 pata_buddha_probe(z, &xsurf_ent);
293 late_initcall(pata_buddha_late_init);
295 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
296 MODULE_DESCRIPTION("low-level driver for Buddha/Catweasel/X-Surf PATA");
297 MODULE_LICENSE("GPL v2");
298 MODULE_VERSION(DRV_VERSION);