rt61pci.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  1. /*
  2. Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt61pci
  19. Abstract: rt61pci device specific routines.
  20. Supported chipsets: RT2561, RT2561s, RT2661.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/eeprom_93cx6.h>
  29. #include "rt2x00.h"
  30. #include "rt2x00pci.h"
  31. #include "rt61pci.h"
  32. /*
  33. * Register access.
  34. * BBP and RF register require indirect register access,
  35. * and use the CSR registers PHY_CSR3 and PHY_CSR4 to achieve this.
  36. * These indirect registers work with busy bits,
  37. * and we will try maximal REGISTER_BUSY_COUNT times to access
  38. * the register while taking a REGISTER_BUSY_DELAY us delay
  39. * between each attampt. When the busy bit is still set at that time,
  40. * the access attempt is considered to have failed,
  41. * and we will print an error.
  42. */
  43. static u32 rt61pci_bbp_check(struct rt2x00_dev *rt2x00dev)
  44. {
  45. u32 reg;
  46. unsigned int i;
  47. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  48. rt2x00pci_register_read(rt2x00dev, PHY_CSR3, &reg);
  49. if (!rt2x00_get_field32(reg, PHY_CSR3_BUSY))
  50. break;
  51. udelay(REGISTER_BUSY_DELAY);
  52. }
  53. return reg;
  54. }
  55. static void rt61pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  56. const unsigned int word, const u8 value)
  57. {
  58. u32 reg;
  59. /*
  60. * Wait until the BBP becomes ready.
  61. */
  62. reg = rt61pci_bbp_check(rt2x00dev);
  63. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  64. ERROR(rt2x00dev, "PHY_CSR3 register busy. Write failed.\n");
  65. return;
  66. }
  67. /*
  68. * Write the data into the BBP.
  69. */
  70. reg = 0;
  71. rt2x00_set_field32(&reg, PHY_CSR3_VALUE, value);
  72. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  73. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  74. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 0);
  75. rt2x00pci_register_write(rt2x00dev, PHY_CSR3, reg);
  76. }
  77. static void rt61pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  78. const unsigned int word, u8 *value)
  79. {
  80. u32 reg;
  81. /*
  82. * Wait until the BBP becomes ready.
  83. */
  84. reg = rt61pci_bbp_check(rt2x00dev);
  85. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  86. ERROR(rt2x00dev, "PHY_CSR3 register busy. Read failed.\n");
  87. return;
  88. }
  89. /*
  90. * Write the request into the BBP.
  91. */
  92. reg = 0;
  93. rt2x00_set_field32(&reg, PHY_CSR3_REGNUM, word);
  94. rt2x00_set_field32(&reg, PHY_CSR3_BUSY, 1);
  95. rt2x00_set_field32(&reg, PHY_CSR3_READ_CONTROL, 1);
  96. rt2x00pci_register_write(rt2x00dev, PHY_CSR3, reg);
  97. /*
  98. * Wait until the BBP becomes ready.
  99. */
  100. reg = rt61pci_bbp_check(rt2x00dev);
  101. if (rt2x00_get_field32(reg, PHY_CSR3_BUSY)) {
  102. ERROR(rt2x00dev, "PHY_CSR3 register busy. Read failed.\n");
  103. *value = 0xff;
  104. return;
  105. }
  106. *value = rt2x00_get_field32(reg, PHY_CSR3_VALUE);
  107. }
  108. static void rt61pci_rf_write(struct rt2x00_dev *rt2x00dev,
  109. const unsigned int word, const u32 value)
  110. {
  111. u32 reg;
  112. unsigned int i;
  113. if (!word)
  114. return;
  115. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  116. rt2x00pci_register_read(rt2x00dev, PHY_CSR4, &reg);
  117. if (!rt2x00_get_field32(reg, PHY_CSR4_BUSY))
  118. goto rf_write;
  119. udelay(REGISTER_BUSY_DELAY);
  120. }
  121. ERROR(rt2x00dev, "PHY_CSR4 register busy. Write failed.\n");
  122. return;
  123. rf_write:
  124. reg = 0;
  125. rt2x00_set_field32(&reg, PHY_CSR4_VALUE, value);
  126. rt2x00_set_field32(&reg, PHY_CSR4_NUMBER_OF_BITS, 21);
  127. rt2x00_set_field32(&reg, PHY_CSR4_IF_SELECT, 0);
  128. rt2x00_set_field32(&reg, PHY_CSR4_BUSY, 1);
  129. rt2x00pci_register_write(rt2x00dev, PHY_CSR4, reg);
  130. rt2x00_rf_write(rt2x00dev, word, value);
  131. }
  132. static void rt61pci_mcu_request(struct rt2x00_dev *rt2x00dev,
  133. const u8 command, const u8 token,
  134. const u8 arg0, const u8 arg1)
  135. {
  136. u32 reg;
  137. rt2x00pci_register_read(rt2x00dev, H2M_MAILBOX_CSR, &reg);
  138. if (rt2x00_get_field32(reg, H2M_MAILBOX_CSR_OWNER)) {
  139. ERROR(rt2x00dev, "mcu request error. "
  140. "Request 0x%02x failed for token 0x%02x.\n",
  141. command, token);
  142. return;
  143. }
  144. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  145. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  146. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  147. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  148. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, reg);
  149. rt2x00pci_register_read(rt2x00dev, HOST_CMD_CSR, &reg);
  150. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  151. rt2x00_set_field32(&reg, HOST_CMD_CSR_INTERRUPT_MCU, 1);
  152. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, reg);
  153. }
  154. static void rt61pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  155. {
  156. struct rt2x00_dev *rt2x00dev = eeprom->data;
  157. u32 reg;
  158. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  159. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  160. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  161. eeprom->reg_data_clock =
  162. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  163. eeprom->reg_chip_select =
  164. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  165. }
  166. static void rt61pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  167. {
  168. struct rt2x00_dev *rt2x00dev = eeprom->data;
  169. u32 reg = 0;
  170. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  171. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  172. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  173. !!eeprom->reg_data_clock);
  174. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  175. !!eeprom->reg_chip_select);
  176. rt2x00pci_register_write(rt2x00dev, E2PROM_CSR, reg);
  177. }
  178. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  179. #define CSR_OFFSET(__word) ( CSR_REG_BASE + ((__word) * sizeof(u32)) )
  180. static void rt61pci_read_csr(struct rt2x00_dev *rt2x00dev,
  181. const unsigned int word, u32 *data)
  182. {
  183. rt2x00pci_register_read(rt2x00dev, CSR_OFFSET(word), data);
  184. }
  185. static void rt61pci_write_csr(struct rt2x00_dev *rt2x00dev,
  186. const unsigned int word, u32 data)
  187. {
  188. rt2x00pci_register_write(rt2x00dev, CSR_OFFSET(word), data);
  189. }
  190. static const struct rt2x00debug rt61pci_rt2x00debug = {
  191. .owner = THIS_MODULE,
  192. .csr = {
  193. .read = rt61pci_read_csr,
  194. .write = rt61pci_write_csr,
  195. .word_size = sizeof(u32),
  196. .word_count = CSR_REG_SIZE / sizeof(u32),
  197. },
  198. .eeprom = {
  199. .read = rt2x00_eeprom_read,
  200. .write = rt2x00_eeprom_write,
  201. .word_size = sizeof(u16),
  202. .word_count = EEPROM_SIZE / sizeof(u16),
  203. },
  204. .bbp = {
  205. .read = rt61pci_bbp_read,
  206. .write = rt61pci_bbp_write,
  207. .word_size = sizeof(u8),
  208. .word_count = BBP_SIZE / sizeof(u8),
  209. },
  210. .rf = {
  211. .read = rt2x00_rf_read,
  212. .write = rt61pci_rf_write,
  213. .word_size = sizeof(u32),
  214. .word_count = RF_SIZE / sizeof(u32),
  215. },
  216. };
  217. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  218. #ifdef CONFIG_RT61PCI_RFKILL
  219. static int rt61pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  220. {
  221. u32 reg;
  222. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  223. return rt2x00_get_field32(reg, MAC_CSR13_BIT5);;
  224. }
  225. #else
  226. #define rt61pci_rfkill_poll NULL
  227. #endif /* CONFIG_RT61PCI_RFKILL */
  228. /*
  229. * Configuration handlers.
  230. */
  231. static void rt61pci_config_mac_addr(struct rt2x00_dev *rt2x00dev, __le32 *mac)
  232. {
  233. u32 tmp;
  234. tmp = le32_to_cpu(mac[1]);
  235. rt2x00_set_field32(&tmp, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  236. mac[1] = cpu_to_le32(tmp);
  237. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR2, mac,
  238. (2 * sizeof(__le32)));
  239. }
  240. static void rt61pci_config_bssid(struct rt2x00_dev *rt2x00dev, __le32 *bssid)
  241. {
  242. u32 tmp;
  243. tmp = le32_to_cpu(bssid[1]);
  244. rt2x00_set_field32(&tmp, MAC_CSR5_BSS_ID_MASK, 3);
  245. bssid[1] = cpu_to_le32(tmp);
  246. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR4, bssid,
  247. (2 * sizeof(__le32)));
  248. }
  249. static void rt61pci_config_type(struct rt2x00_dev *rt2x00dev, const int type,
  250. const int tsf_sync)
  251. {
  252. u32 reg;
  253. /*
  254. * Clear current synchronisation setup.
  255. * For the Beacon base registers we only need to clear
  256. * the first byte since that byte contains the VALID and OWNER
  257. * bits which (when set to 0) will invalidate the entire beacon.
  258. */
  259. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, 0);
  260. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  261. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  262. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  263. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  264. /*
  265. * Enable synchronisation.
  266. */
  267. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  268. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  269. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE,
  270. (tsf_sync == TSF_SYNC_BEACON));
  271. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  272. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, tsf_sync);
  273. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  274. }
  275. static void rt61pci_config_preamble(struct rt2x00_dev *rt2x00dev,
  276. const int short_preamble,
  277. const int ack_timeout,
  278. const int ack_consume_time)
  279. {
  280. u32 reg;
  281. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  282. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, ack_timeout);
  283. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  284. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  285. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  286. !!short_preamble);
  287. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  288. }
  289. static void rt61pci_config_phymode(struct rt2x00_dev *rt2x00dev,
  290. const int basic_rate_mask)
  291. {
  292. rt2x00pci_register_write(rt2x00dev, TXRX_CSR5, basic_rate_mask);
  293. }
  294. static void rt61pci_config_channel(struct rt2x00_dev *rt2x00dev,
  295. struct rf_channel *rf, const int txpower)
  296. {
  297. u8 r3;
  298. u8 r94;
  299. u8 smart;
  300. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER, TXPOWER_TO_DEV(txpower));
  301. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  302. smart = !(rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  303. rt2x00_rf(&rt2x00dev->chip, RF2527));
  304. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  305. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE, smart);
  306. rt61pci_bbp_write(rt2x00dev, 3, r3);
  307. r94 = 6;
  308. if (txpower > MAX_TXPOWER && txpower <= (MAX_TXPOWER + r94))
  309. r94 += txpower - MAX_TXPOWER;
  310. else if (txpower < MIN_TXPOWER && txpower >= (MIN_TXPOWER - r94))
  311. r94 += txpower;
  312. rt61pci_bbp_write(rt2x00dev, 94, r94);
  313. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  314. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  315. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  316. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  317. udelay(200);
  318. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  319. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  320. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  321. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  322. udelay(200);
  323. rt61pci_rf_write(rt2x00dev, 1, rf->rf1);
  324. rt61pci_rf_write(rt2x00dev, 2, rf->rf2);
  325. rt61pci_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  326. rt61pci_rf_write(rt2x00dev, 4, rf->rf4);
  327. msleep(1);
  328. }
  329. static void rt61pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  330. const int txpower)
  331. {
  332. struct rf_channel rf;
  333. rt2x00_rf_read(rt2x00dev, 1, &rf.rf1);
  334. rt2x00_rf_read(rt2x00dev, 2, &rf.rf2);
  335. rt2x00_rf_read(rt2x00dev, 3, &rf.rf3);
  336. rt2x00_rf_read(rt2x00dev, 4, &rf.rf4);
  337. rt61pci_config_channel(rt2x00dev, &rf, txpower);
  338. }
  339. static void rt61pci_config_antenna_5x(struct rt2x00_dev *rt2x00dev,
  340. struct antenna_setup *ant)
  341. {
  342. u8 r3;
  343. u8 r4;
  344. u8 r77;
  345. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  346. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  347. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  348. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE,
  349. rt2x00_rf(&rt2x00dev->chip, RF5325));
  350. /*
  351. * Configure the RX antenna.
  352. */
  353. switch (ant->rx) {
  354. case ANTENNA_HW_DIVERSITY:
  355. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  356. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  357. (rt2x00dev->curr_hwmode != HWMODE_A));
  358. break;
  359. case ANTENNA_A:
  360. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  361. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  362. if (rt2x00dev->curr_hwmode == HWMODE_A)
  363. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  364. else
  365. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  366. break;
  367. case ANTENNA_SW_DIVERSITY:
  368. /*
  369. * NOTE: We should never come here because rt2x00lib is
  370. * supposed to catch this and send us the correct antenna
  371. * explicitely. However we are nog going to bug about this.
  372. * Instead, just default to antenna B.
  373. */
  374. case ANTENNA_B:
  375. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  376. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END, 0);
  377. if (rt2x00dev->curr_hwmode == HWMODE_A)
  378. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  379. else
  380. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  381. break;
  382. }
  383. rt61pci_bbp_write(rt2x00dev, 77, r77);
  384. rt61pci_bbp_write(rt2x00dev, 3, r3);
  385. rt61pci_bbp_write(rt2x00dev, 4, r4);
  386. }
  387. static void rt61pci_config_antenna_2x(struct rt2x00_dev *rt2x00dev,
  388. struct antenna_setup *ant)
  389. {
  390. u8 r3;
  391. u8 r4;
  392. u8 r77;
  393. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  394. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  395. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  396. rt2x00_set_field8(&r3, BBP_R3_SMART_MODE,
  397. rt2x00_rf(&rt2x00dev->chip, RF2529));
  398. rt2x00_set_field8(&r4, BBP_R4_RX_FRAME_END,
  399. !test_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags));
  400. /*
  401. * Configure the RX antenna.
  402. */
  403. switch (ant->rx) {
  404. case ANTENNA_HW_DIVERSITY:
  405. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 2);
  406. break;
  407. case ANTENNA_A:
  408. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  409. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  410. break;
  411. case ANTENNA_SW_DIVERSITY:
  412. /*
  413. * NOTE: We should never come here because rt2x00lib is
  414. * supposed to catch this and send us the correct antenna
  415. * explicitely. However we are nog going to bug about this.
  416. * Instead, just default to antenna B.
  417. */
  418. case ANTENNA_B:
  419. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  420. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  421. break;
  422. }
  423. rt61pci_bbp_write(rt2x00dev, 77, r77);
  424. rt61pci_bbp_write(rt2x00dev, 3, r3);
  425. rt61pci_bbp_write(rt2x00dev, 4, r4);
  426. }
  427. static void rt61pci_config_antenna_2529_rx(struct rt2x00_dev *rt2x00dev,
  428. const int p1, const int p2)
  429. {
  430. u32 reg;
  431. rt2x00pci_register_read(rt2x00dev, MAC_CSR13, &reg);
  432. rt2x00_set_field32(&reg, MAC_CSR13_BIT4, p1);
  433. rt2x00_set_field32(&reg, MAC_CSR13_BIT12, 0);
  434. rt2x00_set_field32(&reg, MAC_CSR13_BIT3, !p2);
  435. rt2x00_set_field32(&reg, MAC_CSR13_BIT11, 0);
  436. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, reg);
  437. }
  438. static void rt61pci_config_antenna_2529(struct rt2x00_dev *rt2x00dev,
  439. struct antenna_setup *ant)
  440. {
  441. u8 r3;
  442. u8 r4;
  443. u8 r77;
  444. rt61pci_bbp_read(rt2x00dev, 3, &r3);
  445. rt61pci_bbp_read(rt2x00dev, 4, &r4);
  446. rt61pci_bbp_read(rt2x00dev, 77, &r77);
  447. /* FIXME: Antenna selection for the rf 2529 is very confusing in the
  448. * legacy driver. The code below should be ok for non-diversity setups.
  449. */
  450. /*
  451. * Configure the RX antenna.
  452. */
  453. switch (ant->rx) {
  454. case ANTENNA_A:
  455. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  456. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 0);
  457. rt61pci_config_antenna_2529_rx(rt2x00dev, 0, 0);
  458. break;
  459. case ANTENNA_SW_DIVERSITY:
  460. case ANTENNA_HW_DIVERSITY:
  461. /*
  462. * NOTE: We should never come here because rt2x00lib is
  463. * supposed to catch this and send us the correct antenna
  464. * explicitely. However we are nog going to bug about this.
  465. * Instead, just default to antenna B.
  466. */
  467. case ANTENNA_B:
  468. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA_CONTROL, 1);
  469. rt2x00_set_field8(&r77, BBP_R77_RX_ANTENNA, 3);
  470. rt61pci_config_antenna_2529_rx(rt2x00dev, 1, 1);
  471. break;
  472. }
  473. rt61pci_bbp_write(rt2x00dev, 77, r77);
  474. rt61pci_bbp_write(rt2x00dev, 3, r3);
  475. rt61pci_bbp_write(rt2x00dev, 4, r4);
  476. }
  477. struct antenna_sel {
  478. u8 word;
  479. /*
  480. * value[0] -> non-LNA
  481. * value[1] -> LNA
  482. */
  483. u8 value[2];
  484. };
  485. static const struct antenna_sel antenna_sel_a[] = {
  486. { 96, { 0x58, 0x78 } },
  487. { 104, { 0x38, 0x48 } },
  488. { 75, { 0xfe, 0x80 } },
  489. { 86, { 0xfe, 0x80 } },
  490. { 88, { 0xfe, 0x80 } },
  491. { 35, { 0x60, 0x60 } },
  492. { 97, { 0x58, 0x58 } },
  493. { 98, { 0x58, 0x58 } },
  494. };
  495. static const struct antenna_sel antenna_sel_bg[] = {
  496. { 96, { 0x48, 0x68 } },
  497. { 104, { 0x2c, 0x3c } },
  498. { 75, { 0xfe, 0x80 } },
  499. { 86, { 0xfe, 0x80 } },
  500. { 88, { 0xfe, 0x80 } },
  501. { 35, { 0x50, 0x50 } },
  502. { 97, { 0x48, 0x48 } },
  503. { 98, { 0x48, 0x48 } },
  504. };
  505. static void rt61pci_config_antenna(struct rt2x00_dev *rt2x00dev,
  506. struct antenna_setup *ant)
  507. {
  508. const struct antenna_sel *sel;
  509. unsigned int lna;
  510. unsigned int i;
  511. u32 reg;
  512. if (rt2x00dev->curr_hwmode == HWMODE_A) {
  513. sel = antenna_sel_a;
  514. lna = test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  515. } else {
  516. sel = antenna_sel_bg;
  517. lna = test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  518. }
  519. for (i = 0; i < ARRAY_SIZE(antenna_sel_a); i++)
  520. rt61pci_bbp_write(rt2x00dev, sel[i].word, sel[i].value[lna]);
  521. rt2x00pci_register_read(rt2x00dev, PHY_CSR0, &reg);
  522. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_BG,
  523. (rt2x00dev->curr_hwmode == HWMODE_B ||
  524. rt2x00dev->curr_hwmode == HWMODE_G));
  525. rt2x00_set_field32(&reg, PHY_CSR0_PA_PE_A,
  526. (rt2x00dev->curr_hwmode == HWMODE_A));
  527. rt2x00pci_register_write(rt2x00dev, PHY_CSR0, reg);
  528. if (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  529. rt2x00_rf(&rt2x00dev->chip, RF5325))
  530. rt61pci_config_antenna_5x(rt2x00dev, ant);
  531. else if (rt2x00_rf(&rt2x00dev->chip, RF2527))
  532. rt61pci_config_antenna_2x(rt2x00dev, ant);
  533. else if (rt2x00_rf(&rt2x00dev->chip, RF2529)) {
  534. if (test_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags))
  535. rt61pci_config_antenna_2x(rt2x00dev, ant);
  536. else
  537. rt61pci_config_antenna_2529(rt2x00dev, ant);
  538. }
  539. }
  540. static void rt61pci_config_duration(struct rt2x00_dev *rt2x00dev,
  541. struct rt2x00lib_conf *libconf)
  542. {
  543. u32 reg;
  544. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  545. rt2x00_set_field32(&reg, MAC_CSR9_SLOT_TIME, libconf->slot_time);
  546. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  547. rt2x00pci_register_read(rt2x00dev, MAC_CSR8, &reg);
  548. rt2x00_set_field32(&reg, MAC_CSR8_SIFS, libconf->sifs);
  549. rt2x00_set_field32(&reg, MAC_CSR8_SIFS_AFTER_RX_OFDM, 3);
  550. rt2x00_set_field32(&reg, MAC_CSR8_EIFS, libconf->eifs);
  551. rt2x00pci_register_write(rt2x00dev, MAC_CSR8, reg);
  552. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  553. rt2x00_set_field32(&reg, TXRX_CSR0_TSF_OFFSET, IEEE80211_HEADER);
  554. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  555. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  556. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_ENABLE, 1);
  557. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  558. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  559. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_INTERVAL,
  560. libconf->conf->beacon_int * 16);
  561. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  562. }
  563. static void rt61pci_config(struct rt2x00_dev *rt2x00dev,
  564. const unsigned int flags,
  565. struct rt2x00lib_conf *libconf)
  566. {
  567. if (flags & CONFIG_UPDATE_PHYMODE)
  568. rt61pci_config_phymode(rt2x00dev, libconf->basic_rates);
  569. if (flags & CONFIG_UPDATE_CHANNEL)
  570. rt61pci_config_channel(rt2x00dev, &libconf->rf,
  571. libconf->conf->power_level);
  572. if ((flags & CONFIG_UPDATE_TXPOWER) && !(flags & CONFIG_UPDATE_CHANNEL))
  573. rt61pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  574. if (flags & CONFIG_UPDATE_ANTENNA)
  575. rt61pci_config_antenna(rt2x00dev, &libconf->ant);
  576. if (flags & (CONFIG_UPDATE_SLOT_TIME | CONFIG_UPDATE_BEACON_INT))
  577. rt61pci_config_duration(rt2x00dev, libconf);
  578. }
  579. /*
  580. * LED functions.
  581. */
  582. static void rt61pci_enable_led(struct rt2x00_dev *rt2x00dev)
  583. {
  584. u32 reg;
  585. u8 arg0;
  586. u8 arg1;
  587. rt2x00pci_register_read(rt2x00dev, MAC_CSR14, &reg);
  588. rt2x00_set_field32(&reg, MAC_CSR14_ON_PERIOD, 70);
  589. rt2x00_set_field32(&reg, MAC_CSR14_OFF_PERIOD, 30);
  590. rt2x00pci_register_write(rt2x00dev, MAC_CSR14, reg);
  591. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_RADIO_STATUS, 1);
  592. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_LINK_A_STATUS,
  593. (rt2x00dev->rx_status.phymode == MODE_IEEE80211A));
  594. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_LINK_BG_STATUS,
  595. (rt2x00dev->rx_status.phymode != MODE_IEEE80211A));
  596. arg0 = rt2x00dev->led_reg & 0xff;
  597. arg1 = (rt2x00dev->led_reg >> 8) & 0xff;
  598. rt61pci_mcu_request(rt2x00dev, MCU_LED, 0xff, arg0, arg1);
  599. }
  600. static void rt61pci_disable_led(struct rt2x00_dev *rt2x00dev)
  601. {
  602. u16 led_reg;
  603. u8 arg0;
  604. u8 arg1;
  605. led_reg = rt2x00dev->led_reg;
  606. rt2x00_set_field16(&led_reg, MCU_LEDCS_RADIO_STATUS, 0);
  607. rt2x00_set_field16(&led_reg, MCU_LEDCS_LINK_BG_STATUS, 0);
  608. rt2x00_set_field16(&led_reg, MCU_LEDCS_LINK_A_STATUS, 0);
  609. arg0 = led_reg & 0xff;
  610. arg1 = (led_reg >> 8) & 0xff;
  611. rt61pci_mcu_request(rt2x00dev, MCU_LED, 0xff, arg0, arg1);
  612. }
  613. static void rt61pci_activity_led(struct rt2x00_dev *rt2x00dev, int rssi)
  614. {
  615. u8 led;
  616. if (rt2x00dev->led_mode != LED_MODE_SIGNAL_STRENGTH)
  617. return;
  618. /*
  619. * Led handling requires a positive value for the rssi,
  620. * to do that correctly we need to add the correction.
  621. */
  622. rssi += rt2x00dev->rssi_offset;
  623. if (rssi <= 30)
  624. led = 0;
  625. else if (rssi <= 39)
  626. led = 1;
  627. else if (rssi <= 49)
  628. led = 2;
  629. else if (rssi <= 53)
  630. led = 3;
  631. else if (rssi <= 63)
  632. led = 4;
  633. else
  634. led = 5;
  635. rt61pci_mcu_request(rt2x00dev, MCU_LED_STRENGTH, 0xff, led, 0);
  636. }
  637. /*
  638. * Link tuning
  639. */
  640. static void rt61pci_link_stats(struct rt2x00_dev *rt2x00dev,
  641. struct link_qual *qual)
  642. {
  643. u32 reg;
  644. /*
  645. * Update FCS error count from register.
  646. */
  647. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  648. qual->rx_failed = rt2x00_get_field32(reg, STA_CSR0_FCS_ERROR);
  649. /*
  650. * Update False CCA count from register.
  651. */
  652. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  653. qual->false_cca = rt2x00_get_field32(reg, STA_CSR1_FALSE_CCA_ERROR);
  654. }
  655. static void rt61pci_reset_tuner(struct rt2x00_dev *rt2x00dev)
  656. {
  657. rt61pci_bbp_write(rt2x00dev, 17, 0x20);
  658. rt2x00dev->link.vgc_level = 0x20;
  659. }
  660. static void rt61pci_link_tuner(struct rt2x00_dev *rt2x00dev)
  661. {
  662. int rssi = rt2x00_get_link_rssi(&rt2x00dev->link);
  663. u8 r17;
  664. u8 up_bound;
  665. u8 low_bound;
  666. /*
  667. * Update Led strength
  668. */
  669. rt61pci_activity_led(rt2x00dev, rssi);
  670. rt61pci_bbp_read(rt2x00dev, 17, &r17);
  671. /*
  672. * Determine r17 bounds.
  673. */
  674. if (rt2x00dev->rx_status.phymode == MODE_IEEE80211A) {
  675. low_bound = 0x28;
  676. up_bound = 0x48;
  677. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags)) {
  678. low_bound += 0x10;
  679. up_bound += 0x10;
  680. }
  681. } else {
  682. low_bound = 0x20;
  683. up_bound = 0x40;
  684. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  685. low_bound += 0x10;
  686. up_bound += 0x10;
  687. }
  688. }
  689. /*
  690. * Special big-R17 for very short distance
  691. */
  692. if (rssi >= -35) {
  693. if (r17 != 0x60)
  694. rt61pci_bbp_write(rt2x00dev, 17, 0x60);
  695. return;
  696. }
  697. /*
  698. * Special big-R17 for short distance
  699. */
  700. if (rssi >= -58) {
  701. if (r17 != up_bound)
  702. rt61pci_bbp_write(rt2x00dev, 17, up_bound);
  703. return;
  704. }
  705. /*
  706. * Special big-R17 for middle-short distance
  707. */
  708. if (rssi >= -66) {
  709. low_bound += 0x10;
  710. if (r17 != low_bound)
  711. rt61pci_bbp_write(rt2x00dev, 17, low_bound);
  712. return;
  713. }
  714. /*
  715. * Special mid-R17 for middle distance
  716. */
  717. if (rssi >= -74) {
  718. low_bound += 0x08;
  719. if (r17 != low_bound)
  720. rt61pci_bbp_write(rt2x00dev, 17, low_bound);
  721. return;
  722. }
  723. /*
  724. * Special case: Change up_bound based on the rssi.
  725. * Lower up_bound when rssi is weaker then -74 dBm.
  726. */
  727. up_bound -= 2 * (-74 - rssi);
  728. if (low_bound > up_bound)
  729. up_bound = low_bound;
  730. if (r17 > up_bound) {
  731. rt61pci_bbp_write(rt2x00dev, 17, up_bound);
  732. return;
  733. }
  734. /*
  735. * r17 does not yet exceed upper limit, continue and base
  736. * the r17 tuning on the false CCA count.
  737. */
  738. if (rt2x00dev->link.qual.false_cca > 512 && r17 < up_bound) {
  739. if (++r17 > up_bound)
  740. r17 = up_bound;
  741. rt61pci_bbp_write(rt2x00dev, 17, r17);
  742. } else if (rt2x00dev->link.qual.false_cca < 100 && r17 > low_bound) {
  743. if (--r17 < low_bound)
  744. r17 = low_bound;
  745. rt61pci_bbp_write(rt2x00dev, 17, r17);
  746. }
  747. }
  748. /*
  749. * Firmware name function.
  750. */
  751. static char *rt61pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  752. {
  753. char *fw_name;
  754. switch (rt2x00dev->chip.rt) {
  755. case RT2561:
  756. fw_name = FIRMWARE_RT2561;
  757. break;
  758. case RT2561s:
  759. fw_name = FIRMWARE_RT2561s;
  760. break;
  761. case RT2661:
  762. fw_name = FIRMWARE_RT2661;
  763. break;
  764. default:
  765. fw_name = NULL;
  766. break;
  767. }
  768. return fw_name;
  769. }
  770. /*
  771. * Initialization functions.
  772. */
  773. static int rt61pci_load_firmware(struct rt2x00_dev *rt2x00dev, void *data,
  774. const size_t len)
  775. {
  776. int i;
  777. u32 reg;
  778. /*
  779. * Wait for stable hardware.
  780. */
  781. for (i = 0; i < 100; i++) {
  782. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  783. if (reg)
  784. break;
  785. msleep(1);
  786. }
  787. if (!reg) {
  788. ERROR(rt2x00dev, "Unstable hardware.\n");
  789. return -EBUSY;
  790. }
  791. /*
  792. * Prepare MCU and mailbox for firmware loading.
  793. */
  794. reg = 0;
  795. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  796. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  797. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  798. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  799. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, 0);
  800. /*
  801. * Write firmware to device.
  802. */
  803. reg = 0;
  804. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  805. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 1);
  806. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  807. rt2x00pci_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  808. data, len);
  809. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 0);
  810. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  811. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 0);
  812. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  813. for (i = 0; i < 100; i++) {
  814. rt2x00pci_register_read(rt2x00dev, MCU_CNTL_CSR, &reg);
  815. if (rt2x00_get_field32(reg, MCU_CNTL_CSR_READY))
  816. break;
  817. msleep(1);
  818. }
  819. if (i == 100) {
  820. ERROR(rt2x00dev, "MCU Control register not ready.\n");
  821. return -EBUSY;
  822. }
  823. /*
  824. * Reset MAC and BBP registers.
  825. */
  826. reg = 0;
  827. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  828. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  829. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  830. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  831. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  832. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  833. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  834. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  835. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  836. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  837. return 0;
  838. }
  839. static void rt61pci_init_rxentry(struct rt2x00_dev *rt2x00dev,
  840. struct data_entry *entry)
  841. {
  842. __le32 *rxd = entry->priv;
  843. u32 word;
  844. rt2x00_desc_read(rxd, 5, &word);
  845. rt2x00_set_field32(&word, RXD_W5_BUFFER_PHYSICAL_ADDRESS,
  846. entry->data_dma);
  847. rt2x00_desc_write(rxd, 5, word);
  848. rt2x00_desc_read(rxd, 0, &word);
  849. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  850. rt2x00_desc_write(rxd, 0, word);
  851. }
  852. static void rt61pci_init_txentry(struct rt2x00_dev *rt2x00dev,
  853. struct data_entry *entry)
  854. {
  855. __le32 *txd = entry->priv;
  856. u32 word;
  857. rt2x00_desc_read(txd, 1, &word);
  858. rt2x00_set_field32(&word, TXD_W1_BUFFER_COUNT, 1);
  859. rt2x00_desc_write(txd, 1, word);
  860. rt2x00_desc_read(txd, 5, &word);
  861. rt2x00_set_field32(&word, TXD_W5_PID_TYPE, entry->ring->queue_idx);
  862. rt2x00_set_field32(&word, TXD_W5_PID_SUBTYPE, entry->entry_idx);
  863. rt2x00_desc_write(txd, 5, word);
  864. rt2x00_desc_read(txd, 6, &word);
  865. rt2x00_set_field32(&word, TXD_W6_BUFFER_PHYSICAL_ADDRESS,
  866. entry->data_dma);
  867. rt2x00_desc_write(txd, 6, word);
  868. rt2x00_desc_read(txd, 0, &word);
  869. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  870. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  871. rt2x00_desc_write(txd, 0, word);
  872. }
  873. static int rt61pci_init_rings(struct rt2x00_dev *rt2x00dev)
  874. {
  875. u32 reg;
  876. /*
  877. * Initialize registers.
  878. */
  879. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR0, &reg);
  880. rt2x00_set_field32(&reg, TX_RING_CSR0_AC0_RING_SIZE,
  881. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA0].stats.limit);
  882. rt2x00_set_field32(&reg, TX_RING_CSR0_AC1_RING_SIZE,
  883. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA1].stats.limit);
  884. rt2x00_set_field32(&reg, TX_RING_CSR0_AC2_RING_SIZE,
  885. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA2].stats.limit);
  886. rt2x00_set_field32(&reg, TX_RING_CSR0_AC3_RING_SIZE,
  887. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA3].stats.limit);
  888. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR0, reg);
  889. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR1, &reg);
  890. rt2x00_set_field32(&reg, TX_RING_CSR1_MGMT_RING_SIZE,
  891. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA4].stats.limit);
  892. rt2x00_set_field32(&reg, TX_RING_CSR1_TXD_SIZE,
  893. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA0].desc_size /
  894. 4);
  895. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR1, reg);
  896. rt2x00pci_register_read(rt2x00dev, AC0_BASE_CSR, &reg);
  897. rt2x00_set_field32(&reg, AC0_BASE_CSR_RING_REGISTER,
  898. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA0].data_dma);
  899. rt2x00pci_register_write(rt2x00dev, AC0_BASE_CSR, reg);
  900. rt2x00pci_register_read(rt2x00dev, AC1_BASE_CSR, &reg);
  901. rt2x00_set_field32(&reg, AC1_BASE_CSR_RING_REGISTER,
  902. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA1].data_dma);
  903. rt2x00pci_register_write(rt2x00dev, AC1_BASE_CSR, reg);
  904. rt2x00pci_register_read(rt2x00dev, AC2_BASE_CSR, &reg);
  905. rt2x00_set_field32(&reg, AC2_BASE_CSR_RING_REGISTER,
  906. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA2].data_dma);
  907. rt2x00pci_register_write(rt2x00dev, AC2_BASE_CSR, reg);
  908. rt2x00pci_register_read(rt2x00dev, AC3_BASE_CSR, &reg);
  909. rt2x00_set_field32(&reg, AC3_BASE_CSR_RING_REGISTER,
  910. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA3].data_dma);
  911. rt2x00pci_register_write(rt2x00dev, AC3_BASE_CSR, reg);
  912. rt2x00pci_register_read(rt2x00dev, MGMT_BASE_CSR, &reg);
  913. rt2x00_set_field32(&reg, MGMT_BASE_CSR_RING_REGISTER,
  914. rt2x00dev->tx[IEEE80211_TX_QUEUE_DATA4].data_dma);
  915. rt2x00pci_register_write(rt2x00dev, MGMT_BASE_CSR, reg);
  916. rt2x00pci_register_read(rt2x00dev, RX_RING_CSR, &reg);
  917. rt2x00_set_field32(&reg, RX_RING_CSR_RING_SIZE,
  918. rt2x00dev->rx->stats.limit);
  919. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_SIZE,
  920. rt2x00dev->rx->desc_size / 4);
  921. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_WRITEBACK_SIZE, 4);
  922. rt2x00pci_register_write(rt2x00dev, RX_RING_CSR, reg);
  923. rt2x00pci_register_read(rt2x00dev, RX_BASE_CSR, &reg);
  924. rt2x00_set_field32(&reg, RX_BASE_CSR_RING_REGISTER,
  925. rt2x00dev->rx->data_dma);
  926. rt2x00pci_register_write(rt2x00dev, RX_BASE_CSR, reg);
  927. rt2x00pci_register_read(rt2x00dev, TX_DMA_DST_CSR, &reg);
  928. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC0, 2);
  929. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC1, 2);
  930. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC2, 2);
  931. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC3, 2);
  932. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_MGMT, 0);
  933. rt2x00pci_register_write(rt2x00dev, TX_DMA_DST_CSR, reg);
  934. rt2x00pci_register_read(rt2x00dev, LOAD_TX_RING_CSR, &reg);
  935. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC0, 1);
  936. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC1, 1);
  937. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC2, 1);
  938. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC3, 1);
  939. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_MGMT, 1);
  940. rt2x00pci_register_write(rt2x00dev, LOAD_TX_RING_CSR, reg);
  941. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  942. rt2x00_set_field32(&reg, RX_CNTL_CSR_LOAD_RXD, 1);
  943. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  944. return 0;
  945. }
  946. static int rt61pci_init_registers(struct rt2x00_dev *rt2x00dev)
  947. {
  948. u32 reg;
  949. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  950. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  951. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  952. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  953. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  954. rt2x00pci_register_read(rt2x00dev, TXRX_CSR1, &reg);
  955. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  956. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  957. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  958. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  959. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  960. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  961. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  962. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  963. rt2x00pci_register_write(rt2x00dev, TXRX_CSR1, reg);
  964. /*
  965. * CCK TXD BBP registers
  966. */
  967. rt2x00pci_register_read(rt2x00dev, TXRX_CSR2, &reg);
  968. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  969. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  970. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  971. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  972. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  973. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  974. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  975. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  976. rt2x00pci_register_write(rt2x00dev, TXRX_CSR2, reg);
  977. /*
  978. * OFDM TXD BBP registers
  979. */
  980. rt2x00pci_register_read(rt2x00dev, TXRX_CSR3, &reg);
  981. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  982. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  983. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  984. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  985. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  986. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  987. rt2x00pci_register_write(rt2x00dev, TXRX_CSR3, reg);
  988. rt2x00pci_register_read(rt2x00dev, TXRX_CSR7, &reg);
  989. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  990. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  991. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  992. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  993. rt2x00pci_register_write(rt2x00dev, TXRX_CSR7, reg);
  994. rt2x00pci_register_read(rt2x00dev, TXRX_CSR8, &reg);
  995. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  996. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  997. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  998. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  999. rt2x00pci_register_write(rt2x00dev, TXRX_CSR8, reg);
  1000. rt2x00pci_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  1001. rt2x00pci_register_write(rt2x00dev, MAC_CSR6, 0x00000fff);
  1002. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  1003. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  1004. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  1005. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x0000071c);
  1006. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  1007. return -EBUSY;
  1008. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, 0x0000e000);
  1009. /*
  1010. * Invalidate all Shared Keys (SEC_CSR0),
  1011. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  1012. */
  1013. rt2x00pci_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  1014. rt2x00pci_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  1015. rt2x00pci_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  1016. rt2x00pci_register_write(rt2x00dev, PHY_CSR1, 0x000023b0);
  1017. rt2x00pci_register_write(rt2x00dev, PHY_CSR5, 0x060a100c);
  1018. rt2x00pci_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  1019. rt2x00pci_register_write(rt2x00dev, PHY_CSR7, 0x00000a08);
  1020. rt2x00pci_register_write(rt2x00dev, PCI_CFG_CSR, 0x28ca4404);
  1021. rt2x00pci_register_write(rt2x00dev, TEST_MODE_CSR, 0x00000200);
  1022. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1023. rt2x00pci_register_read(rt2x00dev, AC_TXOP_CSR0, &reg);
  1024. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC0_TX_OP, 0);
  1025. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC1_TX_OP, 0);
  1026. rt2x00pci_register_write(rt2x00dev, AC_TXOP_CSR0, reg);
  1027. rt2x00pci_register_read(rt2x00dev, AC_TXOP_CSR1, &reg);
  1028. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC2_TX_OP, 192);
  1029. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC3_TX_OP, 48);
  1030. rt2x00pci_register_write(rt2x00dev, AC_TXOP_CSR1, reg);
  1031. /*
  1032. * We must clear the error counters.
  1033. * These registers are cleared on read,
  1034. * so we may pass a useless variable to store the value.
  1035. */
  1036. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  1037. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  1038. rt2x00pci_register_read(rt2x00dev, STA_CSR2, &reg);
  1039. /*
  1040. * Reset MAC and BBP registers.
  1041. */
  1042. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1043. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1044. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1045. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1046. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1047. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1048. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1049. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1050. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1051. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1052. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1053. return 0;
  1054. }
  1055. static int rt61pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1056. {
  1057. unsigned int i;
  1058. u16 eeprom;
  1059. u8 reg_id;
  1060. u8 value;
  1061. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1062. rt61pci_bbp_read(rt2x00dev, 0, &value);
  1063. if ((value != 0xff) && (value != 0x00))
  1064. goto continue_csr_init;
  1065. NOTICE(rt2x00dev, "Waiting for BBP register.\n");
  1066. udelay(REGISTER_BUSY_DELAY);
  1067. }
  1068. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1069. return -EACCES;
  1070. continue_csr_init:
  1071. rt61pci_bbp_write(rt2x00dev, 3, 0x00);
  1072. rt61pci_bbp_write(rt2x00dev, 15, 0x30);
  1073. rt61pci_bbp_write(rt2x00dev, 21, 0xc8);
  1074. rt61pci_bbp_write(rt2x00dev, 22, 0x38);
  1075. rt61pci_bbp_write(rt2x00dev, 23, 0x06);
  1076. rt61pci_bbp_write(rt2x00dev, 24, 0xfe);
  1077. rt61pci_bbp_write(rt2x00dev, 25, 0x0a);
  1078. rt61pci_bbp_write(rt2x00dev, 26, 0x0d);
  1079. rt61pci_bbp_write(rt2x00dev, 34, 0x12);
  1080. rt61pci_bbp_write(rt2x00dev, 37, 0x07);
  1081. rt61pci_bbp_write(rt2x00dev, 39, 0xf8);
  1082. rt61pci_bbp_write(rt2x00dev, 41, 0x60);
  1083. rt61pci_bbp_write(rt2x00dev, 53, 0x10);
  1084. rt61pci_bbp_write(rt2x00dev, 54, 0x18);
  1085. rt61pci_bbp_write(rt2x00dev, 60, 0x10);
  1086. rt61pci_bbp_write(rt2x00dev, 61, 0x04);
  1087. rt61pci_bbp_write(rt2x00dev, 62, 0x04);
  1088. rt61pci_bbp_write(rt2x00dev, 75, 0xfe);
  1089. rt61pci_bbp_write(rt2x00dev, 86, 0xfe);
  1090. rt61pci_bbp_write(rt2x00dev, 88, 0xfe);
  1091. rt61pci_bbp_write(rt2x00dev, 90, 0x0f);
  1092. rt61pci_bbp_write(rt2x00dev, 99, 0x00);
  1093. rt61pci_bbp_write(rt2x00dev, 102, 0x16);
  1094. rt61pci_bbp_write(rt2x00dev, 107, 0x04);
  1095. DEBUG(rt2x00dev, "Start initialization from EEPROM...\n");
  1096. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1097. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1098. if (eeprom != 0xffff && eeprom != 0x0000) {
  1099. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1100. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1101. DEBUG(rt2x00dev, "BBP: 0x%02x, value: 0x%02x.\n",
  1102. reg_id, value);
  1103. rt61pci_bbp_write(rt2x00dev, reg_id, value);
  1104. }
  1105. }
  1106. DEBUG(rt2x00dev, "...End initialization from EEPROM.\n");
  1107. return 0;
  1108. }
  1109. /*
  1110. * Device state switch handlers.
  1111. */
  1112. static void rt61pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  1113. enum dev_state state)
  1114. {
  1115. u32 reg;
  1116. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1117. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX,
  1118. state == STATE_RADIO_RX_OFF);
  1119. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1120. }
  1121. static void rt61pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1122. enum dev_state state)
  1123. {
  1124. int mask = (state == STATE_RADIO_IRQ_OFF);
  1125. u32 reg;
  1126. /*
  1127. * When interrupts are being enabled, the interrupt registers
  1128. * should clear the register to assure a clean state.
  1129. */
  1130. if (state == STATE_RADIO_IRQ_ON) {
  1131. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1132. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1133. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg);
  1134. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg);
  1135. }
  1136. /*
  1137. * Only toggle the interrupts bits we are going to use.
  1138. * Non-checked interrupt bits are disabled by default.
  1139. */
  1140. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1141. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDONE, mask);
  1142. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDONE, mask);
  1143. rt2x00_set_field32(&reg, INT_MASK_CSR_ENABLE_MITIGATION, mask);
  1144. rt2x00_set_field32(&reg, INT_MASK_CSR_MITIGATION_PERIOD, 0xff);
  1145. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1146. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1147. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_0, mask);
  1148. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_1, mask);
  1149. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_2, mask);
  1150. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_3, mask);
  1151. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_4, mask);
  1152. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_5, mask);
  1153. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_6, mask);
  1154. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_7, mask);
  1155. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1156. }
  1157. static int rt61pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1158. {
  1159. u32 reg;
  1160. /*
  1161. * Initialize all registers.
  1162. */
  1163. if (rt61pci_init_rings(rt2x00dev) ||
  1164. rt61pci_init_registers(rt2x00dev) ||
  1165. rt61pci_init_bbp(rt2x00dev)) {
  1166. ERROR(rt2x00dev, "Register initialization failed.\n");
  1167. return -EIO;
  1168. }
  1169. /*
  1170. * Enable interrupts.
  1171. */
  1172. rt61pci_toggle_irq(rt2x00dev, STATE_RADIO_IRQ_ON);
  1173. /*
  1174. * Enable RX.
  1175. */
  1176. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1177. rt2x00_set_field32(&reg, RX_CNTL_CSR_ENABLE_RX_DMA, 1);
  1178. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1179. /*
  1180. * Enable LED
  1181. */
  1182. rt61pci_enable_led(rt2x00dev);
  1183. return 0;
  1184. }
  1185. static void rt61pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1186. {
  1187. u32 reg;
  1188. /*
  1189. * Disable LED
  1190. */
  1191. rt61pci_disable_led(rt2x00dev);
  1192. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  1193. /*
  1194. * Disable synchronisation.
  1195. */
  1196. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, 0);
  1197. /*
  1198. * Cancel RX and TX.
  1199. */
  1200. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1201. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC0, 1);
  1202. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC1, 1);
  1203. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC2, 1);
  1204. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC3, 1);
  1205. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_MGMT, 1);
  1206. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1207. /*
  1208. * Disable interrupts.
  1209. */
  1210. rt61pci_toggle_irq(rt2x00dev, STATE_RADIO_IRQ_OFF);
  1211. }
  1212. static int rt61pci_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  1213. {
  1214. u32 reg;
  1215. unsigned int i;
  1216. char put_to_sleep;
  1217. char current_state;
  1218. put_to_sleep = (state != STATE_AWAKE);
  1219. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1220. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  1221. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  1222. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1223. /*
  1224. * Device is not guaranteed to be in the requested state yet.
  1225. * We must wait until the register indicates that the
  1226. * device has entered the correct state.
  1227. */
  1228. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1229. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1230. current_state =
  1231. rt2x00_get_field32(reg, MAC_CSR12_BBP_CURRENT_STATE);
  1232. if (current_state == !put_to_sleep)
  1233. return 0;
  1234. msleep(10);
  1235. }
  1236. NOTICE(rt2x00dev, "Device failed to enter state %d, "
  1237. "current device state %d.\n", !put_to_sleep, current_state);
  1238. return -EBUSY;
  1239. }
  1240. static int rt61pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1241. enum dev_state state)
  1242. {
  1243. int retval = 0;
  1244. switch (state) {
  1245. case STATE_RADIO_ON:
  1246. retval = rt61pci_enable_radio(rt2x00dev);
  1247. break;
  1248. case STATE_RADIO_OFF:
  1249. rt61pci_disable_radio(rt2x00dev);
  1250. break;
  1251. case STATE_RADIO_RX_ON:
  1252. case STATE_RADIO_RX_OFF:
  1253. rt61pci_toggle_rx(rt2x00dev, state);
  1254. break;
  1255. case STATE_DEEP_SLEEP:
  1256. case STATE_SLEEP:
  1257. case STATE_STANDBY:
  1258. case STATE_AWAKE:
  1259. retval = rt61pci_set_state(rt2x00dev, state);
  1260. break;
  1261. default:
  1262. retval = -ENOTSUPP;
  1263. break;
  1264. }
  1265. return retval;
  1266. }
  1267. /*
  1268. * TX descriptor initialization
  1269. */
  1270. static void rt61pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  1271. struct sk_buff *skb,
  1272. struct txdata_entry_desc *desc,
  1273. struct ieee80211_tx_control *control)
  1274. {
  1275. struct skb_desc *skbdesc = get_skb_desc(skb);
  1276. __le32 *txd = skbdesc->desc;
  1277. u32 word;
  1278. /*
  1279. * Start writing the descriptor words.
  1280. */
  1281. rt2x00_desc_read(txd, 1, &word);
  1282. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, desc->queue);
  1283. rt2x00_set_field32(&word, TXD_W1_AIFSN, desc->aifs);
  1284. rt2x00_set_field32(&word, TXD_W1_CWMIN, desc->cw_min);
  1285. rt2x00_set_field32(&word, TXD_W1_CWMAX, desc->cw_max);
  1286. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, IEEE80211_HEADER);
  1287. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE, 1);
  1288. rt2x00_desc_write(txd, 1, word);
  1289. rt2x00_desc_read(txd, 2, &word);
  1290. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, desc->signal);
  1291. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, desc->service);
  1292. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW, desc->length_low);
  1293. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH, desc->length_high);
  1294. rt2x00_desc_write(txd, 2, word);
  1295. rt2x00_desc_read(txd, 5, &word);
  1296. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1297. TXPOWER_TO_DEV(control->power_level));
  1298. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1299. rt2x00_desc_write(txd, 5, word);
  1300. rt2x00_desc_read(txd, 11, &word);
  1301. rt2x00_set_field32(&word, TXD_W11_BUFFER_LENGTH0, skbdesc->data_len);
  1302. rt2x00_desc_write(txd, 11, word);
  1303. rt2x00_desc_read(txd, 0, &word);
  1304. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  1305. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1306. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1307. test_bit(ENTRY_TXD_MORE_FRAG, &desc->flags));
  1308. rt2x00_set_field32(&word, TXD_W0_ACK,
  1309. test_bit(ENTRY_TXD_ACK, &desc->flags));
  1310. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1311. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc->flags));
  1312. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1313. test_bit(ENTRY_TXD_OFDM_RATE, &desc->flags));
  1314. rt2x00_set_field32(&word, TXD_W0_IFS, desc->ifs);
  1315. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1316. !!(control->flags &
  1317. IEEE80211_TXCTL_LONG_RETRY_LIMIT));
  1318. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC, 0);
  1319. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, skbdesc->data_len);
  1320. rt2x00_set_field32(&word, TXD_W0_BURST,
  1321. test_bit(ENTRY_TXD_BURST, &desc->flags));
  1322. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, CIPHER_NONE);
  1323. rt2x00_desc_write(txd, 0, word);
  1324. }
  1325. /*
  1326. * TX data initialization
  1327. */
  1328. static void rt61pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  1329. unsigned int queue)
  1330. {
  1331. u32 reg;
  1332. if (queue == IEEE80211_TX_QUEUE_BEACON) {
  1333. /*
  1334. * For Wi-Fi faily generated beacons between participating
  1335. * stations. Set TBTT phase adaptive adjustment step to 8us.
  1336. */
  1337. rt2x00pci_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1338. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1339. if (!rt2x00_get_field32(reg, TXRX_CSR9_BEACON_GEN)) {
  1340. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1341. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1342. }
  1343. return;
  1344. }
  1345. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1346. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC0,
  1347. (queue == IEEE80211_TX_QUEUE_DATA0));
  1348. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC1,
  1349. (queue == IEEE80211_TX_QUEUE_DATA1));
  1350. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC2,
  1351. (queue == IEEE80211_TX_QUEUE_DATA2));
  1352. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC3,
  1353. (queue == IEEE80211_TX_QUEUE_DATA3));
  1354. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_MGMT,
  1355. (queue == IEEE80211_TX_QUEUE_DATA4));
  1356. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1357. }
  1358. /*
  1359. * RX control handlers
  1360. */
  1361. static int rt61pci_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1362. {
  1363. u16 eeprom;
  1364. u8 offset;
  1365. u8 lna;
  1366. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1367. switch (lna) {
  1368. case 3:
  1369. offset = 90;
  1370. break;
  1371. case 2:
  1372. offset = 74;
  1373. break;
  1374. case 1:
  1375. offset = 64;
  1376. break;
  1377. default:
  1378. return 0;
  1379. }
  1380. if (rt2x00dev->rx_status.phymode == MODE_IEEE80211A) {
  1381. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  1382. offset += 14;
  1383. if (lna == 3 || lna == 2)
  1384. offset += 10;
  1385. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  1386. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  1387. } else {
  1388. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
  1389. offset += 14;
  1390. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  1391. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  1392. }
  1393. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1394. }
  1395. static void rt61pci_fill_rxdone(struct data_entry *entry,
  1396. struct rxdata_entry_desc *desc)
  1397. {
  1398. __le32 *rxd = entry->priv;
  1399. u32 word0;
  1400. u32 word1;
  1401. rt2x00_desc_read(rxd, 0, &word0);
  1402. rt2x00_desc_read(rxd, 1, &word1);
  1403. desc->flags = 0;
  1404. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1405. desc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1406. /*
  1407. * Obtain the status about this packet.
  1408. */
  1409. desc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1410. desc->rssi = rt61pci_agc_to_rssi(entry->ring->rt2x00dev, word1);
  1411. desc->ofdm = rt2x00_get_field32(word0, RXD_W0_OFDM);
  1412. desc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1413. desc->my_bss = !!rt2x00_get_field32(word0, RXD_W0_MY_BSS);
  1414. }
  1415. /*
  1416. * Interrupt functions.
  1417. */
  1418. static void rt61pci_txdone(struct rt2x00_dev *rt2x00dev)
  1419. {
  1420. struct data_ring *ring;
  1421. struct data_entry *entry;
  1422. struct data_entry *entry_done;
  1423. __le32 *txd;
  1424. u32 word;
  1425. u32 reg;
  1426. u32 old_reg;
  1427. int type;
  1428. int index;
  1429. int tx_status;
  1430. int retry;
  1431. /*
  1432. * During each loop we will compare the freshly read
  1433. * STA_CSR4 register value with the value read from
  1434. * the previous loop. If the 2 values are equal then
  1435. * we should stop processing because the chance it
  1436. * quite big that the device has been unplugged and
  1437. * we risk going into an endless loop.
  1438. */
  1439. old_reg = 0;
  1440. while (1) {
  1441. rt2x00pci_register_read(rt2x00dev, STA_CSR4, &reg);
  1442. if (!rt2x00_get_field32(reg, STA_CSR4_VALID))
  1443. break;
  1444. if (old_reg == reg)
  1445. break;
  1446. old_reg = reg;
  1447. /*
  1448. * Skip this entry when it contains an invalid
  1449. * ring identication number.
  1450. */
  1451. type = rt2x00_get_field32(reg, STA_CSR4_PID_TYPE);
  1452. ring = rt2x00lib_get_ring(rt2x00dev, type);
  1453. if (unlikely(!ring))
  1454. continue;
  1455. /*
  1456. * Skip this entry when it contains an invalid
  1457. * index number.
  1458. */
  1459. index = rt2x00_get_field32(reg, STA_CSR4_PID_SUBTYPE);
  1460. if (unlikely(index >= ring->stats.limit))
  1461. continue;
  1462. entry = &ring->entry[index];
  1463. txd = entry->priv;
  1464. rt2x00_desc_read(txd, 0, &word);
  1465. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1466. !rt2x00_get_field32(word, TXD_W0_VALID))
  1467. return;
  1468. entry_done = rt2x00_get_data_entry_done(ring);
  1469. while (entry != entry_done) {
  1470. /* Catch up. Just report any entries we missed as
  1471. * failed. */
  1472. WARNING(rt2x00dev,
  1473. "TX status report missed for entry %p\n",
  1474. entry_done);
  1475. rt2x00pci_txdone(rt2x00dev, entry_done, TX_FAIL_OTHER,
  1476. 0);
  1477. entry_done = rt2x00_get_data_entry_done(ring);
  1478. }
  1479. /*
  1480. * Obtain the status about this packet.
  1481. */
  1482. tx_status = rt2x00_get_field32(reg, STA_CSR4_TX_RESULT);
  1483. retry = rt2x00_get_field32(reg, STA_CSR4_RETRY_COUNT);
  1484. rt2x00pci_txdone(rt2x00dev, entry, tx_status, retry);
  1485. }
  1486. }
  1487. static irqreturn_t rt61pci_interrupt(int irq, void *dev_instance)
  1488. {
  1489. struct rt2x00_dev *rt2x00dev = dev_instance;
  1490. u32 reg_mcu;
  1491. u32 reg;
  1492. /*
  1493. * Get the interrupt sources & saved to local variable.
  1494. * Write register value back to clear pending interrupts.
  1495. */
  1496. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg_mcu);
  1497. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg_mcu);
  1498. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1499. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1500. if (!reg && !reg_mcu)
  1501. return IRQ_NONE;
  1502. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  1503. return IRQ_HANDLED;
  1504. /*
  1505. * Handle interrupts, walk through all bits
  1506. * and run the tasks, the bits are checked in order of
  1507. * priority.
  1508. */
  1509. /*
  1510. * 1 - Rx ring done interrupt.
  1511. */
  1512. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RXDONE))
  1513. rt2x00pci_rxdone(rt2x00dev);
  1514. /*
  1515. * 2 - Tx ring done interrupt.
  1516. */
  1517. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TXDONE))
  1518. rt61pci_txdone(rt2x00dev);
  1519. /*
  1520. * 3 - Handle MCU command done.
  1521. */
  1522. if (reg_mcu)
  1523. rt2x00pci_register_write(rt2x00dev,
  1524. M2H_CMD_DONE_CSR, 0xffffffff);
  1525. return IRQ_HANDLED;
  1526. }
  1527. /*
  1528. * Device probe functions.
  1529. */
  1530. static int rt61pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  1531. {
  1532. struct eeprom_93cx6 eeprom;
  1533. u32 reg;
  1534. u16 word;
  1535. u8 *mac;
  1536. s8 value;
  1537. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  1538. eeprom.data = rt2x00dev;
  1539. eeprom.register_read = rt61pci_eepromregister_read;
  1540. eeprom.register_write = rt61pci_eepromregister_write;
  1541. eeprom.width = rt2x00_get_field32(reg, E2PROM_CSR_TYPE_93C46) ?
  1542. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  1543. eeprom.reg_data_in = 0;
  1544. eeprom.reg_data_out = 0;
  1545. eeprom.reg_data_clock = 0;
  1546. eeprom.reg_chip_select = 0;
  1547. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  1548. EEPROM_SIZE / sizeof(u16));
  1549. /*
  1550. * Start validation of the data that has been read.
  1551. */
  1552. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  1553. if (!is_valid_ether_addr(mac)) {
  1554. DECLARE_MAC_BUF(macbuf);
  1555. random_ether_addr(mac);
  1556. EEPROM(rt2x00dev, "MAC: %s\n", print_mac(macbuf, mac));
  1557. }
  1558. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  1559. if (word == 0xffff) {
  1560. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  1561. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  1562. ANTENNA_B);
  1563. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  1564. ANTENNA_B);
  1565. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  1566. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  1567. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  1568. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5225);
  1569. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  1570. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  1571. }
  1572. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  1573. if (word == 0xffff) {
  1574. rt2x00_set_field16(&word, EEPROM_NIC_ENABLE_DIVERSITY, 0);
  1575. rt2x00_set_field16(&word, EEPROM_NIC_TX_DIVERSITY, 0);
  1576. rt2x00_set_field16(&word, EEPROM_NIC_TX_RX_FIXED, 0);
  1577. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  1578. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  1579. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  1580. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  1581. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  1582. }
  1583. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  1584. if (word == 0xffff) {
  1585. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  1586. LED_MODE_DEFAULT);
  1587. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  1588. EEPROM(rt2x00dev, "Led: 0x%04x\n", word);
  1589. }
  1590. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  1591. if (word == 0xffff) {
  1592. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  1593. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  1594. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  1595. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  1596. }
  1597. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  1598. if (word == 0xffff) {
  1599. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1600. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1601. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1602. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  1603. } else {
  1604. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  1605. if (value < -10 || value > 10)
  1606. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1607. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  1608. if (value < -10 || value > 10)
  1609. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1610. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1611. }
  1612. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  1613. if (word == 0xffff) {
  1614. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1615. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1616. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1617. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  1618. } else {
  1619. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  1620. if (value < -10 || value > 10)
  1621. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1622. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  1623. if (value < -10 || value > 10)
  1624. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1625. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1626. }
  1627. return 0;
  1628. }
  1629. static int rt61pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  1630. {
  1631. u32 reg;
  1632. u16 value;
  1633. u16 eeprom;
  1634. u16 device;
  1635. /*
  1636. * Read EEPROM word for configuration.
  1637. */
  1638. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  1639. /*
  1640. * Identify RF chipset.
  1641. * To determine the RT chip we have to read the
  1642. * PCI header of the device.
  1643. */
  1644. pci_read_config_word(rt2x00dev_pci(rt2x00dev),
  1645. PCI_CONFIG_HEADER_DEVICE, &device);
  1646. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  1647. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  1648. rt2x00_set_chip(rt2x00dev, device, value, reg);
  1649. if (!rt2x00_rf(&rt2x00dev->chip, RF5225) &&
  1650. !rt2x00_rf(&rt2x00dev->chip, RF5325) &&
  1651. !rt2x00_rf(&rt2x00dev->chip, RF2527) &&
  1652. !rt2x00_rf(&rt2x00dev->chip, RF2529)) {
  1653. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  1654. return -ENODEV;
  1655. }
  1656. /*
  1657. * Determine number of antenna's.
  1658. */
  1659. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_NUM) == 2)
  1660. __set_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags);
  1661. /*
  1662. * Identify default antenna configuration.
  1663. */
  1664. rt2x00dev->default_ant.tx =
  1665. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  1666. rt2x00dev->default_ant.rx =
  1667. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  1668. /*
  1669. * Read the Frame type.
  1670. */
  1671. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  1672. __set_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags);
  1673. /*
  1674. * Detect if this device has an hardware controlled radio.
  1675. */
  1676. #ifdef CONFIG_RT61PCI_RFKILL
  1677. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  1678. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  1679. #endif /* CONFIG_RT61PCI_RFKILL */
  1680. /*
  1681. * Read frequency offset and RF programming sequence.
  1682. */
  1683. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  1684. if (rt2x00_get_field16(eeprom, EEPROM_FREQ_SEQ))
  1685. __set_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags);
  1686. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  1687. /*
  1688. * Read external LNA informations.
  1689. */
  1690. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  1691. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  1692. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  1693. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  1694. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  1695. /*
  1696. * When working with a RF2529 chip without double antenna
  1697. * the antenna settings should be gathered from the NIC
  1698. * eeprom word.
  1699. */
  1700. if (rt2x00_rf(&rt2x00dev->chip, RF2529) &&
  1701. !test_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags)) {
  1702. switch (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_RX_FIXED)) {
  1703. case 0:
  1704. rt2x00dev->default_ant.tx = ANTENNA_B;
  1705. rt2x00dev->default_ant.rx = ANTENNA_A;
  1706. break;
  1707. case 1:
  1708. rt2x00dev->default_ant.tx = ANTENNA_B;
  1709. rt2x00dev->default_ant.rx = ANTENNA_B;
  1710. break;
  1711. case 2:
  1712. rt2x00dev->default_ant.tx = ANTENNA_A;
  1713. rt2x00dev->default_ant.rx = ANTENNA_A;
  1714. break;
  1715. case 3:
  1716. rt2x00dev->default_ant.tx = ANTENNA_A;
  1717. rt2x00dev->default_ant.rx = ANTENNA_B;
  1718. break;
  1719. }
  1720. if (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_DIVERSITY))
  1721. rt2x00dev->default_ant.tx = ANTENNA_SW_DIVERSITY;
  1722. if (rt2x00_get_field16(eeprom, EEPROM_NIC_ENABLE_DIVERSITY))
  1723. rt2x00dev->default_ant.rx = ANTENNA_SW_DIVERSITY;
  1724. }
  1725. /*
  1726. * Store led settings, for correct led behaviour.
  1727. * If the eeprom value is invalid,
  1728. * switch to default led mode.
  1729. */
  1730. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  1731. rt2x00dev->led_mode = rt2x00_get_field16(eeprom, EEPROM_LED_LED_MODE);
  1732. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_LED_MODE,
  1733. rt2x00dev->led_mode);
  1734. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_0,
  1735. rt2x00_get_field16(eeprom,
  1736. EEPROM_LED_POLARITY_GPIO_0));
  1737. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_1,
  1738. rt2x00_get_field16(eeprom,
  1739. EEPROM_LED_POLARITY_GPIO_1));
  1740. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_2,
  1741. rt2x00_get_field16(eeprom,
  1742. EEPROM_LED_POLARITY_GPIO_2));
  1743. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_3,
  1744. rt2x00_get_field16(eeprom,
  1745. EEPROM_LED_POLARITY_GPIO_3));
  1746. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_4,
  1747. rt2x00_get_field16(eeprom,
  1748. EEPROM_LED_POLARITY_GPIO_4));
  1749. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_ACT,
  1750. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  1751. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_READY_BG,
  1752. rt2x00_get_field16(eeprom,
  1753. EEPROM_LED_POLARITY_RDY_G));
  1754. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_READY_A,
  1755. rt2x00_get_field16(eeprom,
  1756. EEPROM_LED_POLARITY_RDY_A));
  1757. return 0;
  1758. }
  1759. /*
  1760. * RF value list for RF5225 & RF5325
  1761. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence disabled
  1762. */
  1763. static const struct rf_channel rf_vals_noseq[] = {
  1764. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  1765. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  1766. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  1767. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  1768. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  1769. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  1770. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  1771. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  1772. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  1773. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  1774. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  1775. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  1776. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  1777. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  1778. /* 802.11 UNI / HyperLan 2 */
  1779. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  1780. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  1781. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  1782. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  1783. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  1784. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  1785. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  1786. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  1787. /* 802.11 HyperLan 2 */
  1788. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  1789. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  1790. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  1791. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  1792. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  1793. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  1794. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  1795. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  1796. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  1797. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  1798. /* 802.11 UNII */
  1799. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  1800. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  1801. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  1802. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  1803. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  1804. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  1805. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1806. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  1807. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  1808. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  1809. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  1810. };
  1811. /*
  1812. * RF value list for RF5225 & RF5325
  1813. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence enabled
  1814. */
  1815. static const struct rf_channel rf_vals_seq[] = {
  1816. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  1817. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  1818. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  1819. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  1820. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  1821. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  1822. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  1823. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  1824. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  1825. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  1826. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  1827. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  1828. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  1829. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  1830. /* 802.11 UNI / HyperLan 2 */
  1831. { 36, 0x00002cd4, 0x0004481a, 0x00098455, 0x000c0a03 },
  1832. { 40, 0x00002cd0, 0x00044682, 0x00098455, 0x000c0a03 },
  1833. { 44, 0x00002cd0, 0x00044686, 0x00098455, 0x000c0a1b },
  1834. { 48, 0x00002cd0, 0x0004468e, 0x00098655, 0x000c0a0b },
  1835. { 52, 0x00002cd0, 0x00044692, 0x00098855, 0x000c0a23 },
  1836. { 56, 0x00002cd0, 0x0004469a, 0x00098c55, 0x000c0a13 },
  1837. { 60, 0x00002cd0, 0x000446a2, 0x00098e55, 0x000c0a03 },
  1838. { 64, 0x00002cd0, 0x000446a6, 0x00099255, 0x000c0a1b },
  1839. /* 802.11 HyperLan 2 */
  1840. { 100, 0x00002cd4, 0x0004489a, 0x000b9855, 0x000c0a03 },
  1841. { 104, 0x00002cd4, 0x000448a2, 0x000b9855, 0x000c0a03 },
  1842. { 108, 0x00002cd4, 0x000448aa, 0x000b9855, 0x000c0a03 },
  1843. { 112, 0x00002cd4, 0x000448b2, 0x000b9a55, 0x000c0a03 },
  1844. { 116, 0x00002cd4, 0x000448ba, 0x000b9a55, 0x000c0a03 },
  1845. { 120, 0x00002cd0, 0x00044702, 0x000b9a55, 0x000c0a03 },
  1846. { 124, 0x00002cd0, 0x00044706, 0x000b9a55, 0x000c0a1b },
  1847. { 128, 0x00002cd0, 0x0004470e, 0x000b9c55, 0x000c0a0b },
  1848. { 132, 0x00002cd0, 0x00044712, 0x000b9c55, 0x000c0a23 },
  1849. { 136, 0x00002cd0, 0x0004471a, 0x000b9e55, 0x000c0a13 },
  1850. /* 802.11 UNII */
  1851. { 140, 0x00002cd0, 0x00044722, 0x000b9e55, 0x000c0a03 },
  1852. { 149, 0x00002cd0, 0x0004472e, 0x000ba255, 0x000c0a1b },
  1853. { 153, 0x00002cd0, 0x00044736, 0x000ba255, 0x000c0a0b },
  1854. { 157, 0x00002cd4, 0x0004490a, 0x000ba255, 0x000c0a17 },
  1855. { 161, 0x00002cd4, 0x00044912, 0x000ba255, 0x000c0a17 },
  1856. { 165, 0x00002cd4, 0x0004491a, 0x000ba255, 0x000c0a17 },
  1857. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1858. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000c0a0b },
  1859. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000c0a13 },
  1860. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000c0a1b },
  1861. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000c0a23 },
  1862. };
  1863. static void rt61pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  1864. {
  1865. struct hw_mode_spec *spec = &rt2x00dev->spec;
  1866. u8 *txpower;
  1867. unsigned int i;
  1868. /*
  1869. * Initialize all hw fields.
  1870. */
  1871. rt2x00dev->hw->flags =
  1872. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  1873. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  1874. rt2x00dev->hw->extra_tx_headroom = 0;
  1875. rt2x00dev->hw->max_signal = MAX_SIGNAL;
  1876. rt2x00dev->hw->max_rssi = MAX_RX_SSI;
  1877. rt2x00dev->hw->queues = 5;
  1878. SET_IEEE80211_DEV(rt2x00dev->hw, &rt2x00dev_pci(rt2x00dev)->dev);
  1879. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  1880. rt2x00_eeprom_addr(rt2x00dev,
  1881. EEPROM_MAC_ADDR_0));
  1882. /*
  1883. * Convert tx_power array in eeprom.
  1884. */
  1885. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  1886. for (i = 0; i < 14; i++)
  1887. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1888. /*
  1889. * Initialize hw_mode information.
  1890. */
  1891. spec->num_modes = 2;
  1892. spec->num_rates = 12;
  1893. spec->tx_power_a = NULL;
  1894. spec->tx_power_bg = txpower;
  1895. spec->tx_power_default = DEFAULT_TXPOWER;
  1896. if (!test_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags)) {
  1897. spec->num_channels = 14;
  1898. spec->channels = rf_vals_noseq;
  1899. } else {
  1900. spec->num_channels = 14;
  1901. spec->channels = rf_vals_seq;
  1902. }
  1903. if (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  1904. rt2x00_rf(&rt2x00dev->chip, RF5325)) {
  1905. spec->num_modes = 3;
  1906. spec->num_channels = ARRAY_SIZE(rf_vals_seq);
  1907. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  1908. for (i = 0; i < 14; i++)
  1909. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1910. spec->tx_power_a = txpower;
  1911. }
  1912. }
  1913. static int rt61pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  1914. {
  1915. int retval;
  1916. /*
  1917. * Allocate eeprom data.
  1918. */
  1919. retval = rt61pci_validate_eeprom(rt2x00dev);
  1920. if (retval)
  1921. return retval;
  1922. retval = rt61pci_init_eeprom(rt2x00dev);
  1923. if (retval)
  1924. return retval;
  1925. /*
  1926. * Initialize hw specifications.
  1927. */
  1928. rt61pci_probe_hw_mode(rt2x00dev);
  1929. /*
  1930. * This device requires firmware
  1931. */
  1932. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  1933. /*
  1934. * Set the rssi offset.
  1935. */
  1936. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  1937. return 0;
  1938. }
  1939. /*
  1940. * IEEE80211 stack callback functions.
  1941. */
  1942. static void rt61pci_configure_filter(struct ieee80211_hw *hw,
  1943. unsigned int changed_flags,
  1944. unsigned int *total_flags,
  1945. int mc_count,
  1946. struct dev_addr_list *mc_list)
  1947. {
  1948. struct rt2x00_dev *rt2x00dev = hw->priv;
  1949. u32 reg;
  1950. /*
  1951. * Mask off any flags we are going to ignore from
  1952. * the total_flags field.
  1953. */
  1954. *total_flags &=
  1955. FIF_ALLMULTI |
  1956. FIF_FCSFAIL |
  1957. FIF_PLCPFAIL |
  1958. FIF_CONTROL |
  1959. FIF_OTHER_BSS |
  1960. FIF_PROMISC_IN_BSS;
  1961. /*
  1962. * Apply some rules to the filters:
  1963. * - Some filters imply different filters to be set.
  1964. * - Some things we can't filter out at all.
  1965. */
  1966. if (mc_count)
  1967. *total_flags |= FIF_ALLMULTI;
  1968. if (*total_flags & FIF_OTHER_BSS ||
  1969. *total_flags & FIF_PROMISC_IN_BSS)
  1970. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  1971. /*
  1972. * Check if there is any work left for us.
  1973. */
  1974. if (rt2x00dev->packet_filter == *total_flags)
  1975. return;
  1976. rt2x00dev->packet_filter = *total_flags;
  1977. /*
  1978. * Start configuration steps.
  1979. * Note that the version error will always be dropped
  1980. * and broadcast frames will always be accepted since
  1981. * there is no filter for it at this time.
  1982. */
  1983. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1984. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CRC,
  1985. !(*total_flags & FIF_FCSFAIL));
  1986. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_PHYSICAL,
  1987. !(*total_flags & FIF_PLCPFAIL));
  1988. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CONTROL,
  1989. !(*total_flags & FIF_CONTROL));
  1990. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_NOT_TO_ME,
  1991. !(*total_flags & FIF_PROMISC_IN_BSS));
  1992. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_TO_DS,
  1993. !(*total_flags & FIF_PROMISC_IN_BSS));
  1994. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_VERSION_ERROR, 1);
  1995. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_MULTICAST,
  1996. !(*total_flags & FIF_ALLMULTI));
  1997. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_BORADCAST, 0);
  1998. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_ACK_CTS, 1);
  1999. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  2000. }
  2001. static int rt61pci_set_retry_limit(struct ieee80211_hw *hw,
  2002. u32 short_retry, u32 long_retry)
  2003. {
  2004. struct rt2x00_dev *rt2x00dev = hw->priv;
  2005. u32 reg;
  2006. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  2007. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT, long_retry);
  2008. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT, short_retry);
  2009. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  2010. return 0;
  2011. }
  2012. static u64 rt61pci_get_tsf(struct ieee80211_hw *hw)
  2013. {
  2014. struct rt2x00_dev *rt2x00dev = hw->priv;
  2015. u64 tsf;
  2016. u32 reg;
  2017. rt2x00pci_register_read(rt2x00dev, TXRX_CSR13, &reg);
  2018. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  2019. rt2x00pci_register_read(rt2x00dev, TXRX_CSR12, &reg);
  2020. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  2021. return tsf;
  2022. }
  2023. static void rt61pci_reset_tsf(struct ieee80211_hw *hw)
  2024. {
  2025. struct rt2x00_dev *rt2x00dev = hw->priv;
  2026. rt2x00pci_register_write(rt2x00dev, TXRX_CSR12, 0);
  2027. rt2x00pci_register_write(rt2x00dev, TXRX_CSR13, 0);
  2028. }
  2029. static int rt61pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  2030. struct ieee80211_tx_control *control)
  2031. {
  2032. struct rt2x00_dev *rt2x00dev = hw->priv;
  2033. struct skb_desc *desc;
  2034. struct data_ring *ring;
  2035. struct data_entry *entry;
  2036. /*
  2037. * Just in case the ieee80211 doesn't set this,
  2038. * but we need this queue set for the descriptor
  2039. * initialization.
  2040. */
  2041. control->queue = IEEE80211_TX_QUEUE_BEACON;
  2042. ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
  2043. entry = rt2x00_get_data_entry(ring);
  2044. /*
  2045. * We need to append the descriptor in front of the
  2046. * beacon frame.
  2047. */
  2048. if (skb_headroom(skb) < TXD_DESC_SIZE) {
  2049. if (pskb_expand_head(skb, TXD_DESC_SIZE, 0, GFP_ATOMIC)) {
  2050. dev_kfree_skb(skb);
  2051. return -ENOMEM;
  2052. }
  2053. }
  2054. /*
  2055. * Add the descriptor in front of the skb.
  2056. */
  2057. skb_push(skb, ring->desc_size);
  2058. memset(skb->data, 0, ring->desc_size);
  2059. /*
  2060. * Fill in skb descriptor
  2061. */
  2062. desc = get_skb_desc(skb);
  2063. desc->desc_len = ring->desc_size;
  2064. desc->data_len = skb->len - ring->desc_size;
  2065. desc->desc = skb->data;
  2066. desc->data = skb->data + ring->desc_size;
  2067. desc->ring = ring;
  2068. desc->entry = entry;
  2069. rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
  2070. /*
  2071. * Write entire beacon with descriptor to register,
  2072. * and kick the beacon generator.
  2073. */
  2074. rt2x00pci_register_multiwrite(rt2x00dev, HW_BEACON_BASE0,
  2075. skb->data, skb->len);
  2076. rt61pci_kick_tx_queue(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  2077. return 0;
  2078. }
  2079. static const struct ieee80211_ops rt61pci_mac80211_ops = {
  2080. .tx = rt2x00mac_tx,
  2081. .start = rt2x00mac_start,
  2082. .stop = rt2x00mac_stop,
  2083. .add_interface = rt2x00mac_add_interface,
  2084. .remove_interface = rt2x00mac_remove_interface,
  2085. .config = rt2x00mac_config,
  2086. .config_interface = rt2x00mac_config_interface,
  2087. .configure_filter = rt61pci_configure_filter,
  2088. .get_stats = rt2x00mac_get_stats,
  2089. .set_retry_limit = rt61pci_set_retry_limit,
  2090. .bss_info_changed = rt2x00mac_bss_info_changed,
  2091. .conf_tx = rt2x00mac_conf_tx,
  2092. .get_tx_stats = rt2x00mac_get_tx_stats,
  2093. .get_tsf = rt61pci_get_tsf,
  2094. .reset_tsf = rt61pci_reset_tsf,
  2095. .beacon_update = rt61pci_beacon_update,
  2096. };
  2097. static const struct rt2x00lib_ops rt61pci_rt2x00_ops = {
  2098. .irq_handler = rt61pci_interrupt,
  2099. .probe_hw = rt61pci_probe_hw,
  2100. .get_firmware_name = rt61pci_get_firmware_name,
  2101. .load_firmware = rt61pci_load_firmware,
  2102. .initialize = rt2x00pci_initialize,
  2103. .uninitialize = rt2x00pci_uninitialize,
  2104. .init_rxentry = rt61pci_init_rxentry,
  2105. .init_txentry = rt61pci_init_txentry,
  2106. .set_device_state = rt61pci_set_device_state,
  2107. .rfkill_poll = rt61pci_rfkill_poll,
  2108. .link_stats = rt61pci_link_stats,
  2109. .reset_tuner = rt61pci_reset_tuner,
  2110. .link_tuner = rt61pci_link_tuner,
  2111. .write_tx_desc = rt61pci_write_tx_desc,
  2112. .write_tx_data = rt2x00pci_write_tx_data,
  2113. .kick_tx_queue = rt61pci_kick_tx_queue,
  2114. .fill_rxdone = rt61pci_fill_rxdone,
  2115. .config_mac_addr = rt61pci_config_mac_addr,
  2116. .config_bssid = rt61pci_config_bssid,
  2117. .config_type = rt61pci_config_type,
  2118. .config_preamble = rt61pci_config_preamble,
  2119. .config = rt61pci_config,
  2120. };
  2121. static const struct rt2x00_ops rt61pci_ops = {
  2122. .name = KBUILD_MODNAME,
  2123. .rxd_size = RXD_DESC_SIZE,
  2124. .txd_size = TXD_DESC_SIZE,
  2125. .eeprom_size = EEPROM_SIZE,
  2126. .rf_size = RF_SIZE,
  2127. .lib = &rt61pci_rt2x00_ops,
  2128. .hw = &rt61pci_mac80211_ops,
  2129. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2130. .debugfs = &rt61pci_rt2x00debug,
  2131. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2132. };
  2133. /*
  2134. * RT61pci module information.
  2135. */
  2136. static struct pci_device_id rt61pci_device_table[] = {
  2137. /* RT2561s */
  2138. { PCI_DEVICE(0x1814, 0x0301), PCI_DEVICE_DATA(&rt61pci_ops) },
  2139. /* RT2561 v2 */
  2140. { PCI_DEVICE(0x1814, 0x0302), PCI_DEVICE_DATA(&rt61pci_ops) },
  2141. /* RT2661 */
  2142. { PCI_DEVICE(0x1814, 0x0401), PCI_DEVICE_DATA(&rt61pci_ops) },
  2143. { 0, }
  2144. };
  2145. MODULE_AUTHOR(DRV_PROJECT);
  2146. MODULE_VERSION(DRV_VERSION);
  2147. MODULE_DESCRIPTION("Ralink RT61 PCI & PCMCIA Wireless LAN driver.");
  2148. MODULE_SUPPORTED_DEVICE("Ralink RT2561, RT2561s & RT2661 "
  2149. "PCI & PCMCIA chipset based cards");
  2150. MODULE_DEVICE_TABLE(pci, rt61pci_device_table);
  2151. MODULE_FIRMWARE(FIRMWARE_RT2561);
  2152. MODULE_FIRMWARE(FIRMWARE_RT2561s);
  2153. MODULE_FIRMWARE(FIRMWARE_RT2661);
  2154. MODULE_LICENSE("GPL");
  2155. static struct pci_driver rt61pci_driver = {
  2156. .name = KBUILD_MODNAME,
  2157. .id_table = rt61pci_device_table,
  2158. .probe = rt2x00pci_probe,
  2159. .remove = __devexit_p(rt2x00pci_remove),
  2160. .suspend = rt2x00pci_suspend,
  2161. .resume = rt2x00pci_resume,
  2162. };
  2163. static int __init rt61pci_init(void)
  2164. {
  2165. return pci_register_driver(&rt61pci_driver);
  2166. }
  2167. static void __exit rt61pci_exit(void)
  2168. {
  2169. pci_unregister_driver(&rt61pci_driver);
  2170. }
  2171. module_init(rt61pci_init);
  2172. module_exit(rt61pci_exit);