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 /* Changes:
16 
17         1.01    GRG 1998.05.06 init_proto, release_proto
18 
19 */
20 
21 #define KBIC_VERSION      "1.01"
22 
23 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/wait.h>
28 #include <asm/io.h>
29 
30 #include "paride.h"
31 
32 #define r12w()			(delay_p,inw(pi->port+1)&0xffff)
33 
34 #define j44(a,b)                ((((a>>4)&0x0f)|(b&0xf0))^0x88)
35 #define j53(w)                  (((w>>3)&0x1f)|((w>>4)&0xe0))
36 
37 
38 /* cont = 0 - access the IDE register file
39    cont = 1 - access the IDE command set
40 */
41 
42 static int  cont_map[2] = { 0x80, 0x40 };
43 
kbic_read_regr(PIA * pi,int cont,int regr)44 static int kbic_read_regr( PIA *pi, int cont, int regr )
45 
46 {       int     a, b, s;
47 
48         s = cont_map[cont];
49 
50 	switch (pi->mode) {
51 
52 	case 0: w0(regr|0x18|s); w2(4); w2(6); w2(4); w2(1); w0(8);
53 	        a = r1(); w0(0x28); b = r1(); w2(4);
54 		return j44(a,b);
55 
56 	case 1: w0(regr|0x38|s); w2(4); w2(6); w2(4); w2(5); w0(8);
57 		a = r12w(); w2(4);
58 		return j53(a);
59 
60 	case 2: w0(regr|0x08|s); w2(4); w2(6); w2(4); w2(0xa5); w2(0xa1);
61 		a = r0(); w2(4);
62        		return a;
63 
64 	case 3:
65 	case 4:
66 	case 5: w0(0x20|s); w2(4); w2(6); w2(4); w3(regr);
67 		a = r4(); b = r4(); w2(4); w2(0); w2(4);
68 		return a;
69 
70 	}
71 	return -1;
72 }
73 
kbic_write_regr(PIA * pi,int cont,int regr,int val)74 static void  kbic_write_regr( PIA *pi, int cont, int regr, int val)
75 
76 {       int  s;
77 
78         s = cont_map[cont];
79 
80         switch (pi->mode) {
81 
82 	case 0:
83         case 1:
84 	case 2:	w0(regr|0x10|s); w2(4); w2(6); w2(4);
85 		w0(val); w2(5); w2(4);
86 		break;
87 
88 	case 3:
89 	case 4:
90 	case 5: w0(0x20|s); w2(4); w2(6); w2(4); w3(regr);
91 		w4(val); w4(val);
92 		w2(4); w2(0); w2(4);
93                 break;
94 
95 	}
96 }
97 
k951_connect(PIA * pi)98 static void k951_connect ( PIA *pi  )
99 
100 { 	pi->saved_r0 = r0();
101         pi->saved_r2 = r2();
102         w2(4);
103 }
104 
k951_disconnect(PIA * pi)105 static void k951_disconnect ( PIA *pi )
106 
107 {      	w0(pi->saved_r0);
108         w2(pi->saved_r2);
109 }
110 
111 #define	CCP(x)	w2(0xc4);w0(0xaa);w0(0x55);w0(0);w0(0xff);w0(0x87);\
112 		w0(0x78);w0(x);w2(0xc5);w2(0xc4);w0(0xff);
113 
k971_connect(PIA * pi)114 static void k971_connect ( PIA *pi  )
115 
116 { 	pi->saved_r0 = r0();
117         pi->saved_r2 = r2();
118 	CCP(0x20);
119         w2(4);
120 }
121 
k971_disconnect(PIA * pi)122 static void k971_disconnect ( PIA *pi )
123 
124 {       CCP(0x30);
125 	w0(pi->saved_r0);
126         w2(pi->saved_r2);
127 }
128 
129 /* counts must be congruent to 0 MOD 4, but all known applications
130    have this property.
131 */
132 
kbic_read_block(PIA * pi,char * buf,int count)133 static void kbic_read_block( PIA *pi, char * buf, int count )
134 
135 {       int     k, a, b;
136 
137         switch (pi->mode) {
138 
139         case 0: w0(0x98); w2(4); w2(6); w2(4);
140                 for (k=0;k<count/2;k++) {
141 			w2(1); w0(8);    a = r1();
142 			       w0(0x28); b = r1();
143 			buf[2*k]   = j44(a,b);
144 			w2(5);           b = r1();
145 			       w0(8);    a = r1();
146 			buf[2*k+1] = j44(a,b);
147 			w2(4);
148                 }
149                 break;
150 
151         case 1: w0(0xb8); w2(4); w2(6); w2(4);
152                 for (k=0;k<count/4;k++) {
153                         w0(0xb8);
154 			w2(4); w2(5);
155                         w0(8);    buf[4*k]   = j53(r12w());
156 			w0(0xb8); buf[4*k+1] = j53(r12w());
157 			w2(4); w2(5);
158 			          buf[4*k+3] = j53(r12w());
159 			w0(8);    buf[4*k+2] = j53(r12w());
160                 }
161                 w2(4);
162                 break;
163 
164         case 2: w0(0x88); w2(4); w2(6); w2(4);
165                 for (k=0;k<count/2;k++) {
166                         w2(0xa0); w2(0xa1); buf[2*k] = r0();
167                         w2(0xa5); buf[2*k+1] = r0();
168                 }
169                 w2(4);
170                 break;
171 
172         case 3: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
173                 for (k=0;k<count;k++) buf[k] = r4();
174                 w2(4); w2(0); w2(4);
175                 break;
176 
177 	case 4: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
178                 for (k=0;k<count/2;k++) ((u16 *)buf)[k] = r4w();
179                 w2(4); w2(0); w2(4);
180                 break;
181 
182         case 5: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
183                 for (k=0;k<count/4;k++) ((u32 *)buf)[k] = r4l();
184                 w2(4); w2(0); w2(4);
185                 break;
186 
187 
188         }
189 }
190 
kbic_write_block(PIA * pi,char * buf,int count)191 static void kbic_write_block( PIA *pi, char * buf, int count )
192 
193 {       int     k;
194 
195         switch (pi->mode) {
196 
197         case 0:
198         case 1:
199         case 2: w0(0x90); w2(4); w2(6); w2(4);
200 		for(k=0;k<count/2;k++) {
201 			w0(buf[2*k+1]); w2(0); w2(4);
202 			w0(buf[2*k]);   w2(5); w2(4);
203 		}
204 		break;
205 
206         case 3: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
207 		for(k=0;k<count/2;k++) {
208 			w4(buf[2*k+1]);
209                         w4(buf[2*k]);
210                 }
211 		w2(4); w2(0); w2(4);
212 		break;
213 
214 	case 4: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
215                 for(k=0;k<count/2;k++) w4w(pi_swab16(buf,k));
216                 w2(4); w2(0); w2(4);
217                 break;
218 
219         case 5: w0(0xa0); w2(4); w2(6); w2(4); w3(0);
220                 for(k=0;k<count/4;k++) w4l(pi_swab32(buf,k));
221                 w2(4); w2(0); w2(4);
222                 break;
223 
224         }
225 
226 }
227 
kbic_log_adapter(PIA * pi,char * scratch,int verbose,char * chip)228 static void kbic_log_adapter( PIA *pi, char * scratch,
229 			      int verbose, char * chip )
230 
231 {       char    *mode_string[6] = {"4-bit","5/3","8-bit",
232 				   "EPP-8","EPP_16","EPP-32"};
233 
234         printk("%s: kbic %s, KingByte %s at 0x%x, ",
235                 pi->device,KBIC_VERSION,chip,pi->port);
236         printk("mode %d (%s), delay %d\n",pi->mode,
237 		mode_string[pi->mode],pi->delay);
238 
239 }
240 
k951_log_adapter(PIA * pi,char * scratch,int verbose)241 static void k951_log_adapter( PIA *pi, char * scratch, int verbose )
242 
243 {	kbic_log_adapter(pi,scratch,verbose,"KBIC-951A");
244 }
245 
k971_log_adapter(PIA * pi,char * scratch,int verbose)246 static void k971_log_adapter( PIA *pi, char * scratch, int verbose )
247 
248 {       kbic_log_adapter(pi,scratch,verbose,"KBIC-971A");
249 }
250 
kbic_init_proto(PIA * pi)251 static void kbic_init_proto( PIA *pi)
252 
253 {       MOD_INC_USE_COUNT;
254 }
255 
kbic_release_proto(PIA * pi)256 static void kbic_release_proto( PIA *pi)
257 
258 {       MOD_DEC_USE_COUNT;
259 }
260 
261 struct pi_protocol k951 = {"k951",0,6,3,1,1,
262                            kbic_write_regr,
263                            kbic_read_regr,
264                            kbic_write_block,
265                            kbic_read_block,
266                            k951_connect,
267                            k951_disconnect,
268                            0,
269                            0,
270                            0,
271                            k951_log_adapter,
272                            kbic_init_proto,
273                            kbic_release_proto
274   			  };
275 
276 
277 struct pi_protocol k971 = {"k971",0,6,3,1,1,
278                            kbic_write_regr,
279                            kbic_read_regr,
280                            kbic_write_block,
281                            kbic_read_block,
282                            k971_connect,
283                            k971_disconnect,
284                            0,
285                            0,
286                            0,
287                            k971_log_adapter,
288                            kbic_init_proto,
289                            kbic_release_proto
290                           };
291 
292 #ifdef MODULE
293 
init_module(void)294 int     init_module(void)
295 
296 {       int s5,s7;
297 
298 	s5 = pi_register(&k951);
299 	s7 = pi_register(&k971);
300 
301 	return (s5 || s7) - 1;
302 }
303 
cleanup_module(void)304 void    cleanup_module(void)
305 
306 {       pi_unregister( &k951 );
307 	pi_unregister( &k971 );
308 }
309 
310 #endif
311 
312 /* end of kbic.c */
313 MODULE_LICENSE("GPL");
314