1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (c) 1997-1998 Grant R. Guenther <grant@torque.net>
5 * This is the low level protocol driver for the EPAT parallel
6 * to IDE adapter from Shuttle Technologies. This adapter is
7 * used in many popular parallel port disk products such as the
8 * SyQuest EZ drives, the Avatar Shark and the Imation SuperDisk.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/wait.h>
18 #include "pata_parport.h"
20 #define j44(a, b) (((a >> 4) & 0x0f) + (b & 0xf0))
21 #define j53(a, b) (((a >> 3) & 0x1f) + ((b << 4) & 0xe0))
25 module_param(epatc8, int, 0);
26 MODULE_PARM_DESC(epatc8,
27 "support for the Shuttle EP1284 chip, "
28 "used in any recent Imation SuperDisk (LS-120) drive.");
31 * cont = 0 IDE register file
32 * cont = 1 IDE control registers
33 * cont = 2 internal EPAT registers
35 static int cont_map[3] = { 0x18, 0x10, 0 };
37 static void epat_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
39 int r = regr + cont_map[cont];
45 w0(0x60+r); w2(1); w0(val); w2(4);
55 static int epat_read_regr(struct pi_adapter *pi, int cont, int regr)
59 r = regr + cont_map[cont];
65 a = r1(); w2(4); b = r1();
68 w0(0x40+r); w2(1); w2(4);
69 a = r1(); b = r2(); w0(0xff);
72 w0(0x20+r); w2(1); w2(0x25);
78 w3(r); w2(0x24); a = r4(); w2(4);
82 return -1; /* never gets here */
85 static void epat_read_block(struct pi_adapter *pi, char *buf, int count)
92 w0(7); w2(1); w2(3); w0(0xff);
94 for (k = 0; k < count; k++) {
110 w0(0x47); w2(1); w2(5); w0(0xff);
112 for (k = 0; k < count; k++) {
124 w0(0x27); w2(1); w2(0x25); w0(0);
126 for (k = 0; k < count - 1; k++) {
132 buf[count - 1] = r0();
138 for (k = 0; k < count - 1; k++)
140 w2(4); w3(0xa0); w2(0x24);
141 buf[count - 1] = r4();
147 for (k = 0; k < count / 2 - 1; k++)
148 ((u16 *)buf)[k] = r4w();
149 buf[count - 2] = r4();
150 w2(4); w3(0xa0); w2(0x24);
151 buf[count - 1] = r4();
157 for (k = 0; k < count / 4 - 1; k++)
158 ((u32 *)buf)[k] = r4l();
159 for (k = count - 4; k < count - 1; k++)
161 w2(4); w3(0xa0); w2(0x24);
162 buf[count - 1] = r4();
168 static void epat_write_block(struct pi_adapter *pi, char *buf, int count)
176 w0(0x67); w2(1); w2(5);
178 for (k = 0; k < count; k++) {
187 for (k = 0; k < count; k++)
193 for (k = 0; k < count / 2; k++)
194 w4w(((u16 *)buf)[k]);
199 for (k = 0; k < count / 4; k++)
200 w4l(((u32 *)buf)[k]);
206 /* these macros access the EPAT registers in native addressing */
208 #define WR(r, v) epat_write_regr(pi, 2, r, v)
209 #define RR(r) epat_read_regr(pi, 2, r)
211 /* and these access the IDE task file */
213 #define WRi(r, v) epat_write_regr(pi, 0, r, v)
214 #define RRi(r) epat_read_regr(pi, 0, r)
216 /* FIXME: the CPP stuff should be fixed to handle multiple EPATs on a chain */
220 w2(4); w0(0x22); w0(0xaa); \
221 w0(0x55); w0(0); w0(0xff); \
222 w0(0x87); w0(0x78); w0(x); \
223 w2(4); w2(5); w2(4); w0(0xff); \
226 static void epat_connect(struct pi_adapter *pi)
231 /* Initialize the chip */
235 CPP(0x40); CPP(0xe0);
242 /* WR(0xe,0xa);WR(0xf,4); */
248 /* Connect to the chip */
250 w0(0); w2(1); w2(4); /* Idle into SPP */
252 w0(0); w2(1); w2(4); w2(0xc);
254 w0(0x40); w2(6); w2(7); w2(4); w2(0xc); w2(4);
265 static void epat_disconnect(struct pi_adapter *pi)
272 static int epat_test_proto(struct pi_adapter *pi)
284 WRi(6, 0xa0 + j * 0x10);
285 for (k = 0; k < 256; k++) {
288 if (RRi(2) != (k ^ 0xaa))
296 WR(0x13, 1); WR(0x13, 0); WR(0xa, 0x11);
297 epat_read_block(pi, scratch, 512);
299 for (k = 0; k < 256; k++) {
300 if ((scratch[2 * k] & 0xff) != k)
302 if ((scratch[2 * k + 1] & 0xff) != 0xff - k)
308 "epat: port 0x%x, mode %d, ccr %x, test=(%d,%d,%d)\n",
309 pi->port, pi->mode, cc, e[0], e[1], f);
311 return (e[0] && e[1]) || f;
314 static void epat_log_adapter(struct pi_adapter *pi)
317 char *mode_string[6] =
318 { "4-bit", "5/3", "8-bit", "EPP-8", "EPP-16", "EPP-32" };
321 WR(0xa, 0x38); /* read the version code */
326 "Shuttle EPAT chip %x at 0x%x, mode %d (%s), delay %d\n",
327 ver, pi->port, pi->mode, mode_string[pi->mode], pi->delay);
330 static struct pi_protocol epat = {
331 .owner = THIS_MODULE,
337 .write_regr = epat_write_regr,
338 .read_regr = epat_read_regr,
339 .write_block = epat_write_block,
340 .read_block = epat_read_block,
341 .connect = epat_connect,
342 .disconnect = epat_disconnect,
343 .test_proto = epat_test_proto,
344 .log_adapter = epat_log_adapter,
347 static int __init epat_init(void)
349 #ifdef CONFIG_PATA_PARPORT_EPATC8
352 return pata_parport_register_driver(&epat);
355 static void __exit epat_exit(void)
357 pata_parport_unregister_driver(&epat);
360 MODULE_LICENSE("GPL");
361 module_init(epat_init)
362 module_exit(epat_exit)