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 * 22nd Aug 2003 Initial version.
20 * 20th Dec 2004 Added ssp_config for changing port config without
22 * 4th Aug 2005 Added option to disable irq handler registration and
23 * cleaned up irq and clock detection.
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/ioport.h>
33 #include <linux/init.h>
34 #include <linux/mutex.h>
35 #include <linux/clk.h>
36 #include <linux/err.h>
37 #include <linux/platform_device.h>
41 #include <asm/hardware.h>
42 #include <asm/arch/ssp.h>
43 #include <asm/arch/pxa-regs.h>
44 #include <asm/arch/regs-ssp.h>
46 #define TIMEOUT 100000
48 static irqreturn_t
ssp_interrupt(int irq
, void *dev_id
)
50 struct ssp_dev
*dev
= dev_id
;
51 struct ssp_device
*ssp
= dev
->ssp
;
54 status
= __raw_readl(ssp
->mmio_base
+ SSSR
);
55 __raw_writel(status
, ssp
->mmio_base
+ SSSR
);
57 if (status
& SSSR_ROR
)
58 printk(KERN_WARNING
"SSP(%d): receiver overrun\n", dev
->port
);
60 if (status
& SSSR_TUR
)
61 printk(KERN_WARNING
"SSP(%d): transmitter underrun\n", dev
->port
);
63 if (status
& SSSR_BCE
)
64 printk(KERN_WARNING
"SSP(%d): bit count error\n", dev
->port
);
70 * ssp_write_word - write a word to the SSP port
71 * @data: 32-bit, MSB justified data to write.
73 * Wait for a free entry in the SSP transmit FIFO, and write a data
74 * word to the SSP port.
76 * The caller is expected to perform the necessary locking.
79 * %-ETIMEDOUT timeout occurred
82 int ssp_write_word(struct ssp_dev
*dev
, u32 data
)
84 struct ssp_device
*ssp
= dev
->ssp
;
85 int timeout
= TIMEOUT
;
87 while (!(__raw_readl(ssp
->mmio_base
+ SSSR
) & SSSR_TNF
)) {
93 __raw_writel(data
, ssp
->mmio_base
+ SSDR
);
99 * ssp_read_word - read a word from the SSP port
101 * Wait for a data word in the SSP receive FIFO, and return the
102 * received data. Data is LSB justified.
104 * Note: Currently, if data is not expected to be received, this
105 * function will wait for ever.
107 * The caller is expected to perform the necessary locking.
110 * %-ETIMEDOUT timeout occurred
111 * 32-bit data success
113 int ssp_read_word(struct ssp_dev
*dev
, u32
*data
)
115 struct ssp_device
*ssp
= dev
->ssp
;
116 int timeout
= TIMEOUT
;
118 while (!(__raw_readl(ssp
->mmio_base
+ SSSR
) & SSSR_RNE
)) {
124 *data
= __raw_readl(ssp
->mmio_base
+ SSDR
);
129 * ssp_flush - flush the transmit and receive FIFOs
131 * Wait for the SSP to idle, and ensure that the receive FIFO
134 * The caller is expected to perform the necessary locking.
136 int ssp_flush(struct ssp_dev
*dev
)
138 struct ssp_device
*ssp
= dev
->ssp
;
139 int timeout
= TIMEOUT
* 2;
141 /* ensure TX FIFO is empty instead of not full */
142 if (cpu_is_pxa3xx()) {
143 while (__raw_readl(ssp
->mmio_base
+ SSSR
) & 0xf00) {
148 timeout
= TIMEOUT
* 2;
152 while (__raw_readl(ssp
->mmio_base
+ SSSR
) & SSSR_RNE
) {
155 (void)__raw_readl(ssp
->mmio_base
+ SSDR
);
159 } while (__raw_readl(ssp
->mmio_base
+ SSSR
) & SSSR_BSY
);
165 * ssp_enable - enable the SSP port
167 * Turn on the SSP port.
169 void ssp_enable(struct ssp_dev
*dev
)
171 struct ssp_device
*ssp
= dev
->ssp
;
174 sscr0
= __raw_readl(ssp
->mmio_base
+ SSCR0
);
176 __raw_writel(sscr0
, ssp
->mmio_base
+ SSCR0
);
180 * ssp_disable - shut down the SSP port
182 * Turn off the SSP port, optionally powering it down.
184 void ssp_disable(struct ssp_dev
*dev
)
186 struct ssp_device
*ssp
= dev
->ssp
;
189 sscr0
= __raw_readl(ssp
->mmio_base
+ SSCR0
);
191 __raw_writel(sscr0
, ssp
->mmio_base
+ SSCR0
);
195 * ssp_save_state - save the SSP configuration
196 * @ssp: pointer to structure to save SSP configuration
198 * Save the configured SSP state for suspend.
200 void ssp_save_state(struct ssp_dev
*dev
, struct ssp_state
*state
)
202 struct ssp_device
*ssp
= dev
->ssp
;
204 state
->cr0
= __raw_readl(ssp
->mmio_base
+ SSCR0
);
205 state
->cr1
= __raw_readl(ssp
->mmio_base
+ SSCR1
);
206 state
->to
= __raw_readl(ssp
->mmio_base
+ SSTO
);
207 state
->psp
= __raw_readl(ssp
->mmio_base
+ SSPSP
);
213 * ssp_restore_state - restore a previously saved SSP configuration
214 * @ssp: pointer to configuration saved by ssp_save_state
216 * Restore the SSP configuration saved previously by ssp_save_state.
218 void ssp_restore_state(struct ssp_dev
*dev
, struct ssp_state
*state
)
220 struct ssp_device
*ssp
= dev
->ssp
;
221 uint32_t sssr
= SSSR_ROR
| SSSR_TUR
| SSSR_BCE
;
223 __raw_writel(sssr
, ssp
->mmio_base
+ SSSR
);
225 __raw_writel(state
->cr0
& ~SSCR0_SSE
, ssp
->mmio_base
+ SSCR0
);
226 __raw_writel(state
->cr1
, ssp
->mmio_base
+ SSCR1
);
227 __raw_writel(state
->to
, ssp
->mmio_base
+ SSTO
);
228 __raw_writel(state
->psp
, ssp
->mmio_base
+ SSPSP
);
229 __raw_writel(state
->cr0
, ssp
->mmio_base
+ SSCR0
);
233 * ssp_config - configure SSP port settings
234 * @mode: port operating mode
235 * @flags: port config flags
236 * @psp_flags: port PSP config flags
239 * Port MUST be disabled by ssp_disable before making any config changes.
241 int ssp_config(struct ssp_dev
*dev
, u32 mode
, u32 flags
, u32 psp_flags
, u32 speed
)
243 struct ssp_device
*ssp
= dev
->ssp
;
247 dev
->psp_flags
= psp_flags
;
250 /* set up port type, speed, port settings */
251 __raw_writel((dev
->speed
| dev
->mode
), ssp
->mmio_base
+ SSCR0
);
252 __raw_writel(dev
->flags
, ssp
->mmio_base
+ SSCR1
);
253 __raw_writel(dev
->psp_flags
, ssp
->mmio_base
+ SSPSP
);
259 * ssp_init - setup the SSP port
261 * initialise and claim resources for the SSP port.
264 * %-ENODEV if the SSP port is unavailable
265 * %-EBUSY if the resources are already in use
268 int ssp_init(struct ssp_dev
*dev
, u32 port
, u32 init_flags
)
270 struct ssp_device
*ssp
;
273 ssp
= ssp_request(port
, "SSP");
280 /* do we need to get irq */
281 if (!(init_flags
& SSP_NO_IRQ
)) {
282 ret
= request_irq(ssp
->irq
, ssp_interrupt
,
290 /* turn on SSP port clock */
291 clk_enable(ssp
->clk
);
300 * ssp_exit - undo the effects of ssp_init
302 * release and free resources for the SSP port.
304 void ssp_exit(struct ssp_dev
*dev
)
306 struct ssp_device
*ssp
= dev
->ssp
;
309 free_irq(dev
->irq
, dev
);
310 clk_disable(ssp
->clk
);
314 static DEFINE_MUTEX(ssp_lock
);
315 static LIST_HEAD(ssp_list
);
317 struct ssp_device
*ssp_request(int port
, const char *label
)
319 struct ssp_device
*ssp
= NULL
;
321 mutex_lock(&ssp_lock
);
323 list_for_each_entry(ssp
, &ssp_list
, node
) {
324 if (ssp
->port_id
== port
&& ssp
->use_count
== 0) {
331 mutex_unlock(&ssp_lock
);
333 if (ssp
->port_id
!= port
)
338 EXPORT_SYMBOL(ssp_request
);
340 void ssp_free(struct ssp_device
*ssp
)
342 mutex_lock(&ssp_lock
);
343 if (ssp
->use_count
) {
347 dev_err(&ssp
->pdev
->dev
, "device already free\n");
348 mutex_unlock(&ssp_lock
);
350 EXPORT_SYMBOL(ssp_free
);
352 static int __devinit
ssp_probe(struct platform_device
*pdev
, int type
)
354 struct resource
*res
;
355 struct ssp_device
*ssp
;
358 ssp
= kzalloc(sizeof(struct ssp_device
), GFP_KERNEL
);
360 dev_err(&pdev
->dev
, "failed to allocate memory");
364 ssp
->clk
= clk_get(&pdev
->dev
, "SSPCLK");
365 if (IS_ERR(ssp
->clk
)) {
366 ret
= PTR_ERR(ssp
->clk
);
370 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
372 dev_err(&pdev
->dev
, "no memory resource defined\n");
377 res
= request_mem_region(res
->start
, res
->end
- res
->start
+ 1,
380 dev_err(&pdev
->dev
, "failed to request memory resource\n");
385 ssp
->phys_base
= res
->start
;
387 ssp
->mmio_base
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
388 if (ssp
->mmio_base
== NULL
) {
389 dev_err(&pdev
->dev
, "failed to ioremap() registers\n");
394 ssp
->irq
= platform_get_irq(pdev
, 0);
396 dev_err(&pdev
->dev
, "no IRQ resource defined\n");
401 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
403 dev_err(&pdev
->dev
, "no SSP RX DRCMR defined\n");
407 ssp
->drcmr_rx
= res
->start
;
409 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
411 dev_err(&pdev
->dev
, "no SSP TX DRCMR defined\n");
415 ssp
->drcmr_tx
= res
->start
;
417 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
418 * starts from 0, do a translation here
420 ssp
->port_id
= pdev
->id
+ 1;
424 mutex_lock(&ssp_lock
);
425 list_add(&ssp
->node
, &ssp_list
);
426 mutex_unlock(&ssp_lock
);
428 platform_set_drvdata(pdev
, ssp
);
432 iounmap(ssp
->mmio_base
);
434 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
442 static int __devexit
ssp_remove(struct platform_device
*pdev
)
444 struct resource
*res
;
445 struct ssp_device
*ssp
;
447 ssp
= platform_get_drvdata(pdev
);
451 iounmap(ssp
->mmio_base
);
453 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
454 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
458 mutex_lock(&ssp_lock
);
459 list_del(&ssp
->node
);
460 mutex_unlock(&ssp_lock
);
466 static int __devinit
pxa25x_ssp_probe(struct platform_device
*pdev
)
468 return ssp_probe(pdev
, PXA25x_SSP
);
471 static int __devinit
pxa25x_nssp_probe(struct platform_device
*pdev
)
473 return ssp_probe(pdev
, PXA25x_NSSP
);
476 static int __devinit
pxa27x_ssp_probe(struct platform_device
*pdev
)
478 return ssp_probe(pdev
, PXA27x_SSP
);
481 static struct platform_driver pxa25x_ssp_driver
= {
483 .name
= "pxa25x-ssp",
485 .probe
= pxa25x_ssp_probe
,
486 .remove
= __devexit_p(ssp_remove
),
489 static struct platform_driver pxa25x_nssp_driver
= {
491 .name
= "pxa25x-nssp",
493 .probe
= pxa25x_nssp_probe
,
494 .remove
= __devexit_p(ssp_remove
),
497 static struct platform_driver pxa27x_ssp_driver
= {
499 .name
= "pxa27x-ssp",
501 .probe
= pxa27x_ssp_probe
,
502 .remove
= __devexit_p(ssp_remove
),
505 static int __init
pxa_ssp_init(void)
509 ret
= platform_driver_register(&pxa25x_ssp_driver
);
511 printk(KERN_ERR
"failed to register pxa25x_ssp_driver");
515 ret
= platform_driver_register(&pxa25x_nssp_driver
);
517 printk(KERN_ERR
"failed to register pxa25x_nssp_driver");
521 ret
= platform_driver_register(&pxa27x_ssp_driver
);
523 printk(KERN_ERR
"failed to register pxa27x_ssp_driver");
530 static void __exit
pxa_ssp_exit(void)
532 platform_driver_unregister(&pxa25x_ssp_driver
);
533 platform_driver_unregister(&pxa25x_nssp_driver
);
534 platform_driver_unregister(&pxa27x_ssp_driver
);
537 arch_initcall(pxa_ssp_init
);
538 module_exit(pxa_ssp_exit
);
540 EXPORT_SYMBOL(ssp_write_word
);
541 EXPORT_SYMBOL(ssp_read_word
);
542 EXPORT_SYMBOL(ssp_flush
);
543 EXPORT_SYMBOL(ssp_enable
);
544 EXPORT_SYMBOL(ssp_disable
);
545 EXPORT_SYMBOL(ssp_save_state
);
546 EXPORT_SYMBOL(ssp_restore_state
);
547 EXPORT_SYMBOL(ssp_init
);
548 EXPORT_SYMBOL(ssp_exit
);
549 EXPORT_SYMBOL(ssp_config
);
551 MODULE_DESCRIPTION("PXA SSP driver");
552 MODULE_AUTHOR("Liam Girdwood");
553 MODULE_LICENSE("GPL");