aq_hw_utils.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * aQuantia Corporation Network Driver
  3. * Copyright (C) 2014-2017 aQuantia Corporation. All rights reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. /* File aq_hw_utils.c: Definitions of helper functions used across
  10. * hardware layer.
  11. */
  12. #include "aq_hw_utils.h"
  13. #include "aq_hw.h"
  14. void aq_hw_write_reg_bit(struct aq_hw_s *aq_hw, u32 addr, u32 msk,
  15. u32 shift, u32 val)
  16. {
  17. if (msk ^ ~0) {
  18. u32 reg_old, reg_new;
  19. reg_old = aq_hw_read_reg(aq_hw, addr);
  20. reg_new = (reg_old & (~msk)) | (val << shift);
  21. if (reg_old != reg_new)
  22. aq_hw_write_reg(aq_hw, addr, reg_new);
  23. } else {
  24. aq_hw_write_reg(aq_hw, addr, val);
  25. }
  26. }
  27. u32 aq_hw_read_reg_bit(struct aq_hw_s *aq_hw, u32 addr, u32 msk, u32 shift)
  28. {
  29. return ((aq_hw_read_reg(aq_hw, addr) & msk) >> shift);
  30. }
  31. u32 aq_hw_read_reg(struct aq_hw_s *hw, u32 reg)
  32. {
  33. u32 value = readl(hw->mmio + reg);
  34. if ((~0U) == value && (~0U) == readl(hw->mmio + hw->not_ff_addr))
  35. aq_utils_obj_set(&hw->header.flags, AQ_HW_FLAG_ERR_UNPLUG);
  36. return value;
  37. }
  38. void aq_hw_write_reg(struct aq_hw_s *hw, u32 reg, u32 value)
  39. {
  40. writel(value, hw->mmio + reg);
  41. }
  42. int aq_hw_err_from_flags(struct aq_hw_s *hw)
  43. {
  44. int err = 0;
  45. if (aq_utils_obj_test(&hw->header.flags, AQ_HW_FLAG_ERR_UNPLUG)) {
  46. err = -ENXIO;
  47. goto err_exit;
  48. }
  49. if (aq_utils_obj_test(&hw->header.flags, AQ_HW_FLAG_ERR_HW)) {
  50. err = -EIO;
  51. goto err_exit;
  52. }
  53. err_exit:
  54. return err;
  55. }