1 From 2bd534c30688bcb3f70f1816fbcff813fc746103 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sat, 27 Aug 2011 18:12:26 +0200
4 Subject: [PATCH 13/24] MIPS: lantiq: adds FALC-ON spi driver
6 The external bus unit (EBU) found on the FALC-ON SoC has spi emulation that is
7 designed for serial flash access.
9 Signed-off-by: Thomas Langer <thomas.langer@lantiq.com>
10 Signed-off-by: John Crispin <blogic@openwrt.org>
12 arch/mips/lantiq/falcon/devices.c | 12 +-
13 arch/mips/lantiq/falcon/devices.h | 4 +
14 arch/mips/lantiq/falcon/mach-easy98000.c | 27 ++
15 drivers/spi/Kconfig | 4 +
16 drivers/spi/Makefile | 1 +
17 drivers/spi/spi-falcon.c | 477 ++++++++++++++++++++++++++++++
18 6 files changed, 523 insertions(+), 2 deletions(-)
19 create mode 100644 drivers/spi/spi-falcon.c
21 Index: linux-3.0.3/arch/mips/lantiq/falcon/devices.c
22 ===================================================================
23 --- linux-3.0.3.orig/arch/mips/lantiq/falcon/devices.c 2011-10-05 12:30:34.584838403 +0200
24 +++ linux-3.0.3/arch/mips/lantiq/falcon/devices.c 2011-10-05 12:42:58.696870214 +0200
28 static struct resource falcon_i2c_resources[] = {
29 - MEM_RES("i2c", GPON_I2C_BASE,GPON_I2C_END),
30 + MEM_RES("i2c", LTQ_I2C_BASE_ADDR, LTQ_I2C_SIZE),
31 IRQ_RES("i2c_lb", FALCON_IRQ_I2C_LBREQ),
32 IRQ_RES("i2c_b", FALCON_IRQ_I2C_BREQ),
33 IRQ_RES("i2c_err", FALCON_IRQ_I2C_I2C_ERR),
36 platform_device_register_simple("i2c-falcon", 0,
37 falcon_i2c_resources, ARRAY_SIZE(falcon_i2c_resources));
38 - sys1_hw_activate(ACTS_I2C_ACT);
39 + ltq_sysctl_activate(SYSCTL_SYS1, ACTS_I2C_ACT);
42 -void __init falcon_register_crypto(void)
44 +static struct platform_device ltq_spi = {
45 + .name = "falcon_spi",
50 +falcon_register_spi_flash(struct spi_board_info *data)
52 - platform_device_register_simple("ltq_falcon_deu", 0, NULL, 0);
53 + spi_register_board_info(data, 1);
54 + platform_device_register(<q_spi);
56 Index: linux-3.0.3/arch/mips/lantiq/falcon/devices.h
57 ===================================================================
58 --- linux-3.0.3.orig/arch/mips/lantiq/falcon/devices.h 2011-10-05 12:30:34.584838403 +0200
59 +++ linux-3.0.3/arch/mips/lantiq/falcon/devices.h 2011-10-05 12:30:34.600838405 +0200
61 #ifndef _FALCON_DEVICES_H__
62 #define _FALCON_DEVICES_H__
64 +#include <linux/spi/spi.h>
65 +#include <linux/spi/flash.h>
67 #include "../devices.h"
69 extern void falcon_register_nand(void);
70 extern void falcon_register_gpio(void);
71 extern void falcon_register_gpio_extra(void);
72 extern void falcon_register_i2c(void);
73 +extern void falcon_register_spi_flash(struct spi_board_info *data);
76 Index: linux-3.0.3/arch/mips/lantiq/falcon/mach-easy98000.c
77 ===================================================================
78 --- linux-3.0.3.orig/arch/mips/lantiq/falcon/mach-easy98000.c 2011-10-05 12:30:34.552838402 +0200
79 +++ linux-3.0.3/arch/mips/lantiq/falcon/mach-easy98000.c 2011-10-05 12:30:34.600838405 +0200
81 .parts = easy98000_nor_partitions,
84 +static struct flash_platform_data easy98000_spi_flash_platform_data = {
86 + .parts = easy98000_nor_partitions,
87 + .nr_parts = ARRAY_SIZE(easy98000_nor_partitions)
90 +static struct spi_board_info easy98000_spi_flash_data __initdata = {
91 + .modalias = "m25p80",
94 + .max_speed_hz = 10 * 1000 * 1000,
96 + .platform_data = &easy98000_spi_flash_platform_data
99 /* setup gpio based spi bus/device for access to the eeprom on the board */
100 #define SPI_GPIO_MRST 102
101 #define SPI_GPIO_MTSR 103
106 +easy98000sf_init(void)
108 + easy98000_init_common();
109 + falcon_register_spi_flash(&easy98000_spi_flash_data);
113 easy98000nand_init(void)
115 easy98000_init_common();
117 "EASY98000 Eval Board",
120 +MIPS_MACHINE(LANTIQ_MACH_EASY98000SF,
122 + "EASY98000 Eval Board (Serial Flash)",
125 MIPS_MACHINE(LANTIQ_MACH_EASY98000NAND,
127 "EASY98000 Eval Board (NAND Flash)",
128 Index: linux-3.0.3/drivers/spi/Kconfig
129 ===================================================================
130 --- linux-3.0.3.orig/drivers/spi/Kconfig 2011-10-05 12:30:33.608838362 +0200
131 +++ linux-3.0.3/drivers/spi/Kconfig 2011-10-05 12:41:56.864867570 +0200
133 This drivers supports the MPC52xx SPI controller in master SPI
137 + tristate "Falcon SPI controller support"
138 + depends on SOC_FALCON
140 config SPI_MPC52xx_PSC
141 tristate "Freescale MPC52xx PSC SPI controller"
142 depends on PPC_MPC52xx && EXPERIMENTAL
143 Index: linux-3.0.3/drivers/spi/Makefile
144 ===================================================================
145 --- linux-3.0.3.orig/drivers/spi/Makefile 2011-10-05 12:30:33.608838362 +0200
146 +++ linux-3.0.3/drivers/spi/Makefile 2011-10-05 12:41:56.884867571 +0200
148 obj-$(CONFIG_SPI_SH_MSIOF) += spi_sh_msiof.o
149 obj-$(CONFIG_SPI_STMP3XXX) += spi_stmp.o
150 obj-$(CONFIG_SPI_NUC900) += spi_nuc900.o
151 +obj-$(CONFIG_SPI_FALCON) += spi-falcon.o
153 # special build for s3c24xx spi driver with fiq support
154 spi_s3c24xx_hw-y := spi_s3c24xx.o
155 Index: linux-3.0.3/drivers/spi/spi-falcon.c
156 ===================================================================
157 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
158 +++ linux-3.0.3/drivers/spi/spi-falcon.c 2011-10-05 12:30:34.600838405 +0200
161 + * This program is free software; you can redistribute it and/or modify it
162 + * under the terms of the GNU General Public License version 2 as published
163 + * by the Free Software Foundation.
165 + * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
168 +#include <linux/module.h>
169 +#include <linux/device.h>
170 +#include <linux/platform_device.h>
171 +#include <linux/spi/spi.h>
172 +#include <linux/delay.h>
173 +#include <linux/workqueue.h>
175 +#include <lantiq_soc.h>
177 +#define DRV_NAME "falcon_spi"
179 +#define FALCON_SPI_XFER_BEGIN (1 << 0)
180 +#define FALCON_SPI_XFER_END (1 << 1)
182 +/* Bus Read Configuration Register0 */
183 +#define LTQ_BUSRCON0 0x00000010
184 +/* Bus Write Configuration Register0 */
185 +#define LTQ_BUSWCON0 0x00000018
186 +/* Serial Flash Configuration Register */
187 +#define LTQ_SFCON 0x00000080
188 +/* Serial Flash Time Register */
189 +#define LTQ_SFTIME 0x00000084
190 +/* Serial Flash Status Register */
191 +#define LTQ_SFSTAT 0x00000088
192 +/* Serial Flash Command Register */
193 +#define LTQ_SFCMD 0x0000008C
194 +/* Serial Flash Address Register */
195 +#define LTQ_SFADDR 0x00000090
196 +/* Serial Flash Data Register */
197 +#define LTQ_SFDATA 0x00000094
198 +/* Serial Flash I/O Control Register */
199 +#define LTQ_SFIO 0x00000098
200 +/* EBU Clock Control Register */
201 +#define LTQ_EBUCC 0x000000C4
203 +/* Dummy Phase Length */
204 +#define SFCMD_DUMLEN_OFFSET 16
205 +#define SFCMD_DUMLEN_MASK 0x000F0000
207 +#define SFCMD_CS_OFFSET 24
208 +#define SFCMD_CS_MASK 0x07000000
210 +#define SFCMD_ALEN_OFFSET 20
211 +#define SFCMD_ALEN_MASK 0x00700000
212 +/* SCK Rise-edge Position */
213 +#define SFTIME_SCKR_POS_OFFSET 8
214 +#define SFTIME_SCKR_POS_MASK 0x00000F00
216 +#define SFTIME_SCK_PER_OFFSET 0
217 +#define SFTIME_SCK_PER_MASK 0x0000000F
218 +/* SCK Fall-edge Position */
219 +#define SFTIME_SCKF_POS_OFFSET 12
220 +#define SFTIME_SCKF_POS_MASK 0x0000F000
222 +#define SFCON_DEV_SIZE_A23_0 0x03000000
223 +#define SFCON_DEV_SIZE_MASK 0x0F000000
224 +/* Read Data Position */
225 +#define SFTIME_RD_POS_MASK 0x000F0000
227 +#define SFIO_UNUSED_WD_MASK 0x0000000F
228 +/* Command Opcode mask */
229 +#define SFCMD_OPC_MASK 0x000000FF
230 +/* dlen bytes of data to write */
231 +#define SFCMD_DIR_WRITE 0x00000100
232 +/* Data Length offset */
233 +#define SFCMD_DLEN_OFFSET 9
235 +#define SFSTAT_CMD_ERR 0x20000000
236 +/* Access Command Pending */
237 +#define SFSTAT_CMD_PEND 0x00400000
238 +/* Frequency set to 100MHz. */
239 +#define EBUCC_EBUDIV_SELF100 0x00000001
241 +#define BUSRCON0_AGEN_SERIAL_FLASH 0xF0000000
242 +/* 8-bit multiplexed */
243 +#define BUSRCON0_PORTW_8_BIT_MUX 0x00000000
245 +#define BUSWCON0_AGEN_SERIAL_FLASH 0xF0000000
246 +/* Chip Select after opcode */
247 +#define SFCMD_KEEP_CS_KEEP_SELECTED 0x00008000
250 + u32 sfcmd; /* for caching of opcode, direction, ... */
251 + struct spi_master *master;
255 +falcon_spi_xfer(struct spi_device *spi,
256 + struct spi_transfer *t,
257 + unsigned long flags)
259 + struct device *dev = &spi->dev;
260 + struct falcon_spi *priv = spi_master_get_devdata(spi->master);
261 + const u8 *txp = t->tx_buf;
262 + u8 *rxp = t->rx_buf;
263 + unsigned int bytelen = ((8 * t->len + 7) / 8);
264 + unsigned int len, alen, dumlen;
268 + state_command_prepare,
273 + } state = state_init;
277 + case state_init: /* detect phase of upper layer sequence */
279 + /* initial write ? */
280 + if (flags & FALCON_SPI_XFER_BEGIN) {
283 + "BEGIN without tx data!\n");
287 + * Prepare the parts of the sfcmd register,
289 + * change during a sequence!
290 + * Only exception are the length fields,
291 + * especially alen and dumlen.
294 + priv->sfcmd = ((spi->chip_select
295 + << SFCMD_CS_OFFSET)
297 + priv->sfcmd |= SFCMD_KEEP_CS_KEEP_SELECTED;
298 + priv->sfcmd |= *txp;
303 + * maybe address and/or dummy */
304 + state = state_command_prepare;
307 + dev_dbg(dev, "write cmd %02X\n",
308 + priv->sfcmd & SFCMD_OPC_MASK);
311 + /* continued write ? */
312 + if (txp && bytelen) {
313 + state = state_write;
317 + if (rxp && bytelen) {
318 + state = state_read;
321 + /* end of sequence? */
322 + if (flags & FALCON_SPI_XFER_END)
323 + state = state_disable_cs;
328 + case state_command_prepare: /* collect tx data for
329 + address and dummy phase */
331 + /* txp is valid, already checked */
335 + while (bytelen > 0) {
337 + val = (val<<8)|(*txp++);
339 + } else if ((dumlen < 15) && (*txp == 0)) {
341 + * assume dummy bytes are set to 0
350 + priv->sfcmd &= ~(SFCMD_ALEN_MASK | SFCMD_DUMLEN_MASK);
351 + priv->sfcmd |= (alen << SFCMD_ALEN_OFFSET) |
352 + (dumlen << SFCMD_DUMLEN_OFFSET);
354 + ltq_ebu_w32(val, LTQ_SFADDR);
356 + dev_dbg(dev, "write cmd %02X, alen=%d "
357 + "(addr=%06X) dumlen=%d\n",
358 + priv->sfcmd & SFCMD_OPC_MASK,
359 + alen, val, dumlen);
362 + /* continue with write */
363 + state = state_write;
364 + } else if (flags & FALCON_SPI_XFER_END) {
365 + /* end of sequence? */
366 + state = state_disable_cs;
368 + /* go to end and expect another
369 + * call (read or write) */
376 + /* txp still valid */
377 + priv->sfcmd |= SFCMD_DIR_WRITE;
382 + val |= (*txp++) << (8 * len++);
383 + if ((flags & FALCON_SPI_XFER_END)
384 + && (bytelen == 0)) {
386 + ~SFCMD_KEEP_CS_KEEP_SELECTED;
388 + if ((len == 4) || (bytelen == 0)) {
389 + ltq_ebu_w32(val, LTQ_SFDATA);
390 + ltq_ebu_w32(priv->sfcmd
391 + | (len<<SFCMD_DLEN_OFFSET),
395 + priv->sfcmd &= ~(SFCMD_ALEN_MASK
396 + | SFCMD_DUMLEN_MASK);
405 + priv->sfcmd &= ~SFCMD_DIR_WRITE;
407 + if ((flags & FALCON_SPI_XFER_END)
408 + && (bytelen <= 4)) {
410 + ~SFCMD_KEEP_CS_KEEP_SELECTED;
412 + len = (bytelen > 4) ? 4 : bytelen;
414 + ltq_ebu_w32(priv->sfcmd
415 + |(len<<SFCMD_DLEN_OFFSET), LTQ_SFCMD);
416 + priv->sfcmd &= ~(SFCMD_ALEN_MASK
417 + | SFCMD_DUMLEN_MASK);
419 + val = ltq_ebu_r32(LTQ_SFSTAT);
420 + if (val & SFSTAT_CMD_ERR) {
421 + /* reset error status */
422 + dev_err(dev, "SFSTAT: CMD_ERR "
424 + ltq_ebu_w32(SFSTAT_CMD_ERR,
428 + } while (val & SFSTAT_CMD_PEND);
429 + val = ltq_ebu_r32(LTQ_SFDATA);
431 + *rxp = (val & 0xFF);
440 + case state_disable_cs:
442 + priv->sfcmd &= ~SFCMD_KEEP_CS_KEEP_SELECTED;
443 + ltq_ebu_w32(priv->sfcmd | (0 << SFCMD_DLEN_OFFSET),
445 + val = ltq_ebu_r32(LTQ_SFSTAT);
446 + if (val & SFSTAT_CMD_ERR) {
447 + /* reset error status */
448 + dev_err(dev, "SFSTAT: CMD_ERR (%x)\n", val);
449 + ltq_ebu_w32(SFSTAT_CMD_ERR, LTQ_SFSTAT);
458 + } while (state != state_end);
464 +falcon_spi_setup(struct spi_device *spi)
466 + struct device *dev = &spi->dev;
467 + const u32 ebuclk = CLOCK_100M;
469 + unsigned long flags;
471 + dev_dbg(dev, "setup\n");
473 + if (spi->master->bus_num > 0 || spi->chip_select > 0)
476 + spin_lock_irqsave(&ebu_lock, flags);
478 + if (ebuclk < spi->max_speed_hz) {
479 + /* set EBU clock to 100 MHz */
480 + ltq_sys1_w32_mask(0, EBUCC_EBUDIV_SELF100, LTQ_EBUCC);
481 + i = 1; /* divider */
483 + /* set EBU clock to 50 MHz */
484 + ltq_sys1_w32_mask(EBUCC_EBUDIV_SELF100, 0, LTQ_EBUCC);
486 + /* search for suitable divider */
487 + for (i = 1; i < 7; i++) {
488 + if (ebuclk / i <= spi->max_speed_hz)
493 + /* setup period of serial clock */
494 + ltq_ebu_w32_mask(SFTIME_SCKF_POS_MASK
495 + | SFTIME_SCKR_POS_MASK
496 + | SFTIME_SCK_PER_MASK,
497 + (i << SFTIME_SCKR_POS_OFFSET)
498 + | (i << (SFTIME_SCK_PER_OFFSET + 1)),
501 + /* set some bits of unused_wd, to not trigger HOLD/WP
502 + * signals on non QUAD flashes */
503 + ltq_ebu_w32((SFIO_UNUSED_WD_MASK & (0x8 | 0x4)), LTQ_SFIO);
505 + ltq_ebu_w32(BUSRCON0_AGEN_SERIAL_FLASH | BUSRCON0_PORTW_8_BIT_MUX,
507 + ltq_ebu_w32(BUSWCON0_AGEN_SERIAL_FLASH, LTQ_BUSWCON0);
508 + /* set address wrap around to maximum for 24-bit addresses */
509 + ltq_ebu_w32_mask(SFCON_DEV_SIZE_MASK, SFCON_DEV_SIZE_A23_0, LTQ_SFCON);
511 + spin_unlock_irqrestore(&ebu_lock, flags);
517 +falcon_spi_transfer(struct spi_device *spi, struct spi_message *m)
519 + struct falcon_spi *priv = spi_master_get_devdata(spi->master);
520 + struct spi_transfer *t;
521 + unsigned long spi_flags;
522 + unsigned long flags;
526 + m->actual_length = 0;
528 + spi_flags = FALCON_SPI_XFER_BEGIN;
529 + list_for_each_entry(t, &m->transfers, transfer_list) {
530 + if (list_is_last(&t->transfer_list, &m->transfers))
531 + spi_flags |= FALCON_SPI_XFER_END;
533 + spin_lock_irqsave(&ebu_lock, flags);
534 + ret = falcon_spi_xfer(spi, t, spi_flags);
535 + spin_unlock_irqrestore(&ebu_lock, flags);
540 + m->actual_length += t->len;
542 + if (t->delay_usecs || t->cs_change)
549 + m->complete(m->context);
555 +falcon_spi_cleanup(struct spi_device *spi)
557 + struct device *dev = &spi->dev;
559 + dev_dbg(dev, "cleanup\n");
562 +static int __devinit
563 +falcon_spi_probe(struct platform_device *pdev)
565 + struct device *dev = &pdev->dev;
566 + struct falcon_spi *priv;
567 + struct spi_master *master;
570 + dev_dbg(dev, "probing\n");
572 + master = spi_alloc_master(&pdev->dev, sizeof(*priv));
574 + dev_err(dev, "no memory for spi_master\n");
578 + priv = spi_master_get_devdata(master);
579 + priv->master = master;
581 + master->mode_bits = SPI_MODE_3;
582 + master->num_chipselect = 1;
583 + master->bus_num = 0;
585 + master->setup = falcon_spi_setup;
586 + master->transfer = falcon_spi_transfer;
587 + master->cleanup = falcon_spi_cleanup;
589 + platform_set_drvdata(pdev, priv);
591 + ret = spi_register_master(master);
593 + spi_master_put(master);
598 +static int __devexit
599 +falcon_spi_remove(struct platform_device *pdev)
601 + struct device *dev = &pdev->dev;
602 + struct falcon_spi *priv = platform_get_drvdata(pdev);
604 + dev_dbg(dev, "removed\n");
606 + spi_unregister_master(priv->master);
611 +static struct platform_driver falcon_spi_driver = {
612 + .probe = falcon_spi_probe,
613 + .remove = __devexit_p(falcon_spi_remove),
616 + .owner = THIS_MODULE
621 +falcon_spi_init(void)
623 + return platform_driver_register(&falcon_spi_driver);
627 +falcon_spi_exit(void)
629 + platform_driver_unregister(&falcon_spi_driver);
632 +module_init(falcon_spi_init);
633 +module_exit(falcon_spi_exit);
635 +MODULE_LICENSE("GPL");
636 +MODULE_DESCRIPTION("Lantiq Falcon SPI controller driver");
637 Index: linux-3.0.3/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h
638 ===================================================================
639 --- linux-3.0.3.orig/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h 2011-10-05 12:38:16.176858136 +0200
640 +++ linux-3.0.3/arch/mips/include/asm/mach-lantiq/falcon/lantiq_soc.h 2011-10-05 12:39:54.936862358 +0200
643 #define LTQ_EBU_MODCON 0x000C
646 +#define LTQ_I2C_BASE_ADDR 0x1E200000
647 +#define LTQ_I2C_SIZE 0x00010000
650 #define LTQ_GPIO0_BASE_ADDR 0x1D810000
651 #define LTQ_GPIO0_SIZE 0x0080
654 /* Activation Status Register */
655 #define ACTS_ASC1_ACT 0x00000800
656 +#define ACTS_I2C_ACT 0x00004000
657 #define ACTS_P0 0x00010000
658 #define ACTS_P1 0x00010000
659 #define ACTS_P2 0x00020000