···66 - "ti,omap4-spi" for OMAP4+.77- ti,spi-num-cs : Number of chipselect supported by the instance.88- ti,hwmods: Name of the hwmod associated to the McSPI99-99+- ti,pindir-d0-out-d1-in: Select the D0 pin as output and D1 as1010+ input. The default is D0 as input and1111+ D1 as output.10121113Example:1214
+14
drivers/spi/Kconfig
···392392 help393393 SPI driver for Freescale MXS devices.394394395395+config SPI_TEGRA20_SFLASH396396+ tristate "Nvidia Tegra20 Serial flash Controller"397397+ depends on ARCH_TEGRA398398+ help399399+ SPI driver for Nvidia Tegra20 Serial flash Controller interface.400400+ The main usecase of this controller is to use spi flash as boot401401+ device.402402+403403+config SPI_TEGRA20_SLINK404404+ tristate "Nvidia Tegra20/Tegra30 SLINK Controller"405405+ depends on ARCH_TEGRA && TEGRA20_APB_DMA406406+ help407407+ SPI driver for Nvidia Tegra20/Tegra30 SLINK Controller interface.408408+395409config SPI_TI_SSP396410 tristate "TI Sequencer Serial Port - SPI Support"397411 depends on MFD_TI_SSP
···129129 struct omap2_mcspi_dma *dma_channels;130130 struct device *dev;131131 struct omap2_mcspi_regs ctx;132132+ unsigned int pin_dir:1;132133};133134134135struct omap2_mcspi_cs {···323322 struct omap2_mcspi *mcspi;324323 struct omap2_mcspi_dma *mcspi_dma;325324 unsigned int count;326326- u8 * rx;327327- const u8 * tx;328328- void __iomem *chstat_reg;329329- struct omap2_mcspi_cs *cs = spi->controller_state;330325331326 mcspi = spi_master_get_devdata(spi->master);332327 mcspi_dma = &mcspi->dma_channels[spi->chip_select];333328 count = xfer->len;334334-335335- rx = xfer->rx_buf;336336- tx = xfer->tx_buf;337337- chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;338329339330 if (mcspi_dma->dma_tx) {340331 struct dma_async_tx_descriptor *tx;···351358 dma_async_issue_pending(mcspi_dma->dma_tx);352359 omap2_mcspi_set_dma_req(spi, 0, 1);353360354354- wait_for_completion(&mcspi_dma->dma_tx_completion);355355- dma_unmap_single(mcspi->dev, xfer->tx_dma, count,356356- DMA_TO_DEVICE);357357-358358- /* for TX_ONLY mode, be sure all words have shifted out */359359- if (rx == NULL) {360360- if (mcspi_wait_for_reg_bit(chstat_reg,361361- OMAP2_MCSPI_CHSTAT_TXS) < 0)362362- dev_err(&spi->dev, "TXS timed out\n");363363- else if (mcspi_wait_for_reg_bit(chstat_reg,364364- OMAP2_MCSPI_CHSTAT_EOT) < 0)365365- dev_err(&spi->dev, "EOT timed out\n");366366- }367361}368362369363static unsigned···471491 struct dma_slave_config cfg;472492 enum dma_slave_buswidth width;473493 unsigned es;494494+ void __iomem *chstat_reg;474495475496 mcspi = spi_master_get_devdata(spi->master);476497 mcspi_dma = &mcspi->dma_channels[spi->chip_select];···506525 omap2_mcspi_tx_dma(spi, xfer, cfg);507526508527 if (rx != NULL)509509- return omap2_mcspi_rx_dma(spi, xfer, cfg, es);528528+ count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);510529530530+ if (tx != NULL) {531531+ chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;532532+ wait_for_completion(&mcspi_dma->dma_tx_completion);533533+ dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,534534+ DMA_TO_DEVICE);535535+536536+ /* for TX_ONLY mode, be sure all words have shifted out */537537+ if (rx == NULL) {538538+ if (mcspi_wait_for_reg_bit(chstat_reg,539539+ OMAP2_MCSPI_CHSTAT_TXS) < 0)540540+ dev_err(&spi->dev, "TXS timed out\n");541541+ else if (mcspi_wait_for_reg_bit(chstat_reg,542542+ OMAP2_MCSPI_CHSTAT_EOT) < 0)543543+ dev_err(&spi->dev, "EOT timed out\n");544544+ }545545+ }511546 return count;512547}513548···761764 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS762765 * REVISIT: this controller could support SPI_3WIRE mode.763766 */764764- l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);765765- l |= OMAP2_MCSPI_CHCONF_DPE0;767767+ if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {768768+ l &= ~OMAP2_MCSPI_CHCONF_IS;769769+ l &= ~OMAP2_MCSPI_CHCONF_DPE1;770770+ l |= OMAP2_MCSPI_CHCONF_DPE0;771771+ } else {772772+ l |= OMAP2_MCSPI_CHCONF_IS;773773+ l |= OMAP2_MCSPI_CHCONF_DPE1;774774+ l &= ~OMAP2_MCSPI_CHCONF_DPE0;775775+ }766776767777 /* wordlength */768778 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;···11701166 master->cleanup = omap2_mcspi_cleanup;11711167 master->dev.of_node = node;1172116811691169+ dev_set_drvdata(&pdev->dev, master);11701170+11711171+ mcspi = spi_master_get_devdata(master);11721172+ mcspi->master = master;11731173+11731174 match = of_match_device(omap_mcspi_of_match, &pdev->dev);11741175 if (match) {11751176 u32 num_cs = 1; /* default number of chipselect */···11831174 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);11841175 master->num_chipselect = num_cs;11851176 master->bus_num = bus_num++;11771177+ if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))11781178+ mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;11861179 } else {11871180 pdata = pdev->dev.platform_data;11881181 master->num_chipselect = pdata->num_cs;11891182 if (pdev->id != -1)11901183 master->bus_num = pdev->id;11841184+ mcspi->pin_dir = pdata->pin_dir;11911185 }11921186 regs_offset = pdata->regs_offset;11931193-11941194- dev_set_drvdata(&pdev->dev, master);11951195-11961196- mcspi = spi_master_get_devdata(master);11971197- mcspi->master = master;1198118711991188 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);12001189 if (r == NULL) {
+36-16
drivers/spi/spi-pl022.c
···371371 /* Two optional pin states - default & sleep */372372 struct pinctrl *pinctrl;373373 struct pinctrl_state *pins_default;374374+ struct pinctrl_state *pins_idle;374375 struct pinctrl_state *pins_sleep;375376 struct spi_master *master;376377 struct pl022_ssp_controller *master_info;···21172116 } else21182117 dev_err(dev, "could not get default pinstate\n");2119211821192119+ pl022->pins_idle = pinctrl_lookup_state(pl022->pinctrl,21202120+ PINCTRL_STATE_IDLE);21212121+ if (IS_ERR(pl022->pins_idle))21222122+ dev_dbg(dev, "could not get idle pinstate\n");21232123+21202124 pl022->pins_sleep = pinctrl_lookup_state(pl022->pinctrl,21212125 PINCTRL_STATE_SLEEP);21222126 if (IS_ERR(pl022->pins_sleep))···22522246 pm_runtime_set_autosuspend_delay(dev,22532247 platform_info->autosuspend_delay);22542248 pm_runtime_use_autosuspend(dev);22552255- pm_runtime_put_autosuspend(dev);22562256- } else {22572257- pm_runtime_put(dev);22582249 }22502250+ pm_runtime_put(dev);22512251+22592252 return 0;2260225322612254 err_spi_register:···23082303 * the runtime counterparts to handle external resources like23092304 * clocks, pins and regulators when going to sleep.23102305 */23112311-static void pl022_suspend_resources(struct pl022 *pl022)23062306+static void pl022_suspend_resources(struct pl022 *pl022, bool runtime)23122307{23132308 int ret;23092309+ struct pinctrl_state *pins_state;2314231023152311 clk_disable(pl022->clk);2316231223132313+ pins_state = runtime ? pl022->pins_idle : pl022->pins_sleep;23172314 /* Optionally let pins go into sleep states */23182318- if (!IS_ERR(pl022->pins_sleep)) {23192319- ret = pinctrl_select_state(pl022->pinctrl,23202320- pl022->pins_sleep);23152315+ if (!IS_ERR(pins_state)) {23162316+ ret = pinctrl_select_state(pl022->pinctrl, pins_state);23212317 if (ret)23222322- dev_err(&pl022->adev->dev,23232323- "could not set pins to sleep state\n");23182318+ dev_err(&pl022->adev->dev, "could not set %s pins\n",23192319+ runtime ? "idle" : "sleep");23242320 }23252321}2326232223272327-static void pl022_resume_resources(struct pl022 *pl022)23232323+static void pl022_resume_resources(struct pl022 *pl022, bool runtime)23282324{23292325 int ret;2330232623312327 /* Optionaly enable pins to be muxed in and configured */23282328+ /* First go to the default state */23322329 if (!IS_ERR(pl022->pins_default)) {23332333- ret = pinctrl_select_state(pl022->pinctrl,23342334- pl022->pins_default);23302330+ ret = pinctrl_select_state(pl022->pinctrl, pl022->pins_default);23352331 if (ret)23362332 dev_err(&pl022->adev->dev,23372333 "could not set default pins\n");23342334+ }23352335+23362336+ if (!runtime) {23372337+ /* Then let's idle the pins until the next transfer happens */23382338+ if (!IS_ERR(pl022->pins_idle)) {23392339+ ret = pinctrl_select_state(pl022->pinctrl,23402340+ pl022->pins_idle);23412341+ if (ret)23422342+ dev_err(&pl022->adev->dev,23432343+ "could not set idle pins\n");23442344+ }23382345 }2339234623402347 clk_enable(pl022->clk);···23642347 dev_warn(dev, "cannot suspend master\n");23652348 return ret;23662349 }23672367- pl022_suspend_resources(pl022);23502350+23512351+ pm_runtime_get_sync(dev);23522352+ pl022_suspend_resources(pl022, false);2368235323692354 dev_dbg(dev, "suspended\n");23702355 return 0;···23772358 struct pl022 *pl022 = dev_get_drvdata(dev);23782359 int ret;2379236023802380- pl022_resume_resources(pl022);23612361+ pl022_resume_resources(pl022, false);23622362+ pm_runtime_put(dev);2381236323822364 /* Start the queue running */23832365 ret = spi_master_resume(pl022->master);···23962376{23972377 struct pl022 *pl022 = dev_get_drvdata(dev);2398237823992399- pl022_suspend_resources(pl022);23792379+ pl022_suspend_resources(pl022, true);24002380 return 0;24012381}24022382···24042384{24052385 struct pl022 *pl022 = dev_get_drvdata(dev);2406238624072407- pl022_resume_resources(pl022);23872387+ pl022_resume_resources(pl022, true);24082388 return 0;24092389}24102390#endif
+17-17
drivers/spi/spi-s3c64xx.c
···516516517517 /* Disable Clock */518518 if (sdd->port_conf->clk_from_cmu) {519519- clk_disable(sdd->src_clk);519519+ clk_disable_unprepare(sdd->src_clk);520520 } else {521521 val = readl(regs + S3C64XX_SPI_CLK_CFG);522522 val &= ~S3C64XX_SPI_ENCLK_ENABLE;···564564 /* There is half-multiplier before the SPI */565565 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);566566 /* Enable Clock */567567- clk_enable(sdd->src_clk);567567+ clk_prepare_enable(sdd->src_clk);568568 } else {569569 /* Configure Clock */570570 val = readl(regs + S3C64XX_SPI_CLK_CFG);···11121112 dev_err(dev, "invalid gpio[%d]: %d\n", idx, gpio);11131113 goto free_gpio;11141114 }11151115-11151115+ sdd->gpios[idx] = gpio;11161116 ret = gpio_request(gpio, "spi-bus");11171117 if (ret) {11181118 dev_err(dev, "gpio [%d] request failed: %d\n",···13021302 goto err3;13031303 }1304130413051305- if (clk_enable(sdd->clk)) {13051305+ if (clk_prepare_enable(sdd->clk)) {13061306 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");13071307 ret = -EBUSY;13081308 goto err4;···13171317 goto err5;13181318 }1319131913201320- if (clk_enable(sdd->src_clk)) {13201320+ if (clk_prepare_enable(sdd->src_clk)) {13211321 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);13221322 ret = -EBUSY;13231323 goto err6;···13611361err8:13621362 free_irq(irq, sdd);13631363err7:13641364- clk_disable(sdd->src_clk);13641364+ clk_disable_unprepare(sdd->src_clk);13651365err6:13661366 clk_put(sdd->src_clk);13671367err5:13681368- clk_disable(sdd->clk);13681368+ clk_disable_unprepare(sdd->clk);13691369err4:13701370 clk_put(sdd->clk);13711371err3:···1393139313941394 free_irq(platform_get_irq(pdev, 0), sdd);1395139513961396- clk_disable(sdd->src_clk);13961396+ clk_disable_unprepare(sdd->src_clk);13971397 clk_put(sdd->src_clk);1398139813991399- clk_disable(sdd->clk);13991399+ clk_disable_unprepare(sdd->clk);14001400 clk_put(sdd->clk);1401140114021402 if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)···14171417 spi_master_suspend(master);1418141814191419 /* Disable the clock */14201420- clk_disable(sdd->src_clk);14211421- clk_disable(sdd->clk);14201420+ clk_disable_unprepare(sdd->src_clk);14211421+ clk_disable_unprepare(sdd->clk);1422142214231423 if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)14241424 s3c64xx_spi_dt_gpio_free(sdd);···14401440 sci->cfg_gpio();1441144114421442 /* Enable the clock */14431443- clk_enable(sdd->src_clk);14441444- clk_enable(sdd->clk);14431443+ clk_prepare_enable(sdd->src_clk);14441444+ clk_prepare_enable(sdd->clk);1445144514461446 s3c64xx_spi_hwinit(sdd, sdd->port_id);14471447···14571457 struct spi_master *master = dev_get_drvdata(dev);14581458 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);1459145914601460- clk_disable(sdd->clk);14611461- clk_disable(sdd->src_clk);14601460+ clk_disable_unprepare(sdd->clk);14611461+ clk_disable_unprepare(sdd->src_clk);1462146214631463 return 0;14641464}···14681468 struct spi_master *master = dev_get_drvdata(dev);14691469 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);1470147014711471- clk_enable(sdd->src_clk);14721472- clk_enable(sdd->clk);14711471+ clk_prepare_enable(sdd->src_clk);14721472+ clk_prepare_enable(sdd->clk);1473147314741474 return 0;14751475}
+665
drivers/spi/spi-tegra20-sflash.c
···11+/*22+ * SPI driver for Nvidia's Tegra20 Serial Flash Controller.33+ *44+ * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.55+ *66+ * Author: Laxman Dewangan <ldewangan@nvidia.com>77+ *88+ * This program is free software; you can redistribute it and/or modify it99+ * under the terms and conditions of the GNU General Public License,1010+ * version 2, as published by the Free Software Foundation.1111+ *1212+ * This program is distributed in the hope it will be useful, but WITHOUT1313+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or1414+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for1515+ * more details.1616+ *1717+ * You should have received a copy of the GNU General Public License1818+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1919+ */2020+2121+#include <linux/clk.h>2222+#include <linux/completion.h>2323+#include <linux/delay.h>2424+#include <linux/err.h>2525+#include <linux/init.h>2626+#include <linux/interrupt.h>2727+#include <linux/io.h>2828+#include <linux/kernel.h>2929+#include <linux/kthread.h>3030+#include <linux/module.h>3131+#include <linux/platform_device.h>3232+#include <linux/pm_runtime.h>3333+#include <linux/of.h>3434+#include <linux/of_device.h>3535+#include <linux/spi/spi.h>3636+#include <linux/spi/spi-tegra.h>3737+#include <mach/clk.h>3838+3939+#define SPI_COMMAND 0x0004040+#define SPI_GO BIT(30)4141+#define SPI_M_S BIT(28)4242+#define SPI_ACTIVE_SCLK_MASK (0x3 << 26)4343+#define SPI_ACTIVE_SCLK_DRIVE_LOW (0 << 26)4444+#define SPI_ACTIVE_SCLK_DRIVE_HIGH (1 << 26)4545+#define SPI_ACTIVE_SCLK_PULL_LOW (2 << 26)4646+#define SPI_ACTIVE_SCLK_PULL_HIGH (3 << 26)4747+4848+#define SPI_CK_SDA_FALLING (1 << 21)4949+#define SPI_CK_SDA_RISING (0 << 21)5050+#define SPI_CK_SDA_MASK (1 << 21)5151+#define SPI_ACTIVE_SDA (0x3 << 18)5252+#define SPI_ACTIVE_SDA_DRIVE_LOW (0 << 18)5353+#define SPI_ACTIVE_SDA_DRIVE_HIGH (1 << 18)5454+#define SPI_ACTIVE_SDA_PULL_LOW (2 << 18)5555+#define SPI_ACTIVE_SDA_PULL_HIGH (3 << 18)5656+5757+#define SPI_CS_POL_INVERT BIT(16)5858+#define SPI_TX_EN BIT(15)5959+#define SPI_RX_EN BIT(14)6060+#define SPI_CS_VAL_HIGH BIT(13)6161+#define SPI_CS_VAL_LOW 0x06262+#define SPI_CS_SW BIT(12)6363+#define SPI_CS_HW 0x06464+#define SPI_CS_DELAY_MASK (7 << 9)6565+#define SPI_CS3_EN BIT(8)6666+#define SPI_CS2_EN BIT(7)6767+#define SPI_CS1_EN BIT(6)6868+#define SPI_CS0_EN BIT(5)6969+7070+#define SPI_CS_MASK (SPI_CS3_EN | SPI_CS2_EN | \7171+ SPI_CS1_EN | SPI_CS0_EN)7272+#define SPI_BIT_LENGTH(x) (((x) & 0x1f) << 0)7373+7474+#define SPI_MODES (SPI_ACTIVE_SCLK_MASK | SPI_CK_SDA_MASK)7575+7676+#define SPI_STATUS 0x0047777+#define SPI_BSY BIT(31)7878+#define SPI_RDY BIT(30)7979+#define SPI_TXF_FLUSH BIT(29)8080+#define SPI_RXF_FLUSH BIT(28)8181+#define SPI_RX_UNF BIT(27)8282+#define SPI_TX_OVF BIT(26)8383+#define SPI_RXF_EMPTY BIT(25)8484+#define SPI_RXF_FULL BIT(24)8585+#define SPI_TXF_EMPTY BIT(23)8686+#define SPI_TXF_FULL BIT(22)8787+#define SPI_BLK_CNT(count) (((count) & 0xffff) + 1)8888+8989+#define SPI_FIFO_ERROR (SPI_RX_UNF | SPI_TX_OVF)9090+#define SPI_FIFO_EMPTY (SPI_TX_EMPTY | SPI_RX_EMPTY)9191+9292+#define SPI_RX_CMP 0x89393+#define SPI_DMA_CTL 0x0C9494+#define SPI_DMA_EN BIT(31)9595+#define SPI_IE_RXC BIT(27)9696+#define SPI_IE_TXC BIT(26)9797+#define SPI_PACKED BIT(20)9898+#define SPI_RX_TRIG_MASK (0x3 << 18)9999+#define SPI_RX_TRIG_1W (0x0 << 18)100100+#define SPI_RX_TRIG_4W (0x1 << 18)101101+#define SPI_TX_TRIG_MASK (0x3 << 16)102102+#define SPI_TX_TRIG_1W (0x0 << 16)103103+#define SPI_TX_TRIG_4W (0x1 << 16)104104+#define SPI_DMA_BLK_COUNT(count) (((count) - 1) & 0xFFFF);105105+106106+#define SPI_TX_FIFO 0x10107107+#define SPI_RX_FIFO 0x20108108+109109+#define DATA_DIR_TX (1 << 0)110110+#define DATA_DIR_RX (1 << 1)111111+112112+#define MAX_CHIP_SELECT 4113113+#define SPI_FIFO_DEPTH 4114114+#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))115115+116116+struct tegra_sflash_data {117117+ struct device *dev;118118+ struct spi_master *master;119119+ spinlock_t lock;120120+121121+ struct clk *clk;122122+ void __iomem *base;123123+ unsigned irq;124124+ u32 spi_max_frequency;125125+ u32 cur_speed;126126+127127+ struct spi_device *cur_spi;128128+ unsigned cur_pos;129129+ unsigned cur_len;130130+ unsigned bytes_per_word;131131+ unsigned cur_direction;132132+ unsigned curr_xfer_words;133133+134134+ unsigned cur_rx_pos;135135+ unsigned cur_tx_pos;136136+137137+ u32 tx_status;138138+ u32 rx_status;139139+ u32 status_reg;140140+141141+ u32 def_command_reg;142142+ u32 command_reg;143143+ u32 dma_control_reg;144144+145145+ struct completion xfer_completion;146146+ struct spi_transfer *curr_xfer;147147+};148148+149149+static int tegra_sflash_runtime_suspend(struct device *dev);150150+static int tegra_sflash_runtime_resume(struct device *dev);151151+152152+static inline unsigned long tegra_sflash_readl(struct tegra_sflash_data *tsd,153153+ unsigned long reg)154154+{155155+ return readl(tsd->base + reg);156156+}157157+158158+static inline void tegra_sflash_writel(struct tegra_sflash_data *tsd,159159+ unsigned long val, unsigned long reg)160160+{161161+ writel(val, tsd->base + reg);162162+}163163+164164+static void tegra_sflash_clear_status(struct tegra_sflash_data *tsd)165165+{166166+ /* Write 1 to clear status register */167167+ tegra_sflash_writel(tsd, SPI_RDY | SPI_FIFO_ERROR, SPI_STATUS);168168+}169169+170170+static unsigned tegra_sflash_calculate_curr_xfer_param(171171+ struct spi_device *spi, struct tegra_sflash_data *tsd,172172+ struct spi_transfer *t)173173+{174174+ unsigned remain_len = t->len - tsd->cur_pos;175175+ unsigned max_word;176176+177177+ tsd->bytes_per_word = (t->bits_per_word - 1) / 8 + 1;178178+ max_word = remain_len / tsd->bytes_per_word;179179+ if (max_word > SPI_FIFO_DEPTH)180180+ max_word = SPI_FIFO_DEPTH;181181+ tsd->curr_xfer_words = max_word;182182+ return max_word;183183+}184184+185185+static unsigned tegra_sflash_fill_tx_fifo_from_client_txbuf(186186+ struct tegra_sflash_data *tsd, struct spi_transfer *t)187187+{188188+ unsigned nbytes;189189+ unsigned long status;190190+ unsigned max_n_32bit = tsd->curr_xfer_words;191191+ u8 *tx_buf = (u8 *)t->tx_buf + tsd->cur_tx_pos;192192+193193+ if (max_n_32bit > SPI_FIFO_DEPTH)194194+ max_n_32bit = SPI_FIFO_DEPTH;195195+ nbytes = max_n_32bit * tsd->bytes_per_word;196196+197197+ status = tegra_sflash_readl(tsd, SPI_STATUS);198198+ while (!(status & SPI_TXF_FULL)) {199199+ int i;200200+ unsigned int x = 0;201201+202202+ for (i = 0; nbytes && (i < tsd->bytes_per_word);203203+ i++, nbytes--)204204+ x |= ((*tx_buf++) << i*8);205205+ tegra_sflash_writel(tsd, x, SPI_TX_FIFO);206206+ if (!nbytes)207207+ break;208208+209209+ status = tegra_sflash_readl(tsd, SPI_STATUS);210210+ }211211+ tsd->cur_tx_pos += max_n_32bit * tsd->bytes_per_word;212212+ return max_n_32bit;213213+}214214+215215+static int tegra_sflash_read_rx_fifo_to_client_rxbuf(216216+ struct tegra_sflash_data *tsd, struct spi_transfer *t)217217+{218218+ unsigned long status;219219+ unsigned int read_words = 0;220220+ u8 *rx_buf = (u8 *)t->rx_buf + tsd->cur_rx_pos;221221+222222+ status = tegra_sflash_readl(tsd, SPI_STATUS);223223+ while (!(status & SPI_RXF_EMPTY)) {224224+ int i;225225+ unsigned long x;226226+227227+ x = tegra_sflash_readl(tsd, SPI_RX_FIFO);228228+ for (i = 0; (i < tsd->bytes_per_word); i++)229229+ *rx_buf++ = (x >> (i*8)) & 0xFF;230230+ read_words++;231231+ status = tegra_sflash_readl(tsd, SPI_STATUS);232232+ }233233+ tsd->cur_rx_pos += read_words * tsd->bytes_per_word;234234+ return 0;235235+}236236+237237+static int tegra_sflash_start_cpu_based_transfer(238238+ struct tegra_sflash_data *tsd, struct spi_transfer *t)239239+{240240+ unsigned long val = 0;241241+ unsigned cur_words;242242+243243+ if (tsd->cur_direction & DATA_DIR_TX)244244+ val |= SPI_IE_TXC;245245+246246+ if (tsd->cur_direction & DATA_DIR_RX)247247+ val |= SPI_IE_RXC;248248+249249+ tegra_sflash_writel(tsd, val, SPI_DMA_CTL);250250+ tsd->dma_control_reg = val;251251+252252+ if (tsd->cur_direction & DATA_DIR_TX)253253+ cur_words = tegra_sflash_fill_tx_fifo_from_client_txbuf(tsd, t);254254+ else255255+ cur_words = tsd->curr_xfer_words;256256+ val |= SPI_DMA_BLK_COUNT(cur_words);257257+ tegra_sflash_writel(tsd, val, SPI_DMA_CTL);258258+ tsd->dma_control_reg = val;259259+ val |= SPI_DMA_EN;260260+ tegra_sflash_writel(tsd, val, SPI_DMA_CTL);261261+ return 0;262262+}263263+264264+static int tegra_sflash_start_transfer_one(struct spi_device *spi,265265+ struct spi_transfer *t, bool is_first_of_msg,266266+ bool is_single_xfer)267267+{268268+ struct tegra_sflash_data *tsd = spi_master_get_devdata(spi->master);269269+ u32 speed;270270+ unsigned long command;271271+272272+ speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;273273+ if (!speed)274274+ speed = tsd->spi_max_frequency;275275+ if (speed != tsd->cur_speed) {276276+ clk_set_rate(tsd->clk, speed);277277+ tsd->cur_speed = speed;278278+ }279279+280280+ tsd->cur_spi = spi;281281+ tsd->cur_pos = 0;282282+ tsd->cur_rx_pos = 0;283283+ tsd->cur_tx_pos = 0;284284+ tsd->curr_xfer = t;285285+ tegra_sflash_calculate_curr_xfer_param(spi, tsd, t);286286+ if (is_first_of_msg) {287287+ command = tsd->def_command_reg;288288+ command |= SPI_BIT_LENGTH(t->bits_per_word - 1);289289+ command |= SPI_CS_VAL_HIGH;290290+291291+ command &= ~SPI_MODES;292292+ if (spi->mode & SPI_CPHA)293293+ command |= SPI_CK_SDA_FALLING;294294+295295+ if (spi->mode & SPI_CPOL)296296+ command |= SPI_ACTIVE_SCLK_DRIVE_HIGH;297297+ else298298+ command |= SPI_ACTIVE_SCLK_DRIVE_LOW;299299+ command |= SPI_CS0_EN << spi->chip_select;300300+ } else {301301+ command = tsd->command_reg;302302+ command &= ~SPI_BIT_LENGTH(~0);303303+ command |= SPI_BIT_LENGTH(t->bits_per_word - 1);304304+ command &= ~(SPI_RX_EN | SPI_TX_EN);305305+ }306306+307307+ tsd->cur_direction = 0;308308+ if (t->rx_buf) {309309+ command |= SPI_RX_EN;310310+ tsd->cur_direction |= DATA_DIR_RX;311311+ }312312+ if (t->tx_buf) {313313+ command |= SPI_TX_EN;314314+ tsd->cur_direction |= DATA_DIR_TX;315315+ }316316+ tegra_sflash_writel(tsd, command, SPI_COMMAND);317317+ tsd->command_reg = command;318318+319319+ return tegra_sflash_start_cpu_based_transfer(tsd, t);320320+}321321+322322+static int tegra_sflash_transfer_one_message(struct spi_master *master,323323+ struct spi_message *msg)324324+{325325+ bool is_first_msg = true;326326+ int single_xfer;327327+ struct tegra_sflash_data *tsd = spi_master_get_devdata(master);328328+ struct spi_transfer *xfer;329329+ struct spi_device *spi = msg->spi;330330+ int ret;331331+332332+ ret = pm_runtime_get_sync(tsd->dev);333333+ if (ret < 0) {334334+ dev_err(tsd->dev, "pm_runtime_get() failed, err = %d\n", ret);335335+ return ret;336336+ }337337+338338+ msg->status = 0;339339+ msg->actual_length = 0;340340+ single_xfer = list_is_singular(&msg->transfers);341341+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {342342+ INIT_COMPLETION(tsd->xfer_completion);343343+ ret = tegra_sflash_start_transfer_one(spi, xfer,344344+ is_first_msg, single_xfer);345345+ if (ret < 0) {346346+ dev_err(tsd->dev,347347+ "spi can not start transfer, err %d\n", ret);348348+ goto exit;349349+ }350350+ is_first_msg = false;351351+ ret = wait_for_completion_timeout(&tsd->xfer_completion,352352+ SPI_DMA_TIMEOUT);353353+ if (WARN_ON(ret == 0)) {354354+ dev_err(tsd->dev,355355+ "spi trasfer timeout, err %d\n", ret);356356+ ret = -EIO;357357+ goto exit;358358+ }359359+360360+ if (tsd->tx_status || tsd->rx_status) {361361+ dev_err(tsd->dev, "Error in Transfer\n");362362+ ret = -EIO;363363+ goto exit;364364+ }365365+ msg->actual_length += xfer->len;366366+ if (xfer->cs_change && xfer->delay_usecs) {367367+ tegra_sflash_writel(tsd, tsd->def_command_reg,368368+ SPI_COMMAND);369369+ udelay(xfer->delay_usecs);370370+ }371371+ }372372+ ret = 0;373373+exit:374374+ tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);375375+ msg->status = ret;376376+ spi_finalize_current_message(master);377377+ pm_runtime_put(tsd->dev);378378+ return ret;379379+}380380+381381+static irqreturn_t handle_cpu_based_xfer(struct tegra_sflash_data *tsd)382382+{383383+ struct spi_transfer *t = tsd->curr_xfer;384384+ unsigned long flags;385385+386386+ spin_lock_irqsave(&tsd->lock, flags);387387+ if (tsd->tx_status || tsd->rx_status || (tsd->status_reg & SPI_BSY)) {388388+ dev_err(tsd->dev,389389+ "CpuXfer ERROR bit set 0x%x\n", tsd->status_reg);390390+ dev_err(tsd->dev,391391+ "CpuXfer 0x%08x:0x%08x\n", tsd->command_reg,392392+ tsd->dma_control_reg);393393+ tegra_periph_reset_assert(tsd->clk);394394+ udelay(2);395395+ tegra_periph_reset_deassert(tsd->clk);396396+ complete(&tsd->xfer_completion);397397+ goto exit;398398+ }399399+400400+ if (tsd->cur_direction & DATA_DIR_RX)401401+ tegra_sflash_read_rx_fifo_to_client_rxbuf(tsd, t);402402+403403+ if (tsd->cur_direction & DATA_DIR_TX)404404+ tsd->cur_pos = tsd->cur_tx_pos;405405+ else406406+ tsd->cur_pos = tsd->cur_rx_pos;407407+408408+ if (tsd->cur_pos == t->len) {409409+ complete(&tsd->xfer_completion);410410+ goto exit;411411+ }412412+413413+ tegra_sflash_calculate_curr_xfer_param(tsd->cur_spi, tsd, t);414414+ tegra_sflash_start_cpu_based_transfer(tsd, t);415415+exit:416416+ spin_unlock_irqrestore(&tsd->lock, flags);417417+ return IRQ_HANDLED;418418+}419419+420420+static irqreturn_t tegra_sflash_isr(int irq, void *context_data)421421+{422422+ struct tegra_sflash_data *tsd = context_data;423423+424424+ tsd->status_reg = tegra_sflash_readl(tsd, SPI_STATUS);425425+ if (tsd->cur_direction & DATA_DIR_TX)426426+ tsd->tx_status = tsd->status_reg & SPI_TX_OVF;427427+428428+ if (tsd->cur_direction & DATA_DIR_RX)429429+ tsd->rx_status = tsd->status_reg & SPI_RX_UNF;430430+ tegra_sflash_clear_status(tsd);431431+432432+ return handle_cpu_based_xfer(tsd);433433+}434434+435435+static struct tegra_spi_platform_data *tegra_sflash_parse_dt(436436+ struct platform_device *pdev)437437+{438438+ struct tegra_spi_platform_data *pdata;439439+ struct device_node *np = pdev->dev.of_node;440440+ u32 max_freq;441441+442442+ pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);443443+ if (!pdata) {444444+ dev_err(&pdev->dev, "Memory alloc for pdata failed\n");445445+ return NULL;446446+ }447447+448448+ if (!of_property_read_u32(np, "spi-max-frequency", &max_freq))449449+ pdata->spi_max_frequency = max_freq;450450+451451+ return pdata;452452+}453453+454454+static struct of_device_id tegra_sflash_of_match[] __devinitconst = {455455+ { .compatible = "nvidia,tegra20-sflash", },456456+ {}457457+};458458+MODULE_DEVICE_TABLE(of, tegra_sflash_of_match);459459+460460+static int __devinit tegra_sflash_probe(struct platform_device *pdev)461461+{462462+ struct spi_master *master;463463+ struct tegra_sflash_data *tsd;464464+ struct resource *r;465465+ struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;466466+ int ret;467467+ const struct of_device_id *match;468468+469469+ match = of_match_device(of_match_ptr(tegra_sflash_of_match),470470+ &pdev->dev);471471+ if (!match) {472472+ dev_err(&pdev->dev, "Error: No device match found\n");473473+ return -ENODEV;474474+ }475475+476476+ if (!pdata && pdev->dev.of_node)477477+ pdata = tegra_sflash_parse_dt(pdev);478478+479479+ if (!pdata) {480480+ dev_err(&pdev->dev, "No platform data, exiting\n");481481+ return -ENODEV;482482+ }483483+484484+ if (!pdata->spi_max_frequency)485485+ pdata->spi_max_frequency = 25000000; /* 25MHz */486486+487487+ master = spi_alloc_master(&pdev->dev, sizeof(*tsd));488488+ if (!master) {489489+ dev_err(&pdev->dev, "master allocation failed\n");490490+ return -ENOMEM;491491+ }492492+493493+ /* the spi->mode bits understood by this driver: */494494+ master->mode_bits = SPI_CPOL | SPI_CPHA;495495+ master->transfer_one_message = tegra_sflash_transfer_one_message;496496+ master->num_chipselect = MAX_CHIP_SELECT;497497+ master->bus_num = -1;498498+499499+ dev_set_drvdata(&pdev->dev, master);500500+ tsd = spi_master_get_devdata(master);501501+ tsd->master = master;502502+ tsd->dev = &pdev->dev;503503+ spin_lock_init(&tsd->lock);504504+505505+ r = platform_get_resource(pdev, IORESOURCE_MEM, 0);506506+ if (!r) {507507+ dev_err(&pdev->dev, "No IO memory resource\n");508508+ ret = -ENODEV;509509+ goto exit_free_master;510510+ }511511+ tsd->base = devm_request_and_ioremap(&pdev->dev, r);512512+ if (!tsd->base) {513513+ dev_err(&pdev->dev,514514+ "Cannot request memregion/iomap dma address\n");515515+ ret = -EADDRNOTAVAIL;516516+ goto exit_free_master;517517+ }518518+519519+ tsd->irq = platform_get_irq(pdev, 0);520520+ ret = request_irq(tsd->irq, tegra_sflash_isr, 0,521521+ dev_name(&pdev->dev), tsd);522522+ if (ret < 0) {523523+ dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",524524+ tsd->irq);525525+ goto exit_free_master;526526+ }527527+528528+ tsd->clk = devm_clk_get(&pdev->dev, "spi");529529+ if (IS_ERR(tsd->clk)) {530530+ dev_err(&pdev->dev, "can not get clock\n");531531+ ret = PTR_ERR(tsd->clk);532532+ goto exit_free_irq;533533+ }534534+535535+ tsd->spi_max_frequency = pdata->spi_max_frequency;536536+ init_completion(&tsd->xfer_completion);537537+ pm_runtime_enable(&pdev->dev);538538+ if (!pm_runtime_enabled(&pdev->dev)) {539539+ ret = tegra_sflash_runtime_resume(&pdev->dev);540540+ if (ret)541541+ goto exit_pm_disable;542542+ }543543+544544+ ret = pm_runtime_get_sync(&pdev->dev);545545+ if (ret < 0) {546546+ dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);547547+ goto exit_pm_disable;548548+ }549549+550550+ /* Reset controller */551551+ tegra_periph_reset_assert(tsd->clk);552552+ udelay(2);553553+ tegra_periph_reset_deassert(tsd->clk);554554+555555+ tsd->def_command_reg = SPI_M_S | SPI_CS_SW;556556+ tegra_sflash_writel(tsd, tsd->def_command_reg, SPI_COMMAND);557557+ pm_runtime_put(&pdev->dev);558558+559559+ master->dev.of_node = pdev->dev.of_node;560560+ ret = spi_register_master(master);561561+ if (ret < 0) {562562+ dev_err(&pdev->dev, "can not register to master err %d\n", ret);563563+ goto exit_pm_disable;564564+ }565565+ return ret;566566+567567+exit_pm_disable:568568+ pm_runtime_disable(&pdev->dev);569569+ if (!pm_runtime_status_suspended(&pdev->dev))570570+ tegra_sflash_runtime_suspend(&pdev->dev);571571+exit_free_irq:572572+ free_irq(tsd->irq, tsd);573573+exit_free_master:574574+ spi_master_put(master);575575+ return ret;576576+}577577+578578+static int __devexit tegra_sflash_remove(struct platform_device *pdev)579579+{580580+ struct spi_master *master = dev_get_drvdata(&pdev->dev);581581+ struct tegra_sflash_data *tsd = spi_master_get_devdata(master);582582+583583+ free_irq(tsd->irq, tsd);584584+ spi_unregister_master(master);585585+586586+ pm_runtime_disable(&pdev->dev);587587+ if (!pm_runtime_status_suspended(&pdev->dev))588588+ tegra_sflash_runtime_suspend(&pdev->dev);589589+590590+ return 0;591591+}592592+593593+#ifdef CONFIG_PM_SLEEP594594+static int tegra_sflash_suspend(struct device *dev)595595+{596596+ struct spi_master *master = dev_get_drvdata(dev);597597+598598+ return spi_master_suspend(master);599599+}600600+601601+static int tegra_sflash_resume(struct device *dev)602602+{603603+ struct spi_master *master = dev_get_drvdata(dev);604604+ struct tegra_sflash_data *tsd = spi_master_get_devdata(master);605605+ int ret;606606+607607+ ret = pm_runtime_get_sync(dev);608608+ if (ret < 0) {609609+ dev_err(dev, "pm runtime failed, e = %d\n", ret);610610+ return ret;611611+ }612612+ tegra_sflash_writel(tsd, tsd->command_reg, SPI_COMMAND);613613+ pm_runtime_put(dev);614614+615615+ return spi_master_resume(master);616616+}617617+#endif618618+619619+static int tegra_sflash_runtime_suspend(struct device *dev)620620+{621621+ struct spi_master *master = dev_get_drvdata(dev);622622+ struct tegra_sflash_data *tsd = spi_master_get_devdata(master);623623+624624+ /* Flush all write which are in PPSB queue by reading back */625625+ tegra_sflash_readl(tsd, SPI_COMMAND);626626+627627+ clk_disable_unprepare(tsd->clk);628628+ return 0;629629+}630630+631631+static int tegra_sflash_runtime_resume(struct device *dev)632632+{633633+ struct spi_master *master = dev_get_drvdata(dev);634634+ struct tegra_sflash_data *tsd = spi_master_get_devdata(master);635635+ int ret;636636+637637+ ret = clk_prepare_enable(tsd->clk);638638+ if (ret < 0) {639639+ dev_err(tsd->dev, "clk_prepare failed: %d\n", ret);640640+ return ret;641641+ }642642+ return 0;643643+}644644+645645+static const struct dev_pm_ops slink_pm_ops = {646646+ SET_RUNTIME_PM_OPS(tegra_sflash_runtime_suspend,647647+ tegra_sflash_runtime_resume, NULL)648648+ SET_SYSTEM_SLEEP_PM_OPS(tegra_sflash_suspend, tegra_sflash_resume)649649+};650650+static struct platform_driver tegra_sflash_driver = {651651+ .driver = {652652+ .name = "spi-tegra-sflash",653653+ .owner = THIS_MODULE,654654+ .pm = &slink_pm_ops,655655+ .of_match_table = of_match_ptr(tegra_sflash_of_match),656656+ },657657+ .probe = tegra_sflash_probe,658658+ .remove = __devexit_p(tegra_sflash_remove),659659+};660660+module_platform_driver(tegra_sflash_driver);661661+662662+MODULE_ALIAS("platform:spi-tegra-sflash");663663+MODULE_DESCRIPTION("NVIDIA Tegra20 Serial Flash Controller Driver");664664+MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");665665+MODULE_LICENSE("GPL v2");
+1358
drivers/spi/spi-tegra20-slink.c
···11+/*22+ * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.33+ *44+ * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.55+ *66+ * This program is free software; you can redistribute it and/or modify it77+ * under the terms and conditions of the GNU General Public License,88+ * version 2, as published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope it will be useful, but WITHOUT1111+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or1212+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for1313+ * more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1717+ */1818+1919+#include <linux/clk.h>2020+#include <linux/completion.h>2121+#include <linux/delay.h>2222+#include <linux/dmaengine.h>2323+#include <linux/dma-mapping.h>2424+#include <linux/dmapool.h>2525+#include <linux/err.h>2626+#include <linux/init.h>2727+#include <linux/interrupt.h>2828+#include <linux/io.h>2929+#include <linux/kernel.h>3030+#include <linux/kthread.h>3131+#include <linux/module.h>3232+#include <linux/platform_device.h>3333+#include <linux/pm_runtime.h>3434+#include <linux/of.h>3535+#include <linux/of_device.h>3636+#include <linux/spi/spi.h>3737+#include <linux/spi/spi-tegra.h>3838+#include <mach/clk.h>3939+4040+#define SLINK_COMMAND 0x0004141+#define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)4242+#define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)4343+#define SLINK_BOTH_EN (1 << 10)4444+#define SLINK_CS_SW (1 << 11)4545+#define SLINK_CS_VALUE (1 << 12)4646+#define SLINK_CS_POLARITY (1 << 13)4747+#define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)4848+#define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)4949+#define SLINK_IDLE_SDA_PULL_LOW (2 << 16)5050+#define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)5151+#define SLINK_IDLE_SDA_MASK (3 << 16)5252+#define SLINK_CS_POLARITY1 (1 << 20)5353+#define SLINK_CK_SDA (1 << 21)5454+#define SLINK_CS_POLARITY2 (1 << 22)5555+#define SLINK_CS_POLARITY3 (1 << 23)5656+#define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)5757+#define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)5858+#define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)5959+#define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)6060+#define SLINK_IDLE_SCLK_MASK (3 << 24)6161+#define SLINK_M_S (1 << 28)6262+#define SLINK_WAIT (1 << 29)6363+#define SLINK_GO (1 << 30)6464+#define SLINK_ENB (1 << 31)6565+6666+#define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)6767+6868+#define SLINK_COMMAND2 0x0046969+#define SLINK_LSBFE (1 << 0)7070+#define SLINK_SSOE (1 << 1)7171+#define SLINK_SPIE (1 << 4)7272+#define SLINK_BIDIROE (1 << 6)7373+#define SLINK_MODFEN (1 << 7)7474+#define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)7575+#define SLINK_CS_ACTIVE_BETWEEN (1 << 17)7676+#define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)7777+#define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)7878+#define SLINK_FIFO_REFILLS_0 (0 << 22)7979+#define SLINK_FIFO_REFILLS_1 (1 << 22)8080+#define SLINK_FIFO_REFILLS_2 (2 << 22)8181+#define SLINK_FIFO_REFILLS_3 (3 << 22)8282+#define SLINK_FIFO_REFILLS_MASK (3 << 22)8383+#define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)8484+#define SLINK_SPC0 (1 << 29)8585+#define SLINK_TXEN (1 << 30)8686+#define SLINK_RXEN (1 << 31)8787+8888+#define SLINK_STATUS 0x0088989+#define SLINK_COUNT(val) (((val) >> 0) & 0x1f)9090+#define SLINK_WORD(val) (((val) >> 5) & 0x1f)9191+#define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)9292+#define SLINK_MODF (1 << 16)9393+#define SLINK_RX_UNF (1 << 18)9494+#define SLINK_TX_OVF (1 << 19)9595+#define SLINK_TX_FULL (1 << 20)9696+#define SLINK_TX_EMPTY (1 << 21)9797+#define SLINK_RX_FULL (1 << 22)9898+#define SLINK_RX_EMPTY (1 << 23)9999+#define SLINK_TX_UNF (1 << 24)100100+#define SLINK_RX_OVF (1 << 25)101101+#define SLINK_TX_FLUSH (1 << 26)102102+#define SLINK_RX_FLUSH (1 << 27)103103+#define SLINK_SCLK (1 << 28)104104+#define SLINK_ERR (1 << 29)105105+#define SLINK_RDY (1 << 30)106106+#define SLINK_BSY (1 << 31)107107+#define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \108108+ SLINK_TX_UNF | SLINK_RX_OVF)109109+110110+#define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)111111+112112+#define SLINK_MAS_DATA 0x010113113+#define SLINK_SLAVE_DATA 0x014114114+115115+#define SLINK_DMA_CTL 0x018116116+#define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)117117+#define SLINK_TX_TRIG_1 (0 << 16)118118+#define SLINK_TX_TRIG_4 (1 << 16)119119+#define SLINK_TX_TRIG_8 (2 << 16)120120+#define SLINK_TX_TRIG_16 (3 << 16)121121+#define SLINK_TX_TRIG_MASK (3 << 16)122122+#define SLINK_RX_TRIG_1 (0 << 18)123123+#define SLINK_RX_TRIG_4 (1 << 18)124124+#define SLINK_RX_TRIG_8 (2 << 18)125125+#define SLINK_RX_TRIG_16 (3 << 18)126126+#define SLINK_RX_TRIG_MASK (3 << 18)127127+#define SLINK_PACKED (1 << 20)128128+#define SLINK_PACK_SIZE_4 (0 << 21)129129+#define SLINK_PACK_SIZE_8 (1 << 21)130130+#define SLINK_PACK_SIZE_16 (2 << 21)131131+#define SLINK_PACK_SIZE_32 (3 << 21)132132+#define SLINK_PACK_SIZE_MASK (3 << 21)133133+#define SLINK_IE_TXC (1 << 26)134134+#define SLINK_IE_RXC (1 << 27)135135+#define SLINK_DMA_EN (1 << 31)136136+137137+#define SLINK_STATUS2 0x01c138138+#define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)139139+#define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)140140+#define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)141141+142142+#define SLINK_TX_FIFO 0x100143143+#define SLINK_RX_FIFO 0x180144144+145145+#define DATA_DIR_TX (1 << 0)146146+#define DATA_DIR_RX (1 << 1)147147+148148+#define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))149149+150150+#define DEFAULT_SPI_DMA_BUF_LEN (16*1024)151151+#define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)152152+#define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)153153+154154+#define SLINK_STATUS2_RESET \155155+ (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)156156+157157+#define MAX_CHIP_SELECT 4158158+#define SLINK_FIFO_DEPTH 32159159+160160+struct tegra_slink_chip_data {161161+ bool cs_hold_time;162162+};163163+164164+struct tegra_slink_data {165165+ struct device *dev;166166+ struct spi_master *master;167167+ const struct tegra_slink_chip_data *chip_data;168168+ spinlock_t lock;169169+170170+ struct clk *clk;171171+ void __iomem *base;172172+ phys_addr_t phys;173173+ unsigned irq;174174+ int dma_req_sel;175175+ u32 spi_max_frequency;176176+ u32 cur_speed;177177+178178+ struct spi_device *cur_spi;179179+ unsigned cur_pos;180180+ unsigned cur_len;181181+ unsigned words_per_32bit;182182+ unsigned bytes_per_word;183183+ unsigned curr_dma_words;184184+ unsigned cur_direction;185185+186186+ unsigned cur_rx_pos;187187+ unsigned cur_tx_pos;188188+189189+ unsigned dma_buf_size;190190+ unsigned max_buf_size;191191+ bool is_curr_dma_xfer;192192+ bool is_hw_based_cs;193193+194194+ struct completion rx_dma_complete;195195+ struct completion tx_dma_complete;196196+197197+ u32 tx_status;198198+ u32 rx_status;199199+ u32 status_reg;200200+ bool is_packed;201201+ unsigned long packed_size;202202+203203+ u32 command_reg;204204+ u32 command2_reg;205205+ u32 dma_control_reg;206206+ u32 def_command_reg;207207+ u32 def_command2_reg;208208+209209+ struct completion xfer_completion;210210+ struct spi_transfer *curr_xfer;211211+ struct dma_chan *rx_dma_chan;212212+ u32 *rx_dma_buf;213213+ dma_addr_t rx_dma_phys;214214+ struct dma_async_tx_descriptor *rx_dma_desc;215215+216216+ struct dma_chan *tx_dma_chan;217217+ u32 *tx_dma_buf;218218+ dma_addr_t tx_dma_phys;219219+ struct dma_async_tx_descriptor *tx_dma_desc;220220+};221221+222222+static int tegra_slink_runtime_suspend(struct device *dev);223223+static int tegra_slink_runtime_resume(struct device *dev);224224+225225+static inline unsigned long tegra_slink_readl(struct tegra_slink_data *tspi,226226+ unsigned long reg)227227+{228228+ return readl(tspi->base + reg);229229+}230230+231231+static inline void tegra_slink_writel(struct tegra_slink_data *tspi,232232+ unsigned long val, unsigned long reg)233233+{234234+ writel(val, tspi->base + reg);235235+236236+ /* Read back register to make sure that register writes completed */237237+ if (reg != SLINK_TX_FIFO)238238+ readl(tspi->base + SLINK_MAS_DATA);239239+}240240+241241+static void tegra_slink_clear_status(struct tegra_slink_data *tspi)242242+{243243+ unsigned long val;244244+ unsigned long val_write = 0;245245+246246+ val = tegra_slink_readl(tspi, SLINK_STATUS);247247+248248+ /* Write 1 to clear status register */249249+ val_write = SLINK_RDY | SLINK_FIFO_ERROR;250250+ tegra_slink_writel(tspi, val_write, SLINK_STATUS);251251+}252252+253253+static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,254254+ struct spi_transfer *t)255255+{256256+ unsigned long val;257257+258258+ switch (tspi->bytes_per_word) {259259+ case 0:260260+ val = SLINK_PACK_SIZE_4;261261+ break;262262+ case 1:263263+ val = SLINK_PACK_SIZE_8;264264+ break;265265+ case 2:266266+ val = SLINK_PACK_SIZE_16;267267+ break;268268+ case 4:269269+ val = SLINK_PACK_SIZE_32;270270+ break;271271+ default:272272+ val = 0;273273+ }274274+ return val;275275+}276276+277277+static unsigned tegra_slink_calculate_curr_xfer_param(278278+ struct spi_device *spi, struct tegra_slink_data *tspi,279279+ struct spi_transfer *t)280280+{281281+ unsigned remain_len = t->len - tspi->cur_pos;282282+ unsigned max_word;283283+ unsigned bits_per_word ;284284+ unsigned max_len;285285+ unsigned total_fifo_words;286286+287287+ bits_per_word = t->bits_per_word ? t->bits_per_word :288288+ spi->bits_per_word;289289+ tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;290290+291291+ if (bits_per_word == 8 || bits_per_word == 16) {292292+ tspi->is_packed = 1;293293+ tspi->words_per_32bit = 32/bits_per_word;294294+ } else {295295+ tspi->is_packed = 0;296296+ tspi->words_per_32bit = 1;297297+ }298298+ tspi->packed_size = tegra_slink_get_packed_size(tspi, t);299299+300300+ if (tspi->is_packed) {301301+ max_len = min(remain_len, tspi->max_buf_size);302302+ tspi->curr_dma_words = max_len/tspi->bytes_per_word;303303+ total_fifo_words = max_len/4;304304+ } else {305305+ max_word = (remain_len - 1) / tspi->bytes_per_word + 1;306306+ max_word = min(max_word, tspi->max_buf_size/4);307307+ tspi->curr_dma_words = max_word;308308+ total_fifo_words = max_word;309309+ }310310+ return total_fifo_words;311311+}312312+313313+static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(314314+ struct tegra_slink_data *tspi, struct spi_transfer *t)315315+{316316+ unsigned nbytes;317317+ unsigned tx_empty_count;318318+ unsigned long fifo_status;319319+ unsigned max_n_32bit;320320+ unsigned i, count;321321+ unsigned long x;322322+ unsigned int written_words;323323+ unsigned fifo_words_left;324324+ u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;325325+326326+ fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);327327+ tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);328328+329329+ if (tspi->is_packed) {330330+ fifo_words_left = tx_empty_count * tspi->words_per_32bit;331331+ written_words = min(fifo_words_left, tspi->curr_dma_words);332332+ nbytes = written_words * tspi->bytes_per_word;333333+ max_n_32bit = DIV_ROUND_UP(nbytes, 4);334334+ for (count = 0; count < max_n_32bit; count++) {335335+ x = 0;336336+ for (i = 0; (i < 4) && nbytes; i++, nbytes--)337337+ x |= (*tx_buf++) << (i*8);338338+ tegra_slink_writel(tspi, x, SLINK_TX_FIFO);339339+ }340340+ } else {341341+ max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);342342+ written_words = max_n_32bit;343343+ nbytes = written_words * tspi->bytes_per_word;344344+ for (count = 0; count < max_n_32bit; count++) {345345+ x = 0;346346+ for (i = 0; nbytes && (i < tspi->bytes_per_word);347347+ i++, nbytes--)348348+ x |= ((*tx_buf++) << i*8);349349+ tegra_slink_writel(tspi, x, SLINK_TX_FIFO);350350+ }351351+ }352352+ tspi->cur_tx_pos += written_words * tspi->bytes_per_word;353353+ return written_words;354354+}355355+356356+static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(357357+ struct tegra_slink_data *tspi, struct spi_transfer *t)358358+{359359+ unsigned rx_full_count;360360+ unsigned long fifo_status;361361+ unsigned i, count;362362+ unsigned long x;363363+ unsigned int read_words = 0;364364+ unsigned len;365365+ u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;366366+367367+ fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);368368+ rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);369369+ if (tspi->is_packed) {370370+ len = tspi->curr_dma_words * tspi->bytes_per_word;371371+ for (count = 0; count < rx_full_count; count++) {372372+ x = tegra_slink_readl(tspi, SLINK_RX_FIFO);373373+ for (i = 0; len && (i < 4); i++, len--)374374+ *rx_buf++ = (x >> i*8) & 0xFF;375375+ }376376+ tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;377377+ read_words += tspi->curr_dma_words;378378+ } else {379379+ unsigned int bits_per_word;380380+381381+ bits_per_word = t->bits_per_word ? t->bits_per_word :382382+ tspi->cur_spi->bits_per_word;383383+ for (count = 0; count < rx_full_count; count++) {384384+ x = tegra_slink_readl(tspi, SLINK_RX_FIFO);385385+ for (i = 0; (i < tspi->bytes_per_word); i++)386386+ *rx_buf++ = (x >> (i*8)) & 0xFF;387387+ }388388+ tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;389389+ read_words += rx_full_count;390390+ }391391+ return read_words;392392+}393393+394394+static void tegra_slink_copy_client_txbuf_to_spi_txbuf(395395+ struct tegra_slink_data *tspi, struct spi_transfer *t)396396+{397397+ unsigned len;398398+399399+ /* Make the dma buffer to read by cpu */400400+ dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,401401+ tspi->dma_buf_size, DMA_TO_DEVICE);402402+403403+ if (tspi->is_packed) {404404+ len = tspi->curr_dma_words * tspi->bytes_per_word;405405+ memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);406406+ } else {407407+ unsigned int i;408408+ unsigned int count;409409+ u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;410410+ unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;411411+ unsigned int x;412412+413413+ for (count = 0; count < tspi->curr_dma_words; count++) {414414+ x = 0;415415+ for (i = 0; consume && (i < tspi->bytes_per_word);416416+ i++, consume--)417417+ x |= ((*tx_buf++) << i * 8);418418+ tspi->tx_dma_buf[count] = x;419419+ }420420+ }421421+ tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;422422+423423+ /* Make the dma buffer to read by dma */424424+ dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,425425+ tspi->dma_buf_size, DMA_TO_DEVICE);426426+}427427+428428+static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(429429+ struct tegra_slink_data *tspi, struct spi_transfer *t)430430+{431431+ unsigned len;432432+433433+ /* Make the dma buffer to read by cpu */434434+ dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,435435+ tspi->dma_buf_size, DMA_FROM_DEVICE);436436+437437+ if (tspi->is_packed) {438438+ len = tspi->curr_dma_words * tspi->bytes_per_word;439439+ memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);440440+ } else {441441+ unsigned int i;442442+ unsigned int count;443443+ unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;444444+ unsigned int x;445445+ unsigned int rx_mask, bits_per_word;446446+447447+ bits_per_word = t->bits_per_word ? t->bits_per_word :448448+ tspi->cur_spi->bits_per_word;449449+ rx_mask = (1 << bits_per_word) - 1;450450+ for (count = 0; count < tspi->curr_dma_words; count++) {451451+ x = tspi->rx_dma_buf[count];452452+ x &= rx_mask;453453+ for (i = 0; (i < tspi->bytes_per_word); i++)454454+ *rx_buf++ = (x >> (i*8)) & 0xFF;455455+ }456456+ }457457+ tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;458458+459459+ /* Make the dma buffer to read by dma */460460+ dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,461461+ tspi->dma_buf_size, DMA_FROM_DEVICE);462462+}463463+464464+static void tegra_slink_dma_complete(void *args)465465+{466466+ struct completion *dma_complete = args;467467+468468+ complete(dma_complete);469469+}470470+471471+static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)472472+{473473+ INIT_COMPLETION(tspi->tx_dma_complete);474474+ tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,475475+ tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,476476+ DMA_PREP_INTERRUPT | DMA_CTRL_ACK);477477+ if (!tspi->tx_dma_desc) {478478+ dev_err(tspi->dev, "Not able to get desc for Tx\n");479479+ return -EIO;480480+ }481481+482482+ tspi->tx_dma_desc->callback = tegra_slink_dma_complete;483483+ tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;484484+485485+ dmaengine_submit(tspi->tx_dma_desc);486486+ dma_async_issue_pending(tspi->tx_dma_chan);487487+ return 0;488488+}489489+490490+static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)491491+{492492+ INIT_COMPLETION(tspi->rx_dma_complete);493493+ tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,494494+ tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,495495+ DMA_PREP_INTERRUPT | DMA_CTRL_ACK);496496+ if (!tspi->rx_dma_desc) {497497+ dev_err(tspi->dev, "Not able to get desc for Rx\n");498498+ return -EIO;499499+ }500500+501501+ tspi->rx_dma_desc->callback = tegra_slink_dma_complete;502502+ tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;503503+504504+ dmaengine_submit(tspi->rx_dma_desc);505505+ dma_async_issue_pending(tspi->rx_dma_chan);506506+ return 0;507507+}508508+509509+static int tegra_slink_start_dma_based_transfer(510510+ struct tegra_slink_data *tspi, struct spi_transfer *t)511511+{512512+ unsigned long val;513513+ unsigned long test_val;514514+ unsigned int len;515515+ int ret = 0;516516+ unsigned long status;517517+518518+ /* Make sure that Rx and Tx fifo are empty */519519+ status = tegra_slink_readl(tspi, SLINK_STATUS);520520+ if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {521521+ dev_err(tspi->dev,522522+ "Rx/Tx fifo are not empty status 0x%08lx\n", status);523523+ return -EIO;524524+ }525525+526526+ val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);527527+ val |= tspi->packed_size;528528+ if (tspi->is_packed)529529+ len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,530530+ 4) * 4;531531+ else532532+ len = tspi->curr_dma_words * 4;533533+534534+ /* Set attention level based on length of transfer */535535+ if (len & 0xF)536536+ val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;537537+ else if (((len) >> 4) & 0x1)538538+ val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;539539+ else540540+ val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;541541+542542+ if (tspi->cur_direction & DATA_DIR_TX)543543+ val |= SLINK_IE_TXC;544544+545545+ if (tspi->cur_direction & DATA_DIR_RX)546546+ val |= SLINK_IE_RXC;547547+548548+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);549549+ tspi->dma_control_reg = val;550550+551551+ if (tspi->cur_direction & DATA_DIR_TX) {552552+ tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);553553+ wmb();554554+ ret = tegra_slink_start_tx_dma(tspi, len);555555+ if (ret < 0) {556556+ dev_err(tspi->dev,557557+ "Starting tx dma failed, err %d\n", ret);558558+ return ret;559559+ }560560+561561+ /* Wait for tx fifo to be fill before starting slink */562562+ test_val = tegra_slink_readl(tspi, SLINK_STATUS);563563+ while (!(test_val & SLINK_TX_FULL))564564+ test_val = tegra_slink_readl(tspi, SLINK_STATUS);565565+ }566566+567567+ if (tspi->cur_direction & DATA_DIR_RX) {568568+ /* Make the dma buffer to read by dma */569569+ dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,570570+ tspi->dma_buf_size, DMA_FROM_DEVICE);571571+572572+ ret = tegra_slink_start_rx_dma(tspi, len);573573+ if (ret < 0) {574574+ dev_err(tspi->dev,575575+ "Starting rx dma failed, err %d\n", ret);576576+ if (tspi->cur_direction & DATA_DIR_TX)577577+ dmaengine_terminate_all(tspi->tx_dma_chan);578578+ return ret;579579+ }580580+ }581581+ tspi->is_curr_dma_xfer = true;582582+ if (tspi->is_packed) {583583+ val |= SLINK_PACKED;584584+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);585585+ /* HW need small delay after settign Packed mode */586586+ udelay(1);587587+ }588588+ tspi->dma_control_reg = val;589589+590590+ val |= SLINK_DMA_EN;591591+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);592592+ return ret;593593+}594594+595595+static int tegra_slink_start_cpu_based_transfer(596596+ struct tegra_slink_data *tspi, struct spi_transfer *t)597597+{598598+ unsigned long val;599599+ unsigned cur_words;600600+601601+ val = tspi->packed_size;602602+ if (tspi->cur_direction & DATA_DIR_TX)603603+ val |= SLINK_IE_TXC;604604+605605+ if (tspi->cur_direction & DATA_DIR_RX)606606+ val |= SLINK_IE_RXC;607607+608608+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);609609+ tspi->dma_control_reg = val;610610+611611+ if (tspi->cur_direction & DATA_DIR_TX)612612+ cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);613613+ else614614+ cur_words = tspi->curr_dma_words;615615+ val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);616616+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);617617+ tspi->dma_control_reg = val;618618+619619+ tspi->is_curr_dma_xfer = false;620620+ if (tspi->is_packed) {621621+ val |= SLINK_PACKED;622622+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);623623+ udelay(1);624624+ wmb();625625+ }626626+ tspi->dma_control_reg = val;627627+ val |= SLINK_DMA_EN;628628+ tegra_slink_writel(tspi, val, SLINK_DMA_CTL);629629+ return 0;630630+}631631+632632+static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,633633+ bool dma_to_memory)634634+{635635+ struct dma_chan *dma_chan;636636+ u32 *dma_buf;637637+ dma_addr_t dma_phys;638638+ int ret;639639+ struct dma_slave_config dma_sconfig;640640+ dma_cap_mask_t mask;641641+642642+ dma_cap_zero(mask);643643+ dma_cap_set(DMA_SLAVE, mask);644644+ dma_chan = dma_request_channel(mask, NULL, NULL);645645+ if (!dma_chan) {646646+ dev_err(tspi->dev,647647+ "Dma channel is not available, will try later\n");648648+ return -EPROBE_DEFER;649649+ }650650+651651+ dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,652652+ &dma_phys, GFP_KERNEL);653653+ if (!dma_buf) {654654+ dev_err(tspi->dev, " Not able to allocate the dma buffer\n");655655+ dma_release_channel(dma_chan);656656+ return -ENOMEM;657657+ }658658+659659+ dma_sconfig.slave_id = tspi->dma_req_sel;660660+ if (dma_to_memory) {661661+ dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;662662+ dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;663663+ dma_sconfig.src_maxburst = 0;664664+ } else {665665+ dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;666666+ dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;667667+ dma_sconfig.dst_maxburst = 0;668668+ }669669+670670+ ret = dmaengine_slave_config(dma_chan, &dma_sconfig);671671+ if (ret)672672+ goto scrub;673673+ if (dma_to_memory) {674674+ tspi->rx_dma_chan = dma_chan;675675+ tspi->rx_dma_buf = dma_buf;676676+ tspi->rx_dma_phys = dma_phys;677677+ } else {678678+ tspi->tx_dma_chan = dma_chan;679679+ tspi->tx_dma_buf = dma_buf;680680+ tspi->tx_dma_phys = dma_phys;681681+ }682682+ return 0;683683+684684+scrub:685685+ dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);686686+ dma_release_channel(dma_chan);687687+ return ret;688688+}689689+690690+static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,691691+ bool dma_to_memory)692692+{693693+ u32 *dma_buf;694694+ dma_addr_t dma_phys;695695+ struct dma_chan *dma_chan;696696+697697+ if (dma_to_memory) {698698+ dma_buf = tspi->rx_dma_buf;699699+ dma_chan = tspi->rx_dma_chan;700700+ dma_phys = tspi->rx_dma_phys;701701+ tspi->rx_dma_chan = NULL;702702+ tspi->rx_dma_buf = NULL;703703+ } else {704704+ dma_buf = tspi->tx_dma_buf;705705+ dma_chan = tspi->tx_dma_chan;706706+ dma_phys = tspi->tx_dma_phys;707707+ tspi->tx_dma_buf = NULL;708708+ tspi->tx_dma_chan = NULL;709709+ }710710+ if (!dma_chan)711711+ return;712712+713713+ dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);714714+ dma_release_channel(dma_chan);715715+}716716+717717+static int tegra_slink_start_transfer_one(struct spi_device *spi,718718+ struct spi_transfer *t, bool is_first_of_msg,719719+ bool is_single_xfer)720720+{721721+ struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);722722+ u32 speed;723723+ u8 bits_per_word;724724+ unsigned total_fifo_words;725725+ int ret;726726+ struct tegra_spi_device_controller_data *cdata = spi->controller_data;727727+ unsigned long command;728728+ unsigned long command2;729729+730730+ bits_per_word = t->bits_per_word;731731+ speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;732732+ if (!speed)733733+ speed = tspi->spi_max_frequency;734734+ if (speed != tspi->cur_speed) {735735+ clk_set_rate(tspi->clk, speed * 4);736736+ tspi->cur_speed = speed;737737+ }738738+739739+ tspi->cur_spi = spi;740740+ tspi->cur_pos = 0;741741+ tspi->cur_rx_pos = 0;742742+ tspi->cur_tx_pos = 0;743743+ tspi->curr_xfer = t;744744+ total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);745745+746746+ if (is_first_of_msg) {747747+ tegra_slink_clear_status(tspi);748748+749749+ command = tspi->def_command_reg;750750+ command |= SLINK_BIT_LENGTH(bits_per_word - 1);751751+752752+ command2 = tspi->def_command2_reg;753753+ command2 |= SLINK_SS_EN_CS(spi->chip_select);754754+755755+ /* possibly use the hw based chip select */756756+ tspi->is_hw_based_cs = false;757757+ if (cdata && cdata->is_hw_based_cs && is_single_xfer &&758758+ ((tspi->curr_dma_words * tspi->bytes_per_word) ==759759+ (t->len - tspi->cur_pos))) {760760+ int setup_count;761761+ int sts2;762762+763763+ setup_count = cdata->cs_setup_clk_count >> 1;764764+ setup_count = max(setup_count, 3);765765+ command2 |= SLINK_SS_SETUP(setup_count);766766+ if (tspi->chip_data->cs_hold_time) {767767+ int hold_count;768768+769769+ hold_count = cdata->cs_hold_clk_count;770770+ hold_count = max(hold_count, 0xF);771771+ sts2 = tegra_slink_readl(tspi, SLINK_STATUS2);772772+ sts2 &= ~SLINK_SS_HOLD_TIME(0xF);773773+ sts2 |= SLINK_SS_HOLD_TIME(hold_count);774774+ tegra_slink_writel(tspi, sts2, SLINK_STATUS2);775775+ }776776+ tspi->is_hw_based_cs = true;777777+ }778778+779779+ if (tspi->is_hw_based_cs)780780+ command &= ~SLINK_CS_SW;781781+ else782782+ command |= SLINK_CS_SW | SLINK_CS_VALUE;783783+784784+ command &= ~SLINK_MODES;785785+ if (spi->mode & SPI_CPHA)786786+ command |= SLINK_CK_SDA;787787+788788+ if (spi->mode & SPI_CPOL)789789+ command |= SLINK_IDLE_SCLK_DRIVE_HIGH;790790+ else791791+ command |= SLINK_IDLE_SCLK_DRIVE_LOW;792792+ } else {793793+ command = tspi->command_reg;794794+ command &= ~SLINK_BIT_LENGTH(~0);795795+ command |= SLINK_BIT_LENGTH(bits_per_word - 1);796796+797797+ command2 = tspi->command2_reg;798798+ command2 &= ~(SLINK_RXEN | SLINK_TXEN);799799+ }800800+801801+ tegra_slink_writel(tspi, command, SLINK_COMMAND);802802+ tspi->command_reg = command;803803+804804+ tspi->cur_direction = 0;805805+ if (t->rx_buf) {806806+ command2 |= SLINK_RXEN;807807+ tspi->cur_direction |= DATA_DIR_RX;808808+ }809809+ if (t->tx_buf) {810810+ command2 |= SLINK_TXEN;811811+ tspi->cur_direction |= DATA_DIR_TX;812812+ }813813+ tegra_slink_writel(tspi, command2, SLINK_COMMAND2);814814+ tspi->command2_reg = command2;815815+816816+ if (total_fifo_words > SLINK_FIFO_DEPTH)817817+ ret = tegra_slink_start_dma_based_transfer(tspi, t);818818+ else819819+ ret = tegra_slink_start_cpu_based_transfer(tspi, t);820820+ return ret;821821+}822822+823823+static int tegra_slink_setup(struct spi_device *spi)824824+{825825+ struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);826826+ unsigned long val;827827+ unsigned long flags;828828+ int ret;829829+ unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {830830+ SLINK_CS_POLARITY,831831+ SLINK_CS_POLARITY1,832832+ SLINK_CS_POLARITY2,833833+ SLINK_CS_POLARITY3,834834+ };835835+836836+ dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",837837+ spi->bits_per_word,838838+ spi->mode & SPI_CPOL ? "" : "~",839839+ spi->mode & SPI_CPHA ? "" : "~",840840+ spi->max_speed_hz);841841+842842+ BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);843843+844844+ ret = pm_runtime_get_sync(tspi->dev);845845+ if (ret < 0) {846846+ dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);847847+ return ret;848848+ }849849+850850+ spin_lock_irqsave(&tspi->lock, flags);851851+ val = tspi->def_command_reg;852852+ if (spi->mode & SPI_CS_HIGH)853853+ val |= cs_pol_bit[spi->chip_select];854854+ else855855+ val &= ~cs_pol_bit[spi->chip_select];856856+ tspi->def_command_reg = val;857857+ tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);858858+ spin_unlock_irqrestore(&tspi->lock, flags);859859+860860+ pm_runtime_put(tspi->dev);861861+ return 0;862862+}863863+864864+static int tegra_slink_prepare_transfer(struct spi_master *master)865865+{866866+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);867867+868868+ return pm_runtime_get_sync(tspi->dev);869869+}870870+871871+static int tegra_slink_unprepare_transfer(struct spi_master *master)872872+{873873+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);874874+875875+ pm_runtime_put(tspi->dev);876876+ return 0;877877+}878878+879879+static int tegra_slink_transfer_one_message(struct spi_master *master,880880+ struct spi_message *msg)881881+{882882+ bool is_first_msg = true;883883+ int single_xfer;884884+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);885885+ struct spi_transfer *xfer;886886+ struct spi_device *spi = msg->spi;887887+ int ret;888888+889889+ msg->status = 0;890890+ msg->actual_length = 0;891891+ single_xfer = list_is_singular(&msg->transfers);892892+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {893893+ INIT_COMPLETION(tspi->xfer_completion);894894+ ret = tegra_slink_start_transfer_one(spi, xfer,895895+ is_first_msg, single_xfer);896896+ if (ret < 0) {897897+ dev_err(tspi->dev,898898+ "spi can not start transfer, err %d\n", ret);899899+ goto exit;900900+ }901901+ is_first_msg = false;902902+ ret = wait_for_completion_timeout(&tspi->xfer_completion,903903+ SLINK_DMA_TIMEOUT);904904+ if (WARN_ON(ret == 0)) {905905+ dev_err(tspi->dev,906906+ "spi trasfer timeout, err %d\n", ret);907907+ ret = -EIO;908908+ goto exit;909909+ }910910+911911+ if (tspi->tx_status || tspi->rx_status) {912912+ dev_err(tspi->dev, "Error in Transfer\n");913913+ ret = -EIO;914914+ goto exit;915915+ }916916+ msg->actual_length += xfer->len;917917+ if (xfer->cs_change && xfer->delay_usecs) {918918+ tegra_slink_writel(tspi, tspi->def_command_reg,919919+ SLINK_COMMAND);920920+ udelay(xfer->delay_usecs);921921+ }922922+ }923923+ ret = 0;924924+exit:925925+ tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);926926+ tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);927927+ msg->status = ret;928928+ spi_finalize_current_message(master);929929+ return ret;930930+}931931+932932+static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)933933+{934934+ struct spi_transfer *t = tspi->curr_xfer;935935+ unsigned long flags;936936+937937+ spin_lock_irqsave(&tspi->lock, flags);938938+ if (tspi->tx_status || tspi->rx_status ||939939+ (tspi->status_reg & SLINK_BSY)) {940940+ dev_err(tspi->dev,941941+ "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);942942+ dev_err(tspi->dev,943943+ "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,944944+ tspi->command2_reg, tspi->dma_control_reg);945945+ tegra_periph_reset_assert(tspi->clk);946946+ udelay(2);947947+ tegra_periph_reset_deassert(tspi->clk);948948+ complete(&tspi->xfer_completion);949949+ goto exit;950950+ }951951+952952+ if (tspi->cur_direction & DATA_DIR_RX)953953+ tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);954954+955955+ if (tspi->cur_direction & DATA_DIR_TX)956956+ tspi->cur_pos = tspi->cur_tx_pos;957957+ else958958+ tspi->cur_pos = tspi->cur_rx_pos;959959+960960+ if (tspi->cur_pos == t->len) {961961+ complete(&tspi->xfer_completion);962962+ goto exit;963963+ }964964+965965+ tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);966966+ tegra_slink_start_cpu_based_transfer(tspi, t);967967+exit:968968+ spin_unlock_irqrestore(&tspi->lock, flags);969969+ return IRQ_HANDLED;970970+}971971+972972+static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)973973+{974974+ struct spi_transfer *t = tspi->curr_xfer;975975+ long wait_status;976976+ int err = 0;977977+ unsigned total_fifo_words;978978+ unsigned long flags;979979+980980+ /* Abort dmas if any error */981981+ if (tspi->cur_direction & DATA_DIR_TX) {982982+ if (tspi->tx_status) {983983+ dmaengine_terminate_all(tspi->tx_dma_chan);984984+ err += 1;985985+ } else {986986+ wait_status = wait_for_completion_interruptible_timeout(987987+ &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);988988+ if (wait_status <= 0) {989989+ dmaengine_terminate_all(tspi->tx_dma_chan);990990+ dev_err(tspi->dev, "TxDma Xfer failed\n");991991+ err += 1;992992+ }993993+ }994994+ }995995+996996+ if (tspi->cur_direction & DATA_DIR_RX) {997997+ if (tspi->rx_status) {998998+ dmaengine_terminate_all(tspi->rx_dma_chan);999999+ err += 2;10001000+ } else {10011001+ wait_status = wait_for_completion_interruptible_timeout(10021002+ &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);10031003+ if (wait_status <= 0) {10041004+ dmaengine_terminate_all(tspi->rx_dma_chan);10051005+ dev_err(tspi->dev, "RxDma Xfer failed\n");10061006+ err += 2;10071007+ }10081008+ }10091009+ }10101010+10111011+ spin_lock_irqsave(&tspi->lock, flags);10121012+ if (err) {10131013+ dev_err(tspi->dev,10141014+ "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);10151015+ dev_err(tspi->dev,10161016+ "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,10171017+ tspi->command2_reg, tspi->dma_control_reg);10181018+ tegra_periph_reset_assert(tspi->clk);10191019+ udelay(2);10201020+ tegra_periph_reset_deassert(tspi->clk);10211021+ complete(&tspi->xfer_completion);10221022+ spin_unlock_irqrestore(&tspi->lock, flags);10231023+ return IRQ_HANDLED;10241024+ }10251025+10261026+ if (tspi->cur_direction & DATA_DIR_RX)10271027+ tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);10281028+10291029+ if (tspi->cur_direction & DATA_DIR_TX)10301030+ tspi->cur_pos = tspi->cur_tx_pos;10311031+ else10321032+ tspi->cur_pos = tspi->cur_rx_pos;10331033+10341034+ if (tspi->cur_pos == t->len) {10351035+ complete(&tspi->xfer_completion);10361036+ goto exit;10371037+ }10381038+10391039+ /* Continue transfer in current message */10401040+ total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,10411041+ tspi, t);10421042+ if (total_fifo_words > SLINK_FIFO_DEPTH)10431043+ err = tegra_slink_start_dma_based_transfer(tspi, t);10441044+ else10451045+ err = tegra_slink_start_cpu_based_transfer(tspi, t);10461046+10471047+exit:10481048+ spin_unlock_irqrestore(&tspi->lock, flags);10491049+ return IRQ_HANDLED;10501050+}10511051+10521052+static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)10531053+{10541054+ struct tegra_slink_data *tspi = context_data;10551055+10561056+ if (!tspi->is_curr_dma_xfer)10571057+ return handle_cpu_based_xfer(tspi);10581058+ return handle_dma_based_xfer(tspi);10591059+}10601060+10611061+static irqreturn_t tegra_slink_isr(int irq, void *context_data)10621062+{10631063+ struct tegra_slink_data *tspi = context_data;10641064+10651065+ tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);10661066+ if (tspi->cur_direction & DATA_DIR_TX)10671067+ tspi->tx_status = tspi->status_reg &10681068+ (SLINK_TX_OVF | SLINK_TX_UNF);10691069+10701070+ if (tspi->cur_direction & DATA_DIR_RX)10711071+ tspi->rx_status = tspi->status_reg &10721072+ (SLINK_RX_OVF | SLINK_RX_UNF);10731073+ tegra_slink_clear_status(tspi);10741074+10751075+ return IRQ_WAKE_THREAD;10761076+}10771077+10781078+static struct tegra_spi_platform_data *tegra_slink_parse_dt(10791079+ struct platform_device *pdev)10801080+{10811081+ struct tegra_spi_platform_data *pdata;10821082+ const unsigned int *prop;10831083+ struct device_node *np = pdev->dev.of_node;10841084+ u32 of_dma[2];10851085+10861086+ pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);10871087+ if (!pdata) {10881088+ dev_err(&pdev->dev, "Memory alloc for pdata failed\n");10891089+ return NULL;10901090+ }10911091+10921092+ if (of_property_read_u32_array(np, "nvidia,dma-request-selector",10931093+ of_dma, 2) >= 0)10941094+ pdata->dma_req_sel = of_dma[1];10951095+10961096+ prop = of_get_property(np, "spi-max-frequency", NULL);10971097+ if (prop)10981098+ pdata->spi_max_frequency = be32_to_cpup(prop);10991099+11001100+ return pdata;11011101+}11021102+11031103+const struct tegra_slink_chip_data tegra30_spi_cdata = {11041104+ .cs_hold_time = true,11051105+};11061106+11071107+const struct tegra_slink_chip_data tegra20_spi_cdata = {11081108+ .cs_hold_time = false,11091109+};11101110+11111111+static struct of_device_id tegra_slink_of_match[] __devinitconst = {11121112+ { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },11131113+ { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },11141114+ {}11151115+};11161116+MODULE_DEVICE_TABLE(of, tegra_slink_of_match);11171117+11181118+static int __devinit tegra_slink_probe(struct platform_device *pdev)11191119+{11201120+ struct spi_master *master;11211121+ struct tegra_slink_data *tspi;11221122+ struct resource *r;11231123+ struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;11241124+ int ret, spi_irq;11251125+ const struct tegra_slink_chip_data *cdata = NULL;11261126+ const struct of_device_id *match;11271127+11281128+ match = of_match_device(of_match_ptr(tegra_slink_of_match), &pdev->dev);11291129+ if (!match) {11301130+ dev_err(&pdev->dev, "Error: No device match found\n");11311131+ return -ENODEV;11321132+ }11331133+ cdata = match->data;11341134+ if (!pdata && pdev->dev.of_node)11351135+ pdata = tegra_slink_parse_dt(pdev);11361136+11371137+ if (!pdata) {11381138+ dev_err(&pdev->dev, "No platform data, exiting\n");11391139+ return -ENODEV;11401140+ }11411141+11421142+ if (!pdata->spi_max_frequency)11431143+ pdata->spi_max_frequency = 25000000; /* 25MHz */11441144+11451145+ master = spi_alloc_master(&pdev->dev, sizeof(*tspi));11461146+ if (!master) {11471147+ dev_err(&pdev->dev, "master allocation failed\n");11481148+ return -ENOMEM;11491149+ }11501150+11511151+ /* the spi->mode bits understood by this driver: */11521152+ master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;11531153+ master->setup = tegra_slink_setup;11541154+ master->prepare_transfer_hardware = tegra_slink_prepare_transfer;11551155+ master->transfer_one_message = tegra_slink_transfer_one_message;11561156+ master->unprepare_transfer_hardware = tegra_slink_unprepare_transfer;11571157+ master->num_chipselect = MAX_CHIP_SELECT;11581158+ master->bus_num = -1;11591159+11601160+ dev_set_drvdata(&pdev->dev, master);11611161+ tspi = spi_master_get_devdata(master);11621162+ tspi->master = master;11631163+ tspi->dma_req_sel = pdata->dma_req_sel;11641164+ tspi->dev = &pdev->dev;11651165+ tspi->chip_data = cdata;11661166+ spin_lock_init(&tspi->lock);11671167+11681168+ r = platform_get_resource(pdev, IORESOURCE_MEM, 0);11691169+ if (!r) {11701170+ dev_err(&pdev->dev, "No IO memory resource\n");11711171+ ret = -ENODEV;11721172+ goto exit_free_master;11731173+ }11741174+ tspi->phys = r->start;11751175+ tspi->base = devm_request_and_ioremap(&pdev->dev, r);11761176+ if (!tspi->base) {11771177+ dev_err(&pdev->dev,11781178+ "Cannot request memregion/iomap dma address\n");11791179+ ret = -EADDRNOTAVAIL;11801180+ goto exit_free_master;11811181+ }11821182+11831183+ spi_irq = platform_get_irq(pdev, 0);11841184+ tspi->irq = spi_irq;11851185+ ret = request_threaded_irq(tspi->irq, tegra_slink_isr,11861186+ tegra_slink_isr_thread, IRQF_ONESHOT,11871187+ dev_name(&pdev->dev), tspi);11881188+ if (ret < 0) {11891189+ dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",11901190+ tspi->irq);11911191+ goto exit_free_master;11921192+ }11931193+11941194+ tspi->clk = devm_clk_get(&pdev->dev, "slink");11951195+ if (IS_ERR(tspi->clk)) {11961196+ dev_err(&pdev->dev, "can not get clock\n");11971197+ ret = PTR_ERR(tspi->clk);11981198+ goto exit_free_irq;11991199+ }12001200+12011201+ tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;12021202+ tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;12031203+ tspi->spi_max_frequency = pdata->spi_max_frequency;12041204+12051205+ if (pdata->dma_req_sel) {12061206+ ret = tegra_slink_init_dma_param(tspi, true);12071207+ if (ret < 0) {12081208+ dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);12091209+ goto exit_free_irq;12101210+ }12111211+12121212+ ret = tegra_slink_init_dma_param(tspi, false);12131213+ if (ret < 0) {12141214+ dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);12151215+ goto exit_rx_dma_free;12161216+ }12171217+ tspi->max_buf_size = tspi->dma_buf_size;12181218+ init_completion(&tspi->tx_dma_complete);12191219+ init_completion(&tspi->rx_dma_complete);12201220+ }12211221+12221222+ init_completion(&tspi->xfer_completion);12231223+12241224+ pm_runtime_enable(&pdev->dev);12251225+ if (!pm_runtime_enabled(&pdev->dev)) {12261226+ ret = tegra_slink_runtime_resume(&pdev->dev);12271227+ if (ret)12281228+ goto exit_pm_disable;12291229+ }12301230+12311231+ ret = pm_runtime_get_sync(&pdev->dev);12321232+ if (ret < 0) {12331233+ dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);12341234+ goto exit_pm_disable;12351235+ }12361236+ tspi->def_command_reg = SLINK_M_S;12371237+ tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;12381238+ tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);12391239+ tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);12401240+ pm_runtime_put(&pdev->dev);12411241+12421242+ master->dev.of_node = pdev->dev.of_node;12431243+ ret = spi_register_master(master);12441244+ if (ret < 0) {12451245+ dev_err(&pdev->dev, "can not register to master err %d\n", ret);12461246+ goto exit_pm_disable;12471247+ }12481248+ return ret;12491249+12501250+exit_pm_disable:12511251+ pm_runtime_disable(&pdev->dev);12521252+ if (!pm_runtime_status_suspended(&pdev->dev))12531253+ tegra_slink_runtime_suspend(&pdev->dev);12541254+ tegra_slink_deinit_dma_param(tspi, false);12551255+exit_rx_dma_free:12561256+ tegra_slink_deinit_dma_param(tspi, true);12571257+exit_free_irq:12581258+ free_irq(spi_irq, tspi);12591259+exit_free_master:12601260+ spi_master_put(master);12611261+ return ret;12621262+}12631263+12641264+static int __devexit tegra_slink_remove(struct platform_device *pdev)12651265+{12661266+ struct spi_master *master = dev_get_drvdata(&pdev->dev);12671267+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);12681268+12691269+ free_irq(tspi->irq, tspi);12701270+ spi_unregister_master(master);12711271+12721272+ if (tspi->tx_dma_chan)12731273+ tegra_slink_deinit_dma_param(tspi, false);12741274+12751275+ if (tspi->rx_dma_chan)12761276+ tegra_slink_deinit_dma_param(tspi, true);12771277+12781278+ pm_runtime_disable(&pdev->dev);12791279+ if (!pm_runtime_status_suspended(&pdev->dev))12801280+ tegra_slink_runtime_suspend(&pdev->dev);12811281+12821282+ return 0;12831283+}12841284+12851285+#ifdef CONFIG_PM_SLEEP12861286+static int tegra_slink_suspend(struct device *dev)12871287+{12881288+ struct spi_master *master = dev_get_drvdata(dev);12891289+12901290+ return spi_master_suspend(master);12911291+}12921292+12931293+static int tegra_slink_resume(struct device *dev)12941294+{12951295+ struct spi_master *master = dev_get_drvdata(dev);12961296+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);12971297+ int ret;12981298+12991299+ ret = pm_runtime_get_sync(dev);13001300+ if (ret < 0) {13011301+ dev_err(dev, "pm runtime failed, e = %d\n", ret);13021302+ return ret;13031303+ }13041304+ tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);13051305+ tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);13061306+ pm_runtime_put(dev);13071307+13081308+ return spi_master_resume(master);13091309+}13101310+#endif13111311+13121312+static int tegra_slink_runtime_suspend(struct device *dev)13131313+{13141314+ struct spi_master *master = dev_get_drvdata(dev);13151315+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);13161316+13171317+ /* Flush all write which are in PPSB queue by reading back */13181318+ tegra_slink_readl(tspi, SLINK_MAS_DATA);13191319+13201320+ clk_disable_unprepare(tspi->clk);13211321+ return 0;13221322+}13231323+13241324+static int tegra_slink_runtime_resume(struct device *dev)13251325+{13261326+ struct spi_master *master = dev_get_drvdata(dev);13271327+ struct tegra_slink_data *tspi = spi_master_get_devdata(master);13281328+ int ret;13291329+13301330+ ret = clk_prepare_enable(tspi->clk);13311331+ if (ret < 0) {13321332+ dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);13331333+ return ret;13341334+ }13351335+ return 0;13361336+}13371337+13381338+static const struct dev_pm_ops slink_pm_ops = {13391339+ SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,13401340+ tegra_slink_runtime_resume, NULL)13411341+ SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)13421342+};13431343+static struct platform_driver tegra_slink_driver = {13441344+ .driver = {13451345+ .name = "spi-tegra-slink",13461346+ .owner = THIS_MODULE,13471347+ .pm = &slink_pm_ops,13481348+ .of_match_table = of_match_ptr(tegra_slink_of_match),13491349+ },13501350+ .probe = tegra_slink_probe,13511351+ .remove = __devexit_p(tegra_slink_remove),13521352+};13531353+module_platform_driver(tegra_slink_driver);13541354+13551355+MODULE_ALIAS("platform:spi-tegra-slink");13561356+MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");13571357+MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");13581358+MODULE_LICENSE("GPL v2");
+13-3
drivers/spi/spi.c
···12041204int spi_setup(struct spi_device *spi)12051205{12061206 unsigned bad_bits;12071207- int status;12071207+ int status = 0;1208120812091209 /* help drivers fail *cleanly* when they need options12101210 * that aren't supported with their current master···12191219 if (!spi->bits_per_word)12201220 spi->bits_per_word = 8;1221122112221222- status = spi->master->setup(spi);12221222+ if (spi->master->setup)12231223+ status = spi->master->setup(spi);1223122412241225 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"12251226 "%u bits/w, %u Hz max --> %d\n",···12391238static int __spi_async(struct spi_device *spi, struct spi_message *message)12401239{12411240 struct spi_master *master = spi->master;12411241+ struct spi_transfer *xfer;1242124212431243 /* Half-duplex links include original MicroWire, and ones with12441244 * only one data pin like SPI_3WIRE (switches direction) or where···12481246 */12491247 if ((master->flags & SPI_MASTER_HALF_DUPLEX)12501248 || (spi->mode & SPI_3WIRE)) {12511251- struct spi_transfer *xfer;12521249 unsigned flags = master->flags;1253125012541251 list_for_each_entry(xfer, &message->transfers, transfer_list) {···12581257 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)12591258 return -EINVAL;12601259 }12601260+ }12611261+12621262+ /**12631263+ * Set transfer bits_per_word as spi device default if it is not12641264+ * set for this transfer.12651265+ */12661266+ list_for_each_entry(xfer, &message->transfers, transfer_list) {12671267+ if (!xfer->bits_per_word)12681268+ xfer->bits_per_word = spi->bits_per_word;12611269 }1262127012631271 message->spi = spi;
···7788#define OMAP4_MCSPI_REG_OFFSET 0x100991010+#define MCSPI_PINDIR_D0_IN_D1_OUT 01111+#define MCSPI_PINDIR_D0_OUT_D1_IN 11212+1013struct omap2_mcspi_platform_config {1114 unsigned short num_cs;1215 unsigned int regs_offset;1616+ unsigned int pin_dir:1;1317};14181519struct omap2_mcspi_dev_attr {
+40
include/linux/spi/spi-tegra.h
···11+/*22+ * spi-tegra.h: SPI interface for Nvidia Tegra20 SLINK controller.33+ *44+ * Copyright (C) 2011 NVIDIA Corporation55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or99+ * (at your option) any later version.1010+ *1111+ * This program is distributed in the hope that it will be useful, but WITHOUT1212+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or1313+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for1414+ * more details.1515+ *1616+ * You should have received a copy of the GNU General Public License along1717+ * with this program; if not, write to the Free Software Foundation, Inc.,1818+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.1919+ */2020+2121+#ifndef _LINUX_SPI_TEGRA_H2222+#define _LINUX_SPI_TEGRA_H2323+2424+struct tegra_spi_platform_data {2525+ int dma_req_sel;2626+ unsigned int spi_max_frequency;2727+};2828+2929+/*3030+ * Controller data from device to pass some info like3131+ * hw based chip select can be used or not and if yes3232+ * then CS hold and setup time.3333+ */3434+struct tegra_spi_device_controller_data {3535+ bool is_hw_based_cs;3636+ int cs_setup_clk_count;3737+ int cs_hold_clk_count;3838+};3939+4040+#endif /* _LINUX_SPI_TEGRA_H */