blob: efeda03bbacec9df7ea7e41bab811c6de53491b3 [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>
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +010024#include <linux/of_device.h>
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010025#include <linux/platform_device.h>
26#include <linux/pm_runtime.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000027
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +010028#include <linux/spi/sh_msiof.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000029#include <linux/spi/spi.h>
30#include <linux/spi/spi_bitbang.h>
Magnus Damm8051eff2009-11-26 11:10:05 +000031
Magnus Damm8051eff2009-11-26 11:10:05 +000032#include <asm/unaligned.h>
33
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +010034
35struct sh_msiof_chipdata {
36 u16 tx_fifo_size;
37 u16 rx_fifo_size;
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +010038 u16 master_flags;
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +010039};
40
Magnus Damm8051eff2009-11-26 11:10:05 +000041struct sh_msiof_spi_priv {
42 struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
43 void __iomem *mapbase;
44 struct clk *clk;
45 struct platform_device *pdev;
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +010046 const struct sh_msiof_chipdata *chipdata;
Magnus Damm8051eff2009-11-26 11:10:05 +000047 struct sh_msiof_spi_info *info;
48 struct completion done;
Magnus Damm8051eff2009-11-26 11:10:05 +000049 int tx_fifo_size;
50 int rx_fifo_size;
51};
52
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010053#define TMDR1 0x00 /* Transmit Mode Register 1 */
54#define TMDR2 0x04 /* Transmit Mode Register 2 */
55#define TMDR3 0x08 /* Transmit Mode Register 3 */
56#define RMDR1 0x10 /* Receive Mode Register 1 */
57#define RMDR2 0x14 /* Receive Mode Register 2 */
58#define RMDR3 0x18 /* Receive Mode Register 3 */
59#define TSCR 0x20 /* Transmit Clock Select Register */
60#define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */
61#define CTR 0x28 /* Control Register */
62#define FCTR 0x30 /* FIFO Control Register */
63#define STR 0x40 /* Status Register */
64#define IER 0x44 /* Interrupt Enable Register */
65#define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */
66#define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */
67#define TFDR 0x50 /* Transmit FIFO Data Register */
68#define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */
69#define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */
70#define RFDR 0x60 /* Receive FIFO Data Register */
Magnus Damm8051eff2009-11-26 11:10:05 +000071
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010072/* TMDR1 and RMDR1 */
73#define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */
74#define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
75#define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */
76#define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */
77#define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */
78#define MDR1_BITLSB_SHIFT 24 /* MSB/LSB First (1 = LSB first) */
79#define MDR1_FLD_MASK 0x000000c0 /* Frame Sync Signal Interval (0-3) */
80#define MDR1_FLD_SHIFT 2
81#define MDR1_XXSTP 0x00000001 /* Transmission/Reception Stop on FIFO */
82/* TMDR1 */
83#define TMDR1_PCON 0x40000000 /* Transfer Signal Connection */
Magnus Damm8051eff2009-11-26 11:10:05 +000084
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +010085/* TMDR2 and RMDR2 */
86#define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */
87#define MDR2_WDLEN1(i) (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
88#define MDR2_GRPMASK1 0x00000001 /* Group Output Mask 1 (SH, A1) */
89
90/* TSCR and RSCR */
91#define SCR_BRPS_MASK 0x1f00 /* Prescaler Setting (1-32) */
92#define SCR_BRPS(i) (((i) - 1) << 8)
93#define SCR_BRDV_MASK 0x0007 /* Baud Rate Generator's Division Ratio */
94#define SCR_BRDV_DIV_2 0x0000
95#define SCR_BRDV_DIV_4 0x0001
96#define SCR_BRDV_DIV_8 0x0002
97#define SCR_BRDV_DIV_16 0x0003
98#define SCR_BRDV_DIV_32 0x0004
99#define SCR_BRDV_DIV_1 0x0007
100
101/* CTR */
102#define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */
103#define CTR_TSCKIZ_SCK 0x80000000 /* Disable SCK when TX disabled */
104#define CTR_TSCKIZ_POL_SHIFT 30 /* Transmit Clock Polarity */
105#define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */
106#define CTR_RSCKIZ_SCK 0x20000000 /* Must match CTR_TSCKIZ_SCK */
107#define CTR_RSCKIZ_POL_SHIFT 28 /* Receive Clock Polarity */
108#define CTR_TEDG_SHIFT 27 /* Transmit Timing (1 = falling edge) */
109#define CTR_REDG_SHIFT 26 /* Receive Timing (1 = falling edge) */
110#define CTR_TXDIZ_MASK 0x00c00000 /* Pin Output When TX is Disabled */
111#define CTR_TXDIZ_LOW 0x00000000 /* 0 */
112#define CTR_TXDIZ_HIGH 0x00400000 /* 1 */
113#define CTR_TXDIZ_HIZ 0x00800000 /* High-impedance */
114#define CTR_TSCKE 0x00008000 /* Transmit Serial Clock Output Enable */
115#define CTR_TFSE 0x00004000 /* Transmit Frame Sync Signal Output Enable */
116#define CTR_TXE 0x00000200 /* Transmit Enable */
117#define CTR_RXE 0x00000100 /* Receive Enable */
118
119/* STR and IER */
120#define STR_TEOF 0x00800000 /* Frame Transmission End */
121#define STR_REOF 0x00000080 /* Frame Reception End */
122
123
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100124static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
Magnus Damm8051eff2009-11-26 11:10:05 +0000125{
126 switch (reg_offs) {
127 case TSCR:
128 case RSCR:
129 return ioread16(p->mapbase + reg_offs);
130 default:
131 return ioread32(p->mapbase + reg_offs);
132 }
133}
134
135static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100136 u32 value)
Magnus Damm8051eff2009-11-26 11:10:05 +0000137{
138 switch (reg_offs) {
139 case TSCR:
140 case RSCR:
141 iowrite16(value, p->mapbase + reg_offs);
142 break;
143 default:
144 iowrite32(value, p->mapbase + reg_offs);
145 break;
146 }
147}
148
149static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100150 u32 clr, u32 set)
Magnus Damm8051eff2009-11-26 11:10:05 +0000151{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100152 u32 mask = clr | set;
153 u32 data;
Magnus Damm8051eff2009-11-26 11:10:05 +0000154 int k;
155
156 data = sh_msiof_read(p, CTR);
157 data &= ~clr;
158 data |= set;
159 sh_msiof_write(p, CTR, data);
160
161 for (k = 100; k > 0; k--) {
162 if ((sh_msiof_read(p, CTR) & mask) == set)
163 break;
164
165 udelay(10);
166 }
167
168 return k > 0 ? 0 : -ETIMEDOUT;
169}
170
171static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
172{
173 struct sh_msiof_spi_priv *p = data;
174
175 /* just disable the interrupt and wake up */
176 sh_msiof_write(p, IER, 0);
177 complete(&p->done);
178
179 return IRQ_HANDLED;
180}
181
182static struct {
183 unsigned short div;
184 unsigned short scr;
185} const sh_msiof_spi_clk_table[] = {
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100186 { 1, SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
187 { 2, SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
188 { 4, SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
189 { 8, SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
190 { 16, SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
191 { 32, SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
192 { 64, SCR_BRPS(32) | SCR_BRDV_DIV_2 },
193 { 128, SCR_BRPS(32) | SCR_BRDV_DIV_4 },
194 { 256, SCR_BRPS(32) | SCR_BRDV_DIV_8 },
195 { 512, SCR_BRPS(32) | SCR_BRDV_DIV_16 },
196 { 1024, SCR_BRPS(32) | SCR_BRDV_DIV_32 },
Magnus Damm8051eff2009-11-26 11:10:05 +0000197};
198
199static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100200 unsigned long parent_rate, u32 spi_hz)
Magnus Damm8051eff2009-11-26 11:10:05 +0000201{
202 unsigned long div = 1024;
203 size_t k;
204
205 if (!WARN_ON(!spi_hz || !parent_rate))
Takashi Yoshiie4d313f2013-12-02 03:19:13 +0900206 div = DIV_ROUND_UP(parent_rate, spi_hz);
Magnus Damm8051eff2009-11-26 11:10:05 +0000207
208 /* TODO: make more fine grained */
209
210 for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
211 if (sh_msiof_spi_clk_table[k].div >= div)
212 break;
213 }
214
215 k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
216
217 sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100218 if (!(p->chipdata->master_flags & SPI_MASTER_MUST_TX))
219 sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
Magnus Damm8051eff2009-11-26 11:10:05 +0000220}
221
222static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100223 u32 cpol, u32 cpha,
Takashi Yoshii50a77992013-12-02 03:19:15 +0900224 u32 tx_hi_z, u32 lsb_first, u32 cs_high)
Magnus Damm8051eff2009-11-26 11:10:05 +0000225{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100226 u32 tmp;
Magnus Damm8051eff2009-11-26 11:10:05 +0000227 int edge;
228
229 /*
Markus Pietreke8708ef2010-02-02 11:29:15 +0900230 * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
231 * 0 0 10 10 1 1
232 * 0 1 10 10 0 0
233 * 1 0 11 11 0 0
234 * 1 1 11 11 1 1
Magnus Damm8051eff2009-11-26 11:10:05 +0000235 */
Magnus Damm8051eff2009-11-26 11:10:05 +0000236 sh_msiof_write(p, FCTR, 0);
Takashi Yoshii50a77992013-12-02 03:19:15 +0900237
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100238 tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
239 tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
240 tmp |= lsb_first << MDR1_BITLSB_SHIFT;
241 sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100242 if (p->chipdata->master_flags & SPI_MASTER_MUST_TX) {
243 /* These bits are reserved if RX needs TX */
244 tmp &= ~0x0000ffff;
245 }
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100246 sh_msiof_write(p, RMDR1, tmp);
Magnus Damm8051eff2009-11-26 11:10:05 +0000247
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100248 tmp = 0;
249 tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
250 tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
Magnus Damm8051eff2009-11-26 11:10:05 +0000251
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100252 edge = cpol ^ !cpha;
Magnus Damm8051eff2009-11-26 11:10:05 +0000253
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100254 tmp |= edge << CTR_TEDG_SHIFT;
255 tmp |= edge << CTR_REDG_SHIFT;
256 tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
Magnus Damm8051eff2009-11-26 11:10:05 +0000257 sh_msiof_write(p, CTR, tmp);
258}
259
260static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
261 const void *tx_buf, void *rx_buf,
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100262 u32 bits, u32 words)
Magnus Damm8051eff2009-11-26 11:10:05 +0000263{
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100264 u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
Magnus Damm8051eff2009-11-26 11:10:05 +0000265
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100266 if (tx_buf || (p->chipdata->master_flags & SPI_MASTER_MUST_TX))
Magnus Damm8051eff2009-11-26 11:10:05 +0000267 sh_msiof_write(p, TMDR2, dr2);
268 else
Geert Uytterhoeven01cfef52014-02-20 15:43:03 +0100269 sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
Magnus Damm8051eff2009-11-26 11:10:05 +0000270
271 if (rx_buf)
272 sh_msiof_write(p, RMDR2, dr2);
273
274 sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
275}
276
277static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
278{
279 sh_msiof_write(p, STR, sh_msiof_read(p, STR));
280}
281
282static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
283 const void *tx_buf, int words, int fs)
284{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100285 const u8 *buf_8 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000286 int k;
287
288 for (k = 0; k < words; k++)
289 sh_msiof_write(p, TFDR, buf_8[k] << fs);
290}
291
292static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
293 const void *tx_buf, int words, int fs)
294{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100295 const u16 *buf_16 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000296 int k;
297
298 for (k = 0; k < words; k++)
299 sh_msiof_write(p, TFDR, buf_16[k] << fs);
300}
301
302static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
303 const void *tx_buf, int words, int fs)
304{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100305 const u16 *buf_16 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000306 int k;
307
308 for (k = 0; k < words; k++)
309 sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
310}
311
312static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
313 const void *tx_buf, int words, int fs)
314{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100315 const u32 *buf_32 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000316 int k;
317
318 for (k = 0; k < words; k++)
319 sh_msiof_write(p, TFDR, buf_32[k] << fs);
320}
321
322static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
323 const void *tx_buf, int words, int fs)
324{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100325 const u32 *buf_32 = tx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000326 int k;
327
328 for (k = 0; k < words; k++)
329 sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
330}
331
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100332static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
333 const void *tx_buf, int words, int fs)
334{
335 const u32 *buf_32 = tx_buf;
336 int k;
337
338 for (k = 0; k < words; k++)
339 sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
340}
341
342static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
343 const void *tx_buf, int words, int fs)
344{
345 const u32 *buf_32 = tx_buf;
346 int k;
347
348 for (k = 0; k < words; k++)
349 sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
350}
351
Magnus Damm8051eff2009-11-26 11:10:05 +0000352static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
353 void *rx_buf, int words, int fs)
354{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100355 u8 *buf_8 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000356 int k;
357
358 for (k = 0; k < words; k++)
359 buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
360}
361
362static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
363 void *rx_buf, int words, int fs)
364{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100365 u16 *buf_16 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000366 int k;
367
368 for (k = 0; k < words; k++)
369 buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
370}
371
372static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
373 void *rx_buf, int words, int fs)
374{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100375 u16 *buf_16 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000376 int k;
377
378 for (k = 0; k < words; k++)
379 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
380}
381
382static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
383 void *rx_buf, int words, int fs)
384{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100385 u32 *buf_32 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000386 int k;
387
388 for (k = 0; k < words; k++)
389 buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
390}
391
392static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
393 void *rx_buf, int words, int fs)
394{
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100395 u32 *buf_32 = rx_buf;
Magnus Damm8051eff2009-11-26 11:10:05 +0000396 int k;
397
398 for (k = 0; k < words; k++)
399 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
400}
401
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100402static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
403 void *rx_buf, int words, int fs)
404{
405 u32 *buf_32 = rx_buf;
406 int k;
407
408 for (k = 0; k < words; k++)
409 buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
410}
411
412static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
413 void *rx_buf, int words, int fs)
414{
415 u32 *buf_32 = rx_buf;
416 int k;
417
418 for (k = 0; k < words; k++)
419 put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
420}
421
Magnus Damm8051eff2009-11-26 11:10:05 +0000422static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
423{
424 int bits;
425
426 bits = t ? t->bits_per_word : 0;
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100427 if (!bits)
428 bits = spi->bits_per_word;
Magnus Damm8051eff2009-11-26 11:10:05 +0000429 return bits;
430}
431
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100432static u32 sh_msiof_spi_hz(struct spi_device *spi, struct spi_transfer *t)
Magnus Damm8051eff2009-11-26 11:10:05 +0000433{
Geert Uytterhoeven6a85fc52014-02-20 15:43:02 +0100434 u32 hz;
Magnus Damm8051eff2009-11-26 11:10:05 +0000435
436 hz = t ? t->speed_hz : 0;
Guennadi Liakhovetskie2dbf5e2011-01-21 16:56:37 +0100437 if (!hz)
438 hz = spi->max_speed_hz;
Magnus Damm8051eff2009-11-26 11:10:05 +0000439 return hz;
440}
441
442static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
443 struct spi_transfer *t)
444{
445 int bits;
446
447 /* noting to check hz values against since parent clock is disabled */
448
449 bits = sh_msiof_spi_bits(spi, t);
450 if (bits < 8)
451 return -EINVAL;
452 if (bits > 32)
453 return -EINVAL;
454
455 return spi_bitbang_setup_transfer(spi, t);
456}
457
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100458static int sh_msiof_spi_setup(struct spi_device *spi)
459{
460 struct device_node *np = spi->master->dev.of_node;
Geert Uytterhoevenc833ff72014-02-25 11:21:11 +0100461 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100462
463 if (!np) {
464 /*
465 * Use spi->controller_data for CS (same strategy as spi_gpio),
466 * if any. otherwise let HW control CS
467 */
468 spi->cs_gpio = (uintptr_t)spi->controller_data;
469 }
470
Geert Uytterhoevenc833ff72014-02-25 11:21:11 +0100471 /* Configure pins before deasserting CS */
472 sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
473 !!(spi->mode & SPI_CPHA),
474 !!(spi->mode & SPI_3WIRE),
475 !!(spi->mode & SPI_LSB_FIRST),
476 !!(spi->mode & SPI_CS_HIGH));
477
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100478 return spi_bitbang_setup(spi);
479}
480
Geert Uytterhoevenc833ff72014-02-25 11:21:11 +0100481static int sh_msiof_prepare_message(struct spi_master *master,
482 struct spi_message *msg)
483{
484 struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
485 const struct spi_device *spi = msg->spi;
486
487 pm_runtime_get_sync(&p->pdev->dev);
488 clk_enable(p->clk);
489
490 /* Configure pins before asserting CS */
491 sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
492 !!(spi->mode & SPI_CPHA),
493 !!(spi->mode & SPI_3WIRE),
494 !!(spi->mode & SPI_LSB_FIRST),
495 !!(spi->mode & SPI_CS_HIGH));
496 return 0;
497}
498
499static int sh_msiof_unprepare_message(struct spi_master *master,
500 struct spi_message *msg)
501{
502 struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
503
504 clk_disable(p->clk);
505 pm_runtime_put(&p->pdev->dev);
506 return 0;
507}
508
Magnus Damm8051eff2009-11-26 11:10:05 +0000509static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
510{
Magnus Damm8051eff2009-11-26 11:10:05 +0000511 int value;
512
513 /* chip select is active low unless SPI_CS_HIGH is set */
514 if (spi->mode & SPI_CS_HIGH)
515 value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
516 else
517 value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
518
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100519 if (spi->cs_gpio >= 0)
520 gpio_set_value(spi->cs_gpio, value);
Magnus Damm8051eff2009-11-26 11:10:05 +0000521}
522
523static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
524 void (*tx_fifo)(struct sh_msiof_spi_priv *,
525 const void *, int, int),
526 void (*rx_fifo)(struct sh_msiof_spi_priv *,
527 void *, int, int),
528 const void *tx_buf, void *rx_buf,
529 int words, int bits)
530{
531 int fifo_shift;
532 int ret;
533
534 /* limit maximum word transfer to rx/tx fifo size */
535 if (tx_buf)
536 words = min_t(int, words, p->tx_fifo_size);
537 if (rx_buf)
538 words = min_t(int, words, p->rx_fifo_size);
539
540 /* the fifo contents need shifting */
541 fifo_shift = 32 - bits;
542
543 /* setup msiof transfer mode registers */
544 sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
545
546 /* write tx fifo */
547 if (tx_buf)
548 tx_fifo(p, tx_buf, words, fifo_shift);
549
550 /* setup clock and rx/tx signals */
551 ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
552 if (rx_buf)
553 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
554 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
555
556 /* start by setting frame bit */
Wolfram Sang16735d02013-11-14 14:32:02 -0800557 reinit_completion(&p->done);
Magnus Damm8051eff2009-11-26 11:10:05 +0000558 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
559 if (ret) {
560 dev_err(&p->pdev->dev, "failed to start hardware\n");
561 goto err;
562 }
563
564 /* wait for tx fifo to be emptied / rx fifo to be filled */
565 wait_for_completion(&p->done);
566
567 /* read rx fifo */
568 if (rx_buf)
569 rx_fifo(p, rx_buf, words, fifo_shift);
570
571 /* clear status bits */
572 sh_msiof_reset_str(p);
573
Geert Uytterhoevena669c112014-02-20 15:43:01 +0100574 /* shut down frame, rx/tx and clock signals */
Magnus Damm8051eff2009-11-26 11:10:05 +0000575 ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
576 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
577 if (rx_buf)
578 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
579 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
580 if (ret) {
581 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
582 goto err;
583 }
584
585 return words;
586
587 err:
588 sh_msiof_write(p, IER, 0);
589 return ret;
590}
591
592static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
593{
594 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
595 void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
596 void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
597 int bits;
598 int bytes_per_word;
599 int bytes_done;
600 int words;
601 int n;
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100602 bool swab;
Magnus Damm8051eff2009-11-26 11:10:05 +0000603
604 bits = sh_msiof_spi_bits(spi, t);
605
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100606 if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
607 bits = 32;
608 swab = true;
609 } else {
610 swab = false;
611 }
612
Magnus Damm8051eff2009-11-26 11:10:05 +0000613 /* setup bytes per word and fifo read/write functions */
614 if (bits <= 8) {
615 bytes_per_word = 1;
616 tx_fifo = sh_msiof_spi_write_fifo_8;
617 rx_fifo = sh_msiof_spi_read_fifo_8;
618 } else if (bits <= 16) {
619 bytes_per_word = 2;
620 if ((unsigned long)t->tx_buf & 0x01)
621 tx_fifo = sh_msiof_spi_write_fifo_16u;
622 else
623 tx_fifo = sh_msiof_spi_write_fifo_16;
624
625 if ((unsigned long)t->rx_buf & 0x01)
626 rx_fifo = sh_msiof_spi_read_fifo_16u;
627 else
628 rx_fifo = sh_msiof_spi_read_fifo_16;
Guennadi Liakhovetski9dabb3f2011-01-21 16:56:42 +0100629 } else if (swab) {
630 bytes_per_word = 4;
631 if ((unsigned long)t->tx_buf & 0x03)
632 tx_fifo = sh_msiof_spi_write_fifo_s32u;
633 else
634 tx_fifo = sh_msiof_spi_write_fifo_s32;
635
636 if ((unsigned long)t->rx_buf & 0x03)
637 rx_fifo = sh_msiof_spi_read_fifo_s32u;
638 else
639 rx_fifo = sh_msiof_spi_read_fifo_s32;
Magnus Damm8051eff2009-11-26 11:10:05 +0000640 } else {
641 bytes_per_word = 4;
642 if ((unsigned long)t->tx_buf & 0x03)
643 tx_fifo = sh_msiof_spi_write_fifo_32u;
644 else
645 tx_fifo = sh_msiof_spi_write_fifo_32;
646
647 if ((unsigned long)t->rx_buf & 0x03)
648 rx_fifo = sh_msiof_spi_read_fifo_32u;
649 else
650 rx_fifo = sh_msiof_spi_read_fifo_32;
651 }
652
653 /* setup clocks (clock already enabled in chipselect()) */
654 sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
655 sh_msiof_spi_hz(spi, t));
656
657 /* transfer in fifo sized chunks */
658 words = t->len / bytes_per_word;
659 bytes_done = 0;
660
661 while (bytes_done < t->len) {
Guennadi Liakhovetski8a6afb92011-01-21 16:56:47 +0100662 void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
663 const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
Magnus Damm8051eff2009-11-26 11:10:05 +0000664 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
Guennadi Liakhovetski8a6afb92011-01-21 16:56:47 +0100665 tx_buf,
666 rx_buf,
Magnus Damm8051eff2009-11-26 11:10:05 +0000667 words, bits);
668 if (n < 0)
669 break;
670
671 bytes_done += n * bytes_per_word;
672 words -= n;
673 }
674
675 return bytes_done;
676}
677
678static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
679 u32 word, u8 bits)
680{
681 BUG(); /* unused but needed by bitbang code */
682 return 0;
683}
684
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100685static const struct sh_msiof_chipdata sh_data = {
686 .tx_fifo_size = 64,
687 .rx_fifo_size = 64,
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100688 .master_flags = 0,
689};
690
691static const struct sh_msiof_chipdata r8a779x_data = {
692 .tx_fifo_size = 64,
693 .rx_fifo_size = 256,
694 .master_flags = SPI_MASTER_MUST_TX,
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100695};
696
697static const struct of_device_id sh_msiof_match[] = {
698 { .compatible = "renesas,sh-msiof", .data = &sh_data },
699 { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data },
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100700 { .compatible = "renesas,msiof-r8a7790", .data = &r8a779x_data },
701 { .compatible = "renesas,msiof-r8a7791", .data = &r8a779x_data },
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100702 {},
703};
704MODULE_DEVICE_TABLE(of, sh_msiof_match);
705
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100706#ifdef CONFIG_OF
707static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
708{
709 struct sh_msiof_spi_info *info;
710 struct device_node *np = dev->of_node;
Geert Uytterhoeven32d3b2d2014-02-25 11:21:08 +0100711 u32 num_cs = 1;
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100712
713 info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
714 if (!info) {
715 dev_err(dev, "failed to allocate setup data\n");
716 return NULL;
717 }
718
719 /* Parse the MSIOF properties */
720 of_property_read_u32(np, "num-cs", &num_cs);
721 of_property_read_u32(np, "renesas,tx-fifo-size",
722 &info->tx_fifo_override);
723 of_property_read_u32(np, "renesas,rx-fifo-size",
724 &info->rx_fifo_override);
725
726 info->num_chipselect = num_cs;
727
728 return info;
729}
730#else
731static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
732{
733 return NULL;
734}
735#endif
736
Magnus Damm8051eff2009-11-26 11:10:05 +0000737static int sh_msiof_spi_probe(struct platform_device *pdev)
738{
739 struct resource *r;
740 struct spi_master *master;
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100741 const struct of_device_id *of_id;
Magnus Damm8051eff2009-11-26 11:10:05 +0000742 struct sh_msiof_spi_priv *p;
Magnus Damm8051eff2009-11-26 11:10:05 +0000743 int i;
744 int ret;
745
746 master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
747 if (master == NULL) {
748 dev_err(&pdev->dev, "failed to allocate spi master\n");
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100749 return -ENOMEM;
Magnus Damm8051eff2009-11-26 11:10:05 +0000750 }
751
752 p = spi_master_get_devdata(master);
753
754 platform_set_drvdata(pdev, p);
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100755
756 of_id = of_match_device(sh_msiof_match, &pdev->dev);
757 if (of_id) {
758 p->chipdata = of_id->data;
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100759 p->info = sh_msiof_spi_parse_dt(&pdev->dev);
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100760 } else {
761 p->chipdata = (const void *)pdev->id_entry->driver_data;
Jingoo Han8074cf02013-07-30 16:58:59 +0900762 p->info = dev_get_platdata(&pdev->dev);
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100763 }
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100764
765 if (!p->info) {
766 dev_err(&pdev->dev, "failed to obtain device info\n");
767 ret = -ENXIO;
768 goto err1;
769 }
770
Magnus Damm8051eff2009-11-26 11:10:05 +0000771 init_completion(&p->done);
772
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100773 p->clk = devm_clk_get(&pdev->dev, NULL);
Magnus Damm8051eff2009-11-26 11:10:05 +0000774 if (IS_ERR(p->clk)) {
Bastian Hecht078b6ea2012-11-07 12:40:04 +0100775 dev_err(&pdev->dev, "cannot get clock\n");
Magnus Damm8051eff2009-11-26 11:10:05 +0000776 ret = PTR_ERR(p->clk);
777 goto err1;
778 }
779
Magnus Damm8051eff2009-11-26 11:10:05 +0000780 i = platform_get_irq(pdev, 0);
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100781 if (i < 0) {
782 dev_err(&pdev->dev, "cannot get platform IRQ\n");
Magnus Damm8051eff2009-11-26 11:10:05 +0000783 ret = -ENOENT;
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100784 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000785 }
786
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100787 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
788 p->mapbase = devm_ioremap_resource(&pdev->dev, r);
789 if (IS_ERR(p->mapbase)) {
790 ret = PTR_ERR(p->mapbase);
791 goto err1;
792 }
793
794 ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
795 dev_name(&pdev->dev), p);
Magnus Damm8051eff2009-11-26 11:10:05 +0000796 if (ret) {
797 dev_err(&pdev->dev, "unable to request irq\n");
Laurent Pinchartb4dd05d2013-11-28 02:39:42 +0100798 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000799 }
800
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100801 ret = clk_prepare(p->clk);
802 if (ret < 0) {
803 dev_err(&pdev->dev, "unable to prepare clock\n");
804 goto err1;
Magnus Damm8051eff2009-11-26 11:10:05 +0000805 }
806
807 p->pdev = pdev;
808 pm_runtime_enable(&pdev->dev);
809
Magnus Damm8051eff2009-11-26 11:10:05 +0000810 /* Platform data may override FIFO sizes */
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100811 p->tx_fifo_size = p->chipdata->tx_fifo_size;
812 p->rx_fifo_size = p->chipdata->rx_fifo_size;
Magnus Damm8051eff2009-11-26 11:10:05 +0000813 if (p->info->tx_fifo_override)
814 p->tx_fifo_size = p->info->tx_fifo_override;
815 if (p->info->rx_fifo_override)
816 p->rx_fifo_size = p->info->rx_fifo_override;
817
818 /* init master and bitbang code */
819 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
820 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100821 master->flags = p->chipdata->master_flags;
Magnus Damm8051eff2009-11-26 11:10:05 +0000822 master->bus_num = pdev->id;
Geert Uytterhoevenf7c05e82014-02-20 15:43:00 +0100823 master->dev.of_node = pdev->dev.of_node;
Magnus Damm8051eff2009-11-26 11:10:05 +0000824 master->num_chipselect = p->info->num_chipselect;
Geert Uytterhoeven8d195342014-02-20 15:43:04 +0100825 master->setup = sh_msiof_spi_setup;
Magnus Damm8051eff2009-11-26 11:10:05 +0000826 master->cleanup = spi_bitbang_cleanup;
Geert Uytterhoevenc833ff72014-02-25 11:21:11 +0100827 master->prepare_message = sh_msiof_prepare_message;
828 master->unprepare_message = sh_msiof_unprepare_message;
Magnus Damm8051eff2009-11-26 11:10:05 +0000829
830 p->bitbang.master = master;
831 p->bitbang.chipselect = sh_msiof_spi_chipselect;
832 p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
833 p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
834 p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
835 p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
836 p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
837 p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
838
839 ret = spi_bitbang_start(&p->bitbang);
840 if (ret == 0)
841 return 0;
842
843 pm_runtime_disable(&pdev->dev);
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100844 clk_unprepare(p->clk);
Magnus Damm8051eff2009-11-26 11:10:05 +0000845 err1:
846 spi_master_put(master);
Magnus Damm8051eff2009-11-26 11:10:05 +0000847 return ret;
848}
849
850static int sh_msiof_spi_remove(struct platform_device *pdev)
851{
852 struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
853 int ret;
854
855 ret = spi_bitbang_stop(&p->bitbang);
856 if (!ret) {
857 pm_runtime_disable(&pdev->dev);
Laurent Pinchart5c32d292013-11-28 02:39:43 +0100858 clk_unprepare(p->clk);
Magnus Damm8051eff2009-11-26 11:10:05 +0000859 spi_master_put(p->bitbang.master);
860 }
861 return ret;
862}
863
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100864static struct platform_device_id spi_driver_ids[] = {
865 { "spi_sh_msiof", (kernel_ulong_t)&sh_data },
Geert Uytterhoevenbeb74bb2014-02-25 11:21:10 +0100866 { "spi_r8a7790_msiof", (kernel_ulong_t)&r8a779x_data },
867 { "spi_r8a7791_msiof", (kernel_ulong_t)&r8a779x_data },
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100868 {},
869};
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100870MODULE_DEVICE_TABLE(platform, spi_driver_ids);
Bastian Hechtcf9c86e2012-12-12 12:54:48 +0100871
Magnus Damm8051eff2009-11-26 11:10:05 +0000872static struct platform_driver sh_msiof_spi_drv = {
873 .probe = sh_msiof_spi_probe,
874 .remove = sh_msiof_spi_remove,
Geert Uytterhoeven50a7e232014-02-25 11:21:09 +0100875 .id_table = spi_driver_ids,
Magnus Damm8051eff2009-11-26 11:10:05 +0000876 .driver = {
877 .name = "spi_sh_msiof",
878 .owner = THIS_MODULE,
Sachin Kamat691ee4e2013-03-14 15:31:51 +0530879 .of_match_table = of_match_ptr(sh_msiof_match),
Magnus Damm8051eff2009-11-26 11:10:05 +0000880 },
881};
Grant Likely940ab882011-10-05 11:29:49 -0600882module_platform_driver(sh_msiof_spi_drv);
Magnus Damm8051eff2009-11-26 11:10:05 +0000883
884MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
885MODULE_AUTHOR("Magnus Damm");
886MODULE_LICENSE("GPL v2");
887MODULE_ALIAS("platform:spi_sh_msiof");