xref: /openbmc/linux/drivers/ata/pata_parport/frpw.c (revision cec148c2)
1f19c694fSDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
272f2b0b2SOndrej Zary /*
3f19c694fSDamien Le Moal  * (c) 1996-1998  Grant R. Guenther <grant@torque.net>
4f19c694fSDamien Le Moal  *
5f19c694fSDamien Le Moal  * frpw.c is a low-level protocol driver for the Freecom "Power" parallel port
6f19c694fSDamien Le Moal  * IDE adapter.
7f19c694fSDamien Le Moal  *
8f19c694fSDamien Le Moal  * Some applications of this adapter may require a "printer" reset prior to
9f19c694fSDamien Le Moal  * loading the driver.  This can be done by loading and unloading the "lp"
10f19c694fSDamien Le Moal  * driver, or it can be done by this driver if you define FRPW_HARD_RESET.
11f19c694fSDamien Le Moal  * The latter is not recommended as it may upset devices on other ports.
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 cec4		w2(0xc);w2(0xe);w2(0xe);w2(0xc);w2(4);w2(4);w2(4);
2472f2b0b2SOndrej Zary #define j44(l,h)	(((l>>4)&0x0f)|(h&0xf0))
2572f2b0b2SOndrej Zary 
26f19c694fSDamien Le Moal /*
27f19c694fSDamien Le Moal  * cont = 0 - access the IDE register file
28f19c694fSDamien Le Moal  * cont = 1 - access the IDE command set
2972f2b0b2SOndrej Zary  */
3072f2b0b2SOndrej Zary static int  cont_map[2] = { 0x08, 0x10 };
3172f2b0b2SOndrej Zary 
frpw_read_regr(struct pi_adapter * pi,int cont,int regr)32882ff0caSOndrej Zary static int frpw_read_regr(struct pi_adapter *pi, int cont, int regr)
33f19c694fSDamien Le Moal {
34f19c694fSDamien Le Moal 	int h, l, r;
3572f2b0b2SOndrej Zary 
3672f2b0b2SOndrej Zary 	r = regr + cont_map[cont];
3772f2b0b2SOndrej Zary 
3872f2b0b2SOndrej Zary 	w2(4);
3972f2b0b2SOndrej Zary 	w0(r); cec4;
4072f2b0b2SOndrej Zary 	w2(6); l = r1();
4172f2b0b2SOndrej Zary 	w2(4); h = r1();
4272f2b0b2SOndrej Zary 	w2(4);
4372f2b0b2SOndrej Zary 
4472f2b0b2SOndrej Zary 	return j44(l, h);
4572f2b0b2SOndrej Zary }
4672f2b0b2SOndrej Zary 
frpw_write_regr(struct pi_adapter * pi,int cont,int regr,int val)47882ff0caSOndrej Zary static void frpw_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
48f19c694fSDamien Le Moal {
49f19c694fSDamien Le Moal 	int r = regr + cont_map[cont];
5072f2b0b2SOndrej Zary 
5172f2b0b2SOndrej Zary 	w2(4); w0(r); cec4;
5272f2b0b2SOndrej Zary 	w0(val);
5372f2b0b2SOndrej Zary 	w2(5); w2(7); w2(5); w2(4);
5472f2b0b2SOndrej Zary }
5572f2b0b2SOndrej Zary 
frpw_read_block_int(struct pi_adapter * pi,char * buf,int count,int regr)56f19c694fSDamien Le Moal static void frpw_read_block_int(struct pi_adapter *pi, char *buf, int count,
57f19c694fSDamien Le Moal 				int regr)
58f19c694fSDamien Le Moal {
59f19c694fSDamien Le Moal 	int h, l, k, ph;
6072f2b0b2SOndrej Zary 
6172f2b0b2SOndrej Zary 	switch (pi->mode) {
62f19c694fSDamien Le Moal 	case 0:
63f19c694fSDamien Le Moal 		w2(4); w0(regr); cec4;
6472f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
6572f2b0b2SOndrej Zary 			w2(6); l = r1();
6672f2b0b2SOndrej Zary 			w2(4); h = r1();
6772f2b0b2SOndrej Zary 			buf[k] = j44(l, h);
6872f2b0b2SOndrej Zary 		}
6972f2b0b2SOndrej Zary 		w2(4);
7072f2b0b2SOndrej Zary 		break;
7172f2b0b2SOndrej Zary 
72f19c694fSDamien Le Moal 	case 1:
73f19c694fSDamien Le Moal 		ph = 2;
7472f2b0b2SOndrej Zary 		w2(4); w0(regr + 0xc0); cec4;
7572f2b0b2SOndrej Zary 		w0(0xff);
7672f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
7772f2b0b2SOndrej Zary 			w2(0xa4 + ph);
7872f2b0b2SOndrej Zary 			buf[k] = r0();
7972f2b0b2SOndrej Zary 			ph = 2 - ph;
8072f2b0b2SOndrej Zary 		}
8172f2b0b2SOndrej Zary 		w2(0xac); w2(0xa4); w2(4);
8272f2b0b2SOndrej Zary 		break;
8372f2b0b2SOndrej Zary 
84f19c694fSDamien Le Moal 	case 2:
85f19c694fSDamien Le Moal 		w2(4); w0(regr + 0x80); cec4;
86f19c694fSDamien Le Moal 		for (k = 0; k < count; k++)
87f19c694fSDamien Le Moal 			buf[k] = r4();
8872f2b0b2SOndrej Zary 		w2(0xac); w2(0xa4);
8972f2b0b2SOndrej Zary 		w2(4);
9072f2b0b2SOndrej Zary 		break;
9172f2b0b2SOndrej Zary 
92f19c694fSDamien Le Moal 	case 3:
93f19c694fSDamien Le Moal 		w2(4); w0(regr + 0x80); cec4;
94f19c694fSDamien Le Moal 		for (k = 0; k < count - 2; k++)
95f19c694fSDamien Le Moal 			buf[k] = r4();
9672f2b0b2SOndrej Zary 		w2(0xac); w2(0xa4);
9772f2b0b2SOndrej Zary 		buf[count - 2] = r4();
9872f2b0b2SOndrej Zary 		buf[count - 1] = r4();
9972f2b0b2SOndrej Zary 		w2(4);
10072f2b0b2SOndrej Zary 		break;
10172f2b0b2SOndrej Zary 
102f19c694fSDamien Le Moal 	case 4:
103f19c694fSDamien Le Moal 		w2(4); w0(regr + 0x80); cec4;
104f19c694fSDamien Le Moal 		for (k = 0; k < count / 2 - 1; k++)
105f19c694fSDamien Le Moal 			((u16 *)buf)[k] = r4w();
10672f2b0b2SOndrej Zary 		w2(0xac); w2(0xa4);
10772f2b0b2SOndrej Zary 		buf[count - 2] = r4();
10872f2b0b2SOndrej Zary 		buf[count - 1] = r4();
10972f2b0b2SOndrej Zary 		w2(4);
11072f2b0b2SOndrej Zary 		break;
11172f2b0b2SOndrej Zary 
112f19c694fSDamien Le Moal 	case 5:
113f19c694fSDamien Le Moal 		w2(4); w0(regr + 0x80); cec4;
114f19c694fSDamien Le Moal 		for (k = 0; k < count / 4 - 1; k++)
115f19c694fSDamien Le Moal 			((u32 *)buf)[k] = r4l();
11672f2b0b2SOndrej Zary 		buf[count - 4] = r4();
11772f2b0b2SOndrej Zary 		buf[count - 3] = r4();
11872f2b0b2SOndrej Zary 		w2(0xac); w2(0xa4);
11972f2b0b2SOndrej Zary 		buf[count - 2] = r4();
12072f2b0b2SOndrej Zary 		buf[count - 1] = r4();
12172f2b0b2SOndrej Zary 		w2(4);
12272f2b0b2SOndrej Zary 		break;
12372f2b0b2SOndrej Zary         }
12472f2b0b2SOndrej Zary }
12572f2b0b2SOndrej Zary 
frpw_read_block(struct pi_adapter * pi,char * buf,int count)126882ff0caSOndrej Zary static void frpw_read_block(struct pi_adapter *pi, char *buf, int count)
127f19c694fSDamien Le Moal {
128f19c694fSDamien Le Moal 	frpw_read_block_int(pi, buf, count, 0x08);
12972f2b0b2SOndrej Zary }
13072f2b0b2SOndrej Zary 
frpw_write_block(struct pi_adapter * pi,char * buf,int count)131882ff0caSOndrej Zary static void frpw_write_block(struct pi_adapter *pi, char *buf, int count)
132f19c694fSDamien Le Moal {
133f19c694fSDamien Le Moal 	int k;
13472f2b0b2SOndrej Zary 
13572f2b0b2SOndrej Zary 	switch (pi->mode) {
13672f2b0b2SOndrej Zary 	case 0:
13772f2b0b2SOndrej Zary 	case 1:
138f19c694fSDamien Le Moal 	case 2:
139f19c694fSDamien Le Moal 		w2(4); w0(8); cec4; w2(5);
14072f2b0b2SOndrej Zary 		for (k = 0; k < count; k++) {
14172f2b0b2SOndrej Zary 			w0(buf[k]);
14272f2b0b2SOndrej Zary 			w2(7); w2(5);
14372f2b0b2SOndrej Zary 		}
14472f2b0b2SOndrej Zary 		w2(4);
14572f2b0b2SOndrej Zary 		break;
14672f2b0b2SOndrej Zary 
147f19c694fSDamien Le Moal 	case 3:
148f19c694fSDamien Le Moal 		w2(4); w0(0xc8); cec4; w2(5);
149f19c694fSDamien Le Moal 		for (k = 0; k < count; k++)
150f19c694fSDamien Le Moal 			w4(buf[k]);
15172f2b0b2SOndrej Zary 		w2(4);
15272f2b0b2SOndrej Zary 		break;
15372f2b0b2SOndrej Zary 
154f19c694fSDamien Le Moal 	case 4:
155f19c694fSDamien Le Moal 		w2(4); w0(0xc8); cec4; w2(5);
156f19c694fSDamien Le Moal 		for (k = 0; k < count / 2; k++)
157f19c694fSDamien Le Moal 			w4w(((u16 *)buf)[k]);
15872f2b0b2SOndrej Zary 		w2(4);
15972f2b0b2SOndrej Zary 		break;
16072f2b0b2SOndrej Zary 
161f19c694fSDamien Le Moal 	case 5:
162f19c694fSDamien Le Moal 		w2(4); w0(0xc8); cec4; w2(5);
163f19c694fSDamien Le Moal 		for (k = 0; k < count / 4; k++)
164f19c694fSDamien Le Moal 			w4l(((u32 *)buf)[k]);
16572f2b0b2SOndrej Zary 		w2(4);
16672f2b0b2SOndrej Zary 		break;
16772f2b0b2SOndrej Zary 	}
16872f2b0b2SOndrej Zary }
16972f2b0b2SOndrej Zary 
frpw_connect(struct pi_adapter * pi)170882ff0caSOndrej Zary static void frpw_connect(struct pi_adapter *pi)
171f19c694fSDamien Le Moal {
172f19c694fSDamien Le Moal 	pi->saved_r0 = r0();
17372f2b0b2SOndrej Zary 	pi->saved_r2 = r2();
17472f2b0b2SOndrej Zary 	w2(4);
17572f2b0b2SOndrej Zary }
17672f2b0b2SOndrej Zary 
frpw_disconnect(struct pi_adapter * pi)177882ff0caSOndrej Zary static void frpw_disconnect(struct pi_adapter *pi)
178f19c694fSDamien Le Moal {
179f19c694fSDamien Le Moal 	w2(4); w0(0x20); cec4;
18072f2b0b2SOndrej Zary 	w0(pi->saved_r0);
18172f2b0b2SOndrej Zary 	w2(pi->saved_r2);
18272f2b0b2SOndrej Zary }
18372f2b0b2SOndrej Zary 
184f19c694fSDamien Le Moal /*
185f19c694fSDamien Le Moal  * Stub logic to see if PNP string is available - used to distinguish
186f19c694fSDamien Le Moal  * between the Xilinx and ASIC implementations of the Freecom adapter.
187f19c694fSDamien Le Moal  * returns chip_type:   0 = Xilinx, 1 = ASIC
18872f2b0b2SOndrej Zary  */
frpw_test_pnp(struct pi_adapter * pi)189882ff0caSOndrej Zary static int frpw_test_pnp(struct pi_adapter *pi)
190f19c694fSDamien Le Moal {
191f19c694fSDamien Le Moal 	int olddelay, a, b;
19272f2b0b2SOndrej Zary 
19372f2b0b2SOndrej Zary #ifdef FRPW_HARD_RESET
19472f2b0b2SOndrej Zary         w0(0); w2(8); udelay(50); w2(0xc);   /* parallel bus reset */
19572f2b0b2SOndrej Zary         mdelay(1500);
19672f2b0b2SOndrej Zary #endif
19772f2b0b2SOndrej Zary 
19872f2b0b2SOndrej Zary 	olddelay = pi->delay;
19972f2b0b2SOndrej Zary 	pi->delay = 10;
20072f2b0b2SOndrej Zary 
20172f2b0b2SOndrej Zary 	pi->saved_r0 = r0();
20272f2b0b2SOndrej Zary         pi->saved_r2 = r2();
20372f2b0b2SOndrej Zary 
20472f2b0b2SOndrej Zary 	w2(4); w0(4); w2(6); w2(7);
20572f2b0b2SOndrej Zary 	a = r1() & 0xff; w2(4); b = r1() & 0xff;
20672f2b0b2SOndrej Zary 	w2(0xc); w2(0xe); w2(4);
20772f2b0b2SOndrej Zary 
20872f2b0b2SOndrej Zary 	pi->delay = olddelay;
20972f2b0b2SOndrej Zary         w0(pi->saved_r0);
21072f2b0b2SOndrej Zary         w2(pi->saved_r2);
21172f2b0b2SOndrej Zary 
21272f2b0b2SOndrej Zary 	return ((~a & 0x40) && (b & 0x40));
21372f2b0b2SOndrej Zary }
21472f2b0b2SOndrej Zary 
215f19c694fSDamien Le Moal /*
216f19c694fSDamien Le Moal  * We use the pi->private to remember the result of the PNP test.
217f19c694fSDamien Le Moal  * To make this work, private = port*2 + chip.  Yes, I know it's a hack :-(
21872f2b0b2SOndrej Zary  */
frpw_test_proto(struct pi_adapter * pi)219b42251a8SOndrej Zary static int frpw_test_proto(struct pi_adapter *pi)
220f19c694fSDamien Le Moal {
221f19c694fSDamien Le Moal 	int j, k, r;
22272f2b0b2SOndrej Zary 	int e[2] = { 0, 0 };
223b42251a8SOndrej Zary 	char scratch[512];
22472f2b0b2SOndrej Zary 
22572f2b0b2SOndrej Zary 	if ((pi->private >> 1) != pi->port)
22672f2b0b2SOndrej Zary 		pi->private = frpw_test_pnp(pi) + 2*pi->port;
22772f2b0b2SOndrej Zary 
228f19c694fSDamien Le Moal 	if (((pi->private & 0x1) == 0) && (pi->mode > 2)) {
229f19c694fSDamien Le Moal 		dev_dbg(&pi->dev,
230f19c694fSDamien Le Moal 			"frpw: Xilinx does not support mode %d\n", pi->mode);
23172f2b0b2SOndrej Zary 		return 1;
23272f2b0b2SOndrej Zary 	}
23372f2b0b2SOndrej Zary 
234f19c694fSDamien Le Moal 	if (((pi->private & 0x1) == 1) && (pi->mode == 2)) {
2355f1145d8SOndrej Zary 		dev_dbg(&pi->dev, "frpw: ASIC does not support mode 2\n");
23672f2b0b2SOndrej Zary 		return 1;
23772f2b0b2SOndrej Zary 	}
23872f2b0b2SOndrej Zary 
23972f2b0b2SOndrej Zary 	frpw_connect(pi);
24072f2b0b2SOndrej Zary 	for (j = 0; j < 2; j++) {
24172f2b0b2SOndrej Zary 		frpw_write_regr(pi, 0, 6, 0xa0 + j * 0x10);
24272f2b0b2SOndrej Zary 		for (k = 0; k < 256; k++) {
24372f2b0b2SOndrej Zary 			frpw_write_regr(pi, 0, 2, k ^ 0xaa);
24472f2b0b2SOndrej Zary 			frpw_write_regr(pi, 0, 3, k ^ 0x55);
245f19c694fSDamien Le Moal 			if (frpw_read_regr(pi, 0, 2) != (k ^ 0xaa))
246f19c694fSDamien Le Moal 				e[j]++;
24772f2b0b2SOndrej Zary 		}
24872f2b0b2SOndrej Zary 	}
24972f2b0b2SOndrej Zary 	frpw_disconnect(pi);
25072f2b0b2SOndrej Zary 
25172f2b0b2SOndrej Zary 	frpw_connect(pi);
25272f2b0b2SOndrej Zary 	frpw_read_block_int(pi, scratch, 512, 0x10);
25372f2b0b2SOndrej Zary 	r = 0;
254f19c694fSDamien Le Moal 	for (k = 0; k < 128; k++) {
255f19c694fSDamien Le Moal 		if (scratch[k] != k)
256f19c694fSDamien Le Moal 			r++;
257f19c694fSDamien Le Moal 	}
25872f2b0b2SOndrej Zary 	frpw_disconnect(pi);
25972f2b0b2SOndrej Zary 
260f19c694fSDamien Le Moal 	dev_dbg(&pi->dev,
261f19c694fSDamien Le Moal 		"frpw: port 0x%x, chip %ld, mode %d, test=(%d,%d,%d)\n",
262a4f2ff92SOndrej Zary 		pi->port, (pi->private%2), pi->mode, e[0], e[1], r);
26372f2b0b2SOndrej Zary 
264f19c694fSDamien Le Moal 	return r || (e[0] && e[1]);
26572f2b0b2SOndrej Zary }
26672f2b0b2SOndrej Zary 
frpw_log_adapter(struct pi_adapter * pi)2675b77db9cSOndrej Zary static void frpw_log_adapter(struct pi_adapter *pi)
26872f2b0b2SOndrej Zary 
269f19c694fSDamien Le Moal {
270f19c694fSDamien Le Moal 	char *mode[6] = { "4-bit", "8-bit", "EPP", "EPP-8", "EPP-16", "EPP-32"};
27172f2b0b2SOndrej Zary 
272f19c694fSDamien Le Moal 	dev_info(&pi->dev,
273f19c694fSDamien Le Moal 		 "Freecom (%s) adapter at 0x%x, mode %d (%s), delay %d\n",
274f19c694fSDamien Le Moal 		 ((pi->private & 0x1) == 0) ? "Xilinx" : "ASIC",
275f19c694fSDamien Le Moal 		 pi->port, pi->mode, mode[pi->mode], pi->delay);
27672f2b0b2SOndrej Zary }
27772f2b0b2SOndrej Zary 
27872f2b0b2SOndrej Zary static struct pi_protocol frpw = {
27972f2b0b2SOndrej Zary 	.owner		= THIS_MODULE,
28072f2b0b2SOndrej Zary 	.name		= "frpw",
28172f2b0b2SOndrej Zary 	.max_mode	= 6,
28272f2b0b2SOndrej Zary 	.epp_first	= 2,
28372f2b0b2SOndrej Zary 	.default_delay	= 2,
28472f2b0b2SOndrej Zary 	.max_units	= 1,
28572f2b0b2SOndrej Zary 	.write_regr	= frpw_write_regr,
28672f2b0b2SOndrej Zary 	.read_regr	= frpw_read_regr,
28772f2b0b2SOndrej Zary 	.write_block	= frpw_write_block,
28872f2b0b2SOndrej Zary 	.read_block	= frpw_read_block,
28972f2b0b2SOndrej Zary 	.connect	= frpw_connect,
29072f2b0b2SOndrej Zary 	.disconnect	= frpw_disconnect,
29172f2b0b2SOndrej Zary 	.test_proto	= frpw_test_proto,
29272f2b0b2SOndrej Zary 	.log_adapter	= frpw_log_adapter,
29372f2b0b2SOndrej Zary };
29472f2b0b2SOndrej Zary 
29572f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
296*cec148c2SDamien Le Moal MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>");
297*cec148c2SDamien Le Moal MODULE_DESCRIPTION("Freecom Power parallel port IDE adapter protocol driver");
2982c08ec0fSOndrej Zary module_pata_parport_driver(frpw);
299