1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
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
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  */
17 
18 #include <linux/firmware.h>
19 #include "rsi_sdio.h"
20 #include "rsi_common.h"
21 
22 /**
23  * rsi_sdio_master_access_msword() - This function sets the AHB master access
24  *				     MS word in the SDIO slave registers.
25  * @adapter: Pointer to the adapter structure.
26  * @ms_word: ms word need to be initialized.
27  *
28  * Return: status: 0 on success, -1 on failure.
29  */
30 static int rsi_sdio_master_access_msword(struct rsi_hw *adapter,
31 					 u16 ms_word)
32 {
33 	u8 byte;
34 	u8 function = 0;
35 	int status = 0;
36 
37 	byte = (u8)(ms_word & 0x00FF);
38 
39 	rsi_dbg(INIT_ZONE,
40 		"%s: MASTER_ACCESS_MSBYTE:0x%x\n", __func__, byte);
41 
42 	status = rsi_sdio_write_register(adapter,
43 					 function,
44 					 SDIO_MASTER_ACCESS_MSBYTE,
45 					 &byte);
46 	if (status) {
47 		rsi_dbg(ERR_ZONE,
48 			"%s: fail to access MASTER_ACCESS_MSBYTE\n",
49 			__func__);
50 		return -1;
51 	}
52 
53 	byte = (u8)(ms_word >> 8);
54 
55 	rsi_dbg(INIT_ZONE, "%s:MASTER_ACCESS_LSBYTE:0x%x\n", __func__, byte);
56 	status = rsi_sdio_write_register(adapter,
57 					 function,
58 					 SDIO_MASTER_ACCESS_LSBYTE,
59 					 &byte);
60 	return status;
61 }
62 
63 /**
64  * rsi_copy_to_card() - This function includes the actual funtionality of
65  *			copying the TA firmware to the card.Basically this
66  *			function includes opening the TA file,reading the
67  *			TA file and writing their values in blocks of data.
68  * @common: Pointer to the driver private structure.
69  * @fw: Pointer to the firmware value to be written.
70  * @len: length of firmware file.
71  * @num_blocks: Number of blocks to be written to the card.
72  *
73  * Return: 0 on success and -1 on failure.
74  */
75 static int rsi_copy_to_card(struct rsi_common *common,
76 			    const u8 *fw,
77 			    u32 len,
78 			    u32 num_blocks)
79 {
80 	struct rsi_hw *adapter = common->priv;
81 	struct rsi_91x_sdiodev *dev =
82 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
83 	u32 indx, ii;
84 	u32 block_size = dev->tx_blk_size;
85 	u32 lsb_address;
86 	__le32 data[] = { TA_HOLD_THREAD_VALUE, TA_SOFT_RST_CLR,
87 			  TA_PC_ZERO, TA_RELEASE_THREAD_VALUE };
88 	u32 address[] = { TA_HOLD_THREAD_REG, TA_SOFT_RESET_REG,
89 			  TA_TH0_PC_REG, TA_RELEASE_THREAD_REG };
90 	u32 base_address;
91 	u16 msb_address;
92 
93 	base_address = TA_LOAD_ADDRESS;
94 	msb_address = base_address >> 16;
95 
96 	for (indx = 0, ii = 0; ii < num_blocks; ii++, indx += block_size) {
97 		lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
98 		if (rsi_sdio_write_register_multiple(adapter,
99 						     lsb_address,
100 						     (u8 *)(fw + indx),
101 						     block_size)) {
102 			rsi_dbg(ERR_ZONE,
103 				"%s: Unable to load %s blk\n", __func__,
104 				FIRMWARE_RSI9113);
105 			return -1;
106 		}
107 		rsi_dbg(INIT_ZONE, "%s: loading block: %d\n", __func__, ii);
108 		base_address += block_size;
109 		if ((base_address >> 16) != msb_address) {
110 			msb_address += 1;
111 			if (rsi_sdio_master_access_msword(adapter,
112 							  msb_address)) {
113 				rsi_dbg(ERR_ZONE,
114 					"%s: Unable to set ms word reg\n",
115 					__func__);
116 				return -1;
117 			}
118 		}
119 	}
120 
121 	if (len % block_size) {
122 		lsb_address = ((u16) base_address | RSI_SD_REQUEST_MASTER);
123 		if (rsi_sdio_write_register_multiple(adapter,
124 						     lsb_address,
125 						     (u8 *)(fw + indx),
126 						     len % block_size)) {
127 			rsi_dbg(ERR_ZONE,
128 				"%s: Unable to load f/w\n", __func__);
129 			return -1;
130 		}
131 	}
132 	rsi_dbg(INIT_ZONE,
133 		"%s: Succesfully loaded TA instructions\n", __func__);
134 
135 	if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
136 		rsi_dbg(ERR_ZONE,
137 			"%s: Unable to set ms word to common reg\n",
138 			__func__);
139 		return -1;
140 	}
141 
142 	for (ii = 0; ii < ARRAY_SIZE(data); ii++) {
143 		/* Bringing TA out of reset */
144 		if (rsi_sdio_write_register_multiple(adapter,
145 						     (address[ii] |
146 						     RSI_SD_REQUEST_MASTER),
147 						     (u8 *)&data[ii],
148 						     4)) {
149 			rsi_dbg(ERR_ZONE,
150 				"%s: Unable to hold TA threads\n", __func__);
151 			return -1;
152 		}
153 	}
154 
155 	rsi_dbg(INIT_ZONE, "%s: loaded firmware\n", __func__);
156 	return 0;
157 }
158 
159 /**
160  * rsi_load_ta_instructions() - This function includes the actual funtionality
161  *				of loading the TA firmware.This function also
162  *				includes opening the TA file,reading the TA
163  *				file and writing their value in blocks of data.
164  * @common: Pointer to the driver private structure.
165  *
166  * Return: status: 0 on success, -1 on failure.
167  */
168 static int rsi_load_ta_instructions(struct rsi_common *common)
169 {
170 	struct rsi_hw *adapter = common->priv;
171 	struct rsi_91x_sdiodev *dev =
172 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
173 	u32 len;
174 	u32 num_blocks;
175 	const struct firmware *fw_entry = NULL;
176 	u32 block_size = dev->tx_blk_size;
177 	int status = 0;
178 	u32 base_address;
179 	u16 msb_address;
180 
181 	if (rsi_sdio_master_access_msword(adapter, TA_BASE_ADDR)) {
182 		rsi_dbg(ERR_ZONE,
183 			"%s: Unable to set ms word to common reg\n",
184 			__func__);
185 		return -1;
186 	}
187 	base_address = TA_LOAD_ADDRESS;
188 	msb_address = (base_address >> 16);
189 
190 	if (rsi_sdio_master_access_msword(adapter, msb_address)) {
191 		rsi_dbg(ERR_ZONE,
192 			"%s: Unable to set ms word reg\n", __func__);
193 		return -1;
194 	}
195 
196 	status = request_firmware(&fw_entry, FIRMWARE_RSI9113, adapter->device);
197 	if (status < 0) {
198 		rsi_dbg(ERR_ZONE, "%s Firmware file %s not found\n",
199 			__func__, FIRMWARE_RSI9113);
200 		return status;
201 	}
202 
203 	len = fw_entry->size;
204 
205 	if (len % 4)
206 		len += (4 - (len % 4));
207 
208 	num_blocks = (len / block_size);
209 
210 	rsi_dbg(INIT_ZONE, "%s: Instruction size:%d\n", __func__, len);
211 	rsi_dbg(INIT_ZONE, "%s: num blocks: %d\n", __func__, num_blocks);
212 
213 	status = rsi_copy_to_card(common, fw_entry->data, len, num_blocks);
214 	release_firmware(fw_entry);
215 	return status;
216 }
217 
218 /**
219  * rsi_process_pkt() - This Function reads rx_blocks register and figures out
220  *		       the size of the rx pkt.
221  * @common: Pointer to the driver private structure.
222  *
223  * Return: 0 on success, -1 on failure.
224  */
225 static int rsi_process_pkt(struct rsi_common *common)
226 {
227 	struct rsi_hw *adapter = common->priv;
228 	u8 num_blks = 0;
229 	u32 rcv_pkt_len = 0;
230 	int status = 0;
231 
232 	status = rsi_sdio_read_register(adapter,
233 					SDIO_RX_NUM_BLOCKS_REG,
234 					&num_blks);
235 
236 	if (status) {
237 		rsi_dbg(ERR_ZONE,
238 			"%s: Failed to read pkt length from the card:\n",
239 			__func__);
240 		return status;
241 	}
242 	rcv_pkt_len = (num_blks * 256);
243 
244 	common->rx_data_pkt = kmalloc(rcv_pkt_len, GFP_KERNEL);
245 	if (!common->rx_data_pkt) {
246 		rsi_dbg(ERR_ZONE, "%s: Failed in memory allocation\n",
247 			__func__);
248 		return -ENOMEM;
249 	}
250 
251 	status = rsi_sdio_host_intf_read_pkt(adapter,
252 					     common->rx_data_pkt,
253 					     rcv_pkt_len);
254 	if (status) {
255 		rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n",
256 			__func__);
257 		goto fail;
258 	}
259 
260 	status = rsi_read_pkt(common, rcv_pkt_len);
261 
262 fail:
263 	kfree(common->rx_data_pkt);
264 	return status;
265 }
266 
267 /**
268  * rsi_init_sdio_slave_regs() - This function does the actual initialization
269  *				of SDBUS slave registers.
270  * @adapter: Pointer to the adapter structure.
271  *
272  * Return: status: 0 on success, -1 on failure.
273  */
274 int rsi_init_sdio_slave_regs(struct rsi_hw *adapter)
275 {
276 	struct rsi_91x_sdiodev *dev =
277 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
278 	u8 function = 0;
279 	u8 byte;
280 	int status = 0;
281 
282 	if (dev->next_read_delay) {
283 		byte = dev->next_read_delay;
284 		status = rsi_sdio_write_register(adapter,
285 						 function,
286 						 SDIO_NXT_RD_DELAY2,
287 						 &byte);
288 		if (status) {
289 			rsi_dbg(ERR_ZONE,
290 				"%s: Failed to write SDIO_NXT_RD_DELAY2\n",
291 				__func__);
292 			return -1;
293 		}
294 	}
295 
296 	if (dev->sdio_high_speed_enable) {
297 		rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__);
298 		byte = 0x3;
299 
300 		status = rsi_sdio_write_register(adapter,
301 						 function,
302 						 SDIO_REG_HIGH_SPEED,
303 						 &byte);
304 		if (status) {
305 			rsi_dbg(ERR_ZONE,
306 				"%s: Failed to enable SDIO high speed\n",
307 				__func__);
308 			return -1;
309 		}
310 	}
311 
312 	/* This tells SDIO FIFO when to start read to host */
313 	rsi_dbg(INIT_ZONE, "%s: Initialzing SDIO read start level\n", __func__);
314 	byte = 0x24;
315 
316 	status = rsi_sdio_write_register(adapter,
317 					 function,
318 					 SDIO_READ_START_LVL,
319 					 &byte);
320 	if (status) {
321 		rsi_dbg(ERR_ZONE,
322 			"%s: Failed to write SDIO_READ_START_LVL\n", __func__);
323 		return -1;
324 	}
325 
326 	rsi_dbg(INIT_ZONE, "%s: Initialzing FIFO ctrl registers\n", __func__);
327 	byte = (128 - 32);
328 
329 	status = rsi_sdio_write_register(adapter,
330 					 function,
331 					 SDIO_READ_FIFO_CTL,
332 					 &byte);
333 	if (status) {
334 		rsi_dbg(ERR_ZONE,
335 			"%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__);
336 		return -1;
337 	}
338 
339 	byte = 32;
340 	status = rsi_sdio_write_register(adapter,
341 					 function,
342 					 SDIO_WRITE_FIFO_CTL,
343 					 &byte);
344 	if (status) {
345 		rsi_dbg(ERR_ZONE,
346 			"%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__);
347 		return -1;
348 	}
349 
350 	return 0;
351 }
352 
353 /**
354  * rsi_interrupt_handler() - This function read and process SDIO interrupts.
355  * @adapter: Pointer to the adapter structure.
356  *
357  * Return: None.
358  */
359 void rsi_interrupt_handler(struct rsi_hw *adapter)
360 {
361 	struct rsi_common *common = adapter->priv;
362 	struct rsi_91x_sdiodev *dev =
363 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
364 	int status;
365 	enum sdio_interrupt_type isr_type;
366 	u8 isr_status = 0;
367 	u8 fw_status = 0;
368 
369 	dev->rx_info.sdio_int_counter++;
370 
371 	do {
372 		mutex_lock(&common->tx_rxlock);
373 		status = rsi_sdio_read_register(common->priv,
374 						RSI_FN1_INT_REGISTER,
375 						&isr_status);
376 		if (status) {
377 			rsi_dbg(ERR_ZONE,
378 				"%s: Failed to Read Intr Status Register\n",
379 				__func__);
380 			mutex_unlock(&common->tx_rxlock);
381 			return;
382 		}
383 
384 		if (isr_status == 0) {
385 			rsi_set_event(&common->tx_thread.event);
386 			dev->rx_info.sdio_intr_status_zero++;
387 			mutex_unlock(&common->tx_rxlock);
388 			return;
389 		}
390 
391 		rsi_dbg(ISR_ZONE, "%s: Intr_status = %x %d %d\n",
392 			__func__, isr_status, (1 << MSDU_PKT_PENDING),
393 			(1 << FW_ASSERT_IND));
394 
395 		do {
396 			RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type);
397 
398 			switch (isr_type) {
399 			case BUFFER_AVAILABLE:
400 				dev->rx_info.watch_bufferfull_count = 0;
401 				dev->rx_info.buffer_full = false;
402 				dev->rx_info.semi_buffer_full = false;
403 				dev->rx_info.mgmt_buffer_full = false;
404 				rsi_sdio_ack_intr(common->priv,
405 						  (1 << PKT_BUFF_AVAILABLE));
406 				rsi_set_event(&common->tx_thread.event);
407 
408 				rsi_dbg(ISR_ZONE,
409 					"%s: ==> BUFFER_AVAILABLE <==\n",
410 					__func__);
411 				dev->rx_info.buf_available_counter++;
412 				break;
413 
414 			case FIRMWARE_ASSERT_IND:
415 				rsi_dbg(ERR_ZONE,
416 					"%s: ==> FIRMWARE Assert <==\n",
417 					__func__);
418 				status = rsi_sdio_read_register(common->priv,
419 							SDIO_FW_STATUS_REG,
420 							&fw_status);
421 				if (status) {
422 					rsi_dbg(ERR_ZONE,
423 						"%s: Failed to read f/w reg\n",
424 						__func__);
425 				} else {
426 					rsi_dbg(ERR_ZONE,
427 						"%s: Firmware Status is 0x%x\n",
428 						__func__ , fw_status);
429 					rsi_sdio_ack_intr(common->priv,
430 							  (1 << FW_ASSERT_IND));
431 				}
432 
433 				common->fsm_state = FSM_CARD_NOT_READY;
434 				break;
435 
436 			case MSDU_PACKET_PENDING:
437 				rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n");
438 				dev->rx_info.total_sdio_msdu_pending_intr++;
439 
440 				status = rsi_process_pkt(common);
441 				if (status) {
442 					rsi_dbg(ERR_ZONE,
443 						"%s: Failed to read pkt\n",
444 						__func__);
445 					mutex_unlock(&common->tx_rxlock);
446 					return;
447 				}
448 				break;
449 			default:
450 				rsi_sdio_ack_intr(common->priv, isr_status);
451 				dev->rx_info.total_sdio_unknown_intr++;
452 				isr_status = 0;
453 				rsi_dbg(ISR_ZONE,
454 					"Unknown Interrupt %x\n",
455 					isr_status);
456 				break;
457 			}
458 			isr_status ^= BIT(isr_type - 1);
459 		} while (isr_status);
460 		mutex_unlock(&common->tx_rxlock);
461 	} while (1);
462 }
463 
464 /**
465  * rsi_device_init() - This Function Initializes The HAL.
466  * @common: Pointer to the driver private structure.
467  *
468  * Return: 0 on success, -1 on failure.
469  */
470 int rsi_sdio_device_init(struct rsi_common *common)
471 {
472 	if (rsi_load_ta_instructions(common))
473 		return -1;
474 
475 	if (rsi_sdio_master_access_msword(common->priv, MISC_CFG_BASE_ADDR)) {
476 		rsi_dbg(ERR_ZONE, "%s: Unable to set ms word reg\n",
477 			__func__);
478 		return -1;
479 	}
480 	rsi_dbg(INIT_ZONE,
481 		"%s: Setting ms word to 0x41050000\n", __func__);
482 
483 	return 0;
484 }
485 
486 /**
487  * rsi_sdio_read_buffer_status_register() - This function is used to the read
488  *					    buffer status register and set
489  *					    relevant fields in
490  *					    rsi_91x_sdiodev struct.
491  * @adapter: Pointer to the driver hw structure.
492  * @q_num: The Q number whose status is to be found.
493  *
494  * Return: status: -1 on failure or else queue full/stop is indicated.
495  */
496 int rsi_sdio_read_buffer_status_register(struct rsi_hw *adapter, u8 q_num)
497 {
498 	struct rsi_common *common = adapter->priv;
499 	struct rsi_91x_sdiodev *dev =
500 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
501 	u8 buf_status = 0;
502 	int status = 0;
503 
504 	status = rsi_sdio_read_register(common->priv,
505 					RSI_DEVICE_BUFFER_STATUS_REGISTER,
506 					&buf_status);
507 
508 	if (status) {
509 		rsi_dbg(ERR_ZONE,
510 			"%s: Failed to read status register\n", __func__);
511 		return -1;
512 	}
513 
514 	if (buf_status & (BIT(PKT_MGMT_BUFF_FULL))) {
515 		if (!dev->rx_info.mgmt_buffer_full)
516 			dev->rx_info.mgmt_buf_full_counter++;
517 		dev->rx_info.mgmt_buffer_full = true;
518 	} else {
519 		dev->rx_info.mgmt_buffer_full = false;
520 	}
521 
522 	if (buf_status & (BIT(PKT_BUFF_FULL))) {
523 		if (!dev->rx_info.buffer_full)
524 			dev->rx_info.buf_full_counter++;
525 		dev->rx_info.buffer_full = true;
526 	} else {
527 		dev->rx_info.buffer_full = false;
528 	}
529 
530 	if (buf_status & (BIT(PKT_BUFF_SEMI_FULL))) {
531 		if (!dev->rx_info.semi_buffer_full)
532 			dev->rx_info.buf_semi_full_counter++;
533 		dev->rx_info.semi_buffer_full = true;
534 	} else {
535 		dev->rx_info.semi_buffer_full = false;
536 	}
537 
538 	if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full))
539 		return QUEUE_FULL;
540 
541 	if (dev->rx_info.buffer_full)
542 		return QUEUE_FULL;
543 
544 	return QUEUE_NOT_FULL;
545 }
546 
547 /**
548  * rsi_sdio_determine_event_timeout() - This Function determines the event
549  *					timeout duration.
550  * @adapter: Pointer to the adapter structure.
551  *
552  * Return: timeout duration is returned.
553  */
554 int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter)
555 {
556 	struct rsi_91x_sdiodev *dev =
557 		(struct rsi_91x_sdiodev *)adapter->rsi_dev;
558 
559 	/* Once buffer full is seen, event timeout to occur every 2 msecs */
560 	if (dev->rx_info.buffer_full)
561 		return 2;
562 
563 	return EVENT_WAIT_FOREVER;
564 }
565