1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
4 * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
6 * Derived from the PCAN project file driver/src/pcan_pci.c:
8 * Copyright (C) 2001-2006 PEAK System-Technik GmbH
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/netdevice.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
18 #include <linux/can.h>
19 #include <linux/can/dev.h>
21 #include "peak_canfd_user.h"
23 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
24 MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCIe/M.2 FD family cards");
25 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCIe/M.2 FD CAN cards");
26 MODULE_LICENSE("GPL v2");
28 #define PCIEFD_DRV_NAME "peak_pciefd"
30 #define PEAK_PCI_VENDOR_ID 0x001c /* The PCI device and vendor IDs */
31 #define PEAK_PCIEFD_ID 0x0013 /* for PCIe slot cards */
32 #define PCAN_CPCIEFD_ID 0x0014 /* for Compact-PCI Serial slot cards */
33 #define PCAN_PCIE104FD_ID 0x0017 /* for PCIe-104 Express slot cards */
34 #define PCAN_MINIPCIEFD_ID 0x0018 /* for mini-PCIe slot cards */
35 #define PCAN_PCIEFD_OEM_ID 0x0019 /* for PCIe slot OEM cards */
36 #define PCAN_M2_ID 0x001a /* for M2 slot cards */
38 /* PEAK PCIe board access description */
39 #define PCIEFD_BAR0_SIZE (64 * 1024)
40 #define PCIEFD_RX_DMA_SIZE (4 * 1024)
41 #define PCIEFD_TX_DMA_SIZE (4 * 1024)
43 #define PCIEFD_TX_PAGE_SIZE (2 * 1024)
45 /* System Control Registers */
46 #define PCIEFD_REG_SYS_CTL_SET 0x0000 /* set bits */
47 #define PCIEFD_REG_SYS_CTL_CLR 0x0004 /* clear bits */
49 /* Version info registers */
50 #define PCIEFD_REG_SYS_VER1 0x0040 /* version reg #1 */
51 #define PCIEFD_REG_SYS_VER2 0x0044 /* version reg #2 */
53 #define PCIEFD_FW_VERSION(x, y, z) (((u32)(x) << 24) | \
57 /* System Control Registers Bits */
58 #define PCIEFD_SYS_CTL_TS_RST 0x00000001 /* timestamp clock */
59 #define PCIEFD_SYS_CTL_CLK_EN 0x00000002 /* system clock */
61 /* CAN-FD channel addresses */
62 #define PCIEFD_CANX_OFF(c) (((c) + 1) * 0x1000)
64 #define PCIEFD_ECHO_SKB_MAX PCANFD_ECHO_SKB_DEF
66 /* CAN-FD channel registers */
67 #define PCIEFD_REG_CAN_MISC 0x0000 /* Misc. control */
68 #define PCIEFD_REG_CAN_CLK_SEL 0x0008 /* Clock selector */
69 #define PCIEFD_REG_CAN_CMD_PORT_L 0x0010 /* 64-bits command port */
70 #define PCIEFD_REG_CAN_CMD_PORT_H 0x0014
71 #define PCIEFD_REG_CAN_TX_REQ_ACC 0x0020 /* Tx request accumulator */
72 #define PCIEFD_REG_CAN_TX_CTL_SET 0x0030 /* Tx control set register */
73 #define PCIEFD_REG_CAN_TX_CTL_CLR 0x0038 /* Tx control clear register */
74 #define PCIEFD_REG_CAN_TX_DMA_ADDR_L 0x0040 /* 64-bits addr for Tx DMA */
75 #define PCIEFD_REG_CAN_TX_DMA_ADDR_H 0x0044
76 #define PCIEFD_REG_CAN_RX_CTL_SET 0x0050 /* Rx control set register */
77 #define PCIEFD_REG_CAN_RX_CTL_CLR 0x0058 /* Rx control clear register */
78 #define PCIEFD_REG_CAN_RX_CTL_WRT 0x0060 /* Rx control write register */
79 #define PCIEFD_REG_CAN_RX_CTL_ACK 0x0068 /* Rx control ACK register */
80 #define PCIEFD_REG_CAN_RX_DMA_ADDR_L 0x0070 /* 64-bits addr for Rx DMA */
81 #define PCIEFD_REG_CAN_RX_DMA_ADDR_H 0x0074
83 /* CAN-FD channel misc register bits */
84 #define CANFD_MISC_TS_RST 0x00000001 /* timestamp cnt rst */
86 /* CAN-FD channel Clock SELector Source & DIVider */
87 #define CANFD_CLK_SEL_DIV_MASK 0x00000007
88 #define CANFD_CLK_SEL_DIV_60MHZ 0x00000000 /* SRC=240MHz only */
89 #define CANFD_CLK_SEL_DIV_40MHZ 0x00000001 /* SRC=240MHz only */
90 #define CANFD_CLK_SEL_DIV_30MHZ 0x00000002 /* SRC=240MHz only */
91 #define CANFD_CLK_SEL_DIV_24MHZ 0x00000003 /* SRC=240MHz only */
92 #define CANFD_CLK_SEL_DIV_20MHZ 0x00000004 /* SRC=240MHz only */
94 #define CANFD_CLK_SEL_SRC_MASK 0x00000008 /* 0=80MHz, 1=240MHz */
95 #define CANFD_CLK_SEL_SRC_240MHZ 0x00000008
96 #define CANFD_CLK_SEL_SRC_80MHZ (~CANFD_CLK_SEL_SRC_240MHZ & \
97 CANFD_CLK_SEL_SRC_MASK)
99 #define CANFD_CLK_SEL_20MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
100 CANFD_CLK_SEL_DIV_20MHZ)
101 #define CANFD_CLK_SEL_24MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
102 CANFD_CLK_SEL_DIV_24MHZ)
103 #define CANFD_CLK_SEL_30MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
104 CANFD_CLK_SEL_DIV_30MHZ)
105 #define CANFD_CLK_SEL_40MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
106 CANFD_CLK_SEL_DIV_40MHZ)
107 #define CANFD_CLK_SEL_60MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
108 CANFD_CLK_SEL_DIV_60MHZ)
109 #define CANFD_CLK_SEL_80MHZ (CANFD_CLK_SEL_SRC_80MHZ)
111 /* CAN-FD channel Rx/Tx control register bits */
112 #define CANFD_CTL_UNC_BIT 0x00010000 /* Uncached DMA mem */
113 #define CANFD_CTL_RST_BIT 0x00020000 /* reset DMA action */
114 #define CANFD_CTL_IEN_BIT 0x00040000 /* IRQ enable */
116 /* Rx IRQ Count and Time Limits */
117 #define CANFD_CTL_IRQ_CL_DEF 16 /* Rx msg max nb per IRQ in Rx DMA */
118 #define CANFD_CTL_IRQ_TL_DEF 10 /* Time before IRQ if < CL (x100 µs) */
120 #define CANFD_OPTIONS_SET (CANFD_OPTION_ERROR | CANFD_OPTION_BUSLOAD)
122 /* Tx anticipation window (link logical address should be aligned on 2K
125 #define PCIEFD_TX_PAGE_COUNT (PCIEFD_TX_DMA_SIZE / PCIEFD_TX_PAGE_SIZE)
127 #define CANFD_MSG_LNK_TX 0x1001 /* Tx msgs link */
129 /* 32-bits IRQ status fields, heading Rx DMA area */
130 static inline int pciefd_irq_tag(u32 irq_status
)
132 return irq_status
& 0x0000000f;
135 static inline int pciefd_irq_rx_cnt(u32 irq_status
)
137 return (irq_status
& 0x000007f0) >> 4;
140 static inline int pciefd_irq_is_lnk(u32 irq_status
)
142 return irq_status
& 0x00010000;
146 struct pciefd_rx_dma
{
149 __le32 sys_time_high
;
150 struct pucan_rx_msg msg
[0];
151 } __packed
__aligned(4);
154 struct pciefd_tx_link
{
159 } __packed
__aligned(4);
161 /* Tx page descriptor */
163 void *vbase
; /* page virtual address */
164 dma_addr_t lbase
; /* page logical address */
169 /* CAN-FD channel object */
172 struct peak_canfd_priv ucan
; /* must be the first member */
173 void __iomem
*reg_base
; /* channel config base addr */
174 struct pciefd_board
*board
; /* reverse link */
176 struct pucan_command pucan_cmd
; /* command buffer */
178 dma_addr_t rx_dma_laddr
; /* DMA virtual and logical addr */
179 void *rx_dma_vaddr
; /* for Rx and Tx areas */
180 dma_addr_t tx_dma_laddr
;
183 struct pciefd_page tx_pages
[PCIEFD_TX_PAGE_COUNT
];
184 u16 tx_pages_free
; /* free Tx pages counter */
185 u16 tx_page_index
; /* current page used for Tx */
189 u32 irq_tag
; /* next irq tag */
192 /* PEAK-PCIe FD board object */
193 struct pciefd_board
{
194 void __iomem
*reg_base
;
195 struct pci_dev
*pci_dev
;
197 spinlock_t cmd_lock
; /* 64-bits cmds must be atomic */
198 struct pciefd_can
*can
[0]; /* array of network devices */
201 /* supported device ids. */
202 static const struct pci_device_id peak_pciefd_tbl
[] = {
203 {PEAK_PCI_VENDOR_ID
, PEAK_PCIEFD_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
204 {PEAK_PCI_VENDOR_ID
, PCAN_CPCIEFD_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
205 {PEAK_PCI_VENDOR_ID
, PCAN_PCIE104FD_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
206 {PEAK_PCI_VENDOR_ID
, PCAN_MINIPCIEFD_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
207 {PEAK_PCI_VENDOR_ID
, PCAN_PCIEFD_OEM_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
208 {PEAK_PCI_VENDOR_ID
, PCAN_M2_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
212 MODULE_DEVICE_TABLE(pci
, peak_pciefd_tbl
);
214 /* read a 32 bits value from a SYS block register */
215 static inline u32
pciefd_sys_readreg(const struct pciefd_board
*priv
, u16 reg
)
217 return readl(priv
->reg_base
+ reg
);
220 /* write a 32 bits value into a SYS block register */
221 static inline void pciefd_sys_writereg(const struct pciefd_board
*priv
,
224 writel(val
, priv
->reg_base
+ reg
);
227 /* read a 32 bits value from CAN-FD block register */
228 static inline u32
pciefd_can_readreg(const struct pciefd_can
*priv
, u16 reg
)
230 return readl(priv
->reg_base
+ reg
);
233 /* write a 32 bits value into a CAN-FD block register */
234 static inline void pciefd_can_writereg(const struct pciefd_can
*priv
,
237 writel(val
, priv
->reg_base
+ reg
);
240 /* give a channel logical Rx DMA address to the board */
241 static void pciefd_can_setup_rx_dma(struct pciefd_can
*priv
)
243 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
244 const u32 dma_addr_h
= (u32
)(priv
->rx_dma_laddr
>> 32);
246 const u32 dma_addr_h
= 0;
249 /* (DMA must be reset for Rx) */
250 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
, PCIEFD_REG_CAN_RX_CTL_SET
);
252 /* write the logical address of the Rx DMA area for this channel */
253 pciefd_can_writereg(priv
, (u32
)priv
->rx_dma_laddr
,
254 PCIEFD_REG_CAN_RX_DMA_ADDR_L
);
255 pciefd_can_writereg(priv
, dma_addr_h
, PCIEFD_REG_CAN_RX_DMA_ADDR_H
);
257 /* also indicates that Rx DMA is cacheable */
258 pciefd_can_writereg(priv
, CANFD_CTL_UNC_BIT
, PCIEFD_REG_CAN_RX_CTL_CLR
);
261 /* clear channel logical Rx DMA address from the board */
262 static void pciefd_can_clear_rx_dma(struct pciefd_can
*priv
)
264 /* DMA must be reset for Rx */
265 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
, PCIEFD_REG_CAN_RX_CTL_SET
);
267 /* clear the logical address of the Rx DMA area for this channel */
268 pciefd_can_writereg(priv
, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_L
);
269 pciefd_can_writereg(priv
, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_H
);
272 /* give a channel logical Tx DMA address to the board */
273 static void pciefd_can_setup_tx_dma(struct pciefd_can
*priv
)
275 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
276 const u32 dma_addr_h
= (u32
)(priv
->tx_dma_laddr
>> 32);
278 const u32 dma_addr_h
= 0;
281 /* (DMA must be reset for Tx) */
282 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
, PCIEFD_REG_CAN_TX_CTL_SET
);
284 /* write the logical address of the Tx DMA area for this channel */
285 pciefd_can_writereg(priv
, (u32
)priv
->tx_dma_laddr
,
286 PCIEFD_REG_CAN_TX_DMA_ADDR_L
);
287 pciefd_can_writereg(priv
, dma_addr_h
, PCIEFD_REG_CAN_TX_DMA_ADDR_H
);
289 /* also indicates that Tx DMA is cacheable */
290 pciefd_can_writereg(priv
, CANFD_CTL_UNC_BIT
, PCIEFD_REG_CAN_TX_CTL_CLR
);
293 /* clear channel logical Tx DMA address from the board */
294 static void pciefd_can_clear_tx_dma(struct pciefd_can
*priv
)
296 /* DMA must be reset for Tx */
297 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
, PCIEFD_REG_CAN_TX_CTL_SET
);
299 /* clear the logical address of the Tx DMA area for this channel */
300 pciefd_can_writereg(priv
, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_L
);
301 pciefd_can_writereg(priv
, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_H
);
304 static void pciefd_can_ack_rx_dma(struct pciefd_can
*priv
)
306 /* read value of current IRQ tag and inc it for next one */
307 priv
->irq_tag
= le32_to_cpu(*(__le32
*)priv
->rx_dma_vaddr
);
309 priv
->irq_tag
&= 0xf;
311 /* write the next IRQ tag for this CAN */
312 pciefd_can_writereg(priv
, priv
->irq_tag
, PCIEFD_REG_CAN_RX_CTL_ACK
);
316 static irqreturn_t
pciefd_irq_handler(int irq
, void *arg
)
318 struct pciefd_can
*priv
= arg
;
319 struct pciefd_rx_dma
*rx_dma
= priv
->rx_dma_vaddr
;
321 /* INTA mode only to sync with PCIe transaction */
322 if (!pci_dev_msi_enabled(priv
->board
->pci_dev
))
323 (void)pciefd_sys_readreg(priv
->board
, PCIEFD_REG_SYS_VER1
);
325 /* read IRQ status from the first 32-bits of the Rx DMA area */
326 priv
->irq_status
= le32_to_cpu(rx_dma
->irq_status
);
328 /* check if this (shared) IRQ is for this CAN */
329 if (pciefd_irq_tag(priv
->irq_status
) != priv
->irq_tag
)
332 /* handle rx messages (if any) */
333 peak_canfd_handle_msgs_list(&priv
->ucan
,
335 pciefd_irq_rx_cnt(priv
->irq_status
));
337 /* handle tx link interrupt (if any) */
338 if (pciefd_irq_is_lnk(priv
->irq_status
)) {
341 spin_lock_irqsave(&priv
->tx_lock
, flags
);
342 priv
->tx_pages_free
++;
343 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
345 /* wake producer up (only if enough room in echo_skb array) */
346 spin_lock_irqsave(&priv
->ucan
.echo_lock
, flags
);
347 if (!priv
->ucan
.can
.echo_skb
[priv
->ucan
.echo_idx
])
348 netif_wake_queue(priv
->ucan
.ndev
);
350 spin_unlock_irqrestore(&priv
->ucan
.echo_lock
, flags
);
353 /* re-enable Rx DMA transfer for this CAN */
354 pciefd_can_ack_rx_dma(priv
);
359 static int pciefd_enable_tx_path(struct peak_canfd_priv
*ucan
)
361 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
364 /* initialize the Tx pages descriptors */
365 priv
->tx_pages_free
= PCIEFD_TX_PAGE_COUNT
- 1;
366 priv
->tx_page_index
= 0;
368 priv
->tx_pages
[0].vbase
= priv
->tx_dma_vaddr
;
369 priv
->tx_pages
[0].lbase
= priv
->tx_dma_laddr
;
371 for (i
= 0; i
< PCIEFD_TX_PAGE_COUNT
; i
++) {
372 priv
->tx_pages
[i
].offset
= 0;
373 priv
->tx_pages
[i
].size
= PCIEFD_TX_PAGE_SIZE
-
374 sizeof(struct pciefd_tx_link
);
376 priv
->tx_pages
[i
].vbase
=
377 priv
->tx_pages
[i
- 1].vbase
+
379 priv
->tx_pages
[i
].lbase
=
380 priv
->tx_pages
[i
- 1].lbase
+
385 /* setup Tx DMA addresses into IP core */
386 pciefd_can_setup_tx_dma(priv
);
388 /* start (TX_RST=0) Tx Path */
389 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
, PCIEFD_REG_CAN_TX_CTL_CLR
);
394 /* board specific CANFD command pre-processing */
395 static int pciefd_pre_cmd(struct peak_canfd_priv
*ucan
)
397 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
398 u16 cmd
= pucan_cmd_get_opcode(&priv
->pucan_cmd
);
401 /* pre-process command */
403 case PUCAN_CMD_NORMAL_MODE
:
404 case PUCAN_CMD_LISTEN_ONLY_MODE
:
406 if (ucan
->can
.state
== CAN_STATE_BUS_OFF
)
409 /* going into operational mode: setup IRQ handler */
410 err
= request_irq(priv
->ucan
.ndev
->irq
,
418 /* setup Rx DMA address */
419 pciefd_can_setup_rx_dma(priv
);
421 /* setup max count of msgs per IRQ */
422 pciefd_can_writereg(priv
, (CANFD_CTL_IRQ_TL_DEF
) << 8 |
423 CANFD_CTL_IRQ_CL_DEF
,
424 PCIEFD_REG_CAN_RX_CTL_WRT
);
426 /* clear DMA RST for Rx (Rx start) */
427 pciefd_can_writereg(priv
, CANFD_CTL_RST_BIT
,
428 PCIEFD_REG_CAN_RX_CTL_CLR
);
430 /* reset timestamps */
431 pciefd_can_writereg(priv
, !CANFD_MISC_TS_RST
,
432 PCIEFD_REG_CAN_MISC
);
434 /* do an initial ACK */
435 pciefd_can_ack_rx_dma(priv
);
437 /* enable IRQ for this CAN after having set next irq_tag */
438 pciefd_can_writereg(priv
, CANFD_CTL_IEN_BIT
,
439 PCIEFD_REG_CAN_RX_CTL_SET
);
441 /* Tx path will be setup as soon as RX_BARRIER is received */
450 /* write a command */
451 static int pciefd_write_cmd(struct peak_canfd_priv
*ucan
)
453 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
456 /* 64-bits command is atomic */
457 spin_lock_irqsave(&priv
->board
->cmd_lock
, flags
);
459 pciefd_can_writereg(priv
, *(u32
*)ucan
->cmd_buffer
,
460 PCIEFD_REG_CAN_CMD_PORT_L
);
461 pciefd_can_writereg(priv
, *(u32
*)(ucan
->cmd_buffer
+ 4),
462 PCIEFD_REG_CAN_CMD_PORT_H
);
464 spin_unlock_irqrestore(&priv
->board
->cmd_lock
, flags
);
469 /* board specific CANFD command post-processing */
470 static int pciefd_post_cmd(struct peak_canfd_priv
*ucan
)
472 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
473 u16 cmd
= pucan_cmd_get_opcode(&priv
->pucan_cmd
);
476 case PUCAN_CMD_RESET_MODE
:
478 if (ucan
->can
.state
== CAN_STATE_STOPPED
)
481 /* controller now in reset mode: */
483 /* disable IRQ for this CAN */
484 pciefd_can_writereg(priv
, CANFD_CTL_IEN_BIT
,
485 PCIEFD_REG_CAN_RX_CTL_CLR
);
487 /* stop and reset DMA addresses in Tx/Rx engines */
488 pciefd_can_clear_tx_dma(priv
);
489 pciefd_can_clear_rx_dma(priv
);
491 /* wait for above commands to complete (read cycle) */
492 (void)pciefd_sys_readreg(priv
->board
, PCIEFD_REG_SYS_VER1
);
494 free_irq(priv
->ucan
.ndev
->irq
, priv
);
496 ucan
->can
.state
= CAN_STATE_STOPPED
;
504 static void *pciefd_alloc_tx_msg(struct peak_canfd_priv
*ucan
, u16 msg_size
,
507 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
508 struct pciefd_page
*page
= priv
->tx_pages
+ priv
->tx_page_index
;
512 spin_lock_irqsave(&priv
->tx_lock
, flags
);
514 if (page
->offset
+ msg_size
> page
->size
) {
515 struct pciefd_tx_link
*lk
;
517 /* not enough space in this page: try another one */
518 if (!priv
->tx_pages_free
) {
519 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
525 priv
->tx_pages_free
--;
527 /* keep address of the very last free slot of current page */
528 lk
= page
->vbase
+ page
->offset
;
530 /* next, move on a new free page */
531 priv
->tx_page_index
= (priv
->tx_page_index
+ 1) %
532 PCIEFD_TX_PAGE_COUNT
;
533 page
= priv
->tx_pages
+ priv
->tx_page_index
;
535 /* put link record to this new page at the end of prev one */
536 lk
->size
= cpu_to_le16(sizeof(*lk
));
537 lk
->type
= cpu_to_le16(CANFD_MSG_LNK_TX
);
538 lk
->laddr_lo
= cpu_to_le32(page
->lbase
);
540 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
541 lk
->laddr_hi
= cpu_to_le32(page
->lbase
>> 32);
545 /* next msgs will be put from the begininng of this new page */
549 *room_left
= priv
->tx_pages_free
* page
->size
;
551 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
553 msg
= page
->vbase
+ page
->offset
;
555 /* give back room left in the tx ring */
556 *room_left
+= page
->size
- (page
->offset
+ msg_size
);
561 static int pciefd_write_tx_msg(struct peak_canfd_priv
*ucan
,
562 struct pucan_tx_msg
*msg
)
564 struct pciefd_can
*priv
= (struct pciefd_can
*)ucan
;
565 struct pciefd_page
*page
= priv
->tx_pages
+ priv
->tx_page_index
;
567 /* this slot is now reserved for writing the frame */
568 page
->offset
+= le16_to_cpu(msg
->size
);
570 /* tell the board a frame has been written in Tx DMA area */
571 pciefd_can_writereg(priv
, 1, PCIEFD_REG_CAN_TX_REQ_ACC
);
576 /* probe for CAN-FD channel #pciefd_board->can_count */
577 static int pciefd_can_probe(struct pciefd_board
*pciefd
)
579 struct net_device
*ndev
;
580 struct pciefd_can
*priv
;
584 /* allocate the candev object with default isize of echo skbs ring */
585 ndev
= alloc_peak_canfd_dev(sizeof(*priv
), pciefd
->can_count
,
586 PCIEFD_ECHO_SKB_MAX
);
588 dev_err(&pciefd
->pci_dev
->dev
,
589 "failed to alloc candev object\n");
593 priv
= netdev_priv(ndev
);
595 /* fill-in candev private object: */
597 /* setup PCIe-FD own callbacks */
598 priv
->ucan
.pre_cmd
= pciefd_pre_cmd
;
599 priv
->ucan
.write_cmd
= pciefd_write_cmd
;
600 priv
->ucan
.post_cmd
= pciefd_post_cmd
;
601 priv
->ucan
.enable_tx_path
= pciefd_enable_tx_path
;
602 priv
->ucan
.alloc_tx_msg
= pciefd_alloc_tx_msg
;
603 priv
->ucan
.write_tx_msg
= pciefd_write_tx_msg
;
605 /* setup PCIe-FD own command buffer */
606 priv
->ucan
.cmd_buffer
= &priv
->pucan_cmd
;
607 priv
->ucan
.cmd_maxlen
= sizeof(priv
->pucan_cmd
);
609 priv
->board
= pciefd
;
611 /* CAN config regs block address */
612 priv
->reg_base
= pciefd
->reg_base
+ PCIEFD_CANX_OFF(priv
->ucan
.index
);
614 /* allocate non-cacheable DMA'able 4KB memory area for Rx */
615 priv
->rx_dma_vaddr
= dmam_alloc_coherent(&pciefd
->pci_dev
->dev
,
619 if (!priv
->rx_dma_vaddr
) {
620 dev_err(&pciefd
->pci_dev
->dev
,
621 "Rx dmam_alloc_coherent(%u) failure\n",
623 goto err_free_candev
;
626 /* allocate non-cacheable DMA'able 4KB memory area for Tx */
627 priv
->tx_dma_vaddr
= dmam_alloc_coherent(&pciefd
->pci_dev
->dev
,
631 if (!priv
->tx_dma_vaddr
) {
632 dev_err(&pciefd
->pci_dev
->dev
,
633 "Tx dmam_alloc_coherent(%u) failure\n",
635 goto err_free_candev
;
638 /* CAN clock in RST mode */
639 pciefd_can_writereg(priv
, CANFD_MISC_TS_RST
, PCIEFD_REG_CAN_MISC
);
641 /* read current clock value */
642 clk
= pciefd_can_readreg(priv
, PCIEFD_REG_CAN_CLK_SEL
);
644 case CANFD_CLK_SEL_20MHZ
:
645 priv
->ucan
.can
.clock
.freq
= 20 * 1000 * 1000;
647 case CANFD_CLK_SEL_24MHZ
:
648 priv
->ucan
.can
.clock
.freq
= 24 * 1000 * 1000;
650 case CANFD_CLK_SEL_30MHZ
:
651 priv
->ucan
.can
.clock
.freq
= 30 * 1000 * 1000;
653 case CANFD_CLK_SEL_40MHZ
:
654 priv
->ucan
.can
.clock
.freq
= 40 * 1000 * 1000;
656 case CANFD_CLK_SEL_60MHZ
:
657 priv
->ucan
.can
.clock
.freq
= 60 * 1000 * 1000;
660 pciefd_can_writereg(priv
, CANFD_CLK_SEL_80MHZ
,
661 PCIEFD_REG_CAN_CLK_SEL
);
664 case CANFD_CLK_SEL_80MHZ
:
665 priv
->ucan
.can
.clock
.freq
= 80 * 1000 * 1000;
669 ndev
->irq
= pciefd
->pci_dev
->irq
;
671 SET_NETDEV_DEV(ndev
, &pciefd
->pci_dev
->dev
);
673 err
= register_candev(ndev
);
675 dev_err(&pciefd
->pci_dev
->dev
,
676 "couldn't register CAN device: %d\n", err
);
677 goto err_free_candev
;
680 spin_lock_init(&priv
->tx_lock
);
682 /* save the object address in the board structure */
683 pciefd
->can
[pciefd
->can_count
] = priv
;
685 dev_info(&pciefd
->pci_dev
->dev
, "%s at reg_base=0x%p irq=%d\n",
686 ndev
->name
, priv
->reg_base
, ndev
->irq
);
697 /* remove a CAN-FD channel by releasing all of its resources */
698 static void pciefd_can_remove(struct pciefd_can
*priv
)
700 /* unregister (close) the can device to go back to RST mode first */
701 unregister_candev(priv
->ucan
.ndev
);
703 /* finally, free the candev object */
704 free_candev(priv
->ucan
.ndev
);
707 /* remove all CAN-FD channels by releasing their own resources */
708 static void pciefd_can_remove_all(struct pciefd_board
*pciefd
)
710 while (pciefd
->can_count
> 0)
711 pciefd_can_remove(pciefd
->can
[--pciefd
->can_count
]);
714 /* probe for the entire device */
715 static int peak_pciefd_probe(struct pci_dev
*pdev
,
716 const struct pci_device_id
*ent
)
718 struct pciefd_board
*pciefd
;
726 err
= pci_enable_device(pdev
);
729 err
= pci_request_regions(pdev
, PCIEFD_DRV_NAME
);
731 goto err_disable_pci
;
733 /* the number of channels depends on sub-system id */
734 err
= pci_read_config_word(pdev
, PCI_SUBSYSTEM_ID
, &sub_sys_id
);
736 goto err_release_regions
;
738 dev_dbg(&pdev
->dev
, "probing device %04x:%04x:%04x\n",
739 pdev
->vendor
, pdev
->device
, sub_sys_id
);
741 if (sub_sys_id
>= 0x0012)
743 else if (sub_sys_id
>= 0x0010)
745 else if (sub_sys_id
>= 0x0004)
750 /* allocate board structure object */
751 pciefd
= devm_kzalloc(&pdev
->dev
, struct_size(pciefd
, can
, can_count
),
755 goto err_release_regions
;
758 /* initialize the board structure */
759 pciefd
->pci_dev
= pdev
;
760 spin_lock_init(&pciefd
->cmd_lock
);
762 /* save the PCI BAR0 virtual address for further system regs access */
763 pciefd
->reg_base
= pci_iomap(pdev
, 0, PCIEFD_BAR0_SIZE
);
764 if (!pciefd
->reg_base
) {
765 dev_err(&pdev
->dev
, "failed to map PCI resource #0\n");
767 goto err_release_regions
;
770 /* read the firmware version number */
771 v2
= pciefd_sys_readreg(pciefd
, PCIEFD_REG_SYS_VER2
);
773 hw_ver_major
= (v2
& 0x0000f000) >> 12;
774 hw_ver_minor
= (v2
& 0x00000f00) >> 8;
775 hw_ver_sub
= (v2
& 0x000000f0) >> 4;
778 "%ux CAN-FD PCAN-PCIe FPGA v%u.%u.%u:\n", can_count
,
779 hw_ver_major
, hw_ver_minor
, hw_ver_sub
);
781 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
782 /* FW < v3.3.0 DMA logic doesn't handle correctly the mix of 32-bit and
783 * 64-bit logical addresses: this workaround forces usage of 32-bit
784 * DMA addresses only when such a fw is detected.
786 if (PCIEFD_FW_VERSION(hw_ver_major
, hw_ver_minor
, hw_ver_sub
) <
787 PCIEFD_FW_VERSION(3, 3, 0)) {
788 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
791 "warning: can't set DMA mask %llxh (err %d)\n",
792 DMA_BIT_MASK(32), err
);
796 /* stop system clock */
797 pciefd_sys_writereg(pciefd
, PCIEFD_SYS_CTL_CLK_EN
,
798 PCIEFD_REG_SYS_CTL_CLR
);
800 pci_set_master(pdev
);
802 /* create now the corresponding channels objects */
803 while (pciefd
->can_count
< can_count
) {
804 err
= pciefd_can_probe(pciefd
);
811 /* set system timestamps counter in RST mode */
812 pciefd_sys_writereg(pciefd
, PCIEFD_SYS_CTL_TS_RST
,
813 PCIEFD_REG_SYS_CTL_SET
);
815 /* wait a bit (read cycle) */
816 (void)pciefd_sys_readreg(pciefd
, PCIEFD_REG_SYS_VER1
);
818 /* free all clocks */
819 pciefd_sys_writereg(pciefd
, PCIEFD_SYS_CTL_TS_RST
,
820 PCIEFD_REG_SYS_CTL_CLR
);
822 /* start system clock */
823 pciefd_sys_writereg(pciefd
, PCIEFD_SYS_CTL_CLK_EN
,
824 PCIEFD_REG_SYS_CTL_SET
);
826 /* remember the board structure address in the device user data */
827 pci_set_drvdata(pdev
, pciefd
);
832 pciefd_can_remove_all(pciefd
);
834 pci_iounmap(pdev
, pciefd
->reg_base
);
837 pci_release_regions(pdev
);
840 pci_disable_device(pdev
);
842 /* pci_xxx_config_word() return positive PCIBIOS_xxx error codes while
843 * the probe() function must return a negative errno in case of failure
844 * (err is unchanged if negative) */
845 return pcibios_err_to_errno(err
);
848 /* free the board structure object, as well as its resources: */
849 static void peak_pciefd_remove(struct pci_dev
*pdev
)
851 struct pciefd_board
*pciefd
= pci_get_drvdata(pdev
);
853 /* release CAN-FD channels resources */
854 pciefd_can_remove_all(pciefd
);
856 pci_iounmap(pdev
, pciefd
->reg_base
);
858 pci_release_regions(pdev
);
859 pci_disable_device(pdev
);
862 static struct pci_driver peak_pciefd_driver
= {
863 .name
= PCIEFD_DRV_NAME
,
864 .id_table
= peak_pciefd_tbl
,
865 .probe
= peak_pciefd_probe
,
866 .remove
= peak_pciefd_remove
,
869 module_pci_driver(peak_pciefd_driver
);