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> 31ca632f55SGrant Likely #include <linux/slab.h> 323343b7a6SMika Westerberg #include <linux/clk.h> 337d94a505SMika Westerberg #include <linux/pm_runtime.h> 34a3496855SMika Westerberg #include <linux/acpi.h> 35ca632f55SGrant Likely 36cd7bed00SMika Westerberg #include "spi-pxa2xx.h" 37ca632f55SGrant Likely 38ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street"); 39ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); 40ca632f55SGrant Likely MODULE_LICENSE("GPL"); 41ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi"); 42ca632f55SGrant Likely 43ca632f55SGrant Likely #define TIMOUT_DFLT 1000 44ca632f55SGrant Likely 45ca632f55SGrant Likely /* 46ca632f55SGrant Likely * for testing SSCR1 changes that require SSP restart, basically 47ca632f55SGrant Likely * everything except the service and interrupt enables, the pxa270 developer 48ca632f55SGrant Likely * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this 49ca632f55SGrant Likely * list, but the PXA255 dev man says all bits without really meaning the 50ca632f55SGrant Likely * service and interrupt enables 51ca632f55SGrant Likely */ 52ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 53ca632f55SGrant Likely | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 54ca632f55SGrant Likely | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 55ca632f55SGrant Likely | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 56ca632f55SGrant Likely | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ 57ca632f55SGrant Likely | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 58ca632f55SGrant Likely 59e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ 60e5262d05SWeike Chen | QUARK_X1000_SSCR1_EFWR \ 61e5262d05SWeike Chen | QUARK_X1000_SSCR1_RFT \ 62e5262d05SWeike Chen | QUARK_X1000_SSCR1_TFT \ 63e5262d05SWeike Chen | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 64e5262d05SWeike Chen 65624ea72eSJarkko Nikula #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) 66624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_SW_MODE BIT(0) 67624ea72eSJarkko Nikula #define LPSS_CS_CONTROL_CS_HIGH BIT(1) 688b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_SHIFT 9 698b136baaSJarkko Nikula #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT) 70a0d2642eSMika Westerberg 71dccf7369SJarkko Nikula struct lpss_config { 72dccf7369SJarkko Nikula /* LPSS offset from drv_data->ioaddr */ 73dccf7369SJarkko Nikula unsigned offset; 74dccf7369SJarkko Nikula /* Register offsets from drv_data->lpss_base or -1 */ 75dccf7369SJarkko Nikula int reg_general; 76dccf7369SJarkko Nikula int reg_ssp; 77dccf7369SJarkko Nikula int reg_cs_ctrl; 788b136baaSJarkko Nikula int reg_capabilities; 79dccf7369SJarkko Nikula /* FIFO thresholds */ 80dccf7369SJarkko Nikula u32 rx_threshold; 81dccf7369SJarkko Nikula u32 tx_threshold_lo; 82dccf7369SJarkko Nikula u32 tx_threshold_hi; 83*c1e4a53cSMika Westerberg /* Chip select control */ 84*c1e4a53cSMika Westerberg unsigned cs_sel_shift; 85*c1e4a53cSMika Westerberg unsigned cs_sel_mask; 86dccf7369SJarkko Nikula }; 87dccf7369SJarkko Nikula 88dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */ 89dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = { 90dccf7369SJarkko Nikula { /* LPSS_LPT_SSP */ 91dccf7369SJarkko Nikula .offset = 0x800, 92dccf7369SJarkko Nikula .reg_general = 0x08, 93dccf7369SJarkko Nikula .reg_ssp = 0x0c, 94dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 958b136baaSJarkko Nikula .reg_capabilities = -1, 96dccf7369SJarkko Nikula .rx_threshold = 64, 97dccf7369SJarkko Nikula .tx_threshold_lo = 160, 98dccf7369SJarkko Nikula .tx_threshold_hi = 224, 99dccf7369SJarkko Nikula }, 100dccf7369SJarkko Nikula { /* LPSS_BYT_SSP */ 101dccf7369SJarkko Nikula .offset = 0x400, 102dccf7369SJarkko Nikula .reg_general = 0x08, 103dccf7369SJarkko Nikula .reg_ssp = 0x0c, 104dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 1058b136baaSJarkko Nikula .reg_capabilities = -1, 106dccf7369SJarkko Nikula .rx_threshold = 64, 107dccf7369SJarkko Nikula .tx_threshold_lo = 160, 108dccf7369SJarkko Nikula .tx_threshold_hi = 224, 109dccf7369SJarkko Nikula }, 11034cadd9cSJarkko Nikula { /* LPSS_SPT_SSP */ 11134cadd9cSJarkko Nikula .offset = 0x200, 11234cadd9cSJarkko Nikula .reg_general = -1, 11334cadd9cSJarkko Nikula .reg_ssp = 0x20, 11434cadd9cSJarkko Nikula .reg_cs_ctrl = 0x24, 1158b136baaSJarkko Nikula .reg_capabilities = 0xfc, 11634cadd9cSJarkko Nikula .rx_threshold = 1, 11734cadd9cSJarkko Nikula .tx_threshold_lo = 32, 11834cadd9cSJarkko Nikula .tx_threshold_hi = 56, 11934cadd9cSJarkko Nikula }, 120b7c08cf8SJarkko Nikula { /* LPSS_BXT_SSP */ 121b7c08cf8SJarkko Nikula .offset = 0x200, 122b7c08cf8SJarkko Nikula .reg_general = -1, 123b7c08cf8SJarkko Nikula .reg_ssp = 0x20, 124b7c08cf8SJarkko Nikula .reg_cs_ctrl = 0x24, 125b7c08cf8SJarkko Nikula .reg_capabilities = 0xfc, 126b7c08cf8SJarkko Nikula .rx_threshold = 1, 127b7c08cf8SJarkko Nikula .tx_threshold_lo = 16, 128b7c08cf8SJarkko Nikula .tx_threshold_hi = 48, 129*c1e4a53cSMika Westerberg .cs_sel_shift = 8, 130*c1e4a53cSMika Westerberg .cs_sel_mask = 3 << 8, 131b7c08cf8SJarkko Nikula }, 132dccf7369SJarkko Nikula }; 133dccf7369SJarkko Nikula 134dccf7369SJarkko Nikula static inline const struct lpss_config 135dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data) 136dccf7369SJarkko Nikula { 137dccf7369SJarkko Nikula return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; 138dccf7369SJarkko Nikula } 139dccf7369SJarkko Nikula 140a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data) 141a0d2642eSMika Westerberg { 14203fbf488SJarkko Nikula switch (drv_data->ssp_type) { 14303fbf488SJarkko Nikula case LPSS_LPT_SSP: 14403fbf488SJarkko Nikula case LPSS_BYT_SSP: 14534cadd9cSJarkko Nikula case LPSS_SPT_SSP: 146b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 14703fbf488SJarkko Nikula return true; 14803fbf488SJarkko Nikula default: 14903fbf488SJarkko Nikula return false; 15003fbf488SJarkko Nikula } 151a0d2642eSMika Westerberg } 152a0d2642eSMika Westerberg 153e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data) 154e5262d05SWeike Chen { 155e5262d05SWeike Chen return drv_data->ssp_type == QUARK_X1000_SSP; 156e5262d05SWeike Chen } 157e5262d05SWeike Chen 1584fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) 1594fdb2424SWeike Chen { 1604fdb2424SWeike Chen switch (drv_data->ssp_type) { 161e5262d05SWeike Chen case QUARK_X1000_SSP: 162e5262d05SWeike Chen return QUARK_X1000_SSCR1_CHANGE_MASK; 1634fdb2424SWeike Chen default: 1644fdb2424SWeike Chen return SSCR1_CHANGE_MASK; 1654fdb2424SWeike Chen } 1664fdb2424SWeike Chen } 1674fdb2424SWeike Chen 1684fdb2424SWeike Chen static u32 1694fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) 1704fdb2424SWeike Chen { 1714fdb2424SWeike Chen switch (drv_data->ssp_type) { 172e5262d05SWeike Chen case QUARK_X1000_SSP: 173e5262d05SWeike Chen return RX_THRESH_QUARK_X1000_DFLT; 1744fdb2424SWeike Chen default: 1754fdb2424SWeike Chen return RX_THRESH_DFLT; 1764fdb2424SWeike Chen } 1774fdb2424SWeike Chen } 1784fdb2424SWeike Chen 1794fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) 1804fdb2424SWeike Chen { 1814fdb2424SWeike Chen u32 mask; 1824fdb2424SWeike Chen 1834fdb2424SWeike Chen switch (drv_data->ssp_type) { 184e5262d05SWeike Chen case QUARK_X1000_SSP: 185e5262d05SWeike Chen mask = QUARK_X1000_SSSR_TFL_MASK; 186e5262d05SWeike Chen break; 1874fdb2424SWeike Chen default: 1884fdb2424SWeike Chen mask = SSSR_TFL_MASK; 1894fdb2424SWeike Chen break; 1904fdb2424SWeike Chen } 1914fdb2424SWeike Chen 192c039dd27SJarkko Nikula return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask; 1934fdb2424SWeike Chen } 1944fdb2424SWeike Chen 1954fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, 1964fdb2424SWeike Chen u32 *sccr1_reg) 1974fdb2424SWeike Chen { 1984fdb2424SWeike Chen u32 mask; 1994fdb2424SWeike Chen 2004fdb2424SWeike Chen switch (drv_data->ssp_type) { 201e5262d05SWeike Chen case QUARK_X1000_SSP: 202e5262d05SWeike Chen mask = QUARK_X1000_SSCR1_RFT; 203e5262d05SWeike Chen break; 2044fdb2424SWeike Chen default: 2054fdb2424SWeike Chen mask = SSCR1_RFT; 2064fdb2424SWeike Chen break; 2074fdb2424SWeike Chen } 2084fdb2424SWeike Chen *sccr1_reg &= ~mask; 2094fdb2424SWeike Chen } 2104fdb2424SWeike Chen 2114fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, 2124fdb2424SWeike Chen u32 *sccr1_reg, u32 threshold) 2134fdb2424SWeike Chen { 2144fdb2424SWeike Chen switch (drv_data->ssp_type) { 215e5262d05SWeike Chen case QUARK_X1000_SSP: 216e5262d05SWeike Chen *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); 217e5262d05SWeike Chen break; 2184fdb2424SWeike Chen default: 2194fdb2424SWeike Chen *sccr1_reg |= SSCR1_RxTresh(threshold); 2204fdb2424SWeike Chen break; 2214fdb2424SWeike Chen } 2224fdb2424SWeike Chen } 2234fdb2424SWeike Chen 2244fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, 2254fdb2424SWeike Chen u32 clk_div, u8 bits) 2264fdb2424SWeike Chen { 2274fdb2424SWeike Chen switch (drv_data->ssp_type) { 228e5262d05SWeike Chen case QUARK_X1000_SSP: 229e5262d05SWeike Chen return clk_div 230e5262d05SWeike Chen | QUARK_X1000_SSCR0_Motorola 231e5262d05SWeike Chen | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits) 232e5262d05SWeike Chen | SSCR0_SSE; 2334fdb2424SWeike Chen default: 2344fdb2424SWeike Chen return clk_div 2354fdb2424SWeike Chen | SSCR0_Motorola 2364fdb2424SWeike Chen | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) 2374fdb2424SWeike Chen | SSCR0_SSE 2384fdb2424SWeike Chen | (bits > 16 ? SSCR0_EDSS : 0); 2394fdb2424SWeike Chen } 2404fdb2424SWeike Chen } 2414fdb2424SWeike Chen 242a0d2642eSMika Westerberg /* 243a0d2642eSMika Westerberg * Read and write LPSS SSP private registers. Caller must first check that 244a0d2642eSMika Westerberg * is_lpss_ssp() returns true before these can be called. 245a0d2642eSMika Westerberg */ 246a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) 247a0d2642eSMika Westerberg { 248a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 249a0d2642eSMika Westerberg return readl(drv_data->lpss_base + offset); 250a0d2642eSMika Westerberg } 251a0d2642eSMika Westerberg 252a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data, 253a0d2642eSMika Westerberg unsigned offset, u32 value) 254a0d2642eSMika Westerberg { 255a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 256a0d2642eSMika Westerberg writel(value, drv_data->lpss_base + offset); 257a0d2642eSMika Westerberg } 258a0d2642eSMika Westerberg 259a0d2642eSMika Westerberg /* 260a0d2642eSMika Westerberg * lpss_ssp_setup - perform LPSS SSP specific setup 261a0d2642eSMika Westerberg * @drv_data: pointer to the driver private data 262a0d2642eSMika Westerberg * 263a0d2642eSMika Westerberg * Perform LPSS SSP specific setup. This function must be called first if 264a0d2642eSMika Westerberg * one is going to use LPSS SSP private registers. 265a0d2642eSMika Westerberg */ 266a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data) 267a0d2642eSMika Westerberg { 268dccf7369SJarkko Nikula const struct lpss_config *config; 269dccf7369SJarkko Nikula u32 value; 270a0d2642eSMika Westerberg 271dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 272dccf7369SJarkko Nikula drv_data->lpss_base = drv_data->ioaddr + config->offset; 273a0d2642eSMika Westerberg 274a0d2642eSMika Westerberg /* Enable software chip select control */ 2750e897218SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 276624ea72eSJarkko Nikula value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH); 277624ea72eSJarkko Nikula value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH; 278dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 2790054e28dSMika Westerberg 2800054e28dSMika Westerberg /* Enable multiblock DMA transfers */ 2811de70612SMika Westerberg if (drv_data->master_info->enable_dma) { 282dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); 2831de70612SMika Westerberg 28482ba2c2aSJarkko Nikula if (config->reg_general >= 0) { 28582ba2c2aSJarkko Nikula value = __lpss_ssp_read_priv(drv_data, 28682ba2c2aSJarkko Nikula config->reg_general); 287624ea72eSJarkko Nikula value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE; 28882ba2c2aSJarkko Nikula __lpss_ssp_write_priv(drv_data, 28982ba2c2aSJarkko Nikula config->reg_general, value); 29082ba2c2aSJarkko Nikula } 2911de70612SMika Westerberg } 292a0d2642eSMika Westerberg } 293a0d2642eSMika Westerberg 294*c1e4a53cSMika Westerberg static void lpss_ssp_select_cs(struct driver_data *drv_data, 295*c1e4a53cSMika Westerberg const struct lpss_config *config) 296a0d2642eSMika Westerberg { 297d0283eb2SJarkko Nikula u32 value, cs; 298a0d2642eSMika Westerberg 299*c1e4a53cSMika Westerberg if (!config->cs_sel_mask) 300*c1e4a53cSMika Westerberg return; 301dccf7369SJarkko Nikula 302dccf7369SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 303*c1e4a53cSMika Westerberg 304d0283eb2SJarkko Nikula cs = drv_data->cur_msg->spi->chip_select; 305*c1e4a53cSMika Westerberg cs <<= config->cs_sel_shift; 306*c1e4a53cSMika Westerberg if (cs != (value & config->cs_sel_mask)) { 307d0283eb2SJarkko Nikula /* 308*c1e4a53cSMika Westerberg * When switching another chip select output active the 309*c1e4a53cSMika Westerberg * output must be selected first and wait 2 ssp_clk cycles 310*c1e4a53cSMika Westerberg * before changing state to active. Otherwise a short 311*c1e4a53cSMika Westerberg * glitch will occur on the previous chip select since 312*c1e4a53cSMika Westerberg * output select is latched but state control is not. 313d0283eb2SJarkko Nikula */ 314*c1e4a53cSMika Westerberg value &= ~config->cs_sel_mask; 315d0283eb2SJarkko Nikula value |= cs; 316d0283eb2SJarkko Nikula __lpss_ssp_write_priv(drv_data, 317d0283eb2SJarkko Nikula config->reg_cs_ctrl, value); 318d0283eb2SJarkko Nikula ndelay(1000000000 / 319d0283eb2SJarkko Nikula (drv_data->master->max_speed_hz / 2)); 320d0283eb2SJarkko Nikula } 321d0283eb2SJarkko Nikula } 322*c1e4a53cSMika Westerberg 323*c1e4a53cSMika Westerberg static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable) 324*c1e4a53cSMika Westerberg { 325*c1e4a53cSMika Westerberg const struct lpss_config *config; 326*c1e4a53cSMika Westerberg u32 value; 327*c1e4a53cSMika Westerberg 328*c1e4a53cSMika Westerberg config = lpss_get_config(drv_data); 329*c1e4a53cSMika Westerberg 330*c1e4a53cSMika Westerberg if (enable) 331*c1e4a53cSMika Westerberg lpss_ssp_select_cs(drv_data, config); 332*c1e4a53cSMika Westerberg 333*c1e4a53cSMika Westerberg value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 334*c1e4a53cSMika Westerberg if (enable) 335*c1e4a53cSMika Westerberg value &= ~LPSS_CS_CONTROL_CS_HIGH; 336*c1e4a53cSMika Westerberg else 337*c1e4a53cSMika Westerberg value |= LPSS_CS_CONTROL_CS_HIGH; 338dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 339a0d2642eSMika Westerberg } 340a0d2642eSMika Westerberg 341ca632f55SGrant Likely static void cs_assert(struct driver_data *drv_data) 342ca632f55SGrant Likely { 343ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 344ca632f55SGrant Likely 345ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 346c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm); 347ca632f55SGrant Likely return; 348ca632f55SGrant Likely } 349ca632f55SGrant Likely 350ca632f55SGrant Likely if (chip->cs_control) { 351ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_ASSERT); 352ca632f55SGrant Likely return; 353ca632f55SGrant Likely } 354ca632f55SGrant Likely 355a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 356ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); 357a0d2642eSMika Westerberg return; 358a0d2642eSMika Westerberg } 359a0d2642eSMika Westerberg 3607566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 361a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, true); 362ca632f55SGrant Likely } 363ca632f55SGrant Likely 364ca632f55SGrant Likely static void cs_deassert(struct driver_data *drv_data) 365ca632f55SGrant Likely { 366ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 367ca632f55SGrant Likely 368ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 369ca632f55SGrant Likely return; 370ca632f55SGrant Likely 371ca632f55SGrant Likely if (chip->cs_control) { 372ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_DEASSERT); 373ca632f55SGrant Likely return; 374ca632f55SGrant Likely } 375ca632f55SGrant Likely 376a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 377ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); 378a0d2642eSMika Westerberg return; 379a0d2642eSMika Westerberg } 380a0d2642eSMika Westerberg 3817566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 382a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, false); 383ca632f55SGrant Likely } 384ca632f55SGrant Likely 385cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data) 386ca632f55SGrant Likely { 387ca632f55SGrant Likely unsigned long limit = loops_per_jiffy << 1; 388ca632f55SGrant Likely 389ca632f55SGrant Likely do { 390c039dd27SJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 391c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 392c039dd27SJarkko Nikula } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); 393ca632f55SGrant Likely write_SSSR_CS(drv_data, SSSR_ROR); 394ca632f55SGrant Likely 395ca632f55SGrant Likely return limit; 396ca632f55SGrant Likely } 397ca632f55SGrant Likely 398ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data) 399ca632f55SGrant Likely { 400ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 401ca632f55SGrant Likely 4024fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 403ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 404ca632f55SGrant Likely return 0; 405ca632f55SGrant Likely 406c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, 0); 407ca632f55SGrant Likely drv_data->tx += n_bytes; 408ca632f55SGrant Likely 409ca632f55SGrant Likely return 1; 410ca632f55SGrant Likely } 411ca632f55SGrant Likely 412ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data) 413ca632f55SGrant Likely { 414ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 415ca632f55SGrant Likely 416c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 417ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 418c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 419ca632f55SGrant Likely drv_data->rx += n_bytes; 420ca632f55SGrant Likely } 421ca632f55SGrant Likely 422ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 423ca632f55SGrant Likely } 424ca632f55SGrant Likely 425ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data) 426ca632f55SGrant Likely { 4274fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 428ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 429ca632f55SGrant Likely return 0; 430ca632f55SGrant Likely 431c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); 432ca632f55SGrant Likely ++drv_data->tx; 433ca632f55SGrant Likely 434ca632f55SGrant Likely return 1; 435ca632f55SGrant Likely } 436ca632f55SGrant Likely 437ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data) 438ca632f55SGrant Likely { 439c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 440ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 441c039dd27SJarkko Nikula *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 442ca632f55SGrant Likely ++drv_data->rx; 443ca632f55SGrant Likely } 444ca632f55SGrant Likely 445ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 446ca632f55SGrant Likely } 447ca632f55SGrant Likely 448ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data) 449ca632f55SGrant Likely { 4504fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 451ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 452ca632f55SGrant Likely return 0; 453ca632f55SGrant Likely 454c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); 455ca632f55SGrant Likely drv_data->tx += 2; 456ca632f55SGrant Likely 457ca632f55SGrant Likely return 1; 458ca632f55SGrant Likely } 459ca632f55SGrant Likely 460ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data) 461ca632f55SGrant Likely { 462c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 463ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 464c039dd27SJarkko Nikula *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 465ca632f55SGrant Likely drv_data->rx += 2; 466ca632f55SGrant Likely } 467ca632f55SGrant Likely 468ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 469ca632f55SGrant Likely } 470ca632f55SGrant Likely 471ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data) 472ca632f55SGrant Likely { 4734fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 474ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 475ca632f55SGrant Likely return 0; 476ca632f55SGrant Likely 477c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); 478ca632f55SGrant Likely drv_data->tx += 4; 479ca632f55SGrant Likely 480ca632f55SGrant Likely return 1; 481ca632f55SGrant Likely } 482ca632f55SGrant Likely 483ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data) 484ca632f55SGrant Likely { 485c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 486ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 487c039dd27SJarkko Nikula *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 488ca632f55SGrant Likely drv_data->rx += 4; 489ca632f55SGrant Likely } 490ca632f55SGrant Likely 491ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 492ca632f55SGrant Likely } 493ca632f55SGrant Likely 494cd7bed00SMika Westerberg void *pxa2xx_spi_next_transfer(struct driver_data *drv_data) 495ca632f55SGrant Likely { 496ca632f55SGrant Likely struct spi_message *msg = drv_data->cur_msg; 497ca632f55SGrant Likely struct spi_transfer *trans = drv_data->cur_transfer; 498ca632f55SGrant Likely 499ca632f55SGrant Likely /* Move to next transfer */ 500ca632f55SGrant Likely if (trans->transfer_list.next != &msg->transfers) { 501ca632f55SGrant Likely drv_data->cur_transfer = 502ca632f55SGrant Likely list_entry(trans->transfer_list.next, 503ca632f55SGrant Likely struct spi_transfer, 504ca632f55SGrant Likely transfer_list); 505ca632f55SGrant Likely return RUNNING_STATE; 506ca632f55SGrant Likely } else 507ca632f55SGrant Likely return DONE_STATE; 508ca632f55SGrant Likely } 509ca632f55SGrant Likely 510ca632f55SGrant Likely /* caller already set message->status; dma and pio irqs are blocked */ 511ca632f55SGrant Likely static void giveback(struct driver_data *drv_data) 512ca632f55SGrant Likely { 513ca632f55SGrant Likely struct spi_transfer* last_transfer; 514ca632f55SGrant Likely struct spi_message *msg; 5157a8d44bcSJarkko Nikula unsigned long timeout; 516ca632f55SGrant Likely 517ca632f55SGrant Likely msg = drv_data->cur_msg; 518ca632f55SGrant Likely drv_data->cur_msg = NULL; 519ca632f55SGrant Likely drv_data->cur_transfer = NULL; 520ca632f55SGrant Likely 52123e2c2aaSAxel Lin last_transfer = list_last_entry(&msg->transfers, struct spi_transfer, 522ca632f55SGrant Likely transfer_list); 523ca632f55SGrant Likely 524ca632f55SGrant Likely /* Delay if requested before any change in chip select */ 525ca632f55SGrant Likely if (last_transfer->delay_usecs) 526ca632f55SGrant Likely udelay(last_transfer->delay_usecs); 527ca632f55SGrant Likely 5287a8d44bcSJarkko Nikula /* Wait until SSP becomes idle before deasserting the CS */ 5297a8d44bcSJarkko Nikula timeout = jiffies + msecs_to_jiffies(10); 5307a8d44bcSJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY && 5317a8d44bcSJarkko Nikula !time_after(jiffies, timeout)) 5327a8d44bcSJarkko Nikula cpu_relax(); 5337a8d44bcSJarkko Nikula 534ca632f55SGrant Likely /* Drop chip select UNLESS cs_change is true or we are returning 535ca632f55SGrant Likely * a message with an error, or next message is for another chip 536ca632f55SGrant Likely */ 537ca632f55SGrant Likely if (!last_transfer->cs_change) 538ca632f55SGrant Likely cs_deassert(drv_data); 539ca632f55SGrant Likely else { 540ca632f55SGrant Likely struct spi_message *next_msg; 541ca632f55SGrant Likely 542ca632f55SGrant Likely /* Holding of cs was hinted, but we need to make sure 543ca632f55SGrant Likely * the next message is for the same chip. Don't waste 544ca632f55SGrant Likely * time with the following tests unless this was hinted. 545ca632f55SGrant Likely * 546ca632f55SGrant Likely * We cannot postpone this until pump_messages, because 547ca632f55SGrant Likely * after calling msg->complete (below) the driver that 548ca632f55SGrant Likely * sent the current message could be unloaded, which 549ca632f55SGrant Likely * could invalidate the cs_control() callback... 550ca632f55SGrant Likely */ 551ca632f55SGrant Likely 552ca632f55SGrant Likely /* get a pointer to the next message, if any */ 5537f86bde9SMika Westerberg next_msg = spi_get_next_queued_message(drv_data->master); 554ca632f55SGrant Likely 555ca632f55SGrant Likely /* see if the next and current messages point 556ca632f55SGrant Likely * to the same chip 557ca632f55SGrant Likely */ 558ca632f55SGrant Likely if (next_msg && next_msg->spi != msg->spi) 559ca632f55SGrant Likely next_msg = NULL; 560ca632f55SGrant Likely if (!next_msg || msg->state == ERROR_STATE) 561ca632f55SGrant Likely cs_deassert(drv_data); 562ca632f55SGrant Likely } 563ca632f55SGrant Likely 564ca632f55SGrant Likely drv_data->cur_chip = NULL; 565c957e8f0SMika Westerberg spi_finalize_current_message(drv_data->master); 566ca632f55SGrant Likely } 567ca632f55SGrant Likely 568ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data) 569ca632f55SGrant Likely { 570ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 571ca632f55SGrant Likely u32 sccr1_reg; 572ca632f55SGrant Likely 573c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; 574ca632f55SGrant Likely sccr1_reg &= ~SSCR1_RFT; 575ca632f55SGrant Likely sccr1_reg |= chip->threshold; 576c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 577ca632f55SGrant Likely } 578ca632f55SGrant Likely 579ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg) 580ca632f55SGrant Likely { 581ca632f55SGrant Likely /* Stop and reset SSP */ 582ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 583ca632f55SGrant Likely reset_sccr1(drv_data); 584ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 585c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 586cd7bed00SMika Westerberg pxa2xx_spi_flush(drv_data); 587c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 588c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 589ca632f55SGrant Likely 590ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "%s\n", msg); 591ca632f55SGrant Likely 592ca632f55SGrant Likely drv_data->cur_msg->state = ERROR_STATE; 593ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 594ca632f55SGrant Likely } 595ca632f55SGrant Likely 596ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data) 597ca632f55SGrant Likely { 59807550df0SJarkko Nikula /* Clear and disable interrupts */ 599ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 600ca632f55SGrant Likely reset_sccr1(drv_data); 601ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 602c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 603ca632f55SGrant Likely 604ca632f55SGrant Likely /* Update total byte transferred return count actual bytes read */ 605ca632f55SGrant Likely drv_data->cur_msg->actual_length += drv_data->len - 606ca632f55SGrant Likely (drv_data->rx_end - drv_data->rx); 607ca632f55SGrant Likely 608ca632f55SGrant Likely /* Transfer delays and chip select release are 609ca632f55SGrant Likely * handled in pump_transfers or giveback 610ca632f55SGrant Likely */ 611ca632f55SGrant Likely 612ca632f55SGrant Likely /* Move to next transfer */ 613cd7bed00SMika Westerberg drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data); 614ca632f55SGrant Likely 615ca632f55SGrant Likely /* Schedule transfer tasklet */ 616ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 617ca632f55SGrant Likely } 618ca632f55SGrant Likely 619ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data) 620ca632f55SGrant Likely { 621c039dd27SJarkko Nikula u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ? 622ca632f55SGrant Likely drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; 623ca632f55SGrant Likely 624c039dd27SJarkko Nikula u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask; 625ca632f55SGrant Likely 626ca632f55SGrant Likely if (irq_status & SSSR_ROR) { 627ca632f55SGrant Likely int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); 628ca632f55SGrant Likely return IRQ_HANDLED; 629ca632f55SGrant Likely } 630ca632f55SGrant Likely 631ca632f55SGrant Likely if (irq_status & SSSR_TINT) { 632c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); 633ca632f55SGrant Likely if (drv_data->read(drv_data)) { 634ca632f55SGrant Likely int_transfer_complete(drv_data); 635ca632f55SGrant Likely return IRQ_HANDLED; 636ca632f55SGrant Likely } 637ca632f55SGrant Likely } 638ca632f55SGrant Likely 639ca632f55SGrant Likely /* Drain rx fifo, Fill tx fifo and prevent overruns */ 640ca632f55SGrant Likely do { 641ca632f55SGrant Likely if (drv_data->read(drv_data)) { 642ca632f55SGrant Likely int_transfer_complete(drv_data); 643ca632f55SGrant Likely return IRQ_HANDLED; 644ca632f55SGrant Likely } 645ca632f55SGrant Likely } while (drv_data->write(drv_data)); 646ca632f55SGrant Likely 647ca632f55SGrant Likely if (drv_data->read(drv_data)) { 648ca632f55SGrant Likely int_transfer_complete(drv_data); 649ca632f55SGrant Likely return IRQ_HANDLED; 650ca632f55SGrant Likely } 651ca632f55SGrant Likely 652ca632f55SGrant Likely if (drv_data->tx == drv_data->tx_end) { 653ca632f55SGrant Likely u32 bytes_left; 654ca632f55SGrant Likely u32 sccr1_reg; 655ca632f55SGrant Likely 656c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 657ca632f55SGrant Likely sccr1_reg &= ~SSCR1_TIE; 658ca632f55SGrant Likely 659ca632f55SGrant Likely /* 660ca632f55SGrant Likely * PXA25x_SSP has no timeout, set up rx threshould for the 661ca632f55SGrant Likely * remaining RX bytes. 662ca632f55SGrant Likely */ 663ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 6644fdb2424SWeike Chen u32 rx_thre; 665ca632f55SGrant Likely 6664fdb2424SWeike Chen pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); 667ca632f55SGrant Likely 668ca632f55SGrant Likely bytes_left = drv_data->rx_end - drv_data->rx; 669ca632f55SGrant Likely switch (drv_data->n_bytes) { 670ca632f55SGrant Likely case 4: 671ca632f55SGrant Likely bytes_left >>= 1; 672ca632f55SGrant Likely case 2: 673ca632f55SGrant Likely bytes_left >>= 1; 674ca632f55SGrant Likely } 675ca632f55SGrant Likely 6764fdb2424SWeike Chen rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); 6774fdb2424SWeike Chen if (rx_thre > bytes_left) 6784fdb2424SWeike Chen rx_thre = bytes_left; 679ca632f55SGrant Likely 6804fdb2424SWeike Chen pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); 681ca632f55SGrant Likely } 682c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 683ca632f55SGrant Likely } 684ca632f55SGrant Likely 685ca632f55SGrant Likely /* We did something */ 686ca632f55SGrant Likely return IRQ_HANDLED; 687ca632f55SGrant Likely } 688ca632f55SGrant Likely 689ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id) 690ca632f55SGrant Likely { 691ca632f55SGrant Likely struct driver_data *drv_data = dev_id; 6927d94a505SMika Westerberg u32 sccr1_reg; 693ca632f55SGrant Likely u32 mask = drv_data->mask_sr; 694ca632f55SGrant Likely u32 status; 695ca632f55SGrant Likely 6967d94a505SMika Westerberg /* 6977d94a505SMika Westerberg * The IRQ might be shared with other peripherals so we must first 6987d94a505SMika Westerberg * check that are we RPM suspended or not. If we are we assume that 6997d94a505SMika Westerberg * the IRQ was not for us (we shouldn't be RPM suspended when the 7007d94a505SMika Westerberg * interrupt is enabled). 7017d94a505SMika Westerberg */ 7027d94a505SMika Westerberg if (pm_runtime_suspended(&drv_data->pdev->dev)) 7037d94a505SMika Westerberg return IRQ_NONE; 7047d94a505SMika Westerberg 705269e4a41SMika Westerberg /* 706269e4a41SMika Westerberg * If the device is not yet in RPM suspended state and we get an 707269e4a41SMika Westerberg * interrupt that is meant for another device, check if status bits 708269e4a41SMika Westerberg * are all set to one. That means that the device is already 709269e4a41SMika Westerberg * powered off. 710269e4a41SMika Westerberg */ 711c039dd27SJarkko Nikula status = pxa2xx_spi_read(drv_data, SSSR); 712269e4a41SMika Westerberg if (status == ~0) 713269e4a41SMika Westerberg return IRQ_NONE; 714269e4a41SMika Westerberg 715c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 716ca632f55SGrant Likely 717ca632f55SGrant Likely /* Ignore possible writes if we don't need to write */ 718ca632f55SGrant Likely if (!(sccr1_reg & SSCR1_TIE)) 719ca632f55SGrant Likely mask &= ~SSSR_TFS; 720ca632f55SGrant Likely 72102bc933eSTan, Jui Nee /* Ignore RX timeout interrupt if it is disabled */ 72202bc933eSTan, Jui Nee if (!(sccr1_reg & SSCR1_TINTE)) 72302bc933eSTan, Jui Nee mask &= ~SSSR_TINT; 72402bc933eSTan, Jui Nee 725ca632f55SGrant Likely if (!(status & mask)) 726ca632f55SGrant Likely return IRQ_NONE; 727ca632f55SGrant Likely 728ca632f55SGrant Likely if (!drv_data->cur_msg) { 729ca632f55SGrant Likely 730c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 731c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) 732c039dd27SJarkko Nikula & ~SSCR0_SSE); 733c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, 734c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR1) 735c039dd27SJarkko Nikula & ~drv_data->int_cr1); 736ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 737c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 738ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 739ca632f55SGrant Likely 740f6bd03a7SJarkko Nikula dev_err(&drv_data->pdev->dev, 741f6bd03a7SJarkko Nikula "bad message state in interrupt handler\n"); 742ca632f55SGrant Likely 743ca632f55SGrant Likely /* Never fail */ 744ca632f55SGrant Likely return IRQ_HANDLED; 745ca632f55SGrant Likely } 746ca632f55SGrant Likely 747ca632f55SGrant Likely return drv_data->transfer_handler(drv_data); 748ca632f55SGrant Likely } 749ca632f55SGrant Likely 750e5262d05SWeike Chen /* 7519df461ecSAndy Shevchenko * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply 7529df461ecSAndy Shevchenko * input frequency by fractions of 2^24. It also has a divider by 5. 7539df461ecSAndy Shevchenko * 7549df461ecSAndy Shevchenko * There are formulas to get baud rate value for given input frequency and 7559df461ecSAndy Shevchenko * divider parameters, such as DDS_CLK_RATE and SCR: 7569df461ecSAndy Shevchenko * 7579df461ecSAndy Shevchenko * Fsys = 200MHz 7589df461ecSAndy Shevchenko * 7599df461ecSAndy Shevchenko * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) 7609df461ecSAndy Shevchenko * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) 7619df461ecSAndy Shevchenko * 7629df461ecSAndy Shevchenko * DDS_CLK_RATE either 2^n or 2^n / 5. 7639df461ecSAndy Shevchenko * SCR is in range 0 .. 255 7649df461ecSAndy Shevchenko * 7659df461ecSAndy Shevchenko * Divisor = 5^i * 2^j * 2 * k 7669df461ecSAndy Shevchenko * i = [0, 1] i = 1 iff j = 0 or j > 3 7679df461ecSAndy Shevchenko * j = [0, 23] j = 0 iff i = 1 7689df461ecSAndy Shevchenko * k = [1, 256] 7699df461ecSAndy Shevchenko * Special case: j = 0, i = 1: Divisor = 2 / 5 7709df461ecSAndy Shevchenko * 7719df461ecSAndy Shevchenko * Accordingly to the specification the recommended values for DDS_CLK_RATE 7729df461ecSAndy Shevchenko * are: 7739df461ecSAndy Shevchenko * Case 1: 2^n, n = [0, 23] 7749df461ecSAndy Shevchenko * Case 2: 2^24 * 2 / 5 (0x666666) 7759df461ecSAndy Shevchenko * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) 7769df461ecSAndy Shevchenko * 7779df461ecSAndy Shevchenko * In all cases the lowest possible value is better. 7789df461ecSAndy Shevchenko * 7799df461ecSAndy Shevchenko * The function calculates parameters for all cases and chooses the one closest 7809df461ecSAndy Shevchenko * to the asked baud rate. 781e5262d05SWeike Chen */ 7829df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) 783e5262d05SWeike Chen { 7849df461ecSAndy Shevchenko unsigned long xtal = 200000000; 7859df461ecSAndy Shevchenko unsigned long fref = xtal / 2; /* mandatory division by 2, 7869df461ecSAndy Shevchenko see (2) */ 7879df461ecSAndy Shevchenko /* case 3 */ 7889df461ecSAndy Shevchenko unsigned long fref1 = fref / 2; /* case 1 */ 7899df461ecSAndy Shevchenko unsigned long fref2 = fref * 2 / 5; /* case 2 */ 7909df461ecSAndy Shevchenko unsigned long scale; 7919df461ecSAndy Shevchenko unsigned long q, q1, q2; 7929df461ecSAndy Shevchenko long r, r1, r2; 7939df461ecSAndy Shevchenko u32 mul; 794e5262d05SWeike Chen 7959df461ecSAndy Shevchenko /* Case 1 */ 7969df461ecSAndy Shevchenko 7979df461ecSAndy Shevchenko /* Set initial value for DDS_CLK_RATE */ 7989df461ecSAndy Shevchenko mul = (1 << 24) >> 1; 7999df461ecSAndy Shevchenko 8009df461ecSAndy Shevchenko /* Calculate initial quot */ 8013ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref1, rate); 8029df461ecSAndy Shevchenko 8039df461ecSAndy Shevchenko /* Scale q1 if it's too big */ 8049df461ecSAndy Shevchenko if (q1 > 256) { 8059df461ecSAndy Shevchenko /* Scale q1 to range [1, 512] */ 8069df461ecSAndy Shevchenko scale = fls_long(q1 - 1); 8079df461ecSAndy Shevchenko if (scale > 9) { 8089df461ecSAndy Shevchenko q1 >>= scale - 9; 8099df461ecSAndy Shevchenko mul >>= scale - 9; 8109df461ecSAndy Shevchenko } 8119df461ecSAndy Shevchenko 8129df461ecSAndy Shevchenko /* Round the result if we have a remainder */ 8139df461ecSAndy Shevchenko q1 += q1 & 1; 8149df461ecSAndy Shevchenko } 8159df461ecSAndy Shevchenko 8169df461ecSAndy Shevchenko /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ 8179df461ecSAndy Shevchenko scale = __ffs(q1); 8189df461ecSAndy Shevchenko q1 >>= scale; 8199df461ecSAndy Shevchenko mul >>= scale; 8209df461ecSAndy Shevchenko 8219df461ecSAndy Shevchenko /* Get the remainder */ 8229df461ecSAndy Shevchenko r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); 8239df461ecSAndy Shevchenko 8249df461ecSAndy Shevchenko /* Case 2 */ 8259df461ecSAndy Shevchenko 8263ad48062SAndy Shevchenko q2 = DIV_ROUND_UP(fref2, rate); 8279df461ecSAndy Shevchenko r2 = abs(fref2 / q2 - rate); 8289df461ecSAndy Shevchenko 8299df461ecSAndy Shevchenko /* 8309df461ecSAndy Shevchenko * Choose the best between two: less remainder we have the better. We 8319df461ecSAndy Shevchenko * can't go case 2 if q2 is greater than 256 since SCR register can 8329df461ecSAndy Shevchenko * hold only values 0 .. 255. 8339df461ecSAndy Shevchenko */ 8349df461ecSAndy Shevchenko if (r2 >= r1 || q2 > 256) { 8359df461ecSAndy Shevchenko /* case 1 is better */ 8369df461ecSAndy Shevchenko r = r1; 8379df461ecSAndy Shevchenko q = q1; 8389df461ecSAndy Shevchenko } else { 8399df461ecSAndy Shevchenko /* case 2 is better */ 8409df461ecSAndy Shevchenko r = r2; 8419df461ecSAndy Shevchenko q = q2; 8429df461ecSAndy Shevchenko mul = (1 << 24) * 2 / 5; 8439df461ecSAndy Shevchenko } 8449df461ecSAndy Shevchenko 8453ad48062SAndy Shevchenko /* Check case 3 only if the divisor is big enough */ 8469df461ecSAndy Shevchenko if (fref / rate >= 80) { 8479df461ecSAndy Shevchenko u64 fssp; 8489df461ecSAndy Shevchenko u32 m; 8499df461ecSAndy Shevchenko 8509df461ecSAndy Shevchenko /* Calculate initial quot */ 8513ad48062SAndy Shevchenko q1 = DIV_ROUND_UP(fref, rate); 8529df461ecSAndy Shevchenko m = (1 << 24) / q1; 8539df461ecSAndy Shevchenko 8549df461ecSAndy Shevchenko /* Get the remainder */ 8559df461ecSAndy Shevchenko fssp = (u64)fref * m; 8569df461ecSAndy Shevchenko do_div(fssp, 1 << 24); 8579df461ecSAndy Shevchenko r1 = abs(fssp - rate); 8589df461ecSAndy Shevchenko 8599df461ecSAndy Shevchenko /* Choose this one if it suits better */ 8609df461ecSAndy Shevchenko if (r1 < r) { 8619df461ecSAndy Shevchenko /* case 3 is better */ 8629df461ecSAndy Shevchenko q = 1; 8639df461ecSAndy Shevchenko mul = m; 864e5262d05SWeike Chen } 865e5262d05SWeike Chen } 866e5262d05SWeike Chen 8679df461ecSAndy Shevchenko *dds = mul; 8689df461ecSAndy Shevchenko return q - 1; 869e5262d05SWeike Chen } 870e5262d05SWeike Chen 8713343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) 872ca632f55SGrant Likely { 8730eca7cf2SJarkko Nikula unsigned long ssp_clk = drv_data->master->max_speed_hz; 8743343b7a6SMika Westerberg const struct ssp_device *ssp = drv_data->ssp; 8753343b7a6SMika Westerberg 8763343b7a6SMika Westerberg rate = min_t(int, ssp_clk, rate); 877ca632f55SGrant Likely 878ca632f55SGrant Likely if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) 879025ffe88SAndy Shevchenko return (ssp_clk / (2 * rate) - 1) & 0xff; 880ca632f55SGrant Likely else 881025ffe88SAndy Shevchenko return (ssp_clk / rate - 1) & 0xfff; 882ca632f55SGrant Likely } 883ca632f55SGrant Likely 884e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, 885d2c2f6a4SAndy Shevchenko int rate) 886e5262d05SWeike Chen { 887d2c2f6a4SAndy Shevchenko struct chip_data *chip = drv_data->cur_chip; 888025ffe88SAndy Shevchenko unsigned int clk_div; 889e5262d05SWeike Chen 890e5262d05SWeike Chen switch (drv_data->ssp_type) { 891e5262d05SWeike Chen case QUARK_X1000_SSP: 8929df461ecSAndy Shevchenko clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); 893eecacf73SDan Carpenter break; 894e5262d05SWeike Chen default: 895025ffe88SAndy Shevchenko clk_div = ssp_get_clk_div(drv_data, rate); 896eecacf73SDan Carpenter break; 897e5262d05SWeike Chen } 898025ffe88SAndy Shevchenko return clk_div << 8; 899e5262d05SWeike Chen } 900e5262d05SWeike Chen 901ca632f55SGrant Likely static void pump_transfers(unsigned long data) 902ca632f55SGrant Likely { 903ca632f55SGrant Likely struct driver_data *drv_data = (struct driver_data *)data; 904ca632f55SGrant Likely struct spi_message *message = NULL; 905ca632f55SGrant Likely struct spi_transfer *transfer = NULL; 906ca632f55SGrant Likely struct spi_transfer *previous = NULL; 907ca632f55SGrant Likely struct chip_data *chip = NULL; 908ca632f55SGrant Likely u32 clk_div = 0; 909ca632f55SGrant Likely u8 bits = 0; 910ca632f55SGrant Likely u32 speed = 0; 911ca632f55SGrant Likely u32 cr0; 912ca632f55SGrant Likely u32 cr1; 913ca632f55SGrant Likely u32 dma_thresh = drv_data->cur_chip->dma_threshold; 914ca632f55SGrant Likely u32 dma_burst = drv_data->cur_chip->dma_burst_size; 9154fdb2424SWeike Chen u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); 916ca632f55SGrant Likely 917ca632f55SGrant Likely /* Get current state information */ 918ca632f55SGrant Likely message = drv_data->cur_msg; 919ca632f55SGrant Likely transfer = drv_data->cur_transfer; 920ca632f55SGrant Likely chip = drv_data->cur_chip; 921ca632f55SGrant Likely 922ca632f55SGrant Likely /* Handle for abort */ 923ca632f55SGrant Likely if (message->state == ERROR_STATE) { 924ca632f55SGrant Likely message->status = -EIO; 925ca632f55SGrant Likely giveback(drv_data); 926ca632f55SGrant Likely return; 927ca632f55SGrant Likely } 928ca632f55SGrant Likely 929ca632f55SGrant Likely /* Handle end of message */ 930ca632f55SGrant Likely if (message->state == DONE_STATE) { 931ca632f55SGrant Likely message->status = 0; 932ca632f55SGrant Likely giveback(drv_data); 933ca632f55SGrant Likely return; 934ca632f55SGrant Likely } 935ca632f55SGrant Likely 936ca632f55SGrant Likely /* Delay if requested at end of transfer before CS change */ 937ca632f55SGrant Likely if (message->state == RUNNING_STATE) { 938ca632f55SGrant Likely previous = list_entry(transfer->transfer_list.prev, 939ca632f55SGrant Likely struct spi_transfer, 940ca632f55SGrant Likely transfer_list); 941ca632f55SGrant Likely if (previous->delay_usecs) 942ca632f55SGrant Likely udelay(previous->delay_usecs); 943ca632f55SGrant Likely 944ca632f55SGrant Likely /* Drop chip select only if cs_change is requested */ 945ca632f55SGrant Likely if (previous->cs_change) 946ca632f55SGrant Likely cs_deassert(drv_data); 947ca632f55SGrant Likely } 948ca632f55SGrant Likely 949cd7bed00SMika Westerberg /* Check if we can DMA this transfer */ 950cd7bed00SMika Westerberg if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) { 951ca632f55SGrant Likely 952ca632f55SGrant Likely /* reject already-mapped transfers; PIO won't always work */ 953ca632f55SGrant Likely if (message->is_dma_mapped 954ca632f55SGrant Likely || transfer->rx_dma || transfer->tx_dma) { 955ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, 956f6bd03a7SJarkko Nikula "pump_transfers: mapped transfer length of " 957f6bd03a7SJarkko Nikula "%u is greater than %d\n", 958ca632f55SGrant Likely transfer->len, MAX_DMA_LEN); 959ca632f55SGrant Likely message->status = -EINVAL; 960ca632f55SGrant Likely giveback(drv_data); 961ca632f55SGrant Likely return; 962ca632f55SGrant Likely } 963ca632f55SGrant Likely 964ca632f55SGrant Likely /* warn ... we force this to PIO mode */ 965f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 966f6bd03a7SJarkko Nikula "pump_transfers: DMA disabled for transfer length %ld " 967ca632f55SGrant Likely "greater than %d\n", 968ca632f55SGrant Likely (long)drv_data->len, MAX_DMA_LEN); 969ca632f55SGrant Likely } 970ca632f55SGrant Likely 971ca632f55SGrant Likely /* Setup the transfer state based on the type of transfer */ 972cd7bed00SMika Westerberg if (pxa2xx_spi_flush(drv_data) == 0) { 973ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); 974ca632f55SGrant Likely message->status = -EIO; 975ca632f55SGrant Likely giveback(drv_data); 976ca632f55SGrant Likely return; 977ca632f55SGrant Likely } 978ca632f55SGrant Likely drv_data->n_bytes = chip->n_bytes; 979ca632f55SGrant Likely drv_data->tx = (void *)transfer->tx_buf; 980ca632f55SGrant Likely drv_data->tx_end = drv_data->tx + transfer->len; 981ca632f55SGrant Likely drv_data->rx = transfer->rx_buf; 982ca632f55SGrant Likely drv_data->rx_end = drv_data->rx + transfer->len; 983ca632f55SGrant Likely drv_data->rx_dma = transfer->rx_dma; 984ca632f55SGrant Likely drv_data->tx_dma = transfer->tx_dma; 985cd7bed00SMika Westerberg drv_data->len = transfer->len; 986ca632f55SGrant Likely drv_data->write = drv_data->tx ? chip->write : null_writer; 987ca632f55SGrant Likely drv_data->read = drv_data->rx ? chip->read : null_reader; 988ca632f55SGrant Likely 989ca632f55SGrant Likely /* Change speed and bit per word on a per transfer */ 990ca632f55SGrant Likely bits = transfer->bits_per_word; 991ca632f55SGrant Likely speed = transfer->speed_hz; 992ca632f55SGrant Likely 993d2c2f6a4SAndy Shevchenko clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); 994ca632f55SGrant Likely 995ca632f55SGrant Likely if (bits <= 8) { 996ca632f55SGrant Likely drv_data->n_bytes = 1; 997ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 998ca632f55SGrant Likely u8_reader : null_reader; 999ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1000ca632f55SGrant Likely u8_writer : null_writer; 1001ca632f55SGrant Likely } else if (bits <= 16) { 1002ca632f55SGrant Likely drv_data->n_bytes = 2; 1003ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1004ca632f55SGrant Likely u16_reader : null_reader; 1005ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1006ca632f55SGrant Likely u16_writer : null_writer; 1007ca632f55SGrant Likely } else if (bits <= 32) { 1008ca632f55SGrant Likely drv_data->n_bytes = 4; 1009ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 1010ca632f55SGrant Likely u32_reader : null_reader; 1011ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 1012ca632f55SGrant Likely u32_writer : null_writer; 1013ca632f55SGrant Likely } 1014196b0e2cSJarkko Nikula /* 1015196b0e2cSJarkko Nikula * if bits/word is changed in dma mode, then must check the 1016196b0e2cSJarkko Nikula * thresholds and burst also 1017196b0e2cSJarkko Nikula */ 1018ca632f55SGrant Likely if (chip->enable_dma) { 1019cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, 1020cd7bed00SMika Westerberg message->spi, 1021ca632f55SGrant Likely bits, &dma_burst, 1022ca632f55SGrant Likely &dma_thresh)) 1023f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 1024f6bd03a7SJarkko Nikula "pump_transfers: DMA burst size reduced to match bits_per_word\n"); 1025ca632f55SGrant Likely } 1026ca632f55SGrant Likely 1027ca632f55SGrant Likely message->state = RUNNING_STATE; 1028ca632f55SGrant Likely 1029ca632f55SGrant Likely drv_data->dma_mapped = 0; 1030cd7bed00SMika Westerberg if (pxa2xx_spi_dma_is_possible(drv_data->len)) 1031cd7bed00SMika Westerberg drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data); 1032ca632f55SGrant Likely if (drv_data->dma_mapped) { 1033ca632f55SGrant Likely 1034ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1035cd7bed00SMika Westerberg drv_data->transfer_handler = pxa2xx_spi_dma_transfer; 1036ca632f55SGrant Likely 1037cd7bed00SMika Westerberg pxa2xx_spi_dma_prepare(drv_data, dma_burst); 1038ca632f55SGrant Likely 1039ca632f55SGrant Likely /* Clear status and start DMA engine */ 1040ca632f55SGrant Likely cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; 1041c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); 1042cd7bed00SMika Westerberg 1043cd7bed00SMika Westerberg pxa2xx_spi_dma_start(drv_data); 1044ca632f55SGrant Likely } else { 1045ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 1046ca632f55SGrant Likely drv_data->transfer_handler = interrupt_transfer; 1047ca632f55SGrant Likely 1048ca632f55SGrant Likely /* Clear status */ 1049ca632f55SGrant Likely cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; 1050ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 1051ca632f55SGrant Likely } 1052ca632f55SGrant Likely 1053ee03672dSJarkko Nikula /* NOTE: PXA25x_SSP _could_ use external clocking ... */ 1054ee03672dSJarkko Nikula cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); 1055ee03672dSJarkko Nikula if (!pxa25x_ssp_comp(drv_data)) 1056ee03672dSJarkko Nikula dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", 1057ee03672dSJarkko Nikula drv_data->master->max_speed_hz 1058ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), 1059ee03672dSJarkko Nikula drv_data->dma_mapped ? "DMA" : "PIO"); 1060ee03672dSJarkko Nikula else 1061ee03672dSJarkko Nikula dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", 1062ee03672dSJarkko Nikula drv_data->master->max_speed_hz / 2 1063ee03672dSJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), 1064ee03672dSJarkko Nikula drv_data->dma_mapped ? "DMA" : "PIO"); 1065ee03672dSJarkko Nikula 1066a0d2642eSMika Westerberg if (is_lpss_ssp(drv_data)) { 1067c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff) 1068c039dd27SJarkko Nikula != chip->lpss_rx_threshold) 1069c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSIRF, 1070c039dd27SJarkko Nikula chip->lpss_rx_threshold); 1071c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff) 1072c039dd27SJarkko Nikula != chip->lpss_tx_threshold) 1073c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSITF, 1074c039dd27SJarkko Nikula chip->lpss_tx_threshold); 1075a0d2642eSMika Westerberg } 1076a0d2642eSMika Westerberg 1077e5262d05SWeike Chen if (is_quark_x1000_ssp(drv_data) && 1078c039dd27SJarkko Nikula (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate)) 1079c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate); 1080e5262d05SWeike Chen 1081ca632f55SGrant Likely /* see if we need to reload the config registers */ 1082c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0) 1083c039dd27SJarkko Nikula || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask) 1084c039dd27SJarkko Nikula != (cr1 & change_mask)) { 1085ca632f55SGrant Likely /* stop the SSP, and update the other bits */ 1086c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE); 1087ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1088c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1089ca632f55SGrant Likely /* first set CR1 without interrupt and service enables */ 1090c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask); 1091ca632f55SGrant Likely /* restart the SSP */ 1092c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0); 1093ca632f55SGrant Likely 1094ca632f55SGrant Likely } else { 1095ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1096c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1097ca632f55SGrant Likely } 1098ca632f55SGrant Likely 1099ca632f55SGrant Likely cs_assert(drv_data); 1100ca632f55SGrant Likely 1101ca632f55SGrant Likely /* after chip select, release the data by enabling service 1102ca632f55SGrant Likely * requests and interrupts, without changing any mode bits */ 1103c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1); 1104ca632f55SGrant Likely } 1105ca632f55SGrant Likely 11067f86bde9SMika Westerberg static int pxa2xx_spi_transfer_one_message(struct spi_master *master, 11077f86bde9SMika Westerberg struct spi_message *msg) 1108ca632f55SGrant Likely { 11097f86bde9SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 1110ca632f55SGrant Likely 11117f86bde9SMika Westerberg drv_data->cur_msg = msg; 1112ca632f55SGrant Likely /* Initial message state*/ 1113ca632f55SGrant Likely drv_data->cur_msg->state = START_STATE; 1114ca632f55SGrant Likely drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, 1115ca632f55SGrant Likely struct spi_transfer, 1116ca632f55SGrant Likely transfer_list); 1117ca632f55SGrant Likely 1118ca632f55SGrant Likely /* prepare to setup the SSP, in pump_transfers, using the per 1119ca632f55SGrant Likely * chip configuration */ 1120ca632f55SGrant Likely drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); 1121ca632f55SGrant Likely 1122ca632f55SGrant Likely /* Mark as busy and launch transfers */ 1123ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 1124ca632f55SGrant Likely return 0; 1125ca632f55SGrant Likely } 1126ca632f55SGrant Likely 11277d94a505SMika Westerberg static int pxa2xx_spi_unprepare_transfer(struct spi_master *master) 11287d94a505SMika Westerberg { 11297d94a505SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 11307d94a505SMika Westerberg 11317d94a505SMika Westerberg /* Disable the SSP now */ 1132c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 1133c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 11347d94a505SMika Westerberg 11357d94a505SMika Westerberg return 0; 11367d94a505SMika Westerberg } 11377d94a505SMika Westerberg 1138ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip, 1139ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info) 1140ca632f55SGrant Likely { 1141ca632f55SGrant Likely int err = 0; 1142ca632f55SGrant Likely 1143ca632f55SGrant Likely if (chip == NULL || chip_info == NULL) 1144ca632f55SGrant Likely return 0; 1145ca632f55SGrant Likely 1146ca632f55SGrant Likely /* NOTE: setup() can be called multiple times, possibly with 1147ca632f55SGrant Likely * different chip_info, release previously requested GPIO 1148ca632f55SGrant Likely */ 1149ca632f55SGrant Likely if (gpio_is_valid(chip->gpio_cs)) 1150ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1151ca632f55SGrant Likely 1152ca632f55SGrant Likely /* If (*cs_control) is provided, ignore GPIO chip select */ 1153ca632f55SGrant Likely if (chip_info->cs_control) { 1154ca632f55SGrant Likely chip->cs_control = chip_info->cs_control; 1155ca632f55SGrant Likely return 0; 1156ca632f55SGrant Likely } 1157ca632f55SGrant Likely 1158ca632f55SGrant Likely if (gpio_is_valid(chip_info->gpio_cs)) { 1159ca632f55SGrant Likely err = gpio_request(chip_info->gpio_cs, "SPI_CS"); 1160ca632f55SGrant Likely if (err) { 1161f6bd03a7SJarkko Nikula dev_err(&spi->dev, "failed to request chip select GPIO%d\n", 1162f6bd03a7SJarkko Nikula chip_info->gpio_cs); 1163ca632f55SGrant Likely return err; 1164ca632f55SGrant Likely } 1165ca632f55SGrant Likely 1166ca632f55SGrant Likely chip->gpio_cs = chip_info->gpio_cs; 1167ca632f55SGrant Likely chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 1168ca632f55SGrant Likely 1169ca632f55SGrant Likely err = gpio_direction_output(chip->gpio_cs, 1170ca632f55SGrant Likely !chip->gpio_cs_inverted); 1171ca632f55SGrant Likely } 1172ca632f55SGrant Likely 1173ca632f55SGrant Likely return err; 1174ca632f55SGrant Likely } 1175ca632f55SGrant Likely 1176ca632f55SGrant Likely static int setup(struct spi_device *spi) 1177ca632f55SGrant Likely { 1178ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info = NULL; 1179ca632f55SGrant Likely struct chip_data *chip; 1180dccf7369SJarkko Nikula const struct lpss_config *config; 1181ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1182a0d2642eSMika Westerberg uint tx_thres, tx_hi_thres, rx_thres; 1183a0d2642eSMika Westerberg 1184e5262d05SWeike Chen switch (drv_data->ssp_type) { 1185e5262d05SWeike Chen case QUARK_X1000_SSP: 1186e5262d05SWeike Chen tx_thres = TX_THRESH_QUARK_X1000_DFLT; 1187e5262d05SWeike Chen tx_hi_thres = 0; 1188e5262d05SWeike Chen rx_thres = RX_THRESH_QUARK_X1000_DFLT; 1189e5262d05SWeike Chen break; 119003fbf488SJarkko Nikula case LPSS_LPT_SSP: 119103fbf488SJarkko Nikula case LPSS_BYT_SSP: 119234cadd9cSJarkko Nikula case LPSS_SPT_SSP: 1193b7c08cf8SJarkko Nikula case LPSS_BXT_SSP: 1194dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 1195dccf7369SJarkko Nikula tx_thres = config->tx_threshold_lo; 1196dccf7369SJarkko Nikula tx_hi_thres = config->tx_threshold_hi; 1197dccf7369SJarkko Nikula rx_thres = config->rx_threshold; 1198e5262d05SWeike Chen break; 1199e5262d05SWeike Chen default: 1200a0d2642eSMika Westerberg tx_thres = TX_THRESH_DFLT; 1201a0d2642eSMika Westerberg tx_hi_thres = 0; 1202a0d2642eSMika Westerberg rx_thres = RX_THRESH_DFLT; 1203e5262d05SWeike Chen break; 1204a0d2642eSMika Westerberg } 1205ca632f55SGrant Likely 1206ca632f55SGrant Likely /* Only alloc on first setup */ 1207ca632f55SGrant Likely chip = spi_get_ctldata(spi); 1208ca632f55SGrant Likely if (!chip) { 1209ca632f55SGrant Likely chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 12109deae459SJingoo Han if (!chip) 1211ca632f55SGrant Likely return -ENOMEM; 1212ca632f55SGrant Likely 1213ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 1214ca632f55SGrant Likely if (spi->chip_select > 4) { 1215f6bd03a7SJarkko Nikula dev_err(&spi->dev, 1216f6bd03a7SJarkko Nikula "failed setup: cs number must not be > 4.\n"); 1217ca632f55SGrant Likely kfree(chip); 1218ca632f55SGrant Likely return -EINVAL; 1219ca632f55SGrant Likely } 1220ca632f55SGrant Likely 1221ca632f55SGrant Likely chip->frm = spi->chip_select; 1222ca632f55SGrant Likely } else 1223ca632f55SGrant Likely chip->gpio_cs = -1; 1224ca632f55SGrant Likely chip->enable_dma = 0; 1225ca632f55SGrant Likely chip->timeout = TIMOUT_DFLT; 1226ca632f55SGrant Likely } 1227ca632f55SGrant Likely 1228ca632f55SGrant Likely /* protocol drivers may change the chip settings, so... 1229ca632f55SGrant Likely * if chip_info exists, use it */ 1230ca632f55SGrant Likely chip_info = spi->controller_data; 1231ca632f55SGrant Likely 1232ca632f55SGrant Likely /* chip_info isn't always needed */ 1233ca632f55SGrant Likely chip->cr1 = 0; 1234ca632f55SGrant Likely if (chip_info) { 1235ca632f55SGrant Likely if (chip_info->timeout) 1236ca632f55SGrant Likely chip->timeout = chip_info->timeout; 1237ca632f55SGrant Likely if (chip_info->tx_threshold) 1238ca632f55SGrant Likely tx_thres = chip_info->tx_threshold; 1239a0d2642eSMika Westerberg if (chip_info->tx_hi_threshold) 1240a0d2642eSMika Westerberg tx_hi_thres = chip_info->tx_hi_threshold; 1241ca632f55SGrant Likely if (chip_info->rx_threshold) 1242ca632f55SGrant Likely rx_thres = chip_info->rx_threshold; 1243ca632f55SGrant Likely chip->enable_dma = drv_data->master_info->enable_dma; 1244ca632f55SGrant Likely chip->dma_threshold = 0; 1245ca632f55SGrant Likely if (chip_info->enable_loopback) 1246ca632f55SGrant Likely chip->cr1 = SSCR1_LBM; 1247a3496855SMika Westerberg } else if (ACPI_HANDLE(&spi->dev)) { 1248a3496855SMika Westerberg /* 1249a3496855SMika Westerberg * Slave devices enumerated from ACPI namespace don't 1250a3496855SMika Westerberg * usually have chip_info but we still might want to use 1251a3496855SMika Westerberg * DMA with them. 1252a3496855SMika Westerberg */ 1253a3496855SMika Westerberg chip->enable_dma = drv_data->master_info->enable_dma; 1254ca632f55SGrant Likely } 1255ca632f55SGrant Likely 1256a0d2642eSMika Westerberg chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); 1257a0d2642eSMika Westerberg chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) 1258a0d2642eSMika Westerberg | SSITF_TxHiThresh(tx_hi_thres); 1259a0d2642eSMika Westerberg 1260ca632f55SGrant Likely /* set dma burst and threshold outside of chip_info path so that if 1261ca632f55SGrant Likely * chip_info goes away after setting chip->enable_dma, the 1262ca632f55SGrant Likely * burst and threshold can still respond to changes in bits_per_word */ 1263ca632f55SGrant Likely if (chip->enable_dma) { 1264ca632f55SGrant Likely /* set up legal burst and threshold for dma */ 1265cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, 1266cd7bed00SMika Westerberg spi->bits_per_word, 1267ca632f55SGrant Likely &chip->dma_burst_size, 1268ca632f55SGrant Likely &chip->dma_threshold)) { 1269f6bd03a7SJarkko Nikula dev_warn(&spi->dev, 1270f6bd03a7SJarkko Nikula "in setup: DMA burst size reduced to match bits_per_word\n"); 1271ca632f55SGrant Likely } 1272ca632f55SGrant Likely } 1273ca632f55SGrant Likely 1274e5262d05SWeike Chen switch (drv_data->ssp_type) { 1275e5262d05SWeike Chen case QUARK_X1000_SSP: 1276e5262d05SWeike Chen chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) 1277e5262d05SWeike Chen & QUARK_X1000_SSCR1_RFT) 1278e5262d05SWeike Chen | (QUARK_X1000_SSCR1_TxTresh(tx_thres) 1279e5262d05SWeike Chen & QUARK_X1000_SSCR1_TFT); 1280e5262d05SWeike Chen break; 1281e5262d05SWeike Chen default: 1282e5262d05SWeike Chen chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | 1283e5262d05SWeike Chen (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); 1284e5262d05SWeike Chen break; 1285e5262d05SWeike Chen } 1286e5262d05SWeike Chen 1287ca632f55SGrant Likely chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); 1288ca632f55SGrant Likely chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) 1289ca632f55SGrant Likely | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); 1290ca632f55SGrant Likely 1291b833172fSMika Westerberg if (spi->mode & SPI_LOOP) 1292b833172fSMika Westerberg chip->cr1 |= SSCR1_LBM; 1293b833172fSMika Westerberg 1294ca632f55SGrant Likely if (spi->bits_per_word <= 8) { 1295ca632f55SGrant Likely chip->n_bytes = 1; 1296ca632f55SGrant Likely chip->read = u8_reader; 1297ca632f55SGrant Likely chip->write = u8_writer; 1298ca632f55SGrant Likely } else if (spi->bits_per_word <= 16) { 1299ca632f55SGrant Likely chip->n_bytes = 2; 1300ca632f55SGrant Likely chip->read = u16_reader; 1301ca632f55SGrant Likely chip->write = u16_writer; 1302ca632f55SGrant Likely } else if (spi->bits_per_word <= 32) { 1303ca632f55SGrant Likely chip->n_bytes = 4; 1304ca632f55SGrant Likely chip->read = u32_reader; 1305ca632f55SGrant Likely chip->write = u32_writer; 1306ca632f55SGrant Likely } 1307ca632f55SGrant Likely 1308ca632f55SGrant Likely spi_set_ctldata(spi, chip); 1309ca632f55SGrant Likely 1310ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 1311ca632f55SGrant Likely return 0; 1312ca632f55SGrant Likely 1313ca632f55SGrant Likely return setup_cs(spi, chip, chip_info); 1314ca632f55SGrant Likely } 1315ca632f55SGrant Likely 1316ca632f55SGrant Likely static void cleanup(struct spi_device *spi) 1317ca632f55SGrant Likely { 1318ca632f55SGrant Likely struct chip_data *chip = spi_get_ctldata(spi); 1319ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1320ca632f55SGrant Likely 1321ca632f55SGrant Likely if (!chip) 1322ca632f55SGrant Likely return; 1323ca632f55SGrant Likely 1324ca632f55SGrant Likely if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs)) 1325ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1326ca632f55SGrant Likely 1327ca632f55SGrant Likely kfree(chip); 1328ca632f55SGrant Likely } 1329ca632f55SGrant Likely 13300db64215SJarkko Nikula #ifdef CONFIG_PCI 1331a3496855SMika Westerberg #ifdef CONFIG_ACPI 133203fbf488SJarkko Nikula 13338422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { 133403fbf488SJarkko Nikula { "INT33C0", LPSS_LPT_SSP }, 133503fbf488SJarkko Nikula { "INT33C1", LPSS_LPT_SSP }, 133603fbf488SJarkko Nikula { "INT3430", LPSS_LPT_SSP }, 133703fbf488SJarkko Nikula { "INT3431", LPSS_LPT_SSP }, 133803fbf488SJarkko Nikula { "80860F0E", LPSS_BYT_SSP }, 133903fbf488SJarkko Nikula { "8086228E", LPSS_BYT_SSP }, 134003fbf488SJarkko Nikula { }, 134103fbf488SJarkko Nikula }; 134203fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); 134303fbf488SJarkko Nikula 13440db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev) 13450db64215SJarkko Nikula { 13460db64215SJarkko Nikula unsigned int devid; 13470db64215SJarkko Nikula int port_id = -1; 13480db64215SJarkko Nikula 13490db64215SJarkko Nikula if (adev && adev->pnp.unique_id && 13500db64215SJarkko Nikula !kstrtouint(adev->pnp.unique_id, 0, &devid)) 13510db64215SJarkko Nikula port_id = devid; 13520db64215SJarkko Nikula return port_id; 13530db64215SJarkko Nikula } 13540db64215SJarkko Nikula #else /* !CONFIG_ACPI */ 13550db64215SJarkko Nikula static int pxa2xx_spi_get_port_id(struct acpi_device *adev) 13560db64215SJarkko Nikula { 13570db64215SJarkko Nikula return -1; 13580db64215SJarkko Nikula } 13590db64215SJarkko Nikula #endif 13600db64215SJarkko Nikula 136134cadd9cSJarkko Nikula /* 136234cadd9cSJarkko Nikula * PCI IDs of compound devices that integrate both host controller and private 136334cadd9cSJarkko Nikula * integrated DMA engine. Please note these are not used in module 136434cadd9cSJarkko Nikula * autoloading and probing in this module but matching the LPSS SSP type. 136534cadd9cSJarkko Nikula */ 136634cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { 136734cadd9cSJarkko Nikula /* SPT-LP */ 136834cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, 136934cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, 137034cadd9cSJarkko Nikula /* SPT-H */ 137134cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, 137234cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, 1373b7c08cf8SJarkko Nikula /* BXT */ 1374b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP }, 1375b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP }, 1376b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP }, 1377b7c08cf8SJarkko Nikula /* APL */ 1378b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP }, 1379b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP }, 1380b7c08cf8SJarkko Nikula { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP }, 138194e5c23dSAxel Lin { }, 138234cadd9cSJarkko Nikula }; 138334cadd9cSJarkko Nikula 138434cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) 138534cadd9cSJarkko Nikula { 138634cadd9cSJarkko Nikula struct device *dev = param; 138734cadd9cSJarkko Nikula 138834cadd9cSJarkko Nikula if (dev != chan->device->dev->parent) 138934cadd9cSJarkko Nikula return false; 139034cadd9cSJarkko Nikula 139134cadd9cSJarkko Nikula return true; 139234cadd9cSJarkko Nikula } 139334cadd9cSJarkko Nikula 1394a3496855SMika Westerberg static struct pxa2xx_spi_master * 13950db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev) 1396a3496855SMika Westerberg { 1397a3496855SMika Westerberg struct pxa2xx_spi_master *pdata; 1398a3496855SMika Westerberg struct acpi_device *adev; 1399a3496855SMika Westerberg struct ssp_device *ssp; 1400a3496855SMika Westerberg struct resource *res; 140134cadd9cSJarkko Nikula const struct acpi_device_id *adev_id = NULL; 140234cadd9cSJarkko Nikula const struct pci_device_id *pcidev_id = NULL; 14033b8b6d05SJarkko Nikula int type; 1404a3496855SMika Westerberg 1405b9f6940aSJarkko Nikula adev = ACPI_COMPANION(&pdev->dev); 1406a3496855SMika Westerberg 140734cadd9cSJarkko Nikula if (dev_is_pci(pdev->dev.parent)) 140834cadd9cSJarkko Nikula pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, 140934cadd9cSJarkko Nikula to_pci_dev(pdev->dev.parent)); 14100db64215SJarkko Nikula else if (adev) 141134cadd9cSJarkko Nikula adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table, 141234cadd9cSJarkko Nikula &pdev->dev); 14130db64215SJarkko Nikula else 14140db64215SJarkko Nikula return NULL; 141534cadd9cSJarkko Nikula 141634cadd9cSJarkko Nikula if (adev_id) 141734cadd9cSJarkko Nikula type = (int)adev_id->driver_data; 141834cadd9cSJarkko Nikula else if (pcidev_id) 141934cadd9cSJarkko Nikula type = (int)pcidev_id->driver_data; 142003fbf488SJarkko Nikula else 142103fbf488SJarkko Nikula return NULL; 142203fbf488SJarkko Nikula 1423cc0ee987SMika Westerberg pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 14249deae459SJingoo Han if (!pdata) 1425a3496855SMika Westerberg return NULL; 1426a3496855SMika Westerberg 1427a3496855SMika Westerberg res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1428a3496855SMika Westerberg if (!res) 1429a3496855SMika Westerberg return NULL; 1430a3496855SMika Westerberg 1431a3496855SMika Westerberg ssp = &pdata->ssp; 1432a3496855SMika Westerberg 1433a3496855SMika Westerberg ssp->phys_base = res->start; 1434cbfd6a21SSachin Kamat ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); 1435cbfd6a21SSachin Kamat if (IS_ERR(ssp->mmio_base)) 14366dc81f6fSMika Westerberg return NULL; 1437a3496855SMika Westerberg 143834cadd9cSJarkko Nikula if (pcidev_id) { 143934cadd9cSJarkko Nikula pdata->tx_param = pdev->dev.parent; 144034cadd9cSJarkko Nikula pdata->rx_param = pdev->dev.parent; 144134cadd9cSJarkko Nikula pdata->dma_filter = pxa2xx_spi_idma_filter; 144234cadd9cSJarkko Nikula } 144334cadd9cSJarkko Nikula 1444a3496855SMika Westerberg ssp->clk = devm_clk_get(&pdev->dev, NULL); 1445a3496855SMika Westerberg ssp->irq = platform_get_irq(pdev, 0); 144603fbf488SJarkko Nikula ssp->type = type; 1447a3496855SMika Westerberg ssp->pdev = pdev; 14480db64215SJarkko Nikula ssp->port_id = pxa2xx_spi_get_port_id(adev); 1449a3496855SMika Westerberg 1450a3496855SMika Westerberg pdata->num_chipselect = 1; 1451cddb339bSMika Westerberg pdata->enable_dma = true; 1452a3496855SMika Westerberg 1453a3496855SMika Westerberg return pdata; 1454a3496855SMika Westerberg } 1455a3496855SMika Westerberg 14560db64215SJarkko Nikula #else /* !CONFIG_PCI */ 1457a3496855SMika Westerberg static inline struct pxa2xx_spi_master * 14580db64215SJarkko Nikula pxa2xx_spi_init_pdata(struct platform_device *pdev) 1459a3496855SMika Westerberg { 1460a3496855SMika Westerberg return NULL; 1461a3496855SMika Westerberg } 1462a3496855SMika Westerberg #endif 1463a3496855SMika Westerberg 14640c27d9cfSMika Westerberg static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs) 14650c27d9cfSMika Westerberg { 14660c27d9cfSMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 14670c27d9cfSMika Westerberg 14680c27d9cfSMika Westerberg if (has_acpi_companion(&drv_data->pdev->dev)) { 14690c27d9cfSMika Westerberg switch (drv_data->ssp_type) { 14700c27d9cfSMika Westerberg /* 14710c27d9cfSMika Westerberg * For Atoms the ACPI DeviceSelection used by the Windows 14720c27d9cfSMika Westerberg * driver starts from 1 instead of 0 so translate it here 14730c27d9cfSMika Westerberg * to match what Linux expects. 14740c27d9cfSMika Westerberg */ 14750c27d9cfSMika Westerberg case LPSS_BYT_SSP: 14760c27d9cfSMika Westerberg return cs - 1; 14770c27d9cfSMika Westerberg 14780c27d9cfSMika Westerberg default: 14790c27d9cfSMika Westerberg break; 14800c27d9cfSMika Westerberg } 14810c27d9cfSMika Westerberg } 14820c27d9cfSMika Westerberg 14830c27d9cfSMika Westerberg return cs; 14840c27d9cfSMika Westerberg } 14850c27d9cfSMika Westerberg 1486fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev) 1487ca632f55SGrant Likely { 1488ca632f55SGrant Likely struct device *dev = &pdev->dev; 1489ca632f55SGrant Likely struct pxa2xx_spi_master *platform_info; 1490ca632f55SGrant Likely struct spi_master *master; 1491ca632f55SGrant Likely struct driver_data *drv_data; 1492ca632f55SGrant Likely struct ssp_device *ssp; 14938b136baaSJarkko Nikula const struct lpss_config *config; 1494ca632f55SGrant Likely int status; 1495c039dd27SJarkko Nikula u32 tmp; 1496ca632f55SGrant Likely 1497851bacf5SMika Westerberg platform_info = dev_get_platdata(dev); 1498851bacf5SMika Westerberg if (!platform_info) { 14990db64215SJarkko Nikula platform_info = pxa2xx_spi_init_pdata(pdev); 1500a3496855SMika Westerberg if (!platform_info) { 1501851bacf5SMika Westerberg dev_err(&pdev->dev, "missing platform data\n"); 1502851bacf5SMika Westerberg return -ENODEV; 1503851bacf5SMika Westerberg } 1504a3496855SMika Westerberg } 1505ca632f55SGrant Likely 1506ca632f55SGrant Likely ssp = pxa_ssp_request(pdev->id, pdev->name); 1507851bacf5SMika Westerberg if (!ssp) 1508851bacf5SMika Westerberg ssp = &platform_info->ssp; 1509851bacf5SMika Westerberg 1510851bacf5SMika Westerberg if (!ssp->mmio_base) { 1511851bacf5SMika Westerberg dev_err(&pdev->dev, "failed to get ssp\n"); 1512ca632f55SGrant Likely return -ENODEV; 1513ca632f55SGrant Likely } 1514ca632f55SGrant Likely 1515757fe8d5SJarkko Nikula master = spi_alloc_master(dev, sizeof(struct driver_data)); 1516ca632f55SGrant Likely if (!master) { 1517ca632f55SGrant Likely dev_err(&pdev->dev, "cannot alloc spi_master\n"); 1518ca632f55SGrant Likely pxa_ssp_free(ssp); 1519ca632f55SGrant Likely return -ENOMEM; 1520ca632f55SGrant Likely } 1521ca632f55SGrant Likely drv_data = spi_master_get_devdata(master); 1522ca632f55SGrant Likely drv_data->master = master; 1523ca632f55SGrant Likely drv_data->master_info = platform_info; 1524ca632f55SGrant Likely drv_data->pdev = pdev; 1525ca632f55SGrant Likely drv_data->ssp = ssp; 1526ca632f55SGrant Likely 1527ca632f55SGrant Likely master->dev.parent = &pdev->dev; 1528ca632f55SGrant Likely master->dev.of_node = pdev->dev.of_node; 1529ca632f55SGrant Likely /* the spi->mode bits understood by this driver: */ 1530b833172fSMika Westerberg master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; 1531ca632f55SGrant Likely 1532851bacf5SMika Westerberg master->bus_num = ssp->port_id; 1533ca632f55SGrant Likely master->dma_alignment = DMA_ALIGNMENT; 1534ca632f55SGrant Likely master->cleanup = cleanup; 1535ca632f55SGrant Likely master->setup = setup; 15367f86bde9SMika Westerberg master->transfer_one_message = pxa2xx_spi_transfer_one_message; 15377d94a505SMika Westerberg master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; 15380c27d9cfSMika Westerberg master->fw_translate_cs = pxa2xx_spi_fw_translate_cs; 15397dd62787SMark Brown master->auto_runtime_pm = true; 1540ca632f55SGrant Likely 1541ca632f55SGrant Likely drv_data->ssp_type = ssp->type; 1542ca632f55SGrant Likely 1543ca632f55SGrant Likely drv_data->ioaddr = ssp->mmio_base; 1544ca632f55SGrant Likely drv_data->ssdr_physical = ssp->phys_base + SSDR; 1545ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 1546e5262d05SWeike Chen switch (drv_data->ssp_type) { 1547e5262d05SWeike Chen case QUARK_X1000_SSP: 1548e5262d05SWeike Chen master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1549e5262d05SWeike Chen break; 1550e5262d05SWeike Chen default: 155124778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); 1552e5262d05SWeike Chen break; 1553e5262d05SWeike Chen } 1554e5262d05SWeike Chen 1555ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; 1556ca632f55SGrant Likely drv_data->dma_cr1 = 0; 1557ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR; 1558ca632f55SGrant Likely drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; 1559ca632f55SGrant Likely } else { 156024778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1561ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; 15625928808eSMika Westerberg drv_data->dma_cr1 = DEFAULT_DMA_CR1; 1563ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR | SSSR_TINT; 1564ca632f55SGrant Likely drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; 1565ca632f55SGrant Likely } 1566ca632f55SGrant Likely 1567ca632f55SGrant Likely status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), 1568ca632f55SGrant Likely drv_data); 1569ca632f55SGrant Likely if (status < 0) { 1570ca632f55SGrant Likely dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); 1571ca632f55SGrant Likely goto out_error_master_alloc; 1572ca632f55SGrant Likely } 1573ca632f55SGrant Likely 1574ca632f55SGrant Likely /* Setup DMA if requested */ 1575ca632f55SGrant Likely if (platform_info->enable_dma) { 1576cd7bed00SMika Westerberg status = pxa2xx_spi_dma_setup(drv_data); 1577cd7bed00SMika Westerberg if (status) { 1578cddb339bSMika Westerberg dev_dbg(dev, "no DMA channels available, using PIO\n"); 1579cd7bed00SMika Westerberg platform_info->enable_dma = false; 1580ca632f55SGrant Likely } 1581ca632f55SGrant Likely } 1582ca632f55SGrant Likely 1583ca632f55SGrant Likely /* Enable SOC clock */ 15843343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 15853343b7a6SMika Westerberg 15860eca7cf2SJarkko Nikula master->max_speed_hz = clk_get_rate(ssp->clk); 1587ca632f55SGrant Likely 1588ca632f55SGrant Likely /* Load default SSP configuration */ 1589c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 1590e5262d05SWeike Chen switch (drv_data->ssp_type) { 1591e5262d05SWeike Chen case QUARK_X1000_SSP: 1592c039dd27SJarkko Nikula tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) 1593c039dd27SJarkko Nikula | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); 1594c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1595e5262d05SWeike Chen 1596e5262d05SWeike Chen /* using the Motorola SPI protocol and use 8 bit frame */ 1597c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 1598c039dd27SJarkko Nikula QUARK_X1000_SSCR0_Motorola 1599c039dd27SJarkko Nikula | QUARK_X1000_SSCR0_DataSize(8)); 1600e5262d05SWeike Chen break; 1601e5262d05SWeike Chen default: 1602c039dd27SJarkko Nikula tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | 1603c039dd27SJarkko Nikula SSCR1_TxTresh(TX_THRESH_DFLT); 1604c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1605c039dd27SJarkko Nikula tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); 1606c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, tmp); 1607e5262d05SWeike Chen break; 1608e5262d05SWeike Chen } 1609e5262d05SWeike Chen 1610ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1611c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 1612e5262d05SWeike Chen 1613e5262d05SWeike Chen if (!is_quark_x1000_ssp(drv_data)) 1614c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSPSP, 0); 1615ca632f55SGrant Likely 16168b136baaSJarkko Nikula if (is_lpss_ssp(drv_data)) { 16178b136baaSJarkko Nikula lpss_ssp_setup(drv_data); 16188b136baaSJarkko Nikula config = lpss_get_config(drv_data); 16198b136baaSJarkko Nikula if (config->reg_capabilities >= 0) { 16208b136baaSJarkko Nikula tmp = __lpss_ssp_read_priv(drv_data, 16218b136baaSJarkko Nikula config->reg_capabilities); 16228b136baaSJarkko Nikula tmp &= LPSS_CAPS_CS_EN_MASK; 16238b136baaSJarkko Nikula tmp >>= LPSS_CAPS_CS_EN_SHIFT; 16248b136baaSJarkko Nikula platform_info->num_chipselect = ffz(tmp); 16258b136baaSJarkko Nikula } 16268b136baaSJarkko Nikula } 16278b136baaSJarkko Nikula master->num_chipselect = platform_info->num_chipselect; 16288b136baaSJarkko Nikula 16297f86bde9SMika Westerberg tasklet_init(&drv_data->pump_transfers, pump_transfers, 16307f86bde9SMika Westerberg (unsigned long)drv_data); 1631ca632f55SGrant Likely 1632836d1a22SAntonio Ospite pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 1633836d1a22SAntonio Ospite pm_runtime_use_autosuspend(&pdev->dev); 1634836d1a22SAntonio Ospite pm_runtime_set_active(&pdev->dev); 1635836d1a22SAntonio Ospite pm_runtime_enable(&pdev->dev); 1636836d1a22SAntonio Ospite 1637ca632f55SGrant Likely /* Register with the SPI framework */ 1638ca632f55SGrant Likely platform_set_drvdata(pdev, drv_data); 1639a807fcd0SJingoo Han status = devm_spi_register_master(&pdev->dev, master); 1640ca632f55SGrant Likely if (status != 0) { 1641ca632f55SGrant Likely dev_err(&pdev->dev, "problem registering spi master\n"); 16427f86bde9SMika Westerberg goto out_error_clock_enabled; 1643ca632f55SGrant Likely } 1644ca632f55SGrant Likely 1645ca632f55SGrant Likely return status; 1646ca632f55SGrant Likely 1647ca632f55SGrant Likely out_error_clock_enabled: 16483343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1649cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1650ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1651ca632f55SGrant Likely 1652ca632f55SGrant Likely out_error_master_alloc: 1653ca632f55SGrant Likely spi_master_put(master); 1654ca632f55SGrant Likely pxa_ssp_free(ssp); 1655ca632f55SGrant Likely return status; 1656ca632f55SGrant Likely } 1657ca632f55SGrant Likely 1658ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev) 1659ca632f55SGrant Likely { 1660ca632f55SGrant Likely struct driver_data *drv_data = platform_get_drvdata(pdev); 1661ca632f55SGrant Likely struct ssp_device *ssp; 1662ca632f55SGrant Likely 1663ca632f55SGrant Likely if (!drv_data) 1664ca632f55SGrant Likely return 0; 1665ca632f55SGrant Likely ssp = drv_data->ssp; 1666ca632f55SGrant Likely 16677d94a505SMika Westerberg pm_runtime_get_sync(&pdev->dev); 16687d94a505SMika Westerberg 1669ca632f55SGrant Likely /* Disable the SSP at the peripheral and SOC level */ 1670c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 16713343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1672ca632f55SGrant Likely 1673ca632f55SGrant Likely /* Release DMA */ 1674cd7bed00SMika Westerberg if (drv_data->master_info->enable_dma) 1675cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1676ca632f55SGrant Likely 16777d94a505SMika Westerberg pm_runtime_put_noidle(&pdev->dev); 16787d94a505SMika Westerberg pm_runtime_disable(&pdev->dev); 16797d94a505SMika Westerberg 1680ca632f55SGrant Likely /* Release IRQ */ 1681ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1682ca632f55SGrant Likely 1683ca632f55SGrant Likely /* Release SSP */ 1684ca632f55SGrant Likely pxa_ssp_free(ssp); 1685ca632f55SGrant Likely 1686ca632f55SGrant Likely return 0; 1687ca632f55SGrant Likely } 1688ca632f55SGrant Likely 1689ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev) 1690ca632f55SGrant Likely { 1691ca632f55SGrant Likely int status = 0; 1692ca632f55SGrant Likely 1693ca632f55SGrant Likely if ((status = pxa2xx_spi_remove(pdev)) != 0) 1694ca632f55SGrant Likely dev_err(&pdev->dev, "shutdown failed with %d\n", status); 1695ca632f55SGrant Likely } 1696ca632f55SGrant Likely 1697382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP 1698ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev) 1699ca632f55SGrant Likely { 1700ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1701ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1702ca632f55SGrant Likely int status = 0; 1703ca632f55SGrant Likely 17047f86bde9SMika Westerberg status = spi_master_suspend(drv_data->master); 1705ca632f55SGrant Likely if (status != 0) 1706ca632f55SGrant Likely return status; 1707c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 17082b9375b9SDmitry Eremin-Solenikov 17092b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 17103343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1711ca632f55SGrant Likely 1712ca632f55SGrant Likely return 0; 1713ca632f55SGrant Likely } 1714ca632f55SGrant Likely 1715ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev) 1716ca632f55SGrant Likely { 1717ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1718ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1719ca632f55SGrant Likely int status = 0; 1720ca632f55SGrant Likely 1721ca632f55SGrant Likely /* Enable the SSP clock */ 17222b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 17233343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 1724ca632f55SGrant Likely 1725c50325f7SChew, Chiau Ee /* Restore LPSS private register bits */ 172648421adfSJarkko Nikula if (is_lpss_ssp(drv_data)) 1727c50325f7SChew, Chiau Ee lpss_ssp_setup(drv_data); 1728c50325f7SChew, Chiau Ee 1729ca632f55SGrant Likely /* Start the queue running */ 17307f86bde9SMika Westerberg status = spi_master_resume(drv_data->master); 1731ca632f55SGrant Likely if (status != 0) { 1732ca632f55SGrant Likely dev_err(dev, "problem starting queue (%d)\n", status); 1733ca632f55SGrant Likely return status; 1734ca632f55SGrant Likely } 1735ca632f55SGrant Likely 1736ca632f55SGrant Likely return 0; 1737ca632f55SGrant Likely } 17387d94a505SMika Westerberg #endif 17397d94a505SMika Westerberg 1740ec833050SRafael J. Wysocki #ifdef CONFIG_PM 17417d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev) 17427d94a505SMika Westerberg { 17437d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 17447d94a505SMika Westerberg 17457d94a505SMika Westerberg clk_disable_unprepare(drv_data->ssp->clk); 17467d94a505SMika Westerberg return 0; 17477d94a505SMika Westerberg } 17487d94a505SMika Westerberg 17497d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev) 17507d94a505SMika Westerberg { 17517d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 17527d94a505SMika Westerberg 17537d94a505SMika Westerberg clk_prepare_enable(drv_data->ssp->clk); 17547d94a505SMika Westerberg return 0; 17557d94a505SMika Westerberg } 17567d94a505SMika Westerberg #endif 1757ca632f55SGrant Likely 1758ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = { 17597d94a505SMika Westerberg SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) 17607d94a505SMika Westerberg SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, 17617d94a505SMika Westerberg pxa2xx_spi_runtime_resume, NULL) 1762ca632f55SGrant Likely }; 1763ca632f55SGrant Likely 1764ca632f55SGrant Likely static struct platform_driver driver = { 1765ca632f55SGrant Likely .driver = { 1766ca632f55SGrant Likely .name = "pxa2xx-spi", 1767ca632f55SGrant Likely .pm = &pxa2xx_spi_pm_ops, 1768a3496855SMika Westerberg .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), 1769ca632f55SGrant Likely }, 1770ca632f55SGrant Likely .probe = pxa2xx_spi_probe, 1771ca632f55SGrant Likely .remove = pxa2xx_spi_remove, 1772ca632f55SGrant Likely .shutdown = pxa2xx_spi_shutdown, 1773ca632f55SGrant Likely }; 1774ca632f55SGrant Likely 1775ca632f55SGrant Likely static int __init pxa2xx_spi_init(void) 1776ca632f55SGrant Likely { 1777ca632f55SGrant Likely return platform_driver_register(&driver); 1778ca632f55SGrant Likely } 1779ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init); 1780ca632f55SGrant Likely 1781ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void) 1782ca632f55SGrant Likely { 1783ca632f55SGrant Likely platform_driver_unregister(&driver); 1784ca632f55SGrant Likely } 1785ca632f55SGrant Likely module_exit(pxa2xx_spi_exit); 1786