blob: 0225ee016f2dddd1de6d60b6c2f2b690889dae53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Liam Girdwoodb216c012005-11-10 17:45:39 +000022 * 4th Aug 2005 Added option to disable irq handler registration and
23 * cleaned up irq and clock detection.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
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>
Arjan van de Ven00431702006-01-12 18:42:23 +000034#include <linux/mutex.h>
eric miao88286452007-12-06 17:56:42 +080035#include <linux/clk.h>
36#include <linux/err.h>
37#include <linux/platform_device.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/hardware.h>
42#include <asm/arch/ssp.h>
43#include <asm/arch/pxa-regs.h>
eric miao0aea1fd2007-11-21 16:57:12 +080044#include <asm/arch/regs-ssp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010046#define TIMEOUT 100000
47
Linus Torvalds0cd61b62006-10-06 10:53:39 -070048static irqreturn_t ssp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct ssp_dev *dev = (struct ssp_dev*) dev_id;
51 unsigned int status = SSSR_P(dev->port);
52
53 SSSR_P(dev->port) = status; /* clear status bits */
54
55 if (status & SSSR_ROR)
56 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
57
58 if (status & SSSR_TUR)
59 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
60
61 if (status & SSSR_BCE)
62 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
63
64 return IRQ_HANDLED;
65}
66
67/**
68 * ssp_write_word - write a word to the SSP port
69 * @data: 32-bit, MSB justified data to write.
70 *
71 * Wait for a free entry in the SSP transmit FIFO, and write a data
72 * word to the SSP port.
73 *
74 * The caller is expected to perform the necessary locking.
75 *
76 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010077 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * 0 success
79 */
80int ssp_write_word(struct ssp_dev *dev, u32 data)
81{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010082 int timeout = TIMEOUT;
83
84 while (!(SSSR_P(dev->port) & SSSR_TNF)) {
85 if (!--timeout)
86 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 cpu_relax();
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 SSDR_P(dev->port) = data;
91
92 return 0;
93}
94
95/**
96 * ssp_read_word - read a word from the SSP port
97 *
98 * Wait for a data word in the SSP receive FIFO, and return the
99 * received data. Data is LSB justified.
100 *
101 * Note: Currently, if data is not expected to be received, this
102 * function will wait for ever.
103 *
104 * The caller is expected to perform the necessary locking.
105 *
106 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100107 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * 32-bit data success
109 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100110int ssp_read_word(struct ssp_dev *dev, u32 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100112 int timeout = TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100114 while (!(SSSR_P(dev->port) & SSSR_RNE)) {
115 if (!--timeout)
116 return -ETIMEDOUT;
117 cpu_relax();
118 }
119
120 *data = SSDR_P(dev->port);
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/**
125 * ssp_flush - flush the transmit and receive FIFOs
126 *
127 * Wait for the SSP to idle, and ensure that the receive FIFO
128 * is empty.
129 *
130 * The caller is expected to perform the necessary locking.
131 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100132int ssp_flush(struct ssp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100134 int timeout = TIMEOUT * 2;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 do {
137 while (SSSR_P(dev->port) & SSSR_RNE) {
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100138 if (!--timeout)
139 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 (void) SSDR_P(dev->port);
141 }
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100142 if (!--timeout)
143 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 } while (SSSR_P(dev->port) & SSSR_BSY);
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100145
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/**
150 * ssp_enable - enable the SSP port
151 *
152 * Turn on the SSP port.
153 */
154void ssp_enable(struct ssp_dev *dev)
155{
156 SSCR0_P(dev->port) |= SSCR0_SSE;
157}
158
159/**
160 * ssp_disable - shut down the SSP port
161 *
162 * Turn off the SSP port, optionally powering it down.
163 */
164void ssp_disable(struct ssp_dev *dev)
165{
166 SSCR0_P(dev->port) &= ~SSCR0_SSE;
167}
168
169/**
170 * ssp_save_state - save the SSP configuration
171 * @ssp: pointer to structure to save SSP configuration
172 *
173 * Save the configured SSP state for suspend.
174 */
175void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
176{
177 ssp->cr0 = SSCR0_P(dev->port);
178 ssp->cr1 = SSCR1_P(dev->port);
179 ssp->to = SSTO_P(dev->port);
180 ssp->psp = SSPSP_P(dev->port);
181
182 SSCR0_P(dev->port) &= ~SSCR0_SSE;
183}
184
185/**
186 * ssp_restore_state - restore a previously saved SSP configuration
187 * @ssp: pointer to configuration saved by ssp_save_state
188 *
189 * Restore the SSP configuration saved previously by ssp_save_state.
190 */
191void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
192{
193 SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
194
195 SSCR0_P(dev->port) = ssp->cr0 & ~SSCR0_SSE;
196 SSCR1_P(dev->port) = ssp->cr1;
197 SSTO_P(dev->port) = ssp->to;
198 SSPSP_P(dev->port) = ssp->psp;
199
200 SSCR0_P(dev->port) = ssp->cr0;
201}
202
203/**
204 * ssp_config - configure SSP port settings
205 * @mode: port operating mode
206 * @flags: port config flags
207 * @psp_flags: port PSP config flags
208 * @speed: port speed
209 *
210 * Port MUST be disabled by ssp_disable before making any config changes.
211 */
212int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
213{
214 dev->mode = mode;
215 dev->flags = flags;
216 dev->psp_flags = psp_flags;
217 dev->speed = speed;
218
219 /* set up port type, speed, port settings */
220 SSCR0_P(dev->port) = (dev->speed | dev->mode);
221 SSCR1_P(dev->port) = dev->flags;
222 SSPSP_P(dev->port) = dev->psp_flags;
223
224 return 0;
225}
226
227/**
228 * ssp_init - setup the SSP port
229 *
230 * initialise and claim resources for the SSP port.
231 *
232 * Returns:
233 * %-ENODEV if the SSP port is unavailable
234 * %-EBUSY if the resources are already in use
235 * %0 on success
236 */
Liam Girdwoodb216c012005-11-10 17:45:39 +0000237int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
eric miao88286452007-12-06 17:56:42 +0800239 struct ssp_device *ssp;
Liam Girdwoodb216c012005-11-10 17:45:39 +0000240 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
eric miao88286452007-12-06 17:56:42 +0800242 ssp = ssp_request(port, "SSP");
243 if (ssp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENODEV;
245
eric miao88286452007-12-06 17:56:42 +0800246 dev->ssp = ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dev->port = port;
248
Liam Girdwoodb216c012005-11-10 17:45:39 +0000249 /* do we need to get irq */
250 if (!(init_flags & SSP_NO_IRQ)) {
eric miao88286452007-12-06 17:56:42 +0800251 ret = request_irq(ssp->irq, ssp_interrupt,
Liam Girdwoodb216c012005-11-10 17:45:39 +0000252 0, "SSP", dev);
253 if (ret)
254 goto out_region;
eric miao88286452007-12-06 17:56:42 +0800255 dev->irq = ssp->irq;
Liam Girdwoodb216c012005-11-10 17:45:39 +0000256 } else
257 dev->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* turn on SSP port clock */
eric miao88286452007-12-06 17:56:42 +0800260 clk_enable(ssp->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262
263out_region:
eric miao88286452007-12-06 17:56:42 +0800264 ssp_free(ssp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return ret;
266}
267
268/**
269 * ssp_exit - undo the effects of ssp_init
270 *
271 * release and free resources for the SSP port.
272 */
273void ssp_exit(struct ssp_dev *dev)
274{
eric miao88286452007-12-06 17:56:42 +0800275 struct ssp_device *ssp = dev->ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
eric miao88286452007-12-06 17:56:42 +0800277 SSCR0_P(dev->port) &= ~SSCR0_SSE;
278 free_irq(dev->irq, dev);
279 clk_disable(ssp->clk);
280 ssp_free(ssp);
281}
282
283static DEFINE_MUTEX(ssp_lock);
284static LIST_HEAD(ssp_list);
285
286struct ssp_device *ssp_request(int port, const char *label)
287{
288 struct ssp_device *ssp = NULL;
289
290 mutex_lock(&ssp_lock);
291
292 list_for_each_entry(ssp, &ssp_list, node) {
293 if (ssp->port_id == port && ssp->use_count == 0) {
294 ssp->use_count++;
295 ssp->label = label;
296 break;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
eric miao88286452007-12-06 17:56:42 +0800300 mutex_unlock(&ssp_lock);
301
302 if (ssp->port_id != port)
303 return NULL;
304
305 return ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
eric miao88286452007-12-06 17:56:42 +0800307EXPORT_SYMBOL(ssp_request);
308
309void ssp_free(struct ssp_device *ssp)
310{
311 mutex_lock(&ssp_lock);
312 if (ssp->use_count) {
313 ssp->use_count--;
314 ssp->label = NULL;
315 } else
316 dev_err(&ssp->pdev->dev, "device already free\n");
317 mutex_unlock(&ssp_lock);
318}
319EXPORT_SYMBOL(ssp_free);
320
321static int __devinit ssp_probe(struct platform_device *pdev, int type)
322{
323 struct resource *res;
324 struct ssp_device *ssp;
325 int ret = 0;
326
327 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
328 if (ssp == NULL) {
329 dev_err(&pdev->dev, "failed to allocate memory");
330 return -ENOMEM;
331 }
332
333 ssp->clk = clk_get(&pdev->dev, "SSPCLK");
334 if (IS_ERR(ssp->clk)) {
335 ret = PTR_ERR(ssp->clk);
336 goto err_free;
337 }
338
339 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
340 if (res == NULL) {
341 dev_err(&pdev->dev, "no memory resource defined\n");
342 ret = -ENODEV;
343 goto err_free_clk;
344 }
345
346 res = request_mem_region(res->start, res->end - res->start + 1,
347 pdev->name);
348 if (res == NULL) {
349 dev_err(&pdev->dev, "failed to request memory resource\n");
350 ret = -EBUSY;
351 goto err_free_clk;
352 }
353
354 ssp->phys_base = res->start;
355
356 ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
357 if (ssp->mmio_base == NULL) {
358 dev_err(&pdev->dev, "failed to ioremap() registers\n");
359 ret = -ENODEV;
360 goto err_free_mem;
361 }
362
363 ssp->irq = platform_get_irq(pdev, 0);
364 if (ssp->irq < 0) {
365 dev_err(&pdev->dev, "no IRQ resource defined\n");
366 ret = -ENODEV;
367 goto err_free_io;
368 }
369
370 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
371 if (res == NULL) {
372 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
373 ret = -ENODEV;
374 goto err_free_io;
375 }
376 ssp->drcmr_rx = res->start;
377
378 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
379 if (res == NULL) {
380 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
381 ret = -ENODEV;
382 goto err_free_io;
383 }
384 ssp->drcmr_tx = res->start;
385
386 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
387 * starts from 0, do a translation here
388 */
389 ssp->port_id = pdev->id + 1;
390 ssp->use_count = 0;
391 ssp->type = type;
392
393 mutex_lock(&ssp_lock);
394 list_add(&ssp->node, &ssp_list);
395 mutex_unlock(&ssp_lock);
396
397 platform_set_drvdata(pdev, ssp);
398 return 0;
399
400err_free_io:
401 iounmap(ssp->mmio_base);
402err_free_mem:
403 release_mem_region(res->start, res->end - res->start + 1);
404err_free_clk:
405 clk_put(ssp->clk);
406err_free:
407 kfree(ssp);
408 return ret;
409}
410
411static int __devexit ssp_remove(struct platform_device *pdev)
412{
413 struct resource *res;
414 struct ssp_device *ssp;
415
416 ssp = platform_get_drvdata(pdev);
417 if (ssp == NULL)
418 return -ENODEV;
419
420 iounmap(ssp->mmio_base);
421
422 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 release_mem_region(res->start, res->end - res->start + 1);
424
425 clk_put(ssp->clk);
426
427 mutex_lock(&ssp_lock);
428 list_del(&ssp->node);
429 mutex_unlock(&ssp_lock);
430
431 kfree(ssp);
432 return 0;
433}
434
435static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
436{
437 return ssp_probe(pdev, PXA25x_SSP);
438}
439
440static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
441{
442 return ssp_probe(pdev, PXA25x_NSSP);
443}
444
445static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
446{
447 return ssp_probe(pdev, PXA27x_SSP);
448}
449
450static struct platform_driver pxa25x_ssp_driver = {
451 .driver = {
452 .name = "pxa25x-ssp",
453 },
454 .probe = pxa25x_ssp_probe,
455 .remove = __devexit_p(ssp_remove),
456};
457
458static struct platform_driver pxa25x_nssp_driver = {
459 .driver = {
460 .name = "pxa25x-nssp",
461 },
462 .probe = pxa25x_nssp_probe,
463 .remove = __devexit_p(ssp_remove),
464};
465
466static struct platform_driver pxa27x_ssp_driver = {
467 .driver = {
468 .name = "pxa27x-ssp",
469 },
470 .probe = pxa27x_ssp_probe,
471 .remove = __devexit_p(ssp_remove),
472};
473
474static int __init pxa_ssp_init(void)
475{
476 int ret = 0;
477
478 ret = platform_driver_register(&pxa25x_ssp_driver);
479 if (ret) {
480 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
481 return ret;
482 }
483
484 ret = platform_driver_register(&pxa25x_nssp_driver);
485 if (ret) {
486 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
487 return ret;
488 }
489
490 ret = platform_driver_register(&pxa27x_ssp_driver);
491 if (ret) {
492 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
493 return ret;
494 }
495
496 return ret;
497}
498
499static void __exit pxa_ssp_exit(void)
500{
501 platform_driver_unregister(&pxa25x_ssp_driver);
502 platform_driver_unregister(&pxa25x_nssp_driver);
503 platform_driver_unregister(&pxa27x_ssp_driver);
504}
505
506module_init(pxa_ssp_init);
507module_exit(pxa_ssp_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509EXPORT_SYMBOL(ssp_write_word);
510EXPORT_SYMBOL(ssp_read_word);
511EXPORT_SYMBOL(ssp_flush);
512EXPORT_SYMBOL(ssp_enable);
513EXPORT_SYMBOL(ssp_disable);
514EXPORT_SYMBOL(ssp_save_state);
515EXPORT_SYMBOL(ssp_restore_state);
516EXPORT_SYMBOL(ssp_init);
517EXPORT_SYMBOL(ssp_exit);
518EXPORT_SYMBOL(ssp_config);
519
520MODULE_DESCRIPTION("PXA SSP driver");
521MODULE_AUTHOR("Liam Girdwood");
522MODULE_LICENSE("GPL");
523