eclib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "eclib.h"
  2. #include <sys/mman.h>
  3. static int fd;
  4. #ifdef FT
  5. #define CONTROL_NOTE "/dev/mem"
  6. const off_t OFF_EC_PORT = 0x20000000;
  7. uint64_t map_addr = MAP_FAILED;
  8. bool port_dev_init(void)
  9. {
  10. fd = open(CONTROL_NOTE, O_RDWR | O_SYNC);
  11. if (fd < 0) {
  12. return false;
  13. }
  14. map_addr = (uint64_t)mmap(NULL, 0x1000, PROT_WRITE|O_SYNC, MAP_SHARED, fd, OFF_EC_PORT);
  15. if (map_addr == MAP_FAILED) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. void port_dev_exit(void)
  21. {
  22. if (map_addr != MAP_FAILED) {
  23. munmap((void*)map_addr, 0x1000);
  24. }
  25. if (fd > 0)
  26. close(fd);
  27. }
  28. uint8_t ioread8(uint16_t port)
  29. {
  30. uint8_t val = 0;
  31. val = *(uint8_t *)(map_addr + port);
  32. return val;
  33. }
  34. void iowrite8(uint16_t port, uint8_t data)
  35. {
  36. *(uint8_t *)(map_addr + port) = data;
  37. }
  38. #else
  39. #define CONTROL_NOTE "/dev/port"
  40. bool port_dev_init(void)
  41. {
  42. fd = open(CONTROL_NOTE, O_SYNC);
  43. if (fd < 0) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. void port_dev_exit(void)
  49. {
  50. close(fd);
  51. }
  52. uint8_t ioread8(uint16_t port)
  53. {
  54. uint8_t buf[2] = { 0 };
  55. lseek(fd, port, SEEK_SET);
  56. read(fd, buf, 1);
  57. return buf[0];
  58. }
  59. void iowrite8(uint16_t port, uint8_t data)
  60. {
  61. uint8_t buf[2] = { 0 };
  62. printf("OFF_EC_D_PORT = 0x%X \n", port);
  63. lseek(fd, port, SEEK_SET);
  64. buf[0] = data;
  65. write(fd, buf, 1);
  66. }
  67. #endif
  68. bool wait_kbc_ibe(uint16_t cmd_state)
  69. {
  70. uint8_t kbd_cmd_state = 0;
  71. uint64_t index;
  72. for (index = 0; index < EC_TIME_OUT; index ++) {
  73. kbd_cmd_state = (uint8_t)ioread8(cmd_state);
  74. if (!(kbd_cmd_state & EC_S_IBF)) {
  75. return true;
  76. }
  77. else {
  78. //delay
  79. }
  80. }
  81. printf("wait ibe time out\n");
  82. return false;
  83. }
  84. bool wait_kbc_obf (uint16_t cmd_state)
  85. {
  86. uint8_t kbd_cmd_state = 0;
  87. uint64_t index;
  88. for (index = 0; index < EC_TIME_OUT; index ++) {
  89. kbd_cmd_state = (uint8_t)ioread8(cmd_state);
  90. if (kbd_cmd_state & EC_S_OBF) {
  91. return true;
  92. }
  93. else {
  94. //delay
  95. }
  96. }
  97. printf("wait ibe time out\n");
  98. return false;
  99. }
  100. void send_data (uint8_t port, uint8_t data)
  101. {
  102. wait_kbc_ibe (port); // Wait Input Buffer Empty
  103. iowrite8 (port - 4, data);
  104. wait_kbc_ibe (port); // Wait Input Buffer Empty
  105. }
  106. uint8_t get_data (uint8_t port, bool kb_ms_ctrl)
  107. {
  108. uint8_t data;
  109. wait_kbc_ibe (port); // Wait Input Buffer Empty
  110. wait_kbc_obf (port); // Wait Output Buffer Full
  111. data = ioread8 (port - 4);
  112. return data;
  113. }
  114. void send_cmd(uint8_t port, uint8_t cmd, bool kb_ms_ctrl)
  115. {
  116. wait_kbc_ibe(port);
  117. iowrite8(port, cmd);
  118. wait_kbc_ibe(port);
  119. }
  120. uint8_t read_ec_ram(uint8_t index)
  121. {
  122. send_cmd (EC_C_PORT, EC_C_READ_MEM, false);
  123. /* send ECRAM offset to DATA port */
  124. send_data (EC_C_PORT, index);
  125. /* get DATA from EC */
  126. return get_data (EC_C_PORT, false);
  127. }
  128. void write_ec_ram(uint8_t index, uint8_t value)
  129. {
  130. /* Write Command */
  131. send_cmd (EC_C_PORT, EC_C_WRITE_MEM, false);
  132. /* send ECRAM offset to DATA port */
  133. send_data (EC_C_PORT, index);
  134. /* Write DATA to EC ram */
  135. send_data (EC_C_PORT, value);
  136. }
  137. uint8_t read_ec(uint16_t offset)
  138. {
  139. uint8_t data;
  140. iowrite8 ((uint16_t)(EC_IO_BASE + 1), (uint8_t)((offset & 0xFF00) >> 8));
  141. iowrite8 ((uint16_t)(EC_IO_BASE + 2), (uint8_t)(offset & 0x00FF));
  142. data = ioread8 ((uint16_t)(EC_IO_BASE + 3));
  143. return data;
  144. }
  145. void write_ec(uint16_t offset, uint8_t data)
  146. {
  147. iowrite8 ((uint16_t)(EC_IO_BASE + 1), (uint8_t)((offset & 0xFF00) >> 8));
  148. iowrite8 ((uint16_t)(EC_IO_BASE + 2), (uint8_t)(offset & 0x00FF));
  149. iowrite8 ((uint16_t)(EC_IO_BASE + 3), data);
  150. }