xref: /openbmc/linux/drivers/ata/pata_buddha.c (revision 65844828)
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/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>
27 
28 #include <asm/amigahw.h>
29 #include <asm/amigaints.h>
30 #include <asm/ide.h>
31 #include <asm/setup.h>
32 
33 #define DRV_NAME "pata_buddha"
34 #define DRV_VERSION "0.1.1"
35 
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 */
45 
46 enum {
47 	BOARD_BUDDHA = 0,
48 	BOARD_CATWEASEL,
49 	BOARD_XSURF
50 };
51 
52 static unsigned int buddha_bases[3] = {
53 	BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
54 };
55 
56 static unsigned int xsurf_bases[2] = {
57 	XSURF_BASE1, XSURF_BASE2
58 };
59 
60 static struct scsi_host_template pata_buddha_sht = {
61 	ATA_PIO_SHT(DRV_NAME),
62 };
63 
64 /* FIXME: is this needed? */
65 static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
66 					 unsigned char *buf,
67 					 unsigned int buflen, int rw)
68 {
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;
73 
74 	/* Transfer multiple of 2 bytes */
75 	if (rw == READ)
76 		raw_insw((u16 *)data_addr, (u16 *)buf, words);
77 	else
78 		raw_outsw((u16 *)data_addr, (u16 *)buf, words);
79 
80 	/* Transfer trailing byte, if any. */
81 	if (unlikely(buflen & 0x01)) {
82 		unsigned char pad[2] = { };
83 
84 		/* Point buf to the tail of buffer */
85 		buf += buflen - 1;
86 
87 		if (rw == READ) {
88 			raw_insw((u16 *)data_addr, (u16 *)pad, 1);
89 			*buf = pad[0];
90 		} else {
91 			pad[0] = *buf;
92 			raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
93 		}
94 		words++;
95 	}
96 
97 	return words << 1;
98 }
99 
100 /*
101  * Provide our own set_mode() as we don't want to change anything that has
102  * already been configured..
103  */
104 static int pata_buddha_set_mode(struct ata_link *link,
105 				struct ata_device **unused)
106 {
107 	struct ata_device *dev;
108 
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");
115 	}
116 	return 0;
117 }
118 
119 static bool pata_buddha_irq_check(struct ata_port *ap)
120 {
121 	u8 ch;
122 
123 	ch = z_readb((unsigned long)ap->private_data);
124 
125 	return !!(ch & 0x80);
126 }
127 
128 static void pata_xsurf_irq_clear(struct ata_port *ap)
129 {
130 	z_writeb(0, (unsigned long)ap->private_data);
131 }
132 
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,
139 };
140 
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,
148 };
149 
150 static int pata_buddha_probe(struct zorro_dev *z,
151 			     const struct zorro_device_id *ent)
152 {
153 	static const char * const board_name[] = {
154 		"Buddha", "Catweasel", "X-Surf"
155 	};
156 	struct ata_host *host;
157 	void __iomem *buddha_board;
158 	unsigned long board;
159 	unsigned int type = ent->driver_data;
160 	unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
161 	void *old_drvdata;
162 	int i;
163 
164 	dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
165 
166 	board = z->resource.start;
167 
168 	if (type != BOARD_XSURF) {
169 		if (!devm_request_mem_region(&z->dev,
170 					     board + BUDDHA_BASE1,
171 					     0x800, DRV_NAME))
172 			return -ENXIO;
173 	} else {
174 		if (!devm_request_mem_region(&z->dev,
175 					     board + XSURF_BASE1,
176 					     0x1000, DRV_NAME))
177 			return -ENXIO;
178 		if (!devm_request_mem_region(&z->dev,
179 					     board + XSURF_BASE2,
180 					     0x1000, DRV_NAME)) {
181 		}
182 	}
183 
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);
187 
188 	/* allocate host */
189 	host = ata_host_alloc(&z->dev, nr_ports);
190 	if (type == BOARD_XSURF)
191 		dev_set_drvdata(&z->dev, old_drvdata);
192 	if (!host)
193 		return -ENXIO;
194 
195 	buddha_board = ZTWO_VADDR(board);
196 
197 	/* enable the board IRQ on Buddha/Catweasel */
198 	if (type != BOARD_XSURF)
199 		z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
200 
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;
205 
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;
211 		} else {
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;
216 		}
217 
218 		ap->pio_mask = ATA_PIO4;
219 		ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
220 
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;
231 
232 		if (ctl) {
233 			ap->ioaddr.altstatus_addr = base + ctl;
234 			ap->ioaddr.ctl_addr	  = base + ctl;
235 		}
236 
237 		ap->private_data = (void *)irqport;
238 
239 		ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
240 			      ctl ? board + buddha_bases[i] + ctl : 0);
241 	}
242 
243 	ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
244 			  IRQF_SHARED, &pata_buddha_sht);
245 
246 	return 0;
247 }
248 
249 static void pata_buddha_remove(struct zorro_dev *z)
250 {
251 	struct ata_host *host = dev_get_drvdata(&z->dev);
252 
253 	ata_host_detach(host);
254 }
255 
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},
259 	{ 0 }
260 };
261 MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
262 
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,
268 };
269 
270 /*
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.
275  */
276 static int __init pata_buddha_late_init(void)
277 {
278 	struct zorro_dev *z = NULL;
279 
280 	/* Auto-bind to regular boards */
281 	zorro_register_driver(&pata_buddha_driver);
282 
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
287 		};
288 
289 		pata_buddha_probe(z, &xsurf_ent);
290 	}
291 
292 	return 0;
293 }
294 late_initcall(pata_buddha_late_init);
295 
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);
300