/* Host tool: turn the vendored RTL8821A tables (third_party/rtw88) into * firmware/rtl8821a.tbl, which the kernel driver (rtl8821au.c) reads from * the pack. Keeping ~11 KB of register tables out of the kernel image keeps * _end well below the 0x90000 wall. * * Format (little endian): * "RTWT", u32 version (1), u32 section count, then per section * u32 id, u32 byte length, data (already a multiple of 4 bytes). * RTW_TBL_MAC/AGC/BB/RF_A: u32 (addr, data) pairs exactly as in Linux, * including the condition words (bit 31 / bit 30 headers). * RTW_TBL_PWR_*: 8-byte commands {u16 offset, cut_mask, intf_mask, base, * cmd, mask, value}; a flow's sub-sequences are concatenated and end with * one RTW_PWR_CMD_END entry. */ #include #include #include #include "rtl8821a_tbl.h" typedef unsigned int u32; typedef unsigned short u16; typedef unsigned char u8; #define BIT(n) (1u << (n)) struct rtw_pwr_seq_cmd { u16 offset; u8 cut_mask; u8 intf_mask; u8 base:4; u8 cmd:4; u8 mask; u8 value; }; #define RTW_PWR_ADDR_MAC 0x00 #define RTW_PWR_ADDR_USB 0x01 #define RTW_PWR_ADDR_PCIE 0x02 #define RTW_PWR_ADDR_SDIO 0x03 #define RTW_PWR_INTF_SDIO_MSK BIT(0) #define RTW_PWR_INTF_USB_MSK BIT(1) #define RTW_PWR_INTF_PCI_MSK BIT(2) #define RTW_PWR_INTF_ALL_MSK (BIT(0) | BIT(1) | BIT(2) | BIT(3)) #define RTW_PWR_CUT_TEST_MSK BIT(0) #define RTW_PWR_CUT_A_MSK BIT(1) #define RTW_PWR_CUT_B_MSK BIT(2) #define RTW_PWR_CUT_C_MSK BIT(3) #define RTW_PWR_CUT_D_MSK BIT(4) #define RTW_PWR_CUT_E_MSK BIT(5) #define RTW_PWR_CUT_F_MSK BIT(6) #define RTW_PWR_CUT_G_MSK BIT(7) #define RTW_PWR_CUT_ALL_MSK 0xFF #define RTW_PWR_DELAY_US RTL_PWR_DELAY_US #define RTW_PWR_DELAY_MS RTL_PWR_DELAY_MS #define RTW_PWR_CMD_READ RTL_PWR_CMD_READ #define RTW_PWR_CMD_WRITE RTL_PWR_CMD_WRITE #define RTW_PWR_CMD_POLLING RTL_PWR_CMD_POLLING #define RTW_PWR_CMD_DELAY RTL_PWR_CMD_DELAY #define RTW_PWR_CMD_END RTL_PWR_CMD_END #include "third_party/rtw88/rtw8821a_table.inc" #define N(a) (sizeof(a) / sizeof((a)[0])) static unsigned char out[65536]; static unsigned int olen, nsect; static void put32(u32 v) { if (olen + 4 > sizeof(out)) { fprintf(stderr, "mkrtwtbl: output too large\n"); exit(1); } out[olen++] = v; out[olen++] = v >> 8; out[olen++] = v >> 16; out[olen++] = v >> 24; } static void sect_words(u32 id, const u32 *w, unsigned int n) { unsigned int i; put32(id); put32(n * 4); for (i = 0; i < n; i++) put32(w[i]); nsect++; } static void put_cmd(const struct rtw_pwr_seq_cmd *c) { put32(c->offset | ((u32)c->cut_mask << 16) | ((u32)c->intf_mask << 24)); put32(c->base | ((u32)c->cmd << 8) | ((u32)c->mask << 16) | ((u32)c->value << 24)); } static void sect_flow(u32 id, const struct rtw_pwr_seq_cmd *const *flow) { static const struct rtw_pwr_seq_cmd end = { 0xffff, 0, 0, 0, RTL_PWR_CMD_END, 0, 0 }; unsigned int n = 0, at; const struct rtw_pwr_seq_cmd *c; put32(id); at = olen; put32(0); for (; *flow; flow++) for (c = *flow; c->cmd != RTL_PWR_CMD_END; c++, n++) put_cmd(c); put_cmd(&end); n++; out[at] = (n * 8); out[at + 1] = (n * 8) >> 8; out[at + 2] = (n * 8) >> 16; out[at + 3] = 0; nsect++; } int main(int argc, char **argv) { FILE *f; if (argc != 2) { fprintf(stderr, "usage: mkrtwtbl out.tbl\n"); return 1; } put32(RTL_TBL_MAGIC); put32(RTL_TBL_VERSION); put32(0); /* patched below */ sect_words(RTL_TBL_MAC, rtw8821a_mac, N(rtw8821a_mac)); sect_words(RTL_TBL_AGC, rtw8821a_agc, N(rtw8821a_agc)); sect_words(RTL_TBL_BB, rtw8821a_bb, N(rtw8821a_bb)); sect_words(RTL_TBL_RF_A, rtw8821a_rf_a, N(rtw8821a_rf_a)); sect_flow(RTL_TBL_PWR_ON, card_enable_flow_8821a); sect_flow(RTL_TBL_PWR_OFF, card_disable_flow_8821a); sect_flow(RTL_TBL_PWR_LPS, enter_lps_flow_8821a); out[8] = nsect; f = fopen(argv[1], "wb"); if (!f || fwrite(out, 1, olen, f) != olen || fclose(f) != 0) { perror(argv[1]); return 1; } return 0; }