blob: aba00fd0791f30b0c7674cf299784e49d8406443 [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
Mingkai Hub36ece82010-10-12 18:18:31 +080042static void fsl_spi_change_mode(struct spi_device *spi)
Anton Vorontsova35c1712009-10-12 20:49:24 +040043{
44 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
45 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Mingkai Hub36ece82010-10-12 18:18:31 +080046 struct fsl_spi_reg *reg_base = mspi->reg_base;
47 __be32 __iomem *mode = &reg_base->mode;
Anton Vorontsova35c1712009-10-12 20:49:24 +040048 unsigned long flags;
49
50 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
51 return;
52
53 /* Turn off IRQs locally to minimize time that SPI is disabled. */
54 local_irq_save(flags);
55
56 /* Turn off SPI unit prior changing mode */
57 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
Anton Vorontsova35c1712009-10-12 20:49:24 +040058
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040059 /* When in CPM mode, we need to reinit tx and rx. */
60 if (mspi->flags & SPI_CPM_MODE) {
Andreas Larssone8beacb2013-02-15 16:52:21 +010061 fsl_spi_cpm_reinit_txrx(mspi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040062 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -060063 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
Anton Vorontsova35c1712009-10-12 20:49:24 +040064 local_irq_restore(flags);
65}
66
Mingkai Hub36ece82010-10-12 18:18:31 +080067static void fsl_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -070068{
Anton Vorontsov575c5802009-06-18 16:49:08 -070069 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kenth Eriksson5039a862012-03-30 17:05:30 +020070 struct fsl_spi_platform_data *pdata;
Anton Vorontsov364fdbc2009-03-31 15:24:36 -070071 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -070072 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -070073
Kenth Eriksson5039a862012-03-30 17:05:30 +020074 pdata = spi->dev.parent->parent->platform_data;
75
Kumar Galaccf06992006-05-20 15:00:15 -070076 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -070077 if (pdata->cs_control)
78 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -070079 }
80
81 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -070082 mpc8xxx_spi->rx_shift = cs->rx_shift;
83 mpc8xxx_spi->tx_shift = cs->tx_shift;
84 mpc8xxx_spi->get_rx = cs->get_rx;
85 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -070086
Mingkai Hub36ece82010-10-12 18:18:31 +080087 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -070088
Anton Vorontsov364fdbc2009-03-31 15:24:36 -070089 if (pdata->cs_control)
90 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -070091 }
92}
93
Andreas Larssonb48c4e32013-02-15 16:52:23 +010094static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
95 int bits_per_word, int msb_first)
96{
97 *rx_shift = 0;
98 *tx_shift = 0;
99 if (msb_first) {
100 if (bits_per_word <= 8) {
101 *rx_shift = 16;
102 *tx_shift = 24;
103 } else if (bits_per_word <= 16) {
104 *rx_shift = 16;
105 *tx_shift = 16;
106 }
107 } else {
108 if (bits_per_word <= 8)
109 *rx_shift = 8;
110 }
111}
112
Mingkai Hub36ece82010-10-12 18:18:31 +0800113static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
114 struct spi_device *spi,
115 struct mpc8xxx_spi *mpc8xxx_spi,
116 int bits_per_word)
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000117{
118 cs->rx_shift = 0;
119 cs->tx_shift = 0;
120 if (bits_per_word <= 8) {
121 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
122 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000123 } else if (bits_per_word <= 16) {
124 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
125 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000126 } else if (bits_per_word <= 32) {
127 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
128 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
129 } else
130 return -EINVAL;
131
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100132 if (mpc8xxx_spi->set_shifts)
133 mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
134 bits_per_word,
135 !(spi->mode & SPI_LSB_FIRST));
136
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000137 mpc8xxx_spi->rx_shift = cs->rx_shift;
138 mpc8xxx_spi->tx_shift = cs->tx_shift;
139 mpc8xxx_spi->get_rx = cs->get_rx;
140 mpc8xxx_spi->get_tx = cs->get_tx;
141
142 return bits_per_word;
143}
144
Mingkai Hub36ece82010-10-12 18:18:31 +0800145static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
146 struct spi_device *spi,
147 int bits_per_word)
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000148{
149 /* QE uses Little Endian for words > 8
150 * so transform all words > 8 into 8 bits
151 * Unfortnatly that doesn't work for LSB so
152 * reject these for now */
153 /* Note: 32 bits word, LSB works iff
154 * tfcr/rfcr is set to CPMFCR_GBL */
155 if (spi->mode & SPI_LSB_FIRST &&
156 bits_per_word > 8)
157 return -EINVAL;
158 if (bits_per_word > 8)
159 return 8; /* pretend its 8 bits */
160 return bits_per_word;
161}
162
Mingkai Hub36ece82010-10-12 18:18:31 +0800163static int fsl_spi_setup_transfer(struct spi_device *spi,
164 struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700165{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700166 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800167 int bits_per_word = 0;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000168 u8 pm;
Mingkai Hub36ece82010-10-12 18:18:31 +0800169 u32 hz = 0;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700170 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700171
Anton Vorontsov575c5802009-06-18 16:49:08 -0700172 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700173
174 if (t) {
175 bits_per_word = t->bits_per_word;
176 hz = t->speed_hz;
Kumar Galaccf06992006-05-20 15:00:15 -0700177 }
178
179 /* spi_transfer level calls that work per-word */
180 if (!bits_per_word)
181 bits_per_word = spi->bits_per_word;
182
183 /* Make sure its a bit width we support [4..16, 32] */
184 if ((bits_per_word < 4)
185 || ((bits_per_word > 16) && (bits_per_word != 32)))
186 return -EINVAL;
187
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700188 if (!hz)
189 hz = spi->max_speed_hz;
190
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000191 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
192 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
193 mpc8xxx_spi,
194 bits_per_word);
195 else if (mpc8xxx_spi->flags & SPI_QE)
196 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
197 bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700198
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000199 if (bits_per_word < 0)
200 return bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700201
202 if (bits_per_word == 32)
203 bits_per_word = 0;
204 else
205 bits_per_word = bits_per_word - 1;
206
Anton Vorontsov32421da2007-07-31 00:38:41 -0700207 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700208 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
209 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700210
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700211 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700212
Anton Vorontsov575c5802009-06-18 16:49:08 -0700213 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700214 cs->hw_mode |= SPMODE_DIV16;
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700215 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700216
217 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
218 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Anton Vorontsov575c5802009-06-18 16:49:08 -0700219 hz, mpc8xxx_spi->spibrg / 1024);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700220 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700221 pm = 16;
Mingkai Hub36ece82010-10-12 18:18:31 +0800222 } else {
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700223 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
Mingkai Hub36ece82010-10-12 18:18:31 +0800224 }
Chen Gonga61f5342008-07-23 21:29:52 -0700225 if (pm)
226 pm--;
227
228 cs->hw_mode |= SPMODE_PM(pm);
David Brownelldccd5732007-07-17 04:04:02 -0700229
Mingkai Hub36ece82010-10-12 18:18:31 +0800230 fsl_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700231 return 0;
232}
233
Mingkai Hub36ece82010-10-12 18:18:31 +0800234static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400235 struct spi_transfer *t, unsigned int len)
236{
237 u32 word;
Mingkai Hub36ece82010-10-12 18:18:31 +0800238 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400239
240 mspi->count = len;
241
242 /* enable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800243 mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400244
245 /* transmit word */
246 word = mspi->get_tx(mspi);
Mingkai Hub36ece82010-10-12 18:18:31 +0800247 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400248
249 return 0;
250}
251
Mingkai Hub36ece82010-10-12 18:18:31 +0800252static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400253 bool is_dma_mapped)
254{
255 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Mingkai Hub36ece82010-10-12 18:18:31 +0800256 struct fsl_spi_reg *reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400257 unsigned int len = t->len;
258 u8 bits_per_word;
259 int ret;
260
Mingkai Hub36ece82010-10-12 18:18:31 +0800261 reg_base = mpc8xxx_spi->reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700262 bits_per_word = spi->bits_per_word;
263 if (t->bits_per_word)
264 bits_per_word = t->bits_per_word;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400265
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700266 if (bits_per_word > 8) {
267 /* invalid length? */
268 if (len & 1)
269 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700270 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700271 }
272 if (bits_per_word > 16) {
273 /* invalid length? */
274 if (len & 1)
275 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700276 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700277 }
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400278
279 mpc8xxx_spi->tx = t->tx_buf;
280 mpc8xxx_spi->rx = t->rx_buf;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700281
Anton Vorontsov575c5802009-06-18 16:49:08 -0700282 INIT_COMPLETION(mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700283
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400284 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800285 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400286 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800287 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400288 if (ret)
289 return ret;
Kumar Galaccf06992006-05-20 15:00:15 -0700290
Anton Vorontsov575c5802009-06-18 16:49:08 -0700291 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700292
293 /* disable rx ints */
Mingkai Hub36ece82010-10-12 18:18:31 +0800294 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700295
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400296 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800297 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400298
Anton Vorontsov575c5802009-06-18 16:49:08 -0700299 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700300}
301
Mingkai Hub36ece82010-10-12 18:18:31 +0800302static void fsl_spi_do_one_msg(struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700303{
304 struct spi_device *spi = m->spi;
305 struct spi_transfer *t;
306 unsigned int cs_change;
307 const int nsecs = 50;
308 int status;
309
310 cs_change = 1;
311 status = 0;
312 list_for_each_entry(t, &m->transfers, transfer_list) {
313 if (t->bits_per_word || t->speed_hz) {
314 /* Don't allow changes if CS is active */
315 status = -EINVAL;
316
317 if (cs_change)
Mingkai Hub36ece82010-10-12 18:18:31 +0800318 status = fsl_spi_setup_transfer(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700319 if (status < 0)
320 break;
321 }
322
323 if (cs_change) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800324 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700325 ndelay(nsecs);
326 }
327 cs_change = t->cs_change;
328 if (t->len)
Mingkai Hub36ece82010-10-12 18:18:31 +0800329 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700330 if (status) {
331 status = -EMSGSIZE;
332 break;
333 }
334 m->actual_length += t->len;
335
336 if (t->delay_usecs)
337 udelay(t->delay_usecs);
338
339 if (cs_change) {
340 ndelay(nsecs);
Mingkai Hub36ece82010-10-12 18:18:31 +0800341 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700342 ndelay(nsecs);
343 }
344 }
345
346 m->status = status;
347 m->complete(m->context);
348
349 if (status || !cs_change) {
350 ndelay(nsecs);
Mingkai Hub36ece82010-10-12 18:18:31 +0800351 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700352 }
353
Mingkai Hub36ece82010-10-12 18:18:31 +0800354 fsl_spi_setup_transfer(spi, NULL);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700355}
356
Mingkai Hub36ece82010-10-12 18:18:31 +0800357static int fsl_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700358{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700359 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800360 struct fsl_spi_reg *reg_base;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700361 int retval;
362 u32 hw_mode;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700363 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700364
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700365 if (!spi->max_speed_hz)
366 return -EINVAL;
367
368 if (!cs) {
369 cs = kzalloc(sizeof *cs, GFP_KERNEL);
370 if (!cs)
371 return -ENOMEM;
372 spi->controller_state = cs;
373 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700374 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700375
Mingkai Hub36ece82010-10-12 18:18:31 +0800376 reg_base = mpc8xxx_spi->reg_base;
377
Thomas Weber88393162010-03-16 11:47:56 +0100378 hw_mode = cs->hw_mode; /* Save original settings */
Mingkai Hub36ece82010-10-12 18:18:31 +0800379 cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700380 /* mask out bits we are going to set */
381 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
382 | SPMODE_REV | SPMODE_LOOP);
383
384 if (spi->mode & SPI_CPHA)
385 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
386 if (spi->mode & SPI_CPOL)
387 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
388 if (!(spi->mode & SPI_LSB_FIRST))
389 cs->hw_mode |= SPMODE_REV;
390 if (spi->mode & SPI_LOOP)
391 cs->hw_mode |= SPMODE_LOOP;
392
Mingkai Hub36ece82010-10-12 18:18:31 +0800393 retval = fsl_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700394 if (retval < 0) {
395 cs->hw_mode = hw_mode; /* Restore settings */
396 return retval;
397 }
Andreas Larssonf482cd02013-02-15 16:52:22 +0100398
399 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
400 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
401
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700402 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700403}
404
Mingkai Hub36ece82010-10-12 18:18:31 +0800405static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400406{
Mingkai Hub36ece82010-10-12 18:18:31 +0800407 struct fsl_spi_reg *reg_base = mspi->reg_base;
408
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400409 /* We need handle RX first */
410 if (events & SPIE_NE) {
Mingkai Hub36ece82010-10-12 18:18:31 +0800411 u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400412
413 if (mspi->rx)
414 mspi->get_rx(rx_data, mspi);
415 }
416
417 if ((events & SPIE_NF) == 0)
418 /* spin until TX is done */
419 while (((events =
Mingkai Hub36ece82010-10-12 18:18:31 +0800420 mpc8xxx_spi_read_reg(&reg_base->event)) &
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400421 SPIE_NF) == 0)
422 cpu_relax();
423
424 /* Clear the events */
Mingkai Hub36ece82010-10-12 18:18:31 +0800425 mpc8xxx_spi_write_reg(&reg_base->event, events);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400426
427 mspi->count -= 1;
428 if (mspi->count) {
429 u32 word = mspi->get_tx(mspi);
430
Mingkai Hub36ece82010-10-12 18:18:31 +0800431 mpc8xxx_spi_write_reg(&reg_base->transmit, word);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400432 } else {
433 complete(&mspi->done);
434 }
435}
436
Mingkai Hub36ece82010-10-12 18:18:31 +0800437static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400438{
439 struct mpc8xxx_spi *mspi = context_data;
440 irqreturn_t ret = IRQ_NONE;
441 u32 events;
Mingkai Hub36ece82010-10-12 18:18:31 +0800442 struct fsl_spi_reg *reg_base = mspi->reg_base;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400443
444 /* Get interrupt events(tx/rx) */
Mingkai Hub36ece82010-10-12 18:18:31 +0800445 events = mpc8xxx_spi_read_reg(&reg_base->event);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400446 if (events)
447 ret = IRQ_HANDLED;
448
449 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
450
451 if (mspi->flags & SPI_CPM_MODE)
Mingkai Hub36ece82010-10-12 18:18:31 +0800452 fsl_spi_cpm_irq(mspi, events);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400453 else
Mingkai Hub36ece82010-10-12 18:18:31 +0800454 fsl_spi_cpu_irq(mspi, events);
Kumar Galaccf06992006-05-20 15:00:15 -0700455
456 return ret;
457}
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400458
Mingkai Hub36ece82010-10-12 18:18:31 +0800459static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400460{
Mingkai Hub36ece82010-10-12 18:18:31 +0800461 iounmap(mspi->reg_base);
462 fsl_spi_cpm_free(mspi);
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400463}
464
Grant Likelyfd4a3192012-12-07 16:57:14 +0000465static struct spi_master * fsl_spi_probe(struct device *dev,
Mingkai Hub36ece82010-10-12 18:18:31 +0800466 struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700467{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700468 struct fsl_spi_platform_data *pdata = dev->platform_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700469 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700470 struct mpc8xxx_spi *mpc8xxx_spi;
Mingkai Hub36ece82010-10-12 18:18:31 +0800471 struct fsl_spi_reg *reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700472 u32 regval;
473 int ret = 0;
474
Anton Vorontsov575c5802009-06-18 16:49:08 -0700475 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700476 if (master == NULL) {
477 ret = -ENOMEM;
478 goto err;
479 }
480
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700481 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -0700482
Mingkai Hub36ece82010-10-12 18:18:31 +0800483 ret = mpc8xxx_spi_probe(dev, mem, irq);
484 if (ret)
485 goto err_probe;
David Brownelle7db06b2009-06-17 16:26:04 -0700486
Mingkai Hub36ece82010-10-12 18:18:31 +0800487 master->setup = fsl_spi_setup;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700488
Anton Vorontsov575c5802009-06-18 16:49:08 -0700489 mpc8xxx_spi = spi_master_get_devdata(master);
Mingkai Hub36ece82010-10-12 18:18:31 +0800490 mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
491 mpc8xxx_spi->spi_remove = fsl_spi_remove;
Anton Vorontsove24a4d12007-08-10 13:01:01 -0700492
Mingkai Hub36ece82010-10-12 18:18:31 +0800493
494 ret = fsl_spi_cpm_init(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400495 if (ret)
496 goto err_cpm_init;
497
Andreas Larssonb48c4e32013-02-15 16:52:23 +0100498 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
499 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
500
501 if (mpc8xxx_spi->set_shifts)
502 /* 8 bits per word and MSB first */
503 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
504 &mpc8xxx_spi->tx_shift, 8, 1);
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700505
Mingkai Hub36ece82010-10-12 18:18:31 +0800506 mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
507 if (mpc8xxx_spi->reg_base == NULL) {
Kumar Galaccf06992006-05-20 15:00:15 -0700508 ret = -ENOMEM;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400509 goto err_ioremap;
Kumar Galaccf06992006-05-20 15:00:15 -0700510 }
511
Kumar Galaccf06992006-05-20 15:00:15 -0700512 /* Register for SPI Interrupt */
Mingkai Hub36ece82010-10-12 18:18:31 +0800513 ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
514 0, "fsl_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700515
516 if (ret != 0)
Mingkai Hub36ece82010-10-12 18:18:31 +0800517 goto free_irq;
Kumar Galaccf06992006-05-20 15:00:15 -0700518
Mingkai Hub36ece82010-10-12 18:18:31 +0800519 reg_base = mpc8xxx_spi->reg_base;
Kumar Galaccf06992006-05-20 15:00:15 -0700520
521 /* SPI controller initializations */
Mingkai Hub36ece82010-10-12 18:18:31 +0800522 mpc8xxx_spi_write_reg(&reg_base->mode, 0);
523 mpc8xxx_spi_write_reg(&reg_base->mask, 0);
524 mpc8xxx_spi_write_reg(&reg_base->command, 0);
525 mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -0700526
527 /* Enable SPI interface */
528 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400529 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700530 regval |= SPMODE_OP;
531
Mingkai Hub36ece82010-10-12 18:18:31 +0800532 mpc8xxx_spi_write_reg(&reg_base->mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700533
534 ret = spi_register_master(master);
535 if (ret < 0)
536 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -0700537
Mingkai Hub36ece82010-10-12 18:18:31 +0800538 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400539 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
Kumar Galaccf06992006-05-20 15:00:15 -0700540
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700541 return master;
Kumar Galaccf06992006-05-20 15:00:15 -0700542
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700543unreg_master:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700544 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
Mingkai Hub36ece82010-10-12 18:18:31 +0800545free_irq:
546 iounmap(mpc8xxx_spi->reg_base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400547err_ioremap:
Mingkai Hub36ece82010-10-12 18:18:31 +0800548 fsl_spi_cpm_free(mpc8xxx_spi);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400549err_cpm_init:
Mingkai Hub36ece82010-10-12 18:18:31 +0800550err_probe:
Kumar Galaccf06992006-05-20 15:00:15 -0700551 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -0700552err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700553 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -0700554}
555
Mingkai Hub36ece82010-10-12 18:18:31 +0800556static void fsl_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700557{
Herton Ronaldo Krzesinski067aa482012-05-11 15:29:50 -0700558 struct device *dev = spi->dev.parent->parent;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700559 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700560 u16 cs = spi->chip_select;
561 int gpio = pinfo->gpios[cs];
562 bool alow = pinfo->alow_flags[cs];
563
564 gpio_set_value(gpio, on ^ alow);
565}
566
Mingkai Hub36ece82010-10-12 18:18:31 +0800567static int of_fsl_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700568{
Grant Likely61c7a082010-04-13 16:12:29 -0700569 struct device_node *np = dev->of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700570 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700571 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Grant Likelye80beb22013-02-12 17:48:37 +0000572 int ngpios;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700573 int i = 0;
574 int ret;
575
576 ngpios = of_gpio_count(np);
Grant Likelye80beb22013-02-12 17:48:37 +0000577 if (ngpios <= 0) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700578 /*
579 * SPI w/o chip-select line. One SPI device is still permitted
580 * though.
581 */
582 pdata->max_chipselect = 1;
583 return 0;
584 }
585
Roel Kluin02141542009-06-16 15:31:15 -0700586 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700587 if (!pinfo->gpios)
588 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -0700589 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700590
Roel Kluin02141542009-06-16 15:31:15 -0700591 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700592 GFP_KERNEL);
593 if (!pinfo->alow_flags) {
594 ret = -ENOMEM;
595 goto err_alloc_flags;
596 }
597
598 for (; i < ngpios; i++) {
599 int gpio;
600 enum of_gpio_flags flags;
601
602 gpio = of_get_gpio_flags(np, i, &flags);
603 if (!gpio_is_valid(gpio)) {
604 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
Anton Vorontsov783058f2009-10-12 20:49:22 +0400605 ret = gpio;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700606 goto err_loop;
607 }
608
609 ret = gpio_request(gpio, dev_name(dev));
610 if (ret) {
611 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
612 goto err_loop;
613 }
614
615 pinfo->gpios[i] = gpio;
616 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
617
618 ret = gpio_direction_output(pinfo->gpios[i],
619 pinfo->alow_flags[i]);
620 if (ret) {
621 dev_err(dev, "can't set output direction for gpio "
622 "#%d: %d\n", i, ret);
623 goto err_loop;
624 }
625 }
626
627 pdata->max_chipselect = ngpios;
Mingkai Hub36ece82010-10-12 18:18:31 +0800628 pdata->cs_control = fsl_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700629
630 return 0;
631
632err_loop:
633 while (i >= 0) {
634 if (gpio_is_valid(pinfo->gpios[i]))
635 gpio_free(pinfo->gpios[i]);
636 i--;
637 }
638
639 kfree(pinfo->alow_flags);
640 pinfo->alow_flags = NULL;
641err_alloc_flags:
642 kfree(pinfo->gpios);
643 pinfo->gpios = NULL;
644 return ret;
645}
646
Mingkai Hub36ece82010-10-12 18:18:31 +0800647static int of_fsl_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700648{
649 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700650 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700651 int i;
652
653 if (!pinfo->gpios)
654 return 0;
655
656 for (i = 0; i < pdata->max_chipselect; i++) {
657 if (gpio_is_valid(pinfo->gpios[i]))
658 gpio_free(pinfo->gpios[i]);
659 }
660
661 kfree(pinfo->gpios);
662 kfree(pinfo->alow_flags);
663 return 0;
664}
665
Grant Likelyfd4a3192012-12-07 16:57:14 +0000666static int of_fsl_spi_probe(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700667{
668 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700669 struct device_node *np = ofdev->dev.of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700670 struct spi_master *master;
671 struct resource mem;
Andreas Larssone8beacb2013-02-15 16:52:21 +0100672 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700673 int ret = -ENOMEM;
674
Grant Likely18d306d2011-02-22 21:02:43 -0700675 ret = of_mpc8xxx_spi_probe(ofdev);
Mingkai Hub36ece82010-10-12 18:18:31 +0800676 if (ret)
677 return ret;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700678
Mingkai Hub36ece82010-10-12 18:18:31 +0800679 ret = of_fsl_spi_get_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700680 if (ret)
681 goto err;
682
683 ret = of_address_to_resource(np, 0, &mem);
684 if (ret)
685 goto err;
686
Andreas Larssone8beacb2013-02-15 16:52:21 +0100687 irq = irq_of_parse_and_map(np, 0);
688 if (!irq) {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700689 ret = -EINVAL;
690 goto err;
691 }
692
Andreas Larssone8beacb2013-02-15 16:52:21 +0100693 master = fsl_spi_probe(dev, &mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700694 if (IS_ERR(master)) {
695 ret = PTR_ERR(master);
696 goto err;
697 }
698
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700699 return 0;
700
701err:
Mingkai Hub36ece82010-10-12 18:18:31 +0800702 of_fsl_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700703 return ret;
704}
705
Grant Likelyfd4a3192012-12-07 16:57:14 +0000706static int of_fsl_spi_remove(struct platform_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700707{
708 int ret;
709
Anton Vorontsov575c5802009-06-18 16:49:08 -0700710 ret = mpc8xxx_spi_remove(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700711 if (ret)
712 return ret;
Mingkai Hub36ece82010-10-12 18:18:31 +0800713 of_fsl_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700714 return 0;
715}
716
Mingkai Hub36ece82010-10-12 18:18:31 +0800717static const struct of_device_id of_fsl_spi_match[] = {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700718 { .compatible = "fsl,spi" },
Mingkai Hub36ece82010-10-12 18:18:31 +0800719 {}
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700720};
Mingkai Hub36ece82010-10-12 18:18:31 +0800721MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700722
Grant Likely18d306d2011-02-22 21:02:43 -0700723static struct platform_driver of_fsl_spi_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700724 .driver = {
Mingkai Hub36ece82010-10-12 18:18:31 +0800725 .name = "fsl_spi",
Grant Likely40182942010-04-13 16:13:02 -0700726 .owner = THIS_MODULE,
Mingkai Hub36ece82010-10-12 18:18:31 +0800727 .of_match_table = of_fsl_spi_match,
Grant Likely40182942010-04-13 16:13:02 -0700728 },
Mingkai Hub36ece82010-10-12 18:18:31 +0800729 .probe = of_fsl_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000730 .remove = of_fsl_spi_remove,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700731};
732
733#ifdef CONFIG_MPC832x_RDB
734/*
Mingkai Hub36ece82010-10-12 18:18:31 +0800735 * XXX XXX XXX
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700736 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
737 * only. The driver should go away soon, since newer MPC8323E-RDB's device
738 * tree can work with OpenFirmware driver. But for now we support old trees
739 * as well.
740 */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000741static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700742{
743 struct resource *mem;
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -0700744 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700745 struct spi_master *master;
746
747 if (!pdev->dev.platform_data)
748 return -EINVAL;
749
750 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
751 if (!mem)
752 return -EINVAL;
753
754 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -0700755 if (irq <= 0)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700756 return -EINVAL;
757
Mingkai Hub36ece82010-10-12 18:18:31 +0800758 master = fsl_spi_probe(&pdev->dev, mem, irq);
Alexandru Gheorghiue4d43782013-03-14 11:07:31 +0200759 return PTR_RET(master);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700760}
761
Grant Likelyfd4a3192012-12-07 16:57:14 +0000762static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700763{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700764 return mpc8xxx_spi_remove(&pdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700765}
766
Anton Vorontsov575c5802009-06-18 16:49:08 -0700767MODULE_ALIAS("platform:mpc8xxx_spi");
768static struct platform_driver mpc8xxx_spi_driver = {
769 .probe = plat_mpc8xxx_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000770 .remove = plat_mpc8xxx_spi_remove,
Kumar Galaccf06992006-05-20 15:00:15 -0700771 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700772 .name = "mpc8xxx_spi",
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700773 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -0700774 },
775};
776
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700777static bool legacy_driver_failed;
778
779static void __init legacy_driver_register(void)
780{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700781 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700782}
783
784static void __exit legacy_driver_unregister(void)
785{
786 if (legacy_driver_failed)
787 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700788 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700789}
790#else
791static void __init legacy_driver_register(void) {}
792static void __exit legacy_driver_unregister(void) {}
793#endif /* CONFIG_MPC832x_RDB */
794
Mingkai Hub36ece82010-10-12 18:18:31 +0800795static int __init fsl_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700796{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700797 legacy_driver_register();
Grant Likely18d306d2011-02-22 21:02:43 -0700798 return platform_driver_register(&of_fsl_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -0700799}
Mingkai Hub36ece82010-10-12 18:18:31 +0800800module_init(fsl_spi_init);
Kumar Galaccf06992006-05-20 15:00:15 -0700801
Mingkai Hub36ece82010-10-12 18:18:31 +0800802static void __exit fsl_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700803{
Grant Likely18d306d2011-02-22 21:02:43 -0700804 platform_driver_unregister(&of_fsl_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700805 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -0700806}
Mingkai Hub36ece82010-10-12 18:18:31 +0800807module_exit(fsl_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -0700808
809MODULE_AUTHOR("Kumar Gala");
Mingkai Hub36ece82010-10-12 18:18:31 +0800810MODULE_DESCRIPTION("Simple Freescale SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -0700811MODULE_LICENSE("GPL");