i2c-algo-sibyte.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* ------------------------------------------------------------------------- */
  2. /* i2c-algo-sibyte.c i2c driver algorithms for bit-shift adapters */
  3. /* ------------------------------------------------------------------------- */
  4. /* Copyright (C) 2001,2002,2003 Broadcom Corporation
  5. Copyright (C) 1995-2000 Simon G. Vogl
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  17. /* ------------------------------------------------------------------------- */
  18. /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
  19. Frodo Looijaard <frodol@dds.nl>. */
  20. /* Ported for SiByte SOCs by Broadcom Corporation. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <asm/io.h>
  25. #include <asm/sibyte/sb1250_regs.h>
  26. #include <asm/sibyte/sb1250_smbus.h>
  27. #include <linux/i2c.h>
  28. #include <linux/i2c-algo-sibyte.h>
  29. /* ----- global defines ----------------------------------------------- */
  30. #define SMB_CSR(a,r) ((long)(a->reg_base + r))
  31. /* ----- global variables --------------------------------------------- */
  32. /* module parameters:
  33. */
  34. static int bit_scan=0; /* have a look at what's hanging 'round */
  35. static int smbus_xfer(struct i2c_adapter *i2c_adap, u16 addr,
  36. unsigned short flags, char read_write,
  37. u8 command, int size, union i2c_smbus_data * data)
  38. {
  39. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  40. int data_bytes = 0;
  41. int error;
  42. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  43. ;
  44. switch (size) {
  45. case I2C_SMBUS_QUICK:
  46. csr_out32((V_SMB_ADDR(addr) | (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), SMB_CSR(adap, R_SMB_DATA));
  68. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  69. SMB_CSR(adap, R_SMB_START));
  70. }
  71. break;
  72. case I2C_SMBUS_WORD_DATA:
  73. csr_out32(V_SMB_CMD(command), SMB_CSR(adap, R_SMB_CMD));
  74. if (read_write == I2C_SMBUS_READ) {
  75. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_CMD_RD2BYTE),
  76. SMB_CSR(adap, R_SMB_START));
  77. data_bytes = 2;
  78. } else {
  79. csr_out32(V_SMB_LB(data->word & 0xff), SMB_CSR(adap, R_SMB_DATA));
  80. csr_out32(V_SMB_MB(data->word >> 8), SMB_CSR(adap, R_SMB_DATA));
  81. csr_out32((V_SMB_ADDR(addr) | V_SMB_TT_WR2BYTE),
  82. SMB_CSR(adap, R_SMB_START));
  83. }
  84. break;
  85. default:
  86. return -1; /* XXXKW better error code? */
  87. }
  88. while (csr_in32(SMB_CSR(adap, R_SMB_STATUS)) & M_SMB_BUSY)
  89. ;
  90. error = csr_in32(SMB_CSR(adap, R_SMB_STATUS));
  91. if (error & M_SMB_ERROR) {
  92. /* Clear error bit by writing a 1 */
  93. csr_out32(M_SMB_ERROR, SMB_CSR(adap, R_SMB_STATUS));
  94. return -1; /* XXXKW better error code? */
  95. }
  96. if (data_bytes == 1)
  97. data->byte = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xff;
  98. if (data_bytes == 2)
  99. data->word = csr_in32(SMB_CSR(adap, R_SMB_DATA)) & 0xffff;
  100. return 0;
  101. }
  102. static int algo_control(struct i2c_adapter *adapter,
  103. unsigned int cmd, unsigned long arg)
  104. {
  105. return 0;
  106. }
  107. static u32 bit_func(struct i2c_adapter *adap)
  108. {
  109. return (I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
  110. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA);
  111. }
  112. /* -----exported algorithm data: ------------------------------------- */
  113. static struct i2c_algorithm i2c_sibyte_algo = {
  114. .name = "SiByte algorithm",
  115. .id = I2C_ALGO_SIBYTE,
  116. .smbus_xfer = smbus_xfer,
  117. .algo_control = algo_control, /* ioctl */
  118. .functionality = bit_func,
  119. };
  120. /*
  121. * registering functions to load algorithms at runtime
  122. */
  123. int i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
  124. {
  125. int i;
  126. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  127. /* register new adapter to i2c module... */
  128. i2c_adap->id |= i2c_sibyte_algo.id;
  129. i2c_adap->algo = &i2c_sibyte_algo;
  130. /* Set the frequency to 100 kHz */
  131. csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
  132. csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
  133. /* scan bus */
  134. if (bit_scan) {
  135. union i2c_smbus_data data;
  136. int rc;
  137. printk(KERN_INFO " i2c-algo-sibyte.o: scanning bus %s.\n",
  138. i2c_adap->name);
  139. for (i = 0x00; i < 0x7f; i++) {
  140. /* XXXKW is this a realistic probe? */
  141. rc = smbus_xfer(i2c_adap, i, 0, I2C_SMBUS_READ, 0,
  142. I2C_SMBUS_BYTE_DATA, &data);
  143. if (!rc) {
  144. printk("(%02x)",i);
  145. } else
  146. printk(".");
  147. }
  148. printk("\n");
  149. }
  150. i2c_add_adapter(i2c_adap);
  151. return 0;
  152. }
  153. int i2c_sibyte_del_bus(struct i2c_adapter *adap)
  154. {
  155. int res;
  156. if ((res = i2c_del_adapter(adap)) < 0)
  157. return res;
  158. return 0;
  159. }
  160. int __init i2c_algo_sibyte_init (void)
  161. {
  162. printk("i2c-algo-sibyte.o: i2c SiByte algorithm module\n");
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(i2c_sibyte_add_bus);
  166. EXPORT_SYMBOL(i2c_sibyte_del_bus);
  167. #ifdef MODULE
  168. MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
  169. MODULE_DESCRIPTION("SiByte I2C-Bus algorithm");
  170. MODULE_PARM(bit_scan, "i");
  171. MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
  172. MODULE_LICENSE("GPL");
  173. int init_module(void)
  174. {
  175. return i2c_algo_sibyte_init();
  176. }
  177. void cleanup_module(void)
  178. {
  179. }
  180. #endif