ssp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  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. #include <asm/io.h>
  21. #include <asm/irq.h>
  22. #include <asm/hardware.h>
  23. #include <asm/hardware/ssp.h>
  24. #define TIMEOUT 100000
  25. static irqreturn_t ssp_interrupt(int irq, void *dev_id)
  26. {
  27. unsigned int status = Ser4SSSR;
  28. if (status & SSSR_ROR) {
  29. printk(KERN_WARNING "SSP: receiver overrun\n");
  30. }
  31. Ser4SSSR = SSSR_ROR;
  32. return status ? IRQ_HANDLED : IRQ_NONE;
  33. }
  34. /**
  35. * ssp_write_word - write a word to the SSP port
  36. * @data: 16-bit, MSB justified data to write.
  37. *
  38. * Wait for a free entry in the SSP transmit FIFO, and write a data
  39. * word to the SSP port. Wait for the SSP port to start sending
  40. * the data.
  41. *
  42. * The caller is expected to perform the necessary locking.
  43. *
  44. * Returns:
  45. * %-ETIMEDOUT timeout occurred
  46. * 0 success
  47. */
  48. int ssp_write_word(u16 data)
  49. {
  50. int timeout = TIMEOUT;
  51. while (!(Ser4SSSR & SSSR_TNF)) {
  52. if (!--timeout)
  53. return -ETIMEDOUT;
  54. cpu_relax();
  55. }
  56. Ser4SSDR = data;
  57. timeout = TIMEOUT;
  58. while (!(Ser4SSSR & SSSR_BSY)) {
  59. if (!--timeout)
  60. return -ETIMEDOUT;
  61. cpu_relax();
  62. }
  63. return 0;
  64. }
  65. /**
  66. * ssp_read_word - read a word from the SSP port
  67. *
  68. * Wait for a data word in the SSP receive FIFO, and return the
  69. * received data. Data is LSB justified.
  70. *
  71. * Note: Currently, if data is not expected to be received, this
  72. * function will wait for ever.
  73. *
  74. * The caller is expected to perform the necessary locking.
  75. *
  76. * Returns:
  77. * %-ETIMEDOUT timeout occurred
  78. * 16-bit data success
  79. */
  80. int ssp_read_word(u16 *data)
  81. {
  82. int timeout = TIMEOUT;
  83. while (!(Ser4SSSR & SSSR_RNE)) {
  84. if (!--timeout)
  85. return -ETIMEDOUT;
  86. cpu_relax();
  87. }
  88. *data = (u16)Ser4SSDR;
  89. return 0;
  90. }
  91. /**
  92. * ssp_flush - flush the transmit and receive FIFOs
  93. *
  94. * Wait for the SSP to idle, and ensure that the receive FIFO
  95. * is empty.
  96. *
  97. * The caller is expected to perform the necessary locking.
  98. *
  99. * Returns:
  100. * %-ETIMEDOUT timeout occurred
  101. * 0 success
  102. */
  103. int ssp_flush(void)
  104. {
  105. int timeout = TIMEOUT * 2;
  106. do {
  107. while (Ser4SSSR & SSSR_RNE) {
  108. if (!--timeout)
  109. return -ETIMEDOUT;
  110. (void) Ser4SSDR;
  111. }
  112. if (!--timeout)
  113. return -ETIMEDOUT;
  114. } while (Ser4SSSR & SSSR_BSY);
  115. return 0;
  116. }
  117. /**
  118. * ssp_enable - enable the SSP port
  119. *
  120. * Turn on the SSP port.
  121. */
  122. void ssp_enable(void)
  123. {
  124. Ser4SSCR0 |= SSCR0_SSE;
  125. }
  126. /**
  127. * ssp_disable - shut down the SSP port
  128. *
  129. * Turn off the SSP port, optionally powering it down.
  130. */
  131. void ssp_disable(void)
  132. {
  133. Ser4SSCR0 &= ~SSCR0_SSE;
  134. }
  135. /**
  136. * ssp_save_state - save the SSP configuration
  137. * @ssp: pointer to structure to save SSP configuration
  138. *
  139. * Save the configured SSP state for suspend.
  140. */
  141. void ssp_save_state(struct ssp_state *ssp)
  142. {
  143. ssp->cr0 = Ser4SSCR0;
  144. ssp->cr1 = Ser4SSCR1;
  145. Ser4SSCR0 &= ~SSCR0_SSE;
  146. }
  147. /**
  148. * ssp_restore_state - restore a previously saved SSP configuration
  149. * @ssp: pointer to configuration saved by ssp_save_state
  150. *
  151. * Restore the SSP configuration saved previously by ssp_save_state.
  152. */
  153. void ssp_restore_state(struct ssp_state *ssp)
  154. {
  155. Ser4SSSR = SSSR_ROR;
  156. Ser4SSCR0 = ssp->cr0 & ~SSCR0_SSE;
  157. Ser4SSCR1 = ssp->cr1;
  158. Ser4SSCR0 = ssp->cr0;
  159. }
  160. /**
  161. * ssp_init - setup the SSP port
  162. *
  163. * initialise and claim resources for the SSP port.
  164. *
  165. * Returns:
  166. * %-ENODEV if the SSP port is unavailable
  167. * %-EBUSY if the resources are already in use
  168. * %0 on success
  169. */
  170. int ssp_init(void)
  171. {
  172. int ret;
  173. if (!(PPAR & PPAR_SPR) && (Ser4MCCR0 & MCCR0_MCE))
  174. return -ENODEV;
  175. if (!request_mem_region(__PREG(Ser4SSCR0), 0x18, "SSP")) {
  176. return -EBUSY;
  177. }
  178. Ser4SSSR = SSSR_ROR;
  179. ret = request_irq(IRQ_Ser4SSP, ssp_interrupt, 0, "SSP", NULL);
  180. if (ret)
  181. goto out_region;
  182. return 0;
  183. out_region:
  184. release_mem_region(__PREG(Ser4SSCR0), 0x18);
  185. return ret;
  186. }
  187. /**
  188. * ssp_exit - undo the effects of ssp_init
  189. *
  190. * release and free resources for the SSP port.
  191. */
  192. void ssp_exit(void)
  193. {
  194. Ser4SSCR0 &= ~SSCR0_SSE;
  195. free_irq(IRQ_Ser4SSP, NULL);
  196. release_mem_region(__PREG(Ser4SSCR0), 0x18);
  197. }
  198. MODULE_AUTHOR("Russell King");
  199. MODULE_DESCRIPTION("SA11x0 SSP PIO driver");
  200. MODULE_LICENSE("GPL");
  201. EXPORT_SYMBOL(ssp_write_word);
  202. EXPORT_SYMBOL(ssp_read_word);
  203. EXPORT_SYMBOL(ssp_flush);
  204. EXPORT_SYMBOL(ssp_enable);
  205. EXPORT_SYMBOL(ssp_disable);
  206. EXPORT_SYMBOL(ssp_save_state);
  207. EXPORT_SYMBOL(ssp_restore_state);
  208. EXPORT_SYMBOL(ssp_init);
  209. EXPORT_SYMBOL(ssp_exit);