Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *      Low-level parallel-support for PC-style hardware integrated in the 
0004  *  LASI-Controller (on GSC-Bus) for HP-PARISC Workstations
0005  *
0006  *  (C) 1999-2001 by Helge Deller <deller@gmx.de>
0007  * 
0008  * based on parport_pc.c by 
0009  *      Grant Guenther <grant@torque.net>
0010  *      Phil Blundell <philb@gnu.org>
0011  *          Tim Waugh <tim@cyberelk.demon.co.uk>
0012  *      Jose Renau <renau@acm.org>
0013  *          David Campbell
0014  *          Andrea Arcangeli
0015  */
0016 
0017 #undef DEBUG    /* undef for production */
0018 
0019 #include <linux/module.h>
0020 #include <linux/init.h>
0021 #include <linux/delay.h>
0022 #include <linux/errno.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/ioport.h>
0025 #include <linux/kernel.h>
0026 #include <linux/slab.h>
0027 #include <linux/pci.h>
0028 #include <linux/sysctl.h>
0029 
0030 #include <asm/io.h>
0031 #include <asm/dma.h>
0032 #include <linux/uaccess.h>
0033 #include <asm/superio.h>
0034 
0035 #include <linux/parport.h>
0036 #include <asm/pdc.h>
0037 #include <asm/parisc-device.h>
0038 #include <asm/hardware.h>
0039 #include "parport_gsc.h"
0040 
0041 
0042 MODULE_AUTHOR("Helge Deller <deller@gmx.de>");
0043 MODULE_DESCRIPTION("HP-PARISC PC-style parallel port driver");
0044 MODULE_LICENSE("GPL");
0045 
0046 
0047 /*
0048  * Clear TIMEOUT BIT in EPP MODE
0049  *
0050  * This is also used in SPP detection.
0051  */
0052 static int clear_epp_timeout(struct parport *pb)
0053 {
0054     unsigned char r;
0055 
0056     if (!(parport_gsc_read_status(pb) & 0x01))
0057         return 1;
0058 
0059     /* To clear timeout some chips require double read */
0060     parport_gsc_read_status(pb);
0061     r = parport_gsc_read_status(pb);
0062     parport_writeb (r | 0x01, STATUS (pb)); /* Some reset by writing 1 */
0063     parport_writeb (r & 0xfe, STATUS (pb)); /* Others by writing 0 */
0064     r = parport_gsc_read_status(pb);
0065 
0066     return !(r & 0x01);
0067 }
0068 
0069 /*
0070  * Access functions.
0071  *
0072  * Most of these aren't static because they may be used by the
0073  * parport_xxx_yyy macros.  extern __inline__ versions of several
0074  * of these are in parport_gsc.h.
0075  */
0076 
0077 void parport_gsc_init_state(struct pardevice *dev, struct parport_state *s)
0078 {
0079     s->u.pc.ctr = 0xc | (dev->irq_func ? 0x10 : 0x0);
0080 }
0081 
0082 void parport_gsc_save_state(struct parport *p, struct parport_state *s)
0083 {
0084     s->u.pc.ctr = parport_readb (CONTROL (p));
0085 }
0086 
0087 void parport_gsc_restore_state(struct parport *p, struct parport_state *s)
0088 {
0089     parport_writeb (s->u.pc.ctr, CONTROL (p));
0090 }
0091 
0092 struct parport_operations parport_gsc_ops = 
0093 {
0094     .write_data = parport_gsc_write_data,
0095     .read_data  = parport_gsc_read_data,
0096 
0097     .write_control  = parport_gsc_write_control,
0098     .read_control   = parport_gsc_read_control,
0099     .frob_control   = parport_gsc_frob_control,
0100 
0101     .read_status    = parport_gsc_read_status,
0102 
0103     .enable_irq = parport_gsc_enable_irq,
0104     .disable_irq    = parport_gsc_disable_irq,
0105 
0106     .data_forward   = parport_gsc_data_forward,
0107     .data_reverse   = parport_gsc_data_reverse,
0108 
0109     .init_state = parport_gsc_init_state,
0110     .save_state = parport_gsc_save_state,
0111     .restore_state  = parport_gsc_restore_state,
0112 
0113     .epp_write_data = parport_ieee1284_epp_write_data,
0114     .epp_read_data  = parport_ieee1284_epp_read_data,
0115     .epp_write_addr = parport_ieee1284_epp_write_addr,
0116     .epp_read_addr  = parport_ieee1284_epp_read_addr,
0117 
0118     .ecp_write_data = parport_ieee1284_ecp_write_data,
0119     .ecp_read_data  = parport_ieee1284_ecp_read_data,
0120     .ecp_write_addr = parport_ieee1284_ecp_write_addr,
0121 
0122     .compat_write_data  = parport_ieee1284_write_compat,
0123     .nibble_read_data   = parport_ieee1284_read_nibble,
0124     .byte_read_data     = parport_ieee1284_read_byte,
0125 
0126     .owner      = THIS_MODULE,
0127 };
0128 
0129 /* --- Mode detection ------------------------------------- */
0130 
0131 /*
0132  * Checks for port existence, all ports support SPP MODE
0133  */
0134 static int parport_SPP_supported(struct parport *pb)
0135 {
0136     unsigned char r, w;
0137 
0138     /*
0139      * first clear an eventually pending EPP timeout 
0140      * I (sailer@ife.ee.ethz.ch) have an SMSC chipset
0141      * that does not even respond to SPP cycles if an EPP
0142      * timeout is pending
0143      */
0144     clear_epp_timeout(pb);
0145 
0146     /* Do a simple read-write test to make sure the port exists. */
0147     w = 0xc;
0148     parport_writeb (w, CONTROL (pb));
0149 
0150     /* Is there a control register that we can read from?  Some
0151      * ports don't allow reads, so read_control just returns a
0152      * software copy. Some ports _do_ allow reads, so bypass the
0153      * software copy here.  In addition, some bits aren't
0154      * writable. */
0155     r = parport_readb (CONTROL (pb));
0156     if ((r & 0xf) == w) {
0157         w = 0xe;
0158         parport_writeb (w, CONTROL (pb));
0159         r = parport_readb (CONTROL (pb));
0160         parport_writeb (0xc, CONTROL (pb));
0161         if ((r & 0xf) == w)
0162             return PARPORT_MODE_PCSPP;
0163     }
0164 
0165     /* Try the data register.  The data lines aren't tri-stated at
0166      * this stage, so we expect back what we wrote. */
0167     w = 0xaa;
0168     parport_gsc_write_data (pb, w);
0169     r = parport_gsc_read_data (pb);
0170     if (r == w) {
0171         w = 0x55;
0172         parport_gsc_write_data (pb, w);
0173         r = parport_gsc_read_data (pb);
0174         if (r == w)
0175             return PARPORT_MODE_PCSPP;
0176     }
0177 
0178     return 0;
0179 }
0180 
0181 /* Detect PS/2 support.
0182  *
0183  * Bit 5 (0x20) sets the PS/2 data direction; setting this high
0184  * allows us to read data from the data lines.  In theory we would get back
0185  * 0xff but any peripheral attached to the port may drag some or all of the
0186  * lines down to zero.  So if we get back anything that isn't the contents
0187  * of the data register we deem PS/2 support to be present. 
0188  *
0189  * Some SPP ports have "half PS/2" ability - you can't turn off the line
0190  * drivers, but an external peripheral with sufficiently beefy drivers of
0191  * its own can overpower them and assert its own levels onto the bus, from
0192  * where they can then be read back as normal.  Ports with this property
0193  * and the right type of device attached are likely to fail the SPP test,
0194  * (as they will appear to have stuck bits) and so the fact that they might
0195  * be misdetected here is rather academic. 
0196  */
0197 
0198 static int parport_PS2_supported(struct parport *pb)
0199 {
0200     int ok = 0;
0201   
0202     clear_epp_timeout(pb);
0203 
0204     /* try to tri-state the buffer */
0205     parport_gsc_data_reverse (pb);
0206     
0207     parport_gsc_write_data(pb, 0x55);
0208     if (parport_gsc_read_data(pb) != 0x55) ok++;
0209 
0210     parport_gsc_write_data(pb, 0xaa);
0211     if (parport_gsc_read_data(pb) != 0xaa) ok++;
0212 
0213     /* cancel input mode */
0214     parport_gsc_data_forward (pb);
0215 
0216     if (ok) {
0217         pb->modes |= PARPORT_MODE_TRISTATE;
0218     } else {
0219         struct parport_gsc_private *priv = pb->private_data;
0220         priv->ctr_writable &= ~0x20;
0221     }
0222 
0223     return ok;
0224 }
0225 
0226 
0227 /* --- Initialisation code -------------------------------- */
0228 
0229 struct parport *parport_gsc_probe_port(unsigned long base,
0230                        unsigned long base_hi, int irq,
0231                        int dma, struct parisc_device *padev)
0232 {
0233     struct parport_gsc_private *priv;
0234     struct parport_operations *ops;
0235     struct parport tmp;
0236     struct parport *p = &tmp;
0237 
0238     priv = kzalloc (sizeof (struct parport_gsc_private), GFP_KERNEL);
0239     if (!priv) {
0240         printk(KERN_DEBUG "parport (0x%lx): no memory!\n", base);
0241         return NULL;
0242     }
0243     ops = kmemdup(&parport_gsc_ops, sizeof(struct parport_operations),
0244               GFP_KERNEL);
0245     if (!ops) {
0246         printk(KERN_DEBUG "parport (0x%lx): no memory for ops!\n",
0247                base);
0248         kfree (priv);
0249         return NULL;
0250     }
0251     priv->ctr = 0xc;
0252     priv->ctr_writable = 0xff;
0253     priv->dma_buf = NULL;
0254     priv->dma_handle = 0;
0255     p->base = base;
0256     p->base_hi = base_hi;
0257     p->irq = irq;
0258     p->dma = dma;
0259     p->modes = PARPORT_MODE_PCSPP | PARPORT_MODE_SAFEININT;
0260     p->ops = ops;
0261     p->private_data = priv;
0262     p->physport = p;
0263     if (!parport_SPP_supported (p)) {
0264         /* No port. */
0265         kfree (priv);
0266         kfree(ops);
0267         return NULL;
0268     }
0269     parport_PS2_supported (p);
0270 
0271     if (!(p = parport_register_port(base, PARPORT_IRQ_NONE,
0272                     PARPORT_DMA_NONE, ops))) {
0273         kfree (priv);
0274         kfree (ops);
0275         return NULL;
0276     }
0277 
0278     p->dev = &padev->dev;
0279     p->base_hi = base_hi;
0280     p->modes = tmp.modes;
0281     p->size = (p->modes & PARPORT_MODE_EPP)?8:3;
0282     p->private_data = priv;
0283 
0284     pr_info("%s: PC-style at 0x%lx", p->name, p->base);
0285     p->irq = irq;
0286     if (p->irq == PARPORT_IRQ_AUTO) {
0287         p->irq = PARPORT_IRQ_NONE;
0288     }
0289     if (p->irq != PARPORT_IRQ_NONE) {
0290         pr_cont(", irq %d", p->irq);
0291 
0292         if (p->dma == PARPORT_DMA_AUTO) {
0293             p->dma = PARPORT_DMA_NONE;
0294         }
0295     }
0296     if (p->dma == PARPORT_DMA_AUTO) /* To use DMA, giving the irq
0297                                            is mandatory (see above) */
0298         p->dma = PARPORT_DMA_NONE;
0299 
0300     pr_cont(" [");
0301 #define printmode(x)                            \
0302 do {                                    \
0303     if (p->modes & PARPORT_MODE_##x)                \
0304         pr_cont("%s%s", f++ ? "," : "", #x);            \
0305 } while (0)
0306     {
0307         int f = 0;
0308         printmode(PCSPP);
0309         printmode(TRISTATE);
0310         printmode(COMPAT);
0311         printmode(EPP);
0312 //      printmode(ECP);
0313 //      printmode(DMA);
0314     }
0315 #undef printmode
0316     pr_cont("]\n");
0317 
0318     if (p->irq != PARPORT_IRQ_NONE) {
0319         if (request_irq (p->irq, parport_irq_handler,
0320                  0, p->name, p)) {
0321             pr_warn("%s: irq %d in use, resorting to polled operation\n",
0322                 p->name, p->irq);
0323             p->irq = PARPORT_IRQ_NONE;
0324             p->dma = PARPORT_DMA_NONE;
0325         }
0326     }
0327 
0328     /* Done probing.  Now put the port into a sensible start-up state. */
0329 
0330     parport_gsc_write_data(p, 0);
0331     parport_gsc_data_forward (p);
0332 
0333     /* Now that we've told the sharing engine about the port, and
0334        found out its characteristics, let the high-level drivers
0335        know about it. */
0336     parport_announce_port (p);
0337 
0338     return p;
0339 }
0340 
0341 
0342 #define PARPORT_GSC_OFFSET 0x800
0343 
0344 static int parport_count;
0345 
0346 static int __init parport_init_chip(struct parisc_device *dev)
0347 {
0348     struct parport *p;
0349     unsigned long port;
0350 
0351     if (!dev->irq) {
0352         pr_warn("IRQ not found for parallel device at 0x%llx\n",
0353             (unsigned long long)dev->hpa.start);
0354         return -ENODEV;
0355     }
0356 
0357     port = dev->hpa.start + PARPORT_GSC_OFFSET;
0358     
0359     /* some older machines with ASP-chip don't support
0360      * the enhanced parport modes.
0361      */
0362     if (boot_cpu_data.cpu_type > pcxt && !pdc_add_valid(port+4)) {
0363 
0364         /* Initialize bidirectional-mode (0x10) & data-tranfer-mode #1 (0x20) */
0365         pr_info("%s: initialize bidirectional-mode\n", __func__);
0366         parport_writeb ( (0x10 + 0x20), port + 4);
0367 
0368     } else {
0369         pr_info("%s: enhanced parport-modes not supported\n", __func__);
0370     }
0371     
0372     p = parport_gsc_probe_port(port, 0, dev->irq,
0373             /* PARPORT_IRQ_NONE */ PARPORT_DMA_NONE, dev);
0374     if (p)
0375         parport_count++;
0376     dev_set_drvdata(&dev->dev, p);
0377 
0378     return 0;
0379 }
0380 
0381 static void __exit parport_remove_chip(struct parisc_device *dev)
0382 {
0383     struct parport *p = dev_get_drvdata(&dev->dev);
0384     if (p) {
0385         struct parport_gsc_private *priv = p->private_data;
0386         struct parport_operations *ops = p->ops;
0387         parport_remove_port(p);
0388         if (p->dma != PARPORT_DMA_NONE)
0389             free_dma(p->dma);
0390         if (p->irq != PARPORT_IRQ_NONE)
0391             free_irq(p->irq, p);
0392         if (priv->dma_buf)
0393             dma_free_coherent(&priv->dev->dev, PAGE_SIZE,
0394                       priv->dma_buf, priv->dma_handle);
0395         kfree (p->private_data);
0396         parport_put_port(p);
0397         kfree (ops); /* hope no-one cached it */
0398     }
0399 }
0400 
0401 static const struct parisc_device_id parport_tbl[] __initconst = {
0402     { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x74 },
0403     { 0, }
0404 };
0405 
0406 MODULE_DEVICE_TABLE(parisc, parport_tbl);
0407 
0408 static struct parisc_driver parport_driver __refdata = {
0409     .name       = "Parallel",
0410     .id_table   = parport_tbl,
0411     .probe      = parport_init_chip,
0412     .remove     = __exit_p(parport_remove_chip),
0413 };
0414 
0415 int parport_gsc_init(void)
0416 {
0417     return register_parisc_driver(&parport_driver);
0418 }
0419 
0420 static void parport_gsc_exit(void)
0421 {
0422     unregister_parisc_driver(&parport_driver);
0423 }
0424 
0425 module_init(parport_gsc_init);
0426 module_exit(parport_gsc_exit);