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 int rsi_sdio_master_access_msword(struct rsi_hw *adapter, u16 ms_word) 31 { 32 u8 byte; 33 u8 function = 0; 34 int status = 0; 35 36 byte = (u8)(ms_word & 0x00FF); 37 38 rsi_dbg(INIT_ZONE, 39 "%s: MASTER_ACCESS_MSBYTE:0x%x\n", __func__, byte); 40 41 status = rsi_sdio_write_register(adapter, 42 function, 43 SDIO_MASTER_ACCESS_MSBYTE, 44 &byte); 45 if (status) { 46 rsi_dbg(ERR_ZONE, 47 "%s: fail to access MASTER_ACCESS_MSBYTE\n", 48 __func__); 49 return -1; 50 } 51 52 byte = (u8)(ms_word >> 8); 53 54 rsi_dbg(INIT_ZONE, "%s:MASTER_ACCESS_LSBYTE:0x%x\n", __func__, byte); 55 status = rsi_sdio_write_register(adapter, 56 function, 57 SDIO_MASTER_ACCESS_LSBYTE, 58 &byte); 59 return status; 60 } 61 62 /** 63 * rsi_process_pkt() - This Function reads rx_blocks register and figures out 64 * the size of the rx pkt. 65 * @common: Pointer to the driver private structure. 66 * 67 * Return: 0 on success, -1 on failure. 68 */ 69 static int rsi_process_pkt(struct rsi_common *common) 70 { 71 struct rsi_hw *adapter = common->priv; 72 struct rsi_91x_sdiodev *dev = 73 (struct rsi_91x_sdiodev *)adapter->rsi_dev; 74 u8 num_blks = 0; 75 u32 rcv_pkt_len = 0; 76 int status = 0; 77 u8 value = 0; 78 79 num_blks = ((adapter->interrupt_status & 1) | 80 ((adapter->interrupt_status >> RECV_NUM_BLOCKS) << 1)); 81 82 if (!num_blks) { 83 status = rsi_sdio_read_register(adapter, 84 SDIO_RX_NUM_BLOCKS_REG, 85 &value); 86 if (status) { 87 rsi_dbg(ERR_ZONE, 88 "%s: Failed to read pkt length from the card:\n", 89 __func__); 90 return status; 91 } 92 num_blks = value & 0x1f; 93 } 94 95 if (dev->write_fail == 2) 96 rsi_sdio_ack_intr(common->priv, (1 << MSDU_PKT_PENDING)); 97 98 if (unlikely(!num_blks)) { 99 dev->write_fail = 2; 100 return -1; 101 } 102 103 rcv_pkt_len = (num_blks * 256); 104 105 common->rx_data_pkt = kmalloc(rcv_pkt_len, GFP_KERNEL); 106 if (!common->rx_data_pkt) { 107 rsi_dbg(ERR_ZONE, "%s: Failed in memory allocation\n", 108 __func__); 109 return -ENOMEM; 110 } 111 112 status = rsi_sdio_host_intf_read_pkt(adapter, 113 common->rx_data_pkt, 114 rcv_pkt_len); 115 if (status) { 116 rsi_dbg(ERR_ZONE, "%s: Failed to read packet from card\n", 117 __func__); 118 goto fail; 119 } 120 121 status = rsi_read_pkt(common, rcv_pkt_len); 122 123 fail: 124 kfree(common->rx_data_pkt); 125 return status; 126 } 127 128 /** 129 * rsi_init_sdio_slave_regs() - This function does the actual initialization 130 * of SDBUS slave registers. 131 * @adapter: Pointer to the adapter structure. 132 * 133 * Return: status: 0 on success, -1 on failure. 134 */ 135 int rsi_init_sdio_slave_regs(struct rsi_hw *adapter) 136 { 137 struct rsi_91x_sdiodev *dev = 138 (struct rsi_91x_sdiodev *)adapter->rsi_dev; 139 u8 function = 0; 140 u8 byte; 141 int status = 0; 142 143 if (dev->next_read_delay) { 144 byte = dev->next_read_delay; 145 status = rsi_sdio_write_register(adapter, 146 function, 147 SDIO_NXT_RD_DELAY2, 148 &byte); 149 if (status) { 150 rsi_dbg(ERR_ZONE, 151 "%s: Failed to write SDIO_NXT_RD_DELAY2\n", 152 __func__); 153 return -1; 154 } 155 } 156 157 if (dev->sdio_high_speed_enable) { 158 rsi_dbg(INIT_ZONE, "%s: Enabling SDIO High speed\n", __func__); 159 byte = 0x3; 160 161 status = rsi_sdio_write_register(adapter, 162 function, 163 SDIO_REG_HIGH_SPEED, 164 &byte); 165 if (status) { 166 rsi_dbg(ERR_ZONE, 167 "%s: Failed to enable SDIO high speed\n", 168 __func__); 169 return -1; 170 } 171 } 172 173 /* This tells SDIO FIFO when to start read to host */ 174 rsi_dbg(INIT_ZONE, "%s: Initialzing SDIO read start level\n", __func__); 175 byte = 0x24; 176 177 status = rsi_sdio_write_register(adapter, 178 function, 179 SDIO_READ_START_LVL, 180 &byte); 181 if (status) { 182 rsi_dbg(ERR_ZONE, 183 "%s: Failed to write SDIO_READ_START_LVL\n", __func__); 184 return -1; 185 } 186 187 rsi_dbg(INIT_ZONE, "%s: Initialzing FIFO ctrl registers\n", __func__); 188 byte = (128 - 32); 189 190 status = rsi_sdio_write_register(adapter, 191 function, 192 SDIO_READ_FIFO_CTL, 193 &byte); 194 if (status) { 195 rsi_dbg(ERR_ZONE, 196 "%s: Failed to write SDIO_READ_FIFO_CTL\n", __func__); 197 return -1; 198 } 199 200 byte = 32; 201 status = rsi_sdio_write_register(adapter, 202 function, 203 SDIO_WRITE_FIFO_CTL, 204 &byte); 205 if (status) { 206 rsi_dbg(ERR_ZONE, 207 "%s: Failed to write SDIO_WRITE_FIFO_CTL\n", __func__); 208 return -1; 209 } 210 211 return 0; 212 } 213 214 /** 215 * rsi_interrupt_handler() - This function read and process SDIO interrupts. 216 * @adapter: Pointer to the adapter structure. 217 * 218 * Return: None. 219 */ 220 void rsi_interrupt_handler(struct rsi_hw *adapter) 221 { 222 struct rsi_common *common = adapter->priv; 223 struct rsi_91x_sdiodev *dev = 224 (struct rsi_91x_sdiodev *)adapter->rsi_dev; 225 int status; 226 enum sdio_interrupt_type isr_type; 227 u8 isr_status = 0; 228 u8 fw_status = 0; 229 230 dev->rx_info.sdio_int_counter++; 231 232 do { 233 mutex_lock(&common->rx_lock); 234 status = rsi_sdio_read_register(common->priv, 235 RSI_FN1_INT_REGISTER, 236 &isr_status); 237 if (status) { 238 rsi_dbg(ERR_ZONE, 239 "%s: Failed to Read Intr Status Register\n", 240 __func__); 241 mutex_unlock(&common->rx_lock); 242 return; 243 } 244 adapter->interrupt_status = isr_status; 245 246 if (isr_status == 0) { 247 rsi_set_event(&common->tx_thread.event); 248 dev->rx_info.sdio_intr_status_zero++; 249 mutex_unlock(&common->rx_lock); 250 return; 251 } 252 253 rsi_dbg(ISR_ZONE, "%s: Intr_status = %x %d %d\n", 254 __func__, isr_status, (1 << MSDU_PKT_PENDING), 255 (1 << FW_ASSERT_IND)); 256 257 do { 258 RSI_GET_SDIO_INTERRUPT_TYPE(isr_status, isr_type); 259 260 switch (isr_type) { 261 case BUFFER_AVAILABLE: 262 status = rsi_sdio_check_buffer_status(adapter, 263 0); 264 if (status < 0) 265 rsi_dbg(ERR_ZONE, 266 "%s: Failed to check buffer status\n", 267 __func__); 268 rsi_sdio_ack_intr(common->priv, 269 (1 << PKT_BUFF_AVAILABLE)); 270 rsi_set_event(&common->tx_thread.event); 271 272 rsi_dbg(ISR_ZONE, 273 "%s: ==> BUFFER_AVAILABLE <==\n", 274 __func__); 275 dev->buff_status_updated = true; 276 break; 277 278 case FIRMWARE_ASSERT_IND: 279 rsi_dbg(ERR_ZONE, 280 "%s: ==> FIRMWARE Assert <==\n", 281 __func__); 282 status = rsi_sdio_read_register(common->priv, 283 SDIO_FW_STATUS_REG, 284 &fw_status); 285 if (status) { 286 rsi_dbg(ERR_ZONE, 287 "%s: Failed to read f/w reg\n", 288 __func__); 289 } else { 290 rsi_dbg(ERR_ZONE, 291 "%s: Firmware Status is 0x%x\n", 292 __func__ , fw_status); 293 rsi_sdio_ack_intr(common->priv, 294 (1 << FW_ASSERT_IND)); 295 } 296 297 common->fsm_state = FSM_CARD_NOT_READY; 298 break; 299 300 case MSDU_PACKET_PENDING: 301 rsi_dbg(ISR_ZONE, "Pkt pending interrupt\n"); 302 dev->rx_info.total_sdio_msdu_pending_intr++; 303 304 status = rsi_process_pkt(common); 305 if (status) { 306 rsi_dbg(ERR_ZONE, 307 "%s: Failed to read pkt\n", 308 __func__); 309 mutex_unlock(&common->rx_lock); 310 return; 311 } 312 break; 313 default: 314 rsi_sdio_ack_intr(common->priv, isr_status); 315 dev->rx_info.total_sdio_unknown_intr++; 316 isr_status = 0; 317 rsi_dbg(ISR_ZONE, 318 "Unknown Interrupt %x\n", 319 isr_status); 320 break; 321 } 322 isr_status ^= BIT(isr_type - 1); 323 } while (isr_status); 324 mutex_unlock(&common->rx_lock); 325 } while (1); 326 } 327 328 /* This function is used to read buffer status register and 329 * set relevant fields in rsi_91x_sdiodev struct. 330 */ 331 int rsi_sdio_check_buffer_status(struct rsi_hw *adapter, u8 q_num) 332 { 333 struct rsi_common *common = adapter->priv; 334 struct rsi_91x_sdiodev *dev = 335 (struct rsi_91x_sdiodev *)adapter->rsi_dev; 336 u8 buf_status = 0; 337 int status = 0; 338 static int counter = 4; 339 340 if (!dev->buff_status_updated && counter) { 341 counter--; 342 goto out; 343 } 344 345 dev->buff_status_updated = false; 346 status = rsi_sdio_read_register(common->priv, 347 RSI_DEVICE_BUFFER_STATUS_REGISTER, 348 &buf_status); 349 350 if (status) { 351 rsi_dbg(ERR_ZONE, 352 "%s: Failed to read status register\n", __func__); 353 return -1; 354 } 355 356 if (buf_status & (BIT(PKT_MGMT_BUFF_FULL))) { 357 if (!dev->rx_info.mgmt_buffer_full) 358 dev->rx_info.mgmt_buf_full_counter++; 359 dev->rx_info.mgmt_buffer_full = true; 360 } else { 361 dev->rx_info.mgmt_buffer_full = false; 362 } 363 364 if (buf_status & (BIT(PKT_BUFF_FULL))) { 365 if (!dev->rx_info.buffer_full) 366 dev->rx_info.buf_full_counter++; 367 dev->rx_info.buffer_full = true; 368 } else { 369 dev->rx_info.buffer_full = false; 370 } 371 372 if (buf_status & (BIT(PKT_BUFF_SEMI_FULL))) { 373 if (!dev->rx_info.semi_buffer_full) 374 dev->rx_info.buf_semi_full_counter++; 375 dev->rx_info.semi_buffer_full = true; 376 } else { 377 dev->rx_info.semi_buffer_full = false; 378 } 379 380 if (dev->rx_info.mgmt_buffer_full || dev->rx_info.buf_full_counter) 381 counter = 1; 382 else 383 counter = 4; 384 385 out: 386 if ((q_num == MGMT_SOFT_Q) && (dev->rx_info.mgmt_buffer_full)) 387 return QUEUE_FULL; 388 389 if ((q_num < MGMT_SOFT_Q) && (dev->rx_info.buffer_full)) 390 return QUEUE_FULL; 391 392 return QUEUE_NOT_FULL; 393 } 394 395 /** 396 * rsi_sdio_determine_event_timeout() - This Function determines the event 397 * timeout duration. 398 * @adapter: Pointer to the adapter structure. 399 * 400 * Return: timeout duration is returned. 401 */ 402 int rsi_sdio_determine_event_timeout(struct rsi_hw *adapter) 403 { 404 struct rsi_91x_sdiodev *dev = 405 (struct rsi_91x_sdiodev *)adapter->rsi_dev; 406 407 /* Once buffer full is seen, event timeout to occur every 2 msecs */ 408 if (dev->rx_info.buffer_full) 409 return 2; 410 411 return EVENT_WAIT_FOREVER; 412 } 413