xref: /linux/drivers/ata/pata_buddha.c (revision edbafe65eef2b58625db1e113fbbfb1fe10c0291)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Buddha, Catweasel and X-Surf PATA controller driver
5  *
6  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
7  *		http://www.samsung.com
8  *
9  * Based on buddha.c:
10  *
11  *	Copyright (C) 1997, 2001 by Geert Uytterhoeven and others
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/types.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/setup.h>
30 
31 #define DRV_NAME "pata_buddha"
32 #define DRV_VERSION "0.1.1"
33 
34 #define BUDDHA_BASE1	0x800
35 #define BUDDHA_BASE2	0xa00
36 #define BUDDHA_BASE3	0xc00
37 #define XSURF_BASE1	0xb000 /* 2.5" interface */
38 #define XSURF_BASE2	0xd000 /* 3.5" interface */
39 #define BUDDHA_CONTROL	0x11a
40 #define BUDDHA_IRQ	0xf00
41 #define XSURF_IRQ	0x7e
42 #define BUDDHA_IRQ_MR	0xfc0	/* master interrupt enable */
43 
44 enum {
45 	BOARD_BUDDHA = 0,
46 	BOARD_CATWEASEL,
47 	BOARD_XSURF
48 };
49 
50 static unsigned int buddha_bases[3] = {
51 	BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
52 };
53 
54 static unsigned int xsurf_bases[2] = {
55 	XSURF_BASE1, XSURF_BASE2
56 };
57 
58 static const struct scsi_host_template pata_buddha_sht = {
59 	ATA_PIO_SHT(DRV_NAME),
60 };
61 
62 /* FIXME: is this needed? */
63 static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
64 					 unsigned char *buf,
65 					 unsigned int buflen, int rw)
66 {
67 	struct ata_device *dev = qc->dev;
68 	struct ata_port *ap = dev->link->ap;
69 	void __iomem *data_addr = ap->ioaddr.data_addr;
70 	unsigned int words = buflen >> 1;
71 
72 	/* Transfer multiple of 2 bytes */
73 	if (rw == READ)
74 		raw_insw((u16 *)data_addr, (u16 *)buf, words);
75 	else
76 		raw_outsw((u16 *)data_addr, (u16 *)buf, words);
77 
78 	/* Transfer trailing byte, if any. */
79 	if (unlikely(buflen & 0x01)) {
80 		unsigned char pad[2] = { };
81 
82 		/* Point buf to the tail of buffer */
83 		buf += buflen - 1;
84 
85 		if (rw == READ) {
86 			raw_insw((u16 *)data_addr, (u16 *)pad, 1);
87 			*buf = pad[0];
88 		} else {
89 			pad[0] = *buf;
90 			raw_outsw((u16 *)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  */
102 static int pata_buddha_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 bool pata_buddha_irq_check(struct ata_port *ap)
118 {
119 	u8 ch;
120 
121 	ch = z_readb((unsigned long)ap->private_data);
122 
123 	return !!(ch & 0x80);
124 }
125 
126 static void pata_xsurf_irq_clear(struct ata_port *ap)
127 {
128 	z_writeb(0, (unsigned long)ap->private_data);
129 }
130 
131 static struct ata_port_operations pata_buddha_ops = {
132 	.inherits	= &ata_sff_port_ops,
133 	.sff_data_xfer	= pata_buddha_data_xfer,
134 	.sff_irq_check	= pata_buddha_irq_check,
135 	.cable_detect	= ata_cable_unknown,
136 	.set_mode	= pata_buddha_set_mode,
137 };
138 
139 static struct ata_port_operations pata_xsurf_ops = {
140 	.inherits	= &ata_sff_port_ops,
141 	.sff_data_xfer	= pata_buddha_data_xfer,
142 	.sff_irq_check	= pata_buddha_irq_check,
143 	.sff_irq_clear	= pata_xsurf_irq_clear,
144 	.cable_detect	= ata_cable_unknown,
145 	.set_mode	= pata_buddha_set_mode,
146 };
147 
148 static int pata_buddha_probe(struct zorro_dev *z,
149 			     const struct zorro_device_id *ent)
150 {
151 	static const char * const board_name[] = {
152 		"Buddha", "Catweasel", "X-Surf"
153 	};
154 	struct ata_host *host;
155 	void __iomem *buddha_board;
156 	unsigned long board;
157 	unsigned int type = ent->driver_data;
158 	unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
159 	void *old_drvdata;
160 	int i;
161 
162 	dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
163 
164 	board = z->resource.start;
165 
166 	if (type != BOARD_XSURF) {
167 		if (!devm_request_mem_region(&z->dev,
168 					     board + BUDDHA_BASE1,
169 					     0x800, DRV_NAME))
170 			return -ENXIO;
171 	} else {
172 		if (!devm_request_mem_region(&z->dev,
173 					     board + XSURF_BASE1,
174 					     0x1000, DRV_NAME))
175 			return -ENXIO;
176 		if (!devm_request_mem_region(&z->dev,
177 					     board + XSURF_BASE2,
178 					     0x1000, DRV_NAME)) {
179 		}
180 	}
181 
182 	/* Workaround for X-Surf: Save drvdata in case zorro8390 has set it */
183 	if (type == BOARD_XSURF)
184 		old_drvdata = dev_get_drvdata(&z->dev);
185 
186 	/* allocate host */
187 	host = ata_host_alloc(&z->dev, nr_ports);
188 	if (type == BOARD_XSURF)
189 		dev_set_drvdata(&z->dev, old_drvdata);
190 	if (!host)
191 		return -ENXIO;
192 
193 	buddha_board = ZTWO_VADDR(board);
194 
195 	/* enable the board IRQ on Buddha/Catweasel */
196 	if (type != BOARD_XSURF)
197 		z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
198 
199 	for (i = 0; i < nr_ports; i++) {
200 		struct ata_port *ap = host->ports[i];
201 		void __iomem *base, *irqport;
202 		unsigned long ctl = 0;
203 
204 		if (type != BOARD_XSURF) {
205 			ap->ops = &pata_buddha_ops;
206 			base = buddha_board + buddha_bases[i];
207 			ctl = BUDDHA_CONTROL;
208 			irqport = buddha_board + BUDDHA_IRQ + i * 0x40;
209 		} else {
210 			ap->ops = &pata_xsurf_ops;
211 			base = buddha_board + xsurf_bases[i];
212 			/* X-Surf has no CS1* (Control/AltStat) */
213 			irqport = buddha_board + XSURF_IRQ;
214 		}
215 
216 		ap->pio_mask = ATA_PIO4;
217 		ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
218 
219 		ap->ioaddr.data_addr		= base;
220 		ap->ioaddr.error_addr		= base + 2 + 1 * 4;
221 		ap->ioaddr.feature_addr		= base + 2 + 1 * 4;
222 		ap->ioaddr.nsect_addr		= base + 2 + 2 * 4;
223 		ap->ioaddr.lbal_addr		= base + 2 + 3 * 4;
224 		ap->ioaddr.lbam_addr		= base + 2 + 4 * 4;
225 		ap->ioaddr.lbah_addr		= base + 2 + 5 * 4;
226 		ap->ioaddr.device_addr		= base + 2 + 6 * 4;
227 		ap->ioaddr.status_addr		= base + 2 + 7 * 4;
228 		ap->ioaddr.command_addr		= base + 2 + 7 * 4;
229 
230 		if (ctl) {
231 			ap->ioaddr.altstatus_addr = base + ctl;
232 			ap->ioaddr.ctl_addr	  = base + ctl;
233 		}
234 
235 		ap->private_data = (void *)irqport;
236 
237 		ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
238 			      ctl ? board + buddha_bases[i] + ctl : 0);
239 	}
240 
241 	ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
242 			  IRQF_SHARED, &pata_buddha_sht);
243 
244 	return 0;
245 }
246 
247 static void pata_buddha_remove(struct zorro_dev *z)
248 {
249 	struct ata_host *host = dev_get_drvdata(&z->dev);
250 
251 	ata_host_detach(host);
252 }
253 
254 static const struct zorro_device_id pata_buddha_zorro_tbl[] = {
255 	{ ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA, BOARD_BUDDHA},
256 	{ ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL, BOARD_CATWEASEL},
257 	{ 0 }
258 };
259 MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
260 
261 static struct zorro_driver pata_buddha_driver = {
262 	.name           = "pata_buddha",
263 	.id_table       = pata_buddha_zorro_tbl,
264 	.probe          = pata_buddha_probe,
265 	.remove         = pata_buddha_remove,
266 };
267 
268 /*
269  * We cannot have a modalias for X-Surf boards, as it competes with the
270  * zorro8390 network driver. As a stopgap measure until we have proper
271  * MFD support for this board, we manually attach to it late after Zorro
272  * has enumerated its boards.
273  */
274 static int __init pata_buddha_late_init(void)
275 {
276 	struct zorro_dev *z = NULL;
277 
278 	/* Auto-bind to regular boards */
279 	zorro_register_driver(&pata_buddha_driver);
280 
281 	/* Manually bind to all X-Surf boards */
282 	while ((z = zorro_find_device(ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, z))) {
283 		static struct zorro_device_id xsurf_ent = {
284 			ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, BOARD_XSURF
285 		};
286 
287 		pata_buddha_probe(z, &xsurf_ent);
288 	}
289 
290 	return 0;
291 }
292 late_initcall(pata_buddha_late_init);
293 
294 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
295 MODULE_DESCRIPTION("low-level driver for Buddha/Catweasel/X-Surf PATA");
296 MODULE_LICENSE("GPL v2");
297 MODULE_VERSION(DRV_VERSION);
298