zd_chip.c 39 KB

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