ssp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.
  22. * 4th Aug 2005 Added option to disable irq handler registration and
  23. * cleaned up irq and clock detection.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/errno.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/ioport.h>
  32. #include <linux/init.h>
  33. #include <linux/mutex.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/hardware.h>
  37. #include <asm/arch/ssp.h>
  38. #include <asm/arch/pxa-regs.h>
  39. #define PXA_SSP_PORTS 3
  40. #define TIMEOUT 100000
  41. struct ssp_info_ {
  42. int irq;
  43. u32 clock;
  44. };
  45. /*
  46. * SSP port clock and IRQ settings
  47. */
  48. static const struct ssp_info_ ssp_info[PXA_SSP_PORTS] = {
  49. #if defined (CONFIG_PXA27x)
  50. {IRQ_SSP, CKEN23_SSP1},
  51. {IRQ_SSP2, CKEN3_SSP2},
  52. {IRQ_SSP3, CKEN4_SSP3},
  53. #else
  54. {IRQ_SSP, CKEN3_SSP},
  55. {IRQ_NSSP, CKEN9_NSSP},
  56. {IRQ_ASSP, CKEN10_ASSP},
  57. #endif
  58. };
  59. static DEFINE_MUTEX(mutex);
  60. static int use_count[PXA_SSP_PORTS] = {0, 0, 0};
  61. static irqreturn_t ssp_interrupt(int irq, void *dev_id)
  62. {
  63. struct ssp_dev *dev = (struct ssp_dev*) dev_id;
  64. unsigned int status = SSSR_P(dev->port);
  65. SSSR_P(dev->port) = status; /* clear status bits */
  66. if (status & SSSR_ROR)
  67. printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
  68. if (status & SSSR_TUR)
  69. printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
  70. if (status & SSSR_BCE)
  71. printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
  72. return IRQ_HANDLED;
  73. }
  74. /**
  75. * ssp_write_word - write a word to the SSP port
  76. * @data: 32-bit, MSB justified data to write.
  77. *
  78. * Wait for a free entry in the SSP transmit FIFO, and write a data
  79. * word to the SSP port.
  80. *
  81. * The caller is expected to perform the necessary locking.
  82. *
  83. * Returns:
  84. * %-ETIMEDOUT timeout occurred
  85. * 0 success
  86. */
  87. int ssp_write_word(struct ssp_dev *dev, u32 data)
  88. {
  89. int timeout = TIMEOUT;
  90. while (!(SSSR_P(dev->port) & SSSR_TNF)) {
  91. if (!--timeout)
  92. return -ETIMEDOUT;
  93. cpu_relax();
  94. }
  95. SSDR_P(dev->port) = data;
  96. return 0;
  97. }
  98. /**
  99. * ssp_read_word - read a word from the SSP port
  100. *
  101. * Wait for a data word in the SSP receive FIFO, and return the
  102. * received data. Data is LSB justified.
  103. *
  104. * Note: Currently, if data is not expected to be received, this
  105. * function will wait for ever.
  106. *
  107. * The caller is expected to perform the necessary locking.
  108. *
  109. * Returns:
  110. * %-ETIMEDOUT timeout occurred
  111. * 32-bit data success
  112. */
  113. int ssp_read_word(struct ssp_dev *dev, u32 *data)
  114. {
  115. int timeout = TIMEOUT;
  116. while (!(SSSR_P(dev->port) & SSSR_RNE)) {
  117. if (!--timeout)
  118. return -ETIMEDOUT;
  119. cpu_relax();
  120. }
  121. *data = SSDR_P(dev->port);
  122. return 0;
  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. */
  132. int ssp_flush(struct ssp_dev *dev)
  133. {
  134. int timeout = TIMEOUT * 2;
  135. do {
  136. while (SSSR_P(dev->port) & SSSR_RNE) {
  137. if (!--timeout)
  138. return -ETIMEDOUT;
  139. (void) SSDR_P(dev->port);
  140. }
  141. if (!--timeout)
  142. return -ETIMEDOUT;
  143. } while (SSSR_P(dev->port) & SSSR_BSY);
  144. return 0;
  145. }
  146. /**
  147. * ssp_enable - enable the SSP port
  148. *
  149. * Turn on the SSP port.
  150. */
  151. void ssp_enable(struct ssp_dev *dev)
  152. {
  153. SSCR0_P(dev->port) |= SSCR0_SSE;
  154. }
  155. /**
  156. * ssp_disable - shut down the SSP port
  157. *
  158. * Turn off the SSP port, optionally powering it down.
  159. */
  160. void ssp_disable(struct ssp_dev *dev)
  161. {
  162. SSCR0_P(dev->port) &= ~SSCR0_SSE;
  163. }
  164. /**
  165. * ssp_save_state - save the SSP configuration
  166. * @ssp: pointer to structure to save SSP configuration
  167. *
  168. * Save the configured SSP state for suspend.
  169. */
  170. void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
  171. {
  172. ssp->cr0 = SSCR0_P(dev->port);
  173. ssp->cr1 = SSCR1_P(dev->port);
  174. ssp->to = SSTO_P(dev->port);
  175. ssp->psp = SSPSP_P(dev->port);
  176. SSCR0_P(dev->port) &= ~SSCR0_SSE;
  177. }
  178. /**
  179. * ssp_restore_state - restore a previously saved SSP configuration
  180. * @ssp: pointer to configuration saved by ssp_save_state
  181. *
  182. * Restore the SSP configuration saved previously by ssp_save_state.
  183. */
  184. void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
  185. {
  186. SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
  187. SSCR0_P(dev->port) = ssp->cr0 & ~SSCR0_SSE;
  188. SSCR1_P(dev->port) = ssp->cr1;
  189. SSTO_P(dev->port) = ssp->to;
  190. SSPSP_P(dev->port) = ssp->psp;
  191. SSCR0_P(dev->port) = ssp->cr0;
  192. }
  193. /**
  194. * ssp_config - configure SSP port settings
  195. * @mode: port operating mode
  196. * @flags: port config flags
  197. * @psp_flags: port PSP config flags
  198. * @speed: port speed
  199. *
  200. * Port MUST be disabled by ssp_disable before making any config changes.
  201. */
  202. int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
  203. {
  204. dev->mode = mode;
  205. dev->flags = flags;
  206. dev->psp_flags = psp_flags;
  207. dev->speed = speed;
  208. /* set up port type, speed, port settings */
  209. SSCR0_P(dev->port) = (dev->speed | dev->mode);
  210. SSCR1_P(dev->port) = dev->flags;
  211. SSPSP_P(dev->port) = dev->psp_flags;
  212. return 0;
  213. }
  214. /**
  215. * ssp_init - setup the SSP port
  216. *
  217. * initialise and claim resources for the SSP port.
  218. *
  219. * Returns:
  220. * %-ENODEV if the SSP port is unavailable
  221. * %-EBUSY if the resources are already in use
  222. * %0 on success
  223. */
  224. int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
  225. {
  226. int ret;
  227. if (port > PXA_SSP_PORTS || port == 0)
  228. return -ENODEV;
  229. mutex_lock(&mutex);
  230. if (use_count[port - 1]) {
  231. mutex_unlock(&mutex);
  232. return -EBUSY;
  233. }
  234. use_count[port - 1]++;
  235. if (!request_mem_region(__PREG(SSCR0_P(port)), 0x2c, "SSP")) {
  236. use_count[port - 1]--;
  237. mutex_unlock(&mutex);
  238. return -EBUSY;
  239. }
  240. dev->port = port;
  241. /* do we need to get irq */
  242. if (!(init_flags & SSP_NO_IRQ)) {
  243. ret = request_irq(ssp_info[port-1].irq, ssp_interrupt,
  244. 0, "SSP", dev);
  245. if (ret)
  246. goto out_region;
  247. dev->irq = ssp_info[port-1].irq;
  248. } else
  249. dev->irq = 0;
  250. /* turn on SSP port clock */
  251. pxa_set_cken(ssp_info[port-1].clock, 1);
  252. mutex_unlock(&mutex);
  253. return 0;
  254. out_region:
  255. release_mem_region(__PREG(SSCR0_P(port)), 0x2c);
  256. use_count[port - 1]--;
  257. mutex_unlock(&mutex);
  258. return ret;
  259. }
  260. /**
  261. * ssp_exit - undo the effects of ssp_init
  262. *
  263. * release and free resources for the SSP port.
  264. */
  265. void ssp_exit(struct ssp_dev *dev)
  266. {
  267. mutex_lock(&mutex);
  268. SSCR0_P(dev->port) &= ~SSCR0_SSE;
  269. if (dev->port > PXA_SSP_PORTS || dev->port == 0) {
  270. printk(KERN_WARNING "SSP: tried to close invalid port\n");
  271. return;
  272. }
  273. pxa_set_cken(ssp_info[dev->port-1].clock, 0);
  274. if (dev->irq)
  275. free_irq(dev->irq, dev);
  276. release_mem_region(__PREG(SSCR0_P(dev->port)), 0x2c);
  277. use_count[dev->port - 1]--;
  278. mutex_unlock(&mutex);
  279. }
  280. EXPORT_SYMBOL(ssp_write_word);
  281. EXPORT_SYMBOL(ssp_read_word);
  282. EXPORT_SYMBOL(ssp_flush);
  283. EXPORT_SYMBOL(ssp_enable);
  284. EXPORT_SYMBOL(ssp_disable);
  285. EXPORT_SYMBOL(ssp_save_state);
  286. EXPORT_SYMBOL(ssp_restore_state);
  287. EXPORT_SYMBOL(ssp_init);
  288. EXPORT_SYMBOL(ssp_exit);
  289. EXPORT_SYMBOL(ssp_config);
  290. MODULE_DESCRIPTION("PXA SSP driver");
  291. MODULE_AUTHOR("Liam Girdwood");
  292. MODULE_LICENSE("GPL");