rt61pci.c 75 KB

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