1/*
2 *  Amiga Buddha, Catweasel and X-Surf IDE Driver
3 *
4 *	Copyright (C) 1997, 2001 by Geert Uytterhoeven and others
5 *
6 *  This driver was written based on the specifications in README.buddha and
7 *  the X-Surf info from Inside_XSurf.txt available at
8 *  http://www.jschoenfeld.com
9 *
10 *  This file is subject to the terms and conditions of the GNU General Public
11 *  License.  See the file COPYING in the main directory of this archive for
12 *  more details.
13 *
14 *  TODO:
15 *    - test it :-)
16 *    - tune the timings using the speed-register
17 */
18
19#include <linux/types.h>
20#include <linux/mm.h>
21#include <linux/interrupt.h>
22#include <linux/blkdev.h>
23#include <linux/zorro.h>
24#include <linux/ide.h>
25#include <linux/init.h>
26#include <linux/module.h>
27
28#include <asm/amigahw.h>
29#include <asm/amigaints.h>
30
31
32    /*
33     *  The Buddha has 2 IDE interfaces, the Catweasel has 3, X-Surf has 2
34     */
35
36#define BUDDHA_NUM_HWIFS	2
37#define CATWEASEL_NUM_HWIFS	3
38#define XSURF_NUM_HWIFS         2
39
40#define MAX_NUM_HWIFS		3
41
42    /*
43     *  Bases of the IDE interfaces (relative to the board address)
44     */
45
46#define BUDDHA_BASE1	0x800
47#define BUDDHA_BASE2	0xa00
48#define BUDDHA_BASE3	0xc00
49
50#define XSURF_BASE1     0xb000 /* 2.5" Interface */
51#define XSURF_BASE2     0xd000 /* 3.5" Interface */
52
53static u_int buddha_bases[CATWEASEL_NUM_HWIFS] __initdata = {
54    BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
55};
56
57static u_int xsurf_bases[XSURF_NUM_HWIFS] __initdata = {
58     XSURF_BASE1, XSURF_BASE2
59};
60
61    /*
62     *  Offsets from one of the above bases
63     */
64
65#define BUDDHA_CONTROL	0x11a
66
67    /*
68     *  Other registers
69     */
70
71#define BUDDHA_IRQ1	0xf00		/* MSB = 1, Harddisk is source of */
72#define BUDDHA_IRQ2	0xf40		/* interrupt */
73#define BUDDHA_IRQ3	0xf80
74
75#define XSURF_IRQ1      0x7e
76#define XSURF_IRQ2      0x7e
77
78static int buddha_irqports[CATWEASEL_NUM_HWIFS] __initdata = {
79    BUDDHA_IRQ1, BUDDHA_IRQ2, BUDDHA_IRQ3
80};
81
82static int xsurf_irqports[XSURF_NUM_HWIFS] __initdata = {
83    XSURF_IRQ1, XSURF_IRQ2
84};
85
86#define BUDDHA_IRQ_MR	0xfc0		/* master interrupt enable */
87
88
89    /*
90     *  Board information
91     */
92
93typedef enum BuddhaType_Enum {
94    BOARD_BUDDHA, BOARD_CATWEASEL, BOARD_XSURF
95} BuddhaType;
96
97static const char *buddha_board_name[] = { "Buddha", "Catweasel", "X-Surf" };
98
99    /*
100     *  Check and acknowledge the interrupt status
101     */
102
103static int buddha_test_irq(ide_hwif_t *hwif)
104{
105    unsigned char ch;
106
107    ch = z_readb(hwif->io_ports.irq_addr);
108    if (!(ch & 0x80))
109	    return 0;
110    return 1;
111}
112
113static void xsurf_clear_irq(ide_drive_t *drive)
114{
115    /*
116     * X-Surf needs 0 written to IRQ register to ensure ISA bit A11 stays at 0
117     */
118    z_writeb(0, drive->hwif->io_ports.irq_addr);
119}
120
121static void __init buddha_setup_ports(struct ide_hw *hw, unsigned long base,
122				      unsigned long ctl, unsigned long irq_port)
123{
124	int i;
125
126	memset(hw, 0, sizeof(*hw));
127
128	hw->io_ports.data_addr = base;
129
130	for (i = 1; i < 8; i++)
131		hw->io_ports_array[i] = base + 2 + i * 4;
132
133	hw->io_ports.ctl_addr = ctl;
134	hw->io_ports.irq_addr = irq_port;
135
136	hw->irq = IRQ_AMIGA_PORTS;
137}
138
139static const struct ide_port_ops buddha_port_ops = {
140	.test_irq		= buddha_test_irq,
141};
142
143static const struct ide_port_ops xsurf_port_ops = {
144	.clear_irq		= xsurf_clear_irq,
145	.test_irq		= buddha_test_irq,
146};
147
148static const struct ide_port_info buddha_port_info = {
149	.port_ops		= &buddha_port_ops,
150	.host_flags		= IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
151	.irq_flags		= IRQF_SHARED,
152	.chipset		= ide_generic,
153};
154
155    /*
156     *  Probe for a Buddha or Catweasel IDE interface
157     */
158
159static int __init buddha_init(void)
160{
161	struct zorro_dev *z = NULL;
162	u_long buddha_board = 0;
163	BuddhaType type;
164	int buddha_num_hwifs, i;
165
166	while ((z = zorro_find_device(ZORRO_WILDCARD, z))) {
167		unsigned long board;
168		struct ide_hw hw[MAX_NUM_HWIFS], *hws[MAX_NUM_HWIFS];
169		struct ide_port_info d = buddha_port_info;
170
171		if (z->id == ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA) {
172			buddha_num_hwifs = BUDDHA_NUM_HWIFS;
173			type=BOARD_BUDDHA;
174		} else if (z->id == ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL) {
175			buddha_num_hwifs = CATWEASEL_NUM_HWIFS;
176			type=BOARD_CATWEASEL;
177		} else if (z->id == ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF) {
178			buddha_num_hwifs = XSURF_NUM_HWIFS;
179			type=BOARD_XSURF;
180			d.port_ops = &xsurf_port_ops;
181		} else
182			continue;
183
184		board = z->resource.start;
185
186		if(type != BOARD_XSURF) {
187			if (!request_mem_region(board+BUDDHA_BASE1, 0x800, "IDE"))
188				continue;
189		} else {
190			if (!request_mem_region(board+XSURF_BASE1, 0x1000, "IDE"))
191				continue;
192			if (!request_mem_region(board+XSURF_BASE2, 0x1000, "IDE"))
193				goto fail_base2;
194			if (!request_mem_region(board+XSURF_IRQ1, 0x8, "IDE")) {
195				release_mem_region(board+XSURF_BASE2, 0x1000);
196fail_base2:
197				release_mem_region(board+XSURF_BASE1, 0x1000);
198				continue;
199			}
200		}
201		buddha_board = (unsigned long)ZTWO_VADDR(board);
202
203		/* write to BUDDHA_IRQ_MR to enable the board IRQ */
204		/* X-Surf doesn't have this.  IRQs are always on */
205		if (type != BOARD_XSURF)
206			z_writeb(0, buddha_board+BUDDHA_IRQ_MR);
207
208		printk(KERN_INFO "ide: %s IDE controller\n",
209				 buddha_board_name[type]);
210
211		for (i = 0; i < buddha_num_hwifs; i++) {
212			unsigned long base, ctl, irq_port;
213
214			if (type != BOARD_XSURF) {
215				base = buddha_board + buddha_bases[i];
216				ctl = base + BUDDHA_CONTROL;
217				irq_port = buddha_board + buddha_irqports[i];
218			} else {
219				base = buddha_board + xsurf_bases[i];
220				/* X-Surf has no CS1* (Control/AltStat) */
221				ctl = 0;
222				irq_port = buddha_board + xsurf_irqports[i];
223			}
224
225			buddha_setup_ports(&hw[i], base, ctl, irq_port);
226
227			hws[i] = &hw[i];
228		}
229
230		ide_host_add(&d, hws, i, NULL);
231	}
232
233	return 0;
234}
235
236module_init(buddha_init);
237
238MODULE_LICENSE("GPL");
239