zd_chip.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* This file implements all the hardware specific functions for the ZD1211
  21. * and ZD1211B chips. Support for the ZD1211B was possible after Timothy
  22. * Legge sent me a ZD1211B device. Thank you Tim. -- Uli
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include "zd_def.h"
  27. #include "zd_chip.h"
  28. #include "zd_mac.h"
  29. #include "zd_rf.h"
  30. void zd_chip_init(struct zd_chip *chip,
  31. struct ieee80211_hw *hw,
  32. struct usb_interface *intf)
  33. {
  34. memset(chip, 0, sizeof(*chip));
  35. mutex_init(&chip->mutex);
  36. zd_usb_init(&chip->usb, hw, intf);
  37. zd_rf_init(&chip->rf);
  38. }
  39. void zd_chip_clear(struct zd_chip *chip)
  40. {
  41. ZD_ASSERT(!mutex_is_locked(&chip->mutex));
  42. zd_usb_clear(&chip->usb);
  43. zd_rf_clear(&chip->rf);
  44. mutex_destroy(&chip->mutex);
  45. ZD_MEMCLEAR(chip, sizeof(*chip));
  46. }
  47. static int scnprint_mac_oui(struct zd_chip *chip, char *buffer, size_t size)
  48. {
  49. u8 *addr = zd_mac_get_perm_addr(zd_chip_to_mac(chip));
  50. return scnprintf(buffer, size, "%02x-%02x-%02x",
  51. addr[0], addr[1], addr[2]);
  52. }
  53. /* Prints an identifier line, which will support debugging. */
  54. static int scnprint_id(struct zd_chip *chip, char *buffer, size_t size)
  55. {
  56. int i = 0;
  57. i = scnprintf(buffer, size, "zd1211%s chip ",
  58. zd_chip_is_zd1211b(chip) ? "b" : "");
  59. i += zd_usb_scnprint_id(&chip->usb, buffer+i, size-i);
  60. i += scnprintf(buffer+i, size-i, " ");
  61. i += scnprint_mac_oui(chip, buffer+i, size-i);
  62. i += scnprintf(buffer+i, size-i, " ");
  63. i += zd_rf_scnprint_id(&chip->rf, buffer+i, size-i);
  64. i += scnprintf(buffer+i, size-i, " pa%1x %c%c%c%c%c", chip->pa_type,
  65. chip->patch_cck_gain ? 'g' : '-',
  66. chip->patch_cr157 ? '7' : '-',
  67. chip->patch_6m_band_edge ? '6' : '-',
  68. chip->new_phy_layout ? 'N' : '-',
  69. chip->al2230s_bit ? 'S' : '-');
  70. return i;
  71. }
  72. static void print_id(struct zd_chip *chip)
  73. {
  74. char buffer[80];
  75. scnprint_id(chip, buffer, sizeof(buffer));
  76. buffer[sizeof(buffer)-1] = 0;
  77. dev_info(zd_chip_dev(chip), "%s\n", buffer);
  78. }
  79. static zd_addr_t inc_addr(zd_addr_t addr)
  80. {
  81. u16 a = (u16)addr;
  82. /* Control registers use byte addressing, but everything else uses word
  83. * addressing. */
  84. if ((a & 0xf000) == CR_START)
  85. a += 2;
  86. else
  87. a += 1;
  88. return (zd_addr_t)a;
  89. }
  90. /* Read a variable number of 32-bit values. Parameter count is not allowed to
  91. * exceed USB_MAX_IOREAD32_COUNT.
  92. */
  93. int zd_ioread32v_locked(struct zd_chip *chip, u32 *values, const zd_addr_t *addr,
  94. unsigned int count)
  95. {
  96. int r;
  97. int i;
  98. zd_addr_t *a16;
  99. u16 *v16;
  100. unsigned int count16;
  101. if (count > USB_MAX_IOREAD32_COUNT)
  102. return -EINVAL;
  103. /* Allocate a single memory block for values and addresses. */
  104. count16 = 2*count;
  105. a16 = (zd_addr_t *) kmalloc(count16 * (sizeof(zd_addr_t) + sizeof(u16)),
  106. GFP_KERNEL);
  107. if (!a16) {
  108. dev_dbg_f(zd_chip_dev(chip),
  109. "error ENOMEM in allocation of a16\n");
  110. r = -ENOMEM;
  111. goto out;
  112. }
  113. v16 = (u16 *)(a16 + count16);
  114. for (i = 0; i < count; i++) {
  115. int j = 2*i;
  116. /* We read the high word always first. */
  117. a16[j] = inc_addr(addr[i]);
  118. a16[j+1] = addr[i];
  119. }
  120. r = zd_ioread16v_locked(chip, v16, a16, count16);
  121. if (r) {
  122. dev_dbg_f(zd_chip_dev(chip),
  123. "error: zd_ioread16v_locked. Error number %d\n", r);
  124. goto out;
  125. }
  126. for (i = 0; i < count; i++) {
  127. int j = 2*i;
  128. values[i] = (v16[j] << 16) | v16[j+1];
  129. }
  130. out:
  131. kfree((void *)a16);
  132. return r;
  133. }
  134. int _zd_iowrite32v_locked(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  135. unsigned int count)
  136. {
  137. int i, j, r;
  138. struct zd_ioreq16 *ioreqs16;
  139. unsigned int count16;
  140. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  141. if (count == 0)
  142. return 0;
  143. if (count > USB_MAX_IOWRITE32_COUNT)
  144. return -EINVAL;
  145. /* Allocate a single memory block for values and addresses. */
  146. count16 = 2*count;
  147. ioreqs16 = kmalloc(count16 * sizeof(struct zd_ioreq16), GFP_KERNEL);
  148. if (!ioreqs16) {
  149. r = -ENOMEM;
  150. dev_dbg_f(zd_chip_dev(chip),
  151. "error %d in ioreqs16 allocation\n", r);
  152. goto out;
  153. }
  154. for (i = 0; i < count; i++) {
  155. j = 2*i;
  156. /* We write the high word always first. */
  157. ioreqs16[j].value = ioreqs[i].value >> 16;
  158. ioreqs16[j].addr = inc_addr(ioreqs[i].addr);
  159. ioreqs16[j+1].value = ioreqs[i].value;
  160. ioreqs16[j+1].addr = ioreqs[i].addr;
  161. }
  162. r = zd_usb_iowrite16v(&chip->usb, ioreqs16, count16);
  163. #ifdef DEBUG
  164. if (r) {
  165. dev_dbg_f(zd_chip_dev(chip),
  166. "error %d in zd_usb_write16v\n", r);
  167. }
  168. #endif /* DEBUG */
  169. out:
  170. kfree(ioreqs16);
  171. return r;
  172. }
  173. int zd_iowrite16a_locked(struct zd_chip *chip,
  174. const struct zd_ioreq16 *ioreqs, unsigned int count)
  175. {
  176. int r;
  177. unsigned int i, j, t, max;
  178. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  179. for (i = 0; i < count; i += j + t) {
  180. t = 0;
  181. max = count-i;
  182. if (max > USB_MAX_IOWRITE16_COUNT)
  183. max = USB_MAX_IOWRITE16_COUNT;
  184. for (j = 0; j < max; j++) {
  185. if (!ioreqs[i+j].addr) {
  186. t = 1;
  187. break;
  188. }
  189. }
  190. r = zd_usb_iowrite16v(&chip->usb, &ioreqs[i], j);
  191. if (r) {
  192. dev_dbg_f(zd_chip_dev(chip),
  193. "error zd_usb_iowrite16v. Error number %d\n",
  194. r);
  195. return r;
  196. }
  197. }
  198. return 0;
  199. }
  200. /* Writes a variable number of 32 bit registers. The functions will split
  201. * that in several USB requests. A split can be forced by inserting an IO
  202. * request with an zero address field.
  203. */
  204. int zd_iowrite32a_locked(struct zd_chip *chip,
  205. const struct zd_ioreq32 *ioreqs, unsigned int count)
  206. {
  207. int r;
  208. unsigned int i, j, t, max;
  209. for (i = 0; i < count; i += j + t) {
  210. t = 0;
  211. max = count-i;
  212. if (max > USB_MAX_IOWRITE32_COUNT)
  213. max = USB_MAX_IOWRITE32_COUNT;
  214. for (j = 0; j < max; j++) {
  215. if (!ioreqs[i+j].addr) {
  216. t = 1;
  217. break;
  218. }
  219. }
  220. r = _zd_iowrite32v_locked(chip, &ioreqs[i], j);
  221. if (r) {
  222. dev_dbg_f(zd_chip_dev(chip),
  223. "error _zd_iowrite32v_locked."
  224. " Error number %d\n", r);
  225. return r;
  226. }
  227. }
  228. return 0;
  229. }
  230. int zd_ioread16(struct zd_chip *chip, zd_addr_t addr, u16 *value)
  231. {
  232. int r;
  233. mutex_lock(&chip->mutex);
  234. r = zd_ioread16_locked(chip, value, addr);
  235. mutex_unlock(&chip->mutex);
  236. return r;
  237. }
  238. int zd_ioread32(struct zd_chip *chip, zd_addr_t addr, u32 *value)
  239. {
  240. int r;
  241. mutex_lock(&chip->mutex);
  242. r = zd_ioread32_locked(chip, value, addr);
  243. mutex_unlock(&chip->mutex);
  244. return r;
  245. }
  246. int zd_iowrite16(struct zd_chip *chip, zd_addr_t addr, u16 value)
  247. {
  248. int r;
  249. mutex_lock(&chip->mutex);
  250. r = zd_iowrite16_locked(chip, value, addr);
  251. mutex_unlock(&chip->mutex);
  252. return r;
  253. }
  254. int zd_iowrite32(struct zd_chip *chip, zd_addr_t addr, u32 value)
  255. {
  256. int r;
  257. mutex_lock(&chip->mutex);
  258. r = zd_iowrite32_locked(chip, value, addr);
  259. mutex_unlock(&chip->mutex);
  260. return r;
  261. }
  262. int zd_ioread32v(struct zd_chip *chip, const zd_addr_t *addresses,
  263. u32 *values, unsigned int count)
  264. {
  265. int r;
  266. mutex_lock(&chip->mutex);
  267. r = zd_ioread32v_locked(chip, values, addresses, count);
  268. mutex_unlock(&chip->mutex);
  269. return r;
  270. }
  271. int zd_iowrite32a(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  272. unsigned int count)
  273. {
  274. int r;
  275. mutex_lock(&chip->mutex);
  276. r = zd_iowrite32a_locked(chip, ioreqs, count);
  277. mutex_unlock(&chip->mutex);
  278. return r;
  279. }
  280. static int read_pod(struct zd_chip *chip, u8 *rf_type)
  281. {
  282. int r;
  283. u32 value;
  284. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  285. r = zd_ioread32_locked(chip, &value, E2P_POD);
  286. if (r)
  287. goto error;
  288. dev_dbg_f(zd_chip_dev(chip), "E2P_POD %#010x\n", value);
  289. /* FIXME: AL2230 handling (Bit 7 in POD) */
  290. *rf_type = value & 0x0f;
  291. chip->pa_type = (value >> 16) & 0x0f;
  292. chip->patch_cck_gain = (value >> 8) & 0x1;
  293. chip->patch_cr157 = (value >> 13) & 0x1;
  294. chip->patch_6m_band_edge = (value >> 21) & 0x1;
  295. chip->new_phy_layout = (value >> 31) & 0x1;
  296. chip->al2230s_bit = (value >> 7) & 0x1;
  297. chip->link_led = ((value >> 4) & 1) ? LED1 : LED2;
  298. chip->supports_tx_led = 1;
  299. if (value & (1 << 24)) { /* LED scenario */
  300. if (value & (1 << 29))
  301. chip->supports_tx_led = 0;
  302. }
  303. dev_dbg_f(zd_chip_dev(chip),
  304. "RF %s %#01x PA type %#01x patch CCK %d patch CR157 %d "
  305. "patch 6M %d new PHY %d link LED%d tx led %d\n",
  306. zd_rf_name(*rf_type), *rf_type,
  307. chip->pa_type, chip->patch_cck_gain,
  308. chip->patch_cr157, chip->patch_6m_band_edge,
  309. chip->new_phy_layout,
  310. chip->link_led == LED1 ? 1 : 2,
  311. chip->supports_tx_led);
  312. return 0;
  313. error:
  314. *rf_type = 0;
  315. chip->pa_type = 0;
  316. chip->patch_cck_gain = 0;
  317. chip->patch_cr157 = 0;
  318. chip->patch_6m_band_edge = 0;
  319. chip->new_phy_layout = 0;
  320. return r;
  321. }
  322. /* MAC address: if custom mac addresses are to be used CR_MAC_ADDR_P1 and
  323. * CR_MAC_ADDR_P2 must be overwritten
  324. */
  325. int zd_write_mac_addr(struct zd_chip *chip, const u8 *mac_addr)
  326. {
  327. int r;
  328. struct zd_ioreq32 reqs[2] = {
  329. [0] = { .addr = CR_MAC_ADDR_P1 },
  330. [1] = { .addr = CR_MAC_ADDR_P2 },
  331. };
  332. if (mac_addr) {
  333. reqs[0].value = (mac_addr[3] << 24)
  334. | (mac_addr[2] << 16)
  335. | (mac_addr[1] << 8)
  336. | mac_addr[0];
  337. reqs[1].value = (mac_addr[5] << 8)
  338. | mac_addr[4];
  339. dev_dbg_f(zd_chip_dev(chip), "mac addr %pM\n", mac_addr);
  340. } else {
  341. dev_dbg_f(zd_chip_dev(chip), "set NULL mac\n");
  342. }
  343. mutex_lock(&chip->mutex);
  344. r = zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  345. mutex_unlock(&chip->mutex);
  346. return r;
  347. }
  348. int zd_read_regdomain(struct zd_chip *chip, u8 *regdomain)
  349. {
  350. int r;
  351. u32 value;
  352. mutex_lock(&chip->mutex);
  353. r = zd_ioread32_locked(chip, &value, E2P_SUBID);
  354. mutex_unlock(&chip->mutex);
  355. if (r)
  356. return r;
  357. *regdomain = value >> 16;
  358. dev_dbg_f(zd_chip_dev(chip), "regdomain: %#04x\n", *regdomain);
  359. return 0;
  360. }
  361. static int read_values(struct zd_chip *chip, u8 *values, size_t count,
  362. zd_addr_t e2p_addr, u32 guard)
  363. {
  364. int r;
  365. int i;
  366. u32 v;
  367. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  368. for (i = 0;;) {
  369. r = zd_ioread32_locked(chip, &v,
  370. (zd_addr_t)((u16)e2p_addr+i/2));
  371. if (r)
  372. return r;
  373. v -= guard;
  374. if (i+4 < count) {
  375. values[i++] = v;
  376. values[i++] = v >> 8;
  377. values[i++] = v >> 16;
  378. values[i++] = v >> 24;
  379. continue;
  380. }
  381. for (;i < count; i++)
  382. values[i] = v >> (8*(i%3));
  383. return 0;
  384. }
  385. }
  386. static int read_pwr_cal_values(struct zd_chip *chip)
  387. {
  388. return read_values(chip, chip->pwr_cal_values,
  389. E2P_CHANNEL_COUNT, E2P_PWR_CAL_VALUE1,
  390. 0);
  391. }
  392. static int read_pwr_int_values(struct zd_chip *chip)
  393. {
  394. return read_values(chip, chip->pwr_int_values,
  395. E2P_CHANNEL_COUNT, E2P_PWR_INT_VALUE1,
  396. E2P_PWR_INT_GUARD);
  397. }
  398. static int read_ofdm_cal_values(struct zd_chip *chip)
  399. {
  400. int r;
  401. int i;
  402. static const zd_addr_t addresses[] = {
  403. E2P_36M_CAL_VALUE1,
  404. E2P_48M_CAL_VALUE1,
  405. E2P_54M_CAL_VALUE1,
  406. };
  407. for (i = 0; i < 3; i++) {
  408. r = read_values(chip, chip->ofdm_cal_values[i],
  409. E2P_CHANNEL_COUNT, addresses[i], 0);
  410. if (r)
  411. return r;
  412. }
  413. return 0;
  414. }
  415. static int read_cal_int_tables(struct zd_chip *chip)
  416. {
  417. int r;
  418. r = read_pwr_cal_values(chip);
  419. if (r)
  420. return r;
  421. r = read_pwr_int_values(chip);
  422. if (r)
  423. return r;
  424. r = read_ofdm_cal_values(chip);
  425. if (r)
  426. return r;
  427. return 0;
  428. }
  429. /* phy means physical registers */
  430. int zd_chip_lock_phy_regs(struct zd_chip *chip)
  431. {
  432. int r;
  433. u32 tmp;
  434. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  435. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  436. if (r) {
  437. dev_err(zd_chip_dev(chip), "error ioread32(CR_REG1): %d\n", r);
  438. return r;
  439. }
  440. tmp &= ~UNLOCK_PHY_REGS;
  441. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  442. if (r)
  443. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  444. return r;
  445. }
  446. int zd_chip_unlock_phy_regs(struct zd_chip *chip)
  447. {
  448. int r;
  449. u32 tmp;
  450. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  451. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  452. if (r) {
  453. dev_err(zd_chip_dev(chip),
  454. "error ioread32(CR_REG1): %d\n", r);
  455. return r;
  456. }
  457. tmp |= UNLOCK_PHY_REGS;
  458. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  459. if (r)
  460. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  461. return r;
  462. }
  463. /* CR157 can be optionally patched by the EEPROM for original ZD1211 */
  464. static int patch_cr157(struct zd_chip *chip)
  465. {
  466. int r;
  467. u16 value;
  468. if (!chip->patch_cr157)
  469. return 0;
  470. r = zd_ioread16_locked(chip, &value, E2P_PHY_REG);
  471. if (r)
  472. return r;
  473. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value >> 8);
  474. return zd_iowrite32_locked(chip, value >> 8, CR157);
  475. }
  476. /*
  477. * 6M band edge can be optionally overwritten for certain RF's
  478. * Vendor driver says: for FCC regulation, enabled per HWFeature 6M band edge
  479. * bit (for AL2230, AL2230S)
  480. */
  481. static int patch_6m_band_edge(struct zd_chip *chip, u8 channel)
  482. {
  483. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  484. if (!chip->patch_6m_band_edge)
  485. return 0;
  486. return zd_rf_patch_6m_band_edge(&chip->rf, channel);
  487. }
  488. /* Generic implementation of 6M band edge patching, used by most RFs via
  489. * zd_rf_generic_patch_6m() */
  490. int zd_chip_generic_patch_6m_band(struct zd_chip *chip, int channel)
  491. {
  492. struct zd_ioreq16 ioreqs[] = {
  493. { CR128, 0x14 }, { CR129, 0x12 }, { CR130, 0x10 },
  494. { CR47, 0x1e },
  495. };
  496. /* FIXME: Channel 11 is not the edge for all regulatory domains. */
  497. if (channel == 1 || channel == 11)
  498. ioreqs[0].value = 0x12;
  499. dev_dbg_f(zd_chip_dev(chip), "patching for channel %d\n", channel);
  500. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  501. }
  502. static int zd1211_hw_reset_phy(struct zd_chip *chip)
  503. {
  504. static const struct zd_ioreq16 ioreqs[] = {
  505. { CR0, 0x0a }, { CR1, 0x06 }, { CR2, 0x26 },
  506. { CR3, 0x38 }, { CR4, 0x80 }, { CR9, 0xa0 },
  507. { CR10, 0x81 }, { CR11, 0x00 }, { CR12, 0x7f },
  508. { CR13, 0x8c }, { CR14, 0x80 }, { CR15, 0x3d },
  509. { CR16, 0x20 }, { CR17, 0x1e }, { CR18, 0x0a },
  510. { CR19, 0x48 }, { CR20, 0x0c }, { CR21, 0x0c },
  511. { CR22, 0x23 }, { CR23, 0x90 }, { CR24, 0x14 },
  512. { CR25, 0x40 }, { CR26, 0x10 }, { CR27, 0x19 },
  513. { CR28, 0x7f }, { CR29, 0x80 }, { CR30, 0x4b },
  514. { CR31, 0x60 }, { CR32, 0x43 }, { CR33, 0x08 },
  515. { CR34, 0x06 }, { CR35, 0x0a }, { CR36, 0x00 },
  516. { CR37, 0x00 }, { CR38, 0x38 }, { CR39, 0x0c },
  517. { CR40, 0x84 }, { CR41, 0x2a }, { CR42, 0x80 },
  518. { CR43, 0x10 }, { CR44, 0x12 }, { CR46, 0xff },
  519. { CR47, 0x1E }, { CR48, 0x26 }, { CR49, 0x5b },
  520. { CR64, 0xd0 }, { CR65, 0x04 }, { CR66, 0x58 },
  521. { CR67, 0xc9 }, { CR68, 0x88 }, { CR69, 0x41 },
  522. { CR70, 0x23 }, { CR71, 0x10 }, { CR72, 0xff },
  523. { CR73, 0x32 }, { CR74, 0x30 }, { CR75, 0x65 },
  524. { CR76, 0x41 }, { CR77, 0x1b }, { CR78, 0x30 },
  525. { CR79, 0x68 }, { CR80, 0x64 }, { CR81, 0x64 },
  526. { CR82, 0x00 }, { CR83, 0x00 }, { CR84, 0x00 },
  527. { CR85, 0x02 }, { CR86, 0x00 }, { CR87, 0x00 },
  528. { CR88, 0xff }, { CR89, 0xfc }, { CR90, 0x00 },
  529. { CR91, 0x00 }, { CR92, 0x00 }, { CR93, 0x08 },
  530. { CR94, 0x00 }, { CR95, 0x00 }, { CR96, 0xff },
  531. { CR97, 0xe7 }, { CR98, 0x00 }, { CR99, 0x00 },
  532. { CR100, 0x00 }, { CR101, 0xae }, { CR102, 0x02 },
  533. { CR103, 0x00 }, { CR104, 0x03 }, { CR105, 0x65 },
  534. { CR106, 0x04 }, { CR107, 0x00 }, { CR108, 0x0a },
  535. { CR109, 0xaa }, { CR110, 0xaa }, { CR111, 0x25 },
  536. { CR112, 0x25 }, { CR113, 0x00 }, { CR119, 0x1e },
  537. { CR125, 0x90 }, { CR126, 0x00 }, { CR127, 0x00 },
  538. { },
  539. { CR5, 0x00 }, { CR6, 0x00 }, { CR7, 0x00 },
  540. { CR8, 0x00 }, { CR9, 0x20 }, { CR12, 0xf0 },
  541. { CR20, 0x0e }, { CR21, 0x0e }, { CR27, 0x10 },
  542. { CR44, 0x33 }, { CR47, 0x1E }, { CR83, 0x24 },
  543. { CR84, 0x04 }, { CR85, 0x00 }, { CR86, 0x0C },
  544. { CR87, 0x12 }, { CR88, 0x0C }, { CR89, 0x00 },
  545. { CR90, 0x10 }, { CR91, 0x08 }, { CR93, 0x00 },
  546. { CR94, 0x01 }, { CR95, 0x00 }, { CR96, 0x50 },
  547. { CR97, 0x37 }, { CR98, 0x35 }, { CR101, 0x13 },
  548. { CR102, 0x27 }, { CR103, 0x27 }, { CR104, 0x18 },
  549. { CR105, 0x12 }, { CR109, 0x27 }, { CR110, 0x27 },
  550. { CR111, 0x27 }, { CR112, 0x27 }, { CR113, 0x27 },
  551. { CR114, 0x27 }, { CR115, 0x26 }, { CR116, 0x24 },
  552. { CR117, 0xfc }, { CR118, 0xfa }, { CR120, 0x4f },
  553. { CR125, 0xaa }, { CR127, 0x03 }, { CR128, 0x14 },
  554. { CR129, 0x12 }, { CR130, 0x10 }, { CR131, 0x0C },
  555. { CR136, 0xdf }, { CR137, 0x40 }, { CR138, 0xa0 },
  556. { CR139, 0xb0 }, { CR140, 0x99 }, { CR141, 0x82 },
  557. { CR142, 0x54 }, { CR143, 0x1c }, { CR144, 0x6c },
  558. { CR147, 0x07 }, { CR148, 0x4c }, { CR149, 0x50 },
  559. { CR150, 0x0e }, { CR151, 0x18 }, { CR160, 0xfe },
  560. { CR161, 0xee }, { CR162, 0xaa }, { CR163, 0xfa },
  561. { CR164, 0xfa }, { CR165, 0xea }, { CR166, 0xbe },
  562. { CR167, 0xbe }, { CR168, 0x6a }, { CR169, 0xba },
  563. { CR170, 0xba }, { CR171, 0xba },
  564. /* Note: CR204 must lead the CR203 */
  565. { CR204, 0x7d },
  566. { },
  567. { CR203, 0x30 },
  568. };
  569. int r, t;
  570. dev_dbg_f(zd_chip_dev(chip), "\n");
  571. r = zd_chip_lock_phy_regs(chip);
  572. if (r)
  573. goto out;
  574. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  575. if (r)
  576. goto unlock;
  577. r = patch_cr157(chip);
  578. unlock:
  579. t = zd_chip_unlock_phy_regs(chip);
  580. if (t && !r)
  581. r = t;
  582. out:
  583. return r;
  584. }
  585. static int zd1211b_hw_reset_phy(struct zd_chip *chip)
  586. {
  587. static const struct zd_ioreq16 ioreqs[] = {
  588. { CR0, 0x14 }, { CR1, 0x06 }, { CR2, 0x26 },
  589. { CR3, 0x38 }, { CR4, 0x80 }, { CR9, 0xe0 },
  590. { CR10, 0x81 },
  591. /* power control { { CR11, 1 << 6 }, */
  592. { CR11, 0x00 },
  593. { CR12, 0xf0 }, { CR13, 0x8c }, { CR14, 0x80 },
  594. { CR15, 0x3d }, { CR16, 0x20 }, { CR17, 0x1e },
  595. { CR18, 0x0a }, { CR19, 0x48 },
  596. { CR20, 0x10 }, /* Org:0x0E, ComTrend:RalLink AP */
  597. { CR21, 0x0e }, { CR22, 0x23 }, { CR23, 0x90 },
  598. { CR24, 0x14 }, { CR25, 0x40 }, { CR26, 0x10 },
  599. { CR27, 0x10 }, { CR28, 0x7f }, { CR29, 0x80 },
  600. { CR30, 0x4b }, /* ASIC/FWT, no jointly decoder */
  601. { CR31, 0x60 }, { CR32, 0x43 }, { CR33, 0x08 },
  602. { CR34, 0x06 }, { CR35, 0x0a }, { CR36, 0x00 },
  603. { CR37, 0x00 }, { CR38, 0x38 }, { CR39, 0x0c },
  604. { CR40, 0x84 }, { CR41, 0x2a }, { CR42, 0x80 },
  605. { CR43, 0x10 }, { CR44, 0x33 }, { CR46, 0xff },
  606. { CR47, 0x1E }, { CR48, 0x26 }, { CR49, 0x5b },
  607. { CR64, 0xd0 }, { CR65, 0x04 }, { CR66, 0x58 },
  608. { CR67, 0xc9 }, { CR68, 0x88 }, { CR69, 0x41 },
  609. { CR70, 0x23 }, { CR71, 0x10 }, { CR72, 0xff },
  610. { CR73, 0x32 }, { CR74, 0x30 }, { CR75, 0x65 },
  611. { CR76, 0x41 }, { CR77, 0x1b }, { CR78, 0x30 },
  612. { CR79, 0xf0 }, { CR80, 0x64 }, { CR81, 0x64 },
  613. { CR82, 0x00 }, { CR83, 0x24 }, { CR84, 0x04 },
  614. { CR85, 0x00 }, { CR86, 0x0c }, { CR87, 0x12 },
  615. { CR88, 0x0c }, { CR89, 0x00 }, { CR90, 0x58 },
  616. { CR91, 0x04 }, { CR92, 0x00 }, { CR93, 0x00 },
  617. { CR94, 0x01 },
  618. { CR95, 0x20 }, /* ZD1211B */
  619. { CR96, 0x50 }, { CR97, 0x37 }, { CR98, 0x35 },
  620. { CR99, 0x00 }, { CR100, 0x01 }, { CR101, 0x13 },
  621. { CR102, 0x27 }, { CR103, 0x27 }, { CR104, 0x18 },
  622. { CR105, 0x12 }, { CR106, 0x04 }, { CR107, 0x00 },
  623. { CR108, 0x0a }, { CR109, 0x27 }, { CR110, 0x27 },
  624. { CR111, 0x27 }, { CR112, 0x27 }, { CR113, 0x27 },
  625. { CR114, 0x27 }, { CR115, 0x26 }, { CR116, 0x24 },
  626. { CR117, 0xfc }, { CR118, 0xfa }, { CR119, 0x1e },
  627. { CR125, 0x90 }, { CR126, 0x00 }, { CR127, 0x00 },
  628. { CR128, 0x14 }, { CR129, 0x12 }, { CR130, 0x10 },
  629. { CR131, 0x0c }, { CR136, 0xdf }, { CR137, 0xa0 },
  630. { CR138, 0xa8 }, { CR139, 0xb4 }, { CR140, 0x98 },
  631. { CR141, 0x82 }, { CR142, 0x53 }, { CR143, 0x1c },
  632. { CR144, 0x6c }, { CR147, 0x07 }, { CR148, 0x40 },
  633. { CR149, 0x40 }, /* Org:0x50 ComTrend:RalLink AP */
  634. { CR150, 0x14 }, /* Org:0x0E ComTrend:RalLink AP */
  635. { CR151, 0x18 }, { CR159, 0x70 }, { CR160, 0xfe },
  636. { CR161, 0xee }, { CR162, 0xaa }, { CR163, 0xfa },
  637. { CR164, 0xfa }, { CR165, 0xea }, { CR166, 0xbe },
  638. { CR167, 0xbe }, { CR168, 0x6a }, { CR169, 0xba },
  639. { CR170, 0xba }, { CR171, 0xba },
  640. /* Note: CR204 must lead the CR203 */
  641. { CR204, 0x7d },
  642. {},
  643. { CR203, 0x30 },
  644. };
  645. int r, t;
  646. dev_dbg_f(zd_chip_dev(chip), "\n");
  647. r = zd_chip_lock_phy_regs(chip);
  648. if (r)
  649. goto out;
  650. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  651. t = zd_chip_unlock_phy_regs(chip);
  652. if (t && !r)
  653. r = t;
  654. out:
  655. return r;
  656. }
  657. static int hw_reset_phy(struct zd_chip *chip)
  658. {
  659. return zd_chip_is_zd1211b(chip) ? zd1211b_hw_reset_phy(chip) :
  660. zd1211_hw_reset_phy(chip);
  661. }
  662. static int zd1211_hw_init_hmac(struct zd_chip *chip)
  663. {
  664. static const struct zd_ioreq32 ioreqs[] = {
  665. { CR_ZD1211_RETRY_MAX, ZD1211_RETRY_COUNT },
  666. { CR_RX_THRESHOLD, 0x000c0640 },
  667. };
  668. dev_dbg_f(zd_chip_dev(chip), "\n");
  669. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  670. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  671. }
  672. static int zd1211b_hw_init_hmac(struct zd_chip *chip)
  673. {
  674. static const struct zd_ioreq32 ioreqs[] = {
  675. { CR_ZD1211B_RETRY_MAX, ZD1211B_RETRY_COUNT },
  676. { CR_ZD1211B_CWIN_MAX_MIN_AC0, 0x007f003f },
  677. { CR_ZD1211B_CWIN_MAX_MIN_AC1, 0x007f003f },
  678. { CR_ZD1211B_CWIN_MAX_MIN_AC2, 0x003f001f },
  679. { CR_ZD1211B_CWIN_MAX_MIN_AC3, 0x001f000f },
  680. { CR_ZD1211B_AIFS_CTL1, 0x00280028 },
  681. { CR_ZD1211B_AIFS_CTL2, 0x008C003C },
  682. { CR_ZD1211B_TXOP, 0x01800824 },
  683. { CR_RX_THRESHOLD, 0x000c0eff, },
  684. };
  685. dev_dbg_f(zd_chip_dev(chip), "\n");
  686. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  687. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  688. }
  689. static int hw_init_hmac(struct zd_chip *chip)
  690. {
  691. int r;
  692. static const struct zd_ioreq32 ioreqs[] = {
  693. { CR_ACK_TIMEOUT_EXT, 0x20 },
  694. { CR_ADDA_MBIAS_WARMTIME, 0x30000808 },
  695. { CR_SNIFFER_ON, 0 },
  696. { CR_RX_FILTER, STA_RX_FILTER },
  697. { CR_GROUP_HASH_P1, 0x00 },
  698. { CR_GROUP_HASH_P2, 0x80000000 },
  699. { CR_REG1, 0xa4 },
  700. { CR_ADDA_PWR_DWN, 0x7f },
  701. { CR_BCN_PLCP_CFG, 0x00f00401 },
  702. { CR_PHY_DELAY, 0x00 },
  703. { CR_ACK_TIMEOUT_EXT, 0x80 },
  704. { CR_ADDA_PWR_DWN, 0x00 },
  705. { CR_ACK_TIME_80211, 0x100 },
  706. { CR_RX_PE_DELAY, 0x70 },
  707. { CR_PS_CTRL, 0x10000000 },
  708. { CR_RTS_CTS_RATE, 0x02030203 },
  709. { CR_AFTER_PNP, 0x1 },
  710. { CR_WEP_PROTECT, 0x114 },
  711. { CR_IFS_VALUE, IFS_VALUE_DEFAULT },
  712. { CR_CAM_MODE, MODE_AP_WDS},
  713. };
  714. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  715. r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  716. if (r)
  717. return r;
  718. return zd_chip_is_zd1211b(chip) ?
  719. zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip);
  720. }
  721. struct aw_pt_bi {
  722. u32 atim_wnd_period;
  723. u32 pre_tbtt;
  724. u32 beacon_interval;
  725. };
  726. static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  727. {
  728. int r;
  729. static const zd_addr_t aw_pt_bi_addr[] =
  730. { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL };
  731. u32 values[3];
  732. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  733. ARRAY_SIZE(aw_pt_bi_addr));
  734. if (r) {
  735. memset(s, 0, sizeof(*s));
  736. return r;
  737. }
  738. s->atim_wnd_period = values[0];
  739. s->pre_tbtt = values[1];
  740. s->beacon_interval = values[2];
  741. return 0;
  742. }
  743. static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  744. {
  745. struct zd_ioreq32 reqs[3];
  746. if (s->beacon_interval <= 5)
  747. s->beacon_interval = 5;
  748. if (s->pre_tbtt < 4 || s->pre_tbtt >= s->beacon_interval)
  749. s->pre_tbtt = s->beacon_interval - 1;
  750. if (s->atim_wnd_period >= s->pre_tbtt)
  751. s->atim_wnd_period = s->pre_tbtt - 1;
  752. reqs[0].addr = CR_ATIM_WND_PERIOD;
  753. reqs[0].value = s->atim_wnd_period;
  754. reqs[1].addr = CR_PRE_TBTT;
  755. reqs[1].value = s->pre_tbtt;
  756. reqs[2].addr = CR_BCN_INTERVAL;
  757. reqs[2].value = s->beacon_interval;
  758. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  759. }
  760. static int set_beacon_interval(struct zd_chip *chip, u32 interval)
  761. {
  762. int r;
  763. struct aw_pt_bi s;
  764. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  765. r = get_aw_pt_bi(chip, &s);
  766. if (r)
  767. return r;
  768. s.beacon_interval = interval;
  769. return set_aw_pt_bi(chip, &s);
  770. }
  771. int zd_set_beacon_interval(struct zd_chip *chip, u32 interval)
  772. {
  773. int r;
  774. mutex_lock(&chip->mutex);
  775. r = set_beacon_interval(chip, interval);
  776. mutex_unlock(&chip->mutex);
  777. return r;
  778. }
  779. static int hw_init(struct zd_chip *chip)
  780. {
  781. int r;
  782. dev_dbg_f(zd_chip_dev(chip), "\n");
  783. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  784. r = hw_reset_phy(chip);
  785. if (r)
  786. return r;
  787. r = hw_init_hmac(chip);
  788. if (r)
  789. return r;
  790. return set_beacon_interval(chip, 100);
  791. }
  792. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  793. {
  794. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  795. }
  796. #ifdef DEBUG
  797. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  798. const char *addr_string)
  799. {
  800. int r;
  801. u32 value;
  802. r = zd_ioread32_locked(chip, &value, addr);
  803. if (r) {
  804. dev_dbg_f(zd_chip_dev(chip),
  805. "error reading %s. Error number %d\n", addr_string, r);
  806. return r;
  807. }
  808. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  809. addr_string, (unsigned int)value);
  810. return 0;
  811. }
  812. static int test_init(struct zd_chip *chip)
  813. {
  814. int r;
  815. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  816. if (r)
  817. return r;
  818. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  819. if (r)
  820. return r;
  821. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  822. }
  823. static void dump_fw_registers(struct zd_chip *chip)
  824. {
  825. const zd_addr_t addr[4] = {
  826. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  827. fw_reg_addr(chip, FW_REG_USB_SPEED),
  828. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  829. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  830. };
  831. int r;
  832. u16 values[4];
  833. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  834. ARRAY_SIZE(addr));
  835. if (r) {
  836. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  837. r);
  838. return;
  839. }
  840. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  841. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  842. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  843. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  844. }
  845. #endif /* DEBUG */
  846. static int print_fw_version(struct zd_chip *chip)
  847. {
  848. int r;
  849. u16 version;
  850. r = zd_ioread16_locked(chip, &version,
  851. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  852. if (r)
  853. return r;
  854. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  855. return 0;
  856. }
  857. static int set_mandatory_rates(struct zd_chip *chip, int gmode)
  858. {
  859. u32 rates;
  860. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  861. /* This sets the mandatory rates, which only depend from the standard
  862. * that the device is supporting. Until further notice we should try
  863. * to support 802.11g also for full speed USB.
  864. */
  865. if (!gmode)
  866. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  867. else
  868. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  869. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  870. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  871. }
  872. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  873. int preamble)
  874. {
  875. u32 value = 0;
  876. dev_dbg_f(zd_chip_dev(chip), "preamble=%x\n", preamble);
  877. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  878. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  879. /* We always send 11M RTS/self-CTS messages, like the vendor driver. */
  880. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_RTS_RATE;
  881. value |= ZD_RX_CCK << RTSCTS_SH_RTS_MOD_TYPE;
  882. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE;
  883. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  884. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  885. }
  886. int zd_chip_enable_hwint(struct zd_chip *chip)
  887. {
  888. int r;
  889. mutex_lock(&chip->mutex);
  890. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  891. mutex_unlock(&chip->mutex);
  892. return r;
  893. }
  894. static int disable_hwint(struct zd_chip *chip)
  895. {
  896. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  897. }
  898. int zd_chip_disable_hwint(struct zd_chip *chip)
  899. {
  900. int r;
  901. mutex_lock(&chip->mutex);
  902. r = disable_hwint(chip);
  903. mutex_unlock(&chip->mutex);
  904. return r;
  905. }
  906. static int read_fw_regs_offset(struct zd_chip *chip)
  907. {
  908. int r;
  909. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  910. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  911. FWRAW_REGS_ADDR);
  912. if (r)
  913. return r;
  914. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  915. (u16)chip->fw_regs_base);
  916. return 0;
  917. }
  918. /* Read mac address using pre-firmware interface */
  919. int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr)
  920. {
  921. dev_dbg_f(zd_chip_dev(chip), "\n");
  922. return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr,
  923. ETH_ALEN);
  924. }
  925. int zd_chip_init_hw(struct zd_chip *chip)
  926. {
  927. int r;
  928. u8 rf_type;
  929. dev_dbg_f(zd_chip_dev(chip), "\n");
  930. mutex_lock(&chip->mutex);
  931. #ifdef DEBUG
  932. r = test_init(chip);
  933. if (r)
  934. goto out;
  935. #endif
  936. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  937. if (r)
  938. goto out;
  939. r = read_fw_regs_offset(chip);
  940. if (r)
  941. goto out;
  942. /* GPI is always disabled, also in the other driver.
  943. */
  944. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  945. if (r)
  946. goto out;
  947. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  948. if (r)
  949. goto out;
  950. /* Currently we support IEEE 802.11g for full and high speed USB.
  951. * It might be discussed, whether we should suppport pure b mode for
  952. * full speed USB.
  953. */
  954. r = set_mandatory_rates(chip, 1);
  955. if (r)
  956. goto out;
  957. /* Disabling interrupts is certainly a smart thing here.
  958. */
  959. r = disable_hwint(chip);
  960. if (r)
  961. goto out;
  962. r = read_pod(chip, &rf_type);
  963. if (r)
  964. goto out;
  965. r = hw_init(chip);
  966. if (r)
  967. goto out;
  968. r = zd_rf_init_hw(&chip->rf, rf_type);
  969. if (r)
  970. goto out;
  971. r = print_fw_version(chip);
  972. if (r)
  973. goto out;
  974. #ifdef DEBUG
  975. dump_fw_registers(chip);
  976. r = test_init(chip);
  977. if (r)
  978. goto out;
  979. #endif /* DEBUG */
  980. r = read_cal_int_tables(chip);
  981. if (r)
  982. goto out;
  983. print_id(chip);
  984. out:
  985. mutex_unlock(&chip->mutex);
  986. return r;
  987. }
  988. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  989. {
  990. u8 value = chip->pwr_int_values[channel - 1];
  991. return zd_iowrite16_locked(chip, value, CR31);
  992. }
  993. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  994. {
  995. u8 value = chip->pwr_cal_values[channel-1];
  996. return zd_iowrite16_locked(chip, value, CR68);
  997. }
  998. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  999. {
  1000. struct zd_ioreq16 ioreqs[3];
  1001. ioreqs[0].addr = CR67;
  1002. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1003. ioreqs[1].addr = CR66;
  1004. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1005. ioreqs[2].addr = CR65;
  1006. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1007. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1008. }
  1009. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1010. u8 channel)
  1011. {
  1012. int r;
  1013. if (!zd_rf_should_update_pwr_int(&chip->rf))
  1014. return 0;
  1015. r = update_pwr_int(chip, channel);
  1016. if (r)
  1017. return r;
  1018. if (zd_chip_is_zd1211b(chip)) {
  1019. static const struct zd_ioreq16 ioreqs[] = {
  1020. { CR69, 0x28 },
  1021. {},
  1022. { CR69, 0x2a },
  1023. };
  1024. r = update_ofdm_cal(chip, channel);
  1025. if (r)
  1026. return r;
  1027. r = update_pwr_cal(chip, channel);
  1028. if (r)
  1029. return r;
  1030. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1031. if (r)
  1032. return r;
  1033. }
  1034. return 0;
  1035. }
  1036. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1037. static int patch_cck_gain(struct zd_chip *chip)
  1038. {
  1039. int r;
  1040. u32 value;
  1041. if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf))
  1042. return 0;
  1043. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1044. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1045. if (r)
  1046. return r;
  1047. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1048. return zd_iowrite16_locked(chip, value & 0xff, CR47);
  1049. }
  1050. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1051. {
  1052. int r, t;
  1053. mutex_lock(&chip->mutex);
  1054. r = zd_chip_lock_phy_regs(chip);
  1055. if (r)
  1056. goto out;
  1057. r = zd_rf_set_channel(&chip->rf, channel);
  1058. if (r)
  1059. goto unlock;
  1060. r = update_channel_integration_and_calibration(chip, channel);
  1061. if (r)
  1062. goto unlock;
  1063. r = patch_cck_gain(chip);
  1064. if (r)
  1065. goto unlock;
  1066. r = patch_6m_band_edge(chip, channel);
  1067. if (r)
  1068. goto unlock;
  1069. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1070. unlock:
  1071. t = zd_chip_unlock_phy_regs(chip);
  1072. if (t && !r)
  1073. r = t;
  1074. out:
  1075. mutex_unlock(&chip->mutex);
  1076. return r;
  1077. }
  1078. u8 zd_chip_get_channel(struct zd_chip *chip)
  1079. {
  1080. u8 channel;
  1081. mutex_lock(&chip->mutex);
  1082. channel = chip->rf.channel;
  1083. mutex_unlock(&chip->mutex);
  1084. return channel;
  1085. }
  1086. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1087. {
  1088. const zd_addr_t a[] = {
  1089. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1090. CR_LED,
  1091. };
  1092. int r;
  1093. u16 v[ARRAY_SIZE(a)];
  1094. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1095. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1096. [1] = { CR_LED },
  1097. };
  1098. u16 other_led;
  1099. mutex_lock(&chip->mutex);
  1100. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1101. if (r)
  1102. goto out;
  1103. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1104. switch (status) {
  1105. case ZD_LED_OFF:
  1106. ioreqs[0].value = FW_LINK_OFF;
  1107. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1108. break;
  1109. case ZD_LED_SCANNING:
  1110. ioreqs[0].value = FW_LINK_OFF;
  1111. ioreqs[1].value = v[1] & ~other_led;
  1112. if (get_seconds() % 3 == 0) {
  1113. ioreqs[1].value &= ~chip->link_led;
  1114. } else {
  1115. ioreqs[1].value |= chip->link_led;
  1116. }
  1117. break;
  1118. case ZD_LED_ASSOCIATED:
  1119. ioreqs[0].value = FW_LINK_TX;
  1120. ioreqs[1].value = v[1] & ~other_led;
  1121. ioreqs[1].value |= chip->link_led;
  1122. break;
  1123. default:
  1124. r = -EINVAL;
  1125. goto out;
  1126. }
  1127. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1128. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1129. if (r)
  1130. goto out;
  1131. }
  1132. r = 0;
  1133. out:
  1134. mutex_unlock(&chip->mutex);
  1135. return r;
  1136. }
  1137. int zd_chip_set_basic_rates(struct zd_chip *chip, u16 cr_rates)
  1138. {
  1139. int r;
  1140. if (cr_rates & ~(CR_RATES_80211B|CR_RATES_80211G))
  1141. return -EINVAL;
  1142. mutex_lock(&chip->mutex);
  1143. r = zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1144. mutex_unlock(&chip->mutex);
  1145. return r;
  1146. }
  1147. static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame)
  1148. {
  1149. return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame);
  1150. }
  1151. /**
  1152. * zd_rx_rate - report zd-rate
  1153. * @rx_frame - received frame
  1154. * @rx_status - rx_status as given by the device
  1155. *
  1156. * This function converts the rate as encoded in the received packet to the
  1157. * zd-rate, we are using on other places in the driver.
  1158. */
  1159. u8 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1160. {
  1161. u8 zd_rate;
  1162. if (status->frame_status & ZD_RX_OFDM) {
  1163. zd_rate = zd_rate_from_ofdm_plcp_header(rx_frame);
  1164. } else {
  1165. switch (zd_cck_plcp_header_signal(rx_frame)) {
  1166. case ZD_CCK_PLCP_SIGNAL_1M:
  1167. zd_rate = ZD_CCK_RATE_1M;
  1168. break;
  1169. case ZD_CCK_PLCP_SIGNAL_2M:
  1170. zd_rate = ZD_CCK_RATE_2M;
  1171. break;
  1172. case ZD_CCK_PLCP_SIGNAL_5M5:
  1173. zd_rate = ZD_CCK_RATE_5_5M;
  1174. break;
  1175. case ZD_CCK_PLCP_SIGNAL_11M:
  1176. zd_rate = ZD_CCK_RATE_11M;
  1177. break;
  1178. default:
  1179. zd_rate = 0;
  1180. }
  1181. }
  1182. return zd_rate;
  1183. }
  1184. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1185. {
  1186. int r;
  1187. mutex_lock(&chip->mutex);
  1188. r = zd_switch_radio_on(&chip->rf);
  1189. mutex_unlock(&chip->mutex);
  1190. return r;
  1191. }
  1192. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1193. {
  1194. int r;
  1195. mutex_lock(&chip->mutex);
  1196. r = zd_switch_radio_off(&chip->rf);
  1197. mutex_unlock(&chip->mutex);
  1198. return r;
  1199. }
  1200. int zd_chip_enable_int(struct zd_chip *chip)
  1201. {
  1202. int r;
  1203. mutex_lock(&chip->mutex);
  1204. r = zd_usb_enable_int(&chip->usb);
  1205. mutex_unlock(&chip->mutex);
  1206. return r;
  1207. }
  1208. void zd_chip_disable_int(struct zd_chip *chip)
  1209. {
  1210. mutex_lock(&chip->mutex);
  1211. zd_usb_disable_int(&chip->usb);
  1212. mutex_unlock(&chip->mutex);
  1213. }
  1214. int zd_chip_enable_rxtx(struct zd_chip *chip)
  1215. {
  1216. int r;
  1217. mutex_lock(&chip->mutex);
  1218. zd_usb_enable_tx(&chip->usb);
  1219. r = zd_usb_enable_rx(&chip->usb);
  1220. mutex_unlock(&chip->mutex);
  1221. return r;
  1222. }
  1223. void zd_chip_disable_rxtx(struct zd_chip *chip)
  1224. {
  1225. mutex_lock(&chip->mutex);
  1226. zd_usb_disable_rx(&chip->usb);
  1227. zd_usb_disable_tx(&chip->usb);
  1228. mutex_unlock(&chip->mutex);
  1229. }
  1230. int zd_rfwritev_locked(struct zd_chip *chip,
  1231. const u32* values, unsigned int count, u8 bits)
  1232. {
  1233. int r;
  1234. unsigned int i;
  1235. for (i = 0; i < count; i++) {
  1236. r = zd_rfwrite_locked(chip, values[i], bits);
  1237. if (r)
  1238. return r;
  1239. }
  1240. return 0;
  1241. }
  1242. /*
  1243. * We can optionally program the RF directly through CR regs, if supported by
  1244. * the hardware. This is much faster than the older method.
  1245. */
  1246. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1247. {
  1248. struct zd_ioreq16 ioreqs[] = {
  1249. { CR244, (value >> 16) & 0xff },
  1250. { CR243, (value >> 8) & 0xff },
  1251. { CR242, value & 0xff },
  1252. };
  1253. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1254. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1255. }
  1256. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1257. const u32 *values, unsigned int count)
  1258. {
  1259. int r;
  1260. unsigned int i;
  1261. for (i = 0; i < count; i++) {
  1262. r = zd_rfwrite_cr_locked(chip, values[i]);
  1263. if (r)
  1264. return r;
  1265. }
  1266. return 0;
  1267. }
  1268. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1269. struct zd_mc_hash *hash)
  1270. {
  1271. struct zd_ioreq32 ioreqs[] = {
  1272. { CR_GROUP_HASH_P1, hash->low },
  1273. { CR_GROUP_HASH_P2, hash->high },
  1274. };
  1275. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1276. }
  1277. u64 zd_chip_get_tsf(struct zd_chip *chip)
  1278. {
  1279. int r;
  1280. static const zd_addr_t aw_pt_bi_addr[] =
  1281. { CR_TSF_LOW_PART, CR_TSF_HIGH_PART };
  1282. u32 values[2];
  1283. u64 tsf;
  1284. mutex_lock(&chip->mutex);
  1285. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  1286. ARRAY_SIZE(aw_pt_bi_addr));
  1287. mutex_unlock(&chip->mutex);
  1288. if (r)
  1289. return 0;
  1290. tsf = values[1];
  1291. tsf = (tsf << 32) | values[0];
  1292. return tsf;
  1293. }