Skip to contentSkip to conversation

hw/net/can: Add mcp25625 model

PATCH In review Ben Dooks 2 participants 4 messages Mar 16, 2023 – Mar 27, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH v2 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     | 1312 +++++++++++++++++++++++++++++++++
 hw/net/can/meson.build        |    1 +
 hw/net/can/trace-events       |   13 +
 include/hw/net/can_mcp25625.h |   10 +
 5 files changed, 1341 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 18c7851efe..36f1cd1f3e 100644
@@ -137,6 +137,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 +1312 -0

1,312 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.2
#1 lore raw

Hello Ben,

thanks for update.

Quoted 8 lines · On Thursday 16 of March 2023 13:41:13 Ben Dooks wrote:

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.

Quoted 7 lines

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*"

Code looks good, I have patched actual QEMU sources and build it successfully with your change.

I have not seen any warning.

I would like to test the mcp25625 CAN functionality.

I would prefer against some target which is already available in QEMU and Linux kernel mainlines, so if somebody can suggest some ARM which can connect SPI/SSI device it would be great.

I have setup /srv/nfs/debian-riscv64 chroot and used it to prepare minimal 3 MB ramdisk.cpio with busybox and full GLIBC and ip package.

I can run it with Debian provided RISC-V kernel under QEMU compiled with your mcp25625 chip emulation

qemu-system-riscv64 -m 1G -M sifive_u -smp 2 \
      -initrd ramdisk.cpio \
      -kernel vmlinux-6.1.0-6-riscv64 \
      -nographic \
      -object can-bus,id=canbus0 \
      -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
      -global driver=mcp25625,property=canbus,value=canbus0

I can see

/sys/bus/platform/devices/10040000.spi
/sys/bus/platform/devices/10050000.spi

I can run

modprobe spi-sifive.ko

[   41.524160] sifive_spi 10040000.spi: mapped; irq=21, cs=1
[   41.529305] sifive_spi 10050000.spi: mapped; irq=22, cs=1

modprobe mcp251x.ko

I can imagine to build device tree overlay and setup it from within kernel if the device is already mapped

  cd /sys/kernel/config/device-tree/overlays
  [ -d  sifive_u-mcp25625 ] && rmdir sifive_u-mcp25625
  mkdir sifive_u-mcp25625
  cd sifive_u-mcp25625
  cat sifive_u-mcp25625.dtbo >dtbo
  echo 1 >status

which is what we do with CTU CAN FD ip on Zynq system to run PL/FPGA update.

But from QEMU info qtree, I see that device is not mapped in QEMU... Which is logic...

So please, can you send instruction how to proceed forward.

Do you have DTB prepared for testing or something similar?

In a longer term perspective, it would be ideal to provide some update for documentation, how to use mcp25625 emulation

https://www.qemu.org/docs/master/system/devices/can.html

By the way, if the Raspberry Pi emulation does not provide right SPI emulation as you have noticed, what about BeagleBoneBlack?

Does it support SPI? It could be good target to test that mcp25625 chip emulation is portable..

Best wishes,
Pavel
--
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

author
#2 lore raw
Quoted 81 lines · Pavel Pisa

Hello Ben,

thanks for update.

Quoted 8 lines · On Thursday 16 of March 2023 13:41:13 Ben Dooks wrote:

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.

Quoted 7 lines

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*"

Code looks good, I have patched actual QEMU sources and build it successfully with your change.

I have not seen any warning.

I would like to test the mcp25625 CAN functionality.

I would prefer against some target which is already available in QEMU and Linux kernel mainlines, so if somebody can suggest some ARM which can connect SPI/SSI device it would be great.

I have setup /srv/nfs/debian-riscv64 chroot and used it to prepare minimal 3 MB ramdisk.cpio with busybox and full GLIBC and ip package.

I can run it with Debian provided RISC-V kernel under QEMU compiled with your mcp25625 chip emulation

qemu-system-riscv64 -m 1G -M sifive_u -smp 2 \
       -initrd ramdisk.cpio \
       -kernel vmlinux-6.1.0-6-riscv64 \
       -nographic \
       -object can-bus,id=canbus0 \
       -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
       -global driver=mcp25625,property=canbus,value=canbus0

I can see

/sys/bus/platform/devices/10040000.spi
/sys/bus/platform/devices/10050000.spi

I can run

modprobe spi-sifive.ko

[   41.524160] sifive_spi 10040000.spi: mapped; irq=21, cs=1
[   41.529305] sifive_spi 10050000.spi: mapped; irq=22, cs=1

modprobe mcp251x.ko

I can imagine to build device tree overlay and setup it from within kernel if the device is already mapped

   cd /sys/kernel/config/device-tree/overlays
   [ -d  sifive_u-mcp25625 ] && rmdir sifive_u-mcp25625
   mkdir sifive_u-mcp25625
   cd sifive_u-mcp25625
   cat sifive_u-mcp25625.dtbo >dtbo
   echo 1 >status

which is what we do with CTU CAN FD ip on Zynq system to run PL/FPGA update.

But from QEMU info qtree, I see that device is not mapped in QEMU... Which is logic...

So please, can you send instruction how to proceed forward.

Do you have DTB prepared for testing or something similar?

Quoted 7 lines

In a longer term perspective, it would be ideal to provide some update for documentation, how to use mcp25625 emulation

https://www.qemu.org/docs/master/system/devices/can.html

By the way, if the Raspberry Pi emulation does not provide right SPI emulation as you have noticed, what about BeagleBoneBlack?

At the moment it seems that the as a whole qemu doesn't have a good way of adding a generic spi device to a bus.

Quoted 2 lines

Does it support SPI? It could be good target to test that mcp25625 chip emulation is portable..

I've pushed our test branch out to:
https://gitlab.com/CodethinkLabs/qemu/-/commits/mcp25625_test

That adds an spi channel to the sifive_u machine and puts the right dtb entry in there.

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

https://www.codethink.co.uk/privacy.html
Tested-by Acked-by
#3 lore raw

Hello Ben,

Quoted 14 lines · On Thursday 16 of March 2023 13:41:13 Ben Dooks wrote:

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*"

I have used your test branch

https://gitlab.com/CodethinkLabs/qemu/-/commits/mcp25625_test

I confirm that it contains unmodified mcp25625 model patch applied directly to QEMU master and then additional patches for actual integration with qemu/hw/riscv/sifive_u.c

I have used stripped down initramfs system build from debootstraped Debian Bookworm - GLIBC, busybox, ip route, can-utils and Linux kernel have been used. Kernel version

Linux (none) 6.1.0-7-riscv64 #1 SMP Debian 6.1.20-1 (2023-03-19) riscv64 GNU/Linux

QEMU invocation command

  qemu-system-riscv64 -m 1G -M sifive_u -smp 2 \
      -initrd ramdisk.cpio \
      -kernel vmlinux-6.1.0-7-riscv64 \
      -nographic \
      -object can-bus,id=canbus0 \
      -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
      -global driver=mcp25625,property=canbus,value=canbus0

The virtual SocketCAN interface has been used on the host side and mutual CAN dump/CAN gen has been used and worked in the both directons.

I support inclusion of your patch in QEMU mainline to allow keep it in sync with the rest of the projects.

But I would be happy if some integration is included as well. Do you plan to submit your integration to qemu/hw/riscv/sifive_u.c as well? If so, is there some plan to make mapping to SPI configurable? May be some other developer could help with suggestion what are the plans in SPI peripherals mapping in QEMU.

The mapping of IRQ over SIFIVE_U_MISC_IRQ in your followup testing patches seems to me quite ad-hock solution. It should go through some GPIO pin on a real board probably.

I would like to see at least one more system and architecture to provide option to map and test
the mcp25625 model. Raspberry Pi or some other
ARM would be nice.

Anyway, I would suggest to think about integration of CAN/CAN FD controller to SiFive SoC directly on the CPU bus because access over SPI causes latencies as I have already pointed to Martin Prudek's thesis prepared for Honeywell when we tried to enhance their choice of Raspberry Pi Matlab Simulink target used with SPI based Microchip CAN controller

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

See Figure 6.27. Cumulative latency histogram of CAN messages on page 43...

We have lot more data and tools now from our CAN latency tester project based on our CTU CAN FD IP core. There is some documentation in Matej Vasilevski's thesis

https://dspace.cvut.cz/bitstream/handle/10467/101450/F3-DP-2022-Vasilevski-Matej-vasilmat.pdf

and the followup project with continuous latency testing on actual Linux kernel mainline and preemp-rt will be submitted by this June.

Best wishes,

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

Quoted 101 lines · On Tuesday 21 of March 2023 11:49:15 Ben Dooks wrote:
Quoted 81 lines · Pavel Pisa

Hello Ben,

thanks for update.

Quoted 8 lines · On Thursday 16 of March 2023 13:41:13 Ben Dooks wrote:

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.

Quoted 7 lines

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*"

Code looks good, I have patched actual QEMU sources and build it successfully with your change.

I have not seen any warning.

I would like to test the mcp25625 CAN functionality.

I would prefer against some target which is already available in QEMU and Linux kernel mainlines, so if somebody can suggest some ARM which can connect SPI/SSI device it would be great.

I have setup /srv/nfs/debian-riscv64 chroot and used it to prepare minimal 3 MB ramdisk.cpio with busybox and full GLIBC and ip package.

I can run it with Debian provided RISC-V kernel under QEMU compiled with your mcp25625 chip emulation

qemu-system-riscv64 -m 1G -M sifive_u -smp 2 \
       -initrd ramdisk.cpio \
       -kernel vmlinux-6.1.0-6-riscv64 \
       -nographic \
       -object can-bus,id=canbus0 \
       -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus0 \
       -global driver=mcp25625,property=canbus,value=canbus0

I can see

/sys/bus/platform/devices/10040000.spi
/sys/bus/platform/devices/10050000.spi

I can run

modprobe spi-sifive.ko

[   41.524160] sifive_spi 10040000.spi: mapped; irq=21, cs=1
[   41.529305] sifive_spi 10050000.spi: mapped; irq=22, cs=1

modprobe mcp251x.ko

I can imagine to build device tree overlay and setup it from within kernel if the device is already mapped

   cd /sys/kernel/config/device-tree/overlays
   [ -d  sifive_u-mcp25625 ] && rmdir sifive_u-mcp25625
   mkdir sifive_u-mcp25625
   cd sifive_u-mcp25625
   cat sifive_u-mcp25625.dtbo >dtbo
   echo 1 >status

which is what we do with CTU CAN FD ip on Zynq system to run PL/FPGA update.

But from QEMU info qtree, I see that device is not mapped in QEMU... Which is logic...

So please, can you send instruction how to proceed forward.

Do you have DTB prepared for testing or something similar?

Quoted 7 lines

In a longer term perspective, it would be ideal to provide some update for documentation, how to use mcp25625 emulation

https://www.qemu.org/docs/master/system/devices/can.html

By the way, if the Raspberry Pi emulation does not provide right SPI emulation as you have noticed, what about BeagleBoneBlack?

At the moment it seems that the as a whole qemu doesn't have a good way of adding a generic spi device to a bus.

Quoted 2 lines

Does it support SPI? It could be good target to test that mcp25625 chip emulation is portable..

I've pushed our test branch out to:
https://gitlab.com/CodethinkLabs/qemu/-/commits/mcp25625_test

That adds an spi channel to the sifive_u machine and puts the right dtb entry in there.