2
0

pm_smbus.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/hw.h"
  21. #include "hw/i386/pc.h"
  22. #include "hw/i2c/pm_smbus.h"
  23. #include "hw/i2c/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 STS_HOST_BUSY (1)
  33. #define STS_INTR (1<<1)
  34. #define STS_DEV_ERR (1<<2)
  35. #define STS_BUS_ERR (1<<3)
  36. #define STS_FAILED (1<<4)
  37. #define STS_SMBALERT (1<<5)
  38. #define STS_INUSE_STS (1<<6)
  39. #define STS_BYTE_DONE (1<<7)
  40. /* Signs of successfully transaction end :
  41. * ByteDoneStatus = 1 (STS_BYTE_DONE) and INTR = 1 (STS_INTR )
  42. */
  43. //#define DEBUG
  44. #ifdef DEBUG
  45. # define SMBUS_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  46. #else
  47. # define SMBUS_DPRINTF(format, ...) do { } while (0)
  48. #endif
  49. static void smb_transaction(PMSMBus *s)
  50. {
  51. uint8_t prot = (s->smb_ctl >> 2) & 0x07;
  52. uint8_t read = s->smb_addr & 0x01;
  53. uint8_t cmd = s->smb_cmd;
  54. uint8_t addr = s->smb_addr >> 1;
  55. I2CBus *bus = s->smbus;
  56. int ret;
  57. SMBUS_DPRINTF("SMBus trans addr=0x%02x prot=0x%02x\n", addr, prot);
  58. /* Transaction isn't exec if STS_DEV_ERR bit set */
  59. if ((s->smb_stat & STS_DEV_ERR) != 0) {
  60. goto error;
  61. }
  62. switch(prot) {
  63. case 0x0:
  64. ret = smbus_quick_command(bus, addr, read);
  65. goto done;
  66. case 0x1:
  67. if (read) {
  68. ret = smbus_receive_byte(bus, addr);
  69. goto data8;
  70. } else {
  71. ret = smbus_send_byte(bus, addr, cmd);
  72. goto done;
  73. }
  74. case 0x2:
  75. if (read) {
  76. ret = smbus_read_byte(bus, addr, cmd);
  77. goto data8;
  78. } else {
  79. ret = smbus_write_byte(bus, addr, cmd, s->smb_data0);
  80. goto done;
  81. }
  82. break;
  83. case 0x3:
  84. if (read) {
  85. ret = smbus_read_word(bus, addr, cmd);
  86. goto data16;
  87. } else {
  88. ret = smbus_write_word(bus, addr, cmd, (s->smb_data1 << 8) | s->smb_data0);
  89. goto done;
  90. }
  91. break;
  92. case 0x5:
  93. if (read) {
  94. ret = smbus_read_block(bus, addr, cmd, s->smb_data);
  95. goto data8;
  96. } else {
  97. ret = smbus_write_block(bus, addr, cmd, s->smb_data, s->smb_data0);
  98. goto done;
  99. }
  100. break;
  101. default:
  102. goto error;
  103. }
  104. abort();
  105. data16:
  106. if (ret < 0) {
  107. goto error;
  108. }
  109. s->smb_data1 = ret >> 8;
  110. data8:
  111. if (ret < 0) {
  112. goto error;
  113. }
  114. s->smb_data0 = ret;
  115. done:
  116. if (ret < 0) {
  117. goto error;
  118. }
  119. s->smb_stat |= STS_BYTE_DONE | STS_INTR;
  120. return;
  121. error:
  122. s->smb_stat |= STS_DEV_ERR;
  123. return;
  124. }
  125. static void smb_ioport_writeb(void *opaque, hwaddr addr, uint64_t val,
  126. unsigned width)
  127. {
  128. PMSMBus *s = opaque;
  129. SMBUS_DPRINTF("SMB writeb port=0x%04x val=0x%02x\n", addr, val);
  130. switch(addr) {
  131. case SMBHSTSTS:
  132. s->smb_stat = (~(val & 0xff)) & s->smb_stat;
  133. s->smb_index = 0;
  134. break;
  135. case SMBHSTCNT:
  136. s->smb_ctl = val;
  137. if (val & 0x40)
  138. smb_transaction(s);
  139. break;
  140. case SMBHSTCMD:
  141. s->smb_cmd = val;
  142. break;
  143. case SMBHSTADD:
  144. s->smb_addr = val;
  145. break;
  146. case SMBHSTDAT0:
  147. s->smb_data0 = val;
  148. break;
  149. case SMBHSTDAT1:
  150. s->smb_data1 = val;
  151. break;
  152. case SMBBLKDAT:
  153. s->smb_data[s->smb_index++] = val;
  154. if (s->smb_index > 31)
  155. s->smb_index = 0;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. static uint64_t smb_ioport_readb(void *opaque, hwaddr addr, unsigned width)
  162. {
  163. PMSMBus *s = opaque;
  164. uint32_t val;
  165. switch(addr) {
  166. case SMBHSTSTS:
  167. val = s->smb_stat;
  168. break;
  169. case SMBHSTCNT:
  170. s->smb_index = 0;
  171. val = s->smb_ctl & 0x1f;
  172. break;
  173. case SMBHSTCMD:
  174. val = s->smb_cmd;
  175. break;
  176. case SMBHSTADD:
  177. val = s->smb_addr;
  178. break;
  179. case SMBHSTDAT0:
  180. val = s->smb_data0;
  181. break;
  182. case SMBHSTDAT1:
  183. val = s->smb_data1;
  184. break;
  185. case SMBBLKDAT:
  186. val = s->smb_data[s->smb_index++];
  187. if (s->smb_index > 31)
  188. s->smb_index = 0;
  189. break;
  190. default:
  191. val = 0;
  192. break;
  193. }
  194. SMBUS_DPRINTF("SMB readb port=0x%04x val=0x%02x\n", addr, val);
  195. return val;
  196. }
  197. static const MemoryRegionOps pm_smbus_ops = {
  198. .read = smb_ioport_readb,
  199. .write = smb_ioport_writeb,
  200. .valid.min_access_size = 1,
  201. .valid.max_access_size = 1,
  202. .endianness = DEVICE_LITTLE_ENDIAN,
  203. };
  204. void pm_smbus_init(DeviceState *parent, PMSMBus *smb)
  205. {
  206. smb->smbus = i2c_init_bus(parent, "i2c");
  207. memory_region_init_io(&smb->io, OBJECT(parent), &pm_smbus_ops, smb,
  208. "pm-smbus", 64);
  209. }