blob: 41185d0557fa5575531d6da9318b7c135198a681 [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
Axel Lin23e2c2a2014-02-12 22:13:27 +0800365 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
Stephen Streete0c99052006-03-07 23:53:24 -0800366 transfer_list);
367
Ned Forrester84235972008-09-13 02:33:17 -0700368 /* Delay if requested before any change in chip select */
369 if (last_transfer->delay_usecs)
370 udelay(last_transfer->delay_usecs);
371
372 /* Drop chip select UNLESS cs_change is true or we are returning
373 * a message with an error, or next message is for another chip
374 */
Stephen Streete0c99052006-03-07 23:53:24 -0800375 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700376 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700377 else {
378 struct spi_message *next_msg;
379
380 /* Holding of cs was hinted, but we need to make sure
381 * the next message is for the same chip. Don't waste
382 * time with the following tests unless this was hinted.
383 *
384 * We cannot postpone this until pump_messages, because
385 * after calling msg->complete (below) the driver that
386 * sent the current message could be unloaded, which
387 * could invalidate the cs_control() callback...
388 */
389
390 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200391 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700392
393 /* see if the next and current messages point
394 * to the same chip
395 */
396 if (next_msg && next_msg->spi != msg->spi)
397 next_msg = NULL;
398 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700399 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700400 }
Stephen Streete0c99052006-03-07 23:53:24 -0800401
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200402 spi_finalize_current_message(drv_data->master);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700403 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800404}
405
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800406static void reset_sccr1(struct driver_data *drv_data)
407{
408 void __iomem *reg = drv_data->ioaddr;
409 struct chip_data *chip = drv_data->cur_chip;
410 u32 sccr1_reg;
411
412 sccr1_reg = read_SSCR1(reg) & ~drv_data->int_cr1;
413 sccr1_reg &= ~SSCR1_RFT;
414 sccr1_reg |= chip->threshold;
415 write_SSCR1(sccr1_reg, reg);
416}
417
Stephen Street8d94cc52006-12-10 02:18:54 -0800418static void int_error_stop(struct driver_data *drv_data, const char* msg)
419{
David Brownellcf433692008-04-28 02:14:17 -0700420 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800421
422 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800423 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800424 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800425 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800426 write_SSTO(0, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200427 pxa2xx_spi_flush(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800428 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
429
430 dev_err(&drv_data->pdev->dev, "%s\n", msg);
431
432 drv_data->cur_msg->state = ERROR_STATE;
433 tasklet_schedule(&drv_data->pump_transfers);
434}
435
436static void int_transfer_complete(struct driver_data *drv_data)
437{
David Brownellcf433692008-04-28 02:14:17 -0700438 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800439
440 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800441 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800442 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800443 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800444 write_SSTO(0, reg);
445
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300446 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800447 drv_data->cur_msg->actual_length += drv_data->len -
448 (drv_data->rx_end - drv_data->rx);
449
Ned Forrester84235972008-09-13 02:33:17 -0700450 /* Transfer delays and chip select release are
451 * handled in pump_transfers or giveback
452 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800453
454 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200455 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800456
457 /* Schedule transfer tasklet */
458 tasklet_schedule(&drv_data->pump_transfers);
459}
460
Stephen Streete0c99052006-03-07 23:53:24 -0800461static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
462{
David Brownellcf433692008-04-28 02:14:17 -0700463 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800464
Stephen Street5daa3ba2006-05-20 15:00:19 -0700465 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
466 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800467
Stephen Street8d94cc52006-12-10 02:18:54 -0800468 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800469
Stephen Street8d94cc52006-12-10 02:18:54 -0800470 if (irq_status & SSSR_ROR) {
471 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
472 return IRQ_HANDLED;
473 }
Stephen Streete0c99052006-03-07 23:53:24 -0800474
Stephen Street8d94cc52006-12-10 02:18:54 -0800475 if (irq_status & SSSR_TINT) {
476 write_SSSR(SSSR_TINT, reg);
477 if (drv_data->read(drv_data)) {
478 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800479 return IRQ_HANDLED;
480 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800481 }
Stephen Streete0c99052006-03-07 23:53:24 -0800482
Stephen Street8d94cc52006-12-10 02:18:54 -0800483 /* Drain rx fifo, Fill tx fifo and prevent overruns */
484 do {
485 if (drv_data->read(drv_data)) {
486 int_transfer_complete(drv_data);
487 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800488 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800489 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800490
Stephen Street8d94cc52006-12-10 02:18:54 -0800491 if (drv_data->read(drv_data)) {
492 int_transfer_complete(drv_data);
493 return IRQ_HANDLED;
494 }
Stephen Streete0c99052006-03-07 23:53:24 -0800495
Stephen Street8d94cc52006-12-10 02:18:54 -0800496 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800497 u32 bytes_left;
498 u32 sccr1_reg;
499
500 sccr1_reg = read_SSCR1(reg);
501 sccr1_reg &= ~SSCR1_TIE;
502
503 /*
504 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300505 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800506 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800507 if (pxa25x_ssp_comp(drv_data)) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800508
509 sccr1_reg &= ~SSCR1_RFT;
510
511 bytes_left = drv_data->rx_end - drv_data->rx;
512 switch (drv_data->n_bytes) {
513 case 4:
514 bytes_left >>= 1;
515 case 2:
516 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800517 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800518
519 if (bytes_left > RX_THRESH_DFLT)
520 bytes_left = RX_THRESH_DFLT;
521
522 sccr1_reg |= SSCR1_RxTresh(bytes_left);
Stephen Streete0c99052006-03-07 23:53:24 -0800523 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800524 write_SSCR1(sccr1_reg, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800525 }
526
Stephen Street5daa3ba2006-05-20 15:00:19 -0700527 /* We did something */
528 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800529}
530
David Howells7d12e782006-10-05 14:55:46 +0100531static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800532{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400533 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700534 void __iomem *reg = drv_data->ioaddr;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200535 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800536 u32 mask = drv_data->mask_sr;
537 u32 status;
538
Mika Westerberg7d94a502013-01-22 12:26:30 +0200539 /*
540 * The IRQ might be shared with other peripherals so we must first
541 * check that are we RPM suspended or not. If we are we assume that
542 * the IRQ was not for us (we shouldn't be RPM suspended when the
543 * interrupt is enabled).
544 */
545 if (pm_runtime_suspended(&drv_data->pdev->dev))
546 return IRQ_NONE;
547
Mika Westerberg269e4a42013-09-04 13:37:43 +0300548 /*
549 * If the device is not yet in RPM suspended state and we get an
550 * interrupt that is meant for another device, check if status bits
551 * are all set to one. That means that the device is already
552 * powered off.
553 */
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800554 status = read_SSSR(reg);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300555 if (status == ~0)
556 return IRQ_NONE;
557
558 sccr1_reg = read_SSCR1(reg);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800559
560 /* Ignore possible writes if we don't need to write */
561 if (!(sccr1_reg & SSCR1_TIE))
562 mask &= ~SSSR_TFS;
563
564 if (!(status & mask))
565 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800566
567 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700568
569 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
570 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800571 if (!pxa25x_ssp_comp(drv_data))
Stephen Street5daa3ba2006-05-20 15:00:19 -0700572 write_SSTO(0, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800573 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700574
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300575 dev_err(&drv_data->pdev->dev,
576 "bad message state in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700577
Stephen Streete0c99052006-03-07 23:53:24 -0800578 /* Never fail */
579 return IRQ_HANDLED;
580 }
581
582 return drv_data->transfer_handler(drv_data);
583}
584
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200585static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800586{
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200587 unsigned long ssp_clk = drv_data->max_clk_rate;
588 const struct ssp_device *ssp = drv_data->ssp;
589
590 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800591
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800592 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
eric miao2f1a74e2007-11-21 18:50:53 +0800593 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
594 else
595 return ((ssp_clk / rate - 1) & 0xfff) << 8;
596}
597
Stephen Streete0c99052006-03-07 23:53:24 -0800598static void pump_transfers(unsigned long data)
599{
600 struct driver_data *drv_data = (struct driver_data *)data;
601 struct spi_message *message = NULL;
602 struct spi_transfer *transfer = NULL;
603 struct spi_transfer *previous = NULL;
604 struct chip_data *chip = NULL;
David Brownellcf433692008-04-28 02:14:17 -0700605 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800606 u32 clk_div = 0;
607 u8 bits = 0;
608 u32 speed = 0;
609 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800610 u32 cr1;
611 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
612 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800613
614 /* Get current state information */
615 message = drv_data->cur_msg;
616 transfer = drv_data->cur_transfer;
617 chip = drv_data->cur_chip;
618
619 /* Handle for abort */
620 if (message->state == ERROR_STATE) {
621 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700622 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800623 return;
624 }
625
626 /* Handle end of message */
627 if (message->state == DONE_STATE) {
628 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700629 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800630 return;
631 }
632
Ned Forrester84235972008-09-13 02:33:17 -0700633 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800634 if (message->state == RUNNING_STATE) {
635 previous = list_entry(transfer->transfer_list.prev,
636 struct spi_transfer,
637 transfer_list);
638 if (previous->delay_usecs)
639 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700640
641 /* Drop chip select only if cs_change is requested */
642 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700643 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800644 }
645
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200646 /* Check if we can DMA this transfer */
647 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700648
649 /* reject already-mapped transfers; PIO won't always work */
650 if (message->is_dma_mapped
651 || transfer->rx_dma || transfer->tx_dma) {
652 dev_err(&drv_data->pdev->dev,
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300653 "pump_transfers: mapped transfer length of "
654 "%u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700655 transfer->len, MAX_DMA_LEN);
656 message->status = -EINVAL;
657 giveback(drv_data);
658 return;
659 }
660
661 /* warn ... we force this to PIO mode */
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300662 dev_warn_ratelimited(&message->spi->dev,
663 "pump_transfers: DMA disabled for transfer length %ld "
664 "greater than %d\n",
665 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800666 }
667
Stephen Streete0c99052006-03-07 23:53:24 -0800668 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200669 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800670 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
671 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700672 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800673 return;
674 }
Stephen Street9708c122006-03-28 14:05:23 -0800675 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800676 drv_data->tx = (void *)transfer->tx_buf;
677 drv_data->tx_end = drv_data->tx + transfer->len;
678 drv_data->rx = transfer->rx_buf;
679 drv_data->rx_end = drv_data->rx + transfer->len;
680 drv_data->rx_dma = transfer->rx_dma;
681 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200682 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800683 drv_data->write = drv_data->tx ? chip->write : null_writer;
684 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800685
686 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800687 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800688 if (transfer->speed_hz || transfer->bits_per_word) {
689
Stephen Street9708c122006-03-28 14:05:23 -0800690 bits = chip->bits_per_word;
691 speed = chip->speed_hz;
692
693 if (transfer->speed_hz)
694 speed = transfer->speed_hz;
695
696 if (transfer->bits_per_word)
697 bits = transfer->bits_per_word;
698
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200699 clk_div = ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800700
701 if (bits <= 8) {
702 drv_data->n_bytes = 1;
Stephen Street9708c122006-03-28 14:05:23 -0800703 drv_data->read = drv_data->read != null_reader ?
704 u8_reader : null_reader;
705 drv_data->write = drv_data->write != null_writer ?
706 u8_writer : null_writer;
707 } else if (bits <= 16) {
708 drv_data->n_bytes = 2;
Stephen Street9708c122006-03-28 14:05:23 -0800709 drv_data->read = drv_data->read != null_reader ?
710 u16_reader : null_reader;
711 drv_data->write = drv_data->write != null_writer ?
712 u16_writer : null_writer;
713 } else if (bits <= 32) {
714 drv_data->n_bytes = 4;
Stephen Street9708c122006-03-28 14:05:23 -0800715 drv_data->read = drv_data->read != null_reader ?
716 u32_reader : null_reader;
717 drv_data->write = drv_data->write != null_writer ?
718 u32_writer : null_writer;
719 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800720 /* if bits/word is changed in dma mode, then must check the
721 * thresholds and burst also */
722 if (chip->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200723 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
724 message->spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800725 bits, &dma_burst,
726 &dma_thresh))
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300727 dev_warn_ratelimited(&message->spi->dev,
728 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -0800729 }
Stephen Street9708c122006-03-28 14:05:23 -0800730
731 cr0 = clk_div
732 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700733 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -0800734 | SSCR0_SSE
735 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -0800736 }
737
Stephen Streete0c99052006-03-07 23:53:24 -0800738 message->state = RUNNING_STATE;
739
Ned Forrester7e964452008-09-13 02:33:18 -0700740 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200741 if (pxa2xx_spi_dma_is_possible(drv_data->len))
742 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700743 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800744
745 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200746 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800747
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200748 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800749
Stephen Street8d94cc52006-12-10 02:18:54 -0800750 /* Clear status and start DMA engine */
751 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800752 write_SSSR(drv_data->clear_sr, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200753
754 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800755 } else {
756 /* Ensure we have the correct interrupt handler */
757 drv_data->transfer_handler = interrupt_transfer;
758
Stephen Street8d94cc52006-12-10 02:18:54 -0800759 /* Clear status */
760 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800761 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800762 }
763
Mika Westerberga0d26422013-01-22 12:26:32 +0200764 if (is_lpss_ssp(drv_data)) {
765 if ((read_SSIRF(reg) & 0xff) != chip->lpss_rx_threshold)
766 write_SSIRF(chip->lpss_rx_threshold, reg);
767 if ((read_SSITF(reg) & 0xffff) != chip->lpss_tx_threshold)
768 write_SSITF(chip->lpss_tx_threshold, reg);
769 }
770
Stephen Street8d94cc52006-12-10 02:18:54 -0800771 /* see if we need to reload the config registers */
772 if ((read_SSCR0(reg) != cr0)
773 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
774 (cr1 & SSCR1_CHANGE_MASK)) {
775
Ned Forresterb97c74b2008-02-23 15:23:40 -0800776 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -0800777 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800778 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -0800779 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800780 /* first set CR1 without interrupt and service enables */
781 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
782 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -0800783 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800784
Stephen Street8d94cc52006-12-10 02:18:54 -0800785 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800786 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800787 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800788 }
Ned Forresterb97c74b2008-02-23 15:23:40 -0800789
Eric Miaoa7bb3902009-04-06 19:00:54 -0700790 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800791
792 /* after chip select, release the data by enabling service
793 * requests and interrupts, without changing any mode bits */
794 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800795}
796
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200797static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
798 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -0800799{
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200800 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -0800801
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200802 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800803 /* Initial message state*/
804 drv_data->cur_msg->state = START_STATE;
805 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
806 struct spi_transfer,
807 transfer_list);
808
Stephen Street8d94cc52006-12-10 02:18:54 -0800809 /* prepare to setup the SSP, in pump_transfers, using the per
810 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -0800811 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -0800812
813 /* Mark as busy and launch transfers */
814 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -0800815 return 0;
816}
817
Mika Westerberg7d94a502013-01-22 12:26:30 +0200818static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
819{
820 struct driver_data *drv_data = spi_master_get_devdata(master);
821
822 /* Disable the SSP now */
823 write_SSCR0(read_SSCR0(drv_data->ioaddr) & ~SSCR0_SSE,
824 drv_data->ioaddr);
825
Mika Westerberg7d94a502013-01-22 12:26:30 +0200826 return 0;
827}
828
Eric Miaoa7bb3902009-04-06 19:00:54 -0700829static int setup_cs(struct spi_device *spi, struct chip_data *chip,
830 struct pxa2xx_spi_chip *chip_info)
831{
832 int err = 0;
833
834 if (chip == NULL || chip_info == NULL)
835 return 0;
836
837 /* NOTE: setup() can be called multiple times, possibly with
838 * different chip_info, release previously requested GPIO
839 */
840 if (gpio_is_valid(chip->gpio_cs))
841 gpio_free(chip->gpio_cs);
842
843 /* If (*cs_control) is provided, ignore GPIO chip select */
844 if (chip_info->cs_control) {
845 chip->cs_control = chip_info->cs_control;
846 return 0;
847 }
848
849 if (gpio_is_valid(chip_info->gpio_cs)) {
850 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
851 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300852 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
853 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700854 return err;
855 }
856
857 chip->gpio_cs = chip_info->gpio_cs;
858 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
859
860 err = gpio_direction_output(chip->gpio_cs,
861 !chip->gpio_cs_inverted);
862 }
863
864 return err;
865}
866
Stephen Streete0c99052006-03-07 23:53:24 -0800867static int setup(struct spi_device *spi)
868{
869 struct pxa2xx_spi_chip *chip_info = NULL;
870 struct chip_data *chip;
871 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
872 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +0200873 uint tx_thres, tx_hi_thres, rx_thres;
874
875 if (is_lpss_ssp(drv_data)) {
876 tx_thres = LPSS_TX_LOTHRESH_DFLT;
877 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
878 rx_thres = LPSS_RX_THRESH_DFLT;
879 } else {
880 tx_thres = TX_THRESH_DFLT;
881 tx_hi_thres = 0;
882 rx_thres = RX_THRESH_DFLT;
883 }
Stephen Streete0c99052006-03-07 23:53:24 -0800884
Stephen Street8d94cc52006-12-10 02:18:54 -0800885 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -0800886 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -0800887 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -0800888 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -0800889 if (!chip) {
890 dev_err(&spi->dev,
891 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -0800892 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -0800893 }
Stephen Streete0c99052006-03-07 23:53:24 -0800894
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800895 if (drv_data->ssp_type == CE4100_SSP) {
896 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300897 dev_err(&spi->dev,
898 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800899 kfree(chip);
900 return -EINVAL;
901 }
902
903 chip->frm = spi->chip_select;
904 } else
905 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -0800906 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700907 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -0800908 }
909
Stephen Street8d94cc52006-12-10 02:18:54 -0800910 /* protocol drivers may change the chip settings, so...
911 * if chip_info exists, use it */
912 chip_info = spi->controller_data;
913
Stephen Streete0c99052006-03-07 23:53:24 -0800914 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -0800915 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800916 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700917 if (chip_info->timeout)
918 chip->timeout = chip_info->timeout;
919 if (chip_info->tx_threshold)
920 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +0200921 if (chip_info->tx_hi_threshold)
922 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700923 if (chip_info->rx_threshold)
924 rx_thres = chip_info->rx_threshold;
925 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800926 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800927 if (chip_info->enable_loopback)
928 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +0200929 } else if (ACPI_HANDLE(&spi->dev)) {
930 /*
931 * Slave devices enumerated from ACPI namespace don't
932 * usually have chip_info but we still might want to use
933 * DMA with them.
934 */
935 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800936 }
937
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700938 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
939 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
940
Mika Westerberga0d26422013-01-22 12:26:32 +0200941 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
942 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
943 | SSITF_TxHiThresh(tx_hi_thres);
944
Stephen Street8d94cc52006-12-10 02:18:54 -0800945 /* set dma burst and threshold outside of chip_info path so that if
946 * chip_info goes away after setting chip->enable_dma, the
947 * burst and threshold can still respond to changes in bits_per_word */
948 if (chip->enable_dma) {
949 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200950 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
951 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -0800952 &chip->dma_burst_size,
953 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300954 dev_warn(&spi->dev,
955 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -0800956 }
957 }
958
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200959 clk_div = ssp_get_clk_div(drv_data, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -0800960 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -0800961
962 chip->cr0 = clk_div
963 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700964 | SSCR0_DataSize(spi->bits_per_word > 16 ?
965 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -0800966 | SSCR0_SSE
967 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -0800968 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
969 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
970 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -0800971
Mika Westerbergb8331722013-01-22 12:26:31 +0200972 if (spi->mode & SPI_LOOP)
973 chip->cr1 |= SSCR1_LBM;
974
Stephen Streete0c99052006-03-07 23:53:24 -0800975 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800976 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -0700977 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200978 drv_data->max_clk_rate
Eric Miaoc9840da2010-03-16 16:48:01 +0800979 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
980 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800981 else
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 / 2
Eric Miaoc9840da2010-03-16 16:48:01 +0800984 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
985 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800986
987 if (spi->bits_per_word <= 8) {
988 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800989 chip->read = u8_reader;
990 chip->write = u8_writer;
991 } else if (spi->bits_per_word <= 16) {
992 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -0800993 chip->read = u16_reader;
994 chip->write = u16_writer;
995 } else if (spi->bits_per_word <= 32) {
996 chip->cr0 |= SSCR0_EDSS;
997 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -0800998 chip->read = u32_reader;
999 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001000 }
Stephen Street9708c122006-03-28 14:05:23 -08001001 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001002
1003 spi_set_ctldata(spi, chip);
1004
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001005 if (drv_data->ssp_type == CE4100_SSP)
1006 return 0;
1007
Eric Miaoa7bb3902009-04-06 19:00:54 -07001008 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001009}
1010
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001011static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001012{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001013 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001014 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001015
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001016 if (!chip)
1017 return;
1018
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001019 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001020 gpio_free(chip->gpio_cs);
1021
Stephen Streete0c99052006-03-07 23:53:24 -08001022 kfree(chip);
1023}
1024
Mika Westerberga3496852013-01-22 12:26:33 +02001025#ifdef CONFIG_ACPI
Mika Westerberga3496852013-01-22 12:26:33 +02001026static struct pxa2xx_spi_master *
1027pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1028{
1029 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001030 struct acpi_device *adev;
1031 struct ssp_device *ssp;
1032 struct resource *res;
1033 int devid;
1034
1035 if (!ACPI_HANDLE(&pdev->dev) ||
1036 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1037 return NULL;
1038
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001039 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Mika Westerberga3496852013-01-22 12:26:33 +02001040 if (!pdata) {
1041 dev_err(&pdev->dev,
1042 "failed to allocate memory for platform data\n");
1043 return NULL;
1044 }
1045
1046 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1047 if (!res)
1048 return NULL;
1049
1050 ssp = &pdata->ssp;
1051
1052 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301053 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1054 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001055 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001056
1057 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1058 ssp->irq = platform_get_irq(pdev, 0);
1059 ssp->type = LPSS_SSP;
1060 ssp->pdev = pdev;
1061
1062 ssp->port_id = -1;
1063 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1064 ssp->port_id = devid;
1065
1066 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001067 pdata->enable_dma = true;
Mika Westerberg483c3192014-01-13 11:17:04 +02001068 pdata->tx_chan_id = -1;
1069 pdata->rx_chan_id = -1;
Mika Westerberga3496852013-01-22 12:26:33 +02001070
1071 return pdata;
1072}
1073
1074static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1075 { "INT33C0", 0 },
1076 { "INT33C1", 0 },
Mika Westerberg54acbd92013-11-12 12:06:21 +02001077 { "INT3430", 0 },
1078 { "INT3431", 0 },
Mika Westerberg4b30f2a2013-05-13 13:45:11 +03001079 { "80860F0E", 0 },
Mika Westerberga3496852013-01-22 12:26:33 +02001080 { },
1081};
1082MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1083#else
1084static inline struct pxa2xx_spi_master *
1085pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1086{
1087 return NULL;
1088}
1089#endif
1090
Grant Likelyfd4a3192012-12-07 16:57:14 +00001091static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001092{
1093 struct device *dev = &pdev->dev;
1094 struct pxa2xx_spi_master *platform_info;
1095 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001096 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001097 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001098 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001099
Mika Westerberg851bacf2013-01-07 12:44:33 +02001100 platform_info = dev_get_platdata(dev);
1101 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001102 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1103 if (!platform_info) {
1104 dev_err(&pdev->dev, "missing platform data\n");
1105 return -ENODEV;
1106 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001107 }
Stephen Streete0c99052006-03-07 23:53:24 -08001108
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001109 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001110 if (!ssp)
1111 ssp = &platform_info->ssp;
1112
1113 if (!ssp->mmio_base) {
1114 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001115 return -ENODEV;
1116 }
1117
1118 /* Allocate master with space for drv_data and null dma buffer */
1119 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1120 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001121 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001122 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001123 return -ENOMEM;
1124 }
1125 drv_data = spi_master_get_devdata(master);
1126 drv_data->master = master;
1127 drv_data->master_info = platform_info;
1128 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001129 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001130
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001131 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001132 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001133 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001134 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001135
Mika Westerberg851bacf2013-01-07 12:44:33 +02001136 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001137 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001138 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001139 master->cleanup = cleanup;
1140 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001141 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001142 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001143 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001144
eric miao2f1a74e2007-11-21 18:50:53 +08001145 drv_data->ssp_type = ssp->type;
Mika Westerberg2b9b84f2013-01-22 12:26:25 +02001146 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
Stephen Streete0c99052006-03-07 23:53:24 -08001147
eric miao2f1a74e2007-11-21 18:50:53 +08001148 drv_data->ioaddr = ssp->mmio_base;
1149 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001150 if (pxa25x_ssp_comp(drv_data)) {
Stephen Warren24778be2013-05-21 20:36:35 -06001151 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Stephen Streete0c99052006-03-07 23:53:24 -08001152 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1153 drv_data->dma_cr1 = 0;
1154 drv_data->clear_sr = SSSR_ROR;
1155 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1156 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001157 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001158 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001159 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001160 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1161 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1162 }
1163
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001164 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1165 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001166 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001167 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001168 goto out_error_master_alloc;
1169 }
1170
1171 /* Setup DMA if requested */
1172 drv_data->tx_channel = -1;
1173 drv_data->rx_channel = -1;
1174 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001175 status = pxa2xx_spi_dma_setup(drv_data);
1176 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001177 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001178 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001179 }
Stephen Streete0c99052006-03-07 23:53:24 -08001180 }
1181
1182 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001183 clk_prepare_enable(ssp->clk);
1184
1185 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001186
1187 /* Load default SSP configuration */
1188 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001189 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1190 SSCR1_TxTresh(TX_THRESH_DFLT),
1191 drv_data->ioaddr);
Eric Miaoc9840da2010-03-16 16:48:01 +08001192 write_SSCR0(SSCR0_SCR(2)
Stephen Streete0c99052006-03-07 23:53:24 -08001193 | SSCR0_Motorola
1194 | SSCR0_DataSize(8),
1195 drv_data->ioaddr);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001196 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -08001197 write_SSTO(0, drv_data->ioaddr);
1198 write_SSPSP(0, drv_data->ioaddr);
1199
Mika Westerberga0d26422013-01-22 12:26:32 +02001200 lpss_ssp_setup(drv_data);
1201
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001202 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1203 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001204
1205 /* Register with the SPI framework */
1206 platform_set_drvdata(pdev, drv_data);
Jingoo Hana807fcd2013-09-24 13:46:55 +09001207 status = devm_spi_register_master(&pdev->dev, master);
Stephen Streete0c99052006-03-07 23:53:24 -08001208 if (status != 0) {
1209 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001210 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001211 }
1212
Mika Westerberg7d94a502013-01-22 12:26:30 +02001213 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1214 pm_runtime_use_autosuspend(&pdev->dev);
1215 pm_runtime_set_active(&pdev->dev);
1216 pm_runtime_enable(&pdev->dev);
1217
Stephen Streete0c99052006-03-07 23:53:24 -08001218 return status;
1219
Stephen Streete0c99052006-03-07 23:53:24 -08001220out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001221 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001222 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001223 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001224
1225out_error_master_alloc:
1226 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001227 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001228 return status;
1229}
1230
1231static int pxa2xx_spi_remove(struct platform_device *pdev)
1232{
1233 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001234 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001235
1236 if (!drv_data)
1237 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001238 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001239
Mika Westerberg7d94a502013-01-22 12:26:30 +02001240 pm_runtime_get_sync(&pdev->dev);
1241
Stephen Streete0c99052006-03-07 23:53:24 -08001242 /* Disable the SSP at the peripheral and SOC level */
1243 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001244 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001245
1246 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001247 if (drv_data->master_info->enable_dma)
1248 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001249
Mika Westerberg7d94a502013-01-22 12:26:30 +02001250 pm_runtime_put_noidle(&pdev->dev);
1251 pm_runtime_disable(&pdev->dev);
1252
Stephen Streete0c99052006-03-07 23:53:24 -08001253 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001254 free_irq(ssp->irq, drv_data);
1255
1256 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001257 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001258
Stephen Streete0c99052006-03-07 23:53:24 -08001259 return 0;
1260}
1261
1262static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1263{
1264 int status = 0;
1265
1266 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1267 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1268}
1269
Mika Westerberg382cebb2014-01-16 14:50:55 +02001270#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001271static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001272{
Mike Rapoport86d25932009-07-21 17:50:16 +03001273 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001274 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001275 int status = 0;
1276
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001277 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001278 if (status != 0)
1279 return status;
1280 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001281 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001282
1283 return 0;
1284}
1285
Mike Rapoport86d25932009-07-21 17:50:16 +03001286static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001287{
Mike Rapoport86d25932009-07-21 17:50:16 +03001288 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001289 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001290 int status = 0;
1291
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001292 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001293
Stephen Streete0c99052006-03-07 23:53:24 -08001294 /* Enable the SSP clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001295 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001296
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001297 /* Restore LPSS private register bits */
1298 lpss_ssp_setup(drv_data);
1299
Stephen Streete0c99052006-03-07 23:53:24 -08001300 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001301 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001302 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001303 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001304 return status;
1305 }
1306
1307 return 0;
1308}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001309#endif
1310
1311#ifdef CONFIG_PM_RUNTIME
1312static int pxa2xx_spi_runtime_suspend(struct device *dev)
1313{
1314 struct driver_data *drv_data = dev_get_drvdata(dev);
1315
1316 clk_disable_unprepare(drv_data->ssp->clk);
1317 return 0;
1318}
1319
1320static int pxa2xx_spi_runtime_resume(struct device *dev)
1321{
1322 struct driver_data *drv_data = dev_get_drvdata(dev);
1323
1324 clk_prepare_enable(drv_data->ssp->clk);
1325 return 0;
1326}
1327#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001328
Alexey Dobriyan47145212009-12-14 18:00:08 -08001329static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001330 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1331 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1332 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001333};
Stephen Streete0c99052006-03-07 23:53:24 -08001334
1335static struct platform_driver driver = {
1336 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001337 .name = "pxa2xx-spi",
1338 .owner = THIS_MODULE,
Mike Rapoport86d25932009-07-21 17:50:16 +03001339 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001340 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001341 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001342 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001343 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001344 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001345};
1346
1347static int __init pxa2xx_spi_init(void)
1348{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001349 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001350}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001351subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001352
1353static void __exit pxa2xx_spi_exit(void)
1354{
1355 platform_driver_unregister(&driver);
1356}
1357module_exit(pxa2xx_spi_exit);