spi: pxa2xx: replace ugly table by approximation
[deliverable/linux.git] / drivers / spi / spi-pxa2xx.c
CommitLineData
e0c9905e
SS
1/*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
a0d2642e 3 * Copyright (C) 2013, Intel Corporation
e0c9905e
SS
4 *
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.
e0c9905e
SS
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/ioport.h>
20#include <linux/errno.h>
cbfd6a21 21#include <linux/err.h>
e0c9905e 22#include <linux/interrupt.h>
9df461ec 23#include <linux/kernel.h>
e0c9905e 24#include <linux/platform_device.h>
8348c259 25#include <linux/spi/pxa2xx_spi.h>
e0c9905e 26#include <linux/spi/spi.h>
e0c9905e 27#include <linux/delay.h>
a7bb3909 28#include <linux/gpio.h>
5a0e3ad6 29#include <linux/slab.h>
3343b7a6 30#include <linux/clk.h>
7d94a505 31#include <linux/pm_runtime.h>
a3496855 32#include <linux/acpi.h>
e0c9905e 33
cd7bed00 34#include "spi-pxa2xx.h"
e0c9905e
SS
35
36MODULE_AUTHOR("Stephen Street");
037cdafe 37MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
e0c9905e 38MODULE_LICENSE("GPL");
7e38c3c4 39MODULE_ALIAS("platform:pxa2xx-spi");
e0c9905e 40
f1f640a9
VS
41#define TIMOUT_DFLT 1000
42
b97c74bd
NF
43/*
44 * for testing SSCR1 changes that require SSP restart, basically
45 * everything except the service and interrupt enables, the pxa270 developer
46 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
47 * list, but the PXA255 dev man says all bits without really meaning the
48 * service and interrupt enables
49 */
50#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
8d94cc50 51 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
b97c74bd
NF
52 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
53 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
54 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
55 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
8d94cc50 56
e5262d05
WC
57#define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
58 | QUARK_X1000_SSCR1_EFWR \
59 | QUARK_X1000_SSCR1_RFT \
60 | QUARK_X1000_SSCR1_TFT \
61 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
62
a0d2642e
MW
63#define LPSS_RX_THRESH_DFLT 64
64#define LPSS_TX_LOTHRESH_DFLT 160
65#define LPSS_TX_HITHRESH_DFLT 224
66
67/* Offset from drv_data->lpss_base */
1de70612
MW
68#define GENERAL_REG 0x08
69#define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
0054e28d 70#define SSP_REG 0x0c
a0d2642e
MW
71#define SPI_CS_CONTROL 0x18
72#define SPI_CS_CONTROL_SW_MODE BIT(0)
73#define SPI_CS_CONTROL_CS_HIGH BIT(1)
74
75static bool is_lpss_ssp(const struct driver_data *drv_data)
76{
77 return drv_data->ssp_type == LPSS_SSP;
78}
79
e5262d05
WC
80static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
81{
82 return drv_data->ssp_type == QUARK_X1000_SSP;
83}
84
4fdb2424
WC
85static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
86{
87 switch (drv_data->ssp_type) {
e5262d05
WC
88 case QUARK_X1000_SSP:
89 return QUARK_X1000_SSCR1_CHANGE_MASK;
4fdb2424
WC
90 default:
91 return SSCR1_CHANGE_MASK;
92 }
93}
94
95static u32
96pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
97{
98 switch (drv_data->ssp_type) {
e5262d05
WC
99 case QUARK_X1000_SSP:
100 return RX_THRESH_QUARK_X1000_DFLT;
4fdb2424
WC
101 default:
102 return RX_THRESH_DFLT;
103 }
104}
105
106static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
107{
4fdb2424
WC
108 u32 mask;
109
110 switch (drv_data->ssp_type) {
e5262d05
WC
111 case QUARK_X1000_SSP:
112 mask = QUARK_X1000_SSSR_TFL_MASK;
113 break;
4fdb2424
WC
114 default:
115 mask = SSSR_TFL_MASK;
116 break;
117 }
118
c039dd27 119 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
4fdb2424
WC
120}
121
122static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
123 u32 *sccr1_reg)
124{
125 u32 mask;
126
127 switch (drv_data->ssp_type) {
e5262d05
WC
128 case QUARK_X1000_SSP:
129 mask = QUARK_X1000_SSCR1_RFT;
130 break;
4fdb2424
WC
131 default:
132 mask = SSCR1_RFT;
133 break;
134 }
135 *sccr1_reg &= ~mask;
136}
137
138static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
139 u32 *sccr1_reg, u32 threshold)
140{
141 switch (drv_data->ssp_type) {
e5262d05
WC
142 case QUARK_X1000_SSP:
143 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
144 break;
4fdb2424
WC
145 default:
146 *sccr1_reg |= SSCR1_RxTresh(threshold);
147 break;
148 }
149}
150
151static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
152 u32 clk_div, u8 bits)
153{
154 switch (drv_data->ssp_type) {
e5262d05
WC
155 case QUARK_X1000_SSP:
156 return clk_div
157 | QUARK_X1000_SSCR0_Motorola
158 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
159 | SSCR0_SSE;
4fdb2424
WC
160 default:
161 return clk_div
162 | SSCR0_Motorola
163 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
164 | SSCR0_SSE
165 | (bits > 16 ? SSCR0_EDSS : 0);
166 }
167}
168
a0d2642e
MW
169/*
170 * Read and write LPSS SSP private registers. Caller must first check that
171 * is_lpss_ssp() returns true before these can be called.
172 */
173static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
174{
175 WARN_ON(!drv_data->lpss_base);
176 return readl(drv_data->lpss_base + offset);
177}
178
179static void __lpss_ssp_write_priv(struct driver_data *drv_data,
180 unsigned offset, u32 value)
181{
182 WARN_ON(!drv_data->lpss_base);
183 writel(value, drv_data->lpss_base + offset);
184}
185
186/*
187 * lpss_ssp_setup - perform LPSS SSP specific setup
188 * @drv_data: pointer to the driver private data
189 *
190 * Perform LPSS SSP specific setup. This function must be called first if
191 * one is going to use LPSS SSP private registers.
192 */
193static void lpss_ssp_setup(struct driver_data *drv_data)
194{
195 unsigned offset = 0x400;
196 u32 value, orig;
197
a0d2642e
MW
198 /*
199 * Perform auto-detection of the LPSS SSP private registers. They
200 * can be either at 1k or 2k offset from the base address.
201 */
202 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
203
e61f487f 204 /* Test SPI_CS_CONTROL_SW_MODE bit enabling */
a0d2642e
MW
205 value = orig | SPI_CS_CONTROL_SW_MODE;
206 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
207 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
208 if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
209 offset = 0x800;
210 goto detection_done;
211 }
212
e61f487f
CCE
213 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
214
215 /* Test SPI_CS_CONTROL_SW_MODE bit disabling */
216 value = orig & ~SPI_CS_CONTROL_SW_MODE;
a0d2642e
MW
217 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
218 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
e61f487f 219 if (value != (orig & ~SPI_CS_CONTROL_SW_MODE)) {
a0d2642e
MW
220 offset = 0x800;
221 goto detection_done;
222 }
223
224detection_done:
225 /* Now set the LPSS base */
226 drv_data->lpss_base = drv_data->ioaddr + offset;
227
228 /* Enable software chip select control */
229 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
230 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
0054e28d
MW
231
232 /* Enable multiblock DMA transfers */
1de70612 233 if (drv_data->master_info->enable_dma) {
0054e28d 234 __lpss_ssp_write_priv(drv_data, SSP_REG, 1);
1de70612
MW
235
236 value = __lpss_ssp_read_priv(drv_data, GENERAL_REG);
237 value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
238 __lpss_ssp_write_priv(drv_data, GENERAL_REG, value);
239 }
a0d2642e
MW
240}
241
242static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
243{
244 u32 value;
245
a0d2642e
MW
246 value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
247 if (enable)
248 value &= ~SPI_CS_CONTROL_CS_HIGH;
249 else
250 value |= SPI_CS_CONTROL_CS_HIGH;
251 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
252}
253
a7bb3909
EM
254static void cs_assert(struct driver_data *drv_data)
255{
256 struct chip_data *chip = drv_data->cur_chip;
257
2a8626a9 258 if (drv_data->ssp_type == CE4100_SSP) {
c039dd27 259 pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
2a8626a9
SAS
260 return;
261 }
262
a7bb3909
EM
263 if (chip->cs_control) {
264 chip->cs_control(PXA2XX_CS_ASSERT);
265 return;
266 }
267
a0d2642e 268 if (gpio_is_valid(chip->gpio_cs)) {
a7bb3909 269 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
a0d2642e
MW
270 return;
271 }
272
7566bcc7
JN
273 if (is_lpss_ssp(drv_data))
274 lpss_ssp_cs_control(drv_data, true);
a7bb3909
EM
275}
276
277static void cs_deassert(struct driver_data *drv_data)
278{
279 struct chip_data *chip = drv_data->cur_chip;
280
2a8626a9
SAS
281 if (drv_data->ssp_type == CE4100_SSP)
282 return;
283
a7bb3909 284 if (chip->cs_control) {
2b2562d3 285 chip->cs_control(PXA2XX_CS_DEASSERT);
a7bb3909
EM
286 return;
287 }
288
a0d2642e 289 if (gpio_is_valid(chip->gpio_cs)) {
a7bb3909 290 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
a0d2642e
MW
291 return;
292 }
293
7566bcc7
JN
294 if (is_lpss_ssp(drv_data))
295 lpss_ssp_cs_control(drv_data, false);
a7bb3909
EM
296}
297
cd7bed00 298int pxa2xx_spi_flush(struct driver_data *drv_data)
e0c9905e
SS
299{
300 unsigned long limit = loops_per_jiffy << 1;
301
e0c9905e 302 do {
c039dd27
JN
303 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
304 pxa2xx_spi_read(drv_data, SSDR);
305 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
2a8626a9 306 write_SSSR_CS(drv_data, SSSR_ROR);
e0c9905e
SS
307
308 return limit;
309}
310
8d94cc50 311static int null_writer(struct driver_data *drv_data)
e0c9905e 312{
9708c121 313 u8 n_bytes = drv_data->n_bytes;
e0c9905e 314
4fdb2424 315 if (pxa2xx_spi_txfifo_full(drv_data)
8d94cc50
SS
316 || (drv_data->tx == drv_data->tx_end))
317 return 0;
318
c039dd27 319 pxa2xx_spi_write(drv_data, SSDR, 0);
8d94cc50
SS
320 drv_data->tx += n_bytes;
321
322 return 1;
e0c9905e
SS
323}
324
8d94cc50 325static int null_reader(struct driver_data *drv_data)
e0c9905e 326{
9708c121 327 u8 n_bytes = drv_data->n_bytes;
e0c9905e 328
c039dd27
JN
329 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
330 && (drv_data->rx < drv_data->rx_end)) {
331 pxa2xx_spi_read(drv_data, SSDR);
e0c9905e
SS
332 drv_data->rx += n_bytes;
333 }
8d94cc50
SS
334
335 return drv_data->rx == drv_data->rx_end;
e0c9905e
SS
336}
337
8d94cc50 338static int u8_writer(struct driver_data *drv_data)
e0c9905e 339{
4fdb2424 340 if (pxa2xx_spi_txfifo_full(drv_data)
8d94cc50
SS
341 || (drv_data->tx == drv_data->tx_end))
342 return 0;
343
c039dd27 344 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
8d94cc50
SS
345 ++drv_data->tx;
346
347 return 1;
e0c9905e
SS
348}
349
8d94cc50 350static int u8_reader(struct driver_data *drv_data)
e0c9905e 351{
c039dd27
JN
352 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
353 && (drv_data->rx < drv_data->rx_end)) {
354 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
e0c9905e
SS
355 ++drv_data->rx;
356 }
8d94cc50
SS
357
358 return drv_data->rx == drv_data->rx_end;
e0c9905e
SS
359}
360
8d94cc50 361static int u16_writer(struct driver_data *drv_data)
e0c9905e 362{
4fdb2424 363 if (pxa2xx_spi_txfifo_full(drv_data)
8d94cc50
SS
364 || (drv_data->tx == drv_data->tx_end))
365 return 0;
366
c039dd27 367 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
8d94cc50
SS
368 drv_data->tx += 2;
369
370 return 1;
e0c9905e
SS
371}
372
8d94cc50 373static int u16_reader(struct driver_data *drv_data)
e0c9905e 374{
c039dd27
JN
375 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
376 && (drv_data->rx < drv_data->rx_end)) {
377 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
e0c9905e
SS
378 drv_data->rx += 2;
379 }
8d94cc50
SS
380
381 return drv_data->rx == drv_data->rx_end;
e0c9905e 382}
8d94cc50
SS
383
384static int u32_writer(struct driver_data *drv_data)
e0c9905e 385{
4fdb2424 386 if (pxa2xx_spi_txfifo_full(drv_data)
8d94cc50
SS
387 || (drv_data->tx == drv_data->tx_end))
388 return 0;
389
c039dd27 390 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
8d94cc50
SS
391 drv_data->tx += 4;
392
393 return 1;
e0c9905e
SS
394}
395
8d94cc50 396static int u32_reader(struct driver_data *drv_data)
e0c9905e 397{
c039dd27
JN
398 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
399 && (drv_data->rx < drv_data->rx_end)) {
400 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
e0c9905e
SS
401 drv_data->rx += 4;
402 }
8d94cc50
SS
403
404 return drv_data->rx == drv_data->rx_end;
e0c9905e
SS
405}
406
cd7bed00 407void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
e0c9905e
SS
408{
409 struct spi_message *msg = drv_data->cur_msg;
410 struct spi_transfer *trans = drv_data->cur_transfer;
411
412 /* Move to next transfer */
413 if (trans->transfer_list.next != &msg->transfers) {
414 drv_data->cur_transfer =
415 list_entry(trans->transfer_list.next,
416 struct spi_transfer,
417 transfer_list);
418 return RUNNING_STATE;
419 } else
420 return DONE_STATE;
421}
422
e0c9905e 423/* caller already set message->status; dma and pio irqs are blocked */
5daa3ba0 424static void giveback(struct driver_data *drv_data)
e0c9905e
SS
425{
426 struct spi_transfer* last_transfer;
5daa3ba0 427 struct spi_message *msg;
e0c9905e 428
5daa3ba0
SS
429 msg = drv_data->cur_msg;
430 drv_data->cur_msg = NULL;
431 drv_data->cur_transfer = NULL;
5daa3ba0 432
23e2c2aa 433 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
e0c9905e
SS
434 transfer_list);
435
8423597d
NF
436 /* Delay if requested before any change in chip select */
437 if (last_transfer->delay_usecs)
438 udelay(last_transfer->delay_usecs);
439
440 /* Drop chip select UNLESS cs_change is true or we are returning
441 * a message with an error, or next message is for another chip
442 */
e0c9905e 443 if (!last_transfer->cs_change)
a7bb3909 444 cs_deassert(drv_data);
8423597d
NF
445 else {
446 struct spi_message *next_msg;
447
448 /* Holding of cs was hinted, but we need to make sure
449 * the next message is for the same chip. Don't waste
450 * time with the following tests unless this was hinted.
451 *
452 * We cannot postpone this until pump_messages, because
453 * after calling msg->complete (below) the driver that
454 * sent the current message could be unloaded, which
455 * could invalidate the cs_control() callback...
456 */
457
458 /* get a pointer to the next message, if any */
7f86bde9 459 next_msg = spi_get_next_queued_message(drv_data->master);
8423597d
NF
460
461 /* see if the next and current messages point
462 * to the same chip
463 */
464 if (next_msg && next_msg->spi != msg->spi)
465 next_msg = NULL;
466 if (!next_msg || msg->state == ERROR_STATE)
a7bb3909 467 cs_deassert(drv_data);
8423597d 468 }
e0c9905e 469
a7bb3909 470 drv_data->cur_chip = NULL;
c957e8f0 471 spi_finalize_current_message(drv_data->master);
e0c9905e
SS
472}
473
579d3bb2
SAS
474static void reset_sccr1(struct driver_data *drv_data)
475{
579d3bb2
SAS
476 struct chip_data *chip = drv_data->cur_chip;
477 u32 sccr1_reg;
478
c039dd27 479 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
579d3bb2
SAS
480 sccr1_reg &= ~SSCR1_RFT;
481 sccr1_reg |= chip->threshold;
c039dd27 482 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
579d3bb2
SAS
483}
484
8d94cc50 485static void int_error_stop(struct driver_data *drv_data, const char* msg)
e0c9905e 486{
8d94cc50 487 /* Stop and reset SSP */
2a8626a9 488 write_SSSR_CS(drv_data, drv_data->clear_sr);
579d3bb2 489 reset_sccr1(drv_data);
2a8626a9 490 if (!pxa25x_ssp_comp(drv_data))
c039dd27 491 pxa2xx_spi_write(drv_data, SSTO, 0);
cd7bed00 492 pxa2xx_spi_flush(drv_data);
c039dd27
JN
493 pxa2xx_spi_write(drv_data, SSCR0,
494 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
e0c9905e 495
8d94cc50 496 dev_err(&drv_data->pdev->dev, "%s\n", msg);
e0c9905e 497
8d94cc50
SS
498 drv_data->cur_msg->state = ERROR_STATE;
499 tasklet_schedule(&drv_data->pump_transfers);
500}
5daa3ba0 501
8d94cc50
SS
502static void int_transfer_complete(struct driver_data *drv_data)
503{
8d94cc50 504 /* Stop SSP */
2a8626a9 505 write_SSSR_CS(drv_data, drv_data->clear_sr);
579d3bb2 506 reset_sccr1(drv_data);
2a8626a9 507 if (!pxa25x_ssp_comp(drv_data))
c039dd27 508 pxa2xx_spi_write(drv_data, SSTO, 0);
e0c9905e 509
25985edc 510 /* Update total byte transferred return count actual bytes read */
8d94cc50
SS
511 drv_data->cur_msg->actual_length += drv_data->len -
512 (drv_data->rx_end - drv_data->rx);
e0c9905e 513
8423597d
NF
514 /* Transfer delays and chip select release are
515 * handled in pump_transfers or giveback
516 */
e0c9905e 517
8d94cc50 518 /* Move to next transfer */
cd7bed00 519 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
e0c9905e 520
8d94cc50
SS
521 /* Schedule transfer tasklet */
522 tasklet_schedule(&drv_data->pump_transfers);
523}
e0c9905e 524
8d94cc50
SS
525static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
526{
c039dd27
JN
527 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
528 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
e0c9905e 529
c039dd27 530 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
e0c9905e 531
8d94cc50
SS
532 if (irq_status & SSSR_ROR) {
533 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
534 return IRQ_HANDLED;
535 }
e0c9905e 536
8d94cc50 537 if (irq_status & SSSR_TINT) {
c039dd27 538 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
8d94cc50
SS
539 if (drv_data->read(drv_data)) {
540 int_transfer_complete(drv_data);
541 return IRQ_HANDLED;
542 }
543 }
e0c9905e 544
8d94cc50
SS
545 /* Drain rx fifo, Fill tx fifo and prevent overruns */
546 do {
547 if (drv_data->read(drv_data)) {
548 int_transfer_complete(drv_data);
549 return IRQ_HANDLED;
550 }
551 } while (drv_data->write(drv_data));
e0c9905e 552
8d94cc50
SS
553 if (drv_data->read(drv_data)) {
554 int_transfer_complete(drv_data);
555 return IRQ_HANDLED;
556 }
e0c9905e 557
8d94cc50 558 if (drv_data->tx == drv_data->tx_end) {
579d3bb2
SAS
559 u32 bytes_left;
560 u32 sccr1_reg;
561
c039dd27 562 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
579d3bb2
SAS
563 sccr1_reg &= ~SSCR1_TIE;
564
565 /*
566 * PXA25x_SSP has no timeout, set up rx threshould for the
25985edc 567 * remaining RX bytes.
579d3bb2 568 */
2a8626a9 569 if (pxa25x_ssp_comp(drv_data)) {
4fdb2424 570 u32 rx_thre;
579d3bb2 571
4fdb2424 572 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
579d3bb2
SAS
573
574 bytes_left = drv_data->rx_end - drv_data->rx;
575 switch (drv_data->n_bytes) {
576 case 4:
577 bytes_left >>= 1;
578 case 2:
579 bytes_left >>= 1;
8d94cc50 580 }
579d3bb2 581
4fdb2424
WC
582 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
583 if (rx_thre > bytes_left)
584 rx_thre = bytes_left;
579d3bb2 585
4fdb2424 586 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
e0c9905e 587 }
c039dd27 588 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
e0c9905e
SS
589 }
590
5daa3ba0
SS
591 /* We did something */
592 return IRQ_HANDLED;
e0c9905e
SS
593}
594
7d12e780 595static irqreturn_t ssp_int(int irq, void *dev_id)
e0c9905e 596{
c7bec5ab 597 struct driver_data *drv_data = dev_id;
7d94a505 598 u32 sccr1_reg;
49cbb1e0
SAS
599 u32 mask = drv_data->mask_sr;
600 u32 status;
601
7d94a505
MW
602 /*
603 * The IRQ might be shared with other peripherals so we must first
604 * check that are we RPM suspended or not. If we are we assume that
605 * the IRQ was not for us (we shouldn't be RPM suspended when the
606 * interrupt is enabled).
607 */
608 if (pm_runtime_suspended(&drv_data->pdev->dev))
609 return IRQ_NONE;
610
269e4a41
MW
611 /*
612 * If the device is not yet in RPM suspended state and we get an
613 * interrupt that is meant for another device, check if status bits
614 * are all set to one. That means that the device is already
615 * powered off.
616 */
c039dd27 617 status = pxa2xx_spi_read(drv_data, SSSR);
269e4a41
MW
618 if (status == ~0)
619 return IRQ_NONE;
620
c039dd27 621 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
49cbb1e0
SAS
622
623 /* Ignore possible writes if we don't need to write */
624 if (!(sccr1_reg & SSCR1_TIE))
625 mask &= ~SSSR_TFS;
626
627 if (!(status & mask))
628 return IRQ_NONE;
e0c9905e
SS
629
630 if (!drv_data->cur_msg) {
5daa3ba0 631
c039dd27
JN
632 pxa2xx_spi_write(drv_data, SSCR0,
633 pxa2xx_spi_read(drv_data, SSCR0)
634 & ~SSCR0_SSE);
635 pxa2xx_spi_write(drv_data, SSCR1,
636 pxa2xx_spi_read(drv_data, SSCR1)
637 & ~drv_data->int_cr1);
2a8626a9 638 if (!pxa25x_ssp_comp(drv_data))
c039dd27 639 pxa2xx_spi_write(drv_data, SSTO, 0);
2a8626a9 640 write_SSSR_CS(drv_data, drv_data->clear_sr);
5daa3ba0 641
f6bd03a7
JN
642 dev_err(&drv_data->pdev->dev,
643 "bad message state in interrupt handler\n");
5daa3ba0 644
e0c9905e
SS
645 /* Never fail */
646 return IRQ_HANDLED;
647 }
648
649 return drv_data->transfer_handler(drv_data);
650}
651
e5262d05 652/*
9df461ec
AS
653 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
654 * input frequency by fractions of 2^24. It also has a divider by 5.
655 *
656 * There are formulas to get baud rate value for given input frequency and
657 * divider parameters, such as DDS_CLK_RATE and SCR:
658 *
659 * Fsys = 200MHz
660 *
661 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
662 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
663 *
664 * DDS_CLK_RATE either 2^n or 2^n / 5.
665 * SCR is in range 0 .. 255
666 *
667 * Divisor = 5^i * 2^j * 2 * k
668 * i = [0, 1] i = 1 iff j = 0 or j > 3
669 * j = [0, 23] j = 0 iff i = 1
670 * k = [1, 256]
671 * Special case: j = 0, i = 1: Divisor = 2 / 5
672 *
673 * Accordingly to the specification the recommended values for DDS_CLK_RATE
674 * are:
675 * Case 1: 2^n, n = [0, 23]
676 * Case 2: 2^24 * 2 / 5 (0x666666)
677 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
678 *
679 * In all cases the lowest possible value is better.
680 *
681 * The function calculates parameters for all cases and chooses the one closest
682 * to the asked baud rate.
e5262d05 683 */
9df461ec
AS
684static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
685{
686 unsigned long xtal = 200000000;
687 unsigned long fref = xtal / 2; /* mandatory division by 2,
688 see (2) */
689 /* case 3 */
690 unsigned long fref1 = fref / 2; /* case 1 */
691 unsigned long fref2 = fref * 2 / 5; /* case 2 */
692 unsigned long scale;
693 unsigned long q, q1, q2;
694 long r, r1, r2;
695 u32 mul;
696
697 /* Case 1 */
698
699 /* Set initial value for DDS_CLK_RATE */
700 mul = (1 << 24) >> 1;
701
702 /* Calculate initial quot */
703 q1 = DIV_ROUND_CLOSEST(fref1, rate);
704
705 /* Scale q1 if it's too big */
706 if (q1 > 256) {
707 /* Scale q1 to range [1, 512] */
708 scale = fls_long(q1 - 1);
709 if (scale > 9) {
710 q1 >>= scale - 9;
711 mul >>= scale - 9;
e5262d05 712 }
9df461ec
AS
713
714 /* Round the result if we have a remainder */
715 q1 += q1 & 1;
716 }
717
718 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
719 scale = __ffs(q1);
720 q1 >>= scale;
721 mul >>= scale;
722
723 /* Get the remainder */
724 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
725
726 /* Case 2 */
727
728 q2 = DIV_ROUND_CLOSEST(fref2, rate);
729 r2 = abs(fref2 / q2 - rate);
730
731 /*
732 * Choose the best between two: less remainder we have the better. We
733 * can't go case 2 if q2 is greater than 256 since SCR register can
734 * hold only values 0 .. 255.
735 */
736 if (r2 >= r1 || q2 > 256) {
737 /* case 1 is better */
738 r = r1;
739 q = q1;
740 } else {
741 /* case 2 is better */
742 r = r2;
743 q = q2;
744 mul = (1 << 24) * 2 / 5;
e5262d05
WC
745 }
746
9df461ec
AS
747 /* Check case 3 only If the divisor is big enough */
748 if (fref / rate >= 80) {
749 u64 fssp;
750 u32 m;
751
752 /* Calculate initial quot */
753 q1 = DIV_ROUND_CLOSEST(fref, rate);
754 m = (1 << 24) / q1;
755
756 /* Get the remainder */
757 fssp = (u64)fref * m;
758 do_div(fssp, 1 << 24);
759 r1 = abs(fssp - rate);
760
761 /* Choose this one if it suits better */
762 if (r1 < r) {
763 /* case 3 is better */
764 q = 1;
765 mul = m;
766 }
767 }
e5262d05 768
9df461ec
AS
769 *dds = mul;
770 return q - 1;
e5262d05
WC
771}
772
3343b7a6 773static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
2f1a74e5 774{
3343b7a6
MW
775 unsigned long ssp_clk = drv_data->max_clk_rate;
776 const struct ssp_device *ssp = drv_data->ssp;
777
778 rate = min_t(int, ssp_clk, rate);
2f1a74e5 779
2a8626a9 780 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
025ffe88 781 return (ssp_clk / (2 * rate) - 1) & 0xff;
2f1a74e5 782 else
025ffe88 783 return (ssp_clk / rate - 1) & 0xfff;
2f1a74e5 784}
785
e5262d05
WC
786static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
787 struct chip_data *chip, int rate)
788{
025ffe88 789 unsigned int clk_div;
e5262d05
WC
790
791 switch (drv_data->ssp_type) {
792 case QUARK_X1000_SSP:
9df461ec 793 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
e5262d05 794 default:
025ffe88 795 clk_div = ssp_get_clk_div(drv_data, rate);
e5262d05 796 }
025ffe88 797 return clk_div << 8;
e5262d05
WC
798}
799
e0c9905e
SS
800static void pump_transfers(unsigned long data)
801{
802 struct driver_data *drv_data = (struct driver_data *)data;
803 struct spi_message *message = NULL;
804 struct spi_transfer *transfer = NULL;
805 struct spi_transfer *previous = NULL;
806 struct chip_data *chip = NULL;
9708c121
SS
807 u32 clk_div = 0;
808 u8 bits = 0;
809 u32 speed = 0;
810 u32 cr0;
8d94cc50
SS
811 u32 cr1;
812 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
813 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
4fdb2424 814 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
e0c9905e
SS
815
816 /* Get current state information */
817 message = drv_data->cur_msg;
818 transfer = drv_data->cur_transfer;
819 chip = drv_data->cur_chip;
820
821 /* Handle for abort */
822 if (message->state == ERROR_STATE) {
823 message->status = -EIO;
5daa3ba0 824 giveback(drv_data);
e0c9905e
SS
825 return;
826 }
827
828 /* Handle end of message */
829 if (message->state == DONE_STATE) {
830 message->status = 0;
5daa3ba0 831 giveback(drv_data);
e0c9905e
SS
832 return;
833 }
834
8423597d 835 /* Delay if requested at end of transfer before CS change */
e0c9905e
SS
836 if (message->state == RUNNING_STATE) {
837 previous = list_entry(transfer->transfer_list.prev,
838 struct spi_transfer,
839 transfer_list);
840 if (previous->delay_usecs)
841 udelay(previous->delay_usecs);
8423597d
NF
842
843 /* Drop chip select only if cs_change is requested */
844 if (previous->cs_change)
a7bb3909 845 cs_deassert(drv_data);
e0c9905e
SS
846 }
847
cd7bed00
MW
848 /* Check if we can DMA this transfer */
849 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
7e964455
NF
850
851 /* reject already-mapped transfers; PIO won't always work */
852 if (message->is_dma_mapped
853 || transfer->rx_dma || transfer->tx_dma) {
854 dev_err(&drv_data->pdev->dev,
f6bd03a7
JN
855 "pump_transfers: mapped transfer length of "
856 "%u is greater than %d\n",
7e964455
NF
857 transfer->len, MAX_DMA_LEN);
858 message->status = -EINVAL;
859 giveback(drv_data);
860 return;
861 }
862
863 /* warn ... we force this to PIO mode */
f6bd03a7
JN
864 dev_warn_ratelimited(&message->spi->dev,
865 "pump_transfers: DMA disabled for transfer length %ld "
866 "greater than %d\n",
867 (long)drv_data->len, MAX_DMA_LEN);
8d94cc50
SS
868 }
869
e0c9905e 870 /* Setup the transfer state based on the type of transfer */
cd7bed00 871 if (pxa2xx_spi_flush(drv_data) == 0) {
e0c9905e
SS
872 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
873 message->status = -EIO;
5daa3ba0 874 giveback(drv_data);
e0c9905e
SS
875 return;
876 }
9708c121 877 drv_data->n_bytes = chip->n_bytes;
e0c9905e
SS
878 drv_data->tx = (void *)transfer->tx_buf;
879 drv_data->tx_end = drv_data->tx + transfer->len;
880 drv_data->rx = transfer->rx_buf;
881 drv_data->rx_end = drv_data->rx + transfer->len;
882 drv_data->rx_dma = transfer->rx_dma;
883 drv_data->tx_dma = transfer->tx_dma;
cd7bed00 884 drv_data->len = transfer->len;
e0c9905e
SS
885 drv_data->write = drv_data->tx ? chip->write : null_writer;
886 drv_data->read = drv_data->rx ? chip->read : null_reader;
9708c121
SS
887
888 /* Change speed and bit per word on a per transfer */
8d94cc50 889 cr0 = chip->cr0;
9708c121
SS
890 if (transfer->speed_hz || transfer->bits_per_word) {
891
9708c121
SS
892 bits = chip->bits_per_word;
893 speed = chip->speed_hz;
894
895 if (transfer->speed_hz)
896 speed = transfer->speed_hz;
897
898 if (transfer->bits_per_word)
899 bits = transfer->bits_per_word;
900
e5262d05 901 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
9708c121
SS
902
903 if (bits <= 8) {
904 drv_data->n_bytes = 1;
9708c121
SS
905 drv_data->read = drv_data->read != null_reader ?
906 u8_reader : null_reader;
907 drv_data->write = drv_data->write != null_writer ?
908 u8_writer : null_writer;
909 } else if (bits <= 16) {
910 drv_data->n_bytes = 2;
9708c121
SS
911 drv_data->read = drv_data->read != null_reader ?
912 u16_reader : null_reader;
913 drv_data->write = drv_data->write != null_writer ?
914 u16_writer : null_writer;
915 } else if (bits <= 32) {
916 drv_data->n_bytes = 4;
9708c121
SS
917 drv_data->read = drv_data->read != null_reader ?
918 u32_reader : null_reader;
919 drv_data->write = drv_data->write != null_writer ?
920 u32_writer : null_writer;
921 }
8d94cc50
SS
922 /* if bits/word is changed in dma mode, then must check the
923 * thresholds and burst also */
924 if (chip->enable_dma) {
cd7bed00
MW
925 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
926 message->spi,
8d94cc50
SS
927 bits, &dma_burst,
928 &dma_thresh))
f6bd03a7
JN
929 dev_warn_ratelimited(&message->spi->dev,
930 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
8d94cc50 931 }
9708c121 932
4fdb2424 933 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
9708c121
SS
934 }
935
e0c9905e
SS
936 message->state = RUNNING_STATE;
937
7e964455 938 drv_data->dma_mapped = 0;
cd7bed00
MW
939 if (pxa2xx_spi_dma_is_possible(drv_data->len))
940 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
7e964455 941 if (drv_data->dma_mapped) {
e0c9905e
SS
942
943 /* Ensure we have the correct interrupt handler */
cd7bed00
MW
944 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
945
946 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
e0c9905e 947
8d94cc50
SS
948 /* Clear status and start DMA engine */
949 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
c039dd27 950 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
cd7bed00
MW
951
952 pxa2xx_spi_dma_start(drv_data);
e0c9905e
SS
953 } else {
954 /* Ensure we have the correct interrupt handler */
955 drv_data->transfer_handler = interrupt_transfer;
956
8d94cc50
SS
957 /* Clear status */
958 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
2a8626a9 959 write_SSSR_CS(drv_data, drv_data->clear_sr);
8d94cc50
SS
960 }
961
a0d2642e 962 if (is_lpss_ssp(drv_data)) {
c039dd27
JN
963 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
964 != chip->lpss_rx_threshold)
965 pxa2xx_spi_write(drv_data, SSIRF,
966 chip->lpss_rx_threshold);
967 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
968 != chip->lpss_tx_threshold)
969 pxa2xx_spi_write(drv_data, SSITF,
970 chip->lpss_tx_threshold);
a0d2642e
MW
971 }
972
e5262d05 973 if (is_quark_x1000_ssp(drv_data) &&
c039dd27
JN
974 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
975 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
e5262d05 976
8d94cc50 977 /* see if we need to reload the config registers */
c039dd27
JN
978 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
979 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
980 != (cr1 & change_mask)) {
b97c74bd 981 /* stop the SSP, and update the other bits */
c039dd27 982 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
2a8626a9 983 if (!pxa25x_ssp_comp(drv_data))
c039dd27 984 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
b97c74bd 985 /* first set CR1 without interrupt and service enables */
c039dd27 986 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
b97c74bd 987 /* restart the SSP */
c039dd27 988 pxa2xx_spi_write(drv_data, SSCR0, cr0);
b97c74bd 989
8d94cc50 990 } else {
2a8626a9 991 if (!pxa25x_ssp_comp(drv_data))
c039dd27 992 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
e0c9905e 993 }
b97c74bd 994
a7bb3909 995 cs_assert(drv_data);
b97c74bd
NF
996
997 /* after chip select, release the data by enabling service
998 * requests and interrupts, without changing any mode bits */
c039dd27 999 pxa2xx_spi_write(drv_data, SSCR1, cr1);
e0c9905e
SS
1000}
1001
7f86bde9
MW
1002static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
1003 struct spi_message *msg)
e0c9905e 1004{
7f86bde9 1005 struct driver_data *drv_data = spi_master_get_devdata(master);
e0c9905e 1006
7f86bde9 1007 drv_data->cur_msg = msg;
e0c9905e
SS
1008 /* Initial message state*/
1009 drv_data->cur_msg->state = START_STATE;
1010 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1011 struct spi_transfer,
1012 transfer_list);
1013
8d94cc50
SS
1014 /* prepare to setup the SSP, in pump_transfers, using the per
1015 * chip configuration */
e0c9905e 1016 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
e0c9905e
SS
1017
1018 /* Mark as busy and launch transfers */
1019 tasklet_schedule(&drv_data->pump_transfers);
e0c9905e
SS
1020 return 0;
1021}
1022
7d94a505
MW
1023static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
1024{
1025 struct driver_data *drv_data = spi_master_get_devdata(master);
1026
1027 /* Disable the SSP now */
c039dd27
JN
1028 pxa2xx_spi_write(drv_data, SSCR0,
1029 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
7d94a505 1030
7d94a505
MW
1031 return 0;
1032}
1033
a7bb3909
EM
1034static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1035 struct pxa2xx_spi_chip *chip_info)
1036{
1037 int err = 0;
1038
1039 if (chip == NULL || chip_info == NULL)
1040 return 0;
1041
1042 /* NOTE: setup() can be called multiple times, possibly with
1043 * different chip_info, release previously requested GPIO
1044 */
1045 if (gpio_is_valid(chip->gpio_cs))
1046 gpio_free(chip->gpio_cs);
1047
1048 /* If (*cs_control) is provided, ignore GPIO chip select */
1049 if (chip_info->cs_control) {
1050 chip->cs_control = chip_info->cs_control;
1051 return 0;
1052 }
1053
1054 if (gpio_is_valid(chip_info->gpio_cs)) {
1055 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1056 if (err) {
f6bd03a7
JN
1057 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1058 chip_info->gpio_cs);
a7bb3909
EM
1059 return err;
1060 }
1061
1062 chip->gpio_cs = chip_info->gpio_cs;
1063 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1064
1065 err = gpio_direction_output(chip->gpio_cs,
1066 !chip->gpio_cs_inverted);
1067 }
1068
1069 return err;
1070}
1071
e0c9905e
SS
1072static int setup(struct spi_device *spi)
1073{
1074 struct pxa2xx_spi_chip *chip_info = NULL;
1075 struct chip_data *chip;
1076 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1077 unsigned int clk_div;
a0d2642e
MW
1078 uint tx_thres, tx_hi_thres, rx_thres;
1079
e5262d05
WC
1080 switch (drv_data->ssp_type) {
1081 case QUARK_X1000_SSP:
1082 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1083 tx_hi_thres = 0;
1084 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1085 break;
1086 case LPSS_SSP:
a0d2642e
MW
1087 tx_thres = LPSS_TX_LOTHRESH_DFLT;
1088 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
1089 rx_thres = LPSS_RX_THRESH_DFLT;
e5262d05
WC
1090 break;
1091 default:
a0d2642e
MW
1092 tx_thres = TX_THRESH_DFLT;
1093 tx_hi_thres = 0;
1094 rx_thres = RX_THRESH_DFLT;
e5262d05 1095 break;
a0d2642e 1096 }
e0c9905e 1097
8d94cc50 1098 /* Only alloc on first setup */
e0c9905e 1099 chip = spi_get_ctldata(spi);
8d94cc50 1100 if (!chip) {
e0c9905e 1101 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
9deae459 1102 if (!chip)
e0c9905e
SS
1103 return -ENOMEM;
1104
2a8626a9
SAS
1105 if (drv_data->ssp_type == CE4100_SSP) {
1106 if (spi->chip_select > 4) {
f6bd03a7
JN
1107 dev_err(&spi->dev,
1108 "failed setup: cs number must not be > 4.\n");
2a8626a9
SAS
1109 kfree(chip);
1110 return -EINVAL;
1111 }
1112
1113 chip->frm = spi->chip_select;
1114 } else
1115 chip->gpio_cs = -1;
e0c9905e 1116 chip->enable_dma = 0;
f1f640a9 1117 chip->timeout = TIMOUT_DFLT;
e0c9905e
SS
1118 }
1119
8d94cc50
SS
1120 /* protocol drivers may change the chip settings, so...
1121 * if chip_info exists, use it */
1122 chip_info = spi->controller_data;
1123
e0c9905e 1124 /* chip_info isn't always needed */
8d94cc50 1125 chip->cr1 = 0;
e0c9905e 1126 if (chip_info) {
f1f640a9
VS
1127 if (chip_info->timeout)
1128 chip->timeout = chip_info->timeout;
1129 if (chip_info->tx_threshold)
1130 tx_thres = chip_info->tx_threshold;
a0d2642e
MW
1131 if (chip_info->tx_hi_threshold)
1132 tx_hi_thres = chip_info->tx_hi_threshold;
f1f640a9
VS
1133 if (chip_info->rx_threshold)
1134 rx_thres = chip_info->rx_threshold;
1135 chip->enable_dma = drv_data->master_info->enable_dma;
e0c9905e 1136 chip->dma_threshold = 0;
e0c9905e
SS
1137 if (chip_info->enable_loopback)
1138 chip->cr1 = SSCR1_LBM;
a3496855
MW
1139 } else if (ACPI_HANDLE(&spi->dev)) {
1140 /*
1141 * Slave devices enumerated from ACPI namespace don't
1142 * usually have chip_info but we still might want to use
1143 * DMA with them.
1144 */
1145 chip->enable_dma = drv_data->master_info->enable_dma;
e0c9905e
SS
1146 }
1147
a0d2642e
MW
1148 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1149 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1150 | SSITF_TxHiThresh(tx_hi_thres);
1151
8d94cc50
SS
1152 /* set dma burst and threshold outside of chip_info path so that if
1153 * chip_info goes away after setting chip->enable_dma, the
1154 * burst and threshold can still respond to changes in bits_per_word */
1155 if (chip->enable_dma) {
1156 /* set up legal burst and threshold for dma */
cd7bed00
MW
1157 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1158 spi->bits_per_word,
8d94cc50
SS
1159 &chip->dma_burst_size,
1160 &chip->dma_threshold)) {
f6bd03a7
JN
1161 dev_warn(&spi->dev,
1162 "in setup: DMA burst size reduced to match bits_per_word\n");
8d94cc50
SS
1163 }
1164 }
1165
e5262d05 1166 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
9708c121 1167 chip->speed_hz = spi->max_speed_hz;
e0c9905e 1168
4fdb2424
WC
1169 chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
1170 spi->bits_per_word);
e5262d05
WC
1171 switch (drv_data->ssp_type) {
1172 case QUARK_X1000_SSP:
1173 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1174 & QUARK_X1000_SSCR1_RFT)
1175 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1176 & QUARK_X1000_SSCR1_TFT);
1177 break;
1178 default:
1179 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1180 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1181 break;
1182 }
1183
7f6ee1ad
JC
1184 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1185 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1186 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
e0c9905e 1187
b833172f
MW
1188 if (spi->mode & SPI_LOOP)
1189 chip->cr1 |= SSCR1_LBM;
1190
e0c9905e 1191 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
2a8626a9 1192 if (!pxa25x_ssp_comp(drv_data))
7d077197 1193 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
3343b7a6 1194 drv_data->max_clk_rate
c9840daa
EM
1195 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
1196 chip->enable_dma ? "DMA" : "PIO");
e0c9905e 1197 else
7d077197 1198 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
3343b7a6 1199 drv_data->max_clk_rate / 2
c9840daa
EM
1200 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1201 chip->enable_dma ? "DMA" : "PIO");
e0c9905e
SS
1202
1203 if (spi->bits_per_word <= 8) {
1204 chip->n_bytes = 1;
e0c9905e
SS
1205 chip->read = u8_reader;
1206 chip->write = u8_writer;
1207 } else if (spi->bits_per_word <= 16) {
1208 chip->n_bytes = 2;
e0c9905e
SS
1209 chip->read = u16_reader;
1210 chip->write = u16_writer;
1211 } else if (spi->bits_per_word <= 32) {
e5262d05
WC
1212 if (!is_quark_x1000_ssp(drv_data))
1213 chip->cr0 |= SSCR0_EDSS;
e0c9905e 1214 chip->n_bytes = 4;
e0c9905e
SS
1215 chip->read = u32_reader;
1216 chip->write = u32_writer;
e0c9905e 1217 }
9708c121 1218 chip->bits_per_word = spi->bits_per_word;
e0c9905e
SS
1219
1220 spi_set_ctldata(spi, chip);
1221
2a8626a9
SAS
1222 if (drv_data->ssp_type == CE4100_SSP)
1223 return 0;
1224
a7bb3909 1225 return setup_cs(spi, chip, chip_info);
e0c9905e
SS
1226}
1227
0ffa0285 1228static void cleanup(struct spi_device *spi)
e0c9905e 1229{
0ffa0285 1230 struct chip_data *chip = spi_get_ctldata(spi);
2a8626a9 1231 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
e0c9905e 1232
7348d82a
DR
1233 if (!chip)
1234 return;
1235
2a8626a9 1236 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
a7bb3909
EM
1237 gpio_free(chip->gpio_cs);
1238
e0c9905e
SS
1239 kfree(chip);
1240}
1241
a3496855 1242#ifdef CONFIG_ACPI
a3496855
MW
1243static struct pxa2xx_spi_master *
1244pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1245{
1246 struct pxa2xx_spi_master *pdata;
a3496855
MW
1247 struct acpi_device *adev;
1248 struct ssp_device *ssp;
1249 struct resource *res;
1250 int devid;
1251
1252 if (!ACPI_HANDLE(&pdev->dev) ||
1253 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1254 return NULL;
1255
cc0ee987 1256 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
9deae459 1257 if (!pdata)
a3496855 1258 return NULL;
a3496855
MW
1259
1260 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1261 if (!res)
1262 return NULL;
1263
1264 ssp = &pdata->ssp;
1265
1266 ssp->phys_base = res->start;
cbfd6a21
SK
1267 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1268 if (IS_ERR(ssp->mmio_base))
6dc81f6f 1269 return NULL;
a3496855
MW
1270
1271 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1272 ssp->irq = platform_get_irq(pdev, 0);
1273 ssp->type = LPSS_SSP;
1274 ssp->pdev = pdev;
1275
1276 ssp->port_id = -1;
1277 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1278 ssp->port_id = devid;
1279
1280 pdata->num_chipselect = 1;
cddb339b 1281 pdata->enable_dma = true;
a3496855
MW
1282
1283 return pdata;
1284}
1285
1286static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1287 { "INT33C0", 0 },
1288 { "INT33C1", 0 },
54acbd96
MW
1289 { "INT3430", 0 },
1290 { "INT3431", 0 },
4b30f2a1 1291 { "80860F0E", 0 },
aca26364 1292 { "8086228E", 0 },
a3496855
MW
1293 { },
1294};
1295MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1296#else
1297static inline struct pxa2xx_spi_master *
1298pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1299{
1300 return NULL;
1301}
1302#endif
1303
fd4a319b 1304static int pxa2xx_spi_probe(struct platform_device *pdev)
e0c9905e
SS
1305{
1306 struct device *dev = &pdev->dev;
1307 struct pxa2xx_spi_master *platform_info;
1308 struct spi_master *master;
65a00a20 1309 struct driver_data *drv_data;
2f1a74e5 1310 struct ssp_device *ssp;
65a00a20 1311 int status;
c039dd27 1312 u32 tmp;
e0c9905e 1313
851bacf5
MW
1314 platform_info = dev_get_platdata(dev);
1315 if (!platform_info) {
a3496855
MW
1316 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1317 if (!platform_info) {
1318 dev_err(&pdev->dev, "missing platform data\n");
1319 return -ENODEV;
1320 }
851bacf5 1321 }
e0c9905e 1322
baffe169 1323 ssp = pxa_ssp_request(pdev->id, pdev->name);
851bacf5
MW
1324 if (!ssp)
1325 ssp = &platform_info->ssp;
1326
1327 if (!ssp->mmio_base) {
1328 dev_err(&pdev->dev, "failed to get ssp\n");
e0c9905e
SS
1329 return -ENODEV;
1330 }
1331
1332 /* Allocate master with space for drv_data and null dma buffer */
1333 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1334 if (!master) {
65a00a20 1335 dev_err(&pdev->dev, "cannot alloc spi_master\n");
baffe169 1336 pxa_ssp_free(ssp);
e0c9905e
SS
1337 return -ENOMEM;
1338 }
1339 drv_data = spi_master_get_devdata(master);
1340 drv_data->master = master;
1341 drv_data->master_info = platform_info;
1342 drv_data->pdev = pdev;
2f1a74e5 1343 drv_data->ssp = ssp;
e0c9905e 1344
21486af0 1345 master->dev.parent = &pdev->dev;
21486af0 1346 master->dev.of_node = pdev->dev.of_node;
e7db06b5 1347 /* the spi->mode bits understood by this driver: */
b833172f 1348 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
e7db06b5 1349
851bacf5 1350 master->bus_num = ssp->port_id;
e0c9905e 1351 master->num_chipselect = platform_info->num_chipselect;
7ad0ba91 1352 master->dma_alignment = DMA_ALIGNMENT;
e0c9905e
SS
1353 master->cleanup = cleanup;
1354 master->setup = setup;
7f86bde9 1355 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
7d94a505 1356 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
7dd62787 1357 master->auto_runtime_pm = true;
e0c9905e 1358
2f1a74e5 1359 drv_data->ssp_type = ssp->type;
2b9b84f4 1360 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
e0c9905e 1361
2f1a74e5 1362 drv_data->ioaddr = ssp->mmio_base;
1363 drv_data->ssdr_physical = ssp->phys_base + SSDR;
2a8626a9 1364 if (pxa25x_ssp_comp(drv_data)) {
e5262d05
WC
1365 switch (drv_data->ssp_type) {
1366 case QUARK_X1000_SSP:
1367 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1368 break;
1369 default:
1370 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1371 break;
1372 }
1373
e0c9905e
SS
1374 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1375 drv_data->dma_cr1 = 0;
1376 drv_data->clear_sr = SSSR_ROR;
1377 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1378 } else {
24778be2 1379 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
e0c9905e 1380 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
5928808e 1381 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
e0c9905e
SS
1382 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1383 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1384 }
1385
49cbb1e0
SAS
1386 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1387 drv_data);
e0c9905e 1388 if (status < 0) {
65a00a20 1389 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
e0c9905e
SS
1390 goto out_error_master_alloc;
1391 }
1392
1393 /* Setup DMA if requested */
1394 drv_data->tx_channel = -1;
1395 drv_data->rx_channel = -1;
1396 if (platform_info->enable_dma) {
cd7bed00
MW
1397 status = pxa2xx_spi_dma_setup(drv_data);
1398 if (status) {
cddb339b 1399 dev_dbg(dev, "no DMA channels available, using PIO\n");
cd7bed00 1400 platform_info->enable_dma = false;
e0c9905e 1401 }
e0c9905e
SS
1402 }
1403
1404 /* Enable SOC clock */
3343b7a6
MW
1405 clk_prepare_enable(ssp->clk);
1406
1407 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
e0c9905e
SS
1408
1409 /* Load default SSP configuration */
c039dd27 1410 pxa2xx_spi_write(drv_data, SSCR0, 0);
e5262d05
WC
1411 switch (drv_data->ssp_type) {
1412 case QUARK_X1000_SSP:
c039dd27
JN
1413 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1414 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1415 pxa2xx_spi_write(drv_data, SSCR1, tmp);
e5262d05
WC
1416
1417 /* using the Motorola SPI protocol and use 8 bit frame */
c039dd27
JN
1418 pxa2xx_spi_write(drv_data, SSCR0,
1419 QUARK_X1000_SSCR0_Motorola
1420 | QUARK_X1000_SSCR0_DataSize(8));
e5262d05
WC
1421 break;
1422 default:
c039dd27
JN
1423 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1424 SSCR1_TxTresh(TX_THRESH_DFLT);
1425 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1426 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1427 pxa2xx_spi_write(drv_data, SSCR0, tmp);
e5262d05
WC
1428 break;
1429 }
1430
2a8626a9 1431 if (!pxa25x_ssp_comp(drv_data))
c039dd27 1432 pxa2xx_spi_write(drv_data, SSTO, 0);
e5262d05
WC
1433
1434 if (!is_quark_x1000_ssp(drv_data))
c039dd27 1435 pxa2xx_spi_write(drv_data, SSPSP, 0);
e0c9905e 1436
7566bcc7
JN
1437 if (is_lpss_ssp(drv_data))
1438 lpss_ssp_setup(drv_data);
a0d2642e 1439
7f86bde9
MW
1440 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1441 (unsigned long)drv_data);
e0c9905e 1442
836d1a22
AO
1443 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1444 pm_runtime_use_autosuspend(&pdev->dev);
1445 pm_runtime_set_active(&pdev->dev);
1446 pm_runtime_enable(&pdev->dev);
1447
e0c9905e
SS
1448 /* Register with the SPI framework */
1449 platform_set_drvdata(pdev, drv_data);
a807fcd0 1450 status = devm_spi_register_master(&pdev->dev, master);
e0c9905e
SS
1451 if (status != 0) {
1452 dev_err(&pdev->dev, "problem registering spi master\n");
7f86bde9 1453 goto out_error_clock_enabled;
e0c9905e
SS
1454 }
1455
1456 return status;
1457
e0c9905e 1458out_error_clock_enabled:
3343b7a6 1459 clk_disable_unprepare(ssp->clk);
cd7bed00 1460 pxa2xx_spi_dma_release(drv_data);
2f1a74e5 1461 free_irq(ssp->irq, drv_data);
e0c9905e
SS
1462
1463out_error_master_alloc:
1464 spi_master_put(master);
baffe169 1465 pxa_ssp_free(ssp);
e0c9905e
SS
1466 return status;
1467}
1468
1469static int pxa2xx_spi_remove(struct platform_device *pdev)
1470{
1471 struct driver_data *drv_data = platform_get_drvdata(pdev);
51e911e2 1472 struct ssp_device *ssp;
e0c9905e
SS
1473
1474 if (!drv_data)
1475 return 0;
51e911e2 1476 ssp = drv_data->ssp;
e0c9905e 1477
7d94a505
MW
1478 pm_runtime_get_sync(&pdev->dev);
1479
e0c9905e 1480 /* Disable the SSP at the peripheral and SOC level */
c039dd27 1481 pxa2xx_spi_write(drv_data, SSCR0, 0);
3343b7a6 1482 clk_disable_unprepare(ssp->clk);
e0c9905e
SS
1483
1484 /* Release DMA */
cd7bed00
MW
1485 if (drv_data->master_info->enable_dma)
1486 pxa2xx_spi_dma_release(drv_data);
e0c9905e 1487
7d94a505
MW
1488 pm_runtime_put_noidle(&pdev->dev);
1489 pm_runtime_disable(&pdev->dev);
1490
e0c9905e 1491 /* Release IRQ */
2f1a74e5 1492 free_irq(ssp->irq, drv_data);
1493
1494 /* Release SSP */
baffe169 1495 pxa_ssp_free(ssp);
e0c9905e 1496
e0c9905e
SS
1497 return 0;
1498}
1499
1500static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1501{
1502 int status = 0;
1503
1504 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1505 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1506}
1507
382cebb0 1508#ifdef CONFIG_PM_SLEEP
86d2593a 1509static int pxa2xx_spi_suspend(struct device *dev)
e0c9905e 1510{
86d2593a 1511 struct driver_data *drv_data = dev_get_drvdata(dev);
2f1a74e5 1512 struct ssp_device *ssp = drv_data->ssp;
e0c9905e
SS
1513 int status = 0;
1514
7f86bde9 1515 status = spi_master_suspend(drv_data->master);
e0c9905e
SS
1516 if (status != 0)
1517 return status;
c039dd27 1518 pxa2xx_spi_write(drv_data, SSCR0, 0);
2b9375b9
DES
1519
1520 if (!pm_runtime_suspended(dev))
1521 clk_disable_unprepare(ssp->clk);
e0c9905e
SS
1522
1523 return 0;
1524}
1525
86d2593a 1526static int pxa2xx_spi_resume(struct device *dev)
e0c9905e 1527{
86d2593a 1528 struct driver_data *drv_data = dev_get_drvdata(dev);
2f1a74e5 1529 struct ssp_device *ssp = drv_data->ssp;
e0c9905e
SS
1530 int status = 0;
1531
cd7bed00 1532 pxa2xx_spi_dma_resume(drv_data);
148da331 1533
e0c9905e 1534 /* Enable the SSP clock */
2b9375b9
DES
1535 if (!pm_runtime_suspended(dev))
1536 clk_prepare_enable(ssp->clk);
e0c9905e 1537
c50325f7 1538 /* Restore LPSS private register bits */
48421adf
JN
1539 if (is_lpss_ssp(drv_data))
1540 lpss_ssp_setup(drv_data);
c50325f7 1541
e0c9905e 1542 /* Start the queue running */
7f86bde9 1543 status = spi_master_resume(drv_data->master);
e0c9905e 1544 if (status != 0) {
86d2593a 1545 dev_err(dev, "problem starting queue (%d)\n", status);
e0c9905e
SS
1546 return status;
1547 }
1548
1549 return 0;
1550}
7d94a505
MW
1551#endif
1552
ec833050 1553#ifdef CONFIG_PM
7d94a505
MW
1554static int pxa2xx_spi_runtime_suspend(struct device *dev)
1555{
1556 struct driver_data *drv_data = dev_get_drvdata(dev);
1557
1558 clk_disable_unprepare(drv_data->ssp->clk);
1559 return 0;
1560}
1561
1562static int pxa2xx_spi_runtime_resume(struct device *dev)
1563{
1564 struct driver_data *drv_data = dev_get_drvdata(dev);
1565
1566 clk_prepare_enable(drv_data->ssp->clk);
1567 return 0;
1568}
1569#endif
86d2593a 1570
47145210 1571static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
7d94a505
MW
1572 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1573 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1574 pxa2xx_spi_runtime_resume, NULL)
86d2593a 1575};
e0c9905e
SS
1576
1577static struct platform_driver driver = {
1578 .driver = {
86d2593a 1579 .name = "pxa2xx-spi",
86d2593a 1580 .pm = &pxa2xx_spi_pm_ops,
a3496855 1581 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
e0c9905e 1582 },
fbd29a14 1583 .probe = pxa2xx_spi_probe,
d1e44d9c 1584 .remove = pxa2xx_spi_remove,
e0c9905e 1585 .shutdown = pxa2xx_spi_shutdown,
e0c9905e
SS
1586};
1587
1588static int __init pxa2xx_spi_init(void)
1589{
fbd29a14 1590 return platform_driver_register(&driver);
e0c9905e 1591}
5b61a749 1592subsys_initcall(pxa2xx_spi_init);
e0c9905e
SS
1593
1594static void __exit pxa2xx_spi_exit(void)
1595{
1596 platform_driver_unregister(&driver);
1597}
1598module_exit(pxa2xx_spi_exit);
This page took 1.019525 seconds and 5 git commands to generate.