blob: 59703c6fb29bc514a83a5d4135989758ad801db9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-sa1100/ssp.c
3 *
4 * Copyright (C) 2003 Russell King.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Generic SSP driver. This provides the generic core for simple
11 * IO-based SSP applications.
12 */
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/errno.h>
17#include <linux/interrupt.h>
18#include <linux/ioport.h>
19#include <linux/init.h>
20
21#include <asm/io.h>
22#include <asm/irq.h>
23#include <asm/hardware.h>
24#include <asm/hardware/ssp.h>
25
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010026#define TIMEOUT 100000
27
Linus Torvalds0cd61b62006-10-06 10:53:39 -070028static irqreturn_t ssp_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 unsigned int status = Ser4SSSR;
31
32 if (status & SSSR_ROR) {
33 printk(KERN_WARNING "SSP: receiver overrun\n");
34 }
35
36 Ser4SSSR = SSSR_ROR;
37
38 return status ? IRQ_HANDLED : IRQ_NONE;
39}
40
41/**
42 * ssp_write_word - write a word to the SSP port
43 * @data: 16-bit, MSB justified data to write.
44 *
45 * Wait for a free entry in the SSP transmit FIFO, and write a data
46 * word to the SSP port. Wait for the SSP port to start sending
47 * the data.
48 *
49 * The caller is expected to perform the necessary locking.
50 *
51 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010052 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * 0 success
54 */
55int ssp_write_word(u16 data)
56{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010057 int timeout = TIMEOUT;
58
59 while (!(Ser4SSSR & SSSR_TNF)) {
60 if (!--timeout)
61 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 cpu_relax();
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 Ser4SSDR = data;
66
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010067 timeout = TIMEOUT;
68 while (!(Ser4SSSR & SSSR_BSY)) {
69 if (!--timeout)
70 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cpu_relax();
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return 0;
75}
76
77/**
78 * ssp_read_word - read a word from the SSP port
79 *
80 * Wait for a data word in the SSP receive FIFO, and return the
81 * received data. Data is LSB justified.
82 *
83 * Note: Currently, if data is not expected to be received, this
84 * function will wait for ever.
85 *
86 * The caller is expected to perform the necessary locking.
87 *
88 * Returns:
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010089 * %-ETIMEDOUT timeout occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * 16-bit data success
91 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010092int ssp_read_word(u16 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010094 int timeout = TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +010096 while (!(Ser4SSSR & SSSR_RNE)) {
97 if (!--timeout)
98 return -ETIMEDOUT;
99 cpu_relax();
100 }
101
102 *data = (u16)Ser4SSDR;
103
104 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/**
108 * ssp_flush - flush the transmit and receive FIFOs
109 *
110 * Wait for the SSP to idle, and ensure that the receive FIFO
111 * is empty.
112 *
113 * The caller is expected to perform the necessary locking.
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100114 *
115 * Returns:
116 * %-ETIMEDOUT timeout occurred
117 * 0 success
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100119int ssp_flush(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100121 int timeout = TIMEOUT * 2;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 do {
124 while (Ser4SSSR & SSSR_RNE) {
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100125 if (!--timeout)
126 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 (void) Ser4SSDR;
128 }
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100129 if (!--timeout)
130 return -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } while (Ser4SSSR & SSSR_BSY);
Paul Sokolovsky8f1bf872006-08-27 12:54:56 +0100132
133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136/**
137 * ssp_enable - enable the SSP port
138 *
139 * Turn on the SSP port.
140 */
141void ssp_enable(void)
142{
143 Ser4SSCR0 |= SSCR0_SSE;
144}
145
146/**
147 * ssp_disable - shut down the SSP port
148 *
149 * Turn off the SSP port, optionally powering it down.
150 */
151void ssp_disable(void)
152{
153 Ser4SSCR0 &= ~SSCR0_SSE;
154}
155
156/**
157 * ssp_save_state - save the SSP configuration
158 * @ssp: pointer to structure to save SSP configuration
159 *
160 * Save the configured SSP state for suspend.
161 */
162void ssp_save_state(struct ssp_state *ssp)
163{
164 ssp->cr0 = Ser4SSCR0;
165 ssp->cr1 = Ser4SSCR1;
166
167 Ser4SSCR0 &= ~SSCR0_SSE;
168}
169
170/**
171 * ssp_restore_state - restore a previously saved SSP configuration
172 * @ssp: pointer to configuration saved by ssp_save_state
173 *
174 * Restore the SSP configuration saved previously by ssp_save_state.
175 */
176void ssp_restore_state(struct ssp_state *ssp)
177{
178 Ser4SSSR = SSSR_ROR;
179
180 Ser4SSCR0 = ssp->cr0 & ~SSCR0_SSE;
181 Ser4SSCR1 = ssp->cr1;
182 Ser4SSCR0 = ssp->cr0;
183}
184
185/**
186 * ssp_init - setup the SSP port
187 *
188 * initialise and claim resources for the SSP port.
189 *
190 * Returns:
191 * %-ENODEV if the SSP port is unavailable
192 * %-EBUSY if the resources are already in use
193 * %0 on success
194 */
195int ssp_init(void)
196{
197 int ret;
198
199 if (!(PPAR & PPAR_SPR) && (Ser4MCCR0 & MCCR0_MCE))
200 return -ENODEV;
201
202 if (!request_mem_region(__PREG(Ser4SSCR0), 0x18, "SSP")) {
203 return -EBUSY;
204 }
205
206 Ser4SSSR = SSSR_ROR;
207
208 ret = request_irq(IRQ_Ser4SSP, ssp_interrupt, 0, "SSP", NULL);
209 if (ret)
210 goto out_region;
211
212 return 0;
213
214 out_region:
215 release_mem_region(__PREG(Ser4SSCR0), 0x18);
216 return ret;
217}
218
219/**
220 * ssp_exit - undo the effects of ssp_init
221 *
222 * release and free resources for the SSP port.
223 */
224void ssp_exit(void)
225{
226 Ser4SSCR0 &= ~SSCR0_SSE;
227
228 free_irq(IRQ_Ser4SSP, NULL);
229 release_mem_region(__PREG(Ser4SSCR0), 0x18);
230}
231
232MODULE_AUTHOR("Russell King");
233MODULE_DESCRIPTION("SA11x0 SSP PIO driver");
234MODULE_LICENSE("GPL");
235
236EXPORT_SYMBOL(ssp_write_word);
237EXPORT_SYMBOL(ssp_read_word);
238EXPORT_SYMBOL(ssp_flush);
239EXPORT_SYMBOL(ssp_enable);
240EXPORT_SYMBOL(ssp_disable);
241EXPORT_SYMBOL(ssp_save_state);
242EXPORT_SYMBOL(ssp_restore_state);
243EXPORT_SYMBOL(ssp_init);
244EXPORT_SYMBOL(ssp_exit);