1 // SPDX-License-Identifier: GPL-2.0-only
3 * Low-level I/O functions.
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
8 #include <linux/kernel.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
20 * About CONFIG_VMAP_STACK:
21 * When CONFIG_VMAP_STACK is enabled, it is not possible to run DMA on stack
22 * allocated data. Functions below that work with registers (aka functions
23 * ending with "32") automatically reallocate buffers with kmalloc. However,
24 * functions that work with arbitrary length buffers let's caller to handle
25 * memory location. In doubt, enable CONFIG_DEBUG_SG to detect badly located
29 static int read32(struct wfx_dev
*wdev
, int reg
, u32
*val
)
32 __le32
*tmp
= kmalloc(sizeof(u32
), GFP_KERNEL
);
34 *val
= ~0; // Never return undefined value
37 ret
= wdev
->hwbus_ops
->copy_from_io(wdev
->hwbus_priv
, reg
, tmp
,
40 *val
= le32_to_cpu(*tmp
);
43 dev_err(wdev
->dev
, "%s: bus communication error: %d\n",
48 static int write32(struct wfx_dev
*wdev
, int reg
, u32 val
)
51 __le32
*tmp
= kmalloc(sizeof(u32
), GFP_KERNEL
);
55 *tmp
= cpu_to_le32(val
);
56 ret
= wdev
->hwbus_ops
->copy_to_io(wdev
->hwbus_priv
, reg
, tmp
,
60 dev_err(wdev
->dev
, "%s: bus communication error: %d\n",
65 static int read32_locked(struct wfx_dev
*wdev
, int reg
, u32
*val
)
69 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
70 ret
= read32(wdev
, reg
, val
);
71 _trace_io_read32(reg
, *val
);
72 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
76 static int write32_locked(struct wfx_dev
*wdev
, int reg
, u32 val
)
80 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
81 ret
= write32(wdev
, reg
, val
);
82 _trace_io_write32(reg
, val
);
83 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
87 static int write32_bits_locked(struct wfx_dev
*wdev
, int reg
, u32 mask
, u32 val
)
94 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
95 ret
= read32(wdev
, reg
, &val_r
);
96 _trace_io_read32(reg
, val_r
);
99 val_w
= (val_r
& ~mask
) | val
;
100 if (val_w
!= val_r
) {
101 ret
= write32(wdev
, reg
, val_w
);
102 _trace_io_write32(reg
, val_w
);
105 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
109 static int indirect_read(struct wfx_dev
*wdev
, int reg
, u32 addr
,
110 void *buf
, size_t len
)
117 WARN_ON(len
>= 0x2000);
118 WARN_ON(reg
!= WFX_REG_AHB_DPORT
&& reg
!= WFX_REG_SRAM_DPORT
);
120 if (reg
== WFX_REG_AHB_DPORT
)
121 prefetch
= CFG_PREFETCH_AHB
;
122 else if (reg
== WFX_REG_SRAM_DPORT
)
123 prefetch
= CFG_PREFETCH_SRAM
;
127 ret
= write32(wdev
, WFX_REG_BASE_ADDR
, addr
);
131 ret
= read32(wdev
, WFX_REG_CONFIG
, &cfg
);
135 ret
= write32(wdev
, WFX_REG_CONFIG
, cfg
| prefetch
);
139 for (i
= 0; i
< 20; i
++) {
140 ret
= read32(wdev
, WFX_REG_CONFIG
, &cfg
);
143 if (!(cfg
& prefetch
))
145 usleep_range(200, 250);
152 ret
= wdev
->hwbus_ops
->copy_from_io(wdev
->hwbus_priv
, reg
, buf
, len
);
156 memset(buf
, 0xFF, len
); // Never return undefined value
160 static int indirect_write(struct wfx_dev
*wdev
, int reg
, u32 addr
,
161 const void *buf
, size_t len
)
165 WARN_ON(len
>= 0x2000);
166 WARN_ON(reg
!= WFX_REG_AHB_DPORT
&& reg
!= WFX_REG_SRAM_DPORT
);
167 ret
= write32(wdev
, WFX_REG_BASE_ADDR
, addr
);
171 return wdev
->hwbus_ops
->copy_to_io(wdev
->hwbus_priv
, reg
, buf
, len
);
174 static int indirect_read_locked(struct wfx_dev
*wdev
, int reg
, u32 addr
,
175 void *buf
, size_t len
)
179 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
180 ret
= indirect_read(wdev
, reg
, addr
, buf
, len
);
181 _trace_io_ind_read(reg
, addr
, buf
, len
);
182 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
186 static int indirect_write_locked(struct wfx_dev
*wdev
, int reg
, u32 addr
,
187 const void *buf
, size_t len
)
191 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
192 ret
= indirect_write(wdev
, reg
, addr
, buf
, len
);
193 _trace_io_ind_write(reg
, addr
, buf
, len
);
194 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
198 static int indirect_read32_locked(struct wfx_dev
*wdev
, int reg
,
202 __le32
*tmp
= kmalloc(sizeof(u32
), GFP_KERNEL
);
206 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
207 ret
= indirect_read(wdev
, reg
, addr
, tmp
, sizeof(u32
));
208 *val
= le32_to_cpu(*tmp
);
209 _trace_io_ind_read32(reg
, addr
, *val
);
210 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
215 static int indirect_write32_locked(struct wfx_dev
*wdev
, int reg
,
219 __le32
*tmp
= kmalloc(sizeof(u32
), GFP_KERNEL
);
223 *tmp
= cpu_to_le32(val
);
224 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
225 ret
= indirect_write(wdev
, reg
, addr
, tmp
, sizeof(u32
));
226 _trace_io_ind_write32(reg
, addr
, val
);
227 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
232 int wfx_data_read(struct wfx_dev
*wdev
, void *buf
, size_t len
)
236 WARN((long)buf
& 3, "%s: unaligned buffer", __func__
);
237 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
238 ret
= wdev
->hwbus_ops
->copy_from_io(wdev
->hwbus_priv
,
239 WFX_REG_IN_OUT_QUEUE
, buf
, len
);
240 _trace_io_read(WFX_REG_IN_OUT_QUEUE
, buf
, len
);
241 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
243 dev_err(wdev
->dev
, "%s: bus communication error: %d\n",
248 int wfx_data_write(struct wfx_dev
*wdev
, const void *buf
, size_t len
)
252 WARN((long)buf
& 3, "%s: unaligned buffer", __func__
);
253 wdev
->hwbus_ops
->lock(wdev
->hwbus_priv
);
254 ret
= wdev
->hwbus_ops
->copy_to_io(wdev
->hwbus_priv
,
255 WFX_REG_IN_OUT_QUEUE
, buf
, len
);
256 _trace_io_write(WFX_REG_IN_OUT_QUEUE
, buf
, len
);
257 wdev
->hwbus_ops
->unlock(wdev
->hwbus_priv
);
259 dev_err(wdev
->dev
, "%s: bus communication error: %d\n",
264 int sram_buf_read(struct wfx_dev
*wdev
, u32 addr
, void *buf
, size_t len
)
266 return indirect_read_locked(wdev
, WFX_REG_SRAM_DPORT
, addr
, buf
, len
);
269 int ahb_buf_read(struct wfx_dev
*wdev
, u32 addr
, void *buf
, size_t len
)
271 return indirect_read_locked(wdev
, WFX_REG_AHB_DPORT
, addr
, buf
, len
);
274 int sram_buf_write(struct wfx_dev
*wdev
, u32 addr
, const void *buf
, size_t len
)
276 return indirect_write_locked(wdev
, WFX_REG_SRAM_DPORT
, addr
, buf
, len
);
279 int ahb_buf_write(struct wfx_dev
*wdev
, u32 addr
, const void *buf
, size_t len
)
281 return indirect_write_locked(wdev
, WFX_REG_AHB_DPORT
, addr
, buf
, len
);
284 int sram_reg_read(struct wfx_dev
*wdev
, u32 addr
, u32
*val
)
286 return indirect_read32_locked(wdev
, WFX_REG_SRAM_DPORT
, addr
, val
);
289 int ahb_reg_read(struct wfx_dev
*wdev
, u32 addr
, u32
*val
)
291 return indirect_read32_locked(wdev
, WFX_REG_AHB_DPORT
, addr
, val
);
294 int sram_reg_write(struct wfx_dev
*wdev
, u32 addr
, u32 val
)
296 return indirect_write32_locked(wdev
, WFX_REG_SRAM_DPORT
, addr
, val
);
299 int ahb_reg_write(struct wfx_dev
*wdev
, u32 addr
, u32 val
)
301 return indirect_write32_locked(wdev
, WFX_REG_AHB_DPORT
, addr
, val
);
304 int config_reg_read(struct wfx_dev
*wdev
, u32
*val
)
306 return read32_locked(wdev
, WFX_REG_CONFIG
, val
);
309 int config_reg_write(struct wfx_dev
*wdev
, u32 val
)
311 return write32_locked(wdev
, WFX_REG_CONFIG
, val
);
314 int config_reg_write_bits(struct wfx_dev
*wdev
, u32 mask
, u32 val
)
316 return write32_bits_locked(wdev
, WFX_REG_CONFIG
, mask
, val
);
319 int control_reg_read(struct wfx_dev
*wdev
, u32
*val
)
321 return read32_locked(wdev
, WFX_REG_CONTROL
, val
);
324 int control_reg_write(struct wfx_dev
*wdev
, u32 val
)
326 return write32_locked(wdev
, WFX_REG_CONTROL
, val
);
329 int control_reg_write_bits(struct wfx_dev
*wdev
, u32 mask
, u32 val
)
331 return write32_bits_locked(wdev
, WFX_REG_CONTROL
, mask
, val
);
334 int igpr_reg_read(struct wfx_dev
*wdev
, int index
, u32
*val
)
338 *val
= ~0; // Never return undefined value
339 ret
= write32_locked(wdev
, WFX_REG_SET_GEN_R_W
, IGPR_RW
| index
<< 24);
342 ret
= read32_locked(wdev
, WFX_REG_SET_GEN_R_W
, val
);
349 int igpr_reg_write(struct wfx_dev
*wdev
, int index
, u32 val
)
351 return write32_locked(wdev
, WFX_REG_SET_GEN_R_W
, index
<< 24 | val
);