]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - include/bitfield.h
spl: Pass the loader into spl_load_image()
[people/ms/u-boot.git] / include / bitfield.h
index b884c7460013faab98010d48b08952c77287683d..a59f3c279aadc3583a837e53acfe747f8a698d7a 100644 (file)
  * 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.
  */
@@ -56,3 +62,29 @@ static inline uint bitfield_replace(uint reg_val, uint shift, uint width,
 
        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);
+}