1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef	_bcmsdh_h_
18 #define	_bcmsdh_h_
19 
20 #include <linux/skbuff.h>
21 #define BCMSDH_ERROR_VAL	0x0001	/* Error */
22 #define BCMSDH_INFO_VAL		0x0002	/* Info */
23 extern const uint bcmsdh_msglevel;
24 
25 #ifdef BCMDBG
26 #define BCMSDH_ERROR(x) \
27 	do { \
28 		if ((bcmsdh_msglevel & BCMSDH_ERROR_VAL) && net_ratelimit()) \
29 			printk x; \
30 	} while (0)
31 #define BCMSDH_INFO(x)	\
32 	do { \
33 		if ((bcmsdh_msglevel & BCMSDH_INFO_VAL) && net_ratelimit()) \
34 			printk x; \
35 	} while (0)
36 #else				/* BCMDBG */
37 #define BCMSDH_ERROR(x)
38 #define BCMSDH_INFO(x)
39 #endif				/* BCMDBG */
40 
41 /* forward declarations */
42 typedef struct bcmsdh_info bcmsdh_info_t;
43 typedef void (*bcmsdh_cb_fn_t) (void *);
44 
45 /* Attach and build an interface to the underlying SD host driver.
46  *  - Allocates resources (structs, arrays, mem, OS handles, etc) needed by bcmsdh.
47  *  - Returns the bcmsdh handle and virtual address base for register access.
48  *    The returned handle should be used in all subsequent calls, but the bcmsh
49  *    implementation may maintain a single "default" handle (e.g. the first or
50  *    most recent one) to enable single-instance implementations to pass NULL.
51  */
52 extern bcmsdh_info_t *bcmsdh_attach(void *cfghdl, void **regsva, uint irq);
53 
54 /* Detach - freeup resources allocated in attach */
55 extern int bcmsdh_detach(void *sdh);
56 
57 /* Query if SD device interrupts are enabled */
58 extern bool bcmsdh_intr_query(void *sdh);
59 
60 /* Enable/disable SD interrupt */
61 extern int bcmsdh_intr_enable(void *sdh);
62 extern int bcmsdh_intr_disable(void *sdh);
63 
64 /* Register/deregister device interrupt handler. */
65 extern int bcmsdh_intr_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh);
66 extern int bcmsdh_intr_dereg(void *sdh);
67 
68 #if defined(DHD_DEBUG)
69 /* Query pending interrupt status from the host controller */
70 extern bool bcmsdh_intr_pending(void *sdh);
71 #endif
72 extern int bcmsdh_claim_host_and_lock(void *sdh);
73 extern int bcmsdh_release_host_and_unlock(void *sdh);
74 
75 /* Register a callback to be called if and when bcmsdh detects
76  * device removal. No-op in the case of non-removable/hardwired devices.
77  */
78 extern int bcmsdh_devremove_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh);
79 
80 /* Access SDIO address space (e.g. CCCR) using CMD52 (single-byte interface).
81  *   fn:   function number
82  *   addr: unmodified SDIO-space address
83  *   data: data byte to write
84  *   err:  pointer to error code (or NULL)
85  */
86 extern u8 bcmsdh_cfg_read(void *sdh, uint func, u32 addr, int *err);
87 extern void bcmsdh_cfg_write(void *sdh, uint func, u32 addr, u8 data,
88 			     int *err);
89 
90 /* Read/Write 4bytes from/to cfg space */
91 extern u32 bcmsdh_cfg_read_word(void *sdh, uint fnc_num, u32 addr,
92 				   int *err);
93 extern void bcmsdh_cfg_write_word(void *sdh, uint fnc_num, u32 addr,
94 				  u32 data, int *err);
95 
96 /* Read CIS content for specified function.
97  *   fn:     function whose CIS is being requested (0 is common CIS)
98  *   cis:    pointer to memory location to place results
99  *   length: number of bytes to read
100  * Internally, this routine uses the values from the cis base regs (0x9-0xB)
101  * to form an SDIO-space address to read the data from.
102  */
103 extern int bcmsdh_cis_read(void *sdh, uint func, u8 *cis, uint length);
104 
105 /* Synchronous access to device (client) core registers via CMD53 to F1.
106  *   addr: backplane address (i.e. >= regsva from attach)
107  *   size: register width in bytes (2 or 4)
108  *   data: data for register write
109  */
110 extern u32 bcmsdh_reg_read(void *sdh, u32 addr, uint size);
111 extern u32 bcmsdh_reg_write(void *sdh, u32 addr, uint size, u32 data);
112 
113 /* Indicate if last reg read/write failed */
114 extern bool bcmsdh_regfail(void *sdh);
115 
116 /* Buffer transfer to/from device (client) core via cmd53.
117  *   fn:       function number
118  *   addr:     backplane address (i.e. >= regsva from attach)
119  *   flags:    backplane width, address increment, sync/async
120  *   buf:      pointer to memory data buffer
121  *   nbytes:   number of bytes to transfer to/from buf
122  *   pkt:      pointer to packet associated with buf (if any)
123  *   complete: callback function for command completion (async only)
124  *   handle:   handle for completion callback (first arg in callback)
125  * Returns 0 or error code.
126  * NOTE: Async operation is not currently supported.
127  */
128 typedef void (*bcmsdh_cmplt_fn_t) (void *handle, int status, bool sync_waiting);
129 extern int bcmsdh_send_buf(void *sdh, u32 addr, uint fn, uint flags,
130 			   u8 *buf, uint nbytes, void *pkt,
131 			   bcmsdh_cmplt_fn_t complete, void *handle);
132 extern int bcmsdh_recv_buf(void *sdh, u32 addr, uint fn, uint flags,
133 			   u8 *buf, uint nbytes, struct sk_buff *pkt,
134 			   bcmsdh_cmplt_fn_t complete, void *handle);
135 
136 /* Flags bits */
137 #define SDIO_REQ_4BYTE	0x1	/* Four-byte target (backplane) width (vs. two-byte) */
138 #define SDIO_REQ_FIXED	0x2	/* Fixed address (FIFO) (vs. incrementing address) */
139 #define SDIO_REQ_ASYNC	0x4	/* Async request (vs. sync request) */
140 
141 /* Pending (non-error) return code */
142 #define BCME_PENDING	1
143 
144 /* Read/write to memory block (F1, no FIFO) via CMD53 (sync only).
145  *   rw:       read or write (0/1)
146  *   addr:     direct SDIO address
147  *   buf:      pointer to memory data buffer
148  *   nbytes:   number of bytes to transfer to/from buf
149  * Returns 0 or error code.
150  */
151 extern int bcmsdh_rwdata(void *sdh, uint rw, u32 addr, u8 *buf,
152 			 uint nbytes);
153 
154 /* Issue an abort to the specified function */
155 extern int bcmsdh_abort(void *sdh, uint fn);
156 
157 /* Start SDIO Host Controller communication */
158 extern int bcmsdh_start(void *sdh, int stage);
159 
160 /* Stop SDIO Host Controller communication */
161 extern int bcmsdh_stop(void *sdh);
162 
163 /* Returns the "Device ID" of target device on the SDIO bus. */
164 extern int bcmsdh_query_device(void *sdh);
165 
166 /* Returns the number of IO functions reported by the device */
167 extern uint bcmsdh_query_iofnum(void *sdh);
168 
169 /* Miscellaneous knob tweaker. */
170 extern int bcmsdh_iovar_op(void *sdh, const char *name,
171 			   void *params, int plen, void *arg, int len,
172 			   bool set);
173 
174 /* Reset and reinitialize the device */
175 extern int bcmsdh_reset(bcmsdh_info_t *sdh);
176 
177 /* helper functions */
178 
179 extern void *bcmsdh_get_sdioh(bcmsdh_info_t *sdh);
180 
181 /* callback functions */
182 typedef struct {
183 	/* attach to device */
184 	void *(*attach) (u16 vend_id, u16 dev_id, u16 bus, u16 slot,
185 			 u16 func, uint bustype, void *regsva, void *param);
186 	/* detach from device */
187 	void (*detach) (void *ch);
188 } bcmsdh_driver_t;
189 
190 /* platform specific/high level functions */
191 extern int bcmsdh_register(bcmsdh_driver_t *driver);
192 extern void bcmsdh_unregister(void);
193 extern bool bcmsdh_chipmatch(u16 vendor, u16 device);
194 extern void bcmsdh_device_remove(void *sdh);
195 
196 /* Function to pass device-status bits to DHD. */
197 extern u32 bcmsdh_get_dstatus(void *sdh);
198 
199 /* Function to return current window addr */
200 extern u32 bcmsdh_cur_sbwad(void *sdh);
201 
202 /* Function to pass chipid and rev to lower layers for controlling pr's */
203 extern void bcmsdh_chipinfo(void *sdh, u32 chip, u32 chiprev);
204 
205 #endif				/* _bcmsdh_h_ */
206