i2c-yosemite.c 5.4 KB

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