i2c-sibyte.c 4.9 KB

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