1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2012 Thomas Langer <thomas.langer@lantiq.com>
7 #include <linux/module.h>
8 #include <linux/device.h>
9 #include <linux/platform_device.h>
10 #include <linux/spi/spi.h>
11 #include <linux/delay.h>
13 #include <linux/of_platform.h>
15 #include <lantiq_soc.h>
17 #define DRV_NAME "sflash-falcon"
19 #define FALCON_SPI_XFER_BEGIN (1 << 0)
20 #define FALCON_SPI_XFER_END (1 << 1)
22 /* Bus Read Configuration Register0 */
23 #define BUSRCON0 0x00000010
24 /* Bus Write Configuration Register0 */
25 #define BUSWCON0 0x00000018
26 /* Serial Flash Configuration Register */
27 #define SFCON 0x00000080
28 /* Serial Flash Time Register */
29 #define SFTIME 0x00000084
30 /* Serial Flash Status Register */
31 #define SFSTAT 0x00000088
32 /* Serial Flash Command Register */
33 #define SFCMD 0x0000008C
34 /* Serial Flash Address Register */
35 #define SFADDR 0x00000090
36 /* Serial Flash Data Register */
37 #define SFDATA 0x00000094
38 /* Serial Flash I/O Control Register */
39 #define SFIO 0x00000098
40 /* EBU Clock Control Register */
41 #define EBUCC 0x000000C4
43 /* Dummy Phase Length */
44 #define SFCMD_DUMLEN_OFFSET 16
45 #define SFCMD_DUMLEN_MASK 0x000F0000
47 #define SFCMD_CS_OFFSET 24
48 #define SFCMD_CS_MASK 0x07000000
50 #define SFCMD_ALEN_OFFSET 20
51 #define SFCMD_ALEN_MASK 0x00700000
52 /* SCK Rise-edge Position */
53 #define SFTIME_SCKR_POS_OFFSET 8
54 #define SFTIME_SCKR_POS_MASK 0x00000F00
56 #define SFTIME_SCK_PER_OFFSET 0
57 #define SFTIME_SCK_PER_MASK 0x0000000F
58 /* SCK Fall-edge Position */
59 #define SFTIME_SCKF_POS_OFFSET 12
60 #define SFTIME_SCKF_POS_MASK 0x0000F000
62 #define SFCON_DEV_SIZE_A23_0 0x03000000
63 #define SFCON_DEV_SIZE_MASK 0x0F000000
64 /* Read Data Position */
65 #define SFTIME_RD_POS_MASK 0x000F0000
67 #define SFIO_UNUSED_WD_MASK 0x0000000F
68 /* Command Opcode mask */
69 #define SFCMD_OPC_MASK 0x000000FF
70 /* dlen bytes of data to write */
71 #define SFCMD_DIR_WRITE 0x00000100
72 /* Data Length offset */
73 #define SFCMD_DLEN_OFFSET 9
75 #define SFSTAT_CMD_ERR 0x20000000
76 /* Access Command Pending */
77 #define SFSTAT_CMD_PEND 0x00400000
78 /* Frequency set to 100MHz. */
79 #define EBUCC_EBUDIV_SELF100 0x00000001
81 #define BUSRCON0_AGEN_SERIAL_FLASH 0xF0000000
82 /* 8-bit multiplexed */
83 #define BUSRCON0_PORTW_8_BIT_MUX 0x00000000
85 #define BUSWCON0_AGEN_SERIAL_FLASH 0xF0000000
86 /* Chip Select after opcode */
87 #define SFCMD_KEEP_CS_KEEP_SELECTED 0x00008000
89 #define CLOCK_100M 100000000
90 #define CLOCK_50M 50000000
92 struct falcon_sflash
{
93 u32 sfcmd
; /* for caching of opcode, direction, ... */
94 struct spi_master
*master
;
97 int falcon_sflash_xfer(struct spi_device
*spi
, struct spi_transfer
*t
,
100 struct device
*dev
= &spi
->dev
;
101 struct falcon_sflash
*priv
= spi_master_get_devdata(spi
->master
);
102 const u8
*txp
= t
->tx_buf
;
104 unsigned int bytelen
= ((8 * t
->len
+ 7) / 8);
105 unsigned int len
, alen
, dumlen
;
109 state_command_prepare
,
114 } state
= state_init
;
118 case state_init
: /* detect phase of upper layer sequence */
120 /* initial write ? */
121 if (flags
& FALCON_SPI_XFER_BEGIN
) {
124 "BEGIN without tx data!\n");
128 * Prepare the parts of the sfcmd register,
129 * which should not change during a sequence!
130 * Only exception are the length fields,
131 * especially alen and dumlen.
134 priv
->sfcmd
= ((spi
->chip_select
137 priv
->sfcmd
|= SFCMD_KEEP_CS_KEEP_SELECTED
;
144 * maybe address and/or dummy
146 state
= state_command_prepare
;
149 dev_dbg(dev
, "write cmd %02X\n",
150 priv
->sfcmd
& SFCMD_OPC_MASK
);
153 /* continued write ? */
154 if (txp
&& bytelen
) {
159 if (rxp
&& bytelen
) {
163 /* end of sequence? */
164 if (flags
& FALCON_SPI_XFER_END
)
165 state
= state_disable_cs
;
170 /* collect tx data for address and dummy phase */
171 case state_command_prepare
:
173 /* txp is valid, already checked */
177 while (bytelen
> 0) {
179 val
= (val
<< 8) | (*txp
++);
181 } else if ((dumlen
< 15) && (*txp
== 0)) {
183 * assume dummy bytes are set to 0
193 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
| SFCMD_DUMLEN_MASK
);
194 priv
->sfcmd
|= (alen
<< SFCMD_ALEN_OFFSET
) |
195 (dumlen
<< SFCMD_DUMLEN_OFFSET
);
197 ltq_ebu_w32(val
, SFADDR
);
199 dev_dbg(dev
, "wr %02X, alen=%d (addr=%06X) dlen=%d\n",
200 priv
->sfcmd
& SFCMD_OPC_MASK
,
204 /* continue with write */
206 } else if (flags
& FALCON_SPI_XFER_END
) {
207 /* end of sequence? */
208 state
= state_disable_cs
;
211 * go to end and expect another
212 * call (read or write)
220 /* txp still valid */
221 priv
->sfcmd
|= SFCMD_DIR_WRITE
;
226 val
|= (*txp
++) << (8 * len
++);
227 if ((flags
& FALCON_SPI_XFER_END
)
230 ~SFCMD_KEEP_CS_KEEP_SELECTED
;
232 if ((len
== 4) || (bytelen
== 0)) {
233 ltq_ebu_w32(val
, SFDATA
);
234 ltq_ebu_w32(priv
->sfcmd
235 | (len
<<SFCMD_DLEN_OFFSET
),
239 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
240 | SFCMD_DUMLEN_MASK
);
249 priv
->sfcmd
&= ~SFCMD_DIR_WRITE
;
251 if ((flags
& FALCON_SPI_XFER_END
)
254 ~SFCMD_KEEP_CS_KEEP_SELECTED
;
256 len
= (bytelen
> 4) ? 4 : bytelen
;
258 ltq_ebu_w32(priv
->sfcmd
259 | (len
<< SFCMD_DLEN_OFFSET
), SFCMD
);
260 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
261 | SFCMD_DUMLEN_MASK
);
263 val
= ltq_ebu_r32(SFSTAT
);
264 if (val
& SFSTAT_CMD_ERR
) {
265 /* reset error status */
266 dev_err(dev
, "SFSTAT: CMD_ERR");
267 dev_err(dev
, " (%x)\n", val
);
268 ltq_ebu_w32(SFSTAT_CMD_ERR
,
272 } while (val
& SFSTAT_CMD_PEND
);
273 val
= ltq_ebu_r32(SFDATA
);
284 case state_disable_cs
:
286 priv
->sfcmd
&= ~SFCMD_KEEP_CS_KEEP_SELECTED
;
287 ltq_ebu_w32(priv
->sfcmd
| (0 << SFCMD_DLEN_OFFSET
),
289 val
= ltq_ebu_r32(SFSTAT
);
290 if (val
& SFSTAT_CMD_ERR
) {
291 /* reset error status */
292 dev_err(dev
, "SFSTAT: CMD_ERR (%x)\n", val
);
293 ltq_ebu_w32(SFSTAT_CMD_ERR
, SFSTAT
);
302 } while (state
!= state_end
);
307 static int falcon_sflash_setup(struct spi_device
*spi
)
312 spin_lock_irqsave(&ebu_lock
, flags
);
314 if (spi
->max_speed_hz
>= CLOCK_100M
) {
315 /* set EBU clock to 100 MHz */
316 ltq_sys1_w32_mask(0, EBUCC_EBUDIV_SELF100
, EBUCC
);
319 /* set EBU clock to 50 MHz */
320 ltq_sys1_w32_mask(EBUCC_EBUDIV_SELF100
, 0, EBUCC
);
322 /* search for suitable divider */
323 for (i
= 1; i
< 7; i
++) {
324 if (CLOCK_50M
/ i
<= spi
->max_speed_hz
)
329 /* setup period of serial clock */
330 ltq_ebu_w32_mask(SFTIME_SCKF_POS_MASK
331 | SFTIME_SCKR_POS_MASK
332 | SFTIME_SCK_PER_MASK
,
333 (i
<< SFTIME_SCKR_POS_OFFSET
)
334 | (i
<< (SFTIME_SCK_PER_OFFSET
+ 1)),
338 * set some bits of unused_wd, to not trigger HOLD/WP
339 * signals on non QUAD flashes
341 ltq_ebu_w32((SFIO_UNUSED_WD_MASK
& (0x8 | 0x4)), SFIO
);
343 ltq_ebu_w32(BUSRCON0_AGEN_SERIAL_FLASH
| BUSRCON0_PORTW_8_BIT_MUX
,
345 ltq_ebu_w32(BUSWCON0_AGEN_SERIAL_FLASH
, BUSWCON0
);
346 /* set address wrap around to maximum for 24-bit addresses */
347 ltq_ebu_w32_mask(SFCON_DEV_SIZE_MASK
, SFCON_DEV_SIZE_A23_0
, SFCON
);
349 spin_unlock_irqrestore(&ebu_lock
, flags
);
354 static int falcon_sflash_xfer_one(struct spi_master
*master
,
355 struct spi_message
*m
)
357 struct falcon_sflash
*priv
= spi_master_get_devdata(master
);
358 struct spi_transfer
*t
;
359 unsigned long spi_flags
;
364 m
->actual_length
= 0;
366 spi_flags
= FALCON_SPI_XFER_BEGIN
;
367 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
368 if (list_is_last(&t
->transfer_list
, &m
->transfers
))
369 spi_flags
|= FALCON_SPI_XFER_END
;
371 spin_lock_irqsave(&ebu_lock
, flags
);
372 ret
= falcon_sflash_xfer(m
->spi
, t
, spi_flags
);
373 spin_unlock_irqrestore(&ebu_lock
, flags
);
378 m
->actual_length
+= t
->len
;
380 WARN_ON(t
->delay_usecs
|| t
->delay
.value
|| t
->cs_change
);
385 spi_finalize_current_message(master
);
390 static int falcon_sflash_probe(struct platform_device
*pdev
)
392 struct falcon_sflash
*priv
;
393 struct spi_master
*master
;
396 master
= spi_alloc_master(&pdev
->dev
, sizeof(*priv
));
400 priv
= spi_master_get_devdata(master
);
401 priv
->master
= master
;
403 master
->mode_bits
= SPI_MODE_3
;
404 master
->flags
= SPI_MASTER_HALF_DUPLEX
;
405 master
->setup
= falcon_sflash_setup
;
406 master
->transfer_one_message
= falcon_sflash_xfer_one
;
407 master
->dev
.of_node
= pdev
->dev
.of_node
;
409 ret
= devm_spi_register_master(&pdev
->dev
, master
);
411 spi_master_put(master
);
415 static const struct of_device_id falcon_sflash_match
[] = {
416 { .compatible
= "lantiq,sflash-falcon" },
419 MODULE_DEVICE_TABLE(of
, falcon_sflash_match
);
421 static struct platform_driver falcon_sflash_driver
= {
422 .probe
= falcon_sflash_probe
,
425 .of_match_table
= falcon_sflash_match
,
429 module_platform_driver(falcon_sflash_driver
);
431 MODULE_LICENSE("GPL");
432 MODULE_DESCRIPTION("Lantiq Falcon SPI/SFLASH controller driver");