zd_chip.c 41 KB

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