epia.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. epia.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. epia.c is a low-level protocol driver for Shuttle Technologies
  5. EPIA parallel to IDE adapter chip. This device is now obsolete
  6. and has been replaced with the EPAT chip, which is supported
  7. by epat.c, however, some devices based on EPIA are still
  8. available.
  9. */
  10. /* Changes:
  11. 1.01 GRG 1998.05.06 init_proto, release_proto
  12. 1.02 GRG 1998.06.17 support older versions of EPIA
  13. */
  14. #define EPIA_VERSION "1.02"
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/wait.h>
  21. #include <asm/io.h>
  22. #include "paride.h"
  23. /* mode codes: 0 nybble reads on port 1, 8-bit writes
  24. 1 5/3 reads on ports 1 & 2, 8-bit writes
  25. 2 8-bit reads and writes
  26. 3 8-bit EPP mode
  27. 4 16-bit EPP
  28. 5 32-bit EPP
  29. */
  30. #define j44(a,b) (((a>>4)&0x0f)+(b&0xf0))
  31. #define j53(a,b) (((a>>3)&0x1f)+((b<<4)&0xe0))
  32. /* cont = 0 IDE register file
  33. cont = 1 IDE control registers
  34. */
  35. static int cont_map[2] = { 0, 0x80 };
  36. static int epia_read_regr( PIA *pi, int cont, int regr )
  37. { int a, b, r;
  38. regr += cont_map[cont];
  39. switch (pi->mode) {
  40. case 0: r = regr^0x39;
  41. w0(r); w2(1); w2(3); w0(r);
  42. a = r1(); w2(1); b = r1(); w2(4);
  43. return j44(a,b);
  44. case 1: r = regr^0x31;
  45. w0(r); w2(1); w0(r&0x37);
  46. w2(3); w2(5); w0(r|0xf0);
  47. a = r1(); b = r2(); w2(4);
  48. return j53(a,b);
  49. case 2: r = regr^0x29;
  50. w0(r); w2(1); w2(0X21); w2(0x23);
  51. a = r0(); w2(4);
  52. return a;
  53. case 3:
  54. case 4:
  55. case 5: w3(regr); w2(0x24); a = r4(); w2(4);
  56. return a;
  57. }
  58. return -1;
  59. }
  60. static void epia_write_regr( PIA *pi, int cont, int regr, int val)
  61. { int r;
  62. regr += cont_map[cont];
  63. switch (pi->mode) {
  64. case 0:
  65. case 1:
  66. case 2: r = regr^0x19;
  67. w0(r); w2(1); w0(val); w2(3); w2(4);
  68. break;
  69. case 3:
  70. case 4:
  71. case 5: r = regr^0x40;
  72. w3(r); w4(val); w2(4);
  73. break;
  74. }
  75. }
  76. #define WR(r,v) epia_write_regr(pi,0,r,v)
  77. #define RR(r) (epia_read_regr(pi,0,r))
  78. /* The use of register 0x84 is entirely unclear - it seems to control
  79. some EPP counters ... currently we know about 3 different block
  80. sizes: the standard 512 byte reads and writes, 12 byte writes and
  81. 2048 byte reads (the last two being used in the CDrom drivers.
  82. */
  83. static void epia_connect ( PIA *pi )
  84. { pi->saved_r0 = r0();
  85. pi->saved_r2 = r2();
  86. w2(4); w0(0xa0); w0(0x50); w0(0xc0); w0(0x30); w0(0xa0); w0(0);
  87. w2(1); w2(4);
  88. if (pi->mode >= 3) {
  89. w0(0xa); w2(1); w2(4); w0(0x82); w2(4); w2(0xc); w2(4);
  90. w2(0x24); w2(0x26); w2(4);
  91. }
  92. WR(0x86,8);
  93. }
  94. static void epia_disconnect ( PIA *pi )
  95. { /* WR(0x84,0x10); */
  96. w0(pi->saved_r0);
  97. w2(1); w2(4);
  98. w0(pi->saved_r0);
  99. w2(pi->saved_r2);
  100. }
  101. static void epia_read_block( PIA *pi, char * buf, int count )
  102. { int k, ph, a, b;
  103. switch (pi->mode) {
  104. case 0: w0(0x81); w2(1); w2(3); w0(0xc1);
  105. ph = 1;
  106. for (k=0;k<count;k++) {
  107. w2(2+ph); a = r1();
  108. w2(4+ph); b = r1();
  109. buf[k] = j44(a,b);
  110. ph = 1 - ph;
  111. }
  112. w0(0); w2(4);
  113. break;
  114. case 1: w0(0x91); w2(1); w0(0x10); w2(3);
  115. w0(0x51); w2(5); w0(0xd1);
  116. ph = 1;
  117. for (k=0;k<count;k++) {
  118. w2(4+ph);
  119. a = r1(); b = r2();
  120. buf[k] = j53(a,b);
  121. ph = 1 - ph;
  122. }
  123. w0(0); w2(4);
  124. break;
  125. case 2: w0(0x89); w2(1); w2(0x23); w2(0x21);
  126. ph = 1;
  127. for (k=0;k<count;k++) {
  128. w2(0x24+ph);
  129. buf[k] = r0();
  130. ph = 1 - ph;
  131. }
  132. w2(6); w2(4);
  133. break;
  134. case 3: if (count > 512) WR(0x84,3);
  135. w3(0); w2(0x24);
  136. for (k=0;k<count;k++) buf[k] = r4();
  137. w2(4); WR(0x84,0);
  138. break;
  139. case 4: if (count > 512) WR(0x84,3);
  140. w3(0); w2(0x24);
  141. for (k=0;k<count/2;k++) ((u16 *)buf)[k] = r4w();
  142. w2(4); WR(0x84,0);
  143. break;
  144. case 5: if (count > 512) WR(0x84,3);
  145. w3(0); w2(0x24);
  146. for (k=0;k<count/4;k++) ((u32 *)buf)[k] = r4l();
  147. w2(4); WR(0x84,0);
  148. break;
  149. }
  150. }
  151. static void epia_write_block( PIA *pi, char * buf, int count )
  152. { int ph, k, last, d;
  153. switch (pi->mode) {
  154. case 0:
  155. case 1:
  156. case 2: w0(0xa1); w2(1); w2(3); w2(1); w2(5);
  157. ph = 0; last = 0x8000;
  158. for (k=0;k<count;k++) {
  159. d = buf[k];
  160. if (d != last) { last = d; w0(d); }
  161. w2(4+ph);
  162. ph = 1 - ph;
  163. }
  164. w2(7); w2(4);
  165. break;
  166. case 3: if (count < 512) WR(0x84,1);
  167. w3(0x40);
  168. for (k=0;k<count;k++) w4(buf[k]);
  169. if (count < 512) WR(0x84,0);
  170. break;
  171. case 4: if (count < 512) WR(0x84,1);
  172. w3(0x40);
  173. for (k=0;k<count/2;k++) w4w(((u16 *)buf)[k]);
  174. if (count < 512) WR(0x84,0);
  175. break;
  176. case 5: if (count < 512) WR(0x84,1);
  177. w3(0x40);
  178. for (k=0;k<count/4;k++) w4l(((u32 *)buf)[k]);
  179. if (count < 512) WR(0x84,0);
  180. break;
  181. }
  182. }
  183. static int epia_test_proto( PIA *pi, char * scratch, int verbose )
  184. { int j, k, f;
  185. int e[2] = {0,0};
  186. epia_connect(pi);
  187. for (j=0;j<2;j++) {
  188. WR(6,0xa0+j*0x10);
  189. for (k=0;k<256;k++) {
  190. WR(2,k^0xaa);
  191. WR(3,k^0x55);
  192. if (RR(2) != (k^0xaa)) e[j]++;
  193. }
  194. WR(2,1); WR(3,1);
  195. }
  196. epia_disconnect(pi);
  197. f = 0;
  198. epia_connect(pi);
  199. WR(0x84,8);
  200. epia_read_block(pi,scratch,512);
  201. for (k=0;k<256;k++) {
  202. if ((scratch[2*k] & 0xff) != ((k+1) & 0xff)) f++;
  203. if ((scratch[2*k+1] & 0xff) != ((-2-k) & 0xff)) f++;
  204. }
  205. WR(0x84,0);
  206. epia_disconnect(pi);
  207. if (verbose) {
  208. printk("%s: epia: port 0x%x, mode %d, test=(%d,%d,%d)\n",
  209. pi->device,pi->port,pi->mode,e[0],e[1],f);
  210. }
  211. return (e[0] && e[1]) || f;
  212. }
  213. static void epia_log_adapter( PIA *pi, char * scratch, int verbose )
  214. { char *mode_string[6] = {"4-bit","5/3","8-bit",
  215. "EPP-8","EPP-16","EPP-32"};
  216. printk("%s: epia %s, Shuttle EPIA at 0x%x, ",
  217. pi->device,EPIA_VERSION,pi->port);
  218. printk("mode %d (%s), delay %d\n",pi->mode,
  219. mode_string[pi->mode],pi->delay);
  220. }
  221. static struct pi_protocol epia = {
  222. .owner = THIS_MODULE,
  223. .name = "epia",
  224. .max_mode = 6,
  225. .epp_first = 3,
  226. .default_delay = 1,
  227. .max_units = 1,
  228. .write_regr = epia_write_regr,
  229. .read_regr = epia_read_regr,
  230. .write_block = epia_write_block,
  231. .read_block = epia_read_block,
  232. .connect = epia_connect,
  233. .disconnect = epia_disconnect,
  234. .test_proto = epia_test_proto,
  235. .log_adapter = epia_log_adapter,
  236. };
  237. static int __init epia_init(void)
  238. {
  239. return paride_register(&epia);
  240. }
  241. static void __exit epia_exit(void)
  242. {
  243. paride_unregister(&epia);
  244. }
  245. MODULE_LICENSE("GPL");
  246. module_init(epia_init)
  247. module_exit(epia_exit)