zd_chip.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* This file implements all the hardware specific functions for the ZD1211
  21. * and ZD1211B chips. Support for the ZD1211B was possible after Timothy
  22. * Legge sent me a ZD1211B device. Thank you Tim. -- Uli
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include "zd_def.h"
  28. #include "zd_chip.h"
  29. #include "zd_mac.h"
  30. #include "zd_rf.h"
  31. void zd_chip_init(struct zd_chip *chip,
  32. struct ieee80211_hw *hw,
  33. struct usb_interface *intf)
  34. {
  35. memset(chip, 0, sizeof(*chip));
  36. mutex_init(&chip->mutex);
  37. zd_usb_init(&chip->usb, hw, intf);
  38. zd_rf_init(&chip->rf);
  39. }
  40. void zd_chip_clear(struct zd_chip *chip)
  41. {
  42. ZD_ASSERT(!mutex_is_locked(&chip->mutex));
  43. zd_usb_clear(&chip->usb);
  44. zd_rf_clear(&chip->rf);
  45. mutex_destroy(&chip->mutex);
  46. ZD_MEMCLEAR(chip, sizeof(*chip));
  47. }
  48. static int scnprint_mac_oui(struct zd_chip *chip, char *buffer, size_t size)
  49. {
  50. u8 *addr = zd_mac_get_perm_addr(zd_chip_to_mac(chip));
  51. return scnprintf(buffer, size, "%02x-%02x-%02x",
  52. addr[0], addr[1], addr[2]);
  53. }
  54. /* Prints an identifier line, which will support debugging. */
  55. static int scnprint_id(struct zd_chip *chip, char *buffer, size_t size)
  56. {
  57. int i = 0;
  58. i = scnprintf(buffer, size, "zd1211%s chip ",
  59. zd_chip_is_zd1211b(chip) ? "b" : "");
  60. i += zd_usb_scnprint_id(&chip->usb, buffer+i, size-i);
  61. i += scnprintf(buffer+i, size-i, " ");
  62. i += scnprint_mac_oui(chip, buffer+i, size-i);
  63. i += scnprintf(buffer+i, size-i, " ");
  64. i += zd_rf_scnprint_id(&chip->rf, buffer+i, size-i);
  65. i += scnprintf(buffer+i, size-i, " pa%1x %c%c%c%c%c", chip->pa_type,
  66. chip->patch_cck_gain ? 'g' : '-',
  67. chip->patch_cr157 ? '7' : '-',
  68. chip->patch_6m_band_edge ? '6' : '-',
  69. chip->new_phy_layout ? 'N' : '-',
  70. chip->al2230s_bit ? 'S' : '-');
  71. return i;
  72. }
  73. static void print_id(struct zd_chip *chip)
  74. {
  75. char buffer[80];
  76. scnprint_id(chip, buffer, sizeof(buffer));
  77. buffer[sizeof(buffer)-1] = 0;
  78. dev_info(zd_chip_dev(chip), "%s\n", buffer);
  79. }
  80. static zd_addr_t inc_addr(zd_addr_t addr)
  81. {
  82. u16 a = (u16)addr;
  83. /* Control registers use byte addressing, but everything else uses word
  84. * addressing. */
  85. if ((a & 0xf000) == CR_START)
  86. a += 2;
  87. else
  88. a += 1;
  89. return (zd_addr_t)a;
  90. }
  91. /* Read a variable number of 32-bit values. Parameter count is not allowed to
  92. * exceed USB_MAX_IOREAD32_COUNT.
  93. */
  94. int zd_ioread32v_locked(struct zd_chip *chip, u32 *values, const zd_addr_t *addr,
  95. unsigned int count)
  96. {
  97. int r;
  98. int i;
  99. zd_addr_t a16[USB_MAX_IOREAD32_COUNT * 2];
  100. u16 v16[USB_MAX_IOREAD32_COUNT * 2];
  101. unsigned int count16;
  102. if (count > USB_MAX_IOREAD32_COUNT)
  103. return -EINVAL;
  104. /* Use stack for values and addresses. */
  105. count16 = 2 * count;
  106. BUG_ON(count16 * sizeof(zd_addr_t) > sizeof(a16));
  107. BUG_ON(count16 * sizeof(u16) > sizeof(v16));
  108. for (i = 0; i < count; i++) {
  109. int j = 2*i;
  110. /* We read the high word always first. */
  111. a16[j] = inc_addr(addr[i]);
  112. a16[j+1] = addr[i];
  113. }
  114. r = zd_ioread16v_locked(chip, v16, a16, count16);
  115. if (r) {
  116. dev_dbg_f(zd_chip_dev(chip),
  117. "error: zd_ioread16v_locked. Error number %d\n", r);
  118. return r;
  119. }
  120. for (i = 0; i < count; i++) {
  121. int j = 2*i;
  122. values[i] = (v16[j] << 16) | v16[j+1];
  123. }
  124. return 0;
  125. }
  126. static int _zd_iowrite32v_async_locked(struct zd_chip *chip,
  127. const struct zd_ioreq32 *ioreqs,
  128. unsigned int count)
  129. {
  130. int i, j, r;
  131. struct zd_ioreq16 ioreqs16[USB_MAX_IOWRITE32_COUNT * 2];
  132. unsigned int count16;
  133. /* Use stack for values and addresses. */
  134. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  135. if (count == 0)
  136. return 0;
  137. if (count > USB_MAX_IOWRITE32_COUNT)
  138. return -EINVAL;
  139. count16 = 2 * count;
  140. BUG_ON(count16 * sizeof(struct zd_ioreq16) > sizeof(ioreqs16));
  141. for (i = 0; i < count; i++) {
  142. j = 2*i;
  143. /* We write the high word always first. */
  144. ioreqs16[j].value = ioreqs[i].value >> 16;
  145. ioreqs16[j].addr = inc_addr(ioreqs[i].addr);
  146. ioreqs16[j+1].value = ioreqs[i].value;
  147. ioreqs16[j+1].addr = ioreqs[i].addr;
  148. }
  149. r = zd_usb_iowrite16v_async(&chip->usb, ioreqs16, count16);
  150. #ifdef DEBUG
  151. if (r) {
  152. dev_dbg_f(zd_chip_dev(chip),
  153. "error %d in zd_usb_write16v\n", r);
  154. }
  155. #endif /* DEBUG */
  156. return r;
  157. }
  158. int _zd_iowrite32v_locked(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  159. unsigned int count)
  160. {
  161. int r;
  162. zd_usb_iowrite16v_async_start(&chip->usb);
  163. r = _zd_iowrite32v_async_locked(chip, ioreqs, count);
  164. if (r) {
  165. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  166. return r;
  167. }
  168. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  169. }
  170. int zd_iowrite16a_locked(struct zd_chip *chip,
  171. const struct zd_ioreq16 *ioreqs, unsigned int count)
  172. {
  173. int r;
  174. unsigned int i, j, t, max;
  175. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  176. zd_usb_iowrite16v_async_start(&chip->usb);
  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_async(&chip->usb, &ioreqs[i], j);
  189. if (r) {
  190. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  191. dev_dbg_f(zd_chip_dev(chip),
  192. "error zd_usb_iowrite16v. Error number %d\n",
  193. r);
  194. return r;
  195. }
  196. }
  197. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  198. }
  199. /* Writes a variable number of 32 bit registers. The functions will split
  200. * that in several USB requests. A split can be forced by inserting an IO
  201. * request with an zero address field.
  202. */
  203. int zd_iowrite32a_locked(struct zd_chip *chip,
  204. const struct zd_ioreq32 *ioreqs, unsigned int count)
  205. {
  206. int r;
  207. unsigned int i, j, t, max;
  208. zd_usb_iowrite16v_async_start(&chip->usb);
  209. for (i = 0; i < count; i += j + t) {
  210. t = 0;
  211. max = count-i;
  212. if (max > USB_MAX_IOWRITE32_COUNT)
  213. max = USB_MAX_IOWRITE32_COUNT;
  214. for (j = 0; j < max; j++) {
  215. if (!ioreqs[i+j].addr) {
  216. t = 1;
  217. break;
  218. }
  219. }
  220. r = _zd_iowrite32v_async_locked(chip, &ioreqs[i], j);
  221. if (r) {
  222. zd_usb_iowrite16v_async_end(&chip->usb, 0);
  223. dev_dbg_f(zd_chip_dev(chip),
  224. "error _zd_iowrite32v_locked."
  225. " Error number %d\n", r);
  226. return r;
  227. }
  228. }
  229. return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */);
  230. }
  231. int zd_ioread16(struct zd_chip *chip, zd_addr_t addr, u16 *value)
  232. {
  233. int r;
  234. mutex_lock(&chip->mutex);
  235. r = zd_ioread16_locked(chip, value, addr);
  236. mutex_unlock(&chip->mutex);
  237. return r;
  238. }
  239. int zd_ioread32(struct zd_chip *chip, zd_addr_t addr, u32 *value)
  240. {
  241. int r;
  242. mutex_lock(&chip->mutex);
  243. r = zd_ioread32_locked(chip, value, addr);
  244. mutex_unlock(&chip->mutex);
  245. return r;
  246. }
  247. int zd_iowrite16(struct zd_chip *chip, zd_addr_t addr, u16 value)
  248. {
  249. int r;
  250. mutex_lock(&chip->mutex);
  251. r = zd_iowrite16_locked(chip, value, addr);
  252. mutex_unlock(&chip->mutex);
  253. return r;
  254. }
  255. int zd_iowrite32(struct zd_chip *chip, zd_addr_t addr, u32 value)
  256. {
  257. int r;
  258. mutex_lock(&chip->mutex);
  259. r = zd_iowrite32_locked(chip, value, addr);
  260. mutex_unlock(&chip->mutex);
  261. return r;
  262. }
  263. int zd_ioread32v(struct zd_chip *chip, const zd_addr_t *addresses,
  264. u32 *values, unsigned int count)
  265. {
  266. int r;
  267. mutex_lock(&chip->mutex);
  268. r = zd_ioread32v_locked(chip, values, addresses, count);
  269. mutex_unlock(&chip->mutex);
  270. return r;
  271. }
  272. int zd_iowrite32a(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs,
  273. unsigned int count)
  274. {
  275. int r;
  276. mutex_lock(&chip->mutex);
  277. r = zd_iowrite32a_locked(chip, ioreqs, count);
  278. mutex_unlock(&chip->mutex);
  279. return r;
  280. }
  281. static int read_pod(struct zd_chip *chip, u8 *rf_type)
  282. {
  283. int r;
  284. u32 value;
  285. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  286. r = zd_ioread32_locked(chip, &value, E2P_POD);
  287. if (r)
  288. goto error;
  289. dev_dbg_f(zd_chip_dev(chip), "E2P_POD %#010x\n", value);
  290. /* FIXME: AL2230 handling (Bit 7 in POD) */
  291. *rf_type = value & 0x0f;
  292. chip->pa_type = (value >> 16) & 0x0f;
  293. chip->patch_cck_gain = (value >> 8) & 0x1;
  294. chip->patch_cr157 = (value >> 13) & 0x1;
  295. chip->patch_6m_band_edge = (value >> 21) & 0x1;
  296. chip->new_phy_layout = (value >> 31) & 0x1;
  297. chip->al2230s_bit = (value >> 7) & 0x1;
  298. chip->link_led = ((value >> 4) & 1) ? LED1 : LED2;
  299. chip->supports_tx_led = 1;
  300. if (value & (1 << 24)) { /* LED scenario */
  301. if (value & (1 << 29))
  302. chip->supports_tx_led = 0;
  303. }
  304. dev_dbg_f(zd_chip_dev(chip),
  305. "RF %s %#01x PA type %#01x patch CCK %d patch CR157 %d "
  306. "patch 6M %d new PHY %d link LED%d tx led %d\n",
  307. zd_rf_name(*rf_type), *rf_type,
  308. chip->pa_type, chip->patch_cck_gain,
  309. chip->patch_cr157, chip->patch_6m_band_edge,
  310. chip->new_phy_layout,
  311. chip->link_led == LED1 ? 1 : 2,
  312. chip->supports_tx_led);
  313. return 0;
  314. error:
  315. *rf_type = 0;
  316. chip->pa_type = 0;
  317. chip->patch_cck_gain = 0;
  318. chip->patch_cr157 = 0;
  319. chip->patch_6m_band_edge = 0;
  320. chip->new_phy_layout = 0;
  321. return r;
  322. }
  323. static int zd_write_mac_addr_common(struct zd_chip *chip, const u8 *mac_addr,
  324. const struct zd_ioreq32 *in_reqs,
  325. const char *type)
  326. {
  327. int r;
  328. struct zd_ioreq32 reqs[2] = {in_reqs[0], in_reqs[1]};
  329. if (mac_addr) {
  330. reqs[0].value = (mac_addr[3] << 24)
  331. | (mac_addr[2] << 16)
  332. | (mac_addr[1] << 8)
  333. | mac_addr[0];
  334. reqs[1].value = (mac_addr[5] << 8)
  335. | mac_addr[4];
  336. dev_dbg_f(zd_chip_dev(chip), "%s addr %pM\n", type, mac_addr);
  337. } else {
  338. dev_dbg_f(zd_chip_dev(chip), "set NULL %s\n", type);
  339. }
  340. mutex_lock(&chip->mutex);
  341. r = zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  342. mutex_unlock(&chip->mutex);
  343. return r;
  344. }
  345. /* MAC address: if custom mac addresses are to be used CR_MAC_ADDR_P1 and
  346. * CR_MAC_ADDR_P2 must be overwritten
  347. */
  348. int zd_write_mac_addr(struct zd_chip *chip, const u8 *mac_addr)
  349. {
  350. static const struct zd_ioreq32 reqs[2] = {
  351. [0] = { .addr = CR_MAC_ADDR_P1 },
  352. [1] = { .addr = CR_MAC_ADDR_P2 },
  353. };
  354. return zd_write_mac_addr_common(chip, mac_addr, reqs, "mac");
  355. }
  356. int zd_write_bssid(struct zd_chip *chip, const u8 *bssid)
  357. {
  358. static const struct zd_ioreq32 reqs[2] = {
  359. [0] = { .addr = CR_BSSID_P1 },
  360. [1] = { .addr = CR_BSSID_P2 },
  361. };
  362. return zd_write_mac_addr_common(chip, bssid, reqs, "bssid");
  363. }
  364. int zd_read_regdomain(struct zd_chip *chip, u8 *regdomain)
  365. {
  366. int r;
  367. u32 value;
  368. mutex_lock(&chip->mutex);
  369. r = zd_ioread32_locked(chip, &value, E2P_SUBID);
  370. mutex_unlock(&chip->mutex);
  371. if (r)
  372. return r;
  373. *regdomain = value >> 16;
  374. dev_dbg_f(zd_chip_dev(chip), "regdomain: %#04x\n", *regdomain);
  375. return 0;
  376. }
  377. static int read_values(struct zd_chip *chip, u8 *values, size_t count,
  378. zd_addr_t e2p_addr, u32 guard)
  379. {
  380. int r;
  381. int i;
  382. u32 v;
  383. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  384. for (i = 0;;) {
  385. r = zd_ioread32_locked(chip, &v,
  386. (zd_addr_t)((u16)e2p_addr+i/2));
  387. if (r)
  388. return r;
  389. v -= guard;
  390. if (i+4 < count) {
  391. values[i++] = v;
  392. values[i++] = v >> 8;
  393. values[i++] = v >> 16;
  394. values[i++] = v >> 24;
  395. continue;
  396. }
  397. for (;i < count; i++)
  398. values[i] = v >> (8*(i%3));
  399. return 0;
  400. }
  401. }
  402. static int read_pwr_cal_values(struct zd_chip *chip)
  403. {
  404. return read_values(chip, chip->pwr_cal_values,
  405. E2P_CHANNEL_COUNT, E2P_PWR_CAL_VALUE1,
  406. 0);
  407. }
  408. static int read_pwr_int_values(struct zd_chip *chip)
  409. {
  410. return read_values(chip, chip->pwr_int_values,
  411. E2P_CHANNEL_COUNT, E2P_PWR_INT_VALUE1,
  412. E2P_PWR_INT_GUARD);
  413. }
  414. static int read_ofdm_cal_values(struct zd_chip *chip)
  415. {
  416. int r;
  417. int i;
  418. static const zd_addr_t addresses[] = {
  419. E2P_36M_CAL_VALUE1,
  420. E2P_48M_CAL_VALUE1,
  421. E2P_54M_CAL_VALUE1,
  422. };
  423. for (i = 0; i < 3; i++) {
  424. r = read_values(chip, chip->ofdm_cal_values[i],
  425. E2P_CHANNEL_COUNT, addresses[i], 0);
  426. if (r)
  427. return r;
  428. }
  429. return 0;
  430. }
  431. static int read_cal_int_tables(struct zd_chip *chip)
  432. {
  433. int r;
  434. r = read_pwr_cal_values(chip);
  435. if (r)
  436. return r;
  437. r = read_pwr_int_values(chip);
  438. if (r)
  439. return r;
  440. r = read_ofdm_cal_values(chip);
  441. if (r)
  442. return r;
  443. return 0;
  444. }
  445. /* phy means physical registers */
  446. int zd_chip_lock_phy_regs(struct zd_chip *chip)
  447. {
  448. int r;
  449. u32 tmp;
  450. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  451. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  452. if (r) {
  453. dev_err(zd_chip_dev(chip), "error ioread32(CR_REG1): %d\n", r);
  454. return r;
  455. }
  456. tmp &= ~UNLOCK_PHY_REGS;
  457. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  458. if (r)
  459. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  460. return r;
  461. }
  462. int zd_chip_unlock_phy_regs(struct zd_chip *chip)
  463. {
  464. int r;
  465. u32 tmp;
  466. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  467. r = zd_ioread32_locked(chip, &tmp, CR_REG1);
  468. if (r) {
  469. dev_err(zd_chip_dev(chip),
  470. "error ioread32(CR_REG1): %d\n", r);
  471. return r;
  472. }
  473. tmp |= UNLOCK_PHY_REGS;
  474. r = zd_iowrite32_locked(chip, tmp, CR_REG1);
  475. if (r)
  476. dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r);
  477. return r;
  478. }
  479. /* ZD_CR157 can be optionally patched by the EEPROM for original ZD1211 */
  480. static int patch_cr157(struct zd_chip *chip)
  481. {
  482. int r;
  483. u16 value;
  484. if (!chip->patch_cr157)
  485. return 0;
  486. r = zd_ioread16_locked(chip, &value, E2P_PHY_REG);
  487. if (r)
  488. return r;
  489. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value >> 8);
  490. return zd_iowrite32_locked(chip, value >> 8, ZD_CR157);
  491. }
  492. /*
  493. * 6M band edge can be optionally overwritten for certain RF's
  494. * Vendor driver says: for FCC regulation, enabled per HWFeature 6M band edge
  495. * bit (for AL2230, AL2230S)
  496. */
  497. static int patch_6m_band_edge(struct zd_chip *chip, u8 channel)
  498. {
  499. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  500. if (!chip->patch_6m_band_edge)
  501. return 0;
  502. return zd_rf_patch_6m_band_edge(&chip->rf, channel);
  503. }
  504. /* Generic implementation of 6M band edge patching, used by most RFs via
  505. * zd_rf_generic_patch_6m() */
  506. int zd_chip_generic_patch_6m_band(struct zd_chip *chip, int channel)
  507. {
  508. struct zd_ioreq16 ioreqs[] = {
  509. { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 },
  510. { ZD_CR47, 0x1e },
  511. };
  512. /* FIXME: Channel 11 is not the edge for all regulatory domains. */
  513. if (channel == 1 || channel == 11)
  514. ioreqs[0].value = 0x12;
  515. dev_dbg_f(zd_chip_dev(chip), "patching for channel %d\n", channel);
  516. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  517. }
  518. static int zd1211_hw_reset_phy(struct zd_chip *chip)
  519. {
  520. static const struct zd_ioreq16 ioreqs[] = {
  521. { ZD_CR0, 0x0a }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 },
  522. { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xa0 },
  523. { ZD_CR10, 0x81 }, { ZD_CR11, 0x00 }, { ZD_CR12, 0x7f },
  524. { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 }, { ZD_CR15, 0x3d },
  525. { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e }, { ZD_CR18, 0x0a },
  526. { ZD_CR19, 0x48 }, { ZD_CR20, 0x0c }, { ZD_CR21, 0x0c },
  527. { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 }, { ZD_CR24, 0x14 },
  528. { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 }, { ZD_CR27, 0x19 },
  529. { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 }, { ZD_CR30, 0x4b },
  530. { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 },
  531. { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 },
  532. { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c },
  533. { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 },
  534. { ZD_CR43, 0x10 }, { ZD_CR44, 0x12 }, { ZD_CR46, 0xff },
  535. { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b },
  536. { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 },
  537. { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 },
  538. { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff },
  539. { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 },
  540. { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 },
  541. { ZD_CR79, 0x68 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 },
  542. { ZD_CR82, 0x00 }, { ZD_CR83, 0x00 }, { ZD_CR84, 0x00 },
  543. { ZD_CR85, 0x02 }, { ZD_CR86, 0x00 }, { ZD_CR87, 0x00 },
  544. { ZD_CR88, 0xff }, { ZD_CR89, 0xfc }, { ZD_CR90, 0x00 },
  545. { ZD_CR91, 0x00 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x08 },
  546. { ZD_CR94, 0x00 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0xff },
  547. { ZD_CR97, 0xe7 }, { ZD_CR98, 0x00 }, { ZD_CR99, 0x00 },
  548. { ZD_CR100, 0x00 }, { ZD_CR101, 0xae }, { ZD_CR102, 0x02 },
  549. { ZD_CR103, 0x00 }, { ZD_CR104, 0x03 }, { ZD_CR105, 0x65 },
  550. { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 }, { ZD_CR108, 0x0a },
  551. { ZD_CR109, 0xaa }, { ZD_CR110, 0xaa }, { ZD_CR111, 0x25 },
  552. { ZD_CR112, 0x25 }, { ZD_CR113, 0x00 }, { ZD_CR119, 0x1e },
  553. { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 },
  554. { },
  555. { ZD_CR5, 0x00 }, { ZD_CR6, 0x00 }, { ZD_CR7, 0x00 },
  556. { ZD_CR8, 0x00 }, { ZD_CR9, 0x20 }, { ZD_CR12, 0xf0 },
  557. { ZD_CR20, 0x0e }, { ZD_CR21, 0x0e }, { ZD_CR27, 0x10 },
  558. { ZD_CR44, 0x33 }, { ZD_CR47, 0x1E }, { ZD_CR83, 0x24 },
  559. { ZD_CR84, 0x04 }, { ZD_CR85, 0x00 }, { ZD_CR86, 0x0C },
  560. { ZD_CR87, 0x12 }, { ZD_CR88, 0x0C }, { ZD_CR89, 0x00 },
  561. { ZD_CR90, 0x10 }, { ZD_CR91, 0x08 }, { ZD_CR93, 0x00 },
  562. { ZD_CR94, 0x01 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0x50 },
  563. { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 }, { ZD_CR101, 0x13 },
  564. { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 },
  565. { ZD_CR105, 0x12 }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 },
  566. { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 },
  567. { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 },
  568. { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR120, 0x4f },
  569. { ZD_CR125, 0xaa }, { ZD_CR127, 0x03 }, { ZD_CR128, 0x14 },
  570. { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 }, { ZD_CR131, 0x0C },
  571. { ZD_CR136, 0xdf }, { ZD_CR137, 0x40 }, { ZD_CR138, 0xa0 },
  572. { ZD_CR139, 0xb0 }, { ZD_CR140, 0x99 }, { ZD_CR141, 0x82 },
  573. { ZD_CR142, 0x54 }, { ZD_CR143, 0x1c }, { ZD_CR144, 0x6c },
  574. { ZD_CR147, 0x07 }, { ZD_CR148, 0x4c }, { ZD_CR149, 0x50 },
  575. { ZD_CR150, 0x0e }, { ZD_CR151, 0x18 }, { ZD_CR160, 0xfe },
  576. { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa },
  577. { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe },
  578. { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba },
  579. { ZD_CR170, 0xba }, { ZD_CR171, 0xba },
  580. /* Note: ZD_CR204 must lead the ZD_CR203 */
  581. { ZD_CR204, 0x7d },
  582. { },
  583. { ZD_CR203, 0x30 },
  584. };
  585. int r, t;
  586. dev_dbg_f(zd_chip_dev(chip), "\n");
  587. r = zd_chip_lock_phy_regs(chip);
  588. if (r)
  589. goto out;
  590. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  591. if (r)
  592. goto unlock;
  593. r = patch_cr157(chip);
  594. unlock:
  595. t = zd_chip_unlock_phy_regs(chip);
  596. if (t && !r)
  597. r = t;
  598. out:
  599. return r;
  600. }
  601. static int zd1211b_hw_reset_phy(struct zd_chip *chip)
  602. {
  603. static const struct zd_ioreq16 ioreqs[] = {
  604. { ZD_CR0, 0x14 }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 },
  605. { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xe0 },
  606. { ZD_CR10, 0x81 },
  607. /* power control { { ZD_CR11, 1 << 6 }, */
  608. { ZD_CR11, 0x00 },
  609. { ZD_CR12, 0xf0 }, { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 },
  610. { ZD_CR15, 0x3d }, { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e },
  611. { ZD_CR18, 0x0a }, { ZD_CR19, 0x48 },
  612. { ZD_CR20, 0x10 }, /* Org:0x0E, ComTrend:RalLink AP */
  613. { ZD_CR21, 0x0e }, { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 },
  614. { ZD_CR24, 0x14 }, { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 },
  615. { ZD_CR27, 0x10 }, { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 },
  616. { ZD_CR30, 0x4b }, /* ASIC/FWT, no jointly decoder */
  617. { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 },
  618. { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 },
  619. { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c },
  620. { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 },
  621. { ZD_CR43, 0x10 }, { ZD_CR44, 0x33 }, { ZD_CR46, 0xff },
  622. { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b },
  623. { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 },
  624. { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 },
  625. { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff },
  626. { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 },
  627. { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 },
  628. { ZD_CR79, 0xf0 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 },
  629. { ZD_CR82, 0x00 }, { ZD_CR83, 0x24 }, { ZD_CR84, 0x04 },
  630. { ZD_CR85, 0x00 }, { ZD_CR86, 0x0c }, { ZD_CR87, 0x12 },
  631. { ZD_CR88, 0x0c }, { ZD_CR89, 0x00 }, { ZD_CR90, 0x58 },
  632. { ZD_CR91, 0x04 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x00 },
  633. { ZD_CR94, 0x01 },
  634. { ZD_CR95, 0x20 }, /* ZD1211B */
  635. { ZD_CR96, 0x50 }, { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 },
  636. { ZD_CR99, 0x00 }, { ZD_CR100, 0x01 }, { ZD_CR101, 0x13 },
  637. { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 },
  638. { ZD_CR105, 0x12 }, { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 },
  639. { ZD_CR108, 0x0a }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 },
  640. { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 },
  641. { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 },
  642. { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR119, 0x1e },
  643. { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 },
  644. { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 },
  645. { ZD_CR131, 0x0c }, { ZD_CR136, 0xdf }, { ZD_CR137, 0xa0 },
  646. { ZD_CR138, 0xa8 }, { ZD_CR139, 0xb4 }, { ZD_CR140, 0x98 },
  647. { ZD_CR141, 0x82 }, { ZD_CR142, 0x53 }, { ZD_CR143, 0x1c },
  648. { ZD_CR144, 0x6c }, { ZD_CR147, 0x07 }, { ZD_CR148, 0x40 },
  649. { ZD_CR149, 0x40 }, /* Org:0x50 ComTrend:RalLink AP */
  650. { ZD_CR150, 0x14 }, /* Org:0x0E ComTrend:RalLink AP */
  651. { ZD_CR151, 0x18 }, { ZD_CR159, 0x70 }, { ZD_CR160, 0xfe },
  652. { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa },
  653. { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe },
  654. { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba },
  655. { ZD_CR170, 0xba }, { ZD_CR171, 0xba },
  656. /* Note: ZD_CR204 must lead the ZD_CR203 */
  657. { ZD_CR204, 0x7d },
  658. {},
  659. { ZD_CR203, 0x30 },
  660. };
  661. int r, t;
  662. dev_dbg_f(zd_chip_dev(chip), "\n");
  663. r = zd_chip_lock_phy_regs(chip);
  664. if (r)
  665. goto out;
  666. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  667. t = zd_chip_unlock_phy_regs(chip);
  668. if (t && !r)
  669. r = t;
  670. out:
  671. return r;
  672. }
  673. static int hw_reset_phy(struct zd_chip *chip)
  674. {
  675. return zd_chip_is_zd1211b(chip) ? zd1211b_hw_reset_phy(chip) :
  676. zd1211_hw_reset_phy(chip);
  677. }
  678. static int zd1211_hw_init_hmac(struct zd_chip *chip)
  679. {
  680. static const struct zd_ioreq32 ioreqs[] = {
  681. { CR_ZD1211_RETRY_MAX, ZD1211_RETRY_COUNT },
  682. { CR_RX_THRESHOLD, 0x000c0640 },
  683. };
  684. dev_dbg_f(zd_chip_dev(chip), "\n");
  685. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  686. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  687. }
  688. static int zd1211b_hw_init_hmac(struct zd_chip *chip)
  689. {
  690. static const struct zd_ioreq32 ioreqs[] = {
  691. { CR_ZD1211B_RETRY_MAX, ZD1211B_RETRY_COUNT },
  692. { CR_ZD1211B_CWIN_MAX_MIN_AC0, 0x007f003f },
  693. { CR_ZD1211B_CWIN_MAX_MIN_AC1, 0x007f003f },
  694. { CR_ZD1211B_CWIN_MAX_MIN_AC2, 0x003f001f },
  695. { CR_ZD1211B_CWIN_MAX_MIN_AC3, 0x001f000f },
  696. { CR_ZD1211B_AIFS_CTL1, 0x00280028 },
  697. { CR_ZD1211B_AIFS_CTL2, 0x008C003C },
  698. { CR_ZD1211B_TXOP, 0x01800824 },
  699. { CR_RX_THRESHOLD, 0x000c0eff, },
  700. };
  701. dev_dbg_f(zd_chip_dev(chip), "\n");
  702. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  703. return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  704. }
  705. static int hw_init_hmac(struct zd_chip *chip)
  706. {
  707. int r;
  708. static const struct zd_ioreq32 ioreqs[] = {
  709. { CR_ACK_TIMEOUT_EXT, 0x20 },
  710. { CR_ADDA_MBIAS_WARMTIME, 0x30000808 },
  711. { CR_SNIFFER_ON, 0 },
  712. { CR_RX_FILTER, STA_RX_FILTER },
  713. { CR_GROUP_HASH_P1, 0x00 },
  714. { CR_GROUP_HASH_P2, 0x80000000 },
  715. { CR_REG1, 0xa4 },
  716. { CR_ADDA_PWR_DWN, 0x7f },
  717. { CR_BCN_PLCP_CFG, 0x00f00401 },
  718. { CR_PHY_DELAY, 0x00 },
  719. { CR_ACK_TIMEOUT_EXT, 0x80 },
  720. { CR_ADDA_PWR_DWN, 0x00 },
  721. { CR_ACK_TIME_80211, 0x100 },
  722. { CR_RX_PE_DELAY, 0x70 },
  723. { CR_PS_CTRL, 0x10000000 },
  724. { CR_RTS_CTS_RATE, 0x02030203 },
  725. { CR_AFTER_PNP, 0x1 },
  726. { CR_WEP_PROTECT, 0x114 },
  727. { CR_IFS_VALUE, IFS_VALUE_DEFAULT },
  728. { CR_CAM_MODE, MODE_AP_WDS},
  729. };
  730. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  731. r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  732. if (r)
  733. return r;
  734. return zd_chip_is_zd1211b(chip) ?
  735. zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip);
  736. }
  737. struct aw_pt_bi {
  738. u32 atim_wnd_period;
  739. u32 pre_tbtt;
  740. u32 beacon_interval;
  741. };
  742. static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  743. {
  744. int r;
  745. static const zd_addr_t aw_pt_bi_addr[] =
  746. { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL };
  747. u32 values[3];
  748. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  749. ARRAY_SIZE(aw_pt_bi_addr));
  750. if (r) {
  751. memset(s, 0, sizeof(*s));
  752. return r;
  753. }
  754. s->atim_wnd_period = values[0];
  755. s->pre_tbtt = values[1];
  756. s->beacon_interval = values[2];
  757. return 0;
  758. }
  759. static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s)
  760. {
  761. struct zd_ioreq32 reqs[3];
  762. u16 b_interval = s->beacon_interval & 0xffff;
  763. if (b_interval <= 5)
  764. b_interval = 5;
  765. if (s->pre_tbtt < 4 || s->pre_tbtt >= b_interval)
  766. s->pre_tbtt = b_interval - 1;
  767. if (s->atim_wnd_period >= s->pre_tbtt)
  768. s->atim_wnd_period = s->pre_tbtt - 1;
  769. reqs[0].addr = CR_ATIM_WND_PERIOD;
  770. reqs[0].value = s->atim_wnd_period;
  771. reqs[1].addr = CR_PRE_TBTT;
  772. reqs[1].value = s->pre_tbtt;
  773. reqs[2].addr = CR_BCN_INTERVAL;
  774. reqs[2].value = (s->beacon_interval & ~0xffff) | b_interval;
  775. return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs));
  776. }
  777. static int set_beacon_interval(struct zd_chip *chip, u16 interval,
  778. u8 dtim_period, int type)
  779. {
  780. int r;
  781. struct aw_pt_bi s;
  782. u32 b_interval, mode_flag;
  783. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  784. if (interval > 0) {
  785. switch (type) {
  786. case NL80211_IFTYPE_ADHOC:
  787. case NL80211_IFTYPE_MESH_POINT:
  788. mode_flag = BCN_MODE_IBSS;
  789. break;
  790. case NL80211_IFTYPE_AP:
  791. mode_flag = BCN_MODE_AP;
  792. break;
  793. default:
  794. mode_flag = 0;
  795. break;
  796. }
  797. } else {
  798. dtim_period = 0;
  799. mode_flag = 0;
  800. }
  801. b_interval = mode_flag | (dtim_period << 16) | interval;
  802. r = zd_iowrite32_locked(chip, b_interval, CR_BCN_INTERVAL);
  803. if (r)
  804. return r;
  805. r = get_aw_pt_bi(chip, &s);
  806. if (r)
  807. return r;
  808. return set_aw_pt_bi(chip, &s);
  809. }
  810. int zd_set_beacon_interval(struct zd_chip *chip, u16 interval, u8 dtim_period,
  811. int type)
  812. {
  813. int r;
  814. mutex_lock(&chip->mutex);
  815. r = set_beacon_interval(chip, interval, dtim_period, type);
  816. mutex_unlock(&chip->mutex);
  817. return r;
  818. }
  819. static int hw_init(struct zd_chip *chip)
  820. {
  821. int r;
  822. dev_dbg_f(zd_chip_dev(chip), "\n");
  823. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  824. r = hw_reset_phy(chip);
  825. if (r)
  826. return r;
  827. r = hw_init_hmac(chip);
  828. if (r)
  829. return r;
  830. return set_beacon_interval(chip, 100, 0, NL80211_IFTYPE_UNSPECIFIED);
  831. }
  832. static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset)
  833. {
  834. return (zd_addr_t)((u16)chip->fw_regs_base + offset);
  835. }
  836. #ifdef DEBUG
  837. static int dump_cr(struct zd_chip *chip, const zd_addr_t addr,
  838. const char *addr_string)
  839. {
  840. int r;
  841. u32 value;
  842. r = zd_ioread32_locked(chip, &value, addr);
  843. if (r) {
  844. dev_dbg_f(zd_chip_dev(chip),
  845. "error reading %s. Error number %d\n", addr_string, r);
  846. return r;
  847. }
  848. dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n",
  849. addr_string, (unsigned int)value);
  850. return 0;
  851. }
  852. static int test_init(struct zd_chip *chip)
  853. {
  854. int r;
  855. r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP");
  856. if (r)
  857. return r;
  858. r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN");
  859. if (r)
  860. return r;
  861. return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT");
  862. }
  863. static void dump_fw_registers(struct zd_chip *chip)
  864. {
  865. const zd_addr_t addr[4] = {
  866. fw_reg_addr(chip, FW_REG_FIRMWARE_VER),
  867. fw_reg_addr(chip, FW_REG_USB_SPEED),
  868. fw_reg_addr(chip, FW_REG_FIX_TX_RATE),
  869. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  870. };
  871. int r;
  872. u16 values[4];
  873. r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr,
  874. ARRAY_SIZE(addr));
  875. if (r) {
  876. dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n",
  877. r);
  878. return;
  879. }
  880. dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]);
  881. dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]);
  882. dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]);
  883. dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]);
  884. }
  885. #endif /* DEBUG */
  886. static int print_fw_version(struct zd_chip *chip)
  887. {
  888. struct wiphy *wiphy = zd_chip_to_mac(chip)->hw->wiphy;
  889. int r;
  890. u16 version;
  891. r = zd_ioread16_locked(chip, &version,
  892. fw_reg_addr(chip, FW_REG_FIRMWARE_VER));
  893. if (r)
  894. return r;
  895. dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version);
  896. snprintf(wiphy->fw_version, sizeof(wiphy->fw_version),
  897. "%04hx", version);
  898. return 0;
  899. }
  900. static int set_mandatory_rates(struct zd_chip *chip, int gmode)
  901. {
  902. u32 rates;
  903. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  904. /* This sets the mandatory rates, which only depend from the standard
  905. * that the device is supporting. Until further notice we should try
  906. * to support 802.11g also for full speed USB.
  907. */
  908. if (!gmode)
  909. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M;
  910. else
  911. rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M|
  912. CR_RATE_6M|CR_RATE_12M|CR_RATE_24M;
  913. return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL);
  914. }
  915. int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip,
  916. int preamble)
  917. {
  918. u32 value = 0;
  919. dev_dbg_f(zd_chip_dev(chip), "preamble=%x\n", preamble);
  920. value |= preamble << RTSCTS_SH_RTS_PMB_TYPE;
  921. value |= preamble << RTSCTS_SH_CTS_PMB_TYPE;
  922. /* We always send 11M RTS/self-CTS messages, like the vendor driver. */
  923. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_RTS_RATE;
  924. value |= ZD_RX_CCK << RTSCTS_SH_RTS_MOD_TYPE;
  925. value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE;
  926. value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE;
  927. return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE);
  928. }
  929. int zd_chip_enable_hwint(struct zd_chip *chip)
  930. {
  931. int r;
  932. mutex_lock(&chip->mutex);
  933. r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT);
  934. mutex_unlock(&chip->mutex);
  935. return r;
  936. }
  937. static int disable_hwint(struct zd_chip *chip)
  938. {
  939. return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT);
  940. }
  941. int zd_chip_disable_hwint(struct zd_chip *chip)
  942. {
  943. int r;
  944. mutex_lock(&chip->mutex);
  945. r = disable_hwint(chip);
  946. mutex_unlock(&chip->mutex);
  947. return r;
  948. }
  949. static int read_fw_regs_offset(struct zd_chip *chip)
  950. {
  951. int r;
  952. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  953. r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base,
  954. FWRAW_REGS_ADDR);
  955. if (r)
  956. return r;
  957. dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n",
  958. (u16)chip->fw_regs_base);
  959. return 0;
  960. }
  961. /* Read mac address using pre-firmware interface */
  962. int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr)
  963. {
  964. dev_dbg_f(zd_chip_dev(chip), "\n");
  965. return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr,
  966. ETH_ALEN);
  967. }
  968. int zd_chip_init_hw(struct zd_chip *chip)
  969. {
  970. int r;
  971. u8 rf_type;
  972. dev_dbg_f(zd_chip_dev(chip), "\n");
  973. mutex_lock(&chip->mutex);
  974. #ifdef DEBUG
  975. r = test_init(chip);
  976. if (r)
  977. goto out;
  978. #endif
  979. r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP);
  980. if (r)
  981. goto out;
  982. r = read_fw_regs_offset(chip);
  983. if (r)
  984. goto out;
  985. /* GPI is always disabled, also in the other driver.
  986. */
  987. r = zd_iowrite32_locked(chip, 0, CR_GPI_EN);
  988. if (r)
  989. goto out;
  990. r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX);
  991. if (r)
  992. goto out;
  993. /* Currently we support IEEE 802.11g for full and high speed USB.
  994. * It might be discussed, whether we should suppport pure b mode for
  995. * full speed USB.
  996. */
  997. r = set_mandatory_rates(chip, 1);
  998. if (r)
  999. goto out;
  1000. /* Disabling interrupts is certainly a smart thing here.
  1001. */
  1002. r = disable_hwint(chip);
  1003. if (r)
  1004. goto out;
  1005. r = read_pod(chip, &rf_type);
  1006. if (r)
  1007. goto out;
  1008. r = hw_init(chip);
  1009. if (r)
  1010. goto out;
  1011. r = zd_rf_init_hw(&chip->rf, rf_type);
  1012. if (r)
  1013. goto out;
  1014. r = print_fw_version(chip);
  1015. if (r)
  1016. goto out;
  1017. #ifdef DEBUG
  1018. dump_fw_registers(chip);
  1019. r = test_init(chip);
  1020. if (r)
  1021. goto out;
  1022. #endif /* DEBUG */
  1023. r = read_cal_int_tables(chip);
  1024. if (r)
  1025. goto out;
  1026. print_id(chip);
  1027. out:
  1028. mutex_unlock(&chip->mutex);
  1029. return r;
  1030. }
  1031. static int update_pwr_int(struct zd_chip *chip, u8 channel)
  1032. {
  1033. u8 value = chip->pwr_int_values[channel - 1];
  1034. return zd_iowrite16_locked(chip, value, ZD_CR31);
  1035. }
  1036. static int update_pwr_cal(struct zd_chip *chip, u8 channel)
  1037. {
  1038. u8 value = chip->pwr_cal_values[channel-1];
  1039. return zd_iowrite16_locked(chip, value, ZD_CR68);
  1040. }
  1041. static int update_ofdm_cal(struct zd_chip *chip, u8 channel)
  1042. {
  1043. struct zd_ioreq16 ioreqs[3];
  1044. ioreqs[0].addr = ZD_CR67;
  1045. ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1];
  1046. ioreqs[1].addr = ZD_CR66;
  1047. ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1];
  1048. ioreqs[2].addr = ZD_CR65;
  1049. ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1];
  1050. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1051. }
  1052. static int update_channel_integration_and_calibration(struct zd_chip *chip,
  1053. u8 channel)
  1054. {
  1055. int r;
  1056. if (!zd_rf_should_update_pwr_int(&chip->rf))
  1057. return 0;
  1058. r = update_pwr_int(chip, channel);
  1059. if (r)
  1060. return r;
  1061. if (zd_chip_is_zd1211b(chip)) {
  1062. static const struct zd_ioreq16 ioreqs[] = {
  1063. { ZD_CR69, 0x28 },
  1064. {},
  1065. { ZD_CR69, 0x2a },
  1066. };
  1067. r = update_ofdm_cal(chip, channel);
  1068. if (r)
  1069. return r;
  1070. r = update_pwr_cal(chip, channel);
  1071. if (r)
  1072. return r;
  1073. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1074. if (r)
  1075. return r;
  1076. }
  1077. return 0;
  1078. }
  1079. /* The CCK baseband gain can be optionally patched by the EEPROM */
  1080. static int patch_cck_gain(struct zd_chip *chip)
  1081. {
  1082. int r;
  1083. u32 value;
  1084. if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf))
  1085. return 0;
  1086. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1087. r = zd_ioread32_locked(chip, &value, E2P_PHY_REG);
  1088. if (r)
  1089. return r;
  1090. dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff);
  1091. return zd_iowrite16_locked(chip, value & 0xff, ZD_CR47);
  1092. }
  1093. int zd_chip_set_channel(struct zd_chip *chip, u8 channel)
  1094. {
  1095. int r, t;
  1096. mutex_lock(&chip->mutex);
  1097. r = zd_chip_lock_phy_regs(chip);
  1098. if (r)
  1099. goto out;
  1100. r = zd_rf_set_channel(&chip->rf, channel);
  1101. if (r)
  1102. goto unlock;
  1103. r = update_channel_integration_and_calibration(chip, channel);
  1104. if (r)
  1105. goto unlock;
  1106. r = patch_cck_gain(chip);
  1107. if (r)
  1108. goto unlock;
  1109. r = patch_6m_band_edge(chip, channel);
  1110. if (r)
  1111. goto unlock;
  1112. r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS);
  1113. unlock:
  1114. t = zd_chip_unlock_phy_regs(chip);
  1115. if (t && !r)
  1116. r = t;
  1117. out:
  1118. mutex_unlock(&chip->mutex);
  1119. return r;
  1120. }
  1121. u8 zd_chip_get_channel(struct zd_chip *chip)
  1122. {
  1123. u8 channel;
  1124. mutex_lock(&chip->mutex);
  1125. channel = chip->rf.channel;
  1126. mutex_unlock(&chip->mutex);
  1127. return channel;
  1128. }
  1129. int zd_chip_control_leds(struct zd_chip *chip, enum led_status status)
  1130. {
  1131. const zd_addr_t a[] = {
  1132. fw_reg_addr(chip, FW_REG_LED_LINK_STATUS),
  1133. CR_LED,
  1134. };
  1135. int r;
  1136. u16 v[ARRAY_SIZE(a)];
  1137. struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = {
  1138. [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) },
  1139. [1] = { CR_LED },
  1140. };
  1141. u16 other_led;
  1142. mutex_lock(&chip->mutex);
  1143. r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a));
  1144. if (r)
  1145. goto out;
  1146. other_led = chip->link_led == LED1 ? LED2 : LED1;
  1147. switch (status) {
  1148. case ZD_LED_OFF:
  1149. ioreqs[0].value = FW_LINK_OFF;
  1150. ioreqs[1].value = v[1] & ~(LED1|LED2);
  1151. break;
  1152. case ZD_LED_SCANNING:
  1153. ioreqs[0].value = FW_LINK_OFF;
  1154. ioreqs[1].value = v[1] & ~other_led;
  1155. if (get_seconds() % 3 == 0) {
  1156. ioreqs[1].value &= ~chip->link_led;
  1157. } else {
  1158. ioreqs[1].value |= chip->link_led;
  1159. }
  1160. break;
  1161. case ZD_LED_ASSOCIATED:
  1162. ioreqs[0].value = FW_LINK_TX;
  1163. ioreqs[1].value = v[1] & ~other_led;
  1164. ioreqs[1].value |= chip->link_led;
  1165. break;
  1166. default:
  1167. r = -EINVAL;
  1168. goto out;
  1169. }
  1170. if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) {
  1171. r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1172. if (r)
  1173. goto out;
  1174. }
  1175. r = 0;
  1176. out:
  1177. mutex_unlock(&chip->mutex);
  1178. return r;
  1179. }
  1180. int zd_chip_set_basic_rates(struct zd_chip *chip, u16 cr_rates)
  1181. {
  1182. int r;
  1183. if (cr_rates & ~(CR_RATES_80211B|CR_RATES_80211G))
  1184. return -EINVAL;
  1185. mutex_lock(&chip->mutex);
  1186. r = zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL);
  1187. mutex_unlock(&chip->mutex);
  1188. return r;
  1189. }
  1190. static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame)
  1191. {
  1192. return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame);
  1193. }
  1194. /**
  1195. * zd_rx_rate - report zd-rate
  1196. * @rx_frame - received frame
  1197. * @rx_status - rx_status as given by the device
  1198. *
  1199. * This function converts the rate as encoded in the received packet to the
  1200. * zd-rate, we are using on other places in the driver.
  1201. */
  1202. u8 zd_rx_rate(const void *rx_frame, const struct rx_status *status)
  1203. {
  1204. u8 zd_rate;
  1205. if (status->frame_status & ZD_RX_OFDM) {
  1206. zd_rate = zd_rate_from_ofdm_plcp_header(rx_frame);
  1207. } else {
  1208. switch (zd_cck_plcp_header_signal(rx_frame)) {
  1209. case ZD_CCK_PLCP_SIGNAL_1M:
  1210. zd_rate = ZD_CCK_RATE_1M;
  1211. break;
  1212. case ZD_CCK_PLCP_SIGNAL_2M:
  1213. zd_rate = ZD_CCK_RATE_2M;
  1214. break;
  1215. case ZD_CCK_PLCP_SIGNAL_5M5:
  1216. zd_rate = ZD_CCK_RATE_5_5M;
  1217. break;
  1218. case ZD_CCK_PLCP_SIGNAL_11M:
  1219. zd_rate = ZD_CCK_RATE_11M;
  1220. break;
  1221. default:
  1222. zd_rate = 0;
  1223. }
  1224. }
  1225. return zd_rate;
  1226. }
  1227. int zd_chip_switch_radio_on(struct zd_chip *chip)
  1228. {
  1229. int r;
  1230. mutex_lock(&chip->mutex);
  1231. r = zd_switch_radio_on(&chip->rf);
  1232. mutex_unlock(&chip->mutex);
  1233. return r;
  1234. }
  1235. int zd_chip_switch_radio_off(struct zd_chip *chip)
  1236. {
  1237. int r;
  1238. mutex_lock(&chip->mutex);
  1239. r = zd_switch_radio_off(&chip->rf);
  1240. mutex_unlock(&chip->mutex);
  1241. return r;
  1242. }
  1243. int zd_chip_enable_int(struct zd_chip *chip)
  1244. {
  1245. int r;
  1246. mutex_lock(&chip->mutex);
  1247. r = zd_usb_enable_int(&chip->usb);
  1248. mutex_unlock(&chip->mutex);
  1249. return r;
  1250. }
  1251. void zd_chip_disable_int(struct zd_chip *chip)
  1252. {
  1253. mutex_lock(&chip->mutex);
  1254. zd_usb_disable_int(&chip->usb);
  1255. mutex_unlock(&chip->mutex);
  1256. /* cancel pending interrupt work */
  1257. cancel_work_sync(&zd_chip_to_mac(chip)->process_intr);
  1258. }
  1259. int zd_chip_enable_rxtx(struct zd_chip *chip)
  1260. {
  1261. int r;
  1262. mutex_lock(&chip->mutex);
  1263. zd_usb_enable_tx(&chip->usb);
  1264. r = zd_usb_enable_rx(&chip->usb);
  1265. zd_tx_watchdog_enable(&chip->usb);
  1266. mutex_unlock(&chip->mutex);
  1267. return r;
  1268. }
  1269. void zd_chip_disable_rxtx(struct zd_chip *chip)
  1270. {
  1271. mutex_lock(&chip->mutex);
  1272. zd_tx_watchdog_disable(&chip->usb);
  1273. zd_usb_disable_rx(&chip->usb);
  1274. zd_usb_disable_tx(&chip->usb);
  1275. mutex_unlock(&chip->mutex);
  1276. }
  1277. int zd_rfwritev_locked(struct zd_chip *chip,
  1278. const u32* values, unsigned int count, u8 bits)
  1279. {
  1280. int r;
  1281. unsigned int i;
  1282. for (i = 0; i < count; i++) {
  1283. r = zd_rfwrite_locked(chip, values[i], bits);
  1284. if (r)
  1285. return r;
  1286. }
  1287. return 0;
  1288. }
  1289. /*
  1290. * We can optionally program the RF directly through CR regs, if supported by
  1291. * the hardware. This is much faster than the older method.
  1292. */
  1293. int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value)
  1294. {
  1295. const struct zd_ioreq16 ioreqs[] = {
  1296. { ZD_CR244, (value >> 16) & 0xff },
  1297. { ZD_CR243, (value >> 8) & 0xff },
  1298. { ZD_CR242, value & 0xff },
  1299. };
  1300. ZD_ASSERT(mutex_is_locked(&chip->mutex));
  1301. return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1302. }
  1303. int zd_rfwritev_cr_locked(struct zd_chip *chip,
  1304. const u32 *values, unsigned int count)
  1305. {
  1306. int r;
  1307. unsigned int i;
  1308. for (i = 0; i < count; i++) {
  1309. r = zd_rfwrite_cr_locked(chip, values[i]);
  1310. if (r)
  1311. return r;
  1312. }
  1313. return 0;
  1314. }
  1315. int zd_chip_set_multicast_hash(struct zd_chip *chip,
  1316. struct zd_mc_hash *hash)
  1317. {
  1318. const struct zd_ioreq32 ioreqs[] = {
  1319. { CR_GROUP_HASH_P1, hash->low },
  1320. { CR_GROUP_HASH_P2, hash->high },
  1321. };
  1322. return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs));
  1323. }
  1324. u64 zd_chip_get_tsf(struct zd_chip *chip)
  1325. {
  1326. int r;
  1327. static const zd_addr_t aw_pt_bi_addr[] =
  1328. { CR_TSF_LOW_PART, CR_TSF_HIGH_PART };
  1329. u32 values[2];
  1330. u64 tsf;
  1331. mutex_lock(&chip->mutex);
  1332. r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr,
  1333. ARRAY_SIZE(aw_pt_bi_addr));
  1334. mutex_unlock(&chip->mutex);
  1335. if (r)
  1336. return 0;
  1337. tsf = values[1];
  1338. tsf = (tsf << 32) | values[0];
  1339. return tsf;
  1340. }