blob: 0d534268e9661a73cc1cfd757e9386386cb66e5b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wills Wangc1024532016-03-16 16:59:56 +08002/*
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
Wills Wangc1024532016-03-16 16:59:56 +08004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Wills Wangc1024532016-03-16 16:59:56 +080011#include <asm/io.h>
12#include <dm/pinctrl.h>
13#include <mach/ar71xx_regs.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17enum periph_id {
18 PERIPH_ID_UART0,
19 PERIPH_ID_SPI0,
20 PERIPH_ID_NONE = -1,
21};
22
23struct qca953x_pinctrl_priv {
24 void __iomem *regs;
25};
26
27static void pinctrl_qca953x_spi_config(struct qca953x_pinctrl_priv *priv, int cs)
28{
29 switch (cs) {
30 case 0:
31 clrsetbits_be32(priv->regs + AR71XX_GPIO_REG_OE,
32 QCA953X_GPIO(5) | QCA953X_GPIO(6) |
33 QCA953X_GPIO(7), QCA953X_GPIO(8));
34
35 clrsetbits_be32(priv->regs + QCA953X_GPIO_REG_OUT_FUNC1,
36 QCA953X_GPIO_MUX_MASK(8) |
37 QCA953X_GPIO_MUX_MASK(16) |
38 QCA953X_GPIO_MUX_MASK(24),
39 (QCA953X_GPIO_OUT_MUX_SPI_CS0 << 8) |
40 (QCA953X_GPIO_OUT_MUX_SPI_CLK << 16) |
41 (QCA953X_GPIO_OUT_MUX_SPI_MOSI << 24));
42
43 clrsetbits_be32(priv->regs + QCA953X_GPIO_REG_IN_ENABLE0,
44 QCA953X_GPIO_MUX_MASK(0),
45 QCA953X_GPIO_IN_MUX_SPI_DATA_IN);
46
47 setbits_be32(priv->regs + AR71XX_GPIO_REG_OUT,
48 QCA953X_GPIO(8));
49 break;
50 }
51}
52
53static void pinctrl_qca953x_uart_config(struct qca953x_pinctrl_priv *priv, int uart_id)
54{
55 switch (uart_id) {
56 case PERIPH_ID_UART0:
57 clrsetbits_be32(priv->regs + AR71XX_GPIO_REG_OE,
58 QCA953X_GPIO(9), QCA953X_GPIO(10));
59
60 clrsetbits_be32(priv->regs + QCA953X_GPIO_REG_OUT_FUNC2,
61 QCA953X_GPIO_MUX_MASK(16),
62 QCA953X_GPIO_OUT_MUX_UART0_SOUT << 16);
63
64 clrsetbits_be32(priv->regs + QCA953X_GPIO_REG_IN_ENABLE0,
65 QCA953X_GPIO_MUX_MASK(8),
66 QCA953X_GPIO_IN_MUX_UART0_SIN << 8);
67
68 setbits_be32(priv->regs + AR71XX_GPIO_REG_OUT,
69 QCA953X_GPIO(10));
70 break;
71 }
72}
73
74static int qca953x_pinctrl_request(struct udevice *dev, int func, int flags)
75{
76 struct qca953x_pinctrl_priv *priv = dev_get_priv(dev);
77
78 debug("%s: func=%x, flags=%x\n", __func__, func, flags);
79 switch (func) {
80 case PERIPH_ID_SPI0:
81 pinctrl_qca953x_spi_config(priv, flags);
82 break;
83 case PERIPH_ID_UART0:
84 pinctrl_qca953x_uart_config(priv, func);
85 break;
86 default:
87 return -EINVAL;
88 }
89
90 return 0;
91}
92
93static int qca953x_pinctrl_get_periph_id(struct udevice *dev,
94 struct udevice *periph)
95{
96 u32 cell[2];
97 int ret;
98
Simon Glasse160f7d2017-01-17 16:52:55 -070099 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(periph),
Wills Wangc1024532016-03-16 16:59:56 +0800100 "interrupts", cell, ARRAY_SIZE(cell));
101 if (ret < 0)
102 return -EINVAL;
103
104 switch (cell[0]) {
105 case 128:
106 return PERIPH_ID_UART0;
107 case 129:
108 return PERIPH_ID_SPI0;
109 }
110 return -ENOENT;
111}
112
113static int qca953x_pinctrl_set_state_simple(struct udevice *dev,
114 struct udevice *periph)
115{
116 int func;
117
118 func = qca953x_pinctrl_get_periph_id(dev, periph);
119 if (func < 0)
120 return func;
121 return qca953x_pinctrl_request(dev, func, 0);
122}
123
124static struct pinctrl_ops qca953x_pinctrl_ops = {
125 .set_state_simple = qca953x_pinctrl_set_state_simple,
126 .request = qca953x_pinctrl_request,
127 .get_periph_id = qca953x_pinctrl_get_periph_id,
128};
129
130static int qca953x_pinctrl_probe(struct udevice *dev)
131{
132 struct qca953x_pinctrl_priv *priv = dev_get_priv(dev);
133 fdt_addr_t addr;
134
Masahiro Yamada25484932020-07-17 14:36:48 +0900135 addr = dev_read_addr(dev);
Wills Wangc1024532016-03-16 16:59:56 +0800136 if (addr == FDT_ADDR_T_NONE)
137 return -EINVAL;
138
139 priv->regs = map_physmem(addr,
140 AR71XX_GPIO_SIZE,
141 MAP_NOCACHE);
142 return 0;
143}
144
145static const struct udevice_id qca953x_pinctrl_ids[] = {
146 { .compatible = "qca,qca953x-pinctrl" },
147 { }
148};
149
150U_BOOT_DRIVER(pinctrl_qca953x) = {
151 .name = "pinctrl_qca953x",
152 .id = UCLASS_PINCTRL,
153 .of_match = qca953x_pinctrl_ids,
Simon Glass41575d82020-12-03 16:55:17 -0700154 .priv_auto = sizeof(struct qca953x_pinctrl_priv),
Wills Wangc1024532016-03-16 16:59:56 +0800155 .ops = &qca953x_pinctrl_ops,
156 .probe = qca953x_pinctrl_probe,
157};