blob: bdf3910a88572c3ca71bad34d67c3ee4ffce435c [file] [log] [blame]
Andy Yan09aa7c42017-06-01 18:00:10 +08001/*
2 * (C) Copyright 2016 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/grf_rv1108.h>
13#include <asm/arch/hardware.h>
14#include <asm/arch/periph.h>
15#include <dm/pinctrl.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19struct rv1108_pinctrl_priv {
20 struct rv1108_grf *grf;
21};
22
23static void pinctrl_rv1108_uart_config(struct rv1108_grf *grf, int uart_id)
24{
25 switch (uart_id) {
26 case PERIPH_ID_UART0:
27 rk_clrsetreg(&grf->gpio3a_iomux,
28 GPIO3A6_MASK | GPIO3A5_MASK,
29 GPIO3A6_UART1_SOUT << GPIO3A6_SHIFT |
30 GPIO3A5_UART1_SIN << GPIO3A5_SHIFT);
31 break;
32 case PERIPH_ID_UART1:
33 rk_clrsetreg(&grf->gpio1d_iomux,
34 GPIO1D3_MASK | GPIO1D2_MASK | GPIO1D1_MASK |
35 GPIO1D0_MASK,
36 GPIO1D3_UART0_SOUT << GPIO1D3_SHIFT |
37 GPIO1D2_UART0_SIN << GPIO1D2_SHIFT |
38 GPIO1D1_UART0_RTSN << GPIO1D1_SHIFT |
39 GPIO1D0_UART0_CTSN << GPIO1D0_SHIFT);
40 break;
41 case PERIPH_ID_UART2:
42 rk_clrsetreg(&grf->gpio2d_iomux,
43 GPIO2D2_MASK | GPIO2D1_MASK,
44 GPIO2D2_UART2_SOUT_M0 << GPIO2D2_SHIFT |
45 GPIO2D1_UART2_SIN_M0 << GPIO2D1_SHIFT);
46 break;
47 }
48}
49
50static void pinctrl_rv1108_gmac_config(struct rv1108_grf *grf, int func)
51{
52 rk_clrsetreg(&grf->gpio1b_iomux,
53 GPIO1B7_MASK | GPIO1B6_MASK | GPIO1B5_MASK |
54 GPIO1B4_MASK | GPIO1B3_MASK | GPIO1B2_MASK,
55 GPIO1B7_GMAC_RXDV << GPIO1B7_SHIFT |
56 GPIO1B6_GMAC_RXD1 << GPIO1B6_SHIFT |
57 GPIO1B5_GMAC_RXD0 << GPIO1B5_SHIFT |
58 GPIO1B4_GMAC_TXEN << GPIO1B4_SHIFT |
59 GPIO1B3_GMAC_TXD1 << GPIO1B3_SHIFT |
60 GPIO1B2_GMAC_TXD0 << GPIO1B2_SHIFT);
61 rk_clrsetreg(&grf->gpio1c_iomux,
62 GPIO1C5_MASK | GPIO1C4_MASK |
63 GPIO1C3_MASK | GPIO1C2_MASK,
64 GPIO1C5_GMAC_CLK << GPIO1C5_SHIFT |
65 GPIO1C4_GMAC_MDC << GPIO1C4_SHIFT |
66 GPIO1C3_GMAC_MDIO << GPIO1C3_SHIFT |
67 GPIO1C2_GMAC_RXER << GPIO1C2_SHIFT);
68 writel(0xffff57f5, &grf->gpio1b_drv);
69}
70
71static void pinctrl_rv1108_sfc_config(struct rv1108_grf *grf)
72{
73 rk_clrsetreg(&grf->gpio2a_iomux, GPIO2A3_MASK | GPIO2A2_MASK |
74 GPIO2A1_MASK | GPIO2A0_MASK,
75 GPIO2A3_SFC_HOLD_IO3 << GPIO2A3_SHIFT |
76 GPIO2A2_SFC_WP_IO2 << GPIO2A2_SHIFT |
77 GPIO2A1_SFC_SO_IO1 << GPIO2A1_SHIFT |
78 GPIO2A0_SFC_SI_IO0 << GPIO2A0_SHIFT);
79 rk_clrsetreg(&grf->gpio2b_iomux, GPIO2B7_MASK | GPIO2B4_MASK,
80 GPIO2B7_SFC_CLK << GPIO2B7_SHIFT |
81 GPIO2B4_SFC_CSN0 << GPIO2B4_SHIFT);
82}
83
84static int rv1108_pinctrl_request(struct udevice *dev, int func, int flags)
85{
86 struct rv1108_pinctrl_priv *priv = dev_get_priv(dev);
87
88 switch (func) {
89 case PERIPH_ID_UART0:
90 case PERIPH_ID_UART1:
91 case PERIPH_ID_UART2:
92 pinctrl_rv1108_uart_config(priv->grf, func);
93 break;
94 case PERIPH_ID_GMAC:
95 pinctrl_rv1108_gmac_config(priv->grf, func);
96 case PERIPH_ID_SFC:
97 pinctrl_rv1108_sfc_config(priv->grf);
98 default:
99 return -EINVAL;
100 }
101
102 return 0;
103}
104
105static int rv1108_pinctrl_get_periph_id(struct udevice *dev,
106 struct udevice *periph)
107{
108 u32 cell[3];
109 int ret;
110
111 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(periph),
112 "interrupts", cell, ARRAY_SIZE(cell));
113 if (ret < 0)
114 return -EINVAL;
115
116 switch (cell[1]) {
117 case 11:
118 return PERIPH_ID_SDCARD;
119 case 13:
120 return PERIPH_ID_EMMC;
121 case 19:
122 return PERIPH_ID_GMAC;
123 case 30:
124 return PERIPH_ID_I2C0;
125 case 31:
126 return PERIPH_ID_I2C1;
127 case 32:
128 return PERIPH_ID_I2C2;
129 case 39:
130 return PERIPH_ID_PWM0;
131 case 44:
132 return PERIPH_ID_UART0;
133 case 45:
134 return PERIPH_ID_UART1;
135 case 46:
136 return PERIPH_ID_UART2;
137 case 56:
138 return PERIPH_ID_SFC;
139 }
140
141 return -ENOENT;
142}
143
144static int rv1108_pinctrl_set_state_simple(struct udevice *dev,
145 struct udevice *periph)
146{
147 int func;
148
149 func = rv1108_pinctrl_get_periph_id(dev, periph);
150 if (func < 0)
151 return func;
152
153 return rv1108_pinctrl_request(dev, func, 0);
154}
155
156static struct pinctrl_ops rv1108_pinctrl_ops = {
157 .set_state_simple = rv1108_pinctrl_set_state_simple,
158 .request = rv1108_pinctrl_request,
159 .get_periph_id = rv1108_pinctrl_get_periph_id,
160};
161
162static int rv1108_pinctrl_probe(struct udevice *dev)
163{
164 struct rv1108_pinctrl_priv *priv = dev_get_priv(dev);
165
166 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
167
168 return 0;
169}
170
171static const struct udevice_id rv1108_pinctrl_ids[] = {
172 {.compatible = "rockchip,rv1108-pinctrl" },
173 { }
174};
175
176U_BOOT_DRIVER(pinctrl_rv1108) = {
177 .name = "pinctrl_rv1108",
178 .id = UCLASS_PINCTRL,
179 .of_match = rv1108_pinctrl_ids,
180 .priv_auto_alloc_size = sizeof(struct rv1108_pinctrl_priv),
181 .ops = &rv1108_pinctrl_ops,
182 .bind = dm_scan_fdt_dev,
183 .probe = rv1108_pinctrl_probe,
184};