From patchwork Wed Jun 26 07:41:11 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Poddar, Sourav" X-Patchwork-Id: 2783281 Return-Path: X-Original-To: patchwork-spi-devel-general@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 5FE1FC0AB1 for ; Wed, 26 Jun 2013 07:42:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C50F120215 for ; Wed, 26 Jun 2013 07:42:44 +0000 (UTC) Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0E43420213 for ; Wed, 26 Jun 2013 07:42:43 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1UrkNG-00047b-CD; Wed, 26 Jun 2013 07:42:42 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1UrkND-00047R-8n for spi-devel-general@lists.sourceforge.net; Wed, 26 Jun 2013 07:42:39 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of ti.com designates 198.47.26.152 as permitted sender) client-ip=198.47.26.152; envelope-from=sourav.poddar@ti.com; helo=comal.ext.ti.com; Received: from comal.ext.ti.com ([198.47.26.152]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1UrkN9-0006gS-M3 for spi-devel-general@lists.sourceforge.net; Wed, 26 Jun 2013 07:42:39 +0000 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by comal.ext.ti.com (8.13.7/8.13.7) with ESMTP id r5Q7ffwR032740; Wed, 26 Jun 2013 02:41:41 -0500 Received: from DLEE71.ent.ti.com (dlee71.ent.ti.com [157.170.170.114]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id r5Q7ffQD025483; Wed, 26 Jun 2013 02:41:41 -0500 Received: from dlelxv22.itg.ti.com (172.17.1.197) by DLEE71.ent.ti.com (157.170.170.114) with Microsoft SMTP Server id 14.2.342.3; Wed, 26 Jun 2013 02:41:41 -0500 Received: from a0131647.apr.dhcp.ti.com (a0131647.apr.dhcp.ti.com [172.24.145.168]) by dlelxv22.itg.ti.com (8.13.8/8.13.8) with ESMTP id r5Q7fP1Y021398; Wed, 26 Jun 2013 02:41:37 -0500 From: Sourav Poddar To: , , , , , , Subject: [PATCH 2/3] drivers: spi: Add qspi flash controller Date: Wed, 26 Jun 2013 13:11:11 +0530 Message-ID: <1372232472-2641-3-git-send-email-sourav.poddar@ti.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1372232472-2641-1-git-send-email-sourav.poddar@ti.com> References: <1372232472-2641-1-git-send-email-sourav.poddar@ti.com> MIME-Version: 1.0 X-Spam-Score: -2.8 (--) X-Headers-End: 1UrkN9-0006gS-M3 Cc: Sourav Poddar , linux-omap@vger.kernel.org, rnayak@ti.com, linux-kernel@vger.kernel.org, balbi@ti.com X-BeenThere: spi-devel-general@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux SPI core/device drivers discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: spi-devel-general-bounces@lists.sourceforge.net X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The patch add basic support for the quad spi controller. QSPI is a kind of spi module that allows single, dual and quad read access to external spi devices. The module has a memory mapped interface which provide direct interface for accessing data form external spi devices. The patch will configure controller clocks, device control register and for defining low level transfer apis which will be used by the spi framework to transfer data to the slave spi device(flash in this case). Signed-off-by: Sourav Poddar --- drivers/spi/Kconfig | 6 + drivers/spi/Makefile | 1 + drivers/spi/ti-qspi.c | 352 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 359 insertions(+), 0 deletions(-) create mode 100644 drivers/spi/ti-qspi.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 92a9345..29a363b 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -285,6 +285,12 @@ config SPI_OMAP24XX SPI master controller for OMAP24XX and later Multichannel SPI (McSPI) modules. +config QSPI_DRA7xxx + tristate "DRA7xxx QSPI controller support" + depends on ARCH_OMAP2PLUS + help + QSPI master controller for DRA7xxx used for flash devices. + config SPI_OMAP_100K tristate "OMAP SPI 100K" depends on ARCH_OMAP850 || ARCH_OMAP730 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 33f9c09..ea14eff 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -46,6 +46,7 @@ obj-$(CONFIG_SPI_OCTEON) += spi-octeon.o obj-$(CONFIG_SPI_OMAP_UWIRE) += spi-omap-uwire.o obj-$(CONFIG_SPI_OMAP_100K) += spi-omap-100k.o obj-$(CONFIG_SPI_OMAP24XX) += spi-omap2-mcspi.o +obj-$(CONFIG_QSPI_DRA7xxx) += ti-qspi.o obj-$(CONFIG_SPI_ORION) += spi-orion.o obj-$(CONFIG_SPI_PL022) += spi-pl022.o obj-$(CONFIG_SPI_PPC4xx) += spi-ppc4xx.o diff --git a/drivers/spi/ti-qspi.c b/drivers/spi/ti-qspi.c new file mode 100644 index 0000000..b33646a --- /dev/null +++ b/drivers/spi/ti-qspi.c @@ -0,0 +1,352 @@ +/* + * TI QSPI driver + * + * Copyright (C) 2013, Texas Instruments, Incorporated + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +struct dra7xxx_qspi { + struct spi_master *master; + void __iomem *base; + int device_type; + struct device *dev; + u32 spi_max_frequency; + u32 cmd; + u32 dc; +}; + +#define QSPI_PID (0x0) +#define QSPI_SYSCONFIG (0x10) +#define QSPI_INTR_STATUS_RAW_SET (0x20) +#define QSPI_INTR_STATUS_ENABLED_CLEAR (0x24) +#define QSPI_INTR_ENABLE_SET_REG (0x28) +#define QSPI_INTR_ENABLE_CLEAR_REG (0x2c) +#define QSPI_SPI_CLOCK_CNTRL_REG (0x40) +#define QSPI_SPI_DC_REG (0x44) +#define QSPI_SPI_CMD_REG (0x48) +#define QSPI_SPI_STATUS_REG (0x4c) +#define QSPI_SPI_DATA_REG (0x50) +#define QSPI_SPI_SETUP0_REG (0x54) +#define QSPI_SPI_SWITCH_REG (0x64) +#define QSPI_SPI_SETUP1_REG (0x58) +#define QSPI_SPI_SETUP2_REG (0x5c) +#define QSPI_SPI_SETUP3_REG (0x60) +#define QSPI_SPI_DATA_REG_1 (0x68) +#define QSPI_SPI_DATA_REG_2 (0x6c) +#define QSPI_SPI_DATA_REG_3 (0x70) + +#define QSPI_TIMEOUT 2000000 + +#define QSPI_FCLK 192000000 + +/* Clock Control */ +#define QSPI_CLK_EN (1 << 31) +#define QSPI_CLK_DIV_MAX 0xffff + +/* Command */ +#define QSPI_EN_CS(n) (n << 28) +#define QSPI_WLEN(n) ((n-1) << 19) +#define QSPI_3_PIN (1 << 18) +#define QSPI_RD_SNGL (1 << 16) +#define QSPI_WR_SNGL (2 << 16) +#define QSPI_RD_QUAD (7 << 16) +#define QSPI_INVAL (4 << 16) + +/* Device Control */ +#define QSPI_DD(m, n) (m << (3 + n*8)) +#define QSPI_CKPHA(n) (1 << (2 + n*8)) +#define QSPI_CSPOL(n) (1 << (1 + n*8)) +#define QSPI_CKPOL(n) (1 << (n*8)) + +/* Status */ +#define QSPI_WC (1 << 1) +#define QSPI_BUSY (1 << 0) +#define QSPI_WC_BUSY (QSPI_WC | QSPI_BUSY) +#define QSPI_XFER_DONE QSPI_WC + +#define XFER_END 0x01 + +#define SPI_AUTOSUSPEND_TIMEOUT 2000 + +static inline unsigned long dra7xxx_readl(struct dra7xxx_qspi *qspi, + unsigned long reg) +{ + return readl(qspi->base + reg); +} + +static inline void dra7xxx_writel(struct dra7xxx_qspi *qspi, + unsigned long val, unsigned long reg) +{ + writel(val, qspi->base + reg); +} + +static int dra7xxx_qspi_setup(struct spi_device *spi) +{ + struct dra7xxx_qspi *qspi = + spi_master_get_devdata(spi->master); + + int clk_div; + + if (!qspi->spi_max_frequency) + clk_div = 0; + else + clk_div = (QSPI_FCLK / qspi->spi_max_frequency) - 1; + + pr_debug("%s: hz: %d, clock divider %d\n", __func__, + qspi->spi_max_frequency, clk_div); + + pm_runtime_get_sync(qspi->dev); + + /* disable SCLK */ + dra7xxx_writel(qspi, dra7xxx_readl(qspi, QSPI_SPI_CLOCK_CNTRL_REG) + & ~QSPI_CLK_EN, QSPI_SPI_CLOCK_CNTRL_REG); + + if (clk_div < 0) { + pr_debug("%s: clock divider < 0, using /1 divider\n", __func__); + clk_div = 0; + } + + if (clk_div > QSPI_CLK_DIV_MAX) { + pr_debug("%s: clock divider >%d , using /%d divider\n", + __func__, QSPI_CLK_DIV_MAX, QSPI_CLK_DIV_MAX + 1); + clk_div = QSPI_CLK_DIV_MAX; + } + + /* enable SCLK */ + dra7xxx_writel(qspi, QSPI_CLK_EN | clk_div, QSPI_SPI_CLOCK_CNTRL_REG); + + pm_runtime_mark_last_busy(qspi->dev); + pm_runtime_put_autosuspend(qspi->dev); + + pr_debug("%s: spi_clock_cntrl %ld\n", __func__, + dra7xxx_readl(qspi, QSPI_SPI_CLOCK_CNTRL_REG)); + + return 0; +} + +static int dra7xxx_qspi_prepare_xfer(struct spi_master *master) +{ + return 0; +} + +static int dra7xxx_qspi_unprepare_xfer(struct spi_master *master) +{ + return 0; +} + +static int qspi_transfer_msg(struct dra7xxx_qspi *qspi, unsigned count, + const u8 *txbuf, u8 *rxbuf, bool flags) +{ + uint status; + int timeout; + + pm_runtime_get_sync(qspi->dev); + + while (count--) { + if (txbuf) { + pr_debug("tx cmd %08x dc %08x data %02x\n", + qspi->cmd | QSPI_WR_SNGL, qspi->dc, *txbuf); + dra7xxx_writel(qspi, *txbuf++, QSPI_SPI_DATA_REG); + dra7xxx_writel(qspi, qspi->dc, QSPI_SPI_DC_REG); + dra7xxx_writel(qspi, qspi->cmd | QSPI_WR_SNGL, + QSPI_SPI_CMD_REG); + status = dra7xxx_readl(qspi, QSPI_SPI_STATUS_REG); + timeout = QSPI_TIMEOUT; + while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) { + if (--timeout < 0) { + pr_debug("QSPI tx timed out\n"); + return -1; + } + status = dra7xxx_readl(qspi, QSPI_SPI_STATUS_REG); + } + pr_debug("tx done, status %08x\n", status); + } + if (rxbuf) { + pr_debug("rx cmd %08x dc %08x\n", + qspi->cmd | QSPI_RD_SNGL, qspi->dc); + dra7xxx_writel(qspi, qspi->dc, QSPI_SPI_DC_REG); + dra7xxx_writel(qspi, qspi->cmd | QSPI_RD_SNGL, + QSPI_SPI_CMD_REG); + status = dra7xxx_readl(qspi, QSPI_SPI_STATUS_REG); + timeout = QSPI_TIMEOUT; + while ((status & QSPI_WC_BUSY) != QSPI_XFER_DONE) { + if (--timeout < 0) { + pr_debug("QSPI rx timed out\n"); + return -1; + } + status = dra7xxx_readl(qspi, QSPI_SPI_STATUS_REG); + } + *rxbuf++ = dra7xxx_readl(qspi, QSPI_SPI_DATA_REG); + pr_debug("rx done, status %08x, read %02x\n", + status, *(rxbuf-1)); + } + } + + if (flags & XFER_END) + dra7xxx_writel(qspi, qspi->cmd | QSPI_INVAL, QSPI_SPI_CMD_REG); + + pm_runtime_mark_last_busy(qspi->dev); + pm_runtime_put_autosuspend(qspi->dev); + + return 0; +} + +static int dra7xxx_qspi_start_transfer_one(struct spi_master *master, + struct spi_message *m) +{ + struct dra7xxx_qspi *qspi = spi_master_get_devdata(master); + struct spi_device *spi = m->spi; + struct spi_transfer *t; + int status = 0; + int flags = 0; + + /* setup command reg */ + qspi->cmd = 0; + qspi->cmd |= QSPI_WLEN(8); + qspi->cmd |= QSPI_EN_CS(0); + qspi->cmd |= 0xfff; + + /* setup device control reg */ + qspi->dc = 0; + + if (spi->mode & SPI_CPHA) + qspi->dc |= QSPI_CKPHA(0); + if (spi->mode & SPI_CPOL) + qspi->dc |= QSPI_CKPOL(0); + if (spi->mode & SPI_CS_HIGH) + qspi->dc |= QSPI_CSPOL(0); + + list_for_each_entry(t, &m->transfers, transfer_list) { + if (list_is_last(&t->transfer_list, &m->transfers)) + flags = XFER_END; + + qspi_transfer_msg(qspi, t->len, t->tx_buf, + t->rx_buf, flags); + + m->actual_length += t->len; + } + m->status = status; + spi_finalize_current_message(master); + + return status; +} + +static int dra7xxx_qspi_probe(struct platform_device *pdev) +{ + struct dra7xxx_qspi *qspi; + struct spi_master *master; + struct resource *r; + struct device_node *np = pdev->dev.of_node; + u32 max_freq; + int ret; + + master = spi_alloc_master(&pdev->dev, sizeof(*qspi)); + if (!master) + return -ENOMEM; + + master->mode_bits = SPI_CPOL | SPI_CPHA; + + master->num_chipselect = 1; + master->bus_num = -1; + master->setup = dra7xxx_qspi_setup; + master->prepare_transfer_hardware = dra7xxx_qspi_prepare_xfer; + master->transfer_one_message = dra7xxx_qspi_start_transfer_one; + master->unprepare_transfer_hardware = dra7xxx_qspi_unprepare_xfer; + master->dev.of_node = pdev->dev.of_node; + + dev_set_drvdata(&pdev->dev, master); + + qspi = spi_master_get_devdata(master); + qspi->master = master; + qspi->dev = &pdev->dev; + + r = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (r == NULL) { + ret = -ENODEV; + goto free_master; + } + + qspi->base = devm_request_and_ioremap(&pdev->dev, r); + if (!qspi->base) { + dev_dbg(&pdev->dev, "can't ioremap MCSPI\n"); + ret = -ENOMEM; + goto free_master; + } + + pm_runtime_use_autosuspend(&pdev->dev); + pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT); + pm_runtime_enable(&pdev->dev); + + if (!of_property_read_u32(np, "spi-max-frequency", &max_freq)) + qspi->spi_max_frequency = max_freq; + + ret = spi_register_master(master); + if (ret) + goto free_master; + + return ret; + +free_master: + spi_master_put(master); + return ret; +} + +static int dra7xxx_qspi_remove(struct platform_device *pdev) +{ + struct dra7xxx_qspi *qspi = platform_get_drvdata(pdev); + + spi_unregister_master(qspi->master); + + return 0; +} + +static const struct of_device_id dra7xxx_qspi_match[] = { + {.compatible = "ti,dra7xxx-qspi" }, + {}, +}; +MODULE_DEVICE_TABLE(of, dra7xxx_qspi_match); + +static struct platform_driver dra7xxx_qspi_driver = { + .probe = dra7xxx_qspi_probe, + .remove = dra7xxx_qspi_remove, + .driver = { + .name = "ti,dra7xxx-qspi", + .owner = THIS_MODULE, + .of_match_table = dra7xxx_qspi_match, + } +}; + +module_platform_driver(dra7xxx_qspi_driver); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("TI QSPI controller driver");