1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ca632f55SGrant Likely /* 3ca632f55SGrant Likely * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs 4a0d2642eSMika Westerberg * Copyright (C) 2013, Intel Corporation 5ca632f55SGrant Likely */ 6ca632f55SGrant Likely 75ce25705SAndy Shevchenko #include <linux/acpi.h> 88b136baaSJarkko Nikula #include <linux/bitops.h> 95ce25705SAndy Shevchenko #include <linux/clk.h> 105ce25705SAndy Shevchenko #include <linux/delay.h> 11ca632f55SGrant Likely #include <linux/device.h> 120e476871SAndy Shevchenko #include <linux/dmaengine.h> 13cbfd6a21SSachin Kamat #include <linux/err.h> 145ce25705SAndy Shevchenko #include <linux/errno.h> 155ce25705SAndy Shevchenko #include <linux/gpio/consumer.h> 165ce25705SAndy Shevchenko #include <linux/gpio.h> 175ce25705SAndy Shevchenko #include <linux/init.h> 18ca632f55SGrant Likely #include <linux/interrupt.h> 195ce25705SAndy Shevchenko #include <linux/ioport.h> 209df461ecSAndy Shevchenko #include <linux/kernel.h> 215ce25705SAndy Shevchenko #include <linux/module.h> 22ae8fbf1dSAndy Shevchenko #include <linux/mod_devicetable.h> 23ae8fbf1dSAndy Shevchenko #include <linux/of.h> 2434cadd9cSJarkko Nikula #include <linux/pci.h> 25ca632f55SGrant Likely #include <linux/platform_device.h> 265ce25705SAndy Shevchenko #include <linux/pm_runtime.h> 27f2faa3ecSAndy Shevchenko #include <linux/property.h> 285ce25705SAndy Shevchenko #include <linux/slab.h> 290e476871SAndy Shevchenko 30ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h> 31ca632f55SGrant Likely #include <linux/spi/spi.h> 32ca632f55SGrant Likely 33cd7bed00SMika Westerberg #include "spi-pxa2xx.h" 34ca632f55SGrant Likely 35ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street"); 36ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); 37ca632f55SGrant Likely MODULE_LICENSE("GPL"); 38ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi"); 39ca632f55SGrant Likely 40ca632f55SGrant Likely #define TIMOUT_DFLT 1000 41ca632f55SGrant Likely 42ca632f55SGrant Likely /* 43ca632f55SGrant Likely * for testing SSCR1 changes that require SSP restart, basically 44ca632f55SGrant Likely * everything except the service and interrupt enables, the pxa270 developer 45ca632f55SGrant Likely * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this 46ca632f55SGrant Likely * list, but the PXA255 dev man says all bits without really meaning the 47ca632f55SGrant Likely * service and interrupt enables 48ca632f55SGrant Likely */ 49ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 50ca632f55SGrant Likely | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 51ca632f55SGrant Likely | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 52ca632f55SGrant Likely | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 53ca632f55SGrant Likely | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ 54ca632f55SGrant Likely | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 55ca632f55SGrant Likely 56e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ 57e5262d05SWeike Chen | QUARK_X1000_SSCR1_EFWR \ 58e5262d05SWeike Chen | QUARK_X1000_SSCR1_RFT \ 59e5262d05SWeike Chen | QUARK_X1000_SSCR1_TFT \ 60e5262d05SWeike Chen | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 61e5262d05SWeike Chen 627c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 637c7289a4SAndy Shevchenko | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 647c7289a4SAndy Shevchenko | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 657c7289a4SAndy Shevchenko | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 667c7289a4SAndy Shevchenko | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \ 677c7289a4SAndy Shevchenko | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 687c7289a4SAndy Shevchenko 69624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) 70624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE BIT(0) 71624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH BIT(1) 728b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT 9 738b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT) 74a0d2642eSMika Westerberg 75683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE 0x38 76683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3 77683f65deSEvan Green #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3 78683f65deSEvan Green 79dccf7369SJarkko Nikula struct lpss_config { 80dccf7369SJarkko Nikula /* LPSS offset from drv_data->ioaddr */ 81dccf7369SJarkko Nikula unsigned offset; 82dccf7369SJarkko Nikula /* Register offsets from drv_data->lpss_base or -1 */ 83dccf7369SJarkko Nikula int reg_general; 84dccf7369SJarkko Nikula int reg_ssp; 85dccf7369SJarkko Nikula int reg_cs_ctrl; 868b136baaSJarkko Nikula int reg_capabilities; 87dccf7369SJarkko Nikula /* FIFO thresholds */ 88dccf7369SJarkko Nikula u32 rx_threshold; 89dccf7369SJarkko Nikula u32 tx_threshold_lo; 90dccf7369SJarkko Nikula u32 tx_threshold_hi; 91c1e4a53cSMika Westerberg /* Chip select control */ 92c1e4a53cSMika Westerberg unsigned cs_sel_shift; 93c1e4a53cSMika Westerberg unsigned cs_sel_mask; 9430f3a6abSMika Westerberg unsigned cs_num; 95683f65deSEvan Green /* Quirks */ 96683f65deSEvan Green unsigned cs_clk_stays_gated : 1; 97dccf7369SJarkko Nikula }; 98dccf7369SJarkko Nikula 99dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */ 100dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = { 101dccf7369SJarkko Nikula { /* LPSS_LPT_SSP */ 102dccf7369SJarkko Nikula .offset = 0x800, 103dccf7369SJarkko Nikula .reg_general = 0x08, 104dccf7369SJarkko Nikula .reg_ssp = 0x0c, 105dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 1068b136baaSJarkko Nikula .reg_capabilities = -1, 107dccf7369SJarkko Nikula .rx_threshold = 64, 108dccf7369SJarkko Nikula .tx_threshold_lo = 160, 109dccf7369SJarkko Nikula .tx_threshold_hi = 224, 110dccf7369SJarkko Nikula }, 111dccf7369SJarkko Nikula { /* LPSS_BYT_SSP */ 112dccf7369SJarkko Nikula .offset = 0x400, 113dccf7369SJarkko Nikula .reg_general = 0x08, 114dccf7369SJarkko Nikula .reg_ssp = 0x0c, 115dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 1168b136baaSJarkko Nikula .reg_capabilities = -1, 117dccf7369SJarkko Nikula .rx_threshold = 64, 118dccf7369SJarkko Nikula .tx_threshold_lo = 160, 119dccf7369SJarkko Nikula .tx_threshold_hi = 224, 120dccf7369SJarkko Nikula }, 12130f3a6abSMika Westerberg { /* LPSS_BSW_SSP */ 12230f3a6abSMika Westerberg .offset = 0x400, 12330f3a6abSMika Westerberg .reg_general = 0x08, 12430f3a6abSMika Westerberg .reg_ssp = 0x0c, 12530f3a6abSMika Westerberg .reg_cs_ctrl = 0x18, 12630f3a6abSMika Westerberg .reg_capabilities = -1, 12730f3a6abSMika Westerberg .rx_threshold = 64, 12830f3a6abSMika Westerberg .tx_threshold_lo = 160, 12930f3a6abSMika Westerberg .tx_threshold_hi = 224, 13030f3a6abSMika Westerberg .cs_sel_shift = 2, 13130f3a6abSMika Westerberg .cs_sel_mask = 1 << 2, 13230f3a6abSMika Westerberg .cs_num = 2, 13330f3a6abSMika Westerberg }, 13434cadd9cSJarkko Nikula { /* LPSS_SPT_SSP */ 13534cadd9cSJarkko Nikula .offset = 0x200, 13634cadd9cSJarkko Nikula .reg_general = -1, 13734cadd9cSJarkko Nikula .reg_ssp = 0x20, 13834cadd9cSJarkko Nikula .reg_cs_ctrl = 0x24, 13966ec246eSJarkko Nikula .reg_capabilities = -1, 14034cadd9cSJarkko Nikula .rx_threshold = 1, 14134cadd9cSJarkko Nikula .tx_threshold_lo = 32, 14234cadd9cSJarkko Nikula .tx_threshold_hi = 56, 14334cadd9cSJarkko Nikula }, 144b7c08cf8SJarkko Nikula { /* LPSS_BXT_SSP */ 145b7c08cf8SJarkko Nikula .offset = 0x200, 146b7c08cf8SJarkko Nikula .reg_general = -1, 147b7c08cf8SJarkko Nikula .reg_ssp = 0x20, 148b7c08cf8SJarkko Nikula .reg_cs_ctrl = 0x24, 149b7c08cf8SJarkko Nikula .reg_capabilities = 0xfc, 150b7c08cf8SJarkko Nikula .rx_threshold = 1, 151b7c08cf8SJarkko Nikula .tx_threshold_lo = 16, 152b7c08cf8SJarkko Nikula .tx_threshold_hi = 48, 153c1e4a53cSMika Westerberg .cs_sel_shift = 8, 154c1e4a53cSMika Westerberg .cs_sel_mask = 3 << 8, 1556eefaee4SEvan Green .cs_clk_stays_gated = true, 156b7c08cf8SJarkko Nikula }, 157fc0b2accSJarkko Nikula { /* LPSS_CNL_SSP */ 158fc0b2accSJarkko Nikula .offset = 0x200, 159fc0b2accSJarkko Nikula .reg_general = -1, 160fc0b2accSJarkko Nikula .reg_ssp = 0x20, 161fc0b2accSJarkko Nikula .reg_cs_ctrl = 0x24, 162fc0b2accSJarkko Nikula .reg_capabilities = 0xfc, 163fc0b2accSJarkko Nikula .rx_threshold = 1, 164fc0b2accSJarkko Nikula .tx_threshold_lo = 32, 165fc0b2accSJarkko Nikula .tx_threshold_hi = 56, 166fc0b2accSJarkko Nikula .cs_sel_shift = 8, 167fc0b2accSJarkko Nikula .cs_sel_mask = 3 << 8, 168683f65deSEvan Green .cs_clk_stays_gated = true, 169fc0b2accSJarkko Nikula }, 170dccf7369SJarkko Nikula }; 171dccf7369SJarkko Nikula 172dccf7369SJarkko Nikula static inline const struct lpss_config 173dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data) 174dccf7369SJarkko Nikula { 175dccf7369SJarkko Nikula return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; 176dccf7369SJarkko Nikula } 177dccf7369SJarkko Nikula 178a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data) 179a0d2642eSMika Westerberg { 18003fbf488SJarkko Nikula switch (drv_data->ssp_type) { 18103fbf488SJarkko Nikula case LPSS_LPT_SSP: 18203fbf488SJarkko Nikula case LPSS_BYT_SSP: 18330f3a6abSMika Westerberg case LPSS_BSW_SSP: 18434cadd9cSJarkko Nikula case LPSS_SPT_SSP: 185b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 186fc0b2accSJarkko Nikula case LPSS_CNL_SSP: 18703fbf488SJarkko Nikula return true; 18803fbf488SJarkko Nikula default: 18903fbf488SJarkko Nikula return false; 19003fbf488SJarkko Nikula } 191a0d2642eSMika Westerberg } 192a0d2642eSMika Westerberg 193e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data) 194e5262d05SWeike Chen { 195e5262d05SWeike Chen return drv_data->ssp_type == QUARK_X1000_SSP; 196e5262d05SWeike Chen } 197e5262d05SWeike Chen 19841c98841SAndy Shevchenko static bool is_mmp2_ssp(const struct driver_data *drv_data) 19941c98841SAndy Shevchenko { 20041c98841SAndy Shevchenko return drv_data->ssp_type == MMP2_SSP; 20141c98841SAndy Shevchenko } 20241c98841SAndy Shevchenko 2031bed378cSAndy Shevchenko static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value) 2041bed378cSAndy Shevchenko { 2051bed378cSAndy Shevchenko if ((pxa2xx_spi_read(drv_data, reg) & mask) != value) 2061bed378cSAndy Shevchenko pxa2xx_spi_write(drv_data, reg, value & mask); 2071bed378cSAndy Shevchenko } 2081bed378cSAndy Shevchenko 2094fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) 2104fdb2424SWeike Chen { 2114fdb2424SWeike Chen switch (drv_data->ssp_type) { 212e5262d05SWeike Chen case QUARK_X1000_SSP: 213e5262d05SWeike Chen return QUARK_X1000_SSCR1_CHANGE_MASK; 2147c7289a4SAndy Shevchenko case CE4100_SSP: 2157c7289a4SAndy Shevchenko return CE4100_SSCR1_CHANGE_MASK; 2164fdb2424SWeike Chen default: 2174fdb2424SWeike Chen return SSCR1_CHANGE_MASK; 2184fdb2424SWeike Chen } 2194fdb2424SWeike Chen } 2204fdb2424SWeike Chen 2214fdb2424SWeike Chen static u32 2224fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) 2234fdb2424SWeike Chen { 2244fdb2424SWeike Chen switch (drv_data->ssp_type) { 225e5262d05SWeike Chen case QUARK_X1000_SSP: 226e5262d05SWeike Chen return RX_THRESH_QUARK_X1000_DFLT; 2277c7289a4SAndy Shevchenko case CE4100_SSP: 2287c7289a4SAndy Shevchenko return RX_THRESH_CE4100_DFLT; 2294fdb2424SWeike Chen default: 2304fdb2424SWeike Chen return RX_THRESH_DFLT; 2314fdb2424SWeike Chen } 2324fdb2424SWeike Chen } 2334fdb2424SWeike Chen 2344fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) 2354fdb2424SWeike Chen { 2364fdb2424SWeike Chen u32 mask; 2374fdb2424SWeike Chen 2384fdb2424SWeike Chen switch (drv_data->ssp_type) { 239e5262d05SWeike Chen case QUARK_X1000_SSP: 240e5262d05SWeike Chen mask = QUARK_X1000_SSSR_TFL_MASK; 241e5262d05SWeike Chen break; 2427c7289a4SAndy Shevchenko case CE4100_SSP: 2437c7289a4SAndy Shevchenko mask = CE4100_SSSR_TFL_MASK; 2447c7289a4SAndy Shevchenko break; 2454fdb2424SWeike Chen default: 2464fdb2424SWeike Chen mask = SSSR_TFL_MASK; 2474fdb2424SWeike Chen break; 2484fdb2424SWeike Chen } 2494fdb2424SWeike Chen 250*6d380132SAndy Shevchenko return read_SSSR_bits(drv_data, mask) == mask; 2514fdb2424SWeike Chen } 2524fdb2424SWeike Chen 2534fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, 2544fdb2424SWeike Chen u32 *sccr1_reg) 2554fdb2424SWeike Chen { 2564fdb2424SWeike Chen u32 mask; 2574fdb2424SWeike Chen 2584fdb2424SWeike Chen switch (drv_data->ssp_type) { 259e5262d05SWeike Chen case QUARK_X1000_SSP: 260e5262d05SWeike Chen mask = QUARK_X1000_SSCR1_RFT; 261e5262d05SWeike Chen break; 2627c7289a4SAndy Shevchenko case CE4100_SSP: 2637c7289a4SAndy Shevchenko mask = CE4100_SSCR1_RFT; 2647c7289a4SAndy Shevchenko break; 2654fdb2424SWeike Chen default: 2664fdb2424SWeike Chen mask = SSCR1_RFT; 2674fdb2424SWeike Chen break; 2684fdb2424SWeike Chen } 2694fdb2424SWeike Chen *sccr1_reg &= ~mask; 2704fdb2424SWeike Chen } 2714fdb2424SWeike Chen 2724fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, 2734fdb2424SWeike Chen u32 *sccr1_reg, u32 threshold) 2744fdb2424SWeike Chen { 2754fdb2424SWeike Chen switch (drv_data->ssp_type) { 276e5262d05SWeike Chen case QUARK_X1000_SSP: 277e5262d05SWeike Chen *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); 278e5262d05SWeike Chen break; 2797c7289a4SAndy Shevchenko case CE4100_SSP: 2807c7289a4SAndy Shevchenko *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold); 2817c7289a4SAndy Shevchenko break; 2824fdb2424SWeike Chen default: 2834fdb2424SWeike Chen *sccr1_reg |= SSCR1_RxTresh(threshold); 2844fdb2424SWeike Chen break; 2854fdb2424SWeike Chen } 2864fdb2424SWeike Chen } 2874fdb2424SWeike Chen 2884fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, 2894fdb2424SWeike Chen u32 clk_div, u8 bits) 2904fdb2424SWeike Chen { 2914fdb2424SWeike Chen switch (drv_data->ssp_type) { 292e5262d05SWeike Chen case QUARK_X1000_SSP: 293e5262d05SWeike Chen return clk_div 294e5262d05SWeike Chen | QUARK_X1000_SSCR0_Motorola 2950c8ccd8bSAndy Shevchenko | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits); 2964fdb2424SWeike Chen default: 2974fdb2424SWeike Chen return clk_div 2984fdb2424SWeike Chen | SSCR0_Motorola 2994fdb2424SWeike Chen | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) 3004fdb2424SWeike Chen | (bits > 16 ? SSCR0_EDSS : 0); 3014fdb2424SWeike Chen } 3024fdb2424SWeike Chen } 3034fdb2424SWeike Chen 304a0d2642eSMika Westerberg /* 305a0d2642eSMika Westerberg * Read and write LPSS SSP private registers. Caller must first check that 306a0d2642eSMika Westerberg * is_lpss_ssp() returns true before these can be called. 307a0d2642eSMika Westerberg */ 308a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) 309a0d2642eSMika Westerberg { 310a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 311a0d2642eSMika Westerberg return readl(drv_data->lpss_base + offset); 312a0d2642eSMika Westerberg } 313a0d2642eSMika Westerberg 314a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data, 315a0d2642eSMika Westerberg unsigned offset, u32 value) 316a0d2642eSMika Westerberg { 317a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 318a0d2642eSMika Westerberg writel(value, drv_data->lpss_base + offset); 319a0d2642eSMika Westerberg } 320a0d2642eSMika Westerberg 321a0d2642eSMika Westerberg /* 322a0d2642eSMika Westerberg * lpss_ssp_setup - perform LPSS SSP specific setup 323a0d2642eSMika Westerberg * @drv_data: pointer to the driver private data 324a0d2642eSMika Westerberg * 325a0d2642eSMika Westerberg * Perform LPSS SSP specific setup. This function must be called first if 326a0d2642eSMika Westerberg * one is going to use LPSS SSP private registers. 327a0d2642eSMika Westerberg */ 328a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data) 329a0d2642eSMika Westerberg { 330dccf7369SJarkko Nikula const struct lpss_config *config; 331dccf7369SJarkko Nikula u32 value; 332a0d2642eSMika Westerberg 333dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 3349e43c9a8SAndy Shevchenko drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset; 335a0d2642eSMika Westerberg 336a0d2642eSMika Westerberg /* Enable software chip select control */ 3370e897218SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 338624ea72eSJarkko Nikula value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH); 339624ea72eSJarkko Nikula value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH; 340dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 3410054e28dSMika Westerberg 3420054e28dSMika Westerberg /* Enable multiblock DMA transfers */ 34351eea52dSLubomir Rintel if (drv_data->controller_info->enable_dma) { 344dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); 3451de70612SMika Westerberg 34682ba2c2aSJarkko Nikula if (config->reg_general >= 0) { 34782ba2c2aSJarkko Nikula value = __lpss_ssp_read_priv(drv_data, 34882ba2c2aSJarkko Nikula config->reg_general); 349624ea72eSJarkko Nikula value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE; 35082ba2c2aSJarkko Nikula __lpss_ssp_write_priv(drv_data, 35182ba2c2aSJarkko Nikula config->reg_general, value); 35282ba2c2aSJarkko Nikula } 3531de70612SMika Westerberg } 354a0d2642eSMika Westerberg } 355a0d2642eSMika Westerberg 356d5898e19SJarkko Nikula static void lpss_ssp_select_cs(struct spi_device *spi, 357c1e4a53cSMika Westerberg const struct lpss_config *config) 358a0d2642eSMika Westerberg { 359d5898e19SJarkko Nikula struct driver_data *drv_data = 360d5898e19SJarkko Nikula spi_controller_get_devdata(spi->controller); 361d0283eb2SJarkko Nikula u32 value, cs; 362a0d2642eSMika Westerberg 363c1e4a53cSMika Westerberg if (!config->cs_sel_mask) 364c1e4a53cSMika Westerberg return; 365dccf7369SJarkko Nikula 366dccf7369SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 367c1e4a53cSMika Westerberg 368d5898e19SJarkko Nikula cs = spi->chip_select; 369c1e4a53cSMika Westerberg cs <<= config->cs_sel_shift; 370c1e4a53cSMika Westerberg if (cs != (value & config->cs_sel_mask)) { 371d0283eb2SJarkko Nikula /* 372c1e4a53cSMika Westerberg * When switching another chip select output active the 373c1e4a53cSMika Westerberg * output must be selected first and wait 2 ssp_clk cycles 374c1e4a53cSMika Westerberg * before changing state to active. Otherwise a short 375c1e4a53cSMika Westerberg * glitch will occur on the previous chip select since 376c1e4a53cSMika Westerberg * output select is latched but state control is not. 377d0283eb2SJarkko Nikula */ 378c1e4a53cSMika Westerberg value &= ~config->cs_sel_mask; 379d0283eb2SJarkko Nikula value |= cs; 380d0283eb2SJarkko Nikula __lpss_ssp_write_priv(drv_data, 381d0283eb2SJarkko Nikula config->reg_cs_ctrl, value); 382d0283eb2SJarkko Nikula ndelay(1000000000 / 38351eea52dSLubomir Rintel (drv_data->controller->max_speed_hz / 2)); 384d0283eb2SJarkko Nikula } 385d0283eb2SJarkko Nikula } 386c1e4a53cSMika Westerberg 387d5898e19SJarkko Nikula static void lpss_ssp_cs_control(struct spi_device *spi, bool enable) 388c1e4a53cSMika Westerberg { 389d5898e19SJarkko Nikula struct driver_data *drv_data = 390d5898e19SJarkko Nikula spi_controller_get_devdata(spi->controller); 391c1e4a53cSMika Westerberg const struct lpss_config *config; 392c1e4a53cSMika Westerberg u32 value; 393c1e4a53cSMika Westerberg 394c1e4a53cSMika Westerberg config = lpss_get_config(drv_data); 395c1e4a53cSMika Westerberg 396c1e4a53cSMika Westerberg if (enable) 397d5898e19SJarkko Nikula lpss_ssp_select_cs(spi, config); 398c1e4a53cSMika Westerberg 399c1e4a53cSMika Westerberg value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 400c1e4a53cSMika Westerberg if (enable) 401c1e4a53cSMika Westerberg value &= ~LPSS_CS_CONTROL_CS_HIGH; 402c1e4a53cSMika Westerberg else 403c1e4a53cSMika Westerberg value |= LPSS_CS_CONTROL_CS_HIGH; 404dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 405683f65deSEvan Green if (config->cs_clk_stays_gated) { 406683f65deSEvan Green u32 clkgate; 407683f65deSEvan Green 408683f65deSEvan Green /* 409683f65deSEvan Green * Changing CS alone when dynamic clock gating is on won't 410683f65deSEvan Green * actually flip CS at that time. This ruins SPI transfers 411683f65deSEvan Green * that specify delays, or have no data. Toggle the clock mode 412683f65deSEvan Green * to force on briefly to poke the CS pin to move. 413683f65deSEvan Green */ 414683f65deSEvan Green clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE); 415683f65deSEvan Green value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) | 416683f65deSEvan Green LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON; 417683f65deSEvan Green 418683f65deSEvan Green __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value); 419683f65deSEvan Green __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate); 420683f65deSEvan Green } 421a0d2642eSMika Westerberg } 422a0d2642eSMika Westerberg 423d5898e19SJarkko Nikula static void cs_assert(struct spi_device *spi) 424ca632f55SGrant Likely { 425d5898e19SJarkko Nikula struct chip_data *chip = spi_get_ctldata(spi); 426d5898e19SJarkko Nikula struct driver_data *drv_data = 427d5898e19SJarkko Nikula spi_controller_get_devdata(spi->controller); 428ca632f55SGrant Likely 429ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 43096579a4eSJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, chip->frm); 431ca632f55SGrant Likely return; 432ca632f55SGrant Likely } 433ca632f55SGrant Likely 434ca632f55SGrant Likely if (chip->cs_control) { 435ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_ASSERT); 436ca632f55SGrant Likely return; 437ca632f55SGrant Likely } 438ca632f55SGrant Likely 439c18d925fSJan Kiszka if (chip->gpiod_cs) { 440c18d925fSJan Kiszka gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted); 441a0d2642eSMika Westerberg return; 442a0d2642eSMika Westerberg } 443a0d2642eSMika Westerberg 4447566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 445d5898e19SJarkko Nikula lpss_ssp_cs_control(spi, true); 446ca632f55SGrant Likely } 447ca632f55SGrant Likely 448d5898e19SJarkko Nikula static void cs_deassert(struct spi_device *spi) 449ca632f55SGrant Likely { 450d5898e19SJarkko Nikula struct chip_data *chip = spi_get_ctldata(spi); 451d5898e19SJarkko Nikula struct driver_data *drv_data = 452d5898e19SJarkko Nikula spi_controller_get_devdata(spi->controller); 453104e51afSJarkko Nikula unsigned long timeout; 454ca632f55SGrant Likely 455ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 456ca632f55SGrant Likely return; 457ca632f55SGrant Likely 458104e51afSJarkko Nikula /* Wait until SSP becomes idle before deasserting the CS */ 459104e51afSJarkko Nikula timeout = jiffies + msecs_to_jiffies(10); 460104e51afSJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY && 461104e51afSJarkko Nikula !time_after(jiffies, timeout)) 462104e51afSJarkko Nikula cpu_relax(); 463104e51afSJarkko Nikula 464ca632f55SGrant Likely if (chip->cs_control) { 465ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_DEASSERT); 466ca632f55SGrant Likely return; 467ca632f55SGrant Likely } 468ca632f55SGrant Likely 469c18d925fSJan Kiszka if (chip->gpiod_cs) { 470c18d925fSJan Kiszka gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted); 471a0d2642eSMika Westerberg return; 472a0d2642eSMika Westerberg } 473a0d2642eSMika Westerberg 4747566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 475d5898e19SJarkko Nikula lpss_ssp_cs_control(spi, false); 476d5898e19SJarkko Nikula } 477d5898e19SJarkko Nikula 478d5898e19SJarkko Nikula static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level) 479d5898e19SJarkko Nikula { 480d5898e19SJarkko Nikula if (level) 481d5898e19SJarkko Nikula cs_deassert(spi); 482d5898e19SJarkko Nikula else 483d5898e19SJarkko Nikula cs_assert(spi); 484ca632f55SGrant Likely } 485ca632f55SGrant Likely 486cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data) 487ca632f55SGrant Likely { 488ca632f55SGrant Likely unsigned long limit = loops_per_jiffy << 1; 489ca632f55SGrant Likely 490ca632f55SGrant Likely do { 491*6d380132SAndy Shevchenko while (read_SSSR_bits(drv_data, SSSR_RNE)) 492c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 493c039dd27SJarkko Nikula } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); 494ca632f55SGrant Likely write_SSSR_CS(drv_data, SSSR_ROR); 495ca632f55SGrant Likely 496ca632f55SGrant Likely return limit; 497ca632f55SGrant Likely } 498ca632f55SGrant Likely 49929d7e05cSLubomir Rintel static void pxa2xx_spi_off(struct driver_data *drv_data) 50029d7e05cSLubomir Rintel { 50141c98841SAndy Shevchenko /* On MMP, disabling SSE seems to corrupt the Rx FIFO */ 50241c98841SAndy Shevchenko if (is_mmp2_ssp(drv_data)) 50329d7e05cSLubomir Rintel return; 50429d7e05cSLubomir Rintel 5050c8ccd8bSAndy Shevchenko pxa_ssp_disable(drv_data->ssp); 50629d7e05cSLubomir Rintel } 50729d7e05cSLubomir Rintel 508ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data) 509ca632f55SGrant Likely { 510ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 511ca632f55SGrant Likely 5124fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 513ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 514ca632f55SGrant Likely return 0; 515ca632f55SGrant Likely 516c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, 0); 517ca632f55SGrant Likely drv_data->tx += n_bytes; 518ca632f55SGrant Likely 519ca632f55SGrant Likely return 1; 520ca632f55SGrant Likely } 521ca632f55SGrant Likely 522ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data) 523ca632f55SGrant Likely { 524ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 525ca632f55SGrant Likely 526*6d380132SAndy Shevchenko while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { 527c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 528ca632f55SGrant Likely drv_data->rx += n_bytes; 529ca632f55SGrant Likely } 530ca632f55SGrant Likely 531ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 532ca632f55SGrant Likely } 533ca632f55SGrant Likely 534ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data) 535ca632f55SGrant Likely { 5364fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 537ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 538ca632f55SGrant Likely return 0; 539ca632f55SGrant Likely 540c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); 541ca632f55SGrant Likely ++drv_data->tx; 542ca632f55SGrant Likely 543ca632f55SGrant Likely return 1; 544ca632f55SGrant Likely } 545ca632f55SGrant Likely 546ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data) 547ca632f55SGrant Likely { 548*6d380132SAndy Shevchenko while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { 549c039dd27SJarkko Nikula *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 550ca632f55SGrant Likely ++drv_data->rx; 551ca632f55SGrant Likely } 552ca632f55SGrant Likely 553ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 554ca632f55SGrant Likely } 555ca632f55SGrant Likely 556ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data) 557ca632f55SGrant Likely { 5584fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 559ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 560ca632f55SGrant Likely return 0; 561ca632f55SGrant Likely 562c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); 563ca632f55SGrant Likely drv_data->tx += 2; 564ca632f55SGrant Likely 565ca632f55SGrant Likely return 1; 566ca632f55SGrant Likely } 567ca632f55SGrant Likely 568ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data) 569ca632f55SGrant Likely { 570*6d380132SAndy Shevchenko while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { 571c039dd27SJarkko Nikula *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 572ca632f55SGrant Likely drv_data->rx += 2; 573ca632f55SGrant Likely } 574ca632f55SGrant Likely 575ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 576ca632f55SGrant Likely } 577ca632f55SGrant Likely 578ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data) 579ca632f55SGrant Likely { 5804fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 581ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 582ca632f55SGrant Likely return 0; 583ca632f55SGrant Likely 584c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); 585ca632f55SGrant Likely drv_data->tx += 4; 586ca632f55SGrant Likely 587ca632f55SGrant Likely return 1; 588ca632f55SGrant Likely } 589ca632f55SGrant Likely 590ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data) 591ca632f55SGrant Likely { 592*6d380132SAndy Shevchenko while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { 593c039dd27SJarkko Nikula *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 594ca632f55SGrant Likely drv_data->rx += 4; 595ca632f55SGrant Likely } 596ca632f55SGrant Likely 597ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 598ca632f55SGrant Likely } 599ca632f55SGrant Likely 600ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data) 601ca632f55SGrant Likely { 60296579a4eSJarkko Nikula struct chip_data *chip = 60351eea52dSLubomir Rintel spi_get_ctldata(drv_data->controller->cur_msg->spi); 604ca632f55SGrant Likely u32 sccr1_reg; 605ca632f55SGrant Likely 606c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; 607152bc19eSAndy Shevchenko switch (drv_data->ssp_type) { 608152bc19eSAndy Shevchenko case QUARK_X1000_SSP: 609152bc19eSAndy Shevchenko sccr1_reg &= ~QUARK_X1000_SSCR1_RFT; 610152bc19eSAndy Shevchenko break; 6117c7289a4SAndy Shevchenko case CE4100_SSP: 6127c7289a4SAndy Shevchenko sccr1_reg &= ~CE4100_SSCR1_RFT; 6137c7289a4SAndy Shevchenko break; 614152bc19eSAndy Shevchenko default: 615ca632f55SGrant Likely sccr1_reg &= ~SSCR1_RFT; 616152bc19eSAndy Shevchenko break; 617152bc19eSAndy Shevchenko } 618ca632f55SGrant Likely sccr1_reg |= chip->threshold; 619c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 620ca632f55SGrant Likely } 621ca632f55SGrant Likely 622ab77fe89SAndy Shevchenko static void int_stop_and_reset(struct driver_data *drv_data) 623ca632f55SGrant Likely { 624ab77fe89SAndy Shevchenko /* Clear and disable interrupts */ 625ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 626ca632f55SGrant Likely reset_sccr1(drv_data); 627ab77fe89SAndy Shevchenko if (pxa25x_ssp_comp(drv_data)) 628ab77fe89SAndy Shevchenko return; 629ab77fe89SAndy Shevchenko 630c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 631ab77fe89SAndy Shevchenko } 632ab77fe89SAndy Shevchenko 6334761d2e7SAndy Shevchenko static void int_error_stop(struct driver_data *drv_data, const char *msg, int err) 634ab77fe89SAndy Shevchenko { 635ab77fe89SAndy Shevchenko int_stop_and_reset(drv_data); 636cd7bed00SMika Westerberg pxa2xx_spi_flush(drv_data); 63729d7e05cSLubomir Rintel pxa2xx_spi_off(drv_data); 638ca632f55SGrant Likely 639c3dce24cSAndy Shevchenko dev_err(drv_data->ssp->dev, "%s\n", msg); 640ca632f55SGrant Likely 6414761d2e7SAndy Shevchenko drv_data->controller->cur_msg->status = err; 64251eea52dSLubomir Rintel spi_finalize_current_transfer(drv_data->controller); 643ca632f55SGrant Likely } 644ca632f55SGrant Likely 645ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data) 646ca632f55SGrant Likely { 647ab77fe89SAndy Shevchenko int_stop_and_reset(drv_data); 648ca632f55SGrant Likely 64951eea52dSLubomir Rintel spi_finalize_current_transfer(drv_data->controller); 650ca632f55SGrant Likely } 651ca632f55SGrant Likely 652ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data) 653ca632f55SGrant Likely { 654*6d380132SAndy Shevchenko u32 irq_status; 655ca632f55SGrant Likely 656*6d380132SAndy Shevchenko irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr); 657*6d380132SAndy Shevchenko if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE)) 658*6d380132SAndy Shevchenko irq_status &= ~SSSR_TFS; 659ca632f55SGrant Likely 660ca632f55SGrant Likely if (irq_status & SSSR_ROR) { 6614761d2e7SAndy Shevchenko int_error_stop(drv_data, "interrupt_transfer: fifo overrun", -EIO); 662ca632f55SGrant Likely return IRQ_HANDLED; 663ca632f55SGrant Likely } 664ca632f55SGrant Likely 665ec93cb6fSLubomir Rintel if (irq_status & SSSR_TUR) { 6664761d2e7SAndy Shevchenko int_error_stop(drv_data, "interrupt_transfer: fifo underrun", -EIO); 667ec93cb6fSLubomir Rintel return IRQ_HANDLED; 668ec93cb6fSLubomir Rintel } 669ec93cb6fSLubomir Rintel 670ca632f55SGrant Likely if (irq_status & SSSR_TINT) { 671c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); 672ca632f55SGrant Likely if (drv_data->read(drv_data)) { 673ca632f55SGrant Likely int_transfer_complete(drv_data); 674ca632f55SGrant Likely return IRQ_HANDLED; 675ca632f55SGrant Likely } 676ca632f55SGrant Likely } 677ca632f55SGrant Likely 678ca632f55SGrant Likely /* Drain rx fifo, Fill tx fifo and prevent overruns */ 679ca632f55SGrant Likely do { 680ca632f55SGrant Likely if (drv_data->read(drv_data)) { 681ca632f55SGrant Likely int_transfer_complete(drv_data); 682ca632f55SGrant Likely return IRQ_HANDLED; 683ca632f55SGrant Likely } 684ca632f55SGrant Likely } while (drv_data->write(drv_data)); 685ca632f55SGrant Likely 686ca632f55SGrant Likely if (drv_data->read(drv_data)) { 687ca632f55SGrant Likely int_transfer_complete(drv_data); 688ca632f55SGrant Likely return IRQ_HANDLED; 689ca632f55SGrant Likely } 690ca632f55SGrant Likely 691ca632f55SGrant Likely if (drv_data->tx == drv_data->tx_end) { 692ca632f55SGrant Likely u32 bytes_left; 693ca632f55SGrant Likely u32 sccr1_reg; 694ca632f55SGrant Likely 695c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 696ca632f55SGrant Likely sccr1_reg &= ~SSCR1_TIE; 697ca632f55SGrant Likely 698ca632f55SGrant Likely /* 699ca632f55SGrant Likely * PXA25x_SSP has no timeout, set up rx threshould for the 700ca632f55SGrant Likely * remaining RX bytes. 701ca632f55SGrant Likely */ 702ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 7034fdb2424SWeike Chen u32 rx_thre; 704ca632f55SGrant Likely 7054fdb2424SWeike Chen pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); 706ca632f55SGrant Likely 707ca632f55SGrant Likely bytes_left = drv_data->rx_end - drv_data->rx; 708ca632f55SGrant Likely switch (drv_data->n_bytes) { 709ca632f55SGrant Likely case 4: 7102c183376SGustavo A. R. Silva bytes_left >>= 2; 7112c183376SGustavo A. R. Silva break; 712ca632f55SGrant Likely case 2: 713ca632f55SGrant Likely bytes_left >>= 1; 7142c183376SGustavo A. R. Silva break; 715ca632f55SGrant Likely } 716ca632f55SGrant Likely 7174fdb2424SWeike Chen rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); 7184fdb2424SWeike Chen if (rx_thre > bytes_left) 7194fdb2424SWeike Chen rx_thre = bytes_left; 720ca632f55SGrant Likely 7214fdb2424SWeike Chen pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); 722ca632f55SGrant Likely } 723c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 724ca632f55SGrant Likely } 725ca632f55SGrant Likely 726ca632f55SGrant Likely /* We did something */ 727ca632f55SGrant Likely return IRQ_HANDLED; 728ca632f55SGrant Likely } 729ca632f55SGrant Likely 730b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data) 731b0312482SJan Kiszka { 73229d7e05cSLubomir Rintel pxa2xx_spi_off(drv_data); 73342c80cd4SAndy Shevchenko clear_SSCR1_bits(drv_data, drv_data->int_cr1); 734b0312482SJan Kiszka if (!pxa25x_ssp_comp(drv_data)) 735b0312482SJan Kiszka pxa2xx_spi_write(drv_data, SSTO, 0); 736b0312482SJan Kiszka write_SSSR_CS(drv_data, drv_data->clear_sr); 737b0312482SJan Kiszka 738c3dce24cSAndy Shevchenko dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n"); 739b0312482SJan Kiszka } 740b0312482SJan Kiszka 741ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id) 742ca632f55SGrant Likely { 743ca632f55SGrant Likely struct driver_data *drv_data = dev_id; 7447d94a505SMika Westerberg u32 sccr1_reg; 745ca632f55SGrant Likely u32 mask = drv_data->mask_sr; 746ca632f55SGrant Likely u32 status; 747ca632f55SGrant Likely 7487d94a505SMika Westerberg /* 7497d94a505SMika Westerberg * The IRQ might be shared with other peripherals so we must first 7507d94a505SMika Westerberg * check that are we RPM suspended or not. If we are we assume that 7517d94a505SMika Westerberg * the IRQ was not for us (we shouldn't be RPM suspended when the 7527d94a505SMika Westerberg * interrupt is enabled). 7537d94a505SMika Westerberg */ 754c3dce24cSAndy Shevchenko if (pm_runtime_suspended(drv_data->ssp->dev)) 7557d94a505SMika Westerberg return IRQ_NONE; 7567d94a505SMika Westerberg 757269e4a41SMika Westerberg /* 758269e4a41SMika Westerberg * If the device is not yet in RPM suspended state and we get an 759269e4a41SMika Westerberg * interrupt that is meant for another device, check if status bits 760269e4a41SMika Westerberg * are all set to one. That means that the device is already 761269e4a41SMika Westerberg * powered off. 762269e4a41SMika Westerberg */ 763c039dd27SJarkko Nikula status = pxa2xx_spi_read(drv_data, SSSR); 764269e4a41SMika Westerberg if (status == ~0) 765269e4a41SMika Westerberg return IRQ_NONE; 766269e4a41SMika Westerberg 767c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 768ca632f55SGrant Likely 769ca632f55SGrant Likely /* Ignore possible writes if we don't need to write */ 770ca632f55SGrant Likely if (!(sccr1_reg & SSCR1_TIE)) 771ca632f55SGrant Likely mask &= ~SSSR_TFS; 772ca632f55SGrant Likely 77302bc933eSTan, Jui Nee /* Ignore RX timeout interrupt if it is disabled */ 77402bc933eSTan, Jui Nee if (!(sccr1_reg & SSCR1_TINTE)) 77502bc933eSTan, Jui Nee mask &= ~SSSR_TINT; 77602bc933eSTan, Jui Nee 777ca632f55SGrant Likely if (!(status & mask)) 778ca632f55SGrant Likely return IRQ_NONE; 779ca632f55SGrant Likely 780e51e9b93SJan Kiszka pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1); 781e51e9b93SJan Kiszka pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 782e51e9b93SJan Kiszka 78351eea52dSLubomir Rintel if (!drv_data->controller->cur_msg) { 784b0312482SJan Kiszka handle_bad_msg(drv_data); 785ca632f55SGrant Likely /* Never fail */ 786ca632f55SGrant Likely return IRQ_HANDLED; 787ca632f55SGrant Likely } 788ca632f55SGrant Likely 789ca632f55SGrant Likely return drv_data->transfer_handler(drv_data); 790ca632f55SGrant Likely } 791ca632f55SGrant Likely 792e5262d05SWeike Chen /* 7939df461ecSAndy Shevchenko * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply 7949df461ecSAndy Shevchenko * input frequency by fractions of 2^24. It also has a divider by 5. 7959df461ecSAndy Shevchenko * 7969df461ecSAndy Shevchenko * There are formulas to get baud rate value for given input frequency and 7979df461ecSAndy Shevchenko * divider parameters, such as DDS_CLK_RATE and SCR: 7989df461ecSAndy Shevchenko * 7999df461ecSAndy Shevchenko * Fsys = 200MHz 8009df461ecSAndy Shevchenko * 8019df461ecSAndy Shevchenko * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) 8029df461ecSAndy Shevchenko * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) 8039df461ecSAndy Shevchenko * 8049df461ecSAndy Shevchenko * DDS_CLK_RATE either 2^n or 2^n / 5. 8059df461ecSAndy Shevchenko * SCR is in range 0 .. 255 8069df461ecSAndy Shevchenko * 8079df461ecSAndy Shevchenko * Divisor = 5^i * 2^j * 2 * k 8089df461ecSAndy Shevchenko * i = [0, 1] i = 1 iff j = 0 or j > 3 8099df461ecSAndy Shevchenko * j = [0, 23] j = 0 iff i = 1 8109df461ecSAndy Shevchenko * k = [1, 256] 8119df461ecSAndy Shevchenko * Special case: j = 0, i = 1: Divisor = 2 / 5 8129df461ecSAndy Shevchenko * 8139df461ecSAndy Shevchenko * Accordingly to the specification the recommended values for DDS_CLK_RATE 8149df461ecSAndy Shevchenko * are: 8159df461ecSAndy Shevchenko * Case 1: 2^n, n = [0, 23] 8169df461ecSAndy Shevchenko * Case 2: 2^24 * 2 / 5 (0x666666) 8179df461ecSAndy Shevchenko * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) 8189df461ecSAndy Shevchenko * 8199df461ecSAndy Shevchenko * In all cases the lowest possible value is better. 8209df461ecSAndy Shevchenko * 8219df461ecSAndy Shevchenko * The function calculates parameters for all cases and chooses the one closest 8229df461ecSAndy Shevchenko * to the asked baud rate. 823e5262d05SWeike Chen */ 8249df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) 825e5262d05SWeike Chen { 8269df461ecSAndy Shevchenko unsigned long xtal = 200000000; 8279df461ecSAndy Shevchenko unsigned long fref = xtal / 2; /* mandatory division by 2, 8289df461ecSAndy Shevchenko see (2) */ 8299df461ecSAndy Shevchenko /* case 3 */ 8309df461ecSAndy Shevchenko unsigned long fref1 = fref / 2; /* case 1 */ 8319df461ecSAndy Shevchenko unsigned long fref2 = fref * 2 / 5; /* case 2 */ 8329df461ecSAndy Shevchenko unsigned long scale; 8339df461ecSAndy Shevchenko unsigned long q, q1, q2; 8349df461ecSAndy Shevchenko long r, r1, r2; 8359df461ecSAndy Shevchenko u32 mul; 836e5262d05SWeike Chen 8379df461ecSAndy Shevchenko /* Case 1 */ 8389df461ecSAndy Shevchenko 8399df461ecSAndy Shevchenko /* Set initial value for DDS_CLK_RATE */ 8409df461ecSAndy Shevchenko mul = (1 << 24) >> 1; 8419df461ecSAndy Shevchenko 8429df461ecSAndy Shevchenko /* Calculate initial quot */ 8433ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref1, rate); 8449df461ecSAndy Shevchenko 8459df461ecSAndy Shevchenko /* Scale q1 if it's too big */ 8469df461ecSAndy Shevchenko if (q1 > 256) { 8479df461ecSAndy Shevchenko /* Scale q1 to range [1, 512] */ 8489df461ecSAndy Shevchenko scale = fls_long(q1 - 1); 8499df461ecSAndy Shevchenko if (scale > 9) { 8509df461ecSAndy Shevchenko q1 >>= scale - 9; 8519df461ecSAndy Shevchenko mul >>= scale - 9; 8529df461ecSAndy Shevchenko } 8539df461ecSAndy Shevchenko 8549df461ecSAndy Shevchenko /* Round the result if we have a remainder */ 8559df461ecSAndy Shevchenko q1 += q1 & 1; 8569df461ecSAndy Shevchenko } 8579df461ecSAndy Shevchenko 8589df461ecSAndy Shevchenko /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ 8599df461ecSAndy Shevchenko scale = __ffs(q1); 8609df461ecSAndy Shevchenko q1 >>= scale; 8619df461ecSAndy Shevchenko mul >>= scale; 8629df461ecSAndy Shevchenko 8639df461ecSAndy Shevchenko /* Get the remainder */ 8649df461ecSAndy Shevchenko r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); 8659df461ecSAndy Shevchenko 8669df461ecSAndy Shevchenko /* Case 2 */ 8679df461ecSAndy Shevchenko 8683ad48062SAndy Shevchenko q2 = DIV_ROUND_UP(fref2, rate); 8699df461ecSAndy Shevchenko r2 = abs(fref2 / q2 - rate); 8709df461ecSAndy Shevchenko 8719df461ecSAndy Shevchenko /* 8729df461ecSAndy Shevchenko * Choose the best between two: less remainder we have the better. We 8739df461ecSAndy Shevchenko * can't go case 2 if q2 is greater than 256 since SCR register can 8749df461ecSAndy Shevchenko * hold only values 0 .. 255. 8759df461ecSAndy Shevchenko */ 8769df461ecSAndy Shevchenko if (r2 >= r1 || q2 > 256) { 8779df461ecSAndy Shevchenko /* case 1 is better */ 8789df461ecSAndy Shevchenko r = r1; 8799df461ecSAndy Shevchenko q = q1; 8809df461ecSAndy Shevchenko } else { 8819df461ecSAndy Shevchenko /* case 2 is better */ 8829df461ecSAndy Shevchenko r = r2; 8839df461ecSAndy Shevchenko q = q2; 8849df461ecSAndy Shevchenko mul = (1 << 24) * 2 / 5; 8859df461ecSAndy Shevchenko } 8869df461ecSAndy Shevchenko 8873ad48062SAndy Shevchenko /* Check case 3 only if the divisor is big enough */ 8889df461ecSAndy Shevchenko if (fref / rate >= 80) { 8899df461ecSAndy Shevchenko u64 fssp; 8909df461ecSAndy Shevchenko u32 m; 8919df461ecSAndy Shevchenko 8929df461ecSAndy Shevchenko /* Calculate initial quot */ 8933ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref, rate); 8949df461ecSAndy Shevchenko m = (1 << 24) / q1; 8959df461ecSAndy Shevchenko 8969df461ecSAndy Shevchenko /* Get the remainder */ 8979df461ecSAndy Shevchenko fssp = (u64)fref * m; 8989df461ecSAndy Shevchenko do_div(fssp, 1 << 24); 8999df461ecSAndy Shevchenko r1 = abs(fssp - rate); 9009df461ecSAndy Shevchenko 9019df461ecSAndy Shevchenko /* Choose this one if it suits better */ 9029df461ecSAndy Shevchenko if (r1 < r) { 9039df461ecSAndy Shevchenko /* case 3 is better */ 9049df461ecSAndy Shevchenko q = 1; 9059df461ecSAndy Shevchenko mul = m; 906e5262d05SWeike Chen } 907e5262d05SWeike Chen } 908e5262d05SWeike Chen 9099df461ecSAndy Shevchenko *dds = mul; 9109df461ecSAndy Shevchenko return q - 1; 911e5262d05SWeike Chen } 912e5262d05SWeike Chen 9133343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) 914ca632f55SGrant Likely { 91551eea52dSLubomir Rintel unsigned long ssp_clk = drv_data->controller->max_speed_hz; 9163343b7a6SMika Westerberg const struct ssp_device *ssp = drv_data->ssp; 9173343b7a6SMika Westerberg 9183343b7a6SMika Westerberg rate = min_t(int, ssp_clk, rate); 919ca632f55SGrant Likely 92029f21337SFlavio Suligoi /* 92129f21337SFlavio Suligoi * Calculate the divisor for the SCR (Serial Clock Rate), avoiding 92229f21337SFlavio Suligoi * that the SSP transmission rate can be greater than the device rate 92329f21337SFlavio Suligoi */ 924ca632f55SGrant Likely if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) 92529f21337SFlavio Suligoi return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff; 926ca632f55SGrant Likely else 92729f21337SFlavio Suligoi return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff; 928ca632f55SGrant Likely } 929ca632f55SGrant Likely 930e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, 931d2c2f6a4SAndy Shevchenko int rate) 932e5262d05SWeike Chen { 93396579a4eSJarkko Nikula struct chip_data *chip = 93451eea52dSLubomir Rintel spi_get_ctldata(drv_data->controller->cur_msg->spi); 935025ffe88SAndy Shevchenko unsigned int clk_div; 936e5262d05SWeike Chen 937e5262d05SWeike Chen switch (drv_data->ssp_type) { 938e5262d05SWeike Chen case QUARK_X1000_SSP: 9399df461ecSAndy Shevchenko clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); 940eecacf73SDan Carpenter break; 941e5262d05SWeike Chen default: 942025ffe88SAndy Shevchenko clk_div = ssp_get_clk_div(drv_data, rate); 943eecacf73SDan Carpenter break; 944e5262d05SWeike Chen } 945025ffe88SAndy Shevchenko return clk_div << 8; 946e5262d05SWeike Chen } 947e5262d05SWeike Chen 94851eea52dSLubomir Rintel static bool pxa2xx_spi_can_dma(struct spi_controller *controller, 949b6ced294SJarkko Nikula struct spi_device *spi, 950b6ced294SJarkko Nikula struct spi_transfer *xfer) 951b6ced294SJarkko Nikula { 952b6ced294SJarkko Nikula struct chip_data *chip = spi_get_ctldata(spi); 953b6ced294SJarkko Nikula 954b6ced294SJarkko Nikula return chip->enable_dma && 955b6ced294SJarkko Nikula xfer->len <= MAX_DMA_LEN && 956b6ced294SJarkko Nikula xfer->len >= chip->dma_burst_size; 957b6ced294SJarkko Nikula } 958b6ced294SJarkko Nikula 95951eea52dSLubomir Rintel static int pxa2xx_spi_transfer_one(struct spi_controller *controller, 960d5898e19SJarkko Nikula struct spi_device *spi, 961d5898e19SJarkko Nikula struct spi_transfer *transfer) 962ca632f55SGrant Likely { 96351eea52dSLubomir Rintel struct driver_data *drv_data = spi_controller_get_devdata(controller); 96451eea52dSLubomir Rintel struct spi_message *message = controller->cur_msg; 96520f4c379SJarkko Nikula struct chip_data *chip = spi_get_ctldata(spi); 96696579a4eSJarkko Nikula u32 dma_thresh = chip->dma_threshold; 96796579a4eSJarkko Nikula u32 dma_burst = chip->dma_burst_size; 96896579a4eSJarkko Nikula u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); 969bffc967eSJarkko Nikula u32 clk_div; 970bffc967eSJarkko Nikula u8 bits; 971bffc967eSJarkko Nikula u32 speed; 972ca632f55SGrant Likely u32 cr0; 973ca632f55SGrant Likely u32 cr1; 9747d1f1bf6SAndy Shevchenko int err; 975b6ced294SJarkko Nikula int dma_mapped; 976ca632f55SGrant Likely 977cd7bed00SMika Westerberg /* Check if we can DMA this transfer */ 978b6ced294SJarkko Nikula if (transfer->len > MAX_DMA_LEN && chip->enable_dma) { 979ca632f55SGrant Likely 980ca632f55SGrant Likely /* reject already-mapped transfers; PIO won't always work */ 981ca632f55SGrant Likely if (message->is_dma_mapped 982ca632f55SGrant Likely || transfer->rx_dma || transfer->tx_dma) { 983748fbadfSJarkko Nikula dev_err(&spi->dev, 9848ae55af3SJarkko Nikula "Mapped transfer length of %u is greater than %d\n", 985ca632f55SGrant Likely transfer->len, MAX_DMA_LEN); 986d5898e19SJarkko Nikula return -EINVAL; 987ca632f55SGrant Likely } 988ca632f55SGrant Likely 989ca632f55SGrant Likely /* warn ... we force this to PIO mode */ 99020f4c379SJarkko Nikula dev_warn_ratelimited(&spi->dev, 9918ae55af3SJarkko Nikula "DMA disabled for transfer length %ld greater than %d\n", 992d5898e19SJarkko Nikula (long)transfer->len, MAX_DMA_LEN); 993ca632f55SGrant Likely } 994ca632f55SGrant Likely 995ca632f55SGrant Likely /* Setup the transfer state based on the type of transfer */ 996cd7bed00SMika Westerberg if (pxa2xx_spi_flush(drv_data) == 0) { 997748fbadfSJarkko Nikula dev_err(&spi->dev, "Flush failed\n"); 998d5898e19SJarkko Nikula return -EIO; 999ca632f55SGrant Likely } 1000ca632f55SGrant Likely drv_data->n_bytes = chip->n_bytes; 1001ca632f55SGrant Likely drv_data->tx = (void *)transfer->tx_buf; 1002ca632f55SGrant Likely drv_data->tx_end = drv_data->tx + transfer->len; 1003ca632f55SGrant Likely drv_data->rx = transfer->rx_buf; 1004ca632f55SGrant Likely drv_data->rx_end = drv_data->rx + transfer->len; 1005ca632f55SGrant Likely drv_data->write = drv_data->tx ? chip->write : null_writer; 1006ca632f55SGrant Likely drv_data->read = drv_data->rx ? chip->read : null_reader; 1007ca632f55SGrant Likely 1008ca632f55SGrant Likely /* Change speed and bit per word on a per transfer */ 1009ca632f55SGrant Likely bits = transfer->bits_per_word; 1010ca632f55SGrant Likely speed = transfer->speed_hz; 1011ca632f55SGrant Likely 1012d2c2f6a4SAndy Shevchenko clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); 1013ca632f55SGrant Likely 1014ca632f55SGrant Likely if (bits <= 8) { 1015ca632f55SGrant Likely drv_data->n_bytes = 1; 1016ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1017ca632f55SGrant Likely u8_reader : null_reader; 1018ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1019ca632f55SGrant Likely u8_writer : null_writer; 1020ca632f55SGrant Likely } else if (bits <= 16) { 1021ca632f55SGrant Likely drv_data->n_bytes = 2; 1022ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1023ca632f55SGrant Likely u16_reader : null_reader; 1024ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1025ca632f55SGrant Likely u16_writer : null_writer; 1026ca632f55SGrant Likely } else if (bits <= 32) { 1027ca632f55SGrant Likely drv_data->n_bytes = 4; 1028ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1029ca632f55SGrant Likely u32_reader : null_reader; 1030ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1031ca632f55SGrant Likely u32_writer : null_writer; 1032ca632f55SGrant Likely } 1033196b0e2cSJarkko Nikula /* 1034196b0e2cSJarkko Nikula * if bits/word is changed in dma mode, then must check the 1035196b0e2cSJarkko Nikula * thresholds and burst also 1036196b0e2cSJarkko Nikula */ 1037ca632f55SGrant Likely if (chip->enable_dma) { 1038cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, 103920f4c379SJarkko Nikula spi, 1040ca632f55SGrant Likely bits, &dma_burst, 1041ca632f55SGrant Likely &dma_thresh)) 104220f4c379SJarkko Nikula dev_warn_ratelimited(&spi->dev, 10438ae55af3SJarkko Nikula "DMA burst size reduced to match bits_per_word\n"); 1044ca632f55SGrant Likely } 1045ca632f55SGrant Likely 104651eea52dSLubomir Rintel dma_mapped = controller->can_dma && 104720f4c379SJarkko Nikula controller->can_dma(controller, spi, transfer) && 104851eea52dSLubomir Rintel controller->cur_msg_mapped; 1049b6ced294SJarkko Nikula if (dma_mapped) { 1050ca632f55SGrant Likely 1051ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1052cd7bed00SMika Westerberg drv_data->transfer_handler = pxa2xx_spi_dma_transfer; 1053ca632f55SGrant Likely 1054d5898e19SJarkko Nikula err = pxa2xx_spi_dma_prepare(drv_data, transfer); 1055d5898e19SJarkko Nikula if (err) 1056d5898e19SJarkko Nikula return err; 1057ca632f55SGrant Likely 1058ca632f55SGrant Likely /* Clear status and start DMA engine */ 1059ca632f55SGrant Likely cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; 1060c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); 1061cd7bed00SMika Westerberg 1062cd7bed00SMika Westerberg pxa2xx_spi_dma_start(drv_data); 1063ca632f55SGrant Likely } else { 1064ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1065ca632f55SGrant Likely drv_data->transfer_handler = interrupt_transfer; 1066ca632f55SGrant Likely 1067ca632f55SGrant Likely /* Clear status */ 1068ca632f55SGrant Likely cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; 1069ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 1070ca632f55SGrant Likely } 1071ca632f55SGrant Likely 1072ee03672dSJarkko Nikula /* NOTE: PXA25x_SSP _could_ use external clocking ... */ 1073ee03672dSJarkko Nikula cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); 1074ee03672dSJarkko Nikula if (!pxa25x_ssp_comp(drv_data)) 107520f4c379SJarkko Nikula dev_dbg(&spi->dev, "%u Hz actual, %s\n", 107651eea52dSLubomir Rintel controller->max_speed_hz 1077ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), 1078b6ced294SJarkko Nikula dma_mapped ? "DMA" : "PIO"); 1079ee03672dSJarkko Nikula else 108020f4c379SJarkko Nikula dev_dbg(&spi->dev, "%u Hz actual, %s\n", 108151eea52dSLubomir Rintel controller->max_speed_hz / 2 1082ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), 1083b6ced294SJarkko Nikula dma_mapped ? "DMA" : "PIO"); 1084ee03672dSJarkko Nikula 1085a0d2642eSMika Westerberg if (is_lpss_ssp(drv_data)) { 10861bed378cSAndy Shevchenko pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold); 10871bed378cSAndy Shevchenko pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold); 1088a0d2642eSMika Westerberg } 1089a0d2642eSMika Westerberg 10901bed378cSAndy Shevchenko if (is_quark_x1000_ssp(drv_data)) 10911bed378cSAndy Shevchenko pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate); 1092e5262d05SWeike Chen 10930c8ccd8bSAndy Shevchenko /* Stop the SSP */ 10940c8ccd8bSAndy Shevchenko if (!is_mmp2_ssp(drv_data)) 10950c8ccd8bSAndy Shevchenko pxa_ssp_disable(drv_data->ssp); 10960c8ccd8bSAndy Shevchenko 10970c8ccd8bSAndy Shevchenko if (!pxa25x_ssp_comp(drv_data)) 10980c8ccd8bSAndy Shevchenko pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 10990c8ccd8bSAndy Shevchenko 1100ca632f55SGrant Likely /* first set CR1 without interrupt and service enables */ 11011bed378cSAndy Shevchenko pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1); 11021bed378cSAndy Shevchenko 11031bed378cSAndy Shevchenko /* see if we need to reload the config registers */ 11041bed378cSAndy Shevchenko pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0); 1105ca632f55SGrant Likely 11060c8ccd8bSAndy Shevchenko /* Restart the SSP */ 11070c8ccd8bSAndy Shevchenko pxa_ssp_enable(drv_data->ssp); 11080c8ccd8bSAndy Shevchenko 110941c98841SAndy Shevchenko if (is_mmp2_ssp(drv_data)) { 1110*6d380132SAndy Shevchenko u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8; 111182391856SLubomir Rintel 111282391856SLubomir Rintel if (tx_level) { 111382391856SLubomir Rintel /* On MMP2, flipping SSE doesn't to empty TXFIFO. */ 111482391856SLubomir Rintel dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n", 111582391856SLubomir Rintel tx_level); 111682391856SLubomir Rintel if (tx_level > transfer->len) 111782391856SLubomir Rintel tx_level = transfer->len; 111882391856SLubomir Rintel drv_data->tx += tx_level; 111982391856SLubomir Rintel } 112082391856SLubomir Rintel } 112182391856SLubomir Rintel 112251eea52dSLubomir Rintel if (spi_controller_is_slave(controller)) { 1123ec93cb6fSLubomir Rintel while (drv_data->write(drv_data)) 1124ec93cb6fSLubomir Rintel ; 112577d33897SLubomir Rintel if (drv_data->gpiod_ready) { 112677d33897SLubomir Rintel gpiod_set_value(drv_data->gpiod_ready, 1); 112777d33897SLubomir Rintel udelay(1); 112877d33897SLubomir Rintel gpiod_set_value(drv_data->gpiod_ready, 0); 112977d33897SLubomir Rintel } 1130ec93cb6fSLubomir Rintel } 1131ec93cb6fSLubomir Rintel 1132d5898e19SJarkko Nikula /* 1133d5898e19SJarkko Nikula * Release the data by enabling service requests and interrupts, 1134d5898e19SJarkko Nikula * without changing any mode bits 1135d5898e19SJarkko Nikula */ 1136c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1); 1137d5898e19SJarkko Nikula 1138d5898e19SJarkko Nikula return 1; 1139ca632f55SGrant Likely } 1140ca632f55SGrant Likely 114151eea52dSLubomir Rintel static int pxa2xx_spi_slave_abort(struct spi_controller *controller) 1142ec93cb6fSLubomir Rintel { 114351eea52dSLubomir Rintel struct driver_data *drv_data = spi_controller_get_devdata(controller); 1144ec93cb6fSLubomir Rintel 11454761d2e7SAndy Shevchenko int_error_stop(drv_data, "transfer aborted", -EINTR); 1146ec93cb6fSLubomir Rintel 1147ec93cb6fSLubomir Rintel return 0; 1148ec93cb6fSLubomir Rintel } 1149ec93cb6fSLubomir Rintel 115051eea52dSLubomir Rintel static void pxa2xx_spi_handle_err(struct spi_controller *controller, 11517f86bde9SMika Westerberg struct spi_message *msg) 1152ca632f55SGrant Likely { 115351eea52dSLubomir Rintel struct driver_data *drv_data = spi_controller_get_devdata(controller); 1154ca632f55SGrant Likely 1155d5898e19SJarkko Nikula /* Disable the SSP */ 115629d7e05cSLubomir Rintel pxa2xx_spi_off(drv_data); 1157d5898e19SJarkko Nikula /* Clear and disable interrupts and service requests */ 1158d5898e19SJarkko Nikula write_SSSR_CS(drv_data, drv_data->clear_sr); 115942c80cd4SAndy Shevchenko clear_SSCR1_bits(drv_data, drv_data->int_cr1 | drv_data->dma_cr1); 1160d5898e19SJarkko Nikula if (!pxa25x_ssp_comp(drv_data)) 1161d5898e19SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 1162ca632f55SGrant Likely 1163d5898e19SJarkko Nikula /* 1164d5898e19SJarkko Nikula * Stop the DMA if running. Note DMA callback handler may have unset 1165d5898e19SJarkko Nikula * the dma_running already, which is fine as stopping is not needed 1166d5898e19SJarkko Nikula * then but we shouldn't rely this flag for anything else than 1167d5898e19SJarkko Nikula * stopping. For instance to differentiate between PIO and DMA 1168d5898e19SJarkko Nikula * transfers. 1169d5898e19SJarkko Nikula */ 1170d5898e19SJarkko Nikula if (atomic_read(&drv_data->dma_running)) 1171d5898e19SJarkko Nikula pxa2xx_spi_dma_stop(drv_data); 1172ca632f55SGrant Likely } 1173ca632f55SGrant Likely 117451eea52dSLubomir Rintel static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller) 11757d94a505SMika Westerberg { 117651eea52dSLubomir Rintel struct driver_data *drv_data = spi_controller_get_devdata(controller); 11777d94a505SMika Westerberg 11787d94a505SMika Westerberg /* Disable the SSP now */ 117929d7e05cSLubomir Rintel pxa2xx_spi_off(drv_data); 11807d94a505SMika Westerberg 11817d94a505SMika Westerberg return 0; 11827d94a505SMika Westerberg } 11837d94a505SMika Westerberg 1184ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip, 1185ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info) 1186ca632f55SGrant Likely { 11873cc7b0e3SJarkko Nikula struct driver_data *drv_data = 11883cc7b0e3SJarkko Nikula spi_controller_get_devdata(spi->controller); 1189c18d925fSJan Kiszka struct gpio_desc *gpiod; 1190ca632f55SGrant Likely int err = 0; 1191ca632f55SGrant Likely 119299f499cdSMika Westerberg if (chip == NULL) 119399f499cdSMika Westerberg return 0; 119499f499cdSMika Westerberg 11956ac5a435SAndy Shevchenko if (drv_data->cs_gpiods) { 11966ac5a435SAndy Shevchenko gpiod = drv_data->cs_gpiods[spi->chip_select]; 11976ac5a435SAndy Shevchenko if (gpiod) { 1198c18d925fSJan Kiszka chip->gpiod_cs = gpiod; 119999f499cdSMika Westerberg chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 120099f499cdSMika Westerberg gpiod_set_value(gpiod, chip->gpio_cs_inverted); 12016ac5a435SAndy Shevchenko } 120299f499cdSMika Westerberg 120399f499cdSMika Westerberg return 0; 120499f499cdSMika Westerberg } 120599f499cdSMika Westerberg 120699f499cdSMika Westerberg if (chip_info == NULL) 1207ca632f55SGrant Likely return 0; 1208ca632f55SGrant Likely 1209ca632f55SGrant Likely /* NOTE: setup() can be called multiple times, possibly with 1210ca632f55SGrant Likely * different chip_info, release previously requested GPIO 1211ca632f55SGrant Likely */ 1212c18d925fSJan Kiszka if (chip->gpiod_cs) { 1213a885eebcSMark Brown gpiod_put(chip->gpiod_cs); 1214c18d925fSJan Kiszka chip->gpiod_cs = NULL; 1215c18d925fSJan Kiszka } 1216ca632f55SGrant Likely 1217ca632f55SGrant Likely /* If (*cs_control) is provided, ignore GPIO chip select */ 1218ca632f55SGrant Likely if (chip_info->cs_control) { 1219ca632f55SGrant Likely chip->cs_control = chip_info->cs_control; 1220ca632f55SGrant Likely return 0; 1221ca632f55SGrant Likely } 1222ca632f55SGrant Likely 1223ca632f55SGrant Likely if (gpio_is_valid(chip_info->gpio_cs)) { 1224ca632f55SGrant Likely err = gpio_request(chip_info->gpio_cs, "SPI_CS"); 1225ca632f55SGrant Likely if (err) { 1226f6bd03a7SJarkko Nikula dev_err(&spi->dev, "failed to request chip select GPIO%d\n", 1227f6bd03a7SJarkko Nikula chip_info->gpio_cs); 1228ca632f55SGrant Likely return err; 1229ca632f55SGrant Likely } 1230ca632f55SGrant Likely 1231c18d925fSJan Kiszka gpiod = gpio_to_desc(chip_info->gpio_cs); 1232c18d925fSJan Kiszka chip->gpiod_cs = gpiod; 1233ca632f55SGrant Likely chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 1234ca632f55SGrant Likely 1235c18d925fSJan Kiszka err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted); 1236ca632f55SGrant Likely } 1237ca632f55SGrant Likely 1238ca632f55SGrant Likely return err; 1239ca632f55SGrant Likely } 1240ca632f55SGrant Likely 1241ca632f55SGrant Likely static int setup(struct spi_device *spi) 1242ca632f55SGrant Likely { 1243bffc967eSJarkko Nikula struct pxa2xx_spi_chip *chip_info; 1244ca632f55SGrant Likely struct chip_data *chip; 1245dccf7369SJarkko Nikula const struct lpss_config *config; 12463cc7b0e3SJarkko Nikula struct driver_data *drv_data = 12473cc7b0e3SJarkko Nikula spi_controller_get_devdata(spi->controller); 1248a0d2642eSMika Westerberg uint tx_thres, tx_hi_thres, rx_thres; 1249a0d2642eSMika Westerberg 1250e5262d05SWeike Chen switch (drv_data->ssp_type) { 1251e5262d05SWeike Chen case QUARK_X1000_SSP: 1252e5262d05SWeike Chen tx_thres = TX_THRESH_QUARK_X1000_DFLT; 1253e5262d05SWeike Chen tx_hi_thres = 0; 1254e5262d05SWeike Chen rx_thres = RX_THRESH_QUARK_X1000_DFLT; 1255e5262d05SWeike Chen break; 12567c7289a4SAndy Shevchenko case CE4100_SSP: 12577c7289a4SAndy Shevchenko tx_thres = TX_THRESH_CE4100_DFLT; 12587c7289a4SAndy Shevchenko tx_hi_thres = 0; 12597c7289a4SAndy Shevchenko rx_thres = RX_THRESH_CE4100_DFLT; 12607c7289a4SAndy Shevchenko break; 126103fbf488SJarkko Nikula case LPSS_LPT_SSP: 126203fbf488SJarkko Nikula case LPSS_BYT_SSP: 126330f3a6abSMika Westerberg case LPSS_BSW_SSP: 126434cadd9cSJarkko Nikula case LPSS_SPT_SSP: 1265b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 1266fc0b2accSJarkko Nikula case LPSS_CNL_SSP: 1267dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 1268dccf7369SJarkko Nikula tx_thres = config->tx_threshold_lo; 1269dccf7369SJarkko Nikula tx_hi_thres = config->tx_threshold_hi; 1270dccf7369SJarkko Nikula rx_thres = config->rx_threshold; 1271e5262d05SWeike Chen break; 1272e5262d05SWeike Chen default: 1273a0d2642eSMika Westerberg tx_hi_thres = 0; 127451eea52dSLubomir Rintel if (spi_controller_is_slave(drv_data->controller)) { 1275ec93cb6fSLubomir Rintel tx_thres = 1; 1276ec93cb6fSLubomir Rintel rx_thres = 2; 1277ec93cb6fSLubomir Rintel } else { 1278ec93cb6fSLubomir Rintel tx_thres = TX_THRESH_DFLT; 1279a0d2642eSMika Westerberg rx_thres = RX_THRESH_DFLT; 1280ec93cb6fSLubomir Rintel } 1281e5262d05SWeike Chen break; 1282a0d2642eSMika Westerberg } 1283ca632f55SGrant Likely 1284ca632f55SGrant Likely /* Only alloc on first setup */ 1285ca632f55SGrant Likely chip = spi_get_ctldata(spi); 1286ca632f55SGrant Likely if (!chip) { 1287ca632f55SGrant Likely chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 12889deae459SJingoo Han if (!chip) 1289ca632f55SGrant Likely return -ENOMEM; 1290ca632f55SGrant Likely 1291ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 1292ca632f55SGrant Likely if (spi->chip_select > 4) { 1293f6bd03a7SJarkko Nikula dev_err(&spi->dev, 1294f6bd03a7SJarkko Nikula "failed setup: cs number must not be > 4.\n"); 1295ca632f55SGrant Likely kfree(chip); 1296ca632f55SGrant Likely return -EINVAL; 1297ca632f55SGrant Likely } 1298ca632f55SGrant Likely 1299ca632f55SGrant Likely chip->frm = spi->chip_select; 1300c18d925fSJan Kiszka } 130151eea52dSLubomir Rintel chip->enable_dma = drv_data->controller_info->enable_dma; 1302ca632f55SGrant Likely chip->timeout = TIMOUT_DFLT; 1303ca632f55SGrant Likely } 1304ca632f55SGrant Likely 1305ca632f55SGrant Likely /* protocol drivers may change the chip settings, so... 1306ca632f55SGrant Likely * if chip_info exists, use it */ 1307ca632f55SGrant Likely chip_info = spi->controller_data; 1308ca632f55SGrant Likely 1309ca632f55SGrant Likely /* chip_info isn't always needed */ 1310ca632f55SGrant Likely chip->cr1 = 0; 1311ca632f55SGrant Likely if (chip_info) { 1312ca632f55SGrant Likely if (chip_info->timeout) 1313ca632f55SGrant Likely chip->timeout = chip_info->timeout; 1314ca632f55SGrant Likely if (chip_info->tx_threshold) 1315ca632f55SGrant Likely tx_thres = chip_info->tx_threshold; 1316a0d2642eSMika Westerberg if (chip_info->tx_hi_threshold) 1317a0d2642eSMika Westerberg tx_hi_thres = chip_info->tx_hi_threshold; 1318ca632f55SGrant Likely if (chip_info->rx_threshold) 1319ca632f55SGrant Likely rx_thres = chip_info->rx_threshold; 1320ca632f55SGrant Likely chip->dma_threshold = 0; 1321ca632f55SGrant Likely if (chip_info->enable_loopback) 1322ca632f55SGrant Likely chip->cr1 = SSCR1_LBM; 1323ca632f55SGrant Likely } 132451eea52dSLubomir Rintel if (spi_controller_is_slave(drv_data->controller)) { 1325ec93cb6fSLubomir Rintel chip->cr1 |= SSCR1_SCFR; 1326ec93cb6fSLubomir Rintel chip->cr1 |= SSCR1_SCLKDIR; 1327ec93cb6fSLubomir Rintel chip->cr1 |= SSCR1_SFRMDIR; 1328ec93cb6fSLubomir Rintel chip->cr1 |= SSCR1_SPH; 1329ec93cb6fSLubomir Rintel } 1330ca632f55SGrant Likely 1331a0d2642eSMika Westerberg chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); 1332a0d2642eSMika Westerberg chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) 1333a0d2642eSMika Westerberg | SSITF_TxHiThresh(tx_hi_thres); 1334a0d2642eSMika Westerberg 1335ca632f55SGrant Likely /* set dma burst and threshold outside of chip_info path so that if 1336ca632f55SGrant Likely * chip_info goes away after setting chip->enable_dma, the 1337ca632f55SGrant Likely * burst and threshold can still respond to changes in bits_per_word */ 1338ca632f55SGrant Likely if (chip->enable_dma) { 1339ca632f55SGrant Likely /* set up legal burst and threshold for dma */ 1340cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, 1341cd7bed00SMika Westerberg spi->bits_per_word, 1342ca632f55SGrant Likely &chip->dma_burst_size, 1343ca632f55SGrant Likely &chip->dma_threshold)) { 1344f6bd03a7SJarkko Nikula dev_warn(&spi->dev, 1345f6bd03a7SJarkko Nikula "in setup: DMA burst size reduced to match bits_per_word\n"); 1346ca632f55SGrant Likely } 1347000c6af4SAndy Shevchenko dev_dbg(&spi->dev, 1348000c6af4SAndy Shevchenko "in setup: DMA burst size set to %u\n", 1349000c6af4SAndy Shevchenko chip->dma_burst_size); 1350ca632f55SGrant Likely } 1351ca632f55SGrant Likely 1352e5262d05SWeike Chen switch (drv_data->ssp_type) { 1353e5262d05SWeike Chen case QUARK_X1000_SSP: 1354e5262d05SWeike Chen chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) 1355e5262d05SWeike Chen & QUARK_X1000_SSCR1_RFT) 1356e5262d05SWeike Chen | (QUARK_X1000_SSCR1_TxTresh(tx_thres) 1357e5262d05SWeike Chen & QUARK_X1000_SSCR1_TFT); 1358e5262d05SWeike Chen break; 13597c7289a4SAndy Shevchenko case CE4100_SSP: 13607c7289a4SAndy Shevchenko chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) | 13617c7289a4SAndy Shevchenko (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT); 13627c7289a4SAndy Shevchenko break; 1363e5262d05SWeike Chen default: 1364e5262d05SWeike Chen chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | 1365e5262d05SWeike Chen (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); 1366e5262d05SWeike Chen break; 1367e5262d05SWeike Chen } 1368e5262d05SWeike Chen 1369ca632f55SGrant Likely chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); 1370ca632f55SGrant Likely chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) 1371ca632f55SGrant Likely | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); 1372ca632f55SGrant Likely 1373b833172fSMika Westerberg if (spi->mode & SPI_LOOP) 1374b833172fSMika Westerberg chip->cr1 |= SSCR1_LBM; 1375b833172fSMika Westerberg 1376ca632f55SGrant Likely if (spi->bits_per_word <= 8) { 1377ca632f55SGrant Likely chip->n_bytes = 1; 1378ca632f55SGrant Likely chip->read = u8_reader; 1379ca632f55SGrant Likely chip->write = u8_writer; 1380ca632f55SGrant Likely } else if (spi->bits_per_word <= 16) { 1381ca632f55SGrant Likely chip->n_bytes = 2; 1382ca632f55SGrant Likely chip->read = u16_reader; 1383ca632f55SGrant Likely chip->write = u16_writer; 1384ca632f55SGrant Likely } else if (spi->bits_per_word <= 32) { 1385ca632f55SGrant Likely chip->n_bytes = 4; 1386ca632f55SGrant Likely chip->read = u32_reader; 1387ca632f55SGrant Likely chip->write = u32_writer; 1388ca632f55SGrant Likely } 1389ca632f55SGrant Likely 1390ca632f55SGrant Likely spi_set_ctldata(spi, chip); 1391ca632f55SGrant Likely 1392ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 1393ca632f55SGrant Likely return 0; 1394ca632f55SGrant Likely 1395ca632f55SGrant Likely return setup_cs(spi, chip, chip_info); 1396ca632f55SGrant Likely } 1397ca632f55SGrant Likely 1398ca632f55SGrant Likely static void cleanup(struct spi_device *spi) 1399ca632f55SGrant Likely { 1400ca632f55SGrant Likely struct chip_data *chip = spi_get_ctldata(spi); 14013cc7b0e3SJarkko Nikula struct driver_data *drv_data = 14023cc7b0e3SJarkko Nikula spi_controller_get_devdata(spi->controller); 1403ca632f55SGrant Likely 1404ca632f55SGrant Likely if (!chip) 1405ca632f55SGrant Likely return; 1406ca632f55SGrant Likely 14076ac5a435SAndy Shevchenko if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods && 1408c18d925fSJan Kiszka chip->gpiod_cs) 1409a885eebcSMark Brown gpiod_put(chip->gpiod_cs); 1410ca632f55SGrant Likely 1411ca632f55SGrant Likely kfree(chip); 1412ca632f55SGrant Likely } 1413ca632f55SGrant Likely 14149b2d6119SLee Jones #ifdef CONFIG_ACPI 14158422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { 141603fbf488SJarkko Nikula { "INT33C0", LPSS_LPT_SSP }, 141703fbf488SJarkko Nikula { "INT33C1", LPSS_LPT_SSP }, 141803fbf488SJarkko Nikula { "INT3430", LPSS_LPT_SSP }, 141903fbf488SJarkko Nikula { "INT3431", LPSS_LPT_SSP }, 142003fbf488SJarkko Nikula { "80860F0E", LPSS_BYT_SSP }, 142130f3a6abSMika Westerberg { "8086228E", LPSS_BSW_SSP }, 142203fbf488SJarkko Nikula { }, 142303fbf488SJarkko Nikula }; 142403fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); 14259b2d6119SLee Jones #endif 142603fbf488SJarkko Nikula 142734cadd9cSJarkko Nikula /* 142834cadd9cSJarkko Nikula * PCI IDs of compound devices that integrate both host controller and private 142934cadd9cSJarkko Nikula * integrated DMA engine. Please note these are not used in module 143034cadd9cSJarkko Nikula * autoloading and probing in this module but matching the LPSS SSP type. 143134cadd9cSJarkko Nikula */ 143234cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { 143334cadd9cSJarkko Nikula /* SPT-LP */ 143434cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, 143534cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, 143634cadd9cSJarkko Nikula /* SPT-H */ 143734cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, 143834cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, 1439704d2b07SMika Westerberg /* KBL-H */ 1440704d2b07SMika Westerberg { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP }, 1441704d2b07SMika Westerberg { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP }, 14426157d4c2SJarkko Nikula /* CML-V */ 14436157d4c2SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa3a9), LPSS_SPT_SSP }, 14446157d4c2SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa3aa), LPSS_SPT_SSP }, 1445c1b03f11SJarkko Nikula /* BXT A-Step */ 1446b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP }, 1447b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP }, 1448b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP }, 1449c1b03f11SJarkko Nikula /* BXT B-Step */ 1450c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP }, 1451c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP }, 1452c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP }, 1453e18a80acSDavid E. Box /* GLK */ 1454e18a80acSDavid E. Box { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP }, 1455e18a80acSDavid E. Box { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP }, 1456e18a80acSDavid E. Box { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP }, 145722d71a50SMika Westerberg /* ICL-LP */ 145822d71a50SMika Westerberg { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP }, 145922d71a50SMika Westerberg { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP }, 146022d71a50SMika Westerberg { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP }, 14618cc77204SJarkko Nikula /* EHL */ 14628cc77204SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4b2a), LPSS_BXT_SSP }, 14638cc77204SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4b2b), LPSS_BXT_SSP }, 14648cc77204SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4b37), LPSS_BXT_SSP }, 14659c7315c9SJarkko Nikula /* JSL */ 14669c7315c9SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4daa), LPSS_CNL_SSP }, 14679c7315c9SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4dab), LPSS_CNL_SSP }, 14689c7315c9SJarkko Nikula { PCI_VDEVICE(INTEL, 0x4dfb), LPSS_CNL_SSP }, 1469cf961fceSJarkko Nikula /* TGL-H */ 1470cf961fceSJarkko Nikula { PCI_VDEVICE(INTEL, 0x43aa), LPSS_CNL_SSP }, 1471cf961fceSJarkko Nikula { PCI_VDEVICE(INTEL, 0x43ab), LPSS_CNL_SSP }, 1472cf961fceSJarkko Nikula { PCI_VDEVICE(INTEL, 0x43fb), LPSS_CNL_SSP }, 1473cf961fceSJarkko Nikula { PCI_VDEVICE(INTEL, 0x43fd), LPSS_CNL_SSP }, 1474a402e397SJarkko Nikula /* ADL-P */ 1475a402e397SJarkko Nikula { PCI_VDEVICE(INTEL, 0x51aa), LPSS_CNL_SSP }, 1476a402e397SJarkko Nikula { PCI_VDEVICE(INTEL, 0x51ab), LPSS_CNL_SSP }, 1477a402e397SJarkko Nikula { PCI_VDEVICE(INTEL, 0x51fb), LPSS_CNL_SSP }, 14788c4ffe4dSJarkko Nikula /* ADL-M */ 14798c4ffe4dSJarkko Nikula { PCI_VDEVICE(INTEL, 0x54aa), LPSS_CNL_SSP }, 14808c4ffe4dSJarkko Nikula { PCI_VDEVICE(INTEL, 0x54ab), LPSS_CNL_SSP }, 14818c4ffe4dSJarkko Nikula { PCI_VDEVICE(INTEL, 0x54fb), LPSS_CNL_SSP }, 1482b7c08cf8SJarkko Nikula /* APL */ 1483b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP }, 1484b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP }, 1485b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP }, 1486b8450e01SJarkko Nikula /* ADL-S */ 1487b8450e01SJarkko Nikula { PCI_VDEVICE(INTEL, 0x7aaa), LPSS_CNL_SSP }, 1488b8450e01SJarkko Nikula { PCI_VDEVICE(INTEL, 0x7aab), LPSS_CNL_SSP }, 1489b8450e01SJarkko Nikula { PCI_VDEVICE(INTEL, 0x7af9), LPSS_CNL_SSP }, 1490b8450e01SJarkko Nikula { PCI_VDEVICE(INTEL, 0x7afb), LPSS_CNL_SSP }, 1491fc0b2accSJarkko Nikula /* CNL-LP */ 1492fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP }, 1493fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP }, 1494fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP }, 1495fc0b2accSJarkko Nikula /* CNL-H */ 1496fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP }, 1497fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP }, 1498fc0b2accSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP }, 149941a91802SEvan Green /* CML-LP */ 150041a91802SEvan Green { PCI_VDEVICE(INTEL, 0x02aa), LPSS_CNL_SSP }, 150141a91802SEvan Green { PCI_VDEVICE(INTEL, 0x02ab), LPSS_CNL_SSP }, 150241a91802SEvan Green { PCI_VDEVICE(INTEL, 0x02fb), LPSS_CNL_SSP }, 1503f0cf17edSJarkko Nikula /* CML-H */ 1504f0cf17edSJarkko Nikula { PCI_VDEVICE(INTEL, 0x06aa), LPSS_CNL_SSP }, 1505f0cf17edSJarkko Nikula { PCI_VDEVICE(INTEL, 0x06ab), LPSS_CNL_SSP }, 1506f0cf17edSJarkko Nikula { PCI_VDEVICE(INTEL, 0x06fb), LPSS_CNL_SSP }, 1507a4127952SJarkko Nikula /* TGL-LP */ 1508a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0aa), LPSS_CNL_SSP }, 1509a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0ab), LPSS_CNL_SSP }, 1510a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0de), LPSS_CNL_SSP }, 1511a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0df), LPSS_CNL_SSP }, 1512a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0fb), LPSS_CNL_SSP }, 1513a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0fd), LPSS_CNL_SSP }, 1514a4127952SJarkko Nikula { PCI_VDEVICE(INTEL, 0xa0fe), LPSS_CNL_SSP }, 151594e5c23dSAxel Lin { }, 151634cadd9cSJarkko Nikula }; 151734cadd9cSJarkko Nikula 151887ae1d2dSLubomir Rintel static const struct of_device_id pxa2xx_spi_of_match[] = { 151987ae1d2dSLubomir Rintel { .compatible = "marvell,mmp2-ssp", .data = (void *)MMP2_SSP }, 152087ae1d2dSLubomir Rintel {}, 152187ae1d2dSLubomir Rintel }; 152287ae1d2dSLubomir Rintel MODULE_DEVICE_TABLE(of, pxa2xx_spi_of_match); 152387ae1d2dSLubomir Rintel 152487ae1d2dSLubomir Rintel #ifdef CONFIG_ACPI 152587ae1d2dSLubomir Rintel 1526365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev) 152787ae1d2dSLubomir Rintel { 1528365e856eSAndy Shevchenko struct acpi_device *adev; 152987ae1d2dSLubomir Rintel unsigned int devid; 153087ae1d2dSLubomir Rintel int port_id = -1; 153187ae1d2dSLubomir Rintel 1532365e856eSAndy Shevchenko adev = ACPI_COMPANION(dev); 153387ae1d2dSLubomir Rintel if (adev && adev->pnp.unique_id && 153487ae1d2dSLubomir Rintel !kstrtouint(adev->pnp.unique_id, 0, &devid)) 153587ae1d2dSLubomir Rintel port_id = devid; 153687ae1d2dSLubomir Rintel return port_id; 153787ae1d2dSLubomir Rintel } 153887ae1d2dSLubomir Rintel 153987ae1d2dSLubomir Rintel #else /* !CONFIG_ACPI */ 154087ae1d2dSLubomir Rintel 1541365e856eSAndy Shevchenko static int pxa2xx_spi_get_port_id(struct device *dev) 154287ae1d2dSLubomir Rintel { 154387ae1d2dSLubomir Rintel return -1; 154487ae1d2dSLubomir Rintel } 154587ae1d2dSLubomir Rintel 154687ae1d2dSLubomir Rintel #endif /* CONFIG_ACPI */ 154787ae1d2dSLubomir Rintel 154887ae1d2dSLubomir Rintel 154987ae1d2dSLubomir Rintel #ifdef CONFIG_PCI 155087ae1d2dSLubomir Rintel 155134cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) 155234cadd9cSJarkko Nikula { 15535ba846b1SAndy Shevchenko return param == chan->device->dev; 155434cadd9cSJarkko Nikula } 155534cadd9cSJarkko Nikula 155687ae1d2dSLubomir Rintel #endif /* CONFIG_PCI */ 155787ae1d2dSLubomir Rintel 155851eea52dSLubomir Rintel static struct pxa2xx_spi_controller * 15590db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev) 1560a3496855SMika Westerberg { 156151eea52dSLubomir Rintel struct pxa2xx_spi_controller *pdata; 1562a3496855SMika Westerberg struct ssp_device *ssp; 1563a3496855SMika Westerberg struct resource *res; 15646fb7427dSAndy Shevchenko struct device *parent = pdev->dev.parent; 15656fb7427dSAndy Shevchenko struct pci_dev *pcidev = dev_is_pci(parent) ? to_pci_dev(parent) : NULL; 156634cadd9cSJarkko Nikula const struct pci_device_id *pcidev_id = NULL; 156755ef8262SLubomir Rintel enum pxa_ssp_type type; 1568f2faa3ecSAndy Shevchenko const void *match; 1569a3496855SMika Westerberg 15706fb7427dSAndy Shevchenko if (pcidev) 15716fb7427dSAndy Shevchenko pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, pcidev); 1572a3496855SMika Westerberg 1573f2faa3ecSAndy Shevchenko match = device_get_match_data(&pdev->dev); 1574f2faa3ecSAndy Shevchenko if (match) 1575f2faa3ecSAndy Shevchenko type = (enum pxa_ssp_type)match; 157634cadd9cSJarkko Nikula else if (pcidev_id) 157755ef8262SLubomir Rintel type = (enum pxa_ssp_type)pcidev_id->driver_data; 157803fbf488SJarkko Nikula else 157914af1df3SAndy Shevchenko return ERR_PTR(-EINVAL); 158003fbf488SJarkko Nikula 1581cc0ee987SMika Westerberg pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 15829deae459SJingoo Han if (!pdata) 158314af1df3SAndy Shevchenko return ERR_PTR(-ENOMEM); 1584a3496855SMika Westerberg 1585a3496855SMika Westerberg ssp = &pdata->ssp; 1586a3496855SMika Westerberg 158777c544d2SAndy Shevchenko res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1588cbfd6a21SSachin Kamat ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); 1589cbfd6a21SSachin Kamat if (IS_ERR(ssp->mmio_base)) 159014af1df3SAndy Shevchenko return ERR_CAST(ssp->mmio_base); 1591a3496855SMika Westerberg 159277c544d2SAndy Shevchenko ssp->phys_base = res->start; 159377c544d2SAndy Shevchenko 159487ae1d2dSLubomir Rintel #ifdef CONFIG_PCI 159534cadd9cSJarkko Nikula if (pcidev_id) { 15966fb7427dSAndy Shevchenko pdata->tx_param = parent; 15976fb7427dSAndy Shevchenko pdata->rx_param = parent; 159834cadd9cSJarkko Nikula pdata->dma_filter = pxa2xx_spi_idma_filter; 159934cadd9cSJarkko Nikula } 160087ae1d2dSLubomir Rintel #endif 160134cadd9cSJarkko Nikula 1602a3496855SMika Westerberg ssp->clk = devm_clk_get(&pdev->dev, NULL); 16035eb263efSChuhong Yuan if (IS_ERR(ssp->clk)) 160414af1df3SAndy Shevchenko return ERR_CAST(ssp->clk); 1605a3496855SMika Westerberg 1606a3496855SMika Westerberg ssp->irq = platform_get_irq(pdev, 0); 16075eb263efSChuhong Yuan if (ssp->irq < 0) 160814af1df3SAndy Shevchenko return ERR_PTR(ssp->irq); 16095eb263efSChuhong Yuan 1610a3496855SMika Westerberg ssp->type = type; 16114f3d9577SAndy Shevchenko ssp->dev = &pdev->dev; 1612365e856eSAndy Shevchenko ssp->port_id = pxa2xx_spi_get_port_id(&pdev->dev); 1613a3496855SMika Westerberg 1614f2faa3ecSAndy Shevchenko pdata->is_slave = device_property_read_bool(&pdev->dev, "spi-slave"); 1615a3496855SMika Westerberg pdata->num_chipselect = 1; 1616cddb339bSMika Westerberg pdata->enable_dma = true; 161737821a82SAndy Shevchenko pdata->dma_burst_size = 1; 1618a3496855SMika Westerberg 1619a3496855SMika Westerberg return pdata; 1620a3496855SMika Westerberg } 1621a3496855SMika Westerberg 162251eea52dSLubomir Rintel static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller, 16233cc7b0e3SJarkko Nikula unsigned int cs) 16240c27d9cfSMika Westerberg { 162551eea52dSLubomir Rintel struct driver_data *drv_data = spi_controller_get_devdata(controller); 16260c27d9cfSMika Westerberg 1627c3dce24cSAndy Shevchenko if (has_acpi_companion(drv_data->ssp->dev)) { 16280c27d9cfSMika Westerberg switch (drv_data->ssp_type) { 16290c27d9cfSMika Westerberg /* 16300c27d9cfSMika Westerberg * For Atoms the ACPI DeviceSelection used by the Windows 16310c27d9cfSMika Westerberg * driver starts from 1 instead of 0 so translate it here 16320c27d9cfSMika Westerberg * to match what Linux expects. 16330c27d9cfSMika Westerberg */ 16340c27d9cfSMika Westerberg case LPSS_BYT_SSP: 163530f3a6abSMika Westerberg case LPSS_BSW_SSP: 16360c27d9cfSMika Westerberg return cs - 1; 16370c27d9cfSMika Westerberg 16380c27d9cfSMika Westerberg default: 16390c27d9cfSMika Westerberg break; 16400c27d9cfSMika Westerberg } 16410c27d9cfSMika Westerberg } 16420c27d9cfSMika Westerberg 16430c27d9cfSMika Westerberg return cs; 16440c27d9cfSMika Westerberg } 16450c27d9cfSMika Westerberg 1646b2662a16SDaniel Vetter static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi) 1647b2662a16SDaniel Vetter { 1648b2662a16SDaniel Vetter return MAX_DMA_LEN; 1649b2662a16SDaniel Vetter } 1650b2662a16SDaniel Vetter 1651fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev) 1652ca632f55SGrant Likely { 1653ca632f55SGrant Likely struct device *dev = &pdev->dev; 165451eea52dSLubomir Rintel struct pxa2xx_spi_controller *platform_info; 165551eea52dSLubomir Rintel struct spi_controller *controller; 1656ca632f55SGrant Likely struct driver_data *drv_data; 1657ca632f55SGrant Likely struct ssp_device *ssp; 16588b136baaSJarkko Nikula const struct lpss_config *config; 165999f499cdSMika Westerberg int status, count; 1660c039dd27SJarkko Nikula u32 tmp; 1661ca632f55SGrant Likely 1662851bacf5SMika Westerberg platform_info = dev_get_platdata(dev); 1663851bacf5SMika Westerberg if (!platform_info) { 16640db64215SJarkko Nikula platform_info = pxa2xx_spi_init_pdata(pdev); 166514af1df3SAndy Shevchenko if (IS_ERR(platform_info)) { 1666851bacf5SMika Westerberg dev_err(&pdev->dev, "missing platform data\n"); 166714af1df3SAndy Shevchenko return PTR_ERR(platform_info); 1668851bacf5SMika Westerberg } 1669a3496855SMika Westerberg } 1670ca632f55SGrant Likely 1671ca632f55SGrant Likely ssp = pxa_ssp_request(pdev->id, pdev->name); 1672851bacf5SMika Westerberg if (!ssp) 1673851bacf5SMika Westerberg ssp = &platform_info->ssp; 1674851bacf5SMika Westerberg 1675851bacf5SMika Westerberg if (!ssp->mmio_base) { 1676851bacf5SMika Westerberg dev_err(&pdev->dev, "failed to get ssp\n"); 1677ca632f55SGrant Likely return -ENODEV; 1678ca632f55SGrant Likely } 1679ca632f55SGrant Likely 1680ec93cb6fSLubomir Rintel if (platform_info->is_slave) 16815626308bSLukas Wunner controller = devm_spi_alloc_slave(dev, sizeof(*drv_data)); 1682ec93cb6fSLubomir Rintel else 16835626308bSLukas Wunner controller = devm_spi_alloc_master(dev, sizeof(*drv_data)); 1684ec93cb6fSLubomir Rintel 168551eea52dSLubomir Rintel if (!controller) { 168651eea52dSLubomir Rintel dev_err(&pdev->dev, "cannot alloc spi_controller\n"); 1687f2eed8caSAndy Shevchenko status = -ENOMEM; 1688f2eed8caSAndy Shevchenko goto out_error_controller_alloc; 1689ca632f55SGrant Likely } 169051eea52dSLubomir Rintel drv_data = spi_controller_get_devdata(controller); 169151eea52dSLubomir Rintel drv_data->controller = controller; 169251eea52dSLubomir Rintel drv_data->controller_info = platform_info; 1693ca632f55SGrant Likely drv_data->ssp = ssp; 1694ca632f55SGrant Likely 169551eea52dSLubomir Rintel controller->dev.of_node = pdev->dev.of_node; 1696ca632f55SGrant Likely /* the spi->mode bits understood by this driver: */ 169751eea52dSLubomir Rintel controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; 1698ca632f55SGrant Likely 169951eea52dSLubomir Rintel controller->bus_num = ssp->port_id; 170051eea52dSLubomir Rintel controller->dma_alignment = DMA_ALIGNMENT; 170151eea52dSLubomir Rintel controller->cleanup = cleanup; 170251eea52dSLubomir Rintel controller->setup = setup; 170351eea52dSLubomir Rintel controller->set_cs = pxa2xx_spi_set_cs; 170451eea52dSLubomir Rintel controller->transfer_one = pxa2xx_spi_transfer_one; 170551eea52dSLubomir Rintel controller->slave_abort = pxa2xx_spi_slave_abort; 170651eea52dSLubomir Rintel controller->handle_err = pxa2xx_spi_handle_err; 170751eea52dSLubomir Rintel controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; 170851eea52dSLubomir Rintel controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs; 170951eea52dSLubomir Rintel controller->auto_runtime_pm = true; 171051eea52dSLubomir Rintel controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX; 1711ca632f55SGrant Likely 1712ca632f55SGrant Likely drv_data->ssp_type = ssp->type; 1713ca632f55SGrant Likely 1714ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 1715e5262d05SWeike Chen switch (drv_data->ssp_type) { 1716e5262d05SWeike Chen case QUARK_X1000_SSP: 171751eea52dSLubomir Rintel controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1718e5262d05SWeike Chen break; 1719e5262d05SWeike Chen default: 172051eea52dSLubomir Rintel controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); 1721e5262d05SWeike Chen break; 1722e5262d05SWeike Chen } 1723e5262d05SWeike Chen 1724ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; 1725ca632f55SGrant Likely drv_data->dma_cr1 = 0; 1726ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR; 1727ca632f55SGrant Likely drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; 1728ca632f55SGrant Likely } else { 172951eea52dSLubomir Rintel controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1730ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; 17315928808eSMika Westerberg drv_data->dma_cr1 = DEFAULT_DMA_CR1; 1732ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR | SSSR_TINT; 1733ec93cb6fSLubomir Rintel drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS 1734ec93cb6fSLubomir Rintel | SSSR_ROR | SSSR_TUR; 1735ca632f55SGrant Likely } 1736ca632f55SGrant Likely 1737ca632f55SGrant Likely status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), 1738ca632f55SGrant Likely drv_data); 1739ca632f55SGrant Likely if (status < 0) { 1740ca632f55SGrant Likely dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); 174151eea52dSLubomir Rintel goto out_error_controller_alloc; 1742ca632f55SGrant Likely } 1743ca632f55SGrant Likely 1744ca632f55SGrant Likely /* Setup DMA if requested */ 1745ca632f55SGrant Likely if (platform_info->enable_dma) { 1746cd7bed00SMika Westerberg status = pxa2xx_spi_dma_setup(drv_data); 1747cd7bed00SMika Westerberg if (status) { 17488b57b11bSFlavio Suligoi dev_warn(dev, "no DMA channels available, using PIO\n"); 1749cd7bed00SMika Westerberg platform_info->enable_dma = false; 1750b6ced294SJarkko Nikula } else { 175151eea52dSLubomir Rintel controller->can_dma = pxa2xx_spi_can_dma; 1752bf9f742cSMark Brown controller->max_dma_len = MAX_DMA_LEN; 1753b2662a16SDaniel Vetter controller->max_transfer_size = 1754b2662a16SDaniel Vetter pxa2xx_spi_max_dma_transfer_size; 1755ca632f55SGrant Likely } 1756ca632f55SGrant Likely } 1757ca632f55SGrant Likely 1758ca632f55SGrant Likely /* Enable SOC clock */ 175962bbc864STobias Jordan status = clk_prepare_enable(ssp->clk); 176062bbc864STobias Jordan if (status) 176162bbc864STobias Jordan goto out_error_dma_irq_alloc; 17623343b7a6SMika Westerberg 176351eea52dSLubomir Rintel controller->max_speed_hz = clk_get_rate(ssp->clk); 176423cdddb2SJarkko Nikula /* 176523cdddb2SJarkko Nikula * Set minimum speed for all other platforms than Intel Quark which is 176623cdddb2SJarkko Nikula * able do under 1 Hz transfers. 176723cdddb2SJarkko Nikula */ 176823cdddb2SJarkko Nikula if (!pxa25x_ssp_comp(drv_data)) 176923cdddb2SJarkko Nikula controller->min_speed_hz = 177023cdddb2SJarkko Nikula DIV_ROUND_UP(controller->max_speed_hz, 4096); 177123cdddb2SJarkko Nikula else if (!is_quark_x1000_ssp(drv_data)) 177223cdddb2SJarkko Nikula controller->min_speed_hz = 177323cdddb2SJarkko Nikula DIV_ROUND_UP(controller->max_speed_hz, 512); 1774ca632f55SGrant Likely 17750c8ccd8bSAndy Shevchenko pxa_ssp_disable(ssp); 17760c8ccd8bSAndy Shevchenko 1777ca632f55SGrant Likely /* Load default SSP configuration */ 1778e5262d05SWeike Chen switch (drv_data->ssp_type) { 1779e5262d05SWeike Chen case QUARK_X1000_SSP: 17807c7289a4SAndy Shevchenko tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) | 17817c7289a4SAndy Shevchenko QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); 1782c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1783e5262d05SWeike Chen 1784e5262d05SWeike Chen /* using the Motorola SPI protocol and use 8 bit frame */ 17857c7289a4SAndy Shevchenko tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8); 17867c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR0, tmp); 1787e5262d05SWeike Chen break; 17887c7289a4SAndy Shevchenko case CE4100_SSP: 17897c7289a4SAndy Shevchenko tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) | 17907c7289a4SAndy Shevchenko CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT); 17917c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR1, tmp); 17927c7289a4SAndy Shevchenko tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); 17937c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR0, tmp); 1794a2dd8af0SAndy Shevchenko break; 1795e5262d05SWeike Chen default: 1796ec93cb6fSLubomir Rintel 179751eea52dSLubomir Rintel if (spi_controller_is_slave(controller)) { 1798ec93cb6fSLubomir Rintel tmp = SSCR1_SCFR | 1799ec93cb6fSLubomir Rintel SSCR1_SCLKDIR | 1800ec93cb6fSLubomir Rintel SSCR1_SFRMDIR | 1801ec93cb6fSLubomir Rintel SSCR1_RxTresh(2) | 1802ec93cb6fSLubomir Rintel SSCR1_TxTresh(1) | 1803ec93cb6fSLubomir Rintel SSCR1_SPH; 1804ec93cb6fSLubomir Rintel } else { 1805c039dd27SJarkko Nikula tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | 1806c039dd27SJarkko Nikula SSCR1_TxTresh(TX_THRESH_DFLT); 1807ec93cb6fSLubomir Rintel } 1808c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1809ec93cb6fSLubomir Rintel tmp = SSCR0_Motorola | SSCR0_DataSize(8); 181051eea52dSLubomir Rintel if (!spi_controller_is_slave(controller)) 1811ec93cb6fSLubomir Rintel tmp |= SSCR0_SCR(2); 1812c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, tmp); 1813e5262d05SWeike Chen break; 1814e5262d05SWeike Chen } 1815e5262d05SWeike Chen 1816ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1817c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 1818e5262d05SWeike Chen 1819e5262d05SWeike Chen if (!is_quark_x1000_ssp(drv_data)) 1820c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSPSP, 0); 1821ca632f55SGrant Likely 18228b136baaSJarkko Nikula if (is_lpss_ssp(drv_data)) { 18238b136baaSJarkko Nikula lpss_ssp_setup(drv_data); 18248b136baaSJarkko Nikula config = lpss_get_config(drv_data); 18258b136baaSJarkko Nikula if (config->reg_capabilities >= 0) { 18268b136baaSJarkko Nikula tmp = __lpss_ssp_read_priv(drv_data, 18278b136baaSJarkko Nikula config->reg_capabilities); 18288b136baaSJarkko Nikula tmp &= LPSS_CAPS_CS_EN_MASK; 18298b136baaSJarkko Nikula tmp >>= LPSS_CAPS_CS_EN_SHIFT; 18308b136baaSJarkko Nikula platform_info->num_chipselect = ffz(tmp); 183130f3a6abSMika Westerberg } else if (config->cs_num) { 183230f3a6abSMika Westerberg platform_info->num_chipselect = config->cs_num; 18338b136baaSJarkko Nikula } 18348b136baaSJarkko Nikula } 183551eea52dSLubomir Rintel controller->num_chipselect = platform_info->num_chipselect; 18368b136baaSJarkko Nikula 183799f499cdSMika Westerberg count = gpiod_count(&pdev->dev, "cs"); 18386ac5a435SAndy Shevchenko if (count > 0) { 18396ac5a435SAndy Shevchenko int i; 18406ac5a435SAndy Shevchenko 184151eea52dSLubomir Rintel controller->num_chipselect = max_t(int, count, 184251eea52dSLubomir Rintel controller->num_chipselect); 184399f499cdSMika Westerberg 18446ac5a435SAndy Shevchenko drv_data->cs_gpiods = devm_kcalloc(&pdev->dev, 184551eea52dSLubomir Rintel controller->num_chipselect, sizeof(struct gpio_desc *), 18466ac5a435SAndy Shevchenko GFP_KERNEL); 18476ac5a435SAndy Shevchenko if (!drv_data->cs_gpiods) { 18486ac5a435SAndy Shevchenko status = -ENOMEM; 18496ac5a435SAndy Shevchenko goto out_error_clock_enabled; 18506ac5a435SAndy Shevchenko } 18516ac5a435SAndy Shevchenko 185251eea52dSLubomir Rintel for (i = 0; i < controller->num_chipselect; i++) { 18536ac5a435SAndy Shevchenko struct gpio_desc *gpiod; 18546ac5a435SAndy Shevchenko 1855d35f2dc9SAndy Shevchenko gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS); 18566ac5a435SAndy Shevchenko if (IS_ERR(gpiod)) { 18576ac5a435SAndy Shevchenko /* Means use native chip select */ 18586ac5a435SAndy Shevchenko if (PTR_ERR(gpiod) == -ENOENT) 18596ac5a435SAndy Shevchenko continue; 18606ac5a435SAndy Shevchenko 186177d33897SLubomir Rintel status = PTR_ERR(gpiod); 18626ac5a435SAndy Shevchenko goto out_error_clock_enabled; 18636ac5a435SAndy Shevchenko } else { 18646ac5a435SAndy Shevchenko drv_data->cs_gpiods[i] = gpiod; 18656ac5a435SAndy Shevchenko } 18666ac5a435SAndy Shevchenko } 18676ac5a435SAndy Shevchenko } 18686ac5a435SAndy Shevchenko 186977d33897SLubomir Rintel if (platform_info->is_slave) { 187077d33897SLubomir Rintel drv_data->gpiod_ready = devm_gpiod_get_optional(dev, 187177d33897SLubomir Rintel "ready", GPIOD_OUT_LOW); 187277d33897SLubomir Rintel if (IS_ERR(drv_data->gpiod_ready)) { 187377d33897SLubomir Rintel status = PTR_ERR(drv_data->gpiod_ready); 187477d33897SLubomir Rintel goto out_error_clock_enabled; 187577d33897SLubomir Rintel } 187677d33897SLubomir Rintel } 187777d33897SLubomir Rintel 1878836d1a22SAntonio Ospite pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 1879836d1a22SAntonio Ospite pm_runtime_use_autosuspend(&pdev->dev); 1880836d1a22SAntonio Ospite pm_runtime_set_active(&pdev->dev); 1881836d1a22SAntonio Ospite pm_runtime_enable(&pdev->dev); 1882836d1a22SAntonio Ospite 1883ca632f55SGrant Likely /* Register with the SPI framework */ 1884ca632f55SGrant Likely platform_set_drvdata(pdev, drv_data); 188532e5b572SLukas Wunner status = spi_register_controller(controller); 1886ca632f55SGrant Likely if (status != 0) { 188751eea52dSLubomir Rintel dev_err(&pdev->dev, "problem registering spi controller\n"); 188812742045SLubomir Rintel goto out_error_pm_runtime_enabled; 1889ca632f55SGrant Likely } 1890ca632f55SGrant Likely 1891ca632f55SGrant Likely return status; 1892ca632f55SGrant Likely 189312742045SLubomir Rintel out_error_pm_runtime_enabled: 1894e2b714afSJarkko Nikula pm_runtime_disable(&pdev->dev); 189512742045SLubomir Rintel 189612742045SLubomir Rintel out_error_clock_enabled: 18973343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 189862bbc864STobias Jordan 189962bbc864STobias Jordan out_error_dma_irq_alloc: 1900cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1901ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1902ca632f55SGrant Likely 190351eea52dSLubomir Rintel out_error_controller_alloc: 1904ca632f55SGrant Likely pxa_ssp_free(ssp); 1905ca632f55SGrant Likely return status; 1906ca632f55SGrant Likely } 1907ca632f55SGrant Likely 1908ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev) 1909ca632f55SGrant Likely { 1910ca632f55SGrant Likely struct driver_data *drv_data = platform_get_drvdata(pdev); 19113d24b2a4SAndy Shevchenko struct ssp_device *ssp = drv_data->ssp; 1912ca632f55SGrant Likely 19137d94a505SMika Westerberg pm_runtime_get_sync(&pdev->dev); 19147d94a505SMika Westerberg 191532e5b572SLukas Wunner spi_unregister_controller(drv_data->controller); 191632e5b572SLukas Wunner 1917ca632f55SGrant Likely /* Disable the SSP at the peripheral and SOC level */ 19180c8ccd8bSAndy Shevchenko pxa_ssp_disable(ssp); 19193343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1920ca632f55SGrant Likely 1921ca632f55SGrant Likely /* Release DMA */ 192251eea52dSLubomir Rintel if (drv_data->controller_info->enable_dma) 1923cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1924ca632f55SGrant Likely 19257d94a505SMika Westerberg pm_runtime_put_noidle(&pdev->dev); 19267d94a505SMika Westerberg pm_runtime_disable(&pdev->dev); 19277d94a505SMika Westerberg 1928ca632f55SGrant Likely /* Release IRQ */ 1929ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1930ca632f55SGrant Likely 1931ca632f55SGrant Likely /* Release SSP */ 1932ca632f55SGrant Likely pxa_ssp_free(ssp); 1933ca632f55SGrant Likely 1934ca632f55SGrant Likely return 0; 1935ca632f55SGrant Likely } 1936ca632f55SGrant Likely 1937382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP 1938ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev) 1939ca632f55SGrant Likely { 1940ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1941ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1942bffc967eSJarkko Nikula int status; 1943ca632f55SGrant Likely 194451eea52dSLubomir Rintel status = spi_controller_suspend(drv_data->controller); 1945ca632f55SGrant Likely if (status != 0) 1946ca632f55SGrant Likely return status; 19470c8ccd8bSAndy Shevchenko 19480c8ccd8bSAndy Shevchenko pxa_ssp_disable(ssp); 19492b9375b9SDmitry Eremin-Solenikov 19502b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 19513343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1952ca632f55SGrant Likely 1953ca632f55SGrant Likely return 0; 1954ca632f55SGrant Likely } 1955ca632f55SGrant Likely 1956ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev) 1957ca632f55SGrant Likely { 1958ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1959ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1960bffc967eSJarkko Nikula int status; 1961ca632f55SGrant Likely 1962ca632f55SGrant Likely /* Enable the SSP clock */ 196362bbc864STobias Jordan if (!pm_runtime_suspended(dev)) { 196462bbc864STobias Jordan status = clk_prepare_enable(ssp->clk); 196562bbc864STobias Jordan if (status) 196662bbc864STobias Jordan return status; 196762bbc864STobias Jordan } 1968ca632f55SGrant Likely 1969ca632f55SGrant Likely /* Start the queue running */ 197051eea52dSLubomir Rintel return spi_controller_resume(drv_data->controller); 1971ca632f55SGrant Likely } 19727d94a505SMika Westerberg #endif 19737d94a505SMika Westerberg 1974ec833050SRafael J. Wysocki #ifdef CONFIG_PM 19757d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev) 19767d94a505SMika Westerberg { 19777d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 19787d94a505SMika Westerberg 19797d94a505SMika Westerberg clk_disable_unprepare(drv_data->ssp->clk); 19807d94a505SMika Westerberg return 0; 19817d94a505SMika Westerberg } 19827d94a505SMika Westerberg 19837d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev) 19847d94a505SMika Westerberg { 19857d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 198662bbc864STobias Jordan int status; 19877d94a505SMika Westerberg 198862bbc864STobias Jordan status = clk_prepare_enable(drv_data->ssp->clk); 198962bbc864STobias Jordan return status; 19907d94a505SMika Westerberg } 19917d94a505SMika Westerberg #endif 1992ca632f55SGrant Likely 1993ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = { 19947d94a505SMika Westerberg SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) 19957d94a505SMika Westerberg SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, 19967d94a505SMika Westerberg pxa2xx_spi_runtime_resume, NULL) 1997ca632f55SGrant Likely }; 1998ca632f55SGrant Likely 1999ca632f55SGrant Likely static struct platform_driver driver = { 2000ca632f55SGrant Likely .driver = { 2001ca632f55SGrant Likely .name = "pxa2xx-spi", 2002ca632f55SGrant Likely .pm = &pxa2xx_spi_pm_ops, 2003a3496855SMika Westerberg .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), 200487ae1d2dSLubomir Rintel .of_match_table = of_match_ptr(pxa2xx_spi_of_match), 2005ca632f55SGrant Likely }, 2006ca632f55SGrant Likely .probe = pxa2xx_spi_probe, 2007ca632f55SGrant Likely .remove = pxa2xx_spi_remove, 2008ca632f55SGrant Likely }; 2009ca632f55SGrant Likely 2010ca632f55SGrant Likely static int __init pxa2xx_spi_init(void) 2011ca632f55SGrant Likely { 2012ca632f55SGrant Likely return platform_driver_register(&driver); 2013ca632f55SGrant Likely } 2014ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init); 2015ca632f55SGrant Likely 2016ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void) 2017ca632f55SGrant Likely { 2018ca632f55SGrant Likely platform_driver_unregister(&driver); 2019ca632f55SGrant Likely } 2020ca632f55SGrant Likely module_exit(pxa2xx_spi_exit); 202151ebf6acSFlavio Suligoi 202251ebf6acSFlavio Suligoi MODULE_SOFTDEP("pre: dw_dmac"); 2023