ssp.c 7.3 KB

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