blob: 9060aee5a7b1cdfcf44ae01a2b3bfab64947f660 [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
Jarkko Nikula624ea722015-10-28 15:13:39 +020064#define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
65#define LPSS_CS_CONTROL_SW_MODE BIT(0)
66#define LPSS_CS_CONTROL_CS_HIGH BIT(1)
Mika Westerberga0d26422013-01-22 12:26:32 +020067
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 */
Jarkko Nikula0e897212015-10-22 16:44:42 +0300252 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200253 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
254 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300255 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200256
257 /* Enable multiblock DMA transfers */
Mika Westerberg1de70612013-07-03 13:25:06 +0300258 if (drv_data->master_info->enable_dma) {
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300259 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300260
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300261 if (config->reg_general >= 0) {
262 value = __lpss_ssp_read_priv(drv_data,
263 config->reg_general);
Jarkko Nikula624ea722015-10-28 15:13:39 +0200264 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
Jarkko Nikula82ba2c22015-06-04 16:55:12 +0300265 __lpss_ssp_write_priv(drv_data,
266 config->reg_general, value);
267 }
Mika Westerberg1de70612013-07-03 13:25:06 +0300268 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200269}
270
271static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
272{
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300273 const struct lpss_config *config;
Mika Westerberga0d26422013-01-22 12:26:32 +0200274 u32 value;
275
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300276 config = lpss_get_config(drv_data);
277
278 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
Mika Westerberga0d26422013-01-22 12:26:32 +0200279 if (enable)
Jarkko Nikula624ea722015-10-28 15:13:39 +0200280 value &= ~LPSS_CS_CONTROL_CS_HIGH;
Mika Westerberga0d26422013-01-22 12:26:32 +0200281 else
Jarkko Nikula624ea722015-10-28 15:13:39 +0200282 value |= LPSS_CS_CONTROL_CS_HIGH;
Jarkko Nikuladccf7362015-06-04 16:55:11 +0300283 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
Mika Westerberga0d26422013-01-22 12:26:32 +0200284}
285
Eric Miaoa7bb3902009-04-06 19:00:54 -0700286static void cs_assert(struct driver_data *drv_data)
287{
288 struct chip_data *chip = drv_data->cur_chip;
289
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800290 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200291 pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800292 return;
293 }
294
Eric Miaoa7bb3902009-04-06 19:00:54 -0700295 if (chip->cs_control) {
296 chip->cs_control(PXA2XX_CS_ASSERT);
297 return;
298 }
299
Mika Westerberga0d26422013-01-22 12:26:32 +0200300 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700301 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200302 return;
303 }
304
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200305 if (is_lpss_ssp(drv_data))
306 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700307}
308
309static void cs_deassert(struct driver_data *drv_data)
310{
311 struct chip_data *chip = drv_data->cur_chip;
312
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800313 if (drv_data->ssp_type == CE4100_SSP)
314 return;
315
Eric Miaoa7bb3902009-04-06 19:00:54 -0700316 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300317 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700318 return;
319 }
320
Mika Westerberga0d26422013-01-22 12:26:32 +0200321 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700322 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200323 return;
324 }
325
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200326 if (is_lpss_ssp(drv_data))
327 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700328}
329
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200330int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800331{
332 unsigned long limit = loops_per_jiffy << 1;
333
Stephen Streete0c99052006-03-07 23:53:24 -0800334 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200335 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
336 pxa2xx_spi_read(drv_data, SSDR);
337 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800338 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800339
340 return limit;
341}
342
Stephen Street8d94cc52006-12-10 02:18:54 -0800343static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800344{
Stephen Street9708c122006-03-28 14:05:23 -0800345 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800346
Weike Chen4fdb2422014-10-08 08:50:22 -0700347 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800348 || (drv_data->tx == drv_data->tx_end))
349 return 0;
350
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200351 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800352 drv_data->tx += n_bytes;
353
354 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800355}
356
Stephen Street8d94cc52006-12-10 02:18:54 -0800357static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800358{
Stephen Street9708c122006-03-28 14:05:23 -0800359 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800360
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200361 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
362 && (drv_data->rx < drv_data->rx_end)) {
363 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800364 drv_data->rx += n_bytes;
365 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800366
367 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800368}
369
Stephen Street8d94cc52006-12-10 02:18:54 -0800370static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800371{
Weike Chen4fdb2422014-10-08 08:50:22 -0700372 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800373 || (drv_data->tx == drv_data->tx_end))
374 return 0;
375
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200376 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800377 ++drv_data->tx;
378
379 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800380}
381
Stephen Street8d94cc52006-12-10 02:18:54 -0800382static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800383{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200384 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
385 && (drv_data->rx < drv_data->rx_end)) {
386 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800387 ++drv_data->rx;
388 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800389
390 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800391}
392
Stephen Street8d94cc52006-12-10 02:18:54 -0800393static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800394{
Weike Chen4fdb2422014-10-08 08:50:22 -0700395 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800396 || (drv_data->tx == drv_data->tx_end))
397 return 0;
398
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200399 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800400 drv_data->tx += 2;
401
402 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800403}
404
Stephen Street8d94cc52006-12-10 02:18:54 -0800405static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800406{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200407 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
408 && (drv_data->rx < drv_data->rx_end)) {
409 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800410 drv_data->rx += 2;
411 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800412
413 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800414}
Stephen Street8d94cc52006-12-10 02:18:54 -0800415
416static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800417{
Weike Chen4fdb2422014-10-08 08:50:22 -0700418 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800419 || (drv_data->tx == drv_data->tx_end))
420 return 0;
421
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200422 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800423 drv_data->tx += 4;
424
425 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800426}
427
Stephen Street8d94cc52006-12-10 02:18:54 -0800428static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800429{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200430 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
431 && (drv_data->rx < drv_data->rx_end)) {
432 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800433 drv_data->rx += 4;
434 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800435
436 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800437}
438
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200439void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800440{
441 struct spi_message *msg = drv_data->cur_msg;
442 struct spi_transfer *trans = drv_data->cur_transfer;
443
444 /* Move to next transfer */
445 if (trans->transfer_list.next != &msg->transfers) {
446 drv_data->cur_transfer =
447 list_entry(trans->transfer_list.next,
448 struct spi_transfer,
449 transfer_list);
450 return RUNNING_STATE;
451 } else
452 return DONE_STATE;
453}
454
Stephen Streete0c99052006-03-07 23:53:24 -0800455/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700456static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800457{
458 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700459 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800460
Stephen Street5daa3ba2006-05-20 15:00:19 -0700461 msg = drv_data->cur_msg;
462 drv_data->cur_msg = NULL;
463 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700464
Axel Lin23e2c2a2014-02-12 22:13:27 +0800465 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
Stephen Streete0c99052006-03-07 23:53:24 -0800466 transfer_list);
467
Ned Forrester84235972008-09-13 02:33:17 -0700468 /* Delay if requested before any change in chip select */
469 if (last_transfer->delay_usecs)
470 udelay(last_transfer->delay_usecs);
471
472 /* Drop chip select UNLESS cs_change is true or we are returning
473 * a message with an error, or next message is for another chip
474 */
Stephen Streete0c99052006-03-07 23:53:24 -0800475 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700476 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700477 else {
478 struct spi_message *next_msg;
479
480 /* Holding of cs was hinted, but we need to make sure
481 * the next message is for the same chip. Don't waste
482 * time with the following tests unless this was hinted.
483 *
484 * We cannot postpone this until pump_messages, because
485 * after calling msg->complete (below) the driver that
486 * sent the current message could be unloaded, which
487 * could invalidate the cs_control() callback...
488 */
489
490 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200491 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700492
493 /* see if the next and current messages point
494 * to the same chip
495 */
496 if (next_msg && next_msg->spi != msg->spi)
497 next_msg = NULL;
498 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700499 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700500 }
Stephen Streete0c99052006-03-07 23:53:24 -0800501
Eric Miaoa7bb3902009-04-06 19:00:54 -0700502 drv_data->cur_chip = NULL;
Mika Westerbergc957e8f2014-12-29 10:33:36 +0200503 spi_finalize_current_message(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -0800504}
505
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800506static void reset_sccr1(struct driver_data *drv_data)
507{
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800508 struct chip_data *chip = drv_data->cur_chip;
509 u32 sccr1_reg;
510
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200511 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800512 sccr1_reg &= ~SSCR1_RFT;
513 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200514 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800515}
516
Stephen Street8d94cc52006-12-10 02:18:54 -0800517static void int_error_stop(struct driver_data *drv_data, const char* msg)
518{
Stephen Street8d94cc52006-12-10 02:18:54 -0800519 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800520 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800521 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800522 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200523 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200524 pxa2xx_spi_flush(drv_data);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200525 pxa2xx_spi_write(drv_data, SSCR0,
526 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Stephen Street8d94cc52006-12-10 02:18:54 -0800527
528 dev_err(&drv_data->pdev->dev, "%s\n", msg);
529
530 drv_data->cur_msg->state = ERROR_STATE;
531 tasklet_schedule(&drv_data->pump_transfers);
532}
533
534static void int_transfer_complete(struct driver_data *drv_data)
535{
Stephen Street8d94cc52006-12-10 02:18:54 -0800536 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800537 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800538 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800539 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200540 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800541
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300542 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800543 drv_data->cur_msg->actual_length += drv_data->len -
544 (drv_data->rx_end - drv_data->rx);
545
Ned Forrester84235972008-09-13 02:33:17 -0700546 /* Transfer delays and chip select release are
547 * handled in pump_transfers or giveback
548 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800549
550 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200551 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800552
553 /* Schedule transfer tasklet */
554 tasklet_schedule(&drv_data->pump_transfers);
555}
556
Stephen Streete0c99052006-03-07 23:53:24 -0800557static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
558{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200559 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
560 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800561
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200562 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800563
Stephen Street8d94cc52006-12-10 02:18:54 -0800564 if (irq_status & SSSR_ROR) {
565 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
566 return IRQ_HANDLED;
567 }
Stephen Streete0c99052006-03-07 23:53:24 -0800568
Stephen Street8d94cc52006-12-10 02:18:54 -0800569 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200570 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800571 if (drv_data->read(drv_data)) {
572 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800573 return IRQ_HANDLED;
574 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800575 }
Stephen Streete0c99052006-03-07 23:53:24 -0800576
Stephen Street8d94cc52006-12-10 02:18:54 -0800577 /* Drain rx fifo, Fill tx fifo and prevent overruns */
578 do {
579 if (drv_data->read(drv_data)) {
580 int_transfer_complete(drv_data);
581 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800582 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800583 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800584
Stephen Street8d94cc52006-12-10 02:18:54 -0800585 if (drv_data->read(drv_data)) {
586 int_transfer_complete(drv_data);
587 return IRQ_HANDLED;
588 }
Stephen Streete0c99052006-03-07 23:53:24 -0800589
Stephen Street8d94cc52006-12-10 02:18:54 -0800590 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800591 u32 bytes_left;
592 u32 sccr1_reg;
593
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200594 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800595 sccr1_reg &= ~SSCR1_TIE;
596
597 /*
598 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300599 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800600 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800601 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700602 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800603
Weike Chen4fdb2422014-10-08 08:50:22 -0700604 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800605
606 bytes_left = drv_data->rx_end - drv_data->rx;
607 switch (drv_data->n_bytes) {
608 case 4:
609 bytes_left >>= 1;
610 case 2:
611 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800612 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800613
Weike Chen4fdb2422014-10-08 08:50:22 -0700614 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
615 if (rx_thre > bytes_left)
616 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800617
Weike Chen4fdb2422014-10-08 08:50:22 -0700618 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800619 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200620 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800621 }
622
Stephen Street5daa3ba2006-05-20 15:00:19 -0700623 /* We did something */
624 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800625}
626
David Howells7d12e782006-10-05 14:55:46 +0100627static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800628{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400629 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200630 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800631 u32 mask = drv_data->mask_sr;
632 u32 status;
633
Mika Westerberg7d94a502013-01-22 12:26:30 +0200634 /*
635 * The IRQ might be shared with other peripherals so we must first
636 * check that are we RPM suspended or not. If we are we assume that
637 * the IRQ was not for us (we shouldn't be RPM suspended when the
638 * interrupt is enabled).
639 */
640 if (pm_runtime_suspended(&drv_data->pdev->dev))
641 return IRQ_NONE;
642
Mika Westerberg269e4a42013-09-04 13:37:43 +0300643 /*
644 * If the device is not yet in RPM suspended state and we get an
645 * interrupt that is meant for another device, check if status bits
646 * are all set to one. That means that the device is already
647 * powered off.
648 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200649 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300650 if (status == ~0)
651 return IRQ_NONE;
652
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200653 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800654
655 /* Ignore possible writes if we don't need to write */
656 if (!(sccr1_reg & SSCR1_TIE))
657 mask &= ~SSSR_TFS;
658
659 if (!(status & mask))
660 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800661
662 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700663
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200664 pxa2xx_spi_write(drv_data, SSCR0,
665 pxa2xx_spi_read(drv_data, SSCR0)
666 & ~SSCR0_SSE);
667 pxa2xx_spi_write(drv_data, SSCR1,
668 pxa2xx_spi_read(drv_data, SSCR1)
669 & ~drv_data->int_cr1);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800670 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200671 pxa2xx_spi_write(drv_data, SSTO, 0);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800672 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700673
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300674 dev_err(&drv_data->pdev->dev,
675 "bad message state in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700676
Stephen Streete0c99052006-03-07 23:53:24 -0800677 /* Never fail */
678 return IRQ_HANDLED;
679 }
680
681 return drv_data->transfer_handler(drv_data);
682}
683
Weike Chene5262d02014-11-26 02:35:10 -0800684/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200685 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
686 * input frequency by fractions of 2^24. It also has a divider by 5.
687 *
688 * There are formulas to get baud rate value for given input frequency and
689 * divider parameters, such as DDS_CLK_RATE and SCR:
690 *
691 * Fsys = 200MHz
692 *
693 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
694 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
695 *
696 * DDS_CLK_RATE either 2^n or 2^n / 5.
697 * SCR is in range 0 .. 255
698 *
699 * Divisor = 5^i * 2^j * 2 * k
700 * i = [0, 1] i = 1 iff j = 0 or j > 3
701 * j = [0, 23] j = 0 iff i = 1
702 * k = [1, 256]
703 * Special case: j = 0, i = 1: Divisor = 2 / 5
704 *
705 * Accordingly to the specification the recommended values for DDS_CLK_RATE
706 * are:
707 * Case 1: 2^n, n = [0, 23]
708 * Case 2: 2^24 * 2 / 5 (0x666666)
709 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
710 *
711 * In all cases the lowest possible value is better.
712 *
713 * The function calculates parameters for all cases and chooses the one closest
714 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800715 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200716static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800717{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200718 unsigned long xtal = 200000000;
719 unsigned long fref = xtal / 2; /* mandatory division by 2,
720 see (2) */
721 /* case 3 */
722 unsigned long fref1 = fref / 2; /* case 1 */
723 unsigned long fref2 = fref * 2 / 5; /* case 2 */
724 unsigned long scale;
725 unsigned long q, q1, q2;
726 long r, r1, r2;
727 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800728
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200729 /* Case 1 */
730
731 /* Set initial value for DDS_CLK_RATE */
732 mul = (1 << 24) >> 1;
733
734 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300735 q1 = DIV_ROUND_UP(fref1, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200736
737 /* Scale q1 if it's too big */
738 if (q1 > 256) {
739 /* Scale q1 to range [1, 512] */
740 scale = fls_long(q1 - 1);
741 if (scale > 9) {
742 q1 >>= scale - 9;
743 mul >>= scale - 9;
744 }
745
746 /* Round the result if we have a remainder */
747 q1 += q1 & 1;
748 }
749
750 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
751 scale = __ffs(q1);
752 q1 >>= scale;
753 mul >>= scale;
754
755 /* Get the remainder */
756 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
757
758 /* Case 2 */
759
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300760 q2 = DIV_ROUND_UP(fref2, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200761 r2 = abs(fref2 / q2 - rate);
762
763 /*
764 * Choose the best between two: less remainder we have the better. We
765 * can't go case 2 if q2 is greater than 256 since SCR register can
766 * hold only values 0 .. 255.
767 */
768 if (r2 >= r1 || q2 > 256) {
769 /* case 1 is better */
770 r = r1;
771 q = q1;
772 } else {
773 /* case 2 is better */
774 r = r2;
775 q = q2;
776 mul = (1 << 24) * 2 / 5;
777 }
778
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300779 /* Check case 3 only if the divisor is big enough */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200780 if (fref / rate >= 80) {
781 u64 fssp;
782 u32 m;
783
784 /* Calculate initial quot */
Andy Shevchenko3ad48062015-10-13 17:09:14 +0300785 q1 = DIV_ROUND_UP(fref, rate);
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200786 m = (1 << 24) / q1;
787
788 /* Get the remainder */
789 fssp = (u64)fref * m;
790 do_div(fssp, 1 << 24);
791 r1 = abs(fssp - rate);
792
793 /* Choose this one if it suits better */
794 if (r1 < r) {
795 /* case 3 is better */
796 q = 1;
797 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800798 }
799 }
800
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200801 *dds = mul;
802 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800803}
804
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200805static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800806{
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +0300807 unsigned long ssp_clk = drv_data->master->max_speed_hz;
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200808 const struct ssp_device *ssp = drv_data->ssp;
809
810 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800811
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800812 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200813 return (ssp_clk / (2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800814 else
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200815 return (ssp_clk / rate - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800816}
817
Weike Chene5262d02014-11-26 02:35:10 -0800818static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300819 int rate)
Weike Chene5262d02014-11-26 02:35:10 -0800820{
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300821 struct chip_data *chip = drv_data->cur_chip;
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200822 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800823
824 switch (drv_data->ssp_type) {
825 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200826 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300827 break;
Weike Chene5262d02014-11-26 02:35:10 -0800828 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200829 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300830 break;
Weike Chene5262d02014-11-26 02:35:10 -0800831 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200832 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800833}
834
Stephen Streete0c99052006-03-07 23:53:24 -0800835static void pump_transfers(unsigned long data)
836{
837 struct driver_data *drv_data = (struct driver_data *)data;
838 struct spi_message *message = NULL;
839 struct spi_transfer *transfer = NULL;
840 struct spi_transfer *previous = NULL;
841 struct chip_data *chip = NULL;
Stephen Street9708c122006-03-28 14:05:23 -0800842 u32 clk_div = 0;
843 u8 bits = 0;
844 u32 speed = 0;
845 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800846 u32 cr1;
847 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
848 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Weike Chen4fdb2422014-10-08 08:50:22 -0700849 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800850
851 /* Get current state information */
852 message = drv_data->cur_msg;
853 transfer = drv_data->cur_transfer;
854 chip = drv_data->cur_chip;
855
856 /* Handle for abort */
857 if (message->state == ERROR_STATE) {
858 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700859 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800860 return;
861 }
862
863 /* Handle end of message */
864 if (message->state == DONE_STATE) {
865 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700866 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800867 return;
868 }
869
Ned Forrester84235972008-09-13 02:33:17 -0700870 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800871 if (message->state == RUNNING_STATE) {
872 previous = list_entry(transfer->transfer_list.prev,
873 struct spi_transfer,
874 transfer_list);
875 if (previous->delay_usecs)
876 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700877
878 /* Drop chip select only if cs_change is requested */
879 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700880 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800881 }
882
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200883 /* Check if we can DMA this transfer */
884 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700885
886 /* reject already-mapped transfers; PIO won't always work */
887 if (message->is_dma_mapped
888 || transfer->rx_dma || transfer->tx_dma) {
889 dev_err(&drv_data->pdev->dev,
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300890 "pump_transfers: mapped transfer length of "
891 "%u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700892 transfer->len, MAX_DMA_LEN);
893 message->status = -EINVAL;
894 giveback(drv_data);
895 return;
896 }
897
898 /* warn ... we force this to PIO mode */
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300899 dev_warn_ratelimited(&message->spi->dev,
900 "pump_transfers: DMA disabled for transfer length %ld "
901 "greater than %d\n",
902 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800903 }
904
Stephen Streete0c99052006-03-07 23:53:24 -0800905 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200906 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800907 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
908 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700909 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800910 return;
911 }
Stephen Street9708c122006-03-28 14:05:23 -0800912 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800913 drv_data->tx = (void *)transfer->tx_buf;
914 drv_data->tx_end = drv_data->tx + transfer->len;
915 drv_data->rx = transfer->rx_buf;
916 drv_data->rx_end = drv_data->rx + transfer->len;
917 drv_data->rx_dma = transfer->rx_dma;
918 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200919 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800920 drv_data->write = drv_data->tx ? chip->write : null_writer;
921 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800922
923 /* Change speed and bit per word on a per transfer */
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300924 bits = transfer->bits_per_word;
925 speed = transfer->speed_hz;
Stephen Street9708c122006-03-28 14:05:23 -0800926
Andy Shevchenkod2c2f6a2015-10-22 16:44:40 +0300927 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800928
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300929 if (bits <= 8) {
930 drv_data->n_bytes = 1;
931 drv_data->read = drv_data->read != null_reader ?
932 u8_reader : null_reader;
933 drv_data->write = drv_data->write != null_writer ?
934 u8_writer : null_writer;
935 } else if (bits <= 16) {
936 drv_data->n_bytes = 2;
937 drv_data->read = drv_data->read != null_reader ?
938 u16_reader : null_reader;
939 drv_data->write = drv_data->write != null_writer ?
940 u16_writer : null_writer;
941 } else if (bits <= 32) {
942 drv_data->n_bytes = 4;
943 drv_data->read = drv_data->read != null_reader ?
944 u32_reader : null_reader;
945 drv_data->write = drv_data->write != null_writer ?
946 u32_writer : null_writer;
Stephen Street9708c122006-03-28 14:05:23 -0800947 }
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300948 /*
949 * if bits/word is changed in dma mode, then must check the
950 * thresholds and burst also
951 */
952 if (chip->enable_dma) {
953 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
954 message->spi,
955 bits, &dma_burst,
956 &dma_thresh))
957 dev_warn_ratelimited(&message->spi->dev,
958 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
959 }
960
Andy Shevchenkod74c4b12015-10-22 16:44:39 +0300961 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Jarkko Nikula196b0e22015-09-15 16:26:27 +0300962 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
Andy Shevchenkod74c4b12015-10-22 16:44:39 +0300963 if (!pxa25x_ssp_comp(drv_data))
964 dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
965 drv_data->master->max_speed_hz
966 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
967 chip->enable_dma ? "DMA" : "PIO");
968 else
969 dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
970 drv_data->master->max_speed_hz / 2
971 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
972 chip->enable_dma ? "DMA" : "PIO");
Stephen Street9708c122006-03-28 14:05:23 -0800973
Stephen Streete0c99052006-03-07 23:53:24 -0800974 message->state = RUNNING_STATE;
975
Ned Forrester7e964452008-09-13 02:33:18 -0700976 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200977 if (pxa2xx_spi_dma_is_possible(drv_data->len))
978 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700979 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800980
981 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200982 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800983
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200984 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800985
Stephen Street8d94cc52006-12-10 02:18:54 -0800986 /* Clear status and start DMA engine */
987 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200988 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200989
990 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800991 } else {
992 /* Ensure we have the correct interrupt handler */
993 drv_data->transfer_handler = interrupt_transfer;
994
Stephen Street8d94cc52006-12-10 02:18:54 -0800995 /* Clear status */
996 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800997 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800998 }
999
Mika Westerberga0d26422013-01-22 12:26:32 +02001000 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001001 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1002 != chip->lpss_rx_threshold)
1003 pxa2xx_spi_write(drv_data, SSIRF,
1004 chip->lpss_rx_threshold);
1005 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1006 != chip->lpss_tx_threshold)
1007 pxa2xx_spi_write(drv_data, SSITF,
1008 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +02001009 }
1010
Weike Chene5262d02014-11-26 02:35:10 -08001011 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001012 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1013 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -08001014
Stephen Street8d94cc52006-12-10 02:18:54 -08001015 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001016 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1017 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1018 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -08001019 /* stop the SSP, and update the other bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001020 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001021 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001022 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001023 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001024 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001025 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001026 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001027
Stephen Street8d94cc52006-12-10 02:18:54 -08001028 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001029 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001030 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -08001031 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001032
Eric Miaoa7bb3902009-04-06 19:00:54 -07001033 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001034
1035 /* after chip select, release the data by enabling service
1036 * requests and interrupts, without changing any mode bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001037 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Stephen Streete0c99052006-03-07 23:53:24 -08001038}
1039
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001040static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
1041 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001042{
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001043 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -08001044
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001045 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -08001046 /* Initial message state*/
1047 drv_data->cur_msg->state = START_STATE;
1048 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1049 struct spi_transfer,
1050 transfer_list);
1051
Stephen Street8d94cc52006-12-10 02:18:54 -08001052 /* prepare to setup the SSP, in pump_transfers, using the per
1053 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001054 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001055
1056 /* Mark as busy and launch transfers */
1057 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -08001058 return 0;
1059}
1060
Mika Westerberg7d94a502013-01-22 12:26:30 +02001061static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
1062{
1063 struct driver_data *drv_data = spi_master_get_devdata(master);
1064
1065 /* Disable the SSP now */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001066 pxa2xx_spi_write(drv_data, SSCR0,
1067 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001068
Mika Westerberg7d94a502013-01-22 12:26:30 +02001069 return 0;
1070}
1071
Eric Miaoa7bb3902009-04-06 19:00:54 -07001072static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1073 struct pxa2xx_spi_chip *chip_info)
1074{
1075 int err = 0;
1076
1077 if (chip == NULL || chip_info == NULL)
1078 return 0;
1079
1080 /* NOTE: setup() can be called multiple times, possibly with
1081 * different chip_info, release previously requested GPIO
1082 */
1083 if (gpio_is_valid(chip->gpio_cs))
1084 gpio_free(chip->gpio_cs);
1085
1086 /* If (*cs_control) is provided, ignore GPIO chip select */
1087 if (chip_info->cs_control) {
1088 chip->cs_control = chip_info->cs_control;
1089 return 0;
1090 }
1091
1092 if (gpio_is_valid(chip_info->gpio_cs)) {
1093 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1094 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001095 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1096 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001097 return err;
1098 }
1099
1100 chip->gpio_cs = chip_info->gpio_cs;
1101 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1102
1103 err = gpio_direction_output(chip->gpio_cs,
1104 !chip->gpio_cs_inverted);
1105 }
1106
1107 return err;
1108}
1109
Stephen Streete0c99052006-03-07 23:53:24 -08001110static int setup(struct spi_device *spi)
1111{
1112 struct pxa2xx_spi_chip *chip_info = NULL;
1113 struct chip_data *chip;
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001114 const struct lpss_config *config;
Stephen Streete0c99052006-03-07 23:53:24 -08001115 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Mika Westerberga0d26422013-01-22 12:26:32 +02001116 uint tx_thres, tx_hi_thres, rx_thres;
1117
Weike Chene5262d02014-11-26 02:35:10 -08001118 switch (drv_data->ssp_type) {
1119 case QUARK_X1000_SSP:
1120 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1121 tx_hi_thres = 0;
1122 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1123 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001124 case LPSS_LPT_SSP:
1125 case LPSS_BYT_SSP:
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001126 case LPSS_SPT_SSP:
Jarkko Nikuladccf7362015-06-04 16:55:11 +03001127 config = lpss_get_config(drv_data);
1128 tx_thres = config->tx_threshold_lo;
1129 tx_hi_thres = config->tx_threshold_hi;
1130 rx_thres = config->rx_threshold;
Weike Chene5262d02014-11-26 02:35:10 -08001131 break;
1132 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001133 tx_thres = TX_THRESH_DFLT;
1134 tx_hi_thres = 0;
1135 rx_thres = RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001136 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001137 }
Stephen Streete0c99052006-03-07 23:53:24 -08001138
Stephen Street8d94cc52006-12-10 02:18:54 -08001139 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001140 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001141 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001142 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001143 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001144 return -ENOMEM;
1145
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001146 if (drv_data->ssp_type == CE4100_SSP) {
1147 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001148 dev_err(&spi->dev,
1149 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001150 kfree(chip);
1151 return -EINVAL;
1152 }
1153
1154 chip->frm = spi->chip_select;
1155 } else
1156 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001157 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001158 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001159 }
1160
Stephen Street8d94cc52006-12-10 02:18:54 -08001161 /* protocol drivers may change the chip settings, so...
1162 * if chip_info exists, use it */
1163 chip_info = spi->controller_data;
1164
Stephen Streete0c99052006-03-07 23:53:24 -08001165 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001166 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001167 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001168 if (chip_info->timeout)
1169 chip->timeout = chip_info->timeout;
1170 if (chip_info->tx_threshold)
1171 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001172 if (chip_info->tx_hi_threshold)
1173 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001174 if (chip_info->rx_threshold)
1175 rx_thres = chip_info->rx_threshold;
1176 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001177 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001178 if (chip_info->enable_loopback)
1179 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +02001180 } else if (ACPI_HANDLE(&spi->dev)) {
1181 /*
1182 * Slave devices enumerated from ACPI namespace don't
1183 * usually have chip_info but we still might want to use
1184 * DMA with them.
1185 */
1186 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001187 }
1188
Mika Westerberga0d26422013-01-22 12:26:32 +02001189 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1190 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1191 | SSITF_TxHiThresh(tx_hi_thres);
1192
Stephen Street8d94cc52006-12-10 02:18:54 -08001193 /* set dma burst and threshold outside of chip_info path so that if
1194 * chip_info goes away after setting chip->enable_dma, the
1195 * burst and threshold can still respond to changes in bits_per_word */
1196 if (chip->enable_dma) {
1197 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001198 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1199 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001200 &chip->dma_burst_size,
1201 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001202 dev_warn(&spi->dev,
1203 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001204 }
1205 }
1206
Weike Chene5262d02014-11-26 02:35:10 -08001207 switch (drv_data->ssp_type) {
1208 case QUARK_X1000_SSP:
1209 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1210 & QUARK_X1000_SSCR1_RFT)
1211 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1212 & QUARK_X1000_SSCR1_TFT);
1213 break;
1214 default:
1215 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1216 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1217 break;
1218 }
1219
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001220 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1221 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1222 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001223
Mika Westerbergb8331722013-01-22 12:26:31 +02001224 if (spi->mode & SPI_LOOP)
1225 chip->cr1 |= SSCR1_LBM;
1226
Stephen Streete0c99052006-03-07 23:53:24 -08001227 if (spi->bits_per_word <= 8) {
1228 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001229 chip->read = u8_reader;
1230 chip->write = u8_writer;
1231 } else if (spi->bits_per_word <= 16) {
1232 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001233 chip->read = u16_reader;
1234 chip->write = u16_writer;
1235 } else if (spi->bits_per_word <= 32) {
Stephen Streete0c99052006-03-07 23:53:24 -08001236 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001237 chip->read = u32_reader;
1238 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001239 }
Stephen Streete0c99052006-03-07 23:53:24 -08001240
1241 spi_set_ctldata(spi, chip);
1242
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001243 if (drv_data->ssp_type == CE4100_SSP)
1244 return 0;
1245
Eric Miaoa7bb3902009-04-06 19:00:54 -07001246 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001247}
1248
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001249static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001250{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001251 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001252 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001253
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001254 if (!chip)
1255 return;
1256
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001257 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001258 gpio_free(chip->gpio_cs);
1259
Stephen Streete0c99052006-03-07 23:53:24 -08001260 kfree(chip);
1261}
1262
Mika Westerberga3496852013-01-22 12:26:33 +02001263#ifdef CONFIG_ACPI
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001264
Mathias Krause8422ddf2015-06-13 14:22:14 +02001265static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001266 { "INT33C0", LPSS_LPT_SSP },
1267 { "INT33C1", LPSS_LPT_SSP },
1268 { "INT3430", LPSS_LPT_SSP },
1269 { "INT3431", LPSS_LPT_SSP },
1270 { "80860F0E", LPSS_BYT_SSP },
1271 { "8086228E", LPSS_BYT_SSP },
1272 { },
1273};
1274MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1275
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001276/*
1277 * PCI IDs of compound devices that integrate both host controller and private
1278 * integrated DMA engine. Please note these are not used in module
1279 * autoloading and probing in this module but matching the LPSS SSP type.
1280 */
1281static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1282 /* SPT-LP */
1283 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1284 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1285 /* SPT-H */
1286 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1287 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
Axel Lin94e5c232015-08-04 13:52:22 +08001288 { },
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001289};
1290
1291static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1292{
1293 struct device *dev = param;
1294
1295 if (dev != chan->device->dev->parent)
1296 return false;
1297
1298 return true;
1299}
1300
Mika Westerberga3496852013-01-22 12:26:33 +02001301static struct pxa2xx_spi_master *
1302pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1303{
1304 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001305 struct acpi_device *adev;
1306 struct ssp_device *ssp;
1307 struct resource *res;
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001308 const struct acpi_device_id *adev_id = NULL;
1309 const struct pci_device_id *pcidev_id = NULL;
Jarkko Nikula3b8b6d02015-10-22 16:44:41 +03001310 unsigned int devid;
1311 int type;
Mika Westerberga3496852013-01-22 12:26:33 +02001312
Jarkko Nikulab9f69402015-09-25 10:27:18 +03001313 adev = ACPI_COMPANION(&pdev->dev);
1314 if (!adev)
Mika Westerberga3496852013-01-22 12:26:33 +02001315 return NULL;
1316
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001317 if (dev_is_pci(pdev->dev.parent))
1318 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
1319 to_pci_dev(pdev->dev.parent));
1320 else
1321 adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
1322 &pdev->dev);
1323
1324 if (adev_id)
1325 type = (int)adev_id->driver_data;
1326 else if (pcidev_id)
1327 type = (int)pcidev_id->driver_data;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001328 else
1329 return NULL;
1330
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001331 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001332 if (!pdata)
Mika Westerberga3496852013-01-22 12:26:33 +02001333 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001334
1335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1336 if (!res)
1337 return NULL;
1338
1339 ssp = &pdata->ssp;
1340
1341 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301342 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1343 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001344 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001345
Jarkko Nikula34cadd92015-07-30 16:30:07 +03001346 if (pcidev_id) {
1347 pdata->tx_param = pdev->dev.parent;
1348 pdata->rx_param = pdev->dev.parent;
1349 pdata->dma_filter = pxa2xx_spi_idma_filter;
1350 }
1351
Mika Westerberga3496852013-01-22 12:26:33 +02001352 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1353 ssp->irq = platform_get_irq(pdev, 0);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001354 ssp->type = type;
Mika Westerberga3496852013-01-22 12:26:33 +02001355 ssp->pdev = pdev;
1356
1357 ssp->port_id = -1;
Jarkko Nikula3b8b6d02015-10-22 16:44:41 +03001358 if (adev->pnp.unique_id && !kstrtouint(adev->pnp.unique_id, 0, &devid))
Mika Westerberga3496852013-01-22 12:26:33 +02001359 ssp->port_id = devid;
1360
1361 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001362 pdata->enable_dma = true;
Mika Westerberga3496852013-01-22 12:26:33 +02001363
1364 return pdata;
1365}
1366
Mika Westerberga3496852013-01-22 12:26:33 +02001367#else
1368static inline struct pxa2xx_spi_master *
1369pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1370{
1371 return NULL;
1372}
1373#endif
1374
Grant Likelyfd4a3192012-12-07 16:57:14 +00001375static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001376{
1377 struct device *dev = &pdev->dev;
1378 struct pxa2xx_spi_master *platform_info;
1379 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001380 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001381 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001382 int status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001383 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001384
Mika Westerberg851bacf2013-01-07 12:44:33 +02001385 platform_info = dev_get_platdata(dev);
1386 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001387 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1388 if (!platform_info) {
1389 dev_err(&pdev->dev, "missing platform data\n");
1390 return -ENODEV;
1391 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001392 }
Stephen Streete0c99052006-03-07 23:53:24 -08001393
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001394 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001395 if (!ssp)
1396 ssp = &platform_info->ssp;
1397
1398 if (!ssp->mmio_base) {
1399 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001400 return -ENODEV;
1401 }
1402
Jarkko Nikula757fe8d2015-08-05 10:04:05 +03001403 master = spi_alloc_master(dev, sizeof(struct driver_data));
Stephen Streete0c99052006-03-07 23:53:24 -08001404 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001405 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001406 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001407 return -ENOMEM;
1408 }
1409 drv_data = spi_master_get_devdata(master);
1410 drv_data->master = master;
1411 drv_data->master_info = platform_info;
1412 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001413 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001414
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001415 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001416 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001417 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001418 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001419
Mika Westerberg851bacf2013-01-07 12:44:33 +02001420 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001421 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001422 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001423 master->cleanup = cleanup;
1424 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001425 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001426 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001427 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001428
eric miao2f1a74e2007-11-21 18:50:53 +08001429 drv_data->ssp_type = ssp->type;
Stephen Streete0c99052006-03-07 23:53:24 -08001430
eric miao2f1a74e2007-11-21 18:50:53 +08001431 drv_data->ioaddr = ssp->mmio_base;
1432 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001433 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001434 switch (drv_data->ssp_type) {
1435 case QUARK_X1000_SSP:
1436 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1437 break;
1438 default:
1439 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1440 break;
1441 }
1442
Stephen Streete0c99052006-03-07 23:53:24 -08001443 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1444 drv_data->dma_cr1 = 0;
1445 drv_data->clear_sr = SSSR_ROR;
1446 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1447 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001448 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001449 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001450 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001451 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1452 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1453 }
1454
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001455 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1456 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001457 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001458 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001459 goto out_error_master_alloc;
1460 }
1461
1462 /* Setup DMA if requested */
Stephen Streete0c99052006-03-07 23:53:24 -08001463 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001464 status = pxa2xx_spi_dma_setup(drv_data);
1465 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001466 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001467 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001468 }
Stephen Streete0c99052006-03-07 23:53:24 -08001469 }
1470
1471 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001472 clk_prepare_enable(ssp->clk);
1473
Jarkko Nikula0eca7cf2015-09-25 10:27:17 +03001474 master->max_speed_hz = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001475
1476 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001477 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001478 switch (drv_data->ssp_type) {
1479 case QUARK_X1000_SSP:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001480 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1481 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1482 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001483
1484 /* using the Motorola SPI protocol and use 8 bit frame */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001485 pxa2xx_spi_write(drv_data, SSCR0,
1486 QUARK_X1000_SSCR0_Motorola
1487 | QUARK_X1000_SSCR0_DataSize(8));
Weike Chene5262d02014-11-26 02:35:10 -08001488 break;
1489 default:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001490 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1491 SSCR1_TxTresh(TX_THRESH_DFLT);
1492 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1493 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1494 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001495 break;
1496 }
1497
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001498 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001499 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001500
1501 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001502 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001503
Jarkko Nikula7566bcc2014-12-18 15:04:20 +02001504 if (is_lpss_ssp(drv_data))
1505 lpss_ssp_setup(drv_data);
Mika Westerberga0d26422013-01-22 12:26:32 +02001506
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001507 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1508 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001509
Antonio Ospite836d1a222014-05-30 18:18:09 +02001510 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1511 pm_runtime_use_autosuspend(&pdev->dev);
1512 pm_runtime_set_active(&pdev->dev);
1513 pm_runtime_enable(&pdev->dev);
1514
Stephen Streete0c99052006-03-07 23:53:24 -08001515 /* Register with the SPI framework */
1516 platform_set_drvdata(pdev, drv_data);
Jingoo Hana807fcd2013-09-24 13:46:55 +09001517 status = devm_spi_register_master(&pdev->dev, master);
Stephen Streete0c99052006-03-07 23:53:24 -08001518 if (status != 0) {
1519 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001520 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001521 }
1522
1523 return status;
1524
Stephen Streete0c99052006-03-07 23:53:24 -08001525out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001526 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001527 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001528 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001529
1530out_error_master_alloc:
1531 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001532 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001533 return status;
1534}
1535
1536static int pxa2xx_spi_remove(struct platform_device *pdev)
1537{
1538 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001539 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001540
1541 if (!drv_data)
1542 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001543 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001544
Mika Westerberg7d94a502013-01-22 12:26:30 +02001545 pm_runtime_get_sync(&pdev->dev);
1546
Stephen Streete0c99052006-03-07 23:53:24 -08001547 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001548 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001549 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001550
1551 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001552 if (drv_data->master_info->enable_dma)
1553 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001554
Mika Westerberg7d94a502013-01-22 12:26:30 +02001555 pm_runtime_put_noidle(&pdev->dev);
1556 pm_runtime_disable(&pdev->dev);
1557
Stephen Streete0c99052006-03-07 23:53:24 -08001558 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001559 free_irq(ssp->irq, drv_data);
1560
1561 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001562 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001563
Stephen Streete0c99052006-03-07 23:53:24 -08001564 return 0;
1565}
1566
1567static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1568{
1569 int status = 0;
1570
1571 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1572 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1573}
1574
Mika Westerberg382cebb2014-01-16 14:50:55 +02001575#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001576static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001577{
Mike Rapoport86d25932009-07-21 17:50:16 +03001578 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001579 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001580 int status = 0;
1581
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001582 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001583 if (status != 0)
1584 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001585 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001586
1587 if (!pm_runtime_suspended(dev))
1588 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001589
1590 return 0;
1591}
1592
Mike Rapoport86d25932009-07-21 17:50:16 +03001593static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001594{
Mike Rapoport86d25932009-07-21 17:50:16 +03001595 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001596 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001597 int status = 0;
1598
1599 /* Enable the SSP clock */
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001600 if (!pm_runtime_suspended(dev))
1601 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001602
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001603 /* Restore LPSS private register bits */
Jarkko Nikula48421ad2015-01-28 10:09:42 +02001604 if (is_lpss_ssp(drv_data))
1605 lpss_ssp_setup(drv_data);
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001606
Stephen Streete0c99052006-03-07 23:53:24 -08001607 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001608 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001609 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001610 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001611 return status;
1612 }
1613
1614 return 0;
1615}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001616#endif
1617
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001618#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001619static int pxa2xx_spi_runtime_suspend(struct device *dev)
1620{
1621 struct driver_data *drv_data = dev_get_drvdata(dev);
1622
1623 clk_disable_unprepare(drv_data->ssp->clk);
1624 return 0;
1625}
1626
1627static int pxa2xx_spi_runtime_resume(struct device *dev)
1628{
1629 struct driver_data *drv_data = dev_get_drvdata(dev);
1630
1631 clk_prepare_enable(drv_data->ssp->clk);
1632 return 0;
1633}
1634#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001635
Alexey Dobriyan47145212009-12-14 18:00:08 -08001636static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001637 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1638 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1639 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001640};
Stephen Streete0c99052006-03-07 23:53:24 -08001641
1642static struct platform_driver driver = {
1643 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001644 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001645 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001646 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001647 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001648 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001649 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001650 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001651};
1652
1653static int __init pxa2xx_spi_init(void)
1654{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001655 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001656}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001657subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001658
1659static void __exit pxa2xx_spi_exit(void)
1660{
1661 platform_driver_unregister(&driver);
1662}
1663module_exit(pxa2xx_spi_exit);