1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2007 by Silicon Motion, Inc. (SMI)
5 * swi2c.c --- SM750/SM718 DDK
6 * This file contains the source code for I2C using software
10 #include "ddk750_chip.h"
11 #include "ddk750_reg.h"
12 #include "ddk750_swi2c.h"
13 #include "ddk750_power.h"
16 * I2C Software Master Driver:
17 * ===========================
18 * Each i2c cycle is split into 4 sections. Each of these section marks
19 * a point in time where the SCL or SDA may be changed.
21 * 1 Cycle == | Section I. | Section 2. | Section 3. | Section 4. |
22 * +-------------+-------------+-------------+-------------+
23 * | SCL set LOW |SCL no change| SCL set HIGH|SCL no change|
25 * ____________ _____________
26 * SCL == XXXX _____________ ____________ /
28 * I.e. the SCL may only be changed in section 1. and section 3. while
29 * the SDA may only be changed in section 2. and section 4. The table
30 * below gives the changes for these 2 lines in the varios sections.
32 * Section changes Table:
33 * ======================
34 * blank = no change, L = set bit LOW, H = set bit HIGH
37 * ---------------+---+---+---+---+
38 * Tx Start SDA | | H | | L |
40 * ---------------+---+---+---+---+
41 * Tx Stop SDA | | L | | H |
43 * ---------------+---+---+---+---+
44 * Tx bit H SDA | | H | | |
46 * ---------------+---+---+---+---+
47 * Tx bit L SDA | | L | | |
49 * ---------------+---+---+---+---+
53 /* GPIO pins used for this I2C. It ranges from 0 to 63. */
54 static unsigned char sw_i2c_clk_gpio = DEFAULT_I2C_SCL;
55 static unsigned char sw_i2c_data_gpio = DEFAULT_I2C_SDA;
58 * Below is the variable declaration for the GPIO pin register usage
59 * for the i2c Clock and i2c Data.
62 * Notice that the GPIO usage for the i2c clock and i2c Data are
63 * separated. This is to make this code flexible enough when
64 * two separate GPIO pins for the clock and data are located
65 * in two different GPIO register set (worst case).
68 /* i2c Clock GPIO Register usage */
69 static unsigned long sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
70 static unsigned long sw_i2c_clk_gpio_data_reg = GPIO_DATA;
71 static unsigned long sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
73 /* i2c Data GPIO Register usage */
74 static unsigned long sw_i2c_data_gpio_mux_reg = GPIO_MUX;
75 static unsigned long sw_i2c_data_gpio_data_reg = GPIO_DATA;
76 static unsigned long sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
79 * This function puts a delay between command
81 static void sw_i2c_wait(void)
84 * peekIO method works well before suspend/resume
85 * but after suspend, peekIO(0x3ce,0x61) & 0x10
86 * always be non-zero,which makes the while loop
88 * use non-ultimate for loop below is safe
91 /* Change wait algorithm to use PCI bus clock,
92 * it's more reliable than counter loop ..
93 * write 0x61 to 0x3ce and read from 0x3cf
97 for (i = 0; i < 600; i++) {
104 * This function set/reset the SCL GPIO pin
107 * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
110 * When setting SCL to high, just set the GPIO as input where the pull up
111 * resistor will pull the signal up. Do not use software to pull up the
112 * signal because the i2c will fail when other device try to drive the
113 * signal due to SM50x will drive the signal to always high.
115 static void sw_i2c_scl(unsigned char value)
117 unsigned long gpio_data;
118 unsigned long gpio_dir;
120 gpio_dir = peek32(sw_i2c_clk_gpio_data_dir_reg);
121 if (value) { /* High */
123 * Set direction as input. This will automatically
124 * pull the signal up.
126 gpio_dir &= ~(1 << sw_i2c_clk_gpio);
127 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
129 /* Set the signal down */
130 gpio_data = peek32(sw_i2c_clk_gpio_data_reg);
131 gpio_data &= ~(1 << sw_i2c_clk_gpio);
132 poke32(sw_i2c_clk_gpio_data_reg, gpio_data);
134 /* Set direction as output */
135 gpio_dir |= (1 << sw_i2c_clk_gpio);
136 poke32(sw_i2c_clk_gpio_data_dir_reg, gpio_dir);
141 * This function set/reset the SDA GPIO pin
144 * value - Bit value to set to the SCL or SDA (0 = low, 1 = high)
147 * When setting SCL to high, just set the GPIO as input where the pull up
148 * resistor will pull the signal up. Do not use software to pull up the
149 * signal because the i2c will fail when other device try to drive the
150 * signal due to SM50x will drive the signal to always high.
152 static void sw_i2c_sda(unsigned char value)
154 unsigned long gpio_data;
155 unsigned long gpio_dir;
157 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
158 if (value) { /* High */
160 * Set direction as input. This will automatically
161 * pull the signal up.
163 gpio_dir &= ~(1 << sw_i2c_data_gpio);
164 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
166 /* Set the signal down */
167 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
168 gpio_data &= ~(1 << sw_i2c_data_gpio);
169 poke32(sw_i2c_data_gpio_data_reg, gpio_data);
171 /* Set direction as output */
172 gpio_dir |= (1 << sw_i2c_data_gpio);
173 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
178 * This function read the data from the SDA GPIO pin
181 * The SDA data bit sent by the Slave
183 static unsigned char sw_i2c_read_sda(void)
185 unsigned long gpio_dir;
186 unsigned long gpio_data;
187 unsigned long dir_mask = 1 << sw_i2c_data_gpio;
189 /* Make sure that the direction is input (High) */
190 gpio_dir = peek32(sw_i2c_data_gpio_data_dir_reg);
191 if ((gpio_dir & dir_mask) != ~dir_mask) {
192 gpio_dir &= ~(1 << sw_i2c_data_gpio);
193 poke32(sw_i2c_data_gpio_data_dir_reg, gpio_dir);
196 /* Now read the SDA line */
197 gpio_data = peek32(sw_i2c_data_gpio_data_reg);
198 if (gpio_data & (1 << sw_i2c_data_gpio))
205 * This function sends ACK signal
207 static void sw_i2c_ack(void)
209 return; /* Single byte read is ok without it. */
213 * This function sends the start command to the slave device
215 static void sw_i2c_start(void)
224 * This function sends the stop command to the slave device
226 static void sw_i2c_stop(void)
235 * This function writes one byte to the slave device
238 * data - Data to be write to the slave device
242 * -1 - Fail to write byte
244 static long sw_i2c_write_byte(unsigned char data)
246 unsigned char value = data;
249 /* Sending the data bit by bit */
250 for (i = 0; i < 8; i++) {
255 if ((value & 0x80) != 0)
262 /* Toggle clk line to one */
266 /* Shift byte to be sent */
270 /* Set the SCL Low and SDA High (prepare to get input) */
274 /* Set the SCL High for ack */
279 /* Read SDA, until SDA==0 */
280 for (i = 0; i < 0xff; i++) {
281 if (!sw_i2c_read_sda())
290 /* Set the SCL Low and SDA High */
301 * This function reads one byte from the slave device
304 * ack - Flag to indicate either to send the acknowledge
305 * message to the slave device or not
308 * One byte data read from the Slave device
310 static unsigned char sw_i2c_read_byte(unsigned char ack)
313 unsigned char data = 0;
315 for (i = 7; i >= 0; i--) {
316 /* Set the SCL to Low and SDA to High (Input) */
321 /* Set the SCL High */
325 /* Read data bits from SDA */
326 data |= (sw_i2c_read_sda() << i);
332 /* Set the SCL Low and SDA High */
340 * This function initializes GPIO port for SW I2C communication.
343 * clk_gpio - The GPIO pin to be used as i2c SCL
344 * data_gpio - The GPIO pin to be used as i2c SDA
347 * -1 - Fail to initialize the i2c
350 static long sm750le_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
354 /* Initialize the GPIO pin for the i2c Clock Register */
355 sw_i2c_clk_gpio_data_reg = GPIO_DATA_SM750LE;
356 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
358 /* Initialize the Clock GPIO Offset */
359 sw_i2c_clk_gpio = clk_gpio;
361 /* Initialize the GPIO pin for the i2c Data Register */
362 sw_i2c_data_gpio_data_reg = GPIO_DATA_SM750LE;
363 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION_SM750LE;
365 /* Initialize the Data GPIO Offset */
366 sw_i2c_data_gpio = data_gpio;
368 /* Note that SM750LE don't have GPIO MUX and power is always on */
370 /* Clear the i2c lines. */
371 for (i = 0; i < 9; i++)
378 * This function initializes the i2c attributes and bus
381 * clk_gpio - The GPIO pin to be used as i2c SCL
382 * data_gpio - The GPIO pin to be used as i2c SDA
385 * -1 - Fail to initialize the i2c
388 long sm750_sw_i2c_init(unsigned char clk_gpio, unsigned char data_gpio)
393 * Return 0 if the GPIO pins to be used is out of range. The
394 * range is only from [0..63]
396 if ((clk_gpio > 31) || (data_gpio > 31))
399 if (sm750_get_chip_type() == SM750LE)
400 return sm750le_i2c_init(clk_gpio, data_gpio);
402 /* Initialize the GPIO pin for the i2c Clock Register */
403 sw_i2c_clk_gpio_mux_reg = GPIO_MUX;
404 sw_i2c_clk_gpio_data_reg = GPIO_DATA;
405 sw_i2c_clk_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
407 /* Initialize the Clock GPIO Offset */
408 sw_i2c_clk_gpio = clk_gpio;
410 /* Initialize the GPIO pin for the i2c Data Register */
411 sw_i2c_data_gpio_mux_reg = GPIO_MUX;
412 sw_i2c_data_gpio_data_reg = GPIO_DATA;
413 sw_i2c_data_gpio_data_dir_reg = GPIO_DATA_DIRECTION;
415 /* Initialize the Data GPIO Offset */
416 sw_i2c_data_gpio = data_gpio;
418 /* Enable the GPIO pins for the i2c Clock and Data (GPIO MUX) */
419 poke32(sw_i2c_clk_gpio_mux_reg,
420 peek32(sw_i2c_clk_gpio_mux_reg) & ~(1 << sw_i2c_clk_gpio));
421 poke32(sw_i2c_data_gpio_mux_reg,
422 peek32(sw_i2c_data_gpio_mux_reg) & ~(1 << sw_i2c_data_gpio));
424 /* Enable GPIO power */
425 sm750_enable_gpio(1);
427 /* Clear the i2c lines. */
428 for (i = 0; i < 9; i++)
435 * This function reads the slave device's register
438 * addr - i2c Slave device address which register
440 * reg - Slave device's register to be read
445 unsigned char sm750_sw_i2c_read_reg(unsigned char addr, unsigned char reg)
449 /* Send the Start signal */
452 /* Send the device address */
453 sw_i2c_write_byte(addr);
455 /* Send the register index */
456 sw_i2c_write_byte(reg);
458 /* Get the bus again and get the data from the device read address */
460 sw_i2c_write_byte(addr + 1);
461 data = sw_i2c_read_byte(1);
463 /* Stop swI2C and release the bus */
470 * This function writes a value to the slave device's register
473 * addr - i2c Slave device address which register
475 * reg - Slave device's register to be written
476 * data - Data to be written to the register
482 long sm750_sw_i2c_write_reg(unsigned char addr,
488 /* Send the Start signal */
491 /* Send the device address and read the data. All should return success
492 * in order for the writing processed to be successful
494 if ((sw_i2c_write_byte(addr) != 0) ||
495 (sw_i2c_write_byte(reg) != 0) ||
496 (sw_i2c_write_byte(data) != 0)) {
500 /* Stop i2c and release the bus */