Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 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 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/printk.h> |
| 21 | #include <linux/pci_ids.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/mmc/sdio.h> |
| 26 | #include <linux/mmc/sdio_func.h> |
| 27 | #include <linux/mmc/card.h> |
| 28 | #include <linux/semaphore.h> |
| 29 | #include <linux/firmware.h> |
Stephen Rothwell | b7a57e7 | 2011-10-12 21:35:07 +0200 | [diff] [blame] | 30 | #include <linux/module.h> |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 31 | #include <linux/bcma/bcma.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
| 33 | #include <defs.h> |
| 34 | #include <brcmu_wifi.h> |
| 35 | #include <brcmu_utils.h> |
| 36 | #include <brcm_hw_ids.h> |
| 37 | #include <soc.h> |
| 38 | #include "sdio_host.h" |
Franky Lin | a83369b | 2011-11-04 22:23:28 +0100 | [diff] [blame] | 39 | #include "sdio_chip.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 40 | |
| 41 | #define DCMD_RESP_TIMEOUT 2000 /* In milli second */ |
| 42 | |
| 43 | #ifdef BCMDBG |
| 44 | |
| 45 | #define BRCMF_TRAP_INFO_SIZE 80 |
| 46 | |
| 47 | #define CBUF_LEN (128) |
| 48 | |
| 49 | struct rte_log_le { |
| 50 | __le32 buf; /* Can't be pointer on (64-bit) hosts */ |
| 51 | __le32 buf_size; |
| 52 | __le32 idx; |
| 53 | char *_buf_compat; /* Redundant pointer for backward compat. */ |
| 54 | }; |
| 55 | |
| 56 | struct rte_console { |
| 57 | /* Virtual UART |
| 58 | * When there is no UART (e.g. Quickturn), |
| 59 | * the host should write a complete |
| 60 | * input line directly into cbuf and then write |
| 61 | * the length into vcons_in. |
| 62 | * This may also be used when there is a real UART |
| 63 | * (at risk of conflicting with |
| 64 | * the real UART). vcons_out is currently unused. |
| 65 | */ |
| 66 | uint vcons_in; |
| 67 | uint vcons_out; |
| 68 | |
| 69 | /* Output (logging) buffer |
| 70 | * Console output is written to a ring buffer log_buf at index log_idx. |
| 71 | * The host may read the output when it sees log_idx advance. |
| 72 | * Output will be lost if the output wraps around faster than the host |
| 73 | * polls. |
| 74 | */ |
| 75 | struct rte_log_le log_le; |
| 76 | |
| 77 | /* Console input line buffer |
| 78 | * Characters are read one at a time into cbuf |
| 79 | * until <CR> is received, then |
| 80 | * the buffer is processed as a command line. |
| 81 | * Also used for virtual UART. |
| 82 | */ |
| 83 | uint cbuf_idx; |
| 84 | char cbuf[CBUF_LEN]; |
| 85 | }; |
| 86 | |
| 87 | #endif /* BCMDBG */ |
| 88 | #include <chipcommon.h> |
| 89 | |
| 90 | #include "dhd.h" |
| 91 | #include "dhd_bus.h" |
| 92 | #include "dhd_proto.h" |
| 93 | #include "dhd_dbg.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 94 | |
| 95 | #define TXQLEN 2048 /* bulk tx queue length */ |
| 96 | #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */ |
| 97 | #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */ |
| 98 | #define PRIOMASK 7 |
| 99 | |
| 100 | #define TXRETRIES 2 /* # of retries for tx frames */ |
| 101 | |
| 102 | #define BRCMF_RXBOUND 50 /* Default for max rx frames in |
| 103 | one scheduling */ |
| 104 | |
| 105 | #define BRCMF_TXBOUND 20 /* Default for max tx frames in |
| 106 | one scheduling */ |
| 107 | |
| 108 | #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */ |
| 109 | |
| 110 | #define MEMBLOCK 2048 /* Block size used for downloading |
| 111 | of dongle image */ |
| 112 | #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold |
| 113 | biggest possible glom */ |
| 114 | |
| 115 | #define BRCMF_FIRSTREAD (1 << 6) |
| 116 | |
| 117 | |
| 118 | /* SBSDIO_DEVICE_CTL */ |
| 119 | |
| 120 | /* 1: device will assert busy signal when receiving CMD53 */ |
| 121 | #define SBSDIO_DEVCTL_SETBUSY 0x01 |
| 122 | /* 1: assertion of sdio interrupt is synchronous to the sdio clock */ |
| 123 | #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02 |
| 124 | /* 1: mask all interrupts to host except the chipActive (rev 8) */ |
| 125 | #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04 |
| 126 | /* 1: isolate internal sdio signals, put external pads in tri-state; requires |
| 127 | * sdio bus power cycle to clear (rev 9) */ |
| 128 | #define SBSDIO_DEVCTL_PADS_ISO 0x08 |
| 129 | /* Force SD->SB reset mapping (rev 11) */ |
| 130 | #define SBSDIO_DEVCTL_SB_RST_CTL 0x30 |
| 131 | /* Determined by CoreControl bit */ |
| 132 | #define SBSDIO_DEVCTL_RST_CORECTL 0x00 |
| 133 | /* Force backplane reset */ |
| 134 | #define SBSDIO_DEVCTL_RST_BPRESET 0x10 |
| 135 | /* Force no backplane reset */ |
| 136 | #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20 |
| 137 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 138 | /* direct(mapped) cis space */ |
| 139 | |
| 140 | /* MAPPED common CIS address */ |
| 141 | #define SBSDIO_CIS_BASE_COMMON 0x1000 |
| 142 | /* maximum bytes in one CIS */ |
| 143 | #define SBSDIO_CIS_SIZE_LIMIT 0x200 |
| 144 | /* cis offset addr is < 17 bits */ |
| 145 | #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF |
| 146 | |
| 147 | /* manfid tuple length, include tuple, link bytes */ |
| 148 | #define SBSDIO_CIS_MANFID_TUPLE_LEN 6 |
| 149 | |
| 150 | /* intstatus */ |
| 151 | #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */ |
| 152 | #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */ |
| 153 | #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */ |
| 154 | #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */ |
| 155 | #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */ |
| 156 | #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */ |
| 157 | #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */ |
| 158 | #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */ |
| 159 | #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */ |
| 160 | #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */ |
| 161 | #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */ |
| 162 | #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */ |
| 163 | #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */ |
| 164 | #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */ |
| 165 | #define I_PC (1 << 10) /* descriptor error */ |
| 166 | #define I_PD (1 << 11) /* data error */ |
| 167 | #define I_DE (1 << 12) /* Descriptor protocol Error */ |
| 168 | #define I_RU (1 << 13) /* Receive descriptor Underflow */ |
| 169 | #define I_RO (1 << 14) /* Receive fifo Overflow */ |
| 170 | #define I_XU (1 << 15) /* Transmit fifo Underflow */ |
| 171 | #define I_RI (1 << 16) /* Receive Interrupt */ |
| 172 | #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */ |
| 173 | #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */ |
| 174 | #define I_XI (1 << 24) /* Transmit Interrupt */ |
| 175 | #define I_RF_TERM (1 << 25) /* Read Frame Terminate */ |
| 176 | #define I_WF_TERM (1 << 26) /* Write Frame Terminate */ |
| 177 | #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */ |
| 178 | #define I_SBINT (1 << 28) /* sbintstatus Interrupt */ |
| 179 | #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */ |
| 180 | #define I_SRESET (1 << 30) /* CCCR RES interrupt */ |
| 181 | #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */ |
| 182 | #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU) |
| 183 | #define I_DMA (I_RI | I_XI | I_ERRORS) |
| 184 | |
| 185 | /* corecontrol */ |
| 186 | #define CC_CISRDY (1 << 0) /* CIS Ready */ |
| 187 | #define CC_BPRESEN (1 << 1) /* CCCR RES signal */ |
| 188 | #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */ |
| 189 | #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */ |
| 190 | #define CC_XMTDATAAVAIL_MODE (1 << 4) |
| 191 | #define CC_XMTDATAAVAIL_CTRL (1 << 5) |
| 192 | |
| 193 | /* SDA_FRAMECTRL */ |
| 194 | #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */ |
| 195 | #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */ |
| 196 | #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */ |
| 197 | #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */ |
| 198 | |
| 199 | /* HW frame tag */ |
| 200 | #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */ |
| 201 | |
| 202 | /* Total length of frame header for dongle protocol */ |
| 203 | #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN) |
| 204 | #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN) |
| 205 | |
| 206 | /* |
| 207 | * Software allocation of To SB Mailbox resources |
| 208 | */ |
| 209 | |
| 210 | /* tosbmailbox bits corresponding to intstatus bits */ |
| 211 | #define SMB_NAK (1 << 0) /* Frame NAK */ |
| 212 | #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */ |
| 213 | #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */ |
| 214 | #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */ |
| 215 | |
| 216 | /* tosbmailboxdata */ |
| 217 | #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */ |
| 218 | |
| 219 | /* |
| 220 | * Software allocation of To Host Mailbox resources |
| 221 | */ |
| 222 | |
| 223 | /* intstatus bits */ |
| 224 | #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */ |
| 225 | #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */ |
| 226 | #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */ |
| 227 | #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */ |
| 228 | |
| 229 | /* tohostmailboxdata */ |
| 230 | #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */ |
| 231 | #define HMB_DATA_DEVREADY 2 /* talk to host after enable */ |
| 232 | #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */ |
| 233 | #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */ |
| 234 | |
| 235 | #define HMB_DATA_FCDATA_MASK 0xff000000 |
| 236 | #define HMB_DATA_FCDATA_SHIFT 24 |
| 237 | |
| 238 | #define HMB_DATA_VERSION_MASK 0x00ff0000 |
| 239 | #define HMB_DATA_VERSION_SHIFT 16 |
| 240 | |
| 241 | /* |
| 242 | * Software-defined protocol header |
| 243 | */ |
| 244 | |
| 245 | /* Current protocol version */ |
| 246 | #define SDPCM_PROT_VERSION 4 |
| 247 | |
| 248 | /* SW frame header */ |
| 249 | #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff) |
| 250 | |
| 251 | #define SDPCM_CHANNEL_MASK 0x00000f00 |
| 252 | #define SDPCM_CHANNEL_SHIFT 8 |
| 253 | #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f) |
| 254 | |
| 255 | #define SDPCM_NEXTLEN_OFFSET 2 |
| 256 | |
| 257 | /* Data Offset from SOF (HW Tag, SW Tag, Pad) */ |
| 258 | #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */ |
| 259 | #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff) |
| 260 | #define SDPCM_DOFFSET_MASK 0xff000000 |
| 261 | #define SDPCM_DOFFSET_SHIFT 24 |
| 262 | #define SDPCM_FCMASK_OFFSET 4 /* Flow control */ |
| 263 | #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff) |
| 264 | #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */ |
| 265 | #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff) |
| 266 | |
| 267 | #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */ |
| 268 | |
| 269 | /* logical channel numbers */ |
| 270 | #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */ |
| 271 | #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */ |
| 272 | #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */ |
| 273 | #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */ |
| 274 | #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */ |
| 275 | |
| 276 | #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */ |
| 277 | |
| 278 | #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80) |
| 279 | |
| 280 | /* |
| 281 | * Shared structure between dongle and the host. |
| 282 | * The structure contains pointers to trap or assert information. |
| 283 | */ |
| 284 | #define SDPCM_SHARED_VERSION 0x0002 |
| 285 | #define SDPCM_SHARED_VERSION_MASK 0x00FF |
| 286 | #define SDPCM_SHARED_ASSERT_BUILT 0x0100 |
| 287 | #define SDPCM_SHARED_ASSERT 0x0200 |
| 288 | #define SDPCM_SHARED_TRAP 0x0400 |
| 289 | |
| 290 | /* Space for header read, limit for data packets */ |
| 291 | #define MAX_HDR_READ (1 << 6) |
| 292 | #define MAX_RX_DATASZ 2048 |
| 293 | |
| 294 | /* Maximum milliseconds to wait for F2 to come up */ |
| 295 | #define BRCMF_WAIT_F2RDY 3000 |
| 296 | |
| 297 | /* Bump up limit on waiting for HT to account for first startup; |
| 298 | * if the image is doing a CRC calculation before programming the PMU |
| 299 | * for HT availability, it could take a couple hundred ms more, so |
| 300 | * max out at a 1 second (1000000us). |
| 301 | */ |
| 302 | #undef PMU_MAX_TRANSITION_DLY |
| 303 | #define PMU_MAX_TRANSITION_DLY 1000000 |
| 304 | |
| 305 | /* Value for ChipClockCSR during initial setup */ |
| 306 | #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \ |
| 307 | SBSDIO_ALP_AVAIL_REQ) |
| 308 | |
| 309 | /* Flags for SDH calls */ |
| 310 | #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED) |
| 311 | |
Franky Lin | 8dd939c | 2011-11-22 17:21:41 -0800 | [diff] [blame] | 312 | #define BRCMFMAC_FW_NAME "brcm/brcmfmac.bin" |
| 313 | #define BRCMFMAC_NV_NAME "brcm/brcmfmac.txt" |
| 314 | MODULE_FIRMWARE(BRCMFMAC_FW_NAME); |
| 315 | MODULE_FIRMWARE(BRCMFMAC_NV_NAME); |
| 316 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 317 | /* |
| 318 | * Conversion of 802.1D priority to precedence level |
| 319 | */ |
| 320 | static uint prio2prec(u32 prio) |
| 321 | { |
| 322 | return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ? |
| 323 | (prio^2) : prio; |
| 324 | } |
| 325 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 326 | /* core registers */ |
| 327 | struct sdpcmd_regs { |
| 328 | u32 corecontrol; /* 0x00, rev8 */ |
| 329 | u32 corestatus; /* rev8 */ |
| 330 | u32 PAD[1]; |
| 331 | u32 biststatus; /* rev8 */ |
| 332 | |
| 333 | /* PCMCIA access */ |
| 334 | u16 pcmciamesportaladdr; /* 0x010, rev8 */ |
| 335 | u16 PAD[1]; |
| 336 | u16 pcmciamesportalmask; /* rev8 */ |
| 337 | u16 PAD[1]; |
| 338 | u16 pcmciawrframebc; /* rev8 */ |
| 339 | u16 PAD[1]; |
| 340 | u16 pcmciaunderflowtimer; /* rev8 */ |
| 341 | u16 PAD[1]; |
| 342 | |
| 343 | /* interrupt */ |
| 344 | u32 intstatus; /* 0x020, rev8 */ |
| 345 | u32 hostintmask; /* rev8 */ |
| 346 | u32 intmask; /* rev8 */ |
| 347 | u32 sbintstatus; /* rev8 */ |
| 348 | u32 sbintmask; /* rev8 */ |
| 349 | u32 funcintmask; /* rev4 */ |
| 350 | u32 PAD[2]; |
| 351 | u32 tosbmailbox; /* 0x040, rev8 */ |
| 352 | u32 tohostmailbox; /* rev8 */ |
| 353 | u32 tosbmailboxdata; /* rev8 */ |
| 354 | u32 tohostmailboxdata; /* rev8 */ |
| 355 | |
| 356 | /* synchronized access to registers in SDIO clock domain */ |
| 357 | u32 sdioaccess; /* 0x050, rev8 */ |
| 358 | u32 PAD[3]; |
| 359 | |
| 360 | /* PCMCIA frame control */ |
| 361 | u8 pcmciaframectrl; /* 0x060, rev8 */ |
| 362 | u8 PAD[3]; |
| 363 | u8 pcmciawatermark; /* rev8 */ |
| 364 | u8 PAD[155]; |
| 365 | |
| 366 | /* interrupt batching control */ |
| 367 | u32 intrcvlazy; /* 0x100, rev8 */ |
| 368 | u32 PAD[3]; |
| 369 | |
| 370 | /* counters */ |
| 371 | u32 cmd52rd; /* 0x110, rev8 */ |
| 372 | u32 cmd52wr; /* rev8 */ |
| 373 | u32 cmd53rd; /* rev8 */ |
| 374 | u32 cmd53wr; /* rev8 */ |
| 375 | u32 abort; /* rev8 */ |
| 376 | u32 datacrcerror; /* rev8 */ |
| 377 | u32 rdoutofsync; /* rev8 */ |
| 378 | u32 wroutofsync; /* rev8 */ |
| 379 | u32 writebusy; /* rev8 */ |
| 380 | u32 readwait; /* rev8 */ |
| 381 | u32 readterm; /* rev8 */ |
| 382 | u32 writeterm; /* rev8 */ |
| 383 | u32 PAD[40]; |
| 384 | u32 clockctlstatus; /* rev8 */ |
| 385 | u32 PAD[7]; |
| 386 | |
| 387 | u32 PAD[128]; /* DMA engines */ |
| 388 | |
| 389 | /* SDIO/PCMCIA CIS region */ |
| 390 | char cis[512]; /* 0x400-0x5ff, rev6 */ |
| 391 | |
| 392 | /* PCMCIA function control registers */ |
| 393 | char pcmciafcr[256]; /* 0x600-6ff, rev6 */ |
| 394 | u16 PAD[55]; |
| 395 | |
| 396 | /* PCMCIA backplane access */ |
| 397 | u16 backplanecsr; /* 0x76E, rev6 */ |
| 398 | u16 backplaneaddr0; /* rev6 */ |
| 399 | u16 backplaneaddr1; /* rev6 */ |
| 400 | u16 backplaneaddr2; /* rev6 */ |
| 401 | u16 backplaneaddr3; /* rev6 */ |
| 402 | u16 backplanedata0; /* rev6 */ |
| 403 | u16 backplanedata1; /* rev6 */ |
| 404 | u16 backplanedata2; /* rev6 */ |
| 405 | u16 backplanedata3; /* rev6 */ |
| 406 | u16 PAD[31]; |
| 407 | |
| 408 | /* sprom "size" & "blank" info */ |
| 409 | u16 spromstatus; /* 0x7BE, rev2 */ |
| 410 | u32 PAD[464]; |
| 411 | |
| 412 | u16 PAD[0x80]; |
| 413 | }; |
| 414 | |
| 415 | #ifdef BCMDBG |
| 416 | /* Device console log buffer state */ |
| 417 | struct brcmf_console { |
| 418 | uint count; /* Poll interval msec counter */ |
| 419 | uint log_addr; /* Log struct address (fixed) */ |
| 420 | struct rte_log_le log_le; /* Log struct (host copy) */ |
| 421 | uint bufsize; /* Size of log buffer */ |
| 422 | u8 *buf; /* Log buffer (host copy) */ |
| 423 | uint last; /* Last buffer read index */ |
| 424 | }; |
| 425 | #endif /* BCMDBG */ |
| 426 | |
| 427 | struct sdpcm_shared { |
| 428 | u32 flags; |
| 429 | u32 trap_addr; |
| 430 | u32 assert_exp_addr; |
| 431 | u32 assert_file_addr; |
| 432 | u32 assert_line; |
| 433 | u32 console_addr; /* Address of struct rte_console */ |
| 434 | u32 msgtrace_addr; |
| 435 | u8 tag[32]; |
| 436 | }; |
| 437 | |
| 438 | struct sdpcm_shared_le { |
| 439 | __le32 flags; |
| 440 | __le32 trap_addr; |
| 441 | __le32 assert_exp_addr; |
| 442 | __le32 assert_file_addr; |
| 443 | __le32 assert_line; |
| 444 | __le32 console_addr; /* Address of struct rte_console */ |
| 445 | __le32 msgtrace_addr; |
| 446 | u8 tag[32]; |
| 447 | }; |
| 448 | |
| 449 | |
| 450 | /* misc chip info needed by some of the routines */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 451 | /* Private data for SDIO bus interaction */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 452 | struct brcmf_sdio { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 453 | struct brcmf_pub *drvr; |
| 454 | |
| 455 | struct brcmf_sdio_dev *sdiodev; /* sdio device handler */ |
| 456 | struct chip_info *ci; /* Chip info struct */ |
| 457 | char *vars; /* Variables (from CIS and/or other) */ |
| 458 | uint varsz; /* Size of variables buffer */ |
| 459 | |
| 460 | u32 ramsize; /* Size of RAM in SOCRAM (bytes) */ |
| 461 | |
| 462 | u32 hostintmask; /* Copy of Host Interrupt Mask */ |
| 463 | u32 intstatus; /* Intstatus bits (events) pending */ |
| 464 | bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */ |
| 465 | bool fcstate; /* State of dongle flow-control */ |
| 466 | |
| 467 | uint blocksize; /* Block size of SDIO transfers */ |
| 468 | uint roundup; /* Max roundup limit */ |
| 469 | |
| 470 | struct pktq txq; /* Queue length used for flow-control */ |
| 471 | u8 flowcontrol; /* per prio flow control bitmask */ |
| 472 | u8 tx_seq; /* Transmit sequence number (next) */ |
| 473 | u8 tx_max; /* Maximum transmit sequence allowed */ |
| 474 | |
| 475 | u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN]; |
| 476 | u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */ |
| 477 | u16 nextlen; /* Next Read Len from last header */ |
| 478 | u8 rx_seq; /* Receive sequence number (expected) */ |
| 479 | bool rxskip; /* Skip receive (awaiting NAK ACK) */ |
| 480 | |
| 481 | uint rxbound; /* Rx frames to read before resched */ |
| 482 | uint txbound; /* Tx frames to send before resched */ |
| 483 | uint txminmax; |
| 484 | |
| 485 | struct sk_buff *glomd; /* Packet containing glomming descriptor */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 486 | struct sk_buff_head glom; /* Packet list for glommed superframe */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 487 | uint glomerr; /* Glom packet read errors */ |
| 488 | |
| 489 | u8 *rxbuf; /* Buffer for receiving control packets */ |
| 490 | uint rxblen; /* Allocated length of rxbuf */ |
| 491 | u8 *rxctl; /* Aligned pointer into rxbuf */ |
| 492 | u8 *databuf; /* Buffer for receiving big glom packet */ |
| 493 | u8 *dataptr; /* Aligned pointer into databuf */ |
| 494 | uint rxlen; /* Length of valid data in buffer */ |
| 495 | |
| 496 | u8 sdpcm_ver; /* Bus protocol reported by dongle */ |
| 497 | |
| 498 | bool intr; /* Use interrupts */ |
| 499 | bool poll; /* Use polling */ |
| 500 | bool ipend; /* Device interrupt is pending */ |
| 501 | uint intrcount; /* Count of device interrupt callbacks */ |
| 502 | uint lastintrs; /* Count as of last watchdog timer */ |
| 503 | uint spurious; /* Count of spurious interrupts */ |
| 504 | uint pollrate; /* Ticks between device polls */ |
| 505 | uint polltick; /* Tick counter */ |
| 506 | uint pollcnt; /* Count of active polls */ |
| 507 | |
| 508 | #ifdef BCMDBG |
| 509 | uint console_interval; |
| 510 | struct brcmf_console console; /* Console output polling support */ |
| 511 | uint console_addr; /* Console address from shared struct */ |
| 512 | #endif /* BCMDBG */ |
| 513 | |
| 514 | uint regfails; /* Count of R_REG failures */ |
| 515 | |
| 516 | uint clkstate; /* State of sd and backplane clock(s) */ |
| 517 | bool activity; /* Activity flag for clock down */ |
| 518 | s32 idletime; /* Control for activity timeout */ |
| 519 | s32 idlecount; /* Activity timeout counter */ |
| 520 | s32 idleclock; /* How to set bus driver when idle */ |
| 521 | s32 sd_rxchain; |
| 522 | bool use_rxchain; /* If brcmf should use PKT chains */ |
| 523 | bool sleeping; /* Is SDIO bus sleeping? */ |
| 524 | bool rxflow_mode; /* Rx flow control mode */ |
| 525 | bool rxflow; /* Is rx flow control on */ |
| 526 | bool alp_only; /* Don't use HT clock (ALP only) */ |
| 527 | /* Field to decide if rx of control frames happen in rxbuf or lb-pool */ |
| 528 | bool usebufpool; |
| 529 | |
| 530 | /* Some additional counters */ |
| 531 | uint tx_sderrs; /* Count of tx attempts with sd errors */ |
| 532 | uint fcqueued; /* Tx packets that got queued */ |
| 533 | uint rxrtx; /* Count of rtx requests (NAK to dongle) */ |
| 534 | uint rx_toolong; /* Receive frames too long to receive */ |
| 535 | uint rxc_errors; /* SDIO errors when reading control frames */ |
| 536 | uint rx_hdrfail; /* SDIO errors on header reads */ |
| 537 | uint rx_badhdr; /* Bad received headers (roosync?) */ |
| 538 | uint rx_badseq; /* Mismatched rx sequence number */ |
| 539 | uint fc_rcvd; /* Number of flow-control events received */ |
| 540 | uint fc_xoff; /* Number which turned on flow-control */ |
| 541 | uint fc_xon; /* Number which turned off flow-control */ |
| 542 | uint rxglomfail; /* Failed deglom attempts */ |
| 543 | uint rxglomframes; /* Number of glom frames (superframes) */ |
| 544 | uint rxglompkts; /* Number of packets from glom frames */ |
| 545 | uint f2rxhdrs; /* Number of header reads */ |
| 546 | uint f2rxdata; /* Number of frame data reads */ |
| 547 | uint f2txdata; /* Number of f2 frame writes */ |
| 548 | uint f1regdata; /* Number of f1 register accesses */ |
| 549 | |
| 550 | u8 *ctrl_frame_buf; |
| 551 | u32 ctrl_frame_len; |
| 552 | bool ctrl_frame_stat; |
| 553 | |
| 554 | spinlock_t txqlock; |
| 555 | wait_queue_head_t ctrl_wait; |
| 556 | wait_queue_head_t dcmd_resp_wait; |
| 557 | |
| 558 | struct timer_list timer; |
| 559 | struct completion watchdog_wait; |
| 560 | struct task_struct *watchdog_tsk; |
| 561 | bool wd_timer_valid; |
| 562 | uint save_ms; |
| 563 | |
| 564 | struct task_struct *dpc_tsk; |
| 565 | struct completion dpc_wait; |
| 566 | |
| 567 | struct semaphore sdsem; |
| 568 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 569 | const struct firmware *firmware; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 570 | u32 fw_ptr; |
| 571 | }; |
| 572 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 573 | /* clkstate */ |
| 574 | #define CLK_NONE 0 |
| 575 | #define CLK_SDONLY 1 |
| 576 | #define CLK_PENDING 2 /* Not used yet */ |
| 577 | #define CLK_AVAIL 3 |
| 578 | |
| 579 | #ifdef BCMDBG |
| 580 | static int qcount[NUMPRIO]; |
| 581 | static int tx_packets[NUMPRIO]; |
| 582 | #endif /* BCMDBG */ |
| 583 | |
| 584 | #define SDIO_DRIVE_STRENGTH 6 /* in milliamps */ |
| 585 | |
| 586 | #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL) |
| 587 | |
| 588 | /* Retry count for register access failures */ |
| 589 | static const uint retry_limit = 2; |
| 590 | |
| 591 | /* Limit on rounding up frames */ |
| 592 | static const uint max_roundup = 512; |
| 593 | |
| 594 | #define ALIGNMENT 4 |
| 595 | |
| 596 | static void pkt_align(struct sk_buff *p, int len, int align) |
| 597 | { |
| 598 | uint datalign; |
| 599 | datalign = (unsigned long)(p->data); |
| 600 | datalign = roundup(datalign, (align)) - datalign; |
| 601 | if (datalign) |
| 602 | skb_pull(p, datalign); |
| 603 | __skb_trim(p, len); |
| 604 | } |
| 605 | |
| 606 | /* To check if there's window offered */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 607 | static bool data_ok(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 608 | { |
| 609 | return (u8)(bus->tx_max - bus->tx_seq) != 0 && |
| 610 | ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Reads a register in the SDIO hardware block. This block occupies a series of |
| 615 | * adresses on the 32 bit backplane bus. |
| 616 | */ |
| 617 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 618 | r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 reg_offset, u32 *retryvar) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 619 | { |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 620 | u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 621 | *retryvar = 0; |
| 622 | do { |
| 623 | *regvar = brcmf_sdcard_reg_read(bus->sdiodev, |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 624 | bus->ci->c_inf[idx].base + reg_offset, |
| 625 | sizeof(u32)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 626 | } while (brcmf_sdcard_regfail(bus->sdiodev) && |
| 627 | (++(*retryvar) <= retry_limit)); |
| 628 | if (*retryvar) { |
| 629 | bus->regfails += (*retryvar-1); |
| 630 | if (*retryvar > retry_limit) { |
| 631 | brcmf_dbg(ERROR, "FAILED READ %Xh\n", reg_offset); |
| 632 | *regvar = 0; |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 638 | w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset, u32 *retryvar) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 639 | { |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 640 | u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 641 | *retryvar = 0; |
| 642 | do { |
| 643 | brcmf_sdcard_reg_write(bus->sdiodev, |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 644 | bus->ci->c_inf[idx].base + reg_offset, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 645 | sizeof(u32), regval); |
| 646 | } while (brcmf_sdcard_regfail(bus->sdiodev) && |
| 647 | (++(*retryvar) <= retry_limit)); |
| 648 | if (*retryvar) { |
| 649 | bus->regfails += (*retryvar-1); |
| 650 | if (*retryvar > retry_limit) |
| 651 | brcmf_dbg(ERROR, "FAILED REGISTER WRITE %Xh\n", |
| 652 | reg_offset); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND) |
| 657 | |
| 658 | #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE) |
| 659 | |
| 660 | /* Packet free applicable unconditionally for sdio and sdspi. |
| 661 | * Conditional if bufpool was present for gspi bus. |
| 662 | */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 663 | static void brcmf_sdbrcm_pktfree2(struct brcmf_sdio *bus, struct sk_buff *pkt) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 664 | { |
| 665 | if (bus->usebufpool) |
| 666 | brcmu_pkt_buf_free_skb(pkt); |
| 667 | } |
| 668 | |
| 669 | /* Turn backplane clock on or off */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 670 | static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 671 | { |
| 672 | int err; |
| 673 | u8 clkctl, clkreq, devctl; |
| 674 | unsigned long timeout; |
| 675 | |
| 676 | brcmf_dbg(TRACE, "Enter\n"); |
| 677 | |
| 678 | clkctl = 0; |
| 679 | |
| 680 | if (on) { |
| 681 | /* Request HT Avail */ |
| 682 | clkreq = |
| 683 | bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ; |
| 684 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 685 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 686 | SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err); |
| 687 | if (err) { |
| 688 | brcmf_dbg(ERROR, "HT Avail request error: %d\n", err); |
| 689 | return -EBADE; |
| 690 | } |
| 691 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 692 | /* Check current status */ |
| 693 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 694 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 695 | if (err) { |
| 696 | brcmf_dbg(ERROR, "HT Avail read error: %d\n", err); |
| 697 | return -EBADE; |
| 698 | } |
| 699 | |
| 700 | /* Go to pending and await interrupt if appropriate */ |
| 701 | if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) { |
| 702 | /* Allow only clock-available interrupt */ |
| 703 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 704 | SDIO_FUNC_1, |
| 705 | SBSDIO_DEVICE_CTL, &err); |
| 706 | if (err) { |
| 707 | brcmf_dbg(ERROR, "Devctl error setting CA: %d\n", |
| 708 | err); |
| 709 | return -EBADE; |
| 710 | } |
| 711 | |
| 712 | devctl |= SBSDIO_DEVCTL_CA_INT_ONLY; |
| 713 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 714 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 715 | brcmf_dbg(INFO, "CLKCTL: set PENDING\n"); |
| 716 | bus->clkstate = CLK_PENDING; |
| 717 | |
| 718 | return 0; |
| 719 | } else if (bus->clkstate == CLK_PENDING) { |
| 720 | /* Cancel CA-only interrupt filter */ |
| 721 | devctl = |
| 722 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 723 | SBSDIO_DEVICE_CTL, &err); |
| 724 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 725 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 726 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 727 | } |
| 728 | |
| 729 | /* Otherwise, wait here (polling) for HT Avail */ |
| 730 | timeout = jiffies + |
| 731 | msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000); |
| 732 | while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { |
| 733 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 734 | SDIO_FUNC_1, |
| 735 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 736 | &err); |
| 737 | if (time_after(jiffies, timeout)) |
| 738 | break; |
| 739 | else |
| 740 | usleep_range(5000, 10000); |
| 741 | } |
| 742 | if (err) { |
| 743 | brcmf_dbg(ERROR, "HT Avail request error: %d\n", err); |
| 744 | return -EBADE; |
| 745 | } |
| 746 | if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { |
| 747 | brcmf_dbg(ERROR, "HT Avail timeout (%d): clkctl 0x%02x\n", |
| 748 | PMU_MAX_TRANSITION_DLY, clkctl); |
| 749 | return -EBADE; |
| 750 | } |
| 751 | |
| 752 | /* Mark clock available */ |
| 753 | bus->clkstate = CLK_AVAIL; |
| 754 | brcmf_dbg(INFO, "CLKCTL: turned ON\n"); |
| 755 | |
| 756 | #if defined(BCMDBG) |
| 757 | if (bus->alp_only != true) { |
| 758 | if (SBSDIO_ALPONLY(clkctl)) |
| 759 | brcmf_dbg(ERROR, "HT Clock should be on\n"); |
| 760 | } |
| 761 | #endif /* defined (BCMDBG) */ |
| 762 | |
| 763 | bus->activity = true; |
| 764 | } else { |
| 765 | clkreq = 0; |
| 766 | |
| 767 | if (bus->clkstate == CLK_PENDING) { |
| 768 | /* Cancel CA-only interrupt filter */ |
| 769 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 770 | SDIO_FUNC_1, |
| 771 | SBSDIO_DEVICE_CTL, &err); |
| 772 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 773 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 774 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 775 | } |
| 776 | |
| 777 | bus->clkstate = CLK_SDONLY; |
| 778 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 779 | SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err); |
| 780 | brcmf_dbg(INFO, "CLKCTL: turned OFF\n"); |
| 781 | if (err) { |
| 782 | brcmf_dbg(ERROR, "Failed access turning clock off: %d\n", |
| 783 | err); |
| 784 | return -EBADE; |
| 785 | } |
| 786 | } |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | /* Change idle/active SD state */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 791 | static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 792 | { |
| 793 | brcmf_dbg(TRACE, "Enter\n"); |
| 794 | |
| 795 | if (on) |
| 796 | bus->clkstate = CLK_SDONLY; |
| 797 | else |
| 798 | bus->clkstate = CLK_NONE; |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | /* Transition SD and backplane clock readiness */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 804 | static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 805 | { |
| 806 | #ifdef BCMDBG |
| 807 | uint oldstate = bus->clkstate; |
| 808 | #endif /* BCMDBG */ |
| 809 | |
| 810 | brcmf_dbg(TRACE, "Enter\n"); |
| 811 | |
| 812 | /* Early exit if we're already there */ |
| 813 | if (bus->clkstate == target) { |
| 814 | if (target == CLK_AVAIL) { |
| 815 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 816 | bus->activity = true; |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | switch (target) { |
| 822 | case CLK_AVAIL: |
| 823 | /* Make sure SD clock is available */ |
| 824 | if (bus->clkstate == CLK_NONE) |
| 825 | brcmf_sdbrcm_sdclk(bus, true); |
| 826 | /* Now request HT Avail on the backplane */ |
| 827 | brcmf_sdbrcm_htclk(bus, true, pendok); |
| 828 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 829 | bus->activity = true; |
| 830 | break; |
| 831 | |
| 832 | case CLK_SDONLY: |
| 833 | /* Remove HT request, or bring up SD clock */ |
| 834 | if (bus->clkstate == CLK_NONE) |
| 835 | brcmf_sdbrcm_sdclk(bus, true); |
| 836 | else if (bus->clkstate == CLK_AVAIL) |
| 837 | brcmf_sdbrcm_htclk(bus, false, false); |
| 838 | else |
| 839 | brcmf_dbg(ERROR, "request for %d -> %d\n", |
| 840 | bus->clkstate, target); |
| 841 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 842 | break; |
| 843 | |
| 844 | case CLK_NONE: |
| 845 | /* Make sure to remove HT request */ |
| 846 | if (bus->clkstate == CLK_AVAIL) |
| 847 | brcmf_sdbrcm_htclk(bus, false, false); |
| 848 | /* Now remove the SD clock */ |
| 849 | brcmf_sdbrcm_sdclk(bus, false); |
| 850 | brcmf_sdbrcm_wd_timer(bus, 0); |
| 851 | break; |
| 852 | } |
| 853 | #ifdef BCMDBG |
| 854 | brcmf_dbg(INFO, "%d -> %d\n", oldstate, bus->clkstate); |
| 855 | #endif /* BCMDBG */ |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 860 | static int brcmf_sdbrcm_bussleep(struct brcmf_sdio *bus, bool sleep) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 861 | { |
| 862 | uint retries = 0; |
| 863 | |
| 864 | brcmf_dbg(INFO, "request %s (currently %s)\n", |
| 865 | sleep ? "SLEEP" : "WAKE", |
| 866 | bus->sleeping ? "SLEEP" : "WAKE"); |
| 867 | |
| 868 | /* Done if we're already in the requested state */ |
| 869 | if (sleep == bus->sleeping) |
| 870 | return 0; |
| 871 | |
| 872 | /* Going to sleep: set the alarm and turn off the lights... */ |
| 873 | if (sleep) { |
| 874 | /* Don't sleep if something is pending */ |
| 875 | if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq)) |
| 876 | return -EBUSY; |
| 877 | |
| 878 | /* Make sure the controller has the bus up */ |
| 879 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 880 | |
| 881 | /* Tell device to start using OOB wakeup */ |
| 882 | w_sdreg32(bus, SMB_USE_OOB, |
| 883 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 884 | if (retries > retry_limit) |
| 885 | brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"); |
| 886 | |
| 887 | /* Turn off our contribution to the HT clock request */ |
| 888 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 889 | |
| 890 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 891 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 892 | SBSDIO_FORCE_HW_CLKREQ_OFF, NULL); |
| 893 | |
| 894 | /* Isolate the bus */ |
Franky Lin | 718897eb | 2011-11-04 22:23:27 +0100 | [diff] [blame] | 895 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 896 | SBSDIO_DEVICE_CTL, |
| 897 | SBSDIO_DEVCTL_PADS_ISO, NULL); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 898 | |
| 899 | /* Change state */ |
| 900 | bus->sleeping = true; |
| 901 | |
| 902 | } else { |
| 903 | /* Waking up: bus power up is ok, set local state */ |
| 904 | |
| 905 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 906 | SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL); |
| 907 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 908 | /* Make sure the controller has the bus up */ |
| 909 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 910 | |
| 911 | /* Send misc interrupt to indicate OOB not needed */ |
| 912 | w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, tosbmailboxdata), |
| 913 | &retries); |
| 914 | if (retries <= retry_limit) |
| 915 | w_sdreg32(bus, SMB_DEV_INT, |
| 916 | offsetof(struct sdpcmd_regs, tosbmailbox), |
| 917 | &retries); |
| 918 | |
| 919 | if (retries > retry_limit) |
| 920 | brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"); |
| 921 | |
| 922 | /* Make sure we have SD bus access */ |
| 923 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 924 | |
| 925 | /* Change state */ |
| 926 | bus->sleeping = false; |
| 927 | } |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 932 | static void bus_wake(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 933 | { |
| 934 | if (bus->sleeping) |
| 935 | brcmf_sdbrcm_bussleep(bus, false); |
| 936 | } |
| 937 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 938 | static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 939 | { |
| 940 | u32 intstatus = 0; |
| 941 | u32 hmb_data; |
| 942 | u8 fcbits; |
| 943 | uint retries = 0; |
| 944 | |
| 945 | brcmf_dbg(TRACE, "Enter\n"); |
| 946 | |
| 947 | /* Read mailbox data and ack that we did so */ |
| 948 | r_sdreg32(bus, &hmb_data, |
| 949 | offsetof(struct sdpcmd_regs, tohostmailboxdata), &retries); |
| 950 | |
| 951 | if (retries <= retry_limit) |
| 952 | w_sdreg32(bus, SMB_INT_ACK, |
| 953 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 954 | bus->f1regdata += 2; |
| 955 | |
| 956 | /* Dongle recomposed rx frames, accept them again */ |
| 957 | if (hmb_data & HMB_DATA_NAKHANDLED) { |
| 958 | brcmf_dbg(INFO, "Dongle reports NAK handled, expect rtx of %d\n", |
| 959 | bus->rx_seq); |
| 960 | if (!bus->rxskip) |
| 961 | brcmf_dbg(ERROR, "unexpected NAKHANDLED!\n"); |
| 962 | |
| 963 | bus->rxskip = false; |
| 964 | intstatus |= I_HMB_FRAME_IND; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * DEVREADY does not occur with gSPI. |
| 969 | */ |
| 970 | if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) { |
| 971 | bus->sdpcm_ver = |
| 972 | (hmb_data & HMB_DATA_VERSION_MASK) >> |
| 973 | HMB_DATA_VERSION_SHIFT; |
| 974 | if (bus->sdpcm_ver != SDPCM_PROT_VERSION) |
| 975 | brcmf_dbg(ERROR, "Version mismatch, dongle reports %d, " |
| 976 | "expecting %d\n", |
| 977 | bus->sdpcm_ver, SDPCM_PROT_VERSION); |
| 978 | else |
| 979 | brcmf_dbg(INFO, "Dongle ready, protocol version %d\n", |
| 980 | bus->sdpcm_ver); |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Flow Control has been moved into the RX headers and this out of band |
| 985 | * method isn't used any more. |
| 986 | * remaining backward compatible with older dongles. |
| 987 | */ |
| 988 | if (hmb_data & HMB_DATA_FC) { |
| 989 | fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >> |
| 990 | HMB_DATA_FCDATA_SHIFT; |
| 991 | |
| 992 | if (fcbits & ~bus->flowcontrol) |
| 993 | bus->fc_xoff++; |
| 994 | |
| 995 | if (bus->flowcontrol & ~fcbits) |
| 996 | bus->fc_xon++; |
| 997 | |
| 998 | bus->fc_rcvd++; |
| 999 | bus->flowcontrol = fcbits; |
| 1000 | } |
| 1001 | |
| 1002 | /* Shouldn't be any others */ |
| 1003 | if (hmb_data & ~(HMB_DATA_DEVREADY | |
| 1004 | HMB_DATA_NAKHANDLED | |
| 1005 | HMB_DATA_FC | |
| 1006 | HMB_DATA_FWREADY | |
| 1007 | HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) |
| 1008 | brcmf_dbg(ERROR, "Unknown mailbox data content: 0x%02x\n", |
| 1009 | hmb_data); |
| 1010 | |
| 1011 | return intstatus; |
| 1012 | } |
| 1013 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1014 | static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1015 | { |
| 1016 | uint retries = 0; |
| 1017 | u16 lastrbc; |
| 1018 | u8 hi, lo; |
| 1019 | int err; |
| 1020 | |
| 1021 | brcmf_dbg(ERROR, "%sterminate frame%s\n", |
| 1022 | abort ? "abort command, " : "", |
| 1023 | rtx ? ", send NAK" : ""); |
| 1024 | |
| 1025 | if (abort) |
| 1026 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 1027 | |
| 1028 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 1029 | SBSDIO_FUNC1_FRAMECTRL, |
| 1030 | SFC_RF_TERM, &err); |
| 1031 | bus->f1regdata++; |
| 1032 | |
| 1033 | /* Wait until the packet has been flushed (device/FIFO stable) */ |
| 1034 | for (lastrbc = retries = 0xffff; retries > 0; retries--) { |
| 1035 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 1036 | SBSDIO_FUNC1_RFRAMEBCHI, NULL); |
| 1037 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 1038 | SBSDIO_FUNC1_RFRAMEBCLO, NULL); |
| 1039 | bus->f1regdata += 2; |
| 1040 | |
| 1041 | if ((hi == 0) && (lo == 0)) |
| 1042 | break; |
| 1043 | |
| 1044 | if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) { |
| 1045 | brcmf_dbg(ERROR, "count growing: last 0x%04x now 0x%04x\n", |
| 1046 | lastrbc, (hi << 8) + lo); |
| 1047 | } |
| 1048 | lastrbc = (hi << 8) + lo; |
| 1049 | } |
| 1050 | |
| 1051 | if (!retries) |
| 1052 | brcmf_dbg(ERROR, "count never zeroed: last 0x%04x\n", lastrbc); |
| 1053 | else |
| 1054 | brcmf_dbg(INFO, "flush took %d iterations\n", 0xffff - retries); |
| 1055 | |
| 1056 | if (rtx) { |
| 1057 | bus->rxrtx++; |
| 1058 | w_sdreg32(bus, SMB_NAK, |
| 1059 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 1060 | |
| 1061 | bus->f1regdata++; |
| 1062 | if (retries <= retry_limit) |
| 1063 | bus->rxskip = true; |
| 1064 | } |
| 1065 | |
| 1066 | /* Clear partial in any case */ |
| 1067 | bus->nextlen = 0; |
| 1068 | |
| 1069 | /* If we can't reach the device, signal failure */ |
| 1070 | if (err || brcmf_sdcard_regfail(bus->sdiodev)) |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 1071 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1074 | /* copy a buffer into a pkt buffer chain */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1075 | static uint brcmf_sdbrcm_glom_from_buf(struct brcmf_sdio *bus, uint len) |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1076 | { |
| 1077 | uint n, ret = 0; |
| 1078 | struct sk_buff *p; |
| 1079 | u8 *buf; |
| 1080 | |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1081 | buf = bus->dataptr; |
| 1082 | |
| 1083 | /* copy the data */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1084 | skb_queue_walk(&bus->glom, p) { |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1085 | n = min_t(uint, p->len, len); |
| 1086 | memcpy(p->data, buf, n); |
| 1087 | buf += n; |
| 1088 | len -= n; |
| 1089 | ret += n; |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1090 | if (!len) |
| 1091 | break; |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
Arend van Spriel | 9a95e60 | 2011-11-10 20:30:29 +0100 | [diff] [blame] | 1097 | /* return total length of buffer chain */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1098 | static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus) |
Arend van Spriel | 9a95e60 | 2011-11-10 20:30:29 +0100 | [diff] [blame] | 1099 | { |
| 1100 | struct sk_buff *p; |
| 1101 | uint total; |
| 1102 | |
| 1103 | total = 0; |
| 1104 | skb_queue_walk(&bus->glom, p) |
| 1105 | total += p->len; |
| 1106 | return total; |
| 1107 | } |
| 1108 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1109 | static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus) |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame] | 1110 | { |
| 1111 | struct sk_buff *cur, *next; |
| 1112 | |
| 1113 | skb_queue_walk_safe(&bus->glom, cur, next) { |
| 1114 | skb_unlink(cur, &bus->glom); |
| 1115 | brcmu_pkt_buf_free_skb(cur); |
| 1116 | } |
| 1117 | } |
| 1118 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1119 | static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1120 | { |
| 1121 | u16 dlen, totlen; |
| 1122 | u8 *dptr, num = 0; |
| 1123 | |
| 1124 | u16 sublen, check; |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1125 | struct sk_buff *pfirst, *pnext; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1126 | |
| 1127 | int errcode; |
| 1128 | u8 chan, seq, doff, sfdoff; |
| 1129 | u8 txmax; |
| 1130 | |
| 1131 | int ifidx = 0; |
| 1132 | bool usechain = bus->use_rxchain; |
| 1133 | |
| 1134 | /* If packets, issue read(s) and send up packet chain */ |
| 1135 | /* Return sequence numbers consumed? */ |
| 1136 | |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1137 | brcmf_dbg(TRACE, "start: glomd %p glom %p\n", |
| 1138 | bus->glomd, skb_peek(&bus->glom)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1139 | |
| 1140 | /* If there's a descriptor, generate the packet chain */ |
| 1141 | if (bus->glomd) { |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1142 | pfirst = pnext = NULL; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1143 | dlen = (u16) (bus->glomd->len); |
| 1144 | dptr = bus->glomd->data; |
| 1145 | if (!dlen || (dlen & 1)) { |
| 1146 | brcmf_dbg(ERROR, "bad glomd len(%d), ignore descriptor\n", |
| 1147 | dlen); |
| 1148 | dlen = 0; |
| 1149 | } |
| 1150 | |
| 1151 | for (totlen = num = 0; dlen; num++) { |
| 1152 | /* Get (and move past) next length */ |
| 1153 | sublen = get_unaligned_le16(dptr); |
| 1154 | dlen -= sizeof(u16); |
| 1155 | dptr += sizeof(u16); |
| 1156 | if ((sublen < SDPCM_HDRLEN) || |
| 1157 | ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) { |
| 1158 | brcmf_dbg(ERROR, "descriptor len %d bad: %d\n", |
| 1159 | num, sublen); |
| 1160 | pnext = NULL; |
| 1161 | break; |
| 1162 | } |
| 1163 | if (sublen % BRCMF_SDALIGN) { |
| 1164 | brcmf_dbg(ERROR, "sublen %d not multiple of %d\n", |
| 1165 | sublen, BRCMF_SDALIGN); |
| 1166 | usechain = false; |
| 1167 | } |
| 1168 | totlen += sublen; |
| 1169 | |
| 1170 | /* For last frame, adjust read len so total |
| 1171 | is a block multiple */ |
| 1172 | if (!dlen) { |
| 1173 | sublen += |
| 1174 | (roundup(totlen, bus->blocksize) - totlen); |
| 1175 | totlen = roundup(totlen, bus->blocksize); |
| 1176 | } |
| 1177 | |
| 1178 | /* Allocate/chain packet for next subframe */ |
| 1179 | pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN); |
| 1180 | if (pnext == NULL) { |
| 1181 | brcmf_dbg(ERROR, "bcm_pkt_buf_get_skb failed, num %d len %d\n", |
| 1182 | num, sublen); |
| 1183 | break; |
| 1184 | } |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1185 | skb_queue_tail(&bus->glom, pnext); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1186 | |
| 1187 | /* Adhere to start alignment requirements */ |
| 1188 | pkt_align(pnext, sublen, BRCMF_SDALIGN); |
| 1189 | } |
| 1190 | |
| 1191 | /* If all allocations succeeded, save packet chain |
| 1192 | in bus structure */ |
| 1193 | if (pnext) { |
| 1194 | brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n", |
| 1195 | totlen, num); |
| 1196 | if (BRCMF_GLOM_ON() && bus->nextlen && |
| 1197 | totlen != bus->nextlen) { |
| 1198 | brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n", |
| 1199 | bus->nextlen, totlen, rxseq); |
| 1200 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1201 | pfirst = pnext = NULL; |
| 1202 | } else { |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame] | 1203 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1204 | num = 0; |
| 1205 | } |
| 1206 | |
| 1207 | /* Done with descriptor packet */ |
| 1208 | brcmu_pkt_buf_free_skb(bus->glomd); |
| 1209 | bus->glomd = NULL; |
| 1210 | bus->nextlen = 0; |
| 1211 | } |
| 1212 | |
| 1213 | /* Ok -- either we just generated a packet chain, |
| 1214 | or had one from before */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1215 | if (!skb_queue_empty(&bus->glom)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1216 | if (BRCMF_GLOM_ON()) { |
| 1217 | brcmf_dbg(GLOM, "try superframe read, packet chain:\n"); |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1218 | skb_queue_walk(&bus->glom, pnext) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1219 | brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n", |
| 1220 | pnext, (u8 *) (pnext->data), |
| 1221 | pnext->len, pnext->len); |
| 1222 | } |
| 1223 | } |
| 1224 | |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1225 | pfirst = skb_peek(&bus->glom); |
Arend van Spriel | 9a95e60 | 2011-11-10 20:30:29 +0100 | [diff] [blame] | 1226 | dlen = (u16) brcmf_sdbrcm_glom_len(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1227 | |
| 1228 | /* Do an SDIO read for the superframe. Configurable iovar to |
| 1229 | * read directly into the chained packet, or allocate a large |
| 1230 | * packet and and copy into the chain. |
| 1231 | */ |
| 1232 | if (usechain) { |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1233 | errcode = brcmf_sdcard_recv_chain(bus->sdiodev, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1234 | bus->sdiodev->sbwad, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1235 | SDIO_FUNC_2, F2SYNC, &bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1236 | } else if (bus->dataptr) { |
| 1237 | errcode = brcmf_sdcard_recv_buf(bus->sdiodev, |
| 1238 | bus->sdiodev->sbwad, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1239 | SDIO_FUNC_2, F2SYNC, |
| 1240 | bus->dataptr, dlen); |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1241 | sublen = (u16) brcmf_sdbrcm_glom_from_buf(bus, dlen); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1242 | if (sublen != dlen) { |
| 1243 | brcmf_dbg(ERROR, "FAILED TO COPY, dlen %d sublen %d\n", |
| 1244 | dlen, sublen); |
| 1245 | errcode = -1; |
| 1246 | } |
| 1247 | pnext = NULL; |
| 1248 | } else { |
| 1249 | brcmf_dbg(ERROR, "COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n", |
| 1250 | dlen); |
| 1251 | errcode = -1; |
| 1252 | } |
| 1253 | bus->f2rxdata++; |
| 1254 | |
| 1255 | /* On failure, kill the superframe, allow a couple retries */ |
| 1256 | if (errcode < 0) { |
| 1257 | brcmf_dbg(ERROR, "glom read of %d bytes failed: %d\n", |
| 1258 | dlen, errcode); |
| 1259 | bus->drvr->rx_errors++; |
| 1260 | |
| 1261 | if (bus->glomerr++ < 3) { |
| 1262 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1263 | } else { |
| 1264 | bus->glomerr = 0; |
| 1265 | brcmf_sdbrcm_rxfail(bus, true, false); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1266 | bus->rxglomfail++; |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame] | 1267 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1268 | } |
| 1269 | return 0; |
| 1270 | } |
| 1271 | #ifdef BCMDBG |
| 1272 | if (BRCMF_GLOM_ON()) { |
| 1273 | printk(KERN_DEBUG "SUPERFRAME:\n"); |
| 1274 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1275 | pfirst->data, min_t(int, pfirst->len, 48)); |
| 1276 | } |
| 1277 | #endif |
| 1278 | |
| 1279 | /* Validate the superframe header */ |
| 1280 | dptr = (u8 *) (pfirst->data); |
| 1281 | sublen = get_unaligned_le16(dptr); |
| 1282 | check = get_unaligned_le16(dptr + sizeof(u16)); |
| 1283 | |
| 1284 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1285 | seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1286 | bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET]; |
| 1287 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1288 | brcmf_dbg(INFO, "nextlen too large (%d) seq %d\n", |
| 1289 | bus->nextlen, seq); |
| 1290 | bus->nextlen = 0; |
| 1291 | } |
| 1292 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1293 | txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1294 | |
| 1295 | errcode = 0; |
| 1296 | if ((u16)~(sublen ^ check)) { |
| 1297 | brcmf_dbg(ERROR, "(superframe): HW hdr error: len/check 0x%04x/0x%04x\n", |
| 1298 | sublen, check); |
| 1299 | errcode = -1; |
| 1300 | } else if (roundup(sublen, bus->blocksize) != dlen) { |
| 1301 | brcmf_dbg(ERROR, "(superframe): len 0x%04x, rounded 0x%04x, expect 0x%04x\n", |
| 1302 | sublen, roundup(sublen, bus->blocksize), |
| 1303 | dlen); |
| 1304 | errcode = -1; |
| 1305 | } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) != |
| 1306 | SDPCM_GLOM_CHANNEL) { |
| 1307 | brcmf_dbg(ERROR, "(superframe): bad channel %d\n", |
| 1308 | SDPCM_PACKET_CHANNEL( |
| 1309 | &dptr[SDPCM_FRAMETAG_LEN])); |
| 1310 | errcode = -1; |
| 1311 | } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) { |
| 1312 | brcmf_dbg(ERROR, "(superframe): got 2nd descriptor?\n"); |
| 1313 | errcode = -1; |
| 1314 | } else if ((doff < SDPCM_HDRLEN) || |
| 1315 | (doff > (pfirst->len - SDPCM_HDRLEN))) { |
| 1316 | brcmf_dbg(ERROR, "(superframe): Bad data offset %d: HW %d pkt %d min %d\n", |
| 1317 | doff, sublen, pfirst->len, SDPCM_HDRLEN); |
| 1318 | errcode = -1; |
| 1319 | } |
| 1320 | |
| 1321 | /* Check sequence number of superframe SW header */ |
| 1322 | if (rxseq != seq) { |
| 1323 | brcmf_dbg(INFO, "(superframe) rx_seq %d, expected %d\n", |
| 1324 | seq, rxseq); |
| 1325 | bus->rx_badseq++; |
| 1326 | rxseq = seq; |
| 1327 | } |
| 1328 | |
| 1329 | /* Check window for sanity */ |
| 1330 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1331 | brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n", |
| 1332 | txmax, bus->tx_seq); |
| 1333 | txmax = bus->tx_seq + 2; |
| 1334 | } |
| 1335 | bus->tx_max = txmax; |
| 1336 | |
| 1337 | /* Remove superframe header, remember offset */ |
| 1338 | skb_pull(pfirst, doff); |
| 1339 | sfdoff = doff; |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1340 | num = 0; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1341 | |
| 1342 | /* Validate all the subframe headers */ |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1343 | skb_queue_walk(&bus->glom, pnext) { |
| 1344 | /* leave when invalid subframe is found */ |
| 1345 | if (errcode) |
| 1346 | break; |
| 1347 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1348 | dptr = (u8 *) (pnext->data); |
| 1349 | dlen = (u16) (pnext->len); |
| 1350 | sublen = get_unaligned_le16(dptr); |
| 1351 | check = get_unaligned_le16(dptr + sizeof(u16)); |
| 1352 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1353 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1354 | #ifdef BCMDBG |
| 1355 | if (BRCMF_GLOM_ON()) { |
| 1356 | printk(KERN_DEBUG "subframe:\n"); |
| 1357 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1358 | dptr, 32); |
| 1359 | } |
| 1360 | #endif |
| 1361 | |
| 1362 | if ((u16)~(sublen ^ check)) { |
| 1363 | brcmf_dbg(ERROR, "(subframe %d): HW hdr error: len/check 0x%04x/0x%04x\n", |
| 1364 | num, sublen, check); |
| 1365 | errcode = -1; |
| 1366 | } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) { |
| 1367 | brcmf_dbg(ERROR, "(subframe %d): length mismatch: len 0x%04x, expect 0x%04x\n", |
| 1368 | num, sublen, dlen); |
| 1369 | errcode = -1; |
| 1370 | } else if ((chan != SDPCM_DATA_CHANNEL) && |
| 1371 | (chan != SDPCM_EVENT_CHANNEL)) { |
| 1372 | brcmf_dbg(ERROR, "(subframe %d): bad channel %d\n", |
| 1373 | num, chan); |
| 1374 | errcode = -1; |
| 1375 | } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) { |
| 1376 | brcmf_dbg(ERROR, "(subframe %d): Bad data offset %d: HW %d min %d\n", |
| 1377 | num, doff, sublen, SDPCM_HDRLEN); |
| 1378 | errcode = -1; |
| 1379 | } |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1380 | /* increase the subframe count */ |
| 1381 | num++; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | if (errcode) { |
| 1385 | /* Terminate frame on error, request |
| 1386 | a couple retries */ |
| 1387 | if (bus->glomerr++ < 3) { |
| 1388 | /* Restore superframe header space */ |
| 1389 | skb_push(pfirst, sfdoff); |
| 1390 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1391 | } else { |
| 1392 | bus->glomerr = 0; |
| 1393 | brcmf_sdbrcm_rxfail(bus, true, false); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1394 | bus->rxglomfail++; |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame] | 1395 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1396 | } |
| 1397 | bus->nextlen = 0; |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | /* Basic SD framing looks ok - process each packet (header) */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1402 | |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1403 | skb_queue_walk_safe(&bus->glom, pfirst, pnext) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1404 | dptr = (u8 *) (pfirst->data); |
| 1405 | sublen = get_unaligned_le16(dptr); |
| 1406 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1407 | seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1408 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1409 | |
| 1410 | brcmf_dbg(GLOM, "Get subframe %d, %p(%p/%d), sublen %d chan %d seq %d\n", |
| 1411 | num, pfirst, pfirst->data, |
| 1412 | pfirst->len, sublen, chan, seq); |
| 1413 | |
| 1414 | /* precondition: chan == SDPCM_DATA_CHANNEL || |
| 1415 | chan == SDPCM_EVENT_CHANNEL */ |
| 1416 | |
| 1417 | if (rxseq != seq) { |
| 1418 | brcmf_dbg(GLOM, "rx_seq %d, expected %d\n", |
| 1419 | seq, rxseq); |
| 1420 | bus->rx_badseq++; |
| 1421 | rxseq = seq; |
| 1422 | } |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1423 | rxseq++; |
| 1424 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1425 | #ifdef BCMDBG |
| 1426 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 1427 | printk(KERN_DEBUG "Rx Subframe Data:\n"); |
| 1428 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1429 | dptr, dlen); |
| 1430 | } |
| 1431 | #endif |
| 1432 | |
| 1433 | __skb_trim(pfirst, sublen); |
| 1434 | skb_pull(pfirst, doff); |
| 1435 | |
| 1436 | if (pfirst->len == 0) { |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1437 | skb_unlink(pfirst, &bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1438 | brcmu_pkt_buf_free_skb(pfirst); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1439 | continue; |
| 1440 | } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, |
| 1441 | pfirst) != 0) { |
| 1442 | brcmf_dbg(ERROR, "rx protocol error\n"); |
| 1443 | bus->drvr->rx_errors++; |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1444 | skb_unlink(pfirst, &bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1445 | brcmu_pkt_buf_free_skb(pfirst); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1446 | continue; |
| 1447 | } |
| 1448 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1449 | #ifdef BCMDBG |
| 1450 | if (BRCMF_GLOM_ON()) { |
| 1451 | brcmf_dbg(GLOM, "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n", |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1452 | bus->glom.qlen, pfirst, pfirst->data, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1453 | pfirst->len, pfirst->next, |
| 1454 | pfirst->prev); |
| 1455 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1456 | pfirst->data, |
| 1457 | min_t(int, pfirst->len, 32)); |
| 1458 | } |
| 1459 | #endif /* BCMDBG */ |
| 1460 | } |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1461 | /* sent any remaining packets up */ |
| 1462 | if (bus->glom.qlen) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1463 | up(&bus->sdsem); |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1464 | brcmf_rx_frame(bus->drvr, ifidx, &bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1465 | down(&bus->sdsem); |
| 1466 | } |
| 1467 | |
| 1468 | bus->rxglomframes++; |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 1469 | bus->rxglompkts += bus->glom.qlen; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1470 | } |
| 1471 | return num; |
| 1472 | } |
| 1473 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1474 | static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1475 | bool *pending) |
| 1476 | { |
| 1477 | DECLARE_WAITQUEUE(wait, current); |
| 1478 | int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT); |
| 1479 | |
| 1480 | /* Wait until control frame is available */ |
| 1481 | add_wait_queue(&bus->dcmd_resp_wait, &wait); |
| 1482 | set_current_state(TASK_INTERRUPTIBLE); |
| 1483 | |
| 1484 | while (!(*condition) && (!signal_pending(current) && timeout)) |
| 1485 | timeout = schedule_timeout(timeout); |
| 1486 | |
| 1487 | if (signal_pending(current)) |
| 1488 | *pending = true; |
| 1489 | |
| 1490 | set_current_state(TASK_RUNNING); |
| 1491 | remove_wait_queue(&bus->dcmd_resp_wait, &wait); |
| 1492 | |
| 1493 | return timeout; |
| 1494 | } |
| 1495 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1496 | static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1497 | { |
| 1498 | if (waitqueue_active(&bus->dcmd_resp_wait)) |
| 1499 | wake_up_interruptible(&bus->dcmd_resp_wait); |
| 1500 | |
| 1501 | return 0; |
| 1502 | } |
| 1503 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1504 | brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1505 | { |
| 1506 | uint rdlen, pad; |
| 1507 | |
| 1508 | int sdret; |
| 1509 | |
| 1510 | brcmf_dbg(TRACE, "Enter\n"); |
| 1511 | |
| 1512 | /* Set rxctl for frame (w/optional alignment) */ |
| 1513 | bus->rxctl = bus->rxbuf; |
| 1514 | bus->rxctl += BRCMF_FIRSTREAD; |
| 1515 | pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN); |
| 1516 | if (pad) |
| 1517 | bus->rxctl += (BRCMF_SDALIGN - pad); |
| 1518 | bus->rxctl -= BRCMF_FIRSTREAD; |
| 1519 | |
| 1520 | /* Copy the already-read portion over */ |
| 1521 | memcpy(bus->rxctl, hdr, BRCMF_FIRSTREAD); |
| 1522 | if (len <= BRCMF_FIRSTREAD) |
| 1523 | goto gotpkt; |
| 1524 | |
| 1525 | /* Raise rdlen to next SDIO block to avoid tail command */ |
| 1526 | rdlen = len - BRCMF_FIRSTREAD; |
| 1527 | if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) { |
| 1528 | pad = bus->blocksize - (rdlen % bus->blocksize); |
| 1529 | if ((pad <= bus->roundup) && (pad < bus->blocksize) && |
| 1530 | ((len + pad) < bus->drvr->maxctl)) |
| 1531 | rdlen += pad; |
| 1532 | } else if (rdlen % BRCMF_SDALIGN) { |
| 1533 | rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN); |
| 1534 | } |
| 1535 | |
| 1536 | /* Satisfy length-alignment requirements */ |
| 1537 | if (rdlen & (ALIGNMENT - 1)) |
| 1538 | rdlen = roundup(rdlen, ALIGNMENT); |
| 1539 | |
| 1540 | /* Drop if the read is too big or it exceeds our maximum */ |
| 1541 | if ((rdlen + BRCMF_FIRSTREAD) > bus->drvr->maxctl) { |
| 1542 | brcmf_dbg(ERROR, "%d-byte control read exceeds %d-byte buffer\n", |
| 1543 | rdlen, bus->drvr->maxctl); |
| 1544 | bus->drvr->rx_errors++; |
| 1545 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1546 | goto done; |
| 1547 | } |
| 1548 | |
| 1549 | if ((len - doff) > bus->drvr->maxctl) { |
| 1550 | brcmf_dbg(ERROR, "%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n", |
| 1551 | len, len - doff, bus->drvr->maxctl); |
| 1552 | bus->drvr->rx_errors++; |
| 1553 | bus->rx_toolong++; |
| 1554 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1555 | goto done; |
| 1556 | } |
| 1557 | |
| 1558 | /* Read remainder of frame body into the rxctl buffer */ |
| 1559 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, |
| 1560 | bus->sdiodev->sbwad, |
| 1561 | SDIO_FUNC_2, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1562 | F2SYNC, (bus->rxctl + BRCMF_FIRSTREAD), rdlen); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1563 | bus->f2rxdata++; |
| 1564 | |
| 1565 | /* Control frame failures need retransmission */ |
| 1566 | if (sdret < 0) { |
| 1567 | brcmf_dbg(ERROR, "read %d control bytes failed: %d\n", |
| 1568 | rdlen, sdret); |
| 1569 | bus->rxc_errors++; |
| 1570 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1571 | goto done; |
| 1572 | } |
| 1573 | |
| 1574 | gotpkt: |
| 1575 | |
| 1576 | #ifdef BCMDBG |
| 1577 | if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) { |
| 1578 | printk(KERN_DEBUG "RxCtrl:\n"); |
| 1579 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len); |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
| 1583 | /* Point to valid data and indicate its length */ |
| 1584 | bus->rxctl += doff; |
| 1585 | bus->rxlen = len - doff; |
| 1586 | |
| 1587 | done: |
| 1588 | /* Awake any waiters */ |
| 1589 | brcmf_sdbrcm_dcmd_resp_wake(bus); |
| 1590 | } |
| 1591 | |
| 1592 | /* Pad read to blocksize for efficiency */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1593 | static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1594 | { |
| 1595 | if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) { |
| 1596 | *pad = bus->blocksize - (*rdlen % bus->blocksize); |
| 1597 | if (*pad <= bus->roundup && *pad < bus->blocksize && |
| 1598 | *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ) |
| 1599 | *rdlen += *pad; |
| 1600 | } else if (*rdlen % BRCMF_SDALIGN) { |
| 1601 | *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1606 | brcmf_alloc_pkt_and_read(struct brcmf_sdio *bus, u16 rdlen, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1607 | struct sk_buff **pkt, u8 **rxbuf) |
| 1608 | { |
| 1609 | int sdret; /* Return code from calls */ |
| 1610 | |
| 1611 | *pkt = brcmu_pkt_buf_get_skb(rdlen + BRCMF_SDALIGN); |
| 1612 | if (*pkt == NULL) |
| 1613 | return; |
| 1614 | |
| 1615 | pkt_align(*pkt, rdlen, BRCMF_SDALIGN); |
| 1616 | *rxbuf = (u8 *) ((*pkt)->data); |
| 1617 | /* Read the entire frame */ |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1618 | sdret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad, |
| 1619 | SDIO_FUNC_2, F2SYNC, *pkt); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1620 | bus->f2rxdata++; |
| 1621 | |
| 1622 | if (sdret < 0) { |
| 1623 | brcmf_dbg(ERROR, "(nextlen): read %d bytes failed: %d\n", |
| 1624 | rdlen, sdret); |
| 1625 | brcmu_pkt_buf_free_skb(*pkt); |
| 1626 | bus->drvr->rx_errors++; |
| 1627 | /* Force retry w/normal header read. |
| 1628 | * Don't attempt NAK for |
| 1629 | * gSPI |
| 1630 | */ |
| 1631 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1632 | *pkt = NULL; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | /* Checks the header */ |
| 1637 | static int |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1638 | brcmf_check_rxbuf(struct brcmf_sdio *bus, struct sk_buff *pkt, u8 *rxbuf, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1639 | u8 rxseq, u16 nextlen, u16 *len) |
| 1640 | { |
| 1641 | u16 check; |
| 1642 | bool len_consistent; /* Result of comparing readahead len and |
| 1643 | len from hw-hdr */ |
| 1644 | |
| 1645 | memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN); |
| 1646 | |
| 1647 | /* Extract hardware header fields */ |
| 1648 | *len = get_unaligned_le16(bus->rxhdr); |
| 1649 | check = get_unaligned_le16(bus->rxhdr + sizeof(u16)); |
| 1650 | |
| 1651 | /* All zeros means readahead info was bad */ |
| 1652 | if (!(*len | check)) { |
| 1653 | brcmf_dbg(INFO, "(nextlen): read zeros in HW header???\n"); |
| 1654 | goto fail; |
| 1655 | } |
| 1656 | |
| 1657 | /* Validate check bytes */ |
| 1658 | if ((u16)~(*len ^ check)) { |
| 1659 | brcmf_dbg(ERROR, "(nextlen): HW hdr error: nextlen/len/check 0x%04x/0x%04x/0x%04x\n", |
| 1660 | nextlen, *len, check); |
| 1661 | bus->rx_badhdr++; |
| 1662 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1663 | goto fail; |
| 1664 | } |
| 1665 | |
| 1666 | /* Validate frame length */ |
| 1667 | if (*len < SDPCM_HDRLEN) { |
| 1668 | brcmf_dbg(ERROR, "(nextlen): HW hdr length invalid: %d\n", |
| 1669 | *len); |
| 1670 | goto fail; |
| 1671 | } |
| 1672 | |
| 1673 | /* Check for consistency with readahead info */ |
| 1674 | len_consistent = (nextlen != (roundup(*len, 16) >> 4)); |
| 1675 | if (len_consistent) { |
| 1676 | /* Mismatch, force retry w/normal |
| 1677 | header (may be >4K) */ |
| 1678 | brcmf_dbg(ERROR, "(nextlen): mismatch, nextlen %d len %d rnd %d; expected rxseq %d\n", |
| 1679 | nextlen, *len, roundup(*len, 16), |
| 1680 | rxseq); |
| 1681 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1682 | goto fail; |
| 1683 | } |
| 1684 | |
| 1685 | return 0; |
| 1686 | |
| 1687 | fail: |
| 1688 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1689 | return -EINVAL; |
| 1690 | } |
| 1691 | |
| 1692 | /* Return true if there may be more frames to read */ |
| 1693 | static uint |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 1694 | brcmf_sdbrcm_readframes(struct brcmf_sdio *bus, uint maxframes, bool *finished) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1695 | { |
| 1696 | u16 len, check; /* Extracted hardware header fields */ |
| 1697 | u8 chan, seq, doff; /* Extracted software header fields */ |
| 1698 | u8 fcbits; /* Extracted fcbits from software header */ |
| 1699 | |
| 1700 | struct sk_buff *pkt; /* Packet for event or data frames */ |
| 1701 | u16 pad; /* Number of pad bytes to read */ |
| 1702 | u16 rdlen; /* Total number of bytes to read */ |
| 1703 | u8 rxseq; /* Next sequence number to expect */ |
| 1704 | uint rxleft = 0; /* Remaining number of frames allowed */ |
| 1705 | int sdret; /* Return code from calls */ |
| 1706 | u8 txmax; /* Maximum tx sequence offered */ |
| 1707 | u8 *rxbuf; |
| 1708 | int ifidx = 0; |
| 1709 | uint rxcount = 0; /* Total frames read */ |
| 1710 | |
| 1711 | brcmf_dbg(TRACE, "Enter\n"); |
| 1712 | |
| 1713 | /* Not finished unless we encounter no more frames indication */ |
| 1714 | *finished = false; |
| 1715 | |
| 1716 | for (rxseq = bus->rx_seq, rxleft = maxframes; |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 1717 | !bus->rxskip && rxleft && |
| 1718 | bus->drvr->bus_if->state != BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1719 | rxseq++, rxleft--) { |
| 1720 | |
| 1721 | /* Handle glomming separately */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1722 | if (bus->glomd || !skb_queue_empty(&bus->glom)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1723 | u8 cnt; |
| 1724 | brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n", |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1725 | bus->glomd, skb_peek(&bus->glom)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1726 | cnt = brcmf_sdbrcm_rxglom(bus, rxseq); |
| 1727 | brcmf_dbg(GLOM, "rxglom returned %d\n", cnt); |
| 1728 | rxseq += cnt - 1; |
| 1729 | rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1; |
| 1730 | continue; |
| 1731 | } |
| 1732 | |
| 1733 | /* Try doing single read if we can */ |
| 1734 | if (bus->nextlen) { |
| 1735 | u16 nextlen = bus->nextlen; |
| 1736 | bus->nextlen = 0; |
| 1737 | |
| 1738 | rdlen = len = nextlen << 4; |
| 1739 | brcmf_pad(bus, &pad, &rdlen); |
| 1740 | |
| 1741 | /* |
| 1742 | * After the frame is received we have to |
| 1743 | * distinguish whether it is data |
| 1744 | * or non-data frame. |
| 1745 | */ |
| 1746 | brcmf_alloc_pkt_and_read(bus, rdlen, &pkt, &rxbuf); |
| 1747 | if (pkt == NULL) { |
| 1748 | /* Give up on data, request rtx of events */ |
| 1749 | brcmf_dbg(ERROR, "(nextlen): brcmf_alloc_pkt_and_read failed: len %d rdlen %d expected rxseq %d\n", |
| 1750 | len, rdlen, rxseq); |
| 1751 | continue; |
| 1752 | } |
| 1753 | |
| 1754 | if (brcmf_check_rxbuf(bus, pkt, rxbuf, rxseq, nextlen, |
| 1755 | &len) < 0) |
| 1756 | continue; |
| 1757 | |
| 1758 | /* Extract software header fields */ |
| 1759 | chan = SDPCM_PACKET_CHANNEL( |
| 1760 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1761 | seq = SDPCM_PACKET_SEQUENCE( |
| 1762 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1763 | doff = SDPCM_DOFFSET_VALUE( |
| 1764 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1765 | txmax = SDPCM_WINDOW_VALUE( |
| 1766 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1767 | |
| 1768 | bus->nextlen = |
| 1769 | bus->rxhdr[SDPCM_FRAMETAG_LEN + |
| 1770 | SDPCM_NEXTLEN_OFFSET]; |
| 1771 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1772 | brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n", |
| 1773 | bus->nextlen, seq); |
| 1774 | bus->nextlen = 0; |
| 1775 | } |
| 1776 | |
| 1777 | bus->drvr->rx_readahead_cnt++; |
| 1778 | |
| 1779 | /* Handle Flow Control */ |
| 1780 | fcbits = SDPCM_FCMASK_VALUE( |
| 1781 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1782 | |
| 1783 | if (bus->flowcontrol != fcbits) { |
| 1784 | if (~bus->flowcontrol & fcbits) |
| 1785 | bus->fc_xoff++; |
| 1786 | |
| 1787 | if (bus->flowcontrol & ~fcbits) |
| 1788 | bus->fc_xon++; |
| 1789 | |
| 1790 | bus->fc_rcvd++; |
| 1791 | bus->flowcontrol = fcbits; |
| 1792 | } |
| 1793 | |
| 1794 | /* Check and update sequence number */ |
| 1795 | if (rxseq != seq) { |
| 1796 | brcmf_dbg(INFO, "(nextlen): rx_seq %d, expected %d\n", |
| 1797 | seq, rxseq); |
| 1798 | bus->rx_badseq++; |
| 1799 | rxseq = seq; |
| 1800 | } |
| 1801 | |
| 1802 | /* Check window for sanity */ |
| 1803 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1804 | brcmf_dbg(ERROR, "got unlikely tx max %d with tx_seq %d\n", |
| 1805 | txmax, bus->tx_seq); |
| 1806 | txmax = bus->tx_seq + 2; |
| 1807 | } |
| 1808 | bus->tx_max = txmax; |
| 1809 | |
| 1810 | #ifdef BCMDBG |
| 1811 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 1812 | printk(KERN_DEBUG "Rx Data:\n"); |
| 1813 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1814 | rxbuf, len); |
| 1815 | } else if (BRCMF_HDRS_ON()) { |
| 1816 | printk(KERN_DEBUG "RxHdr:\n"); |
| 1817 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1818 | bus->rxhdr, SDPCM_HDRLEN); |
| 1819 | } |
| 1820 | #endif |
| 1821 | |
| 1822 | if (chan == SDPCM_CONTROL_CHANNEL) { |
| 1823 | brcmf_dbg(ERROR, "(nextlen): readahead on control packet %d?\n", |
| 1824 | seq); |
| 1825 | /* Force retry w/normal header read */ |
| 1826 | bus->nextlen = 0; |
| 1827 | brcmf_sdbrcm_rxfail(bus, false, true); |
| 1828 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1829 | continue; |
| 1830 | } |
| 1831 | |
| 1832 | /* Validate data offset */ |
| 1833 | if ((doff < SDPCM_HDRLEN) || (doff > len)) { |
| 1834 | brcmf_dbg(ERROR, "(nextlen): bad data offset %d: HW len %d min %d\n", |
| 1835 | doff, len, SDPCM_HDRLEN); |
| 1836 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1837 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1838 | continue; |
| 1839 | } |
| 1840 | |
| 1841 | /* All done with this one -- now deliver the packet */ |
| 1842 | goto deliver; |
| 1843 | } |
| 1844 | |
| 1845 | /* Read frame header (hardware and software) */ |
| 1846 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, bus->sdiodev->sbwad, |
| 1847 | SDIO_FUNC_2, F2SYNC, bus->rxhdr, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1848 | BRCMF_FIRSTREAD); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1849 | bus->f2rxhdrs++; |
| 1850 | |
| 1851 | if (sdret < 0) { |
| 1852 | brcmf_dbg(ERROR, "RXHEADER FAILED: %d\n", sdret); |
| 1853 | bus->rx_hdrfail++; |
| 1854 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1855 | continue; |
| 1856 | } |
| 1857 | #ifdef BCMDBG |
| 1858 | if (BRCMF_BYTES_ON() || BRCMF_HDRS_ON()) { |
| 1859 | printk(KERN_DEBUG "RxHdr:\n"); |
| 1860 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1861 | bus->rxhdr, SDPCM_HDRLEN); |
| 1862 | } |
| 1863 | #endif |
| 1864 | |
| 1865 | /* Extract hardware header fields */ |
| 1866 | len = get_unaligned_le16(bus->rxhdr); |
| 1867 | check = get_unaligned_le16(bus->rxhdr + sizeof(u16)); |
| 1868 | |
| 1869 | /* All zeros means no more frames */ |
| 1870 | if (!(len | check)) { |
| 1871 | *finished = true; |
| 1872 | break; |
| 1873 | } |
| 1874 | |
| 1875 | /* Validate check bytes */ |
| 1876 | if ((u16) ~(len ^ check)) { |
| 1877 | brcmf_dbg(ERROR, "HW hdr err: len/check 0x%04x/0x%04x\n", |
| 1878 | len, check); |
| 1879 | bus->rx_badhdr++; |
| 1880 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1881 | continue; |
| 1882 | } |
| 1883 | |
| 1884 | /* Validate frame length */ |
| 1885 | if (len < SDPCM_HDRLEN) { |
| 1886 | brcmf_dbg(ERROR, "HW hdr length invalid: %d\n", len); |
| 1887 | continue; |
| 1888 | } |
| 1889 | |
| 1890 | /* Extract software header fields */ |
| 1891 | chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1892 | seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1893 | doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1894 | txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1895 | |
| 1896 | /* Validate data offset */ |
| 1897 | if ((doff < SDPCM_HDRLEN) || (doff > len)) { |
| 1898 | brcmf_dbg(ERROR, "Bad data offset %d: HW len %d, min %d seq %d\n", |
| 1899 | doff, len, SDPCM_HDRLEN, seq); |
| 1900 | bus->rx_badhdr++; |
| 1901 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1902 | continue; |
| 1903 | } |
| 1904 | |
| 1905 | /* Save the readahead length if there is one */ |
| 1906 | bus->nextlen = |
| 1907 | bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET]; |
| 1908 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1909 | brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n", |
| 1910 | bus->nextlen, seq); |
| 1911 | bus->nextlen = 0; |
| 1912 | } |
| 1913 | |
| 1914 | /* Handle Flow Control */ |
| 1915 | fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1916 | |
| 1917 | if (bus->flowcontrol != fcbits) { |
| 1918 | if (~bus->flowcontrol & fcbits) |
| 1919 | bus->fc_xoff++; |
| 1920 | |
| 1921 | if (bus->flowcontrol & ~fcbits) |
| 1922 | bus->fc_xon++; |
| 1923 | |
| 1924 | bus->fc_rcvd++; |
| 1925 | bus->flowcontrol = fcbits; |
| 1926 | } |
| 1927 | |
| 1928 | /* Check and update sequence number */ |
| 1929 | if (rxseq != seq) { |
| 1930 | brcmf_dbg(INFO, "rx_seq %d, expected %d\n", seq, rxseq); |
| 1931 | bus->rx_badseq++; |
| 1932 | rxseq = seq; |
| 1933 | } |
| 1934 | |
| 1935 | /* Check window for sanity */ |
| 1936 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1937 | brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n", |
| 1938 | txmax, bus->tx_seq); |
| 1939 | txmax = bus->tx_seq + 2; |
| 1940 | } |
| 1941 | bus->tx_max = txmax; |
| 1942 | |
| 1943 | /* Call a separate function for control frames */ |
| 1944 | if (chan == SDPCM_CONTROL_CHANNEL) { |
| 1945 | brcmf_sdbrcm_read_control(bus, bus->rxhdr, len, doff); |
| 1946 | continue; |
| 1947 | } |
| 1948 | |
| 1949 | /* precondition: chan is either SDPCM_DATA_CHANNEL, |
| 1950 | SDPCM_EVENT_CHANNEL, SDPCM_TEST_CHANNEL or |
| 1951 | SDPCM_GLOM_CHANNEL */ |
| 1952 | |
| 1953 | /* Length to read */ |
| 1954 | rdlen = (len > BRCMF_FIRSTREAD) ? (len - BRCMF_FIRSTREAD) : 0; |
| 1955 | |
| 1956 | /* May pad read to blocksize for efficiency */ |
| 1957 | if (bus->roundup && bus->blocksize && |
| 1958 | (rdlen > bus->blocksize)) { |
| 1959 | pad = bus->blocksize - (rdlen % bus->blocksize); |
| 1960 | if ((pad <= bus->roundup) && (pad < bus->blocksize) && |
| 1961 | ((rdlen + pad + BRCMF_FIRSTREAD) < MAX_RX_DATASZ)) |
| 1962 | rdlen += pad; |
| 1963 | } else if (rdlen % BRCMF_SDALIGN) { |
| 1964 | rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN); |
| 1965 | } |
| 1966 | |
| 1967 | /* Satisfy length-alignment requirements */ |
| 1968 | if (rdlen & (ALIGNMENT - 1)) |
| 1969 | rdlen = roundup(rdlen, ALIGNMENT); |
| 1970 | |
| 1971 | if ((rdlen + BRCMF_FIRSTREAD) > MAX_RX_DATASZ) { |
| 1972 | /* Too long -- skip this frame */ |
| 1973 | brcmf_dbg(ERROR, "too long: len %d rdlen %d\n", |
| 1974 | len, rdlen); |
| 1975 | bus->drvr->rx_errors++; |
| 1976 | bus->rx_toolong++; |
| 1977 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1978 | continue; |
| 1979 | } |
| 1980 | |
| 1981 | pkt = brcmu_pkt_buf_get_skb(rdlen + |
| 1982 | BRCMF_FIRSTREAD + BRCMF_SDALIGN); |
| 1983 | if (!pkt) { |
| 1984 | /* Give up on data, request rtx of events */ |
| 1985 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: rdlen %d chan %d\n", |
| 1986 | rdlen, chan); |
| 1987 | bus->drvr->rx_dropped++; |
| 1988 | brcmf_sdbrcm_rxfail(bus, false, RETRYCHAN(chan)); |
| 1989 | continue; |
| 1990 | } |
| 1991 | |
| 1992 | /* Leave room for what we already read, and align remainder */ |
| 1993 | skb_pull(pkt, BRCMF_FIRSTREAD); |
| 1994 | pkt_align(pkt, rdlen, BRCMF_SDALIGN); |
| 1995 | |
| 1996 | /* Read the remaining frame data */ |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 1997 | sdret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad, |
| 1998 | SDIO_FUNC_2, F2SYNC, pkt); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1999 | bus->f2rxdata++; |
| 2000 | |
| 2001 | if (sdret < 0) { |
| 2002 | brcmf_dbg(ERROR, "read %d %s bytes failed: %d\n", rdlen, |
| 2003 | ((chan == SDPCM_EVENT_CHANNEL) ? "event" |
| 2004 | : ((chan == SDPCM_DATA_CHANNEL) ? "data" |
| 2005 | : "test")), sdret); |
| 2006 | brcmu_pkt_buf_free_skb(pkt); |
| 2007 | bus->drvr->rx_errors++; |
| 2008 | brcmf_sdbrcm_rxfail(bus, true, RETRYCHAN(chan)); |
| 2009 | continue; |
| 2010 | } |
| 2011 | |
| 2012 | /* Copy the already-read portion */ |
| 2013 | skb_push(pkt, BRCMF_FIRSTREAD); |
| 2014 | memcpy(pkt->data, bus->rxhdr, BRCMF_FIRSTREAD); |
| 2015 | |
| 2016 | #ifdef BCMDBG |
| 2017 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 2018 | printk(KERN_DEBUG "Rx Data:\n"); |
| 2019 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2020 | pkt->data, len); |
| 2021 | } |
| 2022 | #endif |
| 2023 | |
| 2024 | deliver: |
| 2025 | /* Save superframe descriptor and allocate packet frame */ |
| 2026 | if (chan == SDPCM_GLOM_CHANNEL) { |
| 2027 | if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) { |
| 2028 | brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n", |
| 2029 | len); |
| 2030 | #ifdef BCMDBG |
| 2031 | if (BRCMF_GLOM_ON()) { |
| 2032 | printk(KERN_DEBUG "Glom Data:\n"); |
| 2033 | print_hex_dump_bytes("", |
| 2034 | DUMP_PREFIX_OFFSET, |
| 2035 | pkt->data, len); |
| 2036 | } |
| 2037 | #endif |
| 2038 | __skb_trim(pkt, len); |
| 2039 | skb_pull(pkt, SDPCM_HDRLEN); |
| 2040 | bus->glomd = pkt; |
| 2041 | } else { |
| 2042 | brcmf_dbg(ERROR, "%s: glom superframe w/o " |
| 2043 | "descriptor!\n", __func__); |
| 2044 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 2045 | } |
| 2046 | continue; |
| 2047 | } |
| 2048 | |
| 2049 | /* Fill in packet len and prio, deliver upward */ |
| 2050 | __skb_trim(pkt, len); |
| 2051 | skb_pull(pkt, doff); |
| 2052 | |
| 2053 | if (pkt->len == 0) { |
| 2054 | brcmu_pkt_buf_free_skb(pkt); |
| 2055 | continue; |
| 2056 | } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, pkt) != 0) { |
| 2057 | brcmf_dbg(ERROR, "rx protocol error\n"); |
| 2058 | brcmu_pkt_buf_free_skb(pkt); |
| 2059 | bus->drvr->rx_errors++; |
| 2060 | continue; |
| 2061 | } |
| 2062 | |
| 2063 | /* Unlock during rx call */ |
| 2064 | up(&bus->sdsem); |
Arend van Spriel | 0b45bf7 | 2011-11-22 17:21:36 -0800 | [diff] [blame] | 2065 | brcmf_rx_packet(bus->drvr, ifidx, pkt); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2066 | down(&bus->sdsem); |
| 2067 | } |
| 2068 | rxcount = maxframes - rxleft; |
| 2069 | #ifdef BCMDBG |
| 2070 | /* Message if we hit the limit */ |
| 2071 | if (!rxleft) |
| 2072 | brcmf_dbg(DATA, "hit rx limit of %d frames\n", |
| 2073 | maxframes); |
| 2074 | else |
| 2075 | #endif /* BCMDBG */ |
| 2076 | brcmf_dbg(DATA, "processed %d frames\n", rxcount); |
| 2077 | /* Back off rxseq if awaiting rtx, update rx_seq */ |
| 2078 | if (bus->rxskip) |
| 2079 | rxseq--; |
| 2080 | bus->rx_seq = rxseq; |
| 2081 | |
| 2082 | return rxcount; |
| 2083 | } |
| 2084 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2085 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2086 | brcmf_sdbrcm_wait_for_event(struct brcmf_sdio *bus, bool *lockvar) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2087 | { |
| 2088 | up(&bus->sdsem); |
| 2089 | wait_event_interruptible_timeout(bus->ctrl_wait, |
| 2090 | (*lockvar == false), HZ * 2); |
| 2091 | down(&bus->sdsem); |
| 2092 | return; |
| 2093 | } |
| 2094 | |
| 2095 | static void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2096 | brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2097 | { |
| 2098 | if (waitqueue_active(&bus->ctrl_wait)) |
| 2099 | wake_up_interruptible(&bus->ctrl_wait); |
| 2100 | return; |
| 2101 | } |
| 2102 | |
| 2103 | /* Writes a HW/SW header into the packet and sends it. */ |
| 2104 | /* Assumes: (a) header space already there, (b) caller holds lock */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2105 | static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2106 | uint chan, bool free_pkt) |
| 2107 | { |
| 2108 | int ret; |
| 2109 | u8 *frame; |
| 2110 | u16 len, pad = 0; |
| 2111 | u32 swheader; |
| 2112 | struct sk_buff *new; |
| 2113 | int i; |
| 2114 | |
| 2115 | brcmf_dbg(TRACE, "Enter\n"); |
| 2116 | |
| 2117 | frame = (u8 *) (pkt->data); |
| 2118 | |
| 2119 | /* Add alignment padding, allocate new packet if needed */ |
| 2120 | pad = ((unsigned long)frame % BRCMF_SDALIGN); |
| 2121 | if (pad) { |
| 2122 | if (skb_headroom(pkt) < pad) { |
| 2123 | brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n", |
| 2124 | skb_headroom(pkt), pad); |
| 2125 | bus->drvr->tx_realloc++; |
| 2126 | new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN); |
| 2127 | if (!new) { |
| 2128 | brcmf_dbg(ERROR, "couldn't allocate new %d-byte packet\n", |
| 2129 | pkt->len + BRCMF_SDALIGN); |
| 2130 | ret = -ENOMEM; |
| 2131 | goto done; |
| 2132 | } |
| 2133 | |
| 2134 | pkt_align(new, pkt->len, BRCMF_SDALIGN); |
| 2135 | memcpy(new->data, pkt->data, pkt->len); |
| 2136 | if (free_pkt) |
| 2137 | brcmu_pkt_buf_free_skb(pkt); |
| 2138 | /* free the pkt if canned one is not used */ |
| 2139 | free_pkt = true; |
| 2140 | pkt = new; |
| 2141 | frame = (u8 *) (pkt->data); |
| 2142 | /* precondition: (frame % BRCMF_SDALIGN) == 0) */ |
| 2143 | pad = 0; |
| 2144 | } else { |
| 2145 | skb_push(pkt, pad); |
| 2146 | frame = (u8 *) (pkt->data); |
| 2147 | /* precondition: pad + SDPCM_HDRLEN <= pkt->len */ |
| 2148 | memset(frame, 0, pad + SDPCM_HDRLEN); |
| 2149 | } |
| 2150 | } |
| 2151 | /* precondition: pad < BRCMF_SDALIGN */ |
| 2152 | |
| 2153 | /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */ |
| 2154 | len = (u16) (pkt->len); |
| 2155 | *(__le16 *) frame = cpu_to_le16(len); |
| 2156 | *(((__le16 *) frame) + 1) = cpu_to_le16(~len); |
| 2157 | |
| 2158 | /* Software tag: channel, sequence number, data offset */ |
| 2159 | swheader = |
| 2160 | ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq | |
| 2161 | (((pad + |
| 2162 | SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK); |
| 2163 | |
| 2164 | put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN); |
| 2165 | put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader)); |
| 2166 | |
| 2167 | #ifdef BCMDBG |
| 2168 | tx_packets[pkt->priority]++; |
| 2169 | if (BRCMF_BYTES_ON() && |
| 2170 | (((BRCMF_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) || |
| 2171 | (BRCMF_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) { |
| 2172 | printk(KERN_DEBUG "Tx Frame:\n"); |
| 2173 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len); |
| 2174 | } else if (BRCMF_HDRS_ON()) { |
| 2175 | printk(KERN_DEBUG "TxHdr:\n"); |
| 2176 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2177 | frame, min_t(u16, len, 16)); |
| 2178 | } |
| 2179 | #endif |
| 2180 | |
| 2181 | /* Raise len to next SDIO block to eliminate tail command */ |
| 2182 | if (bus->roundup && bus->blocksize && (len > bus->blocksize)) { |
| 2183 | u16 pad = bus->blocksize - (len % bus->blocksize); |
| 2184 | if ((pad <= bus->roundup) && (pad < bus->blocksize)) |
| 2185 | len += pad; |
| 2186 | } else if (len % BRCMF_SDALIGN) { |
| 2187 | len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN); |
| 2188 | } |
| 2189 | |
| 2190 | /* Some controllers have trouble with odd bytes -- round to even */ |
| 2191 | if (len & (ALIGNMENT - 1)) |
| 2192 | len = roundup(len, ALIGNMENT); |
| 2193 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 2194 | ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad, |
| 2195 | SDIO_FUNC_2, F2SYNC, pkt); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2196 | bus->f2txdata++; |
| 2197 | |
| 2198 | if (ret < 0) { |
| 2199 | /* On failure, abort the command and terminate the frame */ |
| 2200 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2201 | ret); |
| 2202 | bus->tx_sderrs++; |
| 2203 | |
| 2204 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2205 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2206 | SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, |
| 2207 | NULL); |
| 2208 | bus->f1regdata++; |
| 2209 | |
| 2210 | for (i = 0; i < 3; i++) { |
| 2211 | u8 hi, lo; |
| 2212 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2213 | SDIO_FUNC_1, |
| 2214 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2215 | NULL); |
| 2216 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2217 | SDIO_FUNC_1, |
| 2218 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2219 | NULL); |
| 2220 | bus->f1regdata += 2; |
| 2221 | if ((hi == 0) && (lo == 0)) |
| 2222 | break; |
| 2223 | } |
| 2224 | |
| 2225 | } |
| 2226 | if (ret == 0) |
| 2227 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2228 | |
| 2229 | done: |
| 2230 | /* restore pkt buffer pointer before calling tx complete routine */ |
| 2231 | skb_pull(pkt, SDPCM_HDRLEN + pad); |
| 2232 | up(&bus->sdsem); |
| 2233 | brcmf_txcomplete(bus->drvr, pkt, ret != 0); |
| 2234 | down(&bus->sdsem); |
| 2235 | |
| 2236 | if (free_pkt) |
| 2237 | brcmu_pkt_buf_free_skb(pkt); |
| 2238 | |
| 2239 | return ret; |
| 2240 | } |
| 2241 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2242 | static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2243 | { |
| 2244 | struct sk_buff *pkt; |
| 2245 | u32 intstatus = 0; |
| 2246 | uint retries = 0; |
| 2247 | int ret = 0, prec_out; |
| 2248 | uint cnt = 0; |
| 2249 | uint datalen; |
| 2250 | u8 tx_prec_map; |
| 2251 | |
| 2252 | struct brcmf_pub *drvr = bus->drvr; |
| 2253 | |
| 2254 | brcmf_dbg(TRACE, "Enter\n"); |
| 2255 | |
| 2256 | tx_prec_map = ~bus->flowcontrol; |
| 2257 | |
| 2258 | /* Send frames until the limit or some other event */ |
| 2259 | for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) { |
| 2260 | spin_lock_bh(&bus->txqlock); |
| 2261 | pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out); |
| 2262 | if (pkt == NULL) { |
| 2263 | spin_unlock_bh(&bus->txqlock); |
| 2264 | break; |
| 2265 | } |
| 2266 | spin_unlock_bh(&bus->txqlock); |
| 2267 | datalen = pkt->len - SDPCM_HDRLEN; |
| 2268 | |
| 2269 | ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true); |
| 2270 | if (ret) |
| 2271 | bus->drvr->tx_errors++; |
| 2272 | else |
| 2273 | bus->drvr->dstats.tx_bytes += datalen; |
| 2274 | |
| 2275 | /* In poll mode, need to check for other events */ |
| 2276 | if (!bus->intr && cnt) { |
| 2277 | /* Check device status, signal pending interrupt */ |
| 2278 | r_sdreg32(bus, &intstatus, |
| 2279 | offsetof(struct sdpcmd_regs, intstatus), |
| 2280 | &retries); |
| 2281 | bus->f2txdata++; |
| 2282 | if (brcmf_sdcard_regfail(bus->sdiodev)) |
| 2283 | break; |
| 2284 | if (intstatus & bus->hostintmask) |
| 2285 | bus->ipend = true; |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | /* Deflow-control stack if needed */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2290 | if (drvr->up && (drvr->bus_if->state == BRCMF_BUS_DATA) && |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2291 | drvr->txoff && (pktq_len(&bus->txq) < TXLOW)) |
| 2292 | brcmf_txflowcontrol(drvr, 0, OFF); |
| 2293 | |
| 2294 | return cnt; |
| 2295 | } |
| 2296 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2297 | static bool brcmf_sdbrcm_dpc(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2298 | { |
| 2299 | u32 intstatus, newstatus = 0; |
| 2300 | uint retries = 0; |
| 2301 | uint rxlimit = bus->rxbound; /* Rx frames to read before resched */ |
| 2302 | uint txlimit = bus->txbound; /* Tx frames to send before resched */ |
| 2303 | uint framecnt = 0; /* Temporary counter of tx/rx frames */ |
| 2304 | bool rxdone = true; /* Flag for no more read data */ |
| 2305 | bool resched = false; /* Flag indicating resched wanted */ |
| 2306 | |
| 2307 | brcmf_dbg(TRACE, "Enter\n"); |
| 2308 | |
| 2309 | /* Start with leftover status bits */ |
| 2310 | intstatus = bus->intstatus; |
| 2311 | |
| 2312 | down(&bus->sdsem); |
| 2313 | |
| 2314 | /* If waiting for HTAVAIL, check status */ |
| 2315 | if (bus->clkstate == CLK_PENDING) { |
| 2316 | int err; |
| 2317 | u8 clkctl, devctl = 0; |
| 2318 | |
| 2319 | #ifdef BCMDBG |
| 2320 | /* Check for inconsistent device control */ |
| 2321 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2322 | SBSDIO_DEVICE_CTL, &err); |
| 2323 | if (err) { |
| 2324 | brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", err); |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2325 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2326 | } |
| 2327 | #endif /* BCMDBG */ |
| 2328 | |
| 2329 | /* Read CSR, if clock on switch to AVAIL, else ignore */ |
| 2330 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2331 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 2332 | if (err) { |
| 2333 | brcmf_dbg(ERROR, "error reading CSR: %d\n", |
| 2334 | err); |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2335 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | brcmf_dbg(INFO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", |
| 2339 | devctl, clkctl); |
| 2340 | |
| 2341 | if (SBSDIO_HTAV(clkctl)) { |
| 2342 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2343 | SDIO_FUNC_1, |
| 2344 | SBSDIO_DEVICE_CTL, &err); |
| 2345 | if (err) { |
| 2346 | brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", |
| 2347 | err); |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2348 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2349 | } |
| 2350 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 2351 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2352 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 2353 | if (err) { |
| 2354 | brcmf_dbg(ERROR, "error writing DEVCTL: %d\n", |
| 2355 | err); |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2356 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2357 | } |
| 2358 | bus->clkstate = CLK_AVAIL; |
| 2359 | } else { |
| 2360 | goto clkwait; |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | bus_wake(bus); |
| 2365 | |
| 2366 | /* Make sure backplane clock is on */ |
| 2367 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true); |
| 2368 | if (bus->clkstate == CLK_PENDING) |
| 2369 | goto clkwait; |
| 2370 | |
| 2371 | /* Pending interrupt indicates new device status */ |
| 2372 | if (bus->ipend) { |
| 2373 | bus->ipend = false; |
| 2374 | r_sdreg32(bus, &newstatus, |
| 2375 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2376 | bus->f1regdata++; |
| 2377 | if (brcmf_sdcard_regfail(bus->sdiodev)) |
| 2378 | newstatus = 0; |
| 2379 | newstatus &= bus->hostintmask; |
| 2380 | bus->fcstate = !!(newstatus & I_HMB_FC_STATE); |
| 2381 | if (newstatus) { |
| 2382 | w_sdreg32(bus, newstatus, |
| 2383 | offsetof(struct sdpcmd_regs, intstatus), |
| 2384 | &retries); |
| 2385 | bus->f1regdata++; |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | /* Merge new bits with previous */ |
| 2390 | intstatus |= newstatus; |
| 2391 | bus->intstatus = 0; |
| 2392 | |
| 2393 | /* Handle flow-control change: read new state in case our ack |
| 2394 | * crossed another change interrupt. If change still set, assume |
| 2395 | * FC ON for safety, let next loop through do the debounce. |
| 2396 | */ |
| 2397 | if (intstatus & I_HMB_FC_CHANGE) { |
| 2398 | intstatus &= ~I_HMB_FC_CHANGE; |
| 2399 | w_sdreg32(bus, I_HMB_FC_CHANGE, |
| 2400 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2401 | |
| 2402 | r_sdreg32(bus, &newstatus, |
| 2403 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2404 | bus->f1regdata += 2; |
| 2405 | bus->fcstate = |
| 2406 | !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)); |
| 2407 | intstatus |= (newstatus & bus->hostintmask); |
| 2408 | } |
| 2409 | |
| 2410 | /* Handle host mailbox indication */ |
| 2411 | if (intstatus & I_HMB_HOST_INT) { |
| 2412 | intstatus &= ~I_HMB_HOST_INT; |
| 2413 | intstatus |= brcmf_sdbrcm_hostmail(bus); |
| 2414 | } |
| 2415 | |
| 2416 | /* Generally don't ask for these, can get CRC errors... */ |
| 2417 | if (intstatus & I_WR_OOSYNC) { |
| 2418 | brcmf_dbg(ERROR, "Dongle reports WR_OOSYNC\n"); |
| 2419 | intstatus &= ~I_WR_OOSYNC; |
| 2420 | } |
| 2421 | |
| 2422 | if (intstatus & I_RD_OOSYNC) { |
| 2423 | brcmf_dbg(ERROR, "Dongle reports RD_OOSYNC\n"); |
| 2424 | intstatus &= ~I_RD_OOSYNC; |
| 2425 | } |
| 2426 | |
| 2427 | if (intstatus & I_SBINT) { |
| 2428 | brcmf_dbg(ERROR, "Dongle reports SBINT\n"); |
| 2429 | intstatus &= ~I_SBINT; |
| 2430 | } |
| 2431 | |
| 2432 | /* Would be active due to wake-wlan in gSPI */ |
| 2433 | if (intstatus & I_CHIPACTIVE) { |
| 2434 | brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n"); |
| 2435 | intstatus &= ~I_CHIPACTIVE; |
| 2436 | } |
| 2437 | |
| 2438 | /* Ignore frame indications if rxskip is set */ |
| 2439 | if (bus->rxskip) |
| 2440 | intstatus &= ~I_HMB_FRAME_IND; |
| 2441 | |
| 2442 | /* On frame indication, read available frames */ |
| 2443 | if (PKT_AVAILABLE()) { |
| 2444 | framecnt = brcmf_sdbrcm_readframes(bus, rxlimit, &rxdone); |
| 2445 | if (rxdone || bus->rxskip) |
| 2446 | intstatus &= ~I_HMB_FRAME_IND; |
| 2447 | rxlimit -= min(framecnt, rxlimit); |
| 2448 | } |
| 2449 | |
| 2450 | /* Keep still-pending events for next scheduling */ |
| 2451 | bus->intstatus = intstatus; |
| 2452 | |
| 2453 | clkwait: |
| 2454 | if (data_ok(bus) && bus->ctrl_frame_stat && |
| 2455 | (bus->clkstate == CLK_AVAIL)) { |
| 2456 | int ret, i; |
| 2457 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 2458 | ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2459 | SDIO_FUNC_2, F2SYNC, (u8 *) bus->ctrl_frame_buf, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 2460 | (u32) bus->ctrl_frame_len); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2461 | |
| 2462 | if (ret < 0) { |
| 2463 | /* On failure, abort the command and |
| 2464 | terminate the frame */ |
| 2465 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2466 | ret); |
| 2467 | bus->tx_sderrs++; |
| 2468 | |
| 2469 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2470 | |
| 2471 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2472 | SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, |
| 2473 | NULL); |
| 2474 | bus->f1regdata++; |
| 2475 | |
| 2476 | for (i = 0; i < 3; i++) { |
| 2477 | u8 hi, lo; |
| 2478 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2479 | SDIO_FUNC_1, |
| 2480 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2481 | NULL); |
| 2482 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2483 | SDIO_FUNC_1, |
| 2484 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2485 | NULL); |
| 2486 | bus->f1regdata += 2; |
| 2487 | if ((hi == 0) && (lo == 0)) |
| 2488 | break; |
| 2489 | } |
| 2490 | |
| 2491 | } |
| 2492 | if (ret == 0) |
| 2493 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2494 | |
| 2495 | brcmf_dbg(INFO, "Return_dpc value is : %d\n", ret); |
| 2496 | bus->ctrl_frame_stat = false; |
| 2497 | brcmf_sdbrcm_wait_event_wakeup(bus); |
| 2498 | } |
| 2499 | /* Send queued frames (limit 1 if rx may still be pending) */ |
| 2500 | else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate && |
| 2501 | brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit |
| 2502 | && data_ok(bus)) { |
| 2503 | framecnt = rxdone ? txlimit : min(txlimit, bus->txminmax); |
| 2504 | framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt); |
| 2505 | txlimit -= framecnt; |
| 2506 | } |
| 2507 | |
| 2508 | /* Resched if events or tx frames are pending, |
| 2509 | else await next interrupt */ |
| 2510 | /* On failed register access, all bets are off: |
| 2511 | no resched or interrupts */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2512 | if ((bus->drvr->bus_if->state == BRCMF_BUS_DOWN) || |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2513 | brcmf_sdcard_regfail(bus->sdiodev)) { |
| 2514 | brcmf_dbg(ERROR, "failed backplane access over SDIO, halting operation %d\n", |
| 2515 | brcmf_sdcard_regfail(bus->sdiodev)); |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2516 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2517 | bus->intstatus = 0; |
| 2518 | } else if (bus->clkstate == CLK_PENDING) { |
| 2519 | brcmf_dbg(INFO, "rescheduled due to CLK_PENDING awaiting I_CHIPACTIVE interrupt\n"); |
| 2520 | resched = true; |
| 2521 | } else if (bus->intstatus || bus->ipend || |
| 2522 | (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) |
| 2523 | && data_ok(bus)) || PKT_AVAILABLE()) { |
| 2524 | resched = true; |
| 2525 | } |
| 2526 | |
| 2527 | bus->dpc_sched = resched; |
| 2528 | |
| 2529 | /* If we're done for now, turn off clock request. */ |
| 2530 | if ((bus->clkstate != CLK_PENDING) |
| 2531 | && bus->idletime == BRCMF_IDLE_IMMEDIATE) { |
| 2532 | bus->activity = false; |
| 2533 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 2534 | } |
| 2535 | |
| 2536 | up(&bus->sdsem); |
| 2537 | |
| 2538 | return resched; |
| 2539 | } |
| 2540 | |
| 2541 | static int brcmf_sdbrcm_dpc_thread(void *data) |
| 2542 | { |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2543 | struct brcmf_sdio *bus = (struct brcmf_sdio *) data; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2544 | |
| 2545 | allow_signal(SIGTERM); |
| 2546 | /* Run until signal received */ |
| 2547 | while (1) { |
| 2548 | if (kthread_should_stop()) |
| 2549 | break; |
| 2550 | if (!wait_for_completion_interruptible(&bus->dpc_wait)) { |
| 2551 | /* Call bus dpc unless it indicated down |
| 2552 | (then clean stop) */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 2553 | if (bus->drvr->bus_if->state != BRCMF_BUS_DOWN) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2554 | if (brcmf_sdbrcm_dpc(bus)) |
| 2555 | complete(&bus->dpc_wait); |
| 2556 | } else { |
| 2557 | /* after stopping the bus, exit thread */ |
Franky Lin | 94c2fb8 | 2011-11-22 17:21:59 -0800 | [diff] [blame^] | 2558 | brcmf_sdbrcm_bus_stop(bus->sdiodev->dev); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2559 | bus->dpc_tsk = NULL; |
| 2560 | break; |
| 2561 | } |
| 2562 | } else |
| 2563 | break; |
| 2564 | } |
| 2565 | return 0; |
| 2566 | } |
| 2567 | |
Franky Lin | bf347bb | 2011-11-22 17:21:56 -0800 | [diff] [blame] | 2568 | int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2569 | { |
| 2570 | int ret = -EBADE; |
| 2571 | uint datalen, prec; |
Franky Lin | bf347bb | 2011-11-22 17:21:56 -0800 | [diff] [blame] | 2572 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 2573 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv; |
| 2574 | struct brcmf_sdio *bus = sdiodev->bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2575 | |
| 2576 | brcmf_dbg(TRACE, "Enter\n"); |
| 2577 | |
| 2578 | datalen = pkt->len; |
| 2579 | |
| 2580 | /* Add space for the header */ |
| 2581 | skb_push(pkt, SDPCM_HDRLEN); |
| 2582 | /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */ |
| 2583 | |
| 2584 | prec = prio2prec((pkt->priority & PRIOMASK)); |
| 2585 | |
| 2586 | /* Check for existing queue, current flow-control, |
| 2587 | pending event, or pending clock */ |
| 2588 | brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq)); |
| 2589 | bus->fcqueued++; |
| 2590 | |
| 2591 | /* Priority based enq */ |
| 2592 | spin_lock_bh(&bus->txqlock); |
| 2593 | if (brcmf_c_prec_enq(bus->drvr, &bus->txq, pkt, prec) == false) { |
| 2594 | skb_pull(pkt, SDPCM_HDRLEN); |
| 2595 | brcmf_txcomplete(bus->drvr, pkt, false); |
| 2596 | brcmu_pkt_buf_free_skb(pkt); |
| 2597 | brcmf_dbg(ERROR, "out of bus->txq !!!\n"); |
| 2598 | ret = -ENOSR; |
| 2599 | } else { |
| 2600 | ret = 0; |
| 2601 | } |
| 2602 | spin_unlock_bh(&bus->txqlock); |
| 2603 | |
| 2604 | if (pktq_len(&bus->txq) >= TXHI) |
| 2605 | brcmf_txflowcontrol(bus->drvr, 0, ON); |
| 2606 | |
| 2607 | #ifdef BCMDBG |
| 2608 | if (pktq_plen(&bus->txq, prec) > qcount[prec]) |
| 2609 | qcount[prec] = pktq_plen(&bus->txq, prec); |
| 2610 | #endif |
| 2611 | /* Schedule DPC if needed to send queued packet(s) */ |
| 2612 | if (!bus->dpc_sched) { |
| 2613 | bus->dpc_sched = true; |
| 2614 | if (bus->dpc_tsk) |
| 2615 | complete(&bus->dpc_wait); |
| 2616 | } |
| 2617 | |
| 2618 | return ret; |
| 2619 | } |
| 2620 | |
| 2621 | static int |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2622 | brcmf_sdbrcm_membytes(struct brcmf_sdio *bus, bool write, u32 address, u8 *data, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2623 | uint size) |
| 2624 | { |
| 2625 | int bcmerror = 0; |
| 2626 | u32 sdaddr; |
| 2627 | uint dsize; |
| 2628 | |
| 2629 | /* Determine initial transfer parameters */ |
| 2630 | sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; |
| 2631 | if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) |
| 2632 | dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); |
| 2633 | else |
| 2634 | dsize = size; |
| 2635 | |
| 2636 | /* Set the backplane window to include the start address */ |
| 2637 | bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, address); |
| 2638 | if (bcmerror) { |
| 2639 | brcmf_dbg(ERROR, "window change failed\n"); |
| 2640 | goto xfer_done; |
| 2641 | } |
| 2642 | |
| 2643 | /* Do the transfer(s) */ |
| 2644 | while (size) { |
| 2645 | brcmf_dbg(INFO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", |
| 2646 | write ? "write" : "read", dsize, |
| 2647 | sdaddr, address & SBSDIO_SBWINDOW_MASK); |
| 2648 | bcmerror = brcmf_sdcard_rwdata(bus->sdiodev, write, |
| 2649 | sdaddr, data, dsize); |
| 2650 | if (bcmerror) { |
| 2651 | brcmf_dbg(ERROR, "membytes transfer failed\n"); |
| 2652 | break; |
| 2653 | } |
| 2654 | |
| 2655 | /* Adjust for next transfer (if any) */ |
| 2656 | size -= dsize; |
| 2657 | if (size) { |
| 2658 | data += dsize; |
| 2659 | address += dsize; |
| 2660 | bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, |
| 2661 | address); |
| 2662 | if (bcmerror) { |
| 2663 | brcmf_dbg(ERROR, "window change failed\n"); |
| 2664 | break; |
| 2665 | } |
| 2666 | sdaddr = 0; |
| 2667 | dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | xfer_done: |
| 2672 | /* Return the window to backplane enumeration space for core access */ |
| 2673 | if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, bus->sdiodev->sbwad)) |
| 2674 | brcmf_dbg(ERROR, "FAILED to set window back to 0x%x\n", |
| 2675 | bus->sdiodev->sbwad); |
| 2676 | |
| 2677 | return bcmerror; |
| 2678 | } |
| 2679 | |
| 2680 | #ifdef BCMDBG |
| 2681 | #define CONSOLE_LINE_MAX 192 |
| 2682 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2683 | static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2684 | { |
| 2685 | struct brcmf_console *c = &bus->console; |
| 2686 | u8 line[CONSOLE_LINE_MAX], ch; |
| 2687 | u32 n, idx, addr; |
| 2688 | int rv; |
| 2689 | |
| 2690 | /* Don't do anything until FWREADY updates console address */ |
| 2691 | if (bus->console_addr == 0) |
| 2692 | return 0; |
| 2693 | |
| 2694 | /* Read console log struct */ |
| 2695 | addr = bus->console_addr + offsetof(struct rte_console, log_le); |
| 2696 | rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log_le, |
| 2697 | sizeof(c->log_le)); |
| 2698 | if (rv < 0) |
| 2699 | return rv; |
| 2700 | |
| 2701 | /* Allocate console buffer (one time only) */ |
| 2702 | if (c->buf == NULL) { |
| 2703 | c->bufsize = le32_to_cpu(c->log_le.buf_size); |
| 2704 | c->buf = kmalloc(c->bufsize, GFP_ATOMIC); |
| 2705 | if (c->buf == NULL) |
| 2706 | return -ENOMEM; |
| 2707 | } |
| 2708 | |
| 2709 | idx = le32_to_cpu(c->log_le.idx); |
| 2710 | |
| 2711 | /* Protect against corrupt value */ |
| 2712 | if (idx > c->bufsize) |
| 2713 | return -EBADE; |
| 2714 | |
| 2715 | /* Skip reading the console buffer if the index pointer |
| 2716 | has not moved */ |
| 2717 | if (idx == c->last) |
| 2718 | return 0; |
| 2719 | |
| 2720 | /* Read the console buffer */ |
| 2721 | addr = le32_to_cpu(c->log_le.buf); |
| 2722 | rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize); |
| 2723 | if (rv < 0) |
| 2724 | return rv; |
| 2725 | |
| 2726 | while (c->last != idx) { |
| 2727 | for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) { |
| 2728 | if (c->last == idx) { |
| 2729 | /* This would output a partial line. |
| 2730 | * Instead, back up |
| 2731 | * the buffer pointer and output this |
| 2732 | * line next time around. |
| 2733 | */ |
| 2734 | if (c->last >= n) |
| 2735 | c->last -= n; |
| 2736 | else |
| 2737 | c->last = c->bufsize - n; |
| 2738 | goto break2; |
| 2739 | } |
| 2740 | ch = c->buf[c->last]; |
| 2741 | c->last = (c->last + 1) % c->bufsize; |
| 2742 | if (ch == '\n') |
| 2743 | break; |
| 2744 | line[n] = ch; |
| 2745 | } |
| 2746 | |
| 2747 | if (n > 0) { |
| 2748 | if (line[n - 1] == '\r') |
| 2749 | n--; |
| 2750 | line[n] = 0; |
| 2751 | printk(KERN_DEBUG "CONSOLE: %s\n", line); |
| 2752 | } |
| 2753 | } |
| 2754 | break2: |
| 2755 | |
| 2756 | return 0; |
| 2757 | } |
| 2758 | #endif /* BCMDBG */ |
| 2759 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2760 | static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2761 | { |
| 2762 | int i; |
| 2763 | int ret; |
| 2764 | |
| 2765 | bus->ctrl_frame_stat = false; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 2766 | ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad, |
| 2767 | SDIO_FUNC_2, F2SYNC, frame, len); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2768 | |
| 2769 | if (ret < 0) { |
| 2770 | /* On failure, abort the command and terminate the frame */ |
| 2771 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2772 | ret); |
| 2773 | bus->tx_sderrs++; |
| 2774 | |
| 2775 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2776 | |
| 2777 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2778 | SBSDIO_FUNC1_FRAMECTRL, |
| 2779 | SFC_WF_TERM, NULL); |
| 2780 | bus->f1regdata++; |
| 2781 | |
| 2782 | for (i = 0; i < 3; i++) { |
| 2783 | u8 hi, lo; |
| 2784 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2785 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2786 | NULL); |
| 2787 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2788 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2789 | NULL); |
| 2790 | bus->f1regdata += 2; |
| 2791 | if (hi == 0 && lo == 0) |
| 2792 | break; |
| 2793 | } |
| 2794 | return ret; |
| 2795 | } |
| 2796 | |
| 2797 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2798 | |
| 2799 | return ret; |
| 2800 | } |
| 2801 | |
| 2802 | int |
Franky Lin | 47a1ce7 | 2011-11-22 17:21:55 -0800 | [diff] [blame] | 2803 | brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2804 | { |
| 2805 | u8 *frame; |
| 2806 | u16 len; |
| 2807 | u32 swheader; |
| 2808 | uint retries = 0; |
| 2809 | u8 doff = 0; |
| 2810 | int ret = -1; |
Franky Lin | 47a1ce7 | 2011-11-22 17:21:55 -0800 | [diff] [blame] | 2811 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 2812 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv; |
| 2813 | struct brcmf_sdio *bus = sdiodev->bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2814 | |
| 2815 | brcmf_dbg(TRACE, "Enter\n"); |
| 2816 | |
| 2817 | /* Back the pointer to make a room for bus header */ |
| 2818 | frame = msg - SDPCM_HDRLEN; |
| 2819 | len = (msglen += SDPCM_HDRLEN); |
| 2820 | |
| 2821 | /* Add alignment padding (optional for ctl frames) */ |
| 2822 | doff = ((unsigned long)frame % BRCMF_SDALIGN); |
| 2823 | if (doff) { |
| 2824 | frame -= doff; |
| 2825 | len += doff; |
| 2826 | msglen += doff; |
| 2827 | memset(frame, 0, doff + SDPCM_HDRLEN); |
| 2828 | } |
| 2829 | /* precondition: doff < BRCMF_SDALIGN */ |
| 2830 | doff += SDPCM_HDRLEN; |
| 2831 | |
| 2832 | /* Round send length to next SDIO block */ |
| 2833 | if (bus->roundup && bus->blocksize && (len > bus->blocksize)) { |
| 2834 | u16 pad = bus->blocksize - (len % bus->blocksize); |
| 2835 | if ((pad <= bus->roundup) && (pad < bus->blocksize)) |
| 2836 | len += pad; |
| 2837 | } else if (len % BRCMF_SDALIGN) { |
| 2838 | len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN); |
| 2839 | } |
| 2840 | |
| 2841 | /* Satisfy length-alignment requirements */ |
| 2842 | if (len & (ALIGNMENT - 1)) |
| 2843 | len = roundup(len, ALIGNMENT); |
| 2844 | |
| 2845 | /* precondition: IS_ALIGNED((unsigned long)frame, 2) */ |
| 2846 | |
| 2847 | /* Need to lock here to protect txseq and SDIO tx calls */ |
| 2848 | down(&bus->sdsem); |
| 2849 | |
| 2850 | bus_wake(bus); |
| 2851 | |
| 2852 | /* Make sure backplane clock is on */ |
| 2853 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 2854 | |
| 2855 | /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */ |
| 2856 | *(__le16 *) frame = cpu_to_le16((u16) msglen); |
| 2857 | *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen); |
| 2858 | |
| 2859 | /* Software tag: channel, sequence number, data offset */ |
| 2860 | swheader = |
| 2861 | ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) & |
| 2862 | SDPCM_CHANNEL_MASK) |
| 2863 | | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) & |
| 2864 | SDPCM_DOFFSET_MASK); |
| 2865 | put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN); |
| 2866 | put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader)); |
| 2867 | |
| 2868 | if (!data_ok(bus)) { |
| 2869 | brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n", |
| 2870 | bus->tx_max, bus->tx_seq); |
| 2871 | bus->ctrl_frame_stat = true; |
| 2872 | /* Send from dpc */ |
| 2873 | bus->ctrl_frame_buf = frame; |
| 2874 | bus->ctrl_frame_len = len; |
| 2875 | |
| 2876 | brcmf_sdbrcm_wait_for_event(bus, &bus->ctrl_frame_stat); |
| 2877 | |
| 2878 | if (bus->ctrl_frame_stat == false) { |
| 2879 | brcmf_dbg(INFO, "ctrl_frame_stat == false\n"); |
| 2880 | ret = 0; |
| 2881 | } else { |
| 2882 | brcmf_dbg(INFO, "ctrl_frame_stat == true\n"); |
| 2883 | ret = -1; |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | if (ret == -1) { |
| 2888 | #ifdef BCMDBG |
| 2889 | if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) { |
| 2890 | printk(KERN_DEBUG "Tx Frame:\n"); |
| 2891 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2892 | frame, len); |
| 2893 | } else if (BRCMF_HDRS_ON()) { |
| 2894 | printk(KERN_DEBUG "TxHdr:\n"); |
| 2895 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2896 | frame, min_t(u16, len, 16)); |
| 2897 | } |
| 2898 | #endif |
| 2899 | |
| 2900 | do { |
| 2901 | ret = brcmf_tx_frame(bus, frame, len); |
| 2902 | } while (ret < 0 && retries++ < TXRETRIES); |
| 2903 | } |
| 2904 | |
| 2905 | if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) { |
| 2906 | bus->activity = false; |
| 2907 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, true); |
| 2908 | } |
| 2909 | |
| 2910 | up(&bus->sdsem); |
| 2911 | |
| 2912 | if (ret) |
| 2913 | bus->drvr->tx_ctlerrs++; |
| 2914 | else |
| 2915 | bus->drvr->tx_ctlpkts++; |
| 2916 | |
| 2917 | return ret ? -EIO : 0; |
| 2918 | } |
| 2919 | |
| 2920 | int |
Franky Lin | 532cdd3 | 2011-11-22 17:21:54 -0800 | [diff] [blame] | 2921 | brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2922 | { |
| 2923 | int timeleft; |
| 2924 | uint rxlen = 0; |
| 2925 | bool pending; |
Franky Lin | 532cdd3 | 2011-11-22 17:21:54 -0800 | [diff] [blame] | 2926 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 2927 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv; |
| 2928 | struct brcmf_sdio *bus = sdiodev->bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2929 | |
| 2930 | brcmf_dbg(TRACE, "Enter\n"); |
| 2931 | |
| 2932 | /* Wait until control frame is available */ |
| 2933 | timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending); |
| 2934 | |
| 2935 | down(&bus->sdsem); |
| 2936 | rxlen = bus->rxlen; |
| 2937 | memcpy(msg, bus->rxctl, min(msglen, rxlen)); |
| 2938 | bus->rxlen = 0; |
| 2939 | up(&bus->sdsem); |
| 2940 | |
| 2941 | if (rxlen) { |
| 2942 | brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n", |
| 2943 | rxlen, msglen); |
| 2944 | } else if (timeleft == 0) { |
| 2945 | brcmf_dbg(ERROR, "resumed on timeout\n"); |
| 2946 | } else if (pending == true) { |
| 2947 | brcmf_dbg(CTL, "cancelled\n"); |
| 2948 | return -ERESTARTSYS; |
| 2949 | } else { |
| 2950 | brcmf_dbg(CTL, "resumed for unknown reason?\n"); |
| 2951 | } |
| 2952 | |
| 2953 | if (rxlen) |
| 2954 | bus->drvr->rx_ctlpkts++; |
| 2955 | else |
| 2956 | bus->drvr->rx_ctlerrs++; |
| 2957 | |
| 2958 | return rxlen ? (int)rxlen : -ETIMEDOUT; |
| 2959 | } |
| 2960 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2961 | static int brcmf_sdbrcm_downloadvars(struct brcmf_sdio *bus, void *arg, int len) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2962 | { |
| 2963 | int bcmerror = 0; |
| 2964 | |
| 2965 | brcmf_dbg(TRACE, "Enter\n"); |
| 2966 | |
| 2967 | /* Basic sanity checks */ |
| 2968 | if (bus->drvr->up) { |
| 2969 | bcmerror = -EISCONN; |
| 2970 | goto err; |
| 2971 | } |
| 2972 | if (!len) { |
| 2973 | bcmerror = -EOVERFLOW; |
| 2974 | goto err; |
| 2975 | } |
| 2976 | |
| 2977 | /* Free the old ones and replace with passed variables */ |
| 2978 | kfree(bus->vars); |
| 2979 | |
| 2980 | bus->vars = kmalloc(len, GFP_ATOMIC); |
| 2981 | bus->varsz = bus->vars ? len : 0; |
| 2982 | if (bus->vars == NULL) { |
| 2983 | bcmerror = -ENOMEM; |
| 2984 | goto err; |
| 2985 | } |
| 2986 | |
| 2987 | /* Copy the passed variables, which should include the |
| 2988 | terminating double-null */ |
| 2989 | memcpy(bus->vars, arg, bus->varsz); |
| 2990 | err: |
| 2991 | return bcmerror; |
| 2992 | } |
| 2993 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 2994 | static int brcmf_sdbrcm_write_vars(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 2995 | { |
| 2996 | int bcmerror = 0; |
| 2997 | u32 varsize; |
| 2998 | u32 varaddr; |
| 2999 | u8 *vbuffer; |
| 3000 | u32 varsizew; |
| 3001 | __le32 varsizew_le; |
| 3002 | #ifdef BCMDBG |
| 3003 | char *nvram_ularray; |
| 3004 | #endif /* BCMDBG */ |
| 3005 | |
| 3006 | /* Even if there are no vars are to be written, we still |
| 3007 | need to set the ramsize. */ |
| 3008 | varsize = bus->varsz ? roundup(bus->varsz, 4) : 0; |
| 3009 | varaddr = (bus->ramsize - 4) - varsize; |
| 3010 | |
| 3011 | if (bus->vars) { |
| 3012 | vbuffer = kzalloc(varsize, GFP_ATOMIC); |
| 3013 | if (!vbuffer) |
| 3014 | return -ENOMEM; |
| 3015 | |
| 3016 | memcpy(vbuffer, bus->vars, bus->varsz); |
| 3017 | |
| 3018 | /* Write the vars list */ |
| 3019 | bcmerror = |
| 3020 | brcmf_sdbrcm_membytes(bus, true, varaddr, vbuffer, varsize); |
| 3021 | #ifdef BCMDBG |
| 3022 | /* Verify NVRAM bytes */ |
| 3023 | brcmf_dbg(INFO, "Compare NVRAM dl & ul; varsize=%d\n", varsize); |
| 3024 | nvram_ularray = kmalloc(varsize, GFP_ATOMIC); |
| 3025 | if (!nvram_ularray) |
| 3026 | return -ENOMEM; |
| 3027 | |
| 3028 | /* Upload image to verify downloaded contents. */ |
| 3029 | memset(nvram_ularray, 0xaa, varsize); |
| 3030 | |
| 3031 | /* Read the vars list to temp buffer for comparison */ |
| 3032 | bcmerror = |
| 3033 | brcmf_sdbrcm_membytes(bus, false, varaddr, nvram_ularray, |
| 3034 | varsize); |
| 3035 | if (bcmerror) { |
| 3036 | brcmf_dbg(ERROR, "error %d on reading %d nvram bytes at 0x%08x\n", |
| 3037 | bcmerror, varsize, varaddr); |
| 3038 | } |
| 3039 | /* Compare the org NVRAM with the one read from RAM */ |
| 3040 | if (memcmp(vbuffer, nvram_ularray, varsize)) |
| 3041 | brcmf_dbg(ERROR, "Downloaded NVRAM image is corrupted\n"); |
| 3042 | else |
| 3043 | brcmf_dbg(ERROR, "Download/Upload/Compare of NVRAM ok\n"); |
| 3044 | |
| 3045 | kfree(nvram_ularray); |
| 3046 | #endif /* BCMDBG */ |
| 3047 | |
| 3048 | kfree(vbuffer); |
| 3049 | } |
| 3050 | |
| 3051 | /* adjust to the user specified RAM */ |
| 3052 | brcmf_dbg(INFO, "Physical memory size: %d\n", bus->ramsize); |
| 3053 | brcmf_dbg(INFO, "Vars are at %d, orig varsize is %d\n", |
| 3054 | varaddr, varsize); |
| 3055 | varsize = ((bus->ramsize - 4) - varaddr); |
| 3056 | |
| 3057 | /* |
| 3058 | * Determine the length token: |
| 3059 | * Varsize, converted to words, in lower 16-bits, checksum |
| 3060 | * in upper 16-bits. |
| 3061 | */ |
| 3062 | if (bcmerror) { |
| 3063 | varsizew = 0; |
| 3064 | varsizew_le = cpu_to_le32(0); |
| 3065 | } else { |
| 3066 | varsizew = varsize / 4; |
| 3067 | varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF); |
| 3068 | varsizew_le = cpu_to_le32(varsizew); |
| 3069 | } |
| 3070 | |
| 3071 | brcmf_dbg(INFO, "New varsize is %d, length token=0x%08x\n", |
| 3072 | varsize, varsizew); |
| 3073 | |
| 3074 | /* Write the length token to the last word */ |
| 3075 | bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->ramsize - 4), |
| 3076 | (u8 *)&varsizew_le, 4); |
| 3077 | |
| 3078 | return bcmerror; |
| 3079 | } |
| 3080 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3081 | static int brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3082 | { |
| 3083 | uint retries; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3084 | int bcmerror = 0; |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3085 | struct chip_info *ci = bus->ci; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3086 | |
| 3087 | /* To enter download state, disable ARM and reset SOCRAM. |
| 3088 | * To exit download state, simply reset ARM (default is RAM boot). |
| 3089 | */ |
| 3090 | if (enter) { |
| 3091 | bus->alp_only = true; |
| 3092 | |
Franky Lin | 086a2e0 | 2011-11-10 20:30:23 +0100 | [diff] [blame] | 3093 | ci->coredisable(bus->sdiodev, ci, BCMA_CORE_ARM_CM3); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3094 | |
Franky Lin | d77e70f | 2011-11-10 20:30:24 +0100 | [diff] [blame] | 3095 | ci->resetcore(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3096 | |
| 3097 | /* Clear the top bit of memory */ |
| 3098 | if (bus->ramsize) { |
| 3099 | u32 zeros = 0; |
| 3100 | brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4, |
| 3101 | (u8 *)&zeros, 4); |
| 3102 | } |
| 3103 | } else { |
Franky Lin | 6ca687d | 2011-11-10 20:30:21 +0100 | [diff] [blame] | 3104 | if (!ci->iscoreup(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3105 | brcmf_dbg(ERROR, "SOCRAM core is down after reset?\n"); |
| 3106 | bcmerror = -EBADE; |
| 3107 | goto fail; |
| 3108 | } |
| 3109 | |
| 3110 | bcmerror = brcmf_sdbrcm_write_vars(bus); |
| 3111 | if (bcmerror) { |
| 3112 | brcmf_dbg(ERROR, "no vars written to RAM\n"); |
| 3113 | bcmerror = 0; |
| 3114 | } |
| 3115 | |
| 3116 | w_sdreg32(bus, 0xFFFFFFFF, |
| 3117 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 3118 | |
Franky Lin | d77e70f | 2011-11-10 20:30:24 +0100 | [diff] [blame] | 3119 | ci->resetcore(bus->sdiodev, ci, BCMA_CORE_ARM_CM3); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3120 | |
| 3121 | /* Allow HT Clock now that the ARM is running. */ |
| 3122 | bus->alp_only = false; |
| 3123 | |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3124 | bus->drvr->bus_if->state = BRCMF_BUS_LOAD; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3125 | } |
| 3126 | fail: |
| 3127 | return bcmerror; |
| 3128 | } |
| 3129 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3130 | static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3131 | { |
| 3132 | if (bus->firmware->size < bus->fw_ptr + len) |
| 3133 | len = bus->firmware->size - bus->fw_ptr; |
| 3134 | |
| 3135 | memcpy(buf, &bus->firmware->data[bus->fw_ptr], len); |
| 3136 | bus->fw_ptr += len; |
| 3137 | return len; |
| 3138 | } |
| 3139 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3140 | static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3141 | { |
| 3142 | int offset = 0; |
| 3143 | uint len; |
| 3144 | u8 *memblock = NULL, *memptr; |
| 3145 | int ret; |
| 3146 | |
| 3147 | brcmf_dbg(INFO, "Enter\n"); |
| 3148 | |
Franky Lin | 8dd939c | 2011-11-22 17:21:41 -0800 | [diff] [blame] | 3149 | ret = request_firmware(&bus->firmware, BRCMFMAC_FW_NAME, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3150 | &bus->sdiodev->func[2]->dev); |
| 3151 | if (ret) { |
| 3152 | brcmf_dbg(ERROR, "Fail to request firmware %d\n", ret); |
| 3153 | return ret; |
| 3154 | } |
| 3155 | bus->fw_ptr = 0; |
| 3156 | |
| 3157 | memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC); |
| 3158 | if (memblock == NULL) { |
| 3159 | ret = -ENOMEM; |
| 3160 | goto err; |
| 3161 | } |
| 3162 | if ((u32)(unsigned long)memblock % BRCMF_SDALIGN) |
| 3163 | memptr += (BRCMF_SDALIGN - |
| 3164 | ((u32)(unsigned long)memblock % BRCMF_SDALIGN)); |
| 3165 | |
| 3166 | /* Download image */ |
| 3167 | while ((len = |
| 3168 | brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) { |
| 3169 | ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len); |
| 3170 | if (ret) { |
| 3171 | brcmf_dbg(ERROR, "error %d on writing %d membytes at 0x%08x\n", |
| 3172 | ret, MEMBLOCK, offset); |
| 3173 | goto err; |
| 3174 | } |
| 3175 | |
| 3176 | offset += MEMBLOCK; |
| 3177 | } |
| 3178 | |
| 3179 | err: |
| 3180 | kfree(memblock); |
| 3181 | |
| 3182 | release_firmware(bus->firmware); |
| 3183 | bus->fw_ptr = 0; |
| 3184 | |
| 3185 | return ret; |
| 3186 | } |
| 3187 | |
| 3188 | /* |
| 3189 | * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file |
| 3190 | * and ending in a NUL. |
| 3191 | * Removes carriage returns, empty lines, comment lines, and converts |
| 3192 | * newlines to NULs. |
| 3193 | * Shortens buffer as needed and pads with NULs. End of buffer is marked |
| 3194 | * by two NULs. |
| 3195 | */ |
| 3196 | |
| 3197 | static uint brcmf_process_nvram_vars(char *varbuf, uint len) |
| 3198 | { |
| 3199 | char *dp; |
| 3200 | bool findNewline; |
| 3201 | int column; |
| 3202 | uint buf_len, n; |
| 3203 | |
| 3204 | dp = varbuf; |
| 3205 | |
| 3206 | findNewline = false; |
| 3207 | column = 0; |
| 3208 | |
| 3209 | for (n = 0; n < len; n++) { |
| 3210 | if (varbuf[n] == 0) |
| 3211 | break; |
| 3212 | if (varbuf[n] == '\r') |
| 3213 | continue; |
| 3214 | if (findNewline && varbuf[n] != '\n') |
| 3215 | continue; |
| 3216 | findNewline = false; |
| 3217 | if (varbuf[n] == '#') { |
| 3218 | findNewline = true; |
| 3219 | continue; |
| 3220 | } |
| 3221 | if (varbuf[n] == '\n') { |
| 3222 | if (column == 0) |
| 3223 | continue; |
| 3224 | *dp++ = 0; |
| 3225 | column = 0; |
| 3226 | continue; |
| 3227 | } |
| 3228 | *dp++ = varbuf[n]; |
| 3229 | column++; |
| 3230 | } |
| 3231 | buf_len = dp - varbuf; |
| 3232 | |
| 3233 | while (dp < varbuf + n) |
| 3234 | *dp++ = 0; |
| 3235 | |
| 3236 | return buf_len; |
| 3237 | } |
| 3238 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3239 | static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3240 | { |
| 3241 | uint len; |
| 3242 | char *memblock = NULL; |
| 3243 | char *bufp; |
| 3244 | int ret; |
| 3245 | |
Franky Lin | 8dd939c | 2011-11-22 17:21:41 -0800 | [diff] [blame] | 3246 | ret = request_firmware(&bus->firmware, BRCMFMAC_NV_NAME, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3247 | &bus->sdiodev->func[2]->dev); |
| 3248 | if (ret) { |
| 3249 | brcmf_dbg(ERROR, "Fail to request nvram %d\n", ret); |
| 3250 | return ret; |
| 3251 | } |
| 3252 | bus->fw_ptr = 0; |
| 3253 | |
| 3254 | memblock = kmalloc(MEMBLOCK, GFP_ATOMIC); |
| 3255 | if (memblock == NULL) { |
| 3256 | ret = -ENOMEM; |
| 3257 | goto err; |
| 3258 | } |
| 3259 | |
| 3260 | len = brcmf_sdbrcm_get_image(memblock, MEMBLOCK, bus); |
| 3261 | |
| 3262 | if (len > 0 && len < MEMBLOCK) { |
| 3263 | bufp = (char *)memblock; |
| 3264 | bufp[len] = 0; |
| 3265 | len = brcmf_process_nvram_vars(bufp, len); |
| 3266 | bufp += len; |
| 3267 | *bufp++ = 0; |
| 3268 | if (len) |
| 3269 | ret = brcmf_sdbrcm_downloadvars(bus, memblock, len + 1); |
| 3270 | if (ret) |
| 3271 | brcmf_dbg(ERROR, "error downloading vars: %d\n", ret); |
| 3272 | } else { |
| 3273 | brcmf_dbg(ERROR, "error reading nvram file: %d\n", len); |
| 3274 | ret = -EIO; |
| 3275 | } |
| 3276 | |
| 3277 | err: |
| 3278 | kfree(memblock); |
| 3279 | |
| 3280 | release_firmware(bus->firmware); |
| 3281 | bus->fw_ptr = 0; |
| 3282 | |
| 3283 | return ret; |
| 3284 | } |
| 3285 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3286 | static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3287 | { |
| 3288 | int bcmerror = -1; |
| 3289 | |
| 3290 | /* Keep arm in reset */ |
| 3291 | if (brcmf_sdbrcm_download_state(bus, true)) { |
| 3292 | brcmf_dbg(ERROR, "error placing ARM core in reset\n"); |
| 3293 | goto err; |
| 3294 | } |
| 3295 | |
| 3296 | /* External image takes precedence if specified */ |
| 3297 | if (brcmf_sdbrcm_download_code_file(bus)) { |
| 3298 | brcmf_dbg(ERROR, "dongle image file download failed\n"); |
| 3299 | goto err; |
| 3300 | } |
| 3301 | |
| 3302 | /* External nvram takes precedence if specified */ |
| 3303 | if (brcmf_sdbrcm_download_nvram(bus)) |
| 3304 | brcmf_dbg(ERROR, "dongle nvram file download failed\n"); |
| 3305 | |
| 3306 | /* Take arm out of reset */ |
| 3307 | if (brcmf_sdbrcm_download_state(bus, false)) { |
| 3308 | brcmf_dbg(ERROR, "error getting out of ARM core reset\n"); |
| 3309 | goto err; |
| 3310 | } |
| 3311 | |
| 3312 | bcmerror = 0; |
| 3313 | |
| 3314 | err: |
| 3315 | return bcmerror; |
| 3316 | } |
| 3317 | |
| 3318 | static bool |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3319 | brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3320 | { |
| 3321 | bool ret; |
| 3322 | |
| 3323 | /* Download the firmware */ |
| 3324 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3325 | |
| 3326 | ret = _brcmf_sdbrcm_download_firmware(bus) == 0; |
| 3327 | |
| 3328 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 3329 | |
| 3330 | return ret; |
| 3331 | } |
| 3332 | |
Franky Lin | 94c2fb8 | 2011-11-22 17:21:59 -0800 | [diff] [blame^] | 3333 | void brcmf_sdbrcm_bus_stop(struct device *dev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3334 | { |
| 3335 | u32 local_hostintmask; |
| 3336 | u8 saveclk; |
| 3337 | uint retries; |
| 3338 | int err; |
Franky Lin | 94c2fb8 | 2011-11-22 17:21:59 -0800 | [diff] [blame^] | 3339 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 3340 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv; |
| 3341 | struct brcmf_sdio *bus = sdiodev->bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3342 | |
| 3343 | brcmf_dbg(TRACE, "Enter\n"); |
| 3344 | |
| 3345 | if (bus->watchdog_tsk) { |
| 3346 | send_sig(SIGTERM, bus->watchdog_tsk, 1); |
| 3347 | kthread_stop(bus->watchdog_tsk); |
| 3348 | bus->watchdog_tsk = NULL; |
| 3349 | } |
| 3350 | |
| 3351 | if (bus->dpc_tsk && bus->dpc_tsk != current) { |
| 3352 | send_sig(SIGTERM, bus->dpc_tsk, 1); |
| 3353 | kthread_stop(bus->dpc_tsk); |
| 3354 | bus->dpc_tsk = NULL; |
| 3355 | } |
| 3356 | |
| 3357 | down(&bus->sdsem); |
| 3358 | |
| 3359 | bus_wake(bus); |
| 3360 | |
| 3361 | /* Enable clock for device interrupts */ |
| 3362 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3363 | |
| 3364 | /* Disable and clear interrupts at the chip level also */ |
| 3365 | w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask), &retries); |
| 3366 | local_hostintmask = bus->hostintmask; |
| 3367 | bus->hostintmask = 0; |
| 3368 | |
| 3369 | /* Change our idea of bus state */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3370 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3371 | |
| 3372 | /* Force clocks on backplane to be sure F2 interrupt propagates */ |
| 3373 | saveclk = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3374 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3375 | if (!err) { |
| 3376 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3377 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3378 | (saveclk | SBSDIO_FORCE_HT), &err); |
| 3379 | } |
| 3380 | if (err) |
| 3381 | brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err); |
| 3382 | |
| 3383 | /* Turn off the bus (F2), free any pending packets */ |
| 3384 | brcmf_dbg(INTR, "disable SDIO interrupts\n"); |
| 3385 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3386 | SDIO_FUNC_ENABLE_1, NULL); |
| 3387 | |
| 3388 | /* Clear any pending interrupts now that F2 is disabled */ |
| 3389 | w_sdreg32(bus, local_hostintmask, |
| 3390 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 3391 | |
| 3392 | /* Turn off the backplane clock (only) */ |
| 3393 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 3394 | |
| 3395 | /* Clear the data packet queues */ |
| 3396 | brcmu_pktq_flush(&bus->txq, true, NULL, NULL); |
| 3397 | |
| 3398 | /* Clear any held glomming stuff */ |
| 3399 | if (bus->glomd) |
| 3400 | brcmu_pkt_buf_free_skb(bus->glomd); |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame] | 3401 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3402 | |
| 3403 | /* Clear rx control and wake any waiters */ |
| 3404 | bus->rxlen = 0; |
| 3405 | brcmf_sdbrcm_dcmd_resp_wake(bus); |
| 3406 | |
| 3407 | /* Reset some F2 state stuff */ |
| 3408 | bus->rxskip = false; |
| 3409 | bus->tx_seq = bus->rx_seq = 0; |
| 3410 | |
| 3411 | up(&bus->sdsem); |
| 3412 | } |
| 3413 | |
Franky Lin | fa20b91 | 2011-11-22 17:21:57 -0800 | [diff] [blame] | 3414 | int brcmf_sdbrcm_bus_init(struct device *dev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3415 | { |
Franky Lin | fa20b91 | 2011-11-22 17:21:57 -0800 | [diff] [blame] | 3416 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 3417 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv; |
| 3418 | struct brcmf_sdio *bus = sdiodev->bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3419 | unsigned long timeout; |
| 3420 | uint retries = 0; |
| 3421 | u8 ready, enable; |
| 3422 | int err, ret = 0; |
| 3423 | u8 saveclk; |
| 3424 | |
| 3425 | brcmf_dbg(TRACE, "Enter\n"); |
| 3426 | |
| 3427 | /* try to download image and nvram to the dongle */ |
Franky Lin | fa20b91 | 2011-11-22 17:21:57 -0800 | [diff] [blame] | 3428 | if (bus_if->state == BRCMF_BUS_DOWN) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3429 | if (!(brcmf_sdbrcm_download_firmware(bus))) |
| 3430 | return -1; |
| 3431 | } |
| 3432 | |
| 3433 | if (!bus->drvr) |
| 3434 | return 0; |
| 3435 | |
| 3436 | /* Start the watchdog timer */ |
| 3437 | bus->drvr->tickcnt = 0; |
| 3438 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 3439 | |
| 3440 | down(&bus->sdsem); |
| 3441 | |
| 3442 | /* Make sure backplane clock is on, needed to generate F2 interrupt */ |
| 3443 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3444 | if (bus->clkstate != CLK_AVAIL) |
| 3445 | goto exit; |
| 3446 | |
| 3447 | /* Force clocks on backplane to be sure F2 interrupt propagates */ |
| 3448 | saveclk = |
| 3449 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3450 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3451 | if (!err) { |
| 3452 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3453 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3454 | (saveclk | SBSDIO_FORCE_HT), &err); |
| 3455 | } |
| 3456 | if (err) { |
| 3457 | brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err); |
| 3458 | goto exit; |
| 3459 | } |
| 3460 | |
| 3461 | /* Enable function 2 (frame transfers) */ |
| 3462 | w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, |
| 3463 | offsetof(struct sdpcmd_regs, tosbmailboxdata), &retries); |
| 3464 | enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2); |
| 3465 | |
| 3466 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3467 | enable, NULL); |
| 3468 | |
| 3469 | timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY); |
| 3470 | ready = 0; |
| 3471 | while (enable != ready) { |
| 3472 | ready = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_0, |
| 3473 | SDIO_CCCR_IORx, NULL); |
| 3474 | if (time_after(jiffies, timeout)) |
| 3475 | break; |
| 3476 | else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50)) |
| 3477 | /* prevent busy waiting if it takes too long */ |
| 3478 | msleep_interruptible(20); |
| 3479 | } |
| 3480 | |
| 3481 | brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready); |
| 3482 | |
| 3483 | /* If F2 successfully enabled, set core and enable interrupts */ |
| 3484 | if (ready == enable) { |
| 3485 | /* Set up the interrupt mask and enable interrupts */ |
| 3486 | bus->hostintmask = HOSTINTMASK; |
| 3487 | w_sdreg32(bus, bus->hostintmask, |
| 3488 | offsetof(struct sdpcmd_regs, hostintmask), &retries); |
| 3489 | |
| 3490 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3491 | SBSDIO_WATERMARK, 8, &err); |
| 3492 | |
| 3493 | /* Set bus state according to enable result */ |
Franky Lin | fa20b91 | 2011-11-22 17:21:57 -0800 | [diff] [blame] | 3494 | bus_if->state = BRCMF_BUS_DATA; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | else { |
| 3498 | /* Disable F2 again */ |
| 3499 | enable = SDIO_FUNC_ENABLE_1; |
| 3500 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, |
| 3501 | SDIO_CCCR_IOEx, enable, NULL); |
| 3502 | } |
| 3503 | |
| 3504 | /* Restore previous clock setting */ |
| 3505 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3506 | SBSDIO_FUNC1_CHIPCLKCSR, saveclk, &err); |
| 3507 | |
| 3508 | /* If we didn't come up, turn off backplane clock */ |
Franky Lin | fa20b91 | 2011-11-22 17:21:57 -0800 | [diff] [blame] | 3509 | if (bus_if->state != BRCMF_BUS_DATA) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3510 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 3511 | |
| 3512 | exit: |
| 3513 | up(&bus->sdsem); |
| 3514 | |
| 3515 | return ret; |
| 3516 | } |
| 3517 | |
| 3518 | void brcmf_sdbrcm_isr(void *arg) |
| 3519 | { |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3520 | struct brcmf_sdio *bus = (struct brcmf_sdio *) arg; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3521 | |
| 3522 | brcmf_dbg(TRACE, "Enter\n"); |
| 3523 | |
| 3524 | if (!bus) { |
| 3525 | brcmf_dbg(ERROR, "bus is null pointer, exiting\n"); |
| 3526 | return; |
| 3527 | } |
| 3528 | |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3529 | if (bus->drvr->bus_if->state == BRCMF_BUS_DOWN) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3530 | brcmf_dbg(ERROR, "bus is down. we have nothing to do\n"); |
| 3531 | return; |
| 3532 | } |
| 3533 | /* Count the interrupt call */ |
| 3534 | bus->intrcount++; |
| 3535 | bus->ipend = true; |
| 3536 | |
| 3537 | /* Shouldn't get this interrupt if we're sleeping? */ |
| 3538 | if (bus->sleeping) { |
| 3539 | brcmf_dbg(ERROR, "INTERRUPT WHILE SLEEPING??\n"); |
| 3540 | return; |
| 3541 | } |
| 3542 | |
| 3543 | /* Disable additional interrupts (is this needed now)? */ |
| 3544 | if (!bus->intr) |
| 3545 | brcmf_dbg(ERROR, "isr w/o interrupt configured!\n"); |
| 3546 | |
| 3547 | bus->dpc_sched = true; |
| 3548 | if (bus->dpc_tsk) |
| 3549 | complete(&bus->dpc_wait); |
| 3550 | } |
| 3551 | |
Franky Lin | cad2b26 | 2011-11-22 17:21:53 -0800 | [diff] [blame] | 3552 | static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3553 | { |
Franky Lin | cad2b26 | 2011-11-22 17:21:53 -0800 | [diff] [blame] | 3554 | #ifdef BCMDBG |
| 3555 | struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev); |
| 3556 | #endif /* BCMDBG */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3557 | |
| 3558 | brcmf_dbg(TIMER, "Enter\n"); |
| 3559 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3560 | /* Ignore the timer if simulating bus down */ |
| 3561 | if (bus->sleeping) |
| 3562 | return false; |
| 3563 | |
| 3564 | down(&bus->sdsem); |
| 3565 | |
| 3566 | /* Poll period: check device if appropriate. */ |
| 3567 | if (bus->poll && (++bus->polltick >= bus->pollrate)) { |
| 3568 | u32 intstatus = 0; |
| 3569 | |
| 3570 | /* Reset poll tick */ |
| 3571 | bus->polltick = 0; |
| 3572 | |
| 3573 | /* Check device if no interrupts */ |
| 3574 | if (!bus->intr || (bus->intrcount == bus->lastintrs)) { |
| 3575 | |
| 3576 | if (!bus->dpc_sched) { |
| 3577 | u8 devpend; |
| 3578 | devpend = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 3579 | SDIO_FUNC_0, SDIO_CCCR_INTx, |
| 3580 | NULL); |
| 3581 | intstatus = |
| 3582 | devpend & (INTR_STATUS_FUNC1 | |
| 3583 | INTR_STATUS_FUNC2); |
| 3584 | } |
| 3585 | |
| 3586 | /* If there is something, make like the ISR and |
| 3587 | schedule the DPC */ |
| 3588 | if (intstatus) { |
| 3589 | bus->pollcnt++; |
| 3590 | bus->ipend = true; |
| 3591 | |
| 3592 | bus->dpc_sched = true; |
| 3593 | if (bus->dpc_tsk) |
| 3594 | complete(&bus->dpc_wait); |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | /* Update interrupt tracking */ |
| 3599 | bus->lastintrs = bus->intrcount; |
| 3600 | } |
| 3601 | #ifdef BCMDBG |
| 3602 | /* Poll for console output periodically */ |
Franky Lin | cad2b26 | 2011-11-22 17:21:53 -0800 | [diff] [blame] | 3603 | if (bus_if->state == BRCMF_BUS_DATA && |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3604 | bus->console_interval != 0) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3605 | bus->console.count += BRCMF_WD_POLL_MS; |
| 3606 | if (bus->console.count >= bus->console_interval) { |
| 3607 | bus->console.count -= bus->console_interval; |
| 3608 | /* Make sure backplane clock is on */ |
| 3609 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3610 | if (brcmf_sdbrcm_readconsole(bus) < 0) |
| 3611 | /* stop on error */ |
| 3612 | bus->console_interval = 0; |
| 3613 | } |
| 3614 | } |
| 3615 | #endif /* BCMDBG */ |
| 3616 | |
| 3617 | /* On idle timeout clear activity flag and/or turn off clock */ |
| 3618 | if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) { |
| 3619 | if (++bus->idlecount >= bus->idletime) { |
| 3620 | bus->idlecount = 0; |
| 3621 | if (bus->activity) { |
| 3622 | bus->activity = false; |
| 3623 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 3624 | } else { |
| 3625 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 3626 | } |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | up(&bus->sdsem); |
| 3631 | |
| 3632 | return bus->ipend; |
| 3633 | } |
| 3634 | |
| 3635 | static bool brcmf_sdbrcm_chipmatch(u16 chipid) |
| 3636 | { |
| 3637 | if (chipid == BCM4329_CHIP_ID) |
| 3638 | return true; |
| 3639 | return false; |
| 3640 | } |
| 3641 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3642 | static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3643 | { |
| 3644 | brcmf_dbg(TRACE, "Enter\n"); |
| 3645 | |
| 3646 | kfree(bus->rxbuf); |
| 3647 | bus->rxctl = bus->rxbuf = NULL; |
| 3648 | bus->rxlen = 0; |
| 3649 | |
| 3650 | kfree(bus->databuf); |
| 3651 | bus->databuf = NULL; |
| 3652 | } |
| 3653 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3654 | static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3655 | { |
| 3656 | brcmf_dbg(TRACE, "Enter\n"); |
| 3657 | |
| 3658 | if (bus->drvr->maxctl) { |
| 3659 | bus->rxblen = |
| 3660 | roundup((bus->drvr->maxctl + SDPCM_HDRLEN), |
| 3661 | ALIGNMENT) + BRCMF_SDALIGN; |
| 3662 | bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC); |
| 3663 | if (!(bus->rxbuf)) |
| 3664 | goto fail; |
| 3665 | } |
| 3666 | |
| 3667 | /* Allocate buffer to receive glomed packet */ |
| 3668 | bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC); |
| 3669 | if (!(bus->databuf)) { |
| 3670 | /* release rxbuf which was already located as above */ |
| 3671 | if (!bus->rxblen) |
| 3672 | kfree(bus->rxbuf); |
| 3673 | goto fail; |
| 3674 | } |
| 3675 | |
| 3676 | /* Align the buffer */ |
| 3677 | if ((unsigned long)bus->databuf % BRCMF_SDALIGN) |
| 3678 | bus->dataptr = bus->databuf + (BRCMF_SDALIGN - |
| 3679 | ((unsigned long)bus->databuf % BRCMF_SDALIGN)); |
| 3680 | else |
| 3681 | bus->dataptr = bus->databuf; |
| 3682 | |
| 3683 | return true; |
| 3684 | |
| 3685 | fail: |
| 3686 | return false; |
| 3687 | } |
| 3688 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3689 | static bool |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3690 | brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3691 | { |
| 3692 | u8 clkctl = 0; |
| 3693 | int err = 0; |
| 3694 | int reg_addr; |
| 3695 | u32 reg_val; |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3696 | u8 idx; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3697 | |
| 3698 | bus->alp_only = true; |
| 3699 | |
| 3700 | /* Return the window to backplane enumeration space for core access */ |
| 3701 | if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, SI_ENUM_BASE)) |
| 3702 | brcmf_dbg(ERROR, "FAILED to return to SI_ENUM_BASE\n"); |
| 3703 | |
| 3704 | #ifdef BCMDBG |
| 3705 | printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n", |
| 3706 | brcmf_sdcard_reg_read(bus->sdiodev, SI_ENUM_BASE, 4)); |
| 3707 | |
| 3708 | #endif /* BCMDBG */ |
| 3709 | |
| 3710 | /* |
Franky Lin | a97e4fc5 | 2011-11-04 22:23:35 +0100 | [diff] [blame] | 3711 | * Force PLL off until brcmf_sdio_chip_attach() |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3712 | * programs PLL control regs |
| 3713 | */ |
| 3714 | |
| 3715 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3716 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3717 | BRCMF_INIT_CLKCTL1, &err); |
| 3718 | if (!err) |
| 3719 | clkctl = |
| 3720 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3721 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3722 | |
| 3723 | if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) { |
| 3724 | brcmf_dbg(ERROR, "ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n", |
| 3725 | err, BRCMF_INIT_CLKCTL1, clkctl); |
| 3726 | goto fail; |
| 3727 | } |
| 3728 | |
Franky Lin | a97e4fc5 | 2011-11-04 22:23:35 +0100 | [diff] [blame] | 3729 | if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) { |
| 3730 | brcmf_dbg(ERROR, "brcmf_sdio_chip_attach failed!\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3731 | goto fail; |
| 3732 | } |
| 3733 | |
| 3734 | if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) { |
| 3735 | brcmf_dbg(ERROR, "unsupported chip: 0x%04x\n", bus->ci->chip); |
| 3736 | goto fail; |
| 3737 | } |
| 3738 | |
Franky Lin | e12afb6 | 2011-11-04 22:23:40 +0100 | [diff] [blame] | 3739 | brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, |
| 3740 | SDIO_DRIVE_STRENGTH); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3741 | |
Franky Lin | 454d2a8 | 2011-11-04 22:23:37 +0100 | [diff] [blame] | 3742 | /* Get info on the SOCRAM cores... */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3743 | bus->ramsize = bus->ci->ramsize; |
| 3744 | if (!(bus->ramsize)) { |
| 3745 | brcmf_dbg(ERROR, "failed to find SOCRAM memory!\n"); |
| 3746 | goto fail; |
| 3747 | } |
| 3748 | |
| 3749 | /* Set core control so an SDIO reset does a backplane reset */ |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3750 | idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
| 3751 | reg_addr = bus->ci->c_inf[idx].base + |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3752 | offsetof(struct sdpcmd_regs, corecontrol); |
| 3753 | reg_val = brcmf_sdcard_reg_read(bus->sdiodev, reg_addr, sizeof(u32)); |
| 3754 | brcmf_sdcard_reg_write(bus->sdiodev, reg_addr, sizeof(u32), |
| 3755 | reg_val | CC_BPRESEN); |
| 3756 | |
| 3757 | brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN); |
| 3758 | |
| 3759 | /* Locate an appropriately-aligned portion of hdrbuf */ |
| 3760 | bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], |
| 3761 | BRCMF_SDALIGN); |
| 3762 | |
| 3763 | /* Set the poll and/or interrupt flags */ |
| 3764 | bus->intr = true; |
| 3765 | bus->poll = false; |
| 3766 | if (bus->poll) |
| 3767 | bus->pollrate = 1; |
| 3768 | |
| 3769 | return true; |
| 3770 | |
| 3771 | fail: |
| 3772 | return false; |
| 3773 | } |
| 3774 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3775 | static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3776 | { |
| 3777 | brcmf_dbg(TRACE, "Enter\n"); |
| 3778 | |
| 3779 | /* Disable F2 to clear any intermediate frame state on the dongle */ |
| 3780 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3781 | SDIO_FUNC_ENABLE_1, NULL); |
| 3782 | |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3783 | bus->drvr->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3784 | bus->sleeping = false; |
| 3785 | bus->rxflow = false; |
| 3786 | |
| 3787 | /* Done with backplane-dependent accesses, can drop clock... */ |
| 3788 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3789 | SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL); |
| 3790 | |
| 3791 | /* ...and initialize clock/power states */ |
| 3792 | bus->clkstate = CLK_SDONLY; |
| 3793 | bus->idletime = BRCMF_IDLE_INTERVAL; |
| 3794 | bus->idleclock = BRCMF_IDLE_ACTIVE; |
| 3795 | |
| 3796 | /* Query the F2 block size, set roundup accordingly */ |
| 3797 | bus->blocksize = bus->sdiodev->func[2]->cur_blksize; |
| 3798 | bus->roundup = min(max_roundup, bus->blocksize); |
| 3799 | |
| 3800 | /* bus module does not support packet chaining */ |
| 3801 | bus->use_rxchain = false; |
| 3802 | bus->sd_rxchain = false; |
| 3803 | |
| 3804 | return true; |
| 3805 | } |
| 3806 | |
| 3807 | static int |
| 3808 | brcmf_sdbrcm_watchdog_thread(void *data) |
| 3809 | { |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3810 | struct brcmf_sdio *bus = (struct brcmf_sdio *)data; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3811 | |
| 3812 | allow_signal(SIGTERM); |
| 3813 | /* Run until signal received */ |
| 3814 | while (1) { |
| 3815 | if (kthread_should_stop()) |
| 3816 | break; |
| 3817 | if (!wait_for_completion_interruptible(&bus->watchdog_wait)) { |
Franky Lin | cad2b26 | 2011-11-22 17:21:53 -0800 | [diff] [blame] | 3818 | brcmf_sdbrcm_bus_watchdog(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3819 | /* Count the tick for reference */ |
| 3820 | bus->drvr->tickcnt++; |
| 3821 | } else |
| 3822 | break; |
| 3823 | } |
| 3824 | return 0; |
| 3825 | } |
| 3826 | |
| 3827 | static void |
| 3828 | brcmf_sdbrcm_watchdog(unsigned long data) |
| 3829 | { |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3830 | struct brcmf_sdio *bus = (struct brcmf_sdio *)data; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3831 | |
| 3832 | if (bus->watchdog_tsk) { |
| 3833 | complete(&bus->watchdog_wait); |
| 3834 | /* Reschedule the watchdog */ |
| 3835 | if (bus->wd_timer_valid) |
| 3836 | mod_timer(&bus->timer, |
| 3837 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000); |
| 3838 | } |
| 3839 | } |
| 3840 | |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3841 | static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3842 | { |
| 3843 | brcmf_dbg(TRACE, "Enter\n"); |
| 3844 | |
| 3845 | if (bus->ci) { |
| 3846 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3847 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
Franky Lin | a8a6c04 | 2011-11-04 22:23:39 +0100 | [diff] [blame] | 3848 | brcmf_sdio_chip_detach(&bus->ci); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3849 | if (bus->vars && bus->varsz) |
| 3850 | kfree(bus->vars); |
| 3851 | bus->vars = NULL; |
| 3852 | } |
| 3853 | |
| 3854 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 3855 | } |
| 3856 | |
| 3857 | /* Detach and free everything */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3858 | static void brcmf_sdbrcm_release(struct brcmf_sdio *bus) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3859 | { |
| 3860 | brcmf_dbg(TRACE, "Enter\n"); |
| 3861 | |
| 3862 | if (bus) { |
| 3863 | /* De-register interrupt handler */ |
| 3864 | brcmf_sdcard_intr_dereg(bus->sdiodev); |
| 3865 | |
| 3866 | if (bus->drvr) { |
| 3867 | brcmf_detach(bus->drvr); |
| 3868 | brcmf_sdbrcm_release_dongle(bus); |
| 3869 | bus->drvr = NULL; |
| 3870 | } |
| 3871 | |
| 3872 | brcmf_sdbrcm_release_malloc(bus); |
| 3873 | |
| 3874 | kfree(bus); |
| 3875 | } |
| 3876 | |
| 3877 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 3878 | } |
| 3879 | |
Franky Lin | 4175b88 | 2011-11-22 17:21:49 -0800 | [diff] [blame] | 3880 | void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3881 | { |
| 3882 | int ret; |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3883 | struct brcmf_sdio *bus; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3884 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3885 | brcmf_dbg(TRACE, "Enter\n"); |
| 3886 | |
| 3887 | /* We make an assumption about address window mappings: |
| 3888 | * regsva == SI_ENUM_BASE*/ |
| 3889 | |
| 3890 | /* Allocate private bus interface state */ |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3891 | bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3892 | if (!bus) |
| 3893 | goto fail; |
| 3894 | |
| 3895 | bus->sdiodev = sdiodev; |
| 3896 | sdiodev->bus = bus; |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 3897 | skb_queue_head_init(&bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3898 | bus->txbound = BRCMF_TXBOUND; |
| 3899 | bus->rxbound = BRCMF_RXBOUND; |
| 3900 | bus->txminmax = BRCMF_TXMINMAX; |
| 3901 | bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1; |
| 3902 | bus->usebufpool = false; /* Use bufpool if allocated, |
| 3903 | else use locally malloced rxbuf */ |
| 3904 | |
| 3905 | /* attempt to attach to the dongle */ |
| 3906 | if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) { |
| 3907 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_attach failed\n"); |
| 3908 | goto fail; |
| 3909 | } |
| 3910 | |
| 3911 | spin_lock_init(&bus->txqlock); |
| 3912 | init_waitqueue_head(&bus->ctrl_wait); |
| 3913 | init_waitqueue_head(&bus->dcmd_resp_wait); |
| 3914 | |
| 3915 | /* Set up the watchdog timer */ |
| 3916 | init_timer(&bus->timer); |
| 3917 | bus->timer.data = (unsigned long)bus; |
| 3918 | bus->timer.function = brcmf_sdbrcm_watchdog; |
| 3919 | |
| 3920 | /* Initialize thread based operation and lock */ |
| 3921 | sema_init(&bus->sdsem, 1); |
| 3922 | |
| 3923 | /* Initialize watchdog thread */ |
| 3924 | init_completion(&bus->watchdog_wait); |
| 3925 | bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread, |
| 3926 | bus, "brcmf_watchdog"); |
| 3927 | if (IS_ERR(bus->watchdog_tsk)) { |
| 3928 | printk(KERN_WARNING |
| 3929 | "brcmf_watchdog thread failed to start\n"); |
| 3930 | bus->watchdog_tsk = NULL; |
| 3931 | } |
| 3932 | /* Initialize DPC thread */ |
| 3933 | init_completion(&bus->dpc_wait); |
| 3934 | bus->dpc_tsk = kthread_run(brcmf_sdbrcm_dpc_thread, |
| 3935 | bus, "brcmf_dpc"); |
| 3936 | if (IS_ERR(bus->dpc_tsk)) { |
| 3937 | printk(KERN_WARNING |
| 3938 | "brcmf_dpc thread failed to start\n"); |
| 3939 | bus->dpc_tsk = NULL; |
| 3940 | } |
| 3941 | |
| 3942 | /* Attach to the brcmf/OS/network interface */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 3943 | bus->drvr = brcmf_attach(bus, SDPCM_RESERVE, bus->sdiodev->dev); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3944 | if (!bus->drvr) { |
| 3945 | brcmf_dbg(ERROR, "brcmf_attach failed\n"); |
| 3946 | goto fail; |
| 3947 | } |
| 3948 | |
| 3949 | /* Allocate buffers */ |
| 3950 | if (!(brcmf_sdbrcm_probe_malloc(bus))) { |
| 3951 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_malloc failed\n"); |
| 3952 | goto fail; |
| 3953 | } |
| 3954 | |
| 3955 | if (!(brcmf_sdbrcm_probe_init(bus))) { |
| 3956 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_init failed\n"); |
| 3957 | goto fail; |
| 3958 | } |
| 3959 | |
| 3960 | /* Register interrupt callback, but mask it (not operational yet). */ |
| 3961 | brcmf_dbg(INTR, "disable SDIO interrupts (not interested yet)\n"); |
| 3962 | ret = brcmf_sdcard_intr_reg(bus->sdiodev); |
| 3963 | if (ret != 0) { |
| 3964 | brcmf_dbg(ERROR, "FAILED: sdcard_intr_reg returned %d\n", ret); |
| 3965 | goto fail; |
| 3966 | } |
| 3967 | brcmf_dbg(INTR, "registered SDIO interrupt function ok\n"); |
| 3968 | |
| 3969 | brcmf_dbg(INFO, "completed!!\n"); |
| 3970 | |
| 3971 | /* if firmware path present try to download and bring up bus */ |
| 3972 | ret = brcmf_bus_start(bus->drvr); |
| 3973 | if (ret != 0) { |
| 3974 | if (ret == -ENOLINK) { |
| 3975 | brcmf_dbg(ERROR, "dongle is not responding\n"); |
| 3976 | goto fail; |
| 3977 | } |
| 3978 | } |
Franky Lin | 15d45b6 | 2011-10-21 16:16:32 +0200 | [diff] [blame] | 3979 | |
| 3980 | /* add interface and open for business */ |
| 3981 | if (brcmf_add_if((struct brcmf_info *)bus->drvr, 0, "wlan%d", NULL)) { |
| 3982 | brcmf_dbg(ERROR, "Add primary net device interface failed!!\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3983 | goto fail; |
| 3984 | } |
| 3985 | |
| 3986 | return bus; |
| 3987 | |
| 3988 | fail: |
| 3989 | brcmf_sdbrcm_release(bus); |
| 3990 | return NULL; |
| 3991 | } |
| 3992 | |
| 3993 | void brcmf_sdbrcm_disconnect(void *ptr) |
| 3994 | { |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 3995 | struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3996 | |
| 3997 | brcmf_dbg(TRACE, "Enter\n"); |
| 3998 | |
| 3999 | if (bus) |
| 4000 | brcmf_sdbrcm_release(bus); |
| 4001 | |
| 4002 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 4003 | } |
| 4004 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4005 | void |
Franky Lin | e92eedf | 2011-11-22 17:21:50 -0800 | [diff] [blame] | 4006 | brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4007 | { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4008 | /* Totally stop the timer */ |
| 4009 | if (!wdtick && bus->wd_timer_valid == true) { |
| 4010 | del_timer_sync(&bus->timer); |
| 4011 | bus->wd_timer_valid = false; |
| 4012 | bus->save_ms = wdtick; |
| 4013 | return; |
| 4014 | } |
| 4015 | |
Franky Lin | ece960e | 2011-10-21 16:16:19 +0200 | [diff] [blame] | 4016 | /* don't start the wd until fw is loaded */ |
Franky Lin | 8d169aa | 2011-11-22 17:21:52 -0800 | [diff] [blame] | 4017 | if (bus->drvr->bus_if->state == BRCMF_BUS_DOWN) |
Franky Lin | ece960e | 2011-10-21 16:16:19 +0200 | [diff] [blame] | 4018 | return; |
| 4019 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4020 | if (wdtick) { |
| 4021 | if (bus->save_ms != BRCMF_WD_POLL_MS) { |
| 4022 | if (bus->wd_timer_valid == true) |
| 4023 | /* Stop timer and restart at new value */ |
| 4024 | del_timer_sync(&bus->timer); |
| 4025 | |
| 4026 | /* Create timer again when watchdog period is |
| 4027 | dynamically changed or in the first instance |
| 4028 | */ |
| 4029 | bus->timer.expires = |
| 4030 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000; |
| 4031 | add_timer(&bus->timer); |
| 4032 | |
| 4033 | } else { |
| 4034 | /* Re arm the timer, at last watchdog period */ |
| 4035 | mod_timer(&bus->timer, |
| 4036 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000); |
| 4037 | } |
| 4038 | |
| 4039 | bus->wd_timer_valid = true; |
| 4040 | bus->save_ms = wdtick; |
| 4041 | } |
| 4042 | } |