ssp.c 7.6 KB

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