2 bpck.c (c) 1996-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU General Public License.
5 bpck.c is a low-level protocol driver for the MicroSolutions
6 "backpack" parallel port IDE adapter.
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
17 #define BPCK_VERSION "1.02"
19 #include <linux/module.h>
20 #include <linux/delay.h>
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/wait.h>
31 #define PC pi->private
32 #define r2() (PC=(in_p(2) & 0xff))
33 #define w2(byte) {out_p(2,byte); PC = byte;}
34 #define t2(pat) {PC ^= pat; out_p(2,PC);}
35 #define e2() {PC &= 0xfe; out_p(2,PC);}
36 #define o2() {PC |= 1; out_p(2,PC);}
38 #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
40 /* cont = 0 - access the IDE register file
41 cont = 1 - access the IDE command set
42 cont = 2 - use internal bpck register addressing
45 static int cont_map[3] = { 0x40, 0x48, 0 };
47 static int bpck_read_regr( PIA *pi, int cont, int regr )
51 r = regr + cont_map[cont];
55 case 0: w0(r & 0xf); w0(r); t2(2); t2(4);
61 case 1: w0(r & 0xf); w0(r); t2(2);
69 case 4: w0(r); w2(9); w2(0); w2(0x20);
78 static void bpck_write_regr( PIA *pi, int cont, int regr, int val )
82 r = regr + cont_map[cont];
95 case 4: w0(r); w2(9); w2(0);
96 w0(val); w2(1); w2(3); w2(0);
102 /* These macros access the bpck registers in native addressing */
104 #define WR(r,v) bpck_write_regr(pi,2,r,v)
105 #define RR(r) (bpck_read_regr(pi,2,r))
107 static void bpck_write_block( PIA *pi, char * buf, int count )
114 w0(0x40); t2(2); t2(1);
115 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
120 w0(0x40); t2(2); t2(1);
121 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
126 w0(0x40); w2(9); w2(0); w2(1);
127 for (i=0;i<count;i++) w4(buf[i]);
133 w0(0x40); w2(9); w2(0); w2(1);
134 for (i=0;i<count/2;i++) w4w(((u16 *)buf)[i]);
140 w0(0x40); w2(9); w2(0); w2(1);
141 for (i=0;i<count/4;i++) w4l(((u32 *)buf)[i]);
148 static void bpck_read_block( PIA *pi, char * buf, int count )
156 for (i=0;i<count;i++) {
165 w0(0x40); t2(2); t2(0x20);
166 for(i=0;i<count;i++) { t2(4); buf[i] = r0(); }
172 w0(0x40); w2(9); w2(0); w2(0x20);
173 for (i=0;i<count;i++) buf[i] = r4();
179 w0(0x40); w2(9); w2(0); w2(0x20);
180 for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
186 w0(0x40); w2(9); w2(0); w2(0x20);
187 for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
195 static int bpck_probe_unit ( PIA *pi )
197 { int o1, o0, f7, id;
202 w2(4); w2(0xe); r2(); t2(2);
205 w0(255-id); w2(4); w0(id);
207 t2(2); t = r1()&0xf8;
208 f7 = ((id % 8) == 7);
209 if ((f7) || (t != o1)) { t2(2); s = r1()&0xf8; }
210 if ((t == o1) && ((!f7) || (s == o1))) {
214 t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
218 static void bpck_connect ( PIA *pi )
220 { pi->saved_r0 = r0();
221 w0(0xff-pi->unit); w2(4); w0(pi->unit);
227 case 0: t2(8); WR(4,0);
230 case 1: t2(8); WR(4,0x10);
235 case 4: w2(0); WR(4,8);
242 if (pi->devtype == PI_PCD) {
243 WR(0x46,0x10); /* fiddle with ESS logic ??? */
252 static void bpck_disconnect ( PIA *pi )
255 if (pi->mode >= 2) { w2(9); w2(0); } else t2(2);
256 w2(0x4c); w0(pi->saved_r0);
259 static void bpck_force_spp ( PIA *pi )
261 /* This fakes the EPP protocol to turn off EPP ... */
263 { pi->saved_r0 = r0();
264 w0(0xff-pi->unit); w2(4); w0(pi->unit);
270 w0(0); w2(1); w2(3); w2(0);
272 w2(0x4c); w0(pi->saved_r0);
277 static int bpck_test_proto( PIA *pi, char * scratch, int verbose )
279 { int i, e, l, h, om;
286 case 0: bpck_connect(pi);
289 for(i=0;i<TEST_LEN;i++) {
297 case 1: bpck_connect(pi);
299 w0(0x13); t2(2); t2(0x20);
300 for(i=0;i<TEST_LEN;i++) { t2(4); buf[i] = r0(); }
307 case 4: om = pi->mode;
316 w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
319 case 2: for (i=0;i<TEST_LEN;i++) buf[i] = r4();
321 case 3: for (i=0;i<TEST_LEN/2;i++) ((u16 *)buf)[i] = r4w();
323 case 4: for (i=0;i<TEST_LEN/4;i++) ((u32 *)buf)[i] = r4l();
336 printk("%s: bpck: 0x%x unit %d mode %d: ",
337 pi->device,pi->port,pi->unit,pi->mode);
338 for (i=0;i<TEST_LEN;i++) printk("%3d",buf[i]);
343 for (i=0;i<TEST_LEN;i++) if (buf[i] != (i+1)) e++;
347 static void bpck_read_eeprom ( PIA *pi, char * buf )
349 { int i,j,k,n,p,v,f, om, od;
353 om = pi->mode; od = pi->delay;
354 pi->mode = 0; pi->delay = 6;
365 f = (((i + 0x180) & p) != 0) * 2;
378 v = 2*v + (f == 0x84);
396 pi->mode = om; pi->delay = od;
399 static int bpck_test_port ( PIA *pi ) /* check for 8-bit port */
403 w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i);
406 if (r == (255-i)) m = 0;
408 w2(0xc); i = r0(); w0(255-i); r = r0(); w0(i);
409 if (r != (255-i)) m = -1;
411 if (m == 0) { w2(6); w2(0xc); r = r0(); w0(0xaa); w0(r); w0(0xaa); }
412 if (m == 2) { w2(0x26); w2(0xc); }
414 if (m == -1) return 0;
418 static void bpck_log_adapter( PIA *pi, char * scratch, int verbose )
420 { char *mode_string[5] = { "4-bit","8-bit","EPP-8",
427 bpck_read_eeprom(pi,scratch);
432 if ((scratch[i] < ' ') || (scratch[i] > '~'))
434 printk("%s: bpck EEPROM: %64.64s\n",pi->device,scratch);
435 printk("%s: %64.64s\n",pi->device,&scratch[64]);
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);
445 static void bpck_init_proto( PIA *pi)
450 static void bpck_release_proto( PIA *pi)
455 struct pi_protocol bpck = { "bpck",0,5,2,4,256,
472 int init_module(void)
474 { return pi_register(&bpck) - 1;
477 void cleanup_module(void)
479 { pi_unregister(&bpck);
485 MODULE_LICENSE("GPL");