1 /*
2 bpck.c (c) 1996-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
4
5 bpck.c is a low-level protocol driver for the MicroSolutions
6 "backpack" parallel port IDE adapter.
7
8 */
9
10 /* Changes:
11
12 1.01 GRG 1998.05.05 init_proto, release_proto, pi->delay
13 1.02 GRG 1998.08.15 default pi->delay returned to 4
14
15 */
16
17 #define BPCK_VERSION "1.02"
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/kernel.h>
23 #include <linux/types.h>
24 #include <linux/wait.h>
25 #include <asm/io.h>
26
27 #include "paride.h"
28
29 #undef r2
30 #undef w2
31
32 #define PC pi->private
33 #define r2() (PC=(in_p(2) & 0xff))
34 #define w2(byte) {out_p(2,byte); PC = byte;}
35 #define t2(pat) {PC ^= pat; out_p(2,PC);}
36 #define e2() {PC &= 0xfe; out_p(2,PC);}
37 #define o2() {PC |= 1; out_p(2,PC);}
38
39 #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
40
41 /* cont = 0 - access the IDE register file
42 cont = 1 - access the IDE command set
43 cont = 2 - use internal bpck register addressing
44 */
45
46 static int cont_map[3] = { 0x40, 0x48, 0 };
47
bpck_read_regr(PIA * pi,int cont,int regr)48 static int bpck_read_regr( PIA *pi, int cont, int regr )
49
50 { int r, l, h;
51
52 r = regr + cont_map[cont];
53
54 switch (pi->mode) {
55
56 case 0: w0(r & 0xf); w0(r); t2(2); t2(4);
57 l = r1();
58 t2(4);
59 h = r1();
60 return j44(l,h);
61
62 case 1: w0(r & 0xf); w0(r); t2(2);
63 e2(); t2(0x20);
64 t2(4); h = r0();
65 t2(1); t2(0x20);
66 return h;
67
68 case 2:
69 case 3:
70 case 4: w0(r); w2(9); w2(0); w2(0x20);
71 h = r4();
72 w2(0);
73 return h;
74
75 }
76 return -1;
77 }
78
bpck_write_regr(PIA * pi,int cont,int regr,int val)79 static void bpck_write_regr( PIA *pi, int cont, int regr, int val )
80
81 { int r;
82
83 r = regr + cont_map[cont];
84
85 switch (pi->mode) {
86
87 case 0:
88 case 1: w0(r);
89 t2(2);
90 w0(val);
91 o2(); t2(4); t2(1);
92 break;
93
94 case 2:
95 case 3:
96 case 4: w0(r); w2(9); w2(0);
97 w0(val); w2(1); w2(3); w2(0);
98 break;
99
100 }
101 }
102
103 /* These macros access the bpck registers in native addressing */
104
105 #define WR(r,v) bpck_write_regr(pi,2,r,v)
106 #define RR(r) (bpck_read_regr(pi,2,r))
107
bpck_write_block(PIA * pi,char * buf,int count)108 static void bpck_write_block( PIA *pi, char * buf, int count )
109
110 { int i;
111
112 switch (pi->mode) {
113
114 case 0: WR(4,0x40);
115 w0(0x40); t2(2); t2(1);
116 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
117 WR(4,0);
118 break;
119
120 case 1: WR(4,0x50);
121 w0(0x40); t2(2); t2(1);
122 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
123 WR(4,0x10);
124 break;
125
126 case 2: WR(4,0x48);
127 w0(0x40); w2(9); w2(0); w2(1);
128 for (i=0;i<count;i++) w4(buf[i]);
129 w2(0);
130 WR(4,8);
131 break;
132
133 case 3: WR(4,0x48);
134 w0(0x40); w2(9); w2(0); w2(1);
135 for (i=0;i<count/2;i++) w4w(((u16 *)buf)[i]);
136 w2(0);
137 WR(4,8);
138 break;
139
140 case 4: WR(4,0x48);
141 w0(0x40); w2(9); w2(0); w2(1);
142 for (i=0;i<count/4;i++) w4l(((u32 *)buf)[i]);
143 w2(0);
144 WR(4,8);
145 break;
146 }
147 }
148
bpck_read_block(PIA * pi,char * buf,int count)149 static void bpck_read_block( PIA *pi, char * buf, int count )
150
151 { int i, l, h;
152
153 switch (pi->mode) {
154
155 case 0: WR(4,0x40);
156 w0(0x40); t2(2);
157 for (i=0;i<count;i++) {
158 t2(4); l = r1();
159 t2(4); h = r1();
160 buf[i] = j44(l,h);
161 }
162 WR(4,0);
163 break;
164
165 case 1: WR(4,0x50);
166 w0(0x40); t2(2); t2(0x20);
167 for(i=0;i<count;i++) { t2(4); buf[i] = r0(); }
168 t2(1); t2(0x20);
169 WR(4,0x10);
170 break;
171
172 case 2: WR(4,0x48);
173 w0(0x40); w2(9); w2(0); w2(0x20);
174 for (i=0;i<count;i++) buf[i] = r4();
175 w2(0);
176 WR(4,8);
177 break;
178
179 case 3: WR(4,0x48);
180 w0(0x40); w2(9); w2(0); w2(0x20);
181 for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
182 w2(0);
183 WR(4,8);
184 break;
185
186 case 4: WR(4,0x48);
187 w0(0x40); w2(9); w2(0); w2(0x20);
188 for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
189 w2(0);
190 WR(4,8);
191 break;
192
193 }
194 }
195
bpck_probe_unit(PIA * pi)196 static int bpck_probe_unit ( PIA *pi )
197
198 { int o1, o0, f7, id;
199 int t, s;
200
201 id = pi->unit;
202 s = 0;
203 w2(4); w2(0xe); r2(); t2(2);
204 o1 = r1()&0xf8;
205 o0 = r0();
206 w0(255-id); w2(4); w0(id);
207 t2(8); t2(8); t2(8);
208 t2(2); t = r1()&0xf8;
209 f7 = ((id % 8) == 7);
210 if ((f7) || (t != o1)) { t2(2); s = r1()&0xf8; }
211 if ((t == o1) && ((!f7) || (s == o1))) {
212 w2(0x4c); w0(o0);
213 return 0;
214 }
215 t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
216 return 1;
217 }
218
bpck_connect(PIA * pi)219 static void bpck_connect ( PIA *pi )
220
221 { pi->saved_r0 = r0();
222 w0(0xff-pi->unit); w2(4); w0(pi->unit);
223 t2(8); t2(8); t2(8);
224 t2(2); t2(2);
225
226 switch (pi->mode) {
227
228 case 0: t2(8); WR(4,0);
229 break;
230
231 case 1: t2(8); WR(4,0x10);
232 break;
233
234 case 2:
235 case 3:
236 case 4: w2(0); WR(4,8);
237 break;
238
239 }
240
241 WR(5,8);
242
243 if (pi->devtype == PI_PCD) {
244 WR(0x46,0x10); /* fiddle with ESS logic ??? */
245 WR(0x4c,0x38);
246 WR(0x4d,0x88);
247 WR(0x46,0xa0);
248 WR(0x41,0);
249 WR(0x4e,8);
250 }
251 }
252
bpck_disconnect(PIA * pi)253 static void bpck_disconnect ( PIA *pi )
254
255 { w0(0);
256 if (pi->mode >= 2) { w2(9); w2(0); } else t2(2);
257 w2(0x4c); w0(pi->saved_r0);
258 }
259
bpck_force_spp(PIA * pi)260 static void bpck_force_spp ( PIA *pi )
261
262 /* This fakes the EPP protocol to turn off EPP ... */
263
264 { pi->saved_r0 = r0();
265 w0(0xff-pi->unit); w2(4); w0(pi->unit);
266 t2(8); t2(8); t2(8);
267 t2(2); t2(2);
268
269 w2(0);
270 w0(4); w2(9); w2(0);
271 w0(0); w2(1); w2(3); w2(0);
272 w0(0); w2(9); w2(0);
273 w2(0x4c); w0(pi->saved_r0);
274 }
275
276 #define TEST_LEN 16
277
bpck_test_proto(PIA * pi,char * scratch,int verbose)278 static int bpck_test_proto( PIA *pi, char * scratch, int verbose )
279
280 { int i, e, l, h, om;
281 char buf[TEST_LEN];
282
283 bpck_force_spp(pi);
284
285 switch (pi->mode) {
286
287 case 0: bpck_connect(pi);
288 WR(0x13,0x7f);
289 w0(0x13); t2(2);
290 for(i=0;i<TEST_LEN;i++) {
291 t2(4); l = r1();
292 t2(4); h = r1();
293 buf[i] = j44(l,h);
294 }
295 bpck_disconnect(pi);
296 break;
297
298 case 1: bpck_connect(pi);
299 WR(0x13,0x7f);
300 w0(0x13); t2(2); t2(0x20);
301 for(i=0;i<TEST_LEN;i++) { t2(4); buf[i] = r0(); }
302 t2(1); t2(0x20);
303 bpck_disconnect(pi);
304 break;
305
306 case 2:
307 case 3:
308 case 4: om = pi->mode;
309 pi->mode = 0;
310 bpck_connect(pi);
311 WR(7,3);
312 WR(4,8);
313 bpck_disconnect(pi);
314
315 pi->mode = om;
316 bpck_connect(pi);
317 w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
318
319 switch (pi->mode) {
320 case 2: for (i=0;i<TEST_LEN;i++) buf[i] = r4();
321 break;
322 case 3: for (i=0;i<TEST_LEN/2;i++) ((u16 *)buf)[i] = r4w();
323 break;
324 case 4: for (i=0;i<TEST_LEN/4;i++) ((u32 *)buf)[i] = r4l();
325 break;
326 }
327
328 w2(0);
329 WR(7,0);
330 bpck_disconnect(pi);
331
332 break;
333
334 }
335
336 if (verbose) {
337 printk("%s: bpck: 0x%x unit %d mode %d: ",
338 pi->device,pi->port,pi->unit,pi->mode);
339 for (i=0;i<TEST_LEN;i++) printk("%3d",buf[i]);
340 printk("\n");
341 }
342
343 e = 0;
344 for (i=0;i<TEST_LEN;i++) if (buf[i] != (i+1)) e++;
345 return e;
346 }
347
bpck_read_eeprom(PIA * pi,char * buf)348 static void bpck_read_eeprom ( PIA *pi, char * buf )
349
350 { int i, j, k, p, v, f, om, od;
351
352 bpck_force_spp(pi);
353
354 om = pi->mode; od = pi->delay;
355 pi->mode = 0; pi->delay = 6;
356
357 bpck_connect(pi);
358
359 WR(4,0);
360 for (i=0;i<64;i++) {
361 WR(6,8);
362 WR(6,0xc);
363 p = 0x100;
364 for (k=0;k<9;k++) {
365 f = (((i + 0x180) & p) != 0) * 2;
366 WR(6,f+0xc);
367 WR(6,f+0xd);
368 WR(6,f+0xc);
369 p = (p >> 1);
370 }
371 for (j=0;j<2;j++) {
372 v = 0;
373 for (k=0;k<8;k++) {
374 WR(6,0xc);
375 WR(6,0xd);
376 WR(6,0xc);
377 f = RR(0);
378 v = 2*v + (f == 0x84);
379 }
380 buf[2*i+1-j] = v;
381 }
382 }
383 WR(6,8);
384 WR(6,0);
385 WR(5,8);
386
387 bpck_disconnect(pi);
388
389 if (om >= 2) {
390 bpck_connect(pi);
391 WR(7,3);
392 WR(4,8);
393 bpck_disconnect(pi);
394 }
395
396 pi->mode = om; pi->delay = od;
397 }
398
bpck_test_port(PIA * pi)399 static int bpck_test_port ( PIA *pi ) /* check for 8-bit port */
400
401 { int i, r, m;
402
403 w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i);
404 m = -1;
405 if (r == i) m = 2;
406 if (r == (255-i)) m = 0;
407
408 w2(0xc); i = r0(); w0(255-i); r = r0(); w0(i);
409 if (r != (255-i)) m = -1;
410
411 if (m == 0) { w2(6); w2(0xc); r = r0(); w0(0xaa); w0(r); w0(0xaa); }
412 if (m == 2) { w2(0x26); w2(0xc); }
413
414 if (m == -1) return 0;
415 return 5;
416 }
417
bpck_log_adapter(PIA * pi,char * scratch,int verbose)418 static void bpck_log_adapter( PIA *pi, char * scratch, int verbose )
419
420 { char *mode_string[5] = { "4-bit","8-bit","EPP-8",
421 "EPP-16","EPP-32" };
422
423 #ifdef DUMP_EEPROM
424 int i;
425 #endif
426
427 bpck_read_eeprom(pi,scratch);
428
429 #ifdef DUMP_EEPROM
430 if (verbose) {
431 for(i=0;i<128;i++)
432 if ((scratch[i] < ' ') || (scratch[i] > '~'))
433 scratch[i] = '.';
434 printk("%s: bpck EEPROM: %64.64s\n",pi->device,scratch);
435 printk("%s: %64.64s\n",pi->device,&scratch[64]);
436 }
437 #endif
438
439 printk("%s: bpck %s, backpack %8.8s unit %d",
440 pi->device,BPCK_VERSION,&scratch[110],pi->unit);
441 printk(" at 0x%x, mode %d (%s), delay %d\n",pi->port,
442 pi->mode,mode_string[pi->mode],pi->delay);
443 }
444
445 static struct pi_protocol bpck = {
446 .owner = THIS_MODULE,
447 .name = "bpck",
448 .max_mode = 5,
449 .epp_first = 2,
450 .default_delay = 4,
451 .max_units = 255,
452 .write_regr = bpck_write_regr,
453 .read_regr = bpck_read_regr,
454 .write_block = bpck_write_block,
455 .read_block = bpck_read_block,
456 .connect = bpck_connect,
457 .disconnect = bpck_disconnect,
458 .test_port = bpck_test_port,
459 .probe_unit = bpck_probe_unit,
460 .test_proto = bpck_test_proto,
461 .log_adapter = bpck_log_adapter,
462 };
463
bpck_init(void)464 static int __init bpck_init(void)
465 {
466 return paride_register(&bpck);
467 }
468
bpck_exit(void)469 static void __exit bpck_exit(void)
470 {
471 paride_unregister(&bpck);
472 }
473
474 MODULE_LICENSE("GPL");
475 module_init(bpck_init)
476 module_exit(bpck_exit)
477