Microchip mcp25xxfd can controller driver
Subsystems (2)
v6 cover Microchip mcp25xxfd can controller driver
From: Martin Sperl <[email protected]>
This patchset adds a driver for the mcp25xxfd CanFD controller.
Most of the features of the controller are supported by this driver release.
The controller includes a few features that the current core Can(FD) implementation does not support: * Transmit Bandwidth Sharing bits
- this waits for a configurable number of syncronization bits before atempting to transmit the next frame - for the moment controllable via a module parameter
* SID11 with CanFD frames
- at this moment not supported by driver
* 3 transmit attempts in addition to ONE_SHOT
* transmitter delay compensation configurations
* micro second exact transmission and reception timestamps
- used internally by driver
* variable number of tx-fifos
- exposed via module parameter at this moment
The driver driver has been split into several patches - first just initial setup of each section and then driver optimization patches.
As requested the driver has now also been split out into smaller files for individual components of the driver - in the hope that this has not been producing regressions...
The driver is able to handle reception of 99.95% of all CAN frames of a 100% saturated 1MHz Can2.0 Bus with Frames with standard IDs and DLC=0 on a Raspberry Pi 3. Note that this statistics is without injection into the network stack, which then drops about 60% of all frames.
At this time the SPI bus is utilized 75% (when counting CS asserted) and 60% from a SPI-Clock perspective.
On the Rasberry pi 3 in such a situation we are using 100% of one CPU for the interrupt thread (as the spi driver is using polling for short transfers) and this has exposed a performance inefficiency in the spi framework that shows as the the spi-worker thread also consuming CPU cycles (30% of one CPU in this test case) unnecessarily.
The driver also allows to use the INT0/1 pins as GPIOs. Lots of internal data/statistics is exposed via debugfs.
On the HW side some errata related to SRAM access have been found during the development of the patchsets. Since then an errata for the controller has been shared by microchip.
At this very moment there is no 100% mitigation for this (besides a newer version of the silicon).
The only instance where this is probably not an issue is when using a SPI bus driver that reliably deasserts CS in the "requested" time-frame of a few Can clock cycles.
When the spi-bus-driver is using GPIO-CS (which is nowadays the preferred implementation of spi-bus-drivers) then we have to rely on interrupt and interrupt worker thread scheduling latencies to be fast enough to fullfill the requirements of the mcp2517fd of SCK stop to CS-deassert. To achive this a faster CPU is needed and preferably more cores.
A lot of possible mitigations have been investigated (Fifo ordering in SRAM, the use of CRC SPI commands,...), but none have been successful. That is why we are only able to reach 99.95% rate, because when this issue occurres then there is the need to reconfigure the controller and that takes some time which can not get used to process frames. Fortunately this only happens on highly congested systems, but it is more likely to occur on low end systems.
So as an example almost everything related to can-transmission can get found in mcp25xxfd_can_tx.c. mcp25xxfd_can_rx.c is responsible for can-reception and mcp25xxfd_can_int.c for interrupt handling.
This also reduces the size of the individual patches to below 128k so that the linux-can mailing-list should accepts the patch.
Changelog:
V1 -> V2: new more generic name based on feedback from microchip
cleanup of code (checkpatch)
address all feedback on code
handle (HW) systemerrors in a much better/reliable way cleanup of dt custom properties removing (most) gpio
related properties
implemented GPIOLIB support as per feedback
V2 -> V3: added vendor-prefix for gpio-opendrain to dt-binding
added gpio-controller to dt-binding
added feedback by Rob Herring
waited for other feedback regarding the code
V3 -> V4: resend
Patch-1 (dt) added: Reviewed-by: Rob Herring <[email protected]>
V4 -> V5: reorganisation of the patchset into smaller patches
review of the whole driver code for better modularization
V5 -> V6: Major refactoring as per feedback from Wilhelm Grandegger
Fixing bugs reported by several other parties Split out of optimizations into separate patches
Martin Sperl (9):
dt-binding: can: mcp25xxfd: document device tree bindings
can: mcp25xxfd: Add Microchip mcp25xxfd CAN FD driver basics
can: mcp25xxfd: add gpiolib support for GPIO0/1 (aka. INT0/INT1)
can: mcp25xxfd: Add Microchip mcp25xxfd CAN FD driver
can: mcp25xxfd: optimize TEF read avoiding unnecessary SPI transfers
can: mcp25xxfd: optimize TEF reads reading multiple TEFs in one go
can: mcp25xxfd: optimize SPI reads of FIFOs in can2.0 mode
can: mcp25xxfd: add prediction of CanFD frames sizes based on history
can: mcp25xxfd: optimize reception of big CanFD frame reception with
BRS
.../bindings/net/can/microchip,mcp25xxfd.txt | 32 + drivers/net/can/spi/Kconfig | 2 + drivers/net/can/spi/Makefile | 2 + drivers/net/can/spi/mcp25xxfd/Kconfig | 5 + drivers/net/can/spi/mcp25xxfd/Makefile | 18 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c | 281 ++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.h | 14 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c | 684 ++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h | 56 ++ .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c | 235 ++++++ .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.h | 44 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.c | 347 +++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.h | 16 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_id.h | 69 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.c | 705 ++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.h | 17 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 203 ++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c | 521 ++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.h | 18 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c | 794 +++++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.h | 86 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.c | 485 +++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.h | 28 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.c | 312 ++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.h | 84 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.c | 31 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.h | 15 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.c | 110 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.h | 30 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.c | 75 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.h | 16 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c | 194 +++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.h | 16 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c | 73 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.h | 15 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h | 83 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h | 661 +++++++++++++++++ 37 files changed, 6377 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.txt create mode 100644 drivers/net/can/spi/mcp25xxfd/Kconfig create mode 100644 drivers/net/can/spi/mcp25xxfd/Makefile create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_id.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h
--
2.11.0
PATCH v6 1/9 dt-binding: can: mcp25xxfd: document device tree bindings
From: Martin Sperl <[email protected]>
Add device-tree bindings for Microcip CanFD Controller mcp2517fd
--
Changelog:
V1 -> V2: new more generic name based on feedback from microchip
cleanup of dt custom properties removing (most) gpio functions
V2 -> V3: added vendor-prefix for gpio-opendrain
s/_/-/
added gpio-controller
V3 -> V4: resend
added: Reviewed-by: Rob Herring <[email protected]>
---
.../bindings/net/can/microchip,mcp25xxfd.txt | 32 ++++++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.txtDocumentation/devicetree/bindings/net/can/microchip,mcp25xxfd.txt new file +32 -0
@@ -0,0 +1,32 @@ +* Microchip MCP2517 stand-alone CAN controller device tree bindings + +Required properties: + - compatible: Should be one of the following: + - "microchip,mcp2517fd" for MCP2517fd. + - reg: SPI chip select. + - clocks: The clock feeding the CAN controller. + - interrupt-parent: The parent interrupt controller. + - interrupts: Should contain IRQ line for the CAN controller. + - gpio-controller: Marks the device node as a GPIO controller + +Optional properties: + - vdd-supply: Regulator that powers the CAN controller. + - xceiver-supply: Regulator that powers the CAN transceiver. + - microchip,clock-out-div = <0|1|2|4|10>: Clock output pin divider + 0 = Start of Frame output + default: 10 + - microchip,clock-div2: bool: divide the internal clock by 2 + - microchip,gpio-open-drain: bool: enable open-drain for all pins + (except cantx) + +Example: + can0: can@1 { + compatible = "microchip,mcp2517fd"; + reg = <1>; + clocks = <&clk24m>; + interrupt-parent = <&gpio4>; + interrupts = <13 0x8>; + vdd-supply = <®5v0>; + xceiver-supply = <®5v0>; + gpio-controller; + };
--
2.11.0
PATCH v6 2/9 can: mcp25xxfd: Add Microchip mcp25xxfd CAN FD driver basics
In reply to [email protected]From: Martin Sperl <[email protected]>
This patch adds basic driver support for the Microchip mcp2517fd CAN-FD controller.
The mcp2517fd is capable of transmitting and receiving standard data frames, extended data frames, remote frames and Can-FD frames. The mcp2517fd interfaces with the host over SPI.
This patch iprovides basic driver functionality:
setting up clocks and the infrastructure for the can and gpio portions of the driver.
Datasheet:
* http://ww1.microchip.com/downloads/en/DeviceDoc/20005688A.pdf
Reference manual:
* http://ww1.microchip.com/downloads/en/DeviceDoc/20005678A.pdf
Errata:
* http://ww1.microchip.com/downloads/en/DeviceDoc/MCP2517FD-Silicon-Errata-and-Data-Sheet-Clarification-DS80000792A.pdf
---
Changelog:
V1 -> V2: implemented support as per feedback
V2 -> V3: moved dt-binding changes to patch 1
V3 -> V4: resend
V4 -> V5: reorganized driver into separate files
added more HW detection test
V5 -> V6: update copyright year
added extra message about spi bus signal quality fixed the use of spi buffer sources/destinations on stack implemented feedback by Wolfgang Gradegger restructured code into separate files
--- drivers/net/can/spi/Kconfig | 2 + drivers/net/can/spi/Makefile | 2 + drivers/net/can/spi/mcp25xxfd/Kconfig | 5 + drivers/net/can/spi/mcp25xxfd/Makefile | 12 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c | 260 +++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.h | 14 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c | 231 ++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h | 19 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.c | 485 ++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.h | 28 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.c | 312 ++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.h | 84 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.c | 31 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.h | 15 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.c | 110 ++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.h | 30 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.c | 75 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.h | 16 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c | 62 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.h | 15 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h | 79 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h | 656 ++++++++++++++++++++++ 22 files changed, 2543 insertions(+) create mode 100644 drivers/net/can/spi/mcp25xxfd/Kconfig create mode 100644 drivers/net/can/spi/mcp25xxfd/Makefile create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h
drivers/net/can/spi/Kconfig +2 -0
@@ -13,4 +13,6 @@ config CAN_MCP251X ---help--- Driver for the Microchip MCP251x SPI CAN controllers. +source "drivers/net/can/spi/mcp25xxfd/Kconfig" + endmenu
drivers/net/can/spi/Makefile +2 -0
@@ -5,3 +5,5 @@ obj-$(CONFIG_CAN_HI311X) += hi311x.o obj-$(CONFIG_CAN_MCP251X) += mcp251x.o + +obj-y += mcp25xxfd/
drivers/net/can/spi/mcp25xxfd/Kconfig new file +5 -0
@@ -0,0 +1,5 @@ +config CAN_MCP25XXFD + tristate "Microchip MCP25xxFD SPI CAN controllers" + depends on HAS_DMA + help + Driver for the Microchip MCP25XXFD SPI FD-CAN controller family.
drivers/net/can/spi/mcp25xxfd/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# +# Makefile for the Linux Controller Area Network SPI drivers. +# +obj-$(CONFIG_CAN_MCP25XXFD) += mcp25xxfd.o +mcp25xxfd-objs := mcp25xxfd_base.o +mcp25xxfd-objs += mcp25xxfd_can.o +mcp25xxfd-objs += mcp25xxfd_clock.o +mcp25xxfd-objs += mcp25xxfd_cmd.o +mcp25xxfd-objs += mcp25xxfd_crc.o +mcp25xxfd-objs += mcp25xxfd_debugfs.o +mcp25xxfd-objs += mcp25xxfd_ecc.o +mcp25xxfd-objs += mcp25xxfd_int.o
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c new file +260 -0
260 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_BASE_H +#define __MCP25XXFD_BASE_H + +#include <linux/regulator/consumer.h> + +int mcp25xxfd_base_power_enable (struct regulator *reg,int enable); + +#endif /* __MCP25XXFD_BASE_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c new file +231 -0
231 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#ifndef __MCP25XXFD_CAN_H +#define __MCP25XXFD_CAN_H + +#include "mcp25xxfd_priv.h" + +/* to put us to sleep fully we need the CAN controller to enter sleep mode */ +int mcp25xxfd_can_sleep_mode (struct mcp25xxfd_priv *priv); + +/* probe the can controller */ +int mcp25xxfd_can_probe (struct mcp25xxfd_priv *priv); + +#endif /* __MCP25XXFD_CAN_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.c new file +485 -0
485 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_clock.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#ifndef __MCP25XXFD_CLOCK_H +#define __MCP25XXFD_CLOCK_H + +#include "mcp25xxfd_priv.h" + +#define MCP25XXFD_CLK_USER_CAN BIT(0) +#define MCP25XXFD_CLK_USER_GPIO0 BIT(1) +#define MCP25XXFD_CLK_USER_GPIO1 BIT(2) +#define MCP25XXFD_CLK_USER_CLKOUT BIT(3) + +/* shared (internal) clock control */ +int mcp25xxfd_clock_init (struct mcp25xxfd_priv *priv); +int mcp25xxfd_clock_probe (struct mcp25xxfd_priv *priv); +void mcp25xxfd_clock_release (struct mcp25xxfd_priv *priv); + +int mcp25xxfd_clock_stop (struct mcp25xxfd_priv *priv,int requestor_mask); +int mcp25xxfd_clock_start (struct mcp25xxfd_priv *priv,int requestor_mask); + +void mcp25xxfd_clock_fake_sleep (struct mcp25xxfd_priv *priv); + +#endif /* __MCP25XXFD_CLOCK_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.c new file +312 -0
312 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_cmd.h new file +84 -0
84 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#include <linux/kernel.h> +#include <linux/spi/spi.h> +#include "mcp25xxfd_cmd.h" +#include "mcp25xxfd_crc.h" +#include "mcp25xxfd_regs.h" +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_crc_enable_int (struct mcp25xxfd_priv *priv,bool enable) +{ +u32 mask =MCP25XXFD_CRC_CRCERRIE |MCP25XXFD_CRC_FERRIE ; + + priv->regs.crc &= ~mask; + priv->regs.crc |= enable ? mask :0 ; + +return mcp25xxfd_cmd_write_mask (priv->spi,MCP25XXFD_CRC , + priv->regs.crc, mask); +} + +int mcp25xxfd_crc_clear_int (struct mcp25xxfd_priv *priv) +{ +return mcp25xxfd_cmd_write_mask (priv->spi,MCP25XXFD_CRC ,0 , +MCP25XXFD_CRC_CRCERRIF | +MCP25XXFD_CRC_FERRIF ); +}
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_crc.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_CRC_H +#define __MCP25XXFD_CRC_H + +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_crc_enable_int (struct mcp25xxfd_priv *priv,bool enable); +int mcp25xxfd_crc_clear_int (struct mcp25xxfd_priv *priv); + +#endif /* __MCP25XXFD_CRC_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.c new file +110 -0
110 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_debugfs.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#ifndef __MCP25XXFD_DEBUGFS_H +#define __MCP25XXFD_DEBUGFS_H + +#include "mcp25xxfd_priv.h" + +#ifdef CONFIG_DEBUG_FS + +void mcp25xxfd_debugfs_setup (struct mcp25xxfd_priv *priv); +void mcp25xxfd_debugfs_remove (struct mcp25xxfd_priv *priv); + +#else + +static inline void mcp25xxfd_debugfs_setup (struct mcp25xxfd_priv *priv) +{ +return 0 ; +} + +static inline void mcp25xxfd_debugfs_remove (struct mcp25xxfd_priv *priv) +{ +} + +#endif /* CONFIG_DEBUG_FS */ +#endif /* __MCP25XXFD_DEBUGFS_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.c new file +75 -0
75 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_ecc.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_ECC_H +#define __MCP25XXFD_ECC_H + +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_ecc_clear_int (struct mcp25xxfd_priv *priv); +int mcp25xxfd_ecc_enable_int (struct mcp25xxfd_priv *priv,bool enable); +int mcp25xxfd_ecc_enable (struct mcp25xxfd_priv *priv); + +#endif /* __MCP25XXFD_ECC_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c new file +62 -0
62 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_INT_H +#define __MCP25XXFD_INT_H + +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_int_clear (struct mcp25xxfd_priv *priv); +int mcp25xxfd_int_enable (struct mcp25xxfd_priv *priv,bool enable); + +#endif /* __MCP25XXFD_INT_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h new file +79 -0
79 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h new file +656 -0
656 lines not on this page · raw message
--
2.11.0
PATCH v6 3/9 can: mcp25xxfd: add gpiolib support for GPIO0/1 (aka. INT0/INT1)
In reply to [email protected]From: Martin Sperl <[email protected]>
Add GPIOLIB support to mcp25xxfd.
---
Changelog:
V1 -> V2: implemented support as per feedback
V2 -> V3: moved dt-binding changes to patch 1
V3 -> V4: resend
V4 -> V5: reorganized driver into separate files V5 -> V6: update copyright year
rebased on changes of patch1
no longer allocate gpio_chip separately on heap
--- drivers/net/can/spi/mcp25xxfd/Makefile | 1 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c | 13 +- drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c | 194 +++++++++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.h | 16 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h | 2 + 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.h
drivers/net/can/spi/mcp25xxfd/Makefile +1 -0
@@ -9,4 +9,5 @@ mcp25xxfd-objs += mcp25xxfd_cmd.o mcp25xxfd-objs += mcp25xxfd_crc.o mcp25xxfd-objs += mcp25xxfd_debugfs.o mcp25xxfd-objs += mcp25xxfd_ecc.o +mcp25xxfd-objs += mcp25xxfd_gpio.o mcp25xxfd-objs += mcp25xxfd_int.o
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c +12 -1
@@ -17,6 +17,7 @@#include "mcp25xxfd_cmd.h" #include "mcp25xxfd_debugfs.h" #include "mcp25xxfd_ecc.h" +#include "mcp25xxfd_gpio.h" #include "mcp25xxfd_int.h" #include "mcp25xxfd_priv.h"
@@ -147,15 +148,22 @@ static int mcp25xxfd_base_probe(struct spi_device *spi)if (ret) goto out_debugfs; +/* setting up GPIO */ + ret =mcp25xxfd_gpio_setup (priv); +if (ret) + goto out_debugfs; +/* and put controller to sleep by stopping the can clock */ ret =mcp25xxfd_clock_stop (priv,MCP25XXFD_CLK_USER_CAN );if (ret) - goto out_debugfs; + goto out_gpio;dev_info (&spi->dev,"MCP%x successfully initialized.\n" , priv->model);return 0 ; +out_gpio: +mcp25xxfd_gpio_remove (priv); out_debugfs:mcp25xxfd_debugfs_remove (priv); out_ctlclk:
@@ -173,6 +181,9 @@ static int mcp25xxfd_base_remove(struct spi_device *spi) {struct mcp25xxfd_priv *priv =spi_get_drvdata (spi); +/* remove gpio */ +mcp25xxfd_gpio_remove (priv); +/* clear all running clocks */ mcp25xxfd_clock_stop (priv, priv->clk_user_mask);
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.c new file +194 -0
194 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_gpio.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_GPIO_H +#define __MCP25XXFD_GPIO_H + +#include "mcp25xxfd_priv.h" + +/* gpiolib support */ +int mcp25xxfd_gpio_setup (struct mcp25xxfd_priv *priv); +void mcp25xxfd_gpio_remove (struct mcp25xxfd_priv *priv); + +#endif /* __MCP25XXFD_GPIO_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h +2 -0
@@ -9,6 +9,7 @@#include <linux/clk.h> #include <linux/debugfs.h> +#include <linux/gpio/driver.h> #include <linux/mutex.h> #include <linux/regulator/consumer.h> #include <linux/spi/spi.h>
@@ -25,6 +26,7 @@ enum mcp25xxfd_model {struct mcp25xxfd_priv {struct spi_device *spi;struct clk *clk; +struct gpio_chip gpio;/* the actual model of the mcp25xxfd */ enum mcp25xxfd_model model;
--
2.11.0
PATCH v6 4/9 can: mcp25xxfd: Add Microchip mcp25xxfd CAN FD driver
In reply to [email protected]From: Martin Sperl <[email protected]>
Add un-optimized Can2.0 and CanFD support.
Optimizations and HW-bug-workarounds are separate patches
On a Rasperry pi 3 it is already able to process Can2.0 Frames with DLC=0 on a CAN bus with 1MHz. without losing any packets on the SPI side. Packets still get lost inside the network stack.
As for transmission on a Rpi3 we can saturate the CAN bus at 1MHz with Can2.0 frames with DLC=0 for the number of configured tx-fifos. Afterwards we need some time to recover the state before we can fill in the fifos again.
With 7 tx fifos we can: send those 7 frames in 0.33ms and then we wait for 0.26ms so that is a 56% duty cycle.
With 24 tx fifos this changes to: 1.19ms for 24 frames and then we wait for 0.52ms so that is a 70% duty cycle.
---
Changelog:
V1 -> V2: new more generic name based on feedback from microchip
cleanup of code (checkpatch)
address all feedback on code
prepare code for GPIO handling (separate patches) handle systemerrors in a much better/reliable way
V2 -> V3: added vendor-prefix for gpio-opendrain
V3 -> V4: resend
V4 -> V5: split can driver into a separate patch
code cleanup/rewrite
V5 -> V6: update copyright year
add rational documentation for basic rx-prefetch length fix CAN_OBJ_FLAGS_BRS and CAN_OBJ_FLAGS_ESI bug fixed TDC configuration - at this moment configurable
via module parameters but with (hopefully) sensible defaults
use length instead of dlc as parameter in call of
mcp25xxfd_can_submit_rx_fd_frame
moved use of mcp25xxfd_can_int_handle_tefif_oportunistic
to separate patch
reorganized code based on feedback by Wolfgang Grandegger fixed transmission problem reported by Hubert Denkmair added extra custom error flags
added voluntary scheduling in interrupt thread
--- drivers/net/can/spi/mcp25xxfd/Makefile | 5 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c | 12 +- drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c | 483 +++++++++++++- drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h | 37 ++ .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c | 202 ++++++ .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.h | 44 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.c | 347 ++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.h | 16 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_id.h | 69 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.c | 705 +++++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.h | 17 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 183 ++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c | 390 ++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.h | 18 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c | 694 ++++++++++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.h | 86 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c | 11 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h | 2 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h | 5 + 19 files changed, 3310 insertions(+), 16 deletions(-) create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_id.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.h create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.h
drivers/net/can/spi/mcp25xxfd/Makefile +5 -0
@@ -4,6 +4,11 @@ obj-$(CONFIG_CAN_MCP25XXFD) += mcp25xxfd.o mcp25xxfd-objs := mcp25xxfd_base.o mcp25xxfd-objs += mcp25xxfd_can.o +mcp25xxfd-objs += mcp25xxfd_can_debugfs.o +mcp25xxfd-objs += mcp25xxfd_can_fifo.o +mcp25xxfd-objs += mcp25xxfd_can_int.o +mcp25xxfd-objs += mcp25xxfd_can_rx.o +mcp25xxfd-objs += mcp25xxfd_can_tx.o mcp25xxfd-objs += mcp25xxfd_clock.o mcp25xxfd-objs += mcp25xxfd_cmd.o mcp25xxfd-objs += mcp25xxfd_crc.o
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_base.c +11 -1
@@ -153,15 +153,22 @@ static int mcp25xxfd_base_probe(struct spi_device *spi)if (ret) goto out_debugfs; +/* setting up CAN */ + ret =mcp25xxfd_can_setup (priv); +if (ret) + goto out_gpio; +/* and put controller to sleep by stopping the can clock */ ret =mcp25xxfd_clock_stop (priv,MCP25XXFD_CLK_USER_CAN );if (ret) - goto out_gpio; + goto out_can;dev_info (&spi->dev,"MCP%x successfully initialized.\n" , priv->model);return 0 ; +out_can: +mcp25xxfd_can_remove (priv); out_gpio:mcp25xxfd_gpio_remove (priv); out_debugfs:
@@ -181,6 +188,9 @@ static int mcp25xxfd_base_remove(struct spi_device *spi) {struct mcp25xxfd_priv *priv =spi_get_drvdata (spi); +/* remove can */ +mcp25xxfd_can_remove (priv); +/* remove gpio */ mcp25xxfd_gpio_remove (priv);
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.c +468 -15
521 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can.h +37 -0
@@ -8,12 +8,49 @@#ifndef __MCP25XXFD_CAN_H#define __MCP25XXFD_CAN_H +#include "mcp25xxfd_can_debugfs.h" +#include "mcp25xxfd_can_priv.h" #include "mcp25xxfd_priv.h" +#include "mcp25xxfd_regs.h" + +/* get the optimal controller target mode */ +static inline +int mcp25xxfd_can_targetmode (struct mcp25xxfd_can_priv *cpriv) +{ +return (cpriv->can.dev->mtu ==CAN_MTU ) ? +MCP25XXFD_CAN_CON_MODE_CAN2_0 :MCP25XXFD_CAN_CON_MODE_MIXED ; +} + +static inline +void mcp25xxfd_can_queue_frame (struct mcp25xxfd_can_priv *cpriv, +s32 fifo,u16 ts,bool is_rx) +{ +int idx = cpriv->fifos.submit_queue_count; + + cpriv->fifos.submit_queue[idx].fifo = fifo; + cpriv->fifos.submit_queue[idx].ts = ts; + cpriv->fifos.submit_queue[idx].is_rx = is_rx; + +MCP25XXFD_DEBUGFS_INCR (cpriv->fifos.submit_queue_count); +} + +/* get the current controller mode */ +int mcp25xxfd_can_get_mode (struct mcp25xxfd_priv *priv,u32 *reg);/* to put us to sleep fully we need the CAN controller to enter sleep mode */ int mcp25xxfd_can_sleep_mode (struct mcp25xxfd_priv *priv); +/* switch controller mode */ +int mcp25xxfd_can_switch_mode_no_wait (struct mcp25xxfd_priv *priv, +u32 *reg,int mode); +int mcp25xxfd_can_switch_mode (struct mcp25xxfd_priv *priv, +u32 *reg,int mode); +/* probe the can controller */ int mcp25xxfd_can_probe (struct mcp25xxfd_priv *priv); +/* setup and the can controller net interface */ +int mcp25xxfd_can_setup (struct mcp25xxfd_priv *priv); +void mcp25xxfd_can_remove (struct mcp25xxfd_priv *priv); +#endif /* __MCP25XXFD_CAN_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c new file +202 -0
202 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.h new file +44 -0
44 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.c new file +347 -0
347 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_fifo.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#ifndef __MCP25XXFD_CAN_FIFO_H +#define __MCP25XXFD_CAN_FIFO_H + +#include "mcp25xxfd_can_priv.h" + +int mcp25xxfd_can_fifo_setup (struct mcp25xxfd_can_priv *cpriv); +void mcp25xxfd_can_fifo_release (struct mcp25xxfd_can_priv *cpriv); + +#endif /* __MCP25XXFD_CAN_FIFO_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_id.h new file +69 -0
69 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.c new file +705 -0
705 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_int.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ +#ifndef __MCP25XXFD_CAN_INT_H +#define __MCP25XXFD_CAN_INT_H + +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_can_int_clear (struct mcp25xxfd_priv *priv); +int mcp25xxfd_can_int_enable (struct mcp25xxfd_priv *priv,bool enable); + +irqreturn_t mcp25xxfd_can_int (int irq,void *dev_id); + +#endif /* __MCP25XXFD_CAN_INT_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h new file +183 -0
183 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c new file +390 -0
390 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* CAN bus driver for Microchip 25XXFD CAN Controller with SPI Interface +* +* Copyright 2019 Martin Sperl <[email protected]> +*/ + +#ifndef __MCP25XXFD_CAN_RX_H +#define __MCP25XXFD_CAN_RX_H + +#include "mcp25xxfd_priv.h" + +int mcp25xxfd_can_rx_submit_frame (struct mcp25xxfd_can_priv *cpriv,int fifo); + +int mcp25xxfd_can_rx_handle_int_rxif (struct mcp25xxfd_can_priv *cpriv); +int mcp25xxfd_can_rx_handle_int_rxovif (struct mcp25xxfd_can_priv *cpriv); + +#endif /* __MCP25XXFD_CAN_RX_H */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c new file +694 -0
396 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.h new file +86 -0
86 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_int.c +11 -0
Lines of this file are not shown. View the raw message.
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_priv.h +2 -0
Lines of this file are not shown. View the raw message.
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_regs.h +5 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
--
2.11.0
Part of the diff is not shown here. View the raw message.
PATCH v6 5/9 can: mcp25xxfd: optimize TEF read avoiding unnecessary SPI transfers
In reply to [email protected]From: Martin Sperl <[email protected]>
We have already enough information to know how many tx-messages have been terminated so that we do not have to query TEF every time if there is anything pending but we can read the tefs blindly.
This avoids 1 SPI transfer per TEF read.
---
Changelog:
V5->V6: moved optimization to separate patch
--- drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c +33 -0
@@ -274,6 +274,13 @@ int mcp25xxfd_can_tx_handle_int_tefif_fifo(struct mcp25xxfd_can_priv *cpriv) MCP25XXFD_CAN_TEFCON_UINC); } +/* reading TEF entries can be made even more efficient by reading +* multiple TEF entries in one go. +* Under the assumption that we have count(TEF) >= count(TX_FIFO) +* we can even release TEFs early (before we read them) +* (and potentially restarting the transmit-queue early aswell) +*/ +static int mcp25xxfd_can_tx_handle_int_tefif_conservative (struct mcp25xxfd_can_priv *cpriv) {
@@ -304,6 +311,25 @@ mcp25xxfd_can_tx_handle_int_tefif_conservative(struct mcp25xxfd_can_priv *cpriv)return 0 ; } +static int +mcp25xxfd_can_tx_handle_int_tefif_optimized (struct mcp25xxfd_can_priv *cpriv, +u32 finished) +{ +int i, fifo, ret; + +/* now iterate those */ +for (i =0 , fifo = cpriv->fifos.tx.start; i < cpriv->fifos.tx.count; + i++, fifo++) { +if (finished &BIT (fifo)) { + ret =mcp25xxfd_can_tx_handle_int_tefif_fifo (cpriv); +if (ret) +return ret; + } + } + +return 0 ; +} +int mcp25xxfd_can_tx_handle_int_tefif (struct mcp25xxfd_can_priv *cpriv) {unsigned long flags;
@@ -323,6 +349,13 @@ int mcp25xxfd_can_tx_handle_int_tefif(struct mcp25xxfd_can_priv *cpriv)spin_unlock_irqrestore (&cpriv->fifos.tx_queue->lock, flags); +/* run in optimized mode if possible */ +if (finished) +return mcp25xxfd_can_tx_handle_int_tefif_optimized (cpriv, + finished); +/* otherwise play it safe */ +netdev_warn (cpriv->can.dev, +"Something is wrong - we got a TEF interrupt but we were not able to detect a finished fifo\n" );return mcp25xxfd_can_tx_handle_int_tefif_conservative (cpriv); }
--
2.11.0
PATCH v6 6/9 can: mcp25xxfd: optimize TEF reads reading multiple TEFs in one go
In reply to [email protected]From: Martin Sperl <[email protected]>
To reduce the amount of spi_messages send this patch optimizes the read TEF so that multiple TEFs are read together.
Statistics in debugfs show for 100000 DLC=0 can messages sent:
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_conservative_reads 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads 44691
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_1 1862
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_2 30349
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_3 12480
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_4 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_5 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_6 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_7 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_optimized_reads_8+ 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_reads 100000
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/tef_read_splits 0
So to read all of those 100000 TEF frames to read we formerly have scheduled 100000 spi_messages.
While with this patch we have only scheduled 44691 spi_messages, so only 44.6% of the former value.
This also means we have not been transferring 110618 (=2*30349+2*2*12480) unnecessary command bytes over the SPI bus.
---
Changelog:
V5->V6: new with this patchset version
--- .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c | 18 +++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 7 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c | 85 +++++++++++++++++++--- 3 files changed, 101 insertions(+), 9 deletions(-)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c +18 -0
@@ -43,6 +43,9 @@ static void mcp25xxfd_can_debugfs_stats(struct mcp25xxfd_can_priv *cpriv, struct dentry *root) {struct dentry *dir =debugfs_create_dir ("stats" , root); +char name[32 ]; +u64 *data; +int i;# define DEBUGFS_CREATE (name, var) debugfs_create_u64(name, 0444, dir, \ &cpriv->stats.var)
@@ -62,6 +65,21 @@ static void mcp25xxfd_can_debugfs_stats(struct mcp25xxfd_can_priv *cpriv,DEBUGFS_CREATE ("int_rx_invalid_message" , int_ivm_count);DEBUGFS_CREATE ("int_crcerror" , int_cerr_count); +DEBUGFS_CREATE ("tef_reads" , tef_reads); +DEBUGFS_CREATE ("tef_conservative_reads" , tef_conservative_reads); +DEBUGFS_CREATE ("tef_optimized_reads" , tef_optimized_reads); +DEBUGFS_CREATE ("tef_read_splits" , tef_read_splits); + +for (i =0 ; i <MCP25XXFD_CAN_TEF_READ_BINS -1 ; i++) { +snprintf (name,sizeof (name), +"tef_optimized_reads_%i" , i +1 ); + data = &cpriv->stats.tef_optimized_read_sizes[i]; +debugfs_create_u64 (name,0444 , dir, data); + } +snprintf (name,sizeof (name),"tef_optimized_reads_%i+" , i +1 ); +debugfs_create_u64 (name,0444 , dir, + &cpriv->stats.tef_optimized_read_sizes[i]); +DEBUGFS_CREATE ("tx_frames_fd" , tx_fd_count);DEBUGFS_CREATE ("tx_frames_brs" , tx_brs_count);
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h +7 -0
@@ -155,6 +155,13 @@ struct mcp25xxfd_can_priv {u64 tx_fd_count;u64 tx_brs_count; +u64 tef_reads; +u64 tef_read_splits; +u64 tef_conservative_reads; +u64 tef_optimized_reads; +#define MCP25XXFD_CAN_TEF_READ_BINS 8 +u64 tef_optimized_read_sizes[MCP25XXFD_CAN_TEF_READ_BINS ]; +u64 rx_reads;u64 rx_reads_prefetched_too_few;u64 rx_reads_prefetched_too_few_bytes;
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_tx.c +76 -9
126 lines not on this page · raw message
--
2.11.0
PATCH v6 7/9 can: mcp25xxfd: optimize SPI reads of FIFOs in can2.0 mode
In reply to [email protected]From: Martin Sperl <[email protected]>
To reduce the amount of spi_messages send this patch optimizes the read of RX fifos so that multiple Fifos are read together.
This only works well in CAN2.0 mode where the overhead of prefetching 8 bytes (even if the actual length is 0) is negligable compared to the SPI-framework overhead.
Statistics in debugfs show for 1000000 DLC=0 can messages received:
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads <== 355411
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_1 <== 92778
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_2 <== 7133
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_3 <== 130390
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_4 <== 123807
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_5 <== 1266
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_6 <== 34
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_7 <== 1
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_bulk_reads_8+ <== 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads <== 999999
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/int_system_error <== 1
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/int_system_error_rx <== 1
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/int_system_error_tx <== 0
There was one SERR error where we lost one can frame during the handling of the error condition.
So to read all of those 999999 RX fifos we formerly have scheduled 999999 spi_messages.
With this patch we have only scheduled 355411 spi_messages, so only 35.5% of the former value.
This also means we have not been transferring 1289176 (=2*(999999-355411)) unnecessary command bytes over the SPI bus.
As we have been issuing less SPI transfers reading SRAM this also has reduced the likleyhood of SERR happening - it does not solve the complete issue as seen above.
---
Changelog:
V5->V6: new with this patchset version
--- .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c | 11 +++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 4 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c | 96 +++++++++++++++++++--- 3 files changed, 100 insertions(+), 11 deletions(-)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c +11 -0
@@ -92,6 +92,17 @@ static void mcp25xxfd_can_debugfs_stats(struct mcp25xxfd_can_priv *cpriv, rx_reads_prefetched_too_many);DEBUGFS_CREATE ("rx_reads_prefetched_too_many_bytes" , rx_reads_prefetched_too_many_bytes); +DEBUGFS_CREATE ("rx_single_reads" , rx_single_reads); +DEBUGFS_CREATE ("rx_bulk_reads" , rx_bulk_reads); + +for (i =0 ; i <MCP25XXFD_CAN_RX_BULK_READ_BINS -1 ; i++) { +snprintf (name,sizeof (name),"rx_bulk_reads_%i" , i +1 ); + data = &cpriv->stats.rx_bulk_read_sizes[i]; +debugfs_create_u64 (name,0444 , dir, data); + } +snprintf (name,sizeof (name),"rx_bulk_reads_%i+" , i +1 ); +debugfs_create_u64 (name,0444 , dir, + &cpriv->stats.rx_bulk_read_sizes[i]);#undef DEBUGFS_CREATE }
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h +4 -0
@@ -167,6 +167,10 @@ struct mcp25xxfd_can_priv {u64 rx_reads_prefetched_too_few_bytes;u64 rx_reads_prefetched_too_many;u64 rx_reads_prefetched_too_many_bytes; +u64 rx_single_reads; +u64 rx_bulk_reads; +#define MCP25XXFD_CAN_RX_BULK_READ_BINS 8 + u64 rx_bulk_read_sizes[MCP25XXFD_CAN_RX_BULK_READ_BINS]; } stats;#endif /* CONFIG_DEBUG_FS */
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c +85 -11
145 lines not on this page · raw message
--
2.11.0
PATCH v6 8/9 can: mcp25xxfd: add prediction of CanFD frames sizes based on history
In reply to [email protected]From: Martin Sperl <[email protected]>
Allow prediction of can frame sizes based on the last 32 Can Frames received.
Naive histogram approach hast been taken for now.
Some simple stats based on 1000 frames received with DLC=6:
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads_prefetched_too_few 16
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads_prefetched_too_few_bytes 96
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads_prefetched_too_many 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads_prefetched_too_many_bytes 0
==> /sys/kernel/debug/mcp25xxfd-spi0.0/can/stats/rx_reads_prefetch_predicted 6
The first 16 frames are predicted as 0, but after that the prediction is 6.
It should be possible to take this prediction to use bulk reads for CanFD as well when we have a prediction of length of 48 or 64.
---
Changelog:
V5->V6: new with this patchset version
--- .../net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c | 4 ++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 8 ++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c | 45 ++++++++++++++++++++-- 3 files changed, 53 insertions(+), 4 deletions(-)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_debugfs.c +4 -0
@@ -103,6 +103,10 @@ static void mcp25xxfd_can_debugfs_stats(struct mcp25xxfd_can_priv *cpriv,snprintf (name,sizeof (name),"rx_bulk_reads_%i+" , i +1 );debugfs_create_u64 (name,0444 , dir, &cpriv->stats.rx_bulk_read_sizes[i]); + +if (cpriv->can.dev->mtu ==CANFD_MTU ) +debugfs_create_u32 ("rx_reads_prefetch_predicted_len" ,0444 , + dir, &cpriv->rx_history.predicted_len);#undef DEBUGFS_CREATE }
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h +8 -0
@@ -174,6 +174,14 @@ struct mcp25xxfd_can_priv { } stats;#endif /* CONFIG_DEBUG_FS */ +/* history of rx-dlc */ +struct { +#define MCP25XXFD_CAN_RX_DLC_HISTORY_SIZE 32 +u8 dlc[MCP25XXFD_CAN_RX_DLC_HISTORY_SIZE ]; +u8 index; +u32 predicted_len; + } rx_history; +/* bus state */ struct {u32 state;
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c +41 -4
63 lines not on this page · raw message
--
2.11.0
PATCH v6 9/9 can: mcp25xxfd: optimize reception of big CanFD frame reception with BRS
In reply to [email protected]From: Martin Sperl <[email protected]>
In case of predicted DLC=15 and in case at least 33% of those are sent via BRS be aggressive and use bulk fifo reads to avoid missing any frames due to releasing FIFOs not as fast as possible.
This would be of most impact when having a 1MHz Can Bus with a 8MHz data bitrate.
---
Changelog:
V5->V6: new with this patchset version
--- drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h | 1 + drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c | 48 +++++++++++++++------- 2 files changed, 35 insertions(+), 14 deletions(-)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_priv.h +1 -0
@@ -178,6 +178,7 @@ struct mcp25xxfd_can_priv {struct {#define MCP25XXFD_CAN_RX_DLC_HISTORY_SIZE 32u8 dlc[MCP25XXFD_CAN_RX_DLC_HISTORY_SIZE ]; +u8 brs[MCP25XXFD_CAN_RX_DLC_HISTORY_SIZE ];u8 index;u32 predicted_len; } rx_history;
drivers/net/can/spi/mcp25xxfd/mcp25xxfd_can_rx.c +34 -14
78 lines not on this page · raw message
--
2.11.0