rt61pci.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  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: rt61pci
  19. Abstract: rt61pci device specific routines.
  20. Supported chipsets: RT2561, RT2561s, RT2661.
  21. */
  22. #include <linux/crc-itu-t.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/slab.h>
  29. #include <linux/pci.h>
  30. #include <linux/eeprom_93cx6.h>
  31. #include "rt2x00.h"
  32. #include "rt2x00mmio.h"
  33. #include "rt2x00pci.h"
  34. #include "rt61pci.h"
  35. /*
  36. * Allow hardware encryption to be disabled.
  37. */
  38. static bool modparam_nohwcrypt = false;
  39. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  40. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  41. /*
  42. * Register access.
  43. * BBP and RF register require indirect register access,
  44. * and use the CSR registers PHY_CSR3 and PHY_CSR4 to achieve this.
  45. * These indirect registers work with busy bits,
  46. * and we will try maximal REGISTER_BUSY_COUNT times to access
  47. * the register while taking a REGISTER_BUSY_DELAY us delay
  48. * between each attempt. When the busy bit is still set at that time,
  49. * the access attempt is considered to have failed,
  50. * and we will print an error.
  51. */
  52. #define WAIT_FOR_BBP(__dev, __reg) \
  53. rt2x00mmio_regbusy_read((__dev), PHY_CSR3, PHY_CSR3_BUSY, (__reg))
  54. #define WAIT_FOR_RF(__dev, __reg) \
  55. rt2x00mmio_regbusy_read((__dev), PHY_CSR4, PHY_CSR4_BUSY, (__reg))
  56. #define WAIT_FOR_MCU(__dev, __reg) \
  57. rt2x00mmio_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  58. H2M_MAILBOX_CSR_OWNER, (__reg))
  59. static void rt61pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  60. const unsigned int word, const u8 value)
  61. {
  62. u32 reg;
  63. mutex_lock(&rt2x00dev->csr_mutex);
  64. /*
  65. * Wait until the BBP becomes available, afterwards we
  66. * can safely write the new data into the register.
  67. */
  68. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  69. reg = 0;
  70. rt2x00_set_field32(&reg, PHY_CSR3_VALUE, value);
  71. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  72. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  73. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 0);
  74. rt2x00mmio_register_write(rt2x00dev, PHY_CSR3, reg);
  75. }
  76. mutex_unlock(&rt2x00dev->csr_mutex);
  77. }
  78. static void rt61pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  79. const unsigned int word, u8 *value)
  80. {
  81. u32 reg;
  82. mutex_lock(&rt2x00dev->csr_mutex);
  83. /*
  84. * Wait until the BBP becomes available, afterwards we
  85. * can safely write the read request into the register.
  86. * After the data has been written, we wait until hardware
  87. * returns the correct value, if at any time the register
  88. * doesn't become available in time, reg will be 0xffffffff
  89. * which means we return 0xff to the caller.
  90. */
  91. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  92. reg = 0;
  93. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  94. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  95. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 1);
  96. rt2x00mmio_register_write(rt2x00dev, PHY_CSR3, reg);
  97. WAIT_FOR_BBP(rt2x00dev, &reg);
  98. }
  99. *value = rt2x00_get_field32(reg, PHY_CSR3_VALUE);
  100. mutex_unlock(&rt2x00dev->csr_mutex);
  101. }
  102. static void rt61pci_rf_write(struct rt2x00_dev *rt2x00dev,
  103. const unsigned int word, const u32 value)
  104. {
  105. u32 reg;
  106. mutex_lock(&rt2x00dev->csr_mutex);
  107. /*
  108. * Wait until the RF becomes available, afterwards we
  109. * can safely write the new data into the register.
  110. */
  111. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  112. reg = 0;
  113. rt2x00_set_field32(&reg, PHY_CSR4_VALUE, value);
  114. rt2x00_set_field32(&reg, PHY_CSR4_NUMBER_OF_BITS, 21);
  115. rt2x00_set_field32(&reg, PHY_CSR4_IF_SELECT, 0);
  116. rt2x00_set_field32(&reg, PHY_CSR4_BUSY, 1);
  117. rt2x00mmio_register_write(rt2x00dev, PHY_CSR4, reg);
  118. rt2x00_rf_write(rt2x00dev, word, value);
  119. }
  120. mutex_unlock(&rt2x00dev->csr_mutex);
  121. }
  122. static void rt61pci_mcu_request(struct rt2x00_dev *rt2x00dev,
  123. const u8 command, const u8 token,
  124. const u8 arg0, const u8 arg1)
  125. {
  126. u32 reg;
  127. mutex_lock(&rt2x00dev->csr_mutex);
  128. /*
  129. * Wait until the MCU becomes available, afterwards we
  130. * can safely write the new data into the register.
  131. */
  132. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  133. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  134. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  135. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  136. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  137. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CSR, reg);
  138. rt2x00mmio_register_read(rt2x00dev, HOST_CMD_CSR, &reg);
  139. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  140. rt2x00_set_field32(&reg, HOST_CMD_CSR_INTERRUPT_MCU, 1);
  141. rt2x00mmio_register_write(rt2x00dev, HOST_CMD_CSR, reg);
  142. }
  143. mutex_unlock(&rt2x00dev->csr_mutex);
  144. }
  145. static void rt61pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  146. {
  147. struct rt2x00_dev *rt2x00dev = eeprom->data;
  148. u32 reg;
  149. rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
  150. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  151. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  152. eeprom->reg_data_clock =
  153. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  154. eeprom->reg_chip_select =
  155. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  156. }
  157. static void rt61pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  158. {
  159. struct rt2x00_dev *rt2x00dev = eeprom->data;
  160. u32 reg = 0;
  161. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  162. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  163. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  164. !!eeprom->reg_data_clock);
  165. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  166. !!eeprom->reg_chip_select);
  167. rt2x00mmio_register_write(rt2x00dev, E2PROM_CSR, reg);
  168. }
  169. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  170. static const struct rt2x00debug rt61pci_rt2x00debug = {
  171. .owner = THIS_MODULE,
  172. .csr = {
  173. .read = rt2x00mmio_register_read,
  174. .write = rt2x00mmio_register_write,
  175. .flags = RT2X00DEBUGFS_OFFSET,
  176. .word_base = CSR_REG_BASE,
  177. .word_size = sizeof(u32),
  178. .word_count = CSR_REG_SIZE / sizeof(u32),
  179. },
  180. .eeprom = {
  181. .read = rt2x00_eeprom_read,
  182. .write = rt2x00_eeprom_write,
  183. .word_base = EEPROM_BASE,
  184. .word_size = sizeof(u16),
  185. .word_count = EEPROM_SIZE / sizeof(u16),
  186. },
  187. .bbp = {
  188. .read = rt61pci_bbp_read,
  189. .write = rt61pci_bbp_write,
  190. .word_base = BBP_BASE,
  191. .word_size = sizeof(u8),
  192. .word_count = BBP_SIZE / sizeof(u8),
  193. },
  194. .rf = {
  195. .read = rt2x00_rf_read,
  196. .write = rt61pci_rf_write,
  197. .word_base = RF_BASE,
  198. .word_size = sizeof(u32),
  199. .word_count = RF_SIZE / sizeof(u32),
  200. },
  201. };
  202. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  203. static int rt61pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  204. {
  205. u32 reg;
  206. rt2x00mmio_register_read(rt2x00dev, MAC_CSR13, &reg);
  207. return rt2x00_get_field32(reg, MAC_CSR13_VAL5);
  208. }
  209. #ifdef CONFIG_RT2X00_LIB_LEDS
  210. static void rt61pci_brightness_set(struct led_classdev *led_cdev,
  211. enum led_brightness brightness)
  212. {
  213. struct rt2x00_led *led =
  214. container_of(led_cdev, struct rt2x00_led, led_dev);
  215. unsigned int enabled = brightness != LED_OFF;
  216. unsigned int a_mode =
  217. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  218. unsigned int bg_mode =
  219. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  220. if (led->type == LED_TYPE_RADIO) {
  221. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  222. MCU_LEDCS_RADIO_STATUS, enabled);
  223. rt61pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff,
  224. (led->rt2x00dev->led_mcu_reg & 0xff),
  225. ((led->rt2x00dev->led_mcu_reg >> 8)));
  226. } else if (led->type == LED_TYPE_ASSOC) {
  227. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  228. MCU_LEDCS_LINK_BG_STATUS, bg_mode);
  229. rt2x00_set_field16(&led->rt2x00dev->led_mcu_reg,
  230. MCU_LEDCS_LINK_A_STATUS, a_mode);
  231. rt61pci_mcu_request(led->rt2x00dev, MCU_LED, 0xff,
  232. (led->rt2x00dev->led_mcu_reg & 0xff),
  233. ((led->rt2x00dev->led_mcu_reg >> 8)));
  234. } else if (led->type == LED_TYPE_QUALITY) {
  235. /*
  236. * The brightness is divided into 6 levels (0 - 5),
  237. * this means we need to convert the brightness
  238. * argument into the matching level within that range.
  239. */
  240. rt61pci_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  241. brightness / (LED_FULL / 6), 0);
  242. }
  243. }
  244. static int rt61pci_blink_set(struct led_classdev *led_cdev,
  245. unsigned long *delay_on,
  246. unsigned long *delay_off)
  247. {
  248. struct rt2x00_led *led =
  249. container_of(led_cdev, struct rt2x00_led, led_dev);
  250. u32 reg;
  251. rt2x00mmio_register_read(led->rt2x00dev, MAC_CSR14, &reg);
  252. rt2x00_set_field32(&reg, MAC_CSR14_ON_PERIOD, *delay_on);
  253. rt2x00_set_field32(&reg, MAC_CSR14_OFF_PERIOD, *delay_off);
  254. rt2x00mmio_register_write(led->rt2x00dev, MAC_CSR14, reg);
  255. return 0;
  256. }
  257. static void rt61pci_init_led(struct rt2x00_dev *rt2x00dev,
  258. struct rt2x00_led *led,
  259. enum led_type type)
  260. {
  261. led->rt2x00dev = rt2x00dev;
  262. led->type = type;
  263. led->led_dev.brightness_set = rt61pci_brightness_set;
  264. led->led_dev.blink_set = rt61pci_blink_set;
  265. led->flags = LED_INITIALIZED;
  266. }
  267. #endif /* CONFIG_RT2X00_LIB_LEDS */
  268. /*
  269. * Configuration handlers.
  270. */
  271. static int rt61pci_config_shared_key(struct rt2x00_dev *rt2x00dev,
  272. struct rt2x00lib_crypto *crypto,
  273. struct ieee80211_key_conf *key)
  274. {
  275. struct hw_key_entry key_entry;
  276. struct rt2x00_field32 field;
  277. u32 mask;
  278. u32 reg;
  279. if (crypto->cmd == SET_KEY) {
  280. /*
  281. * rt2x00lib can't determine the correct free
  282. * key_idx for shared keys. We have 1 register
  283. * with key valid bits. The goal is simple, read
  284. * the register, if that is full we have no slots
  285. * left.
  286. * Note that each BSS is allowed to have up to 4
  287. * shared keys, so put a mask over the allowed
  288. * entries.
  289. */
  290. mask = (0xf << crypto->bssidx);
  291. rt2x00mmio_register_read(rt2x00dev, SEC_CSR0, &reg);
  292. reg &= mask;
  293. if (reg && reg == mask)
  294. return -ENOSPC;
  295. key->hw_key_idx += reg ? ffz(reg) : 0;
  296. /*
  297. * Upload key to hardware
  298. */
  299. memcpy(key_entry.key, crypto->key,
  300. sizeof(key_entry.key));
  301. memcpy(key_entry.tx_mic, crypto->tx_mic,
  302. sizeof(key_entry.tx_mic));
  303. memcpy(key_entry.rx_mic, crypto->rx_mic,
  304. sizeof(key_entry.rx_mic));
  305. reg = SHARED_KEY_ENTRY(key->hw_key_idx);
  306. rt2x00mmio_register_multiwrite(rt2x00dev, reg,
  307. &key_entry, sizeof(key_entry));
  308. /*
  309. * The cipher types are stored over 2 registers.
  310. * bssidx 0 and 1 keys are stored in SEC_CSR1 and
  311. * bssidx 1 and 2 keys are stored in SEC_CSR5.
  312. * Using the correct defines correctly will cause overhead,
  313. * so just calculate the correct offset.
  314. */
  315. if (key->hw_key_idx < 8) {
  316. field.bit_offset = (3 * key->hw_key_idx);
  317. field.bit_mask = 0x7 << field.bit_offset;
  318. rt2x00mmio_register_read(rt2x00dev, SEC_CSR1, &reg);
  319. rt2x00_set_field32(&reg, field, crypto->cipher);
  320. rt2x00mmio_register_write(rt2x00dev, SEC_CSR1, reg);
  321. } else {
  322. field.bit_offset = (3 * (key->hw_key_idx - 8));
  323. field.bit_mask = 0x7 << field.bit_offset;
  324. rt2x00mmio_register_read(rt2x00dev, SEC_CSR5, &reg);
  325. rt2x00_set_field32(&reg, field, crypto->cipher);
  326. rt2x00mmio_register_write(rt2x00dev, SEC_CSR5, reg);
  327. }
  328. /*
  329. * The driver does not support the IV/EIV generation
  330. * in hardware. However it doesn't support the IV/EIV
  331. * inside the ieee80211 frame either, but requires it
  332. * to be provided separately for the descriptor.
  333. * rt2x00lib will cut the IV/EIV data out of all frames
  334. * given to us by mac80211, but we must tell mac80211
  335. * to generate the IV/EIV data.
  336. */
  337. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  338. }
  339. /*
  340. * SEC_CSR0 contains only single-bit fields to indicate
  341. * a particular key is valid. Because using the FIELD32()
  342. * defines directly will cause a lot of overhead, we use
  343. * a calculation to determine the correct bit directly.
  344. */
  345. mask = 1 << key->hw_key_idx;
  346. rt2x00mmio_register_read(rt2x00dev, SEC_CSR0, &reg);
  347. if (crypto->cmd == SET_KEY)
  348. reg |= mask;
  349. else if (crypto->cmd == DISABLE_KEY)
  350. reg &= ~mask;
  351. rt2x00mmio_register_write(rt2x00dev, SEC_CSR0, reg);
  352. return 0;
  353. }
  354. static int rt61pci_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  355. struct rt2x00lib_crypto *crypto,
  356. struct ieee80211_key_conf *key)
  357. {
  358. struct hw_pairwise_ta_entry addr_entry;
  359. struct hw_key_entry key_entry;
  360. u32 mask;
  361. u32 reg;
  362. if (crypto->cmd == SET_KEY) {
  363. /*
  364. * rt2x00lib can't determine the correct free
  365. * key_idx for pairwise keys. We have 2 registers
  366. * with key valid bits. The goal is simple: read
  367. * the first register. If that is full, move to
  368. * the next register.
  369. * When both registers are full, we drop the key.
  370. * Otherwise, we use the first invalid entry.
  371. */
  372. rt2x00mmio_register_read(rt2x00dev, SEC_CSR2, &reg);
  373. if (reg && reg == ~0) {
  374. key->hw_key_idx = 32;
  375. rt2x00mmio_register_read(rt2x00dev, SEC_CSR3, &reg);
  376. if (reg && reg == ~0)
  377. return -ENOSPC;
  378. }
  379. key->hw_key_idx += reg ? ffz(reg) : 0;
  380. /*
  381. * Upload key to hardware
  382. */
  383. memcpy(key_entry.key, crypto->key,
  384. sizeof(key_entry.key));
  385. memcpy(key_entry.tx_mic, crypto->tx_mic,
  386. sizeof(key_entry.tx_mic));
  387. memcpy(key_entry.rx_mic, crypto->rx_mic,
  388. sizeof(key_entry.rx_mic));
  389. memset(&addr_entry, 0, sizeof(addr_entry));
  390. memcpy(&addr_entry, crypto->address, ETH_ALEN);
  391. addr_entry.cipher = crypto->cipher;
  392. reg = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  393. rt2x00mmio_register_multiwrite(rt2x00dev, reg,
  394. &key_entry, sizeof(key_entry));
  395. reg = PAIRWISE_TA_ENTRY(key->hw_key_idx);
  396. rt2x00mmio_register_multiwrite(rt2x00dev, reg,
  397. &addr_entry, sizeof(addr_entry));
  398. /*
  399. * Enable pairwise lookup table for given BSS idx.
  400. * Without this, received frames will not be decrypted
  401. * by the hardware.
  402. */
  403. rt2x00mmio_register_read(rt2x00dev, SEC_CSR4, &reg);
  404. reg |= (1 << crypto->bssidx);
  405. rt2x00mmio_register_write(rt2x00dev, SEC_CSR4, reg);
  406. /*
  407. * The driver does not support the IV/EIV generation
  408. * in hardware. However it doesn't support the IV/EIV
  409. * inside the ieee80211 frame either, but requires it
  410. * to be provided separately for the descriptor.
  411. * rt2x00lib will cut the IV/EIV data out of all frames
  412. * given to us by mac80211, but we must tell mac80211
  413. * to generate the IV/EIV data.
  414. */
  415. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  416. }
  417. /*
  418. * SEC_CSR2 and SEC_CSR3 contain only single-bit fields to indicate
  419. * a particular key is valid. Because using the FIELD32()
  420. * defines directly will cause a lot of overhead, we use
  421. * a calculation to determine the correct bit directly.
  422. */
  423. if (key->hw_key_idx < 32) {
  424. mask = 1 << key->hw_key_idx;
  425. rt2x00mmio_register_read(rt2x00dev, SEC_CSR2, &reg);
  426. if (crypto->cmd == SET_KEY)
  427. reg |= mask;
  428. else if (crypto->cmd == DISABLE_KEY)
  429. reg &= ~mask;
  430. rt2x00mmio_register_write(rt2x00dev, SEC_CSR2, reg);
  431. } else {
  432. mask = 1 << (key->hw_key_idx - 32);
  433. rt2x00mmio_register_read(rt2x00dev, SEC_CSR3, &reg);
  434. if (crypto->cmd == SET_KEY)
  435. reg |= mask;
  436. else if (crypto->cmd == DISABLE_KEY)
  437. reg &= ~mask;
  438. rt2x00mmio_register_write(rt2x00dev, SEC_CSR3, reg);
  439. }
  440. return 0;
  441. }
  442. static void rt61pci_config_filter(struct rt2x00_dev *rt2x00dev,
  443. const unsigned int filter_flags)
  444. {
  445. u32 reg;
  446. /*
  447. * Start configuration steps.
  448. * Note that the version error will always be dropped
  449. * and broadcast frames will always be accepted since
  450. * there is no filter for it at this time.
  451. */
  452. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR0, &reg);
  453. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CRC,
  454. !(filter_flags & FIF_FCSFAIL));
  455. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_PHYSICAL,
  456. !(filter_flags & FIF_PLCPFAIL));
  457. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CONTROL,
  458. !(filter_flags & (FIF_CONTROL | FIF_PSPOLL)));
  459. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_NOT_TO_ME,
  460. !(filter_flags & FIF_PROMISC_IN_BSS));
  461. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_TO_DS,
  462. !(filter_flags & FIF_PROMISC_IN_BSS) &&
  463. !rt2x00dev->intf_ap_count);
  464. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_VERSION_ERROR, 1);
  465. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_MULTICAST,
  466. !(filter_flags & FIF_ALLMULTI));
  467. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_BROADCAST, 0);
  468. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_ACK_CTS,
  469. !(filter_flags & FIF_CONTROL));
  470. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR0, reg);
  471. }
  472. static void rt61pci_config_intf(struct rt2x00_dev *rt2x00dev,
  473. struct rt2x00_intf *intf,
  474. struct rt2x00intf_conf *conf,
  475. const unsigned int flags)
  476. {
  477. u32 reg;
  478. if (flags & CONFIG_UPDATE_TYPE) {
  479. /*
  480. * Enable synchronisation.
  481. */
  482. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  483. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, conf->sync);
  484. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  485. }
  486. if (flags & CONFIG_UPDATE_MAC) {
  487. reg = le32_to_cpu(conf->mac[1]);
  488. rt2x00_set_field32(&reg, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  489. conf->mac[1] = cpu_to_le32(reg);
  490. rt2x00mmio_register_multiwrite(rt2x00dev, MAC_CSR2,
  491. conf->mac, sizeof(conf->mac));
  492. }
  493. if (flags & CONFIG_UPDATE_BSSID) {
  494. reg = le32_to_cpu(conf->bssid[1]);
  495. rt2x00_set_field32(&reg, MAC_CSR5_BSS_ID_MASK, 3);
  496. conf->bssid[1] = cpu_to_le32(reg);
  497. rt2x00mmio_register_multiwrite(rt2x00dev, MAC_CSR4,
  498. conf->bssid,
  499. sizeof(conf->bssid));
  500. }
  501. }
  502. static void rt61pci_config_erp(struct rt2x00_dev *rt2x00dev,
  503. struct rt2x00lib_erp *erp,
  504. u32 changed)
  505. {
  506. u32 reg;
  507. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR0, &reg);
  508. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, 0x32);
  509. rt2x00_set_field32(&reg, TXRX_CSR0_TSF_OFFSET, IEEE80211_HEADER);
  510. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR0, reg);
  511. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  512. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR4, &reg);
  513. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_ENABLE, 1);
  514. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  515. !!erp->short_preamble);
  516. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR4, reg);
  517. }
  518. if (changed & BSS_CHANGED_BASIC_RATES)
  519. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR5,
  520. erp->basic_rates);
  521. if (changed & BSS_CHANGED_BEACON_INT) {
  522. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  523. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL,
  524. erp->beacon_int * 16);
  525. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  526. }
  527. if (changed & BSS_CHANGED_ERP_SLOT) {
  528. rt2x00mmio_register_read(rt2x00dev, MAC_CSR9, &reg);
  529. rt2x00_set_field32(&reg, MAC_CSR9_SLOT_TIME, erp->slot_time);
  530. rt2x00mmio_register_write(rt2x00dev, MAC_CSR9, reg);
  531. rt2x00mmio_register_read(rt2x00dev, MAC_CSR8, &reg);
  532. rt2x00_set_field32(&reg, MAC_CSR8_SIFS, erp->sifs);
  533. rt2x00_set_field32(&reg, MAC_CSR8_SIFS_AFTER_RX_OFDM, 3);
  534. rt2x00_set_field32(&reg, MAC_CSR8_EIFS, erp->eifs);
  535. rt2x00mmio_register_write(rt2x00dev, MAC_CSR8, reg);
  536. }
  537. }
  538. static void rt61pci_config_antenna_5x(struct rt2x00_dev *rt2x00dev,
  539. struct antenna_setup *ant)
  540. {
  541. u8 r3;
  542. u8 r4;
  543. u8 r77;
  544. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  545. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  546. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  547. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF5325));
  548. /*
  549. * Configure the RX antenna.
  550. */
  551. switch (ant->rx) {
  552. case ANTENNA_HW_DIVERSITY:
  553. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  554. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  555. (rt2x00dev->curr_band != IEEE80211_BAND_5GHZ));
  556. break;
  557. case ANTENNA_A:
  558. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  559. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  560. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  561. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  562. else
  563. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  564. break;
  565. case ANTENNA_B:
  566. default:
  567. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  568. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  569. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ)
  570. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  571. else
  572. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  573. break;
  574. }
  575. rt61pci_bbp_write(rt2x00dev, 77, r77);
  576. rt61pci_bbp_write(rt2x00dev, 3, r3);
  577. rt61pci_bbp_write(rt2x00dev, 4, r4);
  578. }
  579. static void rt61pci_config_antenna_2x(struct rt2x00_dev *rt2x00dev,
  580. struct antenna_setup *ant)
  581. {
  582. u8 r3;
  583. u8 r4;
  584. u8 r77;
  585. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  586. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  587. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  588. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, rt2x00_rf(rt2x00dev, RF2529));
  589. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  590. !test_bit(CAPABILITY_FRAME_TYPE, &rt2x00dev->cap_flags));
  591. /*
  592. * Configure the RX antenna.
  593. */
  594. switch (ant->rx) {
  595. case ANTENNA_HW_DIVERSITY:
  596. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  597. break;
  598. case ANTENNA_A:
  599. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  600. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  601. break;
  602. case ANTENNA_B:
  603. default:
  604. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  605. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  606. break;
  607. }
  608. rt61pci_bbp_write(rt2x00dev, 77, r77);
  609. rt61pci_bbp_write(rt2x00dev, 3, r3);
  610. rt61pci_bbp_write(rt2x00dev, 4, r4);
  611. }
  612. static void rt61pci_config_antenna_2529_rx(struct rt2x00_dev *rt2x00dev,
  613. const int p1, const int p2)
  614. {
  615. u32 reg;
  616. rt2x00mmio_register_read(rt2x00dev, MAC_CSR13, &reg);
  617. rt2x00_set_field32(&reg, MAC_CSR13_DIR4, 0);
  618. rt2x00_set_field32(&reg, MAC_CSR13_VAL4, p1);
  619. rt2x00_set_field32(&reg, MAC_CSR13_DIR3, 0);
  620. rt2x00_set_field32(&reg, MAC_CSR13_VAL3, !p2);
  621. rt2x00mmio_register_write(rt2x00dev, MAC_CSR13, reg);
  622. }
  623. static void rt61pci_config_antenna_2529(struct rt2x00_dev *rt2x00dev,
  624. struct antenna_setup *ant)
  625. {
  626. u8 r3;
  627. u8 r4;
  628. u8 r77;
  629. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  630. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  631. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  632. /*
  633. * Configure the RX antenna.
  634. */
  635. switch (ant->rx) {
  636. case ANTENNA_A:
  637. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  638. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  639. rt61pci_config_antenna_2529_rx(rt2x00dev, 0, 0);
  640. break;
  641. case ANTENNA_HW_DIVERSITY:
  642. /*
  643. * FIXME: Antenna selection for the rf 2529 is very confusing
  644. * in the legacy driver. Just default to antenna B until the
  645. * legacy code can be properly translated into rt2x00 code.
  646. */
  647. case ANTENNA_B:
  648. default:
  649. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  650. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  651. rt61pci_config_antenna_2529_rx(rt2x00dev, 1, 1);
  652. break;
  653. }
  654. rt61pci_bbp_write(rt2x00dev, 77, r77);
  655. rt61pci_bbp_write(rt2x00dev, 3, r3);
  656. rt61pci_bbp_write(rt2x00dev, 4, r4);
  657. }
  658. struct antenna_sel {
  659. u8 word;
  660. /*
  661. * value[0] -> non-LNA
  662. * value[1] -> LNA
  663. */
  664. u8 value[2];
  665. };
  666. static const struct antenna_sel antenna_sel_a[] = {
  667. { 96, { 0x58, 0x78 } },
  668. { 104, { 0x38, 0x48 } },
  669. { 75, { 0xfe, 0x80 } },
  670. { 86, { 0xfe, 0x80 } },
  671. { 88, { 0xfe, 0x80 } },
  672. { 35, { 0x60, 0x60 } },
  673. { 97, { 0x58, 0x58 } },
  674. { 98, { 0x58, 0x58 } },
  675. };
  676. static const struct antenna_sel antenna_sel_bg[] = {
  677. { 96, { 0x48, 0x68 } },
  678. { 104, { 0x2c, 0x3c } },
  679. { 75, { 0xfe, 0x80 } },
  680. { 86, { 0xfe, 0x80 } },
  681. { 88, { 0xfe, 0x80 } },
  682. { 35, { 0x50, 0x50 } },
  683. { 97, { 0x48, 0x48 } },
  684. { 98, { 0x48, 0x48 } },
  685. };
  686. static void rt61pci_config_ant(struct rt2x00_dev *rt2x00dev,
  687. struct antenna_setup *ant)
  688. {
  689. const struct antenna_sel *sel;
  690. unsigned int lna;
  691. unsigned int i;
  692. u32 reg;
  693. /*
  694. * We should never come here because rt2x00lib is supposed
  695. * to catch this and send us the correct antenna explicitely.
  696. */
  697. BUG_ON(ant->rx == ANTENNA_SW_DIVERSITY ||
  698. ant->tx == ANTENNA_SW_DIVERSITY);
  699. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  700. sel = antenna_sel_a;
  701. lna = test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
  702. } else {
  703. sel = antenna_sel_bg;
  704. lna = test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
  705. }
  706. for (i = 0; i < ARRAY_SIZE(antenna_sel_a); i++)
  707. rt61pci_bbp_write(rt2x00dev, sel[i].word, sel[i].value[lna]);
  708. rt2x00mmio_register_read(rt2x00dev, PHY_CSR0, &reg);
  709. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_BG,
  710. rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  711. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_A,
  712. rt2x00dev->curr_band == IEEE80211_BAND_5GHZ);
  713. rt2x00mmio_register_write(rt2x00dev, PHY_CSR0, reg);
  714. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325))
  715. rt61pci_config_antenna_5x(rt2x00dev, ant);
  716. else if (rt2x00_rf(rt2x00dev, RF2527))
  717. rt61pci_config_antenna_2x(rt2x00dev, ant);
  718. else if (rt2x00_rf(rt2x00dev, RF2529)) {
  719. if (test_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags))
  720. rt61pci_config_antenna_2x(rt2x00dev, ant);
  721. else
  722. rt61pci_config_antenna_2529(rt2x00dev, ant);
  723. }
  724. }
  725. static void rt61pci_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  726. struct rt2x00lib_conf *libconf)
  727. {
  728. u16 eeprom;
  729. short lna_gain = 0;
  730. if (libconf->conf->chandef.chan->band == IEEE80211_BAND_2GHZ) {
  731. if (test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags))
  732. lna_gain += 14;
  733. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  734. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  735. } else {
  736. if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags))
  737. lna_gain += 14;
  738. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  739. lna_gain -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  740. }
  741. rt2x00dev->lna_gain = lna_gain;
  742. }
  743. static void rt61pci_config_channel(struct rt2x00_dev *rt2x00dev,
  744. struct rf_channel *rf, const int txpower)
  745. {
  746. u8 r3;
  747. u8 r94;
  748. u8 smart;
  749. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
  750. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  751. smart = !(rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF2527));
  752. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  753. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, smart);
  754. rt61pci_bbp_write(rt2x00dev, 3, r3);
  755. r94 = 6;
  756. if (txpower > MAX_TXPOWER && txpower <= (MAX_TXPOWER + r94))
  757. r94 += txpower - MAX_TXPOWER;
  758. else if (txpower < MIN_TXPOWER && txpower >= (MIN_TXPOWER - r94))
  759. r94 += txpower;
  760. rt61pci_bbp_write(rt2x00dev, 94, r94);
  761. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  762. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  763. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  764. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  765. udelay(200);
  766. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  767. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  768. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  769. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  770. udelay(200);
  771. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  772. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  773. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  774. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  775. msleep(1);
  776. }
  777. static void rt61pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  778. const int txpower)
  779. {
  780. struct rf_channel rf;
  781. rt2x00_rf_read(rt2x00dev, 1, &rf.rf1);
  782. rt2x00_rf_read(rt2x00dev, 2, &rf.rf2);
  783. rt2x00_rf_read(rt2x00dev, 3, &rf.rf3);
  784. rt2x00_rf_read(rt2x00dev, 4, &rf.rf4);
  785. rt61pci_config_channel(rt2x00dev, &rf, txpower);
  786. }
  787. static void rt61pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  788. struct rt2x00lib_conf *libconf)
  789. {
  790. u32 reg;
  791. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR4, &reg);
  792. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_DOWN, 1);
  793. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_RATE_STEP, 0);
  794. rt2x00_set_field32(&reg, TXRX_CSR4_OFDM_TX_FALLBACK_CCK, 0);
  795. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT,
  796. libconf->conf->long_frame_max_tx_count);
  797. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT,
  798. libconf->conf->short_frame_max_tx_count);
  799. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR4, reg);
  800. }
  801. static void rt61pci_config_ps(struct rt2x00_dev *rt2x00dev,
  802. struct rt2x00lib_conf *libconf)
  803. {
  804. enum dev_state state =
  805. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  806. STATE_SLEEP : STATE_AWAKE;
  807. u32 reg;
  808. if (state == STATE_SLEEP) {
  809. rt2x00mmio_register_read(rt2x00dev, MAC_CSR11, &reg);
  810. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN,
  811. rt2x00dev->beacon_int - 10);
  812. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP,
  813. libconf->conf->listen_interval - 1);
  814. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 5);
  815. /* We must first disable autowake before it can be enabled */
  816. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  817. rt2x00mmio_register_write(rt2x00dev, MAC_CSR11, reg);
  818. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 1);
  819. rt2x00mmio_register_write(rt2x00dev, MAC_CSR11, reg);
  820. rt2x00mmio_register_write(rt2x00dev, SOFT_RESET_CSR,
  821. 0x00000005);
  822. rt2x00mmio_register_write(rt2x00dev, IO_CNTL_CSR, 0x0000001c);
  823. rt2x00mmio_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000060);
  824. rt61pci_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 0);
  825. } else {
  826. rt2x00mmio_register_read(rt2x00dev, MAC_CSR11, &reg);
  827. rt2x00_set_field32(&reg, MAC_CSR11_DELAY_AFTER_TBCN, 0);
  828. rt2x00_set_field32(&reg, MAC_CSR11_TBCN_BEFORE_WAKEUP, 0);
  829. rt2x00_set_field32(&reg, MAC_CSR11_AUTOWAKE, 0);
  830. rt2x00_set_field32(&reg, MAC_CSR11_WAKEUP_LATENCY, 0);
  831. rt2x00mmio_register_write(rt2x00dev, MAC_CSR11, reg);
  832. rt2x00mmio_register_write(rt2x00dev, SOFT_RESET_CSR,
  833. 0x00000007);
  834. rt2x00mmio_register_write(rt2x00dev, IO_CNTL_CSR, 0x00000018);
  835. rt2x00mmio_register_write(rt2x00dev, PCI_USEC_CSR, 0x00000020);
  836. rt61pci_mcu_request(rt2x00dev, MCU_WAKEUP, 0xff, 0, 0);
  837. }
  838. }
  839. static void rt61pci_config(struct rt2x00_dev *rt2x00dev,
  840. struct rt2x00lib_conf *libconf,
  841. const unsigned int flags)
  842. {
  843. /* Always recalculate LNA gain before changing configuration */
  844. rt61pci_config_lna_gain(rt2x00dev, libconf);
  845. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  846. rt61pci_config_channel(rt2x00dev, &libconf->rf,
  847. libconf->conf->power_level);
  848. if ((flags & IEEE80211_CONF_CHANGE_POWER) &&
  849. !(flags & IEEE80211_CONF_CHANGE_CHANNEL))
  850. rt61pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  851. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  852. rt61pci_config_retry_limit(rt2x00dev, libconf);
  853. if (flags & IEEE80211_CONF_CHANGE_PS)
  854. rt61pci_config_ps(rt2x00dev, libconf);
  855. }
  856. /*
  857. * Link tuning
  858. */
  859. static void rt61pci_link_stats(struct rt2x00_dev *rt2x00dev,
  860. struct link_qual *qual)
  861. {
  862. u32 reg;
  863. /*
  864. * Update FCS error count from register.
  865. */
  866. rt2x00mmio_register_read(rt2x00dev, STA_CSR0, &reg);
  867. qual->rx_failed = rt2x00_get_field32(reg, STA_CSR0_FCS_ERROR);
  868. /*
  869. * Update False CCA count from register.
  870. */
  871. rt2x00mmio_register_read(rt2x00dev, STA_CSR1, &reg);
  872. qual->false_cca = rt2x00_get_field32(reg, STA_CSR1_FALSE_CCA_ERROR);
  873. }
  874. static inline void rt61pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  875. struct link_qual *qual, u8 vgc_level)
  876. {
  877. if (qual->vgc_level != vgc_level) {
  878. rt61pci_bbp_write(rt2x00dev, 17, vgc_level);
  879. qual->vgc_level = vgc_level;
  880. qual->vgc_level_reg = vgc_level;
  881. }
  882. }
  883. static void rt61pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  884. struct link_qual *qual)
  885. {
  886. rt61pci_set_vgc(rt2x00dev, qual, 0x20);
  887. }
  888. static void rt61pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  889. struct link_qual *qual, const u32 count)
  890. {
  891. u8 up_bound;
  892. u8 low_bound;
  893. /*
  894. * Determine r17 bounds.
  895. */
  896. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  897. low_bound = 0x28;
  898. up_bound = 0x48;
  899. if (test_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags)) {
  900. low_bound += 0x10;
  901. up_bound += 0x10;
  902. }
  903. } else {
  904. low_bound = 0x20;
  905. up_bound = 0x40;
  906. if (test_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags)) {
  907. low_bound += 0x10;
  908. up_bound += 0x10;
  909. }
  910. }
  911. /*
  912. * If we are not associated, we should go straight to the
  913. * dynamic CCA tuning.
  914. */
  915. if (!rt2x00dev->intf_associated)
  916. goto dynamic_cca_tune;
  917. /*
  918. * Special big-R17 for very short distance
  919. */
  920. if (qual->rssi >= -35) {
  921. rt61pci_set_vgc(rt2x00dev, qual, 0x60);
  922. return;
  923. }
  924. /*
  925. * Special big-R17 for short distance
  926. */
  927. if (qual->rssi >= -58) {
  928. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  929. return;
  930. }
  931. /*
  932. * Special big-R17 for middle-short distance
  933. */
  934. if (qual->rssi >= -66) {
  935. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x10);
  936. return;
  937. }
  938. /*
  939. * Special mid-R17 for middle distance
  940. */
  941. if (qual->rssi >= -74) {
  942. rt61pci_set_vgc(rt2x00dev, qual, low_bound + 0x08);
  943. return;
  944. }
  945. /*
  946. * Special case: Change up_bound based on the rssi.
  947. * Lower up_bound when rssi is weaker then -74 dBm.
  948. */
  949. up_bound -= 2 * (-74 - qual->rssi);
  950. if (low_bound > up_bound)
  951. up_bound = low_bound;
  952. if (qual->vgc_level > up_bound) {
  953. rt61pci_set_vgc(rt2x00dev, qual, up_bound);
  954. return;
  955. }
  956. dynamic_cca_tune:
  957. /*
  958. * r17 does not yet exceed upper limit, continue and base
  959. * the r17 tuning on the false CCA count.
  960. */
  961. if ((qual->false_cca > 512) && (qual->vgc_level < up_bound))
  962. rt61pci_set_vgc(rt2x00dev, qual, ++qual->vgc_level);
  963. else if ((qual->false_cca < 100) && (qual->vgc_level > low_bound))
  964. rt61pci_set_vgc(rt2x00dev, qual, --qual->vgc_level);
  965. }
  966. /*
  967. * Queue handlers.
  968. */
  969. static void rt61pci_start_queue(struct data_queue *queue)
  970. {
  971. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  972. u32 reg;
  973. switch (queue->qid) {
  974. case QID_RX:
  975. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR0, &reg);
  976. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  977. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR0, reg);
  978. break;
  979. case QID_BEACON:
  980. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  981. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  982. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 1);
  983. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  984. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  985. break;
  986. default:
  987. break;
  988. }
  989. }
  990. static void rt61pci_kick_queue(struct data_queue *queue)
  991. {
  992. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  993. u32 reg;
  994. switch (queue->qid) {
  995. case QID_AC_VO:
  996. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  997. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC0, 1);
  998. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  999. break;
  1000. case QID_AC_VI:
  1001. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1002. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC1, 1);
  1003. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1004. break;
  1005. case QID_AC_BE:
  1006. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1007. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC2, 1);
  1008. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1009. break;
  1010. case QID_AC_BK:
  1011. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1012. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC3, 1);
  1013. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1014. break;
  1015. default:
  1016. break;
  1017. }
  1018. }
  1019. static void rt61pci_stop_queue(struct data_queue *queue)
  1020. {
  1021. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  1022. u32 reg;
  1023. switch (queue->qid) {
  1024. case QID_AC_VO:
  1025. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1026. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC0, 1);
  1027. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1028. break;
  1029. case QID_AC_VI:
  1030. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1031. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC1, 1);
  1032. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1033. break;
  1034. case QID_AC_BE:
  1035. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1036. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC2, 1);
  1037. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1038. break;
  1039. case QID_AC_BK:
  1040. rt2x00mmio_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1041. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC3, 1);
  1042. rt2x00mmio_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1043. break;
  1044. case QID_RX:
  1045. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1046. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 1);
  1047. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR0, reg);
  1048. break;
  1049. case QID_BEACON:
  1050. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1051. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1052. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1053. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1054. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1055. /*
  1056. * Wait for possibly running tbtt tasklets.
  1057. */
  1058. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1059. break;
  1060. default:
  1061. break;
  1062. }
  1063. }
  1064. /*
  1065. * Firmware functions
  1066. */
  1067. static char *rt61pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  1068. {
  1069. u16 chip;
  1070. char *fw_name;
  1071. pci_read_config_word(to_pci_dev(rt2x00dev->dev), PCI_DEVICE_ID, &chip);
  1072. switch (chip) {
  1073. case RT2561_PCI_ID:
  1074. fw_name = FIRMWARE_RT2561;
  1075. break;
  1076. case RT2561s_PCI_ID:
  1077. fw_name = FIRMWARE_RT2561s;
  1078. break;
  1079. case RT2661_PCI_ID:
  1080. fw_name = FIRMWARE_RT2661;
  1081. break;
  1082. default:
  1083. fw_name = NULL;
  1084. break;
  1085. }
  1086. return fw_name;
  1087. }
  1088. static int rt61pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  1089. const u8 *data, const size_t len)
  1090. {
  1091. u16 fw_crc;
  1092. u16 crc;
  1093. /*
  1094. * Only support 8kb firmware files.
  1095. */
  1096. if (len != 8192)
  1097. return FW_BAD_LENGTH;
  1098. /*
  1099. * The last 2 bytes in the firmware array are the crc checksum itself.
  1100. * This means that we should never pass those 2 bytes to the crc
  1101. * algorithm.
  1102. */
  1103. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  1104. /*
  1105. * Use the crc itu-t algorithm.
  1106. */
  1107. crc = crc_itu_t(0, data, len - 2);
  1108. crc = crc_itu_t_byte(crc, 0);
  1109. crc = crc_itu_t_byte(crc, 0);
  1110. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  1111. }
  1112. static int rt61pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  1113. const u8 *data, const size_t len)
  1114. {
  1115. int i;
  1116. u32 reg;
  1117. /*
  1118. * Wait for stable hardware.
  1119. */
  1120. for (i = 0; i < 100; i++) {
  1121. rt2x00mmio_register_read(rt2x00dev, MAC_CSR0, &reg);
  1122. if (reg)
  1123. break;
  1124. msleep(1);
  1125. }
  1126. if (!reg) {
  1127. rt2x00_err(rt2x00dev, "Unstable hardware\n");
  1128. return -EBUSY;
  1129. }
  1130. /*
  1131. * Prepare MCU and mailbox for firmware loading.
  1132. */
  1133. reg = 0;
  1134. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1135. rt2x00mmio_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1136. rt2x00mmio_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1137. rt2x00mmio_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1138. rt2x00mmio_register_write(rt2x00dev, HOST_CMD_CSR, 0);
  1139. /*
  1140. * Write firmware to device.
  1141. */
  1142. reg = 0;
  1143. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  1144. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 1);
  1145. rt2x00mmio_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1146. rt2x00mmio_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  1147. data, len);
  1148. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 0);
  1149. rt2x00mmio_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1150. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 0);
  1151. rt2x00mmio_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  1152. for (i = 0; i < 100; i++) {
  1153. rt2x00mmio_register_read(rt2x00dev, MCU_CNTL_CSR, &reg);
  1154. if (rt2x00_get_field32(reg, MCU_CNTL_CSR_READY))
  1155. break;
  1156. msleep(1);
  1157. }
  1158. if (i == 100) {
  1159. rt2x00_err(rt2x00dev, "MCU Control register not ready\n");
  1160. return -EBUSY;
  1161. }
  1162. /*
  1163. * Hardware needs another millisecond before it is ready.
  1164. */
  1165. msleep(1);
  1166. /*
  1167. * Reset MAC and BBP registers.
  1168. */
  1169. reg = 0;
  1170. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1171. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1172. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1173. rt2x00mmio_register_read(rt2x00dev, MAC_CSR1, &reg);
  1174. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1175. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1176. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1177. rt2x00mmio_register_read(rt2x00dev, MAC_CSR1, &reg);
  1178. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1179. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1180. return 0;
  1181. }
  1182. /*
  1183. * Initialization functions.
  1184. */
  1185. static bool rt61pci_get_entry_state(struct queue_entry *entry)
  1186. {
  1187. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  1188. u32 word;
  1189. if (entry->queue->qid == QID_RX) {
  1190. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1191. return rt2x00_get_field32(word, RXD_W0_OWNER_NIC);
  1192. } else {
  1193. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1194. return (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1195. rt2x00_get_field32(word, TXD_W0_VALID));
  1196. }
  1197. }
  1198. static void rt61pci_clear_entry(struct queue_entry *entry)
  1199. {
  1200. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  1201. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1202. u32 word;
  1203. if (entry->queue->qid == QID_RX) {
  1204. rt2x00_desc_read(entry_priv->desc, 5, &word);
  1205. rt2x00_set_field32(&word, RXD_W5_BUFFER_PHYSICAL_ADDRESS,
  1206. skbdesc->skb_dma);
  1207. rt2x00_desc_write(entry_priv->desc, 5, word);
  1208. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1209. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  1210. rt2x00_desc_write(entry_priv->desc, 0, word);
  1211. } else {
  1212. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1213. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  1214. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  1215. rt2x00_desc_write(entry_priv->desc, 0, word);
  1216. }
  1217. }
  1218. static int rt61pci_init_queues(struct rt2x00_dev *rt2x00dev)
  1219. {
  1220. struct queue_entry_priv_mmio *entry_priv;
  1221. u32 reg;
  1222. /*
  1223. * Initialize registers.
  1224. */
  1225. rt2x00mmio_register_read(rt2x00dev, TX_RING_CSR0, &reg);
  1226. rt2x00_set_field32(&reg, TX_RING_CSR0_AC0_RING_SIZE,
  1227. rt2x00dev->tx[0].limit);
  1228. rt2x00_set_field32(&reg, TX_RING_CSR0_AC1_RING_SIZE,
  1229. rt2x00dev->tx[1].limit);
  1230. rt2x00_set_field32(&reg, TX_RING_CSR0_AC2_RING_SIZE,
  1231. rt2x00dev->tx[2].limit);
  1232. rt2x00_set_field32(&reg, TX_RING_CSR0_AC3_RING_SIZE,
  1233. rt2x00dev->tx[3].limit);
  1234. rt2x00mmio_register_write(rt2x00dev, TX_RING_CSR0, reg);
  1235. rt2x00mmio_register_read(rt2x00dev, TX_RING_CSR1, &reg);
  1236. rt2x00_set_field32(&reg, TX_RING_CSR1_TXD_SIZE,
  1237. rt2x00dev->tx[0].desc_size / 4);
  1238. rt2x00mmio_register_write(rt2x00dev, TX_RING_CSR1, reg);
  1239. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  1240. rt2x00mmio_register_read(rt2x00dev, AC0_BASE_CSR, &reg);
  1241. rt2x00_set_field32(&reg, AC0_BASE_CSR_RING_REGISTER,
  1242. entry_priv->desc_dma);
  1243. rt2x00mmio_register_write(rt2x00dev, AC0_BASE_CSR, reg);
  1244. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  1245. rt2x00mmio_register_read(rt2x00dev, AC1_BASE_CSR, &reg);
  1246. rt2x00_set_field32(&reg, AC1_BASE_CSR_RING_REGISTER,
  1247. entry_priv->desc_dma);
  1248. rt2x00mmio_register_write(rt2x00dev, AC1_BASE_CSR, reg);
  1249. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  1250. rt2x00mmio_register_read(rt2x00dev, AC2_BASE_CSR, &reg);
  1251. rt2x00_set_field32(&reg, AC2_BASE_CSR_RING_REGISTER,
  1252. entry_priv->desc_dma);
  1253. rt2x00mmio_register_write(rt2x00dev, AC2_BASE_CSR, reg);
  1254. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  1255. rt2x00mmio_register_read(rt2x00dev, AC3_BASE_CSR, &reg);
  1256. rt2x00_set_field32(&reg, AC3_BASE_CSR_RING_REGISTER,
  1257. entry_priv->desc_dma);
  1258. rt2x00mmio_register_write(rt2x00dev, AC3_BASE_CSR, reg);
  1259. rt2x00mmio_register_read(rt2x00dev, RX_RING_CSR, &reg);
  1260. rt2x00_set_field32(&reg, RX_RING_CSR_RING_SIZE, rt2x00dev->rx->limit);
  1261. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_SIZE,
  1262. rt2x00dev->rx->desc_size / 4);
  1263. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_WRITEBACK_SIZE, 4);
  1264. rt2x00mmio_register_write(rt2x00dev, RX_RING_CSR, reg);
  1265. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  1266. rt2x00mmio_register_read(rt2x00dev, RX_BASE_CSR, &reg);
  1267. rt2x00_set_field32(&reg, RX_BASE_CSR_RING_REGISTER,
  1268. entry_priv->desc_dma);
  1269. rt2x00mmio_register_write(rt2x00dev, RX_BASE_CSR, reg);
  1270. rt2x00mmio_register_read(rt2x00dev, TX_DMA_DST_CSR, &reg);
  1271. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC0, 2);
  1272. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC1, 2);
  1273. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC2, 2);
  1274. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC3, 2);
  1275. rt2x00mmio_register_write(rt2x00dev, TX_DMA_DST_CSR, reg);
  1276. rt2x00mmio_register_read(rt2x00dev, LOAD_TX_RING_CSR, &reg);
  1277. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC0, 1);
  1278. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC1, 1);
  1279. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC2, 1);
  1280. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC3, 1);
  1281. rt2x00mmio_register_write(rt2x00dev, LOAD_TX_RING_CSR, reg);
  1282. rt2x00mmio_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1283. rt2x00_set_field32(&reg, RX_CNTL_CSR_LOAD_RXD, 1);
  1284. rt2x00mmio_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1285. return 0;
  1286. }
  1287. static int rt61pci_init_registers(struct rt2x00_dev *rt2x00dev)
  1288. {
  1289. u32 reg;
  1290. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1291. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  1292. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  1293. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  1294. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR0, reg);
  1295. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR1, &reg);
  1296. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  1297. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  1298. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  1299. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  1300. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  1301. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  1302. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  1303. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  1304. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR1, reg);
  1305. /*
  1306. * CCK TXD BBP registers
  1307. */
  1308. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR2, &reg);
  1309. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  1310. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  1311. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  1312. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  1313. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  1314. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  1315. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  1316. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  1317. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR2, reg);
  1318. /*
  1319. * OFDM TXD BBP registers
  1320. */
  1321. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR3, &reg);
  1322. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  1323. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  1324. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  1325. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  1326. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  1327. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  1328. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR3, reg);
  1329. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR7, &reg);
  1330. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  1331. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  1332. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  1333. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  1334. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR7, reg);
  1335. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR8, &reg);
  1336. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  1337. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  1338. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  1339. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  1340. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR8, reg);
  1341. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1342. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL, 0);
  1343. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 0);
  1344. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, 0);
  1345. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE, 0);
  1346. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1347. rt2x00_set_field32(&reg, TXRX_CSR9_TIMESTAMP_COMPENSATE, 0);
  1348. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1349. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  1350. rt2x00mmio_register_write(rt2x00dev, MAC_CSR6, 0x00000fff);
  1351. rt2x00mmio_register_read(rt2x00dev, MAC_CSR9, &reg);
  1352. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  1353. rt2x00mmio_register_write(rt2x00dev, MAC_CSR9, reg);
  1354. rt2x00mmio_register_write(rt2x00dev, MAC_CSR10, 0x0000071c);
  1355. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  1356. return -EBUSY;
  1357. rt2x00mmio_register_write(rt2x00dev, MAC_CSR13, 0x0000e000);
  1358. /*
  1359. * Invalidate all Shared Keys (SEC_CSR0),
  1360. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  1361. */
  1362. rt2x00mmio_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  1363. rt2x00mmio_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  1364. rt2x00mmio_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  1365. rt2x00mmio_register_write(rt2x00dev, PHY_CSR1, 0x000023b0);
  1366. rt2x00mmio_register_write(rt2x00dev, PHY_CSR5, 0x060a100c);
  1367. rt2x00mmio_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  1368. rt2x00mmio_register_write(rt2x00dev, PHY_CSR7, 0x00000a08);
  1369. rt2x00mmio_register_write(rt2x00dev, PCI_CFG_CSR, 0x28ca4404);
  1370. rt2x00mmio_register_write(rt2x00dev, TEST_MODE_CSR, 0x00000200);
  1371. rt2x00mmio_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1372. /*
  1373. * Clear all beacons
  1374. * For the Beacon base registers we only need to clear
  1375. * the first byte since that byte contains the VALID and OWNER
  1376. * bits which (when set to 0) will invalidate the entire beacon.
  1377. */
  1378. rt2x00mmio_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1379. rt2x00mmio_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1380. rt2x00mmio_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1381. rt2x00mmio_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1382. /*
  1383. * We must clear the error counters.
  1384. * These registers are cleared on read,
  1385. * so we may pass a useless variable to store the value.
  1386. */
  1387. rt2x00mmio_register_read(rt2x00dev, STA_CSR0, &reg);
  1388. rt2x00mmio_register_read(rt2x00dev, STA_CSR1, &reg);
  1389. rt2x00mmio_register_read(rt2x00dev, STA_CSR2, &reg);
  1390. /*
  1391. * Reset MAC and BBP registers.
  1392. */
  1393. rt2x00mmio_register_read(rt2x00dev, MAC_CSR1, &reg);
  1394. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1395. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1396. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1397. rt2x00mmio_register_read(rt2x00dev, MAC_CSR1, &reg);
  1398. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1399. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1400. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1401. rt2x00mmio_register_read(rt2x00dev, MAC_CSR1, &reg);
  1402. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1403. rt2x00mmio_register_write(rt2x00dev, MAC_CSR1, reg);
  1404. return 0;
  1405. }
  1406. static int rt61pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  1407. {
  1408. unsigned int i;
  1409. u8 value;
  1410. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1411. rt61pci_bbp_read(rt2x00dev, 0, &value);
  1412. if ((value != 0xff) && (value != 0x00))
  1413. return 0;
  1414. udelay(REGISTER_BUSY_DELAY);
  1415. }
  1416. rt2x00_err(rt2x00dev, "BBP register access failed, aborting\n");
  1417. return -EACCES;
  1418. }
  1419. static int rt61pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1420. {
  1421. unsigned int i;
  1422. u16 eeprom;
  1423. u8 reg_id;
  1424. u8 value;
  1425. if (unlikely(rt61pci_wait_bbp_ready(rt2x00dev)))
  1426. return -EACCES;
  1427. rt61pci_bbp_write(rt2x00dev, 3, 0x00);
  1428. rt61pci_bbp_write(rt2x00dev, 15, 0x30);
  1429. rt61pci_bbp_write(rt2x00dev, 21, 0xc8);
  1430. rt61pci_bbp_write(rt2x00dev, 22, 0x38);
  1431. rt61pci_bbp_write(rt2x00dev, 23, 0x06);
  1432. rt61pci_bbp_write(rt2x00dev, 24, 0xfe);
  1433. rt61pci_bbp_write(rt2x00dev, 25, 0x0a);
  1434. rt61pci_bbp_write(rt2x00dev, 26, 0x0d);
  1435. rt61pci_bbp_write(rt2x00dev, 34, 0x12);
  1436. rt61pci_bbp_write(rt2x00dev, 37, 0x07);
  1437. rt61pci_bbp_write(rt2x00dev, 39, 0xf8);
  1438. rt61pci_bbp_write(rt2x00dev, 41, 0x60);
  1439. rt61pci_bbp_write(rt2x00dev, 53, 0x10);
  1440. rt61pci_bbp_write(rt2x00dev, 54, 0x18);
  1441. rt61pci_bbp_write(rt2x00dev, 60, 0x10);
  1442. rt61pci_bbp_write(rt2x00dev, 61, 0x04);
  1443. rt61pci_bbp_write(rt2x00dev, 62, 0x04);
  1444. rt61pci_bbp_write(rt2x00dev, 75, 0xfe);
  1445. rt61pci_bbp_write(rt2x00dev, 86, 0xfe);
  1446. rt61pci_bbp_write(rt2x00dev, 88, 0xfe);
  1447. rt61pci_bbp_write(rt2x00dev, 90, 0x0f);
  1448. rt61pci_bbp_write(rt2x00dev, 99, 0x00);
  1449. rt61pci_bbp_write(rt2x00dev, 102, 0x16);
  1450. rt61pci_bbp_write(rt2x00dev, 107, 0x04);
  1451. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1452. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1453. if (eeprom != 0xffff && eeprom != 0x0000) {
  1454. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1455. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1456. rt61pci_bbp_write(rt2x00dev, reg_id, value);
  1457. }
  1458. }
  1459. return 0;
  1460. }
  1461. /*
  1462. * Device state switch handlers.
  1463. */
  1464. static void rt61pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1465. enum dev_state state)
  1466. {
  1467. int mask = (state == STATE_RADIO_IRQ_OFF);
  1468. u32 reg;
  1469. unsigned long flags;
  1470. /*
  1471. * When interrupts are being enabled, the interrupt registers
  1472. * should clear the register to assure a clean state.
  1473. */
  1474. if (state == STATE_RADIO_IRQ_ON) {
  1475. rt2x00mmio_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1476. rt2x00mmio_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1477. rt2x00mmio_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg);
  1478. rt2x00mmio_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg);
  1479. }
  1480. /*
  1481. * Only toggle the interrupts bits we are going to use.
  1482. * Non-checked interrupt bits are disabled by default.
  1483. */
  1484. spin_lock_irqsave(&rt2x00dev->irqmask_lock, flags);
  1485. rt2x00mmio_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1486. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDONE, mask);
  1487. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDONE, mask);
  1488. rt2x00_set_field32(&reg, INT_MASK_CSR_BEACON_DONE, mask);
  1489. rt2x00_set_field32(&reg, INT_MASK_CSR_ENABLE_MITIGATION, mask);
  1490. rt2x00_set_field32(&reg, INT_MASK_CSR_MITIGATION_PERIOD, 0xff);
  1491. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1492. rt2x00mmio_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1493. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_0, mask);
  1494. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_1, mask);
  1495. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_2, mask);
  1496. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_3, mask);
  1497. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_4, mask);
  1498. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_5, mask);
  1499. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_6, mask);
  1500. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_7, mask);
  1501. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_TWAKEUP, mask);
  1502. rt2x00mmio_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1503. spin_unlock_irqrestore(&rt2x00dev->irqmask_lock, flags);
  1504. if (state == STATE_RADIO_IRQ_OFF) {
  1505. /*
  1506. * Ensure that all tasklets are finished.
  1507. */
  1508. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1509. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1510. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1511. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1512. }
  1513. }
  1514. static int rt61pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1515. {
  1516. u32 reg;
  1517. /*
  1518. * Initialize all registers.
  1519. */
  1520. if (unlikely(rt61pci_init_queues(rt2x00dev) ||
  1521. rt61pci_init_registers(rt2x00dev) ||
  1522. rt61pci_init_bbp(rt2x00dev)))
  1523. return -EIO;
  1524. /*
  1525. * Enable RX.
  1526. */
  1527. rt2x00mmio_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1528. rt2x00_set_field32(&reg, RX_CNTL_CSR_ENABLE_RX_DMA, 1);
  1529. rt2x00mmio_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1530. return 0;
  1531. }
  1532. static void rt61pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1533. {
  1534. /*
  1535. * Disable power
  1536. */
  1537. rt2x00mmio_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  1538. }
  1539. static int rt61pci_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  1540. {
  1541. u32 reg, reg2;
  1542. unsigned int i;
  1543. char put_to_sleep;
  1544. put_to_sleep = (state != STATE_AWAKE);
  1545. rt2x00mmio_register_read(rt2x00dev, MAC_CSR12, &reg);
  1546. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  1547. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  1548. rt2x00mmio_register_write(rt2x00dev, MAC_CSR12, reg);
  1549. /*
  1550. * Device is not guaranteed to be in the requested state yet.
  1551. * We must wait until the register indicates that the
  1552. * device has entered the correct state.
  1553. */
  1554. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1555. rt2x00mmio_register_read(rt2x00dev, MAC_CSR12, &reg2);
  1556. state = rt2x00_get_field32(reg2, MAC_CSR12_BBP_CURRENT_STATE);
  1557. if (state == !put_to_sleep)
  1558. return 0;
  1559. rt2x00mmio_register_write(rt2x00dev, MAC_CSR12, reg);
  1560. msleep(10);
  1561. }
  1562. return -EBUSY;
  1563. }
  1564. static int rt61pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1565. enum dev_state state)
  1566. {
  1567. int retval = 0;
  1568. switch (state) {
  1569. case STATE_RADIO_ON:
  1570. retval = rt61pci_enable_radio(rt2x00dev);
  1571. break;
  1572. case STATE_RADIO_OFF:
  1573. rt61pci_disable_radio(rt2x00dev);
  1574. break;
  1575. case STATE_RADIO_IRQ_ON:
  1576. case STATE_RADIO_IRQ_OFF:
  1577. rt61pci_toggle_irq(rt2x00dev, state);
  1578. break;
  1579. case STATE_DEEP_SLEEP:
  1580. case STATE_SLEEP:
  1581. case STATE_STANDBY:
  1582. case STATE_AWAKE:
  1583. retval = rt61pci_set_state(rt2x00dev, state);
  1584. break;
  1585. default:
  1586. retval = -ENOTSUPP;
  1587. break;
  1588. }
  1589. if (unlikely(retval))
  1590. rt2x00_err(rt2x00dev, "Device failed to enter state %d (%d)\n",
  1591. state, retval);
  1592. return retval;
  1593. }
  1594. /*
  1595. * TX descriptor initialization
  1596. */
  1597. static void rt61pci_write_tx_desc(struct queue_entry *entry,
  1598. struct txentry_desc *txdesc)
  1599. {
  1600. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1601. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  1602. __le32 *txd = entry_priv->desc;
  1603. u32 word;
  1604. /*
  1605. * Start writing the descriptor words.
  1606. */
  1607. rt2x00_desc_read(txd, 1, &word);
  1608. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, entry->queue->qid);
  1609. rt2x00_set_field32(&word, TXD_W1_AIFSN, entry->queue->aifs);
  1610. rt2x00_set_field32(&word, TXD_W1_CWMIN, entry->queue->cw_min);
  1611. rt2x00_set_field32(&word, TXD_W1_CWMAX, entry->queue->cw_max);
  1612. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, txdesc->iv_offset);
  1613. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE,
  1614. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  1615. rt2x00_set_field32(&word, TXD_W1_BUFFER_COUNT, 1);
  1616. rt2x00_desc_write(txd, 1, word);
  1617. rt2x00_desc_read(txd, 2, &word);
  1618. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->u.plcp.signal);
  1619. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->u.plcp.service);
  1620. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW,
  1621. txdesc->u.plcp.length_low);
  1622. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH,
  1623. txdesc->u.plcp.length_high);
  1624. rt2x00_desc_write(txd, 2, word);
  1625. if (test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags)) {
  1626. _rt2x00_desc_write(txd, 3, skbdesc->iv[0]);
  1627. _rt2x00_desc_write(txd, 4, skbdesc->iv[1]);
  1628. }
  1629. rt2x00_desc_read(txd, 5, &word);
  1630. rt2x00_set_field32(&word, TXD_W5_PID_TYPE, entry->queue->qid);
  1631. rt2x00_set_field32(&word, TXD_W5_PID_SUBTYPE,
  1632. skbdesc->entry->entry_idx);
  1633. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1634. TXPOWER_TO_DEV(entry->queue->rt2x00dev->tx_power));
  1635. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1636. rt2x00_desc_write(txd, 5, word);
  1637. if (entry->queue->qid != QID_BEACON) {
  1638. rt2x00_desc_read(txd, 6, &word);
  1639. rt2x00_set_field32(&word, TXD_W6_BUFFER_PHYSICAL_ADDRESS,
  1640. skbdesc->skb_dma);
  1641. rt2x00_desc_write(txd, 6, word);
  1642. rt2x00_desc_read(txd, 11, &word);
  1643. rt2x00_set_field32(&word, TXD_W11_BUFFER_LENGTH0,
  1644. txdesc->length);
  1645. rt2x00_desc_write(txd, 11, word);
  1646. }
  1647. /*
  1648. * Writing TXD word 0 must the last to prevent a race condition with
  1649. * the device, whereby the device may take hold of the TXD before we
  1650. * finished updating it.
  1651. */
  1652. rt2x00_desc_read(txd, 0, &word);
  1653. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  1654. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1655. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1656. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1657. rt2x00_set_field32(&word, TXD_W0_ACK,
  1658. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1659. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1660. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1661. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1662. (txdesc->rate_mode == RATE_MODE_OFDM));
  1663. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->u.plcp.ifs);
  1664. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1665. test_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags));
  1666. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC,
  1667. test_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags));
  1668. rt2x00_set_field32(&word, TXD_W0_KEY_TABLE,
  1669. test_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags));
  1670. rt2x00_set_field32(&word, TXD_W0_KEY_INDEX, txdesc->key_idx);
  1671. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, txdesc->length);
  1672. rt2x00_set_field32(&word, TXD_W0_BURST,
  1673. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1674. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, txdesc->cipher);
  1675. rt2x00_desc_write(txd, 0, word);
  1676. /*
  1677. * Register descriptor details in skb frame descriptor.
  1678. */
  1679. skbdesc->desc = txd;
  1680. skbdesc->desc_len = (entry->queue->qid == QID_BEACON) ? TXINFO_SIZE :
  1681. TXD_DESC_SIZE;
  1682. }
  1683. /*
  1684. * TX data initialization
  1685. */
  1686. static void rt61pci_write_beacon(struct queue_entry *entry,
  1687. struct txentry_desc *txdesc)
  1688. {
  1689. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1690. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  1691. unsigned int beacon_base;
  1692. unsigned int padding_len;
  1693. u32 orig_reg, reg;
  1694. /*
  1695. * Disable beaconing while we are reloading the beacon data,
  1696. * otherwise we might be sending out invalid data.
  1697. */
  1698. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1699. orig_reg = reg;
  1700. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1701. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1702. /*
  1703. * Write the TX descriptor for the beacon.
  1704. */
  1705. rt61pci_write_tx_desc(entry, txdesc);
  1706. /*
  1707. * Dump beacon to userspace through debugfs.
  1708. */
  1709. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
  1710. /*
  1711. * Write entire beacon with descriptor and padding to register.
  1712. */
  1713. padding_len = roundup(entry->skb->len, 4) - entry->skb->len;
  1714. if (padding_len && skb_pad(entry->skb, padding_len)) {
  1715. rt2x00_err(rt2x00dev, "Failure padding beacon, aborting\n");
  1716. /* skb freed by skb_pad() on failure */
  1717. entry->skb = NULL;
  1718. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, orig_reg);
  1719. return;
  1720. }
  1721. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  1722. rt2x00mmio_register_multiwrite(rt2x00dev, beacon_base,
  1723. entry_priv->desc, TXINFO_SIZE);
  1724. rt2x00mmio_register_multiwrite(rt2x00dev, beacon_base + TXINFO_SIZE,
  1725. entry->skb->data,
  1726. entry->skb->len + padding_len);
  1727. /*
  1728. * Enable beaconing again.
  1729. *
  1730. * For Wi-Fi faily generated beacons between participating
  1731. * stations. Set TBTT phase adaptive adjustment step to 8us.
  1732. */
  1733. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1734. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1735. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1736. /*
  1737. * Clean up beacon skb.
  1738. */
  1739. dev_kfree_skb_any(entry->skb);
  1740. entry->skb = NULL;
  1741. }
  1742. static void rt61pci_clear_beacon(struct queue_entry *entry)
  1743. {
  1744. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1745. u32 reg;
  1746. /*
  1747. * Disable beaconing while we are reloading the beacon data,
  1748. * otherwise we might be sending out invalid data.
  1749. */
  1750. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1751. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  1752. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1753. /*
  1754. * Clear beacon.
  1755. */
  1756. rt2x00mmio_register_write(rt2x00dev,
  1757. HW_BEACON_OFFSET(entry->entry_idx), 0);
  1758. /*
  1759. * Enable beaconing again.
  1760. */
  1761. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1762. rt2x00mmio_register_write(rt2x00dev, TXRX_CSR9, reg);
  1763. }
  1764. /*
  1765. * RX control handlers
  1766. */
  1767. static int rt61pci_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1768. {
  1769. u8 offset = rt2x00dev->lna_gain;
  1770. u8 lna;
  1771. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1772. switch (lna) {
  1773. case 3:
  1774. offset += 90;
  1775. break;
  1776. case 2:
  1777. offset += 74;
  1778. break;
  1779. case 1:
  1780. offset += 64;
  1781. break;
  1782. default:
  1783. return 0;
  1784. }
  1785. if (rt2x00dev->curr_band == IEEE80211_BAND_5GHZ) {
  1786. if (lna == 3 || lna == 2)
  1787. offset += 10;
  1788. }
  1789. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1790. }
  1791. static void rt61pci_fill_rxdone(struct queue_entry *entry,
  1792. struct rxdone_entry_desc *rxdesc)
  1793. {
  1794. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1795. struct queue_entry_priv_mmio *entry_priv = entry->priv_data;
  1796. u32 word0;
  1797. u32 word1;
  1798. rt2x00_desc_read(entry_priv->desc, 0, &word0);
  1799. rt2x00_desc_read(entry_priv->desc, 1, &word1);
  1800. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1801. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1802. rxdesc->cipher = rt2x00_get_field32(word0, RXD_W0_CIPHER_ALG);
  1803. rxdesc->cipher_status = rt2x00_get_field32(word0, RXD_W0_CIPHER_ERROR);
  1804. if (rxdesc->cipher != CIPHER_NONE) {
  1805. _rt2x00_desc_read(entry_priv->desc, 2, &rxdesc->iv[0]);
  1806. _rt2x00_desc_read(entry_priv->desc, 3, &rxdesc->iv[1]);
  1807. rxdesc->dev_flags |= RXDONE_CRYPTO_IV;
  1808. _rt2x00_desc_read(entry_priv->desc, 4, &rxdesc->icv);
  1809. rxdesc->dev_flags |= RXDONE_CRYPTO_ICV;
  1810. /*
  1811. * Hardware has stripped IV/EIV data from 802.11 frame during
  1812. * decryption. It has provided the data separately but rt2x00lib
  1813. * should decide if it should be reinserted.
  1814. */
  1815. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  1816. /*
  1817. * The hardware has already checked the Michael Mic and has
  1818. * stripped it from the frame. Signal this to mac80211.
  1819. */
  1820. rxdesc->flags |= RX_FLAG_MMIC_STRIPPED;
  1821. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  1822. rxdesc->flags |= RX_FLAG_DECRYPTED;
  1823. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  1824. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  1825. }
  1826. /*
  1827. * Obtain the status about this packet.
  1828. * When frame was received with an OFDM bitrate,
  1829. * the signal is the PLCP value. If it was received with
  1830. * a CCK bitrate the signal is the rate in 100kbit/s.
  1831. */
  1832. rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1833. rxdesc->rssi = rt61pci_agc_to_rssi(rt2x00dev, word1);
  1834. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1835. if (rt2x00_get_field32(word0, RXD_W0_OFDM))
  1836. rxdesc->dev_flags |= RXDONE_SIGNAL_PLCP;
  1837. else
  1838. rxdesc->dev_flags |= RXDONE_SIGNAL_BITRATE;
  1839. if (rt2x00_get_field32(word0, RXD_W0_MY_BSS))
  1840. rxdesc->dev_flags |= RXDONE_MY_BSS;
  1841. }
  1842. /*
  1843. * Interrupt functions.
  1844. */
  1845. static void rt61pci_txdone(struct rt2x00_dev *rt2x00dev)
  1846. {
  1847. struct data_queue *queue;
  1848. struct queue_entry *entry;
  1849. struct queue_entry *entry_done;
  1850. struct queue_entry_priv_mmio *entry_priv;
  1851. struct txdone_entry_desc txdesc;
  1852. u32 word;
  1853. u32 reg;
  1854. int type;
  1855. int index;
  1856. int i;
  1857. /*
  1858. * TX_STA_FIFO is a stack of X entries, hence read TX_STA_FIFO
  1859. * at most X times and also stop processing once the TX_STA_FIFO_VALID
  1860. * flag is not set anymore.
  1861. *
  1862. * The legacy drivers use X=TX_RING_SIZE but state in a comment
  1863. * that the TX_STA_FIFO stack has a size of 16. We stick to our
  1864. * tx ring size for now.
  1865. */
  1866. for (i = 0; i < rt2x00dev->tx->limit; i++) {
  1867. rt2x00mmio_register_read(rt2x00dev, STA_CSR4, &reg);
  1868. if (!rt2x00_get_field32(reg, STA_CSR4_VALID))
  1869. break;
  1870. /*
  1871. * Skip this entry when it contains an invalid
  1872. * queue identication number.
  1873. */
  1874. type = rt2x00_get_field32(reg, STA_CSR4_PID_TYPE);
  1875. queue = rt2x00queue_get_tx_queue(rt2x00dev, type);
  1876. if (unlikely(!queue))
  1877. continue;
  1878. /*
  1879. * Skip this entry when it contains an invalid
  1880. * index number.
  1881. */
  1882. index = rt2x00_get_field32(reg, STA_CSR4_PID_SUBTYPE);
  1883. if (unlikely(index >= queue->limit))
  1884. continue;
  1885. entry = &queue->entries[index];
  1886. entry_priv = entry->priv_data;
  1887. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1888. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1889. !rt2x00_get_field32(word, TXD_W0_VALID))
  1890. return;
  1891. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1892. while (entry != entry_done) {
  1893. /* Catch up.
  1894. * Just report any entries we missed as failed.
  1895. */
  1896. rt2x00_warn(rt2x00dev, "TX status report missed for entry %d\n",
  1897. entry_done->entry_idx);
  1898. rt2x00lib_txdone_noinfo(entry_done, TXDONE_UNKNOWN);
  1899. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1900. }
  1901. /*
  1902. * Obtain the status about this packet.
  1903. */
  1904. txdesc.flags = 0;
  1905. switch (rt2x00_get_field32(reg, STA_CSR4_TX_RESULT)) {
  1906. case 0: /* Success, maybe with retry */
  1907. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  1908. break;
  1909. case 6: /* Failure, excessive retries */
  1910. __set_bit(TXDONE_EXCESSIVE_RETRY, &txdesc.flags);
  1911. /* Don't break, this is a failed frame! */
  1912. default: /* Failure */
  1913. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  1914. }
  1915. txdesc.retry = rt2x00_get_field32(reg, STA_CSR4_RETRY_COUNT);
  1916. /*
  1917. * the frame was retried at least once
  1918. * -> hw used fallback rates
  1919. */
  1920. if (txdesc.retry)
  1921. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  1922. rt2x00lib_txdone(entry, &txdesc);
  1923. }
  1924. }
  1925. static void rt61pci_wakeup(struct rt2x00_dev *rt2x00dev)
  1926. {
  1927. struct rt2x00lib_conf libconf = { .conf = &rt2x00dev->hw->conf };
  1928. rt61pci_config(rt2x00dev, &libconf, IEEE80211_CONF_CHANGE_PS);
  1929. }
  1930. static inline void rt61pci_enable_interrupt(struct rt2x00_dev *rt2x00dev,
  1931. struct rt2x00_field32 irq_field)
  1932. {
  1933. u32 reg;
  1934. /*
  1935. * Enable a single interrupt. The interrupt mask register
  1936. * access needs locking.
  1937. */
  1938. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1939. rt2x00mmio_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1940. rt2x00_set_field32(&reg, irq_field, 0);
  1941. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1942. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1943. }
  1944. static void rt61pci_enable_mcu_interrupt(struct rt2x00_dev *rt2x00dev,
  1945. struct rt2x00_field32 irq_field)
  1946. {
  1947. u32 reg;
  1948. /*
  1949. * Enable a single MCU interrupt. The interrupt mask register
  1950. * access needs locking.
  1951. */
  1952. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1953. rt2x00mmio_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1954. rt2x00_set_field32(&reg, irq_field, 0);
  1955. rt2x00mmio_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1956. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1957. }
  1958. static void rt61pci_txstatus_tasklet(unsigned long data)
  1959. {
  1960. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1961. rt61pci_txdone(rt2x00dev);
  1962. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1963. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_TXDONE);
  1964. }
  1965. static void rt61pci_tbtt_tasklet(unsigned long data)
  1966. {
  1967. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1968. rt2x00lib_beacondone(rt2x00dev);
  1969. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1970. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_BEACON_DONE);
  1971. }
  1972. static void rt61pci_rxdone_tasklet(unsigned long data)
  1973. {
  1974. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1975. if (rt2x00mmio_rxdone(rt2x00dev))
  1976. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  1977. else if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1978. rt61pci_enable_interrupt(rt2x00dev, INT_MASK_CSR_RXDONE);
  1979. }
  1980. static void rt61pci_autowake_tasklet(unsigned long data)
  1981. {
  1982. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1983. rt61pci_wakeup(rt2x00dev);
  1984. rt2x00mmio_register_write(rt2x00dev,
  1985. M2H_CMD_DONE_CSR, 0xffffffff);
  1986. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1987. rt61pci_enable_mcu_interrupt(rt2x00dev, MCU_INT_MASK_CSR_TWAKEUP);
  1988. }
  1989. static irqreturn_t rt61pci_interrupt(int irq, void *dev_instance)
  1990. {
  1991. struct rt2x00_dev *rt2x00dev = dev_instance;
  1992. u32 reg_mcu, mask_mcu;
  1993. u32 reg, mask;
  1994. /*
  1995. * Get the interrupt sources & saved to local variable.
  1996. * Write register value back to clear pending interrupts.
  1997. */
  1998. rt2x00mmio_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg_mcu);
  1999. rt2x00mmio_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg_mcu);
  2000. rt2x00mmio_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  2001. rt2x00mmio_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  2002. if (!reg && !reg_mcu)
  2003. return IRQ_NONE;
  2004. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  2005. return IRQ_HANDLED;
  2006. /*
  2007. * Schedule tasklets for interrupt handling.
  2008. */
  2009. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RXDONE))
  2010. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  2011. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TXDONE))
  2012. tasklet_schedule(&rt2x00dev->txstatus_tasklet);
  2013. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_BEACON_DONE))
  2014. tasklet_hi_schedule(&rt2x00dev->tbtt_tasklet);
  2015. if (rt2x00_get_field32(reg_mcu, MCU_INT_SOURCE_CSR_TWAKEUP))
  2016. tasklet_schedule(&rt2x00dev->autowake_tasklet);
  2017. /*
  2018. * Since INT_MASK_CSR and INT_SOURCE_CSR use the same bits
  2019. * for interrupts and interrupt masks we can just use the value of
  2020. * INT_SOURCE_CSR to create the interrupt mask.
  2021. */
  2022. mask = reg;
  2023. mask_mcu = reg_mcu;
  2024. /*
  2025. * Disable all interrupts for which a tasklet was scheduled right now,
  2026. * the tasklet will reenable the appropriate interrupts.
  2027. */
  2028. spin_lock(&rt2x00dev->irqmask_lock);
  2029. rt2x00mmio_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  2030. reg |= mask;
  2031. rt2x00mmio_register_write(rt2x00dev, INT_MASK_CSR, reg);
  2032. rt2x00mmio_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  2033. reg |= mask_mcu;
  2034. rt2x00mmio_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  2035. spin_unlock(&rt2x00dev->irqmask_lock);
  2036. return IRQ_HANDLED;
  2037. }
  2038. /*
  2039. * Device probe functions.
  2040. */
  2041. static int rt61pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  2042. {
  2043. struct eeprom_93cx6 eeprom;
  2044. u32 reg;
  2045. u16 word;
  2046. u8 *mac;
  2047. s8 value;
  2048. rt2x00mmio_register_read(rt2x00dev, E2PROM_CSR, &reg);
  2049. eeprom.data = rt2x00dev;
  2050. eeprom.register_read = rt61pci_eepromregister_read;
  2051. eeprom.register_write = rt61pci_eepromregister_write;
  2052. eeprom.width = rt2x00_get_field32(reg, E2PROM_CSR_TYPE_93C46) ?
  2053. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  2054. eeprom.reg_data_in = 0;
  2055. eeprom.reg_data_out = 0;
  2056. eeprom.reg_data_clock = 0;
  2057. eeprom.reg_chip_select = 0;
  2058. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  2059. EEPROM_SIZE / sizeof(u16));
  2060. /*
  2061. * Start validation of the data that has been read.
  2062. */
  2063. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  2064. if (!is_valid_ether_addr(mac)) {
  2065. eth_random_addr(mac);
  2066. rt2x00_eeprom_dbg(rt2x00dev, "MAC: %pM\n", mac);
  2067. }
  2068. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  2069. if (word == 0xffff) {
  2070. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  2071. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  2072. ANTENNA_B);
  2073. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  2074. ANTENNA_B);
  2075. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  2076. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  2077. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  2078. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5225);
  2079. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2080. rt2x00_eeprom_dbg(rt2x00dev, "Antenna: 0x%04x\n", word);
  2081. }
  2082. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  2083. if (word == 0xffff) {
  2084. rt2x00_set_field16(&word, EEPROM_NIC_ENABLE_DIVERSITY, 0);
  2085. rt2x00_set_field16(&word, EEPROM_NIC_TX_DIVERSITY, 0);
  2086. rt2x00_set_field16(&word, EEPROM_NIC_RX_FIXED, 0);
  2087. rt2x00_set_field16(&word, EEPROM_NIC_TX_FIXED, 0);
  2088. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  2089. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  2090. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  2091. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  2092. rt2x00_eeprom_dbg(rt2x00dev, "NIC: 0x%04x\n", word);
  2093. }
  2094. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  2095. if (word == 0xffff) {
  2096. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  2097. LED_MODE_DEFAULT);
  2098. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  2099. rt2x00_eeprom_dbg(rt2x00dev, "Led: 0x%04x\n", word);
  2100. }
  2101. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2102. if (word == 0xffff) {
  2103. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2104. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  2105. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2106. rt2x00_eeprom_dbg(rt2x00dev, "Freq: 0x%04x\n", word);
  2107. }
  2108. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  2109. if (word == 0xffff) {
  2110. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2111. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2112. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2113. rt2x00_eeprom_dbg(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  2114. } else {
  2115. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  2116. if (value < -10 || value > 10)
  2117. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  2118. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  2119. if (value < -10 || value > 10)
  2120. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  2121. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  2122. }
  2123. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  2124. if (word == 0xffff) {
  2125. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2126. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2127. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2128. rt2x00_eeprom_dbg(rt2x00dev, "RSSI OFFSET A: 0x%04x\n", word);
  2129. } else {
  2130. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  2131. if (value < -10 || value > 10)
  2132. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  2133. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  2134. if (value < -10 || value > 10)
  2135. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  2136. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  2137. }
  2138. return 0;
  2139. }
  2140. static int rt61pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2141. {
  2142. u32 reg;
  2143. u16 value;
  2144. u16 eeprom;
  2145. /*
  2146. * Read EEPROM word for configuration.
  2147. */
  2148. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2149. /*
  2150. * Identify RF chipset.
  2151. */
  2152. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  2153. rt2x00mmio_register_read(rt2x00dev, MAC_CSR0, &reg);
  2154. rt2x00_set_chip(rt2x00dev, rt2x00_get_field32(reg, MAC_CSR0_CHIPSET),
  2155. value, rt2x00_get_field32(reg, MAC_CSR0_REVISION));
  2156. if (!rt2x00_rf(rt2x00dev, RF5225) &&
  2157. !rt2x00_rf(rt2x00dev, RF5325) &&
  2158. !rt2x00_rf(rt2x00dev, RF2527) &&
  2159. !rt2x00_rf(rt2x00dev, RF2529)) {
  2160. rt2x00_err(rt2x00dev, "Invalid RF chipset detected\n");
  2161. return -ENODEV;
  2162. }
  2163. /*
  2164. * Determine number of antennas.
  2165. */
  2166. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_NUM) == 2)
  2167. __set_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags);
  2168. /*
  2169. * Identify default antenna configuration.
  2170. */
  2171. rt2x00dev->default_ant.tx =
  2172. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  2173. rt2x00dev->default_ant.rx =
  2174. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  2175. /*
  2176. * Read the Frame type.
  2177. */
  2178. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  2179. __set_bit(CAPABILITY_FRAME_TYPE, &rt2x00dev->cap_flags);
  2180. /*
  2181. * Detect if this device has a hardware controlled radio.
  2182. */
  2183. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  2184. __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
  2185. /*
  2186. * Read frequency offset and RF programming sequence.
  2187. */
  2188. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2189. if (rt2x00_get_field16(eeprom, EEPROM_FREQ_SEQ))
  2190. __set_bit(CAPABILITY_RF_SEQUENCE, &rt2x00dev->cap_flags);
  2191. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2192. /*
  2193. * Read external LNA informations.
  2194. */
  2195. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  2196. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  2197. __set_bit(CAPABILITY_EXTERNAL_LNA_A, &rt2x00dev->cap_flags);
  2198. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  2199. __set_bit(CAPABILITY_EXTERNAL_LNA_BG, &rt2x00dev->cap_flags);
  2200. /*
  2201. * When working with a RF2529 chip without double antenna,
  2202. * the antenna settings should be gathered from the NIC
  2203. * eeprom word.
  2204. */
  2205. if (rt2x00_rf(rt2x00dev, RF2529) &&
  2206. !test_bit(CAPABILITY_DOUBLE_ANTENNA, &rt2x00dev->cap_flags)) {
  2207. rt2x00dev->default_ant.rx =
  2208. ANTENNA_A + rt2x00_get_field16(eeprom, EEPROM_NIC_RX_FIXED);
  2209. rt2x00dev->default_ant.tx =
  2210. ANTENNA_B - rt2x00_get_field16(eeprom, EEPROM_NIC_TX_FIXED);
  2211. if (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_DIVERSITY))
  2212. rt2x00dev->default_ant.tx = ANTENNA_SW_DIVERSITY;
  2213. if (rt2x00_get_field16(eeprom, EEPROM_NIC_ENABLE_DIVERSITY))
  2214. rt2x00dev->default_ant.rx = ANTENNA_SW_DIVERSITY;
  2215. }
  2216. /*
  2217. * Store led settings, for correct led behaviour.
  2218. * If the eeprom value is invalid,
  2219. * switch to default led mode.
  2220. */
  2221. #ifdef CONFIG_RT2X00_LIB_LEDS
  2222. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  2223. value = rt2x00_get_field16(eeprom, EEPROM_LED_LED_MODE);
  2224. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2225. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2226. if (value == LED_MODE_SIGNAL_STRENGTH)
  2227. rt61pci_init_led(rt2x00dev, &rt2x00dev->led_qual,
  2228. LED_TYPE_QUALITY);
  2229. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_LED_MODE, value);
  2230. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_0,
  2231. rt2x00_get_field16(eeprom,
  2232. EEPROM_LED_POLARITY_GPIO_0));
  2233. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_1,
  2234. rt2x00_get_field16(eeprom,
  2235. EEPROM_LED_POLARITY_GPIO_1));
  2236. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_2,
  2237. rt2x00_get_field16(eeprom,
  2238. EEPROM_LED_POLARITY_GPIO_2));
  2239. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_3,
  2240. rt2x00_get_field16(eeprom,
  2241. EEPROM_LED_POLARITY_GPIO_3));
  2242. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_GPIO_4,
  2243. rt2x00_get_field16(eeprom,
  2244. EEPROM_LED_POLARITY_GPIO_4));
  2245. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_ACT,
  2246. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  2247. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_BG,
  2248. rt2x00_get_field16(eeprom,
  2249. EEPROM_LED_POLARITY_RDY_G));
  2250. rt2x00_set_field16(&rt2x00dev->led_mcu_reg, MCU_LEDCS_POLARITY_READY_A,
  2251. rt2x00_get_field16(eeprom,
  2252. EEPROM_LED_POLARITY_RDY_A));
  2253. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2254. return 0;
  2255. }
  2256. /*
  2257. * RF value list for RF5225 & RF5325
  2258. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence disabled
  2259. */
  2260. static const struct rf_channel rf_vals_noseq[] = {
  2261. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2262. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2263. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2264. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2265. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2266. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2267. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2268. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2269. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2270. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2271. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2272. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2273. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2274. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2275. /* 802.11 UNI / HyperLan 2 */
  2276. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  2277. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  2278. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  2279. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  2280. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  2281. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  2282. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  2283. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  2284. /* 802.11 HyperLan 2 */
  2285. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  2286. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  2287. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  2288. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  2289. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  2290. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  2291. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  2292. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  2293. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  2294. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  2295. /* 802.11 UNII */
  2296. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  2297. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  2298. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  2299. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  2300. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  2301. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  2302. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2303. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  2304. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  2305. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  2306. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  2307. };
  2308. /*
  2309. * RF value list for RF5225 & RF5325
  2310. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence enabled
  2311. */
  2312. static const struct rf_channel rf_vals_seq[] = {
  2313. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  2314. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  2315. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  2316. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  2317. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  2318. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  2319. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  2320. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  2321. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  2322. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  2323. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  2324. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  2325. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  2326. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  2327. /* 802.11 UNI / HyperLan 2 */
  2328. { 36, 0x00002cd4, 0x0004481a, 0x00098455, 0x000c0a03 },
  2329. { 40, 0x00002cd0, 0x00044682, 0x00098455, 0x000c0a03 },
  2330. { 44, 0x00002cd0, 0x00044686, 0x00098455, 0x000c0a1b },
  2331. { 48, 0x00002cd0, 0x0004468e, 0x00098655, 0x000c0a0b },
  2332. { 52, 0x00002cd0, 0x00044692, 0x00098855, 0x000c0a23 },
  2333. { 56, 0x00002cd0, 0x0004469a, 0x00098c55, 0x000c0a13 },
  2334. { 60, 0x00002cd0, 0x000446a2, 0x00098e55, 0x000c0a03 },
  2335. { 64, 0x00002cd0, 0x000446a6, 0x00099255, 0x000c0a1b },
  2336. /* 802.11 HyperLan 2 */
  2337. { 100, 0x00002cd4, 0x0004489a, 0x000b9855, 0x000c0a03 },
  2338. { 104, 0x00002cd4, 0x000448a2, 0x000b9855, 0x000c0a03 },
  2339. { 108, 0x00002cd4, 0x000448aa, 0x000b9855, 0x000c0a03 },
  2340. { 112, 0x00002cd4, 0x000448b2, 0x000b9a55, 0x000c0a03 },
  2341. { 116, 0x00002cd4, 0x000448ba, 0x000b9a55, 0x000c0a03 },
  2342. { 120, 0x00002cd0, 0x00044702, 0x000b9a55, 0x000c0a03 },
  2343. { 124, 0x00002cd0, 0x00044706, 0x000b9a55, 0x000c0a1b },
  2344. { 128, 0x00002cd0, 0x0004470e, 0x000b9c55, 0x000c0a0b },
  2345. { 132, 0x00002cd0, 0x00044712, 0x000b9c55, 0x000c0a23 },
  2346. { 136, 0x00002cd0, 0x0004471a, 0x000b9e55, 0x000c0a13 },
  2347. /* 802.11 UNII */
  2348. { 140, 0x00002cd0, 0x00044722, 0x000b9e55, 0x000c0a03 },
  2349. { 149, 0x00002cd0, 0x0004472e, 0x000ba255, 0x000c0a1b },
  2350. { 153, 0x00002cd0, 0x00044736, 0x000ba255, 0x000c0a0b },
  2351. { 157, 0x00002cd4, 0x0004490a, 0x000ba255, 0x000c0a17 },
  2352. { 161, 0x00002cd4, 0x00044912, 0x000ba255, 0x000c0a17 },
  2353. { 165, 0x00002cd4, 0x0004491a, 0x000ba255, 0x000c0a17 },
  2354. /* MMAC(Japan)J52 ch 34,38,42,46 */
  2355. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000c0a0b },
  2356. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000c0a13 },
  2357. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000c0a1b },
  2358. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000c0a23 },
  2359. };
  2360. static int rt61pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2361. {
  2362. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2363. struct channel_info *info;
  2364. char *tx_power;
  2365. unsigned int i;
  2366. /*
  2367. * Disable powersaving as default.
  2368. */
  2369. rt2x00dev->hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2370. /*
  2371. * Initialize all hw fields.
  2372. */
  2373. rt2x00dev->hw->flags =
  2374. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2375. IEEE80211_HW_SIGNAL_DBM |
  2376. IEEE80211_HW_SUPPORTS_PS |
  2377. IEEE80211_HW_PS_NULLFUNC_STACK;
  2378. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2379. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2380. rt2x00_eeprom_addr(rt2x00dev,
  2381. EEPROM_MAC_ADDR_0));
  2382. /*
  2383. * As rt61 has a global fallback table we cannot specify
  2384. * more then one tx rate per frame but since the hw will
  2385. * try several rates (based on the fallback table) we should
  2386. * initialize max_report_rates to the maximum number of rates
  2387. * we are going to try. Otherwise mac80211 will truncate our
  2388. * reported tx rates and the rc algortihm will end up with
  2389. * incorrect data.
  2390. */
  2391. rt2x00dev->hw->max_rates = 1;
  2392. rt2x00dev->hw->max_report_rates = 7;
  2393. rt2x00dev->hw->max_rate_tries = 1;
  2394. /*
  2395. * Initialize hw_mode information.
  2396. */
  2397. spec->supported_bands = SUPPORT_BAND_2GHZ;
  2398. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  2399. if (!test_bit(CAPABILITY_RF_SEQUENCE, &rt2x00dev->cap_flags)) {
  2400. spec->num_channels = 14;
  2401. spec->channels = rf_vals_noseq;
  2402. } else {
  2403. spec->num_channels = 14;
  2404. spec->channels = rf_vals_seq;
  2405. }
  2406. if (rt2x00_rf(rt2x00dev, RF5225) || rt2x00_rf(rt2x00dev, RF5325)) {
  2407. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  2408. spec->num_channels = ARRAY_SIZE(rf_vals_seq);
  2409. }
  2410. /*
  2411. * Create channel information array
  2412. */
  2413. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  2414. if (!info)
  2415. return -ENOMEM;
  2416. spec->channels_info = info;
  2417. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  2418. for (i = 0; i < 14; i++) {
  2419. info[i].max_power = MAX_TXPOWER;
  2420. info[i].default_power1 = TXPOWER_FROM_DEV(tx_power[i]);
  2421. }
  2422. if (spec->num_channels > 14) {
  2423. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  2424. for (i = 14; i < spec->num_channels; i++) {
  2425. info[i].max_power = MAX_TXPOWER;
  2426. info[i].default_power1 =
  2427. TXPOWER_FROM_DEV(tx_power[i - 14]);
  2428. }
  2429. }
  2430. return 0;
  2431. }
  2432. static int rt61pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  2433. {
  2434. int retval;
  2435. u32 reg;
  2436. /*
  2437. * Disable power saving.
  2438. */
  2439. rt2x00mmio_register_write(rt2x00dev, SOFT_RESET_CSR, 0x00000007);
  2440. /*
  2441. * Allocate eeprom data.
  2442. */
  2443. retval = rt61pci_validate_eeprom(rt2x00dev);
  2444. if (retval)
  2445. return retval;
  2446. retval = rt61pci_init_eeprom(rt2x00dev);
  2447. if (retval)
  2448. return retval;
  2449. /*
  2450. * Enable rfkill polling by setting GPIO direction of the
  2451. * rfkill switch GPIO pin correctly.
  2452. */
  2453. rt2x00mmio_register_read(rt2x00dev, MAC_CSR13, &reg);
  2454. rt2x00_set_field32(&reg, MAC_CSR13_DIR5, 1);
  2455. rt2x00mmio_register_write(rt2x00dev, MAC_CSR13, reg);
  2456. /*
  2457. * Initialize hw specifications.
  2458. */
  2459. retval = rt61pci_probe_hw_mode(rt2x00dev);
  2460. if (retval)
  2461. return retval;
  2462. /*
  2463. * This device has multiple filters for control frames,
  2464. * but has no a separate filter for PS Poll frames.
  2465. */
  2466. __set_bit(CAPABILITY_CONTROL_FILTERS, &rt2x00dev->cap_flags);
  2467. /*
  2468. * This device requires firmware and DMA mapped skbs.
  2469. */
  2470. __set_bit(REQUIRE_FIRMWARE, &rt2x00dev->cap_flags);
  2471. __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
  2472. if (!modparam_nohwcrypt)
  2473. __set_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags);
  2474. __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
  2475. /*
  2476. * Set the rssi offset.
  2477. */
  2478. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  2479. return 0;
  2480. }
  2481. /*
  2482. * IEEE80211 stack callback functions.
  2483. */
  2484. static int rt61pci_conf_tx(struct ieee80211_hw *hw,
  2485. struct ieee80211_vif *vif, u16 queue_idx,
  2486. const struct ieee80211_tx_queue_params *params)
  2487. {
  2488. struct rt2x00_dev *rt2x00dev = hw->priv;
  2489. struct data_queue *queue;
  2490. struct rt2x00_field32 field;
  2491. int retval;
  2492. u32 reg;
  2493. u32 offset;
  2494. /*
  2495. * First pass the configuration through rt2x00lib, that will
  2496. * update the queue settings and validate the input. After that
  2497. * we are free to update the registers based on the value
  2498. * in the queue parameter.
  2499. */
  2500. retval = rt2x00mac_conf_tx(hw, vif, queue_idx, params);
  2501. if (retval)
  2502. return retval;
  2503. /*
  2504. * We only need to perform additional register initialization
  2505. * for WMM queues.
  2506. */
  2507. if (queue_idx >= 4)
  2508. return 0;
  2509. queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
  2510. /* Update WMM TXOP register */
  2511. offset = AC_TXOP_CSR0 + (sizeof(u32) * (!!(queue_idx & 2)));
  2512. field.bit_offset = (queue_idx & 1) * 16;
  2513. field.bit_mask = 0xffff << field.bit_offset;
  2514. rt2x00mmio_register_read(rt2x00dev, offset, &reg);
  2515. rt2x00_set_field32(&reg, field, queue->txop);
  2516. rt2x00mmio_register_write(rt2x00dev, offset, reg);
  2517. /* Update WMM registers */
  2518. field.bit_offset = queue_idx * 4;
  2519. field.bit_mask = 0xf << field.bit_offset;
  2520. rt2x00mmio_register_read(rt2x00dev, AIFSN_CSR, &reg);
  2521. rt2x00_set_field32(&reg, field, queue->aifs);
  2522. rt2x00mmio_register_write(rt2x00dev, AIFSN_CSR, reg);
  2523. rt2x00mmio_register_read(rt2x00dev, CWMIN_CSR, &reg);
  2524. rt2x00_set_field32(&reg, field, queue->cw_min);
  2525. rt2x00mmio_register_write(rt2x00dev, CWMIN_CSR, reg);
  2526. rt2x00mmio_register_read(rt2x00dev, CWMAX_CSR, &reg);
  2527. rt2x00_set_field32(&reg, field, queue->cw_max);
  2528. rt2x00mmio_register_write(rt2x00dev, CWMAX_CSR, reg);
  2529. return 0;
  2530. }
  2531. static u64 rt61pci_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  2532. {
  2533. struct rt2x00_dev *rt2x00dev = hw->priv;
  2534. u64 tsf;
  2535. u32 reg;
  2536. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR13, &reg);
  2537. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  2538. rt2x00mmio_register_read(rt2x00dev, TXRX_CSR12, &reg);
  2539. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  2540. return tsf;
  2541. }
  2542. static const struct ieee80211_ops rt61pci_mac80211_ops = {
  2543. .tx = rt2x00mac_tx,
  2544. .start = rt2x00mac_start,
  2545. .stop = rt2x00mac_stop,
  2546. .add_interface = rt2x00mac_add_interface,
  2547. .remove_interface = rt2x00mac_remove_interface,
  2548. .config = rt2x00mac_config,
  2549. .configure_filter = rt2x00mac_configure_filter,
  2550. .set_key = rt2x00mac_set_key,
  2551. .sw_scan_start = rt2x00mac_sw_scan_start,
  2552. .sw_scan_complete = rt2x00mac_sw_scan_complete,
  2553. .get_stats = rt2x00mac_get_stats,
  2554. .bss_info_changed = rt2x00mac_bss_info_changed,
  2555. .conf_tx = rt61pci_conf_tx,
  2556. .get_tsf = rt61pci_get_tsf,
  2557. .rfkill_poll = rt2x00mac_rfkill_poll,
  2558. .flush = rt2x00mac_flush,
  2559. .set_antenna = rt2x00mac_set_antenna,
  2560. .get_antenna = rt2x00mac_get_antenna,
  2561. .get_ringparam = rt2x00mac_get_ringparam,
  2562. .tx_frames_pending = rt2x00mac_tx_frames_pending,
  2563. };
  2564. static const struct rt2x00lib_ops rt61pci_rt2x00_ops = {
  2565. .irq_handler = rt61pci_interrupt,
  2566. .txstatus_tasklet = rt61pci_txstatus_tasklet,
  2567. .tbtt_tasklet = rt61pci_tbtt_tasklet,
  2568. .rxdone_tasklet = rt61pci_rxdone_tasklet,
  2569. .autowake_tasklet = rt61pci_autowake_tasklet,
  2570. .probe_hw = rt61pci_probe_hw,
  2571. .get_firmware_name = rt61pci_get_firmware_name,
  2572. .check_firmware = rt61pci_check_firmware,
  2573. .load_firmware = rt61pci_load_firmware,
  2574. .initialize = rt2x00mmio_initialize,
  2575. .uninitialize = rt2x00mmio_uninitialize,
  2576. .get_entry_state = rt61pci_get_entry_state,
  2577. .clear_entry = rt61pci_clear_entry,
  2578. .set_device_state = rt61pci_set_device_state,
  2579. .rfkill_poll = rt61pci_rfkill_poll,
  2580. .link_stats = rt61pci_link_stats,
  2581. .reset_tuner = rt61pci_reset_tuner,
  2582. .link_tuner = rt61pci_link_tuner,
  2583. .start_queue = rt61pci_start_queue,
  2584. .kick_queue = rt61pci_kick_queue,
  2585. .stop_queue = rt61pci_stop_queue,
  2586. .flush_queue = rt2x00mmio_flush_queue,
  2587. .write_tx_desc = rt61pci_write_tx_desc,
  2588. .write_beacon = rt61pci_write_beacon,
  2589. .clear_beacon = rt61pci_clear_beacon,
  2590. .fill_rxdone = rt61pci_fill_rxdone,
  2591. .config_shared_key = rt61pci_config_shared_key,
  2592. .config_pairwise_key = rt61pci_config_pairwise_key,
  2593. .config_filter = rt61pci_config_filter,
  2594. .config_intf = rt61pci_config_intf,
  2595. .config_erp = rt61pci_config_erp,
  2596. .config_ant = rt61pci_config_ant,
  2597. .config = rt61pci_config,
  2598. };
  2599. static void rt61pci_queue_init(struct data_queue *queue)
  2600. {
  2601. switch (queue->qid) {
  2602. case QID_RX:
  2603. queue->limit = 32;
  2604. queue->data_size = DATA_FRAME_SIZE;
  2605. queue->desc_size = RXD_DESC_SIZE;
  2606. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  2607. break;
  2608. case QID_AC_VO:
  2609. case QID_AC_VI:
  2610. case QID_AC_BE:
  2611. case QID_AC_BK:
  2612. queue->limit = 32;
  2613. queue->data_size = DATA_FRAME_SIZE;
  2614. queue->desc_size = TXD_DESC_SIZE;
  2615. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  2616. break;
  2617. case QID_BEACON:
  2618. queue->limit = 4;
  2619. queue->data_size = 0; /* No DMA required for beacons */
  2620. queue->desc_size = TXINFO_SIZE;
  2621. queue->priv_size = sizeof(struct queue_entry_priv_mmio);
  2622. break;
  2623. case QID_ATIM:
  2624. /* fallthrough */
  2625. default:
  2626. BUG();
  2627. break;
  2628. }
  2629. }
  2630. static const struct rt2x00_ops rt61pci_ops = {
  2631. .name = KBUILD_MODNAME,
  2632. .max_ap_intf = 4,
  2633. .eeprom_size = EEPROM_SIZE,
  2634. .rf_size = RF_SIZE,
  2635. .tx_queues = NUM_TX_QUEUES,
  2636. .queue_init = rt61pci_queue_init,
  2637. .lib = &rt61pci_rt2x00_ops,
  2638. .hw = &rt61pci_mac80211_ops,
  2639. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2640. .debugfs = &rt61pci_rt2x00debug,
  2641. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2642. };
  2643. /*
  2644. * RT61pci module information.
  2645. */
  2646. static DEFINE_PCI_DEVICE_TABLE(rt61pci_device_table) = {
  2647. /* RT2561s */
  2648. { PCI_DEVICE(0x1814, 0x0301) },
  2649. /* RT2561 v2 */
  2650. { PCI_DEVICE(0x1814, 0x0302) },
  2651. /* RT2661 */
  2652. { PCI_DEVICE(0x1814, 0x0401) },
  2653. { 0, }
  2654. };
  2655. MODULE_AUTHOR(DRV_PROJECT);
  2656. MODULE_VERSION(DRV_VERSION);
  2657. MODULE_DESCRIPTION("Ralink RT61 PCI & PCMCIA Wireless LAN driver.");
  2658. MODULE_SUPPORTED_DEVICE("Ralink RT2561, RT2561s & RT2661 "
  2659. "PCI & PCMCIA chipset based cards");
  2660. MODULE_DEVICE_TABLE(pci, rt61pci_device_table);
  2661. MODULE_FIRMWARE(FIRMWARE_RT2561);
  2662. MODULE_FIRMWARE(FIRMWARE_RT2561s);
  2663. MODULE_FIRMWARE(FIRMWARE_RT2661);
  2664. MODULE_LICENSE("GPL");
  2665. static int rt61pci_probe(struct pci_dev *pci_dev,
  2666. const struct pci_device_id *id)
  2667. {
  2668. return rt2x00pci_probe(pci_dev, &rt61pci_ops);
  2669. }
  2670. static struct pci_driver rt61pci_driver = {
  2671. .name = KBUILD_MODNAME,
  2672. .id_table = rt61pci_device_table,
  2673. .probe = rt61pci_probe,
  2674. .remove = rt2x00pci_remove,
  2675. .suspend = rt2x00pci_suspend,
  2676. .resume = rt2x00pci_resume,
  2677. };
  2678. module_pci_driver(rt61pci_driver);