antsel.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/slab.h>
  17. #include <net/mac80211.h>
  18. #include "types.h"
  19. #include "main.h"
  20. #include "phy_shim.h"
  21. #include "antsel.h"
  22. #define ANT_SELCFG_AUTO 0x80 /* bit indicates antenna sel AUTO */
  23. #define ANT_SELCFG_MASK 0x33 /* antenna configuration mask */
  24. #define ANT_SELCFG_TX_UNICAST 0 /* unicast tx antenna configuration */
  25. #define ANT_SELCFG_RX_UNICAST 1 /* unicast rx antenna configuration */
  26. #define ANT_SELCFG_TX_DEF 2 /* default tx antenna configuration */
  27. #define ANT_SELCFG_RX_DEF 3 /* default rx antenna configuration */
  28. /* useful macros */
  29. #define BRCMS_ANTSEL_11N_0(ant) ((((ant) & ANT_SELCFG_MASK) >> 4) & 0xf)
  30. #define BRCMS_ANTSEL_11N_1(ant) (((ant) & ANT_SELCFG_MASK) & 0xf)
  31. #define BRCMS_ANTIDX_11N(ant) (((BRCMS_ANTSEL_11N_0(ant)) << 2) +\
  32. (BRCMS_ANTSEL_11N_1(ant)))
  33. #define BRCMS_ANT_ISAUTO_11N(ant) (((ant) & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO)
  34. #define BRCMS_ANTSEL_11N(ant) ((ant) & ANT_SELCFG_MASK)
  35. /* antenna switch */
  36. /* defines for no boardlevel antenna diversity */
  37. #define ANT_SELCFG_DEF_2x2 0x01 /* default antenna configuration */
  38. /* 2x3 antdiv defines and tables for GPIO communication */
  39. #define ANT_SELCFG_NUM_2x3 3
  40. #define ANT_SELCFG_DEF_2x3 0x01 /* default antenna configuration */
  41. /* 2x4 antdiv rev4 defines and tables for GPIO communication */
  42. #define ANT_SELCFG_NUM_2x4 4
  43. #define ANT_SELCFG_DEF_2x4 0x02 /* default antenna configuration */
  44. static const u16 mimo_2x4_div_antselpat_tbl[] = {
  45. 0, 0, 0x9, 0xa, /* ant0: 0 ant1: 2,3 */
  46. 0, 0, 0x5, 0x6, /* ant0: 1 ant1: 2,3 */
  47. 0, 0, 0, 0, /* n.a. */
  48. 0, 0, 0, 0 /* n.a. */
  49. };
  50. static const u8 mimo_2x4_div_antselid_tbl[16] = {
  51. 0, 0, 0, 0, 0, 2, 3, 0,
  52. 0, 0, 1, 0, 0, 0, 0, 0 /* pat to antselid */
  53. };
  54. static const u16 mimo_2x3_div_antselpat_tbl[] = {
  55. 16, 0, 1, 16, /* ant0: 0 ant1: 1,2 */
  56. 16, 16, 16, 16, /* n.a. */
  57. 16, 2, 16, 16, /* ant0: 2 ant1: 1 */
  58. 16, 16, 16, 16 /* n.a. */
  59. };
  60. static const u8 mimo_2x3_div_antselid_tbl[16] = {
  61. 0, 1, 2, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0, 0, 0, 0, 0 /* pat to antselid */
  63. };
  64. /* boardlevel antenna selection: init antenna selection structure */
  65. static void
  66. brcms_c_antsel_init_cfg(struct antsel_info *asi, struct brcms_antselcfg *antsel,
  67. bool auto_sel)
  68. {
  69. if (asi->antsel_type == ANTSEL_2x3) {
  70. u8 antcfg_def = ANT_SELCFG_DEF_2x3 |
  71. ((asi->antsel_avail && auto_sel) ? ANT_SELCFG_AUTO : 0);
  72. antsel->ant_config[ANT_SELCFG_TX_DEF] = antcfg_def;
  73. antsel->ant_config[ANT_SELCFG_TX_UNICAST] = antcfg_def;
  74. antsel->ant_config[ANT_SELCFG_RX_DEF] = antcfg_def;
  75. antsel->ant_config[ANT_SELCFG_RX_UNICAST] = antcfg_def;
  76. antsel->num_antcfg = ANT_SELCFG_NUM_2x3;
  77. } else if (asi->antsel_type == ANTSEL_2x4) {
  78. antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x4;
  79. antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x4;
  80. antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x4;
  81. antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x4;
  82. antsel->num_antcfg = ANT_SELCFG_NUM_2x4;
  83. } else { /* no antenna selection available */
  84. antsel->ant_config[ANT_SELCFG_TX_DEF] = ANT_SELCFG_DEF_2x2;
  85. antsel->ant_config[ANT_SELCFG_TX_UNICAST] = ANT_SELCFG_DEF_2x2;
  86. antsel->ant_config[ANT_SELCFG_RX_DEF] = ANT_SELCFG_DEF_2x2;
  87. antsel->ant_config[ANT_SELCFG_RX_UNICAST] = ANT_SELCFG_DEF_2x2;
  88. antsel->num_antcfg = 0;
  89. }
  90. }
  91. struct antsel_info *brcms_c_antsel_attach(struct brcms_c_info *wlc)
  92. {
  93. struct antsel_info *asi;
  94. struct si_pub *sih = wlc->hw->sih;
  95. asi = kzalloc(sizeof(struct antsel_info), GFP_ATOMIC);
  96. if (!asi)
  97. return NULL;
  98. asi->wlc = wlc;
  99. asi->pub = wlc->pub;
  100. asi->antsel_type = ANTSEL_NA;
  101. asi->antsel_avail = false;
  102. asi->antsel_antswitch = (u8) getintvar(sih, BRCMS_SROM_ANTSWITCH);
  103. if ((asi->pub->sromrev >= 4) && (asi->antsel_antswitch != 0)) {
  104. switch (asi->antsel_antswitch) {
  105. case ANTSWITCH_TYPE_1:
  106. case ANTSWITCH_TYPE_2:
  107. case ANTSWITCH_TYPE_3:
  108. /* 4321/2 board with 2x3 switch logic */
  109. asi->antsel_type = ANTSEL_2x3;
  110. /* Antenna selection availability */
  111. if (((u16) getintvar(sih, BRCMS_SROM_AA2G) == 7) ||
  112. ((u16) getintvar(sih, BRCMS_SROM_AA5G) == 7)) {
  113. asi->antsel_avail = true;
  114. } else if (
  115. (u16) getintvar(sih, BRCMS_SROM_AA2G) == 3 ||
  116. (u16) getintvar(sih, BRCMS_SROM_AA5G) == 3) {
  117. asi->antsel_avail = false;
  118. } else {
  119. asi->antsel_avail = false;
  120. wiphy_err(wlc->wiphy, "antsel_attach: 2o3 "
  121. "board cfg invalid\n");
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. } else if ((asi->pub->sromrev == 4) &&
  128. ((u16) getintvar(sih, BRCMS_SROM_AA2G) == 7) &&
  129. ((u16) getintvar(sih, BRCMS_SROM_AA5G) == 0)) {
  130. /* hack to match old 4321CB2 cards with 2of3 antenna switch */
  131. asi->antsel_type = ANTSEL_2x3;
  132. asi->antsel_avail = true;
  133. } else if (asi->pub->boardflags2 & BFL2_2X4_DIV) {
  134. asi->antsel_type = ANTSEL_2x4;
  135. asi->antsel_avail = true;
  136. }
  137. /* Set the antenna selection type for the low driver */
  138. brcms_b_antsel_type_set(wlc->hw, asi->antsel_type);
  139. /* Init (auto/manual) antenna selection */
  140. brcms_c_antsel_init_cfg(asi, &asi->antcfg_11n, true);
  141. brcms_c_antsel_init_cfg(asi, &asi->antcfg_cur, true);
  142. return asi;
  143. }
  144. void brcms_c_antsel_detach(struct antsel_info *asi)
  145. {
  146. kfree(asi);
  147. }
  148. /*
  149. * boardlevel antenna selection:
  150. * convert ant_cfg to mimo_antsel (ucode interface)
  151. */
  152. static u16 brcms_c_antsel_antcfg2antsel(struct antsel_info *asi, u8 ant_cfg)
  153. {
  154. u8 idx = BRCMS_ANTIDX_11N(BRCMS_ANTSEL_11N(ant_cfg));
  155. u16 mimo_antsel = 0;
  156. if (asi->antsel_type == ANTSEL_2x4) {
  157. /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
  158. mimo_antsel = (mimo_2x4_div_antselpat_tbl[idx] & 0xf);
  159. return mimo_antsel;
  160. } else if (asi->antsel_type == ANTSEL_2x3) {
  161. /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
  162. mimo_antsel = (mimo_2x3_div_antselpat_tbl[idx] & 0xf);
  163. return mimo_antsel;
  164. }
  165. return mimo_antsel;
  166. }
  167. /* boardlevel antenna selection: ucode interface control */
  168. static int brcms_c_antsel_cfgupd(struct antsel_info *asi,
  169. struct brcms_antselcfg *antsel)
  170. {
  171. struct brcms_c_info *wlc = asi->wlc;
  172. u8 ant_cfg;
  173. u16 mimo_antsel;
  174. /* 1) Update TX antconfig for all frames that are not unicast data
  175. * (aka default TX)
  176. */
  177. ant_cfg = antsel->ant_config[ANT_SELCFG_TX_DEF];
  178. mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
  179. brcms_b_write_shm(wlc->hw, M_MIMO_ANTSEL_TXDFLT, mimo_antsel);
  180. /*
  181. * Update driver stats for currently selected
  182. * default tx/rx antenna config
  183. */
  184. asi->antcfg_cur.ant_config[ANT_SELCFG_TX_DEF] = ant_cfg;
  185. /* 2) Update RX antconfig for all frames that are not unicast data
  186. * (aka default RX)
  187. */
  188. ant_cfg = antsel->ant_config[ANT_SELCFG_RX_DEF];
  189. mimo_antsel = brcms_c_antsel_antcfg2antsel(asi, ant_cfg);
  190. brcms_b_write_shm(wlc->hw, M_MIMO_ANTSEL_RXDFLT, mimo_antsel);
  191. /*
  192. * Update driver stats for currently selected
  193. * default tx/rx antenna config
  194. */
  195. asi->antcfg_cur.ant_config[ANT_SELCFG_RX_DEF] = ant_cfg;
  196. return 0;
  197. }
  198. void brcms_c_antsel_init(struct antsel_info *asi)
  199. {
  200. if ((asi->antsel_type == ANTSEL_2x3) ||
  201. (asi->antsel_type == ANTSEL_2x4))
  202. brcms_c_antsel_cfgupd(asi, &asi->antcfg_11n);
  203. }
  204. /* boardlevel antenna selection: convert id to ant_cfg */
  205. static u8 brcms_c_antsel_id2antcfg(struct antsel_info *asi, u8 id)
  206. {
  207. u8 antcfg = ANT_SELCFG_DEF_2x2;
  208. if (asi->antsel_type == ANTSEL_2x4) {
  209. /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
  210. antcfg = (((id & 0x2) << 3) | ((id & 0x1) + 2));
  211. return antcfg;
  212. } else if (asi->antsel_type == ANTSEL_2x3) {
  213. /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
  214. antcfg = (((id & 0x02) << 4) | ((id & 0x1) + 1));
  215. return antcfg;
  216. }
  217. return antcfg;
  218. }
  219. void
  220. brcms_c_antsel_antcfg_get(struct antsel_info *asi, bool usedef, bool sel,
  221. u8 antselid, u8 fbantselid, u8 *antcfg,
  222. u8 *fbantcfg)
  223. {
  224. u8 ant;
  225. /* if use default, assign it and return */
  226. if (usedef) {
  227. *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_DEF];
  228. *fbantcfg = *antcfg;
  229. return;
  230. }
  231. if (!sel) {
  232. *antcfg = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
  233. *fbantcfg = *antcfg;
  234. } else {
  235. ant = asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
  236. if ((ant & ANT_SELCFG_AUTO) == ANT_SELCFG_AUTO) {
  237. *antcfg = brcms_c_antsel_id2antcfg(asi, antselid);
  238. *fbantcfg = brcms_c_antsel_id2antcfg(asi, fbantselid);
  239. } else {
  240. *antcfg =
  241. asi->antcfg_11n.ant_config[ANT_SELCFG_TX_UNICAST];
  242. *fbantcfg = *antcfg;
  243. }
  244. }
  245. return;
  246. }
  247. /* boardlevel antenna selection: convert mimo_antsel (ucode interface) to id */
  248. u8 brcms_c_antsel_antsel2id(struct antsel_info *asi, u16 antsel)
  249. {
  250. u8 antselid = 0;
  251. if (asi->antsel_type == ANTSEL_2x4) {
  252. /* 2x4 antenna diversity board, 4 cfgs: 0-2 0-3 1-2 1-3 */
  253. antselid = mimo_2x4_div_antselid_tbl[(antsel & 0xf)];
  254. return antselid;
  255. } else if (asi->antsel_type == ANTSEL_2x3) {
  256. /* 2x3 antenna selection, 3 cfgs: 0-1 0-2 2-1 */
  257. antselid = mimo_2x3_div_antselid_tbl[(antsel & 0xf)];
  258. return antselid;
  259. }
  260. return antselid;
  261. }