xref: /openbmc/qemu/hw/net/can/can_sja1000.h (revision 17083d6d1e0635371418c26b613a6fa68d392f49)
1733210e7SPavel Pisa /*
2733210e7SPavel Pisa  * CAN device - SJA1000 chip emulation for QEMU
3733210e7SPavel Pisa  *
4733210e7SPavel Pisa  * Copyright (c) 2013-2014 Jin Yang
5733210e7SPavel Pisa  * Copyright (c) 2014-2018 Pavel Pisa
6733210e7SPavel Pisa  *
7733210e7SPavel Pisa  * Initial development supported by Google GSoC 2013 from RTEMS project slot
8733210e7SPavel Pisa  *
9733210e7SPavel Pisa  * Permission is hereby granted, free of charge, to any person obtaining a copy
10733210e7SPavel Pisa  * of this software and associated documentation files (the "Software"), to deal
11733210e7SPavel Pisa  * in the Software without restriction, including without limitation the rights
12733210e7SPavel Pisa  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13733210e7SPavel Pisa  * copies of the Software, and to permit persons to whom the Software is
14733210e7SPavel Pisa  * furnished to do so, subject to the following conditions:
15733210e7SPavel Pisa  *
16733210e7SPavel Pisa  * The above copyright notice and this permission notice shall be included in
17733210e7SPavel Pisa  * all copies or substantial portions of the Software.
18733210e7SPavel Pisa  *
19733210e7SPavel Pisa  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20733210e7SPavel Pisa  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21733210e7SPavel Pisa  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22733210e7SPavel Pisa  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23733210e7SPavel Pisa  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24733210e7SPavel Pisa  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25733210e7SPavel Pisa  * THE SOFTWARE.
26733210e7SPavel Pisa  */
27733210e7SPavel Pisa #ifndef HW_CAN_SJA1000_H
28733210e7SPavel Pisa #define HW_CAN_SJA1000_H
29733210e7SPavel Pisa 
30d4842052SMarkus Armbruster #include "exec/hwaddr.h"
31733210e7SPavel Pisa #include "net/can_emu.h"
32733210e7SPavel Pisa 
33733210e7SPavel Pisa #define CAN_SJA_MEM_SIZE      128
34733210e7SPavel Pisa 
35733210e7SPavel Pisa /* The max size for a message buffer, EFF and DLC=8, DS-p39 */
36733210e7SPavel Pisa #define SJA_MSG_MAX_LEN       13
37733210e7SPavel Pisa /* The receive buffer size. */
38733210e7SPavel Pisa #define SJA_RCV_BUF_LEN       64
39733210e7SPavel Pisa 
40733210e7SPavel Pisa typedef struct CanSJA1000State {
41733210e7SPavel Pisa     /* PeliCAN state and registers sorted by address */
42733210e7SPavel Pisa     uint8_t         mode;          /* 0  .. Mode register, DS-p26 */
43733210e7SPavel Pisa                                    /* 1  .. Command register */
44733210e7SPavel Pisa     uint8_t         status_pel;    /* 2  .. Status register, p15 */
45733210e7SPavel Pisa     uint8_t         interrupt_pel; /* 3  .. Interrupt register */
46733210e7SPavel Pisa     uint8_t         interrupt_en;  /* 4  .. Interrupt Enable register */
47733210e7SPavel Pisa     uint8_t         rxmsg_cnt;     /* 29 .. RX message counter. DS-p49 */
48733210e7SPavel Pisa     uint8_t         rxbuf_start;   /* 30 .. RX buffer start address, DS-p49 */
49733210e7SPavel Pisa     uint8_t         clock;         /* 31 .. Clock Divider register, DS-p55 */
50733210e7SPavel Pisa 
51733210e7SPavel Pisa     uint8_t         code_mask[8];  /* 16~23 */
52733210e7SPavel Pisa     uint8_t         tx_buff[13];   /* 96~108 .. transmit buffer */
53733210e7SPavel Pisa                                    /* 10~19  .. transmit buffer for BasicCAN */
54733210e7SPavel Pisa 
55733210e7SPavel Pisa     uint8_t         rx_buff[SJA_RCV_BUF_LEN];  /* 32~95 .. 64bytes Rx FIFO */
56733210e7SPavel Pisa     uint32_t        rx_ptr;        /* Count by bytes. */
57733210e7SPavel Pisa     uint32_t        rx_cnt;        /* Count by bytes. */
58733210e7SPavel Pisa 
59733210e7SPavel Pisa     /* PeliCAN state and registers sorted by address */
60733210e7SPavel Pisa     uint8_t         control;       /* 0 .. Control register */
61733210e7SPavel Pisa                                    /* 1 .. Command register */
62733210e7SPavel Pisa     uint8_t         status_bas;    /* 2 .. Status register */
63733210e7SPavel Pisa     uint8_t         interrupt_bas; /* 3 .. Interrupt register */
64733210e7SPavel Pisa     uint8_t         code;          /* 4 .. Acceptance code register */
65733210e7SPavel Pisa     uint8_t         mask;          /* 5 .. Acceptance mask register */
66733210e7SPavel Pisa 
67733210e7SPavel Pisa     qemu_can_filter filter[4];
68733210e7SPavel Pisa 
69733210e7SPavel Pisa     qemu_irq          irq;
70733210e7SPavel Pisa     CanBusClientState bus_client;
71733210e7SPavel Pisa } CanSJA1000State;
72733210e7SPavel Pisa 
73733210e7SPavel Pisa /* PeliCAN mode */
74733210e7SPavel Pisa enum SJA1000_PeliCAN_regs {
75733210e7SPavel Pisa         SJA_MOD      = 0x00,    /* Mode control register */
76733210e7SPavel Pisa         SJA_CMR      = 0x01,    /* Command register */
77733210e7SPavel Pisa         SJA_SR       = 0x02,    /* Status register */
78733210e7SPavel Pisa         SJA_IR       = 0x03,    /* Interrupt register */
79733210e7SPavel Pisa         SJA_IER      = 0x04,    /* Interrupt Enable */
80733210e7SPavel Pisa         SJA_BTR0     = 0x06,    /* Bus Timing register 0 */
81733210e7SPavel Pisa         SJA_BTR1     = 0x07,    /* Bus Timing register 1 */
82733210e7SPavel Pisa         SJA_OCR      = 0x08,    /* Output Control register */
83733210e7SPavel Pisa         SJA_ALC      = 0x0b,    /* Arbitration Lost Capture */
84733210e7SPavel Pisa         SJA_ECC      = 0x0c,    /* Error Code Capture */
85733210e7SPavel Pisa         SJA_EWLR     = 0x0d,    /* Error Warning Limit */
86733210e7SPavel Pisa         SJA_RXERR    = 0x0e,    /* RX Error Counter */
87733210e7SPavel Pisa         SJA_TXERR0   = 0x0e,    /* TX Error Counter */
88733210e7SPavel Pisa         SJA_TXERR1   = 0x0f,
89733210e7SPavel Pisa         SJA_RMC      = 0x1d,    /* Rx Message Counter
90733210e7SPavel Pisa                                  * number of messages in RX FIFO
91733210e7SPavel Pisa                                  */
92733210e7SPavel Pisa         SJA_RBSA     = 0x1e,    /* Rx Buffer Start Addr
93733210e7SPavel Pisa                                  * address of current message
94733210e7SPavel Pisa                                  */
95733210e7SPavel Pisa         SJA_FRM      = 0x10,    /* Transmit Buffer
96733210e7SPavel Pisa                                  * write: Receive Buffer
97733210e7SPavel Pisa                                  * read: Frame Information
98733210e7SPavel Pisa                                  */
99733210e7SPavel Pisa /*
100733210e7SPavel Pisa  * ID bytes (11 bits in 0 and 1 for standard message or
101733210e7SPavel Pisa  *          16 bits in 0,1 and 13 bits in 2,3 for extended message)
102733210e7SPavel Pisa  *          The most significant bit of ID is placed in MSB
103733210e7SPavel Pisa  *          position of ID0 register.
104733210e7SPavel Pisa  */
105733210e7SPavel Pisa         SJA_ID0      = 0x11,    /* ID for standard and extended frames */
106733210e7SPavel Pisa         SJA_ID1      = 0x12,
107733210e7SPavel Pisa         SJA_ID2      = 0x13,    /* ID cont. for extended frames */
108733210e7SPavel Pisa         SJA_ID3      = 0x14,
109733210e7SPavel Pisa 
110733210e7SPavel Pisa         SJA_DATS     = 0x13,    /* Data start standard frame */
111733210e7SPavel Pisa         SJA_DATE     = 0x15,    /* Data start extended frame */
112733210e7SPavel Pisa         SJA_ACR0     = 0x10,    /* Acceptance Code (4 bytes) in RESET mode */
113733210e7SPavel Pisa         SJA_AMR0     = 0x14,    /* Acceptance Mask (4 bytes) in RESET mode */
114733210e7SPavel Pisa         SJA_PeliCAN_AC_LEN = 4, /* 4 bytes */
115733210e7SPavel Pisa         SJA_CDR      = 0x1f     /* Clock Divider */
116733210e7SPavel Pisa };
117733210e7SPavel Pisa 
118733210e7SPavel Pisa 
119733210e7SPavel Pisa /* BasicCAN  mode */
120733210e7SPavel Pisa enum SJA1000_BasicCAN_regs {
121733210e7SPavel Pisa         SJA_BCAN_CTR = 0x00,    /* Control register */
122733210e7SPavel Pisa         SJA_BCAN_CMR = 0x01,    /* Command register */
123733210e7SPavel Pisa         SJA_BCAN_SR  = 0x02,    /* Status register */
124733210e7SPavel Pisa         SJA_BCAN_IR  = 0x03     /* Interrupt register */
125733210e7SPavel Pisa };
126733210e7SPavel Pisa 
127733210e7SPavel Pisa void can_sja_hardware_reset(CanSJA1000State *s);
128733210e7SPavel Pisa 
129733210e7SPavel Pisa void can_sja_mem_write(CanSJA1000State *s, hwaddr addr, uint64_t val,
130733210e7SPavel Pisa                        unsigned size);
131733210e7SPavel Pisa 
132733210e7SPavel Pisa uint64_t can_sja_mem_read(CanSJA1000State *s, hwaddr addr, unsigned size);
133733210e7SPavel Pisa 
134733210e7SPavel Pisa int can_sja_connect_to_bus(CanSJA1000State *s, CanBusState *bus);
135733210e7SPavel Pisa 
136733210e7SPavel Pisa void can_sja_disconnect(CanSJA1000State *s);
137733210e7SPavel Pisa 
138733210e7SPavel Pisa int can_sja_init(CanSJA1000State *s, qemu_irq irq);
139733210e7SPavel Pisa 
140*767cc9a9SPhilippe Mathieu-Daudé bool can_sja_can_receive(CanBusClientState *client);
141733210e7SPavel Pisa 
142733210e7SPavel Pisa ssize_t can_sja_receive(CanBusClientState *client,
143733210e7SPavel Pisa                         const qemu_can_frame *frames, size_t frames_cnt);
144733210e7SPavel Pisa 
145733210e7SPavel Pisa extern const VMStateDescription vmstate_can_sja;
146733210e7SPavel Pisa 
147733210e7SPavel Pisa #endif
148