blob: 394b6581e17fd95c821c71d8522e1b8de3a36998 [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>
Kumar Galaccf06992006-05-20 15:00:15 -070042
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070043#include <sysdev/fsl_soc.h>
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040044#include <asm/cpm.h>
45#include <asm/qe.h>
Kumar Galaccf06992006-05-20 15:00:15 -070046#include <asm/irq.h>
Kumar Galaccf06992006-05-20 15:00:15 -070047
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040048/* CPM1 and CPM2 are mutually exclusive. */
49#ifdef CONFIG_CPM1
50#include <asm/cpm1.h>
51#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
52#else
53#include <asm/cpm2.h>
54#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
55#endif
56
Kumar Galaccf06992006-05-20 15:00:15 -070057/* SPI Controller registers */
Anton Vorontsov575c5802009-06-18 16:49:08 -070058struct mpc8xxx_spi_reg {
Kumar Galaccf06992006-05-20 15:00:15 -070059 u8 res1[0x20];
60 __be32 mode;
61 __be32 event;
62 __be32 mask;
63 __be32 command;
64 __be32 transmit;
65 __be32 receive;
66};
67
Anton Vorontsov4c1fba42009-10-12 20:49:27 +040068/* SPI Parameter RAM */
69struct spi_pram {
70 __be16 rbase; /* Rx Buffer descriptor base address */
71 __be16 tbase; /* Tx Buffer descriptor base address */
72 u8 rfcr; /* Rx function code */
73 u8 tfcr; /* Tx function code */
74 __be16 mrblr; /* Max receive buffer length */
75 __be32 rstate; /* Internal */
76 __be32 rdp; /* Internal */
77 __be16 rbptr; /* Internal */
78 __be16 rbc; /* Internal */
79 __be32 rxtmp; /* Internal */
80 __be32 tstate; /* Internal */
81 __be32 tdp; /* Internal */
82 __be16 tbptr; /* Internal */
83 __be16 tbc; /* Internal */
84 __be32 txtmp; /* Internal */
85 __be32 res; /* Tx temp. */
86 __be16 rpbase; /* Relocation pointer (CPM1 only) */
87 __be16 res1; /* Reserved */
88};
89
Kumar Galaccf06992006-05-20 15:00:15 -070090/* SPI Controller mode register definitions */
Anton Vorontsov2a485d72007-07-31 00:38:45 -070091#define SPMODE_LOOP (1 << 30)
Kumar Galaccf06992006-05-20 15:00:15 -070092#define SPMODE_CI_INACTIVEHIGH (1 << 29)
93#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
94#define SPMODE_DIV16 (1 << 27)
95#define SPMODE_REV (1 << 26)
96#define SPMODE_MS (1 << 25)
97#define SPMODE_ENABLE (1 << 24)
98#define SPMODE_LEN(x) ((x) << 20)
99#define SPMODE_PM(x) ((x) << 16)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700100#define SPMODE_OP (1 << 14)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700101#define SPMODE_CG(x) ((x) << 7)
Kumar Galaccf06992006-05-20 15:00:15 -0700102
103/*
104 * Default for SPI Mode:
105 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
106 */
107#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
108 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
109
110/* SPIE register values */
111#define SPIE_NE 0x00000200 /* Not empty */
112#define SPIE_NF 0x00000100 /* Not full */
113
114/* SPIM register values */
115#define SPIM_NE 0x00000200 /* Not empty */
116#define SPIM_NF 0x00000100 /* Not full */
117
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400118#define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
119#define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
120
121/* SPCOM register values */
122#define SPCOM_STR (1 << 23) /* Start transmit */
123
124#define SPI_PRAM_SIZE 0x100
125#define SPI_MRBLR ((unsigned int)PAGE_SIZE)
126
Kumar Galaccf06992006-05-20 15:00:15 -0700127/* SPI Controller driver's private data. */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700128struct mpc8xxx_spi {
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400129 struct device *dev;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700130 struct mpc8xxx_spi_reg __iomem *base;
Kumar Galaccf06992006-05-20 15:00:15 -0700131
132 /* rx & tx bufs from the spi_transfer */
133 const void *tx;
134 void *rx;
135
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400136 int subblock;
137 struct spi_pram __iomem *pram;
138 struct cpm_buf_desc __iomem *tx_bd;
139 struct cpm_buf_desc __iomem *rx_bd;
140
141 struct spi_transfer *xfer_in_progress;
142
143 /* dma addresses for CPM transfers */
144 dma_addr_t tx_dma;
145 dma_addr_t rx_dma;
146 bool map_tx_dma;
147 bool map_rx_dma;
148
149 dma_addr_t dma_dummy_tx;
150 dma_addr_t dma_dummy_rx;
151
Kumar Galaccf06992006-05-20 15:00:15 -0700152 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700153 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
154 u32(*get_tx) (struct mpc8xxx_spi *);
Kumar Galaccf06992006-05-20 15:00:15 -0700155
156 unsigned int count;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700157 unsigned int irq;
Kumar Galaccf06992006-05-20 15:00:15 -0700158
159 unsigned nsecs; /* (clock cycle time)/2 */
160
Anton Vorontsove24a4d12007-08-10 13:01:01 -0700161 u32 spibrg; /* SPIBRG input clock */
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700162 u32 rx_shift; /* RX data reg shift when in qe mode */
163 u32 tx_shift; /* TX data reg shift when in qe mode */
164
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400165 unsigned int flags;
166#define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400167#define SPI_CPM_MODE (1 << 1) /* CPM/QE ("DMA") mode */
168#define SPI_CPM1 (1 << 2) /* SPI unit is in CPM1 block */
169#define SPI_CPM2 (1 << 3) /* SPI unit is in CPM2 block */
170#define SPI_QE (1 << 4) /* SPI unit is in QE block */
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700171
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700172 struct workqueue_struct *workqueue;
173 struct work_struct work;
174
175 struct list_head queue;
176 spinlock_t lock;
177
178 struct completion done;
179};
180
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400181static void *mpc8xxx_dummy_rx;
182static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
183static int mpc8xxx_dummy_rx_refcnt;
184
Anton Vorontsov575c5802009-06-18 16:49:08 -0700185struct spi_mpc8xxx_cs {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700186 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700187 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
188 u32 (*get_tx) (struct mpc8xxx_spi *);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700189 u32 rx_shift; /* RX data reg shift when in qe mode */
190 u32 tx_shift; /* TX data reg shift when in qe mode */
191 u32 hw_mode; /* Holds HW mode register settings */
Kumar Galaccf06992006-05-20 15:00:15 -0700192};
193
Anton Vorontsov575c5802009-06-18 16:49:08 -0700194static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
Kumar Galaccf06992006-05-20 15:00:15 -0700195{
196 out_be32(reg, val);
197}
198
Anton Vorontsov575c5802009-06-18 16:49:08 -0700199static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
Kumar Galaccf06992006-05-20 15:00:15 -0700200{
201 return in_be32(reg);
202}
203
204#define MPC83XX_SPI_RX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700205static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700206void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700207{ \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700208 type *rx = mpc8xxx_spi->rx; \
209 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
210 mpc8xxx_spi->rx = rx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700211}
212
213#define MPC83XX_SPI_TX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700214static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700215u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700216{ \
217 u32 data; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700218 const type *tx = mpc8xxx_spi->tx; \
David Brownell4b1badf2006-12-29 16:48:39 -0800219 if (!tx) \
220 return 0; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700221 data = *tx++ << mpc8xxx_spi->tx_shift; \
222 mpc8xxx_spi->tx = tx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700223 return data; \
224}
225
226MPC83XX_SPI_RX_BUF(u8)
227MPC83XX_SPI_RX_BUF(u16)
228MPC83XX_SPI_RX_BUF(u32)
229MPC83XX_SPI_TX_BUF(u8)
230MPC83XX_SPI_TX_BUF(u16)
231MPC83XX_SPI_TX_BUF(u32)
232
Anton Vorontsova35c1712009-10-12 20:49:24 +0400233static void mpc8xxx_spi_change_mode(struct spi_device *spi)
234{
235 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
236 struct spi_mpc8xxx_cs *cs = spi->controller_state;
237 __be32 __iomem *mode = &mspi->base->mode;
238 unsigned long flags;
239
240 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
241 return;
242
243 /* Turn off IRQs locally to minimize time that SPI is disabled. */
244 local_irq_save(flags);
245
246 /* Turn off SPI unit prior changing mode */
247 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
248 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
249
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400250 /* When in CPM mode, we need to reinit tx and rx. */
251 if (mspi->flags & SPI_CPM_MODE) {
252 if (mspi->flags & SPI_QE) {
253 qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
254 QE_CR_PROTOCOL_UNSPECIFIED, 0);
255 } else {
256 cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
257 if (mspi->flags & SPI_CPM1) {
258 out_be16(&mspi->pram->rbptr,
259 in_be16(&mspi->pram->rbase));
260 out_be16(&mspi->pram->tbptr,
261 in_be16(&mspi->pram->tbase));
262 }
263 }
264 }
265
Anton Vorontsova35c1712009-10-12 20:49:24 +0400266 local_irq_restore(flags);
267}
268
Anton Vorontsov575c5802009-06-18 16:49:08 -0700269static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -0700270{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700271 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700272 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
273 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700274 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700275
Kumar Galaccf06992006-05-20 15:00:15 -0700276 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700277 if (pdata->cs_control)
278 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700279 }
280
281 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700282 mpc8xxx_spi->rx_shift = cs->rx_shift;
283 mpc8xxx_spi->tx_shift = cs->tx_shift;
284 mpc8xxx_spi->get_rx = cs->get_rx;
285 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700286
Anton Vorontsova35c1712009-10-12 20:49:24 +0400287 mpc8xxx_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700288
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700289 if (pdata->cs_control)
290 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700291 }
292}
293
294static
Anton Vorontsov575c5802009-06-18 16:49:08 -0700295int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700296{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700297 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700298 u8 bits_per_word, pm;
Kumar Galaccf06992006-05-20 15:00:15 -0700299 u32 hz;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700300 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700301
Anton Vorontsov575c5802009-06-18 16:49:08 -0700302 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700303
304 if (t) {
305 bits_per_word = t->bits_per_word;
306 hz = t->speed_hz;
307 } else {
308 bits_per_word = 0;
309 hz = 0;
310 }
311
312 /* spi_transfer level calls that work per-word */
313 if (!bits_per_word)
314 bits_per_word = spi->bits_per_word;
315
316 /* Make sure its a bit width we support [4..16, 32] */
317 if ((bits_per_word < 4)
318 || ((bits_per_word > 16) && (bits_per_word != 32)))
319 return -EINVAL;
320
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700321 if (!hz)
322 hz = spi->max_speed_hz;
323
324 cs->rx_shift = 0;
325 cs->tx_shift = 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700326 if (bits_per_word <= 8) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700327 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
328 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400329 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700330 cs->rx_shift = 16;
331 cs->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700332 }
Kumar Galaccf06992006-05-20 15:00:15 -0700333 } else if (bits_per_word <= 16) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700334 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
335 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400336 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700337 cs->rx_shift = 16;
338 cs->tx_shift = 16;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700339 }
Kumar Galaccf06992006-05-20 15:00:15 -0700340 } else if (bits_per_word <= 32) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700341 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
342 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
Kumar Galaccf06992006-05-20 15:00:15 -0700343 } else
344 return -EINVAL;
345
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400346 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
347 spi->mode & SPI_LSB_FIRST) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700348 cs->tx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700349 if (bits_per_word <= 8)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700350 cs->rx_shift = 8;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700351 else
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700352 cs->rx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700353 }
354
Anton Vorontsov575c5802009-06-18 16:49:08 -0700355 mpc8xxx_spi->rx_shift = cs->rx_shift;
356 mpc8xxx_spi->tx_shift = cs->tx_shift;
357 mpc8xxx_spi->get_rx = cs->get_rx;
358 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700359
360 if (bits_per_word == 32)
361 bits_per_word = 0;
362 else
363 bits_per_word = bits_per_word - 1;
364
Anton Vorontsov32421da2007-07-31 00:38:41 -0700365 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700366 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
367 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700368
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700369 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700370
Anton Vorontsov575c5802009-06-18 16:49:08 -0700371 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700372 cs->hw_mode |= SPMODE_DIV16;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700373 pm = mpc8xxx_spi->spibrg / (hz * 64);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700374
375 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
376 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Anton Vorontsov575c5802009-06-18 16:49:08 -0700377 hz, mpc8xxx_spi->spibrg / 1024);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700378 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700379 pm = 16;
Chen Gonga61f5342008-07-23 21:29:52 -0700380 } else
Anton Vorontsov575c5802009-06-18 16:49:08 -0700381 pm = mpc8xxx_spi->spibrg / (hz * 4);
Chen Gonga61f5342008-07-23 21:29:52 -0700382 if (pm)
383 pm--;
384
385 cs->hw_mode |= SPMODE_PM(pm);
David Brownelldccd5732007-07-17 04:04:02 -0700386
Anton Vorontsova35c1712009-10-12 20:49:24 +0400387 mpc8xxx_spi_change_mode(spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700388 return 0;
389}
390
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400391static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
Kumar Galaccf06992006-05-20 15:00:15 -0700392{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400393 struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
394 struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
395 unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
396 unsigned int xfer_ofs;
Kumar Galaccf06992006-05-20 15:00:15 -0700397
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400398 xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
Kumar Galaccf06992006-05-20 15:00:15 -0700399
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400400 out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
401 out_be16(&rx_bd->cbd_datlen, 0);
402 out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
403
404 out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
405 out_be16(&tx_bd->cbd_datlen, xfer_len);
406 out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
407 BD_SC_LAST);
408
409 /* start transfer */
410 mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
411}
412
413static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
414 struct spi_transfer *t, bool is_dma_mapped)
415{
416 struct device *dev = mspi->dev;
417
418 if (is_dma_mapped) {
419 mspi->map_tx_dma = 0;
420 mspi->map_rx_dma = 0;
421 } else {
422 mspi->map_tx_dma = 1;
423 mspi->map_rx_dma = 1;
424 }
425
426 if (!t->tx_buf) {
427 mspi->tx_dma = mspi->dma_dummy_tx;
428 mspi->map_tx_dma = 0;
429 }
430
431 if (!t->rx_buf) {
432 mspi->rx_dma = mspi->dma_dummy_rx;
433 mspi->map_rx_dma = 0;
434 }
435
436 if (mspi->map_tx_dma) {
437 void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
438
439 mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
440 DMA_TO_DEVICE);
441 if (dma_mapping_error(dev, mspi->tx_dma)) {
442 dev_err(dev, "unable to map tx dma\n");
443 return -ENOMEM;
444 }
445 } else {
446 mspi->tx_dma = t->tx_dma;
447 }
448
449 if (mspi->map_rx_dma) {
450 mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
451 DMA_FROM_DEVICE);
452 if (dma_mapping_error(dev, mspi->rx_dma)) {
453 dev_err(dev, "unable to map rx dma\n");
454 goto err_rx_dma;
455 }
456 } else {
457 mspi->rx_dma = t->rx_dma;
458 }
459
460 /* enable rx ints */
461 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
462
463 mspi->xfer_in_progress = t;
464 mspi->count = t->len;
465
466 /* start CPM transfers */
467 mpc8xxx_spi_cpm_bufs_start(mspi);
468
469 return 0;
470
471err_rx_dma:
472 if (mspi->map_tx_dma)
473 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
474 return -ENOMEM;
475}
476
477static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
478{
479 struct device *dev = mspi->dev;
480 struct spi_transfer *t = mspi->xfer_in_progress;
481
482 if (mspi->map_tx_dma)
483 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
484 if (mspi->map_tx_dma)
485 dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
486 mspi->xfer_in_progress = NULL;
487}
488
489static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
490 struct spi_transfer *t, unsigned int len)
491{
492 u32 word;
493
494 mspi->count = len;
495
496 /* enable rx ints */
497 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
498
499 /* transmit word */
500 word = mspi->get_tx(mspi);
501 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
502
503 return 0;
504}
505
506static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
507 bool is_dma_mapped)
508{
509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
510 unsigned int len = t->len;
511 u8 bits_per_word;
512 int ret;
513
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700514 bits_per_word = spi->bits_per_word;
515 if (t->bits_per_word)
516 bits_per_word = t->bits_per_word;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400517
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700518 if (bits_per_word > 8) {
519 /* invalid length? */
520 if (len & 1)
521 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700522 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700523 }
524 if (bits_per_word > 16) {
525 /* invalid length? */
526 if (len & 1)
527 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700528 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700529 }
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400530
531 mpc8xxx_spi->tx = t->tx_buf;
532 mpc8xxx_spi->rx = t->rx_buf;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700533
Anton Vorontsov575c5802009-06-18 16:49:08 -0700534 INIT_COMPLETION(mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700535
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400536 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
537 ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
538 else
539 ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
540 if (ret)
541 return ret;
Kumar Galaccf06992006-05-20 15:00:15 -0700542
Anton Vorontsov575c5802009-06-18 16:49:08 -0700543 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700544
545 /* disable rx ints */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700546 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700547
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400548 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
549 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
550
Anton Vorontsov575c5802009-06-18 16:49:08 -0700551 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700552}
553
Anton Vorontsov575c5802009-06-18 16:49:08 -0700554static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700555{
556 struct spi_device *spi = m->spi;
557 struct spi_transfer *t;
558 unsigned int cs_change;
559 const int nsecs = 50;
560 int status;
561
562 cs_change = 1;
563 status = 0;
564 list_for_each_entry(t, &m->transfers, transfer_list) {
565 if (t->bits_per_word || t->speed_hz) {
566 /* Don't allow changes if CS is active */
567 status = -EINVAL;
568
569 if (cs_change)
Anton Vorontsov575c5802009-06-18 16:49:08 -0700570 status = mpc8xxx_spi_setup_transfer(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700571 if (status < 0)
572 break;
573 }
574
575 if (cs_change) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700576 mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700577 ndelay(nsecs);
578 }
579 cs_change = t->cs_change;
580 if (t->len)
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400581 status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700582 if (status) {
583 status = -EMSGSIZE;
584 break;
585 }
586 m->actual_length += t->len;
587
588 if (t->delay_usecs)
589 udelay(t->delay_usecs);
590
591 if (cs_change) {
592 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700593 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700594 ndelay(nsecs);
595 }
596 }
597
598 m->status = status;
599 m->complete(m->context);
600
601 if (status || !cs_change) {
602 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700603 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700604 }
605
Anton Vorontsov575c5802009-06-18 16:49:08 -0700606 mpc8xxx_spi_setup_transfer(spi, NULL);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700607}
608
Anton Vorontsov575c5802009-06-18 16:49:08 -0700609static void mpc8xxx_spi_work(struct work_struct *work)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700610{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700611 struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700612 work);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700613
Anton Vorontsov575c5802009-06-18 16:49:08 -0700614 spin_lock_irq(&mpc8xxx_spi->lock);
615 while (!list_empty(&mpc8xxx_spi->queue)) {
616 struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700617 struct spi_message, queue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700618
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700619 list_del_init(&m->queue);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700620 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700621
Anton Vorontsov575c5802009-06-18 16:49:08 -0700622 mpc8xxx_spi_do_one_msg(m);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700623
Anton Vorontsov575c5802009-06-18 16:49:08 -0700624 spin_lock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700625 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700626 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700627}
628
Anton Vorontsov575c5802009-06-18 16:49:08 -0700629static int mpc8xxx_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700630{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700631 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700632 int retval;
633 u32 hw_mode;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700634 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700635
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700636 if (!spi->max_speed_hz)
637 return -EINVAL;
638
639 if (!cs) {
640 cs = kzalloc(sizeof *cs, GFP_KERNEL);
641 if (!cs)
642 return -ENOMEM;
643 spi->controller_state = cs;
644 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700645 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700646
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700647 hw_mode = cs->hw_mode; /* Save orginal settings */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700648 cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700649 /* mask out bits we are going to set */
650 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
651 | SPMODE_REV | SPMODE_LOOP);
652
653 if (spi->mode & SPI_CPHA)
654 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
655 if (spi->mode & SPI_CPOL)
656 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
657 if (!(spi->mode & SPI_LSB_FIRST))
658 cs->hw_mode |= SPMODE_REV;
659 if (spi->mode & SPI_LOOP)
660 cs->hw_mode |= SPMODE_LOOP;
661
Anton Vorontsov575c5802009-06-18 16:49:08 -0700662 retval = mpc8xxx_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700663 if (retval < 0) {
664 cs->hw_mode = hw_mode; /* Restore settings */
665 return retval;
666 }
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700667 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700668}
669
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400670static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
Kumar Galaccf06992006-05-20 15:00:15 -0700671{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400672 u16 len;
Kumar Galaccf06992006-05-20 15:00:15 -0700673
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400674 dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
675 in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
Kumar Galaccf06992006-05-20 15:00:15 -0700676
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400677 len = in_be16(&mspi->rx_bd->cbd_datlen);
678 if (len > mspi->count) {
679 WARN_ON(1);
680 len = mspi->count;
Kumar Galaccf06992006-05-20 15:00:15 -0700681 }
682
683 /* Clear the events */
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400684 mpc8xxx_spi_write_reg(&mspi->base->event, events);
685
686 mspi->count -= len;
687 if (mspi->count)
688 mpc8xxx_spi_cpm_bufs_start(mspi);
689 else
690 complete(&mspi->done);
691}
692
693static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
694{
695 /* We need handle RX first */
696 if (events & SPIE_NE) {
697 u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
698
699 if (mspi->rx)
700 mspi->get_rx(rx_data, mspi);
701 }
702
703 if ((events & SPIE_NF) == 0)
704 /* spin until TX is done */
705 while (((events =
706 mpc8xxx_spi_read_reg(&mspi->base->event)) &
707 SPIE_NF) == 0)
708 cpu_relax();
709
710 /* Clear the events */
711 mpc8xxx_spi_write_reg(&mspi->base->event, events);
712
713 mspi->count -= 1;
714 if (mspi->count) {
715 u32 word = mspi->get_tx(mspi);
716
717 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
718 } else {
719 complete(&mspi->done);
720 }
721}
722
723static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
724{
725 struct mpc8xxx_spi *mspi = context_data;
726 irqreturn_t ret = IRQ_NONE;
727 u32 events;
728
729 /* Get interrupt events(tx/rx) */
730 events = mpc8xxx_spi_read_reg(&mspi->base->event);
731 if (events)
732 ret = IRQ_HANDLED;
733
734 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
735
736 if (mspi->flags & SPI_CPM_MODE)
737 mpc8xxx_spi_cpm_irq(mspi, events);
738 else
739 mpc8xxx_spi_cpu_irq(mspi, events);
Kumar Galaccf06992006-05-20 15:00:15 -0700740
741 return ret;
742}
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400743
Anton Vorontsov575c5802009-06-18 16:49:08 -0700744static int mpc8xxx_spi_transfer(struct spi_device *spi,
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700745 struct spi_message *m)
746{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700747 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700748 unsigned long flags;
749
750 m->actual_length = 0;
751 m->status = -EINPROGRESS;
752
Anton Vorontsov575c5802009-06-18 16:49:08 -0700753 spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
754 list_add_tail(&m->queue, &mpc8xxx_spi->queue);
755 queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
756 spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700757
758 return 0;
759}
760
761
Anton Vorontsov575c5802009-06-18 16:49:08 -0700762static void mpc8xxx_spi_cleanup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700763{
764 kfree(spi->controller_state);
765}
Kumar Galaccf06992006-05-20 15:00:15 -0700766
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400767static void *mpc8xxx_spi_alloc_dummy_rx(void)
768{
769 mutex_lock(&mpc8xxx_dummy_rx_lock);
770
771 if (!mpc8xxx_dummy_rx)
772 mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
773 if (mpc8xxx_dummy_rx)
774 mpc8xxx_dummy_rx_refcnt++;
775
776 mutex_unlock(&mpc8xxx_dummy_rx_lock);
777
778 return mpc8xxx_dummy_rx;
779}
780
781static void mpc8xxx_spi_free_dummy_rx(void)
782{
783 mutex_lock(&mpc8xxx_dummy_rx_lock);
784
785 switch (mpc8xxx_dummy_rx_refcnt) {
786 case 0:
787 WARN_ON(1);
788 break;
789 case 1:
790 kfree(mpc8xxx_dummy_rx);
791 mpc8xxx_dummy_rx = NULL;
792 /* fall through */
793 default:
794 mpc8xxx_dummy_rx_refcnt--;
795 break;
796 }
797
798 mutex_unlock(&mpc8xxx_dummy_rx_lock);
799}
800
801static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
802{
803 struct device *dev = mspi->dev;
804 struct device_node *np = dev_archdata_get_node(&dev->archdata);
805 const u32 *iprop;
806 int size;
807 unsigned long spi_base_ofs;
808 unsigned long pram_ofs = -ENOMEM;
809
810 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
811 iprop = of_get_property(np, "reg", &size);
812
813 /* QE with a fixed pram location? */
814 if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
815 return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
816
817 /* QE but with a dynamic pram location? */
818 if (mspi->flags & SPI_QE) {
819 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
820 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
821 QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
822 return pram_ofs;
823 }
824
825 /* CPM1 and CPM2 pram must be at a fixed addr. */
826 if (!iprop || size != sizeof(*iprop) * 4)
827 return -ENOMEM;
828
829 spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
830 if (IS_ERR_VALUE(spi_base_ofs))
831 return -ENOMEM;
832
833 if (mspi->flags & SPI_CPM2) {
834 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
835 if (!IS_ERR_VALUE(pram_ofs)) {
836 u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
837
838 out_be16(spi_base, pram_ofs);
839 }
840 } else {
841 struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
842 u16 rpbase = in_be16(&pram->rpbase);
843
844 /* Microcode relocation patch applied? */
845 if (rpbase)
846 pram_ofs = rpbase;
847 else
848 return spi_base_ofs;
849 }
850
851 cpm_muram_free(spi_base_ofs);
852 return pram_ofs;
853}
854
855static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
856{
857 struct device *dev = mspi->dev;
858 struct device_node *np = dev_archdata_get_node(&dev->archdata);
859 const u32 *iprop;
860 int size;
861 unsigned long pram_ofs;
862 unsigned long bds_ofs;
863
864 if (!(mspi->flags & SPI_CPM_MODE))
865 return 0;
866
867 if (!mpc8xxx_spi_alloc_dummy_rx())
868 return -ENOMEM;
869
870 if (mspi->flags & SPI_QE) {
871 iprop = of_get_property(np, "cell-index", &size);
872 if (iprop && size == sizeof(*iprop))
873 mspi->subblock = *iprop;
874
875 switch (mspi->subblock) {
876 default:
877 dev_warn(dev, "cell-index unspecified, assuming SPI1");
878 /* fall through */
879 case 0:
880 mspi->subblock = QE_CR_SUBBLOCK_SPI1;
881 break;
882 case 1:
883 mspi->subblock = QE_CR_SUBBLOCK_SPI2;
884 break;
885 }
886 }
887
888 pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
889 if (IS_ERR_VALUE(pram_ofs)) {
890 dev_err(dev, "can't allocate spi parameter ram\n");
891 goto err_pram;
892 }
893
894 bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
895 sizeof(*mspi->rx_bd), 8);
896 if (IS_ERR_VALUE(bds_ofs)) {
897 dev_err(dev, "can't allocate bds\n");
898 goto err_bds;
899 }
900
901 mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
902 DMA_TO_DEVICE);
903 if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
904 dev_err(dev, "unable to map dummy tx buffer\n");
905 goto err_dummy_tx;
906 }
907
908 mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
909 DMA_FROM_DEVICE);
910 if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
911 dev_err(dev, "unable to map dummy rx buffer\n");
912 goto err_dummy_rx;
913 }
914
915 mspi->pram = cpm_muram_addr(pram_ofs);
916
917 mspi->tx_bd = cpm_muram_addr(bds_ofs);
918 mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
919
920 /* Initialize parameter ram. */
921 out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
922 out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
923 out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
924 out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
925 out_be16(&mspi->pram->mrblr, SPI_MRBLR);
926 out_be32(&mspi->pram->rstate, 0);
927 out_be32(&mspi->pram->rdp, 0);
928 out_be16(&mspi->pram->rbptr, 0);
929 out_be16(&mspi->pram->rbc, 0);
930 out_be32(&mspi->pram->rxtmp, 0);
931 out_be32(&mspi->pram->tstate, 0);
932 out_be32(&mspi->pram->tdp, 0);
933 out_be16(&mspi->pram->tbptr, 0);
934 out_be16(&mspi->pram->tbc, 0);
935 out_be32(&mspi->pram->txtmp, 0);
936
937 return 0;
938
939err_dummy_rx:
940 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
941err_dummy_tx:
942 cpm_muram_free(bds_ofs);
943err_bds:
944 cpm_muram_free(pram_ofs);
945err_pram:
946 mpc8xxx_spi_free_dummy_rx();
947 return -ENOMEM;
948}
949
950static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
951{
952 struct device *dev = mspi->dev;
953
954 dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
955 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
956 cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
957 cpm_muram_free(cpm_muram_offset(mspi->pram));
958 mpc8xxx_spi_free_dummy_rx();
959}
960
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400961static const char *mpc8xxx_spi_strmode(unsigned int flags)
962{
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400963 if (flags & SPI_QE_CPU_MODE) {
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400964 return "QE CPU";
Anton Vorontsov4c1fba42009-10-12 20:49:27 +0400965 } else if (flags & SPI_CPM_MODE) {
966 if (flags & SPI_QE)
967 return "QE";
968 else if (flags & SPI_CPM2)
969 return "CPM2";
970 else
971 return "CPM1";
972 }
Anton Vorontsov87ec0e92009-10-12 20:49:25 +0400973 return "CPU";
974}
975
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700976static struct spi_master * __devinit
Anton Vorontsov575c5802009-06-18 16:49:08 -0700977mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700978{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700979 struct fsl_spi_platform_data *pdata = dev->platform_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700980 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700981 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -0700982 u32 regval;
983 int ret = 0;
984
Anton Vorontsov575c5802009-06-18 16:49:08 -0700985 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700986 if (master == NULL) {
987 ret = -ENOMEM;
988 goto err;
989 }
990
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700991 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -0700992
David Brownelle7db06b2009-06-17 16:26:04 -0700993 /* the spi->mode bits understood by this driver: */
994 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
995 | SPI_LSB_FIRST | SPI_LOOP;
996
Anton Vorontsov575c5802009-06-18 16:49:08 -0700997 master->setup = mpc8xxx_spi_setup;
998 master->transfer = mpc8xxx_spi_transfer;
999 master->cleanup = mpc8xxx_spi_cleanup;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001000
Anton Vorontsov575c5802009-06-18 16:49:08 -07001001 mpc8xxx_spi = spi_master_get_devdata(master);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001002 mpc8xxx_spi->dev = dev;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001003 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
1004 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001005 mpc8xxx_spi->flags = pdata->flags;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001006 mpc8xxx_spi->spibrg = pdata->sysclk;
Anton Vorontsove24a4d12007-08-10 13:01:01 -07001007
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001008 ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
1009 if (ret)
1010 goto err_cpm_init;
1011
Anton Vorontsov575c5802009-06-18 16:49:08 -07001012 mpc8xxx_spi->rx_shift = 0;
1013 mpc8xxx_spi->tx_shift = 0;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001014 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -07001015 mpc8xxx_spi->rx_shift = 16;
1016 mpc8xxx_spi->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -07001017 }
1018
Anton Vorontsov575c5802009-06-18 16:49:08 -07001019 init_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -07001020
Anton Vorontsov575c5802009-06-18 16:49:08 -07001021 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
1022 if (mpc8xxx_spi->base == NULL) {
Kumar Galaccf06992006-05-20 15:00:15 -07001023 ret = -ENOMEM;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001024 goto err_ioremap;
Kumar Galaccf06992006-05-20 15:00:15 -07001025 }
1026
Anton Vorontsov575c5802009-06-18 16:49:08 -07001027 mpc8xxx_spi->irq = irq;
Kumar Galaccf06992006-05-20 15:00:15 -07001028
1029 /* Register for SPI Interrupt */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001030 ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
1031 0, "mpc8xxx_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001032
1033 if (ret != 0)
1034 goto unmap_io;
1035
1036 master->bus_num = pdata->bus_num;
1037 master->num_chipselect = pdata->max_chipselect;
1038
1039 /* SPI controller initializations */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001040 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
1041 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
1042 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
1043 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -07001044
1045 /* Enable SPI interface */
1046 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001047 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -07001048 regval |= SPMODE_OP;
1049
Anton Vorontsov575c5802009-06-18 16:49:08 -07001050 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
1051 spin_lock_init(&mpc8xxx_spi->lock);
1052 init_completion(&mpc8xxx_spi->done);
1053 INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
1054 INIT_LIST_HEAD(&mpc8xxx_spi->queue);
Kumar Galaccf06992006-05-20 15:00:15 -07001055
Anton Vorontsov575c5802009-06-18 16:49:08 -07001056 mpc8xxx_spi->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -07001057 dev_name(master->dev.parent));
Anton Vorontsov575c5802009-06-18 16:49:08 -07001058 if (mpc8xxx_spi->workqueue == NULL) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001059 ret = -EBUSY;
Kumar Galaccf06992006-05-20 15:00:15 -07001060 goto free_irq;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001061 }
1062
1063 ret = spi_register_master(master);
1064 if (ret < 0)
1065 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -07001066
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001067 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
1068 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
Kumar Galaccf06992006-05-20 15:00:15 -07001069
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001070 return master;
Kumar Galaccf06992006-05-20 15:00:15 -07001071
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001072unreg_master:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001073 destroy_workqueue(mpc8xxx_spi->workqueue);
Kumar Galaccf06992006-05-20 15:00:15 -07001074free_irq:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001075 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001076unmap_io:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001077 iounmap(mpc8xxx_spi->base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001078err_ioremap:
1079 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1080err_cpm_init:
Kumar Galaccf06992006-05-20 15:00:15 -07001081 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -07001082err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001083 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -07001084}
1085
Anton Vorontsov575c5802009-06-18 16:49:08 -07001086static int __devexit mpc8xxx_spi_remove(struct device *dev)
Kumar Galaccf06992006-05-20 15:00:15 -07001087{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001088 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -07001089 struct spi_master *master;
1090
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001091 master = dev_get_drvdata(dev);
Anton Vorontsov575c5802009-06-18 16:49:08 -07001092 mpc8xxx_spi = spi_master_get_devdata(master);
Kumar Galaccf06992006-05-20 15:00:15 -07001093
Anton Vorontsov575c5802009-06-18 16:49:08 -07001094 flush_workqueue(mpc8xxx_spi->workqueue);
1095 destroy_workqueue(mpc8xxx_spi->workqueue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -07001096 spi_unregister_master(master);
1097
Anton Vorontsov575c5802009-06-18 16:49:08 -07001098 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
1099 iounmap(mpc8xxx_spi->base);
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001100 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -07001101
1102 return 0;
1103}
1104
Anton Vorontsov575c5802009-06-18 16:49:08 -07001105struct mpc8xxx_spi_probe_info {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001106 struct fsl_spi_platform_data pdata;
1107 int *gpios;
1108 bool *alow_flags;
1109};
1110
Anton Vorontsov575c5802009-06-18 16:49:08 -07001111static struct mpc8xxx_spi_probe_info *
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001112to_of_pinfo(struct fsl_spi_platform_data *pdata)
1113{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001114 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001115}
1116
Anton Vorontsov575c5802009-06-18 16:49:08 -07001117static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001118{
1119 struct device *dev = spi->dev.parent;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001120 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001121 u16 cs = spi->chip_select;
1122 int gpio = pinfo->gpios[cs];
1123 bool alow = pinfo->alow_flags[cs];
1124
1125 gpio_set_value(gpio, on ^ alow);
1126}
1127
Anton Vorontsov575c5802009-06-18 16:49:08 -07001128static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001129{
1130 struct device_node *np = dev_archdata_get_node(&dev->archdata);
1131 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001132 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001133 unsigned int ngpios;
1134 int i = 0;
1135 int ret;
1136
1137 ngpios = of_gpio_count(np);
1138 if (!ngpios) {
1139 /*
1140 * SPI w/o chip-select line. One SPI device is still permitted
1141 * though.
1142 */
1143 pdata->max_chipselect = 1;
1144 return 0;
1145 }
1146
Roel Kluin02141542009-06-16 15:31:15 -07001147 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001148 if (!pinfo->gpios)
1149 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -07001150 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001151
Roel Kluin02141542009-06-16 15:31:15 -07001152 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001153 GFP_KERNEL);
1154 if (!pinfo->alow_flags) {
1155 ret = -ENOMEM;
1156 goto err_alloc_flags;
1157 }
1158
1159 for (; i < ngpios; i++) {
1160 int gpio;
1161 enum of_gpio_flags flags;
1162
1163 gpio = of_get_gpio_flags(np, i, &flags);
1164 if (!gpio_is_valid(gpio)) {
1165 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
Anton Vorontsov783058f2009-10-12 20:49:22 +04001166 ret = gpio;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001167 goto err_loop;
1168 }
1169
1170 ret = gpio_request(gpio, dev_name(dev));
1171 if (ret) {
1172 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
1173 goto err_loop;
1174 }
1175
1176 pinfo->gpios[i] = gpio;
1177 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
1178
1179 ret = gpio_direction_output(pinfo->gpios[i],
1180 pinfo->alow_flags[i]);
1181 if (ret) {
1182 dev_err(dev, "can't set output direction for gpio "
1183 "#%d: %d\n", i, ret);
1184 goto err_loop;
1185 }
1186 }
1187
1188 pdata->max_chipselect = ngpios;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001189 pdata->cs_control = mpc8xxx_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001190
1191 return 0;
1192
1193err_loop:
1194 while (i >= 0) {
1195 if (gpio_is_valid(pinfo->gpios[i]))
1196 gpio_free(pinfo->gpios[i]);
1197 i--;
1198 }
1199
1200 kfree(pinfo->alow_flags);
1201 pinfo->alow_flags = NULL;
1202err_alloc_flags:
1203 kfree(pinfo->gpios);
1204 pinfo->gpios = NULL;
1205 return ret;
1206}
1207
Anton Vorontsov575c5802009-06-18 16:49:08 -07001208static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001209{
1210 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001211 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001212 int i;
1213
1214 if (!pinfo->gpios)
1215 return 0;
1216
1217 for (i = 0; i < pdata->max_chipselect; i++) {
1218 if (gpio_is_valid(pinfo->gpios[i]))
1219 gpio_free(pinfo->gpios[i]);
1220 }
1221
1222 kfree(pinfo->gpios);
1223 kfree(pinfo->alow_flags);
1224 return 0;
1225}
1226
Anton Vorontsov575c5802009-06-18 16:49:08 -07001227static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001228 const struct of_device_id *ofid)
1229{
1230 struct device *dev = &ofdev->dev;
1231 struct device_node *np = ofdev->node;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001232 struct mpc8xxx_spi_probe_info *pinfo;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001233 struct fsl_spi_platform_data *pdata;
1234 struct spi_master *master;
1235 struct resource mem;
1236 struct resource irq;
1237 const void *prop;
1238 int ret = -ENOMEM;
1239
1240 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
1241 if (!pinfo)
1242 return -ENOMEM;
1243
1244 pdata = &pinfo->pdata;
1245 dev->platform_data = pdata;
1246
1247 /* Allocate bus num dynamically. */
1248 pdata->bus_num = -1;
1249
1250 /* SPI controller is either clocked from QE or SoC clock. */
1251 pdata->sysclk = get_brgfreq();
1252 if (pdata->sysclk == -1) {
1253 pdata->sysclk = fsl_get_sys_freq();
1254 if (pdata->sysclk == -1) {
1255 ret = -ENODEV;
1256 goto err_clk;
1257 }
1258 }
1259
1260 prop = of_get_property(np, "mode", NULL);
1261 if (prop && !strcmp(prop, "cpu-qe"))
Anton Vorontsov87ec0e92009-10-12 20:49:25 +04001262 pdata->flags = SPI_QE_CPU_MODE;
Anton Vorontsov4c1fba42009-10-12 20:49:27 +04001263 else if (prop && !strcmp(prop, "qe"))
1264 pdata->flags = SPI_CPM_MODE | SPI_QE;
1265 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
1266 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
1267 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
1268 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001269
Anton Vorontsov575c5802009-06-18 16:49:08 -07001270 ret = of_mpc8xxx_spi_get_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001271 if (ret)
1272 goto err;
1273
1274 ret = of_address_to_resource(np, 0, &mem);
1275 if (ret)
1276 goto err;
1277
1278 ret = of_irq_to_resource(np, 0, &irq);
1279 if (!ret) {
1280 ret = -EINVAL;
1281 goto err;
1282 }
1283
Anton Vorontsov575c5802009-06-18 16:49:08 -07001284 master = mpc8xxx_spi_probe(dev, &mem, irq.start);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001285 if (IS_ERR(master)) {
1286 ret = PTR_ERR(master);
1287 goto err;
1288 }
1289
1290 of_register_spi_devices(master, np);
1291
1292 return 0;
1293
1294err:
Anton Vorontsov575c5802009-06-18 16:49:08 -07001295 of_mpc8xxx_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001296err_clk:
1297 kfree(pinfo);
1298 return ret;
1299}
1300
Anton Vorontsov575c5802009-06-18 16:49:08 -07001301static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001302{
1303 int ret;
1304
Anton Vorontsov575c5802009-06-18 16:49:08 -07001305 ret = mpc8xxx_spi_remove(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001306 if (ret)
1307 return ret;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001308 of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001309 return 0;
1310}
1311
Anton Vorontsov575c5802009-06-18 16:49:08 -07001312static const struct of_device_id of_mpc8xxx_spi_match[] = {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001313 { .compatible = "fsl,spi" },
1314 {},
1315};
Anton Vorontsov575c5802009-06-18 16:49:08 -07001316MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001317
Anton Vorontsov575c5802009-06-18 16:49:08 -07001318static struct of_platform_driver of_mpc8xxx_spi_driver = {
1319 .name = "mpc8xxx_spi",
1320 .match_table = of_mpc8xxx_spi_match,
1321 .probe = of_mpc8xxx_spi_probe,
1322 .remove = __devexit_p(of_mpc8xxx_spi_remove),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001323};
1324
1325#ifdef CONFIG_MPC832x_RDB
1326/*
1327 * XXX XXX XXX
1328 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
1329 * only. The driver should go away soon, since newer MPC8323E-RDB's device
1330 * tree can work with OpenFirmware driver. But for now we support old trees
1331 * as well.
1332 */
Anton Vorontsov575c5802009-06-18 16:49:08 -07001333static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001334{
1335 struct resource *mem;
1336 unsigned int irq;
1337 struct spi_master *master;
1338
1339 if (!pdev->dev.platform_data)
1340 return -EINVAL;
1341
1342 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1343 if (!mem)
1344 return -EINVAL;
1345
1346 irq = platform_get_irq(pdev, 0);
1347 if (!irq)
1348 return -EINVAL;
1349
Anton Vorontsov575c5802009-06-18 16:49:08 -07001350 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001351 if (IS_ERR(master))
1352 return PTR_ERR(master);
1353 return 0;
1354}
1355
Anton Vorontsov575c5802009-06-18 16:49:08 -07001356static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001357{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001358 return mpc8xxx_spi_remove(&pdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001359}
1360
Anton Vorontsov575c5802009-06-18 16:49:08 -07001361MODULE_ALIAS("platform:mpc8xxx_spi");
1362static struct platform_driver mpc8xxx_spi_driver = {
1363 .probe = plat_mpc8xxx_spi_probe,
1364 .remove = __exit_p(plat_mpc8xxx_spi_remove),
Kumar Galaccf06992006-05-20 15:00:15 -07001365 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -07001366 .name = "mpc8xxx_spi",
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001367 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -07001368 },
1369};
1370
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001371static bool legacy_driver_failed;
1372
1373static void __init legacy_driver_register(void)
1374{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001375 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001376}
1377
1378static void __exit legacy_driver_unregister(void)
1379{
1380 if (legacy_driver_failed)
1381 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -07001382 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001383}
1384#else
1385static void __init legacy_driver_register(void) {}
1386static void __exit legacy_driver_unregister(void) {}
1387#endif /* CONFIG_MPC832x_RDB */
1388
Anton Vorontsov575c5802009-06-18 16:49:08 -07001389static int __init mpc8xxx_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -07001390{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001391 legacy_driver_register();
Anton Vorontsov575c5802009-06-18 16:49:08 -07001392 return of_register_platform_driver(&of_mpc8xxx_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -07001393}
1394
Anton Vorontsov575c5802009-06-18 16:49:08 -07001395static void __exit mpc8xxx_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -07001396{
Anton Vorontsov575c5802009-06-18 16:49:08 -07001397 of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -07001398 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -07001399}
1400
Anton Vorontsov575c5802009-06-18 16:49:08 -07001401module_init(mpc8xxx_spi_init);
1402module_exit(mpc8xxx_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -07001403
1404MODULE_AUTHOR("Kumar Gala");
Anton Vorontsov575c5802009-06-18 16:49:08 -07001405MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -07001406MODULE_LICENSE("GPL");