blob: e35294239034183f2df1d5dbeb0dc0a34a29dc9f [file] [log] [blame]
Steven King34b8c662010-01-20 13:49:44 -07001/*
2 * Freescale/Motorola Coldfire Queued SPI driver
3 *
4 * Copyright 2010 Steven King <sfking@fdwdc.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
19 *
20*/
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/errno.h>
26#include <linux/platform_device.h>
Greg Ungerer5e1c5332010-07-28 13:32:46 +100027#include <linux/sched.h>
Steven King34b8c662010-01-20 13:49:44 -070028#include <linux/delay.h>
29#include <linux/io.h>
30#include <linux/clk.h>
31#include <linux/err.h>
32#include <linux/spi/spi.h>
Steven Kingbc98d132012-05-10 09:26:55 -070033#include <linux/pm_runtime.h>
Steven King34b8c662010-01-20 13:49:44 -070034
35#include <asm/coldfire.h>
Steven King0b4bf782011-04-24 10:48:07 -070036#include <asm/mcfsim.h>
Steven King34b8c662010-01-20 13:49:44 -070037#include <asm/mcfqspi.h>
38
39#define DRIVER_NAME "mcfqspi"
40
41#define MCFQSPI_BUSCLK (MCF_BUSCLK / 2)
42
43#define MCFQSPI_QMR 0x00
44#define MCFQSPI_QMR_MSTR 0x8000
45#define MCFQSPI_QMR_CPOL 0x0200
46#define MCFQSPI_QMR_CPHA 0x0100
47#define MCFQSPI_QDLYR 0x04
48#define MCFQSPI_QDLYR_SPE 0x8000
49#define MCFQSPI_QWR 0x08
50#define MCFQSPI_QWR_HALT 0x8000
51#define MCFQSPI_QWR_WREN 0x4000
52#define MCFQSPI_QWR_CSIV 0x1000
53#define MCFQSPI_QIR 0x0C
54#define MCFQSPI_QIR_WCEFB 0x8000
55#define MCFQSPI_QIR_ABRTB 0x4000
56#define MCFQSPI_QIR_ABRTL 0x1000
57#define MCFQSPI_QIR_WCEFE 0x0800
58#define MCFQSPI_QIR_ABRTE 0x0400
59#define MCFQSPI_QIR_SPIFE 0x0100
60#define MCFQSPI_QIR_WCEF 0x0008
61#define MCFQSPI_QIR_ABRT 0x0004
62#define MCFQSPI_QIR_SPIF 0x0001
63#define MCFQSPI_QAR 0x010
64#define MCFQSPI_QAR_TXBUF 0x00
65#define MCFQSPI_QAR_RXBUF 0x10
66#define MCFQSPI_QAR_CMDBUF 0x20
67#define MCFQSPI_QDR 0x014
68#define MCFQSPI_QCR 0x014
69#define MCFQSPI_QCR_CONT 0x8000
70#define MCFQSPI_QCR_BITSE 0x4000
71#define MCFQSPI_QCR_DT 0x2000
72
73struct mcfqspi {
74 void __iomem *iobase;
75 int irq;
76 struct clk *clk;
77 struct mcfqspi_cs_control *cs_control;
78
79 wait_queue_head_t waitq;
Steven King34b8c662010-01-20 13:49:44 -070080};
81
82static void mcfqspi_wr_qmr(struct mcfqspi *mcfqspi, u16 val)
83{
84 writew(val, mcfqspi->iobase + MCFQSPI_QMR);
85}
86
87static void mcfqspi_wr_qdlyr(struct mcfqspi *mcfqspi, u16 val)
88{
89 writew(val, mcfqspi->iobase + MCFQSPI_QDLYR);
90}
91
92static u16 mcfqspi_rd_qdlyr(struct mcfqspi *mcfqspi)
93{
94 return readw(mcfqspi->iobase + MCFQSPI_QDLYR);
95}
96
97static void mcfqspi_wr_qwr(struct mcfqspi *mcfqspi, u16 val)
98{
99 writew(val, mcfqspi->iobase + MCFQSPI_QWR);
100}
101
102static void mcfqspi_wr_qir(struct mcfqspi *mcfqspi, u16 val)
103{
104 writew(val, mcfqspi->iobase + MCFQSPI_QIR);
105}
106
107static void mcfqspi_wr_qar(struct mcfqspi *mcfqspi, u16 val)
108{
109 writew(val, mcfqspi->iobase + MCFQSPI_QAR);
110}
111
112static void mcfqspi_wr_qdr(struct mcfqspi *mcfqspi, u16 val)
113{
114 writew(val, mcfqspi->iobase + MCFQSPI_QDR);
115}
116
117static u16 mcfqspi_rd_qdr(struct mcfqspi *mcfqspi)
118{
119 return readw(mcfqspi->iobase + MCFQSPI_QDR);
120}
121
122static void mcfqspi_cs_select(struct mcfqspi *mcfqspi, u8 chip_select,
123 bool cs_high)
124{
125 mcfqspi->cs_control->select(mcfqspi->cs_control, chip_select, cs_high);
126}
127
128static void mcfqspi_cs_deselect(struct mcfqspi *mcfqspi, u8 chip_select,
129 bool cs_high)
130{
131 mcfqspi->cs_control->deselect(mcfqspi->cs_control, chip_select, cs_high);
132}
133
134static int mcfqspi_cs_setup(struct mcfqspi *mcfqspi)
135{
136 return (mcfqspi->cs_control && mcfqspi->cs_control->setup) ?
137 mcfqspi->cs_control->setup(mcfqspi->cs_control) : 0;
138}
139
140static void mcfqspi_cs_teardown(struct mcfqspi *mcfqspi)
141{
142 if (mcfqspi->cs_control && mcfqspi->cs_control->teardown)
143 mcfqspi->cs_control->teardown(mcfqspi->cs_control);
144}
145
146static u8 mcfqspi_qmr_baud(u32 speed_hz)
147{
148 return clamp((MCFQSPI_BUSCLK + speed_hz - 1) / speed_hz, 2u, 255u);
149}
150
151static bool mcfqspi_qdlyr_spe(struct mcfqspi *mcfqspi)
152{
153 return mcfqspi_rd_qdlyr(mcfqspi) & MCFQSPI_QDLYR_SPE;
154}
155
156static irqreturn_t mcfqspi_irq_handler(int this_irq, void *dev_id)
157{
158 struct mcfqspi *mcfqspi = dev_id;
159
160 /* clear interrupt */
161 mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE | MCFQSPI_QIR_SPIF);
162 wake_up(&mcfqspi->waitq);
163
164 return IRQ_HANDLED;
165}
166
167static void mcfqspi_transfer_msg8(struct mcfqspi *mcfqspi, unsigned count,
168 const u8 *txbuf, u8 *rxbuf)
169{
170 unsigned i, n, offset = 0;
171
172 n = min(count, 16u);
173
174 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
175 for (i = 0; i < n; ++i)
176 mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
177
178 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
179 if (txbuf)
180 for (i = 0; i < n; ++i)
181 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
182 else
183 for (i = 0; i < count; ++i)
184 mcfqspi_wr_qdr(mcfqspi, 0);
185
186 count -= n;
187 if (count) {
188 u16 qwr = 0xf08;
189 mcfqspi_wr_qwr(mcfqspi, 0x700);
190 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
191
192 do {
193 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
194 mcfqspi_wr_qwr(mcfqspi, qwr);
195 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
196 if (rxbuf) {
197 mcfqspi_wr_qar(mcfqspi,
198 MCFQSPI_QAR_RXBUF + offset);
199 for (i = 0; i < 8; ++i)
200 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
201 }
202 n = min(count, 8u);
203 if (txbuf) {
204 mcfqspi_wr_qar(mcfqspi,
205 MCFQSPI_QAR_TXBUF + offset);
206 for (i = 0; i < n; ++i)
207 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
208 }
209 qwr = (offset ? 0x808 : 0) + ((n - 1) << 8);
210 offset ^= 8;
211 count -= n;
212 } while (count);
213 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
214 mcfqspi_wr_qwr(mcfqspi, qwr);
215 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
216 if (rxbuf) {
217 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
218 for (i = 0; i < 8; ++i)
219 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
220 offset ^= 8;
221 }
222 } else {
223 mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
224 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
225 }
226 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
227 if (rxbuf) {
228 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
229 for (i = 0; i < n; ++i)
230 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
231 }
232}
233
234static void mcfqspi_transfer_msg16(struct mcfqspi *mcfqspi, unsigned count,
235 const u16 *txbuf, u16 *rxbuf)
236{
237 unsigned i, n, offset = 0;
238
239 n = min(count, 16u);
240
241 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_CMDBUF);
242 for (i = 0; i < n; ++i)
243 mcfqspi_wr_qdr(mcfqspi, MCFQSPI_QCR_BITSE);
244
245 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_TXBUF);
246 if (txbuf)
247 for (i = 0; i < n; ++i)
248 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
249 else
250 for (i = 0; i < count; ++i)
251 mcfqspi_wr_qdr(mcfqspi, 0);
252
253 count -= n;
254 if (count) {
255 u16 qwr = 0xf08;
256 mcfqspi_wr_qwr(mcfqspi, 0x700);
257 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
258
259 do {
260 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
261 mcfqspi_wr_qwr(mcfqspi, qwr);
262 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
263 if (rxbuf) {
264 mcfqspi_wr_qar(mcfqspi,
265 MCFQSPI_QAR_RXBUF + offset);
266 for (i = 0; i < 8; ++i)
267 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
268 }
269 n = min(count, 8u);
270 if (txbuf) {
271 mcfqspi_wr_qar(mcfqspi,
272 MCFQSPI_QAR_TXBUF + offset);
273 for (i = 0; i < n; ++i)
274 mcfqspi_wr_qdr(mcfqspi, *txbuf++);
275 }
276 qwr = (offset ? 0x808 : 0x000) + ((n - 1) << 8);
277 offset ^= 8;
278 count -= n;
279 } while (count);
280 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
281 mcfqspi_wr_qwr(mcfqspi, qwr);
282 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
283 if (rxbuf) {
284 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
285 for (i = 0; i < 8; ++i)
286 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
287 offset ^= 8;
288 }
289 } else {
290 mcfqspi_wr_qwr(mcfqspi, (n - 1) << 8);
291 mcfqspi_wr_qdlyr(mcfqspi, MCFQSPI_QDLYR_SPE);
292 }
293 wait_event(mcfqspi->waitq, !mcfqspi_qdlyr_spe(mcfqspi));
294 if (rxbuf) {
295 mcfqspi_wr_qar(mcfqspi, MCFQSPI_QAR_RXBUF + offset);
296 for (i = 0; i < n; ++i)
297 *rxbuf++ = mcfqspi_rd_qdr(mcfqspi);
298 }
299}
300
Steven Kingbc98d132012-05-10 09:26:55 -0700301static int mcfqspi_transfer_one_message(struct spi_master *master,
302 struct spi_message *msg)
Steven King34b8c662010-01-20 13:49:44 -0700303{
Steven Kingbc98d132012-05-10 09:26:55 -0700304 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
305 struct spi_device *spi = msg->spi;
306 struct spi_transfer *t;
307 int status = 0;
Steven King34b8c662010-01-20 13:49:44 -0700308
Steven Kingbc98d132012-05-10 09:26:55 -0700309 list_for_each_entry(t, &msg->transfers, transfer_list) {
310 bool cs_high = spi->mode & SPI_CS_HIGH;
311 u16 qmr = MCFQSPI_QMR_MSTR;
Steven King34b8c662010-01-20 13:49:44 -0700312
Stephen Warren24778be2013-05-21 20:36:35 -0600313 qmr |= t->bits_per_word << 10;
Steven Kingbc98d132012-05-10 09:26:55 -0700314 if (spi->mode & SPI_CPHA)
315 qmr |= MCFQSPI_QMR_CPHA;
316 if (spi->mode & SPI_CPOL)
317 qmr |= MCFQSPI_QMR_CPOL;
318 if (t->speed_hz)
319 qmr |= mcfqspi_qmr_baud(t->speed_hz);
320 else
321 qmr |= mcfqspi_qmr_baud(spi->max_speed_hz);
322 mcfqspi_wr_qmr(mcfqspi, qmr);
Steven King34b8c662010-01-20 13:49:44 -0700323
Steven Kingbc98d132012-05-10 09:26:55 -0700324 mcfqspi_cs_select(mcfqspi, spi->chip_select, cs_high);
Steven King34b8c662010-01-20 13:49:44 -0700325
Steven Kingbc98d132012-05-10 09:26:55 -0700326 mcfqspi_wr_qir(mcfqspi, MCFQSPI_QIR_SPIFE);
Laxman Dewangan766ed702012-12-18 14:25:43 +0530327 if (t->bits_per_word == 8)
Steven Kingbc98d132012-05-10 09:26:55 -0700328 mcfqspi_transfer_msg8(mcfqspi, t->len, t->tx_buf,
329 t->rx_buf);
330 else
331 mcfqspi_transfer_msg16(mcfqspi, t->len / 2, t->tx_buf,
332 t->rx_buf);
333 mcfqspi_wr_qir(mcfqspi, 0);
Steven King34b8c662010-01-20 13:49:44 -0700334
Steven Kingbc98d132012-05-10 09:26:55 -0700335 if (t->delay_usecs)
336 udelay(t->delay_usecs);
337 if (t->cs_change) {
338 if (!list_is_last(&t->transfer_list, &msg->transfers))
339 mcfqspi_cs_deselect(mcfqspi, spi->chip_select,
340 cs_high);
341 } else {
342 if (list_is_last(&t->transfer_list, &msg->transfers))
343 mcfqspi_cs_deselect(mcfqspi, spi->chip_select,
344 cs_high);
Steven King34b8c662010-01-20 13:49:44 -0700345 }
Steven Kingbc98d132012-05-10 09:26:55 -0700346 msg->actual_length += t->len;
Steven King34b8c662010-01-20 13:49:44 -0700347 }
Steven Kingbc98d132012-05-10 09:26:55 -0700348 msg->status = status;
349 spi_finalize_current_message(master);
350
351 return status;
352
Steven King34b8c662010-01-20 13:49:44 -0700353}
354
Steven King34b8c662010-01-20 13:49:44 -0700355static int mcfqspi_setup(struct spi_device *spi)
356{
Steven King34b8c662010-01-20 13:49:44 -0700357 if (spi->chip_select >= spi->master->num_chipselect) {
358 dev_dbg(&spi->dev, "%d chip select is out of range\n",
359 spi->chip_select);
360 return -EINVAL;
361 }
362
363 mcfqspi_cs_deselect(spi_master_get_devdata(spi->master),
364 spi->chip_select, spi->mode & SPI_CS_HIGH);
365
366 dev_dbg(&spi->dev,
367 "bits per word %d, chip select %d, speed %d KHz\n",
368 spi->bits_per_word, spi->chip_select,
369 (MCFQSPI_BUSCLK / mcfqspi_qmr_baud(spi->max_speed_hz))
370 / 1000);
371
372 return 0;
373}
374
Grant Likelyfd4a3192012-12-07 16:57:14 +0000375static int mcfqspi_probe(struct platform_device *pdev)
Steven King34b8c662010-01-20 13:49:44 -0700376{
377 struct spi_master *master;
378 struct mcfqspi *mcfqspi;
379 struct resource *res;
380 struct mcfqspi_platform_data *pdata;
381 int status;
382
Jingoo Han8074cf02013-07-30 16:58:59 +0900383 pdata = dev_get_platdata(&pdev->dev);
Wei Yongjun4a577f52013-05-16 13:11:32 +0800384 if (!pdata) {
385 dev_dbg(&pdev->dev, "platform data is missing\n");
386 return -ENOENT;
387 }
388
Steven King34b8c662010-01-20 13:49:44 -0700389 master = spi_alloc_master(&pdev->dev, sizeof(*mcfqspi));
390 if (master == NULL) {
391 dev_dbg(&pdev->dev, "spi_alloc_master failed\n");
392 return -ENOMEM;
393 }
394
395 mcfqspi = spi_master_get_devdata(master);
396
397 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jingoo Han9a3ced12013-12-04 14:10:10 +0900398 mcfqspi->iobase = devm_ioremap_resource(&pdev->dev, res);
399 if (IS_ERR(mcfqspi->iobase)) {
400 status = PTR_ERR(mcfqspi->iobase);
Steven King34b8c662010-01-20 13:49:44 -0700401 goto fail0;
402 }
403
Steven King34b8c662010-01-20 13:49:44 -0700404 mcfqspi->irq = platform_get_irq(pdev, 0);
405 if (mcfqspi->irq < 0) {
406 dev_dbg(&pdev->dev, "platform_get_irq failed\n");
407 status = -ENXIO;
Jingoo Han9a3ced12013-12-04 14:10:10 +0900408 goto fail0;
Steven King34b8c662010-01-20 13:49:44 -0700409 }
410
Jingoo Han9a3ced12013-12-04 14:10:10 +0900411 status = devm_request_irq(&pdev->dev, mcfqspi->irq, mcfqspi_irq_handler,
412 0, pdev->name, mcfqspi);
Steven King34b8c662010-01-20 13:49:44 -0700413 if (status) {
414 dev_dbg(&pdev->dev, "request_irq failed\n");
Jingoo Han9a3ced12013-12-04 14:10:10 +0900415 goto fail0;
Steven King34b8c662010-01-20 13:49:44 -0700416 }
417
Jingoo Han9a3ced12013-12-04 14:10:10 +0900418 mcfqspi->clk = devm_clk_get(&pdev->dev, "qspi_clk");
Steven King34b8c662010-01-20 13:49:44 -0700419 if (IS_ERR(mcfqspi->clk)) {
420 dev_dbg(&pdev->dev, "clk_get failed\n");
421 status = PTR_ERR(mcfqspi->clk);
Jingoo Han9a3ced12013-12-04 14:10:10 +0900422 goto fail0;
Steven King34b8c662010-01-20 13:49:44 -0700423 }
424 clk_enable(mcfqspi->clk);
425
Steven King34b8c662010-01-20 13:49:44 -0700426 master->bus_num = pdata->bus_num;
427 master->num_chipselect = pdata->num_chipselect;
428
429 mcfqspi->cs_control = pdata->cs_control;
430 status = mcfqspi_cs_setup(mcfqspi);
431 if (status) {
432 dev_dbg(&pdev->dev, "error initializing cs_control\n");
Jingoo Han9a3ced12013-12-04 14:10:10 +0900433 goto fail1;
Steven King34b8c662010-01-20 13:49:44 -0700434 }
435
Steven Kingbc98d132012-05-10 09:26:55 -0700436 init_waitqueue_head(&mcfqspi->waitq);
Steven Kingbc98d132012-05-10 09:26:55 -0700437
Steven King34b8c662010-01-20 13:49:44 -0700438 master->mode_bits = SPI_CS_HIGH | SPI_CPOL | SPI_CPHA;
Stephen Warren24778be2013-05-21 20:36:35 -0600439 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
Steven King34b8c662010-01-20 13:49:44 -0700440 master->setup = mcfqspi_setup;
Steven Kingbc98d132012-05-10 09:26:55 -0700441 master->transfer_one_message = mcfqspi_transfer_one_message;
Mark Brown3f36e802013-07-28 15:34:21 +0100442 master->auto_runtime_pm = true;
Steven King34b8c662010-01-20 13:49:44 -0700443
444 platform_set_drvdata(pdev, master);
445
Jingoo Han9a3ced12013-12-04 14:10:10 +0900446 status = devm_spi_register_master(&pdev->dev, master);
Steven King34b8c662010-01-20 13:49:44 -0700447 if (status) {
448 dev_dbg(&pdev->dev, "spi_register_master failed\n");
Jingoo Han9a3ced12013-12-04 14:10:10 +0900449 goto fail2;
Steven King34b8c662010-01-20 13:49:44 -0700450 }
Axel Lin8bd31342014-02-14 09:54:25 +0800451 pm_runtime_enable(&pdev->dev);
Steven Kingbc98d132012-05-10 09:26:55 -0700452
Steven King34b8c662010-01-20 13:49:44 -0700453 dev_info(&pdev->dev, "Coldfire QSPI bus driver\n");
454
455 return 0;
456
Steven King34b8c662010-01-20 13:49:44 -0700457fail2:
Jingoo Han9a3ced12013-12-04 14:10:10 +0900458 mcfqspi_cs_teardown(mcfqspi);
Steven King34b8c662010-01-20 13:49:44 -0700459fail1:
Jingoo Han9a3ced12013-12-04 14:10:10 +0900460 clk_disable(mcfqspi->clk);
Steven King34b8c662010-01-20 13:49:44 -0700461fail0:
462 spi_master_put(master);
463
464 dev_dbg(&pdev->dev, "Coldfire QSPI probe failed\n");
465
466 return status;
467}
468
Grant Likelyfd4a3192012-12-07 16:57:14 +0000469static int mcfqspi_remove(struct platform_device *pdev)
Steven King34b8c662010-01-20 13:49:44 -0700470{
471 struct spi_master *master = platform_get_drvdata(pdev);
472 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
Steven King34b8c662010-01-20 13:49:44 -0700473
Axel Lin8bd31342014-02-14 09:54:25 +0800474 pm_runtime_disable(&pdev->dev);
Steven King34b8c662010-01-20 13:49:44 -0700475 /* disable the hardware (set the baud rate to 0) */
476 mcfqspi_wr_qmr(mcfqspi, MCFQSPI_QMR_MSTR);
477
Steven King34b8c662010-01-20 13:49:44 -0700478 mcfqspi_cs_teardown(mcfqspi);
Steven King34b8c662010-01-20 13:49:44 -0700479 clk_disable(mcfqspi->clk);
Steven King34b8c662010-01-20 13:49:44 -0700480
481 return 0;
482}
483
Steven Kingbc98d132012-05-10 09:26:55 -0700484#ifdef CONFIG_PM_SLEEP
Steven King34b8c662010-01-20 13:49:44 -0700485static int mcfqspi_suspend(struct device *dev)
486{
Guenter Roeckaf361072012-08-16 20:26:00 -0700487 struct spi_master *master = dev_get_drvdata(dev);
Steven Kingbc98d132012-05-10 09:26:55 -0700488 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
489
490 spi_master_suspend(master);
Steven King34b8c662010-01-20 13:49:44 -0700491
492 clk_disable(mcfqspi->clk);
493
494 return 0;
495}
496
497static int mcfqspi_resume(struct device *dev)
498{
Guenter Roeckaf361072012-08-16 20:26:00 -0700499 struct spi_master *master = dev_get_drvdata(dev);
Steven Kingbc98d132012-05-10 09:26:55 -0700500 struct mcfqspi *mcfqspi = spi_master_get_devdata(master);
501
502 spi_master_resume(master);
503
504 clk_enable(mcfqspi->clk);
505
506 return 0;
507}
508#endif
509
510#ifdef CONFIG_PM_RUNTIME
511static int mcfqspi_runtime_suspend(struct device *dev)
512{
Axel Lina12163942013-08-09 15:35:16 +0800513 struct mcfqspi *mcfqspi = dev_get_drvdata(dev);
Steven Kingbc98d132012-05-10 09:26:55 -0700514
515 clk_disable(mcfqspi->clk);
516
517 return 0;
518}
519
520static int mcfqspi_runtime_resume(struct device *dev)
521{
Axel Lina12163942013-08-09 15:35:16 +0800522 struct mcfqspi *mcfqspi = dev_get_drvdata(dev);
Steven King34b8c662010-01-20 13:49:44 -0700523
524 clk_enable(mcfqspi->clk);
525
526 return 0;
527}
Steven King34b8c662010-01-20 13:49:44 -0700528#endif
529
Steven Kingbc98d132012-05-10 09:26:55 -0700530static const struct dev_pm_ops mcfqspi_pm = {
531 SET_SYSTEM_SLEEP_PM_OPS(mcfqspi_suspend, mcfqspi_resume)
532 SET_RUNTIME_PM_OPS(mcfqspi_runtime_suspend, mcfqspi_runtime_resume,
533 NULL)
534};
535
Steven King34b8c662010-01-20 13:49:44 -0700536static struct platform_driver mcfqspi_driver = {
537 .driver.name = DRIVER_NAME,
538 .driver.owner = THIS_MODULE,
Steven Kingbc98d132012-05-10 09:26:55 -0700539 .driver.pm = &mcfqspi_pm,
Grant Likely940ab882011-10-05 11:29:49 -0600540 .probe = mcfqspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000541 .remove = mcfqspi_remove,
Steven King34b8c662010-01-20 13:49:44 -0700542};
Grant Likely940ab882011-10-05 11:29:49 -0600543module_platform_driver(mcfqspi_driver);
Steven King34b8c662010-01-20 13:49:44 -0700544
545MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
546MODULE_DESCRIPTION("Coldfire QSPI Controller Driver");
547MODULE_LICENSE("GPL");
548MODULE_ALIAS("platform:" DRIVER_NAME);