phy.c 65 KB

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