spi: txx9: Convert to let spi core handle checking transfer speed
[deliverable/linux.git] / drivers / spi / spi-txx9.c
CommitLineData
f2cac67d 1/*
ca632f55 2 * TXx9 SPI controller driver.
f2cac67d
AN
3 *
4 * Based on linux/arch/mips/tx4938/toshiba_rbtx4938/spi_txx9.c
5 * Copyright (C) 2000-2001 Toshiba Corporation
6 *
7 * 2003-2005 (c) MontaVista Software, Inc. This file is licensed under the
8 * terms of the GNU General Public License version 2. This program is
9 * licensed "as is" without any warranty of any kind, whether express
10 * or implied.
11 *
12 * Support for TX4938 in 2.6 - Manish Lachwani (mlachwani@mvista.com)
13 *
14 * Convert to generic SPI framework - Atsushi Nemoto (anemo@mba.ocn.ne.jp)
15 */
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/errno.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/sched.h>
22#include <linux/spinlock.h>
23#include <linux/workqueue.h>
24#include <linux/spi/spi.h>
25#include <linux/err.h>
26#include <linux/clk.h>
ba0a7f39 27#include <linux/io.h>
d7614de4 28#include <linux/module.h>
e221fa40 29#include <linux/gpio.h>
f2cac67d
AN
30
31
32#define SPI_FIFO_SIZE 4
dbf763a2
AN
33#define SPI_MAX_DIVIDER 0xff /* Max. value for SPCR1.SER */
34#define SPI_MIN_DIVIDER 1 /* Min. value for SPCR1.SER */
f2cac67d
AN
35
36#define TXx9_SPMCR 0x00
37#define TXx9_SPCR0 0x04
38#define TXx9_SPCR1 0x08
39#define TXx9_SPFS 0x0c
40#define TXx9_SPSR 0x14
41#define TXx9_SPDR 0x18
42
43/* SPMCR : SPI Master Control */
44#define TXx9_SPMCR_OPMODE 0xc0
45#define TXx9_SPMCR_CONFIG 0x40
46#define TXx9_SPMCR_ACTIVE 0x80
47#define TXx9_SPMCR_SPSTP 0x02
48#define TXx9_SPMCR_BCLR 0x01
49
50/* SPCR0 : SPI Control 0 */
51#define TXx9_SPCR0_TXIFL_MASK 0xc000
52#define TXx9_SPCR0_RXIFL_MASK 0x3000
53#define TXx9_SPCR0_SIDIE 0x0800
54#define TXx9_SPCR0_SOEIE 0x0400
55#define TXx9_SPCR0_RBSIE 0x0200
56#define TXx9_SPCR0_TBSIE 0x0100
57#define TXx9_SPCR0_IFSPSE 0x0010
58#define TXx9_SPCR0_SBOS 0x0004
59#define TXx9_SPCR0_SPHA 0x0002
60#define TXx9_SPCR0_SPOL 0x0001
61
62/* SPSR : SPI Status */
63#define TXx9_SPSR_TBSI 0x8000
64#define TXx9_SPSR_RBSI 0x4000
65#define TXx9_SPSR_TBS_MASK 0x3800
66#define TXx9_SPSR_RBS_MASK 0x0700
67#define TXx9_SPSR_SPOE 0x0080
68#define TXx9_SPSR_IFSD 0x0008
69#define TXx9_SPSR_SIDLE 0x0004
70#define TXx9_SPSR_STRDY 0x0002
71#define TXx9_SPSR_SRRDY 0x0001
72
73
74struct txx9spi {
75 struct workqueue_struct *workqueue;
76 struct work_struct work;
77 spinlock_t lock; /* protect 'queue' */
78 struct list_head queue;
79 wait_queue_head_t waitq;
80 void __iomem *membase;
f2cac67d
AN
81 int baseclk;
82 struct clk *clk;
f2cac67d
AN
83 int last_chipselect;
84 int last_chipselect_val;
85};
86
87static u32 txx9spi_rd(struct txx9spi *c, int reg)
88{
89 return __raw_readl(c->membase + reg);
90}
91static void txx9spi_wr(struct txx9spi *c, u32 val, int reg)
92{
93 __raw_writel(val, c->membase + reg);
94}
95
96static void txx9spi_cs_func(struct spi_device *spi, struct txx9spi *c,
97 int on, unsigned int cs_delay)
98{
99 int val = (spi->mode & SPI_CS_HIGH) ? on : !on;
100 if (on) {
101 /* deselect the chip with cs_change hint in last transfer */
102 if (c->last_chipselect >= 0)
103 gpio_set_value(c->last_chipselect,
104 !c->last_chipselect_val);
105 c->last_chipselect = spi->chip_select;
106 c->last_chipselect_val = val;
107 } else {
108 c->last_chipselect = -1;
109 ndelay(cs_delay); /* CS Hold Time */
110 }
111 gpio_set_value(spi->chip_select, val);
112 ndelay(cs_delay); /* CS Setup Time / CS Recovery Time */
113}
114
f2cac67d
AN
115static int txx9spi_setup(struct spi_device *spi)
116{
117 struct txx9spi *c = spi_master_get_devdata(spi->master);
f2cac67d 118
425f96d2 119 if (!spi->max_speed_hz)
f2cac67d
AN
120 return -EINVAL;
121
f2cac67d
AN
122 if (gpio_direction_output(spi->chip_select,
123 !(spi->mode & SPI_CS_HIGH))) {
124 dev_err(&spi->dev, "Cannot setup GPIO for chipselect.\n");
125 return -EINVAL;
126 }
127
128 /* deselect chip */
129 spin_lock(&c->lock);
130 txx9spi_cs_func(spi, c, 0, (NSEC_PER_SEC / 2) / spi->max_speed_hz);
131 spin_unlock(&c->lock);
132
133 return 0;
134}
135
136static irqreturn_t txx9spi_interrupt(int irq, void *dev_id)
137{
138 struct txx9spi *c = dev_id;
139
140 /* disable rx intr */
141 txx9spi_wr(c, txx9spi_rd(c, TXx9_SPCR0) & ~TXx9_SPCR0_RBSIE,
142 TXx9_SPCR0);
143 wake_up(&c->waitq);
144 return IRQ_HANDLED;
145}
146
147static void txx9spi_work_one(struct txx9spi *c, struct spi_message *m)
148{
149 struct spi_device *spi = m->spi;
150 struct spi_transfer *t;
151 unsigned int cs_delay;
152 unsigned int cs_change = 1;
153 int status = 0;
154 u32 mcr;
155 u32 prev_speed_hz = 0;
156 u8 prev_bits_per_word = 0;
157
158 /* CS setup/hold/recovery time in nsec */
159 cs_delay = 100 + (NSEC_PER_SEC / 2) / spi->max_speed_hz;
160
161 mcr = txx9spi_rd(c, TXx9_SPMCR);
162 if (unlikely((mcr & TXx9_SPMCR_OPMODE) == TXx9_SPMCR_ACTIVE)) {
163 dev_err(&spi->dev, "Bad mode.\n");
164 status = -EIO;
165 goto exit;
166 }
167 mcr &= ~(TXx9_SPMCR_OPMODE | TXx9_SPMCR_SPSTP | TXx9_SPMCR_BCLR);
168
169 /* enter config mode */
170 txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
171 txx9spi_wr(c, TXx9_SPCR0_SBOS
172 | ((spi->mode & SPI_CPOL) ? TXx9_SPCR0_SPOL : 0)
173 | ((spi->mode & SPI_CPHA) ? TXx9_SPCR0_SPHA : 0)
174 | 0x08,
175 TXx9_SPCR0);
176
256fbf36 177 list_for_each_entry(t, &m->transfers, transfer_list) {
f2cac67d
AN
178 const void *txbuf = t->tx_buf;
179 void *rxbuf = t->rx_buf;
180 u32 data;
181 unsigned int len = t->len;
182 unsigned int wsize;
183 u32 speed_hz = t->speed_hz ? : spi->max_speed_hz;
766ed704 184 u8 bits_per_word = t->bits_per_word;
f2cac67d 185
f2cac67d
AN
186 wsize = bits_per_word >> 3; /* in bytes */
187
188 if (prev_speed_hz != speed_hz
189 || prev_bits_per_word != bits_per_word) {
dbf763a2
AN
190 int n = DIV_ROUND_UP(c->baseclk, speed_hz) - 1;
191 n = clamp(n, SPI_MIN_DIVIDER, SPI_MAX_DIVIDER);
f2cac67d
AN
192 /* enter config mode */
193 txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR,
194 TXx9_SPMCR);
195 txx9spi_wr(c, (n << 8) | bits_per_word, TXx9_SPCR1);
196 /* enter active mode */
197 txx9spi_wr(c, mcr | TXx9_SPMCR_ACTIVE, TXx9_SPMCR);
198
199 prev_speed_hz = speed_hz;
200 prev_bits_per_word = bits_per_word;
201 }
202
203 if (cs_change)
204 txx9spi_cs_func(spi, c, 1, cs_delay);
205 cs_change = t->cs_change;
206 while (len) {
207 unsigned int count = SPI_FIFO_SIZE;
208 int i;
209 u32 cr0;
210
211 if (len < count * wsize)
212 count = len / wsize;
213 /* now tx must be idle... */
214 while (!(txx9spi_rd(c, TXx9_SPSR) & TXx9_SPSR_SIDLE))
215 cpu_relax();
216 cr0 = txx9spi_rd(c, TXx9_SPCR0);
217 cr0 &= ~TXx9_SPCR0_RXIFL_MASK;
218 cr0 |= (count - 1) << 12;
219 /* enable rx intr */
220 cr0 |= TXx9_SPCR0_RBSIE;
221 txx9spi_wr(c, cr0, TXx9_SPCR0);
222 /* send */
223 for (i = 0; i < count; i++) {
224 if (txbuf) {
225 data = (wsize == 1)
226 ? *(const u8 *)txbuf
227 : *(const u16 *)txbuf;
228 txx9spi_wr(c, data, TXx9_SPDR);
229 txbuf += wsize;
230 } else
231 txx9spi_wr(c, 0, TXx9_SPDR);
232 }
233 /* wait all rx data */
234 wait_event(c->waitq,
235 txx9spi_rd(c, TXx9_SPSR) & TXx9_SPSR_RBSI);
236 /* receive */
237 for (i = 0; i < count; i++) {
238 data = txx9spi_rd(c, TXx9_SPDR);
239 if (rxbuf) {
240 if (wsize == 1)
241 *(u8 *)rxbuf = data;
242 else
243 *(u16 *)rxbuf = data;
244 rxbuf += wsize;
245 }
246 }
247 len -= count * wsize;
248 }
249 m->actual_length += t->len;
250 if (t->delay_usecs)
251 udelay(t->delay_usecs);
252
253 if (!cs_change)
254 continue;
255 if (t->transfer_list.next == &m->transfers)
256 break;
257 /* sometimes a short mid-message deselect of the chip
258 * may be needed to terminate a mode or command
259 */
260 txx9spi_cs_func(spi, c, 0, cs_delay);
261 }
262
263exit:
264 m->status = status;
265 m->complete(m->context);
266
267 /* normally deactivate chipselect ... unless no error and
268 * cs_change has hinted that the next message will probably
269 * be for this chip too.
270 */
271 if (!(status == 0 && cs_change))
272 txx9spi_cs_func(spi, c, 0, cs_delay);
273
274 /* enter config mode */
275 txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
276}
277
278static void txx9spi_work(struct work_struct *work)
279{
280 struct txx9spi *c = container_of(work, struct txx9spi, work);
281 unsigned long flags;
282
283 spin_lock_irqsave(&c->lock, flags);
284 while (!list_empty(&c->queue)) {
285 struct spi_message *m;
286
287 m = container_of(c->queue.next, struct spi_message, queue);
288 list_del_init(&m->queue);
289 spin_unlock_irqrestore(&c->lock, flags);
290
291 txx9spi_work_one(c, m);
292
293 spin_lock_irqsave(&c->lock, flags);
294 }
295 spin_unlock_irqrestore(&c->lock, flags);
296}
297
298static int txx9spi_transfer(struct spi_device *spi, struct spi_message *m)
299{
300 struct spi_master *master = spi->master;
301 struct txx9spi *c = spi_master_get_devdata(master);
302 struct spi_transfer *t;
303 unsigned long flags;
304
305 m->actual_length = 0;
306
307 /* check each transfer's parameters */
256fbf36 308 list_for_each_entry(t, &m->transfers, transfer_list) {
766ed704 309 u8 bits_per_word = t->bits_per_word;
f2cac67d 310
f2cac67d
AN
311 if (!t->tx_buf && !t->rx_buf && t->len)
312 return -EINVAL;
f2cac67d
AN
313 if (t->len & ((bits_per_word >> 3) - 1))
314 return -EINVAL;
f2cac67d
AN
315 }
316
317 spin_lock_irqsave(&c->lock, flags);
318 list_add_tail(&m->queue, &c->queue);
319 queue_work(c->workqueue, &c->work);
320 spin_unlock_irqrestore(&c->lock, flags);
321
322 return 0;
323}
324
2deff8d6 325static int txx9spi_probe(struct platform_device *dev)
f2cac67d
AN
326{
327 struct spi_master *master;
328 struct txx9spi *c;
329 struct resource *res;
330 int ret = -ENODEV;
331 u32 mcr;
ba0a7f39 332 int irq;
f2cac67d
AN
333
334 master = spi_alloc_master(&dev->dev, sizeof(*c));
335 if (!master)
336 return ret;
337 c = spi_master_get_devdata(master);
f2cac67d
AN
338 platform_set_drvdata(dev, master);
339
340 INIT_WORK(&c->work, txx9spi_work);
341 spin_lock_init(&c->lock);
342 INIT_LIST_HEAD(&c->queue);
343 init_waitqueue_head(&c->waitq);
344
18e34d56 345 c->clk = devm_clk_get(&dev->dev, "spi-baseclk");
f2cac67d
AN
346 if (IS_ERR(c->clk)) {
347 ret = PTR_ERR(c->clk);
348 c->clk = NULL;
349 goto exit;
350 }
351 ret = clk_enable(c->clk);
352 if (ret) {
f2cac67d
AN
353 c->clk = NULL;
354 goto exit;
355 }
356 c->baseclk = clk_get_rate(c->clk);
425f96d2
AL
357 master->min_speed_hz = DIV_ROUND_UP(c->baseclk, SPI_MAX_DIVIDER + 1);
358 master->max_speed_hz = c->baseclk / (SPI_MIN_DIVIDER + 1);
f2cac67d
AN
359
360 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
361 if (!res)
ba0a7f39 362 goto exit_busy;
d53342bf 363 if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
ba0a7f39
AN
364 "spi_txx9"))
365 goto exit_busy;
d53342bf 366 c->membase = devm_ioremap(&dev->dev, res->start, resource_size(res));
f2cac67d 367 if (!c->membase)
ba0a7f39 368 goto exit_busy;
f2cac67d
AN
369
370 /* enter config mode */
371 mcr = txx9spi_rd(c, TXx9_SPMCR);
372 mcr &= ~(TXx9_SPMCR_OPMODE | TXx9_SPMCR_SPSTP | TXx9_SPMCR_BCLR);
373 txx9spi_wr(c, mcr | TXx9_SPMCR_CONFIG | TXx9_SPMCR_BCLR, TXx9_SPMCR);
374
ba0a7f39
AN
375 irq = platform_get_irq(dev, 0);
376 if (irq < 0)
377 goto exit_busy;
378 ret = devm_request_irq(&dev->dev, irq, txx9spi_interrupt, 0,
379 "spi_txx9", c);
380 if (ret)
f2cac67d 381 goto exit;
f2cac67d 382
6c7377ab
KS
383 c->workqueue = create_singlethread_workqueue(
384 dev_name(master->dev.parent));
f2cac67d 385 if (!c->workqueue)
ba0a7f39 386 goto exit_busy;
f2cac67d
AN
387 c->last_chipselect = -1;
388
389 dev_info(&dev->dev, "at %#llx, irq %d, %dMHz\n",
ba0a7f39 390 (unsigned long long)res->start, irq,
f2cac67d
AN
391 (c->baseclk + 500000) / 1000000);
392
e7db06b5
DB
393 /* the spi->mode bits understood by this driver: */
394 master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
395
f2cac67d
AN
396 master->bus_num = dev->id;
397 master->setup = txx9spi_setup;
398 master->transfer = txx9spi_transfer;
399 master->num_chipselect = (u16)UINT_MAX; /* any GPIO numbers */
24778be2 400 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
f2cac67d 401
2fe7e4ad 402 ret = devm_spi_register_master(&dev->dev, master);
f2cac67d
AN
403 if (ret)
404 goto exit;
405 return 0;
ba0a7f39
AN
406exit_busy:
407 ret = -EBUSY;
f2cac67d
AN
408exit:
409 if (c->workqueue)
410 destroy_workqueue(c->workqueue);
18e34d56 411 if (c->clk)
f2cac67d 412 clk_disable(c->clk);
f2cac67d
AN
413 spi_master_put(master);
414 return ret;
415}
416
2deff8d6 417static int txx9spi_remove(struct platform_device *dev)
f2cac67d 418{
b38f87ec 419 struct spi_master *master = platform_get_drvdata(dev);
f2cac67d
AN
420 struct txx9spi *c = spi_master_get_devdata(master);
421
f2cac67d 422 destroy_workqueue(c->workqueue);
f2cac67d 423 clk_disable(c->clk);
f2cac67d
AN
424 return 0;
425}
426
7e38c3c4
KS
427/* work with hotplug and coldplug */
428MODULE_ALIAS("platform:spi_txx9");
429
f2cac67d 430static struct platform_driver txx9spi_driver = {
1d82d0c2 431 .probe = txx9spi_probe,
2deff8d6 432 .remove = txx9spi_remove,
f2cac67d 433 .driver = {
4ccdb4c8 434 .name = "spi_txx9",
f2cac67d
AN
435 .owner = THIS_MODULE,
436 },
437};
438
439static int __init txx9spi_init(void)
440{
1d82d0c2 441 return platform_driver_register(&txx9spi_driver);
f2cac67d
AN
442}
443subsys_initcall(txx9spi_init);
444
445static void __exit txx9spi_exit(void)
446{
447 platform_driver_unregister(&txx9spi_driver);
448}
449module_exit(txx9spi_exit);
450
451MODULE_DESCRIPTION("TXx9 SPI Driver");
452MODULE_LICENSE("GPL");
This page took 0.569535 seconds and 5 git commands to generate.