mpc5200_dma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Freescale MPC5200 PSC DMA
  3. * ALSA SoC Platform driver
  4. *
  5. * Copyright (C) 2008 Secret Lab Technologies Ltd.
  6. * Copyright (C) 2009 Jon Smirl, Digispeaker
  7. */
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/of_platform.h>
  12. #include <sound/soc.h>
  13. #include <sysdev/bestcomm/bestcomm.h>
  14. #include <sysdev/bestcomm/gen_bd.h>
  15. #include <asm/mpc52xx_psc.h>
  16. #include "mpc5200_dma.h"
  17. /*
  18. * Interrupt handlers
  19. */
  20. static irqreturn_t psc_dma_status_irq(int irq, void *_psc_dma)
  21. {
  22. struct psc_dma *psc_dma = _psc_dma;
  23. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  24. u16 isr;
  25. isr = in_be16(&regs->mpc52xx_psc_isr);
  26. /* Playback underrun error */
  27. if (psc_dma->playback.active && (isr & MPC52xx_PSC_IMR_TXEMP))
  28. psc_dma->stats.underrun_count++;
  29. /* Capture overrun error */
  30. if (psc_dma->capture.active && (isr & MPC52xx_PSC_IMR_ORERR))
  31. psc_dma->stats.overrun_count++;
  32. out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
  33. return IRQ_HANDLED;
  34. }
  35. /**
  36. * psc_dma_bcom_enqueue_next_buffer - Enqueue another audio buffer
  37. * @s: pointer to stream private data structure
  38. *
  39. * Enqueues another audio period buffer into the bestcomm queue.
  40. *
  41. * Note: The routine must only be called when there is space available in
  42. * the queue. Otherwise the enqueue will fail and the audio ring buffer
  43. * will get out of sync
  44. */
  45. static void psc_dma_bcom_enqueue_next_buffer(struct psc_dma_stream *s)
  46. {
  47. struct bcom_bd *bd;
  48. /* Prepare and enqueue the next buffer descriptor */
  49. bd = bcom_prepare_next_buffer(s->bcom_task);
  50. bd->status = s->period_bytes;
  51. bd->data[0] = s->runtime->dma_addr + (s->period_next * s->period_bytes);
  52. bcom_submit_next_buffer(s->bcom_task, NULL);
  53. /* Update for next period */
  54. s->period_next = (s->period_next + 1) % s->runtime->periods;
  55. }
  56. /* Bestcomm DMA irq handler */
  57. static irqreturn_t psc_dma_bcom_irq(int irq, void *_psc_dma_stream)
  58. {
  59. struct psc_dma_stream *s = _psc_dma_stream;
  60. spin_lock(&s->psc_dma->lock);
  61. /* For each finished period, dequeue the completed period buffer
  62. * and enqueue a new one in it's place. */
  63. while (bcom_buffer_done(s->bcom_task)) {
  64. bcom_retrieve_buffer(s->bcom_task, NULL, NULL);
  65. s->period_current = (s->period_current+1) % s->runtime->periods;
  66. s->period_count++;
  67. psc_dma_bcom_enqueue_next_buffer(s);
  68. }
  69. spin_unlock(&s->psc_dma->lock);
  70. /* If the stream is active, then also inform the PCM middle layer
  71. * of the period finished event. */
  72. if (s->active)
  73. snd_pcm_period_elapsed(s->stream);
  74. return IRQ_HANDLED;
  75. }
  76. static int psc_dma_hw_free(struct snd_pcm_substream *substream)
  77. {
  78. snd_pcm_set_runtime_buffer(substream, NULL);
  79. return 0;
  80. }
  81. /**
  82. * psc_dma_trigger: start and stop the DMA transfer.
  83. *
  84. * This function is called by ALSA to start, stop, pause, and resume the DMA
  85. * transfer of data.
  86. */
  87. static int psc_dma_trigger(struct snd_pcm_substream *substream, int cmd)
  88. {
  89. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  90. struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  91. struct snd_pcm_runtime *runtime = substream->runtime;
  92. struct psc_dma_stream *s = to_psc_dma_stream(substream, psc_dma);
  93. struct mpc52xx_psc __iomem *regs = psc_dma->psc_regs;
  94. u16 imr;
  95. unsigned long flags;
  96. int i;
  97. switch (cmd) {
  98. case SNDRV_PCM_TRIGGER_START:
  99. dev_dbg(psc_dma->dev, "START: stream=%i fbits=%u ps=%u #p=%u\n",
  100. substream->pstr->stream, runtime->frame_bits,
  101. (int)runtime->period_size, runtime->periods);
  102. s->period_bytes = frames_to_bytes(runtime,
  103. runtime->period_size);
  104. s->period_next = 0;
  105. s->period_current = 0;
  106. s->active = 1;
  107. s->period_count = 0;
  108. s->runtime = runtime;
  109. /* Fill up the bestcomm bd queue and enable DMA.
  110. * This will begin filling the PSC's fifo.
  111. */
  112. spin_lock_irqsave(&psc_dma->lock, flags);
  113. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  114. bcom_gen_bd_rx_reset(s->bcom_task);
  115. else
  116. bcom_gen_bd_tx_reset(s->bcom_task);
  117. for (i = 0; i < runtime->periods; i++)
  118. if (!bcom_queue_full(s->bcom_task))
  119. psc_dma_bcom_enqueue_next_buffer(s);
  120. bcom_enable(s->bcom_task);
  121. spin_unlock_irqrestore(&psc_dma->lock, flags);
  122. out_8(&regs->command, MPC52xx_PSC_RST_ERR_STAT);
  123. break;
  124. case SNDRV_PCM_TRIGGER_STOP:
  125. dev_dbg(psc_dma->dev, "STOP: stream=%i periods_count=%i\n",
  126. substream->pstr->stream, s->period_count);
  127. s->active = 0;
  128. spin_lock_irqsave(&psc_dma->lock, flags);
  129. bcom_disable(s->bcom_task);
  130. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  131. bcom_gen_bd_rx_reset(s->bcom_task);
  132. else
  133. bcom_gen_bd_tx_reset(s->bcom_task);
  134. spin_unlock_irqrestore(&psc_dma->lock, flags);
  135. break;
  136. default:
  137. dev_dbg(psc_dma->dev, "unhandled trigger: stream=%i cmd=%i\n",
  138. substream->pstr->stream, cmd);
  139. return -EINVAL;
  140. }
  141. /* Update interrupt enable settings */
  142. imr = 0;
  143. if (psc_dma->playback.active)
  144. imr |= MPC52xx_PSC_IMR_TXEMP;
  145. if (psc_dma->capture.active)
  146. imr |= MPC52xx_PSC_IMR_ORERR;
  147. out_be16(&regs->isr_imr.imr, psc_dma->imr | imr);
  148. return 0;
  149. }
  150. /* ---------------------------------------------------------------------
  151. * The PSC DMA 'ASoC platform' driver
  152. *
  153. * Can be referenced by an 'ASoC machine' driver
  154. * This driver only deals with the audio bus; it doesn't have any
  155. * interaction with the attached codec
  156. */
  157. static const struct snd_pcm_hardware psc_dma_hardware = {
  158. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  159. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  160. SNDRV_PCM_INFO_BATCH,
  161. .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_BE |
  162. SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S32_BE,
  163. .rate_min = 8000,
  164. .rate_max = 48000,
  165. .channels_min = 1,
  166. .channels_max = 2,
  167. .period_bytes_max = 1024 * 1024,
  168. .period_bytes_min = 32,
  169. .periods_min = 2,
  170. .periods_max = 256,
  171. .buffer_bytes_max = 2 * 1024 * 1024,
  172. .fifo_size = 512,
  173. };
  174. static int psc_dma_open(struct snd_pcm_substream *substream)
  175. {
  176. struct snd_pcm_runtime *runtime = substream->runtime;
  177. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  178. struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  179. struct psc_dma_stream *s;
  180. int rc;
  181. dev_dbg(psc_dma->dev, "psc_dma_open(substream=%p)\n", substream);
  182. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  183. s = &psc_dma->capture;
  184. else
  185. s = &psc_dma->playback;
  186. snd_soc_set_runtime_hwparams(substream, &psc_dma_hardware);
  187. rc = snd_pcm_hw_constraint_integer(runtime,
  188. SNDRV_PCM_HW_PARAM_PERIODS);
  189. if (rc < 0) {
  190. dev_err(substream->pcm->card->dev, "invalid buffer size\n");
  191. return rc;
  192. }
  193. s->stream = substream;
  194. return 0;
  195. }
  196. static int psc_dma_close(struct snd_pcm_substream *substream)
  197. {
  198. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  199. struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  200. struct psc_dma_stream *s;
  201. dev_dbg(psc_dma->dev, "psc_dma_close(substream=%p)\n", substream);
  202. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  203. s = &psc_dma->capture;
  204. else
  205. s = &psc_dma->playback;
  206. if (!psc_dma->playback.active &&
  207. !psc_dma->capture.active) {
  208. /* Disable all interrupts and reset the PSC */
  209. out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
  210. out_8(&psc_dma->psc_regs->command, 4 << 4); /* reset error */
  211. }
  212. s->stream = NULL;
  213. return 0;
  214. }
  215. static snd_pcm_uframes_t
  216. psc_dma_pointer(struct snd_pcm_substream *substream)
  217. {
  218. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  219. struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  220. struct psc_dma_stream *s;
  221. dma_addr_t count;
  222. if (substream->pstr->stream == SNDRV_PCM_STREAM_CAPTURE)
  223. s = &psc_dma->capture;
  224. else
  225. s = &psc_dma->playback;
  226. count = s->period_current * s->period_bytes;
  227. return bytes_to_frames(substream->runtime, count);
  228. }
  229. static int
  230. psc_dma_hw_params(struct snd_pcm_substream *substream,
  231. struct snd_pcm_hw_params *params)
  232. {
  233. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  234. return 0;
  235. }
  236. static struct snd_pcm_ops psc_dma_ops = {
  237. .open = psc_dma_open,
  238. .close = psc_dma_close,
  239. .hw_free = psc_dma_hw_free,
  240. .ioctl = snd_pcm_lib_ioctl,
  241. .pointer = psc_dma_pointer,
  242. .trigger = psc_dma_trigger,
  243. .hw_params = psc_dma_hw_params,
  244. };
  245. static u64 psc_dma_dmamask = 0xffffffff;
  246. static int psc_dma_new(struct snd_soc_pcm_runtime *rtd)
  247. {
  248. struct snd_card *card = rtd->card->snd_card;
  249. struct snd_soc_dai *dai = rtd->cpu_dai;
  250. struct snd_pcm *pcm = rtd->pcm;
  251. struct psc_dma *psc_dma = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  252. size_t size = psc_dma_hardware.buffer_bytes_max;
  253. int rc = 0;
  254. dev_dbg(rtd->platform->dev, "psc_dma_new(card=%p, dai=%p, pcm=%p)\n",
  255. card, dai, pcm);
  256. if (!card->dev->dma_mask)
  257. card->dev->dma_mask = &psc_dma_dmamask;
  258. if (!card->dev->coherent_dma_mask)
  259. card->dev->coherent_dma_mask = 0xffffffff;
  260. if (pcm->streams[0].substream) {
  261. rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
  262. size, &pcm->streams[0].substream->dma_buffer);
  263. if (rc)
  264. goto playback_alloc_err;
  265. }
  266. if (pcm->streams[1].substream) {
  267. rc = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
  268. size, &pcm->streams[1].substream->dma_buffer);
  269. if (rc)
  270. goto capture_alloc_err;
  271. }
  272. if (rtd->codec->ac97)
  273. rtd->codec->ac97->private_data = psc_dma;
  274. return 0;
  275. capture_alloc_err:
  276. if (pcm->streams[0].substream)
  277. snd_dma_free_pages(&pcm->streams[0].substream->dma_buffer);
  278. playback_alloc_err:
  279. dev_err(card->dev, "Cannot allocate buffer(s)\n");
  280. return -ENOMEM;
  281. }
  282. static void psc_dma_free(struct snd_pcm *pcm)
  283. {
  284. struct snd_soc_pcm_runtime *rtd = pcm->private_data;
  285. struct snd_pcm_substream *substream;
  286. int stream;
  287. dev_dbg(rtd->platform->dev, "psc_dma_free(pcm=%p)\n", pcm);
  288. for (stream = 0; stream < 2; stream++) {
  289. substream = pcm->streams[stream].substream;
  290. if (substream) {
  291. snd_dma_free_pages(&substream->dma_buffer);
  292. substream->dma_buffer.area = NULL;
  293. substream->dma_buffer.addr = 0;
  294. }
  295. }
  296. }
  297. static struct snd_soc_platform_driver mpc5200_audio_dma_platform = {
  298. .ops = &psc_dma_ops,
  299. .pcm_new = &psc_dma_new,
  300. .pcm_free = &psc_dma_free,
  301. };
  302. static int mpc5200_hpcd_probe(struct platform_device *op)
  303. {
  304. phys_addr_t fifo;
  305. struct psc_dma *psc_dma;
  306. struct resource res;
  307. int size, irq, rc;
  308. const __be32 *prop;
  309. void __iomem *regs;
  310. int ret;
  311. /* Fetch the registers and IRQ of the PSC */
  312. irq = irq_of_parse_and_map(op->dev.of_node, 0);
  313. if (of_address_to_resource(op->dev.of_node, 0, &res)) {
  314. dev_err(&op->dev, "Missing reg property\n");
  315. return -ENODEV;
  316. }
  317. regs = ioremap(res.start, resource_size(&res));
  318. if (!regs) {
  319. dev_err(&op->dev, "Could not map registers\n");
  320. return -ENODEV;
  321. }
  322. /* Allocate and initialize the driver private data */
  323. psc_dma = kzalloc(sizeof *psc_dma, GFP_KERNEL);
  324. if (!psc_dma) {
  325. ret = -ENOMEM;
  326. goto out_unmap;
  327. }
  328. /* Get the PSC ID */
  329. prop = of_get_property(op->dev.of_node, "cell-index", &size);
  330. if (!prop || size < sizeof *prop) {
  331. ret = -ENODEV;
  332. goto out_free;
  333. }
  334. spin_lock_init(&psc_dma->lock);
  335. mutex_init(&psc_dma->mutex);
  336. psc_dma->id = be32_to_cpu(*prop);
  337. psc_dma->irq = irq;
  338. psc_dma->psc_regs = regs;
  339. psc_dma->fifo_regs = regs + sizeof *psc_dma->psc_regs;
  340. psc_dma->dev = &op->dev;
  341. psc_dma->playback.psc_dma = psc_dma;
  342. psc_dma->capture.psc_dma = psc_dma;
  343. snprintf(psc_dma->name, sizeof psc_dma->name, "PSC%u", psc_dma->id);
  344. /* Find the address of the fifo data registers and setup the
  345. * DMA tasks */
  346. fifo = res.start + offsetof(struct mpc52xx_psc, buffer.buffer_32);
  347. psc_dma->capture.bcom_task =
  348. bcom_psc_gen_bd_rx_init(psc_dma->id, 10, fifo, 512);
  349. psc_dma->playback.bcom_task =
  350. bcom_psc_gen_bd_tx_init(psc_dma->id, 10, fifo);
  351. if (!psc_dma->capture.bcom_task ||
  352. !psc_dma->playback.bcom_task) {
  353. dev_err(&op->dev, "Could not allocate bestcomm tasks\n");
  354. ret = -ENODEV;
  355. goto out_free;
  356. }
  357. /* Disable all interrupts and reset the PSC */
  358. out_be16(&psc_dma->psc_regs->isr_imr.imr, psc_dma->imr);
  359. /* reset receiver */
  360. out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_RX);
  361. /* reset transmitter */
  362. out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_TX);
  363. /* reset error */
  364. out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_RST_ERR_STAT);
  365. /* reset mode */
  366. out_8(&psc_dma->psc_regs->command, MPC52xx_PSC_SEL_MODE_REG_1);
  367. /* Set up mode register;
  368. * First write: RxRdy (FIFO Alarm) generates rx FIFO irq
  369. * Second write: register Normal mode for non loopback
  370. */
  371. out_8(&psc_dma->psc_regs->mode, 0);
  372. out_8(&psc_dma->psc_regs->mode, 0);
  373. /* Set the TX and RX fifo alarm thresholds */
  374. out_be16(&psc_dma->fifo_regs->rfalarm, 0x100);
  375. out_8(&psc_dma->fifo_regs->rfcntl, 0x4);
  376. out_be16(&psc_dma->fifo_regs->tfalarm, 0x100);
  377. out_8(&psc_dma->fifo_regs->tfcntl, 0x7);
  378. /* Lookup the IRQ numbers */
  379. psc_dma->playback.irq =
  380. bcom_get_task_irq(psc_dma->playback.bcom_task);
  381. psc_dma->capture.irq =
  382. bcom_get_task_irq(psc_dma->capture.bcom_task);
  383. rc = request_irq(psc_dma->irq, &psc_dma_status_irq, IRQF_SHARED,
  384. "psc-dma-status", psc_dma);
  385. rc |= request_irq(psc_dma->capture.irq, &psc_dma_bcom_irq, IRQF_SHARED,
  386. "psc-dma-capture", &psc_dma->capture);
  387. rc |= request_irq(psc_dma->playback.irq, &psc_dma_bcom_irq, IRQF_SHARED,
  388. "psc-dma-playback", &psc_dma->playback);
  389. if (rc) {
  390. ret = -ENODEV;
  391. goto out_irq;
  392. }
  393. /* Save what we've done so it can be found again later */
  394. dev_set_drvdata(&op->dev, psc_dma);
  395. /* Tell the ASoC OF helpers about it */
  396. return snd_soc_register_platform(&op->dev, &mpc5200_audio_dma_platform);
  397. out_irq:
  398. free_irq(psc_dma->irq, psc_dma);
  399. free_irq(psc_dma->capture.irq, &psc_dma->capture);
  400. free_irq(psc_dma->playback.irq, &psc_dma->playback);
  401. out_free:
  402. kfree(psc_dma);
  403. out_unmap:
  404. iounmap(regs);
  405. return ret;
  406. }
  407. static int mpc5200_hpcd_remove(struct platform_device *op)
  408. {
  409. struct psc_dma *psc_dma = dev_get_drvdata(&op->dev);
  410. dev_dbg(&op->dev, "mpc5200_audio_dma_destroy()\n");
  411. snd_soc_unregister_platform(&op->dev);
  412. bcom_gen_bd_rx_release(psc_dma->capture.bcom_task);
  413. bcom_gen_bd_tx_release(psc_dma->playback.bcom_task);
  414. /* Release irqs */
  415. free_irq(psc_dma->irq, psc_dma);
  416. free_irq(psc_dma->capture.irq, &psc_dma->capture);
  417. free_irq(psc_dma->playback.irq, &psc_dma->playback);
  418. iounmap(psc_dma->psc_regs);
  419. kfree(psc_dma);
  420. dev_set_drvdata(&op->dev, NULL);
  421. return 0;
  422. }
  423. static struct of_device_id mpc5200_hpcd_match[] = {
  424. { .compatible = "fsl,mpc5200-pcm", },
  425. {}
  426. };
  427. MODULE_DEVICE_TABLE(of, mpc5200_hpcd_match);
  428. static struct platform_driver mpc5200_hpcd_of_driver = {
  429. .probe = mpc5200_hpcd_probe,
  430. .remove = mpc5200_hpcd_remove,
  431. .driver = {
  432. .owner = THIS_MODULE,
  433. .name = "mpc5200-pcm-audio",
  434. .of_match_table = mpc5200_hpcd_match,
  435. }
  436. };
  437. module_platform_driver(mpc5200_hpcd_of_driver);
  438. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  439. MODULE_DESCRIPTION("Freescale MPC5200 PSC in DMA mode ASoC Driver");
  440. MODULE_LICENSE("GPL");