lm32_hwsetup.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * LatticeMico32 hwsetup helper functions.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * These are helper functions for creating the hardware description blob used
  21. * in the Theobroma's uClinux port.
  22. */
  23. #ifndef QEMU_HW_LM32_HWSETUP_H
  24. #define QEMU_HW_LM32_HWSETUP_H
  25. #include "qemu/cutils.h"
  26. #include "hw/loader.h"
  27. typedef struct {
  28. void *data;
  29. void *ptr;
  30. } HWSetup;
  31. enum hwsetup_tag {
  32. HWSETUP_TAG_EOL = 0,
  33. HWSETUP_TAG_CPU = 1,
  34. HWSETUP_TAG_ASRAM = 2,
  35. HWSETUP_TAG_FLASH = 3,
  36. HWSETUP_TAG_SDRAM = 4,
  37. HWSETUP_TAG_OCM = 5,
  38. HWSETUP_TAG_DDR_SDRAM = 6,
  39. HWSETUP_TAG_DDR2_SDRAM = 7,
  40. HWSETUP_TAG_TIMER = 8,
  41. HWSETUP_TAG_UART = 9,
  42. HWSETUP_TAG_GPIO = 10,
  43. HWSETUP_TAG_TRISPEEDMAC = 11,
  44. HWSETUP_TAG_I2CM = 12,
  45. HWSETUP_TAG_LEDS = 13,
  46. HWSETUP_TAG_7SEG = 14,
  47. HWSETUP_TAG_SPI_S = 15,
  48. HWSETUP_TAG_SPI_M = 16,
  49. };
  50. static inline HWSetup *hwsetup_init(void)
  51. {
  52. HWSetup *hw;
  53. hw = g_malloc(sizeof(HWSetup));
  54. hw->data = g_malloc0(TARGET_PAGE_SIZE);
  55. hw->ptr = hw->data;
  56. return hw;
  57. }
  58. static inline void hwsetup_free(HWSetup *hw)
  59. {
  60. g_free(hw->data);
  61. g_free(hw);
  62. }
  63. static inline void hwsetup_create_rom(HWSetup *hw,
  64. hwaddr base)
  65. {
  66. rom_add_blob("hwsetup", hw->data, TARGET_PAGE_SIZE,
  67. TARGET_PAGE_SIZE, base, NULL, NULL, NULL, NULL, true);
  68. }
  69. static inline void hwsetup_add_u8(HWSetup *hw, uint8_t u)
  70. {
  71. stb_p(hw->ptr, u);
  72. hw->ptr += 1;
  73. }
  74. static inline void hwsetup_add_u32(HWSetup *hw, uint32_t u)
  75. {
  76. stl_p(hw->ptr, u);
  77. hw->ptr += 4;
  78. }
  79. static inline void hwsetup_add_tag(HWSetup *hw, enum hwsetup_tag t)
  80. {
  81. stl_p(hw->ptr, t);
  82. hw->ptr += 4;
  83. }
  84. static inline void hwsetup_add_str(HWSetup *hw, const char *str)
  85. {
  86. pstrcpy(hw->ptr, 32, str);
  87. hw->ptr += 32;
  88. }
  89. static inline void hwsetup_add_trailer(HWSetup *hw)
  90. {
  91. hwsetup_add_u32(hw, 8); /* size */
  92. hwsetup_add_tag(hw, HWSETUP_TAG_EOL);
  93. }
  94. static inline void hwsetup_add_cpu(HWSetup *hw,
  95. const char *name, uint32_t frequency)
  96. {
  97. hwsetup_add_u32(hw, 44); /* size */
  98. hwsetup_add_tag(hw, HWSETUP_TAG_CPU);
  99. hwsetup_add_str(hw, name);
  100. hwsetup_add_u32(hw, frequency);
  101. }
  102. static inline void hwsetup_add_flash(HWSetup *hw,
  103. const char *name, uint32_t base, uint32_t size)
  104. {
  105. hwsetup_add_u32(hw, 52); /* size */
  106. hwsetup_add_tag(hw, HWSETUP_TAG_FLASH);
  107. hwsetup_add_str(hw, name);
  108. hwsetup_add_u32(hw, base);
  109. hwsetup_add_u32(hw, size);
  110. hwsetup_add_u8(hw, 8); /* read latency */
  111. hwsetup_add_u8(hw, 8); /* write latency */
  112. hwsetup_add_u8(hw, 25); /* address width */
  113. hwsetup_add_u8(hw, 32); /* data width */
  114. }
  115. static inline void hwsetup_add_ddr_sdram(HWSetup *hw,
  116. const char *name, uint32_t base, uint32_t size)
  117. {
  118. hwsetup_add_u32(hw, 48); /* size */
  119. hwsetup_add_tag(hw, HWSETUP_TAG_DDR_SDRAM);
  120. hwsetup_add_str(hw, name);
  121. hwsetup_add_u32(hw, base);
  122. hwsetup_add_u32(hw, size);
  123. }
  124. static inline void hwsetup_add_timer(HWSetup *hw,
  125. const char *name, uint32_t base, uint32_t irq)
  126. {
  127. hwsetup_add_u32(hw, 56); /* size */
  128. hwsetup_add_tag(hw, HWSETUP_TAG_TIMER);
  129. hwsetup_add_str(hw, name);
  130. hwsetup_add_u32(hw, base);
  131. hwsetup_add_u8(hw, 1); /* wr_tickcount */
  132. hwsetup_add_u8(hw, 1); /* rd_tickcount */
  133. hwsetup_add_u8(hw, 1); /* start_stop_control */
  134. hwsetup_add_u8(hw, 32); /* counter_width */
  135. hwsetup_add_u32(hw, 20); /* reload_ticks */
  136. hwsetup_add_u8(hw, irq);
  137. hwsetup_add_u8(hw, 0); /* padding */
  138. hwsetup_add_u8(hw, 0); /* padding */
  139. hwsetup_add_u8(hw, 0); /* padding */
  140. }
  141. static inline void hwsetup_add_uart(HWSetup *hw,
  142. const char *name, uint32_t base, uint32_t irq)
  143. {
  144. hwsetup_add_u32(hw, 56); /* size */
  145. hwsetup_add_tag(hw, HWSETUP_TAG_UART);
  146. hwsetup_add_str(hw, name);
  147. hwsetup_add_u32(hw, base);
  148. hwsetup_add_u32(hw, 115200); /* baudrate */
  149. hwsetup_add_u8(hw, 8); /* databits */
  150. hwsetup_add_u8(hw, 1); /* stopbits */
  151. hwsetup_add_u8(hw, 1); /* use_interrupt */
  152. hwsetup_add_u8(hw, 1); /* block_on_transmit */
  153. hwsetup_add_u8(hw, 1); /* block_on_receive */
  154. hwsetup_add_u8(hw, 4); /* rx_buffer_size */
  155. hwsetup_add_u8(hw, 4); /* tx_buffer_size */
  156. hwsetup_add_u8(hw, irq);
  157. }
  158. #endif /* QEMU_HW_LM32_HWSETUP_H */