zd_chip.c 40 KB

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