1 /*
2  * Copyright (c) 2014-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "ol_if_athvar.h"
20 #include "targaddrs.h"
21 #include "ol_cfg.h"
22 #include "i_ar6320v2_regtable.h"
23 #include "ol_fw.h"
24 #ifdef HIF_PCI
25 #include "ce_reg.h"
26 #endif
27 #if defined(HIF_SDIO)
28 #include "regtable_sdio.h"
29 #endif
30 #if defined(HIF_USB)
31 #include "regtable_usb.h"
32 #endif
33 #include "i_bmi.h"
34 #include "cds_api.h"
35 
36 #ifdef CONFIG_DISABLE_SLEEP_BMI_OPTION
ol_sdio_disable_sleep(struct ol_context * ol_ctx)37 static inline void ol_sdio_disable_sleep(struct ol_context *ol_ctx)
38 {
39 	uint32_t value;
40 
41 	BMI_ERR("prevent ROME from sleeping");
42 	bmi_read_soc_register(MBOX_BASE_ADDRESS + LOCAL_SCRATCH_OFFSET,
43 		/* this address should be 0x80C0 for ROME*/
44 		&value,
45 		ol_ctx);
46 
47 	value |= SOC_OPTION_SLEEP_DISABLE;
48 
49 	bmi_write_soc_register(MBOX_BASE_ADDRESS + LOCAL_SCRATCH_OFFSET,
50 				 value,
51 				 ol_ctx);
52 }
53 
54 #else
ol_sdio_disable_sleep(struct ol_context * ol_ctx)55 static inline void ol_sdio_disable_sleep(struct ol_context *ol_ctx)
56 {
57 }
58 
59 #endif
60 
61 /**
62  * ol_usb_extra_initialization() - USB extra initialization
63  * @ol_ctx: pointer to ol_context
64  *
65  * USB specific initialization after firmware download
66  *
67  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
68  */
69 static QDF_STATUS
ol_usb_extra_initialization(struct ol_context * ol_ctx)70 ol_usb_extra_initialization(struct ol_context *ol_ctx)
71 {
72 	struct hif_opaque_softc *scn = ol_ctx->scn;
73 	struct hif_target_info *tgt_info =
74 				hif_get_target_info_handle(scn);
75 	QDF_STATUS status = !QDF_STATUS_SUCCESS;
76 	u_int32_t param = 0;
77 
78 	param |= HI_ACS_FLAGS_ALT_DATA_CREDIT_SIZE;
79 	status = bmi_write_memory(
80 				hif_hia_item_address(tgt_info->target_type,
81 					offsetof(struct host_interest_s,
82 					hi_acs_flags)),
83 				(u_int8_t *)&param, 4, ol_ctx);
84 
85 	return status;
86 }
87 
88 /*Setting SDIO block size, mbox ISR yield limit for SDIO based HIF*/
89 static
ol_sdio_extra_initialization(struct ol_context * ol_ctx)90 QDF_STATUS ol_sdio_extra_initialization(struct ol_context *ol_ctx)
91 {
92 
93 	QDF_STATUS status;
94 	uint32_t param;
95 	uint32_t blocksizes[HTC_MAILBOX_NUM_MAX];
96 	uint32_t MboxIsrYieldValue = 99;
97 	struct hif_opaque_softc *scn = ol_ctx->scn;
98 	struct hif_target_info *tgt_info = hif_get_target_info_handle(scn);
99 	uint32_t target_type = tgt_info->target_type;
100 
101 	/* get the block sizes */
102 	status = hif_get_config_item(scn,
103 				HIF_DEVICE_GET_BLOCK_SIZE,
104 				blocksizes, sizeof(blocksizes));
105 	if (status) {
106 		BMI_ERR("Failed to get block size info from HIF layer");
107 		goto exit;
108 	}
109 	/* note: we actually get the block size for mailbox 1,
110 	 * for SDIO the block size on mailbox 0 is artificially
111 	 * set to 1 must be a power of 2
112 	 */
113 	qdf_assert((blocksizes[1] & (blocksizes[1] - 1)) == 0);
114 
115 	/* set the host interest area for the block size */
116 	status = bmi_write_memory(hif_hia_item_address(target_type,
117 				 offsetof(struct host_interest_s,
118 				 hi_mbox_io_block_sz)),
119 				(uint8_t *)&blocksizes[1],
120 				4,
121 				ol_ctx);
122 
123 	if (status) {
124 		BMI_ERR("BMIWriteMemory for IO block size failed");
125 		goto exit;
126 	}
127 
128 	if (MboxIsrYieldValue != 0) {
129 		/* set the host for the mbox ISR yield limit */
130 		status =
131 		bmi_write_memory(hif_hia_item_address(target_type,
132 				offsetof(struct host_interest_s,
133 				hi_mbox_isr_yield_limit)),
134 				(uint8_t *)&MboxIsrYieldValue,
135 				4,
136 				ol_ctx);
137 
138 		if (status) {
139 			BMI_ERR("BMI write for yield limit failed\n");
140 			goto exit;
141 		}
142 	}
143 	ol_sdio_disable_sleep(ol_ctx);
144 	status = bmi_read_memory(hif_hia_item_address(target_type,
145 			offsetof(struct host_interest_s,
146 			hi_acs_flags)),
147 			(uint8_t *)&param,
148 			4,
149 			ol_ctx);
150 	if (status) {
151 		BMI_ERR("BMIReadMemory for hi_acs_flags failed");
152 		goto exit;
153 	}
154 
155 	param |= HI_ACS_FLAGS_ALT_DATA_CREDIT_SIZE;
156 
157 	/* disable swap mailbox for FTM */
158 	if (cds_get_conparam() != QDF_GLOBAL_FTM_MODE)
159 		param |= HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_SET;
160 
161 	if (!cds_is_ptp_tx_opt_enabled())
162 		param |= HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_SET;
163 
164 	/* enable TX completion to collect tx_desc for pktlog */
165 	if (cds_is_packet_log_enabled())
166 		param &= ~HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_SET;
167 
168 	bmi_write_memory(hif_hia_item_address(target_type,
169 			offsetof(struct host_interest_s,
170 			hi_acs_flags)),
171 			(uint8_t *)&param, 4, ol_ctx);
172 exit:
173 	return status;
174 }
175 
176 /**
177  * ol_extra_initialization() - OL extra initialization
178  * @ol_ctx: pointer to ol_context
179  *
180  * Bus specific initialization after firmware download
181  *
182  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
183  */
ol_extra_initialization(struct ol_context * ol_ctx)184 QDF_STATUS ol_extra_initialization(struct ol_context *ol_ctx)
185 {
186 	struct hif_opaque_softc *scn = ol_ctx->scn;
187 
188 	if (hif_get_bus_type(scn) == QDF_BUS_TYPE_SDIO)
189 		return ol_sdio_extra_initialization(ol_ctx);
190 	else if (hif_get_bus_type(scn) == QDF_BUS_TYPE_USB)
191 		return ol_usb_extra_initialization(ol_ctx);
192 
193 	return QDF_STATUS_SUCCESS;
194 }
195 
ol_target_ready(struct hif_opaque_softc * scn,void * cfg_ctx)196 void ol_target_ready(struct hif_opaque_softc *scn, void *cfg_ctx)
197 {
198 	uint32_t value = 0;
199 	QDF_STATUS status = QDF_STATUS_SUCCESS;
200 	struct hif_target_info *tgt_info = hif_get_target_info_handle(scn);
201 	uint32_t target_type = tgt_info->target_type;
202 
203 	if (hif_get_bus_type(scn) != QDF_BUS_TYPE_SDIO)
204 		return;
205 	status = hif_diag_read_mem(scn,
206 		hif_hia_item_address(target_type,
207 		offsetof(struct host_interest_s, hi_acs_flags)),
208 		(uint8_t *)&value, sizeof(u_int32_t));
209 
210 	if (status != QDF_STATUS_SUCCESS) {
211 		BMI_ERR("HIFDiagReadMem failed");
212 		return;
213 	}
214 
215 	if (value & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
216 		BMI_ERR("MAILBOX SWAP Service is enabled!");
217 		hif_set_mailbox_swap(scn);
218 	}
219 
220 	if (value & HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_FW_ACK) {
221 		BMI_ERR("Reduced Tx Complete service is enabled!");
222 		ol_cfg_set_tx_free_at_download(cfg_ctx);
223 	}
224 }
225