rt61pci.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  1. /*
  2. Copyright (C) 2004 - 2008 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_intf(struct rt2x00_dev *rt2x00dev,
  232. struct rt2x00_intf *intf,
  233. struct rt2x00intf_conf *conf,
  234. const unsigned int flags)
  235. {
  236. unsigned int beacon_base;
  237. u32 reg;
  238. if (flags & CONFIG_UPDATE_TYPE) {
  239. /*
  240. * Clear current synchronisation setup.
  241. * For the Beacon base registers we only need to clear
  242. * the first byte since that byte contains the VALID and OWNER
  243. * bits which (when set to 0) will invalidate the entire beacon.
  244. */
  245. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  246. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, 0);
  247. rt2x00pci_register_write(rt2x00dev, beacon_base, 0);
  248. /*
  249. * Enable synchronisation.
  250. */
  251. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  252. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_TICKING, 1);
  253. rt2x00_set_field32(&reg, TXRX_CSR9_TBTT_ENABLE,
  254. (conf->sync == TSF_SYNC_BEACON));
  255. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 0);
  256. rt2x00_set_field32(&reg, TXRX_CSR9_TSF_SYNC, conf->sync);
  257. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  258. }
  259. if (flags & CONFIG_UPDATE_MAC) {
  260. reg = le32_to_cpu(conf->mac[1]);
  261. rt2x00_set_field32(&reg, MAC_CSR3_UNICAST_TO_ME_MASK, 0xff);
  262. conf->mac[1] = cpu_to_le32(reg);
  263. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR2,
  264. conf->mac, sizeof(conf->mac));
  265. }
  266. if (flags & CONFIG_UPDATE_BSSID) {
  267. reg = le32_to_cpu(conf->bssid[1]);
  268. rt2x00_set_field32(&reg, MAC_CSR5_BSS_ID_MASK, 3);
  269. conf->bssid[1] = cpu_to_le32(reg);
  270. rt2x00pci_register_multiwrite(rt2x00dev, MAC_CSR4,
  271. conf->bssid, sizeof(conf->bssid));
  272. }
  273. }
  274. static int rt61pci_config_preamble(struct rt2x00_dev *rt2x00dev,
  275. const int short_preamble,
  276. const int ack_timeout,
  277. const int ack_consume_time)
  278. {
  279. u32 reg;
  280. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  281. rt2x00_set_field32(&reg, TXRX_CSR0_RX_ACK_TIMEOUT, ack_timeout);
  282. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  283. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  284. rt2x00_set_field32(&reg, TXRX_CSR4_AUTORESPOND_PREAMBLE,
  285. !!short_preamble);
  286. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  287. return 0;
  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. struct rt2x00lib_conf *libconf,
  565. const unsigned int flags)
  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. * If we are not associated, we should go straight to the
  691. * dynamic CCA tuning.
  692. */
  693. if (!rt2x00dev->intf_associated)
  694. goto dynamic_cca_tune;
  695. /*
  696. * Special big-R17 for very short distance
  697. */
  698. if (rssi >= -35) {
  699. if (r17 != 0x60)
  700. rt61pci_bbp_write(rt2x00dev, 17, 0x60);
  701. return;
  702. }
  703. /*
  704. * Special big-R17 for short distance
  705. */
  706. if (rssi >= -58) {
  707. if (r17 != up_bound)
  708. rt61pci_bbp_write(rt2x00dev, 17, up_bound);
  709. return;
  710. }
  711. /*
  712. * Special big-R17 for middle-short distance
  713. */
  714. if (rssi >= -66) {
  715. low_bound += 0x10;
  716. if (r17 != low_bound)
  717. rt61pci_bbp_write(rt2x00dev, 17, low_bound);
  718. return;
  719. }
  720. /*
  721. * Special mid-R17 for middle distance
  722. */
  723. if (rssi >= -74) {
  724. low_bound += 0x08;
  725. if (r17 != low_bound)
  726. rt61pci_bbp_write(rt2x00dev, 17, low_bound);
  727. return;
  728. }
  729. /*
  730. * Special case: Change up_bound based on the rssi.
  731. * Lower up_bound when rssi is weaker then -74 dBm.
  732. */
  733. up_bound -= 2 * (-74 - rssi);
  734. if (low_bound > up_bound)
  735. up_bound = low_bound;
  736. if (r17 > up_bound) {
  737. rt61pci_bbp_write(rt2x00dev, 17, up_bound);
  738. return;
  739. }
  740. dynamic_cca_tune:
  741. /*
  742. * r17 does not yet exceed upper limit, continue and base
  743. * the r17 tuning on the false CCA count.
  744. */
  745. if (rt2x00dev->link.qual.false_cca > 512 && r17 < up_bound) {
  746. if (++r17 > up_bound)
  747. r17 = up_bound;
  748. rt61pci_bbp_write(rt2x00dev, 17, r17);
  749. } else if (rt2x00dev->link.qual.false_cca < 100 && r17 > low_bound) {
  750. if (--r17 < low_bound)
  751. r17 = low_bound;
  752. rt61pci_bbp_write(rt2x00dev, 17, r17);
  753. }
  754. }
  755. /*
  756. * Firmware name function.
  757. */
  758. static char *rt61pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  759. {
  760. char *fw_name;
  761. switch (rt2x00dev->chip.rt) {
  762. case RT2561:
  763. fw_name = FIRMWARE_RT2561;
  764. break;
  765. case RT2561s:
  766. fw_name = FIRMWARE_RT2561s;
  767. break;
  768. case RT2661:
  769. fw_name = FIRMWARE_RT2661;
  770. break;
  771. default:
  772. fw_name = NULL;
  773. break;
  774. }
  775. return fw_name;
  776. }
  777. /*
  778. * Initialization functions.
  779. */
  780. static int rt61pci_load_firmware(struct rt2x00_dev *rt2x00dev, void *data,
  781. const size_t len)
  782. {
  783. int i;
  784. u32 reg;
  785. /*
  786. * Wait for stable hardware.
  787. */
  788. for (i = 0; i < 100; i++) {
  789. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  790. if (reg)
  791. break;
  792. msleep(1);
  793. }
  794. if (!reg) {
  795. ERROR(rt2x00dev, "Unstable hardware.\n");
  796. return -EBUSY;
  797. }
  798. /*
  799. * Prepare MCU and mailbox for firmware loading.
  800. */
  801. reg = 0;
  802. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  803. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  804. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  805. rt2x00pci_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  806. rt2x00pci_register_write(rt2x00dev, HOST_CMD_CSR, 0);
  807. /*
  808. * Write firmware to device.
  809. */
  810. reg = 0;
  811. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 1);
  812. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 1);
  813. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  814. rt2x00pci_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  815. data, len);
  816. rt2x00_set_field32(&reg, MCU_CNTL_CSR_SELECT_BANK, 0);
  817. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  818. rt2x00_set_field32(&reg, MCU_CNTL_CSR_RESET, 0);
  819. rt2x00pci_register_write(rt2x00dev, MCU_CNTL_CSR, reg);
  820. for (i = 0; i < 100; i++) {
  821. rt2x00pci_register_read(rt2x00dev, MCU_CNTL_CSR, &reg);
  822. if (rt2x00_get_field32(reg, MCU_CNTL_CSR_READY))
  823. break;
  824. msleep(1);
  825. }
  826. if (i == 100) {
  827. ERROR(rt2x00dev, "MCU Control register not ready.\n");
  828. return -EBUSY;
  829. }
  830. /*
  831. * Reset MAC and BBP registers.
  832. */
  833. reg = 0;
  834. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  835. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  836. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  837. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  838. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  839. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  840. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  841. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  842. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  843. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  844. return 0;
  845. }
  846. static void rt61pci_init_rxentry(struct rt2x00_dev *rt2x00dev,
  847. struct queue_entry *entry)
  848. {
  849. struct queue_entry_priv_pci_rx *priv_rx = entry->priv_data;
  850. u32 word;
  851. rt2x00_desc_read(priv_rx->desc, 5, &word);
  852. rt2x00_set_field32(&word, RXD_W5_BUFFER_PHYSICAL_ADDRESS, priv_rx->dma);
  853. rt2x00_desc_write(priv_rx->desc, 5, word);
  854. rt2x00_desc_read(priv_rx->desc, 0, &word);
  855. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  856. rt2x00_desc_write(priv_rx->desc, 0, word);
  857. }
  858. static void rt61pci_init_txentry(struct rt2x00_dev *rt2x00dev,
  859. struct queue_entry *entry)
  860. {
  861. struct queue_entry_priv_pci_tx *priv_tx = entry->priv_data;
  862. u32 word;
  863. rt2x00_desc_read(priv_tx->desc, 1, &word);
  864. rt2x00_set_field32(&word, TXD_W1_BUFFER_COUNT, 1);
  865. rt2x00_desc_write(priv_tx->desc, 1, word);
  866. rt2x00_desc_read(priv_tx->desc, 5, &word);
  867. rt2x00_set_field32(&word, TXD_W5_PID_TYPE, entry->queue->qid);
  868. rt2x00_set_field32(&word, TXD_W5_PID_SUBTYPE, entry->entry_idx);
  869. rt2x00_desc_write(priv_tx->desc, 5, word);
  870. rt2x00_desc_read(priv_tx->desc, 6, &word);
  871. rt2x00_set_field32(&word, TXD_W6_BUFFER_PHYSICAL_ADDRESS, priv_tx->dma);
  872. rt2x00_desc_write(priv_tx->desc, 6, word);
  873. rt2x00_desc_read(priv_tx->desc, 0, &word);
  874. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  875. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  876. rt2x00_desc_write(priv_tx->desc, 0, word);
  877. }
  878. static int rt61pci_init_queues(struct rt2x00_dev *rt2x00dev)
  879. {
  880. struct queue_entry_priv_pci_rx *priv_rx;
  881. struct queue_entry_priv_pci_tx *priv_tx;
  882. u32 reg;
  883. /*
  884. * Initialize registers.
  885. */
  886. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR0, &reg);
  887. rt2x00_set_field32(&reg, TX_RING_CSR0_AC0_RING_SIZE,
  888. rt2x00dev->tx[0].limit);
  889. rt2x00_set_field32(&reg, TX_RING_CSR0_AC1_RING_SIZE,
  890. rt2x00dev->tx[1].limit);
  891. rt2x00_set_field32(&reg, TX_RING_CSR0_AC2_RING_SIZE,
  892. rt2x00dev->tx[2].limit);
  893. rt2x00_set_field32(&reg, TX_RING_CSR0_AC3_RING_SIZE,
  894. rt2x00dev->tx[3].limit);
  895. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR0, reg);
  896. rt2x00pci_register_read(rt2x00dev, TX_RING_CSR1, &reg);
  897. rt2x00_set_field32(&reg, TX_RING_CSR1_TXD_SIZE,
  898. rt2x00dev->tx[0].desc_size / 4);
  899. rt2x00pci_register_write(rt2x00dev, TX_RING_CSR1, reg);
  900. priv_tx = rt2x00dev->tx[0].entries[0].priv_data;
  901. rt2x00pci_register_read(rt2x00dev, AC0_BASE_CSR, &reg);
  902. rt2x00_set_field32(&reg, AC0_BASE_CSR_RING_REGISTER, priv_tx->dma);
  903. rt2x00pci_register_write(rt2x00dev, AC0_BASE_CSR, reg);
  904. priv_tx = rt2x00dev->tx[1].entries[0].priv_data;
  905. rt2x00pci_register_read(rt2x00dev, AC1_BASE_CSR, &reg);
  906. rt2x00_set_field32(&reg, AC1_BASE_CSR_RING_REGISTER, priv_tx->dma);
  907. rt2x00pci_register_write(rt2x00dev, AC1_BASE_CSR, reg);
  908. priv_tx = rt2x00dev->tx[2].entries[0].priv_data;
  909. rt2x00pci_register_read(rt2x00dev, AC2_BASE_CSR, &reg);
  910. rt2x00_set_field32(&reg, AC2_BASE_CSR_RING_REGISTER, priv_tx->dma);
  911. rt2x00pci_register_write(rt2x00dev, AC2_BASE_CSR, reg);
  912. priv_tx = rt2x00dev->tx[3].entries[0].priv_data;
  913. rt2x00pci_register_read(rt2x00dev, AC3_BASE_CSR, &reg);
  914. rt2x00_set_field32(&reg, AC3_BASE_CSR_RING_REGISTER, priv_tx->dma);
  915. rt2x00pci_register_write(rt2x00dev, AC3_BASE_CSR, reg);
  916. rt2x00pci_register_read(rt2x00dev, RX_RING_CSR, &reg);
  917. rt2x00_set_field32(&reg, RX_RING_CSR_RING_SIZE, rt2x00dev->rx->limit);
  918. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_SIZE,
  919. rt2x00dev->rx->desc_size / 4);
  920. rt2x00_set_field32(&reg, RX_RING_CSR_RXD_WRITEBACK_SIZE, 4);
  921. rt2x00pci_register_write(rt2x00dev, RX_RING_CSR, reg);
  922. priv_rx = rt2x00dev->rx->entries[0].priv_data;
  923. rt2x00pci_register_read(rt2x00dev, RX_BASE_CSR, &reg);
  924. rt2x00_set_field32(&reg, RX_BASE_CSR_RING_REGISTER, priv_rx->dma);
  925. rt2x00pci_register_write(rt2x00dev, RX_BASE_CSR, reg);
  926. rt2x00pci_register_read(rt2x00dev, TX_DMA_DST_CSR, &reg);
  927. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC0, 2);
  928. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC1, 2);
  929. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC2, 2);
  930. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_AC3, 2);
  931. rt2x00_set_field32(&reg, TX_DMA_DST_CSR_DEST_MGMT, 0);
  932. rt2x00pci_register_write(rt2x00dev, TX_DMA_DST_CSR, reg);
  933. rt2x00pci_register_read(rt2x00dev, LOAD_TX_RING_CSR, &reg);
  934. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC0, 1);
  935. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC1, 1);
  936. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC2, 1);
  937. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_AC3, 1);
  938. rt2x00_set_field32(&reg, LOAD_TX_RING_CSR_LOAD_TXD_MGMT, 0);
  939. rt2x00pci_register_write(rt2x00dev, LOAD_TX_RING_CSR, reg);
  940. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  941. rt2x00_set_field32(&reg, RX_CNTL_CSR_LOAD_RXD, 1);
  942. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  943. return 0;
  944. }
  945. static int rt61pci_init_registers(struct rt2x00_dev *rt2x00dev)
  946. {
  947. u32 reg;
  948. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  949. rt2x00_set_field32(&reg, TXRX_CSR0_AUTO_TX_SEQ, 1);
  950. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX, 0);
  951. rt2x00_set_field32(&reg, TXRX_CSR0_TX_WITHOUT_WAITING, 0);
  952. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  953. rt2x00pci_register_read(rt2x00dev, TXRX_CSR1, &reg);
  954. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0, 47); /* CCK Signal */
  955. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID0_VALID, 1);
  956. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1, 30); /* Rssi */
  957. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID1_VALID, 1);
  958. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2, 42); /* OFDM Rate */
  959. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID2_VALID, 1);
  960. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3, 30); /* Rssi */
  961. rt2x00_set_field32(&reg, TXRX_CSR1_BBP_ID3_VALID, 1);
  962. rt2x00pci_register_write(rt2x00dev, TXRX_CSR1, reg);
  963. /*
  964. * CCK TXD BBP registers
  965. */
  966. rt2x00pci_register_read(rt2x00dev, TXRX_CSR2, &reg);
  967. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0, 13);
  968. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID0_VALID, 1);
  969. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1, 12);
  970. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID1_VALID, 1);
  971. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2, 11);
  972. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID2_VALID, 1);
  973. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3, 10);
  974. rt2x00_set_field32(&reg, TXRX_CSR2_BBP_ID3_VALID, 1);
  975. rt2x00pci_register_write(rt2x00dev, TXRX_CSR2, reg);
  976. /*
  977. * OFDM TXD BBP registers
  978. */
  979. rt2x00pci_register_read(rt2x00dev, TXRX_CSR3, &reg);
  980. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0, 7);
  981. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID0_VALID, 1);
  982. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1, 6);
  983. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID1_VALID, 1);
  984. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2, 5);
  985. rt2x00_set_field32(&reg, TXRX_CSR3_BBP_ID2_VALID, 1);
  986. rt2x00pci_register_write(rt2x00dev, TXRX_CSR3, reg);
  987. rt2x00pci_register_read(rt2x00dev, TXRX_CSR7, &reg);
  988. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_6MBS, 59);
  989. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_9MBS, 53);
  990. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_12MBS, 49);
  991. rt2x00_set_field32(&reg, TXRX_CSR7_ACK_CTS_18MBS, 46);
  992. rt2x00pci_register_write(rt2x00dev, TXRX_CSR7, reg);
  993. rt2x00pci_register_read(rt2x00dev, TXRX_CSR8, &reg);
  994. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_24MBS, 44);
  995. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_36MBS, 42);
  996. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_48MBS, 42);
  997. rt2x00_set_field32(&reg, TXRX_CSR8_ACK_CTS_54MBS, 42);
  998. rt2x00pci_register_write(rt2x00dev, TXRX_CSR8, reg);
  999. rt2x00pci_register_write(rt2x00dev, TXRX_CSR15, 0x0000000f);
  1000. rt2x00pci_register_write(rt2x00dev, MAC_CSR6, 0x00000fff);
  1001. rt2x00pci_register_read(rt2x00dev, MAC_CSR9, &reg);
  1002. rt2x00_set_field32(&reg, MAC_CSR9_CW_SELECT, 0);
  1003. rt2x00pci_register_write(rt2x00dev, MAC_CSR9, reg);
  1004. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x0000071c);
  1005. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  1006. return -EBUSY;
  1007. rt2x00pci_register_write(rt2x00dev, MAC_CSR13, 0x0000e000);
  1008. /*
  1009. * Invalidate all Shared Keys (SEC_CSR0),
  1010. * and clear the Shared key Cipher algorithms (SEC_CSR1 & SEC_CSR5)
  1011. */
  1012. rt2x00pci_register_write(rt2x00dev, SEC_CSR0, 0x00000000);
  1013. rt2x00pci_register_write(rt2x00dev, SEC_CSR1, 0x00000000);
  1014. rt2x00pci_register_write(rt2x00dev, SEC_CSR5, 0x00000000);
  1015. rt2x00pci_register_write(rt2x00dev, PHY_CSR1, 0x000023b0);
  1016. rt2x00pci_register_write(rt2x00dev, PHY_CSR5, 0x060a100c);
  1017. rt2x00pci_register_write(rt2x00dev, PHY_CSR6, 0x00080606);
  1018. rt2x00pci_register_write(rt2x00dev, PHY_CSR7, 0x00000a08);
  1019. rt2x00pci_register_write(rt2x00dev, PCI_CFG_CSR, 0x28ca4404);
  1020. rt2x00pci_register_write(rt2x00dev, TEST_MODE_CSR, 0x00000200);
  1021. rt2x00pci_register_write(rt2x00dev, M2H_CMD_DONE_CSR, 0xffffffff);
  1022. rt2x00pci_register_read(rt2x00dev, AC_TXOP_CSR0, &reg);
  1023. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC0_TX_OP, 0);
  1024. rt2x00_set_field32(&reg, AC_TXOP_CSR0_AC1_TX_OP, 0);
  1025. rt2x00pci_register_write(rt2x00dev, AC_TXOP_CSR0, reg);
  1026. rt2x00pci_register_read(rt2x00dev, AC_TXOP_CSR1, &reg);
  1027. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC2_TX_OP, 192);
  1028. rt2x00_set_field32(&reg, AC_TXOP_CSR1_AC3_TX_OP, 48);
  1029. rt2x00pci_register_write(rt2x00dev, AC_TXOP_CSR1, reg);
  1030. /*
  1031. * Clear all beacons
  1032. * For the Beacon base registers we only need to clear
  1033. * the first byte since that byte contains the VALID and OWNER
  1034. * bits which (when set to 0) will invalidate the entire beacon.
  1035. */
  1036. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1037. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1038. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1039. rt2x00pci_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1040. /*
  1041. * We must clear the error counters.
  1042. * These registers are cleared on read,
  1043. * so we may pass a useless variable to store the value.
  1044. */
  1045. rt2x00pci_register_read(rt2x00dev, STA_CSR0, &reg);
  1046. rt2x00pci_register_read(rt2x00dev, STA_CSR1, &reg);
  1047. rt2x00pci_register_read(rt2x00dev, STA_CSR2, &reg);
  1048. /*
  1049. * Reset MAC and BBP registers.
  1050. */
  1051. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1052. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 1);
  1053. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 1);
  1054. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1055. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1056. rt2x00_set_field32(&reg, MAC_CSR1_SOFT_RESET, 0);
  1057. rt2x00_set_field32(&reg, MAC_CSR1_BBP_RESET, 0);
  1058. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1059. rt2x00pci_register_read(rt2x00dev, MAC_CSR1, &reg);
  1060. rt2x00_set_field32(&reg, MAC_CSR1_HOST_READY, 1);
  1061. rt2x00pci_register_write(rt2x00dev, MAC_CSR1, reg);
  1062. return 0;
  1063. }
  1064. static int rt61pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1065. {
  1066. unsigned int i;
  1067. u16 eeprom;
  1068. u8 reg_id;
  1069. u8 value;
  1070. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1071. rt61pci_bbp_read(rt2x00dev, 0, &value);
  1072. if ((value != 0xff) && (value != 0x00))
  1073. goto continue_csr_init;
  1074. NOTICE(rt2x00dev, "Waiting for BBP register.\n");
  1075. udelay(REGISTER_BUSY_DELAY);
  1076. }
  1077. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1078. return -EACCES;
  1079. continue_csr_init:
  1080. rt61pci_bbp_write(rt2x00dev, 3, 0x00);
  1081. rt61pci_bbp_write(rt2x00dev, 15, 0x30);
  1082. rt61pci_bbp_write(rt2x00dev, 21, 0xc8);
  1083. rt61pci_bbp_write(rt2x00dev, 22, 0x38);
  1084. rt61pci_bbp_write(rt2x00dev, 23, 0x06);
  1085. rt61pci_bbp_write(rt2x00dev, 24, 0xfe);
  1086. rt61pci_bbp_write(rt2x00dev, 25, 0x0a);
  1087. rt61pci_bbp_write(rt2x00dev, 26, 0x0d);
  1088. rt61pci_bbp_write(rt2x00dev, 34, 0x12);
  1089. rt61pci_bbp_write(rt2x00dev, 37, 0x07);
  1090. rt61pci_bbp_write(rt2x00dev, 39, 0xf8);
  1091. rt61pci_bbp_write(rt2x00dev, 41, 0x60);
  1092. rt61pci_bbp_write(rt2x00dev, 53, 0x10);
  1093. rt61pci_bbp_write(rt2x00dev, 54, 0x18);
  1094. rt61pci_bbp_write(rt2x00dev, 60, 0x10);
  1095. rt61pci_bbp_write(rt2x00dev, 61, 0x04);
  1096. rt61pci_bbp_write(rt2x00dev, 62, 0x04);
  1097. rt61pci_bbp_write(rt2x00dev, 75, 0xfe);
  1098. rt61pci_bbp_write(rt2x00dev, 86, 0xfe);
  1099. rt61pci_bbp_write(rt2x00dev, 88, 0xfe);
  1100. rt61pci_bbp_write(rt2x00dev, 90, 0x0f);
  1101. rt61pci_bbp_write(rt2x00dev, 99, 0x00);
  1102. rt61pci_bbp_write(rt2x00dev, 102, 0x16);
  1103. rt61pci_bbp_write(rt2x00dev, 107, 0x04);
  1104. DEBUG(rt2x00dev, "Start initialization from EEPROM...\n");
  1105. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1106. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1107. if (eeprom != 0xffff && eeprom != 0x0000) {
  1108. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1109. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1110. DEBUG(rt2x00dev, "BBP: 0x%02x, value: 0x%02x.\n",
  1111. reg_id, value);
  1112. rt61pci_bbp_write(rt2x00dev, reg_id, value);
  1113. }
  1114. }
  1115. DEBUG(rt2x00dev, "...End initialization from EEPROM.\n");
  1116. return 0;
  1117. }
  1118. /*
  1119. * Device state switch handlers.
  1120. */
  1121. static void rt61pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  1122. enum dev_state state)
  1123. {
  1124. u32 reg;
  1125. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  1126. rt2x00_set_field32(&reg, TXRX_CSR0_DISABLE_RX,
  1127. state == STATE_RADIO_RX_OFF);
  1128. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  1129. }
  1130. static void rt61pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1131. enum dev_state state)
  1132. {
  1133. int mask = (state == STATE_RADIO_IRQ_OFF);
  1134. u32 reg;
  1135. /*
  1136. * When interrupts are being enabled, the interrupt registers
  1137. * should clear the register to assure a clean state.
  1138. */
  1139. if (state == STATE_RADIO_IRQ_ON) {
  1140. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1141. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1142. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg);
  1143. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg);
  1144. }
  1145. /*
  1146. * Only toggle the interrupts bits we are going to use.
  1147. * Non-checked interrupt bits are disabled by default.
  1148. */
  1149. rt2x00pci_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1150. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDONE, mask);
  1151. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDONE, mask);
  1152. rt2x00_set_field32(&reg, INT_MASK_CSR_ENABLE_MITIGATION, mask);
  1153. rt2x00_set_field32(&reg, INT_MASK_CSR_MITIGATION_PERIOD, 0xff);
  1154. rt2x00pci_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1155. rt2x00pci_register_read(rt2x00dev, MCU_INT_MASK_CSR, &reg);
  1156. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_0, mask);
  1157. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_1, mask);
  1158. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_2, mask);
  1159. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_3, mask);
  1160. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_4, mask);
  1161. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_5, mask);
  1162. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_6, mask);
  1163. rt2x00_set_field32(&reg, MCU_INT_MASK_CSR_7, mask);
  1164. rt2x00pci_register_write(rt2x00dev, MCU_INT_MASK_CSR, reg);
  1165. }
  1166. static int rt61pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1167. {
  1168. u32 reg;
  1169. /*
  1170. * Initialize all registers.
  1171. */
  1172. if (rt61pci_init_queues(rt2x00dev) ||
  1173. rt61pci_init_registers(rt2x00dev) ||
  1174. rt61pci_init_bbp(rt2x00dev)) {
  1175. ERROR(rt2x00dev, "Register initialization failed.\n");
  1176. return -EIO;
  1177. }
  1178. /*
  1179. * Enable interrupts.
  1180. */
  1181. rt61pci_toggle_irq(rt2x00dev, STATE_RADIO_IRQ_ON);
  1182. /*
  1183. * Enable RX.
  1184. */
  1185. rt2x00pci_register_read(rt2x00dev, RX_CNTL_CSR, &reg);
  1186. rt2x00_set_field32(&reg, RX_CNTL_CSR_ENABLE_RX_DMA, 1);
  1187. rt2x00pci_register_write(rt2x00dev, RX_CNTL_CSR, reg);
  1188. /*
  1189. * Enable LED
  1190. */
  1191. rt61pci_enable_led(rt2x00dev);
  1192. return 0;
  1193. }
  1194. static void rt61pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1195. {
  1196. u32 reg;
  1197. /*
  1198. * Disable LED
  1199. */
  1200. rt61pci_disable_led(rt2x00dev);
  1201. rt2x00pci_register_write(rt2x00dev, MAC_CSR10, 0x00001818);
  1202. /*
  1203. * Disable synchronisation.
  1204. */
  1205. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, 0);
  1206. /*
  1207. * Cancel RX and TX.
  1208. */
  1209. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1210. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC0, 1);
  1211. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC1, 1);
  1212. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC2, 1);
  1213. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_AC3, 1);
  1214. rt2x00_set_field32(&reg, TX_CNTL_CSR_ABORT_TX_MGMT, 1);
  1215. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1216. /*
  1217. * Disable interrupts.
  1218. */
  1219. rt61pci_toggle_irq(rt2x00dev, STATE_RADIO_IRQ_OFF);
  1220. }
  1221. static int rt61pci_set_state(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  1222. {
  1223. u32 reg;
  1224. unsigned int i;
  1225. char put_to_sleep;
  1226. char current_state;
  1227. put_to_sleep = (state != STATE_AWAKE);
  1228. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1229. rt2x00_set_field32(&reg, MAC_CSR12_FORCE_WAKEUP, !put_to_sleep);
  1230. rt2x00_set_field32(&reg, MAC_CSR12_PUT_TO_SLEEP, put_to_sleep);
  1231. rt2x00pci_register_write(rt2x00dev, MAC_CSR12, reg);
  1232. /*
  1233. * Device is not guaranteed to be in the requested state yet.
  1234. * We must wait until the register indicates that the
  1235. * device has entered the correct state.
  1236. */
  1237. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1238. rt2x00pci_register_read(rt2x00dev, MAC_CSR12, &reg);
  1239. current_state =
  1240. rt2x00_get_field32(reg, MAC_CSR12_BBP_CURRENT_STATE);
  1241. if (current_state == !put_to_sleep)
  1242. return 0;
  1243. msleep(10);
  1244. }
  1245. NOTICE(rt2x00dev, "Device failed to enter state %d, "
  1246. "current device state %d.\n", !put_to_sleep, current_state);
  1247. return -EBUSY;
  1248. }
  1249. static int rt61pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1250. enum dev_state state)
  1251. {
  1252. int retval = 0;
  1253. switch (state) {
  1254. case STATE_RADIO_ON:
  1255. retval = rt61pci_enable_radio(rt2x00dev);
  1256. break;
  1257. case STATE_RADIO_OFF:
  1258. rt61pci_disable_radio(rt2x00dev);
  1259. break;
  1260. case STATE_RADIO_RX_ON:
  1261. case STATE_RADIO_RX_ON_LINK:
  1262. rt61pci_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  1263. break;
  1264. case STATE_RADIO_RX_OFF:
  1265. case STATE_RADIO_RX_OFF_LINK:
  1266. rt61pci_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  1267. break;
  1268. case STATE_DEEP_SLEEP:
  1269. case STATE_SLEEP:
  1270. case STATE_STANDBY:
  1271. case STATE_AWAKE:
  1272. retval = rt61pci_set_state(rt2x00dev, state);
  1273. break;
  1274. default:
  1275. retval = -ENOTSUPP;
  1276. break;
  1277. }
  1278. return retval;
  1279. }
  1280. /*
  1281. * TX descriptor initialization
  1282. */
  1283. static void rt61pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  1284. struct sk_buff *skb,
  1285. struct txentry_desc *txdesc,
  1286. struct ieee80211_tx_control *control)
  1287. {
  1288. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  1289. __le32 *txd = skbdesc->desc;
  1290. u32 word;
  1291. /*
  1292. * Start writing the descriptor words.
  1293. */
  1294. rt2x00_desc_read(txd, 1, &word);
  1295. rt2x00_set_field32(&word, TXD_W1_HOST_Q_ID, txdesc->queue);
  1296. rt2x00_set_field32(&word, TXD_W1_AIFSN, txdesc->aifs);
  1297. rt2x00_set_field32(&word, TXD_W1_CWMIN, txdesc->cw_min);
  1298. rt2x00_set_field32(&word, TXD_W1_CWMAX, txdesc->cw_max);
  1299. rt2x00_set_field32(&word, TXD_W1_IV_OFFSET, IEEE80211_HEADER);
  1300. rt2x00_set_field32(&word, TXD_W1_HW_SEQUENCE, 1);
  1301. rt2x00_desc_write(txd, 1, word);
  1302. rt2x00_desc_read(txd, 2, &word);
  1303. rt2x00_set_field32(&word, TXD_W2_PLCP_SIGNAL, txdesc->signal);
  1304. rt2x00_set_field32(&word, TXD_W2_PLCP_SERVICE, txdesc->service);
  1305. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_LOW, txdesc->length_low);
  1306. rt2x00_set_field32(&word, TXD_W2_PLCP_LENGTH_HIGH, txdesc->length_high);
  1307. rt2x00_desc_write(txd, 2, word);
  1308. rt2x00_desc_read(txd, 5, &word);
  1309. rt2x00_set_field32(&word, TXD_W5_TX_POWER,
  1310. TXPOWER_TO_DEV(control->power_level));
  1311. rt2x00_set_field32(&word, TXD_W5_WAITING_DMA_DONE_INT, 1);
  1312. rt2x00_desc_write(txd, 5, word);
  1313. if (skbdesc->desc_len > TXINFO_SIZE) {
  1314. rt2x00_desc_read(txd, 11, &word);
  1315. rt2x00_set_field32(&word, TXD_W11_BUFFER_LENGTH0, skbdesc->data_len);
  1316. rt2x00_desc_write(txd, 11, word);
  1317. }
  1318. rt2x00_desc_read(txd, 0, &word);
  1319. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  1320. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  1321. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  1322. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1323. rt2x00_set_field32(&word, TXD_W0_ACK,
  1324. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1325. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  1326. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1327. rt2x00_set_field32(&word, TXD_W0_OFDM,
  1328. test_bit(ENTRY_TXD_OFDM_RATE, &txdesc->flags));
  1329. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->ifs);
  1330. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1331. !!(control->flags &
  1332. IEEE80211_TXCTL_LONG_RETRY_LIMIT));
  1333. rt2x00_set_field32(&word, TXD_W0_TKIP_MIC, 0);
  1334. rt2x00_set_field32(&word, TXD_W0_DATABYTE_COUNT, skbdesc->data_len);
  1335. rt2x00_set_field32(&word, TXD_W0_BURST,
  1336. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1337. rt2x00_set_field32(&word, TXD_W0_CIPHER_ALG, CIPHER_NONE);
  1338. rt2x00_desc_write(txd, 0, word);
  1339. }
  1340. /*
  1341. * TX data initialization
  1342. */
  1343. static void rt61pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  1344. unsigned int queue)
  1345. {
  1346. u32 reg;
  1347. if (queue == IEEE80211_TX_QUEUE_BEACON) {
  1348. /*
  1349. * For Wi-Fi faily generated beacons between participating
  1350. * stations. Set TBTT phase adaptive adjustment step to 8us.
  1351. */
  1352. rt2x00pci_register_write(rt2x00dev, TXRX_CSR10, 0x00001008);
  1353. rt2x00pci_register_read(rt2x00dev, TXRX_CSR9, &reg);
  1354. if (!rt2x00_get_field32(reg, TXRX_CSR9_BEACON_GEN)) {
  1355. rt2x00_set_field32(&reg, TXRX_CSR9_BEACON_GEN, 1);
  1356. rt2x00pci_register_write(rt2x00dev, TXRX_CSR9, reg);
  1357. }
  1358. return;
  1359. }
  1360. rt2x00pci_register_read(rt2x00dev, TX_CNTL_CSR, &reg);
  1361. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC0,
  1362. (queue == IEEE80211_TX_QUEUE_DATA0));
  1363. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC1,
  1364. (queue == IEEE80211_TX_QUEUE_DATA1));
  1365. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC2,
  1366. (queue == IEEE80211_TX_QUEUE_DATA2));
  1367. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_AC3,
  1368. (queue == IEEE80211_TX_QUEUE_DATA3));
  1369. rt2x00_set_field32(&reg, TX_CNTL_CSR_KICK_TX_MGMT,
  1370. (queue == IEEE80211_TX_QUEUE_DATA4));
  1371. rt2x00pci_register_write(rt2x00dev, TX_CNTL_CSR, reg);
  1372. }
  1373. /*
  1374. * RX control handlers
  1375. */
  1376. static int rt61pci_agc_to_rssi(struct rt2x00_dev *rt2x00dev, int rxd_w1)
  1377. {
  1378. u16 eeprom;
  1379. u8 offset;
  1380. u8 lna;
  1381. lna = rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_LNA);
  1382. switch (lna) {
  1383. case 3:
  1384. offset = 90;
  1385. break;
  1386. case 2:
  1387. offset = 74;
  1388. break;
  1389. case 1:
  1390. offset = 64;
  1391. break;
  1392. default:
  1393. return 0;
  1394. }
  1395. if (rt2x00dev->rx_status.phymode == MODE_IEEE80211A) {
  1396. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  1397. offset += 14;
  1398. if (lna == 3 || lna == 2)
  1399. offset += 10;
  1400. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &eeprom);
  1401. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_A_1);
  1402. } else {
  1403. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags))
  1404. offset += 14;
  1405. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &eeprom);
  1406. offset -= rt2x00_get_field16(eeprom, EEPROM_RSSI_OFFSET_BG_1);
  1407. }
  1408. return rt2x00_get_field32(rxd_w1, RXD_W1_RSSI_AGC) * 2 - offset;
  1409. }
  1410. static void rt61pci_fill_rxdone(struct queue_entry *entry,
  1411. struct rxdone_entry_desc *rxdesc)
  1412. {
  1413. struct queue_entry_priv_pci_rx *priv_rx = entry->priv_data;
  1414. u32 word0;
  1415. u32 word1;
  1416. rt2x00_desc_read(priv_rx->desc, 0, &word0);
  1417. rt2x00_desc_read(priv_rx->desc, 1, &word1);
  1418. rxdesc->flags = 0;
  1419. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1420. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1421. /*
  1422. * Obtain the status about this packet.
  1423. */
  1424. rxdesc->signal = rt2x00_get_field32(word1, RXD_W1_SIGNAL);
  1425. rxdesc->rssi = rt61pci_agc_to_rssi(entry->queue->rt2x00dev, word1);
  1426. rxdesc->ofdm = rt2x00_get_field32(word0, RXD_W0_OFDM);
  1427. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1428. rxdesc->my_bss = !!rt2x00_get_field32(word0, RXD_W0_MY_BSS);
  1429. }
  1430. /*
  1431. * Interrupt functions.
  1432. */
  1433. static void rt61pci_txdone(struct rt2x00_dev *rt2x00dev)
  1434. {
  1435. struct data_queue *queue;
  1436. struct queue_entry *entry;
  1437. struct queue_entry *entry_done;
  1438. struct queue_entry_priv_pci_tx *priv_tx;
  1439. struct txdone_entry_desc txdesc;
  1440. u32 word;
  1441. u32 reg;
  1442. u32 old_reg;
  1443. int type;
  1444. int index;
  1445. /*
  1446. * During each loop we will compare the freshly read
  1447. * STA_CSR4 register value with the value read from
  1448. * the previous loop. If the 2 values are equal then
  1449. * we should stop processing because the chance it
  1450. * quite big that the device has been unplugged and
  1451. * we risk going into an endless loop.
  1452. */
  1453. old_reg = 0;
  1454. while (1) {
  1455. rt2x00pci_register_read(rt2x00dev, STA_CSR4, &reg);
  1456. if (!rt2x00_get_field32(reg, STA_CSR4_VALID))
  1457. break;
  1458. if (old_reg == reg)
  1459. break;
  1460. old_reg = reg;
  1461. /*
  1462. * Skip this entry when it contains an invalid
  1463. * queue identication number.
  1464. */
  1465. type = rt2x00_get_field32(reg, STA_CSR4_PID_TYPE);
  1466. queue = rt2x00queue_get_queue(rt2x00dev, type);
  1467. if (unlikely(!queue))
  1468. continue;
  1469. /*
  1470. * Skip this entry when it contains an invalid
  1471. * index number.
  1472. */
  1473. index = rt2x00_get_field32(reg, STA_CSR4_PID_SUBTYPE);
  1474. if (unlikely(index >= queue->limit))
  1475. continue;
  1476. entry = &queue->entries[index];
  1477. priv_tx = entry->priv_data;
  1478. rt2x00_desc_read(priv_tx->desc, 0, &word);
  1479. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1480. !rt2x00_get_field32(word, TXD_W0_VALID))
  1481. return;
  1482. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1483. while (entry != entry_done) {
  1484. /* Catch up.
  1485. * Just report any entries we missed as failed.
  1486. */
  1487. WARNING(rt2x00dev,
  1488. "TX status report missed for entry %d\n",
  1489. entry_done->entry_idx);
  1490. txdesc.status = TX_FAIL_OTHER;
  1491. txdesc.retry = 0;
  1492. rt2x00pci_txdone(rt2x00dev, entry_done, &txdesc);
  1493. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1494. }
  1495. /*
  1496. * Obtain the status about this packet.
  1497. */
  1498. txdesc.status = rt2x00_get_field32(reg, STA_CSR4_TX_RESULT);
  1499. txdesc.retry = rt2x00_get_field32(reg, STA_CSR4_RETRY_COUNT);
  1500. rt2x00pci_txdone(rt2x00dev, entry, &txdesc);
  1501. }
  1502. }
  1503. static irqreturn_t rt61pci_interrupt(int irq, void *dev_instance)
  1504. {
  1505. struct rt2x00_dev *rt2x00dev = dev_instance;
  1506. u32 reg_mcu;
  1507. u32 reg;
  1508. /*
  1509. * Get the interrupt sources & saved to local variable.
  1510. * Write register value back to clear pending interrupts.
  1511. */
  1512. rt2x00pci_register_read(rt2x00dev, MCU_INT_SOURCE_CSR, &reg_mcu);
  1513. rt2x00pci_register_write(rt2x00dev, MCU_INT_SOURCE_CSR, reg_mcu);
  1514. rt2x00pci_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1515. rt2x00pci_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1516. if (!reg && !reg_mcu)
  1517. return IRQ_NONE;
  1518. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  1519. return IRQ_HANDLED;
  1520. /*
  1521. * Handle interrupts, walk through all bits
  1522. * and run the tasks, the bits are checked in order of
  1523. * priority.
  1524. */
  1525. /*
  1526. * 1 - Rx ring done interrupt.
  1527. */
  1528. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RXDONE))
  1529. rt2x00pci_rxdone(rt2x00dev);
  1530. /*
  1531. * 2 - Tx ring done interrupt.
  1532. */
  1533. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TXDONE))
  1534. rt61pci_txdone(rt2x00dev);
  1535. /*
  1536. * 3 - Handle MCU command done.
  1537. */
  1538. if (reg_mcu)
  1539. rt2x00pci_register_write(rt2x00dev,
  1540. M2H_CMD_DONE_CSR, 0xffffffff);
  1541. return IRQ_HANDLED;
  1542. }
  1543. /*
  1544. * Device probe functions.
  1545. */
  1546. static int rt61pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  1547. {
  1548. struct eeprom_93cx6 eeprom;
  1549. u32 reg;
  1550. u16 word;
  1551. u8 *mac;
  1552. s8 value;
  1553. rt2x00pci_register_read(rt2x00dev, E2PROM_CSR, &reg);
  1554. eeprom.data = rt2x00dev;
  1555. eeprom.register_read = rt61pci_eepromregister_read;
  1556. eeprom.register_write = rt61pci_eepromregister_write;
  1557. eeprom.width = rt2x00_get_field32(reg, E2PROM_CSR_TYPE_93C46) ?
  1558. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  1559. eeprom.reg_data_in = 0;
  1560. eeprom.reg_data_out = 0;
  1561. eeprom.reg_data_clock = 0;
  1562. eeprom.reg_chip_select = 0;
  1563. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  1564. EEPROM_SIZE / sizeof(u16));
  1565. /*
  1566. * Start validation of the data that has been read.
  1567. */
  1568. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  1569. if (!is_valid_ether_addr(mac)) {
  1570. DECLARE_MAC_BUF(macbuf);
  1571. random_ether_addr(mac);
  1572. EEPROM(rt2x00dev, "MAC: %s\n", print_mac(macbuf, mac));
  1573. }
  1574. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  1575. if (word == 0xffff) {
  1576. rt2x00_set_field16(&word, EEPROM_ANTENNA_NUM, 2);
  1577. rt2x00_set_field16(&word, EEPROM_ANTENNA_TX_DEFAULT,
  1578. ANTENNA_B);
  1579. rt2x00_set_field16(&word, EEPROM_ANTENNA_RX_DEFAULT,
  1580. ANTENNA_B);
  1581. rt2x00_set_field16(&word, EEPROM_ANTENNA_FRAME_TYPE, 0);
  1582. rt2x00_set_field16(&word, EEPROM_ANTENNA_DYN_TXAGC, 0);
  1583. rt2x00_set_field16(&word, EEPROM_ANTENNA_HARDWARE_RADIO, 0);
  1584. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF5225);
  1585. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  1586. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  1587. }
  1588. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  1589. if (word == 0xffff) {
  1590. rt2x00_set_field16(&word, EEPROM_NIC_ENABLE_DIVERSITY, 0);
  1591. rt2x00_set_field16(&word, EEPROM_NIC_TX_DIVERSITY, 0);
  1592. rt2x00_set_field16(&word, EEPROM_NIC_TX_RX_FIXED, 0);
  1593. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  1594. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  1595. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  1596. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  1597. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  1598. }
  1599. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &word);
  1600. if (word == 0xffff) {
  1601. rt2x00_set_field16(&word, EEPROM_LED_LED_MODE,
  1602. LED_MODE_DEFAULT);
  1603. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED, word);
  1604. EEPROM(rt2x00dev, "Led: 0x%04x\n", word);
  1605. }
  1606. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  1607. if (word == 0xffff) {
  1608. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  1609. rt2x00_set_field16(&word, EEPROM_FREQ_SEQ, 0);
  1610. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  1611. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  1612. }
  1613. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_BG, &word);
  1614. if (word == 0xffff) {
  1615. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1616. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1617. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1618. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  1619. } else {
  1620. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_1);
  1621. if (value < -10 || value > 10)
  1622. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_1, 0);
  1623. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_BG_2);
  1624. if (value < -10 || value > 10)
  1625. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_BG_2, 0);
  1626. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_BG, word);
  1627. }
  1628. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_OFFSET_A, &word);
  1629. if (word == 0xffff) {
  1630. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1631. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1632. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1633. EEPROM(rt2x00dev, "RSSI OFFSET BG: 0x%04x\n", word);
  1634. } else {
  1635. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_1);
  1636. if (value < -10 || value > 10)
  1637. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_1, 0);
  1638. value = rt2x00_get_field16(word, EEPROM_RSSI_OFFSET_A_2);
  1639. if (value < -10 || value > 10)
  1640. rt2x00_set_field16(&word, EEPROM_RSSI_OFFSET_A_2, 0);
  1641. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_OFFSET_A, word);
  1642. }
  1643. return 0;
  1644. }
  1645. static int rt61pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  1646. {
  1647. u32 reg;
  1648. u16 value;
  1649. u16 eeprom;
  1650. u16 device;
  1651. /*
  1652. * Read EEPROM word for configuration.
  1653. */
  1654. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  1655. /*
  1656. * Identify RF chipset.
  1657. * To determine the RT chip we have to read the
  1658. * PCI header of the device.
  1659. */
  1660. pci_read_config_word(rt2x00dev_pci(rt2x00dev),
  1661. PCI_CONFIG_HEADER_DEVICE, &device);
  1662. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  1663. rt2x00pci_register_read(rt2x00dev, MAC_CSR0, &reg);
  1664. rt2x00_set_chip(rt2x00dev, device, value, reg);
  1665. if (!rt2x00_rf(&rt2x00dev->chip, RF5225) &&
  1666. !rt2x00_rf(&rt2x00dev->chip, RF5325) &&
  1667. !rt2x00_rf(&rt2x00dev->chip, RF2527) &&
  1668. !rt2x00_rf(&rt2x00dev->chip, RF2529)) {
  1669. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  1670. return -ENODEV;
  1671. }
  1672. /*
  1673. * Determine number of antenna's.
  1674. */
  1675. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_NUM) == 2)
  1676. __set_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags);
  1677. /*
  1678. * Identify default antenna configuration.
  1679. */
  1680. rt2x00dev->default_ant.tx =
  1681. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  1682. rt2x00dev->default_ant.rx =
  1683. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  1684. /*
  1685. * Read the Frame type.
  1686. */
  1687. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_FRAME_TYPE))
  1688. __set_bit(CONFIG_FRAME_TYPE, &rt2x00dev->flags);
  1689. /*
  1690. * Detect if this device has an hardware controlled radio.
  1691. */
  1692. #ifdef CONFIG_RT61PCI_RFKILL
  1693. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  1694. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  1695. #endif /* CONFIG_RT61PCI_RFKILL */
  1696. /*
  1697. * Read frequency offset and RF programming sequence.
  1698. */
  1699. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  1700. if (rt2x00_get_field16(eeprom, EEPROM_FREQ_SEQ))
  1701. __set_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags);
  1702. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  1703. /*
  1704. * Read external LNA informations.
  1705. */
  1706. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  1707. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  1708. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  1709. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  1710. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  1711. /*
  1712. * When working with a RF2529 chip without double antenna
  1713. * the antenna settings should be gathered from the NIC
  1714. * eeprom word.
  1715. */
  1716. if (rt2x00_rf(&rt2x00dev->chip, RF2529) &&
  1717. !test_bit(CONFIG_DOUBLE_ANTENNA, &rt2x00dev->flags)) {
  1718. switch (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_RX_FIXED)) {
  1719. case 0:
  1720. rt2x00dev->default_ant.tx = ANTENNA_B;
  1721. rt2x00dev->default_ant.rx = ANTENNA_A;
  1722. break;
  1723. case 1:
  1724. rt2x00dev->default_ant.tx = ANTENNA_B;
  1725. rt2x00dev->default_ant.rx = ANTENNA_B;
  1726. break;
  1727. case 2:
  1728. rt2x00dev->default_ant.tx = ANTENNA_A;
  1729. rt2x00dev->default_ant.rx = ANTENNA_A;
  1730. break;
  1731. case 3:
  1732. rt2x00dev->default_ant.tx = ANTENNA_A;
  1733. rt2x00dev->default_ant.rx = ANTENNA_B;
  1734. break;
  1735. }
  1736. if (rt2x00_get_field16(eeprom, EEPROM_NIC_TX_DIVERSITY))
  1737. rt2x00dev->default_ant.tx = ANTENNA_SW_DIVERSITY;
  1738. if (rt2x00_get_field16(eeprom, EEPROM_NIC_ENABLE_DIVERSITY))
  1739. rt2x00dev->default_ant.rx = ANTENNA_SW_DIVERSITY;
  1740. }
  1741. /*
  1742. * Store led settings, for correct led behaviour.
  1743. * If the eeprom value is invalid,
  1744. * switch to default led mode.
  1745. */
  1746. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED, &eeprom);
  1747. rt2x00dev->led_mode = rt2x00_get_field16(eeprom, EEPROM_LED_LED_MODE);
  1748. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_LED_MODE,
  1749. rt2x00dev->led_mode);
  1750. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_0,
  1751. rt2x00_get_field16(eeprom,
  1752. EEPROM_LED_POLARITY_GPIO_0));
  1753. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_1,
  1754. rt2x00_get_field16(eeprom,
  1755. EEPROM_LED_POLARITY_GPIO_1));
  1756. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_2,
  1757. rt2x00_get_field16(eeprom,
  1758. EEPROM_LED_POLARITY_GPIO_2));
  1759. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_3,
  1760. rt2x00_get_field16(eeprom,
  1761. EEPROM_LED_POLARITY_GPIO_3));
  1762. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_GPIO_4,
  1763. rt2x00_get_field16(eeprom,
  1764. EEPROM_LED_POLARITY_GPIO_4));
  1765. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_ACT,
  1766. rt2x00_get_field16(eeprom, EEPROM_LED_POLARITY_ACT));
  1767. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_READY_BG,
  1768. rt2x00_get_field16(eeprom,
  1769. EEPROM_LED_POLARITY_RDY_G));
  1770. rt2x00_set_field16(&rt2x00dev->led_reg, MCU_LEDCS_POLARITY_READY_A,
  1771. rt2x00_get_field16(eeprom,
  1772. EEPROM_LED_POLARITY_RDY_A));
  1773. return 0;
  1774. }
  1775. /*
  1776. * RF value list for RF5225 & RF5325
  1777. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence disabled
  1778. */
  1779. static const struct rf_channel rf_vals_noseq[] = {
  1780. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  1781. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  1782. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  1783. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  1784. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  1785. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  1786. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  1787. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  1788. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  1789. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  1790. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  1791. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  1792. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  1793. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  1794. /* 802.11 UNI / HyperLan 2 */
  1795. { 36, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa23 },
  1796. { 40, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa03 },
  1797. { 44, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa0b },
  1798. { 48, 0x00002ccc, 0x000049aa, 0x0009be55, 0x000ffa13 },
  1799. { 52, 0x00002ccc, 0x000049ae, 0x0009ae55, 0x000ffa1b },
  1800. { 56, 0x00002ccc, 0x000049b2, 0x0009ae55, 0x000ffa23 },
  1801. { 60, 0x00002ccc, 0x000049ba, 0x0009ae55, 0x000ffa03 },
  1802. { 64, 0x00002ccc, 0x000049be, 0x0009ae55, 0x000ffa0b },
  1803. /* 802.11 HyperLan 2 */
  1804. { 100, 0x00002ccc, 0x00004a2a, 0x000bae55, 0x000ffa03 },
  1805. { 104, 0x00002ccc, 0x00004a2e, 0x000bae55, 0x000ffa0b },
  1806. { 108, 0x00002ccc, 0x00004a32, 0x000bae55, 0x000ffa13 },
  1807. { 112, 0x00002ccc, 0x00004a36, 0x000bae55, 0x000ffa1b },
  1808. { 116, 0x00002ccc, 0x00004a3a, 0x000bbe55, 0x000ffa23 },
  1809. { 120, 0x00002ccc, 0x00004a82, 0x000bbe55, 0x000ffa03 },
  1810. { 124, 0x00002ccc, 0x00004a86, 0x000bbe55, 0x000ffa0b },
  1811. { 128, 0x00002ccc, 0x00004a8a, 0x000bbe55, 0x000ffa13 },
  1812. { 132, 0x00002ccc, 0x00004a8e, 0x000bbe55, 0x000ffa1b },
  1813. { 136, 0x00002ccc, 0x00004a92, 0x000bbe55, 0x000ffa23 },
  1814. /* 802.11 UNII */
  1815. { 140, 0x00002ccc, 0x00004a9a, 0x000bbe55, 0x000ffa03 },
  1816. { 149, 0x00002ccc, 0x00004aa2, 0x000bbe55, 0x000ffa1f },
  1817. { 153, 0x00002ccc, 0x00004aa6, 0x000bbe55, 0x000ffa27 },
  1818. { 157, 0x00002ccc, 0x00004aae, 0x000bbe55, 0x000ffa07 },
  1819. { 161, 0x00002ccc, 0x00004ab2, 0x000bbe55, 0x000ffa0f },
  1820. { 165, 0x00002ccc, 0x00004ab6, 0x000bbe55, 0x000ffa17 },
  1821. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1822. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000ffa0b },
  1823. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000ffa13 },
  1824. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000ffa1b },
  1825. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000ffa23 },
  1826. };
  1827. /*
  1828. * RF value list for RF5225 & RF5325
  1829. * Supports: 2.4 GHz & 5.2 GHz, rf_sequence enabled
  1830. */
  1831. static const struct rf_channel rf_vals_seq[] = {
  1832. { 1, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa0b },
  1833. { 2, 0x00002ccc, 0x00004786, 0x00068455, 0x000ffa1f },
  1834. { 3, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa0b },
  1835. { 4, 0x00002ccc, 0x0000478a, 0x00068455, 0x000ffa1f },
  1836. { 5, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa0b },
  1837. { 6, 0x00002ccc, 0x0000478e, 0x00068455, 0x000ffa1f },
  1838. { 7, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa0b },
  1839. { 8, 0x00002ccc, 0x00004792, 0x00068455, 0x000ffa1f },
  1840. { 9, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa0b },
  1841. { 10, 0x00002ccc, 0x00004796, 0x00068455, 0x000ffa1f },
  1842. { 11, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa0b },
  1843. { 12, 0x00002ccc, 0x0000479a, 0x00068455, 0x000ffa1f },
  1844. { 13, 0x00002ccc, 0x0000479e, 0x00068455, 0x000ffa0b },
  1845. { 14, 0x00002ccc, 0x000047a2, 0x00068455, 0x000ffa13 },
  1846. /* 802.11 UNI / HyperLan 2 */
  1847. { 36, 0x00002cd4, 0x0004481a, 0x00098455, 0x000c0a03 },
  1848. { 40, 0x00002cd0, 0x00044682, 0x00098455, 0x000c0a03 },
  1849. { 44, 0x00002cd0, 0x00044686, 0x00098455, 0x000c0a1b },
  1850. { 48, 0x00002cd0, 0x0004468e, 0x00098655, 0x000c0a0b },
  1851. { 52, 0x00002cd0, 0x00044692, 0x00098855, 0x000c0a23 },
  1852. { 56, 0x00002cd0, 0x0004469a, 0x00098c55, 0x000c0a13 },
  1853. { 60, 0x00002cd0, 0x000446a2, 0x00098e55, 0x000c0a03 },
  1854. { 64, 0x00002cd0, 0x000446a6, 0x00099255, 0x000c0a1b },
  1855. /* 802.11 HyperLan 2 */
  1856. { 100, 0x00002cd4, 0x0004489a, 0x000b9855, 0x000c0a03 },
  1857. { 104, 0x00002cd4, 0x000448a2, 0x000b9855, 0x000c0a03 },
  1858. { 108, 0x00002cd4, 0x000448aa, 0x000b9855, 0x000c0a03 },
  1859. { 112, 0x00002cd4, 0x000448b2, 0x000b9a55, 0x000c0a03 },
  1860. { 116, 0x00002cd4, 0x000448ba, 0x000b9a55, 0x000c0a03 },
  1861. { 120, 0x00002cd0, 0x00044702, 0x000b9a55, 0x000c0a03 },
  1862. { 124, 0x00002cd0, 0x00044706, 0x000b9a55, 0x000c0a1b },
  1863. { 128, 0x00002cd0, 0x0004470e, 0x000b9c55, 0x000c0a0b },
  1864. { 132, 0x00002cd0, 0x00044712, 0x000b9c55, 0x000c0a23 },
  1865. { 136, 0x00002cd0, 0x0004471a, 0x000b9e55, 0x000c0a13 },
  1866. /* 802.11 UNII */
  1867. { 140, 0x00002cd0, 0x00044722, 0x000b9e55, 0x000c0a03 },
  1868. { 149, 0x00002cd0, 0x0004472e, 0x000ba255, 0x000c0a1b },
  1869. { 153, 0x00002cd0, 0x00044736, 0x000ba255, 0x000c0a0b },
  1870. { 157, 0x00002cd4, 0x0004490a, 0x000ba255, 0x000c0a17 },
  1871. { 161, 0x00002cd4, 0x00044912, 0x000ba255, 0x000c0a17 },
  1872. { 165, 0x00002cd4, 0x0004491a, 0x000ba255, 0x000c0a17 },
  1873. /* MMAC(Japan)J52 ch 34,38,42,46 */
  1874. { 34, 0x00002ccc, 0x0000499a, 0x0009be55, 0x000c0a0b },
  1875. { 38, 0x00002ccc, 0x0000499e, 0x0009be55, 0x000c0a13 },
  1876. { 42, 0x00002ccc, 0x000049a2, 0x0009be55, 0x000c0a1b },
  1877. { 46, 0x00002ccc, 0x000049a6, 0x0009be55, 0x000c0a23 },
  1878. };
  1879. static void rt61pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  1880. {
  1881. struct hw_mode_spec *spec = &rt2x00dev->spec;
  1882. u8 *txpower;
  1883. unsigned int i;
  1884. /*
  1885. * Initialize all hw fields.
  1886. */
  1887. rt2x00dev->hw->flags =
  1888. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  1889. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  1890. rt2x00dev->hw->extra_tx_headroom = 0;
  1891. rt2x00dev->hw->max_signal = MAX_SIGNAL;
  1892. rt2x00dev->hw->max_rssi = MAX_RX_SSI;
  1893. rt2x00dev->hw->queues = 5;
  1894. SET_IEEE80211_DEV(rt2x00dev->hw, &rt2x00dev_pci(rt2x00dev)->dev);
  1895. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  1896. rt2x00_eeprom_addr(rt2x00dev,
  1897. EEPROM_MAC_ADDR_0));
  1898. /*
  1899. * Convert tx_power array in eeprom.
  1900. */
  1901. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_G_START);
  1902. for (i = 0; i < 14; i++)
  1903. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1904. /*
  1905. * Initialize hw_mode information.
  1906. */
  1907. spec->num_modes = 2;
  1908. spec->num_rates = 12;
  1909. spec->tx_power_a = NULL;
  1910. spec->tx_power_bg = txpower;
  1911. spec->tx_power_default = DEFAULT_TXPOWER;
  1912. if (!test_bit(CONFIG_RF_SEQUENCE, &rt2x00dev->flags)) {
  1913. spec->num_channels = 14;
  1914. spec->channels = rf_vals_noseq;
  1915. } else {
  1916. spec->num_channels = 14;
  1917. spec->channels = rf_vals_seq;
  1918. }
  1919. if (rt2x00_rf(&rt2x00dev->chip, RF5225) ||
  1920. rt2x00_rf(&rt2x00dev->chip, RF5325)) {
  1921. spec->num_modes = 3;
  1922. spec->num_channels = ARRAY_SIZE(rf_vals_seq);
  1923. txpower = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A_START);
  1924. for (i = 0; i < 14; i++)
  1925. txpower[i] = TXPOWER_FROM_DEV(txpower[i]);
  1926. spec->tx_power_a = txpower;
  1927. }
  1928. }
  1929. static int rt61pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  1930. {
  1931. int retval;
  1932. /*
  1933. * Allocate eeprom data.
  1934. */
  1935. retval = rt61pci_validate_eeprom(rt2x00dev);
  1936. if (retval)
  1937. return retval;
  1938. retval = rt61pci_init_eeprom(rt2x00dev);
  1939. if (retval)
  1940. return retval;
  1941. /*
  1942. * Initialize hw specifications.
  1943. */
  1944. rt61pci_probe_hw_mode(rt2x00dev);
  1945. /*
  1946. * This device requires firmware.
  1947. */
  1948. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  1949. __set_bit(DRIVER_REQUIRE_FIRMWARE_CRC_ITU_T, &rt2x00dev->flags);
  1950. /*
  1951. * Set the rssi offset.
  1952. */
  1953. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  1954. return 0;
  1955. }
  1956. /*
  1957. * IEEE80211 stack callback functions.
  1958. */
  1959. static void rt61pci_configure_filter(struct ieee80211_hw *hw,
  1960. unsigned int changed_flags,
  1961. unsigned int *total_flags,
  1962. int mc_count,
  1963. struct dev_addr_list *mc_list)
  1964. {
  1965. struct rt2x00_dev *rt2x00dev = hw->priv;
  1966. u32 reg;
  1967. /*
  1968. * Mask off any flags we are going to ignore from
  1969. * the total_flags field.
  1970. */
  1971. *total_flags &=
  1972. FIF_ALLMULTI |
  1973. FIF_FCSFAIL |
  1974. FIF_PLCPFAIL |
  1975. FIF_CONTROL |
  1976. FIF_OTHER_BSS |
  1977. FIF_PROMISC_IN_BSS;
  1978. /*
  1979. * Apply some rules to the filters:
  1980. * - Some filters imply different filters to be set.
  1981. * - Some things we can't filter out at all.
  1982. */
  1983. if (mc_count)
  1984. *total_flags |= FIF_ALLMULTI;
  1985. if (*total_flags & FIF_OTHER_BSS ||
  1986. *total_flags & FIF_PROMISC_IN_BSS)
  1987. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  1988. /*
  1989. * Check if there is any work left for us.
  1990. */
  1991. if (rt2x00dev->packet_filter == *total_flags)
  1992. return;
  1993. rt2x00dev->packet_filter = *total_flags;
  1994. /*
  1995. * Start configuration steps.
  1996. * Note that the version error will always be dropped
  1997. * and broadcast frames will always be accepted since
  1998. * there is no filter for it at this time.
  1999. */
  2000. rt2x00pci_register_read(rt2x00dev, TXRX_CSR0, &reg);
  2001. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CRC,
  2002. !(*total_flags & FIF_FCSFAIL));
  2003. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_PHYSICAL,
  2004. !(*total_flags & FIF_PLCPFAIL));
  2005. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_CONTROL,
  2006. !(*total_flags & FIF_CONTROL));
  2007. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_NOT_TO_ME,
  2008. !(*total_flags & FIF_PROMISC_IN_BSS));
  2009. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_TO_DS,
  2010. !(*total_flags & FIF_PROMISC_IN_BSS));
  2011. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_VERSION_ERROR, 1);
  2012. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_MULTICAST,
  2013. !(*total_flags & FIF_ALLMULTI));
  2014. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_BORADCAST, 0);
  2015. rt2x00_set_field32(&reg, TXRX_CSR0_DROP_ACK_CTS, 1);
  2016. rt2x00pci_register_write(rt2x00dev, TXRX_CSR0, reg);
  2017. }
  2018. static int rt61pci_set_retry_limit(struct ieee80211_hw *hw,
  2019. u32 short_retry, u32 long_retry)
  2020. {
  2021. struct rt2x00_dev *rt2x00dev = hw->priv;
  2022. u32 reg;
  2023. rt2x00pci_register_read(rt2x00dev, TXRX_CSR4, &reg);
  2024. rt2x00_set_field32(&reg, TXRX_CSR4_LONG_RETRY_LIMIT, long_retry);
  2025. rt2x00_set_field32(&reg, TXRX_CSR4_SHORT_RETRY_LIMIT, short_retry);
  2026. rt2x00pci_register_write(rt2x00dev, TXRX_CSR4, reg);
  2027. return 0;
  2028. }
  2029. static u64 rt61pci_get_tsf(struct ieee80211_hw *hw)
  2030. {
  2031. struct rt2x00_dev *rt2x00dev = hw->priv;
  2032. u64 tsf;
  2033. u32 reg;
  2034. rt2x00pci_register_read(rt2x00dev, TXRX_CSR13, &reg);
  2035. tsf = (u64) rt2x00_get_field32(reg, TXRX_CSR13_HIGH_TSFTIMER) << 32;
  2036. rt2x00pci_register_read(rt2x00dev, TXRX_CSR12, &reg);
  2037. tsf |= rt2x00_get_field32(reg, TXRX_CSR12_LOW_TSFTIMER);
  2038. return tsf;
  2039. }
  2040. static void rt61pci_reset_tsf(struct ieee80211_hw *hw)
  2041. {
  2042. struct rt2x00_dev *rt2x00dev = hw->priv;
  2043. rt2x00pci_register_write(rt2x00dev, TXRX_CSR12, 0);
  2044. rt2x00pci_register_write(rt2x00dev, TXRX_CSR13, 0);
  2045. }
  2046. static int rt61pci_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  2047. struct ieee80211_tx_control *control)
  2048. {
  2049. struct rt2x00_dev *rt2x00dev = hw->priv;
  2050. struct rt2x00_intf *intf = vif_to_intf(control->vif);
  2051. struct skb_frame_desc *skbdesc;
  2052. unsigned int beacon_base;
  2053. if (unlikely(!intf->beacon))
  2054. return -ENOBUFS;
  2055. /*
  2056. * We need to append the descriptor in front of the
  2057. * beacon frame.
  2058. */
  2059. if (skb_headroom(skb) < intf->beacon->queue->desc_size) {
  2060. if (pskb_expand_head(skb, intf->beacon->queue->desc_size,
  2061. 0, GFP_ATOMIC)) {
  2062. dev_kfree_skb(skb);
  2063. return -ENOMEM;
  2064. }
  2065. }
  2066. /*
  2067. * Add the descriptor in front of the skb.
  2068. */
  2069. skb_push(skb, intf->beacon->queue->desc_size);
  2070. memset(skb->data, 0, intf->beacon->queue->desc_size);
  2071. /*
  2072. * Fill in skb descriptor
  2073. */
  2074. skbdesc = get_skb_frame_desc(skb);
  2075. memset(skbdesc, 0, sizeof(*skbdesc));
  2076. skbdesc->data = skb->data + intf->beacon->queue->desc_size;
  2077. skbdesc->data_len = skb->len - intf->beacon->queue->desc_size;
  2078. skbdesc->desc = skb->data;
  2079. skbdesc->desc_len = intf->beacon->queue->desc_size;
  2080. skbdesc->entry = intf->beacon;
  2081. /*
  2082. * Just in case the ieee80211 doesn't set this,
  2083. * but we need this queue set for the descriptor
  2084. * initialization.
  2085. */
  2086. control->queue = IEEE80211_TX_QUEUE_BEACON;
  2087. rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
  2088. /*
  2089. * Write entire beacon with descriptor to register,
  2090. * and kick the beacon generator.
  2091. */
  2092. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  2093. rt2x00pci_register_multiwrite(rt2x00dev, beacon_base,
  2094. skb->data, skb->len);
  2095. rt61pci_kick_tx_queue(rt2x00dev, control->queue);
  2096. return 0;
  2097. }
  2098. static const struct ieee80211_ops rt61pci_mac80211_ops = {
  2099. .tx = rt2x00mac_tx,
  2100. .start = rt2x00mac_start,
  2101. .stop = rt2x00mac_stop,
  2102. .add_interface = rt2x00mac_add_interface,
  2103. .remove_interface = rt2x00mac_remove_interface,
  2104. .config = rt2x00mac_config,
  2105. .config_interface = rt2x00mac_config_interface,
  2106. .configure_filter = rt61pci_configure_filter,
  2107. .get_stats = rt2x00mac_get_stats,
  2108. .set_retry_limit = rt61pci_set_retry_limit,
  2109. .bss_info_changed = rt2x00mac_bss_info_changed,
  2110. .conf_tx = rt2x00mac_conf_tx,
  2111. .get_tx_stats = rt2x00mac_get_tx_stats,
  2112. .get_tsf = rt61pci_get_tsf,
  2113. .reset_tsf = rt61pci_reset_tsf,
  2114. .beacon_update = rt61pci_beacon_update,
  2115. };
  2116. static const struct rt2x00lib_ops rt61pci_rt2x00_ops = {
  2117. .irq_handler = rt61pci_interrupt,
  2118. .probe_hw = rt61pci_probe_hw,
  2119. .get_firmware_name = rt61pci_get_firmware_name,
  2120. .load_firmware = rt61pci_load_firmware,
  2121. .initialize = rt2x00pci_initialize,
  2122. .uninitialize = rt2x00pci_uninitialize,
  2123. .init_rxentry = rt61pci_init_rxentry,
  2124. .init_txentry = rt61pci_init_txentry,
  2125. .set_device_state = rt61pci_set_device_state,
  2126. .rfkill_poll = rt61pci_rfkill_poll,
  2127. .link_stats = rt61pci_link_stats,
  2128. .reset_tuner = rt61pci_reset_tuner,
  2129. .link_tuner = rt61pci_link_tuner,
  2130. .write_tx_desc = rt61pci_write_tx_desc,
  2131. .write_tx_data = rt2x00pci_write_tx_data,
  2132. .kick_tx_queue = rt61pci_kick_tx_queue,
  2133. .fill_rxdone = rt61pci_fill_rxdone,
  2134. .config_intf = rt61pci_config_intf,
  2135. .config_preamble = rt61pci_config_preamble,
  2136. .config = rt61pci_config,
  2137. };
  2138. static const struct data_queue_desc rt61pci_queue_rx = {
  2139. .entry_num = RX_ENTRIES,
  2140. .data_size = DATA_FRAME_SIZE,
  2141. .desc_size = RXD_DESC_SIZE,
  2142. .priv_size = sizeof(struct queue_entry_priv_pci_rx),
  2143. };
  2144. static const struct data_queue_desc rt61pci_queue_tx = {
  2145. .entry_num = TX_ENTRIES,
  2146. .data_size = DATA_FRAME_SIZE,
  2147. .desc_size = TXD_DESC_SIZE,
  2148. .priv_size = sizeof(struct queue_entry_priv_pci_tx),
  2149. };
  2150. static const struct data_queue_desc rt61pci_queue_bcn = {
  2151. .entry_num = 4 * BEACON_ENTRIES,
  2152. .data_size = MGMT_FRAME_SIZE,
  2153. .desc_size = TXINFO_SIZE,
  2154. .priv_size = sizeof(struct queue_entry_priv_pci_tx),
  2155. };
  2156. static const struct rt2x00_ops rt61pci_ops = {
  2157. .name = KBUILD_MODNAME,
  2158. .max_sta_intf = 1,
  2159. .max_ap_intf = 4,
  2160. .eeprom_size = EEPROM_SIZE,
  2161. .rf_size = RF_SIZE,
  2162. .rx = &rt61pci_queue_rx,
  2163. .tx = &rt61pci_queue_tx,
  2164. .bcn = &rt61pci_queue_bcn,
  2165. .lib = &rt61pci_rt2x00_ops,
  2166. .hw = &rt61pci_mac80211_ops,
  2167. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2168. .debugfs = &rt61pci_rt2x00debug,
  2169. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2170. };
  2171. /*
  2172. * RT61pci module information.
  2173. */
  2174. static struct pci_device_id rt61pci_device_table[] = {
  2175. /* RT2561s */
  2176. { PCI_DEVICE(0x1814, 0x0301), PCI_DEVICE_DATA(&rt61pci_ops) },
  2177. /* RT2561 v2 */
  2178. { PCI_DEVICE(0x1814, 0x0302), PCI_DEVICE_DATA(&rt61pci_ops) },
  2179. /* RT2661 */
  2180. { PCI_DEVICE(0x1814, 0x0401), PCI_DEVICE_DATA(&rt61pci_ops) },
  2181. { 0, }
  2182. };
  2183. MODULE_AUTHOR(DRV_PROJECT);
  2184. MODULE_VERSION(DRV_VERSION);
  2185. MODULE_DESCRIPTION("Ralink RT61 PCI & PCMCIA Wireless LAN driver.");
  2186. MODULE_SUPPORTED_DEVICE("Ralink RT2561, RT2561s & RT2661 "
  2187. "PCI & PCMCIA chipset based cards");
  2188. MODULE_DEVICE_TABLE(pci, rt61pci_device_table);
  2189. MODULE_FIRMWARE(FIRMWARE_RT2561);
  2190. MODULE_FIRMWARE(FIRMWARE_RT2561s);
  2191. MODULE_FIRMWARE(FIRMWARE_RT2661);
  2192. MODULE_LICENSE("GPL");
  2193. static struct pci_driver rt61pci_driver = {
  2194. .name = KBUILD_MODNAME,
  2195. .id_table = rt61pci_device_table,
  2196. .probe = rt2x00pci_probe,
  2197. .remove = __devexit_p(rt2x00pci_remove),
  2198. .suspend = rt2x00pci_suspend,
  2199. .resume = rt2x00pci_resume,
  2200. };
  2201. static int __init rt61pci_init(void)
  2202. {
  2203. return pci_register_driver(&rt61pci_driver);
  2204. }
  2205. static void __exit rt61pci_exit(void)
  2206. {
  2207. pci_unregister_driver(&rt61pci_driver);
  2208. }
  2209. module_init(rt61pci_init);
  2210. module_exit(rt61pci_exit);