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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/errno.h> |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 25 | #include <linux/err.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/platform_device.h> |
Sebastian Andrzej Siewior | 8348c25 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 28 | #include <linux/spi/pxa2xx_spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 29 | #include <linux/spi/spi.h> |
| 30 | #include <linux/workqueue.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 31 | #include <linux/delay.h> |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 32 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 34 | #include <linux/clk.h> |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 36 | #include <linux/acpi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 37 | |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/irq.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 40 | #include <asm/delay.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 41 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 42 | #include "spi-pxa2xx.h" |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 43 | |
| 44 | MODULE_AUTHOR("Stephen Street"); |
Will Newton | 037cdaf | 2007-12-10 15:49:25 -0800 | [diff] [blame] | 45 | MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 46 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 47 | MODULE_ALIAS("platform:pxa2xx-spi"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 48 | |
| 49 | #define MAX_BUSES 3 |
| 50 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 51 | #define TIMOUT_DFLT 1000 |
| 52 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 53 | /* |
| 54 | * for testing SSCR1 changes that require SSP restart, basically |
| 55 | * everything except the service and interrupt enables, the pxa270 developer |
| 56 | * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this |
| 57 | * list, but the PXA255 dev man says all bits without really meaning the |
| 58 | * service and interrupt enables |
| 59 | */ |
| 60 | #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 61 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 62 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
| 63 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ |
| 64 | | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ |
| 65 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 66 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 67 | #define LPSS_RX_THRESH_DFLT 64 |
| 68 | #define LPSS_TX_LOTHRESH_DFLT 160 |
| 69 | #define LPSS_TX_HITHRESH_DFLT 224 |
| 70 | |
| 71 | /* Offset from drv_data->lpss_base */ |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame^] | 72 | #define GENERAL_REG 0x08 |
| 73 | #define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) |
Mika Westerberg | 0054e28 | 2013-03-05 12:05:17 +0200 | [diff] [blame] | 74 | #define SSP_REG 0x0c |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 75 | #define SPI_CS_CONTROL 0x18 |
| 76 | #define SPI_CS_CONTROL_SW_MODE BIT(0) |
| 77 | #define SPI_CS_CONTROL_CS_HIGH BIT(1) |
| 78 | |
| 79 | static bool is_lpss_ssp(const struct driver_data *drv_data) |
| 80 | { |
| 81 | return drv_data->ssp_type == LPSS_SSP; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Read and write LPSS SSP private registers. Caller must first check that |
| 86 | * is_lpss_ssp() returns true before these can be called. |
| 87 | */ |
| 88 | static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) |
| 89 | { |
| 90 | WARN_ON(!drv_data->lpss_base); |
| 91 | return readl(drv_data->lpss_base + offset); |
| 92 | } |
| 93 | |
| 94 | static void __lpss_ssp_write_priv(struct driver_data *drv_data, |
| 95 | unsigned offset, u32 value) |
| 96 | { |
| 97 | WARN_ON(!drv_data->lpss_base); |
| 98 | writel(value, drv_data->lpss_base + offset); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * lpss_ssp_setup - perform LPSS SSP specific setup |
| 103 | * @drv_data: pointer to the driver private data |
| 104 | * |
| 105 | * Perform LPSS SSP specific setup. This function must be called first if |
| 106 | * one is going to use LPSS SSP private registers. |
| 107 | */ |
| 108 | static void lpss_ssp_setup(struct driver_data *drv_data) |
| 109 | { |
| 110 | unsigned offset = 0x400; |
| 111 | u32 value, orig; |
| 112 | |
| 113 | if (!is_lpss_ssp(drv_data)) |
| 114 | return; |
| 115 | |
| 116 | /* |
| 117 | * Perform auto-detection of the LPSS SSP private registers. They |
| 118 | * can be either at 1k or 2k offset from the base address. |
| 119 | */ |
| 120 | orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL); |
| 121 | |
| 122 | value = orig | SPI_CS_CONTROL_SW_MODE; |
| 123 | writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL); |
| 124 | value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL); |
| 125 | if (value != (orig | SPI_CS_CONTROL_SW_MODE)) { |
| 126 | offset = 0x800; |
| 127 | goto detection_done; |
| 128 | } |
| 129 | |
| 130 | value &= ~SPI_CS_CONTROL_SW_MODE; |
| 131 | writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL); |
| 132 | value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL); |
| 133 | if (value != orig) { |
| 134 | offset = 0x800; |
| 135 | goto detection_done; |
| 136 | } |
| 137 | |
| 138 | detection_done: |
| 139 | /* Now set the LPSS base */ |
| 140 | drv_data->lpss_base = drv_data->ioaddr + offset; |
| 141 | |
| 142 | /* Enable software chip select control */ |
| 143 | value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH; |
| 144 | __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value); |
Mika Westerberg | 0054e28 | 2013-03-05 12:05:17 +0200 | [diff] [blame] | 145 | |
| 146 | /* Enable multiblock DMA transfers */ |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame^] | 147 | if (drv_data->master_info->enable_dma) { |
Mika Westerberg | 0054e28 | 2013-03-05 12:05:17 +0200 | [diff] [blame] | 148 | __lpss_ssp_write_priv(drv_data, SSP_REG, 1); |
Mika Westerberg | 1de7061 | 2013-07-03 13:25:06 +0300 | [diff] [blame^] | 149 | |
| 150 | value = __lpss_ssp_read_priv(drv_data, GENERAL_REG); |
| 151 | value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE; |
| 152 | __lpss_ssp_write_priv(drv_data, GENERAL_REG, value); |
| 153 | } |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable) |
| 157 | { |
| 158 | u32 value; |
| 159 | |
| 160 | if (!is_lpss_ssp(drv_data)) |
| 161 | return; |
| 162 | |
| 163 | value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL); |
| 164 | if (enable) |
| 165 | value &= ~SPI_CS_CONTROL_CS_HIGH; |
| 166 | else |
| 167 | value |= SPI_CS_CONTROL_CS_HIGH; |
| 168 | __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value); |
| 169 | } |
| 170 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 171 | static void cs_assert(struct driver_data *drv_data) |
| 172 | { |
| 173 | struct chip_data *chip = drv_data->cur_chip; |
| 174 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 175 | if (drv_data->ssp_type == CE4100_SSP) { |
| 176 | write_SSSR(drv_data->cur_chip->frm, drv_data->ioaddr); |
| 177 | return; |
| 178 | } |
| 179 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 180 | if (chip->cs_control) { |
| 181 | chip->cs_control(PXA2XX_CS_ASSERT); |
| 182 | return; |
| 183 | } |
| 184 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 185 | if (gpio_is_valid(chip->gpio_cs)) { |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 186 | gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | lpss_ssp_cs_control(drv_data, true); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void cs_deassert(struct driver_data *drv_data) |
| 194 | { |
| 195 | struct chip_data *chip = drv_data->cur_chip; |
| 196 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 197 | if (drv_data->ssp_type == CE4100_SSP) |
| 198 | return; |
| 199 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 200 | if (chip->cs_control) { |
Daniel Ribeiro | 2b2562d | 2009-04-08 22:48:03 -0300 | [diff] [blame] | 201 | chip->cs_control(PXA2XX_CS_DEASSERT); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 205 | if (gpio_is_valid(chip->gpio_cs)) { |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 206 | gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | lpss_ssp_cs_control(drv_data, false); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 213 | int pxa2xx_spi_flush(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 214 | { |
| 215 | unsigned long limit = loops_per_jiffy << 1; |
| 216 | |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 217 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 218 | |
| 219 | do { |
| 220 | while (read_SSSR(reg) & SSSR_RNE) { |
| 221 | read_SSDR(reg); |
| 222 | } |
Roel Kluin | 306c68a | 2009-04-21 12:24:46 -0700 | [diff] [blame] | 223 | } while ((read_SSSR(reg) & SSSR_BSY) && --limit); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 224 | write_SSSR_CS(drv_data, SSSR_ROR); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 225 | |
| 226 | return limit; |
| 227 | } |
| 228 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 229 | static int null_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 230 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 231 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 232 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 233 | |
Sebastian Andrzej Siewior | 4a25605 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 234 | if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 235 | || (drv_data->tx == drv_data->tx_end)) |
| 236 | return 0; |
| 237 | |
| 238 | write_SSDR(0, reg); |
| 239 | drv_data->tx += n_bytes; |
| 240 | |
| 241 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 244 | static int null_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 245 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 246 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 247 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 248 | |
| 249 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 250 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 251 | read_SSDR(reg); |
| 252 | drv_data->rx += n_bytes; |
| 253 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 254 | |
| 255 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 258 | static int u8_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 259 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 260 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 261 | |
Sebastian Andrzej Siewior | 4a25605 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 262 | if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 263 | || (drv_data->tx == drv_data->tx_end)) |
| 264 | return 0; |
| 265 | |
| 266 | write_SSDR(*(u8 *)(drv_data->tx), reg); |
| 267 | ++drv_data->tx; |
| 268 | |
| 269 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 272 | static int u8_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 273 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 274 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 275 | |
| 276 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 277 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 278 | *(u8 *)(drv_data->rx) = read_SSDR(reg); |
| 279 | ++drv_data->rx; |
| 280 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 281 | |
| 282 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 285 | static int u16_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 286 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 287 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 288 | |
Sebastian Andrzej Siewior | 4a25605 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 289 | if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 290 | || (drv_data->tx == drv_data->tx_end)) |
| 291 | return 0; |
| 292 | |
| 293 | write_SSDR(*(u16 *)(drv_data->tx), reg); |
| 294 | drv_data->tx += 2; |
| 295 | |
| 296 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 299 | static int u16_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 300 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 301 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 302 | |
| 303 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 304 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 305 | *(u16 *)(drv_data->rx) = read_SSDR(reg); |
| 306 | drv_data->rx += 2; |
| 307 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 308 | |
| 309 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 310 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 311 | |
| 312 | static int u32_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 313 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 314 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 315 | |
Sebastian Andrzej Siewior | 4a25605 | 2010-11-22 17:12:15 -0800 | [diff] [blame] | 316 | if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 317 | || (drv_data->tx == drv_data->tx_end)) |
| 318 | return 0; |
| 319 | |
| 320 | write_SSDR(*(u32 *)(drv_data->tx), reg); |
| 321 | drv_data->tx += 4; |
| 322 | |
| 323 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 326 | static int u32_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 327 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 328 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 329 | |
| 330 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 331 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 332 | *(u32 *)(drv_data->rx) = read_SSDR(reg); |
| 333 | drv_data->rx += 4; |
| 334 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 335 | |
| 336 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 339 | void *pxa2xx_spi_next_transfer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 340 | { |
| 341 | struct spi_message *msg = drv_data->cur_msg; |
| 342 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 343 | |
| 344 | /* Move to next transfer */ |
| 345 | if (trans->transfer_list.next != &msg->transfers) { |
| 346 | drv_data->cur_transfer = |
| 347 | list_entry(trans->transfer_list.next, |
| 348 | struct spi_transfer, |
| 349 | transfer_list); |
| 350 | return RUNNING_STATE; |
| 351 | } else |
| 352 | return DONE_STATE; |
| 353 | } |
| 354 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 355 | /* caller already set message->status; dma and pio irqs are blocked */ |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 356 | static void giveback(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 357 | { |
| 358 | struct spi_transfer* last_transfer; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 359 | struct spi_message *msg; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 360 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 361 | msg = drv_data->cur_msg; |
| 362 | drv_data->cur_msg = NULL; |
| 363 | drv_data->cur_transfer = NULL; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 364 | |
| 365 | last_transfer = list_entry(msg->transfers.prev, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 366 | struct spi_transfer, |
| 367 | transfer_list); |
| 368 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 369 | /* Delay if requested before any change in chip select */ |
| 370 | if (last_transfer->delay_usecs) |
| 371 | udelay(last_transfer->delay_usecs); |
| 372 | |
| 373 | /* Drop chip select UNLESS cs_change is true or we are returning |
| 374 | * a message with an error, or next message is for another chip |
| 375 | */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 376 | if (!last_transfer->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 377 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 378 | else { |
| 379 | struct spi_message *next_msg; |
| 380 | |
| 381 | /* Holding of cs was hinted, but we need to make sure |
| 382 | * the next message is for the same chip. Don't waste |
| 383 | * time with the following tests unless this was hinted. |
| 384 | * |
| 385 | * We cannot postpone this until pump_messages, because |
| 386 | * after calling msg->complete (below) the driver that |
| 387 | * sent the current message could be unloaded, which |
| 388 | * could invalidate the cs_control() callback... |
| 389 | */ |
| 390 | |
| 391 | /* get a pointer to the next message, if any */ |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 392 | next_msg = spi_get_next_queued_message(drv_data->master); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 393 | |
| 394 | /* see if the next and current messages point |
| 395 | * to the same chip |
| 396 | */ |
| 397 | if (next_msg && next_msg->spi != msg->spi) |
| 398 | next_msg = NULL; |
| 399 | if (!next_msg || msg->state == ERROR_STATE) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 400 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 401 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 402 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 403 | spi_finalize_current_message(drv_data->master); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 404 | drv_data->cur_chip = NULL; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 407 | static void reset_sccr1(struct driver_data *drv_data) |
| 408 | { |
| 409 | void __iomem *reg = drv_data->ioaddr; |
| 410 | struct chip_data *chip = drv_data->cur_chip; |
| 411 | u32 sccr1_reg; |
| 412 | |
| 413 | sccr1_reg = read_SSCR1(reg) & ~drv_data->int_cr1; |
| 414 | sccr1_reg &= ~SSCR1_RFT; |
| 415 | sccr1_reg |= chip->threshold; |
| 416 | write_SSCR1(sccr1_reg, reg); |
| 417 | } |
| 418 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 419 | static void int_error_stop(struct driver_data *drv_data, const char* msg) |
| 420 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 421 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 422 | |
| 423 | /* Stop and reset SSP */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 424 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 425 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 426 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 427 | write_SSTO(0, reg); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 428 | pxa2xx_spi_flush(drv_data); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 429 | write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg); |
| 430 | |
| 431 | dev_err(&drv_data->pdev->dev, "%s\n", msg); |
| 432 | |
| 433 | drv_data->cur_msg->state = ERROR_STATE; |
| 434 | tasklet_schedule(&drv_data->pump_transfers); |
| 435 | } |
| 436 | |
| 437 | static void int_transfer_complete(struct driver_data *drv_data) |
| 438 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 439 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 440 | |
| 441 | /* Stop SSP */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 442 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 443 | reset_sccr1(drv_data); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 444 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 445 | write_SSTO(0, reg); |
| 446 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 447 | /* Update total byte transferred return count actual bytes read */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 448 | drv_data->cur_msg->actual_length += drv_data->len - |
| 449 | (drv_data->rx_end - drv_data->rx); |
| 450 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 451 | /* Transfer delays and chip select release are |
| 452 | * handled in pump_transfers or giveback |
| 453 | */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 454 | |
| 455 | /* Move to next transfer */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 456 | drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 457 | |
| 458 | /* Schedule transfer tasklet */ |
| 459 | tasklet_schedule(&drv_data->pump_transfers); |
| 460 | } |
| 461 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 462 | static irqreturn_t interrupt_transfer(struct driver_data *drv_data) |
| 463 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 464 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 465 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 466 | u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ? |
| 467 | drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 468 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 469 | u32 irq_status = read_SSSR(reg) & irq_mask; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 470 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 471 | if (irq_status & SSSR_ROR) { |
| 472 | int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); |
| 473 | return IRQ_HANDLED; |
| 474 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 475 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 476 | if (irq_status & SSSR_TINT) { |
| 477 | write_SSSR(SSSR_TINT, reg); |
| 478 | if (drv_data->read(drv_data)) { |
| 479 | int_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 480 | return IRQ_HANDLED; |
| 481 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 482 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 483 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 484 | /* Drain rx fifo, Fill tx fifo and prevent overruns */ |
| 485 | do { |
| 486 | if (drv_data->read(drv_data)) { |
| 487 | int_transfer_complete(drv_data); |
| 488 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 489 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 490 | } while (drv_data->write(drv_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 491 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 492 | if (drv_data->read(drv_data)) { |
| 493 | int_transfer_complete(drv_data); |
| 494 | return IRQ_HANDLED; |
| 495 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 496 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 497 | if (drv_data->tx == drv_data->tx_end) { |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 498 | u32 bytes_left; |
| 499 | u32 sccr1_reg; |
| 500 | |
| 501 | sccr1_reg = read_SSCR1(reg); |
| 502 | sccr1_reg &= ~SSCR1_TIE; |
| 503 | |
| 504 | /* |
| 505 | * PXA25x_SSP has no timeout, set up rx threshould for the |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 506 | * remaining RX bytes. |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 507 | */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 508 | if (pxa25x_ssp_comp(drv_data)) { |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 509 | |
| 510 | sccr1_reg &= ~SSCR1_RFT; |
| 511 | |
| 512 | bytes_left = drv_data->rx_end - drv_data->rx; |
| 513 | switch (drv_data->n_bytes) { |
| 514 | case 4: |
| 515 | bytes_left >>= 1; |
| 516 | case 2: |
| 517 | bytes_left >>= 1; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 518 | } |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 519 | |
| 520 | if (bytes_left > RX_THRESH_DFLT) |
| 521 | bytes_left = RX_THRESH_DFLT; |
| 522 | |
| 523 | sccr1_reg |= SSCR1_RxTresh(bytes_left); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 524 | } |
Sebastian Andrzej Siewior | 579d3bb | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 525 | write_SSCR1(sccr1_reg, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 526 | } |
| 527 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 528 | /* We did something */ |
| 529 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 530 | } |
| 531 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 532 | static irqreturn_t ssp_int(int irq, void *dev_id) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 533 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 534 | struct driver_data *drv_data = dev_id; |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 535 | void __iomem *reg = drv_data->ioaddr; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 536 | u32 sccr1_reg; |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 537 | u32 mask = drv_data->mask_sr; |
| 538 | u32 status; |
| 539 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 540 | /* |
| 541 | * The IRQ might be shared with other peripherals so we must first |
| 542 | * check that are we RPM suspended or not. If we are we assume that |
| 543 | * the IRQ was not for us (we shouldn't be RPM suspended when the |
| 544 | * interrupt is enabled). |
| 545 | */ |
| 546 | if (pm_runtime_suspended(&drv_data->pdev->dev)) |
| 547 | return IRQ_NONE; |
| 548 | |
| 549 | sccr1_reg = read_SSCR1(reg); |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 550 | status = read_SSSR(reg); |
| 551 | |
| 552 | /* Ignore possible writes if we don't need to write */ |
| 553 | if (!(sccr1_reg & SSCR1_TIE)) |
| 554 | mask &= ~SSSR_TFS; |
| 555 | |
| 556 | if (!(status & mask)) |
| 557 | return IRQ_NONE; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 558 | |
| 559 | if (!drv_data->cur_msg) { |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 560 | |
| 561 | write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg); |
| 562 | write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 563 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 564 | write_SSTO(0, reg); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 565 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 566 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 567 | dev_err(&drv_data->pdev->dev, "bad message state " |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 568 | "in interrupt handler\n"); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 569 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 570 | /* Never fail */ |
| 571 | return IRQ_HANDLED; |
| 572 | } |
| 573 | |
| 574 | return drv_data->transfer_handler(drv_data); |
| 575 | } |
| 576 | |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 577 | 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] | 578 | { |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 579 | unsigned long ssp_clk = drv_data->max_clk_rate; |
| 580 | const struct ssp_device *ssp = drv_data->ssp; |
| 581 | |
| 582 | rate = min_t(int, ssp_clk, rate); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 583 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 584 | if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 585 | return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8; |
| 586 | else |
| 587 | return ((ssp_clk / rate - 1) & 0xfff) << 8; |
| 588 | } |
| 589 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 590 | static void pump_transfers(unsigned long data) |
| 591 | { |
| 592 | struct driver_data *drv_data = (struct driver_data *)data; |
| 593 | struct spi_message *message = NULL; |
| 594 | struct spi_transfer *transfer = NULL; |
| 595 | struct spi_transfer *previous = NULL; |
| 596 | struct chip_data *chip = NULL; |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 597 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 598 | u32 clk_div = 0; |
| 599 | u8 bits = 0; |
| 600 | u32 speed = 0; |
| 601 | u32 cr0; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 602 | u32 cr1; |
| 603 | u32 dma_thresh = drv_data->cur_chip->dma_threshold; |
| 604 | u32 dma_burst = drv_data->cur_chip->dma_burst_size; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 605 | |
| 606 | /* Get current state information */ |
| 607 | message = drv_data->cur_msg; |
| 608 | transfer = drv_data->cur_transfer; |
| 609 | chip = drv_data->cur_chip; |
| 610 | |
| 611 | /* Handle for abort */ |
| 612 | if (message->state == ERROR_STATE) { |
| 613 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 614 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
| 618 | /* Handle end of message */ |
| 619 | if (message->state == DONE_STATE) { |
| 620 | message->status = 0; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 621 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 622 | return; |
| 623 | } |
| 624 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 625 | /* Delay if requested at end of transfer before CS change */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 626 | if (message->state == RUNNING_STATE) { |
| 627 | previous = list_entry(transfer->transfer_list.prev, |
| 628 | struct spi_transfer, |
| 629 | transfer_list); |
| 630 | if (previous->delay_usecs) |
| 631 | udelay(previous->delay_usecs); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 632 | |
| 633 | /* Drop chip select only if cs_change is requested */ |
| 634 | if (previous->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 635 | cs_deassert(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 638 | /* Check if we can DMA this transfer */ |
| 639 | if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) { |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 640 | |
| 641 | /* reject already-mapped transfers; PIO won't always work */ |
| 642 | if (message->is_dma_mapped |
| 643 | || transfer->rx_dma || transfer->tx_dma) { |
| 644 | dev_err(&drv_data->pdev->dev, |
| 645 | "pump_transfers: mapped transfer length " |
Mike Rapoport | 20b918d | 2008-10-01 10:39:24 -0700 | [diff] [blame] | 646 | "of %u is greater than %d\n", |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 647 | transfer->len, MAX_DMA_LEN); |
| 648 | message->status = -EINVAL; |
| 649 | giveback(drv_data); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | /* warn ... we force this to PIO mode */ |
| 654 | if (printk_ratelimit()) |
| 655 | dev_warn(&message->spi->dev, "pump_transfers: " |
| 656 | "DMA disabled for transfer length %ld " |
| 657 | "greater than %d\n", |
| 658 | (long)drv_data->len, MAX_DMA_LEN); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 661 | /* Setup the transfer state based on the type of transfer */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 662 | if (pxa2xx_spi_flush(drv_data) == 0) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 663 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 664 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 665 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 666 | return; |
| 667 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 668 | drv_data->n_bytes = chip->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 669 | drv_data->tx = (void *)transfer->tx_buf; |
| 670 | drv_data->tx_end = drv_data->tx + transfer->len; |
| 671 | drv_data->rx = transfer->rx_buf; |
| 672 | drv_data->rx_end = drv_data->rx + transfer->len; |
| 673 | drv_data->rx_dma = transfer->rx_dma; |
| 674 | drv_data->tx_dma = transfer->tx_dma; |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 675 | drv_data->len = transfer->len; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 676 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 677 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 678 | |
| 679 | /* Change speed and bit per word on a per transfer */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 680 | cr0 = chip->cr0; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 681 | if (transfer->speed_hz || transfer->bits_per_word) { |
| 682 | |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 683 | bits = chip->bits_per_word; |
| 684 | speed = chip->speed_hz; |
| 685 | |
| 686 | if (transfer->speed_hz) |
| 687 | speed = transfer->speed_hz; |
| 688 | |
| 689 | if (transfer->bits_per_word) |
| 690 | bits = transfer->bits_per_word; |
| 691 | |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 692 | clk_div = ssp_get_clk_div(drv_data, speed); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 693 | |
| 694 | if (bits <= 8) { |
| 695 | drv_data->n_bytes = 1; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 696 | drv_data->read = drv_data->read != null_reader ? |
| 697 | u8_reader : null_reader; |
| 698 | drv_data->write = drv_data->write != null_writer ? |
| 699 | u8_writer : null_writer; |
| 700 | } else if (bits <= 16) { |
| 701 | drv_data->n_bytes = 2; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 702 | drv_data->read = drv_data->read != null_reader ? |
| 703 | u16_reader : null_reader; |
| 704 | drv_data->write = drv_data->write != null_writer ? |
| 705 | u16_writer : null_writer; |
| 706 | } else if (bits <= 32) { |
| 707 | drv_data->n_bytes = 4; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 708 | drv_data->read = drv_data->read != null_reader ? |
| 709 | u32_reader : null_reader; |
| 710 | drv_data->write = drv_data->write != null_writer ? |
| 711 | u32_writer : null_writer; |
| 712 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 713 | /* if bits/word is changed in dma mode, then must check the |
| 714 | * thresholds and burst also */ |
| 715 | if (chip->enable_dma) { |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 716 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, |
| 717 | message->spi, |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 718 | bits, &dma_burst, |
| 719 | &dma_thresh)) |
| 720 | if (printk_ratelimit()) |
| 721 | dev_warn(&message->spi->dev, |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 722 | "pump_transfers: " |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 723 | "DMA burst size reduced to " |
| 724 | "match bits_per_word\n"); |
| 725 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 726 | |
| 727 | cr0 = clk_div |
| 728 | | SSCR0_Motorola |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 729 | | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 730 | | SSCR0_SSE |
| 731 | | (bits > 16 ? SSCR0_EDSS : 0); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 732 | } |
| 733 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 734 | message->state = RUNNING_STATE; |
| 735 | |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 736 | drv_data->dma_mapped = 0; |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 737 | if (pxa2xx_spi_dma_is_possible(drv_data->len)) |
| 738 | drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data); |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 739 | if (drv_data->dma_mapped) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 740 | |
| 741 | /* Ensure we have the correct interrupt handler */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 742 | drv_data->transfer_handler = pxa2xx_spi_dma_transfer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 743 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 744 | pxa2xx_spi_dma_prepare(drv_data, dma_burst); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 745 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 746 | /* Clear status and start DMA engine */ |
| 747 | cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 748 | write_SSSR(drv_data->clear_sr, reg); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 749 | |
| 750 | pxa2xx_spi_dma_start(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 751 | } else { |
| 752 | /* Ensure we have the correct interrupt handler */ |
| 753 | drv_data->transfer_handler = interrupt_transfer; |
| 754 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 755 | /* Clear status */ |
| 756 | cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 757 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 760 | if (is_lpss_ssp(drv_data)) { |
| 761 | if ((read_SSIRF(reg) & 0xff) != chip->lpss_rx_threshold) |
| 762 | write_SSIRF(chip->lpss_rx_threshold, reg); |
| 763 | if ((read_SSITF(reg) & 0xffff) != chip->lpss_tx_threshold) |
| 764 | write_SSITF(chip->lpss_tx_threshold, reg); |
| 765 | } |
| 766 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 767 | /* see if we need to reload the config registers */ |
| 768 | if ((read_SSCR0(reg) != cr0) |
| 769 | || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) != |
| 770 | (cr1 & SSCR1_CHANGE_MASK)) { |
| 771 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 772 | /* stop the SSP, and update the other bits */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 773 | write_SSCR0(cr0 & ~SSCR0_SSE, reg); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 774 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 775 | write_SSTO(chip->timeout, reg); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 776 | /* first set CR1 without interrupt and service enables */ |
| 777 | write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg); |
| 778 | /* restart the SSP */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 779 | write_SSCR0(cr0, reg); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 780 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 781 | } else { |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 782 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 783 | write_SSTO(chip->timeout, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 784 | } |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 785 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 786 | cs_assert(drv_data); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 787 | |
| 788 | /* after chip select, release the data by enabling service |
| 789 | * requests and interrupts, without changing any mode bits */ |
| 790 | write_SSCR1(cr1, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 793 | static int pxa2xx_spi_transfer_one_message(struct spi_master *master, |
| 794 | struct spi_message *msg) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 795 | { |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 796 | struct driver_data *drv_data = spi_master_get_devdata(master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 797 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 798 | drv_data->cur_msg = msg; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 799 | /* Initial message state*/ |
| 800 | drv_data->cur_msg->state = START_STATE; |
| 801 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 802 | struct spi_transfer, |
| 803 | transfer_list); |
| 804 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 805 | /* prepare to setup the SSP, in pump_transfers, using the per |
| 806 | * chip configuration */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 807 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 808 | |
| 809 | /* Mark as busy and launch transfers */ |
| 810 | tasklet_schedule(&drv_data->pump_transfers); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 814 | static int pxa2xx_spi_prepare_transfer(struct spi_master *master) |
| 815 | { |
| 816 | struct driver_data *drv_data = spi_master_get_devdata(master); |
| 817 | |
| 818 | pm_runtime_get_sync(&drv_data->pdev->dev); |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int pxa2xx_spi_unprepare_transfer(struct spi_master *master) |
| 823 | { |
| 824 | struct driver_data *drv_data = spi_master_get_devdata(master); |
| 825 | |
| 826 | /* Disable the SSP now */ |
| 827 | write_SSCR0(read_SSCR0(drv_data->ioaddr) & ~SSCR0_SSE, |
| 828 | drv_data->ioaddr); |
| 829 | |
| 830 | pm_runtime_mark_last_busy(&drv_data->pdev->dev); |
| 831 | pm_runtime_put_autosuspend(&drv_data->pdev->dev); |
| 832 | return 0; |
| 833 | } |
| 834 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 835 | static int setup_cs(struct spi_device *spi, struct chip_data *chip, |
| 836 | struct pxa2xx_spi_chip *chip_info) |
| 837 | { |
| 838 | int err = 0; |
| 839 | |
| 840 | if (chip == NULL || chip_info == NULL) |
| 841 | return 0; |
| 842 | |
| 843 | /* NOTE: setup() can be called multiple times, possibly with |
| 844 | * different chip_info, release previously requested GPIO |
| 845 | */ |
| 846 | if (gpio_is_valid(chip->gpio_cs)) |
| 847 | gpio_free(chip->gpio_cs); |
| 848 | |
| 849 | /* If (*cs_control) is provided, ignore GPIO chip select */ |
| 850 | if (chip_info->cs_control) { |
| 851 | chip->cs_control = chip_info->cs_control; |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | if (gpio_is_valid(chip_info->gpio_cs)) { |
| 856 | err = gpio_request(chip_info->gpio_cs, "SPI_CS"); |
| 857 | if (err) { |
| 858 | dev_err(&spi->dev, "failed to request chip select " |
| 859 | "GPIO%d\n", chip_info->gpio_cs); |
| 860 | return err; |
| 861 | } |
| 862 | |
| 863 | chip->gpio_cs = chip_info->gpio_cs; |
| 864 | chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; |
| 865 | |
| 866 | err = gpio_direction_output(chip->gpio_cs, |
| 867 | !chip->gpio_cs_inverted); |
| 868 | } |
| 869 | |
| 870 | return err; |
| 871 | } |
| 872 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 873 | static int setup(struct spi_device *spi) |
| 874 | { |
| 875 | struct pxa2xx_spi_chip *chip_info = NULL; |
| 876 | struct chip_data *chip; |
| 877 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 878 | unsigned int clk_div; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 879 | uint tx_thres, tx_hi_thres, rx_thres; |
| 880 | |
| 881 | if (is_lpss_ssp(drv_data)) { |
| 882 | tx_thres = LPSS_TX_LOTHRESH_DFLT; |
| 883 | tx_hi_thres = LPSS_TX_HITHRESH_DFLT; |
| 884 | rx_thres = LPSS_RX_THRESH_DFLT; |
| 885 | } else { |
| 886 | tx_thres = TX_THRESH_DFLT; |
| 887 | tx_hi_thres = 0; |
| 888 | rx_thres = RX_THRESH_DFLT; |
| 889 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 890 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 891 | /* Only alloc on first setup */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 892 | chip = spi_get_ctldata(spi); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 893 | if (!chip) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 894 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 895 | if (!chip) { |
| 896 | dev_err(&spi->dev, |
| 897 | "failed setup: can't allocate chip data\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 898 | return -ENOMEM; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 899 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 900 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 901 | if (drv_data->ssp_type == CE4100_SSP) { |
| 902 | if (spi->chip_select > 4) { |
| 903 | dev_err(&spi->dev, "failed setup: " |
| 904 | "cs number must not be > 4.\n"); |
| 905 | kfree(chip); |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | chip->frm = spi->chip_select; |
| 910 | } else |
| 911 | chip->gpio_cs = -1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 912 | chip->enable_dma = 0; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 913 | chip->timeout = TIMOUT_DFLT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 916 | /* protocol drivers may change the chip settings, so... |
| 917 | * if chip_info exists, use it */ |
| 918 | chip_info = spi->controller_data; |
| 919 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 920 | /* chip_info isn't always needed */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 921 | chip->cr1 = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 922 | if (chip_info) { |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 923 | if (chip_info->timeout) |
| 924 | chip->timeout = chip_info->timeout; |
| 925 | if (chip_info->tx_threshold) |
| 926 | tx_thres = chip_info->tx_threshold; |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 927 | if (chip_info->tx_hi_threshold) |
| 928 | tx_hi_thres = chip_info->tx_hi_threshold; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 929 | if (chip_info->rx_threshold) |
| 930 | rx_thres = chip_info->rx_threshold; |
| 931 | chip->enable_dma = drv_data->master_info->enable_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 932 | chip->dma_threshold = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 933 | if (chip_info->enable_loopback) |
| 934 | chip->cr1 = SSCR1_LBM; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 935 | } else if (ACPI_HANDLE(&spi->dev)) { |
| 936 | /* |
| 937 | * Slave devices enumerated from ACPI namespace don't |
| 938 | * usually have chip_info but we still might want to use |
| 939 | * DMA with them. |
| 940 | */ |
| 941 | chip->enable_dma = drv_data->master_info->enable_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 944 | chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | |
| 945 | (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); |
| 946 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 947 | chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); |
| 948 | chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) |
| 949 | | SSITF_TxHiThresh(tx_hi_thres); |
| 950 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 951 | /* set dma burst and threshold outside of chip_info path so that if |
| 952 | * chip_info goes away after setting chip->enable_dma, the |
| 953 | * burst and threshold can still respond to changes in bits_per_word */ |
| 954 | if (chip->enable_dma) { |
| 955 | /* set up legal burst and threshold for dma */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 956 | if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi, |
| 957 | spi->bits_per_word, |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 958 | &chip->dma_burst_size, |
| 959 | &chip->dma_threshold)) { |
| 960 | dev_warn(&spi->dev, "in setup: DMA burst size reduced " |
| 961 | "to match bits_per_word\n"); |
| 962 | } |
| 963 | } |
| 964 | |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 965 | clk_div = ssp_get_clk_div(drv_data, spi->max_speed_hz); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 966 | chip->speed_hz = spi->max_speed_hz; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 967 | |
| 968 | chip->cr0 = clk_div |
| 969 | | SSCR0_Motorola |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 970 | | SSCR0_DataSize(spi->bits_per_word > 16 ? |
| 971 | spi->bits_per_word - 16 : spi->bits_per_word) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 972 | | SSCR0_SSE |
| 973 | | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0); |
Justin Clacherty | 7f6ee1a | 2007-01-26 00:56:44 -0800 | [diff] [blame] | 974 | chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
| 975 | chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) |
| 976 | | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 977 | |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 978 | if (spi->mode & SPI_LOOP) |
| 979 | chip->cr1 |= SSCR1_LBM; |
| 980 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 981 | /* NOTE: PXA25x_SSP _could_ use external clocking ... */ |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 982 | if (!pxa25x_ssp_comp(drv_data)) |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 983 | dev_dbg(&spi->dev, "%ld Hz actual, %s\n", |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 984 | drv_data->max_clk_rate |
Eric Miao | c9840da | 2010-03-16 16:48:01 +0800 | [diff] [blame] | 985 | / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)), |
| 986 | chip->enable_dma ? "DMA" : "PIO"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 987 | else |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 988 | dev_dbg(&spi->dev, "%ld Hz actual, %s\n", |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 989 | drv_data->max_clk_rate / 2 |
Eric Miao | c9840da | 2010-03-16 16:48:01 +0800 | [diff] [blame] | 990 | / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)), |
| 991 | chip->enable_dma ? "DMA" : "PIO"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 992 | |
| 993 | if (spi->bits_per_word <= 8) { |
| 994 | chip->n_bytes = 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 995 | chip->read = u8_reader; |
| 996 | chip->write = u8_writer; |
| 997 | } else if (spi->bits_per_word <= 16) { |
| 998 | chip->n_bytes = 2; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 999 | chip->read = u16_reader; |
| 1000 | chip->write = u16_writer; |
| 1001 | } else if (spi->bits_per_word <= 32) { |
| 1002 | chip->cr0 |= SSCR0_EDSS; |
| 1003 | chip->n_bytes = 4; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1004 | chip->read = u32_reader; |
| 1005 | chip->write = u32_writer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1006 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1007 | chip->bits_per_word = spi->bits_per_word; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1008 | |
| 1009 | spi_set_ctldata(spi, chip); |
| 1010 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1011 | if (drv_data->ssp_type == CE4100_SSP) |
| 1012 | return 0; |
| 1013 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1014 | return setup_cs(spi, chip, chip_info); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1017 | static void cleanup(struct spi_device *spi) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1018 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1019 | struct chip_data *chip = spi_get_ctldata(spi); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1020 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1021 | |
Daniel Ribeiro | 7348d82 | 2009-05-12 13:19:36 -0700 | [diff] [blame] | 1022 | if (!chip) |
| 1023 | return; |
| 1024 | |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1025 | 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] | 1026 | gpio_free(chip->gpio_cs); |
| 1027 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1028 | kfree(chip); |
| 1029 | } |
| 1030 | |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1031 | #ifdef CONFIG_ACPI |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1032 | static struct pxa2xx_spi_master * |
| 1033 | pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) |
| 1034 | { |
| 1035 | struct pxa2xx_spi_master *pdata; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1036 | struct acpi_device *adev; |
| 1037 | struct ssp_device *ssp; |
| 1038 | struct resource *res; |
| 1039 | int devid; |
| 1040 | |
| 1041 | if (!ACPI_HANDLE(&pdev->dev) || |
| 1042 | acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev)) |
| 1043 | return NULL; |
| 1044 | |
Mika Westerberg | cc0ee98 | 2013-06-20 17:44:22 +0300 | [diff] [blame] | 1045 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1046 | if (!pdata) { |
| 1047 | dev_err(&pdev->dev, |
| 1048 | "failed to allocate memory for platform data\n"); |
| 1049 | return NULL; |
| 1050 | } |
| 1051 | |
| 1052 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1053 | if (!res) |
| 1054 | return NULL; |
| 1055 | |
| 1056 | ssp = &pdata->ssp; |
| 1057 | |
| 1058 | ssp->phys_base = res->start; |
Sachin Kamat | cbfd6a2 | 2013-04-08 15:49:33 +0530 | [diff] [blame] | 1059 | ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 1060 | if (IS_ERR(ssp->mmio_base)) |
Mika Westerberg | 6dc81f6 | 2013-05-13 13:45:09 +0300 | [diff] [blame] | 1061 | return NULL; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1062 | |
| 1063 | ssp->clk = devm_clk_get(&pdev->dev, NULL); |
| 1064 | ssp->irq = platform_get_irq(pdev, 0); |
| 1065 | ssp->type = LPSS_SSP; |
| 1066 | ssp->pdev = pdev; |
| 1067 | |
| 1068 | ssp->port_id = -1; |
| 1069 | if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid)) |
| 1070 | ssp->port_id = devid; |
| 1071 | |
| 1072 | pdata->num_chipselect = 1; |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1073 | pdata->enable_dma = true; |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1074 | |
| 1075 | return pdata; |
| 1076 | } |
| 1077 | |
| 1078 | static struct acpi_device_id pxa2xx_spi_acpi_match[] = { |
| 1079 | { "INT33C0", 0 }, |
| 1080 | { "INT33C1", 0 }, |
Mika Westerberg | 4b30f2a | 2013-05-13 13:45:11 +0300 | [diff] [blame] | 1081 | { "80860F0E", 0 }, |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1082 | { }, |
| 1083 | }; |
| 1084 | MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match); |
| 1085 | #else |
| 1086 | static inline struct pxa2xx_spi_master * |
| 1087 | pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev) |
| 1088 | { |
| 1089 | return NULL; |
| 1090 | } |
| 1091 | #endif |
| 1092 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1093 | static int pxa2xx_spi_probe(struct platform_device *pdev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1094 | { |
| 1095 | struct device *dev = &pdev->dev; |
| 1096 | struct pxa2xx_spi_master *platform_info; |
| 1097 | struct spi_master *master; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1098 | struct driver_data *drv_data; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1099 | struct ssp_device *ssp; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1100 | int status; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1101 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1102 | platform_info = dev_get_platdata(dev); |
| 1103 | if (!platform_info) { |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1104 | platform_info = pxa2xx_spi_acpi_get_pdata(pdev); |
| 1105 | if (!platform_info) { |
| 1106 | dev_err(&pdev->dev, "missing platform data\n"); |
| 1107 | return -ENODEV; |
| 1108 | } |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1109 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1110 | |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1111 | ssp = pxa_ssp_request(pdev->id, pdev->name); |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1112 | if (!ssp) |
| 1113 | ssp = &platform_info->ssp; |
| 1114 | |
| 1115 | if (!ssp->mmio_base) { |
| 1116 | dev_err(&pdev->dev, "failed to get ssp\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1117 | return -ENODEV; |
| 1118 | } |
| 1119 | |
| 1120 | /* Allocate master with space for drv_data and null dma buffer */ |
| 1121 | master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); |
| 1122 | if (!master) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1123 | dev_err(&pdev->dev, "cannot alloc spi_master\n"); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1124 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1125 | return -ENOMEM; |
| 1126 | } |
| 1127 | drv_data = spi_master_get_devdata(master); |
| 1128 | drv_data->master = master; |
| 1129 | drv_data->master_info = platform_info; |
| 1130 | drv_data->pdev = pdev; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1131 | drv_data->ssp = ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1132 | |
Sebastian Andrzej Siewior | 21486af | 2010-10-08 18:11:19 +0200 | [diff] [blame] | 1133 | master->dev.parent = &pdev->dev; |
Sebastian Andrzej Siewior | 21486af | 2010-10-08 18:11:19 +0200 | [diff] [blame] | 1134 | master->dev.of_node = pdev->dev.of_node; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1135 | /* the spi->mode bits understood by this driver: */ |
Mika Westerberg | b833172 | 2013-01-22 12:26:31 +0200 | [diff] [blame] | 1136 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1137 | |
Mika Westerberg | 851bacf | 2013-01-07 12:44:33 +0200 | [diff] [blame] | 1138 | master->bus_num = ssp->port_id; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1139 | master->num_chipselect = platform_info->num_chipselect; |
Mike Rapoport | 7ad0ba9 | 2009-04-06 19:00:57 -0700 | [diff] [blame] | 1140 | master->dma_alignment = DMA_ALIGNMENT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1141 | master->cleanup = cleanup; |
| 1142 | master->setup = setup; |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1143 | master->transfer_one_message = pxa2xx_spi_transfer_one_message; |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1144 | master->prepare_transfer_hardware = pxa2xx_spi_prepare_transfer; |
| 1145 | master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1146 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1147 | drv_data->ssp_type = ssp->type; |
Mika Westerberg | 2b9b84f | 2013-01-22 12:26:25 +0200 | [diff] [blame] | 1148 | drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1149 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1150 | drv_data->ioaddr = ssp->mmio_base; |
| 1151 | drv_data->ssdr_physical = ssp->phys_base + SSDR; |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1152 | if (pxa25x_ssp_comp(drv_data)) { |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1153 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1154 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; |
| 1155 | drv_data->dma_cr1 = 0; |
| 1156 | drv_data->clear_sr = SSSR_ROR; |
| 1157 | drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1158 | } else { |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1159 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1160 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
Mika Westerberg | 5928808 | 2013-01-22 12:26:29 +0200 | [diff] [blame] | 1161 | drv_data->dma_cr1 = DEFAULT_DMA_CR1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1162 | drv_data->clear_sr = SSSR_ROR | SSSR_TINT; |
| 1163 | drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1164 | } |
| 1165 | |
Sebastian Andrzej Siewior | 49cbb1e | 2010-11-22 17:12:14 -0800 | [diff] [blame] | 1166 | status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), |
| 1167 | drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1168 | if (status < 0) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1169 | dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1170 | goto out_error_master_alloc; |
| 1171 | } |
| 1172 | |
| 1173 | /* Setup DMA if requested */ |
| 1174 | drv_data->tx_channel = -1; |
| 1175 | drv_data->rx_channel = -1; |
| 1176 | if (platform_info->enable_dma) { |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1177 | status = pxa2xx_spi_dma_setup(drv_data); |
| 1178 | if (status) { |
Mika Westerberg | cddb339 | 2013-05-13 13:45:10 +0300 | [diff] [blame] | 1179 | dev_dbg(dev, "no DMA channels available, using PIO\n"); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1180 | platform_info->enable_dma = false; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1181 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /* Enable SOC clock */ |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1185 | clk_prepare_enable(ssp->clk); |
| 1186 | |
| 1187 | drv_data->max_clk_rate = clk_get_rate(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1188 | |
| 1189 | /* Load default SSP configuration */ |
| 1190 | write_SSCR0(0, drv_data->ioaddr); |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1191 | write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) | |
| 1192 | SSCR1_TxTresh(TX_THRESH_DFLT), |
| 1193 | drv_data->ioaddr); |
Eric Miao | c9840da | 2010-03-16 16:48:01 +0800 | [diff] [blame] | 1194 | write_SSCR0(SSCR0_SCR(2) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1195 | | SSCR0_Motorola |
| 1196 | | SSCR0_DataSize(8), |
| 1197 | drv_data->ioaddr); |
Sebastian Andrzej Siewior | 2a8626a | 2010-11-22 17:12:17 -0800 | [diff] [blame] | 1198 | if (!pxa25x_ssp_comp(drv_data)) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1199 | write_SSTO(0, drv_data->ioaddr); |
| 1200 | write_SSPSP(0, drv_data->ioaddr); |
| 1201 | |
Mika Westerberg | a0d2642 | 2013-01-22 12:26:32 +0200 | [diff] [blame] | 1202 | lpss_ssp_setup(drv_data); |
| 1203 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1204 | tasklet_init(&drv_data->pump_transfers, pump_transfers, |
| 1205 | (unsigned long)drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1206 | |
| 1207 | /* Register with the SPI framework */ |
| 1208 | platform_set_drvdata(pdev, drv_data); |
| 1209 | status = spi_register_master(master); |
| 1210 | if (status != 0) { |
| 1211 | dev_err(&pdev->dev, "problem registering spi master\n"); |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1212 | goto out_error_clock_enabled; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1213 | } |
| 1214 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1215 | pm_runtime_set_autosuspend_delay(&pdev->dev, 50); |
| 1216 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1217 | pm_runtime_set_active(&pdev->dev); |
| 1218 | pm_runtime_enable(&pdev->dev); |
| 1219 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1220 | return status; |
| 1221 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1222 | out_error_clock_enabled: |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1223 | clk_disable_unprepare(ssp->clk); |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1224 | pxa2xx_spi_dma_release(drv_data); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1225 | free_irq(ssp->irq, drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1226 | |
| 1227 | out_error_master_alloc: |
| 1228 | spi_master_put(master); |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1229 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1230 | return status; |
| 1231 | } |
| 1232 | |
| 1233 | static int pxa2xx_spi_remove(struct platform_device *pdev) |
| 1234 | { |
| 1235 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1236 | struct ssp_device *ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1237 | |
| 1238 | if (!drv_data) |
| 1239 | return 0; |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1240 | ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1241 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1242 | pm_runtime_get_sync(&pdev->dev); |
| 1243 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1244 | /* Disable the SSP at the peripheral and SOC level */ |
| 1245 | write_SSCR0(0, drv_data->ioaddr); |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1246 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1247 | |
| 1248 | /* Release DMA */ |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1249 | if (drv_data->master_info->enable_dma) |
| 1250 | pxa2xx_spi_dma_release(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1251 | |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1252 | pm_runtime_put_noidle(&pdev->dev); |
| 1253 | pm_runtime_disable(&pdev->dev); |
| 1254 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1255 | /* Release IRQ */ |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1256 | free_irq(ssp->irq, drv_data); |
| 1257 | |
| 1258 | /* Release SSP */ |
Haojian Zhuang | baffe16 | 2010-05-05 10:11:15 -0400 | [diff] [blame] | 1259 | pxa_ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1260 | |
| 1261 | /* Disconnect from the SPI framework */ |
| 1262 | spi_unregister_master(drv_data->master); |
| 1263 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static void pxa2xx_spi_shutdown(struct platform_device *pdev) |
| 1268 | { |
| 1269 | int status = 0; |
| 1270 | |
| 1271 | if ((status = pxa2xx_spi_remove(pdev)) != 0) |
| 1272 | dev_err(&pdev->dev, "shutdown failed with %d\n", status); |
| 1273 | } |
| 1274 | |
| 1275 | #ifdef CONFIG_PM |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1276 | static int pxa2xx_spi_suspend(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1277 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1278 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1279 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1280 | int status = 0; |
| 1281 | |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1282 | status = spi_master_suspend(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1283 | if (status != 0) |
| 1284 | return status; |
| 1285 | write_SSCR0(0, drv_data->ioaddr); |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1286 | clk_disable_unprepare(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1291 | static int pxa2xx_spi_resume(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1292 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1293 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1294 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1295 | int status = 0; |
| 1296 | |
Mika Westerberg | cd7bed0 | 2013-01-22 12:26:28 +0200 | [diff] [blame] | 1297 | pxa2xx_spi_dma_resume(drv_data); |
Daniel Ribeiro | 148da33 | 2009-04-21 12:24:43 -0700 | [diff] [blame] | 1298 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1299 | /* Enable the SSP clock */ |
Mika Westerberg | 3343b7a | 2013-01-22 12:26:27 +0200 | [diff] [blame] | 1300 | clk_prepare_enable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1301 | |
| 1302 | /* Start the queue running */ |
Mika Westerberg | 7f86bde | 2013-01-22 12:26:26 +0200 | [diff] [blame] | 1303 | status = spi_master_resume(drv_data->master); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1304 | if (status != 0) { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1305 | dev_err(dev, "problem starting queue (%d)\n", status); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1306 | return status; |
| 1307 | } |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1311 | #endif |
| 1312 | |
| 1313 | #ifdef CONFIG_PM_RUNTIME |
| 1314 | static int pxa2xx_spi_runtime_suspend(struct device *dev) |
| 1315 | { |
| 1316 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1317 | |
| 1318 | clk_disable_unprepare(drv_data->ssp->clk); |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | static int pxa2xx_spi_runtime_resume(struct device *dev) |
| 1323 | { |
| 1324 | struct driver_data *drv_data = dev_get_drvdata(dev); |
| 1325 | |
| 1326 | clk_prepare_enable(drv_data->ssp->clk); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | #endif |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1330 | |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1331 | static const struct dev_pm_ops pxa2xx_spi_pm_ops = { |
Mika Westerberg | 7d94a50 | 2013-01-22 12:26:30 +0200 | [diff] [blame] | 1332 | SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) |
| 1333 | SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, |
| 1334 | pxa2xx_spi_runtime_resume, NULL) |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1335 | }; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1336 | |
| 1337 | static struct platform_driver driver = { |
| 1338 | .driver = { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1339 | .name = "pxa2xx-spi", |
| 1340 | .owner = THIS_MODULE, |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1341 | .pm = &pxa2xx_spi_pm_ops, |
Mika Westerberg | a349685 | 2013-01-22 12:26:33 +0200 | [diff] [blame] | 1342 | .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match), |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1343 | }, |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1344 | .probe = pxa2xx_spi_probe, |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1345 | .remove = pxa2xx_spi_remove, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1346 | .shutdown = pxa2xx_spi_shutdown, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1347 | }; |
| 1348 | |
| 1349 | static int __init pxa2xx_spi_init(void) |
| 1350 | { |
Sebastian Andrzej Siewior | fbd29a1 | 2010-11-19 09:00:11 -0800 | [diff] [blame] | 1351 | return platform_driver_register(&driver); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1352 | } |
Antonio Ospite | 5b61a74 | 2009-09-22 16:46:10 -0700 | [diff] [blame] | 1353 | subsys_initcall(pxa2xx_spi_init); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1354 | |
| 1355 | static void __exit pxa2xx_spi_exit(void) |
| 1356 | { |
| 1357 | platform_driver_unregister(&driver); |
| 1358 | } |
| 1359 | module_exit(pxa2xx_spi_exit); |