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>
31 #include <asm/setup.h>
33 #define DRV_NAME "pata_buddha"
34 #define DRV_VERSION "0.1.1"
36 #define BUDDHA_BASE1 0x800
37 #define BUDDHA_BASE2 0xa00
38 #define BUDDHA_BASE3 0xc00
39 #define XSURF_BASE1 0xb000 /* 2.5" interface */
40 #define XSURF_BASE2 0xd000 /* 3.5" interface */
41 #define BUDDHA_CONTROL 0x11a
42 #define BUDDHA_IRQ 0xf00
43 #define XSURF_IRQ 0x7e
44 #define BUDDHA_IRQ_MR 0xfc0 /* master interrupt enable */
52 static unsigned int buddha_bases[3] = {
53 BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
56 static unsigned int xsurf_bases[2] = {
57 XSURF_BASE1, XSURF_BASE2
60 static struct scsi_host_template pata_buddha_sht = {
61 ATA_PIO_SHT(DRV_NAME),
64 /* FIXME: is this needed? */
65 static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
67 unsigned int buflen, int rw)
69 struct ata_device *dev = qc->dev;
70 struct ata_port *ap = dev->link->ap;
71 void __iomem *data_addr = ap->ioaddr.data_addr;
72 unsigned int words = buflen >> 1;
74 /* Transfer multiple of 2 bytes */
76 raw_insw((u16 *)data_addr, (u16 *)buf, words);
78 raw_outsw((u16 *)data_addr, (u16 *)buf, words);
80 /* Transfer trailing byte, if any. */
81 if (unlikely(buflen & 0x01)) {
82 unsigned char pad[2] = { };
84 /* Point buf to the tail of buffer */
88 raw_insw((u16 *)data_addr, (u16 *)pad, 1);
92 raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
101 * Provide our own set_mode() as we don't want to change anything that has
102 * already been configured..
104 static int pata_buddha_set_mode(struct ata_link *link,
105 struct ata_device **unused)
107 struct ata_device *dev;
109 ata_for_each_dev(dev, link, ENABLED) {
110 /* We don't really care */
111 dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
112 dev->xfer_shift = ATA_SHIFT_PIO;
113 dev->flags |= ATA_DFLAG_PIO;
114 ata_dev_info(dev, "configured for PIO\n");
119 static bool pata_buddha_irq_check(struct ata_port *ap)
123 ch = z_readb((unsigned long)ap->private_data);
125 return !!(ch & 0x80);
128 static void pata_xsurf_irq_clear(struct ata_port *ap)
130 z_writeb(0, (unsigned long)ap->private_data);
133 static struct ata_port_operations pata_buddha_ops = {
134 .inherits = &ata_sff_port_ops,
135 .sff_data_xfer = pata_buddha_data_xfer,
136 .sff_irq_check = pata_buddha_irq_check,
137 .cable_detect = ata_cable_unknown,
138 .set_mode = pata_buddha_set_mode,
141 static struct ata_port_operations pata_xsurf_ops = {
142 .inherits = &ata_sff_port_ops,
143 .sff_data_xfer = pata_buddha_data_xfer,
144 .sff_irq_check = pata_buddha_irq_check,
145 .sff_irq_clear = pata_xsurf_irq_clear,
146 .cable_detect = ata_cable_unknown,
147 .set_mode = pata_buddha_set_mode,
150 static int pata_buddha_probe(struct zorro_dev *z,
151 const struct zorro_device_id *ent)
153 static const char * const board_name[] = {
154 "Buddha", "Catweasel", "X-Surf"
156 struct ata_host *host;
157 void __iomem *buddha_board;
159 unsigned int type = ent->driver_data;
160 unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
164 dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
166 board = z->resource.start;
168 if (type != BOARD_XSURF) {
169 if (!devm_request_mem_region(&z->dev,
170 board + BUDDHA_BASE1,
174 if (!devm_request_mem_region(&z->dev,
178 if (!devm_request_mem_region(&z->dev,
184 /* Workaround for X-Surf: Save drvdata in case zorro8390 has set it */
185 if (type == BOARD_XSURF)
186 old_drvdata = dev_get_drvdata(&z->dev);
189 host = ata_host_alloc(&z->dev, nr_ports);
190 if (type == BOARD_XSURF)
191 dev_set_drvdata(&z->dev, old_drvdata);
195 buddha_board = ZTWO_VADDR(board);
197 /* enable the board IRQ on Buddha/Catweasel */
198 if (type != BOARD_XSURF)
199 z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
201 for (i = 0; i < nr_ports; i++) {
202 struct ata_port *ap = host->ports[i];
203 void __iomem *base, *irqport;
204 unsigned long ctl = 0;
206 if (type != BOARD_XSURF) {
207 ap->ops = &pata_buddha_ops;
208 base = buddha_board + buddha_bases[i];
209 ctl = BUDDHA_CONTROL;
210 irqport = buddha_board + BUDDHA_IRQ + i * 0x40;
212 ap->ops = &pata_xsurf_ops;
213 base = buddha_board + xsurf_bases[i];
214 /* X-Surf has no CS1* (Control/AltStat) */
215 irqport = buddha_board + XSURF_IRQ;
218 ap->pio_mask = ATA_PIO4;
219 ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
221 ap->ioaddr.data_addr = base;
222 ap->ioaddr.error_addr = base + 2 + 1 * 4;
223 ap->ioaddr.feature_addr = base + 2 + 1 * 4;
224 ap->ioaddr.nsect_addr = base + 2 + 2 * 4;
225 ap->ioaddr.lbal_addr = base + 2 + 3 * 4;
226 ap->ioaddr.lbam_addr = base + 2 + 4 * 4;
227 ap->ioaddr.lbah_addr = base + 2 + 5 * 4;
228 ap->ioaddr.device_addr = base + 2 + 6 * 4;
229 ap->ioaddr.status_addr = base + 2 + 7 * 4;
230 ap->ioaddr.command_addr = base + 2 + 7 * 4;
233 ap->ioaddr.altstatus_addr = base + ctl;
234 ap->ioaddr.ctl_addr = base + ctl;
237 ap->private_data = (void *)irqport;
239 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
240 ctl ? board + buddha_bases[i] + ctl : 0);
243 ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
244 IRQF_SHARED, &pata_buddha_sht);
249 static void pata_buddha_remove(struct zorro_dev *z)
251 struct ata_host *host = dev_get_drvdata(&z->dev);
253 ata_host_detach(host);
256 static const struct zorro_device_id pata_buddha_zorro_tbl[] = {
257 { ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA, BOARD_BUDDHA},
258 { ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL, BOARD_CATWEASEL},
261 MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
263 static struct zorro_driver pata_buddha_driver = {
264 .name = "pata_buddha",
265 .id_table = pata_buddha_zorro_tbl,
266 .probe = pata_buddha_probe,
267 .remove = pata_buddha_remove,
271 * We cannot have a modalias for X-Surf boards, as it competes with the
272 * zorro8390 network driver. As a stopgap measure until we have proper
273 * MFD support for this board, we manually attach to it late after Zorro
274 * has enumerated its boards.
276 static int __init pata_buddha_late_init(void)
278 struct zorro_dev *z = NULL;
280 /* Auto-bind to regular boards */
281 zorro_register_driver(&pata_buddha_driver);
283 /* Manually bind to all X-Surf boards */
284 while ((z = zorro_find_device(ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, z))) {
285 static struct zorro_device_id xsurf_ent = {
286 ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, BOARD_XSURF
289 pata_buddha_probe(z, &xsurf_ent);
294 late_initcall(pata_buddha_late_init);
296 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
297 MODULE_DESCRIPTION("low-level driver for Buddha/Catweasel/X-Surf PATA");
298 MODULE_LICENSE("GPL v2");
299 MODULE_VERSION(DRV_VERSION);