1 /* 2 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com> 3 * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com> 4 * 5 * Derived from the PCAN project file driver/src/pcan_pci.c: 6 * 7 * Copyright (C) 2001-2006 PEAK System-Technik GmbH 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the version 2 of the GNU General Public License 11 * as published by the Free Software Foundation 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 */ 18 19 #include <linux/kernel.h> 20 #include <linux/module.h> 21 #include <linux/interrupt.h> 22 #include <linux/netdevice.h> 23 #include <linux/delay.h> 24 #include <linux/pci.h> 25 #include <linux/io.h> 26 #include <linux/can.h> 27 #include <linux/can/dev.h> 28 29 #include "peak_canfd_user.h" 30 31 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>"); 32 MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCIe/M.2 FD family cards"); 33 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCIe/M.2 FD CAN cards"); 34 MODULE_LICENSE("GPL v2"); 35 36 #define PCIEFD_DRV_NAME "peak_pciefd" 37 38 #define PEAK_PCI_VENDOR_ID 0x001c /* The PCI device and vendor IDs */ 39 #define PEAK_PCIEFD_ID 0x0013 /* for PCIe slot cards */ 40 #define PCAN_CPCIEFD_ID 0x0014 /* for Compact-PCI Serial slot cards */ 41 #define PCAN_PCIE104FD_ID 0x0017 /* for PCIe-104 Express slot cards */ 42 #define PCAN_MINIPCIEFD_ID 0x0018 /* for mini-PCIe slot cards */ 43 #define PCAN_PCIEFD_OEM_ID 0x0019 /* for PCIe slot OEM cards */ 44 #define PCAN_M2_ID 0x001a /* for M2 slot cards */ 45 46 /* PEAK PCIe board access description */ 47 #define PCIEFD_BAR0_SIZE (64 * 1024) 48 #define PCIEFD_RX_DMA_SIZE (4 * 1024) 49 #define PCIEFD_TX_DMA_SIZE (4 * 1024) 50 51 #define PCIEFD_TX_PAGE_SIZE (2 * 1024) 52 53 /* System Control Registers */ 54 #define PCIEFD_REG_SYS_CTL_SET 0x0000 /* set bits */ 55 #define PCIEFD_REG_SYS_CTL_CLR 0x0004 /* clear bits */ 56 57 /* Version info registers */ 58 #define PCIEFD_REG_SYS_VER1 0x0040 /* version reg #1 */ 59 #define PCIEFD_REG_SYS_VER2 0x0044 /* version reg #2 */ 60 61 #define PCIEFD_FW_VERSION(x, y, z) (((u32)(x) << 24) | \ 62 ((u32)(y) << 16) | \ 63 ((u32)(z) << 8)) 64 65 /* System Control Registers Bits */ 66 #define PCIEFD_SYS_CTL_TS_RST 0x00000001 /* timestamp clock */ 67 #define PCIEFD_SYS_CTL_CLK_EN 0x00000002 /* system clock */ 68 69 /* CAN-FD channel addresses */ 70 #define PCIEFD_CANX_OFF(c) (((c) + 1) * 0x1000) 71 72 #define PCIEFD_ECHO_SKB_MAX PCANFD_ECHO_SKB_DEF 73 74 /* CAN-FD channel registers */ 75 #define PCIEFD_REG_CAN_MISC 0x0000 /* Misc. control */ 76 #define PCIEFD_REG_CAN_CLK_SEL 0x0008 /* Clock selector */ 77 #define PCIEFD_REG_CAN_CMD_PORT_L 0x0010 /* 64-bits command port */ 78 #define PCIEFD_REG_CAN_CMD_PORT_H 0x0014 79 #define PCIEFD_REG_CAN_TX_REQ_ACC 0x0020 /* Tx request accumulator */ 80 #define PCIEFD_REG_CAN_TX_CTL_SET 0x0030 /* Tx control set register */ 81 #define PCIEFD_REG_CAN_TX_CTL_CLR 0x0038 /* Tx control clear register */ 82 #define PCIEFD_REG_CAN_TX_DMA_ADDR_L 0x0040 /* 64-bits addr for Tx DMA */ 83 #define PCIEFD_REG_CAN_TX_DMA_ADDR_H 0x0044 84 #define PCIEFD_REG_CAN_RX_CTL_SET 0x0050 /* Rx control set register */ 85 #define PCIEFD_REG_CAN_RX_CTL_CLR 0x0058 /* Rx control clear register */ 86 #define PCIEFD_REG_CAN_RX_CTL_WRT 0x0060 /* Rx control write register */ 87 #define PCIEFD_REG_CAN_RX_CTL_ACK 0x0068 /* Rx control ACK register */ 88 #define PCIEFD_REG_CAN_RX_DMA_ADDR_L 0x0070 /* 64-bits addr for Rx DMA */ 89 #define PCIEFD_REG_CAN_RX_DMA_ADDR_H 0x0074 90 91 /* CAN-FD channel misc register bits */ 92 #define CANFD_MISC_TS_RST 0x00000001 /* timestamp cnt rst */ 93 94 /* CAN-FD channel Clock SELector Source & DIVider */ 95 #define CANFD_CLK_SEL_DIV_MASK 0x00000007 96 #define CANFD_CLK_SEL_DIV_60MHZ 0x00000000 /* SRC=240MHz only */ 97 #define CANFD_CLK_SEL_DIV_40MHZ 0x00000001 /* SRC=240MHz only */ 98 #define CANFD_CLK_SEL_DIV_30MHZ 0x00000002 /* SRC=240MHz only */ 99 #define CANFD_CLK_SEL_DIV_24MHZ 0x00000003 /* SRC=240MHz only */ 100 #define CANFD_CLK_SEL_DIV_20MHZ 0x00000004 /* SRC=240MHz only */ 101 102 #define CANFD_CLK_SEL_SRC_MASK 0x00000008 /* 0=80MHz, 1=240MHz */ 103 #define CANFD_CLK_SEL_SRC_240MHZ 0x00000008 104 #define CANFD_CLK_SEL_SRC_80MHZ (~CANFD_CLK_SEL_SRC_240MHZ & \ 105 CANFD_CLK_SEL_SRC_MASK) 106 107 #define CANFD_CLK_SEL_20MHZ (CANFD_CLK_SEL_SRC_240MHZ |\ 108 CANFD_CLK_SEL_DIV_20MHZ) 109 #define CANFD_CLK_SEL_24MHZ (CANFD_CLK_SEL_SRC_240MHZ |\ 110 CANFD_CLK_SEL_DIV_24MHZ) 111 #define CANFD_CLK_SEL_30MHZ (CANFD_CLK_SEL_SRC_240MHZ |\ 112 CANFD_CLK_SEL_DIV_30MHZ) 113 #define CANFD_CLK_SEL_40MHZ (CANFD_CLK_SEL_SRC_240MHZ |\ 114 CANFD_CLK_SEL_DIV_40MHZ) 115 #define CANFD_CLK_SEL_60MHZ (CANFD_CLK_SEL_SRC_240MHZ |\ 116 CANFD_CLK_SEL_DIV_60MHZ) 117 #define CANFD_CLK_SEL_80MHZ (CANFD_CLK_SEL_SRC_80MHZ) 118 119 /* CAN-FD channel Rx/Tx control register bits */ 120 #define CANFD_CTL_UNC_BIT 0x00010000 /* Uncached DMA mem */ 121 #define CANFD_CTL_RST_BIT 0x00020000 /* reset DMA action */ 122 #define CANFD_CTL_IEN_BIT 0x00040000 /* IRQ enable */ 123 124 /* Rx IRQ Count and Time Limits */ 125 #define CANFD_CTL_IRQ_CL_DEF 16 /* Rx msg max nb per IRQ in Rx DMA */ 126 #define CANFD_CTL_IRQ_TL_DEF 10 /* Time before IRQ if < CL (x100 µs) */ 127 128 #define CANFD_OPTIONS_SET (CANFD_OPTION_ERROR | CANFD_OPTION_BUSLOAD) 129 130 /* Tx anticipation window (link logical address should be aligned on 2K 131 * boundary) 132 */ 133 #define PCIEFD_TX_PAGE_COUNT (PCIEFD_TX_DMA_SIZE / PCIEFD_TX_PAGE_SIZE) 134 135 #define CANFD_MSG_LNK_TX 0x1001 /* Tx msgs link */ 136 137 /* 32-bits IRQ status fields, heading Rx DMA area */ 138 static inline int pciefd_irq_tag(u32 irq_status) 139 { 140 return irq_status & 0x0000000f; 141 } 142 143 static inline int pciefd_irq_rx_cnt(u32 irq_status) 144 { 145 return (irq_status & 0x000007f0) >> 4; 146 } 147 148 static inline int pciefd_irq_is_lnk(u32 irq_status) 149 { 150 return irq_status & 0x00010000; 151 } 152 153 /* Rx record */ 154 struct pciefd_rx_dma { 155 __le32 irq_status; 156 __le32 sys_time_low; 157 __le32 sys_time_high; 158 struct pucan_rx_msg msg[0]; 159 } __packed __aligned(4); 160 161 /* Tx Link record */ 162 struct pciefd_tx_link { 163 __le16 size; 164 __le16 type; 165 __le32 laddr_lo; 166 __le32 laddr_hi; 167 } __packed __aligned(4); 168 169 /* Tx page descriptor */ 170 struct pciefd_page { 171 void *vbase; /* page virtual address */ 172 dma_addr_t lbase; /* page logical address */ 173 u32 offset; 174 u32 size; 175 }; 176 177 /* CAN-FD channel object */ 178 struct pciefd_board; 179 struct pciefd_can { 180 struct peak_canfd_priv ucan; /* must be the first member */ 181 void __iomem *reg_base; /* channel config base addr */ 182 struct pciefd_board *board; /* reverse link */ 183 184 struct pucan_command pucan_cmd; /* command buffer */ 185 186 dma_addr_t rx_dma_laddr; /* DMA virtual and logical addr */ 187 void *rx_dma_vaddr; /* for Rx and Tx areas */ 188 dma_addr_t tx_dma_laddr; 189 void *tx_dma_vaddr; 190 191 struct pciefd_page tx_pages[PCIEFD_TX_PAGE_COUNT]; 192 u16 tx_pages_free; /* free Tx pages counter */ 193 u16 tx_page_index; /* current page used for Tx */ 194 spinlock_t tx_lock; 195 196 u32 irq_status; 197 u32 irq_tag; /* next irq tag */ 198 }; 199 200 /* PEAK-PCIe FD board object */ 201 struct pciefd_board { 202 void __iomem *reg_base; 203 struct pci_dev *pci_dev; 204 int can_count; 205 spinlock_t cmd_lock; /* 64-bits cmds must be atomic */ 206 struct pciefd_can *can[0]; /* array of network devices */ 207 }; 208 209 /* supported device ids. */ 210 static const struct pci_device_id peak_pciefd_tbl[] = { 211 {PEAK_PCI_VENDOR_ID, PEAK_PCIEFD_ID, PCI_ANY_ID, PCI_ANY_ID,}, 212 {PEAK_PCI_VENDOR_ID, PCAN_CPCIEFD_ID, PCI_ANY_ID, PCI_ANY_ID,}, 213 {PEAK_PCI_VENDOR_ID, PCAN_PCIE104FD_ID, PCI_ANY_ID, PCI_ANY_ID,}, 214 {PEAK_PCI_VENDOR_ID, PCAN_MINIPCIEFD_ID, PCI_ANY_ID, PCI_ANY_ID,}, 215 {PEAK_PCI_VENDOR_ID, PCAN_PCIEFD_OEM_ID, PCI_ANY_ID, PCI_ANY_ID,}, 216 {PEAK_PCI_VENDOR_ID, PCAN_M2_ID, PCI_ANY_ID, PCI_ANY_ID,}, 217 {0,} 218 }; 219 220 MODULE_DEVICE_TABLE(pci, peak_pciefd_tbl); 221 222 /* read a 32 bits value from a SYS block register */ 223 static inline u32 pciefd_sys_readreg(const struct pciefd_board *priv, u16 reg) 224 { 225 return readl(priv->reg_base + reg); 226 } 227 228 /* write a 32 bits value into a SYS block register */ 229 static inline void pciefd_sys_writereg(const struct pciefd_board *priv, 230 u32 val, u16 reg) 231 { 232 writel(val, priv->reg_base + reg); 233 } 234 235 /* read a 32 bits value from CAN-FD block register */ 236 static inline u32 pciefd_can_readreg(const struct pciefd_can *priv, u16 reg) 237 { 238 return readl(priv->reg_base + reg); 239 } 240 241 /* write a 32 bits value into a CAN-FD block register */ 242 static inline void pciefd_can_writereg(const struct pciefd_can *priv, 243 u32 val, u16 reg) 244 { 245 writel(val, priv->reg_base + reg); 246 } 247 248 /* give a channel logical Rx DMA address to the board */ 249 static void pciefd_can_setup_rx_dma(struct pciefd_can *priv) 250 { 251 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 252 const u32 dma_addr_h = (u32)(priv->rx_dma_laddr >> 32); 253 #else 254 const u32 dma_addr_h = 0; 255 #endif 256 257 /* (DMA must be reset for Rx) */ 258 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_RX_CTL_SET); 259 260 /* write the logical address of the Rx DMA area for this channel */ 261 pciefd_can_writereg(priv, (u32)priv->rx_dma_laddr, 262 PCIEFD_REG_CAN_RX_DMA_ADDR_L); 263 pciefd_can_writereg(priv, dma_addr_h, PCIEFD_REG_CAN_RX_DMA_ADDR_H); 264 265 /* also indicates that Rx DMA is cacheable */ 266 pciefd_can_writereg(priv, CANFD_CTL_UNC_BIT, PCIEFD_REG_CAN_RX_CTL_CLR); 267 } 268 269 /* clear channel logical Rx DMA address from the board */ 270 static void pciefd_can_clear_rx_dma(struct pciefd_can *priv) 271 { 272 /* DMA must be reset for Rx */ 273 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_RX_CTL_SET); 274 275 /* clear the logical address of the Rx DMA area for this channel */ 276 pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_L); 277 pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_H); 278 } 279 280 /* give a channel logical Tx DMA address to the board */ 281 static void pciefd_can_setup_tx_dma(struct pciefd_can *priv) 282 { 283 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 284 const u32 dma_addr_h = (u32)(priv->tx_dma_laddr >> 32); 285 #else 286 const u32 dma_addr_h = 0; 287 #endif 288 289 /* (DMA must be reset for Tx) */ 290 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_SET); 291 292 /* write the logical address of the Tx DMA area for this channel */ 293 pciefd_can_writereg(priv, (u32)priv->tx_dma_laddr, 294 PCIEFD_REG_CAN_TX_DMA_ADDR_L); 295 pciefd_can_writereg(priv, dma_addr_h, PCIEFD_REG_CAN_TX_DMA_ADDR_H); 296 297 /* also indicates that Tx DMA is cacheable */ 298 pciefd_can_writereg(priv, CANFD_CTL_UNC_BIT, PCIEFD_REG_CAN_TX_CTL_CLR); 299 } 300 301 /* clear channel logical Tx DMA address from the board */ 302 static void pciefd_can_clear_tx_dma(struct pciefd_can *priv) 303 { 304 /* DMA must be reset for Tx */ 305 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_SET); 306 307 /* clear the logical address of the Tx DMA area for this channel */ 308 pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_L); 309 pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_H); 310 } 311 312 static void pciefd_can_ack_rx_dma(struct pciefd_can *priv) 313 { 314 /* read value of current IRQ tag and inc it for next one */ 315 priv->irq_tag = le32_to_cpu(*(__le32 *)priv->rx_dma_vaddr); 316 priv->irq_tag++; 317 priv->irq_tag &= 0xf; 318 319 /* write the next IRQ tag for this CAN */ 320 pciefd_can_writereg(priv, priv->irq_tag, PCIEFD_REG_CAN_RX_CTL_ACK); 321 } 322 323 /* IRQ handler */ 324 static irqreturn_t pciefd_irq_handler(int irq, void *arg) 325 { 326 struct pciefd_can *priv = arg; 327 struct pciefd_rx_dma *rx_dma = priv->rx_dma_vaddr; 328 329 /* INTA mode only to sync with PCIe transaction */ 330 if (!pci_dev_msi_enabled(priv->board->pci_dev)) 331 (void)pciefd_sys_readreg(priv->board, PCIEFD_REG_SYS_VER1); 332 333 /* read IRQ status from the first 32-bits of the Rx DMA area */ 334 priv->irq_status = le32_to_cpu(rx_dma->irq_status); 335 336 /* check if this (shared) IRQ is for this CAN */ 337 if (pciefd_irq_tag(priv->irq_status) != priv->irq_tag) 338 return IRQ_NONE; 339 340 /* handle rx messages (if any) */ 341 peak_canfd_handle_msgs_list(&priv->ucan, 342 rx_dma->msg, 343 pciefd_irq_rx_cnt(priv->irq_status)); 344 345 /* handle tx link interrupt (if any) */ 346 if (pciefd_irq_is_lnk(priv->irq_status)) { 347 unsigned long flags; 348 349 spin_lock_irqsave(&priv->tx_lock, flags); 350 priv->tx_pages_free++; 351 spin_unlock_irqrestore(&priv->tx_lock, flags); 352 353 /* wake producer up (only if enough room in echo_skb array) */ 354 spin_lock_irqsave(&priv->ucan.echo_lock, flags); 355 if (!priv->ucan.can.echo_skb[priv->ucan.echo_idx]) 356 netif_wake_queue(priv->ucan.ndev); 357 358 spin_unlock_irqrestore(&priv->ucan.echo_lock, flags); 359 } 360 361 /* re-enable Rx DMA transfer for this CAN */ 362 pciefd_can_ack_rx_dma(priv); 363 364 return IRQ_HANDLED; 365 } 366 367 static int pciefd_enable_tx_path(struct peak_canfd_priv *ucan) 368 { 369 struct pciefd_can *priv = (struct pciefd_can *)ucan; 370 int i; 371 372 /* initialize the Tx pages descriptors */ 373 priv->tx_pages_free = PCIEFD_TX_PAGE_COUNT - 1; 374 priv->tx_page_index = 0; 375 376 priv->tx_pages[0].vbase = priv->tx_dma_vaddr; 377 priv->tx_pages[0].lbase = priv->tx_dma_laddr; 378 379 for (i = 0; i < PCIEFD_TX_PAGE_COUNT; i++) { 380 priv->tx_pages[i].offset = 0; 381 priv->tx_pages[i].size = PCIEFD_TX_PAGE_SIZE - 382 sizeof(struct pciefd_tx_link); 383 if (i) { 384 priv->tx_pages[i].vbase = 385 priv->tx_pages[i - 1].vbase + 386 PCIEFD_TX_PAGE_SIZE; 387 priv->tx_pages[i].lbase = 388 priv->tx_pages[i - 1].lbase + 389 PCIEFD_TX_PAGE_SIZE; 390 } 391 } 392 393 /* setup Tx DMA addresses into IP core */ 394 pciefd_can_setup_tx_dma(priv); 395 396 /* start (TX_RST=0) Tx Path */ 397 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_CLR); 398 399 return 0; 400 } 401 402 /* board specific CANFD command pre-processing */ 403 static int pciefd_pre_cmd(struct peak_canfd_priv *ucan) 404 { 405 struct pciefd_can *priv = (struct pciefd_can *)ucan; 406 u16 cmd = pucan_cmd_get_opcode(&priv->pucan_cmd); 407 int err; 408 409 /* pre-process command */ 410 switch (cmd) { 411 case PUCAN_CMD_NORMAL_MODE: 412 case PUCAN_CMD_LISTEN_ONLY_MODE: 413 414 if (ucan->can.state == CAN_STATE_BUS_OFF) 415 break; 416 417 /* going into operational mode: setup IRQ handler */ 418 err = request_irq(priv->ucan.ndev->irq, 419 pciefd_irq_handler, 420 IRQF_SHARED, 421 PCIEFD_DRV_NAME, 422 priv); 423 if (err) 424 return err; 425 426 /* setup Rx DMA address */ 427 pciefd_can_setup_rx_dma(priv); 428 429 /* setup max count of msgs per IRQ */ 430 pciefd_can_writereg(priv, (CANFD_CTL_IRQ_TL_DEF) << 8 | 431 CANFD_CTL_IRQ_CL_DEF, 432 PCIEFD_REG_CAN_RX_CTL_WRT); 433 434 /* clear DMA RST for Rx (Rx start) */ 435 pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, 436 PCIEFD_REG_CAN_RX_CTL_CLR); 437 438 /* reset timestamps */ 439 pciefd_can_writereg(priv, !CANFD_MISC_TS_RST, 440 PCIEFD_REG_CAN_MISC); 441 442 /* do an initial ACK */ 443 pciefd_can_ack_rx_dma(priv); 444 445 /* enable IRQ for this CAN after having set next irq_tag */ 446 pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT, 447 PCIEFD_REG_CAN_RX_CTL_SET); 448 449 /* Tx path will be setup as soon as RX_BARRIER is received */ 450 break; 451 default: 452 break; 453 } 454 455 return 0; 456 } 457 458 /* write a command */ 459 static int pciefd_write_cmd(struct peak_canfd_priv *ucan) 460 { 461 struct pciefd_can *priv = (struct pciefd_can *)ucan; 462 unsigned long flags; 463 464 /* 64-bits command is atomic */ 465 spin_lock_irqsave(&priv->board->cmd_lock, flags); 466 467 pciefd_can_writereg(priv, *(u32 *)ucan->cmd_buffer, 468 PCIEFD_REG_CAN_CMD_PORT_L); 469 pciefd_can_writereg(priv, *(u32 *)(ucan->cmd_buffer + 4), 470 PCIEFD_REG_CAN_CMD_PORT_H); 471 472 spin_unlock_irqrestore(&priv->board->cmd_lock, flags); 473 474 return 0; 475 } 476 477 /* board specific CANFD command post-processing */ 478 static int pciefd_post_cmd(struct peak_canfd_priv *ucan) 479 { 480 struct pciefd_can *priv = (struct pciefd_can *)ucan; 481 u16 cmd = pucan_cmd_get_opcode(&priv->pucan_cmd); 482 483 switch (cmd) { 484 case PUCAN_CMD_RESET_MODE: 485 486 if (ucan->can.state == CAN_STATE_STOPPED) 487 break; 488 489 /* controller now in reset mode: */ 490 491 /* disable IRQ for this CAN */ 492 pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT, 493 PCIEFD_REG_CAN_RX_CTL_CLR); 494 495 /* stop and reset DMA addresses in Tx/Rx engines */ 496 pciefd_can_clear_tx_dma(priv); 497 pciefd_can_clear_rx_dma(priv); 498 499 /* wait for above commands to complete (read cycle) */ 500 (void)pciefd_sys_readreg(priv->board, PCIEFD_REG_SYS_VER1); 501 502 free_irq(priv->ucan.ndev->irq, priv); 503 504 ucan->can.state = CAN_STATE_STOPPED; 505 506 break; 507 } 508 509 return 0; 510 } 511 512 static void *pciefd_alloc_tx_msg(struct peak_canfd_priv *ucan, u16 msg_size, 513 int *room_left) 514 { 515 struct pciefd_can *priv = (struct pciefd_can *)ucan; 516 struct pciefd_page *page = priv->tx_pages + priv->tx_page_index; 517 unsigned long flags; 518 void *msg; 519 520 spin_lock_irqsave(&priv->tx_lock, flags); 521 522 if (page->offset + msg_size > page->size) { 523 struct pciefd_tx_link *lk; 524 525 /* not enough space in this page: try another one */ 526 if (!priv->tx_pages_free) { 527 spin_unlock_irqrestore(&priv->tx_lock, flags); 528 529 /* Tx overflow */ 530 return NULL; 531 } 532 533 priv->tx_pages_free--; 534 535 /* keep address of the very last free slot of current page */ 536 lk = page->vbase + page->offset; 537 538 /* next, move on a new free page */ 539 priv->tx_page_index = (priv->tx_page_index + 1) % 540 PCIEFD_TX_PAGE_COUNT; 541 page = priv->tx_pages + priv->tx_page_index; 542 543 /* put link record to this new page at the end of prev one */ 544 lk->size = cpu_to_le16(sizeof(*lk)); 545 lk->type = cpu_to_le16(CANFD_MSG_LNK_TX); 546 lk->laddr_lo = cpu_to_le32(page->lbase); 547 548 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 549 lk->laddr_hi = cpu_to_le32(page->lbase >> 32); 550 #else 551 lk->laddr_hi = 0; 552 #endif 553 /* next msgs will be put from the begininng of this new page */ 554 page->offset = 0; 555 } 556 557 *room_left = priv->tx_pages_free * page->size; 558 559 spin_unlock_irqrestore(&priv->tx_lock, flags); 560 561 msg = page->vbase + page->offset; 562 563 /* give back room left in the tx ring */ 564 *room_left += page->size - (page->offset + msg_size); 565 566 return msg; 567 } 568 569 static int pciefd_write_tx_msg(struct peak_canfd_priv *ucan, 570 struct pucan_tx_msg *msg) 571 { 572 struct pciefd_can *priv = (struct pciefd_can *)ucan; 573 struct pciefd_page *page = priv->tx_pages + priv->tx_page_index; 574 575 /* this slot is now reserved for writing the frame */ 576 page->offset += le16_to_cpu(msg->size); 577 578 /* tell the board a frame has been written in Tx DMA area */ 579 pciefd_can_writereg(priv, 1, PCIEFD_REG_CAN_TX_REQ_ACC); 580 581 return 0; 582 } 583 584 /* probe for CAN-FD channel #pciefd_board->can_count */ 585 static int pciefd_can_probe(struct pciefd_board *pciefd) 586 { 587 struct net_device *ndev; 588 struct pciefd_can *priv; 589 u32 clk; 590 int err; 591 592 /* allocate the candev object with default isize of echo skbs ring */ 593 ndev = alloc_peak_canfd_dev(sizeof(*priv), pciefd->can_count, 594 PCIEFD_ECHO_SKB_MAX); 595 if (!ndev) { 596 dev_err(&pciefd->pci_dev->dev, 597 "failed to alloc candev object\n"); 598 goto failure; 599 } 600 601 priv = netdev_priv(ndev); 602 603 /* fill-in candev private object: */ 604 605 /* setup PCIe-FD own callbacks */ 606 priv->ucan.pre_cmd = pciefd_pre_cmd; 607 priv->ucan.write_cmd = pciefd_write_cmd; 608 priv->ucan.post_cmd = pciefd_post_cmd; 609 priv->ucan.enable_tx_path = pciefd_enable_tx_path; 610 priv->ucan.alloc_tx_msg = pciefd_alloc_tx_msg; 611 priv->ucan.write_tx_msg = pciefd_write_tx_msg; 612 613 /* setup PCIe-FD own command buffer */ 614 priv->ucan.cmd_buffer = &priv->pucan_cmd; 615 priv->ucan.cmd_maxlen = sizeof(priv->pucan_cmd); 616 617 priv->board = pciefd; 618 619 /* CAN config regs block address */ 620 priv->reg_base = pciefd->reg_base + PCIEFD_CANX_OFF(priv->ucan.index); 621 622 /* allocate non-cacheable DMA'able 4KB memory area for Rx */ 623 priv->rx_dma_vaddr = dmam_alloc_coherent(&pciefd->pci_dev->dev, 624 PCIEFD_RX_DMA_SIZE, 625 &priv->rx_dma_laddr, 626 GFP_KERNEL); 627 if (!priv->rx_dma_vaddr) { 628 dev_err(&pciefd->pci_dev->dev, 629 "Rx dmam_alloc_coherent(%u) failure\n", 630 PCIEFD_RX_DMA_SIZE); 631 goto err_free_candev; 632 } 633 634 /* allocate non-cacheable DMA'able 4KB memory area for Tx */ 635 priv->tx_dma_vaddr = dmam_alloc_coherent(&pciefd->pci_dev->dev, 636 PCIEFD_TX_DMA_SIZE, 637 &priv->tx_dma_laddr, 638 GFP_KERNEL); 639 if (!priv->tx_dma_vaddr) { 640 dev_err(&pciefd->pci_dev->dev, 641 "Tx dmam_alloc_coherent(%u) failure\n", 642 PCIEFD_TX_DMA_SIZE); 643 goto err_free_candev; 644 } 645 646 /* CAN clock in RST mode */ 647 pciefd_can_writereg(priv, CANFD_MISC_TS_RST, PCIEFD_REG_CAN_MISC); 648 649 /* read current clock value */ 650 clk = pciefd_can_readreg(priv, PCIEFD_REG_CAN_CLK_SEL); 651 switch (clk) { 652 case CANFD_CLK_SEL_20MHZ: 653 priv->ucan.can.clock.freq = 20 * 1000 * 1000; 654 break; 655 case CANFD_CLK_SEL_24MHZ: 656 priv->ucan.can.clock.freq = 24 * 1000 * 1000; 657 break; 658 case CANFD_CLK_SEL_30MHZ: 659 priv->ucan.can.clock.freq = 30 * 1000 * 1000; 660 break; 661 case CANFD_CLK_SEL_40MHZ: 662 priv->ucan.can.clock.freq = 40 * 1000 * 1000; 663 break; 664 case CANFD_CLK_SEL_60MHZ: 665 priv->ucan.can.clock.freq = 60 * 1000 * 1000; 666 break; 667 default: 668 pciefd_can_writereg(priv, CANFD_CLK_SEL_80MHZ, 669 PCIEFD_REG_CAN_CLK_SEL); 670 671 /* fallthough */ 672 case CANFD_CLK_SEL_80MHZ: 673 priv->ucan.can.clock.freq = 80 * 1000 * 1000; 674 break; 675 } 676 677 ndev->irq = pciefd->pci_dev->irq; 678 679 SET_NETDEV_DEV(ndev, &pciefd->pci_dev->dev); 680 681 err = register_candev(ndev); 682 if (err) { 683 dev_err(&pciefd->pci_dev->dev, 684 "couldn't register CAN device: %d\n", err); 685 goto err_free_candev; 686 } 687 688 spin_lock_init(&priv->tx_lock); 689 690 /* save the object address in the board structure */ 691 pciefd->can[pciefd->can_count] = priv; 692 693 dev_info(&pciefd->pci_dev->dev, "%s at reg_base=0x%p irq=%d\n", 694 ndev->name, priv->reg_base, ndev->irq); 695 696 return 0; 697 698 err_free_candev: 699 free_candev(ndev); 700 701 failure: 702 return -ENOMEM; 703 } 704 705 /* remove a CAN-FD channel by releasing all of its resources */ 706 static void pciefd_can_remove(struct pciefd_can *priv) 707 { 708 /* unregister (close) the can device to go back to RST mode first */ 709 unregister_candev(priv->ucan.ndev); 710 711 /* finally, free the candev object */ 712 free_candev(priv->ucan.ndev); 713 } 714 715 /* remove all CAN-FD channels by releasing their own resources */ 716 static void pciefd_can_remove_all(struct pciefd_board *pciefd) 717 { 718 while (pciefd->can_count > 0) 719 pciefd_can_remove(pciefd->can[--pciefd->can_count]); 720 } 721 722 /* probe for the entire device */ 723 static int peak_pciefd_probe(struct pci_dev *pdev, 724 const struct pci_device_id *ent) 725 { 726 struct pciefd_board *pciefd; 727 int err, can_count; 728 u16 sub_sys_id; 729 u8 hw_ver_major; 730 u8 hw_ver_minor; 731 u8 hw_ver_sub; 732 u32 v2; 733 734 err = pci_enable_device(pdev); 735 if (err) 736 return err; 737 err = pci_request_regions(pdev, PCIEFD_DRV_NAME); 738 if (err) 739 goto err_disable_pci; 740 741 /* the number of channels depends on sub-system id */ 742 err = pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sub_sys_id); 743 if (err) 744 goto err_release_regions; 745 746 dev_dbg(&pdev->dev, "probing device %04x:%04x:%04x\n", 747 pdev->vendor, pdev->device, sub_sys_id); 748 749 if (sub_sys_id >= 0x0012) 750 can_count = 4; 751 else if (sub_sys_id >= 0x0010) 752 can_count = 3; 753 else if (sub_sys_id >= 0x0004) 754 can_count = 2; 755 else 756 can_count = 1; 757 758 /* allocate board structure object */ 759 pciefd = devm_kzalloc(&pdev->dev, struct_size(pciefd, can, can_count), 760 GFP_KERNEL); 761 if (!pciefd) { 762 err = -ENOMEM; 763 goto err_release_regions; 764 } 765 766 /* initialize the board structure */ 767 pciefd->pci_dev = pdev; 768 spin_lock_init(&pciefd->cmd_lock); 769 770 /* save the PCI BAR0 virtual address for further system regs access */ 771 pciefd->reg_base = pci_iomap(pdev, 0, PCIEFD_BAR0_SIZE); 772 if (!pciefd->reg_base) { 773 dev_err(&pdev->dev, "failed to map PCI resource #0\n"); 774 err = -ENOMEM; 775 goto err_release_regions; 776 } 777 778 /* read the firmware version number */ 779 v2 = pciefd_sys_readreg(pciefd, PCIEFD_REG_SYS_VER2); 780 781 hw_ver_major = (v2 & 0x0000f000) >> 12; 782 hw_ver_minor = (v2 & 0x00000f00) >> 8; 783 hw_ver_sub = (v2 & 0x000000f0) >> 4; 784 785 dev_info(&pdev->dev, 786 "%ux CAN-FD PCAN-PCIe FPGA v%u.%u.%u:\n", can_count, 787 hw_ver_major, hw_ver_minor, hw_ver_sub); 788 789 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 790 /* FW < v3.3.0 DMA logic doesn't handle correctly the mix of 32-bit and 791 * 64-bit logical addresses: this workaround forces usage of 32-bit 792 * DMA addresses only when such a fw is detected. 793 */ 794 if (PCIEFD_FW_VERSION(hw_ver_major, hw_ver_minor, hw_ver_sub) < 795 PCIEFD_FW_VERSION(3, 3, 0)) { 796 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 797 if (err) 798 dev_warn(&pdev->dev, 799 "warning: can't set DMA mask %llxh (err %d)\n", 800 DMA_BIT_MASK(32), err); 801 } 802 #endif 803 804 /* stop system clock */ 805 pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_CLK_EN, 806 PCIEFD_REG_SYS_CTL_CLR); 807 808 pci_set_master(pdev); 809 810 /* create now the corresponding channels objects */ 811 while (pciefd->can_count < can_count) { 812 err = pciefd_can_probe(pciefd); 813 if (err) 814 goto err_free_canfd; 815 816 pciefd->can_count++; 817 } 818 819 /* set system timestamps counter in RST mode */ 820 pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_TS_RST, 821 PCIEFD_REG_SYS_CTL_SET); 822 823 /* wait a bit (read cycle) */ 824 (void)pciefd_sys_readreg(pciefd, PCIEFD_REG_SYS_VER1); 825 826 /* free all clocks */ 827 pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_TS_RST, 828 PCIEFD_REG_SYS_CTL_CLR); 829 830 /* start system clock */ 831 pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_CLK_EN, 832 PCIEFD_REG_SYS_CTL_SET); 833 834 /* remember the board structure address in the device user data */ 835 pci_set_drvdata(pdev, pciefd); 836 837 return 0; 838 839 err_free_canfd: 840 pciefd_can_remove_all(pciefd); 841 842 pci_iounmap(pdev, pciefd->reg_base); 843 844 err_release_regions: 845 pci_release_regions(pdev); 846 847 err_disable_pci: 848 pci_disable_device(pdev); 849 850 /* pci_xxx_config_word() return positive PCIBIOS_xxx error codes while 851 * the probe() function must return a negative errno in case of failure 852 * (err is unchanged if negative) */ 853 return pcibios_err_to_errno(err); 854 } 855 856 /* free the board structure object, as well as its resources: */ 857 static void peak_pciefd_remove(struct pci_dev *pdev) 858 { 859 struct pciefd_board *pciefd = pci_get_drvdata(pdev); 860 861 /* release CAN-FD channels resources */ 862 pciefd_can_remove_all(pciefd); 863 864 pci_iounmap(pdev, pciefd->reg_base); 865 866 pci_release_regions(pdev); 867 pci_disable_device(pdev); 868 } 869 870 static struct pci_driver peak_pciefd_driver = { 871 .name = PCIEFD_DRV_NAME, 872 .id_table = peak_pciefd_tbl, 873 .probe = peak_pciefd_probe, 874 .remove = peak_pciefd_remove, 875 }; 876 877 module_pci_driver(peak_pciefd_driver); 878