1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * This is a low-level driver for the KBIC-951A and KBIC-971A
6 * parallel to IDE adapter chips from KingByte Information Systems.
8 * The chips are almost identical, however, the wakeup code
9 * required for the 971A interferes with the correct operation of
10 * the 951A, so this driver registers itself twice, once for
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/wait.h>
21 #include "pata_parport.h"
23 #define r12w() (delay_p, inw(pi->port + 1) & 0xffff)
25 #define j44(a, b) ((((a >> 4) & 0x0f) | (b & 0xf0)) ^ 0x88)
26 #define j53(w) (((w >> 3) & 0x1f) | ((w >> 4) & 0xe0))
30 * cont = 0 - access the IDE register file
31 * cont = 1 - access the IDE command set
33 static int cont_map[2] = { 0x80, 0x40 };
35 static int kbic_read_regr(struct pi_adapter *pi, int cont, int regr)
43 w0(regr | 0x18 | s); w2(4); w2(6); w2(4); w2(1); w0(8);
44 a = r1(); w0(0x28); b = r1(); w2(4);
47 w0(regr|0x38 | s); w2(4); w2(6); w2(4); w2(5); w0(8);
51 w0(regr | 0x08 | s); w2(4); w2(6); w2(4); w2(0xa5); w2(0xa1);
57 w0(0x20 | s); w2(4); w2(6); w2(4); w3(regr);
58 a = r4(); b = r4(); w2(4); w2(0); w2(4);
65 static void kbic_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
67 int s = cont_map[cont];
73 w0(regr | 0x10 | s); w2(4); w2(6); w2(4);
74 w0(val); w2(5); w2(4);
79 w0(0x20 | s); w2(4); w2(6); w2(4); w3(regr);
86 static void k951_connect(struct pi_adapter *pi)
93 static void k951_disconnect(struct pi_adapter *pi)
101 w2(0xc4); w0(0xaa); w0(0x55); \
102 w0(0); w0(0xff); w0(0x87); \
103 w0(0x78); w0(x); w2(0xc5); \
104 w2(0xc4); w0(0xff); \
107 static void k971_connect(struct pi_adapter *pi)
115 static void k971_disconnect(struct pi_adapter *pi)
123 * count must be congruent to 0 MOD 4, but all known applications
126 static void kbic_read_block(struct pi_adapter *pi, char *buf, int count)
132 w0(0x98); w2(4); w2(6); w2(4);
133 for (k = 0; k < count / 2; k++) {
138 buf[2 * k] = j44(a, b);
143 buf[2 * k + 1] = j44(a, b);
148 w0(0xb8); w2(4); w2(6); w2(4);
149 for (k = 0; k < count / 4; k++) {
153 buf[4 * k] = j53(r12w());
155 buf[4 * k + 1] = j53(r12w());
157 buf[4 * k + 3] = j53(r12w());
159 buf[4 * k + 2] = j53(r12w());
164 w0(0x88); w2(4); w2(6); w2(4);
165 for (k = 0; k < count / 2; k++) {
169 buf[2 * k + 1] = r0();
174 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
175 for (k = 0; k < count; k++)
180 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
181 for (k = 0; k < count / 2; k++)
182 ((u16 *)buf)[k] = r4w();
186 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
187 for (k = 0; k < count / 4; k++)
188 ((u32 *)buf)[k] = r4l();
194 static void kbic_write_block(struct pi_adapter *pi, char *buf, int count)
202 w0(0x90); w2(4); w2(6); w2(4);
203 for (k = 0; k < count / 2; k++) {
211 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
212 for (k = 0; k < count / 2; k++) {
219 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
220 for (k = 0; k < count / 2; k++)
221 w4w(swab16(((u16 *)buf)[k]));
225 w0(0xa0); w2(4); w2(6); w2(4); w3(0);
226 for (k = 0; k < count / 4; k++)
227 w4l(swab16(((u16 *)buf)[2 * k]) |
228 swab16(((u16 *)buf)[2 * k + 1]) << 16);
234 static void kbic_log_adapter(struct pi_adapter *pi, char *chip)
236 char *mode[6] = { "4-bit", "5/3", "8-bit", "EPP-8", "EPP_16", "EPP-32"};
238 dev_info(&pi->dev, "KingByte %s at 0x%x, mode %d (%s), delay %d\n",
239 chip, pi->port, pi->mode, mode[pi->mode], pi->delay);
242 static void k951_log_adapter(struct pi_adapter *pi)
244 kbic_log_adapter(pi, "KBIC-951A");
247 static void k971_log_adapter(struct pi_adapter *pi)
249 kbic_log_adapter(pi, "KBIC-971A");
252 static struct pi_protocol k951 = {
253 .owner = THIS_MODULE,
259 .write_regr = kbic_write_regr,
260 .read_regr = kbic_read_regr,
261 .write_block = kbic_write_block,
262 .read_block = kbic_read_block,
263 .connect = k951_connect,
264 .disconnect = k951_disconnect,
265 .log_adapter = k951_log_adapter,
268 static struct pi_protocol k971 = {
269 .owner = THIS_MODULE,
275 .write_regr = kbic_write_regr,
276 .read_regr = kbic_read_regr,
277 .write_block = kbic_write_block,
278 .read_block = kbic_read_block,
279 .connect = k971_connect,
280 .disconnect = k971_disconnect,
281 .log_adapter = k971_log_adapter,
284 static int __init kbic_init(void)
288 rv = pata_parport_register_driver(&k951);
291 rv = pata_parport_register_driver(&k971);
293 pata_parport_unregister_driver(&k951);
297 static void __exit kbic_exit(void)
299 pata_parport_unregister_driver(&k951);
300 pata_parport_unregister_driver(&k971);
303 MODULE_LICENSE("GPL");
305 MODULE_DESCRIPTION("KingByte Information Systems KBIC-951A and KBIC-971A "
306 "parallel port IDE adapter protocol driver");
307 module_init(kbic_init)
308 module_exit(kbic_exit)