blob: 6e2ba62ceb631f6add248d40b7b4b02f68bb6699 [file] [log] [blame]
Magnus Damm8051eff2009-11-26 11:10:05 +00001/*
2 * SuperH MSIOF SPI Master Interface
3 *
4 * Copyright (c) 2009 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
Magnus Damm8051eff2009-11-26 11:10:05 +000012#include <linux/bitmap.h>
13#include <linux/clk.h>
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010014#include <linux/completion.h>
15#include <linux/delay.h>
Magnus Dammac48eee2010-01-20 13:49:45 -070016#include <linux/err.h>
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010017#include <linux/gpio.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040022#include <linux/module.h>
Bastian Hechtcf9c86e2012-12-12 12:54:48 +010023#include <linux/of.h>
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010024#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000026
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010027#include <linux/spi/sh_msiof.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000028#include <linux/spi/spi.h>
29#include <linux/spi/spi_bitbang.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000030
Magnus Damm8051eff2009-11-26 11:10:05 +000031#include <asm/unaligned.h>
32
33struct sh_msiof_spi_priv {
34 struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
35 void __iomem *mapbase;
36 struct clk *clk;
37 struct platform_device *pdev;
38 struct sh_msiof_spi_info *info;
39 struct completion done;
40 unsigned long flags;
41 int tx_fifo_size;
42 int rx_fifo_size;
43};
44
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010045#define TMDR1 0x00 /* Transmit Mode Register 1 */
46#define TMDR2 0x04 /* Transmit Mode Register 2 */
47#define TMDR3 0x08 /* Transmit Mode Register 3 */
48#define RMDR1 0x10 /* Receive Mode Register 1 */
49#define RMDR2 0x14 /* Receive Mode Register 2 */
50#define RMDR3 0x18 /* Receive Mode Register 3 */
51#define TSCR 0x20 /* Transmit Clock Select Register */
52#define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */
53#define CTR 0x28 /* Control Register */
54#define FCTR 0x30 /* FIFO Control Register */
55#define STR 0x40 /* Status Register */
56#define IER 0x44 /* Interrupt Enable Register */
57#define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */
58#define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */
59#define TFDR 0x50 /* Transmit FIFO Data Register */
60#define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */
61#define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */
62#define RFDR 0x60 /* Receive FIFO Data Register */
Magnus Damm8051eff2009-11-26 11:10:05 +000063
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010064/* TMDR1 and RMDR1 */
65#define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */
66#define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
67#define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */
68#define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */
69#define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */
70#define MDR1_BITLSB_SHIFT 24 /* MSB/LSB First (1 = LSB first) */
71#define MDR1_FLD_MASK 0x000000c0 /* Frame Sync Signal Interval (0-3) */
72#define MDR1_FLD_SHIFT 2
73#define MDR1_XXSTP 0x00000001 /* Transmission/Reception Stop on FIFO */
74/* TMDR1 */
75#define TMDR1_PCON 0x40000000 /* Transfer Signal Connection */
Magnus Damm8051eff2009-11-26 11:10:05 +000076
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010077/* TMDR2 and RMDR2 */
78#define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */
79#define MDR2_WDLEN1(i) (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
80#define MDR2_GRPMASK1 0x00000001 /* Group Output Mask 1 (SH, A1) */
81
82/* TSCR and RSCR */
83#define SCR_BRPS_MASK 0x1f00 /* Prescaler Setting (1-32) */
84#define SCR_BRPS(i) (((i) - 1) << 8)
85#define SCR_BRDV_MASK 0x0007 /* Baud Rate Generator's Division Ratio */
86#define SCR_BRDV_DIV_2 0x0000
87#define SCR_BRDV_DIV_4 0x0001
88#define SCR_BRDV_DIV_8 0x0002
89#define SCR_BRDV_DIV_16 0x0003
90#define SCR_BRDV_DIV_32 0x0004
91#define SCR_BRDV_DIV_1 0x0007
92
93/* CTR */
94#define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */
95#define CTR_TSCKIZ_SCK 0x80000000 /* Disable SCK when TX disabled */
96#define CTR_TSCKIZ_POL_SHIFT 30 /* Transmit Clock Polarity */
97#define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */
98#define CTR_RSCKIZ_SCK 0x20000000 /* Must match CTR_TSCKIZ_SCK */
99#define CTR_RSCKIZ_POL_SHIFT 28 /* Receive Clock Polarity */
100#define CTR_TEDG_SHIFT 27 /* Transmit Timing (1 = falling edge) */
101#define CTR_REDG_SHIFT 26 /* Receive Timing (1 = falling edge) */
102#define CTR_TXDIZ_MASK 0x00c00000 /* Pin Output When TX is Disabled */
103#define CTR_TXDIZ_LOW 0x00000000 /* 0 */
104#define CTR_TXDIZ_HIGH 0x00400000 /* 1 */
105#define CTR_TXDIZ_HIZ 0x00800000 /* High-impedance */
106#define CTR_TSCKE 0x00008000 /* Transmit Serial Clock Output Enable */
107#define CTR_TFSE 0x00004000 /* Transmit Frame Sync Signal Output Enable */
108#define CTR_TXE 0x00000200 /* Transmit Enable */
109#define CTR_RXE 0x00000100 /* Receive Enable */
110
111/* STR and IER */
112#define STR_TEOF 0x00800000 /* Frame Transmission End */
113#define STR_REOF 0x00000080 /* Frame Reception End */
114
115
116#define DEFAULT_TX_FIFO_SIZE 64
117#define DEFAULT_RX_FIFO_SIZE 64
118
Magnus Damm8051eff2009-11-26 11:10:05 +0000119
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100120static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
Magnus Damm8051eff2009-11-26 11:10:05 +0000121{
122 switch (reg_offs) {
123 case TSCR:
124 case RSCR:
125 return ioread16(p->mapbase + reg_offs);
126 default:
127 return ioread32(p->mapbase + reg_offs);
128 }
129}
130
131static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100132 u32 value)
Magnus Damm8051eff2009-11-26 11:10:05 +0000133{
134 switch (reg_offs) {
135 case TSCR:
136 case RSCR:
137 iowrite16(value, p->mapbase + reg_offs);
138 break;
139 default:
140 iowrite32(value, p->mapbase + reg_offs);
141 break;
142 }
143}
144
145static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100146 u32 clr, u32 set)
Magnus Damm8051eff2009-11-26 11:10:05 +0000147{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100148 u32 mask = clr | set;
149 u32 data;
Magnus Damm8051eff2009-11-26 11:10:05 +0000150 int k;
151
152 data = sh_msiof_read(p, CTR);
153 data &= ~clr;
154 data |= set;
155 sh_msiof_write(p, CTR, data);
156
157 for (k = 100; k > 0; k--) {
158 if ((sh_msiof_read(p, CTR) & mask) == set)
159 break;
160
161 udelay(10);
162 }
163
164 return k > 0 ? 0 : -ETIMEDOUT;
165}
166
167static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
168{
169 struct sh_msiof_spi_priv *p = data;
170
171 /* just disable the interrupt and wake up */
172 sh_msiof_write(p, IER, 0);
173 complete(&p->done);
174
175 return IRQ_HANDLED;
176}
177
178static struct {
179 unsigned short div;
180 unsigned short scr;
181} const sh_msiof_spi_clk_table[] = {
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100182 { 1, SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
183 { 2, SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
184 { 4, SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
185 { 8, SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
186 { 16, SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
187 { 32, SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
188 { 64, SCR_BRPS(32) | SCR_BRDV_DIV_2 },
189 { 128, SCR_BRPS(32) | SCR_BRDV_DIV_4 },
190 { 256, SCR_BRPS(32) | SCR_BRDV_DIV_8 },
191 { 512, SCR_BRPS(32) | SCR_BRDV_DIV_16 },
192 { 1024, SCR_BRPS(32) | SCR_BRDV_DIV_32 },
Magnus Damm8051eff2009-11-26 11:10:05 +0000193};
194
195static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100196 unsigned long parent_rate, u32 spi_hz)
Magnus Damm8051eff2009-11-26 11:10:05 +0000197{
198 unsigned long div = 1024;
199 size_t k;
200
201 if (!WARN_ON(!spi_hz || !parent_rate))
Takashi Yoshiie4d313f2013-12-02 03:19:13 +0900202 div = DIV_ROUND_UP(parent_rate, spi_hz);
Magnus Damm8051eff2009-11-26 11:10:05 +0000203
204 /* TODO: make more fine grained */
205
206 for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
207 if (sh_msiof_spi_clk_table[k].div >= div)
208 break;
209 }
210
211 k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
212
213 sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
214 sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
215}
216
217static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100218 u32 cpol, u32 cpha,
Takashi Yoshii50a77992013-12-02 03:19:15 +0900219 u32 tx_hi_z, u32 lsb_first, u32 cs_high)
Magnus Damm8051eff2009-11-26 11:10:05 +0000220{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100221 u32 tmp;
Magnus Damm8051eff2009-11-26 11:10:05 +0000222 int edge;
223
224 /*
Markus Pietreke8708ef2010-02-02 11:29:15 +0900225 * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
226 * 0 0 10 10 1 1
227 * 0 1 10 10 0 0
228 * 1 0 11 11 0 0
229 * 1 1 11 11 1 1
Magnus Damm8051eff2009-11-26 11:10:05 +0000230 */
Magnus Damm8051eff2009-11-26 11:10:05 +0000231 sh_msiof_write(p, FCTR, 0);
Takashi Yoshii50a77992013-12-02 03:19:15 +0900232
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100233 tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
234 tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
235 tmp |= lsb_first << MDR1_BITLSB_SHIFT;
236 sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
237 sh_msiof_write(p, RMDR1, tmp);
Magnus Damm8051eff2009-11-26 11:10:05 +0000238
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100239 tmp = 0;
240 tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
241 tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
Magnus Damm8051eff2009-11-26 11:10:05 +0000242
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100243 edge = cpol ^ !cpha;
Magnus Damm8051eff2009-11-26 11:10:05 +0000244
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100245 tmp |= edge << CTR_TEDG_SHIFT;
246 tmp |= edge << CTR_REDG_SHIFT;
247 tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
Magnus Damm8051eff2009-11-26 11:10:05 +0000248 sh_msiof_write(p, CTR, tmp);
249}
250
251static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
252 const void *tx_buf, void *rx_buf,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100253 u32 bits, u32 words)
Magnus Damm8051eff2009-11-26 11:10:05 +0000254{
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100255 u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
Magnus Damm8051eff2009-11-26 11:10:05 +0000256
257 if (tx_buf)
258 sh_msiof_write(p, TMDR2, dr2);
259 else
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100260 sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
Magnus Damm8051eff2009-11-26 11:10:05 +0000261
262 if (rx_buf)
263 sh_msiof_write(p, RMDR2, dr2);
264
265 sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
266}
267
268static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
269{
270 sh_msiof_write(p, STR, sh_msiof_read(p, STR));
271}
272
273static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
274 const void *tx_buf, int words, int fs)
275{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100276 const u8 *buf_8 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000277 int k;
278
279 for (k = 0; k < words; k++)
280 sh_msiof_write(p, TFDR, buf_8[k] << fs);
281}
282
283static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
284 const void *tx_buf, int words, int fs)
285{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100286 const u16 *buf_16 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000287 int k;
288
289 for (k = 0; k < words; k++)
290 sh_msiof_write(p, TFDR, buf_16[k] << fs);
291}
292
293static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
294 const void *tx_buf, int words, int fs)
295{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100296 const u16 *buf_16 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000297 int k;
298
299 for (k = 0; k < words; k++)
300 sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
301}
302
303static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
304 const void *tx_buf, int words, int fs)
305{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100306 const u32 *buf_32 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000307 int k;
308
309 for (k = 0; k < words; k++)
310 sh_msiof_write(p, TFDR, buf_32[k] << fs);
311}
312
313static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
314 const void *tx_buf, int words, int fs)
315{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100316 const u32 *buf_32 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000317 int k;
318
319 for (k = 0; k < words; k++)
320 sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
321}
322
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100323static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
324 const void *tx_buf, int words, int fs)
325{
326 const u32 *buf_32 = tx_buf;
327 int k;
328
329 for (k = 0; k < words; k++)
330 sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
331}
332
333static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
334 const void *tx_buf, int words, int fs)
335{
336 const u32 *buf_32 = tx_buf;
337 int k;
338
339 for (k = 0; k < words; k++)
340 sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
341}
342
Magnus Damm8051eff2009-11-26 11:10:05 +0000343static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
344 void *rx_buf, int words, int fs)
345{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100346 u8 *buf_8 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000347 int k;
348
349 for (k = 0; k < words; k++)
350 buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
351}
352
353static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
354 void *rx_buf, int words, int fs)
355{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100356 u16 *buf_16 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000357 int k;
358
359 for (k = 0; k < words; k++)
360 buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
361}
362
363static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
364 void *rx_buf, int words, int fs)
365{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100366 u16 *buf_16 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000367 int k;
368
369 for (k = 0; k < words; k++)
370 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
371}
372
373static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
374 void *rx_buf, int words, int fs)
375{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100376 u32 *buf_32 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000377 int k;
378
379 for (k = 0; k < words; k++)
380 buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
381}
382
383static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
384 void *rx_buf, int words, int fs)
385{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100386 u32 *buf_32 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000387 int k;
388
389 for (k = 0; k < words; k++)
390 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
391}
392
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100393static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
394 void *rx_buf, int words, int fs)
395{
396 u32 *buf_32 = rx_buf;
397 int k;
398
399 for (k = 0; k < words; k++)
400 buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
401}
402
403static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
404 void *rx_buf, int words, int fs)
405{
406 u32 *buf_32 = rx_buf;
407 int k;
408
409 for (k = 0; k < words; k++)
410 put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
411}
412
Magnus Damm8051eff2009-11-26 11:10:05 +0000413static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
414{
415 int bits;
416
417 bits = t ? t->bits_per_word : 0;
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100418 if (!bits)
419 bits = spi->bits_per_word;
Magnus Damm8051eff2009-11-26 11:10:05 +0000420 return bits;
421}
422
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100423static u32 sh_msiof_spi_hz(struct spi_device *spi, struct spi_transfer *t)
Magnus Damm8051eff2009-11-26 11:10:05 +0000424{
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100425 u32 hz;
Magnus Damm8051eff2009-11-26 11:10:05 +0000426
427 hz = t ? t->speed_hz : 0;
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100428 if (!hz)
429 hz = spi->max_speed_hz;
Magnus Damm8051eff2009-11-26 11:10:05 +0000430 return hz;
431}
432
433static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
434 struct spi_transfer *t)
435{
436 int bits;
437
438 /* noting to check hz values against since parent clock is disabled */
439
440 bits = sh_msiof_spi_bits(spi, t);
441 if (bits < 8)
442 return -EINVAL;
443 if (bits > 32)
444 return -EINVAL;
445
446 return spi_bitbang_setup_transfer(spi, t);
447}
448
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100449static int sh_msiof_spi_setup(struct spi_device *spi)
450{
451 struct device_node *np = spi->master->dev.of_node;
452
453 if (!np) {
454 /*
455 * Use spi->controller_data for CS (same strategy as spi_gpio),
456 * if any. otherwise let HW control CS
457 */
458 spi->cs_gpio = (uintptr_t)spi->controller_data;
459 }
460
461 return spi_bitbang_setup(spi);
462}
463
Magnus Damm8051eff2009-11-26 11:10:05 +0000464static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
465{
466 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
467 int value;
468
469 /* chip select is active low unless SPI_CS_HIGH is set */
470 if (spi->mode & SPI_CS_HIGH)
471 value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
472 else
473 value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
474
475 if (is_on == BITBANG_CS_ACTIVE) {
476 if (!test_and_set_bit(0, &p->flags)) {
477 pm_runtime_get_sync(&p->pdev->dev);
478 clk_enable(p->clk);
479 }
480
481 /* Configure pins before asserting CS */
482 sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
483 !!(spi->mode & SPI_CPHA),
484 !!(spi->mode & SPI_3WIRE),
Takashi Yoshii50a77992013-12-02 03:19:15 +0900485 !!(spi->mode & SPI_LSB_FIRST),
486 !!(spi->mode & SPI_CS_HIGH));
Magnus Damm8051eff2009-11-26 11:10:05 +0000487 }
488
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100489 if (spi->cs_gpio >= 0)
490 gpio_set_value(spi->cs_gpio, value);
Magnus Damm8051eff2009-11-26 11:10:05 +0000491
492 if (is_on == BITBANG_CS_INACTIVE) {
493 if (test_and_clear_bit(0, &p->flags)) {
494 clk_disable(p->clk);
495 pm_runtime_put(&p->pdev->dev);
496 }
497 }
498}
499
500static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
501 void (*tx_fifo)(struct sh_msiof_spi_priv *,
502 const void *, int, int),
503 void (*rx_fifo)(struct sh_msiof_spi_priv *,
504 void *, int, int),
505 const void *tx_buf, void *rx_buf,
506 int words, int bits)
507{
508 int fifo_shift;
509 int ret;
510
511 /* limit maximum word transfer to rx/tx fifo size */
512 if (tx_buf)
513 words = min_t(int, words, p->tx_fifo_size);
514 if (rx_buf)
515 words = min_t(int, words, p->rx_fifo_size);
516
517 /* the fifo contents need shifting */
518 fifo_shift = 32 - bits;
519
520 /* setup msiof transfer mode registers */
521 sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
522
523 /* write tx fifo */
524 if (tx_buf)
525 tx_fifo(p, tx_buf, words, fifo_shift);
526
527 /* setup clock and rx/tx signals */
528 ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
529 if (rx_buf)
530 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
531 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
532
533 /* start by setting frame bit */
Wolfram Sang16735d02013-11-14 14:32:02 -0800534 reinit_completion(&p->done);
Magnus Damm8051eff2009-11-26 11:10:05 +0000535 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
536 if (ret) {
537 dev_err(&p->pdev->dev, "failed to start hardware\n");
538 goto err;
539 }
540
541 /* wait for tx fifo to be emptied / rx fifo to be filled */
542 wait_for_completion(&p->done);
543
544 /* read rx fifo */
545 if (rx_buf)
546 rx_fifo(p, rx_buf, words, fifo_shift);
547
548 /* clear status bits */
549 sh_msiof_reset_str(p);
550
Geert Uytterhoevena669c112014-02-20 15:43:01 +0100551 /* shut down frame, rx/tx and clock signals */
Magnus Damm8051eff2009-11-26 11:10:05 +0000552 ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
553 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
554 if (rx_buf)
555 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
556 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
557 if (ret) {
558 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
559 goto err;
560 }
561
562 return words;
563
564 err:
565 sh_msiof_write(p, IER, 0);
566 return ret;
567}
568
569static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
570{
571 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
572 void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
573 void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
574 int bits;
575 int bytes_per_word;
576 int bytes_done;
577 int words;
578 int n;
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100579 bool swab;
Magnus Damm8051eff2009-11-26 11:10:05 +0000580
581 bits = sh_msiof_spi_bits(spi, t);
582
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100583 if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
584 bits = 32;
585 swab = true;
586 } else {
587 swab = false;
588 }
589
Magnus Damm8051eff2009-11-26 11:10:05 +0000590 /* setup bytes per word and fifo read/write functions */
591 if (bits <= 8) {
592 bytes_per_word = 1;
593 tx_fifo = sh_msiof_spi_write_fifo_8;
594 rx_fifo = sh_msiof_spi_read_fifo_8;
595 } else if (bits <= 16) {
596 bytes_per_word = 2;
597 if ((unsigned long)t->tx_buf & 0x01)
598 tx_fifo = sh_msiof_spi_write_fifo_16u;
599 else
600 tx_fifo = sh_msiof_spi_write_fifo_16;
601
602 if ((unsigned long)t->rx_buf & 0x01)
603 rx_fifo = sh_msiof_spi_read_fifo_16u;
604 else
605 rx_fifo = sh_msiof_spi_read_fifo_16;
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100606 } else if (swab) {
607 bytes_per_word = 4;
608 if ((unsigned long)t->tx_buf & 0x03)
609 tx_fifo = sh_msiof_spi_write_fifo_s32u;
610 else
611 tx_fifo = sh_msiof_spi_write_fifo_s32;
612
613 if ((unsigned long)t->rx_buf & 0x03)
614 rx_fifo = sh_msiof_spi_read_fifo_s32u;
615 else
616 rx_fifo = sh_msiof_spi_read_fifo_s32;
Magnus Damm8051eff2009-11-26 11:10:05 +0000617 } else {
618 bytes_per_word = 4;
619 if ((unsigned long)t->tx_buf & 0x03)
620 tx_fifo = sh_msiof_spi_write_fifo_32u;
621 else
622 tx_fifo = sh_msiof_spi_write_fifo_32;
623
624 if ((unsigned long)t->rx_buf & 0x03)
625 rx_fifo = sh_msiof_spi_read_fifo_32u;
626 else
627 rx_fifo = sh_msiof_spi_read_fifo_32;
628 }
629
630 /* setup clocks (clock already enabled in chipselect()) */
631 sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
632 sh_msiof_spi_hz(spi, t));
633
634 /* transfer in fifo sized chunks */
635 words = t->len / bytes_per_word;
636 bytes_done = 0;
637
638 while (bytes_done < t->len) {
Guennadi Liakhovetski8a6afb92011-01-21 16:56:47 +0100639 void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
640 const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
Magnus Damm8051eff2009-11-26 11:10:05 +0000641 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
Guennadi Liakhovetski8a6afb92011-01-21 16:56:47 +0100642 tx_buf,
643 rx_buf,
Magnus Damm8051eff2009-11-26 11:10:05 +0000644 words, bits);
645 if (n < 0)
646 break;
647
648 bytes_done += n * bytes_per_word;
649 words -= n;
650 }
651
652 return bytes_done;
653}
654
655static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
656 u32 word, u8 bits)
657{
658 BUG(); /* unused but needed by bitbang code */
659 return 0;
660}
661
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100662#ifdef CONFIG_OF
663static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
664{
665 struct sh_msiof_spi_info *info;
666 struct device_node *np = dev->of_node;
667 u32 num_cs = 0;
668
669 info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
670 if (!info) {
671 dev_err(dev, "failed to allocate setup data\n");
672 return NULL;
673 }
674
675 /* Parse the MSIOF properties */
676 of_property_read_u32(np, "num-cs", &num_cs);
677 of_property_read_u32(np, "renesas,tx-fifo-size",
678 &info->tx_fifo_override);
679 of_property_read_u32(np, "renesas,rx-fifo-size",
680 &info->rx_fifo_override);
681
682 info->num_chipselect = num_cs;
683
684 return info;
685}
686#else
687static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
688{
689 return NULL;
690}
691#endif
692
Magnus Damm8051eff2009-11-26 11:10:05 +0000693static int sh_msiof_spi_probe(struct platform_device *pdev)
694{
695 struct resource *r;
696 struct spi_master *master;
697 struct sh_msiof_spi_priv *p;
Magnus Damm8051eff2009-11-26 11:10:05 +0000698 int i;
699 int ret;
700
701 master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
702 if (master == NULL) {
703 dev_err(&pdev->dev, "failed to allocate spi master\n");
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100704 return -ENOMEM;
Magnus Damm8051eff2009-11-26 11:10:05 +0000705 }
706
707 p = spi_master_get_devdata(master);
708
709 platform_set_drvdata(pdev, p);
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100710 if (pdev->dev.of_node)
711 p->info = sh_msiof_spi_parse_dt(&pdev->dev);
712 else
Jingoo Han8074cf02013-07-30 16:58:59 +0900713 p->info = dev_get_platdata(&pdev->dev);
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100714
715 if (!p->info) {
716 dev_err(&pdev->dev, "failed to obtain device info\n");
717 ret = -ENXIO;
718 goto err1;
719 }
720
Magnus Damm8051eff2009-11-26 11:10:05 +0000721 init_completion(&p->done);
722
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100723 p->clk = devm_clk_get(&pdev->dev, NULL);
Magnus Damm8051eff2009-11-26 11:10:05 +0000724 if (IS_ERR(p->clk)) {
Bastian Hecht078b6ea2012-11-07 12:40:04 +0100725 dev_err(&pdev->dev, "cannot get clock\n");
Magnus Damm8051eff2009-11-26 11:10:05 +0000726 ret = PTR_ERR(p->clk);
727 goto err1;
728 }
729
Magnus Damm8051eff2009-11-26 11:10:05 +0000730 i = platform_get_irq(pdev, 0);
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100731 if (i < 0) {
732 dev_err(&pdev->dev, "cannot get platform IRQ\n");
Magnus Damm8051eff2009-11-26 11:10:05 +0000733 ret = -ENOENT;
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100734 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000735 }
736
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100737 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
738 p->mapbase = devm_ioremap_resource(&pdev->dev, r);
739 if (IS_ERR(p->mapbase)) {
740 ret = PTR_ERR(p->mapbase);
741 goto err1;
742 }
743
744 ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
745 dev_name(&pdev->dev), p);
Magnus Damm8051eff2009-11-26 11:10:05 +0000746 if (ret) {
747 dev_err(&pdev->dev, "unable to request irq\n");
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100748 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000749 }
750
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100751 ret = clk_prepare(p->clk);
752 if (ret < 0) {
753 dev_err(&pdev->dev, "unable to prepare clock\n");
754 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000755 }
756
757 p->pdev = pdev;
758 pm_runtime_enable(&pdev->dev);
759
760 /* The standard version of MSIOF use 64 word FIFOs */
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100761 p->tx_fifo_size = DEFAULT_TX_FIFO_SIZE;
762 p->rx_fifo_size = DEFAULT_RX_FIFO_SIZE;
Magnus Damm8051eff2009-11-26 11:10:05 +0000763
764 /* Platform data may override FIFO sizes */
765 if (p->info->tx_fifo_override)
766 p->tx_fifo_size = p->info->tx_fifo_override;
767 if (p->info->rx_fifo_override)
768 p->rx_fifo_size = p->info->rx_fifo_override;
769
770 /* init master and bitbang code */
771 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
772 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
773 master->flags = 0;
774 master->bus_num = pdev->id;
Geert Uytterhoevenf7c05e82014-02-20 15:43:00 +0100775 master->dev.of_node = pdev->dev.of_node;
Magnus Damm8051eff2009-11-26 11:10:05 +0000776 master->num_chipselect = p->info->num_chipselect;
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100777 master->setup = sh_msiof_spi_setup;
Magnus Damm8051eff2009-11-26 11:10:05 +0000778 master->cleanup = spi_bitbang_cleanup;
779
780 p->bitbang.master = master;
781 p->bitbang.chipselect = sh_msiof_spi_chipselect;
782 p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
783 p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
784 p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
785 p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
786 p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
787 p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
788
789 ret = spi_bitbang_start(&p->bitbang);
790 if (ret == 0)
791 return 0;
792
793 pm_runtime_disable(&pdev->dev);
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100794 clk_unprepare(p->clk);
Magnus Damm8051eff2009-11-26 11:10:05 +0000795 err1:
796 spi_master_put(master);
Magnus Damm8051eff2009-11-26 11:10:05 +0000797 return ret;
798}
799
800static int sh_msiof_spi_remove(struct platform_device *pdev)
801{
802 struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
803 int ret;
804
805 ret = spi_bitbang_stop(&p->bitbang);
806 if (!ret) {
807 pm_runtime_disable(&pdev->dev);
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100808 clk_unprepare(p->clk);
Magnus Damm8051eff2009-11-26 11:10:05 +0000809 spi_master_put(p->bitbang.master);
810 }
811 return ret;
812}
813
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100814#ifdef CONFIG_OF
815static const struct of_device_id sh_msiof_match[] = {
816 { .compatible = "renesas,sh-msiof", },
817 { .compatible = "renesas,sh-mobile-msiof", },
818 {},
819};
820MODULE_DEVICE_TABLE(of, sh_msiof_match);
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100821#endif
822
Magnus Damm8051eff2009-11-26 11:10:05 +0000823static struct platform_driver sh_msiof_spi_drv = {
824 .probe = sh_msiof_spi_probe,
825 .remove = sh_msiof_spi_remove,
826 .driver = {
827 .name = "spi_sh_msiof",
828 .owner = THIS_MODULE,
Sachin Kamat691ee4e2013-03-14 15:31:51 +0530829 .of_match_table = of_match_ptr(sh_msiof_match),
Magnus Damm8051eff2009-11-26 11:10:05 +0000830 },
831};
Grant Likely940ab882011-10-05 11:29:49 -0600832module_platform_driver(sh_msiof_spi_drv);
Magnus Damm8051eff2009-11-26 11:10:05 +0000833
834MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
835MODULE_AUTHOR("Magnus Damm");
836MODULE_LICENSE("GPL v2");
837MODULE_ALIAS("platform:spi_sh_msiof");