2 * Andestech ATCSPI200 SPI controller driver.
4 * Copyright 2017 Andes Technology, Inc.
5 * Author: Rick Chen (rick@andestech.com)
7 * SPDX-License-Identifier: GPL-2.0+
17 DECLARE_GLOBAL_DATA_PTR;
19 #define MAX_TRANSFER_LEN 512
21 #define SPI_TIMEOUT 0x100000
24 #define SPI0_BASE 0xf0b00000
25 #define SPI1_BASE 0xf0f00000
26 #define NSPI_MAX_CS_NUM 1
28 struct atcspi200_spi_regs {
31 u32 format; /* 0x10 */
32 #define DATA_LENGTH(x) ((x-1)<<8)
36 #define TRAMODE_OFFSET 24
37 #define TRAMODE_MASK (0x0F<<TRAMODE_OFFSET)
38 #define TRAMODE_WR_SYNC (0<<TRAMODE_OFFSET)
39 #define TRAMODE_WO (1<<TRAMODE_OFFSET)
40 #define TRAMODE_RO (2<<TRAMODE_OFFSET)
41 #define TRAMODE_WR (3<<TRAMODE_OFFSET)
42 #define TRAMODE_RW (4<<TRAMODE_OFFSET)
43 #define TRAMODE_WDR (5<<TRAMODE_OFFSET)
44 #define TRAMODE_RDW (6<<TRAMODE_OFFSET)
45 #define TRAMODE_NONE (7<<TRAMODE_OFFSET)
46 #define TRAMODE_DW (8<<TRAMODE_OFFSET)
47 #define TRAMODE_DR (9<<TRAMODE_OFFSET)
48 #define WCNT_OFFSET 12
49 #define WCNT_MASK (0x1FF<<WCNT_OFFSET)
51 #define RCNT_MASK (0x1FF<<RCNT_OFFSET)
56 #define TXFTH_OFFSET 16
57 #define RXFTH_OFFSET 8
58 #define TXDMAEN (1<<4)
59 #define RXDMAEN (1<<3)
65 #define TXEPTY (1<<22)
66 #define TXFVE_MASK (0x1F<<16)
68 #define RXFVE_OFFSET (8)
69 #define RXFVE_MASK (0x1F<<RXFVE_OFFSET)
73 u32 timing; /* 0x40 */
74 #define SCLK_DIV_MASK 0xFF
77 struct nds_spi_slave {
78 volatile struct atcspi200_spi_regs *regs;
91 unsigned int max_transfer_length;
94 static int __atcspi200_spi_set_speed(struct nds_spi_slave *ns)
98 tm = ns->regs->timing;
101 if(ns->freq >= ns->clock)
104 for (div = 0; div < 0xff; div++) {
105 if (ns->freq >= ns->clock / (2 * (div + 1)))
111 ns->regs->timing = tm;
117 static int __atcspi200_spi_claim_bus(struct nds_spi_slave *ns)
119 unsigned int format=0;
120 ns->regs->ctrl |= (TXFRST|RXFRST|SPIRST);
121 while((ns->regs->ctrl &(TXFRST|RXFRST|SPIRST))&&(ns->to--))
126 format = ns->mode|DATA_LENGTH(8);
127 ns->regs->format = format;
128 __atcspi200_spi_set_speed(ns);
133 static int __atcspi200_spi_release_bus(struct nds_spi_slave *ns)
139 static int __atcspi200_spi_start(struct nds_spi_slave *ns)
142 int tc = ns->regs->tctrl;
144 tc &= ~(WCNT_MASK|RCNT_MASK|TRAMODE_MASK);
145 if ((ns->din)&&(ns->cmd_len))
154 tc |= (ns->cmd_len+olen-1) << WCNT_OFFSET;
157 tc |= (ns->tran_len-1) << RCNT_OFFSET;
159 ns->regs->tctrl = tc;
162 for (i=0;i<ns->cmd_len;i++)
163 ns->regs->data = ns->cmd_buf[i];
168 static int __atcspi200_spi_stop(struct nds_spi_slave *ns)
170 ns->regs->timing = ns->mtiming;
171 while ((ns->regs->status & SPIBSY)&&(ns->to--))
178 static void __nspi_espi_tx(struct nds_spi_slave *ns, const void *dout)
180 ns->regs->data = *(u8 *)dout;
183 static int __nspi_espi_rx(struct nds_spi_slave *ns, void *din, unsigned int bytes)
185 *(u8 *)din = ns->regs->data;
190 static int __atcspi200_spi_xfer(struct nds_spi_slave *ns,
191 unsigned int bitlen, const void *data_out, void *data_in,
194 unsigned int event, rx_bytes;
195 const void *dout = NULL;
197 int num_blks, num_chunks, max_tran_len, tran_len;
199 u8 *cmd_buf = ns->cmd_buf;
200 size_t cmd_len = ns->cmd_len;
201 size_t data_len = bitlen / 8;
205 max_tran_len = ns->max_transfer_length;
208 cmd_len = ns->cmd_len = data_len;
209 memcpy(cmd_buf, data_out, cmd_len);
217 ns->data_len = data_len;
218 ns->din = (u8 *)data_in;
219 ns->dout = (u8 *)data_out;
222 case SPI_XFER_BEGIN | SPI_XFER_END:
226 cmd_len = ns->cmd_len = data_len;
227 memcpy(cmd_buf, data_out, cmd_len);
230 __atcspi200_spi_start(ns);
233 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) data_len %u\n",
234 *(uint *)data_out, data_out, *(uint *)data_in, data_in, data_len);
235 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
238 while (num_chunks--) {
239 tran_len = min(data_len, (size_t)max_tran_len);
240 ns->tran_len = tran_len;
241 num_blks = DIV_ROUND_UP(tran_len , CHUNK_SIZE);
242 num_bytes = (tran_len) % CHUNK_SIZE;
244 num_bytes = CHUNK_SIZE;
245 __atcspi200_spi_start(ns);
248 event = in_le32(&ns->regs->status);
249 if ((event & TXEPTY) && (data_out)) {
250 __nspi_espi_tx(ns, dout);
251 num_blks -= CHUNK_SIZE;
255 if ((event & RXFVE_MASK) && (data_in)) {
256 rf_cnt = ((event & RXFVE_MASK)>> RXFVE_OFFSET);
257 if (rf_cnt >= CHUNK_SIZE)
258 rx_bytes = CHUNK_SIZE;
259 else if (num_blks == 1 && rf_cnt == num_bytes)
260 rx_bytes = num_bytes;
264 if (__nspi_espi_rx(ns, din, rx_bytes) == rx_bytes) {
265 num_blks -= CHUNK_SIZE;
266 din = (unsigned char *)din + rx_bytes;
271 data_len -= tran_len;
274 ns->cmd_buf[1] += ((tran_len>>16)&0xff);
275 ns->cmd_buf[2] += ((tran_len>>8)&0xff);
276 ns->cmd_buf[3] += ((tran_len)&0xff);
277 ns->data_len = data_len;
279 ret = __atcspi200_spi_stop(ns);
281 ret = __atcspi200_spi_stop(ns);
286 static int atcspi200_spi_set_speed(struct udevice *bus, uint max_hz)
288 struct nds_spi_slave *ns = dev_get_priv(bus);
290 debug("%s speed %u\n", __func__, max_hz);
293 __atcspi200_spi_set_speed(ns);
298 static int atcspi200_spi_set_mode(struct udevice *bus, uint mode)
300 struct nds_spi_slave *ns = dev_get_priv(bus);
302 debug("%s mode %u\n", __func__, mode);
308 static int atcspi200_spi_claim_bus(struct udevice *dev)
310 struct dm_spi_slave_platdata *slave_plat =
311 dev_get_parent_platdata(dev);
312 struct udevice *bus = dev->parent;
313 struct nds_spi_slave *ns = dev_get_priv(bus);
315 if (slave_plat->cs >= ns->num_cs) {
316 printf("Invalid SPI chipselect\n");
320 return __atcspi200_spi_claim_bus(ns);
323 static int atcspi200_spi_release_bus(struct udevice *dev)
325 struct nds_spi_slave *ns = dev_get_priv(dev->parent);
327 return __atcspi200_spi_release_bus(ns);
330 static int atcspi200_spi_xfer(struct udevice *dev, unsigned int bitlen,
331 const void *dout, void *din,
334 struct udevice *bus = dev->parent;
335 struct nds_spi_slave *ns = dev_get_priv(bus);
337 return __atcspi200_spi_xfer(ns, bitlen, dout, din, flags);
340 static int atcspi200_spi_get_clk(struct udevice *bus)
342 struct nds_spi_slave *ns = dev_get_priv(bus);
347 ret = clk_get_by_index(bus, 0, &clk);
351 clk_rate = clk_get_rate(&clk);
355 ns->clock = clk_rate;
361 static int atcspi200_spi_probe(struct udevice *bus)
363 struct nds_spi_slave *ns = dev_get_priv(bus);
365 ns->to = SPI_TIMEOUT;
366 ns->max_transfer_length = MAX_TRANSFER_LEN;
367 ns->mtiming = ns->regs->timing;
368 atcspi200_spi_get_clk(bus);
373 static int atcspi200_ofdata_to_platadata(struct udevice *bus)
375 struct nds_spi_slave *ns = dev_get_priv(bus);
376 const void *blob = gd->fdt_blob;
377 int node = dev_of_offset(bus);
379 ns->regs = map_physmem(devfdt_get_addr(bus),
380 sizeof(struct atcspi200_spi_regs),
383 printf("%s: could not map device address\n", __func__);
386 ns->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
391 static const struct dm_spi_ops atcspi200_spi_ops = {
392 .claim_bus = atcspi200_spi_claim_bus,
393 .release_bus = atcspi200_spi_release_bus,
394 .xfer = atcspi200_spi_xfer,
395 .set_speed = atcspi200_spi_set_speed,
396 .set_mode = atcspi200_spi_set_mode,
399 static const struct udevice_id atcspi200_spi_ids[] = {
400 { .compatible = "andestech,atcspi200" },
404 U_BOOT_DRIVER(atcspi200_spi) = {
405 .name = "atcspi200_spi",
407 .of_match = atcspi200_spi_ids,
408 .ops = &atcspi200_spi_ops,
409 .ofdata_to_platdata = atcspi200_ofdata_to_platadata,
410 .priv_auto_alloc_size = sizeof(struct nds_spi_slave),
411 .probe = atcspi200_spi_probe,