i2c-amd-mp2.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
  2. /*
  3. * AMD MP2 I2C adapter driver
  4. *
  5. * Authors: Shyam Sundar S K <[email protected]>
  6. * Elie Morisse <[email protected]>
  7. */
  8. #ifndef I2C_AMD_PCI_MP2_H
  9. #define I2C_AMD_PCI_MP2_H
  10. #include <linux/i2c.h>
  11. #include <linux/pci.h>
  12. #include <linux/pm_runtime.h>
  13. #define PCI_DEVICE_ID_AMD_MP2 0x15E6
  14. struct amd_i2c_common;
  15. struct amd_mp2_dev;
  16. enum {
  17. /* MP2 C2P Message Registers */
  18. AMD_C2P_MSG0 = 0x10500, /* MP2 Message for I2C0 */
  19. AMD_C2P_MSG1 = 0x10504, /* MP2 Message for I2C1 */
  20. AMD_C2P_MSG2 = 0x10508, /* DRAM Address Lo / Data 0 */
  21. AMD_C2P_MSG3 = 0x1050c, /* DRAM Address HI / Data 1 */
  22. AMD_C2P_MSG4 = 0x10510, /* Data 2 */
  23. AMD_C2P_MSG5 = 0x10514, /* Data 3 */
  24. AMD_C2P_MSG6 = 0x10518, /* Data 4 */
  25. AMD_C2P_MSG7 = 0x1051c, /* Data 5 */
  26. AMD_C2P_MSG8 = 0x10520, /* Data 6 */
  27. AMD_C2P_MSG9 = 0x10524, /* Data 7 */
  28. /* MP2 P2C Message Registers */
  29. AMD_P2C_MSG0 = 0x10680, /* Do not use */
  30. AMD_P2C_MSG1 = 0x10684, /* I2C0 interrupt register */
  31. AMD_P2C_MSG2 = 0x10688, /* I2C1 interrupt register */
  32. AMD_P2C_MSG3 = 0x1068C, /* MP2 debug info */
  33. AMD_P2C_MSG_INTEN = 0x10690, /* MP2 interrupt gen register */
  34. AMD_P2C_MSG_INTSTS = 0x10694, /* Interrupt status */
  35. };
  36. /* Command register data structures */
  37. #define i2c_none (-1)
  38. enum i2c_cmd {
  39. i2c_read = 0,
  40. i2c_write,
  41. i2c_enable,
  42. i2c_disable,
  43. number_of_sensor_discovered,
  44. is_mp2_active,
  45. invalid_cmd = 0xF,
  46. };
  47. enum speed_enum {
  48. speed100k = 0,
  49. speed400k = 1,
  50. speed1000k = 2,
  51. speed1400k = 3,
  52. speed3400k = 4
  53. };
  54. enum mem_type {
  55. use_dram = 0,
  56. use_c2pmsg = 1,
  57. };
  58. /**
  59. * union i2c_cmd_base : bit access of C2P commands
  60. * @i2c_cmd: bit 0..3 i2c R/W command
  61. * @bus_id: bit 4..7 i2c bus index
  62. * @slave_addr: bit 8..15 slave address
  63. * @length: bit 16..27 read/write length
  64. * @i2c_speed: bit 28..30 bus speed
  65. * @mem_type: bit 31 0-DRAM; 1-C2P msg o/p
  66. */
  67. union i2c_cmd_base {
  68. u32 ul;
  69. struct {
  70. enum i2c_cmd i2c_cmd : 4;
  71. u8 bus_id : 4;
  72. u32 slave_addr : 8;
  73. u32 length : 12;
  74. enum speed_enum i2c_speed : 3;
  75. enum mem_type mem_type : 1;
  76. } s;
  77. };
  78. enum response_type {
  79. invalid_response = 0,
  80. command_success = 1,
  81. command_failed = 2,
  82. };
  83. enum status_type {
  84. i2c_readcomplete_event = 0,
  85. i2c_readfail_event = 1,
  86. i2c_writecomplete_event = 2,
  87. i2c_writefail_event = 3,
  88. i2c_busenable_complete = 4,
  89. i2c_busenable_failed = 5,
  90. i2c_busdisable_complete = 6,
  91. i2c_busdisable_failed = 7,
  92. invalid_data_length = 8,
  93. invalid_slave_address = 9,
  94. invalid_i2cbus_id = 10,
  95. invalid_dram_addr = 11,
  96. invalid_command = 12,
  97. mp2_active = 13,
  98. numberof_sensors_discovered_resp = 14,
  99. i2c_bus_notinitialized
  100. };
  101. /**
  102. * union i2c_event : bit access of P2C events
  103. * @response: bit 0..1 i2c response type
  104. * @status: bit 2..6 status_type
  105. * @mem_type: bit 7 0-DRAM; 1-C2P msg o/p
  106. * @bus_id: bit 8..11 i2c bus id
  107. * @length: bit 12..23 message length
  108. * @slave_addr: bit 24-31 slave address
  109. */
  110. union i2c_event {
  111. u32 ul;
  112. struct {
  113. enum response_type response : 2;
  114. enum status_type status : 5;
  115. enum mem_type mem_type : 1;
  116. u8 bus_id : 4;
  117. u32 length : 12;
  118. u32 slave_addr : 8;
  119. } r;
  120. };
  121. /**
  122. * struct amd_i2c_common - per bus/i2c adapter context, shared
  123. * between the pci and the platform driver
  124. * @eventval: MP2 event value set by the IRQ handler
  125. * @mp2_dev: MP2 pci device this adapter is part of
  126. * @msg: i2c message
  127. * @cmd_completion: function called by the IRQ handler to signal
  128. * the platform driver
  129. * @reqcmd: requested i2c command type
  130. * @cmd_success: set to true if the MP2 responded to a command with
  131. * the expected status and response type
  132. * @bus_id: bus index
  133. * @i2c_speed: i2c bus speed determined by the slowest slave
  134. * @dma_buf: if msg length > 32, holds the DMA buffer virtual address
  135. * @dma_addr: if msg length > 32, holds the DMA buffer address
  136. */
  137. struct amd_i2c_common {
  138. union i2c_event eventval;
  139. struct amd_mp2_dev *mp2_dev;
  140. struct i2c_msg *msg;
  141. void (*cmd_completion)(struct amd_i2c_common *i2c_common);
  142. enum i2c_cmd reqcmd;
  143. u8 cmd_success;
  144. u8 bus_id;
  145. enum speed_enum i2c_speed;
  146. u8 *dma_buf;
  147. dma_addr_t dma_addr;
  148. #ifdef CONFIG_PM
  149. int (*suspend)(struct amd_i2c_common *i2c_common);
  150. int (*resume)(struct amd_i2c_common *i2c_common);
  151. #endif /* CONFIG_PM */
  152. };
  153. /**
  154. * struct amd_mp2_dev - per PCI device context
  155. * @pci_dev: PCI driver node
  156. * @busses: MP2 devices may have up to two busses,
  157. * each bus corresponding to an i2c adapter
  158. * @mmio: iommapped registers
  159. * @c2p_lock: controls access to the C2P mailbox shared between
  160. * the two adapters
  161. * @c2p_lock_busid: id of the adapter which locked c2p_lock
  162. */
  163. struct amd_mp2_dev {
  164. struct pci_dev *pci_dev;
  165. struct amd_i2c_common *busses[2];
  166. void __iomem *mmio;
  167. struct mutex c2p_lock;
  168. u8 c2p_lock_busid;
  169. unsigned int probed;
  170. };
  171. /* PCIe communication driver */
  172. int amd_mp2_rw(struct amd_i2c_common *i2c_common, enum i2c_cmd reqcmd);
  173. int amd_mp2_bus_enable_set(struct amd_i2c_common *i2c_common, bool enable);
  174. void amd_mp2_process_event(struct amd_i2c_common *i2c_common);
  175. void amd_mp2_rw_timeout(struct amd_i2c_common *i2c_common);
  176. int amd_mp2_register_cb(struct amd_i2c_common *i2c_common);
  177. int amd_mp2_unregister_cb(struct amd_i2c_common *i2c_common);
  178. struct amd_mp2_dev *amd_mp2_find_device(void);
  179. static inline void amd_mp2_pm_runtime_get(struct amd_mp2_dev *mp2_dev)
  180. {
  181. pm_runtime_get_sync(&mp2_dev->pci_dev->dev);
  182. }
  183. static inline void amd_mp2_pm_runtime_put(struct amd_mp2_dev *mp2_dev)
  184. {
  185. pm_runtime_mark_last_busy(&mp2_dev->pci_dev->dev);
  186. pm_runtime_put_autosuspend(&mp2_dev->pci_dev->dev);
  187. }
  188. #endif