1ca632f55SGrant Likely /* 2ca632f55SGrant Likely * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs 3a0d2642eSMika Westerberg * Copyright (C) 2013, Intel Corporation 4ca632f55SGrant Likely * 5ca632f55SGrant Likely * This program is free software; you can redistribute it and/or modify 6ca632f55SGrant Likely * it under the terms of the GNU General Public License as published by 7ca632f55SGrant Likely * the Free Software Foundation; either version 2 of the License, or 8ca632f55SGrant Likely * (at your option) any later version. 9ca632f55SGrant Likely * 10ca632f55SGrant Likely * This program is distributed in the hope that it will be useful, 11ca632f55SGrant Likely * but WITHOUT ANY WARRANTY; without even the implied warranty of 12ca632f55SGrant Likely * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13ca632f55SGrant Likely * GNU General Public License for more details. 14ca632f55SGrant Likely */ 15ca632f55SGrant Likely 168b136baaSJarkko Nikula #include <linux/bitops.h> 17ca632f55SGrant Likely #include <linux/init.h> 18ca632f55SGrant Likely #include <linux/module.h> 19ca632f55SGrant Likely #include <linux/device.h> 20ca632f55SGrant Likely #include <linux/ioport.h> 21ca632f55SGrant Likely #include <linux/errno.h> 22cbfd6a21SSachin Kamat #include <linux/err.h> 23ca632f55SGrant Likely #include <linux/interrupt.h> 249df461ecSAndy Shevchenko #include <linux/kernel.h> 2534cadd9cSJarkko Nikula #include <linux/pci.h> 26ca632f55SGrant Likely #include <linux/platform_device.h> 27ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h> 28ca632f55SGrant Likely #include <linux/spi/spi.h> 29ca632f55SGrant Likely #include <linux/delay.h> 30ca632f55SGrant Likely #include <linux/gpio.h> 31089bd46dSMika Westerberg #include <linux/gpio/consumer.h> 32ca632f55SGrant Likely #include <linux/slab.h> 333343b7a6SMika Westerberg #include <linux/clk.h> 347d94a505SMika Westerberg #include <linux/pm_runtime.h> 35a3496855SMika Westerberg #include <linux/acpi.h> 36ca632f55SGrant Likely 37cd7bed00SMika Westerberg #include "spi-pxa2xx.h" 38ca632f55SGrant Likely 39ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street"); 40ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); 41ca632f55SGrant Likely MODULE_LICENSE("GPL"); 42ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi"); 43ca632f55SGrant Likely 44ca632f55SGrant Likely #define TIMOUT_DFLT 1000 45ca632f55SGrant Likely 46ca632f55SGrant Likely /* 47ca632f55SGrant Likely * for testing SSCR1 changes that require SSP restart, basically 48ca632f55SGrant Likely * everything except the service and interrupt enables, the pxa270 developer 49ca632f55SGrant Likely * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this 50ca632f55SGrant Likely * list, but the PXA255 dev man says all bits without really meaning the 51ca632f55SGrant Likely * service and interrupt enables 52ca632f55SGrant Likely */ 53ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 54ca632f55SGrant Likely | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 55ca632f55SGrant Likely | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 56ca632f55SGrant Likely | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 57ca632f55SGrant Likely | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ 58ca632f55SGrant Likely | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 59ca632f55SGrant Likely 60e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ 61e5262d05SWeike Chen | QUARK_X1000_SSCR1_EFWR \ 62e5262d05SWeike Chen | QUARK_X1000_SSCR1_RFT \ 63e5262d05SWeike Chen | QUARK_X1000_SSCR1_TFT \ 64e5262d05SWeike Chen | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 65e5262d05SWeike Chen 667c7289a4SAndy Shevchenko #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 677c7289a4SAndy Shevchenko | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 687c7289a4SAndy Shevchenko | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 697c7289a4SAndy Shevchenko | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 707c7289a4SAndy Shevchenko | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \ 717c7289a4SAndy Shevchenko | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 727c7289a4SAndy Shevchenko 73624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) 74624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE BIT(0) 75624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH BIT(1) 768b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT 9 778b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT) 78a0d2642eSMika Westerberg 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; 95dccf7369SJarkko Nikula }; 96dccf7369SJarkko Nikula 97dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */ 98dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = { 99dccf7369SJarkko Nikula { /* LPSS_LPT_SSP */ 100dccf7369SJarkko Nikula .offset = 0x800, 101dccf7369SJarkko Nikula .reg_general = 0x08, 102dccf7369SJarkko Nikula .reg_ssp = 0x0c, 103dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 1048b136baaSJarkko Nikula .reg_capabilities = -1, 105dccf7369SJarkko Nikula .rx_threshold = 64, 106dccf7369SJarkko Nikula .tx_threshold_lo = 160, 107dccf7369SJarkko Nikula .tx_threshold_hi = 224, 108dccf7369SJarkko Nikula }, 109dccf7369SJarkko Nikula { /* LPSS_BYT_SSP */ 110dccf7369SJarkko Nikula .offset = 0x400, 111dccf7369SJarkko Nikula .reg_general = 0x08, 112dccf7369SJarkko Nikula .reg_ssp = 0x0c, 113dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 1148b136baaSJarkko Nikula .reg_capabilities = -1, 115dccf7369SJarkko Nikula .rx_threshold = 64, 116dccf7369SJarkko Nikula .tx_threshold_lo = 160, 117dccf7369SJarkko Nikula .tx_threshold_hi = 224, 118dccf7369SJarkko Nikula }, 11930f3a6abSMika Westerberg { /* LPSS_BSW_SSP */ 12030f3a6abSMika Westerberg .offset = 0x400, 12130f3a6abSMika Westerberg .reg_general = 0x08, 12230f3a6abSMika Westerberg .reg_ssp = 0x0c, 12330f3a6abSMika Westerberg .reg_cs_ctrl = 0x18, 12430f3a6abSMika Westerberg .reg_capabilities = -1, 12530f3a6abSMika Westerberg .rx_threshold = 64, 12630f3a6abSMika Westerberg .tx_threshold_lo = 160, 12730f3a6abSMika Westerberg .tx_threshold_hi = 224, 12830f3a6abSMika Westerberg .cs_sel_shift = 2, 12930f3a6abSMika Westerberg .cs_sel_mask = 1 << 2, 13030f3a6abSMika Westerberg .cs_num = 2, 13130f3a6abSMika Westerberg }, 13234cadd9cSJarkko Nikula { /* LPSS_SPT_SSP */ 13334cadd9cSJarkko Nikula .offset = 0x200, 13434cadd9cSJarkko Nikula .reg_general = -1, 13534cadd9cSJarkko Nikula .reg_ssp = 0x20, 13634cadd9cSJarkko Nikula .reg_cs_ctrl = 0x24, 13766ec246eSJarkko Nikula .reg_capabilities = -1, 13834cadd9cSJarkko Nikula .rx_threshold = 1, 13934cadd9cSJarkko Nikula .tx_threshold_lo = 32, 14034cadd9cSJarkko Nikula .tx_threshold_hi = 56, 14134cadd9cSJarkko Nikula }, 142b7c08cf8SJarkko Nikula { /* LPSS_BXT_SSP */ 143b7c08cf8SJarkko Nikula .offset = 0x200, 144b7c08cf8SJarkko Nikula .reg_general = -1, 145b7c08cf8SJarkko Nikula .reg_ssp = 0x20, 146b7c08cf8SJarkko Nikula .reg_cs_ctrl = 0x24, 147b7c08cf8SJarkko Nikula .reg_capabilities = 0xfc, 148b7c08cf8SJarkko Nikula .rx_threshold = 1, 149b7c08cf8SJarkko Nikula .tx_threshold_lo = 16, 150b7c08cf8SJarkko Nikula .tx_threshold_hi = 48, 151c1e4a53cSMika Westerberg .cs_sel_shift = 8, 152c1e4a53cSMika Westerberg .cs_sel_mask = 3 << 8, 153b7c08cf8SJarkko Nikula }, 154dccf7369SJarkko Nikula }; 155dccf7369SJarkko Nikula 156dccf7369SJarkko Nikula static inline const struct lpss_config 157dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data) 158dccf7369SJarkko Nikula { 159dccf7369SJarkko Nikula return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; 160dccf7369SJarkko Nikula } 161dccf7369SJarkko Nikula 162a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data) 163a0d2642eSMika Westerberg { 16403fbf488SJarkko Nikula switch (drv_data->ssp_type) { 16503fbf488SJarkko Nikula case LPSS_LPT_SSP: 16603fbf488SJarkko Nikula case LPSS_BYT_SSP: 16730f3a6abSMika Westerberg case LPSS_BSW_SSP: 16834cadd9cSJarkko Nikula case LPSS_SPT_SSP: 169b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 17003fbf488SJarkko Nikula return true; 17103fbf488SJarkko Nikula default: 17203fbf488SJarkko Nikula return false; 17303fbf488SJarkko Nikula } 174a0d2642eSMika Westerberg } 175a0d2642eSMika Westerberg 176e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data) 177e5262d05SWeike Chen { 178e5262d05SWeike Chen return drv_data->ssp_type == QUARK_X1000_SSP; 179e5262d05SWeike Chen } 180e5262d05SWeike Chen 1814fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) 1824fdb2424SWeike Chen { 1834fdb2424SWeike Chen switch (drv_data->ssp_type) { 184e5262d05SWeike Chen case QUARK_X1000_SSP: 185e5262d05SWeike Chen return QUARK_X1000_SSCR1_CHANGE_MASK; 1867c7289a4SAndy Shevchenko case CE4100_SSP: 1877c7289a4SAndy Shevchenko return CE4100_SSCR1_CHANGE_MASK; 1884fdb2424SWeike Chen default: 1894fdb2424SWeike Chen return SSCR1_CHANGE_MASK; 1904fdb2424SWeike Chen } 1914fdb2424SWeike Chen } 1924fdb2424SWeike Chen 1934fdb2424SWeike Chen static u32 1944fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) 1954fdb2424SWeike Chen { 1964fdb2424SWeike Chen switch (drv_data->ssp_type) { 197e5262d05SWeike Chen case QUARK_X1000_SSP: 198e5262d05SWeike Chen return RX_THRESH_QUARK_X1000_DFLT; 1997c7289a4SAndy Shevchenko case CE4100_SSP: 2007c7289a4SAndy Shevchenko return RX_THRESH_CE4100_DFLT; 2014fdb2424SWeike Chen default: 2024fdb2424SWeike Chen return RX_THRESH_DFLT; 2034fdb2424SWeike Chen } 2044fdb2424SWeike Chen } 2054fdb2424SWeike Chen 2064fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) 2074fdb2424SWeike Chen { 2084fdb2424SWeike Chen u32 mask; 2094fdb2424SWeike Chen 2104fdb2424SWeike Chen switch (drv_data->ssp_type) { 211e5262d05SWeike Chen case QUARK_X1000_SSP: 212e5262d05SWeike Chen mask = QUARK_X1000_SSSR_TFL_MASK; 213e5262d05SWeike Chen break; 2147c7289a4SAndy Shevchenko case CE4100_SSP: 2157c7289a4SAndy Shevchenko mask = CE4100_SSSR_TFL_MASK; 2167c7289a4SAndy Shevchenko break; 2174fdb2424SWeike Chen default: 2184fdb2424SWeike Chen mask = SSSR_TFL_MASK; 2194fdb2424SWeike Chen break; 2204fdb2424SWeike Chen } 2214fdb2424SWeike Chen 222c039dd27SJarkko Nikula return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask; 2234fdb2424SWeike Chen } 2244fdb2424SWeike Chen 2254fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, 2264fdb2424SWeike Chen u32 *sccr1_reg) 2274fdb2424SWeike Chen { 2284fdb2424SWeike Chen u32 mask; 2294fdb2424SWeike Chen 2304fdb2424SWeike Chen switch (drv_data->ssp_type) { 231e5262d05SWeike Chen case QUARK_X1000_SSP: 232e5262d05SWeike Chen mask = QUARK_X1000_SSCR1_RFT; 233e5262d05SWeike Chen break; 2347c7289a4SAndy Shevchenko case CE4100_SSP: 2357c7289a4SAndy Shevchenko mask = CE4100_SSCR1_RFT; 2367c7289a4SAndy Shevchenko break; 2374fdb2424SWeike Chen default: 2384fdb2424SWeike Chen mask = SSCR1_RFT; 2394fdb2424SWeike Chen break; 2404fdb2424SWeike Chen } 2414fdb2424SWeike Chen *sccr1_reg &= ~mask; 2424fdb2424SWeike Chen } 2434fdb2424SWeike Chen 2444fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, 2454fdb2424SWeike Chen u32 *sccr1_reg, u32 threshold) 2464fdb2424SWeike Chen { 2474fdb2424SWeike Chen switch (drv_data->ssp_type) { 248e5262d05SWeike Chen case QUARK_X1000_SSP: 249e5262d05SWeike Chen *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); 250e5262d05SWeike Chen break; 2517c7289a4SAndy Shevchenko case CE4100_SSP: 2527c7289a4SAndy Shevchenko *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold); 2537c7289a4SAndy Shevchenko break; 2544fdb2424SWeike Chen default: 2554fdb2424SWeike Chen *sccr1_reg |= SSCR1_RxTresh(threshold); 2564fdb2424SWeike Chen break; 2574fdb2424SWeike Chen } 2584fdb2424SWeike Chen } 2594fdb2424SWeike Chen 2604fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, 2614fdb2424SWeike Chen u32 clk_div, u8 bits) 2624fdb2424SWeike Chen { 2634fdb2424SWeike Chen switch (drv_data->ssp_type) { 264e5262d05SWeike Chen case QUARK_X1000_SSP: 265e5262d05SWeike Chen return clk_div 266e5262d05SWeike Chen | QUARK_X1000_SSCR0_Motorola 267e5262d05SWeike Chen | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits) 268e5262d05SWeike Chen | SSCR0_SSE; 2694fdb2424SWeike Chen default: 2704fdb2424SWeike Chen return clk_div 2714fdb2424SWeike Chen | SSCR0_Motorola 2724fdb2424SWeike Chen | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) 2734fdb2424SWeike Chen | SSCR0_SSE 2744fdb2424SWeike Chen | (bits > 16 ? SSCR0_EDSS : 0); 2754fdb2424SWeike Chen } 2764fdb2424SWeike Chen } 2774fdb2424SWeike Chen 278a0d2642eSMika Westerberg /* 279a0d2642eSMika Westerberg * Read and write LPSS SSP private registers. Caller must first check that 280a0d2642eSMika Westerberg * is_lpss_ssp() returns true before these can be called. 281a0d2642eSMika Westerberg */ 282a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) 283a0d2642eSMika Westerberg { 284a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 285a0d2642eSMika Westerberg return readl(drv_data->lpss_base + offset); 286a0d2642eSMika Westerberg } 287a0d2642eSMika Westerberg 288a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data, 289a0d2642eSMika Westerberg unsigned offset, u32 value) 290a0d2642eSMika Westerberg { 291a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 292a0d2642eSMika Westerberg writel(value, drv_data->lpss_base + offset); 293a0d2642eSMika Westerberg } 294a0d2642eSMika Westerberg 295a0d2642eSMika Westerberg /* 296a0d2642eSMika Westerberg * lpss_ssp_setup - perform LPSS SSP specific setup 297a0d2642eSMika Westerberg * @drv_data: pointer to the driver private data 298a0d2642eSMika Westerberg * 299a0d2642eSMika Westerberg * Perform LPSS SSP specific setup. This function must be called first if 300a0d2642eSMika Westerberg * one is going to use LPSS SSP private registers. 301a0d2642eSMika Westerberg */ 302a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data) 303a0d2642eSMika Westerberg { 304dccf7369SJarkko Nikula const struct lpss_config *config; 305dccf7369SJarkko Nikula u32 value; 306a0d2642eSMika Westerberg 307dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 308dccf7369SJarkko Nikula drv_data->lpss_base = drv_data->ioaddr + config->offset; 309a0d2642eSMika Westerberg 310a0d2642eSMika Westerberg /* Enable software chip select control */ 3110e897218SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 312624ea72eSJarkko Nikula value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH); 313624ea72eSJarkko Nikula value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH; 314dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 3150054e28dSMika Westerberg 3160054e28dSMika Westerberg /* Enable multiblock DMA transfers */ 3171de70612SMika Westerberg if (drv_data->master_info->enable_dma) { 318dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); 3191de70612SMika Westerberg 32082ba2c2aSJarkko Nikula if (config->reg_general >= 0) { 32182ba2c2aSJarkko Nikula value = __lpss_ssp_read_priv(drv_data, 32282ba2c2aSJarkko Nikula config->reg_general); 323624ea72eSJarkko Nikula value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE; 32482ba2c2aSJarkko Nikula __lpss_ssp_write_priv(drv_data, 32582ba2c2aSJarkko Nikula config->reg_general, value); 32682ba2c2aSJarkko Nikula } 3271de70612SMika Westerberg } 328a0d2642eSMika Westerberg } 329a0d2642eSMika Westerberg 330c1e4a53cSMika Westerberg static void lpss_ssp_select_cs(struct driver_data *drv_data, 331c1e4a53cSMika Westerberg const struct lpss_config *config) 332a0d2642eSMika Westerberg { 333d0283eb2SJarkko Nikula u32 value, cs; 334a0d2642eSMika Westerberg 335c1e4a53cSMika Westerberg if (!config->cs_sel_mask) 336c1e4a53cSMika Westerberg return; 337dccf7369SJarkko Nikula 338dccf7369SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 339c1e4a53cSMika Westerberg 3404fc0caacSJarkko Nikula cs = drv_data->master->cur_msg->spi->chip_select; 341c1e4a53cSMika Westerberg cs <<= config->cs_sel_shift; 342c1e4a53cSMika Westerberg if (cs != (value & config->cs_sel_mask)) { 343d0283eb2SJarkko Nikula /* 344c1e4a53cSMika Westerberg * When switching another chip select output active the 345c1e4a53cSMika Westerberg * output must be selected first and wait 2 ssp_clk cycles 346c1e4a53cSMika Westerberg * before changing state to active. Otherwise a short 347c1e4a53cSMika Westerberg * glitch will occur on the previous chip select since 348c1e4a53cSMika Westerberg * output select is latched but state control is not. 349d0283eb2SJarkko Nikula */ 350c1e4a53cSMika Westerberg value &= ~config->cs_sel_mask; 351d0283eb2SJarkko Nikula value |= cs; 352d0283eb2SJarkko Nikula __lpss_ssp_write_priv(drv_data, 353d0283eb2SJarkko Nikula config->reg_cs_ctrl, value); 354d0283eb2SJarkko Nikula ndelay(1000000000 / 355d0283eb2SJarkko Nikula (drv_data->master->max_speed_hz / 2)); 356d0283eb2SJarkko Nikula } 357d0283eb2SJarkko Nikula } 358c1e4a53cSMika Westerberg 359c1e4a53cSMika Westerberg static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable) 360c1e4a53cSMika Westerberg { 361c1e4a53cSMika Westerberg const struct lpss_config *config; 362c1e4a53cSMika Westerberg u32 value; 363c1e4a53cSMika Westerberg 364c1e4a53cSMika Westerberg config = lpss_get_config(drv_data); 365c1e4a53cSMika Westerberg 366c1e4a53cSMika Westerberg if (enable) 367c1e4a53cSMika Westerberg lpss_ssp_select_cs(drv_data, config); 368c1e4a53cSMika Westerberg 369c1e4a53cSMika Westerberg value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 370c1e4a53cSMika Westerberg if (enable) 371c1e4a53cSMika Westerberg value &= ~LPSS_CS_CONTROL_CS_HIGH; 372c1e4a53cSMika Westerberg else 373c1e4a53cSMika Westerberg value |= LPSS_CS_CONTROL_CS_HIGH; 374dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 375a0d2642eSMika Westerberg } 376a0d2642eSMika Westerberg 377ca632f55SGrant Likely static void cs_assert(struct driver_data *drv_data) 378ca632f55SGrant Likely { 37996579a4eSJarkko Nikula struct chip_data *chip = 38096579a4eSJarkko Nikula spi_get_ctldata(drv_data->master->cur_msg->spi); 381ca632f55SGrant Likely 382ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 38396579a4eSJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, chip->frm); 384ca632f55SGrant Likely return; 385ca632f55SGrant Likely } 386ca632f55SGrant Likely 387ca632f55SGrant Likely if (chip->cs_control) { 388ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_ASSERT); 389ca632f55SGrant Likely return; 390ca632f55SGrant Likely } 391ca632f55SGrant Likely 392a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 393ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); 394a0d2642eSMika Westerberg return; 395a0d2642eSMika Westerberg } 396a0d2642eSMika Westerberg 3977566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 398a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, true); 399ca632f55SGrant Likely } 400ca632f55SGrant Likely 401ca632f55SGrant Likely static void cs_deassert(struct driver_data *drv_data) 402ca632f55SGrant Likely { 40396579a4eSJarkko Nikula struct chip_data *chip = 40496579a4eSJarkko Nikula spi_get_ctldata(drv_data->master->cur_msg->spi); 405ca632f55SGrant Likely 406ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 407ca632f55SGrant Likely return; 408ca632f55SGrant Likely 409ca632f55SGrant Likely if (chip->cs_control) { 410ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_DEASSERT); 411ca632f55SGrant Likely return; 412ca632f55SGrant Likely } 413ca632f55SGrant Likely 414a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 415ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); 416a0d2642eSMika Westerberg return; 417a0d2642eSMika Westerberg } 418a0d2642eSMika Westerberg 4197566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 420a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, false); 421ca632f55SGrant Likely } 422ca632f55SGrant Likely 423cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data) 424ca632f55SGrant Likely { 425ca632f55SGrant Likely unsigned long limit = loops_per_jiffy << 1; 426ca632f55SGrant Likely 427ca632f55SGrant Likely do { 428c039dd27SJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 429c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 430c039dd27SJarkko Nikula } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); 431ca632f55SGrant Likely write_SSSR_CS(drv_data, SSSR_ROR); 432ca632f55SGrant Likely 433ca632f55SGrant Likely return limit; 434ca632f55SGrant Likely } 435ca632f55SGrant Likely 436ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data) 437ca632f55SGrant Likely { 438ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 439ca632f55SGrant Likely 4404fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 441ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 442ca632f55SGrant Likely return 0; 443ca632f55SGrant Likely 444c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, 0); 445ca632f55SGrant Likely drv_data->tx += n_bytes; 446ca632f55SGrant Likely 447ca632f55SGrant Likely return 1; 448ca632f55SGrant Likely } 449ca632f55SGrant Likely 450ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data) 451ca632f55SGrant Likely { 452ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 453ca632f55SGrant Likely 454c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 455ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 456c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 457ca632f55SGrant Likely drv_data->rx += n_bytes; 458ca632f55SGrant Likely } 459ca632f55SGrant Likely 460ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 461ca632f55SGrant Likely } 462ca632f55SGrant Likely 463ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data) 464ca632f55SGrant Likely { 4654fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 466ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 467ca632f55SGrant Likely return 0; 468ca632f55SGrant Likely 469c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); 470ca632f55SGrant Likely ++drv_data->tx; 471ca632f55SGrant Likely 472ca632f55SGrant Likely return 1; 473ca632f55SGrant Likely } 474ca632f55SGrant Likely 475ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data) 476ca632f55SGrant Likely { 477c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 478ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 479c039dd27SJarkko Nikula *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 480ca632f55SGrant Likely ++drv_data->rx; 481ca632f55SGrant Likely } 482ca632f55SGrant Likely 483ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 484ca632f55SGrant Likely } 485ca632f55SGrant Likely 486ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data) 487ca632f55SGrant Likely { 4884fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 489ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 490ca632f55SGrant Likely return 0; 491ca632f55SGrant Likely 492c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); 493ca632f55SGrant Likely drv_data->tx += 2; 494ca632f55SGrant Likely 495ca632f55SGrant Likely return 1; 496ca632f55SGrant Likely } 497ca632f55SGrant Likely 498ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data) 499ca632f55SGrant Likely { 500c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 501ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 502c039dd27SJarkko Nikula *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 503ca632f55SGrant Likely drv_data->rx += 2; 504ca632f55SGrant Likely } 505ca632f55SGrant Likely 506ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 507ca632f55SGrant Likely } 508ca632f55SGrant Likely 509ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data) 510ca632f55SGrant Likely { 5114fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 512ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 513ca632f55SGrant Likely return 0; 514ca632f55SGrant Likely 515c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); 516ca632f55SGrant Likely drv_data->tx += 4; 517ca632f55SGrant Likely 518ca632f55SGrant Likely return 1; 519ca632f55SGrant Likely } 520ca632f55SGrant Likely 521ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data) 522ca632f55SGrant Likely { 523c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 524ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 525c039dd27SJarkko Nikula *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 526ca632f55SGrant Likely drv_data->rx += 4; 527ca632f55SGrant Likely } 528ca632f55SGrant Likely 529ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 530ca632f55SGrant Likely } 531ca632f55SGrant Likely 532cd7bed00SMika Westerberg void *pxa2xx_spi_next_transfer(struct driver_data *drv_data) 533ca632f55SGrant Likely { 5344fc0caacSJarkko Nikula struct spi_message *msg = drv_data->master->cur_msg; 535ca632f55SGrant Likely struct spi_transfer *trans = drv_data->cur_transfer; 536ca632f55SGrant Likely 537ca632f55SGrant Likely /* Move to next transfer */ 538ca632f55SGrant Likely if (trans->transfer_list.next != &msg->transfers) { 539ca632f55SGrant Likely drv_data->cur_transfer = 540ca632f55SGrant Likely list_entry(trans->transfer_list.next, 541ca632f55SGrant Likely struct spi_transfer, 542ca632f55SGrant Likely transfer_list); 543ca632f55SGrant Likely return RUNNING_STATE; 544ca632f55SGrant Likely } else 545ca632f55SGrant Likely return DONE_STATE; 546ca632f55SGrant Likely } 547ca632f55SGrant Likely 548ca632f55SGrant Likely /* caller already set message->status; dma and pio irqs are blocked */ 549ca632f55SGrant Likely static void giveback(struct driver_data *drv_data) 550ca632f55SGrant Likely { 551ca632f55SGrant Likely struct spi_transfer* last_transfer; 552ca632f55SGrant Likely struct spi_message *msg; 5537a8d44bcSJarkko Nikula unsigned long timeout; 554ca632f55SGrant Likely 5554fc0caacSJarkko Nikula msg = drv_data->master->cur_msg; 556ca632f55SGrant Likely drv_data->cur_transfer = NULL; 557ca632f55SGrant Likely 55823e2c2aaSAxel Lin last_transfer = list_last_entry(&msg->transfers, struct spi_transfer, 559ca632f55SGrant Likely transfer_list); 560ca632f55SGrant Likely 561ca632f55SGrant Likely /* Delay if requested before any change in chip select */ 562ca632f55SGrant Likely if (last_transfer->delay_usecs) 563ca632f55SGrant Likely udelay(last_transfer->delay_usecs); 564ca632f55SGrant Likely 5657a8d44bcSJarkko Nikula /* Wait until SSP becomes idle before deasserting the CS */ 5667a8d44bcSJarkko Nikula timeout = jiffies + msecs_to_jiffies(10); 5677a8d44bcSJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY && 5687a8d44bcSJarkko Nikula !time_after(jiffies, timeout)) 5697a8d44bcSJarkko Nikula cpu_relax(); 5707a8d44bcSJarkko Nikula 571ca632f55SGrant Likely /* Drop chip select UNLESS cs_change is true or we are returning 572ca632f55SGrant Likely * a message with an error, or next message is for another chip 573ca632f55SGrant Likely */ 574ca632f55SGrant Likely if (!last_transfer->cs_change) 575ca632f55SGrant Likely cs_deassert(drv_data); 576ca632f55SGrant Likely else { 577ca632f55SGrant Likely struct spi_message *next_msg; 578ca632f55SGrant Likely 579ca632f55SGrant Likely /* Holding of cs was hinted, but we need to make sure 580ca632f55SGrant Likely * the next message is for the same chip. Don't waste 581ca632f55SGrant Likely * time with the following tests unless this was hinted. 582ca632f55SGrant Likely * 583ca632f55SGrant Likely * We cannot postpone this until pump_messages, because 584ca632f55SGrant Likely * after calling msg->complete (below) the driver that 585ca632f55SGrant Likely * sent the current message could be unloaded, which 586ca632f55SGrant Likely * could invalidate the cs_control() callback... 587ca632f55SGrant Likely */ 588ca632f55SGrant Likely 589ca632f55SGrant Likely /* get a pointer to the next message, if any */ 5907f86bde9SMika Westerberg next_msg = spi_get_next_queued_message(drv_data->master); 591ca632f55SGrant Likely 592ca632f55SGrant Likely /* see if the next and current messages point 593ca632f55SGrant Likely * to the same chip 594ca632f55SGrant Likely */ 595a52db659SChristophe Ricard if ((next_msg && next_msg->spi != msg->spi) || 596a52db659SChristophe Ricard msg->state == ERROR_STATE) 597ca632f55SGrant Likely cs_deassert(drv_data); 598ca632f55SGrant Likely } 599ca632f55SGrant Likely 600c957e8f0SMika Westerberg spi_finalize_current_message(drv_data->master); 601ca632f55SGrant Likely } 602ca632f55SGrant Likely 603ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data) 604ca632f55SGrant Likely { 60596579a4eSJarkko Nikula struct chip_data *chip = 60696579a4eSJarkko Nikula spi_get_ctldata(drv_data->master->cur_msg->spi); 607ca632f55SGrant Likely u32 sccr1_reg; 608ca632f55SGrant Likely 609c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; 610152bc19eSAndy Shevchenko switch (drv_data->ssp_type) { 611152bc19eSAndy Shevchenko case QUARK_X1000_SSP: 612152bc19eSAndy Shevchenko sccr1_reg &= ~QUARK_X1000_SSCR1_RFT; 613152bc19eSAndy Shevchenko break; 6147c7289a4SAndy Shevchenko case CE4100_SSP: 6157c7289a4SAndy Shevchenko sccr1_reg &= ~CE4100_SSCR1_RFT; 6167c7289a4SAndy Shevchenko break; 617152bc19eSAndy Shevchenko default: 618ca632f55SGrant Likely sccr1_reg &= ~SSCR1_RFT; 619152bc19eSAndy Shevchenko break; 620152bc19eSAndy Shevchenko } 621ca632f55SGrant Likely sccr1_reg |= chip->threshold; 622c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 623ca632f55SGrant Likely } 624ca632f55SGrant Likely 625ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg) 626ca632f55SGrant Likely { 627ca632f55SGrant Likely /* Stop and reset SSP */ 628ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 629ca632f55SGrant Likely reset_sccr1(drv_data); 630ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 631c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 632cd7bed00SMika Westerberg pxa2xx_spi_flush(drv_data); 633c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 634c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 635ca632f55SGrant Likely 636ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "%s\n", msg); 637ca632f55SGrant Likely 6384fc0caacSJarkko Nikula drv_data->master->cur_msg->state = ERROR_STATE; 639ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 640ca632f55SGrant Likely } 641ca632f55SGrant Likely 642ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data) 643ca632f55SGrant Likely { 64407550df0SJarkko Nikula /* Clear and disable interrupts */ 645ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 646ca632f55SGrant Likely reset_sccr1(drv_data); 647ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 648c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 649ca632f55SGrant Likely 650ca632f55SGrant Likely /* Update total byte transferred return count actual bytes read */ 6514fc0caacSJarkko Nikula drv_data->master->cur_msg->actual_length += drv_data->len - 652ca632f55SGrant Likely (drv_data->rx_end - drv_data->rx); 653ca632f55SGrant Likely 654ca632f55SGrant Likely /* Transfer delays and chip select release are 655ca632f55SGrant Likely * handled in pump_transfers or giveback 656ca632f55SGrant Likely */ 657ca632f55SGrant Likely 658ca632f55SGrant Likely /* Move to next transfer */ 6594fc0caacSJarkko Nikula drv_data->master->cur_msg->state = pxa2xx_spi_next_transfer(drv_data); 660ca632f55SGrant Likely 661ca632f55SGrant Likely /* Schedule transfer tasklet */ 662ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 663ca632f55SGrant Likely } 664ca632f55SGrant Likely 665ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data) 666ca632f55SGrant Likely { 667c039dd27SJarkko Nikula u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ? 668ca632f55SGrant Likely drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; 669ca632f55SGrant Likely 670c039dd27SJarkko Nikula u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask; 671ca632f55SGrant Likely 672ca632f55SGrant Likely if (irq_status & SSSR_ROR) { 673ca632f55SGrant Likely int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); 674ca632f55SGrant Likely return IRQ_HANDLED; 675ca632f55SGrant Likely } 676ca632f55SGrant Likely 677ca632f55SGrant Likely if (irq_status & SSSR_TINT) { 678c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); 679ca632f55SGrant Likely if (drv_data->read(drv_data)) { 680ca632f55SGrant Likely int_transfer_complete(drv_data); 681ca632f55SGrant Likely return IRQ_HANDLED; 682ca632f55SGrant Likely } 683ca632f55SGrant Likely } 684ca632f55SGrant Likely 685ca632f55SGrant Likely /* Drain rx fifo, Fill tx fifo and prevent overruns */ 686ca632f55SGrant Likely do { 687ca632f55SGrant Likely if (drv_data->read(drv_data)) { 688ca632f55SGrant Likely int_transfer_complete(drv_data); 689ca632f55SGrant Likely return IRQ_HANDLED; 690ca632f55SGrant Likely } 691ca632f55SGrant Likely } while (drv_data->write(drv_data)); 692ca632f55SGrant Likely 693ca632f55SGrant Likely if (drv_data->read(drv_data)) { 694ca632f55SGrant Likely int_transfer_complete(drv_data); 695ca632f55SGrant Likely return IRQ_HANDLED; 696ca632f55SGrant Likely } 697ca632f55SGrant Likely 698ca632f55SGrant Likely if (drv_data->tx == drv_data->tx_end) { 699ca632f55SGrant Likely u32 bytes_left; 700ca632f55SGrant Likely u32 sccr1_reg; 701ca632f55SGrant Likely 702c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 703ca632f55SGrant Likely sccr1_reg &= ~SSCR1_TIE; 704ca632f55SGrant Likely 705ca632f55SGrant Likely /* 706ca632f55SGrant Likely * PXA25x_SSP has no timeout, set up rx threshould for the 707ca632f55SGrant Likely * remaining RX bytes. 708ca632f55SGrant Likely */ 709ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 7104fdb2424SWeike Chen u32 rx_thre; 711ca632f55SGrant Likely 7124fdb2424SWeike Chen pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); 713ca632f55SGrant Likely 714ca632f55SGrant Likely bytes_left = drv_data->rx_end - drv_data->rx; 715ca632f55SGrant Likely switch (drv_data->n_bytes) { 716ca632f55SGrant Likely case 4: 717ca632f55SGrant Likely bytes_left >>= 1; 718ca632f55SGrant Likely case 2: 719ca632f55SGrant Likely bytes_left >>= 1; 720ca632f55SGrant Likely } 721ca632f55SGrant Likely 7224fdb2424SWeike Chen rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); 7234fdb2424SWeike Chen if (rx_thre > bytes_left) 7244fdb2424SWeike Chen rx_thre = bytes_left; 725ca632f55SGrant Likely 7264fdb2424SWeike Chen pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); 727ca632f55SGrant Likely } 728c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 729ca632f55SGrant Likely } 730ca632f55SGrant Likely 731ca632f55SGrant Likely /* We did something */ 732ca632f55SGrant Likely return IRQ_HANDLED; 733ca632f55SGrant Likely } 734ca632f55SGrant Likely 735*b0312482SJan Kiszka static void handle_bad_msg(struct driver_data *drv_data) 736*b0312482SJan Kiszka { 737*b0312482SJan Kiszka pxa2xx_spi_write(drv_data, SSCR0, 738*b0312482SJan Kiszka pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 739*b0312482SJan Kiszka pxa2xx_spi_write(drv_data, SSCR1, 740*b0312482SJan Kiszka pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1); 741*b0312482SJan Kiszka if (!pxa25x_ssp_comp(drv_data)) 742*b0312482SJan Kiszka pxa2xx_spi_write(drv_data, SSTO, 0); 743*b0312482SJan Kiszka write_SSSR_CS(drv_data, drv_data->clear_sr); 744*b0312482SJan Kiszka 745*b0312482SJan Kiszka dev_err(&drv_data->pdev->dev, 746*b0312482SJan Kiszka "bad message state in interrupt handler\n"); 747*b0312482SJan Kiszka } 748*b0312482SJan Kiszka 749ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id) 750ca632f55SGrant Likely { 751ca632f55SGrant Likely struct driver_data *drv_data = dev_id; 7527d94a505SMika Westerberg u32 sccr1_reg; 753ca632f55SGrant Likely u32 mask = drv_data->mask_sr; 754ca632f55SGrant Likely u32 status; 755ca632f55SGrant Likely 7567d94a505SMika Westerberg /* 7577d94a505SMika Westerberg * The IRQ might be shared with other peripherals so we must first 7587d94a505SMika Westerberg * check that are we RPM suspended or not. If we are we assume that 7597d94a505SMika Westerberg * the IRQ was not for us (we shouldn't be RPM suspended when the 7607d94a505SMika Westerberg * interrupt is enabled). 7617d94a505SMika Westerberg */ 7627d94a505SMika Westerberg if (pm_runtime_suspended(&drv_data->pdev->dev)) 7637d94a505SMika Westerberg return IRQ_NONE; 7647d94a505SMika Westerberg 765269e4a41SMika Westerberg /* 766269e4a41SMika Westerberg * If the device is not yet in RPM suspended state and we get an 767269e4a41SMika Westerberg * interrupt that is meant for another device, check if status bits 768269e4a41SMika Westerberg * are all set to one. That means that the device is already 769269e4a41SMika Westerberg * powered off. 770269e4a41SMika Westerberg */ 771c039dd27SJarkko Nikula status = pxa2xx_spi_read(drv_data, SSSR); 772269e4a41SMika Westerberg if (status == ~0) 773269e4a41SMika Westerberg return IRQ_NONE; 774269e4a41SMika Westerberg 775c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 776ca632f55SGrant Likely 777ca632f55SGrant Likely /* Ignore possible writes if we don't need to write */ 778ca632f55SGrant Likely if (!(sccr1_reg & SSCR1_TIE)) 779ca632f55SGrant Likely mask &= ~SSSR_TFS; 780ca632f55SGrant Likely 78102bc933eSTan, Jui Nee /* Ignore RX timeout interrupt if it is disabled */ 78202bc933eSTan, Jui Nee if (!(sccr1_reg & SSCR1_TINTE)) 78302bc933eSTan, Jui Nee mask &= ~SSSR_TINT; 78402bc933eSTan, Jui Nee 785ca632f55SGrant Likely if (!(status & mask)) 786ca632f55SGrant Likely return IRQ_NONE; 787ca632f55SGrant Likely 7884fc0caacSJarkko Nikula if (!drv_data->master->cur_msg) { 789*b0312482SJan Kiszka handle_bad_msg(drv_data); 790ca632f55SGrant Likely /* Never fail */ 791ca632f55SGrant Likely return IRQ_HANDLED; 792ca632f55SGrant Likely } 793ca632f55SGrant Likely 794ca632f55SGrant Likely return drv_data->transfer_handler(drv_data); 795ca632f55SGrant Likely } 796ca632f55SGrant Likely 797e5262d05SWeike Chen /* 7989df461ecSAndy Shevchenko * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply 7999df461ecSAndy Shevchenko * input frequency by fractions of 2^24. It also has a divider by 5. 8009df461ecSAndy Shevchenko * 8019df461ecSAndy Shevchenko * There are formulas to get baud rate value for given input frequency and 8029df461ecSAndy Shevchenko * divider parameters, such as DDS_CLK_RATE and SCR: 8039df461ecSAndy Shevchenko * 8049df461ecSAndy Shevchenko * Fsys = 200MHz 8059df461ecSAndy Shevchenko * 8069df461ecSAndy Shevchenko * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) 8079df461ecSAndy Shevchenko * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) 8089df461ecSAndy Shevchenko * 8099df461ecSAndy Shevchenko * DDS_CLK_RATE either 2^n or 2^n / 5. 8109df461ecSAndy Shevchenko * SCR is in range 0 .. 255 8119df461ecSAndy Shevchenko * 8129df461ecSAndy Shevchenko * Divisor = 5^i * 2^j * 2 * k 8139df461ecSAndy Shevchenko * i = [0, 1] i = 1 iff j = 0 or j > 3 8149df461ecSAndy Shevchenko * j = [0, 23] j = 0 iff i = 1 8159df461ecSAndy Shevchenko * k = [1, 256] 8169df461ecSAndy Shevchenko * Special case: j = 0, i = 1: Divisor = 2 / 5 8179df461ecSAndy Shevchenko * 8189df461ecSAndy Shevchenko * Accordingly to the specification the recommended values for DDS_CLK_RATE 8199df461ecSAndy Shevchenko * are: 8209df461ecSAndy Shevchenko * Case 1: 2^n, n = [0, 23] 8219df461ecSAndy Shevchenko * Case 2: 2^24 * 2 / 5 (0x666666) 8229df461ecSAndy Shevchenko * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) 8239df461ecSAndy Shevchenko * 8249df461ecSAndy Shevchenko * In all cases the lowest possible value is better. 8259df461ecSAndy Shevchenko * 8269df461ecSAndy Shevchenko * The function calculates parameters for all cases and chooses the one closest 8279df461ecSAndy Shevchenko * to the asked baud rate. 828e5262d05SWeike Chen */ 8299df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) 830e5262d05SWeike Chen { 8319df461ecSAndy Shevchenko unsigned long xtal = 200000000; 8329df461ecSAndy Shevchenko unsigned long fref = xtal / 2; /* mandatory division by 2, 8339df461ecSAndy Shevchenko see (2) */ 8349df461ecSAndy Shevchenko /* case 3 */ 8359df461ecSAndy Shevchenko unsigned long fref1 = fref / 2; /* case 1 */ 8369df461ecSAndy Shevchenko unsigned long fref2 = fref * 2 / 5; /* case 2 */ 8379df461ecSAndy Shevchenko unsigned long scale; 8389df461ecSAndy Shevchenko unsigned long q, q1, q2; 8399df461ecSAndy Shevchenko long r, r1, r2; 8409df461ecSAndy Shevchenko u32 mul; 841e5262d05SWeike Chen 8429df461ecSAndy Shevchenko /* Case 1 */ 8439df461ecSAndy Shevchenko 8449df461ecSAndy Shevchenko /* Set initial value for DDS_CLK_RATE */ 8459df461ecSAndy Shevchenko mul = (1 << 24) >> 1; 8469df461ecSAndy Shevchenko 8479df461ecSAndy Shevchenko /* Calculate initial quot */ 8483ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref1, rate); 8499df461ecSAndy Shevchenko 8509df461ecSAndy Shevchenko /* Scale q1 if it's too big */ 8519df461ecSAndy Shevchenko if (q1 > 256) { 8529df461ecSAndy Shevchenko /* Scale q1 to range [1, 512] */ 8539df461ecSAndy Shevchenko scale = fls_long(q1 - 1); 8549df461ecSAndy Shevchenko if (scale > 9) { 8559df461ecSAndy Shevchenko q1 >>= scale - 9; 8569df461ecSAndy Shevchenko mul >>= scale - 9; 8579df461ecSAndy Shevchenko } 8589df461ecSAndy Shevchenko 8599df461ecSAndy Shevchenko /* Round the result if we have a remainder */ 8609df461ecSAndy Shevchenko q1 += q1 & 1; 8619df461ecSAndy Shevchenko } 8629df461ecSAndy Shevchenko 8639df461ecSAndy Shevchenko /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ 8649df461ecSAndy Shevchenko scale = __ffs(q1); 8659df461ecSAndy Shevchenko q1 >>= scale; 8669df461ecSAndy Shevchenko mul >>= scale; 8679df461ecSAndy Shevchenko 8689df461ecSAndy Shevchenko /* Get the remainder */ 8699df461ecSAndy Shevchenko r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); 8709df461ecSAndy Shevchenko 8719df461ecSAndy Shevchenko /* Case 2 */ 8729df461ecSAndy Shevchenko 8733ad48062SAndy Shevchenko q2 = DIV_ROUND_UP(fref2, rate); 8749df461ecSAndy Shevchenko r2 = abs(fref2 / q2 - rate); 8759df461ecSAndy Shevchenko 8769df461ecSAndy Shevchenko /* 8779df461ecSAndy Shevchenko * Choose the best between two: less remainder we have the better. We 8789df461ecSAndy Shevchenko * can't go case 2 if q2 is greater than 256 since SCR register can 8799df461ecSAndy Shevchenko * hold only values 0 .. 255. 8809df461ecSAndy Shevchenko */ 8819df461ecSAndy Shevchenko if (r2 >= r1 || q2 > 256) { 8829df461ecSAndy Shevchenko /* case 1 is better */ 8839df461ecSAndy Shevchenko r = r1; 8849df461ecSAndy Shevchenko q = q1; 8859df461ecSAndy Shevchenko } else { 8869df461ecSAndy Shevchenko /* case 2 is better */ 8879df461ecSAndy Shevchenko r = r2; 8889df461ecSAndy Shevchenko q = q2; 8899df461ecSAndy Shevchenko mul = (1 << 24) * 2 / 5; 8909df461ecSAndy Shevchenko } 8919df461ecSAndy Shevchenko 8923ad48062SAndy Shevchenko /* Check case 3 only if the divisor is big enough */ 8939df461ecSAndy Shevchenko if (fref / rate >= 80) { 8949df461ecSAndy Shevchenko u64 fssp; 8959df461ecSAndy Shevchenko u32 m; 8969df461ecSAndy Shevchenko 8979df461ecSAndy Shevchenko /* Calculate initial quot */ 8983ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref, rate); 8999df461ecSAndy Shevchenko m = (1 << 24) / q1; 9009df461ecSAndy Shevchenko 9019df461ecSAndy Shevchenko /* Get the remainder */ 9029df461ecSAndy Shevchenko fssp = (u64)fref * m; 9039df461ecSAndy Shevchenko do_div(fssp, 1 << 24); 9049df461ecSAndy Shevchenko r1 = abs(fssp - rate); 9059df461ecSAndy Shevchenko 9069df461ecSAndy Shevchenko /* Choose this one if it suits better */ 9079df461ecSAndy Shevchenko if (r1 < r) { 9089df461ecSAndy Shevchenko /* case 3 is better */ 9099df461ecSAndy Shevchenko q = 1; 9109df461ecSAndy Shevchenko mul = m; 911e5262d05SWeike Chen } 912e5262d05SWeike Chen } 913e5262d05SWeike Chen 9149df461ecSAndy Shevchenko *dds = mul; 9159df461ecSAndy Shevchenko return q - 1; 916e5262d05SWeike Chen } 917e5262d05SWeike Chen 9183343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) 919ca632f55SGrant Likely { 9200eca7cf2SJarkko Nikula unsigned long ssp_clk = drv_data->master->max_speed_hz; 9213343b7a6SMika Westerberg const struct ssp_device *ssp = drv_data->ssp; 9223343b7a6SMika Westerberg 9233343b7a6SMika Westerberg rate = min_t(int, ssp_clk, rate); 924ca632f55SGrant Likely 925ca632f55SGrant Likely if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) 926025ffe88SAndy Shevchenko return (ssp_clk / (2 * rate) - 1) & 0xff; 927ca632f55SGrant Likely else 928025ffe88SAndy Shevchenko return (ssp_clk / rate - 1) & 0xfff; 929ca632f55SGrant Likely } 930ca632f55SGrant Likely 931e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, 932d2c2f6a4SAndy Shevchenko int rate) 933e5262d05SWeike Chen { 93496579a4eSJarkko Nikula struct chip_data *chip = 93596579a4eSJarkko Nikula spi_get_ctldata(drv_data->master->cur_msg->spi); 936025ffe88SAndy Shevchenko unsigned int clk_div; 937e5262d05SWeike Chen 938e5262d05SWeike Chen switch (drv_data->ssp_type) { 939e5262d05SWeike Chen case QUARK_X1000_SSP: 9409df461ecSAndy Shevchenko clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); 941eecacf73SDan Carpenter break; 942e5262d05SWeike Chen default: 943025ffe88SAndy Shevchenko clk_div = ssp_get_clk_div(drv_data, rate); 944eecacf73SDan Carpenter break; 945e5262d05SWeike Chen } 946025ffe88SAndy Shevchenko return clk_div << 8; 947e5262d05SWeike Chen } 948e5262d05SWeike Chen 949b6ced294SJarkko Nikula static bool pxa2xx_spi_can_dma(struct spi_master *master, 950b6ced294SJarkko Nikula struct spi_device *spi, 951b6ced294SJarkko Nikula struct spi_transfer *xfer) 952b6ced294SJarkko Nikula { 953b6ced294SJarkko Nikula struct chip_data *chip = spi_get_ctldata(spi); 954b6ced294SJarkko Nikula 955b6ced294SJarkko Nikula return chip->enable_dma && 956b6ced294SJarkko Nikula xfer->len <= MAX_DMA_LEN && 957b6ced294SJarkko Nikula xfer->len >= chip->dma_burst_size; 958b6ced294SJarkko Nikula } 959b6ced294SJarkko Nikula 960ca632f55SGrant Likely static void pump_transfers(unsigned long data) 961ca632f55SGrant Likely { 962ca632f55SGrant Likely struct driver_data *drv_data = (struct driver_data *)data; 9632d7537d8SJarkko Nikula struct spi_master *master = drv_data->master; 9644fc0caacSJarkko Nikula struct spi_message *message = master->cur_msg; 96596579a4eSJarkko Nikula struct chip_data *chip = spi_get_ctldata(message->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 struct spi_transfer *transfer; 970bffc967eSJarkko Nikula struct spi_transfer *previous; 971bffc967eSJarkko Nikula u32 clk_div; 972bffc967eSJarkko Nikula u8 bits; 973bffc967eSJarkko Nikula u32 speed; 974ca632f55SGrant Likely u32 cr0; 975ca632f55SGrant Likely u32 cr1; 9767d1f1bf6SAndy Shevchenko int err; 977b6ced294SJarkko Nikula int dma_mapped; 978ca632f55SGrant Likely 979ca632f55SGrant Likely /* Get current state information */ 980ca632f55SGrant Likely transfer = drv_data->cur_transfer; 981ca632f55SGrant Likely 982ca632f55SGrant Likely /* Handle for abort */ 983ca632f55SGrant Likely if (message->state == ERROR_STATE) { 984ca632f55SGrant Likely message->status = -EIO; 985ca632f55SGrant Likely giveback(drv_data); 986ca632f55SGrant Likely return; 987ca632f55SGrant Likely } 988ca632f55SGrant Likely 989ca632f55SGrant Likely /* Handle end of message */ 990ca632f55SGrant Likely if (message->state == DONE_STATE) { 991ca632f55SGrant Likely message->status = 0; 992ca632f55SGrant Likely giveback(drv_data); 993ca632f55SGrant Likely return; 994ca632f55SGrant Likely } 995ca632f55SGrant Likely 996ca632f55SGrant Likely /* Delay if requested at end of transfer before CS change */ 997ca632f55SGrant Likely if (message->state == RUNNING_STATE) { 998ca632f55SGrant Likely previous = list_entry(transfer->transfer_list.prev, 999ca632f55SGrant Likely struct spi_transfer, 1000ca632f55SGrant Likely transfer_list); 1001ca632f55SGrant Likely if (previous->delay_usecs) 1002ca632f55SGrant Likely udelay(previous->delay_usecs); 1003ca632f55SGrant Likely 1004ca632f55SGrant Likely /* Drop chip select only if cs_change is requested */ 1005ca632f55SGrant Likely if (previous->cs_change) 1006ca632f55SGrant Likely cs_deassert(drv_data); 1007ca632f55SGrant Likely } 1008ca632f55SGrant Likely 1009cd7bed00SMika Westerberg /* Check if we can DMA this transfer */ 1010b6ced294SJarkko Nikula if (transfer->len > MAX_DMA_LEN && chip->enable_dma) { 1011ca632f55SGrant Likely 1012ca632f55SGrant Likely /* reject already-mapped transfers; PIO won't always work */ 1013ca632f55SGrant Likely if (message->is_dma_mapped 1014ca632f55SGrant Likely || transfer->rx_dma || transfer->tx_dma) { 1015ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, 1016f6bd03a7SJarkko Nikula "pump_transfers: mapped transfer length of " 1017f6bd03a7SJarkko Nikula "%u is greater than %d\n", 1018ca632f55SGrant Likely transfer->len, MAX_DMA_LEN); 1019ca632f55SGrant Likely message->status = -EINVAL; 1020ca632f55SGrant Likely giveback(drv_data); 1021ca632f55SGrant Likely return; 1022ca632f55SGrant Likely } 1023ca632f55SGrant Likely 1024ca632f55SGrant Likely /* warn ... we force this to PIO mode */ 1025f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 1026f6bd03a7SJarkko Nikula "pump_transfers: DMA disabled for transfer length %ld " 1027ca632f55SGrant Likely "greater than %d\n", 1028ca632f55SGrant Likely (long)drv_data->len, MAX_DMA_LEN); 1029ca632f55SGrant Likely } 1030ca632f55SGrant Likely 1031ca632f55SGrant Likely /* Setup the transfer state based on the type of transfer */ 1032cd7bed00SMika Westerberg if (pxa2xx_spi_flush(drv_data) == 0) { 1033ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); 1034ca632f55SGrant Likely message->status = -EIO; 1035ca632f55SGrant Likely giveback(drv_data); 1036ca632f55SGrant Likely return; 1037ca632f55SGrant Likely } 1038ca632f55SGrant Likely drv_data->n_bytes = chip->n_bytes; 1039ca632f55SGrant Likely drv_data->tx = (void *)transfer->tx_buf; 1040ca632f55SGrant Likely drv_data->tx_end = drv_data->tx + transfer->len; 1041ca632f55SGrant Likely drv_data->rx = transfer->rx_buf; 1042ca632f55SGrant Likely drv_data->rx_end = drv_data->rx + transfer->len; 1043cd7bed00SMika Westerberg drv_data->len = transfer->len; 1044ca632f55SGrant Likely drv_data->write = drv_data->tx ? chip->write : null_writer; 1045ca632f55SGrant Likely drv_data->read = drv_data->rx ? chip->read : null_reader; 1046ca632f55SGrant Likely 1047ca632f55SGrant Likely /* Change speed and bit per word on a per transfer */ 1048ca632f55SGrant Likely bits = transfer->bits_per_word; 1049ca632f55SGrant Likely speed = transfer->speed_hz; 1050ca632f55SGrant Likely 1051d2c2f6a4SAndy Shevchenko clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); 1052ca632f55SGrant Likely 1053ca632f55SGrant Likely if (bits <= 8) { 1054ca632f55SGrant Likely drv_data->n_bytes = 1; 1055ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1056ca632f55SGrant Likely u8_reader : null_reader; 1057ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1058ca632f55SGrant Likely u8_writer : null_writer; 1059ca632f55SGrant Likely } else if (bits <= 16) { 1060ca632f55SGrant Likely drv_data->n_bytes = 2; 1061ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1062ca632f55SGrant Likely u16_reader : null_reader; 1063ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1064ca632f55SGrant Likely u16_writer : null_writer; 1065ca632f55SGrant Likely } else if (bits <= 32) { 1066ca632f55SGrant Likely drv_data->n_bytes = 4; 1067ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1068ca632f55SGrant Likely u32_reader : null_reader; 1069ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1070ca632f55SGrant Likely u32_writer : null_writer; 1071ca632f55SGrant Likely } 1072196b0e2cSJarkko Nikula /* 1073196b0e2cSJarkko Nikula * if bits/word is changed in dma mode, then must check the 1074196b0e2cSJarkko Nikula * thresholds and burst also 1075196b0e2cSJarkko Nikula */ 1076ca632f55SGrant Likely if (chip->enable_dma) { 1077cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, 1078cd7bed00SMika Westerberg message->spi, 1079ca632f55SGrant Likely bits, &dma_burst, 1080ca632f55SGrant Likely &dma_thresh)) 1081f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 1082f6bd03a7SJarkko Nikula "pump_transfers: DMA burst size reduced to match bits_per_word\n"); 1083ca632f55SGrant Likely } 1084ca632f55SGrant Likely 1085ca632f55SGrant Likely message->state = RUNNING_STATE; 1086ca632f55SGrant Likely 1087b6ced294SJarkko Nikula dma_mapped = master->can_dma && 1088b6ced294SJarkko Nikula master->can_dma(master, message->spi, transfer) && 1089b6ced294SJarkko Nikula master->cur_msg_mapped; 1090b6ced294SJarkko Nikula if (dma_mapped) { 1091ca632f55SGrant Likely 1092ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1093cd7bed00SMika Westerberg drv_data->transfer_handler = pxa2xx_spi_dma_transfer; 1094ca632f55SGrant Likely 10957d1f1bf6SAndy Shevchenko err = pxa2xx_spi_dma_prepare(drv_data, dma_burst); 10967d1f1bf6SAndy Shevchenko if (err) { 10977d1f1bf6SAndy Shevchenko message->status = err; 10987d1f1bf6SAndy Shevchenko giveback(drv_data); 10997d1f1bf6SAndy Shevchenko return; 11007d1f1bf6SAndy Shevchenko } 1101ca632f55SGrant Likely 1102ca632f55SGrant Likely /* Clear status and start DMA engine */ 1103ca632f55SGrant Likely cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; 1104c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); 1105cd7bed00SMika Westerberg 1106cd7bed00SMika Westerberg pxa2xx_spi_dma_start(drv_data); 1107ca632f55SGrant Likely } else { 1108ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1109ca632f55SGrant Likely drv_data->transfer_handler = interrupt_transfer; 1110ca632f55SGrant Likely 1111ca632f55SGrant Likely /* Clear status */ 1112ca632f55SGrant Likely cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; 1113ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 1114ca632f55SGrant Likely } 1115ca632f55SGrant Likely 1116ee03672dSJarkko Nikula /* NOTE: PXA25x_SSP _could_ use external clocking ... */ 1117ee03672dSJarkko Nikula cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); 1118ee03672dSJarkko Nikula if (!pxa25x_ssp_comp(drv_data)) 1119ee03672dSJarkko Nikula dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", 11202d7537d8SJarkko Nikula master->max_speed_hz 1121ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), 1122b6ced294SJarkko Nikula dma_mapped ? "DMA" : "PIO"); 1123ee03672dSJarkko Nikula else 1124ee03672dSJarkko Nikula dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", 11252d7537d8SJarkko Nikula master->max_speed_hz / 2 1126ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), 1127b6ced294SJarkko Nikula dma_mapped ? "DMA" : "PIO"); 1128ee03672dSJarkko Nikula 1129a0d2642eSMika Westerberg if (is_lpss_ssp(drv_data)) { 1130c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff) 1131c039dd27SJarkko Nikula != chip->lpss_rx_threshold) 1132c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSIRF, 1133c039dd27SJarkko Nikula chip->lpss_rx_threshold); 1134c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff) 1135c039dd27SJarkko Nikula != chip->lpss_tx_threshold) 1136c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSITF, 1137c039dd27SJarkko Nikula chip->lpss_tx_threshold); 1138a0d2642eSMika Westerberg } 1139a0d2642eSMika Westerberg 1140e5262d05SWeike Chen if (is_quark_x1000_ssp(drv_data) && 1141c039dd27SJarkko Nikula (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate)) 1142c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate); 1143e5262d05SWeike Chen 1144ca632f55SGrant Likely /* see if we need to reload the config registers */ 1145c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0) 1146c039dd27SJarkko Nikula || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask) 1147c039dd27SJarkko Nikula != (cr1 & change_mask)) { 1148ca632f55SGrant Likely /* stop the SSP, and update the other bits */ 1149c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE); 1150ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1151c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1152ca632f55SGrant Likely /* first set CR1 without interrupt and service enables */ 1153c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask); 1154ca632f55SGrant Likely /* restart the SSP */ 1155c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0); 1156ca632f55SGrant Likely 1157ca632f55SGrant Likely } else { 1158ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1159c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1160ca632f55SGrant Likely } 1161ca632f55SGrant Likely 1162ca632f55SGrant Likely cs_assert(drv_data); 1163ca632f55SGrant Likely 1164ca632f55SGrant Likely /* after chip select, release the data by enabling service 1165ca632f55SGrant Likely * requests and interrupts, without changing any mode bits */ 1166c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1); 1167ca632f55SGrant Likely } 1168ca632f55SGrant Likely 11697f86bde9SMika Westerberg static int pxa2xx_spi_transfer_one_message(struct spi_master *master, 11707f86bde9SMika Westerberg struct spi_message *msg) 1171ca632f55SGrant Likely { 11727f86bde9SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 1173ca632f55SGrant Likely 1174ca632f55SGrant Likely /* Initial message state*/ 11754fc0caacSJarkko Nikula msg->state = START_STATE; 11764fc0caacSJarkko Nikula drv_data->cur_transfer = list_entry(msg->transfers.next, 1177ca632f55SGrant Likely struct spi_transfer, 1178ca632f55SGrant Likely transfer_list); 1179ca632f55SGrant Likely 1180ca632f55SGrant Likely /* Mark as busy and launch transfers */ 1181ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 1182ca632f55SGrant Likely return 0; 1183ca632f55SGrant Likely } 1184ca632f55SGrant Likely 11857d94a505SMika Westerberg static int pxa2xx_spi_unprepare_transfer(struct spi_master *master) 11867d94a505SMika Westerberg { 11877d94a505SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 11887d94a505SMika Westerberg 11897d94a505SMika Westerberg /* Disable the SSP now */ 1190c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 1191c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 11927d94a505SMika Westerberg 11937d94a505SMika Westerberg return 0; 11947d94a505SMika Westerberg } 11957d94a505SMika Westerberg 1196ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip, 1197ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info) 1198ca632f55SGrant Likely { 119999f499cdSMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1200ca632f55SGrant Likely int err = 0; 1201ca632f55SGrant Likely 120299f499cdSMika Westerberg if (chip == NULL) 120399f499cdSMika Westerberg return 0; 120499f499cdSMika Westerberg 120599f499cdSMika Westerberg if (drv_data->cs_gpiods) { 120699f499cdSMika Westerberg struct gpio_desc *gpiod; 120799f499cdSMika Westerberg 120899f499cdSMika Westerberg gpiod = drv_data->cs_gpiods[spi->chip_select]; 120999f499cdSMika Westerberg if (gpiod) { 121099f499cdSMika Westerberg chip->gpio_cs = desc_to_gpio(gpiod); 121199f499cdSMika Westerberg chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 121299f499cdSMika Westerberg gpiod_set_value(gpiod, chip->gpio_cs_inverted); 121399f499cdSMika Westerberg } 121499f499cdSMika Westerberg 121599f499cdSMika Westerberg return 0; 121699f499cdSMika Westerberg } 121799f499cdSMika Westerberg 121899f499cdSMika Westerberg if (chip_info == NULL) 1219ca632f55SGrant Likely return 0; 1220ca632f55SGrant Likely 1221ca632f55SGrant Likely /* NOTE: setup() can be called multiple times, possibly with 1222ca632f55SGrant Likely * different chip_info, release previously requested GPIO 1223ca632f55SGrant Likely */ 1224ca632f55SGrant Likely if (gpio_is_valid(chip->gpio_cs)) 1225ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1226ca632f55SGrant Likely 1227ca632f55SGrant Likely /* If (*cs_control) is provided, ignore GPIO chip select */ 1228ca632f55SGrant Likely if (chip_info->cs_control) { 1229ca632f55SGrant Likely chip->cs_control = chip_info->cs_control; 1230ca632f55SGrant Likely return 0; 1231ca632f55SGrant Likely } 1232ca632f55SGrant Likely 1233ca632f55SGrant Likely if (gpio_is_valid(chip_info->gpio_cs)) { 1234ca632f55SGrant Likely err = gpio_request(chip_info->gpio_cs, "SPI_CS"); 1235ca632f55SGrant Likely if (err) { 1236f6bd03a7SJarkko Nikula dev_err(&spi->dev, "failed to request chip select GPIO%d\n", 1237f6bd03a7SJarkko Nikula chip_info->gpio_cs); 1238ca632f55SGrant Likely return err; 1239ca632f55SGrant Likely } 1240ca632f55SGrant Likely 1241ca632f55SGrant Likely chip->gpio_cs = chip_info->gpio_cs; 1242ca632f55SGrant Likely chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 1243ca632f55SGrant Likely 1244ca632f55SGrant Likely err = gpio_direction_output(chip->gpio_cs, 1245ca632f55SGrant Likely !chip->gpio_cs_inverted); 1246ca632f55SGrant Likely } 1247ca632f55SGrant Likely 1248ca632f55SGrant Likely return err; 1249ca632f55SGrant Likely } 1250ca632f55SGrant Likely 1251ca632f55SGrant Likely static int setup(struct spi_device *spi) 1252ca632f55SGrant Likely { 1253bffc967eSJarkko Nikula struct pxa2xx_spi_chip *chip_info; 1254ca632f55SGrant Likely struct chip_data *chip; 1255dccf7369SJarkko Nikula const struct lpss_config *config; 1256ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1257a0d2642eSMika Westerberg uint tx_thres, tx_hi_thres, rx_thres; 1258a0d2642eSMika Westerberg 1259e5262d05SWeike Chen switch (drv_data->ssp_type) { 1260e5262d05SWeike Chen case QUARK_X1000_SSP: 1261e5262d05SWeike Chen tx_thres = TX_THRESH_QUARK_X1000_DFLT; 1262e5262d05SWeike Chen tx_hi_thres = 0; 1263e5262d05SWeike Chen rx_thres = RX_THRESH_QUARK_X1000_DFLT; 1264e5262d05SWeike Chen break; 12657c7289a4SAndy Shevchenko case CE4100_SSP: 12667c7289a4SAndy Shevchenko tx_thres = TX_THRESH_CE4100_DFLT; 12677c7289a4SAndy Shevchenko tx_hi_thres = 0; 12687c7289a4SAndy Shevchenko rx_thres = RX_THRESH_CE4100_DFLT; 12697c7289a4SAndy Shevchenko break; 127003fbf488SJarkko Nikula case LPSS_LPT_SSP: 127103fbf488SJarkko Nikula case LPSS_BYT_SSP: 127230f3a6abSMika Westerberg case LPSS_BSW_SSP: 127334cadd9cSJarkko Nikula case LPSS_SPT_SSP: 1274b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 1275dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 1276dccf7369SJarkko Nikula tx_thres = config->tx_threshold_lo; 1277dccf7369SJarkko Nikula tx_hi_thres = config->tx_threshold_hi; 1278dccf7369SJarkko Nikula rx_thres = config->rx_threshold; 1279e5262d05SWeike Chen break; 1280e5262d05SWeike Chen default: 1281a0d2642eSMika Westerberg tx_thres = TX_THRESH_DFLT; 1282a0d2642eSMika Westerberg tx_hi_thres = 0; 1283a0d2642eSMika Westerberg rx_thres = RX_THRESH_DFLT; 1284e5262d05SWeike Chen break; 1285a0d2642eSMika Westerberg } 1286ca632f55SGrant Likely 1287ca632f55SGrant Likely /* Only alloc on first setup */ 1288ca632f55SGrant Likely chip = spi_get_ctldata(spi); 1289ca632f55SGrant Likely if (!chip) { 1290ca632f55SGrant Likely chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 12919deae459SJingoo Han if (!chip) 1292ca632f55SGrant Likely return -ENOMEM; 1293ca632f55SGrant Likely 1294ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 1295ca632f55SGrant Likely if (spi->chip_select > 4) { 1296f6bd03a7SJarkko Nikula dev_err(&spi->dev, 1297f6bd03a7SJarkko Nikula "failed setup: cs number must not be > 4.\n"); 1298ca632f55SGrant Likely kfree(chip); 1299ca632f55SGrant Likely return -EINVAL; 1300ca632f55SGrant Likely } 1301ca632f55SGrant Likely 1302ca632f55SGrant Likely chip->frm = spi->chip_select; 1303ca632f55SGrant Likely } else 1304ca632f55SGrant Likely chip->gpio_cs = -1; 1305c64e1265SDan O'Donovan chip->enable_dma = drv_data->master_info->enable_dma; 1306ca632f55SGrant Likely chip->timeout = TIMOUT_DFLT; 1307ca632f55SGrant Likely } 1308ca632f55SGrant Likely 1309ca632f55SGrant Likely /* protocol drivers may change the chip settings, so... 1310ca632f55SGrant Likely * if chip_info exists, use it */ 1311ca632f55SGrant Likely chip_info = spi->controller_data; 1312ca632f55SGrant Likely 1313ca632f55SGrant Likely /* chip_info isn't always needed */ 1314ca632f55SGrant Likely chip->cr1 = 0; 1315ca632f55SGrant Likely if (chip_info) { 1316ca632f55SGrant Likely if (chip_info->timeout) 1317ca632f55SGrant Likely chip->timeout = chip_info->timeout; 1318ca632f55SGrant Likely if (chip_info->tx_threshold) 1319ca632f55SGrant Likely tx_thres = chip_info->tx_threshold; 1320a0d2642eSMika Westerberg if (chip_info->tx_hi_threshold) 1321a0d2642eSMika Westerberg tx_hi_thres = chip_info->tx_hi_threshold; 1322ca632f55SGrant Likely if (chip_info->rx_threshold) 1323ca632f55SGrant Likely rx_thres = chip_info->rx_threshold; 1324ca632f55SGrant Likely chip->dma_threshold = 0; 1325ca632f55SGrant Likely if (chip_info->enable_loopback) 1326ca632f55SGrant Likely chip->cr1 = SSCR1_LBM; 1327ca632f55SGrant Likely } 1328ca632f55SGrant Likely 1329a0d2642eSMika Westerberg chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); 1330a0d2642eSMika Westerberg chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) 1331a0d2642eSMika Westerberg | SSITF_TxHiThresh(tx_hi_thres); 1332a0d2642eSMika Westerberg 1333ca632f55SGrant Likely /* set dma burst and threshold outside of chip_info path so that if 1334ca632f55SGrant Likely * chip_info goes away after setting chip->enable_dma, the 1335ca632f55SGrant Likely * burst and threshold can still respond to changes in bits_per_word */ 1336ca632f55SGrant Likely if (chip->enable_dma) { 1337ca632f55SGrant Likely /* set up legal burst and threshold for dma */ 1338cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, 1339cd7bed00SMika Westerberg spi->bits_per_word, 1340ca632f55SGrant Likely &chip->dma_burst_size, 1341ca632f55SGrant Likely &chip->dma_threshold)) { 1342f6bd03a7SJarkko Nikula dev_warn(&spi->dev, 1343f6bd03a7SJarkko Nikula "in setup: DMA burst size reduced to match bits_per_word\n"); 1344ca632f55SGrant Likely } 1345ca632f55SGrant Likely } 1346ca632f55SGrant Likely 1347e5262d05SWeike Chen switch (drv_data->ssp_type) { 1348e5262d05SWeike Chen case QUARK_X1000_SSP: 1349e5262d05SWeike Chen chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) 1350e5262d05SWeike Chen & QUARK_X1000_SSCR1_RFT) 1351e5262d05SWeike Chen | (QUARK_X1000_SSCR1_TxTresh(tx_thres) 1352e5262d05SWeike Chen & QUARK_X1000_SSCR1_TFT); 1353e5262d05SWeike Chen break; 13547c7289a4SAndy Shevchenko case CE4100_SSP: 13557c7289a4SAndy Shevchenko chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) | 13567c7289a4SAndy Shevchenko (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT); 13577c7289a4SAndy Shevchenko break; 1358e5262d05SWeike Chen default: 1359e5262d05SWeike Chen chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | 1360e5262d05SWeike Chen (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); 1361e5262d05SWeike Chen break; 1362e5262d05SWeike Chen } 1363e5262d05SWeike Chen 1364ca632f55SGrant Likely chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); 1365ca632f55SGrant Likely chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) 1366ca632f55SGrant Likely | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); 1367ca632f55SGrant Likely 1368b833172fSMika Westerberg if (spi->mode & SPI_LOOP) 1369b833172fSMika Westerberg chip->cr1 |= SSCR1_LBM; 1370b833172fSMika Westerberg 1371ca632f55SGrant Likely if (spi->bits_per_word <= 8) { 1372ca632f55SGrant Likely chip->n_bytes = 1; 1373ca632f55SGrant Likely chip->read = u8_reader; 1374ca632f55SGrant Likely chip->write = u8_writer; 1375ca632f55SGrant Likely } else if (spi->bits_per_word <= 16) { 1376ca632f55SGrant Likely chip->n_bytes = 2; 1377ca632f55SGrant Likely chip->read = u16_reader; 1378ca632f55SGrant Likely chip->write = u16_writer; 1379ca632f55SGrant Likely } else if (spi->bits_per_word <= 32) { 1380ca632f55SGrant Likely chip->n_bytes = 4; 1381ca632f55SGrant Likely chip->read = u32_reader; 1382ca632f55SGrant Likely chip->write = u32_writer; 1383ca632f55SGrant Likely } 1384ca632f55SGrant Likely 1385ca632f55SGrant Likely spi_set_ctldata(spi, chip); 1386ca632f55SGrant Likely 1387ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 1388ca632f55SGrant Likely return 0; 1389ca632f55SGrant Likely 1390ca632f55SGrant Likely return setup_cs(spi, chip, chip_info); 1391ca632f55SGrant Likely } 1392ca632f55SGrant Likely 1393ca632f55SGrant Likely static void cleanup(struct spi_device *spi) 1394ca632f55SGrant Likely { 1395ca632f55SGrant Likely struct chip_data *chip = spi_get_ctldata(spi); 1396ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1397ca632f55SGrant Likely 1398ca632f55SGrant Likely if (!chip) 1399ca632f55SGrant Likely return; 1400ca632f55SGrant Likely 140199f499cdSMika Westerberg if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods && 140299f499cdSMika Westerberg gpio_is_valid(chip->gpio_cs)) 1403ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1404ca632f55SGrant Likely 1405ca632f55SGrant Likely kfree(chip); 1406ca632f55SGrant Likely } 1407ca632f55SGrant Likely 14080db64215SJarkko Nikula #ifdef CONFIG_PCI 1409a3496855SMika Westerberg #ifdef CONFIG_ACPI 141003fbf488SJarkko Nikula 14118422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { 141203fbf488SJarkko Nikula { "INT33C0", LPSS_LPT_SSP }, 141303fbf488SJarkko Nikula { "INT33C1", LPSS_LPT_SSP }, 141403fbf488SJarkko Nikula { "INT3430", LPSS_LPT_SSP }, 141503fbf488SJarkko Nikula { "INT3431", LPSS_LPT_SSP }, 141603fbf488SJarkko Nikula { "80860F0E", LPSS_BYT_SSP }, 141730f3a6abSMika Westerberg { "8086228E", LPSS_BSW_SSP }, 141803fbf488SJarkko Nikula { }, 141903fbf488SJarkko Nikula }; 142003fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); 142103fbf488SJarkko Nikula 14220db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev) 14230db64215SJarkko Nikula { 14240db64215SJarkko Nikula unsigned int devid; 14250db64215SJarkko Nikula int port_id = -1; 14260db64215SJarkko Nikula 14270db64215SJarkko Nikula if (adev && adev->pnp.unique_id && 14280db64215SJarkko Nikula !kstrtouint(adev->pnp.unique_id, 0, &devid)) 14290db64215SJarkko Nikula port_id = devid; 14300db64215SJarkko Nikula return port_id; 14310db64215SJarkko Nikula } 14320db64215SJarkko Nikula #else /* !CONFIG_ACPI */ 14330db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev) 14340db64215SJarkko Nikula { 14350db64215SJarkko Nikula return -1; 14360db64215SJarkko Nikula } 14370db64215SJarkko Nikula #endif 14380db64215SJarkko Nikula 143934cadd9cSJarkko Nikula /* 144034cadd9cSJarkko Nikula * PCI IDs of compound devices that integrate both host controller and private 144134cadd9cSJarkko Nikula * integrated DMA engine. Please note these are not used in module 144234cadd9cSJarkko Nikula * autoloading and probing in this module but matching the LPSS SSP type. 144334cadd9cSJarkko Nikula */ 144434cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { 144534cadd9cSJarkko Nikula /* SPT-LP */ 144634cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, 144734cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, 144834cadd9cSJarkko Nikula /* SPT-H */ 144934cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, 145034cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, 1451704d2b07SMika Westerberg /* KBL-H */ 1452704d2b07SMika Westerberg { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP }, 1453704d2b07SMika Westerberg { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP }, 1454c1b03f11SJarkko Nikula /* BXT A-Step */ 1455b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP }, 1456b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP }, 1457b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP }, 1458c1b03f11SJarkko Nikula /* BXT B-Step */ 1459c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP }, 1460c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP }, 1461c1b03f11SJarkko Nikula { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP }, 1462b7c08cf8SJarkko Nikula /* APL */ 1463b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP }, 1464b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP }, 1465b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP }, 146694e5c23dSAxel Lin { }, 146734cadd9cSJarkko Nikula }; 146834cadd9cSJarkko Nikula 146934cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) 147034cadd9cSJarkko Nikula { 147134cadd9cSJarkko Nikula struct device *dev = param; 147234cadd9cSJarkko Nikula 147334cadd9cSJarkko Nikula if (dev != chan->device->dev->parent) 147434cadd9cSJarkko Nikula return false; 147534cadd9cSJarkko Nikula 147634cadd9cSJarkko Nikula return true; 147734cadd9cSJarkko Nikula } 147834cadd9cSJarkko Nikula 1479a3496855SMika Westerberg static struct pxa2xx_spi_master * 14800db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev) 1481a3496855SMika Westerberg { 1482a3496855SMika Westerberg struct pxa2xx_spi_master *pdata; 1483a3496855SMika Westerberg struct acpi_device *adev; 1484a3496855SMika Westerberg struct ssp_device *ssp; 1485a3496855SMika Westerberg struct resource *res; 148634cadd9cSJarkko Nikula const struct acpi_device_id *adev_id = NULL; 148734cadd9cSJarkko Nikula const struct pci_device_id *pcidev_id = NULL; 14883b8b6d05SJarkko Nikula int type; 1489a3496855SMika Westerberg 1490b9f6940aSJarkko Nikula adev = ACPI_COMPANION(&pdev->dev); 1491a3496855SMika Westerberg 149234cadd9cSJarkko Nikula if (dev_is_pci(pdev->dev.parent)) 149334cadd9cSJarkko Nikula pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, 149434cadd9cSJarkko Nikula to_pci_dev(pdev->dev.parent)); 14950db64215SJarkko Nikula else if (adev) 149634cadd9cSJarkko Nikula adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table, 149734cadd9cSJarkko Nikula &pdev->dev); 14980db64215SJarkko Nikula else 14990db64215SJarkko Nikula return NULL; 150034cadd9cSJarkko Nikula 150134cadd9cSJarkko Nikula if (adev_id) 150234cadd9cSJarkko Nikula type = (int)adev_id->driver_data; 150334cadd9cSJarkko Nikula else if (pcidev_id) 150434cadd9cSJarkko Nikula type = (int)pcidev_id->driver_data; 150503fbf488SJarkko Nikula else 150603fbf488SJarkko Nikula return NULL; 150703fbf488SJarkko Nikula 1508cc0ee987SMika Westerberg pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 15099deae459SJingoo Han if (!pdata) 1510a3496855SMika Westerberg return NULL; 1511a3496855SMika Westerberg 1512a3496855SMika Westerberg res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1513a3496855SMika Westerberg if (!res) 1514a3496855SMika Westerberg return NULL; 1515a3496855SMika Westerberg 1516a3496855SMika Westerberg ssp = &pdata->ssp; 1517a3496855SMika Westerberg 1518a3496855SMika Westerberg ssp->phys_base = res->start; 1519cbfd6a21SSachin Kamat ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); 1520cbfd6a21SSachin Kamat if (IS_ERR(ssp->mmio_base)) 15216dc81f6fSMika Westerberg return NULL; 1522a3496855SMika Westerberg 152334cadd9cSJarkko Nikula if (pcidev_id) { 152434cadd9cSJarkko Nikula pdata->tx_param = pdev->dev.parent; 152534cadd9cSJarkko Nikula pdata->rx_param = pdev->dev.parent; 152634cadd9cSJarkko Nikula pdata->dma_filter = pxa2xx_spi_idma_filter; 152734cadd9cSJarkko Nikula } 152834cadd9cSJarkko Nikula 1529a3496855SMika Westerberg ssp->clk = devm_clk_get(&pdev->dev, NULL); 1530a3496855SMika Westerberg ssp->irq = platform_get_irq(pdev, 0); 153103fbf488SJarkko Nikula ssp->type = type; 1532a3496855SMika Westerberg ssp->pdev = pdev; 15330db64215SJarkko Nikula ssp->port_id = pxa2xx_spi_get_port_id(adev); 1534a3496855SMika Westerberg 1535a3496855SMika Westerberg pdata->num_chipselect = 1; 1536cddb339bSMika Westerberg pdata->enable_dma = true; 1537a3496855SMika Westerberg 1538a3496855SMika Westerberg return pdata; 1539a3496855SMika Westerberg } 1540a3496855SMika Westerberg 15410db64215SJarkko Nikula #else /* !CONFIG_PCI */ 1542a3496855SMika Westerberg static inline struct pxa2xx_spi_master * 15430db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev) 1544a3496855SMika Westerberg { 1545a3496855SMika Westerberg return NULL; 1546a3496855SMika Westerberg } 1547a3496855SMika Westerberg #endif 1548a3496855SMika Westerberg 15490c27d9cfSMika Westerberg static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs) 15500c27d9cfSMika Westerberg { 15510c27d9cfSMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 15520c27d9cfSMika Westerberg 15530c27d9cfSMika Westerberg if (has_acpi_companion(&drv_data->pdev->dev)) { 15540c27d9cfSMika Westerberg switch (drv_data->ssp_type) { 15550c27d9cfSMika Westerberg /* 15560c27d9cfSMika Westerberg * For Atoms the ACPI DeviceSelection used by the Windows 15570c27d9cfSMika Westerberg * driver starts from 1 instead of 0 so translate it here 15580c27d9cfSMika Westerberg * to match what Linux expects. 15590c27d9cfSMika Westerberg */ 15600c27d9cfSMika Westerberg case LPSS_BYT_SSP: 156130f3a6abSMika Westerberg case LPSS_BSW_SSP: 15620c27d9cfSMika Westerberg return cs - 1; 15630c27d9cfSMika Westerberg 15640c27d9cfSMika Westerberg default: 15650c27d9cfSMika Westerberg break; 15660c27d9cfSMika Westerberg } 15670c27d9cfSMika Westerberg } 15680c27d9cfSMika Westerberg 15690c27d9cfSMika Westerberg return cs; 15700c27d9cfSMika Westerberg } 15710c27d9cfSMika Westerberg 1572fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev) 1573ca632f55SGrant Likely { 1574ca632f55SGrant Likely struct device *dev = &pdev->dev; 1575ca632f55SGrant Likely struct pxa2xx_spi_master *platform_info; 1576ca632f55SGrant Likely struct spi_master *master; 1577ca632f55SGrant Likely struct driver_data *drv_data; 1578ca632f55SGrant Likely struct ssp_device *ssp; 15798b136baaSJarkko Nikula const struct lpss_config *config; 158099f499cdSMika Westerberg int status, count; 1581c039dd27SJarkko Nikula u32 tmp; 1582ca632f55SGrant Likely 1583851bacf5SMika Westerberg platform_info = dev_get_platdata(dev); 1584851bacf5SMika Westerberg if (!platform_info) { 15850db64215SJarkko Nikula platform_info = pxa2xx_spi_init_pdata(pdev); 1586a3496855SMika Westerberg if (!platform_info) { 1587851bacf5SMika Westerberg dev_err(&pdev->dev, "missing platform data\n"); 1588851bacf5SMika Westerberg return -ENODEV; 1589851bacf5SMika Westerberg } 1590a3496855SMika Westerberg } 1591ca632f55SGrant Likely 1592ca632f55SGrant Likely ssp = pxa_ssp_request(pdev->id, pdev->name); 1593851bacf5SMika Westerberg if (!ssp) 1594851bacf5SMika Westerberg ssp = &platform_info->ssp; 1595851bacf5SMika Westerberg 1596851bacf5SMika Westerberg if (!ssp->mmio_base) { 1597851bacf5SMika Westerberg dev_err(&pdev->dev, "failed to get ssp\n"); 1598ca632f55SGrant Likely return -ENODEV; 1599ca632f55SGrant Likely } 1600ca632f55SGrant Likely 1601757fe8d5SJarkko Nikula master = spi_alloc_master(dev, sizeof(struct driver_data)); 1602ca632f55SGrant Likely if (!master) { 1603ca632f55SGrant Likely dev_err(&pdev->dev, "cannot alloc spi_master\n"); 1604ca632f55SGrant Likely pxa_ssp_free(ssp); 1605ca632f55SGrant Likely return -ENOMEM; 1606ca632f55SGrant Likely } 1607ca632f55SGrant Likely drv_data = spi_master_get_devdata(master); 1608ca632f55SGrant Likely drv_data->master = master; 1609ca632f55SGrant Likely drv_data->master_info = platform_info; 1610ca632f55SGrant Likely drv_data->pdev = pdev; 1611ca632f55SGrant Likely drv_data->ssp = ssp; 1612ca632f55SGrant Likely 1613ca632f55SGrant Likely master->dev.of_node = pdev->dev.of_node; 1614ca632f55SGrant Likely /* the spi->mode bits understood by this driver: */ 1615b833172fSMika Westerberg master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; 1616ca632f55SGrant Likely 1617851bacf5SMika Westerberg master->bus_num = ssp->port_id; 1618ca632f55SGrant Likely master->dma_alignment = DMA_ALIGNMENT; 1619ca632f55SGrant Likely master->cleanup = cleanup; 1620ca632f55SGrant Likely master->setup = setup; 16217f86bde9SMika Westerberg master->transfer_one_message = pxa2xx_spi_transfer_one_message; 16227d94a505SMika Westerberg master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; 16230c27d9cfSMika Westerberg master->fw_translate_cs = pxa2xx_spi_fw_translate_cs; 16247dd62787SMark Brown master->auto_runtime_pm = true; 16258c3ad488SJarkko Nikula master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX; 1626ca632f55SGrant Likely 1627ca632f55SGrant Likely drv_data->ssp_type = ssp->type; 1628ca632f55SGrant Likely 1629ca632f55SGrant Likely drv_data->ioaddr = ssp->mmio_base; 1630ca632f55SGrant Likely drv_data->ssdr_physical = ssp->phys_base + SSDR; 1631ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 1632e5262d05SWeike Chen switch (drv_data->ssp_type) { 1633e5262d05SWeike Chen case QUARK_X1000_SSP: 1634e5262d05SWeike Chen master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1635e5262d05SWeike Chen break; 1636e5262d05SWeike Chen default: 163724778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); 1638e5262d05SWeike Chen break; 1639e5262d05SWeike Chen } 1640e5262d05SWeike Chen 1641ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; 1642ca632f55SGrant Likely drv_data->dma_cr1 = 0; 1643ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR; 1644ca632f55SGrant Likely drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; 1645ca632f55SGrant Likely } else { 164624778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1647ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; 16485928808eSMika Westerberg drv_data->dma_cr1 = DEFAULT_DMA_CR1; 1649ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR | SSSR_TINT; 1650ca632f55SGrant Likely drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; 1651ca632f55SGrant Likely } 1652ca632f55SGrant Likely 1653ca632f55SGrant Likely status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), 1654ca632f55SGrant Likely drv_data); 1655ca632f55SGrant Likely if (status < 0) { 1656ca632f55SGrant Likely dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); 1657ca632f55SGrant Likely goto out_error_master_alloc; 1658ca632f55SGrant Likely } 1659ca632f55SGrant Likely 1660ca632f55SGrant Likely /* Setup DMA if requested */ 1661ca632f55SGrant Likely if (platform_info->enable_dma) { 1662cd7bed00SMika Westerberg status = pxa2xx_spi_dma_setup(drv_data); 1663cd7bed00SMika Westerberg if (status) { 1664cddb339bSMika Westerberg dev_dbg(dev, "no DMA channels available, using PIO\n"); 1665cd7bed00SMika Westerberg platform_info->enable_dma = false; 1666b6ced294SJarkko Nikula } else { 1667b6ced294SJarkko Nikula master->can_dma = pxa2xx_spi_can_dma; 1668ca632f55SGrant Likely } 1669ca632f55SGrant Likely } 1670ca632f55SGrant Likely 1671ca632f55SGrant Likely /* Enable SOC clock */ 16723343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 16733343b7a6SMika Westerberg 16740eca7cf2SJarkko Nikula master->max_speed_hz = clk_get_rate(ssp->clk); 1675ca632f55SGrant Likely 1676ca632f55SGrant Likely /* Load default SSP configuration */ 1677c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 1678e5262d05SWeike Chen switch (drv_data->ssp_type) { 1679e5262d05SWeike Chen case QUARK_X1000_SSP: 16807c7289a4SAndy Shevchenko tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) | 16817c7289a4SAndy Shevchenko QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); 1682c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1683e5262d05SWeike Chen 1684e5262d05SWeike Chen /* using the Motorola SPI protocol and use 8 bit frame */ 16857c7289a4SAndy Shevchenko tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8); 16867c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR0, tmp); 1687e5262d05SWeike Chen break; 16887c7289a4SAndy Shevchenko case CE4100_SSP: 16897c7289a4SAndy Shevchenko tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) | 16907c7289a4SAndy Shevchenko CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT); 16917c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR1, tmp); 16927c7289a4SAndy Shevchenko tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); 16937c7289a4SAndy Shevchenko pxa2xx_spi_write(drv_data, SSCR0, tmp); 1694a2dd8af0SAndy Shevchenko break; 1695e5262d05SWeike Chen default: 1696c039dd27SJarkko Nikula tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | 1697c039dd27SJarkko Nikula SSCR1_TxTresh(TX_THRESH_DFLT); 1698c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1699c039dd27SJarkko Nikula tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); 1700c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, tmp); 1701e5262d05SWeike Chen break; 1702e5262d05SWeike Chen } 1703e5262d05SWeike Chen 1704ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1705c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 1706e5262d05SWeike Chen 1707e5262d05SWeike Chen if (!is_quark_x1000_ssp(drv_data)) 1708c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSPSP, 0); 1709ca632f55SGrant Likely 17108b136baaSJarkko Nikula if (is_lpss_ssp(drv_data)) { 17118b136baaSJarkko Nikula lpss_ssp_setup(drv_data); 17128b136baaSJarkko Nikula config = lpss_get_config(drv_data); 17138b136baaSJarkko Nikula if (config->reg_capabilities >= 0) { 17148b136baaSJarkko Nikula tmp = __lpss_ssp_read_priv(drv_data, 17158b136baaSJarkko Nikula config->reg_capabilities); 17168b136baaSJarkko Nikula tmp &= LPSS_CAPS_CS_EN_MASK; 17178b136baaSJarkko Nikula tmp >>= LPSS_CAPS_CS_EN_SHIFT; 17188b136baaSJarkko Nikula platform_info->num_chipselect = ffz(tmp); 171930f3a6abSMika Westerberg } else if (config->cs_num) { 172030f3a6abSMika Westerberg platform_info->num_chipselect = config->cs_num; 17218b136baaSJarkko Nikula } 17228b136baaSJarkko Nikula } 17238b136baaSJarkko Nikula master->num_chipselect = platform_info->num_chipselect; 17248b136baaSJarkko Nikula 172599f499cdSMika Westerberg count = gpiod_count(&pdev->dev, "cs"); 172699f499cdSMika Westerberg if (count > 0) { 172799f499cdSMika Westerberg int i; 172899f499cdSMika Westerberg 172999f499cdSMika Westerberg master->num_chipselect = max_t(int, count, 173099f499cdSMika Westerberg master->num_chipselect); 173199f499cdSMika Westerberg 173299f499cdSMika Westerberg drv_data->cs_gpiods = devm_kcalloc(&pdev->dev, 173399f499cdSMika Westerberg master->num_chipselect, sizeof(struct gpio_desc *), 173499f499cdSMika Westerberg GFP_KERNEL); 173599f499cdSMika Westerberg if (!drv_data->cs_gpiods) { 173699f499cdSMika Westerberg status = -ENOMEM; 173799f499cdSMika Westerberg goto out_error_clock_enabled; 173899f499cdSMika Westerberg } 173999f499cdSMika Westerberg 174099f499cdSMika Westerberg for (i = 0; i < master->num_chipselect; i++) { 174199f499cdSMika Westerberg struct gpio_desc *gpiod; 174299f499cdSMika Westerberg 174399f499cdSMika Westerberg gpiod = devm_gpiod_get_index(dev, "cs", i, 174499f499cdSMika Westerberg GPIOD_OUT_HIGH); 174599f499cdSMika Westerberg if (IS_ERR(gpiod)) { 174699f499cdSMika Westerberg /* Means use native chip select */ 174799f499cdSMika Westerberg if (PTR_ERR(gpiod) == -ENOENT) 174899f499cdSMika Westerberg continue; 174999f499cdSMika Westerberg 175099f499cdSMika Westerberg status = (int)PTR_ERR(gpiod); 175199f499cdSMika Westerberg goto out_error_clock_enabled; 175299f499cdSMika Westerberg } else { 175399f499cdSMika Westerberg drv_data->cs_gpiods[i] = gpiod; 175499f499cdSMika Westerberg } 175599f499cdSMika Westerberg } 175699f499cdSMika Westerberg } 175799f499cdSMika Westerberg 17587f86bde9SMika Westerberg tasklet_init(&drv_data->pump_transfers, pump_transfers, 17597f86bde9SMika Westerberg (unsigned long)drv_data); 1760ca632f55SGrant Likely 1761836d1a22SAntonio Ospite pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 1762836d1a22SAntonio Ospite pm_runtime_use_autosuspend(&pdev->dev); 1763836d1a22SAntonio Ospite pm_runtime_set_active(&pdev->dev); 1764836d1a22SAntonio Ospite pm_runtime_enable(&pdev->dev); 1765836d1a22SAntonio Ospite 1766ca632f55SGrant Likely /* Register with the SPI framework */ 1767ca632f55SGrant Likely platform_set_drvdata(pdev, drv_data); 1768a807fcd0SJingoo Han status = devm_spi_register_master(&pdev->dev, master); 1769ca632f55SGrant Likely if (status != 0) { 1770ca632f55SGrant Likely dev_err(&pdev->dev, "problem registering spi master\n"); 17717f86bde9SMika Westerberg goto out_error_clock_enabled; 1772ca632f55SGrant Likely } 1773ca632f55SGrant Likely 1774ca632f55SGrant Likely return status; 1775ca632f55SGrant Likely 1776ca632f55SGrant Likely out_error_clock_enabled: 17773343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1778cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1779ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1780ca632f55SGrant Likely 1781ca632f55SGrant Likely out_error_master_alloc: 1782ca632f55SGrant Likely spi_master_put(master); 1783ca632f55SGrant Likely pxa_ssp_free(ssp); 1784ca632f55SGrant Likely return status; 1785ca632f55SGrant Likely } 1786ca632f55SGrant Likely 1787ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev) 1788ca632f55SGrant Likely { 1789ca632f55SGrant Likely struct driver_data *drv_data = platform_get_drvdata(pdev); 1790ca632f55SGrant Likely struct ssp_device *ssp; 1791ca632f55SGrant Likely 1792ca632f55SGrant Likely if (!drv_data) 1793ca632f55SGrant Likely return 0; 1794ca632f55SGrant Likely ssp = drv_data->ssp; 1795ca632f55SGrant Likely 17967d94a505SMika Westerberg pm_runtime_get_sync(&pdev->dev); 17977d94a505SMika Westerberg 1798ca632f55SGrant Likely /* Disable the SSP at the peripheral and SOC level */ 1799c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 18003343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1801ca632f55SGrant Likely 1802ca632f55SGrant Likely /* Release DMA */ 1803cd7bed00SMika Westerberg if (drv_data->master_info->enable_dma) 1804cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1805ca632f55SGrant Likely 18067d94a505SMika Westerberg pm_runtime_put_noidle(&pdev->dev); 18077d94a505SMika Westerberg pm_runtime_disable(&pdev->dev); 18087d94a505SMika Westerberg 1809ca632f55SGrant Likely /* Release IRQ */ 1810ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1811ca632f55SGrant Likely 1812ca632f55SGrant Likely /* Release SSP */ 1813ca632f55SGrant Likely pxa_ssp_free(ssp); 1814ca632f55SGrant Likely 1815ca632f55SGrant Likely return 0; 1816ca632f55SGrant Likely } 1817ca632f55SGrant Likely 1818ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev) 1819ca632f55SGrant Likely { 1820ca632f55SGrant Likely int status = 0; 1821ca632f55SGrant Likely 1822ca632f55SGrant Likely if ((status = pxa2xx_spi_remove(pdev)) != 0) 1823ca632f55SGrant Likely dev_err(&pdev->dev, "shutdown failed with %d\n", status); 1824ca632f55SGrant Likely } 1825ca632f55SGrant Likely 1826382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP 1827ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev) 1828ca632f55SGrant Likely { 1829ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1830ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1831bffc967eSJarkko Nikula int status; 1832ca632f55SGrant Likely 18337f86bde9SMika Westerberg status = spi_master_suspend(drv_data->master); 1834ca632f55SGrant Likely if (status != 0) 1835ca632f55SGrant Likely return status; 1836c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 18372b9375b9SDmitry Eremin-Solenikov 18382b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 18393343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1840ca632f55SGrant Likely 1841ca632f55SGrant Likely return 0; 1842ca632f55SGrant Likely } 1843ca632f55SGrant Likely 1844ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev) 1845ca632f55SGrant Likely { 1846ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1847ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1848bffc967eSJarkko Nikula int status; 1849ca632f55SGrant Likely 1850ca632f55SGrant Likely /* Enable the SSP clock */ 18512b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 18523343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 1853ca632f55SGrant Likely 1854c50325f7SChew, Chiau Ee /* Restore LPSS private register bits */ 185548421adfSJarkko Nikula if (is_lpss_ssp(drv_data)) 1856c50325f7SChew, Chiau Ee lpss_ssp_setup(drv_data); 1857c50325f7SChew, Chiau Ee 1858ca632f55SGrant Likely /* Start the queue running */ 18597f86bde9SMika Westerberg status = spi_master_resume(drv_data->master); 1860ca632f55SGrant Likely if (status != 0) { 1861ca632f55SGrant Likely dev_err(dev, "problem starting queue (%d)\n", status); 1862ca632f55SGrant Likely return status; 1863ca632f55SGrant Likely } 1864ca632f55SGrant Likely 1865ca632f55SGrant Likely return 0; 1866ca632f55SGrant Likely } 18677d94a505SMika Westerberg #endif 18687d94a505SMika Westerberg 1869ec833050SRafael J. Wysocki #ifdef CONFIG_PM 18707d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev) 18717d94a505SMika Westerberg { 18727d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 18737d94a505SMika Westerberg 18747d94a505SMika Westerberg clk_disable_unprepare(drv_data->ssp->clk); 18757d94a505SMika Westerberg return 0; 18767d94a505SMika Westerberg } 18777d94a505SMika Westerberg 18787d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev) 18797d94a505SMika Westerberg { 18807d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 18817d94a505SMika Westerberg 18827d94a505SMika Westerberg clk_prepare_enable(drv_data->ssp->clk); 18837d94a505SMika Westerberg return 0; 18847d94a505SMika Westerberg } 18857d94a505SMika Westerberg #endif 1886ca632f55SGrant Likely 1887ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = { 18887d94a505SMika Westerberg SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) 18897d94a505SMika Westerberg SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, 18907d94a505SMika Westerberg pxa2xx_spi_runtime_resume, NULL) 1891ca632f55SGrant Likely }; 1892ca632f55SGrant Likely 1893ca632f55SGrant Likely static struct platform_driver driver = { 1894ca632f55SGrant Likely .driver = { 1895ca632f55SGrant Likely .name = "pxa2xx-spi", 1896ca632f55SGrant Likely .pm = &pxa2xx_spi_pm_ops, 1897a3496855SMika Westerberg .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), 1898ca632f55SGrant Likely }, 1899ca632f55SGrant Likely .probe = pxa2xx_spi_probe, 1900ca632f55SGrant Likely .remove = pxa2xx_spi_remove, 1901ca632f55SGrant Likely .shutdown = pxa2xx_spi_shutdown, 1902ca632f55SGrant Likely }; 1903ca632f55SGrant Likely 1904ca632f55SGrant Likely static int __init pxa2xx_spi_init(void) 1905ca632f55SGrant Likely { 1906ca632f55SGrant Likely return platform_driver_register(&driver); 1907ca632f55SGrant Likely } 1908ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init); 1909ca632f55SGrant Likely 1910ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void) 1911ca632f55SGrant Likely { 1912ca632f55SGrant Likely platform_driver_unregister(&driver); 1913ca632f55SGrant Likely } 1914ca632f55SGrant Likely module_exit(pxa2xx_spi_exit); 1915