1// SPDX-License-Identifier: GPL-2.0
2/******************************************************************************
3 *
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 *
6 ******************************************************************************/
7/*
8
9The purpose of rtw_io.c
10
11a. provides the API
12
13b. provides the protocol engine
14
15c. provides the software interface between caller and the hardware interface
16
17
18Compiler Flag Option:
19
201. CONFIG_SDIO_HCI:
21    a. USE_SYNC_IRP:  Only sync operations are provided.
22    b. USE_ASYNC_IRP:Both sync/async operations are provided.
23
24jackson@realtek.com.tw
25
26*/
27
28#include <drv_types.h>
29#include <rtw_debug.h>
30
31u8 rtw_read8(struct adapter *adapter, u32 addr)
32{
33	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
34	struct io_priv *pio_priv = &adapter->iopriv;
35	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
36	u8 (*_read8)(struct intf_hdl *pintfhdl, u32 addr);
37
38	_read8 = pintfhdl->io_ops._read8;
39
40	return _read8(pintfhdl, addr);
41}
42
43u16 rtw_read16(struct adapter *adapter, u32 addr)
44{
45	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
46	struct io_priv *pio_priv = &adapter->iopriv;
47	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
48	u16 (*_read16)(struct intf_hdl *pintfhdl, u32 addr);
49
50	_read16 = pintfhdl->io_ops._read16;
51
52	return _read16(pintfhdl, addr);
53}
54
55u32 rtw_read32(struct adapter *adapter, u32 addr)
56{
57	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
58	struct io_priv *pio_priv = &adapter->iopriv;
59	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
60	u32 (*_read32)(struct intf_hdl *pintfhdl, u32 addr);
61
62	_read32 = pintfhdl->io_ops._read32;
63
64	return _read32(pintfhdl, addr);
65
66}
67
68int rtw_write8(struct adapter *adapter, u32 addr, u8 val)
69{
70	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
71	struct io_priv *pio_priv = &adapter->iopriv;
72	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
73	int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
74	int ret;
75
76	_write8 = pintfhdl->io_ops._write8;
77
78	ret = _write8(pintfhdl, addr, val);
79
80	return RTW_STATUS_CODE(ret);
81}
82int rtw_write16(struct adapter *adapter, u32 addr, u16 val)
83{
84	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
85	struct io_priv *pio_priv = &adapter->iopriv;
86	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
87	int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
88	int ret;
89
90	_write16 = pintfhdl->io_ops._write16;
91
92	ret = _write16(pintfhdl, addr, val);
93	return RTW_STATUS_CODE(ret);
94}
95int rtw_write32(struct adapter *adapter, u32 addr, u32 val)
96{
97	/* struct	io_queue	*pio_queue = (struct io_queue *)adapter->pio_queue; */
98	struct io_priv *pio_priv = &adapter->iopriv;
99	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
100	int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
101	int ret;
102
103	_write32 = pintfhdl->io_ops._write32;
104
105	ret = _write32(pintfhdl, addr, val);
106
107	return RTW_STATUS_CODE(ret);
108}
109
110u32 rtw_write_port(struct adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
111{
112	u32 (*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
113	struct io_priv *pio_priv = &adapter->iopriv;
114	struct	intf_hdl		*pintfhdl = &(pio_priv->intf);
115
116	_write_port = pintfhdl->io_ops._write_port;
117
118	return _write_port(pintfhdl, addr, cnt, pmem);
119}
120
121int rtw_init_io_priv(struct adapter *padapter, void (*set_intf_ops)(struct adapter *padapter, struct _io_ops *pops))
122{
123	struct io_priv *piopriv = &padapter->iopriv;
124	struct intf_hdl *pintf = &piopriv->intf;
125
126	if (!set_intf_ops)
127		return _FAIL;
128
129	piopriv->padapter = padapter;
130	pintf->padapter = padapter;
131	pintf->pintf_dev = adapter_to_dvobj(padapter);
132
133	set_intf_ops(padapter, &pintf->io_ops);
134
135	return _SUCCESS;
136}
137
138/*
139* Increase and check if the continual_io_error of this @param dvobjprive is larger than MAX_CONTINUAL_IO_ERR
140* @return true:
141* @return false:
142*/
143int rtw_inc_and_chk_continual_io_error(struct dvobj_priv *dvobj)
144{
145	int ret = false;
146	int value = atomic_inc_return(&dvobj->continual_io_error);
147	if (value > MAX_CONTINUAL_IO_ERR)
148		ret = true;
149
150	return ret;
151}
152
153/*
154* Set the continual_io_error of this @param dvobjprive to 0
155*/
156void rtw_reset_continual_io_error(struct dvobj_priv *dvobj)
157{
158	atomic_set(&dvobj->continual_io_error, 0);
159}
160