[1/3] hwrng: imx-rngc - use bitfield macros to read fifo level
Commit Message
Use the mechanism from bitfield.h to read the fifo level field in the
status register. This makes the code a tiny bit simpler.
Signed-off-by: Martin Kaiser <martin@kaiser.cx>
---
drivers/char/hw_random/imx-rngc.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
Comments
On Thu, Apr 27, 2023 at 08:53:55PM +0200, Martin Kaiser wrote:
>
> @@ -133,9 +133,7 @@ static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
> break;
>
> /* how many random numbers are in FIFO? [0-16] */
> - level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
> - RNGC_STATUS_FIFO_LEVEL_SHIFT;
> -
> + level = FIELD_GET(RNGC_STATUS_FIFO_LEVEL, status);
Wouldn't it be simpler to just get rid of the shift?
Thanks,
Thus wrote Herbert Xu (herbert@gondor.apana.org.au):
> On Thu, Apr 27, 2023 at 08:53:55PM +0200, Martin Kaiser wrote:
> > @@ -133,9 +133,7 @@ static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
> > break;
> > /* how many random numbers are in FIFO? [0-16] */
> > - level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
> > - RNGC_STATUS_FIFO_LEVEL_SHIFT;
> > -
> > + level = FIELD_GET(RNGC_STATUS_FIFO_LEVEL, status);
> Wouldn't it be simpler to just get rid of the shift?
Yes, we're only checking that level > 0, there's no need for the shift.
I'll submit a new version of the patch.
Thanks,
Martin
@@ -17,6 +17,7 @@
#include <linux/hw_random.h>
#include <linux/completion.h>
#include <linux/io.h>
+#include <linux/bitfield.h>
#define RNGC_VER_ID 0x0000
#define RNGC_COMMAND 0x0004
@@ -44,8 +45,7 @@
#define RNGC_CTRL_AUTO_SEED 0x00000010
#define RNGC_STATUS_ERROR 0x00010000
-#define RNGC_STATUS_FIFO_LEVEL_MASK 0x00000f00
-#define RNGC_STATUS_FIFO_LEVEL_SHIFT 8
+#define RNGC_STATUS_FIFO_LEVEL GENMASK(11, 8)
#define RNGC_STATUS_SEED_DONE 0x00000020
#define RNGC_STATUS_ST_DONE 0x00000010
@@ -133,9 +133,7 @@ static int imx_rngc_read(struct hwrng *rng, void *data, size_t max, bool wait)
break;
/* how many random numbers are in FIFO? [0-16] */
- level = (status & RNGC_STATUS_FIFO_LEVEL_MASK) >>
- RNGC_STATUS_FIFO_LEVEL_SHIFT;
-
+ level = FIELD_GET(RNGC_STATUS_FIFO_LEVEL, status);
if (level) {
/* retrieve a random number from FIFO */
*(u32 *)data = readl(rngc->base + RNGC_FIFO);