From nobody Fri Apr 19 09:45:06 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=codethink.co.uk Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1678970527607400.02613775659245; Thu, 16 Mar 2023 05:42:07 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pcmvO-00072W-AM; Thu, 16 Mar 2023 08:41:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pcmvF-00071x-SV for qemu-devel@nongnu.org; Thu, 16 Mar 2023 08:41:35 -0400 Received: from imap5.colo.codethink.co.uk ([78.40.148.171]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pcmvA-000516-SM for qemu-devel@nongnu.org; Thu, 16 Mar 2023 08:41:32 -0400 Received: from [167.98.27.226] (helo=rainbowdash) by imap5.colo.codethink.co.uk with esmtpsa (Exim 4.94.2 #2 (Debian)) id 1pcmuw-002fc1-Gq; Thu, 16 Mar 2023 12:41:14 +0000 Received: from ben by rainbowdash with local (Exim 4.96) (envelope-from ) id 1pcmuw-000ccm-0R; Thu, 16 Mar 2023 12:41:14 +0000 From: Ben Dooks To: qemu-devel@nongnu.org, pisa@cmp.felk.cvut.cz Cc: jasowang@redhat.com, fnu.vikram@xilinx.com, nazar.kazakov@codethink.co.uk, lawrence.hunter@codethink.co.uk, frank.chang@sifive.com, paul.walmsley@sifive.com, Ben Dooks Subject: [PATCH v2] hw/net/can: Add mcp25625 model Date: Thu, 16 Mar 2023 12:41:13 +0000 Message-Id: <20230316124113.148463-1-ben.dooks@codethink.co.uk> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=78.40.148.171; envelope-from=ben@codethink.co.uk; helo=imap5.colo.codethink.co.uk X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1678970530988100001 Content-Type: text/plain; charset="utf-8" From: Ben Dooks 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=3Dcanbus0 \ -object can-host-socketcan,id=3Dcanhost0,if=3Dvcan0,canbus=3Dcanbus0 \ -global driver=3Dmcp25625,property=3Dcanbus,value=3Dcanbus0 There is tracing support with --trace "*mcp25*" Signed-off-by: Ben Dooks Co-developed-by: Nazar Kazakov Signed-off-by: Nazar Kazakov Co-developed-by: Lawrence Hunter Signed-off-by: Lawrence Hunter Reviewed-by: Frank Chang Acked-by: Pavel Pisa Tested-by: Pavel Pisa --- 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 diff --git a/hw/net/Kconfig b/hw/net/Kconfig index 18c7851efe..36f1cd1f3e 100644 --- a/hw/net/Kconfig +++ b/hw/net/Kconfig @@ -137,6 +137,11 @@ config ROCKER config CAN_BUS bool =20 +config CAN_MCP25625 + bool + default y if SSI + select CAN_BUS + config CAN_SJA1000 bool default y if PCI_DEVICES diff --git a/hw/net/can/can_mcp25625.c b/hw/net/can/can_mcp25625.c new file mode 100644 index 0000000000..bc035721d9 --- /dev/null +++ b/hw/net/can/can_mcp25625.c @@ -0,0 +1,1312 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * CAN device - MCP25625 chip model + * + * Copyright (c) 2022 SiFive, Inc. + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "chardev/char.h" +#include "hw/irq.h" +#include "migration/vmstate.h" +#include "net/can_emu.h" +#include "hw/ssi/ssi.h" +#include "hw/qdev-properties.h" +#include "hw/net/can_mcp25625.h" + +#include "trace.h" + +/* + * Register definitons, addresses and offsets. Since the RX and TX registe= rs + * exist in multiple banks we don't tend to refer to these by absolute add= resses + * as each bank has the same offsets for each register. + * + * note: filter registers read back 0 unless in config mode + */ + +#define BANK_TXB(__b) ((__b) + 3) +#define BANK_RXB(__b) ((__b) + 6) + +#define ADDR_RXB0SIDH (0x61) + +#define OFF_RXFSIDH (0x0) +#define OFF_RXFSIDL (0x1) +#define OFF_RXFEID8 (0x2) +#define OFF_RXFEID0 (0x3) + +#define RXFSIDL_EXIDE (1 << 3) +#define RXFSIDL_WRITEMASK (0xE0 | RXFSIDL_EXIDE | 0x3) + +struct rxfilter { + uint8_t data[4]; +}; + +#define BFPCTRL_WRITEMASK (0x3F) +#define BFPCTRL_B0BFM (1 << 0) +#define BFPCTRL_B0BFE (1 << 2) +#define BFPCTRL_B0BFS (1 << 4) + +#define TXRTSCTRL_WRITEMASK (0x7) + +/* rx mask register offsets (read back 0 unless in config mode) */ +#define OFF_RXMSIDH (0x0) +#define OFF_RXMSIDL (0x1) +#define OFF_RXMEID8 (0x2) +#define OFF_RXMEID0 (0x3) + +#define RXMSIDL_WRITEMASK (0xE3) + +struct rxmask { + uint8_t data[4]; +}; + +#define CNF3_WRITEMASK (0xC7) + +#define ADDR_TXB0SIDH (0x31) + +#define OFF_TXBCTRL (0x0) +#define TXBCTRL_ABTF (1 << 6) +#define TXBCTRL_MLOA (1 << 5) +#define TXBCTRL_TXREQ (1 << 3) +#define TXBCTRL_TXP1 (1 << 1) +#define TXBCTRL_TXP0 (1 << 0) +#define TXBCTRL_TXP (TXBCTRL_TXP1 | TXBCTRL_TXP0) +#define TXBCTRL_WRITEMASK (TXBCTRL_TXREQ | TXBCTRL_TXP) + +#define OFF_TXBSIDH (0x1) + +#define OFF_TXBSIDL (0x2) +#define TXBSIDL_EXIDE (1 << 3) +/* bits 7..5 are SID[2:0], bits 1..0 are EID[17:16] */ +#define TXBSIDL_WRITEMASK (0xE0 | TXBSIDL_EXIDE | 0x3) + +#define OFF_TXBEID8 (0x3) /* this is EID[15:8] */ +#define OFF_TXBEID0 (0x4) /* this is EID[7:0] */ + +#define OFF_TXBDLC (0x5) /* bits 3..0 are DLC */ +#define TXBDLC_RTR (1 << 6) +#define TXBDLC_WRITEMASK (TXBDLC_RTR | 0xF) + +#define OFF_TXBDATA (0x6) + +struct txbuff { + uint8_t data[14]; +}; + +#define OFF_RXBCTRL (0x0) +#define RXBCTRL_RXM_ANY (3 << 5) +#define RXBCTRL_RXM_VALID (0 << 5) +#define RXBCTRL_RXM_MASK (3 << 5) + +#define RXBCTRL_RXRTR (1 << 3) +#define RXBCTRL_BUKT (1 << 2) +#define RXBCTRL_BUKT1 (1 << 1) +/* RXBCTRL0, bit0 =3D filter hit for message */ +/* RXBCTRL1, bits 2..0 show filter hit */ +#define RXBCTRL0_WRITEMASK (RXBCTRL_RXM_MASK | RXBCTRL_BUKT) +#define RXBCTRL1_WRITEMASK (RXBCTRL_RXM_MASK) + +#define OFF_RXBSIDH (0x1) +/* bits 7..0 =3D SID[10:3] */ + +#define OFF_RXBSIDL (0x2) +#define RXBSIDL_SRR (1 << 4) +#define RXBSIDL_IDE (1 << 3) +/* bits 7..5 =3D SID[2:0], bits 1..0 =3D EID[17:16] */ + +#define OFF_RXBEID8 (0x3) /* bits 7..0 =3D EID[15:8] */ +#define OFF_RXBEID0 (0x4) /* bits 7..0 =3D EID[7:0] */ + +#define OFF_RXBDLC (0x5) +#define RXBDLC_RTR (1 << 6) /* bits 3..0 =3D number of bytes= */ + +#define OFF_RXBDATA (0x6) + +struct rxbuff { + uint8_t data[14]; +}; + +#define EFLG_RX1OVR (1 << 7) +#define EFLG_RX0OVR (1 << 6) +/* don't think we need any other errors */ +#define EFLG_WRITEMASK (EFLG_RX1OVR | EFLG_RX0OVR) + +#define IRQ_MERR (1 << 7) +#define IRQ_WAKE (1 << 6) +#define IRQ_ERR (1 << 5) +#define IRQ_TX(__n) (1 << ((__n) + 2)) +#define IRQ_RX(__n) (1 << (__n)) + +#define OFF_CANSTAT (0x0E) +#define OFF_CANCTRL (0x0F) + +#define CANSTAT_ICOD_MASK (0x7 << 1) + +#define CTRL_REQ_NORMAL (0 << 5) +#define CTRL_REQ_SLEEP (1 << 5) +#define CTRL_REQ_LOOP (2 << 5) +#define CTRL_REQ_LISTEN (3 << 5) +#define CTRL_REQ_CFG (4 << 5) +#define CTRL_REQ_MASK (7 << 5) + +#define CTRL_CLK_EN (1 << 2) +#define CTRL_DEF_CLK (CTRL_CLK_EN | 0x3) +#define CTRL_ABAT (1 << 4) + +/* + * The state of the current spi access, from SSI_INSTRUCTION where the + * system is waiting for the instruction byte to then what steps are + * taken given the instruction received. + */ +enum mcp_ssi_state { + SSI_INSTRUCTION, + SSI_ADDRESS, + SSI_WR_DATA, + SSI_RD_DATA, + SSI_WAIT, + SSI_RD_STATUS, + SSI_RD_RXSTATUS, + SSI_MODIFY_ADDR, + SSI_MODIFY_MASK, + SSI_MODIFY_DATA, +}; + +struct MCP25625State; + +typedef struct MCP25625State { + SSIPeripheralClass parent_class; + + qemu_irq irq; + qemu_irq rxb_irq[2]; + + CanBusClientState bus_client; + CanBusState *canbus; + const char *trace_name; + + /* spi bus state */ + + enum mcp_ssi_state ssi_state; + bool ssi_write; + bool ssi_only_cfg_rd; + bool ssi_can_bitmodify; + uint8_t ssi_addr; + uint8_t ssi_modify_mask; + uint8_t *ssi_ptr; + uint8_t ssi_writemask; + uint8_t ssi_rxbuff; + + /* internal state */ + + uint32_t lastirq; + + /* registers */ + + uint8_t canstat; + uint8_t canctrl; + uint8_t bfpctrl; + uint8_t txrtsctrl; + uint8_t tec; + uint8_t rec; + uint8_t cnfs[4]; /* note, cnfs[0] is not used */ + uint8_t caninte; + uint8_t canintf; + uint8_t eflg; + + struct txbuff txbuffs[3]; + struct rxbuff rxbuffs[2]; + struct rxfilter rxfilters[6]; + struct rxmask rxmasks[2]; +} MCP25625State; + +#define trace_name(__s) ((__s)->trace_name) + +static ssize_t mcp25625_can_receive(CanBusClientState *client, + const qemu_can_frame *buf, size_t fram= es_cnt); +static void mcp25625_do_rts(MCP25625State *s, uint32_t tx); +static void mcp25625_update_canctrl(MCP25625State *s, bool wakeup_happened= ); +static void mcp25625_update_irqs(MCP25625State *s, unsigned flags); + +static bool mcp25625_is_in_cfg(MCP25625State *s) +{ + return (s->canstat & CTRL_REQ_MASK) =3D=3D CTRL_REQ_CFG; +} + +static bool mcp25625_is_in_sleep(MCP25625State *s) +{ + return (s->canstat & CTRL_REQ_MASK) =3D=3D CTRL_REQ_SLEEP; +} + +static bool mcp25625_is_in_normal(MCP25625State *s) +{ + return (s->canstat & CTRL_REQ_MASK) =3D=3D CTRL_REQ_NORMAL; +} + +static bool mcp25625_is_in_loopback(MCP25625State *s) +{ + return (s->canstat & CTRL_REQ_MASK) =3D=3D CTRL_REQ_LOOP; +} + +static bool mcp25625_mode_exists(uint8_t mode) +{ + return (mode =3D=3D CTRL_REQ_NORMAL) || + (mode =3D=3D CTRL_REQ_SLEEP) || + (mode =3D=3D CTRL_REQ_LOOP) || + (mode =3D=3D CTRL_REQ_LISTEN) || + (mode =3D=3D CTRL_REQ_CFG); +} + +/* + * Map the regsiter number to our internal storage and set various + * state data about how it can be modified and when. Since the registers + * are in banks, we don't use a linear array. The three TX and two RX banks + * are pretty much the same. + * + * Registers that can be modified with the BIT-MODIFY command: + * TXBxCTRL, RXBxCTRL, CNF[1-3], CANINTE, CANINTF, EFLG, CANCTRL, + * BFPCTRL and TXRTSCTRL + * + * We currently assume that the A7 bit is jut not used even through it + * is shown on the SPI diagrams. The last register is 0x7f so we wrap + * around at that point back to 0x0. + */ +static uint8_t *addr_to_reg(MCP25625State *s, unsigned reg) +{ + unsigned low =3D reg & 0xf; + unsigned bank =3D (reg >> 4) & 0x7; + + s->ssi_only_cfg_rd =3D false; + s->ssi_can_bitmodify =3D false; + s->ssi_writemask =3D 0xff; + + /* CANSTAT and CANCTRL are in all register banks, deal with these firs= t */ + if (low =3D=3D OFF_CANSTAT) { + s->ssi_writemask =3D 0x0; + return &s->canstat; + } + + if (low =3D=3D OFF_CANCTRL) { + s->ssi_can_bitmodify =3D true; + return &s->canctrl; + } + + /* check for rxfilter controls next */ + if (bank < 2 && low < 12) { + if (bank =3D=3D 1) { + low +=3D 3 * 4; + } + + if (low % 4 =3D=3D OFF_RXFSIDL) { + s->ssi_writemask =3D RXFSIDL_WRITEMASK; + } + + s->ssi_only_cfg_rd =3D true; + return &s->rxfilters[low / 4].data[low % 4]; + } + + /* process banks for the high-part of the address */ + switch (bank) { + case 0x0: + /* left with BPFCTRL and TXRTSCTRL here */ + s->ssi_can_bitmodify =3D true; + if (low =3D=3D 12) { + s->ssi_writemask =3D BFPCTRL_WRITEMASK; + return &s->bfpctrl; + } else if (low =3D=3D 13) { + s->ssi_writemask =3D TXRTSCTRL_WRITEMASK; + s->ssi_only_cfg_rd =3D true; + return &s->txrtsctrl; + } + break; + + case 0x01: + /* left with TEC and REC registers */ + if (low =3D=3D 12) { + s->ssi_writemask =3D 0x0; + return &s->tec; + } else if (low =3D=3D 13) { + s->ssi_writemask =3D 0x0; + return &s->rec; + } + break; + + case 0x02: + if (low < 8) { + if (low % 4 =3D=3D 1) { + s->ssi_writemask =3D RXMSIDL_WRITEMASK; + } + s->ssi_only_cfg_rd =3D true; + return &s->rxmasks[low / 4].data[low % 4]; + } + + s->ssi_can_bitmodify =3D true; + switch (low) { + case 8: + s->ssi_writemask =3D CNF3_WRITEMASK; + s->ssi_only_cfg_rd =3D true; + return &s->cnfs[3]; + case 9: + s->ssi_only_cfg_rd =3D true; + return &s->cnfs[2]; + case 10: + s->ssi_only_cfg_rd =3D true; + return &s->cnfs[1]; + case 11: + return &s->caninte; + case 12: + return &s->canintf; + case 13: + s->ssi_writemask =3D EFLG_WRITEMASK; + return &s->eflg; + } + break; + + case BANK_TXB(0) ... BANK_TXB(2): + if (low =3D=3D OFF_TXBCTRL) { + s->ssi_writemask =3D TXBCTRL_WRITEMASK; + s->ssi_can_bitmodify =3D true; + } else if (low =3D=3D OFF_TXBSIDL) { + s->ssi_writemask =3D TXBSIDL_WRITEMASK; + } else if (low =3D=3D OFF_TXBDLC) { + s->ssi_writemask =3D TXBDLC_WRITEMASK; + } + return &s->txbuffs[bank - BANK_TXB(0)].data[low]; + + case BANK_RXB(0) ... BANK_RXB(1): + if (low =3D=3D OFF_RXFSIDH) { + s->ssi_writemask =3D + (bank =3D=3D BANK_RXB(0) ? RXBCTRL0_WRITEMASK : RXBCTR= L1_WRITEMASK); + s->ssi_can_bitmodify =3D true; + } else { + s->ssi_writemask =3D 0x0; + } + return &s->rxbuffs[bank - BANK_RXB(0)].data[low]; + } + + /* we shouldn't really get here */ + return NULL; +} + +static void mcp25625_clear_rx(MCP25625State *s) +{ + trace_mcp25625_rx_clear(trace_name(s), s->ssi_rxbuff); + s->canintf &=3D ~IRQ_RX(s->ssi_rxbuff); + + mcp25625_update_irqs(s, 0x0); +} + +static int mcp25625_cs(SSIPeripheral *ss, bool select) +{ + MCP25625State *s =3D MCP25625(ss); + + if (select) { + /* chip-select has gone inactive */ + if (s->ssi_rxbuff !=3D 0xff) { + mcp25625_clear_rx(s); + } + } else { + /* chip-select is going active */ + s->ssi_rxbuff =3D 0xff; + s->ssi_state =3D SSI_INSTRUCTION; + } + + return 0; +} + +static unsigned irq_flags_to_icod(uint32_t val) +{ + if (val & IRQ_ERR) { + return 1 << 1; + } else if (val & IRQ_WAKE) { + return 2 << 1; + } else if (val & IRQ_TX(0)) { + return 3 << 1; + } else if (val & IRQ_TX(1)) { + return 4 << 1; + } else if (val & IRQ_TX(2)) { + return 5 << 1; + } else if (val & IRQ_RX(0)) { + return 6 << 1; + } else if (val & IRQ_RX(1)) { + return 7 << 1; + } + + return 0; +} + +/* + * RXBFx pins can be mapped to respective buffer full status of the + * CANINTF bits (3.7.3 in datasheet). The pin can either be disabled + * an output or an indicator if the relevant buffer is full. + */ +static void mcp25625_update_rxbf(MCP25625State *s, unsigned buff) +{ + uint8_t bfpctrl =3D s->bfpctrl; + unsigned val =3D 1; + + /* buffer 1 is just buffer 0 controls shifted down by 1 */ + if (buff !=3D 0) { + bfpctrl >>=3D 1; + } + + if (bfpctrl & BFPCTRL_B0BFE) { + if (bfpctrl & BFPCTRL_B0BFM) { + val =3D (s->canintf & IRQ_RX(buff)) ? 0 : 1; + } else { + val =3D (bfpctrl & BFPCTRL_B0BFS) ? 1 : 0; + } + } else { + val =3D 1; + } + + qemu_set_irq(s->rxb_irq[buff], val); +} + +static void mcp25625_update_irqs(MCP25625State *s, unsigned flags) +{ + uint8_t newirq; + + trace_mcp25625_irq_update(trace_name(s), flags); + s->canintf |=3D flags; + newirq =3D s->canintf & s->caninte; + + if (s->lastirq !=3D newirq) { + trace_mcp25625_irq_change(trace_name(s), s->lastirq, newirq); + + s->lastirq =3D newirq; + s->canstat &=3D ~CANSTAT_ICOD_MASK; + if (newirq !=3D 0x0) { + s->canstat |=3D irq_flags_to_icod(newirq); + } + + if ((newirq & IRQ_WAKE) && mcp25625_is_in_sleep(s)) { + mcp25625_update_canctrl(s, true); + } + } + + mcp25625_update_rxbf(s, 0); + mcp25625_update_rxbf(s, 1); + qemu_set_irq(s->irq, newirq ? 1 : 0); +} + +static void mcp25625_got_reset(MCP25625State *s) +{ + /* + * most of tx/rx buffs have undefined values after reset, + * set them to 0 for simplicity and ease of initialisation. + */ + memset(&s->txbuffs, 0x00, sizeof(s->txbuffs)); + memset(&s->rxbuffs, 0x00, sizeof(s->rxbuffs)); + memset(&s->rxfilters, 0x00, sizeof(s->rxfilters)); + memset(&s->rxmasks, 0x00, sizeof(s->rxmasks)); + + /* reset all irqs */ + s->caninte =3D 0x0; + s->canintf =3D 0x0; + s->lastirq =3D ~(uint32_t)0x0; + mcp25625_update_irqs(s, 0x00); + s->eflg =3D 0x0; + + s->bfpctrl =3D 0x0; + s->txrtsctrl =3D 0x0; + s->tec =3D 0x0; + s->rec =3D 0x0; + memset(&s->cnfs, 0x00, sizeof(s->cnfs)); + + /* put controller into config mode now */ + s->canstat =3D CTRL_REQ_CFG; + s->canctrl =3D CTRL_REQ_CFG | CTRL_DEF_CLK; +} + +static void mcp25625_send_txb(MCP25625State *s, unsigned buff) +{ + struct txbuff *txb =3D &s->txbuffs[buff]; + qemu_can_frame frame; + qemu_canid_t id; + unsigned len; + + if (!mcp25625_is_in_normal(s) && !mcp25625_is_in_loopback(s)) { + return; + } + + len =3D txb->data[OFF_TXBDLC] & 0xf; + if (len > 8) { + len =3D 8; + } + trace_mcp25625_send_txb(trace_name(s), buff, len); + + id =3D txb->data[OFF_TXBSIDH] << 3; + id |=3D (txb->data[OFF_TXBSIDL] & 0xE0) >> 5; + + if (txb->data[OFF_TXBSIDL] & TXBSIDL_EXIDE) { + id <<=3D 18; + id |=3D QEMU_CAN_EFF_FLAG; + id |=3D (qemu_canid_t)(txb->data[OFF_TXBSIDL] & 3) << 16; + id |=3D (qemu_canid_t)txb->data[OFF_TXBEID8] << 8; + id |=3D (qemu_canid_t)txb->data[OFF_TXBEID0]; + } + + if (txb->data[OFF_TXBDLC] & TXBDLC_RTR) { + id |=3D QEMU_CAN_RTR_FLAG; + } + + memcpy(frame.data, txb->data + OFF_TXBDATA, len); + frame.can_dlc =3D len; + frame.can_id =3D id; + frame.flags =3D 0; + + if (mcp25625_is_in_normal(s)) { + can_bus_client_send(&s->bus_client, &frame, 1); + } else { + mcp25625_can_receive(&s->bus_client, &frame, 1); + } + + txb->data[OFF_TXBCTRL] &=3D ~TXBCTRL_TXREQ; + mcp25625_update_irqs(s, IRQ_TX(buff)); +} + +static void mcp25625_update_txbctrl(MCP25625State *s, unsigned txbuff) +{ + struct txbuff *txb =3D &s->txbuffs[txbuff]; + uint8_t txbctrl =3D txb->data[OFF_TXBCTRL]; + + if (txbctrl & TXBCTRL_TXREQ) { + mcp25625_send_txb(s, txbuff); + } +} + +static void mcp25625_update_canctrl(MCP25625State *s, bool wakeup_happened) +{ + uint8_t ctrl_op, stat_op; + int txbuff; + + /* check and clear aborted buffers */ + if (s->canctrl & CTRL_ABAT) { + for (txbuff =3D 0; txbuff < 2; txbuff++) { + struct txbuff *txb =3D &s->txbuffs[txbuff]; + uint8_t *txbctrl =3D &txb->data[OFF_TXBCTRL]; + if (*txbctrl & TXBCTRL_TXREQ) { + *txbctrl &=3D ~TXBCTRL_TXREQ; + *txbctrl |=3D TXBCTRL_ABTF; + } + } + } + + ctrl_op =3D s->canctrl & CTRL_REQ_MASK; + stat_op =3D s->canstat & CTRL_REQ_MASK; + + if (!mcp25625_mode_exists(ctrl_op)) { + ctrl_op =3D stat_op; + s->canctrl &=3D ~CTRL_REQ_MASK; + s->canctrl |=3D ctrl_op; + } + + /* check to see if we should change the device mode */ + if (ctrl_op !=3D stat_op && (wakeup_happened || stat_op !=3D CTRL_REQ_= SLEEP)) { + uint8_t rts =3D 0; + + trace_mcp25625_change_mode(trace_name(s), stat_op, ctrl_op); + + /* write the new mode to canstat */ + s->canstat &=3D ~CTRL_REQ_MASK; + s->canstat |=3D ctrl_op; + + /* send pending tx if possible */ + for (txbuff =3D 0; txbuff < 2; txbuff++) { + struct txbuff *txb =3D &s->txbuffs[txbuff]; + uint8_t txbctrl =3D txb->data[OFF_TXBCTRL]; + + if (txbctrl & TXBCTRL_TXREQ) { + rts |=3D (1 << txbuff); + } + } + mcp25625_do_rts(s, rts); + } + + /* mcp25625 sets canctrl.reqop to listen mode during sleep */ + if (s->canstat & CTRL_REQ_SLEEP) { + s->canctrl &=3D ~CTRL_REQ_MASK; + s->canctrl |=3D CTRL_REQ_LISTEN; + } +} + +static void mc25625_update_reg(MCP25625State *s, uint8_t val) +{ + trace_mpc25652_reg_update(trace_name(s), s->ssi_addr, *s->ssi_ptr, val= ); + *s->ssi_ptr &=3D ~s->ssi_writemask; + *s->ssi_ptr |=3D val & s->ssi_writemask; + + if (s->ssi_ptr =3D=3D &s->caninte || s->ssi_ptr =3D=3D &s->canintf) { + mcp25625_update_irqs(s, 0x0); + } else if (s->ssi_ptr =3D=3D &s->bfpctrl) { + mcp25625_update_rxbf(s, 0); + mcp25625_update_rxbf(s, 1); + } else if (s->ssi_ptr =3D=3D &s->canctrl) { + mcp25625_update_canctrl(s, false); + } else if (s->ssi_ptr =3D=3D &s->rxbuffs[0].data[OFF_RXBCTRL]) { + *s->ssi_ptr &=3D ~RXBCTRL_BUKT1; + *s->ssi_ptr |=3D (*s->ssi_ptr & RXBCTRL_BUKT) ? RXBCTRL_BUKT1 : 0; + } else { + unsigned txbuff; + + for (txbuff =3D 0; txbuff <=3D 2; txbuff++) { + if (s->ssi_ptr =3D=3D &s->txbuffs[txbuff].data[OFF_TXBCTRL]) { + if (s->canctrl & CTRL_ABAT) { + *s->ssi_ptr &=3D ~TXBCTRL_TXREQ; + } else { + *s->ssi_ptr &=3D ~TXBCTRL_ABTF; + mcp25625_update_txbctrl(s, txbuff); + } + break; + } + } + } +} + +static uint8_t mcp25625_get_txp(MCP25625State *s, int buff) +{ + struct txbuff *txb =3D &s->txbuffs[buff]; + return txb->data[OFF_TXBCTRL] & TXBCTRL_TXP; +} + +static void mcp25625_do_rts(MCP25625State *s, uint32_t tx) +{ + int ordered[3], ordered_used =3D 0, buf_place; + int i, curr_buf; + + trace_mcp25625_do_rts(trace_name(s), tx); + + if (s->canctrl & CTRL_ABAT) { + return; + } + + for (curr_buf =3D 2; curr_buf >=3D 0; curr_buf--) { + /* skip non-requested bufs */ + if (!(tx & (1 << curr_buf))) { + continue; + } + + /* find a place in `ordered` for current buf */ + for (buf_place =3D 0; buf_place < ordered_used; buf_place++) { + uint8_t our_txp =3D mcp25625_get_txp(s, curr_buf); + uint8_t other_txp =3D mcp25625_get_txp(s, ordered[buf_place]); + + if (our_txp > other_txp) { + break; + } + } + + /* make room for current buf */ + for (i =3D ordered_used; i >=3D buf_place + 1; i--) { + ordered[i] =3D ordered[i - 1]; + } + + /* finally insert current buf in `ordered` */ + ordered[buf_place] =3D curr_buf; + ordered_used +=3D 1; + } + + for (i =3D 0; i < ordered_used; i++) { + struct txbuff *txb =3D &s->txbuffs[ordered[i]]; + txb->data[OFF_TXBCTRL] &=3D ~TXBCTRL_ABTF; + txb->data[OFF_TXBCTRL] |=3D TXBCTRL_TXREQ; + mcp25625_update_txbctrl(s, ordered[i]); + } +} + +static uint32_t mcp25625_get_status(MCP25625State *s) +{ + uint32_t result; + unsigned txb; + + /* lower two bits map directly to irqs for rx0/rx1 */ + result =3D (s->canintf & (IRQ_RX(0) | IRQ_RX(1))); + + for (txb =3D 0; txb < 3; txb++) { + if (s->canintf & IRQ_TX(txb)) { + result |=3D (1 << (3 + txb * 2)); + } + + if (s->txbuffs[txb].data[OFF_TXBCTRL] & TXBCTRL_TXREQ) { + result |=3D (1 << (2 + txb * 2)); + } + } + + return result; +} + +static uint32_t mcp25625_get_rxstatus(MCP25625State *s) +{ + struct rxbuff *rxbuff =3D NULL; + uint32_t result; + + /* + * [7:6] 0=3Dnone, 1=3Drxb0 full, 2=3Drxb1 full, 3=3Dboth full + * [4:3] type of frame received (rx0 priority) + * [2:0] filter hit for frame + */ + + result =3D (s->canintf & (IRQ_RX(0) | IRQ_RX(1))) << 6; + + if (s->canintf & IRQ_RX(0)) { + rxbuff =3D &s->rxbuffs[0]; + } else if (s->canintf & IRQ_RX(1)) { + rxbuff =3D &s->rxbuffs[1]; + } + + if (rxbuff) { + uint8_t rxbctrl =3D rxbuff->data[OFF_RXBCTRL]; + + /* type of frame */ + if (rxbctrl & RXBCTRL_RXRTR) { + result |=3D (1 << 3); + } + + if (rxbuff->data[OFF_RXBSIDL] & RXBSIDL_IDE) { + result |=3D (1 << 4); + } + + /* filter hit depends on buffer */ + if (s->canintf & IRQ_RX(0)) { + result |=3D rxbctrl & 1; + } else { + result |=3D rxbctrl & 7; + if ((result & 7) <=3D 1) { + result +=3D 6; + } + } + } + + return result; +} + +/* + * spi transfer function, used to move data into or out of the device. + * + * Depending on the spi internal state, which is either awaiting a command + * or actioning the data from the command. The device has shortcut commands + * to point at various registers, or modify given registers. + * + * Note, a lot of these commands are only used here so defines aren't used + * for the command numbers. + */ +static uint32_t mcp25625_transfer8(SSIPeripheral *ss, uint32_t tx) +{ + MCP25625State *s =3D MCP25625(ss); + uint32_t ret =3D 0xffff; + + trace_mcp25625_transfer8(trace_name(s), s->ssi_state, s->ssi_addr, tx); + + switch (s->ssi_state) { + case SSI_INSTRUCTION: + s->ssi_write =3D false; + + /* next bit is in datasheet order, not numerical order */ + if (tx =3D=3D (3 << 6)) { + mcp25625_got_reset(s); + s->ssi_state =3D SSI_WAIT; + } else if (tx =3D=3D 3) { + s->ssi_state =3D SSI_ADDRESS; + } else if ((tx & 1) =3D=3D 0 && (tx >=3D 0x90 && tx <=3D 0x96)) { + uint32_t rxptr =3D (tx - 0x90) >> 1; + + /* read-buffer command */ + + s->ssi_addr =3D ADDR_RXB0SIDH + (rxptr >> 1) * 0x10; + s->ssi_addr +=3D (rxptr & 1) ? 5 : 0; + s->ssi_state =3D SSI_RD_DATA; + s->ssi_rxbuff =3D rxptr >> 1; + } else if (tx =3D=3D 2) { /* write-data instruction */ + s->ssi_state =3D SSI_ADDRESS; + s->ssi_write =3D true; + } else if (tx >=3D 0x40 && tx <=3D 0x45) { + uint32_t tx_off =3D tx - 0x40; + + /* load tx-buffer instruction -> point to relevant tx buffer */ + s->ssi_state =3D SSI_WR_DATA; + s->ssi_write =3D true; + s->ssi_addr =3D ADDR_TXB0SIDH + (tx_off >> 1) * 0x10; + s->ssi_addr +=3D (tx_off & 1) ? 5 : 0; + } else if (tx >=3D 0x80 && tx <=3D 0x87) { + s->ssi_state =3D SSI_WAIT; + mcp25625_do_rts(s, tx); + } else if (tx =3D=3D 0xa0) { + s->ssi_state =3D SSI_RD_STATUS; + } else if (tx =3D=3D 0xb0) { + s->ssi_state =3D SSI_RD_RXSTATUS; + } else if (tx =3D=3D 5) { + s->ssi_state =3D SSI_MODIFY_ADDR; + } else { + qemu_log_mask(LOG_GUEST_ERROR, "%s: spi invalid command %02x\n= ", + trace_name(s), tx); + + trace_mcp25625_invalid_cmd(trace_name(s), tx); + s->ssi_addr =3D 0x00; + s->ssi_state =3D SSI_WAIT; + } + + ret =3D 0xff; + break; + + case SSI_RD_STATUS: + ret =3D mcp25625_get_status(s); + break; + + case SSI_RD_RXSTATUS: + ret =3D mcp25625_get_rxstatus(s); + break; + + case SSI_WAIT: + ret =3D 0xff; + break; + + case SSI_ADDRESS: + s->ssi_state =3D (s->ssi_write) ? SSI_WR_DATA : SSI_RD_DATA; + s->ssi_addr =3D tx; + ret =3D 0xff; + break; + + case SSI_WR_DATA: + s->ssi_ptr =3D addr_to_reg(s, s->ssi_addr); + if (s->ssi_ptr) { + mc25625_update_reg(s, tx); + } + s->ssi_addr +=3D 1; + ret =3D 0xff; + break; + + case SSI_RD_DATA: + s->ssi_ptr =3D addr_to_reg(s, s->ssi_addr); + if (s->ssi_only_cfg_rd && !mcp25625_is_in_cfg(s)) { + ret =3D 0x00; + } else if (s->ssi_ptr) { + ret =3D *s->ssi_ptr; + } else { + ret =3D 0xff; + } + s->ssi_addr +=3D 1; + break; + + /* modify register - awaiting address */ + case SSI_MODIFY_ADDR: + s->ssi_addr =3D tx; + s->ssi_ptr =3D addr_to_reg(s, tx); + s->ssi_state =3D SSI_MODIFY_MASK; + ret =3D 0xff; + break; + + /* modify register - awaiting mask data */ + case SSI_MODIFY_MASK: + s->ssi_modify_mask =3D tx; + s->ssi_state =3D SSI_MODIFY_DATA; + ret =3D 0xff; + break; + + case SSI_MODIFY_DATA: + if (!s->ssi_can_bitmodify) { + s->ssi_modify_mask =3D 0xff; + } + if (s->ssi_ptr) { + tx &=3D s->ssi_modify_mask; + tx |=3D *s->ssi_ptr & ~s->ssi_modify_mask; + mc25625_update_reg(s, tx); + } + + ret =3D 0xff; + break; + + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: spi invalid state (ssi_state= =3D%d)\n", + trace_name(s), s->ssi_state); + s->ssi_state =3D SSI_WAIT; + ret =3D 0xff; + } + + trace_mcp25625_transfer8_return(trace_name(s), s->ssi_state, s->ssi_ad= dr, + tx, ret); + return ret; +} + +#define client_to_mcp(__c) container_of(__c, MCP25625State, bus_client) + +static bool mcp25625_check_filter(const qemu_can_frame *frame, + struct rxmask *mask, + struct rxfilter *filt) +{ + uint32_t mask32, filt32, data; + bool frame_is_extended =3D !!(frame->can_id & QEMU_CAN_EFF_FLAG); + bool filter_is_extended =3D !!(filt->data[OFF_RXFSIDL] & RXFSIDL_EXIDE= ); + + if (frame_is_extended !=3D filter_is_extended) { + return false; + } + + mask32 =3D + (mask->data[OFF_RXMSIDH] << 21) + | ((mask->data[OFF_RXMSIDL] & 0xE0) << 13) + | ((mask->data[OFF_RXMSIDL] & 0x3) << 16) + | (mask->data[OFF_RXMEID8] << 8) + | (mask->data[OFF_RXMEID0] << 0); + + filt32 =3D + (filt->data[OFF_RXFSIDH] << 21) + | ((filt->data[OFF_RXFSIDL] & 0xE0) << 13) + | ((filt->data[OFF_RXFSIDL] & 0x3) << 16) + | (filt->data[OFF_RXFEID8] << 8) + | (filt->data[OFF_RXFEID0] << 0); + + if (frame_is_extended) { + data =3D frame->can_id & QEMU_CAN_EFF_MASK; + } else { + /* accept data bytes by default if the frame is too short */ + uint8_t len =3D (frame->can_id & QEMU_CAN_RTR_FLAG) ? 0 : frame->c= an_dlc; + uint8_t data0 =3D len > 0 ? frame->data[0] : filt->data[OFF_RXFEID= 8]; + uint8_t data1 =3D len > 1 ? frame->data[1] : filt->data[OFF_RXFEID= 0]; + + data =3D (frame->can_id & QEMU_CAN_SFF_MASK) << 18 | data0 << 8 | = data1; + filt32 &=3D ~(3 << 16); + } + + return (~mask32 | ~(filt32 ^ data)) =3D=3D 0xFFFFFFFF; +} + +/* + * mcp25625_check_filters: + * for rxb0, rxmask0 and rxfilters[0..1] are used + * for rxb1, rxmask1 and rxfilters[2..5] are used + */ +static int mcp25625_check_filters(MCP25625State *s, + const qemu_can_frame *frame, + unsigned rxb) +{ + struct rxbuff *rxbuff =3D &s->rxbuffs[rxb]; + uint8_t rxbctrl =3D rxbuff->data[OFF_RXBCTRL]; + struct rxmask *mask =3D &s->rxmasks[rxb]; + unsigned nr, end; + + /* if we're not bothering with filters, fake hit on filter 0 */ + if ((rxbctrl & RXBCTRL_RXM_MASK) =3D=3D RXBCTRL_RXM_ANY) { + return 0; + } + + /* note, these are invalid states, so just ignore */ + if ((rxbctrl & RXBCTRL_RXM_MASK) !=3D RXBCTRL_RXM_VALID) { + return -2; + } + + if (rxb =3D=3D 0) { + nr =3D 0; + end =3D 1; + } else { + nr =3D 2; + end =3D 5; + } + + for (; nr <=3D end; nr++) { + if (mcp25625_check_filter(frame, mask, &s->rxfilters[nr])) { + return nr; + } + } + + return -1; +} + +static void mcp25625_rx_into_buf(MCP25625State *s, + const qemu_can_frame *frame, + unsigned buffnr, int filthit) +{ + struct rxbuff *rxbuff =3D &s->rxbuffs[buffnr]; + qemu_canid_t e_id, sidl, id, q_id =3D frame->can_id; + unsigned len =3D frame->can_dlc; + + trace_mcp25625_rx_buf(trace_name(s), buffnr, q_id, len); + + if (len > 8) + return; + + if (q_id & QEMU_CAN_EFF_FLAG) { + e_id =3D q_id & QEMU_CAN_EFF_MASK; + id =3D e_id >> 18; + } else { + id =3D q_id & QEMU_CAN_SFF_MASK; + e_id =3D 0; + } + + sidl =3D (id & 0x7) << 5; + if (q_id & QEMU_CAN_EFF_FLAG) { + sidl |=3D RXBSIDL_IDE; + sidl |=3D (e_id >> 16) & 3; + } else { + if (q_id & QEMU_CAN_RTR_FLAG) { + sidl |=3D RXBSIDL_SRR; + } + } + + rxbuff->data[OFF_RXBSIDL] =3D sidl; + rxbuff->data[OFF_RXBSIDH] =3D id >> 3; + rxbuff->data[OFF_RXBEID8] =3D (e_id >> 8) & 0xff; + rxbuff->data[OFF_RXBEID0] =3D e_id & 0xff; + + rxbuff->data[OFF_RXBDLC] =3D len; + if ((q_id & QEMU_CAN_RTR_FLAG) && (q_id & QEMU_CAN_EFF_FLAG)) { + rxbuff->data[OFF_RXBDLC] |=3D RXBDLC_RTR; + } + + if (buffnr =3D=3D 0) { + rxbuff->data[OFF_RXBCTRL] &=3D ~((1 << 0) | RXBCTRL_RXRTR); + + if (filthit > 0) { + rxbuff->data[OFF_RXBCTRL] |=3D (1 << 0); + } + } else { + rxbuff->data[OFF_RXBCTRL] &=3D ~(7 | RXBCTRL_RXRTR); + rxbuff->data[OFF_RXBCTRL] |=3D filthit; + } + + if (q_id & QEMU_CAN_RTR_FLAG) { + rxbuff->data[OFF_RXBCTRL] |=3D RXBCTRL_RXRTR; + } + + /* finally copy the frame data in */ + memcpy(rxbuff->data + OFF_RXBDATA, frame->data, len); + + mcp25625_update_irqs(s, IRQ_RX(buffnr)); +} + +#define is_full(_s, _b) (s->canintf & IRQ_RX(_b)) +#define is_bukt(_s) (s->rxbuffs[0].data[OFF_RXBCTRL] & RXBCTRL_BUKT) + +static inline void mcp25625_set_eflag(MCP25625State *s, unsigned flag) +{ + s->eflg |=3D flag; + mcp25625_update_irqs(s, IRQ_ERR); +} + +static ssize_t mcp25625_can_receive(CanBusClientState *client, + const qemu_can_frame *buf, + size_t frames_cnt) +{ + MCP25625State *s =3D client_to_mcp(client); + int ret; + + /* support receiving only one frame at a time */ + if (frames_cnt !=3D 1) { + return -1; + } + + /* we don't support error frames or buf->flags */ + if (buf->can_id & QEMU_CAN_ERR_FLAG || buf->flags !=3D 0) { + return -1; + } + + if (mcp25625_is_in_sleep(s)) { + mcp25625_update_irqs(s, IRQ_WAKE); + return 0; + } + + /* initially, does rxb0 pass this */ + ret =3D mcp25625_check_filters(s, buf, 0); + if (ret >=3D 0) { + if (is_full(s, 0)) { + /* if we can roll over into rxb1? */ + + if (is_bukt(s)) { + if (is_full(s, 1)) { + /* generate overflow on rxb0. real hw does this */ + mcp25625_set_eflag(s, EFLG_RX0OVR); + } else { + /* dump this into rxb1 */ + mcp25625_rx_into_buf(s, buf, 1, ret); + } + } else { + /* generate overflow on rxb0 */ + mcp25625_set_eflag(s, EFLG_RX0OVR); + } + } else { + /* dump this into rxb0 */ + mcp25625_rx_into_buf(s, buf, 0, ret); + } + } else { + /* see if rxb1 will take this */ + + ret =3D mcp25625_check_filters(s, buf, 1); + if (ret >=3D 0) { + if (is_full(s, 1)) { + /* generate overflow on rxb1 */ + mcp25625_set_eflag(s, EFLG_RX1OVR); + } else { + /* dump into rxb1 */ + mcp25625_rx_into_buf(s, buf, 1, ret); + } + } + } + + return 0; +} + +static bool mcp25625_can_can_receive(CanBusClientState *client) +{ + MCP25625State *s =3D client_to_mcp(client); + + return !mcp25625_is_in_cfg(s) && !mcp25625_is_in_loopback(s); +} + +static CanBusClientInfo mcp25625_bus_client_info =3D { + .can_receive =3D mcp25625_can_can_receive, + .receive =3D mcp25625_can_receive, +}; + +static void mcp25625_realize(SSIPeripheral *ss, Error **errp) +{ + MCP25625State *s =3D MCP25625(ss); + DeviceState *dev =3D DEVICE(ss); + int ret; + + s->trace_name =3D object_get_canonical_path(OBJECT(s)); + + if (s->canbus) { + s->bus_client.info =3D &mcp25625_bus_client_info; + ret =3D can_bus_insert_client(s->canbus, &s->bus_client); + if (ret) { + error_setg(errp, "cannot connect mcp25625 to canbus"); + return; + } + } + + /* initialise the output pins for irq and the rxb0/rxb1 pins + * used to indicate buffer status if enabled. Note we don't + * use the tx trigger pins yet. + */ + qdev_init_gpio_out_named(dev, &s->irq, "irq", 1); + qdev_init_gpio_out_named(dev, &s->rxb_irq[0], "rxbf0", 1); + qdev_init_gpio_out_named(dev, &s->rxb_irq[1], "rxbf1", 1); +} + +static int mcp25625_post_load(void *op, int ver) +{ + MCP25625State *s =3D op; + + /* setting ssi_ptr also resets other non-saved ssi data */ + s->ssi_ptr =3D addr_to_reg(s, s->ssi_addr); + + /* reset irq state */ + s->lastirq =3D ~(uint32_t)0x0; + mcp25625_update_irqs(s, 0x00); + + return 0; +} + +/* + * Use the mcp25625_got_reset() call to reset the state, which is + * probably good enough for now. + */ +static void mcp25625_reset(DeviceState *d) +{ + MCP25625State *s =3D MCP25625(d); + + mcp25625_got_reset(s); +} + +static Property mcp25625_properties[] =3D { + DEFINE_PROP_LINK("canbus", MCP25625State, canbus, TYPE_CAN_BUS, + CanBusState *), + DEFINE_PROP_END_OF_LIST(), +}; + +static const VMStateDescription vmstate_mcp25625_txbuff =3D { + .name =3D TYPE_MCP25625 "/txbuff", + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8_ARRAY(data, struct txbuff, 14), + }, +}; + +static const VMStateDescription vmstate_mcp25625_rxbuff =3D { + .name =3D TYPE_MCP25625 "/rxbuff", + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8_ARRAY(data, struct rxbuff, 14), + }, +}; + +static const VMStateDescription vmstate_mcp25625_rxmask =3D { + .name =3D TYPE_MCP25625 "/rxmask", + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8_ARRAY(data, struct rxmask, 4), + }, +}; + +static const VMStateDescription vmstate_mcp25625_rxfilter =3D { + .name =3D TYPE_MCP25625 "/rxfilter", + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT8_ARRAY(data, struct rxfilter, 4), + }, +}; + +static const VMStateDescription vmstate_mcp25625 =3D { + .name =3D TYPE_MCP25625, + .version_id =3D 1, + .minimum_version_id =3D 1, + .post_load =3D mcp25625_post_load, + .fields =3D (VMStateField[]) { + /* + * think the ssi transaction state should be store here + * as not sure if we can suspend/migrate over this + */ + VMSTATE_BOOL(ssi_write, MCP25625State), + VMSTATE_UINT32(ssi_state, MCP25625State), + VMSTATE_UINT8(ssi_addr, MCP25625State), + VMSTATE_UINT8(ssi_rxbuff, MCP25625State), + VMSTATE_UINT8(ssi_modify_mask, MCP25625State), + VMSTATE_UINT8(canstat, MCP25625State), + VMSTATE_UINT8(canctrl, MCP25625State), + VMSTATE_UINT8(bfpctrl, MCP25625State), + VMSTATE_UINT8(txrtsctrl, MCP25625State), + VMSTATE_UINT8(tec, MCP25625State), + VMSTATE_UINT8(rec, MCP25625State), + VMSTATE_UINT8(caninte, MCP25625State), + VMSTATE_UINT8(canintf, MCP25625State), + VMSTATE_UINT8(eflg, MCP25625State), + VMSTATE_UINT8_ARRAY(cnfs, MCP25625State, 4), + VMSTATE_STRUCT_ARRAY(txbuffs, MCP25625State, 3, 1, + vmstate_mcp25625_txbuff, struct txbuff), + VMSTATE_STRUCT_ARRAY(rxbuffs, MCP25625State, 2, 1, + vmstate_mcp25625_rxbuff, struct rxbuff), + VMSTATE_STRUCT_ARRAY(rxmasks, MCP25625State, 2, 1, + vmstate_mcp25625_rxmask, struct rxmask), + VMSTATE_STRUCT_ARRAY(rxfilters, MCP25625State, 6, 1, + vmstate_mcp25625_rxfilter, struct rxfilter), + }, +}; + +static void mcp25625_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + SSIPeripheralClass *k =3D SSI_PERIPHERAL_CLASS(klass); + + k->realize =3D mcp25625_realize; + k->transfer =3D mcp25625_transfer8; + k->set_cs =3D mcp25625_cs; + k->cs_polarity =3D SSI_CS_LOW; + dc->vmsd =3D &vmstate_mcp25625; + dc->desc =3D "Microchip MCP25625 CAN-SPI"; + device_class_set_props(dc, mcp25625_properties); + dc->reset =3D mcp25625_reset; +} + +static const TypeInfo mcp25625_info =3D { + .name =3D TYPE_MCP25625, + .parent =3D TYPE_SSI_PERIPHERAL, + .instance_size =3D sizeof(MCP25625State), + .class_init =3D mcp25625_class_init, +}; + +static void mcp25625_register_types(void) +{ + type_register_static(&mcp25625_info); +} + +type_init(mcp25625_register_types); diff --git a/hw/net/can/meson.build b/hw/net/can/meson.build index 8fabbd9ee6..cfec993b52 100644 --- a/hw/net/can/meson.build +++ b/hw/net/can/meson.build @@ -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')) diff --git a/hw/net/can/trace-events b/hw/net/can/trace-events index 8346a98ab5..99dcea7f9b 100644 --- a/hw/net/can/trace-events +++ b/hw/net/can/trace-events @@ -1,3 +1,16 @@ +# mcp25625.c +mcp25625_send_txb(const char *dev, unsigned buff, unsigned len) "%s: buffe= r %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, uint3= 2_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: chang= e 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 =3D 0x%08x" diff --git a/include/hw/net/can_mcp25625.h b/include/hw/net/can_mcp25625.h new file mode 100644 index 0000000000..5ec82eb4f7 --- /dev/null +++ b/include/hw/net/can_mcp25625.h @@ -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) --=20 2.39.2