From patchwork Tue Dec 4 08:31:57 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Hesselbarth X-Patchwork-Id: 203578 X-Patchwork-Delegate: prafulla@marvell.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 4F9142C008C for ; Tue, 4 Dec 2012 19:33:32 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C38A94A122; Tue, 4 Dec 2012 09:33:21 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id bSXeKF4xTPOW; Tue, 4 Dec 2012 09:33:21 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1D65D4A123; Tue, 4 Dec 2012 09:32:45 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id ABDAB4A100 for ; Tue, 4 Dec 2012 09:32:36 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VzgWAYDCeT-g for ; Tue, 4 Dec 2012 09:32:31 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-bk0-f44.google.com (mail-bk0-f44.google.com [209.85.214.44]) by theia.denx.de (Postfix) with ESMTPS id 80C914A0EF for ; Tue, 4 Dec 2012 09:32:25 +0100 (CET) Received: by mail-bk0-f44.google.com with SMTP id w11so1485644bku.3 for ; Tue, 04 Dec 2012 00:32:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=bo3696WqTygbEMi+5ef9dn6LmXBlG1exlVXzNJzw/PM=; b=fWGFo5mlTBUa1sQMX21OrlNvz/R4ELBgE3/SMfV1AwS78ndNJXKHrPzuJfr1XI/f8S SqqzgYPlqDI7LUbcMNQ6ivuAKzZADGpruSrcwD1osbNUN/0plpTjKxVSzfypA1GJBsGH lz21LTZX/3SYMr8uB6ckw7JEAMNAriC3O4TKzXGq3PNlYRJCuYJ/P7rcY7DASkgPHRe+ H6mhMmbQ2e5sH/x+tDR36oxZfqDkhNzH1waMrHhFAwZo9Xr/OHrf6apHkQyl48oMSf9I pdCXsePrKroyzYMrGhvZr1vKZA4wPrBFbk+2MFsJKNI5CrtqeNlfAJuTMn6igukjF9BR qIqA== Received: by 10.204.147.132 with SMTP id l4mr3815417bkv.20.1354609945797; Tue, 04 Dec 2012 00:32:25 -0800 (PST) Received: from nijin.lan (dslc-082-083-245-055.pools.arcor-ip.net. [82.83.245.55]) by mx.google.com with ESMTPS id i20sm269279bkw.5.2012.12.04.00.32.22 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 04 Dec 2012 00:32:25 -0800 (PST) From: Sebastian Hesselbarth To: Sebastian Hesselbarth Date: Tue, 4 Dec 2012 09:31:57 +0100 Message-Id: <1354609923-27320-5-git-send-email-sebastian.hesselbarth@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1354609923-27320-1-git-send-email-sebastian.hesselbarth@gmail.com> References: <1354458982-14403-1-git-send-email-sebastian.hesselbarth@gmail.com> <1354609923-27320-1-git-send-email-sebastian.hesselbarth@gmail.com> Cc: Rabeeh Khoury , Luka Perkov , u-boot@lists.denx.de, Daniel Stodden , Andy Fleming Subject: [U-Boot] [PATCH v2 04/10] SPI: Add Orion SPI driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This adds an SPI driver found on Marvell Orion SoCs. This driver is taken from kirkwood_spi but removes mpp configuration as dove has dedicated spi pins. To have a common driver for orion5x, kirkwood, and dove, mpp configuration should be handled in some cpu/board-specific setup. Signed-off-by: Sebastian Hesselbarth --- Cc: u-boot@lists.denx.de Cc: Sebastian Hesselbarth Cc: Rabeeh Khoury Cc: Albert Aribaud Cc: Prafulla Wadaskar Cc: Andy Fleming Cc: Joe Hershberger Cc: Daniel Stodden Cc: Luka Perkov --- drivers/spi/Makefile | 1 + drivers/spi/orion_spi.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 drivers/spi/orion_spi.c diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index f0b82c6..679ba61 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -34,6 +34,7 @@ COBJS-$(CONFIG_BFIN_SPI) += bfin_spi.o COBJS-$(CONFIG_CF_SPI) += cf_spi.o COBJS-$(CONFIG_CF_QSPI) += cf_qspi.o COBJS-$(CONFIG_DAVINCI_SPI) += davinci_spi.o +COBJS-$(CONFIG_ORION_SPI) += orion_spi.o COBJS-$(CONFIG_KIRKWOOD_SPI) += kirkwood_spi.o COBJS-$(CONFIG_MPC52XX_SPI) += mpc52xx_spi.o COBJS-$(CONFIG_MPC8XXX_SPI) += mpc8xxx_spi.o diff --git a/drivers/spi/orion_spi.c b/drivers/spi/orion_spi.c new file mode 100644 index 0000000..8a50531 --- /dev/null +++ b/drivers/spi/orion_spi.c @@ -0,0 +1,217 @@ +/* + * Marvell Orion SoCs common spi driver + * + * Sebastian Hesselbarth + * based on kirkwood_spi.c written by + * Prafulla Wadaskar + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include + +/* SPI Registers on orion SOC */ +struct orionspi_registers { + u32 ctrl; /* 0x00 */ + u32 cfg; /* 0x04 */ + u32 dout; /* 0x08 */ + u32 din; /* 0x0c */ + u32 irq_cause; /* 0x10 */ + u32 irq_mask; /* 0x14 */ +}; + +#define ORIONSPI_CLKPRESCL_MASK 0x1f +#define ORIONSPI_CLKPRESCL_MIN 0x12 +#define ORIONSPI_CSN_ACT 1 /* Activates serial memory interface */ +#define ORIONSPI_SMEMRDY (1 << 1) /* SerMem Data xfer ready */ +#define ORIONSPI_IRQUNMASK 1 /* unmask SPI interrupt */ +#define ORIONSPI_IRQMASK 0 /* mask SPI interrupt */ +#define ORIONSPI_SMEMRDIRQ 1 /* SerMem data xfer ready irq */ +#define ORIONSPI_XFERLEN_1BYTE 0 +#define ORIONSPI_XFERLEN_2BYTE (1 << 5) +#define ORIONSPI_XFERLEN_MASK (1 << 5) +#define ORIONSPI_ADRLEN_1BYTE 0 +#define ORIONSPI_ADRLEN_2BYTE (1 << 8) +#define ORIONSPI_ADRLEN_3BYTE (2 << 8) +#define ORIONSPI_ADRLEN_4BYTE (3 << 8) +#define ORIONSPI_ADRLEN_MASK (3 << 8) +#define ORIONSPI_TIMEOUT 10000 + +static struct orionspi_registers *spireg = + (struct orionspi_registers *)ORION_SPI_BASE; + +struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, + unsigned int max_hz, unsigned int mode) +{ + struct spi_slave *slave; + u32 data; + u32 kwspi_mpp_config[] = { 0, 0 }; + + if (!spi_cs_is_valid(bus, cs)) + return NULL; + + slave = malloc(sizeof(struct spi_slave)); + if (!slave) + return NULL; + + slave->bus = bus; + slave->cs = cs; + + writel(~ORIONSPI_CSN_ACT | ORIONSPI_SMEMRDY, &spireg->ctrl); + + /* calculate spi clock prescaller using max_hz */ + data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10; + data = data < ORIONSPI_CLKPRESCL_MIN ? ORIONSPI_CLKPRESCL_MIN : data; + data = data > ORIONSPI_CLKPRESCL_MASK ? ORIONSPI_CLKPRESCL_MASK : data; + + /* program spi clock prescaller using max_hz */ + writel(ORIONSPI_ADRLEN_3BYTE | data, &spireg->cfg); + debug("data = 0x%08x\n", data); + + writel(ORIONSPI_SMEMRDIRQ, &spireg->irq_cause); + writel(ORIONSPI_IRQMASK, &spireg->irq_mask); + + return slave; +} + +void spi_free_slave(struct spi_slave *slave) +{ + free(slave); +} + +#if defined(CONFIG_SYS_KW_SPI_MPP) +u32 spi_mpp_backup[4]; +#endif + +__attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave) +{ + return 0; +} + +int spi_claim_bus(struct spi_slave *slave) +{ + return board_spi_claim_bus(slave); +} + +__attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave) +{ +} + +void spi_release_bus(struct spi_slave *slave) +{ + board_spi_release_bus(slave); +} + +#ifndef CONFIG_SPI_CS_IS_VALID +/* + * you can define this function board specific + * define above CONFIG in board specific config file and + * provide the function in board specific src file + */ +int spi_cs_is_valid(unsigned int bus, unsigned int cs) +{ + return (bus == 0 && (cs == 0 || cs == 1)); +} +#endif + +void spi_init(void) +{ +} + +void spi_cs_activate(struct spi_slave *slave) +{ + writel(readl(&spireg->ctrl) | ORIONSPI_IRQUNMASK, &spireg->ctrl); +} + +void spi_cs_deactivate(struct spi_slave *slave) +{ + writel(readl(&spireg->ctrl) & ORIONSPI_IRQMASK, &spireg->ctrl); +} + +int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, + void *din, unsigned long flags) +{ + unsigned int tmpdout, tmpdin; + int tm, isread = 0; + + debug("spi_xfer: slave %u:%u dout %p din %p bitlen %u\n", + slave->bus, slave->cs, dout, din, bitlen); + + if (flags & SPI_XFER_BEGIN) + spi_cs_activate(slave); + + /* + * handle data in 8-bit chunks + * TBD: 2byte xfer mode to be enabled + */ + writel(((readl(&spireg->cfg) & ~ORIONSPI_XFERLEN_MASK) | + ORIONSPI_XFERLEN_1BYTE), &spireg->cfg); + + while (bitlen > 4) { + debug("loopstart bitlen %d\n", bitlen); + tmpdout = 0; + + /* Shift data so it's msb-justified */ + if (dout) + tmpdout = *(u32 *) dout & 0x0ff; + + writel(~ORIONSPI_SMEMRDIRQ, &spireg->irq_cause); + writel(tmpdout, &spireg->dout); /* Write the data out */ + debug("*** spi_xfer: ... %08x written, bitlen %d\n", + tmpdout, bitlen); + + /* + * Wait for SPI transmit to get out + * or time out (1 second = 1000 ms) + * The NE event must be read and cleared first + */ + for (tm = 0, isread = 0; tm < ORIONSPI_TIMEOUT; ++tm) { + if (readl(&spireg->irq_cause) & ORIONSPI_SMEMRDIRQ) { + isread = 1; + tmpdin = readl(&spireg->din); + debug + ("spi_xfer: din %p..%08x read\n", + din, tmpdin); + + if (din) { + *((u8 *) din) = (u8) tmpdin; + din += 1; + } + if (dout) + dout += 1; + bitlen -= 8; + } + if (isread) + break; + } + if (tm >= ORIONSPI_TIMEOUT) + printf("*** spi_xfer: Time out during SPI transfer\n"); + + debug("loopend bitlen %d\n", bitlen); + } + + if (flags & SPI_XFER_END) + spi_cs_deactivate(slave); + + return 0; +}