| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  *  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 Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 22 |  *    4th Aug 2005 Added option to disable irq handler registration and | 
 | 23 |  *                 cleaned up irq and clock detection. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  */ | 
 | 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 Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 34 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/io.h> | 
 | 36 | #include <asm/irq.h> | 
 | 37 | #include <asm/hardware.h> | 
 | 38 | #include <asm/arch/ssp.h> | 
 | 39 | #include <asm/arch/pxa-regs.h> | 
 | 40 |  | 
 | 41 | #define PXA_SSP_PORTS 	3 | 
 | 42 |  | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 43 | struct ssp_info_ { | 
 | 44 | 	int irq; | 
 | 45 | 	u32 clock; | 
 | 46 | }; | 
 | 47 |  | 
 | 48 | /* | 
 | 49 |  * SSP port clock and IRQ settings | 
 | 50 |  */ | 
 | 51 | static const struct ssp_info_ ssp_info[PXA_SSP_PORTS] = { | 
 | 52 | #if defined (CONFIG_PXA27x) | 
 | 53 | 	{IRQ_SSP,	CKEN23_SSP1}, | 
 | 54 | 	{IRQ_SSP2,	CKEN3_SSP2}, | 
 | 55 | 	{IRQ_SSP3,	CKEN4_SSP3}, | 
 | 56 | #else | 
 | 57 | 	{IRQ_SSP,	CKEN3_SSP}, | 
 | 58 | 	{IRQ_NSSP,	CKEN9_NSSP}, | 
 | 59 | 	{IRQ_ASSP,	CKEN10_ASSP}, | 
 | 60 | #endif | 
 | 61 | }; | 
 | 62 |  | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 63 | static DEFINE_MUTEX(mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static int use_count[PXA_SSP_PORTS] = {0, 0, 0}; | 
 | 65 |  | 
 | 66 | static irqreturn_t ssp_interrupt(int irq, void *dev_id, struct pt_regs *regs) | 
 | 67 | { | 
 | 68 | 	struct ssp_dev *dev = (struct ssp_dev*) dev_id; | 
 | 69 | 	unsigned int status = SSSR_P(dev->port); | 
 | 70 |  | 
 | 71 | 	SSSR_P(dev->port) = status; /* clear status bits */ | 
 | 72 |  | 
 | 73 | 	if (status & SSSR_ROR) | 
 | 74 | 		printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port); | 
 | 75 |  | 
 | 76 | 	if (status & SSSR_TUR) | 
 | 77 | 		printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port); | 
 | 78 |  | 
 | 79 | 	if (status & SSSR_BCE) | 
 | 80 | 		printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port); | 
 | 81 |  | 
 | 82 | 	return IRQ_HANDLED; | 
 | 83 | } | 
 | 84 |  | 
 | 85 | /** | 
 | 86 |  * ssp_write_word - write a word to the SSP port | 
 | 87 |  * @data: 32-bit, MSB justified data to write. | 
 | 88 |  * | 
 | 89 |  * Wait for a free entry in the SSP transmit FIFO, and write a data | 
 | 90 |  * word to the SSP port. | 
 | 91 |  * | 
 | 92 |  * The caller is expected to perform the necessary locking. | 
 | 93 |  * | 
 | 94 |  * Returns: | 
 | 95 |  *   %-ETIMEDOUT	timeout occurred (for future) | 
 | 96 |  *   0			success | 
 | 97 |  */ | 
 | 98 | int ssp_write_word(struct ssp_dev *dev, u32 data) | 
 | 99 | { | 
 | 100 | 	while (!(SSSR_P(dev->port) & SSSR_TNF)) | 
 | 101 | 		cpu_relax(); | 
 | 102 |  | 
 | 103 | 	SSDR_P(dev->port) = data; | 
 | 104 |  | 
 | 105 | 	return 0; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | /** | 
 | 109 |  * ssp_read_word - read a word from the SSP port | 
 | 110 |  * | 
 | 111 |  * Wait for a data word in the SSP receive FIFO, and return the | 
 | 112 |  * received data.  Data is LSB justified. | 
 | 113 |  * | 
 | 114 |  * Note: Currently, if data is not expected to be received, this | 
 | 115 |  * function will wait for ever. | 
 | 116 |  * | 
 | 117 |  * The caller is expected to perform the necessary locking. | 
 | 118 |  * | 
 | 119 |  * Returns: | 
 | 120 |  *   %-ETIMEDOUT	timeout occurred (for future) | 
 | 121 |  *   32-bit data	success | 
 | 122 |  */ | 
 | 123 | int ssp_read_word(struct ssp_dev *dev) | 
 | 124 | { | 
 | 125 | 	while (!(SSSR_P(dev->port) & SSSR_RNE)) | 
 | 126 | 		cpu_relax(); | 
 | 127 |  | 
 | 128 | 	return SSDR_P(dev->port); | 
 | 129 | } | 
 | 130 |  | 
 | 131 | /** | 
 | 132 |  * ssp_flush - flush the transmit and receive FIFOs | 
 | 133 |  * | 
 | 134 |  * Wait for the SSP to idle, and ensure that the receive FIFO | 
 | 135 |  * is empty. | 
 | 136 |  * | 
 | 137 |  * The caller is expected to perform the necessary locking. | 
 | 138 |  */ | 
 | 139 | void ssp_flush(struct ssp_dev *dev) | 
 | 140 | { | 
 | 141 | 	do { | 
 | 142 | 		while (SSSR_P(dev->port) & SSSR_RNE) { | 
 | 143 | 			(void) SSDR_P(dev->port); | 
 | 144 | 		} | 
 | 145 | 	} while (SSSR_P(dev->port) & SSSR_BSY); | 
 | 146 | } | 
 | 147 |  | 
 | 148 | /** | 
 | 149 |  * ssp_enable - enable the SSP port | 
 | 150 |  * | 
 | 151 |  * Turn on the SSP port. | 
 | 152 |  */ | 
 | 153 | void 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 |  */ | 
 | 163 | void 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 |  */ | 
 | 174 | void 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 |  */ | 
 | 190 | void 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 |  */ | 
 | 211 | int 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 Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 236 | int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 238 | 	int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 |  | 
 | 240 | 	if (port > PXA_SSP_PORTS || port == 0) | 
 | 241 | 		return -ENODEV; | 
 | 242 |  | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 243 | 	mutex_lock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | 	if (use_count[port - 1]) { | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 245 | 		mutex_unlock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | 		return -EBUSY; | 
 | 247 | 	} | 
 | 248 | 	use_count[port - 1]++; | 
 | 249 |  | 
 | 250 | 	if (!request_mem_region(__PREG(SSCR0_P(port)), 0x2c, "SSP")) { | 
 | 251 | 		use_count[port - 1]--; | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 252 | 		mutex_unlock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | 		return -EBUSY; | 
 | 254 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | 	dev->port = port; | 
 | 256 |  | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 257 | 	/* do we need to get irq */ | 
 | 258 | 	if (!(init_flags & SSP_NO_IRQ)) { | 
 | 259 | 		ret = request_irq(ssp_info[port-1].irq, ssp_interrupt, | 
 | 260 | 				0, "SSP", dev); | 
 | 261 | 	    	if (ret) | 
 | 262 | 			goto out_region; | 
 | 263 | 	    	dev->irq = ssp_info[port-1].irq; | 
 | 264 | 	} else | 
 | 265 | 		dev->irq = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 |  | 
 | 267 | 	/* turn on SSP port clock */ | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 268 | 	pxa_set_cken(ssp_info[port-1].clock, 1); | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 269 | 	mutex_unlock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | 	return 0; | 
 | 271 |  | 
 | 272 | out_region: | 
 | 273 | 	release_mem_region(__PREG(SSCR0_P(port)), 0x2c); | 
 | 274 | 	use_count[port - 1]--; | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 275 | 	mutex_unlock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | 	return ret; | 
 | 277 | } | 
 | 278 |  | 
 | 279 | /** | 
 | 280 |  * ssp_exit - undo the effects of ssp_init | 
 | 281 |  * | 
 | 282 |  * release and free resources for the SSP port. | 
 | 283 |  */ | 
 | 284 | void ssp_exit(struct ssp_dev *dev) | 
 | 285 | { | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 286 | 	mutex_lock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | 	SSCR0_P(dev->port) &= ~SSCR0_SSE; | 
 | 288 |  | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 289 |     	if (dev->port > PXA_SSP_PORTS || dev->port == 0) { | 
 | 290 | 		printk(KERN_WARNING "SSP: tried to close invalid port\n"); | 
 | 291 | 		return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | 	} | 
 | 293 |  | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 294 | 	pxa_set_cken(ssp_info[dev->port-1].clock, 0); | 
 | 295 | 	if (dev->irq) | 
 | 296 | 		free_irq(dev->irq, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | 	release_mem_region(__PREG(SSCR0_P(dev->port)), 0x2c); | 
 | 298 | 	use_count[dev->port - 1]--; | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 299 | 	mutex_unlock(&mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } | 
 | 301 |  | 
 | 302 | EXPORT_SYMBOL(ssp_write_word); | 
 | 303 | EXPORT_SYMBOL(ssp_read_word); | 
 | 304 | EXPORT_SYMBOL(ssp_flush); | 
 | 305 | EXPORT_SYMBOL(ssp_enable); | 
 | 306 | EXPORT_SYMBOL(ssp_disable); | 
 | 307 | EXPORT_SYMBOL(ssp_save_state); | 
 | 308 | EXPORT_SYMBOL(ssp_restore_state); | 
 | 309 | EXPORT_SYMBOL(ssp_init); | 
 | 310 | EXPORT_SYMBOL(ssp_exit); | 
 | 311 | EXPORT_SYMBOL(ssp_config); | 
 | 312 |  | 
 | 313 | MODULE_DESCRIPTION("PXA SSP driver"); | 
 | 314 | MODULE_AUTHOR("Liam Girdwood"); | 
 | 315 | MODULE_LICENSE("GPL"); | 
 | 316 |  |