ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / paride / epat.c
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/init.h>
23 #include <linux/delay.h>
24 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/wait.h>
27 #include <asm/io.h>
28
29 #include "paride.h"
30
31 #define j44(a,b)                (((a>>4)&0x0f)+(b&0xf0))
32 #define j53(a,b)                (((a>>3)&0x1f)+((b<<4)&0xe0))
33
34 /* cont =  0   IDE register file
35    cont =  1   IDE control registers
36    cont =  2   internal EPAT registers
37 */
38
39 static int cont_map[3] = { 0x18, 0x10, 0 };
40
41 static void epat_write_regr( PIA *pi, int cont, int regr, int val)
42
43 {       int r;
44
45         r = regr + cont_map[cont];
46
47         switch (pi->mode) {
48
49         case 0:
50         case 1:
51         case 2: w0(0x60+r); w2(1); w0(val); w2(4);
52                 break;
53
54         case 3:
55         case 4:
56         case 5: w3(0x40+r); w4(val);
57                 break;
58
59         }
60 }
61
62 static int epat_read_regr( PIA *pi, int cont, int regr )
63
64 {       int  a, b, r;
65
66         r = regr + cont_map[cont];
67
68         switch (pi->mode) {
69
70         case 0: w0(r); w2(1); w2(3); 
71                 a = r1(); w2(4); b = r1();
72                 return j44(a,b);
73
74         case 1: w0(0x40+r); w2(1); w2(4);
75                 a = r1(); b = r2(); w0(0xff);
76                 return j53(a,b);
77
78         case 2: w0(0x20+r); w2(1); w2(0x25);
79                 a = r0(); w2(4);
80                 return a;
81
82         case 3:
83         case 4:
84         case 5: w3(r); w2(0x24); a = r4(); w2(4);
85                 return a;
86
87         }
88         return -1;      /* never gets here */
89 }
90
91 static void epat_read_block( PIA *pi, char * buf, int count )
92
93 {       int  k, ph, a, b;
94
95         switch (pi->mode) {
96
97         case 0: w0(7); w2(1); w2(3); w0(0xff);
98                 ph = 0;
99                 for(k=0;k<count;k++) {
100                         if (k == count-1) w0(0xfd);
101                         w2(6+ph); a = r1();
102                         if (a & 8) b = a; 
103                           else { w2(4+ph); b = r1(); }
104                         buf[k] = j44(a,b);
105                         ph =  1 - ph;
106                 }
107                 w0(0); w2(4);
108                 break;
109
110         case 1: w0(0x47); w2(1); w2(5); w0(0xff);
111                 ph = 0;
112                 for(k=0;k<count;k++) {
113                         if (k == count-1) w0(0xfd); 
114                         w2(4+ph);
115                         a = r1(); b = r2();
116                         buf[k] = j53(a,b);
117                         ph = 1 - ph;
118                 }
119                 w0(0); w2(4);
120                 break;
121
122         case 2: w0(0x27); w2(1); w2(0x25); w0(0);
123                 ph = 0;
124                 for(k=0;k<count-1;k++) {
125                         w2(0x24+ph);
126                         buf[k] = r0();
127                         ph = 1 - ph;
128                 }
129                 w2(0x26); w2(0x27); buf[count-1] = r0(); 
130                 w2(0x25); w2(4);
131                 break;
132
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();
136                 w2(4);
137                 break;
138
139         case 4: w3(0x80); w2(0x24);
140                 for(k=0;k<(count/2)-1;k++) ((u16 *)buf)[k] = r4w();
141                 buf[count-2] = r4();
142                 w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
143                 w2(4);
144                 break;
145
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();
150                 w2(4);
151                 break;
152
153         }
154 }
155
156 static void epat_write_block( PIA *pi, char * buf, int count )   
157
158 {       int ph, k;
159
160         switch (pi->mode) {
161
162         case 0:
163         case 1:
164         case 2: w0(0x67); w2(1); w2(5);
165                 ph = 0;
166                 for(k=0;k<count;k++) {
167                         w0(buf[k]);
168                         w2(4+ph);
169                         ph = 1 - ph;
170                 }
171                 w2(7); w2(4);
172                 break;
173
174         case 3: w3(0xc0); 
175                 for(k=0;k<count;k++) w4(buf[k]);
176                 w2(4);
177                 break;
178
179         case 4: w3(0xc0); 
180                 for(k=0;k<(count/2);k++) w4w(((u16 *)buf)[k]);
181                 w2(4);
182                 break;
183
184         case 5: w3(0xc0); 
185                 for(k=0;k<(count/4);k++) w4l(((u32 *)buf)[k]);
186                 w2(4);
187                 break;
188
189         }
190 }
191
192 /* these macros access the EPAT registers in native addressing */
193
194 #define WR(r,v)         epat_write_regr(pi,2,r,v)
195 #define RR(r)           (epat_read_regr(pi,2,r))
196
197 /* and these access the IDE task file */
198
199 #define WRi(r,v)         epat_write_regr(pi,0,r,v)
200 #define RRi(r)           (epat_read_regr(pi,0,r))
201
202 /* FIXME:  the CPP stuff should be fixed to handle multiple EPATs on a chain */
203
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);
206
207 static void epat_connect ( PIA *pi )
208
209 {       pi->saved_r0 = r0();
210         pi->saved_r2 = r2();
211
212 #ifdef CONFIG_PARIDE_EPATC8
213         /* Initialize the chip */
214         CPP(0);CPP(0x40);CPP(0xe0);              
215         w0(0);w2(1);w2(4);
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);
220      /* CPP(0x30); */
221
222         /* Connect to the chip */
223         CPP(0xe0);
224         w0(0);w2(1);w2(4); /* Idle into SPP */
225         if (pi->mode >= 3) {
226           w0(0);w2(1);w2(4);w2(0xc);
227           /* Request EPP */
228           w0(0x40);w2(6);w2(7);w2(4);w2(0xc);w2(4);
229         }
230 #else
231         CPP(0); CPP(0xe0);
232         w0(0); w2(1); w2(4);
233         if (pi->mode >= 3) {
234                 w0(0); w2(1); w2(4); w2(0xc);
235                 w0(0x40); w2(6); w2(7); w2(4); w2(0xc); w2(4);
236         }
237         WR(8,0x10); WR(0xc,0x14); WR(0xa,0x38); WR(0x12,0x10);
238 #endif
239 }
240
241 static void epat_disconnect (PIA *pi)
242 {       CPP(0x30);
243         w0(pi->saved_r0);
244         w2(pi->saved_r2);
245 }
246
247 static int epat_test_proto( PIA *pi, char * scratch, int verbose )
248
249 {       int     k, j, f, cc;
250         int     e[2] = {0,0};
251
252         epat_connect(pi);
253         cc = RR(0xd);
254         epat_disconnect(pi);
255
256         epat_connect(pi);
257         for (j=0;j<2;j++) {
258             WRi(6,0xa0+j*0x10);
259             for (k=0;k<256;k++) {
260                 WRi(2,k^0xaa);
261                 WRi(3,k^0x55);
262                 if (RRi(2) != (k^0xaa)) e[j]++;
263                 }
264             }
265         epat_disconnect(pi);
266
267         f = 0;
268         epat_connect(pi);
269         WR(0x13,1); WR(0x13,0); WR(0xa,0x11);
270         epat_read_block(pi,scratch,512);
271         
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++;
275         }
276         epat_disconnect(pi);
277
278         if (verbose)  {
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);
281         }
282         
283         return (e[0] && e[1]) || f;
284 }
285
286 static void epat_log_adapter( PIA *pi, char * scratch, int verbose )
287
288 {       int     ver;
289         char    *mode_string[6] = 
290                    {"4-bit","5/3","8-bit","EPP-8","EPP-16","EPP-32"};
291
292         epat_connect(pi);
293         WR(0xa,0x38);           /* read the version code */
294         ver = RR(0xb);
295         epat_disconnect(pi);
296
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);
301
302 }
303
304 static struct pi_protocol epat = {
305         .owner          = THIS_MODULE,
306         .name           = "epat",
307         .max_mode       = 6,
308         .epp_first      = 3,
309         .default_delay  = 1,
310         .max_units      = 1,
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,
319 };
320
321 static int __init epat_init(void)
322 {
323         return pi_register(&epat)-1;
324 }
325
326 static void __exit epat_exit(void)
327 {
328         pi_unregister(&epat);
329 }
330
331 MODULE_LICENSE("GPL");
332 module_init(epat_init)
333 module_exit(epat_exit)