rt61pci.c 94 KB

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