2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
6 * Copyright (C) 2012 Thomas Langer <thomas.langer@lantiq.com>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/platform_device.h>
12 #include <linux/spi/spi.h>
13 #include <linux/delay.h>
14 #include <linux/workqueue.h>
16 #include <linux/of_platform.h>
18 #include <lantiq_soc.h>
20 #define DRV_NAME "sflash-falcon"
22 #define FALCON_SPI_XFER_BEGIN (1 << 0)
23 #define FALCON_SPI_XFER_END (1 << 1)
25 /* Bus Read Configuration Register0 */
26 #define BUSRCON0 0x00000010
27 /* Bus Write Configuration Register0 */
28 #define BUSWCON0 0x00000018
29 /* Serial Flash Configuration Register */
30 #define SFCON 0x00000080
31 /* Serial Flash Time Register */
32 #define SFTIME 0x00000084
33 /* Serial Flash Status Register */
34 #define SFSTAT 0x00000088
35 /* Serial Flash Command Register */
36 #define SFCMD 0x0000008C
37 /* Serial Flash Address Register */
38 #define SFADDR 0x00000090
39 /* Serial Flash Data Register */
40 #define SFDATA 0x00000094
41 /* Serial Flash I/O Control Register */
42 #define SFIO 0x00000098
43 /* EBU Clock Control Register */
44 #define EBUCC 0x000000C4
46 /* Dummy Phase Length */
47 #define SFCMD_DUMLEN_OFFSET 16
48 #define SFCMD_DUMLEN_MASK 0x000F0000
50 #define SFCMD_CS_OFFSET 24
51 #define SFCMD_CS_MASK 0x07000000
53 #define SFCMD_ALEN_OFFSET 20
54 #define SFCMD_ALEN_MASK 0x00700000
55 /* SCK Rise-edge Position */
56 #define SFTIME_SCKR_POS_OFFSET 8
57 #define SFTIME_SCKR_POS_MASK 0x00000F00
59 #define SFTIME_SCK_PER_OFFSET 0
60 #define SFTIME_SCK_PER_MASK 0x0000000F
61 /* SCK Fall-edge Position */
62 #define SFTIME_SCKF_POS_OFFSET 12
63 #define SFTIME_SCKF_POS_MASK 0x0000F000
65 #define SFCON_DEV_SIZE_A23_0 0x03000000
66 #define SFCON_DEV_SIZE_MASK 0x0F000000
67 /* Read Data Position */
68 #define SFTIME_RD_POS_MASK 0x000F0000
70 #define SFIO_UNUSED_WD_MASK 0x0000000F
71 /* Command Opcode mask */
72 #define SFCMD_OPC_MASK 0x000000FF
73 /* dlen bytes of data to write */
74 #define SFCMD_DIR_WRITE 0x00000100
75 /* Data Length offset */
76 #define SFCMD_DLEN_OFFSET 9
78 #define SFSTAT_CMD_ERR 0x20000000
79 /* Access Command Pending */
80 #define SFSTAT_CMD_PEND 0x00400000
81 /* Frequency set to 100MHz. */
82 #define EBUCC_EBUDIV_SELF100 0x00000001
84 #define BUSRCON0_AGEN_SERIAL_FLASH 0xF0000000
85 /* 8-bit multiplexed */
86 #define BUSRCON0_PORTW_8_BIT_MUX 0x00000000
88 #define BUSWCON0_AGEN_SERIAL_FLASH 0xF0000000
89 /* Chip Select after opcode */
90 #define SFCMD_KEEP_CS_KEEP_SELECTED 0x00008000
92 #define CLOCK_100M 100000000
93 #define CLOCK_50M 50000000
95 struct falcon_sflash
{
96 u32 sfcmd
; /* for caching of opcode, direction, ... */
97 struct spi_master
*master
;
100 int falcon_sflash_xfer(struct spi_device
*spi
, struct spi_transfer
*t
,
103 struct device
*dev
= &spi
->dev
;
104 struct falcon_sflash
*priv
= spi_master_get_devdata(spi
->master
);
105 const u8
*txp
= t
->tx_buf
;
107 unsigned int bytelen
= ((8 * t
->len
+ 7) / 8);
108 unsigned int len
, alen
, dumlen
;
112 state_command_prepare
,
117 } state
= state_init
;
121 case state_init
: /* detect phase of upper layer sequence */
123 /* initial write ? */
124 if (flags
& FALCON_SPI_XFER_BEGIN
) {
127 "BEGIN without tx data!\n");
131 * Prepare the parts of the sfcmd register,
132 * which should not change during a sequence!
133 * Only exception are the length fields,
134 * especially alen and dumlen.
137 priv
->sfcmd
= ((spi
->chip_select
140 priv
->sfcmd
|= SFCMD_KEEP_CS_KEEP_SELECTED
;
147 * maybe address and/or dummy
149 state
= state_command_prepare
;
152 dev_dbg(dev
, "write cmd %02X\n",
153 priv
->sfcmd
& SFCMD_OPC_MASK
);
156 /* continued write ? */
157 if (txp
&& bytelen
) {
162 if (rxp
&& bytelen
) {
166 /* end of sequence? */
167 if (flags
& FALCON_SPI_XFER_END
)
168 state
= state_disable_cs
;
173 /* collect tx data for address and dummy phase */
174 case state_command_prepare
:
176 /* txp is valid, already checked */
180 while (bytelen
> 0) {
182 val
= (val
<< 8) | (*txp
++);
184 } else if ((dumlen
< 15) && (*txp
== 0)) {
186 * assume dummy bytes are set to 0
196 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
| SFCMD_DUMLEN_MASK
);
197 priv
->sfcmd
|= (alen
<< SFCMD_ALEN_OFFSET
) |
198 (dumlen
<< SFCMD_DUMLEN_OFFSET
);
200 ltq_ebu_w32(val
, SFADDR
);
202 dev_dbg(dev
, "wr %02X, alen=%d (addr=%06X) dlen=%d\n",
203 priv
->sfcmd
& SFCMD_OPC_MASK
,
207 /* continue with write */
209 } else if (flags
& FALCON_SPI_XFER_END
) {
210 /* end of sequence? */
211 state
= state_disable_cs
;
214 * go to end and expect another
215 * call (read or write)
223 /* txp still valid */
224 priv
->sfcmd
|= SFCMD_DIR_WRITE
;
229 val
|= (*txp
++) << (8 * len
++);
230 if ((flags
& FALCON_SPI_XFER_END
)
233 ~SFCMD_KEEP_CS_KEEP_SELECTED
;
235 if ((len
== 4) || (bytelen
== 0)) {
236 ltq_ebu_w32(val
, SFDATA
);
237 ltq_ebu_w32(priv
->sfcmd
238 | (len
<<SFCMD_DLEN_OFFSET
),
242 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
243 | SFCMD_DUMLEN_MASK
);
252 priv
->sfcmd
&= ~SFCMD_DIR_WRITE
;
254 if ((flags
& FALCON_SPI_XFER_END
)
257 ~SFCMD_KEEP_CS_KEEP_SELECTED
;
259 len
= (bytelen
> 4) ? 4 : bytelen
;
261 ltq_ebu_w32(priv
->sfcmd
262 | (len
<< SFCMD_DLEN_OFFSET
), SFCMD
);
263 priv
->sfcmd
&= ~(SFCMD_ALEN_MASK
264 | SFCMD_DUMLEN_MASK
);
266 val
= ltq_ebu_r32(SFSTAT
);
267 if (val
& SFSTAT_CMD_ERR
) {
268 /* reset error status */
269 dev_err(dev
, "SFSTAT: CMD_ERR");
270 dev_err(dev
, " (%x)\n", val
);
271 ltq_ebu_w32(SFSTAT_CMD_ERR
,
275 } while (val
& SFSTAT_CMD_PEND
);
276 val
= ltq_ebu_r32(SFDATA
);
287 case state_disable_cs
:
289 priv
->sfcmd
&= ~SFCMD_KEEP_CS_KEEP_SELECTED
;
290 ltq_ebu_w32(priv
->sfcmd
| (0 << SFCMD_DLEN_OFFSET
),
292 val
= ltq_ebu_r32(SFSTAT
);
293 if (val
& SFSTAT_CMD_ERR
) {
294 /* reset error status */
295 dev_err(dev
, "SFSTAT: CMD_ERR (%x)\n", val
);
296 ltq_ebu_w32(SFSTAT_CMD_ERR
, SFSTAT
);
305 } while (state
!= state_end
);
310 static int falcon_sflash_setup(struct spi_device
*spi
)
315 if (spi
->chip_select
> 0)
318 spin_lock_irqsave(&ebu_lock
, flags
);
320 if (spi
->max_speed_hz
>= CLOCK_100M
) {
321 /* set EBU clock to 100 MHz */
322 ltq_sys1_w32_mask(0, EBUCC_EBUDIV_SELF100
, EBUCC
);
325 /* set EBU clock to 50 MHz */
326 ltq_sys1_w32_mask(EBUCC_EBUDIV_SELF100
, 0, EBUCC
);
328 /* search for suitable divider */
329 for (i
= 1; i
< 7; i
++) {
330 if (CLOCK_50M
/ i
<= spi
->max_speed_hz
)
335 /* setup period of serial clock */
336 ltq_ebu_w32_mask(SFTIME_SCKF_POS_MASK
337 | SFTIME_SCKR_POS_MASK
338 | SFTIME_SCK_PER_MASK
,
339 (i
<< SFTIME_SCKR_POS_OFFSET
)
340 | (i
<< (SFTIME_SCK_PER_OFFSET
+ 1)),
344 * set some bits of unused_wd, to not trigger HOLD/WP
345 * signals on non QUAD flashes
347 ltq_ebu_w32((SFIO_UNUSED_WD_MASK
& (0x8 | 0x4)), SFIO
);
349 ltq_ebu_w32(BUSRCON0_AGEN_SERIAL_FLASH
| BUSRCON0_PORTW_8_BIT_MUX
,
351 ltq_ebu_w32(BUSWCON0_AGEN_SERIAL_FLASH
, BUSWCON0
);
352 /* set address wrap around to maximum for 24-bit addresses */
353 ltq_ebu_w32_mask(SFCON_DEV_SIZE_MASK
, SFCON_DEV_SIZE_A23_0
, SFCON
);
355 spin_unlock_irqrestore(&ebu_lock
, flags
);
360 static int falcon_sflash_prepare_xfer(struct spi_master
*master
)
365 static int falcon_sflash_unprepare_xfer(struct spi_master
*master
)
370 static int falcon_sflash_xfer_one(struct spi_master
*master
,
371 struct spi_message
*m
)
373 struct falcon_sflash
*priv
= spi_master_get_devdata(master
);
374 struct spi_transfer
*t
;
375 unsigned long spi_flags
;
380 m
->actual_length
= 0;
382 spi_flags
= FALCON_SPI_XFER_BEGIN
;
383 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
384 if (list_is_last(&t
->transfer_list
, &m
->transfers
))
385 spi_flags
|= FALCON_SPI_XFER_END
;
387 spin_lock_irqsave(&ebu_lock
, flags
);
388 ret
= falcon_sflash_xfer(m
->spi
, t
, spi_flags
);
389 spin_unlock_irqrestore(&ebu_lock
, flags
);
394 m
->actual_length
+= t
->len
;
396 WARN_ON(t
->delay_usecs
|| t
->cs_change
);
401 spi_finalize_current_message(master
);
406 static int falcon_sflash_probe(struct platform_device
*pdev
)
408 struct falcon_sflash
*priv
;
409 struct spi_master
*master
;
412 if (ltq_boot_select() != BS_SPI
) {
413 dev_err(&pdev
->dev
, "invalid bootstrap options\n");
417 master
= spi_alloc_master(&pdev
->dev
, sizeof(*priv
));
421 priv
= spi_master_get_devdata(master
);
422 priv
->master
= master
;
424 master
->mode_bits
= SPI_MODE_3
;
425 master
->num_chipselect
= 1;
426 master
->flags
= SPI_MASTER_HALF_DUPLEX
;
427 master
->bus_num
= -1;
428 master
->setup
= falcon_sflash_setup
;
429 master
->prepare_transfer_hardware
= falcon_sflash_prepare_xfer
;
430 master
->transfer_one_message
= falcon_sflash_xfer_one
;
431 master
->unprepare_transfer_hardware
= falcon_sflash_unprepare_xfer
;
432 master
->dev
.of_node
= pdev
->dev
.of_node
;
434 platform_set_drvdata(pdev
, priv
);
436 ret
= spi_register_master(master
);
438 spi_master_put(master
);
442 static int falcon_sflash_remove(struct platform_device
*pdev
)
444 struct falcon_sflash
*priv
= platform_get_drvdata(pdev
);
446 spi_unregister_master(priv
->master
);
451 static const struct of_device_id falcon_sflash_match
[] = {
452 { .compatible
= "lantiq,sflash-falcon" },
455 MODULE_DEVICE_TABLE(of
, falcon_sflash_match
);
457 static struct platform_driver falcon_sflash_driver
= {
458 .probe
= falcon_sflash_probe
,
459 .remove
= falcon_sflash_remove
,
462 .owner
= THIS_MODULE
,
463 .of_match_table
= falcon_sflash_match
,
467 module_platform_driver(falcon_sflash_driver
);
469 MODULE_LICENSE("GPL");
470 MODULE_DESCRIPTION("Lantiq Falcon SPI/SFLASH controller driver");