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>
37 #include <asm/hardware.h>
38 #include <asm/arch/ssp.h>
39 #include <asm/arch/pxa-regs.h>
41 #define PXA_SSP_PORTS 3
43 #define TIMEOUT 100000
51 * SSP port clock and IRQ settings
53 static const struct ssp_info_ ssp_info
[PXA_SSP_PORTS
] = {
54 #if defined (CONFIG_PXA27x)
56 {IRQ_SSP2
, CKEN_SSP2
},
57 {IRQ_SSP3
, CKEN_SSP3
},
60 {IRQ_NSSP
, CKEN_NSSP
},
61 {IRQ_ASSP
, CKEN_ASSP
},
65 static DEFINE_MUTEX(mutex
);
66 static int use_count
[PXA_SSP_PORTS
] = {0, 0, 0};
68 static irqreturn_t
ssp_interrupt(int irq
, void *dev_id
)
70 struct ssp_dev
*dev
= (struct ssp_dev
*) dev_id
;
71 unsigned int status
= SSSR_P(dev
->port
);
73 SSSR_P(dev
->port
) = status
; /* clear status bits */
75 if (status
& SSSR_ROR
)
76 printk(KERN_WARNING
"SSP(%d): receiver overrun\n", dev
->port
);
78 if (status
& SSSR_TUR
)
79 printk(KERN_WARNING
"SSP(%d): transmitter underrun\n", dev
->port
);
81 if (status
& SSSR_BCE
)
82 printk(KERN_WARNING
"SSP(%d): bit count error\n", dev
->port
);
88 * ssp_write_word - write a word to the SSP port
89 * @data: 32-bit, MSB justified data to write.
91 * Wait for a free entry in the SSP transmit FIFO, and write a data
92 * word to the SSP port.
94 * The caller is expected to perform the necessary locking.
97 * %-ETIMEDOUT timeout occurred
100 int ssp_write_word(struct ssp_dev
*dev
, u32 data
)
102 int timeout
= TIMEOUT
;
104 while (!(SSSR_P(dev
->port
) & SSSR_TNF
)) {
110 SSDR_P(dev
->port
) = data
;
116 * ssp_read_word - read a word from the SSP port
118 * Wait for a data word in the SSP receive FIFO, and return the
119 * received data. Data is LSB justified.
121 * Note: Currently, if data is not expected to be received, this
122 * function will wait for ever.
124 * The caller is expected to perform the necessary locking.
127 * %-ETIMEDOUT timeout occurred
128 * 32-bit data success
130 int ssp_read_word(struct ssp_dev
*dev
, u32
*data
)
132 int timeout
= TIMEOUT
;
134 while (!(SSSR_P(dev
->port
) & SSSR_RNE
)) {
140 *data
= SSDR_P(dev
->port
);
145 * ssp_flush - flush the transmit and receive FIFOs
147 * Wait for the SSP to idle, and ensure that the receive FIFO
150 * The caller is expected to perform the necessary locking.
152 int ssp_flush(struct ssp_dev
*dev
)
154 int timeout
= TIMEOUT
* 2;
157 while (SSSR_P(dev
->port
) & SSSR_RNE
) {
160 (void) SSDR_P(dev
->port
);
164 } while (SSSR_P(dev
->port
) & SSSR_BSY
);
170 * ssp_enable - enable the SSP port
172 * Turn on the SSP port.
174 void ssp_enable(struct ssp_dev
*dev
)
176 SSCR0_P(dev
->port
) |= SSCR0_SSE
;
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 SSCR0_P(dev
->port
) &= ~SSCR0_SSE
;
190 * ssp_save_state - save the SSP configuration
191 * @ssp: pointer to structure to save SSP configuration
193 * Save the configured SSP state for suspend.
195 void ssp_save_state(struct ssp_dev
*dev
, struct ssp_state
*ssp
)
197 ssp
->cr0
= SSCR0_P(dev
->port
);
198 ssp
->cr1
= SSCR1_P(dev
->port
);
199 ssp
->to
= SSTO_P(dev
->port
);
200 ssp
->psp
= SSPSP_P(dev
->port
);
202 SSCR0_P(dev
->port
) &= ~SSCR0_SSE
;
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
*ssp
)
213 SSSR_P(dev
->port
) = SSSR_ROR
| SSSR_TUR
| SSSR_BCE
;
215 SSCR0_P(dev
->port
) = ssp
->cr0
& ~SSCR0_SSE
;
216 SSCR1_P(dev
->port
) = ssp
->cr1
;
217 SSTO_P(dev
->port
) = ssp
->to
;
218 SSPSP_P(dev
->port
) = ssp
->psp
;
220 SSCR0_P(dev
->port
) = ssp
->cr0
;
224 * ssp_config - configure SSP port settings
225 * @mode: port operating mode
226 * @flags: port config flags
227 * @psp_flags: port PSP config flags
230 * Port MUST be disabled by ssp_disable before making any config changes.
232 int ssp_config(struct ssp_dev
*dev
, u32 mode
, u32 flags
, u32 psp_flags
, u32 speed
)
236 dev
->psp_flags
= psp_flags
;
239 /* set up port type, speed, port settings */
240 SSCR0_P(dev
->port
) = (dev
->speed
| dev
->mode
);
241 SSCR1_P(dev
->port
) = dev
->flags
;
242 SSPSP_P(dev
->port
) = dev
->psp_flags
;
248 * ssp_init - setup the SSP port
250 * initialise and claim resources for the SSP port.
253 * %-ENODEV if the SSP port is unavailable
254 * %-EBUSY if the resources are already in use
257 int ssp_init(struct ssp_dev
*dev
, u32 port
, u32 init_flags
)
261 if (port
> PXA_SSP_PORTS
|| port
== 0)
265 if (use_count
[port
- 1]) {
266 mutex_unlock(&mutex
);
269 use_count
[port
- 1]++;
271 if (!request_mem_region(__PREG(SSCR0_P(port
)), 0x2c, "SSP")) {
272 use_count
[port
- 1]--;
273 mutex_unlock(&mutex
);
278 /* do we need to get irq */
279 if (!(init_flags
& SSP_NO_IRQ
)) {
280 ret
= request_irq(ssp_info
[port
-1].irq
, ssp_interrupt
,
284 dev
->irq
= ssp_info
[port
-1].irq
;
288 /* turn on SSP port clock */
289 pxa_set_cken(ssp_info
[port
-1].clock
, 1);
290 mutex_unlock(&mutex
);
294 release_mem_region(__PREG(SSCR0_P(port
)), 0x2c);
295 use_count
[port
- 1]--;
296 mutex_unlock(&mutex
);
301 * ssp_exit - undo the effects of ssp_init
303 * release and free resources for the SSP port.
305 void ssp_exit(struct ssp_dev
*dev
)
308 SSCR0_P(dev
->port
) &= ~SSCR0_SSE
;
310 if (dev
->port
> PXA_SSP_PORTS
|| dev
->port
== 0) {
311 printk(KERN_WARNING
"SSP: tried to close invalid port\n");
315 pxa_set_cken(ssp_info
[dev
->port
-1].clock
, 0);
317 free_irq(dev
->irq
, dev
);
318 release_mem_region(__PREG(SSCR0_P(dev
->port
)), 0x2c);
319 use_count
[dev
->port
- 1]--;
320 mutex_unlock(&mutex
);
323 EXPORT_SYMBOL(ssp_write_word
);
324 EXPORT_SYMBOL(ssp_read_word
);
325 EXPORT_SYMBOL(ssp_flush
);
326 EXPORT_SYMBOL(ssp_enable
);
327 EXPORT_SYMBOL(ssp_disable
);
328 EXPORT_SYMBOL(ssp_save_state
);
329 EXPORT_SYMBOL(ssp_restore_state
);
330 EXPORT_SYMBOL(ssp_init
);
331 EXPORT_SYMBOL(ssp_exit
);
332 EXPORT_SYMBOL(ssp_config
);
334 MODULE_DESCRIPTION("PXA SSP driver");
335 MODULE_AUTHOR("Liam Girdwood");
336 MODULE_LICENSE("GPL");