1630b64bcSDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
272f2b0b2SOndrej Zary /*
3630b64bcSDamien Le Moal * (c) 1997-1998 Grant R. Guenther <grant@torque.net>
4630b64bcSDamien Le Moal *
5630b64bcSDamien Le Moal * comm.c is a low-level protocol driver for some older models of the DataStor
6630b64bcSDamien Le Moal * "Commuter" parallel to IDE adapter. Some of the parallel port devices
7630b64bcSDamien Le Moal * marketed by Arista currently use this adapter.
872f2b0b2SOndrej Zary */
972f2b0b2SOndrej Zary
1072f2b0b2SOndrej Zary #include <linux/module.h>
1172f2b0b2SOndrej Zary #include <linux/init.h>
1272f2b0b2SOndrej Zary #include <linux/delay.h>
1372f2b0b2SOndrej Zary #include <linux/kernel.h>
1472f2b0b2SOndrej Zary #include <linux/types.h>
1572f2b0b2SOndrej Zary #include <linux/wait.h>
1672f2b0b2SOndrej Zary #include <asm/io.h>
17fe027ff9SOndrej Zary #include "pata_parport.h"
1872f2b0b2SOndrej Zary
19630b64bcSDamien Le Moal /*
20630b64bcSDamien Le Moal * mode codes: 0 nybble reads, 8-bit writes
21630b64bcSDamien Le Moal * 1 8-bit reads and writes
22630b64bcSDamien Le Moal * 2 8-bit EPP mode
2372f2b0b2SOndrej Zary */
2472f2b0b2SOndrej Zary
2572f2b0b2SOndrej Zary #define j44(a, b) (((a >> 3) & 0x0f) | ((b << 1) & 0xf0))
2672f2b0b2SOndrej Zary
2772f2b0b2SOndrej Zary #define P1 w2(5);w2(0xd);w2(0xd);w2(5);w2(4);
2872f2b0b2SOndrej Zary #define P2 w2(5);w2(7);w2(7);w2(5);w2(4);
2972f2b0b2SOndrej Zary
30630b64bcSDamien Le Moal /*
31630b64bcSDamien Le Moal * cont = 0 - access the IDE register file
32630b64bcSDamien Le Moal * cont = 1 - access the IDE command set
3372f2b0b2SOndrej Zary */
3472f2b0b2SOndrej Zary static int cont_map[2] = { 0x08, 0x10 };
3572f2b0b2SOndrej Zary
comm_read_regr(struct pi_adapter * pi,int cont,int regr)36882ff0caSOndrej Zary static int comm_read_regr(struct pi_adapter *pi, int cont, int regr)
37630b64bcSDamien Le Moal {
38630b64bcSDamien Le Moal int l, h, r;
3972f2b0b2SOndrej Zary
4072f2b0b2SOndrej Zary r = regr + cont_map[cont];
4172f2b0b2SOndrej Zary
4272f2b0b2SOndrej Zary switch (pi->mode) {
43630b64bcSDamien Le Moal case 0:
44630b64bcSDamien Le Moal w0(r); P1; w0(0);
4572f2b0b2SOndrej Zary w2(6); l = r1(); w0(0x80); h = r1(); w2(4);
4672f2b0b2SOndrej Zary return j44(l, h);
4772f2b0b2SOndrej Zary
48630b64bcSDamien Le Moal case 1:
49630b64bcSDamien Le Moal w0(r+0x20); P1;
5072f2b0b2SOndrej Zary w0(0); w2(0x26); h = r0(); w2(4);
5172f2b0b2SOndrej Zary return h;
5272f2b0b2SOndrej Zary
5372f2b0b2SOndrej Zary case 2:
5472f2b0b2SOndrej Zary case 3:
55630b64bcSDamien Le Moal case 4:
56630b64bcSDamien Le Moal w3(r+0x20); (void)r1();
5772f2b0b2SOndrej Zary w2(0x24); h = r4(); w2(4);
5872f2b0b2SOndrej Zary return h;
5972f2b0b2SOndrej Zary }
60630b64bcSDamien Le Moal
6172f2b0b2SOndrej Zary return -1;
6272f2b0b2SOndrej Zary }
6372f2b0b2SOndrej Zary
comm_write_regr(struct pi_adapter * pi,int cont,int regr,int val)64882ff0caSOndrej Zary static void comm_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
65630b64bcSDamien Le Moal {
66630b64bcSDamien Le Moal int r = regr + cont_map[cont];
6772f2b0b2SOndrej Zary
6872f2b0b2SOndrej Zary switch (pi->mode) {
6972f2b0b2SOndrej Zary case 0:
70630b64bcSDamien Le Moal case 1:
71630b64bcSDamien Le Moal w0(r); P1; w0(val); P2;
7272f2b0b2SOndrej Zary break;
7372f2b0b2SOndrej Zary case 2:
7472f2b0b2SOndrej Zary case 3:
75630b64bcSDamien Le Moal case 4:
76630b64bcSDamien Le Moal w3(r); (void)r1(); w4(val);
7772f2b0b2SOndrej Zary break;
7872f2b0b2SOndrej Zary }
7972f2b0b2SOndrej Zary }
8072f2b0b2SOndrej Zary
comm_connect(struct pi_adapter * pi)81882ff0caSOndrej Zary static void comm_connect(struct pi_adapter *pi)
82630b64bcSDamien Le Moal {
83630b64bcSDamien Le Moal pi->saved_r0 = r0();
8472f2b0b2SOndrej Zary pi->saved_r2 = r2();
8572f2b0b2SOndrej Zary w2(4); w0(0xff); w2(6);
8672f2b0b2SOndrej Zary w2(4); w0(0xaa); w2(6);
8772f2b0b2SOndrej Zary w2(4); w0(0x00); w2(6);
8872f2b0b2SOndrej Zary w2(4); w0(0x87); w2(6);
8972f2b0b2SOndrej Zary w2(4); w0(0xe0); w2(0xc); w2(0xc); w2(4);
9072f2b0b2SOndrej Zary }
9172f2b0b2SOndrej Zary
comm_disconnect(struct pi_adapter * pi)92882ff0caSOndrej Zary static void comm_disconnect(struct pi_adapter *pi)
93630b64bcSDamien Le Moal {
94630b64bcSDamien Le Moal w2(0); w2(0); w2(0); w2(4);
9572f2b0b2SOndrej Zary w0(pi->saved_r0);
9672f2b0b2SOndrej Zary w2(pi->saved_r2);
9772f2b0b2SOndrej Zary }
9872f2b0b2SOndrej Zary
comm_read_block(struct pi_adapter * pi,char * buf,int count)99882ff0caSOndrej Zary static void comm_read_block(struct pi_adapter *pi, char *buf, int count)
100630b64bcSDamien Le Moal {
101630b64bcSDamien Le Moal int i, l, h;
10272f2b0b2SOndrej Zary
10372f2b0b2SOndrej Zary switch (pi->mode) {
104630b64bcSDamien Le Moal case 0:
105630b64bcSDamien Le Moal w0(0x48); P1;
10672f2b0b2SOndrej Zary for (i = 0; i < count; i++) {
10772f2b0b2SOndrej Zary w0(0); w2(6); l = r1();
10872f2b0b2SOndrej Zary w0(0x80); h = r1(); w2(4);
10972f2b0b2SOndrej Zary buf[i] = j44(l, h);
11072f2b0b2SOndrej Zary }
11172f2b0b2SOndrej Zary break;
112630b64bcSDamien Le Moal case 1:
113630b64bcSDamien Le Moal w0(0x68); P1; w0(0);
11472f2b0b2SOndrej Zary for (i = 0; i < count; i++) {
115630b64bcSDamien Le Moal w2(0x26);
116630b64bcSDamien Le Moal buf[i] = r0();
117630b64bcSDamien Le Moal w2(0x24);
11872f2b0b2SOndrej Zary }
11972f2b0b2SOndrej Zary w2(4);
12072f2b0b2SOndrej Zary break;
121630b64bcSDamien Le Moal case 2:
122630b64bcSDamien Le Moal w3(0x68); (void)r1(); w2(0x24);
123630b64bcSDamien Le Moal for (i = 0; i < count; i++)
124630b64bcSDamien Le Moal buf[i] = r4();
12572f2b0b2SOndrej Zary w2(4);
12672f2b0b2SOndrej Zary break;
127630b64bcSDamien Le Moal case 3:
128630b64bcSDamien Le Moal w3(0x68); (void)r1(); w2(0x24);
129630b64bcSDamien Le Moal for (i = 0; i < count / 2; i++)
130630b64bcSDamien Le Moal ((u16 *)buf)[i] = r4w();
13172f2b0b2SOndrej Zary w2(4);
13272f2b0b2SOndrej Zary break;
133630b64bcSDamien Le Moal case 4:
134630b64bcSDamien Le Moal w3(0x68); (void)r1(); w2(0x24);
135630b64bcSDamien Le Moal for (i = 0; i < count / 4; i++)
136630b64bcSDamien Le Moal ((u32 *)buf)[i] = r4l();
13772f2b0b2SOndrej Zary w2(4);
13872f2b0b2SOndrej Zary break;
13972f2b0b2SOndrej Zary }
14072f2b0b2SOndrej Zary }
14172f2b0b2SOndrej Zary
14272f2b0b2SOndrej Zary /* NB: Watch out for the byte swapped writes ! */
comm_write_block(struct pi_adapter * pi,char * buf,int count)143882ff0caSOndrej Zary static void comm_write_block(struct pi_adapter *pi, char *buf, int count)
144630b64bcSDamien Le Moal {
145630b64bcSDamien Le Moal int k;
14672f2b0b2SOndrej Zary
14772f2b0b2SOndrej Zary switch (pi->mode) {
14872f2b0b2SOndrej Zary case 0:
149630b64bcSDamien Le Moal case 1:
150630b64bcSDamien Le Moal w0(0x68); P1;
15172f2b0b2SOndrej Zary for (k = 0; k < count; k++) {
152630b64bcSDamien Le Moal w2(5);
153630b64bcSDamien Le Moal w0(buf[k ^ 1]);
154630b64bcSDamien Le Moal w2(7);
15572f2b0b2SOndrej Zary }
15672f2b0b2SOndrej Zary w2(5); w2(4);
15772f2b0b2SOndrej Zary break;
158630b64bcSDamien Le Moal case 2:
159630b64bcSDamien Le Moal w3(0x48); (void)r1();
160630b64bcSDamien Le Moal for (k = 0; k < count; k++)
161630b64bcSDamien Le Moal w4(buf[k ^ 1]);
16272f2b0b2SOndrej Zary break;
163630b64bcSDamien Le Moal case 3:
164630b64bcSDamien Le Moal w3(0x48); (void)r1();
165e02c625dSOndrej Zary for (k = 0; k < count / 2; k++)
166e02c625dSOndrej Zary w4w(swab16(((u16 *)buf)[k]));
16772f2b0b2SOndrej Zary break;
168630b64bcSDamien Le Moal case 4:
169630b64bcSDamien Le Moal w3(0x48); (void)r1();
170e02c625dSOndrej Zary for (k = 0; k < count / 4; k++)
171e02c625dSOndrej Zary w4l(swab16(((u16 *)buf)[2 * k]) |
172e02c625dSOndrej Zary swab16(((u16 *)buf)[2 * k + 1]) << 16);
17372f2b0b2SOndrej Zary break;
17472f2b0b2SOndrej Zary }
17572f2b0b2SOndrej Zary }
17672f2b0b2SOndrej Zary
comm_log_adapter(struct pi_adapter * pi)1775b77db9cSOndrej Zary static void comm_log_adapter(struct pi_adapter *pi)
178*e3da4c40SDamien Le Moal {
179*e3da4c40SDamien Le Moal char *mode_string[5] = { "4-bit", "8-bit", "EPP-8", "EPP-16", "EPP-32" };
18072f2b0b2SOndrej Zary
181630b64bcSDamien Le Moal dev_info(&pi->dev,
182630b64bcSDamien Le Moal "DataStor Commuter at 0x%x, mode %d (%s), delay %d\n",
183426eb3c5SOndrej Zary pi->port, pi->mode, mode_string[pi->mode], pi->delay);
18472f2b0b2SOndrej Zary }
18572f2b0b2SOndrej Zary
18672f2b0b2SOndrej Zary static struct pi_protocol comm = {
18772f2b0b2SOndrej Zary .owner = THIS_MODULE,
18872f2b0b2SOndrej Zary .name = "comm",
18972f2b0b2SOndrej Zary .max_mode = 5,
19072f2b0b2SOndrej Zary .epp_first = 2,
19172f2b0b2SOndrej Zary .default_delay = 1,
19272f2b0b2SOndrej Zary .max_units = 1,
19372f2b0b2SOndrej Zary .write_regr = comm_write_regr,
19472f2b0b2SOndrej Zary .read_regr = comm_read_regr,
19572f2b0b2SOndrej Zary .write_block = comm_write_block,
19672f2b0b2SOndrej Zary .read_block = comm_read_block,
19772f2b0b2SOndrej Zary .connect = comm_connect,
19872f2b0b2SOndrej Zary .disconnect = comm_disconnect,
19972f2b0b2SOndrej Zary .log_adapter = comm_log_adapter,
20072f2b0b2SOndrej Zary };
20172f2b0b2SOndrej Zary
20272f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
203cec148c2SDamien Le Moal MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>");
204cec148c2SDamien Le Moal MODULE_DESCRIPTION("DataStor Commuter parallel port IDE adapter protocol driver");
2052c08ec0fSOndrej Zary module_pata_parport_driver(comm);
206