]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/spi/altera_spi.c
spi: altera_spi: Use BIT macro
[people/ms/u-boot.git] / drivers / spi / altera_spi.c
1 /*
2 * Altera SPI driver
3 *
4 * based on bfin_spi.c
5 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <malloc.h>
14 #include <spi.h>
15 #include <fdtdec.h>
16 #include <asm/io.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 #ifndef CONFIG_ALTERA_SPI_IDLE_VAL
21 #define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
22 #endif
23
24 struct altera_spi_regs {
25 u32 rxdata;
26 u32 txdata;
27 u32 status;
28 u32 control;
29 u32 _reserved;
30 u32 slave_sel;
31 };
32
33 struct altera_spi_platdata {
34 struct altera_spi_regs *regs;
35 };
36
37 struct altera_spi_priv {
38 struct altera_spi_regs *regs;
39 };
40
41 #define ALTERA_SPI_STATUS_RRDY_MSK BIT(7)
42 #define ALTERA_SPI_CONTROL_SSO_MSK BIT(10)
43
44 static void spi_cs_activate(struct udevice *dev, uint cs)
45 {
46 struct udevice *bus = dev->parent;
47 struct altera_spi_priv *priv = dev_get_priv(bus);
48 struct altera_spi_regs *const regs = priv->regs;
49
50 writel(1 << cs, &regs->slave_sel);
51 writel(ALTERA_SPI_CONTROL_SSO_MSK, &regs->control);
52 }
53
54 static void spi_cs_deactivate(struct udevice *dev)
55 {
56 struct udevice *bus = dev->parent;
57 struct altera_spi_priv *priv = dev_get_priv(bus);
58 struct altera_spi_regs *const regs = priv->regs;
59
60 writel(0, &regs->control);
61 writel(0, &regs->slave_sel);
62 }
63
64 static int altera_spi_claim_bus(struct udevice *dev)
65 {
66 struct udevice *bus = dev->parent;
67 struct altera_spi_priv *priv = dev_get_priv(bus);
68 struct altera_spi_regs *const regs = priv->regs;
69
70 writel(0, &regs->control);
71 writel(0, &regs->slave_sel);
72
73 return 0;
74 }
75
76 static int altera_spi_release_bus(struct udevice *dev)
77 {
78 struct udevice *bus = dev->parent;
79 struct altera_spi_priv *priv = dev_get_priv(bus);
80 struct altera_spi_regs *const regs = priv->regs;
81
82 writel(0, &regs->slave_sel);
83
84 return 0;
85 }
86
87 static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
88 const void *dout, void *din, unsigned long flags)
89 {
90 struct udevice *bus = dev->parent;
91 struct altera_spi_priv *priv = dev_get_priv(bus);
92 struct altera_spi_regs *const regs = priv->regs;
93 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
94
95 /* assume spi core configured to do 8 bit transfers */
96 unsigned int bytes = bitlen / 8;
97 const unsigned char *txp = dout;
98 unsigned char *rxp = din;
99 uint32_t reg, data, start;
100
101 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
102 bus->seq, slave_plat->cs, bitlen, bytes, flags);
103
104 if (bitlen == 0)
105 goto done;
106
107 if (bitlen % 8) {
108 flags |= SPI_XFER_END;
109 goto done;
110 }
111
112 /* empty read buffer */
113 if (readl(&regs->status) & ALTERA_SPI_STATUS_RRDY_MSK)
114 readl(&regs->rxdata);
115
116 if (flags & SPI_XFER_BEGIN)
117 spi_cs_activate(dev, slave_plat->cs);
118
119 while (bytes--) {
120 if (txp)
121 data = *txp++;
122 else
123 data = CONFIG_ALTERA_SPI_IDLE_VAL;
124
125 debug("%s: tx:%x ", __func__, data);
126 writel(data, &regs->txdata);
127
128 start = get_timer(0);
129 while (1) {
130 reg = readl(&regs->status);
131 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
132 break;
133 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
134 debug("%s: Transmission timed out!\n", __func__);
135 return -1;
136 }
137 }
138
139 data = readl(&regs->rxdata);
140 if (rxp)
141 *rxp++ = data & 0xff;
142
143 debug("rx:%x\n", data);
144 }
145
146 done:
147 if (flags & SPI_XFER_END)
148 spi_cs_deactivate(dev);
149
150 return 0;
151 }
152
153 static int altera_spi_set_speed(struct udevice *bus, uint speed)
154 {
155 return 0;
156 }
157
158 static int altera_spi_set_mode(struct udevice *bus, uint mode)
159 {
160 return 0;
161 }
162
163 static int altera_spi_probe(struct udevice *bus)
164 {
165 struct altera_spi_platdata *plat = dev_get_platdata(bus);
166 struct altera_spi_priv *priv = dev_get_priv(bus);
167
168 priv->regs = plat->regs;
169
170 return 0;
171 }
172
173 static int altera_spi_ofdata_to_platdata(struct udevice *bus)
174 {
175 struct altera_spi_platdata *plat = dev_get_platdata(bus);
176
177 plat->regs = ioremap(dev_get_addr(bus),
178 sizeof(struct altera_spi_regs));
179
180 return 0;
181 }
182
183 static const struct dm_spi_ops altera_spi_ops = {
184 .claim_bus = altera_spi_claim_bus,
185 .release_bus = altera_spi_release_bus,
186 .xfer = altera_spi_xfer,
187 .set_speed = altera_spi_set_speed,
188 .set_mode = altera_spi_set_mode,
189 /*
190 * cs_info is not needed, since we require all chip selects to be
191 * in the device tree explicitly
192 */
193 };
194
195 static const struct udevice_id altera_spi_ids[] = {
196 { .compatible = "altr,spi-1.0", },
197 { }
198 };
199
200 U_BOOT_DRIVER(altera_spi) = {
201 .name = "altera_spi",
202 .id = UCLASS_SPI,
203 .of_match = altera_spi_ids,
204 .ops = &altera_spi_ops,
205 .ofdata_to_platdata = altera_spi_ofdata_to_platdata,
206 .platdata_auto_alloc_size = sizeof(struct altera_spi_platdata),
207 .priv_auto_alloc_size = sizeof(struct altera_spi_priv),
208 .probe = altera_spi_probe,
209 };