mkrtwtbl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Host tool: turn the vendored RTL8821A tables (third_party/rtw88) into
  2. * firmware/rtl8821a.tbl, which the kernel driver (rtl8821au.c) reads from
  3. * the pack. Keeping ~11 KB of register tables out of the kernel image keeps
  4. * _end well below the 0x90000 wall.
  5. *
  6. * Format (little endian):
  7. * "RTWT", u32 version (1), u32 section count, then per section
  8. * u32 id, u32 byte length, data (already a multiple of 4 bytes).
  9. * RTW_TBL_MAC/AGC/BB/RF_A: u32 (addr, data) pairs exactly as in Linux,
  10. * including the condition words (bit 31 / bit 30 headers).
  11. * RTW_TBL_PWR_*: 8-byte commands {u16 offset, cut_mask, intf_mask, base,
  12. * cmd, mask, value}; a flow's sub-sequences are concatenated and end with
  13. * one RTW_PWR_CMD_END entry. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "rtl8821a_tbl.h"
  18. typedef unsigned int u32;
  19. typedef unsigned short u16;
  20. typedef unsigned char u8;
  21. #define BIT(n) (1u << (n))
  22. struct rtw_pwr_seq_cmd {
  23. u16 offset;
  24. u8 cut_mask;
  25. u8 intf_mask;
  26. u8 base:4;
  27. u8 cmd:4;
  28. u8 mask;
  29. u8 value;
  30. };
  31. #define RTW_PWR_ADDR_MAC 0x00
  32. #define RTW_PWR_ADDR_USB 0x01
  33. #define RTW_PWR_ADDR_PCIE 0x02
  34. #define RTW_PWR_ADDR_SDIO 0x03
  35. #define RTW_PWR_INTF_SDIO_MSK BIT(0)
  36. #define RTW_PWR_INTF_USB_MSK BIT(1)
  37. #define RTW_PWR_INTF_PCI_MSK BIT(2)
  38. #define RTW_PWR_INTF_ALL_MSK (BIT(0) | BIT(1) | BIT(2) | BIT(3))
  39. #define RTW_PWR_CUT_TEST_MSK BIT(0)
  40. #define RTW_PWR_CUT_A_MSK BIT(1)
  41. #define RTW_PWR_CUT_B_MSK BIT(2)
  42. #define RTW_PWR_CUT_C_MSK BIT(3)
  43. #define RTW_PWR_CUT_D_MSK BIT(4)
  44. #define RTW_PWR_CUT_E_MSK BIT(5)
  45. #define RTW_PWR_CUT_F_MSK BIT(6)
  46. #define RTW_PWR_CUT_G_MSK BIT(7)
  47. #define RTW_PWR_CUT_ALL_MSK 0xFF
  48. #define RTW_PWR_DELAY_US RTL_PWR_DELAY_US
  49. #define RTW_PWR_DELAY_MS RTL_PWR_DELAY_MS
  50. #define RTW_PWR_CMD_READ RTL_PWR_CMD_READ
  51. #define RTW_PWR_CMD_WRITE RTL_PWR_CMD_WRITE
  52. #define RTW_PWR_CMD_POLLING RTL_PWR_CMD_POLLING
  53. #define RTW_PWR_CMD_DELAY RTL_PWR_CMD_DELAY
  54. #define RTW_PWR_CMD_END RTL_PWR_CMD_END
  55. #include "third_party/rtw88/rtw8821a_table.inc"
  56. #define N(a) (sizeof(a) / sizeof((a)[0]))
  57. static unsigned char out[65536];
  58. static unsigned int olen, nsect;
  59. static void put32(u32 v)
  60. {
  61. if (olen + 4 > sizeof(out)) {
  62. fprintf(stderr, "mkrtwtbl: output too large\n");
  63. exit(1);
  64. }
  65. out[olen++] = v;
  66. out[olen++] = v >> 8;
  67. out[olen++] = v >> 16;
  68. out[olen++] = v >> 24;
  69. }
  70. static void sect_words(u32 id, const u32 *w, unsigned int n)
  71. {
  72. unsigned int i;
  73. put32(id);
  74. put32(n * 4);
  75. for (i = 0; i < n; i++)
  76. put32(w[i]);
  77. nsect++;
  78. }
  79. static void put_cmd(const struct rtw_pwr_seq_cmd *c)
  80. {
  81. put32(c->offset | ((u32)c->cut_mask << 16) | ((u32)c->intf_mask << 24));
  82. put32(c->base | ((u32)c->cmd << 8) | ((u32)c->mask << 16) |
  83. ((u32)c->value << 24));
  84. }
  85. static void sect_flow(u32 id, const struct rtw_pwr_seq_cmd *const *flow)
  86. {
  87. static const struct rtw_pwr_seq_cmd end = { 0xffff, 0, 0, 0,
  88. RTL_PWR_CMD_END, 0, 0 };
  89. unsigned int n = 0, at;
  90. const struct rtw_pwr_seq_cmd *c;
  91. put32(id);
  92. at = olen;
  93. put32(0);
  94. for (; *flow; flow++)
  95. for (c = *flow; c->cmd != RTL_PWR_CMD_END; c++, n++)
  96. put_cmd(c);
  97. put_cmd(&end);
  98. n++;
  99. out[at] = (n * 8);
  100. out[at + 1] = (n * 8) >> 8;
  101. out[at + 2] = (n * 8) >> 16;
  102. out[at + 3] = 0;
  103. nsect++;
  104. }
  105. int main(int argc, char **argv)
  106. {
  107. FILE *f;
  108. if (argc != 2) {
  109. fprintf(stderr, "usage: mkrtwtbl out.tbl\n");
  110. return 1;
  111. }
  112. put32(RTL_TBL_MAGIC);
  113. put32(RTL_TBL_VERSION);
  114. put32(0); /* patched below */
  115. sect_words(RTL_TBL_MAC, rtw8821a_mac, N(rtw8821a_mac));
  116. sect_words(RTL_TBL_AGC, rtw8821a_agc, N(rtw8821a_agc));
  117. sect_words(RTL_TBL_BB, rtw8821a_bb, N(rtw8821a_bb));
  118. sect_words(RTL_TBL_RF_A, rtw8821a_rf_a, N(rtw8821a_rf_a));
  119. sect_flow(RTL_TBL_PWR_ON, card_enable_flow_8821a);
  120. sect_flow(RTL_TBL_PWR_OFF, card_disable_flow_8821a);
  121. sect_flow(RTL_TBL_PWR_LPS, enter_lps_flow_8821a);
  122. out[8] = nsect;
  123. f = fopen(argv[1], "wb");
  124. if (!f || fwrite(out, 1, olen, f) != olen || fclose(f) != 0) {
  125. perror(argv[1]);
  126. return 1;
  127. }
  128. return 0;
  129. }