1 /*****************************************************************************
3 (c) Cambridge Silicon Radio Limited 2011
4 All rights reserved and confidential information of CSR
6 Refer to LICENSE.txt included with this source for details
9 *****************************************************************************/
12 * ---------------------------------------------------------------------------
14 * FILE: csr_wifi_hip_card_sdio.h
17 * Internal header for Card API for SDIO.
18 * ---------------------------------------------------------------------------
20 #ifndef __CARD_SDIO_H__
21 #define __CARD_SDIO_H__
27 #include "csr_wifi_hip_unifi.h"
28 #include "csr_wifi_hip_unifi_udi.h"
29 #include "csr_wifi_hip_unifihw.h"
30 #include "csr_wifi_hip_unifiversion.h"
31 #ifndef CSR_WIFI_HIP_TA_DISABLE
32 #include "csr_wifi_hip_ta_sampling.h"
34 #include "csr_wifi_hip_xbv.h"
35 #include "csr_wifi_hip_chiphelper.h"
40 * Configuration items.
41 * Which of these should go in a platform unifi_config.h file?
46 * When the traffic queues contain more signals than there is space for on
47 * UniFi, a limiting algorithm comes into play.
48 * If a traffic queue has enough slots free to buffer more traffic from the
49 * network stack, then the following check is applied. The number of free
50 * slots is RESUME_XMIT_THRESHOLD.
52 #define RESUME_XMIT_THRESHOLD 4
56 * When reading signals from UniFi, the host processes pending all signals
57 * and then acknowledges them together in a single write to update the
58 * to-host-chunks-read location.
59 * When there is more than one bulk data transfer (e.g. one received data
60 * packet and a request for the payload data of a transmitted packet), the
61 * update can be delayed significantly. This ties up resources on chip.
63 * To remedy this problem, to-host-chunks-read is updated after processing
64 * a signal if TO_HOST_FLUSH_THRESHOLD bytes of bulk data have been
65 * transferred since the last update.
67 #define TO_HOST_FLUSH_THRESHOLD (500 * 5)
70 /* SDIO Card Common Control Registers */
71 #define SDIO_CCCR_SDIO_REVISION (0x00)
72 #define SDIO_SD_SPEC_REVISION (0x01)
73 #define SDIO_IO_ENABLE (0x02)
74 #define SDIO_IO_READY (0x03)
75 #define SDIO_INT_ENABLE (0x04)
76 #define SDIO_INT_PENDING (0x05)
77 #define SDIO_IO_ABORT (0x06)
78 #define SDIO_BUS_IFACE_CONTROL (0x07)
79 #define SDIO_CARD_CAPABILOTY (0x08)
80 #define SDIO_COMMON_CIS_POINTER (0x09)
81 #define SDIO_BUS_SUSPEND (0x0C)
82 #define SDIO_FUNCTION_SELECT (0x0D)
83 #define SDIO_EXEC_FLAGS (0x0E)
84 #define SDIO_READY_FLAGS (0x0F)
85 #define SDIO_FN0_BLOCK_SIZE (0x10)
86 #define SDIO_POWER_CONTROL (0x12)
87 #define SDIO_VENDOR_START (0xF0)
89 #define SDIO_CSR_HOST_WAKEUP (0xf0)
90 #define SDIO_CSR_HOST_INT_CLEAR (0xf1)
91 #define SDIO_CSR_FROM_HOST_SCRATCH0 (0xf2)
92 #define SDIO_CSR_FROM_HOST_SCRATCH1 (0xf3)
93 #define SDIO_CSR_TO_HOST_SCRATCH0 (0xf4)
94 #define SDIO_CSR_TO_HOST_SCRATCH1 (0xf5)
95 #define SDIO_CSR_FUNC_EN (0xf6)
96 #define SDIO_CSR_CSPI_MODE (0xf7)
97 #define SDIO_CSR_CSPI_STATUS (0xf8)
98 #define SDIO_CSR_CSPI_PADDING (0xf9)
101 #define UNIFI_SD_INT_ENABLE_IENM 0x0001 /* Master INT Enable */
103 #ifdef CSR_PRE_ALLOC_NET_DATA
104 #define BULK_DATA_PRE_ALLOC_NUM 16
108 * Structure to hold configuration information read from UniFi.
113 * The version of the SDIO signal queues and bulk data pools
114 * configuration structure. The MSB is the major version number, used to
115 * indicate incompatible changes. The LSB gives the minor revision number,
116 * used to indicate changes that maintain backwards compatibility.
121 * offset from the start of the shared data memory to the SD IO
124 u16 sdio_ctrl_offset;
126 /* Buffer handle of the from-host signal queue */
127 u16 fromhost_sigbuf_handle;
129 /* Buffer handle of the to-host signal queue */
130 u16 tohost_sigbuf_handle;
133 * Maximum number of signal primitive or bulk data command fragments that may be
134 * pending in the to-hw signal queue.
136 u16 num_fromhost_sig_frags;
139 * Number of signal primitive or bulk data command fragments that must be pending
140 * in the to-host signal queue before the host will generate an interrupt
141 * to indicate that it has read a signal. This will usually be the total
142 * capacity of the to-host signal buffer less the size of the largest signal
143 * primitive divided by the signal primitive fragment size, but may be set
144 * to 1 to request interrupts every time that the host read a signal.
145 * Note that the hw may place more signals in the to-host signal queue
146 * than indicated by this field.
148 u16 num_tohost_sig_frags;
151 * Number of to-hw bulk data slots. Slots are numbered from 0 (zero) to
152 * one less than the value in this field
154 u16 num_fromhost_data_slots;
157 * Number of frm-hw bulk data slots. Slots are numbered from 0 (zero) to
158 * one less than the value in this field
160 u16 num_tohost_data_slots;
163 * Size of the bulk data slots (2 octets)
164 * The size of the bulk data slots in octets. This will usually be
165 * the size of the largest MSDU. The value should always be even.
170 * Indicates that the host has finished the initialisation sequence.
171 * Initialised to 0x0000 by the firmware, and set to 0x0001 by us.
175 /* Added by protocol version 0x0001 */
178 /* Added by protocol version 0x0300 */
182 /* Added by protocol version 0x0500 */
183 u16 tohost_signal_padding;
184 } sdio_config_data_t;
187 * These values may change with versions of the Host Interface Protocol.
190 * Size of config info block pointed to by the CSR_SLT_SDIO_SLOT_CONFIG
191 * entry in the f/w symbol table
193 #define SDIO_CONFIG_DATA_SIZE 30
195 /* Offset of the INIT flag in the config info block. */
196 #define SDIO_INIT_FLAG_OFFSET 0x12
197 #define SDIO_TO_HOST_SIG_PADDING_OFFSET 0x1C
200 /* Structure for a bulk data transfer command */
203 u16 cmd_and_len; /* bits 12-15 cmd, bits 0-11 len */
204 u16 data_slot; /* slot number, perhaps OR'd with SLOT_DIR_TO_HOST */
210 /* Bulk Data signal command values */
211 #define SDIO_CMD_SIGNAL 0x00
212 #define SDIO_CMD_TO_HOST_TRANSFER 0x01
213 #define SDIO_CMD_TO_HOST_TRANSFER_ACK 0x02 /*deprecated*/
214 #define SDIO_CMD_FROM_HOST_TRANSFER 0x03
215 #define SDIO_CMD_FROM_HOST_TRANSFER_ACK 0x04 /*deprecated*/
216 #define SDIO_CMD_CLEAR_SLOT 0x05
217 #define SDIO_CMD_OVERLAY_TRANSFER 0x06
218 #define SDIO_CMD_OVERLAY_TRANSFER_ACK 0x07 /*deprecated*/
219 #define SDIO_CMD_FROM_HOST_AND_CLEAR 0x08
220 #define SDIO_CMD_PADDING 0x0f
222 #define SLOT_DIR_TO_HOST 0x8000
225 /* Initialise bulkdata slot
227 * bulk_data_desc_t *bulk_data_slot
229 #define UNIFI_INIT_BULK_DATA(bulk_data_slot) \
231 (bulk_data_slot)->os_data_ptr = NULL; \
232 (bulk_data_slot)->data_length = 0; \
233 (bulk_data_slot)->os_net_buf_ptr = NULL; \
234 (bulk_data_slot)->net_buf_length = 0; \
238 * Structure to contain a SIGNAL datagram.
239 * This is used to build signal queues between the main driver and the
242 * sigbuf Contains the HIP signal is wire-format (i.e. packed,
244 * bulkdata Contains a copy of any associated bulk data
245 * signal_length The size of the signal in the sigbuf
247 typedef struct card_signal
249 u8 sigbuf[UNIFI_PACKED_SIGBUF_SIZE];
251 /* Length of the SIGNAL inside sigbuf */
254 bulk_data_desc_t bulkdata[UNIFI_MAX_DATA_REFERENCES];
259 * Control structure for a generic ring buffer.
261 #define UNIFI_QUEUE_NAME_MAX_LENGTH 16
264 card_signal_t *q_body;
266 /* Num elements in queue (capacity is one less than this!) */
272 char name[UNIFI_QUEUE_NAME_MAX_LENGTH];
276 #define UNIFI_RESERVED_COMMAND_SLOTS 2
278 /* Considering approx 500 us per packet giving 0.5 secs */
279 #define UNIFI_PACKETS_INTERVAL 1000
282 * Dynamic slot reservation for QoS
286 u16 from_host_used_slots[UNIFI_NO_OF_TX_QS];
287 u16 from_host_max_slots[UNIFI_NO_OF_TX_QS];
288 u16 from_host_reserved_slots[UNIFI_NO_OF_TX_QS];
290 /* Parameters to determine if a queue was active.
291 If number of packets sent is greater than the threshold
292 for the queue, the queue is considered active and no
293 re reservation is done, it is important not to keep this
295 /* Packets sent during this interval */
296 u16 packets_txed[UNIFI_NO_OF_TX_QS];
297 u16 total_packets_txed;
299 /* Number of packets to see if slots need to be reassigned */
300 u16 packets_interval;
302 /* Once a queue reaches a stable state, avoid processing */
303 u8 queue_stable[UNIFI_NO_OF_TX_QS];
304 } card_dynamic_slot_t;
307 /* These are type-safe and don't write incorrect values to the
310 /* Return queue slots used count
316 #define CSR_WIFI_HIP_Q_SLOTS_USED(q) \
317 (((q)->q_wr_ptr - (q)->q_rd_ptr < 0)? \
318 ((q)->q_wr_ptr - (q)->q_rd_ptr + (q)->q_length) : ((q)->q_wr_ptr - (q)->q_rd_ptr))
320 /* Return queue slots free count
326 #define CSR_WIFI_HIP_Q_SLOTS_FREE(q) \
327 ((q)->q_length - CSR_WIFI_HIP_Q_SLOTS_USED((q)) - 1)
329 /* Return slot signal data pointer
336 #define CSR_WIFI_HIP_Q_SLOT_DATA(q, slot) \
339 /* Return queue next read slot
345 #define CSR_WIFI_HIP_Q_NEXT_R_SLOT(q) \
348 /* Return queue next write slot
354 #define CSR_WIFI_HIP_Q_NEXT_W_SLOT(q) \
357 /* Return updated queue pointer wrapped around its length
360 * u16 x amount to add to queue pointer
362 * u16 wrapped queue pointer
364 #define CSR_WIFI_HIP_Q_WRAP(q, x) \
365 ((((x) >= (q)->q_length)?((x) % (q)->q_length) : (x)))
367 /* Advance queue read pointer
371 #define CSR_WIFI_HIP_Q_INC_R(q) \
372 ((q)->q_rd_ptr = CSR_WIFI_HIP_Q_WRAP((q), (q)->q_rd_ptr + 1))
374 /* Advance queue write pointer
378 #define CSR_WIFI_HIP_Q_INC_W(q) \
379 ((q)->q_wr_ptr = CSR_WIFI_HIP_Q_WRAP((q), (q)->q_wr_ptr + 1))
381 enum unifi_host_state
383 UNIFI_HOST_STATE_AWAKE = 0,
384 UNIFI_HOST_STATE_DROWSY = 1,
385 UNIFI_HOST_STATE_TORPID = 2
391 unifi_TrafficQueue queue; /* Used for dynamic slot reservation */
395 * Structure describing a UniFi SDIO card.
400 * Back pointer for the higher level OS code. This is passed as
401 * an argument to callbacks (e.g. for received data and indications).
406 * mapping of HIP slot to MA-PACKET.req host tag, the
407 * array is indexed by slot numbers and each index stores
408 * information of the last host tag it was used for
410 u32 *fh_slot_host_tag_record;
413 /* Info read from Symbol Table during probe */
415 char build_id_string[128];
417 /* Retrieve from SDIO driver. */
420 /* Read from GBL_CHIP_VERSION. */
423 /* From the SDIO driver (probably 1) */
426 /* This is sused to get the register addresses and things. */
427 ChipDescript *helper;
430 * Bit mask of PIOs for the loader to waggle during download.
431 * We assume these are connected to LEDs. The main firmware gets
432 * the mask from a MIB entry.
437 * Support for flow control. When the from-host queue of signals
438 * is full, we ask the host upper layer to stop sending packets. When
439 * the queue drains we tell it that it can send packets again.
440 * We use this flag to remember the current state.
442 #define card_is_tx_q_paused(card, q) (card->tx_q_paused_flag[q])
443 #define card_tx_q_unpause(card, q) (card->tx_q_paused_flag[q] = 0)
444 #define card_tx_q_pause(card, q) (card->tx_q_paused_flag[q] = 1)
446 u16 tx_q_paused_flag[UNIFI_TRAFFIC_Q_MAX + 1 + UNIFI_NO_OF_TX_QS]; /* defensive more than big enough */
448 /* UDI callback for logging UniFi interactions */
454 /* SDIO clock speed request from OS layer */
455 u8 request_max_clock;
457 /* Last SDIO clock frequency set */
458 u32 sdio_clock_speed;
461 * Current host state (copy of value in IOABORT register and
462 * spinlock to protect it.
464 enum unifi_host_state host_state;
466 enum unifi_low_power_mode low_power_mode;
467 enum unifi_periodic_wake_mode periodic_wake_mode;
470 * Ring buffer of signal structs for a queue of data packets from
472 * The queue is empty when fh_data_q_num_rd == fh_data_q_num_wr.
473 * To add a packet to the queue, copy it to index given by
474 * (fh_data_q_num_wr%UNIFI_SOFT_Q_LENGTH) and advance fh_data_q_num_wr.
475 * To take a packet from the queue, copy data from index given by
476 * (fh_data_q_num_rd%UNIFI_SOFT_Q_LENGTH) and advance fh_data_q_num_rd.
477 * fh_data_q_num_rd and fh_data_q_num_rd are both modulo 256.
479 card_signal_t fh_command_q_body[UNIFI_SOFT_COMMAND_Q_LENGTH];
480 q_t fh_command_queue;
482 card_signal_t fh_traffic_q_body[UNIFI_NO_OF_TX_QS][UNIFI_SOFT_TRAFFIC_Q_LENGTH];
483 q_t fh_traffic_queue[UNIFI_NO_OF_TX_QS];
486 * Signal counts from UniFi SDIO Control Data Structure.
487 * These are cached and synchronised with the UniFi before and after
488 * a batch of operations.
490 * These are the modulo-256 count of signals written to or read from UniFi
491 * The value is incremented for every signal.
493 s32 from_host_signals_w;
494 s32 from_host_signals_r;
495 s32 to_host_signals_r;
496 s32 to_host_signals_w;
499 /* Should specify buffer size as a number of signals */
501 * Enough for 10 th and 10 fh data slots:
505 #define UNIFI_FH_BUF_SIZE 1024
508 u8 *buf; /* buffer area */
509 u8 *ptr; /* current pos */
510 u16 count; /* signal count */
513 struct sigbuf th_buffer;
517 * Field to use for the incrementing value to write to the UniFi
518 * SHARED_IO_INTERRUPT register.
519 * Flag to say we need to generate an interrupt at end of processing.
521 u32 unifi_interrupt_seq;
522 u8 generate_interrupt;
525 /* Pointers to the bulk data slots */
526 slot_desc_t *from_host_data;
527 bulk_data_desc_t *to_host_data;
531 * Index of the next (hopefully) free data slot.
532 * This is an optimisation that starts searching at a more likely point
533 * than the beginning.
535 s16 from_host_data_head;
537 /* Dynamic slot allocation for queues */
538 card_dynamic_slot_t dynamic_slot_data;
541 * SDIO specific fields
544 /* Interface pointer for the SDIO library */
545 CsrSdioFunction *sdio_if;
547 /* Copy of config_data struct from the card */
548 sdio_config_data_t config_data;
550 /* SDIO address of the Initialised flag and Control Data struct */
554 /* The last value written to the Shared Data Memory Page register */
559 /* SDIO traffic counters limited to 32 bits for Synergy compatibility */
561 u32 sdio_bytes_written;
563 u8 memory_resources_allocated;
565 /* UniFi SDIO I/O Block size. */
566 u16 sdio_io_block_size;
568 /* Pad transfer sizes to SDIO block boundaries */
569 u8 sdio_io_block_pad;
571 /* Read from the XBV */
574 #ifndef CSR_WIFI_HIP_TA_DISABLE
576 ta_data_t ta_sampling;
580 s16 request_coredump_on_reset; /* request coredump on next reset */
581 struct coredump_buf *dump_buf; /* root node */
582 struct coredump_buf *dump_next_write; /* node to fill at next dump */
583 struct coredump_buf *dump_cur_read; /* valid node to read, or NULL */
585 #ifdef CSR_WIFI_HIP_DATA_PLANE_PROFILE
597 u32 cmd52_f0_r_count;
598 u32 cmd52_f0_w_count;
599 u32 cmd52_r8or16_count;
600 u32 cmd52_w8or16_count;
606 u32 sdio_cmd_clear_slot;
607 u32 sdio_cmd_to_host;
608 u32 sdio_cmd_from_host;
609 u32 sdio_cmd_from_host_and_clear;
611 struct cmd_profile cmd_prof;
614 /* Interrupt processing mode flags */
621 /* Historic firmware panic codes */
622 u32 panic_data_phy_addr;
623 u32 panic_data_mac_addr;
624 u16 last_phy_panic_code;
625 u16 last_phy_panic_arg;
626 u16 last_mac_panic_code;
627 u16 last_mac_panic_arg;
628 #ifdef CSR_PRE_ALLOC_NET_DATA
629 bulk_data_desc_t bulk_data_desc_list[BULK_DATA_PRE_ALLOC_NUM];
630 u16 prealloc_netdata_r;
631 u16 prealloc_netdata_w;
637 enum unifi_reset_type
639 UNIFI_COLD_RESET = 1,
644 * unifi_set_host_state() implements signalling for waking UniFi from
645 * deep sleep. The host indicates to UniFi that it is in one of three states:
646 * Torpid - host has nothing to send, UniFi can go to sleep.
647 * Drowsy - host has data to send to UniFi. UniFi will respond with an
648 * SDIO interrupt. When hosts responds it moves to Awake.
649 * Awake - host has data to transfer, UniFi must stay awake.
650 * When host has finished, it moves to Torpid.
652 CsrResult unifi_set_host_state(card_t *card, enum unifi_host_state state);
655 CsrResult unifi_set_proc_select(card_t *card, enum unifi_dbg_processors_select select);
656 s32 card_read_signal_counts(card_t *card);
657 bulk_data_desc_t* card_find_data_slot(card_t *card, s16 slot);
660 CsrResult unifi_read32(card_t *card, u32 unifi_addr, u32 *pdata);
661 CsrResult unifi_readnz(card_t *card, u32 unifi_addr,
662 void *pdata, u16 len);
663 s32 unifi_read_shared_count(card_t *card, u32 addr);
665 CsrResult unifi_writen(card_t *card, u32 unifi_addr, void *pdata, u16 len);
667 CsrResult unifi_bulk_rw(card_t *card, u32 handle,
668 void *pdata, u32 len, s16 direction);
669 CsrResult unifi_bulk_rw_noretry(card_t *card, u32 handle,
670 void *pdata, u32 len, s16 direction);
671 #define UNIFI_SDIO_READ 0
672 #define UNIFI_SDIO_WRITE 1
674 CsrResult unifi_read_8_or_16(card_t *card, u32 unifi_addr, u8 *pdata);
675 CsrResult unifi_write_8_or_16(card_t *card, u32 unifi_addr, u8 data);
676 CsrResult unifi_read_direct_8_or_16(card_t *card, u32 addr, u8 *pdata);
677 CsrResult unifi_write_direct_8_or_16(card_t *card, u32 addr, u8 data);
679 CsrResult unifi_read_direct16(card_t *card, u32 addr, u16 *pdata);
680 CsrResult unifi_read_direct32(card_t *card, u32 addr, u32 *pdata);
681 CsrResult unifi_read_directn(card_t *card, u32 addr, void *pdata, u16 len);
683 CsrResult unifi_write_direct16(card_t *card, u32 addr, u16 data);
684 CsrResult unifi_write_directn(card_t *card, u32 addr, void *pdata, u16 len);
686 CsrResult sdio_read_f0(card_t *card, u32 addr, u8 *pdata);
687 CsrResult sdio_write_f0(card_t *card, u32 addr, u8 data);
689 void unifi_read_panic(card_t *card);
690 #ifdef CSR_PRE_ALLOC_NET_DATA
691 void prealloc_netdata_free(card_t *card);
692 CsrResult prealloc_netdata_alloc(card_t *card);
694 /* For diagnostic use */
695 void dump(void *mem, u16 len);
696 void dump16(void *mem, u16 len);
702 #endif /* __CARD_SDIO_H__ */