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_release_resources(struct parport
*p
)
207 if (p
->irq
!= PARPORT_IRQ_NONE
) {
208 parport_ax_disable_irq(p
);
211 release_region(p
->base
, p
->size
);
212 if (p
->modes
& PARPORT_MODE_PCECR
)
213 release_region(p
->base
+0x400, 3);
214 release_region((unsigned long)p
->private_data
,
215 sizeof(struct linux_ebus_dma
));
219 parport_ax_claim_resources(struct parport
*p
)
221 /* FIXME check that resources are free */
224 if (p
->irq
!= PARPORT_IRQ_NONE
) {
225 if ((err
= request_irq(p
->irq
, parport_ax_interrupt
,
226 0, p
->name
, p
)) != 0)
229 parport_ax_enable_irq(p
);
231 request_region(p
->base
, p
->size
, p
->name
);
232 if (p
->modes
& PARPORT_MODE_PCECR
)
233 request_region(p
->base
+0x400, 3, p
->name
);
234 request_region((unsigned long)p
->private_data
,
235 sizeof(struct linux_ebus_dma
), p
->name
);
240 parport_ax_init_state(struct parport_state
*s
)
247 parport_ax_save_state(struct parport
*p
, struct parport_state
*s
)
249 s
->u
.pc
.ctr
= parport_ax_read_control(p
);
250 s
->u
.pc
.ecr
= parport_ax_read_econtrol(p
);
254 parport_ax_restore_state(struct parport
*p
, struct parport_state
*s
)
256 parport_ax_write_control(p
, s
->u
.pc
.ctr
);
257 parport_ax_write_econtrol(p
, s
->u
.pc
.ecr
);
261 parport_ax_epp_read_block(struct parport
*p
, void *buf
, size_t length
)
263 return 0; /* FIXME */
267 parport_ax_epp_write_block(struct parport
*p
, void *buf
, size_t length
)
269 return 0; /* FIXME */
273 parport_ax_ecp_read_block(struct parport
*p
, void *buf
, size_t length
,
274 void (*fn
)(struct parport
*, void *, size_t),
277 return 0; /* FIXME */
281 parport_ax_ecp_write_block(struct parport
*p
, void *buf
, size_t length
,
282 void (*fn
)(struct parport
*, void *, size_t),
285 return 0; /* FIXME */
289 parport_ax_inc_use_count(void)
297 parport_ax_dec_use_count(void)
304 static void parport_ax_fill_inode(struct inode
*inode
, int fill
)
314 static struct parport_operations parport_ax_ops
=
316 parport_ax_write_data
,
317 parport_ax_read_data
,
319 parport_ax_write_control
,
320 parport_ax_read_control
,
321 parport_ax_frob_control
,
323 parport_ax_write_econtrol
,
324 parport_ax_read_econtrol
,
325 parport_ax_frob_econtrol
,
327 parport_ax_write_status
,
328 parport_ax_read_status
,
330 parport_ax_write_fifo
,
331 parport_ax_read_fifo
,
333 parport_ax_change_mode
,
335 parport_ax_release_resources
,
336 parport_ax_claim_resources
,
338 parport_ax_write_epp
,
340 parport_ax_write_epp_addr
,
341 parport_ax_read_epp_addr
,
342 parport_ax_check_epp_timeout
,
344 parport_ax_epp_write_block
,
345 parport_ax_epp_read_block
,
347 parport_ax_ecp_write_block
,
348 parport_ax_ecp_read_block
,
350 parport_ax_init_state
,
351 parport_ax_save_state
,
352 parport_ax_restore_state
,
354 parport_ax_enable_irq
,
355 parport_ax_disable_irq
,
356 parport_ax_interrupt
,
358 parport_ax_inc_use_count
,
359 parport_ax_dec_use_count
,
360 parport_ax_fill_inode
364 /******************************************************
365 * MODE detection section:
369 * Clear TIMEOUT BIT in EPP MODE
371 int parport_ax_epp_clear_timeout(struct parport
*pb
)
375 if (!(parport_ax_read_status(pb
) & 0x01))
378 /* To clear timeout some chips require double read */
379 parport_ax_read_status(pb
);
380 r
= parport_ax_read_status(pb
);
381 parport_ax_write_status(pb
, r
| 0x01); /* Some reset by writing 1 */
382 parport_ax_write_status(pb
, r
& 0xfe); /* Others by writing 0 */
383 r
= parport_ax_read_status(pb
);
390 * Old style XT ports alias io ports every 0x400, hence accessing ECONTROL
391 * on these cards actually accesses the CTR.
393 * Modern cards don't do this but reading from ECONTROL will return 0xff
394 * regardless of what is written here if the card does NOT support
397 * We will write 0x2c to ECONTROL and 0xcc to CTR since both of these
398 * values are "safe" on the CTR since bits 6-7 of CTR are unused.
400 static int parport_ECR_present(struct parport
*pb
)
403 unsigned char octr
= pb
->ops
->read_control(pb
),
404 oecr
= pb
->ops
->read_econtrol(pb
);
406 r
= pb
->ops
->read_control(pb
);
407 if ((pb
->ops
->read_econtrol(pb
) & 0x3) == (r
& 0x3)) {
408 pb
->ops
->write_control(pb
, r
^ 0x2 ); /* Toggle bit 1 */
410 r
= pb
->ops
->read_control(pb
);
411 if ((pb
->ops
->read_econtrol(pb
) & 0x2) == (r
& 0x2)) {
412 pb
->ops
->write_control(pb
, octr
);
413 return 0; /* Sure that no ECONTROL register exists */
417 if ((pb
->ops
->read_econtrol(pb
) & 0x3 ) != 0x1)
420 pb
->ops
->write_econtrol(pb
, 0x34);
421 if (pb
->ops
->read_econtrol(pb
) != 0x35)
424 pb
->ops
->write_econtrol(pb
, oecr
);
425 pb
->ops
->write_control(pb
, octr
);
427 return PARPORT_MODE_PCECR
;
430 static int parport_ECP_supported(struct parport
*pb
)
433 unsigned char oecr
= pb
->ops
->read_econtrol(pb
);
435 /* If there is no ECONTROL, we have no hope of supporting ECP. */
436 if (!(pb
->modes
& PARPORT_MODE_PCECR
))
440 * Using LGS chipset it uses ECONTROL register, but
441 * it doesn't support ECP or FIFO MODE
444 pb
->ops
->write_econtrol(pb
, 0xc0); /* TEST FIFO */
445 for (i
=0; i
< 1024 && (pb
->ops
->read_econtrol(pb
) & 0x01); i
++)
446 pb
->ops
->write_fifo(pb
, 0xaa);
448 pb
->ops
->write_econtrol(pb
, oecr
);
449 return (i
== 1024) ? 0 : PARPORT_MODE_PCECP
;
452 /* Detect PS/2 support.
454 * Bit 5 (0x20) sets the PS/2 data direction; setting this high
455 * allows us to read data from the data lines. In theory we would get back
456 * 0xff but any peripheral attached to the port may drag some or all of the
457 * lines down to zero. So if we get back anything that isn't the contents
458 * of the data register we deem PS/2 support to be present.
460 * Some SPP ports have "half PS/2" ability - you can't turn off the line
461 * drivers, but an external peripheral with sufficiently beefy drivers of
462 * its own can overpower them and assert its own levels onto the bus, from
463 * where they can then be read back as normal. Ports with this property
464 * and the right type of device attached are likely to fail the SPP test,
465 * (as they will appear to have stuck bits) and so the fact that they might
466 * be misdetected here is rather academic.
469 static int parport_PS2_supported(struct parport
*pb
)
472 unsigned char octr
= pb
->ops
->read_control(pb
);
474 pb
->ops
->write_control(pb
, octr
| 0x20); /* try to tri-state buffer */
476 pb
->ops
->write_data(pb
, 0x55);
477 if (pb
->ops
->read_data(pb
) != 0x55) ok
++;
479 pb
->ops
->write_data(pb
, 0xaa);
480 if (pb
->ops
->read_data(pb
) != 0xaa) ok
++;
482 pb
->ops
->write_control(pb
, octr
); /* cancel input mode */
484 return ok
? PARPORT_MODE_PCPS2
: 0;
487 static int parport_ECPPS2_supported(struct parport
*pb
)
490 unsigned char oecr
= pb
->ops
->read_econtrol(pb
);
492 if (!(pb
->modes
& PARPORT_MODE_PCECR
))
495 pb
->ops
->write_econtrol(pb
, 0x20);
497 mode
= parport_PS2_supported(pb
);
499 pb
->ops
->write_econtrol(pb
, oecr
);
500 return mode
? PARPORT_MODE_PCECPPS2
: 0;
503 #define printmode(x) \
505 if (p->modes & PARPORT_MODE_PC##x) { \
506 printk("%s%s", f ? "," : "", #x); \
512 init_one_port(struct linux_ebus_device
*dev
)
514 struct parport tmpport
, *p
;
516 unsigned long config
;
520 /* Pointer to NS87303 Configuration Registers */
521 config
= dev
->base_address
[1];
523 /* Setup temporary access to Device operations */
524 tmpport
.base
= dev
->base_address
[0];
525 tmpport
.ops
= &parport_ax_ops
;
527 /* Enable ECP mode, set bit 2 of the CTR first */
528 tmpport
.ops
->write_control(&tmpport
, 0x04);
529 tmp
= ns87303_readb(config
, PCR
);
530 tmp
|= (PCR_EPP_IEEE
| PCR_ECP_ENABLE
| PCR_ECP_CLK_ENA
);
531 ns87303_writeb(config
, PCR
, tmp
);
533 /* LPT CTR bit 5 controls direction of parallel port */
534 tmp
= ns87303_readb(config
, PTR
);
535 tmp
|= PTR_LPT_REG_DIR
;
536 ns87303_writeb(config
, PTR
, tmp
);
538 /* Configure IRQ to Push Pull, Level Low */
539 tmp
= ns87303_readb(config
, PCR
);
540 tmp
&= ~(PCR_IRQ_ODRAIN
);
541 tmp
|= PCR_IRQ_POLAR
;
542 ns87303_writeb(config
, PCR
, tmp
);
544 #ifndef HAVE_SLOW_DEVICES
545 /* Enable Zero Wait State for ECP */
546 tmp
= ns87303_readb(config
, FCR
);
548 ns87303_writeb(config
, FCR
, tmp
);
552 * Now continue initializing the port
554 base
= dev
->base_address
[0];
556 dma
= PARPORT_DMA_AUTO
;
558 if (!(p
= parport_register_port(base
, irq
, dma
, &parport_ax_ops
)))
561 /* Save away pointer to our EBus DMA */
562 p
->private_data
= (void *)dev
->base_address
[2];
564 p
->modes
= PARPORT_MODE_PCSPP
| parport_PS2_supported(p
);
565 if (!check_region(p
->base
+ 0x400, 3)) {
566 p
->modes
|= parport_ECR_present(p
);
567 p
->modes
|= parport_ECP_supported(p
);
568 p
->modes
|= parport_ECPPS2_supported(p
);
572 if (p
->dma
== PARPORT_DMA_AUTO
)
573 p
->dma
= (p
->modes
& PARPORT_MODE_PCECP
) ? 0 : PARPORT_DMA_NONE
;
575 printk(KERN_INFO
"%s: PC-style at 0x%lx", p
->name
, p
->base
);
576 if (p
->irq
!= PARPORT_IRQ_NONE
)
577 printk(", irq %s", __irq_itoa(p
->irq
));
578 if (p
->dma
!= PARPORT_DMA_NONE
)
579 printk(", dma %d", p
->dma
);
589 parport_proc_register(p
);
590 p
->flags
|= PARPORT_FLAG_COMA
;
592 p
->ops
->write_control(p
, 0x0c);
593 p
->ops
->write_data(p
, 0);
595 if (parport_probe_hook
)
596 (*parport_probe_hook
)(p
);
604 int init_module(void)
606 __initfunc(int parport_ax_init(void))
609 struct linux_ebus
*ebus
;
610 struct linux_ebus_device
*edev
;
613 for_each_ebus(ebus
) {
614 for_each_ebusdev(edev
, ebus
) {
615 if (!strcmp(edev
->prom_name
, "ecpp"))
616 count
+= init_one_port(edev
);
619 return count
? 0 : -ENODEV
;
626 struct parport
*p
= parport_enumerate(), *tmp
;
629 if (p
->modes
& PARPORT_MODE_PCSPP
) {
630 if (!(p
->flags
& PARPORT_FLAG_COMA
))
632 parport_proc_unregister(p
);
633 parport_unregister_port(p
);