blob: 8de8d0a1be9e18d1242680d27643efb88ffc4dc6 [file] [log] [blame]
Angel Ponse67ab182020-04-04 18:51:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkki13f66502019-03-03 08:01:05 +02002#include <device/mmio.h>
Biao Huang8c83c652015-07-31 17:10:55 +08003#include <assert.h>
Biao Huang8c83c652015-07-31 17:10:55 +08004#include <gpio.h>
5#include <types.h>
Biao Huang8c83c652015-07-31 17:10:55 +08006
7enum {
Tristan Shieh71d227b2018-07-09 18:59:32 +08008 MAX_GPIO_NUMBER = 134,
Biao Huang8c83c652015-07-31 17:10:55 +08009};
10
Tristan Shieh71d227b2018-07-09 18:59:32 +080011static void pos_bit_calc(gpio_t gpio, u32 *pos, u32 *bit)
Biao Huang8c83c652015-07-31 17:10:55 +080012{
Tristan Shieh71d227b2018-07-09 18:59:32 +080013 *pos = gpio.id / MAX_GPIO_REG_BITS;
14 *bit = gpio.id % MAX_GPIO_REG_BITS;
Biao Huang8c83c652015-07-31 17:10:55 +080015}
16
Tristan Shieh71d227b2018-07-09 18:59:32 +080017void gpio_set_pull(gpio_t gpio, enum pull_enable enable,
Biao Huang8c83c652015-07-31 17:10:55 +080018 enum pull_select select)
19{
20 u32 pos;
21 u32 bit;
Tristan Shieh71d227b2018-07-09 18:59:32 +080022 u32 *en_reg, *sel_reg;
23 u32 pin = gpio.id;
Biao Huang8c83c652015-07-31 17:10:55 +080024
Tristan Shieh71d227b2018-07-09 18:59:32 +080025 pos_bit_calc(gpio, &pos, &bit);
Biao Huang8c83c652015-07-31 17:10:55 +080026
27 if (enable == GPIO_PULL_DISABLE) {
Tristan Shieh71d227b2018-07-09 18:59:32 +080028 en_reg = &mtk_gpio->pullen[pos].rst;
Biao Huang8c83c652015-07-31 17:10:55 +080029 } else {
30 /* These pins' pulls can't be set through GPIO controller. */
31 assert(pin < 22 || pin > 27);
32 assert(pin < 47 || pin > 56);
33 assert(pin < 57 || pin > 68);
34 assert(pin < 73 || pin > 78);
35 assert(pin < 100 || pin > 105);
36 assert(pin < 119 || pin > 124);
37
Tristan Shieh71d227b2018-07-09 18:59:32 +080038 en_reg = &mtk_gpio->pullen[pos].set;
Biao Huang8c83c652015-07-31 17:10:55 +080039 sel_reg = (select == GPIO_PULL_DOWN) ?
Tristan Shieh71d227b2018-07-09 18:59:32 +080040 (&mtk_gpio->pullsel[pos].rst) :
41 (&mtk_gpio->pullsel[pos].set);
Biao Huang8c83c652015-07-31 17:10:55 +080042 write16(sel_reg, 1L << bit);
43 }
44 write16(en_reg, 1L << bit);
45}