[66b3e0b] | 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 <usb/debug.h>
|
---|
| 36 | #include <unistd.h>
|
---|
| 37 |
|
---|
| 38 | #include "hw.h"
|
---|
| 39 | #include "wmi.h"
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Try to wait for register value repeatedly until timeout defined by device
|
---|
| 43 | * is reached.
|
---|
| 44 | *
|
---|
| 45 | * @param ar9271 Device structure.
|
---|
| 46 | * @param offset Registry offset (address) to be read.
|
---|
| 47 | * @param mask Mask to apply on read result.
|
---|
| 48 | * @param value Required value we are waiting for.
|
---|
| 49 | *
|
---|
| 50 | * @return EOK if succeed, ETIMEOUT on timeout, negative error code otherwise.
|
---|
| 51 | */
|
---|
| 52 | static int hw_read_wait(ar9271_t *ar9271, uint32_t offset, uint32_t mask,
|
---|
| 53 | uint32_t value)
|
---|
| 54 | {
|
---|
| 55 | uint32_t result;
|
---|
| 56 |
|
---|
| 57 | for(size_t i = 0; i < HW_WAIT_LOOPS; i++) {
|
---|
| 58 | udelay(HW_WAIT_TIME_US);
|
---|
| 59 |
|
---|
| 60 | int rc = wmi_reg_read(ar9271->htc_device, offset, &result);
|
---|
| 61 | if(rc != EOK) {
|
---|
| 62 | usb_log_debug("Failed to read register. Error %d\n",
|
---|
| 63 | rc);
|
---|
| 64 | continue;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if((result & mask) == value) {
|
---|
| 68 | return EOK;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | return ETIMEOUT;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * Hardware reset of AR9271 device.
|
---|
| 77 | *
|
---|
| 78 | * @param ar9271 Device structure.
|
---|
| 79 | *
|
---|
| 80 | * @return EOK if succeed, negative error code otherwise.
|
---|
| 81 | */
|
---|
| 82 | int hw_reset(ar9271_t *ar9271)
|
---|
| 83 | {
|
---|
| 84 | reg_buffer_t buffer[] = {
|
---|
| 85 | {
|
---|
| 86 | .offset = AR9271_RTC_FORCE_WAKE,
|
---|
| 87 | .value = AR9271_RTC_FORCE_WAKE_ENABLE |
|
---|
| 88 | AR9271_RTC_FORCE_WAKE_ON_INT
|
---|
| 89 | },
|
---|
| 90 | {
|
---|
| 91 | .offset = AR9271_RC,
|
---|
| 92 | .value = AR9271_RC_AHB
|
---|
| 93 | },
|
---|
| 94 | {
|
---|
| 95 | .offset = AR9271_RTC_RESET,
|
---|
| 96 | .value = 0
|
---|
| 97 | }
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | int rc = wmi_reg_buffer_write(ar9271->htc_device, buffer,
|
---|
| 101 | sizeof(buffer) / sizeof(reg_buffer_t));
|
---|
| 102 | if(rc != EOK) {
|
---|
| 103 | usb_log_error("Failed to set RT FORCE WAKE register.\n");
|
---|
| 104 | return rc;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | udelay(2);
|
---|
| 108 |
|
---|
| 109 | rc = wmi_reg_write(ar9271->htc_device, AR9271_RC, 0);
|
---|
| 110 | if(rc != EOK) {
|
---|
| 111 | usb_log_error("Failed to reset MAC AHB register.\n");
|
---|
| 112 | return rc;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | rc = wmi_reg_write(ar9271->htc_device, AR9271_RTC_RESET, 1);
|
---|
| 116 | if(rc != EOK) {
|
---|
| 117 | usb_log_error("Failed to bring up RTC register.\n");
|
---|
| 118 | return rc;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | rc = hw_read_wait(ar9271,
|
---|
| 122 | AR9271_RTC_STATUS,
|
---|
| 123 | AR9271_RTC_STATUS_MASK,
|
---|
| 124 | AR9271_RTC_STATUS_ON);
|
---|
| 125 | if(rc != EOK) {
|
---|
| 126 | usb_log_error("Failed to read wake up RTC register.\n");
|
---|
| 127 | return rc;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Initialize hardware of AR9271 device.
|
---|
| 135 | *
|
---|
| 136 | * @param ar9271 Device structure.
|
---|
| 137 | *
|
---|
| 138 | * @return EOK if succeed, negative error code otherwise.
|
---|
| 139 | */
|
---|
| 140 | int hw_init(ar9271_t *ar9271)
|
---|
| 141 | {
|
---|
| 142 | int rc = hw_reset(ar9271);
|
---|
| 143 | if(rc != EOK) {
|
---|
| 144 | usb_log_error("Failed to HW reset device.\n");
|
---|
| 145 | return rc;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | usb_log_info("HW initialization finished successfully.\n");
|
---|
| 149 |
|
---|
| 150 | return EOK;
|
---|
| 151 | } |
---|