1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (c) 2014 Broadcom Corporation 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/firmware.h> 9 #include <linux/pci.h> 10 #include <linux/vmalloc.h> 11 #include <linux/delay.h> 12 #include <linux/interrupt.h> 13 #include <linux/bcma/bcma.h> 14 #include <linux/sched.h> 15 #include <asm/unaligned.h> 16 17 #include <soc.h> 18 #include <chipcommon.h> 19 #include <brcmu_utils.h> 20 #include <brcmu_wifi.h> 21 #include <brcm_hw_ids.h> 22 23 /* Custom brcmf_err() that takes bus arg and passes it further */ 24 #define brcmf_err(bus, fmt, ...) \ 25 do { \ 26 if (IS_ENABLED(CONFIG_BRCMDBG) || \ 27 IS_ENABLED(CONFIG_BRCM_TRACING) || \ 28 net_ratelimit()) \ 29 __brcmf_err(bus, __func__, fmt, ##__VA_ARGS__); \ 30 } while (0) 31 32 #include "debug.h" 33 #include "bus.h" 34 #include "commonring.h" 35 #include "msgbuf.h" 36 #include "pcie.h" 37 #include "firmware.h" 38 #include "chip.h" 39 #include "core.h" 40 #include "common.h" 41 42 43 enum brcmf_pcie_state { 44 BRCMFMAC_PCIE_STATE_DOWN, 45 BRCMFMAC_PCIE_STATE_UP 46 }; 47 48 BRCMF_FW_DEF(43602, "brcmfmac43602-pcie"); 49 BRCMF_FW_DEF(4350, "brcmfmac4350-pcie"); 50 BRCMF_FW_DEF(4350C, "brcmfmac4350c2-pcie"); 51 BRCMF_FW_DEF(4356, "brcmfmac4356-pcie"); 52 BRCMF_FW_DEF(43570, "brcmfmac43570-pcie"); 53 BRCMF_FW_DEF(4358, "brcmfmac4358-pcie"); 54 BRCMF_FW_DEF(4359, "brcmfmac4359-pcie"); 55 BRCMF_FW_DEF(4364, "brcmfmac4364-pcie"); 56 BRCMF_FW_DEF(4365B, "brcmfmac4365b-pcie"); 57 BRCMF_FW_DEF(4365C, "brcmfmac4365c-pcie"); 58 BRCMF_FW_DEF(4366B, "brcmfmac4366b-pcie"); 59 BRCMF_FW_DEF(4366C, "brcmfmac4366c-pcie"); 60 BRCMF_FW_DEF(4371, "brcmfmac4371-pcie"); 61 62 static const struct brcmf_firmware_mapping brcmf_pcie_fwnames[] = { 63 BRCMF_FW_ENTRY(BRCM_CC_43602_CHIP_ID, 0xFFFFFFFF, 43602), 64 BRCMF_FW_ENTRY(BRCM_CC_43465_CHIP_ID, 0xFFFFFFF0, 4366C), 65 BRCMF_FW_ENTRY(BRCM_CC_4350_CHIP_ID, 0x000000FF, 4350C), 66 BRCMF_FW_ENTRY(BRCM_CC_4350_CHIP_ID, 0xFFFFFF00, 4350), 67 BRCMF_FW_ENTRY(BRCM_CC_43525_CHIP_ID, 0xFFFFFFF0, 4365C), 68 BRCMF_FW_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356), 69 BRCMF_FW_ENTRY(BRCM_CC_43567_CHIP_ID, 0xFFFFFFFF, 43570), 70 BRCMF_FW_ENTRY(BRCM_CC_43569_CHIP_ID, 0xFFFFFFFF, 43570), 71 BRCMF_FW_ENTRY(BRCM_CC_43570_CHIP_ID, 0xFFFFFFFF, 43570), 72 BRCMF_FW_ENTRY(BRCM_CC_4358_CHIP_ID, 0xFFFFFFFF, 4358), 73 BRCMF_FW_ENTRY(BRCM_CC_4359_CHIP_ID, 0xFFFFFFFF, 4359), 74 BRCMF_FW_ENTRY(BRCM_CC_4364_CHIP_ID, 0xFFFFFFFF, 4364), 75 BRCMF_FW_ENTRY(BRCM_CC_4365_CHIP_ID, 0x0000000F, 4365B), 76 BRCMF_FW_ENTRY(BRCM_CC_4365_CHIP_ID, 0xFFFFFFF0, 4365C), 77 BRCMF_FW_ENTRY(BRCM_CC_4366_CHIP_ID, 0x0000000F, 4366B), 78 BRCMF_FW_ENTRY(BRCM_CC_4366_CHIP_ID, 0xFFFFFFF0, 4366C), 79 BRCMF_FW_ENTRY(BRCM_CC_43664_CHIP_ID, 0xFFFFFFF0, 4366C), 80 BRCMF_FW_ENTRY(BRCM_CC_4371_CHIP_ID, 0xFFFFFFFF, 4371), 81 }; 82 83 #define BRCMF_PCIE_FW_UP_TIMEOUT 5000 /* msec */ 84 85 #define BRCMF_PCIE_REG_MAP_SIZE (32 * 1024) 86 87 /* backplane addres space accessed by BAR0 */ 88 #define BRCMF_PCIE_BAR0_WINDOW 0x80 89 #define BRCMF_PCIE_BAR0_REG_SIZE 0x1000 90 #define BRCMF_PCIE_BAR0_WRAPPERBASE 0x70 91 92 #define BRCMF_PCIE_BAR0_WRAPBASE_DMP_OFFSET 0x1000 93 #define BRCMF_PCIE_BARO_PCIE_ENUM_OFFSET 0x2000 94 95 #define BRCMF_PCIE_ARMCR4REG_BANKIDX 0x40 96 #define BRCMF_PCIE_ARMCR4REG_BANKPDA 0x4C 97 98 #define BRCMF_PCIE_REG_INTSTATUS 0x90 99 #define BRCMF_PCIE_REG_INTMASK 0x94 100 #define BRCMF_PCIE_REG_SBMBX 0x98 101 102 #define BRCMF_PCIE_REG_LINK_STATUS_CTRL 0xBC 103 104 #define BRCMF_PCIE_PCIE2REG_INTMASK 0x24 105 #define BRCMF_PCIE_PCIE2REG_MAILBOXINT 0x48 106 #define BRCMF_PCIE_PCIE2REG_MAILBOXMASK 0x4C 107 #define BRCMF_PCIE_PCIE2REG_CONFIGADDR 0x120 108 #define BRCMF_PCIE_PCIE2REG_CONFIGDATA 0x124 109 #define BRCMF_PCIE_PCIE2REG_H2D_MAILBOX_0 0x140 110 #define BRCMF_PCIE_PCIE2REG_H2D_MAILBOX_1 0x144 111 112 #define BRCMF_PCIE2_INTA 0x01 113 #define BRCMF_PCIE2_INTB 0x02 114 115 #define BRCMF_PCIE_INT_0 0x01 116 #define BRCMF_PCIE_INT_1 0x02 117 #define BRCMF_PCIE_INT_DEF (BRCMF_PCIE_INT_0 | \ 118 BRCMF_PCIE_INT_1) 119 120 #define BRCMF_PCIE_MB_INT_FN0_0 0x0100 121 #define BRCMF_PCIE_MB_INT_FN0_1 0x0200 122 #define BRCMF_PCIE_MB_INT_D2H0_DB0 0x10000 123 #define BRCMF_PCIE_MB_INT_D2H0_DB1 0x20000 124 #define BRCMF_PCIE_MB_INT_D2H1_DB0 0x40000 125 #define BRCMF_PCIE_MB_INT_D2H1_DB1 0x80000 126 #define BRCMF_PCIE_MB_INT_D2H2_DB0 0x100000 127 #define BRCMF_PCIE_MB_INT_D2H2_DB1 0x200000 128 #define BRCMF_PCIE_MB_INT_D2H3_DB0 0x400000 129 #define BRCMF_PCIE_MB_INT_D2H3_DB1 0x800000 130 131 #define BRCMF_PCIE_MB_INT_D2H_DB (BRCMF_PCIE_MB_INT_D2H0_DB0 | \ 132 BRCMF_PCIE_MB_INT_D2H0_DB1 | \ 133 BRCMF_PCIE_MB_INT_D2H1_DB0 | \ 134 BRCMF_PCIE_MB_INT_D2H1_DB1 | \ 135 BRCMF_PCIE_MB_INT_D2H2_DB0 | \ 136 BRCMF_PCIE_MB_INT_D2H2_DB1 | \ 137 BRCMF_PCIE_MB_INT_D2H3_DB0 | \ 138 BRCMF_PCIE_MB_INT_D2H3_DB1) 139 140 #define BRCMF_PCIE_SHARED_VERSION_7 7 141 #define BRCMF_PCIE_MIN_SHARED_VERSION 5 142 #define BRCMF_PCIE_MAX_SHARED_VERSION BRCMF_PCIE_SHARED_VERSION_7 143 #define BRCMF_PCIE_SHARED_VERSION_MASK 0x00FF 144 #define BRCMF_PCIE_SHARED_DMA_INDEX 0x10000 145 #define BRCMF_PCIE_SHARED_DMA_2B_IDX 0x100000 146 #define BRCMF_PCIE_SHARED_HOSTRDY_DB1 0x10000000 147 148 #define BRCMF_PCIE_FLAGS_HTOD_SPLIT 0x4000 149 #define BRCMF_PCIE_FLAGS_DTOH_SPLIT 0x8000 150 151 #define BRCMF_SHARED_MAX_RXBUFPOST_OFFSET 34 152 #define BRCMF_SHARED_RING_BASE_OFFSET 52 153 #define BRCMF_SHARED_RX_DATAOFFSET_OFFSET 36 154 #define BRCMF_SHARED_CONSOLE_ADDR_OFFSET 20 155 #define BRCMF_SHARED_HTOD_MB_DATA_ADDR_OFFSET 40 156 #define BRCMF_SHARED_DTOH_MB_DATA_ADDR_OFFSET 44 157 #define BRCMF_SHARED_RING_INFO_ADDR_OFFSET 48 158 #define BRCMF_SHARED_DMA_SCRATCH_LEN_OFFSET 52 159 #define BRCMF_SHARED_DMA_SCRATCH_ADDR_OFFSET 56 160 #define BRCMF_SHARED_DMA_RINGUPD_LEN_OFFSET 64 161 #define BRCMF_SHARED_DMA_RINGUPD_ADDR_OFFSET 68 162 163 #define BRCMF_RING_H2D_RING_COUNT_OFFSET 0 164 #define BRCMF_RING_D2H_RING_COUNT_OFFSET 1 165 #define BRCMF_RING_H2D_RING_MEM_OFFSET 4 166 #define BRCMF_RING_H2D_RING_STATE_OFFSET 8 167 168 #define BRCMF_RING_MEM_BASE_ADDR_OFFSET 8 169 #define BRCMF_RING_MAX_ITEM_OFFSET 4 170 #define BRCMF_RING_LEN_ITEMS_OFFSET 6 171 #define BRCMF_RING_MEM_SZ 16 172 #define BRCMF_RING_STATE_SZ 8 173 174 #define BRCMF_DEF_MAX_RXBUFPOST 255 175 176 #define BRCMF_CONSOLE_BUFADDR_OFFSET 8 177 #define BRCMF_CONSOLE_BUFSIZE_OFFSET 12 178 #define BRCMF_CONSOLE_WRITEIDX_OFFSET 16 179 180 #define BRCMF_DMA_D2H_SCRATCH_BUF_LEN 8 181 #define BRCMF_DMA_D2H_RINGUPD_BUF_LEN 1024 182 183 #define BRCMF_D2H_DEV_D3_ACK 0x00000001 184 #define BRCMF_D2H_DEV_DS_ENTER_REQ 0x00000002 185 #define BRCMF_D2H_DEV_DS_EXIT_NOTE 0x00000004 186 #define BRCMF_D2H_DEV_FWHALT 0x10000000 187 188 #define BRCMF_H2D_HOST_D3_INFORM 0x00000001 189 #define BRCMF_H2D_HOST_DS_ACK 0x00000002 190 #define BRCMF_H2D_HOST_D0_INFORM_IN_USE 0x00000008 191 #define BRCMF_H2D_HOST_D0_INFORM 0x00000010 192 193 #define BRCMF_PCIE_MBDATA_TIMEOUT msecs_to_jiffies(2000) 194 195 #define BRCMF_PCIE_CFGREG_STATUS_CMD 0x4 196 #define BRCMF_PCIE_CFGREG_PM_CSR 0x4C 197 #define BRCMF_PCIE_CFGREG_MSI_CAP 0x58 198 #define BRCMF_PCIE_CFGREG_MSI_ADDR_L 0x5C 199 #define BRCMF_PCIE_CFGREG_MSI_ADDR_H 0x60 200 #define BRCMF_PCIE_CFGREG_MSI_DATA 0x64 201 #define BRCMF_PCIE_CFGREG_LINK_STATUS_CTRL 0xBC 202 #define BRCMF_PCIE_CFGREG_LINK_STATUS_CTRL2 0xDC 203 #define BRCMF_PCIE_CFGREG_RBAR_CTRL 0x228 204 #define BRCMF_PCIE_CFGREG_PML1_SUB_CTRL1 0x248 205 #define BRCMF_PCIE_CFGREG_REG_BAR2_CONFIG 0x4E0 206 #define BRCMF_PCIE_CFGREG_REG_BAR3_CONFIG 0x4F4 207 #define BRCMF_PCIE_LINK_STATUS_CTRL_ASPM_ENAB 3 208 209 /* Magic number at a magic location to find RAM size */ 210 #define BRCMF_RAMSIZE_MAGIC 0x534d4152 /* SMAR */ 211 #define BRCMF_RAMSIZE_OFFSET 0x6c 212 213 214 struct brcmf_pcie_console { 215 u32 base_addr; 216 u32 buf_addr; 217 u32 bufsize; 218 u32 read_idx; 219 u8 log_str[256]; 220 u8 log_idx; 221 }; 222 223 struct brcmf_pcie_shared_info { 224 u32 tcm_base_address; 225 u32 flags; 226 struct brcmf_pcie_ringbuf *commonrings[BRCMF_NROF_COMMON_MSGRINGS]; 227 struct brcmf_pcie_ringbuf *flowrings; 228 u16 max_rxbufpost; 229 u16 max_flowrings; 230 u16 max_submissionrings; 231 u16 max_completionrings; 232 u32 rx_dataoffset; 233 u32 htod_mb_data_addr; 234 u32 dtoh_mb_data_addr; 235 u32 ring_info_addr; 236 struct brcmf_pcie_console console; 237 void *scratch; 238 dma_addr_t scratch_dmahandle; 239 void *ringupd; 240 dma_addr_t ringupd_dmahandle; 241 u8 version; 242 }; 243 244 struct brcmf_pcie_core_info { 245 u32 base; 246 u32 wrapbase; 247 }; 248 249 struct brcmf_pciedev_info { 250 enum brcmf_pcie_state state; 251 bool in_irq; 252 struct pci_dev *pdev; 253 char fw_name[BRCMF_FW_NAME_LEN]; 254 char nvram_name[BRCMF_FW_NAME_LEN]; 255 void __iomem *regs; 256 void __iomem *tcm; 257 u32 ram_base; 258 u32 ram_size; 259 struct brcmf_chip *ci; 260 u32 coreid; 261 struct brcmf_pcie_shared_info shared; 262 wait_queue_head_t mbdata_resp_wait; 263 bool mbdata_completed; 264 bool irq_allocated; 265 bool wowl_enabled; 266 u8 dma_idx_sz; 267 void *idxbuf; 268 u32 idxbuf_sz; 269 dma_addr_t idxbuf_dmahandle; 270 u16 (*read_ptr)(struct brcmf_pciedev_info *devinfo, u32 mem_offset); 271 void (*write_ptr)(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 272 u16 value); 273 struct brcmf_mp_device *settings; 274 }; 275 276 struct brcmf_pcie_ringbuf { 277 struct brcmf_commonring commonring; 278 dma_addr_t dma_handle; 279 u32 w_idx_addr; 280 u32 r_idx_addr; 281 struct brcmf_pciedev_info *devinfo; 282 u8 id; 283 }; 284 285 /** 286 * struct brcmf_pcie_dhi_ringinfo - dongle/host interface shared ring info 287 * 288 * @ringmem: dongle memory pointer to ring memory location 289 * @h2d_w_idx_ptr: h2d ring write indices dongle memory pointers 290 * @h2d_r_idx_ptr: h2d ring read indices dongle memory pointers 291 * @d2h_w_idx_ptr: d2h ring write indices dongle memory pointers 292 * @d2h_r_idx_ptr: d2h ring read indices dongle memory pointers 293 * @h2d_w_idx_hostaddr: h2d ring write indices host memory pointers 294 * @h2d_r_idx_hostaddr: h2d ring read indices host memory pointers 295 * @d2h_w_idx_hostaddr: d2h ring write indices host memory pointers 296 * @d2h_r_idx_hostaddr: d2h ring reaD indices host memory pointers 297 * @max_flowrings: maximum number of tx flow rings supported. 298 * @max_submissionrings: maximum number of submission rings(h2d) supported. 299 * @max_completionrings: maximum number of completion rings(d2h) supported. 300 */ 301 struct brcmf_pcie_dhi_ringinfo { 302 __le32 ringmem; 303 __le32 h2d_w_idx_ptr; 304 __le32 h2d_r_idx_ptr; 305 __le32 d2h_w_idx_ptr; 306 __le32 d2h_r_idx_ptr; 307 struct msgbuf_buf_addr h2d_w_idx_hostaddr; 308 struct msgbuf_buf_addr h2d_r_idx_hostaddr; 309 struct msgbuf_buf_addr d2h_w_idx_hostaddr; 310 struct msgbuf_buf_addr d2h_r_idx_hostaddr; 311 __le16 max_flowrings; 312 __le16 max_submissionrings; 313 __le16 max_completionrings; 314 }; 315 316 static const u32 brcmf_ring_max_item[BRCMF_NROF_COMMON_MSGRINGS] = { 317 BRCMF_H2D_MSGRING_CONTROL_SUBMIT_MAX_ITEM, 318 BRCMF_H2D_MSGRING_RXPOST_SUBMIT_MAX_ITEM, 319 BRCMF_D2H_MSGRING_CONTROL_COMPLETE_MAX_ITEM, 320 BRCMF_D2H_MSGRING_TX_COMPLETE_MAX_ITEM, 321 BRCMF_D2H_MSGRING_RX_COMPLETE_MAX_ITEM 322 }; 323 324 static const u32 brcmf_ring_itemsize_pre_v7[BRCMF_NROF_COMMON_MSGRINGS] = { 325 BRCMF_H2D_MSGRING_CONTROL_SUBMIT_ITEMSIZE, 326 BRCMF_H2D_MSGRING_RXPOST_SUBMIT_ITEMSIZE, 327 BRCMF_D2H_MSGRING_CONTROL_COMPLETE_ITEMSIZE, 328 BRCMF_D2H_MSGRING_TX_COMPLETE_ITEMSIZE_PRE_V7, 329 BRCMF_D2H_MSGRING_RX_COMPLETE_ITEMSIZE_PRE_V7 330 }; 331 332 static const u32 brcmf_ring_itemsize[BRCMF_NROF_COMMON_MSGRINGS] = { 333 BRCMF_H2D_MSGRING_CONTROL_SUBMIT_ITEMSIZE, 334 BRCMF_H2D_MSGRING_RXPOST_SUBMIT_ITEMSIZE, 335 BRCMF_D2H_MSGRING_CONTROL_COMPLETE_ITEMSIZE, 336 BRCMF_D2H_MSGRING_TX_COMPLETE_ITEMSIZE, 337 BRCMF_D2H_MSGRING_RX_COMPLETE_ITEMSIZE 338 }; 339 340 static void brcmf_pcie_setup(struct device *dev, int ret, 341 struct brcmf_fw_request *fwreq); 342 static struct brcmf_fw_request * 343 brcmf_pcie_prepare_fw_request(struct brcmf_pciedev_info *devinfo); 344 345 static u32 346 brcmf_pcie_read_reg32(struct brcmf_pciedev_info *devinfo, u32 reg_offset) 347 { 348 void __iomem *address = devinfo->regs + reg_offset; 349 350 return (ioread32(address)); 351 } 352 353 354 static void 355 brcmf_pcie_write_reg32(struct brcmf_pciedev_info *devinfo, u32 reg_offset, 356 u32 value) 357 { 358 void __iomem *address = devinfo->regs + reg_offset; 359 360 iowrite32(value, address); 361 } 362 363 364 static u8 365 brcmf_pcie_read_tcm8(struct brcmf_pciedev_info *devinfo, u32 mem_offset) 366 { 367 void __iomem *address = devinfo->tcm + mem_offset; 368 369 return (ioread8(address)); 370 } 371 372 373 static u16 374 brcmf_pcie_read_tcm16(struct brcmf_pciedev_info *devinfo, u32 mem_offset) 375 { 376 void __iomem *address = devinfo->tcm + mem_offset; 377 378 return (ioread16(address)); 379 } 380 381 382 static void 383 brcmf_pcie_write_tcm16(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 384 u16 value) 385 { 386 void __iomem *address = devinfo->tcm + mem_offset; 387 388 iowrite16(value, address); 389 } 390 391 392 static u16 393 brcmf_pcie_read_idx(struct brcmf_pciedev_info *devinfo, u32 mem_offset) 394 { 395 u16 *address = devinfo->idxbuf + mem_offset; 396 397 return (*(address)); 398 } 399 400 401 static void 402 brcmf_pcie_write_idx(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 403 u16 value) 404 { 405 u16 *address = devinfo->idxbuf + mem_offset; 406 407 *(address) = value; 408 } 409 410 411 static u32 412 brcmf_pcie_read_tcm32(struct brcmf_pciedev_info *devinfo, u32 mem_offset) 413 { 414 void __iomem *address = devinfo->tcm + mem_offset; 415 416 return (ioread32(address)); 417 } 418 419 420 static void 421 brcmf_pcie_write_tcm32(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 422 u32 value) 423 { 424 void __iomem *address = devinfo->tcm + mem_offset; 425 426 iowrite32(value, address); 427 } 428 429 430 static u32 431 brcmf_pcie_read_ram32(struct brcmf_pciedev_info *devinfo, u32 mem_offset) 432 { 433 void __iomem *addr = devinfo->tcm + devinfo->ci->rambase + mem_offset; 434 435 return (ioread32(addr)); 436 } 437 438 439 static void 440 brcmf_pcie_write_ram32(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 441 u32 value) 442 { 443 void __iomem *addr = devinfo->tcm + devinfo->ci->rambase + mem_offset; 444 445 iowrite32(value, addr); 446 } 447 448 449 static void 450 brcmf_pcie_copy_mem_todev(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 451 void *srcaddr, u32 len) 452 { 453 void __iomem *address = devinfo->tcm + mem_offset; 454 __le32 *src32; 455 __le16 *src16; 456 u8 *src8; 457 458 if (((ulong)address & 4) || ((ulong)srcaddr & 4) || (len & 4)) { 459 if (((ulong)address & 2) || ((ulong)srcaddr & 2) || (len & 2)) { 460 src8 = (u8 *)srcaddr; 461 while (len) { 462 iowrite8(*src8, address); 463 address++; 464 src8++; 465 len--; 466 } 467 } else { 468 len = len / 2; 469 src16 = (__le16 *)srcaddr; 470 while (len) { 471 iowrite16(le16_to_cpu(*src16), address); 472 address += 2; 473 src16++; 474 len--; 475 } 476 } 477 } else { 478 len = len / 4; 479 src32 = (__le32 *)srcaddr; 480 while (len) { 481 iowrite32(le32_to_cpu(*src32), address); 482 address += 4; 483 src32++; 484 len--; 485 } 486 } 487 } 488 489 490 static void 491 brcmf_pcie_copy_dev_tomem(struct brcmf_pciedev_info *devinfo, u32 mem_offset, 492 void *dstaddr, u32 len) 493 { 494 void __iomem *address = devinfo->tcm + mem_offset; 495 __le32 *dst32; 496 __le16 *dst16; 497 u8 *dst8; 498 499 if (((ulong)address & 4) || ((ulong)dstaddr & 4) || (len & 4)) { 500 if (((ulong)address & 2) || ((ulong)dstaddr & 2) || (len & 2)) { 501 dst8 = (u8 *)dstaddr; 502 while (len) { 503 *dst8 = ioread8(address); 504 address++; 505 dst8++; 506 len--; 507 } 508 } else { 509 len = len / 2; 510 dst16 = (__le16 *)dstaddr; 511 while (len) { 512 *dst16 = cpu_to_le16(ioread16(address)); 513 address += 2; 514 dst16++; 515 len--; 516 } 517 } 518 } else { 519 len = len / 4; 520 dst32 = (__le32 *)dstaddr; 521 while (len) { 522 *dst32 = cpu_to_le32(ioread32(address)); 523 address += 4; 524 dst32++; 525 len--; 526 } 527 } 528 } 529 530 531 #define WRITECC32(devinfo, reg, value) brcmf_pcie_write_reg32(devinfo, \ 532 CHIPCREGOFFS(reg), value) 533 534 535 static void 536 brcmf_pcie_select_core(struct brcmf_pciedev_info *devinfo, u16 coreid) 537 { 538 const struct pci_dev *pdev = devinfo->pdev; 539 struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); 540 struct brcmf_core *core; 541 u32 bar0_win; 542 543 core = brcmf_chip_get_core(devinfo->ci, coreid); 544 if (core) { 545 bar0_win = core->base; 546 pci_write_config_dword(pdev, BRCMF_PCIE_BAR0_WINDOW, bar0_win); 547 if (pci_read_config_dword(pdev, BRCMF_PCIE_BAR0_WINDOW, 548 &bar0_win) == 0) { 549 if (bar0_win != core->base) { 550 bar0_win = core->base; 551 pci_write_config_dword(pdev, 552 BRCMF_PCIE_BAR0_WINDOW, 553 bar0_win); 554 } 555 } 556 } else { 557 brcmf_err(bus, "Unsupported core selected %x\n", coreid); 558 } 559 } 560 561 562 static void brcmf_pcie_reset_device(struct brcmf_pciedev_info *devinfo) 563 { 564 struct brcmf_core *core; 565 u16 cfg_offset[] = { BRCMF_PCIE_CFGREG_STATUS_CMD, 566 BRCMF_PCIE_CFGREG_PM_CSR, 567 BRCMF_PCIE_CFGREG_MSI_CAP, 568 BRCMF_PCIE_CFGREG_MSI_ADDR_L, 569 BRCMF_PCIE_CFGREG_MSI_ADDR_H, 570 BRCMF_PCIE_CFGREG_MSI_DATA, 571 BRCMF_PCIE_CFGREG_LINK_STATUS_CTRL2, 572 BRCMF_PCIE_CFGREG_RBAR_CTRL, 573 BRCMF_PCIE_CFGREG_PML1_SUB_CTRL1, 574 BRCMF_PCIE_CFGREG_REG_BAR2_CONFIG, 575 BRCMF_PCIE_CFGREG_REG_BAR3_CONFIG }; 576 u32 i; 577 u32 val; 578 u32 lsc; 579 580 if (!devinfo->ci) 581 return; 582 583 /* Disable ASPM */ 584 brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); 585 pci_read_config_dword(devinfo->pdev, BRCMF_PCIE_REG_LINK_STATUS_CTRL, 586 &lsc); 587 val = lsc & (~BRCMF_PCIE_LINK_STATUS_CTRL_ASPM_ENAB); 588 pci_write_config_dword(devinfo->pdev, BRCMF_PCIE_REG_LINK_STATUS_CTRL, 589 val); 590 591 /* Watchdog reset */ 592 brcmf_pcie_select_core(devinfo, BCMA_CORE_CHIPCOMMON); 593 WRITECC32(devinfo, watchdog, 4); 594 msleep(100); 595 596 /* Restore ASPM */ 597 brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); 598 pci_write_config_dword(devinfo->pdev, BRCMF_PCIE_REG_LINK_STATUS_CTRL, 599 lsc); 600 601 core = brcmf_chip_get_core(devinfo->ci, BCMA_CORE_PCIE2); 602 if (core->rev <= 13) { 603 for (i = 0; i < ARRAY_SIZE(cfg_offset); i++) { 604 brcmf_pcie_write_reg32(devinfo, 605 BRCMF_PCIE_PCIE2REG_CONFIGADDR, 606 cfg_offset[i]); 607 val = brcmf_pcie_read_reg32(devinfo, 608 BRCMF_PCIE_PCIE2REG_CONFIGDATA); 609 brcmf_dbg(PCIE, "config offset 0x%04x, value 0x%04x\n", 610 cfg_offset[i], val); 611 brcmf_pcie_write_reg32(devinfo, 612 BRCMF_PCIE_PCIE2REG_CONFIGDATA, 613 val); 614 } 615 } 616 } 617 618 619 static void brcmf_pcie_attach(struct brcmf_pciedev_info *devinfo) 620 { 621 u32 config; 622 623 /* BAR1 window may not be sized properly */ 624 brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); 625 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_CONFIGADDR, 0x4e0); 626 config = brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_CONFIGDATA); 627 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_CONFIGDATA, config); 628 629 device_wakeup_enable(&devinfo->pdev->dev); 630 } 631 632 633 static int brcmf_pcie_enter_download_state(struct brcmf_pciedev_info *devinfo) 634 { 635 if (devinfo->ci->chip == BRCM_CC_43602_CHIP_ID) { 636 brcmf_pcie_select_core(devinfo, BCMA_CORE_ARM_CR4); 637 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_ARMCR4REG_BANKIDX, 638 5); 639 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_ARMCR4REG_BANKPDA, 640 0); 641 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_ARMCR4REG_BANKIDX, 642 7); 643 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_ARMCR4REG_BANKPDA, 644 0); 645 } 646 return 0; 647 } 648 649 650 static int brcmf_pcie_exit_download_state(struct brcmf_pciedev_info *devinfo, 651 u32 resetintr) 652 { 653 struct brcmf_core *core; 654 655 if (devinfo->ci->chip == BRCM_CC_43602_CHIP_ID) { 656 core = brcmf_chip_get_core(devinfo->ci, BCMA_CORE_INTERNAL_MEM); 657 brcmf_chip_resetcore(core, 0, 0, 0); 658 } 659 660 if (!brcmf_chip_set_active(devinfo->ci, resetintr)) 661 return -EINVAL; 662 return 0; 663 } 664 665 666 static int 667 brcmf_pcie_send_mb_data(struct brcmf_pciedev_info *devinfo, u32 htod_mb_data) 668 { 669 struct brcmf_pcie_shared_info *shared; 670 struct brcmf_core *core; 671 u32 addr; 672 u32 cur_htod_mb_data; 673 u32 i; 674 675 shared = &devinfo->shared; 676 addr = shared->htod_mb_data_addr; 677 cur_htod_mb_data = brcmf_pcie_read_tcm32(devinfo, addr); 678 679 if (cur_htod_mb_data != 0) 680 brcmf_dbg(PCIE, "MB transaction is already pending 0x%04x\n", 681 cur_htod_mb_data); 682 683 i = 0; 684 while (cur_htod_mb_data != 0) { 685 msleep(10); 686 i++; 687 if (i > 100) 688 return -EIO; 689 cur_htod_mb_data = brcmf_pcie_read_tcm32(devinfo, addr); 690 } 691 692 brcmf_pcie_write_tcm32(devinfo, addr, htod_mb_data); 693 pci_write_config_dword(devinfo->pdev, BRCMF_PCIE_REG_SBMBX, 1); 694 695 /* Send mailbox interrupt twice as a hardware workaround */ 696 core = brcmf_chip_get_core(devinfo->ci, BCMA_CORE_PCIE2); 697 if (core->rev <= 13) 698 pci_write_config_dword(devinfo->pdev, BRCMF_PCIE_REG_SBMBX, 1); 699 700 return 0; 701 } 702 703 704 static void brcmf_pcie_handle_mb_data(struct brcmf_pciedev_info *devinfo) 705 { 706 struct brcmf_pcie_shared_info *shared; 707 u32 addr; 708 u32 dtoh_mb_data; 709 710 shared = &devinfo->shared; 711 addr = shared->dtoh_mb_data_addr; 712 dtoh_mb_data = brcmf_pcie_read_tcm32(devinfo, addr); 713 714 if (!dtoh_mb_data) 715 return; 716 717 brcmf_pcie_write_tcm32(devinfo, addr, 0); 718 719 brcmf_dbg(PCIE, "D2H_MB_DATA: 0x%04x\n", dtoh_mb_data); 720 if (dtoh_mb_data & BRCMF_D2H_DEV_DS_ENTER_REQ) { 721 brcmf_dbg(PCIE, "D2H_MB_DATA: DEEP SLEEP REQ\n"); 722 brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_DS_ACK); 723 brcmf_dbg(PCIE, "D2H_MB_DATA: sent DEEP SLEEP ACK\n"); 724 } 725 if (dtoh_mb_data & BRCMF_D2H_DEV_DS_EXIT_NOTE) 726 brcmf_dbg(PCIE, "D2H_MB_DATA: DEEP SLEEP EXIT\n"); 727 if (dtoh_mb_data & BRCMF_D2H_DEV_D3_ACK) { 728 brcmf_dbg(PCIE, "D2H_MB_DATA: D3 ACK\n"); 729 devinfo->mbdata_completed = true; 730 wake_up(&devinfo->mbdata_resp_wait); 731 } 732 if (dtoh_mb_data & BRCMF_D2H_DEV_FWHALT) { 733 brcmf_dbg(PCIE, "D2H_MB_DATA: FW HALT\n"); 734 brcmf_fw_crashed(&devinfo->pdev->dev); 735 } 736 } 737 738 739 static void brcmf_pcie_bus_console_init(struct brcmf_pciedev_info *devinfo) 740 { 741 struct brcmf_pcie_shared_info *shared; 742 struct brcmf_pcie_console *console; 743 u32 addr; 744 745 shared = &devinfo->shared; 746 console = &shared->console; 747 addr = shared->tcm_base_address + BRCMF_SHARED_CONSOLE_ADDR_OFFSET; 748 console->base_addr = brcmf_pcie_read_tcm32(devinfo, addr); 749 750 addr = console->base_addr + BRCMF_CONSOLE_BUFADDR_OFFSET; 751 console->buf_addr = brcmf_pcie_read_tcm32(devinfo, addr); 752 addr = console->base_addr + BRCMF_CONSOLE_BUFSIZE_OFFSET; 753 console->bufsize = brcmf_pcie_read_tcm32(devinfo, addr); 754 755 brcmf_dbg(FWCON, "Console: base %x, buf %x, size %d\n", 756 console->base_addr, console->buf_addr, console->bufsize); 757 } 758 759 /** 760 * brcmf_pcie_bus_console_read - reads firmware messages 761 * 762 * @error: specifies if error has occurred (prints messages unconditionally) 763 */ 764 static void brcmf_pcie_bus_console_read(struct brcmf_pciedev_info *devinfo, 765 bool error) 766 { 767 struct pci_dev *pdev = devinfo->pdev; 768 struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); 769 struct brcmf_pcie_console *console; 770 u32 addr; 771 u8 ch; 772 u32 newidx; 773 774 if (!error && !BRCMF_FWCON_ON()) 775 return; 776 777 console = &devinfo->shared.console; 778 addr = console->base_addr + BRCMF_CONSOLE_WRITEIDX_OFFSET; 779 newidx = brcmf_pcie_read_tcm32(devinfo, addr); 780 while (newidx != console->read_idx) { 781 addr = console->buf_addr + console->read_idx; 782 ch = brcmf_pcie_read_tcm8(devinfo, addr); 783 console->read_idx++; 784 if (console->read_idx == console->bufsize) 785 console->read_idx = 0; 786 if (ch == '\r') 787 continue; 788 console->log_str[console->log_idx] = ch; 789 console->log_idx++; 790 if ((ch != '\n') && 791 (console->log_idx == (sizeof(console->log_str) - 2))) { 792 ch = '\n'; 793 console->log_str[console->log_idx] = ch; 794 console->log_idx++; 795 } 796 if (ch == '\n') { 797 console->log_str[console->log_idx] = 0; 798 if (error) 799 __brcmf_err(bus, __func__, "CONSOLE: %s", 800 console->log_str); 801 else 802 pr_debug("CONSOLE: %s", console->log_str); 803 console->log_idx = 0; 804 } 805 } 806 } 807 808 809 static void brcmf_pcie_intr_disable(struct brcmf_pciedev_info *devinfo) 810 { 811 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXMASK, 0); 812 } 813 814 815 static void brcmf_pcie_intr_enable(struct brcmf_pciedev_info *devinfo) 816 { 817 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXMASK, 818 BRCMF_PCIE_MB_INT_D2H_DB | 819 BRCMF_PCIE_MB_INT_FN0_0 | 820 BRCMF_PCIE_MB_INT_FN0_1); 821 } 822 823 static void brcmf_pcie_hostready(struct brcmf_pciedev_info *devinfo) 824 { 825 if (devinfo->shared.flags & BRCMF_PCIE_SHARED_HOSTRDY_DB1) 826 brcmf_pcie_write_reg32(devinfo, 827 BRCMF_PCIE_PCIE2REG_H2D_MAILBOX_1, 1); 828 } 829 830 static irqreturn_t brcmf_pcie_quick_check_isr(int irq, void *arg) 831 { 832 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)arg; 833 834 if (brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT)) { 835 brcmf_pcie_intr_disable(devinfo); 836 brcmf_dbg(PCIE, "Enter\n"); 837 return IRQ_WAKE_THREAD; 838 } 839 return IRQ_NONE; 840 } 841 842 843 static irqreturn_t brcmf_pcie_isr_thread(int irq, void *arg) 844 { 845 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)arg; 846 u32 status; 847 848 devinfo->in_irq = true; 849 status = brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT); 850 brcmf_dbg(PCIE, "Enter %x\n", status); 851 if (status) { 852 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT, 853 status); 854 if (status & (BRCMF_PCIE_MB_INT_FN0_0 | 855 BRCMF_PCIE_MB_INT_FN0_1)) 856 brcmf_pcie_handle_mb_data(devinfo); 857 if (status & BRCMF_PCIE_MB_INT_D2H_DB) { 858 if (devinfo->state == BRCMFMAC_PCIE_STATE_UP) 859 brcmf_proto_msgbuf_rx_trigger( 860 &devinfo->pdev->dev); 861 } 862 } 863 brcmf_pcie_bus_console_read(devinfo, false); 864 if (devinfo->state == BRCMFMAC_PCIE_STATE_UP) 865 brcmf_pcie_intr_enable(devinfo); 866 devinfo->in_irq = false; 867 return IRQ_HANDLED; 868 } 869 870 871 static int brcmf_pcie_request_irq(struct brcmf_pciedev_info *devinfo) 872 { 873 struct pci_dev *pdev = devinfo->pdev; 874 struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); 875 876 brcmf_pcie_intr_disable(devinfo); 877 878 brcmf_dbg(PCIE, "Enter\n"); 879 880 pci_enable_msi(pdev); 881 if (request_threaded_irq(pdev->irq, brcmf_pcie_quick_check_isr, 882 brcmf_pcie_isr_thread, IRQF_SHARED, 883 "brcmf_pcie_intr", devinfo)) { 884 pci_disable_msi(pdev); 885 brcmf_err(bus, "Failed to request IRQ %d\n", pdev->irq); 886 return -EIO; 887 } 888 devinfo->irq_allocated = true; 889 return 0; 890 } 891 892 893 static void brcmf_pcie_release_irq(struct brcmf_pciedev_info *devinfo) 894 { 895 struct pci_dev *pdev = devinfo->pdev; 896 struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); 897 u32 status; 898 u32 count; 899 900 if (!devinfo->irq_allocated) 901 return; 902 903 brcmf_pcie_intr_disable(devinfo); 904 free_irq(pdev->irq, devinfo); 905 pci_disable_msi(pdev); 906 907 msleep(50); 908 count = 0; 909 while ((devinfo->in_irq) && (count < 20)) { 910 msleep(50); 911 count++; 912 } 913 if (devinfo->in_irq) 914 brcmf_err(bus, "Still in IRQ (processing) !!!\n"); 915 916 status = brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT); 917 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT, status); 918 919 devinfo->irq_allocated = false; 920 } 921 922 923 static int brcmf_pcie_ring_mb_write_rptr(void *ctx) 924 { 925 struct brcmf_pcie_ringbuf *ring = (struct brcmf_pcie_ringbuf *)ctx; 926 struct brcmf_pciedev_info *devinfo = ring->devinfo; 927 struct brcmf_commonring *commonring = &ring->commonring; 928 929 if (devinfo->state != BRCMFMAC_PCIE_STATE_UP) 930 return -EIO; 931 932 brcmf_dbg(PCIE, "W r_ptr %d (%d), ring %d\n", commonring->r_ptr, 933 commonring->w_ptr, ring->id); 934 935 devinfo->write_ptr(devinfo, ring->r_idx_addr, commonring->r_ptr); 936 937 return 0; 938 } 939 940 941 static int brcmf_pcie_ring_mb_write_wptr(void *ctx) 942 { 943 struct brcmf_pcie_ringbuf *ring = (struct brcmf_pcie_ringbuf *)ctx; 944 struct brcmf_pciedev_info *devinfo = ring->devinfo; 945 struct brcmf_commonring *commonring = &ring->commonring; 946 947 if (devinfo->state != BRCMFMAC_PCIE_STATE_UP) 948 return -EIO; 949 950 brcmf_dbg(PCIE, "W w_ptr %d (%d), ring %d\n", commonring->w_ptr, 951 commonring->r_ptr, ring->id); 952 953 devinfo->write_ptr(devinfo, ring->w_idx_addr, commonring->w_ptr); 954 955 return 0; 956 } 957 958 959 static int brcmf_pcie_ring_mb_ring_bell(void *ctx) 960 { 961 struct brcmf_pcie_ringbuf *ring = (struct brcmf_pcie_ringbuf *)ctx; 962 struct brcmf_pciedev_info *devinfo = ring->devinfo; 963 964 if (devinfo->state != BRCMFMAC_PCIE_STATE_UP) 965 return -EIO; 966 967 brcmf_dbg(PCIE, "RING !\n"); 968 /* Any arbitrary value will do, lets use 1 */ 969 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_H2D_MAILBOX_0, 1); 970 971 return 0; 972 } 973 974 975 static int brcmf_pcie_ring_mb_update_rptr(void *ctx) 976 { 977 struct brcmf_pcie_ringbuf *ring = (struct brcmf_pcie_ringbuf *)ctx; 978 struct brcmf_pciedev_info *devinfo = ring->devinfo; 979 struct brcmf_commonring *commonring = &ring->commonring; 980 981 if (devinfo->state != BRCMFMAC_PCIE_STATE_UP) 982 return -EIO; 983 984 commonring->r_ptr = devinfo->read_ptr(devinfo, ring->r_idx_addr); 985 986 brcmf_dbg(PCIE, "R r_ptr %d (%d), ring %d\n", commonring->r_ptr, 987 commonring->w_ptr, ring->id); 988 989 return 0; 990 } 991 992 993 static int brcmf_pcie_ring_mb_update_wptr(void *ctx) 994 { 995 struct brcmf_pcie_ringbuf *ring = (struct brcmf_pcie_ringbuf *)ctx; 996 struct brcmf_pciedev_info *devinfo = ring->devinfo; 997 struct brcmf_commonring *commonring = &ring->commonring; 998 999 if (devinfo->state != BRCMFMAC_PCIE_STATE_UP) 1000 return -EIO; 1001 1002 commonring->w_ptr = devinfo->read_ptr(devinfo, ring->w_idx_addr); 1003 1004 brcmf_dbg(PCIE, "R w_ptr %d (%d), ring %d\n", commonring->w_ptr, 1005 commonring->r_ptr, ring->id); 1006 1007 return 0; 1008 } 1009 1010 1011 static void * 1012 brcmf_pcie_init_dmabuffer_for_device(struct brcmf_pciedev_info *devinfo, 1013 u32 size, u32 tcm_dma_phys_addr, 1014 dma_addr_t *dma_handle) 1015 { 1016 void *ring; 1017 u64 address; 1018 1019 ring = dma_alloc_coherent(&devinfo->pdev->dev, size, dma_handle, 1020 GFP_KERNEL); 1021 if (!ring) 1022 return NULL; 1023 1024 address = (u64)*dma_handle; 1025 brcmf_pcie_write_tcm32(devinfo, tcm_dma_phys_addr, 1026 address & 0xffffffff); 1027 brcmf_pcie_write_tcm32(devinfo, tcm_dma_phys_addr + 4, address >> 32); 1028 1029 return (ring); 1030 } 1031 1032 1033 static struct brcmf_pcie_ringbuf * 1034 brcmf_pcie_alloc_dma_and_ring(struct brcmf_pciedev_info *devinfo, u32 ring_id, 1035 u32 tcm_ring_phys_addr) 1036 { 1037 void *dma_buf; 1038 dma_addr_t dma_handle; 1039 struct brcmf_pcie_ringbuf *ring; 1040 u32 size; 1041 u32 addr; 1042 const u32 *ring_itemsize_array; 1043 1044 if (devinfo->shared.version < BRCMF_PCIE_SHARED_VERSION_7) 1045 ring_itemsize_array = brcmf_ring_itemsize_pre_v7; 1046 else 1047 ring_itemsize_array = brcmf_ring_itemsize; 1048 1049 size = brcmf_ring_max_item[ring_id] * ring_itemsize_array[ring_id]; 1050 dma_buf = brcmf_pcie_init_dmabuffer_for_device(devinfo, size, 1051 tcm_ring_phys_addr + BRCMF_RING_MEM_BASE_ADDR_OFFSET, 1052 &dma_handle); 1053 if (!dma_buf) 1054 return NULL; 1055 1056 addr = tcm_ring_phys_addr + BRCMF_RING_MAX_ITEM_OFFSET; 1057 brcmf_pcie_write_tcm16(devinfo, addr, brcmf_ring_max_item[ring_id]); 1058 addr = tcm_ring_phys_addr + BRCMF_RING_LEN_ITEMS_OFFSET; 1059 brcmf_pcie_write_tcm16(devinfo, addr, ring_itemsize_array[ring_id]); 1060 1061 ring = kzalloc(sizeof(*ring), GFP_KERNEL); 1062 if (!ring) { 1063 dma_free_coherent(&devinfo->pdev->dev, size, dma_buf, 1064 dma_handle); 1065 return NULL; 1066 } 1067 brcmf_commonring_config(&ring->commonring, brcmf_ring_max_item[ring_id], 1068 ring_itemsize_array[ring_id], dma_buf); 1069 ring->dma_handle = dma_handle; 1070 ring->devinfo = devinfo; 1071 brcmf_commonring_register_cb(&ring->commonring, 1072 brcmf_pcie_ring_mb_ring_bell, 1073 brcmf_pcie_ring_mb_update_rptr, 1074 brcmf_pcie_ring_mb_update_wptr, 1075 brcmf_pcie_ring_mb_write_rptr, 1076 brcmf_pcie_ring_mb_write_wptr, ring); 1077 1078 return (ring); 1079 } 1080 1081 1082 static void brcmf_pcie_release_ringbuffer(struct device *dev, 1083 struct brcmf_pcie_ringbuf *ring) 1084 { 1085 void *dma_buf; 1086 u32 size; 1087 1088 if (!ring) 1089 return; 1090 1091 dma_buf = ring->commonring.buf_addr; 1092 if (dma_buf) { 1093 size = ring->commonring.depth * ring->commonring.item_len; 1094 dma_free_coherent(dev, size, dma_buf, ring->dma_handle); 1095 } 1096 kfree(ring); 1097 } 1098 1099 1100 static void brcmf_pcie_release_ringbuffers(struct brcmf_pciedev_info *devinfo) 1101 { 1102 u32 i; 1103 1104 for (i = 0; i < BRCMF_NROF_COMMON_MSGRINGS; i++) { 1105 brcmf_pcie_release_ringbuffer(&devinfo->pdev->dev, 1106 devinfo->shared.commonrings[i]); 1107 devinfo->shared.commonrings[i] = NULL; 1108 } 1109 kfree(devinfo->shared.flowrings); 1110 devinfo->shared.flowrings = NULL; 1111 if (devinfo->idxbuf) { 1112 dma_free_coherent(&devinfo->pdev->dev, 1113 devinfo->idxbuf_sz, 1114 devinfo->idxbuf, 1115 devinfo->idxbuf_dmahandle); 1116 devinfo->idxbuf = NULL; 1117 } 1118 } 1119 1120 1121 static int brcmf_pcie_init_ringbuffers(struct brcmf_pciedev_info *devinfo) 1122 { 1123 struct brcmf_bus *bus = dev_get_drvdata(&devinfo->pdev->dev); 1124 struct brcmf_pcie_ringbuf *ring; 1125 struct brcmf_pcie_ringbuf *rings; 1126 u32 d2h_w_idx_ptr; 1127 u32 d2h_r_idx_ptr; 1128 u32 h2d_w_idx_ptr; 1129 u32 h2d_r_idx_ptr; 1130 u32 ring_mem_ptr; 1131 u32 i; 1132 u64 address; 1133 u32 bufsz; 1134 u8 idx_offset; 1135 struct brcmf_pcie_dhi_ringinfo ringinfo; 1136 u16 max_flowrings; 1137 u16 max_submissionrings; 1138 u16 max_completionrings; 1139 1140 memcpy_fromio(&ringinfo, devinfo->tcm + devinfo->shared.ring_info_addr, 1141 sizeof(ringinfo)); 1142 if (devinfo->shared.version >= 6) { 1143 max_submissionrings = le16_to_cpu(ringinfo.max_submissionrings); 1144 max_flowrings = le16_to_cpu(ringinfo.max_flowrings); 1145 max_completionrings = le16_to_cpu(ringinfo.max_completionrings); 1146 } else { 1147 max_submissionrings = le16_to_cpu(ringinfo.max_flowrings); 1148 max_flowrings = max_submissionrings - 1149 BRCMF_NROF_H2D_COMMON_MSGRINGS; 1150 max_completionrings = BRCMF_NROF_D2H_COMMON_MSGRINGS; 1151 } 1152 1153 if (devinfo->dma_idx_sz != 0) { 1154 bufsz = (max_submissionrings + max_completionrings) * 1155 devinfo->dma_idx_sz * 2; 1156 devinfo->idxbuf = dma_alloc_coherent(&devinfo->pdev->dev, bufsz, 1157 &devinfo->idxbuf_dmahandle, 1158 GFP_KERNEL); 1159 if (!devinfo->idxbuf) 1160 devinfo->dma_idx_sz = 0; 1161 } 1162 1163 if (devinfo->dma_idx_sz == 0) { 1164 d2h_w_idx_ptr = le32_to_cpu(ringinfo.d2h_w_idx_ptr); 1165 d2h_r_idx_ptr = le32_to_cpu(ringinfo.d2h_r_idx_ptr); 1166 h2d_w_idx_ptr = le32_to_cpu(ringinfo.h2d_w_idx_ptr); 1167 h2d_r_idx_ptr = le32_to_cpu(ringinfo.h2d_r_idx_ptr); 1168 idx_offset = sizeof(u32); 1169 devinfo->write_ptr = brcmf_pcie_write_tcm16; 1170 devinfo->read_ptr = brcmf_pcie_read_tcm16; 1171 brcmf_dbg(PCIE, "Using TCM indices\n"); 1172 } else { 1173 memset(devinfo->idxbuf, 0, bufsz); 1174 devinfo->idxbuf_sz = bufsz; 1175 idx_offset = devinfo->dma_idx_sz; 1176 devinfo->write_ptr = brcmf_pcie_write_idx; 1177 devinfo->read_ptr = brcmf_pcie_read_idx; 1178 1179 h2d_w_idx_ptr = 0; 1180 address = (u64)devinfo->idxbuf_dmahandle; 1181 ringinfo.h2d_w_idx_hostaddr.low_addr = 1182 cpu_to_le32(address & 0xffffffff); 1183 ringinfo.h2d_w_idx_hostaddr.high_addr = 1184 cpu_to_le32(address >> 32); 1185 1186 h2d_r_idx_ptr = h2d_w_idx_ptr + 1187 max_submissionrings * idx_offset; 1188 address += max_submissionrings * idx_offset; 1189 ringinfo.h2d_r_idx_hostaddr.low_addr = 1190 cpu_to_le32(address & 0xffffffff); 1191 ringinfo.h2d_r_idx_hostaddr.high_addr = 1192 cpu_to_le32(address >> 32); 1193 1194 d2h_w_idx_ptr = h2d_r_idx_ptr + 1195 max_submissionrings * idx_offset; 1196 address += max_submissionrings * idx_offset; 1197 ringinfo.d2h_w_idx_hostaddr.low_addr = 1198 cpu_to_le32(address & 0xffffffff); 1199 ringinfo.d2h_w_idx_hostaddr.high_addr = 1200 cpu_to_le32(address >> 32); 1201 1202 d2h_r_idx_ptr = d2h_w_idx_ptr + 1203 max_completionrings * idx_offset; 1204 address += max_completionrings * idx_offset; 1205 ringinfo.d2h_r_idx_hostaddr.low_addr = 1206 cpu_to_le32(address & 0xffffffff); 1207 ringinfo.d2h_r_idx_hostaddr.high_addr = 1208 cpu_to_le32(address >> 32); 1209 1210 memcpy_toio(devinfo->tcm + devinfo->shared.ring_info_addr, 1211 &ringinfo, sizeof(ringinfo)); 1212 brcmf_dbg(PCIE, "Using host memory indices\n"); 1213 } 1214 1215 ring_mem_ptr = le32_to_cpu(ringinfo.ringmem); 1216 1217 for (i = 0; i < BRCMF_NROF_H2D_COMMON_MSGRINGS; i++) { 1218 ring = brcmf_pcie_alloc_dma_and_ring(devinfo, i, ring_mem_ptr); 1219 if (!ring) 1220 goto fail; 1221 ring->w_idx_addr = h2d_w_idx_ptr; 1222 ring->r_idx_addr = h2d_r_idx_ptr; 1223 ring->id = i; 1224 devinfo->shared.commonrings[i] = ring; 1225 1226 h2d_w_idx_ptr += idx_offset; 1227 h2d_r_idx_ptr += idx_offset; 1228 ring_mem_ptr += BRCMF_RING_MEM_SZ; 1229 } 1230 1231 for (i = BRCMF_NROF_H2D_COMMON_MSGRINGS; 1232 i < BRCMF_NROF_COMMON_MSGRINGS; i++) { 1233 ring = brcmf_pcie_alloc_dma_and_ring(devinfo, i, ring_mem_ptr); 1234 if (!ring) 1235 goto fail; 1236 ring->w_idx_addr = d2h_w_idx_ptr; 1237 ring->r_idx_addr = d2h_r_idx_ptr; 1238 ring->id = i; 1239 devinfo->shared.commonrings[i] = ring; 1240 1241 d2h_w_idx_ptr += idx_offset; 1242 d2h_r_idx_ptr += idx_offset; 1243 ring_mem_ptr += BRCMF_RING_MEM_SZ; 1244 } 1245 1246 devinfo->shared.max_flowrings = max_flowrings; 1247 devinfo->shared.max_submissionrings = max_submissionrings; 1248 devinfo->shared.max_completionrings = max_completionrings; 1249 rings = kcalloc(max_flowrings, sizeof(*ring), GFP_KERNEL); 1250 if (!rings) 1251 goto fail; 1252 1253 brcmf_dbg(PCIE, "Nr of flowrings is %d\n", max_flowrings); 1254 1255 for (i = 0; i < max_flowrings; i++) { 1256 ring = &rings[i]; 1257 ring->devinfo = devinfo; 1258 ring->id = i + BRCMF_H2D_MSGRING_FLOWRING_IDSTART; 1259 brcmf_commonring_register_cb(&ring->commonring, 1260 brcmf_pcie_ring_mb_ring_bell, 1261 brcmf_pcie_ring_mb_update_rptr, 1262 brcmf_pcie_ring_mb_update_wptr, 1263 brcmf_pcie_ring_mb_write_rptr, 1264 brcmf_pcie_ring_mb_write_wptr, 1265 ring); 1266 ring->w_idx_addr = h2d_w_idx_ptr; 1267 ring->r_idx_addr = h2d_r_idx_ptr; 1268 h2d_w_idx_ptr += idx_offset; 1269 h2d_r_idx_ptr += idx_offset; 1270 } 1271 devinfo->shared.flowrings = rings; 1272 1273 return 0; 1274 1275 fail: 1276 brcmf_err(bus, "Allocating ring buffers failed\n"); 1277 brcmf_pcie_release_ringbuffers(devinfo); 1278 return -ENOMEM; 1279 } 1280 1281 1282 static void 1283 brcmf_pcie_release_scratchbuffers(struct brcmf_pciedev_info *devinfo) 1284 { 1285 if (devinfo->shared.scratch) 1286 dma_free_coherent(&devinfo->pdev->dev, 1287 BRCMF_DMA_D2H_SCRATCH_BUF_LEN, 1288 devinfo->shared.scratch, 1289 devinfo->shared.scratch_dmahandle); 1290 if (devinfo->shared.ringupd) 1291 dma_free_coherent(&devinfo->pdev->dev, 1292 BRCMF_DMA_D2H_RINGUPD_BUF_LEN, 1293 devinfo->shared.ringupd, 1294 devinfo->shared.ringupd_dmahandle); 1295 } 1296 1297 static int brcmf_pcie_init_scratchbuffers(struct brcmf_pciedev_info *devinfo) 1298 { 1299 struct brcmf_bus *bus = dev_get_drvdata(&devinfo->pdev->dev); 1300 u64 address; 1301 u32 addr; 1302 1303 devinfo->shared.scratch = 1304 dma_alloc_coherent(&devinfo->pdev->dev, 1305 BRCMF_DMA_D2H_SCRATCH_BUF_LEN, 1306 &devinfo->shared.scratch_dmahandle, 1307 GFP_KERNEL); 1308 if (!devinfo->shared.scratch) 1309 goto fail; 1310 1311 addr = devinfo->shared.tcm_base_address + 1312 BRCMF_SHARED_DMA_SCRATCH_ADDR_OFFSET; 1313 address = (u64)devinfo->shared.scratch_dmahandle; 1314 brcmf_pcie_write_tcm32(devinfo, addr, address & 0xffffffff); 1315 brcmf_pcie_write_tcm32(devinfo, addr + 4, address >> 32); 1316 addr = devinfo->shared.tcm_base_address + 1317 BRCMF_SHARED_DMA_SCRATCH_LEN_OFFSET; 1318 brcmf_pcie_write_tcm32(devinfo, addr, BRCMF_DMA_D2H_SCRATCH_BUF_LEN); 1319 1320 devinfo->shared.ringupd = 1321 dma_alloc_coherent(&devinfo->pdev->dev, 1322 BRCMF_DMA_D2H_RINGUPD_BUF_LEN, 1323 &devinfo->shared.ringupd_dmahandle, 1324 GFP_KERNEL); 1325 if (!devinfo->shared.ringupd) 1326 goto fail; 1327 1328 addr = devinfo->shared.tcm_base_address + 1329 BRCMF_SHARED_DMA_RINGUPD_ADDR_OFFSET; 1330 address = (u64)devinfo->shared.ringupd_dmahandle; 1331 brcmf_pcie_write_tcm32(devinfo, addr, address & 0xffffffff); 1332 brcmf_pcie_write_tcm32(devinfo, addr + 4, address >> 32); 1333 addr = devinfo->shared.tcm_base_address + 1334 BRCMF_SHARED_DMA_RINGUPD_LEN_OFFSET; 1335 brcmf_pcie_write_tcm32(devinfo, addr, BRCMF_DMA_D2H_RINGUPD_BUF_LEN); 1336 return 0; 1337 1338 fail: 1339 brcmf_err(bus, "Allocating scratch buffers failed\n"); 1340 brcmf_pcie_release_scratchbuffers(devinfo); 1341 return -ENOMEM; 1342 } 1343 1344 1345 static void brcmf_pcie_down(struct device *dev) 1346 { 1347 } 1348 1349 1350 static int brcmf_pcie_tx(struct device *dev, struct sk_buff *skb) 1351 { 1352 return 0; 1353 } 1354 1355 1356 static int brcmf_pcie_tx_ctlpkt(struct device *dev, unsigned char *msg, 1357 uint len) 1358 { 1359 return 0; 1360 } 1361 1362 1363 static int brcmf_pcie_rx_ctlpkt(struct device *dev, unsigned char *msg, 1364 uint len) 1365 { 1366 return 0; 1367 } 1368 1369 1370 static void brcmf_pcie_wowl_config(struct device *dev, bool enabled) 1371 { 1372 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1373 struct brcmf_pciedev *buspub = bus_if->bus_priv.pcie; 1374 struct brcmf_pciedev_info *devinfo = buspub->devinfo; 1375 1376 brcmf_dbg(PCIE, "Configuring WOWL, enabled=%d\n", enabled); 1377 devinfo->wowl_enabled = enabled; 1378 } 1379 1380 1381 static size_t brcmf_pcie_get_ramsize(struct device *dev) 1382 { 1383 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1384 struct brcmf_pciedev *buspub = bus_if->bus_priv.pcie; 1385 struct brcmf_pciedev_info *devinfo = buspub->devinfo; 1386 1387 return devinfo->ci->ramsize - devinfo->ci->srsize; 1388 } 1389 1390 1391 static int brcmf_pcie_get_memdump(struct device *dev, void *data, size_t len) 1392 { 1393 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1394 struct brcmf_pciedev *buspub = bus_if->bus_priv.pcie; 1395 struct brcmf_pciedev_info *devinfo = buspub->devinfo; 1396 1397 brcmf_dbg(PCIE, "dump at 0x%08X: len=%zu\n", devinfo->ci->rambase, len); 1398 brcmf_pcie_copy_dev_tomem(devinfo, devinfo->ci->rambase, data, len); 1399 return 0; 1400 } 1401 1402 static 1403 int brcmf_pcie_get_fwname(struct device *dev, const char *ext, u8 *fw_name) 1404 { 1405 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1406 struct brcmf_fw_request *fwreq; 1407 struct brcmf_fw_name fwnames[] = { 1408 { ext, fw_name }, 1409 }; 1410 1411 fwreq = brcmf_fw_alloc_request(bus_if->chip, bus_if->chiprev, 1412 brcmf_pcie_fwnames, 1413 ARRAY_SIZE(brcmf_pcie_fwnames), 1414 fwnames, ARRAY_SIZE(fwnames)); 1415 if (!fwreq) 1416 return -ENOMEM; 1417 1418 kfree(fwreq); 1419 return 0; 1420 } 1421 1422 static int brcmf_pcie_reset(struct device *dev) 1423 { 1424 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1425 struct brcmf_pciedev *buspub = bus_if->bus_priv.pcie; 1426 struct brcmf_pciedev_info *devinfo = buspub->devinfo; 1427 struct brcmf_fw_request *fwreq; 1428 int err; 1429 1430 brcmf_pcie_intr_disable(devinfo); 1431 1432 brcmf_pcie_bus_console_read(devinfo, true); 1433 1434 brcmf_detach(dev); 1435 1436 brcmf_pcie_release_irq(devinfo); 1437 brcmf_pcie_release_scratchbuffers(devinfo); 1438 brcmf_pcie_release_ringbuffers(devinfo); 1439 brcmf_pcie_reset_device(devinfo); 1440 1441 fwreq = brcmf_pcie_prepare_fw_request(devinfo); 1442 if (!fwreq) { 1443 dev_err(dev, "Failed to prepare FW request\n"); 1444 return -ENOMEM; 1445 } 1446 1447 err = brcmf_fw_get_firmwares(dev, fwreq, brcmf_pcie_setup); 1448 if (err) { 1449 dev_err(dev, "Failed to prepare FW request\n"); 1450 kfree(fwreq); 1451 } 1452 1453 return err; 1454 } 1455 1456 static const struct brcmf_bus_ops brcmf_pcie_bus_ops = { 1457 .txdata = brcmf_pcie_tx, 1458 .stop = brcmf_pcie_down, 1459 .txctl = brcmf_pcie_tx_ctlpkt, 1460 .rxctl = brcmf_pcie_rx_ctlpkt, 1461 .wowl_config = brcmf_pcie_wowl_config, 1462 .get_ramsize = brcmf_pcie_get_ramsize, 1463 .get_memdump = brcmf_pcie_get_memdump, 1464 .get_fwname = brcmf_pcie_get_fwname, 1465 .reset = brcmf_pcie_reset, 1466 }; 1467 1468 1469 static void 1470 brcmf_pcie_adjust_ramsize(struct brcmf_pciedev_info *devinfo, u8 *data, 1471 u32 data_len) 1472 { 1473 __le32 *field; 1474 u32 newsize; 1475 1476 if (data_len < BRCMF_RAMSIZE_OFFSET + 8) 1477 return; 1478 1479 field = (__le32 *)&data[BRCMF_RAMSIZE_OFFSET]; 1480 if (le32_to_cpup(field) != BRCMF_RAMSIZE_MAGIC) 1481 return; 1482 field++; 1483 newsize = le32_to_cpup(field); 1484 1485 brcmf_dbg(PCIE, "Found ramsize info in FW, adjusting to 0x%x\n", 1486 newsize); 1487 devinfo->ci->ramsize = newsize; 1488 } 1489 1490 1491 static int 1492 brcmf_pcie_init_share_ram_info(struct brcmf_pciedev_info *devinfo, 1493 u32 sharedram_addr) 1494 { 1495 struct brcmf_bus *bus = dev_get_drvdata(&devinfo->pdev->dev); 1496 struct brcmf_pcie_shared_info *shared; 1497 u32 addr; 1498 1499 shared = &devinfo->shared; 1500 shared->tcm_base_address = sharedram_addr; 1501 1502 shared->flags = brcmf_pcie_read_tcm32(devinfo, sharedram_addr); 1503 shared->version = (u8)(shared->flags & BRCMF_PCIE_SHARED_VERSION_MASK); 1504 brcmf_dbg(PCIE, "PCIe protocol version %d\n", shared->version); 1505 if ((shared->version > BRCMF_PCIE_MAX_SHARED_VERSION) || 1506 (shared->version < BRCMF_PCIE_MIN_SHARED_VERSION)) { 1507 brcmf_err(bus, "Unsupported PCIE version %d\n", 1508 shared->version); 1509 return -EINVAL; 1510 } 1511 1512 /* check firmware support dma indicies */ 1513 if (shared->flags & BRCMF_PCIE_SHARED_DMA_INDEX) { 1514 if (shared->flags & BRCMF_PCIE_SHARED_DMA_2B_IDX) 1515 devinfo->dma_idx_sz = sizeof(u16); 1516 else 1517 devinfo->dma_idx_sz = sizeof(u32); 1518 } 1519 1520 addr = sharedram_addr + BRCMF_SHARED_MAX_RXBUFPOST_OFFSET; 1521 shared->max_rxbufpost = brcmf_pcie_read_tcm16(devinfo, addr); 1522 if (shared->max_rxbufpost == 0) 1523 shared->max_rxbufpost = BRCMF_DEF_MAX_RXBUFPOST; 1524 1525 addr = sharedram_addr + BRCMF_SHARED_RX_DATAOFFSET_OFFSET; 1526 shared->rx_dataoffset = brcmf_pcie_read_tcm32(devinfo, addr); 1527 1528 addr = sharedram_addr + BRCMF_SHARED_HTOD_MB_DATA_ADDR_OFFSET; 1529 shared->htod_mb_data_addr = brcmf_pcie_read_tcm32(devinfo, addr); 1530 1531 addr = sharedram_addr + BRCMF_SHARED_DTOH_MB_DATA_ADDR_OFFSET; 1532 shared->dtoh_mb_data_addr = brcmf_pcie_read_tcm32(devinfo, addr); 1533 1534 addr = sharedram_addr + BRCMF_SHARED_RING_INFO_ADDR_OFFSET; 1535 shared->ring_info_addr = brcmf_pcie_read_tcm32(devinfo, addr); 1536 1537 brcmf_dbg(PCIE, "max rx buf post %d, rx dataoffset %d\n", 1538 shared->max_rxbufpost, shared->rx_dataoffset); 1539 1540 brcmf_pcie_bus_console_init(devinfo); 1541 1542 return 0; 1543 } 1544 1545 1546 static int brcmf_pcie_download_fw_nvram(struct brcmf_pciedev_info *devinfo, 1547 const struct firmware *fw, void *nvram, 1548 u32 nvram_len) 1549 { 1550 struct brcmf_bus *bus = dev_get_drvdata(&devinfo->pdev->dev); 1551 u32 sharedram_addr; 1552 u32 sharedram_addr_written; 1553 u32 loop_counter; 1554 int err; 1555 u32 address; 1556 u32 resetintr; 1557 1558 brcmf_dbg(PCIE, "Halt ARM.\n"); 1559 err = brcmf_pcie_enter_download_state(devinfo); 1560 if (err) 1561 return err; 1562 1563 brcmf_dbg(PCIE, "Download FW %s\n", devinfo->fw_name); 1564 brcmf_pcie_copy_mem_todev(devinfo, devinfo->ci->rambase, 1565 (void *)fw->data, fw->size); 1566 1567 resetintr = get_unaligned_le32(fw->data); 1568 release_firmware(fw); 1569 1570 /* reset last 4 bytes of RAM address. to be used for shared 1571 * area. This identifies when FW is running 1572 */ 1573 brcmf_pcie_write_ram32(devinfo, devinfo->ci->ramsize - 4, 0); 1574 1575 if (nvram) { 1576 brcmf_dbg(PCIE, "Download NVRAM %s\n", devinfo->nvram_name); 1577 address = devinfo->ci->rambase + devinfo->ci->ramsize - 1578 nvram_len; 1579 brcmf_pcie_copy_mem_todev(devinfo, address, nvram, nvram_len); 1580 brcmf_fw_nvram_free(nvram); 1581 } else { 1582 brcmf_dbg(PCIE, "No matching NVRAM file found %s\n", 1583 devinfo->nvram_name); 1584 } 1585 1586 sharedram_addr_written = brcmf_pcie_read_ram32(devinfo, 1587 devinfo->ci->ramsize - 1588 4); 1589 brcmf_dbg(PCIE, "Bring ARM in running state\n"); 1590 err = brcmf_pcie_exit_download_state(devinfo, resetintr); 1591 if (err) 1592 return err; 1593 1594 brcmf_dbg(PCIE, "Wait for FW init\n"); 1595 sharedram_addr = sharedram_addr_written; 1596 loop_counter = BRCMF_PCIE_FW_UP_TIMEOUT / 50; 1597 while ((sharedram_addr == sharedram_addr_written) && (loop_counter)) { 1598 msleep(50); 1599 sharedram_addr = brcmf_pcie_read_ram32(devinfo, 1600 devinfo->ci->ramsize - 1601 4); 1602 loop_counter--; 1603 } 1604 if (sharedram_addr == sharedram_addr_written) { 1605 brcmf_err(bus, "FW failed to initialize\n"); 1606 return -ENODEV; 1607 } 1608 if (sharedram_addr < devinfo->ci->rambase || 1609 sharedram_addr >= devinfo->ci->rambase + devinfo->ci->ramsize) { 1610 brcmf_err(bus, "Invalid shared RAM address 0x%08x\n", 1611 sharedram_addr); 1612 return -ENODEV; 1613 } 1614 brcmf_dbg(PCIE, "Shared RAM addr: 0x%08x\n", sharedram_addr); 1615 1616 return (brcmf_pcie_init_share_ram_info(devinfo, sharedram_addr)); 1617 } 1618 1619 1620 static int brcmf_pcie_get_resource(struct brcmf_pciedev_info *devinfo) 1621 { 1622 struct pci_dev *pdev = devinfo->pdev; 1623 struct brcmf_bus *bus = dev_get_drvdata(&pdev->dev); 1624 int err; 1625 phys_addr_t bar0_addr, bar1_addr; 1626 ulong bar1_size; 1627 1628 err = pci_enable_device(pdev); 1629 if (err) { 1630 brcmf_err(bus, "pci_enable_device failed err=%d\n", err); 1631 return err; 1632 } 1633 1634 pci_set_master(pdev); 1635 1636 /* Bar-0 mapped address */ 1637 bar0_addr = pci_resource_start(pdev, 0); 1638 /* Bar-1 mapped address */ 1639 bar1_addr = pci_resource_start(pdev, 2); 1640 /* read Bar-1 mapped memory range */ 1641 bar1_size = pci_resource_len(pdev, 2); 1642 if ((bar1_size == 0) || (bar1_addr == 0)) { 1643 brcmf_err(bus, "BAR1 Not enabled, device size=%ld, addr=%#016llx\n", 1644 bar1_size, (unsigned long long)bar1_addr); 1645 return -EINVAL; 1646 } 1647 1648 devinfo->regs = ioremap(bar0_addr, BRCMF_PCIE_REG_MAP_SIZE); 1649 devinfo->tcm = ioremap(bar1_addr, bar1_size); 1650 1651 if (!devinfo->regs || !devinfo->tcm) { 1652 brcmf_err(bus, "ioremap() failed (%p,%p)\n", devinfo->regs, 1653 devinfo->tcm); 1654 return -EINVAL; 1655 } 1656 brcmf_dbg(PCIE, "Phys addr : reg space = %p base addr %#016llx\n", 1657 devinfo->regs, (unsigned long long)bar0_addr); 1658 brcmf_dbg(PCIE, "Phys addr : mem space = %p base addr %#016llx size 0x%x\n", 1659 devinfo->tcm, (unsigned long long)bar1_addr, 1660 (unsigned int)bar1_size); 1661 1662 return 0; 1663 } 1664 1665 1666 static void brcmf_pcie_release_resource(struct brcmf_pciedev_info *devinfo) 1667 { 1668 if (devinfo->tcm) 1669 iounmap(devinfo->tcm); 1670 if (devinfo->regs) 1671 iounmap(devinfo->regs); 1672 1673 pci_disable_device(devinfo->pdev); 1674 } 1675 1676 1677 static u32 brcmf_pcie_buscore_prep_addr(const struct pci_dev *pdev, u32 addr) 1678 { 1679 u32 ret_addr; 1680 1681 ret_addr = addr & (BRCMF_PCIE_BAR0_REG_SIZE - 1); 1682 addr &= ~(BRCMF_PCIE_BAR0_REG_SIZE - 1); 1683 pci_write_config_dword(pdev, BRCMF_PCIE_BAR0_WINDOW, addr); 1684 1685 return ret_addr; 1686 } 1687 1688 1689 static u32 brcmf_pcie_buscore_read32(void *ctx, u32 addr) 1690 { 1691 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)ctx; 1692 1693 addr = brcmf_pcie_buscore_prep_addr(devinfo->pdev, addr); 1694 return brcmf_pcie_read_reg32(devinfo, addr); 1695 } 1696 1697 1698 static void brcmf_pcie_buscore_write32(void *ctx, u32 addr, u32 value) 1699 { 1700 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)ctx; 1701 1702 addr = brcmf_pcie_buscore_prep_addr(devinfo->pdev, addr); 1703 brcmf_pcie_write_reg32(devinfo, addr, value); 1704 } 1705 1706 1707 static int brcmf_pcie_buscoreprep(void *ctx) 1708 { 1709 return brcmf_pcie_get_resource(ctx); 1710 } 1711 1712 1713 static int brcmf_pcie_buscore_reset(void *ctx, struct brcmf_chip *chip) 1714 { 1715 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)ctx; 1716 u32 val; 1717 1718 devinfo->ci = chip; 1719 brcmf_pcie_reset_device(devinfo); 1720 1721 val = brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT); 1722 if (val != 0xffffffff) 1723 brcmf_pcie_write_reg32(devinfo, BRCMF_PCIE_PCIE2REG_MAILBOXINT, 1724 val); 1725 1726 return 0; 1727 } 1728 1729 1730 static void brcmf_pcie_buscore_activate(void *ctx, struct brcmf_chip *chip, 1731 u32 rstvec) 1732 { 1733 struct brcmf_pciedev_info *devinfo = (struct brcmf_pciedev_info *)ctx; 1734 1735 brcmf_pcie_write_tcm32(devinfo, 0, rstvec); 1736 } 1737 1738 1739 static const struct brcmf_buscore_ops brcmf_pcie_buscore_ops = { 1740 .prepare = brcmf_pcie_buscoreprep, 1741 .reset = brcmf_pcie_buscore_reset, 1742 .activate = brcmf_pcie_buscore_activate, 1743 .read32 = brcmf_pcie_buscore_read32, 1744 .write32 = brcmf_pcie_buscore_write32, 1745 }; 1746 1747 #define BRCMF_PCIE_FW_CODE 0 1748 #define BRCMF_PCIE_FW_NVRAM 1 1749 1750 static void brcmf_pcie_setup(struct device *dev, int ret, 1751 struct brcmf_fw_request *fwreq) 1752 { 1753 const struct firmware *fw; 1754 void *nvram; 1755 struct brcmf_bus *bus; 1756 struct brcmf_pciedev *pcie_bus_dev; 1757 struct brcmf_pciedev_info *devinfo; 1758 struct brcmf_commonring **flowrings; 1759 u32 i, nvram_len; 1760 1761 /* check firmware loading result */ 1762 if (ret) 1763 goto fail; 1764 1765 bus = dev_get_drvdata(dev); 1766 pcie_bus_dev = bus->bus_priv.pcie; 1767 devinfo = pcie_bus_dev->devinfo; 1768 brcmf_pcie_attach(devinfo); 1769 1770 fw = fwreq->items[BRCMF_PCIE_FW_CODE].binary; 1771 nvram = fwreq->items[BRCMF_PCIE_FW_NVRAM].nv_data.data; 1772 nvram_len = fwreq->items[BRCMF_PCIE_FW_NVRAM].nv_data.len; 1773 kfree(fwreq); 1774 1775 ret = brcmf_chip_get_raminfo(devinfo->ci); 1776 if (ret) { 1777 brcmf_err(bus, "Failed to get RAM info\n"); 1778 goto fail; 1779 } 1780 1781 /* Some of the firmwares have the size of the memory of the device 1782 * defined inside the firmware. This is because part of the memory in 1783 * the device is shared and the devision is determined by FW. Parse 1784 * the firmware and adjust the chip memory size now. 1785 */ 1786 brcmf_pcie_adjust_ramsize(devinfo, (u8 *)fw->data, fw->size); 1787 1788 ret = brcmf_pcie_download_fw_nvram(devinfo, fw, nvram, nvram_len); 1789 if (ret) 1790 goto fail; 1791 1792 devinfo->state = BRCMFMAC_PCIE_STATE_UP; 1793 1794 ret = brcmf_pcie_init_ringbuffers(devinfo); 1795 if (ret) 1796 goto fail; 1797 1798 ret = brcmf_pcie_init_scratchbuffers(devinfo); 1799 if (ret) 1800 goto fail; 1801 1802 brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); 1803 ret = brcmf_pcie_request_irq(devinfo); 1804 if (ret) 1805 goto fail; 1806 1807 /* hook the commonrings in the bus structure. */ 1808 for (i = 0; i < BRCMF_NROF_COMMON_MSGRINGS; i++) 1809 bus->msgbuf->commonrings[i] = 1810 &devinfo->shared.commonrings[i]->commonring; 1811 1812 flowrings = kcalloc(devinfo->shared.max_flowrings, sizeof(*flowrings), 1813 GFP_KERNEL); 1814 if (!flowrings) 1815 goto fail; 1816 1817 for (i = 0; i < devinfo->shared.max_flowrings; i++) 1818 flowrings[i] = &devinfo->shared.flowrings[i].commonring; 1819 bus->msgbuf->flowrings = flowrings; 1820 1821 bus->msgbuf->rx_dataoffset = devinfo->shared.rx_dataoffset; 1822 bus->msgbuf->max_rxbufpost = devinfo->shared.max_rxbufpost; 1823 bus->msgbuf->max_flowrings = devinfo->shared.max_flowrings; 1824 1825 init_waitqueue_head(&devinfo->mbdata_resp_wait); 1826 1827 brcmf_pcie_intr_enable(devinfo); 1828 brcmf_pcie_hostready(devinfo); 1829 1830 ret = brcmf_attach(&devinfo->pdev->dev); 1831 if (ret) 1832 goto fail; 1833 1834 brcmf_pcie_bus_console_read(devinfo, false); 1835 1836 return; 1837 1838 fail: 1839 device_release_driver(dev); 1840 } 1841 1842 static struct brcmf_fw_request * 1843 brcmf_pcie_prepare_fw_request(struct brcmf_pciedev_info *devinfo) 1844 { 1845 struct brcmf_fw_request *fwreq; 1846 struct brcmf_fw_name fwnames[] = { 1847 { ".bin", devinfo->fw_name }, 1848 { ".txt", devinfo->nvram_name }, 1849 }; 1850 1851 fwreq = brcmf_fw_alloc_request(devinfo->ci->chip, devinfo->ci->chiprev, 1852 brcmf_pcie_fwnames, 1853 ARRAY_SIZE(brcmf_pcie_fwnames), 1854 fwnames, ARRAY_SIZE(fwnames)); 1855 if (!fwreq) 1856 return NULL; 1857 1858 fwreq->items[BRCMF_PCIE_FW_CODE].type = BRCMF_FW_TYPE_BINARY; 1859 fwreq->items[BRCMF_PCIE_FW_NVRAM].type = BRCMF_FW_TYPE_NVRAM; 1860 fwreq->items[BRCMF_PCIE_FW_NVRAM].flags = BRCMF_FW_REQF_OPTIONAL; 1861 fwreq->board_type = devinfo->settings->board_type; 1862 /* NVRAM reserves PCI domain 0 for Broadcom's SDK faked bus */ 1863 fwreq->domain_nr = pci_domain_nr(devinfo->pdev->bus) + 1; 1864 fwreq->bus_nr = devinfo->pdev->bus->number; 1865 1866 return fwreq; 1867 } 1868 1869 static int 1870 brcmf_pcie_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1871 { 1872 int ret; 1873 struct brcmf_fw_request *fwreq; 1874 struct brcmf_pciedev_info *devinfo; 1875 struct brcmf_pciedev *pcie_bus_dev; 1876 struct brcmf_bus *bus; 1877 1878 brcmf_dbg(PCIE, "Enter %x:%x\n", pdev->vendor, pdev->device); 1879 1880 ret = -ENOMEM; 1881 devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL); 1882 if (devinfo == NULL) 1883 return ret; 1884 1885 devinfo->pdev = pdev; 1886 pcie_bus_dev = NULL; 1887 devinfo->ci = brcmf_chip_attach(devinfo, &brcmf_pcie_buscore_ops); 1888 if (IS_ERR(devinfo->ci)) { 1889 ret = PTR_ERR(devinfo->ci); 1890 devinfo->ci = NULL; 1891 goto fail; 1892 } 1893 1894 pcie_bus_dev = kzalloc(sizeof(*pcie_bus_dev), GFP_KERNEL); 1895 if (pcie_bus_dev == NULL) { 1896 ret = -ENOMEM; 1897 goto fail; 1898 } 1899 1900 devinfo->settings = brcmf_get_module_param(&devinfo->pdev->dev, 1901 BRCMF_BUSTYPE_PCIE, 1902 devinfo->ci->chip, 1903 devinfo->ci->chiprev); 1904 if (!devinfo->settings) { 1905 ret = -ENOMEM; 1906 goto fail; 1907 } 1908 1909 bus = kzalloc(sizeof(*bus), GFP_KERNEL); 1910 if (!bus) { 1911 ret = -ENOMEM; 1912 goto fail; 1913 } 1914 bus->msgbuf = kzalloc(sizeof(*bus->msgbuf), GFP_KERNEL); 1915 if (!bus->msgbuf) { 1916 ret = -ENOMEM; 1917 kfree(bus); 1918 goto fail; 1919 } 1920 1921 /* hook it all together. */ 1922 pcie_bus_dev->devinfo = devinfo; 1923 pcie_bus_dev->bus = bus; 1924 bus->dev = &pdev->dev; 1925 bus->bus_priv.pcie = pcie_bus_dev; 1926 bus->ops = &brcmf_pcie_bus_ops; 1927 bus->proto_type = BRCMF_PROTO_MSGBUF; 1928 bus->chip = devinfo->coreid; 1929 bus->wowl_supported = pci_pme_capable(pdev, PCI_D3hot); 1930 dev_set_drvdata(&pdev->dev, bus); 1931 1932 ret = brcmf_alloc(&devinfo->pdev->dev, devinfo->settings); 1933 if (ret) 1934 goto fail_bus; 1935 1936 fwreq = brcmf_pcie_prepare_fw_request(devinfo); 1937 if (!fwreq) { 1938 ret = -ENOMEM; 1939 goto fail_bus; 1940 } 1941 1942 ret = brcmf_fw_get_firmwares(bus->dev, fwreq, brcmf_pcie_setup); 1943 if (ret < 0) { 1944 kfree(fwreq); 1945 goto fail_bus; 1946 } 1947 return 0; 1948 1949 fail_bus: 1950 kfree(bus->msgbuf); 1951 kfree(bus); 1952 fail: 1953 brcmf_err(NULL, "failed %x:%x\n", pdev->vendor, pdev->device); 1954 brcmf_pcie_release_resource(devinfo); 1955 if (devinfo->ci) 1956 brcmf_chip_detach(devinfo->ci); 1957 if (devinfo->settings) 1958 brcmf_release_module_param(devinfo->settings); 1959 kfree(pcie_bus_dev); 1960 kfree(devinfo); 1961 return ret; 1962 } 1963 1964 1965 static void 1966 brcmf_pcie_remove(struct pci_dev *pdev) 1967 { 1968 struct brcmf_pciedev_info *devinfo; 1969 struct brcmf_bus *bus; 1970 1971 brcmf_dbg(PCIE, "Enter\n"); 1972 1973 bus = dev_get_drvdata(&pdev->dev); 1974 if (bus == NULL) 1975 return; 1976 1977 devinfo = bus->bus_priv.pcie->devinfo; 1978 1979 devinfo->state = BRCMFMAC_PCIE_STATE_DOWN; 1980 if (devinfo->ci) 1981 brcmf_pcie_intr_disable(devinfo); 1982 1983 brcmf_detach(&pdev->dev); 1984 brcmf_free(&pdev->dev); 1985 1986 kfree(bus->bus_priv.pcie); 1987 kfree(bus->msgbuf->flowrings); 1988 kfree(bus->msgbuf); 1989 kfree(bus); 1990 1991 brcmf_pcie_release_irq(devinfo); 1992 brcmf_pcie_release_scratchbuffers(devinfo); 1993 brcmf_pcie_release_ringbuffers(devinfo); 1994 brcmf_pcie_reset_device(devinfo); 1995 brcmf_pcie_release_resource(devinfo); 1996 1997 if (devinfo->ci) 1998 brcmf_chip_detach(devinfo->ci); 1999 if (devinfo->settings) 2000 brcmf_release_module_param(devinfo->settings); 2001 2002 kfree(devinfo); 2003 dev_set_drvdata(&pdev->dev, NULL); 2004 } 2005 2006 2007 #ifdef CONFIG_PM 2008 2009 2010 static int brcmf_pcie_pm_enter_D3(struct device *dev) 2011 { 2012 struct brcmf_pciedev_info *devinfo; 2013 struct brcmf_bus *bus; 2014 2015 brcmf_dbg(PCIE, "Enter\n"); 2016 2017 bus = dev_get_drvdata(dev); 2018 devinfo = bus->bus_priv.pcie->devinfo; 2019 2020 brcmf_bus_change_state(bus, BRCMF_BUS_DOWN); 2021 2022 devinfo->mbdata_completed = false; 2023 brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_D3_INFORM); 2024 2025 wait_event_timeout(devinfo->mbdata_resp_wait, devinfo->mbdata_completed, 2026 BRCMF_PCIE_MBDATA_TIMEOUT); 2027 if (!devinfo->mbdata_completed) { 2028 brcmf_err(bus, "Timeout on response for entering D3 substate\n"); 2029 brcmf_bus_change_state(bus, BRCMF_BUS_UP); 2030 return -EIO; 2031 } 2032 2033 devinfo->state = BRCMFMAC_PCIE_STATE_DOWN; 2034 2035 return 0; 2036 } 2037 2038 2039 static int brcmf_pcie_pm_leave_D3(struct device *dev) 2040 { 2041 struct brcmf_pciedev_info *devinfo; 2042 struct brcmf_bus *bus; 2043 struct pci_dev *pdev; 2044 int err; 2045 2046 brcmf_dbg(PCIE, "Enter\n"); 2047 2048 bus = dev_get_drvdata(dev); 2049 devinfo = bus->bus_priv.pcie->devinfo; 2050 brcmf_dbg(PCIE, "Enter, dev=%p, bus=%p\n", dev, bus); 2051 2052 /* Check if device is still up and running, if so we are ready */ 2053 if (brcmf_pcie_read_reg32(devinfo, BRCMF_PCIE_PCIE2REG_INTMASK) != 0) { 2054 brcmf_dbg(PCIE, "Try to wakeup device....\n"); 2055 if (brcmf_pcie_send_mb_data(devinfo, BRCMF_H2D_HOST_D0_INFORM)) 2056 goto cleanup; 2057 brcmf_dbg(PCIE, "Hot resume, continue....\n"); 2058 devinfo->state = BRCMFMAC_PCIE_STATE_UP; 2059 brcmf_pcie_select_core(devinfo, BCMA_CORE_PCIE2); 2060 brcmf_bus_change_state(bus, BRCMF_BUS_UP); 2061 brcmf_pcie_intr_enable(devinfo); 2062 brcmf_pcie_hostready(devinfo); 2063 return 0; 2064 } 2065 2066 cleanup: 2067 brcmf_chip_detach(devinfo->ci); 2068 devinfo->ci = NULL; 2069 pdev = devinfo->pdev; 2070 brcmf_pcie_remove(pdev); 2071 2072 err = brcmf_pcie_probe(pdev, NULL); 2073 if (err) 2074 brcmf_err(bus, "probe after resume failed, err=%d\n", err); 2075 2076 return err; 2077 } 2078 2079 2080 static const struct dev_pm_ops brcmf_pciedrvr_pm = { 2081 .suspend = brcmf_pcie_pm_enter_D3, 2082 .resume = brcmf_pcie_pm_leave_D3, 2083 .freeze = brcmf_pcie_pm_enter_D3, 2084 .restore = brcmf_pcie_pm_leave_D3, 2085 }; 2086 2087 2088 #endif /* CONFIG_PM */ 2089 2090 2091 #define BRCMF_PCIE_DEVICE(dev_id) { BRCM_PCIE_VENDOR_ID_BROADCOM, dev_id,\ 2092 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_OTHER << 8, 0xffff00, 0 } 2093 #define BRCMF_PCIE_DEVICE_SUB(dev_id, subvend, subdev) { \ 2094 BRCM_PCIE_VENDOR_ID_BROADCOM, dev_id,\ 2095 subvend, subdev, PCI_CLASS_NETWORK_OTHER << 8, 0xffff00, 0 } 2096 2097 static const struct pci_device_id brcmf_pcie_devid_table[] = { 2098 BRCMF_PCIE_DEVICE(BRCM_PCIE_4350_DEVICE_ID), 2099 BRCMF_PCIE_DEVICE_SUB(0x4355, BRCM_PCIE_VENDOR_ID_BROADCOM, 0x4355), 2100 BRCMF_PCIE_DEVICE(BRCM_PCIE_4354_RAW_DEVICE_ID), 2101 BRCMF_PCIE_DEVICE(BRCM_PCIE_4356_DEVICE_ID), 2102 BRCMF_PCIE_DEVICE(BRCM_PCIE_43567_DEVICE_ID), 2103 BRCMF_PCIE_DEVICE(BRCM_PCIE_43570_DEVICE_ID), 2104 BRCMF_PCIE_DEVICE(BRCM_PCIE_4358_DEVICE_ID), 2105 BRCMF_PCIE_DEVICE(BRCM_PCIE_4359_DEVICE_ID), 2106 BRCMF_PCIE_DEVICE(BRCM_PCIE_43602_DEVICE_ID), 2107 BRCMF_PCIE_DEVICE(BRCM_PCIE_43602_2G_DEVICE_ID), 2108 BRCMF_PCIE_DEVICE(BRCM_PCIE_43602_5G_DEVICE_ID), 2109 BRCMF_PCIE_DEVICE(BRCM_PCIE_43602_RAW_DEVICE_ID), 2110 BRCMF_PCIE_DEVICE(BRCM_PCIE_4364_DEVICE_ID), 2111 BRCMF_PCIE_DEVICE(BRCM_PCIE_4365_DEVICE_ID), 2112 BRCMF_PCIE_DEVICE(BRCM_PCIE_4365_2G_DEVICE_ID), 2113 BRCMF_PCIE_DEVICE(BRCM_PCIE_4365_5G_DEVICE_ID), 2114 BRCMF_PCIE_DEVICE_SUB(0x4365, BRCM_PCIE_VENDOR_ID_BROADCOM, 0x4365), 2115 BRCMF_PCIE_DEVICE(BRCM_PCIE_4366_DEVICE_ID), 2116 BRCMF_PCIE_DEVICE(BRCM_PCIE_4366_2G_DEVICE_ID), 2117 BRCMF_PCIE_DEVICE(BRCM_PCIE_4366_5G_DEVICE_ID), 2118 BRCMF_PCIE_DEVICE(BRCM_PCIE_4371_DEVICE_ID), 2119 { /* end: all zeroes */ } 2120 }; 2121 2122 2123 MODULE_DEVICE_TABLE(pci, brcmf_pcie_devid_table); 2124 2125 2126 static struct pci_driver brcmf_pciedrvr = { 2127 .node = {}, 2128 .name = KBUILD_MODNAME, 2129 .id_table = brcmf_pcie_devid_table, 2130 .probe = brcmf_pcie_probe, 2131 .remove = brcmf_pcie_remove, 2132 #ifdef CONFIG_PM 2133 .driver.pm = &brcmf_pciedrvr_pm, 2134 #endif 2135 .driver.coredump = brcmf_dev_coredump, 2136 }; 2137 2138 2139 void brcmf_pcie_register(void) 2140 { 2141 int err; 2142 2143 brcmf_dbg(PCIE, "Enter\n"); 2144 err = pci_register_driver(&brcmf_pciedrvr); 2145 if (err) 2146 brcmf_err(NULL, "PCIE driver registration failed, err=%d\n", 2147 err); 2148 } 2149 2150 2151 void brcmf_pcie_exit(void) 2152 { 2153 brcmf_dbg(PCIE, "Enter\n"); 2154 pci_unregister_driver(&brcmf_pciedrvr); 2155 } 2156