Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / block / paride / ppc6lnx.c
blob5e5521d3b1dde99d41792835c6d7fa01cce1f982
1 /*
2 ppc6lnx.c (c) 2001 Micro Solutions Inc.
3 Released under the terms of the GNU General Public license
5 ppc6lnx.c is a par of the protocol driver for the Micro Solutions
6 "BACKPACK" parallel port IDE adapter
7 (Works on Series 6 drives)
9 */
11 //***************************************************************************
13 // PPC 6 Code in C sanitized for LINUX
14 // Original x86 ASM by Ron, Converted to C by Clive
16 //***************************************************************************
19 #define port_stb 1
20 #define port_afd 2
21 #define cmd_stb port_afd
22 #define port_init 4
23 #define data_stb port_init
24 #define port_sel 8
25 #define port_int 16
26 #define port_dir 0x20
28 #define ECR_EPP 0x80
29 #define ECR_BI 0x20
31 //***************************************************************************
33 // 60772 Commands
35 #define ACCESS_REG 0x00
36 #define ACCESS_PORT 0x40
38 #define ACCESS_READ 0x00
39 #define ACCESS_WRITE 0x20
41 // 60772 Command Prefix
43 #define CMD_PREFIX_SET 0xe0 // Special command that modifies the next command's operation
44 #define CMD_PREFIX_RESET 0xc0 // Resets current cmd modifier reg bits
45 #define PREFIX_IO16 0x01 // perform 16-bit wide I/O
46 #define PREFIX_FASTWR 0x04 // enable PPC mode fast-write
47 #define PREFIX_BLK 0x08 // enable block transfer mode
49 // 60772 Registers
51 #define REG_STATUS 0x00 // status register
52 #define STATUS_IRQA 0x01 // Peripheral IRQA line
53 #define STATUS_EEPROM_DO 0x40 // Serial EEPROM data bit
54 #define REG_VERSION 0x01 // PPC version register (read)
55 #define REG_HWCFG 0x02 // Hardware Config register
56 #define REG_RAMSIZE 0x03 // Size of RAM Buffer
57 #define RAMSIZE_128K 0x02
58 #define REG_EEPROM 0x06 // EEPROM control register
59 #define EEPROM_SK 0x01 // eeprom SK bit
60 #define EEPROM_DI 0x02 // eeprom DI bit
61 #define EEPROM_CS 0x04 // eeprom CS bit
62 #define EEPROM_EN 0x08 // eeprom output enable
63 #define REG_BLKSIZE 0x08 // Block transfer len (24 bit)
65 //***************************************************************************
67 typedef struct ppc_storage {
68 u16 lpt_addr; // LPT base address
69 u8 ppc_id;
70 u8 mode; // operating mode
71 // 0 = PPC Uni SW
72 // 1 = PPC Uni FW
73 // 2 = PPC Bi SW
74 // 3 = PPC Bi FW
75 // 4 = EPP Byte
76 // 5 = EPP Word
77 // 6 = EPP Dword
78 u8 ppc_flags;
79 u8 org_data; // original LPT data port contents
80 u8 org_ctrl; // original LPT control port contents
81 u8 cur_ctrl; // current control port contents
82 } Interface;
84 //***************************************************************************
86 // ppc_flags
88 #define fifo_wait 0x10
90 //***************************************************************************
92 // DONT CHANGE THESE LEST YOU BREAK EVERYTHING - BIT FIELD DEPENDENCIES
94 #define PPCMODE_UNI_SW 0
95 #define PPCMODE_UNI_FW 1
96 #define PPCMODE_BI_SW 2
97 #define PPCMODE_BI_FW 3
98 #define PPCMODE_EPP_BYTE 4
99 #define PPCMODE_EPP_WORD 5
100 #define PPCMODE_EPP_DWORD 6
102 //***************************************************************************
104 static int ppc6_select(Interface *ppc);
105 static void ppc6_deselect(Interface *ppc);
106 static void ppc6_send_cmd(Interface *ppc, u8 cmd);
107 static void ppc6_wr_data_byte(Interface *ppc, u8 data);
108 static u8 ppc6_rd_data_byte(Interface *ppc);
109 static u8 ppc6_rd_port(Interface *ppc, u8 port);
110 static void ppc6_wr_port(Interface *ppc, u8 port, u8 data);
111 static void ppc6_rd_data_blk(Interface *ppc, u8 *data, long count);
112 static void ppc6_wait_for_fifo(Interface *ppc);
113 static void ppc6_wr_data_blk(Interface *ppc, u8 *data, long count);
114 static void ppc6_rd_port16_blk(Interface *ppc, u8 port, u8 *data, long length);
115 static void ppc6_wr_port16_blk(Interface *ppc, u8 port, u8 *data, long length);
116 static void ppc6_wr_extout(Interface *ppc, u8 regdata);
117 static int ppc6_open(Interface *ppc);
118 static void ppc6_close(Interface *ppc);
120 //***************************************************************************
122 static int ppc6_select(Interface *ppc)
124 u8 i, j, k;
126 i = inb(ppc->lpt_addr + 1);
128 if (i & 1)
129 outb(i, ppc->lpt_addr + 1);
131 ppc->org_data = inb(ppc->lpt_addr);
133 ppc->org_ctrl = inb(ppc->lpt_addr + 2) & 0x5F; // readback ctrl
135 ppc->cur_ctrl = ppc->org_ctrl;
137 ppc->cur_ctrl |= port_sel;
139 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
141 if (ppc->org_data == 'b')
142 outb('x', ppc->lpt_addr);
144 outb('b', ppc->lpt_addr);
145 outb('p', ppc->lpt_addr);
146 outb(ppc->ppc_id, ppc->lpt_addr);
147 outb(~ppc->ppc_id,ppc->lpt_addr);
149 ppc->cur_ctrl &= ~port_sel;
151 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
153 ppc->cur_ctrl = (ppc->cur_ctrl & port_int) | port_init;
155 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
157 i = ppc->mode & 0x0C;
159 if (i == 0)
160 i = (ppc->mode & 2) | 1;
162 outb(i, ppc->lpt_addr);
164 ppc->cur_ctrl |= port_sel;
166 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
168 // DELAY
170 ppc->cur_ctrl |= port_afd;
172 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
174 j = ((i & 0x08) << 4) | ((i & 0x07) << 3);
176 k = inb(ppc->lpt_addr + 1) & 0xB8;
178 if (j == k)
180 ppc->cur_ctrl &= ~port_afd;
182 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
184 k = (inb(ppc->lpt_addr + 1) & 0xB8) ^ 0xB8;
186 if (j == k)
188 if (i & 4) // EPP
189 ppc->cur_ctrl &= ~(port_sel | port_init);
190 else // PPC/ECP
191 ppc->cur_ctrl &= ~port_sel;
193 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
195 return(1);
199 outb(ppc->org_ctrl, ppc->lpt_addr + 2);
201 outb(ppc->org_data, ppc->lpt_addr);
203 return(0); // FAIL
206 //***************************************************************************
208 static void ppc6_deselect(Interface *ppc)
210 if (ppc->mode & 4) // EPP
211 ppc->cur_ctrl |= port_init;
212 else // PPC/ECP
213 ppc->cur_ctrl |= port_sel;
215 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
217 outb(ppc->org_data, ppc->lpt_addr);
219 outb((ppc->org_ctrl | port_sel), ppc->lpt_addr + 2);
221 outb(ppc->org_ctrl, ppc->lpt_addr + 2);
224 //***************************************************************************
226 static void ppc6_send_cmd(Interface *ppc, u8 cmd)
228 switch(ppc->mode)
230 case PPCMODE_UNI_SW :
231 case PPCMODE_UNI_FW :
232 case PPCMODE_BI_SW :
233 case PPCMODE_BI_FW :
235 outb(cmd, ppc->lpt_addr);
237 ppc->cur_ctrl ^= cmd_stb;
239 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
241 break;
244 case PPCMODE_EPP_BYTE :
245 case PPCMODE_EPP_WORD :
246 case PPCMODE_EPP_DWORD :
248 outb(cmd, ppc->lpt_addr + 3);
250 break;
255 //***************************************************************************
257 static void ppc6_wr_data_byte(Interface *ppc, u8 data)
259 switch(ppc->mode)
261 case PPCMODE_UNI_SW :
262 case PPCMODE_UNI_FW :
263 case PPCMODE_BI_SW :
264 case PPCMODE_BI_FW :
266 outb(data, ppc->lpt_addr);
268 ppc->cur_ctrl ^= data_stb;
270 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
272 break;
275 case PPCMODE_EPP_BYTE :
276 case PPCMODE_EPP_WORD :
277 case PPCMODE_EPP_DWORD :
279 outb(data, ppc->lpt_addr + 4);
281 break;
286 //***************************************************************************
288 static u8 ppc6_rd_data_byte(Interface *ppc)
290 u8 data = 0;
292 switch(ppc->mode)
294 case PPCMODE_UNI_SW :
295 case PPCMODE_UNI_FW :
297 ppc->cur_ctrl = (ppc->cur_ctrl & ~port_stb) ^ data_stb;
299 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
301 // DELAY
303 data = inb(ppc->lpt_addr + 1);
305 data = ((data & 0x80) >> 1) | ((data & 0x38) >> 3);
307 ppc->cur_ctrl |= port_stb;
309 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
311 // DELAY
313 data |= inb(ppc->lpt_addr + 1) & 0xB8;
315 break;
318 case PPCMODE_BI_SW :
319 case PPCMODE_BI_FW :
321 ppc->cur_ctrl |= port_dir;
323 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
325 ppc->cur_ctrl = (ppc->cur_ctrl | port_stb) ^ data_stb;
327 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
329 data = inb(ppc->lpt_addr);
331 ppc->cur_ctrl &= ~port_stb;
333 outb(ppc->cur_ctrl,ppc->lpt_addr + 2);
335 ppc->cur_ctrl &= ~port_dir;
337 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
339 break;
342 case PPCMODE_EPP_BYTE :
343 case PPCMODE_EPP_WORD :
344 case PPCMODE_EPP_DWORD :
346 outb((ppc->cur_ctrl | port_dir),ppc->lpt_addr + 2);
348 data = inb(ppc->lpt_addr + 4);
350 outb(ppc->cur_ctrl,ppc->lpt_addr + 2);
352 break;
356 return(data);
359 //***************************************************************************
361 static u8 ppc6_rd_port(Interface *ppc, u8 port)
363 ppc6_send_cmd(ppc,(u8)(port | ACCESS_PORT | ACCESS_READ));
365 return(ppc6_rd_data_byte(ppc));
368 //***************************************************************************
370 static void ppc6_wr_port(Interface *ppc, u8 port, u8 data)
372 ppc6_send_cmd(ppc,(u8)(port | ACCESS_PORT | ACCESS_WRITE));
374 ppc6_wr_data_byte(ppc, data);
377 //***************************************************************************
379 static void ppc6_rd_data_blk(Interface *ppc, u8 *data, long count)
381 switch(ppc->mode)
383 case PPCMODE_UNI_SW :
384 case PPCMODE_UNI_FW :
386 while(count)
388 u8 d;
390 ppc->cur_ctrl = (ppc->cur_ctrl & ~port_stb) ^ data_stb;
392 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
394 // DELAY
396 d = inb(ppc->lpt_addr + 1);
398 d = ((d & 0x80) >> 1) | ((d & 0x38) >> 3);
400 ppc->cur_ctrl |= port_stb;
402 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
404 // DELAY
406 d |= inb(ppc->lpt_addr + 1) & 0xB8;
408 *data++ = d;
409 count--;
412 break;
415 case PPCMODE_BI_SW :
416 case PPCMODE_BI_FW :
418 ppc->cur_ctrl |= port_dir;
420 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
422 ppc->cur_ctrl |= port_stb;
424 while(count)
426 ppc->cur_ctrl ^= data_stb;
428 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
430 *data++ = inb(ppc->lpt_addr);
431 count--;
434 ppc->cur_ctrl &= ~port_stb;
436 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
438 ppc->cur_ctrl &= ~port_dir;
440 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
442 break;
445 case PPCMODE_EPP_BYTE :
447 outb((ppc->cur_ctrl | port_dir), ppc->lpt_addr + 2);
449 // DELAY
451 while(count)
453 *data++ = inb(ppc->lpt_addr + 4);
454 count--;
457 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
459 break;
462 case PPCMODE_EPP_WORD :
464 outb((ppc->cur_ctrl | port_dir), ppc->lpt_addr + 2);
466 // DELAY
468 while(count > 1)
470 *((u16 *)data) = inw(ppc->lpt_addr + 4);
471 data += 2;
472 count -= 2;
475 while(count)
477 *data++ = inb(ppc->lpt_addr + 4);
478 count--;
481 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
483 break;
486 case PPCMODE_EPP_DWORD :
488 outb((ppc->cur_ctrl | port_dir),ppc->lpt_addr + 2);
490 // DELAY
492 while(count > 3)
494 *((u32 *)data) = inl(ppc->lpt_addr + 4);
495 data += 4;
496 count -= 4;
499 while(count)
501 *data++ = inb(ppc->lpt_addr + 4);
502 count--;
505 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
507 break;
513 //***************************************************************************
515 static void ppc6_wait_for_fifo(Interface *ppc)
517 int i;
519 if (ppc->ppc_flags & fifo_wait)
521 for(i=0; i<20; i++)
522 inb(ppc->lpt_addr + 1);
526 //***************************************************************************
528 static void ppc6_wr_data_blk(Interface *ppc, u8 *data, long count)
530 switch(ppc->mode)
532 case PPCMODE_UNI_SW :
533 case PPCMODE_BI_SW :
535 while(count--)
537 outb(*data++, ppc->lpt_addr);
539 ppc->cur_ctrl ^= data_stb;
541 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
544 break;
547 case PPCMODE_UNI_FW :
548 case PPCMODE_BI_FW :
550 u8 this, last;
552 ppc6_send_cmd(ppc,(CMD_PREFIX_SET | PREFIX_FASTWR));
554 ppc->cur_ctrl |= port_stb;
556 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
558 last = *data;
560 outb(last, ppc->lpt_addr);
562 while(count)
564 this = *data++;
565 count--;
567 if (this == last)
569 ppc->cur_ctrl ^= data_stb;
571 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
573 else
575 outb(this, ppc->lpt_addr);
577 last = this;
581 ppc->cur_ctrl &= ~port_stb;
583 outb(ppc->cur_ctrl, ppc->lpt_addr + 2);
585 ppc6_send_cmd(ppc,(CMD_PREFIX_RESET | PREFIX_FASTWR));
587 break;
590 case PPCMODE_EPP_BYTE :
592 while(count)
594 outb(*data++,ppc->lpt_addr + 4);
595 count--;
598 ppc6_wait_for_fifo(ppc);
600 break;
603 case PPCMODE_EPP_WORD :
605 while(count > 1)
607 outw(*((u16 *)data),ppc->lpt_addr + 4);
608 data += 2;
609 count -= 2;
612 while(count)
614 outb(*data++,ppc->lpt_addr + 4);
615 count--;
618 ppc6_wait_for_fifo(ppc);
620 break;
623 case PPCMODE_EPP_DWORD :
625 while(count > 3)
627 outl(*((u32 *)data),ppc->lpt_addr + 4);
628 data += 4;
629 count -= 4;
632 while(count)
634 outb(*data++,ppc->lpt_addr + 4);
635 count--;
638 ppc6_wait_for_fifo(ppc);
640 break;
645 //***************************************************************************
647 static void ppc6_rd_port16_blk(Interface *ppc, u8 port, u8 *data, long length)
649 length = length << 1;
651 ppc6_send_cmd(ppc, (REG_BLKSIZE | ACCESS_REG | ACCESS_WRITE));
652 ppc6_wr_data_byte(ppc,(u8)length);
653 ppc6_wr_data_byte(ppc,(u8)(length >> 8));
654 ppc6_wr_data_byte(ppc,0);
656 ppc6_send_cmd(ppc, (CMD_PREFIX_SET | PREFIX_IO16 | PREFIX_BLK));
658 ppc6_send_cmd(ppc, (u8)(port | ACCESS_PORT | ACCESS_READ));
660 ppc6_rd_data_blk(ppc, data, length);
662 ppc6_send_cmd(ppc, (CMD_PREFIX_RESET | PREFIX_IO16 | PREFIX_BLK));
665 //***************************************************************************
667 static void ppc6_wr_port16_blk(Interface *ppc, u8 port, u8 *data, long length)
669 length = length << 1;
671 ppc6_send_cmd(ppc, (REG_BLKSIZE | ACCESS_REG | ACCESS_WRITE));
672 ppc6_wr_data_byte(ppc,(u8)length);
673 ppc6_wr_data_byte(ppc,(u8)(length >> 8));
674 ppc6_wr_data_byte(ppc,0);
676 ppc6_send_cmd(ppc, (CMD_PREFIX_SET | PREFIX_IO16 | PREFIX_BLK));
678 ppc6_send_cmd(ppc, (u8)(port | ACCESS_PORT | ACCESS_WRITE));
680 ppc6_wr_data_blk(ppc, data, length);
682 ppc6_send_cmd(ppc, (CMD_PREFIX_RESET | PREFIX_IO16 | PREFIX_BLK));
685 //***************************************************************************
687 static void ppc6_wr_extout(Interface *ppc, u8 regdata)
689 ppc6_send_cmd(ppc,(REG_VERSION | ACCESS_REG | ACCESS_WRITE));
691 ppc6_wr_data_byte(ppc, (u8)((regdata & 0x03) << 6));
694 //***************************************************************************
696 static int ppc6_open(Interface *ppc)
698 int ret;
700 ret = ppc6_select(ppc);
702 if (ret == 0)
703 return(ret);
705 ppc->ppc_flags &= ~fifo_wait;
707 ppc6_send_cmd(ppc, (ACCESS_REG | ACCESS_WRITE | REG_RAMSIZE));
708 ppc6_wr_data_byte(ppc, RAMSIZE_128K);
710 ppc6_send_cmd(ppc, (ACCESS_REG | ACCESS_READ | REG_VERSION));
712 if ((ppc6_rd_data_byte(ppc) & 0x3F) == 0x0C)
713 ppc->ppc_flags |= fifo_wait;
715 return(ret);
718 //***************************************************************************
720 static void ppc6_close(Interface *ppc)
722 ppc6_deselect(ppc);
725 //***************************************************************************