spi: Always check complete callback before calling it
[firefly-linux-kernel-4.4.55.git] / drivers / spi / spi-mpc512x-psc.c
1 /*
2  * MPC512x PSC in SPI mode driver.
3  *
4  * Copyright (C) 2007,2008 Freescale Semiconductor Inc.
5  * Original port from 52xx driver:
6  *      Hongjun Chen <hong-jun.chen@freescale.com>
7  *
8  * Fork of mpc52xx_psc_spi.c:
9  *      Copyright (C) 2006 TOPTICA Photonics AG., Dragos Carp
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/interrupt.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_platform.h>
25 #include <linux/completion.h>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/clk.h>
29 #include <linux/spi/spi.h>
30 #include <linux/fsl_devices.h>
31 #include <linux/gpio.h>
32 #include <asm/mpc52xx_psc.h>
33
34 struct mpc512x_psc_spi {
35         void (*cs_control)(struct spi_device *spi, bool on);
36
37         /* driver internal data */
38         struct mpc52xx_psc __iomem *psc;
39         struct mpc512x_psc_fifo __iomem *fifo;
40         unsigned int irq;
41         u8 bits_per_word;
42         struct clk *clk_mclk;
43         struct clk *clk_ipg;
44         u32 mclk_rate;
45
46         struct completion txisrdone;
47 };
48
49 /* controller state */
50 struct mpc512x_psc_spi_cs {
51         int bits_per_word;
52         int speed_hz;
53 };
54
55 /* set clock freq, clock ramp, bits per work
56  * if t is NULL then reset the values to the default values
57  */
58 static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi,
59                                           struct spi_transfer *t)
60 {
61         struct mpc512x_psc_spi_cs *cs = spi->controller_state;
62
63         cs->speed_hz = (t && t->speed_hz)
64             ? t->speed_hz : spi->max_speed_hz;
65         cs->bits_per_word = (t && t->bits_per_word)
66             ? t->bits_per_word : spi->bits_per_word;
67         cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
68         return 0;
69 }
70
71 static void mpc512x_psc_spi_activate_cs(struct spi_device *spi)
72 {
73         struct mpc512x_psc_spi_cs *cs = spi->controller_state;
74         struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
75         struct mpc52xx_psc __iomem *psc = mps->psc;
76         u32 sicr;
77         u32 ccr;
78         int speed;
79         u16 bclkdiv;
80
81         sicr = in_be32(&psc->sicr);
82
83         /* Set clock phase and polarity */
84         if (spi->mode & SPI_CPHA)
85                 sicr |= 0x00001000;
86         else
87                 sicr &= ~0x00001000;
88
89         if (spi->mode & SPI_CPOL)
90                 sicr |= 0x00002000;
91         else
92                 sicr &= ~0x00002000;
93
94         if (spi->mode & SPI_LSB_FIRST)
95                 sicr |= 0x10000000;
96         else
97                 sicr &= ~0x10000000;
98         out_be32(&psc->sicr, sicr);
99
100         ccr = in_be32(&psc->ccr);
101         ccr &= 0xFF000000;
102         speed = cs->speed_hz;
103         if (!speed)
104                 speed = 1000000;        /* default 1MHz */
105         bclkdiv = (mps->mclk_rate / speed) - 1;
106
107         ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
108         out_be32(&psc->ccr, ccr);
109         mps->bits_per_word = cs->bits_per_word;
110
111         if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
112                 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
113 }
114
115 static void mpc512x_psc_spi_deactivate_cs(struct spi_device *spi)
116 {
117         struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
118
119         if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
120                 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
121
122 }
123
124 /* extract and scale size field in txsz or rxsz */
125 #define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2);
126
127 #define EOFBYTE 1
128
129 static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi,
130                                          struct spi_transfer *t)
131 {
132         struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
133         struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
134         size_t tx_len = t->len;
135         size_t rx_len = t->len;
136         u8 *tx_buf = (u8 *)t->tx_buf;
137         u8 *rx_buf = (u8 *)t->rx_buf;
138
139         if (!tx_buf && !rx_buf && t->len)
140                 return -EINVAL;
141
142         while (rx_len || tx_len) {
143                 size_t txcount;
144                 u8 data;
145                 size_t fifosz;
146                 size_t rxcount;
147                 int rxtries;
148
149                 /*
150                  * send the TX bytes in as large a chunk as possible
151                  * but neither exceed the TX nor the RX FIFOs
152                  */
153                 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
154                 txcount = min(fifosz, tx_len);
155                 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->rxsz));
156                 fifosz -= in_be32(&fifo->rxcnt) + 1;
157                 txcount = min(fifosz, txcount);
158                 if (txcount) {
159
160                         /* fill the TX FIFO */
161                         while (txcount-- > 0) {
162                                 data = tx_buf ? *tx_buf++ : 0;
163                                 if (tx_len == EOFBYTE && t->cs_change)
164                                         setbits32(&fifo->txcmd,
165                                                   MPC512x_PSC_FIFO_EOF);
166                                 out_8(&fifo->txdata_8, data);
167                                 tx_len--;
168                         }
169
170                         /* have the ISR trigger when the TX FIFO is empty */
171                         reinit_completion(&mps->txisrdone);
172                         out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
173                         out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
174                         wait_for_completion(&mps->txisrdone);
175                 }
176
177                 /*
178                  * consume as much RX data as the FIFO holds, while we
179                  * iterate over the transfer's TX data length
180                  *
181                  * only insist in draining all the remaining RX bytes
182                  * when the TX bytes were exhausted (that's at the very
183                  * end of this transfer, not when still iterating over
184                  * the transfer's chunks)
185                  */
186                 rxtries = 50;
187                 do {
188
189                         /*
190                          * grab whatever was in the FIFO when we started
191                          * looking, don't bother fetching what was added to
192                          * the FIFO while we read from it -- we'll return
193                          * here eventually and prefer sending out remaining
194                          * TX data
195                          */
196                         fifosz = in_be32(&fifo->rxcnt);
197                         rxcount = min(fifosz, rx_len);
198                         while (rxcount-- > 0) {
199                                 data = in_8(&fifo->rxdata_8);
200                                 if (rx_buf)
201                                         *rx_buf++ = data;
202                                 rx_len--;
203                         }
204
205                         /*
206                          * come back later if there still is TX data to send,
207                          * bail out of the RX drain loop if all of the TX data
208                          * was sent and all of the RX data was received (i.e.
209                          * when the transmission has completed)
210                          */
211                         if (tx_len)
212                                 break;
213                         if (!rx_len)
214                                 break;
215
216                         /*
217                          * TX data transmission has completed while RX data
218                          * is still pending -- that's a transient situation
219                          * which depends on wire speed and specific
220                          * hardware implementation details (buffering) yet
221                          * should resolve very quickly
222                          *
223                          * just yield for a moment to not hog the CPU for
224                          * too long when running SPI at low speed
225                          *
226                          * the timeout range is rather arbitrary and tries
227                          * to balance throughput against system load; the
228                          * chosen values result in a minimal timeout of 50
229                          * times 10us and thus work at speeds as low as
230                          * some 20kbps, while the maximum timeout at the
231                          * transfer's end could be 5ms _if_ nothing else
232                          * ticks in the system _and_ RX data still wasn't
233                          * received, which only occurs in situations that
234                          * are exceptional; removing the unpredictability
235                          * of the timeout either decreases throughput
236                          * (longer timeouts), or puts more load on the
237                          * system (fixed short timeouts) or requires the
238                          * use of a timeout API instead of a counter and an
239                          * unknown inner delay
240                          */
241                         usleep_range(10, 100);
242
243                 } while (--rxtries > 0);
244                 if (!tx_len && rx_len && !rxtries) {
245                         /*
246                          * not enough RX bytes even after several retries
247                          * and the resulting rather long timeout?
248                          */
249                         rxcount = in_be32(&fifo->rxcnt);
250                         dev_warn(&spi->dev,
251                                  "short xfer, missing %zd RX bytes, FIFO level %zd\n",
252                                  rx_len, rxcount);
253                 }
254
255                 /*
256                  * drain and drop RX data which "should not be there" in
257                  * the first place, for undisturbed transmission this turns
258                  * into a NOP (except for the FIFO level fetch)
259                  */
260                 if (!tx_len && !rx_len) {
261                         while (in_be32(&fifo->rxcnt))
262                                 in_8(&fifo->rxdata_8);
263                 }
264
265         }
266         return 0;
267 }
268
269 static int mpc512x_psc_spi_msg_xfer(struct spi_master *master,
270                                     struct spi_message *m)
271 {
272         struct spi_device *spi;
273         unsigned cs_change;
274         int status;
275         struct spi_transfer *t;
276
277         spi = m->spi;
278         cs_change = 1;
279         status = 0;
280         list_for_each_entry(t, &m->transfers, transfer_list) {
281                 if (t->bits_per_word || t->speed_hz) {
282                         status = mpc512x_psc_spi_transfer_setup(spi, t);
283                         if (status < 0)
284                                 break;
285                 }
286
287                 if (cs_change)
288                         mpc512x_psc_spi_activate_cs(spi);
289                 cs_change = t->cs_change;
290
291                 status = mpc512x_psc_spi_transfer_rxtx(spi, t);
292                 if (status)
293                         break;
294                 m->actual_length += t->len;
295
296                 if (t->delay_usecs)
297                         udelay(t->delay_usecs);
298
299                 if (cs_change)
300                         mpc512x_psc_spi_deactivate_cs(spi);
301         }
302
303         m->status = status;
304         if (m->complete)
305                 m->complete(m->context);
306
307         if (status || !cs_change)
308                 mpc512x_psc_spi_deactivate_cs(spi);
309
310         mpc512x_psc_spi_transfer_setup(spi, NULL);
311
312         spi_finalize_current_message(master);
313         return status;
314 }
315
316 static int mpc512x_psc_spi_prep_xfer_hw(struct spi_master *master)
317 {
318         struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
319         struct mpc52xx_psc __iomem *psc = mps->psc;
320
321         dev_dbg(&master->dev, "%s()\n", __func__);
322
323         /* Zero MR2 */
324         in_8(&psc->mode);
325         out_8(&psc->mode, 0x0);
326
327         /* enable transmitter/receiver */
328         out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
329
330         return 0;
331 }
332
333 static int mpc512x_psc_spi_unprep_xfer_hw(struct spi_master *master)
334 {
335         struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
336         struct mpc52xx_psc __iomem *psc = mps->psc;
337         struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
338
339         dev_dbg(&master->dev, "%s()\n", __func__);
340
341         /* disable transmitter/receiver and fifo interrupt */
342         out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
343         out_be32(&fifo->tximr, 0);
344
345         return 0;
346 }
347
348 static int mpc512x_psc_spi_setup(struct spi_device *spi)
349 {
350         struct mpc512x_psc_spi_cs *cs = spi->controller_state;
351         int ret;
352
353         if (spi->bits_per_word % 8)
354                 return -EINVAL;
355
356         if (!cs) {
357                 cs = kzalloc(sizeof *cs, GFP_KERNEL);
358                 if (!cs)
359                         return -ENOMEM;
360
361                 if (gpio_is_valid(spi->cs_gpio)) {
362                         ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
363                         if (ret) {
364                                 dev_err(&spi->dev, "can't get CS gpio: %d\n",
365                                         ret);
366                                 kfree(cs);
367                                 return ret;
368                         }
369                         gpio_direction_output(spi->cs_gpio,
370                                         spi->mode & SPI_CS_HIGH ? 0 : 1);
371                 }
372
373                 spi->controller_state = cs;
374         }
375
376         cs->bits_per_word = spi->bits_per_word;
377         cs->speed_hz = spi->max_speed_hz;
378
379         return 0;
380 }
381
382 static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
383 {
384         if (gpio_is_valid(spi->cs_gpio))
385                 gpio_free(spi->cs_gpio);
386         kfree(spi->controller_state);
387 }
388
389 static int mpc512x_psc_spi_port_config(struct spi_master *master,
390                                        struct mpc512x_psc_spi *mps)
391 {
392         struct mpc52xx_psc __iomem *psc = mps->psc;
393         struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
394         u32 sicr;
395         u32 ccr;
396         int speed;
397         u16 bclkdiv;
398
399         /* Reset the PSC into a known state */
400         out_8(&psc->command, MPC52xx_PSC_RST_RX);
401         out_8(&psc->command, MPC52xx_PSC_RST_TX);
402         out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
403
404         /* Disable psc interrupts all useful interrupts are in fifo */
405         out_be16(&psc->isr_imr.imr, 0);
406
407         /* Disable fifo interrupts, will be enabled later */
408         out_be32(&fifo->tximr, 0);
409         out_be32(&fifo->rximr, 0);
410
411         /* Setup fifo slice address and size */
412         /*out_be32(&fifo->txsz, 0x0fe00004);*/
413         /*out_be32(&fifo->rxsz, 0x0ff00004);*/
414
415         sicr =  0x01000000 |    /* SIM = 0001 -- 8 bit */
416                 0x00800000 |    /* GenClk = 1 -- internal clk */
417                 0x00008000 |    /* SPI = 1 */
418                 0x00004000 |    /* MSTR = 1   -- SPI master */
419                 0x00000800;     /* UseEOF = 1 -- SS low until EOF */
420
421         out_be32(&psc->sicr, sicr);
422
423         ccr = in_be32(&psc->ccr);
424         ccr &= 0xFF000000;
425         speed = 1000000;        /* default 1MHz */
426         bclkdiv = (mps->mclk_rate / speed) - 1;
427         ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
428         out_be32(&psc->ccr, ccr);
429
430         /* Set 2ms DTL delay */
431         out_8(&psc->ctur, 0x00);
432         out_8(&psc->ctlr, 0x82);
433
434         /* we don't use the alarms */
435         out_be32(&fifo->rxalarm, 0xfff);
436         out_be32(&fifo->txalarm, 0);
437
438         /* Enable FIFO slices for Rx/Tx */
439         out_be32(&fifo->rxcmd,
440                  MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
441         out_be32(&fifo->txcmd,
442                  MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
443
444         mps->bits_per_word = 8;
445
446         return 0;
447 }
448
449 static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
450 {
451         struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
452         struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
453
454         /* clear interrupt and wake up the rx/tx routine */
455         if (in_be32(&fifo->txisr) &
456             in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
457                 out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
458                 out_be32(&fifo->tximr, 0);
459                 complete(&mps->txisrdone);
460                 return IRQ_HANDLED;
461         }
462         return IRQ_NONE;
463 }
464
465 static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff)
466 {
467         gpio_set_value(spi->cs_gpio, onoff);
468 }
469
470 /* bus_num is used only for the case dev->platform_data == NULL */
471 static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
472                                               u32 size, unsigned int irq,
473                                               s16 bus_num)
474 {
475         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
476         struct mpc512x_psc_spi *mps;
477         struct spi_master *master;
478         int ret;
479         void *tempp;
480         struct clk *clk;
481
482         master = spi_alloc_master(dev, sizeof *mps);
483         if (master == NULL)
484                 return -ENOMEM;
485
486         dev_set_drvdata(dev, master);
487         mps = spi_master_get_devdata(master);
488         mps->irq = irq;
489
490         if (pdata == NULL) {
491                 mps->cs_control = mpc512x_spi_cs_control;
492                 master->bus_num = bus_num;
493         } else {
494                 mps->cs_control = pdata->cs_control;
495                 master->bus_num = pdata->bus_num;
496                 master->num_chipselect = pdata->max_chipselect;
497         }
498
499         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
500         master->setup = mpc512x_psc_spi_setup;
501         master->prepare_transfer_hardware = mpc512x_psc_spi_prep_xfer_hw;
502         master->transfer_one_message = mpc512x_psc_spi_msg_xfer;
503         master->unprepare_transfer_hardware = mpc512x_psc_spi_unprep_xfer_hw;
504         master->cleanup = mpc512x_psc_spi_cleanup;
505         master->dev.of_node = dev->of_node;
506
507         tempp = devm_ioremap(dev, regaddr, size);
508         if (!tempp) {
509                 dev_err(dev, "could not ioremap I/O port range\n");
510                 ret = -EFAULT;
511                 goto free_master;
512         }
513         mps->psc = tempp;
514         mps->fifo =
515                 (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
516         ret = devm_request_irq(dev, mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
517                                 "mpc512x-psc-spi", mps);
518         if (ret)
519                 goto free_master;
520         init_completion(&mps->txisrdone);
521
522         clk = devm_clk_get(dev, "mclk");
523         if (IS_ERR(clk)) {
524                 ret = PTR_ERR(clk);
525                 goto free_master;
526         }
527         ret = clk_prepare_enable(clk);
528         if (ret)
529                 goto free_master;
530         mps->clk_mclk = clk;
531         mps->mclk_rate = clk_get_rate(clk);
532
533         clk = devm_clk_get(dev, "ipg");
534         if (IS_ERR(clk)) {
535                 ret = PTR_ERR(clk);
536                 goto free_mclk_clock;
537         }
538         ret = clk_prepare_enable(clk);
539         if (ret)
540                 goto free_mclk_clock;
541         mps->clk_ipg = clk;
542
543         ret = mpc512x_psc_spi_port_config(master, mps);
544         if (ret < 0)
545                 goto free_ipg_clock;
546
547         ret = devm_spi_register_master(dev, master);
548         if (ret < 0)
549                 goto free_ipg_clock;
550
551         return ret;
552
553 free_ipg_clock:
554         clk_disable_unprepare(mps->clk_ipg);
555 free_mclk_clock:
556         clk_disable_unprepare(mps->clk_mclk);
557 free_master:
558         spi_master_put(master);
559
560         return ret;
561 }
562
563 static int mpc512x_psc_spi_do_remove(struct device *dev)
564 {
565         struct spi_master *master = dev_get_drvdata(dev);
566         struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
567
568         clk_disable_unprepare(mps->clk_mclk);
569         clk_disable_unprepare(mps->clk_ipg);
570
571         return 0;
572 }
573
574 static int mpc512x_psc_spi_of_probe(struct platform_device *op)
575 {
576         const u32 *regaddr_p;
577         u64 regaddr64, size64;
578         s16 id = -1;
579
580         regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
581         if (!regaddr_p) {
582                 dev_err(&op->dev, "Invalid PSC address\n");
583                 return -EINVAL;
584         }
585         regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
586
587         /* get PSC id (0..11, used by port_config) */
588         id = of_alias_get_id(op->dev.of_node, "spi");
589         if (id < 0) {
590                 dev_err(&op->dev, "no alias id for %s\n",
591                         op->dev.of_node->full_name);
592                 return id;
593         }
594
595         return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
596                                 irq_of_parse_and_map(op->dev.of_node, 0), id);
597 }
598
599 static int mpc512x_psc_spi_of_remove(struct platform_device *op)
600 {
601         return mpc512x_psc_spi_do_remove(&op->dev);
602 }
603
604 static struct of_device_id mpc512x_psc_spi_of_match[] = {
605         { .compatible = "fsl,mpc5121-psc-spi", },
606         {},
607 };
608
609 MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
610
611 static struct platform_driver mpc512x_psc_spi_of_driver = {
612         .probe = mpc512x_psc_spi_of_probe,
613         .remove = mpc512x_psc_spi_of_remove,
614         .driver = {
615                 .name = "mpc512x-psc-spi",
616                 .owner = THIS_MODULE,
617                 .of_match_table = mpc512x_psc_spi_of_match,
618         },
619 };
620 module_platform_driver(mpc512x_psc_spi_of_driver);
621
622 MODULE_AUTHOR("John Rigby");
623 MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
624 MODULE_LICENSE("GPL");