[59fa7ab] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jan Kolarik
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file hw.c
|
---|
| 30 | *
|
---|
| 31 | * AR9271 hardware related functions implementation.
|
---|
| 32 | *
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <macros.h>
|
---|
| 36 | #include <usb/debug.h>
|
---|
| 37 | #include <unistd.h>
|
---|
[fb1007ef] | 38 | #include <errno.h>
|
---|
[59fa7ab] | 39 | #include <nic.h>
|
---|
| 40 | #include <ieee80211.h>
|
---|
| 41 | #include "hw.h"
|
---|
| 42 | #include "wmi.h"
|
---|
| 43 |
|
---|
[8a64320e] | 44 | /** Try to wait for register value repeatedly until timeout is reached.
|
---|
| 45 | *
|
---|
[59fa7ab] | 46 | * @param ar9271 Device structure.
|
---|
| 47 | * @param offset Registry offset (address) to be read.
|
---|
[8a64320e] | 48 | * @param mask Mask to apply on read result.
|
---|
| 49 | * @param value Required value we are waiting for.
|
---|
| 50 | *
|
---|
| 51 | * @return EOK if succeed, ETIMEOUT on timeout,
|
---|
| 52 | * negative error code otherwise.
|
---|
| 53 | *
|
---|
[59fa7ab] | 54 | */
|
---|
| 55 | static int hw_read_wait(ar9271_t *ar9271, uint32_t offset, uint32_t mask,
|
---|
[8a64320e] | 56 | uint32_t value)
|
---|
[59fa7ab] | 57 | {
|
---|
[8a64320e] | 58 | for (size_t i = 0; i < HW_WAIT_LOOPS; i++) {
|
---|
[59fa7ab] | 59 | udelay(HW_WAIT_TIME_US);
|
---|
[8a64320e] | 60 |
|
---|
| 61 | uint32_t result;
|
---|
[59fa7ab] | 62 | wmi_reg_read(ar9271->htc_device, offset, &result);
|
---|
[8a64320e] | 63 | if ((result & mask) == value)
|
---|
[59fa7ab] | 64 | return EOK;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return ETIMEOUT;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | static int hw_reset_power_on(ar9271_t *ar9271)
|
---|
| 71 | {
|
---|
| 72 | wmi_reg_t buffer[] = {
|
---|
| 73 | {
|
---|
| 74 | .offset = AR9271_RTC_FORCE_WAKE,
|
---|
[8a64320e] | 75 | .value = AR9271_RTC_FORCE_WAKE_ENABLE |
|
---|
| 76 | AR9271_RTC_FORCE_WAKE_ON_INT
|
---|
[59fa7ab] | 77 | },
|
---|
| 78 | {
|
---|
| 79 | .offset = AR9271_RC,
|
---|
| 80 | .value = AR9271_RC_AHB
|
---|
| 81 | },
|
---|
| 82 | {
|
---|
| 83 | .offset = AR9271_RTC_RESET,
|
---|
| 84 | .value = 0
|
---|
| 85 | }
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | wmi_reg_buffer_write(ar9271->htc_device, buffer,
|
---|
[8a64320e] | 89 | sizeof(buffer) / sizeof(wmi_reg_t));
|
---|
[59fa7ab] | 90 |
|
---|
| 91 | udelay(2);
|
---|
| 92 |
|
---|
| 93 | wmi_reg_write(ar9271->htc_device, AR9271_RC, 0);
|
---|
| 94 | wmi_reg_write(ar9271->htc_device, AR9271_RTC_RESET, 1);
|
---|
| 95 |
|
---|
[8a64320e] | 96 | int rc = hw_read_wait(ar9271,
|
---|
| 97 | AR9271_RTC_STATUS,
|
---|
| 98 | AR9271_RTC_STATUS_MASK,
|
---|
| 99 | AR9271_RTC_STATUS_ON);
|
---|
| 100 | if (rc != EOK) {
|
---|
[59fa7ab] | 101 | usb_log_error("Failed to wait for RTC wake up register.\n");
|
---|
| 102 | return rc;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | return EOK;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static int hw_set_reset(ar9271_t *ar9271, bool cold)
|
---|
| 109 | {
|
---|
| 110 | uint32_t reset_value = AR9271_RTC_RC_MAC_WARM;
|
---|
| 111 |
|
---|
[8a64320e] | 112 | if (cold)
|
---|
[59fa7ab] | 113 | reset_value |= AR9271_RTC_RC_MAC_COLD;
|
---|
| 114 |
|
---|
| 115 | wmi_reg_t buffer[] = {
|
---|
| 116 | {
|
---|
| 117 | .offset = AR9271_RTC_FORCE_WAKE,
|
---|
[8a64320e] | 118 | .value = AR9271_RTC_FORCE_WAKE_ENABLE |
|
---|
| 119 | AR9271_RTC_FORCE_WAKE_ON_INT
|
---|
[59fa7ab] | 120 | },
|
---|
| 121 | {
|
---|
| 122 | .offset = AR9271_RC,
|
---|
| 123 | .value = AR9271_RC_AHB
|
---|
| 124 | },
|
---|
| 125 | {
|
---|
| 126 | .offset = AR9271_RTC_RC,
|
---|
| 127 | .value = reset_value
|
---|
| 128 | }
|
---|
| 129 | };
|
---|
| 130 |
|
---|
[8a64320e] | 131 | wmi_reg_buffer_write(ar9271->htc_device, buffer,
|
---|
| 132 | sizeof(buffer) / sizeof(wmi_reg_t));
|
---|
[59fa7ab] | 133 |
|
---|
| 134 | udelay(100);
|
---|
| 135 |
|
---|
| 136 | wmi_reg_write(ar9271->htc_device, AR9271_RTC_RC, 0);
|
---|
| 137 |
|
---|
| 138 | int rc = hw_read_wait(ar9271, AR9271_RTC_RC, AR9271_RTC_RC_MASK, 0);
|
---|
[8a64320e] | 139 | if (rc != EOK) {
|
---|
[59fa7ab] | 140 | usb_log_error("Failed to wait for RTC RC register.\n");
|
---|
| 141 | return rc;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | wmi_reg_write(ar9271->htc_device, AR9271_RC, 0);
|
---|
[8a64320e] | 145 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_STATION_ID1,
|
---|
| 146 | AR9271_STATION_ID1_POWER_SAVING);
|
---|
[59fa7ab] | 147 |
|
---|
| 148 | return EOK;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | static int hw_addr_init(ar9271_t *ar9271)
|
---|
| 152 | {
|
---|
| 153 | uint32_t value;
|
---|
| 154 | nic_address_t ar9271_address;
|
---|
| 155 |
|
---|
[8a64320e] | 156 | for (unsigned int i = 0; i < 3; i++) {
|
---|
| 157 | wmi_reg_read(ar9271->htc_device,
|
---|
| 158 | AR9271_EEPROM_MAC_ADDR_START + i * 4, &value);
|
---|
[59fa7ab] | 159 |
|
---|
| 160 | uint16_t two_bytes = uint16_t_be2host(value);
|
---|
| 161 | ar9271_address.address[2*i] = two_bytes >> 8;
|
---|
| 162 | ar9271_address.address[2*i+1] = two_bytes & 0xff;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | nic_t *nic = nic_get_from_ddf_dev(ar9271->ddf_dev);
|
---|
| 166 |
|
---|
| 167 | int rc = nic_report_address(nic, &ar9271_address);
|
---|
[8a64320e] | 168 | if (rc != EOK) {
|
---|
[59fa7ab] | 169 | usb_log_error("Failed to report NIC HW address.\n");
|
---|
| 170 | return rc;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | return EOK;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | static int hw_gpio_set_output(ar9271_t *ar9271, uint32_t gpio, uint32_t type)
|
---|
| 177 | {
|
---|
[8a64320e] | 178 | uint32_t address;
|
---|
[59fa7ab] | 179 |
|
---|
[8a64320e] | 180 | if (gpio > 11)
|
---|
[59fa7ab] | 181 | address = AR9271_GPIO_OUT_MUX3;
|
---|
[8a64320e] | 182 | else if (gpio > 5)
|
---|
[59fa7ab] | 183 | address = AR9271_GPIO_OUT_MUX2;
|
---|
[8a64320e] | 184 | else
|
---|
[59fa7ab] | 185 | address = AR9271_GPIO_OUT_MUX1;
|
---|
| 186 |
|
---|
[8a64320e] | 187 | uint32_t gpio_shift = (gpio % 6) * 5;
|
---|
[59fa7ab] | 188 |
|
---|
[8a64320e] | 189 | uint32_t temp;
|
---|
[59fa7ab] | 190 | wmi_reg_read(ar9271->htc_device, address, &temp);
|
---|
[8a64320e] | 191 |
|
---|
| 192 | temp = ((temp & 0x1f0) << 1) | (temp & ~0x1f0);
|
---|
[59fa7ab] | 193 | temp &= ~(0x1f << gpio_shift);
|
---|
| 194 | temp |= (type << gpio_shift);
|
---|
[8a64320e] | 195 |
|
---|
[59fa7ab] | 196 | wmi_reg_write(ar9271->htc_device, address, temp);
|
---|
| 197 |
|
---|
| 198 | gpio_shift = 2 * gpio;
|
---|
| 199 |
|
---|
| 200 | wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_GPIO_OE_OUT,
|
---|
[8a64320e] | 201 | AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift,
|
---|
| 202 | AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift);
|
---|
[59fa7ab] | 203 |
|
---|
| 204 | return EOK;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | static int hw_gpio_set_value(ar9271_t *ar9271, uint32_t gpio, uint32_t value)
|
---|
| 208 | {
|
---|
| 209 | wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_GPIO_IN_OUT,
|
---|
[8a64320e] | 210 | (~value & 1) << gpio, 1 << gpio);
|
---|
[59fa7ab] | 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[8a64320e] | 214 | /**Hardware init procedure of AR9271 device.
|
---|
| 215 | *
|
---|
[59fa7ab] | 216 | * @param ar9271 Device structure.
|
---|
[8a64320e] | 217 | *
|
---|
[59fa7ab] | 218 | * @return EOK if succeed, negative error code otherwise.
|
---|
[8a64320e] | 219 | *
|
---|
[59fa7ab] | 220 | */
|
---|
| 221 | static int hw_init_proc(ar9271_t *ar9271)
|
---|
| 222 | {
|
---|
| 223 | int rc = hw_reset_power_on(ar9271);
|
---|
[8a64320e] | 224 | if (rc != EOK) {
|
---|
[59fa7ab] | 225 | usb_log_error("Failed to HW reset power on.\n");
|
---|
| 226 | return rc;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | rc = hw_set_reset(ar9271, false);
|
---|
[8a64320e] | 230 | if (rc != EOK) {
|
---|
[59fa7ab] | 231 | usb_log_error("Failed to HW warm reset.\n");
|
---|
| 232 | return rc;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | rc = hw_addr_init(ar9271);
|
---|
[8a64320e] | 236 | if (rc != EOK) {
|
---|
[59fa7ab] | 237 | usb_log_error("Failed to init HW addr.\n");
|
---|
| 238 | return rc;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | return EOK;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | static int hw_init_led(ar9271_t *ar9271)
|
---|
| 245 | {
|
---|
[8a64320e] | 246 | int rc = hw_gpio_set_output(ar9271, AR9271_LED_PIN,
|
---|
| 247 | AR9271_GPIO_OUT_MUX_AS_OUT);
|
---|
| 248 | if (rc != EOK) {
|
---|
[59fa7ab] | 249 | usb_log_error("Failed to set led GPIO to output.\n");
|
---|
| 250 | return rc;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | rc = hw_gpio_set_value(ar9271, AR9271_LED_PIN, 0);
|
---|
[8a64320e] | 254 | if (rc != EOK) {
|
---|
[59fa7ab] | 255 | usb_log_error("Failed to init bring up GPIO led.\n");
|
---|
| 256 | return rc;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return EOK;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[1dcc0b9] | 262 | static int hw_activate_phy(ar9271_t *ar9271)
|
---|
| 263 | {
|
---|
| 264 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_ACTIVE, 1);
|
---|
| 265 | udelay(1000);
|
---|
| 266 |
|
---|
| 267 | return EOK;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[8a64320e] | 270 | static int hw_set_operating_mode(ar9271_t *ar9271,
|
---|
| 271 | ieee80211_operating_mode_t op_mode)
|
---|
[59fa7ab] | 272 | {
|
---|
| 273 | uint32_t set_bit = 0x10000000;
|
---|
| 274 |
|
---|
| 275 | switch(op_mode) {
|
---|
[8a64320e] | 276 | case IEEE80211_OPMODE_ADHOC:
|
---|
| 277 | set_bit |= AR9271_OPMODE_ADHOC_MASK;
|
---|
| 278 | wmi_reg_set_bit(ar9271->htc_device, AR9271_CONFIG,
|
---|
| 279 | AR9271_CONFIG_ADHOC);
|
---|
| 280 | break;
|
---|
| 281 | case IEEE80211_OPMODE_MESH:
|
---|
| 282 | case IEEE80211_OPMODE_AP:
|
---|
| 283 | set_bit |= AR9271_OPMODE_STATION_AP_MASK;
|
---|
| 284 | case IEEE80211_OPMODE_STATION:
|
---|
| 285 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_CONFIG,
|
---|
| 286 | AR9271_CONFIG_ADHOC);
|
---|
[59fa7ab] | 287 | }
|
---|
| 288 |
|
---|
| 289 | wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_STATION_ID1,
|
---|
[8a64320e] | 290 | set_bit, AR9271_OPMODE_STATION_AP_MASK | AR9271_OPMODE_ADHOC_MASK);
|
---|
[59fa7ab] | 291 |
|
---|
| 292 | ieee80211_report_current_op_mode(ar9271->ieee80211_dev, op_mode);
|
---|
| 293 |
|
---|
| 294 | return EOK;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | static int hw_reset_operating_mode(ar9271_t *ar9271)
|
---|
| 298 | {
|
---|
| 299 | int rc = hw_set_operating_mode(ar9271, IEEE80211_OPMODE_STATION);
|
---|
[8a64320e] | 300 | if (rc != EOK) {
|
---|
[59fa7ab] | 301 | usb_log_error("Failed to set opmode to station.\n");
|
---|
| 302 | return rc;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | static int hw_noise_floor_calibration(ar9271_t *ar9271)
|
---|
| 309 | {
|
---|
[1dcc0b9] | 310 | uint32_t value;
|
---|
| 311 | wmi_reg_read(ar9271->htc_device, AR9271_PHY_CAL, &value);
|
---|
[8a64320e] | 312 |
|
---|
| 313 | value &= 0xfffffe00;
|
---|
| 314 | value |= (((uint32_t) AR9271_CALIB_NOMINAL_VALUE_2GHZ << 1) & 0x1ff);
|
---|
| 315 |
|
---|
[1dcc0b9] | 316 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_CAL, value);
|
---|
| 317 |
|
---|
[8a64320e] | 318 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 319 | AR9271_AGC_CONTROL_NF_CALIB_EN);
|
---|
[1dcc0b9] | 320 |
|
---|
[8a64320e] | 321 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 322 | AR9271_AGC_CONTROL_NF_NOT_UPDATE);
|
---|
[1dcc0b9] | 323 |
|
---|
[8a64320e] | 324 | wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 325 | AR9271_AGC_CONTROL_NF_CALIB);
|
---|
[1dcc0b9] | 326 |
|
---|
[8a64320e] | 327 | int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL,
|
---|
| 328 | AR9271_AGC_CONTROL_NF_CALIB, 0);
|
---|
| 329 | if (rc != EOK) {
|
---|
[1dcc0b9] | 330 | usb_log_error("Failed to wait for NF calibration.\n");
|
---|
| 331 | return rc;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[8a64320e] | 334 | wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 335 | AR9271_AGC_CONTROL_NF_CALIB_EN);
|
---|
[59fa7ab] | 336 |
|
---|
[8a64320e] | 337 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 338 | AR9271_AGC_CONTROL_NF_NOT_UPDATE);
|
---|
[59fa7ab] | 339 |
|
---|
[8a64320e] | 340 | wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
| 341 | AR9271_AGC_CONTROL_NF_CALIB);
|
---|
[59fa7ab] | 342 |
|
---|
| 343 | return EOK;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | static int hw_set_freq(ar9271_t *ar9271, uint16_t freq)
|
---|
| 347 | {
|
---|
| 348 | /* Not supported channel frequency. */
|
---|
[8a64320e] | 349 | if ((freq < IEEE80211_FIRST_FREQ) || (freq > IEEE80211_MAX_FREQ))
|
---|
[59fa7ab] | 350 | return EINVAL;
|
---|
| 351 |
|
---|
| 352 | /* Not supported channel frequency. */
|
---|
[8a64320e] | 353 | if ((freq - IEEE80211_FIRST_FREQ) % IEEE80211_CHANNEL_GAP != 0)
|
---|
[59fa7ab] | 354 | return EINVAL;
|
---|
| 355 |
|
---|
| 356 | uint32_t tx_control;
|
---|
| 357 | wmi_reg_read(ar9271->htc_device, AR9271_PHY_CCK_TX_CTRL, &tx_control);
|
---|
| 358 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_CCK_TX_CTRL,
|
---|
[8a64320e] | 359 | tx_control & ~AR9271_PHY_CCK_TX_CTRL_JAPAN);
|
---|
[59fa7ab] | 360 |
|
---|
| 361 | /* Some magic here. */
|
---|
| 362 | uint32_t synth_ctl;
|
---|
| 363 | wmi_reg_read(ar9271->htc_device, AR9271_PHY_SYNTH_CONTROL, &synth_ctl);
|
---|
[8a64320e] | 364 | synth_ctl &= 0xc0000000;
|
---|
[59fa7ab] | 365 | uint32_t channel_select = (freq * 0x10000) / 15;
|
---|
| 366 | synth_ctl = synth_ctl | (1 << 29) | (1 << 28) | channel_select;
|
---|
| 367 |
|
---|
| 368 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_SYNTH_CONTROL, synth_ctl);
|
---|
| 369 |
|
---|
| 370 | ieee80211_report_current_freq(ar9271->ieee80211_dev, freq);
|
---|
| 371 |
|
---|
| 372 | return EOK;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | int hw_freq_switch(ar9271_t *ar9271, uint16_t freq)
|
---|
| 376 | {
|
---|
| 377 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_RFBUS_KILL, 0x1);
|
---|
| 378 |
|
---|
| 379 | int rc = hw_read_wait(ar9271, AR9271_PHY_RFBUS_GRANT, 0x1, 0x1);
|
---|
[8a64320e] | 380 | if (rc != EOK) {
|
---|
[59fa7ab] | 381 | usb_log_error("Failed to kill RF bus.\n");
|
---|
| 382 | return rc;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | rc = hw_set_freq(ar9271, freq);
|
---|
[8a64320e] | 386 | if (rc != EOK) {
|
---|
[59fa7ab] | 387 | usb_log_error("Failed to HW set frequency.\n");
|
---|
| 388 | return rc;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[1dcc0b9] | 391 | rc = hw_activate_phy(ar9271);
|
---|
[8a64320e] | 392 | if (rc != EOK) {
|
---|
[1dcc0b9] | 393 | usb_log_error("Failed to activate physical layer.\n");
|
---|
| 394 | return rc;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[59fa7ab] | 397 | udelay(1000);
|
---|
| 398 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_RFBUS_KILL, 0x0);
|
---|
| 399 |
|
---|
| 400 | rc = hw_noise_floor_calibration(ar9271);
|
---|
[8a64320e] | 401 | if (rc != EOK) {
|
---|
[59fa7ab] | 402 | usb_log_error("Failed to do NF calibration.\n");
|
---|
| 403 | return rc;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | return EOK;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[1dcc0b9] | 409 | int hw_set_rx_filter(ar9271_t *ar9271, bool assoc)
|
---|
[59fa7ab] | 410 | {
|
---|
[1dcc0b9] | 411 | uint32_t additional_bits = 0;
|
---|
| 412 |
|
---|
[8a64320e] | 413 | if (assoc)
|
---|
[1dcc0b9] | 414 | additional_bits |= AR9271_RX_FILTER_MYBEACON;
|
---|
[8a64320e] | 415 | else
|
---|
[1dcc0b9] | 416 | additional_bits |= AR9271_RX_FILTER_BEACON;
|
---|
[59fa7ab] | 417 |
|
---|
[8a64320e] | 418 | uint32_t filter_bits = AR9271_RX_FILTER_UNI |
|
---|
| 419 | AR9271_RX_FILTER_MULTI | AR9271_RX_FILTER_BROAD |
|
---|
| 420 | additional_bits;
|
---|
[59fa7ab] | 421 |
|
---|
| 422 | wmi_reg_write(ar9271->htc_device, AR9271_RX_FILTER, filter_bits);
|
---|
| 423 |
|
---|
| 424 | return EOK;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[1dcc0b9] | 427 | int hw_set_bssid(ar9271_t *ar9271)
|
---|
| 428 | {
|
---|
| 429 | ieee80211_dev_t *ieee80211_dev = ar9271->ieee80211_dev;
|
---|
| 430 |
|
---|
| 431 | nic_address_t bssid;
|
---|
| 432 | ieee80211_query_bssid(ieee80211_dev, &bssid);
|
---|
| 433 |
|
---|
| 434 | uint32_t *first_4bytes = (uint32_t *) &bssid.address;
|
---|
| 435 | uint16_t *last_2bytes = (uint16_t *) &bssid.address[4];
|
---|
| 436 |
|
---|
[8a64320e] | 437 | wmi_reg_write(ar9271->htc_device, AR9271_BSSID0,
|
---|
| 438 | uint32_t_le2host(*first_4bytes));
|
---|
[1dcc0b9] | 439 |
|
---|
[8a64320e] | 440 | wmi_reg_write(ar9271->htc_device, AR9271_BSSID1,
|
---|
| 441 | uint16_t_le2host(*last_2bytes) |
|
---|
| 442 | ((ieee80211_get_aid(ieee80211_dev) & 0x3fff) << 16));
|
---|
[1dcc0b9] | 443 |
|
---|
| 444 | return EOK;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[59fa7ab] | 447 | int hw_rx_init(ar9271_t *ar9271)
|
---|
| 448 | {
|
---|
[8a64320e] | 449 | wmi_reg_write(ar9271->htc_device, AR9271_COMMAND,
|
---|
| 450 | AR9271_COMMAND_RX_ENABLE);
|
---|
[59fa7ab] | 451 |
|
---|
[1dcc0b9] | 452 | int rc = hw_set_rx_filter(ar9271, false);
|
---|
[8a64320e] | 453 | if (rc != EOK) {
|
---|
[59fa7ab] | 454 | usb_log_error("Failed to set RX filtering.\n");
|
---|
| 455 | return rc;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | wmi_reg_write(ar9271->htc_device, AR9271_MULTICAST_FILTER1, ~0);
|
---|
| 459 | wmi_reg_write(ar9271->htc_device, AR9271_MULTICAST_FILTER2, ~0);
|
---|
| 460 |
|
---|
| 461 | /* Disable RX blocking. */
|
---|
| 462 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_DIAG, (0x20 | 0x02000000));
|
---|
| 463 |
|
---|
| 464 | return EOK;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | static int hw_init_pll(ar9271_t *ar9271)
|
---|
| 468 | {
|
---|
| 469 | /* Some magic here (set for 2GHz channels). But VERY important :-) */
|
---|
[8a64320e] | 470 | uint32_t pll = (0x5 << 10) | 0x2c;
|
---|
[59fa7ab] | 471 |
|
---|
| 472 | wmi_reg_write(ar9271->htc_device, AR9271_RTC_PLL_CONTROL, pll);
|
---|
| 473 |
|
---|
[1dcc0b9] | 474 | wmi_reg_write(ar9271->htc_device, AR9271_RTC_SLEEP_CLOCK,
|
---|
[8a64320e] | 475 | AR9271_RTC_SLEEP_CLOCK_FORCE_DERIVED);
|
---|
[1dcc0b9] | 476 | wmi_reg_set_bit(ar9271->htc_device, AR9271_RTC_FORCE_WAKE,
|
---|
[8a64320e] | 477 | AR9271_RTC_FORCE_WAKE_ENABLE);
|
---|
[1dcc0b9] | 478 |
|
---|
[59fa7ab] | 479 | return EOK;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[1dcc0b9] | 482 | static void hw_set_init_values(ar9271_t *ar9271)
|
---|
[59fa7ab] | 483 | {
|
---|
[8a64320e] | 484 | uint32_t reg_offset;
|
---|
| 485 | uint32_t reg_value;
|
---|
[59fa7ab] | 486 |
|
---|
[8a64320e] | 487 | size_t size = ARRAY_SIZE(ar9271_2g_mode_array);
|
---|
[59fa7ab] | 488 |
|
---|
[8a64320e] | 489 | for (size_t i = 0; i < size; i++) {
|
---|
[59fa7ab] | 490 | reg_offset = ar9271_2g_mode_array[i][0];
|
---|
| 491 | reg_value = ar9271_2g_mode_array[i][1];
|
---|
| 492 | wmi_reg_write(ar9271->htc_device, reg_offset, reg_value);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | size = ARRAY_SIZE(ar9271_2g_tx_array);
|
---|
| 496 |
|
---|
[8a64320e] | 497 | for (size_t i = 0; i < size; i++) {
|
---|
[59fa7ab] | 498 | reg_offset = ar9271_2g_tx_array[i][0];
|
---|
| 499 | reg_value = ar9271_2g_tx_array[i][1];
|
---|
| 500 | wmi_reg_write(ar9271->htc_device, reg_offset, reg_value);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | size = ARRAY_SIZE(ar9271_init_array);
|
---|
| 504 |
|
---|
[8a64320e] | 505 | for (size_t i = 0; i < size; i++) {
|
---|
[59fa7ab] | 506 | reg_offset = ar9271_init_array[i][0];
|
---|
| 507 | reg_value = ar9271_init_array[i][1];
|
---|
| 508 | wmi_reg_write(ar9271->htc_device, reg_offset, reg_value);
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | static int hw_calibration(ar9271_t *ar9271)
|
---|
| 513 | {
|
---|
| 514 | wmi_reg_set_bit(ar9271->htc_device, AR9271_CARRIER_LEAK_CONTROL,
|
---|
[8a64320e] | 515 | AR9271_CARRIER_LEAK_CALIB);
|
---|
[59fa7ab] | 516 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_ADC_CONTROL,
|
---|
[8a64320e] | 517 | AR9271_ADC_CONTROL_OFF_PWDADC);
|
---|
[59fa7ab] | 518 | wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
[8a64320e] | 519 | AR9271_AGC_CONTROL_TX_CALIB);
|
---|
[59fa7ab] | 520 | wmi_reg_set_bit(ar9271->htc_device, AR9271_PHY_TPCRG1,
|
---|
[8a64320e] | 521 | AR9271_PHY_TPCRG1_PD_CALIB);
|
---|
[59fa7ab] | 522 | wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
[8a64320e] | 523 | AR9271_AGC_CONTROL_CALIB);
|
---|
[59fa7ab] | 524 |
|
---|
[8a64320e] | 525 | int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL,
|
---|
| 526 | AR9271_AGC_CONTROL_CALIB, 0);
|
---|
| 527 | if (rc != EOK) {
|
---|
[59fa7ab] | 528 | usb_log_error("Failed to wait on calibrate completion.\n");
|
---|
| 529 | return rc;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | wmi_reg_set_bit(ar9271->htc_device, AR9271_ADC_CONTROL,
|
---|
[8a64320e] | 533 | AR9271_ADC_CONTROL_OFF_PWDADC);
|
---|
[59fa7ab] | 534 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_CARRIER_LEAK_CONTROL,
|
---|
[8a64320e] | 535 | AR9271_CARRIER_LEAK_CALIB);
|
---|
[59fa7ab] | 536 | wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
|
---|
[8a64320e] | 537 | AR9271_AGC_CONTROL_TX_CALIB);
|
---|
[59fa7ab] | 538 |
|
---|
| 539 | return EOK;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[8a64320e] | 542 | int hw_reset(ar9271_t *ar9271)
|
---|
[59fa7ab] | 543 | {
|
---|
| 544 | /* Set physical layer as deactivated. */
|
---|
| 545 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_ACTIVE, 0);
|
---|
| 546 |
|
---|
| 547 | if(ar9271->starting_up) {
|
---|
[8a64320e] | 548 | wmi_reg_write(ar9271->htc_device,
|
---|
| 549 | AR9271_RESET_POWER_DOWN_CONTROL,
|
---|
| 550 | AR9271_RADIO_RF_RESET);
|
---|
| 551 |
|
---|
[59fa7ab] | 552 | udelay(50);
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | /* Cold reset when RX is enabled. */
|
---|
| 556 | uint32_t config_reg;
|
---|
| 557 | wmi_reg_read(ar9271->htc_device, AR9271_COMMAND, &config_reg);
|
---|
[8a64320e] | 558 | if (config_reg & AR9271_COMMAND_RX_ENABLE)
|
---|
[59fa7ab] | 559 | hw_set_reset(ar9271, true);
|
---|
| 560 |
|
---|
| 561 | int rc = hw_init_pll(ar9271);
|
---|
[8a64320e] | 562 | if (rc != EOK) {
|
---|
[59fa7ab] | 563 | usb_log_error("Failed to init PLL.\n");
|
---|
| 564 | return rc;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
| 567 | udelay(500);
|
---|
| 568 |
|
---|
[8a64320e] | 569 | wmi_reg_write(ar9271->htc_device, AR9271_CLOCK_CONTROL,
|
---|
| 570 | AR9271_MAX_CPU_CLOCK);
|
---|
[59fa7ab] | 571 |
|
---|
| 572 | udelay(100);
|
---|
| 573 |
|
---|
[8a64320e] | 574 | if (ar9271->starting_up) {
|
---|
| 575 | wmi_reg_write(ar9271->htc_device,
|
---|
| 576 | AR9271_RESET_POWER_DOWN_CONTROL,
|
---|
| 577 | AR9271_GATE_MAC_CONTROL);
|
---|
| 578 |
|
---|
[59fa7ab] | 579 | udelay(50);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[1dcc0b9] | 582 | hw_set_init_values(ar9271);
|
---|
[59fa7ab] | 583 |
|
---|
| 584 | /* Set physical layer mode. */
|
---|
[8a64320e] | 585 | wmi_reg_write(ar9271->htc_device, AR9271_PHY_MODE,
|
---|
| 586 | AR9271_PHY_MODE_DYNAMIC);
|
---|
[59fa7ab] | 587 |
|
---|
| 588 | /* Reset device operating mode. */
|
---|
| 589 | rc = hw_reset_operating_mode(ar9271);
|
---|
[8a64320e] | 590 | if (rc != EOK) {
|
---|
[59fa7ab] | 591 | usb_log_error("Failed to reset operating mode.\n");
|
---|
| 592 | return rc;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /* Set initial channel frequency. */
|
---|
| 596 | rc = hw_set_freq(ar9271, IEEE80211_FIRST_FREQ);
|
---|
[8a64320e] | 597 | if (rc != EOK) {
|
---|
[59fa7ab] | 598 | usb_log_error("Failed to set channel.\n");
|
---|
| 599 | return rc;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | /* Initialize transmission queues. */
|
---|
[8a64320e] | 603 | for (unsigned int i = 0; i < AR9271_QUEUES_COUNT; i++) {
|
---|
| 604 | wmi_reg_write(ar9271->htc_device,
|
---|
| 605 | AR9271_QUEUE_BASE_MASK + (i << 2), 1 << i);
|
---|
[59fa7ab] | 606 | }
|
---|
| 607 |
|
---|
| 608 | /* Activate physical layer. */
|
---|
| 609 | rc = hw_activate_phy(ar9271);
|
---|
[8a64320e] | 610 | if (rc != EOK) {
|
---|
[59fa7ab] | 611 | usb_log_error("Failed to activate physical layer.\n");
|
---|
| 612 | return rc;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 | /* Calibration. */
|
---|
| 616 | rc = hw_calibration(ar9271);
|
---|
[8a64320e] | 617 | if (rc != EOK) {
|
---|
[59fa7ab] | 618 | usb_log_error("Failed to calibrate device.\n");
|
---|
| 619 | return rc;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | rc = hw_noise_floor_calibration(ar9271);
|
---|
[8a64320e] | 623 | if (rc != EOK) {
|
---|
[59fa7ab] | 624 | usb_log_error("Failed to calibrate noise floor.\n");
|
---|
| 625 | return rc;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /* Byteswap TX and RX data buffer words. */
|
---|
| 629 | wmi_reg_write(ar9271->htc_device, AR9271_CONFIG, 0xA);
|
---|
| 630 |
|
---|
| 631 | return EOK;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[8a64320e] | 634 | /** Initialize hardware of AR9271 device.
|
---|
| 635 | *
|
---|
[59fa7ab] | 636 | * @param ar9271 Device structure.
|
---|
[8a64320e] | 637 | *
|
---|
[59fa7ab] | 638 | * @return EOK if succeed, negative error code otherwise.
|
---|
| 639 | */
|
---|
| 640 | int hw_init(ar9271_t *ar9271)
|
---|
| 641 | {
|
---|
| 642 | int rc = hw_init_proc(ar9271);
|
---|
[8a64320e] | 643 | if (rc != EOK) {
|
---|
[59fa7ab] | 644 | usb_log_error("Failed to HW reset device.\n");
|
---|
| 645 | return rc;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | rc = hw_init_led(ar9271);
|
---|
[8a64320e] | 649 | if (rc != EOK) {
|
---|
[59fa7ab] | 650 | usb_log_error("Failed to HW init led.\n");
|
---|
| 651 | return rc;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | usb_log_info("HW initialization finished successfully.\n");
|
---|
| 655 |
|
---|
| 656 | return EOK;
|
---|
[8a64320e] | 657 | }
|
---|