xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd/bcmsdspi_linux.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Broadcom SPI Host Controller Driver - Linux Per-port
3  *
4  * Copyright (C) 2020, Broadcom.
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *
21  * <<Broadcom-WL-IPTag/Open:>>
22  *
23  * $Id$
24  */
25 
26 #include <typedefs.h>
27 #include <bcmutils.h>
28 
29 #include <bcmsdbus.h>		/* bcmsdh to/from specific controller APIs */
30 #include <sdiovar.h>		/* to get msglevel bit values */
31 
32 #ifdef BCMSPI_ANDROID
33 #include <bcmsdh.h>
34 #include <bcmspibrcm.h>
35 #include <linux/spi/spi.h>
36 #else
37 #include <pcicfg.h>
38 #include <sdio.h>		/* SDIO Device and Protocol Specs */
39 #include <linux/sched.h>	/* request_irq(), free_irq() */
40 #include <bcmsdspi.h>
41 #include <bcmspi.h>
42 #endif /* BCMSPI_ANDROID */
43 
44 #ifndef BCMSPI_ANDROID
45 extern uint sd_crc;
46 module_param(sd_crc, uint, 0);
47 
48 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
49 #define KERNEL26
50 #endif
51 #endif /* !BCMSPI_ANDROID */
52 
53 struct sdos_info {
54 	sdioh_info_t *sd;
55 	spinlock_t lock;
56 #ifndef BCMSPI_ANDROID
57 	wait_queue_head_t intr_wait_queue;
58 #endif /* !BCMSPI_ANDROID */
59 };
60 
61 #ifndef BCMSPI_ANDROID
62 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
63 #define BLOCKABLE()	(!in_atomic())
64 #else
65 #define BLOCKABLE()	(!in_interrupt()) /* XXX Doesn't handle CONFIG_PREEMPT? */
66 #endif
67 
68 /* Interrupt handler */
69 static irqreturn_t
sdspi_isr(int irq,void * dev_id,struct pt_regs * ptregs)70 sdspi_isr(int irq, void *dev_id
71 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
72 , struct pt_regs *ptregs
73 #endif
74 )
75 {
76 	sdioh_info_t *sd;
77 	struct sdos_info *sdos;
78 	bool ours;
79 
80 	sd = (sdioh_info_t *)dev_id;
81 	sd->local_intrcount++;
82 
83 	if (!sd->card_init_done) {
84 		sd_err(("%s: Hey Bogus intr...not even initted: irq %d\n", __FUNCTION__, irq));
85 		return IRQ_RETVAL(FALSE);
86 	} else {
87 		ours = spi_check_client_intr(sd, NULL);
88 
89 		/* For local interrupts, wake the waiting process */
90 		if (ours && sd->got_hcint) {
91 			sdos = (struct sdos_info *)sd->sdos_info;
92 			wake_up_interruptible(&sdos->intr_wait_queue);
93 		}
94 
95 		return IRQ_RETVAL(ours);
96 	}
97 }
98 #endif /* !BCMSPI_ANDROID */
99 
100 #ifdef BCMSPI_ANDROID
101 static struct spi_device *gBCMSPI = NULL;
102 
103 extern int bcmsdh_probe(struct device *dev);
104 extern int bcmsdh_remove(struct device *dev);
105 
bcmsdh_spi_probe(struct spi_device * spi_dev)106 static int bcmsdh_spi_probe(struct spi_device *spi_dev)
107 {
108 	int ret = 0;
109 
110 	gBCMSPI = spi_dev;
111 
112 #ifdef SPI_PIO_32BIT_RW
113 	spi_dev->bits_per_word = 32;
114 #else
115 	spi_dev->bits_per_word = 8;
116 #endif /* SPI_PIO_32BIT_RW */
117 	ret = spi_setup(spi_dev);
118 
119 	if (ret) {
120 		sd_err(("bcmsdh_spi_probe: spi_setup fail with %d\n", ret));
121 	}
122 	sd_err(("bcmsdh_spi_probe: spi_setup with %d, bits_per_word=%d\n",
123 		ret, spi_dev->bits_per_word));
124 	ret = bcmsdh_probe(&spi_dev->dev);
125 
126 	return ret;
127 }
128 
bcmsdh_spi_remove(struct spi_device * spi_dev)129 static int  bcmsdh_spi_remove(struct spi_device *spi_dev)
130 {
131 	int ret = 0;
132 
133 	ret = bcmsdh_remove(&spi_dev->dev);
134 	gBCMSPI = NULL;
135 
136 	return ret;
137 }
138 
139 static struct spi_driver bcmsdh_spi_driver = {
140 	.probe		= bcmsdh_spi_probe,
141 	.remove		= bcmsdh_spi_remove,
142 	.driver		= {
143 		.name = "wlan_spi",
144 		.bus    = &spi_bus_type,
145 		.owner  = THIS_MODULE,
146 		},
147 };
148 
149 /*
150  * module init
151 */
bcmsdh_register_client_driver(void)152 int bcmsdh_register_client_driver(void)
153 {
154 	int error = 0;
155 	sd_trace(("bcmsdh_gspi: %s Enter\n", __FUNCTION__));
156 
157 	error = spi_register_driver(&bcmsdh_spi_driver);
158 
159 	return error;
160 }
161 
162 /*
163  * module cleanup
164 */
bcmsdh_unregister_client_driver(void)165 void bcmsdh_unregister_client_driver(void)
166 {
167 	sd_trace(("%s Enter\n", __FUNCTION__));
168 	spi_unregister_driver(&bcmsdh_spi_driver);
169 }
170 #endif /* BCMSPI_ANDROID */
171 
172 /* Register with Linux for interrupts */
173 int
spi_register_irq(sdioh_info_t * sd,uint irq)174 spi_register_irq(sdioh_info_t *sd, uint irq)
175 {
176 #ifndef BCMSPI_ANDROID
177 	sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
178 	if (request_irq(irq, sdspi_isr, IRQF_SHARED, "bcmsdspi", sd) < 0) {
179 		sd_err(("%s: request_irq() failed\n", __FUNCTION__));
180 		return ERROR;
181 	}
182 #endif /* !BCMSPI_ANDROID */
183 	return SUCCESS;
184 }
185 
186 /* Free Linux irq */
187 void
spi_free_irq(uint irq,sdioh_info_t * sd)188 spi_free_irq(uint irq, sdioh_info_t *sd)
189 {
190 #ifndef BCMSPI_ANDROID
191 	free_irq(irq, sd);
192 #endif /* !BCMSPI_ANDROID */
193 }
194 
195 /* Map Host controller registers */
196 #ifndef BCMSPI_ANDROID
197 uint32 *
spi_reg_map(osl_t * osh,uintptr addr,int size)198 spi_reg_map(osl_t *osh, uintptr addr, int size)
199 {
200 	return (uint32 *)REG_MAP(addr, size);
201 }
202 
203 void
spi_reg_unmap(osl_t * osh,uintptr addr,int size)204 spi_reg_unmap(osl_t *osh, uintptr addr, int size)
205 {
206 	REG_UNMAP((void*)(uintptr)addr);
207 }
208 #endif /* !BCMSPI_ANDROID */
209 
210 int
spi_osinit(sdioh_info_t * sd)211 spi_osinit(sdioh_info_t *sd)
212 {
213 	struct sdos_info *sdos;
214 
215 	sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
216 	sd->sdos_info = (void*)sdos;
217 	if (sdos == NULL)
218 		return BCME_NOMEM;
219 
220 	sdos->sd = sd;
221 	spin_lock_init(&sdos->lock);
222 #ifndef BCMSPI_ANDROID
223 	init_waitqueue_head(&sdos->intr_wait_queue);
224 #endif /* !BCMSPI_ANDROID */
225 	return BCME_OK;
226 }
227 
228 void
spi_osfree(sdioh_info_t * sd)229 spi_osfree(sdioh_info_t *sd)
230 {
231 	struct sdos_info *sdos;
232 	ASSERT(sd && sd->sdos_info);
233 
234 	sdos = (struct sdos_info *)sd->sdos_info;
235 	MFREE(sd->osh, sdos, sizeof(struct sdos_info));
236 }
237 
238 /* Interrupt enable/disable */
239 SDIOH_API_RC
sdioh_interrupt_set(sdioh_info_t * sd,bool enable)240 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
241 {
242 	ulong flags;
243 	struct sdos_info *sdos;
244 
245 	sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
246 
247 	sdos = (struct sdos_info *)sd->sdos_info;
248 	ASSERT(sdos);
249 
250 	if (!(sd->host_init_done && sd->card_init_done)) {
251 		sd_err(("%s: Card & Host are not initted - bailing\n", __FUNCTION__));
252 		return SDIOH_API_RC_FAIL;
253 	}
254 
255 #ifndef BCMSPI_ANDROID
256 	if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
257 		sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
258 		return SDIOH_API_RC_FAIL;
259 	}
260 #endif /* !BCMSPI_ANDROID */
261 
262 	/* Ensure atomicity for enable/disable calls */
263 	spin_lock_irqsave(&sdos->lock, flags);
264 
265 	sd->client_intr_enabled = enable;
266 #ifndef BCMSPI_ANDROID
267 	if (enable && !sd->lockcount)
268 		spi_devintr_on(sd);
269 	else
270 		spi_devintr_off(sd);
271 #endif /* !BCMSPI_ANDROID */
272 
273 	spin_unlock_irqrestore(&sdos->lock, flags);
274 
275 	return SDIOH_API_RC_SUCCESS;
276 }
277 
278 /* Protect against reentrancy (disable device interrupts while executing) */
279 void
spi_lock(sdioh_info_t * sd)280 spi_lock(sdioh_info_t *sd)
281 {
282 	ulong flags;
283 	struct sdos_info *sdos;
284 
285 	sdos = (struct sdos_info *)sd->sdos_info;
286 	ASSERT(sdos);
287 
288 	sd_trace(("%s: %d\n", __FUNCTION__, sd->lockcount));
289 
290 	spin_lock_irqsave(&sdos->lock, flags);
291 	if (sd->lockcount) {
292 		sd_err(("%s: Already locked!\n", __FUNCTION__));
293 		ASSERT(sd->lockcount == 0);
294 	}
295 #ifdef BCMSPI_ANDROID
296 	if (sd->client_intr_enabled)
297 		bcmsdh_oob_intr_set(0);
298 #else
299 	spi_devintr_off(sd);
300 #endif /* BCMSPI_ANDROID */
301 	sd->lockcount++;
302 	spin_unlock_irqrestore(&sdos->lock, flags);
303 }
304 
305 /* Enable client interrupt */
306 void
spi_unlock(sdioh_info_t * sd)307 spi_unlock(sdioh_info_t *sd)
308 {
309 	ulong flags;
310 	struct sdos_info *sdos;
311 
312 	sd_trace(("%s: %d, %d\n", __FUNCTION__, sd->lockcount, sd->client_intr_enabled));
313 	ASSERT(sd->lockcount > 0);
314 
315 	sdos = (struct sdos_info *)sd->sdos_info;
316 	ASSERT(sdos);
317 
318 	spin_lock_irqsave(&sdos->lock, flags);
319 	if (--sd->lockcount == 0 && sd->client_intr_enabled) {
320 #ifdef BCMSPI_ANDROID
321 		bcmsdh_oob_intr_set(1);
322 #else
323 		spi_devintr_on(sd);
324 #endif /* BCMSPI_ANDROID */
325 	}
326 	spin_unlock_irqrestore(&sdos->lock, flags);
327 }
328 
329 #ifndef BCMSPI_ANDROID
spi_waitbits(sdioh_info_t * sd,bool yield)330 void spi_waitbits(sdioh_info_t *sd, bool yield)
331 {
332 #ifndef BCMSDYIELD
333 	ASSERT(!yield);
334 #endif
335 	sd_trace(("%s: yield %d canblock %d\n",
336 	          __FUNCTION__, yield, BLOCKABLE()));
337 
338 	/* Clear the "interrupt happened" flag and last intrstatus */
339 	sd->got_hcint = FALSE;
340 
341 #ifdef BCMSDYIELD
342 	if (yield && BLOCKABLE()) {
343 		struct sdos_info *sdos;
344 		sdos = (struct sdos_info *)sd->sdos_info;
345 		/* Wait for the indication, the interrupt will be masked when the ISR fires. */
346 		wait_event_interruptible(sdos->intr_wait_queue, (sd->got_hcint));
347 	} else
348 #endif /* BCMSDYIELD */
349 	{
350 		spi_spinbits(sd);
351 	}
352 
353 }
354 #else /* !BCMSPI_ANDROID */
355 int bcmgspi_dump = 0;		/* Set to dump complete trace of all SPI bus transactions */
356 
357 static void
hexdump(char * pfx,unsigned char * msg,int msglen)358 hexdump(char *pfx, unsigned char *msg, int msglen)
359 {
360 	int i, col;
361 	char buf[80];
362 
363 	ASSERT(strlen(pfx) + 49 <= sizeof(buf));
364 
365 	col = 0;
366 
367 	for (i = 0; i < msglen; i++, col++) {
368 		if (col % 16 == 0)
369 			strcpy(buf, pfx);
370 		sprintf(buf + strlen(buf), "%02x", msg[i]);
371 		if ((col + 1) % 16 == 0)
372 			printf("%s\n", buf);
373 		else
374 			sprintf(buf + strlen(buf), " ");
375 	}
376 
377 	if (col % 16 != 0)
378 		printf("%s\n", buf);
379 }
380 
381 /* Send/Receive an SPI Packet */
382 void
spi_sendrecv(sdioh_info_t * sd,uint8 * msg_out,uint8 * msg_in,int msglen)383 spi_sendrecv(sdioh_info_t *sd, uint8 *msg_out, uint8 *msg_in, int msglen)
384 {
385 	int write = 0;
386 	int tx_len = 0;
387 	struct spi_message msg;
388 	struct spi_transfer t[2];
389 
390 	spi_message_init(&msg);
391 	memset(t, 0, 2*sizeof(struct spi_transfer));
392 
393 	if (sd->wordlen == 2)
394 #if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
395 		write = msg_out[2] & 0x80;	/* XXX bit 7: read:0, write :1 */
396 #else
397 		write = msg_out[1] & 0x80;	/* XXX bit 7: read:0, write :1 */
398 #endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
399 	if (sd->wordlen == 4)
400 #if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
401 		write = msg_out[0] & 0x80;	/* XXX bit 7: read:0, write :1 */
402 #else
403 		write = msg_out[3] & 0x80;	/* XXX bit 7: read:0, write :1 */
404 #endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
405 
406 	if (bcmgspi_dump) {
407 		hexdump(" OUT: ", msg_out, msglen);
408 	}
409 
410 	tx_len = write ? msglen-4 : 4;
411 
412 	sd_trace(("spi_sendrecv: %s, wordlen %d, cmd : 0x%02x 0x%02x 0x%02x 0x%02x\n",
413 		write ? "WR" : "RD", sd->wordlen,
414 		msg_out[0], msg_out[1], msg_out[2], msg_out[3]));
415 
416 	t[0].tx_buf = (char *)&msg_out[0];
417 	t[0].rx_buf = 0;
418 	t[0].len = tx_len;
419 
420 	spi_message_add_tail(&t[0], &msg);
421 
422 	t[1].rx_buf = (char *)&msg_in[tx_len];
423 	t[1].tx_buf = 0;
424 	t[1].len = msglen-tx_len;
425 
426 	spi_message_add_tail(&t[1], &msg);
427 	spi_sync(gBCMSPI, &msg);
428 
429 	if (bcmgspi_dump) {
430 		hexdump(" IN  : ", msg_in, msglen);
431 	}
432 }
433 #endif /* !BCMSPI_ANDROID */
434