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