• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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>
32 
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>
38 
39 #define TIMEOUT 100000
40 
ssp_interrupt(int irq,void * dev_id)41 static irqreturn_t ssp_interrupt(int irq, void *dev_id)
42 {
43 	struct ssp_dev *dev = dev_id;
44 	struct ssp_device *ssp = dev->ssp;
45 	unsigned int status;
46 
47 	status = __raw_readl(ssp->mmio_base + SSSR);
48 	__raw_writel(status, ssp->mmio_base + SSSR);
49 
50 	if (status & SSSR_ROR)
51 		printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
52 
53 	if (status & SSSR_TUR)
54 		printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
55 
56 	if (status & SSSR_BCE)
57 		printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
58 
59 	return IRQ_HANDLED;
60 }
61 
62 /**
63  * ssp_write_word - write a word to the SSP port
64  * @data: 32-bit, MSB justified data to write.
65  *
66  * Wait for a free entry in the SSP transmit FIFO, and write a data
67  * word to the SSP port.
68  *
69  * The caller is expected to perform the necessary locking.
70  *
71  * Returns:
72  *   %-ETIMEDOUT	timeout occurred
73  *   0			success
74  */
ssp_write_word(struct ssp_dev * dev,u32 data)75 int ssp_write_word(struct ssp_dev *dev, u32 data)
76 {
77 	struct ssp_device *ssp = dev->ssp;
78 	int timeout = TIMEOUT;
79 
80 	while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_TNF)) {
81 	        if (!--timeout)
82 	        	return -ETIMEDOUT;
83 		cpu_relax();
84 	}
85 
86 	__raw_writel(data, ssp->mmio_base + SSDR);
87 
88 	return 0;
89 }
90 
91 /**
92  * ssp_read_word - read a word from the SSP port
93  *
94  * Wait for a data word in the SSP receive FIFO, and return the
95  * received data.  Data is LSB justified.
96  *
97  * Note: Currently, if data is not expected to be received, this
98  * function will wait for ever.
99  *
100  * The caller is expected to perform the necessary locking.
101  *
102  * Returns:
103  *   %-ETIMEDOUT	timeout occurred
104  *   32-bit data	success
105  */
ssp_read_word(struct ssp_dev * dev,u32 * data)106 int ssp_read_word(struct ssp_dev *dev, u32 *data)
107 {
108 	struct ssp_device *ssp = dev->ssp;
109 	int timeout = TIMEOUT;
110 
111 	while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE)) {
112 	        if (!--timeout)
113 	        	return -ETIMEDOUT;
114 		cpu_relax();
115 	}
116 
117 	*data = __raw_readl(ssp->mmio_base + SSDR);
118 	return 0;
119 }
120 
121 /**
122  * ssp_flush - flush the transmit and receive FIFOs
123  *
124  * Wait for the SSP to idle, and ensure that the receive FIFO
125  * is empty.
126  *
127  * The caller is expected to perform the necessary locking.
128  */
ssp_flush(struct ssp_dev * dev)129 int ssp_flush(struct ssp_dev *dev)
130 {
131 	struct ssp_device *ssp = dev->ssp;
132 	int timeout = TIMEOUT * 2;
133 
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();
140 		}
141 		timeout = TIMEOUT * 2;
142 	}
143 
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);
149 		}
150 	        if (!--timeout)
151 	        	return -ETIMEDOUT;
152 	} while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_BSY);
153 
154 	return 0;
155 }
156 
157 /**
158  * ssp_enable - enable the SSP port
159  *
160  * Turn on the SSP port.
161  */
ssp_enable(struct ssp_dev * dev)162 void ssp_enable(struct ssp_dev *dev)
163 {
164 	struct ssp_device *ssp = dev->ssp;
165 	uint32_t sscr0;
166 
167 	sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
168 	sscr0 |= SSCR0_SSE;
169 	__raw_writel(sscr0, ssp->mmio_base + SSCR0);
170 }
171 
172 /**
173  * ssp_disable - shut down the SSP port
174  *
175  * Turn off the SSP port, optionally powering it down.
176  */
ssp_disable(struct ssp_dev * dev)177 void ssp_disable(struct ssp_dev *dev)
178 {
179 	struct ssp_device *ssp = dev->ssp;
180 	uint32_t sscr0;
181 
182 	sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
183 	sscr0 &= ~SSCR0_SSE;
184 	__raw_writel(sscr0, ssp->mmio_base + SSCR0);
185 }
186 
187 /**
188  * ssp_save_state - save the SSP configuration
189  * @ssp: pointer to structure to save SSP configuration
190  *
191  * Save the configured SSP state for suspend.
192  */
ssp_save_state(struct ssp_dev * dev,struct ssp_state * state)193 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *state)
194 {
195 	struct ssp_device *ssp = dev->ssp;
196 
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);
201 
202 	ssp_disable(dev);
203 }
204 
205 /**
206  * ssp_restore_state - restore a previously saved SSP configuration
207  * @ssp: pointer to configuration saved by ssp_save_state
208  *
209  * Restore the SSP configuration saved previously by ssp_save_state.
210  */
ssp_restore_state(struct ssp_dev * dev,struct ssp_state * state)211 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *state)
212 {
213 	struct ssp_device *ssp = dev->ssp;
214 	uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
215 
216 	__raw_writel(sssr, ssp->mmio_base + SSSR);
217 
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);
223 }
224 
225 /**
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
231  *
232  * Port MUST be disabled by ssp_disable before making any config changes.
233  */
ssp_config(struct ssp_dev * dev,u32 mode,u32 flags,u32 psp_flags,u32 speed)234 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
235 {
236 	struct ssp_device *ssp = dev->ssp;
237 
238 	dev->mode = mode;
239 	dev->flags = flags;
240 	dev->psp_flags = psp_flags;
241 	dev->speed = speed;
242 
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);
247 
248 	return 0;
249 }
250 
251 /**
252  * ssp_init - setup the SSP port
253  *
254  * initialise and claim resources for the SSP port.
255  *
256  * Returns:
257  *   %-ENODEV	if the SSP port is unavailable
258  *   %-EBUSY	if the resources are already in use
259  *   %0		on success
260  */
ssp_init(struct ssp_dev * dev,u32 port,u32 init_flags)261 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
262 {
263 	struct ssp_device *ssp;
264 	int ret;
265 
266 	ssp = ssp_request(port, "SSP");
267 	if (ssp == NULL)
268 		return -ENODEV;
269 
270 	dev->ssp = ssp;
271 	dev->port = port;
272 
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;
282 
283 	/* turn on SSP port clock */
284 	clk_enable(ssp->clk);
285 	return 0;
286 
287 out_region:
288 	ssp_free(ssp);
289 	return ret;
290 }
291 
292 /**
293  * ssp_exit - undo the effects of ssp_init
294  *
295  * release and free resources for the SSP port.
296  */
ssp_exit(struct ssp_dev * dev)297 void ssp_exit(struct ssp_dev *dev)
298 {
299 	struct ssp_device *ssp = dev->ssp;
300 
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);
306 }
307 
308 static DEFINE_MUTEX(ssp_lock);
309 static LIST_HEAD(ssp_list);
310 
ssp_request(int port,const char * label)311 struct ssp_device *ssp_request(int port, const char *label)
312 {
313 	struct ssp_device *ssp = NULL;
314 
315 	mutex_lock(&ssp_lock);
316 
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;
322 		}
323 	}
324 
325 	mutex_unlock(&ssp_lock);
326 
327 	if (&ssp->node == &ssp_list)
328 		return NULL;
329 
330 	return ssp;
331 }
332 EXPORT_SYMBOL(ssp_request);
333 
ssp_free(struct ssp_device * ssp)334 void ssp_free(struct ssp_device *ssp)
335 {
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);
343 }
344 EXPORT_SYMBOL(ssp_free);
345 
ssp_probe(struct platform_device * pdev,int type)346 static int __devinit ssp_probe(struct platform_device *pdev, int type)
347 {
348 	struct resource *res;
349 	struct ssp_device *ssp;
350 	int ret = 0;
351 
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;
356 	}
357 	ssp->pdev = pdev;
358 
359 	ssp->clk = clk_get(&pdev->dev, NULL);
360 	if (IS_ERR(ssp->clk)) {
361 		ret = PTR_ERR(ssp->clk);
362 		goto err_free;
363 	}
364 
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;
370 	}
371 
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;
378 	}
379 
380 	ssp->phys_base = res->start;
381 
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;
387 	}
388 
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;
394 	}
395 
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;
401 	}
402 	ssp->drcmr_rx = res->start;
403 
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;
409 	}
410 	ssp->drcmr_tx = res->start;
411 
412 	/* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
413 	 * starts from 0, do a translation here
414 	 */
415 	ssp->port_id = pdev->id + 1;
416 	ssp->use_count = 0;
417 	ssp->type = type;
418 
419 	mutex_lock(&ssp_lock);
420 	list_add(&ssp->node, &ssp_list);
421 	mutex_unlock(&ssp_lock);
422 
423 	platform_set_drvdata(pdev, ssp);
424 	return 0;
425 
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;
435 }
436 
ssp_remove(struct platform_device * pdev)437 static int __devexit ssp_remove(struct platform_device *pdev)
438 {
439 	struct resource *res;
440 	struct ssp_device *ssp;
441 
442 	ssp = platform_get_drvdata(pdev);
443 	if (ssp == NULL)
444 		return -ENODEV;
445 
446 	iounmap(ssp->mmio_base);
447 
448 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 	release_mem_region(res->start, res->end - res->start + 1);
450 
451 	clk_put(ssp->clk);
452 
453 	mutex_lock(&ssp_lock);
454 	list_del(&ssp->node);
455 	mutex_unlock(&ssp_lock);
456 
457 	kfree(ssp);
458 	return 0;
459 }
460 
pxa25x_ssp_probe(struct platform_device * pdev)461 static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
462 {
463 	return ssp_probe(pdev, PXA25x_SSP);
464 }
465 
pxa25x_nssp_probe(struct platform_device * pdev)466 static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
467 {
468 	return ssp_probe(pdev, PXA25x_NSSP);
469 }
470 
pxa27x_ssp_probe(struct platform_device * pdev)471 static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
472 {
473 	return ssp_probe(pdev, PXA27x_SSP);
474 }
475 
476 static struct platform_driver pxa25x_ssp_driver = {
477 	.driver		= {
478 		.name	= "pxa25x-ssp",
479 	},
480 	.probe		= pxa25x_ssp_probe,
481 	.remove		= __devexit_p(ssp_remove),
482 };
483 
484 static struct platform_driver pxa25x_nssp_driver = {
485 	.driver		= {
486 		.name	= "pxa25x-nssp",
487 	},
488 	.probe		= pxa25x_nssp_probe,
489 	.remove		= __devexit_p(ssp_remove),
490 };
491 
492 static struct platform_driver pxa27x_ssp_driver = {
493 	.driver		= {
494 		.name	= "pxa27x-ssp",
495 	},
496 	.probe		= pxa27x_ssp_probe,
497 	.remove		= __devexit_p(ssp_remove),
498 };
499 
pxa_ssp_init(void)500 static int __init pxa_ssp_init(void)
501 {
502 	int ret = 0;
503 
504 	ret = platform_driver_register(&pxa25x_ssp_driver);
505 	if (ret) {
506 		printk(KERN_ERR "failed to register pxa25x_ssp_driver");
507 		return ret;
508 	}
509 
510 	ret = platform_driver_register(&pxa25x_nssp_driver);
511 	if (ret) {
512 		printk(KERN_ERR "failed to register pxa25x_nssp_driver");
513 		return ret;
514 	}
515 
516 	ret = platform_driver_register(&pxa27x_ssp_driver);
517 	if (ret) {
518 		printk(KERN_ERR "failed to register pxa27x_ssp_driver");
519 		return ret;
520 	}
521 
522 	return ret;
523 }
524 
pxa_ssp_exit(void)525 static void __exit pxa_ssp_exit(void)
526 {
527 	platform_driver_unregister(&pxa25x_ssp_driver);
528 	platform_driver_unregister(&pxa25x_nssp_driver);
529 	platform_driver_unregister(&pxa27x_ssp_driver);
530 }
531 
532 arch_initcall(pxa_ssp_init);
533 module_exit(pxa_ssp_exit);
534 
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);
545 
546 MODULE_DESCRIPTION("PXA SSP driver");
547 MODULE_AUTHOR("Liam Girdwood");
548 MODULE_LICENSE("GPL");
549 
550