dma-sh7760.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // SH7760 ("camelot") DMABRG audio DMA unit support
  4. //
  5. // Copyright (C) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
  6. //
  7. // The SH7760 DMABRG provides 4 dma channels (2x rec, 2x play), which
  8. // trigger an interrupt when one half of the programmed transfer size
  9. // has been xmitted.
  10. //
  11. // FIXME: little-endian only for now
  12. #include <linux/module.h>
  13. #include <linux/gfp.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <asm/dmabrg.h>
  22. /* registers and bits */
  23. #define BRGATXSAR 0x00
  24. #define BRGARXDAR 0x04
  25. #define BRGATXTCR 0x08
  26. #define BRGARXTCR 0x0C
  27. #define BRGACR 0x10
  28. #define BRGATXTCNT 0x14
  29. #define BRGARXTCNT 0x18
  30. #define ACR_RAR (1 << 18)
  31. #define ACR_RDS (1 << 17)
  32. #define ACR_RDE (1 << 16)
  33. #define ACR_TAR (1 << 2)
  34. #define ACR_TDS (1 << 1)
  35. #define ACR_TDE (1 << 0)
  36. /* receiver/transmitter data alignment */
  37. #define ACR_RAM_NONE (0 << 24)
  38. #define ACR_RAM_4BYTE (1 << 24)
  39. #define ACR_RAM_2WORD (2 << 24)
  40. #define ACR_TAM_NONE (0 << 8)
  41. #define ACR_TAM_4BYTE (1 << 8)
  42. #define ACR_TAM_2WORD (2 << 8)
  43. struct camelot_pcm {
  44. unsigned long mmio; /* DMABRG audio channel control reg MMIO */
  45. unsigned int txid; /* ID of first DMABRG IRQ for this unit */
  46. struct snd_pcm_substream *tx_ss;
  47. unsigned long tx_period_size;
  48. unsigned int tx_period;
  49. struct snd_pcm_substream *rx_ss;
  50. unsigned long rx_period_size;
  51. unsigned int rx_period;
  52. } cam_pcm_data[2] = {
  53. {
  54. .mmio = 0xFE3C0040,
  55. .txid = DMABRGIRQ_A0TXF,
  56. },
  57. {
  58. .mmio = 0xFE3C0060,
  59. .txid = DMABRGIRQ_A1TXF,
  60. },
  61. };
  62. #define BRGREG(x) (*(unsigned long *)(cam->mmio + (x)))
  63. /*
  64. * set a minimum of 16kb per period, to avoid interrupt-"storm" and
  65. * resulting skipping. In general, the bigger the minimum size, the
  66. * better for overall system performance. (The SH7760 is a puny CPU
  67. * with a slow SDRAM interface and poor internal bus bandwidth,
  68. * *especially* when the LCDC is active). The minimum for the DMAC
  69. * is 8 bytes; 16kbytes are enough to get skip-free playback of a
  70. * 44kHz/16bit/stereo MP3 on a lightly loaded system, and maintain
  71. * reasonable responsiveness in MPlayer.
  72. */
  73. #define DMABRG_PERIOD_MIN 16 * 1024
  74. #define DMABRG_PERIOD_MAX 0x03fffffc
  75. #define DMABRG_PREALLOC_BUFFER 32 * 1024
  76. #define DMABRG_PREALLOC_BUFFER_MAX 32 * 1024
  77. static const struct snd_pcm_hardware camelot_pcm_hardware = {
  78. .info = (SNDRV_PCM_INFO_MMAP |
  79. SNDRV_PCM_INFO_INTERLEAVED |
  80. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  81. SNDRV_PCM_INFO_MMAP_VALID |
  82. SNDRV_PCM_INFO_BATCH),
  83. .buffer_bytes_max = DMABRG_PERIOD_MAX,
  84. .period_bytes_min = DMABRG_PERIOD_MIN,
  85. .period_bytes_max = DMABRG_PERIOD_MAX / 2,
  86. .periods_min = 2,
  87. .periods_max = 2,
  88. .fifo_size = 128,
  89. };
  90. static void camelot_txdma(void *data)
  91. {
  92. struct camelot_pcm *cam = data;
  93. cam->tx_period ^= 1;
  94. snd_pcm_period_elapsed(cam->tx_ss);
  95. }
  96. static void camelot_rxdma(void *data)
  97. {
  98. struct camelot_pcm *cam = data;
  99. cam->rx_period ^= 1;
  100. snd_pcm_period_elapsed(cam->rx_ss);
  101. }
  102. static int camelot_pcm_open(struct snd_pcm_substream *substream)
  103. {
  104. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  105. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  106. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  107. int ret, dmairq;
  108. snd_soc_set_runtime_hwparams(substream, &camelot_pcm_hardware);
  109. /* DMABRG buffer half/full events */
  110. dmairq = (recv) ? cam->txid + 2 : cam->txid;
  111. if (recv) {
  112. cam->rx_ss = substream;
  113. ret = dmabrg_request_irq(dmairq, camelot_rxdma, cam);
  114. if (unlikely(ret)) {
  115. pr_debug("audio unit %d irqs already taken!\n",
  116. rtd->cpu_dai->id);
  117. return -EBUSY;
  118. }
  119. (void)dmabrg_request_irq(dmairq + 1,camelot_rxdma, cam);
  120. } else {
  121. cam->tx_ss = substream;
  122. ret = dmabrg_request_irq(dmairq, camelot_txdma, cam);
  123. if (unlikely(ret)) {
  124. pr_debug("audio unit %d irqs already taken!\n",
  125. rtd->cpu_dai->id);
  126. return -EBUSY;
  127. }
  128. (void)dmabrg_request_irq(dmairq + 1, camelot_txdma, cam);
  129. }
  130. return 0;
  131. }
  132. static int camelot_pcm_close(struct snd_pcm_substream *substream)
  133. {
  134. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  135. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  136. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  137. int dmairq;
  138. dmairq = (recv) ? cam->txid + 2 : cam->txid;
  139. if (recv)
  140. cam->rx_ss = NULL;
  141. else
  142. cam->tx_ss = NULL;
  143. dmabrg_free_irq(dmairq + 1);
  144. dmabrg_free_irq(dmairq);
  145. return 0;
  146. }
  147. static int camelot_hw_params(struct snd_pcm_substream *substream,
  148. struct snd_pcm_hw_params *hw_params)
  149. {
  150. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  151. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  152. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  153. int ret;
  154. ret = snd_pcm_lib_malloc_pages(substream,
  155. params_buffer_bytes(hw_params));
  156. if (ret < 0)
  157. return ret;
  158. if (recv) {
  159. cam->rx_period_size = params_period_bytes(hw_params);
  160. cam->rx_period = 0;
  161. } else {
  162. cam->tx_period_size = params_period_bytes(hw_params);
  163. cam->tx_period = 0;
  164. }
  165. return 0;
  166. }
  167. static int camelot_hw_free(struct snd_pcm_substream *substream)
  168. {
  169. return snd_pcm_lib_free_pages(substream);
  170. }
  171. static int camelot_prepare(struct snd_pcm_substream *substream)
  172. {
  173. struct snd_pcm_runtime *runtime = substream->runtime;
  174. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  175. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  176. pr_debug("PCM data: addr 0x%08ulx len %d\n",
  177. (u32)runtime->dma_addr, runtime->dma_bytes);
  178. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  179. BRGREG(BRGATXSAR) = (unsigned long)runtime->dma_area;
  180. BRGREG(BRGATXTCR) = runtime->dma_bytes;
  181. } else {
  182. BRGREG(BRGARXDAR) = (unsigned long)runtime->dma_area;
  183. BRGREG(BRGARXTCR) = runtime->dma_bytes;
  184. }
  185. return 0;
  186. }
  187. static inline void dmabrg_play_dma_start(struct camelot_pcm *cam)
  188. {
  189. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  190. /* start DMABRG engine: XFER start, auto-addr-reload */
  191. BRGREG(BRGACR) = acr | ACR_TDE | ACR_TAR | ACR_TAM_2WORD;
  192. }
  193. static inline void dmabrg_play_dma_stop(struct camelot_pcm *cam)
  194. {
  195. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  196. /* forcibly terminate data transmission */
  197. BRGREG(BRGACR) = acr | ACR_TDS;
  198. }
  199. static inline void dmabrg_rec_dma_start(struct camelot_pcm *cam)
  200. {
  201. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  202. /* start DMABRG engine: recv start, auto-reload */
  203. BRGREG(BRGACR) = acr | ACR_RDE | ACR_RAR | ACR_RAM_2WORD;
  204. }
  205. static inline void dmabrg_rec_dma_stop(struct camelot_pcm *cam)
  206. {
  207. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  208. /* forcibly terminate data receiver */
  209. BRGREG(BRGACR) = acr | ACR_RDS;
  210. }
  211. static int camelot_trigger(struct snd_pcm_substream *substream, int cmd)
  212. {
  213. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  214. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  215. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  216. switch (cmd) {
  217. case SNDRV_PCM_TRIGGER_START:
  218. if (recv)
  219. dmabrg_rec_dma_start(cam);
  220. else
  221. dmabrg_play_dma_start(cam);
  222. break;
  223. case SNDRV_PCM_TRIGGER_STOP:
  224. if (recv)
  225. dmabrg_rec_dma_stop(cam);
  226. else
  227. dmabrg_play_dma_stop(cam);
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. return 0;
  233. }
  234. static snd_pcm_uframes_t camelot_pos(struct snd_pcm_substream *substream)
  235. {
  236. struct snd_pcm_runtime *runtime = substream->runtime;
  237. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  238. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  239. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  240. unsigned long pos;
  241. /* cannot use the DMABRG pointer register: under load, by the
  242. * time ALSA comes around to read the register, it is already
  243. * far ahead (or worse, already done with the fragment) of the
  244. * position at the time the IRQ was triggered, which results in
  245. * fast-playback sound in my test application (ScummVM)
  246. */
  247. if (recv)
  248. pos = cam->rx_period ? cam->rx_period_size : 0;
  249. else
  250. pos = cam->tx_period ? cam->tx_period_size : 0;
  251. return bytes_to_frames(runtime, pos);
  252. }
  253. static const struct snd_pcm_ops camelot_pcm_ops = {
  254. .open = camelot_pcm_open,
  255. .close = camelot_pcm_close,
  256. .ioctl = snd_pcm_lib_ioctl,
  257. .hw_params = camelot_hw_params,
  258. .hw_free = camelot_hw_free,
  259. .prepare = camelot_prepare,
  260. .trigger = camelot_trigger,
  261. .pointer = camelot_pos,
  262. };
  263. static int camelot_pcm_new(struct snd_soc_pcm_runtime *rtd)
  264. {
  265. struct snd_pcm *pcm = rtd->pcm;
  266. /* dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  267. * in MMAP mode (i.e. aplay -M)
  268. */
  269. snd_pcm_lib_preallocate_pages_for_all(pcm,
  270. SNDRV_DMA_TYPE_CONTINUOUS,
  271. snd_dma_continuous_data(GFP_KERNEL),
  272. DMABRG_PREALLOC_BUFFER, DMABRG_PREALLOC_BUFFER_MAX);
  273. return 0;
  274. }
  275. static const struct snd_soc_component_driver sh7760_soc_component = {
  276. .ops = &camelot_pcm_ops,
  277. .pcm_new = camelot_pcm_new,
  278. };
  279. static int sh7760_soc_platform_probe(struct platform_device *pdev)
  280. {
  281. return devm_snd_soc_register_component(&pdev->dev, &sh7760_soc_component,
  282. NULL, 0);
  283. }
  284. static struct platform_driver sh7760_pcm_driver = {
  285. .driver = {
  286. .name = "sh7760-pcm-audio",
  287. },
  288. .probe = sh7760_soc_platform_probe,
  289. };
  290. module_platform_driver(sh7760_pcm_driver);
  291. MODULE_LICENSE("GPL v2");
  292. MODULE_DESCRIPTION("SH7760 Audio DMA (DMABRG) driver");
  293. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");