Merge with http://kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[sfrench/cifs-2.6.git] / arch / arm / mach-pxa / ssp.c
1 /*
2  *  linux/arch/arm/mach-pxa/ssp.c
3  *
4  *  based on linux/arch/arm/mach-sa1100/ssp.c by Russell King
5  *
6  *  Copyright (C) 2003 Russell King.
7  *  Copyright (C) 2003 Wolfson Microelectronics PLC
8  *
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.
12  *
13  *  PXA2xx SSP driver.  This provides the generic core for simple
14  *  IO-based SSP applications and allows easy port setup for DMA access.
15  *
16  *  Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
17  *
18  *  Revision history:
19  *   22nd Aug 2003 Initial version.
20  *   20th Dec 2004 Added ssp_config for changing port config without
21  *                 closing the port.
22  *    4th Aug 2005 Added option to disable irq handler registration and
23  *                 cleaned up irq and clock detection.
24  */
25
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 <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/hardware.h>
37 #include <asm/arch/ssp.h>
38 #include <asm/arch/pxa-regs.h>
39
40 #define PXA_SSP_PORTS   3
41
42 struct ssp_info_ {
43         int irq;
44         u32 clock;
45 };
46
47 /*
48  * SSP port clock and IRQ settings
49  */
50 static const struct ssp_info_ ssp_info[PXA_SSP_PORTS] = {
51 #if defined (CONFIG_PXA27x)
52         {IRQ_SSP,       CKEN23_SSP1},
53         {IRQ_SSP2,      CKEN3_SSP2},
54         {IRQ_SSP3,      CKEN4_SSP3},
55 #else
56         {IRQ_SSP,       CKEN3_SSP},
57         {IRQ_NSSP,      CKEN9_NSSP},
58         {IRQ_ASSP,      CKEN10_ASSP},
59 #endif
60 };
61
62 static DECLARE_MUTEX(sem);
63 static int use_count[PXA_SSP_PORTS] = {0, 0, 0};
64
65 static irqreturn_t ssp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
66 {
67         struct ssp_dev *dev = (struct ssp_dev*) dev_id;
68         unsigned int status = SSSR_P(dev->port);
69
70         SSSR_P(dev->port) = status; /* clear status bits */
71
72         if (status & SSSR_ROR)
73                 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
74
75         if (status & SSSR_TUR)
76                 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
77
78         if (status & SSSR_BCE)
79                 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
80
81         return IRQ_HANDLED;
82 }
83
84 /**
85  * ssp_write_word - write a word to the SSP port
86  * @data: 32-bit, MSB justified data to write.
87  *
88  * Wait for a free entry in the SSP transmit FIFO, and write a data
89  * word to the SSP port.
90  *
91  * The caller is expected to perform the necessary locking.
92  *
93  * Returns:
94  *   %-ETIMEDOUT        timeout occurred (for future)
95  *   0                  success
96  */
97 int ssp_write_word(struct ssp_dev *dev, u32 data)
98 {
99         while (!(SSSR_P(dev->port) & SSSR_TNF))
100                 cpu_relax();
101
102         SSDR_P(dev->port) = data;
103
104         return 0;
105 }
106
107 /**
108  * ssp_read_word - read a word from the SSP port
109  *
110  * Wait for a data word in the SSP receive FIFO, and return the
111  * received data.  Data is LSB justified.
112  *
113  * Note: Currently, if data is not expected to be received, this
114  * function will wait for ever.
115  *
116  * The caller is expected to perform the necessary locking.
117  *
118  * Returns:
119  *   %-ETIMEDOUT        timeout occurred (for future)
120  *   32-bit data        success
121  */
122 int ssp_read_word(struct ssp_dev *dev)
123 {
124         while (!(SSSR_P(dev->port) & SSSR_RNE))
125                 cpu_relax();
126
127         return SSDR_P(dev->port);
128 }
129
130 /**
131  * ssp_flush - flush the transmit and receive FIFOs
132  *
133  * Wait for the SSP to idle, and ensure that the receive FIFO
134  * is empty.
135  *
136  * The caller is expected to perform the necessary locking.
137  */
138 void ssp_flush(struct ssp_dev *dev)
139 {
140         do {
141                 while (SSSR_P(dev->port) & SSSR_RNE) {
142                         (void) SSDR_P(dev->port);
143                 }
144         } while (SSSR_P(dev->port) & SSSR_BSY);
145 }
146
147 /**
148  * ssp_enable - enable the SSP port
149  *
150  * Turn on the SSP port.
151  */
152 void ssp_enable(struct ssp_dev *dev)
153 {
154         SSCR0_P(dev->port) |= SSCR0_SSE;
155 }
156
157 /**
158  * ssp_disable - shut down the SSP port
159  *
160  * Turn off the SSP port, optionally powering it down.
161  */
162 void ssp_disable(struct ssp_dev *dev)
163 {
164         SSCR0_P(dev->port) &= ~SSCR0_SSE;
165 }
166
167 /**
168  * ssp_save_state - save the SSP configuration
169  * @ssp: pointer to structure to save SSP configuration
170  *
171  * Save the configured SSP state for suspend.
172  */
173 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
174 {
175         ssp->cr0 = SSCR0_P(dev->port);
176         ssp->cr1 = SSCR1_P(dev->port);
177         ssp->to = SSTO_P(dev->port);
178         ssp->psp = SSPSP_P(dev->port);
179
180         SSCR0_P(dev->port) &= ~SSCR0_SSE;
181 }
182
183 /**
184  * ssp_restore_state - restore a previously saved SSP configuration
185  * @ssp: pointer to configuration saved by ssp_save_state
186  *
187  * Restore the SSP configuration saved previously by ssp_save_state.
188  */
189 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
190 {
191         SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
192
193         SSCR0_P(dev->port) = ssp->cr0 & ~SSCR0_SSE;
194         SSCR1_P(dev->port) = ssp->cr1;
195         SSTO_P(dev->port) = ssp->to;
196         SSPSP_P(dev->port) = ssp->psp;
197
198         SSCR0_P(dev->port) = ssp->cr0;
199 }
200
201 /**
202  * ssp_config - configure SSP port settings
203  * @mode: port operating mode
204  * @flags: port config flags
205  * @psp_flags: port PSP config flags
206  * @speed: port speed
207  *
208  * Port MUST be disabled by ssp_disable before making any config changes.
209  */
210 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
211 {
212         dev->mode = mode;
213         dev->flags = flags;
214         dev->psp_flags = psp_flags;
215         dev->speed = speed;
216
217         /* set up port type, speed, port settings */
218         SSCR0_P(dev->port) = (dev->speed | dev->mode);
219         SSCR1_P(dev->port) = dev->flags;
220         SSPSP_P(dev->port) = dev->psp_flags;
221
222         return 0;
223 }
224
225 /**
226  * ssp_init - setup the SSP port
227  *
228  * initialise and claim resources for the SSP port.
229  *
230  * Returns:
231  *   %-ENODEV   if the SSP port is unavailable
232  *   %-EBUSY    if the resources are already in use
233  *   %0         on success
234  */
235 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
236 {
237         int ret;
238
239         if (port > PXA_SSP_PORTS || port == 0)
240                 return -ENODEV;
241
242         down(&sem);
243         if (use_count[port - 1]) {
244                 up(&sem);
245                 return -EBUSY;
246         }
247         use_count[port - 1]++;
248
249         if (!request_mem_region(__PREG(SSCR0_P(port)), 0x2c, "SSP")) {
250                 use_count[port - 1]--;
251                 up(&sem);
252                 return -EBUSY;
253         }
254         dev->port = port;
255
256         /* do we need to get irq */
257         if (!(init_flags & SSP_NO_IRQ)) {
258                 ret = request_irq(ssp_info[port-1].irq, ssp_interrupt,
259                                 0, "SSP", dev);
260                 if (ret)
261                         goto out_region;
262                 dev->irq = ssp_info[port-1].irq;
263         } else
264                 dev->irq = 0;
265
266         /* turn on SSP port clock */
267         pxa_set_cken(ssp_info[port-1].clock, 1);
268         up(&sem);
269         return 0;
270
271 out_region:
272         release_mem_region(__PREG(SSCR0_P(port)), 0x2c);
273         use_count[port - 1]--;
274         up(&sem);
275         return ret;
276 }
277
278 /**
279  * ssp_exit - undo the effects of ssp_init
280  *
281  * release and free resources for the SSP port.
282  */
283 void ssp_exit(struct ssp_dev *dev)
284 {
285         down(&sem);
286         SSCR0_P(dev->port) &= ~SSCR0_SSE;
287
288         if (dev->port > PXA_SSP_PORTS || dev->port == 0) {
289                 printk(KERN_WARNING "SSP: tried to close invalid port\n");
290                 return;
291         }
292
293         pxa_set_cken(ssp_info[dev->port-1].clock, 0);
294         if (dev->irq)
295                 free_irq(dev->irq, dev);
296         release_mem_region(__PREG(SSCR0_P(dev->port)), 0x2c);
297         use_count[dev->port - 1]--;
298         up(&sem);
299 }
300
301 EXPORT_SYMBOL(ssp_write_word);
302 EXPORT_SYMBOL(ssp_read_word);
303 EXPORT_SYMBOL(ssp_flush);
304 EXPORT_SYMBOL(ssp_enable);
305 EXPORT_SYMBOL(ssp_disable);
306 EXPORT_SYMBOL(ssp_save_state);
307 EXPORT_SYMBOL(ssp_restore_state);
308 EXPORT_SYMBOL(ssp_init);
309 EXPORT_SYMBOL(ssp_exit);
310 EXPORT_SYMBOL(ssp_config);
311
312 MODULE_DESCRIPTION("PXA SSP driver");
313 MODULE_AUTHOR("Liam Girdwood");
314 MODULE_LICENSE("GPL");
315