rt2800pci.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338
  1. /*
  2. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2800pci
  19. Abstract: rt2800pci device specific routines.
  20. Supported chipsets: RT2800E & RT2800ED.
  21. */
  22. #include <linux/crc-ccitt.h>
  23. #include <linux/delay.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/eeprom_93cx6.h>
  31. #include "rt2x00.h"
  32. #include "rt2x00pci.h"
  33. #include "rt2x00soc.h"
  34. #include "rt2800pci.h"
  35. #ifdef CONFIG_RT2800PCI_PCI_MODULE
  36. #define CONFIG_RT2800PCI_PCI
  37. #endif
  38. #ifdef CONFIG_RT2800PCI_WISOC_MODULE
  39. #define CONFIG_RT2800PCI_WISOC
  40. #endif
  41. /*
  42. * Allow hardware encryption to be disabled.
  43. */
  44. static int modparam_nohwcrypt = 1;
  45. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  46. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  47. /*
  48. * Register access.
  49. * All access to the CSR registers will go through the methods
  50. * rt2800_register_read and rt2800_register_write.
  51. * BBP and RF register require indirect register access,
  52. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  53. * These indirect registers work with busy bits,
  54. * and we will try maximal REGISTER_BUSY_COUNT times to access
  55. * the register while taking a REGISTER_BUSY_DELAY us delay
  56. * between each attampt. When the busy bit is still set at that time,
  57. * the access attempt is considered to have failed,
  58. * and we will print an error.
  59. * The _lock versions must be used if you already hold the csr_mutex
  60. */
  61. #define WAIT_FOR_BBP(__dev, __reg) \
  62. rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
  63. #define WAIT_FOR_RFCSR(__dev, __reg) \
  64. rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
  65. #define WAIT_FOR_RF(__dev, __reg) \
  66. rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
  67. #define WAIT_FOR_MCU(__dev, __reg) \
  68. rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  69. H2M_MAILBOX_CSR_OWNER, (__reg))
  70. static void rt2800pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  71. const unsigned int word, const u8 value)
  72. {
  73. u32 reg;
  74. mutex_lock(&rt2x00dev->csr_mutex);
  75. /*
  76. * Wait until the BBP becomes available, afterwards we
  77. * can safely write the new data into the register.
  78. */
  79. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  80. reg = 0;
  81. rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
  82. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  83. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  84. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
  85. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  86. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  87. }
  88. mutex_unlock(&rt2x00dev->csr_mutex);
  89. }
  90. static void rt2800pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  91. const unsigned int word, u8 *value)
  92. {
  93. u32 reg;
  94. mutex_lock(&rt2x00dev->csr_mutex);
  95. /*
  96. * Wait until the BBP becomes available, afterwards we
  97. * can safely write the read request into the register.
  98. * After the data has been written, we wait until hardware
  99. * returns the correct value, if at any time the register
  100. * doesn't become available in time, reg will be 0xffffffff
  101. * which means we return 0xff to the caller.
  102. */
  103. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  104. reg = 0;
  105. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  106. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  107. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
  108. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  109. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  110. WAIT_FOR_BBP(rt2x00dev, &reg);
  111. }
  112. *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
  113. mutex_unlock(&rt2x00dev->csr_mutex);
  114. }
  115. static inline void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
  116. const unsigned int word, const u8 value)
  117. {
  118. rt2800pci_bbp_write(rt2x00dev, word, value);
  119. }
  120. static inline void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
  121. const unsigned int word, u8 *value)
  122. {
  123. rt2800pci_bbp_read(rt2x00dev, word, value);
  124. }
  125. static void rt2800pci_rfcsr_write(struct rt2x00_dev *rt2x00dev,
  126. const unsigned int word, const u8 value)
  127. {
  128. u32 reg;
  129. mutex_lock(&rt2x00dev->csr_mutex);
  130. /*
  131. * Wait until the RFCSR becomes available, afterwards we
  132. * can safely write the new data into the register.
  133. */
  134. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  135. reg = 0;
  136. rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
  137. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  138. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
  139. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  140. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  141. }
  142. mutex_unlock(&rt2x00dev->csr_mutex);
  143. }
  144. static void rt2800pci_rfcsr_read(struct rt2x00_dev *rt2x00dev,
  145. const unsigned int word, u8 *value)
  146. {
  147. u32 reg;
  148. mutex_lock(&rt2x00dev->csr_mutex);
  149. /*
  150. * Wait until the RFCSR becomes available, afterwards we
  151. * can safely write the read request into the register.
  152. * After the data has been written, we wait until hardware
  153. * returns the correct value, if at any time the register
  154. * doesn't become available in time, reg will be 0xffffffff
  155. * which means we return 0xff to the caller.
  156. */
  157. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  158. reg = 0;
  159. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  160. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
  161. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  162. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  163. WAIT_FOR_RFCSR(rt2x00dev, &reg);
  164. }
  165. *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
  166. mutex_unlock(&rt2x00dev->csr_mutex);
  167. }
  168. static void rt2800pci_rf_write(struct rt2x00_dev *rt2x00dev,
  169. const unsigned int word, const u32 value)
  170. {
  171. u32 reg;
  172. mutex_lock(&rt2x00dev->csr_mutex);
  173. /*
  174. * Wait until the RF becomes available, afterwards we
  175. * can safely write the new data into the register.
  176. */
  177. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  178. reg = 0;
  179. rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
  180. rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
  181. rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
  182. rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
  183. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
  184. rt2x00_rf_write(rt2x00dev, word, value);
  185. }
  186. mutex_unlock(&rt2x00dev->csr_mutex);
  187. }
  188. static void rt2800pci_mcu_request(struct rt2x00_dev *rt2x00dev,
  189. const u8 command, const u8 token,
  190. const u8 arg0, const u8 arg1)
  191. {
  192. u32 reg;
  193. /*
  194. * RT2880 and RT3052 don't support MCU requests.
  195. */
  196. if (rt2x00_rt(&rt2x00dev->chip, RT2880) ||
  197. rt2x00_rt(&rt2x00dev->chip, RT3052))
  198. return;
  199. mutex_lock(&rt2x00dev->csr_mutex);
  200. /*
  201. * Wait until the MCU becomes available, afterwards we
  202. * can safely write the new data into the register.
  203. */
  204. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  205. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  206. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  207. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  208. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  209. rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
  210. reg = 0;
  211. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  212. rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
  213. }
  214. mutex_unlock(&rt2x00dev->csr_mutex);
  215. }
  216. static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
  217. {
  218. unsigned int i;
  219. u32 reg;
  220. for (i = 0; i < 200; i++) {
  221. rt2800_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
  222. if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
  223. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
  224. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
  225. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
  226. break;
  227. udelay(REGISTER_BUSY_DELAY);
  228. }
  229. if (i == 200)
  230. ERROR(rt2x00dev, "MCU request failed, no response from hardware\n");
  231. rt2800_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  232. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  233. }
  234. #ifdef CONFIG_RT2800PCI_WISOC
  235. static void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  236. {
  237. u32 *base_addr = (u32 *) KSEG1ADDR(0x1F040000); /* XXX for RT3052 */
  238. memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
  239. }
  240. #else
  241. static inline void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  242. {
  243. }
  244. #endif /* CONFIG_RT2800PCI_WISOC */
  245. #ifdef CONFIG_RT2800PCI_PCI
  246. static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  247. {
  248. struct rt2x00_dev *rt2x00dev = eeprom->data;
  249. u32 reg;
  250. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  251. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  252. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  253. eeprom->reg_data_clock =
  254. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  255. eeprom->reg_chip_select =
  256. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  257. }
  258. static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  259. {
  260. struct rt2x00_dev *rt2x00dev = eeprom->data;
  261. u32 reg = 0;
  262. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  263. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  264. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  265. !!eeprom->reg_data_clock);
  266. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  267. !!eeprom->reg_chip_select);
  268. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  269. }
  270. static void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  271. {
  272. struct eeprom_93cx6 eeprom;
  273. u32 reg;
  274. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  275. eeprom.data = rt2x00dev;
  276. eeprom.register_read = rt2800pci_eepromregister_read;
  277. eeprom.register_write = rt2800pci_eepromregister_write;
  278. eeprom.width = !rt2x00_get_field32(reg, E2PROM_CSR_TYPE) ?
  279. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  280. eeprom.reg_data_in = 0;
  281. eeprom.reg_data_out = 0;
  282. eeprom.reg_data_clock = 0;
  283. eeprom.reg_chip_select = 0;
  284. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  285. EEPROM_SIZE / sizeof(u16));
  286. }
  287. static void rt2800pci_efuse_read(struct rt2x00_dev *rt2x00dev,
  288. unsigned int i)
  289. {
  290. u32 reg;
  291. rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
  292. rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
  293. rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
  294. rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
  295. rt2800_register_write(rt2x00dev, EFUSE_CTRL, reg);
  296. /* Wait until the EEPROM has been loaded */
  297. rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
  298. /* Apparently the data is read from end to start */
  299. rt2800_register_read(rt2x00dev, EFUSE_DATA3,
  300. (u32 *)&rt2x00dev->eeprom[i]);
  301. rt2800_register_read(rt2x00dev, EFUSE_DATA2,
  302. (u32 *)&rt2x00dev->eeprom[i + 2]);
  303. rt2800_register_read(rt2x00dev, EFUSE_DATA1,
  304. (u32 *)&rt2x00dev->eeprom[i + 4]);
  305. rt2800_register_read(rt2x00dev, EFUSE_DATA0,
  306. (u32 *)&rt2x00dev->eeprom[i + 6]);
  307. }
  308. static void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  309. {
  310. unsigned int i;
  311. for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
  312. rt2800pci_efuse_read(rt2x00dev, i);
  313. }
  314. #else
  315. static inline void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  316. {
  317. }
  318. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  319. {
  320. }
  321. #endif /* CONFIG_RT2800PCI_PCI */
  322. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  323. static const struct rt2x00debug rt2800pci_rt2x00debug = {
  324. .owner = THIS_MODULE,
  325. .csr = {
  326. .read = rt2800_register_read,
  327. .write = rt2800_register_write,
  328. .flags = RT2X00DEBUGFS_OFFSET,
  329. .word_base = CSR_REG_BASE,
  330. .word_size = sizeof(u32),
  331. .word_count = CSR_REG_SIZE / sizeof(u32),
  332. },
  333. .eeprom = {
  334. .read = rt2x00_eeprom_read,
  335. .write = rt2x00_eeprom_write,
  336. .word_base = EEPROM_BASE,
  337. .word_size = sizeof(u16),
  338. .word_count = EEPROM_SIZE / sizeof(u16),
  339. },
  340. .bbp = {
  341. .read = rt2800_bbp_read,
  342. .write = rt2800_bbp_write,
  343. .word_base = BBP_BASE,
  344. .word_size = sizeof(u8),
  345. .word_count = BBP_SIZE / sizeof(u8),
  346. },
  347. .rf = {
  348. .read = rt2x00_rf_read,
  349. .write = rt2800pci_rf_write,
  350. .word_base = RF_BASE,
  351. .word_size = sizeof(u32),
  352. .word_count = RF_SIZE / sizeof(u32),
  353. },
  354. };
  355. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  356. static int rt2800pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  357. {
  358. u32 reg;
  359. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  360. return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
  361. }
  362. #ifdef CONFIG_RT2X00_LIB_LEDS
  363. static void rt2800pci_brightness_set(struct led_classdev *led_cdev,
  364. enum led_brightness brightness)
  365. {
  366. struct rt2x00_led *led =
  367. container_of(led_cdev, struct rt2x00_led, led_dev);
  368. unsigned int enabled = brightness != LED_OFF;
  369. unsigned int bg_mode =
  370. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  371. unsigned int polarity =
  372. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  373. EEPROM_FREQ_LED_POLARITY);
  374. unsigned int ledmode =
  375. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  376. EEPROM_FREQ_LED_MODE);
  377. if (led->type == LED_TYPE_RADIO) {
  378. rt2800pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  379. enabled ? 0x20 : 0);
  380. } else if (led->type == LED_TYPE_ASSOC) {
  381. rt2800pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  382. enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
  383. } else if (led->type == LED_TYPE_QUALITY) {
  384. /*
  385. * The brightness is divided into 6 levels (0 - 5),
  386. * The specs tell us the following levels:
  387. * 0, 1 ,3, 7, 15, 31
  388. * to determine the level in a simple way we can simply
  389. * work with bitshifting:
  390. * (1 << level) - 1
  391. */
  392. rt2800pci_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  393. (1 << brightness / (LED_FULL / 6)) - 1,
  394. polarity);
  395. }
  396. }
  397. static int rt2800pci_blink_set(struct led_classdev *led_cdev,
  398. unsigned long *delay_on,
  399. unsigned long *delay_off)
  400. {
  401. struct rt2x00_led *led =
  402. container_of(led_cdev, struct rt2x00_led, led_dev);
  403. u32 reg;
  404. rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
  405. rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
  406. rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
  407. rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
  408. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
  409. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 12);
  410. rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
  411. rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
  412. rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
  413. return 0;
  414. }
  415. static void rt2800pci_init_led(struct rt2x00_dev *rt2x00dev,
  416. struct rt2x00_led *led,
  417. enum led_type type)
  418. {
  419. led->rt2x00dev = rt2x00dev;
  420. led->type = type;
  421. led->led_dev.brightness_set = rt2800pci_brightness_set;
  422. led->led_dev.blink_set = rt2800pci_blink_set;
  423. led->flags = LED_INITIALIZED;
  424. }
  425. #endif /* CONFIG_RT2X00_LIB_LEDS */
  426. /*
  427. * Configuration handlers.
  428. */
  429. static void rt2800pci_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
  430. struct rt2x00lib_crypto *crypto,
  431. struct ieee80211_key_conf *key)
  432. {
  433. struct mac_wcid_entry wcid_entry;
  434. struct mac_iveiv_entry iveiv_entry;
  435. u32 offset;
  436. u32 reg;
  437. offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
  438. rt2800_register_read(rt2x00dev, offset, &reg);
  439. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
  440. !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
  441. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
  442. (crypto->cmd == SET_KEY) * crypto->cipher);
  443. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
  444. (crypto->cmd == SET_KEY) * crypto->bssidx);
  445. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
  446. rt2800_register_write(rt2x00dev, offset, reg);
  447. offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
  448. memset(&iveiv_entry, 0, sizeof(iveiv_entry));
  449. if ((crypto->cipher == CIPHER_TKIP) ||
  450. (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
  451. (crypto->cipher == CIPHER_AES))
  452. iveiv_entry.iv[3] |= 0x20;
  453. iveiv_entry.iv[3] |= key->keyidx << 6;
  454. rt2800_register_multiwrite(rt2x00dev, offset,
  455. &iveiv_entry, sizeof(iveiv_entry));
  456. offset = MAC_WCID_ENTRY(key->hw_key_idx);
  457. memset(&wcid_entry, 0, sizeof(wcid_entry));
  458. if (crypto->cmd == SET_KEY)
  459. memcpy(&wcid_entry, crypto->address, ETH_ALEN);
  460. rt2800_register_multiwrite(rt2x00dev, offset,
  461. &wcid_entry, sizeof(wcid_entry));
  462. }
  463. static int rt2800pci_config_shared_key(struct rt2x00_dev *rt2x00dev,
  464. struct rt2x00lib_crypto *crypto,
  465. struct ieee80211_key_conf *key)
  466. {
  467. struct hw_key_entry key_entry;
  468. struct rt2x00_field32 field;
  469. u32 offset;
  470. u32 reg;
  471. if (crypto->cmd == SET_KEY) {
  472. key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
  473. memcpy(key_entry.key, crypto->key,
  474. sizeof(key_entry.key));
  475. memcpy(key_entry.tx_mic, crypto->tx_mic,
  476. sizeof(key_entry.tx_mic));
  477. memcpy(key_entry.rx_mic, crypto->rx_mic,
  478. sizeof(key_entry.rx_mic));
  479. offset = SHARED_KEY_ENTRY(key->hw_key_idx);
  480. rt2800_register_multiwrite(rt2x00dev, offset,
  481. &key_entry, sizeof(key_entry));
  482. }
  483. /*
  484. * The cipher types are stored over multiple registers
  485. * starting with SHARED_KEY_MODE_BASE each word will have
  486. * 32 bits and contains the cipher types for 2 bssidx each.
  487. * Using the correct defines correctly will cause overhead,
  488. * so just calculate the correct offset.
  489. */
  490. field.bit_offset = 4 * (key->hw_key_idx % 8);
  491. field.bit_mask = 0x7 << field.bit_offset;
  492. offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
  493. rt2800_register_read(rt2x00dev, offset, &reg);
  494. rt2x00_set_field32(&reg, field,
  495. (crypto->cmd == SET_KEY) * crypto->cipher);
  496. rt2800_register_write(rt2x00dev, offset, reg);
  497. /*
  498. * Update WCID information
  499. */
  500. rt2800pci_config_wcid_attr(rt2x00dev, crypto, key);
  501. return 0;
  502. }
  503. static int rt2800pci_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  504. struct rt2x00lib_crypto *crypto,
  505. struct ieee80211_key_conf *key)
  506. {
  507. struct hw_key_entry key_entry;
  508. u32 offset;
  509. if (crypto->cmd == SET_KEY) {
  510. /*
  511. * 1 pairwise key is possible per AID, this means that the AID
  512. * equals our hw_key_idx. Make sure the WCID starts _after_ the
  513. * last possible shared key entry.
  514. */
  515. if (crypto->aid > (256 - 32))
  516. return -ENOSPC;
  517. key->hw_key_idx = 32 + crypto->aid;
  518. memcpy(key_entry.key, crypto->key,
  519. sizeof(key_entry.key));
  520. memcpy(key_entry.tx_mic, crypto->tx_mic,
  521. sizeof(key_entry.tx_mic));
  522. memcpy(key_entry.rx_mic, crypto->rx_mic,
  523. sizeof(key_entry.rx_mic));
  524. offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  525. rt2800_register_multiwrite(rt2x00dev, offset,
  526. &key_entry, sizeof(key_entry));
  527. }
  528. /*
  529. * Update WCID information
  530. */
  531. rt2800pci_config_wcid_attr(rt2x00dev, crypto, key);
  532. return 0;
  533. }
  534. static void rt2800pci_config_filter(struct rt2x00_dev *rt2x00dev,
  535. const unsigned int filter_flags)
  536. {
  537. u32 reg;
  538. /*
  539. * Start configuration steps.
  540. * Note that the version error will always be dropped
  541. * and broadcast frames will always be accepted since
  542. * there is no filter for it at this time.
  543. */
  544. rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
  545. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
  546. !(filter_flags & FIF_FCSFAIL));
  547. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
  548. !(filter_flags & FIF_PLCPFAIL));
  549. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
  550. !(filter_flags & FIF_PROMISC_IN_BSS));
  551. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
  552. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
  553. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
  554. !(filter_flags & FIF_ALLMULTI));
  555. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
  556. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
  557. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
  558. !(filter_flags & FIF_CONTROL));
  559. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
  560. !(filter_flags & FIF_CONTROL));
  561. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
  562. !(filter_flags & FIF_CONTROL));
  563. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
  564. !(filter_flags & FIF_CONTROL));
  565. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
  566. !(filter_flags & FIF_CONTROL));
  567. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
  568. !(filter_flags & FIF_PSPOLL));
  569. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
  570. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
  571. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
  572. !(filter_flags & FIF_CONTROL));
  573. rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
  574. }
  575. static void rt2800pci_config_intf(struct rt2x00_dev *rt2x00dev,
  576. struct rt2x00_intf *intf,
  577. struct rt2x00intf_conf *conf,
  578. const unsigned int flags)
  579. {
  580. unsigned int beacon_base;
  581. u32 reg;
  582. if (flags & CONFIG_UPDATE_TYPE) {
  583. /*
  584. * Clear current synchronisation setup.
  585. * For the Beacon base registers we only need to clear
  586. * the first byte since that byte contains the VALID and OWNER
  587. * bits which (when set to 0) will invalidate the entire beacon.
  588. */
  589. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  590. rt2800_register_write(rt2x00dev, beacon_base, 0);
  591. /*
  592. * Enable synchronisation.
  593. */
  594. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  595. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  596. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
  597. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  598. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  599. }
  600. if (flags & CONFIG_UPDATE_MAC) {
  601. reg = le32_to_cpu(conf->mac[1]);
  602. rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
  603. conf->mac[1] = cpu_to_le32(reg);
  604. rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
  605. conf->mac, sizeof(conf->mac));
  606. }
  607. if (flags & CONFIG_UPDATE_BSSID) {
  608. reg = le32_to_cpu(conf->bssid[1]);
  609. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
  610. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
  611. conf->bssid[1] = cpu_to_le32(reg);
  612. rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
  613. conf->bssid, sizeof(conf->bssid));
  614. }
  615. }
  616. static void rt2800pci_config_erp(struct rt2x00_dev *rt2x00dev,
  617. struct rt2x00lib_erp *erp)
  618. {
  619. u32 reg;
  620. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  621. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 0x20);
  622. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  623. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  624. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
  625. !!erp->short_preamble);
  626. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
  627. !!erp->short_preamble);
  628. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  629. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  630. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
  631. erp->cts_protection ? 2 : 0);
  632. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  633. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
  634. erp->basic_rates);
  635. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  636. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  637. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
  638. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
  639. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  640. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  641. rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, erp->sifs);
  642. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, erp->sifs);
  643. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
  644. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
  645. rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
  646. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  647. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  648. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  649. erp->beacon_int * 16);
  650. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  651. }
  652. static void rt2800pci_config_ant(struct rt2x00_dev *rt2x00dev,
  653. struct antenna_setup *ant)
  654. {
  655. u8 r1;
  656. u8 r3;
  657. rt2800_bbp_read(rt2x00dev, 1, &r1);
  658. rt2800_bbp_read(rt2x00dev, 3, &r3);
  659. /*
  660. * Configure the TX antenna.
  661. */
  662. switch ((int)ant->tx) {
  663. case 1:
  664. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  665. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  666. break;
  667. case 2:
  668. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
  669. break;
  670. case 3:
  671. /* Do nothing */
  672. break;
  673. }
  674. /*
  675. * Configure the RX antenna.
  676. */
  677. switch ((int)ant->rx) {
  678. case 1:
  679. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  680. break;
  681. case 2:
  682. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
  683. break;
  684. case 3:
  685. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
  686. break;
  687. }
  688. rt2800_bbp_write(rt2x00dev, 3, r3);
  689. rt2800_bbp_write(rt2x00dev, 1, r1);
  690. }
  691. static void rt2800pci_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  692. struct rt2x00lib_conf *libconf)
  693. {
  694. u16 eeprom;
  695. short lna_gain;
  696. if (libconf->rf.channel <= 14) {
  697. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  698. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
  699. } else if (libconf->rf.channel <= 64) {
  700. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  701. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
  702. } else if (libconf->rf.channel <= 128) {
  703. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  704. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
  705. } else {
  706. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  707. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
  708. }
  709. rt2x00dev->lna_gain = lna_gain;
  710. }
  711. static void rt2800pci_config_channel_rt2x(struct rt2x00_dev *rt2x00dev,
  712. struct ieee80211_conf *conf,
  713. struct rf_channel *rf,
  714. struct channel_info *info)
  715. {
  716. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  717. if (rt2x00dev->default_ant.tx == 1)
  718. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
  719. if (rt2x00dev->default_ant.rx == 1) {
  720. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
  721. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  722. } else if (rt2x00dev->default_ant.rx == 2)
  723. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  724. if (rf->channel > 14) {
  725. /*
  726. * When TX power is below 0, we should increase it by 7 to
  727. * make it a positive value (Minumum value is -7).
  728. * However this means that values between 0 and 7 have
  729. * double meaning, and we should set a 7DBm boost flag.
  730. */
  731. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
  732. (info->tx_power1 >= 0));
  733. if (info->tx_power1 < 0)
  734. info->tx_power1 += 7;
  735. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
  736. TXPOWER_A_TO_DEV(info->tx_power1));
  737. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
  738. (info->tx_power2 >= 0));
  739. if (info->tx_power2 < 0)
  740. info->tx_power2 += 7;
  741. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
  742. TXPOWER_A_TO_DEV(info->tx_power2));
  743. } else {
  744. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
  745. TXPOWER_G_TO_DEV(info->tx_power1));
  746. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
  747. TXPOWER_G_TO_DEV(info->tx_power2));
  748. }
  749. rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
  750. rt2800pci_rf_write(rt2x00dev, 1, rf->rf1);
  751. rt2800pci_rf_write(rt2x00dev, 2, rf->rf2);
  752. rt2800pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  753. rt2800pci_rf_write(rt2x00dev, 4, rf->rf4);
  754. udelay(200);
  755. rt2800pci_rf_write(rt2x00dev, 1, rf->rf1);
  756. rt2800pci_rf_write(rt2x00dev, 2, rf->rf2);
  757. rt2800pci_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  758. rt2800pci_rf_write(rt2x00dev, 4, rf->rf4);
  759. udelay(200);
  760. rt2800pci_rf_write(rt2x00dev, 1, rf->rf1);
  761. rt2800pci_rf_write(rt2x00dev, 2, rf->rf2);
  762. rt2800pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  763. rt2800pci_rf_write(rt2x00dev, 4, rf->rf4);
  764. }
  765. static void rt2800pci_config_channel_rt3x(struct rt2x00_dev *rt2x00dev,
  766. struct ieee80211_conf *conf,
  767. struct rf_channel *rf,
  768. struct channel_info *info)
  769. {
  770. u8 rfcsr;
  771. rt2800pci_rfcsr_write(rt2x00dev, 2, rf->rf1);
  772. rt2800pci_rfcsr_write(rt2x00dev, 2, rf->rf3);
  773. rt2800pci_rfcsr_read(rt2x00dev, 6, &rfcsr);
  774. rt2x00_set_field8(&rfcsr, RFCSR6_R, rf->rf2);
  775. rt2800pci_rfcsr_write(rt2x00dev, 6, rfcsr);
  776. rt2800pci_rfcsr_read(rt2x00dev, 12, &rfcsr);
  777. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
  778. TXPOWER_G_TO_DEV(info->tx_power1));
  779. rt2800pci_rfcsr_write(rt2x00dev, 12, rfcsr);
  780. rt2800pci_rfcsr_read(rt2x00dev, 23, &rfcsr);
  781. rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
  782. rt2800pci_rfcsr_write(rt2x00dev, 23, rfcsr);
  783. rt2800pci_rfcsr_write(rt2x00dev, 24,
  784. rt2x00dev->calibration[conf_is_ht40(conf)]);
  785. rt2800pci_rfcsr_read(rt2x00dev, 23, &rfcsr);
  786. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  787. rt2800pci_rfcsr_write(rt2x00dev, 23, rfcsr);
  788. }
  789. static void rt2800pci_config_channel(struct rt2x00_dev *rt2x00dev,
  790. struct ieee80211_conf *conf,
  791. struct rf_channel *rf,
  792. struct channel_info *info)
  793. {
  794. u32 reg;
  795. unsigned int tx_pin;
  796. u8 bbp;
  797. if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
  798. rt2800pci_config_channel_rt2x(rt2x00dev, conf, rf, info);
  799. else
  800. rt2800pci_config_channel_rt3x(rt2x00dev, conf, rf, info);
  801. /*
  802. * Change BBP settings
  803. */
  804. rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
  805. rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
  806. rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
  807. rt2800_bbp_write(rt2x00dev, 86, 0);
  808. if (rf->channel <= 14) {
  809. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  810. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  811. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  812. } else {
  813. rt2800_bbp_write(rt2x00dev, 82, 0x84);
  814. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  815. }
  816. } else {
  817. rt2800_bbp_write(rt2x00dev, 82, 0xf2);
  818. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  819. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  820. else
  821. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  822. }
  823. rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
  824. rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_PLUS, conf_is_ht40_plus(conf));
  825. rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
  826. rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
  827. rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
  828. tx_pin = 0;
  829. /* Turn on unused PA or LNA when not using 1T or 1R */
  830. if (rt2x00dev->default_ant.tx != 1) {
  831. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
  832. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
  833. }
  834. /* Turn on unused PA or LNA when not using 1T or 1R */
  835. if (rt2x00dev->default_ant.rx != 1) {
  836. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
  837. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
  838. }
  839. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
  840. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
  841. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
  842. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
  843. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
  844. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
  845. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  846. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  847. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
  848. rt2800_bbp_write(rt2x00dev, 4, bbp);
  849. rt2800_bbp_read(rt2x00dev, 3, &bbp);
  850. rt2x00_set_field8(&bbp, BBP3_HT40_PLUS, conf_is_ht40_plus(conf));
  851. rt2800_bbp_write(rt2x00dev, 3, bbp);
  852. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
  853. if (conf_is_ht40(conf)) {
  854. rt2800_bbp_write(rt2x00dev, 69, 0x1a);
  855. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  856. rt2800_bbp_write(rt2x00dev, 73, 0x16);
  857. } else {
  858. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  859. rt2800_bbp_write(rt2x00dev, 70, 0x08);
  860. rt2800_bbp_write(rt2x00dev, 73, 0x11);
  861. }
  862. }
  863. msleep(1);
  864. }
  865. static void rt2800pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  866. const int txpower)
  867. {
  868. u32 reg;
  869. u32 value = TXPOWER_G_TO_DEV(txpower);
  870. u8 r1;
  871. rt2800_bbp_read(rt2x00dev, 1, &r1);
  872. rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
  873. rt2800_bbp_write(rt2x00dev, 1, r1);
  874. rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
  875. rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
  876. rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
  877. rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
  878. rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
  879. rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
  880. rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
  881. rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
  882. rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
  883. rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
  884. rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
  885. rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
  886. rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
  887. rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
  888. rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
  889. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
  890. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
  891. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
  892. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
  893. rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
  894. rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
  895. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
  896. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
  897. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
  898. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
  899. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
  900. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
  901. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
  902. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
  903. rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
  904. rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
  905. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
  906. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
  907. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
  908. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
  909. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
  910. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
  911. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
  912. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
  913. rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
  914. rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
  915. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
  916. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
  917. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
  918. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
  919. rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
  920. }
  921. static void rt2800pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  922. struct rt2x00lib_conf *libconf)
  923. {
  924. u32 reg;
  925. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  926. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
  927. libconf->conf->short_frame_max_tx_count);
  928. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
  929. libconf->conf->long_frame_max_tx_count);
  930. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
  931. rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
  932. rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
  933. rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
  934. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  935. }
  936. static void rt2800pci_config_ps(struct rt2x00_dev *rt2x00dev,
  937. struct rt2x00lib_conf *libconf)
  938. {
  939. enum dev_state state =
  940. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  941. STATE_SLEEP : STATE_AWAKE;
  942. u32 reg;
  943. if (state == STATE_SLEEP) {
  944. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
  945. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  946. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
  947. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
  948. libconf->conf->listen_interval - 1);
  949. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
  950. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  951. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  952. } else {
  953. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  954. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  955. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
  956. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
  957. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
  958. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  959. }
  960. }
  961. static void rt2800pci_config(struct rt2x00_dev *rt2x00dev,
  962. struct rt2x00lib_conf *libconf,
  963. const unsigned int flags)
  964. {
  965. /* Always recalculate LNA gain before changing configuration */
  966. rt2800pci_config_lna_gain(rt2x00dev, libconf);
  967. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  968. rt2800pci_config_channel(rt2x00dev, libconf->conf,
  969. &libconf->rf, &libconf->channel);
  970. if (flags & IEEE80211_CONF_CHANGE_POWER)
  971. rt2800pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  972. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  973. rt2800pci_config_retry_limit(rt2x00dev, libconf);
  974. if (flags & IEEE80211_CONF_CHANGE_PS)
  975. rt2800pci_config_ps(rt2x00dev, libconf);
  976. }
  977. /*
  978. * Link tuning
  979. */
  980. static void rt2800pci_link_stats(struct rt2x00_dev *rt2x00dev,
  981. struct link_qual *qual)
  982. {
  983. u32 reg;
  984. /*
  985. * Update FCS error count from register.
  986. */
  987. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  988. qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
  989. }
  990. static u8 rt2800pci_get_default_vgc(struct rt2x00_dev *rt2x00dev)
  991. {
  992. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ)
  993. return 0x2e + rt2x00dev->lna_gain;
  994. if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  995. return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
  996. else
  997. return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
  998. }
  999. static inline void rt2800pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  1000. struct link_qual *qual, u8 vgc_level)
  1001. {
  1002. if (qual->vgc_level != vgc_level) {
  1003. rt2800_bbp_write(rt2x00dev, 66, vgc_level);
  1004. qual->vgc_level = vgc_level;
  1005. qual->vgc_level_reg = vgc_level;
  1006. }
  1007. }
  1008. static void rt2800pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  1009. struct link_qual *qual)
  1010. {
  1011. rt2800pci_set_vgc(rt2x00dev, qual,
  1012. rt2800pci_get_default_vgc(rt2x00dev));
  1013. }
  1014. static void rt2800pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  1015. struct link_qual *qual, const u32 count)
  1016. {
  1017. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION)
  1018. return;
  1019. /*
  1020. * When RSSI is better then -80 increase VGC level with 0x10
  1021. */
  1022. rt2800pci_set_vgc(rt2x00dev, qual,
  1023. rt2800pci_get_default_vgc(rt2x00dev) +
  1024. ((qual->rssi > -80) * 0x10));
  1025. }
  1026. /*
  1027. * Firmware functions
  1028. */
  1029. static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  1030. {
  1031. return FIRMWARE_RT2860;
  1032. }
  1033. static int rt2800pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  1034. const u8 *data, const size_t len)
  1035. {
  1036. u16 fw_crc;
  1037. u16 crc;
  1038. /*
  1039. * Only support 8kb firmware files.
  1040. */
  1041. if (len != 8192)
  1042. return FW_BAD_LENGTH;
  1043. /*
  1044. * The last 2 bytes in the firmware array are the crc checksum itself,
  1045. * this means that we should never pass those 2 bytes to the crc
  1046. * algorithm.
  1047. */
  1048. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  1049. /*
  1050. * Use the crc ccitt algorithm.
  1051. * This will return the same value as the legacy driver which
  1052. * used bit ordering reversion on the both the firmware bytes
  1053. * before input input as well as on the final output.
  1054. * Obviously using crc ccitt directly is much more efficient.
  1055. */
  1056. crc = crc_ccitt(~0, data, len - 2);
  1057. /*
  1058. * There is a small difference between the crc-itu-t + bitrev and
  1059. * the crc-ccitt crc calculation. In the latter method the 2 bytes
  1060. * will be swapped, use swab16 to convert the crc to the correct
  1061. * value.
  1062. */
  1063. crc = swab16(crc);
  1064. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  1065. }
  1066. static int rt2800pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  1067. const u8 *data, const size_t len)
  1068. {
  1069. unsigned int i;
  1070. u32 reg;
  1071. /*
  1072. * Wait for stable hardware.
  1073. */
  1074. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1075. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  1076. if (reg && reg != ~0)
  1077. break;
  1078. msleep(1);
  1079. }
  1080. if (i == REGISTER_BUSY_COUNT) {
  1081. ERROR(rt2x00dev, "Unstable hardware.\n");
  1082. return -EBUSY;
  1083. }
  1084. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  1085. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
  1086. /*
  1087. * Disable DMA, will be reenabled later when enabling
  1088. * the radio.
  1089. */
  1090. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1091. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1092. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1093. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1094. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1095. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1096. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1097. /*
  1098. * enable Host program ram write selection
  1099. */
  1100. reg = 0;
  1101. rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
  1102. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
  1103. /*
  1104. * Write firmware to device.
  1105. */
  1106. rt2800_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  1107. data, len);
  1108. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
  1109. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
  1110. /*
  1111. * Wait for device to stabilize.
  1112. */
  1113. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1114. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  1115. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  1116. break;
  1117. msleep(1);
  1118. }
  1119. if (i == REGISTER_BUSY_COUNT) {
  1120. ERROR(rt2x00dev, "PBF system register not ready.\n");
  1121. return -EBUSY;
  1122. }
  1123. /*
  1124. * Disable interrupts
  1125. */
  1126. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  1127. /*
  1128. * Initialize BBP R/W access agent
  1129. */
  1130. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  1131. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1132. return 0;
  1133. }
  1134. /*
  1135. * Initialization functions.
  1136. */
  1137. static bool rt2800pci_get_entry_state(struct queue_entry *entry)
  1138. {
  1139. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1140. u32 word;
  1141. if (entry->queue->qid == QID_RX) {
  1142. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1143. return (!rt2x00_get_field32(word, RXD_W1_DMA_DONE));
  1144. } else {
  1145. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1146. return (!rt2x00_get_field32(word, TXD_W1_DMA_DONE));
  1147. }
  1148. }
  1149. static void rt2800pci_clear_entry(struct queue_entry *entry)
  1150. {
  1151. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1152. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1153. u32 word;
  1154. if (entry->queue->qid == QID_RX) {
  1155. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1156. rt2x00_set_field32(&word, RXD_W0_SDP0, skbdesc->skb_dma);
  1157. rt2x00_desc_write(entry_priv->desc, 0, word);
  1158. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1159. rt2x00_set_field32(&word, RXD_W1_DMA_DONE, 0);
  1160. rt2x00_desc_write(entry_priv->desc, 1, word);
  1161. } else {
  1162. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1163. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 1);
  1164. rt2x00_desc_write(entry_priv->desc, 1, word);
  1165. }
  1166. }
  1167. static int rt2800pci_init_queues(struct rt2x00_dev *rt2x00dev)
  1168. {
  1169. struct queue_entry_priv_pci *entry_priv;
  1170. u32 reg;
  1171. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  1172. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  1173. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  1174. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  1175. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  1176. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  1177. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  1178. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  1179. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  1180. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  1181. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  1182. /*
  1183. * Initialize registers.
  1184. */
  1185. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  1186. rt2800_register_write(rt2x00dev, TX_BASE_PTR0, entry_priv->desc_dma);
  1187. rt2800_register_write(rt2x00dev, TX_MAX_CNT0, rt2x00dev->tx[0].limit);
  1188. rt2800_register_write(rt2x00dev, TX_CTX_IDX0, 0);
  1189. rt2800_register_write(rt2x00dev, TX_DTX_IDX0, 0);
  1190. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  1191. rt2800_register_write(rt2x00dev, TX_BASE_PTR1, entry_priv->desc_dma);
  1192. rt2800_register_write(rt2x00dev, TX_MAX_CNT1, rt2x00dev->tx[1].limit);
  1193. rt2800_register_write(rt2x00dev, TX_CTX_IDX1, 0);
  1194. rt2800_register_write(rt2x00dev, TX_DTX_IDX1, 0);
  1195. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  1196. rt2800_register_write(rt2x00dev, TX_BASE_PTR2, entry_priv->desc_dma);
  1197. rt2800_register_write(rt2x00dev, TX_MAX_CNT2, rt2x00dev->tx[2].limit);
  1198. rt2800_register_write(rt2x00dev, TX_CTX_IDX2, 0);
  1199. rt2800_register_write(rt2x00dev, TX_DTX_IDX2, 0);
  1200. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  1201. rt2800_register_write(rt2x00dev, TX_BASE_PTR3, entry_priv->desc_dma);
  1202. rt2800_register_write(rt2x00dev, TX_MAX_CNT3, rt2x00dev->tx[3].limit);
  1203. rt2800_register_write(rt2x00dev, TX_CTX_IDX3, 0);
  1204. rt2800_register_write(rt2x00dev, TX_DTX_IDX3, 0);
  1205. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  1206. rt2800_register_write(rt2x00dev, RX_BASE_PTR, entry_priv->desc_dma);
  1207. rt2800_register_write(rt2x00dev, RX_MAX_CNT, rt2x00dev->rx[0].limit);
  1208. rt2800_register_write(rt2x00dev, RX_CRX_IDX, rt2x00dev->rx[0].limit - 1);
  1209. rt2800_register_write(rt2x00dev, RX_DRX_IDX, 0);
  1210. /*
  1211. * Enable global DMA configuration
  1212. */
  1213. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1214. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1215. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1216. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1217. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1218. rt2800_register_write(rt2x00dev, DELAY_INT_CFG, 0);
  1219. return 0;
  1220. }
  1221. static int rt2800pci_init_registers(struct rt2x00_dev *rt2x00dev)
  1222. {
  1223. u32 reg;
  1224. unsigned int i;
  1225. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  1226. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1227. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
  1228. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
  1229. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1230. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  1231. rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
  1232. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
  1233. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
  1234. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
  1235. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
  1236. rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
  1237. rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
  1238. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
  1239. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
  1240. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
  1241. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
  1242. rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
  1243. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
  1244. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  1245. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  1246. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1247. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
  1248. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
  1249. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
  1250. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
  1251. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  1252. rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
  1253. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1254. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
  1255. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  1256. rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
  1257. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
  1258. rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
  1259. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
  1260. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
  1261. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
  1262. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
  1263. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
  1264. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
  1265. rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
  1266. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  1267. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
  1268. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
  1269. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  1270. rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
  1271. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
  1272. if (rt2x00_rev(&rt2x00dev->chip) >= RT2880E_VERSION &&
  1273. rt2x00_rev(&rt2x00dev->chip) < RT3070_VERSION)
  1274. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
  1275. else
  1276. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
  1277. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
  1278. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
  1279. rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
  1280. rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
  1281. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  1282. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
  1283. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
  1284. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
  1285. rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
  1286. rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
  1287. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  1288. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  1289. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 8);
  1290. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
  1291. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
  1292. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1293. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1294. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1295. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1296. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1297. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1298. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  1299. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  1300. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 8);
  1301. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
  1302. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
  1303. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1304. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1305. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1306. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1307. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1308. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1309. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  1310. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  1311. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
  1312. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
  1313. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
  1314. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1315. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1316. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1317. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1318. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1319. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1320. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  1321. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  1322. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
  1323. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
  1324. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
  1325. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1326. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1327. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1328. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1329. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1330. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1331. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  1332. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  1333. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
  1334. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
  1335. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
  1336. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1337. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1338. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1339. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1340. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1341. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1342. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  1343. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  1344. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
  1345. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
  1346. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
  1347. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1348. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1349. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1350. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1351. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1352. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1353. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  1354. rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
  1355. rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
  1356. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  1357. rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
  1358. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
  1359. IEEE80211_MAX_RTS_THRESHOLD);
  1360. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
  1361. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  1362. rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
  1363. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  1364. /*
  1365. * ASIC will keep garbage value after boot, clear encryption keys.
  1366. */
  1367. for (i = 0; i < 4; i++)
  1368. rt2800_register_write(rt2x00dev,
  1369. SHARED_KEY_MODE_ENTRY(i), 0);
  1370. for (i = 0; i < 256; i++) {
  1371. u32 wcid[2] = { 0xffffffff, 0x00ffffff };
  1372. rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
  1373. wcid, sizeof(wcid));
  1374. rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
  1375. rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
  1376. }
  1377. /*
  1378. * Clear all beacons
  1379. * For the Beacon base registers we only need to clear
  1380. * the first byte since that byte contains the VALID and OWNER
  1381. * bits which (when set to 0) will invalidate the entire beacon.
  1382. */
  1383. rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1384. rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1385. rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1386. rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1387. rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
  1388. rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
  1389. rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
  1390. rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
  1391. rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
  1392. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
  1393. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
  1394. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
  1395. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
  1396. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
  1397. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
  1398. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
  1399. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
  1400. rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
  1401. rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
  1402. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
  1403. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
  1404. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
  1405. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
  1406. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
  1407. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
  1408. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
  1409. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
  1410. rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
  1411. rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
  1412. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
  1413. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
  1414. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
  1415. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
  1416. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
  1417. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
  1418. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
  1419. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
  1420. rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
  1421. rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
  1422. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
  1423. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
  1424. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
  1425. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
  1426. rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
  1427. /*
  1428. * We must clear the error counters.
  1429. * These registers are cleared on read,
  1430. * so we may pass a useless variable to store the value.
  1431. */
  1432. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  1433. rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
  1434. rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
  1435. rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
  1436. rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
  1437. rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
  1438. return 0;
  1439. }
  1440. static int rt2800pci_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
  1441. {
  1442. unsigned int i;
  1443. u32 reg;
  1444. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1445. rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
  1446. if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
  1447. return 0;
  1448. udelay(REGISTER_BUSY_DELAY);
  1449. }
  1450. ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
  1451. return -EACCES;
  1452. }
  1453. static int rt2800pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  1454. {
  1455. unsigned int i;
  1456. u8 value;
  1457. /*
  1458. * BBP was enabled after firmware was loaded,
  1459. * but we need to reactivate it now.
  1460. */
  1461. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  1462. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1463. msleep(1);
  1464. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1465. rt2800_bbp_read(rt2x00dev, 0, &value);
  1466. if ((value != 0xff) && (value != 0x00))
  1467. return 0;
  1468. udelay(REGISTER_BUSY_DELAY);
  1469. }
  1470. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1471. return -EACCES;
  1472. }
  1473. static int rt2800pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1474. {
  1475. unsigned int i;
  1476. u16 eeprom;
  1477. u8 reg_id;
  1478. u8 value;
  1479. if (unlikely(rt2800pci_wait_bbp_rf_ready(rt2x00dev) ||
  1480. rt2800pci_wait_bbp_ready(rt2x00dev)))
  1481. return -EACCES;
  1482. rt2800_bbp_write(rt2x00dev, 65, 0x2c);
  1483. rt2800_bbp_write(rt2x00dev, 66, 0x38);
  1484. rt2800_bbp_write(rt2x00dev, 69, 0x12);
  1485. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  1486. rt2800_bbp_write(rt2x00dev, 73, 0x10);
  1487. rt2800_bbp_write(rt2x00dev, 81, 0x37);
  1488. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  1489. rt2800_bbp_write(rt2x00dev, 83, 0x6a);
  1490. rt2800_bbp_write(rt2x00dev, 84, 0x99);
  1491. rt2800_bbp_write(rt2x00dev, 86, 0x00);
  1492. rt2800_bbp_write(rt2x00dev, 91, 0x04);
  1493. rt2800_bbp_write(rt2x00dev, 92, 0x00);
  1494. rt2800_bbp_write(rt2x00dev, 103, 0x00);
  1495. rt2800_bbp_write(rt2x00dev, 105, 0x05);
  1496. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
  1497. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  1498. rt2800_bbp_write(rt2x00dev, 73, 0x12);
  1499. }
  1500. if (rt2x00_rev(&rt2x00dev->chip) > RT2860D_VERSION)
  1501. rt2800_bbp_write(rt2x00dev, 84, 0x19);
  1502. if (rt2x00_rt(&rt2x00dev->chip, RT3052)) {
  1503. rt2800_bbp_write(rt2x00dev, 31, 0x08);
  1504. rt2800_bbp_write(rt2x00dev, 78, 0x0e);
  1505. rt2800_bbp_write(rt2x00dev, 80, 0x08);
  1506. }
  1507. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1508. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1509. if (eeprom != 0xffff && eeprom != 0x0000) {
  1510. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1511. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1512. rt2800_bbp_write(rt2x00dev, reg_id, value);
  1513. }
  1514. }
  1515. return 0;
  1516. }
  1517. static u8 rt2800pci_init_rx_filter(struct rt2x00_dev *rt2x00dev,
  1518. bool bw40, u8 rfcsr24, u8 filter_target)
  1519. {
  1520. unsigned int i;
  1521. u8 bbp;
  1522. u8 rfcsr;
  1523. u8 passband;
  1524. u8 stopband;
  1525. u8 overtuned = 0;
  1526. rt2800pci_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1527. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1528. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
  1529. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1530. rt2800pci_rfcsr_read(rt2x00dev, 22, &rfcsr);
  1531. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
  1532. rt2800pci_rfcsr_write(rt2x00dev, 22, rfcsr);
  1533. /*
  1534. * Set power & frequency of passband test tone
  1535. */
  1536. rt2800_bbp_write(rt2x00dev, 24, 0);
  1537. for (i = 0; i < 100; i++) {
  1538. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  1539. msleep(1);
  1540. rt2800_bbp_read(rt2x00dev, 55, &passband);
  1541. if (passband)
  1542. break;
  1543. }
  1544. /*
  1545. * Set power & frequency of stopband test tone
  1546. */
  1547. rt2800_bbp_write(rt2x00dev, 24, 0x06);
  1548. for (i = 0; i < 100; i++) {
  1549. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  1550. msleep(1);
  1551. rt2800_bbp_read(rt2x00dev, 55, &stopband);
  1552. if ((passband - stopband) <= filter_target) {
  1553. rfcsr24++;
  1554. overtuned += ((passband - stopband) == filter_target);
  1555. } else
  1556. break;
  1557. rt2800pci_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1558. }
  1559. rfcsr24 -= !!overtuned;
  1560. rt2800pci_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1561. return rfcsr24;
  1562. }
  1563. static int rt2800pci_init_rfcsr(struct rt2x00_dev *rt2x00dev)
  1564. {
  1565. u8 rfcsr;
  1566. u8 bbp;
  1567. if (!rt2x00_rf(&rt2x00dev->chip, RF3020) &&
  1568. !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
  1569. !rt2x00_rf(&rt2x00dev->chip, RF3022))
  1570. return 0;
  1571. /*
  1572. * Init RF calibration.
  1573. */
  1574. rt2800pci_rfcsr_read(rt2x00dev, 30, &rfcsr);
  1575. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  1576. rt2800pci_rfcsr_write(rt2x00dev, 30, rfcsr);
  1577. msleep(1);
  1578. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  1579. rt2800pci_rfcsr_write(rt2x00dev, 30, rfcsr);
  1580. rt2800pci_rfcsr_write(rt2x00dev, 0, 0x50);
  1581. rt2800pci_rfcsr_write(rt2x00dev, 1, 0x01);
  1582. rt2800pci_rfcsr_write(rt2x00dev, 2, 0xf7);
  1583. rt2800pci_rfcsr_write(rt2x00dev, 3, 0x75);
  1584. rt2800pci_rfcsr_write(rt2x00dev, 4, 0x40);
  1585. rt2800pci_rfcsr_write(rt2x00dev, 5, 0x03);
  1586. rt2800pci_rfcsr_write(rt2x00dev, 6, 0x02);
  1587. rt2800pci_rfcsr_write(rt2x00dev, 7, 0x50);
  1588. rt2800pci_rfcsr_write(rt2x00dev, 8, 0x39);
  1589. rt2800pci_rfcsr_write(rt2x00dev, 9, 0x0f);
  1590. rt2800pci_rfcsr_write(rt2x00dev, 10, 0x60);
  1591. rt2800pci_rfcsr_write(rt2x00dev, 11, 0x21);
  1592. rt2800pci_rfcsr_write(rt2x00dev, 12, 0x75);
  1593. rt2800pci_rfcsr_write(rt2x00dev, 13, 0x75);
  1594. rt2800pci_rfcsr_write(rt2x00dev, 14, 0x90);
  1595. rt2800pci_rfcsr_write(rt2x00dev, 15, 0x58);
  1596. rt2800pci_rfcsr_write(rt2x00dev, 16, 0xb3);
  1597. rt2800pci_rfcsr_write(rt2x00dev, 17, 0x92);
  1598. rt2800pci_rfcsr_write(rt2x00dev, 18, 0x2c);
  1599. rt2800pci_rfcsr_write(rt2x00dev, 19, 0x02);
  1600. rt2800pci_rfcsr_write(rt2x00dev, 20, 0xba);
  1601. rt2800pci_rfcsr_write(rt2x00dev, 21, 0xdb);
  1602. rt2800pci_rfcsr_write(rt2x00dev, 22, 0x00);
  1603. rt2800pci_rfcsr_write(rt2x00dev, 23, 0x31);
  1604. rt2800pci_rfcsr_write(rt2x00dev, 24, 0x08);
  1605. rt2800pci_rfcsr_write(rt2x00dev, 25, 0x01);
  1606. rt2800pci_rfcsr_write(rt2x00dev, 26, 0x25);
  1607. rt2800pci_rfcsr_write(rt2x00dev, 27, 0x23);
  1608. rt2800pci_rfcsr_write(rt2x00dev, 28, 0x13);
  1609. rt2800pci_rfcsr_write(rt2x00dev, 29, 0x83);
  1610. /*
  1611. * Set RX Filter calibration for 20MHz and 40MHz
  1612. */
  1613. rt2x00dev->calibration[0] =
  1614. rt2800pci_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
  1615. rt2x00dev->calibration[1] =
  1616. rt2800pci_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
  1617. /*
  1618. * Set back to initial state
  1619. */
  1620. rt2800_bbp_write(rt2x00dev, 24, 0);
  1621. rt2800pci_rfcsr_read(rt2x00dev, 22, &rfcsr);
  1622. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
  1623. rt2800pci_rfcsr_write(rt2x00dev, 22, rfcsr);
  1624. /*
  1625. * set BBP back to BW20
  1626. */
  1627. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1628. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
  1629. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1630. return 0;
  1631. }
  1632. /*
  1633. * Device state switch handlers.
  1634. */
  1635. static void rt2800pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  1636. enum dev_state state)
  1637. {
  1638. u32 reg;
  1639. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1640. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
  1641. (state == STATE_RADIO_RX_ON) ||
  1642. (state == STATE_RADIO_RX_ON_LINK));
  1643. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1644. }
  1645. static void rt2800pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1646. enum dev_state state)
  1647. {
  1648. int mask = (state == STATE_RADIO_IRQ_ON);
  1649. u32 reg;
  1650. /*
  1651. * When interrupts are being enabled, the interrupt registers
  1652. * should clear the register to assure a clean state.
  1653. */
  1654. if (state == STATE_RADIO_IRQ_ON) {
  1655. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1656. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1657. }
  1658. rt2800_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1659. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDELAYINT, mask);
  1660. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDELAYINT, mask);
  1661. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_DONE, mask);
  1662. rt2x00_set_field32(&reg, INT_MASK_CSR_AC0_DMA_DONE, mask);
  1663. rt2x00_set_field32(&reg, INT_MASK_CSR_AC1_DMA_DONE, mask);
  1664. rt2x00_set_field32(&reg, INT_MASK_CSR_AC2_DMA_DONE, mask);
  1665. rt2x00_set_field32(&reg, INT_MASK_CSR_AC3_DMA_DONE, mask);
  1666. rt2x00_set_field32(&reg, INT_MASK_CSR_HCCA_DMA_DONE, mask);
  1667. rt2x00_set_field32(&reg, INT_MASK_CSR_MGMT_DMA_DONE, mask);
  1668. rt2x00_set_field32(&reg, INT_MASK_CSR_MCU_COMMAND, mask);
  1669. rt2x00_set_field32(&reg, INT_MASK_CSR_RXTX_COHERENT, mask);
  1670. rt2x00_set_field32(&reg, INT_MASK_CSR_TBTT, mask);
  1671. rt2x00_set_field32(&reg, INT_MASK_CSR_PRE_TBTT, mask);
  1672. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_FIFO_STATUS, mask);
  1673. rt2x00_set_field32(&reg, INT_MASK_CSR_AUTO_WAKEUP, mask);
  1674. rt2x00_set_field32(&reg, INT_MASK_CSR_GPTIMER, mask);
  1675. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_COHERENT, mask);
  1676. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_COHERENT, mask);
  1677. rt2800_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1678. }
  1679. static int rt2800pci_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
  1680. {
  1681. unsigned int i;
  1682. u32 reg;
  1683. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1684. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1685. if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
  1686. !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
  1687. return 0;
  1688. msleep(1);
  1689. }
  1690. ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
  1691. return -EACCES;
  1692. }
  1693. static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1694. {
  1695. u32 reg;
  1696. u16 word;
  1697. /*
  1698. * Initialize all registers.
  1699. */
  1700. if (unlikely(rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  1701. rt2800pci_init_queues(rt2x00dev) ||
  1702. rt2800pci_init_registers(rt2x00dev) ||
  1703. rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  1704. rt2800pci_init_bbp(rt2x00dev) ||
  1705. rt2800pci_init_rfcsr(rt2x00dev)))
  1706. return -EIO;
  1707. /*
  1708. * Send signal to firmware during boot time.
  1709. */
  1710. rt2800pci_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
  1711. /*
  1712. * Enable RX.
  1713. */
  1714. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1715. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  1716. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  1717. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1718. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1719. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  1720. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  1721. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  1722. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1723. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1724. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1725. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  1726. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  1727. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1728. /*
  1729. * Initialize LED control
  1730. */
  1731. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
  1732. rt2800pci_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
  1733. word & 0xff, (word >> 8) & 0xff);
  1734. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
  1735. rt2800pci_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
  1736. word & 0xff, (word >> 8) & 0xff);
  1737. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
  1738. rt2800pci_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
  1739. word & 0xff, (word >> 8) & 0xff);
  1740. return 0;
  1741. }
  1742. static void rt2800pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1743. {
  1744. u32 reg;
  1745. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1746. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1747. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1748. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1749. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1750. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1751. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1752. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
  1753. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
  1754. rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
  1755. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001280);
  1756. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  1757. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  1758. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  1759. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  1760. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  1761. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  1762. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  1763. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  1764. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  1765. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  1766. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  1767. /* Wait for DMA, ignore error */
  1768. rt2800pci_wait_wpdma_ready(rt2x00dev);
  1769. }
  1770. static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
  1771. enum dev_state state)
  1772. {
  1773. /*
  1774. * Always put the device to sleep (even when we intend to wakeup!)
  1775. * if the device is booting and wasn't asleep it will return
  1776. * failure when attempting to wakeup.
  1777. */
  1778. rt2800pci_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
  1779. if (state == STATE_AWAKE) {
  1780. rt2800pci_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKUP, 0, 0);
  1781. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKUP);
  1782. }
  1783. return 0;
  1784. }
  1785. static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1786. enum dev_state state)
  1787. {
  1788. int retval = 0;
  1789. switch (state) {
  1790. case STATE_RADIO_ON:
  1791. /*
  1792. * Before the radio can be enabled, the device first has
  1793. * to be woken up. After that it needs a bit of time
  1794. * to be fully awake and then the radio can be enabled.
  1795. */
  1796. rt2800pci_set_state(rt2x00dev, STATE_AWAKE);
  1797. msleep(1);
  1798. retval = rt2800pci_enable_radio(rt2x00dev);
  1799. break;
  1800. case STATE_RADIO_OFF:
  1801. /*
  1802. * After the radio has been disabled, the device should
  1803. * be put to sleep for powersaving.
  1804. */
  1805. rt2800pci_disable_radio(rt2x00dev);
  1806. rt2800pci_set_state(rt2x00dev, STATE_SLEEP);
  1807. break;
  1808. case STATE_RADIO_RX_ON:
  1809. case STATE_RADIO_RX_ON_LINK:
  1810. case STATE_RADIO_RX_OFF:
  1811. case STATE_RADIO_RX_OFF_LINK:
  1812. rt2800pci_toggle_rx(rt2x00dev, state);
  1813. break;
  1814. case STATE_RADIO_IRQ_ON:
  1815. case STATE_RADIO_IRQ_OFF:
  1816. rt2800pci_toggle_irq(rt2x00dev, state);
  1817. break;
  1818. case STATE_DEEP_SLEEP:
  1819. case STATE_SLEEP:
  1820. case STATE_STANDBY:
  1821. case STATE_AWAKE:
  1822. retval = rt2800pci_set_state(rt2x00dev, state);
  1823. break;
  1824. default:
  1825. retval = -ENOTSUPP;
  1826. break;
  1827. }
  1828. if (unlikely(retval))
  1829. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  1830. state, retval);
  1831. return retval;
  1832. }
  1833. /*
  1834. * TX descriptor initialization
  1835. */
  1836. static void rt2800pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  1837. struct sk_buff *skb,
  1838. struct txentry_desc *txdesc)
  1839. {
  1840. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  1841. __le32 *txd = skbdesc->desc;
  1842. __le32 *txwi = (__le32 *)(skb->data - rt2x00dev->hw->extra_tx_headroom);
  1843. u32 word;
  1844. /*
  1845. * Initialize TX Info descriptor
  1846. */
  1847. rt2x00_desc_read(txwi, 0, &word);
  1848. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  1849. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1850. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
  1851. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  1852. rt2x00_set_field32(&word, TXWI_W0_TS,
  1853. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1854. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  1855. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  1856. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
  1857. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
  1858. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
  1859. rt2x00_set_field32(&word, TXWI_W0_BW,
  1860. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  1861. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  1862. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  1863. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
  1864. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  1865. rt2x00_desc_write(txwi, 0, word);
  1866. rt2x00_desc_read(txwi, 1, &word);
  1867. rt2x00_set_field32(&word, TXWI_W1_ACK,
  1868. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1869. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  1870. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  1871. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
  1872. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  1873. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  1874. txdesc->key_idx : 0xff);
  1875. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  1876. skb->len - txdesc->l2pad);
  1877. rt2x00_set_field32(&word, TXWI_W1_PACKETID,
  1878. skbdesc->entry->queue->qid + 1);
  1879. rt2x00_desc_write(txwi, 1, word);
  1880. /*
  1881. * Always write 0 to IV/EIV fields, hardware will insert the IV
  1882. * from the IVEIV register when TXD_W3_WIV is set to 0.
  1883. * When TXD_W3_WIV is set to 1 it will use the IV data
  1884. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  1885. * crypto entry in the registers should be used to encrypt the frame.
  1886. */
  1887. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  1888. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  1889. /*
  1890. * The buffers pointed by SD_PTR0/SD_LEN0 and SD_PTR1/SD_LEN1
  1891. * must contains a TXWI structure + 802.11 header + padding + 802.11
  1892. * data. We choose to have SD_PTR0/SD_LEN0 only contains TXWI and
  1893. * SD_PTR1/SD_LEN1 contains 802.11 header + padding + 802.11
  1894. * data. It means that LAST_SEC0 is always 0.
  1895. */
  1896. /*
  1897. * Initialize TX descriptor
  1898. */
  1899. rt2x00_desc_read(txd, 0, &word);
  1900. rt2x00_set_field32(&word, TXD_W0_SD_PTR0, skbdesc->skb_dma);
  1901. rt2x00_desc_write(txd, 0, word);
  1902. rt2x00_desc_read(txd, 1, &word);
  1903. rt2x00_set_field32(&word, TXD_W1_SD_LEN1, skb->len);
  1904. rt2x00_set_field32(&word, TXD_W1_LAST_SEC1,
  1905. !test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1906. rt2x00_set_field32(&word, TXD_W1_BURST,
  1907. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1908. rt2x00_set_field32(&word, TXD_W1_SD_LEN0,
  1909. rt2x00dev->hw->extra_tx_headroom);
  1910. rt2x00_set_field32(&word, TXD_W1_LAST_SEC0, 0);
  1911. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 0);
  1912. rt2x00_desc_write(txd, 1, word);
  1913. rt2x00_desc_read(txd, 2, &word);
  1914. rt2x00_set_field32(&word, TXD_W2_SD_PTR1,
  1915. skbdesc->skb_dma + rt2x00dev->hw->extra_tx_headroom);
  1916. rt2x00_desc_write(txd, 2, word);
  1917. rt2x00_desc_read(txd, 3, &word);
  1918. rt2x00_set_field32(&word, TXD_W3_WIV,
  1919. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
  1920. rt2x00_set_field32(&word, TXD_W3_QSEL, 2);
  1921. rt2x00_desc_write(txd, 3, word);
  1922. }
  1923. /*
  1924. * TX data initialization
  1925. */
  1926. static void rt2800pci_write_beacon(struct queue_entry *entry)
  1927. {
  1928. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1929. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1930. unsigned int beacon_base;
  1931. u32 reg;
  1932. /*
  1933. * Disable beaconing while we are reloading the beacon data,
  1934. * otherwise we might be sending out invalid data.
  1935. */
  1936. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1937. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  1938. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1939. /*
  1940. * Write entire beacon with descriptor to register.
  1941. */
  1942. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  1943. rt2800_register_multiwrite(rt2x00dev,
  1944. beacon_base,
  1945. skbdesc->desc, skbdesc->desc_len);
  1946. rt2800_register_multiwrite(rt2x00dev,
  1947. beacon_base + skbdesc->desc_len,
  1948. entry->skb->data, entry->skb->len);
  1949. /*
  1950. * Clean up beacon skb.
  1951. */
  1952. dev_kfree_skb_any(entry->skb);
  1953. entry->skb = NULL;
  1954. }
  1955. static void rt2800pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  1956. const enum data_queue_qid queue_idx)
  1957. {
  1958. struct data_queue *queue;
  1959. unsigned int idx, qidx = 0;
  1960. u32 reg;
  1961. if (queue_idx == QID_BEACON) {
  1962. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1963. if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
  1964. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  1965. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  1966. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  1967. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1968. }
  1969. return;
  1970. }
  1971. if (queue_idx > QID_HCCA && queue_idx != QID_MGMT)
  1972. return;
  1973. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  1974. idx = queue->index[Q_INDEX];
  1975. if (queue_idx == QID_MGMT)
  1976. qidx = 5;
  1977. else
  1978. qidx = queue_idx;
  1979. rt2800_register_write(rt2x00dev, TX_CTX_IDX(qidx), idx);
  1980. }
  1981. static void rt2800pci_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
  1982. const enum data_queue_qid qid)
  1983. {
  1984. u32 reg;
  1985. if (qid == QID_BEACON) {
  1986. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, 0);
  1987. return;
  1988. }
  1989. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  1990. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, (qid == QID_AC_BE));
  1991. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, (qid == QID_AC_BK));
  1992. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, (qid == QID_AC_VI));
  1993. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, (qid == QID_AC_VO));
  1994. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  1995. }
  1996. /*
  1997. * RX control handlers
  1998. */
  1999. static void rt2800pci_fill_rxdone(struct queue_entry *entry,
  2000. struct rxdone_entry_desc *rxdesc)
  2001. {
  2002. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  2003. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  2004. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  2005. __le32 *rxd = entry_priv->desc;
  2006. __le32 *rxwi = (__le32 *)entry->skb->data;
  2007. u32 rxd3;
  2008. u32 rxwi0;
  2009. u32 rxwi1;
  2010. u32 rxwi2;
  2011. u32 rxwi3;
  2012. rt2x00_desc_read(rxd, 3, &rxd3);
  2013. rt2x00_desc_read(rxwi, 0, &rxwi0);
  2014. rt2x00_desc_read(rxwi, 1, &rxwi1);
  2015. rt2x00_desc_read(rxwi, 2, &rxwi2);
  2016. rt2x00_desc_read(rxwi, 3, &rxwi3);
  2017. if (rt2x00_get_field32(rxd3, RXD_W3_CRC_ERROR))
  2018. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  2019. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  2020. /*
  2021. * Unfortunately we don't know the cipher type used during
  2022. * decryption. This prevents us from correct providing
  2023. * correct statistics through debugfs.
  2024. */
  2025. rxdesc->cipher = rt2x00_get_field32(rxwi0, RXWI_W0_UDF);
  2026. rxdesc->cipher_status =
  2027. rt2x00_get_field32(rxd3, RXD_W3_CIPHER_ERROR);
  2028. }
  2029. if (rt2x00_get_field32(rxd3, RXD_W3_DECRYPTED)) {
  2030. /*
  2031. * Hardware has stripped IV/EIV data from 802.11 frame during
  2032. * decryption. Unfortunately the descriptor doesn't contain
  2033. * any fields with the EIV/IV data either, so they can't
  2034. * be restored by rt2x00lib.
  2035. */
  2036. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  2037. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  2038. rxdesc->flags |= RX_FLAG_DECRYPTED;
  2039. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  2040. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  2041. }
  2042. if (rt2x00_get_field32(rxd3, RXD_W3_MY_BSS))
  2043. rxdesc->dev_flags |= RXDONE_MY_BSS;
  2044. if (rt2x00_get_field32(rxd3, RXD_W3_L2PAD)) {
  2045. rxdesc->dev_flags |= RXDONE_L2PAD;
  2046. skbdesc->flags |= SKBDESC_L2_PADDED;
  2047. }
  2048. if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
  2049. rxdesc->flags |= RX_FLAG_SHORT_GI;
  2050. if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
  2051. rxdesc->flags |= RX_FLAG_40MHZ;
  2052. /*
  2053. * Detect RX rate, always use MCS as signal type.
  2054. */
  2055. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  2056. rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
  2057. rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
  2058. /*
  2059. * Mask of 0x8 bit to remove the short preamble flag.
  2060. */
  2061. if (rxdesc->rate_mode == RATE_MODE_CCK)
  2062. rxdesc->signal &= ~0x8;
  2063. rxdesc->rssi =
  2064. (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
  2065. rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
  2066. rxdesc->noise =
  2067. (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
  2068. rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
  2069. rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  2070. /*
  2071. * Set RX IDX in register to inform hardware that we have handled
  2072. * this entry and it is available for reuse again.
  2073. */
  2074. rt2800_register_write(rt2x00dev, RX_CRX_IDX, entry->entry_idx);
  2075. /*
  2076. * Remove TXWI descriptor from start of buffer.
  2077. */
  2078. skb_pull(entry->skb, RXWI_DESC_SIZE);
  2079. skb_trim(entry->skb, rxdesc->size);
  2080. }
  2081. /*
  2082. * Interrupt functions.
  2083. */
  2084. static void rt2800pci_txdone(struct rt2x00_dev *rt2x00dev)
  2085. {
  2086. struct data_queue *queue;
  2087. struct queue_entry *entry;
  2088. struct queue_entry *entry_done;
  2089. struct queue_entry_priv_pci *entry_priv;
  2090. struct txdone_entry_desc txdesc;
  2091. u32 word;
  2092. u32 reg;
  2093. u32 old_reg;
  2094. unsigned int type;
  2095. unsigned int index;
  2096. u16 mcs, real_mcs;
  2097. /*
  2098. * During each loop we will compare the freshly read
  2099. * TX_STA_FIFO register value with the value read from
  2100. * the previous loop. If the 2 values are equal then
  2101. * we should stop processing because the chance it
  2102. * quite big that the device has been unplugged and
  2103. * we risk going into an endless loop.
  2104. */
  2105. old_reg = 0;
  2106. while (1) {
  2107. rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
  2108. if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
  2109. break;
  2110. if (old_reg == reg)
  2111. break;
  2112. old_reg = reg;
  2113. /*
  2114. * Skip this entry when it contains an invalid
  2115. * queue identication number.
  2116. */
  2117. type = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE) - 1;
  2118. if (type >= QID_RX)
  2119. continue;
  2120. queue = rt2x00queue_get_queue(rt2x00dev, type);
  2121. if (unlikely(!queue))
  2122. continue;
  2123. /*
  2124. * Skip this entry when it contains an invalid
  2125. * index number.
  2126. */
  2127. index = rt2x00_get_field32(reg, TX_STA_FIFO_WCID) - 1;
  2128. if (unlikely(index >= queue->limit))
  2129. continue;
  2130. entry = &queue->entries[index];
  2131. entry_priv = entry->priv_data;
  2132. rt2x00_desc_read((__le32 *)entry->skb->data, 0, &word);
  2133. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  2134. while (entry != entry_done) {
  2135. /*
  2136. * Catch up.
  2137. * Just report any entries we missed as failed.
  2138. */
  2139. WARNING(rt2x00dev,
  2140. "TX status report missed for entry %d\n",
  2141. entry_done->entry_idx);
  2142. txdesc.flags = 0;
  2143. __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
  2144. txdesc.retry = 0;
  2145. rt2x00lib_txdone(entry_done, &txdesc);
  2146. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  2147. }
  2148. /*
  2149. * Obtain the status about this packet.
  2150. */
  2151. txdesc.flags = 0;
  2152. if (rt2x00_get_field32(reg, TX_STA_FIFO_TX_SUCCESS))
  2153. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  2154. else
  2155. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  2156. /*
  2157. * Ralink has a retry mechanism using a global fallback
  2158. * table. We setup this fallback table to try immediate
  2159. * lower rate for all rates. In the TX_STA_FIFO,
  2160. * the MCS field contains the MCS used for the successfull
  2161. * transmission. If the first transmission succeed,
  2162. * we have mcs == tx_mcs. On the second transmission,
  2163. * we have mcs = tx_mcs - 1. So the number of
  2164. * retry is (tx_mcs - mcs).
  2165. */
  2166. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  2167. real_mcs = rt2x00_get_field32(reg, TX_STA_FIFO_MCS);
  2168. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  2169. txdesc.retry = mcs - min(mcs, real_mcs);
  2170. rt2x00lib_txdone(entry, &txdesc);
  2171. }
  2172. }
  2173. static irqreturn_t rt2800pci_interrupt(int irq, void *dev_instance)
  2174. {
  2175. struct rt2x00_dev *rt2x00dev = dev_instance;
  2176. u32 reg;
  2177. /* Read status and ACK all interrupts */
  2178. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  2179. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  2180. if (!reg)
  2181. return IRQ_NONE;
  2182. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  2183. return IRQ_HANDLED;
  2184. /*
  2185. * 1 - Rx ring done interrupt.
  2186. */
  2187. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RX_DONE))
  2188. rt2x00pci_rxdone(rt2x00dev);
  2189. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TX_FIFO_STATUS))
  2190. rt2800pci_txdone(rt2x00dev);
  2191. return IRQ_HANDLED;
  2192. }
  2193. /*
  2194. * Device probe functions.
  2195. */
  2196. static int rt2800pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  2197. {
  2198. u16 word;
  2199. u8 *mac;
  2200. u8 default_lna_gain;
  2201. /*
  2202. * Read EEPROM into buffer
  2203. */
  2204. switch(rt2x00dev->chip.rt) {
  2205. case RT2880:
  2206. case RT3052:
  2207. rt2800pci_read_eeprom_soc(rt2x00dev);
  2208. break;
  2209. case RT3090:
  2210. rt2800pci_read_eeprom_efuse(rt2x00dev);
  2211. break;
  2212. default:
  2213. rt2800pci_read_eeprom_pci(rt2x00dev);
  2214. break;
  2215. }
  2216. /*
  2217. * Start validation of the data that has been read.
  2218. */
  2219. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  2220. if (!is_valid_ether_addr(mac)) {
  2221. random_ether_addr(mac);
  2222. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  2223. }
  2224. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  2225. if (word == 0xffff) {
  2226. rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
  2227. rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
  2228. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
  2229. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2230. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  2231. } else if (rt2x00_rev(&rt2x00dev->chip) < RT2883_VERSION) {
  2232. /*
  2233. * There is a max of 2 RX streams for RT2860 series
  2234. */
  2235. if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
  2236. rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
  2237. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2238. }
  2239. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  2240. if (word == 0xffff) {
  2241. rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
  2242. rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
  2243. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  2244. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  2245. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  2246. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
  2247. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
  2248. rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
  2249. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
  2250. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
  2251. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  2252. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  2253. }
  2254. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2255. if ((word & 0x00ff) == 0x00ff) {
  2256. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2257. rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
  2258. LED_MODE_TXRX_ACTIVITY);
  2259. rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
  2260. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2261. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
  2262. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
  2263. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
  2264. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  2265. }
  2266. /*
  2267. * During the LNA validation we are going to use
  2268. * lna0 as correct value. Note that EEPROM_LNA
  2269. * is never validated.
  2270. */
  2271. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
  2272. default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
  2273. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
  2274. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
  2275. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
  2276. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
  2277. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
  2278. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
  2279. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
  2280. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
  2281. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
  2282. if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
  2283. rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
  2284. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
  2285. default_lna_gain);
  2286. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
  2287. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
  2288. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
  2289. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
  2290. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
  2291. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
  2292. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
  2293. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
  2294. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
  2295. rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
  2296. if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
  2297. rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
  2298. rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
  2299. default_lna_gain);
  2300. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
  2301. return 0;
  2302. }
  2303. static int rt2800pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2304. {
  2305. u32 reg;
  2306. u16 value;
  2307. u16 eeprom;
  2308. /*
  2309. * Read EEPROM word for configuration.
  2310. */
  2311. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2312. /*
  2313. * Identify RF chipset.
  2314. */
  2315. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  2316. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  2317. rt2x00_set_chip_rf(rt2x00dev, value, reg);
  2318. if (!rt2x00_rf(&rt2x00dev->chip, RF2820) &&
  2319. !rt2x00_rf(&rt2x00dev->chip, RF2850) &&
  2320. !rt2x00_rf(&rt2x00dev->chip, RF2720) &&
  2321. !rt2x00_rf(&rt2x00dev->chip, RF2750) &&
  2322. !rt2x00_rf(&rt2x00dev->chip, RF3020) &&
  2323. !rt2x00_rf(&rt2x00dev->chip, RF2020) &&
  2324. !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
  2325. !rt2x00_rf(&rt2x00dev->chip, RF3022)) {
  2326. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  2327. return -ENODEV;
  2328. }
  2329. /*
  2330. * Identify default antenna configuration.
  2331. */
  2332. rt2x00dev->default_ant.tx =
  2333. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
  2334. rt2x00dev->default_ant.rx =
  2335. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
  2336. /*
  2337. * Read frequency offset and RF programming sequence.
  2338. */
  2339. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2340. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2341. /*
  2342. * Read external LNA informations.
  2343. */
  2344. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  2345. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  2346. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  2347. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  2348. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  2349. /*
  2350. * Detect if this device has an hardware controlled radio.
  2351. */
  2352. if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
  2353. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  2354. /*
  2355. * Store led settings, for correct led behaviour.
  2356. */
  2357. #ifdef CONFIG_RT2X00_LIB_LEDS
  2358. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2359. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2360. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
  2361. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
  2362. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2363. return 0;
  2364. }
  2365. /*
  2366. * RF value list for rt2860
  2367. * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
  2368. */
  2369. static const struct rf_channel rf_vals[] = {
  2370. { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
  2371. { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
  2372. { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
  2373. { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
  2374. { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
  2375. { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
  2376. { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
  2377. { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
  2378. { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
  2379. { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
  2380. { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
  2381. { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
  2382. { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
  2383. { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
  2384. /* 802.11 UNI / HyperLan 2 */
  2385. { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
  2386. { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
  2387. { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
  2388. { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
  2389. { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
  2390. { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
  2391. { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
  2392. { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
  2393. { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
  2394. { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
  2395. { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
  2396. { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
  2397. /* 802.11 HyperLan 2 */
  2398. { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
  2399. { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
  2400. { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
  2401. { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
  2402. { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
  2403. { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
  2404. { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
  2405. { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
  2406. { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
  2407. { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
  2408. { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
  2409. { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
  2410. { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
  2411. { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
  2412. { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
  2413. { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
  2414. /* 802.11 UNII */
  2415. { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
  2416. { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
  2417. { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
  2418. { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
  2419. { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
  2420. { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
  2421. { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
  2422. /* 802.11 Japan */
  2423. { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
  2424. { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
  2425. { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
  2426. { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
  2427. { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
  2428. { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
  2429. { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
  2430. };
  2431. static int rt2800pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2432. {
  2433. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2434. struct channel_info *info;
  2435. char *tx_power1;
  2436. char *tx_power2;
  2437. unsigned int i;
  2438. u16 eeprom;
  2439. /*
  2440. * Initialize all hw fields.
  2441. */
  2442. rt2x00dev->hw->flags =
  2443. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2444. IEEE80211_HW_SIGNAL_DBM |
  2445. IEEE80211_HW_SUPPORTS_PS |
  2446. IEEE80211_HW_PS_NULLFUNC_STACK;
  2447. rt2x00dev->hw->extra_tx_headroom = TXWI_DESC_SIZE;
  2448. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2449. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2450. rt2x00_eeprom_addr(rt2x00dev,
  2451. EEPROM_MAC_ADDR_0));
  2452. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2453. /*
  2454. * Initialize hw_mode information.
  2455. */
  2456. spec->supported_bands = SUPPORT_BAND_2GHZ;
  2457. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  2458. if (rt2x00_rf(&rt2x00dev->chip, RF2820) ||
  2459. rt2x00_rf(&rt2x00dev->chip, RF2720) ||
  2460. rt2x00_rf(&rt2x00dev->chip, RF3020) ||
  2461. rt2x00_rf(&rt2x00dev->chip, RF3021) ||
  2462. rt2x00_rf(&rt2x00dev->chip, RF3022) ||
  2463. rt2x00_rf(&rt2x00dev->chip, RF2020) ||
  2464. rt2x00_rf(&rt2x00dev->chip, RF3052)) {
  2465. spec->num_channels = 14;
  2466. spec->channels = rf_vals;
  2467. } else if (rt2x00_rf(&rt2x00dev->chip, RF2850) ||
  2468. rt2x00_rf(&rt2x00dev->chip, RF2750)) {
  2469. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  2470. spec->num_channels = ARRAY_SIZE(rf_vals);
  2471. spec->channels = rf_vals;
  2472. }
  2473. /*
  2474. * Initialize HT information.
  2475. */
  2476. spec->ht.ht_supported = true;
  2477. spec->ht.cap =
  2478. IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  2479. IEEE80211_HT_CAP_GRN_FLD |
  2480. IEEE80211_HT_CAP_SGI_20 |
  2481. IEEE80211_HT_CAP_SGI_40 |
  2482. IEEE80211_HT_CAP_TX_STBC |
  2483. IEEE80211_HT_CAP_RX_STBC |
  2484. IEEE80211_HT_CAP_PSMP_SUPPORT;
  2485. spec->ht.ampdu_factor = 3;
  2486. spec->ht.ampdu_density = 4;
  2487. spec->ht.mcs.tx_params =
  2488. IEEE80211_HT_MCS_TX_DEFINED |
  2489. IEEE80211_HT_MCS_TX_RX_DIFF |
  2490. ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
  2491. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  2492. switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
  2493. case 3:
  2494. spec->ht.mcs.rx_mask[2] = 0xff;
  2495. case 2:
  2496. spec->ht.mcs.rx_mask[1] = 0xff;
  2497. case 1:
  2498. spec->ht.mcs.rx_mask[0] = 0xff;
  2499. spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
  2500. break;
  2501. }
  2502. /*
  2503. * Create channel information array
  2504. */
  2505. info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
  2506. if (!info)
  2507. return -ENOMEM;
  2508. spec->channels_info = info;
  2509. tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
  2510. tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
  2511. for (i = 0; i < 14; i++) {
  2512. info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
  2513. info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
  2514. }
  2515. if (spec->num_channels > 14) {
  2516. tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
  2517. tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
  2518. for (i = 14; i < spec->num_channels; i++) {
  2519. info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
  2520. info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
  2521. }
  2522. }
  2523. return 0;
  2524. }
  2525. static int rt2800pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  2526. {
  2527. int retval;
  2528. /*
  2529. * Allocate eeprom data.
  2530. */
  2531. retval = rt2800pci_validate_eeprom(rt2x00dev);
  2532. if (retval)
  2533. return retval;
  2534. retval = rt2800pci_init_eeprom(rt2x00dev);
  2535. if (retval)
  2536. return retval;
  2537. /*
  2538. * Initialize hw specifications.
  2539. */
  2540. retval = rt2800pci_probe_hw_mode(rt2x00dev);
  2541. if (retval)
  2542. return retval;
  2543. /*
  2544. * This device has multiple filters for control frames
  2545. * and has a separate filter for PS Poll frames.
  2546. */
  2547. __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
  2548. __set_bit(DRIVER_SUPPORT_CONTROL_FILTER_PSPOLL, &rt2x00dev->flags);
  2549. /*
  2550. * This device requires firmware.
  2551. */
  2552. if (!rt2x00_rt(&rt2x00dev->chip, RT2880) &&
  2553. !rt2x00_rt(&rt2x00dev->chip, RT3052))
  2554. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  2555. __set_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags);
  2556. __set_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags);
  2557. if (!modparam_nohwcrypt)
  2558. __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
  2559. /*
  2560. * Set the rssi offset.
  2561. */
  2562. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  2563. return 0;
  2564. }
  2565. /*
  2566. * IEEE80211 stack callback functions.
  2567. */
  2568. static void rt2800pci_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
  2569. u32 *iv32, u16 *iv16)
  2570. {
  2571. struct rt2x00_dev *rt2x00dev = hw->priv;
  2572. struct mac_iveiv_entry iveiv_entry;
  2573. u32 offset;
  2574. offset = MAC_IVEIV_ENTRY(hw_key_idx);
  2575. rt2800_register_multiread(rt2x00dev, offset,
  2576. &iveiv_entry, sizeof(iveiv_entry));
  2577. memcpy(&iveiv_entry.iv[0], iv16, sizeof(iv16));
  2578. memcpy(&iveiv_entry.iv[4], iv32, sizeof(iv32));
  2579. }
  2580. static int rt2800pci_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2581. {
  2582. struct rt2x00_dev *rt2x00dev = hw->priv;
  2583. u32 reg;
  2584. bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
  2585. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  2586. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
  2587. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  2588. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  2589. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
  2590. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  2591. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  2592. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
  2593. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  2594. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  2595. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
  2596. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  2597. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  2598. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
  2599. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  2600. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  2601. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
  2602. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  2603. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  2604. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
  2605. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  2606. return 0;
  2607. }
  2608. static int rt2800pci_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  2609. const struct ieee80211_tx_queue_params *params)
  2610. {
  2611. struct rt2x00_dev *rt2x00dev = hw->priv;
  2612. struct data_queue *queue;
  2613. struct rt2x00_field32 field;
  2614. int retval;
  2615. u32 reg;
  2616. u32 offset;
  2617. /*
  2618. * First pass the configuration through rt2x00lib, that will
  2619. * update the queue settings and validate the input. After that
  2620. * we are free to update the registers based on the value
  2621. * in the queue parameter.
  2622. */
  2623. retval = rt2x00mac_conf_tx(hw, queue_idx, params);
  2624. if (retval)
  2625. return retval;
  2626. /*
  2627. * We only need to perform additional register initialization
  2628. * for WMM queues/
  2629. */
  2630. if (queue_idx >= 4)
  2631. return 0;
  2632. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  2633. /* Update WMM TXOP register */
  2634. offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
  2635. field.bit_offset = (queue_idx & 1) * 16;
  2636. field.bit_mask = 0xffff << field.bit_offset;
  2637. rt2800_register_read(rt2x00dev, offset, &reg);
  2638. rt2x00_set_field32(&reg, field, queue->txop);
  2639. rt2800_register_write(rt2x00dev, offset, reg);
  2640. /* Update WMM registers */
  2641. field.bit_offset = queue_idx * 4;
  2642. field.bit_mask = 0xf << field.bit_offset;
  2643. rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
  2644. rt2x00_set_field32(&reg, field, queue->aifs);
  2645. rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
  2646. rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
  2647. rt2x00_set_field32(&reg, field, queue->cw_min);
  2648. rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
  2649. rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
  2650. rt2x00_set_field32(&reg, field, queue->cw_max);
  2651. rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
  2652. /* Update EDCA registers */
  2653. offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
  2654. rt2800_register_read(rt2x00dev, offset, &reg);
  2655. rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
  2656. rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
  2657. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
  2658. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
  2659. rt2800_register_write(rt2x00dev, offset, reg);
  2660. return 0;
  2661. }
  2662. static u64 rt2800pci_get_tsf(struct ieee80211_hw *hw)
  2663. {
  2664. struct rt2x00_dev *rt2x00dev = hw->priv;
  2665. u64 tsf;
  2666. u32 reg;
  2667. rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
  2668. tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
  2669. rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
  2670. tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
  2671. return tsf;
  2672. }
  2673. static const struct ieee80211_ops rt2800pci_mac80211_ops = {
  2674. .tx = rt2x00mac_tx,
  2675. .start = rt2x00mac_start,
  2676. .stop = rt2x00mac_stop,
  2677. .add_interface = rt2x00mac_add_interface,
  2678. .remove_interface = rt2x00mac_remove_interface,
  2679. .config = rt2x00mac_config,
  2680. .configure_filter = rt2x00mac_configure_filter,
  2681. .set_key = rt2x00mac_set_key,
  2682. .get_stats = rt2x00mac_get_stats,
  2683. .get_tkip_seq = rt2800pci_get_tkip_seq,
  2684. .set_rts_threshold = rt2800pci_set_rts_threshold,
  2685. .bss_info_changed = rt2x00mac_bss_info_changed,
  2686. .conf_tx = rt2800pci_conf_tx,
  2687. .get_tx_stats = rt2x00mac_get_tx_stats,
  2688. .get_tsf = rt2800pci_get_tsf,
  2689. .rfkill_poll = rt2x00mac_rfkill_poll,
  2690. };
  2691. static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
  2692. .irq_handler = rt2800pci_interrupt,
  2693. .probe_hw = rt2800pci_probe_hw,
  2694. .get_firmware_name = rt2800pci_get_firmware_name,
  2695. .check_firmware = rt2800pci_check_firmware,
  2696. .load_firmware = rt2800pci_load_firmware,
  2697. .initialize = rt2x00pci_initialize,
  2698. .uninitialize = rt2x00pci_uninitialize,
  2699. .get_entry_state = rt2800pci_get_entry_state,
  2700. .clear_entry = rt2800pci_clear_entry,
  2701. .set_device_state = rt2800pci_set_device_state,
  2702. .rfkill_poll = rt2800pci_rfkill_poll,
  2703. .link_stats = rt2800pci_link_stats,
  2704. .reset_tuner = rt2800pci_reset_tuner,
  2705. .link_tuner = rt2800pci_link_tuner,
  2706. .write_tx_desc = rt2800pci_write_tx_desc,
  2707. .write_tx_data = rt2x00pci_write_tx_data,
  2708. .write_beacon = rt2800pci_write_beacon,
  2709. .kick_tx_queue = rt2800pci_kick_tx_queue,
  2710. .kill_tx_queue = rt2800pci_kill_tx_queue,
  2711. .fill_rxdone = rt2800pci_fill_rxdone,
  2712. .config_shared_key = rt2800pci_config_shared_key,
  2713. .config_pairwise_key = rt2800pci_config_pairwise_key,
  2714. .config_filter = rt2800pci_config_filter,
  2715. .config_intf = rt2800pci_config_intf,
  2716. .config_erp = rt2800pci_config_erp,
  2717. .config_ant = rt2800pci_config_ant,
  2718. .config = rt2800pci_config,
  2719. };
  2720. static const struct data_queue_desc rt2800pci_queue_rx = {
  2721. .entry_num = RX_ENTRIES,
  2722. .data_size = AGGREGATION_SIZE,
  2723. .desc_size = RXD_DESC_SIZE,
  2724. .priv_size = sizeof(struct queue_entry_priv_pci),
  2725. };
  2726. static const struct data_queue_desc rt2800pci_queue_tx = {
  2727. .entry_num = TX_ENTRIES,
  2728. .data_size = AGGREGATION_SIZE,
  2729. .desc_size = TXD_DESC_SIZE,
  2730. .priv_size = sizeof(struct queue_entry_priv_pci),
  2731. };
  2732. static const struct data_queue_desc rt2800pci_queue_bcn = {
  2733. .entry_num = 8 * BEACON_ENTRIES,
  2734. .data_size = 0, /* No DMA required for beacons */
  2735. .desc_size = TXWI_DESC_SIZE,
  2736. .priv_size = sizeof(struct queue_entry_priv_pci),
  2737. };
  2738. static const struct rt2x00_ops rt2800pci_ops = {
  2739. .name = KBUILD_MODNAME,
  2740. .max_sta_intf = 1,
  2741. .max_ap_intf = 8,
  2742. .eeprom_size = EEPROM_SIZE,
  2743. .rf_size = RF_SIZE,
  2744. .tx_queues = NUM_TX_QUEUES,
  2745. .rx = &rt2800pci_queue_rx,
  2746. .tx = &rt2800pci_queue_tx,
  2747. .bcn = &rt2800pci_queue_bcn,
  2748. .lib = &rt2800pci_rt2x00_ops,
  2749. .hw = &rt2800pci_mac80211_ops,
  2750. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2751. .debugfs = &rt2800pci_rt2x00debug,
  2752. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2753. };
  2754. /*
  2755. * RT2800pci module information.
  2756. */
  2757. static struct pci_device_id rt2800pci_device_table[] = {
  2758. { PCI_DEVICE(0x1462, 0x891a), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2759. { PCI_DEVICE(0x1432, 0x7708), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2760. { PCI_DEVICE(0x1432, 0x7727), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2761. { PCI_DEVICE(0x1432, 0x7728), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2762. { PCI_DEVICE(0x1432, 0x7738), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2763. { PCI_DEVICE(0x1432, 0x7748), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2764. { PCI_DEVICE(0x1432, 0x7758), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2765. { PCI_DEVICE(0x1432, 0x7768), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2766. { PCI_DEVICE(0x1814, 0x0601), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2767. { PCI_DEVICE(0x1814, 0x0681), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2768. { PCI_DEVICE(0x1814, 0x0701), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2769. { PCI_DEVICE(0x1814, 0x0781), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2770. { PCI_DEVICE(0x1814, 0x3060), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2771. { PCI_DEVICE(0x1814, 0x3062), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2772. { PCI_DEVICE(0x1814, 0x3090), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2773. { PCI_DEVICE(0x1814, 0x3091), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2774. { PCI_DEVICE(0x1814, 0x3092), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2775. { PCI_DEVICE(0x1814, 0x3562), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2776. { PCI_DEVICE(0x1814, 0x3592), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2777. { PCI_DEVICE(0x1a3b, 0x1059), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2778. { 0, }
  2779. };
  2780. MODULE_AUTHOR(DRV_PROJECT);
  2781. MODULE_VERSION(DRV_VERSION);
  2782. MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
  2783. MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
  2784. #ifdef CONFIG_RT2800PCI_PCI
  2785. MODULE_FIRMWARE(FIRMWARE_RT2860);
  2786. MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
  2787. #endif /* CONFIG_RT2800PCI_PCI */
  2788. MODULE_LICENSE("GPL");
  2789. #ifdef CONFIG_RT2800PCI_WISOC
  2790. #if defined(CONFIG_RALINK_RT288X)
  2791. __rt2x00soc_probe(RT2880, &rt2800pci_ops);
  2792. #elif defined(CONFIG_RALINK_RT305X)
  2793. __rt2x00soc_probe(RT3052, &rt2800pci_ops);
  2794. #endif
  2795. static struct platform_driver rt2800soc_driver = {
  2796. .driver = {
  2797. .name = "rt2800_wmac",
  2798. .owner = THIS_MODULE,
  2799. .mod_name = KBUILD_MODNAME,
  2800. },
  2801. .probe = __rt2x00soc_probe,
  2802. .remove = __devexit_p(rt2x00soc_remove),
  2803. .suspend = rt2x00soc_suspend,
  2804. .resume = rt2x00soc_resume,
  2805. };
  2806. #endif /* CONFIG_RT2800PCI_WISOC */
  2807. #ifdef CONFIG_RT2800PCI_PCI
  2808. static struct pci_driver rt2800pci_driver = {
  2809. .name = KBUILD_MODNAME,
  2810. .id_table = rt2800pci_device_table,
  2811. .probe = rt2x00pci_probe,
  2812. .remove = __devexit_p(rt2x00pci_remove),
  2813. .suspend = rt2x00pci_suspend,
  2814. .resume = rt2x00pci_resume,
  2815. };
  2816. #endif /* CONFIG_RT2800PCI_PCI */
  2817. static int __init rt2800pci_init(void)
  2818. {
  2819. int ret = 0;
  2820. #ifdef CONFIG_RT2800PCI_WISOC
  2821. ret = platform_driver_register(&rt2800soc_driver);
  2822. if (ret)
  2823. return ret;
  2824. #endif
  2825. #ifdef CONFIG_RT2800PCI_PCI
  2826. ret = pci_register_driver(&rt2800pci_driver);
  2827. if (ret) {
  2828. #ifdef CONFIG_RT2800PCI_WISOC
  2829. platform_driver_unregister(&rt2800soc_driver);
  2830. #endif
  2831. return ret;
  2832. }
  2833. #endif
  2834. return ret;
  2835. }
  2836. static void __exit rt2800pci_exit(void)
  2837. {
  2838. #ifdef CONFIG_RT2800PCI_PCI
  2839. pci_unregister_driver(&rt2800pci_driver);
  2840. #endif
  2841. #ifdef CONFIG_RT2800PCI_WISOC
  2842. platform_driver_unregister(&rt2800soc_driver);
  2843. #endif
  2844. }
  2845. module_init(rt2800pci_init);
  2846. module_exit(rt2800pci_exit);