11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 26948300cSKalle Valo /* ZD1211 USB-WLAN driver for Linux 36948300cSKalle Valo * 46948300cSKalle Valo * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de> 56948300cSKalle Valo * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org> 66948300cSKalle Valo */ 76948300cSKalle Valo 86948300cSKalle Valo /* This file implements all the hardware specific functions for the ZD1211 96948300cSKalle Valo * and ZD1211B chips. Support for the ZD1211B was possible after Timothy 106948300cSKalle Valo * Legge sent me a ZD1211B device. Thank you Tim. -- Uli 116948300cSKalle Valo */ 126948300cSKalle Valo 136948300cSKalle Valo #include <linux/kernel.h> 146948300cSKalle Valo #include <linux/errno.h> 156948300cSKalle Valo #include <linux/slab.h> 166948300cSKalle Valo 176948300cSKalle Valo #include "zd_def.h" 186948300cSKalle Valo #include "zd_chip.h" 196948300cSKalle Valo #include "zd_mac.h" 206948300cSKalle Valo #include "zd_rf.h" 216948300cSKalle Valo 226948300cSKalle Valo void zd_chip_init(struct zd_chip *chip, 236948300cSKalle Valo struct ieee80211_hw *hw, 246948300cSKalle Valo struct usb_interface *intf) 256948300cSKalle Valo { 266948300cSKalle Valo memset(chip, 0, sizeof(*chip)); 276948300cSKalle Valo mutex_init(&chip->mutex); 286948300cSKalle Valo zd_usb_init(&chip->usb, hw, intf); 296948300cSKalle Valo zd_rf_init(&chip->rf); 306948300cSKalle Valo } 316948300cSKalle Valo 326948300cSKalle Valo void zd_chip_clear(struct zd_chip *chip) 336948300cSKalle Valo { 346948300cSKalle Valo ZD_ASSERT(!mutex_is_locked(&chip->mutex)); 356948300cSKalle Valo zd_usb_clear(&chip->usb); 366948300cSKalle Valo zd_rf_clear(&chip->rf); 376948300cSKalle Valo mutex_destroy(&chip->mutex); 386948300cSKalle Valo ZD_MEMCLEAR(chip, sizeof(*chip)); 396948300cSKalle Valo } 406948300cSKalle Valo 416948300cSKalle Valo static int scnprint_mac_oui(struct zd_chip *chip, char *buffer, size_t size) 426948300cSKalle Valo { 436948300cSKalle Valo u8 *addr = zd_mac_get_perm_addr(zd_chip_to_mac(chip)); 44*d13b12c3SAndy Shevchenko return scnprintf(buffer, size, "%3phD", addr); 456948300cSKalle Valo } 466948300cSKalle Valo 476948300cSKalle Valo /* Prints an identifier line, which will support debugging. */ 486948300cSKalle Valo static int scnprint_id(struct zd_chip *chip, char *buffer, size_t size) 496948300cSKalle Valo { 506948300cSKalle Valo int i = 0; 516948300cSKalle Valo 526948300cSKalle Valo i = scnprintf(buffer, size, "zd1211%s chip ", 536948300cSKalle Valo zd_chip_is_zd1211b(chip) ? "b" : ""); 546948300cSKalle Valo i += zd_usb_scnprint_id(&chip->usb, buffer+i, size-i); 556948300cSKalle Valo i += scnprintf(buffer+i, size-i, " "); 566948300cSKalle Valo i += scnprint_mac_oui(chip, buffer+i, size-i); 576948300cSKalle Valo i += scnprintf(buffer+i, size-i, " "); 586948300cSKalle Valo i += zd_rf_scnprint_id(&chip->rf, buffer+i, size-i); 596948300cSKalle Valo i += scnprintf(buffer+i, size-i, " pa%1x %c%c%c%c%c", chip->pa_type, 606948300cSKalle Valo chip->patch_cck_gain ? 'g' : '-', 616948300cSKalle Valo chip->patch_cr157 ? '7' : '-', 626948300cSKalle Valo chip->patch_6m_band_edge ? '6' : '-', 636948300cSKalle Valo chip->new_phy_layout ? 'N' : '-', 646948300cSKalle Valo chip->al2230s_bit ? 'S' : '-'); 656948300cSKalle Valo return i; 666948300cSKalle Valo } 676948300cSKalle Valo 686948300cSKalle Valo static void print_id(struct zd_chip *chip) 696948300cSKalle Valo { 706948300cSKalle Valo char buffer[80]; 716948300cSKalle Valo 726948300cSKalle Valo scnprint_id(chip, buffer, sizeof(buffer)); 736948300cSKalle Valo buffer[sizeof(buffer)-1] = 0; 746948300cSKalle Valo dev_info(zd_chip_dev(chip), "%s\n", buffer); 756948300cSKalle Valo } 766948300cSKalle Valo 776948300cSKalle Valo static zd_addr_t inc_addr(zd_addr_t addr) 786948300cSKalle Valo { 796948300cSKalle Valo u16 a = (u16)addr; 806948300cSKalle Valo /* Control registers use byte addressing, but everything else uses word 816948300cSKalle Valo * addressing. */ 826948300cSKalle Valo if ((a & 0xf000) == CR_START) 836948300cSKalle Valo a += 2; 846948300cSKalle Valo else 856948300cSKalle Valo a += 1; 866948300cSKalle Valo return (zd_addr_t)a; 876948300cSKalle Valo } 886948300cSKalle Valo 896948300cSKalle Valo /* Read a variable number of 32-bit values. Parameter count is not allowed to 906948300cSKalle Valo * exceed USB_MAX_IOREAD32_COUNT. 916948300cSKalle Valo */ 926948300cSKalle Valo int zd_ioread32v_locked(struct zd_chip *chip, u32 *values, const zd_addr_t *addr, 936948300cSKalle Valo unsigned int count) 946948300cSKalle Valo { 956948300cSKalle Valo int r; 966948300cSKalle Valo int i; 976948300cSKalle Valo zd_addr_t a16[USB_MAX_IOREAD32_COUNT * 2]; 986948300cSKalle Valo u16 v16[USB_MAX_IOREAD32_COUNT * 2]; 996948300cSKalle Valo unsigned int count16; 1006948300cSKalle Valo 1016948300cSKalle Valo if (count > USB_MAX_IOREAD32_COUNT) 1026948300cSKalle Valo return -EINVAL; 1036948300cSKalle Valo 1046948300cSKalle Valo /* Use stack for values and addresses. */ 1056948300cSKalle Valo count16 = 2 * count; 1066948300cSKalle Valo BUG_ON(count16 * sizeof(zd_addr_t) > sizeof(a16)); 1076948300cSKalle Valo BUG_ON(count16 * sizeof(u16) > sizeof(v16)); 1086948300cSKalle Valo 1096948300cSKalle Valo for (i = 0; i < count; i++) { 1106948300cSKalle Valo int j = 2*i; 1116948300cSKalle Valo /* We read the high word always first. */ 1126948300cSKalle Valo a16[j] = inc_addr(addr[i]); 1136948300cSKalle Valo a16[j+1] = addr[i]; 1146948300cSKalle Valo } 1156948300cSKalle Valo 1166948300cSKalle Valo r = zd_ioread16v_locked(chip, v16, a16, count16); 1176948300cSKalle Valo if (r) { 1186948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 1196948300cSKalle Valo "error: %s. Error number %d\n", __func__, r); 1206948300cSKalle Valo return r; 1216948300cSKalle Valo } 1226948300cSKalle Valo 1236948300cSKalle Valo for (i = 0; i < count; i++) { 1246948300cSKalle Valo int j = 2*i; 1256948300cSKalle Valo values[i] = (v16[j] << 16) | v16[j+1]; 1266948300cSKalle Valo } 1276948300cSKalle Valo 1286948300cSKalle Valo return 0; 1296948300cSKalle Valo } 1306948300cSKalle Valo 1316948300cSKalle Valo static int _zd_iowrite32v_async_locked(struct zd_chip *chip, 1326948300cSKalle Valo const struct zd_ioreq32 *ioreqs, 1336948300cSKalle Valo unsigned int count) 1346948300cSKalle Valo { 1356948300cSKalle Valo int i, j, r; 1366948300cSKalle Valo struct zd_ioreq16 ioreqs16[USB_MAX_IOWRITE32_COUNT * 2]; 1376948300cSKalle Valo unsigned int count16; 1386948300cSKalle Valo 1396948300cSKalle Valo /* Use stack for values and addresses. */ 1406948300cSKalle Valo 1416948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 1426948300cSKalle Valo 1436948300cSKalle Valo if (count == 0) 1446948300cSKalle Valo return 0; 1456948300cSKalle Valo if (count > USB_MAX_IOWRITE32_COUNT) 1466948300cSKalle Valo return -EINVAL; 1476948300cSKalle Valo 1486948300cSKalle Valo count16 = 2 * count; 1496948300cSKalle Valo BUG_ON(count16 * sizeof(struct zd_ioreq16) > sizeof(ioreqs16)); 1506948300cSKalle Valo 1516948300cSKalle Valo for (i = 0; i < count; i++) { 1526948300cSKalle Valo j = 2*i; 1536948300cSKalle Valo /* We write the high word always first. */ 1546948300cSKalle Valo ioreqs16[j].value = ioreqs[i].value >> 16; 1556948300cSKalle Valo ioreqs16[j].addr = inc_addr(ioreqs[i].addr); 1566948300cSKalle Valo ioreqs16[j+1].value = ioreqs[i].value; 1576948300cSKalle Valo ioreqs16[j+1].addr = ioreqs[i].addr; 1586948300cSKalle Valo } 1596948300cSKalle Valo 1606948300cSKalle Valo r = zd_usb_iowrite16v_async(&chip->usb, ioreqs16, count16); 1616948300cSKalle Valo #ifdef DEBUG 1626948300cSKalle Valo if (r) { 1636948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 1646948300cSKalle Valo "error %d in zd_usb_write16v\n", r); 1656948300cSKalle Valo } 1666948300cSKalle Valo #endif /* DEBUG */ 1676948300cSKalle Valo return r; 1686948300cSKalle Valo } 1696948300cSKalle Valo 1706948300cSKalle Valo int _zd_iowrite32v_locked(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs, 1716948300cSKalle Valo unsigned int count) 1726948300cSKalle Valo { 1736948300cSKalle Valo int r; 1746948300cSKalle Valo 1756948300cSKalle Valo zd_usb_iowrite16v_async_start(&chip->usb); 1766948300cSKalle Valo r = _zd_iowrite32v_async_locked(chip, ioreqs, count); 1776948300cSKalle Valo if (r) { 1786948300cSKalle Valo zd_usb_iowrite16v_async_end(&chip->usb, 0); 1796948300cSKalle Valo return r; 1806948300cSKalle Valo } 1816948300cSKalle Valo return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */); 1826948300cSKalle Valo } 1836948300cSKalle Valo 1846948300cSKalle Valo int zd_iowrite16a_locked(struct zd_chip *chip, 1856948300cSKalle Valo const struct zd_ioreq16 *ioreqs, unsigned int count) 1866948300cSKalle Valo { 1876948300cSKalle Valo int r; 1886948300cSKalle Valo unsigned int i, j, t, max; 1896948300cSKalle Valo 1906948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 1916948300cSKalle Valo zd_usb_iowrite16v_async_start(&chip->usb); 1926948300cSKalle Valo 1936948300cSKalle Valo for (i = 0; i < count; i += j + t) { 1946948300cSKalle Valo t = 0; 1956948300cSKalle Valo max = count-i; 1966948300cSKalle Valo if (max > USB_MAX_IOWRITE16_COUNT) 1976948300cSKalle Valo max = USB_MAX_IOWRITE16_COUNT; 1986948300cSKalle Valo for (j = 0; j < max; j++) { 1996948300cSKalle Valo if (!ioreqs[i+j].addr) { 2006948300cSKalle Valo t = 1; 2016948300cSKalle Valo break; 2026948300cSKalle Valo } 2036948300cSKalle Valo } 2046948300cSKalle Valo 2056948300cSKalle Valo r = zd_usb_iowrite16v_async(&chip->usb, &ioreqs[i], j); 2066948300cSKalle Valo if (r) { 2076948300cSKalle Valo zd_usb_iowrite16v_async_end(&chip->usb, 0); 2086948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 2096948300cSKalle Valo "error zd_usb_iowrite16v. Error number %d\n", 2106948300cSKalle Valo r); 2116948300cSKalle Valo return r; 2126948300cSKalle Valo } 2136948300cSKalle Valo } 2146948300cSKalle Valo 2156948300cSKalle Valo return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */); 2166948300cSKalle Valo } 2176948300cSKalle Valo 2186948300cSKalle Valo /* Writes a variable number of 32 bit registers. The functions will split 2196948300cSKalle Valo * that in several USB requests. A split can be forced by inserting an IO 2206948300cSKalle Valo * request with an zero address field. 2216948300cSKalle Valo */ 2226948300cSKalle Valo int zd_iowrite32a_locked(struct zd_chip *chip, 2236948300cSKalle Valo const struct zd_ioreq32 *ioreqs, unsigned int count) 2246948300cSKalle Valo { 2256948300cSKalle Valo int r; 2266948300cSKalle Valo unsigned int i, j, t, max; 2276948300cSKalle Valo 2286948300cSKalle Valo zd_usb_iowrite16v_async_start(&chip->usb); 2296948300cSKalle Valo 2306948300cSKalle Valo for (i = 0; i < count; i += j + t) { 2316948300cSKalle Valo t = 0; 2326948300cSKalle Valo max = count-i; 2336948300cSKalle Valo if (max > USB_MAX_IOWRITE32_COUNT) 2346948300cSKalle Valo max = USB_MAX_IOWRITE32_COUNT; 2356948300cSKalle Valo for (j = 0; j < max; j++) { 2366948300cSKalle Valo if (!ioreqs[i+j].addr) { 2376948300cSKalle Valo t = 1; 2386948300cSKalle Valo break; 2396948300cSKalle Valo } 2406948300cSKalle Valo } 2416948300cSKalle Valo 2426948300cSKalle Valo r = _zd_iowrite32v_async_locked(chip, &ioreqs[i], j); 2436948300cSKalle Valo if (r) { 2446948300cSKalle Valo zd_usb_iowrite16v_async_end(&chip->usb, 0); 2456948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 2466948300cSKalle Valo "error _%s. Error number %d\n", __func__, 2476948300cSKalle Valo r); 2486948300cSKalle Valo return r; 2496948300cSKalle Valo } 2506948300cSKalle Valo } 2516948300cSKalle Valo 2526948300cSKalle Valo return zd_usb_iowrite16v_async_end(&chip->usb, 50 /* ms */); 2536948300cSKalle Valo } 2546948300cSKalle Valo 2556948300cSKalle Valo int zd_ioread16(struct zd_chip *chip, zd_addr_t addr, u16 *value) 2566948300cSKalle Valo { 2576948300cSKalle Valo int r; 2586948300cSKalle Valo 2596948300cSKalle Valo mutex_lock(&chip->mutex); 2606948300cSKalle Valo r = zd_ioread16_locked(chip, value, addr); 2616948300cSKalle Valo mutex_unlock(&chip->mutex); 2626948300cSKalle Valo return r; 2636948300cSKalle Valo } 2646948300cSKalle Valo 2656948300cSKalle Valo int zd_ioread32(struct zd_chip *chip, zd_addr_t addr, u32 *value) 2666948300cSKalle Valo { 2676948300cSKalle Valo int r; 2686948300cSKalle Valo 2696948300cSKalle Valo mutex_lock(&chip->mutex); 2706948300cSKalle Valo r = zd_ioread32_locked(chip, value, addr); 2716948300cSKalle Valo mutex_unlock(&chip->mutex); 2726948300cSKalle Valo return r; 2736948300cSKalle Valo } 2746948300cSKalle Valo 2756948300cSKalle Valo int zd_iowrite16(struct zd_chip *chip, zd_addr_t addr, u16 value) 2766948300cSKalle Valo { 2776948300cSKalle Valo int r; 2786948300cSKalle Valo 2796948300cSKalle Valo mutex_lock(&chip->mutex); 2806948300cSKalle Valo r = zd_iowrite16_locked(chip, value, addr); 2816948300cSKalle Valo mutex_unlock(&chip->mutex); 2826948300cSKalle Valo return r; 2836948300cSKalle Valo } 2846948300cSKalle Valo 2856948300cSKalle Valo int zd_iowrite32(struct zd_chip *chip, zd_addr_t addr, u32 value) 2866948300cSKalle Valo { 2876948300cSKalle Valo int r; 2886948300cSKalle Valo 2896948300cSKalle Valo mutex_lock(&chip->mutex); 2906948300cSKalle Valo r = zd_iowrite32_locked(chip, value, addr); 2916948300cSKalle Valo mutex_unlock(&chip->mutex); 2926948300cSKalle Valo return r; 2936948300cSKalle Valo } 2946948300cSKalle Valo 2956948300cSKalle Valo int zd_ioread32v(struct zd_chip *chip, const zd_addr_t *addresses, 2966948300cSKalle Valo u32 *values, unsigned int count) 2976948300cSKalle Valo { 2986948300cSKalle Valo int r; 2996948300cSKalle Valo 3006948300cSKalle Valo mutex_lock(&chip->mutex); 3016948300cSKalle Valo r = zd_ioread32v_locked(chip, values, addresses, count); 3026948300cSKalle Valo mutex_unlock(&chip->mutex); 3036948300cSKalle Valo return r; 3046948300cSKalle Valo } 3056948300cSKalle Valo 3066948300cSKalle Valo int zd_iowrite32a(struct zd_chip *chip, const struct zd_ioreq32 *ioreqs, 3076948300cSKalle Valo unsigned int count) 3086948300cSKalle Valo { 3096948300cSKalle Valo int r; 3106948300cSKalle Valo 3116948300cSKalle Valo mutex_lock(&chip->mutex); 3126948300cSKalle Valo r = zd_iowrite32a_locked(chip, ioreqs, count); 3136948300cSKalle Valo mutex_unlock(&chip->mutex); 3146948300cSKalle Valo return r; 3156948300cSKalle Valo } 3166948300cSKalle Valo 3176948300cSKalle Valo static int read_pod(struct zd_chip *chip, u8 *rf_type) 3186948300cSKalle Valo { 3196948300cSKalle Valo int r; 3206948300cSKalle Valo u32 value; 3216948300cSKalle Valo 3226948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 3236948300cSKalle Valo r = zd_ioread32_locked(chip, &value, E2P_POD); 3246948300cSKalle Valo if (r) 3256948300cSKalle Valo goto error; 3266948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "E2P_POD %#010x\n", value); 3276948300cSKalle Valo 3286948300cSKalle Valo /* FIXME: AL2230 handling (Bit 7 in POD) */ 3296948300cSKalle Valo *rf_type = value & 0x0f; 3306948300cSKalle Valo chip->pa_type = (value >> 16) & 0x0f; 3316948300cSKalle Valo chip->patch_cck_gain = (value >> 8) & 0x1; 3326948300cSKalle Valo chip->patch_cr157 = (value >> 13) & 0x1; 3336948300cSKalle Valo chip->patch_6m_band_edge = (value >> 21) & 0x1; 3346948300cSKalle Valo chip->new_phy_layout = (value >> 31) & 0x1; 3356948300cSKalle Valo chip->al2230s_bit = (value >> 7) & 0x1; 3366948300cSKalle Valo chip->link_led = ((value >> 4) & 1) ? LED1 : LED2; 3376948300cSKalle Valo chip->supports_tx_led = 1; 3386948300cSKalle Valo if (value & (1 << 24)) { /* LED scenario */ 3396948300cSKalle Valo if (value & (1 << 29)) 3406948300cSKalle Valo chip->supports_tx_led = 0; 3416948300cSKalle Valo } 3426948300cSKalle Valo 3436948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 3446948300cSKalle Valo "RF %s %#01x PA type %#01x patch CCK %d patch CR157 %d " 3456948300cSKalle Valo "patch 6M %d new PHY %d link LED%d tx led %d\n", 3466948300cSKalle Valo zd_rf_name(*rf_type), *rf_type, 3476948300cSKalle Valo chip->pa_type, chip->patch_cck_gain, 3486948300cSKalle Valo chip->patch_cr157, chip->patch_6m_band_edge, 3496948300cSKalle Valo chip->new_phy_layout, 3506948300cSKalle Valo chip->link_led == LED1 ? 1 : 2, 3516948300cSKalle Valo chip->supports_tx_led); 3526948300cSKalle Valo return 0; 3536948300cSKalle Valo error: 3546948300cSKalle Valo *rf_type = 0; 3556948300cSKalle Valo chip->pa_type = 0; 3566948300cSKalle Valo chip->patch_cck_gain = 0; 3576948300cSKalle Valo chip->patch_cr157 = 0; 3586948300cSKalle Valo chip->patch_6m_band_edge = 0; 3596948300cSKalle Valo chip->new_phy_layout = 0; 3606948300cSKalle Valo return r; 3616948300cSKalle Valo } 3626948300cSKalle Valo 3636948300cSKalle Valo static int zd_write_mac_addr_common(struct zd_chip *chip, const u8 *mac_addr, 3646948300cSKalle Valo const struct zd_ioreq32 *in_reqs, 3656948300cSKalle Valo const char *type) 3666948300cSKalle Valo { 3676948300cSKalle Valo int r; 3686948300cSKalle Valo struct zd_ioreq32 reqs[2] = {in_reqs[0], in_reqs[1]}; 3696948300cSKalle Valo 3706948300cSKalle Valo if (mac_addr) { 3716948300cSKalle Valo reqs[0].value = (mac_addr[3] << 24) 3726948300cSKalle Valo | (mac_addr[2] << 16) 3736948300cSKalle Valo | (mac_addr[1] << 8) 3746948300cSKalle Valo | mac_addr[0]; 3756948300cSKalle Valo reqs[1].value = (mac_addr[5] << 8) 3766948300cSKalle Valo | mac_addr[4]; 3776948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "%s addr %pM\n", type, mac_addr); 3786948300cSKalle Valo } else { 3796948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "set NULL %s\n", type); 3806948300cSKalle Valo } 3816948300cSKalle Valo 3826948300cSKalle Valo mutex_lock(&chip->mutex); 3836948300cSKalle Valo r = zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs)); 3846948300cSKalle Valo mutex_unlock(&chip->mutex); 3856948300cSKalle Valo return r; 3866948300cSKalle Valo } 3876948300cSKalle Valo 3886948300cSKalle Valo /* MAC address: if custom mac addresses are to be used CR_MAC_ADDR_P1 and 3896948300cSKalle Valo * CR_MAC_ADDR_P2 must be overwritten 3906948300cSKalle Valo */ 3916948300cSKalle Valo int zd_write_mac_addr(struct zd_chip *chip, const u8 *mac_addr) 3926948300cSKalle Valo { 3936948300cSKalle Valo static const struct zd_ioreq32 reqs[2] = { 3946948300cSKalle Valo [0] = { .addr = CR_MAC_ADDR_P1 }, 3956948300cSKalle Valo [1] = { .addr = CR_MAC_ADDR_P2 }, 3966948300cSKalle Valo }; 3976948300cSKalle Valo 3986948300cSKalle Valo return zd_write_mac_addr_common(chip, mac_addr, reqs, "mac"); 3996948300cSKalle Valo } 4006948300cSKalle Valo 4016948300cSKalle Valo int zd_write_bssid(struct zd_chip *chip, const u8 *bssid) 4026948300cSKalle Valo { 4036948300cSKalle Valo static const struct zd_ioreq32 reqs[2] = { 4046948300cSKalle Valo [0] = { .addr = CR_BSSID_P1 }, 4056948300cSKalle Valo [1] = { .addr = CR_BSSID_P2 }, 4066948300cSKalle Valo }; 4076948300cSKalle Valo 4086948300cSKalle Valo return zd_write_mac_addr_common(chip, bssid, reqs, "bssid"); 4096948300cSKalle Valo } 4106948300cSKalle Valo 4116948300cSKalle Valo int zd_read_regdomain(struct zd_chip *chip, u8 *regdomain) 4126948300cSKalle Valo { 4136948300cSKalle Valo int r; 4146948300cSKalle Valo u32 value; 4156948300cSKalle Valo 4166948300cSKalle Valo mutex_lock(&chip->mutex); 4176948300cSKalle Valo r = zd_ioread32_locked(chip, &value, E2P_SUBID); 4186948300cSKalle Valo mutex_unlock(&chip->mutex); 4196948300cSKalle Valo if (r) 4206948300cSKalle Valo return r; 4216948300cSKalle Valo 4226948300cSKalle Valo *regdomain = value >> 16; 4236948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "regdomain: %#04x\n", *regdomain); 4246948300cSKalle Valo 4256948300cSKalle Valo return 0; 4266948300cSKalle Valo } 4276948300cSKalle Valo 4286948300cSKalle Valo static int read_values(struct zd_chip *chip, u8 *values, size_t count, 4296948300cSKalle Valo zd_addr_t e2p_addr, u32 guard) 4306948300cSKalle Valo { 4316948300cSKalle Valo int r; 4326948300cSKalle Valo int i; 4336948300cSKalle Valo u32 v; 4346948300cSKalle Valo 4356948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 4366948300cSKalle Valo for (i = 0;;) { 4376948300cSKalle Valo r = zd_ioread32_locked(chip, &v, 4386948300cSKalle Valo (zd_addr_t)((u16)e2p_addr+i/2)); 4396948300cSKalle Valo if (r) 4406948300cSKalle Valo return r; 4416948300cSKalle Valo v -= guard; 4426948300cSKalle Valo if (i+4 < count) { 4436948300cSKalle Valo values[i++] = v; 4446948300cSKalle Valo values[i++] = v >> 8; 4456948300cSKalle Valo values[i++] = v >> 16; 4466948300cSKalle Valo values[i++] = v >> 24; 4476948300cSKalle Valo continue; 4486948300cSKalle Valo } 4496948300cSKalle Valo for (;i < count; i++) 4506948300cSKalle Valo values[i] = v >> (8*(i%3)); 4516948300cSKalle Valo return 0; 4526948300cSKalle Valo } 4536948300cSKalle Valo } 4546948300cSKalle Valo 4556948300cSKalle Valo static int read_pwr_cal_values(struct zd_chip *chip) 4566948300cSKalle Valo { 4576948300cSKalle Valo return read_values(chip, chip->pwr_cal_values, 4586948300cSKalle Valo E2P_CHANNEL_COUNT, E2P_PWR_CAL_VALUE1, 4596948300cSKalle Valo 0); 4606948300cSKalle Valo } 4616948300cSKalle Valo 4626948300cSKalle Valo static int read_pwr_int_values(struct zd_chip *chip) 4636948300cSKalle Valo { 4646948300cSKalle Valo return read_values(chip, chip->pwr_int_values, 4656948300cSKalle Valo E2P_CHANNEL_COUNT, E2P_PWR_INT_VALUE1, 4666948300cSKalle Valo E2P_PWR_INT_GUARD); 4676948300cSKalle Valo } 4686948300cSKalle Valo 4696948300cSKalle Valo static int read_ofdm_cal_values(struct zd_chip *chip) 4706948300cSKalle Valo { 4716948300cSKalle Valo int r; 4726948300cSKalle Valo int i; 4736948300cSKalle Valo static const zd_addr_t addresses[] = { 4746948300cSKalle Valo E2P_36M_CAL_VALUE1, 4756948300cSKalle Valo E2P_48M_CAL_VALUE1, 4766948300cSKalle Valo E2P_54M_CAL_VALUE1, 4776948300cSKalle Valo }; 4786948300cSKalle Valo 4796948300cSKalle Valo for (i = 0; i < 3; i++) { 4806948300cSKalle Valo r = read_values(chip, chip->ofdm_cal_values[i], 4816948300cSKalle Valo E2P_CHANNEL_COUNT, addresses[i], 0); 4826948300cSKalle Valo if (r) 4836948300cSKalle Valo return r; 4846948300cSKalle Valo } 4856948300cSKalle Valo return 0; 4866948300cSKalle Valo } 4876948300cSKalle Valo 4886948300cSKalle Valo static int read_cal_int_tables(struct zd_chip *chip) 4896948300cSKalle Valo { 4906948300cSKalle Valo int r; 4916948300cSKalle Valo 4926948300cSKalle Valo r = read_pwr_cal_values(chip); 4936948300cSKalle Valo if (r) 4946948300cSKalle Valo return r; 4956948300cSKalle Valo r = read_pwr_int_values(chip); 4966948300cSKalle Valo if (r) 4976948300cSKalle Valo return r; 4986948300cSKalle Valo r = read_ofdm_cal_values(chip); 4996948300cSKalle Valo if (r) 5006948300cSKalle Valo return r; 5016948300cSKalle Valo return 0; 5026948300cSKalle Valo } 5036948300cSKalle Valo 5046948300cSKalle Valo /* phy means physical registers */ 5056948300cSKalle Valo int zd_chip_lock_phy_regs(struct zd_chip *chip) 5066948300cSKalle Valo { 5076948300cSKalle Valo int r; 5086948300cSKalle Valo u32 tmp; 5096948300cSKalle Valo 5106948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 5116948300cSKalle Valo r = zd_ioread32_locked(chip, &tmp, CR_REG1); 5126948300cSKalle Valo if (r) { 5136948300cSKalle Valo dev_err(zd_chip_dev(chip), "error ioread32(CR_REG1): %d\n", r); 5146948300cSKalle Valo return r; 5156948300cSKalle Valo } 5166948300cSKalle Valo 5176948300cSKalle Valo tmp &= ~UNLOCK_PHY_REGS; 5186948300cSKalle Valo 5196948300cSKalle Valo r = zd_iowrite32_locked(chip, tmp, CR_REG1); 5206948300cSKalle Valo if (r) 5216948300cSKalle Valo dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r); 5226948300cSKalle Valo return r; 5236948300cSKalle Valo } 5246948300cSKalle Valo 5256948300cSKalle Valo int zd_chip_unlock_phy_regs(struct zd_chip *chip) 5266948300cSKalle Valo { 5276948300cSKalle Valo int r; 5286948300cSKalle Valo u32 tmp; 5296948300cSKalle Valo 5306948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 5316948300cSKalle Valo r = zd_ioread32_locked(chip, &tmp, CR_REG1); 5326948300cSKalle Valo if (r) { 5336948300cSKalle Valo dev_err(zd_chip_dev(chip), 5346948300cSKalle Valo "error ioread32(CR_REG1): %d\n", r); 5356948300cSKalle Valo return r; 5366948300cSKalle Valo } 5376948300cSKalle Valo 5386948300cSKalle Valo tmp |= UNLOCK_PHY_REGS; 5396948300cSKalle Valo 5406948300cSKalle Valo r = zd_iowrite32_locked(chip, tmp, CR_REG1); 5416948300cSKalle Valo if (r) 5426948300cSKalle Valo dev_err(zd_chip_dev(chip), "error iowrite32(CR_REG1): %d\n", r); 5436948300cSKalle Valo return r; 5446948300cSKalle Valo } 5456948300cSKalle Valo 5466948300cSKalle Valo /* ZD_CR157 can be optionally patched by the EEPROM for original ZD1211 */ 5476948300cSKalle Valo static int patch_cr157(struct zd_chip *chip) 5486948300cSKalle Valo { 5496948300cSKalle Valo int r; 5506948300cSKalle Valo u16 value; 5516948300cSKalle Valo 5526948300cSKalle Valo if (!chip->patch_cr157) 5536948300cSKalle Valo return 0; 5546948300cSKalle Valo 5556948300cSKalle Valo r = zd_ioread16_locked(chip, &value, E2P_PHY_REG); 5566948300cSKalle Valo if (r) 5576948300cSKalle Valo return r; 5586948300cSKalle Valo 5596948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value >> 8); 5606948300cSKalle Valo return zd_iowrite32_locked(chip, value >> 8, ZD_CR157); 5616948300cSKalle Valo } 5626948300cSKalle Valo 5636948300cSKalle Valo /* 5646948300cSKalle Valo * 6M band edge can be optionally overwritten for certain RF's 5656948300cSKalle Valo * Vendor driver says: for FCC regulation, enabled per HWFeature 6M band edge 5666948300cSKalle Valo * bit (for AL2230, AL2230S) 5676948300cSKalle Valo */ 5686948300cSKalle Valo static int patch_6m_band_edge(struct zd_chip *chip, u8 channel) 5696948300cSKalle Valo { 5706948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 5716948300cSKalle Valo if (!chip->patch_6m_band_edge) 5726948300cSKalle Valo return 0; 5736948300cSKalle Valo 5746948300cSKalle Valo return zd_rf_patch_6m_band_edge(&chip->rf, channel); 5756948300cSKalle Valo } 5766948300cSKalle Valo 5776948300cSKalle Valo /* Generic implementation of 6M band edge patching, used by most RFs via 5786948300cSKalle Valo * zd_rf_generic_patch_6m() */ 5796948300cSKalle Valo int zd_chip_generic_patch_6m_band(struct zd_chip *chip, int channel) 5806948300cSKalle Valo { 5816948300cSKalle Valo struct zd_ioreq16 ioreqs[] = { 5826948300cSKalle Valo { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 }, 5836948300cSKalle Valo { ZD_CR47, 0x1e }, 5846948300cSKalle Valo }; 5856948300cSKalle Valo 5866948300cSKalle Valo /* FIXME: Channel 11 is not the edge for all regulatory domains. */ 5876948300cSKalle Valo if (channel == 1 || channel == 11) 5886948300cSKalle Valo ioreqs[0].value = 0x12; 5896948300cSKalle Valo 5906948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "patching for channel %d\n", channel); 5916948300cSKalle Valo return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 5926948300cSKalle Valo } 5936948300cSKalle Valo 5946948300cSKalle Valo static int zd1211_hw_reset_phy(struct zd_chip *chip) 5956948300cSKalle Valo { 5966948300cSKalle Valo static const struct zd_ioreq16 ioreqs[] = { 5976948300cSKalle Valo { ZD_CR0, 0x0a }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 }, 5986948300cSKalle Valo { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xa0 }, 5996948300cSKalle Valo { ZD_CR10, 0x81 }, { ZD_CR11, 0x00 }, { ZD_CR12, 0x7f }, 6006948300cSKalle Valo { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 }, { ZD_CR15, 0x3d }, 6016948300cSKalle Valo { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e }, { ZD_CR18, 0x0a }, 6026948300cSKalle Valo { ZD_CR19, 0x48 }, { ZD_CR20, 0x0c }, { ZD_CR21, 0x0c }, 6036948300cSKalle Valo { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 }, { ZD_CR24, 0x14 }, 6046948300cSKalle Valo { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 }, { ZD_CR27, 0x19 }, 6056948300cSKalle Valo { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 }, { ZD_CR30, 0x4b }, 6066948300cSKalle Valo { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 }, 6076948300cSKalle Valo { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 }, 6086948300cSKalle Valo { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c }, 6096948300cSKalle Valo { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 }, 6106948300cSKalle Valo { ZD_CR43, 0x10 }, { ZD_CR44, 0x12 }, { ZD_CR46, 0xff }, 6116948300cSKalle Valo { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b }, 6126948300cSKalle Valo { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 }, 6136948300cSKalle Valo { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 }, 6146948300cSKalle Valo { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff }, 6156948300cSKalle Valo { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 }, 6166948300cSKalle Valo { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 }, 6176948300cSKalle Valo { ZD_CR79, 0x68 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 }, 6186948300cSKalle Valo { ZD_CR82, 0x00 }, { ZD_CR83, 0x00 }, { ZD_CR84, 0x00 }, 6196948300cSKalle Valo { ZD_CR85, 0x02 }, { ZD_CR86, 0x00 }, { ZD_CR87, 0x00 }, 6206948300cSKalle Valo { ZD_CR88, 0xff }, { ZD_CR89, 0xfc }, { ZD_CR90, 0x00 }, 6216948300cSKalle Valo { ZD_CR91, 0x00 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x08 }, 6226948300cSKalle Valo { ZD_CR94, 0x00 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0xff }, 6236948300cSKalle Valo { ZD_CR97, 0xe7 }, { ZD_CR98, 0x00 }, { ZD_CR99, 0x00 }, 6246948300cSKalle Valo { ZD_CR100, 0x00 }, { ZD_CR101, 0xae }, { ZD_CR102, 0x02 }, 6256948300cSKalle Valo { ZD_CR103, 0x00 }, { ZD_CR104, 0x03 }, { ZD_CR105, 0x65 }, 6266948300cSKalle Valo { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 }, { ZD_CR108, 0x0a }, 6276948300cSKalle Valo { ZD_CR109, 0xaa }, { ZD_CR110, 0xaa }, { ZD_CR111, 0x25 }, 6286948300cSKalle Valo { ZD_CR112, 0x25 }, { ZD_CR113, 0x00 }, { ZD_CR119, 0x1e }, 6296948300cSKalle Valo { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 }, 6306948300cSKalle Valo { }, 6316948300cSKalle Valo { ZD_CR5, 0x00 }, { ZD_CR6, 0x00 }, { ZD_CR7, 0x00 }, 6326948300cSKalle Valo { ZD_CR8, 0x00 }, { ZD_CR9, 0x20 }, { ZD_CR12, 0xf0 }, 6336948300cSKalle Valo { ZD_CR20, 0x0e }, { ZD_CR21, 0x0e }, { ZD_CR27, 0x10 }, 6346948300cSKalle Valo { ZD_CR44, 0x33 }, { ZD_CR47, 0x1E }, { ZD_CR83, 0x24 }, 6356948300cSKalle Valo { ZD_CR84, 0x04 }, { ZD_CR85, 0x00 }, { ZD_CR86, 0x0C }, 6366948300cSKalle Valo { ZD_CR87, 0x12 }, { ZD_CR88, 0x0C }, { ZD_CR89, 0x00 }, 6376948300cSKalle Valo { ZD_CR90, 0x10 }, { ZD_CR91, 0x08 }, { ZD_CR93, 0x00 }, 6386948300cSKalle Valo { ZD_CR94, 0x01 }, { ZD_CR95, 0x00 }, { ZD_CR96, 0x50 }, 6396948300cSKalle Valo { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 }, { ZD_CR101, 0x13 }, 6406948300cSKalle Valo { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 }, 6416948300cSKalle Valo { ZD_CR105, 0x12 }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 }, 6426948300cSKalle Valo { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 }, 6436948300cSKalle Valo { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 }, 6446948300cSKalle Valo { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR120, 0x4f }, 6456948300cSKalle Valo { ZD_CR125, 0xaa }, { ZD_CR127, 0x03 }, { ZD_CR128, 0x14 }, 6466948300cSKalle Valo { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 }, { ZD_CR131, 0x0C }, 6476948300cSKalle Valo { ZD_CR136, 0xdf }, { ZD_CR137, 0x40 }, { ZD_CR138, 0xa0 }, 6486948300cSKalle Valo { ZD_CR139, 0xb0 }, { ZD_CR140, 0x99 }, { ZD_CR141, 0x82 }, 6496948300cSKalle Valo { ZD_CR142, 0x54 }, { ZD_CR143, 0x1c }, { ZD_CR144, 0x6c }, 6506948300cSKalle Valo { ZD_CR147, 0x07 }, { ZD_CR148, 0x4c }, { ZD_CR149, 0x50 }, 6516948300cSKalle Valo { ZD_CR150, 0x0e }, { ZD_CR151, 0x18 }, { ZD_CR160, 0xfe }, 6526948300cSKalle Valo { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa }, 6536948300cSKalle Valo { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe }, 6546948300cSKalle Valo { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba }, 6556948300cSKalle Valo { ZD_CR170, 0xba }, { ZD_CR171, 0xba }, 6566948300cSKalle Valo /* Note: ZD_CR204 must lead the ZD_CR203 */ 6576948300cSKalle Valo { ZD_CR204, 0x7d }, 6586948300cSKalle Valo { }, 6596948300cSKalle Valo { ZD_CR203, 0x30 }, 6606948300cSKalle Valo }; 6616948300cSKalle Valo 6626948300cSKalle Valo int r, t; 6636948300cSKalle Valo 6646948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 6656948300cSKalle Valo 6666948300cSKalle Valo r = zd_chip_lock_phy_regs(chip); 6676948300cSKalle Valo if (r) 6686948300cSKalle Valo goto out; 6696948300cSKalle Valo 6706948300cSKalle Valo r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 6716948300cSKalle Valo if (r) 6726948300cSKalle Valo goto unlock; 6736948300cSKalle Valo 6746948300cSKalle Valo r = patch_cr157(chip); 6756948300cSKalle Valo unlock: 6766948300cSKalle Valo t = zd_chip_unlock_phy_regs(chip); 6776948300cSKalle Valo if (t && !r) 6786948300cSKalle Valo r = t; 6796948300cSKalle Valo out: 6806948300cSKalle Valo return r; 6816948300cSKalle Valo } 6826948300cSKalle Valo 6836948300cSKalle Valo static int zd1211b_hw_reset_phy(struct zd_chip *chip) 6846948300cSKalle Valo { 6856948300cSKalle Valo static const struct zd_ioreq16 ioreqs[] = { 6866948300cSKalle Valo { ZD_CR0, 0x14 }, { ZD_CR1, 0x06 }, { ZD_CR2, 0x26 }, 6876948300cSKalle Valo { ZD_CR3, 0x38 }, { ZD_CR4, 0x80 }, { ZD_CR9, 0xe0 }, 6886948300cSKalle Valo { ZD_CR10, 0x81 }, 6896948300cSKalle Valo /* power control { { ZD_CR11, 1 << 6 }, */ 6906948300cSKalle Valo { ZD_CR11, 0x00 }, 6916948300cSKalle Valo { ZD_CR12, 0xf0 }, { ZD_CR13, 0x8c }, { ZD_CR14, 0x80 }, 6926948300cSKalle Valo { ZD_CR15, 0x3d }, { ZD_CR16, 0x20 }, { ZD_CR17, 0x1e }, 6936948300cSKalle Valo { ZD_CR18, 0x0a }, { ZD_CR19, 0x48 }, 6946948300cSKalle Valo { ZD_CR20, 0x10 }, /* Org:0x0E, ComTrend:RalLink AP */ 6956948300cSKalle Valo { ZD_CR21, 0x0e }, { ZD_CR22, 0x23 }, { ZD_CR23, 0x90 }, 6966948300cSKalle Valo { ZD_CR24, 0x14 }, { ZD_CR25, 0x40 }, { ZD_CR26, 0x10 }, 6976948300cSKalle Valo { ZD_CR27, 0x10 }, { ZD_CR28, 0x7f }, { ZD_CR29, 0x80 }, 6986948300cSKalle Valo { ZD_CR30, 0x4b }, /* ASIC/FWT, no jointly decoder */ 6996948300cSKalle Valo { ZD_CR31, 0x60 }, { ZD_CR32, 0x43 }, { ZD_CR33, 0x08 }, 7006948300cSKalle Valo { ZD_CR34, 0x06 }, { ZD_CR35, 0x0a }, { ZD_CR36, 0x00 }, 7016948300cSKalle Valo { ZD_CR37, 0x00 }, { ZD_CR38, 0x38 }, { ZD_CR39, 0x0c }, 7026948300cSKalle Valo { ZD_CR40, 0x84 }, { ZD_CR41, 0x2a }, { ZD_CR42, 0x80 }, 7036948300cSKalle Valo { ZD_CR43, 0x10 }, { ZD_CR44, 0x33 }, { ZD_CR46, 0xff }, 7046948300cSKalle Valo { ZD_CR47, 0x1E }, { ZD_CR48, 0x26 }, { ZD_CR49, 0x5b }, 7056948300cSKalle Valo { ZD_CR64, 0xd0 }, { ZD_CR65, 0x04 }, { ZD_CR66, 0x58 }, 7066948300cSKalle Valo { ZD_CR67, 0xc9 }, { ZD_CR68, 0x88 }, { ZD_CR69, 0x41 }, 7076948300cSKalle Valo { ZD_CR70, 0x23 }, { ZD_CR71, 0x10 }, { ZD_CR72, 0xff }, 7086948300cSKalle Valo { ZD_CR73, 0x32 }, { ZD_CR74, 0x30 }, { ZD_CR75, 0x65 }, 7096948300cSKalle Valo { ZD_CR76, 0x41 }, { ZD_CR77, 0x1b }, { ZD_CR78, 0x30 }, 7106948300cSKalle Valo { ZD_CR79, 0xf0 }, { ZD_CR80, 0x64 }, { ZD_CR81, 0x64 }, 7116948300cSKalle Valo { ZD_CR82, 0x00 }, { ZD_CR83, 0x24 }, { ZD_CR84, 0x04 }, 7126948300cSKalle Valo { ZD_CR85, 0x00 }, { ZD_CR86, 0x0c }, { ZD_CR87, 0x12 }, 7136948300cSKalle Valo { ZD_CR88, 0x0c }, { ZD_CR89, 0x00 }, { ZD_CR90, 0x58 }, 7146948300cSKalle Valo { ZD_CR91, 0x04 }, { ZD_CR92, 0x00 }, { ZD_CR93, 0x00 }, 7156948300cSKalle Valo { ZD_CR94, 0x01 }, 7166948300cSKalle Valo { ZD_CR95, 0x20 }, /* ZD1211B */ 7176948300cSKalle Valo { ZD_CR96, 0x50 }, { ZD_CR97, 0x37 }, { ZD_CR98, 0x35 }, 7186948300cSKalle Valo { ZD_CR99, 0x00 }, { ZD_CR100, 0x01 }, { ZD_CR101, 0x13 }, 7196948300cSKalle Valo { ZD_CR102, 0x27 }, { ZD_CR103, 0x27 }, { ZD_CR104, 0x18 }, 7206948300cSKalle Valo { ZD_CR105, 0x12 }, { ZD_CR106, 0x04 }, { ZD_CR107, 0x00 }, 7216948300cSKalle Valo { ZD_CR108, 0x0a }, { ZD_CR109, 0x27 }, { ZD_CR110, 0x27 }, 7226948300cSKalle Valo { ZD_CR111, 0x27 }, { ZD_CR112, 0x27 }, { ZD_CR113, 0x27 }, 7236948300cSKalle Valo { ZD_CR114, 0x27 }, { ZD_CR115, 0x26 }, { ZD_CR116, 0x24 }, 7246948300cSKalle Valo { ZD_CR117, 0xfc }, { ZD_CR118, 0xfa }, { ZD_CR119, 0x1e }, 7256948300cSKalle Valo { ZD_CR125, 0x90 }, { ZD_CR126, 0x00 }, { ZD_CR127, 0x00 }, 7266948300cSKalle Valo { ZD_CR128, 0x14 }, { ZD_CR129, 0x12 }, { ZD_CR130, 0x10 }, 7276948300cSKalle Valo { ZD_CR131, 0x0c }, { ZD_CR136, 0xdf }, { ZD_CR137, 0xa0 }, 7286948300cSKalle Valo { ZD_CR138, 0xa8 }, { ZD_CR139, 0xb4 }, { ZD_CR140, 0x98 }, 7296948300cSKalle Valo { ZD_CR141, 0x82 }, { ZD_CR142, 0x53 }, { ZD_CR143, 0x1c }, 7306948300cSKalle Valo { ZD_CR144, 0x6c }, { ZD_CR147, 0x07 }, { ZD_CR148, 0x40 }, 7316948300cSKalle Valo { ZD_CR149, 0x40 }, /* Org:0x50 ComTrend:RalLink AP */ 7326948300cSKalle Valo { ZD_CR150, 0x14 }, /* Org:0x0E ComTrend:RalLink AP */ 7336948300cSKalle Valo { ZD_CR151, 0x18 }, { ZD_CR159, 0x70 }, { ZD_CR160, 0xfe }, 7346948300cSKalle Valo { ZD_CR161, 0xee }, { ZD_CR162, 0xaa }, { ZD_CR163, 0xfa }, 7356948300cSKalle Valo { ZD_CR164, 0xfa }, { ZD_CR165, 0xea }, { ZD_CR166, 0xbe }, 7366948300cSKalle Valo { ZD_CR167, 0xbe }, { ZD_CR168, 0x6a }, { ZD_CR169, 0xba }, 7376948300cSKalle Valo { ZD_CR170, 0xba }, { ZD_CR171, 0xba }, 7386948300cSKalle Valo /* Note: ZD_CR204 must lead the ZD_CR203 */ 7396948300cSKalle Valo { ZD_CR204, 0x7d }, 7406948300cSKalle Valo {}, 7416948300cSKalle Valo { ZD_CR203, 0x30 }, 7426948300cSKalle Valo }; 7436948300cSKalle Valo 7446948300cSKalle Valo int r, t; 7456948300cSKalle Valo 7466948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 7476948300cSKalle Valo 7486948300cSKalle Valo r = zd_chip_lock_phy_regs(chip); 7496948300cSKalle Valo if (r) 7506948300cSKalle Valo goto out; 7516948300cSKalle Valo 7526948300cSKalle Valo r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 7536948300cSKalle Valo t = zd_chip_unlock_phy_regs(chip); 7546948300cSKalle Valo if (t && !r) 7556948300cSKalle Valo r = t; 7566948300cSKalle Valo out: 7576948300cSKalle Valo return r; 7586948300cSKalle Valo } 7596948300cSKalle Valo 7606948300cSKalle Valo static int hw_reset_phy(struct zd_chip *chip) 7616948300cSKalle Valo { 7626948300cSKalle Valo return zd_chip_is_zd1211b(chip) ? zd1211b_hw_reset_phy(chip) : 7636948300cSKalle Valo zd1211_hw_reset_phy(chip); 7646948300cSKalle Valo } 7656948300cSKalle Valo 7666948300cSKalle Valo static int zd1211_hw_init_hmac(struct zd_chip *chip) 7676948300cSKalle Valo { 7686948300cSKalle Valo static const struct zd_ioreq32 ioreqs[] = { 7696948300cSKalle Valo { CR_ZD1211_RETRY_MAX, ZD1211_RETRY_COUNT }, 7706948300cSKalle Valo { CR_RX_THRESHOLD, 0x000c0640 }, 7716948300cSKalle Valo }; 7726948300cSKalle Valo 7736948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 7746948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 7756948300cSKalle Valo return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 7766948300cSKalle Valo } 7776948300cSKalle Valo 7786948300cSKalle Valo static int zd1211b_hw_init_hmac(struct zd_chip *chip) 7796948300cSKalle Valo { 7806948300cSKalle Valo static const struct zd_ioreq32 ioreqs[] = { 7816948300cSKalle Valo { CR_ZD1211B_RETRY_MAX, ZD1211B_RETRY_COUNT }, 7826948300cSKalle Valo { CR_ZD1211B_CWIN_MAX_MIN_AC0, 0x007f003f }, 7836948300cSKalle Valo { CR_ZD1211B_CWIN_MAX_MIN_AC1, 0x007f003f }, 7846948300cSKalle Valo { CR_ZD1211B_CWIN_MAX_MIN_AC2, 0x003f001f }, 7856948300cSKalle Valo { CR_ZD1211B_CWIN_MAX_MIN_AC3, 0x001f000f }, 7866948300cSKalle Valo { CR_ZD1211B_AIFS_CTL1, 0x00280028 }, 7876948300cSKalle Valo { CR_ZD1211B_AIFS_CTL2, 0x008C003C }, 7886948300cSKalle Valo { CR_ZD1211B_TXOP, 0x01800824 }, 7896948300cSKalle Valo { CR_RX_THRESHOLD, 0x000c0eff, }, 7906948300cSKalle Valo }; 7916948300cSKalle Valo 7926948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 7936948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 7946948300cSKalle Valo return zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 7956948300cSKalle Valo } 7966948300cSKalle Valo 7976948300cSKalle Valo static int hw_init_hmac(struct zd_chip *chip) 7986948300cSKalle Valo { 7996948300cSKalle Valo int r; 8006948300cSKalle Valo static const struct zd_ioreq32 ioreqs[] = { 8016948300cSKalle Valo { CR_ACK_TIMEOUT_EXT, 0x20 }, 8026948300cSKalle Valo { CR_ADDA_MBIAS_WARMTIME, 0x30000808 }, 8036948300cSKalle Valo { CR_SNIFFER_ON, 0 }, 8046948300cSKalle Valo { CR_RX_FILTER, STA_RX_FILTER }, 8056948300cSKalle Valo { CR_GROUP_HASH_P1, 0x00 }, 8066948300cSKalle Valo { CR_GROUP_HASH_P2, 0x80000000 }, 8076948300cSKalle Valo { CR_REG1, 0xa4 }, 8086948300cSKalle Valo { CR_ADDA_PWR_DWN, 0x7f }, 8096948300cSKalle Valo { CR_BCN_PLCP_CFG, 0x00f00401 }, 8106948300cSKalle Valo { CR_PHY_DELAY, 0x00 }, 8116948300cSKalle Valo { CR_ACK_TIMEOUT_EXT, 0x80 }, 8126948300cSKalle Valo { CR_ADDA_PWR_DWN, 0x00 }, 8136948300cSKalle Valo { CR_ACK_TIME_80211, 0x100 }, 8146948300cSKalle Valo { CR_RX_PE_DELAY, 0x70 }, 8156948300cSKalle Valo { CR_PS_CTRL, 0x10000000 }, 8166948300cSKalle Valo { CR_RTS_CTS_RATE, 0x02030203 }, 8176948300cSKalle Valo { CR_AFTER_PNP, 0x1 }, 8186948300cSKalle Valo { CR_WEP_PROTECT, 0x114 }, 8196948300cSKalle Valo { CR_IFS_VALUE, IFS_VALUE_DEFAULT }, 8206948300cSKalle Valo { CR_CAM_MODE, MODE_AP_WDS}, 8216948300cSKalle Valo }; 8226948300cSKalle Valo 8236948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 8246948300cSKalle Valo r = zd_iowrite32a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 8256948300cSKalle Valo if (r) 8266948300cSKalle Valo return r; 8276948300cSKalle Valo 8286948300cSKalle Valo return zd_chip_is_zd1211b(chip) ? 8296948300cSKalle Valo zd1211b_hw_init_hmac(chip) : zd1211_hw_init_hmac(chip); 8306948300cSKalle Valo } 8316948300cSKalle Valo 8326948300cSKalle Valo struct aw_pt_bi { 8336948300cSKalle Valo u32 atim_wnd_period; 8346948300cSKalle Valo u32 pre_tbtt; 8356948300cSKalle Valo u32 beacon_interval; 8366948300cSKalle Valo }; 8376948300cSKalle Valo 8386948300cSKalle Valo static int get_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s) 8396948300cSKalle Valo { 8406948300cSKalle Valo int r; 8416948300cSKalle Valo static const zd_addr_t aw_pt_bi_addr[] = 8426948300cSKalle Valo { CR_ATIM_WND_PERIOD, CR_PRE_TBTT, CR_BCN_INTERVAL }; 8436948300cSKalle Valo u32 values[3]; 8446948300cSKalle Valo 8456948300cSKalle Valo r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr, 8466948300cSKalle Valo ARRAY_SIZE(aw_pt_bi_addr)); 8476948300cSKalle Valo if (r) { 8486948300cSKalle Valo memset(s, 0, sizeof(*s)); 8496948300cSKalle Valo return r; 8506948300cSKalle Valo } 8516948300cSKalle Valo 8526948300cSKalle Valo s->atim_wnd_period = values[0]; 8536948300cSKalle Valo s->pre_tbtt = values[1]; 8546948300cSKalle Valo s->beacon_interval = values[2]; 8556948300cSKalle Valo return 0; 8566948300cSKalle Valo } 8576948300cSKalle Valo 8586948300cSKalle Valo static int set_aw_pt_bi(struct zd_chip *chip, struct aw_pt_bi *s) 8596948300cSKalle Valo { 8606948300cSKalle Valo struct zd_ioreq32 reqs[3]; 8616948300cSKalle Valo u16 b_interval = s->beacon_interval & 0xffff; 8626948300cSKalle Valo 8636948300cSKalle Valo if (b_interval <= 5) 8646948300cSKalle Valo b_interval = 5; 8656948300cSKalle Valo if (s->pre_tbtt < 4 || s->pre_tbtt >= b_interval) 8666948300cSKalle Valo s->pre_tbtt = b_interval - 1; 8676948300cSKalle Valo if (s->atim_wnd_period >= s->pre_tbtt) 8686948300cSKalle Valo s->atim_wnd_period = s->pre_tbtt - 1; 8696948300cSKalle Valo 8706948300cSKalle Valo reqs[0].addr = CR_ATIM_WND_PERIOD; 8716948300cSKalle Valo reqs[0].value = s->atim_wnd_period; 8726948300cSKalle Valo reqs[1].addr = CR_PRE_TBTT; 8736948300cSKalle Valo reqs[1].value = s->pre_tbtt; 8746948300cSKalle Valo reqs[2].addr = CR_BCN_INTERVAL; 8756948300cSKalle Valo reqs[2].value = (s->beacon_interval & ~0xffff) | b_interval; 8766948300cSKalle Valo 8776948300cSKalle Valo return zd_iowrite32a_locked(chip, reqs, ARRAY_SIZE(reqs)); 8786948300cSKalle Valo } 8796948300cSKalle Valo 8806948300cSKalle Valo 8816948300cSKalle Valo static int set_beacon_interval(struct zd_chip *chip, u16 interval, 8826948300cSKalle Valo u8 dtim_period, int type) 8836948300cSKalle Valo { 8846948300cSKalle Valo int r; 8856948300cSKalle Valo struct aw_pt_bi s; 8866948300cSKalle Valo u32 b_interval, mode_flag; 8876948300cSKalle Valo 8886948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 8896948300cSKalle Valo 8906948300cSKalle Valo if (interval > 0) { 8916948300cSKalle Valo switch (type) { 8926948300cSKalle Valo case NL80211_IFTYPE_ADHOC: 8936948300cSKalle Valo case NL80211_IFTYPE_MESH_POINT: 8946948300cSKalle Valo mode_flag = BCN_MODE_IBSS; 8956948300cSKalle Valo break; 8966948300cSKalle Valo case NL80211_IFTYPE_AP: 8976948300cSKalle Valo mode_flag = BCN_MODE_AP; 8986948300cSKalle Valo break; 8996948300cSKalle Valo default: 9006948300cSKalle Valo mode_flag = 0; 9016948300cSKalle Valo break; 9026948300cSKalle Valo } 9036948300cSKalle Valo } else { 9046948300cSKalle Valo dtim_period = 0; 9056948300cSKalle Valo mode_flag = 0; 9066948300cSKalle Valo } 9076948300cSKalle Valo 9086948300cSKalle Valo b_interval = mode_flag | (dtim_period << 16) | interval; 9096948300cSKalle Valo 9106948300cSKalle Valo r = zd_iowrite32_locked(chip, b_interval, CR_BCN_INTERVAL); 9116948300cSKalle Valo if (r) 9126948300cSKalle Valo return r; 9136948300cSKalle Valo r = get_aw_pt_bi(chip, &s); 9146948300cSKalle Valo if (r) 9156948300cSKalle Valo return r; 9166948300cSKalle Valo return set_aw_pt_bi(chip, &s); 9176948300cSKalle Valo } 9186948300cSKalle Valo 9196948300cSKalle Valo int zd_set_beacon_interval(struct zd_chip *chip, u16 interval, u8 dtim_period, 9206948300cSKalle Valo int type) 9216948300cSKalle Valo { 9226948300cSKalle Valo int r; 9236948300cSKalle Valo 9246948300cSKalle Valo mutex_lock(&chip->mutex); 9256948300cSKalle Valo r = set_beacon_interval(chip, interval, dtim_period, type); 9266948300cSKalle Valo mutex_unlock(&chip->mutex); 9276948300cSKalle Valo return r; 9286948300cSKalle Valo } 9296948300cSKalle Valo 9306948300cSKalle Valo static int hw_init(struct zd_chip *chip) 9316948300cSKalle Valo { 9326948300cSKalle Valo int r; 9336948300cSKalle Valo 9346948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 9356948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 9366948300cSKalle Valo r = hw_reset_phy(chip); 9376948300cSKalle Valo if (r) 9386948300cSKalle Valo return r; 9396948300cSKalle Valo 9406948300cSKalle Valo r = hw_init_hmac(chip); 9416948300cSKalle Valo if (r) 9426948300cSKalle Valo return r; 9436948300cSKalle Valo 9446948300cSKalle Valo return set_beacon_interval(chip, 100, 0, NL80211_IFTYPE_UNSPECIFIED); 9456948300cSKalle Valo } 9466948300cSKalle Valo 9476948300cSKalle Valo static zd_addr_t fw_reg_addr(struct zd_chip *chip, u16 offset) 9486948300cSKalle Valo { 9496948300cSKalle Valo return (zd_addr_t)((u16)chip->fw_regs_base + offset); 9506948300cSKalle Valo } 9516948300cSKalle Valo 9526948300cSKalle Valo #ifdef DEBUG 9536948300cSKalle Valo static int dump_cr(struct zd_chip *chip, const zd_addr_t addr, 9546948300cSKalle Valo const char *addr_string) 9556948300cSKalle Valo { 9566948300cSKalle Valo int r; 9576948300cSKalle Valo u32 value; 9586948300cSKalle Valo 9596948300cSKalle Valo r = zd_ioread32_locked(chip, &value, addr); 9606948300cSKalle Valo if (r) { 9616948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), 9626948300cSKalle Valo "error reading %s. Error number %d\n", addr_string, r); 9636948300cSKalle Valo return r; 9646948300cSKalle Valo } 9656948300cSKalle Valo 9666948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "%s %#010x\n", 9676948300cSKalle Valo addr_string, (unsigned int)value); 9686948300cSKalle Valo return 0; 9696948300cSKalle Valo } 9706948300cSKalle Valo 9716948300cSKalle Valo static int test_init(struct zd_chip *chip) 9726948300cSKalle Valo { 9736948300cSKalle Valo int r; 9746948300cSKalle Valo 9756948300cSKalle Valo r = dump_cr(chip, CR_AFTER_PNP, "CR_AFTER_PNP"); 9766948300cSKalle Valo if (r) 9776948300cSKalle Valo return r; 9786948300cSKalle Valo r = dump_cr(chip, CR_GPI_EN, "CR_GPI_EN"); 9796948300cSKalle Valo if (r) 9806948300cSKalle Valo return r; 9816948300cSKalle Valo return dump_cr(chip, CR_INTERRUPT, "CR_INTERRUPT"); 9826948300cSKalle Valo } 9836948300cSKalle Valo 9846948300cSKalle Valo static void dump_fw_registers(struct zd_chip *chip) 9856948300cSKalle Valo { 9866948300cSKalle Valo const zd_addr_t addr[4] = { 9876948300cSKalle Valo fw_reg_addr(chip, FW_REG_FIRMWARE_VER), 9886948300cSKalle Valo fw_reg_addr(chip, FW_REG_USB_SPEED), 9896948300cSKalle Valo fw_reg_addr(chip, FW_REG_FIX_TX_RATE), 9906948300cSKalle Valo fw_reg_addr(chip, FW_REG_LED_LINK_STATUS), 9916948300cSKalle Valo }; 9926948300cSKalle Valo 9936948300cSKalle Valo int r; 9946948300cSKalle Valo u16 values[4]; 9956948300cSKalle Valo 9966948300cSKalle Valo r = zd_ioread16v_locked(chip, values, (const zd_addr_t*)addr, 9976948300cSKalle Valo ARRAY_SIZE(addr)); 9986948300cSKalle Valo if (r) { 9996948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "error %d zd_ioread16v_locked\n", 10006948300cSKalle Valo r); 10016948300cSKalle Valo return; 10026948300cSKalle Valo } 10036948300cSKalle Valo 10046948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "FW_FIRMWARE_VER %#06hx\n", values[0]); 10056948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "FW_USB_SPEED %#06hx\n", values[1]); 10066948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "FW_FIX_TX_RATE %#06hx\n", values[2]); 10076948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "FW_LINK_STATUS %#06hx\n", values[3]); 10086948300cSKalle Valo } 10096948300cSKalle Valo #endif /* DEBUG */ 10106948300cSKalle Valo 10116948300cSKalle Valo static int print_fw_version(struct zd_chip *chip) 10126948300cSKalle Valo { 10136948300cSKalle Valo struct wiphy *wiphy = zd_chip_to_mac(chip)->hw->wiphy; 10146948300cSKalle Valo int r; 10156948300cSKalle Valo u16 version; 10166948300cSKalle Valo 10176948300cSKalle Valo r = zd_ioread16_locked(chip, &version, 10186948300cSKalle Valo fw_reg_addr(chip, FW_REG_FIRMWARE_VER)); 10196948300cSKalle Valo if (r) 10206948300cSKalle Valo return r; 10216948300cSKalle Valo 10226948300cSKalle Valo dev_info(zd_chip_dev(chip),"firmware version %04hx\n", version); 10236948300cSKalle Valo 10246948300cSKalle Valo snprintf(wiphy->fw_version, sizeof(wiphy->fw_version), 10256948300cSKalle Valo "%04hx", version); 10266948300cSKalle Valo 10276948300cSKalle Valo return 0; 10286948300cSKalle Valo } 10296948300cSKalle Valo 10306948300cSKalle Valo static int set_mandatory_rates(struct zd_chip *chip, int gmode) 10316948300cSKalle Valo { 10326948300cSKalle Valo u32 rates; 10336948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 10346948300cSKalle Valo /* This sets the mandatory rates, which only depend from the standard 10356948300cSKalle Valo * that the device is supporting. Until further notice we should try 10366948300cSKalle Valo * to support 802.11g also for full speed USB. 10376948300cSKalle Valo */ 10386948300cSKalle Valo if (!gmode) 10396948300cSKalle Valo rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M; 10406948300cSKalle Valo else 10416948300cSKalle Valo rates = CR_RATE_1M|CR_RATE_2M|CR_RATE_5_5M|CR_RATE_11M| 10426948300cSKalle Valo CR_RATE_6M|CR_RATE_12M|CR_RATE_24M; 10436948300cSKalle Valo 10446948300cSKalle Valo return zd_iowrite32_locked(chip, rates, CR_MANDATORY_RATE_TBL); 10456948300cSKalle Valo } 10466948300cSKalle Valo 10476948300cSKalle Valo int zd_chip_set_rts_cts_rate_locked(struct zd_chip *chip, 10486948300cSKalle Valo int preamble) 10496948300cSKalle Valo { 10506948300cSKalle Valo u32 value = 0; 10516948300cSKalle Valo 10526948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "preamble=%x\n", preamble); 10536948300cSKalle Valo value |= preamble << RTSCTS_SH_RTS_PMB_TYPE; 10546948300cSKalle Valo value |= preamble << RTSCTS_SH_CTS_PMB_TYPE; 10556948300cSKalle Valo 10566948300cSKalle Valo /* We always send 11M RTS/self-CTS messages, like the vendor driver. */ 10576948300cSKalle Valo value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_RTS_RATE; 10586948300cSKalle Valo value |= ZD_RX_CCK << RTSCTS_SH_RTS_MOD_TYPE; 10596948300cSKalle Valo value |= ZD_PURE_RATE(ZD_CCK_RATE_11M) << RTSCTS_SH_CTS_RATE; 10606948300cSKalle Valo value |= ZD_RX_CCK << RTSCTS_SH_CTS_MOD_TYPE; 10616948300cSKalle Valo 10626948300cSKalle Valo return zd_iowrite32_locked(chip, value, CR_RTS_CTS_RATE); 10636948300cSKalle Valo } 10646948300cSKalle Valo 10656948300cSKalle Valo int zd_chip_enable_hwint(struct zd_chip *chip) 10666948300cSKalle Valo { 10676948300cSKalle Valo int r; 10686948300cSKalle Valo 10696948300cSKalle Valo mutex_lock(&chip->mutex); 10706948300cSKalle Valo r = zd_iowrite32_locked(chip, HWINT_ENABLED, CR_INTERRUPT); 10716948300cSKalle Valo mutex_unlock(&chip->mutex); 10726948300cSKalle Valo return r; 10736948300cSKalle Valo } 10746948300cSKalle Valo 10756948300cSKalle Valo static int disable_hwint(struct zd_chip *chip) 10766948300cSKalle Valo { 10776948300cSKalle Valo return zd_iowrite32_locked(chip, HWINT_DISABLED, CR_INTERRUPT); 10786948300cSKalle Valo } 10796948300cSKalle Valo 10806948300cSKalle Valo int zd_chip_disable_hwint(struct zd_chip *chip) 10816948300cSKalle Valo { 10826948300cSKalle Valo int r; 10836948300cSKalle Valo 10846948300cSKalle Valo mutex_lock(&chip->mutex); 10856948300cSKalle Valo r = disable_hwint(chip); 10866948300cSKalle Valo mutex_unlock(&chip->mutex); 10876948300cSKalle Valo return r; 10886948300cSKalle Valo } 10896948300cSKalle Valo 10906948300cSKalle Valo static int read_fw_regs_offset(struct zd_chip *chip) 10916948300cSKalle Valo { 10926948300cSKalle Valo int r; 10936948300cSKalle Valo 10946948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 10956948300cSKalle Valo r = zd_ioread16_locked(chip, (u16*)&chip->fw_regs_base, 10966948300cSKalle Valo FWRAW_REGS_ADDR); 10976948300cSKalle Valo if (r) 10986948300cSKalle Valo return r; 10996948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "fw_regs_base: %#06hx\n", 11006948300cSKalle Valo (u16)chip->fw_regs_base); 11016948300cSKalle Valo 11026948300cSKalle Valo return 0; 11036948300cSKalle Valo } 11046948300cSKalle Valo 11056948300cSKalle Valo /* Read mac address using pre-firmware interface */ 11066948300cSKalle Valo int zd_chip_read_mac_addr_fw(struct zd_chip *chip, u8 *addr) 11076948300cSKalle Valo { 11086948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 11096948300cSKalle Valo return zd_usb_read_fw(&chip->usb, E2P_MAC_ADDR_P1, addr, 11106948300cSKalle Valo ETH_ALEN); 11116948300cSKalle Valo } 11126948300cSKalle Valo 11136948300cSKalle Valo int zd_chip_init_hw(struct zd_chip *chip) 11146948300cSKalle Valo { 11156948300cSKalle Valo int r; 11166948300cSKalle Valo u8 rf_type; 11176948300cSKalle Valo 11186948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "\n"); 11196948300cSKalle Valo 11206948300cSKalle Valo mutex_lock(&chip->mutex); 11216948300cSKalle Valo 11226948300cSKalle Valo #ifdef DEBUG 11236948300cSKalle Valo r = test_init(chip); 11246948300cSKalle Valo if (r) 11256948300cSKalle Valo goto out; 11266948300cSKalle Valo #endif 11276948300cSKalle Valo r = zd_iowrite32_locked(chip, 1, CR_AFTER_PNP); 11286948300cSKalle Valo if (r) 11296948300cSKalle Valo goto out; 11306948300cSKalle Valo 11316948300cSKalle Valo r = read_fw_regs_offset(chip); 11326948300cSKalle Valo if (r) 11336948300cSKalle Valo goto out; 11346948300cSKalle Valo 11356948300cSKalle Valo /* GPI is always disabled, also in the other driver. 11366948300cSKalle Valo */ 11376948300cSKalle Valo r = zd_iowrite32_locked(chip, 0, CR_GPI_EN); 11386948300cSKalle Valo if (r) 11396948300cSKalle Valo goto out; 11406948300cSKalle Valo r = zd_iowrite32_locked(chip, CWIN_SIZE, CR_CWMIN_CWMAX); 11416948300cSKalle Valo if (r) 11426948300cSKalle Valo goto out; 11436948300cSKalle Valo /* Currently we support IEEE 802.11g for full and high speed USB. 11446948300cSKalle Valo * It might be discussed, whether we should support pure b mode for 11456948300cSKalle Valo * full speed USB. 11466948300cSKalle Valo */ 11476948300cSKalle Valo r = set_mandatory_rates(chip, 1); 11486948300cSKalle Valo if (r) 11496948300cSKalle Valo goto out; 11506948300cSKalle Valo /* Disabling interrupts is certainly a smart thing here. 11516948300cSKalle Valo */ 11526948300cSKalle Valo r = disable_hwint(chip); 11536948300cSKalle Valo if (r) 11546948300cSKalle Valo goto out; 11556948300cSKalle Valo r = read_pod(chip, &rf_type); 11566948300cSKalle Valo if (r) 11576948300cSKalle Valo goto out; 11586948300cSKalle Valo r = hw_init(chip); 11596948300cSKalle Valo if (r) 11606948300cSKalle Valo goto out; 11616948300cSKalle Valo r = zd_rf_init_hw(&chip->rf, rf_type); 11626948300cSKalle Valo if (r) 11636948300cSKalle Valo goto out; 11646948300cSKalle Valo 11656948300cSKalle Valo r = print_fw_version(chip); 11666948300cSKalle Valo if (r) 11676948300cSKalle Valo goto out; 11686948300cSKalle Valo 11696948300cSKalle Valo #ifdef DEBUG 11706948300cSKalle Valo dump_fw_registers(chip); 11716948300cSKalle Valo r = test_init(chip); 11726948300cSKalle Valo if (r) 11736948300cSKalle Valo goto out; 11746948300cSKalle Valo #endif /* DEBUG */ 11756948300cSKalle Valo 11766948300cSKalle Valo r = read_cal_int_tables(chip); 11776948300cSKalle Valo if (r) 11786948300cSKalle Valo goto out; 11796948300cSKalle Valo 11806948300cSKalle Valo print_id(chip); 11816948300cSKalle Valo out: 11826948300cSKalle Valo mutex_unlock(&chip->mutex); 11836948300cSKalle Valo return r; 11846948300cSKalle Valo } 11856948300cSKalle Valo 11866948300cSKalle Valo static int update_pwr_int(struct zd_chip *chip, u8 channel) 11876948300cSKalle Valo { 11886948300cSKalle Valo u8 value = chip->pwr_int_values[channel - 1]; 11896948300cSKalle Valo return zd_iowrite16_locked(chip, value, ZD_CR31); 11906948300cSKalle Valo } 11916948300cSKalle Valo 11926948300cSKalle Valo static int update_pwr_cal(struct zd_chip *chip, u8 channel) 11936948300cSKalle Valo { 11946948300cSKalle Valo u8 value = chip->pwr_cal_values[channel-1]; 11956948300cSKalle Valo return zd_iowrite16_locked(chip, value, ZD_CR68); 11966948300cSKalle Valo } 11976948300cSKalle Valo 11986948300cSKalle Valo static int update_ofdm_cal(struct zd_chip *chip, u8 channel) 11996948300cSKalle Valo { 12006948300cSKalle Valo struct zd_ioreq16 ioreqs[3]; 12016948300cSKalle Valo 12026948300cSKalle Valo ioreqs[0].addr = ZD_CR67; 12036948300cSKalle Valo ioreqs[0].value = chip->ofdm_cal_values[OFDM_36M_INDEX][channel-1]; 12046948300cSKalle Valo ioreqs[1].addr = ZD_CR66; 12056948300cSKalle Valo ioreqs[1].value = chip->ofdm_cal_values[OFDM_48M_INDEX][channel-1]; 12066948300cSKalle Valo ioreqs[2].addr = ZD_CR65; 12076948300cSKalle Valo ioreqs[2].value = chip->ofdm_cal_values[OFDM_54M_INDEX][channel-1]; 12086948300cSKalle Valo 12096948300cSKalle Valo return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 12106948300cSKalle Valo } 12116948300cSKalle Valo 12126948300cSKalle Valo static int update_channel_integration_and_calibration(struct zd_chip *chip, 12136948300cSKalle Valo u8 channel) 12146948300cSKalle Valo { 12156948300cSKalle Valo int r; 12166948300cSKalle Valo 12176948300cSKalle Valo if (!zd_rf_should_update_pwr_int(&chip->rf)) 12186948300cSKalle Valo return 0; 12196948300cSKalle Valo 12206948300cSKalle Valo r = update_pwr_int(chip, channel); 12216948300cSKalle Valo if (r) 12226948300cSKalle Valo return r; 12236948300cSKalle Valo if (zd_chip_is_zd1211b(chip)) { 12246948300cSKalle Valo static const struct zd_ioreq16 ioreqs[] = { 12256948300cSKalle Valo { ZD_CR69, 0x28 }, 12266948300cSKalle Valo {}, 12276948300cSKalle Valo { ZD_CR69, 0x2a }, 12286948300cSKalle Valo }; 12296948300cSKalle Valo 12306948300cSKalle Valo r = update_ofdm_cal(chip, channel); 12316948300cSKalle Valo if (r) 12326948300cSKalle Valo return r; 12336948300cSKalle Valo r = update_pwr_cal(chip, channel); 12346948300cSKalle Valo if (r) 12356948300cSKalle Valo return r; 12366948300cSKalle Valo r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 12376948300cSKalle Valo if (r) 12386948300cSKalle Valo return r; 12396948300cSKalle Valo } 12406948300cSKalle Valo 12416948300cSKalle Valo return 0; 12426948300cSKalle Valo } 12436948300cSKalle Valo 12446948300cSKalle Valo /* The CCK baseband gain can be optionally patched by the EEPROM */ 12456948300cSKalle Valo static int patch_cck_gain(struct zd_chip *chip) 12466948300cSKalle Valo { 12476948300cSKalle Valo int r; 12486948300cSKalle Valo u32 value; 12496948300cSKalle Valo 12506948300cSKalle Valo if (!chip->patch_cck_gain || !zd_rf_should_patch_cck_gain(&chip->rf)) 12516948300cSKalle Valo return 0; 12526948300cSKalle Valo 12536948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 12546948300cSKalle Valo r = zd_ioread32_locked(chip, &value, E2P_PHY_REG); 12556948300cSKalle Valo if (r) 12566948300cSKalle Valo return r; 12576948300cSKalle Valo dev_dbg_f(zd_chip_dev(chip), "patching value %x\n", value & 0xff); 12586948300cSKalle Valo return zd_iowrite16_locked(chip, value & 0xff, ZD_CR47); 12596948300cSKalle Valo } 12606948300cSKalle Valo 12616948300cSKalle Valo int zd_chip_set_channel(struct zd_chip *chip, u8 channel) 12626948300cSKalle Valo { 12636948300cSKalle Valo int r, t; 12646948300cSKalle Valo 12656948300cSKalle Valo mutex_lock(&chip->mutex); 12666948300cSKalle Valo r = zd_chip_lock_phy_regs(chip); 12676948300cSKalle Valo if (r) 12686948300cSKalle Valo goto out; 12696948300cSKalle Valo r = zd_rf_set_channel(&chip->rf, channel); 12706948300cSKalle Valo if (r) 12716948300cSKalle Valo goto unlock; 12726948300cSKalle Valo r = update_channel_integration_and_calibration(chip, channel); 12736948300cSKalle Valo if (r) 12746948300cSKalle Valo goto unlock; 12756948300cSKalle Valo r = patch_cck_gain(chip); 12766948300cSKalle Valo if (r) 12776948300cSKalle Valo goto unlock; 12786948300cSKalle Valo r = patch_6m_band_edge(chip, channel); 12796948300cSKalle Valo if (r) 12806948300cSKalle Valo goto unlock; 12816948300cSKalle Valo r = zd_iowrite32_locked(chip, 0, CR_CONFIG_PHILIPS); 12826948300cSKalle Valo unlock: 12836948300cSKalle Valo t = zd_chip_unlock_phy_regs(chip); 12846948300cSKalle Valo if (t && !r) 12856948300cSKalle Valo r = t; 12866948300cSKalle Valo out: 12876948300cSKalle Valo mutex_unlock(&chip->mutex); 12886948300cSKalle Valo return r; 12896948300cSKalle Valo } 12906948300cSKalle Valo 12916948300cSKalle Valo u8 zd_chip_get_channel(struct zd_chip *chip) 12926948300cSKalle Valo { 12936948300cSKalle Valo u8 channel; 12946948300cSKalle Valo 12956948300cSKalle Valo mutex_lock(&chip->mutex); 12966948300cSKalle Valo channel = chip->rf.channel; 12976948300cSKalle Valo mutex_unlock(&chip->mutex); 12986948300cSKalle Valo return channel; 12996948300cSKalle Valo } 13006948300cSKalle Valo 13016948300cSKalle Valo int zd_chip_control_leds(struct zd_chip *chip, enum led_status status) 13026948300cSKalle Valo { 13036948300cSKalle Valo const zd_addr_t a[] = { 13046948300cSKalle Valo fw_reg_addr(chip, FW_REG_LED_LINK_STATUS), 13056948300cSKalle Valo CR_LED, 13066948300cSKalle Valo }; 13076948300cSKalle Valo 13086948300cSKalle Valo int r; 13096948300cSKalle Valo u16 v[ARRAY_SIZE(a)]; 13106948300cSKalle Valo struct zd_ioreq16 ioreqs[ARRAY_SIZE(a)] = { 13116948300cSKalle Valo [0] = { fw_reg_addr(chip, FW_REG_LED_LINK_STATUS) }, 13126948300cSKalle Valo [1] = { CR_LED }, 13136948300cSKalle Valo }; 13146948300cSKalle Valo u16 other_led; 13156948300cSKalle Valo 13166948300cSKalle Valo mutex_lock(&chip->mutex); 13176948300cSKalle Valo r = zd_ioread16v_locked(chip, v, (const zd_addr_t *)a, ARRAY_SIZE(a)); 13186948300cSKalle Valo if (r) 13196948300cSKalle Valo goto out; 13206948300cSKalle Valo 13216948300cSKalle Valo other_led = chip->link_led == LED1 ? LED2 : LED1; 13226948300cSKalle Valo 13236948300cSKalle Valo switch (status) { 13246948300cSKalle Valo case ZD_LED_OFF: 13256948300cSKalle Valo ioreqs[0].value = FW_LINK_OFF; 13266948300cSKalle Valo ioreqs[1].value = v[1] & ~(LED1|LED2); 13276948300cSKalle Valo break; 13286948300cSKalle Valo case ZD_LED_SCANNING: 13296948300cSKalle Valo ioreqs[0].value = FW_LINK_OFF; 13306948300cSKalle Valo ioreqs[1].value = v[1] & ~other_led; 133171e140b5SArnd Bergmann if ((u32)ktime_get_seconds() % 3 == 0) { 13326948300cSKalle Valo ioreqs[1].value &= ~chip->link_led; 13336948300cSKalle Valo } else { 13346948300cSKalle Valo ioreqs[1].value |= chip->link_led; 13356948300cSKalle Valo } 13366948300cSKalle Valo break; 13376948300cSKalle Valo case ZD_LED_ASSOCIATED: 13386948300cSKalle Valo ioreqs[0].value = FW_LINK_TX; 13396948300cSKalle Valo ioreqs[1].value = v[1] & ~other_led; 13406948300cSKalle Valo ioreqs[1].value |= chip->link_led; 13416948300cSKalle Valo break; 13426948300cSKalle Valo default: 13436948300cSKalle Valo r = -EINVAL; 13446948300cSKalle Valo goto out; 13456948300cSKalle Valo } 13466948300cSKalle Valo 13476948300cSKalle Valo if (v[0] != ioreqs[0].value || v[1] != ioreqs[1].value) { 13486948300cSKalle Valo r = zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 13496948300cSKalle Valo if (r) 13506948300cSKalle Valo goto out; 13516948300cSKalle Valo } 13526948300cSKalle Valo r = 0; 13536948300cSKalle Valo out: 13546948300cSKalle Valo mutex_unlock(&chip->mutex); 13556948300cSKalle Valo return r; 13566948300cSKalle Valo } 13576948300cSKalle Valo 13586948300cSKalle Valo int zd_chip_set_basic_rates(struct zd_chip *chip, u16 cr_rates) 13596948300cSKalle Valo { 13606948300cSKalle Valo int r; 13616948300cSKalle Valo 13626948300cSKalle Valo if (cr_rates & ~(CR_RATES_80211B|CR_RATES_80211G)) 13636948300cSKalle Valo return -EINVAL; 13646948300cSKalle Valo 13656948300cSKalle Valo mutex_lock(&chip->mutex); 13666948300cSKalle Valo r = zd_iowrite32_locked(chip, cr_rates, CR_BASIC_RATE_TBL); 13676948300cSKalle Valo mutex_unlock(&chip->mutex); 13686948300cSKalle Valo return r; 13696948300cSKalle Valo } 13706948300cSKalle Valo 13716948300cSKalle Valo static inline u8 zd_rate_from_ofdm_plcp_header(const void *rx_frame) 13726948300cSKalle Valo { 13736948300cSKalle Valo return ZD_OFDM | zd_ofdm_plcp_header_rate(rx_frame); 13746948300cSKalle Valo } 13756948300cSKalle Valo 13766948300cSKalle Valo /** 13776948300cSKalle Valo * zd_rx_rate - report zd-rate 13786948300cSKalle Valo * @rx_frame - received frame 13796948300cSKalle Valo * @rx_status - rx_status as given by the device 13806948300cSKalle Valo * 13816948300cSKalle Valo * This function converts the rate as encoded in the received packet to the 13826948300cSKalle Valo * zd-rate, we are using on other places in the driver. 13836948300cSKalle Valo */ 13846948300cSKalle Valo u8 zd_rx_rate(const void *rx_frame, const struct rx_status *status) 13856948300cSKalle Valo { 13866948300cSKalle Valo u8 zd_rate; 13876948300cSKalle Valo if (status->frame_status & ZD_RX_OFDM) { 13886948300cSKalle Valo zd_rate = zd_rate_from_ofdm_plcp_header(rx_frame); 13896948300cSKalle Valo } else { 13906948300cSKalle Valo switch (zd_cck_plcp_header_signal(rx_frame)) { 13916948300cSKalle Valo case ZD_CCK_PLCP_SIGNAL_1M: 13926948300cSKalle Valo zd_rate = ZD_CCK_RATE_1M; 13936948300cSKalle Valo break; 13946948300cSKalle Valo case ZD_CCK_PLCP_SIGNAL_2M: 13956948300cSKalle Valo zd_rate = ZD_CCK_RATE_2M; 13966948300cSKalle Valo break; 13976948300cSKalle Valo case ZD_CCK_PLCP_SIGNAL_5M5: 13986948300cSKalle Valo zd_rate = ZD_CCK_RATE_5_5M; 13996948300cSKalle Valo break; 14006948300cSKalle Valo case ZD_CCK_PLCP_SIGNAL_11M: 14016948300cSKalle Valo zd_rate = ZD_CCK_RATE_11M; 14026948300cSKalle Valo break; 14036948300cSKalle Valo default: 14046948300cSKalle Valo zd_rate = 0; 14056948300cSKalle Valo } 14066948300cSKalle Valo } 14076948300cSKalle Valo 14086948300cSKalle Valo return zd_rate; 14096948300cSKalle Valo } 14106948300cSKalle Valo 14116948300cSKalle Valo int zd_chip_switch_radio_on(struct zd_chip *chip) 14126948300cSKalle Valo { 14136948300cSKalle Valo int r; 14146948300cSKalle Valo 14156948300cSKalle Valo mutex_lock(&chip->mutex); 14166948300cSKalle Valo r = zd_switch_radio_on(&chip->rf); 14176948300cSKalle Valo mutex_unlock(&chip->mutex); 14186948300cSKalle Valo return r; 14196948300cSKalle Valo } 14206948300cSKalle Valo 14216948300cSKalle Valo int zd_chip_switch_radio_off(struct zd_chip *chip) 14226948300cSKalle Valo { 14236948300cSKalle Valo int r; 14246948300cSKalle Valo 14256948300cSKalle Valo mutex_lock(&chip->mutex); 14266948300cSKalle Valo r = zd_switch_radio_off(&chip->rf); 14276948300cSKalle Valo mutex_unlock(&chip->mutex); 14286948300cSKalle Valo return r; 14296948300cSKalle Valo } 14306948300cSKalle Valo 14316948300cSKalle Valo int zd_chip_enable_int(struct zd_chip *chip) 14326948300cSKalle Valo { 14336948300cSKalle Valo int r; 14346948300cSKalle Valo 14356948300cSKalle Valo mutex_lock(&chip->mutex); 14366948300cSKalle Valo r = zd_usb_enable_int(&chip->usb); 14376948300cSKalle Valo mutex_unlock(&chip->mutex); 14386948300cSKalle Valo return r; 14396948300cSKalle Valo } 14406948300cSKalle Valo 14416948300cSKalle Valo void zd_chip_disable_int(struct zd_chip *chip) 14426948300cSKalle Valo { 14436948300cSKalle Valo mutex_lock(&chip->mutex); 14446948300cSKalle Valo zd_usb_disable_int(&chip->usb); 14456948300cSKalle Valo mutex_unlock(&chip->mutex); 14466948300cSKalle Valo 14476948300cSKalle Valo /* cancel pending interrupt work */ 14486948300cSKalle Valo cancel_work_sync(&zd_chip_to_mac(chip)->process_intr); 14496948300cSKalle Valo } 14506948300cSKalle Valo 14516948300cSKalle Valo int zd_chip_enable_rxtx(struct zd_chip *chip) 14526948300cSKalle Valo { 14536948300cSKalle Valo int r; 14546948300cSKalle Valo 14556948300cSKalle Valo mutex_lock(&chip->mutex); 14566948300cSKalle Valo zd_usb_enable_tx(&chip->usb); 14576948300cSKalle Valo r = zd_usb_enable_rx(&chip->usb); 14586948300cSKalle Valo zd_tx_watchdog_enable(&chip->usb); 14596948300cSKalle Valo mutex_unlock(&chip->mutex); 14606948300cSKalle Valo return r; 14616948300cSKalle Valo } 14626948300cSKalle Valo 14636948300cSKalle Valo void zd_chip_disable_rxtx(struct zd_chip *chip) 14646948300cSKalle Valo { 14656948300cSKalle Valo mutex_lock(&chip->mutex); 14666948300cSKalle Valo zd_tx_watchdog_disable(&chip->usb); 14676948300cSKalle Valo zd_usb_disable_rx(&chip->usb); 14686948300cSKalle Valo zd_usb_disable_tx(&chip->usb); 14696948300cSKalle Valo mutex_unlock(&chip->mutex); 14706948300cSKalle Valo } 14716948300cSKalle Valo 14726948300cSKalle Valo int zd_rfwritev_locked(struct zd_chip *chip, 14736948300cSKalle Valo const u32* values, unsigned int count, u8 bits) 14746948300cSKalle Valo { 14756948300cSKalle Valo int r; 14766948300cSKalle Valo unsigned int i; 14776948300cSKalle Valo 14786948300cSKalle Valo for (i = 0; i < count; i++) { 14796948300cSKalle Valo r = zd_rfwrite_locked(chip, values[i], bits); 14806948300cSKalle Valo if (r) 14816948300cSKalle Valo return r; 14826948300cSKalle Valo } 14836948300cSKalle Valo 14846948300cSKalle Valo return 0; 14856948300cSKalle Valo } 14866948300cSKalle Valo 14876948300cSKalle Valo /* 14886948300cSKalle Valo * We can optionally program the RF directly through CR regs, if supported by 14896948300cSKalle Valo * the hardware. This is much faster than the older method. 14906948300cSKalle Valo */ 14916948300cSKalle Valo int zd_rfwrite_cr_locked(struct zd_chip *chip, u32 value) 14926948300cSKalle Valo { 14936948300cSKalle Valo const struct zd_ioreq16 ioreqs[] = { 14946948300cSKalle Valo { ZD_CR244, (value >> 16) & 0xff }, 14956948300cSKalle Valo { ZD_CR243, (value >> 8) & 0xff }, 14966948300cSKalle Valo { ZD_CR242, value & 0xff }, 14976948300cSKalle Valo }; 14986948300cSKalle Valo ZD_ASSERT(mutex_is_locked(&chip->mutex)); 14996948300cSKalle Valo return zd_iowrite16a_locked(chip, ioreqs, ARRAY_SIZE(ioreqs)); 15006948300cSKalle Valo } 15016948300cSKalle Valo 15026948300cSKalle Valo int zd_rfwritev_cr_locked(struct zd_chip *chip, 15036948300cSKalle Valo const u32 *values, unsigned int count) 15046948300cSKalle Valo { 15056948300cSKalle Valo int r; 15066948300cSKalle Valo unsigned int i; 15076948300cSKalle Valo 15086948300cSKalle Valo for (i = 0; i < count; i++) { 15096948300cSKalle Valo r = zd_rfwrite_cr_locked(chip, values[i]); 15106948300cSKalle Valo if (r) 15116948300cSKalle Valo return r; 15126948300cSKalle Valo } 15136948300cSKalle Valo 15146948300cSKalle Valo return 0; 15156948300cSKalle Valo } 15166948300cSKalle Valo 15176948300cSKalle Valo int zd_chip_set_multicast_hash(struct zd_chip *chip, 15186948300cSKalle Valo struct zd_mc_hash *hash) 15196948300cSKalle Valo { 15206948300cSKalle Valo const struct zd_ioreq32 ioreqs[] = { 15216948300cSKalle Valo { CR_GROUP_HASH_P1, hash->low }, 15226948300cSKalle Valo { CR_GROUP_HASH_P2, hash->high }, 15236948300cSKalle Valo }; 15246948300cSKalle Valo 15256948300cSKalle Valo return zd_iowrite32a(chip, ioreqs, ARRAY_SIZE(ioreqs)); 15266948300cSKalle Valo } 15276948300cSKalle Valo 15286948300cSKalle Valo u64 zd_chip_get_tsf(struct zd_chip *chip) 15296948300cSKalle Valo { 15306948300cSKalle Valo int r; 15316948300cSKalle Valo static const zd_addr_t aw_pt_bi_addr[] = 15326948300cSKalle Valo { CR_TSF_LOW_PART, CR_TSF_HIGH_PART }; 15336948300cSKalle Valo u32 values[2]; 15346948300cSKalle Valo u64 tsf; 15356948300cSKalle Valo 15366948300cSKalle Valo mutex_lock(&chip->mutex); 15376948300cSKalle Valo r = zd_ioread32v_locked(chip, values, (const zd_addr_t *)aw_pt_bi_addr, 15386948300cSKalle Valo ARRAY_SIZE(aw_pt_bi_addr)); 15396948300cSKalle Valo mutex_unlock(&chip->mutex); 15406948300cSKalle Valo if (r) 15416948300cSKalle Valo return 0; 15426948300cSKalle Valo 15436948300cSKalle Valo tsf = values[1]; 15446948300cSKalle Valo tsf = (tsf << 32) | values[0]; 15456948300cSKalle Valo 15466948300cSKalle Valo return tsf; 15476948300cSKalle Valo } 1548