Merge branch 'master' of git://git.denx.de/u-boot-x86
[oweals/u-boot.git] / board / esd / cpci750 / i2c.c
index fcec78eac6678babf51879d2587ee61c6f78233c..b752903a146045b241b418b5403b60f1954652d5 100644 (file)
@@ -27,6 +27,7 @@
 #include <common.h>
 #include <mpc8xx.h>
 #include <malloc.h>
+#include <i2c.h>
 #include "../../Marvell/include/mv_gen_reg.h"
 #include "../../Marvell/include/core.h"
 
 
 /* Assuming that there is only one master on the bus (us) */
 
-static void i2c_init (int speed, int slaveaddr)
+void i2c_init (int speed, int slaveaddr)
 {
        unsigned int n, m, freq, margin, power;
        unsigned int actualN = 0, actualM = 0;
        unsigned int minMargin = 0xffffffff;
-       unsigned int tclk = CFG_TCLK;
+       unsigned int tclk = CONFIG_SYS_TCLK;
        unsigned int i2cFreq = speed;   /* 100000 max. Fast mode not supported */
 
        DP (puts ("i2c_init\n"));
@@ -72,28 +73,28 @@ static void i2c_init (int speed, int slaveaddr)
        /* Setup bus */
        /* gtI2cReset */
        GT_REG_WRITE (I2C_SOFT_RESET, 0);
-        asm(" sync");
+       asm(" sync");
        GT_REG_WRITE (I2C_CONTROL, 0);
-        asm(" sync");
+       asm(" sync");
 
        DP (puts ("set baudrate\n"));
 
        GT_REG_WRITE (I2C_STATUS_BAUDE_RATE, (actualM << 3) | actualN);
-        asm(" sync");
+       asm(" sync");
 
        DP (puts ("udelay...\n"));
 
        udelay (I2C_DELAY);
 
        GT_REG_WRITE (I2C_CONTROL, (0x1 << 2) | (0x1 << 6));
-        asm(" sync");
+       asm(" sync");
 }
 
 
 static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
 {
        unsigned int status, data, bits = 7;
-        unsigned int control;
+       unsigned int control;
        int count = 0;
 
        DP (puts ("i2c_select_device\n"));
@@ -107,19 +108,19 @@ static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
        GT_REG_READ (I2C_CONTROL, &control);
        control |=  (0x1 << 2);
        GT_REG_WRITE (I2C_CONTROL, control);
-        asm(" sync");
+       asm(" sync");
 
        GT_REG_READ (I2C_CONTROL, &control);
        control |= (0x1 << 5);  /* generate the I2C_START_BIT */
        GT_REG_WRITE (I2C_CONTROL, control);
-        asm(" sync");
+       asm(" sync");
        RESET_REG_BITS (I2C_CONTROL, (0x01 << 3));
-        asm(" sync");
+       asm(" sync");
 
        GT_REG_READ (I2C_CONTROL, &status);
        while ((status & 0x08) != 0x08) {
-               GT_REG_READ (I2C_CONTROL, &status);
-               }
+               GT_REG_READ (I2C_CONTROL, &status);
+               }
 
 
        count = 0;
@@ -128,7 +129,7 @@ static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
        while (((status & 0xff) != 0x08) && ((status & 0xff) != 0x10)){
                if (count > 200) {
 #ifdef DEBUG_I2C
-                       printf ("Failed to set startbit: 0x%02x\n", status);
+                       printf ("Failed to set startbit: 0x%02x\n", status);
 #endif
                        GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
                        asm(" sync");
@@ -146,21 +147,21 @@ static uchar i2c_select_device (uchar dev_addr, uchar read, int ten_bit)
        /* set the read bit */
        data |= read;
        GT_REG_WRITE (I2C_DATA, data);
-        asm(" sync");
+       asm(" sync");
        RESET_REG_BITS (I2C_CONTROL, BIT3);
-        asm(" sync");
+       asm(" sync");
 
        GT_REG_READ (I2C_CONTROL, &status);
        while ((status & 0x08) != 0x08) {
-               GT_REG_READ (I2C_CONTROL, &status);
-               }
+               GT_REG_READ (I2C_CONTROL, &status);
+               }
 
        GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
        count = 0;
        while (((status & 0xff) != 0x40) && ((status & 0xff) != 0x18)) {
                if (count > 200) {
 #ifdef DEBUG_I2C
-                       printf ("Failed to write address: 0x%02x\n", status);
+                       printf ("Failed to write address: 0x%02x\n", status);
 #endif
                        GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
                        return (status);
@@ -195,15 +196,15 @@ static uchar i2c_get_data (uchar * return_data, int len)
 
                GT_REG_READ (I2C_CONTROL, &status);
                while ((status & 0x08) != 0x08) {
-                       GT_REG_READ (I2C_CONTROL, &status);
-                       }
+                       GT_REG_READ (I2C_CONTROL, &status);
+                       }
 
                GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
                count++;
                while ((status & 0xff) != 0x50) {
                        if (count > 20) {
 #ifdef DEBUG_I2C
-                               printf ("Failed to get data len status: 0x%02x\n", status);
+                               printf ("Failed to get data len status: 0x%02x\n", status);
 #endif
                                GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /*stop */
                                asm(" sync");
@@ -219,13 +220,13 @@ static uchar i2c_get_data (uchar * return_data, int len)
 
        }
        RESET_REG_BITS (I2C_CONTROL, BIT2 | BIT3);
-        asm(" sync");
+       asm(" sync");
        count = 0;
 
        GT_REG_READ (I2C_CONTROL, &status);
        while ((status & 0x08) != 0x08) {
-               GT_REG_READ (I2C_CONTROL, &status);
-               }
+               GT_REG_READ (I2C_CONTROL, &status);
+               }
 
        while ((status & 0xff) != 0x58) {
                if (count > 2000) {
@@ -236,9 +237,9 @@ static uchar i2c_get_data (uchar * return_data, int len)
                count++;
        }
        GT_REG_WRITE (I2C_CONTROL, (0x1 << 4)); /* stop */
-        asm(" sync");
+       asm(" sync");
        RESET_REG_BITS (I2C_CONTROL, (0x1 << 3));
-        asm(" sync");
+       asm(" sync");
 
        return (0);
 }
@@ -254,7 +255,7 @@ static uchar i2c_write_data (unsigned int *data, int len)
        DP (puts ("i2c_write_data\n"));
 
        while (len) {
-               count = 0;
+               count = 0;
                temp = (unsigned int) (*temp_ptr);
                GT_REG_WRITE (I2C_DATA, temp);
                asm(" sync");
@@ -264,7 +265,7 @@ static uchar i2c_write_data (unsigned int *data, int len)
                GT_REG_READ (I2C_CONTROL, &status);
                while ((status & 0x08) != 0x08) {
                        GT_REG_READ (I2C_CONTROL, &status);
-                       }
+                       }
 
                GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
                count++;
@@ -294,7 +295,7 @@ static uchar i2c_write_byte (unsigned char *data, int len)
        DP (puts ("i2c_write_byte\n"));
 
        while (len) {
-               count = 0;
+               count = 0;
                /* Set and assert the data */
                temp = *temp_ptr;
                GT_REG_WRITE (I2C_DATA, temp);
@@ -306,7 +307,7 @@ static uchar i2c_write_byte (unsigned char *data, int len)
                GT_REG_READ (I2C_CONTROL, &status);
                while ((status & 0x08) != 0x08) {
                        GT_REG_READ (I2C_CONTROL, &status);
-                       }
+                       }
 
                GT_REG_READ (I2C_STATUS_BAUDE_RATE, &status);
                count++;
@@ -375,16 +376,17 @@ i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit,
        return 0;               /* sucessful completion */
 }
 
-uchar
+int
 i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data,
          int len)
 {
        uchar status = 0;
-       unsigned int i2cFreq = CFG_I2C_SPEED;
+       unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
 
        DP (puts ("i2c_read\n"));
 
-       i2c_init (i2cFreq, 0);  /* set the i2c frequency */
+       /* set the i2c frequency */
+       i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
 
        status = i2c_set_dev_offset (dev_addr, offset, 0, alen);        /* send the slave address + offset */
        if (status) {
@@ -419,20 +421,21 @@ i2c_read (uchar dev_addr, unsigned int offset, int alen, uchar * data,
 void i2c_stop (void)
 {
        GT_REG_WRITE (I2C_CONTROL, (0x1 << 4));
-        asm(" sync");
+       asm(" sync");
 }
 
 
-uchar
+int
 i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
           int len)
 {
        uchar status = 0;
-       unsigned int i2cFreq = CFG_I2C_SPEED;
+       unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
 
        DP (puts ("i2c_write\n"));
 
-       i2c_init (i2cFreq, 0);  /* set the i2c frequency */
+       /* set the i2c frequency */
+       i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
 
        status = i2c_set_dev_offset (dev_addr, offset, 0, alen);        /* send the slave address + offset */
        if (status) {
@@ -441,7 +444,7 @@ i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
                        status);
 #endif
                return status;
-               }
+               }
 
 
        status = i2c_write_byte (data, len);    /* write the data */
@@ -450,7 +453,7 @@ i2c_write (uchar dev_addr, unsigned int offset, int alen, uchar * data,
                printf ("Data not written: 0x%02x\n", status);
 #endif
                return status;
-               }
+               }
        /* issue a stop bit */
        i2c_stop ();
        return 0;
@@ -464,11 +467,12 @@ int i2c_probe (uchar chip)
        unsigned int i2c_status;
 #endif
        uchar status = 0;
-       unsigned int i2cFreq = CFG_I2C_SPEED;
+       unsigned int i2cFreq = CONFIG_SYS_I2C_SPEED;
 
        DP (puts ("i2c_probe\n"));
 
-       i2c_init (i2cFreq, 0);  /* set the i2c frequency */
+       /* set the i2c frequency */
+       i2c_init (i2cFreq, CONFIG_SYS_I2C_SLAVE);
 
        status = i2c_set_dev_offset (chip, 0, 0, 0);    /* send the slave address + no offset */
        if (status) {