can_sja1000.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * CAN device - SJA1000 chip emulation for QEMU
  3. *
  4. * Copyright (c) 2013-2014 Jin Yang
  5. * Copyright (c) 2014-2018 Pavel Pisa
  6. *
  7. * Initial development supported by Google GSoC 2013 from RTEMS project slot
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #ifndef HW_CAN_SJA1000_H
  28. #define HW_CAN_SJA1000_H
  29. #include "exec/hwaddr.h"
  30. #include "net/can_emu.h"
  31. #define CAN_SJA_MEM_SIZE 128
  32. /* The max size for a message buffer, EFF and DLC=8, DS-p39 */
  33. #define SJA_MSG_MAX_LEN 13
  34. /* The receive buffer size. */
  35. #define SJA_RCV_BUF_LEN 64
  36. typedef struct CanSJA1000State {
  37. /* PeliCAN state and registers sorted by address */
  38. uint8_t mode; /* 0 .. Mode register, DS-p26 */
  39. /* 1 .. Command register */
  40. uint8_t status_pel; /* 2 .. Status register, p15 */
  41. uint8_t interrupt_pel; /* 3 .. Interrupt register */
  42. uint8_t interrupt_en; /* 4 .. Interrupt Enable register */
  43. uint8_t rxmsg_cnt; /* 29 .. RX message counter. DS-p49 */
  44. uint8_t rxbuf_start; /* 30 .. RX buffer start address, DS-p49 */
  45. uint8_t clock; /* 31 .. Clock Divider register, DS-p55 */
  46. uint8_t code_mask[8]; /* 16~23 */
  47. uint8_t tx_buff[13]; /* 96~108 .. transmit buffer */
  48. /* 10~19 .. transmit buffer for BasicCAN */
  49. uint8_t rx_buff[SJA_RCV_BUF_LEN]; /* 32~95 .. 64bytes Rx FIFO */
  50. uint32_t rx_ptr; /* Count by bytes. */
  51. uint32_t rx_cnt; /* Count by bytes. */
  52. /* PeliCAN state and registers sorted by address */
  53. uint8_t control; /* 0 .. Control register */
  54. /* 1 .. Command register */
  55. uint8_t status_bas; /* 2 .. Status register */
  56. uint8_t interrupt_bas; /* 3 .. Interrupt register */
  57. uint8_t code; /* 4 .. Acceptance code register */
  58. uint8_t mask; /* 5 .. Acceptance mask register */
  59. qemu_can_filter filter[4];
  60. qemu_irq irq;
  61. CanBusClientState bus_client;
  62. } CanSJA1000State;
  63. /* PeliCAN mode */
  64. enum SJA1000_PeliCAN_regs {
  65. SJA_MOD = 0x00, /* Mode control register */
  66. SJA_CMR = 0x01, /* Command register */
  67. SJA_SR = 0x02, /* Status register */
  68. SJA_IR = 0x03, /* Interrupt register */
  69. SJA_IER = 0x04, /* Interrupt Enable */
  70. SJA_BTR0 = 0x06, /* Bus Timing register 0 */
  71. SJA_BTR1 = 0x07, /* Bus Timing register 1 */
  72. SJA_OCR = 0x08, /* Output Control register */
  73. SJA_ALC = 0x0b, /* Arbitration Lost Capture */
  74. SJA_ECC = 0x0c, /* Error Code Capture */
  75. SJA_EWLR = 0x0d, /* Error Warning Limit */
  76. SJA_RXERR = 0x0e, /* RX Error Counter */
  77. SJA_TXERR0 = 0x0e, /* TX Error Counter */
  78. SJA_TXERR1 = 0x0f,
  79. SJA_RMC = 0x1d, /* Rx Message Counter
  80. * number of messages in RX FIFO
  81. */
  82. SJA_RBSA = 0x1e, /* Rx Buffer Start Addr
  83. * address of current message
  84. */
  85. SJA_FRM = 0x10, /* Transmit Buffer
  86. * write: Receive Buffer
  87. * read: Frame Information
  88. */
  89. /*
  90. * ID bytes (11 bits in 0 and 1 for standard message or
  91. * 16 bits in 0,1 and 13 bits in 2,3 for extended message)
  92. * The most significant bit of ID is placed in MSB
  93. * position of ID0 register.
  94. */
  95. SJA_ID0 = 0x11, /* ID for standard and extended frames */
  96. SJA_ID1 = 0x12,
  97. SJA_ID2 = 0x13, /* ID cont. for extended frames */
  98. SJA_ID3 = 0x14,
  99. SJA_DATS = 0x13, /* Data start standard frame */
  100. SJA_DATE = 0x15, /* Data start extended frame */
  101. SJA_ACR0 = 0x10, /* Acceptance Code (4 bytes) in RESET mode */
  102. SJA_AMR0 = 0x14, /* Acceptance Mask (4 bytes) in RESET mode */
  103. SJA_PeliCAN_AC_LEN = 4, /* 4 bytes */
  104. SJA_CDR = 0x1f /* Clock Divider */
  105. };
  106. /* BasicCAN mode */
  107. enum SJA1000_BasicCAN_regs {
  108. SJA_BCAN_CTR = 0x00, /* Control register */
  109. SJA_BCAN_CMR = 0x01, /* Command register */
  110. SJA_BCAN_SR = 0x02, /* Status register */
  111. SJA_BCAN_IR = 0x03 /* Interrupt register */
  112. };
  113. void can_sja_hardware_reset(CanSJA1000State *s);
  114. void can_sja_mem_write(CanSJA1000State *s, hwaddr addr, uint64_t val,
  115. unsigned size);
  116. uint64_t can_sja_mem_read(CanSJA1000State *s, hwaddr addr, unsigned size);
  117. int can_sja_connect_to_bus(CanSJA1000State *s, CanBusState *bus);
  118. void can_sja_disconnect(CanSJA1000State *s);
  119. int can_sja_init(CanSJA1000State *s, qemu_irq irq);
  120. int can_sja_can_receive(CanBusClientState *client);
  121. ssize_t can_sja_receive(CanBusClientState *client,
  122. const qemu_can_frame *frames, size_t frames_cnt);
  123. extern const VMStateDescription vmstate_can_sja;
  124. #endif