Skip to contentSkip to conversation

hw/net/can: Add mcp25625 model

PATCH Superseded Ben Dooks 4 participants 5 messages Jan 4, 2023 – Mar 14, 2023 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH 1/1 hw/net/can: Add mcp25625 model

From: Ben Dooks <[email protected]>

Add support for Microchip MCP25625 SPI based CAN controller which is very similar to the MCP2515 (and covered by the same Linux driver).

This can be added to any machine with SPI support in the machine model file.

Example for using this when configured into a machine:

	-object can-bus,id=canbus0 \
	-object can-host-socketcan,id=canhost0,if=vcan0,canbus=canbus0 \
	-global driver=mcp25625,property=canbus,value=canbus0

There is tracing support with --trace "*mcp25*"

---
 hw/net/Kconfig                |    5 +
 hw/net/can/can_mcp25625.c     | 1317 +++++++++++++++++++++++++++++++++
 hw/net/can/meson.build        |    1 +
 hw/net/can/trace-events       |   13 +
 include/hw/net/can_mcp25625.h |   10 +
 5 files changed, 1346 insertions(+)
 create mode 100644 hw/net/can/can_mcp25625.c
 create mode 100644 include/hw/net/can_mcp25625.h
hw/net/Kconfig +5 -0
index 6d795ec752..e058b97c23 100644
@@ -132,6 +132,11 @@ config ROCKER
 config CAN_BUS
     bool
 
+config CAN_MCP25625
+    bool
+    default y if SSI
+    select CAN_BUS
+
 config CAN_SJA1000
     bool
     default y if PCI_DEVICES
hw/net/can/can_mcp25625.c new file +1317 -0

1,317 lines not on this page · raw message

hw/net/can/meson.build +1 -0
index 8fabbd9ee6..cfec993b52 100644
@@ -1,3 +1,4 @@
+softmmu_ss.add(when: 'CONFIG_CAN_MCP25625', if_true: files('can_mcp25625.c'))
 softmmu_ss.add(when: 'CONFIG_CAN_SJA1000', if_true: files('can_sja1000.c'))
 softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_kvaser_pci.c'))
 softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_pcm3680_pci.c'))
hw/net/can/trace-events +13 -0
index 8346a98ab5..99dcea7f9b 100644
@@ -1,3 +1,16 @@
+# mcp25625.c
+mcp25625_send_txb(const char *dev, unsigned buff, unsigned len) "%s: buffer %d (%d bytes)"
+mcp25625_irq_change(const char *dev, uint32_t from, uint32_t to) "%s: irq changed from 0x%" PRIx32 " to 0x%" PRIx32
+mcp25625_transfer8(const char *dev, int state, unsigned addr, uint32_t d) "%s: state %d, addr 0x%02x, data 0x%" PRIx32
+mpc25652_reg_update(const char *dev, uint32_t addr, uint8_t from, uint8_t to) "%s: update reg 0x%02x from 0x%02x to 0x%02x"
+mcp25625_transfer8_return(const char *dev, int state, unsigned addr, uint32_t d, uint32_t rv) "%s: state %d, addr 0x%02x, data 0x%" PRIx32 " -> 0x%" PRIx32
+mcp25625_invalid_cmd(const char *dev, uint32_t d) "%s: unknown command 0x%" PRIx32
+mcp25625_do_rts(const char *dev, unsigned buff) "%s: mask 0x%08x"
+mcp25625_irq_update(const char *dev, uint32_t flags) "%s: flags 0x%08x"
+mcp25625_change_mode(const char *dev, uint8_t from, uint8_t to) "%s: change mode from 0x%02x to 0x%02x"
+mcp25625_rx_clear(const char *dev, unsigned buff) "%s: clearing rx buffer %u"
+mcp25625_rx_buf(const char *dev, unsigned buff, uint32_t id, unsigned len) "%s: rx buffer %u: id 0x%" PRIx32 " len %u"
+
 # xlnx-zynqmp-can.c
 xlnx_can_update_irq(uint32_t isr, uint32_t ier, uint32_t irq) "ISR: 0x%08x IER: 0x%08x IRQ: 0x%08x"
 xlnx_can_reset(uint32_t val) "Resetting controller with value = 0x%08x"
include/hw/net/can_mcp25625.h new file +10 -0
new file mode 100644
index 0000000000..5ec82eb4f7
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * CAN device - MCP25625 chip model
+ *
+ * Copyright (c) 2022 SiFive, Inc.
+ */
+
+#define TYPE_MCP25625 "mcp25625"
+
+#define MCP25625(obj)  OBJECT_CHECK(MCP25625State, (obj), TYPE_MCP25625)
Signature · 1 line
2.39.0
#1 lore raw
Quoted 14 lines · Ben Dooks

From: Ben Dooks <[email protected]>

Add support for Microchip MCP25625 SPI based CAN controller which is very similar to the MCP2515 (and covered by the same Linux driver).

This can be added to any machine with SPI support in the machine model file.

Example for using this when configured into a machine:

	-object can-bus,id=canbus0 \
	-object can-host-socketcan,id=canhost0,if=vcan0,canbus=canbus0 \
	-global driver=mcp25625,property=canbus,value=canbus0
Quoted 8 lines

There is tracing support with --trace "*mcp25*"

[snip]

OOPS, just noticed I forgot to send user.name and user.email in this copy of the git repo and sent from @codethink instead of @sifive.

Will fix this if needed.

Signature · 1 line
Ben
author
#2 lore raw
In reply to Ben Dooks
Quoted 14 lines · Ben Dooks

From: Ben Dooks <[email protected]>

Add support for Microchip MCP25625 SPI based CAN controller which is very similar to the MCP2515 (and covered by the same Linux driver).

This can be added to any machine with SPI support in the machine model file.

Example for using this when configured into a machine:

	-object can-bus,id=canbus0 \
	-object can-host-socketcan,id=canhost0,if=vcan0,canbus=canbus0 \
	-global driver=mcp25625,property=canbus,value=canbus0
Quoted 8 lines

There is tracing support with --trace "*mcp25*"

Has anyone had chance to review this, it would be great to get this moving along.

Signature · 4 lines
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html
#3 lore raw

Dear Ben,

sorry for longer response times...

Quoted 5 lines · On Tuesday 17 of January 2023 14:32:29 Ben Dooks wrote:
Quoted 4 lines · Ben Dooks

From: Ben Dooks <[email protected]>

Add support for Microchip MCP25625 SPI based CAN controller which is very similar to the MCP2515 (and covered by the same Linux driver).

...

Quoted 2 lines

Has anyone had chance to review this, it would be great to get this moving along.

Generally, I am happy that you consider use and extend our work.

I have looked at the code. But even that implementation of CAN subsystem in QEMU was my idea and I led studnets working on it and sometimes heavily rewritten code to be acceptable, I am not QEMU expert and I have not studied its SSI subsystem so for comment from QEMU code requiremts I would be happy for some other to step in.

I would like to test the peripheral. Please, can you try to elaborate and prepare description how to config QEMU for RPi emulation with mcp2515 overlay?

https://github.com/raspberrypi/linux/blob/rpi-6.1.y/arch/arm/boot/dts/overlays/mcp2515-can0-overlay.dts

I have RPi images and I have experience with this hardware. Even that I consider mcp2515 as really unfortunate solution and we have spent lot of time to help colleagues to enhance a little latencies of this solution when they chose that HW for serious wok instead of some NXP, TI, Xilinx or other sane SoC with CAN.

https://dspace.cvut.cz/bitstream/handle/10467/68605/F3-DP-2017-Prudek-Martin-Dp_2017_prudek_martin.pdf

But yes, people are using this chip a lot so it would worth to have emulation in QEMU. If the SPI connection is required then mcp251xfd seems to have chance for lower SPI transfer count overhead nd performance. But real SoC bus connected controllers are much better for serious project, if you design chip there are more cores available M-CAN, GRCAN and even our own CTU CAN FD which has already emulation in QEMU.

Back to MCP25x1x. I have gone through code and try to understand the function. There is lot of connected to the locations in the registers maps in the chip

Quoted 1 line
  s->ssi_addr = 0x31;

I have took manual but I think that it would help to add there comments with registers names or even use defines for these. But may it be, that for the easy ampping in the table and increment logic numbers are reasonable option... But comments with register symbolic names would help.

The code does define only single property ("canbus") to select CAN bus to connect controller to. Mapping to the SPI peripheral is provided by device tree on QEMU side or by some other machine specific glue code? Please, can you provide more information for intended target use and RPi option to use for testing?

As for the code, I have read it the first time and for full check I would need to spent more time with it. But I expect that functionality check with the respect to mcp25x1x datasheet has been done mainly by you so the full check bit by bit is not necessary. If there is some omitted case it would be (hopefully) found during code use. As for generic code style and redability, I see no problem. I expect that you have checked for QEMU style and if there has been some problem you have propably received QEMU CI and static analysis feedback.

hw/net/can/can_mcp25625.c line 1034

Quoted 8 lines
hw/net/can/can_mcp25625.c new file +8 -0
+static void mcp25625_rx_into_buf(MCP25625State *s,
+                                 const qemu_can_frame *frame,
+                                 unsigned buffnr, int filthit)
+{
+    struct rxbuff *rxbuff = &s->rxbuffs[buffnr];
+    qemu_canid_t e_id, sidl, id, q_id = frame->can_id;
+    unsigned len = frame->can_dlc;
+

I would suggest to check for can_dlc > 8 in this function or in its caller (mcp25625_can_receive), to ensure that QEMU cannot be attacked by some malformed CAN message from the host kernel or other VM...

Quoted 6 lines
hw/net/can/can_mcp25625.c new file +6 -0
+static ssize_t mcp25625_can_receive(CanBusClientState *client,
+                                    const qemu_can_frame *buf,
+                                    size_t frames_cnt)
+{
+    MCP25625State *s = client_to_mcp(client);
+    int ret;

hw/net/can/can_mcp25625.c line 1110

Quoted 10 lines
hw/net/can/can_mcp25625.c new file +10 -0
+
+    /* support receiving only one frame at a time */
+    if (frames_cnt != 1) {
+        return -1;
+    }
+
+    /* we don't support error frames or buf->flags */
+    if (buf->can_id & QEMU_CAN_ERR_FLAG || buf->flags != 0) {
+        return -1;
+    }

I see as next step that you provide description/patch etc for some target that I can test the code with CAN tools on my side. I would prefer connection to some ARM Cortex-A target where I have some images t run ready. RISC-V is really interesting for me too, so if there is some option to run something small, I can try that too. I prefer minimal setup with self compilled bysybox in initramfs and the mapping of some development directories through virtfs into system.

If you plan to visit FOSDEM 2023, we can meet there in person at RISC-V devroom and I want to take tour for automotive and other areas. Another chance is Embedded World where we plan to show CAN latency tester demo with CTU CAN FD and some MCUs...

Best wishes,

Pavel

Signature · 12 lines
                Pavel Pisa
    phone:      +420 603531357
    e-mail:     [email protected]
    Department of Control Engineering FEE CVUT
    Karlovo namesti 13, 121 35, Prague 2
    university: http://control.fel.cvut.cz/
    personal:   http://cmp.felk.cvut.cz/~pisa
    projects:   https://www.openhub.net/accounts/ppisa
    CAN related:http://canbus.pages.fel.cvut.cz/
    RISC-V education: https://comparch.edu.cvut.cz/
    Open Technologies Research Education and Exchange Services
    https://gitlab.fel.cvut.cz/otrees/org/-/wikis/home
#4 lore raw
Quoted 3 lines · Pavel Pisa

Dear Ben,

sorry for longer response times...

I think we've both dropped the ball on this one, just got reminded about this set and found it got deleted from work email.

We've done review upates and will try and get some branches out today and maybe a new patch out for inclusion.

Quoted 23 lines · On Tuesday 17 of January 2023 14:32:29 Ben Dooks wrote:
Quoted 5 lines · Ben Dooks
Quoted 4 lines

From: Ben Dooks <[email protected]>

Add support for Microchip MCP25625 SPI based CAN controller which is very similar to the MCP2515 (and covered by the same Linux driver).

...

Quoted 2 lines

Has anyone had chance to review this, it would be great to get this moving along.

Generally, I am happy that you consider use and extend our work.

I have looked at the code. But even that implementation of CAN subsystem in QEMU was my idea and I led studnets working on it and sometimes heavily rewritten code to be acceptable, I am not QEMU expert and I have not studied its SSI subsystem so for comment from QEMU code requiremts I would be happy for some other to step in.

I would like to test the peripheral. Please, can you try to elaborate and prepare description how to config QEMU for RPi emulation with mcp2515 overlay?

Quoted 7 lines

https://github.com/raspberrypi/linux/blob/rpi-6.1.y/arch/arm/boot/dts/overlays/mcp2515-can0-overlay.dts

I have RPi images and I have experience with this hardware. Even that I consider mcp2515 as really unfortunate solution and we have spent lot of time to help colleagues to enhance a little latencies of this solution when they chose that HW for serious wok instead of some NXP, TI, Xilinx or other sane SoC with CAN.

I had a quick look at setting an pi3b emulation but it does not currently have an SSI port available by default. I will try and post the branch I used with the SiFive Unmatched board later.

Quoted 9 lines

https://dspace.cvut.cz/bitstream/handle/10467/68605/F3-DP-2017-Prudek-Martin-Dp_2017_prudek_martin.pdf

But yes, people are using this chip a lot so it would worth to have emulation in QEMU. If the SPI connection is required then mcp251xfd seems to have chance for lower SPI transfer count overhead nd performance. But real SoC bus connected controllers are much better for serious project, if you design chip there are more cores available M-CAN, GRCAN and even our own CTU CAN FD which has already emulation in QEMU.

Quoted 5 lines

Back to MCP25x1x. I have gone through code and try to understand the function. There is lot of connected to the locations in the registers maps in the chip

Quoted 1 line
  s->ssi_addr = 0x31;

A lot of these where basically used in one place and we map the regs into internal data structs. Nazar and I have been through and tried to tidy some of this up and make it more explicit what is being done.

Quoted 11 lines

I have took manual but I think that it would help to add there comments with registers names or even use defines for these. But may it be, that for the easy ampping in the table and increment logic numbers are reasonable option... But comments with register symbolic names would help.

The code does define only single property ("canbus") to select CAN bus to connect controller to. Mapping to the SPI peripheral is provided by device tree on QEMU side or by some other machine specific glue code? Please, can you provide more information for intended target use and RPi option to use for testing?

Yes, at the moment it seems that command-line mapping of SSI is not easy, or well documented? Currently, other than the CS line that is generally done with the controller, the only ouput needed is the IRQ line. We do have the RXB0 and RXB1 outputs but they're not really needed and only there for people to try if they want.

Quoted 10 lines

As for the code, I have read it the first time and for full check I would need to spent more time with it. But I expect that functionality check with the respect to mcp25x1x datasheet has been done mainly by you so the full check bit by bit is not necessary. If there is some omitted case it would be (hopefully) found during code use. As for generic code style and redability, I see no problem. I expect that you have checked for QEMU style and if there has been some problem you have propably received QEMU CI and static analysis feedback.

Quoted 13 lines

hw/net/can/can_mcp25625.c line 1034

Quoted 8 lines
hw/net/can/can_mcp25625.c new file +8 -0
+static void mcp25625_rx_into_buf(MCP25625State *s,
+                                 const qemu_can_frame *frame,
+                                 unsigned buffnr, int filthit)
+{
+    struct rxbuff *rxbuff = &s->rxbuffs[buffnr];
+    qemu_canid_t e_id, sidl, id, q_id = frame->can_id;
+    unsigned len = frame->can_dlc;
+

I would suggest to check for can_dlc > 8 in this function or in its caller (mcp25625_can_receive), to ensure that QEMU cannot be attacked by some malformed CAN message from the host kernel or other VM...

Ok, added.

Quoted 17 lines
Quoted 6 lines
hw/net/can/can_mcp25625.c new file +6 -0
+static ssize_t mcp25625_can_receive(CanBusClientState *client,
+                                    const qemu_can_frame *buf,
+                                    size_t frames_cnt)
+{
+    MCP25625State *s = client_to_mcp(client);
+    int ret;

hw/net/can/can_mcp25625.c line 1110

Quoted 10 lines
hw/net/can/can_mcp25625.c new file +10 -0
+
+    /* support receiving only one frame at a time */
+    if (frames_cnt != 1) {
+        return -1;
+    }
+
+    /* we don't support error frames or buf->flags */
+    if (buf->can_id & QEMU_CAN_ERR_FLAG || buf->flags != 0) {
+        return -1;
+    }
Quoted 8 lines

I see as next step that you provide description/patch etc for some target that I can test the code with CAN tools on my side. I would prefer connection to some ARM Cortex-A target where I have some images t run ready. RISC-V is really interesting for me too, so if there is some option to run something small, I can try that too. I prefer minimal setup with self compilled bysybox in initramfs and the mapping of some development directories through virtfs into system.

We did this with a riscv buildroot initramfs with sifive-u machine.

I can see if we can share this and a branch.

Quoted 4 lines

If you plan to visit FOSDEM 2023, we can meet there in person at RISC-V devroom and I want to take tour for automotive and other areas. Another chance is Embedded World where we plan to show CAN latency tester demo with CTU CAN FD and some MCUs...

Maybe next year.

Signature · 3 lines
Ben Dooks, [email protected], http://www.fluff.org/ben/

Large Hadron Colada: A large Pina Colada that makes the universe disappear.