blob: 2178534baf0e2609041b46aee230b438ebe1a85d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
rick67dd2332017-08-28 15:08:01 +08002/*
Rick Chenaa274d12017-11-23 14:17:35 +08003 * Andestech ATCSPI200 SPI controller driver.
rick67dd2332017-08-28 15:08:01 +08004 *
5 * Copyright 2017 Andes Technology, Inc.
6 * Author: Rick Chen (rick@andestech.com)
rick67dd2332017-08-28 15:08:01 +08007 */
8
Jagan Teki35153dd2019-05-08 19:42:16 +05309#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
rick67dd2332017-08-28 15:08:01 +080011#include <malloc.h>
12#include <spi.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
rick67dd2332017-08-28 15:08:01 +080014#include <asm/io.h>
15#include <dm.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19#define MAX_TRANSFER_LEN 512
20#define CHUNK_SIZE 1
21#define SPI_TIMEOUT 0x100000
22#define SPI0_BUS 0
23#define SPI1_BUS 1
24#define SPI0_BASE 0xf0b00000
25#define SPI1_BASE 0xf0f00000
26#define NSPI_MAX_CS_NUM 1
27
Rick Chenaa274d12017-11-23 14:17:35 +080028struct atcspi200_spi_regs {
rick67dd2332017-08-28 15:08:01 +080029 u32 rev;
30 u32 reserve1[3];
31 u32 format; /* 0x10 */
32#define DATA_LENGTH(x) ((x-1)<<8)
33 u32 pio;
34 u32 reserve2[2];
35 u32 tctrl; /* 0x20 */
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)
50#define RCNT_OFFSET 0
51#define RCNT_MASK (0x1FF<<RCNT_OFFSET)
52 u32 cmd;
53 u32 addr;
54 u32 data;
55 u32 ctrl; /* 0x30 */
56#define TXFTH_OFFSET 16
57#define RXFTH_OFFSET 8
58#define TXDMAEN (1<<4)
59#define RXDMAEN (1<<3)
60#define TXFRST (1<<2)
61#define RXFRST (1<<1)
62#define SPIRST (1<<0)
63 u32 status;
64#define TXFFL (1<<23)
65#define TXEPTY (1<<22)
66#define TXFVE_MASK (0x1F<<16)
67#define RXFEM (1<<14)
68#define RXFVE_OFFSET (8)
69#define RXFVE_MASK (0x1F<<RXFVE_OFFSET)
70#define SPIBSY (1<<0)
71 u32 inten;
72 u32 intsta;
73 u32 timing; /* 0x40 */
74#define SCLK_DIV_MASK 0xFF
75};
76
77struct nds_spi_slave {
Rick Chenaa274d12017-11-23 14:17:35 +080078 volatile struct atcspi200_spi_regs *regs;
rick67dd2332017-08-28 15:08:01 +080079 int to;
80 unsigned int freq;
81 ulong clock;
82 unsigned int mode;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020083 u8 num_cs;
rick67dd2332017-08-28 15:08:01 +080084 unsigned int mtiming;
85 size_t cmd_len;
86 u8 cmd_buf[16];
87 size_t data_len;
88 size_t tran_len;
89 u8 *din;
90 u8 *dout;
91 unsigned int max_transfer_length;
92};
93
Rick Chenaa274d12017-11-23 14:17:35 +080094static int __atcspi200_spi_set_speed(struct nds_spi_slave *ns)
rick67dd2332017-08-28 15:08:01 +080095{
96 u32 tm;
97 u8 div;
98 tm = ns->regs->timing;
99 tm &= ~SCLK_DIV_MASK;
100
101 if(ns->freq >= ns->clock)
102 div =0xff;
103 else{
104 for (div = 0; div < 0xff; div++) {
105 if (ns->freq >= ns->clock / (2 * (div + 1)))
106 break;
107 }
108 }
109
110 tm |= div;
111 ns->regs->timing = tm;
112
113 return 0;
114
115}
116
Rick Chenaa274d12017-11-23 14:17:35 +0800117static int __atcspi200_spi_claim_bus(struct nds_spi_slave *ns)
rick67dd2332017-08-28 15:08:01 +0800118{
119 unsigned int format=0;
120 ns->regs->ctrl |= (TXFRST|RXFRST|SPIRST);
121 while((ns->regs->ctrl &(TXFRST|RXFRST|SPIRST))&&(ns->to--))
122 if(!ns->to)
123 return -EINVAL;
124
125 ns->cmd_len = 0;
126 format = ns->mode|DATA_LENGTH(8);
127 ns->regs->format = format;
Rick Chenaa274d12017-11-23 14:17:35 +0800128 __atcspi200_spi_set_speed(ns);
rick67dd2332017-08-28 15:08:01 +0800129
130 return 0;
131}
132
Rick Chenaa274d12017-11-23 14:17:35 +0800133static int __atcspi200_spi_release_bus(struct nds_spi_slave *ns)
rick67dd2332017-08-28 15:08:01 +0800134{
135 /* do nothing */
136 return 0;
137}
138
Rick Chenaa274d12017-11-23 14:17:35 +0800139static int __atcspi200_spi_start(struct nds_spi_slave *ns)
rick67dd2332017-08-28 15:08:01 +0800140{
141 int i,olen=0;
142 int tc = ns->regs->tctrl;
143
144 tc &= ~(WCNT_MASK|RCNT_MASK|TRAMODE_MASK);
145 if ((ns->din)&&(ns->cmd_len))
146 tc |= TRAMODE_WR;
147 else if (ns->din)
148 tc |= TRAMODE_RO;
149 else
150 tc |= TRAMODE_WO;
151
152 if(ns->dout)
153 olen = ns->tran_len;
154 tc |= (ns->cmd_len+olen-1) << WCNT_OFFSET;
155
156 if(ns->din)
157 tc |= (ns->tran_len-1) << RCNT_OFFSET;
158
159 ns->regs->tctrl = tc;
160 ns->regs->cmd = 1;
161
162 for (i=0;i<ns->cmd_len;i++)
163 ns->regs->data = ns->cmd_buf[i];
164
165 return 0;
166}
167
Rick Chenaa274d12017-11-23 14:17:35 +0800168static int __atcspi200_spi_stop(struct nds_spi_slave *ns)
rick67dd2332017-08-28 15:08:01 +0800169{
170 ns->regs->timing = ns->mtiming;
171 while ((ns->regs->status & SPIBSY)&&(ns->to--))
172 if (!ns->to)
173 return -EINVAL;
174
175 return 0;
176}
177
178static void __nspi_espi_tx(struct nds_spi_slave *ns, const void *dout)
179{
180 ns->regs->data = *(u8 *)dout;
181}
182
183static int __nspi_espi_rx(struct nds_spi_slave *ns, void *din, unsigned int bytes)
184{
185 *(u8 *)din = ns->regs->data;
186 return bytes;
187}
188
Rick Chenaa274d12017-11-23 14:17:35 +0800189static int __atcspi200_spi_xfer(struct nds_spi_slave *ns,
rick67dd2332017-08-28 15:08:01 +0800190 unsigned int bitlen, const void *data_out, void *data_in,
191 unsigned long flags)
192{
193 unsigned int event, rx_bytes;
194 const void *dout = NULL;
195 void *din = NULL;
196 int num_blks, num_chunks, max_tran_len, tran_len;
197 int num_bytes;
198 u8 *cmd_buf = ns->cmd_buf;
199 size_t cmd_len = ns->cmd_len;
Rick Chenb41aeec2018-05-29 10:40:03 +0800200 unsigned long data_len = bitlen / 8;
rick67dd2332017-08-28 15:08:01 +0800201 int rf_cnt;
Dylan Jhongb40efd32021-04-01 16:48:51 +0800202 int ret = 0, timeout = 0;
rick67dd2332017-08-28 15:08:01 +0800203
204 max_tran_len = ns->max_transfer_length;
205 switch (flags) {
206 case SPI_XFER_BEGIN:
207 cmd_len = ns->cmd_len = data_len;
208 memcpy(cmd_buf, data_out, cmd_len);
209 return 0;
210
211 case 0:
212 case SPI_XFER_END:
213 if (bitlen == 0) {
214 return 0;
215 }
216 ns->data_len = data_len;
217 ns->din = (u8 *)data_in;
218 ns->dout = (u8 *)data_out;
219 break;
220
221 case SPI_XFER_BEGIN | SPI_XFER_END:
222 ns->data_len = 0;
223 ns->din = 0;
224 ns->dout = 0;
225 cmd_len = ns->cmd_len = data_len;
226 memcpy(cmd_buf, data_out, cmd_len);
227 data_out = 0;
228 data_len = 0;
Rick Chenaa274d12017-11-23 14:17:35 +0800229 __atcspi200_spi_start(ns);
rick67dd2332017-08-28 15:08:01 +0800230 break;
231 }
Heinrich Schuchardtab1643b2018-03-18 12:41:43 +0100232 if (data_out)
Tom Rinibc84c442018-05-30 14:51:37 -0400233 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) data_len %lu\n",
Heinrich Schuchardtab1643b2018-03-18 12:41:43 +0100234 *(uint *)data_out, data_out, *(uint *)data_in,
235 data_in, data_len);
rick67dd2332017-08-28 15:08:01 +0800236 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
237 din = data_in;
238 dout = data_out;
239 while (num_chunks--) {
Rick Chenb41aeec2018-05-29 10:40:03 +0800240 tran_len = min((size_t)data_len, (size_t)max_tran_len);
rick67dd2332017-08-28 15:08:01 +0800241 ns->tran_len = tran_len;
242 num_blks = DIV_ROUND_UP(tran_len , CHUNK_SIZE);
243 num_bytes = (tran_len) % CHUNK_SIZE;
Dylan Jhongb40efd32021-04-01 16:48:51 +0800244 timeout = SPI_TIMEOUT;
rick67dd2332017-08-28 15:08:01 +0800245 if(num_bytes == 0)
246 num_bytes = CHUNK_SIZE;
Rick Chenaa274d12017-11-23 14:17:35 +0800247 __atcspi200_spi_start(ns);
rick67dd2332017-08-28 15:08:01 +0800248
Dylan Jhongb40efd32021-04-01 16:48:51 +0800249 while (num_blks && (timeout--)) {
rick67dd2332017-08-28 15:08:01 +0800250 event = in_le32(&ns->regs->status);
251 if ((event & TXEPTY) && (data_out)) {
252 __nspi_espi_tx(ns, dout);
253 num_blks -= CHUNK_SIZE;
254 dout += CHUNK_SIZE;
255 }
256
257 if ((event & RXFVE_MASK) && (data_in)) {
258 rf_cnt = ((event & RXFVE_MASK)>> RXFVE_OFFSET);
259 if (rf_cnt >= CHUNK_SIZE)
260 rx_bytes = CHUNK_SIZE;
261 else if (num_blks == 1 && rf_cnt == num_bytes)
262 rx_bytes = num_bytes;
263 else
264 continue;
265
266 if (__nspi_espi_rx(ns, din, rx_bytes) == rx_bytes) {
267 num_blks -= CHUNK_SIZE;
268 din = (unsigned char *)din + rx_bytes;
269 }
270 }
Dylan Jhongb40efd32021-04-01 16:48:51 +0800271
272 if (!timeout) {
273 debug("spi_xfer: %s() timeout\n", __func__);
274 break;
275 }
rick67dd2332017-08-28 15:08:01 +0800276 }
277
278 data_len -= tran_len;
279 if(data_len)
280 {
281 ns->cmd_buf[1] += ((tran_len>>16)&0xff);
282 ns->cmd_buf[2] += ((tran_len>>8)&0xff);
283 ns->cmd_buf[3] += ((tran_len)&0xff);
284 ns->data_len = data_len;
285 }
Rick Chenaa274d12017-11-23 14:17:35 +0800286 ret = __atcspi200_spi_stop(ns);
rick67dd2332017-08-28 15:08:01 +0800287 }
Rick Chenaa274d12017-11-23 14:17:35 +0800288 ret = __atcspi200_spi_stop(ns);
rick67dd2332017-08-28 15:08:01 +0800289
290 return ret;
291}
292
Rick Chenaa274d12017-11-23 14:17:35 +0800293static int atcspi200_spi_set_speed(struct udevice *bus, uint max_hz)
rick67dd2332017-08-28 15:08:01 +0800294{
295 struct nds_spi_slave *ns = dev_get_priv(bus);
296
297 debug("%s speed %u\n", __func__, max_hz);
298
299 ns->freq = max_hz;
Rick Chenaa274d12017-11-23 14:17:35 +0800300 __atcspi200_spi_set_speed(ns);
rick67dd2332017-08-28 15:08:01 +0800301
302 return 0;
303}
304
Rick Chenaa274d12017-11-23 14:17:35 +0800305static int atcspi200_spi_set_mode(struct udevice *bus, uint mode)
rick67dd2332017-08-28 15:08:01 +0800306{
307 struct nds_spi_slave *ns = dev_get_priv(bus);
308
309 debug("%s mode %u\n", __func__, mode);
310 ns->mode = mode;
311
312 return 0;
313}
314
Rick Chenaa274d12017-11-23 14:17:35 +0800315static int atcspi200_spi_claim_bus(struct udevice *dev)
rick67dd2332017-08-28 15:08:01 +0800316{
Simon Glassb75b15b2020-12-03 16:55:23 -0700317 struct dm_spi_slave_plat *slave_plat =
Simon Glass71fa5b42020-12-03 16:55:18 -0700318 dev_get_parent_plat(dev);
rick67dd2332017-08-28 15:08:01 +0800319 struct udevice *bus = dev->parent;
320 struct nds_spi_slave *ns = dev_get_priv(bus);
321
322 if (slave_plat->cs >= ns->num_cs) {
323 printf("Invalid SPI chipselect\n");
324 return -EINVAL;
325 }
326
Rick Chenaa274d12017-11-23 14:17:35 +0800327 return __atcspi200_spi_claim_bus(ns);
rick67dd2332017-08-28 15:08:01 +0800328}
329
Rick Chenaa274d12017-11-23 14:17:35 +0800330static int atcspi200_spi_release_bus(struct udevice *dev)
rick67dd2332017-08-28 15:08:01 +0800331{
332 struct nds_spi_slave *ns = dev_get_priv(dev->parent);
333
Rick Chenaa274d12017-11-23 14:17:35 +0800334 return __atcspi200_spi_release_bus(ns);
rick67dd2332017-08-28 15:08:01 +0800335}
336
Rick Chenaa274d12017-11-23 14:17:35 +0800337static int atcspi200_spi_xfer(struct udevice *dev, unsigned int bitlen,
rick67dd2332017-08-28 15:08:01 +0800338 const void *dout, void *din,
339 unsigned long flags)
340{
341 struct udevice *bus = dev->parent;
342 struct nds_spi_slave *ns = dev_get_priv(bus);
343
Rick Chenaa274d12017-11-23 14:17:35 +0800344 return __atcspi200_spi_xfer(ns, bitlen, dout, din, flags);
rick67dd2332017-08-28 15:08:01 +0800345}
346
Rick Chenaa274d12017-11-23 14:17:35 +0800347static int atcspi200_spi_get_clk(struct udevice *bus)
rick67dd2332017-08-28 15:08:01 +0800348{
349 struct nds_spi_slave *ns = dev_get_priv(bus);
350 struct clk clk;
351 ulong clk_rate;
352 int ret;
353
354 ret = clk_get_by_index(bus, 0, &clk);
355 if (ret)
356 return -EINVAL;
357
358 clk_rate = clk_get_rate(&clk);
359 if (!clk_rate)
360 return -EINVAL;
361
362 ns->clock = clk_rate;
rick67dd2332017-08-28 15:08:01 +0800363
364 return 0;
365}
366
Rick Chenaa274d12017-11-23 14:17:35 +0800367static int atcspi200_spi_probe(struct udevice *bus)
rick67dd2332017-08-28 15:08:01 +0800368{
369 struct nds_spi_slave *ns = dev_get_priv(bus);
370
371 ns->to = SPI_TIMEOUT;
372 ns->max_transfer_length = MAX_TRANSFER_LEN;
373 ns->mtiming = ns->regs->timing;
Rick Chenaa274d12017-11-23 14:17:35 +0800374 atcspi200_spi_get_clk(bus);
rick67dd2332017-08-28 15:08:01 +0800375
376 return 0;
377}
378
Rick Chenaa274d12017-11-23 14:17:35 +0800379static int atcspi200_ofdata_to_platadata(struct udevice *bus)
rick67dd2332017-08-28 15:08:01 +0800380{
381 struct nds_spi_slave *ns = dev_get_priv(bus);
382 const void *blob = gd->fdt_blob;
383 int node = dev_of_offset(bus);
384
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900385 ns->regs = map_physmem(dev_read_addr(bus),
Rick Chenaa274d12017-11-23 14:17:35 +0800386 sizeof(struct atcspi200_spi_regs),
rick67dd2332017-08-28 15:08:01 +0800387 MAP_NOCACHE);
388 if (!ns->regs) {
389 printf("%s: could not map device address\n", __func__);
390 return -EINVAL;
391 }
392 ns->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
393
394 return 0;
395}
396
Rick Chenaa274d12017-11-23 14:17:35 +0800397static const struct dm_spi_ops atcspi200_spi_ops = {
398 .claim_bus = atcspi200_spi_claim_bus,
399 .release_bus = atcspi200_spi_release_bus,
400 .xfer = atcspi200_spi_xfer,
401 .set_speed = atcspi200_spi_set_speed,
402 .set_mode = atcspi200_spi_set_mode,
rick67dd2332017-08-28 15:08:01 +0800403};
404
Rick Chenaa274d12017-11-23 14:17:35 +0800405static const struct udevice_id atcspi200_spi_ids[] = {
rick67dd2332017-08-28 15:08:01 +0800406 { .compatible = "andestech,atcspi200" },
407 { }
408};
409
Rick Chenaa274d12017-11-23 14:17:35 +0800410U_BOOT_DRIVER(atcspi200_spi) = {
411 .name = "atcspi200_spi",
rick67dd2332017-08-28 15:08:01 +0800412 .id = UCLASS_SPI,
Rick Chenaa274d12017-11-23 14:17:35 +0800413 .of_match = atcspi200_spi_ids,
414 .ops = &atcspi200_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700415 .of_to_plat = atcspi200_ofdata_to_platadata,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700416 .priv_auto = sizeof(struct nds_spi_slave),
Rick Chenaa274d12017-11-23 14:17:35 +0800417 .probe = atcspi200_spi_probe,
rick67dd2332017-08-28 15:08:01 +0800418};