zd_chip.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. if (s->beacon_interval <= 5)
  749. s->beacon_interval = 5;
  750. if (s->pre_tbtt < 4 || s->pre_tbtt >= s->beacon_interval)
  751. s->pre_tbtt = s->beacon_interval - 1;
  752. if (s->atim_wnd_period >= s->pre_tbtt)
  753. s->atim_wnd_period = s->pre_tbtt - 1;
  754. reqs[0].addr = CR_ATIM_WND_PERIOD;
  755. reqs[0].value = s->atim_wnd_period;
  756. reqs[1].addr = CR_PRE_TBTT;
  757. reqs[1].value = s->pre_tbtt;
  758. reqs[2].addr = CR_BCN_INTERVAL;
  759. reqs[2].value = s->beacon_interval;
  760. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  761. }
  762. static int set_beacon_interval(struct zd_chip *chip, u32 interval)
  763. {
  764. int r;
  765. struct aw_pt_bi s;
  766. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  767. r = get_aw_pt_bi(chip, &s);
  768. if (r)
  769. return r;
  770. s.beacon_interval = interval;
  771. return set_aw_pt_bi(chip, &s);
  772. }
  773. int zd_set_beacon_interval(struct zd_chip *chip, u32 interval)
  774. {
  775. int r;
  776. mutex_lock(&chip->mutex);
  777. r = set_beacon_interval(chip, interval);
  778. mutex_unlock(&chip->mutex);
  779. return r;
  780. }
  781. static int hw_init(struct zd_chip *chip)
  782. {
  783. int r;
  784. dev_dbg_f(zd_chip_dev(chip), "\n");
  785. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  786. r = hw_reset_phy(chip);
  787. if (r)
  788. return r;
  789. r = hw_init_hmac(chip);
  790. if (r)
  791. return r;
  792. return set_beacon_interval(chip, 100);
  793. }
  794. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  795. {
  796. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  797. }
  798. #ifdef DEBUG
  799. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  800. const char *addr_string)
  801. {
  802. int r;
  803. u32 value;
  804. r = zd_ioread32_locked(chip, &value, addr);
  805. if (r) {
  806. dev_dbg_f(zd_chip_dev(chip),
  807. "error reading %s. Error number %d\n", addr_string, r);
  808. return r;
  809. }
  810. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  811. addr_string, (unsigned int)value);
  812. return 0;
  813. }
  814. static int test_init(struct zd_chip *chip)
  815. {
  816. int r;
  817. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  818. if (r)
  819. return r;
  820. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  821. if (r)
  822. return r;
  823. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  824. }
  825. static void dump_fw_registers(struct zd_chip *chip)
  826. {
  827. const zd_addr_t addr[4] = {
  828. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  829. fw_reg_addr(chip, FW_REG_USB_SPEED),
  830. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  831. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  832. };
  833. int r;
  834. u16 values[4];
  835. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  836. ARRAY_SIZE(addr));
  837. if (r) {
  838. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  839. r);
  840. return;
  841. }
  842. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  843. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  844. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  845. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  846. }
  847. #endif /* DEBUG */
  848. static int print_fw_version(struct zd_chip *chip)
  849. {
  850. struct wiphy *wiphy = zd_chip_to_mac(chip)->hw->wiphy;
  851. int r;
  852. u16 version;
  853. r = zd_ioread16_locked(chip, &version,
  854. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  855. if (r)
  856. return r;
  857. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  858. snprintf(wiphy->fw_version, sizeof(wiphy->fw_version),
  859. "%04hx", version);
  860. return 0;
  861. }
  862. static int set_mandatory_rates(struct zd_chip *chip, int gmode)
  863. {
  864. u32 rates;
  865. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  866. /* This sets the mandatory rates, which only depend from the standard
  867. * that the device is supporting. Until further notice we should try
  868. * to support 802.11g also for full speed USB.
  869. */
  870. if (!gmode)
  871. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  872. else
  873. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  874. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  875. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  876. }
  877. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  878. int preamble)
  879. {
  880. u32 value = 0;
  881. dev_dbg_f(zd_chip_dev(chip), "preamble=%x\n", preamble);
  882. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  883. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  884. /* We always send 11M RTS/self-CTS messages, like the vendor driver. */
  885. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_RTS_RATE;
  886. value |= ZD_RX_CCK << RTSCTS_SH_RTS_MOD_TYPE;
  887. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE;
  888. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  889. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  890. }
  891. int zd_chip_enable_hwint(struct zd_chip *chip)
  892. {
  893. int r;
  894. mutex_lock(&chip->mutex);
  895. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  896. mutex_unlock(&chip->mutex);
  897. return r;
  898. }
  899. static int disable_hwint(struct zd_chip *chip)
  900. {
  901. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  902. }
  903. int zd_chip_disable_hwint(struct zd_chip *chip)
  904. {
  905. int r;
  906. mutex_lock(&chip->mutex);
  907. r = disable_hwint(chip);
  908. mutex_unlock(&chip->mutex);
  909. return r;
  910. }
  911. static int read_fw_regs_offset(struct zd_chip *chip)
  912. {
  913. int r;
  914. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  915. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  916. FWRAW_REGS_ADDR);
  917. if (r)
  918. return r;
  919. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  920. (u16)chip->fw_regs_base);
  921. return 0;
  922. }
  923. /* Read mac address using pre-firmware interface */
  924. int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr)
  925. {
  926. dev_dbg_f(zd_chip_dev(chip), "\n");
  927. return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr,
  928. ETH_ALEN);
  929. }
  930. int zd_chip_init_hw(struct zd_chip *chip)
  931. {
  932. int r;
  933. u8 rf_type;
  934. dev_dbg_f(zd_chip_dev(chip), "\n");
  935. mutex_lock(&chip->mutex);
  936. #ifdef DEBUG
  937. r = test_init(chip);
  938. if (r)
  939. goto out;
  940. #endif
  941. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  942. if (r)
  943. goto out;
  944. r = read_fw_regs_offset(chip);
  945. if (r)
  946. goto out;
  947. /* GPI is always disabled, also in the other driver.
  948. */
  949. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  950. if (r)
  951. goto out;
  952. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  953. if (r)
  954. goto out;
  955. /* Currently we support IEEE 802.11g for full and high speed USB.
  956. * It might be discussed, whether we should suppport pure b mode for
  957. * full speed USB.
  958. */
  959. r = set_mandatory_rates(chip, 1);
  960. if (r)
  961. goto out;
  962. /* Disabling interrupts is certainly a smart thing here.
  963. */
  964. r = disable_hwint(chip);
  965. if (r)
  966. goto out;
  967. r = read_pod(chip, &rf_type);
  968. if (r)
  969. goto out;
  970. r = hw_init(chip);
  971. if (r)
  972. goto out;
  973. r = zd_rf_init_hw(&chip->rf, rf_type);
  974. if (r)
  975. goto out;
  976. r = print_fw_version(chip);
  977. if (r)
  978. goto out;
  979. #ifdef DEBUG
  980. dump_fw_registers(chip);
  981. r = test_init(chip);
  982. if (r)
  983. goto out;
  984. #endif /* DEBUG */
  985. r = read_cal_int_tables(chip);
  986. if (r)
  987. goto out;
  988. print_id(chip);
  989. out:
  990. mutex_unlock(&chip->mutex);
  991. return r;
  992. }
  993. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  994. {
  995. u8 value = chip->pwr_int_values[channel - 1];
  996. return zd_iowrite16_locked(chip, value, CR31);
  997. }
  998. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  999. {
  1000. u8 value = chip->pwr_cal_values[channel-1];
  1001. return zd_iowrite16_locked(chip, value, CR68);
  1002. }
  1003. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  1004. {
  1005. struct zd_ioreq16 ioreqs[3];
  1006. ioreqs[0].addr = CR67;
  1007. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1008. ioreqs[1].addr = CR66;
  1009. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1010. ioreqs[2].addr = CR65;
  1011. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1012. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1013. }
  1014. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1015. u8 channel)
  1016. {
  1017. int r;
  1018. if (!zd_rf_should_update_pwr_int(&chip->rf))
  1019. return 0;
  1020. r = update_pwr_int(chip, channel);
  1021. if (r)
  1022. return r;
  1023. if (zd_chip_is_zd1211b(chip)) {
  1024. static const struct zd_ioreq16 ioreqs[] = {
  1025. { CR69, 0x28 },
  1026. {},
  1027. { CR69, 0x2a },
  1028. };
  1029. r = update_ofdm_cal(chip, channel);
  1030. if (r)
  1031. return r;
  1032. r = update_pwr_cal(chip, channel);
  1033. if (r)
  1034. return r;
  1035. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1036. if (r)
  1037. return r;
  1038. }
  1039. return 0;
  1040. }
  1041. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1042. static int patch_cck_gain(struct zd_chip *chip)
  1043. {
  1044. int r;
  1045. u32 value;
  1046. if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf))
  1047. return 0;
  1048. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1049. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1050. if (r)
  1051. return r;
  1052. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1053. return zd_iowrite16_locked(chip, value & 0xff, CR47);
  1054. }
  1055. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1056. {
  1057. int r, t;
  1058. mutex_lock(&chip->mutex);
  1059. r = zd_chip_lock_phy_regs(chip);
  1060. if (r)
  1061. goto out;
  1062. r = zd_rf_set_channel(&chip->rf, channel);
  1063. if (r)
  1064. goto unlock;
  1065. r = update_channel_integration_and_calibration(chip, channel);
  1066. if (r)
  1067. goto unlock;
  1068. r = patch_cck_gain(chip);
  1069. if (r)
  1070. goto unlock;
  1071. r = patch_6m_band_edge(chip, channel);
  1072. if (r)
  1073. goto unlock;
  1074. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1075. unlock:
  1076. t = zd_chip_unlock_phy_regs(chip);
  1077. if (t && !r)
  1078. r = t;
  1079. out:
  1080. mutex_unlock(&chip->mutex);
  1081. return r;
  1082. }
  1083. u8 zd_chip_get_channel(struct zd_chip *chip)
  1084. {
  1085. u8 channel;
  1086. mutex_lock(&chip->mutex);
  1087. channel = chip->rf.channel;
  1088. mutex_unlock(&chip->mutex);
  1089. return channel;
  1090. }
  1091. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1092. {
  1093. const zd_addr_t a[] = {
  1094. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1095. CR_LED,
  1096. };
  1097. int r;
  1098. u16 v[ARRAY_SIZE(a)];
  1099. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1100. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1101. [1] = { CR_LED },
  1102. };
  1103. u16 other_led;
  1104. mutex_lock(&chip->mutex);
  1105. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1106. if (r)
  1107. goto out;
  1108. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1109. switch (status) {
  1110. case ZD_LED_OFF:
  1111. ioreqs[0].value = FW_LINK_OFF;
  1112. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1113. break;
  1114. case ZD_LED_SCANNING:
  1115. ioreqs[0].value = FW_LINK_OFF;
  1116. ioreqs[1].value = v[1] & ~other_led;
  1117. if (get_seconds() % 3 == 0) {
  1118. ioreqs[1].value &= ~chip->link_led;
  1119. } else {
  1120. ioreqs[1].value |= chip->link_led;
  1121. }
  1122. break;
  1123. case ZD_LED_ASSOCIATED:
  1124. ioreqs[0].value = FW_LINK_TX;
  1125. ioreqs[1].value = v[1] & ~other_led;
  1126. ioreqs[1].value |= chip->link_led;
  1127. break;
  1128. default:
  1129. r = -EINVAL;
  1130. goto out;
  1131. }
  1132. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1133. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1134. if (r)
  1135. goto out;
  1136. }
  1137. r = 0;
  1138. out:
  1139. mutex_unlock(&chip->mutex);
  1140. return r;
  1141. }
  1142. int zd_chip_set_basic_rates(struct zd_chip *chip, u16 cr_rates)
  1143. {
  1144. int r;
  1145. if (cr_rates & ~(CR_RATES_80211B|CR_RATES_80211G))
  1146. return -EINVAL;
  1147. mutex_lock(&chip->mutex);
  1148. r = zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1149. mutex_unlock(&chip->mutex);
  1150. return r;
  1151. }
  1152. static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame)
  1153. {
  1154. return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame);
  1155. }
  1156. /**
  1157. * zd_rx_rate - report zd-rate
  1158. * @rx_frame - received frame
  1159. * @rx_status - rx_status as given by the device
  1160. *
  1161. * This function converts the rate as encoded in the received packet to the
  1162. * zd-rate, we are using on other places in the driver.
  1163. */
  1164. u8 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1165. {
  1166. u8 zd_rate;
  1167. if (status->frame_status & ZD_RX_OFDM) {
  1168. zd_rate = zd_rate_from_ofdm_plcp_header(rx_frame);
  1169. } else {
  1170. switch (zd_cck_plcp_header_signal(rx_frame)) {
  1171. case ZD_CCK_PLCP_SIGNAL_1M:
  1172. zd_rate = ZD_CCK_RATE_1M;
  1173. break;
  1174. case ZD_CCK_PLCP_SIGNAL_2M:
  1175. zd_rate = ZD_CCK_RATE_2M;
  1176. break;
  1177. case ZD_CCK_PLCP_SIGNAL_5M5:
  1178. zd_rate = ZD_CCK_RATE_5_5M;
  1179. break;
  1180. case ZD_CCK_PLCP_SIGNAL_11M:
  1181. zd_rate = ZD_CCK_RATE_11M;
  1182. break;
  1183. default:
  1184. zd_rate = 0;
  1185. }
  1186. }
  1187. return zd_rate;
  1188. }
  1189. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1190. {
  1191. int r;
  1192. mutex_lock(&chip->mutex);
  1193. r = zd_switch_radio_on(&chip->rf);
  1194. mutex_unlock(&chip->mutex);
  1195. return r;
  1196. }
  1197. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1198. {
  1199. int r;
  1200. mutex_lock(&chip->mutex);
  1201. r = zd_switch_radio_off(&chip->rf);
  1202. mutex_unlock(&chip->mutex);
  1203. return r;
  1204. }
  1205. int zd_chip_enable_int(struct zd_chip *chip)
  1206. {
  1207. int r;
  1208. mutex_lock(&chip->mutex);
  1209. r = zd_usb_enable_int(&chip->usb);
  1210. mutex_unlock(&chip->mutex);
  1211. return r;
  1212. }
  1213. void zd_chip_disable_int(struct zd_chip *chip)
  1214. {
  1215. mutex_lock(&chip->mutex);
  1216. zd_usb_disable_int(&chip->usb);
  1217. mutex_unlock(&chip->mutex);
  1218. }
  1219. int zd_chip_enable_rxtx(struct zd_chip *chip)
  1220. {
  1221. int r;
  1222. mutex_lock(&chip->mutex);
  1223. zd_usb_enable_tx(&chip->usb);
  1224. r = zd_usb_enable_rx(&chip->usb);
  1225. mutex_unlock(&chip->mutex);
  1226. return r;
  1227. }
  1228. void zd_chip_disable_rxtx(struct zd_chip *chip)
  1229. {
  1230. mutex_lock(&chip->mutex);
  1231. zd_usb_disable_rx(&chip->usb);
  1232. zd_usb_disable_tx(&chip->usb);
  1233. mutex_unlock(&chip->mutex);
  1234. }
  1235. int zd_rfwritev_locked(struct zd_chip *chip,
  1236. const u32* values, unsigned int count, u8 bits)
  1237. {
  1238. int r;
  1239. unsigned int i;
  1240. for (i = 0; i < count; i++) {
  1241. r = zd_rfwrite_locked(chip, values[i], bits);
  1242. if (r)
  1243. return r;
  1244. }
  1245. return 0;
  1246. }
  1247. /*
  1248. * We can optionally program the RF directly through CR regs, if supported by
  1249. * the hardware. This is much faster than the older method.
  1250. */
  1251. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1252. {
  1253. const struct zd_ioreq16 ioreqs[] = {
  1254. { CR244, (value >> 16) & 0xff },
  1255. { CR243, (value >> 8) & 0xff },
  1256. { CR242, value & 0xff },
  1257. };
  1258. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1259. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1260. }
  1261. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1262. const u32 *values, unsigned int count)
  1263. {
  1264. int r;
  1265. unsigned int i;
  1266. for (i = 0; i < count; i++) {
  1267. r = zd_rfwrite_cr_locked(chip, values[i]);
  1268. if (r)
  1269. return r;
  1270. }
  1271. return 0;
  1272. }
  1273. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1274. struct zd_mc_hash *hash)
  1275. {
  1276. const struct zd_ioreq32 ioreqs[] = {
  1277. { CR_GROUP_HASH_P1, hash->low },
  1278. { CR_GROUP_HASH_P2, hash->high },
  1279. };
  1280. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1281. }
  1282. u64 zd_chip_get_tsf(struct zd_chip *chip)
  1283. {
  1284. int r;
  1285. static const zd_addr_t aw_pt_bi_addr[] =
  1286. { CR_TSF_LOW_PART, CR_TSF_HIGH_PART };
  1287. u32 values[2];
  1288. u64 tsf;
  1289. mutex_lock(&chip->mutex);
  1290. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  1291. ARRAY_SIZE(aw_pt_bi_addr));
  1292. mutex_unlock(&chip->mutex);
  1293. if (r)
  1294. return 0;
  1295. tsf = values[1];
  1296. tsf = (tsf << 32) | values[0];
  1297. return tsf;
  1298. }