i2c-sibyte.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2004 Steven J. Hill
  3. * Copyright (C) 2001,2002,2003 Broadcom Corporation
  4. * Copyright (C) 1995-2000 Simon G. Vogl
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/i2c.h>
  24. #include <linux/io.h>
  25. #include <asm/sibyte/sb1250_regs.h>
  26. #include <asm/sibyte/sb1250_smbus.h>
  27. struct i2c_algo_sibyte_data {
  28. void *data; /* private data */
  29. int bus; /* which bus */
  30. void *reg_base; /* CSR base */
  31. };
  32. /* ----- global defines ----------------------------------------------- */
  33. #define SMB_CSR(a,r) ((long)(a->reg_base + r))
  34. static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
  35. unsigned short flags, char read_write,
  36. u8 command, int size, union i2c_smbus_data * data)
  37. {
  38. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  39. int data_bytes = 0;
  40. int error;
  41. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  42. ;
  43. switch (size) {
  44. case I2C_SMBUS_QUICK:
  45. csr_out32((V_SMB_ADDR(addr) |
  46. (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) |
  47. V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START));
  48. break;
  49. case I2C_SMBUS_BYTE:
  50. if (read_write == I2C_SMBUS_READ) {
  51. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE),
  52. SMB_CSR(adap, R_SMB_START));
  53. data_bytes = 1;
  54. } else {
  55. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  56. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE),
  57. SMB_CSR(adap, R_SMB_START));
  58. }
  59. break;
  60. case I2C_SMBUS_BYTE_DATA:
  61. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  62. if (read_write == I2C_SMBUS_READ) {
  63. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE),
  64. SMB_CSR(adap, R_SMB_START));
  65. data_bytes = 1;
  66. } else {
  67. csr_out32(V_SMB_LB(data->byte),
  68. SMB_CSR(adap, R_SMB_DATA));
  69. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  70. SMB_CSR(adap, R_SMB_START));
  71. }
  72. break;
  73. case I2C_SMBUS_WORD_DATA:
  74. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  75. if (read_write == I2C_SMBUS_READ) {
  76. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
  77. SMB_CSR(adap, R_SMB_START));
  78. data_bytes = 2;
  79. } else {
  80. csr_out32(V_SMB_LB(data->word & 0xff),
  81. SMB_CSR(adap, R_SMB_DATA));
  82. csr_out32(V_SMB_MB(data->word >> 8),
  83. SMB_CSR(adap, R_SMB_DATA));
  84. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  85. SMB_CSR(adap, R_SMB_START));
  86. }
  87. break;
  88. default:
  89. return -EOPNOTSUPP;
  90. }
  91. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  92. ;
  93. error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
  94. if (error & M_SMB_ERROR) {
  95. /* Clear error bit by writing a 1 */
  96. csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
  97. return (error & M_SMB_ERROR_TYPE) ? -EIO : -ENXIO;
  98. }
  99. if (data_bytes == 1)
  100. data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
  101. if (data_bytes == 2)
  102. data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
  103. return 0;
  104. }
  105. static u32 bit_func(struct i2c_adapter *adap)
  106. {
  107. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  108. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
  109. }
  110. /* -----exported algorithm data: ------------------------------------- */
  111. static const struct i2c_algorithm i2c_sibyte_algo = {
  112. .smbus_xfer = smbus_xfer,
  113. .functionality = bit_func,
  114. };
  115. /*
  116. * registering functions to load algorithms at runtime
  117. */
  118. static int __init i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
  119. {
  120. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  121. /* Register new adapter to i2c module... */
  122. i2c_adap->algo = &i2c_sibyte_algo;
  123. /* Set the requested frequency. */
  124. csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
  125. csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
  126. return i2c_add_numbered_adapter(i2c_adap);
  127. }
  128. static struct i2c_algo_sibyte_data sibyte_board_data[2] = {
  129. { NULL, 0, (void *) (CKSEG1+A_SMB_BASE(0)) },
  130. { NULL, 1, (void *) (CKSEG1+A_SMB_BASE(1)) }
  131. };
  132. static struct i2c_adapter sibyte_board_adapter[2] = {
  133. {
  134. .owner = THIS_MODULE,
  135. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  136. .algo = NULL,
  137. .algo_data = &sibyte_board_data[0],
  138. .nr = 0,
  139. .name = "SiByte SMBus 0",
  140. },
  141. {
  142. .owner = THIS_MODULE,
  143. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  144. .algo = NULL,
  145. .algo_data = &sibyte_board_data[1],
  146. .nr = 1,
  147. .name = "SiByte SMBus 1",
  148. },
  149. };
  150. static int __init i2c_sibyte_init(void)
  151. {
  152. pr_info("i2c-sibyte: i2c SMBus adapter module for SiByte board\n");
  153. if (i2c_sibyte_add_bus(&sibyte_board_adapter[0], K_SMB_FREQ_100KHZ) < 0)
  154. return -ENODEV;
  155. if (i2c_sibyte_add_bus(&sibyte_board_adapter[1],
  156. K_SMB_FREQ_400KHZ) < 0) {
  157. i2c_del_adapter(&sibyte_board_adapter[0]);
  158. return -ENODEV;
  159. }
  160. return 0;
  161. }
  162. static void __exit i2c_sibyte_exit(void)
  163. {
  164. i2c_del_adapter(&sibyte_board_adapter[0]);
  165. i2c_del_adapter(&sibyte_board_adapter[1]);
  166. }
  167. module_init(i2c_sibyte_init);
  168. module_exit(i2c_sibyte_exit);
  169. MODULE_AUTHOR("Kip Walker (Broadcom Corp.), Steven J. Hill <sjhill@realitydiluted.com>");
  170. MODULE_DESCRIPTION("SMBus adapter routines for SiByte boards");
  171. MODULE_LICENSE("GPL");