stm32l4x5_gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * STM32L4x5 GPIO (General Purpose Input/Ouput)
  3. *
  4. * Copyright (c) 2024 Arnaud Minier <arnaud.minier@telecom-paris.fr>
  5. * Copyright (c) 2024 Inès Varhol <ines.varhol@telecom-paris.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. /*
  13. * The reference used is the STMicroElectronics RM0351 Reference manual
  14. * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs.
  15. * https://www.st.com/en/microcontrollers-microprocessors/stm32l4x5/documentation.html
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu/log.h"
  19. #include "hw/gpio/stm32l4x5_gpio.h"
  20. #include "hw/irq.h"
  21. #include "hw/clock.h"
  22. #include "hw/qdev-clock.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qapi/visitor.h"
  25. #include "qapi/error.h"
  26. #include "migration/vmstate.h"
  27. #include "trace.h"
  28. #define GPIO_MODER 0x00
  29. #define GPIO_OTYPER 0x04
  30. #define GPIO_OSPEEDR 0x08
  31. #define GPIO_PUPDR 0x0C
  32. #define GPIO_IDR 0x10
  33. #define GPIO_ODR 0x14
  34. #define GPIO_BSRR 0x18
  35. #define GPIO_LCKR 0x1C
  36. #define GPIO_AFRL 0x20
  37. #define GPIO_AFRH 0x24
  38. #define GPIO_BRR 0x28
  39. #define GPIO_ASCR 0x2C
  40. /* 0b11111111_11111111_00000000_00000000 */
  41. #define RESERVED_BITS_MASK 0xFFFF0000
  42. static void update_gpio_idr(Stm32l4x5GpioState *s);
  43. static bool is_pull_up(Stm32l4x5GpioState *s, unsigned pin)
  44. {
  45. return extract32(s->pupdr, 2 * pin, 2) == 1;
  46. }
  47. static bool is_pull_down(Stm32l4x5GpioState *s, unsigned pin)
  48. {
  49. return extract32(s->pupdr, 2 * pin, 2) == 2;
  50. }
  51. static bool is_output(Stm32l4x5GpioState *s, unsigned pin)
  52. {
  53. return extract32(s->moder, 2 * pin, 2) == 1;
  54. }
  55. static bool is_open_drain(Stm32l4x5GpioState *s, unsigned pin)
  56. {
  57. return extract32(s->otyper, pin, 1) == 1;
  58. }
  59. static bool is_push_pull(Stm32l4x5GpioState *s, unsigned pin)
  60. {
  61. return extract32(s->otyper, pin, 1) == 0;
  62. }
  63. static void stm32l4x5_gpio_reset_hold(Object *obj, ResetType type)
  64. {
  65. Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj);
  66. s->moder = s->moder_reset;
  67. s->otyper = 0x00000000;
  68. s->ospeedr = s->ospeedr_reset;
  69. s->pupdr = s->pupdr_reset;
  70. s->idr = 0x00000000;
  71. s->odr = 0x00000000;
  72. s->lckr = 0x00000000;
  73. s->afrl = 0x00000000;
  74. s->afrh = 0x00000000;
  75. s->ascr = 0x00000000;
  76. s->disconnected_pins = 0xFFFF;
  77. s->pins_connected_high = 0x0000;
  78. update_gpio_idr(s);
  79. }
  80. static void stm32l4x5_gpio_set(void *opaque, int line, int level)
  81. {
  82. Stm32l4x5GpioState *s = opaque;
  83. /*
  84. * The pin isn't set if line is configured in output mode
  85. * except if level is 0 and the output is open-drain.
  86. * This way there will be no short-circuit prone situations.
  87. */
  88. if (is_output(s, line) && !(is_open_drain(s, line) && (level == 0))) {
  89. qemu_log_mask(LOG_GUEST_ERROR, "Line %d can't be driven externally\n",
  90. line);
  91. return;
  92. }
  93. s->disconnected_pins &= ~(1 << line);
  94. if (level) {
  95. s->pins_connected_high |= (1 << line);
  96. } else {
  97. s->pins_connected_high &= ~(1 << line);
  98. }
  99. trace_stm32l4x5_gpio_pins(s->name, s->disconnected_pins,
  100. s->pins_connected_high);
  101. update_gpio_idr(s);
  102. }
  103. static void update_gpio_idr(Stm32l4x5GpioState *s)
  104. {
  105. uint32_t new_idr_mask = 0;
  106. uint32_t new_idr = s->odr;
  107. uint32_t old_idr = s->idr;
  108. int new_pin_state, old_pin_state;
  109. for (int i = 0; i < GPIO_NUM_PINS; i++) {
  110. if (is_output(s, i)) {
  111. if (is_push_pull(s, i)) {
  112. new_idr_mask |= (1 << i);
  113. } else if (!(s->odr & (1 << i))) {
  114. /* open-drain ODR 0 */
  115. new_idr_mask |= (1 << i);
  116. /* open-drain ODR 1 */
  117. } else if (!(s->disconnected_pins & (1 << i)) &&
  118. !(s->pins_connected_high & (1 << i))) {
  119. /* open-drain ODR 1 with pin connected low */
  120. new_idr_mask |= (1 << i);
  121. new_idr &= ~(1 << i);
  122. /* open-drain ODR 1 with unactive pin */
  123. } else if (is_pull_up(s, i)) {
  124. new_idr_mask |= (1 << i);
  125. } else if (is_pull_down(s, i)) {
  126. new_idr_mask |= (1 << i);
  127. new_idr &= ~(1 << i);
  128. }
  129. /*
  130. * The only case left is for open-drain ODR 1
  131. * with unactive pin without pull-up or pull-down :
  132. * the value is floating.
  133. */
  134. /* input or analog mode with connected pin */
  135. } else if (!(s->disconnected_pins & (1 << i))) {
  136. if (s->pins_connected_high & (1 << i)) {
  137. /* pin high */
  138. new_idr_mask |= (1 << i);
  139. new_idr |= (1 << i);
  140. } else {
  141. /* pin low */
  142. new_idr_mask |= (1 << i);
  143. new_idr &= ~(1 << i);
  144. }
  145. /* input or analog mode with disconnected pin */
  146. } else {
  147. if (is_pull_up(s, i)) {
  148. /* pull-up */
  149. new_idr_mask |= (1 << i);
  150. new_idr |= (1 << i);
  151. } else if (is_pull_down(s, i)) {
  152. /* pull-down */
  153. new_idr_mask |= (1 << i);
  154. new_idr &= ~(1 << i);
  155. }
  156. /*
  157. * The only case left is for a disconnected pin
  158. * without pull-up or pull-down :
  159. * the value is floating.
  160. */
  161. }
  162. }
  163. s->idr = (old_idr & ~new_idr_mask) | (new_idr & new_idr_mask);
  164. trace_stm32l4x5_gpio_update_idr(s->name, old_idr, s->idr);
  165. for (int i = 0; i < GPIO_NUM_PINS; i++) {
  166. if (new_idr_mask & (1 << i)) {
  167. new_pin_state = (new_idr & (1 << i)) > 0;
  168. old_pin_state = (old_idr & (1 << i)) > 0;
  169. if (new_pin_state > old_pin_state) {
  170. qemu_irq_raise(s->pin[i]);
  171. } else if (new_pin_state < old_pin_state) {
  172. qemu_irq_lower(s->pin[i]);
  173. }
  174. }
  175. }
  176. }
  177. /*
  178. * Return mask of pins that are both configured in output
  179. * mode and externally driven (except pins in open-drain
  180. * mode externally set to 0).
  181. */
  182. static uint32_t get_gpio_pinmask_to_disconnect(Stm32l4x5GpioState *s)
  183. {
  184. uint32_t pins_to_disconnect = 0;
  185. for (int i = 0; i < GPIO_NUM_PINS; i++) {
  186. /* for each connected pin in output mode */
  187. if (!(s->disconnected_pins & (1 << i)) && is_output(s, i)) {
  188. /* if either push-pull or high level */
  189. if (is_push_pull(s, i) || s->pins_connected_high & (1 << i)) {
  190. pins_to_disconnect |= (1 << i);
  191. qemu_log_mask(LOG_GUEST_ERROR,
  192. "Line %d can't be driven externally\n",
  193. i);
  194. }
  195. }
  196. }
  197. return pins_to_disconnect;
  198. }
  199. /*
  200. * Set field `disconnected_pins` and call `update_gpio_idr()`
  201. */
  202. static void disconnect_gpio_pins(Stm32l4x5GpioState *s, uint16_t lines)
  203. {
  204. s->disconnected_pins |= lines;
  205. trace_stm32l4x5_gpio_pins(s->name, s->disconnected_pins,
  206. s->pins_connected_high);
  207. update_gpio_idr(s);
  208. }
  209. static void disconnected_pins_set(Object *obj, Visitor *v,
  210. const char *name, void *opaque, Error **errp)
  211. {
  212. Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj);
  213. uint16_t value;
  214. if (!visit_type_uint16(v, name, &value, errp)) {
  215. return;
  216. }
  217. disconnect_gpio_pins(s, value);
  218. }
  219. static void disconnected_pins_get(Object *obj, Visitor *v,
  220. const char *name, void *opaque, Error **errp)
  221. {
  222. visit_type_uint16(v, name, (uint16_t *)opaque, errp);
  223. }
  224. static void clock_freq_get(Object *obj, Visitor *v,
  225. const char *name, void *opaque, Error **errp)
  226. {
  227. Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj);
  228. uint32_t clock_freq_hz = clock_get_hz(s->clk);
  229. visit_type_uint32(v, name, &clock_freq_hz, errp);
  230. }
  231. static void stm32l4x5_gpio_write(void *opaque, hwaddr addr,
  232. uint64_t val64, unsigned int size)
  233. {
  234. Stm32l4x5GpioState *s = opaque;
  235. uint32_t value = val64;
  236. trace_stm32l4x5_gpio_write(s->name, addr, val64);
  237. switch (addr) {
  238. case GPIO_MODER:
  239. s->moder = value;
  240. disconnect_gpio_pins(s, get_gpio_pinmask_to_disconnect(s));
  241. qemu_log_mask(LOG_UNIMP,
  242. "%s: Analog and AF modes aren't supported\n\
  243. Analog and AF mode behave like input mode\n",
  244. __func__);
  245. return;
  246. case GPIO_OTYPER:
  247. s->otyper = value & ~RESERVED_BITS_MASK;
  248. disconnect_gpio_pins(s, get_gpio_pinmask_to_disconnect(s));
  249. return;
  250. case GPIO_OSPEEDR:
  251. qemu_log_mask(LOG_UNIMP,
  252. "%s: Changing I/O output speed isn't supported\n\
  253. I/O speed is already maximal\n",
  254. __func__);
  255. s->ospeedr = value;
  256. return;
  257. case GPIO_PUPDR:
  258. s->pupdr = value;
  259. update_gpio_idr(s);
  260. return;
  261. case GPIO_IDR:
  262. qemu_log_mask(LOG_UNIMP,
  263. "%s: GPIO->IDR is read-only\n",
  264. __func__);
  265. return;
  266. case GPIO_ODR:
  267. s->odr = value & ~RESERVED_BITS_MASK;
  268. update_gpio_idr(s);
  269. return;
  270. case GPIO_BSRR: {
  271. uint32_t bits_to_reset = (value & RESERVED_BITS_MASK) >> GPIO_NUM_PINS;
  272. uint32_t bits_to_set = value & ~RESERVED_BITS_MASK;
  273. /* If both BSx and BRx are set, BSx has priority.*/
  274. s->odr &= ~bits_to_reset;
  275. s->odr |= bits_to_set;
  276. update_gpio_idr(s);
  277. return;
  278. }
  279. case GPIO_LCKR:
  280. qemu_log_mask(LOG_UNIMP,
  281. "%s: Locking port bits configuration isn't supported\n",
  282. __func__);
  283. s->lckr = value & ~RESERVED_BITS_MASK;
  284. return;
  285. case GPIO_AFRL:
  286. qemu_log_mask(LOG_UNIMP,
  287. "%s: Alternate functions aren't supported\n",
  288. __func__);
  289. s->afrl = value;
  290. return;
  291. case GPIO_AFRH:
  292. qemu_log_mask(LOG_UNIMP,
  293. "%s: Alternate functions aren't supported\n",
  294. __func__);
  295. s->afrh = value;
  296. return;
  297. case GPIO_BRR: {
  298. uint32_t bits_to_reset = value & ~RESERVED_BITS_MASK;
  299. s->odr &= ~bits_to_reset;
  300. update_gpio_idr(s);
  301. return;
  302. }
  303. case GPIO_ASCR:
  304. qemu_log_mask(LOG_UNIMP,
  305. "%s: ADC function isn't supported\n",
  306. __func__);
  307. s->ascr = value & ~RESERVED_BITS_MASK;
  308. return;
  309. default:
  310. qemu_log_mask(LOG_GUEST_ERROR,
  311. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  312. }
  313. }
  314. static uint64_t stm32l4x5_gpio_read(void *opaque, hwaddr addr,
  315. unsigned int size)
  316. {
  317. Stm32l4x5GpioState *s = opaque;
  318. trace_stm32l4x5_gpio_read(s->name, addr);
  319. switch (addr) {
  320. case GPIO_MODER:
  321. return s->moder;
  322. case GPIO_OTYPER:
  323. return s->otyper;
  324. case GPIO_OSPEEDR:
  325. return s->ospeedr;
  326. case GPIO_PUPDR:
  327. return s->pupdr;
  328. case GPIO_IDR:
  329. return s->idr;
  330. case GPIO_ODR:
  331. return s->odr;
  332. case GPIO_BSRR:
  333. return 0;
  334. case GPIO_LCKR:
  335. return s->lckr;
  336. case GPIO_AFRL:
  337. return s->afrl;
  338. case GPIO_AFRH:
  339. return s->afrh;
  340. case GPIO_BRR:
  341. return 0;
  342. case GPIO_ASCR:
  343. return s->ascr;
  344. default:
  345. qemu_log_mask(LOG_GUEST_ERROR,
  346. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  347. return 0;
  348. }
  349. }
  350. static const MemoryRegionOps stm32l4x5_gpio_ops = {
  351. .read = stm32l4x5_gpio_read,
  352. .write = stm32l4x5_gpio_write,
  353. .endianness = DEVICE_NATIVE_ENDIAN,
  354. .impl = {
  355. .min_access_size = 4,
  356. .max_access_size = 4,
  357. .unaligned = false,
  358. },
  359. .valid = {
  360. .min_access_size = 4,
  361. .max_access_size = 4,
  362. .unaligned = false,
  363. },
  364. };
  365. static void stm32l4x5_gpio_init(Object *obj)
  366. {
  367. Stm32l4x5GpioState *s = STM32L4X5_GPIO(obj);
  368. memory_region_init_io(&s->mmio, obj, &stm32l4x5_gpio_ops, s,
  369. TYPE_STM32L4X5_GPIO, 0x400);
  370. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  371. qdev_init_gpio_out(DEVICE(obj), s->pin, GPIO_NUM_PINS);
  372. qdev_init_gpio_in(DEVICE(obj), stm32l4x5_gpio_set, GPIO_NUM_PINS);
  373. s->clk = qdev_init_clock_in(DEVICE(s), "clk", NULL, s, 0);
  374. object_property_add(obj, "disconnected-pins", "uint16",
  375. disconnected_pins_get, disconnected_pins_set,
  376. NULL, &s->disconnected_pins);
  377. object_property_add(obj, "clock-freq-hz", "uint32",
  378. clock_freq_get, NULL, NULL, NULL);
  379. }
  380. static void stm32l4x5_gpio_realize(DeviceState *dev, Error **errp)
  381. {
  382. Stm32l4x5GpioState *s = STM32L4X5_GPIO(dev);
  383. if (!clock_has_source(s->clk)) {
  384. error_setg(errp, "GPIO: clk input must be connected");
  385. return;
  386. }
  387. }
  388. static const VMStateDescription vmstate_stm32l4x5_gpio = {
  389. .name = TYPE_STM32L4X5_GPIO,
  390. .version_id = 2,
  391. .minimum_version_id = 2,
  392. .fields = (VMStateField[]){
  393. VMSTATE_UINT32(moder, Stm32l4x5GpioState),
  394. VMSTATE_UINT32(otyper, Stm32l4x5GpioState),
  395. VMSTATE_UINT32(ospeedr, Stm32l4x5GpioState),
  396. VMSTATE_UINT32(pupdr, Stm32l4x5GpioState),
  397. VMSTATE_UINT32(idr, Stm32l4x5GpioState),
  398. VMSTATE_UINT32(odr, Stm32l4x5GpioState),
  399. VMSTATE_UINT32(lckr, Stm32l4x5GpioState),
  400. VMSTATE_UINT32(afrl, Stm32l4x5GpioState),
  401. VMSTATE_UINT32(afrh, Stm32l4x5GpioState),
  402. VMSTATE_UINT32(ascr, Stm32l4x5GpioState),
  403. VMSTATE_UINT16(disconnected_pins, Stm32l4x5GpioState),
  404. VMSTATE_UINT16(pins_connected_high, Stm32l4x5GpioState),
  405. VMSTATE_CLOCK(clk, Stm32l4x5GpioState),
  406. VMSTATE_END_OF_LIST()
  407. }
  408. };
  409. static const Property stm32l4x5_gpio_properties[] = {
  410. DEFINE_PROP_STRING("name", Stm32l4x5GpioState, name),
  411. DEFINE_PROP_UINT32("mode-reset", Stm32l4x5GpioState, moder_reset, 0),
  412. DEFINE_PROP_UINT32("ospeed-reset", Stm32l4x5GpioState, ospeedr_reset, 0),
  413. DEFINE_PROP_UINT32("pupd-reset", Stm32l4x5GpioState, pupdr_reset, 0),
  414. };
  415. static void stm32l4x5_gpio_class_init(ObjectClass *klass, void *data)
  416. {
  417. DeviceClass *dc = DEVICE_CLASS(klass);
  418. ResettableClass *rc = RESETTABLE_CLASS(klass);
  419. device_class_set_props(dc, stm32l4x5_gpio_properties);
  420. dc->vmsd = &vmstate_stm32l4x5_gpio;
  421. dc->realize = stm32l4x5_gpio_realize;
  422. rc->phases.hold = stm32l4x5_gpio_reset_hold;
  423. }
  424. static const TypeInfo stm32l4x5_gpio_types[] = {
  425. {
  426. .name = TYPE_STM32L4X5_GPIO,
  427. .parent = TYPE_SYS_BUS_DEVICE,
  428. .instance_size = sizeof(Stm32l4x5GpioState),
  429. .instance_init = stm32l4x5_gpio_init,
  430. .class_init = stm32l4x5_gpio_class_init,
  431. },
  432. };
  433. DEFINE_TYPES(stm32l4x5_gpio_types)