rk3399_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <platform_def.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <drivers/gpio.h>
  12. #include <lib/mmio.h>
  13. #include <plat/common/platform.h>
  14. #include <plat_private.h>
  15. #include <soc.h>
  16. struct gpio_save {
  17. uint32_t swporta_dr;
  18. uint32_t swporta_ddr;
  19. uint32_t inten;
  20. uint32_t intmask;
  21. uint32_t inttype_level;
  22. uint32_t int_polarity;
  23. uint32_t debounce;
  24. uint32_t ls_sync;
  25. } store_gpio[3];
  26. static uint32_t store_grf_gpio[(GRF_GPIO2D_HE - GRF_GPIO2A_IOMUX) / 4 + 1];
  27. #define SWPORTA_DR 0x00
  28. #define SWPORTA_DDR 0x04
  29. #define INTEN 0x30
  30. #define INTMASK 0x34
  31. #define INTTYPE_LEVEL 0x38
  32. #define INT_POLARITY 0x3c
  33. #define DEBOUNCE 0x48
  34. #define LS_SYNC 0x60
  35. #define EXT_PORTA 0x50
  36. #define PMU_GPIO_PORT0 0
  37. #define PMU_GPIO_PORT1 1
  38. #define GPIO_PORT2 2
  39. #define GPIO_PORT3 3
  40. #define GPIO_PORT4 4
  41. #define PMU_GRF_GPIO0A_P 0x40
  42. #define GRF_GPIO2A_P 0xe040
  43. #define GPIO_P_MASK 0x03
  44. #define GET_GPIO_PORT(pin) (pin / 32)
  45. #define GET_GPIO_NUM(pin) (pin % 32)
  46. #define GET_GPIO_BANK(pin) ((pin % 32) / 8)
  47. #define GET_GPIO_ID(pin) ((pin % 32) % 8)
  48. enum {
  49. ENC_ZDZU,
  50. ENC_ZUDR,
  51. ENC_ZUDZ,
  52. NUM_ENC
  53. };
  54. static const struct port_info {
  55. uint32_t clkgate_reg;
  56. uint32_t pull_base;
  57. uint32_t port_base;
  58. /*
  59. * Selects the pull mode encoding per bank,
  60. * first index for pull_type_{hw2sw,sw2hw}
  61. */
  62. uint8_t pull_enc[4];
  63. uint8_t clkgate_bit;
  64. uint8_t max_bank;
  65. } port_info[] = {
  66. {
  67. .clkgate_reg = PMUCRU_BASE + CRU_PMU_CLKGATE_CON(1),
  68. .pull_base = PMUGRF_BASE + PMUGRF_GPIO0A_P,
  69. .port_base = GPIO0_BASE,
  70. .pull_enc = {ENC_ZDZU, ENC_ZDZU},
  71. .clkgate_bit = PCLK_GPIO0_GATE_SHIFT,
  72. .max_bank = 1,
  73. }, {
  74. .clkgate_reg = PMUCRU_BASE + CRU_PMU_CLKGATE_CON(1),
  75. .pull_base = PMUGRF_BASE + PMUGRF_GPIO1A_P,
  76. .port_base = GPIO1_BASE,
  77. .pull_enc = {ENC_ZUDR, ENC_ZUDR, ENC_ZUDR, ENC_ZUDR},
  78. .clkgate_bit = PCLK_GPIO1_GATE_SHIFT,
  79. .max_bank = 3,
  80. }, {
  81. .clkgate_reg = CRU_BASE + CRU_CLKGATE_CON(31),
  82. .pull_base = GRF_BASE + GRF_GPIO2A_P,
  83. .port_base = GPIO2_BASE,
  84. .pull_enc = {ENC_ZUDR, ENC_ZUDR, ENC_ZDZU, ENC_ZDZU},
  85. .clkgate_bit = PCLK_GPIO2_GATE_SHIFT,
  86. .max_bank = 3,
  87. }, {
  88. .clkgate_reg = CRU_BASE + CRU_CLKGATE_CON(31),
  89. .pull_base = GRF_BASE + GRF_GPIO3A_P,
  90. .port_base = GPIO3_BASE,
  91. .pull_enc = {ENC_ZUDR, ENC_ZUDR, ENC_ZUDR, ENC_ZUDR},
  92. .clkgate_bit = PCLK_GPIO3_GATE_SHIFT,
  93. .max_bank = 3,
  94. }, {
  95. .clkgate_reg = CRU_BASE + CRU_CLKGATE_CON(31),
  96. .pull_base = GRF_BASE + GRF_GPIO4A_P,
  97. .port_base = GPIO4_BASE,
  98. .pull_enc = {ENC_ZUDR, ENC_ZUDR, ENC_ZUDR, ENC_ZUDR},
  99. .clkgate_bit = PCLK_GPIO4_GATE_SHIFT,
  100. .max_bank = 3,
  101. }
  102. };
  103. /*
  104. * Mappings between TF-A constants and hardware encodings:
  105. * there are 3 different encoding schemes that may differ between
  106. * banks of the same port: the corresponding value of the pull_enc array
  107. * in port_info is used as the first index
  108. */
  109. static const uint8_t pull_type_hw2sw[NUM_ENC][4] = {
  110. [ENC_ZDZU] = {GPIO_PULL_NONE, GPIO_PULL_DOWN, GPIO_PULL_NONE, GPIO_PULL_UP},
  111. [ENC_ZUDR] = {GPIO_PULL_NONE, GPIO_PULL_UP, GPIO_PULL_DOWN, GPIO_PULL_REPEATER},
  112. [ENC_ZUDZ] = {GPIO_PULL_NONE, GPIO_PULL_UP, GPIO_PULL_DOWN, GPIO_PULL_NONE}
  113. };
  114. static const uint8_t pull_type_sw2hw[NUM_ENC][4] = {
  115. [ENC_ZDZU] = {
  116. [GPIO_PULL_NONE] = 0,
  117. [GPIO_PULL_DOWN] = 1,
  118. [GPIO_PULL_UP] = 3,
  119. [GPIO_PULL_REPEATER] = -1
  120. },
  121. [ENC_ZUDR] = {
  122. [GPIO_PULL_NONE] = 0,
  123. [GPIO_PULL_DOWN] = 2,
  124. [GPIO_PULL_UP] = 1,
  125. [GPIO_PULL_REPEATER] = 3
  126. },
  127. [ENC_ZUDZ] = {
  128. [GPIO_PULL_NONE] = 0,
  129. [GPIO_PULL_DOWN] = 2,
  130. [GPIO_PULL_UP] = 1,
  131. [GPIO_PULL_REPEATER] = -1
  132. }
  133. };
  134. /* Return old clock state, enables clock, in order to do GPIO access */
  135. static int gpio_get_clock(uint32_t gpio_number)
  136. {
  137. uint32_t port = GET_GPIO_PORT(gpio_number);
  138. assert(port < 5U);
  139. const struct port_info *info = &port_info[port];
  140. if ((mmio_read_32(info->clkgate_reg) & (1U << info->clkgate_bit)) == 0U) {
  141. return 0;
  142. }
  143. mmio_write_32(
  144. info->clkgate_reg,
  145. BITS_WITH_WMASK(0, 1, info->clkgate_bit)
  146. );
  147. return 1;
  148. }
  149. /* Restore old state of gpio clock, assuming it is running now */
  150. void gpio_put_clock(uint32_t gpio_number, uint32_t clock_state)
  151. {
  152. if (clock_state == 0) {
  153. return;
  154. }
  155. uint32_t port = GET_GPIO_PORT(gpio_number);
  156. const struct port_info *info = &port_info[port];
  157. mmio_write_32(info->clkgate_reg, BITS_WITH_WMASK(1, 1, info->clkgate_bit));
  158. }
  159. static int get_pull(int gpio)
  160. {
  161. uint32_t port = GET_GPIO_PORT(gpio);
  162. uint32_t bank = GET_GPIO_BANK(gpio);
  163. uint32_t id = GET_GPIO_ID(gpio);
  164. uint32_t val, clock_state;
  165. assert(port < 5U);
  166. const struct port_info *info = &port_info[port];
  167. assert(bank <= info->max_bank);
  168. clock_state = gpio_get_clock(gpio);
  169. val = (mmio_read_32(info->pull_base + 4 * bank) >> (id * 2)) & GPIO_P_MASK;
  170. gpio_put_clock(gpio, clock_state);
  171. return pull_type_hw2sw[info->pull_enc[bank]][val];
  172. }
  173. static void set_pull(int gpio, int pull)
  174. {
  175. uint32_t port = GET_GPIO_PORT(gpio);
  176. uint32_t bank = GET_GPIO_BANK(gpio);
  177. uint32_t id = GET_GPIO_ID(gpio);
  178. uint32_t clock_state;
  179. assert(port < 5U);
  180. const struct port_info *info = &port_info[port];
  181. assert(bank <= info->max_bank);
  182. uint8_t val = pull_type_sw2hw[info->pull_enc[bank]][pull];
  183. assert(val != (uint8_t)-1);
  184. clock_state = gpio_get_clock(gpio);
  185. mmio_write_32(
  186. info->pull_base + 4 * bank,
  187. BITS_WITH_WMASK(val, GPIO_P_MASK, id * 2)
  188. );
  189. gpio_put_clock(gpio, clock_state);
  190. }
  191. static void set_direction(int gpio, int direction)
  192. {
  193. uint32_t port = GET_GPIO_PORT(gpio);
  194. uint32_t num = GET_GPIO_NUM(gpio);
  195. uint32_t clock_state;
  196. assert((port < 5) && (num < 32));
  197. clock_state = gpio_get_clock(gpio);
  198. /*
  199. * in gpio.h
  200. * #define GPIO_DIR_OUT 0
  201. * #define GPIO_DIR_IN 1
  202. * but rk3399 gpio direction 1: output, 0: input
  203. * so need to revert direction value
  204. */
  205. mmio_setbits_32(
  206. port_info[port].port_base + SWPORTA_DDR,
  207. ((direction == 0) ? 1 : 0) << num
  208. );
  209. gpio_put_clock(gpio, clock_state);
  210. }
  211. static int get_direction(int gpio)
  212. {
  213. uint32_t port = GET_GPIO_PORT(gpio);
  214. uint32_t num = GET_GPIO_NUM(gpio);
  215. int direction, clock_state;
  216. assert((port < 5U) && (num < 32U));
  217. clock_state = gpio_get_clock(gpio);
  218. /*
  219. * in gpio.h
  220. * #define GPIO_DIR_OUT 0
  221. * #define GPIO_DIR_IN 1
  222. * but rk3399 gpio direction 1: output, 0: input
  223. * so need to revert direction value
  224. */
  225. direction = (((mmio_read_32(
  226. port_info[port].port_base + SWPORTA_DDR
  227. ) >> num) & 1U) == 0) ? 1 : 0;
  228. gpio_put_clock(gpio, clock_state);
  229. return direction;
  230. }
  231. static int get_value(int gpio)
  232. {
  233. uint32_t port = GET_GPIO_PORT(gpio);
  234. uint32_t num = GET_GPIO_NUM(gpio);
  235. int value, clock_state;
  236. assert((port < 5) && (num < 32));
  237. clock_state = gpio_get_clock(gpio);
  238. value = (mmio_read_32(port_info[port].port_base + EXT_PORTA) >> num) &
  239. 0x1U;
  240. gpio_put_clock(gpio, clock_state);
  241. return value;
  242. }
  243. static void set_value(int gpio, int value)
  244. {
  245. uint32_t port = GET_GPIO_PORT(gpio);
  246. uint32_t num = GET_GPIO_NUM(gpio);
  247. uint32_t clock_state;
  248. assert((port < 5U) && (num < 32U));
  249. clock_state = gpio_get_clock(gpio);
  250. mmio_clrsetbits_32(
  251. port_info[port].port_base + SWPORTA_DR,
  252. 1 << num,
  253. ((value == 0) ? 0 : 1) << num
  254. );
  255. gpio_put_clock(gpio, clock_state);
  256. }
  257. void plat_rockchip_save_gpio(void)
  258. {
  259. unsigned int i;
  260. uint32_t cru_gate_save;
  261. cru_gate_save = mmio_read_32(CRU_BASE + CRU_CLKGATE_CON(31));
  262. /*
  263. * when shutdown logic, we need to save gpio2 ~ gpio4 register,
  264. * we need to enable gpio2 ~ gpio4 clock here, since it may be gating,
  265. * and we do not care gpio0 and gpio1 clock gate, since we never
  266. * gating them
  267. */
  268. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(31),
  269. BITS_WITH_WMASK(0, 0x07, PCLK_GPIO2_GATE_SHIFT));
  270. /*
  271. * since gpio0, gpio1 are pmugpio, they will keep ther value
  272. * when shutdown logic power rail, so only need to save gpio2 ~ gpio4
  273. * register value
  274. */
  275. for (i = 2; i < 5; i++) {
  276. uint32_t base = port_info[i].port_base;
  277. store_gpio[i - 2] = (struct gpio_save) {
  278. .swporta_dr = mmio_read_32(base + SWPORTA_DR),
  279. .swporta_ddr = mmio_read_32(base + SWPORTA_DDR),
  280. .inten = mmio_read_32(base + INTEN),
  281. .intmask = mmio_read_32(base + INTMASK),
  282. .inttype_level = mmio_read_32(base + INTTYPE_LEVEL),
  283. .int_polarity = mmio_read_32(base + INT_POLARITY),
  284. .debounce = mmio_read_32(base + DEBOUNCE),
  285. .ls_sync = mmio_read_32(base + LS_SYNC),
  286. };
  287. }
  288. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(31),
  289. cru_gate_save | REG_SOC_WMSK);
  290. /*
  291. * gpio0, gpio1 in pmuiomux, they will keep ther value
  292. * when shutdown logic power rail, so only need to save gpio2 ~ gpio4
  293. * iomux register value
  294. */
  295. for (i = 0; i < ARRAY_SIZE(store_grf_gpio); i++)
  296. store_grf_gpio[i] =
  297. mmio_read_32(GRF_BASE + GRF_GPIO2A_IOMUX + i * 4);
  298. }
  299. void plat_rockchip_restore_gpio(void)
  300. {
  301. int i;
  302. uint32_t cru_gate_save;
  303. for (i = 0; i < ARRAY_SIZE(store_grf_gpio); i++)
  304. mmio_write_32(GRF_BASE + GRF_GPIO2A_IOMUX + i * 4,
  305. REG_SOC_WMSK | store_grf_gpio[i]);
  306. cru_gate_save = mmio_read_32(CRU_BASE + CRU_CLKGATE_CON(31));
  307. /*
  308. * when shutdown logic, we need to save gpio2 ~ gpio4 register,
  309. * we need to enable gpio2 ~ gpio4 clock here, since it may be gating,
  310. * and we do not care gpio0 and gpio1 clock gate, since we never
  311. * gating them
  312. */
  313. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(31),
  314. BITS_WITH_WMASK(0, 0x07, PCLK_GPIO2_GATE_SHIFT));
  315. for (i = 2; i < 5; i++) {
  316. uint32_t base = port_info[i].port_base;
  317. const struct gpio_save *save = &store_gpio[i - 2];
  318. mmio_write_32(base + SWPORTA_DR, save->swporta_dr);
  319. mmio_write_32(base + SWPORTA_DDR, save->swporta_ddr);
  320. mmio_write_32(base + INTEN, save->inten);
  321. mmio_write_32(base + INTMASK, save->intmask);
  322. mmio_write_32(base + INTTYPE_LEVEL, save->inttype_level);
  323. mmio_write_32(base + INT_POLARITY, save->int_polarity);
  324. mmio_write_32(base + DEBOUNCE, save->debounce);
  325. mmio_write_32(base + LS_SYNC, save->ls_sync);
  326. }
  327. mmio_write_32(CRU_BASE + CRU_CLKGATE_CON(31),
  328. cru_gate_save | REG_SOC_WMSK);
  329. }
  330. const gpio_ops_t rk3399_gpio_ops = {
  331. .get_direction = get_direction,
  332. .set_direction = set_direction,
  333. .get_value = get_value,
  334. .set_value = set_value,
  335. .set_pull = set_pull,
  336. .get_pull = get_pull,
  337. };
  338. void plat_rockchip_gpio_init(void)
  339. {
  340. gpio_init(&rk3399_gpio_ops);
  341. }