xref: /openbmc/linux/drivers/ata/pata_parport/epat.c (revision ffcdf473)
1 /*
2         epat.c  (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                             Under the terms of the GNU General Public License.
4 
5 	This is the low level protocol driver for the EPAT parallel
6         to IDE adapter from Shuttle Technologies.  This adapter is
7         used in many popular parallel port disk products such as the
8         SyQuest EZ drives, the Avatar Shark and the Imation SuperDisk.
9 
10 */
11 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/wait.h>
18 #include <asm/io.h>
19 #include "pata_parport.h"
20 
21 #define j44(a,b)		(((a>>4)&0x0f)+(b&0xf0))
22 #define j53(a,b)		(((a>>3)&0x1f)+((b<<4)&0xe0))
23 
24 static int epatc8;
25 
26 module_param(epatc8, int, 0);
27 MODULE_PARM_DESC(epatc8, "support for the Shuttle EP1284 chip, "
28 	"used in any recent Imation SuperDisk (LS-120) drive.");
29 
30 /* cont =  0   IDE register file
31    cont =  1   IDE control registers
32    cont =  2   internal EPAT registers
33 */
34 
35 static int cont_map[3] = { 0x18, 0x10, 0 };
36 
37 static void epat_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
38 
39 {	int r;
40 
41 	r = regr + cont_map[cont];
42 
43 	switch (pi->mode) {
44 
45 	case 0:
46 	case 1:
47 	case 2:	w0(0x60+r); w2(1); w0(val); w2(4);
48 		break;
49 
50 	case 3:
51 	case 4:
52 	case 5: w3(0x40+r); w4(val);
53 		break;
54 
55 	}
56 }
57 
58 static int epat_read_regr(struct pi_adapter *pi, int cont, int regr)
59 
60 {	int  a, b, r;
61 
62 	r = regr + cont_map[cont];
63 
64 	switch (pi->mode) {
65 
66 	case 0:	w0(r); w2(1); w2(3);
67 		a = r1(); w2(4); b = r1();
68 		return j44(a,b);
69 
70 	case 1: w0(0x40+r); w2(1); w2(4);
71 		a = r1(); b = r2(); w0(0xff);
72 		return j53(a,b);
73 
74 	case 2: w0(0x20+r); w2(1); w2(0x25);
75 		a = r0(); w2(4);
76 		return a;
77 
78 	case 3:
79 	case 4:
80 	case 5: w3(r); w2(0x24); a = r4(); w2(4);
81 		return a;
82 
83 	}
84 	return -1;	/* never gets here */
85 }
86 
87 static void epat_read_block(struct pi_adapter *pi, char *buf, int count)
88 
89 {	int  k, ph, a, b;
90 
91 	switch (pi->mode) {
92 
93 	case 0:	w0(7); w2(1); w2(3); w0(0xff);
94 		ph = 0;
95 		for(k=0;k<count;k++) {
96 			if (k == count-1) w0(0xfd);
97 			w2(6+ph); a = r1();
98 			if (a & 8) b = a;
99 			  else { w2(4+ph); b = r1(); }
100 			buf[k] = j44(a,b);
101 			ph =  1 - ph;
102 		}
103 		w0(0); w2(4);
104 		break;
105 
106 	case 1: w0(0x47); w2(1); w2(5); w0(0xff);
107 		ph = 0;
108 		for(k=0;k<count;k++) {
109 			if (k == count-1) w0(0xfd);
110 			w2(4+ph);
111 			a = r1(); b = r2();
112 			buf[k] = j53(a,b);
113 			ph = 1 - ph;
114 		}
115 		w0(0); w2(4);
116 		break;
117 
118 	case 2: w0(0x27); w2(1); w2(0x25); w0(0);
119 		ph = 0;
120 		for(k=0;k<count-1;k++) {
121 			w2(0x24+ph);
122 			buf[k] = r0();
123 			ph = 1 - ph;
124 		}
125 		w2(0x26); w2(0x27); buf[count-1] = r0();
126 		w2(0x25); w2(4);
127 		break;
128 
129 	case 3: w3(0x80); w2(0x24);
130 		for(k=0;k<count-1;k++) buf[k] = r4();
131 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
132 		w2(4);
133 		break;
134 
135 	case 4: w3(0x80); w2(0x24);
136 		for(k=0;k<(count/2)-1;k++) ((u16 *)buf)[k] = r4w();
137 		buf[count-2] = r4();
138 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
139 		w2(4);
140 		break;
141 
142 	case 5: w3(0x80); w2(0x24);
143 		for(k=0;k<(count/4)-1;k++) ((u32 *)buf)[k] = r4l();
144 		for(k=count-4;k<count-1;k++) buf[k] = r4();
145 		w2(4); w3(0xa0); w2(0x24); buf[count-1] = r4();
146 		w2(4);
147 		break;
148 
149 	}
150 }
151 
152 static void epat_write_block(struct pi_adapter *pi, char *buf, int count)
153 
154 {	int ph, k;
155 
156 	switch (pi->mode) {
157 
158 	case 0:
159 	case 1:
160 	case 2: w0(0x67); w2(1); w2(5);
161 		ph = 0;
162 		for(k=0;k<count;k++) {
163 		  	w0(buf[k]);
164 			w2(4+ph);
165 			ph = 1 - ph;
166 		}
167 		w2(7); w2(4);
168 		break;
169 
170 	case 3: w3(0xc0);
171 		for(k=0;k<count;k++) w4(buf[k]);
172 		w2(4);
173 		break;
174 
175 	case 4: w3(0xc0);
176 		for(k=0;k<(count/2);k++) w4w(((u16 *)buf)[k]);
177 		w2(4);
178 		break;
179 
180 	case 5: w3(0xc0);
181 		for(k=0;k<(count/4);k++) w4l(((u32 *)buf)[k]);
182 		w2(4);
183 		break;
184 
185 	}
186 }
187 
188 /* these macros access the EPAT registers in native addressing */
189 
190 #define	WR(r,v)		epat_write_regr(pi,2,r,v)
191 #define	RR(r)		(epat_read_regr(pi,2,r))
192 
193 /* and these access the IDE task file */
194 
195 #define WRi(r,v)         epat_write_regr(pi,0,r,v)
196 #define RRi(r)           (epat_read_regr(pi,0,r))
197 
198 /* FIXME:  the CPP stuff should be fixed to handle multiple EPATs on a chain */
199 
200 #define CPP(x) 	w2(4);w0(0x22);w0(0xaa);w0(0x55);w0(0);w0(0xff);\
201                 w0(0x87);w0(0x78);w0(x);w2(4);w2(5);w2(4);w0(0xff);
202 
203 static void epat_connect(struct pi_adapter *pi)
204 
205 {       pi->saved_r0 = r0();
206         pi->saved_r2 = r2();
207 
208  	/* Initialize the chip */
209 	CPP(0);
210 
211 	if (epatc8) {
212 		CPP(0x40);CPP(0xe0);
213 		w0(0);w2(1);w2(4);
214 		WR(0x8,0x12);WR(0xc,0x14);WR(0x12,0x10);
215 		WR(0xe,0xf);WR(0xf,4);
216 		/* WR(0xe,0xa);WR(0xf,4); */
217 		WR(0xe,0xd);WR(0xf,0);
218 		/* CPP(0x30); */
219 	}
220 
221         /* Connect to the chip */
222 	CPP(0xe0);
223         w0(0);w2(1);w2(4); /* Idle into SPP */
224         if (pi->mode >= 3) {
225           w0(0);w2(1);w2(4);w2(0xc);
226           /* Request EPP */
227           w0(0x40);w2(6);w2(7);w2(4);w2(0xc);w2(4);
228         }
229 
230 	if (!epatc8) {
231 		WR(8,0x10); WR(0xc,0x14); WR(0xa,0x38); WR(0x12,0x10);
232 	}
233 }
234 
235 static void epat_disconnect(struct pi_adapter *pi)
236 {	CPP(0x30);
237 	w0(pi->saved_r0);
238 	w2(pi->saved_r2);
239 }
240 
241 static int epat_test_proto(struct pi_adapter *pi)
242 
243 {       int     k, j, f, cc;
244 	int	e[2] = {0,0};
245 	char scratch[512];
246 
247         epat_connect(pi);
248 	cc = RR(0xd);
249 	epat_disconnect(pi);
250 
251 	epat_connect(pi);
252 	for (j=0;j<2;j++) {
253   	    WRi(6,0xa0+j*0x10);
254             for (k=0;k<256;k++) {
255                 WRi(2,k^0xaa);
256                 WRi(3,k^0x55);
257                 if (RRi(2) != (k^0xaa)) e[j]++;
258                 }
259 	    }
260         epat_disconnect(pi);
261 
262         f = 0;
263         epat_connect(pi);
264         WR(0x13,1); WR(0x13,0); WR(0xa,0x11);
265         epat_read_block(pi,scratch,512);
266 
267         for (k=0;k<256;k++) {
268             if ((scratch[2*k] & 0xff) != k) f++;
269             if ((scratch[2*k+1] & 0xff) != (0xff-k)) f++;
270         }
271         epat_disconnect(pi);
272 
273 	dev_dbg(&pi->dev, "epat: port 0x%x, mode %d, ccr %x, test=(%d,%d,%d)\n",
274 	       pi->port, pi->mode, cc, e[0], e[1], f);
275 
276         return (e[0] && e[1]) || f;
277 }
278 
279 static void epat_log_adapter(struct pi_adapter *pi)
280 
281 {	int	ver;
282         char    *mode_string[6] =
283 		   {"4-bit","5/3","8-bit","EPP-8","EPP-16","EPP-32"};
284 
285 	epat_connect(pi);
286 	WR(0xa,0x38);		/* read the version code */
287         ver = RR(0xb);
288         epat_disconnect(pi);
289 
290 	dev_info(&pi->dev, "Shuttle EPAT chip %x at 0x%x, mode %d (%s), delay %d\n",
291 		 ver, pi->port, pi->mode, mode_string[pi->mode], pi->delay);
292 }
293 
294 static struct pi_protocol epat = {
295 	.owner		= THIS_MODULE,
296 	.name		= "epat",
297 	.max_mode	= 6,
298 	.epp_first	= 3,
299 	.default_delay	= 1,
300 	.max_units	= 1,
301 	.write_regr	= epat_write_regr,
302 	.read_regr	= epat_read_regr,
303 	.write_block	= epat_write_block,
304 	.read_block	= epat_read_block,
305 	.connect	= epat_connect,
306 	.disconnect	= epat_disconnect,
307 	.test_proto	= epat_test_proto,
308 	.log_adapter	= epat_log_adapter,
309 };
310 
311 static int __init epat_init(void)
312 {
313 #ifdef CONFIG_PATA_PARPORT_EPATC8
314 	epatc8 = 1;
315 #endif
316 	return pata_parport_register_driver(&epat);
317 }
318 
319 static void __exit epat_exit(void)
320 {
321 	pata_parport_unregister_driver(&epat);
322 }
323 
324 MODULE_LICENSE("GPL");
325 module_init(epat_init)
326 module_exit(epat_exit)
327