1 /* $Id: parport_ax.c,v 1.17 1999/01/20 06:18:54 davem Exp $
2 * Parallel-port routines for Sun Ultra/AX architecture
4 * Author: Eddie C. Dost <ecd@skynet.be>
7 * Phil Blundell <Philip.Blundell@pobox.com>
8 * Tim Waugh <tim@cyberelk.demon.co.uk>
9 * Jose Renau <renau@acm.org>
10 * David Campbell <campbell@tirian.che.curtin.edu.au>
11 * Grant Guenther <grant@torque.net>
14 #include <linux/string.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/errno.h>
18 #include <linux/ioport.h>
19 #include <linux/kernel.h>
20 #include <linux/malloc.h>
21 #include <linux/init.h>
23 #include <linux/parport.h>
25 #include <asm/ptrace.h>
26 #include <linux/interrupt.h>
30 #include <asm/ns87303.h>
35 * Define this if you have Devices which don't support short
36 * host read/write cycles.
38 #undef HAVE_SLOW_DEVICES
52 #define ECONTROL 0x402
54 static void parport_ax_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
56 parport_generic_irq(irq
, (struct parport
*) dev_id
, regs
);
60 parport_ax_write_epp(struct parport
*p
, unsigned char d
)
62 outb(d
, p
->base
+ EPPDATA
);
66 parport_ax_read_epp(struct parport
*p
)
68 return inb(p
->base
+ EPPDATA
);
72 parport_ax_write_epp_addr(struct parport
*p
, unsigned char d
)
74 outb(d
, p
->base
+ EPPADDR
);
78 parport_ax_read_epp_addr(struct parport
*p
)
80 return inb(p
->base
+ EPPADDR
);
83 int parport_ax_epp_clear_timeout(struct parport
*pb
);
86 parport_ax_check_epp_timeout(struct parport
*p
)
88 if (!(inb(p
->base
+STATUS
) & 1))
90 parport_ax_epp_clear_timeout(p
);
95 parport_ax_read_configb(struct parport
*p
)
97 return inb(p
->base
+ CONFIGB
);
101 parport_ax_write_data(struct parport
*p
, unsigned char d
)
103 outb(d
, p
->base
+ DATA
);
107 parport_ax_read_data(struct parport
*p
)
109 return inb(p
->base
+ DATA
);
113 parport_ax_write_control(struct parport
*p
, unsigned char d
)
115 outb(d
, p
->base
+ CONTROL
);
119 parport_ax_read_control(struct parport
*p
)
121 return inb(p
->base
+ CONTROL
);
125 parport_ax_frob_control(struct parport
*p
, unsigned char mask
, unsigned char val
)
127 unsigned char old
= inb(p
->base
+ CONTROL
);
128 outb(((old
& ~mask
) ^ val
), p
->base
+ CONTROL
);
133 parport_ax_write_status(struct parport
*p
, unsigned char d
)
135 outb(d
, p
->base
+ STATUS
);
139 parport_ax_read_status(struct parport
*p
)
141 return inb(p
->base
+ STATUS
);
145 parport_ax_write_econtrol(struct parport
*p
, unsigned char d
)
147 outb(d
, p
->base
+ ECONTROL
);
151 parport_ax_read_econtrol(struct parport
*p
)
153 return inb(p
->base
+ ECONTROL
);
157 parport_ax_frob_econtrol(struct parport
*p
, unsigned char mask
, unsigned char val
)
159 unsigned char old
= inb(p
->base
+ ECONTROL
);
160 outb(((old
& ~mask
) ^ val
), p
->base
+ ECONTROL
);
165 parport_ax_change_mode(struct parport
*p
, int m
)
167 parport_ax_frob_econtrol(p
, 0xe0, m
<< 5);
171 parport_ax_write_fifo(struct parport
*p
, unsigned char v
)
173 outb(v
, p
->base
+ DFIFO
);
177 parport_ax_read_fifo(struct parport
*p
)
179 return inb(p
->base
+ DFIFO
);
183 parport_ax_disable_irq(struct parport
*p
)
185 struct linux_ebus_dma
*dma
= p
->private_data
;
188 dcsr
= readl((unsigned long)&dma
->dcsr
);
189 dcsr
&= ~(EBUS_DCSR_INT_EN
);
190 writel(dcsr
, (unsigned long)&dma
->dcsr
);
194 parport_ax_enable_irq(struct parport
*p
)
196 struct linux_ebus_dma
*dma
= p
->private_data
;
199 dcsr
= readl((unsigned long)&dma
->dcsr
);
200 dcsr
|= EBUS_DCSR_INT_EN
;
201 writel(dcsr
, (unsigned long)&dma
->dcsr
);
205 parport_ax_claim_resources(struct parport
*p
)
210 parport_ax_init_state(struct parport_state
*s
)
217 parport_ax_save_state(struct parport
*p
, struct parport_state
*s
)
219 s
->u
.pc
.ctr
= parport_ax_read_control(p
);
220 s
->u
.pc
.ecr
= parport_ax_read_econtrol(p
);
224 parport_ax_restore_state(struct parport
*p
, struct parport_state
*s
)
226 parport_ax_write_control(p
, s
->u
.pc
.ctr
);
227 parport_ax_write_econtrol(p
, s
->u
.pc
.ecr
);
231 parport_ax_epp_read_block(struct parport
*p
, void *buf
, size_t length
)
233 return 0; /* FIXME */
237 parport_ax_epp_write_block(struct parport
*p
, void *buf
, size_t length
)
239 return 0; /* FIXME */
243 parport_ax_ecp_read_block(struct parport
*p
, void *buf
, size_t length
,
244 void (*fn
)(struct parport
*, void *, size_t),
247 return 0; /* FIXME */
251 parport_ax_ecp_write_block(struct parport
*p
, void *buf
, size_t length
,
252 void (*fn
)(struct parport
*, void *, size_t),
255 return 0; /* FIXME */
259 parport_ax_inc_use_count(void)
267 parport_ax_dec_use_count(void)
274 static void parport_ax_fill_inode(struct inode
*inode
, int fill
)
284 static struct parport_operations parport_ax_ops
=
286 parport_ax_write_data
,
287 parport_ax_read_data
,
289 parport_ax_write_control
,
290 parport_ax_read_control
,
291 parport_ax_frob_control
,
293 parport_ax_write_econtrol
,
294 parport_ax_read_econtrol
,
295 parport_ax_frob_econtrol
,
297 parport_ax_write_status
,
298 parport_ax_read_status
,
300 parport_ax_write_fifo
,
301 parport_ax_read_fifo
,
303 parport_ax_change_mode
,
305 parport_ax_write_epp
,
307 parport_ax_write_epp_addr
,
308 parport_ax_read_epp_addr
,
309 parport_ax_check_epp_timeout
,
311 parport_ax_epp_write_block
,
312 parport_ax_epp_read_block
,
314 parport_ax_ecp_write_block
,
315 parport_ax_ecp_read_block
,
317 parport_ax_init_state
,
318 parport_ax_save_state
,
319 parport_ax_restore_state
,
321 parport_ax_enable_irq
,
322 parport_ax_disable_irq
,
323 parport_ax_interrupt
,
325 parport_ax_inc_use_count
,
326 parport_ax_dec_use_count
,
327 parport_ax_fill_inode
331 /******************************************************
332 * MODE detection section:
336 * Clear TIMEOUT BIT in EPP MODE
338 int parport_ax_epp_clear_timeout(struct parport
*pb
)
342 if (!(parport_ax_read_status(pb
) & 0x01))
345 /* To clear timeout some chips require double read */
346 parport_ax_read_status(pb
);
347 r
= parport_ax_read_status(pb
);
348 parport_ax_write_status(pb
, r
| 0x01); /* Some reset by writing 1 */
349 parport_ax_write_status(pb
, r
& 0xfe); /* Others by writing 0 */
350 r
= parport_ax_read_status(pb
);
357 * Old style XT ports alias io ports every 0x400, hence accessing ECONTROL
358 * on these cards actually accesses the CTR.
360 * Modern cards don't do this but reading from ECONTROL will return 0xff
361 * regardless of what is written here if the card does NOT support
364 * We will write 0x2c to ECONTROL and 0xcc to CTR since both of these
365 * values are "safe" on the CTR since bits 6-7 of CTR are unused.
367 static int parport_ECR_present(struct parport
*pb
)
370 unsigned char octr
= pb
->ops
->read_control(pb
),
371 oecr
= pb
->ops
->read_econtrol(pb
);
373 r
= pb
->ops
->read_control(pb
);
374 if ((pb
->ops
->read_econtrol(pb
) & 0x3) == (r
& 0x3)) {
375 pb
->ops
->write_control(pb
, r
^ 0x2 ); /* Toggle bit 1 */
377 r
= pb
->ops
->read_control(pb
);
378 if ((pb
->ops
->read_econtrol(pb
) & 0x2) == (r
& 0x2)) {
379 pb
->ops
->write_control(pb
, octr
);
380 return 0; /* Sure that no ECONTROL register exists */
384 if ((pb
->ops
->read_econtrol(pb
) & 0x3 ) != 0x1)
387 pb
->ops
->write_econtrol(pb
, 0x34);
388 if (pb
->ops
->read_econtrol(pb
) != 0x35)
391 pb
->ops
->write_econtrol(pb
, oecr
);
392 pb
->ops
->write_control(pb
, octr
);
394 return PARPORT_MODE_PCECR
;
397 static int parport_ECP_supported(struct parport
*pb
)
400 unsigned char oecr
= pb
->ops
->read_econtrol(pb
);
402 /* If there is no ECONTROL, we have no hope of supporting ECP. */
403 if (!(pb
->modes
& PARPORT_MODE_PCECR
))
407 * Using LGS chipset it uses ECONTROL register, but
408 * it doesn't support ECP or FIFO MODE
411 pb
->ops
->write_econtrol(pb
, 0xc0); /* TEST FIFO */
412 for (i
=0; i
< 1024 && (pb
->ops
->read_econtrol(pb
) & 0x01); i
++)
413 pb
->ops
->write_fifo(pb
, 0xaa);
415 pb
->ops
->write_econtrol(pb
, oecr
);
416 return (i
== 1024) ? 0 : PARPORT_MODE_PCECP
;
419 /* Detect PS/2 support.
421 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
422 * allows us to read data from the data lines. In theory we would get back
423 * 0xff but any peripheral attached to the port may drag some or all of the
424 * lines down to zero. So if we get back anything that isn't the contents
425 * of the data register we deem PS/2 support to be present.
427 * Some SPP ports have "half PS/2" ability - you can't turn off the line
428 * drivers, but an external peripheral with sufficiently beefy drivers of
429 * its own can overpower them and assert its own levels onto the bus, from
430 * where they can then be read back as normal. Ports with this property
431 * and the right type of device attached are likely to fail the SPP test,
432 * (as they will appear to have stuck bits) and so the fact that they might
433 * be misdetected here is rather academic.
436 static int parport_PS2_supported(struct parport
*pb
)
439 unsigned char octr
= pb
->ops
->read_control(pb
);
441 pb
->ops
->write_control(pb
, octr
| 0x20); /* try to tri-state buffer */
443 pb
->ops
->write_data(pb
, 0x55);
444 if (pb
->ops
->read_data(pb
) != 0x55) ok
++;
446 pb
->ops
->write_data(pb
, 0xaa);
447 if (pb
->ops
->read_data(pb
) != 0xaa) ok
++;
449 pb
->ops
->write_control(pb
, octr
); /* cancel input mode */
451 return ok
? PARPORT_MODE_PCPS2
: 0;
454 static int parport_ECPPS2_supported(struct parport
*pb
)
457 unsigned char oecr
= pb
->ops
->read_econtrol(pb
);
459 if (!(pb
->modes
& PARPORT_MODE_PCECR
))
462 pb
->ops
->write_econtrol(pb
, 0x20);
464 mode
= parport_PS2_supported(pb
);
466 pb
->ops
->write_econtrol(pb
, oecr
);
467 return mode
? PARPORT_MODE_PCECPPS2
: 0;
470 #define printmode(x) \
472 if (p->modes & PARPORT_MODE_PC##x) { \
473 printk("%s%s", f ? "," : "", #x); \
479 init_one_port(struct linux_ebus_device
*dev
)
481 struct parport tmpport
, *p
;
483 unsigned long config
;
487 /* Pointer to NS87303 Configuration Registers */
488 config
= dev
->base_address
[1];
490 /* Setup temporary access to Device operations */
491 tmpport
.base
= dev
->base_address
[0];
492 tmpport
.ops
= &parport_ax_ops
;
494 /* Enable ECP mode, set bit 2 of the CTR first */
495 tmpport
.ops
->write_control(&tmpport
, 0x04);
496 tmp
= ns87303_readb(config
, PCR
);
497 tmp
|= (PCR_EPP_IEEE
| PCR_ECP_ENABLE
| PCR_ECP_CLK_ENA
);
498 ns87303_writeb(config
, PCR
, tmp
);
500 /* LPT CTR bit 5 controls direction of parallel port */
501 tmp
= ns87303_readb(config
, PTR
);
502 tmp
|= PTR_LPT_REG_DIR
;
503 ns87303_writeb(config
, PTR
, tmp
);
505 /* Configure IRQ to Push Pull, Level Low */
506 tmp
= ns87303_readb(config
, PCR
);
507 tmp
&= ~(PCR_IRQ_ODRAIN
);
508 tmp
|= PCR_IRQ_POLAR
;
509 ns87303_writeb(config
, PCR
, tmp
);
511 #ifndef HAVE_SLOW_DEVICES
512 /* Enable Zero Wait State for ECP */
513 tmp
= ns87303_readb(config
, FCR
);
515 ns87303_writeb(config
, FCR
, tmp
);
519 * Now continue initializing the port
521 base
= dev
->base_address
[0];
523 dma
= PARPORT_DMA_AUTO
;
525 if (!(p
= parport_register_port(base
, irq
, dma
, &parport_ax_ops
)))
528 /* Save away pointer to our EBus DMA */
529 p
->private_data
= (void *)dev
->base_address
[2];
531 p
->modes
= PARPORT_MODE_PCSPP
| parport_PS2_supported(p
);
532 if (!check_region(p
->base
+ 0x400, 3)) {
533 p
->modes
|= parport_ECR_present(p
);
534 p
->modes
|= parport_ECP_supported(p
);
535 p
->modes
|= parport_ECPPS2_supported(p
);
539 if (p
->dma
== PARPORT_DMA_AUTO
)
540 p
->dma
= (p
->modes
& PARPORT_MODE_PCECP
) ? 0 : PARPORT_DMA_NONE
;
542 if (p
->irq
!= PARPORT_IRQ_NONE
) {
544 if ((err
= request_irq(p
->irq
, parport_ax_interrupt
,
545 0, p
->name
, p
)) != 0)
548 parport_ax_enable_irq(p
);
550 request_region(p
->base
, p
->size
, p
->name
);
551 if (p
->modes
& PARPORT_MODE_PCECR
)
552 request_region(p
->base
+0x400, 3, p
->name
);
553 request_region((unsigned long)p
->private_data
,
554 sizeof(struct linux_ebus_dma
), p
->name
);
556 printk(KERN_INFO
"%s: PC-style at 0x%lx", p
->name
, p
->base
);
557 if (p
->irq
!= PARPORT_IRQ_NONE
)
558 printk(", irq %s", __irq_itoa(p
->irq
));
559 if (p
->dma
!= PARPORT_DMA_NONE
)
560 printk(", dma %d", p
->dma
);
570 parport_proc_register(p
);
572 p
->ops
->write_control(p
, 0x0c);
573 p
->ops
->write_data(p
, 0);
575 if (parport_probe_hook
)
576 (*parport_probe_hook
)(p
);
578 parport_announce_port (p
);
586 int init_module(void)
588 __initfunc(int parport_ax_init(void))
591 struct linux_ebus
*ebus
;
592 struct linux_ebus_device
*edev
;
595 for_each_ebus(ebus
) {
596 for_each_ebusdev(edev
, ebus
) {
597 if (!strcmp(edev
->prom_name
, "ecpp"))
598 count
+= init_one_port(edev
);
601 return count
? 0 : -ENODEV
;
608 struct parport
*p
= parport_enumerate(), *tmp
;
611 if (p
->modes
& PARPORT_MODE_PCSPP
) {
612 if (p
->irq
!= PARPORT_IRQ_NONE
) {
613 parport_ax_disable_irq(p
);
616 release_region(p
->base
, p
->size
);
617 if (p
->modes
& PARPORT_MODE_PCECR
)
618 release_region(p
->base
+0x400, 3);
619 release_region((unsigned long)p
->private_data
,
620 sizeof(struct linux_ebus_dma
));
621 parport_proc_unregister(p
);
622 parport_unregister_port(p
);