stm32l4x5_gpio.c 15 KB

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