pm_smbus.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * PC SMBus implementation
  3. * splitted from acpi.c
  4. *
  5. * Copyright (c) 2006 Fabrice Bellard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License version 2 as published by the Free Software Foundation.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include "hw.h"
  21. #include "pc.h"
  22. #include "pm_smbus.h"
  23. #include "smbus.h"
  24. /* no save/load? */
  25. #define SMBHSTSTS 0x00
  26. #define SMBHSTCNT 0x02
  27. #define SMBHSTCMD 0x03
  28. #define SMBHSTADD 0x04
  29. #define SMBHSTDAT0 0x05
  30. #define SMBHSTDAT1 0x06
  31. #define SMBBLKDAT 0x07
  32. //#define DEBUG
  33. #ifdef DEBUG
  34. # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  35. #else
  36. # define SMBUS_DPRINTF(format, ...) do { } while (0)
  37. #endif
  38. static void smb_transaction(PMSMBus *s)
  39. {
  40. uint8_t prot = (s->smb_ctl >> 2) & 0x07;
  41. uint8_t read = s->smb_addr & 0x01;
  42. uint8_t cmd = s->smb_cmd;
  43. uint8_t addr = s->smb_addr >> 1;
  44. i2c_bus *bus = s->smbus;
  45. SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
  46. switch(prot) {
  47. case 0x0:
  48. smbus_quick_command(bus, addr, read);
  49. break;
  50. case 0x1:
  51. if (read) {
  52. s->smb_data0 = smbus_receive_byte(bus, addr);
  53. } else {
  54. smbus_send_byte(bus, addr, cmd);
  55. }
  56. break;
  57. case 0x2:
  58. if (read) {
  59. s->smb_data0 = smbus_read_byte(bus, addr, cmd);
  60. } else {
  61. smbus_write_byte(bus, addr, cmd, s->smb_data0);
  62. }
  63. break;
  64. case 0x3:
  65. if (read) {
  66. uint16_t val;
  67. val = smbus_read_word(bus, addr, cmd);
  68. s->smb_data0 = val;
  69. s->smb_data1 = val >> 8;
  70. } else {
  71. smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
  72. }
  73. break;
  74. case 0x5:
  75. if (read) {
  76. s->smb_data0 = smbus_read_block(bus, addr, cmd, s->smb_data);
  77. } else {
  78. smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
  79. }
  80. break;
  81. default:
  82. goto error;
  83. }
  84. return;
  85. error:
  86. s->smb_stat |= 0x04;
  87. }
  88. void smb_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  89. {
  90. PMSMBus *s = opaque;
  91. addr &= 0x3f;
  92. SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
  93. switch(addr) {
  94. case SMBHSTSTS:
  95. s->smb_stat = 0;
  96. s->smb_index = 0;
  97. break;
  98. case SMBHSTCNT:
  99. s->smb_ctl = val;
  100. if (val & 0x40)
  101. smb_transaction(s);
  102. break;
  103. case SMBHSTCMD:
  104. s->smb_cmd = val;
  105. break;
  106. case SMBHSTADD:
  107. s->smb_addr = val;
  108. break;
  109. case SMBHSTDAT0:
  110. s->smb_data0 = val;
  111. break;
  112. case SMBHSTDAT1:
  113. s->smb_data1 = val;
  114. break;
  115. case SMBBLKDAT:
  116. s->smb_data[s->smb_index++] = val;
  117. if (s->smb_index > 31)
  118. s->smb_index = 0;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. uint32_t smb_ioport_readb(void *opaque, uint32_t addr)
  125. {
  126. PMSMBus *s = opaque;
  127. uint32_t val;
  128. addr &= 0x3f;
  129. switch(addr) {
  130. case SMBHSTSTS:
  131. val = s->smb_stat;
  132. break;
  133. case SMBHSTCNT:
  134. s->smb_index = 0;
  135. val = s->smb_ctl & 0x1f;
  136. break;
  137. case SMBHSTCMD:
  138. val = s->smb_cmd;
  139. break;
  140. case SMBHSTADD:
  141. val = s->smb_addr;
  142. break;
  143. case SMBHSTDAT0:
  144. val = s->smb_data0;
  145. break;
  146. case SMBHSTDAT1:
  147. val = s->smb_data1;
  148. break;
  149. case SMBBLKDAT:
  150. val = s->smb_data[s->smb_index++];
  151. if (s->smb_index > 31)
  152. s->smb_index = 0;
  153. break;
  154. default:
  155. val = 0;
  156. break;
  157. }
  158. SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
  159. return val;
  160. }
  161. void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
  162. {
  163. smb->smbus = i2c_init_bus(parent, "i2c");
  164. }