xref: /openbmc/linux/drivers/ata/pata_parport/epia.c (revision cec148c2)
18be31971SDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
272f2b0b2SOndrej Zary /*
38be31971SDamien Le Moal  * (c) 1997-1998  Grant R. Guenther <grant@torque.net>
48be31971SDamien Le Moal  *
58be31971SDamien Le Moal  * epia.c is a low-level protocol driver for Shuttle Technologies
68be31971SDamien Le Moal  * EPIA parallel to IDE adapter chip.  This device is now obsolete
78be31971SDamien Le Moal  * and has been replaced with the EPAT chip, which is supported
88be31971SDamien Le Moal  * by epat.c, however, some devices based on EPIA are still
98be31971SDamien Le Moal  * available.
1072f2b0b2SOndrej Zary  */
1172f2b0b2SOndrej Zary 
1272f2b0b2SOndrej Zary #include <linux/module.h>
1372f2b0b2SOndrej Zary #include <linux/init.h>
1472f2b0b2SOndrej Zary #include <linux/delay.h>
1572f2b0b2SOndrej Zary #include <linux/kernel.h>
1672f2b0b2SOndrej Zary #include <linux/types.h>
1772f2b0b2SOndrej Zary #include <linux/wait.h>
1872f2b0b2SOndrej Zary #include <asm/io.h>
19fe027ff9SOndrej Zary #include "pata_parport.h"
2072f2b0b2SOndrej Zary 
218be31971SDamien Le Moal /*
228be31971SDamien Le Moal  * mode codes:  0  nybble reads on port 1, 8-bit writes
238be31971SDamien Le Moal  *		1  5/3 reads on ports 1 & 2, 8-bit writes
248be31971SDamien Le Moal  *		2  8-bit reads and writes
258be31971SDamien Le Moal  *		3  8-bit EPP mode
268be31971SDamien Le Moal  *		4  16-bit EPP
278be31971SDamien Le Moal  *		5  32-bit EPP
2872f2b0b2SOndrej Zary  */
2972f2b0b2SOndrej Zary 
3072f2b0b2SOndrej Zary #define j44(a, b)	(((a >> 4) & 0x0f) + (b & 0xf0))
3172f2b0b2SOndrej Zary #define j53(a, b)	(((a >> 3) & 0x1f) + ((b << 4) & 0xe0))
3272f2b0b2SOndrej Zary 
338be31971SDamien Le Moal /*
348be31971SDamien Le Moal  * cont =  0   IDE register file
358be31971SDamien Le Moal  * cont =  1   IDE control registers
3672f2b0b2SOndrej Zary  */
3772f2b0b2SOndrej Zary static int cont_map[2] = { 0, 0x80 };
3872f2b0b2SOndrej Zary 
epia_read_regr(struct pi_adapter * pi,int cont,int regr)39882ff0caSOndrej Zary static int epia_read_regr(struct pi_adapter *pi, int cont, int regr)
408be31971SDamien Le Moal {
418be31971SDamien Le Moal 	int a, b, r;
4272f2b0b2SOndrej Zary 
4372f2b0b2SOndrej Zary 	regr += cont_map[cont];
4472f2b0b2SOndrej Zary 
4572f2b0b2SOndrej Zary 	switch (pi->mode)  {
468be31971SDamien Le Moal 	case 0:
478be31971SDamien Le Moal 		r = regr ^ 0x39;
4872f2b0b2SOndrej Zary 		w0(r); w2(1); w2(3); w0(r);
4972f2b0b2SOndrej Zary 		a = r1(); w2(1); b = r1(); w2(4);
5072f2b0b2SOndrej Zary 		return j44(a, b);
518be31971SDamien Le Moal 	case 1:
528be31971SDamien Le Moal 		r = regr ^ 0x31;
5372f2b0b2SOndrej Zary 		w0(r); w2(1); w0(r & 0x37);
5472f2b0b2SOndrej Zary 		w2(3); w2(5); w0(r | 0xf0);
5572f2b0b2SOndrej Zary 		a = r1(); b = r2(); w2(4);
5672f2b0b2SOndrej Zary 		return j53(a, b);
578be31971SDamien Le Moal 	case 2:
588be31971SDamien Le Moal 		r = regr^0x29;
5972f2b0b2SOndrej Zary 		w0(r); w2(1); w2(0X21); w2(0x23);
6072f2b0b2SOndrej Zary 		a = r0(); w2(4);
6172f2b0b2SOndrej Zary 		return a;
6272f2b0b2SOndrej Zary 	case 3:
6372f2b0b2SOndrej Zary 	case 4:
648be31971SDamien Le Moal 	case 5:
658be31971SDamien Le Moal 		w3(regr); w2(0x24); a = r4(); w2(4);
6672f2b0b2SOndrej Zary 		return a;
6772f2b0b2SOndrej Zary 	}
688be31971SDamien Le Moal 
6972f2b0b2SOndrej Zary 	return -1;
7072f2b0b2SOndrej Zary }
7172f2b0b2SOndrej Zary 
epia_write_regr(struct pi_adapter * pi,int cont,int regr,int val)72882ff0caSOndrej Zary static void epia_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
738be31971SDamien Le Moal {
748be31971SDamien Le Moal 	int  r;
7572f2b0b2SOndrej Zary 
7672f2b0b2SOndrej Zary 	regr += cont_map[cont];
7772f2b0b2SOndrej Zary 
7872f2b0b2SOndrej Zary 	switch (pi->mode)  {
7972f2b0b2SOndrej Zary 	case 0:
8072f2b0b2SOndrej Zary 	case 1:
818be31971SDamien Le Moal 	case 2:
828be31971SDamien Le Moal 		r = regr ^ 0x19;
8372f2b0b2SOndrej Zary 		w0(r); w2(1); w0(val); w2(3); w2(4);
8472f2b0b2SOndrej Zary 		break;
8572f2b0b2SOndrej Zary 	case 3:
8672f2b0b2SOndrej Zary 	case 4:
878be31971SDamien Le Moal 	case 5:
888be31971SDamien Le Moal 		r = regr ^ 0x40;
8972f2b0b2SOndrej Zary 		w3(r); w4(val); w2(4);
9072f2b0b2SOndrej Zary 		break;
9172f2b0b2SOndrej Zary 	}
9272f2b0b2SOndrej Zary }
9372f2b0b2SOndrej Zary 
9472f2b0b2SOndrej Zary #define WR(r, v)	epia_write_regr(pi, 0, r, v)
958be31971SDamien Le Moal #define RR(r)		epia_read_regr(pi, 0, r)
9672f2b0b2SOndrej Zary 
978be31971SDamien Le Moal /*
988be31971SDamien Le Moal  * The use of register 0x84 is entirely unclear - it seems to control
998be31971SDamien Le Moal  * some EPP counters ...  currently we know about 3 different block
1008be31971SDamien Le Moal  * sizes:  the standard 512 byte reads and writes, 12 byte writes and
1018be31971SDamien Le Moal  * 2048 byte reads (the last two being used in the CDrom drivers.
10272f2b0b2SOndrej Zary  */
epia_connect(struct pi_adapter * pi)103882ff0caSOndrej Zary static void epia_connect(struct pi_adapter *pi)
1048be31971SDamien Le Moal {
1058be31971SDamien Le Moal 	pi->saved_r0 = r0();
10672f2b0b2SOndrej Zary 	pi->saved_r2 = r2();
10772f2b0b2SOndrej Zary 
10872f2b0b2SOndrej Zary 	w2(4); w0(0xa0); w0(0x50); w0(0xc0); w0(0x30); w0(0xa0); w0(0);
10972f2b0b2SOndrej Zary 	w2(1); w2(4);
11072f2b0b2SOndrej Zary 	if (pi->mode >= 3) {
11172f2b0b2SOndrej Zary 		w0(0xa); w2(1); w2(4); w0(0x82); w2(4); w2(0xc); w2(4);
11272f2b0b2SOndrej Zary 		w2(0x24); w2(0x26); w2(4);
11372f2b0b2SOndrej Zary 	}
11472f2b0b2SOndrej Zary 	WR(0x86, 8);
11572f2b0b2SOndrej Zary }
11672f2b0b2SOndrej Zary 
epia_disconnect(struct pi_adapter * pi)117882ff0caSOndrej Zary static void epia_disconnect(struct pi_adapter *pi)
1188be31971SDamien Le Moal {
1198be31971SDamien Le Moal 	/* WR(0x84,0x10); */
12072f2b0b2SOndrej Zary 	w0(pi->saved_r0);
12172f2b0b2SOndrej Zary 	w2(1); w2(4);
12272f2b0b2SOndrej Zary 	w0(pi->saved_r0);
12372f2b0b2SOndrej Zary 	w2(pi->saved_r2);
12472f2b0b2SOndrej Zary }
12572f2b0b2SOndrej Zary 
epia_read_block(struct pi_adapter * pi,char * buf,int count)126882ff0caSOndrej Zary static void epia_read_block(struct pi_adapter *pi, char *buf, int count)
12772f2b0b2SOndrej Zary 
1288be31971SDamien Le Moal {
1298be31971SDamien Le Moal 	int k, ph, a, b;
13072f2b0b2SOndrej Zary 
13172f2b0b2SOndrej Zary 	switch (pi->mode) {
1328be31971SDamien Le Moal 	case 0:
1338be31971SDamien Le Moal 		w0(0x81); w2(1); w2(3); w0(0xc1);
13472f2b0b2SOndrej Zary 		ph = 1;
13572f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
13672f2b0b2SOndrej Zary 			w2(2+ph); a = r1();
13772f2b0b2SOndrej Zary 			w2(4+ph); b = r1();
13872f2b0b2SOndrej Zary 			buf[k] = j44(a, b);
13972f2b0b2SOndrej Zary 			ph = 1 - ph;
14072f2b0b2SOndrej Zary 		}
14172f2b0b2SOndrej Zary 		w0(0); w2(4);
14272f2b0b2SOndrej Zary 		break;
1438be31971SDamien Le Moal 	case 1:
1448be31971SDamien Le Moal 		w0(0x91); w2(1); w0(0x10); w2(3);
14572f2b0b2SOndrej Zary 		w0(0x51); w2(5); w0(0xd1);
14672f2b0b2SOndrej Zary 		ph = 1;
14772f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
14872f2b0b2SOndrej Zary 			w2(4 + ph);
14972f2b0b2SOndrej Zary 			a = r1(); b = r2();
15072f2b0b2SOndrej Zary 			buf[k] = j53(a, b);
15172f2b0b2SOndrej Zary 			ph = 1 - ph;
15272f2b0b2SOndrej Zary 		}
15372f2b0b2SOndrej Zary 		w0(0); w2(4);
15472f2b0b2SOndrej Zary 		break;
1558be31971SDamien Le Moal 	case 2:
1568be31971SDamien Le Moal 		w0(0x89); w2(1); w2(0x23); w2(0x21);
15772f2b0b2SOndrej Zary 		ph = 1;
15872f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
15972f2b0b2SOndrej Zary 			w2(0x24 + ph);
16072f2b0b2SOndrej Zary 			buf[k] = r0();
16172f2b0b2SOndrej Zary 			ph = 1 - ph;
16272f2b0b2SOndrej Zary 		}
16372f2b0b2SOndrej Zary 		w2(6); w2(4);
16472f2b0b2SOndrej Zary 		break;
1658be31971SDamien Le Moal 	case 3:
1668be31971SDamien Le Moal 		if (count > 512)
1678be31971SDamien Le Moal 			WR(0x84, 3);
16872f2b0b2SOndrej Zary 		w3(0); w2(0x24);
1698be31971SDamien Le Moal 		for (k = 0; k < count; k++)
1708be31971SDamien Le Moal 			buf[k] = r4();
17172f2b0b2SOndrej Zary 		w2(4); WR(0x84, 0);
17272f2b0b2SOndrej Zary 		break;
1738be31971SDamien Le Moal 	case 4:
1748be31971SDamien Le Moal 		if (count > 512)
1758be31971SDamien Le Moal 			WR(0x84, 3);
17672f2b0b2SOndrej Zary 		w3(0); w2(0x24);
1778be31971SDamien Le Moal 		for (k = 0; k < count / 2; k++)
1788be31971SDamien Le Moal 			((u16 *)buf)[k] = r4w();
17972f2b0b2SOndrej Zary 		w2(4); WR(0x84, 0);
18072f2b0b2SOndrej Zary 		break;
1818be31971SDamien Le Moal 	case 5:
1828be31971SDamien Le Moal 		if (count > 512)
1838be31971SDamien Le Moal 			WR(0x84, 3);
18472f2b0b2SOndrej Zary 		w3(0); w2(0x24);
1858be31971SDamien Le Moal 		for (k = 0; k < count / 4; k++)
1868be31971SDamien Le Moal 			((u32 *)buf)[k] = r4l();
18772f2b0b2SOndrej Zary 		w2(4); WR(0x84, 0);
18872f2b0b2SOndrej Zary 		break;
18972f2b0b2SOndrej Zary 	}
19072f2b0b2SOndrej Zary }
19172f2b0b2SOndrej Zary 
epia_write_block(struct pi_adapter * pi,char * buf,int count)192882ff0caSOndrej Zary static void epia_write_block(struct pi_adapter *pi, char *buf, int count)
1938be31971SDamien Le Moal {
1948be31971SDamien Le Moal 	int ph, k, last, d;
19572f2b0b2SOndrej Zary 
19672f2b0b2SOndrej Zary 	switch (pi->mode) {
19772f2b0b2SOndrej Zary 	case 0:
19872f2b0b2SOndrej Zary 	case 1:
1998be31971SDamien Le Moal 	case 2:
2008be31971SDamien Le Moal 		w0(0xa1); w2(1); w2(3); w2(1); w2(5);
20172f2b0b2SOndrej Zary 		ph = 0;  last = 0x8000;
20272f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
20372f2b0b2SOndrej Zary 			d = buf[k];
2048be31971SDamien Le Moal 			if (d != last) {
2058be31971SDamien Le Moal 				last = d;
2068be31971SDamien Le Moal 				w0(d);
2078be31971SDamien Le Moal 			}
20872f2b0b2SOndrej Zary 			w2(4 + ph);
20972f2b0b2SOndrej Zary 			ph = 1 - ph;
21072f2b0b2SOndrej Zary 		}
21172f2b0b2SOndrej Zary 		w2(7); w2(4);
21272f2b0b2SOndrej Zary 		break;
2138be31971SDamien Le Moal 	case 3:
2148be31971SDamien Le Moal 		if (count < 512)
2158be31971SDamien Le Moal 			WR(0x84, 1);
21672f2b0b2SOndrej Zary 		w3(0x40);
2178be31971SDamien Le Moal 		for (k = 0; k < count; k++)
2188be31971SDamien Le Moal 			w4(buf[k]);
2198be31971SDamien Le Moal 		if (count < 512)
2208be31971SDamien Le Moal 			WR(0x84, 0);
22172f2b0b2SOndrej Zary 		break;
2228be31971SDamien Le Moal 	case 4:
2238be31971SDamien Le Moal 		if (count < 512)
2248be31971SDamien Le Moal 			WR(0x84, 1);
22572f2b0b2SOndrej Zary 		w3(0x40);
2268be31971SDamien Le Moal 		for (k = 0; k < count / 2; k++)
2278be31971SDamien Le Moal 			w4w(((u16 *)buf)[k]);
2288be31971SDamien Le Moal 		if (count < 512)
2298be31971SDamien Le Moal 			WR(0x84, 0);
23072f2b0b2SOndrej Zary 		break;
2318be31971SDamien Le Moal 	case 5:
2328be31971SDamien Le Moal 		if (count < 512)
2338be31971SDamien Le Moal 			WR(0x84, 1);
23472f2b0b2SOndrej Zary 		w3(0x40);
2358be31971SDamien Le Moal 		for (k = 0; k < count / 4; k++)
2368be31971SDamien Le Moal 			w4l(((u32 *)buf)[k]);
2378be31971SDamien Le Moal 		if (count < 512)
2388be31971SDamien Le Moal 			WR(0x84, 0);
23972f2b0b2SOndrej Zary 		break;
24072f2b0b2SOndrej Zary 	}
24172f2b0b2SOndrej Zary }
24272f2b0b2SOndrej Zary 
epia_test_proto(struct pi_adapter * pi)243b42251a8SOndrej Zary static int epia_test_proto(struct pi_adapter *pi)
2448be31971SDamien Le Moal {
2458be31971SDamien Le Moal 	int j, k, f;
24672f2b0b2SOndrej Zary 	int e[2] = { 0, 0 };
247b42251a8SOndrej Zary 	char scratch[512];
24872f2b0b2SOndrej Zary 
24972f2b0b2SOndrej Zary 	epia_connect(pi);
25072f2b0b2SOndrej Zary 	for (j = 0; j < 2; j++) {
25172f2b0b2SOndrej Zary 		WR(6, 0xa0 + j * 0x10);
25272f2b0b2SOndrej Zary 		for (k = 0; k < 256; k++) {
25372f2b0b2SOndrej Zary 			WR(2, k ^ 0xaa);
25472f2b0b2SOndrej Zary 			WR(3, k ^ 0x55);
2558be31971SDamien Le Moal 			if (RR(2) != (k ^ 0xaa))
2568be31971SDamien Le Moal 				e[j]++;
25772f2b0b2SOndrej Zary 		}
25872f2b0b2SOndrej Zary 		WR(2, 1); WR(3, 1);
25972f2b0b2SOndrej Zary 	}
26072f2b0b2SOndrej Zary 	epia_disconnect(pi);
26172f2b0b2SOndrej Zary 
26272f2b0b2SOndrej Zary 	f = 0;
26372f2b0b2SOndrej Zary 	epia_connect(pi);
26472f2b0b2SOndrej Zary 	WR(0x84, 8);
26572f2b0b2SOndrej Zary 	epia_read_block(pi, scratch, 512);
26672f2b0b2SOndrej Zary 	for (k = 0; k < 256; k++) {
2678be31971SDamien Le Moal 		if ((scratch[2 * k] & 0xff) != ((k + 1) & 0xff))
2688be31971SDamien Le Moal 			f++;
2698be31971SDamien Le Moal 		if ((scratch[2 * k + 1] & 0xff) != ((-2 - k) & 0xff))
2708be31971SDamien Le Moal 			f++;
27172f2b0b2SOndrej Zary 	}
27272f2b0b2SOndrej Zary 	WR(0x84, 0);
27372f2b0b2SOndrej Zary 	epia_disconnect(pi);
27472f2b0b2SOndrej Zary 
2755f1145d8SOndrej Zary 	dev_dbg(&pi->dev, "epia: port 0x%x, mode %d, test=(%d,%d,%d)\n",
276a4f2ff92SOndrej Zary 		pi->port, pi->mode, e[0], e[1], f);
27772f2b0b2SOndrej Zary 
27872f2b0b2SOndrej Zary 	return (e[0] && e[1]) || f;
27972f2b0b2SOndrej Zary }
28072f2b0b2SOndrej Zary 
28172f2b0b2SOndrej Zary 
epia_log_adapter(struct pi_adapter * pi)2825b77db9cSOndrej Zary static void epia_log_adapter(struct pi_adapter *pi)
2838be31971SDamien Le Moal {
2848be31971SDamien Le Moal 	char *mode[6] = { "4-bit", "5/3", "8-bit", "EPP-8", "EPP-16", "EPP-32"};
28572f2b0b2SOndrej Zary 
2868be31971SDamien Le Moal 	dev_info(&pi->dev,
2878be31971SDamien Le Moal 		 "Shuttle EPIA at 0x%x, mode %d (%s), delay %d\n",
2888be31971SDamien Le Moal 		 pi->port, pi->mode, mode[pi->mode], pi->delay);
28972f2b0b2SOndrej Zary }
29072f2b0b2SOndrej Zary 
29172f2b0b2SOndrej Zary static struct pi_protocol epia = {
29272f2b0b2SOndrej Zary 	.owner		= THIS_MODULE,
29372f2b0b2SOndrej Zary 	.name		= "epia",
29472f2b0b2SOndrej Zary 	.max_mode	= 6,
29572f2b0b2SOndrej Zary 	.epp_first	= 3,
29672f2b0b2SOndrej Zary 	.default_delay	= 1,
29772f2b0b2SOndrej Zary 	.max_units	= 1,
29872f2b0b2SOndrej Zary 	.write_regr	= epia_write_regr,
29972f2b0b2SOndrej Zary 	.read_regr	= epia_read_regr,
30072f2b0b2SOndrej Zary 	.write_block	= epia_write_block,
30172f2b0b2SOndrej Zary 	.read_block	= epia_read_block,
30272f2b0b2SOndrej Zary 	.connect	= epia_connect,
30372f2b0b2SOndrej Zary 	.disconnect	= epia_disconnect,
30472f2b0b2SOndrej Zary 	.test_proto	= epia_test_proto,
30572f2b0b2SOndrej Zary 	.log_adapter	= epia_log_adapter,
30672f2b0b2SOndrej Zary };
30772f2b0b2SOndrej Zary 
30872f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
309*cec148c2SDamien Le Moal MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>");
310*cec148c2SDamien Le Moal MODULE_DESCRIPTION("Shuttle Technologies EPIA parallel port IDE adapter "
311*cec148c2SDamien Le Moal 		   "protocol driver");
3122c08ec0fSOndrej Zary module_pata_parport_driver(epia);
313