1 /*
2  *  arch/mips/pmc-sierra/yosemite/i2c-yosemite.c
3  *
4  *  Copyright (C) 2003 PMC-Sierra Inc.
5  *  Author: Manish Lachwani (lachwani@pmc-sierra.com)
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
13  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
14  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
15  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
16  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
18  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
20  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  *
23  *  You should have received a copy of the  GNU General Public License along
24  *  with this program; if not, write  to the Free Software Foundation, Inc.,
25  *  675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27 
28 /*
29  * Detailed Description:
30  *
31  * This block implements the I2C interface to the slave devices like the Atmel 24C32
32  * EEPROM and the MAX 1619 Sensors device. The I2C Master interface can be controlled
33  * by the SCMB block. And the SCMB block kicks in only when using the Ethernet Mode of
34  * operation and __not__ the SysAD mode
35  *
36  * The SCMB controls the two modes: MDIO and the I2C. The MDIO mode is used to communicate
37  * with the Quad-PHY from Marvel. The I2C is used to communicate with the I2C slave devices.
38  * It seems that the driver does not explicitly deal with the control of SDA and SCL serial
39  * lines. So, the driver will set the slave address, drive the command and then the data.
40  * The SCMB will then control the two serial lines as required.
41  *
42  * It seems the documents are very unclear abt this. Hence, I took some time out to write
43  * the desciption to have an idea of how the I2C can actually work. Currently, this Linux
44  * driver wont be integrated into the generic Linux I2C framework. And finally, the I2C
45  * interface is also known as the 2BI interface. 2BI means 2-bit interface referring to
46  * SDA and SCL serial lines respectively.
47  *
48  * - Manish Lachwani (12/09/2003)
49  */
50 
51 #include "i2c-yosemite.h"
52 
53 /*
54  * Poll the I2C interface for the BUSY bit.
55  */
titan_i2c_poll(void)56 static int titan_i2c_poll(void)
57 {
58 	int		i = 0;
59 	unsigned long	val = 0;
60 
61 	for (i = 0; i < TITAN_I2C_MAX_POLL; i++) {
62 		val = TITAN_I2C_READ(TITAN_I2C_COMMAND);
63 
64 		if ( !(val & 0x8000))
65 			return 0;
66 	}
67 
68 	return TITAN_I2C_ERR_TIMEOUT;
69 }
70 
71 /*
72  * Execute the I2C command
73  */
titan_i2c_xfer(unsigned int slave_addr,titan_i2c_command * cmd,int size,unsigned int * addr)74 int titan_i2c_xfer(unsigned int slave_addr, titan_i2c_command *cmd,
75 						int size, unsigned int *addr)
76 {
77 	int		loop = 0, bytes, i;
78 	unsigned int	*write_data, data, *read_data;
79 	unsigned long	reg_val, val;
80 
81 	write_data = cmd->data;
82 	read_data = addr;
83 
84 	TITAN_I2C_WRITE(TITAN_I2C_SLAVE_ADDRESS, slave_addr);
85 
86 	if (cmd->type == TITAN_I2C_CMD_WRITE)
87 		loop = cmd->write_size;
88 	else
89 		loop = size;
90 
91 	while (loop > 0) {
92 		if ( (cmd->type == TITAN_I2C_CMD_WRITE)  ||
93 			(cmd->type == TITAN_I2C_CMD_READ_WRITE) ) {
94 
95 			reg_val = TITAN_I2C_DATA;
96 			for (i = 0; i < TITAN_I2C_MAX_WORDS_PER_RW; ++i,write_data += 2,
97 								reg_val += 4) {
98 				if (bytes < cmd->write_size) {
99 					data = write_data[0];
100 					++data;
101 				}
102 
103 				if (bytes < cmd->write_size) {
104 					data = write_data[1];
105 					++data;
106 				}
107 
108 				TITAN_I2C_WRITE(reg_val, data);
109 			}
110 		}
111 
112 		TITAN_I2C_WRITE(TITAN_I2C_COMMAND, (unsigned int)(cmd->type << 13));
113 		if (titan_i2c_poll() != TITAN_I2C_ERR_OK)
114 			return TITAN_I2C_ERR_TIMEOUT;
115 
116 		if ( (cmd->type == TITAN_I2C_CMD_READ)  ||
117 			(cmd->type == TITAN_I2C_CMD_READ_WRITE) ) {
118 
119 			reg_val = TITAN_I2C_DATA;
120 			for (i = 0; i < TITAN_I2C_MAX_WORDS_PER_RW; ++i,read_data += 2,
121 								reg_val += 4) {
122 				data = TITAN_I2C_READ(reg_val);
123 
124 				if (bytes < size) {
125 					read_data[0] = data & 0xff;
126 					++bytes;
127 				}
128 
129 				if (bytes < size) {
130 					read_data[1] = ((data >> 8) & 0xff);
131 					++bytes;
132 				}
133 			}
134 		}
135 
136 		loop -= (TITAN_I2C_MAX_WORDS_PER_RW * 2);
137 	}
138 
139 	/*
140 	 * Read the Interrupt status and then return the appropriate error code
141 	 */
142 
143 	val = TITAN_I2C_READ(TITAN_I2C_INTERRUPTS);
144 	if (val & 0x0020)
145 		return TITAN_I2C_ERR_ARB_LOST;
146 
147 	if (val & 0x0040)
148 		return TITAN_I2C_ERR_NO_RESP;
149 
150 	if (val & 0x0080)
151 		return TITAN_I2C_ERR_DATA_COLLISION;
152 
153 	return TITAN_I2C_ERR_OK;
154 }
155 
156 /*
157  * Init the I2C subsystem of the PMC-Sierra Yosemite board
158  */
titan_i2c_init(titan_i2c_config * config)159 int titan_i2c_init(titan_i2c_config *config)
160 {
161 	unsigned int	val;
162 
163 	/*
164 	 * Reset the SCMB and program into the I2C mode
165 	 */
166 	TITAN_I2C_WRITE(TITAN_I2C_SCMB_CONTROL, 0xA000);
167 	TITAN_I2C_WRITE(TITAN_I2C_SCMB_CONTROL, 0x2000);
168 
169 	/*
170 	 * Configure the filtera and clka values
171 	 */
172 	val = TITAN_I2C_READ(TITAN_I2C_SCMB_CLOCK_A);
173 	val |= ( (val & ~(0xF000)) | ( (config->filtera << 12) & 0xF000));
174 	val |= ( (val & ~(0x03FF)) | ( config->clka & 0x03FF));
175 	TITAN_I2C_WRITE(TITAN_I2C_SCMB_CLOCK_A, val);
176 
177 	/*
178 	 * Configure the filterb and clkb values
179 	 */
180 	val = TITAN_I2C_READ(TITAN_I2C_SCMB_CLOCK_B);
181 	val |= ( (val & ~(0xF000)) | ( (config->filterb << 12) & 0xF000));
182 	val |= ( (val & ~(0x03FF)) | ( config->clkb & 0x03FF));
183 	TITAN_I2C_WRITE(TITAN_I2C_SCMB_CLOCK_B, val);
184 
185 	return TITAN_I2C_ERR_OK;
186 }
187