phy.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  1. /*
  2. Broadcom B43legacy wireless driver
  3. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  4. Stefano Brivio <stefano.brivio@polimi.it>
  5. Michael Buesch <mbuesch@freenet.de>
  6. Danny van Dyk <kugelfang@gentoo.org>
  7. Andreas Jaggi <andreas.jaggi@waterwave.ch>
  8. Copyright (c) 2007 Larry Finger <Larry.Finger@lwfinger.net>
  9. Some parts of the code in this file are derived from the ipw2200
  10. driver Copyright(c) 2003 - 2004 Intel Corporation.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; see the file COPYING. If not, write to
  21. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  22. Boston, MA 02110-1301, USA.
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/pci.h>
  26. #include <linux/types.h>
  27. #include "b43legacy.h"
  28. #include "phy.h"
  29. #include "main.h"
  30. #include "radio.h"
  31. #include "ilt.h"
  32. static const s8 b43legacy_tssi2dbm_b_table[] = {
  33. 0x4D, 0x4C, 0x4B, 0x4A,
  34. 0x4A, 0x49, 0x48, 0x47,
  35. 0x47, 0x46, 0x45, 0x45,
  36. 0x44, 0x43, 0x42, 0x42,
  37. 0x41, 0x40, 0x3F, 0x3E,
  38. 0x3D, 0x3C, 0x3B, 0x3A,
  39. 0x39, 0x38, 0x37, 0x36,
  40. 0x35, 0x34, 0x32, 0x31,
  41. 0x30, 0x2F, 0x2D, 0x2C,
  42. 0x2B, 0x29, 0x28, 0x26,
  43. 0x25, 0x23, 0x21, 0x1F,
  44. 0x1D, 0x1A, 0x17, 0x14,
  45. 0x10, 0x0C, 0x06, 0x00,
  46. -7, -7, -7, -7,
  47. -7, -7, -7, -7,
  48. -7, -7, -7, -7,
  49. };
  50. static const s8 b43legacy_tssi2dbm_g_table[] = {
  51. 77, 77, 77, 76,
  52. 76, 76, 75, 75,
  53. 74, 74, 73, 73,
  54. 73, 72, 72, 71,
  55. 71, 70, 70, 69,
  56. 68, 68, 67, 67,
  57. 66, 65, 65, 64,
  58. 63, 63, 62, 61,
  59. 60, 59, 58, 57,
  60. 56, 55, 54, 53,
  61. 52, 50, 49, 47,
  62. 45, 43, 40, 37,
  63. 33, 28, 22, 14,
  64. 5, -7, -20, -20,
  65. -20, -20, -20, -20,
  66. -20, -20, -20, -20,
  67. };
  68. static void b43legacy_phy_initg(struct b43legacy_wldev *dev);
  69. static inline
  70. void b43legacy_voluntary_preempt(void)
  71. {
  72. B43legacy_BUG_ON(!(!in_atomic() && !in_irq() &&
  73. !in_interrupt() && !irqs_disabled()));
  74. #ifndef CONFIG_PREEMPT
  75. cond_resched();
  76. #endif /* CONFIG_PREEMPT */
  77. }
  78. /* Lock the PHY registers against concurrent access from the microcode.
  79. * This lock is nonrecursive. */
  80. void b43legacy_phy_lock(struct b43legacy_wldev *dev)
  81. {
  82. #if B43legacy_DEBUG
  83. B43legacy_WARN_ON(dev->phy.phy_locked);
  84. dev->phy.phy_locked = 1;
  85. #endif
  86. if (dev->dev->id.revision < 3) {
  87. b43legacy_mac_suspend(dev);
  88. } else {
  89. if (!b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP))
  90. b43legacy_power_saving_ctl_bits(dev, -1, 1);
  91. }
  92. }
  93. void b43legacy_phy_unlock(struct b43legacy_wldev *dev)
  94. {
  95. #if B43legacy_DEBUG
  96. B43legacy_WARN_ON(!dev->phy.phy_locked);
  97. dev->phy.phy_locked = 0;
  98. #endif
  99. if (dev->dev->id.revision < 3) {
  100. b43legacy_mac_enable(dev);
  101. } else {
  102. if (!b43legacy_is_mode(dev->wl, NL80211_IFTYPE_AP))
  103. b43legacy_power_saving_ctl_bits(dev, -1, -1);
  104. }
  105. }
  106. u16 b43legacy_phy_read(struct b43legacy_wldev *dev, u16 offset)
  107. {
  108. b43legacy_write16(dev, B43legacy_MMIO_PHY_CONTROL, offset);
  109. return b43legacy_read16(dev, B43legacy_MMIO_PHY_DATA);
  110. }
  111. void b43legacy_phy_write(struct b43legacy_wldev *dev, u16 offset, u16 val)
  112. {
  113. b43legacy_write16(dev, B43legacy_MMIO_PHY_CONTROL, offset);
  114. mmiowb();
  115. b43legacy_write16(dev, B43legacy_MMIO_PHY_DATA, val);
  116. }
  117. void b43legacy_phy_calibrate(struct b43legacy_wldev *dev)
  118. {
  119. struct b43legacy_phy *phy = &dev->phy;
  120. b43legacy_read32(dev, B43legacy_MMIO_MACCTL); /* Dummy read. */
  121. if (phy->calibrated)
  122. return;
  123. if (phy->type == B43legacy_PHYTYPE_G && phy->rev == 1) {
  124. b43legacy_wireless_core_reset(dev, 0);
  125. b43legacy_phy_initg(dev);
  126. b43legacy_wireless_core_reset(dev, B43legacy_TMSLOW_GMODE);
  127. }
  128. phy->calibrated = 1;
  129. }
  130. /* intialize B PHY power control
  131. * as described in http://bcm-specs.sipsolutions.net/InitPowerControl
  132. */
  133. static void b43legacy_phy_init_pctl(struct b43legacy_wldev *dev)
  134. {
  135. struct b43legacy_phy *phy = &dev->phy;
  136. u16 saved_batt = 0;
  137. u16 saved_ratt = 0;
  138. u16 saved_txctl1 = 0;
  139. int must_reset_txpower = 0;
  140. B43legacy_BUG_ON(!(phy->type == B43legacy_PHYTYPE_B ||
  141. phy->type == B43legacy_PHYTYPE_G));
  142. if (is_bcm_board_vendor(dev) &&
  143. (dev->dev->bus->boardinfo.type == 0x0416))
  144. return;
  145. b43legacy_phy_write(dev, 0x0028, 0x8018);
  146. b43legacy_write16(dev, 0x03E6, b43legacy_read16(dev, 0x03E6) & 0xFFDF);
  147. if (phy->type == B43legacy_PHYTYPE_G) {
  148. if (!phy->gmode)
  149. return;
  150. b43legacy_phy_write(dev, 0x047A, 0xC111);
  151. }
  152. if (phy->savedpctlreg != 0xFFFF)
  153. return;
  154. #ifdef CONFIG_B43LEGACY_DEBUG
  155. if (phy->manual_txpower_control)
  156. return;
  157. #endif
  158. if (phy->type == B43legacy_PHYTYPE_B &&
  159. phy->rev >= 2 &&
  160. phy->radio_ver == 0x2050)
  161. b43legacy_radio_write16(dev, 0x0076,
  162. b43legacy_radio_read16(dev, 0x0076)
  163. | 0x0084);
  164. else {
  165. saved_batt = phy->bbatt;
  166. saved_ratt = phy->rfatt;
  167. saved_txctl1 = phy->txctl1;
  168. if ((phy->radio_rev >= 6) && (phy->radio_rev <= 8)
  169. && /*FIXME: incomplete specs for 5 < revision < 9 */ 0)
  170. b43legacy_radio_set_txpower_bg(dev, 0xB, 0x1F, 0);
  171. else
  172. b43legacy_radio_set_txpower_bg(dev, 0xB, 9, 0);
  173. must_reset_txpower = 1;
  174. }
  175. b43legacy_dummy_transmission(dev);
  176. phy->savedpctlreg = b43legacy_phy_read(dev, B43legacy_PHY_G_PCTL);
  177. if (must_reset_txpower)
  178. b43legacy_radio_set_txpower_bg(dev, saved_batt, saved_ratt,
  179. saved_txctl1);
  180. else
  181. b43legacy_radio_write16(dev, 0x0076, b43legacy_radio_read16(dev,
  182. 0x0076) & 0xFF7B);
  183. b43legacy_radio_clear_tssi(dev);
  184. }
  185. static void b43legacy_phy_agcsetup(struct b43legacy_wldev *dev)
  186. {
  187. struct b43legacy_phy *phy = &dev->phy;
  188. u16 offset = 0x0000;
  189. if (phy->rev == 1)
  190. offset = 0x4C00;
  191. b43legacy_ilt_write(dev, offset, 0x00FE);
  192. b43legacy_ilt_write(dev, offset + 1, 0x000D);
  193. b43legacy_ilt_write(dev, offset + 2, 0x0013);
  194. b43legacy_ilt_write(dev, offset + 3, 0x0019);
  195. if (phy->rev == 1) {
  196. b43legacy_ilt_write(dev, 0x1800, 0x2710);
  197. b43legacy_ilt_write(dev, 0x1801, 0x9B83);
  198. b43legacy_ilt_write(dev, 0x1802, 0x9B83);
  199. b43legacy_ilt_write(dev, 0x1803, 0x0F8D);
  200. b43legacy_phy_write(dev, 0x0455, 0x0004);
  201. }
  202. b43legacy_phy_write(dev, 0x04A5, (b43legacy_phy_read(dev, 0x04A5)
  203. & 0x00FF) | 0x5700);
  204. b43legacy_phy_write(dev, 0x041A, (b43legacy_phy_read(dev, 0x041A)
  205. & 0xFF80) | 0x000F);
  206. b43legacy_phy_write(dev, 0x041A, (b43legacy_phy_read(dev, 0x041A)
  207. & 0xC07F) | 0x2B80);
  208. b43legacy_phy_write(dev, 0x048C, (b43legacy_phy_read(dev, 0x048C)
  209. & 0xF0FF) | 0x0300);
  210. b43legacy_radio_write16(dev, 0x007A,
  211. b43legacy_radio_read16(dev, 0x007A)
  212. | 0x0008);
  213. b43legacy_phy_write(dev, 0x04A0, (b43legacy_phy_read(dev, 0x04A0)
  214. & 0xFFF0) | 0x0008);
  215. b43legacy_phy_write(dev, 0x04A1, (b43legacy_phy_read(dev, 0x04A1)
  216. & 0xF0FF) | 0x0600);
  217. b43legacy_phy_write(dev, 0x04A2, (b43legacy_phy_read(dev, 0x04A2)
  218. & 0xF0FF) | 0x0700);
  219. b43legacy_phy_write(dev, 0x04A0, (b43legacy_phy_read(dev, 0x04A0)
  220. & 0xF0FF) | 0x0100);
  221. if (phy->rev == 1)
  222. b43legacy_phy_write(dev, 0x04A2,
  223. (b43legacy_phy_read(dev, 0x04A2)
  224. & 0xFFF0) | 0x0007);
  225. b43legacy_phy_write(dev, 0x0488, (b43legacy_phy_read(dev, 0x0488)
  226. & 0xFF00) | 0x001C);
  227. b43legacy_phy_write(dev, 0x0488, (b43legacy_phy_read(dev, 0x0488)
  228. & 0xC0FF) | 0x0200);
  229. b43legacy_phy_write(dev, 0x0496, (b43legacy_phy_read(dev, 0x0496)
  230. & 0xFF00) | 0x001C);
  231. b43legacy_phy_write(dev, 0x0489, (b43legacy_phy_read(dev, 0x0489)
  232. & 0xFF00) | 0x0020);
  233. b43legacy_phy_write(dev, 0x0489, (b43legacy_phy_read(dev, 0x0489)
  234. & 0xC0FF) | 0x0200);
  235. b43legacy_phy_write(dev, 0x0482, (b43legacy_phy_read(dev, 0x0482)
  236. & 0xFF00) | 0x002E);
  237. b43legacy_phy_write(dev, 0x0496, (b43legacy_phy_read(dev, 0x0496)
  238. & 0x00FF) | 0x1A00);
  239. b43legacy_phy_write(dev, 0x0481, (b43legacy_phy_read(dev, 0x0481)
  240. & 0xFF00) | 0x0028);
  241. b43legacy_phy_write(dev, 0x0481, (b43legacy_phy_read(dev, 0x0481)
  242. & 0x00FF) | 0x2C00);
  243. if (phy->rev == 1) {
  244. b43legacy_phy_write(dev, 0x0430, 0x092B);
  245. b43legacy_phy_write(dev, 0x041B,
  246. (b43legacy_phy_read(dev, 0x041B)
  247. & 0xFFE1) | 0x0002);
  248. } else {
  249. b43legacy_phy_write(dev, 0x041B,
  250. b43legacy_phy_read(dev, 0x041B) & 0xFFE1);
  251. b43legacy_phy_write(dev, 0x041F, 0x287A);
  252. b43legacy_phy_write(dev, 0x0420,
  253. (b43legacy_phy_read(dev, 0x0420)
  254. & 0xFFF0) | 0x0004);
  255. }
  256. if (phy->rev > 2) {
  257. b43legacy_phy_write(dev, 0x0422, 0x287A);
  258. b43legacy_phy_write(dev, 0x0420,
  259. (b43legacy_phy_read(dev, 0x0420)
  260. & 0x0FFF) | 0x3000);
  261. }
  262. b43legacy_phy_write(dev, 0x04A8, (b43legacy_phy_read(dev, 0x04A8)
  263. & 0x8080) | 0x7874);
  264. b43legacy_phy_write(dev, 0x048E, 0x1C00);
  265. if (phy->rev == 1) {
  266. b43legacy_phy_write(dev, 0x04AB,
  267. (b43legacy_phy_read(dev, 0x04AB)
  268. & 0xF0FF) | 0x0600);
  269. b43legacy_phy_write(dev, 0x048B, 0x005E);
  270. b43legacy_phy_write(dev, 0x048C,
  271. (b43legacy_phy_read(dev, 0x048C) & 0xFF00)
  272. | 0x001E);
  273. b43legacy_phy_write(dev, 0x048D, 0x0002);
  274. }
  275. b43legacy_ilt_write(dev, offset + 0x0800, 0);
  276. b43legacy_ilt_write(dev, offset + 0x0801, 7);
  277. b43legacy_ilt_write(dev, offset + 0x0802, 16);
  278. b43legacy_ilt_write(dev, offset + 0x0803, 28);
  279. if (phy->rev >= 6) {
  280. b43legacy_phy_write(dev, 0x0426,
  281. (b43legacy_phy_read(dev, 0x0426) & 0xFFFC));
  282. b43legacy_phy_write(dev, 0x0426,
  283. (b43legacy_phy_read(dev, 0x0426) & 0xEFFF));
  284. }
  285. }
  286. static void b43legacy_phy_setupg(struct b43legacy_wldev *dev)
  287. {
  288. struct b43legacy_phy *phy = &dev->phy;
  289. u16 i;
  290. B43legacy_BUG_ON(phy->type != B43legacy_PHYTYPE_G);
  291. if (phy->rev == 1) {
  292. b43legacy_phy_write(dev, 0x0406, 0x4F19);
  293. b43legacy_phy_write(dev, B43legacy_PHY_G_CRS,
  294. (b43legacy_phy_read(dev,
  295. B43legacy_PHY_G_CRS) & 0xFC3F) | 0x0340);
  296. b43legacy_phy_write(dev, 0x042C, 0x005A);
  297. b43legacy_phy_write(dev, 0x0427, 0x001A);
  298. for (i = 0; i < B43legacy_ILT_FINEFREQG_SIZE; i++)
  299. b43legacy_ilt_write(dev, 0x5800 + i,
  300. b43legacy_ilt_finefreqg[i]);
  301. for (i = 0; i < B43legacy_ILT_NOISEG1_SIZE; i++)
  302. b43legacy_ilt_write(dev, 0x1800 + i,
  303. b43legacy_ilt_noiseg1[i]);
  304. for (i = 0; i < B43legacy_ILT_ROTOR_SIZE; i++)
  305. b43legacy_ilt_write32(dev, 0x2000 + i,
  306. b43legacy_ilt_rotor[i]);
  307. } else {
  308. /* nrssi values are signed 6-bit values. Why 0x7654 here? */
  309. b43legacy_nrssi_hw_write(dev, 0xBA98, (s16)0x7654);
  310. if (phy->rev == 2) {
  311. b43legacy_phy_write(dev, 0x04C0, 0x1861);
  312. b43legacy_phy_write(dev, 0x04C1, 0x0271);
  313. } else if (phy->rev > 2) {
  314. b43legacy_phy_write(dev, 0x04C0, 0x0098);
  315. b43legacy_phy_write(dev, 0x04C1, 0x0070);
  316. b43legacy_phy_write(dev, 0x04C9, 0x0080);
  317. }
  318. b43legacy_phy_write(dev, 0x042B, b43legacy_phy_read(dev,
  319. 0x042B) | 0x800);
  320. for (i = 0; i < 64; i++)
  321. b43legacy_ilt_write(dev, 0x4000 + i, i);
  322. for (i = 0; i < B43legacy_ILT_NOISEG2_SIZE; i++)
  323. b43legacy_ilt_write(dev, 0x1800 + i,
  324. b43legacy_ilt_noiseg2[i]);
  325. }
  326. if (phy->rev <= 2)
  327. for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
  328. b43legacy_ilt_write(dev, 0x1400 + i,
  329. b43legacy_ilt_noisescaleg1[i]);
  330. else if ((phy->rev >= 7) && (b43legacy_phy_read(dev, 0x0449) & 0x0200))
  331. for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
  332. b43legacy_ilt_write(dev, 0x1400 + i,
  333. b43legacy_ilt_noisescaleg3[i]);
  334. else
  335. for (i = 0; i < B43legacy_ILT_NOISESCALEG_SIZE; i++)
  336. b43legacy_ilt_write(dev, 0x1400 + i,
  337. b43legacy_ilt_noisescaleg2[i]);
  338. if (phy->rev == 2)
  339. for (i = 0; i < B43legacy_ILT_SIGMASQR_SIZE; i++)
  340. b43legacy_ilt_write(dev, 0x5000 + i,
  341. b43legacy_ilt_sigmasqr1[i]);
  342. else if ((phy->rev > 2) && (phy->rev <= 8))
  343. for (i = 0; i < B43legacy_ILT_SIGMASQR_SIZE; i++)
  344. b43legacy_ilt_write(dev, 0x5000 + i,
  345. b43legacy_ilt_sigmasqr2[i]);
  346. if (phy->rev == 1) {
  347. for (i = 0; i < B43legacy_ILT_RETARD_SIZE; i++)
  348. b43legacy_ilt_write32(dev, 0x2400 + i,
  349. b43legacy_ilt_retard[i]);
  350. for (i = 4; i < 20; i++)
  351. b43legacy_ilt_write(dev, 0x5400 + i, 0x0020);
  352. b43legacy_phy_agcsetup(dev);
  353. if (is_bcm_board_vendor(dev) &&
  354. (dev->dev->bus->boardinfo.type == 0x0416) &&
  355. (dev->dev->bus->boardinfo.rev == 0x0017))
  356. return;
  357. b43legacy_ilt_write(dev, 0x5001, 0x0002);
  358. b43legacy_ilt_write(dev, 0x5002, 0x0001);
  359. } else {
  360. for (i = 0; i <= 0x20; i++)
  361. b43legacy_ilt_write(dev, 0x1000 + i, 0x0820);
  362. b43legacy_phy_agcsetup(dev);
  363. b43legacy_phy_read(dev, 0x0400); /* dummy read */
  364. b43legacy_phy_write(dev, 0x0403, 0x1000);
  365. b43legacy_ilt_write(dev, 0x3C02, 0x000F);
  366. b43legacy_ilt_write(dev, 0x3C03, 0x0014);
  367. if (is_bcm_board_vendor(dev) &&
  368. (dev->dev->bus->boardinfo.type == 0x0416) &&
  369. (dev->dev->bus->boardinfo.rev == 0x0017))
  370. return;
  371. b43legacy_ilt_write(dev, 0x0401, 0x0002);
  372. b43legacy_ilt_write(dev, 0x0402, 0x0001);
  373. }
  374. }
  375. /* Initialize the APHY portion of a GPHY. */
  376. static void b43legacy_phy_inita(struct b43legacy_wldev *dev)
  377. {
  378. might_sleep();
  379. b43legacy_phy_setupg(dev);
  380. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_PACTRL)
  381. b43legacy_phy_write(dev, 0x046E, 0x03CF);
  382. }
  383. static void b43legacy_phy_initb2(struct b43legacy_wldev *dev)
  384. {
  385. struct b43legacy_phy *phy = &dev->phy;
  386. u16 offset;
  387. int val;
  388. b43legacy_write16(dev, 0x03EC, 0x3F22);
  389. b43legacy_phy_write(dev, 0x0020, 0x301C);
  390. b43legacy_phy_write(dev, 0x0026, 0x0000);
  391. b43legacy_phy_write(dev, 0x0030, 0x00C6);
  392. b43legacy_phy_write(dev, 0x0088, 0x3E00);
  393. val = 0x3C3D;
  394. for (offset = 0x0089; offset < 0x00A7; offset++) {
  395. b43legacy_phy_write(dev, offset, val);
  396. val -= 0x0202;
  397. }
  398. b43legacy_phy_write(dev, 0x03E4, 0x3000);
  399. b43legacy_radio_selectchannel(dev, phy->channel, 0);
  400. if (phy->radio_ver != 0x2050) {
  401. b43legacy_radio_write16(dev, 0x0075, 0x0080);
  402. b43legacy_radio_write16(dev, 0x0079, 0x0081);
  403. }
  404. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  405. b43legacy_radio_write16(dev, 0x0050, 0x0023);
  406. if (phy->radio_ver == 0x2050) {
  407. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  408. b43legacy_radio_write16(dev, 0x005A, 0x0070);
  409. b43legacy_radio_write16(dev, 0x005B, 0x007B);
  410. b43legacy_radio_write16(dev, 0x005C, 0x00B0);
  411. b43legacy_radio_write16(dev, 0x007A, 0x000F);
  412. b43legacy_phy_write(dev, 0x0038, 0x0677);
  413. b43legacy_radio_init2050(dev);
  414. }
  415. b43legacy_phy_write(dev, 0x0014, 0x0080);
  416. b43legacy_phy_write(dev, 0x0032, 0x00CA);
  417. b43legacy_phy_write(dev, 0x0032, 0x00CC);
  418. b43legacy_phy_write(dev, 0x0035, 0x07C2);
  419. b43legacy_phy_lo_b_measure(dev);
  420. b43legacy_phy_write(dev, 0x0026, 0xCC00);
  421. if (phy->radio_ver != 0x2050)
  422. b43legacy_phy_write(dev, 0x0026, 0xCE00);
  423. b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x1000);
  424. b43legacy_phy_write(dev, 0x002A, 0x88A3);
  425. if (phy->radio_ver != 0x2050)
  426. b43legacy_phy_write(dev, 0x002A, 0x88C2);
  427. b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
  428. b43legacy_phy_init_pctl(dev);
  429. }
  430. static void b43legacy_phy_initb4(struct b43legacy_wldev *dev)
  431. {
  432. struct b43legacy_phy *phy = &dev->phy;
  433. u16 offset;
  434. u16 val;
  435. b43legacy_write16(dev, 0x03EC, 0x3F22);
  436. b43legacy_phy_write(dev, 0x0020, 0x301C);
  437. b43legacy_phy_write(dev, 0x0026, 0x0000);
  438. b43legacy_phy_write(dev, 0x0030, 0x00C6);
  439. b43legacy_phy_write(dev, 0x0088, 0x3E00);
  440. val = 0x3C3D;
  441. for (offset = 0x0089; offset < 0x00A7; offset++) {
  442. b43legacy_phy_write(dev, offset, val);
  443. val -= 0x0202;
  444. }
  445. b43legacy_phy_write(dev, 0x03E4, 0x3000);
  446. b43legacy_radio_selectchannel(dev, phy->channel, 0);
  447. if (phy->radio_ver != 0x2050) {
  448. b43legacy_radio_write16(dev, 0x0075, 0x0080);
  449. b43legacy_radio_write16(dev, 0x0079, 0x0081);
  450. }
  451. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  452. b43legacy_radio_write16(dev, 0x0050, 0x0023);
  453. if (phy->radio_ver == 0x2050) {
  454. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  455. b43legacy_radio_write16(dev, 0x005A, 0x0070);
  456. b43legacy_radio_write16(dev, 0x005B, 0x007B);
  457. b43legacy_radio_write16(dev, 0x005C, 0x00B0);
  458. b43legacy_radio_write16(dev, 0x007A, 0x000F);
  459. b43legacy_phy_write(dev, 0x0038, 0x0677);
  460. b43legacy_radio_init2050(dev);
  461. }
  462. b43legacy_phy_write(dev, 0x0014, 0x0080);
  463. b43legacy_phy_write(dev, 0x0032, 0x00CA);
  464. if (phy->radio_ver == 0x2050)
  465. b43legacy_phy_write(dev, 0x0032, 0x00E0);
  466. b43legacy_phy_write(dev, 0x0035, 0x07C2);
  467. b43legacy_phy_lo_b_measure(dev);
  468. b43legacy_phy_write(dev, 0x0026, 0xCC00);
  469. if (phy->radio_ver == 0x2050)
  470. b43legacy_phy_write(dev, 0x0026, 0xCE00);
  471. b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x1100);
  472. b43legacy_phy_write(dev, 0x002A, 0x88A3);
  473. if (phy->radio_ver == 0x2050)
  474. b43legacy_phy_write(dev, 0x002A, 0x88C2);
  475. b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
  476. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI) {
  477. b43legacy_calc_nrssi_slope(dev);
  478. b43legacy_calc_nrssi_threshold(dev);
  479. }
  480. b43legacy_phy_init_pctl(dev);
  481. }
  482. static void b43legacy_phy_initb5(struct b43legacy_wldev *dev)
  483. {
  484. struct b43legacy_phy *phy = &dev->phy;
  485. u16 offset;
  486. u16 value;
  487. u8 old_channel;
  488. if (phy->analog == 1)
  489. b43legacy_radio_write16(dev, 0x007A,
  490. b43legacy_radio_read16(dev, 0x007A)
  491. | 0x0050);
  492. if (!is_bcm_board_vendor(dev) &&
  493. (dev->dev->bus->boardinfo.type != 0x0416)) {
  494. value = 0x2120;
  495. for (offset = 0x00A8 ; offset < 0x00C7; offset++) {
  496. b43legacy_phy_write(dev, offset, value);
  497. value += 0x0202;
  498. }
  499. }
  500. b43legacy_phy_write(dev, 0x0035,
  501. (b43legacy_phy_read(dev, 0x0035) & 0xF0FF)
  502. | 0x0700);
  503. if (phy->radio_ver == 0x2050)
  504. b43legacy_phy_write(dev, 0x0038, 0x0667);
  505. if (phy->gmode) {
  506. if (phy->radio_ver == 0x2050) {
  507. b43legacy_radio_write16(dev, 0x007A,
  508. b43legacy_radio_read16(dev, 0x007A)
  509. | 0x0020);
  510. b43legacy_radio_write16(dev, 0x0051,
  511. b43legacy_radio_read16(dev, 0x0051)
  512. | 0x0004);
  513. }
  514. b43legacy_write16(dev, B43legacy_MMIO_PHY_RADIO, 0x0000);
  515. b43legacy_phy_write(dev, 0x0802, b43legacy_phy_read(dev, 0x0802)
  516. | 0x0100);
  517. b43legacy_phy_write(dev, 0x042B, b43legacy_phy_read(dev, 0x042B)
  518. | 0x2000);
  519. b43legacy_phy_write(dev, 0x001C, 0x186A);
  520. b43legacy_phy_write(dev, 0x0013, (b43legacy_phy_read(dev,
  521. 0x0013) & 0x00FF) | 0x1900);
  522. b43legacy_phy_write(dev, 0x0035, (b43legacy_phy_read(dev,
  523. 0x0035) & 0xFFC0) | 0x0064);
  524. b43legacy_phy_write(dev, 0x005D, (b43legacy_phy_read(dev,
  525. 0x005D) & 0xFF80) | 0x000A);
  526. b43legacy_phy_write(dev, 0x5B, 0x0000);
  527. b43legacy_phy_write(dev, 0x5C, 0x0000);
  528. }
  529. if (dev->bad_frames_preempt)
  530. b43legacy_phy_write(dev, B43legacy_PHY_RADIO_BITFIELD,
  531. b43legacy_phy_read(dev,
  532. B43legacy_PHY_RADIO_BITFIELD) | (1 << 12));
  533. if (phy->analog == 1) {
  534. b43legacy_phy_write(dev, 0x0026, 0xCE00);
  535. b43legacy_phy_write(dev, 0x0021, 0x3763);
  536. b43legacy_phy_write(dev, 0x0022, 0x1BC3);
  537. b43legacy_phy_write(dev, 0x0023, 0x06F9);
  538. b43legacy_phy_write(dev, 0x0024, 0x037E);
  539. } else
  540. b43legacy_phy_write(dev, 0x0026, 0xCC00);
  541. b43legacy_phy_write(dev, 0x0030, 0x00C6);
  542. b43legacy_write16(dev, 0x03EC, 0x3F22);
  543. if (phy->analog == 1)
  544. b43legacy_phy_write(dev, 0x0020, 0x3E1C);
  545. else
  546. b43legacy_phy_write(dev, 0x0020, 0x301C);
  547. if (phy->analog == 0)
  548. b43legacy_write16(dev, 0x03E4, 0x3000);
  549. old_channel = (phy->channel == 0xFF) ? 1 : phy->channel;
  550. /* Force to channel 7, even if not supported. */
  551. b43legacy_radio_selectchannel(dev, 7, 0);
  552. if (phy->radio_ver != 0x2050) {
  553. b43legacy_radio_write16(dev, 0x0075, 0x0080);
  554. b43legacy_radio_write16(dev, 0x0079, 0x0081);
  555. }
  556. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  557. b43legacy_radio_write16(dev, 0x0050, 0x0023);
  558. if (phy->radio_ver == 0x2050) {
  559. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  560. b43legacy_radio_write16(dev, 0x005A, 0x0070);
  561. }
  562. b43legacy_radio_write16(dev, 0x005B, 0x007B);
  563. b43legacy_radio_write16(dev, 0x005C, 0x00B0);
  564. b43legacy_radio_write16(dev, 0x007A, b43legacy_radio_read16(dev,
  565. 0x007A) | 0x0007);
  566. b43legacy_radio_selectchannel(dev, old_channel, 0);
  567. b43legacy_phy_write(dev, 0x0014, 0x0080);
  568. b43legacy_phy_write(dev, 0x0032, 0x00CA);
  569. b43legacy_phy_write(dev, 0x002A, 0x88A3);
  570. b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
  571. if (phy->radio_ver == 0x2050)
  572. b43legacy_radio_write16(dev, 0x005D, 0x000D);
  573. b43legacy_write16(dev, 0x03E4, (b43legacy_read16(dev, 0x03E4) &
  574. 0xFFC0) | 0x0004);
  575. }
  576. static void b43legacy_phy_initb6(struct b43legacy_wldev *dev)
  577. {
  578. struct b43legacy_phy *phy = &dev->phy;
  579. u16 offset;
  580. u16 val;
  581. u8 old_channel;
  582. b43legacy_phy_write(dev, 0x003E, 0x817A);
  583. b43legacy_radio_write16(dev, 0x007A,
  584. (b43legacy_radio_read16(dev, 0x007A) | 0x0058));
  585. if (phy->radio_rev == 4 ||
  586. phy->radio_rev == 5) {
  587. b43legacy_radio_write16(dev, 0x0051, 0x0037);
  588. b43legacy_radio_write16(dev, 0x0052, 0x0070);
  589. b43legacy_radio_write16(dev, 0x0053, 0x00B3);
  590. b43legacy_radio_write16(dev, 0x0054, 0x009B);
  591. b43legacy_radio_write16(dev, 0x005A, 0x0088);
  592. b43legacy_radio_write16(dev, 0x005B, 0x0088);
  593. b43legacy_radio_write16(dev, 0x005D, 0x0088);
  594. b43legacy_radio_write16(dev, 0x005E, 0x0088);
  595. b43legacy_radio_write16(dev, 0x007D, 0x0088);
  596. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
  597. B43legacy_UCODEFLAGS_OFFSET,
  598. (b43legacy_shm_read32(dev,
  599. B43legacy_SHM_SHARED,
  600. B43legacy_UCODEFLAGS_OFFSET)
  601. | 0x00000200));
  602. }
  603. if (phy->radio_rev == 8) {
  604. b43legacy_radio_write16(dev, 0x0051, 0x0000);
  605. b43legacy_radio_write16(dev, 0x0052, 0x0040);
  606. b43legacy_radio_write16(dev, 0x0053, 0x00B7);
  607. b43legacy_radio_write16(dev, 0x0054, 0x0098);
  608. b43legacy_radio_write16(dev, 0x005A, 0x0088);
  609. b43legacy_radio_write16(dev, 0x005B, 0x006B);
  610. b43legacy_radio_write16(dev, 0x005C, 0x000F);
  611. if (dev->dev->bus->sprom.boardflags_lo & 0x8000) {
  612. b43legacy_radio_write16(dev, 0x005D, 0x00FA);
  613. b43legacy_radio_write16(dev, 0x005E, 0x00D8);
  614. } else {
  615. b43legacy_radio_write16(dev, 0x005D, 0x00F5);
  616. b43legacy_radio_write16(dev, 0x005E, 0x00B8);
  617. }
  618. b43legacy_radio_write16(dev, 0x0073, 0x0003);
  619. b43legacy_radio_write16(dev, 0x007D, 0x00A8);
  620. b43legacy_radio_write16(dev, 0x007C, 0x0001);
  621. b43legacy_radio_write16(dev, 0x007E, 0x0008);
  622. }
  623. val = 0x1E1F;
  624. for (offset = 0x0088; offset < 0x0098; offset++) {
  625. b43legacy_phy_write(dev, offset, val);
  626. val -= 0x0202;
  627. }
  628. val = 0x3E3F;
  629. for (offset = 0x0098; offset < 0x00A8; offset++) {
  630. b43legacy_phy_write(dev, offset, val);
  631. val -= 0x0202;
  632. }
  633. val = 0x2120;
  634. for (offset = 0x00A8; offset < 0x00C8; offset++) {
  635. b43legacy_phy_write(dev, offset, (val & 0x3F3F));
  636. val += 0x0202;
  637. }
  638. if (phy->type == B43legacy_PHYTYPE_G) {
  639. b43legacy_radio_write16(dev, 0x007A,
  640. b43legacy_radio_read16(dev, 0x007A) |
  641. 0x0020);
  642. b43legacy_radio_write16(dev, 0x0051,
  643. b43legacy_radio_read16(dev, 0x0051) |
  644. 0x0004);
  645. b43legacy_phy_write(dev, 0x0802,
  646. b43legacy_phy_read(dev, 0x0802) | 0x0100);
  647. b43legacy_phy_write(dev, 0x042B,
  648. b43legacy_phy_read(dev, 0x042B) | 0x2000);
  649. b43legacy_phy_write(dev, 0x5B, 0x0000);
  650. b43legacy_phy_write(dev, 0x5C, 0x0000);
  651. }
  652. old_channel = phy->channel;
  653. if (old_channel >= 8)
  654. b43legacy_radio_selectchannel(dev, 1, 0);
  655. else
  656. b43legacy_radio_selectchannel(dev, 13, 0);
  657. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  658. b43legacy_radio_write16(dev, 0x0050, 0x0023);
  659. udelay(40);
  660. if (phy->radio_rev < 6 || phy->radio_rev == 8) {
  661. b43legacy_radio_write16(dev, 0x007C,
  662. (b43legacy_radio_read16(dev, 0x007C)
  663. | 0x0002));
  664. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  665. }
  666. if (phy->radio_rev <= 2) {
  667. b43legacy_radio_write16(dev, 0x0050, 0x0020);
  668. b43legacy_radio_write16(dev, 0x005A, 0x0070);
  669. b43legacy_radio_write16(dev, 0x005B, 0x007B);
  670. b43legacy_radio_write16(dev, 0x005C, 0x00B0);
  671. }
  672. b43legacy_radio_write16(dev, 0x007A,
  673. (b43legacy_radio_read16(dev,
  674. 0x007A) & 0x00F8) | 0x0007);
  675. b43legacy_radio_selectchannel(dev, old_channel, 0);
  676. b43legacy_phy_write(dev, 0x0014, 0x0200);
  677. if (phy->radio_rev >= 6)
  678. b43legacy_phy_write(dev, 0x002A, 0x88C2);
  679. else
  680. b43legacy_phy_write(dev, 0x002A, 0x8AC0);
  681. b43legacy_phy_write(dev, 0x0038, 0x0668);
  682. b43legacy_radio_set_txpower_bg(dev, 0xFFFF, 0xFFFF, 0xFFFF);
  683. if (phy->radio_rev == 4 || phy->radio_rev == 5)
  684. b43legacy_phy_write(dev, 0x005D, (b43legacy_phy_read(dev,
  685. 0x005D) & 0xFF80) | 0x0003);
  686. if (phy->radio_rev <= 2)
  687. b43legacy_radio_write16(dev, 0x005D, 0x000D);
  688. if (phy->analog == 4) {
  689. b43legacy_write16(dev, 0x03E4, 0x0009);
  690. b43legacy_phy_write(dev, 0x61, b43legacy_phy_read(dev, 0x61)
  691. & 0xFFF);
  692. } else
  693. b43legacy_phy_write(dev, 0x0002, (b43legacy_phy_read(dev,
  694. 0x0002) & 0xFFC0) | 0x0004);
  695. if (phy->type == B43legacy_PHYTYPE_G)
  696. b43legacy_write16(dev, 0x03E6, 0x0);
  697. if (phy->type == B43legacy_PHYTYPE_B) {
  698. b43legacy_write16(dev, 0x03E6, 0x8140);
  699. b43legacy_phy_write(dev, 0x0016, 0x0410);
  700. b43legacy_phy_write(dev, 0x0017, 0x0820);
  701. b43legacy_phy_write(dev, 0x0062, 0x0007);
  702. b43legacy_radio_init2050(dev);
  703. b43legacy_phy_lo_g_measure(dev);
  704. if (dev->dev->bus->sprom.boardflags_lo &
  705. B43legacy_BFL_RSSI) {
  706. b43legacy_calc_nrssi_slope(dev);
  707. b43legacy_calc_nrssi_threshold(dev);
  708. }
  709. b43legacy_phy_init_pctl(dev);
  710. }
  711. }
  712. static void b43legacy_calc_loopback_gain(struct b43legacy_wldev *dev)
  713. {
  714. struct b43legacy_phy *phy = &dev->phy;
  715. u16 backup_phy[15] = {0};
  716. u16 backup_radio[3];
  717. u16 backup_bband;
  718. u16 i;
  719. u16 loop1_cnt;
  720. u16 loop1_done;
  721. u16 loop1_omitted;
  722. u16 loop2_done;
  723. backup_phy[0] = b43legacy_phy_read(dev, 0x0429);
  724. backup_phy[1] = b43legacy_phy_read(dev, 0x0001);
  725. backup_phy[2] = b43legacy_phy_read(dev, 0x0811);
  726. backup_phy[3] = b43legacy_phy_read(dev, 0x0812);
  727. if (phy->rev != 1) {
  728. backup_phy[4] = b43legacy_phy_read(dev, 0x0814);
  729. backup_phy[5] = b43legacy_phy_read(dev, 0x0815);
  730. }
  731. backup_phy[6] = b43legacy_phy_read(dev, 0x005A);
  732. backup_phy[7] = b43legacy_phy_read(dev, 0x0059);
  733. backup_phy[8] = b43legacy_phy_read(dev, 0x0058);
  734. backup_phy[9] = b43legacy_phy_read(dev, 0x000A);
  735. backup_phy[10] = b43legacy_phy_read(dev, 0x0003);
  736. backup_phy[11] = b43legacy_phy_read(dev, 0x080F);
  737. backup_phy[12] = b43legacy_phy_read(dev, 0x0810);
  738. backup_phy[13] = b43legacy_phy_read(dev, 0x002B);
  739. backup_phy[14] = b43legacy_phy_read(dev, 0x0015);
  740. b43legacy_phy_read(dev, 0x002D); /* dummy read */
  741. backup_bband = phy->bbatt;
  742. backup_radio[0] = b43legacy_radio_read16(dev, 0x0052);
  743. backup_radio[1] = b43legacy_radio_read16(dev, 0x0043);
  744. backup_radio[2] = b43legacy_radio_read16(dev, 0x007A);
  745. b43legacy_phy_write(dev, 0x0429,
  746. b43legacy_phy_read(dev, 0x0429) & 0x3FFF);
  747. b43legacy_phy_write(dev, 0x0001,
  748. b43legacy_phy_read(dev, 0x0001) & 0x8000);
  749. b43legacy_phy_write(dev, 0x0811,
  750. b43legacy_phy_read(dev, 0x0811) | 0x0002);
  751. b43legacy_phy_write(dev, 0x0812,
  752. b43legacy_phy_read(dev, 0x0812) & 0xFFFD);
  753. b43legacy_phy_write(dev, 0x0811,
  754. b43legacy_phy_read(dev, 0x0811) | 0x0001);
  755. b43legacy_phy_write(dev, 0x0812,
  756. b43legacy_phy_read(dev, 0x0812) & 0xFFFE);
  757. if (phy->rev != 1) {
  758. b43legacy_phy_write(dev, 0x0814,
  759. b43legacy_phy_read(dev, 0x0814) | 0x0001);
  760. b43legacy_phy_write(dev, 0x0815,
  761. b43legacy_phy_read(dev, 0x0815) & 0xFFFE);
  762. b43legacy_phy_write(dev, 0x0814,
  763. b43legacy_phy_read(dev, 0x0814) | 0x0002);
  764. b43legacy_phy_write(dev, 0x0815,
  765. b43legacy_phy_read(dev, 0x0815) & 0xFFFD);
  766. }
  767. b43legacy_phy_write(dev, 0x0811, b43legacy_phy_read(dev, 0x0811) |
  768. 0x000C);
  769. b43legacy_phy_write(dev, 0x0812, b43legacy_phy_read(dev, 0x0812) |
  770. 0x000C);
  771. b43legacy_phy_write(dev, 0x0811, (b43legacy_phy_read(dev, 0x0811)
  772. & 0xFFCF) | 0x0030);
  773. b43legacy_phy_write(dev, 0x0812, (b43legacy_phy_read(dev, 0x0812)
  774. & 0xFFCF) | 0x0010);
  775. b43legacy_phy_write(dev, 0x005A, 0x0780);
  776. b43legacy_phy_write(dev, 0x0059, 0xC810);
  777. b43legacy_phy_write(dev, 0x0058, 0x000D);
  778. if (phy->analog == 0)
  779. b43legacy_phy_write(dev, 0x0003, 0x0122);
  780. else
  781. b43legacy_phy_write(dev, 0x000A,
  782. b43legacy_phy_read(dev, 0x000A)
  783. | 0x2000);
  784. if (phy->rev != 1) {
  785. b43legacy_phy_write(dev, 0x0814,
  786. b43legacy_phy_read(dev, 0x0814) | 0x0004);
  787. b43legacy_phy_write(dev, 0x0815,
  788. b43legacy_phy_read(dev, 0x0815) & 0xFFFB);
  789. }
  790. b43legacy_phy_write(dev, 0x0003,
  791. (b43legacy_phy_read(dev, 0x0003)
  792. & 0xFF9F) | 0x0040);
  793. if (phy->radio_ver == 0x2050 && phy->radio_rev == 2) {
  794. b43legacy_radio_write16(dev, 0x0052, 0x0000);
  795. b43legacy_radio_write16(dev, 0x0043,
  796. (b43legacy_radio_read16(dev, 0x0043)
  797. & 0xFFF0) | 0x0009);
  798. loop1_cnt = 9;
  799. } else if (phy->radio_rev == 8) {
  800. b43legacy_radio_write16(dev, 0x0043, 0x000F);
  801. loop1_cnt = 15;
  802. } else
  803. loop1_cnt = 0;
  804. b43legacy_phy_set_baseband_attenuation(dev, 11);
  805. if (phy->rev >= 3)
  806. b43legacy_phy_write(dev, 0x080F, 0xC020);
  807. else
  808. b43legacy_phy_write(dev, 0x080F, 0x8020);
  809. b43legacy_phy_write(dev, 0x0810, 0x0000);
  810. b43legacy_phy_write(dev, 0x002B,
  811. (b43legacy_phy_read(dev, 0x002B)
  812. & 0xFFC0) | 0x0001);
  813. b43legacy_phy_write(dev, 0x002B,
  814. (b43legacy_phy_read(dev, 0x002B)
  815. & 0xC0FF) | 0x0800);
  816. b43legacy_phy_write(dev, 0x0811,
  817. b43legacy_phy_read(dev, 0x0811) | 0x0100);
  818. b43legacy_phy_write(dev, 0x0812,
  819. b43legacy_phy_read(dev, 0x0812) & 0xCFFF);
  820. if (dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_EXTLNA) {
  821. if (phy->rev >= 7) {
  822. b43legacy_phy_write(dev, 0x0811,
  823. b43legacy_phy_read(dev, 0x0811)
  824. | 0x0800);
  825. b43legacy_phy_write(dev, 0x0812,
  826. b43legacy_phy_read(dev, 0x0812)
  827. | 0x8000);
  828. }
  829. }
  830. b43legacy_radio_write16(dev, 0x007A,
  831. b43legacy_radio_read16(dev, 0x007A)
  832. & 0x00F7);
  833. for (i = 0; i < loop1_cnt; i++) {
  834. b43legacy_radio_write16(dev, 0x0043, loop1_cnt);
  835. b43legacy_phy_write(dev, 0x0812,
  836. (b43legacy_phy_read(dev, 0x0812)
  837. & 0xF0FF) | (i << 8));
  838. b43legacy_phy_write(dev, 0x0015,
  839. (b43legacy_phy_read(dev, 0x0015)
  840. & 0x0FFF) | 0xA000);
  841. b43legacy_phy_write(dev, 0x0015,
  842. (b43legacy_phy_read(dev, 0x0015)
  843. & 0x0FFF) | 0xF000);
  844. udelay(20);
  845. if (b43legacy_phy_read(dev, 0x002D) >= 0x0DFC)
  846. break;
  847. }
  848. loop1_done = i;
  849. loop1_omitted = loop1_cnt - loop1_done;
  850. loop2_done = 0;
  851. if (loop1_done >= 8) {
  852. b43legacy_phy_write(dev, 0x0812,
  853. b43legacy_phy_read(dev, 0x0812)
  854. | 0x0030);
  855. for (i = loop1_done - 8; i < 16; i++) {
  856. b43legacy_phy_write(dev, 0x0812,
  857. (b43legacy_phy_read(dev, 0x0812)
  858. & 0xF0FF) | (i << 8));
  859. b43legacy_phy_write(dev, 0x0015,
  860. (b43legacy_phy_read(dev, 0x0015)
  861. & 0x0FFF) | 0xA000);
  862. b43legacy_phy_write(dev, 0x0015,
  863. (b43legacy_phy_read(dev, 0x0015)
  864. & 0x0FFF) | 0xF000);
  865. udelay(20);
  866. if (b43legacy_phy_read(dev, 0x002D) >= 0x0DFC)
  867. break;
  868. }
  869. }
  870. if (phy->rev != 1) {
  871. b43legacy_phy_write(dev, 0x0814, backup_phy[4]);
  872. b43legacy_phy_write(dev, 0x0815, backup_phy[5]);
  873. }
  874. b43legacy_phy_write(dev, 0x005A, backup_phy[6]);
  875. b43legacy_phy_write(dev, 0x0059, backup_phy[7]);
  876. b43legacy_phy_write(dev, 0x0058, backup_phy[8]);
  877. b43legacy_phy_write(dev, 0x000A, backup_phy[9]);
  878. b43legacy_phy_write(dev, 0x0003, backup_phy[10]);
  879. b43legacy_phy_write(dev, 0x080F, backup_phy[11]);
  880. b43legacy_phy_write(dev, 0x0810, backup_phy[12]);
  881. b43legacy_phy_write(dev, 0x002B, backup_phy[13]);
  882. b43legacy_phy_write(dev, 0x0015, backup_phy[14]);
  883. b43legacy_phy_set_baseband_attenuation(dev, backup_bband);
  884. b43legacy_radio_write16(dev, 0x0052, backup_radio[0]);
  885. b43legacy_radio_write16(dev, 0x0043, backup_radio[1]);
  886. b43legacy_radio_write16(dev, 0x007A, backup_radio[2]);
  887. b43legacy_phy_write(dev, 0x0811, backup_phy[2] | 0x0003);
  888. udelay(10);
  889. b43legacy_phy_write(dev, 0x0811, backup_phy[2]);
  890. b43legacy_phy_write(dev, 0x0812, backup_phy[3]);
  891. b43legacy_phy_write(dev, 0x0429, backup_phy[0]);
  892. b43legacy_phy_write(dev, 0x0001, backup_phy[1]);
  893. phy->loopback_gain[0] = ((loop1_done * 6) - (loop1_omitted * 4)) - 11;
  894. phy->loopback_gain[1] = (24 - (3 * loop2_done)) * 2;
  895. }
  896. static void b43legacy_phy_initg(struct b43legacy_wldev *dev)
  897. {
  898. struct b43legacy_phy *phy = &dev->phy;
  899. u16 tmp;
  900. if (phy->rev == 1)
  901. b43legacy_phy_initb5(dev);
  902. else
  903. b43legacy_phy_initb6(dev);
  904. if (phy->rev >= 2 && phy->gmode)
  905. b43legacy_phy_inita(dev);
  906. if (phy->rev >= 2) {
  907. b43legacy_phy_write(dev, 0x0814, 0x0000);
  908. b43legacy_phy_write(dev, 0x0815, 0x0000);
  909. }
  910. if (phy->rev == 2) {
  911. b43legacy_phy_write(dev, 0x0811, 0x0000);
  912. b43legacy_phy_write(dev, 0x0015, 0x00C0);
  913. }
  914. if (phy->rev > 5) {
  915. b43legacy_phy_write(dev, 0x0811, 0x0400);
  916. b43legacy_phy_write(dev, 0x0015, 0x00C0);
  917. }
  918. if (phy->gmode) {
  919. tmp = b43legacy_phy_read(dev, 0x0400) & 0xFF;
  920. if (tmp == 3) {
  921. b43legacy_phy_write(dev, 0x04C2, 0x1816);
  922. b43legacy_phy_write(dev, 0x04C3, 0x8606);
  923. }
  924. if (tmp == 4 || tmp == 5) {
  925. b43legacy_phy_write(dev, 0x04C2, 0x1816);
  926. b43legacy_phy_write(dev, 0x04C3, 0x8006);
  927. b43legacy_phy_write(dev, 0x04CC,
  928. (b43legacy_phy_read(dev,
  929. 0x04CC) & 0x00FF) |
  930. 0x1F00);
  931. }
  932. if (phy->rev >= 2)
  933. b43legacy_phy_write(dev, 0x047E, 0x0078);
  934. }
  935. if (phy->radio_rev == 8) {
  936. b43legacy_phy_write(dev, 0x0801, b43legacy_phy_read(dev, 0x0801)
  937. | 0x0080);
  938. b43legacy_phy_write(dev, 0x043E, b43legacy_phy_read(dev, 0x043E)
  939. | 0x0004);
  940. }
  941. if (phy->rev >= 2 && phy->gmode)
  942. b43legacy_calc_loopback_gain(dev);
  943. if (phy->radio_rev != 8) {
  944. if (phy->initval == 0xFFFF)
  945. phy->initval = b43legacy_radio_init2050(dev);
  946. else
  947. b43legacy_radio_write16(dev, 0x0078, phy->initval);
  948. }
  949. if (phy->txctl2 == 0xFFFF)
  950. b43legacy_phy_lo_g_measure(dev);
  951. else {
  952. if (phy->radio_ver == 0x2050 && phy->radio_rev == 8)
  953. b43legacy_radio_write16(dev, 0x0052,
  954. (phy->txctl1 << 4) |
  955. phy->txctl2);
  956. else
  957. b43legacy_radio_write16(dev, 0x0052,
  958. (b43legacy_radio_read16(dev,
  959. 0x0052) & 0xFFF0) |
  960. phy->txctl1);
  961. if (phy->rev >= 6)
  962. b43legacy_phy_write(dev, 0x0036,
  963. (b43legacy_phy_read(dev, 0x0036)
  964. & 0x0FFF) | (phy->txctl2 << 12));
  965. if (dev->dev->bus->sprom.boardflags_lo &
  966. B43legacy_BFL_PACTRL)
  967. b43legacy_phy_write(dev, 0x002E, 0x8075);
  968. else
  969. b43legacy_phy_write(dev, 0x002E, 0x807F);
  970. if (phy->rev < 2)
  971. b43legacy_phy_write(dev, 0x002F, 0x0101);
  972. else
  973. b43legacy_phy_write(dev, 0x002F, 0x0202);
  974. }
  975. if (phy->gmode) {
  976. b43legacy_phy_lo_adjust(dev, 0);
  977. b43legacy_phy_write(dev, 0x080F, 0x8078);
  978. }
  979. if (!(dev->dev->bus->sprom.boardflags_lo & B43legacy_BFL_RSSI)) {
  980. /* The specs state to update the NRSSI LT with
  981. * the value 0x7FFFFFFF here. I think that is some weird
  982. * compiler optimization in the original driver.
  983. * Essentially, what we do here is resetting all NRSSI LT
  984. * entries to -32 (see the clamp_val() in nrssi_hw_update())
  985. */
  986. b43legacy_nrssi_hw_update(dev, 0xFFFF);
  987. b43legacy_calc_nrssi_threshold(dev);
  988. } else if (phy->gmode || phy->rev >= 2) {
  989. if (phy->nrssi[0] == -1000) {
  990. B43legacy_WARN_ON(phy->nrssi[1] != -1000);
  991. b43legacy_calc_nrssi_slope(dev);
  992. } else {
  993. B43legacy_WARN_ON(phy->nrssi[1] == -1000);
  994. b43legacy_calc_nrssi_threshold(dev);
  995. }
  996. }
  997. if (phy->radio_rev == 8)
  998. b43legacy_phy_write(dev, 0x0805, 0x3230);
  999. b43legacy_phy_init_pctl(dev);
  1000. if (dev->dev->bus->chip_id == 0x4306
  1001. && dev->dev->bus->chip_package == 2) {
  1002. b43legacy_phy_write(dev, 0x0429,
  1003. b43legacy_phy_read(dev, 0x0429) & 0xBFFF);
  1004. b43legacy_phy_write(dev, 0x04C3,
  1005. b43legacy_phy_read(dev, 0x04C3) & 0x7FFF);
  1006. }
  1007. }
  1008. static u16 b43legacy_phy_lo_b_r15_loop(struct b43legacy_wldev *dev)
  1009. {
  1010. int i;
  1011. u16 ret = 0;
  1012. unsigned long flags;
  1013. local_irq_save(flags);
  1014. for (i = 0; i < 10; i++) {
  1015. b43legacy_phy_write(dev, 0x0015, 0xAFA0);
  1016. udelay(1);
  1017. b43legacy_phy_write(dev, 0x0015, 0xEFA0);
  1018. udelay(10);
  1019. b43legacy_phy_write(dev, 0x0015, 0xFFA0);
  1020. udelay(40);
  1021. ret += b43legacy_phy_read(dev, 0x002C);
  1022. }
  1023. local_irq_restore(flags);
  1024. b43legacy_voluntary_preempt();
  1025. return ret;
  1026. }
  1027. void b43legacy_phy_lo_b_measure(struct b43legacy_wldev *dev)
  1028. {
  1029. struct b43legacy_phy *phy = &dev->phy;
  1030. u16 regstack[12] = { 0 };
  1031. u16 mls;
  1032. u16 fval;
  1033. int i;
  1034. int j;
  1035. regstack[0] = b43legacy_phy_read(dev, 0x0015);
  1036. regstack[1] = b43legacy_radio_read16(dev, 0x0052) & 0xFFF0;
  1037. if (phy->radio_ver == 0x2053) {
  1038. regstack[2] = b43legacy_phy_read(dev, 0x000A);
  1039. regstack[3] = b43legacy_phy_read(dev, 0x002A);
  1040. regstack[4] = b43legacy_phy_read(dev, 0x0035);
  1041. regstack[5] = b43legacy_phy_read(dev, 0x0003);
  1042. regstack[6] = b43legacy_phy_read(dev, 0x0001);
  1043. regstack[7] = b43legacy_phy_read(dev, 0x0030);
  1044. regstack[8] = b43legacy_radio_read16(dev, 0x0043);
  1045. regstack[9] = b43legacy_radio_read16(dev, 0x007A);
  1046. regstack[10] = b43legacy_read16(dev, 0x03EC);
  1047. regstack[11] = b43legacy_radio_read16(dev, 0x0052) & 0x00F0;
  1048. b43legacy_phy_write(dev, 0x0030, 0x00FF);
  1049. b43legacy_write16(dev, 0x03EC, 0x3F3F);
  1050. b43legacy_phy_write(dev, 0x0035, regstack[4] & 0xFF7F);
  1051. b43legacy_radio_write16(dev, 0x007A, regstack[9] & 0xFFF0);
  1052. }
  1053. b43legacy_phy_write(dev, 0x0015, 0xB000);
  1054. b43legacy_phy_write(dev, 0x002B, 0x0004);
  1055. if (phy->radio_ver == 0x2053) {
  1056. b43legacy_phy_write(dev, 0x002B, 0x0203);
  1057. b43legacy_phy_write(dev, 0x002A, 0x08A3);
  1058. }
  1059. phy->minlowsig[0] = 0xFFFF;
  1060. for (i = 0; i < 4; i++) {
  1061. b43legacy_radio_write16(dev, 0x0052, regstack[1] | i);
  1062. b43legacy_phy_lo_b_r15_loop(dev);
  1063. }
  1064. for (i = 0; i < 10; i++) {
  1065. b43legacy_radio_write16(dev, 0x0052, regstack[1] | i);
  1066. mls = b43legacy_phy_lo_b_r15_loop(dev) / 10;
  1067. if (mls < phy->minlowsig[0]) {
  1068. phy->minlowsig[0] = mls;
  1069. phy->minlowsigpos[0] = i;
  1070. }
  1071. }
  1072. b43legacy_radio_write16(dev, 0x0052, regstack[1]
  1073. | phy->minlowsigpos[0]);
  1074. phy->minlowsig[1] = 0xFFFF;
  1075. for (i = -4; i < 5; i += 2) {
  1076. for (j = -4; j < 5; j += 2) {
  1077. if (j < 0)
  1078. fval = (0x0100 * i) + j + 0x0100;
  1079. else
  1080. fval = (0x0100 * i) + j;
  1081. b43legacy_phy_write(dev, 0x002F, fval);
  1082. mls = b43legacy_phy_lo_b_r15_loop(dev) / 10;
  1083. if (mls < phy->minlowsig[1]) {
  1084. phy->minlowsig[1] = mls;
  1085. phy->minlowsigpos[1] = fval;
  1086. }
  1087. }
  1088. }
  1089. phy->minlowsigpos[1] += 0x0101;
  1090. b43legacy_phy_write(dev, 0x002F, phy->minlowsigpos[1]);
  1091. if (phy->radio_ver == 0x2053) {
  1092. b43legacy_phy_write(dev, 0x000A, regstack[2]);
  1093. b43legacy_phy_write(dev, 0x002A, regstack[3]);
  1094. b43legacy_phy_write(dev, 0x0035, regstack[4]);
  1095. b43legacy_phy_write(dev, 0x0003, regstack[5]);
  1096. b43legacy_phy_write(dev, 0x0001, regstack[6]);
  1097. b43legacy_phy_write(dev, 0x0030, regstack[7]);
  1098. b43legacy_radio_write16(dev, 0x0043, regstack[8]);
  1099. b43legacy_radio_write16(dev, 0x007A, regstack[9]);
  1100. b43legacy_radio_write16(dev, 0x0052,
  1101. (b43legacy_radio_read16(dev, 0x0052)
  1102. & 0x000F) | regstack[11]);
  1103. b43legacy_write16(dev, 0x03EC, regstack[10]);
  1104. }
  1105. b43legacy_phy_write(dev, 0x0015, regstack[0]);
  1106. }
  1107. static inline
  1108. u16 b43legacy_phy_lo_g_deviation_subval(struct b43legacy_wldev *dev,
  1109. u16 control)
  1110. {
  1111. struct b43legacy_phy *phy = &dev->phy;
  1112. u16 ret;
  1113. unsigned long flags;
  1114. local_irq_save(flags);
  1115. if (phy->gmode) {
  1116. b43legacy_phy_write(dev, 0x15, 0xE300);
  1117. control <<= 8;
  1118. b43legacy_phy_write(dev, 0x0812, control | 0x00B0);
  1119. udelay(5);
  1120. b43legacy_phy_write(dev, 0x0812, control | 0x00B2);
  1121. udelay(2);
  1122. b43legacy_phy_write(dev, 0x0812, control | 0x00B3);
  1123. udelay(4);
  1124. b43legacy_phy_write(dev, 0x0015, 0xF300);
  1125. udelay(8);
  1126. } else {
  1127. b43legacy_phy_write(dev, 0x0015, control | 0xEFA0);
  1128. udelay(2);
  1129. b43legacy_phy_write(dev, 0x0015, control | 0xEFE0);
  1130. udelay(4);
  1131. b43legacy_phy_write(dev, 0x0015, control | 0xFFE0);
  1132. udelay(8);
  1133. }
  1134. ret = b43legacy_phy_read(dev, 0x002D);
  1135. local_irq_restore(flags);
  1136. b43legacy_voluntary_preempt();
  1137. return ret;
  1138. }
  1139. static u32 b43legacy_phy_lo_g_singledeviation(struct b43legacy_wldev *dev,
  1140. u16 control)
  1141. {
  1142. int i;
  1143. u32 ret = 0;
  1144. for (i = 0; i < 8; i++)
  1145. ret += b43legacy_phy_lo_g_deviation_subval(dev, control);
  1146. return ret;
  1147. }
  1148. /* Write the LocalOscillator CONTROL */
  1149. static inline
  1150. void b43legacy_lo_write(struct b43legacy_wldev *dev,
  1151. struct b43legacy_lopair *pair)
  1152. {
  1153. u16 value;
  1154. value = (u8)(pair->low);
  1155. value |= ((u8)(pair->high)) << 8;
  1156. #ifdef CONFIG_B43LEGACY_DEBUG
  1157. /* Sanity check. */
  1158. if (pair->low < -8 || pair->low > 8 ||
  1159. pair->high < -8 || pair->high > 8) {
  1160. b43legacydbg(dev->wl,
  1161. "WARNING: Writing invalid LOpair "
  1162. "(low: %d, high: %d)\n",
  1163. pair->low, pair->high);
  1164. dump_stack();
  1165. }
  1166. #endif
  1167. b43legacy_phy_write(dev, B43legacy_PHY_G_LO_CONTROL, value);
  1168. }
  1169. static inline
  1170. struct b43legacy_lopair *b43legacy_find_lopair(struct b43legacy_wldev *dev,
  1171. u16 bbatt,
  1172. u16 rfatt,
  1173. u16 tx)
  1174. {
  1175. static const u8 dict[10] = { 11, 10, 11, 12, 13, 12, 13, 12, 13, 12 };
  1176. struct b43legacy_phy *phy = &dev->phy;
  1177. if (bbatt > 6)
  1178. bbatt = 6;
  1179. B43legacy_WARN_ON(rfatt >= 10);
  1180. if (tx == 3)
  1181. return b43legacy_get_lopair(phy, rfatt, bbatt);
  1182. return b43legacy_get_lopair(phy, dict[rfatt], bbatt);
  1183. }
  1184. static inline
  1185. struct b43legacy_lopair *b43legacy_current_lopair(struct b43legacy_wldev *dev)
  1186. {
  1187. struct b43legacy_phy *phy = &dev->phy;
  1188. return b43legacy_find_lopair(dev, phy->bbatt,
  1189. phy->rfatt, phy->txctl1);
  1190. }
  1191. /* Adjust B/G LO */
  1192. void b43legacy_phy_lo_adjust(struct b43legacy_wldev *dev, int fixed)
  1193. {
  1194. struct b43legacy_lopair *pair;
  1195. if (fixed) {
  1196. /* Use fixed values. Only for initialization. */
  1197. pair = b43legacy_find_lopair(dev, 2, 3, 0);
  1198. } else
  1199. pair = b43legacy_current_lopair(dev);
  1200. b43legacy_lo_write(dev, pair);
  1201. }
  1202. static void b43legacy_phy_lo_g_measure_txctl2(struct b43legacy_wldev *dev)
  1203. {
  1204. struct b43legacy_phy *phy = &dev->phy;
  1205. u16 txctl2 = 0;
  1206. u16 i;
  1207. u32 smallest;
  1208. u32 tmp;
  1209. b43legacy_radio_write16(dev, 0x0052, 0x0000);
  1210. udelay(10);
  1211. smallest = b43legacy_phy_lo_g_singledeviation(dev, 0);
  1212. for (i = 0; i < 16; i++) {
  1213. b43legacy_radio_write16(dev, 0x0052, i);
  1214. udelay(10);
  1215. tmp = b43legacy_phy_lo_g_singledeviation(dev, 0);
  1216. if (tmp < smallest) {
  1217. smallest = tmp;
  1218. txctl2 = i;
  1219. }
  1220. }
  1221. phy->txctl2 = txctl2;
  1222. }
  1223. static
  1224. void b43legacy_phy_lo_g_state(struct b43legacy_wldev *dev,
  1225. const struct b43legacy_lopair *in_pair,
  1226. struct b43legacy_lopair *out_pair,
  1227. u16 r27)
  1228. {
  1229. static const struct b43legacy_lopair transitions[8] = {
  1230. { .high = 1, .low = 1, },
  1231. { .high = 1, .low = 0, },
  1232. { .high = 1, .low = -1, },
  1233. { .high = 0, .low = -1, },
  1234. { .high = -1, .low = -1, },
  1235. { .high = -1, .low = 0, },
  1236. { .high = -1, .low = 1, },
  1237. { .high = 0, .low = 1, },
  1238. };
  1239. struct b43legacy_lopair lowest_transition = {
  1240. .high = in_pair->high,
  1241. .low = in_pair->low,
  1242. };
  1243. struct b43legacy_lopair tmp_pair;
  1244. struct b43legacy_lopair transition;
  1245. int i = 12;
  1246. int state = 0;
  1247. int found_lower;
  1248. int j;
  1249. int begin;
  1250. int end;
  1251. u32 lowest_deviation;
  1252. u32 tmp;
  1253. /* Note that in_pair and out_pair can point to the same pair.
  1254. * Be careful. */
  1255. b43legacy_lo_write(dev, &lowest_transition);
  1256. lowest_deviation = b43legacy_phy_lo_g_singledeviation(dev, r27);
  1257. do {
  1258. found_lower = 0;
  1259. B43legacy_WARN_ON(!(state >= 0 && state <= 8));
  1260. if (state == 0) {
  1261. begin = 1;
  1262. end = 8;
  1263. } else if (state % 2 == 0) {
  1264. begin = state - 1;
  1265. end = state + 1;
  1266. } else {
  1267. begin = state - 2;
  1268. end = state + 2;
  1269. }
  1270. if (begin < 1)
  1271. begin += 8;
  1272. if (end > 8)
  1273. end -= 8;
  1274. j = begin;
  1275. tmp_pair.high = lowest_transition.high;
  1276. tmp_pair.low = lowest_transition.low;
  1277. while (1) {
  1278. B43legacy_WARN_ON(!(j >= 1 && j <= 8));
  1279. transition.high = tmp_pair.high +
  1280. transitions[j - 1].high;
  1281. transition.low = tmp_pair.low + transitions[j - 1].low;
  1282. if ((abs(transition.low) < 9)
  1283. && (abs(transition.high) < 9)) {
  1284. b43legacy_lo_write(dev, &transition);
  1285. tmp = b43legacy_phy_lo_g_singledeviation(dev,
  1286. r27);
  1287. if (tmp < lowest_deviation) {
  1288. lowest_deviation = tmp;
  1289. state = j;
  1290. found_lower = 1;
  1291. lowest_transition.high =
  1292. transition.high;
  1293. lowest_transition.low = transition.low;
  1294. }
  1295. }
  1296. if (j == end)
  1297. break;
  1298. if (j == 8)
  1299. j = 1;
  1300. else
  1301. j++;
  1302. }
  1303. } while (i-- && found_lower);
  1304. out_pair->high = lowest_transition.high;
  1305. out_pair->low = lowest_transition.low;
  1306. }
  1307. /* Set the baseband attenuation value on chip. */
  1308. void b43legacy_phy_set_baseband_attenuation(struct b43legacy_wldev *dev,
  1309. u16 bbatt)
  1310. {
  1311. struct b43legacy_phy *phy = &dev->phy;
  1312. u16 value;
  1313. if (phy->analog == 0) {
  1314. value = (b43legacy_read16(dev, 0x03E6) & 0xFFF0);
  1315. value |= (bbatt & 0x000F);
  1316. b43legacy_write16(dev, 0x03E6, value);
  1317. return;
  1318. }
  1319. if (phy->analog > 1) {
  1320. value = b43legacy_phy_read(dev, 0x0060) & 0xFFC3;
  1321. value |= (bbatt << 2) & 0x003C;
  1322. } else {
  1323. value = b43legacy_phy_read(dev, 0x0060) & 0xFF87;
  1324. value |= (bbatt << 3) & 0x0078;
  1325. }
  1326. b43legacy_phy_write(dev, 0x0060, value);
  1327. }
  1328. /* http://bcm-specs.sipsolutions.net/LocalOscillator/Measure */
  1329. void b43legacy_phy_lo_g_measure(struct b43legacy_wldev *dev)
  1330. {
  1331. static const u8 pairorder[10] = { 3, 1, 5, 7, 9, 2, 0, 4, 6, 8 };
  1332. const int is_initializing = (b43legacy_status(dev)
  1333. < B43legacy_STAT_STARTED);
  1334. struct b43legacy_phy *phy = &dev->phy;
  1335. u16 h;
  1336. u16 i;
  1337. u16 oldi = 0;
  1338. u16 j;
  1339. struct b43legacy_lopair control;
  1340. struct b43legacy_lopair *tmp_control;
  1341. u16 tmp;
  1342. u16 regstack[16] = { 0 };
  1343. u8 oldchannel;
  1344. /* XXX: What are these? */
  1345. u8 r27 = 0;
  1346. u16 r31;
  1347. oldchannel = phy->channel;
  1348. /* Setup */
  1349. if (phy->gmode) {
  1350. regstack[0] = b43legacy_phy_read(dev, B43legacy_PHY_G_CRS);
  1351. regstack[1] = b43legacy_phy_read(dev, 0x0802);
  1352. b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]
  1353. & 0x7FFF);
  1354. b43legacy_phy_write(dev, 0x0802, regstack[1] & 0xFFFC);
  1355. }
  1356. regstack[3] = b43legacy_read16(dev, 0x03E2);
  1357. b43legacy_write16(dev, 0x03E2, regstack[3] | 0x8000);
  1358. regstack[4] = b43legacy_read16(dev, B43legacy_MMIO_CHANNEL_EXT);
  1359. regstack[5] = b43legacy_phy_read(dev, 0x15);
  1360. regstack[6] = b43legacy_phy_read(dev, 0x2A);
  1361. regstack[7] = b43legacy_phy_read(dev, 0x35);
  1362. regstack[8] = b43legacy_phy_read(dev, 0x60);
  1363. regstack[9] = b43legacy_radio_read16(dev, 0x43);
  1364. regstack[10] = b43legacy_radio_read16(dev, 0x7A);
  1365. regstack[11] = b43legacy_radio_read16(dev, 0x52);
  1366. if (phy->gmode) {
  1367. regstack[12] = b43legacy_phy_read(dev, 0x0811);
  1368. regstack[13] = b43legacy_phy_read(dev, 0x0812);
  1369. regstack[14] = b43legacy_phy_read(dev, 0x0814);
  1370. regstack[15] = b43legacy_phy_read(dev, 0x0815);
  1371. }
  1372. b43legacy_radio_selectchannel(dev, 6, 0);
  1373. if (phy->gmode) {
  1374. b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]
  1375. & 0x7FFF);
  1376. b43legacy_phy_write(dev, 0x0802, regstack[1] & 0xFFFC);
  1377. b43legacy_dummy_transmission(dev);
  1378. }
  1379. b43legacy_radio_write16(dev, 0x0043, 0x0006);
  1380. b43legacy_phy_set_baseband_attenuation(dev, 2);
  1381. b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, 0x0000);
  1382. b43legacy_phy_write(dev, 0x002E, 0x007F);
  1383. b43legacy_phy_write(dev, 0x080F, 0x0078);
  1384. b43legacy_phy_write(dev, 0x0035, regstack[7] & ~(1 << 7));
  1385. b43legacy_radio_write16(dev, 0x007A, regstack[10] & 0xFFF0);
  1386. b43legacy_phy_write(dev, 0x002B, 0x0203);
  1387. b43legacy_phy_write(dev, 0x002A, 0x08A3);
  1388. if (phy->gmode) {
  1389. b43legacy_phy_write(dev, 0x0814, regstack[14] | 0x0003);
  1390. b43legacy_phy_write(dev, 0x0815, regstack[15] & 0xFFFC);
  1391. b43legacy_phy_write(dev, 0x0811, 0x01B3);
  1392. b43legacy_phy_write(dev, 0x0812, 0x00B2);
  1393. }
  1394. if (is_initializing)
  1395. b43legacy_phy_lo_g_measure_txctl2(dev);
  1396. b43legacy_phy_write(dev, 0x080F, 0x8078);
  1397. /* Measure */
  1398. control.low = 0;
  1399. control.high = 0;
  1400. for (h = 0; h < 10; h++) {
  1401. /* Loop over each possible RadioAttenuation (0-9) */
  1402. i = pairorder[h];
  1403. if (is_initializing) {
  1404. if (i == 3) {
  1405. control.low = 0;
  1406. control.high = 0;
  1407. } else if (((i % 2 == 1) && (oldi % 2 == 1)) ||
  1408. ((i % 2 == 0) && (oldi % 2 == 0))) {
  1409. tmp_control = b43legacy_get_lopair(phy, oldi,
  1410. 0);
  1411. memcpy(&control, tmp_control, sizeof(control));
  1412. } else {
  1413. tmp_control = b43legacy_get_lopair(phy, 3, 0);
  1414. memcpy(&control, tmp_control, sizeof(control));
  1415. }
  1416. }
  1417. /* Loop over each possible BasebandAttenuation/2 */
  1418. for (j = 0; j < 4; j++) {
  1419. if (is_initializing) {
  1420. tmp = i * 2 + j;
  1421. r27 = 0;
  1422. r31 = 0;
  1423. if (tmp > 14) {
  1424. r31 = 1;
  1425. if (tmp > 17)
  1426. r27 = 1;
  1427. if (tmp > 19)
  1428. r27 = 2;
  1429. }
  1430. } else {
  1431. tmp_control = b43legacy_get_lopair(phy, i,
  1432. j * 2);
  1433. if (!tmp_control->used)
  1434. continue;
  1435. memcpy(&control, tmp_control, sizeof(control));
  1436. r27 = 3;
  1437. r31 = 0;
  1438. }
  1439. b43legacy_radio_write16(dev, 0x43, i);
  1440. b43legacy_radio_write16(dev, 0x52, phy->txctl2);
  1441. udelay(10);
  1442. b43legacy_voluntary_preempt();
  1443. b43legacy_phy_set_baseband_attenuation(dev, j * 2);
  1444. tmp = (regstack[10] & 0xFFF0);
  1445. if (r31)
  1446. tmp |= 0x0008;
  1447. b43legacy_radio_write16(dev, 0x007A, tmp);
  1448. tmp_control = b43legacy_get_lopair(phy, i, j * 2);
  1449. b43legacy_phy_lo_g_state(dev, &control, tmp_control,
  1450. r27);
  1451. }
  1452. oldi = i;
  1453. }
  1454. /* Loop over each possible RadioAttenuation (10-13) */
  1455. for (i = 10; i < 14; i++) {
  1456. /* Loop over each possible BasebandAttenuation/2 */
  1457. for (j = 0; j < 4; j++) {
  1458. if (is_initializing) {
  1459. tmp_control = b43legacy_get_lopair(phy, i - 9,
  1460. j * 2);
  1461. memcpy(&control, tmp_control, sizeof(control));
  1462. /* FIXME: The next line is wrong, as the
  1463. * following if statement can never trigger. */
  1464. tmp = (i - 9) * 2 + j - 5;
  1465. r27 = 0;
  1466. r31 = 0;
  1467. if (tmp > 14) {
  1468. r31 = 1;
  1469. if (tmp > 17)
  1470. r27 = 1;
  1471. if (tmp > 19)
  1472. r27 = 2;
  1473. }
  1474. } else {
  1475. tmp_control = b43legacy_get_lopair(phy, i - 9,
  1476. j * 2);
  1477. if (!tmp_control->used)
  1478. continue;
  1479. memcpy(&control, tmp_control, sizeof(control));
  1480. r27 = 3;
  1481. r31 = 0;
  1482. }
  1483. b43legacy_radio_write16(dev, 0x43, i - 9);
  1484. /* FIXME: shouldn't txctl1 be zero in the next line
  1485. * and 3 in the loop above? */
  1486. b43legacy_radio_write16(dev, 0x52,
  1487. phy->txctl2
  1488. | (3/*txctl1*/ << 4));
  1489. udelay(10);
  1490. b43legacy_voluntary_preempt();
  1491. b43legacy_phy_set_baseband_attenuation(dev, j * 2);
  1492. tmp = (regstack[10] & 0xFFF0);
  1493. if (r31)
  1494. tmp |= 0x0008;
  1495. b43legacy_radio_write16(dev, 0x7A, tmp);
  1496. tmp_control = b43legacy_get_lopair(phy, i, j * 2);
  1497. b43legacy_phy_lo_g_state(dev, &control, tmp_control,
  1498. r27);
  1499. }
  1500. }
  1501. /* Restoration */
  1502. if (phy->gmode) {
  1503. b43legacy_phy_write(dev, 0x0015, 0xE300);
  1504. b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA0);
  1505. udelay(5);
  1506. b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA2);
  1507. udelay(2);
  1508. b43legacy_phy_write(dev, 0x0812, (r27 << 8) | 0xA3);
  1509. b43legacy_voluntary_preempt();
  1510. } else
  1511. b43legacy_phy_write(dev, 0x0015, r27 | 0xEFA0);
  1512. b43legacy_phy_lo_adjust(dev, is_initializing);
  1513. b43legacy_phy_write(dev, 0x002E, 0x807F);
  1514. if (phy->gmode)
  1515. b43legacy_phy_write(dev, 0x002F, 0x0202);
  1516. else
  1517. b43legacy_phy_write(dev, 0x002F, 0x0101);
  1518. b43legacy_write16(dev, B43legacy_MMIO_CHANNEL_EXT, regstack[4]);
  1519. b43legacy_phy_write(dev, 0x0015, regstack[5]);
  1520. b43legacy_phy_write(dev, 0x002A, regstack[6]);
  1521. b43legacy_phy_write(dev, 0x0035, regstack[7]);
  1522. b43legacy_phy_write(dev, 0x0060, regstack[8]);
  1523. b43legacy_radio_write16(dev, 0x0043, regstack[9]);
  1524. b43legacy_radio_write16(dev, 0x007A, regstack[10]);
  1525. regstack[11] &= 0x00F0;
  1526. regstack[11] |= (b43legacy_radio_read16(dev, 0x52) & 0x000F);
  1527. b43legacy_radio_write16(dev, 0x52, regstack[11]);
  1528. b43legacy_write16(dev, 0x03E2, regstack[3]);
  1529. if (phy->gmode) {
  1530. b43legacy_phy_write(dev, 0x0811, regstack[12]);
  1531. b43legacy_phy_write(dev, 0x0812, regstack[13]);
  1532. b43legacy_phy_write(dev, 0x0814, regstack[14]);
  1533. b43legacy_phy_write(dev, 0x0815, regstack[15]);
  1534. b43legacy_phy_write(dev, B43legacy_PHY_G_CRS, regstack[0]);
  1535. b43legacy_phy_write(dev, 0x0802, regstack[1]);
  1536. }
  1537. b43legacy_radio_selectchannel(dev, oldchannel, 1);
  1538. #ifdef CONFIG_B43LEGACY_DEBUG
  1539. {
  1540. /* Sanity check for all lopairs. */
  1541. for (i = 0; i < B43legacy_LO_COUNT; i++) {
  1542. tmp_control = phy->_lo_pairs + i;
  1543. if (tmp_control->low < -8 || tmp_control->low > 8 ||
  1544. tmp_control->high < -8 || tmp_control->high > 8)
  1545. b43legacywarn(dev->wl,
  1546. "WARNING: Invalid LOpair (low: %d, high:"
  1547. " %d, index: %d)\n",
  1548. tmp_control->low, tmp_control->high, i);
  1549. }
  1550. }
  1551. #endif /* CONFIG_B43LEGACY_DEBUG */
  1552. }
  1553. static
  1554. void b43legacy_phy_lo_mark_current_used(struct b43legacy_wldev *dev)
  1555. {
  1556. struct b43legacy_lopair *pair;
  1557. pair = b43legacy_current_lopair(dev);
  1558. pair->used = 1;
  1559. }
  1560. void b43legacy_phy_lo_mark_all_unused(struct b43legacy_wldev *dev)
  1561. {
  1562. struct b43legacy_phy *phy = &dev->phy;
  1563. struct b43legacy_lopair *pair;
  1564. int i;
  1565. for (i = 0; i < B43legacy_LO_COUNT; i++) {
  1566. pair = phy->_lo_pairs + i;
  1567. pair->used = 0;
  1568. }
  1569. }
  1570. /* http://bcm-specs.sipsolutions.net/EstimatePowerOut
  1571. * This function converts a TSSI value to dBm in Q5.2
  1572. */
  1573. static s8 b43legacy_phy_estimate_power_out(struct b43legacy_wldev *dev, s8 tssi)
  1574. {
  1575. struct b43legacy_phy *phy = &dev->phy;
  1576. s8 dbm = 0;
  1577. s32 tmp;
  1578. tmp = phy->idle_tssi;
  1579. tmp += tssi;
  1580. tmp -= phy->savedpctlreg;
  1581. switch (phy->type) {
  1582. case B43legacy_PHYTYPE_B:
  1583. case B43legacy_PHYTYPE_G:
  1584. tmp = clamp_val(tmp, 0x00, 0x3F);
  1585. dbm = phy->tssi2dbm[tmp];
  1586. break;
  1587. default:
  1588. B43legacy_BUG_ON(1);
  1589. }
  1590. return dbm;
  1591. }
  1592. /* http://bcm-specs.sipsolutions.net/RecalculateTransmissionPower */
  1593. void b43legacy_phy_xmitpower(struct b43legacy_wldev *dev)
  1594. {
  1595. struct b43legacy_phy *phy = &dev->phy;
  1596. u16 tmp;
  1597. u16 txpower;
  1598. s8 v0;
  1599. s8 v1;
  1600. s8 v2;
  1601. s8 v3;
  1602. s8 average;
  1603. int max_pwr;
  1604. s16 desired_pwr;
  1605. s16 estimated_pwr;
  1606. s16 pwr_adjust;
  1607. s16 radio_att_delta;
  1608. s16 baseband_att_delta;
  1609. s16 radio_attenuation;
  1610. s16 baseband_attenuation;
  1611. if (phy->savedpctlreg == 0xFFFF)
  1612. return;
  1613. if ((dev->dev->bus->boardinfo.type == 0x0416) &&
  1614. is_bcm_board_vendor(dev))
  1615. return;
  1616. #ifdef CONFIG_B43LEGACY_DEBUG
  1617. if (phy->manual_txpower_control)
  1618. return;
  1619. #endif
  1620. B43legacy_BUG_ON(!(phy->type == B43legacy_PHYTYPE_B ||
  1621. phy->type == B43legacy_PHYTYPE_G));
  1622. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x0058);
  1623. v0 = (s8)(tmp & 0x00FF);
  1624. v1 = (s8)((tmp & 0xFF00) >> 8);
  1625. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x005A);
  1626. v2 = (s8)(tmp & 0x00FF);
  1627. v3 = (s8)((tmp & 0xFF00) >> 8);
  1628. tmp = 0;
  1629. if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F) {
  1630. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1631. 0x0070);
  1632. v0 = (s8)(tmp & 0x00FF);
  1633. v1 = (s8)((tmp & 0xFF00) >> 8);
  1634. tmp = b43legacy_shm_read16(dev, B43legacy_SHM_SHARED,
  1635. 0x0072);
  1636. v2 = (s8)(tmp & 0x00FF);
  1637. v3 = (s8)((tmp & 0xFF00) >> 8);
  1638. if (v0 == 0x7F || v1 == 0x7F || v2 == 0x7F || v3 == 0x7F)
  1639. return;
  1640. v0 = (v0 + 0x20) & 0x3F;
  1641. v1 = (v1 + 0x20) & 0x3F;
  1642. v2 = (v2 + 0x20) & 0x3F;
  1643. v3 = (v3 + 0x20) & 0x3F;
  1644. tmp = 1;
  1645. }
  1646. b43legacy_radio_clear_tssi(dev);
  1647. average = (v0 + v1 + v2 + v3 + 2) / 4;
  1648. if (tmp && (b43legacy_shm_read16(dev, B43legacy_SHM_SHARED, 0x005E)
  1649. & 0x8))
  1650. average -= 13;
  1651. estimated_pwr = b43legacy_phy_estimate_power_out(dev, average);
  1652. max_pwr = dev->dev->bus->sprom.maxpwr_bg;
  1653. if ((dev->dev->bus->sprom.boardflags_lo
  1654. & B43legacy_BFL_PACTRL) &&
  1655. (phy->type == B43legacy_PHYTYPE_G))
  1656. max_pwr -= 0x3;
  1657. if (unlikely(max_pwr <= 0)) {
  1658. b43legacywarn(dev->wl, "Invalid max-TX-power value in SPROM."
  1659. "\n");
  1660. max_pwr = 74; /* fake it */
  1661. dev->dev->bus->sprom.maxpwr_bg = max_pwr;
  1662. }
  1663. /* Use regulatory information to get the maximum power.
  1664. * In the absence of such data from mac80211, we will use 20 dBm, which
  1665. * is the value for the EU, US, Canada, and most of the world.
  1666. * The regulatory maximum is reduced by the antenna gain (from sprom)
  1667. * and 1.5 dBm (a safety factor??). The result is in Q5.2 format
  1668. * which accounts for the factor of 4 */
  1669. #define REG_MAX_PWR 20
  1670. max_pwr = min(REG_MAX_PWR * 4
  1671. - dev->dev->bus->sprom.antenna_gain.ghz24.a0
  1672. - 0x6, max_pwr);
  1673. /* find the desired power in Q5.2 - power_level is in dBm
  1674. * and limit it - max_pwr is already in Q5.2 */
  1675. desired_pwr = clamp_val(phy->power_level << 2, 0, max_pwr);
  1676. if (b43legacy_debug(dev, B43legacy_DBG_XMITPOWER))
  1677. b43legacydbg(dev->wl, "Current TX power output: " Q52_FMT
  1678. " dBm, Desired TX power output: " Q52_FMT
  1679. " dBm\n", Q52_ARG(estimated_pwr),
  1680. Q52_ARG(desired_pwr));
  1681. /* Check if we need to adjust the current power. The factor of 2 is
  1682. * for damping */
  1683. pwr_adjust = (desired_pwr - estimated_pwr) / 2;
  1684. /* RF attenuation delta
  1685. * The minus sign is because lower attenuation => more power */
  1686. radio_att_delta = -(pwr_adjust + 7) >> 3;
  1687. /* Baseband attenuation delta */
  1688. baseband_att_delta = -(pwr_adjust >> 1) - (4 * radio_att_delta);
  1689. /* Do we need to adjust anything? */
  1690. if ((radio_att_delta == 0) && (baseband_att_delta == 0)) {
  1691. b43legacy_phy_lo_mark_current_used(dev);
  1692. return;
  1693. }
  1694. /* Calculate the new attenuation values. */
  1695. baseband_attenuation = phy->bbatt;
  1696. baseband_attenuation += baseband_att_delta;
  1697. radio_attenuation = phy->rfatt;
  1698. radio_attenuation += radio_att_delta;
  1699. /* Get baseband and radio attenuation values into permitted ranges.
  1700. * baseband 0-11, radio 0-9.
  1701. * Radio attenuation affects power level 4 times as much as baseband.
  1702. */
  1703. if (radio_attenuation < 0) {
  1704. baseband_attenuation -= (4 * -radio_attenuation);
  1705. radio_attenuation = 0;
  1706. } else if (radio_attenuation > 9) {
  1707. baseband_attenuation += (4 * (radio_attenuation - 9));
  1708. radio_attenuation = 9;
  1709. } else {
  1710. while (baseband_attenuation < 0 && radio_attenuation > 0) {
  1711. baseband_attenuation += 4;
  1712. radio_attenuation--;
  1713. }
  1714. while (baseband_attenuation > 11 && radio_attenuation < 9) {
  1715. baseband_attenuation -= 4;
  1716. radio_attenuation++;
  1717. }
  1718. }
  1719. baseband_attenuation = clamp_val(baseband_attenuation, 0, 11);
  1720. txpower = phy->txctl1;
  1721. if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 2)) {
  1722. if (radio_attenuation <= 1) {
  1723. if (txpower == 0) {
  1724. txpower = 3;
  1725. radio_attenuation += 2;
  1726. baseband_attenuation += 2;
  1727. } else if (dev->dev->bus->sprom.boardflags_lo
  1728. & B43legacy_BFL_PACTRL) {
  1729. baseband_attenuation += 4 *
  1730. (radio_attenuation - 2);
  1731. radio_attenuation = 2;
  1732. }
  1733. } else if (radio_attenuation > 4 && txpower != 0) {
  1734. txpower = 0;
  1735. if (baseband_attenuation < 3) {
  1736. radio_attenuation -= 3;
  1737. baseband_attenuation += 2;
  1738. } else {
  1739. radio_attenuation -= 2;
  1740. baseband_attenuation -= 2;
  1741. }
  1742. }
  1743. }
  1744. /* Save the control values */
  1745. phy->txctl1 = txpower;
  1746. baseband_attenuation = clamp_val(baseband_attenuation, 0, 11);
  1747. radio_attenuation = clamp_val(radio_attenuation, 0, 9);
  1748. phy->rfatt = radio_attenuation;
  1749. phy->bbatt = baseband_attenuation;
  1750. /* Adjust the hardware */
  1751. b43legacy_phy_lock(dev);
  1752. b43legacy_radio_lock(dev);
  1753. b43legacy_radio_set_txpower_bg(dev, baseband_attenuation,
  1754. radio_attenuation, txpower);
  1755. b43legacy_phy_lo_mark_current_used(dev);
  1756. b43legacy_radio_unlock(dev);
  1757. b43legacy_phy_unlock(dev);
  1758. }
  1759. static inline
  1760. s32 b43legacy_tssi2dbm_ad(s32 num, s32 den)
  1761. {
  1762. if (num < 0)
  1763. return num/den;
  1764. else
  1765. return (num+den/2)/den;
  1766. }
  1767. static inline
  1768. s8 b43legacy_tssi2dbm_entry(s8 entry [], u8 index, s16 pab0, s16 pab1, s16 pab2)
  1769. {
  1770. s32 m1;
  1771. s32 m2;
  1772. s32 f = 256;
  1773. s32 q;
  1774. s32 delta;
  1775. s8 i = 0;
  1776. m1 = b43legacy_tssi2dbm_ad(16 * pab0 + index * pab1, 32);
  1777. m2 = max(b43legacy_tssi2dbm_ad(32768 + index * pab2, 256), 1);
  1778. do {
  1779. if (i > 15)
  1780. return -EINVAL;
  1781. q = b43legacy_tssi2dbm_ad(f * 4096 -
  1782. b43legacy_tssi2dbm_ad(m2 * f, 16) *
  1783. f, 2048);
  1784. delta = abs(q - f);
  1785. f = q;
  1786. i++;
  1787. } while (delta >= 2);
  1788. entry[index] = clamp_val(b43legacy_tssi2dbm_ad(m1 * f, 8192),
  1789. -127, 128);
  1790. return 0;
  1791. }
  1792. /* http://bcm-specs.sipsolutions.net/TSSI_to_DBM_Table */
  1793. int b43legacy_phy_init_tssi2dbm_table(struct b43legacy_wldev *dev)
  1794. {
  1795. struct b43legacy_phy *phy = &dev->phy;
  1796. s16 pab0;
  1797. s16 pab1;
  1798. s16 pab2;
  1799. u8 idx;
  1800. s8 *dyn_tssi2dbm;
  1801. B43legacy_WARN_ON(!(phy->type == B43legacy_PHYTYPE_B ||
  1802. phy->type == B43legacy_PHYTYPE_G));
  1803. pab0 = (s16)(dev->dev->bus->sprom.pa0b0);
  1804. pab1 = (s16)(dev->dev->bus->sprom.pa0b1);
  1805. pab2 = (s16)(dev->dev->bus->sprom.pa0b2);
  1806. if ((dev->dev->bus->chip_id == 0x4301) && (phy->radio_ver != 0x2050)) {
  1807. phy->idle_tssi = 0x34;
  1808. phy->tssi2dbm = b43legacy_tssi2dbm_b_table;
  1809. return 0;
  1810. }
  1811. if (pab0 != 0 && pab1 != 0 && pab2 != 0 &&
  1812. pab0 != -1 && pab1 != -1 && pab2 != -1) {
  1813. /* The pabX values are set in SPROM. Use them. */
  1814. if ((s8)dev->dev->bus->sprom.itssi_bg != 0 &&
  1815. (s8)dev->dev->bus->sprom.itssi_bg != -1)
  1816. phy->idle_tssi = (s8)(dev->dev->bus->sprom.
  1817. itssi_bg);
  1818. else
  1819. phy->idle_tssi = 62;
  1820. dyn_tssi2dbm = kmalloc(64, GFP_KERNEL);
  1821. if (dyn_tssi2dbm == NULL) {
  1822. b43legacyerr(dev->wl, "Could not allocate memory "
  1823. "for tssi2dbm table\n");
  1824. return -ENOMEM;
  1825. }
  1826. for (idx = 0; idx < 64; idx++)
  1827. if (b43legacy_tssi2dbm_entry(dyn_tssi2dbm, idx, pab0,
  1828. pab1, pab2)) {
  1829. phy->tssi2dbm = NULL;
  1830. b43legacyerr(dev->wl, "Could not generate "
  1831. "tssi2dBm table\n");
  1832. kfree(dyn_tssi2dbm);
  1833. return -ENODEV;
  1834. }
  1835. phy->tssi2dbm = dyn_tssi2dbm;
  1836. phy->dyn_tssi_tbl = 1;
  1837. } else {
  1838. /* pabX values not set in SPROM. */
  1839. switch (phy->type) {
  1840. case B43legacy_PHYTYPE_B:
  1841. phy->idle_tssi = 0x34;
  1842. phy->tssi2dbm = b43legacy_tssi2dbm_b_table;
  1843. break;
  1844. case B43legacy_PHYTYPE_G:
  1845. phy->idle_tssi = 0x34;
  1846. phy->tssi2dbm = b43legacy_tssi2dbm_g_table;
  1847. break;
  1848. }
  1849. }
  1850. return 0;
  1851. }
  1852. int b43legacy_phy_init(struct b43legacy_wldev *dev)
  1853. {
  1854. struct b43legacy_phy *phy = &dev->phy;
  1855. int err = -ENODEV;
  1856. switch (phy->type) {
  1857. case B43legacy_PHYTYPE_B:
  1858. switch (phy->rev) {
  1859. case 2:
  1860. b43legacy_phy_initb2(dev);
  1861. err = 0;
  1862. break;
  1863. case 4:
  1864. b43legacy_phy_initb4(dev);
  1865. err = 0;
  1866. break;
  1867. case 5:
  1868. b43legacy_phy_initb5(dev);
  1869. err = 0;
  1870. break;
  1871. case 6:
  1872. b43legacy_phy_initb6(dev);
  1873. err = 0;
  1874. break;
  1875. }
  1876. break;
  1877. case B43legacy_PHYTYPE_G:
  1878. b43legacy_phy_initg(dev);
  1879. err = 0;
  1880. break;
  1881. }
  1882. if (err)
  1883. b43legacyerr(dev->wl, "Unknown PHYTYPE found\n");
  1884. return err;
  1885. }
  1886. void b43legacy_phy_set_antenna_diversity(struct b43legacy_wldev *dev)
  1887. {
  1888. struct b43legacy_phy *phy = &dev->phy;
  1889. u16 antennadiv;
  1890. u16 offset;
  1891. u16 value;
  1892. u32 ucodeflags;
  1893. antennadiv = phy->antenna_diversity;
  1894. if (antennadiv == 0xFFFF)
  1895. antennadiv = 3;
  1896. B43legacy_WARN_ON(antennadiv > 3);
  1897. ucodeflags = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,
  1898. B43legacy_UCODEFLAGS_OFFSET);
  1899. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
  1900. B43legacy_UCODEFLAGS_OFFSET,
  1901. ucodeflags & ~B43legacy_UCODEFLAG_AUTODIV);
  1902. switch (phy->type) {
  1903. case B43legacy_PHYTYPE_G:
  1904. offset = 0x0400;
  1905. if (antennadiv == 2)
  1906. value = (3/*automatic*/ << 7);
  1907. else
  1908. value = (antennadiv << 7);
  1909. b43legacy_phy_write(dev, offset + 1,
  1910. (b43legacy_phy_read(dev, offset + 1)
  1911. & 0x7E7F) | value);
  1912. if (antennadiv >= 2) {
  1913. if (antennadiv == 2)
  1914. value = (antennadiv << 7);
  1915. else
  1916. value = (0/*force0*/ << 7);
  1917. b43legacy_phy_write(dev, offset + 0x2B,
  1918. (b43legacy_phy_read(dev,
  1919. offset + 0x2B)
  1920. & 0xFEFF) | value);
  1921. }
  1922. if (phy->type == B43legacy_PHYTYPE_G) {
  1923. if (antennadiv >= 2)
  1924. b43legacy_phy_write(dev, 0x048C,
  1925. b43legacy_phy_read(dev,
  1926. 0x048C) | 0x2000);
  1927. else
  1928. b43legacy_phy_write(dev, 0x048C,
  1929. b43legacy_phy_read(dev,
  1930. 0x048C) & ~0x2000);
  1931. if (phy->rev >= 2) {
  1932. b43legacy_phy_write(dev, 0x0461,
  1933. b43legacy_phy_read(dev,
  1934. 0x0461) | 0x0010);
  1935. b43legacy_phy_write(dev, 0x04AD,
  1936. (b43legacy_phy_read(dev,
  1937. 0x04AD)
  1938. & 0x00FF) | 0x0015);
  1939. if (phy->rev == 2)
  1940. b43legacy_phy_write(dev, 0x0427,
  1941. 0x0008);
  1942. else
  1943. b43legacy_phy_write(dev, 0x0427,
  1944. (b43legacy_phy_read(dev, 0x0427)
  1945. & 0x00FF) | 0x0008);
  1946. } else if (phy->rev >= 6)
  1947. b43legacy_phy_write(dev, 0x049B, 0x00DC);
  1948. } else {
  1949. if (phy->rev < 3)
  1950. b43legacy_phy_write(dev, 0x002B,
  1951. (b43legacy_phy_read(dev,
  1952. 0x002B) & 0x00FF)
  1953. | 0x0024);
  1954. else {
  1955. b43legacy_phy_write(dev, 0x0061,
  1956. b43legacy_phy_read(dev,
  1957. 0x0061) | 0x0010);
  1958. if (phy->rev == 3) {
  1959. b43legacy_phy_write(dev, 0x0093,
  1960. 0x001D);
  1961. b43legacy_phy_write(dev, 0x0027,
  1962. 0x0008);
  1963. } else {
  1964. b43legacy_phy_write(dev, 0x0093,
  1965. 0x003A);
  1966. b43legacy_phy_write(dev, 0x0027,
  1967. (b43legacy_phy_read(dev, 0x0027)
  1968. & 0x00FF) | 0x0008);
  1969. }
  1970. }
  1971. }
  1972. break;
  1973. case B43legacy_PHYTYPE_B:
  1974. if (dev->dev->id.revision == 2)
  1975. value = (3/*automatic*/ << 7);
  1976. else
  1977. value = (antennadiv << 7);
  1978. b43legacy_phy_write(dev, 0x03E2,
  1979. (b43legacy_phy_read(dev, 0x03E2)
  1980. & 0xFE7F) | value);
  1981. break;
  1982. default:
  1983. B43legacy_WARN_ON(1);
  1984. }
  1985. if (antennadiv >= 2) {
  1986. ucodeflags = b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,
  1987. B43legacy_UCODEFLAGS_OFFSET);
  1988. b43legacy_shm_write32(dev, B43legacy_SHM_SHARED,
  1989. B43legacy_UCODEFLAGS_OFFSET,
  1990. ucodeflags | B43legacy_UCODEFLAG_AUTODIV);
  1991. }
  1992. phy->antenna_diversity = antennadiv;
  1993. }
  1994. /* Set the PowerSavingControlBits.
  1995. * Bitvalues:
  1996. * 0 => unset the bit
  1997. * 1 => set the bit
  1998. * -1 => calculate the bit
  1999. */
  2000. void b43legacy_power_saving_ctl_bits(struct b43legacy_wldev *dev,
  2001. int bit25, int bit26)
  2002. {
  2003. int i;
  2004. u32 status;
  2005. /* FIXME: Force 25 to off and 26 to on for now: */
  2006. bit25 = 0;
  2007. bit26 = 1;
  2008. if (bit25 == -1) {
  2009. /* TODO: If powersave is not off and FIXME is not set and we
  2010. * are not in adhoc and thus is not an AP and we arei
  2011. * associated, set bit 25 */
  2012. }
  2013. if (bit26 == -1) {
  2014. /* TODO: If the device is awake or this is an AP, or we are
  2015. * scanning, or FIXME, or we are associated, or FIXME,
  2016. * or the latest PS-Poll packet sent was successful,
  2017. * set bit26 */
  2018. }
  2019. status = b43legacy_read32(dev, B43legacy_MMIO_MACCTL);
  2020. if (bit25)
  2021. status |= B43legacy_MACCTL_HWPS;
  2022. else
  2023. status &= ~B43legacy_MACCTL_HWPS;
  2024. if (bit26)
  2025. status |= B43legacy_MACCTL_AWAKE;
  2026. else
  2027. status &= ~B43legacy_MACCTL_AWAKE;
  2028. b43legacy_write32(dev, B43legacy_MMIO_MACCTL, status);
  2029. if (bit26 && dev->dev->id.revision >= 5) {
  2030. for (i = 0; i < 100; i++) {
  2031. if (b43legacy_shm_read32(dev, B43legacy_SHM_SHARED,
  2032. 0x0040) != 4)
  2033. break;
  2034. udelay(10);
  2035. }
  2036. }
  2037. }