zd_chip.c 41 KB

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