blob: 53c7899a8aba39a728ad3c1088253c1b0be1aa2d [file] [log] [blame]
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +00001/*
2 * MPC512x PSC in SPI mode driver.
3 *
4 * Copyright (C) 2007,2008 Freescale Semiconductor Inc.
5 * Original port from 52xx driver:
6 * Hongjun Chen <hong-jun.chen@freescale.com>
7 *
8 * Fork of mpc52xx_psc_spi.c:
9 * Copyright (C) 2006 TOPTICA Photonics AG., Dragos Carp
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/interrupt.h>
Grant Likely22ae7822010-07-29 11:49:01 -060022#include <linux/of_address.h>
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +000023#include <linux/of_platform.h>
24#include <linux/workqueue.h>
25#include <linux/completion.h>
26#include <linux/io.h>
27#include <linux/delay.h>
28#include <linux/clk.h>
29#include <linux/spi/spi.h>
30#include <linux/fsl_devices.h>
Anatolij Gustschin86e98742013-04-01 17:29:21 +020031#include <linux/gpio.h>
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +000032#include <asm/mpc52xx_psc.h>
33
34struct mpc512x_psc_spi {
35 void (*cs_control)(struct spi_device *spi, bool on);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +000036
37 /* driver internal data */
38 struct mpc52xx_psc __iomem *psc;
39 struct mpc512x_psc_fifo __iomem *fifo;
40 unsigned int irq;
41 u8 bits_per_word;
42 u8 busy;
43 u32 mclk;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +000044
45 struct workqueue_struct *workqueue;
46 struct work_struct work;
47
48 struct list_head queue;
49 spinlock_t lock; /* Message queue lock */
50
Gerhard Sittigc36e93a2013-06-03 14:03:49 +020051 struct completion txisrdone;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +000052};
53
54/* controller state */
55struct mpc512x_psc_spi_cs {
56 int bits_per_word;
57 int speed_hz;
58};
59
60/* set clock freq, clock ramp, bits per work
61 * if t is NULL then reset the values to the default values
62 */
63static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi,
64 struct spi_transfer *t)
65{
66 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
67
68 cs->speed_hz = (t && t->speed_hz)
69 ? t->speed_hz : spi->max_speed_hz;
70 cs->bits_per_word = (t && t->bits_per_word)
71 ? t->bits_per_word : spi->bits_per_word;
72 cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
73 return 0;
74}
75
76static void mpc512x_psc_spi_activate_cs(struct spi_device *spi)
77{
78 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
79 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
80 struct mpc52xx_psc __iomem *psc = mps->psc;
81 u32 sicr;
82 u32 ccr;
83 u16 bclkdiv;
84
85 sicr = in_be32(&psc->sicr);
86
87 /* Set clock phase and polarity */
88 if (spi->mode & SPI_CPHA)
89 sicr |= 0x00001000;
90 else
91 sicr &= ~0x00001000;
92
93 if (spi->mode & SPI_CPOL)
94 sicr |= 0x00002000;
95 else
96 sicr &= ~0x00002000;
97
98 if (spi->mode & SPI_LSB_FIRST)
99 sicr |= 0x10000000;
100 else
101 sicr &= ~0x10000000;
102 out_be32(&psc->sicr, sicr);
103
104 ccr = in_be32(&psc->ccr);
105 ccr &= 0xFF000000;
106 if (cs->speed_hz)
107 bclkdiv = (mps->mclk / cs->speed_hz) - 1;
108 else
109 bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */
110
111 ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
112 out_be32(&psc->ccr, ccr);
113 mps->bits_per_word = cs->bits_per_word;
114
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200115 if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000116 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
117}
118
119static void mpc512x_psc_spi_deactivate_cs(struct spi_device *spi)
120{
121 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
122
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200123 if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000124 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
125
126}
127
128/* extract and scale size field in txsz or rxsz */
129#define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2);
130
131#define EOFBYTE 1
132
133static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi,
134 struct spi_transfer *t)
135{
136 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
137 struct mpc52xx_psc __iomem *psc = mps->psc;
138 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200139 size_t tx_len = t->len;
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200140 size_t rx_len = t->len;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000141 u8 *tx_buf = (u8 *)t->tx_buf;
142 u8 *rx_buf = (u8 *)t->rx_buf;
143
144 if (!tx_buf && !rx_buf && t->len)
145 return -EINVAL;
146
147 /* Zero MR2 */
148 in_8(&psc->mode);
149 out_8(&psc->mode, 0x0);
150
Anatolij Gustschinc3e2aa82013-04-01 17:31:19 +0200151 /* enable transmiter/receiver */
152 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
153
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200154 while (rx_len || tx_len) {
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200155 size_t txcount;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000156 u8 data;
157 size_t fifosz;
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200158 size_t rxcount;
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200159 int rxtries;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000160
161 /*
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200162 * send the TX bytes in as large a chunk as possible
163 * but neither exceed the TX nor the RX FIFOs
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000164 */
165 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200166 txcount = min(fifosz, tx_len);
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200167 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->rxsz));
168 fifosz -= in_be32(&fifo->rxcnt) + 1;
169 txcount = min(fifosz, txcount);
170 if (txcount) {
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000171
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200172 /* fill the TX FIFO */
173 while (txcount-- > 0) {
174 data = tx_buf ? *tx_buf++ : 0;
175 if (tx_len == EOFBYTE && t->cs_change)
176 setbits32(&fifo->txcmd,
177 MPC512x_PSC_FIFO_EOF);
178 out_8(&fifo->txdata_8, data);
179 tx_len--;
180 }
181
182 /* have the ISR trigger when the TX FIFO is empty */
183 INIT_COMPLETION(mps->txisrdone);
184 out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
185 out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
186 wait_for_completion(&mps->txisrdone);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000187 }
188
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200189 /*
190 * consume as much RX data as the FIFO holds, while we
191 * iterate over the transfer's TX data length
192 *
193 * only insist in draining all the remaining RX bytes
194 * when the TX bytes were exhausted (that's at the very
195 * end of this transfer, not when still iterating over
196 * the transfer's chunks)
197 */
198 rxtries = 50;
199 do {
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000200
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200201 /*
202 * grab whatever was in the FIFO when we started
203 * looking, don't bother fetching what was added to
204 * the FIFO while we read from it -- we'll return
205 * here eventually and prefer sending out remaining
206 * TX data
207 */
208 fifosz = in_be32(&fifo->rxcnt);
209 rxcount = min(fifosz, rx_len);
210 while (rxcount-- > 0) {
211 data = in_8(&fifo->rxdata_8);
212 if (rx_buf)
213 *rx_buf++ = data;
214 rx_len--;
215 }
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000216
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200217 /*
218 * come back later if there still is TX data to send,
219 * bail out of the RX drain loop if all of the TX data
220 * was sent and all of the RX data was received (i.e.
221 * when the transmission has completed)
222 */
223 if (tx_len)
224 break;
225 if (!rx_len)
226 break;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000227
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200228 /*
229 * TX data transmission has completed while RX data
230 * is still pending -- that's a transient situation
231 * which depends on wire speed and specific
232 * hardware implementation details (buffering) yet
233 * should resolve very quickly
234 *
235 * just yield for a moment to not hog the CPU for
236 * too long when running SPI at low speed
237 *
238 * the timeout range is rather arbitrary and tries
239 * to balance throughput against system load; the
240 * chosen values result in a minimal timeout of 50
241 * times 10us and thus work at speeds as low as
242 * some 20kbps, while the maximum timeout at the
243 * transfer's end could be 5ms _if_ nothing else
244 * ticks in the system _and_ RX data still wasn't
245 * received, which only occurs in situations that
246 * are exceptional; removing the unpredictability
247 * of the timeout either decreases throughput
248 * (longer timeouts), or puts more load on the
249 * system (fixed short timeouts) or requires the
250 * use of a timeout API instead of a counter and an
251 * unknown inner delay
252 */
253 usleep_range(10, 100);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000254
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200255 } while (--rxtries > 0);
256 if (!tx_len && rx_len && !rxtries) {
257 /*
258 * not enough RX bytes even after several retries
259 * and the resulting rather long timeout?
260 */
261 rxcount = in_be32(&fifo->rxcnt);
262 dev_warn(&spi->dev,
263 "short xfer, missing %zd RX bytes, FIFO level %zd\n",
264 rx_len, rxcount);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000265 }
266
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200267 /*
268 * drain and drop RX data which "should not be there" in
269 * the first place, for undisturbed transmission this turns
270 * into a NOP (except for the FIFO level fetch)
271 */
272 if (!tx_len && !rx_len) {
273 while (in_be32(&fifo->rxcnt))
274 in_8(&fifo->rxdata_8);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000275 }
Gerhard Sittig5df24ea2013-06-03 14:03:50 +0200276
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000277 }
278 /* disable transmiter/receiver and fifo interrupt */
279 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
280 out_be32(&fifo->tximr, 0);
281 return 0;
282}
283
284static void mpc512x_psc_spi_work(struct work_struct *work)
285{
286 struct mpc512x_psc_spi *mps = container_of(work,
287 struct mpc512x_psc_spi,
288 work);
289
290 spin_lock_irq(&mps->lock);
291 mps->busy = 1;
292 while (!list_empty(&mps->queue)) {
293 struct spi_message *m;
294 struct spi_device *spi;
295 struct spi_transfer *t = NULL;
296 unsigned cs_change;
297 int status;
298
299 m = container_of(mps->queue.next, struct spi_message, queue);
300 list_del_init(&m->queue);
301 spin_unlock_irq(&mps->lock);
302
303 spi = m->spi;
304 cs_change = 1;
305 status = 0;
306 list_for_each_entry(t, &m->transfers, transfer_list) {
307 if (t->bits_per_word || t->speed_hz) {
308 status = mpc512x_psc_spi_transfer_setup(spi, t);
309 if (status < 0)
310 break;
311 }
312
313 if (cs_change)
314 mpc512x_psc_spi_activate_cs(spi);
315 cs_change = t->cs_change;
316
317 status = mpc512x_psc_spi_transfer_rxtx(spi, t);
318 if (status)
319 break;
320 m->actual_length += t->len;
321
322 if (t->delay_usecs)
323 udelay(t->delay_usecs);
324
325 if (cs_change)
326 mpc512x_psc_spi_deactivate_cs(spi);
327 }
328
329 m->status = status;
330 m->complete(m->context);
331
332 if (status || !cs_change)
333 mpc512x_psc_spi_deactivate_cs(spi);
334
335 mpc512x_psc_spi_transfer_setup(spi, NULL);
336
337 spin_lock_irq(&mps->lock);
338 }
339 mps->busy = 0;
340 spin_unlock_irq(&mps->lock);
341}
342
343static int mpc512x_psc_spi_setup(struct spi_device *spi)
344{
345 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
346 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
347 unsigned long flags;
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200348 int ret;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000349
350 if (spi->bits_per_word % 8)
351 return -EINVAL;
352
353 if (!cs) {
354 cs = kzalloc(sizeof *cs, GFP_KERNEL);
355 if (!cs)
356 return -ENOMEM;
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200357
358 if (gpio_is_valid(spi->cs_gpio)) {
359 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
360 if (ret) {
361 dev_err(&spi->dev, "can't get CS gpio: %d\n",
362 ret);
363 kfree(cs);
364 return ret;
365 }
366 gpio_direction_output(spi->cs_gpio,
367 spi->mode & SPI_CS_HIGH ? 0 : 1);
368 }
369
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000370 spi->controller_state = cs;
371 }
372
373 cs->bits_per_word = spi->bits_per_word;
374 cs->speed_hz = spi->max_speed_hz;
375
376 spin_lock_irqsave(&mps->lock, flags);
377 if (!mps->busy)
378 mpc512x_psc_spi_deactivate_cs(spi);
379 spin_unlock_irqrestore(&mps->lock, flags);
380
381 return 0;
382}
383
384static int mpc512x_psc_spi_transfer(struct spi_device *spi,
385 struct spi_message *m)
386{
387 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
388 unsigned long flags;
389
390 m->actual_length = 0;
391 m->status = -EINPROGRESS;
392
393 spin_lock_irqsave(&mps->lock, flags);
394 list_add_tail(&m->queue, &mps->queue);
395 queue_work(mps->workqueue, &mps->work);
396 spin_unlock_irqrestore(&mps->lock, flags);
397
398 return 0;
399}
400
401static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
402{
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200403 if (gpio_is_valid(spi->cs_gpio))
404 gpio_free(spi->cs_gpio);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000405 kfree(spi->controller_state);
406}
407
408static int mpc512x_psc_spi_port_config(struct spi_master *master,
409 struct mpc512x_psc_spi *mps)
410{
411 struct mpc52xx_psc __iomem *psc = mps->psc;
412 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
413 struct clk *spiclk;
414 int ret = 0;
415 char name[32];
416 u32 sicr;
417 u32 ccr;
418 u16 bclkdiv;
419
420 sprintf(name, "psc%d_mclk", master->bus_num);
421 spiclk = clk_get(&master->dev, name);
422 clk_enable(spiclk);
423 mps->mclk = clk_get_rate(spiclk);
424 clk_put(spiclk);
425
426 /* Reset the PSC into a known state */
427 out_8(&psc->command, MPC52xx_PSC_RST_RX);
428 out_8(&psc->command, MPC52xx_PSC_RST_TX);
429 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
430
431 /* Disable psc interrupts all useful interrupts are in fifo */
432 out_be16(&psc->isr_imr.imr, 0);
433
434 /* Disable fifo interrupts, will be enabled later */
435 out_be32(&fifo->tximr, 0);
436 out_be32(&fifo->rximr, 0);
437
438 /* Setup fifo slice address and size */
439 /*out_be32(&fifo->txsz, 0x0fe00004);*/
440 /*out_be32(&fifo->rxsz, 0x0ff00004);*/
441
442 sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */
443 0x00800000 | /* GenClk = 1 -- internal clk */
444 0x00008000 | /* SPI = 1 */
445 0x00004000 | /* MSTR = 1 -- SPI master */
446 0x00000800; /* UseEOF = 1 -- SS low until EOF */
447
448 out_be32(&psc->sicr, sicr);
449
450 ccr = in_be32(&psc->ccr);
451 ccr &= 0xFF000000;
452 bclkdiv = (mps->mclk / 1000000) - 1; /* default 1MHz */
453 ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
454 out_be32(&psc->ccr, ccr);
455
456 /* Set 2ms DTL delay */
457 out_8(&psc->ctur, 0x00);
458 out_8(&psc->ctlr, 0x82);
459
460 /* we don't use the alarms */
461 out_be32(&fifo->rxalarm, 0xfff);
462 out_be32(&fifo->txalarm, 0);
463
464 /* Enable FIFO slices for Rx/Tx */
465 out_be32(&fifo->rxcmd,
466 MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
467 out_be32(&fifo->txcmd,
468 MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
469
470 mps->bits_per_word = 8;
471
472 return ret;
473}
474
475static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
476{
477 struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
478 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
479
480 /* clear interrupt and wake up the work queue */
481 if (in_be32(&fifo->txisr) &
482 in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
483 out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
484 out_be32(&fifo->tximr, 0);
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200485 complete(&mps->txisrdone);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000486 return IRQ_HANDLED;
487 }
488 return IRQ_NONE;
489}
490
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200491static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff)
492{
493 gpio_set_value(spi->cs_gpio, onoff);
494}
495
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000496/* bus_num is used only for the case dev->platform_data == NULL */
Grant Likelyfd4a3192012-12-07 16:57:14 +0000497static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
Anatolij Gustschincf40f082010-07-05 12:17:51 +0200498 u32 size, unsigned int irq,
499 s16 bus_num)
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000500{
501 struct fsl_spi_platform_data *pdata = dev->platform_data;
502 struct mpc512x_psc_spi *mps;
503 struct spi_master *master;
504 int ret;
505 void *tempp;
506
507 master = spi_alloc_master(dev, sizeof *mps);
508 if (master == NULL)
509 return -ENOMEM;
510
511 dev_set_drvdata(dev, master);
512 mps = spi_master_get_devdata(master);
513 mps->irq = irq;
514
515 if (pdata == NULL) {
Anatolij Gustschin86e98742013-04-01 17:29:21 +0200516 mps->cs_control = mpc512x_spi_cs_control;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000517 master->bus_num = bus_num;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000518 } else {
519 mps->cs_control = pdata->cs_control;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000520 master->bus_num = pdata->bus_num;
521 master->num_chipselect = pdata->max_chipselect;
522 }
523
Anatolij Gustschinc88dd342013-01-14 21:27:00 +0100524 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000525 master->setup = mpc512x_psc_spi_setup;
526 master->transfer = mpc512x_psc_spi_transfer;
527 master->cleanup = mpc512x_psc_spi_cleanup;
Anatolij Gustschin12b15e82010-07-27 22:35:58 +0200528 master->dev.of_node = dev->of_node;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000529
530 tempp = ioremap(regaddr, size);
531 if (!tempp) {
532 dev_err(dev, "could not ioremap I/O port range\n");
533 ret = -EFAULT;
534 goto free_master;
535 }
536 mps->psc = tempp;
537 mps->fifo =
538 (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
539
540 ret = request_irq(mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
541 "mpc512x-psc-spi", mps);
542 if (ret)
543 goto free_master;
544
545 ret = mpc512x_psc_spi_port_config(master, mps);
546 if (ret < 0)
547 goto free_irq;
548
549 spin_lock_init(&mps->lock);
Gerhard Sittigc36e93a2013-06-03 14:03:49 +0200550 init_completion(&mps->txisrdone);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000551 INIT_WORK(&mps->work, mpc512x_psc_spi_work);
552 INIT_LIST_HEAD(&mps->queue);
553
554 mps->workqueue =
555 create_singlethread_workqueue(dev_name(master->dev.parent));
556 if (mps->workqueue == NULL) {
557 ret = -EBUSY;
558 goto free_irq;
559 }
560
561 ret = spi_register_master(master);
562 if (ret < 0)
563 goto unreg_master;
564
565 return ret;
566
567unreg_master:
568 destroy_workqueue(mps->workqueue);
569free_irq:
570 free_irq(mps->irq, mps);
571free_master:
572 if (mps->psc)
573 iounmap(mps->psc);
574 spi_master_put(master);
575
576 return ret;
577}
578
Grant Likelyfd4a3192012-12-07 16:57:14 +0000579static int mpc512x_psc_spi_do_remove(struct device *dev)
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000580{
Guenter Roeck21879212012-08-18 09:29:24 -0700581 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000582 struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
583
584 flush_workqueue(mps->workqueue);
585 destroy_workqueue(mps->workqueue);
586 spi_unregister_master(master);
587 free_irq(mps->irq, mps);
588 if (mps->psc)
589 iounmap(mps->psc);
Guenter Roeck21879212012-08-18 09:29:24 -0700590 spi_master_put(master);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000591
592 return 0;
593}
594
Grant Likelyfd4a3192012-12-07 16:57:14 +0000595static int mpc512x_psc_spi_of_probe(struct platform_device *op)
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000596{
597 const u32 *regaddr_p;
598 u64 regaddr64, size64;
599 s16 id = -1;
600
Anatolij Gustschinef7f2e82010-05-31 18:34:54 +0200601 regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000602 if (!regaddr_p) {
603 dev_err(&op->dev, "Invalid PSC address\n");
604 return -EINVAL;
605 }
Anatolij Gustschinef7f2e82010-05-31 18:34:54 +0200606 regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000607
608 /* get PSC id (0..11, used by port_config) */
Anatolij Gustschin9d15a3b2013-01-11 01:05:48 +0100609 id = of_alias_get_id(op->dev.of_node, "spi");
610 if (id < 0) {
611 dev_err(&op->dev, "no alias id for %s\n",
612 op->dev.of_node->full_name);
613 return id;
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000614 }
615
616 return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
Anatolij Gustschinef7f2e82010-05-31 18:34:54 +0200617 irq_of_parse_and_map(op->dev.of_node, 0), id);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000618}
619
Grant Likelyfd4a3192012-12-07 16:57:14 +0000620static int mpc512x_psc_spi_of_remove(struct platform_device *op)
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000621{
622 return mpc512x_psc_spi_do_remove(&op->dev);
623}
624
625static struct of_device_id mpc512x_psc_spi_of_match[] = {
626 { .compatible = "fsl,mpc5121-psc-spi", },
627 {},
628};
629
630MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
631
Grant Likely18d306d2011-02-22 21:02:43 -0700632static struct platform_driver mpc512x_psc_spi_of_driver = {
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000633 .probe = mpc512x_psc_spi_of_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000634 .remove = mpc512x_psc_spi_of_remove,
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000635 .driver = {
636 .name = "mpc512x-psc-spi",
637 .owner = THIS_MODULE,
Anatolij Gustschinef7f2e82010-05-31 18:34:54 +0200638 .of_match_table = mpc512x_psc_spi_of_match,
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000639 },
640};
Grant Likely940ab882011-10-05 11:29:49 -0600641module_platform_driver(mpc512x_psc_spi_of_driver);
Anatolij Gustschin6e27388f1b2010-04-30 13:21:27 +0000642
643MODULE_AUTHOR("John Rigby");
644MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
645MODULE_LICENSE("GPL");