18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2015 Qualcomm Atheros, Inc. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any 58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 118c2ecf20Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 148c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/hw_random.h> 188c2ecf20Sopenharmony_ci#include <linux/kthread.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include "ath9k.h" 218c2ecf20Sopenharmony_ci#include "hw.h" 228c2ecf20Sopenharmony_ci#include "ar9003_phy.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define ATH9K_RNG_BUF_SIZE 320 258c2ecf20Sopenharmony_ci#define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 10) >> 5) /* quality: 10/32 */ 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic DECLARE_WAIT_QUEUE_HEAD(rng_queue); 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci int i, j; 328c2ecf20Sopenharmony_ci u32 v1, v2, rng_last = sc->rng_last; 338c2ecf20Sopenharmony_ci struct ath_hw *ah = sc->sc_ah; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci ath9k_ps_wakeup(sc); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1); 388c2ecf20Sopenharmony_ci REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5); 398c2ecf20Sopenharmony_ci REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, AR_PHY_TEST_CTL_RX_OBS_SEL, 0); 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci for (i = 0, j = 0; i < buf_size; i++) { 428c2ecf20Sopenharmony_ci v1 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff; 438c2ecf20Sopenharmony_ci v2 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci /* wait for data ready */ 468c2ecf20Sopenharmony_ci if (v1 && v2 && rng_last != v1 && v1 != v2 && v1 != 0xffff && 478c2ecf20Sopenharmony_ci v2 != 0xffff) 488c2ecf20Sopenharmony_ci buf[j++] = (v1 << 16) | v2; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci rng_last = v2; 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci ath9k_ps_restore(sc); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci sc->rng_last = rng_last; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return j << 2; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic u32 ath9k_rng_delay_get(u32 fail_stats) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci u32 delay; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci if (fail_stats < 100) 658c2ecf20Sopenharmony_ci delay = 10; 668c2ecf20Sopenharmony_ci else if (fail_stats < 105) 678c2ecf20Sopenharmony_ci delay = 1000; 688c2ecf20Sopenharmony_ci else 698c2ecf20Sopenharmony_ci delay = 10000; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci return delay; 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic int ath9k_rng_kthread(void *data) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci int bytes_read; 778c2ecf20Sopenharmony_ci struct ath_softc *sc = data; 788c2ecf20Sopenharmony_ci u32 *rng_buf; 798c2ecf20Sopenharmony_ci u32 delay, fail_stats = 0; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL); 828c2ecf20Sopenharmony_ci if (!rng_buf) 838c2ecf20Sopenharmony_ci goto out; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci while (!kthread_should_stop()) { 868c2ecf20Sopenharmony_ci bytes_read = ath9k_rng_data_read(sc, rng_buf, 878c2ecf20Sopenharmony_ci ATH9K_RNG_BUF_SIZE); 888c2ecf20Sopenharmony_ci if (unlikely(!bytes_read)) { 898c2ecf20Sopenharmony_ci delay = ath9k_rng_delay_get(++fail_stats); 908c2ecf20Sopenharmony_ci wait_event_interruptible_timeout(rng_queue, 918c2ecf20Sopenharmony_ci kthread_should_stop(), 928c2ecf20Sopenharmony_ci msecs_to_jiffies(delay)); 938c2ecf20Sopenharmony_ci continue; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci fail_stats = 0; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* sleep until entropy bits under write_wakeup_threshold */ 998c2ecf20Sopenharmony_ci add_hwgenerator_randomness((void *)rng_buf, bytes_read, 1008c2ecf20Sopenharmony_ci ATH9K_RNG_ENTROPY(bytes_read)); 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci kfree(rng_buf); 1048c2ecf20Sopenharmony_ciout: 1058c2ecf20Sopenharmony_ci sc->rng_task = NULL; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci return 0; 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_civoid ath9k_rng_start(struct ath_softc *sc) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct ath_hw *ah = sc->sc_ah; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (sc->rng_task) 1158c2ecf20Sopenharmony_ci return; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (!AR_SREV_9300_20_OR_LATER(ah)) 1188c2ecf20Sopenharmony_ci return; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng"); 1218c2ecf20Sopenharmony_ci if (IS_ERR(sc->rng_task)) 1228c2ecf20Sopenharmony_ci sc->rng_task = NULL; 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_civoid ath9k_rng_stop(struct ath_softc *sc) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci if (sc->rng_task) { 1288c2ecf20Sopenharmony_ci kthread_stop(sc->rng_task); 1298c2ecf20Sopenharmony_ci sc->rng_task = NULL; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci} 132