i2c-algo-sibyte.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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; /* 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. .smbus_xfer = smbus_xfer,
  115. .algo_control = algo_control, /* ioctl */
  116. .functionality = bit_func,
  117. };
  118. /*
  119. * registering functions to load algorithms at runtime
  120. */
  121. int i2c_sibyte_add_bus(struct i2c_adapter *i2c_adap, int speed)
  122. {
  123. int i;
  124. struct i2c_algo_sibyte_data *adap = i2c_adap->algo_data;
  125. /* register new adapter to i2c module... */
  126. i2c_adap->algo = &i2c_sibyte_algo;
  127. /* Set the frequency to 100 kHz */
  128. csr_out32(speed, SMB_CSR(adap,R_SMB_FREQ));
  129. csr_out32(0, SMB_CSR(adap,R_SMB_CONTROL));
  130. /* scan bus */
  131. if (bit_scan) {
  132. union i2c_smbus_data data;
  133. int rc;
  134. printk(KERN_INFO " i2c-algo-sibyte.o: scanning bus %s.\n",
  135. i2c_adap->name);
  136. for (i = 0x00; i < 0x7f; i++) {
  137. /* XXXKW is this a realistic probe? */
  138. rc = smbus_xfer(i2c_adap, i, 0, I2C_SMBUS_READ, 0,
  139. I2C_SMBUS_BYTE_DATA, &data);
  140. if (!rc) {
  141. printk("(%02x)",i);
  142. } else
  143. printk(".");
  144. }
  145. printk("\n");
  146. }
  147. i2c_add_adapter(i2c_adap);
  148. return 0;
  149. }
  150. int i2c_sibyte_del_bus(struct i2c_adapter *adap)
  151. {
  152. int res;
  153. if ((res = i2c_del_adapter(adap)) < 0)
  154. return res;
  155. return 0;
  156. }
  157. int __init i2c_algo_sibyte_init (void)
  158. {
  159. printk("i2c-algo-sibyte.o: i2c SiByte algorithm module\n");
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(i2c_sibyte_add_bus);
  163. EXPORT_SYMBOL(i2c_sibyte_del_bus);
  164. #ifdef MODULE
  165. MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
  166. MODULE_DESCRIPTION("SiByte I2C-Bus algorithm");
  167. module_param(bit_scan, int, 0);
  168. MODULE_PARM_DESC(bit_scan, "Scan for active chips on the bus");
  169. MODULE_LICENSE("GPL");
  170. int init_module(void)
  171. {
  172. return i2c_algo_sibyte_init();
  173. }
  174. void cleanup_module(void)
  175. {
  176. }
  177. #endif