rt2800pci.c 106 KB

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