1d6332c12SDamien Le Moal // SPDX-License-Identifier: GPL-2.0-or-later
272f2b0b2SOndrej Zary /*
3d6332c12SDamien Le Moal * (c) 1997-1998 Grant R. Guenther <grant@torque.net>
4d6332c12SDamien Le Moal *
5d6332c12SDamien Le Moal * dstr.c is a low-level protocol driver for the DataStor EP2000 parallel
6d6332c12SDamien Le Moal * to IDE adapter chip.
772f2b0b2SOndrej Zary */
872f2b0b2SOndrej Zary
972f2b0b2SOndrej Zary #include <linux/module.h>
1072f2b0b2SOndrej Zary #include <linux/init.h>
1172f2b0b2SOndrej Zary #include <linux/delay.h>
1272f2b0b2SOndrej Zary #include <linux/kernel.h>
1372f2b0b2SOndrej Zary #include <linux/types.h>
1472f2b0b2SOndrej Zary #include <linux/wait.h>
1572f2b0b2SOndrej Zary #include <asm/io.h>
16fe027ff9SOndrej Zary #include "pata_parport.h"
1772f2b0b2SOndrej Zary
18d6332c12SDamien Le Moal /*
19d6332c12SDamien Le Moal * mode codes: 0 nybble reads, 8-bit writes
20d6332c12SDamien Le Moal * 1 8-bit reads and writes
21d6332c12SDamien Le Moal * 2 8-bit EPP mode
22d6332c12SDamien Le Moal * 3 EPP-16
23d6332c12SDamien Le Moal * 4 EPP-32
2472f2b0b2SOndrej Zary */
2572f2b0b2SOndrej Zary
26d6332c12SDamien Le Moal #define j44(a, b) (((a >> 3) & 0x07) | ((~a >> 4) & 0x08) | \
27d6332c12SDamien Le Moal ((b << 1) & 0x70) | ((~b) & 0x80))
2872f2b0b2SOndrej Zary
2972f2b0b2SOndrej Zary #define P1 w2(5);w2(0xd);w2(5);w2(4);
3072f2b0b2SOndrej Zary #define P2 w2(5);w2(7);w2(5);w2(4);
3172f2b0b2SOndrej Zary #define P3 w2(6);w2(4);w2(6);w2(4);
3272f2b0b2SOndrej Zary
33d6332c12SDamien Le Moal /*
34d6332c12SDamien Le Moal * cont = 0 - access the IDE register file
35d6332c12SDamien Le Moal * cont = 1 - access the IDE command set
3672f2b0b2SOndrej Zary */
3772f2b0b2SOndrej Zary static int cont_map[2] = { 0x20, 0x40 };
3872f2b0b2SOndrej Zary
dstr_read_regr(struct pi_adapter * pi,int cont,int regr)39882ff0caSOndrej Zary static int dstr_read_regr(struct pi_adapter *pi, int cont, int regr)
40d6332c12SDamien Le Moal {
41d6332c12SDamien Le Moal int a, b, r;
4272f2b0b2SOndrej Zary
4372f2b0b2SOndrej Zary r = regr + cont_map[cont];
4472f2b0b2SOndrej Zary
4572f2b0b2SOndrej Zary w0(0x81); P1;
46d6332c12SDamien Le Moal if (pi->mode)
47d6332c12SDamien Le Moal w0(0x11);
48d6332c12SDamien Le Moal else
49d6332c12SDamien Le Moal w0(1);
5072f2b0b2SOndrej Zary P2; w0(r); P1;
5172f2b0b2SOndrej Zary
5272f2b0b2SOndrej Zary switch (pi->mode) {
53d6332c12SDamien Le Moal case 0:
54d6332c12SDamien Le Moal w2(6); a = r1(); w2(4); w2(6); b = r1(); w2(4);
5572f2b0b2SOndrej Zary return j44(a, b);
56d6332c12SDamien Le Moal case 1:
57d6332c12SDamien Le Moal w0(0); w2(0x26); a = r0(); w2(4);
5872f2b0b2SOndrej Zary return a;
5972f2b0b2SOndrej Zary case 2:
6072f2b0b2SOndrej Zary case 3:
61d6332c12SDamien Le Moal case 4:
62d6332c12SDamien Le Moal w2(0x24); a = r4(); w2(4);
6372f2b0b2SOndrej Zary return a;
6472f2b0b2SOndrej Zary }
65d6332c12SDamien Le Moal
6672f2b0b2SOndrej Zary return -1;
6772f2b0b2SOndrej Zary }
6872f2b0b2SOndrej Zary
dstr_write_regr(struct pi_adapter * pi,int cont,int regr,int val)69882ff0caSOndrej Zary static void dstr_write_regr(struct pi_adapter *pi, int cont, int regr, int val)
70d6332c12SDamien Le Moal {
71d6332c12SDamien Le Moal int r = regr + cont_map[cont];
7272f2b0b2SOndrej Zary
7372f2b0b2SOndrej Zary w0(0x81); P1;
74d6332c12SDamien Le Moal if (pi->mode >= 2)
75d6332c12SDamien Le Moal w0(0x11);
76d6332c12SDamien Le Moal else
77d6332c12SDamien Le Moal w0(1);
7872f2b0b2SOndrej Zary P2; w0(r); P1;
7972f2b0b2SOndrej Zary
8072f2b0b2SOndrej Zary switch (pi->mode) {
8172f2b0b2SOndrej Zary case 0:
82d6332c12SDamien Le Moal case 1:
83d6332c12SDamien Le Moal w0(val); w2(5); w2(7); w2(5); w2(4);
8472f2b0b2SOndrej Zary break;
8572f2b0b2SOndrej Zary case 2:
8672f2b0b2SOndrej Zary case 3:
87d6332c12SDamien Le Moal case 4:
88d6332c12SDamien Le Moal w4(val);
8972f2b0b2SOndrej Zary break;
9072f2b0b2SOndrej Zary }
9172f2b0b2SOndrej Zary }
9272f2b0b2SOndrej Zary
93d6332c12SDamien Le Moal #define CCP(x) \
94d6332c12SDamien Le Moal do { \
95d6332c12SDamien Le Moal w0(0xff); w2(0xc); w2(4); \
96d6332c12SDamien Le Moal w0(0xaa); w0(0x55); w0(0); w0(0xff); \
97d6332c12SDamien Le Moal w0(0x87); w0(0x78); \
98d6332c12SDamien Le Moal w0(x); w2(5); w2(4); \
99d6332c12SDamien Le Moal } while (0)
10072f2b0b2SOndrej Zary
dstr_connect(struct pi_adapter * pi)101882ff0caSOndrej Zary static void dstr_connect(struct pi_adapter *pi)
102d6332c12SDamien Le Moal {
103d6332c12SDamien Le Moal pi->saved_r0 = r0();
10472f2b0b2SOndrej Zary pi->saved_r2 = r2();
10572f2b0b2SOndrej Zary w2(4); CCP(0xe0); w0(0xff);
10672f2b0b2SOndrej Zary }
10772f2b0b2SOndrej Zary
dstr_disconnect(struct pi_adapter * pi)108882ff0caSOndrej Zary static void dstr_disconnect(struct pi_adapter *pi)
109d6332c12SDamien Le Moal {
110d6332c12SDamien Le Moal CCP(0x30);
11172f2b0b2SOndrej Zary w0(pi->saved_r0);
11272f2b0b2SOndrej Zary w2(pi->saved_r2);
11372f2b0b2SOndrej Zary }
11472f2b0b2SOndrej Zary
dstr_read_block(struct pi_adapter * pi,char * buf,int count)115882ff0caSOndrej Zary static void dstr_read_block(struct pi_adapter *pi, char *buf, int count)
116d6332c12SDamien Le Moal {
117d6332c12SDamien Le Moal int k, a, b;
11872f2b0b2SOndrej Zary
11972f2b0b2SOndrej Zary w0(0x81); P1;
120d6332c12SDamien Le Moal if (pi->mode)
121d6332c12SDamien Le Moal w0(0x19);
122d6332c12SDamien Le Moal else
123d6332c12SDamien Le Moal w0(9);
12472f2b0b2SOndrej Zary P2; w0(0x82); P1; P3; w0(0x20); P1;
12572f2b0b2SOndrej Zary
12672f2b0b2SOndrej Zary switch (pi->mode) {
127d6332c12SDamien Le Moal case 0:
128d6332c12SDamien Le Moal for (k = 0; k < count; k++) {
12972f2b0b2SOndrej Zary w2(6); a = r1(); w2(4);
13072f2b0b2SOndrej Zary w2(6); b = r1(); w2(4);
13172f2b0b2SOndrej Zary buf[k] = j44(a, b);
13272f2b0b2SOndrej Zary }
13372f2b0b2SOndrej Zary break;
134d6332c12SDamien Le Moal case 1:
135d6332c12SDamien Le Moal w0(0);
13672f2b0b2SOndrej Zary for (k = 0; k < count; k++) {
137d6332c12SDamien Le Moal w2(0x26);
138d6332c12SDamien Le Moal buf[k] = r0();
139d6332c12SDamien Le Moal w2(0x24);
14072f2b0b2SOndrej Zary }
14172f2b0b2SOndrej Zary w2(4);
14272f2b0b2SOndrej Zary break;
143d6332c12SDamien Le Moal case 2:
144d6332c12SDamien Le Moal w2(0x24);
145d6332c12SDamien Le Moal for (k = 0; k < count; k++)
146d6332c12SDamien Le Moal buf[k] = r4();
14772f2b0b2SOndrej Zary w2(4);
14872f2b0b2SOndrej Zary break;
149d6332c12SDamien Le Moal case 3:
150d6332c12SDamien Le Moal w2(0x24);
151d6332c12SDamien Le Moal for (k = 0; k < count / 2; k++)
152d6332c12SDamien Le Moal ((u16 *)buf)[k] = r4w();
15372f2b0b2SOndrej Zary w2(4);
15472f2b0b2SOndrej Zary break;
155d6332c12SDamien Le Moal case 4:
156d6332c12SDamien Le Moal w2(0x24);
157d6332c12SDamien Le Moal for (k = 0; k < count / 4; k++)
158d6332c12SDamien Le Moal ((u32 *)buf)[k] = r4l();
15972f2b0b2SOndrej Zary w2(4);
16072f2b0b2SOndrej Zary break;
16172f2b0b2SOndrej Zary }
16272f2b0b2SOndrej Zary }
16372f2b0b2SOndrej Zary
dstr_write_block(struct pi_adapter * pi,char * buf,int count)164882ff0caSOndrej Zary static void dstr_write_block(struct pi_adapter *pi, char *buf, int count)
165d6332c12SDamien Le Moal {
166d6332c12SDamien Le Moal int k;
16772f2b0b2SOndrej Zary
16872f2b0b2SOndrej Zary w0(0x81); P1;
169d6332c12SDamien Le Moal if (pi->mode)
170d6332c12SDamien Le Moal w0(0x19);
171d6332c12SDamien Le Moal else
172d6332c12SDamien Le Moal w0(9);
17372f2b0b2SOndrej Zary P2; w0(0x82); P1; P3; w0(0x20); P1;
17472f2b0b2SOndrej Zary
17572f2b0b2SOndrej Zary switch (pi->mode) {
17672f2b0b2SOndrej Zary case 0:
177d6332c12SDamien Le Moal case 1:
178d6332c12SDamien Le Moal for (k = 0; k < count; k++) {
179d6332c12SDamien Le Moal w2(5);
180d6332c12SDamien Le Moal w0(buf[k]);
181d6332c12SDamien Le Moal w2(7);
18272f2b0b2SOndrej Zary }
18372f2b0b2SOndrej Zary w2(5); w2(4);
18472f2b0b2SOndrej Zary break;
185d6332c12SDamien Le Moal case 2:
186d6332c12SDamien Le Moal w2(0xc5);
187d6332c12SDamien Le Moal for (k = 0; k < count; k++)
188d6332c12SDamien Le Moal w4(buf[k]);
18972f2b0b2SOndrej Zary w2(0xc4);
19072f2b0b2SOndrej Zary break;
191d6332c12SDamien Le Moal case 3:
192d6332c12SDamien Le Moal w2(0xc5);
193d6332c12SDamien Le Moal for (k = 0; k < count / 2; k++)
194d6332c12SDamien Le Moal w4w(((u16 *)buf)[k]);
19572f2b0b2SOndrej Zary w2(0xc4);
19672f2b0b2SOndrej Zary break;
197d6332c12SDamien Le Moal case 4:
198d6332c12SDamien Le Moal w2(0xc5);
199d6332c12SDamien Le Moal for (k = 0; k < count / 4; k++)
200d6332c12SDamien Le Moal w4l(((u32 *)buf)[k]);
20172f2b0b2SOndrej Zary w2(0xc4);
20272f2b0b2SOndrej Zary break;
20372f2b0b2SOndrej Zary }
20472f2b0b2SOndrej Zary }
20572f2b0b2SOndrej Zary
dstr_log_adapter(struct pi_adapter * pi)2065b77db9cSOndrej Zary static void dstr_log_adapter(struct pi_adapter *pi)
20772f2b0b2SOndrej Zary
208d6332c12SDamien Le Moal {
209d6332c12SDamien Le Moal char *mode_string[5] = { "4-bit", "8-bit", "EPP-8", "EPP-16", "EPP-32" };
21072f2b0b2SOndrej Zary
211d6332c12SDamien Le Moal dev_info(&pi->dev,
212d6332c12SDamien Le Moal "DataStor EP2000 at 0x%x, mode %d (%s), delay %d\n",
213426eb3c5SOndrej Zary pi->port, pi->mode, mode_string[pi->mode], pi->delay);
21472f2b0b2SOndrej Zary }
21572f2b0b2SOndrej Zary
21672f2b0b2SOndrej Zary static struct pi_protocol dstr = {
21772f2b0b2SOndrej Zary .owner = THIS_MODULE,
21872f2b0b2SOndrej Zary .name = "dstr",
21972f2b0b2SOndrej Zary .max_mode = 5,
22072f2b0b2SOndrej Zary .epp_first = 2,
22172f2b0b2SOndrej Zary .default_delay = 1,
22272f2b0b2SOndrej Zary .max_units = 1,
22372f2b0b2SOndrej Zary .write_regr = dstr_write_regr,
22472f2b0b2SOndrej Zary .read_regr = dstr_read_regr,
22572f2b0b2SOndrej Zary .write_block = dstr_write_block,
22672f2b0b2SOndrej Zary .read_block = dstr_read_block,
22772f2b0b2SOndrej Zary .connect = dstr_connect,
22872f2b0b2SOndrej Zary .disconnect = dstr_disconnect,
22972f2b0b2SOndrej Zary .log_adapter = dstr_log_adapter,
23072f2b0b2SOndrej Zary };
23172f2b0b2SOndrej Zary
23272f2b0b2SOndrej Zary MODULE_LICENSE("GPL");
233*cec148c2SDamien Le Moal MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>");
234*cec148c2SDamien Le Moal MODULE_DESCRIPTION("DataStor EP2000 parallel port IDE adapter protocol driver");
2352c08ec0fSOndrej Zary module_pata_parport_driver(dstr);
236