xref: /linux/drivers/net/wireless/morsemicro/mm81x/rate_code.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1*b1906ceaSLachlan Hodges /* SPDX-License-Identifier: GPL-2.0-only */
2*b1906ceaSLachlan Hodges /*
3*b1906ceaSLachlan Hodges  * Copyright (c) 2017-2026 Morse Micro
4*b1906ceaSLachlan Hodges  */
5*b1906ceaSLachlan Hodges 
6*b1906ceaSLachlan Hodges #ifndef _MM81X_RATE_CODE_H_
7*b1906ceaSLachlan Hodges #define _MM81X_RATE_CODE_H_
8*b1906ceaSLachlan Hodges 
9*b1906ceaSLachlan Hodges #include <linux/types.h>
10*b1906ceaSLachlan Hodges 
11*b1906ceaSLachlan Hodges enum dot11_bandwidth {
12*b1906ceaSLachlan Hodges 	DOT11_BANDWIDTH_1MHZ = 0,
13*b1906ceaSLachlan Hodges 	DOT11_BANDWIDTH_2MHZ = 1,
14*b1906ceaSLachlan Hodges 	DOT11_BANDWIDTH_4MHZ = 2,
15*b1906ceaSLachlan Hodges 	DOT11_BANDWIDTH_8MHZ = 3,
16*b1906ceaSLachlan Hodges 	DOT11_BANDWIDTH_16MHZ = 4,
17*b1906ceaSLachlan Hodges 
18*b1906ceaSLachlan Hodges 	DOT11_MAX_BANDWIDTH = DOT11_BANDWIDTH_16MHZ,
19*b1906ceaSLachlan Hodges 	DOT11_INVALID_BANDWIDTH = 5
20*b1906ceaSLachlan Hodges };
21*b1906ceaSLachlan Hodges 
22*b1906ceaSLachlan Hodges enum mm81x_rate_preamble {
23*b1906ceaSLachlan Hodges 	/* S1G LONG format (with SIG-A and SIG-B) */
24*b1906ceaSLachlan Hodges 	MM81X_RATE_PREAMBLE_S1G_LONG = 0,
25*b1906ceaSLachlan Hodges 	/* This is the most common format used */
26*b1906ceaSLachlan Hodges 	MM81X_RATE_PREAMBLE_S1G_SHORT = 1,
27*b1906ceaSLachlan Hodges 	/* S1G 1M format */
28*b1906ceaSLachlan Hodges 	MM81X_RATE_PREAMBLE_S1G_1M = 2,
29*b1906ceaSLachlan Hodges 
30*b1906ceaSLachlan Hodges 	MM81X_RATE_MAX_PREAMBLE = MM81X_RATE_PREAMBLE_S1G_1M,
31*b1906ceaSLachlan Hodges 	MM81X_RATE_INVALID_PREAMBLE = 7
32*b1906ceaSLachlan Hodges };
33*b1906ceaSLachlan Hodges 
34*b1906ceaSLachlan Hodges typedef __le32 mm81x_rate_code_t;
35*b1906ceaSLachlan Hodges 
36*b1906ceaSLachlan Hodges #define MM81X_RATECODE_PREAMBLE (0x0000000F)
37*b1906ceaSLachlan Hodges #define MM81X_RATECODE_MCS_INDEX (0x000000F0)
38*b1906ceaSLachlan Hodges #define MM81X_RATECODE_NSS_INDEX (0x00000700)
39*b1906ceaSLachlan Hodges #define MM81X_RATECODE_BW_INDEX (0x00003800)
40*b1906ceaSLachlan Hodges #define MM81X_RATECODE_RTS_FLAG (0x00010000)
41*b1906ceaSLachlan Hodges #define MM81X_RATECODE_SHORT_GI_FLAG (0x00040000)
42*b1906ceaSLachlan Hodges #define MM81X_RATECODE_DUP_BW_INDEX (0x01C00000)
43*b1906ceaSLachlan Hodges 
44*b1906ceaSLachlan Hodges static inline enum mm81x_rate_preamble
mm81x_ratecode_preamble_get(mm81x_rate_code_t rc)45*b1906ceaSLachlan Hodges mm81x_ratecode_preamble_get(mm81x_rate_code_t rc)
46*b1906ceaSLachlan Hodges {
47*b1906ceaSLachlan Hodges 	return (enum mm81x_rate_preamble)(
48*b1906ceaSLachlan Hodges 		le32_get_bits(rc, MM81X_RATECODE_PREAMBLE));
49*b1906ceaSLachlan Hodges }
50*b1906ceaSLachlan Hodges 
mm81x_ratecode_mcs_index_get(mm81x_rate_code_t rc)51*b1906ceaSLachlan Hodges static inline u8 mm81x_ratecode_mcs_index_get(mm81x_rate_code_t rc)
52*b1906ceaSLachlan Hodges {
53*b1906ceaSLachlan Hodges 	return le32_get_bits(rc, MM81X_RATECODE_MCS_INDEX);
54*b1906ceaSLachlan Hodges }
55*b1906ceaSLachlan Hodges 
mm81x_ratecode_nss_index_get(mm81x_rate_code_t rc)56*b1906ceaSLachlan Hodges static inline u8 mm81x_ratecode_nss_index_get(mm81x_rate_code_t rc)
57*b1906ceaSLachlan Hodges {
58*b1906ceaSLachlan Hodges 	return le32_get_bits(rc, MM81X_RATECODE_NSS_INDEX);
59*b1906ceaSLachlan Hodges }
60*b1906ceaSLachlan Hodges 
61*b1906ceaSLachlan Hodges static inline enum dot11_bandwidth
mm81x_ratecode_bw_index_get(mm81x_rate_code_t rc)62*b1906ceaSLachlan Hodges mm81x_ratecode_bw_index_get(mm81x_rate_code_t rc)
63*b1906ceaSLachlan Hodges {
64*b1906ceaSLachlan Hodges 	return (enum dot11_bandwidth)(
65*b1906ceaSLachlan Hodges 		le32_get_bits(rc, MM81X_RATECODE_BW_INDEX));
66*b1906ceaSLachlan Hodges }
67*b1906ceaSLachlan Hodges 
mm81x_ratecode_rts_get(mm81x_rate_code_t rc)68*b1906ceaSLachlan Hodges static inline bool mm81x_ratecode_rts_get(mm81x_rate_code_t rc)
69*b1906ceaSLachlan Hodges {
70*b1906ceaSLachlan Hodges 	return le32_get_bits(rc, MM81X_RATECODE_RTS_FLAG);
71*b1906ceaSLachlan Hodges }
72*b1906ceaSLachlan Hodges 
mm81x_ratecode_sgi_get(mm81x_rate_code_t rc)73*b1906ceaSLachlan Hodges static inline bool mm81x_ratecode_sgi_get(mm81x_rate_code_t rc)
74*b1906ceaSLachlan Hodges {
75*b1906ceaSLachlan Hodges 	return le32_get_bits(rc, MM81X_RATECODE_SHORT_GI_FLAG);
76*b1906ceaSLachlan Hodges }
77*b1906ceaSLachlan Hodges 
78*b1906ceaSLachlan Hodges static inline enum dot11_bandwidth
mm81x_ratecode_dup_bw_index_get(mm81x_rate_code_t rc)79*b1906ceaSLachlan Hodges mm81x_ratecode_dup_bw_index_get(mm81x_rate_code_t rc)
80*b1906ceaSLachlan Hodges {
81*b1906ceaSLachlan Hodges 	return (enum dot11_bandwidth)(
82*b1906ceaSLachlan Hodges 		le32_get_bits(rc, MM81X_RATECODE_DUP_BW_INDEX));
83*b1906ceaSLachlan Hodges }
84*b1906ceaSLachlan Hodges 
85*b1906ceaSLachlan Hodges #define MM81X_RATECODE_INIT(bw_idx, nss_idx, mcs_idx, preamble)  \
86*b1906ceaSLachlan Hodges 	(le32_encode_bits((bw_idx), MM81X_RATECODE_BW_INDEX) |   \
87*b1906ceaSLachlan Hodges 	 le32_encode_bits((nss_idx), MM81X_RATECODE_NSS_INDEX) | \
88*b1906ceaSLachlan Hodges 	 le32_encode_bits((mcs_idx), MM81X_RATECODE_MCS_INDEX) | \
89*b1906ceaSLachlan Hodges 	 le32_encode_bits((preamble), MM81X_RATECODE_PREAMBLE))
90*b1906ceaSLachlan Hodges 
91*b1906ceaSLachlan Hodges static inline mm81x_rate_code_t
mm81x_ratecode_init(enum dot11_bandwidth bw_index,u32 nss_index,u32 mcs_index,enum mm81x_rate_preamble preamble)92*b1906ceaSLachlan Hodges mm81x_ratecode_init(enum dot11_bandwidth bw_index, u32 nss_index, u32 mcs_index,
93*b1906ceaSLachlan Hodges 		    enum mm81x_rate_preamble preamble)
94*b1906ceaSLachlan Hodges {
95*b1906ceaSLachlan Hodges 	return MM81X_RATECODE_INIT(bw_index, nss_index, mcs_index, preamble);
96*b1906ceaSLachlan Hodges }
97*b1906ceaSLachlan Hodges 
98*b1906ceaSLachlan Hodges static inline void
mm81x_ratecode_preamble_set(mm81x_rate_code_t * rc,enum mm81x_rate_preamble preamble)99*b1906ceaSLachlan Hodges mm81x_ratecode_preamble_set(mm81x_rate_code_t *rc,
100*b1906ceaSLachlan Hodges 			    enum mm81x_rate_preamble preamble)
101*b1906ceaSLachlan Hodges {
102*b1906ceaSLachlan Hodges 	*rc = (*rc & cpu_to_le32(~MM81X_RATECODE_PREAMBLE)) |
103*b1906ceaSLachlan Hodges 	      le32_encode_bits(preamble, MM81X_RATECODE_PREAMBLE);
104*b1906ceaSLachlan Hodges }
105*b1906ceaSLachlan Hodges 
mm81x_ratecode_mcs_index_set(mm81x_rate_code_t * rc,u32 mcs_index)106*b1906ceaSLachlan Hodges static inline void mm81x_ratecode_mcs_index_set(mm81x_rate_code_t *rc,
107*b1906ceaSLachlan Hodges 						u32 mcs_index)
108*b1906ceaSLachlan Hodges {
109*b1906ceaSLachlan Hodges 	*rc = (*rc & cpu_to_le32(~MM81X_RATECODE_MCS_INDEX)) |
110*b1906ceaSLachlan Hodges 	      le32_encode_bits(mcs_index, MM81X_RATECODE_MCS_INDEX);
111*b1906ceaSLachlan Hodges }
112*b1906ceaSLachlan Hodges 
mm81x_ratecode_nss_index_set(mm81x_rate_code_t * rc,u32 nss_index)113*b1906ceaSLachlan Hodges static inline void mm81x_ratecode_nss_index_set(mm81x_rate_code_t *rc,
114*b1906ceaSLachlan Hodges 						u32 nss_index)
115*b1906ceaSLachlan Hodges {
116*b1906ceaSLachlan Hodges 	*rc = (*rc & cpu_to_le32(~MM81X_RATECODE_NSS_INDEX)) |
117*b1906ceaSLachlan Hodges 	      le32_encode_bits(nss_index, MM81X_RATECODE_NSS_INDEX);
118*b1906ceaSLachlan Hodges }
119*b1906ceaSLachlan Hodges 
mm81x_ratecode_bw_index_set(mm81x_rate_code_t * rc,enum dot11_bandwidth bw_index)120*b1906ceaSLachlan Hodges static inline void mm81x_ratecode_bw_index_set(mm81x_rate_code_t *rc,
121*b1906ceaSLachlan Hodges 					       enum dot11_bandwidth bw_index)
122*b1906ceaSLachlan Hodges {
123*b1906ceaSLachlan Hodges 	*rc = (*rc & cpu_to_le32(~MM81X_RATECODE_BW_INDEX)) |
124*b1906ceaSLachlan Hodges 	      le32_encode_bits(bw_index, MM81X_RATECODE_BW_INDEX);
125*b1906ceaSLachlan Hodges }
126*b1906ceaSLachlan Hodges 
127*b1906ceaSLachlan Hodges static inline void
mm81x_ratecode_update_s1g_bw_preamble(mm81x_rate_code_t * rc,enum dot11_bandwidth bw_index)128*b1906ceaSLachlan Hodges mm81x_ratecode_update_s1g_bw_preamble(mm81x_rate_code_t *rc,
129*b1906ceaSLachlan Hodges 				      enum dot11_bandwidth bw_index)
130*b1906ceaSLachlan Hodges {
131*b1906ceaSLachlan Hodges 	enum mm81x_rate_preamble pream = MM81X_RATE_PREAMBLE_S1G_SHORT;
132*b1906ceaSLachlan Hodges 
133*b1906ceaSLachlan Hodges 	if (bw_index == DOT11_BANDWIDTH_1MHZ)
134*b1906ceaSLachlan Hodges 		pream = MM81X_RATE_PREAMBLE_S1G_1M;
135*b1906ceaSLachlan Hodges 
136*b1906ceaSLachlan Hodges 	mm81x_ratecode_preamble_set(rc, pream);
137*b1906ceaSLachlan Hodges 	mm81x_ratecode_bw_index_set(rc, bw_index);
138*b1906ceaSLachlan Hodges }
139*b1906ceaSLachlan Hodges 
140*b1906ceaSLachlan Hodges static inline void
mm81x_ratecode_dup_bw_index_set(mm81x_rate_code_t * rc,enum dot11_bandwidth dup_bw_index)141*b1906ceaSLachlan Hodges mm81x_ratecode_dup_bw_index_set(mm81x_rate_code_t *rc,
142*b1906ceaSLachlan Hodges 				enum dot11_bandwidth dup_bw_index)
143*b1906ceaSLachlan Hodges {
144*b1906ceaSLachlan Hodges 	*rc = (*rc & cpu_to_le32(~MM81X_RATECODE_DUP_BW_INDEX)) |
145*b1906ceaSLachlan Hodges 	      le32_encode_bits(dup_bw_index, MM81X_RATECODE_DUP_BW_INDEX);
146*b1906ceaSLachlan Hodges }
147*b1906ceaSLachlan Hodges 
mm81x_ratecode_enable_rts(mm81x_rate_code_t * rc)148*b1906ceaSLachlan Hodges static inline void mm81x_ratecode_enable_rts(mm81x_rate_code_t *rc)
149*b1906ceaSLachlan Hodges {
150*b1906ceaSLachlan Hodges 	*rc |= cpu_to_le32(MM81X_RATECODE_RTS_FLAG);
151*b1906ceaSLachlan Hodges }
152*b1906ceaSLachlan Hodges 
mm81x_ratecode_enable_sgi(mm81x_rate_code_t * rc)153*b1906ceaSLachlan Hodges static inline void mm81x_ratecode_enable_sgi(mm81x_rate_code_t *rc)
154*b1906ceaSLachlan Hodges {
155*b1906ceaSLachlan Hodges 	*rc |= cpu_to_le32(MM81X_RATECODE_SHORT_GI_FLAG);
156*b1906ceaSLachlan Hodges }
157*b1906ceaSLachlan Hodges 
mm81x_ratecode_bw_mhz_to_bw_index(u8 bw_mhz)158*b1906ceaSLachlan Hodges static inline enum dot11_bandwidth mm81x_ratecode_bw_mhz_to_bw_index(u8 bw_mhz)
159*b1906ceaSLachlan Hodges {
160*b1906ceaSLachlan Hodges 	return ((bw_mhz == 1) ? DOT11_BANDWIDTH_1MHZ :
161*b1906ceaSLachlan Hodges 		(bw_mhz == 2) ? DOT11_BANDWIDTH_2MHZ :
162*b1906ceaSLachlan Hodges 		(bw_mhz == 4) ? DOT11_BANDWIDTH_4MHZ :
163*b1906ceaSLachlan Hodges 		(bw_mhz == 8) ? DOT11_BANDWIDTH_8MHZ :
164*b1906ceaSLachlan Hodges 				DOT11_BANDWIDTH_2MHZ);
165*b1906ceaSLachlan Hodges }
166*b1906ceaSLachlan Hodges 
167*b1906ceaSLachlan Hodges static inline u8
mm81x_ratecode_bw_index_to_s1g_bw_mhz(enum dot11_bandwidth bw_idx)168*b1906ceaSLachlan Hodges mm81x_ratecode_bw_index_to_s1g_bw_mhz(enum dot11_bandwidth bw_idx)
169*b1906ceaSLachlan Hodges {
170*b1906ceaSLachlan Hodges 	return ((bw_idx == DOT11_BANDWIDTH_1MHZ) ? 1 :
171*b1906ceaSLachlan Hodges 		(bw_idx == DOT11_BANDWIDTH_2MHZ) ? 2 :
172*b1906ceaSLachlan Hodges 		(bw_idx == DOT11_BANDWIDTH_4MHZ) ? 4 :
173*b1906ceaSLachlan Hodges 		(bw_idx == DOT11_BANDWIDTH_8MHZ) ? 8 :
174*b1906ceaSLachlan Hodges 						   2);
175*b1906ceaSLachlan Hodges }
176*b1906ceaSLachlan Hodges 
177*b1906ceaSLachlan Hodges #endif
178