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.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/wait.h>
17 #include "pata_parport.h"
23 #define PC pi->private
24 #define r2() (PC=(in_p(2) & 0xff))
25 #define w2(byte) {out_p(2,byte); PC = byte;}
26 #define t2(pat) {PC ^= pat; out_p(2,PC);}
27 #define e2() {PC &= 0xfe; out_p(2,PC);}
28 #define o2() {PC |= 1; out_p(2,PC);}
30 #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
32 /* cont = 0 - access the IDE register file
33 cont = 1 - access the IDE command set
34 cont = 2 - use internal bpck register addressing
37 static int cont_map[3] = { 0x40, 0x48, 0 };
39 static int bpck_read_regr(struct pi_adapter *pi, int cont, int regr)
43 r = regr + cont_map[cont];
47 case 0: w0(r & 0xf); w0(r); t2(2); t2(4);
53 case 1: w0(r & 0xf); w0(r); t2(2);
61 case 4: w0(r); w2(9); w2(0); w2(0x20);
70 static void bpck_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
74 r = regr + cont_map[cont];
87 case 4: w0(r); w2(9); w2(0);
88 w0(val); w2(1); w2(3); w2(0);
94 /* These macros access the bpck registers in native addressing */
96 #define WR(r,v) bpck_write_regr(pi,2,r,v)
97 #define RR(r) (bpck_read_regr(pi,2,r))
99 static void bpck_write_block(struct pi_adapter *pi, char *buf, int count)
106 w0(0x40); t2(2); t2(1);
107 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
112 w0(0x40); t2(2); t2(1);
113 for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
118 w0(0x40); w2(9); w2(0); w2(1);
119 for (i=0;i<count;i++) w4(buf[i]);
125 w0(0x40); w2(9); w2(0); w2(1);
126 for (i=0;i<count/2;i++) w4w(((u16 *)buf)[i]);
132 w0(0x40); w2(9); w2(0); w2(1);
133 for (i=0;i<count/4;i++) w4l(((u32 *)buf)[i]);
140 static void bpck_read_block(struct pi_adapter *pi, char *buf, int count)
148 for (i=0;i<count;i++) {
157 w0(0x40); t2(2); t2(0x20);
158 for(i=0;i<count;i++) { t2(4); buf[i] = r0(); }
164 w0(0x40); w2(9); w2(0); w2(0x20);
165 for (i=0;i<count;i++) buf[i] = r4();
171 w0(0x40); w2(9); w2(0); w2(0x20);
172 for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
178 w0(0x40); w2(9); w2(0); w2(0x20);
179 for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
187 static int bpck_probe_unit(struct pi_adapter *pi)
189 { int o1, o0, f7, id;
194 w2(4); w2(0xe); r2(); t2(2);
197 w0(255-id); w2(4); w0(id);
199 t2(2); t = r1()&0xf8;
200 f7 = ((id % 8) == 7);
201 if ((f7) || (t != o1)) { t2(2); s = r1()&0xf8; }
202 if ((t == o1) && ((!f7) || (s == o1))) {
206 t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
210 static void bpck_connect(struct pi_adapter *pi)
212 { pi->saved_r0 = r0();
213 w0(0xff-pi->unit); w2(4); w0(pi->unit);
219 case 0: t2(8); WR(4,0);
222 case 1: t2(8); WR(4,0x10);
227 case 4: w2(0); WR(4,8);
234 /* if (pi->devtype == PI_PCD) { possibly wrong, purpose unknown */
235 WR(0x46,0x10); /* fiddle with ESS logic ??? */
244 static void bpck_disconnect(struct pi_adapter *pi)
247 if (pi->mode >= 2) { w2(9); w2(0); } else t2(2);
248 w2(0x4c); w0(pi->saved_r0);
251 static void bpck_force_spp(struct pi_adapter *pi)
253 /* This fakes the EPP protocol to turn off EPP ... */
255 { pi->saved_r0 = r0();
256 w0(0xff-pi->unit); w2(4); w0(pi->unit);
262 w0(0); w2(1); w2(3); w2(0);
264 w2(0x4c); w0(pi->saved_r0);
269 static int bpck_test_proto(struct pi_adapter *pi)
271 { int i, e, l, h, om;
278 case 0: bpck_connect(pi);
281 for(i=0;i<TEST_LEN;i++) {
289 case 1: bpck_connect(pi);
291 w0(0x13); t2(2); t2(0x20);
292 for(i=0;i<TEST_LEN;i++) { t2(4); buf[i] = r0(); }
299 case 4: om = pi->mode;
308 w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
311 case 2: for (i=0;i<TEST_LEN;i++) buf[i] = r4();
313 case 3: for (i=0;i<TEST_LEN/2;i++) ((u16 *)buf)[i] = r4w();
315 case 4: for (i=0;i<TEST_LEN/4;i++) ((u32 *)buf)[i] = r4l();
327 dev_dbg(&pi->dev, "bpck: 0x%x unit %d mode %d: ",
328 pi->port, pi->unit, pi->mode);
329 print_hex_dump_debug("bpck: ", DUMP_PREFIX_NONE, TEST_LEN, 1, buf, TEST_LEN, false);
332 for (i=0;i<TEST_LEN;i++) if (buf[i] != (i+1)) e++;
336 static void bpck_read_eeprom(struct pi_adapter *pi, char *buf)
338 { int i, j, k, p, v, f, om, od;
342 om = pi->mode; od = pi->delay;
343 pi->mode = 0; pi->delay = 6;
353 f = (((i + 0x180) & p) != 0) * 2;
366 v = 2*v + (f == 0x84);
384 pi->mode = om; pi->delay = od;
387 static int bpck_test_port(struct pi_adapter *pi) /* check for 8-bit port */
391 w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i);
394 if (r == (255-i)) m = 0;
396 w2(0xc); i = r0(); w0(255-i); r = r0(); w0(i);
397 if (r != (255-i)) m = -1;
399 if (m == 0) { w2(6); w2(0xc); r = r0(); w0(0xaa); w0(r); w0(0xaa); }
400 if (m == 2) { w2(0x26); w2(0xc); }
402 if (m == -1) return 0;
406 static void bpck_log_adapter(struct pi_adapter *pi)
408 { char *mode_string[5] = { "4-bit","8-bit","EPP-8",
412 bpck_read_eeprom(pi,scratch);
413 print_hex_dump_bytes("bpck EEPROM: ", DUMP_PREFIX_NONE, scratch, 128);
414 dev_info(&pi->dev, "backpack %8.8s unit %d at 0x%x, mode %d (%s), delay %d\n",
415 &scratch[110], pi->unit, pi->port, pi->mode,
416 mode_string[pi->mode], pi->delay);
419 static struct pi_protocol bpck = {
420 .owner = THIS_MODULE,
426 .write_regr = bpck_write_regr,
427 .read_regr = bpck_read_regr,
428 .write_block = bpck_write_block,
429 .read_block = bpck_read_block,
430 .connect = bpck_connect,
431 .disconnect = bpck_disconnect,
432 .test_port = bpck_test_port,
433 .probe_unit = bpck_probe_unit,
434 .test_proto = bpck_test_proto,
435 .log_adapter = bpck_log_adapter,
438 MODULE_LICENSE("GPL");
439 module_pata_parport_driver(bpck);