1 /*
2         epat.c  (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                             Under the terms of the GNU General Public License.
4 
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.
9 
10 */
11 
12 /* Changes:
13 
14         1.01    GRG 1998.05.06 init_proto, release_proto
15         1.02    Joshua b. Jore CPP(renamed), epat_connect, epat_disconnect
16 
17 */
18 
19 #define EPAT_VERSION      "1.02"
20 
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/wait.h>
26 #include <asm/io.h>
27 
28 #include "paride.h"
29 
30 #define j44(a,b)		(((a>>4)&0x0f)+(b&0xf0))
31 #define j53(a,b)		(((a>>3)&0x1f)+((b<<4)&0xe0))
32 
33 /* cont =  0   IDE register file
34    cont =  1   IDE control registers
35    cont =  2   internal EPAT registers
36 */
37 
38 static int cont_map[3] = { 0x18, 0x10, 0 };
39 
epat_write_regr(PIA * pi,int cont,int regr,int val)40 static void epat_write_regr( PIA *pi, int cont, int regr, int val)
41 
42 {	int r;
43 
44 	r = regr + cont_map[cont];
45 
46 	switch (pi->mode) {
47 
48 	case 0:
49 	case 1:
50 	case 2:	w0(0x60+r); w2(1); w0(val); w2(4);
51 		break;
52 
53 	case 3:
54 	case 4:
55 	case 5: w3(0x40+r); w4(val);
56 		break;
57 
58 	}
59 }
60 
epat_read_regr(PIA * pi,int cont,int regr)61 static int epat_read_regr( PIA *pi, int cont, int regr )
62 
63 {	int  a, b, r;
64 
65 	r = regr + cont_map[cont];
66 
67 	switch (pi->mode) {
68 
69 	case 0:	w0(r); w2(1); w2(3);
70 		a = r1(); w2(4); b = r1();
71 		return j44(a,b);
72 
73 	case 1: w0(0x40+r); w2(1); w2(4);
74 		a = r1(); b = r2(); w0(0xff);
75 		return j53(a,b);
76 
77 	case 2: w0(0x20+r); w2(1); w2(0x25);
78 		a = r0(); w2(4);
79 		return a;
80 
81 	case 3:
82 	case 4:
83 	case 5: w3(r); w2(0x24); a = r4(); w2(4);
84 		return a;
85 
86 	}
87 	return -1;	/* never gets here */
88 }
89 
epat_read_block(PIA * pi,char * buf,int count)90 static void epat_read_block( PIA *pi, char * buf, int count )
91 
92 {	int  k, ph, a, b;
93 
94 	switch (pi->mode) {
95 
96 	case 0:	w0(7); w2(1); w2(3); w0(0xff);
97 		ph = 0;
98 		for(k=0;k<count;k++) {
99 			if (k == count-1) w0(0xfd);
100 			w2(6+ph); a = r1();
101 			if (a & 8) b = a;
102 			  else { w2(4+ph); b = r1(); }
103 			buf[k] = j44(a,b);
104 			ph =  1 - ph;
105 		}
106 		w0(0); w2(4);
107 		break;
108 
109 	case 1: w0(0x47); w2(1); w2(5); w0(0xff);
110 		ph = 0;
111 		for(k=0;k<count;k++) {
112 			if (k == count-1) w0(0xfd);
113 			w2(4+ph);
114 			a = r1(); b = r2();
115 			buf[k] = j53(a,b);
116 			ph = 1 - ph;
117 		}
118 		w0(0); w2(4);
119 		break;
120 
121 	case 2: w0(0x27); w2(1); w2(0x25); w0(0);
122 		ph = 0;
123 		for(k=0;k<count-1;k++) {
124 			w2(0x24+ph);
125 			buf[k] = r0();
126 			ph = 1 - ph;
127 		}
128 		w2(0x26); w2(0x27); buf[count-1] = r0();
129 		w2(0x25); w2(4);
130 		break;
131 
132 	case 3: w3(0x80); w2(0x24);
133 		for(k=0;k<count-1;k++) buf[k] = r4();
134 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
135 		w2(4);
136 		break;
137 
138 	case 4: w3(0x80); w2(0x24);
139 		for(k=0;k<(count/2)-1;k++) ((u16 *)buf)[k] = r4w();
140 		buf[count-2] = r4();
141 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
142 		w2(4);
143 		break;
144 
145 	case 5: w3(0x80); w2(0x24);
146 		for(k=0;k<(count/4)-1;k++) ((u32 *)buf)[k] = r4l();
147 		for(k=count-4;k<count-1;k++) buf[k] = r4();
148 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
149 		w2(4);
150 		break;
151 
152 	}
153 }
154 
epat_write_block(PIA * pi,char * buf,int count)155 static void epat_write_block( PIA *pi, char * buf, int count )
156 
157 {	int ph, k;
158 
159 	switch (pi->mode) {
160 
161 	case 0:
162 	case 1:
163 	case 2: w0(0x67); w2(1); w2(5);
164 		ph = 0;
165 		for(k=0;k<count;k++) {
166 		  	w0(buf[k]);
167 			w2(4+ph);
168 			ph = 1 - ph;
169 		}
170 		w2(7); w2(4);
171 		break;
172 
173 	case 3: w3(0xc0);
174 		for(k=0;k<count;k++) w4(buf[k]);
175 		w2(4);
176 		break;
177 
178 	case 4: w3(0xc0);
179 		for(k=0;k<(count/2);k++) w4w(((u16 *)buf)[k]);
180 		w2(4);
181 		break;
182 
183 	case 5: w3(0xc0);
184 		for(k=0;k<(count/4);k++) w4l(((u32 *)buf)[k]);
185 		w2(4);
186 		break;
187 
188 	}
189 }
190 
191 /* these macros access the EPAT registers in native addressing */
192 
193 #define	WR(r,v)		epat_write_regr(pi,2,r,v)
194 #define	RR(r)		(epat_read_regr(pi,2,r))
195 
196 /* and these access the IDE task file */
197 
198 #define WRi(r,v)         epat_write_regr(pi,0,r,v)
199 #define RRi(r)           (epat_read_regr(pi,0,r))
200 
201 /* FIXME:  the CPP stuff should be fixed to handle multiple EPATs on a chain */
202 
203 #define CPP(x) 	w2(4);w0(0x22);w0(0xaa);w0(0x55);w0(0);w0(0xff);\
204                 w0(0x87);w0(0x78);w0(x);w2(4);w2(5);w2(4);w0(0xff);
205 
epat_connect(PIA * pi)206 static void epat_connect ( PIA *pi )
207 
208 {       pi->saved_r0 = r0();
209         pi->saved_r2 = r2();
210 
211 #ifdef CONFIG_PARIDE_EPATC8
212  	/* Initialize the chip */
213         CPP(0);CPP(0x40);CPP(0xe0);
214         w0(0);w2(1);w2(4);
215         WR(0x8,0x12);WR(0xc,0x14);WR(0x12,0x10);
216         WR(0xe,0xf);WR(0xf,4);
217      /* WR(0xe,0xa);WR(0xf,4); */
218         WR(0xe,0xd);WR(0xf,0);
219      /* CPP(0x30); */
220 
221         /* Connect to the chip */
222 	CPP(0xe0);
223         w0(0);w2(1);w2(4); /* Idle into SPP */
224         if (pi->mode >= 3) {
225           w0(0);w2(1);w2(4);w2(0xc);
226           /* Request EPP */
227           w0(0x40);w2(6);w2(7);w2(4);w2(0xc);w2(4);
228         }
229 #else
230  	CPP(0); CPP(0xe0);
231 	w0(0); w2(1); w2(4);
232 	if (pi->mode >= 3) {
233 		w0(0); w2(1); w2(4); w2(0xc);
234 		w0(0x40); w2(6); w2(7); w2(4); w2(0xc); w2(4);
235 	}
236 	WR(8,0x10); WR(0xc,0x14); WR(0xa,0x38); WR(0x12,0x10);
237 #endif
238 }
239 
epat_disconnect(PIA * pi)240 static void epat_disconnect (PIA *pi)
241 {	CPP(0x30);
242 	w0(pi->saved_r0);
243 	w2(pi->saved_r2);
244 }
245 
epat_test_proto(PIA * pi,char * scratch,int verbose)246 static int epat_test_proto( PIA *pi, char * scratch, int verbose )
247 
248 {       int     k, j, f, cc;
249 	int	e[2] = {0,0};
250 
251         epat_connect(pi);
252 	cc = RR(0xd);
253 	epat_disconnect(pi);
254 
255 	epat_connect(pi);
256 	for (j=0;j<2;j++) {
257   	    WRi(6,0xa0+j*0x10);
258             for (k=0;k<256;k++) {
259                 WRi(2,k^0xaa);
260                 WRi(3,k^0x55);
261                 if (RRi(2) != (k^0xaa)) e[j]++;
262                 }
263 	    }
264         epat_disconnect(pi);
265 
266         f = 0;
267         epat_connect(pi);
268         WR(0x13,1); WR(0x13,0); WR(0xa,0x11);
269         epat_read_block(pi,scratch,512);
270 
271         for (k=0;k<256;k++) {
272             if ((scratch[2*k] & 0xff) != k) f++;
273             if ((scratch[2*k+1] & 0xff) != (0xff-k)) f++;
274         }
275         epat_disconnect(pi);
276 
277         if (verbose)  {
278             printk("%s: epat: port 0x%x, mode %d, ccr %x, test=(%d,%d,%d)\n",
279 		   pi->device,pi->port,pi->mode,cc,e[0],e[1],f);
280 	}
281 
282         return (e[0] && e[1]) || f;
283 }
284 
epat_log_adapter(PIA * pi,char * scratch,int verbose)285 static void epat_log_adapter( PIA *pi, char * scratch, int verbose )
286 
287 {	int	ver;
288         char    *mode_string[6] =
289 		   {"4-bit","5/3","8-bit","EPP-8","EPP-16","EPP-32"};
290 
291 	epat_connect(pi);
292 	WR(0xa,0x38);		/* read the version code */
293         ver = RR(0xb);
294         epat_disconnect(pi);
295 
296 	printk("%s: epat %s, Shuttle EPAT chip %x at 0x%x, ",
297 		pi->device,EPAT_VERSION,ver,pi->port);
298 	printk("mode %d (%s), delay %d\n",pi->mode,
299 		mode_string[pi->mode],pi->delay);
300 
301 }
302 
epat_init_proto(PIA * pi)303 static void epat_init_proto( PIA *pi)
304 
305 {  	MOD_INC_USE_COUNT;
306 }
307 
epat_release_proto(PIA * pi)308 static void epat_release_proto( PIA *pi)
309 
310 {	MOD_DEC_USE_COUNT;
311 }
312 
313 struct pi_protocol epat = {"epat",0,6,3,1,1,
314 			   epat_write_regr,
315 			   epat_read_regr,
316 			   epat_write_block,
317 			   epat_read_block,
318 			   epat_connect,
319 			   epat_disconnect,
320 			   0,
321 			   0,
322 			   epat_test_proto,
323 			   epat_log_adapter,
324 			   epat_init_proto,
325 			   epat_release_proto
326 			  };
327 
328 
329 #ifdef MODULE
330 
init_module(void)331 int	init_module(void)
332 
333 {	return pi_register( &epat) - 1;
334 }
335 
cleanup_module(void)336 void	cleanup_module(void)
337 
338 {	pi_unregister( &epat);
339 }
340 
341 #endif
342 
343 /* end of epat.c */
344 MODULE_LICENSE("GPL");
345