aspeed_gpio.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. /* AST2700 GPIO Register Address Offsets */
  223. REG32(GPIO_2700_DEBOUNCE_TIME_1, 0x000)
  224. REG32(GPIO_2700_DEBOUNCE_TIME_2, 0x004)
  225. REG32(GPIO_2700_DEBOUNCE_TIME_3, 0x008)
  226. REG32(GPIO_2700_INT_STATUS_1, 0x100)
  227. REG32(GPIO_2700_INT_STATUS_2, 0x104)
  228. REG32(GPIO_2700_INT_STATUS_3, 0x108)
  229. REG32(GPIO_2700_INT_STATUS_4, 0x10C)
  230. REG32(GPIO_2700_INT_STATUS_5, 0x110)
  231. REG32(GPIO_2700_INT_STATUS_6, 0x114)
  232. REG32(GPIO_2700_INT_STATUS_7, 0x118)
  233. /* GPIOA0 - GPIOAA7 Control Register */
  234. REG32(GPIO_A0_CONTROL, 0x180)
  235. SHARED_FIELD(GPIO_CONTROL_OUT_DATA, 0, 1)
  236. SHARED_FIELD(GPIO_CONTROL_DIRECTION, 1, 1)
  237. SHARED_FIELD(GPIO_CONTROL_INT_ENABLE, 2, 1)
  238. SHARED_FIELD(GPIO_CONTROL_INT_SENS_0, 3, 1)
  239. SHARED_FIELD(GPIO_CONTROL_INT_SENS_1, 4, 1)
  240. SHARED_FIELD(GPIO_CONTROL_INT_SENS_2, 5, 1)
  241. SHARED_FIELD(GPIO_CONTROL_RESET_TOLERANCE, 6, 1)
  242. SHARED_FIELD(GPIO_CONTROL_DEBOUNCE_1, 7, 1)
  243. SHARED_FIELD(GPIO_CONTROL_DEBOUNCE_2, 8, 1)
  244. SHARED_FIELD(GPIO_CONTROL_INPUT_MASK, 9, 1)
  245. SHARED_FIELD(GPIO_CONTROL_BLINK_COUNTER_1, 10, 1)
  246. SHARED_FIELD(GPIO_CONTROL_BLINK_COUNTER_2, 11, 1)
  247. SHARED_FIELD(GPIO_CONTROL_INT_STATUS, 12, 1)
  248. SHARED_FIELD(GPIO_CONTROL_IN_DATA, 13, 1)
  249. SHARED_FIELD(GPIO_CONTROL_RESERVED, 14, 18)
  250. REG32(GPIO_AA7_CONTROL, 0x4DC)
  251. #define GPIO_2700_MEM_SIZE 0x4E0
  252. #define GPIO_2700_REG_ARRAY_SIZE (GPIO_2700_MEM_SIZE >> 2)
  253. static int aspeed_evaluate_irq(GPIOSets *regs, int gpio_prev_high, int gpio)
  254. {
  255. uint32_t falling_edge = 0, rising_edge = 0;
  256. uint32_t int_trigger = extract32(regs->int_sens_0, gpio, 1)
  257. | extract32(regs->int_sens_1, gpio, 1) << 1
  258. | extract32(regs->int_sens_2, gpio, 1) << 2;
  259. uint32_t gpio_curr_high = extract32(regs->data_value, gpio, 1);
  260. uint32_t gpio_int_enabled = extract32(regs->int_enable, gpio, 1);
  261. if (!gpio_int_enabled) {
  262. return 0;
  263. }
  264. /* Detect edges */
  265. if (gpio_curr_high && !gpio_prev_high) {
  266. rising_edge = 1;
  267. } else if (!gpio_curr_high && gpio_prev_high) {
  268. falling_edge = 1;
  269. }
  270. if (((int_trigger == ASPEED_FALLING_EDGE) && falling_edge) ||
  271. ((int_trigger == ASPEED_RISING_EDGE) && rising_edge) ||
  272. ((int_trigger == ASPEED_LEVEL_LOW) && !gpio_curr_high) ||
  273. ((int_trigger == ASPEED_LEVEL_HIGH) && gpio_curr_high) ||
  274. ((int_trigger >= ASPEED_DUAL_EDGE) && (rising_edge || falling_edge)))
  275. {
  276. regs->int_status = deposit32(regs->int_status, gpio, 1, 1);
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. #define nested_struct_index(ta, pa, m, tb, pb) \
  282. (pb - ((tb *)(((char *)pa) + offsetof(ta, m))))
  283. static ptrdiff_t aspeed_gpio_set_idx(AspeedGPIOState *s, GPIOSets *regs)
  284. {
  285. return nested_struct_index(AspeedGPIOState, s, sets, GPIOSets, regs);
  286. }
  287. static void aspeed_gpio_update(AspeedGPIOState *s, GPIOSets *regs,
  288. uint32_t value, uint32_t mode_mask)
  289. {
  290. uint32_t input_mask = regs->input_mask;
  291. uint32_t direction = regs->direction;
  292. uint32_t old = regs->data_value;
  293. uint32_t new = value;
  294. uint32_t diff;
  295. int gpio;
  296. diff = (old ^ new);
  297. diff &= mode_mask;
  298. if (diff) {
  299. for (gpio = 0; gpio < ASPEED_GPIOS_PER_SET; gpio++) {
  300. uint32_t mask = 1U << gpio;
  301. /* If the gpio needs to be updated... */
  302. if (!(diff & mask)) {
  303. continue;
  304. }
  305. /* ...and we're output or not input-masked... */
  306. if (!(direction & mask) && (input_mask & mask)) {
  307. continue;
  308. }
  309. /* ...then update the state. */
  310. if (mask & new) {
  311. regs->data_value |= mask;
  312. } else {
  313. regs->data_value &= ~mask;
  314. }
  315. /* If the gpio is set to output... */
  316. if (direction & mask) {
  317. /* ...trigger the line-state IRQ */
  318. ptrdiff_t set = aspeed_gpio_set_idx(s, regs);
  319. qemu_set_irq(s->gpios[set][gpio], !!(new & mask));
  320. } else {
  321. /* ...otherwise if we meet the line's current IRQ policy... */
  322. if (aspeed_evaluate_irq(regs, old & mask, gpio)) {
  323. /* ...trigger the VIC IRQ */
  324. s->pending++;
  325. }
  326. }
  327. }
  328. }
  329. qemu_set_irq(s->irq, !!(s->pending));
  330. }
  331. static bool aspeed_gpio_get_pin_level(AspeedGPIOState *s, uint32_t set_idx,
  332. uint32_t pin)
  333. {
  334. uint32_t reg_val;
  335. uint32_t pin_mask = 1 << pin;
  336. reg_val = s->sets[set_idx].data_value;
  337. return !!(reg_val & pin_mask);
  338. }
  339. static void aspeed_gpio_set_pin_level(AspeedGPIOState *s, uint32_t set_idx,
  340. uint32_t pin, bool level)
  341. {
  342. uint32_t value = s->sets[set_idx].data_value;
  343. uint32_t pin_mask = 1 << pin;
  344. if (level) {
  345. value |= pin_mask;
  346. } else {
  347. value &= ~pin_mask;
  348. }
  349. aspeed_gpio_update(s, &s->sets[set_idx], value,
  350. ~s->sets[set_idx].direction);
  351. }
  352. /*
  353. * | src_1 | src_2 | source |
  354. * |-----------------------------|
  355. * | 0 | 0 | ARM |
  356. * | 0 | 1 | LPC |
  357. * | 1 | 0 | Coprocessor|
  358. * | 1 | 1 | Reserved |
  359. *
  360. * Once the source of a set is programmed, corresponding bits in the
  361. * data_value, direction, interrupt [enable, sens[0-2]], reset_tol and
  362. * debounce registers can only be written by the source.
  363. *
  364. * Source is ARM by default
  365. * only bits 24, 16, 8, and 0 can be set
  366. *
  367. * we don't currently have a model for the LPC or Coprocessor
  368. */
  369. static uint32_t update_value_control_source(GPIOSets *regs, uint32_t old_value,
  370. uint32_t value)
  371. {
  372. int i;
  373. int cmd_source;
  374. /* assume the source is always ARM for now */
  375. int source = ASPEED_SOURCE_ARM;
  376. uint32_t new_value = 0;
  377. /* for each group in set */
  378. for (i = 0; i < ASPEED_GPIOS_PER_SET; i += GPIOS_PER_GROUP) {
  379. cmd_source = extract32(regs->cmd_source_0, i, 1)
  380. | (extract32(regs->cmd_source_1, i, 1) << 1);
  381. if (source == cmd_source) {
  382. new_value |= (0xff << i) & value;
  383. } else {
  384. new_value |= (0xff << i) & old_value;
  385. }
  386. }
  387. return new_value;
  388. }
  389. static const AspeedGPIOReg aspeed_3_3v_gpios[GPIO_3_3V_REG_ARRAY_SIZE] = {
  390. /* Set ABCD */
  391. [GPIO_ABCD_DATA_VALUE] = { 0, gpio_reg_data_value },
  392. [GPIO_ABCD_DIRECTION] = { 0, gpio_reg_direction },
  393. [GPIO_ABCD_INT_ENABLE] = { 0, gpio_reg_int_enable },
  394. [GPIO_ABCD_INT_SENS_0] = { 0, gpio_reg_int_sens_0 },
  395. [GPIO_ABCD_INT_SENS_1] = { 0, gpio_reg_int_sens_1 },
  396. [GPIO_ABCD_INT_SENS_2] = { 0, gpio_reg_int_sens_2 },
  397. [GPIO_ABCD_INT_STATUS] = { 0, gpio_reg_int_status },
  398. [GPIO_ABCD_RESET_TOLERANT] = { 0, gpio_reg_reset_tolerant },
  399. [GPIO_ABCD_DEBOUNCE_1] = { 0, gpio_reg_debounce_1 },
  400. [GPIO_ABCD_DEBOUNCE_2] = { 0, gpio_reg_debounce_2 },
  401. [GPIO_ABCD_COMMAND_SRC_0] = { 0, gpio_reg_cmd_source_0 },
  402. [GPIO_ABCD_COMMAND_SRC_1] = { 0, gpio_reg_cmd_source_1 },
  403. [GPIO_ABCD_DATA_READ] = { 0, gpio_reg_data_read },
  404. [GPIO_ABCD_INPUT_MASK] = { 0, gpio_reg_input_mask },
  405. /* Set EFGH */
  406. [GPIO_EFGH_DATA_VALUE] = { 1, gpio_reg_data_value },
  407. [GPIO_EFGH_DIRECTION] = { 1, gpio_reg_direction },
  408. [GPIO_EFGH_INT_ENABLE] = { 1, gpio_reg_int_enable },
  409. [GPIO_EFGH_INT_SENS_0] = { 1, gpio_reg_int_sens_0 },
  410. [GPIO_EFGH_INT_SENS_1] = { 1, gpio_reg_int_sens_1 },
  411. [GPIO_EFGH_INT_SENS_2] = { 1, gpio_reg_int_sens_2 },
  412. [GPIO_EFGH_INT_STATUS] = { 1, gpio_reg_int_status },
  413. [GPIO_EFGH_RESET_TOLERANT] = { 1, gpio_reg_reset_tolerant },
  414. [GPIO_EFGH_DEBOUNCE_1] = { 1, gpio_reg_debounce_1 },
  415. [GPIO_EFGH_DEBOUNCE_2] = { 1, gpio_reg_debounce_2 },
  416. [GPIO_EFGH_COMMAND_SRC_0] = { 1, gpio_reg_cmd_source_0 },
  417. [GPIO_EFGH_COMMAND_SRC_1] = { 1, gpio_reg_cmd_source_1 },
  418. [GPIO_EFGH_DATA_READ] = { 1, gpio_reg_data_read },
  419. [GPIO_EFGH_INPUT_MASK] = { 1, gpio_reg_input_mask },
  420. /* Set IJKL */
  421. [GPIO_IJKL_DATA_VALUE] = { 2, gpio_reg_data_value },
  422. [GPIO_IJKL_DIRECTION] = { 2, gpio_reg_direction },
  423. [GPIO_IJKL_INT_ENABLE] = { 2, gpio_reg_int_enable },
  424. [GPIO_IJKL_INT_SENS_0] = { 2, gpio_reg_int_sens_0 },
  425. [GPIO_IJKL_INT_SENS_1] = { 2, gpio_reg_int_sens_1 },
  426. [GPIO_IJKL_INT_SENS_2] = { 2, gpio_reg_int_sens_2 },
  427. [GPIO_IJKL_INT_STATUS] = { 2, gpio_reg_int_status },
  428. [GPIO_IJKL_RESET_TOLERANT] = { 2, gpio_reg_reset_tolerant },
  429. [GPIO_IJKL_DEBOUNCE_1] = { 2, gpio_reg_debounce_1 },
  430. [GPIO_IJKL_DEBOUNCE_2] = { 2, gpio_reg_debounce_2 },
  431. [GPIO_IJKL_COMMAND_SRC_0] = { 2, gpio_reg_cmd_source_0 },
  432. [GPIO_IJKL_COMMAND_SRC_1] = { 2, gpio_reg_cmd_source_1 },
  433. [GPIO_IJKL_DATA_READ] = { 2, gpio_reg_data_read },
  434. [GPIO_IJKL_INPUT_MASK] = { 2, gpio_reg_input_mask },
  435. /* Set MNOP */
  436. [GPIO_MNOP_DATA_VALUE] = { 3, gpio_reg_data_value },
  437. [GPIO_MNOP_DIRECTION] = { 3, gpio_reg_direction },
  438. [GPIO_MNOP_INT_ENABLE] = { 3, gpio_reg_int_enable },
  439. [GPIO_MNOP_INT_SENS_0] = { 3, gpio_reg_int_sens_0 },
  440. [GPIO_MNOP_INT_SENS_1] = { 3, gpio_reg_int_sens_1 },
  441. [GPIO_MNOP_INT_SENS_2] = { 3, gpio_reg_int_sens_2 },
  442. [GPIO_MNOP_INT_STATUS] = { 3, gpio_reg_int_status },
  443. [GPIO_MNOP_RESET_TOLERANT] = { 3, gpio_reg_reset_tolerant },
  444. [GPIO_MNOP_DEBOUNCE_1] = { 3, gpio_reg_debounce_1 },
  445. [GPIO_MNOP_DEBOUNCE_2] = { 3, gpio_reg_debounce_2 },
  446. [GPIO_MNOP_COMMAND_SRC_0] = { 3, gpio_reg_cmd_source_0 },
  447. [GPIO_MNOP_COMMAND_SRC_1] = { 3, gpio_reg_cmd_source_1 },
  448. [GPIO_MNOP_DATA_READ] = { 3, gpio_reg_data_read },
  449. [GPIO_MNOP_INPUT_MASK] = { 3, gpio_reg_input_mask },
  450. /* Set QRST */
  451. [GPIO_QRST_DATA_VALUE] = { 4, gpio_reg_data_value },
  452. [GPIO_QRST_DIRECTION] = { 4, gpio_reg_direction },
  453. [GPIO_QRST_INT_ENABLE] = { 4, gpio_reg_int_enable },
  454. [GPIO_QRST_INT_SENS_0] = { 4, gpio_reg_int_sens_0 },
  455. [GPIO_QRST_INT_SENS_1] = { 4, gpio_reg_int_sens_1 },
  456. [GPIO_QRST_INT_SENS_2] = { 4, gpio_reg_int_sens_2 },
  457. [GPIO_QRST_INT_STATUS] = { 4, gpio_reg_int_status },
  458. [GPIO_QRST_RESET_TOLERANT] = { 4, gpio_reg_reset_tolerant },
  459. [GPIO_QRST_DEBOUNCE_1] = { 4, gpio_reg_debounce_1 },
  460. [GPIO_QRST_DEBOUNCE_2] = { 4, gpio_reg_debounce_2 },
  461. [GPIO_QRST_COMMAND_SRC_0] = { 4, gpio_reg_cmd_source_0 },
  462. [GPIO_QRST_COMMAND_SRC_1] = { 4, gpio_reg_cmd_source_1 },
  463. [GPIO_QRST_DATA_READ] = { 4, gpio_reg_data_read },
  464. [GPIO_QRST_INPUT_MASK] = { 4, gpio_reg_input_mask },
  465. /* Set UVWX */
  466. [GPIO_UVWX_DATA_VALUE] = { 5, gpio_reg_data_value },
  467. [GPIO_UVWX_DIRECTION] = { 5, gpio_reg_direction },
  468. [GPIO_UVWX_INT_ENABLE] = { 5, gpio_reg_int_enable },
  469. [GPIO_UVWX_INT_SENS_0] = { 5, gpio_reg_int_sens_0 },
  470. [GPIO_UVWX_INT_SENS_1] = { 5, gpio_reg_int_sens_1 },
  471. [GPIO_UVWX_INT_SENS_2] = { 5, gpio_reg_int_sens_2 },
  472. [GPIO_UVWX_INT_STATUS] = { 5, gpio_reg_int_status },
  473. [GPIO_UVWX_RESET_TOLERANT] = { 5, gpio_reg_reset_tolerant },
  474. [GPIO_UVWX_DEBOUNCE_1] = { 5, gpio_reg_debounce_1 },
  475. [GPIO_UVWX_DEBOUNCE_2] = { 5, gpio_reg_debounce_2 },
  476. [GPIO_UVWX_COMMAND_SRC_0] = { 5, gpio_reg_cmd_source_0 },
  477. [GPIO_UVWX_COMMAND_SRC_1] = { 5, gpio_reg_cmd_source_1 },
  478. [GPIO_UVWX_DATA_READ] = { 5, gpio_reg_data_read },
  479. [GPIO_UVWX_INPUT_MASK] = { 5, gpio_reg_input_mask },
  480. /* Set YZAAAB */
  481. [GPIO_YZAAAB_DATA_VALUE] = { 6, gpio_reg_data_value },
  482. [GPIO_YZAAAB_DIRECTION] = { 6, gpio_reg_direction },
  483. [GPIO_YZAAAB_INT_ENABLE] = { 6, gpio_reg_int_enable },
  484. [GPIO_YZAAAB_INT_SENS_0] = { 6, gpio_reg_int_sens_0 },
  485. [GPIO_YZAAAB_INT_SENS_1] = { 6, gpio_reg_int_sens_1 },
  486. [GPIO_YZAAAB_INT_SENS_2] = { 6, gpio_reg_int_sens_2 },
  487. [GPIO_YZAAAB_INT_STATUS] = { 6, gpio_reg_int_status },
  488. [GPIO_YZAAAB_RESET_TOLERANT] = { 6, gpio_reg_reset_tolerant },
  489. [GPIO_YZAAAB_DEBOUNCE_1] = { 6, gpio_reg_debounce_1 },
  490. [GPIO_YZAAAB_DEBOUNCE_2] = { 6, gpio_reg_debounce_2 },
  491. [GPIO_YZAAAB_COMMAND_SRC_0] = { 6, gpio_reg_cmd_source_0 },
  492. [GPIO_YZAAAB_COMMAND_SRC_1] = { 6, gpio_reg_cmd_source_1 },
  493. [GPIO_YZAAAB_DATA_READ] = { 6, gpio_reg_data_read },
  494. [GPIO_YZAAAB_INPUT_MASK] = { 6, gpio_reg_input_mask },
  495. /* Set AC (ast2500 only) */
  496. [GPIO_AC_DATA_VALUE] = { 7, gpio_reg_data_value },
  497. [GPIO_AC_DIRECTION] = { 7, gpio_reg_direction },
  498. [GPIO_AC_INT_ENABLE] = { 7, gpio_reg_int_enable },
  499. [GPIO_AC_INT_SENS_0] = { 7, gpio_reg_int_sens_0 },
  500. [GPIO_AC_INT_SENS_1] = { 7, gpio_reg_int_sens_1 },
  501. [GPIO_AC_INT_SENS_2] = { 7, gpio_reg_int_sens_2 },
  502. [GPIO_AC_INT_STATUS] = { 7, gpio_reg_int_status },
  503. [GPIO_AC_RESET_TOLERANT] = { 7, gpio_reg_reset_tolerant },
  504. [GPIO_AC_DEBOUNCE_1] = { 7, gpio_reg_debounce_1 },
  505. [GPIO_AC_DEBOUNCE_2] = { 7, gpio_reg_debounce_2 },
  506. [GPIO_AC_COMMAND_SRC_0] = { 7, gpio_reg_cmd_source_0 },
  507. [GPIO_AC_COMMAND_SRC_1] = { 7, gpio_reg_cmd_source_1 },
  508. [GPIO_AC_DATA_READ] = { 7, gpio_reg_data_read },
  509. [GPIO_AC_INPUT_MASK] = { 7, gpio_reg_input_mask },
  510. };
  511. static const AspeedGPIOReg aspeed_1_8v_gpios[GPIO_1_8V_REG_ARRAY_SIZE] = {
  512. /* 1.8V Set ABCD */
  513. [GPIO_1_8V_ABCD_DATA_VALUE] = {0, gpio_reg_data_value},
  514. [GPIO_1_8V_ABCD_DIRECTION] = {0, gpio_reg_direction},
  515. [GPIO_1_8V_ABCD_INT_ENABLE] = {0, gpio_reg_int_enable},
  516. [GPIO_1_8V_ABCD_INT_SENS_0] = {0, gpio_reg_int_sens_0},
  517. [GPIO_1_8V_ABCD_INT_SENS_1] = {0, gpio_reg_int_sens_1},
  518. [GPIO_1_8V_ABCD_INT_SENS_2] = {0, gpio_reg_int_sens_2},
  519. [GPIO_1_8V_ABCD_INT_STATUS] = {0, gpio_reg_int_status},
  520. [GPIO_1_8V_ABCD_RESET_TOLERANT] = {0, gpio_reg_reset_tolerant},
  521. [GPIO_1_8V_ABCD_DEBOUNCE_1] = {0, gpio_reg_debounce_1},
  522. [GPIO_1_8V_ABCD_DEBOUNCE_2] = {0, gpio_reg_debounce_2},
  523. [GPIO_1_8V_ABCD_COMMAND_SRC_0] = {0, gpio_reg_cmd_source_0},
  524. [GPIO_1_8V_ABCD_COMMAND_SRC_1] = {0, gpio_reg_cmd_source_1},
  525. [GPIO_1_8V_ABCD_DATA_READ] = {0, gpio_reg_data_read},
  526. [GPIO_1_8V_ABCD_INPUT_MASK] = {0, gpio_reg_input_mask},
  527. /* 1.8V Set E */
  528. [GPIO_1_8V_E_DATA_VALUE] = {1, gpio_reg_data_value},
  529. [GPIO_1_8V_E_DIRECTION] = {1, gpio_reg_direction},
  530. [GPIO_1_8V_E_INT_ENABLE] = {1, gpio_reg_int_enable},
  531. [GPIO_1_8V_E_INT_SENS_0] = {1, gpio_reg_int_sens_0},
  532. [GPIO_1_8V_E_INT_SENS_1] = {1, gpio_reg_int_sens_1},
  533. [GPIO_1_8V_E_INT_SENS_2] = {1, gpio_reg_int_sens_2},
  534. [GPIO_1_8V_E_INT_STATUS] = {1, gpio_reg_int_status},
  535. [GPIO_1_8V_E_RESET_TOLERANT] = {1, gpio_reg_reset_tolerant},
  536. [GPIO_1_8V_E_DEBOUNCE_1] = {1, gpio_reg_debounce_1},
  537. [GPIO_1_8V_E_DEBOUNCE_2] = {1, gpio_reg_debounce_2},
  538. [GPIO_1_8V_E_COMMAND_SRC_0] = {1, gpio_reg_cmd_source_0},
  539. [GPIO_1_8V_E_COMMAND_SRC_1] = {1, gpio_reg_cmd_source_1},
  540. [GPIO_1_8V_E_DATA_READ] = {1, gpio_reg_data_read},
  541. [GPIO_1_8V_E_INPUT_MASK] = {1, gpio_reg_input_mask},
  542. };
  543. static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)
  544. {
  545. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  546. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  547. uint64_t idx = -1;
  548. const AspeedGPIOReg *reg;
  549. GPIOSets *set;
  550. uint32_t value = 0;
  551. uint64_t debounce_value;
  552. idx = offset >> 2;
  553. if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) {
  554. idx -= GPIO_DEBOUNCE_TIME_1;
  555. debounce_value = (uint64_t) s->debounce_regs[idx];
  556. trace_aspeed_gpio_read(offset, debounce_value);
  557. return debounce_value;
  558. }
  559. if (idx >= agc->reg_table_count) {
  560. qemu_log_mask(LOG_GUEST_ERROR, "%s: idx 0x%" PRIx64 " out of bounds\n",
  561. __func__, idx);
  562. return 0;
  563. }
  564. reg = &agc->reg_table[idx];
  565. if (reg->set_idx >= agc->nr_gpio_sets) {
  566. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  567. PRIx64"\n", __func__, offset);
  568. return 0;
  569. }
  570. set = &s->sets[reg->set_idx];
  571. switch (reg->type) {
  572. case gpio_reg_data_value:
  573. value = set->data_value;
  574. break;
  575. case gpio_reg_direction:
  576. value = set->direction;
  577. break;
  578. case gpio_reg_int_enable:
  579. value = set->int_enable;
  580. break;
  581. case gpio_reg_int_sens_0:
  582. value = set->int_sens_0;
  583. break;
  584. case gpio_reg_int_sens_1:
  585. value = set->int_sens_1;
  586. break;
  587. case gpio_reg_int_sens_2:
  588. value = set->int_sens_2;
  589. break;
  590. case gpio_reg_int_status:
  591. value = set->int_status;
  592. break;
  593. case gpio_reg_reset_tolerant:
  594. value = set->reset_tol;
  595. break;
  596. case gpio_reg_debounce_1:
  597. value = set->debounce_1;
  598. break;
  599. case gpio_reg_debounce_2:
  600. value = set->debounce_2;
  601. break;
  602. case gpio_reg_cmd_source_0:
  603. value = set->cmd_source_0;
  604. break;
  605. case gpio_reg_cmd_source_1:
  606. value = set->cmd_source_1;
  607. break;
  608. case gpio_reg_data_read:
  609. value = set->data_read;
  610. break;
  611. case gpio_reg_input_mask:
  612. value = set->input_mask;
  613. break;
  614. default:
  615. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  616. PRIx64"\n", __func__, offset);
  617. return 0;
  618. }
  619. trace_aspeed_gpio_read(offset, value);
  620. return value;
  621. }
  622. static void aspeed_gpio_write_index_mode(void *opaque, hwaddr offset,
  623. uint64_t data, uint32_t size)
  624. {
  625. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  626. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  627. const GPIOSetProperties *props;
  628. GPIOSets *set;
  629. uint32_t reg_idx_number = FIELD_EX32(data, GPIO_INDEX_REG, NUMBER);
  630. uint32_t reg_idx_type = FIELD_EX32(data, GPIO_INDEX_REG, TYPE);
  631. uint32_t reg_idx_command = FIELD_EX32(data, GPIO_INDEX_REG, COMMAND);
  632. uint32_t set_idx = reg_idx_number / ASPEED_GPIOS_PER_SET;
  633. uint32_t pin_idx = reg_idx_number % ASPEED_GPIOS_PER_SET;
  634. uint32_t group_idx = pin_idx / GPIOS_PER_GROUP;
  635. uint32_t reg_value = 0;
  636. uint32_t pending = 0;
  637. set = &s->sets[set_idx];
  638. props = &agc->props[set_idx];
  639. if (reg_idx_command)
  640. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  641. PRIx64 "index mode wrong command 0x%x\n",
  642. __func__, offset, data, reg_idx_command);
  643. switch (reg_idx_type) {
  644. case gpio_reg_idx_data:
  645. reg_value = set->data_read;
  646. reg_value = deposit32(reg_value, pin_idx, 1,
  647. FIELD_EX32(data, GPIO_INDEX_REG, DATA_VALUE));
  648. reg_value &= props->output;
  649. reg_value = update_value_control_source(set, set->data_value,
  650. reg_value);
  651. set->data_read = reg_value;
  652. aspeed_gpio_update(s, set, reg_value, set->direction);
  653. return;
  654. case gpio_reg_idx_direction:
  655. reg_value = set->direction;
  656. reg_value = deposit32(reg_value, pin_idx, 1,
  657. FIELD_EX32(data, GPIO_INDEX_REG, DIRECTION));
  658. /*
  659. * where data is the value attempted to be written to the pin:
  660. * pin type | input mask | output mask | expected value
  661. * ------------------------------------------------------------
  662. * bidirectional | 1 | 1 | data
  663. * input only | 1 | 0 | 0
  664. * output only | 0 | 1 | 1
  665. * no pin | 0 | 0 | 0
  666. *
  667. * which is captured by:
  668. * data = ( data | ~input) & output;
  669. */
  670. reg_value = (reg_value | ~props->input) & props->output;
  671. set->direction = update_value_control_source(set, set->direction,
  672. reg_value);
  673. break;
  674. case gpio_reg_idx_interrupt:
  675. reg_value = set->int_enable;
  676. reg_value = deposit32(reg_value, pin_idx, 1,
  677. FIELD_EX32(data, GPIO_INDEX_REG, INT_ENABLE));
  678. set->int_enable = update_value_control_source(set, set->int_enable,
  679. reg_value);
  680. reg_value = set->int_sens_0;
  681. reg_value = deposit32(reg_value, pin_idx, 1,
  682. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_0));
  683. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  684. reg_value);
  685. reg_value = set->int_sens_1;
  686. reg_value = deposit32(reg_value, pin_idx, 1,
  687. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_1));
  688. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  689. reg_value);
  690. reg_value = set->int_sens_2;
  691. reg_value = deposit32(reg_value, pin_idx, 1,
  692. FIELD_EX32(data, GPIO_INDEX_REG, INT_SENS_2));
  693. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  694. reg_value);
  695. /* interrupt status */
  696. if (FIELD_EX32(data, GPIO_INDEX_REG, INT_STATUS)) {
  697. /* pending is either 1 or 0 for a 1-bit field */
  698. pending = extract32(set->int_status, pin_idx, 1);
  699. assert(s->pending >= pending);
  700. /* No change to s->pending if pending is 0 */
  701. s->pending -= pending;
  702. /*
  703. * The write acknowledged the interrupt regardless of whether it
  704. * was pending or not. The post-condition is that it mustn't be
  705. * pending. Unconditionally clear the status bit.
  706. */
  707. set->int_status = deposit32(set->int_status, pin_idx, 1, 0);
  708. }
  709. break;
  710. case gpio_reg_idx_debounce:
  711. reg_value = set->debounce_1;
  712. reg_value = deposit32(reg_value, pin_idx, 1,
  713. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_1));
  714. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  715. reg_value);
  716. reg_value = set->debounce_2;
  717. reg_value = deposit32(reg_value, pin_idx, 1,
  718. FIELD_EX32(data, GPIO_INDEX_REG, DEBOUNCE_2));
  719. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  720. reg_value);
  721. return;
  722. case gpio_reg_idx_tolerance:
  723. reg_value = set->reset_tol;
  724. reg_value = deposit32(reg_value, pin_idx, 1,
  725. FIELD_EX32(data, GPIO_INDEX_REG, RESET_TOLERANT));
  726. set->reset_tol = update_value_control_source(set, set->reset_tol,
  727. reg_value);
  728. return;
  729. case gpio_reg_idx_cmd_src:
  730. reg_value = set->cmd_source_0;
  731. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  732. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_0));
  733. set->cmd_source_0 = reg_value & ASPEED_CMD_SRC_MASK;
  734. reg_value = set->cmd_source_1;
  735. reg_value = deposit32(reg_value, GPIOS_PER_GROUP * group_idx, 1,
  736. FIELD_EX32(data, GPIO_INDEX_REG, COMMAND_SRC_1));
  737. set->cmd_source_1 = reg_value & ASPEED_CMD_SRC_MASK;
  738. return;
  739. case gpio_reg_idx_input_mask:
  740. reg_value = set->input_mask;
  741. reg_value = deposit32(reg_value, pin_idx, 1,
  742. FIELD_EX32(data, GPIO_INDEX_REG, INPUT_MASK));
  743. /*
  744. * feeds into interrupt generation
  745. * 0: read from data value reg will be updated
  746. * 1: read from data value reg will not be updated
  747. */
  748. set->input_mask = reg_value & props->input;
  749. break;
  750. default:
  751. qemu_log_mask(LOG_GUEST_ERROR, "%s: offset 0x%" PRIx64 "data 0x%"
  752. PRIx64 "index mode wrong type 0x%x\n",
  753. __func__, offset, data, reg_idx_type);
  754. return;
  755. }
  756. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  757. return;
  758. }
  759. static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,
  760. uint32_t size)
  761. {
  762. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  763. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  764. const GPIOSetProperties *props;
  765. uint64_t idx = -1;
  766. const AspeedGPIOReg *reg;
  767. GPIOSets *set;
  768. uint32_t cleared;
  769. trace_aspeed_gpio_write(offset, data);
  770. idx = offset >> 2;
  771. /* check gpio index mode */
  772. if (idx == R_GPIO_INDEX_REG) {
  773. aspeed_gpio_write_index_mode(opaque, offset, data, size);
  774. return;
  775. }
  776. if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) {
  777. idx -= GPIO_DEBOUNCE_TIME_1;
  778. s->debounce_regs[idx] = (uint32_t) data;
  779. return;
  780. }
  781. if (idx >= agc->reg_table_count) {
  782. qemu_log_mask(LOG_GUEST_ERROR, "%s: idx 0x%" PRIx64 " out of bounds\n",
  783. __func__, idx);
  784. return;
  785. }
  786. reg = &agc->reg_table[idx];
  787. if (reg->set_idx >= agc->nr_gpio_sets) {
  788. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  789. PRIx64"\n", __func__, offset);
  790. return;
  791. }
  792. set = &s->sets[reg->set_idx];
  793. props = &agc->props[reg->set_idx];
  794. switch (reg->type) {
  795. case gpio_reg_data_value:
  796. data &= props->output;
  797. data = update_value_control_source(set, set->data_value, data);
  798. set->data_read = data;
  799. aspeed_gpio_update(s, set, data, set->direction);
  800. return;
  801. case gpio_reg_direction:
  802. /*
  803. * where data is the value attempted to be written to the pin:
  804. * pin type | input mask | output mask | expected value
  805. * ------------------------------------------------------------
  806. * bidirectional | 1 | 1 | data
  807. * input only | 1 | 0 | 0
  808. * output only | 0 | 1 | 1
  809. * no pin | 0 | 0 | 0
  810. *
  811. * which is captured by:
  812. * data = ( data | ~input) & output;
  813. */
  814. data = (data | ~props->input) & props->output;
  815. set->direction = update_value_control_source(set, set->direction, data);
  816. break;
  817. case gpio_reg_int_enable:
  818. set->int_enable = update_value_control_source(set, set->int_enable,
  819. data);
  820. break;
  821. case gpio_reg_int_sens_0:
  822. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  823. data);
  824. break;
  825. case gpio_reg_int_sens_1:
  826. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  827. data);
  828. break;
  829. case gpio_reg_int_sens_2:
  830. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  831. data);
  832. break;
  833. case gpio_reg_int_status:
  834. cleared = ctpop32(data & set->int_status);
  835. if (s->pending && cleared) {
  836. assert(s->pending >= cleared);
  837. s->pending -= cleared;
  838. }
  839. set->int_status &= ~data;
  840. break;
  841. case gpio_reg_reset_tolerant:
  842. set->reset_tol = update_value_control_source(set, set->reset_tol,
  843. data);
  844. return;
  845. case gpio_reg_debounce_1:
  846. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  847. data);
  848. return;
  849. case gpio_reg_debounce_2:
  850. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  851. data);
  852. return;
  853. case gpio_reg_cmd_source_0:
  854. set->cmd_source_0 = data & ASPEED_CMD_SRC_MASK;
  855. return;
  856. case gpio_reg_cmd_source_1:
  857. set->cmd_source_1 = data & ASPEED_CMD_SRC_MASK;
  858. return;
  859. case gpio_reg_data_read:
  860. /* Read only register */
  861. return;
  862. case gpio_reg_input_mask:
  863. /*
  864. * feeds into interrupt generation
  865. * 0: read from data value reg will be updated
  866. * 1: read from data value reg will not be updated
  867. */
  868. set->input_mask = data & props->input;
  869. break;
  870. default:
  871. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  872. PRIx64"\n", __func__, offset);
  873. return;
  874. }
  875. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  876. return;
  877. }
  878. static int get_set_idx(AspeedGPIOState *s, const char *group, int *group_idx)
  879. {
  880. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  881. int set_idx, g_idx;
  882. for (set_idx = 0; set_idx < agc->nr_gpio_sets; set_idx++) {
  883. const GPIOSetProperties *set_props = &agc->props[set_idx];
  884. for (g_idx = 0; g_idx < ASPEED_GROUPS_PER_SET; g_idx++) {
  885. if (!strncmp(group, set_props->group_label[g_idx], strlen(group))) {
  886. *group_idx = g_idx;
  887. return set_idx;
  888. }
  889. }
  890. }
  891. return -1;
  892. }
  893. static void aspeed_gpio_get_pin(Object *obj, Visitor *v, const char *name,
  894. void *opaque, Error **errp)
  895. {
  896. int pin = 0xfff;
  897. bool level = true;
  898. char group[4];
  899. AspeedGPIOState *s = ASPEED_GPIO(obj);
  900. int set_idx, group_idx = 0;
  901. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  902. /* 1.8V gpio */
  903. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  904. error_setg(errp, "%s: error reading %s", __func__, name);
  905. return;
  906. }
  907. }
  908. set_idx = get_set_idx(s, group, &group_idx);
  909. if (set_idx == -1) {
  910. error_setg(errp, "%s: invalid group %s", __func__, group);
  911. return;
  912. }
  913. pin = pin + group_idx * GPIOS_PER_GROUP;
  914. level = aspeed_gpio_get_pin_level(s, set_idx, pin);
  915. visit_type_bool(v, name, &level, errp);
  916. }
  917. static void aspeed_gpio_set_pin(Object *obj, Visitor *v, const char *name,
  918. void *opaque, Error **errp)
  919. {
  920. bool level;
  921. int pin = 0xfff;
  922. char group[4];
  923. AspeedGPIOState *s = ASPEED_GPIO(obj);
  924. int set_idx, group_idx = 0;
  925. if (!visit_type_bool(v, name, &level, errp)) {
  926. return;
  927. }
  928. if (sscanf(name, "gpio%2[A-Z]%1d", group, &pin) != 2) {
  929. /* 1.8V gpio */
  930. if (sscanf(name, "gpio%3[18A-E]%1d", group, &pin) != 2) {
  931. error_setg(errp, "%s: error reading %s", __func__, name);
  932. return;
  933. }
  934. }
  935. set_idx = get_set_idx(s, group, &group_idx);
  936. if (set_idx == -1) {
  937. error_setg(errp, "%s: invalid group %s", __func__, group);
  938. return;
  939. }
  940. pin = pin + group_idx * GPIOS_PER_GROUP;
  941. aspeed_gpio_set_pin_level(s, set_idx, pin, level);
  942. }
  943. static uint64_t aspeed_gpio_2700_read_control_reg(AspeedGPIOState *s,
  944. uint32_t pin)
  945. {
  946. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  947. GPIOSets *set;
  948. uint64_t value = 0;
  949. uint32_t set_idx;
  950. uint32_t pin_idx;
  951. set_idx = pin / ASPEED_GPIOS_PER_SET;
  952. pin_idx = pin % ASPEED_GPIOS_PER_SET;
  953. if (set_idx >= agc->nr_gpio_sets) {
  954. qemu_log_mask(LOG_GUEST_ERROR, "%s: set index: %d, out of bounds\n",
  955. __func__, set_idx);
  956. return 0;
  957. }
  958. set = &s->sets[set_idx];
  959. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_OUT_DATA,
  960. extract32(set->data_read, pin_idx, 1));
  961. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_DIRECTION,
  962. extract32(set->direction, pin_idx, 1));
  963. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INT_ENABLE,
  964. extract32(set->int_enable, pin_idx, 1));
  965. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INT_SENS_0,
  966. extract32(set->int_sens_0, pin_idx, 1));
  967. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INT_SENS_1,
  968. extract32(set->int_sens_1, pin_idx, 1));
  969. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INT_SENS_2,
  970. extract32(set->int_sens_2, pin_idx, 1));
  971. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_RESET_TOLERANCE,
  972. extract32(set->reset_tol, pin_idx, 1));
  973. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_DEBOUNCE_1,
  974. extract32(set->debounce_1, pin_idx, 1));
  975. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_DEBOUNCE_2,
  976. extract32(set->debounce_2, pin_idx, 1));
  977. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INPUT_MASK,
  978. extract32(set->input_mask, pin_idx, 1));
  979. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_INT_STATUS,
  980. extract32(set->int_status, pin_idx, 1));
  981. value = SHARED_FIELD_DP32(value, GPIO_CONTROL_IN_DATA,
  982. extract32(set->data_value, pin_idx, 1));
  983. return value;
  984. }
  985. static void aspeed_gpio_2700_write_control_reg(AspeedGPIOState *s,
  986. uint32_t pin, uint64_t data)
  987. {
  988. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  989. const GPIOSetProperties *props;
  990. GPIOSets *set;
  991. uint32_t set_idx;
  992. uint32_t pin_idx;
  993. uint32_t group_value = 0;
  994. uint32_t pending = 0;
  995. set_idx = pin / ASPEED_GPIOS_PER_SET;
  996. pin_idx = pin % ASPEED_GPIOS_PER_SET;
  997. if (set_idx >= agc->nr_gpio_sets) {
  998. qemu_log_mask(LOG_GUEST_ERROR, "%s: set index: %d, out of bounds\n",
  999. __func__, set_idx);
  1000. return;
  1001. }
  1002. set = &s->sets[set_idx];
  1003. props = &agc->props[set_idx];
  1004. /* direction */
  1005. group_value = set->direction;
  1006. group_value = deposit32(group_value, pin_idx, 1,
  1007. SHARED_FIELD_EX32(data, GPIO_CONTROL_DIRECTION));
  1008. /*
  1009. * where data is the value attempted to be written to the pin:
  1010. * pin type | input mask | output mask | expected value
  1011. * ------------------------------------------------------------
  1012. * bidirectional | 1 | 1 | data
  1013. * input only | 1 | 0 | 0
  1014. * output only | 0 | 1 | 1
  1015. * no pin | 0 | 0 | 0
  1016. *
  1017. * which is captured by:
  1018. * data = ( data | ~input) & output;
  1019. */
  1020. group_value = (group_value | ~props->input) & props->output;
  1021. set->direction = update_value_control_source(set, set->direction,
  1022. group_value);
  1023. /* out data */
  1024. group_value = set->data_read;
  1025. group_value = deposit32(group_value, pin_idx, 1,
  1026. SHARED_FIELD_EX32(data, GPIO_CONTROL_OUT_DATA));
  1027. group_value &= props->output;
  1028. group_value = update_value_control_source(set, set->data_read,
  1029. group_value);
  1030. set->data_read = group_value;
  1031. /* interrupt enable */
  1032. group_value = set->int_enable;
  1033. group_value = deposit32(group_value, pin_idx, 1,
  1034. SHARED_FIELD_EX32(data, GPIO_CONTROL_INT_ENABLE));
  1035. set->int_enable = update_value_control_source(set, set->int_enable,
  1036. group_value);
  1037. /* interrupt sensitivity type 0 */
  1038. group_value = set->int_sens_0;
  1039. group_value = deposit32(group_value, pin_idx, 1,
  1040. SHARED_FIELD_EX32(data, GPIO_CONTROL_INT_SENS_0));
  1041. set->int_sens_0 = update_value_control_source(set, set->int_sens_0,
  1042. group_value);
  1043. /* interrupt sensitivity type 1 */
  1044. group_value = set->int_sens_1;
  1045. group_value = deposit32(group_value, pin_idx, 1,
  1046. SHARED_FIELD_EX32(data, GPIO_CONTROL_INT_SENS_1));
  1047. set->int_sens_1 = update_value_control_source(set, set->int_sens_1,
  1048. group_value);
  1049. /* interrupt sensitivity type 2 */
  1050. group_value = set->int_sens_2;
  1051. group_value = deposit32(group_value, pin_idx, 1,
  1052. SHARED_FIELD_EX32(data, GPIO_CONTROL_INT_SENS_2));
  1053. set->int_sens_2 = update_value_control_source(set, set->int_sens_2,
  1054. group_value);
  1055. /* reset tolerance enable */
  1056. group_value = set->reset_tol;
  1057. group_value = deposit32(group_value, pin_idx, 1,
  1058. SHARED_FIELD_EX32(data, GPIO_CONTROL_RESET_TOLERANCE));
  1059. set->reset_tol = update_value_control_source(set, set->reset_tol,
  1060. group_value);
  1061. /* debounce 1 */
  1062. group_value = set->debounce_1;
  1063. group_value = deposit32(group_value, pin_idx, 1,
  1064. SHARED_FIELD_EX32(data, GPIO_CONTROL_DEBOUNCE_1));
  1065. set->debounce_1 = update_value_control_source(set, set->debounce_1,
  1066. group_value);
  1067. /* debounce 2 */
  1068. group_value = set->debounce_2;
  1069. group_value = deposit32(group_value, pin_idx, 1,
  1070. SHARED_FIELD_EX32(data, GPIO_CONTROL_DEBOUNCE_2));
  1071. set->debounce_2 = update_value_control_source(set, set->debounce_2,
  1072. group_value);
  1073. /* input mask */
  1074. group_value = set->input_mask;
  1075. group_value = deposit32(group_value, pin_idx, 1,
  1076. SHARED_FIELD_EX32(data, GPIO_CONTROL_INPUT_MASK));
  1077. /*
  1078. * feeds into interrupt generation
  1079. * 0: read from data value reg will be updated
  1080. * 1: read from data value reg will not be updated
  1081. */
  1082. set->input_mask = group_value & props->input;
  1083. /* blink counter 1 */
  1084. /* blink counter 2 */
  1085. /* unimplement */
  1086. /* interrupt status */
  1087. if (SHARED_FIELD_EX32(data, GPIO_CONTROL_INT_STATUS)) {
  1088. /* pending is either 1 or 0 for a 1-bit field */
  1089. pending = extract32(set->int_status, pin_idx, 1);
  1090. assert(s->pending >= pending);
  1091. /* No change to s->pending if pending is 0 */
  1092. s->pending -= pending;
  1093. /*
  1094. * The write acknowledged the interrupt regardless of whether it
  1095. * was pending or not. The post-condition is that it mustn't be
  1096. * pending. Unconditionally clear the status bit.
  1097. */
  1098. set->int_status = deposit32(set->int_status, pin_idx, 1, 0);
  1099. }
  1100. aspeed_gpio_update(s, set, set->data_value, UINT32_MAX);
  1101. return;
  1102. }
  1103. static uint64_t aspeed_gpio_2700_read(void *opaque, hwaddr offset,
  1104. uint32_t size)
  1105. {
  1106. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  1107. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  1108. GPIOSets *set;
  1109. uint64_t value;
  1110. uint64_t reg;
  1111. uint32_t pin;
  1112. uint32_t idx;
  1113. reg = offset >> 2;
  1114. if (reg >= agc->reg_table_count) {
  1115. qemu_log_mask(LOG_GUEST_ERROR,
  1116. "%s: offset 0x%" PRIx64 " out of bounds\n",
  1117. __func__, offset);
  1118. return 0;
  1119. }
  1120. switch (reg) {
  1121. case R_GPIO_2700_DEBOUNCE_TIME_1 ... R_GPIO_2700_DEBOUNCE_TIME_3:
  1122. idx = reg - R_GPIO_2700_DEBOUNCE_TIME_1;
  1123. if (idx >= ASPEED_GPIO_NR_DEBOUNCE_REGS) {
  1124. qemu_log_mask(LOG_GUEST_ERROR,
  1125. "%s: debounce index: %d, out of bounds\n",
  1126. __func__, idx);
  1127. return 0;
  1128. }
  1129. value = (uint64_t) s->debounce_regs[idx];
  1130. break;
  1131. case R_GPIO_2700_INT_STATUS_1 ... R_GPIO_2700_INT_STATUS_7:
  1132. idx = reg - R_GPIO_2700_INT_STATUS_1;
  1133. if (idx >= agc->nr_gpio_sets) {
  1134. qemu_log_mask(LOG_GUEST_ERROR,
  1135. "%s: interrupt status index: %d, out of bounds\n",
  1136. __func__, idx);
  1137. return 0;
  1138. }
  1139. set = &s->sets[idx];
  1140. value = (uint64_t) set->int_status;
  1141. break;
  1142. case R_GPIO_A0_CONTROL ... R_GPIO_AA7_CONTROL:
  1143. pin = reg - R_GPIO_A0_CONTROL;
  1144. if (pin >= agc->nr_gpio_pins) {
  1145. qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid pin number: %d\n",
  1146. __func__, pin);
  1147. return 0;
  1148. }
  1149. value = aspeed_gpio_2700_read_control_reg(s, pin);
  1150. break;
  1151. default:
  1152. qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
  1153. PRIx64"\n", __func__, offset);
  1154. return 0;
  1155. }
  1156. trace_aspeed_gpio_read(offset, value);
  1157. return value;
  1158. }
  1159. static void aspeed_gpio_2700_write(void *opaque, hwaddr offset,
  1160. uint64_t data, uint32_t size)
  1161. {
  1162. AspeedGPIOState *s = ASPEED_GPIO(opaque);
  1163. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  1164. uint64_t reg;
  1165. uint32_t pin;
  1166. uint32_t idx;
  1167. trace_aspeed_gpio_write(offset, data);
  1168. reg = offset >> 2;
  1169. if (reg >= agc->reg_table_count) {
  1170. qemu_log_mask(LOG_GUEST_ERROR,
  1171. "%s: offset 0x%" PRIx64 " out of bounds\n",
  1172. __func__, offset);
  1173. return;
  1174. }
  1175. switch (reg) {
  1176. case R_GPIO_2700_DEBOUNCE_TIME_1 ... R_GPIO_2700_DEBOUNCE_TIME_3:
  1177. idx = reg - R_GPIO_2700_DEBOUNCE_TIME_1;
  1178. if (idx >= ASPEED_GPIO_NR_DEBOUNCE_REGS) {
  1179. qemu_log_mask(LOG_GUEST_ERROR,
  1180. "%s: debounce index: %d out of bounds\n",
  1181. __func__, idx);
  1182. return;
  1183. }
  1184. s->debounce_regs[idx] = (uint32_t) data;
  1185. break;
  1186. case R_GPIO_A0_CONTROL ... R_GPIO_AA7_CONTROL:
  1187. pin = reg - R_GPIO_A0_CONTROL;
  1188. if (pin >= agc->nr_gpio_pins) {
  1189. qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid pin number: %d\n",
  1190. __func__, pin);
  1191. return;
  1192. }
  1193. if (SHARED_FIELD_EX32(data, GPIO_CONTROL_RESERVED)) {
  1194. qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid reserved data: 0x%"
  1195. PRIx64"\n", __func__, data);
  1196. return;
  1197. }
  1198. aspeed_gpio_2700_write_control_reg(s, pin, data);
  1199. break;
  1200. default:
  1201. qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
  1202. PRIx64"\n", __func__, offset);
  1203. break;
  1204. }
  1205. return;
  1206. }
  1207. /* Setup functions */
  1208. static const GPIOSetProperties ast2400_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1209. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  1210. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  1211. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  1212. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  1213. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  1214. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  1215. [6] = {0x0000000f, 0x0fffff0f, {"Y", "Z", "AA", "AB"} },
  1216. };
  1217. static const GPIOSetProperties ast2500_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1218. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  1219. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  1220. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  1221. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  1222. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  1223. [5] = {0xffffffff, 0x0000ffff, {"U", "V", "W", "X"} },
  1224. [6] = {0x0fffffff, 0x0fffffff, {"Y", "Z", "AA", "AB"} },
  1225. [7] = {0x000000ff, 0x000000ff, {"AC"} },
  1226. };
  1227. static GPIOSetProperties ast2600_3_3v_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1228. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  1229. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  1230. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  1231. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  1232. [4] = {0xffffffff, 0x00ffffff, {"Q", "R", "S", "T"} },
  1233. [5] = {0xffffffff, 0xffffff00, {"U", "V", "W", "X"} },
  1234. [6] = {0x0000ffff, 0x0000ffff, {"Y", "Z"} },
  1235. };
  1236. static GPIOSetProperties ast2600_1_8v_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1237. [0] = {0xffffffff, 0xffffffff, {"18A", "18B", "18C", "18D"} },
  1238. [1] = {0x0000000f, 0x0000000f, {"18E"} },
  1239. };
  1240. static GPIOSetProperties ast1030_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1241. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  1242. [1] = {0xffffffff, 0xffffffff, {"E", "F", "G", "H"} },
  1243. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  1244. [3] = {0xffffff3f, 0xffffff3f, {"M", "N", "O", "P"} },
  1245. [4] = {0xff060c1f, 0x00060c1f, {"Q", "R", "S", "T"} },
  1246. [5] = {0x000000ff, 0x00000000, {"U"} },
  1247. };
  1248. static GPIOSetProperties ast2700_set_props[ASPEED_GPIO_MAX_NR_SETS] = {
  1249. [0] = {0xffffffff, 0xffffffff, {"A", "B", "C", "D"} },
  1250. [1] = {0x0fffffff, 0x0fffffff, {"E", "F", "G", "H"} },
  1251. [2] = {0xffffffff, 0xffffffff, {"I", "J", "K", "L"} },
  1252. [3] = {0xffffffff, 0xffffffff, {"M", "N", "O", "P"} },
  1253. [4] = {0xffffffff, 0xffffffff, {"Q", "R", "S", "T"} },
  1254. [5] = {0xffffffff, 0xffffffff, {"U", "V", "W", "X"} },
  1255. [6] = {0x00ffffff, 0x00ffffff, {"Y", "Z", "AA"} },
  1256. };
  1257. static const MemoryRegionOps aspeed_gpio_ops = {
  1258. .read = aspeed_gpio_read,
  1259. .write = aspeed_gpio_write,
  1260. .endianness = DEVICE_LITTLE_ENDIAN,
  1261. .valid.min_access_size = 4,
  1262. .valid.max_access_size = 4,
  1263. };
  1264. static const MemoryRegionOps aspeed_gpio_2700_ops = {
  1265. .read = aspeed_gpio_2700_read,
  1266. .write = aspeed_gpio_2700_write,
  1267. .endianness = DEVICE_LITTLE_ENDIAN,
  1268. .valid.min_access_size = 4,
  1269. .valid.max_access_size = 4,
  1270. };
  1271. static void aspeed_gpio_reset(DeviceState *dev)
  1272. {
  1273. AspeedGPIOState *s = ASPEED_GPIO(dev);
  1274. /* TODO: respect the reset tolerance registers */
  1275. memset(s->sets, 0, sizeof(s->sets));
  1276. }
  1277. static void aspeed_gpio_realize(DeviceState *dev, Error **errp)
  1278. {
  1279. AspeedGPIOState *s = ASPEED_GPIO(dev);
  1280. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1281. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  1282. /* Interrupt parent line */
  1283. sysbus_init_irq(sbd, &s->irq);
  1284. /* Individual GPIOs */
  1285. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  1286. const GPIOSetProperties *props = &agc->props[i];
  1287. uint32_t skip = ~(props->input | props->output);
  1288. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  1289. if (skip >> j & 1) {
  1290. continue;
  1291. }
  1292. sysbus_init_irq(sbd, &s->gpios[i][j]);
  1293. }
  1294. }
  1295. memory_region_init_io(&s->iomem, OBJECT(s), agc->reg_ops, s,
  1296. TYPE_ASPEED_GPIO, agc->mem_size);
  1297. sysbus_init_mmio(sbd, &s->iomem);
  1298. }
  1299. static void aspeed_gpio_init(Object *obj)
  1300. {
  1301. AspeedGPIOState *s = ASPEED_GPIO(obj);
  1302. AspeedGPIOClass *agc = ASPEED_GPIO_GET_CLASS(s);
  1303. for (int i = 0; i < ASPEED_GPIO_MAX_NR_SETS; i++) {
  1304. const GPIOSetProperties *props = &agc->props[i];
  1305. uint32_t skip = ~(props->input | props->output);
  1306. for (int j = 0; j < ASPEED_GPIOS_PER_SET; j++) {
  1307. if (skip >> j & 1) {
  1308. continue;
  1309. }
  1310. int group_idx = j / GPIOS_PER_GROUP;
  1311. int pin_idx = j % GPIOS_PER_GROUP;
  1312. const char *group = &props->group_label[group_idx][0];
  1313. char *name = g_strdup_printf("gpio%s%d", group, pin_idx);
  1314. object_property_add(obj, name, "bool", aspeed_gpio_get_pin,
  1315. aspeed_gpio_set_pin, NULL, NULL);
  1316. g_free(name);
  1317. }
  1318. }
  1319. }
  1320. static const VMStateDescription vmstate_gpio_regs = {
  1321. .name = TYPE_ASPEED_GPIO"/regs",
  1322. .version_id = 1,
  1323. .minimum_version_id = 1,
  1324. .fields = (const VMStateField[]) {
  1325. VMSTATE_UINT32(data_value, GPIOSets),
  1326. VMSTATE_UINT32(data_read, GPIOSets),
  1327. VMSTATE_UINT32(direction, GPIOSets),
  1328. VMSTATE_UINT32(int_enable, GPIOSets),
  1329. VMSTATE_UINT32(int_sens_0, GPIOSets),
  1330. VMSTATE_UINT32(int_sens_1, GPIOSets),
  1331. VMSTATE_UINT32(int_sens_2, GPIOSets),
  1332. VMSTATE_UINT32(int_status, GPIOSets),
  1333. VMSTATE_UINT32(reset_tol, GPIOSets),
  1334. VMSTATE_UINT32(cmd_source_0, GPIOSets),
  1335. VMSTATE_UINT32(cmd_source_1, GPIOSets),
  1336. VMSTATE_UINT32(debounce_1, GPIOSets),
  1337. VMSTATE_UINT32(debounce_2, GPIOSets),
  1338. VMSTATE_UINT32(input_mask, GPIOSets),
  1339. VMSTATE_END_OF_LIST(),
  1340. }
  1341. };
  1342. static const VMStateDescription vmstate_aspeed_gpio = {
  1343. .name = TYPE_ASPEED_GPIO,
  1344. .version_id = 1,
  1345. .minimum_version_id = 1,
  1346. .fields = (const VMStateField[]) {
  1347. VMSTATE_STRUCT_ARRAY(sets, AspeedGPIOState, ASPEED_GPIO_MAX_NR_SETS,
  1348. 1, vmstate_gpio_regs, GPIOSets),
  1349. VMSTATE_UINT32_ARRAY(debounce_regs, AspeedGPIOState,
  1350. ASPEED_GPIO_NR_DEBOUNCE_REGS),
  1351. VMSTATE_END_OF_LIST(),
  1352. }
  1353. };
  1354. static void aspeed_gpio_class_init(ObjectClass *klass, void *data)
  1355. {
  1356. DeviceClass *dc = DEVICE_CLASS(klass);
  1357. dc->realize = aspeed_gpio_realize;
  1358. device_class_set_legacy_reset(dc, aspeed_gpio_reset);
  1359. dc->desc = "Aspeed GPIO Controller";
  1360. dc->vmsd = &vmstate_aspeed_gpio;
  1361. }
  1362. static void aspeed_gpio_ast2400_class_init(ObjectClass *klass, void *data)
  1363. {
  1364. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1365. agc->props = ast2400_set_props;
  1366. agc->nr_gpio_pins = 216;
  1367. agc->nr_gpio_sets = 7;
  1368. agc->reg_table = aspeed_3_3v_gpios;
  1369. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1370. agc->mem_size = 0x1000;
  1371. agc->reg_ops = &aspeed_gpio_ops;
  1372. }
  1373. static void aspeed_gpio_2500_class_init(ObjectClass *klass, void *data)
  1374. {
  1375. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1376. agc->props = ast2500_set_props;
  1377. agc->nr_gpio_pins = 228;
  1378. agc->nr_gpio_sets = 8;
  1379. agc->reg_table = aspeed_3_3v_gpios;
  1380. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1381. agc->mem_size = 0x1000;
  1382. agc->reg_ops = &aspeed_gpio_ops;
  1383. }
  1384. static void aspeed_gpio_ast2600_3_3v_class_init(ObjectClass *klass, void *data)
  1385. {
  1386. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1387. agc->props = ast2600_3_3v_set_props;
  1388. agc->nr_gpio_pins = 208;
  1389. agc->nr_gpio_sets = 7;
  1390. agc->reg_table = aspeed_3_3v_gpios;
  1391. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1392. agc->mem_size = 0x800;
  1393. agc->reg_ops = &aspeed_gpio_ops;
  1394. }
  1395. static void aspeed_gpio_ast2600_1_8v_class_init(ObjectClass *klass, void *data)
  1396. {
  1397. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1398. agc->props = ast2600_1_8v_set_props;
  1399. agc->nr_gpio_pins = 36;
  1400. agc->nr_gpio_sets = 2;
  1401. agc->reg_table = aspeed_1_8v_gpios;
  1402. agc->reg_table_count = GPIO_1_8V_REG_ARRAY_SIZE;
  1403. agc->mem_size = 0x800;
  1404. agc->reg_ops = &aspeed_gpio_ops;
  1405. }
  1406. static void aspeed_gpio_1030_class_init(ObjectClass *klass, void *data)
  1407. {
  1408. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1409. agc->props = ast1030_set_props;
  1410. agc->nr_gpio_pins = 151;
  1411. agc->nr_gpio_sets = 6;
  1412. agc->reg_table = aspeed_3_3v_gpios;
  1413. agc->reg_table_count = GPIO_3_3V_REG_ARRAY_SIZE;
  1414. agc->mem_size = 0x1000;
  1415. agc->reg_ops = &aspeed_gpio_ops;
  1416. }
  1417. static void aspeed_gpio_2700_class_init(ObjectClass *klass, void *data)
  1418. {
  1419. AspeedGPIOClass *agc = ASPEED_GPIO_CLASS(klass);
  1420. agc->props = ast2700_set_props;
  1421. agc->nr_gpio_pins = 216;
  1422. agc->nr_gpio_sets = 7;
  1423. agc->reg_table_count = GPIO_2700_REG_ARRAY_SIZE;
  1424. agc->mem_size = 0x1000;
  1425. agc->reg_ops = &aspeed_gpio_2700_ops;
  1426. }
  1427. static const TypeInfo aspeed_gpio_info = {
  1428. .name = TYPE_ASPEED_GPIO,
  1429. .parent = TYPE_SYS_BUS_DEVICE,
  1430. .instance_size = sizeof(AspeedGPIOState),
  1431. .class_size = sizeof(AspeedGPIOClass),
  1432. .class_init = aspeed_gpio_class_init,
  1433. .abstract = true,
  1434. };
  1435. static const TypeInfo aspeed_gpio_ast2400_info = {
  1436. .name = TYPE_ASPEED_GPIO "-ast2400",
  1437. .parent = TYPE_ASPEED_GPIO,
  1438. .class_init = aspeed_gpio_ast2400_class_init,
  1439. .instance_init = aspeed_gpio_init,
  1440. };
  1441. static const TypeInfo aspeed_gpio_ast2500_info = {
  1442. .name = TYPE_ASPEED_GPIO "-ast2500",
  1443. .parent = TYPE_ASPEED_GPIO,
  1444. .class_init = aspeed_gpio_2500_class_init,
  1445. .instance_init = aspeed_gpio_init,
  1446. };
  1447. static const TypeInfo aspeed_gpio_ast2600_3_3v_info = {
  1448. .name = TYPE_ASPEED_GPIO "-ast2600",
  1449. .parent = TYPE_ASPEED_GPIO,
  1450. .class_init = aspeed_gpio_ast2600_3_3v_class_init,
  1451. .instance_init = aspeed_gpio_init,
  1452. };
  1453. static const TypeInfo aspeed_gpio_ast2600_1_8v_info = {
  1454. .name = TYPE_ASPEED_GPIO "-ast2600-1_8v",
  1455. .parent = TYPE_ASPEED_GPIO,
  1456. .class_init = aspeed_gpio_ast2600_1_8v_class_init,
  1457. .instance_init = aspeed_gpio_init,
  1458. };
  1459. static const TypeInfo aspeed_gpio_ast1030_info = {
  1460. .name = TYPE_ASPEED_GPIO "-ast1030",
  1461. .parent = TYPE_ASPEED_GPIO,
  1462. .class_init = aspeed_gpio_1030_class_init,
  1463. .instance_init = aspeed_gpio_init,
  1464. };
  1465. static const TypeInfo aspeed_gpio_ast2700_info = {
  1466. .name = TYPE_ASPEED_GPIO "-ast2700",
  1467. .parent = TYPE_ASPEED_GPIO,
  1468. .class_init = aspeed_gpio_2700_class_init,
  1469. .instance_init = aspeed_gpio_init,
  1470. };
  1471. static void aspeed_gpio_register_types(void)
  1472. {
  1473. type_register_static(&aspeed_gpio_info);
  1474. type_register_static(&aspeed_gpio_ast2400_info);
  1475. type_register_static(&aspeed_gpio_ast2500_info);
  1476. type_register_static(&aspeed_gpio_ast2600_3_3v_info);
  1477. type_register_static(&aspeed_gpio_ast2600_1_8v_info);
  1478. type_register_static(&aspeed_gpio_ast1030_info);
  1479. type_register_static(&aspeed_gpio_ast2700_info);
  1480. }
  1481. type_init(aspeed_gpio_register_types);