MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / arch / arm / mach-pxa / ssp.c
blob6f42004db3ed1fc398108ad35467cda4612838ae
1 /*
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>
31 #include <linux/io.h>
33 #include <asm/irq.h>
34 #include <mach/hardware.h>
35 #include <mach/ssp.h>
36 #include <mach/pxa-regs.h>
37 #include <mach/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;
45 unsigned int status;
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);
59 return IRQ_HANDLED;
62 /**
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.
71 * Returns:
72 * %-ETIMEDOUT timeout occurred
73 * 0 success
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)) {
81 if (!--timeout)
82 return -ETIMEDOUT;
83 cpu_relax();
86 __raw_writel(data, ssp->mmio_base + SSDR);
88 return 0;
91 /**
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.
102 * Returns:
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)) {
112 if (!--timeout)
113 return -ETIMEDOUT;
114 cpu_relax();
117 *data = __raw_readl(ssp->mmio_base + SSDR);
118 return 0;
122 * ssp_flush - flush the transmit and receive FIFOs
124 * Wait for the SSP to idle, and ensure that the receive FIFO
125 * is empty.
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) {
137 if (!--timeout)
138 return -ETIMEDOUT;
139 cpu_relax();
141 timeout = TIMEOUT * 2;
144 do {
145 while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE) {
146 if (!--timeout)
147 return -ETIMEDOUT;
148 (void)__raw_readl(ssp->mmio_base + SSDR);
150 if (!--timeout)
151 return -ETIMEDOUT;
152 } while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_BSY);
154 return 0;
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;
165 uint32_t sscr0;
167 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
168 sscr0 |= SSCR0_SSE;
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;
180 uint32_t sscr0;
182 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
183 sscr0 &= ~SSCR0_SSE;
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);
202 ssp_disable(dev);
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
230 * @speed: port speed
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;
238 dev->mode = mode;
239 dev->flags = flags;
240 dev->psp_flags = psp_flags;
241 dev->speed = speed;
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);
248 return 0;
252 * ssp_init - setup the SSP port
254 * initialise and claim resources for the SSP port.
256 * Returns:
257 * %-ENODEV if the SSP port is unavailable
258 * %-EBUSY if the resources are already in use
259 * %0 on success
261 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
263 struct ssp_device *ssp;
264 int ret;
266 ssp = ssp_request(port, "SSP");
267 if (ssp == NULL)
268 return -ENODEV;
270 dev->ssp = ssp;
271 dev->port = port;
273 /* do we need to get irq */
274 if (!(init_flags & SSP_NO_IRQ)) {
275 ret = request_irq(ssp->irq, ssp_interrupt,
276 0, "SSP", dev);
277 if (ret)
278 goto out_region;
279 dev->irq = ssp->irq;
280 } else
281 dev->irq = NO_IRQ;
283 /* turn on SSP port clock */
284 clk_enable(ssp->clk);
285 return 0;
287 out_region:
288 ssp_free(ssp);
289 return ret;
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;
301 ssp_disable(dev);
302 if (dev->irq != NO_IRQ)
303 free_irq(dev->irq, dev);
304 clk_disable(ssp->clk);
305 ssp_free(ssp);
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) {
319 ssp->use_count++;
320 ssp->label = label;
321 break;
325 mutex_unlock(&ssp_lock);
327 if (&ssp->node == &ssp_list)
328 return NULL;
330 return ssp;
332 EXPORT_SYMBOL(ssp_request);
334 void ssp_free(struct ssp_device *ssp)
336 mutex_lock(&ssp_lock);
337 if (ssp->use_count) {
338 ssp->use_count--;
339 ssp->label = NULL;
340 } else
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;
350 int ret = 0;
352 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
353 if (ssp == NULL) {
354 dev_err(&pdev->dev, "failed to allocate memory");
355 return -ENOMEM;
357 ssp->pdev = pdev;
359 ssp->clk = clk_get(&pdev->dev, NULL);
360 if (IS_ERR(ssp->clk)) {
361 ret = PTR_ERR(ssp->clk);
362 goto err_free;
365 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366 if (res == NULL) {
367 dev_err(&pdev->dev, "no memory resource defined\n");
368 ret = -ENODEV;
369 goto err_free_clk;
372 res = request_mem_region(res->start, res->end - res->start + 1,
373 pdev->name);
374 if (res == NULL) {
375 dev_err(&pdev->dev, "failed to request memory resource\n");
376 ret = -EBUSY;
377 goto err_free_clk;
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");
385 ret = -ENODEV;
386 goto err_free_mem;
389 ssp->irq = platform_get_irq(pdev, 0);
390 if (ssp->irq < 0) {
391 dev_err(&pdev->dev, "no IRQ resource defined\n");
392 ret = -ENODEV;
393 goto err_free_io;
396 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
397 if (res == NULL) {
398 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
399 ret = -ENODEV;
400 goto err_free_io;
402 ssp->drcmr_rx = res->start;
404 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
405 if (res == NULL) {
406 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
407 ret = -ENODEV;
408 goto err_free_io;
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;
416 ssp->use_count = 0;
417 ssp->type = type;
419 mutex_lock(&ssp_lock);
420 list_add(&ssp->node, &ssp_list);
421 mutex_unlock(&ssp_lock);
423 platform_set_drvdata(pdev, ssp);
424 return 0;
426 err_free_io:
427 iounmap(ssp->mmio_base);
428 err_free_mem:
429 release_mem_region(res->start, res->end - res->start + 1);
430 err_free_clk:
431 clk_put(ssp->clk);
432 err_free:
433 kfree(ssp);
434 return ret;
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);
443 if (ssp == NULL)
444 return -ENODEV;
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);
451 clk_put(ssp->clk);
453 mutex_lock(&ssp_lock);
454 list_del(&ssp->node);
455 mutex_unlock(&ssp_lock);
457 kfree(ssp);
458 return 0;
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 = {
477 .driver = {
478 .name = "pxa25x-ssp",
480 .probe = pxa25x_ssp_probe,
481 .remove = __devexit_p(ssp_remove),
484 static struct platform_driver pxa25x_nssp_driver = {
485 .driver = {
486 .name = "pxa25x-nssp",
488 .probe = pxa25x_nssp_probe,
489 .remove = __devexit_p(ssp_remove),
492 static struct platform_driver pxa27x_ssp_driver = {
493 .driver = {
494 .name = "pxa27x-ssp",
496 .probe = pxa27x_ssp_probe,
497 .remove = __devexit_p(ssp_remove),
500 static int __init pxa_ssp_init(void)
502 int ret = 0;
504 ret = platform_driver_register(&pxa25x_ssp_driver);
505 if (ret) {
506 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
507 return ret;
510 ret = platform_driver_register(&pxa25x_nssp_driver);
511 if (ret) {
512 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
513 return ret;
516 ret = platform_driver_register(&pxa27x_ssp_driver);
517 if (ret) {
518 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
519 return ret;
522 return ret;
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");