Angel Pons | e67ab18 | 2020-04-04 18:51:11 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | 13f6650 | 2019-03-03 08:01:05 +0200 | [diff] [blame] | 2 | #include <device/mmio.h> |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 3 | #include <assert.h> |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 4 | #include <gpio.h> |
| 5 | #include <types.h> |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 6 | |
| 7 | enum { |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 8 | MAX_GPIO_NUMBER = 134, |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 9 | }; |
| 10 | |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 11 | static void pos_bit_calc(gpio_t gpio, u32 *pos, u32 *bit) |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 12 | { |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 13 | *pos = gpio.id / MAX_GPIO_REG_BITS; |
| 14 | *bit = gpio.id % MAX_GPIO_REG_BITS; |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 15 | } |
| 16 | |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 17 | void gpio_set_pull(gpio_t gpio, enum pull_enable enable, |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 18 | enum pull_select select) |
| 19 | { |
| 20 | u32 pos; |
| 21 | u32 bit; |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 22 | u32 *en_reg, *sel_reg; |
| 23 | u32 pin = gpio.id; |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 24 | |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 25 | pos_bit_calc(gpio, &pos, &bit); |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 26 | |
| 27 | if (enable == GPIO_PULL_DISABLE) { |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 28 | en_reg = &mtk_gpio->pullen[pos].rst; |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 29 | } 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 Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 38 | en_reg = &mtk_gpio->pullen[pos].set; |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 39 | sel_reg = (select == GPIO_PULL_DOWN) ? |
Tristan Shieh | 71d227b | 2018-07-09 18:59:32 +0800 | [diff] [blame] | 40 | (&mtk_gpio->pullsel[pos].rst) : |
| 41 | (&mtk_gpio->pullsel[pos].set); |
Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 42 | write16(sel_reg, 1L << bit); |
| 43 | } |
| 44 | write16(en_reg, 1L << bit); |
| 45 | } |