2 * Copyright (c) 2015 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/hw_random.h>
18 #include <linux/kthread.h>
22 #include "ar9003_phy.h"
24 #define ATH9K_RNG_BUF_SIZE 320
25 #define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 10) >> 5) /* quality: 10/32 */
27 static DECLARE_WAIT_QUEUE_HEAD(rng_queue
);
29 static int ath9k_rng_data_read(struct ath_softc
*sc
, u32
*buf
, u32 buf_size
)
32 u32 v1
, v2
, rng_last
= sc
->rng_last
;
33 struct ath_hw
*ah
= sc
->sc_ah
;
37 REG_RMW_FIELD(ah
, AR_PHY_TEST
, AR_PHY_TEST_BBB_OBS_SEL
, 1);
38 REG_CLR_BIT(ah
, AR_PHY_TEST
, AR_PHY_TEST_RX_OBS_SEL_BIT5
);
39 REG_RMW_FIELD(ah
, AR_PHY_TEST_CTL_STATUS
, AR_PHY_TEST_CTL_RX_OBS_SEL
, 0);
41 for (i
= 0, j
= 0; i
< buf_size
; i
++) {
42 v1
= REG_READ(ah
, AR_PHY_TST_ADC
) & 0xffff;
43 v2
= REG_READ(ah
, AR_PHY_TST_ADC
) & 0xffff;
45 /* wait for data ready */
46 if (v1
&& v2
&& rng_last
!= v1
&& v1
!= v2
&& v1
!= 0xffff &&
48 buf
[j
++] = (v1
<< 16) | v2
;
55 sc
->rng_last
= rng_last
;
60 static u32
ath9k_rng_delay_get(u32 fail_stats
)
66 else if (fail_stats
< 105)
74 static int ath9k_rng_kthread(void *data
)
77 struct ath_softc
*sc
= data
;
79 u32 delay
, fail_stats
= 0;
81 rng_buf
= kmalloc_array(ATH9K_RNG_BUF_SIZE
, sizeof(u32
), GFP_KERNEL
);
85 while (!kthread_should_stop()) {
86 bytes_read
= ath9k_rng_data_read(sc
, rng_buf
,
88 if (unlikely(!bytes_read
)) {
89 delay
= ath9k_rng_delay_get(++fail_stats
);
90 wait_event_interruptible_timeout(rng_queue
,
91 kthread_should_stop(),
92 msecs_to_jiffies(delay
));
98 /* sleep until entropy bits under write_wakeup_threshold */
99 add_hwgenerator_randomness((void *)rng_buf
, bytes_read
,
100 ATH9K_RNG_ENTROPY(bytes_read
));
110 void ath9k_rng_start(struct ath_softc
*sc
)
112 struct ath_hw
*ah
= sc
->sc_ah
;
117 if (!AR_SREV_9300_20_OR_LATER(ah
))
120 sc
->rng_task
= kthread_run(ath9k_rng_kthread
, sc
, "ath9k-hwrng");
121 if (IS_ERR(sc
->rng_task
))
125 void ath9k_rng_stop(struct ath_softc
*sc
)
128 kthread_stop(sc
->rng_task
);