xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/sdio/native_sdio/include/hif_internal.h (revision 11f5a63a6cbdda84849a730de22f0a71e635d58c)
1 /*
2  * Copyright (c) 2013-2019 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 #ifndef _HIF_INTERNAL_H_
20 #define _HIF_INTERNAL_H_
21 
22 #include <linux/mmc/card.h>
23 #include <linux/mmc/mmc.h>
24 #include <linux/mmc/host.h>
25 #include <linux/mmc/sdio_func.h>
26 #include <linux/mmc/sdio_ids.h>
27 #include <linux/mmc/sdio.h>
28 #include <linux/mmc/sd.h>
29 
30 #include "athdefs.h"
31 #include "a_types.h"
32 #include "a_osapi.h"
33 #include <qdf_types.h>          /* qdf_device_t, qdf_print */
34 #include <qdf_time.h>           /* qdf_system_ticks, etc. */
35 #include <qdf_status.h>
36 #include <qdf_timer.h>
37 #include <qdf_atomic.h>
38 #include <qdf_list.h>
39 #include "hif.h"
40 #include "hif_debug.h"
41 #include "hif_sdio_common.h"
42 #include <linux/scatterlist.h>
43 #include "hif_main.h"
44 
45 #define HIF_LINUX_MMC_SCATTER_SUPPORT
46 
47 #define BUS_REQUEST_MAX_NUM                105
48 
49 #define SDIO_CLOCK_FREQUENCY_DEFAULT       25000000
50 #define SDWLAN_ENABLE_DISABLE_TIMEOUT      20
51 #define FLAGS_CARD_ENAB                    0x02
52 #define FLAGS_CARD_IRQ_UNMSK               0x04
53 
54 /*
55  * direction - Direction of transfer (HIF_SDIO_READ/HIF_SDIO_WRITE).
56  */
57 #define HIF_SDIO_READ			0x00000001
58 #define HIF_SDIO_WRITE			0x00000002
59 #define HIF_SDIO_DIR_MASK		(HIF_SDIO_READ | HIF_SDIO_WRITE)
60 
61 /*
62  * type - An interface may support different kind of rd/wr commands.
63  * For example: SDIO supports CMD52/CMD53s. In case of MSIO it
64  * translates to using different kinds of TPCs. The command type
65  * is thus divided into a basic and an extended command and can
66  * be specified using HIF_BASIC_IO/HIF_EXTENDED_IO.
67  */
68 #define HIF_BASIC_IO			0x00000004
69 #define HIF_EXTENDED_IO			0x00000008
70 #define HIF_TYPE_MASK			(HIF_BASIC_IO | HIF_EXTENDED_IO)
71 
72 /*
73  * This indicates the whether the command is to be executed in a
74  * blocking or non-blocking fashion (HIF_SYNCHRONOUS/
75  * HIF_ASYNCHRONOUS). The read/write data paths in HTC have been
76  * implemented using the asynchronous mode allowing the the bus
77  * driver to indicate the completion of operation through the
78  * registered callback routine. The requirement primarily comes
79  * from the contexts these operations get called from (a driver's
80  * transmit context or the ISR context in case of receive).
81  * Support for both of these modes is essential.
82  */
83 #define HIF_SYNCHRONOUS		0x00000010
84 #define HIF_ASYNCHRONOUS	0x00000020
85 #define HIF_EMODE_MASK		(HIF_SYNCHRONOUS | HIF_ASYNCHRONOUS)
86 
87 /*
88  * An interface may support different kinds of commands based on
89  * the tradeoff between the amount of data it can carry and the
90  * setup time. Byte and Block modes are supported (HIF_BYTE_BASIS/
91  * HIF_BLOCK_BASIS). In case of latter, the data is rounded off
92  * to the nearest block size by padding. The size of the block is
93  * configurable at compile time using the HIF_BLOCK_SIZE and is
94  * negotiated with the target during initialization after the
95  * AR6000 interrupts are enabled.
96  */
97 #define HIF_BYTE_BASIS		0x00000040
98 #define HIF_BLOCK_BASIS		0x00000080
99 #define HIF_DMODE_MASK		(HIF_BYTE_BASIS | HIF_BLOCK_BASIS)
100 
101 /*
102  * This indicates if the address has to be incremented on AR6000
103  * after every read/write operation (HIF?FIXED_ADDRESS/
104  * HIF_INCREMENTAL_ADDRESS).
105  */
106 #define HIF_FIXED_ADDRESS			0x00000100
107 #define HIF_INCREMENTAL_ADDRESS		0x00000200
108 #define HIF_AMODE_MASK				(HIF_FIXED_ADDRESS | \
109 							HIF_INCREMENTAL_ADDRESS)
110 
111 #define HIF_WR_ASYNC_BYTE_FIX   \
112 		(HIF_SDIO_WRITE | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
113 				HIF_BYTE_BASIS | HIF_FIXED_ADDRESS)
114 #define HIF_WR_ASYNC_BYTE_INC   \
115 	(HIF_SDIO_WRITE | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
116 				HIF_BYTE_BASIS | HIF_INCREMENTAL_ADDRESS)
117 #define HIF_WR_ASYNC_BLOCK_INC  \
118 	(HIF_SDIO_WRITE | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
119 				HIF_BLOCK_BASIS | HIF_INCREMENTAL_ADDRESS)
120 #define HIF_WR_SYNC_BYTE_FIX    \
121 	(HIF_SDIO_WRITE | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
122 				HIF_BYTE_BASIS | HIF_FIXED_ADDRESS)
123 #define HIF_WR_SYNC_BYTE_INC    \
124 	(HIF_SDIO_WRITE | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
125 				HIF_BYTE_BASIS | HIF_INCREMENTAL_ADDRESS)
126 #define HIF_WR_SYNC_BLOCK_INC  \
127 	(HIF_SDIO_WRITE | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
128 				HIF_BLOCK_BASIS | HIF_INCREMENTAL_ADDRESS)
129 #define HIF_WR_ASYNC_BLOCK_FIX \
130 	(HIF_SDIO_WRITE | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
131 				HIF_BLOCK_BASIS | HIF_FIXED_ADDRESS)
132 #define HIF_WR_SYNC_BLOCK_FIX  \
133 	(HIF_SDIO_WRITE | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
134 				HIF_BLOCK_BASIS | HIF_FIXED_ADDRESS)
135 #define HIF_RD_SYNC_BYTE_INC    \
136 	(HIF_SDIO_READ | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
137 				HIF_BYTE_BASIS | HIF_INCREMENTAL_ADDRESS)
138 #define HIF_RD_SYNC_BYTE_FIX    \
139 	(HIF_SDIO_READ | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
140 				HIF_BYTE_BASIS | HIF_FIXED_ADDRESS)
141 #define HIF_RD_ASYNC_BYTE_FIX   \
142 	(HIF_SDIO_READ | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
143 				HIF_BYTE_BASIS | HIF_FIXED_ADDRESS)
144 #define HIF_RD_ASYNC_BLOCK_FIX  \
145 	(HIF_SDIO_READ | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
146 				HIF_BLOCK_BASIS | HIF_FIXED_ADDRESS)
147 #define HIF_RD_ASYNC_BYTE_INC   \
148 	(HIF_SDIO_READ | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
149 				HIF_BYTE_BASIS | HIF_INCREMENTAL_ADDRESS)
150 #define HIF_RD_ASYNC_BLOCK_INC  \
151 	(HIF_SDIO_READ | HIF_ASYNCHRONOUS | HIF_EXTENDED_IO | \
152 				HIF_BLOCK_BASIS | HIF_INCREMENTAL_ADDRESS)
153 #define HIF_RD_SYNC_BLOCK_INC  \
154 	(HIF_SDIO_READ | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
155 				HIF_BLOCK_BASIS | HIF_INCREMENTAL_ADDRESS)
156 #define HIF_RD_SYNC_BLOCK_FIX  \
157 	(HIF_SDIO_READ | HIF_SYNCHRONOUS | HIF_EXTENDED_IO | \
158 				HIF_BLOCK_BASIS | HIF_FIXED_ADDRESS)
159 
160 enum hif_sdio_device_state {
161 		HIF_DEVICE_STATE_ON,
162 		HIF_DEVICE_STATE_DEEPSLEEP,
163 		HIF_DEVICE_STATE_CUTPOWER,
164 		HIF_DEVICE_STATE_WOW
165 };
166 
167 struct bus_request {
168 	struct bus_request *next;       /* link list of available requests */
169 	struct bus_request *inusenext;  /* link list of in use requests */
170 	struct semaphore sem_req;
171 	unsigned long address;       /* request data */
172 	char *buffer;
173 	uint32_t length;
174 	uint32_t request;
175 	void *context;
176 	QDF_STATUS status;
177 	struct HIF_SCATTER_REQ_PRIV *scatter_req;
178 };
179 
180 #define HIF_ADMA_MAX_CHANS 2
181 #ifdef CONFIG_SDIO_TRANSFER_ADMA
182 struct rx_q_entry {
183 	qdf_list_node_t entry;
184 	qdf_nbuf_t nbuf;
185 };
186 #endif
187 
188 struct hif_sdio_dev {
189 	struct sdio_func *func;
190 	qdf_spinlock_t asynclock;
191 	struct task_struct *async_task; /* task to handle async commands */
192 	struct semaphore sem_async;     /* wake up for async task */
193 	int async_shutdown;     /* stop the async task */
194 	struct completion async_completion;     /* thread completion */
195 	struct bus_request *asyncreq;  /* request for async tasklet */
196 	struct bus_request *taskreq;   /*  async tasklet data */
197 	qdf_spinlock_t lock;
198 	struct bus_request *bus_request_free_queue;     /* free list */
199 	struct bus_request bus_request[BUS_REQUEST_MAX_NUM]; /* bus requests */
200 	void *claimed_ctx;
201 	struct htc_callbacks htc_callbacks;
202 	uint8_t *dma_buffer;
203 	DL_LIST scatter_req_head; /* scatter request list head */
204 	bool scatter_enabled; /* scatter enabled flag */
205 	bool is_suspend;
206 	bool is_disabled;
207 	atomic_t irq_handling;
208 	enum HIF_DEVICE_POWER_CHANGE_TYPE power_config;
209 	enum hif_sdio_device_state device_state;
210 	const struct sdio_device_id *id;
211 	struct mmc_host *host;
212 	void *htc_context;
213 #ifdef CONFIG_SDIO_TRANSFER_ADMA
214 	struct sdio_al_client_handle *al_client;
215 	struct sdio_al_channel_handle *al_chan[HIF_ADMA_MAX_CHANS];
216 	uint8_t adma_chans_used;
217 	qdf_list_t rx_q;
218 	qdf_spinlock_t rx_q_lock;
219 	qdf_work_t rx_q_alloc_work;
220 	bool rx_q_alloc_work_scheduled;
221 #endif
222 };
223 
224 struct HIF_DEVICE_OS_DEVICE_INFO {
225 	void *os_dev;
226 };
227 
228 struct hif_mailbox_properties {
229 	u_int32_t    extended_address;  /* extended address for larger writes */
230 	u_int32_t    extended_size;
231 };
232 
233 struct hif_device_irq_yield_params {
234 	int recv_packet_yield_count;
235 	/* max number of packets to force DSR to return */
236 };
237 
238 struct hif_device_mbox_info {
239 	u_int32_t mbox_addresses[4];
240 	/* first element for legacy HIFs and return the address and ARRAY of
241 	 * 32bit words
242 	 */
243 	struct hif_mailbox_properties mbox_prop[4];
244 	u_int32_t gmbox_address;
245 	u_int32_t gmbox_size;
246 	u_int32_t flags;
247 	/* flags to describe mbox behavior or usage */
248 };
249 
250 enum hif_device_irq_mode {
251 	HIF_DEVICE_IRQ_SYNC_ONLY,
252 	/* DSR to process all interrupts before returning */
253 	HIF_DEVICE_IRQ_ASYNC_SYNC,  /* DSR to process interrupts */
254 };
255 
256 /* other interrupts are pending, host
257  * needs to read the to monitor
258  */
259 #define HIF_OTHER_EVENTS     (1 << 0)
260 /* pending recv packet */
261 #define HIF_RECV_MSG_AVAIL   (1 << 1)
262 
263 struct _HIF_PENDING_EVENTS_INFO {
264 	uint32_t events;
265 	uint32_t look_ahead;
266 	uint32_t available_recv_bytes;
267 };
268 
269 /* hif-sdio pending events handler type, some HIF modules
270  * use special mechanisms to detect packet available and other interrupts
271  */
272 typedef int (*HIF_PENDING_EVENTS_FUNC)(struct hif_sdio_dev *device,
273 					struct _HIF_PENDING_EVENTS_INFO *
274 					events, void *async_context);
275 
276 #define HIF_MASK_RECV    true
277 #define HIF_UNMASK_RECV  false
278 /* hif-sdio Handler type to mask receive events */
279 typedef int (*HIF_MASK_UNMASK_RECV_EVENT)(struct hif_sdio_dev *device,
280 					  bool mask,
281 					  void *async_context);
282 
283 QDF_STATUS hif_configure_device(struct hif_softc *ol_sc,
284 				struct hif_sdio_dev *device,
285 				enum hif_device_config_opcode opcode,
286 				void *config, uint32_t config_len);
287 
288 QDF_STATUS hif_attach_htc(struct hif_sdio_dev *device,
289 			  struct htc_callbacks *callbacks);
290 
291 void hif_ack_interrupt(struct hif_sdio_dev *device);
292 
293 void hif_mask_interrupt(struct hif_sdio_dev *device);
294 
295 void hif_un_mask_interrupt(struct hif_sdio_dev *device);
296 
297 int hif_sdio_configure_pipes(struct hif_sdio_dev *dev, struct sdio_func *func);
298 
299 struct _HIF_SCATTER_ITEM {
300 	u_int8_t     *buffer; /* CPU accessible address of buffer */
301 	int          length; /* length of transfer to/from this buffer */
302 	void         *caller_contexts[2]; /* caller context */
303 };
304 
305 struct _HIF_SCATTER_REQ;
306 
307 typedef void (*HIF_SCATTER_COMP_CB)(struct _HIF_SCATTER_REQ *);
308 
309 enum HIF_SCATTER_METHOD {
310 	HIF_SCATTER_NONE = 0,
311 	HIF_SCATTER_DMA_REAL, /* Real SG support no restrictions */
312 	HIF_SCATTER_DMA_BOUNCE, /* Uses SG DMA */
313 };
314 
315 struct _HIF_SCATTER_REQ {
316 	DL_LIST             list_link; /* link management */
317 	u_int32_t            address; /* address for the read/write operation */
318 	u_int32_t            request; /* request flags */
319 	u_int32_t            total_length; /* total length of entire transfer */
320 	u_int32_t            caller_flags; /* caller specific flags */
321 	HIF_SCATTER_COMP_CB  completion_routine; /* completion callback */
322 	int                  completion_status; /* status of completion */
323 	void                 *context; /* caller context for this request */
324 	int                  valid_scatter_entries; /* no of valid entries */
325 	/* scatter method handled by HIF */
326 	enum HIF_SCATTER_METHOD   scatter_method;
327 	void                 *hif_private[4]; /* HIF private area */
328 	u_int8_t             *scatter_bounce_buffer; /* bounce buffers */
329 	struct _HIF_SCATTER_ITEM    scatter_list[1]; /* start of scatter list */
330 };
331 
332 typedef struct _HIF_SCATTER_REQ * (*HIF_ALLOCATE_SCATTER_REQUEST)(
333 						struct hif_sdio_dev *device);
334 typedef void (*HIF_FREE_SCATTER_REQUEST)(struct hif_sdio_dev *device,
335 				struct _HIF_SCATTER_REQ *request);
336 typedef QDF_STATUS (*HIF_READWRITE_SCATTER)(struct hif_sdio_dev *device,
337 					struct _HIF_SCATTER_REQ *request);
338 
339 struct HIF_DEVICE_SCATTER_SUPPORT_INFO {
340 	/* information returned from HIF layer */
341 	HIF_ALLOCATE_SCATTER_REQUEST    allocate_req_func;
342 	HIF_FREE_SCATTER_REQUEST        free_req_func;
343 	HIF_READWRITE_SCATTER           read_write_scatter_func;
344 	int                             max_scatter_entries;
345 	int                             max_tx_size_per_scatter_req;
346 };
347 
348 void hif_get_target_revision(struct hif_softc *ol_sc);
349 struct HIF_SCATTER_REQ_PRIV;
350 
351 #define HIF_DMA_BUFFER_SIZE (4 * 1024)
352 #define CMD53_FIXED_ADDRESS 1
353 #define CMD53_INCR_ADDRESS  2
354 
355 struct bus_request *hif_allocate_bus_request(struct hif_sdio_dev *device);
356 void hif_free_bus_request(struct hif_sdio_dev *device,
357 			  struct bus_request *busrequest);
358 void add_to_async_list(struct hif_sdio_dev *device,
359 		       struct bus_request *busrequest);
360 void hif_dump_cccr(struct hif_sdio_dev *hif_device);
361 
362 #ifdef HIF_LINUX_MMC_SCATTER_SUPPORT
363 
364 #define MAX_SCATTER_REQUESTS             4
365 #define MAX_SCATTER_ENTRIES_PER_REQ      16
366 #define MAX_SCATTER_REQ_TRANSFER_SIZE    (32*1024)
367 
368 struct HIF_SCATTER_REQ_PRIV {
369 	struct _HIF_SCATTER_REQ *hif_scatter_req;
370 	struct hif_sdio_dev *device;     /* this device */
371 	struct bus_request *busrequest;
372 	/* scatter list for linux */
373 	struct scatterlist sgentries[MAX_SCATTER_ENTRIES_PER_REQ];
374 };
375 
376 #define ATH_DEBUG_SCATTER  ATH_DEBUG_MAKE_MODULE_MASK(0)
377 
378 QDF_STATUS setup_hif_scatter_support(struct hif_sdio_dev *device,
379 		   struct HIF_DEVICE_SCATTER_SUPPORT_INFO *info);
380 void cleanup_hif_scatter_resources(struct hif_sdio_dev *device);
381 QDF_STATUS do_hif_read_write_scatter(struct hif_sdio_dev *device,
382 				   struct bus_request *busrequest);
383 
384 #else                           /* HIF_LINUX_MMC_SCATTER_SUPPORT */
385 
386 static inline QDF_STATUS setup_hif_scatter_support(struct hif_sdio_dev *device,
387 				struct HIF_DEVICE_SCATTER_SUPPORT_INFO *info)
388 {
389 	return QDF_STATUS_E_NOSUPPORT;
390 }
391 
392 static inline QDF_STATUS do_hif_read_write_scatter(struct hif_sdio_dev *device,
393 					 struct bus_request *busrequest)
394 {
395 	return QDF_STATUS_E_NOSUPPORT;
396 }
397 
398 #define cleanup_hif_scatter_resources(d) { }
399 
400 #endif /* HIF_LINUX_MMC_SCATTER_SUPPORT */
401 
402 #define SDIO_SET_CMD52_ARG(arg, rw, func, raw, address, writedata) \
403 			((arg) = (((rw) & 1) << 31) | \
404 			(((func) & 0x7) << 28) | \
405 			(((raw) & 1) << 27) | \
406 			(1 << 26) | \
407 			(((address) & 0x1FFFF) << 9) | \
408 			(1 << 8) | \
409 			((writedata) & 0xFF))
410 
411 #define SDIO_SET_CMD52_READ_ARG(arg, func, address) \
412 	SDIO_SET_CMD52_ARG(arg, 0, (func), 0, address, 0x00)
413 #define SDIO_SET_CMD52_WRITE_ARG(arg, func, address, value) \
414 	SDIO_SET_CMD52_ARG(arg, 1, (func), 0, address, value)
415 
416 void hif_sdio_quirk_force_drive_strength(struct hif_softc *ol_sc,
417 					 struct sdio_func *func);
418 void hif_sdio_quirk_write_cccr(struct hif_softc *ol_sc, struct sdio_func *func);
419 int hif_sdio_quirk_mod_strength(struct hif_softc *ol_sc,
420 				struct sdio_func *func);
421 int hif_sdio_quirk_async_intr(struct hif_softc *ol_sc, struct sdio_func *func);
422 int hif_sdio_set_bus_speed(struct hif_softc *ol_sc, struct sdio_func *func);
423 int hif_sdio_set_bus_width(struct hif_softc *ol_sc, struct sdio_func *func);
424 QDF_STATUS hif_sdio_func_disable(struct hif_sdio_dev *device,
425 				 struct sdio_func *func,
426 				 bool reset);
427 QDF_STATUS reinit_sdio(struct hif_sdio_dev *device);
428 
429 int func0_cmd52_write_byte(struct mmc_card *card,
430 			   unsigned int address,
431 			   unsigned char byte);
432 
433 int func0_cmd52_read_byte(struct mmc_card *card,
434 			  unsigned int address,
435 			  unsigned char *byte);
436 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0))
437 /**
438  * sdio_card_highspeed() - check if high speed supported
439  * @card: pointer to mmc card struct
440  *
441  * Return: non zero if card supports high speed.
442  */
443 static inline int sdio_card_highspeed(struct mmc_card *card)
444 {
445 	return mmc_card_highspeed(card);
446 }
447 #else
448 static inline int sdio_card_highspeed(struct mmc_card *card)
449 {
450 	return mmc_card_hs(card);
451 }
452 #endif
453 
454 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0))
455 /**
456  * sdio_card_set_highspeed() - set high speed
457  * @card: pointer to mmc card struct
458  *
459  * Return: none.
460  */
461 static inline void sdio_card_set_highspeed(struct mmc_card *card)
462 {
463 	mmc_card_set_highspeed(card);
464 }
465 #else
466 static inline void sdio_card_set_highspeed(struct mmc_card *card)
467 {
468 }
469 #endif
470 
471 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0))
472 /**
473  * sdio_card_state() - set card state
474  * @card: pointer to mmc card struct
475  *
476  * Return: none.
477  */
478 static inline void sdio_card_state(struct mmc_card *card)
479 {
480 	card->state &= ~MMC_STATE_HIGHSPEED;
481 }
482 #else
483 static inline void sdio_card_state(struct mmc_card *card)
484 {
485 }
486 #endif
487 #endif /* _HIF_INTERNAL_H_ */
488