blob: 97ab0a81338adf036bea9e706dbcbe552b6d607c [file] [log] [blame]
Kumar Galaccf06992006-05-20 15:00:15 -07001/*
Anton Vorontsov575c5802009-06-18 16:49:08 -07002 * MPC8xxx SPI controller driver.
Kumar Galaccf06992006-05-20 15:00:15 -07003 *
4 * Maintainer: Kumar Gala
5 *
6 * Copyright (C) 2006 Polycom, Inc.
7 *
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04008 * CPM SPI and QE buffer descriptors mode support:
9 * Copyright (c) 2009 MontaVista Software, Inc.
10 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
11 *
Kumar Galaccf06992006-05-20 15:00:15 -070012 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
20#include <linux/kernel.h>
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -070021#include <linux/bug.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070022#include <linux/errno.h>
23#include <linux/err.h>
Anton Vorontsov9effb952009-06-18 16:49:05 -070024#include <linux/io.h>
Kumar Galaccf06992006-05-20 15:00:15 -070025#include <linux/completion.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
28#include <linux/irq.h>
29#include <linux/device.h>
30#include <linux/spi/spi.h>
31#include <linux/spi/spi_bitbang.h>
32#include <linux/platform_device.h>
33#include <linux/fsl_devices.h>
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040034#include <linux/dma-mapping.h>
35#include <linux/mm.h>
36#include <linux/mutex.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070037#include <linux/of.h>
38#include <linux/of_platform.h>
39#include <linux/gpio.h>
40#include <linux/of_gpio.h>
41#include <linux/of_spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Kumar Galaccf06992006-05-20 15:00:15 -070043
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070044#include <sysdev/fsl_soc.h>
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040045#include <asm/cpm.h>
46#include <asm/qe.h>
Kumar Galaccf06992006-05-20 15:00:15 -070047#include <asm/irq.h>
Kumar Galaccf06992006-05-20 15:00:15 -070048
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040049/* CPM1 and CPM2 are mutually exclusive. */
50#ifdef CONFIG_CPM1
51#include <asm/cpm1.h>
52#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
53#else
54#include <asm/cpm2.h>
55#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
56#endif
57
Kumar Galaccf06992006-05-20 15:00:15 -070058/* SPI Controller registers */
Anton Vorontsov575c5802009-06-18 16:49:08 -070059struct mpc8xxx_spi_reg {
Kumar Galaccf06992006-05-20 15:00:15 -070060 u8 res1[0x20];
61 __be32 mode;
62 __be32 event;
63 __be32 mask;
64 __be32 command;
65 __be32 transmit;
66 __be32 receive;
67};
68
69/* SPI Controller mode register definitions */
Anton Vorontsov2a485d72007-07-31 00:38:45 -070070#define SPMODE_LOOP (1 << 30)
Kumar Galaccf06992006-05-20 15:00:15 -070071#define SPMODE_CI_INACTIVEHIGH (1 << 29)
72#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
73#define SPMODE_DIV16 (1 << 27)
74#define SPMODE_REV (1 << 26)
75#define SPMODE_MS (1 << 25)
76#define SPMODE_ENABLE (1 << 24)
77#define SPMODE_LEN(x) ((x) << 20)
78#define SPMODE_PM(x) ((x) << 16)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070079#define SPMODE_OP (1 << 14)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -070080#define SPMODE_CG(x) ((x) << 7)
Kumar Galaccf06992006-05-20 15:00:15 -070081
82/*
83 * Default for SPI Mode:
84 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
85 */
86#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
87 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
88
89/* SPIE register values */
90#define SPIE_NE 0x00000200 /* Not empty */
91#define SPIE_NF 0x00000100 /* Not full */
92
93/* SPIM register values */
94#define SPIM_NE 0x00000200 /* Not empty */
95#define SPIM_NF 0x00000100 /* Not full */
96
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040097#define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
98#define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
99
100/* SPCOM register values */
101#define SPCOM_STR (1 << 23) /* Start transmit */
102
103#define SPI_PRAM_SIZE 0x100
104#define SPI_MRBLR ((unsigned int)PAGE_SIZE)
105
Kumar Galaccf06992006-05-20 15:00:15 -0700106/* SPI Controller driver's private data. */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700107struct mpc8xxx_spi {
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400108 struct device *dev;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700109 struct mpc8xxx_spi_reg __iomem *base;
Kumar Galaccf06992006-05-20 15:00:15 -0700110
111 /* rx & tx bufs from the spi_transfer */
112 const void *tx;
113 void *rx;
114
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400115 int subblock;
116 struct spi_pram __iomem *pram;
117 struct cpm_buf_desc __iomem *tx_bd;
118 struct cpm_buf_desc __iomem *rx_bd;
119
120 struct spi_transfer *xfer_in_progress;
121
122 /* dma addresses for CPM transfers */
123 dma_addr_t tx_dma;
124 dma_addr_t rx_dma;
125 bool map_tx_dma;
126 bool map_rx_dma;
127
128 dma_addr_t dma_dummy_tx;
129 dma_addr_t dma_dummy_rx;
130
Kumar Galaccf06992006-05-20 15:00:15 -0700131 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700132 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
133 u32(*get_tx) (struct mpc8xxx_spi *);
Kumar Galaccf06992006-05-20 15:00:15 -0700134
135 unsigned int count;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700136 unsigned int irq;
Kumar Galaccf06992006-05-20 15:00:15 -0700137
138 unsigned nsecs; /* (clock cycle time)/2 */
139
Anton Vorontsove24a4d12007-08-10 13:01:01 -0700140 u32 spibrg; /* SPIBRG input clock */
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700141 u32 rx_shift; /* RX data reg shift when in qe mode */
142 u32 tx_shift; /* TX data reg shift when in qe mode */
143
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400144 unsigned int flags;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700145
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700146 struct workqueue_struct *workqueue;
147 struct work_struct work;
148
149 struct list_head queue;
150 spinlock_t lock;
151
152 struct completion done;
153};
154
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400155static void *mpc8xxx_dummy_rx;
156static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
157static int mpc8xxx_dummy_rx_refcnt;
158
Anton Vorontsov575c5802009-06-18 16:49:08 -0700159struct spi_mpc8xxx_cs {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700160 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700161 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
162 u32 (*get_tx) (struct mpc8xxx_spi *);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700163 u32 rx_shift; /* RX data reg shift when in qe mode */
164 u32 tx_shift; /* TX data reg shift when in qe mode */
165 u32 hw_mode; /* Holds HW mode register settings */
Kumar Galaccf06992006-05-20 15:00:15 -0700166};
167
Anton Vorontsov575c5802009-06-18 16:49:08 -0700168static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
Kumar Galaccf06992006-05-20 15:00:15 -0700169{
170 out_be32(reg, val);
171}
172
Anton Vorontsov575c5802009-06-18 16:49:08 -0700173static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
Kumar Galaccf06992006-05-20 15:00:15 -0700174{
175 return in_be32(reg);
176}
177
178#define MPC83XX_SPI_RX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700179static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700180void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700181{ \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700182 type *rx = mpc8xxx_spi->rx; \
183 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
184 mpc8xxx_spi->rx = rx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700185}
186
187#define MPC83XX_SPI_TX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700188static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700189u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700190{ \
191 u32 data; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700192 const type *tx = mpc8xxx_spi->tx; \
David Brownell4b1badf2006-12-29 16:48:39 -0800193 if (!tx) \
194 return 0; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700195 data = *tx++ << mpc8xxx_spi->tx_shift; \
196 mpc8xxx_spi->tx = tx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700197 return data; \
198}
199
200MPC83XX_SPI_RX_BUF(u8)
201MPC83XX_SPI_RX_BUF(u16)
202MPC83XX_SPI_RX_BUF(u32)
203MPC83XX_SPI_TX_BUF(u8)
204MPC83XX_SPI_TX_BUF(u16)
205MPC83XX_SPI_TX_BUF(u32)
206
Anton Vorontsova35c1712009-10-12 20:49:24 +0400207static void mpc8xxx_spi_change_mode(struct spi_device *spi)
208{
209 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
210 struct spi_mpc8xxx_cs *cs = spi->controller_state;
211 __be32 __iomem *mode = &mspi->base->mode;
212 unsigned long flags;
213
214 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
215 return;
216
217 /* Turn off IRQs locally to minimize time that SPI is disabled. */
218 local_irq_save(flags);
219
220 /* Turn off SPI unit prior changing mode */
221 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
Anton Vorontsova35c1712009-10-12 20:49:24 +0400222
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400223 /* When in CPM mode, we need to reinit tx and rx. */
224 if (mspi->flags & SPI_CPM_MODE) {
225 if (mspi->flags & SPI_QE) {
226 qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
227 QE_CR_PROTOCOL_UNSPECIFIED, 0);
228 } else {
229 cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
230 if (mspi->flags & SPI_CPM1) {
231 out_be16(&mspi->pram->rbptr,
232 in_be16(&mspi->pram->rbase));
233 out_be16(&mspi->pram->tbptr,
234 in_be16(&mspi->pram->tbase));
235 }
236 }
237 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -0600238 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
Anton Vorontsova35c1712009-10-12 20:49:24 +0400239 local_irq_restore(flags);
240}
241
Anton Vorontsov575c5802009-06-18 16:49:08 -0700242static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -0700243{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700244 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700245 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
246 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700247 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700248
Kumar Galaccf06992006-05-20 15:00:15 -0700249 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700250 if (pdata->cs_control)
251 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700252 }
253
254 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700255 mpc8xxx_spi->rx_shift = cs->rx_shift;
256 mpc8xxx_spi->tx_shift = cs->tx_shift;
257 mpc8xxx_spi->get_rx = cs->get_rx;
258 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700259
Anton Vorontsova35c1712009-10-12 20:49:24 +0400260 mpc8xxx_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700261
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700262 if (pdata->cs_control)
263 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700264 }
265}
266
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000267static int
268mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
269 struct spi_device *spi,
270 struct mpc8xxx_spi *mpc8xxx_spi,
271 int bits_per_word)
272{
273 cs->rx_shift = 0;
274 cs->tx_shift = 0;
275 if (bits_per_word <= 8) {
276 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
277 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
278 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
279 cs->rx_shift = 16;
280 cs->tx_shift = 24;
281 }
282 } else if (bits_per_word <= 16) {
283 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
284 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
285 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
286 cs->rx_shift = 16;
287 cs->tx_shift = 16;
288 }
289 } else if (bits_per_word <= 32) {
290 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
291 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
292 } else
293 return -EINVAL;
294
295 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
296 spi->mode & SPI_LSB_FIRST) {
297 cs->tx_shift = 0;
298 if (bits_per_word <= 8)
299 cs->rx_shift = 8;
300 else
301 cs->rx_shift = 0;
302 }
303 mpc8xxx_spi->rx_shift = cs->rx_shift;
304 mpc8xxx_spi->tx_shift = cs->tx_shift;
305 mpc8xxx_spi->get_rx = cs->get_rx;
306 mpc8xxx_spi->get_tx = cs->get_tx;
307
308 return bits_per_word;
309}
310
311static int
312mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
313 struct spi_device *spi,
314 int bits_per_word)
315{
316 /* QE uses Little Endian for words > 8
317 * so transform all words > 8 into 8 bits
318 * Unfortnatly that doesn't work for LSB so
319 * reject these for now */
320 /* Note: 32 bits word, LSB works iff
321 * tfcr/rfcr is set to CPMFCR_GBL */
322 if (spi->mode & SPI_LSB_FIRST &&
323 bits_per_word > 8)
324 return -EINVAL;
325 if (bits_per_word > 8)
326 return 8; /* pretend its 8 bits */
327 return bits_per_word;
328}
329
Kumar Galaccf06992006-05-20 15:00:15 -0700330static
Anton Vorontsov575c5802009-06-18 16:49:08 -0700331int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700332{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700333 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000334 int bits_per_word;
335 u8 pm;
Kumar Galaccf06992006-05-20 15:00:15 -0700336 u32 hz;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700337 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700338
Anton Vorontsov575c5802009-06-18 16:49:08 -0700339 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700340
341 if (t) {
342 bits_per_word = t->bits_per_word;
343 hz = t->speed_hz;
344 } else {
345 bits_per_word = 0;
346 hz = 0;
347 }
348
349 /* spi_transfer level calls that work per-word */
350 if (!bits_per_word)
351 bits_per_word = spi->bits_per_word;
352
353 /* Make sure its a bit width we support [4..16, 32] */
354 if ((bits_per_word < 4)
355 || ((bits_per_word > 16) && (bits_per_word != 32)))
356 return -EINVAL;
357
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700358 if (!hz)
359 hz = spi->max_speed_hz;
360
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000361 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
362 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
363 mpc8xxx_spi,
364 bits_per_word);
365 else if (mpc8xxx_spi->flags & SPI_QE)
366 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
367 bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700368
Joakim Tjernlund0398fb72010-05-14 09:14:26 +0000369 if (bits_per_word < 0)
370 return bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700371
372 if (bits_per_word == 32)
373 bits_per_word = 0;
374 else
375 bits_per_word = bits_per_word - 1;
376
Anton Vorontsov32421da2007-07-31 00:38:41 -0700377 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700378 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
379 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700380
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700381 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700382
Anton Vorontsov575c5802009-06-18 16:49:08 -0700383 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700384 cs->hw_mode |= SPMODE_DIV16;
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700385 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700386
387 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
388 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Anton Vorontsov575c5802009-06-18 16:49:08 -0700389 hz, mpc8xxx_spi->spibrg / 1024);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700390 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700391 pm = 16;
Chen Gonga61f5342008-07-23 21:29:52 -0700392 } else
Ernst Schwab4f4517c2010-02-16 21:02:57 -0700393 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
Chen Gonga61f5342008-07-23 21:29:52 -0700394 if (pm)
395 pm--;
396
397 cs->hw_mode |= SPMODE_PM(pm);
David Brownelldccd5732007-07-17 04:04:02 -0700398
Anton Vorontsova35c1712009-10-12 20:49:24 +0400399 mpc8xxx_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700400 return 0;
401}
402
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400403static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
Kumar Galaccf06992006-05-20 15:00:15 -0700404{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400405 struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
406 struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
407 unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
408 unsigned int xfer_ofs;
Kumar Galaccf06992006-05-20 15:00:15 -0700409
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400410 xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
Kumar Galaccf06992006-05-20 15:00:15 -0700411
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400412 out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
413 out_be16(&rx_bd->cbd_datlen, 0);
414 out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
415
416 out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
417 out_be16(&tx_bd->cbd_datlen, xfer_len);
418 out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
419 BD_SC_LAST);
420
421 /* start transfer */
422 mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
423}
424
425static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
426 struct spi_transfer *t, bool is_dma_mapped)
427{
428 struct device *dev = mspi->dev;
429
430 if (is_dma_mapped) {
431 mspi->map_tx_dma = 0;
432 mspi->map_rx_dma = 0;
433 } else {
434 mspi->map_tx_dma = 1;
435 mspi->map_rx_dma = 1;
436 }
437
438 if (!t->tx_buf) {
439 mspi->tx_dma = mspi->dma_dummy_tx;
440 mspi->map_tx_dma = 0;
441 }
442
443 if (!t->rx_buf) {
444 mspi->rx_dma = mspi->dma_dummy_rx;
445 mspi->map_rx_dma = 0;
446 }
447
448 if (mspi->map_tx_dma) {
449 void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
450
451 mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
452 DMA_TO_DEVICE);
453 if (dma_mapping_error(dev, mspi->tx_dma)) {
454 dev_err(dev, "unable to map tx dma\n");
455 return -ENOMEM;
456 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -0600457 } else if (t->tx_buf) {
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400458 mspi->tx_dma = t->tx_dma;
459 }
460
461 if (mspi->map_rx_dma) {
462 mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
463 DMA_FROM_DEVICE);
464 if (dma_mapping_error(dev, mspi->rx_dma)) {
465 dev_err(dev, "unable to map rx dma\n");
466 goto err_rx_dma;
467 }
Joakim Tjernlundf9218c22010-05-22 02:18:02 -0600468 } else if (t->rx_buf) {
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400469 mspi->rx_dma = t->rx_dma;
470 }
471
472 /* enable rx ints */
473 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
474
475 mspi->xfer_in_progress = t;
476 mspi->count = t->len;
477
478 /* start CPM transfers */
479 mpc8xxx_spi_cpm_bufs_start(mspi);
480
481 return 0;
482
483err_rx_dma:
484 if (mspi->map_tx_dma)
485 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
486 return -ENOMEM;
487}
488
489static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
490{
491 struct device *dev = mspi->dev;
492 struct spi_transfer *t = mspi->xfer_in_progress;
493
494 if (mspi->map_tx_dma)
495 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
Joakim Tjernlund338ff292010-05-17 13:17:10 +0000496 if (mspi->map_rx_dma)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400497 dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
498 mspi->xfer_in_progress = NULL;
499}
500
501static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
502 struct spi_transfer *t, unsigned int len)
503{
504 u32 word;
505
506 mspi->count = len;
507
508 /* enable rx ints */
509 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
510
511 /* transmit word */
512 word = mspi->get_tx(mspi);
513 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
514
515 return 0;
516}
517
518static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
519 bool is_dma_mapped)
520{
521 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
522 unsigned int len = t->len;
523 u8 bits_per_word;
524 int ret;
525
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700526 bits_per_word = spi->bits_per_word;
527 if (t->bits_per_word)
528 bits_per_word = t->bits_per_word;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400529
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700530 if (bits_per_word > 8) {
531 /* invalid length? */
532 if (len & 1)
533 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700534 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700535 }
536 if (bits_per_word > 16) {
537 /* invalid length? */
538 if (len & 1)
539 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700540 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700541 }
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400542
543 mpc8xxx_spi->tx = t->tx_buf;
544 mpc8xxx_spi->rx = t->rx_buf;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700545
Anton Vorontsov575c5802009-06-18 16:49:08 -0700546 INIT_COMPLETION(mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700547
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400548 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
549 ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
550 else
551 ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
552 if (ret)
553 return ret;
Kumar Galaccf06992006-05-20 15:00:15 -0700554
Anton Vorontsov575c5802009-06-18 16:49:08 -0700555 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700556
557 /* disable rx ints */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700558 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700559
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400560 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
561 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
562
Anton Vorontsov575c5802009-06-18 16:49:08 -0700563 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700564}
565
Anton Vorontsov575c5802009-06-18 16:49:08 -0700566static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700567{
568 struct spi_device *spi = m->spi;
569 struct spi_transfer *t;
570 unsigned int cs_change;
571 const int nsecs = 50;
572 int status;
573
574 cs_change = 1;
575 status = 0;
576 list_for_each_entry(t, &m->transfers, transfer_list) {
577 if (t->bits_per_word || t->speed_hz) {
578 /* Don't allow changes if CS is active */
579 status = -EINVAL;
580
581 if (cs_change)
Anton Vorontsov575c5802009-06-18 16:49:08 -0700582 status = mpc8xxx_spi_setup_transfer(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700583 if (status < 0)
584 break;
585 }
586
587 if (cs_change) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700588 mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700589 ndelay(nsecs);
590 }
591 cs_change = t->cs_change;
592 if (t->len)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400593 status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700594 if (status) {
595 status = -EMSGSIZE;
596 break;
597 }
598 m->actual_length += t->len;
599
600 if (t->delay_usecs)
601 udelay(t->delay_usecs);
602
603 if (cs_change) {
604 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700605 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700606 ndelay(nsecs);
607 }
608 }
609
610 m->status = status;
611 m->complete(m->context);
612
613 if (status || !cs_change) {
614 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700615 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700616 }
617
Anton Vorontsov575c5802009-06-18 16:49:08 -0700618 mpc8xxx_spi_setup_transfer(spi, NULL);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700619}
620
Anton Vorontsov575c5802009-06-18 16:49:08 -0700621static void mpc8xxx_spi_work(struct work_struct *work)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700622{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700623 struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700624 work);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700625
Anton Vorontsov575c5802009-06-18 16:49:08 -0700626 spin_lock_irq(&mpc8xxx_spi->lock);
627 while (!list_empty(&mpc8xxx_spi->queue)) {
628 struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700629 struct spi_message, queue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700630
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700631 list_del_init(&m->queue);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700632 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700633
Anton Vorontsov575c5802009-06-18 16:49:08 -0700634 mpc8xxx_spi_do_one_msg(m);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700635
Anton Vorontsov575c5802009-06-18 16:49:08 -0700636 spin_lock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700637 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700638 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700639}
640
Anton Vorontsov575c5802009-06-18 16:49:08 -0700641static int mpc8xxx_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700642{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700643 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700644 int retval;
645 u32 hw_mode;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700646 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700647
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700648 if (!spi->max_speed_hz)
649 return -EINVAL;
650
651 if (!cs) {
652 cs = kzalloc(sizeof *cs, GFP_KERNEL);
653 if (!cs)
654 return -ENOMEM;
655 spi->controller_state = cs;
656 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700657 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700658
Thomas Weber88393162010-03-16 11:47:56 +0100659 hw_mode = cs->hw_mode; /* Save original settings */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700660 cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700661 /* mask out bits we are going to set */
662 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
663 | SPMODE_REV | SPMODE_LOOP);
664
665 if (spi->mode & SPI_CPHA)
666 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
667 if (spi->mode & SPI_CPOL)
668 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
669 if (!(spi->mode & SPI_LSB_FIRST))
670 cs->hw_mode |= SPMODE_REV;
671 if (spi->mode & SPI_LOOP)
672 cs->hw_mode |= SPMODE_LOOP;
673
Anton Vorontsov575c5802009-06-18 16:49:08 -0700674 retval = mpc8xxx_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700675 if (retval < 0) {
676 cs->hw_mode = hw_mode; /* Restore settings */
677 return retval;
678 }
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700679 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700680}
681
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400682static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
Kumar Galaccf06992006-05-20 15:00:15 -0700683{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400684 u16 len;
Kumar Galaccf06992006-05-20 15:00:15 -0700685
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400686 dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
687 in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
Kumar Galaccf06992006-05-20 15:00:15 -0700688
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400689 len = in_be16(&mspi->rx_bd->cbd_datlen);
690 if (len > mspi->count) {
691 WARN_ON(1);
692 len = mspi->count;
Kumar Galaccf06992006-05-20 15:00:15 -0700693 }
694
695 /* Clear the events */
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400696 mpc8xxx_spi_write_reg(&mspi->base->event, events);
697
698 mspi->count -= len;
699 if (mspi->count)
700 mpc8xxx_spi_cpm_bufs_start(mspi);
701 else
702 complete(&mspi->done);
703}
704
705static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
706{
707 /* We need handle RX first */
708 if (events & SPIE_NE) {
709 u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
710
711 if (mspi->rx)
712 mspi->get_rx(rx_data, mspi);
713 }
714
715 if ((events & SPIE_NF) == 0)
716 /* spin until TX is done */
717 while (((events =
718 mpc8xxx_spi_read_reg(&mspi->base->event)) &
719 SPIE_NF) == 0)
720 cpu_relax();
721
722 /* Clear the events */
723 mpc8xxx_spi_write_reg(&mspi->base->event, events);
724
725 mspi->count -= 1;
726 if (mspi->count) {
727 u32 word = mspi->get_tx(mspi);
728
729 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
730 } else {
731 complete(&mspi->done);
732 }
733}
734
735static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
736{
737 struct mpc8xxx_spi *mspi = context_data;
738 irqreturn_t ret = IRQ_NONE;
739 u32 events;
740
741 /* Get interrupt events(tx/rx) */
742 events = mpc8xxx_spi_read_reg(&mspi->base->event);
743 if (events)
744 ret = IRQ_HANDLED;
745
746 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
747
748 if (mspi->flags & SPI_CPM_MODE)
749 mpc8xxx_spi_cpm_irq(mspi, events);
750 else
751 mpc8xxx_spi_cpu_irq(mspi, events);
Kumar Galaccf06992006-05-20 15:00:15 -0700752
753 return ret;
754}
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400755
Anton Vorontsov575c5802009-06-18 16:49:08 -0700756static int mpc8xxx_spi_transfer(struct spi_device *spi,
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700757 struct spi_message *m)
758{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700759 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700760 unsigned long flags;
761
762 m->actual_length = 0;
763 m->status = -EINPROGRESS;
764
Anton Vorontsov575c5802009-06-18 16:49:08 -0700765 spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
766 list_add_tail(&m->queue, &mpc8xxx_spi->queue);
767 queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
768 spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700769
770 return 0;
771}
772
773
Anton Vorontsov575c5802009-06-18 16:49:08 -0700774static void mpc8xxx_spi_cleanup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700775{
776 kfree(spi->controller_state);
777}
Kumar Galaccf06992006-05-20 15:00:15 -0700778
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400779static void *mpc8xxx_spi_alloc_dummy_rx(void)
780{
781 mutex_lock(&mpc8xxx_dummy_rx_lock);
782
783 if (!mpc8xxx_dummy_rx)
784 mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
785 if (mpc8xxx_dummy_rx)
786 mpc8xxx_dummy_rx_refcnt++;
787
788 mutex_unlock(&mpc8xxx_dummy_rx_lock);
789
790 return mpc8xxx_dummy_rx;
791}
792
793static void mpc8xxx_spi_free_dummy_rx(void)
794{
795 mutex_lock(&mpc8xxx_dummy_rx_lock);
796
797 switch (mpc8xxx_dummy_rx_refcnt) {
798 case 0:
799 WARN_ON(1);
800 break;
801 case 1:
802 kfree(mpc8xxx_dummy_rx);
803 mpc8xxx_dummy_rx = NULL;
804 /* fall through */
805 default:
806 mpc8xxx_dummy_rx_refcnt--;
807 break;
808 }
809
810 mutex_unlock(&mpc8xxx_dummy_rx_lock);
811}
812
813static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
814{
815 struct device *dev = mspi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700816 struct device_node *np = dev->of_node;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400817 const u32 *iprop;
818 int size;
819 unsigned long spi_base_ofs;
820 unsigned long pram_ofs = -ENOMEM;
821
822 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
823 iprop = of_get_property(np, "reg", &size);
824
825 /* QE with a fixed pram location? */
826 if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
827 return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
828
829 /* QE but with a dynamic pram location? */
830 if (mspi->flags & SPI_QE) {
831 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
832 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
833 QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
834 return pram_ofs;
835 }
836
837 /* CPM1 and CPM2 pram must be at a fixed addr. */
838 if (!iprop || size != sizeof(*iprop) * 4)
839 return -ENOMEM;
840
841 spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
842 if (IS_ERR_VALUE(spi_base_ofs))
843 return -ENOMEM;
844
845 if (mspi->flags & SPI_CPM2) {
846 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
847 if (!IS_ERR_VALUE(pram_ofs)) {
848 u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
849
850 out_be16(spi_base, pram_ofs);
851 }
852 } else {
853 struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
854 u16 rpbase = in_be16(&pram->rpbase);
855
856 /* Microcode relocation patch applied? */
857 if (rpbase)
858 pram_ofs = rpbase;
859 else
860 return spi_base_ofs;
861 }
862
863 cpm_muram_free(spi_base_ofs);
864 return pram_ofs;
865}
866
867static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
868{
869 struct device *dev = mspi->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700870 struct device_node *np = dev->of_node;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400871 const u32 *iprop;
872 int size;
873 unsigned long pram_ofs;
874 unsigned long bds_ofs;
875
876 if (!(mspi->flags & SPI_CPM_MODE))
877 return 0;
878
879 if (!mpc8xxx_spi_alloc_dummy_rx())
880 return -ENOMEM;
881
882 if (mspi->flags & SPI_QE) {
883 iprop = of_get_property(np, "cell-index", &size);
884 if (iprop && size == sizeof(*iprop))
885 mspi->subblock = *iprop;
886
887 switch (mspi->subblock) {
888 default:
889 dev_warn(dev, "cell-index unspecified, assuming SPI1");
890 /* fall through */
891 case 0:
892 mspi->subblock = QE_CR_SUBBLOCK_SPI1;
893 break;
894 case 1:
895 mspi->subblock = QE_CR_SUBBLOCK_SPI2;
896 break;
897 }
898 }
899
900 pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
901 if (IS_ERR_VALUE(pram_ofs)) {
902 dev_err(dev, "can't allocate spi parameter ram\n");
903 goto err_pram;
904 }
905
906 bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
907 sizeof(*mspi->rx_bd), 8);
908 if (IS_ERR_VALUE(bds_ofs)) {
909 dev_err(dev, "can't allocate bds\n");
910 goto err_bds;
911 }
912
913 mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
914 DMA_TO_DEVICE);
915 if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
916 dev_err(dev, "unable to map dummy tx buffer\n");
917 goto err_dummy_tx;
918 }
919
920 mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
921 DMA_FROM_DEVICE);
922 if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
923 dev_err(dev, "unable to map dummy rx buffer\n");
924 goto err_dummy_rx;
925 }
926
927 mspi->pram = cpm_muram_addr(pram_ofs);
928
929 mspi->tx_bd = cpm_muram_addr(bds_ofs);
930 mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
931
932 /* Initialize parameter ram. */
933 out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
934 out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
935 out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
936 out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
937 out_be16(&mspi->pram->mrblr, SPI_MRBLR);
938 out_be32(&mspi->pram->rstate, 0);
939 out_be32(&mspi->pram->rdp, 0);
940 out_be16(&mspi->pram->rbptr, 0);
941 out_be16(&mspi->pram->rbc, 0);
942 out_be32(&mspi->pram->rxtmp, 0);
943 out_be32(&mspi->pram->tstate, 0);
944 out_be32(&mspi->pram->tdp, 0);
945 out_be16(&mspi->pram->tbptr, 0);
946 out_be16(&mspi->pram->tbc, 0);
947 out_be32(&mspi->pram->txtmp, 0);
948
949 return 0;
950
951err_dummy_rx:
952 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
953err_dummy_tx:
954 cpm_muram_free(bds_ofs);
955err_bds:
956 cpm_muram_free(pram_ofs);
957err_pram:
958 mpc8xxx_spi_free_dummy_rx();
959 return -ENOMEM;
960}
961
962static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
963{
964 struct device *dev = mspi->dev;
965
966 dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
967 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
968 cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
969 cpm_muram_free(cpm_muram_offset(mspi->pram));
970 mpc8xxx_spi_free_dummy_rx();
971}
972
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400973static const char *mpc8xxx_spi_strmode(unsigned int flags)
974{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400975 if (flags & SPI_QE_CPU_MODE) {
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400976 return "QE CPU";
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400977 } else if (flags & SPI_CPM_MODE) {
978 if (flags & SPI_QE)
979 return "QE";
980 else if (flags & SPI_CPM2)
981 return "CPM2";
982 else
983 return "CPM1";
984 }
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400985 return "CPU";
986}
987
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700988static struct spi_master * __devinit
Anton Vorontsov575c5802009-06-18 16:49:08 -0700989mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700990{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700991 struct fsl_spi_platform_data *pdata = dev->platform_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700992 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700993 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -0700994 u32 regval;
995 int ret = 0;
996
Anton Vorontsov575c5802009-06-18 16:49:08 -0700997 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700998 if (master == NULL) {
999 ret = -ENOMEM;
1000 goto err;
1001 }
1002
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001003 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -07001004
David Brownelle7db06b2009-06-17 16:26:04 -07001005 /* the spi->mode bits understood by this driver: */
1006 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
1007 | SPI_LSB_FIRST | SPI_LOOP;
1008
Anton Vorontsov575c5802009-06-18 16:49:08 -07001009 master->setup = mpc8xxx_spi_setup;
1010 master->transfer = mpc8xxx_spi_transfer;
1011 master->cleanup = mpc8xxx_spi_cleanup;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001012
Anton Vorontsov575c5802009-06-18 16:49:08 -07001013 mpc8xxx_spi = spi_master_get_devdata(master);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001014 mpc8xxx_spi->dev = dev;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001015 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
1016 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001017 mpc8xxx_spi->flags = pdata->flags;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001018 mpc8xxx_spi->spibrg = pdata->sysclk;
Anton Vorontsove24a4d12007-08-10 13:01:01 -07001019
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001020 ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
1021 if (ret)
1022 goto err_cpm_init;
1023
Anton Vorontsov575c5802009-06-18 16:49:08 -07001024 mpc8xxx_spi->rx_shift = 0;
1025 mpc8xxx_spi->tx_shift = 0;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001026 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -07001027 mpc8xxx_spi->rx_shift = 16;
1028 mpc8xxx_spi->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -07001029 }
1030
Anton Vorontsov575c5802009-06-18 16:49:08 -07001031 init_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -07001032
hartleys82de7652009-12-14 22:37:15 +00001033 mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
Anton Vorontsov575c5802009-06-18 16:49:08 -07001034 if (mpc8xxx_spi->base == NULL) {
Kumar Galaccf06992006-05-20 15:00:15 -07001035 ret = -ENOMEM;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001036 goto err_ioremap;
Kumar Galaccf06992006-05-20 15:00:15 -07001037 }
1038
Anton Vorontsov575c5802009-06-18 16:49:08 -07001039 mpc8xxx_spi->irq = irq;
Kumar Galaccf06992006-05-20 15:00:15 -07001040
1041 /* Register for SPI Interrupt */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001042 ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
1043 0, "mpc8xxx_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001044
1045 if (ret != 0)
1046 goto unmap_io;
1047
1048 master->bus_num = pdata->bus_num;
1049 master->num_chipselect = pdata->max_chipselect;
1050
1051 /* SPI controller initializations */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001052 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
1053 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
1054 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
1055 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -07001056
1057 /* Enable SPI interface */
1058 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001059 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -07001060 regval |= SPMODE_OP;
1061
Anton Vorontsov575c5802009-06-18 16:49:08 -07001062 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
1063 spin_lock_init(&mpc8xxx_spi->lock);
1064 init_completion(&mpc8xxx_spi->done);
1065 INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
1066 INIT_LIST_HEAD(&mpc8xxx_spi->queue);
Kumar Galaccf06992006-05-20 15:00:15 -07001067
Anton Vorontsov575c5802009-06-18 16:49:08 -07001068 mpc8xxx_spi->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001069 dev_name(master->dev.parent));
Anton Vorontsov575c5802009-06-18 16:49:08 -07001070 if (mpc8xxx_spi->workqueue == NULL) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001071 ret = -EBUSY;
Kumar Galaccf06992006-05-20 15:00:15 -07001072 goto free_irq;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001073 }
1074
1075 ret = spi_register_master(master);
1076 if (ret < 0)
1077 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -07001078
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001079 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
1080 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
Kumar Galaccf06992006-05-20 15:00:15 -07001081
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001082 return master;
Kumar Galaccf06992006-05-20 15:00:15 -07001083
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001084unreg_master:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001085 destroy_workqueue(mpc8xxx_spi->workqueue);
Kumar Galaccf06992006-05-20 15:00:15 -07001086free_irq:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001087 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001088unmap_io:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001089 iounmap(mpc8xxx_spi->base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001090err_ioremap:
1091 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1092err_cpm_init:
Kumar Galaccf06992006-05-20 15:00:15 -07001093 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -07001094err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001095 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -07001096}
1097
Anton Vorontsov575c5802009-06-18 16:49:08 -07001098static int __devexit mpc8xxx_spi_remove(struct device *dev)
Kumar Galaccf06992006-05-20 15:00:15 -07001099{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001100 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -07001101 struct spi_master *master;
1102
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001103 master = dev_get_drvdata(dev);
Anton Vorontsov575c5802009-06-18 16:49:08 -07001104 mpc8xxx_spi = spi_master_get_devdata(master);
Kumar Galaccf06992006-05-20 15:00:15 -07001105
Anton Vorontsov575c5802009-06-18 16:49:08 -07001106 flush_workqueue(mpc8xxx_spi->workqueue);
1107 destroy_workqueue(mpc8xxx_spi->workqueue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001108 spi_unregister_master(master);
1109
Anton Vorontsov575c5802009-06-18 16:49:08 -07001110 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
1111 iounmap(mpc8xxx_spi->base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001112 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001113
1114 return 0;
1115}
1116
Anton Vorontsov575c5802009-06-18 16:49:08 -07001117struct mpc8xxx_spi_probe_info {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001118 struct fsl_spi_platform_data pdata;
1119 int *gpios;
1120 bool *alow_flags;
1121};
1122
Anton Vorontsov575c5802009-06-18 16:49:08 -07001123static struct mpc8xxx_spi_probe_info *
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001124to_of_pinfo(struct fsl_spi_platform_data *pdata)
1125{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001126 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001127}
1128
Anton Vorontsov575c5802009-06-18 16:49:08 -07001129static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001130{
1131 struct device *dev = spi->dev.parent;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001132 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001133 u16 cs = spi->chip_select;
1134 int gpio = pinfo->gpios[cs];
1135 bool alow = pinfo->alow_flags[cs];
1136
1137 gpio_set_value(gpio, on ^ alow);
1138}
1139
Anton Vorontsov575c5802009-06-18 16:49:08 -07001140static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001141{
Grant Likely61c7a082010-04-13 16:12:29 -07001142 struct device_node *np = dev->of_node;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001143 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001144 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001145 unsigned int ngpios;
1146 int i = 0;
1147 int ret;
1148
1149 ngpios = of_gpio_count(np);
1150 if (!ngpios) {
1151 /*
1152 * SPI w/o chip-select line. One SPI device is still permitted
1153 * though.
1154 */
1155 pdata->max_chipselect = 1;
1156 return 0;
1157 }
1158
Roel Kluin02141542009-06-16 15:31:15 -07001159 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001160 if (!pinfo->gpios)
1161 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -07001162 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001163
Roel Kluin02141542009-06-16 15:31:15 -07001164 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001165 GFP_KERNEL);
1166 if (!pinfo->alow_flags) {
1167 ret = -ENOMEM;
1168 goto err_alloc_flags;
1169 }
1170
1171 for (; i < ngpios; i++) {
1172 int gpio;
1173 enum of_gpio_flags flags;
1174
1175 gpio = of_get_gpio_flags(np, i, &flags);
1176 if (!gpio_is_valid(gpio)) {
1177 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
Anton Vorontsov783058f2009-10-12 20:49:22 +04001178 ret = gpio;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001179 goto err_loop;
1180 }
1181
1182 ret = gpio_request(gpio, dev_name(dev));
1183 if (ret) {
1184 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
1185 goto err_loop;
1186 }
1187
1188 pinfo->gpios[i] = gpio;
1189 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
1190
1191 ret = gpio_direction_output(pinfo->gpios[i],
1192 pinfo->alow_flags[i]);
1193 if (ret) {
1194 dev_err(dev, "can't set output direction for gpio "
1195 "#%d: %d\n", i, ret);
1196 goto err_loop;
1197 }
1198 }
1199
1200 pdata->max_chipselect = ngpios;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001201 pdata->cs_control = mpc8xxx_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001202
1203 return 0;
1204
1205err_loop:
1206 while (i >= 0) {
1207 if (gpio_is_valid(pinfo->gpios[i]))
1208 gpio_free(pinfo->gpios[i]);
1209 i--;
1210 }
1211
1212 kfree(pinfo->alow_flags);
1213 pinfo->alow_flags = NULL;
1214err_alloc_flags:
1215 kfree(pinfo->gpios);
1216 pinfo->gpios = NULL;
1217 return ret;
1218}
1219
Anton Vorontsov575c5802009-06-18 16:49:08 -07001220static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001221{
1222 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001223 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001224 int i;
1225
1226 if (!pinfo->gpios)
1227 return 0;
1228
1229 for (i = 0; i < pdata->max_chipselect; i++) {
1230 if (gpio_is_valid(pinfo->gpios[i]))
1231 gpio_free(pinfo->gpios[i]);
1232 }
1233
1234 kfree(pinfo->gpios);
1235 kfree(pinfo->alow_flags);
1236 return 0;
1237}
1238
Anton Vorontsov575c5802009-06-18 16:49:08 -07001239static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001240 const struct of_device_id *ofid)
1241{
1242 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -07001243 struct device_node *np = ofdev->dev.of_node;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001244 struct mpc8xxx_spi_probe_info *pinfo;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001245 struct fsl_spi_platform_data *pdata;
1246 struct spi_master *master;
1247 struct resource mem;
1248 struct resource irq;
1249 const void *prop;
1250 int ret = -ENOMEM;
1251
1252 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
1253 if (!pinfo)
1254 return -ENOMEM;
1255
1256 pdata = &pinfo->pdata;
1257 dev->platform_data = pdata;
1258
1259 /* Allocate bus num dynamically. */
1260 pdata->bus_num = -1;
1261
1262 /* SPI controller is either clocked from QE or SoC clock. */
1263 pdata->sysclk = get_brgfreq();
1264 if (pdata->sysclk == -1) {
1265 pdata->sysclk = fsl_get_sys_freq();
1266 if (pdata->sysclk == -1) {
1267 ret = -ENODEV;
1268 goto err_clk;
1269 }
1270 }
1271
1272 prop = of_get_property(np, "mode", NULL);
1273 if (prop && !strcmp(prop, "cpu-qe"))
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001274 pdata->flags = SPI_QE_CPU_MODE;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001275 else if (prop && !strcmp(prop, "qe"))
1276 pdata->flags = SPI_CPM_MODE | SPI_QE;
1277 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
1278 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
1279 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
1280 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001281
Anton Vorontsov575c5802009-06-18 16:49:08 -07001282 ret = of_mpc8xxx_spi_get_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001283 if (ret)
1284 goto err;
1285
1286 ret = of_address_to_resource(np, 0, &mem);
1287 if (ret)
1288 goto err;
1289
1290 ret = of_irq_to_resource(np, 0, &irq);
1291 if (!ret) {
1292 ret = -EINVAL;
1293 goto err;
1294 }
1295
Anton Vorontsov575c5802009-06-18 16:49:08 -07001296 master = mpc8xxx_spi_probe(dev, &mem, irq.start);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001297 if (IS_ERR(master)) {
1298 ret = PTR_ERR(master);
1299 goto err;
1300 }
1301
1302 of_register_spi_devices(master, np);
1303
1304 return 0;
1305
1306err:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001307 of_mpc8xxx_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001308err_clk:
1309 kfree(pinfo);
1310 return ret;
1311}
1312
Anton Vorontsov575c5802009-06-18 16:49:08 -07001313static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001314{
1315 int ret;
1316
Anton Vorontsov575c5802009-06-18 16:49:08 -07001317 ret = mpc8xxx_spi_remove(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001318 if (ret)
1319 return ret;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001320 of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001321 return 0;
1322}
1323
Anton Vorontsov575c5802009-06-18 16:49:08 -07001324static const struct of_device_id of_mpc8xxx_spi_match[] = {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001325 { .compatible = "fsl,spi" },
1326 {},
1327};
Anton Vorontsov575c5802009-06-18 16:49:08 -07001328MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001329
Anton Vorontsov575c5802009-06-18 16:49:08 -07001330static struct of_platform_driver of_mpc8xxx_spi_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001331 .driver = {
1332 .name = "mpc8xxx_spi",
1333 .owner = THIS_MODULE,
1334 .of_match_table = of_mpc8xxx_spi_match,
1335 },
Anton Vorontsov575c5802009-06-18 16:49:08 -07001336 .probe = of_mpc8xxx_spi_probe,
1337 .remove = __devexit_p(of_mpc8xxx_spi_remove),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001338};
1339
1340#ifdef CONFIG_MPC832x_RDB
1341/*
1342 * XXX XXX XXX
1343 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
1344 * only. The driver should go away soon, since newer MPC8323E-RDB's device
1345 * tree can work with OpenFirmware driver. But for now we support old trees
1346 * as well.
1347 */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001348static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001349{
1350 struct resource *mem;
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -07001351 int irq;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001352 struct spi_master *master;
1353
1354 if (!pdev->dev.platform_data)
1355 return -EINVAL;
1356
1357 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1358 if (!mem)
1359 return -EINVAL;
1360
1361 irq = platform_get_irq(pdev, 0);
Uwe Kleine-Könige9a172f2010-01-20 13:49:44 -07001362 if (irq <= 0)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001363 return -EINVAL;
1364
Anton Vorontsov575c5802009-06-18 16:49:08 -07001365 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001366 if (IS_ERR(master))
1367 return PTR_ERR(master);
1368 return 0;
1369}
1370
Anton Vorontsov575c5802009-06-18 16:49:08 -07001371static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001372{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001373 return mpc8xxx_spi_remove(&pdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001374}
1375
Anton Vorontsov575c5802009-06-18 16:49:08 -07001376MODULE_ALIAS("platform:mpc8xxx_spi");
1377static struct platform_driver mpc8xxx_spi_driver = {
1378 .probe = plat_mpc8xxx_spi_probe,
Uwe Kleine-Königb3a08942009-11-24 21:07:27 +00001379 .remove = __devexit_p(plat_mpc8xxx_spi_remove),
Kumar Galaccf06992006-05-20 15:00:15 -07001380 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -07001381 .name = "mpc8xxx_spi",
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001382 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -07001383 },
1384};
1385
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001386static bool legacy_driver_failed;
1387
1388static void __init legacy_driver_register(void)
1389{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001390 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001391}
1392
1393static void __exit legacy_driver_unregister(void)
1394{
1395 if (legacy_driver_failed)
1396 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001397 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001398}
1399#else
1400static void __init legacy_driver_register(void) {}
1401static void __exit legacy_driver_unregister(void) {}
1402#endif /* CONFIG_MPC832x_RDB */
1403
Anton Vorontsov575c5802009-06-18 16:49:08 -07001404static int __init mpc8xxx_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -07001405{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001406 legacy_driver_register();
Anton Vorontsov575c5802009-06-18 16:49:08 -07001407 return of_register_platform_driver(&of_mpc8xxx_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -07001408}
1409
Anton Vorontsov575c5802009-06-18 16:49:08 -07001410static void __exit mpc8xxx_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -07001411{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001412 of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001413 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -07001414}
1415
Anton Vorontsov575c5802009-06-18 16:49:08 -07001416module_init(mpc8xxx_spi_init);
1417module_exit(mpc8xxx_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -07001418
1419MODULE_AUTHOR("Kumar Gala");
Anton Vorontsov575c5802009-06-18 16:49:08 -07001420MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -07001421MODULE_LICENSE("GPL");