cros_ec_lpc_mec.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * cros_ec_lpc_mec - LPC variant I/O for Microchip EC
  3. *
  4. * Copyright (C) 2016 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  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. *
  15. * This driver uses the Chrome OS EC byte-level message-based protocol for
  16. * communicating the keyboard state (which keys are pressed) from a keyboard EC
  17. * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
  18. * but everything else (including deghosting) is done here. The main
  19. * motivation for this is to keep the EC firmware as simple as possible, since
  20. * it cannot be easily upgraded and EC flash/IRAM space is relatively
  21. * expensive.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/io.h>
  25. #include <linux/mfd/cros_ec_commands.h>
  26. #include <linux/mfd/cros_ec_lpc_mec.h>
  27. #include <linux/mutex.h>
  28. #include <linux/types.h>
  29. /*
  30. * This mutex must be held while accessing the EMI unit. We can't rely on the
  31. * EC mutex because memmap data may be accessed without it being held.
  32. */
  33. static struct mutex io_mutex;
  34. /*
  35. * cros_ec_lpc_mec_emi_write_address
  36. *
  37. * Initialize EMI read / write at a given address.
  38. *
  39. * @addr: Starting read / write address
  40. * @access_type: Type of access, typically 32-bit auto-increment
  41. */
  42. static void cros_ec_lpc_mec_emi_write_address(u16 addr,
  43. enum cros_ec_lpc_mec_emi_access_mode access_type)
  44. {
  45. /* Address relative to start of EMI range */
  46. addr -= MEC_EMI_RANGE_START;
  47. outb((addr & 0xfc) | access_type, MEC_EMI_EC_ADDRESS_B0);
  48. outb((addr >> 8) & 0x7f, MEC_EMI_EC_ADDRESS_B1);
  49. }
  50. /*
  51. * cros_ec_lpc_io_bytes_mec - Read / write bytes to MEC EMI port
  52. *
  53. * @io_type: MEC_IO_READ or MEC_IO_WRITE, depending on request
  54. * @offset: Base read / write address
  55. * @length: Number of bytes to read / write
  56. * @buf: Destination / source buffer
  57. *
  58. * @return 8-bit checksum of all bytes read / written
  59. */
  60. u8 cros_ec_lpc_io_bytes_mec(enum cros_ec_lpc_mec_io_type io_type,
  61. unsigned int offset, unsigned int length,
  62. u8 *buf)
  63. {
  64. int i = 0;
  65. int io_addr;
  66. u8 sum = 0;
  67. enum cros_ec_lpc_mec_emi_access_mode access, new_access;
  68. /*
  69. * Long access cannot be used on misaligned data since reading B0 loads
  70. * the data register and writing B3 flushes.
  71. */
  72. if (offset & 0x3 || length < 4)
  73. access = ACCESS_TYPE_BYTE;
  74. else
  75. access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
  76. mutex_lock(&io_mutex);
  77. /* Initialize I/O at desired address */
  78. cros_ec_lpc_mec_emi_write_address(offset, access);
  79. /* Skip bytes in case of misaligned offset */
  80. io_addr = MEC_EMI_EC_DATA_B0 + (offset & 0x3);
  81. while (i < length) {
  82. while (io_addr <= MEC_EMI_EC_DATA_B3) {
  83. if (io_type == MEC_IO_READ)
  84. buf[i] = inb(io_addr++);
  85. else
  86. outb(buf[i], io_addr++);
  87. sum += buf[i++];
  88. offset++;
  89. /* Extra bounds check in case of misaligned length */
  90. if (i == length)
  91. goto done;
  92. }
  93. /*
  94. * Use long auto-increment access except for misaligned write,
  95. * since writing B3 triggers the flush.
  96. */
  97. if (length - i < 4 && io_type == MEC_IO_WRITE)
  98. new_access = ACCESS_TYPE_BYTE;
  99. else
  100. new_access = ACCESS_TYPE_LONG_AUTO_INCREMENT;
  101. if (new_access != access ||
  102. access != ACCESS_TYPE_LONG_AUTO_INCREMENT) {
  103. access = new_access;
  104. cros_ec_lpc_mec_emi_write_address(offset, access);
  105. }
  106. /* Access [B0, B3] on each loop pass */
  107. io_addr = MEC_EMI_EC_DATA_B0;
  108. }
  109. done:
  110. mutex_unlock(&io_mutex);
  111. return sum;
  112. }
  113. EXPORT_SYMBOL(cros_ec_lpc_io_bytes_mec);
  114. void cros_ec_lpc_mec_init(void)
  115. {
  116. mutex_init(&io_mutex);
  117. }
  118. EXPORT_SYMBOL(cros_ec_lpc_mec_init);
  119. void cros_ec_lpc_mec_destroy(void)
  120. {
  121. mutex_destroy(&io_mutex);
  122. }
  123. EXPORT_SYMBOL(cros_ec_lpc_mec_destroy);