2 * linux/arch/arm/mach-pxa/ssp.c
4 * based on linux/arch/arm/mach-sa1100/ssp.c by Russell King
6 * Copyright (C) 2003 Russell King.
7 * Copyright (C) 2003 Wolfson Microelectronics PLC
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * PXA2xx SSP driver. This provides the generic core for simple
14 * IO-based SSP applications and allows easy port setup for DMA access.
16 * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <linux/clk.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
34 #include <asm/hardware.h>
35 #include <asm/arch/ssp.h>
36 #include <asm/arch/pxa-regs.h>
37 #include <asm/arch/regs-ssp.h>
39 #define TIMEOUT 100000
41 static irqreturn_t ssp_interrupt(int irq, void *dev_id)
43 struct ssp_dev *dev = dev_id;
44 struct ssp_device *ssp = dev->ssp;
47 status = __raw_readl(ssp->mmio_base + SSSR);
48 __raw_writel(status, ssp->mmio_base + SSSR);
50 if (status & SSSR_ROR)
51 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
53 if (status & SSSR_TUR)
54 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
56 if (status & SSSR_BCE)
57 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
63 * ssp_write_word - write a word to the SSP port
64 * @data: 32-bit, MSB justified data to write.
66 * Wait for a free entry in the SSP transmit FIFO, and write a data
67 * word to the SSP port.
69 * The caller is expected to perform the necessary locking.
72 * %-ETIMEDOUT timeout occurred
75 int ssp_write_word(struct ssp_dev *dev, u32 data)
77 struct ssp_device *ssp = dev->ssp;
78 int timeout = TIMEOUT;
80 while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_TNF)) {
86 __raw_writel(data, ssp->mmio_base + SSDR);
92 * ssp_read_word - read a word from the SSP port
94 * Wait for a data word in the SSP receive FIFO, and return the
95 * received data. Data is LSB justified.
97 * Note: Currently, if data is not expected to be received, this
98 * function will wait for ever.
100 * The caller is expected to perform the necessary locking.
103 * %-ETIMEDOUT timeout occurred
104 * 32-bit data success
106 int ssp_read_word(struct ssp_dev *dev, u32 *data)
108 struct ssp_device *ssp = dev->ssp;
109 int timeout = TIMEOUT;
111 while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE)) {
117 *data = __raw_readl(ssp->mmio_base + SSDR);
122 * ssp_flush - flush the transmit and receive FIFOs
124 * Wait for the SSP to idle, and ensure that the receive FIFO
127 * The caller is expected to perform the necessary locking.
129 int ssp_flush(struct ssp_dev *dev)
131 struct ssp_device *ssp = dev->ssp;
132 int timeout = TIMEOUT * 2;
134 /* ensure TX FIFO is empty instead of not full */
135 if (cpu_is_pxa3xx()) {
136 while (__raw_readl(ssp->mmio_base + SSSR) & 0xf00) {
141 timeout = TIMEOUT * 2;
145 while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE) {
148 (void)__raw_readl(ssp->mmio_base + SSDR);
152 } while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_BSY);
158 * ssp_enable - enable the SSP port
160 * Turn on the SSP port.
162 void ssp_enable(struct ssp_dev *dev)
164 struct ssp_device *ssp = dev->ssp;
167 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
169 __raw_writel(sscr0, ssp->mmio_base + SSCR0);
173 * ssp_disable - shut down the SSP port
175 * Turn off the SSP port, optionally powering it down.
177 void ssp_disable(struct ssp_dev *dev)
179 struct ssp_device *ssp = dev->ssp;
182 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
184 __raw_writel(sscr0, ssp->mmio_base + SSCR0);
188 * ssp_save_state - save the SSP configuration
189 * @ssp: pointer to structure to save SSP configuration
191 * Save the configured SSP state for suspend.
193 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *state)
195 struct ssp_device *ssp = dev->ssp;
197 state->cr0 = __raw_readl(ssp->mmio_base + SSCR0);
198 state->cr1 = __raw_readl(ssp->mmio_base + SSCR1);
199 state->to = __raw_readl(ssp->mmio_base + SSTO);
200 state->psp = __raw_readl(ssp->mmio_base + SSPSP);
206 * ssp_restore_state - restore a previously saved SSP configuration
207 * @ssp: pointer to configuration saved by ssp_save_state
209 * Restore the SSP configuration saved previously by ssp_save_state.
211 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *state)
213 struct ssp_device *ssp = dev->ssp;
214 uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
216 __raw_writel(sssr, ssp->mmio_base + SSSR);
218 __raw_writel(state->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0);
219 __raw_writel(state->cr1, ssp->mmio_base + SSCR1);
220 __raw_writel(state->to, ssp->mmio_base + SSTO);
221 __raw_writel(state->psp, ssp->mmio_base + SSPSP);
222 __raw_writel(state->cr0, ssp->mmio_base + SSCR0);
226 * ssp_config - configure SSP port settings
227 * @mode: port operating mode
228 * @flags: port config flags
229 * @psp_flags: port PSP config flags
232 * Port MUST be disabled by ssp_disable before making any config changes.
234 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
236 struct ssp_device *ssp = dev->ssp;
240 dev->psp_flags = psp_flags;
243 /* set up port type, speed, port settings */
244 __raw_writel((dev->speed | dev->mode), ssp->mmio_base + SSCR0);
245 __raw_writel(dev->flags, ssp->mmio_base + SSCR1);
246 __raw_writel(dev->psp_flags, ssp->mmio_base + SSPSP);
252 * ssp_init - setup the SSP port
254 * initialise and claim resources for the SSP port.
257 * %-ENODEV if the SSP port is unavailable
258 * %-EBUSY if the resources are already in use
261 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
263 struct ssp_device *ssp;
266 ssp = ssp_request(port, "SSP");
273 /* do we need to get irq */
274 if (!(init_flags & SSP_NO_IRQ)) {
275 ret = request_irq(ssp->irq, ssp_interrupt,
283 /* turn on SSP port clock */
284 clk_enable(ssp->clk);
293 * ssp_exit - undo the effects of ssp_init
295 * release and free resources for the SSP port.
297 void ssp_exit(struct ssp_dev *dev)
299 struct ssp_device *ssp = dev->ssp;
302 if (dev->irq != NO_IRQ)
303 free_irq(dev->irq, dev);
304 clk_disable(ssp->clk);
308 static DEFINE_MUTEX(ssp_lock);
309 static LIST_HEAD(ssp_list);
311 struct ssp_device *ssp_request(int port, const char *label)
313 struct ssp_device *ssp = NULL;
315 mutex_lock(&ssp_lock);
317 list_for_each_entry(ssp, &ssp_list, node) {
318 if (ssp->port_id == port && ssp->use_count == 0) {
325 mutex_unlock(&ssp_lock);
327 if (&ssp->node == &ssp_list)
332 EXPORT_SYMBOL(ssp_request);
334 void ssp_free(struct ssp_device *ssp)
336 mutex_lock(&ssp_lock);
337 if (ssp->use_count) {
341 dev_err(&ssp->pdev->dev, "device already free\n");
342 mutex_unlock(&ssp_lock);
344 EXPORT_SYMBOL(ssp_free);
346 static int __devinit ssp_probe(struct platform_device *pdev, int type)
348 struct resource *res;
349 struct ssp_device *ssp;
352 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
354 dev_err(&pdev->dev, "failed to allocate memory");
359 ssp->clk = clk_get(&pdev->dev, "SSPCLK");
360 if (IS_ERR(ssp->clk)) {
361 ret = PTR_ERR(ssp->clk);
365 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
367 dev_err(&pdev->dev, "no memory resource defined\n");
372 res = request_mem_region(res->start, res->end - res->start + 1,
375 dev_err(&pdev->dev, "failed to request memory resource\n");
380 ssp->phys_base = res->start;
382 ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
383 if (ssp->mmio_base == NULL) {
384 dev_err(&pdev->dev, "failed to ioremap() registers\n");
389 ssp->irq = platform_get_irq(pdev, 0);
391 dev_err(&pdev->dev, "no IRQ resource defined\n");
396 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
398 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
402 ssp->drcmr_rx = res->start;
404 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
406 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
410 ssp->drcmr_tx = res->start;
412 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
413 * starts from 0, do a translation here
415 ssp->port_id = pdev->id + 1;
419 mutex_lock(&ssp_lock);
420 list_add(&ssp->node, &ssp_list);
421 mutex_unlock(&ssp_lock);
423 platform_set_drvdata(pdev, ssp);
427 iounmap(ssp->mmio_base);
429 release_mem_region(res->start, res->end - res->start + 1);
437 static int __devexit ssp_remove(struct platform_device *pdev)
439 struct resource *res;
440 struct ssp_device *ssp;
442 ssp = platform_get_drvdata(pdev);
446 iounmap(ssp->mmio_base);
448 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 release_mem_region(res->start, res->end - res->start + 1);
453 mutex_lock(&ssp_lock);
454 list_del(&ssp->node);
455 mutex_unlock(&ssp_lock);
461 static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
463 return ssp_probe(pdev, PXA25x_SSP);
466 static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
468 return ssp_probe(pdev, PXA25x_NSSP);
471 static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
473 return ssp_probe(pdev, PXA27x_SSP);
476 static struct platform_driver pxa25x_ssp_driver = {
478 .name = "pxa25x-ssp",
480 .probe = pxa25x_ssp_probe,
481 .remove = __devexit_p(ssp_remove),
484 static struct platform_driver pxa25x_nssp_driver = {
486 .name = "pxa25x-nssp",
488 .probe = pxa25x_nssp_probe,
489 .remove = __devexit_p(ssp_remove),
492 static struct platform_driver pxa27x_ssp_driver = {
494 .name = "pxa27x-ssp",
496 .probe = pxa27x_ssp_probe,
497 .remove = __devexit_p(ssp_remove),
500 static int __init pxa_ssp_init(void)
504 ret = platform_driver_register(&pxa25x_ssp_driver);
506 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
510 ret = platform_driver_register(&pxa25x_nssp_driver);
512 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
516 ret = platform_driver_register(&pxa27x_ssp_driver);
518 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
525 static void __exit pxa_ssp_exit(void)
527 platform_driver_unregister(&pxa25x_ssp_driver);
528 platform_driver_unregister(&pxa25x_nssp_driver);
529 platform_driver_unregister(&pxa27x_ssp_driver);
532 arch_initcall(pxa_ssp_init);
533 module_exit(pxa_ssp_exit);
535 EXPORT_SYMBOL(ssp_write_word);
536 EXPORT_SYMBOL(ssp_read_word);
537 EXPORT_SYMBOL(ssp_flush);
538 EXPORT_SYMBOL(ssp_enable);
539 EXPORT_SYMBOL(ssp_disable);
540 EXPORT_SYMBOL(ssp_save_state);
541 EXPORT_SYMBOL(ssp_restore_state);
542 EXPORT_SYMBOL(ssp_init);
543 EXPORT_SYMBOL(ssp_exit);
544 EXPORT_SYMBOL(ssp_config);
546 MODULE_DESCRIPTION("PXA SSP driver");
547 MODULE_AUTHOR("Liam Girdwood");
548 MODULE_LICENSE("GPL");