diff options
Diffstat (limited to 'drivers/spi/spi-mpc512x-psc.c')
-rw-r--r-- | drivers/spi/spi-mpc512x-psc.c | 183 |
1 files changed, 73 insertions, 110 deletions
diff --git a/drivers/spi/spi-mpc512x-psc.c b/drivers/spi/spi-mpc512x-psc.c index 53c7899a8aba..29fce6af5145 100644 --- a/drivers/spi/spi-mpc512x-psc.c +++ b/drivers/spi/spi-mpc512x-psc.c | |||
@@ -21,7 +21,6 @@ | |||
21 | #include <linux/interrupt.h> | 21 | #include <linux/interrupt.h> |
22 | #include <linux/of_address.h> | 22 | #include <linux/of_address.h> |
23 | #include <linux/of_platform.h> | 23 | #include <linux/of_platform.h> |
24 | #include <linux/workqueue.h> | ||
25 | #include <linux/completion.h> | 24 | #include <linux/completion.h> |
26 | #include <linux/io.h> | 25 | #include <linux/io.h> |
27 | #include <linux/delay.h> | 26 | #include <linux/delay.h> |
@@ -39,15 +38,8 @@ struct mpc512x_psc_spi { | |||
39 | struct mpc512x_psc_fifo __iomem *fifo; | 38 | struct mpc512x_psc_fifo __iomem *fifo; |
40 | unsigned int irq; | 39 | unsigned int irq; |
41 | u8 bits_per_word; | 40 | u8 bits_per_word; |
42 | u8 busy; | ||
43 | u32 mclk; | 41 | u32 mclk; |
44 | 42 | ||
45 | struct workqueue_struct *workqueue; | ||
46 | struct work_struct work; | ||
47 | |||
48 | struct list_head queue; | ||
49 | spinlock_t lock; /* Message queue lock */ | ||
50 | |||
51 | struct completion txisrdone; | 43 | struct completion txisrdone; |
52 | }; | 44 | }; |
53 | 45 | ||
@@ -134,7 +126,6 @@ static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi, | |||
134 | struct spi_transfer *t) | 126 | struct spi_transfer *t) |
135 | { | 127 | { |
136 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); | 128 | 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; | 129 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; |
139 | size_t tx_len = t->len; | 130 | size_t tx_len = t->len; |
140 | size_t rx_len = t->len; | 131 | size_t rx_len = t->len; |
@@ -144,13 +135,6 @@ static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi, | |||
144 | if (!tx_buf && !rx_buf && t->len) | 135 | if (!tx_buf && !rx_buf && t->len) |
145 | return -EINVAL; | 136 | return -EINVAL; |
146 | 137 | ||
147 | /* Zero MR2 */ | ||
148 | in_8(&psc->mode); | ||
149 | out_8(&psc->mode, 0x0); | ||
150 | |||
151 | /* enable transmiter/receiver */ | ||
152 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE); | ||
153 | |||
154 | while (rx_len || tx_len) { | 138 | while (rx_len || tx_len) { |
155 | size_t txcount; | 139 | size_t txcount; |
156 | u8 data; | 140 | u8 data; |
@@ -275,76 +259,90 @@ static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi, | |||
275 | } | 259 | } |
276 | 260 | ||
277 | } | 261 | } |
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; | 262 | return 0; |
282 | } | 263 | } |
283 | 264 | ||
284 | static void mpc512x_psc_spi_work(struct work_struct *work) | 265 | static int mpc512x_psc_spi_msg_xfer(struct spi_master *master, |
266 | struct spi_message *m) | ||
285 | { | 267 | { |
286 | struct mpc512x_psc_spi *mps = container_of(work, | 268 | struct spi_device *spi; |
287 | struct mpc512x_psc_spi, | 269 | unsigned cs_change; |
288 | work); | 270 | int status; |
289 | 271 | struct spi_transfer *t; | |
290 | spin_lock_irq(&mps->lock); | 272 | |
291 | mps->busy = 1; | 273 | spi = m->spi; |
292 | while (!list_empty(&mps->queue)) { | 274 | cs_change = 1; |
293 | struct spi_message *m; | 275 | status = 0; |
294 | struct spi_device *spi; | 276 | list_for_each_entry(t, &m->transfers, transfer_list) { |
295 | struct spi_transfer *t = NULL; | 277 | if (t->bits_per_word || t->speed_hz) { |
296 | unsigned cs_change; | 278 | status = mpc512x_psc_spi_transfer_setup(spi, t); |
297 | int status; | 279 | if (status < 0) |
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; | 280 | break; |
320 | m->actual_length += t->len; | 281 | } |
321 | 282 | ||
322 | if (t->delay_usecs) | 283 | if (cs_change) |
323 | udelay(t->delay_usecs); | 284 | mpc512x_psc_spi_activate_cs(spi); |
285 | cs_change = t->cs_change; | ||
324 | 286 | ||
325 | if (cs_change) | 287 | status = mpc512x_psc_spi_transfer_rxtx(spi, t); |
326 | mpc512x_psc_spi_deactivate_cs(spi); | 288 | if (status) |
327 | } | 289 | break; |
290 | m->actual_length += t->len; | ||
328 | 291 | ||
329 | m->status = status; | 292 | if (t->delay_usecs) |
330 | m->complete(m->context); | 293 | udelay(t->delay_usecs); |
331 | 294 | ||
332 | if (status || !cs_change) | 295 | if (cs_change) |
333 | mpc512x_psc_spi_deactivate_cs(spi); | 296 | mpc512x_psc_spi_deactivate_cs(spi); |
297 | } | ||
334 | 298 | ||
335 | mpc512x_psc_spi_transfer_setup(spi, NULL); | 299 | m->status = status; |
300 | m->complete(m->context); | ||
336 | 301 | ||
337 | spin_lock_irq(&mps->lock); | 302 | if (status || !cs_change) |
338 | } | 303 | mpc512x_psc_spi_deactivate_cs(spi); |
339 | mps->busy = 0; | 304 | |
340 | spin_unlock_irq(&mps->lock); | 305 | mpc512x_psc_spi_transfer_setup(spi, NULL); |
306 | |||
307 | spi_finalize_current_message(master); | ||
308 | return status; | ||
309 | } | ||
310 | |||
311 | static int mpc512x_psc_spi_prep_xfer_hw(struct spi_master *master) | ||
312 | { | ||
313 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(master); | ||
314 | struct mpc52xx_psc __iomem *psc = mps->psc; | ||
315 | |||
316 | dev_dbg(&master->dev, "%s()\n", __func__); | ||
317 | |||
318 | /* Zero MR2 */ | ||
319 | in_8(&psc->mode); | ||
320 | out_8(&psc->mode, 0x0); | ||
321 | |||
322 | /* enable transmitter/receiver */ | ||
323 | out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE); | ||
324 | |||
325 | return 0; | ||
326 | } | ||
327 | |||
328 | static int mpc512x_psc_spi_unprep_xfer_hw(struct spi_master *master) | ||
329 | { | ||
330 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(master); | ||
331 | struct mpc52xx_psc __iomem *psc = mps->psc; | ||
332 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; | ||
333 | |||
334 | dev_dbg(&master->dev, "%s()\n", __func__); | ||
335 | |||
336 | /* disable transmitter/receiver and fifo interrupt */ | ||
337 | out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); | ||
338 | out_be32(&fifo->tximr, 0); | ||
339 | |||
340 | return 0; | ||
341 | } | 341 | } |
342 | 342 | ||
343 | static int mpc512x_psc_spi_setup(struct spi_device *spi) | 343 | static int mpc512x_psc_spi_setup(struct spi_device *spi) |
344 | { | 344 | { |
345 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master); | ||
346 | struct mpc512x_psc_spi_cs *cs = spi->controller_state; | 345 | struct mpc512x_psc_spi_cs *cs = spi->controller_state; |
347 | unsigned long flags; | ||
348 | int ret; | 346 | int ret; |
349 | 347 | ||
350 | if (spi->bits_per_word % 8) | 348 | if (spi->bits_per_word % 8) |
@@ -373,28 +371,6 @@ static int mpc512x_psc_spi_setup(struct spi_device *spi) | |||
373 | cs->bits_per_word = spi->bits_per_word; | 371 | cs->bits_per_word = spi->bits_per_word; |
374 | cs->speed_hz = spi->max_speed_hz; | 372 | cs->speed_hz = spi->max_speed_hz; |
375 | 373 | ||
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 | |||
384 | static 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; | 374 | return 0; |
399 | } | 375 | } |
400 | 376 | ||
@@ -477,7 +453,7 @@ static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id) | |||
477 | struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id; | 453 | struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id; |
478 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; | 454 | struct mpc512x_psc_fifo __iomem *fifo = mps->fifo; |
479 | 455 | ||
480 | /* clear interrupt and wake up the work queue */ | 456 | /* clear interrupt and wake up the rx/tx routine */ |
481 | if (in_be32(&fifo->txisr) & | 457 | if (in_be32(&fifo->txisr) & |
482 | in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) { | 458 | in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) { |
483 | out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY); | 459 | out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY); |
@@ -523,7 +499,9 @@ static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr, | |||
523 | 499 | ||
524 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; | 500 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; |
525 | master->setup = mpc512x_psc_spi_setup; | 501 | master->setup = mpc512x_psc_spi_setup; |
526 | master->transfer = mpc512x_psc_spi_transfer; | 502 | master->prepare_transfer_hardware = mpc512x_psc_spi_prep_xfer_hw; |
503 | master->transfer_one_message = mpc512x_psc_spi_msg_xfer; | ||
504 | master->unprepare_transfer_hardware = mpc512x_psc_spi_unprep_xfer_hw; | ||
527 | master->cleanup = mpc512x_psc_spi_cleanup; | 505 | master->cleanup = mpc512x_psc_spi_cleanup; |
528 | master->dev.of_node = dev->of_node; | 506 | master->dev.of_node = dev->of_node; |
529 | 507 | ||
@@ -541,31 +519,18 @@ static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr, | |||
541 | "mpc512x-psc-spi", mps); | 519 | "mpc512x-psc-spi", mps); |
542 | if (ret) | 520 | if (ret) |
543 | goto free_master; | 521 | goto free_master; |
522 | init_completion(&mps->txisrdone); | ||
544 | 523 | ||
545 | ret = mpc512x_psc_spi_port_config(master, mps); | 524 | ret = mpc512x_psc_spi_port_config(master, mps); |
546 | if (ret < 0) | 525 | if (ret < 0) |
547 | goto free_irq; | 526 | goto free_irq; |
548 | 527 | ||
549 | spin_lock_init(&mps->lock); | ||
550 | init_completion(&mps->txisrdone); | ||
551 | 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); | 528 | ret = spi_register_master(master); |
562 | if (ret < 0) | 529 | if (ret < 0) |
563 | goto unreg_master; | 530 | goto free_irq; |
564 | 531 | ||
565 | return ret; | 532 | return ret; |
566 | 533 | ||
567 | unreg_master: | ||
568 | destroy_workqueue(mps->workqueue); | ||
569 | free_irq: | 534 | free_irq: |
570 | free_irq(mps->irq, mps); | 535 | free_irq(mps->irq, mps); |
571 | free_master: | 536 | free_master: |
@@ -581,8 +546,6 @@ static int mpc512x_psc_spi_do_remove(struct device *dev) | |||
581 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); | 546 | struct spi_master *master = spi_master_get(dev_get_drvdata(dev)); |
582 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(master); | 547 | struct mpc512x_psc_spi *mps = spi_master_get_devdata(master); |
583 | 548 | ||
584 | flush_workqueue(mps->workqueue); | ||
585 | destroy_workqueue(mps->workqueue); | ||
586 | spi_unregister_master(master); | 549 | spi_unregister_master(master); |
587 | free_irq(mps->irq, mps); | 550 | free_irq(mps->irq, mps); |
588 | if (mps->psc) | 551 | if (mps->psc) |