rt61pci.c 94 KB

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