Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
| 3 | * Author: Andy Yan <andy.yan@rock-chips.com> |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <syscon.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/hardware.h> |
| 13 | #include <asm/arch/grf_rk3368.h> |
| 14 | #include <asm/arch/periph.h> |
| 15 | #include <dm/pinctrl.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct rk3368_pinctrl_priv { |
| 20 | struct rk3368_grf *grf; |
| 21 | struct rk3368_pmu_grf *pmugrf; |
| 22 | }; |
| 23 | |
| 24 | static void pinctrl_rk3368_uart_config(struct rk3368_pinctrl_priv *priv, |
| 25 | int uart_id) |
| 26 | { |
| 27 | struct rk3368_grf *grf = priv->grf; |
| 28 | struct rk3368_pmu_grf *pmugrf = priv->pmugrf; |
| 29 | |
| 30 | switch (uart_id) { |
| 31 | case PERIPH_ID_UART2: |
| 32 | rk_clrsetreg(&grf->gpio2a_iomux, |
| 33 | GPIO2A6_MASK | GPIO2A5_MASK, |
Philipp Tomsich | 9ac1d83 | 2017-07-25 17:01:06 +0200 | [diff] [blame] | 34 | GPIO2A6_UART2_SIN | GPIO2A5_UART2_SOUT); |
Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 35 | break; |
| 36 | case PERIPH_ID_UART0: |
| 37 | break; |
| 38 | case PERIPH_ID_UART1: |
| 39 | break; |
| 40 | case PERIPH_ID_UART3: |
| 41 | break; |
| 42 | case PERIPH_ID_UART4: |
| 43 | rk_clrsetreg(&pmugrf->gpio0d_iomux, |
| 44 | GPIO0D0_MASK | GPIO0D1_MASK | |
| 45 | GPIO0D2_MASK | GPIO0D3_MASK, |
Philipp Tomsich | 9ac1d83 | 2017-07-25 17:01:06 +0200 | [diff] [blame] | 46 | GPIO0D0_GPIO | GPIO0D1_GPIO | |
| 47 | GPIO0D2_UART4_SOUT | GPIO0D3_UART4_SIN); |
Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 48 | break; |
| 49 | default: |
| 50 | debug("uart id = %d iomux error!\n", uart_id); |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
Philipp Tomsich | 3487943 | 2017-07-14 20:00:58 +0200 | [diff] [blame^] | 55 | #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP) |
| 56 | static void pinctrl_rk3368_gmac_config(struct rk3368_grf *grf, int gmac_id) |
| 57 | { |
| 58 | rk_clrsetreg(&grf->gpio3b_iomux, |
| 59 | GPIO3B0_MASK | GPIO3B1_MASK | |
| 60 | GPIO3B2_MASK | GPIO3B5_MASK | |
| 61 | GPIO3B6_MASK | GPIO3B7_MASK, |
| 62 | GPIO3B0_MAC_TXD0 | GPIO3B1_MAC_TXD1 | |
| 63 | GPIO3B2_MAC_TXD2 | GPIO3B5_MAC_TXEN | |
| 64 | GPIO3B6_MAC_TXD3 | GPIO3B7_MAC_RXD0); |
| 65 | rk_clrsetreg(&grf->gpio3c_iomux, |
| 66 | GPIO3C0_MASK | GPIO3C1_MASK | |
| 67 | GPIO3C2_MASK | GPIO3C3_MASK | |
| 68 | GPIO3C4_MASK | GPIO3C5_MASK | |
| 69 | GPIO3C6_MASK, |
| 70 | GPIO3C0_MAC_RXD1 | GPIO3C1_MAC_RXD2 | |
| 71 | GPIO3C2_MAC_RXD3 | GPIO3C3_MAC_MDC | |
| 72 | GPIO3C4_MAC_RXDV | GPIO3C5_MAC_RXEN | |
| 73 | GPIO3C6_MAC_CLK); |
| 74 | rk_clrsetreg(&grf->gpio3d_iomux, |
| 75 | GPIO3D0_MASK | GPIO3D1_MASK | |
| 76 | GPIO3D4_MASK, |
| 77 | GPIO3D0_MAC_MDIO | GPIO3D1_MAC_RXCLK | |
| 78 | GPIO3D4_MAC_TXCLK); |
| 79 | } |
| 80 | #endif |
| 81 | |
Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 82 | static int rk3368_pinctrl_request(struct udevice *dev, int func, int flags) |
| 83 | { |
| 84 | struct rk3368_pinctrl_priv *priv = dev_get_priv(dev); |
| 85 | |
| 86 | debug("%s: func=%d, flags=%x\n", __func__, func, flags); |
| 87 | switch (func) { |
| 88 | case PERIPH_ID_UART0: |
| 89 | case PERIPH_ID_UART1: |
| 90 | case PERIPH_ID_UART2: |
| 91 | case PERIPH_ID_UART3: |
| 92 | case PERIPH_ID_UART4: |
| 93 | pinctrl_rk3368_uart_config(priv, func); |
| 94 | break; |
Philipp Tomsich | 3487943 | 2017-07-14 20:00:58 +0200 | [diff] [blame^] | 95 | #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP) |
| 96 | case PERIPH_ID_GMAC: |
| 97 | pinctrl_rk3368_gmac_config(priv->grf, func); |
| 98 | break; |
| 99 | #endif |
Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 100 | default: |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int rk3368_pinctrl_get_periph_id(struct udevice *dev, |
| 108 | struct udevice *periph) |
| 109 | { |
| 110 | u32 cell[3]; |
| 111 | int ret; |
| 112 | |
| 113 | ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(periph), |
| 114 | "interrupts", cell, ARRAY_SIZE(cell)); |
| 115 | if (ret < 0) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | switch (cell[1]) { |
| 119 | case 59: |
| 120 | return PERIPH_ID_UART4; |
| 121 | case 58: |
| 122 | return PERIPH_ID_UART3; |
| 123 | case 57: |
| 124 | return PERIPH_ID_UART2; |
| 125 | case 56: |
| 126 | return PERIPH_ID_UART1; |
| 127 | case 55: |
| 128 | return PERIPH_ID_UART0; |
Philipp Tomsich | 3487943 | 2017-07-14 20:00:58 +0200 | [diff] [blame^] | 129 | #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP) |
| 130 | case 27: |
| 131 | return PERIPH_ID_GMAC; |
| 132 | #endif |
Andy Yan | 717733f | 2017-05-15 17:50:35 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return -ENOENT; |
| 136 | } |
| 137 | |
| 138 | static int rk3368_pinctrl_set_state_simple(struct udevice *dev, |
| 139 | struct udevice *periph) |
| 140 | { |
| 141 | int func; |
| 142 | |
| 143 | func = rk3368_pinctrl_get_periph_id(dev, periph); |
| 144 | if (func < 0) |
| 145 | return func; |
| 146 | |
| 147 | return rk3368_pinctrl_request(dev, func, 0); |
| 148 | } |
| 149 | |
| 150 | static struct pinctrl_ops rk3368_pinctrl_ops = { |
| 151 | .set_state_simple = rk3368_pinctrl_set_state_simple, |
| 152 | .request = rk3368_pinctrl_request, |
| 153 | .get_periph_id = rk3368_pinctrl_get_periph_id, |
| 154 | }; |
| 155 | |
| 156 | static int rk3368_pinctrl_probe(struct udevice *dev) |
| 157 | { |
| 158 | struct rk3368_pinctrl_priv *priv = dev_get_priv(dev); |
| 159 | int ret = 0; |
| 160 | |
| 161 | priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 162 | priv->pmugrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUGRF); |
| 163 | |
| 164 | debug("%s: grf=%p pmugrf:%p\n", __func__, priv->grf, priv->pmugrf); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static const struct udevice_id rk3368_pinctrl_ids[] = { |
| 170 | { .compatible = "rockchip,rk3368-pinctrl" }, |
| 171 | { } |
| 172 | }; |
| 173 | |
| 174 | U_BOOT_DRIVER(pinctrl_rk3368) = { |
| 175 | .name = "rockchip_rk3368_pinctrl", |
| 176 | .id = UCLASS_PINCTRL, |
| 177 | .of_match = rk3368_pinctrl_ids, |
| 178 | .priv_auto_alloc_size = sizeof(struct rk3368_pinctrl_priv), |
| 179 | .ops = &rk3368_pinctrl_ops, |
| 180 | .bind = dm_scan_fdt_dev, |
| 181 | .probe = rk3368_pinctrl_probe, |
| 182 | }; |