blob: 7de2ca1c4ee35d00a7d28a436d417542f59d27e3 [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 Westerberg0054e282013-03-05 12:05:17 +020072#define SSP_REG 0x0c
Mika Westerberga0d26422013-01-22 12:26:32 +020073#define SPI_CS_CONTROL 0x18
74#define SPI_CS_CONTROL_SW_MODE BIT(0)
75#define SPI_CS_CONTROL_CS_HIGH BIT(1)
76
77static bool is_lpss_ssp(const struct driver_data *drv_data)
78{
79 return drv_data->ssp_type == LPSS_SSP;
80}
81
82/*
83 * Read and write LPSS SSP private registers. Caller must first check that
84 * is_lpss_ssp() returns true before these can be called.
85 */
86static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
87{
88 WARN_ON(!drv_data->lpss_base);
89 return readl(drv_data->lpss_base + offset);
90}
91
92static void __lpss_ssp_write_priv(struct driver_data *drv_data,
93 unsigned offset, u32 value)
94{
95 WARN_ON(!drv_data->lpss_base);
96 writel(value, drv_data->lpss_base + offset);
97}
98
99/*
100 * lpss_ssp_setup - perform LPSS SSP specific setup
101 * @drv_data: pointer to the driver private data
102 *
103 * Perform LPSS SSP specific setup. This function must be called first if
104 * one is going to use LPSS SSP private registers.
105 */
106static void lpss_ssp_setup(struct driver_data *drv_data)
107{
108 unsigned offset = 0x400;
109 u32 value, orig;
110
111 if (!is_lpss_ssp(drv_data))
112 return;
113
114 /*
115 * Perform auto-detection of the LPSS SSP private registers. They
116 * can be either at 1k or 2k offset from the base address.
117 */
118 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
119
120 value = orig | SPI_CS_CONTROL_SW_MODE;
121 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
122 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
123 if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
124 offset = 0x800;
125 goto detection_done;
126 }
127
128 value &= ~SPI_CS_CONTROL_SW_MODE;
129 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
130 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
131 if (value != orig) {
132 offset = 0x800;
133 goto detection_done;
134 }
135
136detection_done:
137 /* Now set the LPSS base */
138 drv_data->lpss_base = drv_data->ioaddr + offset;
139
140 /* Enable software chip select control */
141 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
142 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200143
144 /* Enable multiblock DMA transfers */
145 if (drv_data->master_info->enable_dma)
146 __lpss_ssp_write_priv(drv_data, SSP_REG, 1);
Mika Westerberga0d26422013-01-22 12:26:32 +0200147}
148
149static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
150{
151 u32 value;
152
153 if (!is_lpss_ssp(drv_data))
154 return;
155
156 value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
157 if (enable)
158 value &= ~SPI_CS_CONTROL_CS_HIGH;
159 else
160 value |= SPI_CS_CONTROL_CS_HIGH;
161 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
162}
163
Eric Miaoa7bb3902009-04-06 19:00:54 -0700164static void cs_assert(struct driver_data *drv_data)
165{
166 struct chip_data *chip = drv_data->cur_chip;
167
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800168 if (drv_data->ssp_type == CE4100_SSP) {
169 write_SSSR(drv_data->cur_chip->frm, drv_data->ioaddr);
170 return;
171 }
172
Eric Miaoa7bb3902009-04-06 19:00:54 -0700173 if (chip->cs_control) {
174 chip->cs_control(PXA2XX_CS_ASSERT);
175 return;
176 }
177
Mika Westerberga0d26422013-01-22 12:26:32 +0200178 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700179 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200180 return;
181 }
182
183 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700184}
185
186static void cs_deassert(struct driver_data *drv_data)
187{
188 struct chip_data *chip = drv_data->cur_chip;
189
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800190 if (drv_data->ssp_type == CE4100_SSP)
191 return;
192
Eric Miaoa7bb3902009-04-06 19:00:54 -0700193 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300194 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700195 return;
196 }
197
Mika Westerberga0d26422013-01-22 12:26:32 +0200198 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700199 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200200 return;
201 }
202
203 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700204}
205
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200206int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800207{
208 unsigned long limit = loops_per_jiffy << 1;
209
David Brownellcf433692008-04-28 02:14:17 -0700210 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800211
212 do {
213 while (read_SSSR(reg) & SSSR_RNE) {
214 read_SSDR(reg);
215 }
Roel Kluin306c68a2009-04-21 12:24:46 -0700216 } while ((read_SSSR(reg) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800217 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800218
219 return limit;
220}
221
Stephen Street8d94cc52006-12-10 02:18:54 -0800222static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800223{
David Brownellcf433692008-04-28 02:14:17 -0700224 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800225 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800226
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800227 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800228 || (drv_data->tx == drv_data->tx_end))
229 return 0;
230
231 write_SSDR(0, reg);
232 drv_data->tx += n_bytes;
233
234 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800235}
236
Stephen Street8d94cc52006-12-10 02:18:54 -0800237static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800238{
David Brownellcf433692008-04-28 02:14:17 -0700239 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800240 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800241
242 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800243 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800244 read_SSDR(reg);
245 drv_data->rx += n_bytes;
246 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800247
248 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800249}
250
Stephen Street8d94cc52006-12-10 02:18:54 -0800251static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800252{
David Brownellcf433692008-04-28 02:14:17 -0700253 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800254
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800255 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800256 || (drv_data->tx == drv_data->tx_end))
257 return 0;
258
259 write_SSDR(*(u8 *)(drv_data->tx), reg);
260 ++drv_data->tx;
261
262 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800263}
264
Stephen Street8d94cc52006-12-10 02:18:54 -0800265static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800266{
David Brownellcf433692008-04-28 02:14:17 -0700267 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800268
269 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800270 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800271 *(u8 *)(drv_data->rx) = read_SSDR(reg);
272 ++drv_data->rx;
273 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800274
275 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800276}
277
Stephen Street8d94cc52006-12-10 02:18:54 -0800278static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800279{
David Brownellcf433692008-04-28 02:14:17 -0700280 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800281
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800282 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800283 || (drv_data->tx == drv_data->tx_end))
284 return 0;
285
286 write_SSDR(*(u16 *)(drv_data->tx), reg);
287 drv_data->tx += 2;
288
289 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800290}
291
Stephen Street8d94cc52006-12-10 02:18:54 -0800292static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800293{
David Brownellcf433692008-04-28 02:14:17 -0700294 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800295
296 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800297 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800298 *(u16 *)(drv_data->rx) = read_SSDR(reg);
299 drv_data->rx += 2;
300 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800301
302 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800303}
Stephen Street8d94cc52006-12-10 02:18:54 -0800304
305static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800306{
David Brownellcf433692008-04-28 02:14:17 -0700307 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800308
Sebastian Andrzej Siewior4a256052010-11-22 17:12:15 -0800309 if (((read_SSSR(reg) & SSSR_TFL_MASK) == SSSR_TFL_MASK)
Stephen Street8d94cc52006-12-10 02:18:54 -0800310 || (drv_data->tx == drv_data->tx_end))
311 return 0;
312
313 write_SSDR(*(u32 *)(drv_data->tx), reg);
314 drv_data->tx += 4;
315
316 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800317}
318
Stephen Street8d94cc52006-12-10 02:18:54 -0800319static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800320{
David Brownellcf433692008-04-28 02:14:17 -0700321 void __iomem *reg = drv_data->ioaddr;
Stephen Streete0c99052006-03-07 23:53:24 -0800322
323 while ((read_SSSR(reg) & SSSR_RNE)
Stephen Street8d94cc52006-12-10 02:18:54 -0800324 && (drv_data->rx < drv_data->rx_end)) {
Stephen Streete0c99052006-03-07 23:53:24 -0800325 *(u32 *)(drv_data->rx) = read_SSDR(reg);
326 drv_data->rx += 4;
327 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800328
329 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800330}
331
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200332void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800333{
334 struct spi_message *msg = drv_data->cur_msg;
335 struct spi_transfer *trans = drv_data->cur_transfer;
336
337 /* Move to next transfer */
338 if (trans->transfer_list.next != &msg->transfers) {
339 drv_data->cur_transfer =
340 list_entry(trans->transfer_list.next,
341 struct spi_transfer,
342 transfer_list);
343 return RUNNING_STATE;
344 } else
345 return DONE_STATE;
346}
347
Stephen Streete0c99052006-03-07 23:53:24 -0800348/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700349static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800350{
351 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700352 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800353
Stephen Street5daa3ba2006-05-20 15:00:19 -0700354 msg = drv_data->cur_msg;
355 drv_data->cur_msg = NULL;
356 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700357
358 last_transfer = list_entry(msg->transfers.prev,
Stephen Streete0c99052006-03-07 23:53:24 -0800359 struct spi_transfer,
360 transfer_list);
361
Ned Forrester84235972008-09-13 02:33:17 -0700362 /* Delay if requested before any change in chip select */
363 if (last_transfer->delay_usecs)
364 udelay(last_transfer->delay_usecs);
365
366 /* Drop chip select UNLESS cs_change is true or we are returning
367 * a message with an error, or next message is for another chip
368 */
Stephen Streete0c99052006-03-07 23:53:24 -0800369 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700370 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700371 else {
372 struct spi_message *next_msg;
373
374 /* Holding of cs was hinted, but we need to make sure
375 * the next message is for the same chip. Don't waste
376 * time with the following tests unless this was hinted.
377 *
378 * We cannot postpone this until pump_messages, because
379 * after calling msg->complete (below) the driver that
380 * sent the current message could be unloaded, which
381 * could invalidate the cs_control() callback...
382 */
383
384 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200385 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700386
387 /* see if the next and current messages point
388 * to the same chip
389 */
390 if (next_msg && next_msg->spi != msg->spi)
391 next_msg = NULL;
392 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700393 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700394 }
Stephen Streete0c99052006-03-07 23:53:24 -0800395
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200396 spi_finalize_current_message(drv_data->master);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700397 drv_data->cur_chip = NULL;
Stephen Streete0c99052006-03-07 23:53:24 -0800398}
399
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800400static void reset_sccr1(struct driver_data *drv_data)
401{
402 void __iomem *reg = drv_data->ioaddr;
403 struct chip_data *chip = drv_data->cur_chip;
404 u32 sccr1_reg;
405
406 sccr1_reg = read_SSCR1(reg) & ~drv_data->int_cr1;
407 sccr1_reg &= ~SSCR1_RFT;
408 sccr1_reg |= chip->threshold;
409 write_SSCR1(sccr1_reg, reg);
410}
411
Stephen Street8d94cc52006-12-10 02:18:54 -0800412static void int_error_stop(struct driver_data *drv_data, const char* msg)
413{
David Brownellcf433692008-04-28 02:14:17 -0700414 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800415
416 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800417 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800418 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800419 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800420 write_SSTO(0, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200421 pxa2xx_spi_flush(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800422 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
423
424 dev_err(&drv_data->pdev->dev, "%s\n", msg);
425
426 drv_data->cur_msg->state = ERROR_STATE;
427 tasklet_schedule(&drv_data->pump_transfers);
428}
429
430static void int_transfer_complete(struct driver_data *drv_data)
431{
David Brownellcf433692008-04-28 02:14:17 -0700432 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800433
434 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800435 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800436 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800437 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800438 write_SSTO(0, reg);
439
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300440 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800441 drv_data->cur_msg->actual_length += drv_data->len -
442 (drv_data->rx_end - drv_data->rx);
443
Ned Forrester84235972008-09-13 02:33:17 -0700444 /* Transfer delays and chip select release are
445 * handled in pump_transfers or giveback
446 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800447
448 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200449 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800450
451 /* Schedule transfer tasklet */
452 tasklet_schedule(&drv_data->pump_transfers);
453}
454
Stephen Streete0c99052006-03-07 23:53:24 -0800455static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
456{
David Brownellcf433692008-04-28 02:14:17 -0700457 void __iomem *reg = drv_data->ioaddr;
Stephen Street8d94cc52006-12-10 02:18:54 -0800458
Stephen Street5daa3ba2006-05-20 15:00:19 -0700459 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
460 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Streete0c99052006-03-07 23:53:24 -0800461
Stephen Street8d94cc52006-12-10 02:18:54 -0800462 u32 irq_status = read_SSSR(reg) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800463
Stephen Street8d94cc52006-12-10 02:18:54 -0800464 if (irq_status & SSSR_ROR) {
465 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
466 return IRQ_HANDLED;
467 }
Stephen Streete0c99052006-03-07 23:53:24 -0800468
Stephen Street8d94cc52006-12-10 02:18:54 -0800469 if (irq_status & SSSR_TINT) {
470 write_SSSR(SSSR_TINT, reg);
471 if (drv_data->read(drv_data)) {
472 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800473 return IRQ_HANDLED;
474 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800475 }
Stephen Streete0c99052006-03-07 23:53:24 -0800476
Stephen Street8d94cc52006-12-10 02:18:54 -0800477 /* Drain rx fifo, Fill tx fifo and prevent overruns */
478 do {
479 if (drv_data->read(drv_data)) {
480 int_transfer_complete(drv_data);
481 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800482 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800483 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800484
Stephen Street8d94cc52006-12-10 02:18:54 -0800485 if (drv_data->read(drv_data)) {
486 int_transfer_complete(drv_data);
487 return IRQ_HANDLED;
488 }
Stephen Streete0c99052006-03-07 23:53:24 -0800489
Stephen Street8d94cc52006-12-10 02:18:54 -0800490 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800491 u32 bytes_left;
492 u32 sccr1_reg;
493
494 sccr1_reg = read_SSCR1(reg);
495 sccr1_reg &= ~SSCR1_TIE;
496
497 /*
498 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800500 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800501 if (pxa25x_ssp_comp(drv_data)) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800502
503 sccr1_reg &= ~SSCR1_RFT;
504
505 bytes_left = drv_data->rx_end - drv_data->rx;
506 switch (drv_data->n_bytes) {
507 case 4:
508 bytes_left >>= 1;
509 case 2:
510 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800511 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800512
513 if (bytes_left > RX_THRESH_DFLT)
514 bytes_left = RX_THRESH_DFLT;
515
516 sccr1_reg |= SSCR1_RxTresh(bytes_left);
Stephen Streete0c99052006-03-07 23:53:24 -0800517 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800518 write_SSCR1(sccr1_reg, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800519 }
520
Stephen Street5daa3ba2006-05-20 15:00:19 -0700521 /* We did something */
522 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800523}
524
David Howells7d12e782006-10-05 14:55:46 +0100525static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800526{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400527 struct driver_data *drv_data = dev_id;
David Brownellcf433692008-04-28 02:14:17 -0700528 void __iomem *reg = drv_data->ioaddr;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200529 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800530 u32 mask = drv_data->mask_sr;
531 u32 status;
532
Mika Westerberg7d94a502013-01-22 12:26:30 +0200533 /*
534 * The IRQ might be shared with other peripherals so we must first
535 * check that are we RPM suspended or not. If we are we assume that
536 * the IRQ was not for us (we shouldn't be RPM suspended when the
537 * interrupt is enabled).
538 */
539 if (pm_runtime_suspended(&drv_data->pdev->dev))
540 return IRQ_NONE;
541
542 sccr1_reg = read_SSCR1(reg);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800543 status = read_SSSR(reg);
544
545 /* Ignore possible writes if we don't need to write */
546 if (!(sccr1_reg & SSCR1_TIE))
547 mask &= ~SSSR_TFS;
548
549 if (!(status & mask))
550 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800551
552 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700553
554 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
555 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800556 if (!pxa25x_ssp_comp(drv_data))
Stephen Street5daa3ba2006-05-20 15:00:19 -0700557 write_SSTO(0, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800558 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700559
Stephen Streete0c99052006-03-07 23:53:24 -0800560 dev_err(&drv_data->pdev->dev, "bad message state "
Stephen Street8d94cc52006-12-10 02:18:54 -0800561 "in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700562
Stephen Streete0c99052006-03-07 23:53:24 -0800563 /* Never fail */
564 return IRQ_HANDLED;
565 }
566
567 return drv_data->transfer_handler(drv_data);
568}
569
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200570static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800571{
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200572 unsigned long ssp_clk = drv_data->max_clk_rate;
573 const struct ssp_device *ssp = drv_data->ssp;
574
575 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800576
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800577 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
eric miao2f1a74e2007-11-21 18:50:53 +0800578 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
579 else
580 return ((ssp_clk / rate - 1) & 0xfff) << 8;
581}
582
Stephen Streete0c99052006-03-07 23:53:24 -0800583static void pump_transfers(unsigned long data)
584{
585 struct driver_data *drv_data = (struct driver_data *)data;
586 struct spi_message *message = NULL;
587 struct spi_transfer *transfer = NULL;
588 struct spi_transfer *previous = NULL;
589 struct chip_data *chip = NULL;
David Brownellcf433692008-04-28 02:14:17 -0700590 void __iomem *reg = drv_data->ioaddr;
Stephen Street9708c122006-03-28 14:05:23 -0800591 u32 clk_div = 0;
592 u8 bits = 0;
593 u32 speed = 0;
594 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800595 u32 cr1;
596 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
597 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Stephen Streete0c99052006-03-07 23:53:24 -0800598
599 /* Get current state information */
600 message = drv_data->cur_msg;
601 transfer = drv_data->cur_transfer;
602 chip = drv_data->cur_chip;
603
604 /* Handle for abort */
605 if (message->state == ERROR_STATE) {
606 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700607 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800608 return;
609 }
610
611 /* Handle end of message */
612 if (message->state == DONE_STATE) {
613 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700614 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800615 return;
616 }
617
Ned Forrester84235972008-09-13 02:33:17 -0700618 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800619 if (message->state == RUNNING_STATE) {
620 previous = list_entry(transfer->transfer_list.prev,
621 struct spi_transfer,
622 transfer_list);
623 if (previous->delay_usecs)
624 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700625
626 /* Drop chip select only if cs_change is requested */
627 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700628 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800629 }
630
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200631 /* Check if we can DMA this transfer */
632 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700633
634 /* reject already-mapped transfers; PIO won't always work */
635 if (message->is_dma_mapped
636 || transfer->rx_dma || transfer->tx_dma) {
637 dev_err(&drv_data->pdev->dev,
638 "pump_transfers: mapped transfer length "
Mike Rapoport20b918d2008-10-01 10:39:24 -0700639 "of %u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700640 transfer->len, MAX_DMA_LEN);
641 message->status = -EINVAL;
642 giveback(drv_data);
643 return;
644 }
645
646 /* warn ... we force this to PIO mode */
647 if (printk_ratelimit())
648 dev_warn(&message->spi->dev, "pump_transfers: "
649 "DMA disabled for transfer length %ld "
650 "greater than %d\n",
651 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800652 }
653
Stephen Streete0c99052006-03-07 23:53:24 -0800654 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200655 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800656 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
657 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700658 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800659 return;
660 }
Stephen Street9708c122006-03-28 14:05:23 -0800661 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800662 drv_data->tx = (void *)transfer->tx_buf;
663 drv_data->tx_end = drv_data->tx + transfer->len;
664 drv_data->rx = transfer->rx_buf;
665 drv_data->rx_end = drv_data->rx + transfer->len;
666 drv_data->rx_dma = transfer->rx_dma;
667 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200668 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800669 drv_data->write = drv_data->tx ? chip->write : null_writer;
670 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800671
672 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800673 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800674 if (transfer->speed_hz || transfer->bits_per_word) {
675
Stephen Street9708c122006-03-28 14:05:23 -0800676 bits = chip->bits_per_word;
677 speed = chip->speed_hz;
678
679 if (transfer->speed_hz)
680 speed = transfer->speed_hz;
681
682 if (transfer->bits_per_word)
683 bits = transfer->bits_per_word;
684
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200685 clk_div = ssp_get_clk_div(drv_data, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800686
687 if (bits <= 8) {
688 drv_data->n_bytes = 1;
Stephen Street9708c122006-03-28 14:05:23 -0800689 drv_data->read = drv_data->read != null_reader ?
690 u8_reader : null_reader;
691 drv_data->write = drv_data->write != null_writer ?
692 u8_writer : null_writer;
693 } else if (bits <= 16) {
694 drv_data->n_bytes = 2;
Stephen Street9708c122006-03-28 14:05:23 -0800695 drv_data->read = drv_data->read != null_reader ?
696 u16_reader : null_reader;
697 drv_data->write = drv_data->write != null_writer ?
698 u16_writer : null_writer;
699 } else if (bits <= 32) {
700 drv_data->n_bytes = 4;
Stephen Street9708c122006-03-28 14:05:23 -0800701 drv_data->read = drv_data->read != null_reader ?
702 u32_reader : null_reader;
703 drv_data->write = drv_data->write != null_writer ?
704 u32_writer : null_writer;
705 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800706 /* if bits/word is changed in dma mode, then must check the
707 * thresholds and burst also */
708 if (chip->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200709 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
710 message->spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800711 bits, &dma_burst,
712 &dma_thresh))
713 if (printk_ratelimit())
714 dev_warn(&message->spi->dev,
Ned Forrester7e964452008-09-13 02:33:18 -0700715 "pump_transfers: "
Stephen Street8d94cc52006-12-10 02:18:54 -0800716 "DMA burst size reduced to "
717 "match bits_per_word\n");
718 }
Stephen Street9708c122006-03-28 14:05:23 -0800719
720 cr0 = clk_div
721 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700722 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
Stephen Street9708c122006-03-28 14:05:23 -0800723 | SSCR0_SSE
724 | (bits > 16 ? SSCR0_EDSS : 0);
Stephen Street9708c122006-03-28 14:05:23 -0800725 }
726
Stephen Streete0c99052006-03-07 23:53:24 -0800727 message->state = RUNNING_STATE;
728
Ned Forrester7e964452008-09-13 02:33:18 -0700729 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200730 if (pxa2xx_spi_dma_is_possible(drv_data->len))
731 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700732 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800733
734 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200735 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800736
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200737 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800738
Stephen Street8d94cc52006-12-10 02:18:54 -0800739 /* Clear status and start DMA engine */
740 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Stephen Streete0c99052006-03-07 23:53:24 -0800741 write_SSSR(drv_data->clear_sr, reg);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200742
743 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800744 } else {
745 /* Ensure we have the correct interrupt handler */
746 drv_data->transfer_handler = interrupt_transfer;
747
Stephen Street8d94cc52006-12-10 02:18:54 -0800748 /* Clear status */
749 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800750 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800751 }
752
Mika Westerberga0d26422013-01-22 12:26:32 +0200753 if (is_lpss_ssp(drv_data)) {
754 if ((read_SSIRF(reg) & 0xff) != chip->lpss_rx_threshold)
755 write_SSIRF(chip->lpss_rx_threshold, reg);
756 if ((read_SSITF(reg) & 0xffff) != chip->lpss_tx_threshold)
757 write_SSITF(chip->lpss_tx_threshold, reg);
758 }
759
Stephen Street8d94cc52006-12-10 02:18:54 -0800760 /* see if we need to reload the config registers */
761 if ((read_SSCR0(reg) != cr0)
762 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
763 (cr1 & SSCR1_CHANGE_MASK)) {
764
Ned Forresterb97c74b2008-02-23 15:23:40 -0800765 /* stop the SSP, and update the other bits */
Stephen Street8d94cc52006-12-10 02:18:54 -0800766 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800767 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -0800768 write_SSTO(chip->timeout, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800769 /* first set CR1 without interrupt and service enables */
770 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
771 /* restart the SSP */
Stephen Street8d94cc52006-12-10 02:18:54 -0800772 write_SSCR0(cr0, reg);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800773
Stephen Street8d94cc52006-12-10 02:18:54 -0800774 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800775 if (!pxa25x_ssp_comp(drv_data))
Stephen Street8d94cc52006-12-10 02:18:54 -0800776 write_SSTO(chip->timeout, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800777 }
Ned Forresterb97c74b2008-02-23 15:23:40 -0800778
Eric Miaoa7bb3902009-04-06 19:00:54 -0700779 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800780
781 /* after chip select, release the data by enabling service
782 * requests and interrupts, without changing any mode bits */
783 write_SSCR1(cr1, reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800784}
785
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200786static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
787 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -0800788{
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200789 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -0800790
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200791 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800792 /* Initial message state*/
793 drv_data->cur_msg->state = START_STATE;
794 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
795 struct spi_transfer,
796 transfer_list);
797
Stephen Street8d94cc52006-12-10 02:18:54 -0800798 /* prepare to setup the SSP, in pump_transfers, using the per
799 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -0800800 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -0800801
802 /* Mark as busy and launch transfers */
803 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -0800804 return 0;
805}
806
Mika Westerberg7d94a502013-01-22 12:26:30 +0200807static int pxa2xx_spi_prepare_transfer(struct spi_master *master)
808{
809 struct driver_data *drv_data = spi_master_get_devdata(master);
810
811 pm_runtime_get_sync(&drv_data->pdev->dev);
812 return 0;
813}
814
815static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
816{
817 struct driver_data *drv_data = spi_master_get_devdata(master);
818
819 /* Disable the SSP now */
820 write_SSCR0(read_SSCR0(drv_data->ioaddr) & ~SSCR0_SSE,
821 drv_data->ioaddr);
822
823 pm_runtime_mark_last_busy(&drv_data->pdev->dev);
824 pm_runtime_put_autosuspend(&drv_data->pdev->dev);
825 return 0;
826}
827
Eric Miaoa7bb3902009-04-06 19:00:54 -0700828static int setup_cs(struct spi_device *spi, struct chip_data *chip,
829 struct pxa2xx_spi_chip *chip_info)
830{
831 int err = 0;
832
833 if (chip == NULL || chip_info == NULL)
834 return 0;
835
836 /* NOTE: setup() can be called multiple times, possibly with
837 * different chip_info, release previously requested GPIO
838 */
839 if (gpio_is_valid(chip->gpio_cs))
840 gpio_free(chip->gpio_cs);
841
842 /* If (*cs_control) is provided, ignore GPIO chip select */
843 if (chip_info->cs_control) {
844 chip->cs_control = chip_info->cs_control;
845 return 0;
846 }
847
848 if (gpio_is_valid(chip_info->gpio_cs)) {
849 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
850 if (err) {
851 dev_err(&spi->dev, "failed to request chip select "
852 "GPIO%d\n", chip_info->gpio_cs);
853 return err;
854 }
855
856 chip->gpio_cs = chip_info->gpio_cs;
857 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
858
859 err = gpio_direction_output(chip->gpio_cs,
860 !chip->gpio_cs_inverted);
861 }
862
863 return err;
864}
865
Stephen Streete0c99052006-03-07 23:53:24 -0800866static int setup(struct spi_device *spi)
867{
868 struct pxa2xx_spi_chip *chip_info = NULL;
869 struct chip_data *chip;
870 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
871 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +0200872 uint tx_thres, tx_hi_thres, rx_thres;
873
874 if (is_lpss_ssp(drv_data)) {
875 tx_thres = LPSS_TX_LOTHRESH_DFLT;
876 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
877 rx_thres = LPSS_RX_THRESH_DFLT;
878 } else {
879 tx_thres = TX_THRESH_DFLT;
880 tx_hi_thres = 0;
881 rx_thres = RX_THRESH_DFLT;
882 }
Stephen Streete0c99052006-03-07 23:53:24 -0800883
Stephen Street8d94cc52006-12-10 02:18:54 -0800884 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -0800885 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -0800886 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -0800887 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Stephen Street8d94cc52006-12-10 02:18:54 -0800888 if (!chip) {
889 dev_err(&spi->dev,
890 "failed setup: can't allocate chip data\n");
Stephen Streete0c99052006-03-07 23:53:24 -0800891 return -ENOMEM;
Stephen Street8d94cc52006-12-10 02:18:54 -0800892 }
Stephen Streete0c99052006-03-07 23:53:24 -0800893
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800894 if (drv_data->ssp_type == CE4100_SSP) {
895 if (spi->chip_select > 4) {
896 dev_err(&spi->dev, "failed setup: "
897 "cs number must not be > 4.\n");
898 kfree(chip);
899 return -EINVAL;
900 }
901
902 chip->frm = spi->chip_select;
903 } else
904 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -0800905 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700906 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -0800907 }
908
Stephen Street8d94cc52006-12-10 02:18:54 -0800909 /* protocol drivers may change the chip settings, so...
910 * if chip_info exists, use it */
911 chip_info = spi->controller_data;
912
Stephen Streete0c99052006-03-07 23:53:24 -0800913 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -0800914 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800915 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700916 if (chip_info->timeout)
917 chip->timeout = chip_info->timeout;
918 if (chip_info->tx_threshold)
919 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +0200920 if (chip_info->tx_hi_threshold)
921 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700922 if (chip_info->rx_threshold)
923 rx_thres = chip_info->rx_threshold;
924 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800925 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -0800926 if (chip_info->enable_loopback)
927 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +0200928 } else if (ACPI_HANDLE(&spi->dev)) {
929 /*
930 * Slave devices enumerated from ACPI namespace don't
931 * usually have chip_info but we still might want to use
932 * DMA with them.
933 */
934 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -0800935 }
936
Vernon Sauderf1f640a2008-10-15 22:02:43 -0700937 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
938 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
939
Mika Westerberga0d26422013-01-22 12:26:32 +0200940 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
941 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
942 | SSITF_TxHiThresh(tx_hi_thres);
943
Stephen Street8d94cc52006-12-10 02:18:54 -0800944 /* set dma burst and threshold outside of chip_info path so that if
945 * chip_info goes away after setting chip->enable_dma, the
946 * burst and threshold can still respond to changes in bits_per_word */
947 if (chip->enable_dma) {
948 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200949 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
950 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -0800951 &chip->dma_burst_size,
952 &chip->dma_threshold)) {
953 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
954 "to match bits_per_word\n");
955 }
956 }
957
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200958 clk_div = ssp_get_clk_div(drv_data, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -0800959 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -0800960
961 chip->cr0 = clk_div
962 | SSCR0_Motorola
Stephen Street5daa3ba2006-05-20 15:00:19 -0700963 | SSCR0_DataSize(spi->bits_per_word > 16 ?
964 spi->bits_per_word - 16 : spi->bits_per_word)
Stephen Streete0c99052006-03-07 23:53:24 -0800965 | SSCR0_SSE
966 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -0800967 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
968 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
969 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -0800970
Mika Westerbergb8331722013-01-22 12:26:31 +0200971 if (spi->mode & SPI_LOOP)
972 chip->cr1 |= SSCR1_LBM;
973
Stephen Streete0c99052006-03-07 23:53:24 -0800974 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800975 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -0700976 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200977 drv_data->max_clk_rate
Eric Miaoc9840da2010-03-16 16:48:01 +0800978 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
979 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800980 else
David Brownell7d077192009-06-17 16:26:03 -0700981 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200982 drv_data->max_clk_rate / 2
Eric Miaoc9840da2010-03-16 16:48:01 +0800983 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
984 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -0800985
986 if (spi->bits_per_word <= 8) {
987 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800988 chip->read = u8_reader;
989 chip->write = u8_writer;
990 } else if (spi->bits_per_word <= 16) {
991 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -0800992 chip->read = u16_reader;
993 chip->write = u16_writer;
994 } else if (spi->bits_per_word <= 32) {
995 chip->cr0 |= SSCR0_EDSS;
996 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -0800997 chip->read = u32_reader;
998 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -0800999 }
Stephen Street9708c122006-03-28 14:05:23 -08001000 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001001
1002 spi_set_ctldata(spi, chip);
1003
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001004 if (drv_data->ssp_type == CE4100_SSP)
1005 return 0;
1006
Eric Miaoa7bb3902009-04-06 19:00:54 -07001007 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001008}
1009
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001010static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001011{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001012 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001013 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001014
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001015 if (!chip)
1016 return;
1017
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001018 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001019 gpio_free(chip->gpio_cs);
1020
Stephen Streete0c99052006-03-07 23:53:24 -08001021 kfree(chip);
1022}
1023
Mika Westerberga3496852013-01-22 12:26:33 +02001024#ifdef CONFIG_ACPI
1025static int pxa2xx_spi_acpi_add_dma(struct acpi_resource *res, void *data)
1026{
1027 struct pxa2xx_spi_master *pdata = data;
1028
1029 if (res->type == ACPI_RESOURCE_TYPE_FIXED_DMA) {
1030 const struct acpi_resource_fixed_dma *dma;
1031
1032 dma = &res->data.fixed_dma;
1033 if (pdata->tx_slave_id < 0) {
1034 pdata->tx_slave_id = dma->request_lines;
1035 pdata->tx_chan_id = dma->channels;
1036 } else if (pdata->rx_slave_id < 0) {
1037 pdata->rx_slave_id = dma->request_lines;
1038 pdata->rx_chan_id = dma->channels;
1039 }
1040 }
1041
1042 /* Tell the ACPI core to skip this resource */
1043 return 1;
1044}
1045
1046static struct pxa2xx_spi_master *
1047pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1048{
1049 struct pxa2xx_spi_master *pdata;
1050 struct list_head resource_list;
1051 struct acpi_device *adev;
1052 struct ssp_device *ssp;
1053 struct resource *res;
1054 int devid;
1055
1056 if (!ACPI_HANDLE(&pdev->dev) ||
1057 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1058 return NULL;
1059
1060 pdata = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
1061 if (!pdata) {
1062 dev_err(&pdev->dev,
1063 "failed to allocate memory for platform data\n");
1064 return NULL;
1065 }
1066
1067 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1068 if (!res)
1069 return NULL;
1070
1071 ssp = &pdata->ssp;
1072
1073 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301074 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1075 if (IS_ERR(ssp->mmio_base))
1076 return PTR_ERR(ssp->mmio_base);
Mika Westerberga3496852013-01-22 12:26:33 +02001077
1078 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1079 ssp->irq = platform_get_irq(pdev, 0);
1080 ssp->type = LPSS_SSP;
1081 ssp->pdev = pdev;
1082
1083 ssp->port_id = -1;
1084 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1085 ssp->port_id = devid;
1086
1087 pdata->num_chipselect = 1;
1088 pdata->rx_slave_id = -1;
1089 pdata->tx_slave_id = -1;
1090
1091 INIT_LIST_HEAD(&resource_list);
1092 acpi_dev_get_resources(adev, &resource_list, pxa2xx_spi_acpi_add_dma,
1093 pdata);
1094 acpi_dev_free_resource_list(&resource_list);
1095
1096 pdata->enable_dma = pdata->rx_slave_id >= 0 && pdata->tx_slave_id >= 0;
1097
1098 return pdata;
1099}
1100
1101static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1102 { "INT33C0", 0 },
1103 { "INT33C1", 0 },
1104 { },
1105};
1106MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1107#else
1108static inline struct pxa2xx_spi_master *
1109pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1110{
1111 return NULL;
1112}
1113#endif
1114
Grant Likelyfd4a3192012-12-07 16:57:14 +00001115static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001116{
1117 struct device *dev = &pdev->dev;
1118 struct pxa2xx_spi_master *platform_info;
1119 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001120 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001121 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001122 int status;
Stephen Streete0c99052006-03-07 23:53:24 -08001123
Mika Westerberg851bacf2013-01-07 12:44:33 +02001124 platform_info = dev_get_platdata(dev);
1125 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001126 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1127 if (!platform_info) {
1128 dev_err(&pdev->dev, "missing platform data\n");
1129 return -ENODEV;
1130 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001131 }
Stephen Streete0c99052006-03-07 23:53:24 -08001132
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001133 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001134 if (!ssp)
1135 ssp = &platform_info->ssp;
1136
1137 if (!ssp->mmio_base) {
1138 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001139 return -ENODEV;
1140 }
1141
1142 /* Allocate master with space for drv_data and null dma buffer */
1143 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1144 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001145 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001146 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001147 return -ENOMEM;
1148 }
1149 drv_data = spi_master_get_devdata(master);
1150 drv_data->master = master;
1151 drv_data->master_info = platform_info;
1152 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001153 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001154
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001155 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001156 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001157 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001158 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001159
Mika Westerberg851bacf2013-01-07 12:44:33 +02001160 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001161 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001162 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001163 master->cleanup = cleanup;
1164 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001165 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001166 master->prepare_transfer_hardware = pxa2xx_spi_prepare_transfer;
1167 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -08001168
eric miao2f1a74e2007-11-21 18:50:53 +08001169 drv_data->ssp_type = ssp->type;
Mika Westerberg2b9b84f2013-01-22 12:26:25 +02001170 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
Stephen Streete0c99052006-03-07 23:53:24 -08001171
eric miao2f1a74e2007-11-21 18:50:53 +08001172 drv_data->ioaddr = ssp->mmio_base;
1173 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001174 if (pxa25x_ssp_comp(drv_data)) {
Stephen Warren24778be2013-05-21 20:36:35 -06001175 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Stephen Streete0c99052006-03-07 23:53:24 -08001176 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1177 drv_data->dma_cr1 = 0;
1178 drv_data->clear_sr = SSSR_ROR;
1179 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1180 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001181 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001182 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001183 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001184 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1185 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1186 }
1187
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001188 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1189 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001190 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001191 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001192 goto out_error_master_alloc;
1193 }
1194
1195 /* Setup DMA if requested */
1196 drv_data->tx_channel = -1;
1197 drv_data->rx_channel = -1;
1198 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001199 status = pxa2xx_spi_dma_setup(drv_data);
1200 if (status) {
1201 dev_warn(dev, "failed to setup DMA, using PIO\n");
1202 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001203 }
Stephen Streete0c99052006-03-07 23:53:24 -08001204 }
1205
1206 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001207 clk_prepare_enable(ssp->clk);
1208
1209 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001210
1211 /* Load default SSP configuration */
1212 write_SSCR0(0, drv_data->ioaddr);
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001213 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1214 SSCR1_TxTresh(TX_THRESH_DFLT),
1215 drv_data->ioaddr);
Eric Miaoc9840da2010-03-16 16:48:01 +08001216 write_SSCR0(SSCR0_SCR(2)
Stephen Streete0c99052006-03-07 23:53:24 -08001217 | SSCR0_Motorola
1218 | SSCR0_DataSize(8),
1219 drv_data->ioaddr);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001220 if (!pxa25x_ssp_comp(drv_data))
Stephen Streete0c99052006-03-07 23:53:24 -08001221 write_SSTO(0, drv_data->ioaddr);
1222 write_SSPSP(0, drv_data->ioaddr);
1223
Mika Westerberga0d26422013-01-22 12:26:32 +02001224 lpss_ssp_setup(drv_data);
1225
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001226 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1227 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001228
1229 /* Register with the SPI framework */
1230 platform_set_drvdata(pdev, drv_data);
1231 status = spi_register_master(master);
1232 if (status != 0) {
1233 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001234 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001235 }
1236
Mika Westerberg7d94a502013-01-22 12:26:30 +02001237 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1238 pm_runtime_use_autosuspend(&pdev->dev);
1239 pm_runtime_set_active(&pdev->dev);
1240 pm_runtime_enable(&pdev->dev);
1241
Stephen Streete0c99052006-03-07 23:53:24 -08001242 return status;
1243
Stephen Streete0c99052006-03-07 23:53:24 -08001244out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001245 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001246 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001247 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001248
1249out_error_master_alloc:
1250 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001251 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001252 return status;
1253}
1254
1255static int pxa2xx_spi_remove(struct platform_device *pdev)
1256{
1257 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001258 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001259
1260 if (!drv_data)
1261 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001262 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001263
Mika Westerberg7d94a502013-01-22 12:26:30 +02001264 pm_runtime_get_sync(&pdev->dev);
1265
Stephen Streete0c99052006-03-07 23:53:24 -08001266 /* Disable the SSP at the peripheral and SOC level */
1267 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001268 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001269
1270 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001271 if (drv_data->master_info->enable_dma)
1272 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001273
Mika Westerberg7d94a502013-01-22 12:26:30 +02001274 pm_runtime_put_noidle(&pdev->dev);
1275 pm_runtime_disable(&pdev->dev);
1276
Stephen Streete0c99052006-03-07 23:53:24 -08001277 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001278 free_irq(ssp->irq, drv_data);
1279
1280 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001281 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001282
1283 /* Disconnect from the SPI framework */
1284 spi_unregister_master(drv_data->master);
1285
1286 /* Prevent double remove */
1287 platform_set_drvdata(pdev, NULL);
1288
1289 return 0;
1290}
1291
1292static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1293{
1294 int status = 0;
1295
1296 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1297 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1298}
1299
1300#ifdef CONFIG_PM
Mike Rapoport86d25932009-07-21 17:50:16 +03001301static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001302{
Mike Rapoport86d25932009-07-21 17:50:16 +03001303 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001304 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001305 int status = 0;
1306
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001307 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001308 if (status != 0)
1309 return status;
1310 write_SSCR0(0, drv_data->ioaddr);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001311 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001312
1313 return 0;
1314}
1315
Mike Rapoport86d25932009-07-21 17:50:16 +03001316static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001317{
Mike Rapoport86d25932009-07-21 17:50:16 +03001318 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001319 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001320 int status = 0;
1321
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001322 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001323
Stephen Streete0c99052006-03-07 23:53:24 -08001324 /* Enable the SSP clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001325 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001326
1327 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001328 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001329 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001330 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001331 return status;
1332 }
1333
1334 return 0;
1335}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001336#endif
1337
1338#ifdef CONFIG_PM_RUNTIME
1339static int pxa2xx_spi_runtime_suspend(struct device *dev)
1340{
1341 struct driver_data *drv_data = dev_get_drvdata(dev);
1342
1343 clk_disable_unprepare(drv_data->ssp->clk);
1344 return 0;
1345}
1346
1347static int pxa2xx_spi_runtime_resume(struct device *dev)
1348{
1349 struct driver_data *drv_data = dev_get_drvdata(dev);
1350
1351 clk_prepare_enable(drv_data->ssp->clk);
1352 return 0;
1353}
1354#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001355
Alexey Dobriyan47145212009-12-14 18:00:08 -08001356static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001357 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1358 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1359 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001360};
Stephen Streete0c99052006-03-07 23:53:24 -08001361
1362static struct platform_driver driver = {
1363 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001364 .name = "pxa2xx-spi",
1365 .owner = THIS_MODULE,
Mike Rapoport86d25932009-07-21 17:50:16 +03001366 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001367 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001368 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001369 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001370 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001371 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001372};
1373
1374static int __init pxa2xx_spi_init(void)
1375{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001376 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001377}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001378subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001379
1380static void __exit pxa2xx_spi_exit(void)
1381{
1382 platform_driver_unregister(&driver);
1383}
1384module_exit(pxa2xx_spi_exit);