2 * drivers/sbus/char/bpp.c
4 * Copyright (c) 1995 Picture Elements
5 * Stephen Williams (steve@icarus.com)
6 * Gus Baldauf (gbaldauf@ix.netcom.com)
8 * Linux/SPARC port by Peter Zaitcev.
9 * Integration into SPARC tree by Tom Dyas.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/version.h>
17 #include <linux/errno.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/ioport.h>
21 #include <linux/major.h>
23 #include <asm/uaccess.h>
27 # include <asm/system.h>
28 # include <asm/segment.h>
31 #if defined(__sparc__)
32 # include <linux/init.h>
33 # include <linux/delay.h> /* udelay() */
35 # include <asm/oplib.h> /* OpenProm Library */
36 # include <asm/sbus.h> /* struct linux_sbus *SBus_chain */
37 # include <asm/io.h> /* sparc_alloc_io() */
42 #define BPP_PROBE_CODE 0x55
45 static const unsigned BPP_MAJOR
= LP_MAJOR
;
46 static const char* dev_name
= "bpp";
48 /* When switching from compatibility to a mode where I can read, try
49 the following mode first. */
51 /* const unsigned char DEFAULT_ECP = 0x10; */
52 static const unsigned char DEFAULT_ECP
= 0x30;
53 static const unsigned char DEFAULT_NIBBLE
= 0x00;
56 * These are 1284 time constraints, in units of jiffies.
59 static const unsigned long TIME_PSetup
= 1;
60 static const unsigned long TIME_PResponse
= 6;
61 static const unsigned long TIME_IDLE_LIMIT
= 2000;
64 * One instance per supported subdevice...
68 enum IEEE_Mode
{ COMPATIBILITY
, NIBBLE
, ECP
, ECP_RLE
, EPP
};
71 unsigned present
: 1; /* True if the hardware exists */
72 unsigned enhanced
: 1; /* True if the hardware in "enhanced" */
73 unsigned opened
: 1; /* True if the device is opened already */
74 unsigned run_flag
: 1; /* True if waiting for a repeate byte */
76 unsigned char direction
; /* 0 --> out, 0x20 --> IN */
77 unsigned char pp_state
; /* State of host controlled pins. */
80 unsigned char run_length
;
81 unsigned char repeat_byte
;
83 /* These members manage timeouts for programmed delays */
84 struct wait_queue
*wait_queue
;
85 struct timer_list timer_list
;
88 static struct inst instances
[BPP_NO
];
92 const unsigned short base_addrs
[BPP_NO
] = { 0x278, 0x378, 0x3bc };
95 * These are for data access.
96 * Control lines accesses are hidden in set_bits() and get_bits().
97 * The exeption is the probe procedure, which is system-dependent.
99 #define bpp_outb_p(data, base) outb_p((data), (base))
100 #define bpp_inb(base) inb(base)
101 #define bpp_inb_p(base) inb_p(base)
104 * This method takes the pin values mask and sets the hardware pins to
105 * the requested value: 1 == high voltage, 0 == low voltage. This
106 * burries the annoying PC bit inversion and preserves the direction
109 static void set_pins(unsigned short pins
, unsigned minor
)
111 unsigned char bits
= instances
[minor
].direction
; /* == 0x20 */
113 if (! (pins
& BPP_PP_nStrobe
)) bits
|= 1;
114 if (! (pins
& BPP_PP_nAutoFd
)) bits
|= 2;
115 if ( pins
& BPP_PP_nInit
) bits
|= 4;
116 if (! (pins
& BPP_PP_nSelectIn
)) bits
|= 8;
118 instances
[minor
].pp_state
= bits
;
120 outb_p(bits
, base_addrs
[minor
]+2);
123 static unsigned short get_pins(unsigned minor
)
125 unsigned short bits
= 0;
127 unsigned value
= instances
[minor
].pp_state
;
128 if (! (value
& 0x01)) bits
|= BPP_PP_nStrobe
;
129 if (! (value
& 0x02)) bits
|= BPP_PP_nAutoFd
;
130 if (value
& 0x04) bits
|= BPP_PP_nInit
;
131 if (! (value
& 0x08)) bits
|= BPP_PP_nSelectIn
;
133 value
= inb_p(base_addrs
[minor
]+1);
134 if (value
& 0x08) bits
|= BPP_GP_nFault
;
135 if (value
& 0x10) bits
|= BPP_GP_Select
;
136 if (value
& 0x20) bits
|= BPP_GP_PError
;
137 if (value
& 0x40) bits
|= BPP_GP_nAck
;
138 if (! (value
& 0x80)) bits
|= BPP_GP_Busy
;
143 #endif /* __i386__ */
145 #if defined(__sparc__)
152 __u32 p_csr
; /* DMA Control/Status Register */
153 __u32 p_addr
; /* Address Register */
154 __u32 p_bcnt
; /* Byte Count Register */
155 __u32 p_tst_csr
; /* Test Control/Status (DMA2 only) */
156 /* Parallel Port registers */
157 __u16 p_hcr
; /* Hardware Configuration Register */
158 __u16 p_ocr
; /* Operation Configuration Register */
159 __u8 p_dr
; /* Parallel Data Register */
160 __u8 p_tcr
; /* Transfer Control Register */
161 __u8 p_or
; /* Output Register */
162 __u8 p_ir
; /* Input Register */
163 __u16 p_icr
; /* Interrupt Control Register */
166 /* P_CSR. Bits of type RW1 are cleared with writting '1'. */
167 #define P_DEV_ID_MASK 0xf0000000 /* R */
168 #define P_DEV_ID_ZEBRA 0x40000000
169 #define P_DEV_ID_L64854 0xa0000000 /* == NCR 89C100+89C105. Pity. */
170 #define P_NA_LOADED 0x08000000 /* R NA wirtten but was not used */
171 #define P_A_LOADED 0x04000000 /* R */
172 #define P_DMA_ON 0x02000000 /* R DMA is not disabled */
173 #define P_EN_NEXT 0x01000000 /* RW */
174 #define P_TCI_DIS 0x00800000 /* RW TCI forbidden from interrupts */
175 #define P_DIAG 0x00100000 /* RW Disables draining and resetting
176 of P-FIFO on loading of P_ADDR*/
177 #define P_BURST_SIZE 0x000c0000 /* RW SBus burst size */
178 #define P_BURST_8 0x00000000
179 #define P_BURST_4 0x00040000
180 #define P_BURST_1 0x00080000 /* "No burst" write */
181 #define P_TC 0x00004000 /* RW1 Term Count, can be cleared when
183 #define P_EN_CNT 0x00002000 /* RW */
184 #define P_EN_DMA 0x00000200 /* RW */
185 #define P_WRITE 0x00000100 /* R DMA dir, 1=to ram, 0=to port */
186 #define P_RESET 0x00000080 /* RW */
187 #define P_SLAVE_ERR 0x00000040 /* RW1 Access size error */
188 #define P_INVALIDATE 0x00000020 /* W Drop P-FIFO */
189 #define P_INT_EN 0x00000010 /* RW OK to P_INT_PEND||P_ERR_PEND */
190 #define P_DRAINING 0x0000000c /* R P-FIFO is draining to memory */
191 #define P_ERR_PEND 0x00000002 /* R */
192 #define P_INT_PEND 0x00000001 /* R */
194 /* P_HCR. Time is in increments of SBus clock. */
195 #define P_HCR_TEST 0x8000 /* Allows buried counters to be read */
196 #define P_HCR_DSW 0x7f00 /* Data strobe width (in ticks) */
197 #define P_HCR_DDS 0x007f /* Data setup before strobe (in ticks) */
200 #define P_OCR_MEM_CLR 0x8000
201 #define P_OCR_DATA_SRC 0x4000 /* ) */
202 #define P_OCR_DS_DSEL 0x2000 /* ) Bidirectional */
203 #define P_OCR_BUSY_DSEL 0x1000 /* ) selects */
204 #define P_OCR_ACK_DSEL 0x0800 /* ) */
205 #define P_OCR_EN_DIAG 0x0400
206 #define P_OCR_BUSY_OP 0x0200 /* Busy operation */
207 #define P_OCR_ACK_OP 0x0100 /* Ack operation */
208 #define P_OCR_SRST 0x0080 /* Reset state machines. Not selfcleaning. */
209 #define P_OCR_IDLE 0x0008 /* PP data transfer state machine is idle */
210 #define P_OCR_V_ILCK 0x0002 /* Versatec faded. Zebra only. */
211 #define P_OCR_EN_VER 0x0001 /* Enable Versatec (0 - enable). Zebra only. */
214 #define P_TCR_DIR 0x08
215 #define P_TCR_BUSY 0x04
216 #define P_TCR_ACK 0x02
217 #define P_TCR_DS 0x01 /* Strobe */
220 #define P_OR_V3 0x20 /* ) */
221 #define P_OR_V2 0x10 /* ) on Zebra only */
222 #define P_OR_V1 0x08 /* ) */
223 #define P_OR_INIT 0x04
224 #define P_OR_AFXN 0x02 /* Auto Feed */
225 #define P_OR_SLCT_IN 0x01
229 #define P_IR_SLCT 0x02
230 #define P_IR_ERR 0x01
233 #define P_DS_IRQ 0x8000 /* RW1 */
234 #define P_ACK_IRQ 0x4000 /* RW1 */
235 #define P_BUSY_IRQ 0x2000 /* RW1 */
236 #define P_PE_IRQ 0x1000 /* RW1 */
237 #define P_SLCT_IRQ 0x0800 /* RW1 */
238 #define P_ERR_IRQ 0x0400 /* RW1 */
239 #define P_DS_IRQ_EN 0x0200 /* RW Always on rising edge */
240 #define P_ACK_IRQ_EN 0x0100 /* RW Always on rising edge */
241 #define P_BUSY_IRP 0x0080 /* RW 1= rising edge */
242 #define P_BUSY_IRQ_EN 0x0040 /* RW */
243 #define P_PE_IRP 0x0020 /* RW 1= rising edge */
244 #define P_PE_IRQ_EN 0x0010 /* RW */
245 #define P_SLCT_IRP 0x0008 /* RW 1= rising edge */
246 #define P_SLCT_IRQ_EN 0x0004 /* RW */
247 #define P_ERR_IRP 0x0002 /* RW1 1= rising edge */
248 #define P_ERR_IRQ_EN 0x0001 /* RW */
250 volatile struct bpp_regs
*base_addrs
[BPP_NO
];
252 static inline void bpp_outb_p(__u8 data
, volatile struct bpp_regs
*base
){
256 #define bpp_inb_p(base) bpp_inb(base)
258 static inline __u8
bpp_inb(volatile struct bpp_regs
*base
){
263 static void set_pins(unsigned short pins
, unsigned minor
)
265 volatile struct bpp_regs
*base
= base_addrs
[minor
];
266 unsigned char bits_tcr
= 0, bits_or
= 0;
268 if (instances
[minor
].direction
& 0x20) bits_tcr
|= P_TCR_DIR
;
269 if ( pins
& BPP_PP_nStrobe
) bits_tcr
|= P_TCR_DS
;
271 if ( pins
& BPP_PP_nAutoFd
) bits_or
|= P_OR_AFXN
;
272 if (! (pins
& BPP_PP_nInit
)) bits_or
|= P_OR_INIT
;
273 if (! (pins
& BPP_PP_nSelectIn
)) bits_or
|= P_OR_SLCT_IN
;
275 base
->p_or
= bits_or
;
276 base
->p_tcr
= bits_tcr
;
280 * i386 people read output pins from a software image.
281 * We may get them back from hardware.
282 * Again, inversion of pins must he buried here.
284 static unsigned short get_pins(unsigned minor
)
286 volatile struct bpp_regs
*base
= base_addrs
[minor
];
287 unsigned short bits
= 0;
288 unsigned value_tcr
= base
->p_tcr
;
289 unsigned value_ir
= base
->p_ir
;
290 unsigned value_or
= base
->p_or
;
292 if (value_tcr
& P_TCR_DS
) bits
|= BPP_PP_nStrobe
;
293 if (value_or
& P_OR_AFXN
) bits
|= BPP_PP_nAutoFd
;
294 if (! (value_or
& P_OR_INIT
)) bits
|= BPP_PP_nInit
;
295 if (! (value_or
& P_OR_SLCT_IN
)) bits
|= BPP_PP_nSelectIn
;
297 if (value_ir
& P_IR_ERR
) bits
|= BPP_GP_nFault
;
298 if (! (value_ir
& P_IR_SLCT
)) bits
|= BPP_GP_Select
;
299 if (! (value_ir
& P_IR_PE
)) bits
|= BPP_GP_PError
;
300 if (! (value_tcr
& P_TCR_ACK
)) bits
|= BPP_GP_nAck
;
301 if (value_tcr
& P_TCR_BUSY
) bits
|= BPP_GP_Busy
;
308 static inline void bpp_snap(const char *msg
, unsigned minor
)
310 volatile struct bpp_regs
*r
= base_addrs
[minor
];
311 printk("bpp.%s: c=%02x o=%02x i=%02x\n", msg
, r
->p_tcr
, r
->p_or
, r
->p_ir
);
315 #endif /* __sparc__ */
318 * This is TRUE if the module_init successfully loaded the module.
321 static int loaded_flag
= 0;
324 static void bpp_wake_up(unsigned long val
)
325 { wake_up(&instances
[val
].wait_queue
); }
327 static void snooze(unsigned long snooze_time
, unsigned minor
)
329 instances
[minor
].timer_list
.expires
= jiffies
+ snooze_time
+ 1;
330 instances
[minor
].timer_list
.data
= minor
;
331 add_timer(&instances
[minor
].timer_list
);
332 sleep_on (&instances
[minor
].wait_queue
);
335 static int wait_for(unsigned short set
, unsigned short clr
,
336 unsigned long delay
, unsigned minor
)
338 unsigned short pins
= get_pins(minor
);
340 unsigned long extime
= 0;
343 * Try a real fast scan for the first jiffy, in case the device
344 * responds real good. The first while loop guesses an expire
345 * time accounting for possible wraparound of jiffies.
347 while (extime
<= jiffies
) extime
= jiffies
+ 1;
348 while ( (jiffies
< extime
)
349 && (((pins
& set
) != set
) || ((pins
& clr
) != 0)) ) {
350 pins
= get_pins(minor
);
356 * If my delay expired or the pins are still not where I want
357 * them, then resort to using the timer and greatly reduce my
358 * sample rate. If the peripheral is going to be slow, this will
359 * give the CPU up to some more worthy process.
361 while ( delay
&& (((pins
& set
) != set
) || ((pins
& clr
) != 0)) ) {
364 pins
= get_pins(minor
);
368 if (delay
== 0) return -1;
373 * Return ZERO(0) If the negotiation succeeds, an errno otherwise. An
374 * errno means something broke, and I do not yet know how to fix it.
376 static int negotiate(unsigned char mode
, unsigned minor
)
379 unsigned short pins
= get_pins(minor
);
380 if (pins
& BPP_PP_nSelectIn
) return -EIO
;
383 /* Event 0: Write the mode to the data lines */
384 bpp_outb_p(mode
, base_addrs
[minor
]);
386 snooze(TIME_PSetup
, minor
);
388 /* Event 1: Strobe the mode code into the peripheral */
389 set_pins(BPP_PP_nSelectIn
|BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
391 /* Wait for Event 2: Peripheral responds as a 1284 device. */
392 rc
= wait_for(BPP_GP_PError
|BPP_GP_Select
|BPP_GP_nFault
,
397 if (rc
== -1) return -ETIMEDOUT
;
399 /* Event 3: latch extensibility request */
400 set_pins(BPP_PP_nSelectIn
|BPP_PP_nInit
, minor
);
402 /* ... quick nap while peripheral ponders the byte i'm sending...*/
405 /* Event 4: restore strobe, to ACK peripheral's response. */
406 set_pins(BPP_PP_nSelectIn
|BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
408 /* Wait for Event 6: Peripheral latches response bits */
409 rc
= wait_for(BPP_GP_nAck
, 0, TIME_PSetup
+TIME_PResponse
, minor
);
410 if (rc
== -1) return -EIO
;
412 /* A 1284 device cannot refuse nibble mode */
413 if (mode
== DEFAULT_NIBBLE
) return 0;
415 if (pins
& BPP_GP_Select
) return 0;
417 return -EPROTONOSUPPORT
;
420 static int terminate(unsigned minor
)
424 /* Event 22: Request termination of 1284 mode */
425 set_pins(BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
427 /* Wait for Events 23 and 24: ACK termination request. */
428 rc
= wait_for(BPP_GP_Busy
|BPP_GP_nFault
,
430 TIME_PSetup
+TIME_PResponse
,
433 instances
[minor
].direction
= 0;
434 instances
[minor
].mode
= COMPATIBILITY
;
440 /* Event 25: Handshake by lowering nAutoFd */
441 set_pins(BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
443 /* Event 26: Peripheral wiggles lines... */
445 /* Event 27: Peripheral sets nAck HIGH to ack handshake */
446 rc
= wait_for(BPP_GP_nAck
, 0, TIME_PResponse
, minor
);
448 set_pins(BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
452 /* Event 28: Finish phase by raising nAutoFd */
453 set_pins(BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, minor
);
460 * Allow only one process to open the device at a time.
462 static int bpp_open(struct inode
*inode
, struct file
*f
)
464 unsigned minor
= MINOR(inode
->i_rdev
);
465 if (minor
>= BPP_NO
) return -ENODEV
;
466 if (! instances
[minor
].present
) return -ENODEV
;
467 if (instances
[minor
].opened
) return -EBUSY
;
469 instances
[minor
].opened
= 1;
475 * When the process closes the device, this method is called to clean
476 * up and reset the hardware. Always leave the device in compatibility
477 * mode as this is a reasonable place to clean up from messes made by
478 * ioctls, or other mayhem.
480 static void bpp_release(struct inode
*inode
, struct file
*f
)
482 unsigned minor
= MINOR(inode
->i_rdev
);
483 instances
[minor
].opened
= 0;
485 if (instances
[minor
].mode
!= COMPATIBILITY
)
489 static long read_nibble(unsigned minor
, char *c
, unsigned long cnt
)
491 unsigned long remaining
= cnt
;
494 while (remaining
> 0) {
495 unsigned char byte
= 0;
498 /* Event 7: request nibble */
499 set_pins(BPP_PP_nSelectIn
|BPP_PP_nStrobe
, minor
);
501 /* Wait for event 9: Peripher strobes first nibble */
502 pins
= wait_for(0, BPP_GP_nAck
, TIME_IDLE_LIMIT
, minor
);
503 if (pins
== -1) return -ETIMEDOUT
;
505 /* Event 10: I handshake nibble */
506 set_pins(BPP_PP_nSelectIn
|BPP_PP_nStrobe
|BPP_PP_nAutoFd
, minor
);
507 if (pins
& BPP_GP_nFault
) byte
|= 0x01;
508 if (pins
& BPP_GP_Select
) byte
|= 0x02;
509 if (pins
& BPP_GP_PError
) byte
|= 0x04;
510 if (pins
& BPP_GP_Busy
) byte
|= 0x08;
512 /* Wait for event 11: Peripheral handshakes nibble */
513 rc
= wait_for(BPP_GP_nAck
, 0, TIME_PResponse
, minor
);
515 /* Event 7: request nibble */
516 set_pins(BPP_PP_nSelectIn
|BPP_PP_nStrobe
, minor
);
518 /* Wait for event 9: Peripher strobes first nibble */
519 pins
= wait_for(0, BPP_GP_nAck
, TIME_PResponse
, minor
);
520 if (rc
== -1) return -ETIMEDOUT
;
522 /* Event 10: I handshake nibble */
523 set_pins(BPP_PP_nSelectIn
|BPP_PP_nStrobe
|BPP_PP_nAutoFd
, minor
);
524 if (pins
& BPP_GP_nFault
) byte
|= 0x10;
525 if (pins
& BPP_GP_Select
) byte
|= 0x20;
526 if (pins
& BPP_GP_PError
) byte
|= 0x40;
527 if (pins
& BPP_GP_Busy
) byte
|= 0x80;
529 put_user_ret(byte
, c
, -EFAULT
);
533 /* Wait for event 11: Peripheral handshakes nibble */
534 rc
= wait_for(BPP_GP_nAck
, 0, TIME_PResponse
, minor
);
535 if (rc
== -1) return -EIO
;
538 return cnt
- remaining
;
541 static long read_ecp(unsigned minor
, char *c
, unsigned long cnt
)
543 unsigned long remaining
;
546 /* Turn ECP mode from forward to reverse if needed. */
547 if (! instances
[minor
].direction
) {
548 unsigned short pins
= get_pins(minor
);
550 /* Event 38: Turn the bus around */
551 instances
[minor
].direction
= 0x20;
552 pins
&= ~BPP_PP_nAutoFd
;
553 set_pins(pins
, minor
);
555 /* Event 39: Set pins for reverse mode. */
556 snooze(TIME_PSetup
, minor
);
557 set_pins(BPP_PP_nStrobe
|BPP_PP_nSelectIn
, minor
);
559 /* Wait for event 40: Peripheral ready to be strobed */
560 rc
= wait_for(0, BPP_GP_PError
, TIME_PResponse
, minor
);
561 if (rc
== -1) return -ETIMEDOUT
;
566 while (remaining
> 0) {
568 /* If there is a run length for a repeated byte, repeat */
569 /* that byte a few times. */
570 if (instances
[minor
].run_length
&& !instances
[minor
].run_flag
) {
574 unsigned repeat
= remaining
< instances
[minor
].run_length
576 : instances
[minor
].run_length
;
578 for (idx
= 0 ; idx
< repeat
; idx
+= 1)
579 buffer
[idx
] = instances
[minor
].repeat_byte
;
581 copy_to_user_ret(c
, buffer
, repeat
, -EFAULT
);
584 instances
[minor
].run_length
-= repeat
;
587 if (remaining
== 0) break;
590 /* Wait for Event 43: Data active on the bus. */
591 rc
= wait_for(0, BPP_GP_nAck
, TIME_IDLE_LIMIT
, minor
);
594 if (rc
& BPP_GP_Busy
) {
595 /* OK, this is data. read it in. */
596 unsigned char byte
= bpp_inb(base_addrs
[minor
]);
597 put_user_ret(byte
, c
, -EFAULT
);
601 if (instances
[minor
].run_flag
) {
602 instances
[minor
].repeat_byte
= byte
;
603 instances
[minor
].run_flag
= 0;
607 unsigned char byte
= bpp_inb(base_addrs
[minor
]);
610 "Ignoring ECP channel %u from device.\n",
613 instances
[minor
].run_length
= byte
;
614 instances
[minor
].run_flag
= 1;
618 /* Event 44: I got it. */
619 set_pins(BPP_PP_nStrobe
|BPP_PP_nAutoFd
|BPP_PP_nSelectIn
, minor
);
621 /* Wait for event 45: peripheral handshake */
622 rc
= wait_for(BPP_GP_nAck
, 0, TIME_PResponse
, minor
);
623 if (rc
== -1) return -ETIMEDOUT
;
625 /* Event 46: Finish handshake */
626 set_pins(BPP_PP_nStrobe
|BPP_PP_nSelectIn
, minor
);
631 return cnt
- remaining
;
634 static long bpp_read(struct inode
*inode
, struct file
*f
,
635 char *c
, unsigned long cnt
)
638 const unsigned minor
= MINOR(inode
->i_rdev
);
639 if (minor
>= BPP_NO
) return -ENODEV
;
640 if (!instances
[minor
].present
) return -ENODEV
;
642 switch (instances
[minor
].mode
) {
645 if (instances
[minor
].mode
!= COMPATIBILITY
)
648 if (instances
[minor
].enhanced
) {
649 /* For now, do all reads with ECP-RLE mode */
652 rc
= negotiate(DEFAULT_ECP
, minor
);
655 instances
[minor
].mode
= ECP_RLE
;
657 /* Event 30: set nAutoFd low to setup for ECP mode */
658 pins
= get_pins(minor
);
659 pins
&= ~BPP_PP_nAutoFd
;
660 set_pins(pins
, minor
);
662 /* Wait for Event 31: peripheral ready */
663 rc
= wait_for(BPP_GP_PError
, 0, TIME_PResponse
, minor
);
664 if (rc
== -1) return -ETIMEDOUT
;
666 rc
= read_ecp(minor
, c
, cnt
);
669 rc
= negotiate(DEFAULT_NIBBLE
, minor
);
672 instances
[minor
].mode
= NIBBLE
;
674 rc
= read_nibble(minor
, c
, cnt
);
679 rc
= read_nibble(minor
, c
, cnt
);
684 rc
= read_ecp(minor
, c
, cnt
);
694 * Compatibility mode handshaking is a matter of writing data,
695 * strobing it, and waiting for the printer to stop being busy.
697 static long write_compat(unsigned minor
, const char *c
, unsigned long cnt
)
700 unsigned short pins
= get_pins(minor
);
702 unsigned long remaining
= cnt
;
704 while (remaining
> 0) {
707 get_user_ret(byte
, c
, -EFAULT
);
709 rc
= wait_for(BPP_GP_nAck
, BPP_GP_Busy
, TIME_IDLE_LIMIT
, minor
);
710 if (rc
== -1) return -ETIMEDOUT
;
712 bpp_outb_p(byte
, base_addrs
[minor
]);
714 /* snooze(1, minor); */
716 pins
&= ~BPP_PP_nStrobe
;
717 set_pins(pins
, minor
);
719 rc
= wait_for(BPP_GP_Busy
, 0, TIME_PResponse
, minor
);
721 pins
|= BPP_PP_nStrobe
;
722 set_pins(pins
, minor
);
725 return cnt
- remaining
;
729 * Write data using ECP mode. Watch out that the port may be set up
730 * for reading. If so, turn the port around.
732 static long write_ecp(unsigned minor
, const char *c
, unsigned long cnt
)
734 unsigned short pins
= get_pins(minor
);
735 unsigned long remaining
= cnt
;
737 if (instances
[minor
].direction
) {
740 /* Event 47 Request bus be turned around */
741 pins
|= BPP_PP_nInit
;
742 set_pins(pins
, minor
);
744 /* Wait for Event 49: Peripheral relinquished bus */
745 rc
= wait_for(BPP_GP_PError
, 0, TIME_PResponse
, minor
);
747 pins
|= BPP_PP_nAutoFd
;
748 instances
[minor
].direction
= 0;
749 set_pins(pins
, minor
);
752 while (remaining
> 0) {
756 get_user_ret(byte
, c
, -EFAULT
);
758 rc
= wait_for(0, BPP_GP_Busy
, TIME_PResponse
, minor
);
759 if (rc
== -1) return -ETIMEDOUT
;
763 bpp_outb_p(byte
, base_addrs
[minor
]);
765 pins
&= ~BPP_PP_nStrobe
;
766 set_pins(pins
, minor
);
768 pins
|= BPP_PP_nStrobe
;
769 rc
= wait_for(BPP_GP_Busy
, 0, TIME_PResponse
, minor
);
770 if (rc
== -1) return -EIO
;
772 set_pins(pins
, minor
);
775 return cnt
- remaining
;
779 * Write to the peripheral. Be sensitive of the current mode. If I'm
780 * in a mode that can be turned around (ECP) then just do
781 * that. Otherwise, terminate and do my writing in compat mode. This
782 * is the safest course as any device can handle it.
784 static long bpp_write(struct inode
*inode
, struct file
*f
,
785 const char *c
, unsigned long cnt
)
788 unsigned minor
= MINOR(inode
->i_rdev
);
789 if (minor
>= BPP_NO
) return -ENODEV
;
790 if (!instances
[minor
].present
) return -ENODEV
;
792 switch (instances
[minor
].mode
) {
796 errno
= write_ecp(minor
, c
, cnt
);
799 errno
= write_compat(minor
, c
, cnt
);
803 errno
= write_compat(minor
, c
, cnt
);
809 static int bpp_ioctl(struct inode
*inode
, struct file
*f
, unsigned int cmd
,
814 unsigned minor
= MINOR(inode
->i_rdev
);
815 if (minor
>= BPP_NO
) return -ENODEV
;
816 if (!instances
[minor
].present
) return -ENODEV
;
822 set_pins(arg
, minor
);
826 errno
= get_pins(minor
);
830 bpp_outb_p(arg
, base_addrs
[minor
]);
834 errno
= bpp_inb_p(base_addrs
[minor
]);
839 if (instances
[minor
].enhanced
) {
840 unsigned short bits
= get_pins(minor
);
841 instances
[minor
].direction
= 0x20;
842 set_pins(bits
, minor
);
847 unsigned short bits
= get_pins(minor
);
848 instances
[minor
].direction
= 0x00;
849 set_pins(bits
, minor
);
860 static struct file_operations bpp_fops
= {
861 NULL
, /* bpp_lseek */
864 NULL
, /* bpp_readdir */
865 NULL
, /* bpp_select */
873 #if defined(__i386__)
875 #define collectLptPorts() {}
877 static void probeLptPort(unsigned idx
)
879 unsigned int testvalue
;
880 const unsigned short lpAddr
= base_addrs
[idx
];
882 instances
[idx
].present
= 0;
883 instances
[idx
].enhanced
= 0;
884 instances
[idx
].direction
= 0;
885 instances
[idx
].mode
= COMPATIBILITY
;
886 instances
[idx
].wait_queue
= 0;
887 instances
[idx
].run_length
= 0;
888 instances
[idx
].run_flag
= 0;
889 init_timer(&instances
[idx
].timer_list
);
890 instances
[idx
].timer_list
.function
= bpp_wake_up
;
891 if (check_region(lpAddr
,3)) return;
894 * First, make sure the instance exists. Do this by writing to
895 * the data latch and reading the value back. If the port *is*
896 * present, test to see if it supports extended-mode
897 * operation. This will be required for IEEE1284 reverse
901 outb_p(BPP_PROBE_CODE
, lpAddr
);
902 for (testvalue
=0; testvalue
<BPP_DELAY
; testvalue
++)
904 testvalue
= inb_p(lpAddr
);
905 if (testvalue
== BPP_PROBE_CODE
) {
907 instances
[idx
].present
= 1;
909 request_region(lpAddr
,3, dev_name
);
910 save
= inb_p(lpAddr
+2);
911 for (testvalue
=0; testvalue
<BPP_DELAY
; testvalue
++)
913 outb_p(save
|0x20, lpAddr
+2);
914 for (testvalue
=0; testvalue
<BPP_DELAY
; testvalue
++)
916 outb_p(~BPP_PROBE_CODE
, lpAddr
);
917 for (testvalue
=0; testvalue
<BPP_DELAY
; testvalue
++)
919 testvalue
= inb_p(lpAddr
);
920 if ((testvalue
&0xff) == (0xff&~BPP_PROBE_CODE
))
921 instances
[idx
].enhanced
= 0;
923 instances
[idx
].enhanced
= 1;
924 outb_p(save
, lpAddr
+2);
928 * Leave the port in compat idle mode.
930 set_pins(BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, idx
);
932 printk("bpp%d: Port at 0x%03x: Enhanced mode %s\n", idx
, base_addrs
[idx
],
933 instances
[idx
].enhanced
? "SUPPORTED" : "UNAVAILABLE");
936 static inline void freeLptPort(int idx
)
938 release_region(base_addrs
[idx
], 3);
943 #if defined(__sparc__)
945 static volatile struct bpp_regs
*map_bpp(struct linux_sbus_device
*dev
, int idx
)
947 volatile struct bpp_regs
*regs
;
949 /* Apply ranges to here, do not pollute Sbus devices list. */
950 struct linux_prom_registers areg
;
953 * PROM reports different numbers on Zebra and on DMA2.
954 * We need to figure out when to apply parent ranges.
955 * printk will show this on different machines.
958 areg
= dev
->reg_addrs
[0];
959 printk("bpp%d.map_bpp: 0x%x.%p[0x%x] i=%d\n", idx
,
960 areg
.which_io
, areg
.phys_addr
, areg
.reg_size
,
962 /* IPC Zebra 1.fa200000[1c] i=2 */
963 /** prom_apply_sbus_ranges (&areg, 1); **/
965 regs
= sparc_alloc_io (areg
.phys_addr
, 0,
966 sizeof(struct bpp_regs
), "bpp",
972 static int collectLptPorts(void)
974 struct linux_sbus
*bus
;
975 struct linux_sbus_device
*dev
;
979 for_all_sbusdev(dev
, bus
) {
980 if (strcmp(dev
->prom_name
, "SUNW,bpp") == 0) {
981 if (count
>= BPP_NO
) {
983 "bpp: More than %d bpp ports,"
984 " rest is ignored\n", BPP_NO
);
987 base_addrs
[count
] = map_bpp(dev
, count
);
994 static void probeLptPort(unsigned idx
)
996 volatile struct bpp_regs
*rp
= base_addrs
[idx
];
1000 instances
[idx
].present
= 0;
1001 instances
[idx
].enhanced
= 0;
1002 instances
[idx
].direction
= 0;
1003 instances
[idx
].mode
= COMPATIBILITY
;
1004 instances
[idx
].wait_queue
= 0;
1005 instances
[idx
].run_length
= 0;
1006 instances
[idx
].run_flag
= 0;
1007 init_timer(&instances
[idx
].timer_list
);
1008 instances
[idx
].timer_list
.function
= bpp_wake_up
;
1010 if (rp
== 0) return;
1012 instances
[idx
].present
= 1;
1013 instances
[idx
].enhanced
= 1; /* Sure */
1015 if (((csr
= rp
->p_csr
) & P_DRAINING
) != 0 && (csr
& P_ERR_PEND
) == 0) {
1018 if ((csr
& P_DRAINING
) != 0 && (csr
& P_ERR_PEND
) == 0) {
1019 printk("bpp%d: DRAINING still active (0x%08x)\n", idx
, csr
);
1022 printk("bpp%d: reset with 0x%08x ..", idx
, csr
);
1023 rp
->p_csr
= (csr
| P_RESET
) & ~P_INT_EN
;
1025 rp
->p_csr
&= ~P_RESET
;
1026 printk(" done with csr=0x%08x ocr=0x%04x\n", rp
->p_csr
, rp
->p_ocr
);
1028 switch (rp
->p_csr
& P_DEV_ID_MASK
) {
1029 case P_DEV_ID_ZEBRA
:
1032 case P_DEV_ID_L64854
:
1038 printk("bpp%d: %s at 0x%p\n", idx
, brand
, rp
);
1041 * Leave the port in compat idle mode.
1043 set_pins(BPP_PP_nAutoFd
|BPP_PP_nStrobe
|BPP_PP_nInit
, idx
);
1048 static inline void freeLptPort(int idx
)
1050 sparc_free_io ((void *)base_addrs
[idx
], sizeof(struct bpp_regs
));
1056 int init_module(void)
1058 __initfunc(int bpp_init(void))
1064 rc
= collectLptPorts();
1068 rc
= register_chrdev(BPP_MAJOR
, dev_name
, &bpp_fops
);
1072 for (idx
= 0; idx
< BPP_NO
; idx
+= 1) {
1073 instances
[idx
].opened
= 0;
1081 void cleanup_module(void)
1085 unregister_chrdev(BPP_MAJOR
, dev_name
);
1087 for (idx
= 0 ; idx
< BPP_NO
; idx
+= 1) {
1088 if (instances
[idx
].present
)