xref: /openbmc/linux/drivers/ata/pata_parport/kbic.c (revision cec148c2)
165db10caSDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
272f2b0b2SOndrej Zary /*
365db10caSDamien Le Moal  * (c) 1997-1998  Grant R. Guenther <grant@torque.net>
465db10caSDamien Le Moal  *
565db10caSDamien Le Moal  * This is a low-level driver for the KBIC-951A and KBIC-971A
665db10caSDamien Le Moal  * parallel to IDE adapter chips from KingByte Information Systems.
765db10caSDamien Le Moal  *
865db10caSDamien Le Moal  * The chips are almost identical, however, the wakeup code
965db10caSDamien Le Moal  * required for the 971A interferes with the correct operation of
1065db10caSDamien Le Moal  * the 951A, so this driver registers itself twice, once for
1165db10caSDamien Le Moal  * each chip.
1272f2b0b2SOndrej Zary  */
1372f2b0b2SOndrej Zary 
1472f2b0b2SOndrej Zary #include <linux/module.h>
1572f2b0b2SOndrej Zary #include <linux/init.h>
1672f2b0b2SOndrej Zary #include <linux/delay.h>
1772f2b0b2SOndrej Zary #include <linux/kernel.h>
1872f2b0b2SOndrej Zary #include <linux/types.h>
1972f2b0b2SOndrej Zary #include <linux/wait.h>
2072f2b0b2SOndrej Zary #include <asm/io.h>
21fe027ff9SOndrej Zary #include "pata_parport.h"
2272f2b0b2SOndrej Zary 
2372f2b0b2SOndrej Zary #define r12w()		(delay_p, inw(pi->port + 1) & 0xffff)
2472f2b0b2SOndrej Zary 
2572f2b0b2SOndrej Zary #define j44(a, b)	((((a >> 4) & 0x0f) | (b & 0xf0)) ^ 0x88)
2672f2b0b2SOndrej Zary #define j53(w)		(((w >> 3) & 0x1f) | ((w >> 4) & 0xe0))
2772f2b0b2SOndrej Zary 
2872f2b0b2SOndrej Zary 
2965db10caSDamien Le Moal /*
3065db10caSDamien Le Moal  * cont = 0 - access the IDE register file
3165db10caSDamien Le Moal  * cont = 1 - access the IDE command set
3272f2b0b2SOndrej Zary  */
3372f2b0b2SOndrej Zary static int cont_map[2] = { 0x80, 0x40 };
3472f2b0b2SOndrej Zary 
kbic_read_regr(struct pi_adapter * pi,int cont,int regr)35882ff0caSOndrej Zary static int kbic_read_regr(struct pi_adapter *pi, int cont, int regr)
3665db10caSDamien Le Moal {
3765db10caSDamien Le Moal 	int a, b, s;
3872f2b0b2SOndrej Zary 
3972f2b0b2SOndrej Zary 	s = cont_map[cont];
4072f2b0b2SOndrej Zary 
4172f2b0b2SOndrej Zary 	switch (pi->mode) {
4265db10caSDamien Le Moal 	case 0:
4365db10caSDamien Le Moal 		w0(regr | 0x18 | s); w2(4); w2(6); w2(4); w2(1); w0(8);
4472f2b0b2SOndrej Zary 		a = r1(); w0(0x28); b = r1(); w2(4);
4572f2b0b2SOndrej Zary 		return j44(a, b);
4665db10caSDamien Le Moal 	case 1:
4765db10caSDamien Le Moal 		w0(regr|0x38 | s); w2(4); w2(6); w2(4); w2(5); w0(8);
4872f2b0b2SOndrej Zary 		a = r12w(); w2(4);
4972f2b0b2SOndrej Zary 		return j53(a);
5065db10caSDamien Le Moal 	case 2:
5165db10caSDamien Le Moal 		w0(regr | 0x08 | s); w2(4); w2(6); w2(4); w2(0xa5); w2(0xa1);
5272f2b0b2SOndrej Zary 		a = r0(); w2(4);
5372f2b0b2SOndrej Zary 		return a;
5472f2b0b2SOndrej Zary 	case 3:
5572f2b0b2SOndrej Zary 	case 4:
5665db10caSDamien Le Moal 	case 5:
5765db10caSDamien Le Moal 		w0(0x20 | s); w2(4); w2(6); w2(4); w3(regr);
5872f2b0b2SOndrej Zary 		a = r4(); b = r4(); w2(4); w2(0); w2(4);
5972f2b0b2SOndrej Zary 		return a;
6072f2b0b2SOndrej Zary 	}
6165db10caSDamien Le Moal 
6272f2b0b2SOndrej Zary 	return -1;
6372f2b0b2SOndrej Zary }
6472f2b0b2SOndrej Zary 
kbic_write_regr(struct pi_adapter * pi,int cont,int regr,int val)65882ff0caSOndrej Zary static void kbic_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
6665db10caSDamien Le Moal {
6765db10caSDamien Le Moal 	int s = cont_map[cont];
6872f2b0b2SOndrej Zary 
6972f2b0b2SOndrej Zary 	switch (pi->mode) {
7072f2b0b2SOndrej Zary 	case 0:
7172f2b0b2SOndrej Zary 	case 1:
7265db10caSDamien Le Moal 	case 2:
7365db10caSDamien Le Moal 		w0(regr | 0x10 | s); w2(4); w2(6); w2(4);
7472f2b0b2SOndrej Zary 		w0(val); w2(5); w2(4);
7572f2b0b2SOndrej Zary 		break;
7672f2b0b2SOndrej Zary 	case 3:
7772f2b0b2SOndrej Zary 	case 4:
7865db10caSDamien Le Moal 	case 5:
7965db10caSDamien Le Moal 		w0(0x20 | s); w2(4); w2(6); w2(4); w3(regr);
8072f2b0b2SOndrej Zary 		w4(val); w4(val);
8172f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
8272f2b0b2SOndrej Zary 		break;
8372f2b0b2SOndrej Zary 	}
8472f2b0b2SOndrej Zary }
8572f2b0b2SOndrej Zary 
k951_connect(struct pi_adapter * pi)86882ff0caSOndrej Zary static void k951_connect(struct pi_adapter *pi)
8765db10caSDamien Le Moal {
8865db10caSDamien Le Moal 	pi->saved_r0 = r0();
8972f2b0b2SOndrej Zary 	pi->saved_r2 = r2();
9072f2b0b2SOndrej Zary 	w2(4);
9172f2b0b2SOndrej Zary }
9272f2b0b2SOndrej Zary 
k951_disconnect(struct pi_adapter * pi)93882ff0caSOndrej Zary static void k951_disconnect(struct pi_adapter *pi)
9465db10caSDamien Le Moal {
9565db10caSDamien Le Moal 	w0(pi->saved_r0);
9672f2b0b2SOndrej Zary 	w2(pi->saved_r2);
9772f2b0b2SOndrej Zary }
9872f2b0b2SOndrej Zary 
9965db10caSDamien Le Moal #define	CCP(x)						\
10065db10caSDamien Le Moal 	do {						\
10165db10caSDamien Le Moal 		w2(0xc4); w0(0xaa); w0(0x55);		\
10265db10caSDamien Le Moal 		w0(0); w0(0xff); w0(0x87);		\
10365db10caSDamien Le Moal 		w0(0x78); w0(x); w2(0xc5);		\
10465db10caSDamien Le Moal 		w2(0xc4); w0(0xff);			\
10565db10caSDamien Le Moal 	} while (0)
10672f2b0b2SOndrej Zary 
k971_connect(struct pi_adapter * pi)107882ff0caSOndrej Zary static void k971_connect(struct pi_adapter *pi)
10865db10caSDamien Le Moal {
10965db10caSDamien Le Moal 	pi->saved_r0 = r0();
11072f2b0b2SOndrej Zary 	pi->saved_r2 = r2();
11172f2b0b2SOndrej Zary 	CCP(0x20);
11272f2b0b2SOndrej Zary 	w2(4);
11372f2b0b2SOndrej Zary }
11472f2b0b2SOndrej Zary 
k971_disconnect(struct pi_adapter * pi)115882ff0caSOndrej Zary static void k971_disconnect(struct pi_adapter *pi)
11665db10caSDamien Le Moal {
11765db10caSDamien Le Moal 	CCP(0x30);
11872f2b0b2SOndrej Zary 	w0(pi->saved_r0);
11972f2b0b2SOndrej Zary 	w2(pi->saved_r2);
12072f2b0b2SOndrej Zary }
12172f2b0b2SOndrej Zary 
12265db10caSDamien Le Moal /*
12365db10caSDamien Le Moal  * count must be congruent to 0 MOD 4, but all known applications
12465db10caSDamien Le Moal  *have this property.
12572f2b0b2SOndrej Zary  */
kbic_read_block(struct pi_adapter * pi,char * buf,int count)126882ff0caSOndrej Zary static void kbic_read_block(struct pi_adapter *pi, char *buf, int count)
12765db10caSDamien Le Moal {
12865db10caSDamien Le Moal 	int k, a, b;
12972f2b0b2SOndrej Zary 
13072f2b0b2SOndrej Zary 	switch (pi->mode) {
13165db10caSDamien Le Moal 	case 0:
13265db10caSDamien Le Moal 		w0(0x98); w2(4); w2(6); w2(4);
13372f2b0b2SOndrej Zary 		for (k = 0; k < count / 2; k++) {
13465db10caSDamien Le Moal 			w2(1); w0(8);
13565db10caSDamien Le Moal 			a = r1();
13665db10caSDamien Le Moal 			w0(0x28);
13765db10caSDamien Le Moal 			b = r1();
13872f2b0b2SOndrej Zary 			buf[2 * k] = j44(a, b);
13965db10caSDamien Le Moal 			w2(5);
14065db10caSDamien Le Moal 			b = r1();
14165db10caSDamien Le Moal 			w0(8);
14265db10caSDamien Le Moal 			a = r1();
14372f2b0b2SOndrej Zary 			buf[2 * k + 1] = j44(a, b);
14472f2b0b2SOndrej Zary 			w2(4);
14572f2b0b2SOndrej Zary 		}
14672f2b0b2SOndrej Zary 		break;
14765db10caSDamien Le Moal 	case 1:
14865db10caSDamien Le Moal 		w0(0xb8); w2(4); w2(6); w2(4);
14972f2b0b2SOndrej Zary 		for (k = 0; k < count / 4; k++) {
15072f2b0b2SOndrej Zary 			w0(0xb8);
15172f2b0b2SOndrej Zary 			w2(4); w2(5);
15265db10caSDamien Le Moal 			w0(8);
15365db10caSDamien Le Moal 			buf[4 * k] = j53(r12w());
15465db10caSDamien Le Moal 			w0(0xb8);
15565db10caSDamien Le Moal 			buf[4 * k + 1] = j53(r12w());
15672f2b0b2SOndrej Zary 			w2(4); w2(5);
15772f2b0b2SOndrej Zary 			buf[4 * k + 3] = j53(r12w());
15865db10caSDamien Le Moal 			w0(8);
15965db10caSDamien Le Moal 			buf[4 * k + 2] = j53(r12w());
16072f2b0b2SOndrej Zary 		}
16172f2b0b2SOndrej Zary 		w2(4);
16272f2b0b2SOndrej Zary 		break;
16365db10caSDamien Le Moal 	case 2:
16465db10caSDamien Le Moal 		w0(0x88); w2(4); w2(6); w2(4);
16572f2b0b2SOndrej Zary 		for (k = 0; k < count / 2; k++) {
16665db10caSDamien Le Moal 			w2(0xa0); w2(0xa1);
16765db10caSDamien Le Moal 			buf[2 * k] = r0();
16865db10caSDamien Le Moal 			w2(0xa5);
16965db10caSDamien Le Moal 			buf[2 * k + 1] = r0();
17072f2b0b2SOndrej Zary 		}
17172f2b0b2SOndrej Zary 		w2(4);
17272f2b0b2SOndrej Zary 		break;
17365db10caSDamien Le Moal 	case 3:
17465db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
17565db10caSDamien Le Moal 		for (k = 0; k < count; k++)
17665db10caSDamien Le Moal 			buf[k] = r4();
17772f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
17872f2b0b2SOndrej Zary 		break;
17965db10caSDamien Le Moal 	case 4:
18065db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
18165db10caSDamien Le Moal 		for (k = 0; k < count / 2; k++)
18265db10caSDamien Le Moal 			((u16 *)buf)[k] = r4w();
18372f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
18472f2b0b2SOndrej Zary 		break;
18565db10caSDamien Le Moal 	case 5:
18665db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
18765db10caSDamien Le Moal 		for (k = 0; k < count / 4; k++)
18865db10caSDamien Le Moal 			((u32 *)buf)[k] = r4l();
18972f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
19072f2b0b2SOndrej Zary 		break;
19172f2b0b2SOndrej Zary 	}
19272f2b0b2SOndrej Zary }
19372f2b0b2SOndrej Zary 
kbic_write_block(struct pi_adapter * pi,char * buf,int count)194882ff0caSOndrej Zary static void kbic_write_block(struct pi_adapter *pi, char *buf, int count)
19565db10caSDamien Le Moal {
19665db10caSDamien Le Moal 	int k;
19772f2b0b2SOndrej Zary 
19872f2b0b2SOndrej Zary 	switch (pi->mode) {
19972f2b0b2SOndrej Zary 	case 0:
20072f2b0b2SOndrej Zary 	case 1:
20165db10caSDamien Le Moal 	case 2:
20265db10caSDamien Le Moal 		w0(0x90); w2(4); w2(6); w2(4);
20372f2b0b2SOndrej Zary 		for (k = 0; k < count / 2; k++) {
20465db10caSDamien Le Moal 			w0(buf[2 * k + 1]);
20565db10caSDamien Le Moal 			w2(0); w2(4);
20665db10caSDamien Le Moal 			w0(buf[2 * k]);
20765db10caSDamien Le Moal 			w2(5); w2(4);
20872f2b0b2SOndrej Zary 		}
20972f2b0b2SOndrej Zary 		break;
21065db10caSDamien Le Moal 	case 3:
21165db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
21272f2b0b2SOndrej Zary 		for (k = 0; k < count / 2; k++) {
21372f2b0b2SOndrej Zary 			w4(buf[2 * k + 1]);
21472f2b0b2SOndrej Zary 			w4(buf[2 * k]);
21572f2b0b2SOndrej Zary 		}
21672f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
21772f2b0b2SOndrej Zary 		break;
21865db10caSDamien Le Moal 	case 4:
21965db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
220e02c625dSOndrej Zary 		for (k = 0; k < count / 2; k++)
221e02c625dSOndrej Zary 			w4w(swab16(((u16 *)buf)[k]));
22272f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
22372f2b0b2SOndrej Zary 		break;
22465db10caSDamien Le Moal 	case 5:
22565db10caSDamien Le Moal 		w0(0xa0); w2(4); w2(6); w2(4); w3(0);
226e02c625dSOndrej Zary 		for (k = 0; k < count / 4; k++)
227e02c625dSOndrej Zary 			w4l(swab16(((u16 *)buf)[2 * k]) |
228e02c625dSOndrej Zary 			    swab16(((u16 *)buf)[2 * k + 1]) << 16);
22972f2b0b2SOndrej Zary 		w2(4); w2(0); w2(4);
23072f2b0b2SOndrej Zary 		break;
23172f2b0b2SOndrej Zary 	}
23272f2b0b2SOndrej Zary }
23372f2b0b2SOndrej Zary 
kbic_log_adapter(struct pi_adapter * pi,char * chip)2345b77db9cSOndrej Zary static void kbic_log_adapter(struct pi_adapter *pi, char *chip)
23565db10caSDamien Le Moal {
23665db10caSDamien Le Moal 	char *mode[6] = { "4-bit", "5/3", "8-bit", "EPP-8", "EPP_16", "EPP-32"};
23772f2b0b2SOndrej Zary 
238426eb3c5SOndrej Zary 	dev_info(&pi->dev, "KingByte %s at 0x%x, mode %d (%s), delay %d\n",
23965db10caSDamien Le Moal 		 chip, pi->port, pi->mode, mode[pi->mode], pi->delay);
24072f2b0b2SOndrej Zary }
24172f2b0b2SOndrej Zary 
k951_log_adapter(struct pi_adapter * pi)2425b77db9cSOndrej Zary static void k951_log_adapter(struct pi_adapter *pi)
2433a7474baSOndrej Zary {
2445b77db9cSOndrej Zary 	kbic_log_adapter(pi, "KBIC-951A");
24572f2b0b2SOndrej Zary }
24672f2b0b2SOndrej Zary 
k971_log_adapter(struct pi_adapter * pi)2475b77db9cSOndrej Zary static void k971_log_adapter(struct pi_adapter *pi)
2483a7474baSOndrej Zary {
2495b77db9cSOndrej Zary 	kbic_log_adapter(pi, "KBIC-971A");
25072f2b0b2SOndrej Zary }
25172f2b0b2SOndrej Zary 
25272f2b0b2SOndrej Zary static struct pi_protocol k951 = {
25372f2b0b2SOndrej Zary 	.owner		= THIS_MODULE,
25472f2b0b2SOndrej Zary 	.name		= "k951",
25572f2b0b2SOndrej Zary 	.max_mode	= 6,
25672f2b0b2SOndrej Zary 	.epp_first	= 3,
25772f2b0b2SOndrej Zary 	.default_delay	= 1,
25872f2b0b2SOndrej Zary 	.max_units	= 1,
25972f2b0b2SOndrej Zary 	.write_regr	= kbic_write_regr,
26072f2b0b2SOndrej Zary 	.read_regr	= kbic_read_regr,
26172f2b0b2SOndrej Zary 	.write_block	= kbic_write_block,
26272f2b0b2SOndrej Zary 	.read_block	= kbic_read_block,
26372f2b0b2SOndrej Zary 	.connect	= k951_connect,
26472f2b0b2SOndrej Zary 	.disconnect	= k951_disconnect,
26572f2b0b2SOndrej Zary 	.log_adapter	= k951_log_adapter,
26672f2b0b2SOndrej Zary };
26772f2b0b2SOndrej Zary 
26872f2b0b2SOndrej Zary static struct pi_protocol k971 = {
26972f2b0b2SOndrej Zary 	.owner		= THIS_MODULE,
27072f2b0b2SOndrej Zary 	.name		= "k971",
27172f2b0b2SOndrej Zary 	.max_mode	= 6,
27272f2b0b2SOndrej Zary 	.epp_first	= 3,
27372f2b0b2SOndrej Zary 	.default_delay	= 1,
27472f2b0b2SOndrej Zary 	.max_units	= 1,
27572f2b0b2SOndrej Zary 	.write_regr	= kbic_write_regr,
27672f2b0b2SOndrej Zary 	.read_regr	= kbic_read_regr,
27772f2b0b2SOndrej Zary 	.write_block	= kbic_write_block,
27872f2b0b2SOndrej Zary 	.read_block	= kbic_read_block,
27972f2b0b2SOndrej Zary 	.connect	= k971_connect,
28072f2b0b2SOndrej Zary 	.disconnect	= k971_disconnect,
28172f2b0b2SOndrej Zary 	.log_adapter	= k971_log_adapter,
28272f2b0b2SOndrej Zary };
28372f2b0b2SOndrej Zary 
kbic_init(void)28472f2b0b2SOndrej Zary static int __init kbic_init(void)
28572f2b0b2SOndrej Zary {
28672f2b0b2SOndrej Zary 	int rv;
28772f2b0b2SOndrej Zary 
2882c08ec0fSOndrej Zary 	rv = pata_parport_register_driver(&k951);
28972f2b0b2SOndrej Zary 	if (rv < 0)
29072f2b0b2SOndrej Zary 		return rv;
2912c08ec0fSOndrej Zary 	rv = pata_parport_register_driver(&k971);
29272f2b0b2SOndrej Zary 	if (rv < 0)
2932c08ec0fSOndrej Zary 		pata_parport_unregister_driver(&k951);
29472f2b0b2SOndrej Zary 	return rv;
29572f2b0b2SOndrej Zary }
29672f2b0b2SOndrej Zary 
kbic_exit(void)29772f2b0b2SOndrej Zary static void __exit kbic_exit(void)
29872f2b0b2SOndrej Zary {
2992c08ec0fSOndrej Zary 	pata_parport_unregister_driver(&k951);
3002c08ec0fSOndrej Zary 	pata_parport_unregister_driver(&k971);
30172f2b0b2SOndrej Zary }
30272f2b0b2SOndrej Zary 
30372f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
304*cec148c2SDamien Le Moal MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>");
305*cec148c2SDamien Le Moal MODULE_DESCRIPTION("KingByte Information Systems KBIC-951A and KBIC-971A "
306*cec148c2SDamien Le Moal 		   "parallel port IDE adapter protocol driver");
30772f2b0b2SOndrej Zary module_init(kbic_init)
30872f2b0b2SOndrej Zary module_exit(kbic_exit)
309