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 16ca632f55SGrant Likely #include <linux/init.h> 17ca632f55SGrant Likely #include <linux/module.h> 18ca632f55SGrant Likely #include <linux/device.h> 19ca632f55SGrant Likely #include <linux/ioport.h> 20ca632f55SGrant Likely #include <linux/errno.h> 21cbfd6a21SSachin Kamat #include <linux/err.h> 22ca632f55SGrant Likely #include <linux/interrupt.h> 239df461ecSAndy Shevchenko #include <linux/kernel.h> 2434cadd9cSJarkko Nikula #include <linux/pci.h> 25ca632f55SGrant Likely #include <linux/platform_device.h> 26ca632f55SGrant Likely #include <linux/spi/pxa2xx_spi.h> 27ca632f55SGrant Likely #include <linux/spi/spi.h> 28ca632f55SGrant Likely #include <linux/delay.h> 29ca632f55SGrant Likely #include <linux/gpio.h> 30ca632f55SGrant Likely #include <linux/slab.h> 313343b7a6SMika Westerberg #include <linux/clk.h> 327d94a505SMika Westerberg #include <linux/pm_runtime.h> 33a3496855SMika Westerberg #include <linux/acpi.h> 34ca632f55SGrant Likely 35cd7bed00SMika Westerberg #include "spi-pxa2xx.h" 36ca632f55SGrant Likely 37ca632f55SGrant Likely MODULE_AUTHOR("Stephen Street"); 38ca632f55SGrant Likely MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); 39ca632f55SGrant Likely MODULE_LICENSE("GPL"); 40ca632f55SGrant Likely MODULE_ALIAS("platform:pxa2xx-spi"); 41ca632f55SGrant Likely 42ca632f55SGrant Likely #define TIMOUT_DFLT 1000 43ca632f55SGrant Likely 44ca632f55SGrant Likely /* 45ca632f55SGrant Likely * for testing SSCR1 changes that require SSP restart, basically 46ca632f55SGrant Likely * everything except the service and interrupt enables, the pxa270 developer 47ca632f55SGrant Likely * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this 48ca632f55SGrant Likely * list, but the PXA255 dev man says all bits without really meaning the 49ca632f55SGrant Likely * service and interrupt enables 50ca632f55SGrant Likely */ 51ca632f55SGrant Likely #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ 52ca632f55SGrant Likely | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ 53ca632f55SGrant Likely | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ 54ca632f55SGrant Likely | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ 55ca632f55SGrant Likely | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ 56ca632f55SGrant Likely | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 57ca632f55SGrant Likely 58e5262d05SWeike Chen #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ 59e5262d05SWeike Chen | QUARK_X1000_SSCR1_EFWR \ 60e5262d05SWeike Chen | QUARK_X1000_SSCR1_RFT \ 61e5262d05SWeike Chen | QUARK_X1000_SSCR1_TFT \ 62e5262d05SWeike Chen | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) 63e5262d05SWeike Chen 641de70612SMika Westerberg #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) 65a0d2642eSMika Westerberg #define SPI_CS_CONTROL_SW_MODE BIT(0) 66a0d2642eSMika Westerberg #define SPI_CS_CONTROL_CS_HIGH BIT(1) 67a0d2642eSMika Westerberg 68dccf7369SJarkko Nikula struct lpss_config { 69dccf7369SJarkko Nikula /* LPSS offset from drv_data->ioaddr */ 70dccf7369SJarkko Nikula unsigned offset; 71dccf7369SJarkko Nikula /* Register offsets from drv_data->lpss_base or -1 */ 72dccf7369SJarkko Nikula int reg_general; 73dccf7369SJarkko Nikula int reg_ssp; 74dccf7369SJarkko Nikula int reg_cs_ctrl; 75dccf7369SJarkko Nikula /* FIFO thresholds */ 76dccf7369SJarkko Nikula u32 rx_threshold; 77dccf7369SJarkko Nikula u32 tx_threshold_lo; 78dccf7369SJarkko Nikula u32 tx_threshold_hi; 79dccf7369SJarkko Nikula }; 80dccf7369SJarkko Nikula 81dccf7369SJarkko Nikula /* Keep these sorted with enum pxa_ssp_type */ 82dccf7369SJarkko Nikula static const struct lpss_config lpss_platforms[] = { 83dccf7369SJarkko Nikula { /* LPSS_LPT_SSP */ 84dccf7369SJarkko Nikula .offset = 0x800, 85dccf7369SJarkko Nikula .reg_general = 0x08, 86dccf7369SJarkko Nikula .reg_ssp = 0x0c, 87dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 88dccf7369SJarkko Nikula .rx_threshold = 64, 89dccf7369SJarkko Nikula .tx_threshold_lo = 160, 90dccf7369SJarkko Nikula .tx_threshold_hi = 224, 91dccf7369SJarkko Nikula }, 92dccf7369SJarkko Nikula { /* LPSS_BYT_SSP */ 93dccf7369SJarkko Nikula .offset = 0x400, 94dccf7369SJarkko Nikula .reg_general = 0x08, 95dccf7369SJarkko Nikula .reg_ssp = 0x0c, 96dccf7369SJarkko Nikula .reg_cs_ctrl = 0x18, 97dccf7369SJarkko Nikula .rx_threshold = 64, 98dccf7369SJarkko Nikula .tx_threshold_lo = 160, 99dccf7369SJarkko Nikula .tx_threshold_hi = 224, 100dccf7369SJarkko Nikula }, 10134cadd9cSJarkko Nikula { /* LPSS_SPT_SSP */ 10234cadd9cSJarkko Nikula .offset = 0x200, 10334cadd9cSJarkko Nikula .reg_general = -1, 10434cadd9cSJarkko Nikula .reg_ssp = 0x20, 10534cadd9cSJarkko Nikula .reg_cs_ctrl = 0x24, 10634cadd9cSJarkko Nikula .rx_threshold = 1, 10734cadd9cSJarkko Nikula .tx_threshold_lo = 32, 10834cadd9cSJarkko Nikula .tx_threshold_hi = 56, 10934cadd9cSJarkko Nikula }, 110dccf7369SJarkko Nikula }; 111dccf7369SJarkko Nikula 112dccf7369SJarkko Nikula static inline const struct lpss_config 113dccf7369SJarkko Nikula *lpss_get_config(const struct driver_data *drv_data) 114dccf7369SJarkko Nikula { 115dccf7369SJarkko Nikula return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; 116dccf7369SJarkko Nikula } 117dccf7369SJarkko Nikula 118a0d2642eSMika Westerberg static bool is_lpss_ssp(const struct driver_data *drv_data) 119a0d2642eSMika Westerberg { 12003fbf488SJarkko Nikula switch (drv_data->ssp_type) { 12103fbf488SJarkko Nikula case LPSS_LPT_SSP: 12203fbf488SJarkko Nikula case LPSS_BYT_SSP: 12334cadd9cSJarkko Nikula case LPSS_SPT_SSP: 12403fbf488SJarkko Nikula return true; 12503fbf488SJarkko Nikula default: 12603fbf488SJarkko Nikula return false; 12703fbf488SJarkko Nikula } 128a0d2642eSMika Westerberg } 129a0d2642eSMika Westerberg 130e5262d05SWeike Chen static bool is_quark_x1000_ssp(const struct driver_data *drv_data) 131e5262d05SWeike Chen { 132e5262d05SWeike Chen return drv_data->ssp_type == QUARK_X1000_SSP; 133e5262d05SWeike Chen } 134e5262d05SWeike Chen 1354fdb2424SWeike Chen static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) 1364fdb2424SWeike Chen { 1374fdb2424SWeike Chen switch (drv_data->ssp_type) { 138e5262d05SWeike Chen case QUARK_X1000_SSP: 139e5262d05SWeike Chen return QUARK_X1000_SSCR1_CHANGE_MASK; 1404fdb2424SWeike Chen default: 1414fdb2424SWeike Chen return SSCR1_CHANGE_MASK; 1424fdb2424SWeike Chen } 1434fdb2424SWeike Chen } 1444fdb2424SWeike Chen 1454fdb2424SWeike Chen static u32 1464fdb2424SWeike Chen pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) 1474fdb2424SWeike Chen { 1484fdb2424SWeike Chen switch (drv_data->ssp_type) { 149e5262d05SWeike Chen case QUARK_X1000_SSP: 150e5262d05SWeike Chen return RX_THRESH_QUARK_X1000_DFLT; 1514fdb2424SWeike Chen default: 1524fdb2424SWeike Chen return RX_THRESH_DFLT; 1534fdb2424SWeike Chen } 1544fdb2424SWeike Chen } 1554fdb2424SWeike Chen 1564fdb2424SWeike Chen static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) 1574fdb2424SWeike Chen { 1584fdb2424SWeike Chen u32 mask; 1594fdb2424SWeike Chen 1604fdb2424SWeike Chen switch (drv_data->ssp_type) { 161e5262d05SWeike Chen case QUARK_X1000_SSP: 162e5262d05SWeike Chen mask = QUARK_X1000_SSSR_TFL_MASK; 163e5262d05SWeike Chen break; 1644fdb2424SWeike Chen default: 1654fdb2424SWeike Chen mask = SSSR_TFL_MASK; 1664fdb2424SWeike Chen break; 1674fdb2424SWeike Chen } 1684fdb2424SWeike Chen 169c039dd27SJarkko Nikula return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask; 1704fdb2424SWeike Chen } 1714fdb2424SWeike Chen 1724fdb2424SWeike Chen static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, 1734fdb2424SWeike Chen u32 *sccr1_reg) 1744fdb2424SWeike Chen { 1754fdb2424SWeike Chen u32 mask; 1764fdb2424SWeike Chen 1774fdb2424SWeike Chen switch (drv_data->ssp_type) { 178e5262d05SWeike Chen case QUARK_X1000_SSP: 179e5262d05SWeike Chen mask = QUARK_X1000_SSCR1_RFT; 180e5262d05SWeike Chen break; 1814fdb2424SWeike Chen default: 1824fdb2424SWeike Chen mask = SSCR1_RFT; 1834fdb2424SWeike Chen break; 1844fdb2424SWeike Chen } 1854fdb2424SWeike Chen *sccr1_reg &= ~mask; 1864fdb2424SWeike Chen } 1874fdb2424SWeike Chen 1884fdb2424SWeike Chen static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, 1894fdb2424SWeike Chen u32 *sccr1_reg, u32 threshold) 1904fdb2424SWeike Chen { 1914fdb2424SWeike Chen switch (drv_data->ssp_type) { 192e5262d05SWeike Chen case QUARK_X1000_SSP: 193e5262d05SWeike Chen *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); 194e5262d05SWeike Chen break; 1954fdb2424SWeike Chen default: 1964fdb2424SWeike Chen *sccr1_reg |= SSCR1_RxTresh(threshold); 1974fdb2424SWeike Chen break; 1984fdb2424SWeike Chen } 1994fdb2424SWeike Chen } 2004fdb2424SWeike Chen 2014fdb2424SWeike Chen static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, 2024fdb2424SWeike Chen u32 clk_div, u8 bits) 2034fdb2424SWeike Chen { 2044fdb2424SWeike Chen switch (drv_data->ssp_type) { 205e5262d05SWeike Chen case QUARK_X1000_SSP: 206e5262d05SWeike Chen return clk_div 207e5262d05SWeike Chen | QUARK_X1000_SSCR0_Motorola 208e5262d05SWeike Chen | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits) 209e5262d05SWeike Chen | SSCR0_SSE; 2104fdb2424SWeike Chen default: 2114fdb2424SWeike Chen return clk_div 2124fdb2424SWeike Chen | SSCR0_Motorola 2134fdb2424SWeike Chen | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) 2144fdb2424SWeike Chen | SSCR0_SSE 2154fdb2424SWeike Chen | (bits > 16 ? SSCR0_EDSS : 0); 2164fdb2424SWeike Chen } 2174fdb2424SWeike Chen } 2184fdb2424SWeike Chen 219a0d2642eSMika Westerberg /* 220a0d2642eSMika Westerberg * Read and write LPSS SSP private registers. Caller must first check that 221a0d2642eSMika Westerberg * is_lpss_ssp() returns true before these can be called. 222a0d2642eSMika Westerberg */ 223a0d2642eSMika Westerberg static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) 224a0d2642eSMika Westerberg { 225a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 226a0d2642eSMika Westerberg return readl(drv_data->lpss_base + offset); 227a0d2642eSMika Westerberg } 228a0d2642eSMika Westerberg 229a0d2642eSMika Westerberg static void __lpss_ssp_write_priv(struct driver_data *drv_data, 230a0d2642eSMika Westerberg unsigned offset, u32 value) 231a0d2642eSMika Westerberg { 232a0d2642eSMika Westerberg WARN_ON(!drv_data->lpss_base); 233a0d2642eSMika Westerberg writel(value, drv_data->lpss_base + offset); 234a0d2642eSMika Westerberg } 235a0d2642eSMika Westerberg 236a0d2642eSMika Westerberg /* 237a0d2642eSMika Westerberg * lpss_ssp_setup - perform LPSS SSP specific setup 238a0d2642eSMika Westerberg * @drv_data: pointer to the driver private data 239a0d2642eSMika Westerberg * 240a0d2642eSMika Westerberg * Perform LPSS SSP specific setup. This function must be called first if 241a0d2642eSMika Westerberg * one is going to use LPSS SSP private registers. 242a0d2642eSMika Westerberg */ 243a0d2642eSMika Westerberg static void lpss_ssp_setup(struct driver_data *drv_data) 244a0d2642eSMika Westerberg { 245dccf7369SJarkko Nikula const struct lpss_config *config; 246dccf7369SJarkko Nikula u32 value; 247a0d2642eSMika Westerberg 248dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 249dccf7369SJarkko Nikula drv_data->lpss_base = drv_data->ioaddr + config->offset; 250a0d2642eSMika Westerberg 251a0d2642eSMika Westerberg /* Enable software chip select control */ 252a0d2642eSMika Westerberg value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH; 253dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 2540054e28dSMika Westerberg 2550054e28dSMika Westerberg /* Enable multiblock DMA transfers */ 2561de70612SMika Westerberg if (drv_data->master_info->enable_dma) { 257dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); 2581de70612SMika Westerberg 25982ba2c2aSJarkko Nikula if (config->reg_general >= 0) { 26082ba2c2aSJarkko Nikula value = __lpss_ssp_read_priv(drv_data, 26182ba2c2aSJarkko Nikula config->reg_general); 2621de70612SMika Westerberg value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE; 26382ba2c2aSJarkko Nikula __lpss_ssp_write_priv(drv_data, 26482ba2c2aSJarkko Nikula config->reg_general, value); 26582ba2c2aSJarkko Nikula } 2661de70612SMika Westerberg } 267a0d2642eSMika Westerberg } 268a0d2642eSMika Westerberg 269a0d2642eSMika Westerberg static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable) 270a0d2642eSMika Westerberg { 271dccf7369SJarkko Nikula const struct lpss_config *config; 272a0d2642eSMika Westerberg u32 value; 273a0d2642eSMika Westerberg 274dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 275dccf7369SJarkko Nikula 276dccf7369SJarkko Nikula value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); 277a0d2642eSMika Westerberg if (enable) 278a0d2642eSMika Westerberg value &= ~SPI_CS_CONTROL_CS_HIGH; 279a0d2642eSMika Westerberg else 280a0d2642eSMika Westerberg value |= SPI_CS_CONTROL_CS_HIGH; 281dccf7369SJarkko Nikula __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); 282a0d2642eSMika Westerberg } 283a0d2642eSMika Westerberg 284ca632f55SGrant Likely static void cs_assert(struct driver_data *drv_data) 285ca632f55SGrant Likely { 286ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 287ca632f55SGrant Likely 288ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 289c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm); 290ca632f55SGrant Likely return; 291ca632f55SGrant Likely } 292ca632f55SGrant Likely 293ca632f55SGrant Likely if (chip->cs_control) { 294ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_ASSERT); 295ca632f55SGrant Likely return; 296ca632f55SGrant Likely } 297ca632f55SGrant Likely 298a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 299ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); 300a0d2642eSMika Westerberg return; 301a0d2642eSMika Westerberg } 302a0d2642eSMika Westerberg 3037566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 304a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, true); 305ca632f55SGrant Likely } 306ca632f55SGrant Likely 307ca632f55SGrant Likely static void cs_deassert(struct driver_data *drv_data) 308ca632f55SGrant Likely { 309ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 310ca632f55SGrant Likely 311ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 312ca632f55SGrant Likely return; 313ca632f55SGrant Likely 314ca632f55SGrant Likely if (chip->cs_control) { 315ca632f55SGrant Likely chip->cs_control(PXA2XX_CS_DEASSERT); 316ca632f55SGrant Likely return; 317ca632f55SGrant Likely } 318ca632f55SGrant Likely 319a0d2642eSMika Westerberg if (gpio_is_valid(chip->gpio_cs)) { 320ca632f55SGrant Likely gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); 321a0d2642eSMika Westerberg return; 322a0d2642eSMika Westerberg } 323a0d2642eSMika Westerberg 3247566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 325a0d2642eSMika Westerberg lpss_ssp_cs_control(drv_data, false); 326ca632f55SGrant Likely } 327ca632f55SGrant Likely 328cd7bed00SMika Westerberg int pxa2xx_spi_flush(struct driver_data *drv_data) 329ca632f55SGrant Likely { 330ca632f55SGrant Likely unsigned long limit = loops_per_jiffy << 1; 331ca632f55SGrant Likely 332ca632f55SGrant Likely do { 333c039dd27SJarkko Nikula while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 334c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 335c039dd27SJarkko Nikula } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); 336ca632f55SGrant Likely write_SSSR_CS(drv_data, SSSR_ROR); 337ca632f55SGrant Likely 338ca632f55SGrant Likely return limit; 339ca632f55SGrant Likely } 340ca632f55SGrant Likely 341ca632f55SGrant Likely static int null_writer(struct driver_data *drv_data) 342ca632f55SGrant Likely { 343ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 344ca632f55SGrant Likely 3454fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 346ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 347ca632f55SGrant Likely return 0; 348ca632f55SGrant Likely 349c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, 0); 350ca632f55SGrant Likely drv_data->tx += n_bytes; 351ca632f55SGrant Likely 352ca632f55SGrant Likely return 1; 353ca632f55SGrant Likely } 354ca632f55SGrant Likely 355ca632f55SGrant Likely static int null_reader(struct driver_data *drv_data) 356ca632f55SGrant Likely { 357ca632f55SGrant Likely u8 n_bytes = drv_data->n_bytes; 358ca632f55SGrant Likely 359c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 360ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 361c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSDR); 362ca632f55SGrant Likely drv_data->rx += n_bytes; 363ca632f55SGrant Likely } 364ca632f55SGrant Likely 365ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 366ca632f55SGrant Likely } 367ca632f55SGrant Likely 368ca632f55SGrant Likely static int u8_writer(struct driver_data *drv_data) 369ca632f55SGrant Likely { 3704fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 371ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 372ca632f55SGrant Likely return 0; 373ca632f55SGrant Likely 374c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); 375ca632f55SGrant Likely ++drv_data->tx; 376ca632f55SGrant Likely 377ca632f55SGrant Likely return 1; 378ca632f55SGrant Likely } 379ca632f55SGrant Likely 380ca632f55SGrant Likely static int u8_reader(struct driver_data *drv_data) 381ca632f55SGrant Likely { 382c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 383ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 384c039dd27SJarkko Nikula *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 385ca632f55SGrant Likely ++drv_data->rx; 386ca632f55SGrant Likely } 387ca632f55SGrant Likely 388ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 389ca632f55SGrant Likely } 390ca632f55SGrant Likely 391ca632f55SGrant Likely static int u16_writer(struct driver_data *drv_data) 392ca632f55SGrant Likely { 3934fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 394ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 395ca632f55SGrant Likely return 0; 396ca632f55SGrant Likely 397c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); 398ca632f55SGrant Likely drv_data->tx += 2; 399ca632f55SGrant Likely 400ca632f55SGrant Likely return 1; 401ca632f55SGrant Likely } 402ca632f55SGrant Likely 403ca632f55SGrant Likely static int u16_reader(struct driver_data *drv_data) 404ca632f55SGrant Likely { 405c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 406ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 407c039dd27SJarkko Nikula *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 408ca632f55SGrant Likely drv_data->rx += 2; 409ca632f55SGrant Likely } 410ca632f55SGrant Likely 411ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 412ca632f55SGrant Likely } 413ca632f55SGrant Likely 414ca632f55SGrant Likely static int u32_writer(struct driver_data *drv_data) 415ca632f55SGrant Likely { 4164fdb2424SWeike Chen if (pxa2xx_spi_txfifo_full(drv_data) 417ca632f55SGrant Likely || (drv_data->tx == drv_data->tx_end)) 418ca632f55SGrant Likely return 0; 419ca632f55SGrant Likely 420c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); 421ca632f55SGrant Likely drv_data->tx += 4; 422ca632f55SGrant Likely 423ca632f55SGrant Likely return 1; 424ca632f55SGrant Likely } 425ca632f55SGrant Likely 426ca632f55SGrant Likely static int u32_reader(struct driver_data *drv_data) 427ca632f55SGrant Likely { 428c039dd27SJarkko Nikula while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) 429ca632f55SGrant Likely && (drv_data->rx < drv_data->rx_end)) { 430c039dd27SJarkko Nikula *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); 431ca632f55SGrant Likely drv_data->rx += 4; 432ca632f55SGrant Likely } 433ca632f55SGrant Likely 434ca632f55SGrant Likely return drv_data->rx == drv_data->rx_end; 435ca632f55SGrant Likely } 436ca632f55SGrant Likely 437cd7bed00SMika Westerberg void *pxa2xx_spi_next_transfer(struct driver_data *drv_data) 438ca632f55SGrant Likely { 439ca632f55SGrant Likely struct spi_message *msg = drv_data->cur_msg; 440ca632f55SGrant Likely struct spi_transfer *trans = drv_data->cur_transfer; 441ca632f55SGrant Likely 442ca632f55SGrant Likely /* Move to next transfer */ 443ca632f55SGrant Likely if (trans->transfer_list.next != &msg->transfers) { 444ca632f55SGrant Likely drv_data->cur_transfer = 445ca632f55SGrant Likely list_entry(trans->transfer_list.next, 446ca632f55SGrant Likely struct spi_transfer, 447ca632f55SGrant Likely transfer_list); 448ca632f55SGrant Likely return RUNNING_STATE; 449ca632f55SGrant Likely } else 450ca632f55SGrant Likely return DONE_STATE; 451ca632f55SGrant Likely } 452ca632f55SGrant Likely 453ca632f55SGrant Likely /* caller already set message->status; dma and pio irqs are blocked */ 454ca632f55SGrant Likely static void giveback(struct driver_data *drv_data) 455ca632f55SGrant Likely { 456ca632f55SGrant Likely struct spi_transfer* last_transfer; 457ca632f55SGrant Likely struct spi_message *msg; 458ca632f55SGrant Likely 459ca632f55SGrant Likely msg = drv_data->cur_msg; 460ca632f55SGrant Likely drv_data->cur_msg = NULL; 461ca632f55SGrant Likely drv_data->cur_transfer = NULL; 462ca632f55SGrant Likely 46323e2c2aaSAxel Lin last_transfer = list_last_entry(&msg->transfers, struct spi_transfer, 464ca632f55SGrant Likely transfer_list); 465ca632f55SGrant Likely 466ca632f55SGrant Likely /* Delay if requested before any change in chip select */ 467ca632f55SGrant Likely if (last_transfer->delay_usecs) 468ca632f55SGrant Likely udelay(last_transfer->delay_usecs); 469ca632f55SGrant Likely 470ca632f55SGrant Likely /* Drop chip select UNLESS cs_change is true or we are returning 471ca632f55SGrant Likely * a message with an error, or next message is for another chip 472ca632f55SGrant Likely */ 473ca632f55SGrant Likely if (!last_transfer->cs_change) 474ca632f55SGrant Likely cs_deassert(drv_data); 475ca632f55SGrant Likely else { 476ca632f55SGrant Likely struct spi_message *next_msg; 477ca632f55SGrant Likely 478ca632f55SGrant Likely /* Holding of cs was hinted, but we need to make sure 479ca632f55SGrant Likely * the next message is for the same chip. Don't waste 480ca632f55SGrant Likely * time with the following tests unless this was hinted. 481ca632f55SGrant Likely * 482ca632f55SGrant Likely * We cannot postpone this until pump_messages, because 483ca632f55SGrant Likely * after calling msg->complete (below) the driver that 484ca632f55SGrant Likely * sent the current message could be unloaded, which 485ca632f55SGrant Likely * could invalidate the cs_control() callback... 486ca632f55SGrant Likely */ 487ca632f55SGrant Likely 488ca632f55SGrant Likely /* get a pointer to the next message, if any */ 4897f86bde9SMika Westerberg next_msg = spi_get_next_queued_message(drv_data->master); 490ca632f55SGrant Likely 491ca632f55SGrant Likely /* see if the next and current messages point 492ca632f55SGrant Likely * to the same chip 493ca632f55SGrant Likely */ 494ca632f55SGrant Likely if (next_msg && next_msg->spi != msg->spi) 495ca632f55SGrant Likely next_msg = NULL; 496ca632f55SGrant Likely if (!next_msg || msg->state == ERROR_STATE) 497ca632f55SGrant Likely cs_deassert(drv_data); 498ca632f55SGrant Likely } 499ca632f55SGrant Likely 500ca632f55SGrant Likely drv_data->cur_chip = NULL; 501c957e8f0SMika Westerberg spi_finalize_current_message(drv_data->master); 502ca632f55SGrant Likely } 503ca632f55SGrant Likely 504ca632f55SGrant Likely static void reset_sccr1(struct driver_data *drv_data) 505ca632f55SGrant Likely { 506ca632f55SGrant Likely struct chip_data *chip = drv_data->cur_chip; 507ca632f55SGrant Likely u32 sccr1_reg; 508ca632f55SGrant Likely 509c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; 510ca632f55SGrant Likely sccr1_reg &= ~SSCR1_RFT; 511ca632f55SGrant Likely sccr1_reg |= chip->threshold; 512c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 513ca632f55SGrant Likely } 514ca632f55SGrant Likely 515ca632f55SGrant Likely static void int_error_stop(struct driver_data *drv_data, const char* msg) 516ca632f55SGrant Likely { 517ca632f55SGrant Likely /* Stop and reset SSP */ 518ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 519ca632f55SGrant Likely reset_sccr1(drv_data); 520ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 521c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 522cd7bed00SMika Westerberg pxa2xx_spi_flush(drv_data); 523c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 524c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 525ca632f55SGrant Likely 526ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "%s\n", msg); 527ca632f55SGrant Likely 528ca632f55SGrant Likely drv_data->cur_msg->state = ERROR_STATE; 529ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 530ca632f55SGrant Likely } 531ca632f55SGrant Likely 532ca632f55SGrant Likely static void int_transfer_complete(struct driver_data *drv_data) 533ca632f55SGrant Likely { 534ca632f55SGrant Likely /* Stop SSP */ 535ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 536ca632f55SGrant Likely reset_sccr1(drv_data); 537ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 538c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 539ca632f55SGrant Likely 540ca632f55SGrant Likely /* Update total byte transferred return count actual bytes read */ 541ca632f55SGrant Likely drv_data->cur_msg->actual_length += drv_data->len - 542ca632f55SGrant Likely (drv_data->rx_end - drv_data->rx); 543ca632f55SGrant Likely 544ca632f55SGrant Likely /* Transfer delays and chip select release are 545ca632f55SGrant Likely * handled in pump_transfers or giveback 546ca632f55SGrant Likely */ 547ca632f55SGrant Likely 548ca632f55SGrant Likely /* Move to next transfer */ 549cd7bed00SMika Westerberg drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data); 550ca632f55SGrant Likely 551ca632f55SGrant Likely /* Schedule transfer tasklet */ 552ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 553ca632f55SGrant Likely } 554ca632f55SGrant Likely 555ca632f55SGrant Likely static irqreturn_t interrupt_transfer(struct driver_data *drv_data) 556ca632f55SGrant Likely { 557c039dd27SJarkko Nikula u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ? 558ca632f55SGrant Likely drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; 559ca632f55SGrant Likely 560c039dd27SJarkko Nikula u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask; 561ca632f55SGrant Likely 562ca632f55SGrant Likely if (irq_status & SSSR_ROR) { 563ca632f55SGrant Likely int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); 564ca632f55SGrant Likely return IRQ_HANDLED; 565ca632f55SGrant Likely } 566ca632f55SGrant Likely 567ca632f55SGrant Likely if (irq_status & SSSR_TINT) { 568c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); 569ca632f55SGrant Likely if (drv_data->read(drv_data)) { 570ca632f55SGrant Likely int_transfer_complete(drv_data); 571ca632f55SGrant Likely return IRQ_HANDLED; 572ca632f55SGrant Likely } 573ca632f55SGrant Likely } 574ca632f55SGrant Likely 575ca632f55SGrant Likely /* Drain rx fifo, Fill tx fifo and prevent overruns */ 576ca632f55SGrant Likely do { 577ca632f55SGrant Likely if (drv_data->read(drv_data)) { 578ca632f55SGrant Likely int_transfer_complete(drv_data); 579ca632f55SGrant Likely return IRQ_HANDLED; 580ca632f55SGrant Likely } 581ca632f55SGrant Likely } while (drv_data->write(drv_data)); 582ca632f55SGrant Likely 583ca632f55SGrant Likely if (drv_data->read(drv_data)) { 584ca632f55SGrant Likely int_transfer_complete(drv_data); 585ca632f55SGrant Likely return IRQ_HANDLED; 586ca632f55SGrant Likely } 587ca632f55SGrant Likely 588ca632f55SGrant Likely if (drv_data->tx == drv_data->tx_end) { 589ca632f55SGrant Likely u32 bytes_left; 590ca632f55SGrant Likely u32 sccr1_reg; 591ca632f55SGrant Likely 592c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 593ca632f55SGrant Likely sccr1_reg &= ~SSCR1_TIE; 594ca632f55SGrant Likely 595ca632f55SGrant Likely /* 596ca632f55SGrant Likely * PXA25x_SSP has no timeout, set up rx threshould for the 597ca632f55SGrant Likely * remaining RX bytes. 598ca632f55SGrant Likely */ 599ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 6004fdb2424SWeike Chen u32 rx_thre; 601ca632f55SGrant Likely 6024fdb2424SWeike Chen pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); 603ca632f55SGrant Likely 604ca632f55SGrant Likely bytes_left = drv_data->rx_end - drv_data->rx; 605ca632f55SGrant Likely switch (drv_data->n_bytes) { 606ca632f55SGrant Likely case 4: 607ca632f55SGrant Likely bytes_left >>= 1; 608ca632f55SGrant Likely case 2: 609ca632f55SGrant Likely bytes_left >>= 1; 610ca632f55SGrant Likely } 611ca632f55SGrant Likely 6124fdb2424SWeike Chen rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); 6134fdb2424SWeike Chen if (rx_thre > bytes_left) 6144fdb2424SWeike Chen rx_thre = bytes_left; 615ca632f55SGrant Likely 6164fdb2424SWeike Chen pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); 617ca632f55SGrant Likely } 618c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); 619ca632f55SGrant Likely } 620ca632f55SGrant Likely 621ca632f55SGrant Likely /* We did something */ 622ca632f55SGrant Likely return IRQ_HANDLED; 623ca632f55SGrant Likely } 624ca632f55SGrant Likely 625ca632f55SGrant Likely static irqreturn_t ssp_int(int irq, void *dev_id) 626ca632f55SGrant Likely { 627ca632f55SGrant Likely struct driver_data *drv_data = dev_id; 6287d94a505SMika Westerberg u32 sccr1_reg; 629ca632f55SGrant Likely u32 mask = drv_data->mask_sr; 630ca632f55SGrant Likely u32 status; 631ca632f55SGrant Likely 6327d94a505SMika Westerberg /* 6337d94a505SMika Westerberg * The IRQ might be shared with other peripherals so we must first 6347d94a505SMika Westerberg * check that are we RPM suspended or not. If we are we assume that 6357d94a505SMika Westerberg * the IRQ was not for us (we shouldn't be RPM suspended when the 6367d94a505SMika Westerberg * interrupt is enabled). 6377d94a505SMika Westerberg */ 6387d94a505SMika Westerberg if (pm_runtime_suspended(&drv_data->pdev->dev)) 6397d94a505SMika Westerberg return IRQ_NONE; 6407d94a505SMika Westerberg 641269e4a41SMika Westerberg /* 642269e4a41SMika Westerberg * If the device is not yet in RPM suspended state and we get an 643269e4a41SMika Westerberg * interrupt that is meant for another device, check if status bits 644269e4a41SMika Westerberg * are all set to one. That means that the device is already 645269e4a41SMika Westerberg * powered off. 646269e4a41SMika Westerberg */ 647c039dd27SJarkko Nikula status = pxa2xx_spi_read(drv_data, SSSR); 648269e4a41SMika Westerberg if (status == ~0) 649269e4a41SMika Westerberg return IRQ_NONE; 650269e4a41SMika Westerberg 651c039dd27SJarkko Nikula sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); 652ca632f55SGrant Likely 653ca632f55SGrant Likely /* Ignore possible writes if we don't need to write */ 654ca632f55SGrant Likely if (!(sccr1_reg & SSCR1_TIE)) 655ca632f55SGrant Likely mask &= ~SSSR_TFS; 656ca632f55SGrant Likely 657ca632f55SGrant Likely if (!(status & mask)) 658ca632f55SGrant Likely return IRQ_NONE; 659ca632f55SGrant Likely 660ca632f55SGrant Likely if (!drv_data->cur_msg) { 661ca632f55SGrant Likely 662c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 663c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) 664c039dd27SJarkko Nikula & ~SSCR0_SSE); 665c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, 666c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR1) 667c039dd27SJarkko Nikula & ~drv_data->int_cr1); 668ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 669c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 670ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 671ca632f55SGrant Likely 672f6bd03a7SJarkko Nikula dev_err(&drv_data->pdev->dev, 673f6bd03a7SJarkko Nikula "bad message state in interrupt handler\n"); 674ca632f55SGrant Likely 675ca632f55SGrant Likely /* Never fail */ 676ca632f55SGrant Likely return IRQ_HANDLED; 677ca632f55SGrant Likely } 678ca632f55SGrant Likely 679ca632f55SGrant Likely return drv_data->transfer_handler(drv_data); 680ca632f55SGrant Likely } 681ca632f55SGrant Likely 682e5262d05SWeike Chen /* 6839df461ecSAndy Shevchenko * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply 6849df461ecSAndy Shevchenko * input frequency by fractions of 2^24. It also has a divider by 5. 6859df461ecSAndy Shevchenko * 6869df461ecSAndy Shevchenko * There are formulas to get baud rate value for given input frequency and 6879df461ecSAndy Shevchenko * divider parameters, such as DDS_CLK_RATE and SCR: 6889df461ecSAndy Shevchenko * 6899df461ecSAndy Shevchenko * Fsys = 200MHz 6909df461ecSAndy Shevchenko * 6919df461ecSAndy Shevchenko * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) 6929df461ecSAndy Shevchenko * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) 6939df461ecSAndy Shevchenko * 6949df461ecSAndy Shevchenko * DDS_CLK_RATE either 2^n or 2^n / 5. 6959df461ecSAndy Shevchenko * SCR is in range 0 .. 255 6969df461ecSAndy Shevchenko * 6979df461ecSAndy Shevchenko * Divisor = 5^i * 2^j * 2 * k 6989df461ecSAndy Shevchenko * i = [0, 1] i = 1 iff j = 0 or j > 3 6999df461ecSAndy Shevchenko * j = [0, 23] j = 0 iff i = 1 7009df461ecSAndy Shevchenko * k = [1, 256] 7019df461ecSAndy Shevchenko * Special case: j = 0, i = 1: Divisor = 2 / 5 7029df461ecSAndy Shevchenko * 7039df461ecSAndy Shevchenko * Accordingly to the specification the recommended values for DDS_CLK_RATE 7049df461ecSAndy Shevchenko * are: 7059df461ecSAndy Shevchenko * Case 1: 2^n, n = [0, 23] 7069df461ecSAndy Shevchenko * Case 2: 2^24 * 2 / 5 (0x666666) 7079df461ecSAndy Shevchenko * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) 7089df461ecSAndy Shevchenko * 7099df461ecSAndy Shevchenko * In all cases the lowest possible value is better. 7109df461ecSAndy Shevchenko * 7119df461ecSAndy Shevchenko * The function calculates parameters for all cases and chooses the one closest 7129df461ecSAndy Shevchenko * to the asked baud rate. 713e5262d05SWeike Chen */ 7149df461ecSAndy Shevchenko static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) 715e5262d05SWeike Chen { 7169df461ecSAndy Shevchenko unsigned long xtal = 200000000; 7179df461ecSAndy Shevchenko unsigned long fref = xtal / 2; /* mandatory division by 2, 7189df461ecSAndy Shevchenko see (2) */ 7199df461ecSAndy Shevchenko /* case 3 */ 7209df461ecSAndy Shevchenko unsigned long fref1 = fref / 2; /* case 1 */ 7219df461ecSAndy Shevchenko unsigned long fref2 = fref * 2 / 5; /* case 2 */ 7229df461ecSAndy Shevchenko unsigned long scale; 7239df461ecSAndy Shevchenko unsigned long q, q1, q2; 7249df461ecSAndy Shevchenko long r, r1, r2; 7259df461ecSAndy Shevchenko u32 mul; 726e5262d05SWeike Chen 7279df461ecSAndy Shevchenko /* Case 1 */ 7289df461ecSAndy Shevchenko 7299df461ecSAndy Shevchenko /* Set initial value for DDS_CLK_RATE */ 7309df461ecSAndy Shevchenko mul = (1 << 24) >> 1; 7319df461ecSAndy Shevchenko 7329df461ecSAndy Shevchenko /* Calculate initial quot */ 7339df461ecSAndy Shevchenko q1 = DIV_ROUND_CLOSEST(fref1, rate); 7349df461ecSAndy Shevchenko 7359df461ecSAndy Shevchenko /* Scale q1 if it's too big */ 7369df461ecSAndy Shevchenko if (q1 > 256) { 7379df461ecSAndy Shevchenko /* Scale q1 to range [1, 512] */ 7389df461ecSAndy Shevchenko scale = fls_long(q1 - 1); 7399df461ecSAndy Shevchenko if (scale > 9) { 7409df461ecSAndy Shevchenko q1 >>= scale - 9; 7419df461ecSAndy Shevchenko mul >>= scale - 9; 7429df461ecSAndy Shevchenko } 7439df461ecSAndy Shevchenko 7449df461ecSAndy Shevchenko /* Round the result if we have a remainder */ 7459df461ecSAndy Shevchenko q1 += q1 & 1; 7469df461ecSAndy Shevchenko } 7479df461ecSAndy Shevchenko 7489df461ecSAndy Shevchenko /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ 7499df461ecSAndy Shevchenko scale = __ffs(q1); 7509df461ecSAndy Shevchenko q1 >>= scale; 7519df461ecSAndy Shevchenko mul >>= scale; 7529df461ecSAndy Shevchenko 7539df461ecSAndy Shevchenko /* Get the remainder */ 7549df461ecSAndy Shevchenko r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); 7559df461ecSAndy Shevchenko 7569df461ecSAndy Shevchenko /* Case 2 */ 7579df461ecSAndy Shevchenko 7589df461ecSAndy Shevchenko q2 = DIV_ROUND_CLOSEST(fref2, rate); 7599df461ecSAndy Shevchenko r2 = abs(fref2 / q2 - rate); 7609df461ecSAndy Shevchenko 7619df461ecSAndy Shevchenko /* 7629df461ecSAndy Shevchenko * Choose the best between two: less remainder we have the better. We 7639df461ecSAndy Shevchenko * can't go case 2 if q2 is greater than 256 since SCR register can 7649df461ecSAndy Shevchenko * hold only values 0 .. 255. 7659df461ecSAndy Shevchenko */ 7669df461ecSAndy Shevchenko if (r2 >= r1 || q2 > 256) { 7679df461ecSAndy Shevchenko /* case 1 is better */ 7689df461ecSAndy Shevchenko r = r1; 7699df461ecSAndy Shevchenko q = q1; 7709df461ecSAndy Shevchenko } else { 7719df461ecSAndy Shevchenko /* case 2 is better */ 7729df461ecSAndy Shevchenko r = r2; 7739df461ecSAndy Shevchenko q = q2; 7749df461ecSAndy Shevchenko mul = (1 << 24) * 2 / 5; 7759df461ecSAndy Shevchenko } 7769df461ecSAndy Shevchenko 7779df461ecSAndy Shevchenko /* Check case 3 only If the divisor is big enough */ 7789df461ecSAndy Shevchenko if (fref / rate >= 80) { 7799df461ecSAndy Shevchenko u64 fssp; 7809df461ecSAndy Shevchenko u32 m; 7819df461ecSAndy Shevchenko 7829df461ecSAndy Shevchenko /* Calculate initial quot */ 7839df461ecSAndy Shevchenko q1 = DIV_ROUND_CLOSEST(fref, rate); 7849df461ecSAndy Shevchenko m = (1 << 24) / q1; 7859df461ecSAndy Shevchenko 7869df461ecSAndy Shevchenko /* Get the remainder */ 7879df461ecSAndy Shevchenko fssp = (u64)fref * m; 7889df461ecSAndy Shevchenko do_div(fssp, 1 << 24); 7899df461ecSAndy Shevchenko r1 = abs(fssp - rate); 7909df461ecSAndy Shevchenko 7919df461ecSAndy Shevchenko /* Choose this one if it suits better */ 7929df461ecSAndy Shevchenko if (r1 < r) { 7939df461ecSAndy Shevchenko /* case 3 is better */ 7949df461ecSAndy Shevchenko q = 1; 7959df461ecSAndy Shevchenko mul = m; 796e5262d05SWeike Chen } 797e5262d05SWeike Chen } 798e5262d05SWeike Chen 7999df461ecSAndy Shevchenko *dds = mul; 8009df461ecSAndy Shevchenko return q - 1; 801e5262d05SWeike Chen } 802e5262d05SWeike Chen 8033343b7a6SMika Westerberg static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) 804ca632f55SGrant Likely { 805*0eca7cf2SJarkko Nikula unsigned long ssp_clk = drv_data->master->max_speed_hz; 8063343b7a6SMika Westerberg const struct ssp_device *ssp = drv_data->ssp; 8073343b7a6SMika Westerberg 8083343b7a6SMika Westerberg rate = min_t(int, ssp_clk, rate); 809ca632f55SGrant Likely 810ca632f55SGrant Likely if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) 811025ffe88SAndy Shevchenko return (ssp_clk / (2 * rate) - 1) & 0xff; 812ca632f55SGrant Likely else 813025ffe88SAndy Shevchenko return (ssp_clk / rate - 1) & 0xfff; 814ca632f55SGrant Likely } 815ca632f55SGrant Likely 816e5262d05SWeike Chen static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, 817e5262d05SWeike Chen struct chip_data *chip, int rate) 818e5262d05SWeike Chen { 819025ffe88SAndy Shevchenko unsigned int clk_div; 820e5262d05SWeike Chen 821e5262d05SWeike Chen switch (drv_data->ssp_type) { 822e5262d05SWeike Chen case QUARK_X1000_SSP: 8239df461ecSAndy Shevchenko clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); 824eecacf73SDan Carpenter break; 825e5262d05SWeike Chen default: 826025ffe88SAndy Shevchenko clk_div = ssp_get_clk_div(drv_data, rate); 827eecacf73SDan Carpenter break; 828e5262d05SWeike Chen } 829025ffe88SAndy Shevchenko return clk_div << 8; 830e5262d05SWeike Chen } 831e5262d05SWeike Chen 832ca632f55SGrant Likely static void pump_transfers(unsigned long data) 833ca632f55SGrant Likely { 834ca632f55SGrant Likely struct driver_data *drv_data = (struct driver_data *)data; 835ca632f55SGrant Likely struct spi_message *message = NULL; 836ca632f55SGrant Likely struct spi_transfer *transfer = NULL; 837ca632f55SGrant Likely struct spi_transfer *previous = NULL; 838ca632f55SGrant Likely struct chip_data *chip = NULL; 839ca632f55SGrant Likely u32 clk_div = 0; 840ca632f55SGrant Likely u8 bits = 0; 841ca632f55SGrant Likely u32 speed = 0; 842ca632f55SGrant Likely u32 cr0; 843ca632f55SGrant Likely u32 cr1; 844ca632f55SGrant Likely u32 dma_thresh = drv_data->cur_chip->dma_threshold; 845ca632f55SGrant Likely u32 dma_burst = drv_data->cur_chip->dma_burst_size; 8464fdb2424SWeike Chen u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); 847ca632f55SGrant Likely 848ca632f55SGrant Likely /* Get current state information */ 849ca632f55SGrant Likely message = drv_data->cur_msg; 850ca632f55SGrant Likely transfer = drv_data->cur_transfer; 851ca632f55SGrant Likely chip = drv_data->cur_chip; 852ca632f55SGrant Likely 853ca632f55SGrant Likely /* Handle for abort */ 854ca632f55SGrant Likely if (message->state == ERROR_STATE) { 855ca632f55SGrant Likely message->status = -EIO; 856ca632f55SGrant Likely giveback(drv_data); 857ca632f55SGrant Likely return; 858ca632f55SGrant Likely } 859ca632f55SGrant Likely 860ca632f55SGrant Likely /* Handle end of message */ 861ca632f55SGrant Likely if (message->state == DONE_STATE) { 862ca632f55SGrant Likely message->status = 0; 863ca632f55SGrant Likely giveback(drv_data); 864ca632f55SGrant Likely return; 865ca632f55SGrant Likely } 866ca632f55SGrant Likely 867ca632f55SGrant Likely /* Delay if requested at end of transfer before CS change */ 868ca632f55SGrant Likely if (message->state == RUNNING_STATE) { 869ca632f55SGrant Likely previous = list_entry(transfer->transfer_list.prev, 870ca632f55SGrant Likely struct spi_transfer, 871ca632f55SGrant Likely transfer_list); 872ca632f55SGrant Likely if (previous->delay_usecs) 873ca632f55SGrant Likely udelay(previous->delay_usecs); 874ca632f55SGrant Likely 875ca632f55SGrant Likely /* Drop chip select only if cs_change is requested */ 876ca632f55SGrant Likely if (previous->cs_change) 877ca632f55SGrant Likely cs_deassert(drv_data); 878ca632f55SGrant Likely } 879ca632f55SGrant Likely 880cd7bed00SMika Westerberg /* Check if we can DMA this transfer */ 881cd7bed00SMika Westerberg if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) { 882ca632f55SGrant Likely 883ca632f55SGrant Likely /* reject already-mapped transfers; PIO won't always work */ 884ca632f55SGrant Likely if (message->is_dma_mapped 885ca632f55SGrant Likely || transfer->rx_dma || transfer->tx_dma) { 886ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, 887f6bd03a7SJarkko Nikula "pump_transfers: mapped transfer length of " 888f6bd03a7SJarkko Nikula "%u is greater than %d\n", 889ca632f55SGrant Likely transfer->len, MAX_DMA_LEN); 890ca632f55SGrant Likely message->status = -EINVAL; 891ca632f55SGrant Likely giveback(drv_data); 892ca632f55SGrant Likely return; 893ca632f55SGrant Likely } 894ca632f55SGrant Likely 895ca632f55SGrant Likely /* warn ... we force this to PIO mode */ 896f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 897f6bd03a7SJarkko Nikula "pump_transfers: DMA disabled for transfer length %ld " 898ca632f55SGrant Likely "greater than %d\n", 899ca632f55SGrant Likely (long)drv_data->len, MAX_DMA_LEN); 900ca632f55SGrant Likely } 901ca632f55SGrant Likely 902ca632f55SGrant Likely /* Setup the transfer state based on the type of transfer */ 903cd7bed00SMika Westerberg if (pxa2xx_spi_flush(drv_data) == 0) { 904ca632f55SGrant Likely dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); 905ca632f55SGrant Likely message->status = -EIO; 906ca632f55SGrant Likely giveback(drv_data); 907ca632f55SGrant Likely return; 908ca632f55SGrant Likely } 909ca632f55SGrant Likely drv_data->n_bytes = chip->n_bytes; 910ca632f55SGrant Likely drv_data->tx = (void *)transfer->tx_buf; 911ca632f55SGrant Likely drv_data->tx_end = drv_data->tx + transfer->len; 912ca632f55SGrant Likely drv_data->rx = transfer->rx_buf; 913ca632f55SGrant Likely drv_data->rx_end = drv_data->rx + transfer->len; 914ca632f55SGrant Likely drv_data->rx_dma = transfer->rx_dma; 915ca632f55SGrant Likely drv_data->tx_dma = transfer->tx_dma; 916cd7bed00SMika Westerberg drv_data->len = transfer->len; 917ca632f55SGrant Likely drv_data->write = drv_data->tx ? chip->write : null_writer; 918ca632f55SGrant Likely drv_data->read = drv_data->rx ? chip->read : null_reader; 919ca632f55SGrant Likely 920ca632f55SGrant Likely /* Change speed and bit per word on a per transfer */ 921ca632f55SGrant Likely bits = transfer->bits_per_word; 9224f1474b3SJarkko Nikula speed = transfer->speed_hz; 923ca632f55SGrant Likely 924e5262d05SWeike Chen clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed); 925ca632f55SGrant Likely 926ca632f55SGrant Likely if (bits <= 8) { 927ca632f55SGrant Likely drv_data->n_bytes = 1; 928ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 929ca632f55SGrant Likely u8_reader : null_reader; 930ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 931ca632f55SGrant Likely u8_writer : null_writer; 932ca632f55SGrant Likely } else if (bits <= 16) { 933ca632f55SGrant Likely drv_data->n_bytes = 2; 934ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 935ca632f55SGrant Likely u16_reader : null_reader; 936ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 937ca632f55SGrant Likely u16_writer : null_writer; 938ca632f55SGrant Likely } else if (bits <= 32) { 939ca632f55SGrant Likely drv_data->n_bytes = 4; 940ca632f55SGrant Likely drv_data->read = drv_data->read != null_reader ? 941ca632f55SGrant Likely u32_reader : null_reader; 942ca632f55SGrant Likely drv_data->write = drv_data->write != null_writer ? 943ca632f55SGrant Likely u32_writer : null_writer; 944ca632f55SGrant Likely } 945196b0e2cSJarkko Nikula /* 946196b0e2cSJarkko Nikula * if bits/word is changed in dma mode, then must check the 947196b0e2cSJarkko Nikula * thresholds and burst also 948196b0e2cSJarkko Nikula */ 949ca632f55SGrant Likely if (chip->enable_dma) { 950cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, 951cd7bed00SMika Westerberg message->spi, 952ca632f55SGrant Likely bits, &dma_burst, 953ca632f55SGrant Likely &dma_thresh)) 954f6bd03a7SJarkko Nikula dev_warn_ratelimited(&message->spi->dev, 955f6bd03a7SJarkko Nikula "pump_transfers: DMA burst size reduced to match bits_per_word\n"); 956ca632f55SGrant Likely } 957ca632f55SGrant Likely 9584fdb2424SWeike Chen cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); 959ca632f55SGrant Likely 960ca632f55SGrant Likely message->state = RUNNING_STATE; 961ca632f55SGrant Likely 962ca632f55SGrant Likely drv_data->dma_mapped = 0; 963cd7bed00SMika Westerberg if (pxa2xx_spi_dma_is_possible(drv_data->len)) 964cd7bed00SMika Westerberg drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data); 965ca632f55SGrant Likely if (drv_data->dma_mapped) { 966ca632f55SGrant Likely 967ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 968cd7bed00SMika Westerberg drv_data->transfer_handler = pxa2xx_spi_dma_transfer; 969ca632f55SGrant Likely 970cd7bed00SMika Westerberg pxa2xx_spi_dma_prepare(drv_data, dma_burst); 971ca632f55SGrant Likely 972ca632f55SGrant Likely /* Clear status and start DMA engine */ 973ca632f55SGrant Likely cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; 974c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); 975cd7bed00SMika Westerberg 976cd7bed00SMika Westerberg pxa2xx_spi_dma_start(drv_data); 977ca632f55SGrant Likely } else { 978ca632f55SGrant Likely /* Ensure we have the correct interrupt handler */ 979ca632f55SGrant Likely drv_data->transfer_handler = interrupt_transfer; 980ca632f55SGrant Likely 981ca632f55SGrant Likely /* Clear status */ 982ca632f55SGrant Likely cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; 983ca632f55SGrant Likely write_SSSR_CS(drv_data, drv_data->clear_sr); 984ca632f55SGrant Likely } 985ca632f55SGrant Likely 986a0d2642eSMika Westerberg if (is_lpss_ssp(drv_data)) { 987c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff) 988c039dd27SJarkko Nikula != chip->lpss_rx_threshold) 989c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSIRF, 990c039dd27SJarkko Nikula chip->lpss_rx_threshold); 991c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff) 992c039dd27SJarkko Nikula != chip->lpss_tx_threshold) 993c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSITF, 994c039dd27SJarkko Nikula chip->lpss_tx_threshold); 995a0d2642eSMika Westerberg } 996a0d2642eSMika Westerberg 997e5262d05SWeike Chen if (is_quark_x1000_ssp(drv_data) && 998c039dd27SJarkko Nikula (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate)) 999c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate); 1000e5262d05SWeike Chen 1001ca632f55SGrant Likely /* see if we need to reload the config registers */ 1002c039dd27SJarkko Nikula if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0) 1003c039dd27SJarkko Nikula || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask) 1004c039dd27SJarkko Nikula != (cr1 & change_mask)) { 1005ca632f55SGrant Likely /* stop the SSP, and update the other bits */ 1006c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE); 1007ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1008c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1009ca632f55SGrant Likely /* first set CR1 without interrupt and service enables */ 1010c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask); 1011ca632f55SGrant Likely /* restart the SSP */ 1012c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, cr0); 1013ca632f55SGrant Likely 1014ca632f55SGrant Likely } else { 1015ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1016c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, chip->timeout); 1017ca632f55SGrant Likely } 1018ca632f55SGrant Likely 1019ca632f55SGrant Likely cs_assert(drv_data); 1020ca632f55SGrant Likely 1021ca632f55SGrant Likely /* after chip select, release the data by enabling service 1022ca632f55SGrant Likely * requests and interrupts, without changing any mode bits */ 1023c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, cr1); 1024ca632f55SGrant Likely } 1025ca632f55SGrant Likely 10267f86bde9SMika Westerberg static int pxa2xx_spi_transfer_one_message(struct spi_master *master, 10277f86bde9SMika Westerberg struct spi_message *msg) 1028ca632f55SGrant Likely { 10297f86bde9SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 1030ca632f55SGrant Likely 10317f86bde9SMika Westerberg drv_data->cur_msg = msg; 1032ca632f55SGrant Likely /* Initial message state*/ 1033ca632f55SGrant Likely drv_data->cur_msg->state = START_STATE; 1034ca632f55SGrant Likely drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, 1035ca632f55SGrant Likely struct spi_transfer, 1036ca632f55SGrant Likely transfer_list); 1037ca632f55SGrant Likely 1038ca632f55SGrant Likely /* prepare to setup the SSP, in pump_transfers, using the per 1039ca632f55SGrant Likely * chip configuration */ 1040ca632f55SGrant Likely drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); 1041ca632f55SGrant Likely 1042ca632f55SGrant Likely /* Mark as busy and launch transfers */ 1043ca632f55SGrant Likely tasklet_schedule(&drv_data->pump_transfers); 1044ca632f55SGrant Likely return 0; 1045ca632f55SGrant Likely } 1046ca632f55SGrant Likely 10477d94a505SMika Westerberg static int pxa2xx_spi_unprepare_transfer(struct spi_master *master) 10487d94a505SMika Westerberg { 10497d94a505SMika Westerberg struct driver_data *drv_data = spi_master_get_devdata(master); 10507d94a505SMika Westerberg 10517d94a505SMika Westerberg /* Disable the SSP now */ 1052c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 1053c039dd27SJarkko Nikula pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); 10547d94a505SMika Westerberg 10557d94a505SMika Westerberg return 0; 10567d94a505SMika Westerberg } 10577d94a505SMika Westerberg 1058ca632f55SGrant Likely static int setup_cs(struct spi_device *spi, struct chip_data *chip, 1059ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info) 1060ca632f55SGrant Likely { 1061ca632f55SGrant Likely int err = 0; 1062ca632f55SGrant Likely 1063ca632f55SGrant Likely if (chip == NULL || chip_info == NULL) 1064ca632f55SGrant Likely return 0; 1065ca632f55SGrant Likely 1066ca632f55SGrant Likely /* NOTE: setup() can be called multiple times, possibly with 1067ca632f55SGrant Likely * different chip_info, release previously requested GPIO 1068ca632f55SGrant Likely */ 1069ca632f55SGrant Likely if (gpio_is_valid(chip->gpio_cs)) 1070ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1071ca632f55SGrant Likely 1072ca632f55SGrant Likely /* If (*cs_control) is provided, ignore GPIO chip select */ 1073ca632f55SGrant Likely if (chip_info->cs_control) { 1074ca632f55SGrant Likely chip->cs_control = chip_info->cs_control; 1075ca632f55SGrant Likely return 0; 1076ca632f55SGrant Likely } 1077ca632f55SGrant Likely 1078ca632f55SGrant Likely if (gpio_is_valid(chip_info->gpio_cs)) { 1079ca632f55SGrant Likely err = gpio_request(chip_info->gpio_cs, "SPI_CS"); 1080ca632f55SGrant Likely if (err) { 1081f6bd03a7SJarkko Nikula dev_err(&spi->dev, "failed to request chip select GPIO%d\n", 1082f6bd03a7SJarkko Nikula chip_info->gpio_cs); 1083ca632f55SGrant Likely return err; 1084ca632f55SGrant Likely } 1085ca632f55SGrant Likely 1086ca632f55SGrant Likely chip->gpio_cs = chip_info->gpio_cs; 1087ca632f55SGrant Likely chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; 1088ca632f55SGrant Likely 1089ca632f55SGrant Likely err = gpio_direction_output(chip->gpio_cs, 1090ca632f55SGrant Likely !chip->gpio_cs_inverted); 1091ca632f55SGrant Likely } 1092ca632f55SGrant Likely 1093ca632f55SGrant Likely return err; 1094ca632f55SGrant Likely } 1095ca632f55SGrant Likely 1096ca632f55SGrant Likely static int setup(struct spi_device *spi) 1097ca632f55SGrant Likely { 1098ca632f55SGrant Likely struct pxa2xx_spi_chip *chip_info = NULL; 1099ca632f55SGrant Likely struct chip_data *chip; 1100dccf7369SJarkko Nikula const struct lpss_config *config; 1101ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1102ca632f55SGrant Likely unsigned int clk_div; 1103a0d2642eSMika Westerberg uint tx_thres, tx_hi_thres, rx_thres; 1104b69d42b5SJarkko Nikula u32 cr0; 1105a0d2642eSMika Westerberg 1106e5262d05SWeike Chen switch (drv_data->ssp_type) { 1107e5262d05SWeike Chen case QUARK_X1000_SSP: 1108e5262d05SWeike Chen tx_thres = TX_THRESH_QUARK_X1000_DFLT; 1109e5262d05SWeike Chen tx_hi_thres = 0; 1110e5262d05SWeike Chen rx_thres = RX_THRESH_QUARK_X1000_DFLT; 1111e5262d05SWeike Chen break; 111203fbf488SJarkko Nikula case LPSS_LPT_SSP: 111303fbf488SJarkko Nikula case LPSS_BYT_SSP: 111434cadd9cSJarkko Nikula case LPSS_SPT_SSP: 1115dccf7369SJarkko Nikula config = lpss_get_config(drv_data); 1116dccf7369SJarkko Nikula tx_thres = config->tx_threshold_lo; 1117dccf7369SJarkko Nikula tx_hi_thres = config->tx_threshold_hi; 1118dccf7369SJarkko Nikula rx_thres = config->rx_threshold; 1119e5262d05SWeike Chen break; 1120e5262d05SWeike Chen default: 1121a0d2642eSMika Westerberg tx_thres = TX_THRESH_DFLT; 1122a0d2642eSMika Westerberg tx_hi_thres = 0; 1123a0d2642eSMika Westerberg rx_thres = RX_THRESH_DFLT; 1124e5262d05SWeike Chen break; 1125a0d2642eSMika Westerberg } 1126ca632f55SGrant Likely 1127ca632f55SGrant Likely /* Only alloc on first setup */ 1128ca632f55SGrant Likely chip = spi_get_ctldata(spi); 1129ca632f55SGrant Likely if (!chip) { 1130ca632f55SGrant Likely chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 11319deae459SJingoo Han if (!chip) 1132ca632f55SGrant Likely return -ENOMEM; 1133ca632f55SGrant Likely 1134ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) { 1135ca632f55SGrant Likely if (spi->chip_select > 4) { 1136f6bd03a7SJarkko Nikula dev_err(&spi->dev, 1137f6bd03a7SJarkko Nikula "failed setup: cs number must not be > 4.\n"); 1138ca632f55SGrant Likely kfree(chip); 1139ca632f55SGrant Likely return -EINVAL; 1140ca632f55SGrant Likely } 1141ca632f55SGrant Likely 1142ca632f55SGrant Likely chip->frm = spi->chip_select; 1143ca632f55SGrant Likely } else 1144ca632f55SGrant Likely chip->gpio_cs = -1; 1145ca632f55SGrant Likely chip->enable_dma = 0; 1146ca632f55SGrant Likely chip->timeout = TIMOUT_DFLT; 1147ca632f55SGrant Likely } 1148ca632f55SGrant Likely 1149ca632f55SGrant Likely /* protocol drivers may change the chip settings, so... 1150ca632f55SGrant Likely * if chip_info exists, use it */ 1151ca632f55SGrant Likely chip_info = spi->controller_data; 1152ca632f55SGrant Likely 1153ca632f55SGrant Likely /* chip_info isn't always needed */ 1154ca632f55SGrant Likely chip->cr1 = 0; 1155ca632f55SGrant Likely if (chip_info) { 1156ca632f55SGrant Likely if (chip_info->timeout) 1157ca632f55SGrant Likely chip->timeout = chip_info->timeout; 1158ca632f55SGrant Likely if (chip_info->tx_threshold) 1159ca632f55SGrant Likely tx_thres = chip_info->tx_threshold; 1160a0d2642eSMika Westerberg if (chip_info->tx_hi_threshold) 1161a0d2642eSMika Westerberg tx_hi_thres = chip_info->tx_hi_threshold; 1162ca632f55SGrant Likely if (chip_info->rx_threshold) 1163ca632f55SGrant Likely rx_thres = chip_info->rx_threshold; 1164ca632f55SGrant Likely chip->enable_dma = drv_data->master_info->enable_dma; 1165ca632f55SGrant Likely chip->dma_threshold = 0; 1166ca632f55SGrant Likely if (chip_info->enable_loopback) 1167ca632f55SGrant Likely chip->cr1 = SSCR1_LBM; 1168a3496855SMika Westerberg } else if (ACPI_HANDLE(&spi->dev)) { 1169a3496855SMika Westerberg /* 1170a3496855SMika Westerberg * Slave devices enumerated from ACPI namespace don't 1171a3496855SMika Westerberg * usually have chip_info but we still might want to use 1172a3496855SMika Westerberg * DMA with them. 1173a3496855SMika Westerberg */ 1174a3496855SMika Westerberg chip->enable_dma = drv_data->master_info->enable_dma; 1175ca632f55SGrant Likely } 1176ca632f55SGrant Likely 1177a0d2642eSMika Westerberg chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); 1178a0d2642eSMika Westerberg chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) 1179a0d2642eSMika Westerberg | SSITF_TxHiThresh(tx_hi_thres); 1180a0d2642eSMika Westerberg 1181ca632f55SGrant Likely /* set dma burst and threshold outside of chip_info path so that if 1182ca632f55SGrant Likely * chip_info goes away after setting chip->enable_dma, the 1183ca632f55SGrant Likely * burst and threshold can still respond to changes in bits_per_word */ 1184ca632f55SGrant Likely if (chip->enable_dma) { 1185ca632f55SGrant Likely /* set up legal burst and threshold for dma */ 1186cd7bed00SMika Westerberg if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, 1187cd7bed00SMika Westerberg spi->bits_per_word, 1188ca632f55SGrant Likely &chip->dma_burst_size, 1189ca632f55SGrant Likely &chip->dma_threshold)) { 1190f6bd03a7SJarkko Nikula dev_warn(&spi->dev, 1191f6bd03a7SJarkko Nikula "in setup: DMA burst size reduced to match bits_per_word\n"); 1192ca632f55SGrant Likely } 1193ca632f55SGrant Likely } 1194ca632f55SGrant Likely 1195e5262d05SWeike Chen clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz); 1196ca632f55SGrant Likely 1197e5262d05SWeike Chen switch (drv_data->ssp_type) { 1198e5262d05SWeike Chen case QUARK_X1000_SSP: 1199e5262d05SWeike Chen chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) 1200e5262d05SWeike Chen & QUARK_X1000_SSCR1_RFT) 1201e5262d05SWeike Chen | (QUARK_X1000_SSCR1_TxTresh(tx_thres) 1202e5262d05SWeike Chen & QUARK_X1000_SSCR1_TFT); 1203e5262d05SWeike Chen break; 1204e5262d05SWeike Chen default: 1205e5262d05SWeike Chen chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | 1206e5262d05SWeike Chen (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); 1207e5262d05SWeike Chen break; 1208e5262d05SWeike Chen } 1209e5262d05SWeike Chen 1210ca632f55SGrant Likely chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); 1211ca632f55SGrant Likely chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) 1212ca632f55SGrant Likely | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); 1213ca632f55SGrant Likely 1214b833172fSMika Westerberg if (spi->mode & SPI_LOOP) 1215b833172fSMika Westerberg chip->cr1 |= SSCR1_LBM; 1216b833172fSMika Westerberg 1217ca632f55SGrant Likely /* NOTE: PXA25x_SSP _could_ use external clocking ... */ 1218b69d42b5SJarkko Nikula cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, spi->bits_per_word); 1219ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1220*0eca7cf2SJarkko Nikula dev_dbg(&spi->dev, "%u Hz actual, %s\n", 1221*0eca7cf2SJarkko Nikula drv_data->master->max_speed_hz 1222b69d42b5SJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), 1223ca632f55SGrant Likely chip->enable_dma ? "DMA" : "PIO"); 1224ca632f55SGrant Likely else 1225*0eca7cf2SJarkko Nikula dev_dbg(&spi->dev, "%u Hz actual, %s\n", 1226*0eca7cf2SJarkko Nikula drv_data->master->max_speed_hz / 2 1227b69d42b5SJarkko Nikula / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), 1228ca632f55SGrant Likely chip->enable_dma ? "DMA" : "PIO"); 1229ca632f55SGrant Likely 1230ca632f55SGrant Likely if (spi->bits_per_word <= 8) { 1231ca632f55SGrant Likely chip->n_bytes = 1; 1232ca632f55SGrant Likely chip->read = u8_reader; 1233ca632f55SGrant Likely chip->write = u8_writer; 1234ca632f55SGrant Likely } else if (spi->bits_per_word <= 16) { 1235ca632f55SGrant Likely chip->n_bytes = 2; 1236ca632f55SGrant Likely chip->read = u16_reader; 1237ca632f55SGrant Likely chip->write = u16_writer; 1238ca632f55SGrant Likely } else if (spi->bits_per_word <= 32) { 1239ca632f55SGrant Likely chip->n_bytes = 4; 1240ca632f55SGrant Likely chip->read = u32_reader; 1241ca632f55SGrant Likely chip->write = u32_writer; 1242ca632f55SGrant Likely } 1243ca632f55SGrant Likely 1244ca632f55SGrant Likely spi_set_ctldata(spi, chip); 1245ca632f55SGrant Likely 1246ca632f55SGrant Likely if (drv_data->ssp_type == CE4100_SSP) 1247ca632f55SGrant Likely return 0; 1248ca632f55SGrant Likely 1249ca632f55SGrant Likely return setup_cs(spi, chip, chip_info); 1250ca632f55SGrant Likely } 1251ca632f55SGrant Likely 1252ca632f55SGrant Likely static void cleanup(struct spi_device *spi) 1253ca632f55SGrant Likely { 1254ca632f55SGrant Likely struct chip_data *chip = spi_get_ctldata(spi); 1255ca632f55SGrant Likely struct driver_data *drv_data = spi_master_get_devdata(spi->master); 1256ca632f55SGrant Likely 1257ca632f55SGrant Likely if (!chip) 1258ca632f55SGrant Likely return; 1259ca632f55SGrant Likely 1260ca632f55SGrant Likely if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs)) 1261ca632f55SGrant Likely gpio_free(chip->gpio_cs); 1262ca632f55SGrant Likely 1263ca632f55SGrant Likely kfree(chip); 1264ca632f55SGrant Likely } 1265ca632f55SGrant Likely 1266a3496855SMika Westerberg #ifdef CONFIG_ACPI 126703fbf488SJarkko Nikula 12688422ddf7SMathias Krause static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { 126903fbf488SJarkko Nikula { "INT33C0", LPSS_LPT_SSP }, 127003fbf488SJarkko Nikula { "INT33C1", LPSS_LPT_SSP }, 127103fbf488SJarkko Nikula { "INT3430", LPSS_LPT_SSP }, 127203fbf488SJarkko Nikula { "INT3431", LPSS_LPT_SSP }, 127303fbf488SJarkko Nikula { "80860F0E", LPSS_BYT_SSP }, 127403fbf488SJarkko Nikula { "8086228E", LPSS_BYT_SSP }, 127503fbf488SJarkko Nikula { }, 127603fbf488SJarkko Nikula }; 127703fbf488SJarkko Nikula MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); 127803fbf488SJarkko Nikula 127934cadd9cSJarkko Nikula /* 128034cadd9cSJarkko Nikula * PCI IDs of compound devices that integrate both host controller and private 128134cadd9cSJarkko Nikula * integrated DMA engine. Please note these are not used in module 128234cadd9cSJarkko Nikula * autoloading and probing in this module but matching the LPSS SSP type. 128334cadd9cSJarkko Nikula */ 128434cadd9cSJarkko Nikula static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { 128534cadd9cSJarkko Nikula /* SPT-LP */ 128634cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, 128734cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, 128834cadd9cSJarkko Nikula /* SPT-H */ 128934cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, 129034cadd9cSJarkko Nikula { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, 129194e5c23dSAxel Lin { }, 129234cadd9cSJarkko Nikula }; 129334cadd9cSJarkko Nikula 129434cadd9cSJarkko Nikula static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) 129534cadd9cSJarkko Nikula { 129634cadd9cSJarkko Nikula struct device *dev = param; 129734cadd9cSJarkko Nikula 129834cadd9cSJarkko Nikula if (dev != chan->device->dev->parent) 129934cadd9cSJarkko Nikula return false; 130034cadd9cSJarkko Nikula 130134cadd9cSJarkko Nikula return true; 130234cadd9cSJarkko Nikula } 130334cadd9cSJarkko Nikula 1304a3496855SMika Westerberg static struct pxa2xx_spi_master * 1305a3496855SMika Westerberg pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) 1306a3496855SMika Westerberg { 1307a3496855SMika Westerberg struct pxa2xx_spi_master *pdata; 1308a3496855SMika Westerberg struct acpi_device *adev; 1309a3496855SMika Westerberg struct ssp_device *ssp; 1310a3496855SMika Westerberg struct resource *res; 131134cadd9cSJarkko Nikula const struct acpi_device_id *adev_id = NULL; 131234cadd9cSJarkko Nikula const struct pci_device_id *pcidev_id = NULL; 131303fbf488SJarkko Nikula int devid, type; 1314a3496855SMika Westerberg 1315a3496855SMika Westerberg if (!ACPI_HANDLE(&pdev->dev) || 1316a3496855SMika Westerberg acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev)) 1317a3496855SMika Westerberg return NULL; 1318a3496855SMika Westerberg 131934cadd9cSJarkko Nikula if (dev_is_pci(pdev->dev.parent)) 132034cadd9cSJarkko Nikula pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, 132134cadd9cSJarkko Nikula to_pci_dev(pdev->dev.parent)); 132234cadd9cSJarkko Nikula else 132334cadd9cSJarkko Nikula adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table, 132434cadd9cSJarkko Nikula &pdev->dev); 132534cadd9cSJarkko Nikula 132634cadd9cSJarkko Nikula if (adev_id) 132734cadd9cSJarkko Nikula type = (int)adev_id->driver_data; 132834cadd9cSJarkko Nikula else if (pcidev_id) 132934cadd9cSJarkko Nikula type = (int)pcidev_id->driver_data; 133003fbf488SJarkko Nikula else 133103fbf488SJarkko Nikula return NULL; 133203fbf488SJarkko Nikula 1333cc0ee987SMika Westerberg pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 13349deae459SJingoo Han if (!pdata) 1335a3496855SMika Westerberg return NULL; 1336a3496855SMika Westerberg 1337a3496855SMika Westerberg res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1338a3496855SMika Westerberg if (!res) 1339a3496855SMika Westerberg return NULL; 1340a3496855SMika Westerberg 1341a3496855SMika Westerberg ssp = &pdata->ssp; 1342a3496855SMika Westerberg 1343a3496855SMika Westerberg ssp->phys_base = res->start; 1344cbfd6a21SSachin Kamat ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); 1345cbfd6a21SSachin Kamat if (IS_ERR(ssp->mmio_base)) 13466dc81f6fSMika Westerberg return NULL; 1347a3496855SMika Westerberg 134834cadd9cSJarkko Nikula if (pcidev_id) { 134934cadd9cSJarkko Nikula pdata->tx_param = pdev->dev.parent; 135034cadd9cSJarkko Nikula pdata->rx_param = pdev->dev.parent; 135134cadd9cSJarkko Nikula pdata->dma_filter = pxa2xx_spi_idma_filter; 135234cadd9cSJarkko Nikula } 135334cadd9cSJarkko Nikula 1354a3496855SMika Westerberg ssp->clk = devm_clk_get(&pdev->dev, NULL); 1355a3496855SMika Westerberg ssp->irq = platform_get_irq(pdev, 0); 135603fbf488SJarkko Nikula ssp->type = type; 1357a3496855SMika Westerberg ssp->pdev = pdev; 1358a3496855SMika Westerberg 1359a3496855SMika Westerberg ssp->port_id = -1; 1360a3496855SMika Westerberg if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid)) 1361a3496855SMika Westerberg ssp->port_id = devid; 1362a3496855SMika Westerberg 1363a3496855SMika Westerberg pdata->num_chipselect = 1; 1364cddb339bSMika Westerberg pdata->enable_dma = true; 1365a3496855SMika Westerberg 1366a3496855SMika Westerberg return pdata; 1367a3496855SMika Westerberg } 1368a3496855SMika Westerberg 1369a3496855SMika Westerberg #else 1370a3496855SMika Westerberg static inline struct pxa2xx_spi_master * 1371a3496855SMika Westerberg pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) 1372a3496855SMika Westerberg { 1373a3496855SMika Westerberg return NULL; 1374a3496855SMika Westerberg } 1375a3496855SMika Westerberg #endif 1376a3496855SMika Westerberg 1377fd4a319bSGrant Likely static int pxa2xx_spi_probe(struct platform_device *pdev) 1378ca632f55SGrant Likely { 1379ca632f55SGrant Likely struct device *dev = &pdev->dev; 1380ca632f55SGrant Likely struct pxa2xx_spi_master *platform_info; 1381ca632f55SGrant Likely struct spi_master *master; 1382ca632f55SGrant Likely struct driver_data *drv_data; 1383ca632f55SGrant Likely struct ssp_device *ssp; 1384ca632f55SGrant Likely int status; 1385c039dd27SJarkko Nikula u32 tmp; 1386ca632f55SGrant Likely 1387851bacf5SMika Westerberg platform_info = dev_get_platdata(dev); 1388851bacf5SMika Westerberg if (!platform_info) { 1389a3496855SMika Westerberg platform_info = pxa2xx_spi_acpi_get_pdata(pdev); 1390a3496855SMika Westerberg if (!platform_info) { 1391851bacf5SMika Westerberg dev_err(&pdev->dev, "missing platform data\n"); 1392851bacf5SMika Westerberg return -ENODEV; 1393851bacf5SMika Westerberg } 1394a3496855SMika Westerberg } 1395ca632f55SGrant Likely 1396ca632f55SGrant Likely ssp = pxa_ssp_request(pdev->id, pdev->name); 1397851bacf5SMika Westerberg if (!ssp) 1398851bacf5SMika Westerberg ssp = &platform_info->ssp; 1399851bacf5SMika Westerberg 1400851bacf5SMika Westerberg if (!ssp->mmio_base) { 1401851bacf5SMika Westerberg dev_err(&pdev->dev, "failed to get ssp\n"); 1402ca632f55SGrant Likely return -ENODEV; 1403ca632f55SGrant Likely } 1404ca632f55SGrant Likely 1405757fe8d5SJarkko Nikula master = spi_alloc_master(dev, sizeof(struct driver_data)); 1406ca632f55SGrant Likely if (!master) { 1407ca632f55SGrant Likely dev_err(&pdev->dev, "cannot alloc spi_master\n"); 1408ca632f55SGrant Likely pxa_ssp_free(ssp); 1409ca632f55SGrant Likely return -ENOMEM; 1410ca632f55SGrant Likely } 1411ca632f55SGrant Likely drv_data = spi_master_get_devdata(master); 1412ca632f55SGrant Likely drv_data->master = master; 1413ca632f55SGrant Likely drv_data->master_info = platform_info; 1414ca632f55SGrant Likely drv_data->pdev = pdev; 1415ca632f55SGrant Likely drv_data->ssp = ssp; 1416ca632f55SGrant Likely 1417ca632f55SGrant Likely master->dev.parent = &pdev->dev; 1418ca632f55SGrant Likely master->dev.of_node = pdev->dev.of_node; 1419ca632f55SGrant Likely /* the spi->mode bits understood by this driver: */ 1420b833172fSMika Westerberg master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; 1421ca632f55SGrant Likely 1422851bacf5SMika Westerberg master->bus_num = ssp->port_id; 1423ca632f55SGrant Likely master->num_chipselect = platform_info->num_chipselect; 1424ca632f55SGrant Likely master->dma_alignment = DMA_ALIGNMENT; 1425ca632f55SGrant Likely master->cleanup = cleanup; 1426ca632f55SGrant Likely master->setup = setup; 14277f86bde9SMika Westerberg master->transfer_one_message = pxa2xx_spi_transfer_one_message; 14287d94a505SMika Westerberg master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; 14297dd62787SMark Brown master->auto_runtime_pm = true; 1430ca632f55SGrant Likely 1431ca632f55SGrant Likely drv_data->ssp_type = ssp->type; 1432ca632f55SGrant Likely 1433ca632f55SGrant Likely drv_data->ioaddr = ssp->mmio_base; 1434ca632f55SGrant Likely drv_data->ssdr_physical = ssp->phys_base + SSDR; 1435ca632f55SGrant Likely if (pxa25x_ssp_comp(drv_data)) { 1436e5262d05SWeike Chen switch (drv_data->ssp_type) { 1437e5262d05SWeike Chen case QUARK_X1000_SSP: 1438e5262d05SWeike Chen master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1439e5262d05SWeike Chen break; 1440e5262d05SWeike Chen default: 144124778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); 1442e5262d05SWeike Chen break; 1443e5262d05SWeike Chen } 1444e5262d05SWeike Chen 1445ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; 1446ca632f55SGrant Likely drv_data->dma_cr1 = 0; 1447ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR; 1448ca632f55SGrant Likely drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; 1449ca632f55SGrant Likely } else { 145024778be2SStephen Warren master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); 1451ca632f55SGrant Likely drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; 14525928808eSMika Westerberg drv_data->dma_cr1 = DEFAULT_DMA_CR1; 1453ca632f55SGrant Likely drv_data->clear_sr = SSSR_ROR | SSSR_TINT; 1454ca632f55SGrant Likely drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; 1455ca632f55SGrant Likely } 1456ca632f55SGrant Likely 1457ca632f55SGrant Likely status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), 1458ca632f55SGrant Likely drv_data); 1459ca632f55SGrant Likely if (status < 0) { 1460ca632f55SGrant Likely dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); 1461ca632f55SGrant Likely goto out_error_master_alloc; 1462ca632f55SGrant Likely } 1463ca632f55SGrant Likely 1464ca632f55SGrant Likely /* Setup DMA if requested */ 1465ca632f55SGrant Likely if (platform_info->enable_dma) { 1466cd7bed00SMika Westerberg status = pxa2xx_spi_dma_setup(drv_data); 1467cd7bed00SMika Westerberg if (status) { 1468cddb339bSMika Westerberg dev_dbg(dev, "no DMA channels available, using PIO\n"); 1469cd7bed00SMika Westerberg platform_info->enable_dma = false; 1470ca632f55SGrant Likely } 1471ca632f55SGrant Likely } 1472ca632f55SGrant Likely 1473ca632f55SGrant Likely /* Enable SOC clock */ 14743343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 14753343b7a6SMika Westerberg 1476*0eca7cf2SJarkko Nikula master->max_speed_hz = clk_get_rate(ssp->clk); 1477ca632f55SGrant Likely 1478ca632f55SGrant Likely /* Load default SSP configuration */ 1479c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 1480e5262d05SWeike Chen switch (drv_data->ssp_type) { 1481e5262d05SWeike Chen case QUARK_X1000_SSP: 1482c039dd27SJarkko Nikula tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) 1483c039dd27SJarkko Nikula | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); 1484c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1485e5262d05SWeike Chen 1486e5262d05SWeike Chen /* using the Motorola SPI protocol and use 8 bit frame */ 1487c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 1488c039dd27SJarkko Nikula QUARK_X1000_SSCR0_Motorola 1489c039dd27SJarkko Nikula | QUARK_X1000_SSCR0_DataSize(8)); 1490e5262d05SWeike Chen break; 1491e5262d05SWeike Chen default: 1492c039dd27SJarkko Nikula tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | 1493c039dd27SJarkko Nikula SSCR1_TxTresh(TX_THRESH_DFLT); 1494c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR1, tmp); 1495c039dd27SJarkko Nikula tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); 1496c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, tmp); 1497e5262d05SWeike Chen break; 1498e5262d05SWeike Chen } 1499e5262d05SWeike Chen 1500ca632f55SGrant Likely if (!pxa25x_ssp_comp(drv_data)) 1501c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSTO, 0); 1502e5262d05SWeike Chen 1503e5262d05SWeike Chen if (!is_quark_x1000_ssp(drv_data)) 1504c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSPSP, 0); 1505ca632f55SGrant Likely 15067566bcc7SJarkko Nikula if (is_lpss_ssp(drv_data)) 1507a0d2642eSMika Westerberg lpss_ssp_setup(drv_data); 1508a0d2642eSMika Westerberg 15097f86bde9SMika Westerberg tasklet_init(&drv_data->pump_transfers, pump_transfers, 15107f86bde9SMika Westerberg (unsigned long)drv_data); 1511ca632f55SGrant Likely 1512836d1a22SAntonio Ospite pm_runtime_set_autosuspend_delay(&pdev->dev, 50); 1513836d1a22SAntonio Ospite pm_runtime_use_autosuspend(&pdev->dev); 1514836d1a22SAntonio Ospite pm_runtime_set_active(&pdev->dev); 1515836d1a22SAntonio Ospite pm_runtime_enable(&pdev->dev); 1516836d1a22SAntonio Ospite 1517ca632f55SGrant Likely /* Register with the SPI framework */ 1518ca632f55SGrant Likely platform_set_drvdata(pdev, drv_data); 1519a807fcd0SJingoo Han status = devm_spi_register_master(&pdev->dev, master); 1520ca632f55SGrant Likely if (status != 0) { 1521ca632f55SGrant Likely dev_err(&pdev->dev, "problem registering spi master\n"); 15227f86bde9SMika Westerberg goto out_error_clock_enabled; 1523ca632f55SGrant Likely } 1524ca632f55SGrant Likely 1525ca632f55SGrant Likely return status; 1526ca632f55SGrant Likely 1527ca632f55SGrant Likely out_error_clock_enabled: 15283343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1529cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1530ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1531ca632f55SGrant Likely 1532ca632f55SGrant Likely out_error_master_alloc: 1533ca632f55SGrant Likely spi_master_put(master); 1534ca632f55SGrant Likely pxa_ssp_free(ssp); 1535ca632f55SGrant Likely return status; 1536ca632f55SGrant Likely } 1537ca632f55SGrant Likely 1538ca632f55SGrant Likely static int pxa2xx_spi_remove(struct platform_device *pdev) 1539ca632f55SGrant Likely { 1540ca632f55SGrant Likely struct driver_data *drv_data = platform_get_drvdata(pdev); 1541ca632f55SGrant Likely struct ssp_device *ssp; 1542ca632f55SGrant Likely 1543ca632f55SGrant Likely if (!drv_data) 1544ca632f55SGrant Likely return 0; 1545ca632f55SGrant Likely ssp = drv_data->ssp; 1546ca632f55SGrant Likely 15477d94a505SMika Westerberg pm_runtime_get_sync(&pdev->dev); 15487d94a505SMika Westerberg 1549ca632f55SGrant Likely /* Disable the SSP at the peripheral and SOC level */ 1550c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 15513343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1552ca632f55SGrant Likely 1553ca632f55SGrant Likely /* Release DMA */ 1554cd7bed00SMika Westerberg if (drv_data->master_info->enable_dma) 1555cd7bed00SMika Westerberg pxa2xx_spi_dma_release(drv_data); 1556ca632f55SGrant Likely 15577d94a505SMika Westerberg pm_runtime_put_noidle(&pdev->dev); 15587d94a505SMika Westerberg pm_runtime_disable(&pdev->dev); 15597d94a505SMika Westerberg 1560ca632f55SGrant Likely /* Release IRQ */ 1561ca632f55SGrant Likely free_irq(ssp->irq, drv_data); 1562ca632f55SGrant Likely 1563ca632f55SGrant Likely /* Release SSP */ 1564ca632f55SGrant Likely pxa_ssp_free(ssp); 1565ca632f55SGrant Likely 1566ca632f55SGrant Likely return 0; 1567ca632f55SGrant Likely } 1568ca632f55SGrant Likely 1569ca632f55SGrant Likely static void pxa2xx_spi_shutdown(struct platform_device *pdev) 1570ca632f55SGrant Likely { 1571ca632f55SGrant Likely int status = 0; 1572ca632f55SGrant Likely 1573ca632f55SGrant Likely if ((status = pxa2xx_spi_remove(pdev)) != 0) 1574ca632f55SGrant Likely dev_err(&pdev->dev, "shutdown failed with %d\n", status); 1575ca632f55SGrant Likely } 1576ca632f55SGrant Likely 1577382cebb0SMika Westerberg #ifdef CONFIG_PM_SLEEP 1578ca632f55SGrant Likely static int pxa2xx_spi_suspend(struct device *dev) 1579ca632f55SGrant Likely { 1580ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1581ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1582ca632f55SGrant Likely int status = 0; 1583ca632f55SGrant Likely 15847f86bde9SMika Westerberg status = spi_master_suspend(drv_data->master); 1585ca632f55SGrant Likely if (status != 0) 1586ca632f55SGrant Likely return status; 1587c039dd27SJarkko Nikula pxa2xx_spi_write(drv_data, SSCR0, 0); 15882b9375b9SDmitry Eremin-Solenikov 15892b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 15903343b7a6SMika Westerberg clk_disable_unprepare(ssp->clk); 1591ca632f55SGrant Likely 1592ca632f55SGrant Likely return 0; 1593ca632f55SGrant Likely } 1594ca632f55SGrant Likely 1595ca632f55SGrant Likely static int pxa2xx_spi_resume(struct device *dev) 1596ca632f55SGrant Likely { 1597ca632f55SGrant Likely struct driver_data *drv_data = dev_get_drvdata(dev); 1598ca632f55SGrant Likely struct ssp_device *ssp = drv_data->ssp; 1599ca632f55SGrant Likely int status = 0; 1600ca632f55SGrant Likely 1601cd7bed00SMika Westerberg pxa2xx_spi_dma_resume(drv_data); 1602ca632f55SGrant Likely 1603ca632f55SGrant Likely /* Enable the SSP clock */ 16042b9375b9SDmitry Eremin-Solenikov if (!pm_runtime_suspended(dev)) 16053343b7a6SMika Westerberg clk_prepare_enable(ssp->clk); 1606ca632f55SGrant Likely 1607c50325f7SChew, Chiau Ee /* Restore LPSS private register bits */ 160848421adfSJarkko Nikula if (is_lpss_ssp(drv_data)) 1609c50325f7SChew, Chiau Ee lpss_ssp_setup(drv_data); 1610c50325f7SChew, Chiau Ee 1611ca632f55SGrant Likely /* Start the queue running */ 16127f86bde9SMika Westerberg status = spi_master_resume(drv_data->master); 1613ca632f55SGrant Likely if (status != 0) { 1614ca632f55SGrant Likely dev_err(dev, "problem starting queue (%d)\n", status); 1615ca632f55SGrant Likely return status; 1616ca632f55SGrant Likely } 1617ca632f55SGrant Likely 1618ca632f55SGrant Likely return 0; 1619ca632f55SGrant Likely } 16207d94a505SMika Westerberg #endif 16217d94a505SMika Westerberg 1622ec833050SRafael J. Wysocki #ifdef CONFIG_PM 16237d94a505SMika Westerberg static int pxa2xx_spi_runtime_suspend(struct device *dev) 16247d94a505SMika Westerberg { 16257d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 16267d94a505SMika Westerberg 16277d94a505SMika Westerberg clk_disable_unprepare(drv_data->ssp->clk); 16287d94a505SMika Westerberg return 0; 16297d94a505SMika Westerberg } 16307d94a505SMika Westerberg 16317d94a505SMika Westerberg static int pxa2xx_spi_runtime_resume(struct device *dev) 16327d94a505SMika Westerberg { 16337d94a505SMika Westerberg struct driver_data *drv_data = dev_get_drvdata(dev); 16347d94a505SMika Westerberg 16357d94a505SMika Westerberg clk_prepare_enable(drv_data->ssp->clk); 16367d94a505SMika Westerberg return 0; 16377d94a505SMika Westerberg } 16387d94a505SMika Westerberg #endif 1639ca632f55SGrant Likely 1640ca632f55SGrant Likely static const struct dev_pm_ops pxa2xx_spi_pm_ops = { 16417d94a505SMika Westerberg SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) 16427d94a505SMika Westerberg SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, 16437d94a505SMika Westerberg pxa2xx_spi_runtime_resume, NULL) 1644ca632f55SGrant Likely }; 1645ca632f55SGrant Likely 1646ca632f55SGrant Likely static struct platform_driver driver = { 1647ca632f55SGrant Likely .driver = { 1648ca632f55SGrant Likely .name = "pxa2xx-spi", 1649ca632f55SGrant Likely .pm = &pxa2xx_spi_pm_ops, 1650a3496855SMika Westerberg .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), 1651ca632f55SGrant Likely }, 1652ca632f55SGrant Likely .probe = pxa2xx_spi_probe, 1653ca632f55SGrant Likely .remove = pxa2xx_spi_remove, 1654ca632f55SGrant Likely .shutdown = pxa2xx_spi_shutdown, 1655ca632f55SGrant Likely }; 1656ca632f55SGrant Likely 1657ca632f55SGrant Likely static int __init pxa2xx_spi_init(void) 1658ca632f55SGrant Likely { 1659ca632f55SGrant Likely return platform_driver_register(&driver); 1660ca632f55SGrant Likely } 1661ca632f55SGrant Likely subsys_initcall(pxa2xx_spi_init); 1662ca632f55SGrant Likely 1663ca632f55SGrant Likely static void __exit pxa2xx_spi_exit(void) 1664ca632f55SGrant Likely { 1665ca632f55SGrant Likely platform_driver_unregister(&driver); 1666ca632f55SGrant Likely } 1667ca632f55SGrant Likely module_exit(pxa2xx_spi_exit); 1668