xref: /openbmc/linux/drivers/ata/pata_parport/comm.c (revision 9ad685db)
1 /*
2         comm.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                               Under the terms of the GNU General Public License.
4 
5 	comm.c is a low-level protocol driver for some older models
6 	of the DataStor "Commuter" parallel to IDE adapter.  Some of
7 	the parallel port devices marketed by Arista currently
8 	use this adapter.
9 */
10 
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/wait.h>
17 #include <asm/io.h>
18 #include "pata_parport.h"
19 
20 /* mode codes:  0  nybble reads, 8-bit writes
21                 1  8-bit reads and writes
22                 2  8-bit EPP mode
23 */
24 
25 #define j44(a,b)	(((a>>3)&0x0f)|((b<<1)&0xf0))
26 
27 #define P1	w2(5);w2(0xd);w2(0xd);w2(5);w2(4);
28 #define P2	w2(5);w2(7);w2(7);w2(5);w2(4);
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] = { 0x08, 0x10 };
35 
36 static int comm_read_regr(struct pi_adapter *pi, int cont, int regr)
37 
38 {       int     l, h, r;
39 
40         r = regr + cont_map[cont];
41 
42         switch (pi->mode)  {
43 
44         case 0: w0(r); P1; w0(0);
45         	w2(6); l = r1(); w0(0x80); h = r1(); w2(4);
46                 return j44(l,h);
47 
48         case 1: w0(r+0x20); P1;
49         	w0(0); w2(0x26); h = r0(); w2(4);
50                 return h;
51 
52 	case 2:
53 	case 3:
54         case 4: w3(r+0x20); (void)r1();
55         	w2(0x24); h = r4(); w2(4);
56                 return h;
57 
58         }
59         return -1;
60 }
61 
62 static void comm_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
63 
64 {       int  r;
65 
66         r = regr + cont_map[cont];
67 
68         switch (pi->mode)  {
69 
70         case 0:
71         case 1: w0(r); P1; w0(val); P2;
72 		break;
73 
74 	case 2:
75 	case 3:
76         case 4: w3(r); (void)r1(); w4(val);
77                 break;
78         }
79 }
80 
81 static void comm_connect(struct pi_adapter *pi)
82 
83 {       pi->saved_r0 = r0();
84         pi->saved_r2 = r2();
85         w2(4); w0(0xff); w2(6);
86         w2(4); w0(0xaa); w2(6);
87         w2(4); w0(0x00); w2(6);
88         w2(4); w0(0x87); w2(6);
89         w2(4); w0(0xe0); w2(0xc); w2(0xc); w2(4);
90 }
91 
92 static void comm_disconnect(struct pi_adapter *pi)
93 
94 {       w2(0); w2(0); w2(0); w2(4);
95 	w0(pi->saved_r0);
96         w2(pi->saved_r2);
97 }
98 
99 static void comm_read_block(struct pi_adapter *pi, char *buf, int count)
100 
101 {       int     i, l, h;
102 
103         switch (pi->mode) {
104 
105         case 0: w0(0x48); P1;
106                 for(i=0;i<count;i++) {
107                         w0(0); w2(6); l = r1();
108                         w0(0x80); h = r1(); w2(4);
109                         buf[i] = j44(l,h);
110                 }
111                 break;
112 
113         case 1: w0(0x68); P1; w0(0);
114                 for(i=0;i<count;i++) {
115                         w2(0x26); buf[i] = r0(); w2(0x24);
116                 }
117 		w2(4);
118 		break;
119 
120 	case 2: w3(0x68); (void)r1(); w2(0x24);
121 		for (i=0;i<count;i++) buf[i] = r4();
122 		w2(4);
123 		break;
124 
125         case 3: w3(0x68); (void)r1(); w2(0x24);
126                 for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
127                 w2(4);
128                 break;
129 
130         case 4: w3(0x68); (void)r1(); w2(0x24);
131                 for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
132                 w2(4);
133                 break;
134 
135 	}
136 }
137 
138 /* NB: Watch out for the byte swapped writes ! */
139 
140 static void comm_write_block(struct pi_adapter *pi, char *buf, int count)
141 
142 {       int	k;
143 
144         switch (pi->mode) {
145 
146         case 0:
147         case 1: w0(0x68); P1;
148         	for (k=0;k<count;k++) {
149                         w2(5); w0(buf[k^1]); w2(7);
150                 }
151                 w2(5); w2(4);
152                 break;
153 
154         case 2: w3(0x48); (void)r1();
155                 for (k=0;k<count;k++) w4(buf[k^1]);
156                 break;
157 
158         case 3: w3(0x48); (void)r1();
159 		for (k = 0; k < count / 2; k++)
160 			w4w(swab16(((u16 *)buf)[k]));
161                 break;
162 
163         case 4: w3(0x48); (void)r1();
164 		for (k = 0; k < count / 4; k++)
165 			w4l(swab16(((u16 *)buf)[2 * k]) |
166 			    swab16(((u16 *)buf)[2 * k + 1]) << 16);
167                 break;
168 
169 
170         }
171 }
172 
173 static void comm_log_adapter(struct pi_adapter *pi)
174 
175 {       char    *mode_string[5] = {"4-bit","8-bit","EPP-8","EPP-16","EPP-32"};
176 
177 	dev_info(&pi->dev, "DataStor Commuter at 0x%x, mode %d (%s), delay %d\n",
178 		pi->port, pi->mode, mode_string[pi->mode], pi->delay);
179 }
180 
181 static struct pi_protocol comm = {
182 	.owner		= THIS_MODULE,
183 	.name		= "comm",
184 	.max_mode	= 5,
185 	.epp_first	= 2,
186 	.default_delay	= 1,
187 	.max_units	= 1,
188 	.write_regr	= comm_write_regr,
189 	.read_regr	= comm_read_regr,
190 	.write_block	= comm_write_block,
191 	.read_block	= comm_read_block,
192 	.connect	= comm_connect,
193 	.disconnect	= comm_disconnect,
194 	.log_adapter	= comm_log_adapter,
195 };
196 
197 MODULE_LICENSE("GPL");
198 module_pata_parport_driver(comm);
199