zd_chip.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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_KERNEL);
  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_KERNEL);
  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 for original ZD1211 */
  513. static int patch_cr157(struct zd_chip *chip)
  514. {
  515. int r;
  516. u16 value;
  517. if (!chip->patch_cr157)
  518. return 0;
  519. r = zd_ioread16_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. t = zd_chip_unlock_phy_regs(chip);
  694. if (t && !r)
  695. r = t;
  696. out:
  697. return r;
  698. }
  699. static int hw_reset_phy(struct zd_chip *chip)
  700. {
  701. return chip->is_zd1211b ? zd1211b_hw_reset_phy(chip) :
  702. zd1211_hw_reset_phy(chip);
  703. }
  704. static int zd1211_hw_init_hmac(struct zd_chip *chip)
  705. {
  706. static const struct zd_ioreq32 ioreqs[] = {
  707. { CR_ZD1211_RETRY_MAX, 0x2 },
  708. { CR_RX_THRESHOLD, 0x000c0640 },
  709. };
  710. dev_dbg_f(zd_chip_dev(chip), "\n");
  711. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  712. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  713. }
  714. static int zd1211b_hw_init_hmac(struct zd_chip *chip)
  715. {
  716. static const struct zd_ioreq32 ioreqs[] = {
  717. { CR_ZD1211B_RETRY_MAX, 0x02020202 },
  718. { CR_ZD1211B_TX_PWR_CTL4, 0x007f003f },
  719. { CR_ZD1211B_TX_PWR_CTL3, 0x007f003f },
  720. { CR_ZD1211B_TX_PWR_CTL2, 0x003f001f },
  721. { CR_ZD1211B_TX_PWR_CTL1, 0x001f000f },
  722. { CR_ZD1211B_AIFS_CTL1, 0x00280028 },
  723. { CR_ZD1211B_AIFS_CTL2, 0x008C003C },
  724. { CR_ZD1211B_TXOP, 0x01800824 },
  725. { CR_RX_THRESHOLD, 0x000c0eff, },
  726. };
  727. dev_dbg_f(zd_chip_dev(chip), "\n");
  728. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  729. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  730. }
  731. static int hw_init_hmac(struct zd_chip *chip)
  732. {
  733. int r;
  734. static const struct zd_ioreq32 ioreqs[] = {
  735. { CR_ACK_TIMEOUT_EXT, 0x20 },
  736. { CR_ADDA_MBIAS_WARMTIME, 0x30000808 },
  737. { CR_SNIFFER_ON, 0 },
  738. { CR_RX_FILTER, STA_RX_FILTER },
  739. { CR_GROUP_HASH_P1, 0x00 },
  740. { CR_GROUP_HASH_P2, 0x80000000 },
  741. { CR_REG1, 0xa4 },
  742. { CR_ADDA_PWR_DWN, 0x7f },
  743. { CR_BCN_PLCP_CFG, 0x00f00401 },
  744. { CR_PHY_DELAY, 0x00 },
  745. { CR_ACK_TIMEOUT_EXT, 0x80 },
  746. { CR_ADDA_PWR_DWN, 0x00 },
  747. { CR_ACK_TIME_80211, 0x100 },
  748. { CR_RX_PE_DELAY, 0x70 },
  749. { CR_PS_CTRL, 0x10000000 },
  750. { CR_RTS_CTS_RATE, 0x02030203 },
  751. { CR_AFTER_PNP, 0x1 },
  752. { CR_WEP_PROTECT, 0x114 },
  753. { CR_IFS_VALUE, IFS_VALUE_DEFAULT },
  754. };
  755. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  756. r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  757. if (r)
  758. return r;
  759. return chip->is_zd1211b ?
  760. zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip);
  761. }
  762. struct aw_pt_bi {
  763. u32 atim_wnd_period;
  764. u32 pre_tbtt;
  765. u32 beacon_interval;
  766. };
  767. static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  768. {
  769. int r;
  770. static const zd_addr_t aw_pt_bi_addr[] =
  771. { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL };
  772. u32 values[3];
  773. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  774. ARRAY_SIZE(aw_pt_bi_addr));
  775. if (r) {
  776. memset(s, 0, sizeof(*s));
  777. return r;
  778. }
  779. s->atim_wnd_period = values[0];
  780. s->pre_tbtt = values[1];
  781. s->beacon_interval = values[2];
  782. dev_dbg_f(zd_chip_dev(chip), "aw %u pt %u bi %u\n",
  783. s->atim_wnd_period, s->pre_tbtt, s->beacon_interval);
  784. return 0;
  785. }
  786. static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  787. {
  788. struct zd_ioreq32 reqs[3];
  789. if (s->beacon_interval <= 5)
  790. s->beacon_interval = 5;
  791. if (s->pre_tbtt < 4 || s->pre_tbtt >= s->beacon_interval)
  792. s->pre_tbtt = s->beacon_interval - 1;
  793. if (s->atim_wnd_period >= s->pre_tbtt)
  794. s->atim_wnd_period = s->pre_tbtt - 1;
  795. reqs[0].addr = CR_ATIM_WND_PERIOD;
  796. reqs[0].value = s->atim_wnd_period;
  797. reqs[1].addr = CR_PRE_TBTT;
  798. reqs[1].value = s->pre_tbtt;
  799. reqs[2].addr = CR_BCN_INTERVAL;
  800. reqs[2].value = s->beacon_interval;
  801. dev_dbg_f(zd_chip_dev(chip),
  802. "aw %u pt %u bi %u\n", s->atim_wnd_period, s->pre_tbtt,
  803. s->beacon_interval);
  804. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  805. }
  806. static int set_beacon_interval(struct zd_chip *chip, u32 interval)
  807. {
  808. int r;
  809. struct aw_pt_bi s;
  810. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  811. r = get_aw_pt_bi(chip, &s);
  812. if (r)
  813. return r;
  814. s.beacon_interval = interval;
  815. return set_aw_pt_bi(chip, &s);
  816. }
  817. int zd_set_beacon_interval(struct zd_chip *chip, u32 interval)
  818. {
  819. int r;
  820. mutex_lock(&chip->mutex);
  821. r = set_beacon_interval(chip, interval);
  822. mutex_unlock(&chip->mutex);
  823. return r;
  824. }
  825. static int hw_init(struct zd_chip *chip)
  826. {
  827. int r;
  828. dev_dbg_f(zd_chip_dev(chip), "\n");
  829. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  830. r = hw_reset_phy(chip);
  831. if (r)
  832. return r;
  833. r = hw_init_hmac(chip);
  834. if (r)
  835. return r;
  836. return set_beacon_interval(chip, 100);
  837. }
  838. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  839. {
  840. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  841. }
  842. #ifdef DEBUG
  843. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  844. const char *addr_string)
  845. {
  846. int r;
  847. u32 value;
  848. r = zd_ioread32_locked(chip, &value, addr);
  849. if (r) {
  850. dev_dbg_f(zd_chip_dev(chip),
  851. "error reading %s. Error number %d\n", addr_string, r);
  852. return r;
  853. }
  854. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  855. addr_string, (unsigned int)value);
  856. return 0;
  857. }
  858. static int test_init(struct zd_chip *chip)
  859. {
  860. int r;
  861. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  862. if (r)
  863. return r;
  864. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  865. if (r)
  866. return r;
  867. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  868. }
  869. static void dump_fw_registers(struct zd_chip *chip)
  870. {
  871. const zd_addr_t addr[4] = {
  872. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  873. fw_reg_addr(chip, FW_REG_USB_SPEED),
  874. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  875. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  876. };
  877. int r;
  878. u16 values[4];
  879. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  880. ARRAY_SIZE(addr));
  881. if (r) {
  882. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  883. r);
  884. return;
  885. }
  886. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  887. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  888. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  889. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  890. }
  891. #endif /* DEBUG */
  892. static int print_fw_version(struct zd_chip *chip)
  893. {
  894. int r;
  895. u16 version;
  896. r = zd_ioread16_locked(chip, &version,
  897. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  898. if (r)
  899. return r;
  900. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  901. return 0;
  902. }
  903. static int set_mandatory_rates(struct zd_chip *chip, enum ieee80211_std std)
  904. {
  905. u32 rates;
  906. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  907. /* This sets the mandatory rates, which only depend from the standard
  908. * that the device is supporting. Until further notice we should try
  909. * to support 802.11g also for full speed USB.
  910. */
  911. switch (std) {
  912. case IEEE80211B:
  913. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  914. break;
  915. case IEEE80211G:
  916. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  917. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  918. break;
  919. default:
  920. return -EINVAL;
  921. }
  922. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  923. }
  924. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  925. u8 rts_rate, int preamble)
  926. {
  927. int rts_mod = ZD_RX_CCK;
  928. u32 value = 0;
  929. /* Modulation bit */
  930. if (ZD_CS_TYPE(rts_rate) == ZD_CS_OFDM)
  931. rts_mod = ZD_RX_OFDM;
  932. dev_dbg_f(zd_chip_dev(chip), "rts_rate=%x preamble=%x\n",
  933. rts_rate, preamble);
  934. value |= rts_rate << RTSCTS_SH_RTS_RATE;
  935. value |= rts_mod << RTSCTS_SH_RTS_MOD_TYPE;
  936. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  937. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  938. /* We always send 11M self-CTS messages, like the vendor driver. */
  939. value |= ZD_CCK_RATE_11M << RTSCTS_SH_CTS_RATE;
  940. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  941. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  942. }
  943. int zd_chip_enable_hwint(struct zd_chip *chip)
  944. {
  945. int r;
  946. mutex_lock(&chip->mutex);
  947. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  948. mutex_unlock(&chip->mutex);
  949. return r;
  950. }
  951. static int disable_hwint(struct zd_chip *chip)
  952. {
  953. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  954. }
  955. int zd_chip_disable_hwint(struct zd_chip *chip)
  956. {
  957. int r;
  958. mutex_lock(&chip->mutex);
  959. r = disable_hwint(chip);
  960. mutex_unlock(&chip->mutex);
  961. return r;
  962. }
  963. static int read_fw_regs_offset(struct zd_chip *chip)
  964. {
  965. int r;
  966. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  967. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  968. FWRAW_REGS_ADDR);
  969. if (r)
  970. return r;
  971. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  972. (u16)chip->fw_regs_base);
  973. return 0;
  974. }
  975. int zd_chip_init_hw(struct zd_chip *chip, u8 device_type)
  976. {
  977. int r;
  978. u8 rf_type;
  979. dev_dbg_f(zd_chip_dev(chip), "\n");
  980. mutex_lock(&chip->mutex);
  981. chip->is_zd1211b = (device_type == DEVICE_ZD1211B) != 0;
  982. #ifdef DEBUG
  983. r = test_init(chip);
  984. if (r)
  985. goto out;
  986. #endif
  987. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  988. if (r)
  989. goto out;
  990. r = read_fw_regs_offset(chip);
  991. if (r)
  992. goto out;
  993. /* GPI is always disabled, also in the other driver.
  994. */
  995. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  996. if (r)
  997. goto out;
  998. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  999. if (r)
  1000. goto out;
  1001. /* Currently we support IEEE 802.11g for full and high speed USB.
  1002. * It might be discussed, whether we should suppport pure b mode for
  1003. * full speed USB.
  1004. */
  1005. r = set_mandatory_rates(chip, IEEE80211G);
  1006. if (r)
  1007. goto out;
  1008. /* Disabling interrupts is certainly a smart thing here.
  1009. */
  1010. r = disable_hwint(chip);
  1011. if (r)
  1012. goto out;
  1013. r = read_pod(chip, &rf_type);
  1014. if (r)
  1015. goto out;
  1016. r = hw_init(chip);
  1017. if (r)
  1018. goto out;
  1019. r = zd_rf_init_hw(&chip->rf, rf_type);
  1020. if (r)
  1021. goto out;
  1022. r = print_fw_version(chip);
  1023. if (r)
  1024. goto out;
  1025. #ifdef DEBUG
  1026. dump_fw_registers(chip);
  1027. r = test_init(chip);
  1028. if (r)
  1029. goto out;
  1030. #endif /* DEBUG */
  1031. r = read_e2p_mac_addr(chip);
  1032. if (r)
  1033. goto out;
  1034. r = read_cal_int_tables(chip);
  1035. if (r)
  1036. goto out;
  1037. print_id(chip);
  1038. out:
  1039. mutex_unlock(&chip->mutex);
  1040. return r;
  1041. }
  1042. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  1043. {
  1044. u8 value = chip->pwr_int_values[channel - 1];
  1045. dev_dbg_f(zd_chip_dev(chip), "channel %d pwr_int %#04x\n",
  1046. channel, value);
  1047. return zd_iowrite16_locked(chip, value, CR31);
  1048. }
  1049. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  1050. {
  1051. u8 value = chip->pwr_cal_values[channel-1];
  1052. dev_dbg_f(zd_chip_dev(chip), "channel %d pwr_cal %#04x\n",
  1053. channel, value);
  1054. return zd_iowrite16_locked(chip, value, CR68);
  1055. }
  1056. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  1057. {
  1058. struct zd_ioreq16 ioreqs[3];
  1059. ioreqs[0].addr = CR67;
  1060. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1061. ioreqs[1].addr = CR66;
  1062. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1063. ioreqs[2].addr = CR65;
  1064. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1065. dev_dbg_f(zd_chip_dev(chip),
  1066. "channel %d ofdm_cal 36M %#04x 48M %#04x 54M %#04x\n",
  1067. channel, ioreqs[0].value, ioreqs[1].value, ioreqs[2].value);
  1068. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1069. }
  1070. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1071. u8 channel)
  1072. {
  1073. int r;
  1074. r = update_pwr_int(chip, channel);
  1075. if (r)
  1076. return r;
  1077. if (chip->is_zd1211b) {
  1078. static const struct zd_ioreq16 ioreqs[] = {
  1079. { CR69, 0x28 },
  1080. {},
  1081. { CR69, 0x2a },
  1082. };
  1083. r = update_ofdm_cal(chip, channel);
  1084. if (r)
  1085. return r;
  1086. r = update_pwr_cal(chip, channel);
  1087. if (r)
  1088. return r;
  1089. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1090. if (r)
  1091. return r;
  1092. }
  1093. return 0;
  1094. }
  1095. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1096. static int patch_cck_gain(struct zd_chip *chip)
  1097. {
  1098. int r;
  1099. u32 value;
  1100. if (!chip->patch_cck_gain)
  1101. return 0;
  1102. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1103. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1104. if (r)
  1105. return r;
  1106. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1107. return zd_iowrite16_locked(chip, value & 0xff, CR47);
  1108. }
  1109. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1110. {
  1111. int r, t;
  1112. mutex_lock(&chip->mutex);
  1113. r = zd_chip_lock_phy_regs(chip);
  1114. if (r)
  1115. goto out;
  1116. r = zd_rf_set_channel(&chip->rf, channel);
  1117. if (r)
  1118. goto unlock;
  1119. r = update_channel_integration_and_calibration(chip, channel);
  1120. if (r)
  1121. goto unlock;
  1122. r = patch_cck_gain(chip);
  1123. if (r)
  1124. goto unlock;
  1125. r = patch_6m_band_edge(chip, channel);
  1126. if (r)
  1127. goto unlock;
  1128. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1129. unlock:
  1130. t = zd_chip_unlock_phy_regs(chip);
  1131. if (t && !r)
  1132. r = t;
  1133. out:
  1134. mutex_unlock(&chip->mutex);
  1135. return r;
  1136. }
  1137. u8 zd_chip_get_channel(struct zd_chip *chip)
  1138. {
  1139. u8 channel;
  1140. mutex_lock(&chip->mutex);
  1141. channel = chip->rf.channel;
  1142. mutex_unlock(&chip->mutex);
  1143. return channel;
  1144. }
  1145. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1146. {
  1147. const zd_addr_t a[] = {
  1148. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1149. CR_LED,
  1150. };
  1151. int r;
  1152. u16 v[ARRAY_SIZE(a)];
  1153. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1154. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1155. [1] = { CR_LED },
  1156. };
  1157. u16 other_led;
  1158. mutex_lock(&chip->mutex);
  1159. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1160. if (r)
  1161. goto out;
  1162. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1163. switch (status) {
  1164. case LED_OFF:
  1165. ioreqs[0].value = FW_LINK_OFF;
  1166. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1167. break;
  1168. case LED_SCANNING:
  1169. ioreqs[0].value = FW_LINK_OFF;
  1170. ioreqs[1].value = v[1] & ~other_led;
  1171. if (get_seconds() % 3 == 0) {
  1172. ioreqs[1].value &= ~chip->link_led;
  1173. } else {
  1174. ioreqs[1].value |= chip->link_led;
  1175. }
  1176. break;
  1177. case LED_ASSOCIATED:
  1178. ioreqs[0].value = FW_LINK_TX;
  1179. ioreqs[1].value = v[1] & ~other_led;
  1180. ioreqs[1].value |= chip->link_led;
  1181. break;
  1182. default:
  1183. r = -EINVAL;
  1184. goto out;
  1185. }
  1186. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1187. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1188. if (r)
  1189. goto out;
  1190. }
  1191. r = 0;
  1192. out:
  1193. mutex_unlock(&chip->mutex);
  1194. return r;
  1195. }
  1196. int zd_chip_set_basic_rates_locked(struct zd_chip *chip, u16 cr_rates)
  1197. {
  1198. ZD_ASSERT((cr_rates & ~(CR_RATES_80211B | CR_RATES_80211G)) == 0);
  1199. dev_dbg_f(zd_chip_dev(chip), "%x\n", cr_rates);
  1200. return zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1201. }
  1202. static int ofdm_qual_db(u8 status_quality, u8 rate, unsigned int size)
  1203. {
  1204. static const u16 constants[] = {
  1205. 715, 655, 585, 540, 470, 410, 360, 315,
  1206. 270, 235, 205, 175, 150, 125, 105, 85,
  1207. 65, 50, 40, 25, 15
  1208. };
  1209. int i;
  1210. u32 x;
  1211. /* It seems that their quality parameter is somehow per signal
  1212. * and is now transferred per bit.
  1213. */
  1214. switch (rate) {
  1215. case ZD_OFDM_RATE_6M:
  1216. case ZD_OFDM_RATE_12M:
  1217. case ZD_OFDM_RATE_24M:
  1218. size *= 2;
  1219. break;
  1220. case ZD_OFDM_RATE_9M:
  1221. case ZD_OFDM_RATE_18M:
  1222. case ZD_OFDM_RATE_36M:
  1223. case ZD_OFDM_RATE_54M:
  1224. size *= 4;
  1225. size /= 3;
  1226. break;
  1227. case ZD_OFDM_RATE_48M:
  1228. size *= 3;
  1229. size /= 2;
  1230. break;
  1231. default:
  1232. return -EINVAL;
  1233. }
  1234. x = (10000 * status_quality)/size;
  1235. for (i = 0; i < ARRAY_SIZE(constants); i++) {
  1236. if (x > constants[i])
  1237. break;
  1238. }
  1239. switch (rate) {
  1240. case ZD_OFDM_RATE_6M:
  1241. case ZD_OFDM_RATE_9M:
  1242. i += 3;
  1243. break;
  1244. case ZD_OFDM_RATE_12M:
  1245. case ZD_OFDM_RATE_18M:
  1246. i += 5;
  1247. break;
  1248. case ZD_OFDM_RATE_24M:
  1249. case ZD_OFDM_RATE_36M:
  1250. i += 9;
  1251. break;
  1252. case ZD_OFDM_RATE_48M:
  1253. case ZD_OFDM_RATE_54M:
  1254. i += 15;
  1255. break;
  1256. default:
  1257. return -EINVAL;
  1258. }
  1259. return i;
  1260. }
  1261. static int ofdm_qual_percent(u8 status_quality, u8 rate, unsigned int size)
  1262. {
  1263. int r;
  1264. r = ofdm_qual_db(status_quality, rate, size);
  1265. ZD_ASSERT(r >= 0);
  1266. if (r < 0)
  1267. r = 0;
  1268. r = (r * 100)/29;
  1269. return r <= 100 ? r : 100;
  1270. }
  1271. static unsigned int log10times100(unsigned int x)
  1272. {
  1273. static const u8 log10[] = {
  1274. 0,
  1275. 0, 30, 47, 60, 69, 77, 84, 90, 95, 100,
  1276. 104, 107, 111, 114, 117, 120, 123, 125, 127, 130,
  1277. 132, 134, 136, 138, 139, 141, 143, 144, 146, 147,
  1278. 149, 150, 151, 153, 154, 155, 156, 157, 159, 160,
  1279. 161, 162, 163, 164, 165, 166, 167, 168, 169, 169,
  1280. 170, 171, 172, 173, 174, 174, 175, 176, 177, 177,
  1281. 178, 179, 179, 180, 181, 181, 182, 183, 183, 184,
  1282. 185, 185, 186, 186, 187, 188, 188, 189, 189, 190,
  1283. 190, 191, 191, 192, 192, 193, 193, 194, 194, 195,
  1284. 195, 196, 196, 197, 197, 198, 198, 199, 199, 200,
  1285. 200, 200, 201, 201, 202, 202, 202, 203, 203, 204,
  1286. 204, 204, 205, 205, 206, 206, 206, 207, 207, 207,
  1287. 208, 208, 208, 209, 209, 210, 210, 210, 211, 211,
  1288. 211, 212, 212, 212, 213, 213, 213, 213, 214, 214,
  1289. 214, 215, 215, 215, 216, 216, 216, 217, 217, 217,
  1290. 217, 218, 218, 218, 219, 219, 219, 219, 220, 220,
  1291. 220, 220, 221, 221, 221, 222, 222, 222, 222, 223,
  1292. 223, 223, 223, 224, 224, 224, 224,
  1293. };
  1294. return x < ARRAY_SIZE(log10) ? log10[x] : 225;
  1295. }
  1296. enum {
  1297. MAX_CCK_EVM_DB = 45,
  1298. };
  1299. static int cck_evm_db(u8 status_quality)
  1300. {
  1301. return (20 * log10times100(status_quality)) / 100;
  1302. }
  1303. static int cck_snr_db(u8 status_quality)
  1304. {
  1305. int r = MAX_CCK_EVM_DB - cck_evm_db(status_quality);
  1306. ZD_ASSERT(r >= 0);
  1307. return r;
  1308. }
  1309. static int cck_qual_percent(u8 status_quality)
  1310. {
  1311. int r;
  1312. r = cck_snr_db(status_quality);
  1313. r = (100*r)/17;
  1314. return r <= 100 ? r : 100;
  1315. }
  1316. u8 zd_rx_qual_percent(const void *rx_frame, unsigned int size,
  1317. const struct rx_status *status)
  1318. {
  1319. return (status->frame_status&ZD_RX_OFDM) ?
  1320. ofdm_qual_percent(status->signal_quality_ofdm,
  1321. zd_ofdm_plcp_header_rate(rx_frame),
  1322. size) :
  1323. cck_qual_percent(status->signal_quality_cck);
  1324. }
  1325. u8 zd_rx_strength_percent(u8 rssi)
  1326. {
  1327. int r = (rssi*100) / 41;
  1328. if (r > 100)
  1329. r = 100;
  1330. return (u8) r;
  1331. }
  1332. u16 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1333. {
  1334. static const u16 ofdm_rates[] = {
  1335. [ZD_OFDM_RATE_6M] = 60,
  1336. [ZD_OFDM_RATE_9M] = 90,
  1337. [ZD_OFDM_RATE_12M] = 120,
  1338. [ZD_OFDM_RATE_18M] = 180,
  1339. [ZD_OFDM_RATE_24M] = 240,
  1340. [ZD_OFDM_RATE_36M] = 360,
  1341. [ZD_OFDM_RATE_48M] = 480,
  1342. [ZD_OFDM_RATE_54M] = 540,
  1343. };
  1344. u16 rate;
  1345. if (status->frame_status & ZD_RX_OFDM) {
  1346. u8 ofdm_rate = zd_ofdm_plcp_header_rate(rx_frame);
  1347. rate = ofdm_rates[ofdm_rate & 0xf];
  1348. } else {
  1349. u8 cck_rate = zd_cck_plcp_header_rate(rx_frame);
  1350. switch (cck_rate) {
  1351. case ZD_CCK_SIGNAL_1M:
  1352. rate = 10;
  1353. break;
  1354. case ZD_CCK_SIGNAL_2M:
  1355. rate = 20;
  1356. break;
  1357. case ZD_CCK_SIGNAL_5M5:
  1358. rate = 55;
  1359. break;
  1360. case ZD_CCK_SIGNAL_11M:
  1361. rate = 110;
  1362. break;
  1363. default:
  1364. rate = 0;
  1365. }
  1366. }
  1367. return rate;
  1368. }
  1369. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1370. {
  1371. int r;
  1372. mutex_lock(&chip->mutex);
  1373. r = zd_switch_radio_on(&chip->rf);
  1374. mutex_unlock(&chip->mutex);
  1375. return r;
  1376. }
  1377. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1378. {
  1379. int r;
  1380. mutex_lock(&chip->mutex);
  1381. r = zd_switch_radio_off(&chip->rf);
  1382. mutex_unlock(&chip->mutex);
  1383. return r;
  1384. }
  1385. int zd_chip_enable_int(struct zd_chip *chip)
  1386. {
  1387. int r;
  1388. mutex_lock(&chip->mutex);
  1389. r = zd_usb_enable_int(&chip->usb);
  1390. mutex_unlock(&chip->mutex);
  1391. return r;
  1392. }
  1393. void zd_chip_disable_int(struct zd_chip *chip)
  1394. {
  1395. mutex_lock(&chip->mutex);
  1396. zd_usb_disable_int(&chip->usb);
  1397. mutex_unlock(&chip->mutex);
  1398. }
  1399. int zd_chip_enable_rx(struct zd_chip *chip)
  1400. {
  1401. int r;
  1402. mutex_lock(&chip->mutex);
  1403. r = zd_usb_enable_rx(&chip->usb);
  1404. mutex_unlock(&chip->mutex);
  1405. return r;
  1406. }
  1407. void zd_chip_disable_rx(struct zd_chip *chip)
  1408. {
  1409. mutex_lock(&chip->mutex);
  1410. zd_usb_disable_rx(&chip->usb);
  1411. mutex_unlock(&chip->mutex);
  1412. }
  1413. int zd_rfwritev_locked(struct zd_chip *chip,
  1414. const u32* values, unsigned int count, u8 bits)
  1415. {
  1416. int r;
  1417. unsigned int i;
  1418. for (i = 0; i < count; i++) {
  1419. r = zd_rfwrite_locked(chip, values[i], bits);
  1420. if (r)
  1421. return r;
  1422. }
  1423. return 0;
  1424. }
  1425. /*
  1426. * We can optionally program the RF directly through CR regs, if supported by
  1427. * the hardware. This is much faster than the older method.
  1428. */
  1429. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1430. {
  1431. struct zd_ioreq16 ioreqs[] = {
  1432. { CR244, (value >> 16) & 0xff },
  1433. { CR243, (value >> 8) & 0xff },
  1434. { CR242, value & 0xff },
  1435. };
  1436. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1437. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1438. }
  1439. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1440. const u32 *values, unsigned int count)
  1441. {
  1442. int r;
  1443. unsigned int i;
  1444. for (i = 0; i < count; i++) {
  1445. r = zd_rfwrite_cr_locked(chip, values[i]);
  1446. if (r)
  1447. return r;
  1448. }
  1449. return 0;
  1450. }
  1451. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1452. struct zd_mc_hash *hash)
  1453. {
  1454. struct zd_ioreq32 ioreqs[] = {
  1455. { CR_GROUP_HASH_P1, hash->low },
  1456. { CR_GROUP_HASH_P2, hash->high },
  1457. };
  1458. dev_dbg_f(zd_chip_dev(chip), "hash l 0x%08x h 0x%08x\n",
  1459. ioreqs[0].value, ioreqs[1].value);
  1460. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1461. }