xref: /openbmc/linux/sound/soc/sof/iomem-utils.c (revision 7559e757)
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. All rights reserved.
7 //
8 // Author: Keyon Jie <yang.jie@linux.intel.com>
9 //
10 
11 #include <linux/io-64-nonatomic-lo-hi.h>
12 #include <linux/platform_device.h>
13 #include <asm/unaligned.h>
14 #include <sound/soc.h>
15 #include <sound/sof.h>
16 #include "sof-priv.h"
17 #include "ops.h"
18 
19 /*
20  * Register IO
21  *
22  * The sof_io_xyz() wrappers are typically referenced in snd_sof_dsp_ops
23  * structures and cannot be inlined.
24  */
25 
26 void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value)
27 {
28 	writel(value, addr);
29 }
30 EXPORT_SYMBOL(sof_io_write);
31 
32 u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr)
33 {
34 	return readl(addr);
35 }
36 EXPORT_SYMBOL(sof_io_read);
37 
38 void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
39 {
40 	writeq(value, addr);
41 }
42 EXPORT_SYMBOL(sof_io_write64);
43 
44 u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
45 {
46 	return readq(addr);
47 }
48 EXPORT_SYMBOL(sof_io_read64);
49 
50 /*
51  * IPC Mailbox IO
52  */
53 
54 void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
55 		       void *message, size_t bytes)
56 {
57 	void __iomem *dest = sdev->bar[sdev->mailbox_bar] + offset;
58 
59 	memcpy_toio(dest, message, bytes);
60 }
61 EXPORT_SYMBOL(sof_mailbox_write);
62 
63 void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
64 		      void *message, size_t bytes)
65 {
66 	void __iomem *src = sdev->bar[sdev->mailbox_bar] + offset;
67 
68 	memcpy_fromio(message, src, bytes);
69 }
70 EXPORT_SYMBOL(sof_mailbox_read);
71 
72 /*
73  * Memory copy.
74  */
75 
76 int sof_block_write(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
77 		    u32 offset, void *src, size_t size)
78 {
79 	int bar = snd_sof_dsp_get_bar_index(sdev, blk_type);
80 	const u8 *src_byte = src;
81 	void __iomem *dest;
82 	u32 affected_mask;
83 	u32 tmp;
84 	int m, n;
85 
86 	if (bar < 0)
87 		return bar;
88 
89 	dest = sdev->bar[bar] + offset;
90 
91 	m = size / 4;
92 	n = size % 4;
93 
94 	/* __iowrite32_copy use 32bit size values so divide by 4 */
95 	__iowrite32_copy(dest, src, m);
96 
97 	if (n) {
98 		affected_mask = (1 << (8 * n)) - 1;
99 
100 		/* first read the 32bit data of dest, then change affected
101 		 * bytes, and write back to dest. For unaffected bytes, it
102 		 * should not be changed
103 		 */
104 		tmp = ioread32(dest + m * 4);
105 		tmp &= ~affected_mask;
106 
107 		tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
108 		iowrite32(tmp, dest + m * 4);
109 	}
110 
111 	return 0;
112 }
113 EXPORT_SYMBOL(sof_block_write);
114 
115 int sof_block_read(struct snd_sof_dev *sdev, enum snd_sof_fw_blk_type blk_type,
116 		   u32 offset, void *dest, size_t size)
117 {
118 	int bar = snd_sof_dsp_get_bar_index(sdev, blk_type);
119 
120 	if (bar < 0)
121 		return bar;
122 
123 	memcpy_fromio(dest, sdev->bar[bar] + offset, size);
124 
125 	return 0;
126 }
127 EXPORT_SYMBOL(sof_block_read);
128