iomem-utils.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018-2022 Intel Corporation
  7. //
  8. // Author: Keyon Jie <yang.jie@linux.intel.com>
  9. //
  10. #include <linux/io-64-nonatomic-lo-hi.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/unaligned.h>
  13. #include <sound/soc.h>
  14. #include <sound/sof.h>
  15. #include "sof-priv.h"
  16. #include "ops.h"
  17. /*
  18. * Register IO
  19. *
  20. * The sof_io_xyz() wrappers are typically referenced in snd_sof_dsp_ops
  21. * structures and cannot be inlined.
  22. */
  23. void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value)
  24. {
  25. writel(value, addr);
  26. }
  27. EXPORT_SYMBOL(sof_io_write);
  28. u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr)
  29. {
  30. return readl(addr);
  31. }
  32. EXPORT_SYMBOL(sof_io_read);
  33. void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
  34. {
  35. writeq(value, addr);
  36. }
  37. EXPORT_SYMBOL(sof_io_write64);
  38. u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
  39. {
  40. return readq(addr);
  41. }
  42. EXPORT_SYMBOL(sof_io_read64);
  43. /*
  44. * IPC Mailbox IO
  45. */
  46. void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
  47. void *message, size_t bytes)
  48. {
  49. void __iomem *dest = sdev->bar[sdev->mailbox_bar] + offset;
  50. memcpy_toio(dest, message, bytes);
  51. }
  52. EXPORT_SYMBOL(sof_mailbox_write);
  53. void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
  54. void *message, size_t bytes)
  55. {
  56. void __iomem *src = sdev->bar[sdev->mailbox_bar] + offset;
  57. memcpy_fromio(message, src, bytes);
  58. }
  59. EXPORT_SYMBOL(sof_mailbox_read);
  60. /*
  61. * Memory copy.
  62. */
  63. int sof_block_write(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
  64. u32 offset, void *src, size_t size)
  65. {
  66. int bar = snd_sof_dsp_get_bar_index(sdev, blk_type);
  67. const u8 *src_byte = src;
  68. void __iomem *dest;
  69. u32 affected_mask;
  70. u32 tmp;
  71. int m, n;
  72. if (bar < 0)
  73. return bar;
  74. dest = sdev->bar[bar] + offset;
  75. m = size / 4;
  76. n = size % 4;
  77. /* __iowrite32_copy use 32bit size values so divide by 4 */
  78. __iowrite32_copy(dest, src, m);
  79. if (n) {
  80. affected_mask = (1 << (8 * n)) - 1;
  81. /* first read the 32bit data of dest, then change affected
  82. * bytes, and write back to dest. For unaffected bytes, it
  83. * should not be changed
  84. */
  85. tmp = ioread32(dest + m * 4);
  86. tmp &= ~affected_mask;
  87. tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
  88. iowrite32(tmp, dest + m * 4);
  89. }
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(sof_block_write);
  93. int sof_block_read(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
  94. u32 offset, void *dest, size_t size)
  95. {
  96. int bar = snd_sof_dsp_get_bar_index(sdev, blk_type);
  97. if (bar < 0)
  98. return bar;
  99. memcpy_fromio(dest, sdev->bar[bar] + offset, size);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(sof_block_read);