xref: /openbmc/linux/drivers/ata/pata_parport/kbic.c (revision ffcdf473)
1 /*
2         kbic.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                               Under the terms of the GNU General Public License.
4 
5         This is a low-level driver for the KBIC-951A and KBIC-971A
6         parallel to IDE adapter chips from KingByte Information Systems.
7 
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
11 	each chip.
12 
13 */
14 
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/wait.h>
21 #include <asm/io.h>
22 #include "pata_parport.h"
23 
24 #define r12w()			(delay_p,inw(pi->port+1)&0xffff)
25 
26 #define j44(a,b)                ((((a>>4)&0x0f)|(b&0xf0))^0x88)
27 #define j53(w)                  (((w>>3)&0x1f)|((w>>4)&0xe0))
28 
29 
30 /* cont = 0 - access the IDE register file
31    cont = 1 - access the IDE command set
32 */
33 
34 static int  cont_map[2] = { 0x80, 0x40 };
35 
36 static int kbic_read_regr(struct pi_adapter *pi, int cont, int regr)
37 
38 {       int     a, b, s;
39 
40         s = cont_map[cont];
41 
42 	switch (pi->mode) {
43 
44 	case 0: w0(regr|0x18|s); w2(4); w2(6); w2(4); w2(1); w0(8);
45 	        a = r1(); w0(0x28); b = r1(); w2(4);
46 		return j44(a,b);
47 
48 	case 1: w0(regr|0x38|s); w2(4); w2(6); w2(4); w2(5); w0(8);
49 		a = r12w(); w2(4);
50 		return j53(a);
51 
52 	case 2: w0(regr|0x08|s); w2(4); w2(6); w2(4); w2(0xa5); w2(0xa1);
53 		a = r0(); w2(4);
54        		return a;
55 
56 	case 3:
57 	case 4:
58 	case 5: w0(0x20|s); w2(4); w2(6); w2(4); w3(regr);
59 		a = r4(); b = r4(); w2(4); w2(0); w2(4);
60 		return a;
61 
62 	}
63 	return -1;
64 }
65 
66 static void kbic_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
67 
68 {       int  s;
69 
70         s = cont_map[cont];
71 
72         switch (pi->mode) {
73 
74 	case 0:
75         case 1:
76 	case 2:	w0(regr|0x10|s); w2(4); w2(6); w2(4);
77 		w0(val); w2(5); w2(4);
78 		break;
79 
80 	case 3:
81 	case 4:
82 	case 5: w0(0x20|s); w2(4); w2(6); w2(4); w3(regr);
83 		w4(val); w4(val);
84 		w2(4); w2(0); w2(4);
85                 break;
86 
87 	}
88 }
89 
90 static void k951_connect(struct pi_adapter *pi)
91 
92 { 	pi->saved_r0 = r0();
93         pi->saved_r2 = r2();
94         w2(4);
95 }
96 
97 static void k951_disconnect(struct pi_adapter *pi)
98 
99 {      	w0(pi->saved_r0);
100         w2(pi->saved_r2);
101 }
102 
103 #define	CCP(x)	w2(0xc4);w0(0xaa);w0(0x55);w0(0);w0(0xff);w0(0x87);\
104 		w0(0x78);w0(x);w2(0xc5);w2(0xc4);w0(0xff);
105 
106 static void k971_connect(struct pi_adapter *pi)
107 
108 { 	pi->saved_r0 = r0();
109         pi->saved_r2 = r2();
110 	CCP(0x20);
111         w2(4);
112 }
113 
114 static void k971_disconnect(struct pi_adapter *pi)
115 
116 {       CCP(0x30);
117 	w0(pi->saved_r0);
118         w2(pi->saved_r2);
119 }
120 
121 /* counts must be congruent to 0 MOD 4, but all known applications
122    have this property.
123 */
124 
125 static void kbic_read_block(struct pi_adapter *pi, char *buf, int count)
126 
127 {       int     k, a, b;
128 
129         switch (pi->mode) {
130 
131         case 0: w0(0x98); w2(4); w2(6); w2(4);
132                 for (k=0;k<count/2;k++) {
133 			w2(1); w0(8);    a = r1();
134 			       w0(0x28); b = r1();
135 			buf[2*k]   = j44(a,b);
136 			w2(5);           b = r1();
137 			       w0(8);    a = r1();
138 			buf[2*k+1] = j44(a,b);
139 			w2(4);
140                 }
141                 break;
142 
143         case 1: w0(0xb8); w2(4); w2(6); w2(4);
144                 for (k=0;k<count/4;k++) {
145                         w0(0xb8);
146 			w2(4); w2(5);
147                         w0(8);    buf[4*k]   = j53(r12w());
148 			w0(0xb8); buf[4*k+1] = j53(r12w());
149 			w2(4); w2(5);
150 			          buf[4*k+3] = j53(r12w());
151 			w0(8);    buf[4*k+2] = j53(r12w());
152                 }
153                 w2(4);
154                 break;
155 
156         case 2: w0(0x88); w2(4); w2(6); w2(4);
157                 for (k=0;k<count/2;k++) {
158                         w2(0xa0); w2(0xa1); buf[2*k] = r0();
159                         w2(0xa5); buf[2*k+1] = r0();
160                 }
161                 w2(4);
162                 break;
163 
164         case 3: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
165                 for (k=0;k<count;k++) buf[k] = r4();
166                 w2(4); w2(0); w2(4);
167                 break;
168 
169 	case 4: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
170                 for (k=0;k<count/2;k++) ((u16 *)buf)[k] = r4w();
171                 w2(4); w2(0); w2(4);
172                 break;
173 
174         case 5: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
175                 for (k=0;k<count/4;k++) ((u32 *)buf)[k] = r4l();
176                 w2(4); w2(0); w2(4);
177                 break;
178 
179 
180         }
181 }
182 
183 static void kbic_write_block(struct pi_adapter *pi, char *buf, int count)
184 
185 {       int     k;
186 
187         switch (pi->mode) {
188 
189         case 0:
190         case 1:
191         case 2: w0(0x90); w2(4); w2(6); w2(4);
192 		for(k=0;k<count/2;k++) {
193 			w0(buf[2*k+1]); w2(0); w2(4);
194 			w0(buf[2*k]);   w2(5); w2(4);
195 		}
196 		break;
197 
198         case 3: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
199 		for(k=0;k<count/2;k++) {
200 			w4(buf[2*k+1]);
201                         w4(buf[2*k]);
202                 }
203 		w2(4); w2(0); w2(4);
204 		break;
205 
206 	case 4: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
207 		for (k = 0; k < count / 2; k++)
208 			w4w(swab16(((u16 *)buf)[k]));
209                 w2(4); w2(0); w2(4);
210                 break;
211 
212         case 5: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
213 		for (k = 0; k < count / 4; k++)
214 			w4l(swab16(((u16 *)buf)[2 * k]) |
215 			    swab16(((u16 *)buf)[2 * k + 1]) << 16);
216                 w2(4); w2(0); w2(4);
217                 break;
218 
219         }
220 
221 }
222 
223 static void kbic_log_adapter(struct pi_adapter *pi, char *chip)
224 
225 {       char    *mode_string[6] = {"4-bit","5/3","8-bit",
226 				   "EPP-8","EPP_16","EPP-32"};
227 
228 	dev_info(&pi->dev, "KingByte %s at 0x%x, mode %d (%s), delay %d\n",
229 		 chip, pi->port, pi->mode, mode_string[pi->mode], pi->delay);
230 }
231 
232 static void k951_log_adapter(struct pi_adapter *pi)
233 {
234 	kbic_log_adapter(pi, "KBIC-951A");
235 }
236 
237 static void k971_log_adapter(struct pi_adapter *pi)
238 {
239 	kbic_log_adapter(pi, "KBIC-971A");
240 }
241 
242 static struct pi_protocol k951 = {
243 	.owner		= THIS_MODULE,
244 	.name		= "k951",
245 	.max_mode	= 6,
246 	.epp_first	= 3,
247 	.default_delay	= 1,
248 	.max_units	= 1,
249 	.write_regr	= kbic_write_regr,
250 	.read_regr	= kbic_read_regr,
251 	.write_block	= kbic_write_block,
252 	.read_block	= kbic_read_block,
253 	.connect	= k951_connect,
254 	.disconnect	= k951_disconnect,
255 	.log_adapter	= k951_log_adapter,
256 };
257 
258 static struct pi_protocol k971 = {
259 	.owner		= THIS_MODULE,
260 	.name		= "k971",
261 	.max_mode	= 6,
262 	.epp_first	= 3,
263 	.default_delay	= 1,
264 	.max_units	= 1,
265 	.write_regr	= kbic_write_regr,
266 	.read_regr	= kbic_read_regr,
267 	.write_block	= kbic_write_block,
268 	.read_block	= kbic_read_block,
269 	.connect	= k971_connect,
270 	.disconnect	= k971_disconnect,
271 	.log_adapter	= k971_log_adapter,
272 };
273 
274 static int __init kbic_init(void)
275 {
276 	int rv;
277 
278 	rv = pata_parport_register_driver(&k951);
279 	if (rv < 0)
280 		return rv;
281 	rv = pata_parport_register_driver(&k971);
282 	if (rv < 0)
283 		pata_parport_unregister_driver(&k951);
284 	return rv;
285 }
286 
287 static void __exit kbic_exit(void)
288 {
289 	pata_parport_unregister_driver(&k951);
290 	pata_parport_unregister_driver(&k971);
291 }
292 
293 MODULE_LICENSE("GPL");
294 module_init(kbic_init)
295 module_exit(kbic_exit)
296