i2c-sibyte.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/i2c.h>
  20. #include <linux/io.h>
  21. #include <asm/sibyte/sb1250_regs.h>
  22. #include <asm/sibyte/sb1250_smbus.h>
  23. struct i2c_algo_sibyte_data {
  24. void *data; /* private data */
  25. int bus; /* which bus */
  26. void *reg_base; /* CSR base */
  27. };
  28. /* ----- global defines ----------------------------------------------- */
  29. #define SMB_CSR(a,r) ((long)(a->reg_base + r))
  30. static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
  31. unsigned short flags, char read_write,
  32. u8 command, int size, union i2c_smbus_data * data)
  33. {
  34. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  35. int data_bytes = 0;
  36. int error;
  37. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  38. ;
  39. switch (size) {
  40. case I2C_SMBUS_QUICK:
  41. csr_out32((V_SMB_ADDR(addr) |
  42. (read_write == I2C_SMBUS_READ ? M_SMB_QDATA : 0) |
  43. V_SMB_TT_QUICKCMD), SMB_CSR(adap, R_SMB_START));
  44. break;
  45. case I2C_SMBUS_BYTE:
  46. if (read_write == I2C_SMBUS_READ) {
  47. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_RD1BYTE),
  48. SMB_CSR(adap, R_SMB_START));
  49. data_bytes = 1;
  50. } else {
  51. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  52. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR1BYTE),
  53. SMB_CSR(adap, R_SMB_START));
  54. }
  55. break;
  56. case I2C_SMBUS_BYTE_DATA:
  57. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  58. if (read_write == I2C_SMBUS_READ) {
  59. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD1BYTE),
  60. SMB_CSR(adap, R_SMB_START));
  61. data_bytes = 1;
  62. } else {
  63. csr_out32(V_SMB_LB(data->byte),
  64. SMB_CSR(adap, R_SMB_DATA));
  65. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  66. SMB_CSR(adap, R_SMB_START));
  67. }
  68. break;
  69. case I2C_SMBUS_WORD_DATA:
  70. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  71. if (read_write == I2C_SMBUS_READ) {
  72. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
  73. SMB_CSR(adap, R_SMB_START));
  74. data_bytes = 2;
  75. } else {
  76. csr_out32(V_SMB_LB(data->word & 0xff),
  77. SMB_CSR(adap, R_SMB_DATA));
  78. csr_out32(V_SMB_MB(data->word >> 8),
  79. SMB_CSR(adap, R_SMB_DATA));
  80. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  81. SMB_CSR(adap, R_SMB_START));
  82. }
  83. break;
  84. default:
  85. return -EOPNOTSUPP;
  86. }
  87. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  88. ;
  89. error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
  90. if (error & M_SMB_ERROR) {
  91. /* Clear error bit by writing a 1 */
  92. csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
  93. return (error & M_SMB_ERROR_TYPE) ? -EIO : -ENXIO;
  94. }
  95. if (data_bytes == 1)
  96. data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
  97. if (data_bytes == 2)
  98. data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
  99. return 0;
  100. }
  101. static u32 bit_func(struct i2c_adapter *adap)
  102. {
  103. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  104. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
  105. }
  106. /* -----exported algorithm data: ------------------------------------- */
  107. static const struct i2c_algorithm i2c_sibyte_algo = {
  108. .smbus_xfer = smbus_xfer,
  109. .functionality = bit_func,
  110. };
  111. /*
  112. * registering functions to load algorithms at runtime
  113. */
  114. static int __init i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
  115. {
  116. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  117. /* Register new adapter to i2c module... */
  118. i2c_adap->algo = &i2c_sibyte_algo;
  119. /* Set the requested frequency. */
  120. csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
  121. csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
  122. return i2c_add_numbered_adapter(i2c_adap);
  123. }
  124. static struct i2c_algo_sibyte_data sibyte_board_data[2] = {
  125. { NULL, 0, (void *) (CKSEG1+A_SMB_BASE(0)) },
  126. { NULL, 1, (void *) (CKSEG1+A_SMB_BASE(1)) }
  127. };
  128. static struct i2c_adapter sibyte_board_adapter[2] = {
  129. {
  130. .owner = THIS_MODULE,
  131. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  132. .algo = NULL,
  133. .algo_data = &sibyte_board_data[0],
  134. .nr = 0,
  135. .name = "SiByte SMBus 0",
  136. },
  137. {
  138. .owner = THIS_MODULE,
  139. .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
  140. .algo = NULL,
  141. .algo_data = &sibyte_board_data[1],
  142. .nr = 1,
  143. .name = "SiByte SMBus 1",
  144. },
  145. };
  146. static int __init i2c_sibyte_init(void)
  147. {
  148. pr_info("i2c-sibyte: i2c SMBus adapter module for SiByte board\n");
  149. if (i2c_sibyte_add_bus(&sibyte_board_adapter[0], K_SMB_FREQ_100KHZ) < 0)
  150. return -ENODEV;
  151. if (i2c_sibyte_add_bus(&sibyte_board_adapter[1],
  152. K_SMB_FREQ_400KHZ) < 0) {
  153. i2c_del_adapter(&sibyte_board_adapter[0]);
  154. return -ENODEV;
  155. }
  156. return 0;
  157. }
  158. static void __exit i2c_sibyte_exit(void)
  159. {
  160. i2c_del_adapter(&sibyte_board_adapter[0]);
  161. i2c_del_adapter(&sibyte_board_adapter[1]);
  162. }
  163. module_init(i2c_sibyte_init);
  164. module_exit(i2c_sibyte_exit);
  165. MODULE_AUTHOR("Kip Walker (Broadcom Corp.), Steven J. Hill <sjhill@realitydiluted.com>");
  166. MODULE_DESCRIPTION("SMBus adapter routines for SiByte boards");
  167. MODULE_LICENSE("GPL");