Introduce Xilinx ZynqMP CAN controller
Subsystems (2)
v11 cover Introduce Xilinx ZynqMP CAN controller
Changelog:
v10 -> v11:
Replace DB_PRINTS with trace-events.
Removed unnecessary local variables.
Added const with tx/rx buffers in qtest.
Added reviewed-by tags for qtest.
v9 -> v10:
Rebase the series with the new meson build system.
v8 -> v9:
Use g_autofree to do automatic cleanup the object_get_canonical_path() used.
v7 -> v8:
Change CAN controller to keep one canbus per controller. Add canbus connections at machine level. Remove ctrl_idx from CAN controller.
v6 -> v7:
Remove '-m 4G' option from xlnx-can-test. This option causes the fail of docker-quick@centos7 build test.
v5 -> v6:
Add ptimer based counter for time stamping on RX messages. Fix reset issues.
Rebase the patches with master latest changes. Added reference clock property for CAN ptimer.
v4 -> v5:
Add XlnxZynqMPCAN controller id to debug messages. Drop parameter errp of object_property_add(). Add formatting related suggestions.
v3 -> v4:
Correct formatting issues.
v2 -> v3:
Rectify the build issue.
Rearrange the patch order.
v1 -> v2:
Rename the CAN device state and address code style issues. Connect the CAN device to Xlnx-ZCU102 board.
Add maintainer entry.
Add QTEST for the CAN device.
Vikram Garhwal (4):
hw/net/can: Introduce Xilinx ZynqMP CAN controller
xlnx-zynqmp: Connect Xilinx ZynqMP CAN controllers tests/qtest: Introduce tests for Xilinx ZynqMP CAN controller
MAINTAINERS: Add maintainer entry for Xilinx ZynqMP CAN controller
meson.build | 1 + hw/net/can/trace.h | 1 + include/hw/arm/xlnx-zynqmp.h | 8 + include/hw/net/xlnx-zynqmp-can.h | 78 +++ hw/arm/xlnx-zcu102.c | 20 + hw/arm/xlnx-zynqmp.c | 34 ++ hw/net/can/xlnx-zynqmp-can.c | 1159 ++++++++++++++++++++++++++++++++++++++ tests/qtest/xlnx-can-test.c | 360 ++++++++++++ MAINTAINERS | 8 + hw/Kconfig | 1 + hw/net/can/meson.build | 1 + hw/net/can/trace-events | 9 + tests/qtest/meson.build | 1 + 13 files changed, 1681 insertions(+) create mode 100644 hw/net/can/trace.h create mode 100644 include/hw/net/xlnx-zynqmp-can.h create mode 100644 hw/net/can/xlnx-zynqmp-can.c create mode 100644 tests/qtest/xlnx-can-test.c create mode 100644 hw/net/can/trace-events
--
2.7.4
PATCH v11 1/4 hw/net/can: Introduce Xilinx ZynqMP CAN controller
The Xilinx ZynqMP CAN controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
Example for using single CAN:
-object can-bus,id=canbus0 \
-machine xlnx-zcu102.canbus0=canbus0 \
-object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0Example for connecting both CAN to same virtual CAN on host machine:
-object can-bus,id=canbus0 -object can-bus,id=canbus1 \
-machine xlnx-zcu102.canbus0=canbus0 \
-machine xlnx-zcu102.canbus1=canbus1 \
-object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0 \
-object can-host-socketcan,id=socketcan1,if=vcan0,canbus=canbus1To create virtual CAN on the host machine, please check the QEMU CAN docs:
https://github.com/qemu/qemu/blob/master/docs/can.txt
--- meson.build | 1 + hw/net/can/trace.h | 1 + include/hw/net/xlnx-zynqmp-can.h | 78 +++ hw/net/can/xlnx-zynqmp-can.c | 1159 ++++++++++++++++++++++++++++++++++++++ hw/Kconfig | 1 + hw/net/can/meson.build | 1 + hw/net/can/trace-events | 9 + 7 files changed, 1250 insertions(+) create mode 100644 hw/net/can/trace.h create mode 100644 include/hw/net/xlnx-zynqmp-can.h create mode 100644 hw/net/can/xlnx-zynqmp-can.c create mode 100644 hw/net/can/trace-events
meson.build +1 -0
@@ -1274,6 +1274,7 @@ if have_system 'hw/misc', 'hw/misc/macio', 'hw/net', + 'hw/net/can', 'hw/nvram', 'hw/pci', 'hw/pci-host',
hw/net/can/trace.h new file +1 -0
@@ -0,0 +1 @@ +#include "trace/trace-hw_net_can.h"
include/hw/net/xlnx-zynqmp-can.h new file +78 -0
78 lines not on this page · raw message
hw/net/can/xlnx-zynqmp-can.c new file +1159 -0
1,159 lines not on this page · raw message
hw/Kconfig +1 -0
@@ -80,3 +80,4 @@ config XILINX_AXI config XLNX_ZYNQMP bool select REGISTER + select CAN_BUS
hw/net/can/meson.build +1 -0
@@ -4,3 +4,4 @@ softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_pcm3680_pci.c')) softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_mioe3680_pci.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD', if_true: files('ctucan_core.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD_PCI', if_true: files('ctucan_pci.c')) +softmmu_ss.add(when: 'CONFIG_XLNX_ZYNQMP', if_true: files('xlnx-zynqmp-can.c'))
hw/net/can/trace-events new file +9 -0
@@ -0,0 +1,9 @@ +# xlnx-zynqmp-can.c +xlnx_can_transfer_fifo(const char *message) "%s" +xlnx_can_srr_pre_write(const char *message) "%s" +xlnx_can_update_rx_fifo(const char *message) "%s" +xlnx_can_rxfifo_pre_read(const char *message) "%s" +xlnx_can_tx_post_write(const char *message) "%s" +xlnx_can_can_receive(const char *message) "%s" +xlnx_can_receive(const char *message) "%s" +xlnx_can_realize(const char *message) "%s"
Signature · 1 line
2.7.4
Re: [PATCH v11 1/4] hw/net/can: Introduce Xilinx ZynqMP CAN controller
Quoted 23 lines · Vikram Garhwal
The Xilinx ZynqMP CAN controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
Example for using single CAN:
-object can-bus,id=canbus0 \ -machine xlnx-zcu102.canbus0=canbus0 \ -object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0Example for connecting both CAN to same virtual CAN on host machine:
-object can-bus,id=canbus0 -object can-bus,id=canbus1 \ -machine xlnx-zcu102.canbus0=canbus0 \ -machine xlnx-zcu102.canbus1=canbus1 \ -object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0 \ -object can-host-socketcan,id=socketcan1,if=vcan0,canbus=canbus1To create virtual CAN on the host machine, please check the QEMU CAN docs:
https://github.com/qemu/qemu/blob/master/docs/can.txtSigned-off-by Vikram Garhwal <[email protected]>
include/hw/net/xlnx-zynqmp-can.h line 48
Quoted 4 lines
include/hw/net/xlnx-zynqmp-can.h new file +4
-0+#define MAX_CAN_CTRLS 2 +#define XLNX_ZYNQMP_CAN_R_MAX (0x84 / 4) +#define MAILBOX_CAPACITY 64 +#define CAN_TIMER_MAX 0XFFFFUL
"0x" is more usual.
Quoted 8 lines
hw/net/can/xlnx-zynqmp-can.c new file +4
-0+if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) { +trace_xlnx_can_can_receive ("Controller is in reset.\n" ); +return false; + }
hw/net/can/trace-events line 9
Quoted 9 lines
hw/net/can/trace-events new file +9
-0@@ -0,0 +1,9 @@ +# xlnx-zynqmp-can.c +xlnx_can_transfer_fifo(const char *message) "%s" +xlnx_can_srr_pre_write(const char *message) "%s" +xlnx_can_update_rx_fifo(const char *message) "%s" +xlnx_can_rxfifo_pre_read(const char *message) "%s" +xlnx_can_tx_post_write(const char *message) "%s" +xlnx_can_can_receive(const char *message) "%s" +xlnx_can_receive(const char *message) "%s" +xlnx_can_realize(const char *message) "%s"
This is not the usual way to do tracepoints. Generally rather than having one trace point which gets passed an opaque string, you should have trace points for each interesting event, which have an event-specific format string that prints out the useful information. Here's an example from a UART model:
pl011_can_receive(uint32_t lcr, int read_count, int r) "LCR 0x%08xread_count %d returning %d"
Notice that it's specific to one event (there's only one place in the code that calls that tracepoint), it mostly relies on the name of the trace event itself to give the user context, and it uses the format string to provide some information about the internal state of the device (not always relevant for all trace events -- sometimes you do just have an event).
That way users of tracepoints can enable, for instance, all the 'xnlx_can_rxfifo*' tracepoints if they want all the logging, or only 'xlnx_can_rxfifo_full' if they only care to log the "fifo filled up" event but not others.
thanks
-- PMM
Re: [PATCH v11 1/4] hw/net/can: Introduce Xilinx ZynqMP CAN controller
Quoted 24 lines · Peter Maydell
Quoted 21 lines · Vikram Garhwal
The Xilinx ZynqMP CAN controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
Example for using single CAN:
-object can-bus,id=canbus0 \ -machine xlnx-zcu102.canbus0=canbus0 \ -object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0Example for connecting both CAN to same virtual CAN on host machine:
-object can-bus,id=canbus0 -object can-bus,id=canbus1 \ -machine xlnx-zcu102.canbus0=canbus0 \ -machine xlnx-zcu102.canbus1=canbus1 \ -object can-host-socketcan,id=socketcan0,if=vcan0,canbus=canbus0 \ -object can-host-socketcan,id=socketcan1,if=vcan0,canbus=canbus1To create virtual CAN on the host machine, please check the QEMU CAN docs:
https://github.com/qemu/qemu/blob/master/docs/can.txtSigned-off-by Vikram Garhwal <[email protected]>
Quoted 6 lines
include/hw/net/xlnx-zynqmp-can.h line 48
Quoted 4 lines
include/hw/net/xlnx-zynqmp-can.h new file +4
-0+#define MAX_CAN_CTRLS 2 +#define XLNX_ZYNQMP_CAN_R_MAX (0x84 / 4) +#define MAILBOX_CAPACITY 64 +#define CAN_TIMER_MAX 0XFFFFUL"0x" is more usual.
I will correct this.
Quoted 35 lines
Quoted 10 lines
hw/net/can/xlnx-zynqmp-can.c new file +4
-0+if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) { +trace_xlnx_can_can_receive ("Controller is in reset.\n" ); +return false; + }hw/net/can/trace-events line 9
Quoted 9 lines
hw/net/can/trace-events new file +9
-0@@ -0,0 +1,9 @@ +# xlnx-zynqmp-can.c +xlnx_can_transfer_fifo(const char *message) "%s" +xlnx_can_srr_pre_write(const char *message) "%s" +xlnx_can_update_rx_fifo(const char *message) "%s" +xlnx_can_rxfifo_pre_read(const char *message) "%s" +xlnx_can_tx_post_write(const char *message) "%s" +xlnx_can_can_receive(const char *message) "%s" +xlnx_can_receive(const char *message) "%s" +xlnx_can_realize(const char *message) "%s"This is not the usual way to do tracepoints. Generally rather than having one trace point which gets passed an opaque string, you should have trace points for each interesting event, which have an event-specific format string that prints out the useful information. Here's an example from a UART model:
pl011_can_receive(uint32_t lcr, int read_count, int r)"LCR 0x%08xread_count%d returning %d"
Notice that it's specific to one event (there's only one place in the code that calls that tracepoint), it mostly relies on the name of the trace event itself to give the user context, and it uses the format string to provide some information about the internal state of the device (not always relevant for all trace events -- sometimes you do just have an event).
Quoted 5 lines
That way users of tracepoints can enable, for instance, all the 'xnlx_can_rxfifo*' tracepoints if they want all the logging, or only '
xlnx_can_rxfifo_full' if they only care to log the "fifo filled up" event but not others.
Hi Peter,
Thank you so much for explaining this in detail. I will create distinct tracepoints for each event in next version. I will prepare v12 and send it soon.
Quoted 2 lines
thanks
-- PMM
PATCH v11 2/4 xlnx-zynqmp: Connect Xilinx ZynqMP CAN controllers
In reply to Vikram GarhwalConnect CAN0 and CAN1 on the ZynqMP.
--- include/hw/arm/xlnx-zynqmp.h | 8 ++++++++ hw/arm/xlnx-zcu102.c | 20 ++++++++++++++++++++ hw/arm/xlnx-zynqmp.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+)
include/hw/arm/xlnx-zynqmp.h +8 -0
@@ -22,6 +22,7 @@#include "hw/intc/arm_gic.h" #include "hw/net/cadence_gem.h" #include "hw/char/cadence_uart.h" +#include "hw/net/xlnx-zynqmp-can.h" #include "hw/ide/ahci.h" #include "hw/sd/sdhci.h" #include "hw/ssi/xilinx_spips.h"
@@ -33,6 +34,7 @@#include "hw/cpu/cluster.h" #include "target/arm/cpu.h" #include "qom/object.h" +#include "net/can_emu.h" #define TYPE_XLNX_ZYNQMP "xlnx,zynqmp"OBJECT_DECLARE_SIMPLE_TYPE (XlnxZynqMPState,XLNX_ZYNQMP )
@@ -41,6 +43,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(XlnxZynqMPState, XLNX_ZYNQMP)#define XLNX_ZYNQMP_NUM_RPU_CPUS 2#define XLNX_ZYNQMP_NUM_GEMS 4#define XLNX_ZYNQMP_NUM_UARTS 2 +#define XLNX_ZYNQMP_NUM_CAN 2 +#define XLNX_ZYNQMP_CAN_REF_CLK (24 * 1000 * 1000)#define XLNX_ZYNQMP_NUM_SDHCI 2#define XLNX_ZYNQMP_NUM_SPIS 2#define XLNX_ZYNQMP_NUM_GDMA_CH 8
@@ -92,6 +96,7 @@ struct XlnxZynqMPState {CadenceGEMState gem[XLNX_ZYNQMP_NUM_GEMS ];CadenceUARTState uart[XLNX_ZYNQMP_NUM_UARTS ]; +XlnxZynqMPCANState can[XLNX_ZYNQMP_NUM_CAN ];SysbusAHCIState sata;SDHCIState sdhci[XLNX_ZYNQMP_NUM_SDHCI ];XilinxSPIPS spi[XLNX_ZYNQMP_NUM_SPIS ];
@@ -112,6 +117,9 @@ struct XlnxZynqMPState {bool virt;/* Has the RPU subsystem? */ bool has_rpu; + +/* CAN bus. */ + CanBusState *canbus[XLNX_ZYNQMP_NUM_CAN]; };#endif
hw/arm/xlnx-zcu102.c +20 -0
@@ -25,6 +25,7 @@#include "sysemu/qtest.h" #include "sysemu/device_tree.h" #include "qom/object.h" +#include "net/can_emu.h" struct XlnxZCU102 {MachineState parent_obj;
@@ -34,6 +35,8 @@ struct XlnxZCU102 {bool secure;bool virt; +CanBusState *canbus[XLNX_ZYNQMP_NUM_CAN ]; +struct arm_boot_info binfo; };
@@ -125,6 +128,14 @@ static void xlnx_zcu102_init(MachineState *machine)object_property_set_bool (OBJECT (&s->soc),"virtualization" , s->virt, &error_fatal); +for (i =0 ; i <XLNX_ZYNQMP_NUM_CAN ; i++) { +gchar *bus_name =g_strdup_printf ("canbus%d" , i); + +object_property_set_link (OBJECT (&s->soc), bus_name, +OBJECT (s->canbus[i]), &error_fatal); +g_free (bus_name); + } +qdev_realize (DEVICE (&s->soc),NULL , &error_fatal);/* Create and plug in the SD cards */
@@ -208,6 +219,15 @@ static void xlnx_zcu102_machine_instance_init(Object *obj) s->secure = false;/* Default to virt (EL2) being disabled */ s->virt = false; +object_property_add_link (obj,"xlnx-zcu102.canbus0" ,TYPE_CAN_BUS , + (Object **)&s->canbus[0 ], + object_property_allow_set_link, +0 ); + +object_property_add_link (obj,"xlnx-zcu102.canbus1" ,TYPE_CAN_BUS , + (Object **)&s->canbus[1 ], + object_property_allow_set_link, +0 ); }static void xlnx_zcu102_machine_class_init (ObjectClass *oc,void *data)
hw/arm/xlnx-zynqmp.c +34 -0
@@ -81,6 +81,14 @@ static const int uart_intr[XLNX_ZYNQMP_NUM_UARTS] = { 21, 22, }; +static const uint64_t can_addr[XLNX_ZYNQMP_NUM_CAN ] = { +0xFF060000 ,0xFF070000 , +}; + +static const int can_intr[XLNX_ZYNQMP_NUM_CAN ] = { +23 ,24 , +}; +static const uint64_t sdhci_addr[XLNX_ZYNQMP_NUM_SDHCI ] = {0xFF160000 ,0xFF170000 , };
@@ -243,6 +251,11 @@ static void xlnx_zynqmp_init(Object *obj) TYPE_CADENCE_UART); } +for (i =0 ; i <XLNX_ZYNQMP_NUM_CAN ; i++) { +object_initialize_child (obj,"can[*]" , &s->can[i], +TYPE_XLNX_ZYNQMP_CAN ); + } +object_initialize_child (obj,"sata" , &s->sata,TYPE_SYSBUS_AHCI );for (i =0 ; i <XLNX_ZYNQMP_NUM_SDHCI ; i++) {
@@ -482,6 +495,23 @@ static void xlnx_zynqmp_realize(DeviceState *dev, Error **errp) gic_spi[uart_intr[i]]); } +for (i =0 ; i <XLNX_ZYNQMP_NUM_CAN ; i++) { +object_property_set_int (OBJECT (&s->can[i]),"ext_clk_freq" , +XLNX_ZYNQMP_CAN_REF_CLK , &error_abort); + +object_property_set_link (OBJECT (&s->can[i]),"canbus" , +OBJECT (s->canbus[i]), &error_fatal); + +sysbus_realize (SYS_BUS_DEVICE (&s->can[i]), &err); +if (err) { +error_propagate (errp, err); +return ; + } +sysbus_mmio_map (SYS_BUS_DEVICE (&s->can[i]),0 , can_addr[i]); +sysbus_connect_irq (SYS_BUS_DEVICE (&s->can[i]),0 , + gic_spi[can_intr[i]]); + } +object_property_set_int (OBJECT (&s->sata),"num-ports" ,SATA_NUM_PORTS , &error_abort);if (!sysbus_realize (SYS_BUS_DEVICE (&s->sata), errp)) {
@@ -619,6 +649,10 @@ static Property xlnx_zynqmp_props[] = {DEFINE_PROP_BOOL ("has_rpu" , XlnxZynqMPState, has_rpu, false),DEFINE_PROP_LINK ("ddr-ram" , XlnxZynqMPState, ddr_ram,TYPE_MEMORY_REGION , MemoryRegion *), +DEFINE_PROP_LINK ("canbus0" , XlnxZynqMPState, canbus[0 ],TYPE_CAN_BUS , + CanBusState *), +DEFINE_PROP_LINK ("canbus1" , XlnxZynqMPState, canbus[1 ],TYPE_CAN_BUS , + CanBusState *), DEFINE_PROP_END_OF_LIST() };
Signature · 1 line
2.7.4
PATCH v11 3/4 tests/qtest: Introduce tests for Xilinx ZynqMP CAN controller
In reply to Vikram GarhwalThe QTests perform five tests on the Xilinx ZynqMP CAN controller:
Tests the CAN controller in loopback, sleep and snoop mode. Tests filtering of incoming CAN messages.
--- tests/qtest/xlnx-can-test.c | 360 ++++++++++++++++++++++++++++++++++++++++++++ tests/qtest/meson.build | 1 + 2 files changed, 361 insertions(+) create mode 100644 tests/qtest/xlnx-can-test.c
tests/qtest/xlnx-can-test.c new file +360 -0
@@ -0,0 +1,360 @@ +/* +* QTests for the Xilinx ZynqMP CAN controller. +* +* Copyright (c) 2020 Xilinx Inc. +* +* Written-by: Vikram Garhwal<[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "libqos/libqtest.h" + +/* Base address. */ +#define CAN0_BASE_ADDR 0xFF060000 +#define CAN1_BASE_ADDR 0xFF070000 + +/* Register addresses. */ +#define R_SRR_OFFSET 0x00 +#define R_MSR_OFFSET 0x04 +#define R_SR_OFFSET 0x18 +#define R_ISR_OFFSET 0x1C +#define R_ICR_OFFSET 0x24 +#define R_TXID_OFFSET 0x30 +#define R_TXDLC_OFFSET 0x34 +#define R_TXDATA1_OFFSET 0x38 +#define R_TXDATA2_OFFSET 0x3C +#define R_RXID_OFFSET 0x50 +#define R_RXDLC_OFFSET 0x54 +#define R_RXDATA1_OFFSET 0x58 +#define R_RXDATA2_OFFSET 0x5C +#define R_AFR 0x60 +#define R_AFMR1 0x64 +#define R_AFIR1 0x68 +#define R_AFMR2 0x6C +#define R_AFIR2 0x70 +#define R_AFMR3 0x74 +#define R_AFIR3 0x78 +#define R_AFMR4 0x7C +#define R_AFIR4 0x80 + +/* CAN modes. */ +#define CONFIG_MODE 0x00 +#define NORMAL_MODE 0x00 +#define LOOPBACK_MODE 0x02 +#define SNOOP_MODE 0x04 +#define SLEEP_MODE 0x01 +#define ENABLE_CAN (1 << 1) +#define STATUS_NORMAL_MODE (1 << 3) +#define STATUS_LOOPBACK_MODE (1 << 1) +#define STATUS_SNOOP_MODE (1 << 12) +#define STATUS_SLEEP_MODE (1 << 2) +#define ISR_TXOK (1 << 1) +#define ISR_RXOK (1 << 4) + +static void match_rx_tx_data (const uint32_t *buf_tx,const uint32_t *buf_rx, +uint8_t can_timestamp) +{ +uint16_t size =0 ; +uint8_t len =4 ; + +while (size < len) { +if (R_RXID_OFFSET +4 * size ==R_RXDLC_OFFSET ) { + g_assert_cmpint(buf_rx[size], ==, buf_tx[size] + can_timestamp); + }else { + g_assert_cmpint(buf_rx[size], ==, buf_tx[size]); + } + + size++; + } +} + +static void read_data (QTestState *qts,uint64_t can_base_addr,uint32_t *buf_rx) +{ +uint32_t int_status; + +/* Read the interrupt on CAN rx. */ + int_status =qtest_readl (qts, can_base_addr +R_ISR_OFFSET ) &ISR_RXOK ; + + g_assert_cmpint(int_status, ==, ISR_RXOK); + +/* Read the RX register data for CAN. */ + buf_rx[0 ] =qtest_readl (qts, can_base_addr +R_RXID_OFFSET ); + buf_rx[1 ] =qtest_readl (qts, can_base_addr +R_RXDLC_OFFSET ); + buf_rx[2 ] =qtest_readl (qts, can_base_addr +R_RXDATA1_OFFSET ); + buf_rx[3 ] =qtest_readl (qts, can_base_addr +R_RXDATA2_OFFSET ); + +/* Clear the RX interrupt. */ +qtest_writel (qts,CAN1_BASE_ADDR +R_ICR_OFFSET ,ISR_RXOK ); +} + +static void send_data (QTestState *qts,uint64_t can_base_addr, +const uint32_t *buf_tx) +{ +uint32_t int_status; + +/* Write the TX register data for CAN. */ +qtest_writel (qts, can_base_addr +R_TXID_OFFSET , buf_tx[0 ]); +qtest_writel (qts, can_base_addr +R_TXDLC_OFFSET , buf_tx[1 ]); +qtest_writel (qts, can_base_addr +R_TXDATA1_OFFSET , buf_tx[2 ]); +qtest_writel (qts, can_base_addr +R_TXDATA2_OFFSET , buf_tx[3 ]); + +/* Read the interrupt on CAN for tx. */ + int_status =qtest_readl (qts, can_base_addr +R_ISR_OFFSET ) &ISR_TXOK ; + + g_assert_cmpint(int_status, ==, ISR_TXOK); + +/* Clear the interrupt for tx. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_ICR_OFFSET ,ISR_TXOK ); +} + +/* +* This test will be transferring data from CAN0 and CAN1 through canbus. CAN0 +* initiate the data transfer to can-bus, CAN1 receives the data. Test compares +* the data sent from CAN0 with received on CAN1. +*/ +static void test_can_bus (void ) +{ +const uint32_t buf_tx[4 ] = {0xFF ,0x80000000 ,0x12345678 ,0x87654321 }; +uint32_t buf_rx[4 ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; +uint8_t can_timestamp =1 ; + +QTestState *qts =qtest_init ("-machine xlnx-zcu102" +" -object can-bus,id=canbus0" +" -machine xlnx-zcu102.canbus0=canbus0" +" -machine xlnx-zcu102.canbus1=canbus0" + ); + +/* Configure the CAN0 and CAN1. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN0_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN1_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); + +/* Check here if CAN0 and CAN1 are in normal mode. */ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + + status =qtest_readl (qts,CAN1_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +send_data (qts,CAN0_BASE_ADDR , buf_tx); + +read_data (qts,CAN1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, can_timestamp); + +qtest_quit (qts); +} + +/* +* This test is performing loopback mode on CAN0 and CAN1. Data sent from TX of +* each CAN0 and CAN1 are compared with RX register data for respective CAN. +*/ +static void test_can_loopback (void ) +{ +uint32_t buf_tx[4 ] = {0xFF ,0x80000000 ,0x12345678 ,0x87654321 }; +uint32_t buf_rx[4 ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; + +QTestState *qts =qtest_init ("-machine xlnx-zcu102" +" -object can-bus,id=canbus0" +" -machine xlnx-zcu102.canbus0=canbus0" +" -machine xlnx-zcu102.canbus1=canbus0" + ); + +/* Configure the CAN0 in loopback mode. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,CONFIG_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_MSR_OFFSET ,LOOPBACK_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); + +/* Check here if CAN0 is set in loopback mode. */ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + + g_assert_cmpint(status, ==, STATUS_LOOPBACK_MODE); + +send_data (qts,CAN0_BASE_ADDR , buf_tx); +read_data (qts,CAN0_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx,0 ); + +/* Configure the CAN1 in loopback mode. */ +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,CONFIG_MODE ); +qtest_writel (qts,CAN1_BASE_ADDR +R_MSR_OFFSET ,LOOPBACK_MODE ); +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); + +/* Check here if CAN1 is set in loopback mode. */ + status =qtest_readl (qts,CAN1_BASE_ADDR +R_SR_OFFSET ); + + g_assert_cmpint(status, ==, STATUS_LOOPBACK_MODE); + +send_data (qts,CAN1_BASE_ADDR , buf_tx); +read_data (qts,CAN1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx,0 ); + +qtest_quit (qts); +} + +/* +* Enable filters for CAN1. This will filter incoming messages with ID. In this +* test message will pass through filter 2. +*/ +static void test_can_filter (void ) +{ +uint32_t buf_tx[4 ] = {0x14 ,0x80000000 ,0x12345678 ,0x87654321 }; +uint32_t buf_rx[4 ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; +uint8_t can_timestamp =1 ; + +QTestState *qts =qtest_init ("-machine xlnx-zcu102" +" -object can-bus,id=canbus0" +" -machine xlnx-zcu102.canbus0=canbus0" +" -machine xlnx-zcu102.canbus1=canbus0" + ); + +/* Configure the CAN0 and CAN1. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN0_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN1_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); + +/* Check here if CAN0 and CAN1 are in normal mode. */ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + + status =qtest_readl (qts,CAN1_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +/* Set filter for CAN1 for incoming messages. */ +qtest_writel (qts,CAN1_BASE_ADDR +R_AFR ,0x0 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFMR1 ,0xF7 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFIR1 ,0x121F ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFMR2 ,0x5431 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFIR2 ,0x14 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFMR3 ,0x1234 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFIR3 ,0x5431 ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFMR4 ,0xFFF ); +qtest_writel (qts,CAN1_BASE_ADDR +R_AFIR4 ,0x1234 ); + +qtest_writel (qts,CAN1_BASE_ADDR +R_AFR ,0xF ); + +send_data (qts,CAN0_BASE_ADDR , buf_tx); + +read_data (qts,CAN1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, can_timestamp); + +qtest_quit (qts); +} + +/* Testing sleep mode on CAN0 while CAN1 is in normal mode. */ +static void test_can_sleepmode (void ) +{ +uint32_t buf_tx[4 ] = {0x14 ,0x80000000 ,0x12345678 ,0x87654321 }; +uint32_t buf_rx[4 ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; +uint8_t can_timestamp =1 ; + +QTestState *qts =qtest_init ("-machine xlnx-zcu102" +" -object can-bus,id=canbus0" +" -machine xlnx-zcu102.canbus0=canbus0" +" -machine xlnx-zcu102.canbus1=canbus0" + ); + +/* Configure the CAN0. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,CONFIG_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_MSR_OFFSET ,SLEEP_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); + +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN1_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); + +/* Check here if CAN0 is in SLEEP mode and CAN1 in normal mode. */ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_SLEEP_MODE); + + status =qtest_readl (qts,CAN1_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +send_data (qts,CAN1_BASE_ADDR , buf_tx); + +/* +* Once CAN1 sends data on can-bus. CAN0 should exit sleep mode. +* Check the CAN0 status now. It should exit the sleep mode and receive the +* incoming data. +*/ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +read_data (qts,CAN0_BASE_ADDR , buf_rx); + +match_rx_tx_data (buf_tx, buf_rx, can_timestamp); + +qtest_quit (qts); +} + +/* Testing Snoop mode on CAN0 while CAN1 is in normal mode. */ +static void test_can_snoopmode (void ) +{ +uint32_t buf_tx[4 ] = {0x14 ,0x80000000 ,0x12345678 ,0x87654321 }; +uint32_t buf_rx[4 ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; +uint8_t can_timestamp =1 ; + +QTestState *qts =qtest_init ("-machine xlnx-zcu102" +" -object can-bus,id=canbus0" +" -machine xlnx-zcu102.canbus0=canbus0" +" -machine xlnx-zcu102.canbus1=canbus0" + ); + +/* Configure the CAN0. */ +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,CONFIG_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_MSR_OFFSET ,SNOOP_MODE ); +qtest_writel (qts,CAN0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); + +qtest_writel (qts,CAN1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CAN ); +qtest_writel (qts,CAN1_BASE_ADDR +R_MSR_OFFSET ,NORMAL_MODE ); + +/* Check here if CAN0 is in SNOOP mode and CAN1 in normal mode. */ + status =qtest_readl (qts,CAN0_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_SNOOP_MODE); + + status =qtest_readl (qts,CAN1_BASE_ADDR +R_SR_OFFSET ); + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +send_data (qts,CAN1_BASE_ADDR , buf_tx); + +read_data (qts,CAN0_BASE_ADDR , buf_rx); + +match_rx_tx_data (buf_tx, buf_rx, can_timestamp); + +qtest_quit (qts); +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); + +qtest_add_func ("/net/can/can_bus" , test_can_bus); +qtest_add_func ("/net/can/can_loopback" , test_can_loopback); +qtest_add_func ("/net/can/can_filter" , test_can_filter); +qtest_add_func ("/net/can/can_test_snoopmode" , test_can_snoopmode); +qtest_add_func ("/net/can/can_test_sleepmode" , test_can_sleepmode); + +return g_test_run (); +}
tests/qtest/meson.build +1 -0
@@ -150,6 +150,7 @@ qtests_aarch64 = \ ['arm-cpu-features', 'numa-test', 'boot-serial-test', + 'xlnx-can-test', 'migration-test'] qtests_s390x = \
Signature · 1 line
2.7.4
PATCH v11 4/4 MAINTAINERS: Add maintainer entry for Xilinx ZynqMP CAN controller
In reply to Vikram Garhwal--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -1559,6 +1559,14 @@ F: hw/net/opencores_eth.c Devices ------- +Xilinx CAN +M: Vikram Garhwal <[email protected]> +M: Francisco Iglesias <[email protected]> +S: Maintained +F: hw/net/can/xlnx-* +F: include/hw/net/xlnx-* +F: tests/qtest/xlnx-can-test* + EDU M: Jiri Slaby <[email protected]> S: Maintained
Signature · 1 line
2.7.4