rt2400pci.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2400pci
  19. Abstract: rt2400pci device specific routines.
  20. Supported chipsets: RT2460.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/eeprom_93cx6.h>
  29. #include <linux/slab.h>
  30. #include "rt2x00.h"
  31. #include "rt2x00pci.h"
  32. #include "rt2400pci.h"
  33. /*
  34. * Register access.
  35. * All access to the CSR registers will go through the methods
  36. * rt2x00pci_register_read and rt2x00pci_register_write.
  37. * BBP and RF register require indirect register access,
  38. * and use the CSR registers BBPCSR and RFCSR to achieve this.
  39. * These indirect registers work with busy bits,
  40. * and we will try maximal REGISTER_BUSY_COUNT times to access
  41. * the register while taking a REGISTER_BUSY_DELAY us delay
  42. * between each attempt. When the busy bit is still set at that time,
  43. * the access attempt is considered to have failed,
  44. * and we will print an error.
  45. */
  46. #define WAIT_FOR_BBP(__dev, __reg) \
  47. rt2x00pci_regbusy_read((__dev), BBPCSR, BBPCSR_BUSY, (__reg))
  48. #define WAIT_FOR_RF(__dev, __reg) \
  49. rt2x00pci_regbusy_read((__dev), RFCSR, RFCSR_BUSY, (__reg))
  50. static void rt2400pci_bbp_write(struct rt2x00_dev *rt2x00dev,
  51. const unsigned int word, const u8 value)
  52. {
  53. u32 reg;
  54. mutex_lock(&rt2x00dev->csr_mutex);
  55. /*
  56. * Wait until the BBP becomes available, afterwards we
  57. * can safely write the new data into the register.
  58. */
  59. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  60. reg = 0;
  61. rt2x00_set_field32(&reg, BBPCSR_VALUE, value);
  62. rt2x00_set_field32(&reg, BBPCSR_REGNUM, word);
  63. rt2x00_set_field32(&reg, BBPCSR_BUSY, 1);
  64. rt2x00_set_field32(&reg, BBPCSR_WRITE_CONTROL, 1);
  65. rt2x00pci_register_write(rt2x00dev, BBPCSR, reg);
  66. }
  67. mutex_unlock(&rt2x00dev->csr_mutex);
  68. }
  69. static void rt2400pci_bbp_read(struct rt2x00_dev *rt2x00dev,
  70. const unsigned int word, u8 *value)
  71. {
  72. u32 reg;
  73. mutex_lock(&rt2x00dev->csr_mutex);
  74. /*
  75. * Wait until the BBP becomes available, afterwards we
  76. * can safely write the read request into the register.
  77. * After the data has been written, we wait until hardware
  78. * returns the correct value, if at any time the register
  79. * doesn't become available in time, reg will be 0xffffffff
  80. * which means we return 0xff to the caller.
  81. */
  82. if (WAIT_FOR_BBP(rt2x00dev, &reg)) {
  83. reg = 0;
  84. rt2x00_set_field32(&reg, BBPCSR_REGNUM, word);
  85. rt2x00_set_field32(&reg, BBPCSR_BUSY, 1);
  86. rt2x00_set_field32(&reg, BBPCSR_WRITE_CONTROL, 0);
  87. rt2x00pci_register_write(rt2x00dev, BBPCSR, reg);
  88. WAIT_FOR_BBP(rt2x00dev, &reg);
  89. }
  90. *value = rt2x00_get_field32(reg, BBPCSR_VALUE);
  91. mutex_unlock(&rt2x00dev->csr_mutex);
  92. }
  93. static void rt2400pci_rf_write(struct rt2x00_dev *rt2x00dev,
  94. const unsigned int word, const u32 value)
  95. {
  96. u32 reg;
  97. mutex_lock(&rt2x00dev->csr_mutex);
  98. /*
  99. * Wait until the RF becomes available, afterwards we
  100. * can safely write the new data into the register.
  101. */
  102. if (WAIT_FOR_RF(rt2x00dev, &reg)) {
  103. reg = 0;
  104. rt2x00_set_field32(&reg, RFCSR_VALUE, value);
  105. rt2x00_set_field32(&reg, RFCSR_NUMBER_OF_BITS, 20);
  106. rt2x00_set_field32(&reg, RFCSR_IF_SELECT, 0);
  107. rt2x00_set_field32(&reg, RFCSR_BUSY, 1);
  108. rt2x00pci_register_write(rt2x00dev, RFCSR, reg);
  109. rt2x00_rf_write(rt2x00dev, word, value);
  110. }
  111. mutex_unlock(&rt2x00dev->csr_mutex);
  112. }
  113. static void rt2400pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  114. {
  115. struct rt2x00_dev *rt2x00dev = eeprom->data;
  116. u32 reg;
  117. rt2x00pci_register_read(rt2x00dev, CSR21, &reg);
  118. eeprom->reg_data_in = !!rt2x00_get_field32(reg, CSR21_EEPROM_DATA_IN);
  119. eeprom->reg_data_out = !!rt2x00_get_field32(reg, CSR21_EEPROM_DATA_OUT);
  120. eeprom->reg_data_clock =
  121. !!rt2x00_get_field32(reg, CSR21_EEPROM_DATA_CLOCK);
  122. eeprom->reg_chip_select =
  123. !!rt2x00_get_field32(reg, CSR21_EEPROM_CHIP_SELECT);
  124. }
  125. static void rt2400pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  126. {
  127. struct rt2x00_dev *rt2x00dev = eeprom->data;
  128. u32 reg = 0;
  129. rt2x00_set_field32(&reg, CSR21_EEPROM_DATA_IN, !!eeprom->reg_data_in);
  130. rt2x00_set_field32(&reg, CSR21_EEPROM_DATA_OUT, !!eeprom->reg_data_out);
  131. rt2x00_set_field32(&reg, CSR21_EEPROM_DATA_CLOCK,
  132. !!eeprom->reg_data_clock);
  133. rt2x00_set_field32(&reg, CSR21_EEPROM_CHIP_SELECT,
  134. !!eeprom->reg_chip_select);
  135. rt2x00pci_register_write(rt2x00dev, CSR21, reg);
  136. }
  137. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  138. static const struct rt2x00debug rt2400pci_rt2x00debug = {
  139. .owner = THIS_MODULE,
  140. .csr = {
  141. .read = rt2x00pci_register_read,
  142. .write = rt2x00pci_register_write,
  143. .flags = RT2X00DEBUGFS_OFFSET,
  144. .word_base = CSR_REG_BASE,
  145. .word_size = sizeof(u32),
  146. .word_count = CSR_REG_SIZE / sizeof(u32),
  147. },
  148. .eeprom = {
  149. .read = rt2x00_eeprom_read,
  150. .write = rt2x00_eeprom_write,
  151. .word_base = EEPROM_BASE,
  152. .word_size = sizeof(u16),
  153. .word_count = EEPROM_SIZE / sizeof(u16),
  154. },
  155. .bbp = {
  156. .read = rt2400pci_bbp_read,
  157. .write = rt2400pci_bbp_write,
  158. .word_base = BBP_BASE,
  159. .word_size = sizeof(u8),
  160. .word_count = BBP_SIZE / sizeof(u8),
  161. },
  162. .rf = {
  163. .read = rt2x00_rf_read,
  164. .write = rt2400pci_rf_write,
  165. .word_base = RF_BASE,
  166. .word_size = sizeof(u32),
  167. .word_count = RF_SIZE / sizeof(u32),
  168. },
  169. };
  170. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  171. static int rt2400pci_rfkill_poll(struct rt2x00_dev *rt2x00dev)
  172. {
  173. u32 reg;
  174. rt2x00pci_register_read(rt2x00dev, GPIOCSR, &reg);
  175. return rt2x00_get_field32(reg, GPIOCSR_BIT0);
  176. }
  177. #ifdef CONFIG_RT2X00_LIB_LEDS
  178. static void rt2400pci_brightness_set(struct led_classdev *led_cdev,
  179. enum led_brightness brightness)
  180. {
  181. struct rt2x00_led *led =
  182. container_of(led_cdev, struct rt2x00_led, led_dev);
  183. unsigned int enabled = brightness != LED_OFF;
  184. u32 reg;
  185. rt2x00pci_register_read(led->rt2x00dev, LEDCSR, &reg);
  186. if (led->type == LED_TYPE_RADIO || led->type == LED_TYPE_ASSOC)
  187. rt2x00_set_field32(&reg, LEDCSR_LINK, enabled);
  188. else if (led->type == LED_TYPE_ACTIVITY)
  189. rt2x00_set_field32(&reg, LEDCSR_ACTIVITY, enabled);
  190. rt2x00pci_register_write(led->rt2x00dev, LEDCSR, reg);
  191. }
  192. static int rt2400pci_blink_set(struct led_classdev *led_cdev,
  193. unsigned long *delay_on,
  194. unsigned long *delay_off)
  195. {
  196. struct rt2x00_led *led =
  197. container_of(led_cdev, struct rt2x00_led, led_dev);
  198. u32 reg;
  199. rt2x00pci_register_read(led->rt2x00dev, LEDCSR, &reg);
  200. rt2x00_set_field32(&reg, LEDCSR_ON_PERIOD, *delay_on);
  201. rt2x00_set_field32(&reg, LEDCSR_OFF_PERIOD, *delay_off);
  202. rt2x00pci_register_write(led->rt2x00dev, LEDCSR, reg);
  203. return 0;
  204. }
  205. static void rt2400pci_init_led(struct rt2x00_dev *rt2x00dev,
  206. struct rt2x00_led *led,
  207. enum led_type type)
  208. {
  209. led->rt2x00dev = rt2x00dev;
  210. led->type = type;
  211. led->led_dev.brightness_set = rt2400pci_brightness_set;
  212. led->led_dev.blink_set = rt2400pci_blink_set;
  213. led->flags = LED_INITIALIZED;
  214. }
  215. #endif /* CONFIG_RT2X00_LIB_LEDS */
  216. /*
  217. * Configuration handlers.
  218. */
  219. static void rt2400pci_config_filter(struct rt2x00_dev *rt2x00dev,
  220. const unsigned int filter_flags)
  221. {
  222. u32 reg;
  223. /*
  224. * Start configuration steps.
  225. * Note that the version error will always be dropped
  226. * since there is no filter for it at this time.
  227. */
  228. rt2x00pci_register_read(rt2x00dev, RXCSR0, &reg);
  229. rt2x00_set_field32(&reg, RXCSR0_DROP_CRC,
  230. !(filter_flags & FIF_FCSFAIL));
  231. rt2x00_set_field32(&reg, RXCSR0_DROP_PHYSICAL,
  232. !(filter_flags & FIF_PLCPFAIL));
  233. rt2x00_set_field32(&reg, RXCSR0_DROP_CONTROL,
  234. !(filter_flags & FIF_CONTROL));
  235. rt2x00_set_field32(&reg, RXCSR0_DROP_NOT_TO_ME,
  236. !(filter_flags & FIF_PROMISC_IN_BSS));
  237. rt2x00_set_field32(&reg, RXCSR0_DROP_TODS,
  238. !(filter_flags & FIF_PROMISC_IN_BSS) &&
  239. !rt2x00dev->intf_ap_count);
  240. rt2x00_set_field32(&reg, RXCSR0_DROP_VERSION_ERROR, 1);
  241. rt2x00pci_register_write(rt2x00dev, RXCSR0, reg);
  242. }
  243. static void rt2400pci_config_intf(struct rt2x00_dev *rt2x00dev,
  244. struct rt2x00_intf *intf,
  245. struct rt2x00intf_conf *conf,
  246. const unsigned int flags)
  247. {
  248. unsigned int bcn_preload;
  249. u32 reg;
  250. if (flags & CONFIG_UPDATE_TYPE) {
  251. /*
  252. * Enable beacon config
  253. */
  254. bcn_preload = PREAMBLE + GET_DURATION(IEEE80211_HEADER, 20);
  255. rt2x00pci_register_read(rt2x00dev, BCNCSR1, &reg);
  256. rt2x00_set_field32(&reg, BCNCSR1_PRELOAD, bcn_preload);
  257. rt2x00pci_register_write(rt2x00dev, BCNCSR1, reg);
  258. /*
  259. * Enable synchronisation.
  260. */
  261. rt2x00pci_register_read(rt2x00dev, CSR14, &reg);
  262. rt2x00_set_field32(&reg, CSR14_TSF_SYNC, conf->sync);
  263. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  264. }
  265. if (flags & CONFIG_UPDATE_MAC)
  266. rt2x00pci_register_multiwrite(rt2x00dev, CSR3,
  267. conf->mac, sizeof(conf->mac));
  268. if (flags & CONFIG_UPDATE_BSSID)
  269. rt2x00pci_register_multiwrite(rt2x00dev, CSR5,
  270. conf->bssid, sizeof(conf->bssid));
  271. }
  272. static void rt2400pci_config_erp(struct rt2x00_dev *rt2x00dev,
  273. struct rt2x00lib_erp *erp,
  274. u32 changed)
  275. {
  276. int preamble_mask;
  277. u32 reg;
  278. /*
  279. * When short preamble is enabled, we should set bit 0x08
  280. */
  281. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  282. preamble_mask = erp->short_preamble << 3;
  283. rt2x00pci_register_read(rt2x00dev, TXCSR1, &reg);
  284. rt2x00_set_field32(&reg, TXCSR1_ACK_TIMEOUT, 0x1ff);
  285. rt2x00_set_field32(&reg, TXCSR1_ACK_CONSUME_TIME, 0x13a);
  286. rt2x00_set_field32(&reg, TXCSR1_TSF_OFFSET, IEEE80211_HEADER);
  287. rt2x00_set_field32(&reg, TXCSR1_AUTORESPONDER, 1);
  288. rt2x00pci_register_write(rt2x00dev, TXCSR1, reg);
  289. rt2x00pci_register_read(rt2x00dev, ARCSR2, &reg);
  290. rt2x00_set_field32(&reg, ARCSR2_SIGNAL, 0x00);
  291. rt2x00_set_field32(&reg, ARCSR2_SERVICE, 0x04);
  292. rt2x00_set_field32(&reg, ARCSR2_LENGTH,
  293. GET_DURATION(ACK_SIZE, 10));
  294. rt2x00pci_register_write(rt2x00dev, ARCSR2, reg);
  295. rt2x00pci_register_read(rt2x00dev, ARCSR3, &reg);
  296. rt2x00_set_field32(&reg, ARCSR3_SIGNAL, 0x01 | preamble_mask);
  297. rt2x00_set_field32(&reg, ARCSR3_SERVICE, 0x04);
  298. rt2x00_set_field32(&reg, ARCSR2_LENGTH,
  299. GET_DURATION(ACK_SIZE, 20));
  300. rt2x00pci_register_write(rt2x00dev, ARCSR3, reg);
  301. rt2x00pci_register_read(rt2x00dev, ARCSR4, &reg);
  302. rt2x00_set_field32(&reg, ARCSR4_SIGNAL, 0x02 | preamble_mask);
  303. rt2x00_set_field32(&reg, ARCSR4_SERVICE, 0x04);
  304. rt2x00_set_field32(&reg, ARCSR2_LENGTH,
  305. GET_DURATION(ACK_SIZE, 55));
  306. rt2x00pci_register_write(rt2x00dev, ARCSR4, reg);
  307. rt2x00pci_register_read(rt2x00dev, ARCSR5, &reg);
  308. rt2x00_set_field32(&reg, ARCSR5_SIGNAL, 0x03 | preamble_mask);
  309. rt2x00_set_field32(&reg, ARCSR5_SERVICE, 0x84);
  310. rt2x00_set_field32(&reg, ARCSR2_LENGTH,
  311. GET_DURATION(ACK_SIZE, 110));
  312. rt2x00pci_register_write(rt2x00dev, ARCSR5, reg);
  313. }
  314. if (changed & BSS_CHANGED_BASIC_RATES)
  315. rt2x00pci_register_write(rt2x00dev, ARCSR1, erp->basic_rates);
  316. if (changed & BSS_CHANGED_ERP_SLOT) {
  317. rt2x00pci_register_read(rt2x00dev, CSR11, &reg);
  318. rt2x00_set_field32(&reg, CSR11_SLOT_TIME, erp->slot_time);
  319. rt2x00pci_register_write(rt2x00dev, CSR11, reg);
  320. rt2x00pci_register_read(rt2x00dev, CSR18, &reg);
  321. rt2x00_set_field32(&reg, CSR18_SIFS, erp->sifs);
  322. rt2x00_set_field32(&reg, CSR18_PIFS, erp->pifs);
  323. rt2x00pci_register_write(rt2x00dev, CSR18, reg);
  324. rt2x00pci_register_read(rt2x00dev, CSR19, &reg);
  325. rt2x00_set_field32(&reg, CSR19_DIFS, erp->difs);
  326. rt2x00_set_field32(&reg, CSR19_EIFS, erp->eifs);
  327. rt2x00pci_register_write(rt2x00dev, CSR19, reg);
  328. }
  329. if (changed & BSS_CHANGED_BEACON_INT) {
  330. rt2x00pci_register_read(rt2x00dev, CSR12, &reg);
  331. rt2x00_set_field32(&reg, CSR12_BEACON_INTERVAL,
  332. erp->beacon_int * 16);
  333. rt2x00_set_field32(&reg, CSR12_CFP_MAX_DURATION,
  334. erp->beacon_int * 16);
  335. rt2x00pci_register_write(rt2x00dev, CSR12, reg);
  336. }
  337. }
  338. static void rt2400pci_config_ant(struct rt2x00_dev *rt2x00dev,
  339. struct antenna_setup *ant)
  340. {
  341. u8 r1;
  342. u8 r4;
  343. /*
  344. * We should never come here because rt2x00lib is supposed
  345. * to catch this and send us the correct antenna explicitely.
  346. */
  347. BUG_ON(ant->rx == ANTENNA_SW_DIVERSITY ||
  348. ant->tx == ANTENNA_SW_DIVERSITY);
  349. rt2400pci_bbp_read(rt2x00dev, 4, &r4);
  350. rt2400pci_bbp_read(rt2x00dev, 1, &r1);
  351. /*
  352. * Configure the TX antenna.
  353. */
  354. switch (ant->tx) {
  355. case ANTENNA_HW_DIVERSITY:
  356. rt2x00_set_field8(&r1, BBP_R1_TX_ANTENNA, 1);
  357. break;
  358. case ANTENNA_A:
  359. rt2x00_set_field8(&r1, BBP_R1_TX_ANTENNA, 0);
  360. break;
  361. case ANTENNA_B:
  362. default:
  363. rt2x00_set_field8(&r1, BBP_R1_TX_ANTENNA, 2);
  364. break;
  365. }
  366. /*
  367. * Configure the RX antenna.
  368. */
  369. switch (ant->rx) {
  370. case ANTENNA_HW_DIVERSITY:
  371. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA, 1);
  372. break;
  373. case ANTENNA_A:
  374. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA, 0);
  375. break;
  376. case ANTENNA_B:
  377. default:
  378. rt2x00_set_field8(&r4, BBP_R4_RX_ANTENNA, 2);
  379. break;
  380. }
  381. rt2400pci_bbp_write(rt2x00dev, 4, r4);
  382. rt2400pci_bbp_write(rt2x00dev, 1, r1);
  383. }
  384. static void rt2400pci_config_channel(struct rt2x00_dev *rt2x00dev,
  385. struct rf_channel *rf)
  386. {
  387. /*
  388. * Switch on tuning bits.
  389. */
  390. rt2x00_set_field32(&rf->rf1, RF1_TUNER, 1);
  391. rt2x00_set_field32(&rf->rf3, RF3_TUNER, 1);
  392. rt2400pci_rf_write(rt2x00dev, 1, rf->rf1);
  393. rt2400pci_rf_write(rt2x00dev, 2, rf->rf2);
  394. rt2400pci_rf_write(rt2x00dev, 3, rf->rf3);
  395. /*
  396. * RF2420 chipset don't need any additional actions.
  397. */
  398. if (rt2x00_rf(rt2x00dev, RF2420))
  399. return;
  400. /*
  401. * For the RT2421 chipsets we need to write an invalid
  402. * reference clock rate to activate auto_tune.
  403. * After that we set the value back to the correct channel.
  404. */
  405. rt2400pci_rf_write(rt2x00dev, 1, rf->rf1);
  406. rt2400pci_rf_write(rt2x00dev, 2, 0x000c2a32);
  407. rt2400pci_rf_write(rt2x00dev, 3, rf->rf3);
  408. msleep(1);
  409. rt2400pci_rf_write(rt2x00dev, 1, rf->rf1);
  410. rt2400pci_rf_write(rt2x00dev, 2, rf->rf2);
  411. rt2400pci_rf_write(rt2x00dev, 3, rf->rf3);
  412. msleep(1);
  413. /*
  414. * Switch off tuning bits.
  415. */
  416. rt2x00_set_field32(&rf->rf1, RF1_TUNER, 0);
  417. rt2x00_set_field32(&rf->rf3, RF3_TUNER, 0);
  418. rt2400pci_rf_write(rt2x00dev, 1, rf->rf1);
  419. rt2400pci_rf_write(rt2x00dev, 3, rf->rf3);
  420. /*
  421. * Clear false CRC during channel switch.
  422. */
  423. rt2x00pci_register_read(rt2x00dev, CNT0, &rf->rf1);
  424. }
  425. static void rt2400pci_config_txpower(struct rt2x00_dev *rt2x00dev, int txpower)
  426. {
  427. rt2400pci_bbp_write(rt2x00dev, 3, TXPOWER_TO_DEV(txpower));
  428. }
  429. static void rt2400pci_config_retry_limit(struct rt2x00_dev *rt2x00dev,
  430. struct rt2x00lib_conf *libconf)
  431. {
  432. u32 reg;
  433. rt2x00pci_register_read(rt2x00dev, CSR11, &reg);
  434. rt2x00_set_field32(&reg, CSR11_LONG_RETRY,
  435. libconf->conf->long_frame_max_tx_count);
  436. rt2x00_set_field32(&reg, CSR11_SHORT_RETRY,
  437. libconf->conf->short_frame_max_tx_count);
  438. rt2x00pci_register_write(rt2x00dev, CSR11, reg);
  439. }
  440. static void rt2400pci_config_ps(struct rt2x00_dev *rt2x00dev,
  441. struct rt2x00lib_conf *libconf)
  442. {
  443. enum dev_state state =
  444. (libconf->conf->flags & IEEE80211_CONF_PS) ?
  445. STATE_SLEEP : STATE_AWAKE;
  446. u32 reg;
  447. if (state == STATE_SLEEP) {
  448. rt2x00pci_register_read(rt2x00dev, CSR20, &reg);
  449. rt2x00_set_field32(&reg, CSR20_DELAY_AFTER_TBCN,
  450. (rt2x00dev->beacon_int - 20) * 16);
  451. rt2x00_set_field32(&reg, CSR20_TBCN_BEFORE_WAKEUP,
  452. libconf->conf->listen_interval - 1);
  453. /* We must first disable autowake before it can be enabled */
  454. rt2x00_set_field32(&reg, CSR20_AUTOWAKE, 0);
  455. rt2x00pci_register_write(rt2x00dev, CSR20, reg);
  456. rt2x00_set_field32(&reg, CSR20_AUTOWAKE, 1);
  457. rt2x00pci_register_write(rt2x00dev, CSR20, reg);
  458. } else {
  459. rt2x00pci_register_read(rt2x00dev, CSR20, &reg);
  460. rt2x00_set_field32(&reg, CSR20_AUTOWAKE, 0);
  461. rt2x00pci_register_write(rt2x00dev, CSR20, reg);
  462. }
  463. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  464. }
  465. static void rt2400pci_config(struct rt2x00_dev *rt2x00dev,
  466. struct rt2x00lib_conf *libconf,
  467. const unsigned int flags)
  468. {
  469. if (flags & IEEE80211_CONF_CHANGE_CHANNEL)
  470. rt2400pci_config_channel(rt2x00dev, &libconf->rf);
  471. if (flags & IEEE80211_CONF_CHANGE_POWER)
  472. rt2400pci_config_txpower(rt2x00dev,
  473. libconf->conf->power_level);
  474. if (flags & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
  475. rt2400pci_config_retry_limit(rt2x00dev, libconf);
  476. if (flags & IEEE80211_CONF_CHANGE_PS)
  477. rt2400pci_config_ps(rt2x00dev, libconf);
  478. }
  479. static void rt2400pci_config_cw(struct rt2x00_dev *rt2x00dev,
  480. const int cw_min, const int cw_max)
  481. {
  482. u32 reg;
  483. rt2x00pci_register_read(rt2x00dev, CSR11, &reg);
  484. rt2x00_set_field32(&reg, CSR11_CWMIN, cw_min);
  485. rt2x00_set_field32(&reg, CSR11_CWMAX, cw_max);
  486. rt2x00pci_register_write(rt2x00dev, CSR11, reg);
  487. }
  488. /*
  489. * Link tuning
  490. */
  491. static void rt2400pci_link_stats(struct rt2x00_dev *rt2x00dev,
  492. struct link_qual *qual)
  493. {
  494. u32 reg;
  495. u8 bbp;
  496. /*
  497. * Update FCS error count from register.
  498. */
  499. rt2x00pci_register_read(rt2x00dev, CNT0, &reg);
  500. qual->rx_failed = rt2x00_get_field32(reg, CNT0_FCS_ERROR);
  501. /*
  502. * Update False CCA count from register.
  503. */
  504. rt2400pci_bbp_read(rt2x00dev, 39, &bbp);
  505. qual->false_cca = bbp;
  506. }
  507. static inline void rt2400pci_set_vgc(struct rt2x00_dev *rt2x00dev,
  508. struct link_qual *qual, u8 vgc_level)
  509. {
  510. if (qual->vgc_level_reg != vgc_level) {
  511. rt2400pci_bbp_write(rt2x00dev, 13, vgc_level);
  512. qual->vgc_level = vgc_level;
  513. qual->vgc_level_reg = vgc_level;
  514. }
  515. }
  516. static void rt2400pci_reset_tuner(struct rt2x00_dev *rt2x00dev,
  517. struct link_qual *qual)
  518. {
  519. rt2400pci_set_vgc(rt2x00dev, qual, 0x08);
  520. }
  521. static void rt2400pci_link_tuner(struct rt2x00_dev *rt2x00dev,
  522. struct link_qual *qual, const u32 count)
  523. {
  524. /*
  525. * The link tuner should not run longer then 60 seconds,
  526. * and should run once every 2 seconds.
  527. */
  528. if (count > 60 || !(count & 1))
  529. return;
  530. /*
  531. * Base r13 link tuning on the false cca count.
  532. */
  533. if ((qual->false_cca > 512) && (qual->vgc_level < 0x20))
  534. rt2400pci_set_vgc(rt2x00dev, qual, ++qual->vgc_level);
  535. else if ((qual->false_cca < 100) && (qual->vgc_level > 0x08))
  536. rt2400pci_set_vgc(rt2x00dev, qual, --qual->vgc_level);
  537. }
  538. /*
  539. * Queue handlers.
  540. */
  541. static void rt2400pci_start_queue(struct data_queue *queue)
  542. {
  543. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  544. u32 reg;
  545. switch (queue->qid) {
  546. case QID_RX:
  547. rt2x00pci_register_read(rt2x00dev, RXCSR0, &reg);
  548. rt2x00_set_field32(&reg, RXCSR0_DISABLE_RX, 0);
  549. rt2x00pci_register_write(rt2x00dev, RXCSR0, reg);
  550. break;
  551. case QID_BEACON:
  552. /*
  553. * Allow the tbtt tasklet to be scheduled.
  554. */
  555. tasklet_enable(&rt2x00dev->tbtt_tasklet);
  556. rt2x00pci_register_read(rt2x00dev, CSR14, &reg);
  557. rt2x00_set_field32(&reg, CSR14_TSF_COUNT, 1);
  558. rt2x00_set_field32(&reg, CSR14_TBCN, 1);
  559. rt2x00_set_field32(&reg, CSR14_BEACON_GEN, 1);
  560. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  561. break;
  562. default:
  563. break;
  564. }
  565. }
  566. static void rt2400pci_kick_queue(struct data_queue *queue)
  567. {
  568. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  569. u32 reg;
  570. switch (queue->qid) {
  571. case QID_AC_VO:
  572. rt2x00pci_register_read(rt2x00dev, TXCSR0, &reg);
  573. rt2x00_set_field32(&reg, TXCSR0_KICK_PRIO, 1);
  574. rt2x00pci_register_write(rt2x00dev, TXCSR0, reg);
  575. break;
  576. case QID_AC_VI:
  577. rt2x00pci_register_read(rt2x00dev, TXCSR0, &reg);
  578. rt2x00_set_field32(&reg, TXCSR0_KICK_TX, 1);
  579. rt2x00pci_register_write(rt2x00dev, TXCSR0, reg);
  580. break;
  581. case QID_ATIM:
  582. rt2x00pci_register_read(rt2x00dev, TXCSR0, &reg);
  583. rt2x00_set_field32(&reg, TXCSR0_KICK_ATIM, 1);
  584. rt2x00pci_register_write(rt2x00dev, TXCSR0, reg);
  585. break;
  586. default:
  587. break;
  588. }
  589. }
  590. static void rt2400pci_stop_queue(struct data_queue *queue)
  591. {
  592. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  593. u32 reg;
  594. switch (queue->qid) {
  595. case QID_AC_VO:
  596. case QID_AC_VI:
  597. case QID_ATIM:
  598. rt2x00pci_register_read(rt2x00dev, TXCSR0, &reg);
  599. rt2x00_set_field32(&reg, TXCSR0_ABORT, 1);
  600. rt2x00pci_register_write(rt2x00dev, TXCSR0, reg);
  601. break;
  602. case QID_RX:
  603. rt2x00pci_register_read(rt2x00dev, RXCSR0, &reg);
  604. rt2x00_set_field32(&reg, RXCSR0_DISABLE_RX, 1);
  605. rt2x00pci_register_write(rt2x00dev, RXCSR0, reg);
  606. break;
  607. case QID_BEACON:
  608. rt2x00pci_register_read(rt2x00dev, CSR14, &reg);
  609. rt2x00_set_field32(&reg, CSR14_TSF_COUNT, 0);
  610. rt2x00_set_field32(&reg, CSR14_TBCN, 0);
  611. rt2x00_set_field32(&reg, CSR14_BEACON_GEN, 0);
  612. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  613. /*
  614. * Wait for possibly running tbtt tasklets.
  615. */
  616. tasklet_disable(&rt2x00dev->tbtt_tasklet);
  617. break;
  618. default:
  619. break;
  620. }
  621. }
  622. /*
  623. * Initialization functions.
  624. */
  625. static bool rt2400pci_get_entry_state(struct queue_entry *entry)
  626. {
  627. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  628. u32 word;
  629. if (entry->queue->qid == QID_RX) {
  630. rt2x00_desc_read(entry_priv->desc, 0, &word);
  631. return rt2x00_get_field32(word, RXD_W0_OWNER_NIC);
  632. } else {
  633. rt2x00_desc_read(entry_priv->desc, 0, &word);
  634. return (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  635. rt2x00_get_field32(word, TXD_W0_VALID));
  636. }
  637. }
  638. static void rt2400pci_clear_entry(struct queue_entry *entry)
  639. {
  640. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  641. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  642. u32 word;
  643. if (entry->queue->qid == QID_RX) {
  644. rt2x00_desc_read(entry_priv->desc, 2, &word);
  645. rt2x00_set_field32(&word, RXD_W2_BUFFER_LENGTH, entry->skb->len);
  646. rt2x00_desc_write(entry_priv->desc, 2, word);
  647. rt2x00_desc_read(entry_priv->desc, 1, &word);
  648. rt2x00_set_field32(&word, RXD_W1_BUFFER_ADDRESS, skbdesc->skb_dma);
  649. rt2x00_desc_write(entry_priv->desc, 1, word);
  650. rt2x00_desc_read(entry_priv->desc, 0, &word);
  651. rt2x00_set_field32(&word, RXD_W0_OWNER_NIC, 1);
  652. rt2x00_desc_write(entry_priv->desc, 0, word);
  653. } else {
  654. rt2x00_desc_read(entry_priv->desc, 0, &word);
  655. rt2x00_set_field32(&word, TXD_W0_VALID, 0);
  656. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 0);
  657. rt2x00_desc_write(entry_priv->desc, 0, word);
  658. }
  659. }
  660. static int rt2400pci_init_queues(struct rt2x00_dev *rt2x00dev)
  661. {
  662. struct queue_entry_priv_pci *entry_priv;
  663. u32 reg;
  664. /*
  665. * Initialize registers.
  666. */
  667. rt2x00pci_register_read(rt2x00dev, TXCSR2, &reg);
  668. rt2x00_set_field32(&reg, TXCSR2_TXD_SIZE, rt2x00dev->tx[0].desc_size);
  669. rt2x00_set_field32(&reg, TXCSR2_NUM_TXD, rt2x00dev->tx[1].limit);
  670. rt2x00_set_field32(&reg, TXCSR2_NUM_ATIM, rt2x00dev->atim->limit);
  671. rt2x00_set_field32(&reg, TXCSR2_NUM_PRIO, rt2x00dev->tx[0].limit);
  672. rt2x00pci_register_write(rt2x00dev, TXCSR2, reg);
  673. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  674. rt2x00pci_register_read(rt2x00dev, TXCSR3, &reg);
  675. rt2x00_set_field32(&reg, TXCSR3_TX_RING_REGISTER,
  676. entry_priv->desc_dma);
  677. rt2x00pci_register_write(rt2x00dev, TXCSR3, reg);
  678. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  679. rt2x00pci_register_read(rt2x00dev, TXCSR5, &reg);
  680. rt2x00_set_field32(&reg, TXCSR5_PRIO_RING_REGISTER,
  681. entry_priv->desc_dma);
  682. rt2x00pci_register_write(rt2x00dev, TXCSR5, reg);
  683. entry_priv = rt2x00dev->atim->entries[0].priv_data;
  684. rt2x00pci_register_read(rt2x00dev, TXCSR4, &reg);
  685. rt2x00_set_field32(&reg, TXCSR4_ATIM_RING_REGISTER,
  686. entry_priv->desc_dma);
  687. rt2x00pci_register_write(rt2x00dev, TXCSR4, reg);
  688. entry_priv = rt2x00dev->bcn->entries[0].priv_data;
  689. rt2x00pci_register_read(rt2x00dev, TXCSR6, &reg);
  690. rt2x00_set_field32(&reg, TXCSR6_BEACON_RING_REGISTER,
  691. entry_priv->desc_dma);
  692. rt2x00pci_register_write(rt2x00dev, TXCSR6, reg);
  693. rt2x00pci_register_read(rt2x00dev, RXCSR1, &reg);
  694. rt2x00_set_field32(&reg, RXCSR1_RXD_SIZE, rt2x00dev->rx->desc_size);
  695. rt2x00_set_field32(&reg, RXCSR1_NUM_RXD, rt2x00dev->rx->limit);
  696. rt2x00pci_register_write(rt2x00dev, RXCSR1, reg);
  697. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  698. rt2x00pci_register_read(rt2x00dev, RXCSR2, &reg);
  699. rt2x00_set_field32(&reg, RXCSR2_RX_RING_REGISTER,
  700. entry_priv->desc_dma);
  701. rt2x00pci_register_write(rt2x00dev, RXCSR2, reg);
  702. return 0;
  703. }
  704. static int rt2400pci_init_registers(struct rt2x00_dev *rt2x00dev)
  705. {
  706. u32 reg;
  707. rt2x00pci_register_write(rt2x00dev, PSCSR0, 0x00020002);
  708. rt2x00pci_register_write(rt2x00dev, PSCSR1, 0x00000002);
  709. rt2x00pci_register_write(rt2x00dev, PSCSR2, 0x00023f20);
  710. rt2x00pci_register_write(rt2x00dev, PSCSR3, 0x00000002);
  711. rt2x00pci_register_read(rt2x00dev, TIMECSR, &reg);
  712. rt2x00_set_field32(&reg, TIMECSR_US_COUNT, 33);
  713. rt2x00_set_field32(&reg, TIMECSR_US_64_COUNT, 63);
  714. rt2x00_set_field32(&reg, TIMECSR_BEACON_EXPECT, 0);
  715. rt2x00pci_register_write(rt2x00dev, TIMECSR, reg);
  716. rt2x00pci_register_read(rt2x00dev, CSR9, &reg);
  717. rt2x00_set_field32(&reg, CSR9_MAX_FRAME_UNIT,
  718. (rt2x00dev->rx->data_size / 128));
  719. rt2x00pci_register_write(rt2x00dev, CSR9, reg);
  720. rt2x00pci_register_read(rt2x00dev, CSR14, &reg);
  721. rt2x00_set_field32(&reg, CSR14_TSF_COUNT, 0);
  722. rt2x00_set_field32(&reg, CSR14_TSF_SYNC, 0);
  723. rt2x00_set_field32(&reg, CSR14_TBCN, 0);
  724. rt2x00_set_field32(&reg, CSR14_TCFP, 0);
  725. rt2x00_set_field32(&reg, CSR14_TATIMW, 0);
  726. rt2x00_set_field32(&reg, CSR14_BEACON_GEN, 0);
  727. rt2x00_set_field32(&reg, CSR14_CFP_COUNT_PRELOAD, 0);
  728. rt2x00_set_field32(&reg, CSR14_TBCM_PRELOAD, 0);
  729. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  730. rt2x00pci_register_write(rt2x00dev, CNT3, 0x3f080000);
  731. rt2x00pci_register_read(rt2x00dev, ARCSR0, &reg);
  732. rt2x00_set_field32(&reg, ARCSR0_AR_BBP_DATA0, 133);
  733. rt2x00_set_field32(&reg, ARCSR0_AR_BBP_ID0, 134);
  734. rt2x00_set_field32(&reg, ARCSR0_AR_BBP_DATA1, 136);
  735. rt2x00_set_field32(&reg, ARCSR0_AR_BBP_ID1, 135);
  736. rt2x00pci_register_write(rt2x00dev, ARCSR0, reg);
  737. rt2x00pci_register_read(rt2x00dev, RXCSR3, &reg);
  738. rt2x00_set_field32(&reg, RXCSR3_BBP_ID0, 3); /* Tx power.*/
  739. rt2x00_set_field32(&reg, RXCSR3_BBP_ID0_VALID, 1);
  740. rt2x00_set_field32(&reg, RXCSR3_BBP_ID1, 32); /* Signal */
  741. rt2x00_set_field32(&reg, RXCSR3_BBP_ID1_VALID, 1);
  742. rt2x00_set_field32(&reg, RXCSR3_BBP_ID2, 36); /* Rssi */
  743. rt2x00_set_field32(&reg, RXCSR3_BBP_ID2_VALID, 1);
  744. rt2x00pci_register_write(rt2x00dev, RXCSR3, reg);
  745. rt2x00pci_register_write(rt2x00dev, PWRCSR0, 0x3f3b3100);
  746. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  747. return -EBUSY;
  748. rt2x00pci_register_write(rt2x00dev, MACCSR0, 0x00217223);
  749. rt2x00pci_register_write(rt2x00dev, MACCSR1, 0x00235518);
  750. rt2x00pci_register_read(rt2x00dev, MACCSR2, &reg);
  751. rt2x00_set_field32(&reg, MACCSR2_DELAY, 64);
  752. rt2x00pci_register_write(rt2x00dev, MACCSR2, reg);
  753. rt2x00pci_register_read(rt2x00dev, RALINKCSR, &reg);
  754. rt2x00_set_field32(&reg, RALINKCSR_AR_BBP_DATA0, 17);
  755. rt2x00_set_field32(&reg, RALINKCSR_AR_BBP_ID0, 154);
  756. rt2x00_set_field32(&reg, RALINKCSR_AR_BBP_DATA1, 0);
  757. rt2x00_set_field32(&reg, RALINKCSR_AR_BBP_ID1, 154);
  758. rt2x00pci_register_write(rt2x00dev, RALINKCSR, reg);
  759. rt2x00pci_register_read(rt2x00dev, CSR1, &reg);
  760. rt2x00_set_field32(&reg, CSR1_SOFT_RESET, 1);
  761. rt2x00_set_field32(&reg, CSR1_BBP_RESET, 0);
  762. rt2x00_set_field32(&reg, CSR1_HOST_READY, 0);
  763. rt2x00pci_register_write(rt2x00dev, CSR1, reg);
  764. rt2x00pci_register_read(rt2x00dev, CSR1, &reg);
  765. rt2x00_set_field32(&reg, CSR1_SOFT_RESET, 0);
  766. rt2x00_set_field32(&reg, CSR1_HOST_READY, 1);
  767. rt2x00pci_register_write(rt2x00dev, CSR1, reg);
  768. /*
  769. * We must clear the FCS and FIFO error count.
  770. * These registers are cleared on read,
  771. * so we may pass a useless variable to store the value.
  772. */
  773. rt2x00pci_register_read(rt2x00dev, CNT0, &reg);
  774. rt2x00pci_register_read(rt2x00dev, CNT4, &reg);
  775. return 0;
  776. }
  777. static int rt2400pci_wait_bbp_ready(struct rt2x00_dev *rt2x00dev)
  778. {
  779. unsigned int i;
  780. u8 value;
  781. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  782. rt2400pci_bbp_read(rt2x00dev, 0, &value);
  783. if ((value != 0xff) && (value != 0x00))
  784. return 0;
  785. udelay(REGISTER_BUSY_DELAY);
  786. }
  787. ERROR(rt2x00dev, "BBP register access failed, aborting.\n");
  788. return -EACCES;
  789. }
  790. static int rt2400pci_init_bbp(struct rt2x00_dev *rt2x00dev)
  791. {
  792. unsigned int i;
  793. u16 eeprom;
  794. u8 reg_id;
  795. u8 value;
  796. if (unlikely(rt2400pci_wait_bbp_ready(rt2x00dev)))
  797. return -EACCES;
  798. rt2400pci_bbp_write(rt2x00dev, 1, 0x00);
  799. rt2400pci_bbp_write(rt2x00dev, 3, 0x27);
  800. rt2400pci_bbp_write(rt2x00dev, 4, 0x08);
  801. rt2400pci_bbp_write(rt2x00dev, 10, 0x0f);
  802. rt2400pci_bbp_write(rt2x00dev, 15, 0x72);
  803. rt2400pci_bbp_write(rt2x00dev, 16, 0x74);
  804. rt2400pci_bbp_write(rt2x00dev, 17, 0x20);
  805. rt2400pci_bbp_write(rt2x00dev, 18, 0x72);
  806. rt2400pci_bbp_write(rt2x00dev, 19, 0x0b);
  807. rt2400pci_bbp_write(rt2x00dev, 20, 0x00);
  808. rt2400pci_bbp_write(rt2x00dev, 28, 0x11);
  809. rt2400pci_bbp_write(rt2x00dev, 29, 0x04);
  810. rt2400pci_bbp_write(rt2x00dev, 30, 0x21);
  811. rt2400pci_bbp_write(rt2x00dev, 31, 0x00);
  812. for (i = 0; i < EEPROM_BBP_SIZE; i++) {
  813. rt2x00_eeprom_read(rt2x00dev, EEPROM_BBP_START + i, &eeprom);
  814. if (eeprom != 0xffff && eeprom != 0x0000) {
  815. reg_id = rt2x00_get_field16(eeprom, EEPROM_BBP_REG_ID);
  816. value = rt2x00_get_field16(eeprom, EEPROM_BBP_VALUE);
  817. rt2400pci_bbp_write(rt2x00dev, reg_id, value);
  818. }
  819. }
  820. return 0;
  821. }
  822. /*
  823. * Device state switch handlers.
  824. */
  825. static void rt2400pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  826. enum dev_state state)
  827. {
  828. int mask = (state == STATE_RADIO_IRQ_OFF);
  829. u32 reg;
  830. unsigned long flags;
  831. /*
  832. * When interrupts are being enabled, the interrupt registers
  833. * should clear the register to assure a clean state.
  834. */
  835. if (state == STATE_RADIO_IRQ_ON) {
  836. rt2x00pci_register_read(rt2x00dev, CSR7, &reg);
  837. rt2x00pci_register_write(rt2x00dev, CSR7, reg);
  838. /*
  839. * Enable tasklets.
  840. */
  841. tasklet_enable(&rt2x00dev->txstatus_tasklet);
  842. tasklet_enable(&rt2x00dev->rxdone_tasklet);
  843. }
  844. /*
  845. * Only toggle the interrupts bits we are going to use.
  846. * Non-checked interrupt bits are disabled by default.
  847. */
  848. spin_lock_irqsave(&rt2x00dev->irqmask_lock, flags);
  849. rt2x00pci_register_read(rt2x00dev, CSR8, &reg);
  850. rt2x00_set_field32(&reg, CSR8_TBCN_EXPIRE, mask);
  851. rt2x00_set_field32(&reg, CSR8_TXDONE_TXRING, mask);
  852. rt2x00_set_field32(&reg, CSR8_TXDONE_ATIMRING, mask);
  853. rt2x00_set_field32(&reg, CSR8_TXDONE_PRIORING, mask);
  854. rt2x00_set_field32(&reg, CSR8_RXDONE, mask);
  855. rt2x00pci_register_write(rt2x00dev, CSR8, reg);
  856. spin_unlock_irqrestore(&rt2x00dev->irqmask_lock, flags);
  857. if (state == STATE_RADIO_IRQ_OFF) {
  858. /*
  859. * Ensure that all tasklets are finished before
  860. * disabling the interrupts.
  861. */
  862. tasklet_disable(&rt2x00dev->txstatus_tasklet);
  863. tasklet_disable(&rt2x00dev->rxdone_tasklet);
  864. }
  865. }
  866. static int rt2400pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  867. {
  868. /*
  869. * Initialize all registers.
  870. */
  871. if (unlikely(rt2400pci_init_queues(rt2x00dev) ||
  872. rt2400pci_init_registers(rt2x00dev) ||
  873. rt2400pci_init_bbp(rt2x00dev)))
  874. return -EIO;
  875. return 0;
  876. }
  877. static void rt2400pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  878. {
  879. /*
  880. * Disable power
  881. */
  882. rt2x00pci_register_write(rt2x00dev, PWRCSR0, 0);
  883. }
  884. static int rt2400pci_set_state(struct rt2x00_dev *rt2x00dev,
  885. enum dev_state state)
  886. {
  887. u32 reg, reg2;
  888. unsigned int i;
  889. char put_to_sleep;
  890. char bbp_state;
  891. char rf_state;
  892. put_to_sleep = (state != STATE_AWAKE);
  893. rt2x00pci_register_read(rt2x00dev, PWRCSR1, &reg);
  894. rt2x00_set_field32(&reg, PWRCSR1_SET_STATE, 1);
  895. rt2x00_set_field32(&reg, PWRCSR1_BBP_DESIRE_STATE, state);
  896. rt2x00_set_field32(&reg, PWRCSR1_RF_DESIRE_STATE, state);
  897. rt2x00_set_field32(&reg, PWRCSR1_PUT_TO_SLEEP, put_to_sleep);
  898. rt2x00pci_register_write(rt2x00dev, PWRCSR1, reg);
  899. /*
  900. * Device is not guaranteed to be in the requested state yet.
  901. * We must wait until the register indicates that the
  902. * device has entered the correct state.
  903. */
  904. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  905. rt2x00pci_register_read(rt2x00dev, PWRCSR1, &reg2);
  906. bbp_state = rt2x00_get_field32(reg2, PWRCSR1_BBP_CURR_STATE);
  907. rf_state = rt2x00_get_field32(reg2, PWRCSR1_RF_CURR_STATE);
  908. if (bbp_state == state && rf_state == state)
  909. return 0;
  910. rt2x00pci_register_write(rt2x00dev, PWRCSR1, reg);
  911. msleep(10);
  912. }
  913. return -EBUSY;
  914. }
  915. static int rt2400pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  916. enum dev_state state)
  917. {
  918. int retval = 0;
  919. switch (state) {
  920. case STATE_RADIO_ON:
  921. retval = rt2400pci_enable_radio(rt2x00dev);
  922. break;
  923. case STATE_RADIO_OFF:
  924. rt2400pci_disable_radio(rt2x00dev);
  925. break;
  926. case STATE_RADIO_IRQ_ON:
  927. case STATE_RADIO_IRQ_OFF:
  928. rt2400pci_toggle_irq(rt2x00dev, state);
  929. break;
  930. case STATE_DEEP_SLEEP:
  931. case STATE_SLEEP:
  932. case STATE_STANDBY:
  933. case STATE_AWAKE:
  934. retval = rt2400pci_set_state(rt2x00dev, state);
  935. break;
  936. default:
  937. retval = -ENOTSUPP;
  938. break;
  939. }
  940. if (unlikely(retval))
  941. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  942. state, retval);
  943. return retval;
  944. }
  945. /*
  946. * TX descriptor initialization
  947. */
  948. static void rt2400pci_write_tx_desc(struct queue_entry *entry,
  949. struct txentry_desc *txdesc)
  950. {
  951. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  952. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  953. __le32 *txd = entry_priv->desc;
  954. u32 word;
  955. /*
  956. * Start writing the descriptor words.
  957. */
  958. rt2x00_desc_read(txd, 1, &word);
  959. rt2x00_set_field32(&word, TXD_W1_BUFFER_ADDRESS, skbdesc->skb_dma);
  960. rt2x00_desc_write(txd, 1, word);
  961. rt2x00_desc_read(txd, 2, &word);
  962. rt2x00_set_field32(&word, TXD_W2_BUFFER_LENGTH, txdesc->length);
  963. rt2x00_set_field32(&word, TXD_W2_DATABYTE_COUNT, txdesc->length);
  964. rt2x00_desc_write(txd, 2, word);
  965. rt2x00_desc_read(txd, 3, &word);
  966. rt2x00_set_field32(&word, TXD_W3_PLCP_SIGNAL, txdesc->u.plcp.signal);
  967. rt2x00_set_field32(&word, TXD_W3_PLCP_SIGNAL_REGNUM, 5);
  968. rt2x00_set_field32(&word, TXD_W3_PLCP_SIGNAL_BUSY, 1);
  969. rt2x00_set_field32(&word, TXD_W3_PLCP_SERVICE, txdesc->u.plcp.service);
  970. rt2x00_set_field32(&word, TXD_W3_PLCP_SERVICE_REGNUM, 6);
  971. rt2x00_set_field32(&word, TXD_W3_PLCP_SERVICE_BUSY, 1);
  972. rt2x00_desc_write(txd, 3, word);
  973. rt2x00_desc_read(txd, 4, &word);
  974. rt2x00_set_field32(&word, TXD_W4_PLCP_LENGTH_LOW,
  975. txdesc->u.plcp.length_low);
  976. rt2x00_set_field32(&word, TXD_W3_PLCP_LENGTH_LOW_REGNUM, 8);
  977. rt2x00_set_field32(&word, TXD_W3_PLCP_LENGTH_LOW_BUSY, 1);
  978. rt2x00_set_field32(&word, TXD_W4_PLCP_LENGTH_HIGH,
  979. txdesc->u.plcp.length_high);
  980. rt2x00_set_field32(&word, TXD_W3_PLCP_LENGTH_HIGH_REGNUM, 7);
  981. rt2x00_set_field32(&word, TXD_W3_PLCP_LENGTH_HIGH_BUSY, 1);
  982. rt2x00_desc_write(txd, 4, word);
  983. /*
  984. * Writing TXD word 0 must the last to prevent a race condition with
  985. * the device, whereby the device may take hold of the TXD before we
  986. * finished updating it.
  987. */
  988. rt2x00_desc_read(txd, 0, &word);
  989. rt2x00_set_field32(&word, TXD_W0_OWNER_NIC, 1);
  990. rt2x00_set_field32(&word, TXD_W0_VALID, 1);
  991. rt2x00_set_field32(&word, TXD_W0_MORE_FRAG,
  992. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  993. rt2x00_set_field32(&word, TXD_W0_ACK,
  994. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  995. rt2x00_set_field32(&word, TXD_W0_TIMESTAMP,
  996. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  997. rt2x00_set_field32(&word, TXD_W0_RTS,
  998. test_bit(ENTRY_TXD_RTS_FRAME, &txdesc->flags));
  999. rt2x00_set_field32(&word, TXD_W0_IFS, txdesc->u.plcp.ifs);
  1000. rt2x00_set_field32(&word, TXD_W0_RETRY_MODE,
  1001. test_bit(ENTRY_TXD_RETRY_MODE, &txdesc->flags));
  1002. rt2x00_desc_write(txd, 0, word);
  1003. /*
  1004. * Register descriptor details in skb frame descriptor.
  1005. */
  1006. skbdesc->desc = txd;
  1007. skbdesc->desc_len = TXD_DESC_SIZE;
  1008. }
  1009. /*
  1010. * TX data initialization
  1011. */
  1012. static void rt2400pci_write_beacon(struct queue_entry *entry,
  1013. struct txentry_desc *txdesc)
  1014. {
  1015. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1016. u32 reg;
  1017. /*
  1018. * Disable beaconing while we are reloading the beacon data,
  1019. * otherwise we might be sending out invalid data.
  1020. */
  1021. rt2x00pci_register_read(rt2x00dev, CSR14, &reg);
  1022. rt2x00_set_field32(&reg, CSR14_BEACON_GEN, 0);
  1023. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  1024. rt2x00queue_map_txskb(entry);
  1025. /*
  1026. * Write the TX descriptor for the beacon.
  1027. */
  1028. rt2400pci_write_tx_desc(entry, txdesc);
  1029. /*
  1030. * Dump beacon to userspace through debugfs.
  1031. */
  1032. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_BEACON, entry->skb);
  1033. /*
  1034. * Enable beaconing again.
  1035. */
  1036. rt2x00_set_field32(&reg, CSR14_BEACON_GEN, 1);
  1037. rt2x00pci_register_write(rt2x00dev, CSR14, reg);
  1038. }
  1039. /*
  1040. * RX control handlers
  1041. */
  1042. static void rt2400pci_fill_rxdone(struct queue_entry *entry,
  1043. struct rxdone_entry_desc *rxdesc)
  1044. {
  1045. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  1046. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  1047. u32 word0;
  1048. u32 word2;
  1049. u32 word3;
  1050. u32 word4;
  1051. u64 tsf;
  1052. u32 rx_low;
  1053. u32 rx_high;
  1054. rt2x00_desc_read(entry_priv->desc, 0, &word0);
  1055. rt2x00_desc_read(entry_priv->desc, 2, &word2);
  1056. rt2x00_desc_read(entry_priv->desc, 3, &word3);
  1057. rt2x00_desc_read(entry_priv->desc, 4, &word4);
  1058. if (rt2x00_get_field32(word0, RXD_W0_CRC_ERROR))
  1059. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  1060. if (rt2x00_get_field32(word0, RXD_W0_PHYSICAL_ERROR))
  1061. rxdesc->flags |= RX_FLAG_FAILED_PLCP_CRC;
  1062. /*
  1063. * We only get the lower 32bits from the timestamp,
  1064. * to get the full 64bits we must complement it with
  1065. * the timestamp from get_tsf().
  1066. * Note that when a wraparound of the lower 32bits
  1067. * has occurred between the frame arrival and the get_tsf()
  1068. * call, we must decrease the higher 32bits with 1 to get
  1069. * to correct value.
  1070. */
  1071. tsf = rt2x00dev->ops->hw->get_tsf(rt2x00dev->hw);
  1072. rx_low = rt2x00_get_field32(word4, RXD_W4_RX_END_TIME);
  1073. rx_high = upper_32_bits(tsf);
  1074. if ((u32)tsf <= rx_low)
  1075. rx_high--;
  1076. /*
  1077. * Obtain the status about this packet.
  1078. * The signal is the PLCP value, and needs to be stripped
  1079. * of the preamble bit (0x08).
  1080. */
  1081. rxdesc->timestamp = ((u64)rx_high << 32) | rx_low;
  1082. rxdesc->signal = rt2x00_get_field32(word2, RXD_W2_SIGNAL) & ~0x08;
  1083. rxdesc->rssi = rt2x00_get_field32(word2, RXD_W3_RSSI) -
  1084. entry->queue->rt2x00dev->rssi_offset;
  1085. rxdesc->size = rt2x00_get_field32(word0, RXD_W0_DATABYTE_COUNT);
  1086. rxdesc->dev_flags |= RXDONE_SIGNAL_PLCP;
  1087. if (rt2x00_get_field32(word0, RXD_W0_MY_BSS))
  1088. rxdesc->dev_flags |= RXDONE_MY_BSS;
  1089. }
  1090. /*
  1091. * Interrupt functions.
  1092. */
  1093. static void rt2400pci_txdone(struct rt2x00_dev *rt2x00dev,
  1094. const enum data_queue_qid queue_idx)
  1095. {
  1096. struct data_queue *queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
  1097. struct queue_entry_priv_pci *entry_priv;
  1098. struct queue_entry *entry;
  1099. struct txdone_entry_desc txdesc;
  1100. u32 word;
  1101. while (!rt2x00queue_empty(queue)) {
  1102. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  1103. entry_priv = entry->priv_data;
  1104. rt2x00_desc_read(entry_priv->desc, 0, &word);
  1105. if (rt2x00_get_field32(word, TXD_W0_OWNER_NIC) ||
  1106. !rt2x00_get_field32(word, TXD_W0_VALID))
  1107. break;
  1108. /*
  1109. * Obtain the status about this packet.
  1110. */
  1111. txdesc.flags = 0;
  1112. switch (rt2x00_get_field32(word, TXD_W0_RESULT)) {
  1113. case 0: /* Success */
  1114. case 1: /* Success with retry */
  1115. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  1116. break;
  1117. case 2: /* Failure, excessive retries */
  1118. __set_bit(TXDONE_EXCESSIVE_RETRY, &txdesc.flags);
  1119. /* Don't break, this is a failed frame! */
  1120. default: /* Failure */
  1121. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  1122. }
  1123. txdesc.retry = rt2x00_get_field32(word, TXD_W0_RETRY_COUNT);
  1124. rt2x00lib_txdone(entry, &txdesc);
  1125. }
  1126. }
  1127. static inline void rt2400pci_enable_interrupt(struct rt2x00_dev *rt2x00dev,
  1128. struct rt2x00_field32 irq_field)
  1129. {
  1130. u32 reg;
  1131. /*
  1132. * Enable a single interrupt. The interrupt mask register
  1133. * access needs locking.
  1134. */
  1135. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1136. rt2x00pci_register_read(rt2x00dev, CSR8, &reg);
  1137. rt2x00_set_field32(&reg, irq_field, 0);
  1138. rt2x00pci_register_write(rt2x00dev, CSR8, reg);
  1139. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1140. }
  1141. static void rt2400pci_txstatus_tasklet(unsigned long data)
  1142. {
  1143. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1144. u32 reg;
  1145. /*
  1146. * Handle all tx queues.
  1147. */
  1148. rt2400pci_txdone(rt2x00dev, QID_ATIM);
  1149. rt2400pci_txdone(rt2x00dev, QID_AC_VO);
  1150. rt2400pci_txdone(rt2x00dev, QID_AC_VI);
  1151. /*
  1152. * Enable all TXDONE interrupts again.
  1153. */
  1154. spin_lock_irq(&rt2x00dev->irqmask_lock);
  1155. rt2x00pci_register_read(rt2x00dev, CSR8, &reg);
  1156. rt2x00_set_field32(&reg, CSR8_TXDONE_TXRING, 0);
  1157. rt2x00_set_field32(&reg, CSR8_TXDONE_ATIMRING, 0);
  1158. rt2x00_set_field32(&reg, CSR8_TXDONE_PRIORING, 0);
  1159. rt2x00pci_register_write(rt2x00dev, CSR8, reg);
  1160. spin_unlock_irq(&rt2x00dev->irqmask_lock);
  1161. }
  1162. static void rt2400pci_tbtt_tasklet(unsigned long data)
  1163. {
  1164. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1165. rt2x00lib_beacondone(rt2x00dev);
  1166. rt2400pci_enable_interrupt(rt2x00dev, CSR8_TBCN_EXPIRE);
  1167. }
  1168. static void rt2400pci_rxdone_tasklet(unsigned long data)
  1169. {
  1170. struct rt2x00_dev *rt2x00dev = (struct rt2x00_dev *)data;
  1171. if (rt2x00pci_rxdone(rt2x00dev))
  1172. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  1173. else
  1174. rt2400pci_enable_interrupt(rt2x00dev, CSR8_RXDONE);
  1175. }
  1176. static irqreturn_t rt2400pci_interrupt(int irq, void *dev_instance)
  1177. {
  1178. struct rt2x00_dev *rt2x00dev = dev_instance;
  1179. u32 reg, mask;
  1180. /*
  1181. * Get the interrupt sources & saved to local variable.
  1182. * Write register value back to clear pending interrupts.
  1183. */
  1184. rt2x00pci_register_read(rt2x00dev, CSR7, &reg);
  1185. rt2x00pci_register_write(rt2x00dev, CSR7, reg);
  1186. if (!reg)
  1187. return IRQ_NONE;
  1188. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  1189. return IRQ_HANDLED;
  1190. mask = reg;
  1191. /*
  1192. * Schedule tasklets for interrupt handling.
  1193. */
  1194. if (rt2x00_get_field32(reg, CSR7_TBCN_EXPIRE))
  1195. tasklet_hi_schedule(&rt2x00dev->tbtt_tasklet);
  1196. if (rt2x00_get_field32(reg, CSR7_RXDONE))
  1197. tasklet_schedule(&rt2x00dev->rxdone_tasklet);
  1198. if (rt2x00_get_field32(reg, CSR7_TXDONE_ATIMRING) ||
  1199. rt2x00_get_field32(reg, CSR7_TXDONE_PRIORING) ||
  1200. rt2x00_get_field32(reg, CSR7_TXDONE_TXRING)) {
  1201. tasklet_schedule(&rt2x00dev->txstatus_tasklet);
  1202. /*
  1203. * Mask out all txdone interrupts.
  1204. */
  1205. rt2x00_set_field32(&mask, CSR8_TXDONE_TXRING, 1);
  1206. rt2x00_set_field32(&mask, CSR8_TXDONE_ATIMRING, 1);
  1207. rt2x00_set_field32(&mask, CSR8_TXDONE_PRIORING, 1);
  1208. }
  1209. /*
  1210. * Disable all interrupts for which a tasklet was scheduled right now,
  1211. * the tasklet will reenable the appropriate interrupts.
  1212. */
  1213. spin_lock(&rt2x00dev->irqmask_lock);
  1214. rt2x00pci_register_read(rt2x00dev, CSR8, &reg);
  1215. reg |= mask;
  1216. rt2x00pci_register_write(rt2x00dev, CSR8, reg);
  1217. spin_unlock(&rt2x00dev->irqmask_lock);
  1218. return IRQ_HANDLED;
  1219. }
  1220. /*
  1221. * Device probe functions.
  1222. */
  1223. static int rt2400pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  1224. {
  1225. struct eeprom_93cx6 eeprom;
  1226. u32 reg;
  1227. u16 word;
  1228. u8 *mac;
  1229. rt2x00pci_register_read(rt2x00dev, CSR21, &reg);
  1230. eeprom.data = rt2x00dev;
  1231. eeprom.register_read = rt2400pci_eepromregister_read;
  1232. eeprom.register_write = rt2400pci_eepromregister_write;
  1233. eeprom.width = rt2x00_get_field32(reg, CSR21_TYPE_93C46) ?
  1234. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  1235. eeprom.reg_data_in = 0;
  1236. eeprom.reg_data_out = 0;
  1237. eeprom.reg_data_clock = 0;
  1238. eeprom.reg_chip_select = 0;
  1239. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  1240. EEPROM_SIZE / sizeof(u16));
  1241. /*
  1242. * Start validation of the data that has been read.
  1243. */
  1244. mac = rt2x00_eeprom_addr(rt2x00dev, EEPROM_MAC_ADDR_0);
  1245. if (!is_valid_ether_addr(mac)) {
  1246. random_ether_addr(mac);
  1247. EEPROM(rt2x00dev, "MAC: %pM\n", mac);
  1248. }
  1249. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &word);
  1250. if (word == 0xffff) {
  1251. ERROR(rt2x00dev, "Invalid EEPROM data detected.\n");
  1252. return -EINVAL;
  1253. }
  1254. return 0;
  1255. }
  1256. static int rt2400pci_init_eeprom(struct rt2x00_dev *rt2x00dev)
  1257. {
  1258. u32 reg;
  1259. u16 value;
  1260. u16 eeprom;
  1261. /*
  1262. * Read EEPROM word for configuration.
  1263. */
  1264. rt2x00_eeprom_read(rt2x00dev, EEPROM_ANTENNA, &eeprom);
  1265. /*
  1266. * Identify RF chipset.
  1267. */
  1268. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RF_TYPE);
  1269. rt2x00pci_register_read(rt2x00dev, CSR0, &reg);
  1270. rt2x00_set_chip(rt2x00dev, RT2460, value,
  1271. rt2x00_get_field32(reg, CSR0_REVISION));
  1272. if (!rt2x00_rf(rt2x00dev, RF2420) && !rt2x00_rf(rt2x00dev, RF2421)) {
  1273. ERROR(rt2x00dev, "Invalid RF chipset detected.\n");
  1274. return -ENODEV;
  1275. }
  1276. /*
  1277. * Identify default antenna configuration.
  1278. */
  1279. rt2x00dev->default_ant.tx =
  1280. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_TX_DEFAULT);
  1281. rt2x00dev->default_ant.rx =
  1282. rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_DEFAULT);
  1283. /*
  1284. * When the eeprom indicates SW_DIVERSITY use HW_DIVERSITY instead.
  1285. * I am not 100% sure about this, but the legacy drivers do not
  1286. * indicate antenna swapping in software is required when
  1287. * diversity is enabled.
  1288. */
  1289. if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
  1290. rt2x00dev->default_ant.tx = ANTENNA_HW_DIVERSITY;
  1291. if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
  1292. rt2x00dev->default_ant.rx = ANTENNA_HW_DIVERSITY;
  1293. /*
  1294. * Store led mode, for correct led behaviour.
  1295. */
  1296. #ifdef CONFIG_RT2X00_LIB_LEDS
  1297. value = rt2x00_get_field16(eeprom, EEPROM_ANTENNA_LED_MODE);
  1298. rt2400pci_init_led(rt2x00dev, &rt2x00dev->led_radio, LED_TYPE_RADIO);
  1299. if (value == LED_MODE_TXRX_ACTIVITY ||
  1300. value == LED_MODE_DEFAULT ||
  1301. value == LED_MODE_ASUS)
  1302. rt2400pci_init_led(rt2x00dev, &rt2x00dev->led_qual,
  1303. LED_TYPE_ACTIVITY);
  1304. #endif /* CONFIG_RT2X00_LIB_LEDS */
  1305. /*
  1306. * Detect if this device has an hardware controlled radio.
  1307. */
  1308. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_HARDWARE_RADIO))
  1309. __set_bit(CAPABILITY_HW_BUTTON, &rt2x00dev->cap_flags);
  1310. /*
  1311. * Check if the BBP tuning should be enabled.
  1312. */
  1313. if (rt2x00_get_field16(eeprom, EEPROM_ANTENNA_RX_AGCVGC_TUNING))
  1314. __set_bit(CAPABILITY_LINK_TUNING, &rt2x00dev->cap_flags);
  1315. return 0;
  1316. }
  1317. /*
  1318. * RF value list for RF2420 & RF2421
  1319. * Supports: 2.4 GHz
  1320. */
  1321. static const struct rf_channel rf_vals_b[] = {
  1322. { 1, 0x00022058, 0x000c1fda, 0x00000101, 0 },
  1323. { 2, 0x00022058, 0x000c1fee, 0x00000101, 0 },
  1324. { 3, 0x00022058, 0x000c2002, 0x00000101, 0 },
  1325. { 4, 0x00022058, 0x000c2016, 0x00000101, 0 },
  1326. { 5, 0x00022058, 0x000c202a, 0x00000101, 0 },
  1327. { 6, 0x00022058, 0x000c203e, 0x00000101, 0 },
  1328. { 7, 0x00022058, 0x000c2052, 0x00000101, 0 },
  1329. { 8, 0x00022058, 0x000c2066, 0x00000101, 0 },
  1330. { 9, 0x00022058, 0x000c207a, 0x00000101, 0 },
  1331. { 10, 0x00022058, 0x000c208e, 0x00000101, 0 },
  1332. { 11, 0x00022058, 0x000c20a2, 0x00000101, 0 },
  1333. { 12, 0x00022058, 0x000c20b6, 0x00000101, 0 },
  1334. { 13, 0x00022058, 0x000c20ca, 0x00000101, 0 },
  1335. { 14, 0x00022058, 0x000c20fa, 0x00000101, 0 },
  1336. };
  1337. static int rt2400pci_probe_hw_mode(struct rt2x00_dev *rt2x00dev)
  1338. {
  1339. struct hw_mode_spec *spec = &rt2x00dev->spec;
  1340. struct channel_info *info;
  1341. char *tx_power;
  1342. unsigned int i;
  1343. /*
  1344. * Initialize all hw fields.
  1345. */
  1346. rt2x00dev->hw->flags = IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1347. IEEE80211_HW_SIGNAL_DBM |
  1348. IEEE80211_HW_SUPPORTS_PS |
  1349. IEEE80211_HW_PS_NULLFUNC_STACK;
  1350. SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->dev);
  1351. SET_IEEE80211_PERM_ADDR(rt2x00dev->hw,
  1352. rt2x00_eeprom_addr(rt2x00dev,
  1353. EEPROM_MAC_ADDR_0));
  1354. /*
  1355. * Initialize hw_mode information.
  1356. */
  1357. spec->supported_bands = SUPPORT_BAND_2GHZ;
  1358. spec->supported_rates = SUPPORT_RATE_CCK;
  1359. spec->num_channels = ARRAY_SIZE(rf_vals_b);
  1360. spec->channels = rf_vals_b;
  1361. /*
  1362. * Create channel information array
  1363. */
  1364. info = kcalloc(spec->num_channels, sizeof(*info), GFP_KERNEL);
  1365. if (!info)
  1366. return -ENOMEM;
  1367. spec->channels_info = info;
  1368. tx_power = rt2x00_eeprom_addr(rt2x00dev, EEPROM_TXPOWER_START);
  1369. for (i = 0; i < 14; i++) {
  1370. info[i].max_power = TXPOWER_FROM_DEV(MAX_TXPOWER);
  1371. info[i].default_power1 = TXPOWER_FROM_DEV(tx_power[i]);
  1372. }
  1373. return 0;
  1374. }
  1375. static int rt2400pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  1376. {
  1377. int retval;
  1378. /*
  1379. * Allocate eeprom data.
  1380. */
  1381. retval = rt2400pci_validate_eeprom(rt2x00dev);
  1382. if (retval)
  1383. return retval;
  1384. retval = rt2400pci_init_eeprom(rt2x00dev);
  1385. if (retval)
  1386. return retval;
  1387. /*
  1388. * Initialize hw specifications.
  1389. */
  1390. retval = rt2400pci_probe_hw_mode(rt2x00dev);
  1391. if (retval)
  1392. return retval;
  1393. /*
  1394. * This device requires the atim queue and DMA-mapped skbs.
  1395. */
  1396. __set_bit(REQUIRE_ATIM_QUEUE, &rt2x00dev->cap_flags);
  1397. __set_bit(REQUIRE_DMA, &rt2x00dev->cap_flags);
  1398. __set_bit(REQUIRE_SW_SEQNO, &rt2x00dev->cap_flags);
  1399. /*
  1400. * Set the rssi offset.
  1401. */
  1402. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  1403. return 0;
  1404. }
  1405. /*
  1406. * IEEE80211 stack callback functions.
  1407. */
  1408. static int rt2400pci_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1409. const struct ieee80211_tx_queue_params *params)
  1410. {
  1411. struct rt2x00_dev *rt2x00dev = hw->priv;
  1412. /*
  1413. * We don't support variating cw_min and cw_max variables
  1414. * per queue. So by default we only configure the TX queue,
  1415. * and ignore all other configurations.
  1416. */
  1417. if (queue != 0)
  1418. return -EINVAL;
  1419. if (rt2x00mac_conf_tx(hw, queue, params))
  1420. return -EINVAL;
  1421. /*
  1422. * Write configuration to register.
  1423. */
  1424. rt2400pci_config_cw(rt2x00dev,
  1425. rt2x00dev->tx->cw_min, rt2x00dev->tx->cw_max);
  1426. return 0;
  1427. }
  1428. static u64 rt2400pci_get_tsf(struct ieee80211_hw *hw)
  1429. {
  1430. struct rt2x00_dev *rt2x00dev = hw->priv;
  1431. u64 tsf;
  1432. u32 reg;
  1433. rt2x00pci_register_read(rt2x00dev, CSR17, &reg);
  1434. tsf = (u64) rt2x00_get_field32(reg, CSR17_HIGH_TSFTIMER) << 32;
  1435. rt2x00pci_register_read(rt2x00dev, CSR16, &reg);
  1436. tsf |= rt2x00_get_field32(reg, CSR16_LOW_TSFTIMER);
  1437. return tsf;
  1438. }
  1439. static int rt2400pci_tx_last_beacon(struct ieee80211_hw *hw)
  1440. {
  1441. struct rt2x00_dev *rt2x00dev = hw->priv;
  1442. u32 reg;
  1443. rt2x00pci_register_read(rt2x00dev, CSR15, &reg);
  1444. return rt2x00_get_field32(reg, CSR15_BEACON_SENT);
  1445. }
  1446. static const struct ieee80211_ops rt2400pci_mac80211_ops = {
  1447. .tx = rt2x00mac_tx,
  1448. .start = rt2x00mac_start,
  1449. .stop = rt2x00mac_stop,
  1450. .add_interface = rt2x00mac_add_interface,
  1451. .remove_interface = rt2x00mac_remove_interface,
  1452. .config = rt2x00mac_config,
  1453. .configure_filter = rt2x00mac_configure_filter,
  1454. .sw_scan_start = rt2x00mac_sw_scan_start,
  1455. .sw_scan_complete = rt2x00mac_sw_scan_complete,
  1456. .get_stats = rt2x00mac_get_stats,
  1457. .bss_info_changed = rt2x00mac_bss_info_changed,
  1458. .conf_tx = rt2400pci_conf_tx,
  1459. .get_tsf = rt2400pci_get_tsf,
  1460. .tx_last_beacon = rt2400pci_tx_last_beacon,
  1461. .rfkill_poll = rt2x00mac_rfkill_poll,
  1462. .flush = rt2x00mac_flush,
  1463. .set_antenna = rt2x00mac_set_antenna,
  1464. .get_antenna = rt2x00mac_get_antenna,
  1465. .get_ringparam = rt2x00mac_get_ringparam,
  1466. .tx_frames_pending = rt2x00mac_tx_frames_pending,
  1467. };
  1468. static const struct rt2x00lib_ops rt2400pci_rt2x00_ops = {
  1469. .irq_handler = rt2400pci_interrupt,
  1470. .txstatus_tasklet = rt2400pci_txstatus_tasklet,
  1471. .tbtt_tasklet = rt2400pci_tbtt_tasklet,
  1472. .rxdone_tasklet = rt2400pci_rxdone_tasklet,
  1473. .probe_hw = rt2400pci_probe_hw,
  1474. .initialize = rt2x00pci_initialize,
  1475. .uninitialize = rt2x00pci_uninitialize,
  1476. .get_entry_state = rt2400pci_get_entry_state,
  1477. .clear_entry = rt2400pci_clear_entry,
  1478. .set_device_state = rt2400pci_set_device_state,
  1479. .rfkill_poll = rt2400pci_rfkill_poll,
  1480. .link_stats = rt2400pci_link_stats,
  1481. .reset_tuner = rt2400pci_reset_tuner,
  1482. .link_tuner = rt2400pci_link_tuner,
  1483. .start_queue = rt2400pci_start_queue,
  1484. .kick_queue = rt2400pci_kick_queue,
  1485. .stop_queue = rt2400pci_stop_queue,
  1486. .flush_queue = rt2x00pci_flush_queue,
  1487. .write_tx_desc = rt2400pci_write_tx_desc,
  1488. .write_beacon = rt2400pci_write_beacon,
  1489. .fill_rxdone = rt2400pci_fill_rxdone,
  1490. .config_filter = rt2400pci_config_filter,
  1491. .config_intf = rt2400pci_config_intf,
  1492. .config_erp = rt2400pci_config_erp,
  1493. .config_ant = rt2400pci_config_ant,
  1494. .config = rt2400pci_config,
  1495. };
  1496. static const struct data_queue_desc rt2400pci_queue_rx = {
  1497. .entry_num = 24,
  1498. .data_size = DATA_FRAME_SIZE,
  1499. .desc_size = RXD_DESC_SIZE,
  1500. .priv_size = sizeof(struct queue_entry_priv_pci),
  1501. };
  1502. static const struct data_queue_desc rt2400pci_queue_tx = {
  1503. .entry_num = 24,
  1504. .data_size = DATA_FRAME_SIZE,
  1505. .desc_size = TXD_DESC_SIZE,
  1506. .priv_size = sizeof(struct queue_entry_priv_pci),
  1507. };
  1508. static const struct data_queue_desc rt2400pci_queue_bcn = {
  1509. .entry_num = 1,
  1510. .data_size = MGMT_FRAME_SIZE,
  1511. .desc_size = TXD_DESC_SIZE,
  1512. .priv_size = sizeof(struct queue_entry_priv_pci),
  1513. };
  1514. static const struct data_queue_desc rt2400pci_queue_atim = {
  1515. .entry_num = 8,
  1516. .data_size = DATA_FRAME_SIZE,
  1517. .desc_size = TXD_DESC_SIZE,
  1518. .priv_size = sizeof(struct queue_entry_priv_pci),
  1519. };
  1520. static const struct rt2x00_ops rt2400pci_ops = {
  1521. .name = KBUILD_MODNAME,
  1522. .max_sta_intf = 1,
  1523. .max_ap_intf = 1,
  1524. .eeprom_size = EEPROM_SIZE,
  1525. .rf_size = RF_SIZE,
  1526. .tx_queues = NUM_TX_QUEUES,
  1527. .extra_tx_headroom = 0,
  1528. .rx = &rt2400pci_queue_rx,
  1529. .tx = &rt2400pci_queue_tx,
  1530. .bcn = &rt2400pci_queue_bcn,
  1531. .atim = &rt2400pci_queue_atim,
  1532. .lib = &rt2400pci_rt2x00_ops,
  1533. .hw = &rt2400pci_mac80211_ops,
  1534. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  1535. .debugfs = &rt2400pci_rt2x00debug,
  1536. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  1537. };
  1538. /*
  1539. * RT2400pci module information.
  1540. */
  1541. static DEFINE_PCI_DEVICE_TABLE(rt2400pci_device_table) = {
  1542. { PCI_DEVICE(0x1814, 0x0101) },
  1543. { 0, }
  1544. };
  1545. MODULE_AUTHOR(DRV_PROJECT);
  1546. MODULE_VERSION(DRV_VERSION);
  1547. MODULE_DESCRIPTION("Ralink RT2400 PCI & PCMCIA Wireless LAN driver.");
  1548. MODULE_SUPPORTED_DEVICE("Ralink RT2460 PCI & PCMCIA chipset based cards");
  1549. MODULE_DEVICE_TABLE(pci, rt2400pci_device_table);
  1550. MODULE_LICENSE("GPL");
  1551. static int rt2400pci_probe(struct pci_dev *pci_dev,
  1552. const struct pci_device_id *id)
  1553. {
  1554. return rt2x00pci_probe(pci_dev, &rt2400pci_ops);
  1555. }
  1556. static struct pci_driver rt2400pci_driver = {
  1557. .name = KBUILD_MODNAME,
  1558. .id_table = rt2400pci_device_table,
  1559. .probe = rt2400pci_probe,
  1560. .remove = __devexit_p(rt2x00pci_remove),
  1561. .suspend = rt2x00pci_suspend,
  1562. .resume = rt2x00pci_resume,
  1563. };
  1564. static int __init rt2400pci_init(void)
  1565. {
  1566. return pci_register_driver(&rt2400pci_driver);
  1567. }
  1568. static void __exit rt2400pci_exit(void)
  1569. {
  1570. pci_unregister_driver(&rt2400pci_driver);
  1571. }
  1572. module_init(rt2400pci_init);
  1573. module_exit(rt2400pci_exit);