| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784 |
- /* Realtek RTL8821AU USB WiFi: see rtl8821au.h.
- *
- * Bring-up follows Linux rtw88 (rtw88xxa_power_on() and friends) step by
- * step; the comments name the Linux function a block comes from. The
- * register tables and power sequences are not in the kernel image: they are
- * read from /ramfs/firmware/rtl8821a.tbl (built by mkrtwtbl.c from
- * third_party/rtw88), the 8051 firmware from /ramfs/firmware/rtw8821a_fw.bin.
- *
- * USB: registers are vendor control requests (bRequest 5, wValue = address).
- * Every TX queue is mapped to one DMA channel and therefore one bulk-OUT
- * endpoint, because the xHCI driver keeps a single bulk-OUT ring and would
- * reconfigure it on every endpoint switch. RX aggregation is off, so one
- * bulk-IN transfer carries one packet (the parser still walks several).
- *
- * Page-allocated buffers only: the kernel image must stay below 0x90000. */
- #include "rtl8821au.h"
- #include "rtl8821a_tbl.h"
- #include "ccmp.h"
- #include "mm.h"
- #include "pci.h"
- #include "ramfs.h"
- #include "serial.h"
- #include "timer.h"
- #include "usb.h"
- #include "wifi.h"
- #define FW_PATH "firmware/rtw8821a_fw.bin"
- #define TBL_PATH "firmware/rtl8821a.tbl"
- /* ---- registers (Linux rtw88 reg.h) -------------------------------------- */
- #define REG_SYS_FUNC_EN 0x0002
- #define REG_APS_FSMCO 0x0004
- #define REG_SYS_CLKR 0x0008
- #define REG_RSV_CTRL 0x001C
- #define REG_RF_CTRL 0x001F
- #define REG_AFE_CTRL3 0x002C
- #define REG_EFUSE_CTRL 0x0030
- #define REG_LDO_EFUSE_CTRL 0x0034
- #define REG_ACLK_MON 0x003E
- #define REG_GPIO_MUXCFG 0x0040
- #define REG_LED_CFG 0x004C
- #define REG_WL_BT_PWR_CTRL 0x0068
- #define REG_SYS_SDIO_CTRL 0x0070
- #define REG_RF_B_CTRL 0x0076
- #define REG_LDO_SWR_CTRL 0x007C
- #define REG_MCUFW_CTRL 0x0080
- #define REG_HIMR0 0x00B0
- #define REG_HIMR1 0x00B8
- #define REG_EFUSE_ACCESS 0x00CF
- #define REG_SYS_CFG1 0x00F0
- #define REG_CR 0x0100
- #define REG_TXDMA_PQ_MAP 0x010C
- #define REG_TRXFF_BNDY 0x0114
- #define REG_HMETFR 0x01CC
- #define REG_LLT_INIT 0x01E0
- #define REG_RQPN 0x0200
- #define REG_DWBCN0_CTRL 0x0208
- #define REG_TXDMA_OFFSET_CHK 0x020C
- #define REG_RQPN_NPQ 0x0214
- #define REG_DWBCN1_CTRL 0x0228
- #define REG_RXDMA_STATUS 0x0288
- #define REG_RXDMA_MODE 0x0290
- #define REG_EARLY_MODE_CONTROL 0x02BC
- #define REG_TXPKT_EMPTY 0x041A
- #define REG_FWHW_TXQ_CTRL 0x0420
- #define REG_HWSEQ_CTRL 0x0423
- #define REG_BCNQ_BDNY 0x0424
- #define REG_MGQ_BDNY 0x0425
- #define REG_SPEC_SIFS 0x0428
- #define REG_RETRY_LIMIT 0x042A
- #define REG_RRSR 0x0440
- #define REG_ARFR0 0x0444
- #define REG_ARFRH0 0x0448
- #define REG_ARFR1_V1 0x044C
- #define REG_ARFRH1_V1 0x0450
- #define REG_CCK_CHECK 0x0454
- #define REG_AMPDU_MAX_TIME 0x0456
- #define REG_AMPDU_MAX_LENGTH 0x0458
- #define REG_WMAC_LBK_BF_HD 0x045D
- #define REG_FAST_EDCA_CTRL 0x0460
- #define REG_DATA_SC 0x0483
- #define REG_ARFR2_V1 0x048C
- #define REG_ARFRH2_V1 0x0490
- #define REG_ARFR3_V1 0x0494
- #define REG_ARFRH3_V1 0x0498
- #define REG_QUEUE_CTRL 0x04C6
- #define REG_SINGLE_AMPDU_CTRL 0x04C7
- #define REG_MAX_AGGR_NUM 0x04CA
- #define REG_BAR_MODE_CTRL 0x04CC
- #define REG_TX_RPT_TIME 0x04F0
- #define REG_EDCA_VO_PARAM 0x0500
- #define REG_EDCA_VI_PARAM 0x0504
- #define REG_EDCA_BE_PARAM 0x0508
- #define REG_EDCA_BK_PARAM 0x050C
- #define REG_BCNTCFG 0x0510
- #define REG_PIFS 0x0512
- #define REG_SIFS 0x0514
- #define REG_TBTT_PROHIBIT 0x0540
- #define REG_BCN_CTRL 0x0550
- #define REG_DRVERLYINT 0x0558
- #define REG_BCNDMATIM 0x0559
- #define REG_USTIME_TSF 0x055C
- #define REG_BCN_MAX_ERR 0x055D
- #define REG_RCR 0x0608
- #define REG_RX_PKT_LIMIT 0x060C
- #define REG_RX_DRVINFO_SZ 0x060F
- #define REG_MACID 0x0610
- #define REG_BSSID 0x0618
- #define REG_MAR 0x0620
- #define REG_USTIME_EDCA 0x0638
- #define REG_MAC_SPEC_SIFS 0x063A
- #define REG_ACKTO 0x0640
- #define REG_NAV_CTRL 0x0650
- #define REG_WMAC_TRXPTCL_CTL 0x0668
- #define REG_SEC_CMD 0x0670
- #define REG_RXFLTMAP0 0x06A0
- #define REG_RXFLTMAP1 0x06A2
- #define REG_RXFLTMAP2 0x06A4
- #define REG_AID 0x06A8
- #define REG_USB3_RXITV 0xF050
- #define REG_USB_HRPWM 0xFE58
- /* baseband / RF (Jaguar) */
- #define REG_RXPSEL 0x0808
- #define REG_TXPSEL 0x080C
- #define REG_L1PKTH 0x0848
- #define REG_CLKTRK 0x0860
- #define REG_ADCCLK 0x08AC
- #define REG_HSSI_READ 0x08B0
- #define REG_FPGA0_XCD_RF_PARA 0x08B4
- #define REG_ADC160 0x08C4
- #define REG_CCK_RX 0x0A04
- #define REG_3WIRE_SWA 0x0C00
- #define REG_TXSCALE_A 0x0C1C
- #define REG_TXAGC_A_CCK 0x0C20
- #define REG_RXIGI_A 0x0C50
- #define REG_TX_PWR_TRAINING_A 0x0C54
- #define REG_LSSI_WRITE_A 0x0C90
- #define REG_RFE_PINMUX_A 0x0CB0
- #define REG_RFE_INV_A 0x0CB4
- #define REG_PI_READ_A 0x0D04
- #define REG_SI_READ_A 0x0D08
- #define REG_TXSCALE_B 0x0E1C
- #define BIT(n) (1u << (n))
- #define RCR_DEFAULT (BIT(31) | BIT(30) | BIT(29) | BIT(20) | BIT(14) | \
- BIT(28) | BIT(3) | BIT(2) | BIT(1)) /* APP_FCS/MIC/ICV,
- PKTCTL_DLEN, HTC_LOC_CTRL, APP_PHYSTS, AB, AM, APM */
- #define RCR_CBSSID_DATA BIT(6)
- #define RCR_CBSSID_BCN BIT(7)
- #define FW_READY_LEGACY 0xC6 /* RAM_DL_SEL|WINTINI_RDY|CHK_RPT|RDY */
- /* TX descriptor queue selects and rates (rtw88 tx.h/main.h) */
- #define QSEL_BE 0
- #define QSEL_MGMT 18
- #define RATE_1M 0x00
- #define RATE_6M 0x04
- #define RATE_12M 0x06
- #define RATEID_BG 6
- #define RATEID_G 7
- #define RATEID_B 8
- #define TXDESC_SZ 40
- #define RXDESC_SZ 24
- #define RX_BUF_SZ 8192
- #define TX_BUF_SZ 4096
- /* DMA mappings (rtw88 main.h enum rtw_dma_mapping) */
- #define DMA_LOW 1
- #define DMA_NORMAL 2
- /* Scan dwell times (ms). */
- #define DWELL_ACTIVE 70
- #define DWELL_PASSIVE 120
- static const unsigned char scan_chans[] = {
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
- 36, 40, 44, 48, 52, 56, 60, 64,
- 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
- 149, 153, 157, 161, 165,
- };
- /* Supported USB IDs: Linux rtw8821au.c. */
- static const unsigned short usb_ids[][2] = {
- { 0x0bda, 0x0811 }, { 0x0bda, 0x0820 }, { 0x0bda, 0x0821 },
- { 0x0bda, 0x8822 }, { 0x0bda, 0x0823 }, { 0x0bda, 0xa811 },
- { 0x0411, 0x0242 }, { 0x0411, 0x029b }, { 0x04bb, 0x0953 },
- { 0x056e, 0x4007 }, { 0x056e, 0x400e }, { 0x056e, 0x400f },
- { 0x0846, 0x9052 }, { 0x0e66, 0x0023 }, { 0x2001, 0x3314 },
- { 0x2001, 0x3318 }, { 0x2019, 0xab32 }, { 0x20f4, 0x804b },
- { 0x2357, 0x011e }, { 0x2357, 0x011f }, { 0x2357, 0x0120 },
- { 0x3823, 0x6249 }, { 0x7392, 0xa811 }, { 0x7392, 0xa812 },
- { 0x7392, 0xa813 }, { 0x7392, 0xb611 },
- };
- static struct {
- /* USB */
- int present;
- unsigned int ctrl, addr, mps, port, gen;
- int low;
- unsigned int vendor, product;
- unsigned int ep_in, ep_in_mps, ep_out[4], n_out, ep_out_mps;
- unsigned int ep_tx; /* the one bulk-OUT endpoint used */
- unsigned int io_errs;
- /* chip */
- int up, fatal;
- unsigned int cut;
- unsigned char *io; /* control-transfer data (page) */
- unsigned char *efuse; /* 512-byte logical map (page) */
- unsigned char *rxbuf; /* 8 KB, inside 16 KB of pages */
- unsigned char *txbuf; /* one page */
- const unsigned char *tbl;
- unsigned int tbl_size;
- unsigned char mac[6];
- unsigned int rfe_cond; /* rtw_phy_setup_phy_cond() rfe */
- unsigned int bb_swing_2g, bb_swing_5g, crystal;
- unsigned int fw_ver, fw_sub;
- char fw_name[24];
- const char *state;
- /* radio */
- unsigned int chan;
- int band5;
- /* scan */
- int scanning;
- unsigned int scan_idx, scan_next;
- /* link */
- unsigned char bssid[6];
- unsigned char tk[16];
- int have_tk;
- unsigned int rx_pkts, rx_crc, tx_pkts, tx_errs;
- int last_rssi;
- } R;
- /* ---- small helpers ------------------------------------------------------- */
- static void rmemcpy(void *d, const void *s, unsigned int n)
- {
- unsigned char *dd = d;
- const unsigned char *ss = s;
- while (n--)
- *dd++ = *ss++;
- }
- static void rmemzero(void *d, unsigned int n)
- {
- unsigned char *dd = d;
- while (n--)
- *dd++ = 0;
- }
- static void udelay(unsigned int us)
- {
- while (us--)
- port_outb(0x80, 0);
- }
- /* Busy-wait: SYS_WIFI runs with interrupts off (syscall_stub.S), so the
- * millisecond tick does not advance inside a call. */
- static void mdelay(unsigned int ms)
- {
- while (ms--)
- udelay(1000);
- }
- static void rlog(const char *s)
- {
- serial_puts(SERIAL_COM1, s);
- }
- static void loghex(const char *s, unsigned int v)
- {
- serial_puts(SERIAL_COM1, s);
- serial_puthex(SERIAL_COM1, v);
- serial_puts(SERIAL_COM1, "\n");
- }
- static unsigned int le32(const unsigned char *p)
- {
- return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
- }
- static void put32(unsigned char *p, unsigned int v)
- {
- p[0] = v; p[1] = v >> 8; p[2] = v >> 16; p[3] = v >> 24;
- }
- static unsigned int ffs32(unsigned int m)
- {
- unsigned int s = 0;
- while (m && !(m & 1)) {
- m >>= 1;
- s++;
- }
- return s;
- }
- /* ---- register access (rtw_usb_read/rtw_usb_write) ------------------------ */
- static void io_fail(void)
- {
- if (++R.io_errs > 16 && !R.fatal) {
- rlog("rtl8821au: too many USB errors, device gone?\n");
- R.fatal = 1;
- }
- }
- static unsigned int rd(unsigned int a, unsigned int len)
- {
- int n;
- if (R.fatal)
- return 0xffffffffu;
- R.io[0] = R.io[1] = R.io[2] = R.io[3] = 0;
- n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0xC0, 0x05,
- (unsigned short)a, 0, R.io, len);
- if (n != (int)len) {
- io_fail();
- return 0xffffffffu;
- }
- R.io_errs = 0;
- return le32(R.io);
- }
- static void wr(unsigned int a, unsigned int v, unsigned int len)
- {
- int n;
- if (R.fatal)
- return;
- put32(R.io, v);
- n = usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
- (unsigned short)a, 0, R.io, len);
- if (n < 0)
- io_fail();
- else
- R.io_errs = 0;
- }
- static unsigned int rd8(unsigned int a) { return rd(a, 1) & 0xff; }
- static unsigned int rd16(unsigned int a) { return rd(a, 2) & 0xffff; }
- static unsigned int rd32(unsigned int a) { return rd(a, 4); }
- static void wr8(unsigned int a, unsigned int v) { wr(a, v & 0xff, 1); }
- static void wr16(unsigned int a, unsigned int v) { wr(a, v & 0xffff, 2); }
- static void wr32(unsigned int a, unsigned int v) { wr(a, v, 4); }
- static void set8(unsigned int a, unsigned int b) { wr8(a, rd8(a) | b); }
- static void clr8(unsigned int a, unsigned int b) { wr8(a, rd8(a) & ~b); }
- static void set16(unsigned int a, unsigned int b) { wr16(a, rd16(a) | b); }
- static void clr16(unsigned int a, unsigned int b) { wr16(a, rd16(a) & ~b); }
- static void set32(unsigned int a, unsigned int b) { wr32(a, rd32(a) | b); }
- static void wr32m(unsigned int a, unsigned int mask, unsigned int v)
- {
- unsigned int s = ffs32(mask);
- wr32(a, (rd32(a) & ~mask) | ((v << s) & mask));
- }
- static unsigned int rd32m(unsigned int a, unsigned int mask)
- {
- return (rd32(a) & mask) >> ffs32(mask);
- }
- /* Bulk write of a block (firmware download), 196 bytes per request. */
- static int wr_block(unsigned int a, const unsigned char *p, unsigned int n)
- {
- if (R.fatal)
- return -1;
- if (usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
- (unsigned short)a, 0, p, n) < 0) {
- io_fail();
- return -1;
- }
- return 0;
- }
- /* ---- RF access (rtw88xxa_phy_read_rf / rtw_phy_write_rf_reg_sipi) ------ */
- #define RFREG_MASK 0xfffffu
- static unsigned int rf_read(unsigned int addr, unsigned int mask)
- {
- unsigned int pi;
- addr &= 0xff;
- pi = rd32m(REG_3WIRE_SWA, 0x4);
- wr32m(REG_HSSI_READ, 0xff, addr);
- udelay(20);
- return rd32m(pi ? REG_PI_READ_A : REG_SI_READ_A, mask);
- }
- static void rf_write(unsigned int addr, unsigned int mask, unsigned int data)
- {
- addr &= 0xff;
- mask &= RFREG_MASK;
- if (mask != RFREG_MASK)
- data = (rf_read(addr, RFREG_MASK) & ~mask) |
- ((data << ffs32(mask)) & mask);
- wr32(REG_LSSI_WRITE_A, ((addr << 20) | (data & 0xfffff)) & 0x0fffffff);
- udelay(13);
- }
- /* ---- table blob ---------------------------------------------------------- */
- static const unsigned char *tbl_section(unsigned int id, unsigned int *len)
- {
- unsigned int off = 12, n, i, sid, slen;
- if (!R.tbl)
- return 0;
- n = le32(R.tbl + 8);
- for (i = 0; i < n && off + 8 <= R.tbl_size; i++) {
- sid = le32(R.tbl + off);
- slen = le32(R.tbl + off + 4);
- if (off + 8 + slen > R.tbl_size)
- return 0;
- if (sid == id) {
- *len = slen;
- return R.tbl + off + 8;
- }
- off += 8 + slen;
- }
- return 0;
- }
- static int load_blobs(void)
- {
- unsigned int size;
- const unsigned char *t = ramfs_lookup(TBL_PATH, &size);
- if (!t || size < 12 || le32(t) != RTL_TBL_MAGIC ||
- le32(t + 4) != RTL_TBL_VERSION) {
- R.state = "missing /ramfs/" TBL_PATH;
- return -1;
- }
- R.tbl = t;
- R.tbl_size = size;
- return 0;
- }
- /* rtw_pwr_seq_parser(): USB interface mask, cut mask from the chip. */
- static int pwr_seq(unsigned int id)
- {
- unsigned int len, i, cut_mask = 1u << (R.cut + 1);
- const unsigned char *p = tbl_section(id, &len);
- if (!p)
- return -1;
- for (i = 0; i + 8 <= len; i += 8) {
- unsigned int off = p[i] | (p[i + 1] << 8);
- unsigned int cm = p[i + 2], im = p[i + 3], cmd = p[i + 5];
- unsigned int mask = p[i + 6], val = p[i + 7], t;
- if (cmd == RTL_PWR_CMD_END)
- break;
- if (!(im & 0x02) || !(cm & cut_mask))
- continue;
- switch (cmd) {
- case RTL_PWR_CMD_WRITE:
- wr8(off, (rd8(off) & ~mask) | (val & mask));
- break;
- case RTL_PWR_CMD_POLLING:
- for (t = 0; t < 5000; t++) {
- if ((rd8(off) & mask) == (val & mask))
- break;
- udelay(50);
- }
- if (t == 5000) {
- loghex("rtl8821au: power poll timeout at ", off);
- return -1;
- }
- break;
- case RTL_PWR_CMD_DELAY:
- if (val == RTL_PWR_DELAY_US)
- udelay(off);
- else
- mdelay(off);
- break;
- default:
- break;
- }
- if (R.fatal)
- return -1;
- }
- return 0;
- }
- /* rtw_parse_tbl_phy_cond() + check_positive() for the 8812A/8821A rules. */
- static int cond_ok(unsigned int c, unsigned int c2)
- {
- unsigned int cut = (c >> 24) & 0xf, pkg = (c >> 12) & 0xf;
- unsigned int intf = (c >> 8) & 0xf, rfe = c & 0xff;
- unsigned int drv_cut = R.cut ? R.cut : 15;
- (void)c2; /* 8821A: glna/gpa/alna/apa types are all 0 */
- if (cut && cut != drv_cut)
- return 0;
- if (pkg && pkg != 15)
- return 0;
- if (intf && intf != 0x2) /* INTF_USB */
- return 0;
- if (!(rfe & 0x0f))
- return 1;
- if ((rfe & R.rfe_cond) != rfe)
- return 0;
- /* Any type check against the all-zero driver types fails unless the
- * table also asks for type 0. */
- if ((rfe & 1) && (c2 & 0xff))
- return 0;
- if ((rfe & 2) && ((c2 >> 8) & 0xff))
- return 0;
- if ((rfe & 4) && ((c2 >> 16) & 0xff))
- return 0;
- if ((rfe & 8) && ((c2 >> 24) & 0xff))
- return 0;
- return 1;
- }
- static void do_cfg(unsigned int id, unsigned int a, unsigned int v)
- {
- switch (id) {
- case RTL_TBL_MAC:
- wr8(a, v);
- break;
- case RTL_TBL_AGC:
- wr32(a, v);
- break;
- case RTL_TBL_BB:
- if (a == 0xfe)
- mdelay(50);
- else if (a == 0xfd)
- mdelay(5);
- else if (a == 0xfc)
- mdelay(1);
- else if (a == 0xfb)
- udelay(60);
- else if (a == 0xfa)
- udelay(5);
- else if (a == 0xf9)
- udelay(1);
- else
- wr32(a, v);
- break;
- case RTL_TBL_RF_A:
- if (a == 0xffe)
- mdelay(50);
- else if (a == 0xfe)
- udelay(110);
- else {
- rf_write(a, RFREG_MASK, v);
- udelay(1);
- }
- break;
- }
- }
- static int load_table(unsigned int id)
- {
- unsigned int len, i, pos_c = 0, pos_c2 = 0;
- int matched = 1, skipped = 0;
- const unsigned char *p = tbl_section(id, &len);
- if (!p)
- return -1;
- for (i = 0; i + 8 <= len; i += 8) {
- unsigned int a = le32(p + i), v = le32(p + i + 4);
- if (a & BIT(31)) { /* pos: branch marker */
- switch ((a >> 28) & 3) {
- case 3: /* ENDIF */
- matched = 1;
- skipped = 0;
- break;
- case 2: /* ELSE */
- matched = !skipped;
- break;
- default: /* IF / ELIF */
- pos_c = a;
- pos_c2 = v;
- break;
- }
- } else if (a & BIT(30)) { /* neg: evaluate */
- if (!skipped) {
- if (cond_ok(pos_c, pos_c2)) {
- matched = 1;
- skipped = 1;
- } else {
- matched = 0;
- skipped = 0;
- }
- } else {
- matched = 0;
- }
- } else if (matched) {
- do_cfg(id, a, v);
- }
- if (R.fatal)
- return -1;
- }
- return 0;
- }
- /* ---- eFuse (rtw_dump_physical_efuse_map + logical map) ------------------ */
- static int read_efuse(void)
- {
- unsigned char *phy = R.rxbuf; /* scratch: 512 bytes */
- unsigned char *log_map = R.efuse;
- unsigned int addr, ctl, t, p, i;
- wr8(REG_EFUSE_ACCESS, 0x69);
- set16(REG_SYS_FUNC_EN, BIT(12));
- set16(REG_SYS_CLKR, BIT(5) | BIT(1));
- wr32m(REG_LDO_EFUSE_CTRL, BIT(8) | BIT(9), 0);
- ctl = rd32(REG_EFUSE_CTRL);
- for (addr = 0; addr < 512; addr++) {
- ctl &= ~(0xffu | (0x3ffu << 8));
- ctl |= addr << 8;
- wr32(REG_EFUSE_CTRL, ctl & ~BIT(31));
- for (t = 0; t < 1000; t++) {
- ctl = rd32(REG_EFUSE_CTRL);
- if (ctl & BIT(31))
- break;
- udelay(1);
- }
- if (t == 1000 || R.fatal) {
- wr8(REG_EFUSE_ACCESS, 0);
- return -1;
- }
- phy[addr] = ctl & 0xff;
- }
- wr8(REG_EFUSE_ACCESS, 0);
- for (i = 0; i < 512; i++)
- log_map[i] = 0xff;
- for (p = 0; p < 512;) {
- unsigned int h1 = phy[p], h2 = p + 1 < 512 ? phy[p + 1] : 0xff;
- unsigned int blk, wen, w;
- if (h1 == 0xff || ((h1 & 0x1f) == 0xf && h2 == 0xff))
- break;
- if ((h1 & 0x1f) == 0xf) {
- blk = ((h2 & 0xf0) >> 1) | ((h1 >> 5) & 7);
- wen = h2 & 0xf;
- p += 2;
- } else {
- blk = (h1 & 0xf0) >> 4;
- wen = h1 & 0xf;
- p += 1;
- }
- for (w = 0; w < 4; w++) {
- unsigned int li = (blk << 3) + (w << 1);
- if (wen & (1u << w))
- continue;
- if (p + 1 >= 512 || li + 1 >= 512)
- return -1;
- log_map[li] = phy[p];
- log_map[li + 1] = phy[p + 1];
- p += 2;
- }
- }
- return 0;
- }
- /* rtw88xxa_read_efuse() + rtw_chip_efuse_info_setup() fixups. */
- static void parse_efuse(void)
- {
- const unsigned char *m = R.efuse;
- unsigned int pa = m[0xbc], lna2 = m[0xbd], lna5 = m[0xbf];
- unsigned int ext_lna_2g, ext_pa_2g, ext_lna_5g, ext_pa_5g, btcoex;
- rmemcpy(R.mac, m + 0x107, 6);
- R.crystal = m[0xb9] == 0xff ? 0x20 : m[0xb9];
- R.bb_swing_2g = m[0xc6] == 0xff ? 0 : m[0xc6];
- R.bb_swing_5g = m[0xc7] == 0xff ? 0 : m[0xc7];
- if (pa == 0xff)
- pa = 0;
- if (lna2 == 0xff)
- lna2 = 0;
- if (lna5 == 0xff)
- lna5 = 0;
- ext_pa_2g = (pa & BIT(4)) ? 1 : 0;
- ext_lna_2g = (lna2 & BIT(3)) ? 1 : 0;
- ext_pa_5g = (pa & BIT(0)) ? 1 : 0;
- ext_lna_5g = (lna5 & BIT(3)) ? 1 : 0;
- btcoex = m[0xc1] != 0xff && (m[0xc1] & 0xe0) == 0x20;
- R.rfe_cond = ext_lna_2g | (ext_pa_2g << 1) | (ext_lna_5g << 2) |
- (ext_pa_5g << 3) | (btcoex << 4);
- if (R.mac[0] & 1 || (R.mac[0] | R.mac[1] | R.mac[2] | R.mac[3] |
- R.mac[4] | R.mac[5]) == 0) {
- /* No valid address programmed: a fixed locally administered
- * one, so the device is still usable. */
- static const unsigned char fallback[6] = {
- 0x02, 0x4e, 0x45, 0x42, 0x08, 0x21 };
- rmemcpy(R.mac, fallback, 6);
- }
- }
- /* ---- power / MAC init ---------------------------------------------------- */
- /* rtw88xxa_reset_8051() */
- static void reset_8051(void)
- {
- unsigned int v;
- clr8(REG_RSV_CTRL, BIT(1));
- clr8(REG_RSV_CTRL + 1, BIT(0));
- v = rd8(REG_SYS_FUNC_EN + 1);
- wr8(REG_SYS_FUNC_EN + 1, v & ~BIT(2));
- clr8(REG_RSV_CTRL, BIT(1));
- set8(REG_RSV_CTRL + 1, BIT(0));
- wr8(REG_SYS_FUNC_EN + 1, v | BIT(2));
- }
- /* rtw88xxau_hw_reset(): undo a previous run that was not shut down. */
- static void hw_reset(void)
- {
- unsigned int v, t;
- if (!(rd8(REG_MCUFW_CTRL) & BIT(7)))
- return;
- reset_8051();
- wr8(REG_MCUFW_CTRL, 0);
- set32(REG_FPGA0_XCD_RF_PARA, BIT(6));
- clr8(REG_SYS_FUNC_EN, BIT(0) | BIT(1));
- wr8(REG_RF_CTRL, 0);
- wr16(REG_CR, 0);
- set8(REG_APS_FSMCO + 1, BIT(1));
- for (t = 0; t < 5000; t++) {
- v = rd8(REG_APS_FSMCO + 1);
- if (!(v & BIT(1)))
- break;
- udelay(1);
- }
- wr8(REG_APS_FSMCO + 1, v | BIT(0));
- clr8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
- set8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
- }
- static int llt_write(unsigned int a, unsigned int d)
- {
- unsigned int n;
- wr32(REG_LLT_INIT, BIT(30) | (a << 8) | d);
- for (n = 0; n <= 20; n++)
- if (!(rd32(REG_LLT_INIT) & (BIT(31) | BIT(30))))
- return 0;
- return -1;
- }
- /* rtw88xxa_llt_init(): reserved pages at the top of the 256-page TX FIFO. */
- static int llt_init(unsigned int boundary)
- {
- unsigned int i;
- for (i = 0; i < boundary - 1; i++)
- if (llt_write(i, i + 1) < 0)
- return -1;
- if (llt_write(boundary - 1, 0xff) < 0)
- return -1;
- for (i = boundary; i < 255; i++)
- if (llt_write(i, i + 1) < 0)
- return -1;
- return llt_write(255, boundary);
- }
- static void cpu_enable(int on)
- {
- if (on) {
- set8(REG_RSV_CTRL + 1, BIT(0));
- set8(REG_SYS_FUNC_EN + 1, BIT(2));
- } else {
- clr8(REG_SYS_FUNC_EN + 1, BIT(2));
- clr8(REG_RSV_CTRL + 1, BIT(0));
- }
- }
- /* __rtw_download_firmware_legacy() with rtw_usb_write_firmware_page(). */
- static int download_fw(void)
- {
- unsigned int size, page, off, n, t, v;
- const unsigned char *fw = ramfs_lookup(FW_PATH, &size);
- if (!fw || size < 64) {
- R.state = "missing /ramfs/" FW_PATH;
- return -1;
- }
- R.fw_ver = fw[4] | (fw[5] << 8);
- R.fw_sub = fw[6];
- fw += 32; /* rtw_fw_hdr_legacy */
- size -= 32;
- cpu_enable(0);
- cpu_enable(1);
- set8(REG_MCUFW_CTRL, BIT(0)); /* MCUFWDL_EN */
- for (t = 0; t < 10 && !(rd8(REG_MCUFW_CTRL) & BIT(0)); t++) {
- set8(REG_MCUFW_CTRL, BIT(0));
- mdelay(20);
- }
- wr32(REG_MCUFW_CTRL, rd32(REG_MCUFW_CTRL) & ~BIT(19)); /* ROM_DLEN */
- set8(REG_MCUFW_CTRL, BIT(2)); /* clear FWDL_CHK_RPT */
- for (page = 0; page * 4096 < size; page++) {
- unsigned int psz = size - page * 4096;
- if (psz > 4096)
- psz = 4096;
- wr32m(REG_MCUFW_CTRL, 0x70000, page);
- for (off = 0; off < psz; off += n) {
- n = psz - off >= 196 ? 196 : (psz - off >= 8 ? 8 : 1);
- if (wr_block(0x1000 + off, fw + page * 4096 + off,
- n) < 0)
- return -1;
- }
- }
- for (t = 0; t < 1000; t++) {
- if (rd8(REG_MCUFW_CTRL) & BIT(2))
- break;
- udelay(50);
- }
- clr8(REG_MCUFW_CTRL, BIT(0));
- if (t == 1000) {
- R.state = "firmware download not acknowledged";
- return -1;
- }
- /* download_firmware_validate_legacy() */
- v = rd32(REG_MCUFW_CTRL);
- v |= BIT(1);
- v &= ~BIT(6);
- wr32(REG_MCUFW_CTRL, v);
- cpu_enable(0);
- cpu_enable(1);
- for (t = 0; t < 20; t++) {
- if ((rd32(REG_MCUFW_CTRL) & FW_READY_LEGACY) == FW_READY_LEGACY)
- return 0;
- mdelay(20);
- }
- R.state = "firmware did not start";
- return -1;
- }
- /* ---- channel / band (rtw88xxa_set_channel and helpers) ------------------- */
- /* rtw88xxa_get_bb_swing(): two bits per path in the eFuse byte. */
- static unsigned int bb_swing(int band5, int path_b)
- {
- static const unsigned int s2s[4] = { 0x200, 0x16a, 0x101, 0x0b6 };
- unsigned int v = band5 ? R.bb_swing_5g : R.bb_swing_2g;
- return s2s[(path_b ? v >> 2 : v) & 3];
- }
- static void switch_band(int band5)
- {
- unsigned int t;
- /* rtw8821a_set_ext_band_switch(): no BT coex, no antenna diversity */
- wr32m(REG_LED_CFG, BIT(23), 0);
- wr32m(REG_LED_CFG, BIT(24), 1);
- wr32m(REG_RFE_INV_A, 0xf, 7);
- wr32m(REG_RFE_INV_A, 0xf0, 7);
- wr32m(REG_RFE_INV_A, BIT(29) | BIT(28), band5 ? 2 : 1);
- if (!band5) {
- set32(REG_RXPSEL, BIT(28) | BIT(29));
- /* rtw8821a_phy_set_rfe_reg_24g() */
- wr32m(REG_RFE_PINMUX_A, 0xf000, 0x7);
- wr32m(REG_RFE_PINMUX_A, 0xf0, 0x7);
- if (R.rfe_cond & 1) {
- wr32m(REG_RFE_INV_A, BIT(20), 1);
- wr32m(REG_RFE_INV_A, BIT(22), 0);
- wr32m(REG_RFE_PINMUX_A, 0x7, 0x2);
- wr32m(REG_RFE_PINMUX_A, 0x700, 0x2);
- } else {
- wr32m(REG_RFE_INV_A, BIT(20), 0);
- wr32m(REG_RFE_INV_A, BIT(22), 0);
- wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
- wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
- }
- wr32m(REG_TXSCALE_A, 0xf00, 0);
- wr32m(REG_TXPSEL, 0xf0, 1);
- wr32m(REG_CCK_RX, 0x0f000000, 1);
- wr32m(REG_RRSR, 0xfffff, 0x15f); /* 1/2/5.5/11/6/12/24 */
- clr8(REG_CCK_CHECK, BIT(7));
- } else {
- /* rtw8821a_phy_set_rfe_reg_5g() */
- wr32m(REG_RFE_PINMUX_A, 0xf000, 0x5);
- wr32m(REG_RFE_PINMUX_A, 0xf0, 0x4);
- wr32m(REG_RFE_INV_A, BIT(20), 0);
- wr32m(REG_RFE_INV_A, BIT(22), 0);
- wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
- wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
- set8(REG_CCK_CHECK, BIT(7));
- for (t = 0; t < 50; t++) {
- if ((rd16(REG_TXPKT_EMPTY) & 0x30) == 0x30)
- break;
- udelay(50);
- }
- set32(REG_RXPSEL, BIT(28) | BIT(29));
- wr32m(REG_TXSCALE_A, 0xf00, 1);
- wr32m(REG_TXPSEL, 0xf0, 0);
- wr32m(REG_CCK_RX, 0x0f000000, 0xf);
- wr32m(REG_RRSR, 0xfffff, 0x150); /* 6/12/24 */
- }
- /* rtw88xxa_set_channel_bb_swing() */
- wr32m(REG_TXSCALE_A, 0xffe00000u, bb_swing(band5, 0));
- wr32m(REG_TXSCALE_B, 0xffe00000u, bb_swing(band5, 1));
- R.band5 = band5;
- }
- /* Channel group of the eFuse power tables (rtw_get_channel_group). */
- static unsigned int chan_group(unsigned int ch)
- {
- if (ch <= 14)
- return ch <= 2 ? 0 : ch <= 5 ? 1 : ch <= 8 ? 2 :
- ch <= 11 ? 3 : ch <= 13 ? 4 : 5;
- if (ch <= 42) return 0;
- if (ch <= 50) return 1;
- if (ch <= 58) return 2;
- if (ch <= 64) return 3;
- if (ch <= 106) return 4;
- if (ch <= 114) return 5;
- if (ch <= 122) return 6;
- if (ch <= 130) return 7;
- if (ch <= 138) return 8;
- if (ch <= 144) return 9;
- if (ch <= 155) return 10;
- if (ch <= 161) return 11;
- if (ch <= 171) return 12;
- return 13;
- }
- static int s4(unsigned int nib)
- {
- nib &= 0xf;
- return nib & 8 ? (int)nib - 16 : (int)nib;
- }
- static unsigned int clamp_idx(int v)
- {
- return v < 0 ? 0 : v > 0x3f ? 0x3f : (unsigned int)v;
- }
- /* TX power: the path-A eFuse base indices (rtw_phy_get_2g/5g_tx_power_index
- * without the by-rate and regulatory-limit tables), written to the TX AGC
- * registers for CCK, OFDM and HT MCS0-7. */
- static void set_tx_power(unsigned int ch)
- {
- const unsigned char *p = R.efuse + 0x10; /* txpwr_idx_table[A] */
- unsigned int g = chan_group(ch), cck, ofdm, ht, v;
- int base, train, i;
- unsigned int word;
- if (ch <= 14) {
- cck = p[g < 6 ? g : 5];
- base = p[6 + (g < 5 ? g : 4)];
- if (cck == 0xff)
- cck = 0x20;
- if (base == 0xff) {
- base = 0x20;
- ofdm = ht = 0x20;
- } else {
- ofdm = clamp_idx(base + s4(p[11]));
- ht = clamp_idx(base + s4(p[11] >> 4));
- }
- } else {
- base = p[18 + g];
- cck = 0;
- if (base == 0xff) {
- ofdm = ht = 0x20;
- } else {
- ofdm = clamp_idx(base + s4(p[32]));
- ht = clamp_idx(base + s4(p[32] >> 4));
- }
- }
- cck = clamp_idx((int)cck);
- if (ch <= 14)
- wr32(REG_TXAGC_A_CCK, cck * 0x01010101u);
- v = ofdm * 0x01010101u;
- wr32(REG_TXAGC_A_CCK + 0x4, v);
- wr32(REG_TXAGC_A_CCK + 0x8, v);
- v = ht * 0x01010101u;
- wr32(REG_TXAGC_A_CCK + 0xc, v);
- wr32(REG_TXAGC_A_CCK + 0x10, v);
- /* rtw88xxa_tx_power_training() */
- word = 0;
- train = (int)ht;
- for (i = 0; i < 3; i++) {
- train -= i == 0 ? 10 : i == 1 ? 8 : 6;
- word |= (unsigned int)(train < 2 ? 2 : train) << (i * 8);
- }
- wr32m(REG_TX_PWR_TRAINING_A, 0xffffff, word);
- }
- static void set_channel(unsigned int ch)
- {
- unsigned int fc, mod;
- int band5 = ch > 14;
- if (ch == R.chan)
- return;
- if (band5 != R.band5 || !R.chan)
- switch_band(band5);
- /* rtw88xxa_switch_channel() */
- if (ch >= 36 && ch <= 48)
- fc = 0x494;
- else if (ch >= 50 && ch <= 64)
- fc = 0x453;
- else if (ch >= 100 && ch <= 116)
- fc = 0x452;
- else if (ch >= 118)
- fc = 0x412;
- else
- fc = 0x96a;
- wr32m(REG_CLKTRK, 0x1ffe0000, fc);
- if (ch >= 36 && ch <= 64)
- mod = 0x101;
- else if (ch >= 100 && ch <= 140)
- mod = 0x301;
- else if (ch > 140)
- mod = 0x501;
- else
- mod = 0;
- rf_write(0x18, BIT(18) | BIT(17) | BIT(16) | BIT(9) | BIT(8), mod);
- rf_write(0x18, 0xff, ch);
- /* rtw88xxa_post_set_bw_mode(): 20 MHz, 1T1R */
- clr16(REG_WMAC_TRXPTCL_CTL, BIT(7) | BIT(8));
- wr8(REG_DATA_SC, 0);
- wr32m(REG_ADCCLK, 0x003003C3, 0x00300200);
- wr32m(REG_ADC160, BIT(30), 0);
- wr32m(REG_L1PKTH, 0x03C00000, 8);
- /* rtw88xxa_set_channel_rf(): RF 0x18 BW bits = 20 MHz */
- rf_write(0x18, BIT(11) | BIT(10), 3);
- set_tx_power(ch);
- R.chan = ch;
- }
- /* ---- power on (rtw88xxa_power_on) ---------------------------------------- */
- static int power_on(void)
- {
- /* page_table_8821a / rqpn: everything on one DMA queue (LOW with 3-4
- * bulk-OUT endpoints, NORMAL with 2). */
- unsigned int hq = 8, nq = 0, lq = 0, exq = 0, pub, boundary = 248;
- unsigned int dma = R.n_out >= 3 ? DMA_LOW : DMA_NORMAL, v;
- if (R.n_out >= 3)
- lq = 8;
- if (R.n_out >= 4)
- exq = 4;
- hw_reset();
- /* rtw88xxau_init_power_on() */
- if (pwr_seq(RTL_TBL_PWR_ON) < 0) {
- R.state = "power-on sequence failed";
- return -1;
- }
- wr16(REG_CR, 0);
- set16(REG_CR, 0x063F); /* HCI/MAC TX+RX DMA, protocol, sched, sec, 32K */
- if (rd8(REG_SYS_CFG1 + 3) & BIT(0))
- set8(REG_LDO_SWR_CTRL, BIT(6));
- /* rtw_set_trx_fifo_info(): 256 pages, 8 reserved -> boundary 248 */
- if (llt_init(boundary) < 0) {
- R.state = "LLT init failed";
- return -1;
- }
- set32(REG_TXDMA_OFFSET_CHK, BIT(9));
- if (download_fw() < 0)
- return -1;
- wr8(REG_HMETFR, 0xf);
- if (load_table(RTL_TBL_MAC) < 0) {
- R.state = "MAC table failed";
- return -1;
- }
- /* rtw88xxau_init_queue_reserved_page() */
- pub = boundary - hq - lq - nq - exq - 1;
- wr32(REG_RQPN_NPQ, nq | (exq << 16));
- wr32(REG_RQPN, BIT(31) | hq | (lq << 8) | (pub << 16));
- /* rtw88xxau_init_tx_buffer_boundary() */
- wr8(REG_BCNQ_BDNY, boundary);
- wr8(REG_MGQ_BDNY, boundary);
- wr8(REG_WMAC_LBK_BF_HD, boundary);
- wr8(REG_TRXFF_BNDY, boundary);
- wr8(REG_DWBCN0_CTRL + 1, boundary);
- /* rtw88xxau_init_queue_priority(), all queues on `dma`; RX
- * aggregation (bit 2) off */
- v = rd16(REG_TXDMA_PQ_MAP) & 0x3;
- v |= (dma << 4) | (dma << 6) | (dma << 8) | (dma << 10) |
- (dma << 12) | (dma << 14);
- wr16(REG_TXDMA_PQ_MAP, v);
- wr16(REG_TRXFF_BNDY + 2, 16128 - 128 - 1);
- wr8(REG_RX_DRVINFO_SZ, 4);
- wr32(REG_HIMR0, 0);
- wr32(REG_HIMR1, 0);
- wr32m(REG_CR, 0x30000, 0); /* net type: no link */
- /* rtw88xxa_init_wmac_setting() */
- wr16(REG_RXFLTMAP0, 0xffff);
- wr16(REG_RXFLTMAP1, 0x0400);
- wr16(REG_RXFLTMAP2, 0xffff);
- wr32(REG_MAR, 0xffffffff);
- wr32(REG_MAR + 4, 0xffffffff);
- /* rtw88xxa_init_adaptive_ctrl() */
- wr32m(REG_RRSR, 0xfffff, 0xffff1);
- wr16(REG_RETRY_LIMIT, 0x3030);
- /* rtw88xxa_init_edca() */
- wr16(REG_SPEC_SIFS, 0x100a);
- wr16(REG_MAC_SPEC_SIFS, 0x100a);
- wr16(REG_SIFS, 0x100a);
- wr16(REG_SIFS + 2, 0x100a);
- wr32(REG_EDCA_BE_PARAM, 0x005EA42B);
- wr32(REG_EDCA_BK_PARAM, 0x0000A44F);
- wr32(REG_EDCA_VI_PARAM, 0x005EA324);
- wr32(REG_EDCA_VO_PARAM, 0x002FA226);
- wr8(REG_USTIME_TSF, 0x50);
- wr8(REG_USTIME_EDCA, 0x50);
- set8(REG_FWHW_TXQ_CTRL, BIT(7));
- wr8(REG_ACKTO, 0x80);
- /* rtw88xxau_tx_aggregation() */
- wr32m(REG_DWBCN0_CTRL, 0xf0, 6);
- wr8(REG_DWBCN1_CTRL, 6 << 1);
- /* rtw88xxa_init_beacon_parameters() */
- wr16(REG_BCN_CTRL, (BIT(4) << 8) | BIT(4));
- wr32m(REG_TBTT_PROHIBIT, 0xfffff, 0x04 | (0x64 << 8));
- wr8(REG_DRVERLYINT, 0x05);
- wr8(REG_BCNDMATIM, 0x02);
- wr16(REG_BCNTCFG, 0x4413);
- wr8(REG_BCN_MAX_ERR, 0xff);
- /* rtw_usb_init_burst_pkt_len(): high speed, 512-byte bursts */
- wr8(REG_RXDMA_MODE, (3 << 2) | BIT(1) | (1 << 4));
- set16(REG_TXDMA_OFFSET_CHK, BIT(9));
- wr8(REG_USB3_RXITV, 0x01);
- wr16(REG_RXDMA_STATUS, 0x7400);
- wr8(REG_RXDMA_STATUS + 1, 0xf5);
- wr8(REG_AMPDU_MAX_TIME, 0x5e);
- wr32(REG_AMPDU_MAX_LENGTH, 0xffffffff);
- wr8(REG_USTIME_TSF, 0x50);
- wr8(REG_USTIME_EDCA, 0x50);
- set8(REG_SINGLE_AMPDU_CTRL, BIT(7));
- wr8(REG_RX_PKT_LIMIT, 0x18);
- wr8(REG_PIFS, 0x00);
- wr16(REG_MAX_AGGR_NUM, 0x1f1f);
- wr8(REG_FWHW_TXQ_CTRL, 0x80);
- wr32(REG_FAST_EDCA_CTRL, 0x03087777);
- set8(REG_RSV_CTRL, BIT(5) | BIT(6));
- wr32(REG_ARFR0, 0x00000010);
- wr32(REG_ARFRH0, 0xfffff000);
- wr32(REG_ARFR1_V1, 0x00000010);
- wr32(REG_ARFRH1_V1, 0x003ff000);
- wr32(REG_ARFR2_V1, 0x00000015);
- wr32(REG_ARFRH2_V1, 0x003ff000);
- wr32(REG_ARFR3_V1, 0x00000015);
- wr32(REG_ARFRH3_V1, 0xffcff000);
- set8(REG_CR, BIT(6) | BIT(7)); /* MACTXEN | MACRXEN */
- /* rtw88xxa_phy_bb_config() */
- v = rd8(REG_SYS_FUNC_EN) | BIT(2);
- wr8(REG_SYS_FUNC_EN, v);
- wr8(REG_SYS_FUNC_EN, v | BIT(0) | BIT(1));
- wr8(REG_RF_CTRL, 7);
- wr8(REG_RF_B_CTRL, 7);
- if (load_table(RTL_TBL_BB) < 0 || load_table(RTL_TBL_AGC) < 0) {
- R.state = "BB table failed";
- return -1;
- }
- wr32m(REG_AFE_CTRL3, 0x00FFF000,
- (R.crystal & 0x3f) | ((R.crystal & 0x3f) << 6));
- /* rtw88xxa_phy_rf_config() */
- if (load_table(RTL_TBL_RF_A) < 0) {
- R.state = "RF table failed";
- return -1;
- }
- R.chan = 0;
- R.band5 = 0;
- switch_band(0);
- wr32(REG_SEC_CMD, BIT(31) | BIT(30)); /* clear the key CAM */
- wr8(REG_HWSEQ_CTRL, 0xff);
- wr32(REG_BAR_MODE_CTRL, 0x0201ffff);
- wr8(REG_NAV_CTRL + 2, 0);
- clr8(REG_GPIO_MUXCFG, BIT(5));
- clr8(REG_QUEUE_CTRL, BIT(3));
- wr8(REG_FWHW_TXQ_CTRL + 1, 0x0f);
- wr8(REG_EARLY_MODE_CONTROL + 3, 0x01);
- wr16(REG_TX_RPT_TIME, 0x3df0);
- wr8(REG_SYS_SDIO_CTRL, 0);
- wr8(REG_ACLK_MON, 0);
- wr8(REG_USB_HRPWM, 0);
- set32(REG_FWHW_TXQ_CTRL, BIT(12));
- /* rtw_core_start(): RX filter; then our address */
- wr32(REG_RCR, RCR_DEFAULT);
- {
- unsigned int i;
- for (i = 0; i < 6; i++) {
- wr8(REG_MACID + i, R.mac[i]);
- wr8(REG_BSSID + i, 0);
- }
- }
- set_channel(1);
- return R.fatal ? -1 : 0;
- }
- /* rtw88xxa_power_off(), without BT coex. */
- static void power_off(void)
- {
- unsigned int cr = rd8(REG_CR);
- if (cr == 0 || cr == 0xea || R.fatal)
- return;
- wr32(REG_HIMR0, 0);
- wr32(REG_HIMR1, 0);
- wr8(REG_CR, 0);
- pwr_seq(RTL_TBL_PWR_LPS);
- if (rd8(REG_MCUFW_CTRL) & BIT(7))
- reset_8051();
- clr8(REG_SYS_FUNC_EN + 1, BIT(2));
- wr8(REG_MCUFW_CTRL, 0);
- pwr_seq(RTL_TBL_PWR_OFF);
- }
- /* ---- RX ------------------------------------------------------------------ */
- static int cck_rx_pwr(unsigned int lna, unsigned int vga)
- {
- static const signed char lna_gain[] = { 15, -1, -17, 0, -30, -38 };
- if (lna > 5 || lna == 3)
- return -100;
- return lna_gain[lna] - 2 * (int)vga;
- }
- static void rx_packet(const unsigned char *d, unsigned int avail)
- {
- unsigned int w0, len, drv, shift, rate, off;
- const unsigned char *phy, *f;
- int rssi = -100;
- while (avail >= RXDESC_SZ) {
- w0 = le32(d);
- len = w0 & 0x3fff;
- drv = ((w0 >> 16) & 0xf) * 8;
- shift = (w0 >> 24) & 3;
- rate = le32(d + 12) & 0x7f;
- off = RXDESC_SZ + drv + shift;
- if (!len || off + len > avail)
- return;
- phy = d + RXDESC_SZ + shift;
- f = d + off;
- if (le32(d + 8) & BIT(28)) {
- /* C2H firmware event: not used. */
- } else if (w0 & BIT(14)) {
- R.rx_crc++;
- } else if (len > 4 + 24) {
- if (drv && (w0 & BIT(26))) {
- if (rate <= 3)
- rssi = cck_rx_pwr((le32(phy + 4) >> 13) & 7,
- (le32(phy + 4) >> 8) & 0x1f);
- else
- rssi = (int)(phy[0] & 0x7f) - 110;
- }
- R.rx_pkts++;
- R.last_rssi = rssi;
- wifi_rx_frame(f, len - 4, rssi, 0, R.chan);
- }
- off = (off + len + 7) & ~7u;
- if (off >= avail)
- return;
- d += off;
- avail -= off;
- }
- }
- /* Drain the RX FIFO: every completed bulk-IN transfer is parsed, then the
- * next one is armed; stop after ~0.3 ms without data or 64 packets (the
- * tick is frozen inside a syscall, so the bounds are counts). */
- static int drain_rx(void)
- {
- unsigned int frames = 0, idle = 0;
- int n;
- while (frames < 64) {
- n = usb_bulk_in(R.ctrl, R.addr, R.ep_in, R.ep_in_mps,
- R.rxbuf, RX_BUF_SZ);
- if (n < 0) {
- io_fail();
- return -1;
- }
- if (n == 0) {
- if (++idle > 6)
- break;
- udelay(50);
- continue;
- }
- idle = 0;
- frames++;
- rx_packet(R.rxbuf, (unsigned int)n);
- }
- return 0;
- }
- /* ---- TX ------------------------------------------------------------------ */
- static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
- int protect)
- {
- unsigned char *b = R.txbuf, *f;
- unsigned int pad, rate, rate_id, qsel, i, sum, w;
- if (!R.up || R.fatal)
- return -1;
- if (len + TXDESC_SZ + 8 + 8 > TX_BUF_SZ)
- return -1;
- if (protect && !R.have_tk)
- return -1;
- /* A transfer of an exact multiple of 512 bytes would need a
- * zero-length packet; the vendor drivers shift the frame by 8 bytes
- * (PKT_OFFSET 1) instead. */
- pad = ((len + (protect ? 8 : 0) + TXDESC_SZ) % 512) == 0 ? 8 : 0;
- f = b + TXDESC_SZ + pad;
- rmemcpy(f, frame, len);
- if (protect) {
- if (ccmp_tx_encrypt(f, len, 24, R.tk) < 0)
- return -1;
- len += 8;
- }
- if (mgmt) {
- rate = R.band5 ? RATE_6M : RATE_1M;
- rate_id = R.band5 ? RATEID_G : RATEID_B;
- qsel = QSEL_MGMT;
- } else {
- rate = RATE_12M;
- rate_id = R.band5 ? RATEID_G : RATEID_BG;
- qsel = QSEL_BE;
- }
- rmemzero(b, TXDESC_SZ + pad);
- /* rtw_tx_fill_tx_desc() */
- put32(b + 0, len | ((TXDESC_SZ + pad) << 16) |
- ((f[4] & 1) ? BIT(24) : 0) | BIT(26) | BIT(31));
- put32(b + 4, (qsel << 8) | (rate_id << 16) | ((pad ? 1 : 0) << 24));
- put32(b + 12, BIT(8) | BIT(10)); /* USE_RATE, DISDATAFB */
- put32(b + 16, rate | (0x1f << 8)); /* old_datarate_fb_limit */
- put32(b + 28, 1u << 24); /* DMA_TXAGG_NUM 1 */
- put32(b + 32, BIT(15)); /* EN_HWSEQ */
- /* fill_txdesc_checksum_common(): XOR of the first 16 le16 words */
- sum = 0;
- for (i = 0; i < 32; i += 2)
- sum ^= b[i] | (b[i + 1] << 8);
- w = le32(b + 28) & ~0xffffu;
- put32(b + 28, w | (sum & 0xffff));
- if (usb_bulk_out(R.ctrl, R.addr, R.ep_tx, R.ep_out_mps, b,
- TXDESC_SZ + pad + len) != 0) {
- R.tx_errs++;
- io_fail();
- return -1;
- }
- R.tx_pkts++;
- return 0;
- }
- static void send_probe_req(void)
- {
- static const unsigned char rates24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
- 0x0c, 0x12, 0x18, 0x24,
- 50, 4, 0x30, 0x48, 0x60, 0x6c };
- static const unsigned char rates5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
- 0xb0, 0x48, 0x60, 0x6c };
- unsigned char f[64];
- unsigned int o;
- rmemzero(f, 24);
- f[0] = 0x40; /* probe request */
- rmemcpy(f + 10, R.mac, 6);
- for (o = 0; o < 6; o++)
- f[4 + o] = f[16 + o] = 0xff;
- o = 24;
- f[o++] = 0; /* wildcard SSID */
- f[o++] = 0;
- if (R.band5) {
- rmemcpy(f + o, rates5, sizeof(rates5));
- o += sizeof(rates5);
- } else {
- rmemcpy(f + o, rates24, sizeof(rates24));
- o += sizeof(rates24);
- }
- tx_frame(f, o, 1, 0);
- }
- /* ---- scan ---------------------------------------------------------------- */
- static int chan_active(unsigned int ch)
- {
- return ch <= 14 || (ch >= 36 && ch <= 48);
- }
- static void scan_step(void)
- {
- unsigned int ch;
- if (R.scan_idx >= sizeof(scan_chans)) {
- R.scanning = 0;
- wifi_scan_done(1);
- return;
- }
- ch = scan_chans[R.scan_idx++];
- set_channel(ch);
- if (chan_active(ch))
- send_probe_req();
- R.scan_next = get_ticks_ms() +
- (chan_active(ch) ? DWELL_ACTIVE : DWELL_PASSIVE);
- }
- /* ---- public API ---------------------------------------------------------- */
- static int check_present(void)
- {
- if (!R.present)
- return 0;
- if (R.port != 0xFFu && !usb_port_connected(R.ctrl, R.port)) {
- R.present = 0;
- R.fatal = 1;
- return 0;
- }
- return 1;
- }
- int rtl8821au_present(void)
- {
- return check_present();
- }
- const char *rtl8821au_chip_name(void)
- {
- return "RTL8821AU USB";
- }
- const char *rtl8821au_fw_name(void)
- {
- return R.up ? R.fw_name : "";
- }
- int rtl8821au_fatal(void)
- {
- return R.up && (R.fatal || !check_present());
- }
- static int alloc_buffers(void)
- {
- unsigned int rx;
- if (R.io)
- return 0;
- R.io = (unsigned char *)mm_alloc_page();
- R.efuse = (unsigned char *)mm_alloc_page();
- R.txbuf = (unsigned char *)mm_alloc_page();
- rx = mm_alloc_pages(4);
- if (!R.io || !R.efuse || !R.txbuf || !rx) {
- R.io = 0;
- return -1;
- }
- /* An 8 KB-aligned 8 KB window never crosses a 64 KB boundary (one
- * xHCI TRB) and is contiguous for EHCI's qTD pages. */
- R.rxbuf = (unsigned char *)((rx + 8191) & ~8191u);
- return 0;
- }
- static void fmt_fw_name(void)
- {
- static const char pre[] = "rtw8821a ";
- unsigned int i = 0, v, d = 10000;
- int started = 0;
- for (; pre[i]; i++)
- R.fw_name[i] = pre[i];
- v = R.fw_ver;
- for (; d; d /= 10) {
- if (v / d || started || d == 1) {
- R.fw_name[i++] = (char)('0' + v / d);
- started = 1;
- }
- v %= d;
- }
- R.fw_name[i++] = '.';
- v = R.fw_sub;
- if (v >= 10)
- R.fw_name[i++] = (char)('0' + v / 10 % 10);
- R.fw_name[i++] = (char)('0' + v % 10);
- R.fw_name[i] = 0;
- }
- int rtl8821au_up(unsigned char mac[6])
- {
- if (!check_present()) {
- R.state = "no RTL8821AU stick";
- return -1;
- }
- if (R.up && !R.fatal) {
- rmemcpy(mac, R.mac, 6);
- return 0;
- }
- R.up = 0;
- R.fatal = 0;
- R.io_errs = 0;
- R.scanning = 0;
- R.have_tk = 0;
- if (alloc_buffers() < 0) {
- R.state = "out of memory";
- return -1;
- }
- if (load_blobs() < 0)
- return -1;
- R.cut = (rd32(REG_SYS_CFG1) >> 12) & 0xf;
- rlog("rtl8821au: power on, cut ");
- serial_putdec(SERIAL_COM1, R.cut);
- rlog("\n");
- /* rtw_chip_efuse_info_setup(): power on the MAC, read the eFuse. */
- wr8(REG_RSV_CTRL, 0);
- wr8(REG_LDO_SWR_CTRL, (rd32(REG_SYS_CFG1) & BIT(24)) ? 0xC3 : 0x83);
- if (pwr_seq(RTL_TBL_PWR_ON) < 0 || read_efuse() < 0) {
- R.state = R.fatal ? "USB I/O failed" : "eFuse read failed";
- return -1;
- }
- parse_efuse();
- rlog("rtl8821au: eFuse ok, rfe ");
- serial_puthex(SERIAL_COM1, R.rfe_cond);
- rlog("\n");
- power_off();
- R.state = 0;
- if (power_on() < 0) {
- if (R.fatal)
- R.state = "USB I/O failed";
- else if (!R.state)
- R.state = "power on failed";
- loghex("rtl8821au: bring-up failed, MCUFW_CTRL ",
- rd32(REG_MCUFW_CTRL));
- rlog("rtl8821au: ");
- rlog(R.state);
- rlog("\n");
- return -1;
- }
- fmt_fw_name();
- R.up = 1;
- R.state = "ready";
- rmemcpy(mac, R.mac, 6);
- rlog("rtl8821au: up, firmware ");
- rlog(R.fw_name);
- rlog("\n");
- return 0;
- }
- void rtl8821au_down(void)
- {
- R.scanning = 0;
- R.have_tk = 0;
- if (R.up && check_present() && !R.fatal)
- power_off();
- R.up = 0;
- R.chan = 0;
- R.state = "down";
- }
- const char *rtl8821au_state(void)
- {
- return R.state ? R.state : "not started";
- }
- int rtl8821au_poll(void)
- {
- if (!R.up)
- return -1;
- if (R.fatal || !check_present())
- return -1;
- if (drain_rx() < 0)
- return -1;
- if (R.scanning && (int)(get_ticks_ms() - R.scan_next) >= 0)
- scan_step();
- return R.fatal ? -1 : 0;
- }
- int rtl8821au_scan_start(void)
- {
- if (!R.up || R.fatal)
- return -1;
- wr32(REG_RCR, RCR_DEFAULT);
- R.scanning = 1;
- R.scan_idx = 0;
- scan_step();
- return 0;
- }
- int rtl8821au_scan_running(void)
- {
- return R.scanning;
- }
- int rtl8821au_prepare_connect(const unsigned char bssid[6],
- unsigned int chan, unsigned int beacon_int)
- {
- unsigned int i;
- (void)beacon_int;
- if (!R.up || R.fatal)
- return -1;
- R.scanning = 0;
- R.have_tk = 0;
- rmemcpy(R.bssid, bssid, 6);
- set_channel(chan);
- for (i = 0; i < 6; i++)
- wr8(REG_BSSID + i, bssid[i]);
- wr32(REG_RCR, RCR_DEFAULT);
- drain_rx();
- return R.fatal ? -1 : 0;
- }
- int rtl8821au_set_assoc(unsigned short aid, unsigned int beacon_int,
- unsigned int dtim)
- {
- (void)beacon_int;
- (void)dtim;
- if (!R.up || R.fatal)
- return -1;
- wr32m(REG_AID, 0x7ff, aid);
- wr32m(REG_CR, 0x30000, 2); /* RTW_NET_MGD_LINKED */
- /* Only our BSS's data and beacons from here on. */
- wr32(REG_RCR, RCR_DEFAULT | RCR_CBSSID_DATA | RCR_CBSSID_BCN);
- return R.fatal ? -1 : 0;
- }
- void rtl8821au_disconnect(void)
- {
- unsigned int i;
- R.have_tk = 0;
- if (!R.up || R.fatal)
- return;
- wr32m(REG_CR, 0x30000, 0);
- wr32m(REG_AID, 0x7ff, 0);
- for (i = 0; i < 6; i++)
- wr8(REG_BSSID + i, 0);
- wr32(REG_RCR, RCR_DEFAULT);
- }
- int rtl8821au_tx_mgmt(const unsigned char *frame, unsigned int len)
- {
- return tx_frame(frame, len, 1, 0);
- }
- int rtl8821au_tx_data(const unsigned char *frame, unsigned int len,
- int protect)
- {
- return tx_frame(frame, len, 0, protect);
- }
- int rtl8821au_set_key(int pairwise, int keyidx, const unsigned char key[16],
- const unsigned char rsc[6])
- {
- (void)keyidx;
- (void)rsc;
- /* The group key is only used for RX, which wifi.c decrypts. */
- if (pairwise) {
- rmemcpy(R.tk, key, 16);
- R.have_tk = 1;
- }
- return 0;
- }
- void rtl8821au_clear_keys(void)
- {
- rmemzero(R.tk, 16);
- R.have_tk = 0;
- }
- /* ---- USB probe ----------------------------------------------------------- */
- static int parse_config(void)
- {
- unsigned char *c = R.io;
- unsigned int total, o, n_out = 0, in = 0, in_mps = 512, out_mps = 512;
- int n;
- n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
- 0x0200, 0, c, 9);
- if (n < 9)
- return -1;
- total = c[2] | (c[3] << 8);
- if (total > 512)
- total = 512;
- n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
- 0x0200, 0, c, total);
- if (n < 9)
- return -1;
- total = (unsigned int)n;
- for (o = 0; o + 2 <= total && c[o] >= 2; o += c[o]) {
- if (c[o + 1] == 4 && o + 9 <= total && c[o + 2] != 0)
- break; /* only interface 0 */
- if (c[o + 1] != 5 || o + 7 > total)
- continue;
- if ((c[o + 3] & 3) != 2)
- continue; /* bulk only */
- if (c[o + 2] & 0x80) {
- if (!in) {
- in = c[o + 2] & 0x0f;
- in_mps = c[o + 4] | (c[o + 5] << 8);
- }
- } else if (n_out < 4) {
- R.ep_out[n_out++] = c[o + 2] & 0x0f;
- out_mps = c[o + 4] | (c[o + 5] << 8);
- }
- }
- if (!in || n_out < 2)
- return -1;
- R.ep_in = in;
- R.ep_in_mps = in_mps ? in_mps : 512;
- R.n_out = n_out;
- R.ep_out_mps = out_mps ? out_mps : 512;
- /* dma_mapping_to_ep(): HIGH 0, NORMAL 1, LOW 2, EXTRA 3 */
- R.ep_tx = R.ep_out[n_out >= 3 ? 2 : 1];
- if (usb_control_out(R.ctrl, R.addr, R.mps, R.low, 0x00, 0x09,
- c[5], 0) < 0)
- return -1;
- return 0;
- }
- static int device_probe(unsigned int ctrl, unsigned int addr,
- unsigned int mps, int low_speed, unsigned int port,
- unsigned int vendor, unsigned int product)
- {
- unsigned int i;
- for (i = 0; i < sizeof(usb_ids) / sizeof(usb_ids[0]); i++)
- if (usb_ids[i][0] == vendor && usb_ids[i][1] == product)
- break;
- if (i == sizeof(usb_ids) / sizeof(usb_ids[0]))
- return 0;
- if (!usb_bulk_supported(ctrl)) {
- rlog("rtl8821au: stick on a controller without bulk "
- "transfers (UHCI/OHCI), ignored\n");
- return 0;
- }
- if (alloc_buffers() < 0)
- return 0;
- /* A re-enumeration resets the chip: a running instance is dead. */
- if (R.up)
- R.fatal = 1;
- R.ctrl = ctrl;
- R.addr = addr;
- R.mps = mps;
- R.low = low_speed;
- R.port = port;
- R.vendor = vendor;
- R.product = product;
- R.gen = usb_generation();
- R.present = 1;
- R.io_errs = 0;
- if (parse_config() < 0) {
- rlog("rtl8821au: unexpected USB configuration\n");
- R.present = 0;
- return 0;
- }
- if (!R.up)
- R.fatal = 0;
- R.state = "firmware not loaded";
- rlog("rtl8821au: found ");
- serial_puthex(SERIAL_COM1, (vendor << 16) | product);
- rlog(", bulk in ");
- serial_putdec(SERIAL_COM1, R.ep_in);
- rlog(", tx ep ");
- serial_putdec(SERIAL_COM1, R.ep_tx);
- rlog(" of ");
- serial_putdec(SERIAL_COM1, R.n_out);
- rlog("\n");
- return 1;
- }
- void rtl8821au_register(void)
- {
- usb_register_device_probe(device_probe);
- }
|