zd_chip.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* This file implements all the hardware specific functions for the ZD1211
  20. * and ZD1211B chips. Support for the ZD1211B was possible after Timothy
  21. * Legge sent me a ZD1211B device. Thank you Tim. -- Uli
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/slab.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[USB_MAX_IOREAD32_COUNT * 2];
  99. u16 v16[USB_MAX_IOREAD32_COUNT * 2];
  100. unsigned int count16;
  101. if (count > USB_MAX_IOREAD32_COUNT)
  102. return -EINVAL;
  103. /* Use stack for values and addresses. */
  104. count16 = 2 * count;
  105. BUG_ON(count16 * sizeof(zd_addr_t) > sizeof(a16));
  106. BUG_ON(count16 * sizeof(u16) > sizeof(v16));
  107. for (i = 0; i < count; i++) {
  108. int j = 2*i;
  109. /* We read the high word always first. */
  110. a16[j] = inc_addr(addr[i]);
  111. a16[j+1] = addr[i];
  112. }
  113. r = zd_ioread16v_locked(chip, v16, a16, count16);
  114. if (r) {
  115. dev_dbg_f(zd_chip_dev(chip),
  116. "error: %s. Error number %d\n", __func__, r);
  117. return r;
  118. }
  119. for (i = 0; i < count; i++) {
  120. int j = 2*i;
  121. values[i] = (v16[j] << 16) | v16[j+1];
  122. }
  123. return 0;
  124. }
  125. static int _zd_iowrite32v_async_locked(struct zd_chip *chip,
  126. const struct zd_ioreq32 *ioreqs,
  127. unsigned int count)
  128. {
  129. int i, j, r;
  130. struct zd_ioreq16 ioreqs16[USB_MAX_IOWRITE32_COUNT * 2];
  131. unsigned int count16;
  132. /* Use stack for values and addresses. */
  133. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  134. if (count == 0)
  135. return 0;
  136. if (count > USB_MAX_IOWRITE32_COUNT)
  137. return -EINVAL;
  138. count16 = 2 * count;
  139. BUG_ON(count16 * sizeof(struct zd_ioreq16) > sizeof(ioreqs16));
  140. for (i = 0; i < count; i++) {
  141. j = 2*i;
  142. /* We write the high word always first. */
  143. ioreqs16[j].value = ioreqs[i].value >> 16;
  144. ioreqs16[j].addr = inc_addr(ioreqs[i].addr);
  145. ioreqs16[j+1].value = ioreqs[i].value;
  146. ioreqs16[j+1].addr = ioreqs[i].addr;
  147. }
  148. r = zd_usb_iowrite16v_async(&chip->usb, ioreqs16, count16);
  149. #ifdef DEBUG
  150. if (r) {
  151. dev_dbg_f(zd_chip_dev(chip),
  152. "error %d in zd_usb_write16v\n", r);
  153. }
  154. #endif /* DEBUG */
  155. return r;
  156. }
  157. int _zd_iowrite32v_locked(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  158. unsigned int count)
  159. {
  160. int r;
  161. zd_usb_iowrite16v_async_start(&chip->usb);
  162. r = _zd_iowrite32v_async_locked(chip, ioreqs, count);
  163. if (r) {
  164. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  165. return r;
  166. }
  167. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  168. }
  169. int zd_iowrite16a_locked(struct zd_chip *chip,
  170. const struct zd_ioreq16 *ioreqs, unsigned int count)
  171. {
  172. int r;
  173. unsigned int i, j, t, max;
  174. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  175. zd_usb_iowrite16v_async_start(&chip->usb);
  176. for (i = 0; i < count; i += j + t) {
  177. t = 0;
  178. max = count-i;
  179. if (max > USB_MAX_IOWRITE16_COUNT)
  180. max = USB_MAX_IOWRITE16_COUNT;
  181. for (j = 0; j < max; j++) {
  182. if (!ioreqs[i+j].addr) {
  183. t = 1;
  184. break;
  185. }
  186. }
  187. r = zd_usb_iowrite16v_async(&chip->usb, &ioreqs[i], j);
  188. if (r) {
  189. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  190. dev_dbg_f(zd_chip_dev(chip),
  191. "error zd_usb_iowrite16v. Error number %d\n",
  192. r);
  193. return r;
  194. }
  195. }
  196. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  197. }
  198. /* Writes a variable number of 32 bit registers. The functions will split
  199. * that in several USB requests. A split can be forced by inserting an IO
  200. * request with an zero address field.
  201. */
  202. int zd_iowrite32a_locked(struct zd_chip *chip,
  203. const struct zd_ioreq32 *ioreqs, unsigned int count)
  204. {
  205. int r;
  206. unsigned int i, j, t, max;
  207. zd_usb_iowrite16v_async_start(&chip->usb);
  208. for (i = 0; i < count; i += j + t) {
  209. t = 0;
  210. max = count-i;
  211. if (max > USB_MAX_IOWRITE32_COUNT)
  212. max = USB_MAX_IOWRITE32_COUNT;
  213. for (j = 0; j < max; j++) {
  214. if (!ioreqs[i+j].addr) {
  215. t = 1;
  216. break;
  217. }
  218. }
  219. r = _zd_iowrite32v_async_locked(chip, &ioreqs[i], j);
  220. if (r) {
  221. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  222. dev_dbg_f(zd_chip_dev(chip),
  223. "error _%s. Error number %d\n", __func__,
  224. r);
  225. return r;
  226. }
  227. }
  228. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  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. static int zd_write_mac_addr_common(struct zd_chip *chip, const u8 *mac_addr,
  323. const struct zd_ioreq32 *in_reqs,
  324. const char *type)
  325. {
  326. int r;
  327. struct zd_ioreq32 reqs[2] = {in_reqs[0], in_reqs[1]};
  328. if (mac_addr) {
  329. reqs[0].value = (mac_addr[3] << 24)
  330. | (mac_addr[2] << 16)
  331. | (mac_addr[1] << 8)
  332. | mac_addr[0];
  333. reqs[1].value = (mac_addr[5] << 8)
  334. | mac_addr[4];
  335. dev_dbg_f(zd_chip_dev(chip), "%s addr %pM\n", type, mac_addr);
  336. } else {
  337. dev_dbg_f(zd_chip_dev(chip), "set NULL %s\n", type);
  338. }
  339. mutex_lock(&chip->mutex);
  340. r = zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  341. mutex_unlock(&chip->mutex);
  342. return r;
  343. }
  344. /* MAC address: if custom mac addresses are to be used CR_MAC_ADDR_P1 and
  345. * CR_MAC_ADDR_P2 must be overwritten
  346. */
  347. int zd_write_mac_addr(struct zd_chip *chip, const u8 *mac_addr)
  348. {
  349. static const struct zd_ioreq32 reqs[2] = {
  350. [0] = { .addr = CR_MAC_ADDR_P1 },
  351. [1] = { .addr = CR_MAC_ADDR_P2 },
  352. };
  353. return zd_write_mac_addr_common(chip, mac_addr, reqs, "mac");
  354. }
  355. int zd_write_bssid(struct zd_chip *chip, const u8 *bssid)
  356. {
  357. static const struct zd_ioreq32 reqs[2] = {
  358. [0] = { .addr = CR_BSSID_P1 },
  359. [1] = { .addr = CR_BSSID_P2 },
  360. };
  361. return zd_write_mac_addr_common(chip, bssid, reqs, "bssid");
  362. }
  363. int zd_read_regdomain(struct zd_chip *chip, u8 *regdomain)
  364. {
  365. int r;
  366. u32 value;
  367. mutex_lock(&chip->mutex);
  368. r = zd_ioread32_locked(chip, &value, E2P_SUBID);
  369. mutex_unlock(&chip->mutex);
  370. if (r)
  371. return r;
  372. *regdomain = value >> 16;
  373. dev_dbg_f(zd_chip_dev(chip), "regdomain: %#04x\n", *regdomain);
  374. return 0;
  375. }
  376. static int read_values(struct zd_chip *chip, u8 *values, size_t count,
  377. zd_addr_t e2p_addr, u32 guard)
  378. {
  379. int r;
  380. int i;
  381. u32 v;
  382. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  383. for (i = 0;;) {
  384. r = zd_ioread32_locked(chip, &v,
  385. (zd_addr_t)((u16)e2p_addr+i/2));
  386. if (r)
  387. return r;
  388. v -= guard;
  389. if (i+4 < count) {
  390. values[i++] = v;
  391. values[i++] = v >> 8;
  392. values[i++] = v >> 16;
  393. values[i++] = v >> 24;
  394. continue;
  395. }
  396. for (;i < count; i++)
  397. values[i] = v >> (8*(i%3));
  398. return 0;
  399. }
  400. }
  401. static int read_pwr_cal_values(struct zd_chip *chip)
  402. {
  403. return read_values(chip, chip->pwr_cal_values,
  404. E2P_CHANNEL_COUNT, E2P_PWR_CAL_VALUE1,
  405. 0);
  406. }
  407. static int read_pwr_int_values(struct zd_chip *chip)
  408. {
  409. return read_values(chip, chip->pwr_int_values,
  410. E2P_CHANNEL_COUNT, E2P_PWR_INT_VALUE1,
  411. E2P_PWR_INT_GUARD);
  412. }
  413. static int read_ofdm_cal_values(struct zd_chip *chip)
  414. {
  415. int r;
  416. int i;
  417. static const zd_addr_t addresses[] = {
  418. E2P_36M_CAL_VALUE1,
  419. E2P_48M_CAL_VALUE1,
  420. E2P_54M_CAL_VALUE1,
  421. };
  422. for (i = 0; i < 3; i++) {
  423. r = read_values(chip, chip->ofdm_cal_values[i],
  424. E2P_CHANNEL_COUNT, addresses[i], 0);
  425. if (r)
  426. return r;
  427. }
  428. return 0;
  429. }
  430. static int read_cal_int_tables(struct zd_chip *chip)
  431. {
  432. int r;
  433. r = read_pwr_cal_values(chip);
  434. if (r)
  435. return r;
  436. r = read_pwr_int_values(chip);
  437. if (r)
  438. return r;
  439. r = read_ofdm_cal_values(chip);
  440. if (r)
  441. return r;
  442. return 0;
  443. }
  444. /* phy means physical registers */
  445. int zd_chip_lock_phy_regs(struct zd_chip *chip)
  446. {
  447. int r;
  448. u32 tmp;
  449. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  450. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  451. if (r) {
  452. dev_err(zd_chip_dev(chip), "error ioread32(CR_REG1): %d\n", r);
  453. return r;
  454. }
  455. tmp &= ~UNLOCK_PHY_REGS;
  456. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  457. if (r)
  458. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  459. return r;
  460. }
  461. int zd_chip_unlock_phy_regs(struct zd_chip *chip)
  462. {
  463. int r;
  464. u32 tmp;
  465. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  466. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  467. if (r) {
  468. dev_err(zd_chip_dev(chip),
  469. "error ioread32(CR_REG1): %d\n", r);
  470. return r;
  471. }
  472. tmp |= UNLOCK_PHY_REGS;
  473. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  474. if (r)
  475. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  476. return r;
  477. }
  478. /* ZD_CR157 can be optionally patched by the EEPROM for original ZD1211 */
  479. static int patch_cr157(struct zd_chip *chip)
  480. {
  481. int r;
  482. u16 value;
  483. if (!chip->patch_cr157)
  484. return 0;
  485. r = zd_ioread16_locked(chip, &value, E2P_PHY_REG);
  486. if (r)
  487. return r;
  488. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value >> 8);
  489. return zd_iowrite32_locked(chip, value >> 8, ZD_CR157);
  490. }
  491. /*
  492. * 6M band edge can be optionally overwritten for certain RF's
  493. * Vendor driver says: for FCC regulation, enabled per HWFeature 6M band edge
  494. * bit (for AL2230, AL2230S)
  495. */
  496. static int patch_6m_band_edge(struct zd_chip *chip, u8 channel)
  497. {
  498. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  499. if (!chip->patch_6m_band_edge)
  500. return 0;
  501. return zd_rf_patch_6m_band_edge(&chip->rf, channel);
  502. }
  503. /* Generic implementation of 6M band edge patching, used by most RFs via
  504. * zd_rf_generic_patch_6m() */
  505. int zd_chip_generic_patch_6m_band(struct zd_chip *chip, int channel)
  506. {
  507. struct zd_ioreq16 ioreqs[] = {
  508. { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 },
  509. { ZD_CR47, 0x1e },
  510. };
  511. /* FIXME: Channel 11 is not the edge for all regulatory domains. */
  512. if (channel == 1 || channel == 11)
  513. ioreqs[0].value = 0x12;
  514. dev_dbg_f(zd_chip_dev(chip), "patching for channel %d\n", channel);
  515. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  516. }
  517. static int zd1211_hw_reset_phy(struct zd_chip *chip)
  518. {
  519. static const struct zd_ioreq16 ioreqs[] = {
  520. { ZD_CR0, 0x0a }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 },
  521. { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xa0 },
  522. { ZD_CR10, 0x81 }, { ZD_CR11, 0x00 }, { ZD_CR12, 0x7f },
  523. { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 }, { ZD_CR15, 0x3d },
  524. { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e }, { ZD_CR18, 0x0a },
  525. { ZD_CR19, 0x48 }, { ZD_CR20, 0x0c }, { ZD_CR21, 0x0c },
  526. { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 }, { ZD_CR24, 0x14 },
  527. { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 }, { ZD_CR27, 0x19 },
  528. { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 }, { ZD_CR30, 0x4b },
  529. { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 },
  530. { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 },
  531. { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c },
  532. { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 },
  533. { ZD_CR43, 0x10 }, { ZD_CR44, 0x12 }, { ZD_CR46, 0xff },
  534. { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b },
  535. { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 },
  536. { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 },
  537. { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff },
  538. { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 },
  539. { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 },
  540. { ZD_CR79, 0x68 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 },
  541. { ZD_CR82, 0x00 }, { ZD_CR83, 0x00 }, { ZD_CR84, 0x00 },
  542. { ZD_CR85, 0x02 }, { ZD_CR86, 0x00 }, { ZD_CR87, 0x00 },
  543. { ZD_CR88, 0xff }, { ZD_CR89, 0xfc }, { ZD_CR90, 0x00 },
  544. { ZD_CR91, 0x00 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x08 },
  545. { ZD_CR94, 0x00 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0xff },
  546. { ZD_CR97, 0xe7 }, { ZD_CR98, 0x00 }, { ZD_CR99, 0x00 },
  547. { ZD_CR100, 0x00 }, { ZD_CR101, 0xae }, { ZD_CR102, 0x02 },
  548. { ZD_CR103, 0x00 }, { ZD_CR104, 0x03 }, { ZD_CR105, 0x65 },
  549. { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 }, { ZD_CR108, 0x0a },
  550. { ZD_CR109, 0xaa }, { ZD_CR110, 0xaa }, { ZD_CR111, 0x25 },
  551. { ZD_CR112, 0x25 }, { ZD_CR113, 0x00 }, { ZD_CR119, 0x1e },
  552. { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 },
  553. { },
  554. { ZD_CR5, 0x00 }, { ZD_CR6, 0x00 }, { ZD_CR7, 0x00 },
  555. { ZD_CR8, 0x00 }, { ZD_CR9, 0x20 }, { ZD_CR12, 0xf0 },
  556. { ZD_CR20, 0x0e }, { ZD_CR21, 0x0e }, { ZD_CR27, 0x10 },
  557. { ZD_CR44, 0x33 }, { ZD_CR47, 0x1E }, { ZD_CR83, 0x24 },
  558. { ZD_CR84, 0x04 }, { ZD_CR85, 0x00 }, { ZD_CR86, 0x0C },
  559. { ZD_CR87, 0x12 }, { ZD_CR88, 0x0C }, { ZD_CR89, 0x00 },
  560. { ZD_CR90, 0x10 }, { ZD_CR91, 0x08 }, { ZD_CR93, 0x00 },
  561. { ZD_CR94, 0x01 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0x50 },
  562. { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 }, { ZD_CR101, 0x13 },
  563. { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 },
  564. { ZD_CR105, 0x12 }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 },
  565. { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 },
  566. { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 },
  567. { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR120, 0x4f },
  568. { ZD_CR125, 0xaa }, { ZD_CR127, 0x03 }, { ZD_CR128, 0x14 },
  569. { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 }, { ZD_CR131, 0x0C },
  570. { ZD_CR136, 0xdf }, { ZD_CR137, 0x40 }, { ZD_CR138, 0xa0 },
  571. { ZD_CR139, 0xb0 }, { ZD_CR140, 0x99 }, { ZD_CR141, 0x82 },
  572. { ZD_CR142, 0x54 }, { ZD_CR143, 0x1c }, { ZD_CR144, 0x6c },
  573. { ZD_CR147, 0x07 }, { ZD_CR148, 0x4c }, { ZD_CR149, 0x50 },
  574. { ZD_CR150, 0x0e }, { ZD_CR151, 0x18 }, { ZD_CR160, 0xfe },
  575. { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa },
  576. { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe },
  577. { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba },
  578. { ZD_CR170, 0xba }, { ZD_CR171, 0xba },
  579. /* Note: ZD_CR204 must lead the ZD_CR203 */
  580. { ZD_CR204, 0x7d },
  581. { },
  582. { ZD_CR203, 0x30 },
  583. };
  584. int r, t;
  585. dev_dbg_f(zd_chip_dev(chip), "\n");
  586. r = zd_chip_lock_phy_regs(chip);
  587. if (r)
  588. goto out;
  589. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  590. if (r)
  591. goto unlock;
  592. r = patch_cr157(chip);
  593. unlock:
  594. t = zd_chip_unlock_phy_regs(chip);
  595. if (t && !r)
  596. r = t;
  597. out:
  598. return r;
  599. }
  600. static int zd1211b_hw_reset_phy(struct zd_chip *chip)
  601. {
  602. static const struct zd_ioreq16 ioreqs[] = {
  603. { ZD_CR0, 0x14 }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 },
  604. { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xe0 },
  605. { ZD_CR10, 0x81 },
  606. /* power control { { ZD_CR11, 1 << 6 }, */
  607. { ZD_CR11, 0x00 },
  608. { ZD_CR12, 0xf0 }, { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 },
  609. { ZD_CR15, 0x3d }, { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e },
  610. { ZD_CR18, 0x0a }, { ZD_CR19, 0x48 },
  611. { ZD_CR20, 0x10 }, /* Org:0x0E, ComTrend:RalLink AP */
  612. { ZD_CR21, 0x0e }, { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 },
  613. { ZD_CR24, 0x14 }, { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 },
  614. { ZD_CR27, 0x10 }, { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 },
  615. { ZD_CR30, 0x4b }, /* ASIC/FWT, no jointly decoder */
  616. { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 },
  617. { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 },
  618. { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c },
  619. { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 },
  620. { ZD_CR43, 0x10 }, { ZD_CR44, 0x33 }, { ZD_CR46, 0xff },
  621. { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b },
  622. { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 },
  623. { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 },
  624. { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff },
  625. { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 },
  626. { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 },
  627. { ZD_CR79, 0xf0 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 },
  628. { ZD_CR82, 0x00 }, { ZD_CR83, 0x24 }, { ZD_CR84, 0x04 },
  629. { ZD_CR85, 0x00 }, { ZD_CR86, 0x0c }, { ZD_CR87, 0x12 },
  630. { ZD_CR88, 0x0c }, { ZD_CR89, 0x00 }, { ZD_CR90, 0x58 },
  631. { ZD_CR91, 0x04 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x00 },
  632. { ZD_CR94, 0x01 },
  633. { ZD_CR95, 0x20 }, /* ZD1211B */
  634. { ZD_CR96, 0x50 }, { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 },
  635. { ZD_CR99, 0x00 }, { ZD_CR100, 0x01 }, { ZD_CR101, 0x13 },
  636. { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 },
  637. { ZD_CR105, 0x12 }, { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 },
  638. { ZD_CR108, 0x0a }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 },
  639. { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 },
  640. { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 },
  641. { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR119, 0x1e },
  642. { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 },
  643. { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 },
  644. { ZD_CR131, 0x0c }, { ZD_CR136, 0xdf }, { ZD_CR137, 0xa0 },
  645. { ZD_CR138, 0xa8 }, { ZD_CR139, 0xb4 }, { ZD_CR140, 0x98 },
  646. { ZD_CR141, 0x82 }, { ZD_CR142, 0x53 }, { ZD_CR143, 0x1c },
  647. { ZD_CR144, 0x6c }, { ZD_CR147, 0x07 }, { ZD_CR148, 0x40 },
  648. { ZD_CR149, 0x40 }, /* Org:0x50 ComTrend:RalLink AP */
  649. { ZD_CR150, 0x14 }, /* Org:0x0E ComTrend:RalLink AP */
  650. { ZD_CR151, 0x18 }, { ZD_CR159, 0x70 }, { ZD_CR160, 0xfe },
  651. { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa },
  652. { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe },
  653. { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba },
  654. { ZD_CR170, 0xba }, { ZD_CR171, 0xba },
  655. /* Note: ZD_CR204 must lead the ZD_CR203 */
  656. { ZD_CR204, 0x7d },
  657. {},
  658. { ZD_CR203, 0x30 },
  659. };
  660. int r, t;
  661. dev_dbg_f(zd_chip_dev(chip), "\n");
  662. r = zd_chip_lock_phy_regs(chip);
  663. if (r)
  664. goto out;
  665. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  666. t = zd_chip_unlock_phy_regs(chip);
  667. if (t && !r)
  668. r = t;
  669. out:
  670. return r;
  671. }
  672. static int hw_reset_phy(struct zd_chip *chip)
  673. {
  674. return zd_chip_is_zd1211b(chip) ? zd1211b_hw_reset_phy(chip) :
  675. zd1211_hw_reset_phy(chip);
  676. }
  677. static int zd1211_hw_init_hmac(struct zd_chip *chip)
  678. {
  679. static const struct zd_ioreq32 ioreqs[] = {
  680. { CR_ZD1211_RETRY_MAX, ZD1211_RETRY_COUNT },
  681. { CR_RX_THRESHOLD, 0x000c0640 },
  682. };
  683. dev_dbg_f(zd_chip_dev(chip), "\n");
  684. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  685. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  686. }
  687. static int zd1211b_hw_init_hmac(struct zd_chip *chip)
  688. {
  689. static const struct zd_ioreq32 ioreqs[] = {
  690. { CR_ZD1211B_RETRY_MAX, ZD1211B_RETRY_COUNT },
  691. { CR_ZD1211B_CWIN_MAX_MIN_AC0, 0x007f003f },
  692. { CR_ZD1211B_CWIN_MAX_MIN_AC1, 0x007f003f },
  693. { CR_ZD1211B_CWIN_MAX_MIN_AC2, 0x003f001f },
  694. { CR_ZD1211B_CWIN_MAX_MIN_AC3, 0x001f000f },
  695. { CR_ZD1211B_AIFS_CTL1, 0x00280028 },
  696. { CR_ZD1211B_AIFS_CTL2, 0x008C003C },
  697. { CR_ZD1211B_TXOP, 0x01800824 },
  698. { CR_RX_THRESHOLD, 0x000c0eff, },
  699. };
  700. dev_dbg_f(zd_chip_dev(chip), "\n");
  701. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  702. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  703. }
  704. static int hw_init_hmac(struct zd_chip *chip)
  705. {
  706. int r;
  707. static const struct zd_ioreq32 ioreqs[] = {
  708. { CR_ACK_TIMEOUT_EXT, 0x20 },
  709. { CR_ADDA_MBIAS_WARMTIME, 0x30000808 },
  710. { CR_SNIFFER_ON, 0 },
  711. { CR_RX_FILTER, STA_RX_FILTER },
  712. { CR_GROUP_HASH_P1, 0x00 },
  713. { CR_GROUP_HASH_P2, 0x80000000 },
  714. { CR_REG1, 0xa4 },
  715. { CR_ADDA_PWR_DWN, 0x7f },
  716. { CR_BCN_PLCP_CFG, 0x00f00401 },
  717. { CR_PHY_DELAY, 0x00 },
  718. { CR_ACK_TIMEOUT_EXT, 0x80 },
  719. { CR_ADDA_PWR_DWN, 0x00 },
  720. { CR_ACK_TIME_80211, 0x100 },
  721. { CR_RX_PE_DELAY, 0x70 },
  722. { CR_PS_CTRL, 0x10000000 },
  723. { CR_RTS_CTS_RATE, 0x02030203 },
  724. { CR_AFTER_PNP, 0x1 },
  725. { CR_WEP_PROTECT, 0x114 },
  726. { CR_IFS_VALUE, IFS_VALUE_DEFAULT },
  727. { CR_CAM_MODE, MODE_AP_WDS},
  728. };
  729. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  730. r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  731. if (r)
  732. return r;
  733. return zd_chip_is_zd1211b(chip) ?
  734. zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip);
  735. }
  736. struct aw_pt_bi {
  737. u32 atim_wnd_period;
  738. u32 pre_tbtt;
  739. u32 beacon_interval;
  740. };
  741. static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  742. {
  743. int r;
  744. static const zd_addr_t aw_pt_bi_addr[] =
  745. { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL };
  746. u32 values[3];
  747. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  748. ARRAY_SIZE(aw_pt_bi_addr));
  749. if (r) {
  750. memset(s, 0, sizeof(*s));
  751. return r;
  752. }
  753. s->atim_wnd_period = values[0];
  754. s->pre_tbtt = values[1];
  755. s->beacon_interval = values[2];
  756. return 0;
  757. }
  758. static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  759. {
  760. struct zd_ioreq32 reqs[3];
  761. u16 b_interval = s->beacon_interval & 0xffff;
  762. if (b_interval <= 5)
  763. b_interval = 5;
  764. if (s->pre_tbtt < 4 || s->pre_tbtt >= b_interval)
  765. s->pre_tbtt = b_interval - 1;
  766. if (s->atim_wnd_period >= s->pre_tbtt)
  767. s->atim_wnd_period = s->pre_tbtt - 1;
  768. reqs[0].addr = CR_ATIM_WND_PERIOD;
  769. reqs[0].value = s->atim_wnd_period;
  770. reqs[1].addr = CR_PRE_TBTT;
  771. reqs[1].value = s->pre_tbtt;
  772. reqs[2].addr = CR_BCN_INTERVAL;
  773. reqs[2].value = (s->beacon_interval & ~0xffff) | b_interval;
  774. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  775. }
  776. static int set_beacon_interval(struct zd_chip *chip, u16 interval,
  777. u8 dtim_period, int type)
  778. {
  779. int r;
  780. struct aw_pt_bi s;
  781. u32 b_interval, mode_flag;
  782. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  783. if (interval > 0) {
  784. switch (type) {
  785. case NL80211_IFTYPE_ADHOC:
  786. case NL80211_IFTYPE_MESH_POINT:
  787. mode_flag = BCN_MODE_IBSS;
  788. break;
  789. case NL80211_IFTYPE_AP:
  790. mode_flag = BCN_MODE_AP;
  791. break;
  792. default:
  793. mode_flag = 0;
  794. break;
  795. }
  796. } else {
  797. dtim_period = 0;
  798. mode_flag = 0;
  799. }
  800. b_interval = mode_flag | (dtim_period << 16) | interval;
  801. r = zd_iowrite32_locked(chip, b_interval, CR_BCN_INTERVAL);
  802. if (r)
  803. return r;
  804. r = get_aw_pt_bi(chip, &s);
  805. if (r)
  806. return r;
  807. return set_aw_pt_bi(chip, &s);
  808. }
  809. int zd_set_beacon_interval(struct zd_chip *chip, u16 interval, u8 dtim_period,
  810. int type)
  811. {
  812. int r;
  813. mutex_lock(&chip->mutex);
  814. r = set_beacon_interval(chip, interval, dtim_period, type);
  815. mutex_unlock(&chip->mutex);
  816. return r;
  817. }
  818. static int hw_init(struct zd_chip *chip)
  819. {
  820. int r;
  821. dev_dbg_f(zd_chip_dev(chip), "\n");
  822. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  823. r = hw_reset_phy(chip);
  824. if (r)
  825. return r;
  826. r = hw_init_hmac(chip);
  827. if (r)
  828. return r;
  829. return set_beacon_interval(chip, 100, 0, NL80211_IFTYPE_UNSPECIFIED);
  830. }
  831. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  832. {
  833. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  834. }
  835. #ifdef DEBUG
  836. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  837. const char *addr_string)
  838. {
  839. int r;
  840. u32 value;
  841. r = zd_ioread32_locked(chip, &value, addr);
  842. if (r) {
  843. dev_dbg_f(zd_chip_dev(chip),
  844. "error reading %s. Error number %d\n", addr_string, r);
  845. return r;
  846. }
  847. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  848. addr_string, (unsigned int)value);
  849. return 0;
  850. }
  851. static int test_init(struct zd_chip *chip)
  852. {
  853. int r;
  854. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  855. if (r)
  856. return r;
  857. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  858. if (r)
  859. return r;
  860. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  861. }
  862. static void dump_fw_registers(struct zd_chip *chip)
  863. {
  864. const zd_addr_t addr[4] = {
  865. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  866. fw_reg_addr(chip, FW_REG_USB_SPEED),
  867. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  868. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  869. };
  870. int r;
  871. u16 values[4];
  872. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  873. ARRAY_SIZE(addr));
  874. if (r) {
  875. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  876. r);
  877. return;
  878. }
  879. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  880. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  881. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  882. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  883. }
  884. #endif /* DEBUG */
  885. static int print_fw_version(struct zd_chip *chip)
  886. {
  887. struct wiphy *wiphy = zd_chip_to_mac(chip)->hw->wiphy;
  888. int r;
  889. u16 version;
  890. r = zd_ioread16_locked(chip, &version,
  891. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  892. if (r)
  893. return r;
  894. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  895. snprintf(wiphy->fw_version, sizeof(wiphy->fw_version),
  896. "%04hx", version);
  897. return 0;
  898. }
  899. static int set_mandatory_rates(struct zd_chip *chip, int gmode)
  900. {
  901. u32 rates;
  902. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  903. /* This sets the mandatory rates, which only depend from the standard
  904. * that the device is supporting. Until further notice we should try
  905. * to support 802.11g also for full speed USB.
  906. */
  907. if (!gmode)
  908. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  909. else
  910. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  911. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  912. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  913. }
  914. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  915. int preamble)
  916. {
  917. u32 value = 0;
  918. dev_dbg_f(zd_chip_dev(chip), "preamble=%x\n", preamble);
  919. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  920. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  921. /* We always send 11M RTS/self-CTS messages, like the vendor driver. */
  922. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_RTS_RATE;
  923. value |= ZD_RX_CCK << RTSCTS_SH_RTS_MOD_TYPE;
  924. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE;
  925. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  926. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  927. }
  928. int zd_chip_enable_hwint(struct zd_chip *chip)
  929. {
  930. int r;
  931. mutex_lock(&chip->mutex);
  932. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  933. mutex_unlock(&chip->mutex);
  934. return r;
  935. }
  936. static int disable_hwint(struct zd_chip *chip)
  937. {
  938. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  939. }
  940. int zd_chip_disable_hwint(struct zd_chip *chip)
  941. {
  942. int r;
  943. mutex_lock(&chip->mutex);
  944. r = disable_hwint(chip);
  945. mutex_unlock(&chip->mutex);
  946. return r;
  947. }
  948. static int read_fw_regs_offset(struct zd_chip *chip)
  949. {
  950. int r;
  951. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  952. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  953. FWRAW_REGS_ADDR);
  954. if (r)
  955. return r;
  956. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  957. (u16)chip->fw_regs_base);
  958. return 0;
  959. }
  960. /* Read mac address using pre-firmware interface */
  961. int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr)
  962. {
  963. dev_dbg_f(zd_chip_dev(chip), "\n");
  964. return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr,
  965. ETH_ALEN);
  966. }
  967. int zd_chip_init_hw(struct zd_chip *chip)
  968. {
  969. int r;
  970. u8 rf_type;
  971. dev_dbg_f(zd_chip_dev(chip), "\n");
  972. mutex_lock(&chip->mutex);
  973. #ifdef DEBUG
  974. r = test_init(chip);
  975. if (r)
  976. goto out;
  977. #endif
  978. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  979. if (r)
  980. goto out;
  981. r = read_fw_regs_offset(chip);
  982. if (r)
  983. goto out;
  984. /* GPI is always disabled, also in the other driver.
  985. */
  986. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  987. if (r)
  988. goto out;
  989. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  990. if (r)
  991. goto out;
  992. /* Currently we support IEEE 802.11g for full and high speed USB.
  993. * It might be discussed, whether we should support pure b mode for
  994. * full speed USB.
  995. */
  996. r = set_mandatory_rates(chip, 1);
  997. if (r)
  998. goto out;
  999. /* Disabling interrupts is certainly a smart thing here.
  1000. */
  1001. r = disable_hwint(chip);
  1002. if (r)
  1003. goto out;
  1004. r = read_pod(chip, &rf_type);
  1005. if (r)
  1006. goto out;
  1007. r = hw_init(chip);
  1008. if (r)
  1009. goto out;
  1010. r = zd_rf_init_hw(&chip->rf, rf_type);
  1011. if (r)
  1012. goto out;
  1013. r = print_fw_version(chip);
  1014. if (r)
  1015. goto out;
  1016. #ifdef DEBUG
  1017. dump_fw_registers(chip);
  1018. r = test_init(chip);
  1019. if (r)
  1020. goto out;
  1021. #endif /* DEBUG */
  1022. r = read_cal_int_tables(chip);
  1023. if (r)
  1024. goto out;
  1025. print_id(chip);
  1026. out:
  1027. mutex_unlock(&chip->mutex);
  1028. return r;
  1029. }
  1030. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  1031. {
  1032. u8 value = chip->pwr_int_values[channel - 1];
  1033. return zd_iowrite16_locked(chip, value, ZD_CR31);
  1034. }
  1035. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  1036. {
  1037. u8 value = chip->pwr_cal_values[channel-1];
  1038. return zd_iowrite16_locked(chip, value, ZD_CR68);
  1039. }
  1040. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  1041. {
  1042. struct zd_ioreq16 ioreqs[3];
  1043. ioreqs[0].addr = ZD_CR67;
  1044. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1045. ioreqs[1].addr = ZD_CR66;
  1046. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1047. ioreqs[2].addr = ZD_CR65;
  1048. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1049. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1050. }
  1051. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1052. u8 channel)
  1053. {
  1054. int r;
  1055. if (!zd_rf_should_update_pwr_int(&chip->rf))
  1056. return 0;
  1057. r = update_pwr_int(chip, channel);
  1058. if (r)
  1059. return r;
  1060. if (zd_chip_is_zd1211b(chip)) {
  1061. static const struct zd_ioreq16 ioreqs[] = {
  1062. { ZD_CR69, 0x28 },
  1063. {},
  1064. { ZD_CR69, 0x2a },
  1065. };
  1066. r = update_ofdm_cal(chip, channel);
  1067. if (r)
  1068. return r;
  1069. r = update_pwr_cal(chip, channel);
  1070. if (r)
  1071. return r;
  1072. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1073. if (r)
  1074. return r;
  1075. }
  1076. return 0;
  1077. }
  1078. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1079. static int patch_cck_gain(struct zd_chip *chip)
  1080. {
  1081. int r;
  1082. u32 value;
  1083. if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf))
  1084. return 0;
  1085. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1086. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1087. if (r)
  1088. return r;
  1089. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1090. return zd_iowrite16_locked(chip, value & 0xff, ZD_CR47);
  1091. }
  1092. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1093. {
  1094. int r, t;
  1095. mutex_lock(&chip->mutex);
  1096. r = zd_chip_lock_phy_regs(chip);
  1097. if (r)
  1098. goto out;
  1099. r = zd_rf_set_channel(&chip->rf, channel);
  1100. if (r)
  1101. goto unlock;
  1102. r = update_channel_integration_and_calibration(chip, channel);
  1103. if (r)
  1104. goto unlock;
  1105. r = patch_cck_gain(chip);
  1106. if (r)
  1107. goto unlock;
  1108. r = patch_6m_band_edge(chip, channel);
  1109. if (r)
  1110. goto unlock;
  1111. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1112. unlock:
  1113. t = zd_chip_unlock_phy_regs(chip);
  1114. if (t && !r)
  1115. r = t;
  1116. out:
  1117. mutex_unlock(&chip->mutex);
  1118. return r;
  1119. }
  1120. u8 zd_chip_get_channel(struct zd_chip *chip)
  1121. {
  1122. u8 channel;
  1123. mutex_lock(&chip->mutex);
  1124. channel = chip->rf.channel;
  1125. mutex_unlock(&chip->mutex);
  1126. return channel;
  1127. }
  1128. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1129. {
  1130. const zd_addr_t a[] = {
  1131. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1132. CR_LED,
  1133. };
  1134. int r;
  1135. u16 v[ARRAY_SIZE(a)];
  1136. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1137. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1138. [1] = { CR_LED },
  1139. };
  1140. u16 other_led;
  1141. mutex_lock(&chip->mutex);
  1142. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1143. if (r)
  1144. goto out;
  1145. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1146. switch (status) {
  1147. case ZD_LED_OFF:
  1148. ioreqs[0].value = FW_LINK_OFF;
  1149. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1150. break;
  1151. case ZD_LED_SCANNING:
  1152. ioreqs[0].value = FW_LINK_OFF;
  1153. ioreqs[1].value = v[1] & ~other_led;
  1154. if (get_seconds() % 3 == 0) {
  1155. ioreqs[1].value &= ~chip->link_led;
  1156. } else {
  1157. ioreqs[1].value |= chip->link_led;
  1158. }
  1159. break;
  1160. case ZD_LED_ASSOCIATED:
  1161. ioreqs[0].value = FW_LINK_TX;
  1162. ioreqs[1].value = v[1] & ~other_led;
  1163. ioreqs[1].value |= chip->link_led;
  1164. break;
  1165. default:
  1166. r = -EINVAL;
  1167. goto out;
  1168. }
  1169. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1170. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1171. if (r)
  1172. goto out;
  1173. }
  1174. r = 0;
  1175. out:
  1176. mutex_unlock(&chip->mutex);
  1177. return r;
  1178. }
  1179. int zd_chip_set_basic_rates(struct zd_chip *chip, u16 cr_rates)
  1180. {
  1181. int r;
  1182. if (cr_rates & ~(CR_RATES_80211B|CR_RATES_80211G))
  1183. return -EINVAL;
  1184. mutex_lock(&chip->mutex);
  1185. r = zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1186. mutex_unlock(&chip->mutex);
  1187. return r;
  1188. }
  1189. static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame)
  1190. {
  1191. return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame);
  1192. }
  1193. /**
  1194. * zd_rx_rate - report zd-rate
  1195. * @rx_frame - received frame
  1196. * @rx_status - rx_status as given by the device
  1197. *
  1198. * This function converts the rate as encoded in the received packet to the
  1199. * zd-rate, we are using on other places in the driver.
  1200. */
  1201. u8 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1202. {
  1203. u8 zd_rate;
  1204. if (status->frame_status & ZD_RX_OFDM) {
  1205. zd_rate = zd_rate_from_ofdm_plcp_header(rx_frame);
  1206. } else {
  1207. switch (zd_cck_plcp_header_signal(rx_frame)) {
  1208. case ZD_CCK_PLCP_SIGNAL_1M:
  1209. zd_rate = ZD_CCK_RATE_1M;
  1210. break;
  1211. case ZD_CCK_PLCP_SIGNAL_2M:
  1212. zd_rate = ZD_CCK_RATE_2M;
  1213. break;
  1214. case ZD_CCK_PLCP_SIGNAL_5M5:
  1215. zd_rate = ZD_CCK_RATE_5_5M;
  1216. break;
  1217. case ZD_CCK_PLCP_SIGNAL_11M:
  1218. zd_rate = ZD_CCK_RATE_11M;
  1219. break;
  1220. default:
  1221. zd_rate = 0;
  1222. }
  1223. }
  1224. return zd_rate;
  1225. }
  1226. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1227. {
  1228. int r;
  1229. mutex_lock(&chip->mutex);
  1230. r = zd_switch_radio_on(&chip->rf);
  1231. mutex_unlock(&chip->mutex);
  1232. return r;
  1233. }
  1234. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1235. {
  1236. int r;
  1237. mutex_lock(&chip->mutex);
  1238. r = zd_switch_radio_off(&chip->rf);
  1239. mutex_unlock(&chip->mutex);
  1240. return r;
  1241. }
  1242. int zd_chip_enable_int(struct zd_chip *chip)
  1243. {
  1244. int r;
  1245. mutex_lock(&chip->mutex);
  1246. r = zd_usb_enable_int(&chip->usb);
  1247. mutex_unlock(&chip->mutex);
  1248. return r;
  1249. }
  1250. void zd_chip_disable_int(struct zd_chip *chip)
  1251. {
  1252. mutex_lock(&chip->mutex);
  1253. zd_usb_disable_int(&chip->usb);
  1254. mutex_unlock(&chip->mutex);
  1255. /* cancel pending interrupt work */
  1256. cancel_work_sync(&zd_chip_to_mac(chip)->process_intr);
  1257. }
  1258. int zd_chip_enable_rxtx(struct zd_chip *chip)
  1259. {
  1260. int r;
  1261. mutex_lock(&chip->mutex);
  1262. zd_usb_enable_tx(&chip->usb);
  1263. r = zd_usb_enable_rx(&chip->usb);
  1264. zd_tx_watchdog_enable(&chip->usb);
  1265. mutex_unlock(&chip->mutex);
  1266. return r;
  1267. }
  1268. void zd_chip_disable_rxtx(struct zd_chip *chip)
  1269. {
  1270. mutex_lock(&chip->mutex);
  1271. zd_tx_watchdog_disable(&chip->usb);
  1272. zd_usb_disable_rx(&chip->usb);
  1273. zd_usb_disable_tx(&chip->usb);
  1274. mutex_unlock(&chip->mutex);
  1275. }
  1276. int zd_rfwritev_locked(struct zd_chip *chip,
  1277. const u32* values, unsigned int count, u8 bits)
  1278. {
  1279. int r;
  1280. unsigned int i;
  1281. for (i = 0; i < count; i++) {
  1282. r = zd_rfwrite_locked(chip, values[i], bits);
  1283. if (r)
  1284. return r;
  1285. }
  1286. return 0;
  1287. }
  1288. /*
  1289. * We can optionally program the RF directly through CR regs, if supported by
  1290. * the hardware. This is much faster than the older method.
  1291. */
  1292. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1293. {
  1294. const struct zd_ioreq16 ioreqs[] = {
  1295. { ZD_CR244, (value >> 16) & 0xff },
  1296. { ZD_CR243, (value >> 8) & 0xff },
  1297. { ZD_CR242, value & 0xff },
  1298. };
  1299. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1300. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1301. }
  1302. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1303. const u32 *values, unsigned int count)
  1304. {
  1305. int r;
  1306. unsigned int i;
  1307. for (i = 0; i < count; i++) {
  1308. r = zd_rfwrite_cr_locked(chip, values[i]);
  1309. if (r)
  1310. return r;
  1311. }
  1312. return 0;
  1313. }
  1314. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1315. struct zd_mc_hash *hash)
  1316. {
  1317. const struct zd_ioreq32 ioreqs[] = {
  1318. { CR_GROUP_HASH_P1, hash->low },
  1319. { CR_GROUP_HASH_P2, hash->high },
  1320. };
  1321. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1322. }
  1323. u64 zd_chip_get_tsf(struct zd_chip *chip)
  1324. {
  1325. int r;
  1326. static const zd_addr_t aw_pt_bi_addr[] =
  1327. { CR_TSF_LOW_PART, CR_TSF_HIGH_PART };
  1328. u32 values[2];
  1329. u64 tsf;
  1330. mutex_lock(&chip->mutex);
  1331. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  1332. ARRAY_SIZE(aw_pt_bi_addr));
  1333. mutex_unlock(&chip->mutex);
  1334. if (r)
  1335. return 0;
  1336. tsf = values[1];
  1337. tsf = (tsf << 32) | values[0];
  1338. return tsf;
  1339. }