linux/drivers/i2c/busses/i2c-cpm.c
<<
>>
Prefs
   1/*
   2 * Freescale CPM1/CPM2 I2C interface.
   3 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
   4 *
   5 * moved into proper i2c interface;
   6 * Brad Parker (brad@heeltoe.com)
   7 *
   8 * Parts from dbox2_i2c.c (cvs.tuxbox.org)
   9 * (C) 2000-2001 Felix Domke (tmbinc@gmx.net), Gillem (htoa@gmx.net)
  10 *
  11 * (C) 2007 Montavista Software, Inc.
  12 * Vitaly Bordug <vitb@kernel.crashing.org>
  13 *
  14 * Converted to of_platform_device. Renamed to i2c-cpm.c.
  15 * (C) 2007,2008 Jochen Friedrich <jochen@scram.de>
  16 *
  17 *  This program is free software; you can redistribute it and/or modify
  18 *  it under the terms of the GNU General Public License as published by
  19 *  the Free Software Foundation; either version 2 of the License, or
  20 *  (at your option) any later version.
  21 *
  22 *  This program is distributed in the hope that it will be useful,
  23 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  24 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25 *  GNU General Public License for more details.
  26 */
  27
  28#include <linux/kernel.h>
  29#include <linux/module.h>
  30#include <linux/delay.h>
  31#include <linux/slab.h>
  32#include <linux/init.h>
  33#include <linux/interrupt.h>
  34#include <linux/errno.h>
  35#include <linux/stddef.h>
  36#include <linux/i2c.h>
  37#include <linux/io.h>
  38#include <linux/dma-mapping.h>
  39#include <linux/of_device.h>
  40#include <linux/of_platform.h>
  41#include <sysdev/fsl_soc.h>
  42#include <asm/cpm.h>
  43
  44/* Try to define this if you have an older CPU (earlier than rev D4) */
  45/* However, better use a GPIO based bitbang driver in this case :/   */
  46#undef  I2C_CHIP_ERRATA
  47
  48#define CPM_MAX_READ    513
  49#define CPM_MAXBD       4
  50
  51#define I2C_EB                  (0x10) /* Big endian mode */
  52#define I2C_EB_CPM2             (0x30) /* Big endian mode, memory snoop */
  53
  54#define DPRAM_BASE              ((u8 __iomem __force *)cpm_muram_addr(0))
  55
  56/* I2C parameter RAM. */
  57struct i2c_ram {
  58        ushort  rbase;          /* Rx Buffer descriptor base address */
  59        ushort  tbase;          /* Tx Buffer descriptor base address */
  60        u_char  rfcr;           /* Rx function code */
  61        u_char  tfcr;           /* Tx function code */
  62        ushort  mrblr;          /* Max receive buffer length */
  63        uint    rstate;         /* Internal */
  64        uint    rdp;            /* Internal */
  65        ushort  rbptr;          /* Rx Buffer descriptor pointer */
  66        ushort  rbc;            /* Internal */
  67        uint    rxtmp;          /* Internal */
  68        uint    tstate;         /* Internal */
  69        uint    tdp;            /* Internal */
  70        ushort  tbptr;          /* Tx Buffer descriptor pointer */
  71        ushort  tbc;            /* Internal */
  72        uint    txtmp;          /* Internal */
  73        char    res1[4];        /* Reserved */
  74        ushort  rpbase;         /* Relocation pointer */
  75        char    res2[2];        /* Reserved */
  76};
  77
  78#define I2COM_START     0x80
  79#define I2COM_MASTER    0x01
  80#define I2CER_TXE       0x10
  81#define I2CER_BUSY      0x04
  82#define I2CER_TXB       0x02
  83#define I2CER_RXB       0x01
  84#define I2MOD_EN        0x01
  85
  86/* I2C Registers */
  87struct i2c_reg {
  88        u8      i2mod;
  89        u8      res1[3];
  90        u8      i2add;
  91        u8      res2[3];
  92        u8      i2brg;
  93        u8      res3[3];
  94        u8      i2com;
  95        u8      res4[3];
  96        u8      i2cer;
  97        u8      res5[3];
  98        u8      i2cmr;
  99};
 100
 101struct cpm_i2c {
 102        char *base;
 103        struct platform_device *ofdev;
 104        struct i2c_adapter adap;
 105        uint dp_addr;
 106        int version; /* CPM1=1, CPM2=2 */
 107        int irq;
 108        int cp_command;
 109        int freq;
 110        struct i2c_reg __iomem *i2c_reg;
 111        struct i2c_ram __iomem *i2c_ram;
 112        u16 i2c_addr;
 113        wait_queue_head_t i2c_wait;
 114        cbd_t __iomem *tbase;
 115        cbd_t __iomem *rbase;
 116        u_char *txbuf[CPM_MAXBD];
 117        u_char *rxbuf[CPM_MAXBD];
 118        u32 txdma[CPM_MAXBD];
 119        u32 rxdma[CPM_MAXBD];
 120};
 121
 122static irqreturn_t cpm_i2c_interrupt(int irq, void *dev_id)
 123{
 124        struct cpm_i2c *cpm;
 125        struct i2c_reg __iomem *i2c_reg;
 126        struct i2c_adapter *adap = dev_id;
 127        int i;
 128
 129        cpm = i2c_get_adapdata(dev_id);
 130        i2c_reg = cpm->i2c_reg;
 131
 132        /* Clear interrupt. */
 133        i = in_8(&i2c_reg->i2cer);
 134        out_8(&i2c_reg->i2cer, i);
 135
 136        dev_dbg(&adap->dev, "Interrupt: %x\n", i);
 137
 138        wake_up(&cpm->i2c_wait);
 139
 140        return i ? IRQ_HANDLED : IRQ_NONE;
 141}
 142
 143static void cpm_reset_i2c_params(struct cpm_i2c *cpm)
 144{
 145        struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
 146
 147        /* Set up the I2C parameters in the parameter ram. */
 148        out_be16(&i2c_ram->tbase, (u8 __iomem *)cpm->tbase - DPRAM_BASE);
 149        out_be16(&i2c_ram->rbase, (u8 __iomem *)cpm->rbase - DPRAM_BASE);
 150
 151        if (cpm->version == 1) {
 152                out_8(&i2c_ram->tfcr, I2C_EB);
 153                out_8(&i2c_ram->rfcr, I2C_EB);
 154        } else {
 155                out_8(&i2c_ram->tfcr, I2C_EB_CPM2);
 156                out_8(&i2c_ram->rfcr, I2C_EB_CPM2);
 157        }
 158
 159        out_be16(&i2c_ram->mrblr, CPM_MAX_READ);
 160
 161        out_be32(&i2c_ram->rstate, 0);
 162        out_be32(&i2c_ram->rdp, 0);
 163        out_be16(&i2c_ram->rbptr, 0);
 164        out_be16(&i2c_ram->rbc, 0);
 165        out_be32(&i2c_ram->rxtmp, 0);
 166        out_be32(&i2c_ram->tstate, 0);
 167        out_be32(&i2c_ram->tdp, 0);
 168        out_be16(&i2c_ram->tbptr, 0);
 169        out_be16(&i2c_ram->tbc, 0);
 170        out_be32(&i2c_ram->txtmp, 0);
 171}
 172
 173static void cpm_i2c_force_close(struct i2c_adapter *adap)
 174{
 175        struct cpm_i2c *cpm = i2c_get_adapdata(adap);
 176        struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
 177
 178        dev_dbg(&adap->dev, "cpm_i2c_force_close()\n");
 179
 180        cpm_command(cpm->cp_command, CPM_CR_CLOSE_RX_BD);
 181
 182        out_8(&i2c_reg->i2cmr, 0x00);   /* Disable all interrupts */
 183        out_8(&i2c_reg->i2cer, 0xff);
 184}
 185
 186static void cpm_i2c_parse_message(struct i2c_adapter *adap,
 187        struct i2c_msg *pmsg, int num, int tx, int rx)
 188{
 189        cbd_t __iomem *tbdf;
 190        cbd_t __iomem *rbdf;
 191        u_char addr;
 192        u_char *tb;
 193        u_char *rb;
 194        struct cpm_i2c *cpm = i2c_get_adapdata(adap);
 195
 196        tbdf = cpm->tbase + tx;
 197        rbdf = cpm->rbase + rx;
 198
 199        addr = pmsg->addr << 1;
 200        if (pmsg->flags & I2C_M_RD)
 201                addr |= 1;
 202
 203        tb = cpm->txbuf[tx];
 204        rb = cpm->rxbuf[rx];
 205
 206        /* Align read buffer */
 207        rb = (u_char *) (((ulong) rb + 1) & ~1);
 208
 209        tb[0] = addr;           /* Device address byte w/rw flag */
 210
 211        out_be16(&tbdf->cbd_datlen, pmsg->len + 1);
 212        out_be16(&tbdf->cbd_sc, 0);
 213
 214        if (!(pmsg->flags & I2C_M_NOSTART))
 215                setbits16(&tbdf->cbd_sc, BD_I2C_START);
 216
 217        if (tx + 1 == num)
 218                setbits16(&tbdf->cbd_sc, BD_SC_LAST | BD_SC_WRAP);
 219
 220        if (pmsg->flags & I2C_M_RD) {
 221                /*
 222                 * To read, we need an empty buffer of the proper length.
 223                 * All that is used is the first byte for address, the remainder
 224                 * is just used for timing (and doesn't really have to exist).
 225                 */
 226
 227                dev_dbg(&adap->dev, "cpm_i2c_read(abyte=0x%x)\n", addr);
 228
 229                out_be16(&rbdf->cbd_datlen, 0);
 230                out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
 231
 232                if (rx + 1 == CPM_MAXBD)
 233                        setbits16(&rbdf->cbd_sc, BD_SC_WRAP);
 234
 235                eieio();
 236                setbits16(&tbdf->cbd_sc, BD_SC_READY);
 237        } else {
 238                dev_dbg(&adap->dev, "cpm_i2c_write(abyte=0x%x)\n", addr);
 239
 240                memcpy(tb+1, pmsg->buf, pmsg->len);
 241
 242                eieio();
 243                setbits16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_INTRPT);
 244        }
 245}
 246
 247static int cpm_i2c_check_message(struct i2c_adapter *adap,
 248        struct i2c_msg *pmsg, int tx, int rx)
 249{
 250        cbd_t __iomem *tbdf;
 251        cbd_t __iomem *rbdf;
 252        u_char *tb;
 253        u_char *rb;
 254        struct cpm_i2c *cpm = i2c_get_adapdata(adap);
 255
 256        tbdf = cpm->tbase + tx;
 257        rbdf = cpm->rbase + rx;
 258
 259        tb = cpm->txbuf[tx];
 260        rb = cpm->rxbuf[rx];
 261
 262        /* Align read buffer */
 263        rb = (u_char *) (((uint) rb + 1) & ~1);
 264
 265        eieio();
 266        if (pmsg->flags & I2C_M_RD) {
 267                dev_dbg(&adap->dev, "tx sc 0x%04x, rx sc 0x%04x\n",
 268                        in_be16(&tbdf->cbd_sc), in_be16(&rbdf->cbd_sc));
 269
 270                if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
 271                        dev_dbg(&adap->dev, "I2C read; No ack\n");
 272                        return -ENXIO;
 273                }
 274                if (in_be16(&rbdf->cbd_sc) & BD_SC_EMPTY) {
 275                        dev_err(&adap->dev,
 276                                "I2C read; complete but rbuf empty\n");
 277                        return -EREMOTEIO;
 278                }
 279                if (in_be16(&rbdf->cbd_sc) & BD_SC_OV) {
 280                        dev_err(&adap->dev, "I2C read; Overrun\n");
 281                        return -EREMOTEIO;
 282                }
 283                memcpy(pmsg->buf, rb, pmsg->len);
 284        } else {
 285                dev_dbg(&adap->dev, "tx sc %d 0x%04x\n", tx,
 286                        in_be16(&tbdf->cbd_sc));
 287
 288                if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
 289                        dev_dbg(&adap->dev, "I2C write; No ack\n");
 290                        return -ENXIO;
 291                }
 292                if (in_be16(&tbdf->cbd_sc) & BD_SC_UN) {
 293                        dev_err(&adap->dev, "I2C write; Underrun\n");
 294                        return -EIO;
 295                }
 296                if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
 297                        dev_err(&adap->dev, "I2C write; Collision\n");
 298                        return -EIO;
 299                }
 300        }
 301        return 0;
 302}
 303
 304static int cpm_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 305{
 306        struct cpm_i2c *cpm = i2c_get_adapdata(adap);
 307        struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
 308        struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
 309        struct i2c_msg *pmsg;
 310        int ret, i;
 311        int tptr;
 312        int rptr;
 313        cbd_t __iomem *tbdf;
 314        cbd_t __iomem *rbdf;
 315
 316        if (num > CPM_MAXBD)
 317                return -EINVAL;
 318
 319        /* Check if we have any oversized READ requests */
 320        for (i = 0; i < num; i++) {
 321                pmsg = &msgs[i];
 322                if (pmsg->len >= CPM_MAX_READ)
 323                        return -EINVAL;
 324        }
 325
 326        /* Reset to use first buffer */
 327        out_be16(&i2c_ram->rbptr, in_be16(&i2c_ram->rbase));
 328        out_be16(&i2c_ram->tbptr, in_be16(&i2c_ram->tbase));
 329
 330        tbdf = cpm->tbase;
 331        rbdf = cpm->rbase;
 332
 333        tptr = 0;
 334        rptr = 0;
 335
 336        while (tptr < num) {
 337                pmsg = &msgs[tptr];
 338                dev_dbg(&adap->dev, "R: %d T: %d\n", rptr, tptr);
 339
 340                cpm_i2c_parse_message(adap, pmsg, num, tptr, rptr);
 341                if (pmsg->flags & I2C_M_RD)
 342                        rptr++;
 343                tptr++;
 344        }
 345        /* Start transfer now */
 346        /* Enable RX/TX/Error interupts */
 347        out_8(&i2c_reg->i2cmr, I2CER_TXE | I2CER_TXB | I2CER_RXB);
 348        out_8(&i2c_reg->i2cer, 0xff);   /* Clear interrupt status */
 349        /* Chip bug, set enable here */
 350        setbits8(&i2c_reg->i2mod, I2MOD_EN);    /* Enable */
 351        /* Begin transmission */
 352        setbits8(&i2c_reg->i2com, I2COM_START);
 353
 354        tptr = 0;
 355        rptr = 0;
 356
 357        while (tptr < num) {
 358                /* Check for outstanding messages */
 359                dev_dbg(&adap->dev, "test ready.\n");
 360                pmsg = &msgs[tptr];
 361                if (pmsg->flags & I2C_M_RD)
 362                        ret = wait_event_timeout(cpm->i2c_wait,
 363                                (in_be16(&tbdf[tptr].cbd_sc) & BD_SC_NAK) ||
 364                                !(in_be16(&rbdf[rptr].cbd_sc) & BD_SC_EMPTY),
 365                                1 * HZ);
 366                else
 367                        ret = wait_event_timeout(cpm->i2c_wait,
 368                                !(in_be16(&tbdf[tptr].cbd_sc) & BD_SC_READY),
 369                                1 * HZ);
 370                if (ret == 0) {
 371                        ret = -EREMOTEIO;
 372                        dev_err(&adap->dev, "I2C transfer: timeout\n");
 373                        goto out_err;
 374                }
 375                if (ret > 0) {
 376                        dev_dbg(&adap->dev, "ready.\n");
 377                        ret = cpm_i2c_check_message(adap, pmsg, tptr, rptr);
 378                        tptr++;
 379                        if (pmsg->flags & I2C_M_RD)
 380                                rptr++;
 381                        if (ret)
 382                                goto out_err;
 383                }
 384        }
 385#ifdef I2C_CHIP_ERRATA
 386        /*
 387         * Chip errata, clear enable. This is not needed on rev D4 CPUs.
 388         * Disabling I2C too early may cause too short stop condition
 389         */
 390        udelay(4);
 391        clrbits8(&i2c_reg->i2mod, I2MOD_EN);
 392#endif
 393        return (num);
 394
 395out_err:
 396        cpm_i2c_force_close(adap);
 397#ifdef I2C_CHIP_ERRATA
 398        /*
 399         * Chip errata, clear enable. This is not needed on rev D4 CPUs.
 400         */
 401        clrbits8(&i2c_reg->i2mod, I2MOD_EN);
 402#endif
 403        return ret;
 404}
 405
 406static u32 cpm_i2c_func(struct i2c_adapter *adap)
 407{
 408        return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
 409}
 410
 411/* -----exported algorithm data: -------------------------------------  */
 412
 413static const struct i2c_algorithm cpm_i2c_algo = {
 414        .master_xfer = cpm_i2c_xfer,
 415        .functionality = cpm_i2c_func,
 416};
 417
 418static const struct i2c_adapter cpm_ops = {
 419        .owner          = THIS_MODULE,
 420        .name           = "i2c-cpm",
 421        .algo           = &cpm_i2c_algo,
 422};
 423
 424static int cpm_i2c_setup(struct cpm_i2c *cpm)
 425{
 426        struct platform_device *ofdev = cpm->ofdev;
 427        const u32 *data;
 428        int len, ret, i;
 429        void __iomem *i2c_base;
 430        cbd_t __iomem *tbdf;
 431        cbd_t __iomem *rbdf;
 432        unsigned char brg;
 433
 434        dev_dbg(&cpm->ofdev->dev, "cpm_i2c_setup()\n");
 435
 436        init_waitqueue_head(&cpm->i2c_wait);
 437
 438        cpm->irq = of_irq_to_resource(ofdev->dev.of_node, 0, NULL);
 439        if (!cpm->irq)
 440                return -EINVAL;
 441
 442        /* Install interrupt handler. */
 443        ret = request_irq(cpm->irq, cpm_i2c_interrupt, 0, "cpm_i2c",
 444                          &cpm->adap);
 445        if (ret)
 446                return ret;
 447
 448        /* I2C parameter RAM */
 449        i2c_base = of_iomap(ofdev->dev.of_node, 1);
 450        if (i2c_base == NULL) {
 451                ret = -EINVAL;
 452                goto out_irq;
 453        }
 454
 455        if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm1-i2c")) {
 456
 457                /* Check for and use a microcode relocation patch. */
 458                cpm->i2c_ram = i2c_base;
 459                cpm->i2c_addr = in_be16(&cpm->i2c_ram->rpbase);
 460
 461                /*
 462                 * Maybe should use cpm_muram_alloc instead of hardcoding
 463                 * this in micropatch.c
 464                 */
 465                if (cpm->i2c_addr) {
 466                        cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
 467                        iounmap(i2c_base);
 468                }
 469
 470                cpm->version = 1;
 471
 472        } else if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm2-i2c")) {
 473                cpm->i2c_addr = cpm_muram_alloc(sizeof(struct i2c_ram), 64);
 474                cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
 475                out_be16(i2c_base, cpm->i2c_addr);
 476                iounmap(i2c_base);
 477
 478                cpm->version = 2;
 479
 480        } else {
 481                iounmap(i2c_base);
 482                ret = -EINVAL;
 483                goto out_irq;
 484        }
 485
 486        /* I2C control/status registers */
 487        cpm->i2c_reg = of_iomap(ofdev->dev.of_node, 0);
 488        if (cpm->i2c_reg == NULL) {
 489                ret = -EINVAL;
 490                goto out_ram;
 491        }
 492
 493        data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
 494        if (!data || len != 4) {
 495                ret = -EINVAL;
 496                goto out_reg;
 497        }
 498        cpm->cp_command = *data;
 499
 500        data = of_get_property(ofdev->dev.of_node, "linux,i2c-class", &len);
 501        if (data && len == 4)
 502                cpm->adap.class = *data;
 503
 504        data = of_get_property(ofdev->dev.of_node, "clock-frequency", &len);
 505        if (data && len == 4)
 506                cpm->freq = *data;
 507        else
 508                cpm->freq = 60000; /* use 60kHz i2c clock by default */
 509
 510        /*
 511         * Allocate space for CPM_MAXBD transmit and receive buffer
 512         * descriptors in the DP ram.
 513         */
 514        cpm->dp_addr = cpm_muram_alloc(sizeof(cbd_t) * 2 * CPM_MAXBD, 8);
 515        if (!cpm->dp_addr) {
 516                ret = -ENOMEM;
 517                goto out_reg;
 518        }
 519
 520        cpm->tbase = cpm_muram_addr(cpm->dp_addr);
 521        cpm->rbase = cpm_muram_addr(cpm->dp_addr + sizeof(cbd_t) * CPM_MAXBD);
 522
 523        /* Allocate TX and RX buffers */
 524
 525        tbdf = cpm->tbase;
 526        rbdf = cpm->rbase;
 527
 528        for (i = 0; i < CPM_MAXBD; i++) {
 529                cpm->rxbuf[i] = dma_alloc_coherent(&cpm->ofdev->dev,
 530                                                   CPM_MAX_READ + 1,
 531                                                   &cpm->rxdma[i], GFP_KERNEL);
 532                if (!cpm->rxbuf[i]) {
 533                        ret = -ENOMEM;
 534                        goto out_muram;
 535                }
 536                out_be32(&rbdf[i].cbd_bufaddr, ((cpm->rxdma[i] + 1) & ~1));
 537
 538                cpm->txbuf[i] = (unsigned char *)dma_alloc_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1, &cpm->txdma[i], GFP_KERNEL);
 539                if (!cpm->txbuf[i]) {
 540                        ret = -ENOMEM;
 541                        goto out_muram;
 542                }
 543                out_be32(&tbdf[i].cbd_bufaddr, cpm->txdma[i]);
 544        }
 545
 546        /* Initialize Tx/Rx parameters. */
 547
 548        cpm_reset_i2c_params(cpm);
 549
 550        dev_dbg(&cpm->ofdev->dev, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
 551                cpm->i2c_ram, cpm->i2c_addr, cpm->freq);
 552        dev_dbg(&cpm->ofdev->dev, "tbase 0x%04x, rbase 0x%04x\n",
 553                (u8 __iomem *)cpm->tbase - DPRAM_BASE,
 554                (u8 __iomem *)cpm->rbase - DPRAM_BASE);
 555
 556        cpm_command(cpm->cp_command, CPM_CR_INIT_TRX);
 557
 558        /*
 559         * Select an invalid address. Just make sure we don't use loopback mode
 560         */
 561        out_8(&cpm->i2c_reg->i2add, 0x7f << 1);
 562
 563        /*
 564         * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
 565         * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
 566         * the actual i2c bus frequency.
 567         */
 568        brg = get_brgfreq() / (32 * 2 * cpm->freq) - 3;
 569        out_8(&cpm->i2c_reg->i2brg, brg);
 570
 571        out_8(&cpm->i2c_reg->i2mod, 0x00);
 572        out_8(&cpm->i2c_reg->i2com, I2COM_MASTER);      /* Master mode */
 573
 574        /* Disable interrupts. */
 575        out_8(&cpm->i2c_reg->i2cmr, 0);
 576        out_8(&cpm->i2c_reg->i2cer, 0xff);
 577
 578        return 0;
 579
 580out_muram:
 581        for (i = 0; i < CPM_MAXBD; i++) {
 582                if (cpm->rxbuf[i])
 583                        dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
 584                                cpm->rxbuf[i], cpm->rxdma[i]);
 585                if (cpm->txbuf[i])
 586                        dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
 587                                cpm->txbuf[i], cpm->txdma[i]);
 588        }
 589        cpm_muram_free(cpm->dp_addr);
 590out_reg:
 591        iounmap(cpm->i2c_reg);
 592out_ram:
 593        if ((cpm->version == 1) && (!cpm->i2c_addr))
 594                iounmap(cpm->i2c_ram);
 595        if (cpm->version == 2)
 596                cpm_muram_free(cpm->i2c_addr);
 597out_irq:
 598        free_irq(cpm->irq, &cpm->adap);
 599        return ret;
 600}
 601
 602static void cpm_i2c_shutdown(struct cpm_i2c *cpm)
 603{
 604        int i;
 605
 606        /* Shut down I2C. */
 607        clrbits8(&cpm->i2c_reg->i2mod, I2MOD_EN);
 608
 609        /* Disable interrupts */
 610        out_8(&cpm->i2c_reg->i2cmr, 0);
 611        out_8(&cpm->i2c_reg->i2cer, 0xff);
 612
 613        free_irq(cpm->irq, &cpm->adap);
 614
 615        /* Free all memory */
 616        for (i = 0; i < CPM_MAXBD; i++) {
 617                dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
 618                        cpm->rxbuf[i], cpm->rxdma[i]);
 619                dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
 620                        cpm->txbuf[i], cpm->txdma[i]);
 621        }
 622
 623        cpm_muram_free(cpm->dp_addr);
 624        iounmap(cpm->i2c_reg);
 625
 626        if ((cpm->version == 1) && (!cpm->i2c_addr))
 627                iounmap(cpm->i2c_ram);
 628        if (cpm->version == 2)
 629                cpm_muram_free(cpm->i2c_addr);
 630}
 631
 632static int cpm_i2c_probe(struct platform_device *ofdev)
 633{
 634        int result, len;
 635        struct cpm_i2c *cpm;
 636        const u32 *data;
 637
 638        cpm = kzalloc(sizeof(struct cpm_i2c), GFP_KERNEL);
 639        if (!cpm)
 640                return -ENOMEM;
 641
 642        cpm->ofdev = ofdev;
 643
 644        dev_set_drvdata(&ofdev->dev, cpm);
 645
 646        cpm->adap = cpm_ops;
 647        i2c_set_adapdata(&cpm->adap, cpm);
 648        cpm->adap.dev.parent = &ofdev->dev;
 649        cpm->adap.dev.of_node = of_node_get(ofdev->dev.of_node);
 650
 651        result = cpm_i2c_setup(cpm);
 652        if (result) {
 653                dev_err(&ofdev->dev, "Unable to init hardware\n");
 654                goto out_free;
 655        }
 656
 657        /* register new adapter to i2c module... */
 658
 659        data = of_get_property(ofdev->dev.of_node, "linux,i2c-index", &len);
 660        cpm->adap.nr = (data && len == 4) ? be32_to_cpup(data) : -1;
 661        result = i2c_add_numbered_adapter(&cpm->adap);
 662
 663        if (result < 0) {
 664                dev_err(&ofdev->dev, "Unable to register with I2C\n");
 665                goto out_shut;
 666        }
 667
 668        dev_dbg(&ofdev->dev, "hw routines for %s registered.\n",
 669                cpm->adap.name);
 670
 671        return 0;
 672out_shut:
 673        cpm_i2c_shutdown(cpm);
 674out_free:
 675        kfree(cpm);
 676
 677        return result;
 678}
 679
 680static int cpm_i2c_remove(struct platform_device *ofdev)
 681{
 682        struct cpm_i2c *cpm = dev_get_drvdata(&ofdev->dev);
 683
 684        i2c_del_adapter(&cpm->adap);
 685
 686        cpm_i2c_shutdown(cpm);
 687
 688        kfree(cpm);
 689
 690        return 0;
 691}
 692
 693static const struct of_device_id cpm_i2c_match[] = {
 694        {
 695                .compatible = "fsl,cpm1-i2c",
 696        },
 697        {
 698                .compatible = "fsl,cpm2-i2c",
 699        },
 700        {},
 701};
 702
 703MODULE_DEVICE_TABLE(of, cpm_i2c_match);
 704
 705static struct platform_driver cpm_i2c_driver = {
 706        .probe          = cpm_i2c_probe,
 707        .remove         = cpm_i2c_remove,
 708        .driver = {
 709                .name = "fsl-i2c-cpm",
 710                .owner = THIS_MODULE,
 711                .of_match_table = cpm_i2c_match,
 712        },
 713};
 714
 715module_platform_driver(cpm_i2c_driver);
 716
 717MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
 718MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
 719MODULE_LICENSE("GPL");
 720