aspeed_gpio.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. reg = &agc->reg_table[idx];
  528. if (reg->set_idx >= agc->nr_gpio_sets) {
  529. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  530. PRIx64"\n", __func__, offset);
  531. return 0;
  532. }
  533. set = &s->sets[reg->set_idx];
  534. switch (reg->type) {
  535. case gpio_reg_data_value:
  536. value = set->data_value;
  537. break;
  538. case gpio_reg_direction:
  539. value = set->direction;
  540. break;
  541. case gpio_reg_int_enable:
  542. value = set->int_enable;
  543. break;
  544. case gpio_reg_int_sens_0:
  545. value = set->int_sens_0;
  546. break;
  547. case gpio_reg_int_sens_1:
  548. value = set->int_sens_1;
  549. break;
  550. case gpio_reg_int_sens_2:
  551. value = set->int_sens_2;
  552. break;
  553. case gpio_reg_int_status:
  554. value = set->int_status;
  555. break;
  556. case gpio_reg_reset_tolerant:
  557. value = set->reset_tol;
  558. break;
  559. case gpio_reg_debounce_1:
  560. value = set->debounce_1;
  561. break;
  562. case gpio_reg_debounce_2:
  563. value = set->debounce_2;
  564. break;
  565. case gpio_reg_cmd_source_0:
  566. value = set->cmd_source_0;
  567. break;
  568. case gpio_reg_cmd_source_1:
  569. value = set->cmd_source_1;
  570. break;
  571. case gpio_reg_data_read:
  572. value = set->data_read;
  573. break;
  574. case gpio_reg_input_mask:
  575. value = set->input_mask;
  576. break;
  577. default:
  578. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  579. PRIx64"\n", __func__, offset);
  580. return 0;
  581. }
  582. trace_aspeed_gpio_read(offset, value);
  583. return value;
  584. }
  585. static void aspeed_gpio_write_index_mode(void *opaque, hwaddr offset,
  586. uint64_t data, uint32_t size)
  587. {
  588. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  589. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  590. const GPIOSetProperties *props;
  591. GPIOSets *set;
  592. uint32_t reg_idx_number = FIELD_EX32(data, GPIO_INDEX_REG, NUMBER);
  593. uint32_t reg_idx_type = FIELD_EX32(data, GPIO_INDEX_REG, TYPE);
  594. uint32_t reg_idx_command = FIELD_EX32(data, GPIO_INDEX_REG, COMMAND);
  595. uint32_t set_idx = reg_idx_number / ASPEED_GPIOS_PER_SET;
  596. uint32_t pin_idx = reg_idx_number % ASPEED_GPIOS_PER_SET;
  597. uint32_t group_idx = pin_idx / GPIOS_PER_GROUP;
  598. uint32_t reg_value = 0;
  599. uint32_t cleared;
  600. set = &s->sets[set_idx];
  601. props = &agc->props[set_idx];
  602. if (reg_idx_command)
  603. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  604. PRIx64 "index mode wrong command 0x%x\n",
  605. __func__, offset, data, reg_idx_command);
  606. switch (reg_idx_type) {
  607. case gpio_reg_idx_data:
  608. reg_value = set->data_read;
  609. reg_value = deposit32(reg_value, pin_idx, 1,
  610. FIELD_EX32(data, GPIO_INDEX_REG, DATA_VALUE));
  611. reg_value &= props->output;
  612. reg_value = update_value_control_source(set, set->data_value,
  613. reg_value);
  614. set->data_read = reg_value;
  615. aspeed_gpio_update(s, set, reg_value, set->direction);
  616. return;
  617. case gpio_reg_idx_direction:
  618. reg_value = set->direction;
  619. reg_value = deposit32(reg_value, pin_idx, 1,
  620. FIELD_EX32(data, GPIO_INDEX_REG, DIRECTION));
  621. /*
  622. * where data is the value attempted to be written to the pin:
  623. * pin type | input mask | output mask | expected value
  624. * ------------------------------------------------------------
  625. * bidirectional | 1 | 1 | data
  626. * input only | 1 | 0 | 0
  627. * output only | 0 | 1 | 1
  628. * no pin | 0 | 0 | 0
  629. *
  630. * which is captured by:
  631. * data = ( data | ~input) & output;
  632. */
  633. reg_value = (reg_value | ~props->input) & props->output;
  634. set->direction = update_value_control_source(set, set->direction,
  635. reg_value);
  636. break;
  637. case gpio_reg_idx_interrupt:
  638. reg_value = set->int_enable;
  639. reg_value = deposit32(reg_value, pin_idx, 1,
  640. FIELD_EX32(data, GPIO_INDEX_REG, INT_ENABLE));
  641. set->int_enable = update_value_control_source(set, set->int_enable,
  642. reg_value);
  643. reg_value = set->int_sens_0;
  644. reg_value = deposit32(reg_value, pin_idx, 1,
  645. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_0));
  646. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  647. reg_value);
  648. reg_value = set->int_sens_1;
  649. reg_value = deposit32(reg_value, pin_idx, 1,
  650. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_1));
  651. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  652. reg_value);
  653. reg_value = set->int_sens_2;
  654. reg_value = deposit32(reg_value, pin_idx, 1,
  655. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_2));
  656. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  657. reg_value);
  658. /* set interrupt status */
  659. reg_value = set->int_status;
  660. reg_value = deposit32(reg_value, pin_idx, 1,
  661. FIELD_EX32(data, GPIO_INDEX_REG, INT_STATUS));
  662. cleared = ctpop32(reg_value & set->int_status);
  663. if (s->pending && cleared) {
  664. assert(s->pending >= cleared);
  665. s->pending -= cleared;
  666. }
  667. set->int_status &= ~reg_value;
  668. break;
  669. case gpio_reg_idx_debounce:
  670. reg_value = set->debounce_1;
  671. reg_value = deposit32(reg_value, pin_idx, 1,
  672. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_1));
  673. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  674. reg_value);
  675. reg_value = set->debounce_2;
  676. reg_value = deposit32(reg_value, pin_idx, 1,
  677. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_2));
  678. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  679. reg_value);
  680. return;
  681. case gpio_reg_idx_tolerance:
  682. reg_value = set->reset_tol;
  683. reg_value = deposit32(reg_value, pin_idx, 1,
  684. FIELD_EX32(data, GPIO_INDEX_REG, RESET_TOLERANT));
  685. set->reset_tol = update_value_control_source(set, set->reset_tol,
  686. reg_value);
  687. return;
  688. case gpio_reg_idx_cmd_src:
  689. reg_value = set->cmd_source_0;
  690. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  691. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_0));
  692. set->cmd_source_0 = reg_value & ASPEED_CMD_SRC_MASK;
  693. reg_value = set->cmd_source_1;
  694. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  695. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_1));
  696. set->cmd_source_1 = reg_value & ASPEED_CMD_SRC_MASK;
  697. return;
  698. case gpio_reg_idx_input_mask:
  699. reg_value = set->input_mask;
  700. reg_value = deposit32(reg_value, pin_idx, 1,
  701. FIELD_EX32(data, GPIO_INDEX_REG, INPUT_MASK));
  702. /*
  703. * feeds into interrupt generation
  704. * 0: read from data value reg will be updated
  705. * 1: read from data value reg will not be updated
  706. */
  707. set->input_mask = reg_value & props->input;
  708. break;
  709. default:
  710. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  711. PRIx64 "index mode wrong type 0x%x\n",
  712. __func__, offset, data, reg_idx_type);
  713. return;
  714. }
  715. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  716. return;
  717. }
  718. static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,
  719. uint32_t size)
  720. {
  721. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  722. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  723. const GPIOSetProperties *props;
  724. uint64_t idx = -1;
  725. const AspeedGPIOReg *reg;
  726. GPIOSets *set;
  727. uint32_t cleared;
  728. trace_aspeed_gpio_write(offset, data);
  729. idx = offset >> 2;
  730. /* check gpio index mode */
  731. if (idx == R_GPIO_INDEX_REG) {
  732. aspeed_gpio_write_index_mode(opaque, offset, data, size);
  733. return;
  734. }
  735. if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) {
  736. idx -= GPIO_DEBOUNCE_TIME_1;
  737. s->debounce_regs[idx] = (uint32_t) data;
  738. return;
  739. }
  740. reg = &agc->reg_table[idx];
  741. if (reg->set_idx >= agc->nr_gpio_sets) {
  742. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  743. PRIx64"\n", __func__, offset);
  744. return;
  745. }
  746. set = &s->sets[reg->set_idx];
  747. props = &agc->props[reg->set_idx];
  748. switch (reg->type) {
  749. case gpio_reg_data_value:
  750. data &= props->output;
  751. data = update_value_control_source(set, set->data_value, data);
  752. set->data_read = data;
  753. aspeed_gpio_update(s, set, data, set->direction);
  754. return;
  755. case gpio_reg_direction:
  756. /*
  757. * where data is the value attempted to be written to the pin:
  758. * pin type | input mask | output mask | expected value
  759. * ------------------------------------------------------------
  760. * bidirectional | 1 | 1 | data
  761. * input only | 1 | 0 | 0
  762. * output only | 0 | 1 | 1
  763. * no pin | 0 | 0 | 0
  764. *
  765. * which is captured by:
  766. * data = ( data | ~input) & output;
  767. */
  768. data = (data | ~props->input) & props->output;
  769. set->direction = update_value_control_source(set, set->direction, data);
  770. break;
  771. case gpio_reg_int_enable:
  772. set->int_enable = update_value_control_source(set, set->int_enable,
  773. data);
  774. break;
  775. case gpio_reg_int_sens_0:
  776. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  777. data);
  778. break;
  779. case gpio_reg_int_sens_1:
  780. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  781. data);
  782. break;
  783. case gpio_reg_int_sens_2:
  784. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  785. data);
  786. break;
  787. case gpio_reg_int_status:
  788. cleared = ctpop32(data & set->int_status);
  789. if (s->pending && cleared) {
  790. assert(s->pending >= cleared);
  791. s->pending -= cleared;
  792. }
  793. set->int_status &= ~data;
  794. break;
  795. case gpio_reg_reset_tolerant:
  796. set->reset_tol = update_value_control_source(set, set->reset_tol,
  797. data);
  798. return;
  799. case gpio_reg_debounce_1:
  800. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  801. data);
  802. return;
  803. case gpio_reg_debounce_2:
  804. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  805. data);
  806. return;
  807. case gpio_reg_cmd_source_0:
  808. set->cmd_source_0 = data & ASPEED_CMD_SRC_MASK;
  809. return;
  810. case gpio_reg_cmd_source_1:
  811. set->cmd_source_1 = data & ASPEED_CMD_SRC_MASK;
  812. return;
  813. case gpio_reg_data_read:
  814. /* Read only register */
  815. return;
  816. case gpio_reg_input_mask:
  817. /*
  818. * feeds into interrupt generation
  819. * 0: read from data value reg will be updated
  820. * 1: read from data value reg will not be updated
  821. */
  822. set->input_mask = data & props->input;
  823. break;
  824. default:
  825. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  826. PRIx64"\n", __func__, offset);
  827. return;
  828. }
  829. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  830. return;
  831. }
  832. static int get_set_idx(AspeedGPIOState *s, const char *group, int *group_idx)
  833. {
  834. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  835. int set_idx, g_idx;
  836. for (set_idx = 0; set_idx < agc->nr_gpio_sets; set_idx++) {
  837. const GPIOSetProperties *set_props = &agc->props[set_idx];
  838. for (g_idx = 0; g_idx < ASPEED_GROUPS_PER_SET; g_idx++) {
  839. if (!strncmp(group, set_props->group_label[g_idx], strlen(group))) {
  840. *group_idx = g_idx;
  841. return set_idx;
  842. }
  843. }
  844. }
  845. return -1;
  846. }
  847. static void aspeed_gpio_get_pin(Object *obj, Visitor *v, const char *name,
  848. void *opaque, Error **errp)
  849. {
  850. int pin = 0xfff;
  851. bool level = true;
  852. char group[4];
  853. AspeedGPIOState *s = ASPEED_GPIO(obj);
  854. int set_idx, group_idx = 0;
  855. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  856. /* 1.8V gpio */
  857. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  858. error_setg(errp, "%s: error reading %s", __func__, name);
  859. return;
  860. }
  861. }
  862. set_idx = get_set_idx(s, group, &group_idx);
  863. if (set_idx == -1) {
  864. error_setg(errp, "%s: invalid group %s", __func__, group);
  865. return;
  866. }
  867. pin = pin + group_idx * GPIOS_PER_GROUP;
  868. level = aspeed_gpio_get_pin_level(s, set_idx, pin);
  869. visit_type_bool(v, name, &level, errp);
  870. }
  871. static void aspeed_gpio_set_pin(Object *obj, Visitor *v, const char *name,
  872. void *opaque, Error **errp)
  873. {
  874. bool level;
  875. int pin = 0xfff;
  876. char group[4];
  877. AspeedGPIOState *s = ASPEED_GPIO(obj);
  878. int set_idx, group_idx = 0;
  879. if (!visit_type_bool(v, name, &level, errp)) {
  880. return;
  881. }
  882. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  883. /* 1.8V gpio */
  884. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  885. error_setg(errp, "%s: error reading %s", __func__, name);
  886. return;
  887. }
  888. }
  889. set_idx = get_set_idx(s, group, &group_idx);
  890. if (set_idx == -1) {
  891. error_setg(errp, "%s: invalid group %s", __func__, group);
  892. return;
  893. }
  894. pin = pin + group_idx * GPIOS_PER_GROUP;
  895. aspeed_gpio_set_pin_level(s, set_idx, pin, level);
  896. }
  897. /****************** Setup functions ******************/
  898. static const GPIOSetProperties ast2400_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  899. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  900. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  901. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  902. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  903. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  904. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  905. [6] = {0x0000000f, 0x0fffff0f, {"Y", "Z", "AA", "AB"} },
  906. };
  907. static const GPIOSetProperties ast2500_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  908. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  909. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  910. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  911. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  912. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  913. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  914. [6] = {0x0fffffff, 0x0fffffff, {"Y", "Z", "AA", "AB"} },
  915. [7] = {0x000000ff, 0x000000ff, {"AC"} },
  916. };
  917. static GPIOSetProperties ast2600_3_3v_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, 0x00ffffff, {"Q", "R", "S", "T"} },
  923. [5] = {0xffffffff, 0xffffff00, {"U", "V", "W", "X"} },
  924. [6] = {0x0000ffff, 0x0000ffff, {"Y", "Z"} },
  925. };
  926. static GPIOSetProperties ast2600_1_8v_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  927. [0] = {0xffffffff, 0xffffffff, {"18A", "18B", "18C", "18D"} },
  928. [1] = {0x0000000f, 0x0000000f, {"18E"} },
  929. };
  930. static GPIOSetProperties ast1030_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  931. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  932. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  933. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  934. [3] = {0xffffff3f, 0xffffff3f, {"M", "N", "O", "P"} },
  935. [4] = {0xff060c1f, 0x00060c1f, {"Q", "R", "S", "T"} },
  936. [5] = {0x000000ff, 0x00000000, {"U"} },
  937. };
  938. static const MemoryRegionOps aspeed_gpio_ops = {
  939. .read = aspeed_gpio_read,
  940. .write = aspeed_gpio_write,
  941. .endianness = DEVICE_LITTLE_ENDIAN,
  942. .valid.min_access_size = 4,
  943. .valid.max_access_size = 4,
  944. };
  945. static void aspeed_gpio_reset(DeviceState *dev)
  946. {
  947. AspeedGPIOState *s = ASPEED_GPIO(dev);
  948. /* TODO: respect the reset tolerance registers */
  949. memset(s->sets, 0, sizeof(s->sets));
  950. }
  951. static void aspeed_gpio_realize(DeviceState *dev, Error **errp)
  952. {
  953. AspeedGPIOState *s = ASPEED_GPIO(dev);
  954. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  955. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  956. /* Interrupt parent line */
  957. sysbus_init_irq(sbd, &s->irq);
  958. /* Individual GPIOs */
  959. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  960. const GPIOSetProperties *props = &agc->props[i];
  961. uint32_t skip = ~(props->input | props->output);
  962. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  963. if (skip >> j & 1) {
  964. continue;
  965. }
  966. sysbus_init_irq(sbd, &s->gpios[i][j]);
  967. }
  968. }
  969. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_gpio_ops, s,
  970. TYPE_ASPEED_GPIO, 0x800);
  971. sysbus_init_mmio(sbd, &s->iomem);
  972. }
  973. static void aspeed_gpio_init(Object *obj)
  974. {
  975. AspeedGPIOState *s = ASPEED_GPIO(obj);
  976. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  977. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  978. const GPIOSetProperties *props = &agc->props[i];
  979. uint32_t skip = ~(props->input | props->output);
  980. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  981. if (skip >> j & 1) {
  982. continue;
  983. }
  984. int group_idx = j / GPIOS_PER_GROUP;
  985. int pin_idx = j % GPIOS_PER_GROUP;
  986. const char *group = &props->group_label[group_idx][0];
  987. char *name = g_strdup_printf("gpio%s%d", group, pin_idx);
  988. object_property_add(obj, name, "bool", aspeed_gpio_get_pin,
  989. aspeed_gpio_set_pin, NULL, NULL);
  990. g_free(name);
  991. }
  992. }
  993. }
  994. static const VMStateDescription vmstate_gpio_regs = {
  995. .name = TYPE_ASPEED_GPIO"/regs",
  996. .version_id = 1,
  997. .minimum_version_id = 1,
  998. .fields = (VMStateField[]) {
  999. VMSTATE_UINT32(data_value, GPIOSets),
  1000. VMSTATE_UINT32(data_read, GPIOSets),
  1001. VMSTATE_UINT32(direction, GPIOSets),
  1002. VMSTATE_UINT32(int_enable, GPIOSets),
  1003. VMSTATE_UINT32(int_sens_0, GPIOSets),
  1004. VMSTATE_UINT32(int_sens_1, GPIOSets),
  1005. VMSTATE_UINT32(int_sens_2, GPIOSets),
  1006. VMSTATE_UINT32(int_status, GPIOSets),
  1007. VMSTATE_UINT32(reset_tol, GPIOSets),
  1008. VMSTATE_UINT32(cmd_source_0, GPIOSets),
  1009. VMSTATE_UINT32(cmd_source_1, GPIOSets),
  1010. VMSTATE_UINT32(debounce_1, GPIOSets),
  1011. VMSTATE_UINT32(debounce_2, GPIOSets),
  1012. VMSTATE_UINT32(input_mask, GPIOSets),
  1013. VMSTATE_END_OF_LIST(),
  1014. }
  1015. };
  1016. static const VMStateDescription vmstate_aspeed_gpio = {
  1017. .name = TYPE_ASPEED_GPIO,
  1018. .version_id = 1,
  1019. .minimum_version_id = 1,
  1020. .fields = (VMStateField[]) {
  1021. VMSTATE_STRUCT_ARRAY(sets, AspeedGPIOState, ASPEED_GPIO_MAX_NR_SETS,
  1022. 1, vmstate_gpio_regs, GPIOSets),
  1023. VMSTATE_UINT32_ARRAY(debounce_regs, AspeedGPIOState,
  1024. ASPEED_GPIO_NR_DEBOUNCE_REGS),
  1025. VMSTATE_END_OF_LIST(),
  1026. }
  1027. };
  1028. static void aspeed_gpio_class_init(ObjectClass *klass, void *data)
  1029. {
  1030. DeviceClass *dc = DEVICE_CLASS(klass);
  1031. dc->realize = aspeed_gpio_realize;
  1032. dc->reset = aspeed_gpio_reset;
  1033. dc->desc = "Aspeed GPIO Controller";
  1034. dc->vmsd = &vmstate_aspeed_gpio;
  1035. }
  1036. static void aspeed_gpio_ast2400_class_init(ObjectClass *klass, void *data)
  1037. {
  1038. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1039. agc->props = ast2400_set_props;
  1040. agc->nr_gpio_pins = 216;
  1041. agc->nr_gpio_sets = 7;
  1042. agc->reg_table = aspeed_3_3v_gpios;
  1043. }
  1044. static void aspeed_gpio_2500_class_init(ObjectClass *klass, void *data)
  1045. {
  1046. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1047. agc->props = ast2500_set_props;
  1048. agc->nr_gpio_pins = 228;
  1049. agc->nr_gpio_sets = 8;
  1050. agc->reg_table = aspeed_3_3v_gpios;
  1051. }
  1052. static void aspeed_gpio_ast2600_3_3v_class_init(ObjectClass *klass, void *data)
  1053. {
  1054. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1055. agc->props = ast2600_3_3v_set_props;
  1056. agc->nr_gpio_pins = 208;
  1057. agc->nr_gpio_sets = 7;
  1058. agc->reg_table = aspeed_3_3v_gpios;
  1059. }
  1060. static void aspeed_gpio_ast2600_1_8v_class_init(ObjectClass *klass, void *data)
  1061. {
  1062. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1063. agc->props = ast2600_1_8v_set_props;
  1064. agc->nr_gpio_pins = 36;
  1065. agc->nr_gpio_sets = 2;
  1066. agc->reg_table = aspeed_1_8v_gpios;
  1067. }
  1068. static void aspeed_gpio_1030_class_init(ObjectClass *klass, void *data)
  1069. {
  1070. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1071. agc->props = ast1030_set_props;
  1072. agc->nr_gpio_pins = 151;
  1073. agc->nr_gpio_sets = 6;
  1074. agc->reg_table = aspeed_3_3v_gpios;
  1075. }
  1076. static const TypeInfo aspeed_gpio_info = {
  1077. .name = TYPE_ASPEED_GPIO,
  1078. .parent = TYPE_SYS_BUS_DEVICE,
  1079. .instance_size = sizeof(AspeedGPIOState),
  1080. .class_size = sizeof(AspeedGPIOClass),
  1081. .class_init = aspeed_gpio_class_init,
  1082. .abstract = true,
  1083. };
  1084. static const TypeInfo aspeed_gpio_ast2400_info = {
  1085. .name = TYPE_ASPEED_GPIO "-ast2400",
  1086. .parent = TYPE_ASPEED_GPIO,
  1087. .class_init = aspeed_gpio_ast2400_class_init,
  1088. .instance_init = aspeed_gpio_init,
  1089. };
  1090. static const TypeInfo aspeed_gpio_ast2500_info = {
  1091. .name = TYPE_ASPEED_GPIO "-ast2500",
  1092. .parent = TYPE_ASPEED_GPIO,
  1093. .class_init = aspeed_gpio_2500_class_init,
  1094. .instance_init = aspeed_gpio_init,
  1095. };
  1096. static const TypeInfo aspeed_gpio_ast2600_3_3v_info = {
  1097. .name = TYPE_ASPEED_GPIO "-ast2600",
  1098. .parent = TYPE_ASPEED_GPIO,
  1099. .class_init = aspeed_gpio_ast2600_3_3v_class_init,
  1100. .instance_init = aspeed_gpio_init,
  1101. };
  1102. static const TypeInfo aspeed_gpio_ast2600_1_8v_info = {
  1103. .name = TYPE_ASPEED_GPIO "-ast2600-1_8v",
  1104. .parent = TYPE_ASPEED_GPIO,
  1105. .class_init = aspeed_gpio_ast2600_1_8v_class_init,
  1106. .instance_init = aspeed_gpio_init,
  1107. };
  1108. static const TypeInfo aspeed_gpio_ast1030_info = {
  1109. .name = TYPE_ASPEED_GPIO "-ast1030",
  1110. .parent = TYPE_ASPEED_GPIO,
  1111. .class_init = aspeed_gpio_1030_class_init,
  1112. .instance_init = aspeed_gpio_init,
  1113. };
  1114. static void aspeed_gpio_register_types(void)
  1115. {
  1116. type_register_static(&aspeed_gpio_info);
  1117. type_register_static(&aspeed_gpio_ast2400_info);
  1118. type_register_static(&aspeed_gpio_ast2500_info);
  1119. type_register_static(&aspeed_gpio_ast2600_3_3v_info);
  1120. type_register_static(&aspeed_gpio_ast2600_1_8v_info);
  1121. type_register_static(&aspeed_gpio_ast1030_info);
  1122. }
  1123. type_init(aspeed_gpio_register_types);