blob: 0e075db589ca64aee4b73a44cb4bd77a4ec8404a [file] [log] [blame]
Stephen Streete0c99052006-03-07 23:53:24 -08001/*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
Mika Westerberga0d26422013-01-22 12:26:32 +02003 * Copyright (C) 2013, Intel Corporation
Stephen Streete0c99052006-03-07 23:53:24 -08004 *
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 Streete0c99052006-03-07 23:53:24 -080014 */
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 Kamatcbfd6a22013-04-08 15:49:33 +053021#include <linux/err.h>
Stephen Streete0c99052006-03-07 23:53:24 -080022#include <linux/interrupt.h>
Andy Shevchenko9df461e2015-03-25 15:06:16 +020023#include <linux/kernel.h>
Jarkko Nikula34cadd92015-07-30 16:30:07 +030024#include <linux/pci.h>
Stephen Streete0c99052006-03-07 23:53:24 -080025#include <linux/platform_device.h>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080026#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080027#include <linux/spi/spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080028#include <linux/delay.h>
Eric Miaoa7bb3902009-04-06 19:00:54 -070029#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Mika Westerberg3343b7a2013-01-22 12:26:27 +020031#include <linux/clk.h>
Mika Westerberg7d94a502013-01-22 12:26:30 +020032#include <linux/pm_runtime.h>
Mika Westerberga3496852013-01-22 12:26:33 +020033#include <linux/acpi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080034
Mika Westerbergcd7bed02013-01-22 12:26:28 +020035#include "spi-pxa2xx.h"
Stephen Streete0c99052006-03-07 23:53:24 -080036
37MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080038MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080039MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070040MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080041
Vernon Sauderf1f640a2008-10-15 22:02:43 -070042#define TIMOUT_DFLT 1000
43
Ned Forresterb97c74b2008-02-23 15:23:40 -080044/*
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 Street8d94cc52006-12-10 02:18:54 -080052 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080053 | 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 Street8d94cc52006-12-10 02:18:54 -080057
Weike Chene5262d02014-11-26 02:35:10 -080058#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 Westerberg1de70612013-07-03 13:25:06 +030064#define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
Mika Westerberga0d26422013-01-22 12:26:32 +020065#define SPI_CS_CONTROL_SW_MODE BIT(0)
66#define SPI_CS_CONTROL_CS_HIGH BIT(1)
67
Jarkko Nikuladccf7362015-06-04 16:55:11 +030068struct 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 */
82static 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 Nikula34cadd92015-07-30 16:30:07 +0300101 { /* 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 Nikuladccf7362015-06-04 16:55:11 +0300110};
111
112static 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 Westerberga0d26422013-01-22 12:26:32 +0200118static bool is_lpss_ssp(const struct driver_data *drv_data)
119{
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300120 switch (drv_data->ssp_type) {
121 case LPSS_LPT_SSP:
122 case LPSS_BYT_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +0300123 case LPSS_SPT_SSP:
Jarkko Nikula03fbf482015-06-04 16:55:10 +0300124 return true;
125 default:
126 return false;
127 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200128}
129
Weike Chene5262d02014-11-26 02:35:10 -0800130static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
131{
132 return drv_data->ssp_type == QUARK_X1000_SSP;
133}
134
Weike Chen4fdb2422014-10-08 08:50:22 -0700135static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
136{
137 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800138 case QUARK_X1000_SSP:
139 return QUARK_X1000_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -0700140 default:
141 return SSCR1_CHANGE_MASK;
142 }
143}
144
145static u32
146pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
147{
148 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800149 case QUARK_X1000_SSP:
150 return RX_THRESH_QUARK_X1000_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700151 default:
152 return RX_THRESH_DFLT;
153 }
154}
155
156static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
157{
Weike Chen4fdb2422014-10-08 08:50:22 -0700158 u32 mask;
159
160 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800161 case QUARK_X1000_SSP:
162 mask = QUARK_X1000_SSSR_TFL_MASK;
163 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700164 default:
165 mask = SSSR_TFL_MASK;
166 break;
167 }
168
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200169 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700170}
171
172static 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 Chene5262d02014-11-26 02:35:10 -0800178 case QUARK_X1000_SSP:
179 mask = QUARK_X1000_SSCR1_RFT;
180 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700181 default:
182 mask = SSCR1_RFT;
183 break;
184 }
185 *sccr1_reg &= ~mask;
186}
187
188static 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 Chene5262d02014-11-26 02:35:10 -0800192 case QUARK_X1000_SSP:
193 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
194 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700195 default:
196 *sccr1_reg |= SSCR1_RxTresh(threshold);
197 break;
198 }
199}
200
201static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
202 u32 clk_div, u8 bits)
203{
204 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800205 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 Chen4fdb2422014-10-08 08:50:22 -0700210 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 Westerberga0d26422013-01-22 12:26:32 +0200219/*
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 */
223static 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
229static 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 */
243static void lpss_ssp_setup(struct driver_data *drv_data)
244{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300245 const struct lpss_config *config;
246 u32 value;
Mika Westerberga0d26422013-01-22 12:26:32 +0200247
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300248 config = lpss_get_config(drv_data);
249 drv_data->lpss_base = drv_data->ioaddr + config->offset;
Mika Westerberga0d26422013-01-22 12:26:32 +0200250
251 /* Enable software chip select control */
252 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300253 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200254
255 /* Enable multiblock DMA transfers */
Mika Westerberg1de70612013-07-03 13:25:06 +0300256 if (drv_data->master_info->enable_dma) {
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300257 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300258
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300259 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 Westerberg1de70612013-07-03 13:25:06 +0300266 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200267}
268
269static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
270{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300271 const struct lpss_config *config;
Mika Westerberga0d26422013-01-22 12:26:32 +0200272 u32 value;
273
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300274 config = lpss_get_config(drv_data);
275
276 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Mika Westerberga0d26422013-01-22 12:26:32 +0200277 if (enable)
278 value &= ~SPI_CS_CONTROL_CS_HIGH;
279 else
280 value |= SPI_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300281 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberga0d26422013-01-22 12:26:32 +0200282}
283
Eric Miaoa7bb3902009-04-06 19:00:54 -0700284static void cs_assert(struct driver_data *drv_data)
285{
286 struct chip_data *chip = drv_data->cur_chip;
287
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800288 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200289 pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800290 return;
291 }
292
Eric Miaoa7bb3902009-04-06 19:00:54 -0700293 if (chip->cs_control) {
294 chip->cs_control(PXA2XX_CS_ASSERT);
295 return;
296 }
297
Mika Westerberga0d26422013-01-22 12:26:32 +0200298 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700299 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200300 return;
301 }
302
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200303 if (is_lpss_ssp(drv_data))
304 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700305}
306
307static void cs_deassert(struct driver_data *drv_data)
308{
309 struct chip_data *chip = drv_data->cur_chip;
310
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800311 if (drv_data->ssp_type == CE4100_SSP)
312 return;
313
Eric Miaoa7bb3902009-04-06 19:00:54 -0700314 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300315 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700316 return;
317 }
318
Mika Westerberga0d26422013-01-22 12:26:32 +0200319 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700320 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200321 return;
322 }
323
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200324 if (is_lpss_ssp(drv_data))
325 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700326}
327
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200328int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800329{
330 unsigned long limit = loops_per_jiffy << 1;
331
Stephen Streete0c99052006-03-07 23:53:24 -0800332 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200333 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 Siewior2a8626a2010-11-22 17:12:17 -0800336 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800337
338 return limit;
339}
340
Stephen Street8d94cc52006-12-10 02:18:54 -0800341static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800342{
Stephen Street9708c122006-03-28 14:05:23 -0800343 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800344
Weike Chen4fdb2422014-10-08 08:50:22 -0700345 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800346 || (drv_data->tx == drv_data->tx_end))
347 return 0;
348
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200349 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800350 drv_data->tx += n_bytes;
351
352 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800353}
354
Stephen Street8d94cc52006-12-10 02:18:54 -0800355static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800356{
Stephen Street9708c122006-03-28 14:05:23 -0800357 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800358
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200359 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 Streete0c99052006-03-07 23:53:24 -0800362 drv_data->rx += n_bytes;
363 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800364
365 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800366}
367
Stephen Street8d94cc52006-12-10 02:18:54 -0800368static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800369{
Weike Chen4fdb2422014-10-08 08:50:22 -0700370 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800371 || (drv_data->tx == drv_data->tx_end))
372 return 0;
373
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200374 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800375 ++drv_data->tx;
376
377 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800378}
379
Stephen Street8d94cc52006-12-10 02:18:54 -0800380static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800381{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200382 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 Streete0c99052006-03-07 23:53:24 -0800385 ++drv_data->rx;
386 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800387
388 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800389}
390
Stephen Street8d94cc52006-12-10 02:18:54 -0800391static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800392{
Weike Chen4fdb2422014-10-08 08:50:22 -0700393 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800394 || (drv_data->tx == drv_data->tx_end))
395 return 0;
396
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200397 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800398 drv_data->tx += 2;
399
400 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800401}
402
Stephen Street8d94cc52006-12-10 02:18:54 -0800403static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800404{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200405 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 Streete0c99052006-03-07 23:53:24 -0800408 drv_data->rx += 2;
409 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800410
411 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800412}
Stephen Street8d94cc52006-12-10 02:18:54 -0800413
414static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800415{
Weike Chen4fdb2422014-10-08 08:50:22 -0700416 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800417 || (drv_data->tx == drv_data->tx_end))
418 return 0;
419
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200420 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800421 drv_data->tx += 4;
422
423 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800424}
425
Stephen Street8d94cc52006-12-10 02:18:54 -0800426static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800427{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200428 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 Streete0c99052006-03-07 23:53:24 -0800431 drv_data->rx += 4;
432 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800433
434 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800435}
436
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200437void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800438{
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 Streete0c99052006-03-07 23:53:24 -0800453/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700454static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800455{
456 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700457 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800458
Stephen Street5daa3ba2006-05-20 15:00:19 -0700459 msg = drv_data->cur_msg;
460 drv_data->cur_msg = NULL;
461 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700462
Axel Lin23e2c2a2014-02-12 22:13:27 +0800463 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
Stephen Streete0c99052006-03-07 23:53:24 -0800464 transfer_list);
465
Ned Forrester84235972008-09-13 02:33:17 -0700466 /* 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 Streete0c99052006-03-07 23:53:24 -0800473 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700474 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700475 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 Westerberg7f86bde2013-01-22 12:26:26 +0200489 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700490
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 Miaoa7bb3902009-04-06 19:00:54 -0700497 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700498 }
Stephen Streete0c99052006-03-07 23:53:24 -0800499
Eric Miaoa7bb3902009-04-06 19:00:54 -0700500 drv_data->cur_chip = NULL;
Mika Westerbergc957e8f2014-12-29 10:33:36 +0200501 spi_finalize_current_message(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -0800502}
503
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800504static void reset_sccr1(struct driver_data *drv_data)
505{
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800506 struct chip_data *chip = drv_data->cur_chip;
507 u32 sccr1_reg;
508
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200509 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800510 sccr1_reg &= ~SSCR1_RFT;
511 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200512 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800513}
514
Stephen Street8d94cc52006-12-10 02:18:54 -0800515static void int_error_stop(struct driver_data *drv_data, const char* msg)
516{
Stephen Street8d94cc52006-12-10 02:18:54 -0800517 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800518 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800519 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800520 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200521 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200522 pxa2xx_spi_flush(drv_data);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200523 pxa2xx_spi_write(drv_data, SSCR0,
524 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Stephen Street8d94cc52006-12-10 02:18:54 -0800525
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
532static void int_transfer_complete(struct driver_data *drv_data)
533{
Stephen Street8d94cc52006-12-10 02:18:54 -0800534 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800535 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800536 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800537 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200538 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800539
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300540 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800541 drv_data->cur_msg->actual_length += drv_data->len -
542 (drv_data->rx_end - drv_data->rx);
543
Ned Forrester84235972008-09-13 02:33:17 -0700544 /* Transfer delays and chip select release are
545 * handled in pump_transfers or giveback
546 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800547
548 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200549 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800550
551 /* Schedule transfer tasklet */
552 tasklet_schedule(&drv_data->pump_transfers);
553}
554
Stephen Streete0c99052006-03-07 23:53:24 -0800555static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
556{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200557 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
558 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800559
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200560 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800561
Stephen Street8d94cc52006-12-10 02:18:54 -0800562 if (irq_status & SSSR_ROR) {
563 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
564 return IRQ_HANDLED;
565 }
Stephen Streete0c99052006-03-07 23:53:24 -0800566
Stephen Street8d94cc52006-12-10 02:18:54 -0800567 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200568 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800569 if (drv_data->read(drv_data)) {
570 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800571 return IRQ_HANDLED;
572 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800573 }
Stephen Streete0c99052006-03-07 23:53:24 -0800574
Stephen Street8d94cc52006-12-10 02:18:54 -0800575 /* 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 Streete0c99052006-03-07 23:53:24 -0800580 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800581 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800582
Stephen Street8d94cc52006-12-10 02:18:54 -0800583 if (drv_data->read(drv_data)) {
584 int_transfer_complete(drv_data);
585 return IRQ_HANDLED;
586 }
Stephen Streete0c99052006-03-07 23:53:24 -0800587
Stephen Street8d94cc52006-12-10 02:18:54 -0800588 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800589 u32 bytes_left;
590 u32 sccr1_reg;
591
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200592 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800593 sccr1_reg &= ~SSCR1_TIE;
594
595 /*
596 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300597 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800598 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800599 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700600 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800601
Weike Chen4fdb2422014-10-08 08:50:22 -0700602 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800603
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 Street8d94cc52006-12-10 02:18:54 -0800610 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800611
Weike Chen4fdb2422014-10-08 08:50:22 -0700612 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
613 if (rx_thre > bytes_left)
614 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800615
Weike Chen4fdb2422014-10-08 08:50:22 -0700616 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800617 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200618 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800619 }
620
Stephen Street5daa3ba2006-05-20 15:00:19 -0700621 /* We did something */
622 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800623}
624
David Howells7d12e782006-10-05 14:55:46 +0100625static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800626{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400627 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200628 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800629 u32 mask = drv_data->mask_sr;
630 u32 status;
631
Mika Westerberg7d94a502013-01-22 12:26:30 +0200632 /*
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 Westerberg269e4a42013-09-04 13:37:43 +0300641 /*
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 Nikulac039dd22014-12-18 15:04:23 +0200647 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300648 if (status == ~0)
649 return IRQ_NONE;
650
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200651 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800652
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 Streete0c99052006-03-07 23:53:24 -0800659
660 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700661
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200662 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 Siewior2a8626a2010-11-22 17:12:17 -0800668 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200669 pxa2xx_spi_write(drv_data, SSTO, 0);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800670 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700671
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300672 dev_err(&drv_data->pdev->dev,
673 "bad message state in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700674
Stephen Streete0c99052006-03-07 23:53:24 -0800675 /* Never fail */
676 return IRQ_HANDLED;
677 }
678
679 return drv_data->transfer_handler(drv_data);
680}
681
Weike Chene5262d02014-11-26 02:35:10 -0800682/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200683 * 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 Chene5262d02014-11-26 02:35:10 -0800713 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200714static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800715{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200716 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 Chene5262d02014-11-26 02:35:10 -0800726
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200727 /* Case 1 */
728
729 /* Set initial value for DDS_CLK_RATE */
730 mul = (1 << 24) >> 1;
731
732 /* Calculate initial quot */
733 q1 = DIV_ROUND_CLOSEST(fref1, rate);
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
758 q2 = DIV_ROUND_CLOSEST(fref2, rate);
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
777 /* Check case 3 only If the divisor is big enough */
778 if (fref / rate >= 80) {
779 u64 fssp;
780 u32 m;
781
782 /* Calculate initial quot */
783 q1 = DIV_ROUND_CLOSEST(fref, rate);
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 Chene5262d02014-11-26 02:35:10 -0800796 }
797 }
798
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200799 *dds = mul;
800 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800801}
802
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200803static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800804{
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +0300805 unsigned long ssp_clk = drv_data->master->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200806 const struct ssp_device *ssp = drv_data->ssp;
807
808 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800809
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800810 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200811 return (ssp_clk / (2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800812 else
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200813 return (ssp_clk / rate - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800814}
815
Weike Chene5262d02014-11-26 02:35:10 -0800816static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
817 struct chip_data *chip, int rate)
818{
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200819 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800820
821 switch (drv_data->ssp_type) {
822 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200823 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300824 break;
Weike Chene5262d02014-11-26 02:35:10 -0800825 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200826 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300827 break;
Weike Chene5262d02014-11-26 02:35:10 -0800828 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200829 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800830}
831
Stephen Streete0c99052006-03-07 23:53:24 -0800832static void pump_transfers(unsigned long data)
833{
834 struct driver_data *drv_data = (struct driver_data *)data;
835 struct spi_message *message = NULL;
836 struct spi_transfer *transfer = NULL;
837 struct spi_transfer *previous = NULL;
838 struct chip_data *chip = NULL;
Stephen Street9708c122006-03-28 14:05:23 -0800839 u32 clk_div = 0;
840 u8 bits = 0;
841 u32 speed = 0;
842 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800843 u32 cr1;
844 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
845 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Weike Chen4fdb2422014-10-08 08:50:22 -0700846 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800847
848 /* Get current state information */
849 message = drv_data->cur_msg;
850 transfer = drv_data->cur_transfer;
851 chip = drv_data->cur_chip;
852
853 /* Handle for abort */
854 if (message->state == ERROR_STATE) {
855 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700856 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800857 return;
858 }
859
860 /* Handle end of message */
861 if (message->state == DONE_STATE) {
862 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700863 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800864 return;
865 }
866
Ned Forrester84235972008-09-13 02:33:17 -0700867 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800868 if (message->state == RUNNING_STATE) {
869 previous = list_entry(transfer->transfer_list.prev,
870 struct spi_transfer,
871 transfer_list);
872 if (previous->delay_usecs)
873 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700874
875 /* Drop chip select only if cs_change is requested */
876 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700877 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800878 }
879
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200880 /* Check if we can DMA this transfer */
881 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700882
883 /* reject already-mapped transfers; PIO won't always work */
884 if (message->is_dma_mapped
885 || transfer->rx_dma || transfer->tx_dma) {
886 dev_err(&drv_data->pdev->dev,
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300887 "pump_transfers: mapped transfer length of "
888 "%u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700889 transfer->len, MAX_DMA_LEN);
890 message->status = -EINVAL;
891 giveback(drv_data);
892 return;
893 }
894
895 /* warn ... we force this to PIO mode */
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300896 dev_warn_ratelimited(&message->spi->dev,
897 "pump_transfers: DMA disabled for transfer length %ld "
898 "greater than %d\n",
899 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800900 }
901
Stephen Streete0c99052006-03-07 23:53:24 -0800902 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200903 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800904 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
905 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700906 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800907 return;
908 }
Stephen Street9708c122006-03-28 14:05:23 -0800909 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800910 drv_data->tx = (void *)transfer->tx_buf;
911 drv_data->tx_end = drv_data->tx + transfer->len;
912 drv_data->rx = transfer->rx_buf;
913 drv_data->rx_end = drv_data->rx + transfer->len;
914 drv_data->rx_dma = transfer->rx_dma;
915 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200916 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800917 drv_data->write = drv_data->tx ? chip->write : null_writer;
918 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800919
920 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300921 bits = transfer->bits_per_word;
922 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -0800923
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300924 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800925
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300926 if (bits <= 8) {
927 drv_data->n_bytes = 1;
928 drv_data->read = drv_data->read != null_reader ?
929 u8_reader : null_reader;
930 drv_data->write = drv_data->write != null_writer ?
931 u8_writer : null_writer;
932 } else if (bits <= 16) {
933 drv_data->n_bytes = 2;
934 drv_data->read = drv_data->read != null_reader ?
935 u16_reader : null_reader;
936 drv_data->write = drv_data->write != null_writer ?
937 u16_writer : null_writer;
938 } else if (bits <= 32) {
939 drv_data->n_bytes = 4;
940 drv_data->read = drv_data->read != null_reader ?
941 u32_reader : null_reader;
942 drv_data->write = drv_data->write != null_writer ?
943 u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -0800944 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300945 /*
946 * if bits/word is changed in dma mode, then must check the
947 * thresholds and burst also
948 */
949 if (chip->enable_dma) {
950 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
951 message->spi,
952 bits, &dma_burst,
953 &dma_thresh))
954 dev_warn_ratelimited(&message->spi->dev,
955 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
956 }
957
958 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
Stephen Street9708c122006-03-28 14:05:23 -0800959
Stephen Streete0c99052006-03-07 23:53:24 -0800960 message->state = RUNNING_STATE;
961
Ned Forrester7e964452008-09-13 02:33:18 -0700962 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200963 if (pxa2xx_spi_dma_is_possible(drv_data->len))
964 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700965 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800966
967 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200968 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800969
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200970 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800971
Stephen Street8d94cc52006-12-10 02:18:54 -0800972 /* Clear status and start DMA engine */
973 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200974 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200975
976 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800977 } else {
978 /* Ensure we have the correct interrupt handler */
979 drv_data->transfer_handler = interrupt_transfer;
980
Stephen Street8d94cc52006-12-10 02:18:54 -0800981 /* Clear status */
982 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800983 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800984 }
985
Mika Westerberga0d26422013-01-22 12:26:32 +0200986 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200987 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
988 != chip->lpss_rx_threshold)
989 pxa2xx_spi_write(drv_data, SSIRF,
990 chip->lpss_rx_threshold);
991 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
992 != chip->lpss_tx_threshold)
993 pxa2xx_spi_write(drv_data, SSITF,
994 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +0200995 }
996
Weike Chene5262d02014-11-26 02:35:10 -0800997 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200998 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
999 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001000
Stephen Street8d94cc52006-12-10 02:18:54 -08001001 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001002 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1003 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1004 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -08001005 /* stop the SSP, and update the other bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001006 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001007 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001008 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001009 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001010 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001011 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001012 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001013
Stephen Street8d94cc52006-12-10 02:18:54 -08001014 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001015 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001016 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -08001017 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001018
Eric Miaoa7bb3902009-04-06 19:00:54 -07001019 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001020
1021 /* after chip select, release the data by enabling service
1022 * requests and interrupts, without changing any mode bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001023 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Stephen Streete0c99052006-03-07 23:53:24 -08001024}
1025
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001026static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
1027 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001028{
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001029 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -08001030
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001031 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -08001032 /* Initial message state*/
1033 drv_data->cur_msg->state = START_STATE;
1034 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1035 struct spi_transfer,
1036 transfer_list);
1037
Stephen Street8d94cc52006-12-10 02:18:54 -08001038 /* prepare to setup the SSP, in pump_transfers, using the per
1039 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001040 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001041
1042 /* Mark as busy and launch transfers */
1043 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -08001044 return 0;
1045}
1046
Mika Westerberg7d94a502013-01-22 12:26:30 +02001047static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
1048{
1049 struct driver_data *drv_data = spi_master_get_devdata(master);
1050
1051 /* Disable the SSP now */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001052 pxa2xx_spi_write(drv_data, SSCR0,
1053 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001054
Mika Westerberg7d94a502013-01-22 12:26:30 +02001055 return 0;
1056}
1057
Eric Miaoa7bb3902009-04-06 19:00:54 -07001058static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1059 struct pxa2xx_spi_chip *chip_info)
1060{
1061 int err = 0;
1062
1063 if (chip == NULL || chip_info == NULL)
1064 return 0;
1065
1066 /* NOTE: setup() can be called multiple times, possibly with
1067 * different chip_info, release previously requested GPIO
1068 */
1069 if (gpio_is_valid(chip->gpio_cs))
1070 gpio_free(chip->gpio_cs);
1071
1072 /* If (*cs_control) is provided, ignore GPIO chip select */
1073 if (chip_info->cs_control) {
1074 chip->cs_control = chip_info->cs_control;
1075 return 0;
1076 }
1077
1078 if (gpio_is_valid(chip_info->gpio_cs)) {
1079 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1080 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001081 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1082 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001083 return err;
1084 }
1085
1086 chip->gpio_cs = chip_info->gpio_cs;
1087 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1088
1089 err = gpio_direction_output(chip->gpio_cs,
1090 !chip->gpio_cs_inverted);
1091 }
1092
1093 return err;
1094}
1095
Stephen Streete0c99052006-03-07 23:53:24 -08001096static int setup(struct spi_device *spi)
1097{
1098 struct pxa2xx_spi_chip *chip_info = NULL;
1099 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001100 const struct lpss_config *config;
Stephen Streete0c99052006-03-07 23:53:24 -08001101 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1102 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +02001103 uint tx_thres, tx_hi_thres, rx_thres;
Jarkko Nikulab69d42b2015-09-15 16:26:28 +03001104 u32 cr0;
Mika Westerberga0d26422013-01-22 12:26:32 +02001105
Weike Chene5262d02014-11-26 02:35:10 -08001106 switch (drv_data->ssp_type) {
1107 case QUARK_X1000_SSP:
1108 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1109 tx_hi_thres = 0;
1110 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1111 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001112 case LPSS_LPT_SSP:
1113 case LPSS_BYT_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001114 case LPSS_SPT_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001115 config = lpss_get_config(drv_data);
1116 tx_thres = config->tx_threshold_lo;
1117 tx_hi_thres = config->tx_threshold_hi;
1118 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001119 break;
1120 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001121 tx_thres = TX_THRESH_DFLT;
1122 tx_hi_thres = 0;
1123 rx_thres = RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001124 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001125 }
Stephen Streete0c99052006-03-07 23:53:24 -08001126
Stephen Street8d94cc52006-12-10 02:18:54 -08001127 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001128 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001129 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001130 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001131 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001132 return -ENOMEM;
1133
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001134 if (drv_data->ssp_type == CE4100_SSP) {
1135 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001136 dev_err(&spi->dev,
1137 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001138 kfree(chip);
1139 return -EINVAL;
1140 }
1141
1142 chip->frm = spi->chip_select;
1143 } else
1144 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001145 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001146 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001147 }
1148
Stephen Street8d94cc52006-12-10 02:18:54 -08001149 /* protocol drivers may change the chip settings, so...
1150 * if chip_info exists, use it */
1151 chip_info = spi->controller_data;
1152
Stephen Streete0c99052006-03-07 23:53:24 -08001153 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001154 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001155 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001156 if (chip_info->timeout)
1157 chip->timeout = chip_info->timeout;
1158 if (chip_info->tx_threshold)
1159 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001160 if (chip_info->tx_hi_threshold)
1161 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001162 if (chip_info->rx_threshold)
1163 rx_thres = chip_info->rx_threshold;
1164 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001165 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001166 if (chip_info->enable_loopback)
1167 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +02001168 } else if (ACPI_HANDLE(&spi->dev)) {
1169 /*
1170 * Slave devices enumerated from ACPI namespace don't
1171 * usually have chip_info but we still might want to use
1172 * DMA with them.
1173 */
1174 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001175 }
1176
Mika Westerberga0d26422013-01-22 12:26:32 +02001177 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1178 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1179 | SSITF_TxHiThresh(tx_hi_thres);
1180
Stephen Street8d94cc52006-12-10 02:18:54 -08001181 /* set dma burst and threshold outside of chip_info path so that if
1182 * chip_info goes away after setting chip->enable_dma, the
1183 * burst and threshold can still respond to changes in bits_per_word */
1184 if (chip->enable_dma) {
1185 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001186 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1187 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001188 &chip->dma_burst_size,
1189 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001190 dev_warn(&spi->dev,
1191 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001192 }
1193 }
1194
Weike Chene5262d02014-11-26 02:35:10 -08001195 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
Stephen Streete0c99052006-03-07 23:53:24 -08001196
Weike Chene5262d02014-11-26 02:35:10 -08001197 switch (drv_data->ssp_type) {
1198 case QUARK_X1000_SSP:
1199 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1200 & QUARK_X1000_SSCR1_RFT)
1201 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1202 & QUARK_X1000_SSCR1_TFT);
1203 break;
1204 default:
1205 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1206 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1207 break;
1208 }
1209
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001210 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1211 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1212 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001213
Mika Westerbergb8331722013-01-22 12:26:31 +02001214 if (spi->mode & SPI_LOOP)
1215 chip->cr1 |= SSCR1_LBM;
1216
Stephen Streete0c99052006-03-07 23:53:24 -08001217 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Jarkko Nikulab69d42b2015-09-15 16:26:28 +03001218 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, spi->bits_per_word);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001219 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +03001220 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1221 drv_data->master->max_speed_hz
Jarkko Nikulab69d42b2015-09-15 16:26:28 +03001222 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
Eric Miaoc9840da2010-03-16 16:48:01 +08001223 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001224 else
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +03001225 dev_dbg(&spi->dev, "%u Hz actual, %s\n",
1226 drv_data->master->max_speed_hz / 2
Jarkko Nikulab69d42b2015-09-15 16:26:28 +03001227 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
Eric Miaoc9840da2010-03-16 16:48:01 +08001228 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001229
1230 if (spi->bits_per_word <= 8) {
1231 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001232 chip->read = u8_reader;
1233 chip->write = u8_writer;
1234 } else if (spi->bits_per_word <= 16) {
1235 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001236 chip->read = u16_reader;
1237 chip->write = u16_writer;
1238 } else if (spi->bits_per_word <= 32) {
Stephen Streete0c99052006-03-07 23:53:24 -08001239 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001240 chip->read = u32_reader;
1241 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001242 }
Stephen Streete0c99052006-03-07 23:53:24 -08001243
1244 spi_set_ctldata(spi, chip);
1245
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001246 if (drv_data->ssp_type == CE4100_SSP)
1247 return 0;
1248
Eric Miaoa7bb3902009-04-06 19:00:54 -07001249 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001250}
1251
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001252static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001253{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001254 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001255 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001256
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001257 if (!chip)
1258 return;
1259
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001260 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001261 gpio_free(chip->gpio_cs);
1262
Stephen Streete0c99052006-03-07 23:53:24 -08001263 kfree(chip);
1264}
1265
Mika Westerberga3496852013-01-22 12:26:33 +02001266#ifdef CONFIG_ACPI
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001267
Mathias Krause8422ddf2015-06-13 14:22:14 +02001268static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001269 { "INT33C0", LPSS_LPT_SSP },
1270 { "INT33C1", LPSS_LPT_SSP },
1271 { "INT3430", LPSS_LPT_SSP },
1272 { "INT3431", LPSS_LPT_SSP },
1273 { "80860F0E", LPSS_BYT_SSP },
1274 { "8086228E", LPSS_BYT_SSP },
1275 { },
1276};
1277MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1278
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001279/*
1280 * PCI IDs of compound devices that integrate both host controller and private
1281 * integrated DMA engine. Please note these are not used in module
1282 * autoloading and probing in this module but matching the LPSS SSP type.
1283 */
1284static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1285 /* SPT-LP */
1286 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1287 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1288 /* SPT-H */
1289 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1290 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
Axel Lin94e5c232015-08-04 13:52:22 +08001291 { },
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001292};
1293
1294static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1295{
1296 struct device *dev = param;
1297
1298 if (dev != chan->device->dev->parent)
1299 return false;
1300
1301 return true;
1302}
1303
Mika Westerberga3496852013-01-22 12:26:33 +02001304static struct pxa2xx_spi_master *
1305pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1306{
1307 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001308 struct acpi_device *adev;
1309 struct ssp_device *ssp;
1310 struct resource *res;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001311 const struct acpi_device_id *adev_id = NULL;
1312 const struct pci_device_id *pcidev_id = NULL;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001313 int devid, type;
Mika Westerberga3496852013-01-22 12:26:33 +02001314
1315 if (!ACPI_HANDLE(&pdev->dev) ||
1316 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1317 return NULL;
1318
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001319 if (dev_is_pci(pdev->dev.parent))
1320 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
1321 to_pci_dev(pdev->dev.parent));
1322 else
1323 adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
1324 &pdev->dev);
1325
1326 if (adev_id)
1327 type = (int)adev_id->driver_data;
1328 else if (pcidev_id)
1329 type = (int)pcidev_id->driver_data;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001330 else
1331 return NULL;
1332
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001333 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001334 if (!pdata)
Mika Westerberga3496852013-01-22 12:26:33 +02001335 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001336
1337 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1338 if (!res)
1339 return NULL;
1340
1341 ssp = &pdata->ssp;
1342
1343 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301344 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1345 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001346 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001347
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001348 if (pcidev_id) {
1349 pdata->tx_param = pdev->dev.parent;
1350 pdata->rx_param = pdev->dev.parent;
1351 pdata->dma_filter = pxa2xx_spi_idma_filter;
1352 }
1353
Mika Westerberga3496852013-01-22 12:26:33 +02001354 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1355 ssp->irq = platform_get_irq(pdev, 0);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001356 ssp->type = type;
Mika Westerberga3496852013-01-22 12:26:33 +02001357 ssp->pdev = pdev;
1358
1359 ssp->port_id = -1;
1360 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1361 ssp->port_id = devid;
1362
1363 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001364 pdata->enable_dma = true;
Mika Westerberga3496852013-01-22 12:26:33 +02001365
1366 return pdata;
1367}
1368
Mika Westerberga3496852013-01-22 12:26:33 +02001369#else
1370static inline struct pxa2xx_spi_master *
1371pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1372{
1373 return NULL;
1374}
1375#endif
1376
Grant Likelyfd4a3192012-12-07 16:57:14 +00001377static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001378{
1379 struct device *dev = &pdev->dev;
1380 struct pxa2xx_spi_master *platform_info;
1381 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001382 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001383 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001384 int status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001385 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001386
Mika Westerberg851bacf2013-01-07 12:44:33 +02001387 platform_info = dev_get_platdata(dev);
1388 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001389 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1390 if (!platform_info) {
1391 dev_err(&pdev->dev, "missing platform data\n");
1392 return -ENODEV;
1393 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001394 }
Stephen Streete0c99052006-03-07 23:53:24 -08001395
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001396 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001397 if (!ssp)
1398 ssp = &platform_info->ssp;
1399
1400 if (!ssp->mmio_base) {
1401 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001402 return -ENODEV;
1403 }
1404
Jarkko Nikula757fe8d2015-08-05 10:04:05 +03001405 master = spi_alloc_master(dev, sizeof(struct driver_data));
Stephen Streete0c99052006-03-07 23:53:24 -08001406 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001407 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001408 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001409 return -ENOMEM;
1410 }
1411 drv_data = spi_master_get_devdata(master);
1412 drv_data->master = master;
1413 drv_data->master_info = platform_info;
1414 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001415 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001416
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001417 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001418 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001419 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001420 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001421
Mika Westerberg851bacf2013-01-07 12:44:33 +02001422 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001423 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001424 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001425 master->cleanup = cleanup;
1426 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001427 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001428 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001429 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001430
eric miao2f1a74e2007-11-21 18:50:53 +08001431 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001432
eric miao2f1a74e2007-11-21 18:50:53 +08001433 drv_data->ioaddr = ssp->mmio_base;
1434 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001435 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001436 switch (drv_data->ssp_type) {
1437 case QUARK_X1000_SSP:
1438 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1439 break;
1440 default:
1441 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1442 break;
1443 }
1444
Stephen Streete0c99052006-03-07 23:53:24 -08001445 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1446 drv_data->dma_cr1 = 0;
1447 drv_data->clear_sr = SSSR_ROR;
1448 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1449 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001450 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001451 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001452 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001453 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1454 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1455 }
1456
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001457 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1458 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001459 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001460 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001461 goto out_error_master_alloc;
1462 }
1463
1464 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001465 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001466 status = pxa2xx_spi_dma_setup(drv_data);
1467 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001468 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001469 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001470 }
Stephen Streete0c99052006-03-07 23:53:24 -08001471 }
1472
1473 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001474 clk_prepare_enable(ssp->clk);
1475
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +03001476 master->max_speed_hz = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001477
1478 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001479 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001480 switch (drv_data->ssp_type) {
1481 case QUARK_X1000_SSP:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001482 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1483 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1484 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001485
1486 /* using the Motorola SPI protocol and use 8 bit frame */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001487 pxa2xx_spi_write(drv_data, SSCR0,
1488 QUARK_X1000_SSCR0_Motorola
1489 | QUARK_X1000_SSCR0_DataSize(8));
Weike Chene5262d02014-11-26 02:35:10 -08001490 break;
1491 default:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001492 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1493 SSCR1_TxTresh(TX_THRESH_DFLT);
1494 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1495 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1496 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001497 break;
1498 }
1499
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001500 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001501 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001502
1503 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001504 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001505
Jarkko Nikula7566bcc2014-12-18 15:04:20 +02001506 if (is_lpss_ssp(drv_data))
1507 lpss_ssp_setup(drv_data);
Mika Westerberga0d26422013-01-22 12:26:32 +02001508
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001509 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1510 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001511
Antonio Ospite836d1a222014-05-30 18:18:09 +02001512 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1513 pm_runtime_use_autosuspend(&pdev->dev);
1514 pm_runtime_set_active(&pdev->dev);
1515 pm_runtime_enable(&pdev->dev);
1516
Stephen Streete0c99052006-03-07 23:53:24 -08001517 /* Register with the SPI framework */
1518 platform_set_drvdata(pdev, drv_data);
Jingoo Hana807fcd2013-09-24 13:46:55 +09001519 status = devm_spi_register_master(&pdev->dev, master);
Stephen Streete0c99052006-03-07 23:53:24 -08001520 if (status != 0) {
1521 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001522 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001523 }
1524
1525 return status;
1526
Stephen Streete0c99052006-03-07 23:53:24 -08001527out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001528 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001529 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001530 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001531
1532out_error_master_alloc:
1533 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001534 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001535 return status;
1536}
1537
1538static int pxa2xx_spi_remove(struct platform_device *pdev)
1539{
1540 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001541 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001542
1543 if (!drv_data)
1544 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001545 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001546
Mika Westerberg7d94a502013-01-22 12:26:30 +02001547 pm_runtime_get_sync(&pdev->dev);
1548
Stephen Streete0c99052006-03-07 23:53:24 -08001549 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001550 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001551 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001552
1553 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001554 if (drv_data->master_info->enable_dma)
1555 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001556
Mika Westerberg7d94a502013-01-22 12:26:30 +02001557 pm_runtime_put_noidle(&pdev->dev);
1558 pm_runtime_disable(&pdev->dev);
1559
Stephen Streete0c99052006-03-07 23:53:24 -08001560 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001561 free_irq(ssp->irq, drv_data);
1562
1563 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001564 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001565
Stephen Streete0c99052006-03-07 23:53:24 -08001566 return 0;
1567}
1568
1569static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1570{
1571 int status = 0;
1572
1573 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1574 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1575}
1576
Mika Westerberg382cebb2014-01-16 14:50:55 +02001577#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001578static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001579{
Mike Rapoport86d25932009-07-21 17:50:16 +03001580 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001581 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001582 int status = 0;
1583
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001584 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001585 if (status != 0)
1586 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001587 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001588
1589 if (!pm_runtime_suspended(dev))
1590 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001591
1592 return 0;
1593}
1594
Mike Rapoport86d25932009-07-21 17:50:16 +03001595static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001596{
Mike Rapoport86d25932009-07-21 17:50:16 +03001597 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001598 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001599 int status = 0;
1600
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001601 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001602
Stephen Streete0c99052006-03-07 23:53:24 -08001603 /* Enable the SSP clock */
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001604 if (!pm_runtime_suspended(dev))
1605 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001606
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001607 /* Restore LPSS private register bits */
Jarkko Nikula48421ad2015-01-28 10:09:42 +02001608 if (is_lpss_ssp(drv_data))
1609 lpss_ssp_setup(drv_data);
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001610
Stephen Streete0c99052006-03-07 23:53:24 -08001611 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001612 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001613 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001614 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001615 return status;
1616 }
1617
1618 return 0;
1619}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001620#endif
1621
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001622#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001623static int pxa2xx_spi_runtime_suspend(struct device *dev)
1624{
1625 struct driver_data *drv_data = dev_get_drvdata(dev);
1626
1627 clk_disable_unprepare(drv_data->ssp->clk);
1628 return 0;
1629}
1630
1631static int pxa2xx_spi_runtime_resume(struct device *dev)
1632{
1633 struct driver_data *drv_data = dev_get_drvdata(dev);
1634
1635 clk_prepare_enable(drv_data->ssp->clk);
1636 return 0;
1637}
1638#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001639
Alexey Dobriyan47145212009-12-14 18:00:08 -08001640static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001641 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1642 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1643 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001644};
Stephen Streete0c99052006-03-07 23:53:24 -08001645
1646static struct platform_driver driver = {
1647 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001648 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001649 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001650 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001651 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001652 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001653 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001654 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001655};
1656
1657static int __init pxa2xx_spi_init(void)
1658{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001659 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001660}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001661subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001662
1663static void __exit pxa2xx_spi_exit(void)
1664{
1665 platform_driver_unregister(&driver);
1666}
1667module_exit(pxa2xx_spi_exit);