blob: 7eff63a284ff1ab3d977c35d2a180bd86f5d9e6f [file] [log] [blame]
Kumar Galaccf06992006-05-20 15:00:15 -07001/*
Mingkai Hub36ece82010-10-12 18:18:31 +08002 * Freescale SPI controller driver.
Kumar Galaccf06992006-05-20 15:00:15 -07003 *
4 * Maintainer: Kumar Gala
5 *
6 * Copyright (C) 2006 Polycom, Inc.
Mingkai Hub36ece82010-10-12 18:18:31 +08007 * Copyright 2010 Freescale Semiconductor, Inc.
Kumar Galaccf06992006-05-20 15:00:15 -07008 *
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04009 * CPM SPI and QE buffer descriptors mode support:
10 * Copyright (c) 2009 MontaVista Software, Inc.
11 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
12 *
Kumar Galaccf06992006-05-20 15:00:15 -070013 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 */
18#include <linux/module.h>
Kumar Galaccf06992006-05-20 15:00:15 -070019#include <linux/types.h>
20#include <linux/kernel.h>
Kumar Galaccf06992006-05-20 15:00:15 -070021#include <linux/interrupt.h>
22#include <linux/delay.h>
23#include <linux/irq.h>
Kumar Galaccf06992006-05-20 15:00:15 -070024#include <linux/spi/spi.h>
25#include <linux/spi/spi_bitbang.h>
26#include <linux/platform_device.h>
27#include <linux/fsl_devices.h>
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040028#include <linux/dma-mapping.h>
29#include <linux/mm.h>
30#include <linux/mutex.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070031#include <linux/of.h>
32#include <linux/of_platform.h>
Andreas Larssone8beacb2013-02-15 16:52:21 +010033#include <linux/of_address.h>
34#include <linux/of_irq.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070035#include <linux/gpio.h>
36#include <linux/of_gpio.h>
Kumar Galaccf06992006-05-20 15:00:15 -070037
Grant Likelyca632f52011-06-06 01:16:30 -060038#include "spi-fsl-lib.h"
Andreas Larssone8beacb2013-02-15 16:52:21 +010039#include "spi-fsl-cpm.h"
40#include "spi-fsl-spi.h"
Kumar Galaccf06992006-05-20 15:00:15 -070041
Andreas Larssonc3f3e772013-02-15 16:52:24 +010042#define TYPE_FSL 0
43
44struct fsl_spi_match_data {
45 int type;
46};
47
48static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
49 .type = TYPE_FSL,
50};
51
52static struct of_device_id of_fsl_spi_match[] = {
53 {
54 .compatible = "fsl,spi",
55 .data = &of_fsl_spi_fsl_config,
56 },
57 {}
58};
59MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
60
61static int fsl_spi_get_type(struct device *dev)
62{
63 const struct of_device_id *match;
64
65 if (dev->of_node) {
66 match = of_match_node(of_fsl_spi_match, dev->of_node);
67 if (match && match->data)
68 return ((struct fsl_spi_match_data *)match->data)->type;
69 }
70 return TYPE_FSL;
71}
72
Mingkai Hub36ece82010-10-12 18:18:31 +080073static void fsl_spi_change_mode(struct spi_device *spi)
Anton Vorontsova35c1712009-10-12 20:49:24 +040074{
75 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
76 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Mingkai Hub36ece82010-10-12 18:18:31 +080077 struct fsl_spi_reg *reg_base = mspi->reg_base;
78 __be32 __iomem *mode = &reg_base->mode;
Anton Vorontsova35c1712009-10-12 20:49:24 +040079 unsigned long flags;
80
81 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
82 return;
83
84 /* Turn off IRQs locally to minimize time that SPI is disabled. */
85 local_irq_save(flags);
86
87 /* Turn off SPI unit prior changing mode */
88 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
Anton Vorontsova35c1712009-10-12 20:49:24 +040089
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040090 /* When in CPM mode, we need to reinit tx and rx. */
91 if (mspi->flags & SPI_CPM_MODE) {
Andreas Larssone8beacb2013-02-15 16:52:21 +010092 fsl_spi_cpm_reinit_txrx(mspi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040093 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -060094 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
Anton Vorontsova35c1712009-10-12 20:49:24 +040095 local_irq_restore(flags);
96}
97
Mingkai Hub36ece82010-10-12 18:18:31 +080098static void fsl_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -070099{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700100 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kenth Eriksson5039a862012-03-30 17:05:30 +0200101 struct fsl_spi_platform_data *pdata;
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700102 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700103 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700104
Kenth Eriksson5039a862012-03-30 17:05:30 +0200105 pdata = spi->dev.parent->parent->platform_data;
106
Kumar Galaccf06992006-05-20 15:00:15 -0700107 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700108 if (pdata->cs_control)
109 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700110 }
111
112 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700113 mpc8xxx_spi->rx_shift = cs->rx_shift;
114 mpc8xxx_spi->tx_shift = cs->tx_shift;
115 mpc8xxx_spi->get_rx = cs->get_rx;
116 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700117
Mingkai Hub36ece82010-10-12 18:18:31 +0800118 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700119
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700120 if (pdata->cs_control)
121 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700122 }
123}
124
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100125static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
126 int bits_per_word, int msb_first)
127{
128 *rx_shift = 0;
129 *tx_shift = 0;
130 if (msb_first) {
131 if (bits_per_word <= 8) {
132 *rx_shift = 16;
133 *tx_shift = 24;
134 } else if (bits_per_word <= 16) {
135 *rx_shift = 16;
136 *tx_shift = 16;
137 }
138 } else {
139 if (bits_per_word <= 8)
140 *rx_shift = 8;
141 }
142}
143
Mingkai Hub36ece82010-10-12 18:18:31 +0800144static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
145 struct spi_device *spi,
146 struct mpc8xxx_spi *mpc8xxx_spi,
147 int bits_per_word)
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000148{
149 cs->rx_shift = 0;
150 cs->tx_shift = 0;
151 if (bits_per_word <= 8) {
152 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
153 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000154 } else if (bits_per_word <= 16) {
155 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
156 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000157 } else if (bits_per_word <= 32) {
158 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
159 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
160 } else
161 return -EINVAL;
162
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100163 if (mpc8xxx_spi->set_shifts)
164 mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
165 bits_per_word,
166 !(spi->mode & SPI_LSB_FIRST));
167
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000168 mpc8xxx_spi->rx_shift = cs->rx_shift;
169 mpc8xxx_spi->tx_shift = cs->tx_shift;
170 mpc8xxx_spi->get_rx = cs->get_rx;
171 mpc8xxx_spi->get_tx = cs->get_tx;
172
173 return bits_per_word;
174}
175
Mingkai Hub36ece82010-10-12 18:18:31 +0800176static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
177 struct spi_device *spi,
178 int bits_per_word)
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000179{
180 /* QE uses Little Endian for words > 8
181 * so transform all words > 8 into 8 bits
182 * Unfortnatly that doesn't work for LSB so
183 * reject these for now */
184 /* Note: 32 bits word, LSB works iff
185 * tfcr/rfcr is set to CPMFCR_GBL */
186 if (spi->mode & SPI_LSB_FIRST &&
187 bits_per_word > 8)
188 return -EINVAL;
189 if (bits_per_word > 8)
190 return 8; /* pretend its 8 bits */
191 return bits_per_word;
192}
193
Mingkai Hub36ece82010-10-12 18:18:31 +0800194static int fsl_spi_setup_transfer(struct spi_device *spi,
195 struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700196{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700197 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800198 int bits_per_word = 0;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000199 u8 pm;
Mingkai Hub36ece82010-10-12 18:18:31 +0800200 u32 hz = 0;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700201 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700202
Anton Vorontsov575c5802009-06-18 16:49:08 -0700203 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700204
205 if (t) {
206 bits_per_word = t->bits_per_word;
207 hz = t->speed_hz;
Kumar Galaccf06992006-05-20 15:00:15 -0700208 }
209
210 /* spi_transfer level calls that work per-word */
211 if (!bits_per_word)
212 bits_per_word = spi->bits_per_word;
213
214 /* Make sure its a bit width we support [4..16, 32] */
215 if ((bits_per_word < 4)
Andreas Larsson8922a362013-02-15 16:52:25 +0100216 || ((bits_per_word > 16) && (bits_per_word != 32))
217 || (bits_per_word > mpc8xxx_spi->max_bits_per_word))
Kumar Galaccf06992006-05-20 15:00:15 -0700218 return -EINVAL;
219
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700220 if (!hz)
221 hz = spi->max_speed_hz;
222
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000223 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
224 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
225 mpc8xxx_spi,
226 bits_per_word);
227 else if (mpc8xxx_spi->flags & SPI_QE)
228 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
229 bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700230
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000231 if (bits_per_word < 0)
232 return bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700233
234 if (bits_per_word == 32)
235 bits_per_word = 0;
236 else
237 bits_per_word = bits_per_word - 1;
238
Anton Vorontsov32421da2007-07-31 00:38:41 -0700239 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700240 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
241 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700242
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700243 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700244
Anton Vorontsov575c5802009-06-18 16:49:08 -0700245 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700246 cs->hw_mode |= SPMODE_DIV16;
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700247 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700248
249 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
250 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Anton Vorontsov575c5802009-06-18 16:49:08 -0700251 hz, mpc8xxx_spi->spibrg / 1024);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700252 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700253 pm = 16;
Mingkai Hub36ece82010-10-12 18:18:31 +0800254 } else {
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700255 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
Mingkai Hub36ece82010-10-12 18:18:31 +0800256 }
Chen Gonga61f5342008-07-23 21:29:52 -0700257 if (pm)
258 pm--;
259
260 cs->hw_mode |= SPMODE_PM(pm);
David Brownelldccd5732007-07-17 04:04:02 -0700261
Mingkai Hub36ece82010-10-12 18:18:31 +0800262 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700263 return 0;
264}
265
Mingkai Hub36ece82010-10-12 18:18:31 +0800266static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400267 struct spi_transfer *t, unsigned int len)
268{
269 u32 word;
Mingkai Hub36ece82010-10-12 18:18:31 +0800270 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400271
272 mspi->count = len;
273
274 /* enable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800275 mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400276
277 /* transmit word */
278 word = mspi->get_tx(mspi);
Mingkai Hub36ece82010-10-12 18:18:31 +0800279 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400280
281 return 0;
282}
283
Mingkai Hub36ece82010-10-12 18:18:31 +0800284static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400285 bool is_dma_mapped)
286{
287 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Mingkai Hub36ece82010-10-12 18:18:31 +0800288 struct fsl_spi_reg *reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400289 unsigned int len = t->len;
290 u8 bits_per_word;
291 int ret;
292
Mingkai Hub36ece82010-10-12 18:18:31 +0800293 reg_base = mpc8xxx_spi->reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700294 bits_per_word = spi->bits_per_word;
295 if (t->bits_per_word)
296 bits_per_word = t->bits_per_word;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400297
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700298 if (bits_per_word > 8) {
299 /* invalid length? */
300 if (len & 1)
301 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700302 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700303 }
304 if (bits_per_word > 16) {
305 /* invalid length? */
306 if (len & 1)
307 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700308 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700309 }
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400310
311 mpc8xxx_spi->tx = t->tx_buf;
312 mpc8xxx_spi->rx = t->rx_buf;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700313
Anton Vorontsov575c5802009-06-18 16:49:08 -0700314 INIT_COMPLETION(mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700315
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400316 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800317 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400318 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800319 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400320 if (ret)
321 return ret;
Kumar Galaccf06992006-05-20 15:00:15 -0700322
Anton Vorontsov575c5802009-06-18 16:49:08 -0700323 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700324
325 /* disable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800326 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700327
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400328 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800329 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400330
Anton Vorontsov575c5802009-06-18 16:49:08 -0700331 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700332}
333
Mingkai Hub36ece82010-10-12 18:18:31 +0800334static void fsl_spi_do_one_msg(struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700335{
336 struct spi_device *spi = m->spi;
337 struct spi_transfer *t;
338 unsigned int cs_change;
339 const int nsecs = 50;
340 int status;
341
342 cs_change = 1;
343 status = 0;
344 list_for_each_entry(t, &m->transfers, transfer_list) {
345 if (t->bits_per_word || t->speed_hz) {
346 /* Don't allow changes if CS is active */
347 status = -EINVAL;
348
349 if (cs_change)
Mingkai Hub36ece82010-10-12 18:18:31 +0800350 status = fsl_spi_setup_transfer(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700351 if (status < 0)
352 break;
353 }
354
355 if (cs_change) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800356 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700357 ndelay(nsecs);
358 }
359 cs_change = t->cs_change;
360 if (t->len)
Mingkai Hub36ece82010-10-12 18:18:31 +0800361 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700362 if (status) {
363 status = -EMSGSIZE;
364 break;
365 }
366 m->actual_length += t->len;
367
368 if (t->delay_usecs)
369 udelay(t->delay_usecs);
370
371 if (cs_change) {
372 ndelay(nsecs);
Mingkai Hub36ece82010-10-12 18:18:31 +0800373 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700374 ndelay(nsecs);
375 }
376 }
377
378 m->status = status;
379 m->complete(m->context);
380
381 if (status || !cs_change) {
382 ndelay(nsecs);
Mingkai Hub36ece82010-10-12 18:18:31 +0800383 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700384 }
385
Mingkai Hub36ece82010-10-12 18:18:31 +0800386 fsl_spi_setup_transfer(spi, NULL);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700387}
388
Mingkai Hub36ece82010-10-12 18:18:31 +0800389static int fsl_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700390{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700391 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800392 struct fsl_spi_reg *reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700393 int retval;
394 u32 hw_mode;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700395 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700396
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700397 if (!spi->max_speed_hz)
398 return -EINVAL;
399
400 if (!cs) {
401 cs = kzalloc(sizeof *cs, GFP_KERNEL);
402 if (!cs)
403 return -ENOMEM;
404 spi->controller_state = cs;
405 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700406 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700407
Mingkai Hub36ece82010-10-12 18:18:31 +0800408 reg_base = mpc8xxx_spi->reg_base;
409
Thomas Weber88393162010-03-16 11:47:56 +0100410 hw_mode = cs->hw_mode; /* Save original settings */
Mingkai Hub36ece82010-10-12 18:18:31 +0800411 cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700412 /* mask out bits we are going to set */
413 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
414 | SPMODE_REV | SPMODE_LOOP);
415
416 if (spi->mode & SPI_CPHA)
417 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
418 if (spi->mode & SPI_CPOL)
419 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
420 if (!(spi->mode & SPI_LSB_FIRST))
421 cs->hw_mode |= SPMODE_REV;
422 if (spi->mode & SPI_LOOP)
423 cs->hw_mode |= SPMODE_LOOP;
424
Mingkai Hub36ece82010-10-12 18:18:31 +0800425 retval = fsl_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700426 if (retval < 0) {
427 cs->hw_mode = hw_mode; /* Restore settings */
428 return retval;
429 }
Andreas Larssonf482cd02013-02-15 16:52:22 +0100430
431 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
432 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
433
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700434 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700435}
436
Mingkai Hub36ece82010-10-12 18:18:31 +0800437static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400438{
Mingkai Hub36ece82010-10-12 18:18:31 +0800439 struct fsl_spi_reg *reg_base = mspi->reg_base;
440
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400441 /* We need handle RX first */
442 if (events & SPIE_NE) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800443 u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400444
445 if (mspi->rx)
446 mspi->get_rx(rx_data, mspi);
447 }
448
449 if ((events & SPIE_NF) == 0)
450 /* spin until TX is done */
451 while (((events =
Mingkai Hub36ece82010-10-12 18:18:31 +0800452 mpc8xxx_spi_read_reg(&reg_base->event)) &
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400453 SPIE_NF) == 0)
454 cpu_relax();
455
456 /* Clear the events */
Mingkai Hub36ece82010-10-12 18:18:31 +0800457 mpc8xxx_spi_write_reg(&reg_base->event, events);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400458
459 mspi->count -= 1;
460 if (mspi->count) {
461 u32 word = mspi->get_tx(mspi);
462
Mingkai Hub36ece82010-10-12 18:18:31 +0800463 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400464 } else {
465 complete(&mspi->done);
466 }
467}
468
Mingkai Hub36ece82010-10-12 18:18:31 +0800469static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400470{
471 struct mpc8xxx_spi *mspi = context_data;
472 irqreturn_t ret = IRQ_NONE;
473 u32 events;
Mingkai Hub36ece82010-10-12 18:18:31 +0800474 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400475
476 /* Get interrupt events(tx/rx) */
Mingkai Hub36ece82010-10-12 18:18:31 +0800477 events = mpc8xxx_spi_read_reg(&reg_base->event);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400478 if (events)
479 ret = IRQ_HANDLED;
480
481 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
482
483 if (mspi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800484 fsl_spi_cpm_irq(mspi, events);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400485 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800486 fsl_spi_cpu_irq(mspi, events);
Kumar Galaccf06992006-05-20 15:00:15 -0700487
488 return ret;
489}
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400490
Mingkai Hub36ece82010-10-12 18:18:31 +0800491static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400492{
Mingkai Hub36ece82010-10-12 18:18:31 +0800493 iounmap(mspi->reg_base);
494 fsl_spi_cpm_free(mspi);
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400495}
496
Grant Likelyfd4a3192012-12-07 16:57:14 +0000497static struct spi_master * fsl_spi_probe(struct device *dev,
Mingkai Hub36ece82010-10-12 18:18:31 +0800498 struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700499{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700500 struct fsl_spi_platform_data *pdata = dev->platform_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700501 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700502 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800503 struct fsl_spi_reg *reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700504 u32 regval;
505 int ret = 0;
506
Anton Vorontsov575c5802009-06-18 16:49:08 -0700507 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700508 if (master == NULL) {
509 ret = -ENOMEM;
510 goto err;
511 }
512
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700513 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -0700514
Mingkai Hub36ece82010-10-12 18:18:31 +0800515 ret = mpc8xxx_spi_probe(dev, mem, irq);
516 if (ret)
517 goto err_probe;
David Brownelle7db06b2009-06-17 16:26:04 -0700518
Mingkai Hub36ece82010-10-12 18:18:31 +0800519 master->setup = fsl_spi_setup;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700520
Anton Vorontsov575c5802009-06-18 16:49:08 -0700521 mpc8xxx_spi = spi_master_get_devdata(master);
Mingkai Hub36ece82010-10-12 18:18:31 +0800522 mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
523 mpc8xxx_spi->spi_remove = fsl_spi_remove;
Andreas Larsson8922a362013-02-15 16:52:25 +0100524 mpc8xxx_spi->max_bits_per_word = 32;
Andreas Larssonc3f3e772013-02-15 16:52:24 +0100525 mpc8xxx_spi->type = fsl_spi_get_type(dev);
Mingkai Hub36ece82010-10-12 18:18:31 +0800526
527 ret = fsl_spi_cpm_init(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400528 if (ret)
529 goto err_cpm_init;
530
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100531 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
532 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
533
534 if (mpc8xxx_spi->set_shifts)
535 /* 8 bits per word and MSB first */
536 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
537 &mpc8xxx_spi->tx_shift, 8, 1);
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700538
Mingkai Hub36ece82010-10-12 18:18:31 +0800539 mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
540 if (mpc8xxx_spi->reg_base == NULL) {
Kumar Galaccf06992006-05-20 15:00:15 -0700541 ret = -ENOMEM;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400542 goto err_ioremap;
Kumar Galaccf06992006-05-20 15:00:15 -0700543 }
544
Kumar Galaccf06992006-05-20 15:00:15 -0700545 /* Register for SPI Interrupt */
Mingkai Hub36ece82010-10-12 18:18:31 +0800546 ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
547 0, "fsl_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700548
549 if (ret != 0)
Mingkai Hub36ece82010-10-12 18:18:31 +0800550 goto free_irq;
Kumar Galaccf06992006-05-20 15:00:15 -0700551
Mingkai Hub36ece82010-10-12 18:18:31 +0800552 reg_base = mpc8xxx_spi->reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700553
554 /* SPI controller initializations */
Mingkai Hub36ece82010-10-12 18:18:31 +0800555 mpc8xxx_spi_write_reg(&reg_base->mode, 0);
556 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
557 mpc8xxx_spi_write_reg(&reg_base->command, 0);
558 mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -0700559
560 /* Enable SPI interface */
561 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Andreas Larsson8922a362013-02-15 16:52:25 +0100562 if (mpc8xxx_spi->max_bits_per_word < 8) {
563 regval &= ~SPMODE_LEN(0xF);
564 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
565 }
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400566 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700567 regval |= SPMODE_OP;
568
Mingkai Hub36ece82010-10-12 18:18:31 +0800569 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700570
571 ret = spi_register_master(master);
572 if (ret < 0)
573 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -0700574
Mingkai Hub36ece82010-10-12 18:18:31 +0800575 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400576 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
Kumar Galaccf06992006-05-20 15:00:15 -0700577
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700578 return master;
Kumar Galaccf06992006-05-20 15:00:15 -0700579
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700580unreg_master:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700581 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
Mingkai Hub36ece82010-10-12 18:18:31 +0800582free_irq:
583 iounmap(mpc8xxx_spi->reg_base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400584err_ioremap:
Mingkai Hub36ece82010-10-12 18:18:31 +0800585 fsl_spi_cpm_free(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400586err_cpm_init:
Mingkai Hub36ece82010-10-12 18:18:31 +0800587err_probe:
Kumar Galaccf06992006-05-20 15:00:15 -0700588 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -0700589err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700590 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -0700591}
592
Mingkai Hub36ece82010-10-12 18:18:31 +0800593static void fsl_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700594{
Herton Ronaldo Krzesinski067aa482012-05-11 15:29:50 -0700595 struct device *dev = spi->dev.parent->parent;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700596 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700597 u16 cs = spi->chip_select;
598 int gpio = pinfo->gpios[cs];
599 bool alow = pinfo->alow_flags[cs];
600
601 gpio_set_value(gpio, on ^ alow);
602}
603
Mingkai Hub36ece82010-10-12 18:18:31 +0800604static int of_fsl_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700605{
Grant Likely61c7a082010-04-13 16:12:29 -0700606 struct device_node *np = dev->of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700607 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700608 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Grant Likelye80beb22013-02-12 17:48:37 +0000609 int ngpios;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700610 int i = 0;
611 int ret;
612
613 ngpios = of_gpio_count(np);
Grant Likelye80beb22013-02-12 17:48:37 +0000614 if (ngpios <= 0) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700615 /*
616 * SPI w/o chip-select line. One SPI device is still permitted
617 * though.
618 */
619 pdata->max_chipselect = 1;
620 return 0;
621 }
622
Roel Kluin02141542009-06-16 15:31:15 -0700623 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700624 if (!pinfo->gpios)
625 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -0700626 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700627
Roel Kluin02141542009-06-16 15:31:15 -0700628 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700629 GFP_KERNEL);
630 if (!pinfo->alow_flags) {
631 ret = -ENOMEM;
632 goto err_alloc_flags;
633 }
634
635 for (; i < ngpios; i++) {
636 int gpio;
637 enum of_gpio_flags flags;
638
639 gpio = of_get_gpio_flags(np, i, &flags);
640 if (!gpio_is_valid(gpio)) {
641 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
Anton Vorontsov783058f2009-10-12 20:49:22 +0400642 ret = gpio;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700643 goto err_loop;
644 }
645
646 ret = gpio_request(gpio, dev_name(dev));
647 if (ret) {
648 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
649 goto err_loop;
650 }
651
652 pinfo->gpios[i] = gpio;
653 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
654
655 ret = gpio_direction_output(pinfo->gpios[i],
656 pinfo->alow_flags[i]);
657 if (ret) {
658 dev_err(dev, "can't set output direction for gpio "
659 "#%d: %d\n", i, ret);
660 goto err_loop;
661 }
662 }
663
664 pdata->max_chipselect = ngpios;
Mingkai Hub36ece82010-10-12 18:18:31 +0800665 pdata->cs_control = fsl_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700666
667 return 0;
668
669err_loop:
670 while (i >= 0) {
671 if (gpio_is_valid(pinfo->gpios[i]))
672 gpio_free(pinfo->gpios[i]);
673 i--;
674 }
675
676 kfree(pinfo->alow_flags);
677 pinfo->alow_flags = NULL;
678err_alloc_flags:
679 kfree(pinfo->gpios);
680 pinfo->gpios = NULL;
681 return ret;
682}
683
Mingkai Hub36ece82010-10-12 18:18:31 +0800684static int of_fsl_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700685{
686 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700687 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700688 int i;
689
690 if (!pinfo->gpios)
691 return 0;
692
693 for (i = 0; i < pdata->max_chipselect; i++) {
694 if (gpio_is_valid(pinfo->gpios[i]))
695 gpio_free(pinfo->gpios[i]);
696 }
697
698 kfree(pinfo->gpios);
699 kfree(pinfo->alow_flags);
700 return 0;
701}
702
Grant Likelyfd4a3192012-12-07 16:57:14 +0000703static int of_fsl_spi_probe(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700704{
705 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700706 struct device_node *np = ofdev->dev.of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700707 struct spi_master *master;
708 struct resource mem;
Andreas Larssone8beacb2013-02-15 16:52:21 +0100709 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700710 int ret = -ENOMEM;
711
Grant Likely18d306d2011-02-22 21:02:43 -0700712 ret = of_mpc8xxx_spi_probe(ofdev);
Mingkai Hub36ece82010-10-12 18:18:31 +0800713 if (ret)
714 return ret;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700715
Mingkai Hub36ece82010-10-12 18:18:31 +0800716 ret = of_fsl_spi_get_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700717 if (ret)
718 goto err;
719
720 ret = of_address_to_resource(np, 0, &mem);
721 if (ret)
722 goto err;
723
Andreas Larssone8beacb2013-02-15 16:52:21 +0100724 irq = irq_of_parse_and_map(np, 0);
725 if (!irq) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700726 ret = -EINVAL;
727 goto err;
728 }
729
Andreas Larssone8beacb2013-02-15 16:52:21 +0100730 master = fsl_spi_probe(dev, &mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700731 if (IS_ERR(master)) {
732 ret = PTR_ERR(master);
733 goto err;
734 }
735
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700736 return 0;
737
738err:
Mingkai Hub36ece82010-10-12 18:18:31 +0800739 of_fsl_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700740 return ret;
741}
742
Grant Likelyfd4a3192012-12-07 16:57:14 +0000743static int of_fsl_spi_remove(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700744{
745 int ret;
746
Anton Vorontsov575c5802009-06-18 16:49:08 -0700747 ret = mpc8xxx_spi_remove(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700748 if (ret)
749 return ret;
Mingkai Hub36ece82010-10-12 18:18:31 +0800750 of_fsl_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700751 return 0;
752}
753
Grant Likely18d306d2011-02-22 21:02:43 -0700754static struct platform_driver of_fsl_spi_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700755 .driver = {
Mingkai Hub36ece82010-10-12 18:18:31 +0800756 .name = "fsl_spi",
Grant Likely40182942010-04-13 16:13:02 -0700757 .owner = THIS_MODULE,
Mingkai Hub36ece82010-10-12 18:18:31 +0800758 .of_match_table = of_fsl_spi_match,
Grant Likely40182942010-04-13 16:13:02 -0700759 },
Mingkai Hub36ece82010-10-12 18:18:31 +0800760 .probe = of_fsl_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000761 .remove = of_fsl_spi_remove,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700762};
763
764#ifdef CONFIG_MPC832x_RDB
765/*
Mingkai Hub36ece82010-10-12 18:18:31 +0800766 * XXX XXX XXX
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700767 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
768 * only. The driver should go away soon, since newer MPC8323E-RDB's device
769 * tree can work with OpenFirmware driver. But for now we support old trees
770 * as well.
771 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000772static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700773{
774 struct resource *mem;
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -0700775 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700776 struct spi_master *master;
777
778 if (!pdev->dev.platform_data)
779 return -EINVAL;
780
781 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
782 if (!mem)
783 return -EINVAL;
784
785 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -0700786 if (irq <= 0)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700787 return -EINVAL;
788
Mingkai Hub36ece82010-10-12 18:18:31 +0800789 master = fsl_spi_probe(&pdev->dev, mem, irq);
Alexandru Gheorghiue4d43782013-03-14 11:07:31 +0200790 return PTR_RET(master);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700791}
792
Grant Likelyfd4a3192012-12-07 16:57:14 +0000793static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700794{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700795 return mpc8xxx_spi_remove(&pdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700796}
797
Anton Vorontsov575c5802009-06-18 16:49:08 -0700798MODULE_ALIAS("platform:mpc8xxx_spi");
799static struct platform_driver mpc8xxx_spi_driver = {
800 .probe = plat_mpc8xxx_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000801 .remove = plat_mpc8xxx_spi_remove,
Kumar Galaccf06992006-05-20 15:00:15 -0700802 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700803 .name = "mpc8xxx_spi",
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700804 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -0700805 },
806};
807
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700808static bool legacy_driver_failed;
809
810static void __init legacy_driver_register(void)
811{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700812 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700813}
814
815static void __exit legacy_driver_unregister(void)
816{
817 if (legacy_driver_failed)
818 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700819 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700820}
821#else
822static void __init legacy_driver_register(void) {}
823static void __exit legacy_driver_unregister(void) {}
824#endif /* CONFIG_MPC832x_RDB */
825
Mingkai Hub36ece82010-10-12 18:18:31 +0800826static int __init fsl_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700827{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700828 legacy_driver_register();
Grant Likely18d306d2011-02-22 21:02:43 -0700829 return platform_driver_register(&of_fsl_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -0700830}
Mingkai Hub36ece82010-10-12 18:18:31 +0800831module_init(fsl_spi_init);
Kumar Galaccf06992006-05-20 15:00:15 -0700832
Mingkai Hub36ece82010-10-12 18:18:31 +0800833static void __exit fsl_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700834{
Grant Likely18d306d2011-02-22 21:02:43 -0700835 platform_driver_unregister(&of_fsl_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700836 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -0700837}
Mingkai Hub36ece82010-10-12 18:18:31 +0800838module_exit(fsl_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -0700839
840MODULE_AUTHOR("Kumar Gala");
Mingkai Hub36ece82010-10-12 18:18:31 +0800841MODULE_DESCRIPTION("Simple Freescale SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -0700842MODULE_LICENSE("GPL");