ssp.c 7.2 KB

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