Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 3 | * Copyright (C) 2013, Intel Corporation |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/ioport.h> |
| 20 | #include <linux/errno.h> |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 21 | #include <linux/err.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 24 | #include <linux/pci.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | 8348c25 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 26 | #include <linux/spi/pxa2xx_spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 27 | #include <linux/spi/spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 28 | #include <linux/delay.h> |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 29 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 31 | #include <linux/clk.h> |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 33 | #include <linux/acpi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 34 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 35 | #include "spi-pxa2xx.h" |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 36 | |
| 37 | MODULE_AUTHOR("Stephen Street"); |
Will Newton | 037cdaf | 2007-12-10 15:49:25 -0800 | [diff] [blame] | 38 | MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 39 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 40 | MODULE_ALIAS("platform:pxa2xx-spi"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 41 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 42 | #define TIMOUT_DFLT 1000 |
| 43 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 44 | /* |
| 45 | * for testing SSCR1 changes that require SSP restart, basically |
| 46 | * everything except the service and interrupt enables, the pxa270 developer |
| 47 | * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this |
| 48 | * list, but the PXA255 dev man says all bits without really meaning the |
| 49 | * service and interrupt enables |
| 50 | */ |
| 51 | #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 52 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 53 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
| 54 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ |
| 55 | | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ |
| 56 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 57 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 58 | #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ |
| 59 | | QUARK_X1000_SSCR1_EFWR \ |
| 60 | | QUARK_X1000_SSCR1_RFT \ |
| 61 | | QUARK_X1000_SSCR1_TFT \ |
| 62 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
| 63 | |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 64 | #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 65 | #define SPI_CS_CONTROL_SW_MODE BIT(0) |
| 66 | #define SPI_CS_CONTROL_CS_HIGH BIT(1) |
| 67 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 68 | struct lpss_config { |
| 69 | /* LPSS offset from drv_data->ioaddr */ |
| 70 | unsigned offset; |
| 71 | /* Register offsets from drv_data->lpss_base or -1 */ |
| 72 | int reg_general; |
| 73 | int reg_ssp; |
| 74 | int reg_cs_ctrl; |
| 75 | /* FIFO thresholds */ |
| 76 | u32 rx_threshold; |
| 77 | u32 tx_threshold_lo; |
| 78 | u32 tx_threshold_hi; |
| 79 | }; |
| 80 | |
| 81 | /* Keep these sorted with enum pxa_ssp_type */ |
| 82 | static const struct lpss_config lpss_platforms[] = { |
| 83 | { /* LPSS_LPT_SSP */ |
| 84 | .offset = 0x800, |
| 85 | .reg_general = 0x08, |
| 86 | .reg_ssp = 0x0c, |
| 87 | .reg_cs_ctrl = 0x18, |
| 88 | .rx_threshold = 64, |
| 89 | .tx_threshold_lo = 160, |
| 90 | .tx_threshold_hi = 224, |
| 91 | }, |
| 92 | { /* LPSS_BYT_SSP */ |
| 93 | .offset = 0x400, |
| 94 | .reg_general = 0x08, |
| 95 | .reg_ssp = 0x0c, |
| 96 | .reg_cs_ctrl = 0x18, |
| 97 | .rx_threshold = 64, |
| 98 | .tx_threshold_lo = 160, |
| 99 | .tx_threshold_hi = 224, |
| 100 | }, |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 101 | { /* LPSS_SPT_SSP */ |
| 102 | .offset = 0x200, |
| 103 | .reg_general = -1, |
| 104 | .reg_ssp = 0x20, |
| 105 | .reg_cs_ctrl = 0x24, |
| 106 | .rx_threshold = 1, |
| 107 | .tx_threshold_lo = 32, |
| 108 | .tx_threshold_hi = 56, |
| 109 | }, |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | static inline const struct lpss_config |
| 113 | *lpss_get_config(const struct driver_data *drv_data) |
| 114 | { |
| 115 | return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; |
| 116 | } |
| 117 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 118 | static bool is_lpss_ssp(const struct driver_data *drv_data) |
| 119 | { |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 120 | switch (drv_data->ssp_type) { |
| 121 | case LPSS_LPT_SSP: |
| 122 | case LPSS_BYT_SSP: |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 123 | case LPSS_SPT_SSP: |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 124 | return true; |
| 125 | default: |
| 126 | return false; |
| 127 | } |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 130 | static bool is_quark_x1000_ssp(const struct driver_data *drv_data) |
| 131 | { |
| 132 | return drv_data->ssp_type == QUARK_X1000_SSP; |
| 133 | } |
| 134 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 135 | static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) |
| 136 | { |
| 137 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 138 | case QUARK_X1000_SSP: |
| 139 | return QUARK_X1000_SSCR1_CHANGE_MASK; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 140 | default: |
| 141 | return SSCR1_CHANGE_MASK; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | static u32 |
| 146 | pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) |
| 147 | { |
| 148 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 149 | case QUARK_X1000_SSP: |
| 150 | return RX_THRESH_QUARK_X1000_DFLT; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 151 | default: |
| 152 | return RX_THRESH_DFLT; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) |
| 157 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 158 | u32 mask; |
| 159 | |
| 160 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 161 | case QUARK_X1000_SSP: |
| 162 | mask = QUARK_X1000_SSSR_TFL_MASK; |
| 163 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 164 | default: |
| 165 | mask = SSSR_TFL_MASK; |
| 166 | break; |
| 167 | } |
| 168 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 169 | return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, |
| 173 | u32 *sccr1_reg) |
| 174 | { |
| 175 | u32 mask; |
| 176 | |
| 177 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 178 | case QUARK_X1000_SSP: |
| 179 | mask = QUARK_X1000_SSCR1_RFT; |
| 180 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 181 | default: |
| 182 | mask = SSCR1_RFT; |
| 183 | break; |
| 184 | } |
| 185 | *sccr1_reg &= ~mask; |
| 186 | } |
| 187 | |
| 188 | static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, |
| 189 | u32 *sccr1_reg, u32 threshold) |
| 190 | { |
| 191 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 192 | case QUARK_X1000_SSP: |
| 193 | *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); |
| 194 | break; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 195 | default: |
| 196 | *sccr1_reg |= SSCR1_RxTresh(threshold); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, |
| 202 | u32 clk_div, u8 bits) |
| 203 | { |
| 204 | switch (drv_data->ssp_type) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 205 | case QUARK_X1000_SSP: |
| 206 | return clk_div |
| 207 | | QUARK_X1000_SSCR0_Motorola |
| 208 | | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits) |
| 209 | | SSCR0_SSE; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 210 | default: |
| 211 | return clk_div |
| 212 | | SSCR0_Motorola |
| 213 | | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) |
| 214 | | SSCR0_SSE |
| 215 | | (bits > 16 ? SSCR0_EDSS : 0); |
| 216 | } |
| 217 | } |
| 218 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 219 | /* |
| 220 | * Read and write LPSS SSP private registers. Caller must first check that |
| 221 | * is_lpss_ssp() returns true before these can be called. |
| 222 | */ |
| 223 | static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) |
| 224 | { |
| 225 | WARN_ON(!drv_data->lpss_base); |
| 226 | return readl(drv_data->lpss_base + offset); |
| 227 | } |
| 228 | |
| 229 | static void __lpss_ssp_write_priv(struct driver_data *drv_data, |
| 230 | unsigned offset, u32 value) |
| 231 | { |
| 232 | WARN_ON(!drv_data->lpss_base); |
| 233 | writel(value, drv_data->lpss_base + offset); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * lpss_ssp_setup - perform LPSS SSP specific setup |
| 238 | * @drv_data: pointer to the driver private data |
| 239 | * |
| 240 | * Perform LPSS SSP specific setup. This function must be called first if |
| 241 | * one is going to use LPSS SSP private registers. |
| 242 | */ |
| 243 | static void lpss_ssp_setup(struct driver_data *drv_data) |
| 244 | { |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 245 | const struct lpss_config *config; |
| 246 | u32 value; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 247 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 248 | config = lpss_get_config(drv_data); |
| 249 | drv_data->lpss_base = drv_data->ioaddr + config->offset; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 250 | |
| 251 | /* Enable software chip select control */ |
| 252 | value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 253 | __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); |
Mika Westerberg | 0054e28 | 2013-03-05 12:05:17 +0200 | [diff] [blame] | 254 | |
| 255 | /* Enable multiblock DMA transfers */ |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 256 | if (drv_data->master_info->enable_dma) { |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 257 | __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1); |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 258 | |
Jarkko Nikula | 82ba2c2 | 2015-06-04 16:55:12 +0300 | [diff] [blame] | 259 | if (config->reg_general >= 0) { |
| 260 | value = __lpss_ssp_read_priv(drv_data, |
| 261 | config->reg_general); |
| 262 | value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE; |
| 263 | __lpss_ssp_write_priv(drv_data, |
| 264 | config->reg_general, value); |
| 265 | } |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame] | 266 | } |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable) |
| 270 | { |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 271 | const struct lpss_config *config; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 272 | u32 value; |
| 273 | |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 274 | config = lpss_get_config(drv_data); |
| 275 | |
| 276 | value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 277 | if (enable) |
| 278 | value &= ~SPI_CS_CONTROL_CS_HIGH; |
| 279 | else |
| 280 | value |= SPI_CS_CONTROL_CS_HIGH; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 281 | __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 284 | static void cs_assert(struct driver_data *drv_data) |
| 285 | { |
| 286 | struct chip_data *chip = drv_data->cur_chip; |
| 287 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 288 | if (drv_data->ssp_type == CE4100_SSP) { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 289 | pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 293 | if (chip->cs_control) { |
| 294 | chip->cs_control(PXA2XX_CS_ASSERT); |
| 295 | return; |
| 296 | } |
| 297 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 298 | if (gpio_is_valid(chip->gpio_cs)) { |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 299 | gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 300 | return; |
| 301 | } |
| 302 | |
Jarkko Nikula | 7566bcc | 2014-12-18 15:04:20 +0200 | [diff] [blame] | 303 | if (is_lpss_ssp(drv_data)) |
| 304 | lpss_ssp_cs_control(drv_data, true); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void cs_deassert(struct driver_data *drv_data) |
| 308 | { |
| 309 | struct chip_data *chip = drv_data->cur_chip; |
| 310 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 311 | if (drv_data->ssp_type == CE4100_SSP) |
| 312 | return; |
| 313 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 314 | if (chip->cs_control) { |
Daniel Ribeiro | 2b2562d | 2009-04-08 22:48:03 -0300 | [diff] [blame] | 315 | chip->cs_control(PXA2XX_CS_DEASSERT); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 319 | if (gpio_is_valid(chip->gpio_cs)) { |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 320 | gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | |
Jarkko Nikula | 7566bcc | 2014-12-18 15:04:20 +0200 | [diff] [blame] | 324 | if (is_lpss_ssp(drv_data)) |
| 325 | lpss_ssp_cs_control(drv_data, false); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 328 | int pxa2xx_spi_flush(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 329 | { |
| 330 | unsigned long limit = loops_per_jiffy << 1; |
| 331 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 332 | do { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 333 | while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 334 | pxa2xx_spi_read(drv_data, SSDR); |
| 335 | } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 336 | write_SSSR_CS(drv_data, SSSR_ROR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 337 | |
| 338 | return limit; |
| 339 | } |
| 340 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 341 | static int null_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 342 | { |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 343 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 344 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 345 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 346 | || (drv_data->tx == drv_data->tx_end)) |
| 347 | return 0; |
| 348 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 349 | pxa2xx_spi_write(drv_data, SSDR, 0); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 350 | drv_data->tx += n_bytes; |
| 351 | |
| 352 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 355 | static int null_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 356 | { |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 357 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 358 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 359 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 360 | && (drv_data->rx < drv_data->rx_end)) { |
| 361 | pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 362 | drv_data->rx += n_bytes; |
| 363 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 364 | |
| 365 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 368 | static int u8_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 369 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 370 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 371 | || (drv_data->tx == drv_data->tx_end)) |
| 372 | return 0; |
| 373 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 374 | pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 375 | ++drv_data->tx; |
| 376 | |
| 377 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 380 | static int u8_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 381 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 382 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 383 | && (drv_data->rx < drv_data->rx_end)) { |
| 384 | *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 385 | ++drv_data->rx; |
| 386 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 387 | |
| 388 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 391 | static int u16_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 392 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 393 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 394 | || (drv_data->tx == drv_data->tx_end)) |
| 395 | return 0; |
| 396 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 397 | pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 398 | drv_data->tx += 2; |
| 399 | |
| 400 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 403 | static int u16_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 404 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 405 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 406 | && (drv_data->rx < drv_data->rx_end)) { |
| 407 | *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 408 | drv_data->rx += 2; |
| 409 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 410 | |
| 411 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 412 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 413 | |
| 414 | static int u32_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 415 | { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 416 | if (pxa2xx_spi_txfifo_full(drv_data) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 417 | || (drv_data->tx == drv_data->tx_end)) |
| 418 | return 0; |
| 419 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 420 | pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 421 | drv_data->tx += 4; |
| 422 | |
| 423 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 426 | static int u32_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 427 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 428 | while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE) |
| 429 | && (drv_data->rx < drv_data->rx_end)) { |
| 430 | *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 431 | drv_data->rx += 4; |
| 432 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 433 | |
| 434 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 437 | void *pxa2xx_spi_next_transfer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 438 | { |
| 439 | struct spi_message *msg = drv_data->cur_msg; |
| 440 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 441 | |
| 442 | /* Move to next transfer */ |
| 443 | if (trans->transfer_list.next != &msg->transfers) { |
| 444 | drv_data->cur_transfer = |
| 445 | list_entry(trans->transfer_list.next, |
| 446 | struct spi_transfer, |
| 447 | transfer_list); |
| 448 | return RUNNING_STATE; |
| 449 | } else |
| 450 | return DONE_STATE; |
| 451 | } |
| 452 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 453 | /* caller already set message->status; dma and pio irqs are blocked */ |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 454 | static void giveback(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 455 | { |
| 456 | struct spi_transfer* last_transfer; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 457 | struct spi_message *msg; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 458 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 459 | msg = drv_data->cur_msg; |
| 460 | drv_data->cur_msg = NULL; |
| 461 | drv_data->cur_transfer = NULL; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 462 | |
Axel Lin | 23e2c2a | 2014-02-12 22:13:27 +0800 | [diff] [blame] | 463 | last_transfer = list_last_entry(&msg->transfers, struct spi_transfer, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 464 | transfer_list); |
| 465 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 466 | /* Delay if requested before any change in chip select */ |
| 467 | if (last_transfer->delay_usecs) |
| 468 | udelay(last_transfer->delay_usecs); |
| 469 | |
| 470 | /* Drop chip select UNLESS cs_change is true or we are returning |
| 471 | * a message with an error, or next message is for another chip |
| 472 | */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 473 | if (!last_transfer->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 474 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 475 | else { |
| 476 | struct spi_message *next_msg; |
| 477 | |
| 478 | /* Holding of cs was hinted, but we need to make sure |
| 479 | * the next message is for the same chip. Don't waste |
| 480 | * time with the following tests unless this was hinted. |
| 481 | * |
| 482 | * We cannot postpone this until pump_messages, because |
| 483 | * after calling msg->complete (below) the driver that |
| 484 | * sent the current message could be unloaded, which |
| 485 | * could invalidate the cs_control() callback... |
| 486 | */ |
| 487 | |
| 488 | /* get a pointer to the next message, if any */ |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 489 | next_msg = spi_get_next_queued_message(drv_data->master); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 490 | |
| 491 | /* see if the next and current messages point |
| 492 | * to the same chip |
| 493 | */ |
| 494 | if (next_msg && next_msg->spi != msg->spi) |
| 495 | next_msg = NULL; |
| 496 | if (!next_msg || msg->state == ERROR_STATE) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 497 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 498 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 499 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 500 | drv_data->cur_chip = NULL; |
Mika Westerberg | c957e8f | 2014-12-29 10:33:36 +0200 | [diff] [blame] | 501 | spi_finalize_current_message(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 504 | static void reset_sccr1(struct driver_data *drv_data) |
| 505 | { |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 506 | struct chip_data *chip = drv_data->cur_chip; |
| 507 | u32 sccr1_reg; |
| 508 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 509 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1; |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 510 | sccr1_reg &= ~SSCR1_RFT; |
| 511 | sccr1_reg |= chip->threshold; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 512 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 515 | static void int_error_stop(struct driver_data *drv_data, const char* msg) |
| 516 | { |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 517 | /* Stop and reset SSP */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 518 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 519 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 520 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 521 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 522 | pxa2xx_spi_flush(drv_data); |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 523 | pxa2xx_spi_write(drv_data, SSCR0, |
| 524 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 525 | |
| 526 | dev_err(&drv_data->pdev->dev, "%s\n", msg); |
| 527 | |
| 528 | drv_data->cur_msg->state = ERROR_STATE; |
| 529 | tasklet_schedule(&drv_data->pump_transfers); |
| 530 | } |
| 531 | |
| 532 | static void int_transfer_complete(struct driver_data *drv_data) |
| 533 | { |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 534 | /* Stop SSP */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 535 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 536 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 537 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 538 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 539 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 540 | /* Update total byte transferred return count actual bytes read */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 541 | drv_data->cur_msg->actual_length += drv_data->len - |
| 542 | (drv_data->rx_end - drv_data->rx); |
| 543 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 544 | /* Transfer delays and chip select release are |
| 545 | * handled in pump_transfers or giveback |
| 546 | */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 547 | |
| 548 | /* Move to next transfer */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 549 | drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 550 | |
| 551 | /* Schedule transfer tasklet */ |
| 552 | tasklet_schedule(&drv_data->pump_transfers); |
| 553 | } |
| 554 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 555 | static irqreturn_t interrupt_transfer(struct driver_data *drv_data) |
| 556 | { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 557 | u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ? |
| 558 | drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 559 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 560 | u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 561 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 562 | if (irq_status & SSSR_ROR) { |
| 563 | int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); |
| 564 | return IRQ_HANDLED; |
| 565 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 566 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 567 | if (irq_status & SSSR_TINT) { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 568 | pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 569 | if (drv_data->read(drv_data)) { |
| 570 | int_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 571 | return IRQ_HANDLED; |
| 572 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 573 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 574 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 575 | /* Drain rx fifo, Fill tx fifo and prevent overruns */ |
| 576 | do { |
| 577 | if (drv_data->read(drv_data)) { |
| 578 | int_transfer_complete(drv_data); |
| 579 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 580 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 581 | } while (drv_data->write(drv_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 582 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 583 | if (drv_data->read(drv_data)) { |
| 584 | int_transfer_complete(drv_data); |
| 585 | return IRQ_HANDLED; |
| 586 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 587 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 588 | if (drv_data->tx == drv_data->tx_end) { |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 589 | u32 bytes_left; |
| 590 | u32 sccr1_reg; |
| 591 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 592 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 593 | sccr1_reg &= ~SSCR1_TIE; |
| 594 | |
| 595 | /* |
| 596 | * PXA25x_SSP has no timeout, set up rx threshould for the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 597 | * remaining RX bytes. |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 598 | */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 599 | if (pxa25x_ssp_comp(drv_data)) { |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 600 | u32 rx_thre; |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 601 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 602 | pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 603 | |
| 604 | bytes_left = drv_data->rx_end - drv_data->rx; |
| 605 | switch (drv_data->n_bytes) { |
| 606 | case 4: |
| 607 | bytes_left >>= 1; |
| 608 | case 2: |
| 609 | bytes_left >>= 1; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 610 | } |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 611 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 612 | rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); |
| 613 | if (rx_thre > bytes_left) |
| 614 | rx_thre = bytes_left; |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 615 | |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 616 | pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 617 | } |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 618 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 621 | /* We did something */ |
| 622 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 623 | } |
| 624 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 625 | static irqreturn_t ssp_int(int irq, void *dev_id) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 626 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 627 | struct driver_data *drv_data = dev_id; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 628 | u32 sccr1_reg; |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 629 | u32 mask = drv_data->mask_sr; |
| 630 | u32 status; |
| 631 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 632 | /* |
| 633 | * The IRQ might be shared with other peripherals so we must first |
| 634 | * check that are we RPM suspended or not. If we are we assume that |
| 635 | * the IRQ was not for us (we shouldn't be RPM suspended when the |
| 636 | * interrupt is enabled). |
| 637 | */ |
| 638 | if (pm_runtime_suspended(&drv_data->pdev->dev)) |
| 639 | return IRQ_NONE; |
| 640 | |
Mika Westerberg | 269e4a4 | 2013-09-04 13:37:43 +0300 | [diff] [blame] | 641 | /* |
| 642 | * If the device is not yet in RPM suspended state and we get an |
| 643 | * interrupt that is meant for another device, check if status bits |
| 644 | * are all set to one. That means that the device is already |
| 645 | * powered off. |
| 646 | */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 647 | status = pxa2xx_spi_read(drv_data, SSSR); |
Mika Westerberg | 269e4a4 | 2013-09-04 13:37:43 +0300 | [diff] [blame] | 648 | if (status == ~0) |
| 649 | return IRQ_NONE; |
| 650 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 651 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 652 | |
| 653 | /* Ignore possible writes if we don't need to write */ |
| 654 | if (!(sccr1_reg & SSCR1_TIE)) |
| 655 | mask &= ~SSSR_TFS; |
| 656 | |
| 657 | if (!(status & mask)) |
| 658 | return IRQ_NONE; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 659 | |
| 660 | if (!drv_data->cur_msg) { |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 661 | |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 662 | pxa2xx_spi_write(drv_data, SSCR0, |
| 663 | pxa2xx_spi_read(drv_data, SSCR0) |
| 664 | & ~SSCR0_SSE); |
| 665 | pxa2xx_spi_write(drv_data, SSCR1, |
| 666 | pxa2xx_spi_read(drv_data, SSCR1) |
| 667 | & ~drv_data->int_cr1); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 668 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 669 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 670 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 671 | |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 672 | dev_err(&drv_data->pdev->dev, |
| 673 | "bad message state in interrupt handler\n"); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 674 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 675 | /* Never fail */ |
| 676 | return IRQ_HANDLED; |
| 677 | } |
| 678 | |
| 679 | return drv_data->transfer_handler(drv_data); |
| 680 | } |
| 681 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 682 | /* |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 683 | * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply |
| 684 | * input frequency by fractions of 2^24. It also has a divider by 5. |
| 685 | * |
| 686 | * There are formulas to get baud rate value for given input frequency and |
| 687 | * divider parameters, such as DDS_CLK_RATE and SCR: |
| 688 | * |
| 689 | * Fsys = 200MHz |
| 690 | * |
| 691 | * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) |
| 692 | * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) |
| 693 | * |
| 694 | * DDS_CLK_RATE either 2^n or 2^n / 5. |
| 695 | * SCR is in range 0 .. 255 |
| 696 | * |
| 697 | * Divisor = 5^i * 2^j * 2 * k |
| 698 | * i = [0, 1] i = 1 iff j = 0 or j > 3 |
| 699 | * j = [0, 23] j = 0 iff i = 1 |
| 700 | * k = [1, 256] |
| 701 | * Special case: j = 0, i = 1: Divisor = 2 / 5 |
| 702 | * |
| 703 | * Accordingly to the specification the recommended values for DDS_CLK_RATE |
| 704 | * are: |
| 705 | * Case 1: 2^n, n = [0, 23] |
| 706 | * Case 2: 2^24 * 2 / 5 (0x666666) |
| 707 | * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) |
| 708 | * |
| 709 | * In all cases the lowest possible value is better. |
| 710 | * |
| 711 | * The function calculates parameters for all cases and chooses the one closest |
| 712 | * to the asked baud rate. |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 713 | */ |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 714 | static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 715 | { |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 716 | unsigned long xtal = 200000000; |
| 717 | unsigned long fref = xtal / 2; /* mandatory division by 2, |
| 718 | see (2) */ |
| 719 | /* case 3 */ |
| 720 | unsigned long fref1 = fref / 2; /* case 1 */ |
| 721 | unsigned long fref2 = fref * 2 / 5; /* case 2 */ |
| 722 | unsigned long scale; |
| 723 | unsigned long q, q1, q2; |
| 724 | long r, r1, r2; |
| 725 | u32 mul; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 726 | |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 727 | /* Case 1 */ |
| 728 | |
| 729 | /* Set initial value for DDS_CLK_RATE */ |
| 730 | mul = (1 << 24) >> 1; |
| 731 | |
| 732 | /* Calculate initial quot */ |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 733 | q1 = DIV_ROUND_UP(fref1, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 734 | |
| 735 | /* Scale q1 if it's too big */ |
| 736 | if (q1 > 256) { |
| 737 | /* Scale q1 to range [1, 512] */ |
| 738 | scale = fls_long(q1 - 1); |
| 739 | if (scale > 9) { |
| 740 | q1 >>= scale - 9; |
| 741 | mul >>= scale - 9; |
| 742 | } |
| 743 | |
| 744 | /* Round the result if we have a remainder */ |
| 745 | q1 += q1 & 1; |
| 746 | } |
| 747 | |
| 748 | /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ |
| 749 | scale = __ffs(q1); |
| 750 | q1 >>= scale; |
| 751 | mul >>= scale; |
| 752 | |
| 753 | /* Get the remainder */ |
| 754 | r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); |
| 755 | |
| 756 | /* Case 2 */ |
| 757 | |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 758 | q2 = DIV_ROUND_UP(fref2, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 759 | r2 = abs(fref2 / q2 - rate); |
| 760 | |
| 761 | /* |
| 762 | * Choose the best between two: less remainder we have the better. We |
| 763 | * can't go case 2 if q2 is greater than 256 since SCR register can |
| 764 | * hold only values 0 .. 255. |
| 765 | */ |
| 766 | if (r2 >= r1 || q2 > 256) { |
| 767 | /* case 1 is better */ |
| 768 | r = r1; |
| 769 | q = q1; |
| 770 | } else { |
| 771 | /* case 2 is better */ |
| 772 | r = r2; |
| 773 | q = q2; |
| 774 | mul = (1 << 24) * 2 / 5; |
| 775 | } |
| 776 | |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 777 | /* Check case 3 only if the divisor is big enough */ |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 778 | if (fref / rate >= 80) { |
| 779 | u64 fssp; |
| 780 | u32 m; |
| 781 | |
| 782 | /* Calculate initial quot */ |
Andy Shevchenko | 3ad4806 | 2015-10-13 17:09:14 +0300 | [diff] [blame] | 783 | q1 = DIV_ROUND_UP(fref, rate); |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 784 | m = (1 << 24) / q1; |
| 785 | |
| 786 | /* Get the remainder */ |
| 787 | fssp = (u64)fref * m; |
| 788 | do_div(fssp, 1 << 24); |
| 789 | r1 = abs(fssp - rate); |
| 790 | |
| 791 | /* Choose this one if it suits better */ |
| 792 | if (r1 < r) { |
| 793 | /* case 3 is better */ |
| 794 | q = 1; |
| 795 | mul = m; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 799 | *dds = mul; |
| 800 | return q - 1; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 803 | static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 804 | { |
Jarkko Nikula | 0eca7cf | 2015-09-25 10:27:17 +0300 | [diff] [blame] | 805 | unsigned long ssp_clk = drv_data->master->max_speed_hz; |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 806 | const struct ssp_device *ssp = drv_data->ssp; |
| 807 | |
| 808 | rate = min_t(int, ssp_clk, rate); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 809 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 810 | if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 811 | return (ssp_clk / (2 * rate) - 1) & 0xff; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 812 | else |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 813 | return (ssp_clk / rate - 1) & 0xfff; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 814 | } |
| 815 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 816 | static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, |
Andy Shevchenko | d2c2f6a | 2015-10-22 16:44:40 +0300 | [diff] [blame^] | 817 | int rate) |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 818 | { |
Andy Shevchenko | d2c2f6a | 2015-10-22 16:44:40 +0300 | [diff] [blame^] | 819 | struct chip_data *chip = drv_data->cur_chip; |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 820 | unsigned int clk_div; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 821 | |
| 822 | switch (drv_data->ssp_type) { |
| 823 | case QUARK_X1000_SSP: |
Andy Shevchenko | 9df461e | 2015-03-25 15:06:16 +0200 | [diff] [blame] | 824 | clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); |
Dan Carpenter | eecacf7 | 2015-03-31 16:49:38 +0300 | [diff] [blame] | 825 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 826 | default: |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 827 | clk_div = ssp_get_clk_div(drv_data, rate); |
Dan Carpenter | eecacf7 | 2015-03-31 16:49:38 +0300 | [diff] [blame] | 828 | break; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 829 | } |
Andy Shevchenko | 025ffe8 | 2015-03-24 17:43:21 +0200 | [diff] [blame] | 830 | return clk_div << 8; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 833 | static void pump_transfers(unsigned long data) |
| 834 | { |
| 835 | struct driver_data *drv_data = (struct driver_data *)data; |
| 836 | struct spi_message *message = NULL; |
| 837 | struct spi_transfer *transfer = NULL; |
| 838 | struct spi_transfer *previous = NULL; |
| 839 | struct chip_data *chip = NULL; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 840 | u32 clk_div = 0; |
| 841 | u8 bits = 0; |
| 842 | u32 speed = 0; |
| 843 | u32 cr0; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 844 | u32 cr1; |
| 845 | u32 dma_thresh = drv_data->cur_chip->dma_threshold; |
| 846 | u32 dma_burst = drv_data->cur_chip->dma_burst_size; |
Weike Chen | 4fdb242 | 2014-10-08 08:50:22 -0700 | [diff] [blame] | 847 | u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 848 | |
| 849 | /* Get current state information */ |
| 850 | message = drv_data->cur_msg; |
| 851 | transfer = drv_data->cur_transfer; |
| 852 | chip = drv_data->cur_chip; |
| 853 | |
| 854 | /* Handle for abort */ |
| 855 | if (message->state == ERROR_STATE) { |
| 856 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 857 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 858 | return; |
| 859 | } |
| 860 | |
| 861 | /* Handle end of message */ |
| 862 | if (message->state == DONE_STATE) { |
| 863 | message->status = 0; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 864 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 865 | return; |
| 866 | } |
| 867 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 868 | /* Delay if requested at end of transfer before CS change */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 869 | if (message->state == RUNNING_STATE) { |
| 870 | previous = list_entry(transfer->transfer_list.prev, |
| 871 | struct spi_transfer, |
| 872 | transfer_list); |
| 873 | if (previous->delay_usecs) |
| 874 | udelay(previous->delay_usecs); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 875 | |
| 876 | /* Drop chip select only if cs_change is requested */ |
| 877 | if (previous->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 878 | cs_deassert(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 881 | /* Check if we can DMA this transfer */ |
| 882 | if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) { |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 883 | |
| 884 | /* reject already-mapped transfers; PIO won't always work */ |
| 885 | if (message->is_dma_mapped |
| 886 | || transfer->rx_dma || transfer->tx_dma) { |
| 887 | dev_err(&drv_data->pdev->dev, |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 888 | "pump_transfers: mapped transfer length of " |
| 889 | "%u is greater than %d\n", |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 890 | transfer->len, MAX_DMA_LEN); |
| 891 | message->status = -EINVAL; |
| 892 | giveback(drv_data); |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | /* warn ... we force this to PIO mode */ |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 897 | dev_warn_ratelimited(&message->spi->dev, |
| 898 | "pump_transfers: DMA disabled for transfer length %ld " |
| 899 | "greater than %d\n", |
| 900 | (long)drv_data->len, MAX_DMA_LEN); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 903 | /* Setup the transfer state based on the type of transfer */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 904 | if (pxa2xx_spi_flush(drv_data) == 0) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 905 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 906 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 907 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 908 | return; |
| 909 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 910 | drv_data->n_bytes = chip->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 911 | drv_data->tx = (void *)transfer->tx_buf; |
| 912 | drv_data->tx_end = drv_data->tx + transfer->len; |
| 913 | drv_data->rx = transfer->rx_buf; |
| 914 | drv_data->rx_end = drv_data->rx + transfer->len; |
| 915 | drv_data->rx_dma = transfer->rx_dma; |
| 916 | drv_data->tx_dma = transfer->tx_dma; |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 917 | drv_data->len = transfer->len; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 918 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 919 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 920 | |
| 921 | /* Change speed and bit per word on a per transfer */ |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 922 | bits = transfer->bits_per_word; |
| 923 | speed = transfer->speed_hz; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 924 | |
Andy Shevchenko | d2c2f6a | 2015-10-22 16:44:40 +0300 | [diff] [blame^] | 925 | clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 926 | |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 927 | if (bits <= 8) { |
| 928 | drv_data->n_bytes = 1; |
| 929 | drv_data->read = drv_data->read != null_reader ? |
| 930 | u8_reader : null_reader; |
| 931 | drv_data->write = drv_data->write != null_writer ? |
| 932 | u8_writer : null_writer; |
| 933 | } else if (bits <= 16) { |
| 934 | drv_data->n_bytes = 2; |
| 935 | drv_data->read = drv_data->read != null_reader ? |
| 936 | u16_reader : null_reader; |
| 937 | drv_data->write = drv_data->write != null_writer ? |
| 938 | u16_writer : null_writer; |
| 939 | } else if (bits <= 32) { |
| 940 | drv_data->n_bytes = 4; |
| 941 | drv_data->read = drv_data->read != null_reader ? |
| 942 | u32_reader : null_reader; |
| 943 | drv_data->write = drv_data->write != null_writer ? |
| 944 | u32_writer : null_writer; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 945 | } |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 946 | /* |
| 947 | * if bits/word is changed in dma mode, then must check the |
| 948 | * thresholds and burst also |
| 949 | */ |
| 950 | if (chip->enable_dma) { |
| 951 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, |
| 952 | message->spi, |
| 953 | bits, &dma_burst, |
| 954 | &dma_thresh)) |
| 955 | dev_warn_ratelimited(&message->spi->dev, |
| 956 | "pump_transfers: DMA burst size reduced to match bits_per_word\n"); |
| 957 | } |
| 958 | |
Andy Shevchenko | d74c4b1 | 2015-10-22 16:44:39 +0300 | [diff] [blame] | 959 | /* NOTE: PXA25x_SSP _could_ use external clocking ... */ |
Jarkko Nikula | 196b0e2 | 2015-09-15 16:26:27 +0300 | [diff] [blame] | 960 | cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); |
Andy Shevchenko | d74c4b1 | 2015-10-22 16:44:39 +0300 | [diff] [blame] | 961 | if (!pxa25x_ssp_comp(drv_data)) |
| 962 | dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", |
| 963 | drv_data->master->max_speed_hz |
| 964 | / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), |
| 965 | chip->enable_dma ? "DMA" : "PIO"); |
| 966 | else |
| 967 | dev_dbg(&message->spi->dev, "%u Hz actual, %s\n", |
| 968 | drv_data->master->max_speed_hz / 2 |
| 969 | / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), |
| 970 | chip->enable_dma ? "DMA" : "PIO"); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 971 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 972 | message->state = RUNNING_STATE; |
| 973 | |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 974 | drv_data->dma_mapped = 0; |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 975 | if (pxa2xx_spi_dma_is_possible(drv_data->len)) |
| 976 | drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data); |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 977 | if (drv_data->dma_mapped) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 978 | |
| 979 | /* Ensure we have the correct interrupt handler */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 980 | drv_data->transfer_handler = pxa2xx_spi_dma_transfer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 981 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 982 | pxa2xx_spi_dma_prepare(drv_data, dma_burst); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 983 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 984 | /* Clear status and start DMA engine */ |
| 985 | cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 986 | pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 987 | |
| 988 | pxa2xx_spi_dma_start(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 989 | } else { |
| 990 | /* Ensure we have the correct interrupt handler */ |
| 991 | drv_data->transfer_handler = interrupt_transfer; |
| 992 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 993 | /* Clear status */ |
| 994 | cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 995 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 996 | } |
| 997 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 998 | if (is_lpss_ssp(drv_data)) { |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 999 | if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff) |
| 1000 | != chip->lpss_rx_threshold) |
| 1001 | pxa2xx_spi_write(drv_data, SSIRF, |
| 1002 | chip->lpss_rx_threshold); |
| 1003 | if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff) |
| 1004 | != chip->lpss_tx_threshold) |
| 1005 | pxa2xx_spi_write(drv_data, SSITF, |
| 1006 | chip->lpss_tx_threshold); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1009 | if (is_quark_x1000_ssp(drv_data) && |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1010 | (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate)) |
| 1011 | pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1012 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1013 | /* see if we need to reload the config registers */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1014 | if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0) |
| 1015 | || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask) |
| 1016 | != (cr1 & change_mask)) { |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1017 | /* stop the SSP, and update the other bits */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1018 | pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1019 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1020 | pxa2xx_spi_write(drv_data, SSTO, chip->timeout); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1021 | /* first set CR1 without interrupt and service enables */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1022 | pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1023 | /* restart the SSP */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1024 | pxa2xx_spi_write(drv_data, SSCR0, cr0); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1025 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1026 | } else { |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1027 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1028 | pxa2xx_spi_write(drv_data, SSTO, chip->timeout); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1029 | } |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1030 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1031 | cs_assert(drv_data); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1032 | |
| 1033 | /* after chip select, release the data by enabling service |
| 1034 | * requests and interrupts, without changing any mode bits */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1035 | pxa2xx_spi_write(drv_data, SSCR1, cr1); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1038 | static int pxa2xx_spi_transfer_one_message(struct spi_master *master, |
| 1039 | struct spi_message *msg) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1040 | { |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1041 | struct driver_data *drv_data = spi_master_get_devdata(master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1042 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1043 | drv_data->cur_msg = msg; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1044 | /* Initial message state*/ |
| 1045 | drv_data->cur_msg->state = START_STATE; |
| 1046 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 1047 | struct spi_transfer, |
| 1048 | transfer_list); |
| 1049 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1050 | /* prepare to setup the SSP, in pump_transfers, using the per |
| 1051 | * chip configuration */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1052 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1053 | |
| 1054 | /* Mark as busy and launch transfers */ |
| 1055 | tasklet_schedule(&drv_data->pump_transfers); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1056 | return 0; |
| 1057 | } |
| 1058 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1059 | static int pxa2xx_spi_unprepare_transfer(struct spi_master *master) |
| 1060 | { |
| 1061 | struct driver_data *drv_data = spi_master_get_devdata(master); |
| 1062 | |
| 1063 | /* Disable the SSP now */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1064 | pxa2xx_spi_write(drv_data, SSCR0, |
| 1065 | pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE); |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1066 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1070 | static int setup_cs(struct spi_device *spi, struct chip_data *chip, |
| 1071 | struct pxa2xx_spi_chip *chip_info) |
| 1072 | { |
| 1073 | int err = 0; |
| 1074 | |
| 1075 | if (chip == NULL || chip_info == NULL) |
| 1076 | return 0; |
| 1077 | |
| 1078 | /* NOTE: setup() can be called multiple times, possibly with |
| 1079 | * different chip_info, release previously requested GPIO |
| 1080 | */ |
| 1081 | if (gpio_is_valid(chip->gpio_cs)) |
| 1082 | gpio_free(chip->gpio_cs); |
| 1083 | |
| 1084 | /* If (*cs_control) is provided, ignore GPIO chip select */ |
| 1085 | if (chip_info->cs_control) { |
| 1086 | chip->cs_control = chip_info->cs_control; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | if (gpio_is_valid(chip_info->gpio_cs)) { |
| 1091 | err = gpio_request(chip_info->gpio_cs, "SPI_CS"); |
| 1092 | if (err) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1093 | dev_err(&spi->dev, "failed to request chip select GPIO%d\n", |
| 1094 | chip_info->gpio_cs); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1095 | return err; |
| 1096 | } |
| 1097 | |
| 1098 | chip->gpio_cs = chip_info->gpio_cs; |
| 1099 | chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; |
| 1100 | |
| 1101 | err = gpio_direction_output(chip->gpio_cs, |
| 1102 | !chip->gpio_cs_inverted); |
| 1103 | } |
| 1104 | |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1108 | static int setup(struct spi_device *spi) |
| 1109 | { |
| 1110 | struct pxa2xx_spi_chip *chip_info = NULL; |
| 1111 | struct chip_data *chip; |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 1112 | const struct lpss_config *config; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1113 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1114 | uint tx_thres, tx_hi_thres, rx_thres; |
| 1115 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1116 | switch (drv_data->ssp_type) { |
| 1117 | case QUARK_X1000_SSP: |
| 1118 | tx_thres = TX_THRESH_QUARK_X1000_DFLT; |
| 1119 | tx_hi_thres = 0; |
| 1120 | rx_thres = RX_THRESH_QUARK_X1000_DFLT; |
| 1121 | break; |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1122 | case LPSS_LPT_SSP: |
| 1123 | case LPSS_BYT_SSP: |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1124 | case LPSS_SPT_SSP: |
Jarkko Nikula | dccf736 | 2015-06-04 16:55:11 +0300 | [diff] [blame] | 1125 | config = lpss_get_config(drv_data); |
| 1126 | tx_thres = config->tx_threshold_lo; |
| 1127 | tx_hi_thres = config->tx_threshold_hi; |
| 1128 | rx_thres = config->rx_threshold; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1129 | break; |
| 1130 | default: |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1131 | tx_thres = TX_THRESH_DFLT; |
| 1132 | tx_hi_thres = 0; |
| 1133 | rx_thres = RX_THRESH_DFLT; |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1134 | break; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1135 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1136 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1137 | /* Only alloc on first setup */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1138 | chip = spi_get_ctldata(spi); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1139 | if (!chip) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1140 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Jingoo Han | 9deae45 | 2014-04-29 17:19:38 +0900 | [diff] [blame] | 1141 | if (!chip) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1142 | return -ENOMEM; |
| 1143 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1144 | if (drv_data->ssp_type == CE4100_SSP) { |
| 1145 | if (spi->chip_select > 4) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1146 | dev_err(&spi->dev, |
| 1147 | "failed setup: cs number must not be > 4.\n"); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1148 | kfree(chip); |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | |
| 1152 | chip->frm = spi->chip_select; |
| 1153 | } else |
| 1154 | chip->gpio_cs = -1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1155 | chip->enable_dma = 0; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1156 | chip->timeout = TIMOUT_DFLT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1159 | /* protocol drivers may change the chip settings, so... |
| 1160 | * if chip_info exists, use it */ |
| 1161 | chip_info = spi->controller_data; |
| 1162 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1163 | /* chip_info isn't always needed */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1164 | chip->cr1 = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1165 | if (chip_info) { |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1166 | if (chip_info->timeout) |
| 1167 | chip->timeout = chip_info->timeout; |
| 1168 | if (chip_info->tx_threshold) |
| 1169 | tx_thres = chip_info->tx_threshold; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1170 | if (chip_info->tx_hi_threshold) |
| 1171 | tx_hi_thres = chip_info->tx_hi_threshold; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1172 | if (chip_info->rx_threshold) |
| 1173 | rx_thres = chip_info->rx_threshold; |
| 1174 | chip->enable_dma = drv_data->master_info->enable_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1175 | chip->dma_threshold = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1176 | if (chip_info->enable_loopback) |
| 1177 | chip->cr1 = SSCR1_LBM; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1178 | } else if (ACPI_HANDLE(&spi->dev)) { |
| 1179 | /* |
| 1180 | * Slave devices enumerated from ACPI namespace don't |
| 1181 | * usually have chip_info but we still might want to use |
| 1182 | * DMA with them. |
| 1183 | */ |
| 1184 | chip->enable_dma = drv_data->master_info->enable_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1187 | chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); |
| 1188 | chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
| 1189 | | SSITF_TxHiThresh(tx_hi_thres); |
| 1190 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1191 | /* set dma burst and threshold outside of chip_info path so that if |
| 1192 | * chip_info goes away after setting chip->enable_dma, the |
| 1193 | * burst and threshold can still respond to changes in bits_per_word */ |
| 1194 | if (chip->enable_dma) { |
| 1195 | /* set up legal burst and threshold for dma */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1196 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, |
| 1197 | spi->bits_per_word, |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1198 | &chip->dma_burst_size, |
| 1199 | &chip->dma_threshold)) { |
Jarkko Nikula | f6bd03a | 2013-10-11 13:54:00 +0300 | [diff] [blame] | 1200 | dev_warn(&spi->dev, |
| 1201 | "in setup: DMA burst size reduced to match bits_per_word\n"); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1205 | switch (drv_data->ssp_type) { |
| 1206 | case QUARK_X1000_SSP: |
| 1207 | chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) |
| 1208 | & QUARK_X1000_SSCR1_RFT) |
| 1209 | | (QUARK_X1000_SSCR1_TxTresh(tx_thres) |
| 1210 | & QUARK_X1000_SSCR1_TFT); |
| 1211 | break; |
| 1212 | default: |
| 1213 | chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | |
| 1214 | (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); |
| 1215 | break; |
| 1216 | } |
| 1217 | |
Justin Clacherty | 7f6ee1a | 2007-01-26 00:56:44 -0800 | [diff] [blame] | 1218 | chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
| 1219 | chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) |
| 1220 | | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1221 | |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 1222 | if (spi->mode & SPI_LOOP) |
| 1223 | chip->cr1 |= SSCR1_LBM; |
| 1224 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1225 | if (spi->bits_per_word <= 8) { |
| 1226 | chip->n_bytes = 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1227 | chip->read = u8_reader; |
| 1228 | chip->write = u8_writer; |
| 1229 | } else if (spi->bits_per_word <= 16) { |
| 1230 | chip->n_bytes = 2; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1231 | chip->read = u16_reader; |
| 1232 | chip->write = u16_writer; |
| 1233 | } else if (spi->bits_per_word <= 32) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1234 | chip->n_bytes = 4; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1235 | chip->read = u32_reader; |
| 1236 | chip->write = u32_writer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1237 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1238 | |
| 1239 | spi_set_ctldata(spi, chip); |
| 1240 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1241 | if (drv_data->ssp_type == CE4100_SSP) |
| 1242 | return 0; |
| 1243 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1244 | return setup_cs(spi, chip, chip_info); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1247 | static void cleanup(struct spi_device *spi) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1248 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1249 | struct chip_data *chip = spi_get_ctldata(spi); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1250 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1251 | |
Daniel Ribeiro | 7348d82 | 2009-05-12 13:19:36 -0700 | [diff] [blame] | 1252 | if (!chip) |
| 1253 | return; |
| 1254 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1255 | if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs)) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1256 | gpio_free(chip->gpio_cs); |
| 1257 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1258 | kfree(chip); |
| 1259 | } |
| 1260 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1261 | #ifdef CONFIG_ACPI |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1262 | |
Mathias Krause | 8422ddf | 2015-06-13 14:22:14 +0200 | [diff] [blame] | 1263 | static const struct acpi_device_id pxa2xx_spi_acpi_match[] = { |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1264 | { "INT33C0", LPSS_LPT_SSP }, |
| 1265 | { "INT33C1", LPSS_LPT_SSP }, |
| 1266 | { "INT3430", LPSS_LPT_SSP }, |
| 1267 | { "INT3431", LPSS_LPT_SSP }, |
| 1268 | { "80860F0E", LPSS_BYT_SSP }, |
| 1269 | { "8086228E", LPSS_BYT_SSP }, |
| 1270 | { }, |
| 1271 | }; |
| 1272 | MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); |
| 1273 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1274 | /* |
| 1275 | * PCI IDs of compound devices that integrate both host controller and private |
| 1276 | * integrated DMA engine. Please note these are not used in module |
| 1277 | * autoloading and probing in this module but matching the LPSS SSP type. |
| 1278 | */ |
| 1279 | static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = { |
| 1280 | /* SPT-LP */ |
| 1281 | { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP }, |
| 1282 | { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP }, |
| 1283 | /* SPT-H */ |
| 1284 | { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP }, |
| 1285 | { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP }, |
Axel Lin | 94e5c23 | 2015-08-04 13:52:22 +0800 | [diff] [blame] | 1286 | { }, |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1287 | }; |
| 1288 | |
| 1289 | static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param) |
| 1290 | { |
| 1291 | struct device *dev = param; |
| 1292 | |
| 1293 | if (dev != chan->device->dev->parent) |
| 1294 | return false; |
| 1295 | |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1299 | static struct pxa2xx_spi_master * |
| 1300 | pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) |
| 1301 | { |
| 1302 | struct pxa2xx_spi_master *pdata; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1303 | struct acpi_device *adev; |
| 1304 | struct ssp_device *ssp; |
| 1305 | struct resource *res; |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1306 | const struct acpi_device_id *adev_id = NULL; |
| 1307 | const struct pci_device_id *pcidev_id = NULL; |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1308 | int devid, type; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1309 | |
Jarkko Nikula | b9f6940 | 2015-09-25 10:27:18 +0300 | [diff] [blame] | 1310 | adev = ACPI_COMPANION(&pdev->dev); |
| 1311 | if (!adev) |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1312 | return NULL; |
| 1313 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1314 | if (dev_is_pci(pdev->dev.parent)) |
| 1315 | pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match, |
| 1316 | to_pci_dev(pdev->dev.parent)); |
| 1317 | else |
| 1318 | adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table, |
| 1319 | &pdev->dev); |
| 1320 | |
| 1321 | if (adev_id) |
| 1322 | type = (int)adev_id->driver_data; |
| 1323 | else if (pcidev_id) |
| 1324 | type = (int)pcidev_id->driver_data; |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1325 | else |
| 1326 | return NULL; |
| 1327 | |
Mika Westerberg | cc0ee98 | 2013-06-20 17:44:22 +0300 | [diff] [blame] | 1328 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
Jingoo Han | 9deae45 | 2014-04-29 17:19:38 +0900 | [diff] [blame] | 1329 | if (!pdata) |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1330 | return NULL; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1331 | |
| 1332 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1333 | if (!res) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | ssp = &pdata->ssp; |
| 1337 | |
| 1338 | ssp->phys_base = res->start; |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 1339 | ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 1340 | if (IS_ERR(ssp->mmio_base)) |
Mika Westerberg | 6dc81f6 | 2013-05-13 13:45:09 +0300 | [diff] [blame] | 1341 | return NULL; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1342 | |
Jarkko Nikula | 34cadd9 | 2015-07-30 16:30:07 +0300 | [diff] [blame] | 1343 | if (pcidev_id) { |
| 1344 | pdata->tx_param = pdev->dev.parent; |
| 1345 | pdata->rx_param = pdev->dev.parent; |
| 1346 | pdata->dma_filter = pxa2xx_spi_idma_filter; |
| 1347 | } |
| 1348 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1349 | ssp->clk = devm_clk_get(&pdev->dev, NULL); |
| 1350 | ssp->irq = platform_get_irq(pdev, 0); |
Jarkko Nikula | 03fbf48 | 2015-06-04 16:55:10 +0300 | [diff] [blame] | 1351 | ssp->type = type; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1352 | ssp->pdev = pdev; |
| 1353 | |
| 1354 | ssp->port_id = -1; |
| 1355 | if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid)) |
| 1356 | ssp->port_id = devid; |
| 1357 | |
| 1358 | pdata->num_chipselect = 1; |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1359 | pdata->enable_dma = true; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1360 | |
| 1361 | return pdata; |
| 1362 | } |
| 1363 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1364 | #else |
| 1365 | static inline struct pxa2xx_spi_master * |
| 1366 | pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) |
| 1367 | { |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | #endif |
| 1371 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1372 | static int pxa2xx_spi_probe(struct platform_device *pdev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1373 | { |
| 1374 | struct device *dev = &pdev->dev; |
| 1375 | struct pxa2xx_spi_master *platform_info; |
| 1376 | struct spi_master *master; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1377 | struct driver_data *drv_data; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1378 | struct ssp_device *ssp; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1379 | int status; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1380 | u32 tmp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1381 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1382 | platform_info = dev_get_platdata(dev); |
| 1383 | if (!platform_info) { |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1384 | platform_info = pxa2xx_spi_acpi_get_pdata(pdev); |
| 1385 | if (!platform_info) { |
| 1386 | dev_err(&pdev->dev, "missing platform data\n"); |
| 1387 | return -ENODEV; |
| 1388 | } |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1389 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1390 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1391 | ssp = pxa_ssp_request(pdev->id, pdev->name); |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1392 | if (!ssp) |
| 1393 | ssp = &platform_info->ssp; |
| 1394 | |
| 1395 | if (!ssp->mmio_base) { |
| 1396 | dev_err(&pdev->dev, "failed to get ssp\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1397 | return -ENODEV; |
| 1398 | } |
| 1399 | |
Jarkko Nikula | 757fe8d | 2015-08-05 10:04:05 +0300 | [diff] [blame] | 1400 | master = spi_alloc_master(dev, sizeof(struct driver_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1401 | if (!master) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1402 | dev_err(&pdev->dev, "cannot alloc spi_master\n"); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1403 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1404 | return -ENOMEM; |
| 1405 | } |
| 1406 | drv_data = spi_master_get_devdata(master); |
| 1407 | drv_data->master = master; |
| 1408 | drv_data->master_info = platform_info; |
| 1409 | drv_data->pdev = pdev; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1410 | drv_data->ssp = ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1411 | |
Sebastian Andrzej Siewior | 21486af | 2010-10-08 18:11:19 +0200 | [diff] [blame] | 1412 | master->dev.parent = &pdev->dev; |
Sebastian Andrzej Siewior | 21486af | 2010-10-08 18:11:19 +0200 | [diff] [blame] | 1413 | master->dev.of_node = pdev->dev.of_node; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1414 | /* the spi->mode bits understood by this driver: */ |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 1415 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1416 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1417 | master->bus_num = ssp->port_id; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1418 | master->num_chipselect = platform_info->num_chipselect; |
Mike Rapoport | 7ad0ba9 | 2009-04-06 19:00:57 -0700 | [diff] [blame] | 1419 | master->dma_alignment = DMA_ALIGNMENT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1420 | master->cleanup = cleanup; |
| 1421 | master->setup = setup; |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1422 | master->transfer_one_message = pxa2xx_spi_transfer_one_message; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1423 | master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; |
Mark Brown | 7dd6278 | 2013-07-28 15:35:21 +0100 | [diff] [blame] | 1424 | master->auto_runtime_pm = true; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1425 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1426 | drv_data->ssp_type = ssp->type; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1427 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1428 | drv_data->ioaddr = ssp->mmio_base; |
| 1429 | drv_data->ssdr_physical = ssp->phys_base + SSDR; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1430 | if (pxa25x_ssp_comp(drv_data)) { |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1431 | switch (drv_data->ssp_type) { |
| 1432 | case QUARK_X1000_SSP: |
| 1433 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
| 1434 | break; |
| 1435 | default: |
| 1436 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
| 1437 | break; |
| 1438 | } |
| 1439 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1440 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; |
| 1441 | drv_data->dma_cr1 = 0; |
| 1442 | drv_data->clear_sr = SSSR_ROR; |
| 1443 | drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1444 | } else { |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1445 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1446 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
Mika Westerberg | 5928808 | 2013-01-22 12:26:29 +0200 | [diff] [blame] | 1447 | drv_data->dma_cr1 = DEFAULT_DMA_CR1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1448 | drv_data->clear_sr = SSSR_ROR | SSSR_TINT; |
| 1449 | drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1450 | } |
| 1451 | |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 1452 | status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), |
| 1453 | drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1454 | if (status < 0) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1455 | dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1456 | goto out_error_master_alloc; |
| 1457 | } |
| 1458 | |
| 1459 | /* Setup DMA if requested */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1460 | if (platform_info->enable_dma) { |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1461 | status = pxa2xx_spi_dma_setup(drv_data); |
| 1462 | if (status) { |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1463 | dev_dbg(dev, "no DMA channels available, using PIO\n"); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1464 | platform_info->enable_dma = false; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1465 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /* Enable SOC clock */ |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1469 | clk_prepare_enable(ssp->clk); |
| 1470 | |
Jarkko Nikula | 0eca7cf | 2015-09-25 10:27:17 +0300 | [diff] [blame] | 1471 | master->max_speed_hz = clk_get_rate(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1472 | |
| 1473 | /* Load default SSP configuration */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1474 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1475 | switch (drv_data->ssp_type) { |
| 1476 | case QUARK_X1000_SSP: |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1477 | tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
| 1478 | | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); |
| 1479 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1480 | |
| 1481 | /* using the Motorola SPI protocol and use 8 bit frame */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1482 | pxa2xx_spi_write(drv_data, SSCR0, |
| 1483 | QUARK_X1000_SSCR0_Motorola |
| 1484 | | QUARK_X1000_SSCR0_DataSize(8)); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1485 | break; |
| 1486 | default: |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1487 | tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | |
| 1488 | SSCR1_TxTresh(TX_THRESH_DFLT); |
| 1489 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
| 1490 | tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); |
| 1491 | pxa2xx_spi_write(drv_data, SSCR0, tmp); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1492 | break; |
| 1493 | } |
| 1494 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1495 | if (!pxa25x_ssp_comp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1496 | pxa2xx_spi_write(drv_data, SSTO, 0); |
Weike Chen | e5262d0 | 2014-11-26 02:35:10 -0800 | [diff] [blame] | 1497 | |
| 1498 | if (!is_quark_x1000_ssp(drv_data)) |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1499 | pxa2xx_spi_write(drv_data, SSPSP, 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1500 | |
Jarkko Nikula | 7566bcc | 2014-12-18 15:04:20 +0200 | [diff] [blame] | 1501 | if (is_lpss_ssp(drv_data)) |
| 1502 | lpss_ssp_setup(drv_data); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1503 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1504 | tasklet_init(&drv_data->pump_transfers, pump_transfers, |
| 1505 | (unsigned long)drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1506 | |
Antonio Ospite | 836d1a22 | 2014-05-30 18:18:09 +0200 | [diff] [blame] | 1507 | pm_runtime_set_autosuspend_delay(&pdev->dev, 50); |
| 1508 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1509 | pm_runtime_set_active(&pdev->dev); |
| 1510 | pm_runtime_enable(&pdev->dev); |
| 1511 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1512 | /* Register with the SPI framework */ |
| 1513 | platform_set_drvdata(pdev, drv_data); |
Jingoo Han | a807fcd | 2013-09-24 13:46:55 +0900 | [diff] [blame] | 1514 | status = devm_spi_register_master(&pdev->dev, master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1515 | if (status != 0) { |
| 1516 | dev_err(&pdev->dev, "problem registering spi master\n"); |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1517 | goto out_error_clock_enabled; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | return status; |
| 1521 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1522 | out_error_clock_enabled: |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1523 | clk_disable_unprepare(ssp->clk); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1524 | pxa2xx_spi_dma_release(drv_data); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1525 | free_irq(ssp->irq, drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1526 | |
| 1527 | out_error_master_alloc: |
| 1528 | spi_master_put(master); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1529 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1530 | return status; |
| 1531 | } |
| 1532 | |
| 1533 | static int pxa2xx_spi_remove(struct platform_device *pdev) |
| 1534 | { |
| 1535 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1536 | struct ssp_device *ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1537 | |
| 1538 | if (!drv_data) |
| 1539 | return 0; |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1540 | ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1541 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1542 | pm_runtime_get_sync(&pdev->dev); |
| 1543 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1544 | /* Disable the SSP at the peripheral and SOC level */ |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1545 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1546 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1547 | |
| 1548 | /* Release DMA */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1549 | if (drv_data->master_info->enable_dma) |
| 1550 | pxa2xx_spi_dma_release(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1551 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1552 | pm_runtime_put_noidle(&pdev->dev); |
| 1553 | pm_runtime_disable(&pdev->dev); |
| 1554 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1555 | /* Release IRQ */ |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1556 | free_irq(ssp->irq, drv_data); |
| 1557 | |
| 1558 | /* Release SSP */ |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1559 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1560 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | static void pxa2xx_spi_shutdown(struct platform_device *pdev) |
| 1565 | { |
| 1566 | int status = 0; |
| 1567 | |
| 1568 | if ((status = pxa2xx_spi_remove(pdev)) != 0) |
| 1569 | dev_err(&pdev->dev, "shutdown failed with %d\n", status); |
| 1570 | } |
| 1571 | |
Mika Westerberg | 382cebb | 2014-01-16 14:50:55 +0200 | [diff] [blame] | 1572 | #ifdef CONFIG_PM_SLEEP |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1573 | static int pxa2xx_spi_suspend(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1574 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1575 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1576 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1577 | int status = 0; |
| 1578 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1579 | status = spi_master_suspend(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1580 | if (status != 0) |
| 1581 | return status; |
Jarkko Nikula | c039dd2 | 2014-12-18 15:04:23 +0200 | [diff] [blame] | 1582 | pxa2xx_spi_write(drv_data, SSCR0, 0); |
Dmitry Eremin-Solenikov | 2b9375b | 2014-11-06 14:08:29 +0300 | [diff] [blame] | 1583 | |
| 1584 | if (!pm_runtime_suspended(dev)) |
| 1585 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1586 | |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1590 | static int pxa2xx_spi_resume(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1591 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1592 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1593 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1594 | int status = 0; |
| 1595 | |
| 1596 | /* Enable the SSP clock */ |
Dmitry Eremin-Solenikov | 2b9375b | 2014-11-06 14:08:29 +0300 | [diff] [blame] | 1597 | if (!pm_runtime_suspended(dev)) |
| 1598 | clk_prepare_enable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1599 | |
Chew, Chiau Ee | c50325f | 2013-11-29 02:13:11 +0800 | [diff] [blame] | 1600 | /* Restore LPSS private register bits */ |
Jarkko Nikula | 48421ad | 2015-01-28 10:09:42 +0200 | [diff] [blame] | 1601 | if (is_lpss_ssp(drv_data)) |
| 1602 | lpss_ssp_setup(drv_data); |
Chew, Chiau Ee | c50325f | 2013-11-29 02:13:11 +0800 | [diff] [blame] | 1603 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1604 | /* Start the queue running */ |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1605 | status = spi_master_resume(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1606 | if (status != 0) { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1607 | dev_err(dev, "problem starting queue (%d)\n", status); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1608 | return status; |
| 1609 | } |
| 1610 | |
| 1611 | return 0; |
| 1612 | } |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1613 | #endif |
| 1614 | |
Rafael J. Wysocki | ec83305 | 2014-12-13 00:41:15 +0100 | [diff] [blame] | 1615 | #ifdef CONFIG_PM |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1616 | static int pxa2xx_spi_runtime_suspend(struct device *dev) |
| 1617 | { |
| 1618 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1619 | |
| 1620 | clk_disable_unprepare(drv_data->ssp->clk); |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | static int pxa2xx_spi_runtime_resume(struct device *dev) |
| 1625 | { |
| 1626 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1627 | |
| 1628 | clk_prepare_enable(drv_data->ssp->clk); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | #endif |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1632 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1633 | static const struct dev_pm_ops pxa2xx_spi_pm_ops = { |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1634 | SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) |
| 1635 | SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, |
| 1636 | pxa2xx_spi_runtime_resume, NULL) |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1637 | }; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1638 | |
| 1639 | static struct platform_driver driver = { |
| 1640 | .driver = { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1641 | .name = "pxa2xx-spi", |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1642 | .pm = &pxa2xx_spi_pm_ops, |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1643 | .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1644 | }, |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1645 | .probe = pxa2xx_spi_probe, |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1646 | .remove = pxa2xx_spi_remove, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1647 | .shutdown = pxa2xx_spi_shutdown, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1648 | }; |
| 1649 | |
| 1650 | static int __init pxa2xx_spi_init(void) |
| 1651 | { |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1652 | return platform_driver_register(&driver); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1653 | } |
Antonio Ospite | 5b61a74 | 2009-09-22 16:46:10 -0700 | [diff] [blame] | 1654 | subsys_initcall(pxa2xx_spi_init); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1655 | |
| 1656 | static void __exit pxa2xx_spi_exit(void) |
| 1657 | { |
| 1658 | platform_driver_unregister(&driver); |
| 1659 | } |
| 1660 | module_exit(pxa2xx_spi_exit); |