2 * Copyright (c) 2007 by Silicon Motion, Inc. (SMI)
4 * All rights are reserved. Reproduction or in part is prohibited
5 * without the written consent of the copyright owner.
7 * swi2c.c --- SM750/SM718 DDK
8 * This file contains the source code for I2C using software
12 #include "ddk750_chip.h"
13 #include "ddk750_reg.h"
14 #include "ddk750_swi2c.h"
15 #include "ddk750_power.h"
18 * I2C Software Master Driver:
19 * ===========================
20 * Each i2c cycle is split into 4 sections. Each of these section marks
21 * a point in time where the SCL or SDA may be changed.
23 * 1 Cycle == | Section I. | Section 2. | Section 3. | Section 4. |
24 * +-------------+-------------+-------------+-------------+
25 * | SCL set LOW |SCL no change| SCL set HIGH|SCL no change|
27 * ____________ _____________
28 * SCL == XXXX _____________ ____________ /
30 * I.e. the SCL may only be changed in section 1. and section 3. while
31 * the SDA may only be changed in section 2. and section 4. The table
32 * below gives the changes for these 2 lines in the varios sections.
34 * Section changes Table:
35 * ======================
36 * blank = no change, L = set bit LOW, H = set bit HIGH
39 * ---------------+---+---+---+---+
40 * Tx Start SDA | | H | | L |
42 * ---------------+---+---+---+---+
43 * Tx Stop SDA | | L | | H |
45 * ---------------+---+---+---+---+
46 * Tx bit H SDA | | H | | |
48 * ---------------+---+---+---+---+
49 * Tx bit L SDA | | L | | |
51 * ---------------+---+---+---+---+
55 /* GPIO pins used for this I2C. It ranges from 0 to 63. */
56 static unsigned char sw_i2c_clk_gpio = DEFAULT_I2C_SCL;
57 static unsigned char sw_i2c_data_gpio = DEFAULT_I2C_SDA;
60 * Below is the variable declaration for the GPIO pin register usage
61 * for the i2c Clock and i2c Data.
64 * Notice that the GPIO usage for the i2c clock and i2c Data are
65 * separated. This is to make this code flexible enough when
66 * two separate GPIO pins for the clock and data are located
67 * in two different GPIO register set (worst case).
70 /* i2c Clock GPIO Register usage */
71 static unsigned long sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
72 static unsigned long sw_i2c_clk_gpio_data_reg = GPIO_DATA;
73 static unsigned long sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
75 /* i2c Data GPIO Register usage */
76 static unsigned long sw_i2c_data_gpio_mux_reg = GPIO_MUX;
77 static unsigned long sw_i2c_data_gpio_data_reg = GPIO_DATA;
78 static unsigned long sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
81 * This function puts a delay between command
83 static void sw_i2c_wait(void)
86 * peekIO method works well before suspend/resume
87 * but after suspend, peekIO(0x3ce,0x61) & 0x10
88 * always be non-zero,which makes the while loop
90 * use non-ultimate for loop below is safe
93 /* Change wait algorithm to use PCI bus clock,
94 * it's more reliable than counter loop ..
95 * write 0x61 to 0x3ce and read from 0x3cf
99 for (i = 0; i < 600; i++) {
106 * This function set/reset the SCL GPIO pin
109 * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
112 * When setting SCL to high, just set the GPIO as input where the pull up
113 * resistor will pull the signal up. Do not use software to pull up the
114 * signal because the i2c will fail when other device try to drive the
115 * signal due to SM50x will drive the signal to always high.
117 static void sw_i2c_scl(unsigned char value)
119 unsigned long gpio_data;
120 unsigned long gpio_dir;
122 gpio_dir = peek32(sw_i2c_clk_gpio_data_dir_reg);
123 if (value) { /* High */
125 * Set direction as input. This will automatically
126 * pull the signal up.
128 gpio_dir &= ~(1 << sw_i2c_clk_gpio);
129 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
131 /* Set the signal down */
132 gpio_data = peek32(sw_i2c_clk_gpio_data_reg);
133 gpio_data &= ~(1 << sw_i2c_clk_gpio);
134 poke32(sw_i2c_clk_gpio_data_reg, gpio_data);
136 /* Set direction as output */
137 gpio_dir |= (1 << sw_i2c_clk_gpio);
138 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
143 * This function set/reset the SDA GPIO pin
146 * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
149 * When setting SCL to high, just set the GPIO as input where the pull up
150 * resistor will pull the signal up. Do not use software to pull up the
151 * signal because the i2c will fail when other device try to drive the
152 * signal due to SM50x will drive the signal to always high.
154 static void sw_i2c_sda(unsigned char value)
156 unsigned long gpio_data;
157 unsigned long gpio_dir;
159 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
160 if (value) { /* High */
162 * Set direction as input. This will automatically
163 * pull the signal up.
165 gpio_dir &= ~(1 << sw_i2c_data_gpio);
166 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
168 /* Set the signal down */
169 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
170 gpio_data &= ~(1 << sw_i2c_data_gpio);
171 poke32(sw_i2c_data_gpio_data_reg, gpio_data);
173 /* Set direction as output */
174 gpio_dir |= (1 << sw_i2c_data_gpio);
175 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
180 * This function read the data from the SDA GPIO pin
183 * The SDA data bit sent by the Slave
185 static unsigned char sw_i2c_read_sda(void)
187 unsigned long gpio_dir;
188 unsigned long gpio_data;
189 unsigned long dir_mask = 1 << sw_i2c_data_gpio;
191 /* Make sure that the direction is input (High) */
192 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
193 if ((gpio_dir & dir_mask) != ~dir_mask) {
194 gpio_dir &= ~(1 << sw_i2c_data_gpio);
195 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
198 /* Now read the SDA line */
199 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
200 if (gpio_data & (1 << sw_i2c_data_gpio))
207 * This function sends ACK signal
209 static void sw_i2c_ack(void)
211 return; /* Single byte read is ok without it. */
215 * This function sends the start command to the slave device
217 static void sw_i2c_start(void)
226 * This function sends the stop command to the slave device
228 static void sw_i2c_stop(void)
237 * This function writes one byte to the slave device
240 * data - Data to be write to the slave device
244 * -1 - Fail to write byte
246 static long sw_i2c_write_byte(unsigned char data)
248 unsigned char value = data;
251 /* Sending the data bit by bit */
252 for (i = 0; i < 8; i++) {
257 if ((value & 0x80) != 0)
264 /* Toggle clk line to one */
268 /* Shift byte to be sent */
272 /* Set the SCL Low and SDA High (prepare to get input) */
276 /* Set the SCL High for ack */
281 /* Read SDA, until SDA==0 */
282 for (i = 0; i < 0xff; i++) {
283 if (!sw_i2c_read_sda())
292 /* Set the SCL Low and SDA High */
303 * This function reads one byte from the slave device
306 * ack - Flag to indicate either to send the acknowledge
307 * message to the slave device or not
310 * One byte data read from the Slave device
312 static unsigned char sw_i2c_read_byte(unsigned char ack)
315 unsigned char data = 0;
317 for (i = 7; i >= 0; i--) {
318 /* Set the SCL to Low and SDA to High (Input) */
323 /* Set the SCL High */
327 /* Read data bits from SDA */
328 data |= (sw_i2c_read_sda() << i);
334 /* Set the SCL Low and SDA High */
342 * This function initializes GPIO port for SW I2C communication.
345 * clk_gpio - The GPIO pin to be used as i2c SCL
346 * data_gpio - The GPIO pin to be used as i2c SDA
349 * -1 - Fail to initialize the i2c
352 static long sm750le_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
356 /* Initialize the GPIO pin for the i2c Clock Register */
357 sw_i2c_clk_gpio_data_reg = GPIO_DATA_SM750LE;
358 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
360 /* Initialize the Clock GPIO Offset */
361 sw_i2c_clk_gpio = clk_gpio;
363 /* Initialize the GPIO pin for the i2c Data Register */
364 sw_i2c_data_gpio_data_reg = GPIO_DATA_SM750LE;
365 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
367 /* Initialize the Data GPIO Offset */
368 sw_i2c_data_gpio = data_gpio;
370 /* Note that SM750LE don't have GPIO MUX and power is always on */
372 /* Clear the i2c lines. */
373 for (i = 0; i < 9; i++)
380 * This function initializes the i2c attributes and bus
383 * clk_gpio - The GPIO pin to be used as i2c SCL
384 * data_gpio - The GPIO pin to be used as i2c SDA
387 * -1 - Fail to initialize the i2c
390 long sm750_sw_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
395 * Return 0 if the GPIO pins to be used is out of range. The
396 * range is only from [0..63]
398 if ((clk_gpio > 31) || (data_gpio > 31))
401 if (sm750_get_chip_type() == SM750LE)
402 return sm750le_i2c_init(clk_gpio, data_gpio);
404 /* Initialize the GPIO pin for the i2c Clock Register */
405 sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
406 sw_i2c_clk_gpio_data_reg = GPIO_DATA;
407 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
409 /* Initialize the Clock GPIO Offset */
410 sw_i2c_clk_gpio = clk_gpio;
412 /* Initialize the GPIO pin for the i2c Data Register */
413 sw_i2c_data_gpio_mux_reg = GPIO_MUX;
414 sw_i2c_data_gpio_data_reg = GPIO_DATA;
415 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
417 /* Initialize the Data GPIO Offset */
418 sw_i2c_data_gpio = data_gpio;
420 /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */
421 poke32(sw_i2c_clk_gpio_mux_reg,
422 peek32(sw_i2c_clk_gpio_mux_reg) & ~(1 << sw_i2c_clk_gpio));
423 poke32(sw_i2c_data_gpio_mux_reg,
424 peek32(sw_i2c_data_gpio_mux_reg) & ~(1 << sw_i2c_data_gpio));
426 /* Enable GPIO power */
427 sm750_enable_gpio(1);
429 /* Clear the i2c lines. */
430 for (i = 0; i < 9; i++)
437 * This function reads the slave device's register
440 * addr - i2c Slave device address which register
442 * reg - Slave device's register to be read
447 unsigned char sm750_sw_i2c_read_reg(unsigned char addr, unsigned char reg)
451 /* Send the Start signal */
454 /* Send the device address */
455 sw_i2c_write_byte(addr);
457 /* Send the register index */
458 sw_i2c_write_byte(reg);
460 /* Get the bus again and get the data from the device read address */
462 sw_i2c_write_byte(addr + 1);
463 data = sw_i2c_read_byte(1);
465 /* Stop swI2C and release the bus */
472 * This function writes a value to the slave device's register
475 * addr - i2c Slave device address which register
477 * reg - Slave device's register to be written
478 * data - Data to be written to the register
484 long sm750_sw_i2c_write_reg(unsigned char addr,
490 /* Send the Start signal */
493 /* Send the device address and read the data. All should return success
494 * in order for the writing processed to be successful
496 if ((sw_i2c_write_byte(addr) != 0) ||
497 (sw_i2c_write_byte(reg) != 0) ||
498 (sw_i2c_write_byte(data) != 0)) {
502 /* Stop i2c and release the bus */