ar9003_mci.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  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
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/export.h>
  17. #include "hw.h"
  18. #include "hw-ops.h"
  19. #include "ar9003_phy.h"
  20. #include "ar9003_mci.h"
  21. static void ar9003_mci_reset_req_wakeup(struct ath_hw *ah)
  22. {
  23. REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
  24. AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 1);
  25. udelay(1);
  26. REG_RMW_FIELD(ah, AR_MCI_COMMAND2,
  27. AR_MCI_COMMAND2_RESET_REQ_WAKEUP, 0);
  28. }
  29. static int ar9003_mci_wait_for_interrupt(struct ath_hw *ah, u32 address,
  30. u32 bit_position, int time_out)
  31. {
  32. struct ath_common *common = ath9k_hw_common(ah);
  33. while (time_out) {
  34. if (!(REG_READ(ah, address) & bit_position)) {
  35. udelay(10);
  36. time_out -= 10;
  37. if (time_out < 0)
  38. break;
  39. else
  40. continue;
  41. }
  42. REG_WRITE(ah, address, bit_position);
  43. if (address != AR_MCI_INTERRUPT_RX_MSG_RAW)
  44. break;
  45. if (bit_position & AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE)
  46. ar9003_mci_reset_req_wakeup(ah);
  47. if (bit_position & (AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING |
  48. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING))
  49. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  50. AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
  51. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_RX_MSG);
  52. break;
  53. }
  54. if (time_out <= 0) {
  55. ath_dbg(common, MCI,
  56. "MCI Wait for Reg 0x%08x = 0x%08x timeout\n",
  57. address, bit_position);
  58. ath_dbg(common, MCI,
  59. "MCI INT_RAW = 0x%08x, RX_MSG_RAW = 0x%08x\n",
  60. REG_READ(ah, AR_MCI_INTERRUPT_RAW),
  61. REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
  62. time_out = 0;
  63. }
  64. return time_out;
  65. }
  66. static void ar9003_mci_remote_reset(struct ath_hw *ah, bool wait_done)
  67. {
  68. u32 payload[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffff00};
  69. ar9003_mci_send_message(ah, MCI_REMOTE_RESET, 0, payload, 16,
  70. wait_done, false);
  71. udelay(5);
  72. }
  73. static void ar9003_mci_send_lna_transfer(struct ath_hw *ah, bool wait_done)
  74. {
  75. u32 payload = 0x00000000;
  76. ar9003_mci_send_message(ah, MCI_LNA_TRANS, 0, &payload, 1,
  77. wait_done, false);
  78. }
  79. static void ar9003_mci_send_req_wake(struct ath_hw *ah, bool wait_done)
  80. {
  81. ar9003_mci_send_message(ah, MCI_REQ_WAKE, MCI_FLAG_DISABLE_TIMESTAMP,
  82. NULL, 0, wait_done, false);
  83. udelay(5);
  84. }
  85. static void ar9003_mci_send_sys_waking(struct ath_hw *ah, bool wait_done)
  86. {
  87. ar9003_mci_send_message(ah, MCI_SYS_WAKING, MCI_FLAG_DISABLE_TIMESTAMP,
  88. NULL, 0, wait_done, false);
  89. }
  90. static void ar9003_mci_send_lna_take(struct ath_hw *ah, bool wait_done)
  91. {
  92. u32 payload = 0x70000000;
  93. ar9003_mci_send_message(ah, MCI_LNA_TAKE, 0, &payload, 1,
  94. wait_done, false);
  95. }
  96. static void ar9003_mci_send_sys_sleeping(struct ath_hw *ah, bool wait_done)
  97. {
  98. ar9003_mci_send_message(ah, MCI_SYS_SLEEPING,
  99. MCI_FLAG_DISABLE_TIMESTAMP,
  100. NULL, 0, wait_done, false);
  101. }
  102. static void ar9003_mci_send_coex_version_query(struct ath_hw *ah,
  103. bool wait_done)
  104. {
  105. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  106. u32 payload[4] = {0, 0, 0, 0};
  107. if (mci->bt_version_known ||
  108. (mci->bt_state == MCI_BT_SLEEP))
  109. return;
  110. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  111. MCI_GPM_COEX_VERSION_QUERY);
  112. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  113. }
  114. static void ar9003_mci_send_coex_version_response(struct ath_hw *ah,
  115. bool wait_done)
  116. {
  117. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  118. u32 payload[4] = {0, 0, 0, 0};
  119. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  120. MCI_GPM_COEX_VERSION_RESPONSE);
  121. *(((u8 *)payload) + MCI_GPM_COEX_B_MAJOR_VERSION) =
  122. mci->wlan_ver_major;
  123. *(((u8 *)payload) + MCI_GPM_COEX_B_MINOR_VERSION) =
  124. mci->wlan_ver_minor;
  125. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  126. }
  127. static void ar9003_mci_send_coex_wlan_channels(struct ath_hw *ah,
  128. bool wait_done)
  129. {
  130. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  131. u32 *payload = &mci->wlan_channels[0];
  132. if (!mci->wlan_channels_update ||
  133. (mci->bt_state == MCI_BT_SLEEP))
  134. return;
  135. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  136. MCI_GPM_COEX_WLAN_CHANNELS);
  137. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  138. MCI_GPM_SET_TYPE_OPCODE(payload, 0xff, 0xff);
  139. }
  140. static void ar9003_mci_send_coex_bt_status_query(struct ath_hw *ah,
  141. bool wait_done, u8 query_type)
  142. {
  143. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  144. u32 payload[4] = {0, 0, 0, 0};
  145. bool query_btinfo;
  146. if (mci->bt_state == MCI_BT_SLEEP)
  147. return;
  148. query_btinfo = !!(query_type & (MCI_GPM_COEX_QUERY_BT_ALL_INFO |
  149. MCI_GPM_COEX_QUERY_BT_TOPOLOGY));
  150. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  151. MCI_GPM_COEX_STATUS_QUERY);
  152. *(((u8 *)payload) + MCI_GPM_COEX_B_BT_BITMAP) = query_type;
  153. /*
  154. * If bt_status_query message is not sent successfully,
  155. * then need_flush_btinfo should be set again.
  156. */
  157. if (!ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
  158. wait_done, true)) {
  159. if (query_btinfo)
  160. mci->need_flush_btinfo = true;
  161. }
  162. if (query_btinfo)
  163. mci->query_bt = false;
  164. }
  165. static void ar9003_mci_send_coex_halt_bt_gpm(struct ath_hw *ah, bool halt,
  166. bool wait_done)
  167. {
  168. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  169. u32 payload[4] = {0, 0, 0, 0};
  170. MCI_GPM_SET_TYPE_OPCODE(payload, MCI_GPM_COEX_AGENT,
  171. MCI_GPM_COEX_HALT_BT_GPM);
  172. if (halt) {
  173. mci->query_bt = true;
  174. /* Send next unhalt no matter halt sent or not */
  175. mci->unhalt_bt_gpm = true;
  176. mci->need_flush_btinfo = true;
  177. *(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
  178. MCI_GPM_COEX_BT_GPM_HALT;
  179. } else
  180. *(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) =
  181. MCI_GPM_COEX_BT_GPM_UNHALT;
  182. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16, wait_done, true);
  183. }
  184. static void ar9003_mci_prep_interface(struct ath_hw *ah)
  185. {
  186. struct ath_common *common = ath9k_hw_common(ah);
  187. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  188. u32 saved_mci_int_en;
  189. u32 mci_timeout = 150;
  190. mci->bt_state = MCI_BT_SLEEP;
  191. saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
  192. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  193. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  194. REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW));
  195. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  196. REG_READ(ah, AR_MCI_INTERRUPT_RAW));
  197. ar9003_mci_remote_reset(ah, true);
  198. ar9003_mci_send_req_wake(ah, true);
  199. if (!ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  200. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING, 500))
  201. goto clear_redunt;
  202. mci->bt_state = MCI_BT_AWAKE;
  203. /*
  204. * we don't need to send more remote_reset at this moment.
  205. * If BT receive first remote_reset, then BT HW will
  206. * be cleaned up and will be able to receive req_wake
  207. * and BT HW will respond sys_waking.
  208. * In this case, WLAN will receive BT's HW sys_waking.
  209. * Otherwise, if BT SW missed initial remote_reset,
  210. * that remote_reset will still clean up BT MCI RX,
  211. * and the req_wake will wake BT up,
  212. * and BT SW will respond this req_wake with a remote_reset and
  213. * sys_waking. In this case, WLAN will receive BT's SW
  214. * sys_waking. In either case, BT's RX is cleaned up. So we
  215. * don't need to reply BT's remote_reset now, if any.
  216. * Similarly, if in any case, WLAN can receive BT's sys_waking,
  217. * that means WLAN's RX is also fine.
  218. */
  219. ar9003_mci_send_sys_waking(ah, true);
  220. udelay(10);
  221. /*
  222. * Set BT priority interrupt value to be 0xff to
  223. * avoid having too many BT PRIORITY interrupts.
  224. */
  225. REG_WRITE(ah, AR_MCI_BT_PRI0, 0xFFFFFFFF);
  226. REG_WRITE(ah, AR_MCI_BT_PRI1, 0xFFFFFFFF);
  227. REG_WRITE(ah, AR_MCI_BT_PRI2, 0xFFFFFFFF);
  228. REG_WRITE(ah, AR_MCI_BT_PRI3, 0xFFFFFFFF);
  229. REG_WRITE(ah, AR_MCI_BT_PRI, 0X000000FF);
  230. /*
  231. * A contention reset will be received after send out
  232. * sys_waking. Also BT priority interrupt bits will be set.
  233. * Clear those bits before the next step.
  234. */
  235. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  236. AR_MCI_INTERRUPT_RX_MSG_CONT_RST);
  237. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, AR_MCI_INTERRUPT_BT_PRI);
  238. if (mci->is_2g) {
  239. ar9003_mci_send_lna_transfer(ah, true);
  240. udelay(5);
  241. }
  242. if ((mci->is_2g && !mci->update_2g5g)) {
  243. if (ar9003_mci_wait_for_interrupt(ah,
  244. AR_MCI_INTERRUPT_RX_MSG_RAW,
  245. AR_MCI_INTERRUPT_RX_MSG_LNA_INFO,
  246. mci_timeout))
  247. ath_dbg(common, MCI,
  248. "MCI WLAN has control over the LNA & BT obeys it\n");
  249. else
  250. ath_dbg(common, MCI,
  251. "MCI BT didn't respond to LNA_TRANS\n");
  252. }
  253. clear_redunt:
  254. /* Clear the extra redundant SYS_WAKING from BT */
  255. if ((mci->bt_state == MCI_BT_AWAKE) &&
  256. (REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  257. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING)) &&
  258. (REG_READ_FIELD(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  259. AR_MCI_INTERRUPT_RX_MSG_SYS_SLEEPING) == 0)) {
  260. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  261. AR_MCI_INTERRUPT_RX_MSG_SYS_WAKING);
  262. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  263. AR_MCI_INTERRUPT_REMOTE_SLEEP_UPDATE);
  264. }
  265. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
  266. }
  267. void ar9003_mci_set_full_sleep(struct ath_hw *ah)
  268. {
  269. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  270. if (ar9003_mci_state(ah, MCI_STATE_ENABLE) &&
  271. (mci->bt_state != MCI_BT_SLEEP) &&
  272. !mci->halted_bt_gpm) {
  273. ar9003_mci_send_coex_halt_bt_gpm(ah, true, true);
  274. }
  275. mci->ready = false;
  276. }
  277. static void ar9003_mci_disable_interrupt(struct ath_hw *ah)
  278. {
  279. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  280. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
  281. }
  282. static void ar9003_mci_enable_interrupt(struct ath_hw *ah)
  283. {
  284. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, AR_MCI_INTERRUPT_DEFAULT);
  285. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN,
  286. AR_MCI_INTERRUPT_RX_MSG_DEFAULT);
  287. }
  288. static bool ar9003_mci_check_int(struct ath_hw *ah, u32 ints)
  289. {
  290. u32 intr;
  291. intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
  292. return ((intr & ints) == ints);
  293. }
  294. void ar9003_mci_get_interrupt(struct ath_hw *ah, u32 *raw_intr,
  295. u32 *rx_msg_intr)
  296. {
  297. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  298. *raw_intr = mci->raw_intr;
  299. *rx_msg_intr = mci->rx_msg_intr;
  300. /* Clean int bits after the values are read. */
  301. mci->raw_intr = 0;
  302. mci->rx_msg_intr = 0;
  303. }
  304. EXPORT_SYMBOL(ar9003_mci_get_interrupt);
  305. void ar9003_mci_get_isr(struct ath_hw *ah, enum ath9k_int *masked)
  306. {
  307. struct ath_common *common = ath9k_hw_common(ah);
  308. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  309. u32 raw_intr, rx_msg_intr;
  310. rx_msg_intr = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_RAW);
  311. raw_intr = REG_READ(ah, AR_MCI_INTERRUPT_RAW);
  312. if ((raw_intr == 0xdeadbeef) || (rx_msg_intr == 0xdeadbeef)) {
  313. ath_dbg(common, MCI,
  314. "MCI gets 0xdeadbeef during int processing\n");
  315. } else {
  316. mci->rx_msg_intr |= rx_msg_intr;
  317. mci->raw_intr |= raw_intr;
  318. *masked |= ATH9K_INT_MCI;
  319. if (rx_msg_intr & AR_MCI_INTERRUPT_RX_MSG_CONT_INFO)
  320. mci->cont_status = REG_READ(ah, AR_MCI_CONT_STATUS);
  321. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW, rx_msg_intr);
  322. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW, raw_intr);
  323. }
  324. }
  325. static void ar9003_mci_2g5g_changed(struct ath_hw *ah, bool is_2g)
  326. {
  327. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  328. if (!mci->update_2g5g &&
  329. (mci->is_2g != is_2g))
  330. mci->update_2g5g = true;
  331. mci->is_2g = is_2g;
  332. }
  333. static bool ar9003_mci_is_gpm_valid(struct ath_hw *ah, u32 msg_index)
  334. {
  335. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  336. u32 *payload;
  337. u32 recv_type, offset;
  338. if (msg_index == MCI_GPM_INVALID)
  339. return false;
  340. offset = msg_index << 4;
  341. payload = (u32 *)(mci->gpm_buf + offset);
  342. recv_type = MCI_GPM_TYPE(payload);
  343. if (recv_type == MCI_GPM_RSVD_PATTERN)
  344. return false;
  345. return true;
  346. }
  347. static void ar9003_mci_observation_set_up(struct ath_hw *ah)
  348. {
  349. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  350. if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MCI) {
  351. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_DATA);
  352. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_MCI_WLAN_CLK);
  353. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
  354. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
  355. } else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_TXRX) {
  356. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_WL_IN_TX);
  357. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_WL_IN_RX);
  358. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
  359. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
  360. ath9k_hw_cfg_output(ah, 5, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  361. } else if (mci->config & ATH_MCI_CONFIG_MCI_OBS_BT) {
  362. ath9k_hw_cfg_output(ah, 3, AR_GPIO_OUTPUT_MUX_AS_BT_IN_TX);
  363. ath9k_hw_cfg_output(ah, 2, AR_GPIO_OUTPUT_MUX_AS_BT_IN_RX);
  364. ath9k_hw_cfg_output(ah, 1, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_DATA);
  365. ath9k_hw_cfg_output(ah, 0, AR_GPIO_OUTPUT_MUX_AS_MCI_BT_CLK);
  366. } else
  367. return;
  368. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
  369. REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL, AR_GLB_DS_JTAG_DISABLE, 1);
  370. REG_RMW_FIELD(ah, AR_PHY_GLB_CONTROL, AR_GLB_WLAN_UART_INTF_EN, 0);
  371. REG_SET_BIT(ah, AR_GLB_GPIO_CONTROL, ATH_MCI_CONFIG_MCI_OBS_GPIO);
  372. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_GPIO_OBS_SEL, 0);
  373. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_MAC_BB_OBS_SEL, 1);
  374. REG_WRITE(ah, AR_OBS, 0x4b);
  375. REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL1, 0x03);
  376. REG_RMW_FIELD(ah, AR_DIAG_SW, AR_DIAG_OBS_PT_SEL2, 0x01);
  377. REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_LSB, 0x02);
  378. REG_RMW_FIELD(ah, AR_MACMISC, AR_MACMISC_MISC_OBS_BUS_MSB, 0x03);
  379. REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS,
  380. AR_PHY_TEST_CTL_DEBUGPORT_SEL, 0x07);
  381. }
  382. static bool ar9003_mci_send_coex_bt_flags(struct ath_hw *ah, bool wait_done,
  383. u8 opcode, u32 bt_flags)
  384. {
  385. u32 pld[4] = {0, 0, 0, 0};
  386. MCI_GPM_SET_TYPE_OPCODE(pld, MCI_GPM_COEX_AGENT,
  387. MCI_GPM_COEX_BT_UPDATE_FLAGS);
  388. *(((u8 *)pld) + MCI_GPM_COEX_B_BT_FLAGS_OP) = opcode;
  389. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 0) = bt_flags & 0xFF;
  390. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 1) = (bt_flags >> 8) & 0xFF;
  391. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 2) = (bt_flags >> 16) & 0xFF;
  392. *(((u8 *)pld) + MCI_GPM_COEX_W_BT_FLAGS + 3) = (bt_flags >> 24) & 0xFF;
  393. return ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16,
  394. wait_done, true);
  395. }
  396. static void ar9003_mci_sync_bt_state(struct ath_hw *ah)
  397. {
  398. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  399. u32 cur_bt_state;
  400. cur_bt_state = ar9003_mci_state(ah, MCI_STATE_REMOTE_SLEEP);
  401. if (mci->bt_state != cur_bt_state)
  402. mci->bt_state = cur_bt_state;
  403. if (mci->bt_state != MCI_BT_SLEEP) {
  404. ar9003_mci_send_coex_version_query(ah, true);
  405. ar9003_mci_send_coex_wlan_channels(ah, true);
  406. if (mci->unhalt_bt_gpm == true)
  407. ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
  408. }
  409. }
  410. void ar9003_mci_check_bt(struct ath_hw *ah)
  411. {
  412. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  413. if (!mci_hw->ready)
  414. return;
  415. /*
  416. * check BT state again to make
  417. * sure it's not changed.
  418. */
  419. ar9003_mci_sync_bt_state(ah);
  420. ar9003_mci_2g5g_switch(ah, true);
  421. if ((mci_hw->bt_state == MCI_BT_AWAKE) &&
  422. (mci_hw->query_bt == true)) {
  423. mci_hw->need_flush_btinfo = true;
  424. }
  425. }
  426. static void ar9003_mci_process_gpm_extra(struct ath_hw *ah, u8 gpm_type,
  427. u8 gpm_opcode, u32 *p_gpm)
  428. {
  429. struct ath_common *common = ath9k_hw_common(ah);
  430. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  431. u8 *p_data = (u8 *) p_gpm;
  432. if (gpm_type != MCI_GPM_COEX_AGENT)
  433. return;
  434. switch (gpm_opcode) {
  435. case MCI_GPM_COEX_VERSION_QUERY:
  436. ath_dbg(common, MCI, "MCI Recv GPM COEX Version Query\n");
  437. ar9003_mci_send_coex_version_response(ah, true);
  438. break;
  439. case MCI_GPM_COEX_VERSION_RESPONSE:
  440. ath_dbg(common, MCI, "MCI Recv GPM COEX Version Response\n");
  441. mci->bt_ver_major =
  442. *(p_data + MCI_GPM_COEX_B_MAJOR_VERSION);
  443. mci->bt_ver_minor =
  444. *(p_data + MCI_GPM_COEX_B_MINOR_VERSION);
  445. mci->bt_version_known = true;
  446. ath_dbg(common, MCI, "MCI BT Coex version: %d.%d\n",
  447. mci->bt_ver_major, mci->bt_ver_minor);
  448. break;
  449. case MCI_GPM_COEX_STATUS_QUERY:
  450. ath_dbg(common, MCI,
  451. "MCI Recv GPM COEX Status Query = 0x%02X\n",
  452. *(p_data + MCI_GPM_COEX_B_WLAN_BITMAP));
  453. mci->wlan_channels_update = true;
  454. ar9003_mci_send_coex_wlan_channels(ah, true);
  455. break;
  456. case MCI_GPM_COEX_BT_PROFILE_INFO:
  457. mci->query_bt = true;
  458. ath_dbg(common, MCI, "MCI Recv GPM COEX BT_Profile_Info\n");
  459. break;
  460. case MCI_GPM_COEX_BT_STATUS_UPDATE:
  461. mci->query_bt = true;
  462. ath_dbg(common, MCI,
  463. "MCI Recv GPM COEX BT_Status_Update SEQ=%d (drop&query)\n",
  464. *(p_gpm + 3));
  465. break;
  466. default:
  467. break;
  468. }
  469. }
  470. static u32 ar9003_mci_wait_for_gpm(struct ath_hw *ah, u8 gpm_type,
  471. u8 gpm_opcode, int time_out)
  472. {
  473. struct ath_common *common = ath9k_hw_common(ah);
  474. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  475. u32 *p_gpm = NULL, mismatch = 0, more_data;
  476. u32 offset;
  477. u8 recv_type = 0, recv_opcode = 0;
  478. bool b_is_bt_cal_done = (gpm_type == MCI_GPM_BT_CAL_DONE);
  479. more_data = time_out ? MCI_GPM_NOMORE : MCI_GPM_MORE;
  480. while (time_out > 0) {
  481. if (p_gpm) {
  482. MCI_GPM_RECYCLE(p_gpm);
  483. p_gpm = NULL;
  484. }
  485. if (more_data != MCI_GPM_MORE)
  486. time_out = ar9003_mci_wait_for_interrupt(ah,
  487. AR_MCI_INTERRUPT_RX_MSG_RAW,
  488. AR_MCI_INTERRUPT_RX_MSG_GPM,
  489. time_out);
  490. if (!time_out)
  491. break;
  492. offset = ar9003_mci_get_next_gpm_offset(ah, false, &more_data);
  493. if (offset == MCI_GPM_INVALID)
  494. continue;
  495. p_gpm = (u32 *) (mci->gpm_buf + offset);
  496. recv_type = MCI_GPM_TYPE(p_gpm);
  497. recv_opcode = MCI_GPM_OPCODE(p_gpm);
  498. if (MCI_GPM_IS_CAL_TYPE(recv_type)) {
  499. if (recv_type == gpm_type) {
  500. if ((gpm_type == MCI_GPM_BT_CAL_DONE) &&
  501. !b_is_bt_cal_done) {
  502. gpm_type = MCI_GPM_BT_CAL_GRANT;
  503. continue;
  504. }
  505. break;
  506. }
  507. } else if ((recv_type == gpm_type) &&
  508. (recv_opcode == gpm_opcode))
  509. break;
  510. /*
  511. * check if it's cal_grant
  512. *
  513. * When we're waiting for cal_grant in reset routine,
  514. * it's possible that BT sends out cal_request at the
  515. * same time. Since BT's calibration doesn't happen
  516. * that often, we'll let BT completes calibration then
  517. * we continue to wait for cal_grant from BT.
  518. * Orginal: Wait BT_CAL_GRANT.
  519. * New: Receive BT_CAL_REQ -> send WLAN_CAL_GRANT->wait
  520. * BT_CAL_DONE -> Wait BT_CAL_GRANT.
  521. */
  522. if ((gpm_type == MCI_GPM_BT_CAL_GRANT) &&
  523. (recv_type == MCI_GPM_BT_CAL_REQ)) {
  524. u32 payload[4] = {0, 0, 0, 0};
  525. gpm_type = MCI_GPM_BT_CAL_DONE;
  526. MCI_GPM_SET_CAL_TYPE(payload,
  527. MCI_GPM_WLAN_CAL_GRANT);
  528. ar9003_mci_send_message(ah, MCI_GPM, 0, payload, 16,
  529. false, false);
  530. continue;
  531. } else {
  532. ath_dbg(common, MCI, "MCI GPM subtype not match 0x%x\n",
  533. *(p_gpm + 1));
  534. mismatch++;
  535. ar9003_mci_process_gpm_extra(ah, recv_type,
  536. recv_opcode, p_gpm);
  537. }
  538. }
  539. if (p_gpm) {
  540. MCI_GPM_RECYCLE(p_gpm);
  541. p_gpm = NULL;
  542. }
  543. if (time_out <= 0)
  544. time_out = 0;
  545. while (more_data == MCI_GPM_MORE) {
  546. offset = ar9003_mci_get_next_gpm_offset(ah, false, &more_data);
  547. if (offset == MCI_GPM_INVALID)
  548. break;
  549. p_gpm = (u32 *) (mci->gpm_buf + offset);
  550. recv_type = MCI_GPM_TYPE(p_gpm);
  551. recv_opcode = MCI_GPM_OPCODE(p_gpm);
  552. if (!MCI_GPM_IS_CAL_TYPE(recv_type))
  553. ar9003_mci_process_gpm_extra(ah, recv_type,
  554. recv_opcode, p_gpm);
  555. MCI_GPM_RECYCLE(p_gpm);
  556. }
  557. return time_out;
  558. }
  559. bool ar9003_mci_start_reset(struct ath_hw *ah, struct ath9k_channel *chan)
  560. {
  561. struct ath_common *common = ath9k_hw_common(ah);
  562. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  563. u32 payload[4] = {0, 0, 0, 0};
  564. ar9003_mci_2g5g_changed(ah, IS_CHAN_2GHZ(chan));
  565. if (mci_hw->bt_state != MCI_BT_CAL_START)
  566. return false;
  567. mci_hw->bt_state = MCI_BT_CAL;
  568. /*
  569. * MCI FIX: disable mci interrupt here. This is to avoid
  570. * SW_MSG_DONE or RX_MSG bits to trigger MCI_INT and
  571. * lead to mci_intr reentry.
  572. */
  573. ar9003_mci_disable_interrupt(ah);
  574. MCI_GPM_SET_CAL_TYPE(payload, MCI_GPM_WLAN_CAL_GRANT);
  575. ar9003_mci_send_message(ah, MCI_GPM, 0, payload,
  576. 16, true, false);
  577. /* Wait BT calibration to be completed for 25ms */
  578. if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_DONE,
  579. 0, 25000))
  580. ath_dbg(common, MCI, "MCI BT_CAL_DONE received\n");
  581. else
  582. ath_dbg(common, MCI,
  583. "MCI BT_CAL_DONE not received\n");
  584. mci_hw->bt_state = MCI_BT_AWAKE;
  585. /* MCI FIX: enable mci interrupt here */
  586. ar9003_mci_enable_interrupt(ah);
  587. return true;
  588. }
  589. EXPORT_SYMBOL(ar9003_mci_start_reset);
  590. int ar9003_mci_end_reset(struct ath_hw *ah, struct ath9k_channel *chan,
  591. struct ath9k_hw_cal_data *caldata)
  592. {
  593. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  594. if (!mci_hw->ready)
  595. return 0;
  596. if (!IS_CHAN_2GHZ(chan) || (mci_hw->bt_state != MCI_BT_SLEEP))
  597. goto exit;
  598. if (!ar9003_mci_check_int(ah, AR_MCI_INTERRUPT_RX_MSG_REMOTE_RESET) &&
  599. !ar9003_mci_check_int(ah, AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE))
  600. goto exit;
  601. /*
  602. * BT is sleeping. Check if BT wakes up during
  603. * WLAN calibration. If BT wakes up during
  604. * WLAN calibration, need to go through all
  605. * message exchanges again and recal.
  606. */
  607. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  608. (AR_MCI_INTERRUPT_RX_MSG_REMOTE_RESET |
  609. AR_MCI_INTERRUPT_RX_MSG_REQ_WAKE));
  610. ar9003_mci_remote_reset(ah, true);
  611. ar9003_mci_send_sys_waking(ah, true);
  612. udelay(1);
  613. if (IS_CHAN_2GHZ(chan))
  614. ar9003_mci_send_lna_transfer(ah, true);
  615. mci_hw->bt_state = MCI_BT_AWAKE;
  616. if (caldata) {
  617. caldata->done_txiqcal_once = false;
  618. caldata->done_txclcal_once = false;
  619. caldata->rtt_done = false;
  620. }
  621. if (!ath9k_hw_init_cal(ah, chan))
  622. return -EIO;
  623. exit:
  624. ar9003_mci_enable_interrupt(ah);
  625. return 0;
  626. }
  627. static void ar9003_mci_mute_bt(struct ath_hw *ah)
  628. {
  629. /* disable all MCI messages */
  630. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE, 0xffff0000);
  631. REG_SET_BIT(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  632. /* wait pending HW messages to flush out */
  633. udelay(10);
  634. /*
  635. * Send LNA_TAKE and SYS_SLEEPING when
  636. * 1. reset not after resuming from full sleep
  637. * 2. before reset MCI RX, to quiet BT and avoid MCI RX misalignment
  638. */
  639. ar9003_mci_send_lna_take(ah, true);
  640. udelay(5);
  641. ar9003_mci_send_sys_sleeping(ah, true);
  642. }
  643. static void ar9003_mci_osla_setup(struct ath_hw *ah, bool enable)
  644. {
  645. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  646. u32 thresh;
  647. if (!enable) {
  648. REG_CLR_BIT(ah, AR_BTCOEX_CTRL,
  649. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  650. return;
  651. }
  652. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2, AR_MCI_SCHD_TABLE_2_HW_BASED, 1);
  653. REG_RMW_FIELD(ah, AR_MCI_SCHD_TABLE_2,
  654. AR_MCI_SCHD_TABLE_2_MEM_BASED, 1);
  655. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_AGGR_THRESH)) {
  656. thresh = MS(mci->config, ATH_MCI_CONFIG_AGGR_THRESH);
  657. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  658. AR_BTCOEX_CTRL_AGGR_THRESH, thresh);
  659. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  660. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 1);
  661. } else
  662. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  663. AR_BTCOEX_CTRL_TIME_TO_NEXT_BT_THRESH_EN, 0);
  664. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL,
  665. AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN, 1);
  666. }
  667. int ar9003_mci_reset(struct ath_hw *ah, bool en_int, bool is_2g,
  668. bool is_full_sleep)
  669. {
  670. struct ath_common *common = ath9k_hw_common(ah);
  671. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  672. u32 regval;
  673. ath_dbg(common, MCI, "MCI Reset (full_sleep = %d, is_2g = %d)\n",
  674. is_full_sleep, is_2g);
  675. if (!mci->gpm_addr && !mci->sched_addr) {
  676. ath_err(common, "MCI GPM and schedule buffers are not allocated\n");
  677. return -ENOMEM;
  678. }
  679. if (REG_READ(ah, AR_BTCOEX_CTRL) == 0xdeadbeef) {
  680. ath_err(common, "BTCOEX control register is dead\n");
  681. return -EINVAL;
  682. }
  683. /* Program MCI DMA related registers */
  684. REG_WRITE(ah, AR_MCI_GPM_0, mci->gpm_addr);
  685. REG_WRITE(ah, AR_MCI_GPM_1, mci->gpm_len);
  686. REG_WRITE(ah, AR_MCI_SCHD_TABLE_0, mci->sched_addr);
  687. /*
  688. * To avoid MCI state machine be affected by incoming remote MCI msgs,
  689. * MCI mode will be enabled later, right before reset the MCI TX and RX.
  690. */
  691. regval = SM(1, AR_BTCOEX_CTRL_AR9462_MODE) |
  692. SM(1, AR_BTCOEX_CTRL_WBTIMER_EN) |
  693. SM(1, AR_BTCOEX_CTRL_PA_SHARED) |
  694. SM(1, AR_BTCOEX_CTRL_LNA_SHARED) |
  695. SM(2, AR_BTCOEX_CTRL_NUM_ANTENNAS) |
  696. SM(3, AR_BTCOEX_CTRL_RX_CHAIN_MASK) |
  697. SM(0, AR_BTCOEX_CTRL_1_CHAIN_ACK) |
  698. SM(0, AR_BTCOEX_CTRL_1_CHAIN_BCN) |
  699. SM(0, AR_BTCOEX_CTRL_ONE_STEP_LOOK_AHEAD_EN);
  700. REG_WRITE(ah, AR_BTCOEX_CTRL, regval);
  701. if (is_2g && !(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  702. ar9003_mci_osla_setup(ah, true);
  703. else
  704. ar9003_mci_osla_setup(ah, false);
  705. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  706. AR_BTCOEX_CTRL_SPDT_ENABLE);
  707. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL3,
  708. AR_BTCOEX_CTRL3_CONT_INFO_TIMEOUT, 20);
  709. REG_RMW_FIELD(ah, AR_BTCOEX_CTRL2, AR_BTCOEX_CTRL2_RX_DEWEIGHT, 1);
  710. REG_RMW_FIELD(ah, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX, 0);
  711. regval = MS(mci->config, ATH_MCI_CONFIG_CLK_DIV);
  712. REG_RMW_FIELD(ah, AR_MCI_TX_CTRL, AR_MCI_TX_CTRL_CLK_DIV, regval);
  713. REG_SET_BIT(ah, AR_BTCOEX_CTRL, AR_BTCOEX_CTRL_MCI_MODE_EN);
  714. /* Resetting the Rx and Tx paths of MCI */
  715. regval = REG_READ(ah, AR_MCI_COMMAND2);
  716. regval |= SM(1, AR_MCI_COMMAND2_RESET_TX);
  717. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  718. udelay(1);
  719. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_TX);
  720. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  721. if (is_full_sleep) {
  722. ar9003_mci_mute_bt(ah);
  723. udelay(100);
  724. }
  725. /* Check pending GPM msg before MCI Reset Rx */
  726. ar9003_mci_check_gpm_offset(ah);
  727. regval |= SM(1, AR_MCI_COMMAND2_RESET_RX);
  728. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  729. udelay(1);
  730. regval &= ~SM(1, AR_MCI_COMMAND2_RESET_RX);
  731. REG_WRITE(ah, AR_MCI_COMMAND2, regval);
  732. ar9003_mci_get_next_gpm_offset(ah, true, NULL);
  733. REG_WRITE(ah, AR_MCI_MSG_ATTRIBUTES_TABLE,
  734. (SM(0xe801, AR_MCI_MSG_ATTRIBUTES_TABLE_INVALID_HDR) |
  735. SM(0x0000, AR_MCI_MSG_ATTRIBUTES_TABLE_CHECKSUM)));
  736. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  737. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  738. ar9003_mci_observation_set_up(ah);
  739. mci->ready = true;
  740. ar9003_mci_prep_interface(ah);
  741. if (en_int)
  742. ar9003_mci_enable_interrupt(ah);
  743. return 0;
  744. }
  745. void ar9003_mci_stop_bt(struct ath_hw *ah, bool save_fullsleep)
  746. {
  747. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  748. ar9003_mci_disable_interrupt(ah);
  749. if (mci_hw->ready && !save_fullsleep) {
  750. ar9003_mci_mute_bt(ah);
  751. udelay(20);
  752. REG_WRITE(ah, AR_BTCOEX_CTRL, 0);
  753. }
  754. mci_hw->bt_state = MCI_BT_SLEEP;
  755. mci_hw->ready = false;
  756. }
  757. static void ar9003_mci_send_2g5g_status(struct ath_hw *ah, bool wait_done)
  758. {
  759. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  760. u32 new_flags, to_set, to_clear;
  761. if (!mci->update_2g5g || (mci->bt_state == MCI_BT_SLEEP))
  762. return;
  763. if (mci->is_2g) {
  764. new_flags = MCI_2G_FLAGS;
  765. to_clear = MCI_2G_FLAGS_CLEAR_MASK;
  766. to_set = MCI_2G_FLAGS_SET_MASK;
  767. } else {
  768. new_flags = MCI_5G_FLAGS;
  769. to_clear = MCI_5G_FLAGS_CLEAR_MASK;
  770. to_set = MCI_5G_FLAGS_SET_MASK;
  771. }
  772. if (to_clear)
  773. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  774. MCI_GPM_COEX_BT_FLAGS_CLEAR,
  775. to_clear);
  776. if (to_set)
  777. ar9003_mci_send_coex_bt_flags(ah, wait_done,
  778. MCI_GPM_COEX_BT_FLAGS_SET,
  779. to_set);
  780. }
  781. static void ar9003_mci_queue_unsent_gpm(struct ath_hw *ah, u8 header,
  782. u32 *payload, bool queue)
  783. {
  784. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  785. u8 type, opcode;
  786. /* check if the message is to be queued */
  787. if (header != MCI_GPM)
  788. return;
  789. type = MCI_GPM_TYPE(payload);
  790. opcode = MCI_GPM_OPCODE(payload);
  791. if (type != MCI_GPM_COEX_AGENT)
  792. return;
  793. switch (opcode) {
  794. case MCI_GPM_COEX_BT_UPDATE_FLAGS:
  795. if (*(((u8 *)payload) + MCI_GPM_COEX_B_BT_FLAGS_OP) ==
  796. MCI_GPM_COEX_BT_FLAGS_READ)
  797. break;
  798. mci->update_2g5g = queue;
  799. break;
  800. case MCI_GPM_COEX_WLAN_CHANNELS:
  801. mci->wlan_channels_update = queue;
  802. break;
  803. case MCI_GPM_COEX_HALT_BT_GPM:
  804. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  805. MCI_GPM_COEX_BT_GPM_UNHALT) {
  806. mci->unhalt_bt_gpm = queue;
  807. if (!queue)
  808. mci->halted_bt_gpm = false;
  809. }
  810. if (*(((u8 *)payload) + MCI_GPM_COEX_B_HALT_STATE) ==
  811. MCI_GPM_COEX_BT_GPM_HALT) {
  812. mci->halted_bt_gpm = !queue;
  813. }
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. void ar9003_mci_2g5g_switch(struct ath_hw *ah, bool force)
  820. {
  821. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  822. if (!mci->update_2g5g && !force)
  823. return;
  824. if (mci->is_2g) {
  825. ar9003_mci_send_2g5g_status(ah, true);
  826. ar9003_mci_send_lna_transfer(ah, true);
  827. udelay(5);
  828. REG_CLR_BIT(ah, AR_MCI_TX_CTRL,
  829. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  830. REG_CLR_BIT(ah, AR_PHY_GLB_CONTROL,
  831. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  832. if (!(mci->config & ATH_MCI_CONFIG_DISABLE_OSLA))
  833. ar9003_mci_osla_setup(ah, true);
  834. REG_WRITE(ah, AR_SELFGEN_MASK, 0x02);
  835. } else {
  836. ar9003_mci_send_lna_take(ah, true);
  837. udelay(5);
  838. REG_SET_BIT(ah, AR_MCI_TX_CTRL,
  839. AR_MCI_TX_CTRL_DISABLE_LNA_UPDATE);
  840. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL,
  841. AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  842. ar9003_mci_osla_setup(ah, false);
  843. ar9003_mci_send_2g5g_status(ah, true);
  844. }
  845. }
  846. bool ar9003_mci_send_message(struct ath_hw *ah, u8 header, u32 flag,
  847. u32 *payload, u8 len, bool wait_done,
  848. bool check_bt)
  849. {
  850. struct ath_common *common = ath9k_hw_common(ah);
  851. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  852. bool msg_sent = false;
  853. u32 regval;
  854. u32 saved_mci_int_en;
  855. int i;
  856. saved_mci_int_en = REG_READ(ah, AR_MCI_INTERRUPT_EN);
  857. regval = REG_READ(ah, AR_BTCOEX_CTRL);
  858. if ((regval == 0xdeadbeef) || !(regval & AR_BTCOEX_CTRL_MCI_MODE_EN)) {
  859. ath_dbg(common, MCI,
  860. "MCI Not sending 0x%x. MCI is not enabled. full_sleep = %d\n",
  861. header, (ah->power_mode == ATH9K_PM_FULL_SLEEP) ? 1 : 0);
  862. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  863. return false;
  864. } else if (check_bt && (mci->bt_state == MCI_BT_SLEEP)) {
  865. ath_dbg(common, MCI,
  866. "MCI Don't send message 0x%x. BT is in sleep state\n",
  867. header);
  868. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  869. return false;
  870. }
  871. if (wait_done)
  872. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, 0);
  873. /* Need to clear SW_MSG_DONE raw bit before wait */
  874. REG_WRITE(ah, AR_MCI_INTERRUPT_RAW,
  875. (AR_MCI_INTERRUPT_SW_MSG_DONE |
  876. AR_MCI_INTERRUPT_MSG_FAIL_MASK));
  877. if (payload) {
  878. for (i = 0; (i * 4) < len; i++)
  879. REG_WRITE(ah, (AR_MCI_TX_PAYLOAD0 + i * 4),
  880. *(payload + i));
  881. }
  882. REG_WRITE(ah, AR_MCI_COMMAND0,
  883. (SM((flag & MCI_FLAG_DISABLE_TIMESTAMP),
  884. AR_MCI_COMMAND0_DISABLE_TIMESTAMP) |
  885. SM(len, AR_MCI_COMMAND0_LEN) |
  886. SM(header, AR_MCI_COMMAND0_HEADER)));
  887. if (wait_done &&
  888. !(ar9003_mci_wait_for_interrupt(ah, AR_MCI_INTERRUPT_RAW,
  889. AR_MCI_INTERRUPT_SW_MSG_DONE, 500)))
  890. ar9003_mci_queue_unsent_gpm(ah, header, payload, true);
  891. else {
  892. ar9003_mci_queue_unsent_gpm(ah, header, payload, false);
  893. msg_sent = true;
  894. }
  895. if (wait_done)
  896. REG_WRITE(ah, AR_MCI_INTERRUPT_EN, saved_mci_int_en);
  897. return msg_sent;
  898. }
  899. EXPORT_SYMBOL(ar9003_mci_send_message);
  900. void ar9003_mci_init_cal_req(struct ath_hw *ah, bool *is_reusable)
  901. {
  902. struct ath_common *common = ath9k_hw_common(ah);
  903. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  904. u32 pld[4] = {0, 0, 0, 0};
  905. if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
  906. (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
  907. return;
  908. MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_REQ);
  909. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_seq++;
  910. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  911. if (ar9003_mci_wait_for_gpm(ah, MCI_GPM_BT_CAL_GRANT, 0, 50000)) {
  912. ath_dbg(common, MCI, "MCI BT_CAL_GRANT received\n");
  913. } else {
  914. *is_reusable = false;
  915. ath_dbg(common, MCI, "MCI BT_CAL_GRANT not received\n");
  916. }
  917. }
  918. void ar9003_mci_init_cal_done(struct ath_hw *ah)
  919. {
  920. struct ath9k_hw_mci *mci_hw = &ah->btcoex_hw.mci;
  921. u32 pld[4] = {0, 0, 0, 0};
  922. if ((mci_hw->bt_state != MCI_BT_AWAKE) ||
  923. (mci_hw->config & ATH_MCI_CONFIG_DISABLE_MCI_CAL))
  924. return;
  925. MCI_GPM_SET_CAL_TYPE(pld, MCI_GPM_WLAN_CAL_DONE);
  926. pld[MCI_GPM_WLAN_CAL_W_SEQUENCE] = mci_hw->wlan_cal_done++;
  927. ar9003_mci_send_message(ah, MCI_GPM, 0, pld, 16, true, false);
  928. }
  929. int ar9003_mci_setup(struct ath_hw *ah, u32 gpm_addr, void *gpm_buf,
  930. u16 len, u32 sched_addr)
  931. {
  932. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  933. mci->gpm_addr = gpm_addr;
  934. mci->gpm_buf = gpm_buf;
  935. mci->gpm_len = len;
  936. mci->sched_addr = sched_addr;
  937. return ar9003_mci_reset(ah, true, true, true);
  938. }
  939. EXPORT_SYMBOL(ar9003_mci_setup);
  940. void ar9003_mci_cleanup(struct ath_hw *ah)
  941. {
  942. /* Turn off MCI and Jupiter mode. */
  943. REG_WRITE(ah, AR_BTCOEX_CTRL, 0x00);
  944. ar9003_mci_disable_interrupt(ah);
  945. }
  946. EXPORT_SYMBOL(ar9003_mci_cleanup);
  947. u32 ar9003_mci_state(struct ath_hw *ah, u32 state_type)
  948. {
  949. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  950. u32 value = 0;
  951. u8 query_type;
  952. switch (state_type) {
  953. case MCI_STATE_ENABLE:
  954. if (mci->ready) {
  955. value = REG_READ(ah, AR_BTCOEX_CTRL);
  956. if ((value == 0xdeadbeef) || (value == 0xffffffff))
  957. value = 0;
  958. }
  959. value &= AR_BTCOEX_CTRL_MCI_MODE_EN;
  960. break;
  961. case MCI_STATE_LAST_SCHD_MSG_OFFSET:
  962. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  963. AR_MCI_RX_LAST_SCHD_MSG_INDEX);
  964. /* Make it in bytes */
  965. value <<= 4;
  966. break;
  967. case MCI_STATE_REMOTE_SLEEP:
  968. value = MS(REG_READ(ah, AR_MCI_RX_STATUS),
  969. AR_MCI_RX_REMOTE_SLEEP) ?
  970. MCI_BT_SLEEP : MCI_BT_AWAKE;
  971. break;
  972. case MCI_STATE_SET_BT_AWAKE:
  973. mci->bt_state = MCI_BT_AWAKE;
  974. ar9003_mci_send_coex_version_query(ah, true);
  975. ar9003_mci_send_coex_wlan_channels(ah, true);
  976. if (mci->unhalt_bt_gpm)
  977. ar9003_mci_send_coex_halt_bt_gpm(ah, false, true);
  978. ar9003_mci_2g5g_switch(ah, false);
  979. break;
  980. case MCI_STATE_RESET_REQ_WAKE:
  981. ar9003_mci_reset_req_wakeup(ah);
  982. mci->update_2g5g = true;
  983. if (mci->config & ATH_MCI_CONFIG_MCI_OBS_MASK) {
  984. /* Check if we still have control of the GPIOs */
  985. if ((REG_READ(ah, AR_GLB_GPIO_CONTROL) &
  986. ATH_MCI_CONFIG_MCI_OBS_GPIO) !=
  987. ATH_MCI_CONFIG_MCI_OBS_GPIO) {
  988. ar9003_mci_observation_set_up(ah);
  989. }
  990. }
  991. break;
  992. case MCI_STATE_SEND_WLAN_COEX_VERSION:
  993. ar9003_mci_send_coex_version_response(ah, true);
  994. break;
  995. case MCI_STATE_SEND_VERSION_QUERY:
  996. ar9003_mci_send_coex_version_query(ah, true);
  997. break;
  998. case MCI_STATE_SEND_STATUS_QUERY:
  999. query_type = MCI_GPM_COEX_QUERY_BT_TOPOLOGY;
  1000. ar9003_mci_send_coex_bt_status_query(ah, true, query_type);
  1001. break;
  1002. case MCI_STATE_RECOVER_RX:
  1003. ar9003_mci_prep_interface(ah);
  1004. mci->query_bt = true;
  1005. mci->need_flush_btinfo = true;
  1006. ar9003_mci_send_coex_wlan_channels(ah, true);
  1007. ar9003_mci_2g5g_switch(ah, false);
  1008. break;
  1009. case MCI_STATE_NEED_FTP_STOMP:
  1010. value = !(mci->config & ATH_MCI_CONFIG_DISABLE_FTP_STOMP);
  1011. break;
  1012. case MCI_STATE_NEED_FLUSH_BT_INFO:
  1013. value = (!mci->unhalt_bt_gpm && mci->need_flush_btinfo) ? 1 : 0;
  1014. mci->need_flush_btinfo = false;
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. return value;
  1020. }
  1021. EXPORT_SYMBOL(ar9003_mci_state);
  1022. void ar9003_mci_bt_gain_ctrl(struct ath_hw *ah)
  1023. {
  1024. struct ath_common *common = ath9k_hw_common(ah);
  1025. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1026. ath_dbg(common, MCI, "Give LNA and SPDT control to BT\n");
  1027. ar9003_mci_send_lna_take(ah, true);
  1028. udelay(50);
  1029. REG_SET_BIT(ah, AR_PHY_GLB_CONTROL, AR_BTCOEX_CTRL_BT_OWN_SPDT_CTRL);
  1030. mci->is_2g = false;
  1031. mci->update_2g5g = true;
  1032. ar9003_mci_send_2g5g_status(ah, true);
  1033. /* Force another 2g5g update at next scanning */
  1034. mci->update_2g5g = true;
  1035. }
  1036. void ar9003_mci_set_power_awake(struct ath_hw *ah)
  1037. {
  1038. u32 btcoex_ctrl2, diag_sw;
  1039. int i;
  1040. u8 lna_ctrl, bt_sleep;
  1041. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1042. btcoex_ctrl2 = REG_READ(ah, AR_BTCOEX_CTRL2);
  1043. if (btcoex_ctrl2 != 0xdeadbeef)
  1044. break;
  1045. udelay(AH_TIME_QUANTUM);
  1046. }
  1047. REG_WRITE(ah, AR_BTCOEX_CTRL2, (btcoex_ctrl2 | BIT(23)));
  1048. for (i = 0; i < AH_WAIT_TIMEOUT; i++) {
  1049. diag_sw = REG_READ(ah, AR_DIAG_SW);
  1050. if (diag_sw != 0xdeadbeef)
  1051. break;
  1052. udelay(AH_TIME_QUANTUM);
  1053. }
  1054. REG_WRITE(ah, AR_DIAG_SW, (diag_sw | BIT(27) | BIT(19) | BIT(18)));
  1055. lna_ctrl = REG_READ(ah, AR_OBS_BUS_CTRL) & 0x3;
  1056. bt_sleep = MS(REG_READ(ah, AR_MCI_RX_STATUS), AR_MCI_RX_REMOTE_SLEEP);
  1057. REG_WRITE(ah, AR_BTCOEX_CTRL2, btcoex_ctrl2);
  1058. REG_WRITE(ah, AR_DIAG_SW, diag_sw);
  1059. if (bt_sleep && (lna_ctrl == 2)) {
  1060. REG_SET_BIT(ah, AR_BTCOEX_RC, 0x1);
  1061. REG_CLR_BIT(ah, AR_BTCOEX_RC, 0x1);
  1062. udelay(50);
  1063. }
  1064. }
  1065. void ar9003_mci_check_gpm_offset(struct ath_hw *ah)
  1066. {
  1067. struct ath_common *common = ath9k_hw_common(ah);
  1068. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1069. u32 offset;
  1070. /*
  1071. * This should only be called before "MAC Warm Reset" or "MCI Reset Rx".
  1072. */
  1073. offset = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1074. if (mci->gpm_idx == offset)
  1075. return;
  1076. ath_dbg(common, MCI, "GPM cached write pointer mismatch %d %d\n",
  1077. mci->gpm_idx, offset);
  1078. mci->query_bt = true;
  1079. mci->need_flush_btinfo = true;
  1080. mci->gpm_idx = 0;
  1081. }
  1082. u32 ar9003_mci_get_next_gpm_offset(struct ath_hw *ah, bool first, u32 *more)
  1083. {
  1084. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1085. u32 offset, more_gpm = 0, gpm_ptr;
  1086. if (first) {
  1087. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1088. if (gpm_ptr >= mci->gpm_len)
  1089. gpm_ptr = 0;
  1090. mci->gpm_idx = gpm_ptr;
  1091. return gpm_ptr;
  1092. }
  1093. /*
  1094. * This could be useful to avoid new GPM message interrupt which
  1095. * may lead to spurious interrupt after power sleep, or multiple
  1096. * entry of ath_mci_intr().
  1097. * Adding empty GPM check by returning HAL_MCI_GPM_INVALID can
  1098. * alleviate this effect, but clearing GPM RX interrupt bit is
  1099. * safe, because whether this is called from hw or driver code
  1100. * there must be an interrupt bit set/triggered initially
  1101. */
  1102. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_RAW,
  1103. AR_MCI_INTERRUPT_RX_MSG_GPM);
  1104. gpm_ptr = MS(REG_READ(ah, AR_MCI_GPM_1), AR_MCI_GPM_WRITE_PTR);
  1105. offset = gpm_ptr;
  1106. if (!offset)
  1107. offset = mci->gpm_len - 1;
  1108. else if (offset >= mci->gpm_len) {
  1109. if (offset != 0xFFFF)
  1110. offset = 0;
  1111. } else {
  1112. offset--;
  1113. }
  1114. if ((offset == 0xFFFF) || (gpm_ptr == mci->gpm_idx)) {
  1115. offset = MCI_GPM_INVALID;
  1116. more_gpm = MCI_GPM_NOMORE;
  1117. goto out;
  1118. }
  1119. for (;;) {
  1120. u32 temp_index;
  1121. /* skip reserved GPM if any */
  1122. if (offset != mci->gpm_idx)
  1123. more_gpm = MCI_GPM_MORE;
  1124. else
  1125. more_gpm = MCI_GPM_NOMORE;
  1126. temp_index = mci->gpm_idx;
  1127. if (temp_index >= mci->gpm_len)
  1128. temp_index = 0;
  1129. mci->gpm_idx++;
  1130. if (mci->gpm_idx >= mci->gpm_len)
  1131. mci->gpm_idx = 0;
  1132. if (ar9003_mci_is_gpm_valid(ah, temp_index)) {
  1133. offset = temp_index;
  1134. break;
  1135. }
  1136. if (more_gpm == MCI_GPM_NOMORE) {
  1137. offset = MCI_GPM_INVALID;
  1138. break;
  1139. }
  1140. }
  1141. if (offset != MCI_GPM_INVALID)
  1142. offset <<= 4;
  1143. out:
  1144. if (more)
  1145. *more = more_gpm;
  1146. return offset;
  1147. }
  1148. EXPORT_SYMBOL(ar9003_mci_get_next_gpm_offset);
  1149. void ar9003_mci_set_bt_version(struct ath_hw *ah, u8 major, u8 minor)
  1150. {
  1151. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1152. mci->bt_ver_major = major;
  1153. mci->bt_ver_minor = minor;
  1154. mci->bt_version_known = true;
  1155. ath_dbg(ath9k_hw_common(ah), MCI, "MCI BT version set: %d.%d\n",
  1156. mci->bt_ver_major, mci->bt_ver_minor);
  1157. }
  1158. EXPORT_SYMBOL(ar9003_mci_set_bt_version);
  1159. void ar9003_mci_send_wlan_channels(struct ath_hw *ah)
  1160. {
  1161. struct ath9k_hw_mci *mci = &ah->btcoex_hw.mci;
  1162. mci->wlan_channels_update = true;
  1163. ar9003_mci_send_coex_wlan_channels(ah, true);
  1164. }
  1165. EXPORT_SYMBOL(ar9003_mci_send_wlan_channels);