spi: spi-fsl-spi: relax message sanity checking a little
[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         cs_change = 1;
374         list_for_each_entry(t, &m->transfers, transfer_list) {
375                 if (cs_change)
376                         first = t;
377                 cs_change = t->cs_change;
378                 if ((first->bits_per_word != t->bits_per_word) ||
379                         (first->speed_hz != t->speed_hz)) {
380                         dev_err(&spi->dev,
381                                 "bits_per_word/speed_hz cannot change while CS is active\n");
382                         return -EINVAL;
383                 }
384         }
385
386         cs_change = 1;
387         status = -EINVAL;
388         list_for_each_entry(t, &m->transfers, transfer_list) {
389                 if (cs_change)
390                         status = fsl_spi_setup_transfer(spi, t);
391                 if (status < 0)
392                         break;
393
394                 if (cs_change) {
395                         fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
396                         ndelay(nsecs);
397                 }
398                 cs_change = t->cs_change;
399                 if (t->len)
400                         status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
401                 if (status) {
402                         status = -EMSGSIZE;
403                         break;
404                 }
405                 m->actual_length += t->len;
406
407                 if (t->delay_usecs)
408                         udelay(t->delay_usecs);
409
410                 if (cs_change) {
411                         ndelay(nsecs);
412                         fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
413                         ndelay(nsecs);
414                 }
415         }
416
417         m->status = status;
418         spi_finalize_current_message(master);
419
420         if (status || !cs_change) {
421                 ndelay(nsecs);
422                 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
423         }
424
425         fsl_spi_setup_transfer(spi, NULL);
426         return 0;
427 }
428
429 static int fsl_spi_setup(struct spi_device *spi)
430 {
431         struct mpc8xxx_spi *mpc8xxx_spi;
432         struct fsl_spi_reg *reg_base;
433         int retval;
434         u32 hw_mode;
435         struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
436
437         if (!spi->max_speed_hz)
438                 return -EINVAL;
439
440         if (!cs) {
441                 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
442                 if (!cs)
443                         return -ENOMEM;
444                 spi_set_ctldata(spi, cs);
445         }
446         mpc8xxx_spi = spi_master_get_devdata(spi->master);
447
448         reg_base = mpc8xxx_spi->reg_base;
449
450         hw_mode = cs->hw_mode; /* Save original settings */
451         cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
452         /* mask out bits we are going to set */
453         cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
454                          | SPMODE_REV | SPMODE_LOOP);
455
456         if (spi->mode & SPI_CPHA)
457                 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
458         if (spi->mode & SPI_CPOL)
459                 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
460         if (!(spi->mode & SPI_LSB_FIRST))
461                 cs->hw_mode |= SPMODE_REV;
462         if (spi->mode & SPI_LOOP)
463                 cs->hw_mode |= SPMODE_LOOP;
464
465         retval = fsl_spi_setup_transfer(spi, NULL);
466         if (retval < 0) {
467                 cs->hw_mode = hw_mode; /* Restore settings */
468                 return retval;
469         }
470
471         if (mpc8xxx_spi->type == TYPE_GRLIB) {
472                 if (gpio_is_valid(spi->cs_gpio)) {
473                         int desel;
474
475                         retval = gpio_request(spi->cs_gpio,
476                                               dev_name(&spi->dev));
477                         if (retval)
478                                 return retval;
479
480                         desel = !(spi->mode & SPI_CS_HIGH);
481                         retval = gpio_direction_output(spi->cs_gpio, desel);
482                         if (retval) {
483                                 gpio_free(spi->cs_gpio);
484                                 return retval;
485                         }
486                 } else if (spi->cs_gpio != -ENOENT) {
487                         if (spi->cs_gpio < 0)
488                                 return spi->cs_gpio;
489                         return -EINVAL;
490                 }
491                 /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
492                  * indicates to use native chipselect if present, or allow for
493                  * an always selected chip
494                  */
495         }
496
497         /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
498         fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
499
500         return 0;
501 }
502
503 static void fsl_spi_cleanup(struct spi_device *spi)
504 {
505         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
506         struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
507
508         if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
509                 gpio_free(spi->cs_gpio);
510
511         kfree(cs);
512         spi_set_ctldata(spi, NULL);
513 }
514
515 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
516 {
517         struct fsl_spi_reg *reg_base = mspi->reg_base;
518
519         /* We need handle RX first */
520         if (events & SPIE_NE) {
521                 u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
522
523                 if (mspi->rx)
524                         mspi->get_rx(rx_data, mspi);
525         }
526
527         if ((events & SPIE_NF) == 0)
528                 /* spin until TX is done */
529                 while (((events =
530                         mpc8xxx_spi_read_reg(&reg_base->event)) &
531                                                 SPIE_NF) == 0)
532                         cpu_relax();
533
534         /* Clear the events */
535         mpc8xxx_spi_write_reg(&reg_base->event, events);
536
537         mspi->count -= 1;
538         if (mspi->count) {
539                 u32 word = mspi->get_tx(mspi);
540
541                 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
542         } else {
543                 complete(&mspi->done);
544         }
545 }
546
547 static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
548 {
549         struct mpc8xxx_spi *mspi = context_data;
550         irqreturn_t ret = IRQ_NONE;
551         u32 events;
552         struct fsl_spi_reg *reg_base = mspi->reg_base;
553
554         /* Get interrupt events(tx/rx) */
555         events = mpc8xxx_spi_read_reg(&reg_base->event);
556         if (events)
557                 ret = IRQ_HANDLED;
558
559         dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
560
561         if (mspi->flags & SPI_CPM_MODE)
562                 fsl_spi_cpm_irq(mspi, events);
563         else
564                 fsl_spi_cpu_irq(mspi, events);
565
566         return ret;
567 }
568
569 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
570 {
571         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
572         struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
573         u32 slvsel;
574         u16 cs = spi->chip_select;
575
576         if (gpio_is_valid(spi->cs_gpio)) {
577                 gpio_set_value(spi->cs_gpio, on);
578         } else if (cs < mpc8xxx_spi->native_chipselects) {
579                 slvsel = mpc8xxx_spi_read_reg(&reg_base->slvsel);
580                 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
581                 mpc8xxx_spi_write_reg(&reg_base->slvsel, slvsel);
582         }
583 }
584
585 static void fsl_spi_grlib_probe(struct device *dev)
586 {
587         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
588         struct spi_master *master = dev_get_drvdata(dev);
589         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
590         struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
591         int mbits;
592         u32 capabilities;
593
594         capabilities = mpc8xxx_spi_read_reg(&reg_base->cap);
595
596         mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
597         mbits = SPCAP_MAXWLEN(capabilities);
598         if (mbits)
599                 mpc8xxx_spi->max_bits_per_word = mbits + 1;
600
601         mpc8xxx_spi->native_chipselects = 0;
602         if (SPCAP_SSEN(capabilities)) {
603                 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
604                 mpc8xxx_spi_write_reg(&reg_base->slvsel, 0xffffffff);
605         }
606         master->num_chipselect = mpc8xxx_spi->native_chipselects;
607         pdata->cs_control = fsl_spi_grlib_cs_control;
608 }
609
610 static struct spi_master * fsl_spi_probe(struct device *dev,
611                 struct resource *mem, unsigned int irq)
612 {
613         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
614         struct spi_master *master;
615         struct mpc8xxx_spi *mpc8xxx_spi;
616         struct fsl_spi_reg *reg_base;
617         u32 regval;
618         int ret = 0;
619
620         master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
621         if (master == NULL) {
622                 ret = -ENOMEM;
623                 goto err;
624         }
625
626         dev_set_drvdata(dev, master);
627
628         mpc8xxx_spi_probe(dev, mem, irq);
629
630         master->setup = fsl_spi_setup;
631         master->cleanup = fsl_spi_cleanup;
632         master->transfer_one_message = fsl_spi_do_one_msg;
633
634         mpc8xxx_spi = spi_master_get_devdata(master);
635         mpc8xxx_spi->max_bits_per_word = 32;
636         mpc8xxx_spi->type = fsl_spi_get_type(dev);
637
638         ret = fsl_spi_cpm_init(mpc8xxx_spi);
639         if (ret)
640                 goto err_cpm_init;
641
642         mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
643         if (IS_ERR(mpc8xxx_spi->reg_base)) {
644                 ret = PTR_ERR(mpc8xxx_spi->reg_base);
645                 goto err_probe;
646         }
647
648         if (mpc8xxx_spi->type == TYPE_GRLIB)
649                 fsl_spi_grlib_probe(dev);
650
651         master->bits_per_word_mask =
652                 (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) &
653                 SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
654
655         if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
656                 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
657
658         if (mpc8xxx_spi->set_shifts)
659                 /* 8 bits per word and MSB first */
660                 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
661                                         &mpc8xxx_spi->tx_shift, 8, 1);
662
663         /* Register for SPI Interrupt */
664         ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
665                                0, "fsl_spi", mpc8xxx_spi);
666
667         if (ret != 0)
668                 goto err_probe;
669
670         reg_base = mpc8xxx_spi->reg_base;
671
672         /* SPI controller initializations */
673         mpc8xxx_spi_write_reg(&reg_base->mode, 0);
674         mpc8xxx_spi_write_reg(&reg_base->mask, 0);
675         mpc8xxx_spi_write_reg(&reg_base->command, 0);
676         mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
677
678         /* Enable SPI interface */
679         regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
680         if (mpc8xxx_spi->max_bits_per_word < 8) {
681                 regval &= ~SPMODE_LEN(0xF);
682                 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
683         }
684         if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
685                 regval |= SPMODE_OP;
686
687         mpc8xxx_spi_write_reg(&reg_base->mode, regval);
688
689         ret = devm_spi_register_master(dev, master);
690         if (ret < 0)
691                 goto err_probe;
692
693         dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
694                  mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
695
696         return master;
697
698 err_probe:
699         fsl_spi_cpm_free(mpc8xxx_spi);
700 err_cpm_init:
701         spi_master_put(master);
702 err:
703         return ERR_PTR(ret);
704 }
705
706 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
707 {
708         struct device *dev = spi->dev.parent->parent;
709         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
710         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
711         u16 cs = spi->chip_select;
712
713         if (cs < pinfo->ngpios) {
714                 int gpio = pinfo->gpios[cs];
715                 bool alow = pinfo->alow_flags[cs];
716
717                 gpio_set_value(gpio, on ^ alow);
718         } else {
719                 if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs))
720                         return;
721                 iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs);
722         }
723 }
724
725 static int of_fsl_spi_get_chipselects(struct device *dev)
726 {
727         struct device_node *np = dev->of_node;
728         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
729         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
730         bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&
731                 of_property_read_bool(np, "fsl,spisel_boot");
732         int ngpios;
733         int i = 0;
734         int ret;
735
736         ngpios = of_gpio_count(np);
737         ngpios = max(ngpios, 0);
738         if (ngpios == 0 && !spisel_boot) {
739                 /*
740                  * SPI w/o chip-select line. One SPI device is still permitted
741                  * though.
742                  */
743                 pdata->max_chipselect = 1;
744                 return 0;
745         }
746
747         pinfo->ngpios = ngpios;
748         pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
749                                      GFP_KERNEL);
750         if (!pinfo->gpios)
751                 return -ENOMEM;
752         memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
753
754         pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
755                                     GFP_KERNEL);
756         if (!pinfo->alow_flags) {
757                 ret = -ENOMEM;
758                 goto err_alloc_flags;
759         }
760
761         for (; i < ngpios; i++) {
762                 int gpio;
763                 enum of_gpio_flags flags;
764
765                 gpio = of_get_gpio_flags(np, i, &flags);
766                 if (!gpio_is_valid(gpio)) {
767                         dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
768                         ret = gpio;
769                         goto err_loop;
770                 }
771
772                 ret = gpio_request(gpio, dev_name(dev));
773                 if (ret) {
774                         dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
775                         goto err_loop;
776                 }
777
778                 pinfo->gpios[i] = gpio;
779                 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
780
781                 ret = gpio_direction_output(pinfo->gpios[i],
782                                             pinfo->alow_flags[i]);
783                 if (ret) {
784                         dev_err(dev,
785                                 "can't set output direction for gpio #%d: %d\n",
786                                 i, ret);
787                         goto err_loop;
788                 }
789         }
790
791 #if IS_ENABLED(CONFIG_FSL_SOC)
792         if (spisel_boot) {
793                 pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);
794                 if (!pinfo->immr_spi_cs) {
795                         ret = -ENOMEM;
796                         i = ngpios - 1;
797                         goto err_loop;
798                 }
799         }
800 #endif
801
802         pdata->max_chipselect = ngpios + spisel_boot;
803         pdata->cs_control = fsl_spi_cs_control;
804
805         return 0;
806
807 err_loop:
808         while (i >= 0) {
809                 if (gpio_is_valid(pinfo->gpios[i]))
810                         gpio_free(pinfo->gpios[i]);
811                 i--;
812         }
813
814         kfree(pinfo->alow_flags);
815         pinfo->alow_flags = NULL;
816 err_alloc_flags:
817         kfree(pinfo->gpios);
818         pinfo->gpios = NULL;
819         return ret;
820 }
821
822 static int of_fsl_spi_free_chipselects(struct device *dev)
823 {
824         struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
825         struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
826         int i;
827
828         if (!pinfo->gpios)
829                 return 0;
830
831         for (i = 0; i < pdata->max_chipselect; i++) {
832                 if (gpio_is_valid(pinfo->gpios[i]))
833                         gpio_free(pinfo->gpios[i]);
834         }
835
836         kfree(pinfo->gpios);
837         kfree(pinfo->alow_flags);
838         return 0;
839 }
840
841 static int of_fsl_spi_probe(struct platform_device *ofdev)
842 {
843         struct device *dev = &ofdev->dev;
844         struct device_node *np = ofdev->dev.of_node;
845         struct spi_master *master;
846         struct resource mem;
847         int irq = 0, type;
848         int ret = -ENOMEM;
849
850         ret = of_mpc8xxx_spi_probe(ofdev);
851         if (ret)
852                 return ret;
853
854         type = fsl_spi_get_type(&ofdev->dev);
855         if (type == TYPE_FSL) {
856                 ret = of_fsl_spi_get_chipselects(dev);
857                 if (ret)
858                         goto err;
859         }
860
861         ret = of_address_to_resource(np, 0, &mem);
862         if (ret)
863                 goto err;
864
865         irq = irq_of_parse_and_map(np, 0);
866         if (!irq) {
867                 ret = -EINVAL;
868                 goto err;
869         }
870
871         master = fsl_spi_probe(dev, &mem, irq);
872         if (IS_ERR(master)) {
873                 ret = PTR_ERR(master);
874                 goto err;
875         }
876
877         return 0;
878
879 err:
880         irq_dispose_mapping(irq);
881         if (type == TYPE_FSL)
882                 of_fsl_spi_free_chipselects(dev);
883         return ret;
884 }
885
886 static int of_fsl_spi_remove(struct platform_device *ofdev)
887 {
888         struct spi_master *master = platform_get_drvdata(ofdev);
889         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
890
891         fsl_spi_cpm_free(mpc8xxx_spi);
892         if (mpc8xxx_spi->type == TYPE_FSL)
893                 of_fsl_spi_free_chipselects(&ofdev->dev);
894         return 0;
895 }
896
897 static struct platform_driver of_fsl_spi_driver = {
898         .driver = {
899                 .name = "fsl_spi",
900                 .of_match_table = of_fsl_spi_match,
901         },
902         .probe          = of_fsl_spi_probe,
903         .remove         = of_fsl_spi_remove,
904 };
905
906 #ifdef CONFIG_MPC832x_RDB
907 /*
908  * XXX XXX XXX
909  * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
910  * only. The driver should go away soon, since newer MPC8323E-RDB's device
911  * tree can work with OpenFirmware driver. But for now we support old trees
912  * as well.
913  */
914 static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
915 {
916         struct resource *mem;
917         int irq;
918         struct spi_master *master;
919
920         if (!dev_get_platdata(&pdev->dev))
921                 return -EINVAL;
922
923         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
924         if (!mem)
925                 return -EINVAL;
926
927         irq = platform_get_irq(pdev, 0);
928         if (irq <= 0)
929                 return -EINVAL;
930
931         master = fsl_spi_probe(&pdev->dev, mem, irq);
932         return PTR_ERR_OR_ZERO(master);
933 }
934
935 static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
936 {
937         struct spi_master *master = platform_get_drvdata(pdev);
938         struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
939
940         fsl_spi_cpm_free(mpc8xxx_spi);
941
942         return 0;
943 }
944
945 MODULE_ALIAS("platform:mpc8xxx_spi");
946 static struct platform_driver mpc8xxx_spi_driver = {
947         .probe = plat_mpc8xxx_spi_probe,
948         .remove = plat_mpc8xxx_spi_remove,
949         .driver = {
950                 .name = "mpc8xxx_spi",
951         },
952 };
953
954 static bool legacy_driver_failed;
955
956 static void __init legacy_driver_register(void)
957 {
958         legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
959 }
960
961 static void __exit legacy_driver_unregister(void)
962 {
963         if (legacy_driver_failed)
964                 return;
965         platform_driver_unregister(&mpc8xxx_spi_driver);
966 }
967 #else
968 static void __init legacy_driver_register(void) {}
969 static void __exit legacy_driver_unregister(void) {}
970 #endif /* CONFIG_MPC832x_RDB */
971
972 static int __init fsl_spi_init(void)
973 {
974         legacy_driver_register();
975         return platform_driver_register(&of_fsl_spi_driver);
976 }
977 module_init(fsl_spi_init);
978
979 static void __exit fsl_spi_exit(void)
980 {
981         platform_driver_unregister(&of_fsl_spi_driver);
982         legacy_driver_unregister();
983 }
984 module_exit(fsl_spi_exit);
985
986 MODULE_AUTHOR("Kumar Gala");
987 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
988 MODULE_LICENSE("GPL");