mcp25xxfd: initial driver support
Subsystems (2)
Marc Kleine-Budde
maintainerPATCH v53 1/6 mcp25xxfd: initial driver support
Hello,
this is the mcp25xxfd driver I'm including in the upcoming pull request. Changes since the last post:
- Limit SPI clock to 85% of sysclk/2 (thx Thomas Kopp)
- Add clock rate overview for atmel,sama5d27 (thx Thomas Kopp)
- Add Thomas Kopp as reveiwer
- configure rx_int gpio before requesting interrupts
- print frequency of external osc during registration
- Update clock rate table for imx8mm and alwinner h3
- sanitize contents on CAN messages where length is inbetween two dlcs
- add commets to smp_mb()
regards,
Marc
Marc Kleine-Budde
maintainerPATCH v53 1/6 dt-binding: can: mcp25xxfd: document device tree bindings
From: Oleksij Rempel <[email protected]>
This patch adds the device-tree binding documentation for the Microchip MCP25xxFD SPI CAN controller family.
--- .../bindings/net/can/microchip,mcp25xxfd.yaml | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.yaml
Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.yaml new file +79 -0
@@ -0,0 +1,79 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/net/can/microchip,mcp25xxfd.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: + Microchip MCP2517FD and MCP2518FD stand-alone CAN controller device tree + bindings + +maintainers: + - Marc Kleine-Budde <[email protected]> + +properties: + compatible: + oneOf: + - const: microchip,mcp2517fd + description: for MCP2517FD + - const: microchip,mcp2518fd + description: for MCP2518FD + - const: microchip,mcp25xxfd + description: to autodetect chip variant + + reg: + maxItems: 1 + + interrupts-extended: + maxItems: 1 + + clocks: + maxItems: 1 + + vdd-supply: + description: Regulator that powers the CAN controller. + maxItems: 1 + + xceiver-supply: + description: Regulator that powers the CAN transceiver. + maxItems: 1 + + microchip,rx-int-gpios: + description: + GPIO phandle of GPIO connected to to INT1 pin of the MCP25XXFD, which + signals a pending RX interrupt. + maxItems: 1 + + spi-max-frequency: + description: + Must be half or less of "clocks" frequency. + maximum: 20000000 + +required: + - compatible + - reg + - interrupts-extended + - clocks + +examples: + - | + #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/interrupt-controller/irq.h> + + spi0 { + #address-cells = <1>; + #size-cells = <0>; + + can@0 { + compatible = "microchip,mcp25xxfd"; + reg = <0>; + clocks = <&can0_osc>; + pinctrl-names = "default"; + pinctrl-0 = <&can0_pins>; + spi-max-frequency = <20000000>; + interrupts-extended = <&gpio 13 IRQ_TYPE_LEVEL_LOW>; + microchip,rx-int-gpios = <&gpio 27 GPIO_ACTIVE_LOW>; + vdd-supply = <®5v0>; + xceiver-supply = <®5v0>; + }; + };
Signature · 1 line
2.28.0
Marc Kleine-Budde
maintainerPATCH v53 2/6 can: mcp25xxfd: add regmap infrastructure
In reply to Marc Kleine-BuddeThis patch adds the regmap infrastructure for the Microchip MCP25xxFD SPI CAN controller family. The actual driver is added in the next commit.
--- drivers/net/can/spi/Kconfig | 2 + drivers/net/can/spi/Makefile | 1 + drivers/net/can/spi/mcp25xxfd/Kconfig | 17 + drivers/net/can/spi/mcp25xxfd/Makefile | 7 + .../net/can/spi/mcp25xxfd/mcp25xxfd-crc16.c | 89 ++ .../net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c | 556 ++++++++++++ drivers/net/can/spi/mcp25xxfd/mcp25xxfd.h | 835 ++++++++++++++++++ 7 files changed, 1507 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-crc16.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd.h
drivers/net/can/spi/Kconfig +2 -0
@@ -13,4 +13,6 @@ config CAN_MCP251X Driver for the Microchip MCP251x and MCP25625 SPI CAN controllers. +source "drivers/net/can/spi/mcp25xxfd/Kconfig" + endmenu
drivers/net/can/spi/Makefile +1 -0
@@ -6,3 +6,4 @@ obj-$(CONFIG_CAN_HI311X) += hi311x.o obj-$(CONFIG_CAN_MCP251X) += mcp251x.o +obj-y += mcp25xxfd/
drivers/net/can/spi/mcp25xxfd/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config CAN_MCP25XXFD + tristate "Microchip MCP25xxFD SPI CAN controllers" + select REGMAP + help + Driver for the Microchip MCP25XXFD SPI FD-CAN controller + family. + +config CAN_MCP25XXFD_SANITY + depends on CAN_MCP25XXFD + bool "Additional Sanity Checks" + help + This option enables additional sanity checks in the driver, + that compares various internal counters with the in chip + variants. This comes with a runtime overhead. + Disable if unsure.
drivers/net/can/spi/mcp25xxfd/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_CAN_MCP25XXFD) += mcp25xxfd.o + +mcp25xxfd-objs := +mcp25xxfd-objs += mcp25xxfd-crc16.o +mcp25xxfd-objs += mcp25xxfd-regmap.o
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-crc16.c new file +89 -0
89 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +556 -0
556 lines not on this page · raw message
drivers/net/can/spi/mcp25xxfd/mcp25xxfd.h new file +835 -0
835 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v53 2/6] can: mcp25xxfd: add regmap infrastructure
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 75
Quoted 4 lines · Marc Kleine-Budde
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +4
-0+static int +mcp25xxfd_regmap_nocrc_update_bits (void *context,unsigned int reg, +unsigned int mask,unsigned int val) +{
This doesn't look like the hardware has an update_bits() operation so why is there a driver specific implementation?
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 434
Quoted 8 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +8
-0+static int +mcp25xxfd_regmap_init_nocrc (struct mcp25xxfd_priv *priv) +{ +if (!priv->map_nocrc) { +struct regmap *map; + + map =devm_regmap_init (&priv->spi->dev, &mcp25xxfd_bus_nocrc, + priv->spi, &mcp25xxfd_regmap_nocrc);
Why all these checks to see if things might already be allocated?
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 473
Quoted 6 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +6
-0+static void mcp25xxfd_regmap_destroy_nocrc (struct mcp25xxfd_priv *priv) +{ +if (priv->map_buf_nocrc_rx) { +devm_kfree (&priv->spi->dev, priv->map_buf_nocrc_rx); + priv->map_buf_nocrc_rx =NULL ; + }
Why explicitly free managed allocations like this?
Marc Kleine-Budde
maintainerRe: [PATCH v53 2/6] can: mcp25xxfd: add regmap infrastructure
Quoted 9 lines · Mark Brown
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 75
Quoted 4 lines · Marc Kleine-Budde
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +4
-0+static int +mcp25xxfd_regmap_nocrc_update_bits (void *context,unsigned int reg, +unsigned int mask,unsigned int val) +{This doesn't look like the hardware has an
update_bits()operation so why is there a driver specific implementation?
It's an optimization.
The registers of the device are 32 bits in general, but support 8 bit read/write operations. So if only bits in the e.g. 2nd 8 bit are changed, both in the read and write operation we can save 3 bytes.
Further in some registers the driver only changes all writeable bits in the respective 8bit part of the register at the same time. This means we can skip the read completely.
Quoted 11 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 434
Quoted 8 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +8
-0+static int +mcp25xxfd_regmap_init_nocrc (struct mcp25xxfd_priv *priv) +{ +if (!priv->map_nocrc) { +struct regmap *map; + + map =devm_regmap_init (&priv->spi->dev, &mcp25xxfd_bus_nocrc, + priv->spi, &mcp25xxfd_regmap_nocrc);Why all these checks to see if things might already be allocated?
Quoted 8 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 473
Quoted 6 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +6
-0+static void mcp25xxfd_regmap_destroy_nocrc (struct mcp25xxfd_priv *priv) +{ +if (priv->map_buf_nocrc_rx) { +devm_kfree (&priv->spi->dev, priv->map_buf_nocrc_rx); + priv->map_buf_nocrc_rx =NULL ; + }Why explicitly free managed allocations like this?
The driver covers two chips, the mcp2517fd and the mcp2518fd. Due to erratas we need different quirks for these variants. Further you can configure if regmap should use transfers with or without CRC checks (separately for register access, RAM access in RX and TX path).
It's possible to distinguish between chip variants by reading/writing a register, so we first init the regmap with CRC mode, autodetect the chip variant, and then re-init the regmap with the quirks of that detected variant.
The mcp25xxfd regmap init is written so that resources that are allocated first, but not needed due to different quirks are then freed (both regmap and the buffers for rx/tx).
regards,
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung West/Dortmund | Phone: +49-231-2826-924 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH v53 2/6] can: mcp25xxfd: add regmap infrastructure
Quoted 14 lines · Marc Kleine-Budde
Quoted 6 lines · Mark Brown
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 473
Quoted 4 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +4
-0+if (priv->map_buf_nocrc_rx) { +devm_kfree (&priv->spi->dev, priv->map_buf_nocrc_rx); + priv->map_buf_nocrc_rx =NULL ; + }Why explicitly free managed allocations like this?
The driver covers two chips, the mcp2517fd and the mcp2518fd. Due to erratas we need different quirks for these variants. Further you can configure if regmap should use transfers with or without CRC checks (separately for register access, RAM access in RX and TX path).
Quoted 7 lines
It's possible to distinguish between chip variants by reading/writing a register, so we first init the regmap with CRC mode, autodetect the chip variant, and then re-init the regmap with the quirks of that detected variant.
The mcp25xxfd regmap init is written so that resources that are allocated first, but not needed due to different quirks are then freed (both regmap and the buffers for rx/tx).
This feels like a non-idiomatic way of doing this - usually you'd enumerate then allocate the extra maps (using regmap_reinit_cache() to replace the regmap used to do the enumeration).
Marc Kleine-Budde
maintainerRe: [PATCH v53 2/6] can: mcp25xxfd: add regmap infrastructure
Quoted 19 lines · Mark Brown
Quoted 17 lines · Marc Kleine-Budde
Quoted 6 lines · Mark Brown
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c line 473
Quoted 4 lines
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-regmap.c new file +4
-0+if (priv->map_buf_nocrc_rx) { +devm_kfree (&priv->spi->dev, priv->map_buf_nocrc_rx); + priv->map_buf_nocrc_rx =NULL ; + }Why explicitly free managed allocations like this?
The driver covers two chips, the mcp2517fd and the mcp2518fd. Due to erratas we need different quirks for these variants. Further you can configure if regmap should use transfers with or without CRC checks (separately for register access, RAM access in RX and TX path).
It's possible to distinguish between chip variants by reading/writing a register, so we first init the regmap with CRC mode, autodetect the chip variant, and then re-init the regmap with the quirks of that detected variant.
Quoted 7 lines
Quoted 3 lines
The mcp25xxfd regmap init is written so that resources that are allocated first, but not needed due to different quirks are then freed (both regmap and the buffers for rx/tx).
This feels like a non-idiomatic way of doing this - usually you'd enumerate then allocate the extra maps (using
regmap_reinit_cache()to replace the regmap used to do the enumeration).
I have implemented two regmap clients for this driver. One does transfers with CRC, the other one not. Both are REGCACHE_NONE.
For autodetection the driver initializes the regmap which does transfers with CRC. Then it detects the chip variant and maybe that variant works properly without CRC. The CRC regmap is not needed anymore, so the buffer used to linearize the data, is freed.
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Embedded Linux | https://www.pengutronix.de | Vertretung West/Dortmund | Phone: +49-231-2826-924 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH v53 2/6] can: mcp25xxfd: add regmap infrastructure
Quoted 8 lines · Marc Kleine-Budde
Quoted 3 lines · Mark Brown
This feels like a non-idiomatic way of doing this - usually you'd enumerate then allocate the extra maps (using
regmap_reinit_cache()to replace the regmap used to do the enumeration).I have implemented two regmap clients for this driver. One does transfers with CRC, the other one not. Both are
REGCACHE_NONE.
Hrm, right. The assumption when that was written was that nothing fundamental would change about the regmap format but we should probably extend to allow that since it's the general idea. However it looks like that might not be needed here.
Quoted 4 lines
For autodetection the driver initializes the regmap which does transfers with CRC. Then it detects the chip variant and maybe that variant works properly without CRC. The CRC regmap is not needed anymore, so the buffer used to linearize the data, is freed.
That flow sounds sensible but the way the code is written it's really not clear that this is what's going on since there's only one init function with no indication that it'll be invoked multiple times, the logic for detection is in a completely separate place and functions by changing the quirk flags underneath this code - just reading the regmap handling alone you'd be hard pressed to spot that this is what's going on. It might be clearer to have an explicit function to generate the regmap for detection and then call the current function _reinit(), which would also allow it to be a bit more explicit about the transitions (if it only needs to handle the CRC->no CRC transition). Failing that at least some comments about what's going on would be useful, even just a "this will be called twice because...".
Marc Kleine-Budde
maintainerPATCH v53 3/6 can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN
In reply to Marc Kleine-BuddeThis patch adds support for the Microchip MCP25xxFD SPI CAN controller family.
--- drivers/net/can/spi/mcp25xxfd/Makefile | 1 + .../net/can/spi/mcp25xxfd/mcp25xxfd-core.c | 2907 +++++++++++++++++ 2 files changed, 2908 insertions(+) create mode 100644 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c
drivers/net/can/spi/mcp25xxfd/Makefile +1 -0
@@ -3,5 +3,6 @@ obj-$(CONFIG_CAN_MCP25XXFD) += mcp25xxfd.o mcp25xxfd-objs := +mcp25xxfd-objs += mcp25xxfd-core.o mcp25xxfd-objs += mcp25xxfd-crc16.o mcp25xxfd-objs += mcp25xxfd-regmap.o
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c new file +2907 -0
2,907 lines not on this page · raw message
Signature · 1 line
2.28.0
Marc Kleine-Budde
maintainerPATCH v53 4/6 can: mcp25xxfd: add listen-only mode
In reply to Marc Kleine-BuddeFrom: Kurt Van Dijck <[email protected]>
This commit enables listen-only mode, which works internally like CANFD mode.
--- drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c +10 -6
@@ -392,7 +392,8 @@ static int mcp25xxfd_ring_alloc(struct mcp25xxfd_priv *priv)int ram_free, i; tef_obj_size =sizeof (struct mcp25xxfd_hw_tef_obj ); -if (priv->can.ctrlmode &CAN_CTRLMODE_FD ) { +/* listen-only mode works like FD mode */ +if (priv->can.ctrlmode & (CAN_CTRLMODE_LISTENONLY |CAN_CTRLMODE_FD )) { tx_obj_num =MCP25XXFD_TX_OBJ_NUM_CANFD ; tx_obj_size =sizeof (struct mcp25xxfd_hw_tx_obj_canfd ); rx_obj_size =sizeof (struct mcp25xxfd_hw_rx_obj_canfd );
@@ -807,7 +808,7 @@ mcp25xxfd_chip_rx_fifo_init_one(const struct mcp25xxfd_priv *priv,MCP25XXFD_REG_FIFOCON_RXOVIE |MCP25XXFD_REG_FIFOCON_TFNRFNIE ; -if (priv->can.ctrlmode &CAN_CTRLMODE_FD ) +if (priv->can.ctrlmode & (CAN_CTRLMODE_LISTENONLY |CAN_CTRLMODE_FD )) fifo_con |=FIELD_PREP (MCP25XXFD_REG_FIFOCON_PLSIZE_MASK ,MCP25XXFD_REG_FIFOCON_PLSIZE_64 );else
@@ -857,7 +858,7 @@ static int mcp25xxfd_chip_fifo_init(const struct mcp25xxfd_priv *priv)MCP25XXFD_REG_FIFOCON_TXEN |MCP25XXFD_REG_FIFOCON_TXATIE ; -if (priv->can.ctrlmode &CAN_CTRLMODE_FD ) +if (priv->can.ctrlmode & (CAN_CTRLMODE_LISTENONLY |CAN_CTRLMODE_FD )) val |=FIELD_PREP (MCP25XXFD_REG_FIFOCON_PLSIZE_MASK ,MCP25XXFD_REG_FIFOCON_PLSIZE_64 );else
@@ -930,7 +931,9 @@ static u8 mcp25xxfd_get_normal_mode(const struct mcp25xxfd_priv *priv) {u8 mode; -if (priv->can.ctrlmode &CAN_CTRLMODE_FD ) +if (priv->can.ctrlmode &CAN_CTRLMODE_LISTENONLY ) + mode =MCP25XXFD_REG_CON_MODE_LISTENONLY ; +else if (priv->can.ctrlmode &CAN_CTRLMODE_FD ) mode =MCP25XXFD_REG_CON_MODE_MIXED ;else mode =MCP25XXFD_REG_CON_MODE_CAN2_0 ;
@@ -2792,8 +2795,9 @@ static int mcp25xxfd_probe(struct spi_device *spi) priv->can.do_get_berr_counter = mcp25xxfd_get_berr_counter; priv->can.bittiming_const = &mcp25xxfd_bittiming_const; priv->can.data_bittiming_const = &mcp25xxfd_data_bittiming_const; - priv->can.ctrlmode_supported =CAN_CTRLMODE_BERR_REPORTING | -CAN_CTRLMODE_FD |CAN_CTRLMODE_FD_NON_ISO ; + priv->can.ctrlmode_supported =CAN_CTRLMODE_LISTENONLY | +CAN_CTRLMODE_BERR_REPORTING |CAN_CTRLMODE_FD | +CAN_CTRLMODE_FD_NON_ISO ; priv->ndev = ndev; priv->spi = spi; priv->rx_int = rx_int;
Signature · 1 line
2.28.0
Marc Kleine-Budde
maintainerPATCH v53 5/6 MAINTAINERS: Add entry for Microchip MCP25XXFD SPI-CAN network driver
In reply to Marc Kleine-BuddeFrom: Manivannan Sadhasivam <[email protected]>
Add MAINTAINERS entry for Microchip MCP25XXFD SPI-CAN network driver.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -10671,6 +10671,14 @@ L: [email protected] S: Maintained F: drivers/hid/hid-mcp2221.c +MCP25XXFD SPI-CAN NETWORK DRIVER +M: Marc Kleine-Budde <[email protected]> +M: Manivannan Sadhasivam <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.yaml +F: drivers/net/can/spi/mcp25xxfd/ + MCP4018 AND MCP4531 MICROCHIP DIGITAL POTENTIOMETER DRIVERS M: Peter Rosin <[email protected]> L: [email protected]
Signature · 1 line
2.28.0
Marc Kleine-Budde
maintainerPATCH v53 6/6 MAINTAINERS: Add reviewer entry for microchip mcp25xxfd SPI-CAN network driver
In reply to Marc Kleine-BuddeFrom: Thomas Kopp <[email protected]>
This patch adds Thomas Kopp as a reviewer for the mcp25xxfd CAN driver.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -10674,6 +10674,7 @@ F: drivers/hid/hid-mcp2221.c MCP25XXFD SPI-CAN NETWORK DRIVER M: Marc Kleine-Budde <[email protected]> M: Manivannan Sadhasivam <[email protected]> +R: Thomas Kopp <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/net/can/microchip,mcp25xxfd.yaml
Signature · 1 line
2.28.0