Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* bbc_i2c.c: I2C low-level driver for BBC device on UltraSPARC-III
0003  *            platforms.
0004  *
0005  * Copyright (C) 2001, 2008 David S. Miller (davem@davemloft.net)
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/types.h>
0011 #include <linux/slab.h>
0012 #include <linux/sched.h>
0013 #include <linux/wait.h>
0014 #include <linux/delay.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 #include <asm/bbc.h>
0019 #include <asm/io.h>
0020 
0021 #include "bbc_i2c.h"
0022 
0023 /* Convert this driver to use i2c bus layer someday... */
0024 #define I2C_PCF_PIN 0x80
0025 #define I2C_PCF_ESO 0x40
0026 #define I2C_PCF_ES1 0x20
0027 #define I2C_PCF_ES2 0x10
0028 #define I2C_PCF_ENI 0x08
0029 #define I2C_PCF_STA 0x04
0030 #define I2C_PCF_STO 0x02
0031 #define I2C_PCF_ACK 0x01
0032 
0033 #define I2C_PCF_START    (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_ENI | I2C_PCF_STA | I2C_PCF_ACK)
0034 #define I2C_PCF_STOP     (I2C_PCF_PIN | I2C_PCF_ESO | I2C_PCF_STO | I2C_PCF_ACK)
0035 #define I2C_PCF_REPSTART (              I2C_PCF_ESO | I2C_PCF_STA | I2C_PCF_ACK)
0036 #define I2C_PCF_IDLE     (I2C_PCF_PIN | I2C_PCF_ESO               | I2C_PCF_ACK)
0037 
0038 #define I2C_PCF_INI 0x40   /* 1 if not initialized */
0039 #define I2C_PCF_STS 0x20
0040 #define I2C_PCF_BER 0x10
0041 #define I2C_PCF_AD0 0x08
0042 #define I2C_PCF_LRB 0x08
0043 #define I2C_PCF_AAS 0x04
0044 #define I2C_PCF_LAB 0x02
0045 #define I2C_PCF_BB  0x01
0046 
0047 /* The BBC devices have two I2C controllers.  The first I2C controller
0048  * connects mainly to configuration proms (NVRAM, cpu configuration,
0049  * dimm types, etc.).  Whereas the second I2C controller connects to
0050  * environmental control devices such as fans and temperature sensors.
0051  * The second controller also connects to the smartcard reader, if present.
0052  */
0053 
0054 static void set_device_claimage(struct bbc_i2c_bus *bp, struct platform_device *op, int val)
0055 {
0056     int i;
0057 
0058     for (i = 0; i < NUM_CHILDREN; i++) {
0059         if (bp->devs[i].device == op) {
0060             bp->devs[i].client_claimed = val;
0061             return;
0062         }
0063     }
0064 }
0065 
0066 #define claim_device(BP,ECHILD)     set_device_claimage(BP,ECHILD,1)
0067 #define release_device(BP,ECHILD)   set_device_claimage(BP,ECHILD,0)
0068 
0069 struct platform_device *bbc_i2c_getdev(struct bbc_i2c_bus *bp, int index)
0070 {
0071     struct platform_device *op = NULL;
0072     int curidx = 0, i;
0073 
0074     for (i = 0; i < NUM_CHILDREN; i++) {
0075         if (!(op = bp->devs[i].device))
0076             break;
0077         if (curidx == index)
0078             goto out;
0079         op = NULL;
0080         curidx++;
0081     }
0082 
0083 out:
0084     if (curidx == index)
0085         return op;
0086     return NULL;
0087 }
0088 
0089 struct bbc_i2c_client *bbc_i2c_attach(struct bbc_i2c_bus *bp, struct platform_device *op)
0090 {
0091     struct bbc_i2c_client *client;
0092     const u32 *reg;
0093 
0094     client = kzalloc(sizeof(*client), GFP_KERNEL);
0095     if (!client)
0096         return NULL;
0097     client->bp = bp;
0098     client->op = op;
0099 
0100     reg = of_get_property(op->dev.of_node, "reg", NULL);
0101     if (!reg) {
0102         kfree(client);
0103         return NULL;
0104     }
0105 
0106     client->bus = reg[0];
0107     client->address = reg[1];
0108 
0109     claim_device(bp, op);
0110 
0111     return client;
0112 }
0113 
0114 void bbc_i2c_detach(struct bbc_i2c_client *client)
0115 {
0116     struct bbc_i2c_bus *bp = client->bp;
0117     struct platform_device *op = client->op;
0118 
0119     release_device(bp, op);
0120     kfree(client);
0121 }
0122 
0123 static int wait_for_pin(struct bbc_i2c_bus *bp, u8 *status)
0124 {
0125     DECLARE_WAITQUEUE(wait, current);
0126     int limit = 32;
0127     int ret = 1;
0128 
0129     bp->waiting = 1;
0130     add_wait_queue(&bp->wq, &wait);
0131     while (limit-- > 0) {
0132         long val;
0133 
0134         val = wait_event_interruptible_timeout(
0135                 bp->wq,
0136                 (((*status = readb(bp->i2c_control_regs + 0))
0137                   & I2C_PCF_PIN) == 0),
0138                 msecs_to_jiffies(250));
0139         if (val > 0) {
0140             ret = 0;
0141             break;
0142         }
0143     }
0144     remove_wait_queue(&bp->wq, &wait);
0145     bp->waiting = 0;
0146 
0147     return ret;
0148 }
0149 
0150 int bbc_i2c_writeb(struct bbc_i2c_client *client, unsigned char val, int off)
0151 {
0152     struct bbc_i2c_bus *bp = client->bp;
0153     int address = client->address;
0154     u8 status;
0155     int ret = -1;
0156 
0157     if (bp->i2c_bussel_reg != NULL)
0158         writeb(client->bus, bp->i2c_bussel_reg);
0159 
0160     writeb(address, bp->i2c_control_regs + 0x1);
0161     writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
0162     if (wait_for_pin(bp, &status))
0163         goto out;
0164 
0165     writeb(off, bp->i2c_control_regs + 0x1);
0166     if (wait_for_pin(bp, &status) ||
0167         (status & I2C_PCF_LRB) != 0)
0168         goto out;
0169 
0170     writeb(val, bp->i2c_control_regs + 0x1);
0171     if (wait_for_pin(bp, &status))
0172         goto out;
0173 
0174     ret = 0;
0175 
0176 out:
0177     writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
0178     return ret;
0179 }
0180 
0181 int bbc_i2c_readb(struct bbc_i2c_client *client, unsigned char *byte, int off)
0182 {
0183     struct bbc_i2c_bus *bp = client->bp;
0184     unsigned char address = client->address, status;
0185     int ret = -1;
0186 
0187     if (bp->i2c_bussel_reg != NULL)
0188         writeb(client->bus, bp->i2c_bussel_reg);
0189 
0190     writeb(address, bp->i2c_control_regs + 0x1);
0191     writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
0192     if (wait_for_pin(bp, &status))
0193         goto out;
0194 
0195     writeb(off, bp->i2c_control_regs + 0x1);
0196     if (wait_for_pin(bp, &status) ||
0197         (status & I2C_PCF_LRB) != 0)
0198         goto out;
0199 
0200     writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
0201 
0202     address |= 0x1; /* READ */
0203 
0204     writeb(address, bp->i2c_control_regs + 0x1);
0205     writeb(I2C_PCF_START, bp->i2c_control_regs + 0x0);
0206     if (wait_for_pin(bp, &status))
0207         goto out;
0208 
0209     /* Set PIN back to one so the device sends the first
0210      * byte.
0211      */
0212     (void) readb(bp->i2c_control_regs + 0x1);
0213     if (wait_for_pin(bp, &status))
0214         goto out;
0215 
0216     writeb(I2C_PCF_ESO | I2C_PCF_ENI, bp->i2c_control_regs + 0x0);
0217     *byte = readb(bp->i2c_control_regs + 0x1);
0218     if (wait_for_pin(bp, &status))
0219         goto out;
0220 
0221     ret = 0;
0222 
0223 out:
0224     writeb(I2C_PCF_STOP, bp->i2c_control_regs + 0x0);
0225     (void) readb(bp->i2c_control_regs + 0x1);
0226 
0227     return ret;
0228 }
0229 
0230 int bbc_i2c_write_buf(struct bbc_i2c_client *client,
0231               char *buf, int len, int off)
0232 {
0233     int ret = 0;
0234 
0235     while (len > 0) {
0236         ret = bbc_i2c_writeb(client, *buf, off);
0237         if (ret < 0)
0238             break;
0239         len--;
0240         buf++;
0241         off++;
0242     }
0243     return ret;
0244 }
0245 
0246 int bbc_i2c_read_buf(struct bbc_i2c_client *client,
0247              char *buf, int len, int off)
0248 {
0249     int ret = 0;
0250 
0251     while (len > 0) {
0252         ret = bbc_i2c_readb(client, buf, off);
0253         if (ret < 0)
0254             break;
0255         len--;
0256         buf++;
0257         off++;
0258     }
0259 
0260     return ret;
0261 }
0262 
0263 EXPORT_SYMBOL(bbc_i2c_getdev);
0264 EXPORT_SYMBOL(bbc_i2c_attach);
0265 EXPORT_SYMBOL(bbc_i2c_detach);
0266 EXPORT_SYMBOL(bbc_i2c_writeb);
0267 EXPORT_SYMBOL(bbc_i2c_readb);
0268 EXPORT_SYMBOL(bbc_i2c_write_buf);
0269 EXPORT_SYMBOL(bbc_i2c_read_buf);
0270 
0271 static irqreturn_t bbc_i2c_interrupt(int irq, void *dev_id)
0272 {
0273     struct bbc_i2c_bus *bp = dev_id;
0274 
0275     /* PIN going from set to clear is the only event which
0276      * makes the i2c assert an interrupt.
0277      */
0278     if (bp->waiting &&
0279         !(readb(bp->i2c_control_regs + 0x0) & I2C_PCF_PIN))
0280         wake_up_interruptible(&bp->wq);
0281 
0282     return IRQ_HANDLED;
0283 }
0284 
0285 static void reset_one_i2c(struct bbc_i2c_bus *bp)
0286 {
0287     writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
0288     writeb(bp->own, bp->i2c_control_regs + 0x1);
0289     writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
0290     writeb(bp->clock, bp->i2c_control_regs + 0x1);
0291     writeb(I2C_PCF_IDLE, bp->i2c_control_regs + 0x0);
0292 }
0293 
0294 static struct bbc_i2c_bus * attach_one_i2c(struct platform_device *op, int index)
0295 {
0296     struct bbc_i2c_bus *bp;
0297     struct device_node *dp;
0298     int entry;
0299 
0300     bp = kzalloc(sizeof(*bp), GFP_KERNEL);
0301     if (!bp)
0302         return NULL;
0303 
0304     INIT_LIST_HEAD(&bp->temps);
0305     INIT_LIST_HEAD(&bp->fans);
0306 
0307     bp->i2c_control_regs = of_ioremap(&op->resource[0], 0, 0x2, "bbc_i2c_regs");
0308     if (!bp->i2c_control_regs)
0309         goto fail;
0310 
0311     if (op->num_resources == 2) {
0312         bp->i2c_bussel_reg = of_ioremap(&op->resource[1], 0, 0x1, "bbc_i2c_bussel");
0313         if (!bp->i2c_bussel_reg)
0314             goto fail;
0315     }
0316 
0317     bp->waiting = 0;
0318     init_waitqueue_head(&bp->wq);
0319     if (request_irq(op->archdata.irqs[0], bbc_i2c_interrupt,
0320             IRQF_SHARED, "bbc_i2c", bp))
0321         goto fail;
0322 
0323     bp->index = index;
0324     bp->op = op;
0325 
0326     spin_lock_init(&bp->lock);
0327 
0328     entry = 0;
0329     for (dp = op->dev.of_node->child;
0330          dp && entry < 8;
0331          dp = dp->sibling, entry++) {
0332         struct platform_device *child_op;
0333 
0334         child_op = of_find_device_by_node(dp);
0335         bp->devs[entry].device = child_op;
0336         bp->devs[entry].client_claimed = 0;
0337     }
0338 
0339     writeb(I2C_PCF_PIN, bp->i2c_control_regs + 0x0);
0340     bp->own = readb(bp->i2c_control_regs + 0x01);
0341     writeb(I2C_PCF_PIN | I2C_PCF_ES1, bp->i2c_control_regs + 0x0);
0342     bp->clock = readb(bp->i2c_control_regs + 0x01);
0343 
0344     printk(KERN_INFO "i2c-%d: Regs at %p, %d devices, own %02x, clock %02x.\n",
0345            bp->index, bp->i2c_control_regs, entry, bp->own, bp->clock);
0346 
0347     reset_one_i2c(bp);
0348 
0349     return bp;
0350 
0351 fail:
0352     if (bp->i2c_bussel_reg)
0353         of_iounmap(&op->resource[1], bp->i2c_bussel_reg, 1);
0354     if (bp->i2c_control_regs)
0355         of_iounmap(&op->resource[0], bp->i2c_control_regs, 2);
0356     kfree(bp);
0357     return NULL;
0358 }
0359 
0360 extern int bbc_envctrl_init(struct bbc_i2c_bus *bp);
0361 extern void bbc_envctrl_cleanup(struct bbc_i2c_bus *bp);
0362 
0363 static int bbc_i2c_probe(struct platform_device *op)
0364 {
0365     struct bbc_i2c_bus *bp;
0366     int err, index = 0;
0367 
0368     bp = attach_one_i2c(op, index);
0369     if (!bp)
0370         return -EINVAL;
0371 
0372     err = bbc_envctrl_init(bp);
0373     if (err) {
0374         free_irq(op->archdata.irqs[0], bp);
0375         if (bp->i2c_bussel_reg)
0376             of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
0377         if (bp->i2c_control_regs)
0378             of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
0379         kfree(bp);
0380     } else {
0381         dev_set_drvdata(&op->dev, bp);
0382     }
0383 
0384     return err;
0385 }
0386 
0387 static int bbc_i2c_remove(struct platform_device *op)
0388 {
0389     struct bbc_i2c_bus *bp = dev_get_drvdata(&op->dev);
0390 
0391     bbc_envctrl_cleanup(bp);
0392 
0393     free_irq(op->archdata.irqs[0], bp);
0394 
0395     if (bp->i2c_bussel_reg)
0396         of_iounmap(&op->resource[0], bp->i2c_bussel_reg, 1);
0397     if (bp->i2c_control_regs)
0398         of_iounmap(&op->resource[1], bp->i2c_control_regs, 2);
0399 
0400     kfree(bp);
0401 
0402     return 0;
0403 }
0404 
0405 static const struct of_device_id bbc_i2c_match[] = {
0406     {
0407         .name = "i2c",
0408         .compatible = "SUNW,bbc-i2c",
0409     },
0410     {},
0411 };
0412 MODULE_DEVICE_TABLE(of, bbc_i2c_match);
0413 
0414 static struct platform_driver bbc_i2c_driver = {
0415     .driver = {
0416         .name = "bbc_i2c",
0417         .of_match_table = bbc_i2c_match,
0418     },
0419     .probe      = bbc_i2c_probe,
0420     .remove     = bbc_i2c_remove,
0421 };
0422 
0423 module_platform_driver(bbc_i2c_driver);
0424 
0425 MODULE_LICENSE("GPL");