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