aspeed_gpio.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * ASPEED GPIO Controller
  3. *
  4. * Copyright (C) 2017-2019 IBM Corp.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/host-utils.h"
  10. #include "qemu/log.h"
  11. #include "hw/gpio/aspeed_gpio.h"
  12. #include "hw/misc/aspeed_scu.h"
  13. #include "qapi/error.h"
  14. #include "qapi/visitor.h"
  15. #include "hw/irq.h"
  16. #include "migration/vmstate.h"
  17. #include "trace.h"
  18. #include "hw/registerfields.h"
  19. #define GPIOS_PER_GROUP 8
  20. /* GPIO Source Types */
  21. #define ASPEED_CMD_SRC_MASK 0x01010101
  22. #define ASPEED_SOURCE_ARM 0
  23. #define ASPEED_SOURCE_LPC 1
  24. #define ASPEED_SOURCE_COPROCESSOR 2
  25. #define ASPEED_SOURCE_RESERVED 3
  26. /* GPIO Interrupt Triggers */
  27. /*
  28. * For each set of gpios there are three sensitivity registers that control
  29. * the interrupt trigger mode.
  30. *
  31. * | 2 | 1 | 0 | trigger mode
  32. * -----------------------------
  33. * | 0 | 0 | 0 | falling-edge
  34. * | 0 | 0 | 1 | rising-edge
  35. * | 0 | 1 | 0 | level-low
  36. * | 0 | 1 | 1 | level-high
  37. * | 1 | X | X | dual-edge
  38. */
  39. #define ASPEED_FALLING_EDGE 0
  40. #define ASPEED_RISING_EDGE 1
  41. #define ASPEED_LEVEL_LOW 2
  42. #define ASPEED_LEVEL_HIGH 3
  43. #define ASPEED_DUAL_EDGE 4
  44. /* GPIO Register Address Offsets */
  45. #define GPIO_ABCD_DATA_VALUE (0x000 >> 2)
  46. #define GPIO_ABCD_DIRECTION (0x004 >> 2)
  47. #define GPIO_ABCD_INT_ENABLE (0x008 >> 2)
  48. #define GPIO_ABCD_INT_SENS_0 (0x00C >> 2)
  49. #define GPIO_ABCD_INT_SENS_1 (0x010 >> 2)
  50. #define GPIO_ABCD_INT_SENS_2 (0x014 >> 2)
  51. #define GPIO_ABCD_INT_STATUS (0x018 >> 2)
  52. #define GPIO_ABCD_RESET_TOLERANT (0x01C >> 2)
  53. #define GPIO_EFGH_DATA_VALUE (0x020 >> 2)
  54. #define GPIO_EFGH_DIRECTION (0x024 >> 2)
  55. #define GPIO_EFGH_INT_ENABLE (0x028 >> 2)
  56. #define GPIO_EFGH_INT_SENS_0 (0x02C >> 2)
  57. #define GPIO_EFGH_INT_SENS_1 (0x030 >> 2)
  58. #define GPIO_EFGH_INT_SENS_2 (0x034 >> 2)
  59. #define GPIO_EFGH_INT_STATUS (0x038 >> 2)
  60. #define GPIO_EFGH_RESET_TOLERANT (0x03C >> 2)
  61. #define GPIO_ABCD_DEBOUNCE_1 (0x040 >> 2)
  62. #define GPIO_ABCD_DEBOUNCE_2 (0x044 >> 2)
  63. #define GPIO_EFGH_DEBOUNCE_1 (0x048 >> 2)
  64. #define GPIO_EFGH_DEBOUNCE_2 (0x04C >> 2)
  65. #define GPIO_DEBOUNCE_TIME_1 (0x050 >> 2)
  66. #define GPIO_DEBOUNCE_TIME_2 (0x054 >> 2)
  67. #define GPIO_DEBOUNCE_TIME_3 (0x058 >> 2)
  68. #define GPIO_ABCD_COMMAND_SRC_0 (0x060 >> 2)
  69. #define GPIO_ABCD_COMMAND_SRC_1 (0x064 >> 2)
  70. #define GPIO_EFGH_COMMAND_SRC_0 (0x068 >> 2)
  71. #define GPIO_EFGH_COMMAND_SRC_1 (0x06C >> 2)
  72. #define GPIO_IJKL_DATA_VALUE (0x070 >> 2)
  73. #define GPIO_IJKL_DIRECTION (0x074 >> 2)
  74. #define GPIO_MNOP_DATA_VALUE (0x078 >> 2)
  75. #define GPIO_MNOP_DIRECTION (0x07C >> 2)
  76. #define GPIO_QRST_DATA_VALUE (0x080 >> 2)
  77. #define GPIO_QRST_DIRECTION (0x084 >> 2)
  78. #define GPIO_UVWX_DATA_VALUE (0x088 >> 2)
  79. #define GPIO_UVWX_DIRECTION (0x08C >> 2)
  80. #define GPIO_IJKL_COMMAND_SRC_0 (0x090 >> 2)
  81. #define GPIO_IJKL_COMMAND_SRC_1 (0x094 >> 2)
  82. #define GPIO_IJKL_INT_ENABLE (0x098 >> 2)
  83. #define GPIO_IJKL_INT_SENS_0 (0x09C >> 2)
  84. #define GPIO_IJKL_INT_SENS_1 (0x0A0 >> 2)
  85. #define GPIO_IJKL_INT_SENS_2 (0x0A4 >> 2)
  86. #define GPIO_IJKL_INT_STATUS (0x0A8 >> 2)
  87. #define GPIO_IJKL_RESET_TOLERANT (0x0AC >> 2)
  88. #define GPIO_IJKL_DEBOUNCE_1 (0x0B0 >> 2)
  89. #define GPIO_IJKL_DEBOUNCE_2 (0x0B4 >> 2)
  90. #define GPIO_IJKL_INPUT_MASK (0x0B8 >> 2)
  91. #define GPIO_ABCD_DATA_READ (0x0C0 >> 2)
  92. #define GPIO_EFGH_DATA_READ (0x0C4 >> 2)
  93. #define GPIO_IJKL_DATA_READ (0x0C8 >> 2)
  94. #define GPIO_MNOP_DATA_READ (0x0CC >> 2)
  95. #define GPIO_QRST_DATA_READ (0x0D0 >> 2)
  96. #define GPIO_UVWX_DATA_READ (0x0D4 >> 2)
  97. #define GPIO_YZAAAB_DATA_READ (0x0D8 >> 2)
  98. #define GPIO_AC_DATA_READ (0x0DC >> 2)
  99. #define GPIO_MNOP_COMMAND_SRC_0 (0x0E0 >> 2)
  100. #define GPIO_MNOP_COMMAND_SRC_1 (0x0E4 >> 2)
  101. #define GPIO_MNOP_INT_ENABLE (0x0E8 >> 2)
  102. #define GPIO_MNOP_INT_SENS_0 (0x0EC >> 2)
  103. #define GPIO_MNOP_INT_SENS_1 (0x0F0 >> 2)
  104. #define GPIO_MNOP_INT_SENS_2 (0x0F4 >> 2)
  105. #define GPIO_MNOP_INT_STATUS (0x0F8 >> 2)
  106. #define GPIO_MNOP_RESET_TOLERANT (0x0FC >> 2)
  107. #define GPIO_MNOP_DEBOUNCE_1 (0x100 >> 2)
  108. #define GPIO_MNOP_DEBOUNCE_2 (0x104 >> 2)
  109. #define GPIO_MNOP_INPUT_MASK (0x108 >> 2)
  110. #define GPIO_QRST_COMMAND_SRC_0 (0x110 >> 2)
  111. #define GPIO_QRST_COMMAND_SRC_1 (0x114 >> 2)
  112. #define GPIO_QRST_INT_ENABLE (0x118 >> 2)
  113. #define GPIO_QRST_INT_SENS_0 (0x11C >> 2)
  114. #define GPIO_QRST_INT_SENS_1 (0x120 >> 2)
  115. #define GPIO_QRST_INT_SENS_2 (0x124 >> 2)
  116. #define GPIO_QRST_INT_STATUS (0x128 >> 2)
  117. #define GPIO_QRST_RESET_TOLERANT (0x12C >> 2)
  118. #define GPIO_QRST_DEBOUNCE_1 (0x130 >> 2)
  119. #define GPIO_QRST_DEBOUNCE_2 (0x134 >> 2)
  120. #define GPIO_QRST_INPUT_MASK (0x138 >> 2)
  121. #define GPIO_UVWX_COMMAND_SRC_0 (0x140 >> 2)
  122. #define GPIO_UVWX_COMMAND_SRC_1 (0x144 >> 2)
  123. #define GPIO_UVWX_INT_ENABLE (0x148 >> 2)
  124. #define GPIO_UVWX_INT_SENS_0 (0x14C >> 2)
  125. #define GPIO_UVWX_INT_SENS_1 (0x150 >> 2)
  126. #define GPIO_UVWX_INT_SENS_2 (0x154 >> 2)
  127. #define GPIO_UVWX_INT_STATUS (0x158 >> 2)
  128. #define GPIO_UVWX_RESET_TOLERANT (0x15C >> 2)
  129. #define GPIO_UVWX_DEBOUNCE_1 (0x160 >> 2)
  130. #define GPIO_UVWX_DEBOUNCE_2 (0x164 >> 2)
  131. #define GPIO_UVWX_INPUT_MASK (0x168 >> 2)
  132. #define GPIO_YZAAAB_COMMAND_SRC_0 (0x170 >> 2)
  133. #define GPIO_YZAAAB_COMMAND_SRC_1 (0x174 >> 2)
  134. #define GPIO_YZAAAB_INT_ENABLE (0x178 >> 2)
  135. #define GPIO_YZAAAB_INT_SENS_0 (0x17C >> 2)
  136. #define GPIO_YZAAAB_INT_SENS_1 (0x180 >> 2)
  137. #define GPIO_YZAAAB_INT_SENS_2 (0x184 >> 2)
  138. #define GPIO_YZAAAB_INT_STATUS (0x188 >> 2)
  139. #define GPIO_YZAAAB_RESET_TOLERANT (0x18C >> 2)
  140. #define GPIO_YZAAAB_DEBOUNCE_1 (0x190 >> 2)
  141. #define GPIO_YZAAAB_DEBOUNCE_2 (0x194 >> 2)
  142. #define GPIO_YZAAAB_INPUT_MASK (0x198 >> 2)
  143. #define GPIO_AC_COMMAND_SRC_0 (0x1A0 >> 2)
  144. #define GPIO_AC_COMMAND_SRC_1 (0x1A4 >> 2)
  145. #define GPIO_AC_INT_ENABLE (0x1A8 >> 2)
  146. #define GPIO_AC_INT_SENS_0 (0x1AC >> 2)
  147. #define GPIO_AC_INT_SENS_1 (0x1B0 >> 2)
  148. #define GPIO_AC_INT_SENS_2 (0x1B4 >> 2)
  149. #define GPIO_AC_INT_STATUS (0x1B8 >> 2)
  150. #define GPIO_AC_RESET_TOLERANT (0x1BC >> 2)
  151. #define GPIO_AC_DEBOUNCE_1 (0x1C0 >> 2)
  152. #define GPIO_AC_DEBOUNCE_2 (0x1C4 >> 2)
  153. #define GPIO_AC_INPUT_MASK (0x1C8 >> 2)
  154. #define GPIO_ABCD_INPUT_MASK (0x1D0 >> 2)
  155. #define GPIO_EFGH_INPUT_MASK (0x1D4 >> 2)
  156. #define GPIO_YZAAAB_DATA_VALUE (0x1E0 >> 2)
  157. #define GPIO_YZAAAB_DIRECTION (0x1E4 >> 2)
  158. #define GPIO_AC_DATA_VALUE (0x1E8 >> 2)
  159. #define GPIO_AC_DIRECTION (0x1EC >> 2)
  160. #define GPIO_3_3V_MEM_SIZE 0x1F0
  161. #define GPIO_3_3V_REG_ARRAY_SIZE (GPIO_3_3V_MEM_SIZE >> 2)
  162. /* AST2600 only - 1.8V gpios */
  163. /*
  164. * The AST2600 two copies of the GPIO controller: the same 3.3V gpios as the
  165. * AST2400 (memory offsets 0x0-0x198) and a second controller with 1.8V gpios
  166. * (memory offsets 0x800-0x9D4).
  167. */
  168. #define GPIO_1_8V_ABCD_DATA_VALUE (0x000 >> 2)
  169. #define GPIO_1_8V_ABCD_DIRECTION (0x004 >> 2)
  170. #define GPIO_1_8V_ABCD_INT_ENABLE (0x008 >> 2)
  171. #define GPIO_1_8V_ABCD_INT_SENS_0 (0x00C >> 2)
  172. #define GPIO_1_8V_ABCD_INT_SENS_1 (0x010 >> 2)
  173. #define GPIO_1_8V_ABCD_INT_SENS_2 (0x014 >> 2)
  174. #define GPIO_1_8V_ABCD_INT_STATUS (0x018 >> 2)
  175. #define GPIO_1_8V_ABCD_RESET_TOLERANT (0x01C >> 2)
  176. #define GPIO_1_8V_E_DATA_VALUE (0x020 >> 2)
  177. #define GPIO_1_8V_E_DIRECTION (0x024 >> 2)
  178. #define GPIO_1_8V_E_INT_ENABLE (0x028 >> 2)
  179. #define GPIO_1_8V_E_INT_SENS_0 (0x02C >> 2)
  180. #define GPIO_1_8V_E_INT_SENS_1 (0x030 >> 2)
  181. #define GPIO_1_8V_E_INT_SENS_2 (0x034 >> 2)
  182. #define GPIO_1_8V_E_INT_STATUS (0x038 >> 2)
  183. #define GPIO_1_8V_E_RESET_TOLERANT (0x03C >> 2)
  184. #define GPIO_1_8V_ABCD_DEBOUNCE_1 (0x040 >> 2)
  185. #define GPIO_1_8V_ABCD_DEBOUNCE_2 (0x044 >> 2)
  186. #define GPIO_1_8V_E_DEBOUNCE_1 (0x048 >> 2)
  187. #define GPIO_1_8V_E_DEBOUNCE_2 (0x04C >> 2)
  188. #define GPIO_1_8V_DEBOUNCE_TIME_1 (0x050 >> 2)
  189. #define GPIO_1_8V_DEBOUNCE_TIME_2 (0x054 >> 2)
  190. #define GPIO_1_8V_DEBOUNCE_TIME_3 (0x058 >> 2)
  191. #define GPIO_1_8V_ABCD_COMMAND_SRC_0 (0x060 >> 2)
  192. #define GPIO_1_8V_ABCD_COMMAND_SRC_1 (0x064 >> 2)
  193. #define GPIO_1_8V_E_COMMAND_SRC_0 (0x068 >> 2)
  194. #define GPIO_1_8V_E_COMMAND_SRC_1 (0x06C >> 2)
  195. #define GPIO_1_8V_ABCD_DATA_READ (0x0C0 >> 2)
  196. #define GPIO_1_8V_E_DATA_READ (0x0C4 >> 2)
  197. #define GPIO_1_8V_ABCD_INPUT_MASK (0x1D0 >> 2)
  198. #define GPIO_1_8V_E_INPUT_MASK (0x1D4 >> 2)
  199. #define GPIO_1_8V_MEM_SIZE 0x1D8
  200. #define GPIO_1_8V_REG_ARRAY_SIZE (GPIO_1_8V_MEM_SIZE >> 2)
  201. /*
  202. * GPIO index mode support
  203. * It only supports write operation
  204. */
  205. REG32(GPIO_INDEX_REG, 0x2AC)
  206. FIELD(GPIO_INDEX_REG, NUMBER, 0, 8)
  207. FIELD(GPIO_INDEX_REG, COMMAND, 12, 1)
  208. FIELD(GPIO_INDEX_REG, TYPE, 16, 4)
  209. FIELD(GPIO_INDEX_REG, DATA_VALUE, 20, 1)
  210. FIELD(GPIO_INDEX_REG, DIRECTION, 20, 1)
  211. FIELD(GPIO_INDEX_REG, INT_ENABLE, 20, 1)
  212. FIELD(GPIO_INDEX_REG, INT_SENS_0, 21, 1)
  213. FIELD(GPIO_INDEX_REG, INT_SENS_1, 22, 1)
  214. FIELD(GPIO_INDEX_REG, INT_SENS_2, 23, 1)
  215. FIELD(GPIO_INDEX_REG, INT_STATUS, 24, 1)
  216. FIELD(GPIO_INDEX_REG, DEBOUNCE_1, 20, 1)
  217. FIELD(GPIO_INDEX_REG, DEBOUNCE_2, 21, 1)
  218. FIELD(GPIO_INDEX_REG, RESET_TOLERANT, 20, 1)
  219. FIELD(GPIO_INDEX_REG, COMMAND_SRC_0, 20, 1)
  220. FIELD(GPIO_INDEX_REG, COMMAND_SRC_1, 21, 1)
  221. FIELD(GPIO_INDEX_REG, INPUT_MASK, 20, 1)
  222. static int aspeed_evaluate_irq(GPIOSets *regs, int gpio_prev_high, int gpio)
  223. {
  224. uint32_t falling_edge = 0, rising_edge = 0;
  225. uint32_t int_trigger = extract32(regs->int_sens_0, gpio, 1)
  226. | extract32(regs->int_sens_1, gpio, 1) << 1
  227. | extract32(regs->int_sens_2, gpio, 1) << 2;
  228. uint32_t gpio_curr_high = extract32(regs->data_value, gpio, 1);
  229. uint32_t gpio_int_enabled = extract32(regs->int_enable, gpio, 1);
  230. if (!gpio_int_enabled) {
  231. return 0;
  232. }
  233. /* Detect edges */
  234. if (gpio_curr_high && !gpio_prev_high) {
  235. rising_edge = 1;
  236. } else if (!gpio_curr_high && gpio_prev_high) {
  237. falling_edge = 1;
  238. }
  239. if (((int_trigger == ASPEED_FALLING_EDGE) && falling_edge) ||
  240. ((int_trigger == ASPEED_RISING_EDGE) && rising_edge) ||
  241. ((int_trigger == ASPEED_LEVEL_LOW) && !gpio_curr_high) ||
  242. ((int_trigger == ASPEED_LEVEL_HIGH) && gpio_curr_high) ||
  243. ((int_trigger >= ASPEED_DUAL_EDGE) && (rising_edge || falling_edge)))
  244. {
  245. regs->int_status = deposit32(regs->int_status, gpio, 1, 1);
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. #define nested_struct_index(ta, pa, m, tb, pb) \
  251. (pb - ((tb *)(((char *)pa) + offsetof(ta, m))))
  252. static ptrdiff_t aspeed_gpio_set_idx(AspeedGPIOState *s, GPIOSets *regs)
  253. {
  254. return nested_struct_index(AspeedGPIOState, s, sets, GPIOSets, regs);
  255. }
  256. static void aspeed_gpio_update(AspeedGPIOState *s, GPIOSets *regs,
  257. uint32_t value, uint32_t mode_mask)
  258. {
  259. uint32_t input_mask = regs->input_mask;
  260. uint32_t direction = regs->direction;
  261. uint32_t old = regs->data_value;
  262. uint32_t new = value;
  263. uint32_t diff;
  264. int gpio;
  265. diff = (old ^ new);
  266. diff &= mode_mask;
  267. if (diff) {
  268. for (gpio = 0; gpio < ASPEED_GPIOS_PER_SET; gpio++) {
  269. uint32_t mask = 1 << gpio;
  270. /* If the gpio needs to be updated... */
  271. if (!(diff & mask)) {
  272. continue;
  273. }
  274. /* ...and we're output or not input-masked... */
  275. if (!(direction & mask) && (input_mask & mask)) {
  276. continue;
  277. }
  278. /* ...then update the state. */
  279. if (mask & new) {
  280. regs->data_value |= mask;
  281. } else {
  282. regs->data_value &= ~mask;
  283. }
  284. /* If the gpio is set to output... */
  285. if (direction & mask) {
  286. /* ...trigger the line-state IRQ */
  287. ptrdiff_t set = aspeed_gpio_set_idx(s, regs);
  288. qemu_set_irq(s->gpios[set][gpio], !!(new & mask));
  289. } else {
  290. /* ...otherwise if we meet the line's current IRQ policy... */
  291. if (aspeed_evaluate_irq(regs, old & mask, gpio)) {
  292. /* ...trigger the VIC IRQ */
  293. s->pending++;
  294. }
  295. }
  296. }
  297. }
  298. qemu_set_irq(s->irq, !!(s->pending));
  299. }
  300. static bool aspeed_gpio_get_pin_level(AspeedGPIOState *s, uint32_t set_idx,
  301. uint32_t pin)
  302. {
  303. uint32_t reg_val;
  304. uint32_t pin_mask = 1 << pin;
  305. reg_val = s->sets[set_idx].data_value;
  306. return !!(reg_val & pin_mask);
  307. }
  308. static void aspeed_gpio_set_pin_level(AspeedGPIOState *s, uint32_t set_idx,
  309. uint32_t pin, bool level)
  310. {
  311. uint32_t value = s->sets[set_idx].data_value;
  312. uint32_t pin_mask = 1 << pin;
  313. if (level) {
  314. value |= pin_mask;
  315. } else {
  316. value &= ~pin_mask;
  317. }
  318. aspeed_gpio_update(s, &s->sets[set_idx], value, ~s->sets[set_idx].direction);
  319. }
  320. /*
  321. * | src_1 | src_2 | source |
  322. * |-----------------------------|
  323. * | 0 | 0 | ARM |
  324. * | 0 | 1 | LPC |
  325. * | 1 | 0 | Coprocessor|
  326. * | 1 | 1 | Reserved |
  327. *
  328. * Once the source of a set is programmed, corresponding bits in the
  329. * data_value, direction, interrupt [enable, sens[0-2]], reset_tol and
  330. * debounce registers can only be written by the source.
  331. *
  332. * Source is ARM by default
  333. * only bits 24, 16, 8, and 0 can be set
  334. *
  335. * we don't currently have a model for the LPC or Coprocessor
  336. */
  337. static uint32_t update_value_control_source(GPIOSets *regs, uint32_t old_value,
  338. uint32_t value)
  339. {
  340. int i;
  341. int cmd_source;
  342. /* assume the source is always ARM for now */
  343. int source = ASPEED_SOURCE_ARM;
  344. uint32_t new_value = 0;
  345. /* for each group in set */
  346. for (i = 0; i < ASPEED_GPIOS_PER_SET; i += GPIOS_PER_GROUP) {
  347. cmd_source = extract32(regs->cmd_source_0, i, 1)
  348. | (extract32(regs->cmd_source_1, i, 1) << 1);
  349. if (source == cmd_source) {
  350. new_value |= (0xff << i) & value;
  351. } else {
  352. new_value |= (0xff << i) & old_value;
  353. }
  354. }
  355. return new_value;
  356. }
  357. static const AspeedGPIOReg aspeed_3_3v_gpios[GPIO_3_3V_REG_ARRAY_SIZE] = {
  358. /* Set ABCD */
  359. [GPIO_ABCD_DATA_VALUE] = { 0, gpio_reg_data_value },
  360. [GPIO_ABCD_DIRECTION] = { 0, gpio_reg_direction },
  361. [GPIO_ABCD_INT_ENABLE] = { 0, gpio_reg_int_enable },
  362. [GPIO_ABCD_INT_SENS_0] = { 0, gpio_reg_int_sens_0 },
  363. [GPIO_ABCD_INT_SENS_1] = { 0, gpio_reg_int_sens_1 },
  364. [GPIO_ABCD_INT_SENS_2] = { 0, gpio_reg_int_sens_2 },
  365. [GPIO_ABCD_INT_STATUS] = { 0, gpio_reg_int_status },
  366. [GPIO_ABCD_RESET_TOLERANT] = { 0, gpio_reg_reset_tolerant },
  367. [GPIO_ABCD_DEBOUNCE_1] = { 0, gpio_reg_debounce_1 },
  368. [GPIO_ABCD_DEBOUNCE_2] = { 0, gpio_reg_debounce_2 },
  369. [GPIO_ABCD_COMMAND_SRC_0] = { 0, gpio_reg_cmd_source_0 },
  370. [GPIO_ABCD_COMMAND_SRC_1] = { 0, gpio_reg_cmd_source_1 },
  371. [GPIO_ABCD_DATA_READ] = { 0, gpio_reg_data_read },
  372. [GPIO_ABCD_INPUT_MASK] = { 0, gpio_reg_input_mask },
  373. /* Set EFGH */
  374. [GPIO_EFGH_DATA_VALUE] = { 1, gpio_reg_data_value },
  375. [GPIO_EFGH_DIRECTION] = { 1, gpio_reg_direction },
  376. [GPIO_EFGH_INT_ENABLE] = { 1, gpio_reg_int_enable },
  377. [GPIO_EFGH_INT_SENS_0] = { 1, gpio_reg_int_sens_0 },
  378. [GPIO_EFGH_INT_SENS_1] = { 1, gpio_reg_int_sens_1 },
  379. [GPIO_EFGH_INT_SENS_2] = { 1, gpio_reg_int_sens_2 },
  380. [GPIO_EFGH_INT_STATUS] = { 1, gpio_reg_int_status },
  381. [GPIO_EFGH_RESET_TOLERANT] = { 1, gpio_reg_reset_tolerant },
  382. [GPIO_EFGH_DEBOUNCE_1] = { 1, gpio_reg_debounce_1 },
  383. [GPIO_EFGH_DEBOUNCE_2] = { 1, gpio_reg_debounce_2 },
  384. [GPIO_EFGH_COMMAND_SRC_0] = { 1, gpio_reg_cmd_source_0 },
  385. [GPIO_EFGH_COMMAND_SRC_1] = { 1, gpio_reg_cmd_source_1 },
  386. [GPIO_EFGH_DATA_READ] = { 1, gpio_reg_data_read },
  387. [GPIO_EFGH_INPUT_MASK] = { 1, gpio_reg_input_mask },
  388. /* Set IJKL */
  389. [GPIO_IJKL_DATA_VALUE] = { 2, gpio_reg_data_value },
  390. [GPIO_IJKL_DIRECTION] = { 2, gpio_reg_direction },
  391. [GPIO_IJKL_INT_ENABLE] = { 2, gpio_reg_int_enable },
  392. [GPIO_IJKL_INT_SENS_0] = { 2, gpio_reg_int_sens_0 },
  393. [GPIO_IJKL_INT_SENS_1] = { 2, gpio_reg_int_sens_1 },
  394. [GPIO_IJKL_INT_SENS_2] = { 2, gpio_reg_int_sens_2 },
  395. [GPIO_IJKL_INT_STATUS] = { 2, gpio_reg_int_status },
  396. [GPIO_IJKL_RESET_TOLERANT] = { 2, gpio_reg_reset_tolerant },
  397. [GPIO_IJKL_DEBOUNCE_1] = { 2, gpio_reg_debounce_1 },
  398. [GPIO_IJKL_DEBOUNCE_2] = { 2, gpio_reg_debounce_2 },
  399. [GPIO_IJKL_COMMAND_SRC_0] = { 2, gpio_reg_cmd_source_0 },
  400. [GPIO_IJKL_COMMAND_SRC_1] = { 2, gpio_reg_cmd_source_1 },
  401. [GPIO_IJKL_DATA_READ] = { 2, gpio_reg_data_read },
  402. [GPIO_IJKL_INPUT_MASK] = { 2, gpio_reg_input_mask },
  403. /* Set MNOP */
  404. [GPIO_MNOP_DATA_VALUE] = { 3, gpio_reg_data_value },
  405. [GPIO_MNOP_DIRECTION] = { 3, gpio_reg_direction },
  406. [GPIO_MNOP_INT_ENABLE] = { 3, gpio_reg_int_enable },
  407. [GPIO_MNOP_INT_SENS_0] = { 3, gpio_reg_int_sens_0 },
  408. [GPIO_MNOP_INT_SENS_1] = { 3, gpio_reg_int_sens_1 },
  409. [GPIO_MNOP_INT_SENS_2] = { 3, gpio_reg_int_sens_2 },
  410. [GPIO_MNOP_INT_STATUS] = { 3, gpio_reg_int_status },
  411. [GPIO_MNOP_RESET_TOLERANT] = { 3, gpio_reg_reset_tolerant },
  412. [GPIO_MNOP_DEBOUNCE_1] = { 3, gpio_reg_debounce_1 },
  413. [GPIO_MNOP_DEBOUNCE_2] = { 3, gpio_reg_debounce_2 },
  414. [GPIO_MNOP_COMMAND_SRC_0] = { 3, gpio_reg_cmd_source_0 },
  415. [GPIO_MNOP_COMMAND_SRC_1] = { 3, gpio_reg_cmd_source_1 },
  416. [GPIO_MNOP_DATA_READ] = { 3, gpio_reg_data_read },
  417. [GPIO_MNOP_INPUT_MASK] = { 3, gpio_reg_input_mask },
  418. /* Set QRST */
  419. [GPIO_QRST_DATA_VALUE] = { 4, gpio_reg_data_value },
  420. [GPIO_QRST_DIRECTION] = { 4, gpio_reg_direction },
  421. [GPIO_QRST_INT_ENABLE] = { 4, gpio_reg_int_enable },
  422. [GPIO_QRST_INT_SENS_0] = { 4, gpio_reg_int_sens_0 },
  423. [GPIO_QRST_INT_SENS_1] = { 4, gpio_reg_int_sens_1 },
  424. [GPIO_QRST_INT_SENS_2] = { 4, gpio_reg_int_sens_2 },
  425. [GPIO_QRST_INT_STATUS] = { 4, gpio_reg_int_status },
  426. [GPIO_QRST_RESET_TOLERANT] = { 4, gpio_reg_reset_tolerant },
  427. [GPIO_QRST_DEBOUNCE_1] = { 4, gpio_reg_debounce_1 },
  428. [GPIO_QRST_DEBOUNCE_2] = { 4, gpio_reg_debounce_2 },
  429. [GPIO_QRST_COMMAND_SRC_0] = { 4, gpio_reg_cmd_source_0 },
  430. [GPIO_QRST_COMMAND_SRC_1] = { 4, gpio_reg_cmd_source_1 },
  431. [GPIO_QRST_DATA_READ] = { 4, gpio_reg_data_read },
  432. [GPIO_QRST_INPUT_MASK] = { 4, gpio_reg_input_mask },
  433. /* Set UVWX */
  434. [GPIO_UVWX_DATA_VALUE] = { 5, gpio_reg_data_value },
  435. [GPIO_UVWX_DIRECTION] = { 5, gpio_reg_direction },
  436. [GPIO_UVWX_INT_ENABLE] = { 5, gpio_reg_int_enable },
  437. [GPIO_UVWX_INT_SENS_0] = { 5, gpio_reg_int_sens_0 },
  438. [GPIO_UVWX_INT_SENS_1] = { 5, gpio_reg_int_sens_1 },
  439. [GPIO_UVWX_INT_SENS_2] = { 5, gpio_reg_int_sens_2 },
  440. [GPIO_UVWX_INT_STATUS] = { 5, gpio_reg_int_status },
  441. [GPIO_UVWX_RESET_TOLERANT] = { 5, gpio_reg_reset_tolerant },
  442. [GPIO_UVWX_DEBOUNCE_1] = { 5, gpio_reg_debounce_1 },
  443. [GPIO_UVWX_DEBOUNCE_2] = { 5, gpio_reg_debounce_2 },
  444. [GPIO_UVWX_COMMAND_SRC_0] = { 5, gpio_reg_cmd_source_0 },
  445. [GPIO_UVWX_COMMAND_SRC_1] = { 5, gpio_reg_cmd_source_1 },
  446. [GPIO_UVWX_DATA_READ] = { 5, gpio_reg_data_read },
  447. [GPIO_UVWX_INPUT_MASK] = { 5, gpio_reg_input_mask },
  448. /* Set YZAAAB */
  449. [GPIO_YZAAAB_DATA_VALUE] = { 6, gpio_reg_data_value },
  450. [GPIO_YZAAAB_DIRECTION] = { 6, gpio_reg_direction },
  451. [GPIO_YZAAAB_INT_ENABLE] = { 6, gpio_reg_int_enable },
  452. [GPIO_YZAAAB_INT_SENS_0] = { 6, gpio_reg_int_sens_0 },
  453. [GPIO_YZAAAB_INT_SENS_1] = { 6, gpio_reg_int_sens_1 },
  454. [GPIO_YZAAAB_INT_SENS_2] = { 6, gpio_reg_int_sens_2 },
  455. [GPIO_YZAAAB_INT_STATUS] = { 6, gpio_reg_int_status },
  456. [GPIO_YZAAAB_RESET_TOLERANT] = { 6, gpio_reg_reset_tolerant },
  457. [GPIO_YZAAAB_DEBOUNCE_1] = { 6, gpio_reg_debounce_1 },
  458. [GPIO_YZAAAB_DEBOUNCE_2] = { 6, gpio_reg_debounce_2 },
  459. [GPIO_YZAAAB_COMMAND_SRC_0] = { 6, gpio_reg_cmd_source_0 },
  460. [GPIO_YZAAAB_COMMAND_SRC_1] = { 6, gpio_reg_cmd_source_1 },
  461. [GPIO_YZAAAB_DATA_READ] = { 6, gpio_reg_data_read },
  462. [GPIO_YZAAAB_INPUT_MASK] = { 6, gpio_reg_input_mask },
  463. /* Set AC (ast2500 only) */
  464. [GPIO_AC_DATA_VALUE] = { 7, gpio_reg_data_value },
  465. [GPIO_AC_DIRECTION] = { 7, gpio_reg_direction },
  466. [GPIO_AC_INT_ENABLE] = { 7, gpio_reg_int_enable },
  467. [GPIO_AC_INT_SENS_0] = { 7, gpio_reg_int_sens_0 },
  468. [GPIO_AC_INT_SENS_1] = { 7, gpio_reg_int_sens_1 },
  469. [GPIO_AC_INT_SENS_2] = { 7, gpio_reg_int_sens_2 },
  470. [GPIO_AC_INT_STATUS] = { 7, gpio_reg_int_status },
  471. [GPIO_AC_RESET_TOLERANT] = { 7, gpio_reg_reset_tolerant },
  472. [GPIO_AC_DEBOUNCE_1] = { 7, gpio_reg_debounce_1 },
  473. [GPIO_AC_DEBOUNCE_2] = { 7, gpio_reg_debounce_2 },
  474. [GPIO_AC_COMMAND_SRC_0] = { 7, gpio_reg_cmd_source_0 },
  475. [GPIO_AC_COMMAND_SRC_1] = { 7, gpio_reg_cmd_source_1 },
  476. [GPIO_AC_DATA_READ] = { 7, gpio_reg_data_read },
  477. [GPIO_AC_INPUT_MASK] = { 7, gpio_reg_input_mask },
  478. };
  479. static const AspeedGPIOReg aspeed_1_8v_gpios[GPIO_1_8V_REG_ARRAY_SIZE] = {
  480. /* 1.8V Set ABCD */
  481. [GPIO_1_8V_ABCD_DATA_VALUE] = {0, gpio_reg_data_value},
  482. [GPIO_1_8V_ABCD_DIRECTION] = {0, gpio_reg_direction},
  483. [GPIO_1_8V_ABCD_INT_ENABLE] = {0, gpio_reg_int_enable},
  484. [GPIO_1_8V_ABCD_INT_SENS_0] = {0, gpio_reg_int_sens_0},
  485. [GPIO_1_8V_ABCD_INT_SENS_1] = {0, gpio_reg_int_sens_1},
  486. [GPIO_1_8V_ABCD_INT_SENS_2] = {0, gpio_reg_int_sens_2},
  487. [GPIO_1_8V_ABCD_INT_STATUS] = {0, gpio_reg_int_status},
  488. [GPIO_1_8V_ABCD_RESET_TOLERANT] = {0, gpio_reg_reset_tolerant},
  489. [GPIO_1_8V_ABCD_DEBOUNCE_1] = {0, gpio_reg_debounce_1},
  490. [GPIO_1_8V_ABCD_DEBOUNCE_2] = {0, gpio_reg_debounce_2},
  491. [GPIO_1_8V_ABCD_COMMAND_SRC_0] = {0, gpio_reg_cmd_source_0},
  492. [GPIO_1_8V_ABCD_COMMAND_SRC_1] = {0, gpio_reg_cmd_source_1},
  493. [GPIO_1_8V_ABCD_DATA_READ] = {0, gpio_reg_data_read},
  494. [GPIO_1_8V_ABCD_INPUT_MASK] = {0, gpio_reg_input_mask},
  495. /* 1.8V Set E */
  496. [GPIO_1_8V_E_DATA_VALUE] = {1, gpio_reg_data_value},
  497. [GPIO_1_8V_E_DIRECTION] = {1, gpio_reg_direction},
  498. [GPIO_1_8V_E_INT_ENABLE] = {1, gpio_reg_int_enable},
  499. [GPIO_1_8V_E_INT_SENS_0] = {1, gpio_reg_int_sens_0},
  500. [GPIO_1_8V_E_INT_SENS_1] = {1, gpio_reg_int_sens_1},
  501. [GPIO_1_8V_E_INT_SENS_2] = {1, gpio_reg_int_sens_2},
  502. [GPIO_1_8V_E_INT_STATUS] = {1, gpio_reg_int_status},
  503. [GPIO_1_8V_E_RESET_TOLERANT] = {1, gpio_reg_reset_tolerant},
  504. [GPIO_1_8V_E_DEBOUNCE_1] = {1, gpio_reg_debounce_1},
  505. [GPIO_1_8V_E_DEBOUNCE_2] = {1, gpio_reg_debounce_2},
  506. [GPIO_1_8V_E_COMMAND_SRC_0] = {1, gpio_reg_cmd_source_0},
  507. [GPIO_1_8V_E_COMMAND_SRC_1] = {1, gpio_reg_cmd_source_1},
  508. [GPIO_1_8V_E_DATA_READ] = {1, gpio_reg_data_read},
  509. [GPIO_1_8V_E_INPUT_MASK] = {1, gpio_reg_input_mask},
  510. };
  511. static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)
  512. {
  513. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  514. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  515. uint64_t idx = -1;
  516. const AspeedGPIOReg *reg;
  517. GPIOSets *set;
  518. uint32_t value = 0;
  519. uint64_t debounce_value;
  520. idx = offset >> 2;
  521. if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) {
  522. idx -= GPIO_DEBOUNCE_TIME_1;
  523. debounce_value = (uint64_t) s->debounce_regs[idx];
  524. trace_aspeed_gpio_read(offset, debounce_value);
  525. return debounce_value;
  526. }
  527. if (idx >= agc->reg_table_count) {
  528. qemu_log_mask(LOG_GUEST_ERROR, "%s: idx 0x%" PRIx64 " out of bounds\n",
  529. __func__, idx);
  530. return 0;
  531. }
  532. reg = &agc->reg_table[idx];
  533. if (reg->set_idx >= agc->nr_gpio_sets) {
  534. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  535. PRIx64"\n", __func__, offset);
  536. return 0;
  537. }
  538. set = &s->sets[reg->set_idx];
  539. switch (reg->type) {
  540. case gpio_reg_data_value:
  541. value = set->data_value;
  542. break;
  543. case gpio_reg_direction:
  544. value = set->direction;
  545. break;
  546. case gpio_reg_int_enable:
  547. value = set->int_enable;
  548. break;
  549. case gpio_reg_int_sens_0:
  550. value = set->int_sens_0;
  551. break;
  552. case gpio_reg_int_sens_1:
  553. value = set->int_sens_1;
  554. break;
  555. case gpio_reg_int_sens_2:
  556. value = set->int_sens_2;
  557. break;
  558. case gpio_reg_int_status:
  559. value = set->int_status;
  560. break;
  561. case gpio_reg_reset_tolerant:
  562. value = set->reset_tol;
  563. break;
  564. case gpio_reg_debounce_1:
  565. value = set->debounce_1;
  566. break;
  567. case gpio_reg_debounce_2:
  568. value = set->debounce_2;
  569. break;
  570. case gpio_reg_cmd_source_0:
  571. value = set->cmd_source_0;
  572. break;
  573. case gpio_reg_cmd_source_1:
  574. value = set->cmd_source_1;
  575. break;
  576. case gpio_reg_data_read:
  577. value = set->data_read;
  578. break;
  579. case gpio_reg_input_mask:
  580. value = set->input_mask;
  581. break;
  582. default:
  583. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  584. PRIx64"\n", __func__, offset);
  585. return 0;
  586. }
  587. trace_aspeed_gpio_read(offset, value);
  588. return value;
  589. }
  590. static void aspeed_gpio_write_index_mode(void *opaque, hwaddr offset,
  591. uint64_t data, uint32_t size)
  592. {
  593. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  594. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  595. const GPIOSetProperties *props;
  596. GPIOSets *set;
  597. uint32_t reg_idx_number = FIELD_EX32(data, GPIO_INDEX_REG, NUMBER);
  598. uint32_t reg_idx_type = FIELD_EX32(data, GPIO_INDEX_REG, TYPE);
  599. uint32_t reg_idx_command = FIELD_EX32(data, GPIO_INDEX_REG, COMMAND);
  600. uint32_t set_idx = reg_idx_number / ASPEED_GPIOS_PER_SET;
  601. uint32_t pin_idx = reg_idx_number % ASPEED_GPIOS_PER_SET;
  602. uint32_t group_idx = pin_idx / GPIOS_PER_GROUP;
  603. uint32_t reg_value = 0;
  604. uint32_t cleared;
  605. set = &s->sets[set_idx];
  606. props = &agc->props[set_idx];
  607. if (reg_idx_command)
  608. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  609. PRIx64 "index mode wrong command 0x%x\n",
  610. __func__, offset, data, reg_idx_command);
  611. switch (reg_idx_type) {
  612. case gpio_reg_idx_data:
  613. reg_value = set->data_read;
  614. reg_value = deposit32(reg_value, pin_idx, 1,
  615. FIELD_EX32(data, GPIO_INDEX_REG, DATA_VALUE));
  616. reg_value &= props->output;
  617. reg_value = update_value_control_source(set, set->data_value,
  618. reg_value);
  619. set->data_read = reg_value;
  620. aspeed_gpio_update(s, set, reg_value, set->direction);
  621. return;
  622. case gpio_reg_idx_direction:
  623. reg_value = set->direction;
  624. reg_value = deposit32(reg_value, pin_idx, 1,
  625. FIELD_EX32(data, GPIO_INDEX_REG, DIRECTION));
  626. /*
  627. * where data is the value attempted to be written to the pin:
  628. * pin type | input mask | output mask | expected value
  629. * ------------------------------------------------------------
  630. * bidirectional | 1 | 1 | data
  631. * input only | 1 | 0 | 0
  632. * output only | 0 | 1 | 1
  633. * no pin | 0 | 0 | 0
  634. *
  635. * which is captured by:
  636. * data = ( data | ~input) & output;
  637. */
  638. reg_value = (reg_value | ~props->input) & props->output;
  639. set->direction = update_value_control_source(set, set->direction,
  640. reg_value);
  641. break;
  642. case gpio_reg_idx_interrupt:
  643. reg_value = set->int_enable;
  644. reg_value = deposit32(reg_value, pin_idx, 1,
  645. FIELD_EX32(data, GPIO_INDEX_REG, INT_ENABLE));
  646. set->int_enable = update_value_control_source(set, set->int_enable,
  647. reg_value);
  648. reg_value = set->int_sens_0;
  649. reg_value = deposit32(reg_value, pin_idx, 1,
  650. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_0));
  651. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  652. reg_value);
  653. reg_value = set->int_sens_1;
  654. reg_value = deposit32(reg_value, pin_idx, 1,
  655. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_1));
  656. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  657. reg_value);
  658. reg_value = set->int_sens_2;
  659. reg_value = deposit32(reg_value, pin_idx, 1,
  660. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_2));
  661. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  662. reg_value);
  663. /* set interrupt status */
  664. reg_value = set->int_status;
  665. reg_value = deposit32(reg_value, pin_idx, 1,
  666. FIELD_EX32(data, GPIO_INDEX_REG, INT_STATUS));
  667. cleared = ctpop32(reg_value & set->int_status);
  668. if (s->pending && cleared) {
  669. assert(s->pending >= cleared);
  670. s->pending -= cleared;
  671. }
  672. set->int_status &= ~reg_value;
  673. break;
  674. case gpio_reg_idx_debounce:
  675. reg_value = set->debounce_1;
  676. reg_value = deposit32(reg_value, pin_idx, 1,
  677. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_1));
  678. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  679. reg_value);
  680. reg_value = set->debounce_2;
  681. reg_value = deposit32(reg_value, pin_idx, 1,
  682. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_2));
  683. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  684. reg_value);
  685. return;
  686. case gpio_reg_idx_tolerance:
  687. reg_value = set->reset_tol;
  688. reg_value = deposit32(reg_value, pin_idx, 1,
  689. FIELD_EX32(data, GPIO_INDEX_REG, RESET_TOLERANT));
  690. set->reset_tol = update_value_control_source(set, set->reset_tol,
  691. reg_value);
  692. return;
  693. case gpio_reg_idx_cmd_src:
  694. reg_value = set->cmd_source_0;
  695. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  696. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_0));
  697. set->cmd_source_0 = reg_value & ASPEED_CMD_SRC_MASK;
  698. reg_value = set->cmd_source_1;
  699. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  700. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_1));
  701. set->cmd_source_1 = reg_value & ASPEED_CMD_SRC_MASK;
  702. return;
  703. case gpio_reg_idx_input_mask:
  704. reg_value = set->input_mask;
  705. reg_value = deposit32(reg_value, pin_idx, 1,
  706. FIELD_EX32(data, GPIO_INDEX_REG, INPUT_MASK));
  707. /*
  708. * feeds into interrupt generation
  709. * 0: read from data value reg will be updated
  710. * 1: read from data value reg will not be updated
  711. */
  712. set->input_mask = reg_value & props->input;
  713. break;
  714. default:
  715. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  716. PRIx64 "index mode wrong type 0x%x\n",
  717. __func__, offset, data, reg_idx_type);
  718. return;
  719. }
  720. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  721. return;
  722. }
  723. static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,
  724. uint32_t size)
  725. {
  726. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  727. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  728. const GPIOSetProperties *props;
  729. uint64_t idx = -1;
  730. const AspeedGPIOReg *reg;
  731. GPIOSets *set;
  732. uint32_t cleared;
  733. trace_aspeed_gpio_write(offset, data);
  734. idx = offset >> 2;
  735. /* check gpio index mode */
  736. if (idx == R_GPIO_INDEX_REG) {
  737. aspeed_gpio_write_index_mode(opaque, offset, data, size);
  738. return;
  739. }
  740. if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) {
  741. idx -= GPIO_DEBOUNCE_TIME_1;
  742. s->debounce_regs[idx] = (uint32_t) data;
  743. return;
  744. }
  745. if (idx >= agc->reg_table_count) {
  746. qemu_log_mask(LOG_GUEST_ERROR, "%s: idx 0x%" PRIx64 " out of bounds\n",
  747. __func__, idx);
  748. return;
  749. }
  750. reg = &agc->reg_table[idx];
  751. if (reg->set_idx >= agc->nr_gpio_sets) {
  752. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  753. PRIx64"\n", __func__, offset);
  754. return;
  755. }
  756. set = &s->sets[reg->set_idx];
  757. props = &agc->props[reg->set_idx];
  758. switch (reg->type) {
  759. case gpio_reg_data_value:
  760. data &= props->output;
  761. data = update_value_control_source(set, set->data_value, data);
  762. set->data_read = data;
  763. aspeed_gpio_update(s, set, data, set->direction);
  764. return;
  765. case gpio_reg_direction:
  766. /*
  767. * where data is the value attempted to be written to the pin:
  768. * pin type | input mask | output mask | expected value
  769. * ------------------------------------------------------------
  770. * bidirectional | 1 | 1 | data
  771. * input only | 1 | 0 | 0
  772. * output only | 0 | 1 | 1
  773. * no pin | 0 | 0 | 0
  774. *
  775. * which is captured by:
  776. * data = ( data | ~input) & output;
  777. */
  778. data = (data | ~props->input) & props->output;
  779. set->direction = update_value_control_source(set, set->direction, data);
  780. break;
  781. case gpio_reg_int_enable:
  782. set->int_enable = update_value_control_source(set, set->int_enable,
  783. data);
  784. break;
  785. case gpio_reg_int_sens_0:
  786. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  787. data);
  788. break;
  789. case gpio_reg_int_sens_1:
  790. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  791. data);
  792. break;
  793. case gpio_reg_int_sens_2:
  794. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  795. data);
  796. break;
  797. case gpio_reg_int_status:
  798. cleared = ctpop32(data & set->int_status);
  799. if (s->pending && cleared) {
  800. assert(s->pending >= cleared);
  801. s->pending -= cleared;
  802. }
  803. set->int_status &= ~data;
  804. break;
  805. case gpio_reg_reset_tolerant:
  806. set->reset_tol = update_value_control_source(set, set->reset_tol,
  807. data);
  808. return;
  809. case gpio_reg_debounce_1:
  810. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  811. data);
  812. return;
  813. case gpio_reg_debounce_2:
  814. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  815. data);
  816. return;
  817. case gpio_reg_cmd_source_0:
  818. set->cmd_source_0 = data & ASPEED_CMD_SRC_MASK;
  819. return;
  820. case gpio_reg_cmd_source_1:
  821. set->cmd_source_1 = data & ASPEED_CMD_SRC_MASK;
  822. return;
  823. case gpio_reg_data_read:
  824. /* Read only register */
  825. return;
  826. case gpio_reg_input_mask:
  827. /*
  828. * feeds into interrupt generation
  829. * 0: read from data value reg will be updated
  830. * 1: read from data value reg will not be updated
  831. */
  832. set->input_mask = data & props->input;
  833. break;
  834. default:
  835. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  836. PRIx64"\n", __func__, offset);
  837. return;
  838. }
  839. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  840. return;
  841. }
  842. static int get_set_idx(AspeedGPIOState *s, const char *group, int *group_idx)
  843. {
  844. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  845. int set_idx, g_idx;
  846. for (set_idx = 0; set_idx < agc->nr_gpio_sets; set_idx++) {
  847. const GPIOSetProperties *set_props = &agc->props[set_idx];
  848. for (g_idx = 0; g_idx < ASPEED_GROUPS_PER_SET; g_idx++) {
  849. if (!strncmp(group, set_props->group_label[g_idx], strlen(group))) {
  850. *group_idx = g_idx;
  851. return set_idx;
  852. }
  853. }
  854. }
  855. return -1;
  856. }
  857. static void aspeed_gpio_get_pin(Object *obj, Visitor *v, const char *name,
  858. void *opaque, Error **errp)
  859. {
  860. int pin = 0xfff;
  861. bool level = true;
  862. char group[4];
  863. AspeedGPIOState *s = ASPEED_GPIO(obj);
  864. int set_idx, group_idx = 0;
  865. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  866. /* 1.8V gpio */
  867. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  868. error_setg(errp, "%s: error reading %s", __func__, name);
  869. return;
  870. }
  871. }
  872. set_idx = get_set_idx(s, group, &group_idx);
  873. if (set_idx == -1) {
  874. error_setg(errp, "%s: invalid group %s", __func__, group);
  875. return;
  876. }
  877. pin = pin + group_idx * GPIOS_PER_GROUP;
  878. level = aspeed_gpio_get_pin_level(s, set_idx, pin);
  879. visit_type_bool(v, name, &level, errp);
  880. }
  881. static void aspeed_gpio_set_pin(Object *obj, Visitor *v, const char *name,
  882. void *opaque, Error **errp)
  883. {
  884. bool level;
  885. int pin = 0xfff;
  886. char group[4];
  887. AspeedGPIOState *s = ASPEED_GPIO(obj);
  888. int set_idx, group_idx = 0;
  889. if (!visit_type_bool(v, name, &level, errp)) {
  890. return;
  891. }
  892. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  893. /* 1.8V gpio */
  894. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  895. error_setg(errp, "%s: error reading %s", __func__, name);
  896. return;
  897. }
  898. }
  899. set_idx = get_set_idx(s, group, &group_idx);
  900. if (set_idx == -1) {
  901. error_setg(errp, "%s: invalid group %s", __func__, group);
  902. return;
  903. }
  904. pin = pin + group_idx * GPIOS_PER_GROUP;
  905. aspeed_gpio_set_pin_level(s, set_idx, pin, level);
  906. }
  907. /****************** Setup functions ******************/
  908. static const GPIOSetProperties ast2400_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  909. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  910. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  911. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  912. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  913. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  914. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  915. [6] = {0x0000000f, 0x0fffff0f, {"Y", "Z", "AA", "AB"} },
  916. };
  917. static const GPIOSetProperties ast2500_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  918. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  919. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  920. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  921. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  922. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  923. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  924. [6] = {0x0fffffff, 0x0fffffff, {"Y", "Z", "AA", "AB"} },
  925. [7] = {0x000000ff, 0x000000ff, {"AC"} },
  926. };
  927. static GPIOSetProperties ast2600_3_3v_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  928. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  929. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  930. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  931. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  932. [4] = {0xffffffff, 0x00ffffff, {"Q", "R", "S", "T"} },
  933. [5] = {0xffffffff, 0xffffff00, {"U", "V", "W", "X"} },
  934. [6] = {0x0000ffff, 0x0000ffff, {"Y", "Z"} },
  935. };
  936. static GPIOSetProperties ast2600_1_8v_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  937. [0] = {0xffffffff, 0xffffffff, {"18A", "18B", "18C", "18D"} },
  938. [1] = {0x0000000f, 0x0000000f, {"18E"} },
  939. };
  940. static GPIOSetProperties ast1030_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  941. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  942. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  943. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  944. [3] = {0xffffff3f, 0xffffff3f, {"M", "N", "O", "P"} },
  945. [4] = {0xff060c1f, 0x00060c1f, {"Q", "R", "S", "T"} },
  946. [5] = {0x000000ff, 0x00000000, {"U"} },
  947. };
  948. static const MemoryRegionOps aspeed_gpio_ops = {
  949. .read = aspeed_gpio_read,
  950. .write = aspeed_gpio_write,
  951. .endianness = DEVICE_LITTLE_ENDIAN,
  952. .valid.min_access_size = 4,
  953. .valid.max_access_size = 4,
  954. };
  955. static void aspeed_gpio_reset(DeviceState *dev)
  956. {
  957. AspeedGPIOState *s = ASPEED_GPIO(dev);
  958. /* TODO: respect the reset tolerance registers */
  959. memset(s->sets, 0, sizeof(s->sets));
  960. }
  961. static void aspeed_gpio_realize(DeviceState *dev, Error **errp)
  962. {
  963. AspeedGPIOState *s = ASPEED_GPIO(dev);
  964. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  965. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  966. /* Interrupt parent line */
  967. sysbus_init_irq(sbd, &s->irq);
  968. /* Individual GPIOs */
  969. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  970. const GPIOSetProperties *props = &agc->props[i];
  971. uint32_t skip = ~(props->input | props->output);
  972. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  973. if (skip >> j & 1) {
  974. continue;
  975. }
  976. sysbus_init_irq(sbd, &s->gpios[i][j]);
  977. }
  978. }
  979. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_gpio_ops, s,
  980. TYPE_ASPEED_GPIO, 0x800);
  981. sysbus_init_mmio(sbd, &s->iomem);
  982. }
  983. static void aspeed_gpio_init(Object *obj)
  984. {
  985. AspeedGPIOState *s = ASPEED_GPIO(obj);
  986. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  987. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  988. const GPIOSetProperties *props = &agc->props[i];
  989. uint32_t skip = ~(props->input | props->output);
  990. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  991. if (skip >> j & 1) {
  992. continue;
  993. }
  994. int group_idx = j / GPIOS_PER_GROUP;
  995. int pin_idx = j % GPIOS_PER_GROUP;
  996. const char *group = &props->group_label[group_idx][0];
  997. char *name = g_strdup_printf("gpio%s%d", group, pin_idx);
  998. object_property_add(obj, name, "bool", aspeed_gpio_get_pin,
  999. aspeed_gpio_set_pin, NULL, NULL);
  1000. g_free(name);
  1001. }
  1002. }
  1003. }
  1004. static const VMStateDescription vmstate_gpio_regs = {
  1005. .name = TYPE_ASPEED_GPIO"/regs",
  1006. .version_id = 1,
  1007. .minimum_version_id = 1,
  1008. .fields = (const VMStateField[]) {
  1009. VMSTATE_UINT32(data_value, GPIOSets),
  1010. VMSTATE_UINT32(data_read, GPIOSets),
  1011. VMSTATE_UINT32(direction, GPIOSets),
  1012. VMSTATE_UINT32(int_enable, GPIOSets),
  1013. VMSTATE_UINT32(int_sens_0, GPIOSets),
  1014. VMSTATE_UINT32(int_sens_1, GPIOSets),
  1015. VMSTATE_UINT32(int_sens_2, GPIOSets),
  1016. VMSTATE_UINT32(int_status, GPIOSets),
  1017. VMSTATE_UINT32(reset_tol, GPIOSets),
  1018. VMSTATE_UINT32(cmd_source_0, GPIOSets),
  1019. VMSTATE_UINT32(cmd_source_1, GPIOSets),
  1020. VMSTATE_UINT32(debounce_1, GPIOSets),
  1021. VMSTATE_UINT32(debounce_2, GPIOSets),
  1022. VMSTATE_UINT32(input_mask, GPIOSets),
  1023. VMSTATE_END_OF_LIST(),
  1024. }
  1025. };
  1026. static const VMStateDescription vmstate_aspeed_gpio = {
  1027. .name = TYPE_ASPEED_GPIO,
  1028. .version_id = 1,
  1029. .minimum_version_id = 1,
  1030. .fields = (const VMStateField[]) {
  1031. VMSTATE_STRUCT_ARRAY(sets, AspeedGPIOState, ASPEED_GPIO_MAX_NR_SETS,
  1032. 1, vmstate_gpio_regs, GPIOSets),
  1033. VMSTATE_UINT32_ARRAY(debounce_regs, AspeedGPIOState,
  1034. ASPEED_GPIO_NR_DEBOUNCE_REGS),
  1035. VMSTATE_END_OF_LIST(),
  1036. }
  1037. };
  1038. static void aspeed_gpio_class_init(ObjectClass *klass, void *data)
  1039. {
  1040. DeviceClass *dc = DEVICE_CLASS(klass);
  1041. dc->realize = aspeed_gpio_realize;
  1042. device_class_set_legacy_reset(dc, aspeed_gpio_reset);
  1043. dc->desc = "Aspeed GPIO Controller";
  1044. dc->vmsd = &vmstate_aspeed_gpio;
  1045. }
  1046. static void aspeed_gpio_ast2400_class_init(ObjectClass *klass, void *data)
  1047. {
  1048. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1049. agc->props = ast2400_set_props;
  1050. agc->nr_gpio_pins = 216;
  1051. agc->nr_gpio_sets = 7;
  1052. agc->reg_table = aspeed_3_3v_gpios;
  1053. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1054. }
  1055. static void aspeed_gpio_2500_class_init(ObjectClass *klass, void *data)
  1056. {
  1057. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1058. agc->props = ast2500_set_props;
  1059. agc->nr_gpio_pins = 228;
  1060. agc->nr_gpio_sets = 8;
  1061. agc->reg_table = aspeed_3_3v_gpios;
  1062. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1063. }
  1064. static void aspeed_gpio_ast2600_3_3v_class_init(ObjectClass *klass, void *data)
  1065. {
  1066. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1067. agc->props = ast2600_3_3v_set_props;
  1068. agc->nr_gpio_pins = 208;
  1069. agc->nr_gpio_sets = 7;
  1070. agc->reg_table = aspeed_3_3v_gpios;
  1071. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1072. }
  1073. static void aspeed_gpio_ast2600_1_8v_class_init(ObjectClass *klass, void *data)
  1074. {
  1075. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1076. agc->props = ast2600_1_8v_set_props;
  1077. agc->nr_gpio_pins = 36;
  1078. agc->nr_gpio_sets = 2;
  1079. agc->reg_table = aspeed_1_8v_gpios;
  1080. agc->reg_table_count = GPIO_1_8V_REG_ARRAY_SIZE;
  1081. }
  1082. static void aspeed_gpio_1030_class_init(ObjectClass *klass, void *data)
  1083. {
  1084. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1085. agc->props = ast1030_set_props;
  1086. agc->nr_gpio_pins = 151;
  1087. agc->nr_gpio_sets = 6;
  1088. agc->reg_table = aspeed_3_3v_gpios;
  1089. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1090. }
  1091. static const TypeInfo aspeed_gpio_info = {
  1092. .name = TYPE_ASPEED_GPIO,
  1093. .parent = TYPE_SYS_BUS_DEVICE,
  1094. .instance_size = sizeof(AspeedGPIOState),
  1095. .class_size = sizeof(AspeedGPIOClass),
  1096. .class_init = aspeed_gpio_class_init,
  1097. .abstract = true,
  1098. };
  1099. static const TypeInfo aspeed_gpio_ast2400_info = {
  1100. .name = TYPE_ASPEED_GPIO "-ast2400",
  1101. .parent = TYPE_ASPEED_GPIO,
  1102. .class_init = aspeed_gpio_ast2400_class_init,
  1103. .instance_init = aspeed_gpio_init,
  1104. };
  1105. static const TypeInfo aspeed_gpio_ast2500_info = {
  1106. .name = TYPE_ASPEED_GPIO "-ast2500",
  1107. .parent = TYPE_ASPEED_GPIO,
  1108. .class_init = aspeed_gpio_2500_class_init,
  1109. .instance_init = aspeed_gpio_init,
  1110. };
  1111. static const TypeInfo aspeed_gpio_ast2600_3_3v_info = {
  1112. .name = TYPE_ASPEED_GPIO "-ast2600",
  1113. .parent = TYPE_ASPEED_GPIO,
  1114. .class_init = aspeed_gpio_ast2600_3_3v_class_init,
  1115. .instance_init = aspeed_gpio_init,
  1116. };
  1117. static const TypeInfo aspeed_gpio_ast2600_1_8v_info = {
  1118. .name = TYPE_ASPEED_GPIO "-ast2600-1_8v",
  1119. .parent = TYPE_ASPEED_GPIO,
  1120. .class_init = aspeed_gpio_ast2600_1_8v_class_init,
  1121. .instance_init = aspeed_gpio_init,
  1122. };
  1123. static const TypeInfo aspeed_gpio_ast1030_info = {
  1124. .name = TYPE_ASPEED_GPIO "-ast1030",
  1125. .parent = TYPE_ASPEED_GPIO,
  1126. .class_init = aspeed_gpio_1030_class_init,
  1127. .instance_init = aspeed_gpio_init,
  1128. };
  1129. static void aspeed_gpio_register_types(void)
  1130. {
  1131. type_register_static(&aspeed_gpio_info);
  1132. type_register_static(&aspeed_gpio_ast2400_info);
  1133. type_register_static(&aspeed_gpio_ast2500_info);
  1134. type_register_static(&aspeed_gpio_ast2600_3_3v_info);
  1135. type_register_static(&aspeed_gpio_ast2600_1_8v_info);
  1136. type_register_static(&aspeed_gpio_ast1030_info);
  1137. }
  1138. type_init(aspeed_gpio_register_types);