bpck.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. bpck.c (c) 1996-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. bpck.c is a low-level protocol driver for the MicroSolutions
  5. "backpack" parallel port IDE adapter.
  6. */
  7. /* Changes:
  8. 1.01 GRG 1998.05.05 init_proto, release_proto, pi->delay
  9. 1.02 GRG 1998.08.15 default pi->delay returned to 4
  10. */
  11. #define BPCK_VERSION "1.02"
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/wait.h>
  18. #include <asm/io.h>
  19. #include "paride.h"
  20. #undef r2
  21. #undef w2
  22. #define PC pi->private
  23. #define r2() (PC=(in_p(2) & 0xff))
  24. #define w2(byte) {out_p(2,byte); PC = byte;}
  25. #define t2(pat) {PC ^= pat; out_p(2,PC);}
  26. #define e2() {PC &= 0xfe; out_p(2,PC);}
  27. #define o2() {PC |= 1; out_p(2,PC);}
  28. #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
  29. /* cont = 0 - access the IDE register file
  30. cont = 1 - access the IDE command set
  31. cont = 2 - use internal bpck register addressing
  32. */
  33. static int cont_map[3] = { 0x40, 0x48, 0 };
  34. static int bpck_read_regr( PIA *pi, int cont, int regr )
  35. { int r, l, h;
  36. r = regr + cont_map[cont];
  37. switch (pi->mode) {
  38. case 0: w0(r & 0xf); w0(r); t2(2); t2(4);
  39. l = r1();
  40. t2(4);
  41. h = r1();
  42. return j44(l,h);
  43. case 1: w0(r & 0xf); w0(r); t2(2);
  44. e2(); t2(0x20);
  45. t2(4); h = r0();
  46. t2(1); t2(0x20);
  47. return h;
  48. case 2:
  49. case 3:
  50. case 4: w0(r); w2(9); w2(0); w2(0x20);
  51. h = r4();
  52. w2(0);
  53. return h;
  54. }
  55. return -1;
  56. }
  57. static void bpck_write_regr( PIA *pi, int cont, int regr, int val )
  58. { int r;
  59. r = regr + cont_map[cont];
  60. switch (pi->mode) {
  61. case 0:
  62. case 1: w0(r);
  63. t2(2);
  64. w0(val);
  65. o2(); t2(4); t2(1);
  66. break;
  67. case 2:
  68. case 3:
  69. case 4: w0(r); w2(9); w2(0);
  70. w0(val); w2(1); w2(3); w2(0);
  71. break;
  72. }
  73. }
  74. /* These macros access the bpck registers in native addressing */
  75. #define WR(r,v) bpck_write_regr(pi,2,r,v)
  76. #define RR(r) (bpck_read_regr(pi,2,r))
  77. static void bpck_write_block( PIA *pi, char * buf, int count )
  78. { int i;
  79. switch (pi->mode) {
  80. case 0: WR(4,0x40);
  81. w0(0x40); t2(2); t2(1);
  82. for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
  83. WR(4,0);
  84. break;
  85. case 1: WR(4,0x50);
  86. w0(0x40); t2(2); t2(1);
  87. for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
  88. WR(4,0x10);
  89. break;
  90. case 2: WR(4,0x48);
  91. w0(0x40); w2(9); w2(0); w2(1);
  92. for (i=0;i<count;i++) w4(buf[i]);
  93. w2(0);
  94. WR(4,8);
  95. break;
  96. case 3: WR(4,0x48);
  97. w0(0x40); w2(9); w2(0); w2(1);
  98. for (i=0;i<count/2;i++) w4w(((u16 *)buf)[i]);
  99. w2(0);
  100. WR(4,8);
  101. break;
  102. case 4: WR(4,0x48);
  103. w0(0x40); w2(9); w2(0); w2(1);
  104. for (i=0;i<count/4;i++) w4l(((u32 *)buf)[i]);
  105. w2(0);
  106. WR(4,8);
  107. break;
  108. }
  109. }
  110. static void bpck_read_block( PIA *pi, char * buf, int count )
  111. { int i, l, h;
  112. switch (pi->mode) {
  113. case 0: WR(4,0x40);
  114. w0(0x40); t2(2);
  115. for (i=0;i<count;i++) {
  116. t2(4); l = r1();
  117. t2(4); h = r1();
  118. buf[i] = j44(l,h);
  119. }
  120. WR(4,0);
  121. break;
  122. case 1: WR(4,0x50);
  123. w0(0x40); t2(2); t2(0x20);
  124. for(i=0;i<count;i++) { t2(4); buf[i] = r0(); }
  125. t2(1); t2(0x20);
  126. WR(4,0x10);
  127. break;
  128. case 2: WR(4,0x48);
  129. w0(0x40); w2(9); w2(0); w2(0x20);
  130. for (i=0;i<count;i++) buf[i] = r4();
  131. w2(0);
  132. WR(4,8);
  133. break;
  134. case 3: WR(4,0x48);
  135. w0(0x40); w2(9); w2(0); w2(0x20);
  136. for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
  137. w2(0);
  138. WR(4,8);
  139. break;
  140. case 4: WR(4,0x48);
  141. w0(0x40); w2(9); w2(0); w2(0x20);
  142. for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
  143. w2(0);
  144. WR(4,8);
  145. break;
  146. }
  147. }
  148. static int bpck_probe_unit ( PIA *pi )
  149. { int o1, o0, f7, id;
  150. int t, s;
  151. id = pi->unit;
  152. s = 0;
  153. w2(4); w2(0xe); r2(); t2(2);
  154. o1 = r1()&0xf8;
  155. o0 = r0();
  156. w0(255-id); w2(4); w0(id);
  157. t2(8); t2(8); t2(8);
  158. t2(2); t = r1()&0xf8;
  159. f7 = ((id % 8) == 7);
  160. if ((f7) || (t != o1)) { t2(2); s = r1()&0xf8; }
  161. if ((t == o1) && ((!f7) || (s == o1))) {
  162. w2(0x4c); w0(o0);
  163. return 0;
  164. }
  165. t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
  166. return 1;
  167. }
  168. static void bpck_connect ( PIA *pi )
  169. { pi->saved_r0 = r0();
  170. w0(0xff-pi->unit); w2(4); w0(pi->unit);
  171. t2(8); t2(8); t2(8);
  172. t2(2); t2(2);
  173. switch (pi->mode) {
  174. case 0: t2(8); WR(4,0);
  175. break;
  176. case 1: t2(8); WR(4,0x10);
  177. break;
  178. case 2:
  179. case 3:
  180. case 4: w2(0); WR(4,8);
  181. break;
  182. }
  183. WR(5,8);
  184. if (pi->devtype == PI_PCD) {
  185. WR(0x46,0x10); /* fiddle with ESS logic ??? */
  186. WR(0x4c,0x38);
  187. WR(0x4d,0x88);
  188. WR(0x46,0xa0);
  189. WR(0x41,0);
  190. WR(0x4e,8);
  191. }
  192. }
  193. static void bpck_disconnect ( PIA *pi )
  194. { w0(0);
  195. if (pi->mode >= 2) { w2(9); w2(0); } else t2(2);
  196. w2(0x4c); w0(pi->saved_r0);
  197. }
  198. static void bpck_force_spp ( PIA *pi )
  199. /* This fakes the EPP protocol to turn off EPP ... */
  200. { pi->saved_r0 = r0();
  201. w0(0xff-pi->unit); w2(4); w0(pi->unit);
  202. t2(8); t2(8); t2(8);
  203. t2(2); t2(2);
  204. w2(0);
  205. w0(4); w2(9); w2(0);
  206. w0(0); w2(1); w2(3); w2(0);
  207. w0(0); w2(9); w2(0);
  208. w2(0x4c); w0(pi->saved_r0);
  209. }
  210. #define TEST_LEN 16
  211. static int bpck_test_proto( PIA *pi, char * scratch, int verbose )
  212. { int i, e, l, h, om;
  213. char buf[TEST_LEN];
  214. bpck_force_spp(pi);
  215. switch (pi->mode) {
  216. case 0: bpck_connect(pi);
  217. WR(0x13,0x7f);
  218. w0(0x13); t2(2);
  219. for(i=0;i<TEST_LEN;i++) {
  220. t2(4); l = r1();
  221. t2(4); h = r1();
  222. buf[i] = j44(l,h);
  223. }
  224. bpck_disconnect(pi);
  225. break;
  226. case 1: bpck_connect(pi);
  227. WR(0x13,0x7f);
  228. w0(0x13); t2(2); t2(0x20);
  229. for(i=0;i<TEST_LEN;i++) { t2(4); buf[i] = r0(); }
  230. t2(1); t2(0x20);
  231. bpck_disconnect(pi);
  232. break;
  233. case 2:
  234. case 3:
  235. case 4: om = pi->mode;
  236. pi->mode = 0;
  237. bpck_connect(pi);
  238. WR(7,3);
  239. WR(4,8);
  240. bpck_disconnect(pi);
  241. pi->mode = om;
  242. bpck_connect(pi);
  243. w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
  244. switch (pi->mode) {
  245. case 2: for (i=0;i<TEST_LEN;i++) buf[i] = r4();
  246. break;
  247. case 3: for (i=0;i<TEST_LEN/2;i++) ((u16 *)buf)[i] = r4w();
  248. break;
  249. case 4: for (i=0;i<TEST_LEN/4;i++) ((u32 *)buf)[i] = r4l();
  250. break;
  251. }
  252. w2(0);
  253. WR(7,0);
  254. bpck_disconnect(pi);
  255. break;
  256. }
  257. if (verbose) {
  258. printk("%s: bpck: 0x%x unit %d mode %d: ",
  259. pi->device,pi->port,pi->unit,pi->mode);
  260. for (i=0;i<TEST_LEN;i++) printk("%3d",buf[i]);
  261. printk("\n");
  262. }
  263. e = 0;
  264. for (i=0;i<TEST_LEN;i++) if (buf[i] != (i+1)) e++;
  265. return e;
  266. }
  267. static void bpck_read_eeprom ( PIA *pi, char * buf )
  268. { int i,j,k,n,p,v,f, om, od;
  269. bpck_force_spp(pi);
  270. om = pi->mode; od = pi->delay;
  271. pi->mode = 0; pi->delay = 6;
  272. bpck_connect(pi);
  273. n = 0;
  274. WR(4,0);
  275. for (i=0;i<64;i++) {
  276. WR(6,8);
  277. WR(6,0xc);
  278. p = 0x100;
  279. for (k=0;k<9;k++) {
  280. f = (((i + 0x180) & p) != 0) * 2;
  281. WR(6,f+0xc);
  282. WR(6,f+0xd);
  283. WR(6,f+0xc);
  284. p = (p >> 1);
  285. }
  286. for (j=0;j<2;j++) {
  287. v = 0;
  288. for (k=0;k<8;k++) {
  289. WR(6,0xc);
  290. WR(6,0xd);
  291. WR(6,0xc);
  292. f = RR(0);
  293. v = 2*v + (f == 0x84);
  294. }
  295. buf[2*i+1-j] = v;
  296. }
  297. }
  298. WR(6,8);
  299. WR(6,0);
  300. WR(5,8);
  301. bpck_disconnect(pi);
  302. if (om >= 2) {
  303. bpck_connect(pi);
  304. WR(7,3);
  305. WR(4,8);
  306. bpck_disconnect(pi);
  307. }
  308. pi->mode = om; pi->delay = od;
  309. }
  310. static int bpck_test_port ( PIA *pi ) /* check for 8-bit port */
  311. { int i, r, m;
  312. w2(0x2c); i = r0(); w0(255-i); r = r0(); w0(i);
  313. m = -1;
  314. if (r == i) m = 2;
  315. if (r == (255-i)) m = 0;
  316. w2(0xc); i = r0(); w0(255-i); r = r0(); w0(i);
  317. if (r != (255-i)) m = -1;
  318. if (m == 0) { w2(6); w2(0xc); r = r0(); w0(0xaa); w0(r); w0(0xaa); }
  319. if (m == 2) { w2(0x26); w2(0xc); }
  320. if (m == -1) return 0;
  321. return 5;
  322. }
  323. static void bpck_log_adapter( PIA *pi, char * scratch, int verbose )
  324. { char *mode_string[5] = { "4-bit","8-bit","EPP-8",
  325. "EPP-16","EPP-32" };
  326. #ifdef DUMP_EEPROM
  327. int i;
  328. #endif
  329. bpck_read_eeprom(pi,scratch);
  330. #ifdef DUMP_EEPROM
  331. if (verbose) {
  332. for(i=0;i<128;i++)
  333. if ((scratch[i] < ' ') || (scratch[i] > '~'))
  334. scratch[i] = '.';
  335. printk("%s: bpck EEPROM: %64.64s\n",pi->device,scratch);
  336. printk("%s: %64.64s\n",pi->device,&scratch[64]);
  337. }
  338. #endif
  339. printk("%s: bpck %s, backpack %8.8s unit %d",
  340. pi->device,BPCK_VERSION,&scratch[110],pi->unit);
  341. printk(" at 0x%x, mode %d (%s), delay %d\n",pi->port,
  342. pi->mode,mode_string[pi->mode],pi->delay);
  343. }
  344. static struct pi_protocol bpck = {
  345. .owner = THIS_MODULE,
  346. .name = "bpck",
  347. .max_mode = 5,
  348. .epp_first = 2,
  349. .default_delay = 4,
  350. .max_units = 255,
  351. .write_regr = bpck_write_regr,
  352. .read_regr = bpck_read_regr,
  353. .write_block = bpck_write_block,
  354. .read_block = bpck_read_block,
  355. .connect = bpck_connect,
  356. .disconnect = bpck_disconnect,
  357. .test_port = bpck_test_port,
  358. .probe_unit = bpck_probe_unit,
  359. .test_proto = bpck_test_proto,
  360. .log_adapter = bpck_log_adapter,
  361. };
  362. static int __init bpck_init(void)
  363. {
  364. return paride_register(&bpck);
  365. }
  366. static void __exit bpck_exit(void)
  367. {
  368. paride_unregister(&bpck);
  369. }
  370. MODULE_LICENSE("GPL");
  371. module_init(bpck_init)
  372. module_exit(bpck_exit)