1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * bpck.c is a low-level protocol driver for the MicroSolutions
6 * "backpack" parallel port IDE adapter.
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/wait.h>
16 #include "pata_parport.h"
22 #define PC pi->private
23 #define r2() (PC=(in_p(2) & 0xff))
24 #define w2(byte) {out_p(2,byte); PC = byte;}
25 #define t2(pat) {PC ^= pat; out_p(2,PC);}
26 #define e2() {PC &= 0xfe; out_p(2,PC);}
27 #define o2() {PC |= 1; out_p(2,PC);}
29 #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
36 static int cont_map[3] = { 0x40, 0x48, 0 };
38 static int bpck_read_regr(struct pi_adapter *pi, int cont, int regr)
42 r = regr + cont_map[cont];
46 w0(r & 0xf); w0(r); t2(2); t2(4);
52 w0(r & 0xf); w0(r); t2(2);
60 w0(r); w2(9); w2(0); w2(0x20);
69 static void bpck_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
73 r = regr + cont_map[cont];
84 case 4: w0(r); w2(9); w2(0);
85 w0(val); w2(1); w2(3); w2(0);
91 /* These macros access the bpck registers in native addressing */
93 #define WR(r,v) bpck_write_regr(pi,2,r,v)
94 #define RR(r) (bpck_read_regr(pi,2,r))
96 static void bpck_write_block(struct pi_adapter *pi, char *buf, int count)
104 w0(0x40); t2(2); t2(1);
105 for (i = 0; i < count; i++) {
114 w0(0x40); t2(2); t2(1);
115 for (i = 0; i < count; i++) {
124 w0(0x40); w2(9); w2(0); w2(1);
125 for (i = 0; i < count; i++)
133 w0(0x40); w2(9); w2(0); w2(1);
134 for (i = 0; i < count / 2; i++)
135 w4w(((u16 *)buf)[i]);
142 w0(0x40); w2(9); w2(0); w2(1);
143 for (i = 0; i < count / 4; i++)
144 w4l(((u32 *)buf)[i]);
151 static void bpck_read_block(struct pi_adapter *pi, char *buf, int count)
160 for (i = 0; i < count; i++) {
170 w0(0x40); t2(2); t2(0x20);
171 for (i = 0; i < count; i++) {
181 w0(0x40); w2(9); w2(0); w2(0x20);
182 for (i = 0; i < count; i++)
190 w0(0x40); w2(9); w2(0); w2(0x20);
191 for (i = 0; i < count / 2; i++)
192 ((u16 *)buf)[i] = r4w();
199 w0(0x40); w2(9); w2(0); w2(0x20);
200 for (i = 0; i < count / 4; i++)
201 ((u32 *)buf)[i] = r4l();
209 static int bpck_probe_unit(struct pi_adapter *pi)
216 w2(4); w2(0xe); r2(); t2(2);
219 w0(255-id); w2(4); w0(id);
221 t2(2); t = r1()&0xf8;
222 f7 = ((id % 8) == 7);
223 if ((f7) || (t != o1)) {
227 if ((t == o1) && ((!f7) || (s == o1))) {
231 t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
235 static void bpck_connect(struct pi_adapter *pi)
238 w0(0xff-pi->unit); w2(4); w0(pi->unit);
259 * Possibly wrong, purpose unknown (fiddle with ESS logic ???)
260 * if (pi->devtype == PI_PCD) {
271 static void bpck_disconnect(struct pi_adapter *pi)
279 w2(0x4c); w0(pi->saved_r0);
282 static void bpck_force_spp(struct pi_adapter *pi)
284 /* This fakes the EPP protocol to turn off EPP ... */
286 w0(0xff-pi->unit); w2(4); w0(pi->unit);
292 w0(0); w2(1); w2(3); w2(0);
294 w2(0x4c); w0(pi->saved_r0);
299 static int bpck_test_proto(struct pi_adapter *pi)
312 for (i = 0; i < TEST_LEN; i++) {
323 w0(0x13); t2(2); t2(0x20);
324 for (i = 0; i < TEST_LEN; i++) {
344 w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
348 for (i = 0; i < TEST_LEN; i++)
352 for (i = 0; i < TEST_LEN / 2; i++)
353 ((u16 *)buf)[i] = r4w();
356 for (i = 0; i < TEST_LEN / 4; i++)
357 ((u32 *)buf)[i] = r4l();
368 dev_dbg(&pi->dev, "bpck: 0x%x unit %d mode %d: ",
369 pi->port, pi->unit, pi->mode);
370 print_hex_dump_debug("bpck: ", DUMP_PREFIX_NONE, TEST_LEN, 1, buf,
374 for (i = 0; i < TEST_LEN; i++) {
382 static void bpck_read_eeprom(struct pi_adapter *pi, char *buf)
384 int i, j, k, p, v, f, om, od;
388 om = pi->mode; od = pi->delay;
389 pi->mode = 0; pi->delay = 6;
394 for (i = 0; i < 64; i++) {
398 for (k = 0; k < 9; k++) {
399 f = (((i + 0x180) & p) != 0) * 2;
405 for (j = 0; j < 2; j++) {
407 for (k = 0; k < 8; k++) {
412 v = 2 * v + (f == 0x84);
414 buf[2 * i + 1 - j] = v;
430 pi->mode = om; pi->delay = od;
433 static int bpck_test_port(struct pi_adapter *pi)
437 /* Check for 8-bit port */
438 w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i);
472 static void bpck_log_adapter(struct pi_adapter *pi)
474 char *mode_str[5] = { "4-bit", "8-bit", "EPP-8", "EPP-16", "EPP-32" };
477 bpck_read_eeprom(pi,scratch);
478 print_hex_dump_bytes("bpck EEPROM: ", DUMP_PREFIX_NONE, scratch, 128);
480 "backpack %8.8s unit %d at 0x%x, mode %d (%s), delay %d\n",
481 &scratch[110], pi->unit, pi->port, pi->mode,
482 mode_str[pi->mode], pi->delay);
485 static struct pi_protocol bpck = {
486 .owner = THIS_MODULE,
492 .write_regr = bpck_write_regr,
493 .read_regr = bpck_read_regr,
494 .write_block = bpck_write_block,
495 .read_block = bpck_read_block,
496 .connect = bpck_connect,
497 .disconnect = bpck_disconnect,
498 .test_port = bpck_test_port,
499 .probe_unit = bpck_probe_unit,
500 .test_proto = bpck_test_proto,
501 .log_adapter = bpck_log_adapter,
504 MODULE_LICENSE("GPL");
506 MODULE_DESCRIPTION("MicroSolutions BACKPACK parallel port IDE adapter protocol driver");
507 module_pata_parport_driver(bpck);