2 epat.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
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.
14 1.01 GRG 1998.05.06 init_proto, release_proto
15 1.02 Joshua b. Jore CPP(renamed), epat_connect, epat_disconnect
19 #define EPAT_VERSION "1.02"
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/wait.h>
31 #define j44(a,b) (((a>>4)&0x0f)+(b&0xf0))
32 #define j53(a,b) (((a>>3)&0x1f)+((b<<4)&0xe0))
34 /* cont = 0 IDE register file
35 cont = 1 IDE control registers
36 cont = 2 internal EPAT registers
39 static int cont_map[3] = { 0x18, 0x10, 0 };
41 static void epat_write_regr( PIA *pi, int cont, int regr, int val)
45 r = regr + cont_map[cont];
51 case 2: w0(0x60+r); w2(1); w0(val); w2(4);
56 case 5: w3(0x40+r); w4(val);
62 static int epat_read_regr( PIA *pi, int cont, int regr )
66 r = regr + cont_map[cont];
70 case 0: w0(r); w2(1); w2(3);
71 a = r1(); w2(4); b = r1();
74 case 1: w0(0x40+r); w2(1); w2(4);
75 a = r1(); b = r2(); w0(0xff);
78 case 2: w0(0x20+r); w2(1); w2(0x25);
84 case 5: w3(r); w2(0x24); a = r4(); w2(4);
88 return -1; /* never gets here */
91 static void epat_read_block( PIA *pi, char * buf, int count )
97 case 0: w0(7); w2(1); w2(3); w0(0xff);
99 for(k=0;k<count;k++) {
100 if (k == count-1) w0(0xfd);
103 else { w2(4+ph); b = r1(); }
110 case 1: w0(0x47); w2(1); w2(5); w0(0xff);
112 for(k=0;k<count;k++) {
113 if (k == count-1) w0(0xfd);
122 case 2: w0(0x27); w2(1); w2(0x25); w0(0);
124 for(k=0;k<count-1;k++) {
129 w2(0x26); w2(0x27); buf[count-1] = r0();
133 case 3: w3(0x80); w2(0x24);
134 for(k=0;k<count-1;k++) buf[k] = r4();
135 w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
139 case 4: w3(0x80); w2(0x24);
140 for(k=0;k<(count/2)-1;k++) ((u16 *)buf)[k] = r4w();
142 w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
146 case 5: w3(0x80); w2(0x24);
147 for(k=0;k<(count/4)-1;k++) ((u32 *)buf)[k] = r4l();
148 for(k=count-4;k<count-1;k++) buf[k] = r4();
149 w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
156 static void epat_write_block( PIA *pi, char * buf, int count )
164 case 2: w0(0x67); w2(1); w2(5);
166 for(k=0;k<count;k++) {
175 for(k=0;k<count;k++) w4(buf[k]);
180 for(k=0;k<(count/2);k++) w4w(((u16 *)buf)[k]);
185 for(k=0;k<(count/4);k++) w4l(((u32 *)buf)[k]);
192 /* these macros access the EPAT registers in native addressing */
194 #define WR(r,v) epat_write_regr(pi,2,r,v)
195 #define RR(r) (epat_read_regr(pi,2,r))
197 /* and these access the IDE task file */
199 #define WRi(r,v) epat_write_regr(pi,0,r,v)
200 #define RRi(r) (epat_read_regr(pi,0,r))
202 /* FIXME: the CPP stuff should be fixed to handle multiple EPATs on a chain */
204 #define CPP(x) w2(4);w0(0x22);w0(0xaa);w0(0x55);w0(0);w0(0xff);\
205 w0(0x87);w0(0x78);w0(x);w2(4);w2(5);w2(4);w0(0xff);
207 static void epat_connect ( PIA *pi )
209 { pi->saved_r0 = r0();
212 #ifdef CONFIG_PARIDE_EPATC8
213 /* Initialize the chip */
214 CPP(0);CPP(0x40);CPP(0xe0);
216 WR(0x8,0x12);WR(0xc,0x14);WR(0x12,0x10);
217 WR(0xe,0xf);WR(0xf,4);
218 /* WR(0xe,0xa);WR(0xf,4); */
219 WR(0xe,0xd);WR(0xf,0);
222 /* Connect to the chip */
224 w0(0);w2(1);w2(4); /* Idle into SPP */
226 w0(0);w2(1);w2(4);w2(0xc);
228 w0(0x40);w2(6);w2(7);w2(4);w2(0xc);w2(4);
234 w0(0); w2(1); w2(4); w2(0xc);
235 w0(0x40); w2(6); w2(7); w2(4); w2(0xc); w2(4);
237 WR(8,0x10); WR(0xc,0x14); WR(0xa,0x38); WR(0x12,0x10);
241 static void epat_disconnect (PIA *pi)
247 static int epat_test_proto( PIA *pi, char * scratch, int verbose )
259 for (k=0;k<256;k++) {
262 if (RRi(2) != (k^0xaa)) e[j]++;
269 WR(0x13,1); WR(0x13,0); WR(0xa,0x11);
270 epat_read_block(pi,scratch,512);
272 for (k=0;k<256;k++) {
273 if ((scratch[2*k] & 0xff) != k) f++;
274 if ((scratch[2*k+1] & 0xff) != (0xff-k)) f++;
279 printk("%s: epat: port 0x%x, mode %d, ccr %x, test=(%d,%d,%d)\n",
280 pi->device,pi->port,pi->mode,cc,e[0],e[1],f);
283 return (e[0] && e[1]) || f;
286 static void epat_log_adapter( PIA *pi, char * scratch, int verbose )
289 char *mode_string[6] =
290 {"4-bit","5/3","8-bit","EPP-8","EPP-16","EPP-32"};
293 WR(0xa,0x38); /* read the version code */
297 printk("%s: epat %s, Shuttle EPAT chip %x at 0x%x, ",
298 pi->device,EPAT_VERSION,ver,pi->port);
299 printk("mode %d (%s), delay %d\n",pi->mode,
300 mode_string[pi->mode],pi->delay);
304 static struct pi_protocol epat = {
305 .owner = THIS_MODULE,
311 .write_regr = epat_write_regr,
312 .read_regr = epat_read_regr,
313 .write_block = epat_write_block,
314 .read_block = epat_read_block,
315 .connect = epat_connect,
316 .disconnect = epat_disconnect,
317 .test_proto = epat_test_proto,
318 .log_adapter = epat_log_adapter,
321 static int __init epat_init(void)
323 return pi_register(&epat)-1;
326 static void __exit epat_exit(void)
328 pi_unregister(&epat);
331 MODULE_LICENSE("GPL");
332 module_init(epat_init)
333 module_exit(epat_exit)