X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=include%2Fbitfield.h;h=d3ceeedccda681b884f63422d0a49dc4804562c0;hb=6786ce1ce14feb4d02854a0c04bc0cce505be46e;hp=ec4815c8e05179508c00cce24655acd7382ba62e;hpb=eeb72e67619b98d2502fe634a3a5d9953de92ad0;p=platform%2Fkernel%2Fu-boot.git diff --git a/include/bitfield.h b/include/bitfield.h index ec4815c..d3ceeed 100644 --- a/include/bitfield.h +++ b/include/bitfield.h @@ -1,7 +1,6 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ /* * Copyright 2013 Broadcom Corporation. - * - * SPDX-License-Identifier: GPL-2.0+ */ /* @@ -27,10 +26,17 @@ * old = bitfield_extract(old_reg_val, 10, 5); * new_reg_val = bitfield_replace(old_reg_val, 10, 5, new); * + * or + * + * mask = bitfield_mask(10, 5); + * old = bitfield_extract_by_mask(old_reg_val, mask); + * new_reg_val = bitfield_replace_by_mask(old_reg_val, mask, new); + * * The numbers 10 and 5 could for example come from data * tables which describe all bitfields in all registers. */ +#include #include /* Produces a mask of set bits covering a range of a uint value */ @@ -54,5 +60,31 @@ static inline uint bitfield_replace(uint reg_val, uint shift, uint width, { uint mask = bitfield_mask(shift, width); - return (reg_val & ~mask) | (bitfield_val << shift); + return (reg_val & ~mask) | ((bitfield_val << shift) & mask); +} + +/* Produces a shift of the bitfield given a mask */ +static inline uint bitfield_shift(uint mask) +{ + return mask ? ffs(mask) - 1 : 0; +} + +/* Extract the value of a bitfield found within a given register value */ +static inline uint bitfield_extract_by_mask(uint reg_val, uint mask) +{ + uint shift = bitfield_shift(mask); + + return (reg_val & mask) >> shift; +} + +/* + * Replace the value of a bitfield found within a given register value + * Returns the newly modified uint value with the replaced field. + */ +static inline uint bitfield_replace_by_mask(uint reg_val, uint mask, + uint bitfield_val) +{ + uint shift = bitfield_shift(mask); + + return (reg_val & ~mask) | ((bitfield_val << shift) & mask); }