1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
4 * Copyright (C) 2018 BayLibre, SAS
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 * Amlogic Meson SPI Flash Controller driver
17 #include <linux/bitfield.h>
23 #define REG_CTRL1 0x0c
24 #define REG_STATUS 0x10
25 #define REG_CTRL2 0x14
26 #define REG_CLOCK 0x18
28 #define REG_USER1 0x20
29 #define REG_USER2 0x24
30 #define REG_USER3 0x28
31 #define REG_USER4 0x2c
32 #define REG_SLAVE 0x30
33 #define REG_SLAVE1 0x34
34 #define REG_SLAVE2 0x38
35 #define REG_SLAVE3 0x3c
41 #define CMD_USER BIT(18)
42 #define CTRL_ENABLE_AHB BIT(17)
43 #define CLOCK_SOURCE BIT(31)
44 #define CLOCK_DIV_SHIFT 12
45 #define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT)
46 #define CLOCK_CNT_HIGH_SHIFT 6
47 #define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT)
48 #define CLOCK_CNT_LOW_SHIFT 0
49 #define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT)
50 #define USER_DIN_EN_MS BIT(0)
51 #define USER_CMP_MODE BIT(2)
52 #define USER_CLK_NOT_INV BIT(7)
53 #define USER_UC_DOUT_SEL BIT(27)
54 #define USER_UC_DIN_SEL BIT(28)
55 #define USER_UC_MASK ((BIT(5) - 1) << 27)
56 #define USER1_BN_UC_DOUT_SHIFT 17
57 #define USER1_BN_UC_DOUT_MASK (0xff << 16)
58 #define USER1_BN_UC_DIN_SHIFT 8
59 #define USER1_BN_UC_DIN_MASK (0xff << 8)
60 #define USER4_CS_POL_HIGH BIT(23)
61 #define USER4_IDLE_CLK_HIGH BIT(29)
62 #define USER4_CS_ACT BIT(30)
63 #define SLAVE_TRST_DONE BIT(4)
64 #define SLAVE_OP_MODE BIT(30)
65 #define SLAVE_SW_RST BIT(31)
67 #define SPIFC_BUFFER_SIZE 64
69 struct meson_spifc_priv {
70 struct regmap *regmap;
75 * meson_spifc_drain_buffer() - copy data from device buffer to memory
76 * @spifc: the Meson SPI device
77 * @buf: the destination buffer
78 * @len: number of bytes to copy
80 static void meson_spifc_drain_buffer(struct meson_spifc_priv *spifc,
87 regmap_read(spifc->regmap, REG_C0 + i, &data);
93 memcpy(buf, &data, len - i);
101 * meson_spifc_fill_buffer() - copy data from memory to device buffer
102 * @spifc: the Meson SPI device
103 * @buf: the source buffer
104 * @len: number of bytes to copy
106 static void meson_spifc_fill_buffer(struct meson_spifc_priv *spifc,
107 const u8 *buf, int len)
116 memcpy(&data, buf, len - i);
118 regmap_write(spifc->regmap, REG_C0 + i, data);
126 * meson_spifc_txrx() - transfer a chunk of data
127 * @spifc: the Meson SPI device
128 * @dout: data buffer for TX
129 * @din: data buffer for RX
130 * @offset: offset of the data to transfer
131 * @len: length of the data to transfer
132 * @last_xfer: whether this is the last transfer of the message
133 * @last_chunk: whether this is the last chunk of the transfer
134 * Return: 0 on success, a negative value on error
136 static int meson_spifc_txrx(struct meson_spifc_priv *spifc,
137 const u8 *dout, u8 *din, int offset,
138 int len, bool last_xfer, bool last_chunk)
145 meson_spifc_fill_buffer(spifc, dout + offset, len);
147 /* enable DOUT stage */
148 regmap_update_bits(spifc->regmap, REG_USER, USER_UC_MASK,
150 regmap_write(spifc->regmap, REG_USER1,
151 (8 * len - 1) << USER1_BN_UC_DOUT_SHIFT);
153 /* enable data input during DOUT */
154 regmap_update_bits(spifc->regmap, REG_USER, USER_DIN_EN_MS,
157 if (last_chunk && last_xfer)
160 regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_ACT,
161 keep_cs ? USER4_CS_ACT : 0);
163 /* clear transition done bit */
164 regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_TRST_DONE, 0);
166 regmap_update_bits(spifc->regmap, REG_CMD, CMD_USER, CMD_USER);
168 /* wait for the current operation to terminate */
169 ret = regmap_read_poll_timeout(spifc->regmap, REG_SLAVE, data,
170 (data & SLAVE_TRST_DONE),
171 0, 5 * CONFIG_SYS_HZ);
174 meson_spifc_drain_buffer(spifc, din + offset, len);
180 * meson_spifc_xfer() - perform a single transfer
181 * @dev: the SPI controller device
182 * @bitlen: length of the transfer
183 * @dout: data buffer for TX
184 * @din: data buffer for RX
185 * @flags: transfer flags
186 * Return: 0 on success, a negative value on error
188 static int meson_spifc_xfer(struct udevice *slave, unsigned int bitlen,
189 const void *dout, void *din, unsigned long flags)
191 struct meson_spifc_priv *spifc = dev_get_priv(slave->parent);
192 int blen = bitlen / 8;
193 int len, done = 0, ret = 0;
198 debug("xfer len %d (%d) dout %p din %p\n", bitlen, blen, dout, din);
200 regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, 0);
202 while (done < blen && !ret) {
203 len = min_t(int, blen - done, SPIFC_BUFFER_SIZE);
204 ret = meson_spifc_txrx(spifc, dout, din, done, len,
205 flags & SPI_XFER_END,
210 regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB,
217 * meson_spifc_set_speed() - program the clock divider
218 * @dev: the SPI controller device
219 * @speed: desired speed in Hz
221 static int meson_spifc_set_speed(struct udevice *dev, uint speed)
223 struct meson_spifc_priv *spifc = dev_get_priv(dev);
224 unsigned long parent, value;
227 parent = clk_get_rate(&spifc->clk);
228 n = max_t(int, parent / speed - 1, 1);
230 debug("parent %lu, speed %u, n %d\n", parent, speed, n);
232 value = (n << CLOCK_DIV_SHIFT) & CLOCK_DIV_MASK;
233 value |= (n << CLOCK_CNT_LOW_SHIFT) & CLOCK_CNT_LOW_MASK;
234 value |= (((n + 1) / 2 - 1) << CLOCK_CNT_HIGH_SHIFT) &
237 regmap_write(spifc->regmap, REG_CLOCK, value);
243 * meson_spifc_set_mode() - setups the SPI bus mode
244 * @dev: the SPI controller device
245 * @mode: desired mode bitfield
246 * Return: 0 on success, -ENODEV on error
248 static int meson_spifc_set_mode(struct udevice *dev, uint mode)
250 struct meson_spifc_priv *spifc = dev_get_priv(dev);
252 if (mode & (SPI_CPHA | SPI_RX_QUAD | SPI_RX_DUAL |
253 SPI_TX_QUAD | SPI_TX_DUAL))
256 regmap_update_bits(spifc->regmap, REG_USER, USER_CLK_NOT_INV,
257 mode & SPI_CPOL ? USER_CLK_NOT_INV : 0);
259 regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_POL_HIGH,
260 mode & SPI_CS_HIGH ? USER4_CS_POL_HIGH : 0);
266 * meson_spifc_hw_init() - reset and initialize the SPI controller
267 * @spifc: the Meson SPI device
269 static void meson_spifc_hw_init(struct meson_spifc_priv *spifc)
272 regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_SW_RST,
274 /* disable compatible mode */
275 regmap_update_bits(spifc->regmap, REG_USER, USER_CMP_MODE, 0);
276 /* set master mode */
277 regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_OP_MODE, 0);
280 static const struct dm_spi_ops meson_spifc_ops = {
281 .xfer = meson_spifc_xfer,
282 .set_speed = meson_spifc_set_speed,
283 .set_mode = meson_spifc_set_mode,
286 static int meson_spifc_probe(struct udevice *dev)
288 struct meson_spifc_priv *priv = dev_get_priv(dev);
291 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
295 ret = clk_get_by_index(dev, 0, &priv->clk);
299 ret = clk_enable(&priv->clk);
303 meson_spifc_hw_init(priv);
308 static const struct udevice_id meson_spifc_ids[] = {
309 { .compatible = "amlogic,meson-gxbb-spifc", },
313 U_BOOT_DRIVER(meson_spifc) = {
314 .name = "meson_spifc",
316 .of_match = meson_spifc_ids,
317 .ops = &meson_spifc_ops,
318 .probe = meson_spifc_probe,
319 .priv_auto_alloc_size = sizeof(struct meson_spifc_priv),