blob: c1a50674c1e359deb3e83fc2a04c9acf2324f58d [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/ioport.h>
24#include <linux/errno.h>
Sachin Kamatcbfd6a22013-04-08 15:49:33 +053025#include <linux/err.h>
Stephen Streete0c99052006-03-07 23:53:24 -080026#include <linux/interrupt.h>
27#include <linux/platform_device.h>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080028#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080029#include <linux/spi/spi.h>
30#include <linux/workqueue.h>
Stephen Streete0c99052006-03-07 23:53:24 -080031#include <linux/delay.h>
Eric Miaoa7bb3902009-04-06 19:00:54 -070032#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Mika Westerberg3343b7a2013-01-22 12:26:27 +020034#include <linux/clk.h>
Mika Westerberg7d94a502013-01-22 12:26:30 +020035#include <linux/pm_runtime.h>
Mika Westerberga3496852013-01-22 12:26:33 +020036#include <linux/acpi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080037
38#include <asm/io.h>
39#include <asm/irq.h>
Stephen Streete0c99052006-03-07 23:53:24 -080040#include <asm/delay.h>
Stephen Streete0c99052006-03-07 23:53:24 -080041
Mika Westerbergcd7bed02013-01-22 12:26:28 +020042#include "spi-pxa2xx.h"
Stephen Streete0c99052006-03-07 23:53:24 -080043
44MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080045MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080046MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070047MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080048
49#define MAX_BUSES 3
50
Vernon Sauderf1f640a2008-10-15 22:02:43 -070051#define TIMOUT_DFLT 1000
52
Ned Forresterb97c74b2008-02-23 15:23:40 -080053/*
54 * for testing SSCR1 changes that require SSP restart, basically
55 * everything except the service and interrupt enables, the pxa270 developer
56 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
57 * list, but the PXA255 dev man says all bits without really meaning the
58 * service and interrupt enables
59 */
60#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080061 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080062 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
63 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
64 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
65 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080066
Mika Westerberga0d26422013-01-22 12:26:32 +020067#define LPSS_RX_THRESH_DFLT 64
68#define LPSS_TX_LOTHRESH_DFLT 160
69#define LPSS_TX_HITHRESH_DFLT 224
70
71/* Offset from drv_data->lpss_base */
Mika Westerberg1de70612013-07-03 13:25:06 +030072#define GENERAL_REG 0x08
73#define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
Mika Westerberg0054e282013-03-05 12:05:17 +020074#define SSP_REG 0x0c
Mika Westerberga0d26422013-01-22 12:26:32 +020075#define SPI_CS_CONTROL 0x18
76#define SPI_CS_CONTROL_SW_MODE BIT(0)
77#define SPI_CS_CONTROL_CS_HIGH BIT(1)
78
79static bool is_lpss_ssp(const struct driver_data *drv_data)
80{
81 return drv_data->ssp_type == LPSS_SSP;
82}
83
84/*
85 * Read and write LPSS SSP private registers. Caller must first check that
86 * is_lpss_ssp() returns true before these can be called.
87 */
88static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
89{
90 WARN_ON(!drv_data->lpss_base);
91 return readl(drv_data->lpss_base + offset);
92}
93
94static void __lpss_ssp_write_priv(struct driver_data *drv_data,
95 unsigned offset, u32 value)
96{
97 WARN_ON(!drv_data->lpss_base);
98 writel(value, drv_data->lpss_base + offset);
99}
100
101/*
102 * lpss_ssp_setup - perform LPSS SSP specific setup
103 * @drv_data: pointer to the driver private data
104 *
105 * Perform LPSS SSP specific setup. This function must be called first if
106 * one is going to use LPSS SSP private registers.
107 */
108static void lpss_ssp_setup(struct driver_data *drv_data)
109{
110 unsigned offset = 0x400;
111 u32 value, orig;
112
113 if (!is_lpss_ssp(drv_data))
114 return;
115
116 /*
117 * Perform auto-detection of the LPSS SSP private registers. They
118 * can be either at 1k or 2k offset from the base address.
119 */
120 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
121
122 value = orig | SPI_CS_CONTROL_SW_MODE;
123 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
124 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
125 if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
126 offset = 0x800;
127 goto detection_done;
128 }
129
130 value &= ~SPI_CS_CONTROL_SW_MODE;
131 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
132 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
133 if (value != orig) {
134 offset = 0x800;
135 goto detection_done;
136 }
137
138detection_done:
139 /* Now set the LPSS base */
140 drv_data->lpss_base = drv_data->ioaddr + offset;
141
142 /* Enable software chip select control */
143 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
144 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200145
146 /* Enable multiblock DMA transfers */
Mika Westerberg1de70612013-07-03 13:25:06 +0300147 if (drv_data->master_info->enable_dma) {
Mika Westerberg0054e282013-03-05 12:05:17 +0200148 __lpss_ssp_write_priv(drv_data, SSP_REG, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300149
150 value = __lpss_ssp_read_priv(drv_data, GENERAL_REG);
151 value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
152 __lpss_ssp_write_priv(drv_data, GENERAL_REG, value);
153 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200154}
155
156static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
157{
158 u32 value;
159
160 if (!is_lpss_ssp(drv_data))
161 return;
162
163 value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
164 if (enable)
165 value &= ~SPI_CS_CONTROL_CS_HIGH;
166 else
167 value |= SPI_CS_CONTROL_CS_HIGH;
168 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
169}
170
Eric Miaoa7bb3902009-04-06 19:00:54 -0700171static void cs_assert(struct driver_data *drv_data)
172{
173 struct chip_data *chip = drv_data->cur_chip;
174
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800175 if (drv_data->ssp_type == CE4100_SSP) {
176 write_SSSR(drv_data->cur_chip->frm, drv_data->ioaddr);
177 return;
178 }
179
Eric Miaoa7bb3902009-04-06 19:00:54 -0700180 if (chip->cs_control) {
181 chip->cs_control(PXA2XX_CS_ASSERT);
182 return;
183 }
184
Mika Westerberga0d26422013-01-22 12:26:32 +0200185 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700186 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200187 return;
188 }
189
190 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700191}
192
193static void cs_deassert(struct driver_data *drv_data)
194{
195 struct chip_data *chip = drv_data->cur_chip;
196
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800197 if (drv_data->ssp_type == CE4100_SSP)
198 return;
199
Eric Miaoa7bb3902009-04-06 19:00:54 -0700200 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300201 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700202 return;
203 }
204
Mika Westerberga0d26422013-01-22 12:26:32 +0200205 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700206 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200207 return;
208 }
209
210 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700211}
212
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200213int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800214{
215 unsigned long limit = loops_per_jiffy << 1;
216
David Brownellcf433692008-04-28 02:14:17 -0700217 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800218
219 do {
220 while (read_SSSR(reg) & SSSR_RNE) {
221 read_SSDR(reg);
222 }
Roel Kluin306c68a2009-04-21 12:24:46 -0700223 } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800224 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800225
226 return limit;
227}
228
Stephen Street8d94cc52006-12-10 02:18:54 -0800229static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800230{
David Brownellcf433692008-04-28 02:14:17 -0700231 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800232 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800233
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800234 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800235 || (drv_data->tx == drv_data->tx_end))
236 return 0;
237
238 write_SSDR(0, reg);
239 drv_data->tx += n_bytes;
240
241 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800242}
243
Stephen Street8d94cc52006-12-10 02:18:54 -0800244static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800245{
David Brownellcf433692008-04-28 02:14:17 -0700246 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800247 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800248
249 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800250 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800251 read_SSDR(reg);
252 drv_data->rx += n_bytes;
253 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800254
255 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800256}
257
Stephen Street8d94cc52006-12-10 02:18:54 -0800258static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800259{
David Brownellcf433692008-04-28 02:14:17 -0700260 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800261
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800262 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800263 || (drv_data->tx == drv_data->tx_end))
264 return 0;
265
266 write_SSDR(*(u8 *)(drv_data->tx), reg);
267 ++drv_data->tx;
268
269 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800270}
271
Stephen Street8d94cc52006-12-10 02:18:54 -0800272static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800273{
David Brownellcf433692008-04-28 02:14:17 -0700274 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800275
276 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800277 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800278 *(u8 *)(drv_data->rx) = read_SSDR(reg);
279 ++drv_data->rx;
280 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800281
282 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800283}
284
Stephen Street8d94cc52006-12-10 02:18:54 -0800285static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800286{
David Brownellcf433692008-04-28 02:14:17 -0700287 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800288
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800289 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800290 || (drv_data->tx == drv_data->tx_end))
291 return 0;
292
293 write_SSDR(*(u16 *)(drv_data->tx), reg);
294 drv_data->tx += 2;
295
296 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800297}
298
Stephen Street8d94cc52006-12-10 02:18:54 -0800299static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800300{
David Brownellcf433692008-04-28 02:14:17 -0700301 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800302
303 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800304 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800305 *(u16 *)(drv_data->rx) = read_SSDR(reg);
306 drv_data->rx += 2;
307 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800308
309 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800310}
Stephen Street8d94cc52006-12-10 02:18:54 -0800311
312static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800313{
David Brownellcf433692008-04-28 02:14:17 -0700314 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800315
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800316 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800317 || (drv_data->tx == drv_data->tx_end))
318 return 0;
319
320 write_SSDR(*(u32 *)(drv_data->tx), reg);
321 drv_data->tx += 4;
322
323 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800324}
325
Stephen Street8d94cc52006-12-10 02:18:54 -0800326static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800327{
David Brownellcf433692008-04-28 02:14:17 -0700328 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800329
330 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800331 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800332 *(u32 *)(drv_data->rx) = read_SSDR(reg);
333 drv_data->rx += 4;
334 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800335
336 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800337}
338
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200339void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800340{
341 struct spi_message *msg = drv_data->cur_msg;
342 struct spi_transfer *trans = drv_data->cur_transfer;
343
344 /* Move to next transfer */
345 if (trans->transfer_list.next != &msg->transfers) {
346 drv_data->cur_transfer =
347 list_entry(trans->transfer_list.next,
348 struct spi_transfer,
349 transfer_list);
350 return RUNNING_STATE;
351 } else
352 return DONE_STATE;
353}
354
Stephen Streete0c99052006-03-07 23:53:24 -0800355/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700356static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800357{
358 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700359 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800360
Stephen Street5daa3ba2006-05-20 15:00:19 -0700361 msg = drv_data->cur_msg;
362 drv_data->cur_msg = NULL;
363 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700364
365 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800366 struct spi_transfer,
367 transfer_list);
368
Ned Forrester84235972008-09-13 02:33:17 -0700369 /* Delay if requested before any change in chip select */
370 if (last_transfer->delay_usecs)
371 udelay(last_transfer->delay_usecs);
372
373 /* Drop chip select UNLESS cs_change is true or we are returning
374 * a message with an error, or next message is for another chip
375 */
Stephen Streete0c99052006-03-07 23:53:24 -0800376 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700377 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700378 else {
379 struct spi_message *next_msg;
380
381 /* Holding of cs was hinted, but we need to make sure
382 * the next message is for the same chip. Don't waste
383 * time with the following tests unless this was hinted.
384 *
385 * We cannot postpone this until pump_messages, because
386 * after calling msg->complete (below) the driver that
387 * sent the current message could be unloaded, which
388 * could invalidate the cs_control() callback...
389 */
390
391 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200392 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700393
394 /* see if the next and current messages point
395 * to the same chip
396 */
397 if (next_msg && next_msg->spi != msg->spi)
398 next_msg = NULL;
399 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700400 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700401 }
Stephen Streete0c99052006-03-07 23:53:24 -0800402
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200403 spi_finalize_current_message(drv_data->master);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700404 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800405}
406
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800407static void reset_sccr1(struct driver_data *drv_data)
408{
409 void __iomem *reg = drv_data->ioaddr;
410 struct chip_data *chip = drv_data->cur_chip;
411 u32 sccr1_reg;
412
413 sccr1_reg = read_SSCR1(reg) & ~drv_data->int_cr1;
414 sccr1_reg &= ~SSCR1_RFT;
415 sccr1_reg |= chip->threshold;
416 write_SSCR1(sccr1_reg, reg);
417}
418
Stephen Street8d94cc52006-12-10 02:18:54 -0800419static void int_error_stop(struct driver_data *drv_data, const char* msg)
420{
David Brownellcf433692008-04-28 02:14:17 -0700421 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800422
423 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800424 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800425 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800426 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800427 write_SSTO(0, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200428 pxa2xx_spi_flush(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800429 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
430
431 dev_err(&drv_data->pdev->dev, "%s\n", msg);
432
433 drv_data->cur_msg->state = ERROR_STATE;
434 tasklet_schedule(&drv_data->pump_transfers);
435}
436
437static void int_transfer_complete(struct driver_data *drv_data)
438{
David Brownellcf433692008-04-28 02:14:17 -0700439 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800440
441 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800442 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800443 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800444 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800445 write_SSTO(0, reg);
446
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300447 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800448 drv_data->cur_msg->actual_length += drv_data->len -
449 (drv_data->rx_end - drv_data->rx);
450
Ned Forrester84235972008-09-13 02:33:17 -0700451 /* Transfer delays and chip select release are
452 * handled in pump_transfers or giveback
453 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800454
455 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200456 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800457
458 /* Schedule transfer tasklet */
459 tasklet_schedule(&drv_data->pump_transfers);
460}
461
Stephen Streete0c99052006-03-07 23:53:24 -0800462static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
463{
David Brownellcf433692008-04-28 02:14:17 -0700464 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800465
Stephen Street5daa3ba2006-05-20 15:00:19 -0700466 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
467 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800468
Stephen Street8d94cc52006-12-10 02:18:54 -0800469 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800470
Stephen Street8d94cc52006-12-10 02:18:54 -0800471 if (irq_status & SSSR_ROR) {
472 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
473 return IRQ_HANDLED;
474 }
Stephen Streete0c99052006-03-07 23:53:24 -0800475
Stephen Street8d94cc52006-12-10 02:18:54 -0800476 if (irq_status & SSSR_TINT) {
477 write_SSSR(SSSR_TINT, reg);
478 if (drv_data->read(drv_data)) {
479 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800480 return IRQ_HANDLED;
481 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800482 }
Stephen Streete0c99052006-03-07 23:53:24 -0800483
Stephen Street8d94cc52006-12-10 02:18:54 -0800484 /* Drain rx fifo, Fill tx fifo and prevent overruns */
485 do {
486 if (drv_data->read(drv_data)) {
487 int_transfer_complete(drv_data);
488 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800489 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800490 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800491
Stephen Street8d94cc52006-12-10 02:18:54 -0800492 if (drv_data->read(drv_data)) {
493 int_transfer_complete(drv_data);
494 return IRQ_HANDLED;
495 }
Stephen Streete0c99052006-03-07 23:53:24 -0800496
Stephen Street8d94cc52006-12-10 02:18:54 -0800497 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800498 u32 bytes_left;
499 u32 sccr1_reg;
500
501 sccr1_reg = read_SSCR1(reg);
502 sccr1_reg &= ~SSCR1_TIE;
503
504 /*
505 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300506 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800507 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800508 if (pxa25x_ssp_comp(drv_data)) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800509
510 sccr1_reg &= ~SSCR1_RFT;
511
512 bytes_left = drv_data->rx_end - drv_data->rx;
513 switch (drv_data->n_bytes) {
514 case 4:
515 bytes_left >>= 1;
516 case 2:
517 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800518 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800519
520 if (bytes_left > RX_THRESH_DFLT)
521 bytes_left = RX_THRESH_DFLT;
522
523 sccr1_reg |= SSCR1_RxTresh(bytes_left);
Stephen Streete0c99052006-03-07 23:53:24 -0800524 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800525 write_SSCR1(sccr1_reg, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800526 }
527
Stephen Street5daa3ba2006-05-20 15:00:19 -0700528 /* We did something */
529 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800530}
531
David Howells7d12e782006-10-05 14:55:46 +0100532static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800533{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400534 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700535 void __iomem *reg = drv_data->ioaddr;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200536 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800537 u32 mask = drv_data->mask_sr;
538 u32 status;
539
Mika Westerberg7d94a502013-01-22 12:26:30 +0200540 /*
541 * The IRQ might be shared with other peripherals so we must first
542 * check that are we RPM suspended or not. If we are we assume that
543 * the IRQ was not for us (we shouldn't be RPM suspended when the
544 * interrupt is enabled).
545 */
546 if (pm_runtime_suspended(&drv_data->pdev->dev))
547 return IRQ_NONE;
548
Mika Westerberg269e4a42013-09-04 13:37:43 +0300549 /*
550 * If the device is not yet in RPM suspended state and we get an
551 * interrupt that is meant for another device, check if status bits
552 * are all set to one. That means that the device is already
553 * powered off.
554 */
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800555 status = read_SSSR(reg);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300556 if (status == ~0)
557 return IRQ_NONE;
558
559 sccr1_reg = read_SSCR1(reg);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800560
561 /* Ignore possible writes if we don't need to write */
562 if (!(sccr1_reg & SSCR1_TIE))
563 mask &= ~SSSR_TFS;
564
565 if (!(status & mask))
566 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800567
568 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700569
570 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
571 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800572 if (!pxa25x_ssp_comp(drv_data))
Stephen Street5daa3ba2006-05-20 15:00:19 -0700573 write_SSTO(0, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800574 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700575
Stephen Streete0c99052006-03-07 23:53:24 -0800576 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800577 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700578
Stephen Streete0c99052006-03-07 23:53:24 -0800579 /* Never fail */
580 return IRQ_HANDLED;
581 }
582
583 return drv_data->transfer_handler(drv_data);
584}
585
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200586static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800587{
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200588 unsigned long ssp_clk = drv_data->max_clk_rate;
589 const struct ssp_device *ssp = drv_data->ssp;
590
591 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800592
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800593 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
eric miao2f1a74e2007-11-21 18:50:53 +0800594 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
595 else
596 return ((ssp_clk / rate - 1) & 0xfff) << 8;
597}
598
Stephen Streete0c99052006-03-07 23:53:24 -0800599static void pump_transfers(unsigned long data)
600{
601 struct driver_data *drv_data = (struct driver_data *)data;
602 struct spi_message *message = NULL;
603 struct spi_transfer *transfer = NULL;
604 struct spi_transfer *previous = NULL;
605 struct chip_data *chip = NULL;
David Brownellcf433692008-04-28 02:14:17 -0700606 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800607 u32 clk_div = 0;
608 u8 bits = 0;
609 u32 speed = 0;
610 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800611 u32 cr1;
612 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
613 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800614
615 /* Get current state information */
616 message = drv_data->cur_msg;
617 transfer = drv_data->cur_transfer;
618 chip = drv_data->cur_chip;
619
620 /* Handle for abort */
621 if (message->state == ERROR_STATE) {
622 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700623 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800624 return;
625 }
626
627 /* Handle end of message */
628 if (message->state == DONE_STATE) {
629 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700630 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800631 return;
632 }
633
Ned Forrester84235972008-09-13 02:33:17 -0700634 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800635 if (message->state == RUNNING_STATE) {
636 previous = list_entry(transfer->transfer_list.prev,
637 struct spi_transfer,
638 transfer_list);
639 if (previous->delay_usecs)
640 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700641
642 /* Drop chip select only if cs_change is requested */
643 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700644 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800645 }
646
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200647 /* Check if we can DMA this transfer */
648 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700649
650 /* reject already-mapped transfers; PIO won't always work */
651 if (message->is_dma_mapped
652 || transfer->rx_dma || transfer->tx_dma) {
653 dev_err(&drv_data->pdev->dev,
654 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700655 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700656 transfer->len, MAX_DMA_LEN);
657 message->status = -EINVAL;
658 giveback(drv_data);
659 return;
660 }
661
662 /* warn ... we force this to PIO mode */
663 if (printk_ratelimit())
664 dev_warn(&message->spi->dev, "pump_transfers: "
665 "DMA disabled for transfer length %ld "
666 "greater than %d\n",
667 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800668 }
669
Stephen Streete0c99052006-03-07 23:53:24 -0800670 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200671 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800672 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
673 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700674 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800675 return;
676 }
Stephen Street9708c122006-03-28 14:05:23 -0800677 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800678 drv_data->tx = (void *)transfer->tx_buf;
679 drv_data->tx_end = drv_data->tx + transfer->len;
680 drv_data->rx = transfer->rx_buf;
681 drv_data->rx_end = drv_data->rx + transfer->len;
682 drv_data->rx_dma = transfer->rx_dma;
683 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200684 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800685 drv_data->write = drv_data->tx ? chip->write : null_writer;
686 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800687
688 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800689 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800690 if (transfer->speed_hz || transfer->bits_per_word) {
691
Stephen Street9708c122006-03-28 14:05:23 -0800692 bits = chip->bits_per_word;
693 speed = chip->speed_hz;
694
695 if (transfer->speed_hz)
696 speed = transfer->speed_hz;
697
698 if (transfer->bits_per_word)
699 bits = transfer->bits_per_word;
700
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200701 clk_div = ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800702
703 if (bits <= 8) {
704 drv_data->n_bytes = 1;
Stephen Street9708c122006-03-28 14:05:23 -0800705 drv_data->read = drv_data->read != null_reader ?
706 u8_reader : null_reader;
707 drv_data->write = drv_data->write != null_writer ?
708 u8_writer : null_writer;
709 } else if (bits <= 16) {
710 drv_data->n_bytes = 2;
Stephen Street9708c122006-03-28 14:05:23 -0800711 drv_data->read = drv_data->read != null_reader ?
712 u16_reader : null_reader;
713 drv_data->write = drv_data->write != null_writer ?
714 u16_writer : null_writer;
715 } else if (bits <= 32) {
716 drv_data->n_bytes = 4;
Stephen Street9708c122006-03-28 14:05:23 -0800717 drv_data->read = drv_data->read != null_reader ?
718 u32_reader : null_reader;
719 drv_data->write = drv_data->write != null_writer ?
720 u32_writer : null_writer;
721 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800722 /* if bits/word is changed in dma mode, then must check the
723 * thresholds and burst also */
724 if (chip->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200725 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
726 message->spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800727 bits, &dma_burst,
728 &dma_thresh))
729 if (printk_ratelimit())
730 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -0700731 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -0800732 "DMA burst size reduced to "
733 "match bits_per_word\n");
734 }
Stephen Street9708c122006-03-28 14:05:23 -0800735
736 cr0 = clk_div
737 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700738 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -0800739 | SSCR0_SSE
740 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -0800741 }
742
Stephen Streete0c99052006-03-07 23:53:24 -0800743 message->state = RUNNING_STATE;
744
Ned Forrester7e964452008-09-13 02:33:18 -0700745 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200746 if (pxa2xx_spi_dma_is_possible(drv_data->len))
747 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700748 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800749
750 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200751 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800752
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200753 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800754
Stephen Street8d94cc52006-12-10 02:18:54 -0800755 /* Clear status and start DMA engine */
756 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800757 write_SSSR(drv_data->clear_sr, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200758
759 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800760 } else {
761 /* Ensure we have the correct interrupt handler */
762 drv_data->transfer_handler = interrupt_transfer;
763
Stephen Street8d94cc52006-12-10 02:18:54 -0800764 /* Clear status */
765 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800766 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800767 }
768
Mika Westerberga0d26422013-01-22 12:26:32 +0200769 if (is_lpss_ssp(drv_data)) {
770 if ((read_SSIRF(reg) & 0xff) != chip->lpss_rx_threshold)
771 write_SSIRF(chip->lpss_rx_threshold, reg);
772 if ((read_SSITF(reg) & 0xffff) != chip->lpss_tx_threshold)
773 write_SSITF(chip->lpss_tx_threshold, reg);
774 }
775
Stephen Street8d94cc52006-12-10 02:18:54 -0800776 /* see if we need to reload the config registers */
777 if ((read_SSCR0(reg) != cr0)
778 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
779 (cr1 & SSCR1_CHANGE_MASK)) {
780
Ned Forresterb97c74b2008-02-23 15:23:40 -0800781 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -0800782 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800783 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -0800784 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800785 /* first set CR1 without interrupt and service enables */
786 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
787 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -0800788 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800789
Stephen Street8d94cc52006-12-10 02:18:54 -0800790 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800791 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800792 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800793 }
Ned Forresterb97c74b2008-02-23 15:23:40 -0800794
Eric Miaoa7bb3902009-04-06 19:00:54 -0700795 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800796
797 /* after chip select, release the data by enabling service
798 * requests and interrupts, without changing any mode bits */
799 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800800}
801
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200802static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
803 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -0800804{
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200805 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -0800806
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200807 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800808 /* Initial message state*/
809 drv_data->cur_msg->state = START_STATE;
810 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
811 struct spi_transfer,
812 transfer_list);
813
Stephen Street8d94cc52006-12-10 02:18:54 -0800814 /* prepare to setup the SSP, in pump_transfers, using the per
815 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -0800816 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -0800817
818 /* Mark as busy and launch transfers */
819 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -0800820 return 0;
821}
822
Mika Westerberg7d94a502013-01-22 12:26:30 +0200823static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
824{
825 struct driver_data *drv_data = spi_master_get_devdata(master);
826
827 /* Disable the SSP now */
828 write_SSCR0(read_SSCR0(drv_data->ioaddr) & ~SSCR0_SSE,
829 drv_data->ioaddr);
830
Mika Westerberg7d94a502013-01-22 12:26:30 +0200831 return 0;
832}
833
Eric Miaoa7bb3902009-04-06 19:00:54 -0700834static int setup_cs(struct spi_device *spi, struct chip_data *chip,
835 struct pxa2xx_spi_chip *chip_info)
836{
837 int err = 0;
838
839 if (chip == NULL || chip_info == NULL)
840 return 0;
841
842 /* NOTE: setup() can be called multiple times, possibly with
843 * different chip_info, release previously requested GPIO
844 */
845 if (gpio_is_valid(chip->gpio_cs))
846 gpio_free(chip->gpio_cs);
847
848 /* If (*cs_control) is provided, ignore GPIO chip select */
849 if (chip_info->cs_control) {
850 chip->cs_control = chip_info->cs_control;
851 return 0;
852 }
853
854 if (gpio_is_valid(chip_info->gpio_cs)) {
855 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
856 if (err) {
857 dev_err(&spi->dev, "failed to request chip select "
858 "GPIO%d\n", chip_info->gpio_cs);
859 return err;
860 }
861
862 chip->gpio_cs = chip_info->gpio_cs;
863 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
864
865 err = gpio_direction_output(chip->gpio_cs,
866 !chip->gpio_cs_inverted);
867 }
868
869 return err;
870}
871
Stephen Streete0c99052006-03-07 23:53:24 -0800872static int setup(struct spi_device *spi)
873{
874 struct pxa2xx_spi_chip *chip_info = NULL;
875 struct chip_data *chip;
876 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
877 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +0200878 uint tx_thres, tx_hi_thres, rx_thres;
879
880 if (is_lpss_ssp(drv_data)) {
881 tx_thres = LPSS_TX_LOTHRESH_DFLT;
882 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
883 rx_thres = LPSS_RX_THRESH_DFLT;
884 } else {
885 tx_thres = TX_THRESH_DFLT;
886 tx_hi_thres = 0;
887 rx_thres = RX_THRESH_DFLT;
888 }
Stephen Streete0c99052006-03-07 23:53:24 -0800889
Stephen Street8d94cc52006-12-10 02:18:54 -0800890 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -0800891 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -0800892 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -0800893 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -0800894 if (!chip) {
895 dev_err(&spi->dev,
896 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -0800897 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -0800898 }
Stephen Streete0c99052006-03-07 23:53:24 -0800899
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800900 if (drv_data->ssp_type == CE4100_SSP) {
901 if (spi->chip_select > 4) {
902 dev_err(&spi->dev, "failed setup: "
903 "cs number must not be > 4.\n");
904 kfree(chip);
905 return -EINVAL;
906 }
907
908 chip->frm = spi->chip_select;
909 } else
910 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -0800911 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700912 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -0800913 }
914
Stephen Street8d94cc52006-12-10 02:18:54 -0800915 /* protocol drivers may change the chip settings, so...
916 * if chip_info exists, use it */
917 chip_info = spi->controller_data;
918
Stephen Streete0c99052006-03-07 23:53:24 -0800919 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -0800920 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800921 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700922 if (chip_info->timeout)
923 chip->timeout = chip_info->timeout;
924 if (chip_info->tx_threshold)
925 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +0200926 if (chip_info->tx_hi_threshold)
927 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700928 if (chip_info->rx_threshold)
929 rx_thres = chip_info->rx_threshold;
930 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800931 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800932 if (chip_info->enable_loopback)
933 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +0200934 } else if (ACPI_HANDLE(&spi->dev)) {
935 /*
936 * Slave devices enumerated from ACPI namespace don't
937 * usually have chip_info but we still might want to use
938 * DMA with them.
939 */
940 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800941 }
942
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700943 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
944 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
945
Mika Westerberga0d26422013-01-22 12:26:32 +0200946 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
947 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
948 | SSITF_TxHiThresh(tx_hi_thres);
949
Stephen Street8d94cc52006-12-10 02:18:54 -0800950 /* set dma burst and threshold outside of chip_info path so that if
951 * chip_info goes away after setting chip->enable_dma, the
952 * burst and threshold can still respond to changes in bits_per_word */
953 if (chip->enable_dma) {
954 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200955 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
956 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -0800957 &chip->dma_burst_size,
958 &chip->dma_threshold)) {
959 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
960 "to match bits_per_word\n");
961 }
962 }
963
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200964 clk_div = ssp_get_clk_div(drv_data, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -0800965 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -0800966
967 chip->cr0 = clk_div
968 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700969 | SSCR0_DataSize(spi->bits_per_word > 16 ?
970 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -0800971 | SSCR0_SSE
972 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -0800973 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
974 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
975 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -0800976
Mika Westerbergb8331722013-01-22 12:26:31 +0200977 if (spi->mode & SPI_LOOP)
978 chip->cr1 |= SSCR1_LBM;
979
Stephen Streete0c99052006-03-07 23:53:24 -0800980 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800981 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -0700982 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200983 drv_data->max_clk_rate
Eric Miaoc9840da2010-03-16 16:48:01 +0800984 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
985 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800986 else
David Brownell7d077192009-06-17 16:26:03 -0700987 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200988 drv_data->max_clk_rate / 2
Eric Miaoc9840da2010-03-16 16:48:01 +0800989 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
990 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800991
992 if (spi->bits_per_word <= 8) {
993 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800994 chip->read = u8_reader;
995 chip->write = u8_writer;
996 } else if (spi->bits_per_word <= 16) {
997 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -0800998 chip->read = u16_reader;
999 chip->write = u16_writer;
1000 } else if (spi->bits_per_word <= 32) {
1001 chip->cr0 |= SSCR0_EDSS;
1002 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001003 chip->read = u32_reader;
1004 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001005 }
Stephen Street9708c122006-03-28 14:05:23 -08001006 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001007
1008 spi_set_ctldata(spi, chip);
1009
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001010 if (drv_data->ssp_type == CE4100_SSP)
1011 return 0;
1012
Eric Miaoa7bb3902009-04-06 19:00:54 -07001013 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001014}
1015
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001016static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001017{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001018 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001019 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001020
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001021 if (!chip)
1022 return;
1023
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001024 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001025 gpio_free(chip->gpio_cs);
1026
Stephen Streete0c99052006-03-07 23:53:24 -08001027 kfree(chip);
1028}
1029
Mika Westerberga3496852013-01-22 12:26:33 +02001030#ifdef CONFIG_ACPI
Mika Westerberga3496852013-01-22 12:26:33 +02001031static struct pxa2xx_spi_master *
1032pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1033{
1034 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001035 struct acpi_device *adev;
1036 struct ssp_device *ssp;
1037 struct resource *res;
1038 int devid;
1039
1040 if (!ACPI_HANDLE(&pdev->dev) ||
1041 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1042 return NULL;
1043
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001044 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Mika Westerberga3496852013-01-22 12:26:33 +02001045 if (!pdata) {
1046 dev_err(&pdev->dev,
1047 "failed to allocate memory for platform data\n");
1048 return NULL;
1049 }
1050
1051 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1052 if (!res)
1053 return NULL;
1054
1055 ssp = &pdata->ssp;
1056
1057 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301058 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1059 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001060 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001061
1062 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1063 ssp->irq = platform_get_irq(pdev, 0);
1064 ssp->type = LPSS_SSP;
1065 ssp->pdev = pdev;
1066
1067 ssp->port_id = -1;
1068 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1069 ssp->port_id = devid;
1070
1071 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001072 pdata->enable_dma = true;
Mika Westerberga3496852013-01-22 12:26:33 +02001073
1074 return pdata;
1075}
1076
1077static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1078 { "INT33C0", 0 },
1079 { "INT33C1", 0 },
Mika Westerberg4b30f2a2013-05-13 13:45:11 +03001080 { "80860F0E", 0 },
Mika Westerberga3496852013-01-22 12:26:33 +02001081 { },
1082};
1083MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1084#else
1085static inline struct pxa2xx_spi_master *
1086pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1087{
1088 return NULL;
1089}
1090#endif
1091
Grant Likelyfd4a3192012-12-07 16:57:14 +00001092static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001093{
1094 struct device *dev = &pdev->dev;
1095 struct pxa2xx_spi_master *platform_info;
1096 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001097 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001098 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001099 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001100
Mika Westerberg851bacf2013-01-07 12:44:33 +02001101 platform_info = dev_get_platdata(dev);
1102 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001103 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1104 if (!platform_info) {
1105 dev_err(&pdev->dev, "missing platform data\n");
1106 return -ENODEV;
1107 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001108 }
Stephen Streete0c99052006-03-07 23:53:24 -08001109
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001110 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001111 if (!ssp)
1112 ssp = &platform_info->ssp;
1113
1114 if (!ssp->mmio_base) {
1115 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001116 return -ENODEV;
1117 }
1118
1119 /* Allocate master with space for drv_data and null dma buffer */
1120 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1121 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001122 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001123 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001124 return -ENOMEM;
1125 }
1126 drv_data = spi_master_get_devdata(master);
1127 drv_data->master = master;
1128 drv_data->master_info = platform_info;
1129 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001130 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001131
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001132 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001133 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001134 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001135 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001136
Mika Westerberg851bacf2013-01-07 12:44:33 +02001137 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001138 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001139 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001140 master->cleanup = cleanup;
1141 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001142 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001143 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001144 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001145
eric miao2f1a74e2007-11-21 18:50:53 +08001146 drv_data->ssp_type = ssp->type;
Mika Westerberg2b9b84f2013-01-22 12:26:25 +02001147 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
Stephen Streete0c99052006-03-07 23:53:24 -08001148
eric miao2f1a74e2007-11-21 18:50:53 +08001149 drv_data->ioaddr = ssp->mmio_base;
1150 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001151 if (pxa25x_ssp_comp(drv_data)) {
Stephen Warren24778be2013-05-21 20:36:35 -06001152 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Stephen Streete0c99052006-03-07 23:53:24 -08001153 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1154 drv_data->dma_cr1 = 0;
1155 drv_data->clear_sr = SSSR_ROR;
1156 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1157 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001158 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001159 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001160 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001161 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1162 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1163 }
1164
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001165 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1166 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001167 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001168 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001169 goto out_error_master_alloc;
1170 }
1171
1172 /* Setup DMA if requested */
1173 drv_data->tx_channel = -1;
1174 drv_data->rx_channel = -1;
1175 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001176 status = pxa2xx_spi_dma_setup(drv_data);
1177 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001178 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001179 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001180 }
Stephen Streete0c99052006-03-07 23:53:24 -08001181 }
1182
1183 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001184 clk_prepare_enable(ssp->clk);
1185
1186 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001187
1188 /* Load default SSP configuration */
1189 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001190 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1191 SSCR1_TxTresh(TX_THRESH_DFLT),
1192 drv_data->ioaddr);
Eric Miaoc9840da2010-03-16 16:48:01 +08001193 write_SSCR0(SSCR0_SCR(2)
Stephen Streete0c99052006-03-07 23:53:24 -08001194 | SSCR0_Motorola
1195 | SSCR0_DataSize(8),
1196 drv_data->ioaddr);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001197 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -08001198 write_SSTO(0, drv_data->ioaddr);
1199 write_SSPSP(0, drv_data->ioaddr);
1200
Mika Westerberga0d26422013-01-22 12:26:32 +02001201 lpss_ssp_setup(drv_data);
1202
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001203 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1204 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001205
1206 /* Register with the SPI framework */
1207 platform_set_drvdata(pdev, drv_data);
1208 status = spi_register_master(master);
1209 if (status != 0) {
1210 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001211 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001212 }
1213
Mika Westerberg7d94a502013-01-22 12:26:30 +02001214 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1215 pm_runtime_use_autosuspend(&pdev->dev);
1216 pm_runtime_set_active(&pdev->dev);
1217 pm_runtime_enable(&pdev->dev);
1218
Stephen Streete0c99052006-03-07 23:53:24 -08001219 return status;
1220
Stephen Streete0c99052006-03-07 23:53:24 -08001221out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001222 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001223 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001224 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001225
1226out_error_master_alloc:
1227 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001228 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001229 return status;
1230}
1231
1232static int pxa2xx_spi_remove(struct platform_device *pdev)
1233{
1234 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001235 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001236
1237 if (!drv_data)
1238 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001239 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001240
Mika Westerberg7d94a502013-01-22 12:26:30 +02001241 pm_runtime_get_sync(&pdev->dev);
1242
Stephen Streete0c99052006-03-07 23:53:24 -08001243 /* Disable the SSP at the peripheral and SOC level */
1244 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001245 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001246
1247 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001248 if (drv_data->master_info->enable_dma)
1249 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001250
Mika Westerberg7d94a502013-01-22 12:26:30 +02001251 pm_runtime_put_noidle(&pdev->dev);
1252 pm_runtime_disable(&pdev->dev);
1253
Stephen Streete0c99052006-03-07 23:53:24 -08001254 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001255 free_irq(ssp->irq, drv_data);
1256
1257 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001258 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001259
1260 /* Disconnect from the SPI framework */
1261 spi_unregister_master(drv_data->master);
1262
Stephen Streete0c99052006-03-07 23:53:24 -08001263 return 0;
1264}
1265
1266static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1267{
1268 int status = 0;
1269
1270 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1271 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1272}
1273
1274#ifdef CONFIG_PM
Mike Rapoport86d25932009-07-21 17:50:16 +03001275static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001276{
Mike Rapoport86d25932009-07-21 17:50:16 +03001277 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001278 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001279 int status = 0;
1280
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001281 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001282 if (status != 0)
1283 return status;
1284 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001285 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001286
1287 return 0;
1288}
1289
Mike Rapoport86d25932009-07-21 17:50:16 +03001290static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001291{
Mike Rapoport86d25932009-07-21 17:50:16 +03001292 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001293 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001294 int status = 0;
1295
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001296 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001297
Stephen Streete0c99052006-03-07 23:53:24 -08001298 /* Enable the SSP clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001299 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001300
1301 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001302 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001303 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001304 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001305 return status;
1306 }
1307
1308 return 0;
1309}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001310#endif
1311
1312#ifdef CONFIG_PM_RUNTIME
1313static int pxa2xx_spi_runtime_suspend(struct device *dev)
1314{
1315 struct driver_data *drv_data = dev_get_drvdata(dev);
1316
1317 clk_disable_unprepare(drv_data->ssp->clk);
1318 return 0;
1319}
1320
1321static int pxa2xx_spi_runtime_resume(struct device *dev)
1322{
1323 struct driver_data *drv_data = dev_get_drvdata(dev);
1324
1325 clk_prepare_enable(drv_data->ssp->clk);
1326 return 0;
1327}
1328#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001329
Alexey Dobriyan47145212009-12-14 18:00:08 -08001330static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001331 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1332 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1333 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001334};
Stephen Streete0c99052006-03-07 23:53:24 -08001335
1336static struct platform_driver driver = {
1337 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001338 .name = "pxa2xx-spi",
1339 .owner = THIS_MODULE,
Mike Rapoport86d25932009-07-21 17:50:16 +03001340 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001341 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001342 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001343 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001344 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001345 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001346};
1347
1348static int __init pxa2xx_spi_init(void)
1349{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001350 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001351}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001352subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001353
1354static void __exit pxa2xx_spi_exit(void)
1355{
1356 platform_driver_unregister(&driver);
1357}
1358module_exit(pxa2xx_spi_exit);