6d114daa178a35919ea30e21627b181d7789c8a2
[linux-block.git] / drivers / spi / spi-fsl-spi.c
1 /*
2  * Freescale SPI controller driver.
3  *
4  * Maintainer: Kumar Gala
5  *
6  * Copyright (C) 2006 Polycom, Inc.
7  * Copyright 2010 Freescale Semiconductor, Inc.
8  *
9  * CPM SPI and QE buffer descriptors mode support:
10  * Copyright (c) 2009  MontaVista Software, Inc.
11  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
12  *
13  * GRLIB support:
14  * Copyright (c) 2012 Aeroflex Gaisler AB.
15  * Author: Andreas Larsson <andreas@gaisler.com>
16  *
17  * This program is free software; you can redistribute  it and/or modify it
18  * under  the terms of  the GNU General  Public License as published by the
19  * Free Software Foundation;  either version 2 of the  License, or (at your
20  * option) any later version.
21  */
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/fsl_devices.h>
25 #include <linux/gpio.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/kernel.h>
29 #include <linux/mm.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
32 #include <linux/of.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_gpio.h>
36 #include <linux/of_platform.h>
37 #include <linux/platform_device.h>
38 #include <linux/spi/spi.h>
39 #include <linux/spi/spi_bitbang.h>
40 #include <linux/types.h>
41
42 #ifdef CONFIG_FSL_SOC
43 #include <sysdev/fsl_soc.h>
44 #endif
45
46 /* Specific to the MPC8306/MPC8309 */
47 #define IMMR_SPI_CS_OFFSET 0x14c
48 #define SPI_BOOT_SEL_BIT   0x80000000
49
50 #include "spi-fsl-lib.h"
51 #include "spi-fsl-cpm.h"
52 #include "spi-fsl-spi.h"
53
54 #define TYPE_FSL        0
55 #define TYPE_GRLIB      1
56
57 struct fsl_spi_match_data {
58         int type;
59 };
60
61 static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
62         .type = TYPE_FSL,
63 };
64
65 static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
66         .type = TYPE_GRLIB,
67 };
68
69 static const struct of_device_id of_fsl_spi_match[] = {
70         {
71                 .compatible = "fsl,spi",
72                 .data = &of_fsl_spi_fsl_config,
73         },
74         {
75                 .compatible = "aeroflexgaisler,spictrl",
76                 .data = &of_fsl_spi_grlib_config,
77         },
78         {}
79 };
80 MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
81
82 static int fsl_spi_get_type(struct device *dev)
83 {
84         const struct of_device_id *match;
85
86         if (dev->of_node) {
87                 match = of_match_node(of_fsl_spi_match, dev->of_node);
88                 if (match && match->data)
89                         return ((struct fsl_spi_match_data *)match->data)->type;
90         }
91         return TYPE_FSL;
92 }
93
94 static void fsl_spi_change_mode(struct spi_device *spi)
95 {
96         struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
97         struct spi_mpc8xxx_cs *cs = spi->controller_state;
98         struct fsl_spi_reg *reg_base = mspi->reg_base;
99         __be32 __iomem *mode = &reg_base->mode;
100         unsigned long flags;
101
102         if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
103                 return;
104
105         /* Turn off IRQs locally to minimize time that SPI is disabled. */
106         local_irq_save(flags);
107
108         /* Turn off SPI unit prior changing mode */
109         mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
110
111         /* When in CPM mode, we need to reinit tx and rx. */
112         if (mspi->flags & SPI_CPM_MODE) {
113                 fsl_spi_cpm_reinit_txrx(mspi);
114         }
115         mpc8xxx_spi_write_reg(mode, cs->hw_mode);
116         local_irq_restore(flags);
117 }
118
119 static void fsl_spi_chipselect(struct spi_device *spi, int value)
120 {
121         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
122         struct fsl_spi_platform_data *pdata;
123         bool pol = spi->mode & SPI_CS_HIGH;
124         struct spi_mpc8xxx_cs   *cs = spi->controller_state;
125
126         pdata = spi->dev.parent->parent->platform_data;
127
128         if (value == BITBANG_CS_INACTIVE) {
129                 if (pdata->cs_control)
130                         pdata->cs_control(spi, !pol);
131         }
132
133         if (value == BITBANG_CS_ACTIVE) {
134                 mpc8xxx_spi->rx_shift = cs->rx_shift;
135                 mpc8xxx_spi->tx_shift = cs->tx_shift;
136                 mpc8xxx_spi->get_rx = cs->get_rx;
137                 mpc8xxx_spi->get_tx = cs->get_tx;
138
139                 fsl_spi_change_mode(spi);
140
141                 if (pdata->cs_control)
142                         pdata->cs_control(spi, pol);
143         }
144 }
145
146 static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
147                                       int bits_per_word, int msb_first)
148 {
149         *rx_shift = 0;
150         *tx_shift = 0;
151         if (msb_first) {
152                 if (bits_per_word <= 8) {
153                         *rx_shift = 16;
154                         *tx_shift = 24;
155                 } else if (bits_per_word <= 16) {
156                         *rx_shift = 16;
157                         *tx_shift = 16;
158                 }
159         } else {
160                 if (bits_per_word <= 8)
161                         *rx_shift = 8;
162         }
163 }
164
165 static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift,
166                                      int bits_per_word, int msb_first)
167 {
168         *rx_shift = 0;
169         *tx_shift = 0;
170         if (bits_per_word <= 16) {
171                 if (msb_first) {
172                         *rx_shift = 16; /* LSB in bit 16 */
173                         *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */
174                 } else {
175                         *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */
176                 }
177         }
178 }
179
180 static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
181                                 struct spi_device *spi,
182                                 struct mpc8xxx_spi *mpc8xxx_spi,
183                                 int bits_per_word)
184 {
185         cs->rx_shift = 0;
186         cs->tx_shift = 0;
187         if (bits_per_word <= 8) {
188                 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
189                 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
190         } else if (bits_per_word <= 16) {
191                 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
192                 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
193         } else if (bits_per_word <= 32) {
194                 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
195                 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
196         } else
197                 return -EINVAL;
198
199         if (mpc8xxx_spi->set_shifts)
200                 mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
201                                         bits_per_word,
202                                         !(spi->mode & SPI_LSB_FIRST));
203
204         mpc8xxx_spi->rx_shift = cs->rx_shift;
205         mpc8xxx_spi->tx_shift = cs->tx_shift;
206         mpc8xxx_spi->get_rx = cs->get_rx;
207         mpc8xxx_spi->get_tx = cs->get_tx;
208
209         return bits_per_word;
210 }
211
212 static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
213                                 struct spi_device *spi,
214                                 int bits_per_word)
215 {
216         /* QE uses Little Endian for words > 8
217          * so transform all words > 8 into 8 bits
218          * Unfortnatly that doesn't work for LSB so
219          * reject these for now */
220         /* Note: 32 bits word, LSB works iff
221          * tfcr/rfcr is set to CPMFCR_GBL */
222         if (spi->mode & SPI_LSB_FIRST &&
223             bits_per_word > 8)
224                 return -EINVAL;
225         if (bits_per_word > 8)
226                 return 8; /* pretend its 8 bits */
227         return bits_per_word;
228 }
229
230 static int fsl_spi_setup_transfer(struct spi_device *spi,
231                                         struct spi_transfer *t)
232 {
233         struct mpc8xxx_spi *mpc8xxx_spi;
234         int bits_per_word = 0;
235         u8 pm;
236         u32 hz = 0;
237         struct spi_mpc8xxx_cs   *cs = spi->controller_state;
238
239         mpc8xxx_spi = spi_master_get_devdata(spi->master);
240
241         if (t) {
242                 bits_per_word = t->bits_per_word;
243                 hz = t->speed_hz;
244         }
245
246         /* spi_transfer level calls that work per-word */
247         if (!bits_per_word)
248                 bits_per_word = spi->bits_per_word;
249
250         if (!hz)
251                 hz = spi->max_speed_hz;
252
253         if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
254                 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
255                                                            mpc8xxx_spi,
256                                                            bits_per_word);
257         else if (mpc8xxx_spi->flags & SPI_QE)
258                 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
259                                                           bits_per_word);
260
261         if (bits_per_word < 0)
262                 return bits_per_word;
263
264         if (bits_per_word == 32)
265                 bits_per_word = 0;
266         else
267                 bits_per_word = bits_per_word - 1;
268
269         /* mask out bits we are going to set */
270         cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
271                                   | SPMODE_PM(0xF));
272
273         cs->hw_mode |= SPMODE_LEN(bits_per_word);
274
275         if ((mpc8xxx_spi->spibrg / hz) > 64) {
276                 cs->hw_mode |= SPMODE_DIV16;
277                 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
278                 WARN_ONCE(pm > 16,
279                           "%s: Requested speed is too low: %d Hz. Will use %d Hz instead.\n",
280                           dev_name(&spi->dev), hz, mpc8xxx_spi->spibrg / 1024);
281                 if (pm > 16)
282                         pm = 16;
283         } else {
284                 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
285         }
286         if (pm)
287                 pm--;
288
289         cs->hw_mode |= SPMODE_PM(pm);
290
291         fsl_spi_change_mode(spi);
292         return 0;
293 }
294
295 static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
296                                 struct spi_transfer *t, unsigned int len)
297 {
298         u32 word;
299         struct fsl_spi_reg *reg_base = mspi->reg_base;
300
301         mspi->count = len;
302
303         /* enable rx ints */
304         mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
305
306         /* transmit word */
307         word = mspi->get_tx(mspi);
308         mpc8xxx_spi_write_reg(&reg_base->transmit, word);
309
310         return 0;
311 }
312
313 static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
314                             bool is_dma_mapped)
315 {
316         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
317         struct fsl_spi_reg *reg_base;
318         unsigned int len = t->len;
319         u8 bits_per_word;
320         int ret;
321
322         reg_base = mpc8xxx_spi->reg_base;
323         bits_per_word = spi->bits_per_word;
324         if (t->bits_per_word)
325                 bits_per_word = t->bits_per_word;
326
327         if (bits_per_word > 8) {
328                 /* invalid length? */
329                 if (len & 1)
330                         return -EINVAL;
331                 len /= 2;
332         }
333         if (bits_per_word > 16) {
334                 /* invalid length? */
335                 if (len & 1)
336                         return -EINVAL;
337                 len /= 2;
338         }
339
340         mpc8xxx_spi->tx = t->tx_buf;
341         mpc8xxx_spi->rx = t->rx_buf;
342
343         reinit_completion(&mpc8xxx_spi->done);
344
345         if (mpc8xxx_spi->flags & SPI_CPM_MODE)
346                 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
347         else
348                 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
349         if (ret)
350                 return ret;
351
352         wait_for_completion(&mpc8xxx_spi->done);
353
354         /* disable rx ints */
355         mpc8xxx_spi_write_reg(&reg_base->mask, 0);
356
357         if (mpc8xxx_spi->flags & SPI_CPM_MODE)
358                 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
359
360         return mpc8xxx_spi->count;
361 }
362
363 static int fsl_spi_do_one_msg(struct spi_master *master,
364                               struct spi_message *m)
365 {
366         struct spi_device *spi = m->spi;
367         struct spi_transfer *t, *first;
368         unsigned int cs_change;
369         const int nsecs = 50;
370         int status;
371
372         /* Don't allow changes if CS is active */
373         first = list_first_entry(&m->transfers, struct spi_transfer,
374                         transfer_list);
375         list_for_each_entry(t, &m->transfers, transfer_list) {
376                 if ((first->bits_per_word != t->bits_per_word) ||
377                         (first->speed_hz != t->speed_hz)) {
378                         dev_err(&spi->dev,
379                                 "bits_per_word/speed_hz should be same for the same SPI transfer\n");
380                         return -EINVAL;
381                 }
382         }
383
384         cs_change = 1;
385         status = -EINVAL;
386         list_for_each_entry(t, &m->transfers, transfer_list) {
387                 if (cs_change)
388                         status = fsl_spi_setup_transfer(spi, t);
389                 if (status < 0)
390                         break;
391
392                 if (cs_change) {
393                         fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
394                         ndelay(nsecs);
395                 }
396                 cs_change = t->cs_change;
397                 if (t->len)
398                         status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
399                 if (status) {
400                         status = -EMSGSIZE;
401                         break;
402                 }
403                 m->actual_length += t->len;
404
405                 if (t->delay_usecs)
406                         udelay(t->delay_usecs);
407
408                 if (cs_change) {
409                         ndelay(nsecs);
410                         fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
411                         ndelay(nsecs);
412                 }
413         }
414
415         m->status = status;
416         spi_finalize_current_message(master);
417
418         if (status || !cs_change) {
419                 ndelay(nsecs);
420                 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
421         }
422
423         fsl_spi_setup_transfer(spi, NULL);
424         return 0;
425 }
426
427 static int fsl_spi_setup(struct spi_device *spi)
428 {
429         struct mpc8xxx_spi *mpc8xxx_spi;
430         struct fsl_spi_reg *reg_base;
431         int retval;
432         u32 hw_mode;
433         struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
434
435         if (!spi->max_speed_hz)
436                 return -EINVAL;
437
438         if (!cs) {
439                 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
440                 if (!cs)
441                         return -ENOMEM;
442                 spi_set_ctldata(spi, cs);
443         }
444         mpc8xxx_spi = spi_master_get_devdata(spi->master);
445
446         reg_base = mpc8xxx_spi->reg_base;
447
448         hw_mode = cs->hw_mode; /* Save original settings */
449         cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
450         /* mask out bits we are going to set */
451         cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
452                          | SPMODE_REV | SPMODE_LOOP);
453
454         if (spi->mode & SPI_CPHA)
455                 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
456         if (spi->mode & SPI_CPOL)
457                 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
458         if (!(spi->mode & SPI_LSB_FIRST))
459                 cs->hw_mode |= SPMODE_REV;
460         if (spi->mode & SPI_LOOP)
461                 cs->hw_mode |= SPMODE_LOOP;
462
463         retval = fsl_spi_setup_transfer(spi, NULL);
464         if (retval < 0) {
465                 cs->hw_mode = hw_mode; /* Restore settings */
466                 return retval;
467         }
468
469         if (mpc8xxx_spi->type == TYPE_GRLIB) {
470                 if (gpio_is_valid(spi->cs_gpio)) {
471                         int desel;
472
473                         retval = gpio_request(spi->cs_gpio,
474                                               dev_name(&spi->dev));
475                         if (retval)
476                                 return retval;
477
478                         desel = !(spi->mode & SPI_CS_HIGH);
479                         retval = gpio_direction_output(spi->cs_gpio, desel);
480                         if (retval) {
481                                 gpio_free(spi->cs_gpio);
482                                 return retval;
483                         }
484                 } else if (spi->cs_gpio != -ENOENT) {
485                         if (spi->cs_gpio < 0)
486                                 return spi->cs_gpio;
487                         return -EINVAL;
488                 }
489                 /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
490                  * indicates to use native chipselect if present, or allow for
491                  * an always selected chip
492                  */
493         }
494
495         /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
496         fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
497
498         return 0;
499 }
500
501 static void fsl_spi_cleanup(struct spi_device *spi)
502 {
503         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
504         struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
505
506         if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
507                 gpio_free(spi->cs_gpio);
508
509         kfree(cs);
510         spi_set_ctldata(spi, NULL);
511 }
512
513 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
514 {
515         struct fsl_spi_reg *reg_base = mspi->reg_base;
516
517         /* We need handle RX first */
518         if (events & SPIE_NE) {
519                 u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
520
521                 if (mspi->rx)
522                         mspi->get_rx(rx_data, mspi);
523         }
524
525         if ((events & SPIE_NF) == 0)
526                 /* spin until TX is done */
527                 while (((events =
528                         mpc8xxx_spi_read_reg(&reg_base->event)) &
529                                                 SPIE_NF) == 0)
530                         cpu_relax();
531
532         /* Clear the events */
533         mpc8xxx_spi_write_reg(&reg_base->event, events);
534
535         mspi->count -= 1;
536         if (mspi->count) {
537                 u32 word = mspi->get_tx(mspi);
538
539                 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
540         } else {
541                 complete(&mspi->done);
542         }
543 }
544
545 static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
546 {
547         struct mpc8xxx_spi *mspi = context_data;
548         irqreturn_t ret = IRQ_NONE;
549         u32 events;
550         struct fsl_spi_reg *reg_base = mspi->reg_base;
551
552         /* Get interrupt events(tx/rx) */
553         events = mpc8xxx_spi_read_reg(&reg_base->event);
554         if (events)
555                 ret = IRQ_HANDLED;
556
557         dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
558
559         if (mspi->flags & SPI_CPM_MODE)
560                 fsl_spi_cpm_irq(mspi, events);
561         else
562                 fsl_spi_cpu_irq(mspi, events);
563
564         return ret;
565 }
566
567 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
568 {
569         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
570         struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
571         u32 slvsel;
572         u16 cs = spi->chip_select;
573
574         if (gpio_is_valid(spi->cs_gpio)) {
575                 gpio_set_value(spi->cs_gpio, on);
576         } else if (cs < mpc8xxx_spi->native_chipselects) {
577                 slvsel = mpc8xxx_spi_read_reg(&reg_base->slvsel);
578                 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
579                 mpc8xxx_spi_write_reg(&reg_base->slvsel, slvsel);
580         }
581 }
582
583 static void fsl_spi_grlib_probe(struct device *dev)
584 {
585         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
586         struct spi_master *master = dev_get_drvdata(dev);
587         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
588         struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
589         int mbits;
590         u32 capabilities;
591
592         capabilities = mpc8xxx_spi_read_reg(&reg_base->cap);
593
594         mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
595         mbits = SPCAP_MAXWLEN(capabilities);
596         if (mbits)
597                 mpc8xxx_spi->max_bits_per_word = mbits + 1;
598
599         mpc8xxx_spi->native_chipselects = 0;
600         if (SPCAP_SSEN(capabilities)) {
601                 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
602                 mpc8xxx_spi_write_reg(&reg_base->slvsel, 0xffffffff);
603         }
604         master->num_chipselect = mpc8xxx_spi->native_chipselects;
605         pdata->cs_control = fsl_spi_grlib_cs_control;
606 }
607
608 static struct spi_master * fsl_spi_probe(struct device *dev,
609                 struct resource *mem, unsigned int irq)
610 {
611         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
612         struct spi_master *master;
613         struct mpc8xxx_spi *mpc8xxx_spi;
614         struct fsl_spi_reg *reg_base;
615         u32 regval;
616         int ret = 0;
617
618         master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
619         if (master == NULL) {
620                 ret = -ENOMEM;
621                 goto err;
622         }
623
624         dev_set_drvdata(dev, master);
625
626         mpc8xxx_spi_probe(dev, mem, irq);
627
628         master->setup = fsl_spi_setup;
629         master->cleanup = fsl_spi_cleanup;
630         master->transfer_one_message = fsl_spi_do_one_msg;
631
632         mpc8xxx_spi = spi_master_get_devdata(master);
633         mpc8xxx_spi->max_bits_per_word = 32;
634         mpc8xxx_spi->type = fsl_spi_get_type(dev);
635
636         ret = fsl_spi_cpm_init(mpc8xxx_spi);
637         if (ret)
638                 goto err_cpm_init;
639
640         mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
641         if (IS_ERR(mpc8xxx_spi->reg_base)) {
642                 ret = PTR_ERR(mpc8xxx_spi->reg_base);
643                 goto err_probe;
644         }
645
646         if (mpc8xxx_spi->type == TYPE_GRLIB)
647                 fsl_spi_grlib_probe(dev);
648
649         master->bits_per_word_mask =
650                 (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) &
651                 SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
652
653         if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
654                 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
655
656         if (mpc8xxx_spi->set_shifts)
657                 /* 8 bits per word and MSB first */
658                 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
659                                         &mpc8xxx_spi->tx_shift, 8, 1);
660
661         /* Register for SPI Interrupt */
662         ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
663                                0, "fsl_spi", mpc8xxx_spi);
664
665         if (ret != 0)
666                 goto err_probe;
667
668         reg_base = mpc8xxx_spi->reg_base;
669
670         /* SPI controller initializations */
671         mpc8xxx_spi_write_reg(&reg_base->mode, 0);
672         mpc8xxx_spi_write_reg(&reg_base->mask, 0);
673         mpc8xxx_spi_write_reg(&reg_base->command, 0);
674         mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
675
676         /* Enable SPI interface */
677         regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
678         if (mpc8xxx_spi->max_bits_per_word < 8) {
679                 regval &= ~SPMODE_LEN(0xF);
680                 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
681         }
682         if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
683                 regval |= SPMODE_OP;
684
685         mpc8xxx_spi_write_reg(&reg_base->mode, regval);
686
687         ret = devm_spi_register_master(dev, master);
688         if (ret < 0)
689                 goto err_probe;
690
691         dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
692                  mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
693
694         return master;
695
696 err_probe:
697         fsl_spi_cpm_free(mpc8xxx_spi);
698 err_cpm_init:
699         spi_master_put(master);
700 err:
701         return ERR_PTR(ret);
702 }
703
704 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
705 {
706         struct device *dev = spi->dev.parent->parent;
707         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
708         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
709         u16 cs = spi->chip_select;
710
711         if (cs < pinfo->ngpios) {
712                 int gpio = pinfo->gpios[cs];
713                 bool alow = pinfo->alow_flags[cs];
714
715                 gpio_set_value(gpio, on ^ alow);
716         } else {
717                 if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs))
718                         return;
719                 iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs);
720         }
721 }
722
723 static int of_fsl_spi_get_chipselects(struct device *dev)
724 {
725         struct device_node *np = dev->of_node;
726         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
727         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
728         bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&
729                 of_property_read_bool(np, "fsl,spisel_boot");
730         int ngpios;
731         int i = 0;
732         int ret;
733
734         ngpios = of_gpio_count(np);
735         ngpios = max(ngpios, 0);
736         if (ngpios == 0 && !spisel_boot) {
737                 /*
738                  * SPI w/o chip-select line. One SPI device is still permitted
739                  * though.
740                  */
741                 pdata->max_chipselect = 1;
742                 return 0;
743         }
744
745         pinfo->ngpios = ngpios;
746         pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
747                                      GFP_KERNEL);
748         if (!pinfo->gpios)
749                 return -ENOMEM;
750         memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
751
752         pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
753                                     GFP_KERNEL);
754         if (!pinfo->alow_flags) {
755                 ret = -ENOMEM;
756                 goto err_alloc_flags;
757         }
758
759         for (; i < ngpios; i++) {
760                 int gpio;
761                 enum of_gpio_flags flags;
762
763                 gpio = of_get_gpio_flags(np, i, &flags);
764                 if (!gpio_is_valid(gpio)) {
765                         dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
766                         ret = gpio;
767                         goto err_loop;
768                 }
769
770                 ret = gpio_request(gpio, dev_name(dev));
771                 if (ret) {
772                         dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
773                         goto err_loop;
774                 }
775
776                 pinfo->gpios[i] = gpio;
777                 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
778
779                 ret = gpio_direction_output(pinfo->gpios[i],
780                                             pinfo->alow_flags[i]);
781                 if (ret) {
782                         dev_err(dev,
783                                 "can't set output direction for gpio #%d: %d\n",
784                                 i, ret);
785                         goto err_loop;
786                 }
787         }
788
789 #if IS_ENABLED(CONFIG_FSL_SOC)
790         if (spisel_boot) {
791                 pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);
792                 if (!pinfo->immr_spi_cs) {
793                         ret = -ENOMEM;
794                         i = ngpios - 1;
795                         goto err_loop;
796                 }
797         }
798 #endif
799
800         pdata->max_chipselect = ngpios + spisel_boot;
801         pdata->cs_control = fsl_spi_cs_control;
802
803         return 0;
804
805 err_loop:
806         while (i >= 0) {
807                 if (gpio_is_valid(pinfo->gpios[i]))
808                         gpio_free(pinfo->gpios[i]);
809                 i--;
810         }
811
812         kfree(pinfo->alow_flags);
813         pinfo->alow_flags = NULL;
814 err_alloc_flags:
815         kfree(pinfo->gpios);
816         pinfo->gpios = NULL;
817         return ret;
818 }
819
820 static int of_fsl_spi_free_chipselects(struct device *dev)
821 {
822         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
823         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
824         int i;
825
826         if (!pinfo->gpios)
827                 return 0;
828
829         for (i = 0; i < pdata->max_chipselect; i++) {
830                 if (gpio_is_valid(pinfo->gpios[i]))
831                         gpio_free(pinfo->gpios[i]);
832         }
833
834         kfree(pinfo->gpios);
835         kfree(pinfo->alow_flags);
836         return 0;
837 }
838
839 static int of_fsl_spi_probe(struct platform_device *ofdev)
840 {
841         struct device *dev = &ofdev->dev;
842         struct device_node *np = ofdev->dev.of_node;
843         struct spi_master *master;
844         struct resource mem;
845         int irq = 0, type;
846         int ret = -ENOMEM;
847
848         ret = of_mpc8xxx_spi_probe(ofdev);
849         if (ret)
850                 return ret;
851
852         type = fsl_spi_get_type(&ofdev->dev);
853         if (type == TYPE_FSL) {
854                 ret = of_fsl_spi_get_chipselects(dev);
855                 if (ret)
856                         goto err;
857         }
858
859         ret = of_address_to_resource(np, 0, &mem);
860         if (ret)
861                 goto err;
862
863         irq = irq_of_parse_and_map(np, 0);
864         if (!irq) {
865                 ret = -EINVAL;
866                 goto err;
867         }
868
869         master = fsl_spi_probe(dev, &mem, irq);
870         if (IS_ERR(master)) {
871                 ret = PTR_ERR(master);
872                 goto err;
873         }
874
875         return 0;
876
877 err:
878         irq_dispose_mapping(irq);
879         if (type == TYPE_FSL)
880                 of_fsl_spi_free_chipselects(dev);
881         return ret;
882 }
883
884 static int of_fsl_spi_remove(struct platform_device *ofdev)
885 {
886         struct spi_master *master = platform_get_drvdata(ofdev);
887         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
888
889         fsl_spi_cpm_free(mpc8xxx_spi);
890         if (mpc8xxx_spi->type == TYPE_FSL)
891                 of_fsl_spi_free_chipselects(&ofdev->dev);
892         return 0;
893 }
894
895 static struct platform_driver of_fsl_spi_driver = {
896         .driver = {
897                 .name = "fsl_spi",
898                 .of_match_table = of_fsl_spi_match,
899         },
900         .probe          = of_fsl_spi_probe,
901         .remove         = of_fsl_spi_remove,
902 };
903
904 #ifdef CONFIG_MPC832x_RDB
905 /*
906  * XXX XXX XXX
907  * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
908  * only. The driver should go away soon, since newer MPC8323E-RDB's device
909  * tree can work with OpenFirmware driver. But for now we support old trees
910  * as well.
911  */
912 static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
913 {
914         struct resource *mem;
915         int irq;
916         struct spi_master *master;
917
918         if (!dev_get_platdata(&pdev->dev))
919                 return -EINVAL;
920
921         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
922         if (!mem)
923                 return -EINVAL;
924
925         irq = platform_get_irq(pdev, 0);
926         if (irq <= 0)
927                 return -EINVAL;
928
929         master = fsl_spi_probe(&pdev->dev, mem, irq);
930         return PTR_ERR_OR_ZERO(master);
931 }
932
933 static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
934 {
935         struct spi_master *master = platform_get_drvdata(pdev);
936         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
937
938         fsl_spi_cpm_free(mpc8xxx_spi);
939
940         return 0;
941 }
942
943 MODULE_ALIAS("platform:mpc8xxx_spi");
944 static struct platform_driver mpc8xxx_spi_driver = {
945         .probe = plat_mpc8xxx_spi_probe,
946         .remove = plat_mpc8xxx_spi_remove,
947         .driver = {
948                 .name = "mpc8xxx_spi",
949         },
950 };
951
952 static bool legacy_driver_failed;
953
954 static void __init legacy_driver_register(void)
955 {
956         legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
957 }
958
959 static void __exit legacy_driver_unregister(void)
960 {
961         if (legacy_driver_failed)
962                 return;
963         platform_driver_unregister(&mpc8xxx_spi_driver);
964 }
965 #else
966 static void __init legacy_driver_register(void) {}
967 static void __exit legacy_driver_unregister(void) {}
968 #endif /* CONFIG_MPC832x_RDB */
969
970 static int __init fsl_spi_init(void)
971 {
972         legacy_driver_register();
973         return platform_driver_register(&of_fsl_spi_driver);
974 }
975 module_init(fsl_spi_init);
976
977 static void __exit fsl_spi_exit(void)
978 {
979         platform_driver_unregister(&of_fsl_spi_driver);
980         legacy_driver_unregister();
981 }
982 module_exit(fsl_spi_exit);
983
984 MODULE_AUTHOR("Kumar Gala");
985 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
986 MODULE_LICENSE("GPL");