rt2800pci.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376
  1. /*
  2. Copyright (C) 2004 - 2009 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: rt2800pci
  19. Abstract: rt2800pci device specific routines.
  20. Supported chipsets: RT2800E & RT2800ED.
  21. */
  22. #include <linux/crc-ccitt.h>
  23. #include <linux/delay.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/eeprom_93cx6.h>
  31. #include "rt2x00.h"
  32. #include "rt2x00pci.h"
  33. #include "rt2x00soc.h"
  34. #include "rt2800pci.h"
  35. #ifdef CONFIG_RT2800PCI_PCI_MODULE
  36. #define CONFIG_RT2800PCI_PCI
  37. #endif
  38. #ifdef CONFIG_RT2800PCI_WISOC_MODULE
  39. #define CONFIG_RT2800PCI_WISOC
  40. #endif
  41. /*
  42. * Allow hardware encryption to be disabled.
  43. */
  44. static int modparam_nohwcrypt = 1;
  45. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  46. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  47. /*
  48. * Register access.
  49. * All access to the CSR registers will go through the methods
  50. * rt2800_register_read and rt2800_register_write.
  51. * BBP and RF register require indirect register access,
  52. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  53. * These indirect registers work with busy bits,
  54. * and we will try maximal REGISTER_BUSY_COUNT times to access
  55. * the register while taking a REGISTER_BUSY_DELAY us delay
  56. * between each attampt. When the busy bit is still set at that time,
  57. * the access attempt is considered to have failed,
  58. * and we will print an error.
  59. * The _lock versions must be used if you already hold the csr_mutex
  60. */
  61. #define WAIT_FOR_BBP(__dev, __reg) \
  62. rt2800_regbusy_read((__dev), BBP_CSR_CFG, BBP_CSR_CFG_BUSY, (__reg))
  63. #define WAIT_FOR_RFCSR(__dev, __reg) \
  64. rt2800_regbusy_read((__dev), RF_CSR_CFG, RF_CSR_CFG_BUSY, (__reg))
  65. #define WAIT_FOR_RF(__dev, __reg) \
  66. rt2800_regbusy_read((__dev), RF_CSR_CFG0, RF_CSR_CFG0_BUSY, (__reg))
  67. #define WAIT_FOR_MCU(__dev, __reg) \
  68. rt2800_regbusy_read((__dev), H2M_MAILBOX_CSR, \
  69. H2M_MAILBOX_CSR_OWNER, (__reg))
  70. static void rt2800pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  71. const unsigned int word, const u8 value)
  72. {
  73. u32 reg;
  74. mutex_lock(&rt2x00dev->csr_mutex);
  75. /*
  76. * Wait until the BBP becomes available, afterwards we
  77. * can safely write the new data into the register.
  78. */
  79. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  80. reg = 0;
  81. rt2x00_set_field32(&reg, BBP_CSR_CFG_VALUE, value);
  82. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  83. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  84. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 0);
  85. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  86. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  87. }
  88. mutex_unlock(&rt2x00dev->csr_mutex);
  89. }
  90. static void rt2800pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  91. const unsigned int word, u8 *value)
  92. {
  93. u32 reg;
  94. mutex_lock(&rt2x00dev->csr_mutex);
  95. /*
  96. * Wait until the BBP becomes available, afterwards we
  97. * can safely write the read request into the register.
  98. * After the data has been written, we wait until hardware
  99. * returns the correct value, if at any time the register
  100. * doesn't become available in time, reg will be 0xffffffff
  101. * which means we return 0xff to the caller.
  102. */
  103. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  104. reg = 0;
  105. rt2x00_set_field32(&reg, BBP_CSR_CFG_REGNUM, word);
  106. rt2x00_set_field32(&reg, BBP_CSR_CFG_BUSY, 1);
  107. rt2x00_set_field32(&reg, BBP_CSR_CFG_READ_CONTROL, 1);
  108. rt2x00_set_field32(&reg, BBP_CSR_CFG_BBP_RW_MODE, 1);
  109. rt2800_register_write_lock(rt2x00dev, BBP_CSR_CFG, reg);
  110. WAIT_FOR_BBP(rt2x00dev, &reg);
  111. }
  112. *value = rt2x00_get_field32(reg, BBP_CSR_CFG_VALUE);
  113. mutex_unlock(&rt2x00dev->csr_mutex);
  114. }
  115. static inline void rt2800_bbp_write(struct rt2x00_dev *rt2x00dev,
  116. const unsigned int word, const u8 value)
  117. {
  118. rt2800pci_bbp_write(rt2x00dev, word, value);
  119. }
  120. static inline void rt2800_bbp_read(struct rt2x00_dev *rt2x00dev,
  121. const unsigned int word, u8 *value)
  122. {
  123. rt2800pci_bbp_read(rt2x00dev, word, value);
  124. }
  125. static void rt2800pci_rfcsr_write(struct rt2x00_dev *rt2x00dev,
  126. const unsigned int word, const u8 value)
  127. {
  128. u32 reg;
  129. mutex_lock(&rt2x00dev->csr_mutex);
  130. /*
  131. * Wait until the RFCSR becomes available, afterwards we
  132. * can safely write the new data into the register.
  133. */
  134. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  135. reg = 0;
  136. rt2x00_set_field32(&reg, RF_CSR_CFG_DATA, value);
  137. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  138. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 1);
  139. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  140. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  141. }
  142. mutex_unlock(&rt2x00dev->csr_mutex);
  143. }
  144. static void rt2800pci_rfcsr_read(struct rt2x00_dev *rt2x00dev,
  145. const unsigned int word, u8 *value)
  146. {
  147. u32 reg;
  148. mutex_lock(&rt2x00dev->csr_mutex);
  149. /*
  150. * Wait until the RFCSR becomes available, afterwards we
  151. * can safely write the read request into the register.
  152. * After the data has been written, we wait until hardware
  153. * returns the correct value, if at any time the register
  154. * doesn't become available in time, reg will be 0xffffffff
  155. * which means we return 0xff to the caller.
  156. */
  157. if (WAIT_FOR_RFCSR(rt2x00dev, &reg)) {
  158. reg = 0;
  159. rt2x00_set_field32(&reg, RF_CSR_CFG_REGNUM, word);
  160. rt2x00_set_field32(&reg, RF_CSR_CFG_WRITE, 0);
  161. rt2x00_set_field32(&reg, RF_CSR_CFG_BUSY, 1);
  162. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG, reg);
  163. WAIT_FOR_RFCSR(rt2x00dev, &reg);
  164. }
  165. *value = rt2x00_get_field32(reg, RF_CSR_CFG_DATA);
  166. mutex_unlock(&rt2x00dev->csr_mutex);
  167. }
  168. static inline void rt2800_rfcsr_write(struct rt2x00_dev *rt2x00dev,
  169. const unsigned int word, const u8 value)
  170. {
  171. rt2800pci_rfcsr_write(rt2x00dev, word, value);
  172. }
  173. static inline void rt2800_rfcsr_read(struct rt2x00_dev *rt2x00dev,
  174. const unsigned int word, u8 *value)
  175. {
  176. rt2800pci_rfcsr_read(rt2x00dev, word, value);
  177. }
  178. static void rt2800pci_rf_write(struct rt2x00_dev *rt2x00dev,
  179. const unsigned int word, const u32 value)
  180. {
  181. u32 reg;
  182. mutex_lock(&rt2x00dev->csr_mutex);
  183. /*
  184. * Wait until the RF becomes available, afterwards we
  185. * can safely write the new data into the register.
  186. */
  187. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  188. reg = 0;
  189. rt2x00_set_field32(&reg, RF_CSR_CFG0_REG_VALUE_BW, value);
  190. rt2x00_set_field32(&reg, RF_CSR_CFG0_STANDBYMODE, 0);
  191. rt2x00_set_field32(&reg, RF_CSR_CFG0_SEL, 0);
  192. rt2x00_set_field32(&reg, RF_CSR_CFG0_BUSY, 1);
  193. rt2800_register_write_lock(rt2x00dev, RF_CSR_CFG0, reg);
  194. rt2x00_rf_write(rt2x00dev, word, value);
  195. }
  196. mutex_unlock(&rt2x00dev->csr_mutex);
  197. }
  198. static inline void rt2800_rf_write(struct rt2x00_dev *rt2x00dev,
  199. const unsigned int word, const u32 value)
  200. {
  201. rt2800pci_rf_write(rt2x00dev, word, value);
  202. }
  203. static void rt2800pci_mcu_request(struct rt2x00_dev *rt2x00dev,
  204. const u8 command, const u8 token,
  205. const u8 arg0, const u8 arg1)
  206. {
  207. u32 reg;
  208. /*
  209. * RT2880 and RT3052 don't support MCU requests.
  210. */
  211. if (rt2x00_rt(&rt2x00dev->chip, RT2880) ||
  212. rt2x00_rt(&rt2x00dev->chip, RT3052))
  213. return;
  214. mutex_lock(&rt2x00dev->csr_mutex);
  215. /*
  216. * Wait until the MCU becomes available, afterwards we
  217. * can safely write the new data into the register.
  218. */
  219. if (WAIT_FOR_MCU(rt2x00dev, &reg)) {
  220. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_OWNER, 1);
  221. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_CMD_TOKEN, token);
  222. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG0, arg0);
  223. rt2x00_set_field32(&reg, H2M_MAILBOX_CSR_ARG1, arg1);
  224. rt2800_register_write_lock(rt2x00dev, H2M_MAILBOX_CSR, reg);
  225. reg = 0;
  226. rt2x00_set_field32(&reg, HOST_CMD_CSR_HOST_COMMAND, command);
  227. rt2800_register_write_lock(rt2x00dev, HOST_CMD_CSR, reg);
  228. }
  229. mutex_unlock(&rt2x00dev->csr_mutex);
  230. }
  231. static inline void rt2800_mcu_request(struct rt2x00_dev *rt2x00dev,
  232. const u8 command, const u8 token,
  233. const u8 arg0, const u8 arg1)
  234. {
  235. rt2800pci_mcu_request(rt2x00dev, command, token, arg0, arg1);
  236. }
  237. static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
  238. {
  239. unsigned int i;
  240. u32 reg;
  241. for (i = 0; i < 200; i++) {
  242. rt2800_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
  243. if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
  244. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
  245. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
  246. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
  247. break;
  248. udelay(REGISTER_BUSY_DELAY);
  249. }
  250. if (i == 200)
  251. ERROR(rt2x00dev, "MCU request failed, no response from hardware\n");
  252. rt2800_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  253. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  254. }
  255. #ifdef CONFIG_RT2800PCI_WISOC
  256. static void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  257. {
  258. u32 *base_addr = (u32 *) KSEG1ADDR(0x1F040000); /* XXX for RT3052 */
  259. memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
  260. }
  261. #else
  262. static inline void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  263. {
  264. }
  265. #endif /* CONFIG_RT2800PCI_WISOC */
  266. #ifdef CONFIG_RT2800PCI_PCI
  267. static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  268. {
  269. struct rt2x00_dev *rt2x00dev = eeprom->data;
  270. u32 reg;
  271. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  272. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  273. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  274. eeprom->reg_data_clock =
  275. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  276. eeprom->reg_chip_select =
  277. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  278. }
  279. static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  280. {
  281. struct rt2x00_dev *rt2x00dev = eeprom->data;
  282. u32 reg = 0;
  283. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  284. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  285. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  286. !!eeprom->reg_data_clock);
  287. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  288. !!eeprom->reg_chip_select);
  289. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  290. }
  291. static void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  292. {
  293. struct eeprom_93cx6 eeprom;
  294. u32 reg;
  295. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  296. eeprom.data = rt2x00dev;
  297. eeprom.register_read = rt2800pci_eepromregister_read;
  298. eeprom.register_write = rt2800pci_eepromregister_write;
  299. eeprom.width = !rt2x00_get_field32(reg, E2PROM_CSR_TYPE) ?
  300. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  301. eeprom.reg_data_in = 0;
  302. eeprom.reg_data_out = 0;
  303. eeprom.reg_data_clock = 0;
  304. eeprom.reg_chip_select = 0;
  305. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  306. EEPROM_SIZE / sizeof(u16));
  307. }
  308. static void rt2800pci_efuse_read(struct rt2x00_dev *rt2x00dev,
  309. unsigned int i)
  310. {
  311. u32 reg;
  312. rt2800_register_read(rt2x00dev, EFUSE_CTRL, &reg);
  313. rt2x00_set_field32(&reg, EFUSE_CTRL_ADDRESS_IN, i);
  314. rt2x00_set_field32(&reg, EFUSE_CTRL_MODE, 0);
  315. rt2x00_set_field32(&reg, EFUSE_CTRL_KICK, 1);
  316. rt2800_register_write(rt2x00dev, EFUSE_CTRL, reg);
  317. /* Wait until the EEPROM has been loaded */
  318. rt2800_regbusy_read(rt2x00dev, EFUSE_CTRL, EFUSE_CTRL_KICK, &reg);
  319. /* Apparently the data is read from end to start */
  320. rt2800_register_read(rt2x00dev, EFUSE_DATA3,
  321. (u32 *)&rt2x00dev->eeprom[i]);
  322. rt2800_register_read(rt2x00dev, EFUSE_DATA2,
  323. (u32 *)&rt2x00dev->eeprom[i + 2]);
  324. rt2800_register_read(rt2x00dev, EFUSE_DATA1,
  325. (u32 *)&rt2x00dev->eeprom[i + 4]);
  326. rt2800_register_read(rt2x00dev, EFUSE_DATA0,
  327. (u32 *)&rt2x00dev->eeprom[i + 6]);
  328. }
  329. static void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  330. {
  331. unsigned int i;
  332. for (i = 0; i < EEPROM_SIZE / sizeof(u16); i += 8)
  333. rt2800pci_efuse_read(rt2x00dev, i);
  334. }
  335. #else
  336. static inline void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  337. {
  338. }
  339. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  340. {
  341. }
  342. #endif /* CONFIG_RT2800PCI_PCI */
  343. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  344. static const struct rt2x00debug rt2800pci_rt2x00debug = {
  345. .owner = THIS_MODULE,
  346. .csr = {
  347. .read = rt2800_register_read,
  348. .write = rt2800_register_write,
  349. .flags = RT2X00DEBUGFS_OFFSET,
  350. .word_base = CSR_REG_BASE,
  351. .word_size = sizeof(u32),
  352. .word_count = CSR_REG_SIZE / sizeof(u32),
  353. },
  354. .eeprom = {
  355. .read = rt2x00_eeprom_read,
  356. .write = rt2x00_eeprom_write,
  357. .word_base = EEPROM_BASE,
  358. .word_size = sizeof(u16),
  359. .word_count = EEPROM_SIZE / sizeof(u16),
  360. },
  361. .bbp = {
  362. .read = rt2800_bbp_read,
  363. .write = rt2800_bbp_write,
  364. .word_base = BBP_BASE,
  365. .word_size = sizeof(u8),
  366. .word_count = BBP_SIZE / sizeof(u8),
  367. },
  368. .rf = {
  369. .read = rt2x00_rf_read,
  370. .write = rt2800_rf_write,
  371. .word_base = RF_BASE,
  372. .word_size = sizeof(u32),
  373. .word_count = RF_SIZE / sizeof(u32),
  374. },
  375. };
  376. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  377. static int rt2800pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  378. {
  379. u32 reg;
  380. rt2800_register_read(rt2x00dev, GPIO_CTRL_CFG, &reg);
  381. return rt2x00_get_field32(reg, GPIO_CTRL_CFG_BIT2);
  382. }
  383. #ifdef CONFIG_RT2X00_LIB_LEDS
  384. static void rt2800pci_brightness_set(struct led_classdev *led_cdev,
  385. enum led_brightness brightness)
  386. {
  387. struct rt2x00_led *led =
  388. container_of(led_cdev, struct rt2x00_led, led_dev);
  389. unsigned int enabled = brightness != LED_OFF;
  390. unsigned int bg_mode =
  391. (enabled && led->rt2x00dev->curr_band == IEEE80211_BAND_2GHZ);
  392. unsigned int polarity =
  393. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  394. EEPROM_FREQ_LED_POLARITY);
  395. unsigned int ledmode =
  396. rt2x00_get_field16(led->rt2x00dev->led_mcu_reg,
  397. EEPROM_FREQ_LED_MODE);
  398. if (led->type == LED_TYPE_RADIO) {
  399. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  400. enabled ? 0x20 : 0);
  401. } else if (led->type == LED_TYPE_ASSOC) {
  402. rt2800_mcu_request(led->rt2x00dev, MCU_LED, 0xff, ledmode,
  403. enabled ? (bg_mode ? 0x60 : 0xa0) : 0x20);
  404. } else if (led->type == LED_TYPE_QUALITY) {
  405. /*
  406. * The brightness is divided into 6 levels (0 - 5),
  407. * The specs tell us the following levels:
  408. * 0, 1 ,3, 7, 15, 31
  409. * to determine the level in a simple way we can simply
  410. * work with bitshifting:
  411. * (1 << level) - 1
  412. */
  413. rt2800_mcu_request(led->rt2x00dev, MCU_LED_STRENGTH, 0xff,
  414. (1 << brightness / (LED_FULL / 6)) - 1,
  415. polarity);
  416. }
  417. }
  418. static int rt2800pci_blink_set(struct led_classdev *led_cdev,
  419. unsigned long *delay_on,
  420. unsigned long *delay_off)
  421. {
  422. struct rt2x00_led *led =
  423. container_of(led_cdev, struct rt2x00_led, led_dev);
  424. u32 reg;
  425. rt2800_register_read(led->rt2x00dev, LED_CFG, &reg);
  426. rt2x00_set_field32(&reg, LED_CFG_ON_PERIOD, *delay_on);
  427. rt2x00_set_field32(&reg, LED_CFG_OFF_PERIOD, *delay_off);
  428. rt2x00_set_field32(&reg, LED_CFG_SLOW_BLINK_PERIOD, 3);
  429. rt2x00_set_field32(&reg, LED_CFG_R_LED_MODE, 3);
  430. rt2x00_set_field32(&reg, LED_CFG_G_LED_MODE, 12);
  431. rt2x00_set_field32(&reg, LED_CFG_Y_LED_MODE, 3);
  432. rt2x00_set_field32(&reg, LED_CFG_LED_POLAR, 1);
  433. rt2800_register_write(led->rt2x00dev, LED_CFG, reg);
  434. return 0;
  435. }
  436. static void rt2800pci_init_led(struct rt2x00_dev *rt2x00dev,
  437. struct rt2x00_led *led,
  438. enum led_type type)
  439. {
  440. led->rt2x00dev = rt2x00dev;
  441. led->type = type;
  442. led->led_dev.brightness_set = rt2800pci_brightness_set;
  443. led->led_dev.blink_set = rt2800pci_blink_set;
  444. led->flags = LED_INITIALIZED;
  445. }
  446. #endif /* CONFIG_RT2X00_LIB_LEDS */
  447. /*
  448. * Configuration handlers.
  449. */
  450. static void rt2800pci_config_wcid_attr(struct rt2x00_dev *rt2x00dev,
  451. struct rt2x00lib_crypto *crypto,
  452. struct ieee80211_key_conf *key)
  453. {
  454. struct mac_wcid_entry wcid_entry;
  455. struct mac_iveiv_entry iveiv_entry;
  456. u32 offset;
  457. u32 reg;
  458. offset = MAC_WCID_ATTR_ENTRY(key->hw_key_idx);
  459. rt2800_register_read(rt2x00dev, offset, &reg);
  460. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_KEYTAB,
  461. !!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE));
  462. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_CIPHER,
  463. (crypto->cmd == SET_KEY) * crypto->cipher);
  464. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_BSS_IDX,
  465. (crypto->cmd == SET_KEY) * crypto->bssidx);
  466. rt2x00_set_field32(&reg, MAC_WCID_ATTRIBUTE_RX_WIUDF, crypto->cipher);
  467. rt2800_register_write(rt2x00dev, offset, reg);
  468. offset = MAC_IVEIV_ENTRY(key->hw_key_idx);
  469. memset(&iveiv_entry, 0, sizeof(iveiv_entry));
  470. if ((crypto->cipher == CIPHER_TKIP) ||
  471. (crypto->cipher == CIPHER_TKIP_NO_MIC) ||
  472. (crypto->cipher == CIPHER_AES))
  473. iveiv_entry.iv[3] |= 0x20;
  474. iveiv_entry.iv[3] |= key->keyidx << 6;
  475. rt2800_register_multiwrite(rt2x00dev, offset,
  476. &iveiv_entry, sizeof(iveiv_entry));
  477. offset = MAC_WCID_ENTRY(key->hw_key_idx);
  478. memset(&wcid_entry, 0, sizeof(wcid_entry));
  479. if (crypto->cmd == SET_KEY)
  480. memcpy(&wcid_entry, crypto->address, ETH_ALEN);
  481. rt2800_register_multiwrite(rt2x00dev, offset,
  482. &wcid_entry, sizeof(wcid_entry));
  483. }
  484. static int rt2800pci_config_shared_key(struct rt2x00_dev *rt2x00dev,
  485. struct rt2x00lib_crypto *crypto,
  486. struct ieee80211_key_conf *key)
  487. {
  488. struct hw_key_entry key_entry;
  489. struct rt2x00_field32 field;
  490. u32 offset;
  491. u32 reg;
  492. if (crypto->cmd == SET_KEY) {
  493. key->hw_key_idx = (4 * crypto->bssidx) + key->keyidx;
  494. memcpy(key_entry.key, crypto->key,
  495. sizeof(key_entry.key));
  496. memcpy(key_entry.tx_mic, crypto->tx_mic,
  497. sizeof(key_entry.tx_mic));
  498. memcpy(key_entry.rx_mic, crypto->rx_mic,
  499. sizeof(key_entry.rx_mic));
  500. offset = SHARED_KEY_ENTRY(key->hw_key_idx);
  501. rt2800_register_multiwrite(rt2x00dev, offset,
  502. &key_entry, sizeof(key_entry));
  503. }
  504. /*
  505. * The cipher types are stored over multiple registers
  506. * starting with SHARED_KEY_MODE_BASE each word will have
  507. * 32 bits and contains the cipher types for 2 bssidx each.
  508. * Using the correct defines correctly will cause overhead,
  509. * so just calculate the correct offset.
  510. */
  511. field.bit_offset = 4 * (key->hw_key_idx % 8);
  512. field.bit_mask = 0x7 << field.bit_offset;
  513. offset = SHARED_KEY_MODE_ENTRY(key->hw_key_idx / 8);
  514. rt2800_register_read(rt2x00dev, offset, &reg);
  515. rt2x00_set_field32(&reg, field,
  516. (crypto->cmd == SET_KEY) * crypto->cipher);
  517. rt2800_register_write(rt2x00dev, offset, reg);
  518. /*
  519. * Update WCID information
  520. */
  521. rt2800pci_config_wcid_attr(rt2x00dev, crypto, key);
  522. return 0;
  523. }
  524. static int rt2800pci_config_pairwise_key(struct rt2x00_dev *rt2x00dev,
  525. struct rt2x00lib_crypto *crypto,
  526. struct ieee80211_key_conf *key)
  527. {
  528. struct hw_key_entry key_entry;
  529. u32 offset;
  530. if (crypto->cmd == SET_KEY) {
  531. /*
  532. * 1 pairwise key is possible per AID, this means that the AID
  533. * equals our hw_key_idx. Make sure the WCID starts _after_ the
  534. * last possible shared key entry.
  535. */
  536. if (crypto->aid > (256 - 32))
  537. return -ENOSPC;
  538. key->hw_key_idx = 32 + crypto->aid;
  539. memcpy(key_entry.key, crypto->key,
  540. sizeof(key_entry.key));
  541. memcpy(key_entry.tx_mic, crypto->tx_mic,
  542. sizeof(key_entry.tx_mic));
  543. memcpy(key_entry.rx_mic, crypto->rx_mic,
  544. sizeof(key_entry.rx_mic));
  545. offset = PAIRWISE_KEY_ENTRY(key->hw_key_idx);
  546. rt2800_register_multiwrite(rt2x00dev, offset,
  547. &key_entry, sizeof(key_entry));
  548. }
  549. /*
  550. * Update WCID information
  551. */
  552. rt2800pci_config_wcid_attr(rt2x00dev, crypto, key);
  553. return 0;
  554. }
  555. static void rt2800pci_config_filter(struct rt2x00_dev *rt2x00dev,
  556. const unsigned int filter_flags)
  557. {
  558. u32 reg;
  559. /*
  560. * Start configuration steps.
  561. * Note that the version error will always be dropped
  562. * and broadcast frames will always be accepted since
  563. * there is no filter for it at this time.
  564. */
  565. rt2800_register_read(rt2x00dev, RX_FILTER_CFG, &reg);
  566. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CRC_ERROR,
  567. !(filter_flags & FIF_FCSFAIL));
  568. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PHY_ERROR,
  569. !(filter_flags & FIF_PLCPFAIL));
  570. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_TO_ME,
  571. !(filter_flags & FIF_PROMISC_IN_BSS));
  572. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_NOT_MY_BSSD, 0);
  573. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_VER_ERROR, 1);
  574. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_MULTICAST,
  575. !(filter_flags & FIF_ALLMULTI));
  576. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BROADCAST, 0);
  577. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_DUPLICATE, 1);
  578. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END_ACK,
  579. !(filter_flags & FIF_CONTROL));
  580. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CF_END,
  581. !(filter_flags & FIF_CONTROL));
  582. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_ACK,
  583. !(filter_flags & FIF_CONTROL));
  584. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CTS,
  585. !(filter_flags & FIF_CONTROL));
  586. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_RTS,
  587. !(filter_flags & FIF_CONTROL));
  588. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_PSPOLL,
  589. !(filter_flags & FIF_PSPOLL));
  590. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BA, 1);
  591. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_BAR, 0);
  592. rt2x00_set_field32(&reg, RX_FILTER_CFG_DROP_CNTL,
  593. !(filter_flags & FIF_CONTROL));
  594. rt2800_register_write(rt2x00dev, RX_FILTER_CFG, reg);
  595. }
  596. static void rt2800pci_config_intf(struct rt2x00_dev *rt2x00dev,
  597. struct rt2x00_intf *intf,
  598. struct rt2x00intf_conf *conf,
  599. const unsigned int flags)
  600. {
  601. unsigned int beacon_base;
  602. u32 reg;
  603. if (flags & CONFIG_UPDATE_TYPE) {
  604. /*
  605. * Clear current synchronisation setup.
  606. * For the Beacon base registers we only need to clear
  607. * the first byte since that byte contains the VALID and OWNER
  608. * bits which (when set to 0) will invalidate the entire beacon.
  609. */
  610. beacon_base = HW_BEACON_OFFSET(intf->beacon->entry_idx);
  611. rt2800_register_write(rt2x00dev, beacon_base, 0);
  612. /*
  613. * Enable synchronisation.
  614. */
  615. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  616. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  617. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, conf->sync);
  618. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  619. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  620. }
  621. if (flags & CONFIG_UPDATE_MAC) {
  622. reg = le32_to_cpu(conf->mac[1]);
  623. rt2x00_set_field32(&reg, MAC_ADDR_DW1_UNICAST_TO_ME_MASK, 0xff);
  624. conf->mac[1] = cpu_to_le32(reg);
  625. rt2800_register_multiwrite(rt2x00dev, MAC_ADDR_DW0,
  626. conf->mac, sizeof(conf->mac));
  627. }
  628. if (flags & CONFIG_UPDATE_BSSID) {
  629. reg = le32_to_cpu(conf->bssid[1]);
  630. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_ID_MASK, 0);
  631. rt2x00_set_field32(&reg, MAC_BSSID_DW1_BSS_BCN_NUM, 0);
  632. conf->bssid[1] = cpu_to_le32(reg);
  633. rt2800_register_multiwrite(rt2x00dev, MAC_BSSID_DW0,
  634. conf->bssid, sizeof(conf->bssid));
  635. }
  636. }
  637. static void rt2800pci_config_erp(struct rt2x00_dev *rt2x00dev,
  638. struct rt2x00lib_erp *erp)
  639. {
  640. u32 reg;
  641. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  642. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_RX_ACK_TIMEOUT, 0x20);
  643. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  644. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  645. rt2x00_set_field32(&reg, AUTO_RSP_CFG_BAC_ACK_POLICY,
  646. !!erp->short_preamble);
  647. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AR_PREAMBLE,
  648. !!erp->short_preamble);
  649. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  650. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  651. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL,
  652. erp->cts_protection ? 2 : 0);
  653. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  654. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE,
  655. erp->basic_rates);
  656. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  657. rt2800_register_read(rt2x00dev, BKOFF_SLOT_CFG, &reg);
  658. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_SLOT_TIME, erp->slot_time);
  659. rt2x00_set_field32(&reg, BKOFF_SLOT_CFG_CC_DELAY_TIME, 2);
  660. rt2800_register_write(rt2x00dev, BKOFF_SLOT_CFG, reg);
  661. rt2800_register_read(rt2x00dev, XIFS_TIME_CFG, &reg);
  662. rt2x00_set_field32(&reg, XIFS_TIME_CFG_CCKM_SIFS_TIME, erp->sifs);
  663. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_SIFS_TIME, erp->sifs);
  664. rt2x00_set_field32(&reg, XIFS_TIME_CFG_OFDM_XIFS_TIME, 4);
  665. rt2x00_set_field32(&reg, XIFS_TIME_CFG_EIFS, erp->eifs);
  666. rt2x00_set_field32(&reg, XIFS_TIME_CFG_BB_RXEND_ENABLE, 1);
  667. rt2800_register_write(rt2x00dev, XIFS_TIME_CFG, reg);
  668. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  669. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL,
  670. erp->beacon_int * 16);
  671. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  672. }
  673. static void rt2800pci_config_ant(struct rt2x00_dev *rt2x00dev,
  674. struct antenna_setup *ant)
  675. {
  676. u8 r1;
  677. u8 r3;
  678. rt2800_bbp_read(rt2x00dev, 1, &r1);
  679. rt2800_bbp_read(rt2x00dev, 3, &r3);
  680. /*
  681. * Configure the TX antenna.
  682. */
  683. switch ((int)ant->tx) {
  684. case 1:
  685. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 0);
  686. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  687. break;
  688. case 2:
  689. rt2x00_set_field8(&r1, BBP1_TX_ANTENNA, 2);
  690. break;
  691. case 3:
  692. /* Do nothing */
  693. break;
  694. }
  695. /*
  696. * Configure the RX antenna.
  697. */
  698. switch ((int)ant->rx) {
  699. case 1:
  700. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 0);
  701. break;
  702. case 2:
  703. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 1);
  704. break;
  705. case 3:
  706. rt2x00_set_field8(&r3, BBP3_RX_ANTENNA, 2);
  707. break;
  708. }
  709. rt2800_bbp_write(rt2x00dev, 3, r3);
  710. rt2800_bbp_write(rt2x00dev, 1, r1);
  711. }
  712. static void rt2800pci_config_lna_gain(struct rt2x00_dev *rt2x00dev,
  713. struct rt2x00lib_conf *libconf)
  714. {
  715. u16 eeprom;
  716. short lna_gain;
  717. if (libconf->rf.channel <= 14) {
  718. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  719. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_BG);
  720. } else if (libconf->rf.channel <= 64) {
  721. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &eeprom);
  722. lna_gain = rt2x00_get_field16(eeprom, EEPROM_LNA_A0);
  723. } else if (libconf->rf.channel <= 128) {
  724. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &eeprom);
  725. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_BG2_LNA_A1);
  726. } else {
  727. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &eeprom);
  728. lna_gain = rt2x00_get_field16(eeprom, EEPROM_RSSI_A2_LNA_A2);
  729. }
  730. rt2x00dev->lna_gain = lna_gain;
  731. }
  732. static void rt2800pci_config_channel_rt2x(struct rt2x00_dev *rt2x00dev,
  733. struct ieee80211_conf *conf,
  734. struct rf_channel *rf,
  735. struct channel_info *info)
  736. {
  737. rt2x00_set_field32(&rf->rf4, RF4_FREQ_OFFSET, rt2x00dev->freq_offset);
  738. if (rt2x00dev->default_ant.tx == 1)
  739. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_TX1, 1);
  740. if (rt2x00dev->default_ant.rx == 1) {
  741. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX1, 1);
  742. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  743. } else if (rt2x00dev->default_ant.rx == 2)
  744. rt2x00_set_field32(&rf->rf2, RF2_ANTENNA_RX2, 1);
  745. if (rf->channel > 14) {
  746. /*
  747. * When TX power is below 0, we should increase it by 7 to
  748. * make it a positive value (Minumum value is -7).
  749. * However this means that values between 0 and 7 have
  750. * double meaning, and we should set a 7DBm boost flag.
  751. */
  752. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A_7DBM_BOOST,
  753. (info->tx_power1 >= 0));
  754. if (info->tx_power1 < 0)
  755. info->tx_power1 += 7;
  756. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_A,
  757. TXPOWER_A_TO_DEV(info->tx_power1));
  758. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A_7DBM_BOOST,
  759. (info->tx_power2 >= 0));
  760. if (info->tx_power2 < 0)
  761. info->tx_power2 += 7;
  762. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_A,
  763. TXPOWER_A_TO_DEV(info->tx_power2));
  764. } else {
  765. rt2x00_set_field32(&rf->rf3, RF3_TXPOWER_G,
  766. TXPOWER_G_TO_DEV(info->tx_power1));
  767. rt2x00_set_field32(&rf->rf4, RF4_TXPOWER_G,
  768. TXPOWER_G_TO_DEV(info->tx_power2));
  769. }
  770. rt2x00_set_field32(&rf->rf4, RF4_HT40, conf_is_ht40(conf));
  771. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  772. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  773. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  774. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  775. udelay(200);
  776. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  777. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  778. rt2800_rf_write(rt2x00dev, 3, rf->rf3 | 0x00000004);
  779. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  780. udelay(200);
  781. rt2800_rf_write(rt2x00dev, 1, rf->rf1);
  782. rt2800_rf_write(rt2x00dev, 2, rf->rf2);
  783. rt2800_rf_write(rt2x00dev, 3, rf->rf3 & ~0x00000004);
  784. rt2800_rf_write(rt2x00dev, 4, rf->rf4);
  785. }
  786. static void rt2800pci_config_channel_rt3x(struct rt2x00_dev *rt2x00dev,
  787. struct ieee80211_conf *conf,
  788. struct rf_channel *rf,
  789. struct channel_info *info)
  790. {
  791. u8 rfcsr;
  792. rt2800_rfcsr_write(rt2x00dev, 2, rf->rf1);
  793. rt2800_rfcsr_write(rt2x00dev, 2, rf->rf3);
  794. rt2800_rfcsr_read(rt2x00dev, 6, &rfcsr);
  795. rt2x00_set_field8(&rfcsr, RFCSR6_R, rf->rf2);
  796. rt2800_rfcsr_write(rt2x00dev, 6, rfcsr);
  797. rt2800_rfcsr_read(rt2x00dev, 12, &rfcsr);
  798. rt2x00_set_field8(&rfcsr, RFCSR12_TX_POWER,
  799. TXPOWER_G_TO_DEV(info->tx_power1));
  800. rt2800_rfcsr_write(rt2x00dev, 12, rfcsr);
  801. rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
  802. rt2x00_set_field8(&rfcsr, RFCSR23_FREQ_OFFSET, rt2x00dev->freq_offset);
  803. rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
  804. rt2800_rfcsr_write(rt2x00dev, 24,
  805. rt2x00dev->calibration[conf_is_ht40(conf)]);
  806. rt2800_rfcsr_read(rt2x00dev, 23, &rfcsr);
  807. rt2x00_set_field8(&rfcsr, RFCSR7_RF_TUNING, 1);
  808. rt2800_rfcsr_write(rt2x00dev, 23, rfcsr);
  809. }
  810. static void rt2800pci_config_channel(struct rt2x00_dev *rt2x00dev,
  811. struct ieee80211_conf *conf,
  812. struct rf_channel *rf,
  813. struct channel_info *info)
  814. {
  815. u32 reg;
  816. unsigned int tx_pin;
  817. u8 bbp;
  818. if (rt2x00_rev(&rt2x00dev->chip) != RT3070_VERSION)
  819. rt2800pci_config_channel_rt2x(rt2x00dev, conf, rf, info);
  820. else
  821. rt2800pci_config_channel_rt3x(rt2x00dev, conf, rf, info);
  822. /*
  823. * Change BBP settings
  824. */
  825. rt2800_bbp_write(rt2x00dev, 62, 0x37 - rt2x00dev->lna_gain);
  826. rt2800_bbp_write(rt2x00dev, 63, 0x37 - rt2x00dev->lna_gain);
  827. rt2800_bbp_write(rt2x00dev, 64, 0x37 - rt2x00dev->lna_gain);
  828. rt2800_bbp_write(rt2x00dev, 86, 0);
  829. if (rf->channel <= 14) {
  830. if (test_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags)) {
  831. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  832. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  833. } else {
  834. rt2800_bbp_write(rt2x00dev, 82, 0x84);
  835. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  836. }
  837. } else {
  838. rt2800_bbp_write(rt2x00dev, 82, 0xf2);
  839. if (test_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags))
  840. rt2800_bbp_write(rt2x00dev, 75, 0x46);
  841. else
  842. rt2800_bbp_write(rt2x00dev, 75, 0x50);
  843. }
  844. rt2800_register_read(rt2x00dev, TX_BAND_CFG, &reg);
  845. rt2x00_set_field32(&reg, TX_BAND_CFG_HT40_PLUS, conf_is_ht40_plus(conf));
  846. rt2x00_set_field32(&reg, TX_BAND_CFG_A, rf->channel > 14);
  847. rt2x00_set_field32(&reg, TX_BAND_CFG_BG, rf->channel <= 14);
  848. rt2800_register_write(rt2x00dev, TX_BAND_CFG, reg);
  849. tx_pin = 0;
  850. /* Turn on unused PA or LNA when not using 1T or 1R */
  851. if (rt2x00dev->default_ant.tx != 1) {
  852. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A1_EN, 1);
  853. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G1_EN, 1);
  854. }
  855. /* Turn on unused PA or LNA when not using 1T or 1R */
  856. if (rt2x00dev->default_ant.rx != 1) {
  857. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A1_EN, 1);
  858. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G1_EN, 1);
  859. }
  860. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_A0_EN, 1);
  861. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_LNA_PE_G0_EN, 1);
  862. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_RFTR_EN, 1);
  863. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_TRSW_EN, 1);
  864. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_G0_EN, rf->channel <= 14);
  865. rt2x00_set_field32(&tx_pin, TX_PIN_CFG_PA_PE_A0_EN, rf->channel > 14);
  866. rt2800_register_write(rt2x00dev, TX_PIN_CFG, tx_pin);
  867. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  868. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * conf_is_ht40(conf));
  869. rt2800_bbp_write(rt2x00dev, 4, bbp);
  870. rt2800_bbp_read(rt2x00dev, 3, &bbp);
  871. rt2x00_set_field8(&bbp, BBP3_HT40_PLUS, conf_is_ht40_plus(conf));
  872. rt2800_bbp_write(rt2x00dev, 3, bbp);
  873. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
  874. if (conf_is_ht40(conf)) {
  875. rt2800_bbp_write(rt2x00dev, 69, 0x1a);
  876. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  877. rt2800_bbp_write(rt2x00dev, 73, 0x16);
  878. } else {
  879. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  880. rt2800_bbp_write(rt2x00dev, 70, 0x08);
  881. rt2800_bbp_write(rt2x00dev, 73, 0x11);
  882. }
  883. }
  884. msleep(1);
  885. }
  886. static void rt2800pci_config_txpower(struct rt2x00_dev *rt2x00dev,
  887. const int txpower)
  888. {
  889. u32 reg;
  890. u32 value = TXPOWER_G_TO_DEV(txpower);
  891. u8 r1;
  892. rt2800_bbp_read(rt2x00dev, 1, &r1);
  893. rt2x00_set_field8(&reg, BBP1_TX_POWER, 0);
  894. rt2800_bbp_write(rt2x00dev, 1, r1);
  895. rt2800_register_read(rt2x00dev, TX_PWR_CFG_0, &reg);
  896. rt2x00_set_field32(&reg, TX_PWR_CFG_0_1MBS, value);
  897. rt2x00_set_field32(&reg, TX_PWR_CFG_0_2MBS, value);
  898. rt2x00_set_field32(&reg, TX_PWR_CFG_0_55MBS, value);
  899. rt2x00_set_field32(&reg, TX_PWR_CFG_0_11MBS, value);
  900. rt2x00_set_field32(&reg, TX_PWR_CFG_0_6MBS, value);
  901. rt2x00_set_field32(&reg, TX_PWR_CFG_0_9MBS, value);
  902. rt2x00_set_field32(&reg, TX_PWR_CFG_0_12MBS, value);
  903. rt2x00_set_field32(&reg, TX_PWR_CFG_0_18MBS, value);
  904. rt2800_register_write(rt2x00dev, TX_PWR_CFG_0, reg);
  905. rt2800_register_read(rt2x00dev, TX_PWR_CFG_1, &reg);
  906. rt2x00_set_field32(&reg, TX_PWR_CFG_1_24MBS, value);
  907. rt2x00_set_field32(&reg, TX_PWR_CFG_1_36MBS, value);
  908. rt2x00_set_field32(&reg, TX_PWR_CFG_1_48MBS, value);
  909. rt2x00_set_field32(&reg, TX_PWR_CFG_1_54MBS, value);
  910. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS0, value);
  911. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS1, value);
  912. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS2, value);
  913. rt2x00_set_field32(&reg, TX_PWR_CFG_1_MCS3, value);
  914. rt2800_register_write(rt2x00dev, TX_PWR_CFG_1, reg);
  915. rt2800_register_read(rt2x00dev, TX_PWR_CFG_2, &reg);
  916. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS4, value);
  917. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS5, value);
  918. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS6, value);
  919. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS7, value);
  920. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS8, value);
  921. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS9, value);
  922. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS10, value);
  923. rt2x00_set_field32(&reg, TX_PWR_CFG_2_MCS11, value);
  924. rt2800_register_write(rt2x00dev, TX_PWR_CFG_2, reg);
  925. rt2800_register_read(rt2x00dev, TX_PWR_CFG_3, &reg);
  926. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS12, value);
  927. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS13, value);
  928. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS14, value);
  929. rt2x00_set_field32(&reg, TX_PWR_CFG_3_MCS15, value);
  930. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN1, value);
  931. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN2, value);
  932. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN3, value);
  933. rt2x00_set_field32(&reg, TX_PWR_CFG_3_UKNOWN4, value);
  934. rt2800_register_write(rt2x00dev, TX_PWR_CFG_3, reg);
  935. rt2800_register_read(rt2x00dev, TX_PWR_CFG_4, &reg);
  936. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN5, value);
  937. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN6, value);
  938. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN7, value);
  939. rt2x00_set_field32(&reg, TX_PWR_CFG_4_UKNOWN8, value);
  940. rt2800_register_write(rt2x00dev, TX_PWR_CFG_4, reg);
  941. }
  942. static void rt2800pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  943. struct rt2x00lib_conf *libconf)
  944. {
  945. u32 reg;
  946. rt2800_register_read(rt2x00dev, TX_RTY_CFG, &reg);
  947. rt2x00_set_field32(&reg, TX_RTY_CFG_SHORT_RTY_LIMIT,
  948. libconf->conf->short_frame_max_tx_count);
  949. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_LIMIT,
  950. libconf->conf->long_frame_max_tx_count);
  951. rt2x00_set_field32(&reg, TX_RTY_CFG_LONG_RTY_THRE, 2000);
  952. rt2x00_set_field32(&reg, TX_RTY_CFG_NON_AGG_RTY_MODE, 0);
  953. rt2x00_set_field32(&reg, TX_RTY_CFG_AGG_RTY_MODE, 0);
  954. rt2x00_set_field32(&reg, TX_RTY_CFG_TX_AUTO_FB_ENABLE, 1);
  955. rt2800_register_write(rt2x00dev, TX_RTY_CFG, reg);
  956. }
  957. static void rt2800pci_config_ps(struct rt2x00_dev *rt2x00dev,
  958. struct rt2x00lib_conf *libconf)
  959. {
  960. enum dev_state state =
  961. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  962. STATE_SLEEP : STATE_AWAKE;
  963. u32 reg;
  964. if (state == STATE_SLEEP) {
  965. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0);
  966. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  967. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 5);
  968. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE,
  969. libconf->conf->listen_interval - 1);
  970. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 1);
  971. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  972. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  973. } else {
  974. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  975. rt2800_register_read(rt2x00dev, AUTOWAKEUP_CFG, &reg);
  976. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTO_LEAD_TIME, 0);
  977. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_TBCN_BEFORE_WAKE, 0);
  978. rt2x00_set_field32(&reg, AUTOWAKEUP_CFG_AUTOWAKE, 0);
  979. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, reg);
  980. }
  981. }
  982. static void rt2800pci_config(struct rt2x00_dev *rt2x00dev,
  983. struct rt2x00lib_conf *libconf,
  984. const unsigned int flags)
  985. {
  986. /* Always recalculate LNA gain before changing configuration */
  987. rt2800pci_config_lna_gain(rt2x00dev, libconf);
  988. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  989. rt2800pci_config_channel(rt2x00dev, libconf->conf,
  990. &libconf->rf, &libconf->channel);
  991. if (flags & IEEE80211_CONF_CHANGE_POWER)
  992. rt2800pci_config_txpower(rt2x00dev, libconf->conf->power_level);
  993. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  994. rt2800pci_config_retry_limit(rt2x00dev, libconf);
  995. if (flags & IEEE80211_CONF_CHANGE_PS)
  996. rt2800pci_config_ps(rt2x00dev, libconf);
  997. }
  998. /*
  999. * Link tuning
  1000. */
  1001. static void rt2800pci_link_stats(struct rt2x00_dev *rt2x00dev,
  1002. struct link_qual *qual)
  1003. {
  1004. u32 reg;
  1005. /*
  1006. * Update FCS error count from register.
  1007. */
  1008. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  1009. qual->rx_failed = rt2x00_get_field32(reg, RX_STA_CNT0_CRC_ERR);
  1010. }
  1011. static u8 rt2800pci_get_default_vgc(struct rt2x00_dev *rt2x00dev)
  1012. {
  1013. if (rt2x00dev->curr_band == IEEE80211_BAND_2GHZ)
  1014. return 0x2e + rt2x00dev->lna_gain;
  1015. if (!test_bit(CONFIG_CHANNEL_HT40, &rt2x00dev->flags))
  1016. return 0x32 + (rt2x00dev->lna_gain * 5) / 3;
  1017. else
  1018. return 0x3a + (rt2x00dev->lna_gain * 5) / 3;
  1019. }
  1020. static inline void rt2800pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  1021. struct link_qual *qual, u8 vgc_level)
  1022. {
  1023. if (qual->vgc_level != vgc_level) {
  1024. rt2800_bbp_write(rt2x00dev, 66, vgc_level);
  1025. qual->vgc_level = vgc_level;
  1026. qual->vgc_level_reg = vgc_level;
  1027. }
  1028. }
  1029. static void rt2800pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  1030. struct link_qual *qual)
  1031. {
  1032. rt2800pci_set_vgc(rt2x00dev, qual,
  1033. rt2800pci_get_default_vgc(rt2x00dev));
  1034. }
  1035. static void rt2800pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  1036. struct link_qual *qual, const u32 count)
  1037. {
  1038. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION)
  1039. return;
  1040. /*
  1041. * When RSSI is better then -80 increase VGC level with 0x10
  1042. */
  1043. rt2800pci_set_vgc(rt2x00dev, qual,
  1044. rt2800pci_get_default_vgc(rt2x00dev) +
  1045. ((qual->rssi > -80) * 0x10));
  1046. }
  1047. /*
  1048. * Firmware functions
  1049. */
  1050. static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  1051. {
  1052. return FIRMWARE_RT2860;
  1053. }
  1054. static int rt2800pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  1055. const u8 *data, const size_t len)
  1056. {
  1057. u16 fw_crc;
  1058. u16 crc;
  1059. /*
  1060. * Only support 8kb firmware files.
  1061. */
  1062. if (len != 8192)
  1063. return FW_BAD_LENGTH;
  1064. /*
  1065. * The last 2 bytes in the firmware array are the crc checksum itself,
  1066. * this means that we should never pass those 2 bytes to the crc
  1067. * algorithm.
  1068. */
  1069. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  1070. /*
  1071. * Use the crc ccitt algorithm.
  1072. * This will return the same value as the legacy driver which
  1073. * used bit ordering reversion on the both the firmware bytes
  1074. * before input input as well as on the final output.
  1075. * Obviously using crc ccitt directly is much more efficient.
  1076. */
  1077. crc = crc_ccitt(~0, data, len - 2);
  1078. /*
  1079. * There is a small difference between the crc-itu-t + bitrev and
  1080. * the crc-ccitt crc calculation. In the latter method the 2 bytes
  1081. * will be swapped, use swab16 to convert the crc to the correct
  1082. * value.
  1083. */
  1084. crc = swab16(crc);
  1085. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  1086. }
  1087. static int rt2800pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  1088. const u8 *data, const size_t len)
  1089. {
  1090. unsigned int i;
  1091. u32 reg;
  1092. /*
  1093. * Wait for stable hardware.
  1094. */
  1095. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1096. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  1097. if (reg && reg != ~0)
  1098. break;
  1099. msleep(1);
  1100. }
  1101. if (i == REGISTER_BUSY_COUNT) {
  1102. ERROR(rt2x00dev, "Unstable hardware.\n");
  1103. return -EBUSY;
  1104. }
  1105. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  1106. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
  1107. /*
  1108. * Disable DMA, will be reenabled later when enabling
  1109. * the radio.
  1110. */
  1111. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1112. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1113. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1114. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1115. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1116. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1117. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1118. /*
  1119. * enable Host program ram write selection
  1120. */
  1121. reg = 0;
  1122. rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
  1123. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
  1124. /*
  1125. * Write firmware to device.
  1126. */
  1127. rt2800_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  1128. data, len);
  1129. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
  1130. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
  1131. /*
  1132. * Wait for device to stabilize.
  1133. */
  1134. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1135. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  1136. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  1137. break;
  1138. msleep(1);
  1139. }
  1140. if (i == REGISTER_BUSY_COUNT) {
  1141. ERROR(rt2x00dev, "PBF system register not ready.\n");
  1142. return -EBUSY;
  1143. }
  1144. /*
  1145. * Disable interrupts
  1146. */
  1147. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  1148. /*
  1149. * Initialize BBP R/W access agent
  1150. */
  1151. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  1152. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1153. return 0;
  1154. }
  1155. /*
  1156. * Initialization functions.
  1157. */
  1158. static bool rt2800pci_get_entry_state(struct queue_entry *entry)
  1159. {
  1160. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1161. u32 word;
  1162. if (entry->queue->qid == QID_RX) {
  1163. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1164. return (!rt2x00_get_field32(word, RXD_W1_DMA_DONE));
  1165. } else {
  1166. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1167. return (!rt2x00_get_field32(word, TXD_W1_DMA_DONE));
  1168. }
  1169. }
  1170. static void rt2800pci_clear_entry(struct queue_entry *entry)
  1171. {
  1172. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1173. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1174. u32 word;
  1175. if (entry->queue->qid == QID_RX) {
  1176. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1177. rt2x00_set_field32(&word, RXD_W0_SDP0, skbdesc->skb_dma);
  1178. rt2x00_desc_write(entry_priv->desc, 0, word);
  1179. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1180. rt2x00_set_field32(&word, RXD_W1_DMA_DONE, 0);
  1181. rt2x00_desc_write(entry_priv->desc, 1, word);
  1182. } else {
  1183. rt2x00_desc_read(entry_priv->desc, 1, &word);
  1184. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 1);
  1185. rt2x00_desc_write(entry_priv->desc, 1, word);
  1186. }
  1187. }
  1188. static int rt2800pci_init_queues(struct rt2x00_dev *rt2x00dev)
  1189. {
  1190. struct queue_entry_priv_pci *entry_priv;
  1191. u32 reg;
  1192. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  1193. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  1194. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  1195. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  1196. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  1197. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  1198. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  1199. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  1200. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  1201. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  1202. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  1203. /*
  1204. * Initialize registers.
  1205. */
  1206. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  1207. rt2800_register_write(rt2x00dev, TX_BASE_PTR0, entry_priv->desc_dma);
  1208. rt2800_register_write(rt2x00dev, TX_MAX_CNT0, rt2x00dev->tx[0].limit);
  1209. rt2800_register_write(rt2x00dev, TX_CTX_IDX0, 0);
  1210. rt2800_register_write(rt2x00dev, TX_DTX_IDX0, 0);
  1211. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  1212. rt2800_register_write(rt2x00dev, TX_BASE_PTR1, entry_priv->desc_dma);
  1213. rt2800_register_write(rt2x00dev, TX_MAX_CNT1, rt2x00dev->tx[1].limit);
  1214. rt2800_register_write(rt2x00dev, TX_CTX_IDX1, 0);
  1215. rt2800_register_write(rt2x00dev, TX_DTX_IDX1, 0);
  1216. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  1217. rt2800_register_write(rt2x00dev, TX_BASE_PTR2, entry_priv->desc_dma);
  1218. rt2800_register_write(rt2x00dev, TX_MAX_CNT2, rt2x00dev->tx[2].limit);
  1219. rt2800_register_write(rt2x00dev, TX_CTX_IDX2, 0);
  1220. rt2800_register_write(rt2x00dev, TX_DTX_IDX2, 0);
  1221. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  1222. rt2800_register_write(rt2x00dev, TX_BASE_PTR3, entry_priv->desc_dma);
  1223. rt2800_register_write(rt2x00dev, TX_MAX_CNT3, rt2x00dev->tx[3].limit);
  1224. rt2800_register_write(rt2x00dev, TX_CTX_IDX3, 0);
  1225. rt2800_register_write(rt2x00dev, TX_DTX_IDX3, 0);
  1226. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  1227. rt2800_register_write(rt2x00dev, RX_BASE_PTR, entry_priv->desc_dma);
  1228. rt2800_register_write(rt2x00dev, RX_MAX_CNT, rt2x00dev->rx[0].limit);
  1229. rt2800_register_write(rt2x00dev, RX_CRX_IDX, rt2x00dev->rx[0].limit - 1);
  1230. rt2800_register_write(rt2x00dev, RX_DRX_IDX, 0);
  1231. /*
  1232. * Enable global DMA configuration
  1233. */
  1234. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1235. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1236. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1237. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1238. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1239. rt2800_register_write(rt2x00dev, DELAY_INT_CFG, 0);
  1240. return 0;
  1241. }
  1242. static int rt2800pci_init_registers(struct rt2x00_dev *rt2x00dev)
  1243. {
  1244. u32 reg;
  1245. unsigned int i;
  1246. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  1247. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1248. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_CSR, 1);
  1249. rt2x00_set_field32(&reg, MAC_SYS_CTRL_RESET_BBP, 1);
  1250. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1251. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  1252. rt2800_register_read(rt2x00dev, BCN_OFFSET0, &reg);
  1253. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN0, 0xe0); /* 0x3800 */
  1254. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN1, 0xe8); /* 0x3a00 */
  1255. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN2, 0xf0); /* 0x3c00 */
  1256. rt2x00_set_field32(&reg, BCN_OFFSET0_BCN3, 0xf8); /* 0x3e00 */
  1257. rt2800_register_write(rt2x00dev, BCN_OFFSET0, reg);
  1258. rt2800_register_read(rt2x00dev, BCN_OFFSET1, &reg);
  1259. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN4, 0xc8); /* 0x3200 */
  1260. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN5, 0xd0); /* 0x3400 */
  1261. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN6, 0x77); /* 0x1dc0 */
  1262. rt2x00_set_field32(&reg, BCN_OFFSET1_BCN7, 0x6f); /* 0x1bc0 */
  1263. rt2800_register_write(rt2x00dev, BCN_OFFSET1, reg);
  1264. rt2800_register_write(rt2x00dev, LEGACY_BASIC_RATE, 0x0000013f);
  1265. rt2800_register_write(rt2x00dev, HT_BASIC_RATE, 0x00008003);
  1266. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0x00000000);
  1267. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1268. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_INTERVAL, 0);
  1269. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 0);
  1270. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_SYNC, 0);
  1271. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 0);
  1272. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  1273. rt2x00_set_field32(&reg, BCN_TIME_CFG_TX_TIME_COMPENSATE, 0);
  1274. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1275. rt2800_register_write(rt2x00dev, TX_SW_CFG0, 0x00000000);
  1276. rt2800_register_write(rt2x00dev, TX_SW_CFG1, 0x00080606);
  1277. rt2800_register_read(rt2x00dev, TX_LINK_CFG, &reg);
  1278. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB_LIFETIME, 32);
  1279. rt2x00_set_field32(&reg, TX_LINK_CFG_MFB_ENABLE, 0);
  1280. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_UMFS_ENABLE, 0);
  1281. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_MRQ_EN, 0);
  1282. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_RDG_EN, 0);
  1283. rt2x00_set_field32(&reg, TX_LINK_CFG_TX_CF_ACK_EN, 1);
  1284. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFB, 0);
  1285. rt2x00_set_field32(&reg, TX_LINK_CFG_REMOTE_MFS, 0);
  1286. rt2800_register_write(rt2x00dev, TX_LINK_CFG, reg);
  1287. rt2800_register_read(rt2x00dev, TX_TIMEOUT_CFG, &reg);
  1288. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_MPDU_LIFETIME, 9);
  1289. rt2x00_set_field32(&reg, TX_TIMEOUT_CFG_TX_OP_TIMEOUT, 10);
  1290. rt2800_register_write(rt2x00dev, TX_TIMEOUT_CFG, reg);
  1291. rt2800_register_read(rt2x00dev, MAX_LEN_CFG, &reg);
  1292. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_MPDU, AGGREGATION_SIZE);
  1293. if (rt2x00_rev(&rt2x00dev->chip) >= RT2880E_VERSION &&
  1294. rt2x00_rev(&rt2x00dev->chip) < RT3070_VERSION)
  1295. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 2);
  1296. else
  1297. rt2x00_set_field32(&reg, MAX_LEN_CFG_MAX_PSDU, 1);
  1298. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_PSDU, 0);
  1299. rt2x00_set_field32(&reg, MAX_LEN_CFG_MIN_MPDU, 0);
  1300. rt2800_register_write(rt2x00dev, MAX_LEN_CFG, reg);
  1301. rt2800_register_write(rt2x00dev, PBF_MAX_PCNT, 0x1f3fbf9f);
  1302. rt2800_register_read(rt2x00dev, AUTO_RSP_CFG, &reg);
  1303. rt2x00_set_field32(&reg, AUTO_RSP_CFG_AUTORESPONDER, 1);
  1304. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MMODE, 0);
  1305. rt2x00_set_field32(&reg, AUTO_RSP_CFG_CTS_40_MREF, 0);
  1306. rt2x00_set_field32(&reg, AUTO_RSP_CFG_DUAL_CTS_EN, 0);
  1307. rt2x00_set_field32(&reg, AUTO_RSP_CFG_ACK_CTS_PSM_BIT, 0);
  1308. rt2800_register_write(rt2x00dev, AUTO_RSP_CFG, reg);
  1309. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  1310. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_RATE, 8);
  1311. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_CTRL, 0);
  1312. rt2x00_set_field32(&reg, CCK_PROT_CFG_PROTECT_NAV, 1);
  1313. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1314. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1315. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1316. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1317. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1318. rt2x00_set_field32(&reg, CCK_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1319. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  1320. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  1321. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_RATE, 8);
  1322. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_CTRL, 0);
  1323. rt2x00_set_field32(&reg, OFDM_PROT_CFG_PROTECT_NAV, 1);
  1324. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1325. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1326. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1327. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1328. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1329. rt2x00_set_field32(&reg, OFDM_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1330. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  1331. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  1332. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_RATE, 0x4004);
  1333. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_CTRL, 0);
  1334. rt2x00_set_field32(&reg, MM20_PROT_CFG_PROTECT_NAV, 1);
  1335. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1336. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1337. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1338. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1339. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1340. rt2x00_set_field32(&reg, MM20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1341. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  1342. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  1343. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_RATE, 0x4084);
  1344. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_CTRL, 0);
  1345. rt2x00_set_field32(&reg, MM40_PROT_CFG_PROTECT_NAV, 1);
  1346. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1347. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1348. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1349. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1350. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1351. rt2x00_set_field32(&reg, MM40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1352. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  1353. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  1354. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_RATE, 0x4004);
  1355. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_CTRL, 0);
  1356. rt2x00_set_field32(&reg, GF20_PROT_CFG_PROTECT_NAV, 1);
  1357. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1358. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1359. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1360. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_MM40, 0);
  1361. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1362. rt2x00_set_field32(&reg, GF20_PROT_CFG_TX_OP_ALLOW_GF40, 0);
  1363. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  1364. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  1365. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_RATE, 0x4084);
  1366. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_CTRL, 0);
  1367. rt2x00_set_field32(&reg, GF40_PROT_CFG_PROTECT_NAV, 1);
  1368. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_CCK, 1);
  1369. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_OFDM, 1);
  1370. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM20, 1);
  1371. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_MM40, 1);
  1372. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF20, 1);
  1373. rt2x00_set_field32(&reg, GF40_PROT_CFG_TX_OP_ALLOW_GF40, 1);
  1374. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  1375. rt2800_register_write(rt2x00dev, TXOP_CTRL_CFG, 0x0000583f);
  1376. rt2800_register_write(rt2x00dev, TXOP_HLDR_ET, 0x00000002);
  1377. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  1378. rt2x00_set_field32(&reg, TX_RTS_CFG_AUTO_RTS_RETRY_LIMIT, 32);
  1379. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES,
  1380. IEEE80211_MAX_RTS_THRESHOLD);
  1381. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_FBK_EN, 0);
  1382. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  1383. rt2800_register_write(rt2x00dev, EXP_ACK_TIME, 0x002400ca);
  1384. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000003);
  1385. /*
  1386. * ASIC will keep garbage value after boot, clear encryption keys.
  1387. */
  1388. for (i = 0; i < 4; i++)
  1389. rt2800_register_write(rt2x00dev,
  1390. SHARED_KEY_MODE_ENTRY(i), 0);
  1391. for (i = 0; i < 256; i++) {
  1392. u32 wcid[2] = { 0xffffffff, 0x00ffffff };
  1393. rt2800_register_multiwrite(rt2x00dev, MAC_WCID_ENTRY(i),
  1394. wcid, sizeof(wcid));
  1395. rt2800_register_write(rt2x00dev, MAC_WCID_ATTR_ENTRY(i), 1);
  1396. rt2800_register_write(rt2x00dev, MAC_IVEIV_ENTRY(i), 0);
  1397. }
  1398. /*
  1399. * Clear all beacons
  1400. * For the Beacon base registers we only need to clear
  1401. * the first byte since that byte contains the VALID and OWNER
  1402. * bits which (when set to 0) will invalidate the entire beacon.
  1403. */
  1404. rt2800_register_write(rt2x00dev, HW_BEACON_BASE0, 0);
  1405. rt2800_register_write(rt2x00dev, HW_BEACON_BASE1, 0);
  1406. rt2800_register_write(rt2x00dev, HW_BEACON_BASE2, 0);
  1407. rt2800_register_write(rt2x00dev, HW_BEACON_BASE3, 0);
  1408. rt2800_register_write(rt2x00dev, HW_BEACON_BASE4, 0);
  1409. rt2800_register_write(rt2x00dev, HW_BEACON_BASE5, 0);
  1410. rt2800_register_write(rt2x00dev, HW_BEACON_BASE6, 0);
  1411. rt2800_register_write(rt2x00dev, HW_BEACON_BASE7, 0);
  1412. rt2800_register_read(rt2x00dev, HT_FBK_CFG0, &reg);
  1413. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS0FBK, 0);
  1414. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS1FBK, 0);
  1415. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS2FBK, 1);
  1416. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS3FBK, 2);
  1417. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS4FBK, 3);
  1418. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS5FBK, 4);
  1419. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS6FBK, 5);
  1420. rt2x00_set_field32(&reg, HT_FBK_CFG0_HTMCS7FBK, 6);
  1421. rt2800_register_write(rt2x00dev, HT_FBK_CFG0, reg);
  1422. rt2800_register_read(rt2x00dev, HT_FBK_CFG1, &reg);
  1423. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS8FBK, 8);
  1424. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS9FBK, 8);
  1425. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS10FBK, 9);
  1426. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS11FBK, 10);
  1427. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS12FBK, 11);
  1428. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS13FBK, 12);
  1429. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS14FBK, 13);
  1430. rt2x00_set_field32(&reg, HT_FBK_CFG1_HTMCS15FBK, 14);
  1431. rt2800_register_write(rt2x00dev, HT_FBK_CFG1, reg);
  1432. rt2800_register_read(rt2x00dev, LG_FBK_CFG0, &reg);
  1433. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS0FBK, 8);
  1434. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS1FBK, 8);
  1435. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS2FBK, 9);
  1436. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS3FBK, 10);
  1437. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS4FBK, 11);
  1438. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS5FBK, 12);
  1439. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS6FBK, 13);
  1440. rt2x00_set_field32(&reg, LG_FBK_CFG0_OFDMMCS7FBK, 14);
  1441. rt2800_register_write(rt2x00dev, LG_FBK_CFG0, reg);
  1442. rt2800_register_read(rt2x00dev, LG_FBK_CFG1, &reg);
  1443. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS0FBK, 0);
  1444. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS1FBK, 0);
  1445. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS2FBK, 1);
  1446. rt2x00_set_field32(&reg, LG_FBK_CFG0_CCKMCS3FBK, 2);
  1447. rt2800_register_write(rt2x00dev, LG_FBK_CFG1, reg);
  1448. /*
  1449. * We must clear the error counters.
  1450. * These registers are cleared on read,
  1451. * so we may pass a useless variable to store the value.
  1452. */
  1453. rt2800_register_read(rt2x00dev, RX_STA_CNT0, &reg);
  1454. rt2800_register_read(rt2x00dev, RX_STA_CNT1, &reg);
  1455. rt2800_register_read(rt2x00dev, RX_STA_CNT2, &reg);
  1456. rt2800_register_read(rt2x00dev, TX_STA_CNT0, &reg);
  1457. rt2800_register_read(rt2x00dev, TX_STA_CNT1, &reg);
  1458. rt2800_register_read(rt2x00dev, TX_STA_CNT2, &reg);
  1459. return 0;
  1460. }
  1461. static int rt2800pci_wait_bbp_rf_ready(struct rt2x00_dev *rt2x00dev)
  1462. {
  1463. unsigned int i;
  1464. u32 reg;
  1465. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1466. rt2800_register_read(rt2x00dev, MAC_STATUS_CFG, &reg);
  1467. if (!rt2x00_get_field32(reg, MAC_STATUS_CFG_BBP_RF_BUSY))
  1468. return 0;
  1469. udelay(REGISTER_BUSY_DELAY);
  1470. }
  1471. ERROR(rt2x00dev, "BBP/RF register access failed, aborting.\n");
  1472. return -EACCES;
  1473. }
  1474. static int rt2800pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  1475. {
  1476. unsigned int i;
  1477. u8 value;
  1478. /*
  1479. * BBP was enabled after firmware was loaded,
  1480. * but we need to reactivate it now.
  1481. */
  1482. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  1483. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  1484. msleep(1);
  1485. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1486. rt2800_bbp_read(rt2x00dev, 0, &value);
  1487. if ((value != 0xff) && (value != 0x00))
  1488. return 0;
  1489. udelay(REGISTER_BUSY_DELAY);
  1490. }
  1491. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  1492. return -EACCES;
  1493. }
  1494. static int rt2800pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  1495. {
  1496. unsigned int i;
  1497. u16 eeprom;
  1498. u8 reg_id;
  1499. u8 value;
  1500. if (unlikely(rt2800pci_wait_bbp_rf_ready(rt2x00dev) ||
  1501. rt2800pci_wait_bbp_ready(rt2x00dev)))
  1502. return -EACCES;
  1503. rt2800_bbp_write(rt2x00dev, 65, 0x2c);
  1504. rt2800_bbp_write(rt2x00dev, 66, 0x38);
  1505. rt2800_bbp_write(rt2x00dev, 69, 0x12);
  1506. rt2800_bbp_write(rt2x00dev, 70, 0x0a);
  1507. rt2800_bbp_write(rt2x00dev, 73, 0x10);
  1508. rt2800_bbp_write(rt2x00dev, 81, 0x37);
  1509. rt2800_bbp_write(rt2x00dev, 82, 0x62);
  1510. rt2800_bbp_write(rt2x00dev, 83, 0x6a);
  1511. rt2800_bbp_write(rt2x00dev, 84, 0x99);
  1512. rt2800_bbp_write(rt2x00dev, 86, 0x00);
  1513. rt2800_bbp_write(rt2x00dev, 91, 0x04);
  1514. rt2800_bbp_write(rt2x00dev, 92, 0x00);
  1515. rt2800_bbp_write(rt2x00dev, 103, 0x00);
  1516. rt2800_bbp_write(rt2x00dev, 105, 0x05);
  1517. if (rt2x00_rev(&rt2x00dev->chip) == RT2860C_VERSION) {
  1518. rt2800_bbp_write(rt2x00dev, 69, 0x16);
  1519. rt2800_bbp_write(rt2x00dev, 73, 0x12);
  1520. }
  1521. if (rt2x00_rev(&rt2x00dev->chip) > RT2860D_VERSION)
  1522. rt2800_bbp_write(rt2x00dev, 84, 0x19);
  1523. if (rt2x00_rt(&rt2x00dev->chip, RT3052)) {
  1524. rt2800_bbp_write(rt2x00dev, 31, 0x08);
  1525. rt2800_bbp_write(rt2x00dev, 78, 0x0e);
  1526. rt2800_bbp_write(rt2x00dev, 80, 0x08);
  1527. }
  1528. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  1529. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  1530. if (eeprom != 0xffff && eeprom != 0x0000) {
  1531. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  1532. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  1533. rt2800_bbp_write(rt2x00dev, reg_id, value);
  1534. }
  1535. }
  1536. return 0;
  1537. }
  1538. static u8 rt2800pci_init_rx_filter(struct rt2x00_dev *rt2x00dev,
  1539. bool bw40, u8 rfcsr24, u8 filter_target)
  1540. {
  1541. unsigned int i;
  1542. u8 bbp;
  1543. u8 rfcsr;
  1544. u8 passband;
  1545. u8 stopband;
  1546. u8 overtuned = 0;
  1547. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1548. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1549. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 2 * bw40);
  1550. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1551. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  1552. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 1);
  1553. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  1554. /*
  1555. * Set power & frequency of passband test tone
  1556. */
  1557. rt2800_bbp_write(rt2x00dev, 24, 0);
  1558. for (i = 0; i < 100; i++) {
  1559. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  1560. msleep(1);
  1561. rt2800_bbp_read(rt2x00dev, 55, &passband);
  1562. if (passband)
  1563. break;
  1564. }
  1565. /*
  1566. * Set power & frequency of stopband test tone
  1567. */
  1568. rt2800_bbp_write(rt2x00dev, 24, 0x06);
  1569. for (i = 0; i < 100; i++) {
  1570. rt2800_bbp_write(rt2x00dev, 25, 0x90);
  1571. msleep(1);
  1572. rt2800_bbp_read(rt2x00dev, 55, &stopband);
  1573. if ((passband - stopband) <= filter_target) {
  1574. rfcsr24++;
  1575. overtuned += ((passband - stopband) == filter_target);
  1576. } else
  1577. break;
  1578. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1579. }
  1580. rfcsr24 -= !!overtuned;
  1581. rt2800_rfcsr_write(rt2x00dev, 24, rfcsr24);
  1582. return rfcsr24;
  1583. }
  1584. static int rt2800pci_init_rfcsr(struct rt2x00_dev *rt2x00dev)
  1585. {
  1586. u8 rfcsr;
  1587. u8 bbp;
  1588. if (!rt2x00_rf(&rt2x00dev->chip, RF3020) &&
  1589. !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
  1590. !rt2x00_rf(&rt2x00dev->chip, RF3022))
  1591. return 0;
  1592. /*
  1593. * Init RF calibration.
  1594. */
  1595. rt2800_rfcsr_read(rt2x00dev, 30, &rfcsr);
  1596. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 1);
  1597. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1598. msleep(1);
  1599. rt2x00_set_field8(&rfcsr, RFCSR30_RF_CALIBRATION, 0);
  1600. rt2800_rfcsr_write(rt2x00dev, 30, rfcsr);
  1601. rt2800_rfcsr_write(rt2x00dev, 0, 0x50);
  1602. rt2800_rfcsr_write(rt2x00dev, 1, 0x01);
  1603. rt2800_rfcsr_write(rt2x00dev, 2, 0xf7);
  1604. rt2800_rfcsr_write(rt2x00dev, 3, 0x75);
  1605. rt2800_rfcsr_write(rt2x00dev, 4, 0x40);
  1606. rt2800_rfcsr_write(rt2x00dev, 5, 0x03);
  1607. rt2800_rfcsr_write(rt2x00dev, 6, 0x02);
  1608. rt2800_rfcsr_write(rt2x00dev, 7, 0x50);
  1609. rt2800_rfcsr_write(rt2x00dev, 8, 0x39);
  1610. rt2800_rfcsr_write(rt2x00dev, 9, 0x0f);
  1611. rt2800_rfcsr_write(rt2x00dev, 10, 0x60);
  1612. rt2800_rfcsr_write(rt2x00dev, 11, 0x21);
  1613. rt2800_rfcsr_write(rt2x00dev, 12, 0x75);
  1614. rt2800_rfcsr_write(rt2x00dev, 13, 0x75);
  1615. rt2800_rfcsr_write(rt2x00dev, 14, 0x90);
  1616. rt2800_rfcsr_write(rt2x00dev, 15, 0x58);
  1617. rt2800_rfcsr_write(rt2x00dev, 16, 0xb3);
  1618. rt2800_rfcsr_write(rt2x00dev, 17, 0x92);
  1619. rt2800_rfcsr_write(rt2x00dev, 18, 0x2c);
  1620. rt2800_rfcsr_write(rt2x00dev, 19, 0x02);
  1621. rt2800_rfcsr_write(rt2x00dev, 20, 0xba);
  1622. rt2800_rfcsr_write(rt2x00dev, 21, 0xdb);
  1623. rt2800_rfcsr_write(rt2x00dev, 22, 0x00);
  1624. rt2800_rfcsr_write(rt2x00dev, 23, 0x31);
  1625. rt2800_rfcsr_write(rt2x00dev, 24, 0x08);
  1626. rt2800_rfcsr_write(rt2x00dev, 25, 0x01);
  1627. rt2800_rfcsr_write(rt2x00dev, 26, 0x25);
  1628. rt2800_rfcsr_write(rt2x00dev, 27, 0x23);
  1629. rt2800_rfcsr_write(rt2x00dev, 28, 0x13);
  1630. rt2800_rfcsr_write(rt2x00dev, 29, 0x83);
  1631. /*
  1632. * Set RX Filter calibration for 20MHz and 40MHz
  1633. */
  1634. rt2x00dev->calibration[0] =
  1635. rt2800pci_init_rx_filter(rt2x00dev, false, 0x07, 0x16);
  1636. rt2x00dev->calibration[1] =
  1637. rt2800pci_init_rx_filter(rt2x00dev, true, 0x27, 0x19);
  1638. /*
  1639. * Set back to initial state
  1640. */
  1641. rt2800_bbp_write(rt2x00dev, 24, 0);
  1642. rt2800_rfcsr_read(rt2x00dev, 22, &rfcsr);
  1643. rt2x00_set_field8(&rfcsr, RFCSR22_BASEBAND_LOOPBACK, 0);
  1644. rt2800_rfcsr_write(rt2x00dev, 22, rfcsr);
  1645. /*
  1646. * set BBP back to BW20
  1647. */
  1648. rt2800_bbp_read(rt2x00dev, 4, &bbp);
  1649. rt2x00_set_field8(&bbp, BBP4_BANDWIDTH, 0);
  1650. rt2800_bbp_write(rt2x00dev, 4, bbp);
  1651. return 0;
  1652. }
  1653. /*
  1654. * Device state switch handlers.
  1655. */
  1656. static void rt2800pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  1657. enum dev_state state)
  1658. {
  1659. u32 reg;
  1660. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1661. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
  1662. (state == STATE_RADIO_RX_ON) ||
  1663. (state == STATE_RADIO_RX_ON_LINK));
  1664. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1665. }
  1666. static void rt2800pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  1667. enum dev_state state)
  1668. {
  1669. int mask = (state == STATE_RADIO_IRQ_ON);
  1670. u32 reg;
  1671. /*
  1672. * When interrupts are being enabled, the interrupt registers
  1673. * should clear the register to assure a clean state.
  1674. */
  1675. if (state == STATE_RADIO_IRQ_ON) {
  1676. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  1677. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  1678. }
  1679. rt2800_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  1680. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDELAYINT, mask);
  1681. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDELAYINT, mask);
  1682. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_DONE, mask);
  1683. rt2x00_set_field32(&reg, INT_MASK_CSR_AC0_DMA_DONE, mask);
  1684. rt2x00_set_field32(&reg, INT_MASK_CSR_AC1_DMA_DONE, mask);
  1685. rt2x00_set_field32(&reg, INT_MASK_CSR_AC2_DMA_DONE, mask);
  1686. rt2x00_set_field32(&reg, INT_MASK_CSR_AC3_DMA_DONE, mask);
  1687. rt2x00_set_field32(&reg, INT_MASK_CSR_HCCA_DMA_DONE, mask);
  1688. rt2x00_set_field32(&reg, INT_MASK_CSR_MGMT_DMA_DONE, mask);
  1689. rt2x00_set_field32(&reg, INT_MASK_CSR_MCU_COMMAND, mask);
  1690. rt2x00_set_field32(&reg, INT_MASK_CSR_RXTX_COHERENT, mask);
  1691. rt2x00_set_field32(&reg, INT_MASK_CSR_TBTT, mask);
  1692. rt2x00_set_field32(&reg, INT_MASK_CSR_PRE_TBTT, mask);
  1693. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_FIFO_STATUS, mask);
  1694. rt2x00_set_field32(&reg, INT_MASK_CSR_AUTO_WAKEUP, mask);
  1695. rt2x00_set_field32(&reg, INT_MASK_CSR_GPTIMER, mask);
  1696. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_COHERENT, mask);
  1697. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_COHERENT, mask);
  1698. rt2800_register_write(rt2x00dev, INT_MASK_CSR, reg);
  1699. }
  1700. static int rt2800pci_wait_wpdma_ready(struct rt2x00_dev *rt2x00dev)
  1701. {
  1702. unsigned int i;
  1703. u32 reg;
  1704. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  1705. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1706. if (!rt2x00_get_field32(reg, WPDMA_GLO_CFG_TX_DMA_BUSY) &&
  1707. !rt2x00_get_field32(reg, WPDMA_GLO_CFG_RX_DMA_BUSY))
  1708. return 0;
  1709. msleep(1);
  1710. }
  1711. ERROR(rt2x00dev, "WPDMA TX/RX busy, aborting.\n");
  1712. return -EACCES;
  1713. }
  1714. static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  1715. {
  1716. u32 reg;
  1717. u16 word;
  1718. /*
  1719. * Initialize all registers.
  1720. */
  1721. if (unlikely(rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  1722. rt2800pci_init_queues(rt2x00dev) ||
  1723. rt2800pci_init_registers(rt2x00dev) ||
  1724. rt2800pci_wait_wpdma_ready(rt2x00dev) ||
  1725. rt2800pci_init_bbp(rt2x00dev) ||
  1726. rt2800pci_init_rfcsr(rt2x00dev)))
  1727. return -EIO;
  1728. /*
  1729. * Send signal to firmware during boot time.
  1730. */
  1731. rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
  1732. /*
  1733. * Enable RX.
  1734. */
  1735. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1736. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  1737. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  1738. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1739. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1740. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  1741. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  1742. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  1743. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1744. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1745. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  1746. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  1747. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  1748. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  1749. /*
  1750. * Initialize LED control
  1751. */
  1752. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
  1753. rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
  1754. word & 0xff, (word >> 8) & 0xff);
  1755. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
  1756. rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
  1757. word & 0xff, (word >> 8) & 0xff);
  1758. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
  1759. rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
  1760. word & 0xff, (word >> 8) & 0xff);
  1761. return 0;
  1762. }
  1763. static void rt2800pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  1764. {
  1765. u32 reg;
  1766. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  1767. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  1768. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  1769. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  1770. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  1771. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  1772. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  1773. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
  1774. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
  1775. rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
  1776. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001280);
  1777. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  1778. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  1779. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  1780. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  1781. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  1782. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  1783. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  1784. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  1785. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  1786. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  1787. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  1788. /* Wait for DMA, ignore error */
  1789. rt2800pci_wait_wpdma_ready(rt2x00dev);
  1790. }
  1791. static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
  1792. enum dev_state state)
  1793. {
  1794. /*
  1795. * Always put the device to sleep (even when we intend to wakeup!)
  1796. * if the device is booting and wasn't asleep it will return
  1797. * failure when attempting to wakeup.
  1798. */
  1799. rt2800_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
  1800. if (state == STATE_AWAKE) {
  1801. rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKUP, 0, 0);
  1802. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKUP);
  1803. }
  1804. return 0;
  1805. }
  1806. static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  1807. enum dev_state state)
  1808. {
  1809. int retval = 0;
  1810. switch (state) {
  1811. case STATE_RADIO_ON:
  1812. /*
  1813. * Before the radio can be enabled, the device first has
  1814. * to be woken up. After that it needs a bit of time
  1815. * to be fully awake and then the radio can be enabled.
  1816. */
  1817. rt2800pci_set_state(rt2x00dev, STATE_AWAKE);
  1818. msleep(1);
  1819. retval = rt2800pci_enable_radio(rt2x00dev);
  1820. break;
  1821. case STATE_RADIO_OFF:
  1822. /*
  1823. * After the radio has been disabled, the device should
  1824. * be put to sleep for powersaving.
  1825. */
  1826. rt2800pci_disable_radio(rt2x00dev);
  1827. rt2800pci_set_state(rt2x00dev, STATE_SLEEP);
  1828. break;
  1829. case STATE_RADIO_RX_ON:
  1830. case STATE_RADIO_RX_ON_LINK:
  1831. case STATE_RADIO_RX_OFF:
  1832. case STATE_RADIO_RX_OFF_LINK:
  1833. rt2800pci_toggle_rx(rt2x00dev, state);
  1834. break;
  1835. case STATE_RADIO_IRQ_ON:
  1836. case STATE_RADIO_IRQ_OFF:
  1837. rt2800pci_toggle_irq(rt2x00dev, state);
  1838. break;
  1839. case STATE_DEEP_SLEEP:
  1840. case STATE_SLEEP:
  1841. case STATE_STANDBY:
  1842. case STATE_AWAKE:
  1843. retval = rt2800pci_set_state(rt2x00dev, state);
  1844. break;
  1845. default:
  1846. retval = -ENOTSUPP;
  1847. break;
  1848. }
  1849. if (unlikely(retval))
  1850. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  1851. state, retval);
  1852. return retval;
  1853. }
  1854. /*
  1855. * TX descriptor initialization
  1856. */
  1857. static void rt2800pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  1858. struct sk_buff *skb,
  1859. struct txentry_desc *txdesc)
  1860. {
  1861. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  1862. __le32 *txd = skbdesc->desc;
  1863. __le32 *txwi = (__le32 *)(skb->data - rt2x00dev->hw->extra_tx_headroom);
  1864. u32 word;
  1865. /*
  1866. * Initialize TX Info descriptor
  1867. */
  1868. rt2x00_desc_read(txwi, 0, &word);
  1869. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  1870. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1871. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
  1872. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  1873. rt2x00_set_field32(&word, TXWI_W0_TS,
  1874. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  1875. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  1876. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  1877. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
  1878. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
  1879. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
  1880. rt2x00_set_field32(&word, TXWI_W0_BW,
  1881. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  1882. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  1883. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  1884. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
  1885. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  1886. rt2x00_desc_write(txwi, 0, word);
  1887. rt2x00_desc_read(txwi, 1, &word);
  1888. rt2x00_set_field32(&word, TXWI_W1_ACK,
  1889. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  1890. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  1891. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  1892. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
  1893. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  1894. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  1895. txdesc->key_idx : 0xff);
  1896. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  1897. skb->len - txdesc->l2pad);
  1898. rt2x00_set_field32(&word, TXWI_W1_PACKETID,
  1899. skbdesc->entry->queue->qid + 1);
  1900. rt2x00_desc_write(txwi, 1, word);
  1901. /*
  1902. * Always write 0 to IV/EIV fields, hardware will insert the IV
  1903. * from the IVEIV register when TXD_W3_WIV is set to 0.
  1904. * When TXD_W3_WIV is set to 1 it will use the IV data
  1905. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  1906. * crypto entry in the registers should be used to encrypt the frame.
  1907. */
  1908. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  1909. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  1910. /*
  1911. * The buffers pointed by SD_PTR0/SD_LEN0 and SD_PTR1/SD_LEN1
  1912. * must contains a TXWI structure + 802.11 header + padding + 802.11
  1913. * data. We choose to have SD_PTR0/SD_LEN0 only contains TXWI and
  1914. * SD_PTR1/SD_LEN1 contains 802.11 header + padding + 802.11
  1915. * data. It means that LAST_SEC0 is always 0.
  1916. */
  1917. /*
  1918. * Initialize TX descriptor
  1919. */
  1920. rt2x00_desc_read(txd, 0, &word);
  1921. rt2x00_set_field32(&word, TXD_W0_SD_PTR0, skbdesc->skb_dma);
  1922. rt2x00_desc_write(txd, 0, word);
  1923. rt2x00_desc_read(txd, 1, &word);
  1924. rt2x00_set_field32(&word, TXD_W1_SD_LEN1, skb->len);
  1925. rt2x00_set_field32(&word, TXD_W1_LAST_SEC1,
  1926. !test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  1927. rt2x00_set_field32(&word, TXD_W1_BURST,
  1928. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  1929. rt2x00_set_field32(&word, TXD_W1_SD_LEN0,
  1930. rt2x00dev->hw->extra_tx_headroom);
  1931. rt2x00_set_field32(&word, TXD_W1_LAST_SEC0, 0);
  1932. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 0);
  1933. rt2x00_desc_write(txd, 1, word);
  1934. rt2x00_desc_read(txd, 2, &word);
  1935. rt2x00_set_field32(&word, TXD_W2_SD_PTR1,
  1936. skbdesc->skb_dma + rt2x00dev->hw->extra_tx_headroom);
  1937. rt2x00_desc_write(txd, 2, word);
  1938. rt2x00_desc_read(txd, 3, &word);
  1939. rt2x00_set_field32(&word, TXD_W3_WIV,
  1940. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
  1941. rt2x00_set_field32(&word, TXD_W3_QSEL, 2);
  1942. rt2x00_desc_write(txd, 3, word);
  1943. }
  1944. /*
  1945. * TX data initialization
  1946. */
  1947. static void rt2800pci_write_beacon(struct queue_entry *entry)
  1948. {
  1949. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1950. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  1951. unsigned int beacon_base;
  1952. u32 reg;
  1953. /*
  1954. * Disable beaconing while we are reloading the beacon data,
  1955. * otherwise we might be sending out invalid data.
  1956. */
  1957. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1958. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  1959. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1960. /*
  1961. * Write entire beacon with descriptor to register.
  1962. */
  1963. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  1964. rt2800_register_multiwrite(rt2x00dev,
  1965. beacon_base,
  1966. skbdesc->desc, skbdesc->desc_len);
  1967. rt2800_register_multiwrite(rt2x00dev,
  1968. beacon_base + skbdesc->desc_len,
  1969. entry->skb->data, entry->skb->len);
  1970. /*
  1971. * Clean up beacon skb.
  1972. */
  1973. dev_kfree_skb_any(entry->skb);
  1974. entry->skb = NULL;
  1975. }
  1976. static void rt2800pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  1977. const enum data_queue_qid queue_idx)
  1978. {
  1979. struct data_queue *queue;
  1980. unsigned int idx, qidx = 0;
  1981. u32 reg;
  1982. if (queue_idx == QID_BEACON) {
  1983. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  1984. if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
  1985. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  1986. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  1987. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  1988. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  1989. }
  1990. return;
  1991. }
  1992. if (queue_idx > QID_HCCA && queue_idx != QID_MGMT)
  1993. return;
  1994. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  1995. idx = queue->index[Q_INDEX];
  1996. if (queue_idx == QID_MGMT)
  1997. qidx = 5;
  1998. else
  1999. qidx = queue_idx;
  2000. rt2800_register_write(rt2x00dev, TX_CTX_IDX(qidx), idx);
  2001. }
  2002. static void rt2800pci_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
  2003. const enum data_queue_qid qid)
  2004. {
  2005. u32 reg;
  2006. if (qid == QID_BEACON) {
  2007. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, 0);
  2008. return;
  2009. }
  2010. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  2011. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, (qid == QID_AC_BE));
  2012. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, (qid == QID_AC_BK));
  2013. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, (qid == QID_AC_VI));
  2014. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, (qid == QID_AC_VO));
  2015. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  2016. }
  2017. /*
  2018. * RX control handlers
  2019. */
  2020. static void rt2800pci_fill_rxdone(struct queue_entry *entry,
  2021. struct rxdone_entry_desc *rxdesc)
  2022. {
  2023. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  2024. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  2025. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  2026. __le32 *rxd = entry_priv->desc;
  2027. __le32 *rxwi = (__le32 *)entry->skb->data;
  2028. u32 rxd3;
  2029. u32 rxwi0;
  2030. u32 rxwi1;
  2031. u32 rxwi2;
  2032. u32 rxwi3;
  2033. rt2x00_desc_read(rxd, 3, &rxd3);
  2034. rt2x00_desc_read(rxwi, 0, &rxwi0);
  2035. rt2x00_desc_read(rxwi, 1, &rxwi1);
  2036. rt2x00_desc_read(rxwi, 2, &rxwi2);
  2037. rt2x00_desc_read(rxwi, 3, &rxwi3);
  2038. if (rt2x00_get_field32(rxd3, RXD_W3_CRC_ERROR))
  2039. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  2040. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  2041. /*
  2042. * Unfortunately we don't know the cipher type used during
  2043. * decryption. This prevents us from correct providing
  2044. * correct statistics through debugfs.
  2045. */
  2046. rxdesc->cipher = rt2x00_get_field32(rxwi0, RXWI_W0_UDF);
  2047. rxdesc->cipher_status =
  2048. rt2x00_get_field32(rxd3, RXD_W3_CIPHER_ERROR);
  2049. }
  2050. if (rt2x00_get_field32(rxd3, RXD_W3_DECRYPTED)) {
  2051. /*
  2052. * Hardware has stripped IV/EIV data from 802.11 frame during
  2053. * decryption. Unfortunately the descriptor doesn't contain
  2054. * any fields with the EIV/IV data either, so they can't
  2055. * be restored by rt2x00lib.
  2056. */
  2057. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  2058. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  2059. rxdesc->flags |= RX_FLAG_DECRYPTED;
  2060. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  2061. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  2062. }
  2063. if (rt2x00_get_field32(rxd3, RXD_W3_MY_BSS))
  2064. rxdesc->dev_flags |= RXDONE_MY_BSS;
  2065. if (rt2x00_get_field32(rxd3, RXD_W3_L2PAD)) {
  2066. rxdesc->dev_flags |= RXDONE_L2PAD;
  2067. skbdesc->flags |= SKBDESC_L2_PADDED;
  2068. }
  2069. if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
  2070. rxdesc->flags |= RX_FLAG_SHORT_GI;
  2071. if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
  2072. rxdesc->flags |= RX_FLAG_40MHZ;
  2073. /*
  2074. * Detect RX rate, always use MCS as signal type.
  2075. */
  2076. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  2077. rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
  2078. rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
  2079. /*
  2080. * Mask of 0x8 bit to remove the short preamble flag.
  2081. */
  2082. if (rxdesc->rate_mode == RATE_MODE_CCK)
  2083. rxdesc->signal &= ~0x8;
  2084. rxdesc->rssi =
  2085. (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
  2086. rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
  2087. rxdesc->noise =
  2088. (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
  2089. rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
  2090. rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  2091. /*
  2092. * Set RX IDX in register to inform hardware that we have handled
  2093. * this entry and it is available for reuse again.
  2094. */
  2095. rt2800_register_write(rt2x00dev, RX_CRX_IDX, entry->entry_idx);
  2096. /*
  2097. * Remove TXWI descriptor from start of buffer.
  2098. */
  2099. skb_pull(entry->skb, RXWI_DESC_SIZE);
  2100. skb_trim(entry->skb, rxdesc->size);
  2101. }
  2102. /*
  2103. * Interrupt functions.
  2104. */
  2105. static void rt2800pci_txdone(struct rt2x00_dev *rt2x00dev)
  2106. {
  2107. struct data_queue *queue;
  2108. struct queue_entry *entry;
  2109. struct queue_entry *entry_done;
  2110. struct queue_entry_priv_pci *entry_priv;
  2111. struct txdone_entry_desc txdesc;
  2112. u32 word;
  2113. u32 reg;
  2114. u32 old_reg;
  2115. unsigned int type;
  2116. unsigned int index;
  2117. u16 mcs, real_mcs;
  2118. /*
  2119. * During each loop we will compare the freshly read
  2120. * TX_STA_FIFO register value with the value read from
  2121. * the previous loop. If the 2 values are equal then
  2122. * we should stop processing because the chance it
  2123. * quite big that the device has been unplugged and
  2124. * we risk going into an endless loop.
  2125. */
  2126. old_reg = 0;
  2127. while (1) {
  2128. rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
  2129. if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
  2130. break;
  2131. if (old_reg == reg)
  2132. break;
  2133. old_reg = reg;
  2134. /*
  2135. * Skip this entry when it contains an invalid
  2136. * queue identication number.
  2137. */
  2138. type = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE) - 1;
  2139. if (type >= QID_RX)
  2140. continue;
  2141. queue = rt2x00queue_get_queue(rt2x00dev, type);
  2142. if (unlikely(!queue))
  2143. continue;
  2144. /*
  2145. * Skip this entry when it contains an invalid
  2146. * index number.
  2147. */
  2148. index = rt2x00_get_field32(reg, TX_STA_FIFO_WCID) - 1;
  2149. if (unlikely(index >= queue->limit))
  2150. continue;
  2151. entry = &queue->entries[index];
  2152. entry_priv = entry->priv_data;
  2153. rt2x00_desc_read((__le32 *)entry->skb->data, 0, &word);
  2154. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  2155. while (entry != entry_done) {
  2156. /*
  2157. * Catch up.
  2158. * Just report any entries we missed as failed.
  2159. */
  2160. WARNING(rt2x00dev,
  2161. "TX status report missed for entry %d\n",
  2162. entry_done->entry_idx);
  2163. txdesc.flags = 0;
  2164. __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
  2165. txdesc.retry = 0;
  2166. rt2x00lib_txdone(entry_done, &txdesc);
  2167. entry_done = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  2168. }
  2169. /*
  2170. * Obtain the status about this packet.
  2171. */
  2172. txdesc.flags = 0;
  2173. if (rt2x00_get_field32(reg, TX_STA_FIFO_TX_SUCCESS))
  2174. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  2175. else
  2176. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  2177. /*
  2178. * Ralink has a retry mechanism using a global fallback
  2179. * table. We setup this fallback table to try immediate
  2180. * lower rate for all rates. In the TX_STA_FIFO,
  2181. * the MCS field contains the MCS used for the successfull
  2182. * transmission. If the first transmission succeed,
  2183. * we have mcs == tx_mcs. On the second transmission,
  2184. * we have mcs = tx_mcs - 1. So the number of
  2185. * retry is (tx_mcs - mcs).
  2186. */
  2187. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  2188. real_mcs = rt2x00_get_field32(reg, TX_STA_FIFO_MCS);
  2189. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  2190. txdesc.retry = mcs - min(mcs, real_mcs);
  2191. rt2x00lib_txdone(entry, &txdesc);
  2192. }
  2193. }
  2194. static irqreturn_t rt2800pci_interrupt(int irq, void *dev_instance)
  2195. {
  2196. struct rt2x00_dev *rt2x00dev = dev_instance;
  2197. u32 reg;
  2198. /* Read status and ACK all interrupts */
  2199. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  2200. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  2201. if (!reg)
  2202. return IRQ_NONE;
  2203. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  2204. return IRQ_HANDLED;
  2205. /*
  2206. * 1 - Rx ring done interrupt.
  2207. */
  2208. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RX_DONE))
  2209. rt2x00pci_rxdone(rt2x00dev);
  2210. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TX_FIFO_STATUS))
  2211. rt2800pci_txdone(rt2x00dev);
  2212. return IRQ_HANDLED;
  2213. }
  2214. /*
  2215. * Device probe functions.
  2216. */
  2217. static int rt2800pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  2218. {
  2219. u16 word;
  2220. u8 *mac;
  2221. u8 default_lna_gain;
  2222. /*
  2223. * Read EEPROM into buffer
  2224. */
  2225. switch(rt2x00dev->chip.rt) {
  2226. case RT2880:
  2227. case RT3052:
  2228. rt2800pci_read_eeprom_soc(rt2x00dev);
  2229. break;
  2230. case RT3090:
  2231. rt2800pci_read_eeprom_efuse(rt2x00dev);
  2232. break;
  2233. default:
  2234. rt2800pci_read_eeprom_pci(rt2x00dev);
  2235. break;
  2236. }
  2237. /*
  2238. * Start validation of the data that has been read.
  2239. */
  2240. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  2241. if (!is_valid_ether_addr(mac)) {
  2242. random_ether_addr(mac);
  2243. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  2244. }
  2245. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  2246. if (word == 0xffff) {
  2247. rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
  2248. rt2x00_set_field16(&word, EEPROM_ANTENNA_TXPATH, 1);
  2249. rt2x00_set_field16(&word, EEPROM_ANTENNA_RF_TYPE, RF2820);
  2250. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2251. EEPROM(rt2x00dev, "Antenna: 0x%04x\n", word);
  2252. } else if (rt2x00_rev(&rt2x00dev->chip) < RT2883_VERSION) {
  2253. /*
  2254. * There is a max of 2 RX streams for RT2860 series
  2255. */
  2256. if (rt2x00_get_field16(word, EEPROM_ANTENNA_RXPATH) > 2)
  2257. rt2x00_set_field16(&word, EEPROM_ANTENNA_RXPATH, 2);
  2258. rt2x00_eeprom_write(rt2x00dev, EEPROM_ANTENNA, word);
  2259. }
  2260. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &word);
  2261. if (word == 0xffff) {
  2262. rt2x00_set_field16(&word, EEPROM_NIC_HW_RADIO, 0);
  2263. rt2x00_set_field16(&word, EEPROM_NIC_DYNAMIC_TX_AGC, 0);
  2264. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_BG, 0);
  2265. rt2x00_set_field16(&word, EEPROM_NIC_EXTERNAL_LNA_A, 0);
  2266. rt2x00_set_field16(&word, EEPROM_NIC_CARDBUS_ACCEL, 0);
  2267. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_BG, 0);
  2268. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_SB_A, 0);
  2269. rt2x00_set_field16(&word, EEPROM_NIC_WPS_PBC, 0);
  2270. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_BG, 0);
  2271. rt2x00_set_field16(&word, EEPROM_NIC_BW40M_A, 0);
  2272. rt2x00_eeprom_write(rt2x00dev, EEPROM_NIC, word);
  2273. EEPROM(rt2x00dev, "NIC: 0x%04x\n", word);
  2274. }
  2275. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &word);
  2276. if ((word & 0x00ff) == 0x00ff) {
  2277. rt2x00_set_field16(&word, EEPROM_FREQ_OFFSET, 0);
  2278. rt2x00_set_field16(&word, EEPROM_FREQ_LED_MODE,
  2279. LED_MODE_TXRX_ACTIVITY);
  2280. rt2x00_set_field16(&word, EEPROM_FREQ_LED_POLARITY, 0);
  2281. rt2x00_eeprom_write(rt2x00dev, EEPROM_FREQ, word);
  2282. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED1, 0x5555);
  2283. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED2, 0x2221);
  2284. rt2x00_eeprom_write(rt2x00dev, EEPROM_LED3, 0xa9f8);
  2285. EEPROM(rt2x00dev, "Freq: 0x%04x\n", word);
  2286. }
  2287. /*
  2288. * During the LNA validation we are going to use
  2289. * lna0 as correct value. Note that EEPROM_LNA
  2290. * is never validated.
  2291. */
  2292. rt2x00_eeprom_read(rt2x00dev, EEPROM_LNA, &word);
  2293. default_lna_gain = rt2x00_get_field16(word, EEPROM_LNA_A0);
  2294. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG, &word);
  2295. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET0)) > 10)
  2296. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET0, 0);
  2297. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG_OFFSET1)) > 10)
  2298. rt2x00_set_field16(&word, EEPROM_RSSI_BG_OFFSET1, 0);
  2299. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG, word);
  2300. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_BG2, &word);
  2301. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_BG2_OFFSET2)) > 10)
  2302. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_OFFSET2, 0);
  2303. if (rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0x00 ||
  2304. rt2x00_get_field16(word, EEPROM_RSSI_BG2_LNA_A1) == 0xff)
  2305. rt2x00_set_field16(&word, EEPROM_RSSI_BG2_LNA_A1,
  2306. default_lna_gain);
  2307. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_BG2, word);
  2308. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A, &word);
  2309. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET0)) > 10)
  2310. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET0, 0);
  2311. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A_OFFSET1)) > 10)
  2312. rt2x00_set_field16(&word, EEPROM_RSSI_A_OFFSET1, 0);
  2313. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A, word);
  2314. rt2x00_eeprom_read(rt2x00dev, EEPROM_RSSI_A2, &word);
  2315. if (abs(rt2x00_get_field16(word, EEPROM_RSSI_A2_OFFSET2)) > 10)
  2316. rt2x00_set_field16(&word, EEPROM_RSSI_A2_OFFSET2, 0);
  2317. if (rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0x00 ||
  2318. rt2x00_get_field16(word, EEPROM_RSSI_A2_LNA_A2) == 0xff)
  2319. rt2x00_set_field16(&word, EEPROM_RSSI_A2_LNA_A2,
  2320. default_lna_gain);
  2321. rt2x00_eeprom_write(rt2x00dev, EEPROM_RSSI_A2, word);
  2322. return 0;
  2323. }
  2324. static int rt2800pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  2325. {
  2326. u32 reg;
  2327. u16 value;
  2328. u16 eeprom;
  2329. /*
  2330. * Read EEPROM word for configuration.
  2331. */
  2332. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2333. /*
  2334. * Identify RF chipset.
  2335. */
  2336. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  2337. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  2338. rt2x00_set_chip_rf(rt2x00dev, value, reg);
  2339. if (!rt2x00_rf(&rt2x00dev->chip, RF2820) &&
  2340. !rt2x00_rf(&rt2x00dev->chip, RF2850) &&
  2341. !rt2x00_rf(&rt2x00dev->chip, RF2720) &&
  2342. !rt2x00_rf(&rt2x00dev->chip, RF2750) &&
  2343. !rt2x00_rf(&rt2x00dev->chip, RF3020) &&
  2344. !rt2x00_rf(&rt2x00dev->chip, RF2020) &&
  2345. !rt2x00_rf(&rt2x00dev->chip, RF3021) &&
  2346. !rt2x00_rf(&rt2x00dev->chip, RF3022)) {
  2347. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  2348. return -ENODEV;
  2349. }
  2350. /*
  2351. * Identify default antenna configuration.
  2352. */
  2353. rt2x00dev->default_ant.tx =
  2354. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH);
  2355. rt2x00dev->default_ant.rx =
  2356. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH);
  2357. /*
  2358. * Read frequency offset and RF programming sequence.
  2359. */
  2360. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &eeprom);
  2361. rt2x00dev->freq_offset = rt2x00_get_field16(eeprom, EEPROM_FREQ_OFFSET);
  2362. /*
  2363. * Read external LNA informations.
  2364. */
  2365. rt2x00_eeprom_read(rt2x00dev, EEPROM_NIC, &eeprom);
  2366. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_A))
  2367. __set_bit(CONFIG_EXTERNAL_LNA_A, &rt2x00dev->flags);
  2368. if (rt2x00_get_field16(eeprom, EEPROM_NIC_EXTERNAL_LNA_BG))
  2369. __set_bit(CONFIG_EXTERNAL_LNA_BG, &rt2x00dev->flags);
  2370. /*
  2371. * Detect if this device has an hardware controlled radio.
  2372. */
  2373. if (rt2x00_get_field16(eeprom, EEPROM_NIC_HW_RADIO))
  2374. __set_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags);
  2375. /*
  2376. * Store led settings, for correct led behaviour.
  2377. */
  2378. #ifdef CONFIG_RT2X00_LIB_LEDS
  2379. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  2380. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_assoc, LED_TYPE_ASSOC);
  2381. rt2800pci_init_led(rt2x00dev, &rt2x00dev->led_qual, LED_TYPE_QUALITY);
  2382. rt2x00_eeprom_read(rt2x00dev, EEPROM_FREQ, &rt2x00dev->led_mcu_reg);
  2383. #endif /* CONFIG_RT2X00_LIB_LEDS */
  2384. return 0;
  2385. }
  2386. /*
  2387. * RF value list for rt2860
  2388. * Supports: 2.4 GHz (all) & 5.2 GHz (RF2850 & RF2750)
  2389. */
  2390. static const struct rf_channel rf_vals[] = {
  2391. { 1, 0x18402ecc, 0x184c0786, 0x1816b455, 0x1800510b },
  2392. { 2, 0x18402ecc, 0x184c0786, 0x18168a55, 0x1800519f },
  2393. { 3, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800518b },
  2394. { 4, 0x18402ecc, 0x184c078a, 0x18168a55, 0x1800519f },
  2395. { 5, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800518b },
  2396. { 6, 0x18402ecc, 0x184c078e, 0x18168a55, 0x1800519f },
  2397. { 7, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800518b },
  2398. { 8, 0x18402ecc, 0x184c0792, 0x18168a55, 0x1800519f },
  2399. { 9, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800518b },
  2400. { 10, 0x18402ecc, 0x184c0796, 0x18168a55, 0x1800519f },
  2401. { 11, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800518b },
  2402. { 12, 0x18402ecc, 0x184c079a, 0x18168a55, 0x1800519f },
  2403. { 13, 0x18402ecc, 0x184c079e, 0x18168a55, 0x1800518b },
  2404. { 14, 0x18402ecc, 0x184c07a2, 0x18168a55, 0x18005193 },
  2405. /* 802.11 UNI / HyperLan 2 */
  2406. { 36, 0x18402ecc, 0x184c099a, 0x18158a55, 0x180ed1a3 },
  2407. { 38, 0x18402ecc, 0x184c099e, 0x18158a55, 0x180ed193 },
  2408. { 40, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed183 },
  2409. { 44, 0x18402ec8, 0x184c0682, 0x18158a55, 0x180ed1a3 },
  2410. { 46, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed18b },
  2411. { 48, 0x18402ec8, 0x184c0686, 0x18158a55, 0x180ed19b },
  2412. { 52, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed193 },
  2413. { 54, 0x18402ec8, 0x184c068a, 0x18158a55, 0x180ed1a3 },
  2414. { 56, 0x18402ec8, 0x184c068e, 0x18158a55, 0x180ed18b },
  2415. { 60, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed183 },
  2416. { 62, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed193 },
  2417. { 64, 0x18402ec8, 0x184c0692, 0x18158a55, 0x180ed1a3 },
  2418. /* 802.11 HyperLan 2 */
  2419. { 100, 0x18402ec8, 0x184c06b2, 0x18178a55, 0x180ed783 },
  2420. { 102, 0x18402ec8, 0x184c06b2, 0x18578a55, 0x180ed793 },
  2421. { 104, 0x18402ec8, 0x185c06b2, 0x18578a55, 0x180ed1a3 },
  2422. { 108, 0x18402ecc, 0x185c0a32, 0x18578a55, 0x180ed193 },
  2423. { 110, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed183 },
  2424. { 112, 0x18402ecc, 0x184c0a36, 0x18178a55, 0x180ed19b },
  2425. { 116, 0x18402ecc, 0x184c0a3a, 0x18178a55, 0x180ed1a3 },
  2426. { 118, 0x18402ecc, 0x184c0a3e, 0x18178a55, 0x180ed193 },
  2427. { 120, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed183 },
  2428. { 124, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed193 },
  2429. { 126, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed15b },
  2430. { 128, 0x18402ec4, 0x184c0382, 0x18178a55, 0x180ed1a3 },
  2431. { 132, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed18b },
  2432. { 134, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed193 },
  2433. { 136, 0x18402ec4, 0x184c0386, 0x18178a55, 0x180ed19b },
  2434. { 140, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed183 },
  2435. /* 802.11 UNII */
  2436. { 149, 0x18402ec4, 0x184c038a, 0x18178a55, 0x180ed1a7 },
  2437. { 151, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed187 },
  2438. { 153, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed18f },
  2439. { 157, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed19f },
  2440. { 159, 0x18402ec4, 0x184c038e, 0x18178a55, 0x180ed1a7 },
  2441. { 161, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed187 },
  2442. { 165, 0x18402ec4, 0x184c0392, 0x18178a55, 0x180ed197 },
  2443. /* 802.11 Japan */
  2444. { 184, 0x15002ccc, 0x1500491e, 0x1509be55, 0x150c0a0b },
  2445. { 188, 0x15002ccc, 0x15004922, 0x1509be55, 0x150c0a13 },
  2446. { 192, 0x15002ccc, 0x15004926, 0x1509be55, 0x150c0a1b },
  2447. { 196, 0x15002ccc, 0x1500492a, 0x1509be55, 0x150c0a23 },
  2448. { 208, 0x15002ccc, 0x1500493a, 0x1509be55, 0x150c0a13 },
  2449. { 212, 0x15002ccc, 0x1500493e, 0x1509be55, 0x150c0a1b },
  2450. { 216, 0x15002ccc, 0x15004982, 0x1509be55, 0x150c0a23 },
  2451. };
  2452. static int rt2800pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  2453. {
  2454. struct hw_mode_spec *spec = &rt2x00dev->spec;
  2455. struct channel_info *info;
  2456. char *tx_power1;
  2457. char *tx_power2;
  2458. unsigned int i;
  2459. u16 eeprom;
  2460. /*
  2461. * Initialize all hw fields.
  2462. */
  2463. rt2x00dev->hw->flags =
  2464. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2465. IEEE80211_HW_SIGNAL_DBM |
  2466. IEEE80211_HW_SUPPORTS_PS |
  2467. IEEE80211_HW_PS_NULLFUNC_STACK;
  2468. rt2x00dev->hw->extra_tx_headroom = TXWI_DESC_SIZE;
  2469. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  2470. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  2471. rt2x00_eeprom_addr(rt2x00dev,
  2472. EEPROM_MAC_ADDR_0));
  2473. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  2474. /*
  2475. * Initialize hw_mode information.
  2476. */
  2477. spec->supported_bands = SUPPORT_BAND_2GHZ;
  2478. spec->supported_rates = SUPPORT_RATE_CCK | SUPPORT_RATE_OFDM;
  2479. if (rt2x00_rf(&rt2x00dev->chip, RF2820) ||
  2480. rt2x00_rf(&rt2x00dev->chip, RF2720) ||
  2481. rt2x00_rf(&rt2x00dev->chip, RF3020) ||
  2482. rt2x00_rf(&rt2x00dev->chip, RF3021) ||
  2483. rt2x00_rf(&rt2x00dev->chip, RF3022) ||
  2484. rt2x00_rf(&rt2x00dev->chip, RF2020) ||
  2485. rt2x00_rf(&rt2x00dev->chip, RF3052)) {
  2486. spec->num_channels = 14;
  2487. spec->channels = rf_vals;
  2488. } else if (rt2x00_rf(&rt2x00dev->chip, RF2850) ||
  2489. rt2x00_rf(&rt2x00dev->chip, RF2750)) {
  2490. spec->supported_bands |= SUPPORT_BAND_5GHZ;
  2491. spec->num_channels = ARRAY_SIZE(rf_vals);
  2492. spec->channels = rf_vals;
  2493. }
  2494. /*
  2495. * Initialize HT information.
  2496. */
  2497. spec->ht.ht_supported = true;
  2498. spec->ht.cap =
  2499. IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  2500. IEEE80211_HT_CAP_GRN_FLD |
  2501. IEEE80211_HT_CAP_SGI_20 |
  2502. IEEE80211_HT_CAP_SGI_40 |
  2503. IEEE80211_HT_CAP_TX_STBC |
  2504. IEEE80211_HT_CAP_RX_STBC |
  2505. IEEE80211_HT_CAP_PSMP_SUPPORT;
  2506. spec->ht.ampdu_factor = 3;
  2507. spec->ht.ampdu_density = 4;
  2508. spec->ht.mcs.tx_params =
  2509. IEEE80211_HT_MCS_TX_DEFINED |
  2510. IEEE80211_HT_MCS_TX_RX_DIFF |
  2511. ((rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TXPATH) - 1) <<
  2512. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  2513. switch (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RXPATH)) {
  2514. case 3:
  2515. spec->ht.mcs.rx_mask[2] = 0xff;
  2516. case 2:
  2517. spec->ht.mcs.rx_mask[1] = 0xff;
  2518. case 1:
  2519. spec->ht.mcs.rx_mask[0] = 0xff;
  2520. spec->ht.mcs.rx_mask[4] = 0x1; /* MCS32 */
  2521. break;
  2522. }
  2523. /*
  2524. * Create channel information array
  2525. */
  2526. info = kzalloc(spec->num_channels * sizeof(*info), GFP_KERNEL);
  2527. if (!info)
  2528. return -ENOMEM;
  2529. spec->channels_info = info;
  2530. tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG1);
  2531. tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_BG2);
  2532. for (i = 0; i < 14; i++) {
  2533. info[i].tx_power1 = TXPOWER_G_FROM_DEV(tx_power1[i]);
  2534. info[i].tx_power2 = TXPOWER_G_FROM_DEV(tx_power2[i]);
  2535. }
  2536. if (spec->num_channels > 14) {
  2537. tx_power1 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A1);
  2538. tx_power2 = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_A2);
  2539. for (i = 14; i < spec->num_channels; i++) {
  2540. info[i].tx_power1 = TXPOWER_A_FROM_DEV(tx_power1[i]);
  2541. info[i].tx_power2 = TXPOWER_A_FROM_DEV(tx_power2[i]);
  2542. }
  2543. }
  2544. return 0;
  2545. }
  2546. static const struct rt2800_ops rt2800pci_rt2800_ops = {
  2547. .register_read = rt2x00pci_register_read,
  2548. .register_write = rt2x00pci_register_write,
  2549. .register_write_lock = rt2x00pci_register_write, /* same for PCI */
  2550. .register_multiread = rt2x00pci_register_multiread,
  2551. .register_multiwrite = rt2x00pci_register_multiwrite,
  2552. .regbusy_read = rt2x00pci_regbusy_read,
  2553. };
  2554. static int rt2800pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  2555. {
  2556. int retval;
  2557. rt2x00dev->priv = (void *)&rt2800pci_rt2800_ops;
  2558. /*
  2559. * Allocate eeprom data.
  2560. */
  2561. retval = rt2800pci_validate_eeprom(rt2x00dev);
  2562. if (retval)
  2563. return retval;
  2564. retval = rt2800pci_init_eeprom(rt2x00dev);
  2565. if (retval)
  2566. return retval;
  2567. /*
  2568. * Initialize hw specifications.
  2569. */
  2570. retval = rt2800pci_probe_hw_mode(rt2x00dev);
  2571. if (retval)
  2572. return retval;
  2573. /*
  2574. * This device has multiple filters for control frames
  2575. * and has a separate filter for PS Poll frames.
  2576. */
  2577. __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
  2578. __set_bit(DRIVER_SUPPORT_CONTROL_FILTER_PSPOLL, &rt2x00dev->flags);
  2579. /*
  2580. * This device requires firmware.
  2581. */
  2582. if (!rt2x00_rt(&rt2x00dev->chip, RT2880) &&
  2583. !rt2x00_rt(&rt2x00dev->chip, RT3052))
  2584. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  2585. __set_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags);
  2586. __set_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags);
  2587. if (!modparam_nohwcrypt)
  2588. __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
  2589. /*
  2590. * Set the rssi offset.
  2591. */
  2592. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  2593. return 0;
  2594. }
  2595. /*
  2596. * IEEE80211 stack callback functions.
  2597. */
  2598. static void rt2800pci_get_tkip_seq(struct ieee80211_hw *hw, u8 hw_key_idx,
  2599. u32 *iv32, u16 *iv16)
  2600. {
  2601. struct rt2x00_dev *rt2x00dev = hw->priv;
  2602. struct mac_iveiv_entry iveiv_entry;
  2603. u32 offset;
  2604. offset = MAC_IVEIV_ENTRY(hw_key_idx);
  2605. rt2800_register_multiread(rt2x00dev, offset,
  2606. &iveiv_entry, sizeof(iveiv_entry));
  2607. memcpy(&iveiv_entry.iv[0], iv16, sizeof(iv16));
  2608. memcpy(&iveiv_entry.iv[4], iv32, sizeof(iv32));
  2609. }
  2610. static int rt2800pci_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2611. {
  2612. struct rt2x00_dev *rt2x00dev = hw->priv;
  2613. u32 reg;
  2614. bool enabled = (value < IEEE80211_MAX_RTS_THRESHOLD);
  2615. rt2800_register_read(rt2x00dev, TX_RTS_CFG, &reg);
  2616. rt2x00_set_field32(&reg, TX_RTS_CFG_RTS_THRES, value);
  2617. rt2800_register_write(rt2x00dev, TX_RTS_CFG, reg);
  2618. rt2800_register_read(rt2x00dev, CCK_PROT_CFG, &reg);
  2619. rt2x00_set_field32(&reg, CCK_PROT_CFG_RTS_TH_EN, enabled);
  2620. rt2800_register_write(rt2x00dev, CCK_PROT_CFG, reg);
  2621. rt2800_register_read(rt2x00dev, OFDM_PROT_CFG, &reg);
  2622. rt2x00_set_field32(&reg, OFDM_PROT_CFG_RTS_TH_EN, enabled);
  2623. rt2800_register_write(rt2x00dev, OFDM_PROT_CFG, reg);
  2624. rt2800_register_read(rt2x00dev, MM20_PROT_CFG, &reg);
  2625. rt2x00_set_field32(&reg, MM20_PROT_CFG_RTS_TH_EN, enabled);
  2626. rt2800_register_write(rt2x00dev, MM20_PROT_CFG, reg);
  2627. rt2800_register_read(rt2x00dev, MM40_PROT_CFG, &reg);
  2628. rt2x00_set_field32(&reg, MM40_PROT_CFG_RTS_TH_EN, enabled);
  2629. rt2800_register_write(rt2x00dev, MM40_PROT_CFG, reg);
  2630. rt2800_register_read(rt2x00dev, GF20_PROT_CFG, &reg);
  2631. rt2x00_set_field32(&reg, GF20_PROT_CFG_RTS_TH_EN, enabled);
  2632. rt2800_register_write(rt2x00dev, GF20_PROT_CFG, reg);
  2633. rt2800_register_read(rt2x00dev, GF40_PROT_CFG, &reg);
  2634. rt2x00_set_field32(&reg, GF40_PROT_CFG_RTS_TH_EN, enabled);
  2635. rt2800_register_write(rt2x00dev, GF40_PROT_CFG, reg);
  2636. return 0;
  2637. }
  2638. static int rt2800pci_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  2639. const struct ieee80211_tx_queue_params *params)
  2640. {
  2641. struct rt2x00_dev *rt2x00dev = hw->priv;
  2642. struct data_queue *queue;
  2643. struct rt2x00_field32 field;
  2644. int retval;
  2645. u32 reg;
  2646. u32 offset;
  2647. /*
  2648. * First pass the configuration through rt2x00lib, that will
  2649. * update the queue settings and validate the input. After that
  2650. * we are free to update the registers based on the value
  2651. * in the queue parameter.
  2652. */
  2653. retval = rt2x00mac_conf_tx(hw, queue_idx, params);
  2654. if (retval)
  2655. return retval;
  2656. /*
  2657. * We only need to perform additional register initialization
  2658. * for WMM queues/
  2659. */
  2660. if (queue_idx >= 4)
  2661. return 0;
  2662. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  2663. /* Update WMM TXOP register */
  2664. offset = WMM_TXOP0_CFG + (sizeof(u32) * (!!(queue_idx & 2)));
  2665. field.bit_offset = (queue_idx & 1) * 16;
  2666. field.bit_mask = 0xffff << field.bit_offset;
  2667. rt2800_register_read(rt2x00dev, offset, &reg);
  2668. rt2x00_set_field32(&reg, field, queue->txop);
  2669. rt2800_register_write(rt2x00dev, offset, reg);
  2670. /* Update WMM registers */
  2671. field.bit_offset = queue_idx * 4;
  2672. field.bit_mask = 0xf << field.bit_offset;
  2673. rt2800_register_read(rt2x00dev, WMM_AIFSN_CFG, &reg);
  2674. rt2x00_set_field32(&reg, field, queue->aifs);
  2675. rt2800_register_write(rt2x00dev, WMM_AIFSN_CFG, reg);
  2676. rt2800_register_read(rt2x00dev, WMM_CWMIN_CFG, &reg);
  2677. rt2x00_set_field32(&reg, field, queue->cw_min);
  2678. rt2800_register_write(rt2x00dev, WMM_CWMIN_CFG, reg);
  2679. rt2800_register_read(rt2x00dev, WMM_CWMAX_CFG, &reg);
  2680. rt2x00_set_field32(&reg, field, queue->cw_max);
  2681. rt2800_register_write(rt2x00dev, WMM_CWMAX_CFG, reg);
  2682. /* Update EDCA registers */
  2683. offset = EDCA_AC0_CFG + (sizeof(u32) * queue_idx);
  2684. rt2800_register_read(rt2x00dev, offset, &reg);
  2685. rt2x00_set_field32(&reg, EDCA_AC0_CFG_TX_OP, queue->txop);
  2686. rt2x00_set_field32(&reg, EDCA_AC0_CFG_AIFSN, queue->aifs);
  2687. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMIN, queue->cw_min);
  2688. rt2x00_set_field32(&reg, EDCA_AC0_CFG_CWMAX, queue->cw_max);
  2689. rt2800_register_write(rt2x00dev, offset, reg);
  2690. return 0;
  2691. }
  2692. static u64 rt2800pci_get_tsf(struct ieee80211_hw *hw)
  2693. {
  2694. struct rt2x00_dev *rt2x00dev = hw->priv;
  2695. u64 tsf;
  2696. u32 reg;
  2697. rt2800_register_read(rt2x00dev, TSF_TIMER_DW1, &reg);
  2698. tsf = (u64) rt2x00_get_field32(reg, TSF_TIMER_DW1_HIGH_WORD) << 32;
  2699. rt2800_register_read(rt2x00dev, TSF_TIMER_DW0, &reg);
  2700. tsf |= rt2x00_get_field32(reg, TSF_TIMER_DW0_LOW_WORD);
  2701. return tsf;
  2702. }
  2703. static const struct ieee80211_ops rt2800pci_mac80211_ops = {
  2704. .tx = rt2x00mac_tx,
  2705. .start = rt2x00mac_start,
  2706. .stop = rt2x00mac_stop,
  2707. .add_interface = rt2x00mac_add_interface,
  2708. .remove_interface = rt2x00mac_remove_interface,
  2709. .config = rt2x00mac_config,
  2710. .configure_filter = rt2x00mac_configure_filter,
  2711. .set_key = rt2x00mac_set_key,
  2712. .get_stats = rt2x00mac_get_stats,
  2713. .get_tkip_seq = rt2800pci_get_tkip_seq,
  2714. .set_rts_threshold = rt2800pci_set_rts_threshold,
  2715. .bss_info_changed = rt2x00mac_bss_info_changed,
  2716. .conf_tx = rt2800pci_conf_tx,
  2717. .get_tx_stats = rt2x00mac_get_tx_stats,
  2718. .get_tsf = rt2800pci_get_tsf,
  2719. .rfkill_poll = rt2x00mac_rfkill_poll,
  2720. };
  2721. static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
  2722. .irq_handler = rt2800pci_interrupt,
  2723. .probe_hw = rt2800pci_probe_hw,
  2724. .get_firmware_name = rt2800pci_get_firmware_name,
  2725. .check_firmware = rt2800pci_check_firmware,
  2726. .load_firmware = rt2800pci_load_firmware,
  2727. .initialize = rt2x00pci_initialize,
  2728. .uninitialize = rt2x00pci_uninitialize,
  2729. .get_entry_state = rt2800pci_get_entry_state,
  2730. .clear_entry = rt2800pci_clear_entry,
  2731. .set_device_state = rt2800pci_set_device_state,
  2732. .rfkill_poll = rt2800pci_rfkill_poll,
  2733. .link_stats = rt2800pci_link_stats,
  2734. .reset_tuner = rt2800pci_reset_tuner,
  2735. .link_tuner = rt2800pci_link_tuner,
  2736. .write_tx_desc = rt2800pci_write_tx_desc,
  2737. .write_tx_data = rt2x00pci_write_tx_data,
  2738. .write_beacon = rt2800pci_write_beacon,
  2739. .kick_tx_queue = rt2800pci_kick_tx_queue,
  2740. .kill_tx_queue = rt2800pci_kill_tx_queue,
  2741. .fill_rxdone = rt2800pci_fill_rxdone,
  2742. .config_shared_key = rt2800pci_config_shared_key,
  2743. .config_pairwise_key = rt2800pci_config_pairwise_key,
  2744. .config_filter = rt2800pci_config_filter,
  2745. .config_intf = rt2800pci_config_intf,
  2746. .config_erp = rt2800pci_config_erp,
  2747. .config_ant = rt2800pci_config_ant,
  2748. .config = rt2800pci_config,
  2749. };
  2750. static const struct data_queue_desc rt2800pci_queue_rx = {
  2751. .entry_num = RX_ENTRIES,
  2752. .data_size = AGGREGATION_SIZE,
  2753. .desc_size = RXD_DESC_SIZE,
  2754. .priv_size = sizeof(struct queue_entry_priv_pci),
  2755. };
  2756. static const struct data_queue_desc rt2800pci_queue_tx = {
  2757. .entry_num = TX_ENTRIES,
  2758. .data_size = AGGREGATION_SIZE,
  2759. .desc_size = TXD_DESC_SIZE,
  2760. .priv_size = sizeof(struct queue_entry_priv_pci),
  2761. };
  2762. static const struct data_queue_desc rt2800pci_queue_bcn = {
  2763. .entry_num = 8 * BEACON_ENTRIES,
  2764. .data_size = 0, /* No DMA required for beacons */
  2765. .desc_size = TXWI_DESC_SIZE,
  2766. .priv_size = sizeof(struct queue_entry_priv_pci),
  2767. };
  2768. static const struct rt2x00_ops rt2800pci_ops = {
  2769. .name = KBUILD_MODNAME,
  2770. .max_sta_intf = 1,
  2771. .max_ap_intf = 8,
  2772. .eeprom_size = EEPROM_SIZE,
  2773. .rf_size = RF_SIZE,
  2774. .tx_queues = NUM_TX_QUEUES,
  2775. .rx = &rt2800pci_queue_rx,
  2776. .tx = &rt2800pci_queue_tx,
  2777. .bcn = &rt2800pci_queue_bcn,
  2778. .lib = &rt2800pci_rt2x00_ops,
  2779. .hw = &rt2800pci_mac80211_ops,
  2780. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  2781. .debugfs = &rt2800pci_rt2x00debug,
  2782. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  2783. };
  2784. /*
  2785. * RT2800pci module information.
  2786. */
  2787. static struct pci_device_id rt2800pci_device_table[] = {
  2788. { PCI_DEVICE(0x1462, 0x891a), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2789. { PCI_DEVICE(0x1432, 0x7708), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2790. { PCI_DEVICE(0x1432, 0x7727), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2791. { PCI_DEVICE(0x1432, 0x7728), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2792. { PCI_DEVICE(0x1432, 0x7738), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2793. { PCI_DEVICE(0x1432, 0x7748), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2794. { PCI_DEVICE(0x1432, 0x7758), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2795. { PCI_DEVICE(0x1432, 0x7768), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2796. { PCI_DEVICE(0x1814, 0x0601), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2797. { PCI_DEVICE(0x1814, 0x0681), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2798. { PCI_DEVICE(0x1814, 0x0701), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2799. { PCI_DEVICE(0x1814, 0x0781), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2800. { PCI_DEVICE(0x1814, 0x3060), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2801. { PCI_DEVICE(0x1814, 0x3062), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2802. { PCI_DEVICE(0x1814, 0x3090), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2803. { PCI_DEVICE(0x1814, 0x3091), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2804. { PCI_DEVICE(0x1814, 0x3092), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2805. { PCI_DEVICE(0x1814, 0x3562), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2806. { PCI_DEVICE(0x1814, 0x3592), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2807. { PCI_DEVICE(0x1a3b, 0x1059), PCI_DEVICE_DATA(&rt2800pci_ops) },
  2808. { 0, }
  2809. };
  2810. MODULE_AUTHOR(DRV_PROJECT);
  2811. MODULE_VERSION(DRV_VERSION);
  2812. MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
  2813. MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
  2814. #ifdef CONFIG_RT2800PCI_PCI
  2815. MODULE_FIRMWARE(FIRMWARE_RT2860);
  2816. MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
  2817. #endif /* CONFIG_RT2800PCI_PCI */
  2818. MODULE_LICENSE("GPL");
  2819. #ifdef CONFIG_RT2800PCI_WISOC
  2820. #if defined(CONFIG_RALINK_RT288X)
  2821. __rt2x00soc_probe(RT2880, &rt2800pci_ops);
  2822. #elif defined(CONFIG_RALINK_RT305X)
  2823. __rt2x00soc_probe(RT3052, &rt2800pci_ops);
  2824. #endif
  2825. static struct platform_driver rt2800soc_driver = {
  2826. .driver = {
  2827. .name = "rt2800_wmac",
  2828. .owner = THIS_MODULE,
  2829. .mod_name = KBUILD_MODNAME,
  2830. },
  2831. .probe = __rt2x00soc_probe,
  2832. .remove = __devexit_p(rt2x00soc_remove),
  2833. .suspend = rt2x00soc_suspend,
  2834. .resume = rt2x00soc_resume,
  2835. };
  2836. #endif /* CONFIG_RT2800PCI_WISOC */
  2837. #ifdef CONFIG_RT2800PCI_PCI
  2838. static struct pci_driver rt2800pci_driver = {
  2839. .name = KBUILD_MODNAME,
  2840. .id_table = rt2800pci_device_table,
  2841. .probe = rt2x00pci_probe,
  2842. .remove = __devexit_p(rt2x00pci_remove),
  2843. .suspend = rt2x00pci_suspend,
  2844. .resume = rt2x00pci_resume,
  2845. };
  2846. #endif /* CONFIG_RT2800PCI_PCI */
  2847. static int __init rt2800pci_init(void)
  2848. {
  2849. int ret = 0;
  2850. #ifdef CONFIG_RT2800PCI_WISOC
  2851. ret = platform_driver_register(&rt2800soc_driver);
  2852. if (ret)
  2853. return ret;
  2854. #endif
  2855. #ifdef CONFIG_RT2800PCI_PCI
  2856. ret = pci_register_driver(&rt2800pci_driver);
  2857. if (ret) {
  2858. #ifdef CONFIG_RT2800PCI_WISOC
  2859. platform_driver_unregister(&rt2800soc_driver);
  2860. #endif
  2861. return ret;
  2862. }
  2863. #endif
  2864. return ret;
  2865. }
  2866. static void __exit rt2800pci_exit(void)
  2867. {
  2868. #ifdef CONFIG_RT2800PCI_PCI
  2869. pci_unregister_driver(&rt2800pci_driver);
  2870. #endif
  2871. #ifdef CONFIG_RT2800PCI_WISOC
  2872. platform_driver_unregister(&rt2800soc_driver);
  2873. #endif
  2874. }
  2875. module_init(rt2800pci_init);
  2876. module_exit(rt2800pci_exit);