can_kvaser_pci.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Kvaser PCI CAN device (SJA1000 based) emulation
  3. *
  4. * Copyright (c) 2013-2014 Jin Yang
  5. * Copyright (c) 2014-2018 Pavel Pisa
  6. *
  7. * Partially based on educational PCIexpress APOHW hardware
  8. * emulator used fro class A0B36APO at CTU FEE course by
  9. * Rostislav Lisovy and Pavel Pisa
  10. *
  11. * Initial development supported by Google GSoC 2013 from RTEMS project slot
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. #include "qemu/osdep.h"
  32. #include "qemu/event_notifier.h"
  33. #include "qemu/module.h"
  34. #include "qemu/thread.h"
  35. #include "qemu/sockets.h"
  36. #include "qapi/error.h"
  37. #include "chardev/char.h"
  38. #include "hw/irq.h"
  39. #include "hw/pci/pci.h"
  40. #include "hw/qdev-properties.h"
  41. #include "migration/vmstate.h"
  42. #include "net/can_emu.h"
  43. #include "can_sja1000.h"
  44. #include "qom/object.h"
  45. #define TYPE_CAN_PCI_DEV "kvaser_pci"
  46. typedef struct KvaserPCIState KvaserPCIState;
  47. DECLARE_INSTANCE_CHECKER(KvaserPCIState, KVASER_PCI_DEV,
  48. TYPE_CAN_PCI_DEV)
  49. #ifndef KVASER_PCI_VENDOR_ID1
  50. #define KVASER_PCI_VENDOR_ID1 0x10e8 /* the PCI device and vendor IDs */
  51. #endif
  52. #ifndef KVASER_PCI_DEVICE_ID1
  53. #define KVASER_PCI_DEVICE_ID1 0x8406
  54. #endif
  55. #define KVASER_PCI_S5920_RANGE 0x80
  56. #define KVASER_PCI_SJA_RANGE 0x80
  57. #define KVASER_PCI_XILINX_RANGE 0x8
  58. #define KVASER_PCI_BYTES_PER_SJA 0x20
  59. #define S5920_OMB 0x0C
  60. #define S5920_IMB 0x1C
  61. #define S5920_MBEF 0x34
  62. #define S5920_INTCSR 0x38
  63. #define S5920_RCR 0x3C
  64. #define S5920_PTCR 0x60
  65. #define S5920_INTCSR_ADDON_INTENABLE_M 0x2000
  66. #define S5920_INTCSR_INTERRUPT_ASSERTED_M 0x800000
  67. #define KVASER_PCI_XILINX_VERINT 7 /* Lower nibble simulate interrupts,
  68. high nibble version number. */
  69. #define KVASER_PCI_XILINX_VERSION_NUMBER 13
  70. struct KvaserPCIState {
  71. /*< private >*/
  72. PCIDevice dev;
  73. /*< public >*/
  74. MemoryRegion s5920_io;
  75. MemoryRegion sja_io;
  76. MemoryRegion xilinx_io;
  77. CanSJA1000State sja_state;
  78. qemu_irq irq;
  79. uint32_t s5920_intcsr;
  80. uint32_t s5920_irqstate;
  81. CanBusState *canbus;
  82. };
  83. static void kvaser_pci_irq_handler(void *opaque, int irq_num, int level)
  84. {
  85. KvaserPCIState *d = (KvaserPCIState *)opaque;
  86. d->s5920_irqstate = level;
  87. if (d->s5920_intcsr & S5920_INTCSR_ADDON_INTENABLE_M) {
  88. pci_set_irq(&d->dev, level);
  89. }
  90. }
  91. static void kvaser_pci_reset(DeviceState *dev)
  92. {
  93. KvaserPCIState *d = KVASER_PCI_DEV(dev);
  94. CanSJA1000State *s = &d->sja_state;
  95. can_sja_hardware_reset(s);
  96. }
  97. static uint64_t kvaser_pci_s5920_io_read(void *opaque, hwaddr addr,
  98. unsigned size)
  99. {
  100. KvaserPCIState *d = opaque;
  101. uint64_t val;
  102. switch (addr) {
  103. case S5920_INTCSR:
  104. val = d->s5920_intcsr;
  105. val &= ~S5920_INTCSR_INTERRUPT_ASSERTED_M;
  106. if (d->s5920_irqstate) {
  107. val |= S5920_INTCSR_INTERRUPT_ASSERTED_M;
  108. }
  109. return val;
  110. }
  111. return 0;
  112. }
  113. static void kvaser_pci_s5920_io_write(void *opaque, hwaddr addr, uint64_t data,
  114. unsigned size)
  115. {
  116. KvaserPCIState *d = opaque;
  117. switch (addr) {
  118. case S5920_INTCSR:
  119. if (d->s5920_irqstate &&
  120. ((d->s5920_intcsr ^ data) & S5920_INTCSR_ADDON_INTENABLE_M)) {
  121. pci_set_irq(&d->dev, !!(data & S5920_INTCSR_ADDON_INTENABLE_M));
  122. }
  123. d->s5920_intcsr = data;
  124. break;
  125. }
  126. }
  127. static uint64_t kvaser_pci_sja_io_read(void *opaque, hwaddr addr, unsigned size)
  128. {
  129. KvaserPCIState *d = opaque;
  130. CanSJA1000State *s = &d->sja_state;
  131. if (addr >= KVASER_PCI_BYTES_PER_SJA) {
  132. return 0;
  133. }
  134. return can_sja_mem_read(s, addr, size);
  135. }
  136. static void kvaser_pci_sja_io_write(void *opaque, hwaddr addr, uint64_t data,
  137. unsigned size)
  138. {
  139. KvaserPCIState *d = opaque;
  140. CanSJA1000State *s = &d->sja_state;
  141. if (addr >= KVASER_PCI_BYTES_PER_SJA) {
  142. return;
  143. }
  144. can_sja_mem_write(s, addr, data, size);
  145. }
  146. static uint64_t kvaser_pci_xilinx_io_read(void *opaque, hwaddr addr,
  147. unsigned size)
  148. {
  149. switch (addr) {
  150. case KVASER_PCI_XILINX_VERINT:
  151. return (KVASER_PCI_XILINX_VERSION_NUMBER << 4) | 0;
  152. }
  153. return 0;
  154. }
  155. static void kvaser_pci_xilinx_io_write(void *opaque, hwaddr addr, uint64_t data,
  156. unsigned size)
  157. {
  158. }
  159. static const MemoryRegionOps kvaser_pci_s5920_io_ops = {
  160. .read = kvaser_pci_s5920_io_read,
  161. .write = kvaser_pci_s5920_io_write,
  162. .endianness = DEVICE_LITTLE_ENDIAN,
  163. .impl = {
  164. .min_access_size = 4,
  165. .max_access_size = 4,
  166. },
  167. };
  168. static const MemoryRegionOps kvaser_pci_sja_io_ops = {
  169. .read = kvaser_pci_sja_io_read,
  170. .write = kvaser_pci_sja_io_write,
  171. .endianness = DEVICE_LITTLE_ENDIAN,
  172. .impl = {
  173. .max_access_size = 1,
  174. },
  175. };
  176. static const MemoryRegionOps kvaser_pci_xilinx_io_ops = {
  177. .read = kvaser_pci_xilinx_io_read,
  178. .write = kvaser_pci_xilinx_io_write,
  179. .endianness = DEVICE_LITTLE_ENDIAN,
  180. .impl = {
  181. .max_access_size = 1,
  182. },
  183. };
  184. static void kvaser_pci_realize(PCIDevice *pci_dev, Error **errp)
  185. {
  186. KvaserPCIState *d = KVASER_PCI_DEV(pci_dev);
  187. CanSJA1000State *s = &d->sja_state;
  188. uint8_t *pci_conf;
  189. pci_conf = pci_dev->config;
  190. pci_conf[PCI_INTERRUPT_PIN] = 0x01; /* interrupt pin A */
  191. d->irq = qemu_allocate_irq(kvaser_pci_irq_handler, d, 0);
  192. can_sja_init(s, d->irq);
  193. if (can_sja_connect_to_bus(s, d->canbus) < 0) {
  194. error_setg(errp, "can_sja_connect_to_bus failed");
  195. return;
  196. }
  197. memory_region_init_io(&d->s5920_io, OBJECT(d), &kvaser_pci_s5920_io_ops,
  198. d, "kvaser_pci-s5920", KVASER_PCI_S5920_RANGE);
  199. memory_region_init_io(&d->sja_io, OBJECT(d), &kvaser_pci_sja_io_ops,
  200. d, "kvaser_pci-sja", KVASER_PCI_SJA_RANGE);
  201. memory_region_init_io(&d->xilinx_io, OBJECT(d), &kvaser_pci_xilinx_io_ops,
  202. d, "kvaser_pci-xilinx", KVASER_PCI_XILINX_RANGE);
  203. pci_register_bar(&d->dev, /*BAR*/ 0, PCI_BASE_ADDRESS_SPACE_IO,
  204. &d->s5920_io);
  205. pci_register_bar(&d->dev, /*BAR*/ 1, PCI_BASE_ADDRESS_SPACE_IO,
  206. &d->sja_io);
  207. pci_register_bar(&d->dev, /*BAR*/ 2, PCI_BASE_ADDRESS_SPACE_IO,
  208. &d->xilinx_io);
  209. }
  210. static void kvaser_pci_exit(PCIDevice *pci_dev)
  211. {
  212. KvaserPCIState *d = KVASER_PCI_DEV(pci_dev);
  213. CanSJA1000State *s = &d->sja_state;
  214. can_sja_disconnect(s);
  215. qemu_free_irq(d->irq);
  216. }
  217. static const VMStateDescription vmstate_kvaser_pci = {
  218. .name = "kvaser_pci",
  219. .version_id = 1,
  220. .minimum_version_id = 1,
  221. .minimum_version_id_old = 1,
  222. .fields = (VMStateField[]) {
  223. VMSTATE_PCI_DEVICE(dev, KvaserPCIState),
  224. /* Load this before sja_state. */
  225. VMSTATE_UINT32(s5920_intcsr, KvaserPCIState),
  226. VMSTATE_STRUCT(sja_state, KvaserPCIState, 0, vmstate_can_sja,
  227. CanSJA1000State),
  228. VMSTATE_END_OF_LIST()
  229. }
  230. };
  231. static void kvaser_pci_instance_init(Object *obj)
  232. {
  233. KvaserPCIState *d = KVASER_PCI_DEV(obj);
  234. object_property_add_link(obj, "canbus", TYPE_CAN_BUS,
  235. (Object **)&d->canbus,
  236. qdev_prop_allow_set_link_before_realize,
  237. 0);
  238. }
  239. static void kvaser_pci_class_init(ObjectClass *klass, void *data)
  240. {
  241. DeviceClass *dc = DEVICE_CLASS(klass);
  242. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  243. k->realize = kvaser_pci_realize;
  244. k->exit = kvaser_pci_exit;
  245. k->vendor_id = KVASER_PCI_VENDOR_ID1;
  246. k->device_id = KVASER_PCI_DEVICE_ID1;
  247. k->revision = 0x00;
  248. k->class_id = 0x00ff00;
  249. dc->desc = "Kvaser PCICANx";
  250. dc->vmsd = &vmstate_kvaser_pci;
  251. dc->reset = kvaser_pci_reset;
  252. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  253. }
  254. static const TypeInfo kvaser_pci_info = {
  255. .name = TYPE_CAN_PCI_DEV,
  256. .parent = TYPE_PCI_DEVICE,
  257. .instance_size = sizeof(KvaserPCIState),
  258. .class_init = kvaser_pci_class_init,
  259. .instance_init = kvaser_pci_instance_init,
  260. .interfaces = (InterfaceInfo[]) {
  261. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  262. { },
  263. },
  264. };
  265. static void kvaser_pci_register_types(void)
  266. {
  267. type_register_static(&kvaser_pci_info);
  268. }
  269. type_init(kvaser_pci_register_types)