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