blob: 451a837e5613ca6b53f328b9f87a7fd61c38b985 [file] [log] [blame]
Chao Fu349ad662013-08-16 11:08:55 +08001/*
2 * drivers/spi/spi-fsl-dspi.c
3 *
4 * Copyright 2013 Freescale Semiconductor, Inc.
5 *
6 * Freescale DSPI driver
7 * This file contains a driver for the Freescale DSPI
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
Xiubo Lia3108362014-09-29 10:57:06 +080016#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/errno.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
Chao Fu349ad662013-08-16 11:08:55 +080022#include <linux/kernel.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070023#include <linux/math64.h>
Chao Fu349ad662013-08-16 11:08:55 +080024#include <linux/module.h>
Chao Fu349ad662013-08-16 11:08:55 +080025#include <linux/of.h>
26#include <linux/of_device.h>
Xiubo Lia3108362014-09-29 10:57:06 +080027#include <linux/platform_device.h>
28#include <linux/pm_runtime.h>
29#include <linux/regmap.h>
30#include <linux/sched.h>
31#include <linux/spi/spi.h>
32#include <linux/spi/spi_bitbang.h>
Aaron Brice95bf15f2015-04-03 13:39:31 -070033#include <linux/time.h>
Chao Fu349ad662013-08-16 11:08:55 +080034
35#define DRIVER_NAME "fsl-dspi"
36
37#define TRAN_STATE_RX_VOID 0x01
38#define TRAN_STATE_TX_VOID 0x02
39#define TRAN_STATE_WORD_ODD_NUM 0x04
40
41#define DSPI_FIFO_SIZE 4
42
43#define SPI_MCR 0x00
44#define SPI_MCR_MASTER (1 << 31)
45#define SPI_MCR_PCSIS (0x3F << 16)
46#define SPI_MCR_CLR_TXF (1 << 11)
47#define SPI_MCR_CLR_RXF (1 << 10)
48
49#define SPI_TCR 0x08
Haikun Wangc042af92015-06-09 19:45:37 +080050#define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
Chao Fu349ad662013-08-16 11:08:55 +080051
Alexander Stein5cc7b042014-11-04 09:20:18 +010052#define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
Chao Fu349ad662013-08-16 11:08:55 +080053#define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
54#define SPI_CTAR_CPOL(x) ((x) << 26)
55#define SPI_CTAR_CPHA(x) ((x) << 25)
56#define SPI_CTAR_LSBFE(x) ((x) << 24)
Aaron Brice95bf15f2015-04-03 13:39:31 -070057#define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
Chao Fu349ad662013-08-16 11:08:55 +080058#define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
59#define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
60#define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
61#define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
62#define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
63#define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
64#define SPI_CTAR_BR(x) ((x) & 0x0000000f)
Aaron Brice95bf15f2015-04-03 13:39:31 -070065#define SPI_CTAR_SCALE_BITS 0xf
Chao Fu349ad662013-08-16 11:08:55 +080066
67#define SPI_CTAR0_SLAVE 0x0c
68
69#define SPI_SR 0x2c
70#define SPI_SR_EOQF 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080071#define SPI_SR_TCFQF 0x80000000
Chao Fu349ad662013-08-16 11:08:55 +080072
73#define SPI_RSER 0x30
74#define SPI_RSER_EOQFE 0x10000000
Haikun Wangd1f4a382015-06-09 19:45:27 +080075#define SPI_RSER_TCFQE 0x80000000
Chao Fu349ad662013-08-16 11:08:55 +080076
77#define SPI_PUSHR 0x34
78#define SPI_PUSHR_CONT (1 << 31)
Alexander Stein5cc7b042014-11-04 09:20:18 +010079#define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
Chao Fu349ad662013-08-16 11:08:55 +080080#define SPI_PUSHR_EOQ (1 << 27)
81#define SPI_PUSHR_CTCNT (1 << 26)
82#define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
83#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
84
85#define SPI_PUSHR_SLAVE 0x34
86
87#define SPI_POPR 0x38
88#define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
89
90#define SPI_TXFR0 0x3c
91#define SPI_TXFR1 0x40
92#define SPI_TXFR2 0x44
93#define SPI_TXFR3 0x48
94#define SPI_RXFR0 0x7c
95#define SPI_RXFR1 0x80
96#define SPI_RXFR2 0x84
97#define SPI_RXFR3 0x88
98
99#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
100#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
101#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
102#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
103
104#define SPI_CS_INIT 0x01
105#define SPI_CS_ASSERT 0x02
106#define SPI_CS_DROP 0x04
107
Haikun Wangc042af92015-06-09 19:45:37 +0800108#define SPI_TCR_TCNT_MAX 0x10000
109
Chao Fu349ad662013-08-16 11:08:55 +0800110struct chip_data {
111 u32 mcr_val;
112 u32 ctar_val;
113 u16 void_write_data;
114};
115
Haikun Wangd1f4a382015-06-09 19:45:27 +0800116enum dspi_trans_mode {
117 DSPI_EOQ_MODE = 0,
118 DSPI_TCFQ_MODE,
119};
120
121struct fsl_dspi_devtype_data {
122 enum dspi_trans_mode trans_mode;
123};
124
125static const struct fsl_dspi_devtype_data vf610_data = {
126 .trans_mode = DSPI_EOQ_MODE,
127};
128
129static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
130 .trans_mode = DSPI_TCFQ_MODE,
131};
132
133static const struct fsl_dspi_devtype_data ls2085a_data = {
134 .trans_mode = DSPI_TCFQ_MODE,
135};
136
Chao Fu349ad662013-08-16 11:08:55 +0800137struct fsl_dspi {
Chao Fu9298bc72015-01-27 16:27:22 +0530138 struct spi_master *master;
Chao Fu349ad662013-08-16 11:08:55 +0800139 struct platform_device *pdev;
140
Chao Fu1acbdeb2014-02-12 15:29:05 +0800141 struct regmap *regmap;
Chao Fu349ad662013-08-16 11:08:55 +0800142 int irq;
Chao Fu88386e82014-02-12 15:29:06 +0800143 struct clk *clk;
Chao Fu349ad662013-08-16 11:08:55 +0800144
Chao Fu88386e82014-02-12 15:29:06 +0800145 struct spi_transfer *cur_transfer;
Chao Fu9298bc72015-01-27 16:27:22 +0530146 struct spi_message *cur_msg;
Chao Fu349ad662013-08-16 11:08:55 +0800147 struct chip_data *cur_chip;
148 size_t len;
149 void *tx;
150 void *tx_end;
151 void *rx;
152 void *rx_end;
153 char dataflags;
154 u8 cs;
155 u16 void_write_data;
Chao Fu9298bc72015-01-27 16:27:22 +0530156 u32 cs_change;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800157 struct fsl_dspi_devtype_data *devtype_data;
Chao Fu349ad662013-08-16 11:08:55 +0800158
Chao Fu88386e82014-02-12 15:29:06 +0800159 wait_queue_head_t waitq;
160 u32 waitflags;
Haikun Wangc042af92015-06-09 19:45:37 +0800161
162 u32 spi_tcnt;
Chao Fu349ad662013-08-16 11:08:55 +0800163};
164
165static inline int is_double_byte_mode(struct fsl_dspi *dspi)
166{
Chao Fu1acbdeb2014-02-12 15:29:05 +0800167 unsigned int val;
Chao Fu349ad662013-08-16 11:08:55 +0800168
Chao Fu1acbdeb2014-02-12 15:29:05 +0800169 regmap_read(dspi->regmap, SPI_CTAR(dspi->cs), &val);
Chao Fu349ad662013-08-16 11:08:55 +0800170
Chao Fu1acbdeb2014-02-12 15:29:05 +0800171 return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
Chao Fu349ad662013-08-16 11:08:55 +0800172}
173
174static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
175 unsigned long clkrate)
176{
177 /* Valid baud rate pre-scaler values */
178 int pbr_tbl[4] = {2, 3, 5, 7};
179 int brs[16] = { 2, 4, 6, 8,
180 16, 32, 64, 128,
181 256, 512, 1024, 2048,
182 4096, 8192, 16384, 32768 };
Aaron Brice6fd63082015-03-30 10:49:15 -0700183 int scale_needed, scale, minscale = INT_MAX;
184 int i, j;
Chao Fu349ad662013-08-16 11:08:55 +0800185
Aaron Brice6fd63082015-03-30 10:49:15 -0700186 scale_needed = clkrate / speed_hz;
Aaron Bricee689d6d2015-04-03 13:39:29 -0700187 if (clkrate % speed_hz)
188 scale_needed++;
Chao Fu349ad662013-08-16 11:08:55 +0800189
Aaron Brice6fd63082015-03-30 10:49:15 -0700190 for (i = 0; i < ARRAY_SIZE(brs); i++)
191 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
192 scale = brs[i] * pbr_tbl[j];
193 if (scale >= scale_needed) {
194 if (scale < minscale) {
195 minscale = scale;
196 *br = i;
197 *pbr = j;
198 }
199 break;
Chao Fu349ad662013-08-16 11:08:55 +0800200 }
201 }
202
Aaron Brice6fd63082015-03-30 10:49:15 -0700203 if (minscale == INT_MAX) {
204 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
205 speed_hz, clkrate);
206 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
207 *br = ARRAY_SIZE(brs) - 1;
208 }
Chao Fu349ad662013-08-16 11:08:55 +0800209}
210
Aaron Brice95bf15f2015-04-03 13:39:31 -0700211static void ns_delay_scale(char *psc, char *sc, int delay_ns,
212 unsigned long clkrate)
213{
214 int pscale_tbl[4] = {1, 3, 5, 7};
215 int scale_needed, scale, minscale = INT_MAX;
216 int i, j;
217 u32 remainder;
218
219 scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
220 &remainder);
221 if (remainder)
222 scale_needed++;
223
224 for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
225 for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
226 scale = pscale_tbl[i] * (2 << j);
227 if (scale >= scale_needed) {
228 if (scale < minscale) {
229 minscale = scale;
230 *psc = i;
231 *sc = j;
232 }
233 break;
234 }
235 }
236
237 if (minscale == INT_MAX) {
238 pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
239 delay_ns, clkrate);
240 *psc = ARRAY_SIZE(pscale_tbl) - 1;
241 *sc = SPI_CTAR_SCALE_BITS;
242 }
Chao Fu349ad662013-08-16 11:08:55 +0800243}
244
Haikun Wangd1f4a382015-06-09 19:45:27 +0800245static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
246{
247 u16 d16;
248
249 if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
250 d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
251 else
252 d16 = dspi->void_write_data;
253
254 dspi->tx += tx_word + 1;
255 dspi->len -= tx_word + 1;
256
257 return SPI_PUSHR_TXDATA(d16) |
258 SPI_PUSHR_PCS(dspi->cs) |
259 SPI_PUSHR_CTAS(dspi->cs) |
260 SPI_PUSHR_CONT;
261}
262
263static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
264{
265 u16 d;
266 unsigned int val;
267
268 regmap_read(dspi->regmap, SPI_POPR, &val);
269 d = SPI_POPR_RXDATA(val);
270
271 if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
272 rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
273
274 dspi->rx += rx_word + 1;
275}
276
277static int dspi_eoq_write(struct fsl_dspi *dspi)
Chao Fu349ad662013-08-16 11:08:55 +0800278{
279 int tx_count = 0;
280 int tx_word;
Chao Fu349ad662013-08-16 11:08:55 +0800281 u32 dspi_pushr = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800282
283 tx_word = is_double_byte_mode(dspi);
284
Chao Fu349ad662013-08-16 11:08:55 +0800285 while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
Haikun Wangd1f4a382015-06-09 19:45:27 +0800286 /* If we are in word mode, only have a single byte to transfer
287 * switch to byte mode temporarily. Will switch back at the
288 * end of the transfer.
289 */
290 if (tx_word && (dspi->len == 1)) {
291 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
292 regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
293 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
294 tx_word = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800295 }
296
Haikun Wangd1f4a382015-06-09 19:45:27 +0800297 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
298
Chao Fu349ad662013-08-16 11:08:55 +0800299 if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
300 /* last transfer in the transfer */
301 dspi_pushr |= SPI_PUSHR_EOQ;
Chao Fu9298bc72015-01-27 16:27:22 +0530302 if ((dspi->cs_change) && (!dspi->len))
303 dspi_pushr &= ~SPI_PUSHR_CONT;
Chao Fu349ad662013-08-16 11:08:55 +0800304 } else if (tx_word && (dspi->len == 1))
305 dspi_pushr |= SPI_PUSHR_EOQ;
306
Chao Fu1acbdeb2014-02-12 15:29:05 +0800307 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
308
Chao Fu349ad662013-08-16 11:08:55 +0800309 tx_count++;
310 }
311
312 return tx_count * (tx_word + 1);
313}
314
Haikun Wangd1f4a382015-06-09 19:45:27 +0800315static int dspi_eoq_read(struct fsl_dspi *dspi)
Chao Fu349ad662013-08-16 11:08:55 +0800316{
317 int rx_count = 0;
318 int rx_word = is_double_byte_mode(dspi);
Chao Fu9298bc72015-01-27 16:27:22 +0530319
Chao Fu349ad662013-08-16 11:08:55 +0800320 while ((dspi->rx < dspi->rx_end)
321 && (rx_count < DSPI_FIFO_SIZE)) {
Haikun Wangd1f4a382015-06-09 19:45:27 +0800322 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
323 rx_word = 0;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800324
Haikun Wangd1f4a382015-06-09 19:45:27 +0800325 dspi_data_from_popr(dspi, rx_word);
Chao Fu349ad662013-08-16 11:08:55 +0800326 rx_count++;
327 }
328
329 return rx_count;
330}
331
Haikun Wangd1f4a382015-06-09 19:45:27 +0800332static int dspi_tcfq_write(struct fsl_dspi *dspi)
333{
334 int tx_word;
335 u32 dspi_pushr = 0;
336
337 tx_word = is_double_byte_mode(dspi);
338
339 if (tx_word && (dspi->len == 1)) {
340 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
341 regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
342 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
343 tx_word = 0;
344 }
345
346 dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
347
348 if ((dspi->cs_change) && (!dspi->len))
349 dspi_pushr &= ~SPI_PUSHR_CONT;
350
351 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
352
353 return tx_word + 1;
354}
355
356static void dspi_tcfq_read(struct fsl_dspi *dspi)
357{
358 int rx_word = is_double_byte_mode(dspi);
359
360 if (rx_word && (dspi->rx_end - dspi->rx) == 1)
361 rx_word = 0;
362
363 dspi_data_from_popr(dspi, rx_word);
364}
365
Chao Fu9298bc72015-01-27 16:27:22 +0530366static int dspi_transfer_one_message(struct spi_master *master,
367 struct spi_message *message)
Chao Fu349ad662013-08-16 11:08:55 +0800368{
Chao Fu9298bc72015-01-27 16:27:22 +0530369 struct fsl_dspi *dspi = spi_master_get_devdata(master);
370 struct spi_device *spi = message->spi;
371 struct spi_transfer *transfer;
372 int status = 0;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800373 enum dspi_trans_mode trans_mode;
Haikun Wangc042af92015-06-09 19:45:37 +0800374 u32 spi_tcr;
375
376 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
377 dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800378
Chao Fu9298bc72015-01-27 16:27:22 +0530379 message->actual_length = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800380
Chao Fu9298bc72015-01-27 16:27:22 +0530381 list_for_each_entry(transfer, &message->transfers, transfer_list) {
382 dspi->cur_transfer = transfer;
383 dspi->cur_msg = message;
384 dspi->cur_chip = spi_get_ctldata(spi);
385 dspi->cs = spi->chip_select;
Haikun Wang9deef022015-05-13 18:12:15 +0800386 dspi->cs_change = 0;
Chao Fu9298bc72015-01-27 16:27:22 +0530387 if (dspi->cur_transfer->transfer_list.next
388 == &dspi->cur_msg->transfers)
Haikun Wang9deef022015-05-13 18:12:15 +0800389 dspi->cs_change = 1;
Chao Fu9298bc72015-01-27 16:27:22 +0530390 dspi->void_write_data = dspi->cur_chip->void_write_data;
Chao Fu349ad662013-08-16 11:08:55 +0800391
Chao Fu9298bc72015-01-27 16:27:22 +0530392 dspi->dataflags = 0;
393 dspi->tx = (void *)transfer->tx_buf;
394 dspi->tx_end = dspi->tx + transfer->len;
395 dspi->rx = transfer->rx_buf;
396 dspi->rx_end = dspi->rx + transfer->len;
397 dspi->len = transfer->len;
Chao Fu349ad662013-08-16 11:08:55 +0800398
Chao Fu9298bc72015-01-27 16:27:22 +0530399 if (!dspi->rx)
400 dspi->dataflags |= TRAN_STATE_RX_VOID;
Chao Fu349ad662013-08-16 11:08:55 +0800401
Chao Fu9298bc72015-01-27 16:27:22 +0530402 if (!dspi->tx)
403 dspi->dataflags |= TRAN_STATE_TX_VOID;
Chao Fu349ad662013-08-16 11:08:55 +0800404
Chao Fu9298bc72015-01-27 16:27:22 +0530405 regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
406 regmap_update_bits(dspi->regmap, SPI_MCR,
407 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
408 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
Chao Fu1acbdeb2014-02-12 15:29:05 +0800409 regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
410 dspi->cur_chip->ctar_val);
Chao Fu9298bc72015-01-27 16:27:22 +0530411 if (transfer->speed_hz)
412 regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
413 dspi->cur_chip->ctar_val);
Chao Fu349ad662013-08-16 11:08:55 +0800414
Haikun Wangd1f4a382015-06-09 19:45:27 +0800415 trans_mode = dspi->devtype_data->trans_mode;
416 switch (trans_mode) {
417 case DSPI_EOQ_MODE:
418 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
Haikun Wangc042af92015-06-09 19:45:37 +0800419 dspi_eoq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800420 break;
421 case DSPI_TCFQ_MODE:
422 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
Haikun Wangc042af92015-06-09 19:45:37 +0800423 dspi_tcfq_write(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800424 break;
425 default:
426 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
427 trans_mode);
428 status = -EINVAL;
429 goto out;
430 }
Chao Fu349ad662013-08-16 11:08:55 +0800431
Chao Fu9298bc72015-01-27 16:27:22 +0530432 if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
433 dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
434 dspi->waitflags = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800435
Chao Fu9298bc72015-01-27 16:27:22 +0530436 if (transfer->delay_usecs)
437 udelay(transfer->delay_usecs);
Chao Fu349ad662013-08-16 11:08:55 +0800438 }
439
Haikun Wangd1f4a382015-06-09 19:45:27 +0800440out:
Chao Fu9298bc72015-01-27 16:27:22 +0530441 message->status = status;
442 spi_finalize_current_message(master);
443
444 return status;
Chao Fu349ad662013-08-16 11:08:55 +0800445}
446
Chao Fu9298bc72015-01-27 16:27:22 +0530447static int dspi_setup(struct spi_device *spi)
Chao Fu349ad662013-08-16 11:08:55 +0800448{
449 struct chip_data *chip;
450 struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
Aaron Brice95bf15f2015-04-03 13:39:31 -0700451 u32 cs_sck_delay = 0, sck_cs_delay = 0;
452 unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
453 unsigned char pasc = 0, asc = 0, fmsz = 0;
454 unsigned long clkrate;
Chao Fu349ad662013-08-16 11:08:55 +0800455
Bhuvanchandra DVceadfd82015-01-31 22:03:25 +0530456 if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
457 fmsz = spi->bits_per_word - 1;
458 } else {
459 pr_err("Invalid wordsize\n");
460 return -ENODEV;
461 }
462
Chao Fu349ad662013-08-16 11:08:55 +0800463 /* Only alloc on first setup */
464 chip = spi_get_ctldata(spi);
465 if (chip == NULL) {
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530466 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Chao Fu349ad662013-08-16 11:08:55 +0800467 if (!chip)
468 return -ENOMEM;
469 }
470
Aaron Brice95bf15f2015-04-03 13:39:31 -0700471 of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
472 &cs_sck_delay);
473
474 of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
475 &sck_cs_delay);
476
Chao Fu349ad662013-08-16 11:08:55 +0800477 chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
478 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
Chao Fu349ad662013-08-16 11:08:55 +0800479
480 chip->void_write_data = 0;
481
Aaron Brice95bf15f2015-04-03 13:39:31 -0700482 clkrate = clk_get_rate(dspi->clk);
483 hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
484
485 /* Set PCS to SCK delay scale values */
486 ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
487
488 /* Set After SCK delay scale values */
489 ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
Chao Fu349ad662013-08-16 11:08:55 +0800490
491 chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
492 | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
493 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
494 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
Aaron Brice95bf15f2015-04-03 13:39:31 -0700495 | SPI_CTAR_PCSSCK(pcssck)
496 | SPI_CTAR_CSSCK(cssck)
497 | SPI_CTAR_PASC(pasc)
498 | SPI_CTAR_ASC(asc)
Chao Fu349ad662013-08-16 11:08:55 +0800499 | SPI_CTAR_PBR(pbr)
500 | SPI_CTAR_BR(br);
501
502 spi_set_ctldata(spi, chip);
503
504 return 0;
505}
506
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530507static void dspi_cleanup(struct spi_device *spi)
508{
509 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
510
511 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
512 spi->master->bus_num, spi->chip_select);
513
514 kfree(chip);
515}
516
Chao Fu349ad662013-08-16 11:08:55 +0800517static irqreturn_t dspi_interrupt(int irq, void *dev_id)
518{
519 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
Chao Fu9298bc72015-01-27 16:27:22 +0530520 struct spi_message *msg = dspi->cur_msg;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800521 enum dspi_trans_mode trans_mode;
Haikun Wangc042af92015-06-09 19:45:37 +0800522 u32 spi_sr, spi_tcr;
523 u32 spi_tcnt, tcnt_diff;
524 int tx_word;
Chao Fu349ad662013-08-16 11:08:55 +0800525
Haikun Wangd1f4a382015-06-09 19:45:27 +0800526 regmap_read(dspi->regmap, SPI_SR, &spi_sr);
527 regmap_write(dspi->regmap, SPI_SR, spi_sr);
528
Chao Fu349ad662013-08-16 11:08:55 +0800529
Haikun Wangc042af92015-06-09 19:45:37 +0800530 if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
531 tx_word = is_double_byte_mode(dspi);
Chao Fu1acbdeb2014-02-12 15:29:05 +0800532
Haikun Wangc042af92015-06-09 19:45:37 +0800533 regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
534 spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
535 /*
536 * The width of SPI Transfer Counter in SPI_TCR is 16bits,
537 * so the max couner is 65535. When the counter reach 65535,
538 * it will wrap around, counter reset to zero.
539 * spi_tcnt my be less than dspi->spi_tcnt, it means the
540 * counter already wrapped around.
541 * SPI Transfer Counter is a counter of transmitted frames.
542 * The size of frame maybe two bytes.
543 */
544 tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
545 % SPI_TCR_TCNT_MAX;
546 tcnt_diff *= (tx_word + 1);
547 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
548 tcnt_diff--;
549
550 msg->actual_length += tcnt_diff;
551
552 dspi->spi_tcnt = spi_tcnt;
553
554 trans_mode = dspi->devtype_data->trans_mode;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800555 switch (trans_mode) {
556 case DSPI_EOQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800557 dspi_eoq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800558 break;
559 case DSPI_TCFQ_MODE:
Haikun Wangc042af92015-06-09 19:45:37 +0800560 dspi_tcfq_read(dspi);
Haikun Wangd1f4a382015-06-09 19:45:27 +0800561 break;
562 default:
563 dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
564 trans_mode);
Haikun Wangc042af92015-06-09 19:45:37 +0800565 return IRQ_HANDLED;
566 }
567
568 if (!dspi->len) {
569 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
570 regmap_update_bits(dspi->regmap,
571 SPI_CTAR(dspi->cs),
572 SPI_FRAME_BITS_MASK,
573 SPI_FRAME_BITS(16));
574 dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
575 }
576
577 dspi->waitflags = 1;
578 wake_up_interruptible(&dspi->waitq);
579 } else {
580 switch (trans_mode) {
581 case DSPI_EOQ_MODE:
582 dspi_eoq_write(dspi);
583 break;
584 case DSPI_TCFQ_MODE:
585 dspi_tcfq_write(dspi);
586 break;
587 default:
588 dev_err(&dspi->pdev->dev,
589 "unsupported trans_mode %u\n",
590 trans_mode);
591 }
Haikun Wangd1f4a382015-06-09 19:45:27 +0800592 }
593 }
Haikun Wangc042af92015-06-09 19:45:37 +0800594
Chao Fu349ad662013-08-16 11:08:55 +0800595 return IRQ_HANDLED;
596}
597
Jingoo Han790d1902014-05-07 16:45:41 +0900598static const struct of_device_id fsl_dspi_dt_ids[] = {
Haikun Wangd1f4a382015-06-09 19:45:27 +0800599 { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
600 { .compatible = "fsl,ls1021a-v1.0-dspi",
601 .data = (void *)&ls1021a_v1_data, },
602 { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
Chao Fu349ad662013-08-16 11:08:55 +0800603 { /* sentinel */ }
604};
605MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
606
607#ifdef CONFIG_PM_SLEEP
608static int dspi_suspend(struct device *dev)
609{
610 struct spi_master *master = dev_get_drvdata(dev);
611 struct fsl_dspi *dspi = spi_master_get_devdata(master);
612
613 spi_master_suspend(master);
614 clk_disable_unprepare(dspi->clk);
615
616 return 0;
617}
618
619static int dspi_resume(struct device *dev)
620{
Chao Fu349ad662013-08-16 11:08:55 +0800621 struct spi_master *master = dev_get_drvdata(dev);
622 struct fsl_dspi *dspi = spi_master_get_devdata(master);
623
624 clk_prepare_enable(dspi->clk);
625 spi_master_resume(master);
626
627 return 0;
628}
629#endif /* CONFIG_PM_SLEEP */
630
Jingoo Hanba811ad2014-02-26 10:30:14 +0900631static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
Chao Fu349ad662013-08-16 11:08:55 +0800632
Xiubo Li409851c2014-10-09 11:27:45 +0800633static const struct regmap_config dspi_regmap_config = {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800634 .reg_bits = 32,
635 .val_bits = 32,
636 .reg_stride = 4,
637 .max_register = 0x88,
Chao Fu349ad662013-08-16 11:08:55 +0800638};
639
640static int dspi_probe(struct platform_device *pdev)
641{
642 struct device_node *np = pdev->dev.of_node;
643 struct spi_master *master;
644 struct fsl_dspi *dspi;
645 struct resource *res;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800646 void __iomem *base;
Chao Fu349ad662013-08-16 11:08:55 +0800647 int ret = 0, cs_num, bus_num;
Haikun Wangd1f4a382015-06-09 19:45:27 +0800648 const struct of_device_id *of_id =
649 of_match_device(fsl_dspi_dt_ids, &pdev->dev);
Chao Fu349ad662013-08-16 11:08:55 +0800650
651 master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
652 if (!master)
653 return -ENOMEM;
654
655 dspi = spi_master_get_devdata(master);
656 dspi->pdev = pdev;
Chao Fu9298bc72015-01-27 16:27:22 +0530657 dspi->master = master;
658
659 master->transfer = NULL;
660 master->setup = dspi_setup;
661 master->transfer_one_message = dspi_transfer_one_message;
662 master->dev.of_node = pdev->dev.of_node;
Chao Fu349ad662013-08-16 11:08:55 +0800663
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530664 master->cleanup = dspi_cleanup;
Chao Fu349ad662013-08-16 11:08:55 +0800665 master->mode_bits = SPI_CPOL | SPI_CPHA;
666 master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
667 SPI_BPW_MASK(16);
668
669 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
670 if (ret < 0) {
671 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
672 goto out_master_put;
673 }
674 master->num_chipselect = cs_num;
675
676 ret = of_property_read_u32(np, "bus-num", &bus_num);
677 if (ret < 0) {
678 dev_err(&pdev->dev, "can't get bus-num\n");
679 goto out_master_put;
680 }
681 master->bus_num = bus_num;
682
Haikun Wangd1f4a382015-06-09 19:45:27 +0800683 dspi->devtype_data = (struct fsl_dspi_devtype_data *)of_id->data;
684 if (!dspi->devtype_data) {
685 dev_err(&pdev->dev, "can't get devtype_data\n");
686 ret = -EFAULT;
687 goto out_master_put;
688 }
689
Chao Fu349ad662013-08-16 11:08:55 +0800690 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chao Fu1acbdeb2014-02-12 15:29:05 +0800691 base = devm_ioremap_resource(&pdev->dev, res);
692 if (IS_ERR(base)) {
693 ret = PTR_ERR(base);
Chao Fu349ad662013-08-16 11:08:55 +0800694 goto out_master_put;
695 }
696
Haikun Wangd2233322015-04-24 18:54:47 +0800697 dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
Chao Fu1acbdeb2014-02-12 15:29:05 +0800698 &dspi_regmap_config);
699 if (IS_ERR(dspi->regmap)) {
700 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
701 PTR_ERR(dspi->regmap));
702 return PTR_ERR(dspi->regmap);
703 }
704
Chao Fu349ad662013-08-16 11:08:55 +0800705 dspi->irq = platform_get_irq(pdev, 0);
706 if (dspi->irq < 0) {
707 dev_err(&pdev->dev, "can't get platform irq\n");
708 ret = dspi->irq;
709 goto out_master_put;
710 }
711
712 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
713 pdev->name, dspi);
714 if (ret < 0) {
715 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
716 goto out_master_put;
717 }
718
719 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
720 if (IS_ERR(dspi->clk)) {
721 ret = PTR_ERR(dspi->clk);
722 dev_err(&pdev->dev, "unable to get clock\n");
723 goto out_master_put;
724 }
725 clk_prepare_enable(dspi->clk);
726
727 init_waitqueue_head(&dspi->waitq);
Axel Lin017145f2014-02-14 12:49:12 +0800728 platform_set_drvdata(pdev, master);
Chao Fu349ad662013-08-16 11:08:55 +0800729
Chao Fu9298bc72015-01-27 16:27:22 +0530730 ret = spi_register_master(master);
Chao Fu349ad662013-08-16 11:08:55 +0800731 if (ret != 0) {
732 dev_err(&pdev->dev, "Problem registering DSPI master\n");
733 goto out_clk_put;
734 }
735
Chao Fu349ad662013-08-16 11:08:55 +0800736 return ret;
737
738out_clk_put:
739 clk_disable_unprepare(dspi->clk);
740out_master_put:
741 spi_master_put(master);
Chao Fu349ad662013-08-16 11:08:55 +0800742
743 return ret;
744}
745
746static int dspi_remove(struct platform_device *pdev)
747{
Axel Lin017145f2014-02-14 12:49:12 +0800748 struct spi_master *master = platform_get_drvdata(pdev);
749 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Chao Fu349ad662013-08-16 11:08:55 +0800750
751 /* Disconnect from the SPI framework */
Wei Yongjun05209f42013-10-12 15:15:31 +0800752 clk_disable_unprepare(dspi->clk);
Chao Fu9298bc72015-01-27 16:27:22 +0530753 spi_unregister_master(dspi->master);
754 spi_master_put(dspi->master);
Chao Fu349ad662013-08-16 11:08:55 +0800755
756 return 0;
757}
758
759static struct platform_driver fsl_dspi_driver = {
760 .driver.name = DRIVER_NAME,
761 .driver.of_match_table = fsl_dspi_dt_ids,
762 .driver.owner = THIS_MODULE,
763 .driver.pm = &dspi_pm,
764 .probe = dspi_probe,
765 .remove = dspi_remove,
766};
767module_platform_driver(fsl_dspi_driver);
768
769MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
Uwe Kleine-Königb444d1d2013-09-10 10:46:33 +0200770MODULE_LICENSE("GPL");
Chao Fu349ad662013-08-16 11:08:55 +0800771MODULE_ALIAS("platform:" DRIVER_NAME);