blob: be6fee78f439dd04ee14ff2cf0f495a77ce15f07 [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>
44
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010045#define TIMEOUT 100000
46
Linus Torvalds0cd61b62006-10-06 10:53:39 -070047static irqreturn_t ssp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 struct ssp_dev *dev = (struct ssp_dev*) dev_id;
50 unsigned int status = SSSR_P(dev->port);
51
52 SSSR_P(dev->port) = status; /* clear status bits */
53
54 if (status & SSSR_ROR)
55 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
56
57 if (status & SSSR_TUR)
58 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
59
60 if (status & SSSR_BCE)
61 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
62
63 return IRQ_HANDLED;
64}
65
66/**
67 * ssp_write_word - write a word to the SSP port
68 * @data: 32-bit, MSB justified data to write.
69 *
70 * Wait for a free entry in the SSP transmit FIFO, and write a data
71 * word to the SSP port.
72 *
73 * The caller is expected to perform the necessary locking.
74 *
75 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010076 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * 0 success
78 */
79int ssp_write_word(struct ssp_dev *dev, u32 data)
80{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010081 int timeout = TIMEOUT;
82
83 while (!(SSSR_P(dev->port) & SSSR_TNF)) {
84 if (!--timeout)
85 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 cpu_relax();
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 SSDR_P(dev->port) = data;
90
91 return 0;
92}
93
94/**
95 * ssp_read_word - read a word from the SSP port
96 *
97 * Wait for a data word in the SSP receive FIFO, and return the
98 * received data. Data is LSB justified.
99 *
100 * Note: Currently, if data is not expected to be received, this
101 * function will wait for ever.
102 *
103 * The caller is expected to perform the necessary locking.
104 *
105 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100106 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * 32-bit data success
108 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100109int ssp_read_word(struct ssp_dev *dev, u32 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100111 int timeout = TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100113 while (!(SSSR_P(dev->port) & SSSR_RNE)) {
114 if (!--timeout)
115 return -ETIMEDOUT;
116 cpu_relax();
117 }
118
119 *data = SSDR_P(dev->port);
120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/**
124 * ssp_flush - flush the transmit and receive FIFOs
125 *
126 * Wait for the SSP to idle, and ensure that the receive FIFO
127 * is empty.
128 *
129 * The caller is expected to perform the necessary locking.
130 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100131int ssp_flush(struct ssp_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100133 int timeout = TIMEOUT * 2;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 do {
136 while (SSSR_P(dev->port) & SSSR_RNE) {
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100137 if (!--timeout)
138 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (void) SSDR_P(dev->port);
140 }
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100141 if (!--timeout)
142 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } while (SSSR_P(dev->port) & SSSR_BSY);
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100144
145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/**
149 * ssp_enable - enable the SSP port
150 *
151 * Turn on the SSP port.
152 */
153void ssp_enable(struct ssp_dev *dev)
154{
155 SSCR0_P(dev->port) |= SSCR0_SSE;
156}
157
158/**
159 * ssp_disable - shut down the SSP port
160 *
161 * Turn off the SSP port, optionally powering it down.
162 */
163void ssp_disable(struct ssp_dev *dev)
164{
165 SSCR0_P(dev->port) &= ~SSCR0_SSE;
166}
167
168/**
169 * ssp_save_state - save the SSP configuration
170 * @ssp: pointer to structure to save SSP configuration
171 *
172 * Save the configured SSP state for suspend.
173 */
174void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
175{
176 ssp->cr0 = SSCR0_P(dev->port);
177 ssp->cr1 = SSCR1_P(dev->port);
178 ssp->to = SSTO_P(dev->port);
179 ssp->psp = SSPSP_P(dev->port);
180
181 SSCR0_P(dev->port) &= ~SSCR0_SSE;
182}
183
184/**
185 * ssp_restore_state - restore a previously saved SSP configuration
186 * @ssp: pointer to configuration saved by ssp_save_state
187 *
188 * Restore the SSP configuration saved previously by ssp_save_state.
189 */
190void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
191{
192 SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
193
194 SSCR0_P(dev->port) = ssp->cr0 & ~SSCR0_SSE;
195 SSCR1_P(dev->port) = ssp->cr1;
196 SSTO_P(dev->port) = ssp->to;
197 SSPSP_P(dev->port) = ssp->psp;
198
199 SSCR0_P(dev->port) = ssp->cr0;
200}
201
202/**
203 * ssp_config - configure SSP port settings
204 * @mode: port operating mode
205 * @flags: port config flags
206 * @psp_flags: port PSP config flags
207 * @speed: port speed
208 *
209 * Port MUST be disabled by ssp_disable before making any config changes.
210 */
211int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
212{
213 dev->mode = mode;
214 dev->flags = flags;
215 dev->psp_flags = psp_flags;
216 dev->speed = speed;
217
218 /* set up port type, speed, port settings */
219 SSCR0_P(dev->port) = (dev->speed | dev->mode);
220 SSCR1_P(dev->port) = dev->flags;
221 SSPSP_P(dev->port) = dev->psp_flags;
222
223 return 0;
224}
225
226/**
227 * ssp_init - setup the SSP port
228 *
229 * initialise and claim resources for the SSP port.
230 *
231 * Returns:
232 * %-ENODEV if the SSP port is unavailable
233 * %-EBUSY if the resources are already in use
234 * %0 on success
235 */
Liam Girdwoodb216c012005-11-10 17:45:39 +0000236int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
eric miao88286452007-12-06 17:56:42 +0800238 struct ssp_device *ssp;
Liam Girdwoodb216c012005-11-10 17:45:39 +0000239 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
eric miao88286452007-12-06 17:56:42 +0800241 ssp = ssp_request(port, "SSP");
242 if (ssp == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ENODEV;
244
eric miao88286452007-12-06 17:56:42 +0800245 dev->ssp = ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 dev->port = port;
247
Liam Girdwoodb216c012005-11-10 17:45:39 +0000248 /* do we need to get irq */
249 if (!(init_flags & SSP_NO_IRQ)) {
eric miao88286452007-12-06 17:56:42 +0800250 ret = request_irq(ssp->irq, ssp_interrupt,
Liam Girdwoodb216c012005-11-10 17:45:39 +0000251 0, "SSP", dev);
252 if (ret)
253 goto out_region;
eric miao88286452007-12-06 17:56:42 +0800254 dev->irq = ssp->irq;
Liam Girdwoodb216c012005-11-10 17:45:39 +0000255 } else
256 dev->irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* turn on SSP port clock */
eric miao88286452007-12-06 17:56:42 +0800259 clk_enable(ssp->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261
262out_region:
eric miao88286452007-12-06 17:56:42 +0800263 ssp_free(ssp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return ret;
265}
266
267/**
268 * ssp_exit - undo the effects of ssp_init
269 *
270 * release and free resources for the SSP port.
271 */
272void ssp_exit(struct ssp_dev *dev)
273{
eric miao88286452007-12-06 17:56:42 +0800274 struct ssp_device *ssp = dev->ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
eric miao88286452007-12-06 17:56:42 +0800276 SSCR0_P(dev->port) &= ~SSCR0_SSE;
277 free_irq(dev->irq, dev);
278 clk_disable(ssp->clk);
279 ssp_free(ssp);
280}
281
282static DEFINE_MUTEX(ssp_lock);
283static LIST_HEAD(ssp_list);
284
285struct ssp_device *ssp_request(int port, const char *label)
286{
287 struct ssp_device *ssp = NULL;
288
289 mutex_lock(&ssp_lock);
290
291 list_for_each_entry(ssp, &ssp_list, node) {
292 if (ssp->port_id == port && ssp->use_count == 0) {
293 ssp->use_count++;
294 ssp->label = label;
295 break;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
eric miao88286452007-12-06 17:56:42 +0800299 mutex_unlock(&ssp_lock);
300
301 if (ssp->port_id != port)
302 return NULL;
303
304 return ssp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
eric miao88286452007-12-06 17:56:42 +0800306EXPORT_SYMBOL(ssp_request);
307
308void ssp_free(struct ssp_device *ssp)
309{
310 mutex_lock(&ssp_lock);
311 if (ssp->use_count) {
312 ssp->use_count--;
313 ssp->label = NULL;
314 } else
315 dev_err(&ssp->pdev->dev, "device already free\n");
316 mutex_unlock(&ssp_lock);
317}
318EXPORT_SYMBOL(ssp_free);
319
320static int __devinit ssp_probe(struct platform_device *pdev, int type)
321{
322 struct resource *res;
323 struct ssp_device *ssp;
324 int ret = 0;
325
326 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
327 if (ssp == NULL) {
328 dev_err(&pdev->dev, "failed to allocate memory");
329 return -ENOMEM;
330 }
331
332 ssp->clk = clk_get(&pdev->dev, "SSPCLK");
333 if (IS_ERR(ssp->clk)) {
334 ret = PTR_ERR(ssp->clk);
335 goto err_free;
336 }
337
338 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
339 if (res == NULL) {
340 dev_err(&pdev->dev, "no memory resource defined\n");
341 ret = -ENODEV;
342 goto err_free_clk;
343 }
344
345 res = request_mem_region(res->start, res->end - res->start + 1,
346 pdev->name);
347 if (res == NULL) {
348 dev_err(&pdev->dev, "failed to request memory resource\n");
349 ret = -EBUSY;
350 goto err_free_clk;
351 }
352
353 ssp->phys_base = res->start;
354
355 ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
356 if (ssp->mmio_base == NULL) {
357 dev_err(&pdev->dev, "failed to ioremap() registers\n");
358 ret = -ENODEV;
359 goto err_free_mem;
360 }
361
362 ssp->irq = platform_get_irq(pdev, 0);
363 if (ssp->irq < 0) {
364 dev_err(&pdev->dev, "no IRQ resource defined\n");
365 ret = -ENODEV;
366 goto err_free_io;
367 }
368
369 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
370 if (res == NULL) {
371 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
372 ret = -ENODEV;
373 goto err_free_io;
374 }
375 ssp->drcmr_rx = res->start;
376
377 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
378 if (res == NULL) {
379 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
380 ret = -ENODEV;
381 goto err_free_io;
382 }
383 ssp->drcmr_tx = res->start;
384
385 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
386 * starts from 0, do a translation here
387 */
388 ssp->port_id = pdev->id + 1;
389 ssp->use_count = 0;
390 ssp->type = type;
391
392 mutex_lock(&ssp_lock);
393 list_add(&ssp->node, &ssp_list);
394 mutex_unlock(&ssp_lock);
395
396 platform_set_drvdata(pdev, ssp);
397 return 0;
398
399err_free_io:
400 iounmap(ssp->mmio_base);
401err_free_mem:
402 release_mem_region(res->start, res->end - res->start + 1);
403err_free_clk:
404 clk_put(ssp->clk);
405err_free:
406 kfree(ssp);
407 return ret;
408}
409
410static int __devexit ssp_remove(struct platform_device *pdev)
411{
412 struct resource *res;
413 struct ssp_device *ssp;
414
415 ssp = platform_get_drvdata(pdev);
416 if (ssp == NULL)
417 return -ENODEV;
418
419 iounmap(ssp->mmio_base);
420
421 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422 release_mem_region(res->start, res->end - res->start + 1);
423
424 clk_put(ssp->clk);
425
426 mutex_lock(&ssp_lock);
427 list_del(&ssp->node);
428 mutex_unlock(&ssp_lock);
429
430 kfree(ssp);
431 return 0;
432}
433
434static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
435{
436 return ssp_probe(pdev, PXA25x_SSP);
437}
438
439static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
440{
441 return ssp_probe(pdev, PXA25x_NSSP);
442}
443
444static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
445{
446 return ssp_probe(pdev, PXA27x_SSP);
447}
448
449static struct platform_driver pxa25x_ssp_driver = {
450 .driver = {
451 .name = "pxa25x-ssp",
452 },
453 .probe = pxa25x_ssp_probe,
454 .remove = __devexit_p(ssp_remove),
455};
456
457static struct platform_driver pxa25x_nssp_driver = {
458 .driver = {
459 .name = "pxa25x-nssp",
460 },
461 .probe = pxa25x_nssp_probe,
462 .remove = __devexit_p(ssp_remove),
463};
464
465static struct platform_driver pxa27x_ssp_driver = {
466 .driver = {
467 .name = "pxa27x-ssp",
468 },
469 .probe = pxa27x_ssp_probe,
470 .remove = __devexit_p(ssp_remove),
471};
472
473static int __init pxa_ssp_init(void)
474{
475 int ret = 0;
476
477 ret = platform_driver_register(&pxa25x_ssp_driver);
478 if (ret) {
479 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
480 return ret;
481 }
482
483 ret = platform_driver_register(&pxa25x_nssp_driver);
484 if (ret) {
485 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
486 return ret;
487 }
488
489 ret = platform_driver_register(&pxa27x_ssp_driver);
490 if (ret) {
491 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
492 return ret;
493 }
494
495 return ret;
496}
497
498static void __exit pxa_ssp_exit(void)
499{
500 platform_driver_unregister(&pxa25x_ssp_driver);
501 platform_driver_unregister(&pxa25x_nssp_driver);
502 platform_driver_unregister(&pxa27x_ssp_driver);
503}
504
505module_init(pxa_ssp_init);
506module_exit(pxa_ssp_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508EXPORT_SYMBOL(ssp_write_word);
509EXPORT_SYMBOL(ssp_read_word);
510EXPORT_SYMBOL(ssp_flush);
511EXPORT_SYMBOL(ssp_enable);
512EXPORT_SYMBOL(ssp_disable);
513EXPORT_SYMBOL(ssp_save_state);
514EXPORT_SYMBOL(ssp_restore_state);
515EXPORT_SYMBOL(ssp_init);
516EXPORT_SYMBOL(ssp_exit);
517EXPORT_SYMBOL(ssp_config);
518
519MODULE_DESCRIPTION("PXA SSP driver");
520MODULE_AUTHOR("Liam Girdwood");
521MODULE_LICENSE("GPL");
522