zd_chip.c 37 KB

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