zd_chip.c 39 KB

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