1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * (c) 1997-1998 Grant R. Guenther <grant@torque.net> 4 * 5 * comm.c is a low-level protocol driver for some older models of the DataStor 6 * "Commuter" parallel to IDE adapter. Some of the parallel port devices 7 * marketed by Arista currently use this adapter. 8 */ 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/delay.h> 13 #include <linux/kernel.h> 14 #include <linux/types.h> 15 #include <linux/wait.h> 16 #include <asm/io.h> 17 #include "pata_parport.h" 18 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 /* 31 * cont = 0 - access the IDE register file 32 * cont = 1 - access the IDE command set 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 case 0: 44 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: 49 w0(r+0x20); P1; 50 w0(0); w2(0x26); h = r0(); w2(4); 51 return h; 52 53 case 2: 54 case 3: 55 case 4: 56 w3(r+0x20); (void)r1(); 57 w2(0x24); h = r4(); w2(4); 58 return h; 59 } 60 61 return -1; 62 } 63 64 static void comm_write_regr(struct pi_adapter *pi, int cont, int regr, int val) 65 { 66 int r = regr + cont_map[cont]; 67 68 switch (pi->mode) { 69 case 0: 70 case 1: 71 w0(r); P1; w0(val); P2; 72 break; 73 case 2: 74 case 3: 75 case 4: 76 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 { 95 w2(0); w2(0); w2(0); w2(4); 96 w0(pi->saved_r0); 97 w2(pi->saved_r2); 98 } 99 100 static void comm_read_block(struct pi_adapter *pi, char *buf, int count) 101 { 102 int i, l, h; 103 104 switch (pi->mode) { 105 case 0: 106 w0(0x48); P1; 107 for (i = 0; i < count; i++) { 108 w0(0); w2(6); l = r1(); 109 w0(0x80); h = r1(); w2(4); 110 buf[i] = j44(l, h); 111 } 112 break; 113 case 1: 114 w0(0x68); P1; w0(0); 115 for (i = 0; i < count; i++) { 116 w2(0x26); 117 buf[i] = r0(); 118 w2(0x24); 119 } 120 w2(4); 121 break; 122 case 2: 123 w3(0x68); (void)r1(); w2(0x24); 124 for (i = 0; i < count; i++) 125 buf[i] = r4(); 126 w2(4); 127 break; 128 case 3: 129 w3(0x68); (void)r1(); w2(0x24); 130 for (i = 0; i < count / 2; i++) 131 ((u16 *)buf)[i] = r4w(); 132 w2(4); 133 break; 134 case 4: 135 w3(0x68); (void)r1(); w2(0x24); 136 for (i = 0; i < count / 4; i++) 137 ((u32 *)buf)[i] = r4l(); 138 w2(4); 139 break; 140 } 141 } 142 143 /* NB: Watch out for the byte swapped writes ! */ 144 static void comm_write_block(struct pi_adapter *pi, char *buf, int count) 145 { 146 int k; 147 148 switch (pi->mode) { 149 case 0: 150 case 1: 151 w0(0x68); P1; 152 for (k = 0; k < count; k++) { 153 w2(5); 154 w0(buf[k ^ 1]); 155 w2(7); 156 } 157 w2(5); w2(4); 158 break; 159 case 2: 160 w3(0x48); (void)r1(); 161 for (k = 0; k < count; k++) 162 w4(buf[k ^ 1]); 163 break; 164 case 3: 165 w3(0x48); (void)r1(); 166 for (k = 0; k < count / 2; k++) 167 w4w(swab16(((u16 *)buf)[k])); 168 break; 169 case 4: 170 w3(0x48); (void)r1(); 171 for (k = 0; k < count / 4; k++) 172 w4l(swab16(((u16 *)buf)[2 * k]) | 173 swab16(((u16 *)buf)[2 * k + 1]) << 16); 174 break; 175 } 176 } 177 178 static void comm_log_adapter(struct pi_adapter *pi) 179 180 { char *mode_string[5] = { "4-bit", "8-bit", "EPP-8", "EPP-16", "EPP-32" }; 181 182 dev_info(&pi->dev, 183 "DataStor Commuter at 0x%x, mode %d (%s), delay %d\n", 184 pi->port, pi->mode, mode_string[pi->mode], pi->delay); 185 } 186 187 static struct pi_protocol comm = { 188 .owner = THIS_MODULE, 189 .name = "comm", 190 .max_mode = 5, 191 .epp_first = 2, 192 .default_delay = 1, 193 .max_units = 1, 194 .write_regr = comm_write_regr, 195 .read_regr = comm_read_regr, 196 .write_block = comm_write_block, 197 .read_block = comm_read_block, 198 .connect = comm_connect, 199 .disconnect = comm_disconnect, 200 .log_adapter = comm_log_adapter, 201 }; 202 203 MODULE_LICENSE("GPL"); 204 MODULE_AUTHOR("Grant R. Guenther <grant@torque.net>"); 205 MODULE_DESCRIPTION("DataStor Commuter parallel port IDE adapter protocol driver"); 206 module_pata_parport_driver(comm); 207