1*9d26506aSNeil Armstrong // SPDX-License-Identifier: GPL-2.0+
2*9d26506aSNeil Armstrong /*
3*9d26506aSNeil Armstrong * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
4*9d26506aSNeil Armstrong * Copyright (C) 2018 BayLibre, SAS
5*9d26506aSNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com>
6*9d26506aSNeil Armstrong *
7*9d26506aSNeil Armstrong * Amlogic Meson SPI Flash Controller driver
8*9d26506aSNeil Armstrong */
9*9d26506aSNeil Armstrong
10*9d26506aSNeil Armstrong #include <common.h>
11*9d26506aSNeil Armstrong #include <spi.h>
12*9d26506aSNeil Armstrong #include <clk.h>
13*9d26506aSNeil Armstrong #include <dm.h>
14*9d26506aSNeil Armstrong #include <regmap.h>
15*9d26506aSNeil Armstrong #include <errno.h>
16*9d26506aSNeil Armstrong #include <asm/io.h>
17*9d26506aSNeil Armstrong #include <linux/bitfield.h>
18*9d26506aSNeil Armstrong
19*9d26506aSNeil Armstrong /* register map */
20*9d26506aSNeil Armstrong #define REG_CMD 0x00
21*9d26506aSNeil Armstrong #define REG_ADDR 0x04
22*9d26506aSNeil Armstrong #define REG_CTRL 0x08
23*9d26506aSNeil Armstrong #define REG_CTRL1 0x0c
24*9d26506aSNeil Armstrong #define REG_STATUS 0x10
25*9d26506aSNeil Armstrong #define REG_CTRL2 0x14
26*9d26506aSNeil Armstrong #define REG_CLOCK 0x18
27*9d26506aSNeil Armstrong #define REG_USER 0x1c
28*9d26506aSNeil Armstrong #define REG_USER1 0x20
29*9d26506aSNeil Armstrong #define REG_USER2 0x24
30*9d26506aSNeil Armstrong #define REG_USER3 0x28
31*9d26506aSNeil Armstrong #define REG_USER4 0x2c
32*9d26506aSNeil Armstrong #define REG_SLAVE 0x30
33*9d26506aSNeil Armstrong #define REG_SLAVE1 0x34
34*9d26506aSNeil Armstrong #define REG_SLAVE2 0x38
35*9d26506aSNeil Armstrong #define REG_SLAVE3 0x3c
36*9d26506aSNeil Armstrong #define REG_C0 0x40
37*9d26506aSNeil Armstrong #define REG_B8 0x60
38*9d26506aSNeil Armstrong #define REG_MAX 0x7c
39*9d26506aSNeil Armstrong
40*9d26506aSNeil Armstrong /* register fields */
41*9d26506aSNeil Armstrong #define CMD_USER BIT(18)
42*9d26506aSNeil Armstrong #define CTRL_ENABLE_AHB BIT(17)
43*9d26506aSNeil Armstrong #define CLOCK_SOURCE BIT(31)
44*9d26506aSNeil Armstrong #define CLOCK_DIV_SHIFT 12
45*9d26506aSNeil Armstrong #define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT)
46*9d26506aSNeil Armstrong #define CLOCK_CNT_HIGH_SHIFT 6
47*9d26506aSNeil Armstrong #define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT)
48*9d26506aSNeil Armstrong #define CLOCK_CNT_LOW_SHIFT 0
49*9d26506aSNeil Armstrong #define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT)
50*9d26506aSNeil Armstrong #define USER_DIN_EN_MS BIT(0)
51*9d26506aSNeil Armstrong #define USER_CMP_MODE BIT(2)
52*9d26506aSNeil Armstrong #define USER_CLK_NOT_INV BIT(7)
53*9d26506aSNeil Armstrong #define USER_UC_DOUT_SEL BIT(27)
54*9d26506aSNeil Armstrong #define USER_UC_DIN_SEL BIT(28)
55*9d26506aSNeil Armstrong #define USER_UC_MASK ((BIT(5) - 1) << 27)
56*9d26506aSNeil Armstrong #define USER1_BN_UC_DOUT_SHIFT 17
57*9d26506aSNeil Armstrong #define USER1_BN_UC_DOUT_MASK (0xff << 16)
58*9d26506aSNeil Armstrong #define USER1_BN_UC_DIN_SHIFT 8
59*9d26506aSNeil Armstrong #define USER1_BN_UC_DIN_MASK (0xff << 8)
60*9d26506aSNeil Armstrong #define USER4_CS_POL_HIGH BIT(23)
61*9d26506aSNeil Armstrong #define USER4_IDLE_CLK_HIGH BIT(29)
62*9d26506aSNeil Armstrong #define USER4_CS_ACT BIT(30)
63*9d26506aSNeil Armstrong #define SLAVE_TRST_DONE BIT(4)
64*9d26506aSNeil Armstrong #define SLAVE_OP_MODE BIT(30)
65*9d26506aSNeil Armstrong #define SLAVE_SW_RST BIT(31)
66*9d26506aSNeil Armstrong
67*9d26506aSNeil Armstrong #define SPIFC_BUFFER_SIZE 64
68*9d26506aSNeil Armstrong
69*9d26506aSNeil Armstrong struct meson_spifc_priv {
70*9d26506aSNeil Armstrong struct regmap *regmap;
71*9d26506aSNeil Armstrong struct clk clk;
72*9d26506aSNeil Armstrong };
73*9d26506aSNeil Armstrong
74*9d26506aSNeil Armstrong /**
75*9d26506aSNeil Armstrong * meson_spifc_drain_buffer() - copy data from device buffer to memory
76*9d26506aSNeil Armstrong * @spifc: the Meson SPI device
77*9d26506aSNeil Armstrong * @buf: the destination buffer
78*9d26506aSNeil Armstrong * @len: number of bytes to copy
79*9d26506aSNeil Armstrong */
meson_spifc_drain_buffer(struct meson_spifc_priv * spifc,u8 * buf,int len)80*9d26506aSNeil Armstrong static void meson_spifc_drain_buffer(struct meson_spifc_priv *spifc,
81*9d26506aSNeil Armstrong u8 *buf, int len)
82*9d26506aSNeil Armstrong {
83*9d26506aSNeil Armstrong u32 data;
84*9d26506aSNeil Armstrong int i = 0;
85*9d26506aSNeil Armstrong
86*9d26506aSNeil Armstrong while (i < len) {
87*9d26506aSNeil Armstrong regmap_read(spifc->regmap, REG_C0 + i, &data);
88*9d26506aSNeil Armstrong
89*9d26506aSNeil Armstrong if (len - i >= 4) {
90*9d26506aSNeil Armstrong *((u32 *)buf) = data;
91*9d26506aSNeil Armstrong buf += 4;
92*9d26506aSNeil Armstrong } else {
93*9d26506aSNeil Armstrong memcpy(buf, &data, len - i);
94*9d26506aSNeil Armstrong break;
95*9d26506aSNeil Armstrong }
96*9d26506aSNeil Armstrong i += 4;
97*9d26506aSNeil Armstrong }
98*9d26506aSNeil Armstrong }
99*9d26506aSNeil Armstrong
100*9d26506aSNeil Armstrong /**
101*9d26506aSNeil Armstrong * meson_spifc_fill_buffer() - copy data from memory to device buffer
102*9d26506aSNeil Armstrong * @spifc: the Meson SPI device
103*9d26506aSNeil Armstrong * @buf: the source buffer
104*9d26506aSNeil Armstrong * @len: number of bytes to copy
105*9d26506aSNeil Armstrong */
meson_spifc_fill_buffer(struct meson_spifc_priv * spifc,const u8 * buf,int len)106*9d26506aSNeil Armstrong static void meson_spifc_fill_buffer(struct meson_spifc_priv *spifc,
107*9d26506aSNeil Armstrong const u8 *buf, int len)
108*9d26506aSNeil Armstrong {
109*9d26506aSNeil Armstrong u32 data = 0;
110*9d26506aSNeil Armstrong int i = 0;
111*9d26506aSNeil Armstrong
112*9d26506aSNeil Armstrong while (i < len) {
113*9d26506aSNeil Armstrong if (len - i >= 4)
114*9d26506aSNeil Armstrong data = *(u32 *)buf;
115*9d26506aSNeil Armstrong else
116*9d26506aSNeil Armstrong memcpy(&data, buf, len - i);
117*9d26506aSNeil Armstrong
118*9d26506aSNeil Armstrong regmap_write(spifc->regmap, REG_C0 + i, data);
119*9d26506aSNeil Armstrong
120*9d26506aSNeil Armstrong buf += 4;
121*9d26506aSNeil Armstrong i += 4;
122*9d26506aSNeil Armstrong }
123*9d26506aSNeil Armstrong }
124*9d26506aSNeil Armstrong
125*9d26506aSNeil Armstrong /**
126*9d26506aSNeil Armstrong * meson_spifc_txrx() - transfer a chunk of data
127*9d26506aSNeil Armstrong * @spifc: the Meson SPI device
128*9d26506aSNeil Armstrong * @dout: data buffer for TX
129*9d26506aSNeil Armstrong * @din: data buffer for RX
130*9d26506aSNeil Armstrong * @offset: offset of the data to transfer
131*9d26506aSNeil Armstrong * @len: length of the data to transfer
132*9d26506aSNeil Armstrong * @last_xfer: whether this is the last transfer of the message
133*9d26506aSNeil Armstrong * @last_chunk: whether this is the last chunk of the transfer
134*9d26506aSNeil Armstrong * Return: 0 on success, a negative value on error
135*9d26506aSNeil Armstrong */
meson_spifc_txrx(struct meson_spifc_priv * spifc,const u8 * dout,u8 * din,int offset,int len,bool last_xfer,bool last_chunk)136*9d26506aSNeil Armstrong static int meson_spifc_txrx(struct meson_spifc_priv *spifc,
137*9d26506aSNeil Armstrong const u8 *dout, u8 *din, int offset,
138*9d26506aSNeil Armstrong int len, bool last_xfer, bool last_chunk)
139*9d26506aSNeil Armstrong {
140*9d26506aSNeil Armstrong bool keep_cs = true;
141*9d26506aSNeil Armstrong u32 data;
142*9d26506aSNeil Armstrong int ret;
143*9d26506aSNeil Armstrong
144*9d26506aSNeil Armstrong if (dout)
145*9d26506aSNeil Armstrong meson_spifc_fill_buffer(spifc, dout + offset, len);
146*9d26506aSNeil Armstrong
147*9d26506aSNeil Armstrong /* enable DOUT stage */
148*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER, USER_UC_MASK,
149*9d26506aSNeil Armstrong USER_UC_DOUT_SEL);
150*9d26506aSNeil Armstrong regmap_write(spifc->regmap, REG_USER1,
151*9d26506aSNeil Armstrong (8 * len - 1) << USER1_BN_UC_DOUT_SHIFT);
152*9d26506aSNeil Armstrong
153*9d26506aSNeil Armstrong /* enable data input during DOUT */
154*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER, USER_DIN_EN_MS,
155*9d26506aSNeil Armstrong USER_DIN_EN_MS);
156*9d26506aSNeil Armstrong
157*9d26506aSNeil Armstrong if (last_chunk && last_xfer)
158*9d26506aSNeil Armstrong keep_cs = false;
159*9d26506aSNeil Armstrong
160*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_ACT,
161*9d26506aSNeil Armstrong keep_cs ? USER4_CS_ACT : 0);
162*9d26506aSNeil Armstrong
163*9d26506aSNeil Armstrong /* clear transition done bit */
164*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_TRST_DONE, 0);
165*9d26506aSNeil Armstrong /* start transfer */
166*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_CMD, CMD_USER, CMD_USER);
167*9d26506aSNeil Armstrong
168*9d26506aSNeil Armstrong /* wait for the current operation to terminate */
169*9d26506aSNeil Armstrong ret = regmap_read_poll_timeout(spifc->regmap, REG_SLAVE, data,
170*9d26506aSNeil Armstrong (data & SLAVE_TRST_DONE),
171*9d26506aSNeil Armstrong 0, 5 * CONFIG_SYS_HZ);
172*9d26506aSNeil Armstrong
173*9d26506aSNeil Armstrong if (!ret && din)
174*9d26506aSNeil Armstrong meson_spifc_drain_buffer(spifc, din + offset, len);
175*9d26506aSNeil Armstrong
176*9d26506aSNeil Armstrong return ret;
177*9d26506aSNeil Armstrong }
178*9d26506aSNeil Armstrong
179*9d26506aSNeil Armstrong /**
180*9d26506aSNeil Armstrong * meson_spifc_xfer() - perform a single transfer
181*9d26506aSNeil Armstrong * @dev: the SPI controller device
182*9d26506aSNeil Armstrong * @bitlen: length of the transfer
183*9d26506aSNeil Armstrong * @dout: data buffer for TX
184*9d26506aSNeil Armstrong * @din: data buffer for RX
185*9d26506aSNeil Armstrong * @flags: transfer flags
186*9d26506aSNeil Armstrong * Return: 0 on success, a negative value on error
187*9d26506aSNeil Armstrong */
meson_spifc_xfer(struct udevice * slave,unsigned int bitlen,const void * dout,void * din,unsigned long flags)188*9d26506aSNeil Armstrong static int meson_spifc_xfer(struct udevice *slave, unsigned int bitlen,
189*9d26506aSNeil Armstrong const void *dout, void *din, unsigned long flags)
190*9d26506aSNeil Armstrong {
191*9d26506aSNeil Armstrong struct meson_spifc_priv *spifc = dev_get_priv(slave->parent);
192*9d26506aSNeil Armstrong int blen = bitlen / 8;
193*9d26506aSNeil Armstrong int len, done = 0, ret = 0;
194*9d26506aSNeil Armstrong
195*9d26506aSNeil Armstrong if (bitlen % 8)
196*9d26506aSNeil Armstrong return -EINVAL;
197*9d26506aSNeil Armstrong
198*9d26506aSNeil Armstrong debug("xfer len %d (%d) dout %p din %p\n", bitlen, blen, dout, din);
199*9d26506aSNeil Armstrong
200*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, 0);
201*9d26506aSNeil Armstrong
202*9d26506aSNeil Armstrong while (done < blen && !ret) {
203*9d26506aSNeil Armstrong len = min_t(int, blen - done, SPIFC_BUFFER_SIZE);
204*9d26506aSNeil Armstrong ret = meson_spifc_txrx(spifc, dout, din, done, len,
205*9d26506aSNeil Armstrong flags & SPI_XFER_END,
206*9d26506aSNeil Armstrong done + len >= blen);
207*9d26506aSNeil Armstrong done += len;
208*9d26506aSNeil Armstrong }
209*9d26506aSNeil Armstrong
210*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB,
211*9d26506aSNeil Armstrong CTRL_ENABLE_AHB);
212*9d26506aSNeil Armstrong
213*9d26506aSNeil Armstrong return ret;
214*9d26506aSNeil Armstrong }
215*9d26506aSNeil Armstrong
216*9d26506aSNeil Armstrong /**
217*9d26506aSNeil Armstrong * meson_spifc_set_speed() - program the clock divider
218*9d26506aSNeil Armstrong * @dev: the SPI controller device
219*9d26506aSNeil Armstrong * @speed: desired speed in Hz
220*9d26506aSNeil Armstrong */
meson_spifc_set_speed(struct udevice * dev,uint speed)221*9d26506aSNeil Armstrong static int meson_spifc_set_speed(struct udevice *dev, uint speed)
222*9d26506aSNeil Armstrong {
223*9d26506aSNeil Armstrong struct meson_spifc_priv *spifc = dev_get_priv(dev);
224*9d26506aSNeil Armstrong unsigned long parent, value;
225*9d26506aSNeil Armstrong int n;
226*9d26506aSNeil Armstrong
227*9d26506aSNeil Armstrong parent = clk_get_rate(&spifc->clk);
228*9d26506aSNeil Armstrong n = max_t(int, parent / speed - 1, 1);
229*9d26506aSNeil Armstrong
230*9d26506aSNeil Armstrong debug("parent %lu, speed %u, n %d\n", parent, speed, n);
231*9d26506aSNeil Armstrong
232*9d26506aSNeil Armstrong value = (n << CLOCK_DIV_SHIFT) & CLOCK_DIV_MASK;
233*9d26506aSNeil Armstrong value |= (n << CLOCK_CNT_LOW_SHIFT) & CLOCK_CNT_LOW_MASK;
234*9d26506aSNeil Armstrong value |= (((n + 1) / 2 - 1) << CLOCK_CNT_HIGH_SHIFT) &
235*9d26506aSNeil Armstrong CLOCK_CNT_HIGH_MASK;
236*9d26506aSNeil Armstrong
237*9d26506aSNeil Armstrong regmap_write(spifc->regmap, REG_CLOCK, value);
238*9d26506aSNeil Armstrong
239*9d26506aSNeil Armstrong return 0;
240*9d26506aSNeil Armstrong }
241*9d26506aSNeil Armstrong
242*9d26506aSNeil Armstrong /**
243*9d26506aSNeil Armstrong * meson_spifc_set_mode() - setups the SPI bus mode
244*9d26506aSNeil Armstrong * @dev: the SPI controller device
245*9d26506aSNeil Armstrong * @mode: desired mode bitfield
246*9d26506aSNeil Armstrong * Return: 0 on success, -ENODEV on error
247*9d26506aSNeil Armstrong */
meson_spifc_set_mode(struct udevice * dev,uint mode)248*9d26506aSNeil Armstrong static int meson_spifc_set_mode(struct udevice *dev, uint mode)
249*9d26506aSNeil Armstrong {
250*9d26506aSNeil Armstrong struct meson_spifc_priv *spifc = dev_get_priv(dev);
251*9d26506aSNeil Armstrong
252*9d26506aSNeil Armstrong if (mode & (SPI_CPHA | SPI_RX_QUAD | SPI_RX_DUAL |
253*9d26506aSNeil Armstrong SPI_TX_QUAD | SPI_TX_DUAL))
254*9d26506aSNeil Armstrong return -ENODEV;
255*9d26506aSNeil Armstrong
256*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER, USER_CLK_NOT_INV,
257*9d26506aSNeil Armstrong mode & SPI_CPOL ? USER_CLK_NOT_INV : 0);
258*9d26506aSNeil Armstrong
259*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_POL_HIGH,
260*9d26506aSNeil Armstrong mode & SPI_CS_HIGH ? USER4_CS_POL_HIGH : 0);
261*9d26506aSNeil Armstrong
262*9d26506aSNeil Armstrong return 0;
263*9d26506aSNeil Armstrong }
264*9d26506aSNeil Armstrong
265*9d26506aSNeil Armstrong /**
266*9d26506aSNeil Armstrong * meson_spifc_hw_init() - reset and initialize the SPI controller
267*9d26506aSNeil Armstrong * @spifc: the Meson SPI device
268*9d26506aSNeil Armstrong */
meson_spifc_hw_init(struct meson_spifc_priv * spifc)269*9d26506aSNeil Armstrong static void meson_spifc_hw_init(struct meson_spifc_priv *spifc)
270*9d26506aSNeil Armstrong {
271*9d26506aSNeil Armstrong /* reset device */
272*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_SW_RST,
273*9d26506aSNeil Armstrong SLAVE_SW_RST);
274*9d26506aSNeil Armstrong /* disable compatible mode */
275*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_USER, USER_CMP_MODE, 0);
276*9d26506aSNeil Armstrong /* set master mode */
277*9d26506aSNeil Armstrong regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_OP_MODE, 0);
278*9d26506aSNeil Armstrong }
279*9d26506aSNeil Armstrong
280*9d26506aSNeil Armstrong static const struct dm_spi_ops meson_spifc_ops = {
281*9d26506aSNeil Armstrong .xfer = meson_spifc_xfer,
282*9d26506aSNeil Armstrong .set_speed = meson_spifc_set_speed,
283*9d26506aSNeil Armstrong .set_mode = meson_spifc_set_mode,
284*9d26506aSNeil Armstrong };
285*9d26506aSNeil Armstrong
meson_spifc_probe(struct udevice * dev)286*9d26506aSNeil Armstrong static int meson_spifc_probe(struct udevice *dev)
287*9d26506aSNeil Armstrong {
288*9d26506aSNeil Armstrong struct meson_spifc_priv *priv = dev_get_priv(dev);
289*9d26506aSNeil Armstrong int ret;
290*9d26506aSNeil Armstrong
291*9d26506aSNeil Armstrong ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
292*9d26506aSNeil Armstrong if (ret)
293*9d26506aSNeil Armstrong return ret;
294*9d26506aSNeil Armstrong
295*9d26506aSNeil Armstrong ret = clk_get_by_index(dev, 0, &priv->clk);
296*9d26506aSNeil Armstrong if (ret)
297*9d26506aSNeil Armstrong return ret;
298*9d26506aSNeil Armstrong
299*9d26506aSNeil Armstrong ret = clk_enable(&priv->clk);
300*9d26506aSNeil Armstrong if (ret)
301*9d26506aSNeil Armstrong return ret;
302*9d26506aSNeil Armstrong
303*9d26506aSNeil Armstrong meson_spifc_hw_init(priv);
304*9d26506aSNeil Armstrong
305*9d26506aSNeil Armstrong return 0;
306*9d26506aSNeil Armstrong }
307*9d26506aSNeil Armstrong
308*9d26506aSNeil Armstrong static const struct udevice_id meson_spifc_ids[] = {
309*9d26506aSNeil Armstrong { .compatible = "amlogic,meson-gxbb-spifc", },
310*9d26506aSNeil Armstrong { }
311*9d26506aSNeil Armstrong };
312*9d26506aSNeil Armstrong
313*9d26506aSNeil Armstrong U_BOOT_DRIVER(meson_spifc) = {
314*9d26506aSNeil Armstrong .name = "meson_spifc",
315*9d26506aSNeil Armstrong .id = UCLASS_SPI,
316*9d26506aSNeil Armstrong .of_match = meson_spifc_ids,
317*9d26506aSNeil Armstrong .ops = &meson_spifc_ops,
318*9d26506aSNeil Armstrong .probe = meson_spifc_probe,
319*9d26506aSNeil Armstrong .priv_auto_alloc_size = sizeof(struct meson_spifc_priv),
320*9d26506aSNeil Armstrong };
321