bpck.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. bpck.c (c) 1996-8 Grant R. Guenther <[email protected]>
  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. #undef PC
  23. #define PC pi->private
  24. #define r2() (PC=(in_p(2) & 0xff))
  25. #define w2(byte) {out_p(2,byte); PC = byte;}
  26. #define t2(pat) {PC ^= pat; out_p(2,PC);}
  27. #define e2() {PC &= 0xfe; out_p(2,PC);}
  28. #define o2() {PC |= 1; out_p(2,PC);}
  29. #define j44(l,h) (((l>>3)&0x7)|((l>>4)&0x8)|((h<<1)&0x70)|(h&0x80))
  30. /* cont = 0 - access the IDE register file
  31. cont = 1 - access the IDE command set
  32. cont = 2 - use internal bpck register addressing
  33. */
  34. static int cont_map[3] = { 0x40, 0x48, 0 };
  35. static int bpck_read_regr( PIA *pi, int cont, int regr )
  36. { int r, l, h;
  37. r = regr + cont_map[cont];
  38. switch (pi->mode) {
  39. case 0: w0(r & 0xf); w0(r); t2(2); t2(4);
  40. l = r1();
  41. t2(4);
  42. h = r1();
  43. return j44(l,h);
  44. case 1: w0(r & 0xf); w0(r); t2(2);
  45. e2(); t2(0x20);
  46. t2(4); h = r0();
  47. t2(1); t2(0x20);
  48. return h;
  49. case 2:
  50. case 3:
  51. case 4: w0(r); w2(9); w2(0); w2(0x20);
  52. h = r4();
  53. w2(0);
  54. return h;
  55. }
  56. return -1;
  57. }
  58. static void bpck_write_regr( PIA *pi, int cont, int regr, int val )
  59. { int r;
  60. r = regr + cont_map[cont];
  61. switch (pi->mode) {
  62. case 0:
  63. case 1: w0(r);
  64. t2(2);
  65. w0(val);
  66. o2(); t2(4); t2(1);
  67. break;
  68. case 2:
  69. case 3:
  70. case 4: w0(r); w2(9); w2(0);
  71. w0(val); w2(1); w2(3); w2(0);
  72. break;
  73. }
  74. }
  75. /* These macros access the bpck registers in native addressing */
  76. #define WR(r,v) bpck_write_regr(pi,2,r,v)
  77. #define RR(r) (bpck_read_regr(pi,2,r))
  78. static void bpck_write_block( PIA *pi, char * buf, int count )
  79. { int i;
  80. switch (pi->mode) {
  81. case 0: WR(4,0x40);
  82. w0(0x40); t2(2); t2(1);
  83. for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
  84. WR(4,0);
  85. break;
  86. case 1: WR(4,0x50);
  87. w0(0x40); t2(2); t2(1);
  88. for (i=0;i<count;i++) { w0(buf[i]); t2(4); }
  89. WR(4,0x10);
  90. break;
  91. case 2: WR(4,0x48);
  92. w0(0x40); w2(9); w2(0); w2(1);
  93. for (i=0;i<count;i++) w4(buf[i]);
  94. w2(0);
  95. WR(4,8);
  96. break;
  97. case 3: WR(4,0x48);
  98. w0(0x40); w2(9); w2(0); w2(1);
  99. for (i=0;i<count/2;i++) w4w(((u16 *)buf)[i]);
  100. w2(0);
  101. WR(4,8);
  102. break;
  103. case 4: WR(4,0x48);
  104. w0(0x40); w2(9); w2(0); w2(1);
  105. for (i=0;i<count/4;i++) w4l(((u32 *)buf)[i]);
  106. w2(0);
  107. WR(4,8);
  108. break;
  109. }
  110. }
  111. static void bpck_read_block( PIA *pi, char * buf, int count )
  112. { int i, l, h;
  113. switch (pi->mode) {
  114. case 0: WR(4,0x40);
  115. w0(0x40); t2(2);
  116. for (i=0;i<count;i++) {
  117. t2(4); l = r1();
  118. t2(4); h = r1();
  119. buf[i] = j44(l,h);
  120. }
  121. WR(4,0);
  122. break;
  123. case 1: WR(4,0x50);
  124. w0(0x40); t2(2); t2(0x20);
  125. for(i=0;i<count;i++) { t2(4); buf[i] = r0(); }
  126. t2(1); t2(0x20);
  127. WR(4,0x10);
  128. break;
  129. case 2: WR(4,0x48);
  130. w0(0x40); w2(9); w2(0); w2(0x20);
  131. for (i=0;i<count;i++) buf[i] = r4();
  132. w2(0);
  133. WR(4,8);
  134. break;
  135. case 3: WR(4,0x48);
  136. w0(0x40); w2(9); w2(0); w2(0x20);
  137. for (i=0;i<count/2;i++) ((u16 *)buf)[i] = r4w();
  138. w2(0);
  139. WR(4,8);
  140. break;
  141. case 4: WR(4,0x48);
  142. w0(0x40); w2(9); w2(0); w2(0x20);
  143. for (i=0;i<count/4;i++) ((u32 *)buf)[i] = r4l();
  144. w2(0);
  145. WR(4,8);
  146. break;
  147. }
  148. }
  149. static int bpck_probe_unit ( PIA *pi )
  150. { int o1, o0, f7, id;
  151. int t, s;
  152. id = pi->unit;
  153. s = 0;
  154. w2(4); w2(0xe); r2(); t2(2);
  155. o1 = r1()&0xf8;
  156. o0 = r0();
  157. w0(255-id); w2(4); w0(id);
  158. t2(8); t2(8); t2(8);
  159. t2(2); t = r1()&0xf8;
  160. f7 = ((id % 8) == 7);
  161. if ((f7) || (t != o1)) { t2(2); s = r1()&0xf8; }
  162. if ((t == o1) && ((!f7) || (s == o1))) {
  163. w2(0x4c); w0(o0);
  164. return 0;
  165. }
  166. t2(8); w0(0); t2(2); w2(0x4c); w0(o0);
  167. return 1;
  168. }
  169. static void bpck_connect ( PIA *pi )
  170. { pi->saved_r0 = r0();
  171. w0(0xff-pi->unit); w2(4); w0(pi->unit);
  172. t2(8); t2(8); t2(8);
  173. t2(2); t2(2);
  174. switch (pi->mode) {
  175. case 0: t2(8); WR(4,0);
  176. break;
  177. case 1: t2(8); WR(4,0x10);
  178. break;
  179. case 2:
  180. case 3:
  181. case 4: w2(0); WR(4,8);
  182. break;
  183. }
  184. WR(5,8);
  185. if (pi->devtype == PI_PCD) {
  186. WR(0x46,0x10); /* fiddle with ESS logic ??? */
  187. WR(0x4c,0x38);
  188. WR(0x4d,0x88);
  189. WR(0x46,0xa0);
  190. WR(0x41,0);
  191. WR(0x4e,8);
  192. }
  193. }
  194. static void bpck_disconnect ( PIA *pi )
  195. { w0(0);
  196. if (pi->mode >= 2) { w2(9); w2(0); } else t2(2);
  197. w2(0x4c); w0(pi->saved_r0);
  198. }
  199. static void bpck_force_spp ( PIA *pi )
  200. /* This fakes the EPP protocol to turn off EPP ... */
  201. { pi->saved_r0 = r0();
  202. w0(0xff-pi->unit); w2(4); w0(pi->unit);
  203. t2(8); t2(8); t2(8);
  204. t2(2); t2(2);
  205. w2(0);
  206. w0(4); w2(9); w2(0);
  207. w0(0); w2(1); w2(3); w2(0);
  208. w0(0); w2(9); w2(0);
  209. w2(0x4c); w0(pi->saved_r0);
  210. }
  211. #define TEST_LEN 16
  212. static int bpck_test_proto( PIA *pi, char * scratch, int verbose )
  213. { int i, e, l, h, om;
  214. char buf[TEST_LEN];
  215. bpck_force_spp(pi);
  216. switch (pi->mode) {
  217. case 0: bpck_connect(pi);
  218. WR(0x13,0x7f);
  219. w0(0x13); t2(2);
  220. for(i=0;i<TEST_LEN;i++) {
  221. t2(4); l = r1();
  222. t2(4); h = r1();
  223. buf[i] = j44(l,h);
  224. }
  225. bpck_disconnect(pi);
  226. break;
  227. case 1: bpck_connect(pi);
  228. WR(0x13,0x7f);
  229. w0(0x13); t2(2); t2(0x20);
  230. for(i=0;i<TEST_LEN;i++) { t2(4); buf[i] = r0(); }
  231. t2(1); t2(0x20);
  232. bpck_disconnect(pi);
  233. break;
  234. case 2:
  235. case 3:
  236. case 4: om = pi->mode;
  237. pi->mode = 0;
  238. bpck_connect(pi);
  239. WR(7,3);
  240. WR(4,8);
  241. bpck_disconnect(pi);
  242. pi->mode = om;
  243. bpck_connect(pi);
  244. w0(0x13); w2(9); w2(1); w0(0); w2(3); w2(0); w2(0xe0);
  245. switch (pi->mode) {
  246. case 2: for (i=0;i<TEST_LEN;i++) buf[i] = r4();
  247. break;
  248. case 3: for (i=0;i<TEST_LEN/2;i++) ((u16 *)buf)[i] = r4w();
  249. break;
  250. case 4: for (i=0;i<TEST_LEN/4;i++) ((u32 *)buf)[i] = r4l();
  251. break;
  252. }
  253. w2(0);
  254. WR(7,0);
  255. bpck_disconnect(pi);
  256. break;
  257. }
  258. if (verbose) {
  259. printk("%s: bpck: 0x%x unit %d mode %d: ",
  260. pi->device,pi->port,pi->unit,pi->mode);
  261. for (i=0;i<TEST_LEN;i++) printk("%3d",buf[i]);
  262. printk("\n");
  263. }
  264. e = 0;
  265. for (i=0;i<TEST_LEN;i++) if (buf[i] != (i+1)) e++;
  266. return e;
  267. }
  268. static void bpck_read_eeprom ( PIA *pi, char * buf )
  269. { int i, j, k, p, v, f, om, od;
  270. bpck_force_spp(pi);
  271. om = pi->mode; od = pi->delay;
  272. pi->mode = 0; pi->delay = 6;
  273. bpck_connect(pi);
  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)