zd_chip.c 40 KB

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