rt61pci.c 76 KB

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