1 /* ------------------------------------------------------------------------- */
 
   2 /* i2c-elektor.c i2c-hw access for PCF8584 style isa bus adaptes             */
 
   3 /* ------------------------------------------------------------------------- */
 
   4 /*   Copyright (C) 1995-97 Simon G. Vogl
 
   7     This program is free software; you can redistribute it and/or modify
 
   8     it under the terms of the GNU General Public License as published by
 
   9     the Free Software Foundation; either version 2 of the License, or
 
  10     (at your option) any later version.
 
  12     This program is distributed in the hope that it will be useful,
 
  13     but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  15     GNU General Public License for more details.
 
  17     You should have received a copy of the GNU General Public License
 
  18     along with this program; if not, write to the Free Software
 
  19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
 
  20 /* ------------------------------------------------------------------------- */
 
  22 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
 
  23    Frodo Looijaard <frodol@dds.nl> */
 
  25 /* Partialy rewriten by Oleg I. Vdovikin for mmapped support of
 
  26    for Alpha Processor Inc. UP-2000(+) boards */
 
  28 #include <linux/kernel.h>
 
  29 #include <linux/ioport.h>
 
  30 #include <linux/module.h>
 
  31 #include <linux/delay.h>
 
  32 #include <linux/slab.h>
 
  33 #include <linux/init.h>
 
  34 #include <linux/interrupt.h>
 
  35 #include <linux/pci.h>
 
  36 #include <linux/wait.h>
 
  38 #include <linux/i2c.h>
 
  39 #include <linux/i2c-algo-pcf.h>
 
  44 #include "../algos/i2c-algo-pcf.h"
 
  46 #define DEFAULT_BASE 0x330
 
  49 static u8 __iomem *base_iomem;
 
  52 static int clock  = 0x1c;
 
  53 static int own    = 0x55;
 
  56 /* vdovikin: removed static struct i2c_pcf_isa gpi; code -
 
  57   this module in real supports only one device, due to missing arguments
 
  58   in some functions, called from the algo-pcf module. Sometimes it's
 
  59   need to be rewriten - but for now just remove this for simpler reading */
 
  61 static wait_queue_head_t pcf_wait;
 
  62 static int pcf_pending;
 
  63 static spinlock_t lock;
 
  65 static struct i2c_adapter pcf_isa_ops;
 
  67 /* ----- local functions ---------------------------------------------- */
 
  69 static void pcf_isa_setbyte(void *data, int ctl, int val)
 
  71         u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
 
  73         /* enable irq if any specified for serial operation */
 
  74         if (ctl && irq && (val & I2C_PCF_ESO)) {
 
  78         pr_debug("%s: Write %p 0x%02X\n", pcf_isa_ops.name, address, val);
 
  79         iowrite8(val, address);
 
  81         /* API UP2000 needs some hardware fudging to make the write stick */
 
  82         iowrite8(val, address);
 
  86 static int pcf_isa_getbyte(void *data, int ctl)
 
  88         u8 __iomem *address = ctl ? (base_iomem + 1) : base_iomem;
 
  89         int val = ioread8(address);
 
  91         pr_debug("%s: Read %p 0x%02X\n", pcf_isa_ops.name, address, val);
 
  95 static int pcf_isa_getown(void *data)
 
 101 static int pcf_isa_getclock(void *data)
 
 106 static void pcf_isa_waitforpin(void) {
 
 112                 spin_lock_irqsave(&lock, flags);
 
 113                 if (pcf_pending == 0) {
 
 114                         spin_unlock_irqrestore(&lock, flags);
 
 115                         prepare_to_wait(&pcf_wait, &wait, TASK_INTERRUPTIBLE);
 
 116                         if (schedule_timeout(timeout*HZ)) {
 
 117                                 spin_lock_irqsave(&lock, flags);
 
 118                                 if (pcf_pending == 1) {
 
 121                                 spin_unlock_irqrestore(&lock, flags);
 
 123                         finish_wait(&pcf_wait, &wait);
 
 126                         spin_unlock_irqrestore(&lock, flags);
 
 134 static irqreturn_t pcf_isa_handler(int this_irq, void *dev_id, struct pt_regs *regs) {
 
 138         wake_up_interruptible(&pcf_wait);
 
 143 static int pcf_isa_init(void)
 
 145         spin_lock_init(&lock);
 
 147                 if (!request_region(base, 2, pcf_isa_ops.name)) {
 
 148                         printk(KERN_ERR "%s: requested I/O region (%#x:2) is "
 
 149                                "in use\n", pcf_isa_ops.name, base);
 
 152                 base_iomem = ioport_map(base, 2);
 
 154                         printk(KERN_ERR "%s: remap of I/O region %#x failed\n",
 
 155                                pcf_isa_ops.name, base);
 
 156                         release_region(base, 2);
 
 160                 if (!request_mem_region(base, 2, pcf_isa_ops.name)) {
 
 161                         printk(KERN_ERR "%s: requested memory region (%#x:2) "
 
 162                                "is in use\n", pcf_isa_ops.name, base);
 
 165                 base_iomem = ioremap(base, 2);
 
 166                 if (base_iomem == NULL) {
 
 167                         printk(KERN_ERR "%s: remap of memory region %#x "
 
 168                                "failed\n", pcf_isa_ops.name, base);
 
 169                         release_mem_region(base, 2);
 
 173         pr_debug("%s: registers %#x remapped to %p\n", pcf_isa_ops.name, base,
 
 177                 if (request_irq(irq, pcf_isa_handler, 0, pcf_isa_ops.name,
 
 179                         printk(KERN_ERR "%s: Request irq%d failed\n",
 
 180                                pcf_isa_ops.name, irq);
 
 188 /* ------------------------------------------------------------------------
 
 189  * Encapsulate the above functions in the correct operations structure.
 
 190  * This is only done when more than one hardware adapter is supported.
 
 192 static struct i2c_algo_pcf_data pcf_isa_data = {
 
 193         .setpcf     = pcf_isa_setbyte,
 
 194         .getpcf     = pcf_isa_getbyte,
 
 195         .getown     = pcf_isa_getown,
 
 196         .getclock   = pcf_isa_getclock,
 
 197         .waitforpin = pcf_isa_waitforpin,
 
 203 static struct i2c_adapter pcf_isa_ops = {
 
 204         .owner          = THIS_MODULE,
 
 205         .class          = I2C_CLASS_HWMON,
 
 207         .algo_data      = &pcf_isa_data,
 
 208         .name           = "i2c-elektor",
 
 211 static int __init i2c_pcfisa_init(void)
 
 214         /* check to see we have memory mapped PCF8584 connected to the
 
 215         Cypress cy82c693 PCI-ISA bridge as on UP2000 board */
 
 217                 struct pci_dev *cy693_dev;
 
 219                 cy693_dev = pci_get_device(PCI_VENDOR_ID_CONTAQ,
 
 220                                            PCI_DEVICE_ID_CONTAQ_82C693, NULL);
 
 222                         unsigned char config;
 
 223                         /* yeap, we've found cypress, let's check config */
 
 224                         if (!pci_read_config_byte(cy693_dev, 0x47, &config)) {
 
 226                                 pr_debug("%s: found cy82c693, config "
 
 227                                          "register 0x47 = 0x%02x\n",
 
 228                                          pcf_isa_ops.name, config);
 
 230                                 /* UP2000 board has this register set to 0xe1,
 
 231                                    but the most significant bit as seems can be
 
 232                                    reset during the proper initialisation
 
 233                                    sequence if guys from API decides to do that
 
 234                                    (so, we can even enable Tsunami Pchip
 
 235                                    window for the upper 1 Gb) */
 
 237                                 /* so just check for ROMCS at 0xe0000,
 
 238                                    ROMCS enabled for writes
 
 239                                    and external XD Bus buffer in use. */
 
 240                                 if ((config & 0x7f) == 0x61) {
 
 241                                         /* seems to be UP2000 like board */
 
 244                                         /* UP2000 drives ISA with
 
 245                                            8.25 MHz (PCI/4) clock
 
 246                                            (this can be read from cypress) */
 
 247                                         clock = I2C_PCF_CLK | I2C_PCF_TRNS90;
 
 248                                         pr_info("%s: found API UP2000 like "
 
 249                                                 "board, will probe PCF8584 "
 
 250                                                 "later\n", pcf_isa_ops.name);
 
 253                         pci_dev_put(cy693_dev);
 
 258         /* sanity checks for mmapped I/O */
 
 259         if (mmapped && base < 0xc8000) {
 
 260                 printk(KERN_ERR "%s: incorrect base address (%#x) specified "
 
 261                        "for mmapped I/O\n", pcf_isa_ops.name, base);
 
 269         init_waitqueue_head(&pcf_wait);
 
 272         if (i2c_pcf_add_bus(&pcf_isa_ops) < 0)
 
 275         dev_info(&pcf_isa_ops.dev, "found device at %#x\n", base);
 
 286                 ioport_unmap(base_iomem);
 
 287                 release_region(base, 2);
 
 290                 release_mem_region(base, 2);
 
 295 static void i2c_pcfisa_exit(void)
 
 297         i2c_pcf_del_bus(&pcf_isa_ops);
 
 305                 ioport_unmap(base_iomem);
 
 306                 release_region(base, 2);
 
 309                 release_mem_region(base, 2);
 
 313 MODULE_AUTHOR("Hans Berglund <hb@spacetec.no>");
 
 314 MODULE_DESCRIPTION("I2C-Bus adapter routines for PCF8584 ISA bus adapter");
 
 315 MODULE_LICENSE("GPL");
 
 317 module_param(base, int, 0);
 
 318 module_param(irq, int, 0);
 
 319 module_param(clock, int, 0);
 
 320 module_param(own, int, 0);
 
 321 module_param(mmapped, int, 0);
 
 323 module_init(i2c_pcfisa_init);
 
 324 module_exit(i2c_pcfisa_exit);