2 bpck.c (c) 1996-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU 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>
30 #define PC pi->private
31 #define r2() (PC=(in_p(2) & 0xff))
32 #define w2(byte) {out_p(2,byte); PC = byte;}
33 #define t2(pat) {PC ^= pat; out_p(2,PC);}
34 #define e2() {PC &= 0xfe; out_p(2,PC);}
35 #define o2() {PC |= 1; out_p(2,PC);}
37 #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
39 /* cont = 0 - access the IDE register file
40 cont = 1 - access the IDE command set
41 cont = 2 - use internal bpck register addressing
44 static int cont_map
[3] = { 0x40, 0x48, 0 };
46 static int bpck_read_regr( PIA
*pi
, int cont
, int regr
)
50 r
= regr
+ cont_map
[cont
];
54 case 0: w0(r
& 0xf); w0(r
); t2(2); t2(4);
60 case 1: w0(r
& 0xf); w0(r
); t2(2);
68 case 4: w0(r
); w2(9); w2(0); w2(0x20);
77 static void bpck_write_regr( PIA
*pi
, int cont
, int regr
, int val
)
81 r
= regr
+ cont_map
[cont
];
94 case 4: w0(r
); w2(9); w2(0);
95 w0(val
); w2(1); w2(3); w2(0);
101 /* These macros access the bpck registers in native addressing */
103 #define WR(r,v) bpck_write_regr(pi,2,r,v)
104 #define RR(r) (bpck_read_regr(pi,2,r))
106 static void bpck_write_block( PIA
*pi
, char * buf
, int count
)
113 w0(0x40); t2(2); t2(1);
114 for (i
=0;i
<count
;i
++) { w0(buf
[i
]); t2(4); }
119 w0(0x40); t2(2); t2(1);
120 for (i
=0;i
<count
;i
++) { w0(buf
[i
]); t2(4); }
125 w0(0x40); w2(9); w2(0); w2(1);
126 for (i
=0;i
<count
;i
++) w4(buf
[i
]);
132 w0(0x40); w2(9); w2(0); w2(1);
133 for (i
=0;i
<count
/2;i
++) w4w(((u16
*)buf
)[i
]);
139 w0(0x40); w2(9); w2(0); w2(1);
140 for (i
=0;i
<count
/4;i
++) w4l(((u32
*)buf
)[i
]);
147 static void bpck_read_block( PIA
*pi
, char * buf
, int count
)
155 for (i
=0;i
<count
;i
++) {
164 w0(0x40); t2(2); t2(0x20);
165 for(i
=0;i
<count
;i
++) { t2(4); buf
[i
] = r0(); }
171 w0(0x40); w2(9); w2(0); w2(0x20);
172 for (i
=0;i
<count
;i
++) buf
[i
] = r4();
178 w0(0x40); w2(9); w2(0); w2(0x20);
179 for (i
=0;i
<count
/2;i
++) ((u16
*)buf
)[i
] = r4w();
185 w0(0x40); w2(9); w2(0); w2(0x20);
186 for (i
=0;i
<count
/4;i
++) ((u32
*)buf
)[i
] = r4l();
194 static int bpck_probe_unit ( PIA
*pi
)
196 { int o1
, o0
, f7
, id
;
201 w2(4); w2(0xe); r2(); t2(2);
204 w0(255-id
); w2(4); w0(id
);
206 t2(2); t
= r1()&0xf8;
207 f7
= ((id
% 8) == 7);
208 if ((f7
) || (t
!= o1
)) { t2(2); s
= r1()&0xf8; }
209 if ((t
== o1
) && ((!f7
) || (s
== o1
))) {
213 t2(8); w0(0); t2(2); w2(0x4c); w0(o0
);
217 static void bpck_connect ( PIA
*pi
)
219 { pi
->saved_r0
= r0();
220 w0(0xff-pi
->unit
); w2(4); w0(pi
->unit
);
226 case 0: t2(8); WR(4,0);
229 case 1: t2(8); WR(4,0x10);
234 case 4: w2(0); WR(4,8);
241 if (pi
->devtype
== PI_PCD
) {
242 WR(0x46,0x10); /* fiddle with ESS logic ??? */
251 static void bpck_disconnect ( PIA
*pi
)
254 if (pi
->mode
>= 2) { w2(9); w2(0); } else t2(2);
255 w2(0x4c); w0(pi
->saved_r0
);
258 static void bpck_force_spp ( PIA
*pi
)
260 /* This fakes the EPP protocol to turn off EPP ... */
262 { pi
->saved_r0
= r0();
263 w0(0xff-pi
->unit
); w2(4); w0(pi
->unit
);
269 w0(0); w2(1); w2(3); w2(0);
271 w2(0x4c); w0(pi
->saved_r0
);
276 static int bpck_test_proto( PIA
*pi
, char * scratch
, int verbose
)
278 { int i
, e
, l
, h
, om
;
285 case 0: bpck_connect(pi
);
288 for(i
=0;i
<TEST_LEN
;i
++) {
296 case 1: bpck_connect(pi
);
298 w0(0x13); t2(2); t2(0x20);
299 for(i
=0;i
<TEST_LEN
;i
++) { t2(4); buf
[i
] = r0(); }
306 case 4: om
= pi
->mode
;
315 w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
318 case 2: for (i
=0;i
<TEST_LEN
;i
++) buf
[i
] = r4();
320 case 3: for (i
=0;i
<TEST_LEN
/2;i
++) ((u16
*)buf
)[i
] = r4w();
322 case 4: for (i
=0;i
<TEST_LEN
/4;i
++) ((u32
*)buf
)[i
] = r4l();
335 printk("%s: bpck: 0x%x unit %d mode %d: ",
336 pi
->device
,pi
->port
,pi
->unit
,pi
->mode
);
337 for (i
=0;i
<TEST_LEN
;i
++) printk("%3d",buf
[i
]);
342 for (i
=0;i
<TEST_LEN
;i
++) if (buf
[i
] != (i
+1)) e
++;
346 static void bpck_read_eeprom ( PIA
*pi
, char * buf
)
348 { int i
,j
,k
,n
,p
,v
,f
, om
, od
;
352 om
= pi
->mode
; od
= pi
->delay
;
353 pi
->mode
= 0; pi
->delay
= 6;
364 f
= (((i
+ 0x180) & p
) != 0) * 2;
377 v
= 2*v
+ (f
== 0x84);
395 pi
->mode
= om
; pi
->delay
= od
;
398 static int bpck_test_port ( PIA
*pi
) /* check for 8-bit port */
402 w2(0x2c); i
= r0(); w0(255-i
); r
= r0(); w0(i
);
405 if (r
== (255-i
)) m
= 0;
407 w2(0xc); i
= r0(); w0(255-i
); r
= r0(); w0(i
);
408 if (r
!= (255-i
)) m
= -1;
410 if (m
== 0) { w2(6); w2(0xc); r
= r0(); w0(0xaa); w0(r
); w0(0xaa); }
411 if (m
== 2) { w2(0x26); w2(0xc); }
413 if (m
== -1) return 0;
417 static void bpck_log_adapter( PIA
*pi
, char * scratch
, int verbose
)
419 { char *mode_string
[5] = { "4-bit","8-bit","EPP-8",
426 bpck_read_eeprom(pi
,scratch
);
431 if ((scratch
[i
] < ' ') || (scratch
[i
] > '~'))
433 printk("%s: bpck EEPROM: %64.64s\n",pi
->device
,scratch
);
434 printk("%s: %64.64s\n",pi
->device
,&scratch
[64]);
438 printk("%s: bpck %s, backpack %8.8s unit %d",
439 pi
->device
,BPCK_VERSION
,&scratch
[110],pi
->unit
);
440 printk(" at 0x%x, mode %d (%s), delay %d\n",pi
->port
,
441 pi
->mode
,mode_string
[pi
->mode
],pi
->delay
);
444 static void bpck_init_proto( PIA
*pi
)
449 static void bpck_release_proto( PIA
*pi
)
454 struct pi_protocol bpck
= { "bpck",0,5,2,4,256,
471 int init_module(void)
473 { return pi_register(&bpck
) - 1;
476 void cleanup_module(void)
478 { pi_unregister(&bpck
);