dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Renesas R-Car Audio DMAC support
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corp.
  5. * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/of_dma.h>
  13. #include "rsnd.h"
  14. /*
  15. * Audio DMAC peri peri register
  16. */
  17. #define PDMASAR 0x00
  18. #define PDMADAR 0x04
  19. #define PDMACHCR 0x0c
  20. /* PDMACHCR */
  21. #define PDMACHCR_DE (1 << 0)
  22. struct rsnd_dmaen {
  23. struct dma_chan *chan;
  24. dma_addr_t dma_buf;
  25. unsigned int dma_len;
  26. unsigned int dma_period;
  27. unsigned int dma_cnt;
  28. };
  29. struct rsnd_dmapp {
  30. int dmapp_id;
  31. u32 chcr;
  32. };
  33. struct rsnd_dma {
  34. struct rsnd_mod mod;
  35. struct rsnd_mod *mod_from;
  36. struct rsnd_mod *mod_to;
  37. dma_addr_t src_addr;
  38. dma_addr_t dst_addr;
  39. union {
  40. struct rsnd_dmaen en;
  41. struct rsnd_dmapp pp;
  42. } dma;
  43. };
  44. struct rsnd_dma_ctrl {
  45. void __iomem *base;
  46. int dmaen_num;
  47. int dmapp_num;
  48. };
  49. #define rsnd_priv_to_dmac(p) ((struct rsnd_dma_ctrl *)(p)->dma)
  50. #define rsnd_mod_to_dma(_mod) container_of((_mod), struct rsnd_dma, mod)
  51. #define rsnd_dma_to_dmaen(dma) (&(dma)->dma.en)
  52. #define rsnd_dma_to_dmapp(dma) (&(dma)->dma.pp)
  53. /*
  54. * Audio DMAC
  55. */
  56. #define rsnd_dmaen_sync(dmaen, io, i) __rsnd_dmaen_sync(dmaen, io, i, 1)
  57. #define rsnd_dmaen_unsync(dmaen, io, i) __rsnd_dmaen_sync(dmaen, io, i, 0)
  58. static void __rsnd_dmaen_sync(struct rsnd_dmaen *dmaen, struct rsnd_dai_stream *io,
  59. int i, int sync)
  60. {
  61. struct device *dev = dmaen->chan->device->dev;
  62. enum dma_data_direction dir;
  63. int is_play = rsnd_io_is_play(io);
  64. dma_addr_t buf;
  65. int len, max;
  66. size_t period;
  67. len = dmaen->dma_len;
  68. period = dmaen->dma_period;
  69. max = len / period;
  70. i = i % max;
  71. buf = dmaen->dma_buf + (period * i);
  72. dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  73. if (sync)
  74. dma_sync_single_for_device(dev, buf, period, dir);
  75. else
  76. dma_sync_single_for_cpu(dev, buf, period, dir);
  77. }
  78. static void __rsnd_dmaen_complete(struct rsnd_mod *mod,
  79. struct rsnd_dai_stream *io)
  80. {
  81. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  82. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  83. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  84. bool elapsed = false;
  85. unsigned long flags;
  86. /*
  87. * Renesas sound Gen1 needs 1 DMAC,
  88. * Gen2 needs 2 DMAC.
  89. * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
  90. * But, Audio-DMAC-peri-peri doesn't have interrupt,
  91. * and this driver is assuming that here.
  92. *
  93. * If Audio-DMAC-peri-peri has interrpt,
  94. * rsnd_dai_pointer_update() will be called twice,
  95. * ant it will breaks io->byte_pos
  96. */
  97. spin_lock_irqsave(&priv->lock, flags);
  98. if (rsnd_io_is_working(io)) {
  99. rsnd_dmaen_unsync(dmaen, io, dmaen->dma_cnt);
  100. /*
  101. * Next period is already started.
  102. * Let's sync Next Next period
  103. * see
  104. * rsnd_dmaen_start()
  105. */
  106. rsnd_dmaen_sync(dmaen, io, dmaen->dma_cnt + 2);
  107. elapsed = rsnd_dai_pointer_update(io, io->byte_per_period);
  108. dmaen->dma_cnt++;
  109. }
  110. spin_unlock_irqrestore(&priv->lock, flags);
  111. if (elapsed)
  112. rsnd_dai_period_elapsed(io);
  113. }
  114. static void rsnd_dmaen_complete(void *data)
  115. {
  116. struct rsnd_mod *mod = data;
  117. rsnd_mod_interrupt(mod, __rsnd_dmaen_complete);
  118. }
  119. static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_dai_stream *io,
  120. struct rsnd_mod *mod_from,
  121. struct rsnd_mod *mod_to)
  122. {
  123. if ((!mod_from && !mod_to) ||
  124. (mod_from && mod_to))
  125. return NULL;
  126. if (mod_from)
  127. return rsnd_mod_dma_req(io, mod_from);
  128. else
  129. return rsnd_mod_dma_req(io, mod_to);
  130. }
  131. static int rsnd_dmaen_stop(struct rsnd_mod *mod,
  132. struct rsnd_dai_stream *io,
  133. struct rsnd_priv *priv)
  134. {
  135. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  136. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  137. if (dmaen->chan) {
  138. int is_play = rsnd_io_is_play(io);
  139. dmaengine_terminate_all(dmaen->chan);
  140. dma_unmap_single(dmaen->chan->device->dev,
  141. dmaen->dma_buf, dmaen->dma_len,
  142. is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  143. }
  144. return 0;
  145. }
  146. static int rsnd_dmaen_nolock_stop(struct rsnd_mod *mod,
  147. struct rsnd_dai_stream *io,
  148. struct rsnd_priv *priv)
  149. {
  150. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  151. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  152. /*
  153. * DMAEngine release uses mutex lock.
  154. * Thus, it shouldn't be called under spinlock.
  155. * Let's call it under nolock_start
  156. */
  157. if (dmaen->chan)
  158. dma_release_channel(dmaen->chan);
  159. dmaen->chan = NULL;
  160. return 0;
  161. }
  162. static int rsnd_dmaen_nolock_start(struct rsnd_mod *mod,
  163. struct rsnd_dai_stream *io,
  164. struct rsnd_priv *priv)
  165. {
  166. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  167. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  168. struct device *dev = rsnd_priv_to_dev(priv);
  169. if (dmaen->chan) {
  170. dev_err(dev, "it already has dma channel\n");
  171. return -EIO;
  172. }
  173. /*
  174. * DMAEngine request uses mutex lock.
  175. * Thus, it shouldn't be called under spinlock.
  176. * Let's call it under nolock_start
  177. */
  178. dmaen->chan = rsnd_dmaen_request_channel(io,
  179. dma->mod_from,
  180. dma->mod_to);
  181. if (IS_ERR_OR_NULL(dmaen->chan)) {
  182. int ret = PTR_ERR(dmaen->chan);
  183. dmaen->chan = NULL;
  184. dev_err(dev, "can't get dma channel\n");
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int rsnd_dmaen_start(struct rsnd_mod *mod,
  190. struct rsnd_dai_stream *io,
  191. struct rsnd_priv *priv)
  192. {
  193. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  194. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  195. struct snd_pcm_substream *substream = io->substream;
  196. struct device *dev = rsnd_priv_to_dev(priv);
  197. struct dma_async_tx_descriptor *desc;
  198. struct dma_slave_config cfg = {};
  199. dma_addr_t buf;
  200. size_t len;
  201. size_t period;
  202. int is_play = rsnd_io_is_play(io);
  203. int i;
  204. int ret;
  205. cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  206. cfg.src_addr = dma->src_addr;
  207. cfg.dst_addr = dma->dst_addr;
  208. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  209. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  210. dev_dbg(dev, "%s[%d] %pad -> %pad\n",
  211. rsnd_mod_name(mod), rsnd_mod_id(mod),
  212. &cfg.src_addr, &cfg.dst_addr);
  213. ret = dmaengine_slave_config(dmaen->chan, &cfg);
  214. if (ret < 0)
  215. return ret;
  216. len = snd_pcm_lib_buffer_bytes(substream);
  217. period = snd_pcm_lib_period_bytes(substream);
  218. buf = dma_map_single(dmaen->chan->device->dev,
  219. substream->runtime->dma_area,
  220. len,
  221. is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  222. if (dma_mapping_error(dmaen->chan->device->dev, buf)) {
  223. dev_err(dev, "dma map failed\n");
  224. return -EIO;
  225. }
  226. desc = dmaengine_prep_dma_cyclic(dmaen->chan,
  227. buf, len, period,
  228. is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  229. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  230. if (!desc) {
  231. dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
  232. return -EIO;
  233. }
  234. desc->callback = rsnd_dmaen_complete;
  235. desc->callback_param = rsnd_mod_get(dma);
  236. dmaen->dma_buf = buf;
  237. dmaen->dma_len = len;
  238. dmaen->dma_period = period;
  239. dmaen->dma_cnt = 0;
  240. /*
  241. * synchronize this and next period
  242. * see
  243. * __rsnd_dmaen_complete()
  244. */
  245. for (i = 0; i < 2; i++)
  246. rsnd_dmaen_sync(dmaen, io, i);
  247. if (dmaengine_submit(desc) < 0) {
  248. dev_err(dev, "dmaengine_submit() fail\n");
  249. return -EIO;
  250. }
  251. dma_async_issue_pending(dmaen->chan);
  252. return 0;
  253. }
  254. struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node,
  255. struct rsnd_mod *mod, char *name)
  256. {
  257. struct dma_chan *chan = NULL;
  258. struct device_node *np;
  259. int i = 0;
  260. for_each_child_of_node(of_node, np) {
  261. if (i == rsnd_mod_id(mod) && (!chan))
  262. chan = of_dma_request_slave_channel(np, name);
  263. i++;
  264. }
  265. /* It should call of_node_put(), since, it is rsnd_xxx_of_node() */
  266. of_node_put(of_node);
  267. return chan;
  268. }
  269. static int rsnd_dmaen_attach(struct rsnd_dai_stream *io,
  270. struct rsnd_dma *dma,
  271. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
  272. {
  273. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  274. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  275. struct dma_chan *chan;
  276. /* try to get DMAEngine channel */
  277. chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);
  278. if (IS_ERR_OR_NULL(chan)) {
  279. /*
  280. * DMA failed. try to PIO mode
  281. * see
  282. * rsnd_ssi_fallback()
  283. * rsnd_rdai_continuance_probe()
  284. */
  285. return -EAGAIN;
  286. }
  287. dma_release_channel(chan);
  288. dmac->dmaen_num++;
  289. return 0;
  290. }
  291. static struct rsnd_mod_ops rsnd_dmaen_ops = {
  292. .name = "audmac",
  293. .nolock_start = rsnd_dmaen_nolock_start,
  294. .nolock_stop = rsnd_dmaen_nolock_stop,
  295. .start = rsnd_dmaen_start,
  296. .stop = rsnd_dmaen_stop,
  297. };
  298. /*
  299. * Audio DMAC peri peri
  300. */
  301. static const u8 gen2_id_table_ssiu[] = {
  302. 0x00, /* SSI00 */
  303. 0x04, /* SSI10 */
  304. 0x08, /* SSI20 */
  305. 0x0c, /* SSI3 */
  306. 0x0d, /* SSI4 */
  307. 0x0e, /* SSI5 */
  308. 0x0f, /* SSI6 */
  309. 0x10, /* SSI7 */
  310. 0x11, /* SSI8 */
  311. 0x12, /* SSI90 */
  312. };
  313. static const u8 gen2_id_table_scu[] = {
  314. 0x2d, /* SCU_SRCI0 */
  315. 0x2e, /* SCU_SRCI1 */
  316. 0x2f, /* SCU_SRCI2 */
  317. 0x30, /* SCU_SRCI3 */
  318. 0x31, /* SCU_SRCI4 */
  319. 0x32, /* SCU_SRCI5 */
  320. 0x33, /* SCU_SRCI6 */
  321. 0x34, /* SCU_SRCI7 */
  322. 0x35, /* SCU_SRCI8 */
  323. 0x36, /* SCU_SRCI9 */
  324. };
  325. static const u8 gen2_id_table_cmd[] = {
  326. 0x37, /* SCU_CMD0 */
  327. 0x38, /* SCU_CMD1 */
  328. };
  329. static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
  330. struct rsnd_mod *mod)
  331. {
  332. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  333. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  334. struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
  335. const u8 *entry = NULL;
  336. int id = rsnd_mod_id(mod);
  337. int size = 0;
  338. if (mod == ssi) {
  339. entry = gen2_id_table_ssiu;
  340. size = ARRAY_SIZE(gen2_id_table_ssiu);
  341. } else if (mod == src) {
  342. entry = gen2_id_table_scu;
  343. size = ARRAY_SIZE(gen2_id_table_scu);
  344. } else if (mod == dvc) {
  345. entry = gen2_id_table_cmd;
  346. size = ARRAY_SIZE(gen2_id_table_cmd);
  347. }
  348. if ((!entry) || (size <= id)) {
  349. struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
  350. dev_err(dev, "unknown connection (%s[%d])\n",
  351. rsnd_mod_name(mod), rsnd_mod_id(mod));
  352. /* use non-prohibited SRS number as error */
  353. return 0x00; /* SSI00 */
  354. }
  355. return entry[id];
  356. }
  357. static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
  358. struct rsnd_mod *mod_from,
  359. struct rsnd_mod *mod_to)
  360. {
  361. return (rsnd_dmapp_get_id(io, mod_from) << 24) +
  362. (rsnd_dmapp_get_id(io, mod_to) << 16);
  363. }
  364. #define rsnd_dmapp_addr(dmac, dma, reg) \
  365. (dmac->base + 0x20 + reg + \
  366. (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
  367. static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
  368. {
  369. struct rsnd_mod *mod = rsnd_mod_get(dma);
  370. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  371. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  372. struct device *dev = rsnd_priv_to_dev(priv);
  373. dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
  374. iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
  375. }
  376. static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
  377. {
  378. struct rsnd_mod *mod = rsnd_mod_get(dma);
  379. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  380. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  381. return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
  382. }
  383. static int rsnd_dmapp_stop(struct rsnd_mod *mod,
  384. struct rsnd_dai_stream *io,
  385. struct rsnd_priv *priv)
  386. {
  387. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  388. int i;
  389. rsnd_dmapp_write(dma, 0, PDMACHCR);
  390. for (i = 0; i < 1024; i++) {
  391. if (0 == rsnd_dmapp_read(dma, PDMACHCR))
  392. return 0;
  393. udelay(1);
  394. }
  395. return -EIO;
  396. }
  397. static int rsnd_dmapp_start(struct rsnd_mod *mod,
  398. struct rsnd_dai_stream *io,
  399. struct rsnd_priv *priv)
  400. {
  401. struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
  402. struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
  403. rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
  404. rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
  405. rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
  406. return 0;
  407. }
  408. static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,
  409. struct rsnd_dma *dma,
  410. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
  411. {
  412. struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
  413. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  414. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  415. struct device *dev = rsnd_priv_to_dev(priv);
  416. dmapp->dmapp_id = dmac->dmapp_num;
  417. dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
  418. dmac->dmapp_num++;
  419. dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
  420. dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
  421. return 0;
  422. }
  423. static struct rsnd_mod_ops rsnd_dmapp_ops = {
  424. .name = "audmac-pp",
  425. .start = rsnd_dmapp_start,
  426. .stop = rsnd_dmapp_stop,
  427. .quit = rsnd_dmapp_stop,
  428. };
  429. /*
  430. * Common DMAC Interface
  431. */
  432. /*
  433. * DMA read/write register offset
  434. *
  435. * RSND_xxx_I_N for Audio DMAC input
  436. * RSND_xxx_O_N for Audio DMAC output
  437. * RSND_xxx_I_P for Audio DMAC peri peri input
  438. * RSND_xxx_O_P for Audio DMAC peri peri output
  439. *
  440. * ex) R-Car H2 case
  441. * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
  442. * SSI : 0xec541000 / 0xec241008 / 0xec24100c
  443. * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
  444. * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
  445. * CMD : 0xec500000 / / 0xec008000 0xec308000
  446. */
  447. #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
  448. #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
  449. #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
  450. #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
  451. #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
  452. #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
  453. #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
  454. #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
  455. #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
  456. #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
  457. #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
  458. #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
  459. static dma_addr_t
  460. rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
  461. struct rsnd_mod *mod,
  462. int is_play, int is_from)
  463. {
  464. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  465. struct device *dev = rsnd_priv_to_dev(priv);
  466. phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
  467. phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
  468. int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
  469. int use_src = !!rsnd_io_to_mod_src(io);
  470. int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
  471. !!rsnd_io_to_mod_mix(io) ||
  472. !!rsnd_io_to_mod_ctu(io);
  473. int id = rsnd_mod_id(mod);
  474. struct dma_addr {
  475. dma_addr_t out_addr;
  476. dma_addr_t in_addr;
  477. } dma_addrs[3][2][3] = {
  478. /* SRC */
  479. {{{ 0, 0 },
  480. /* Capture */
  481. { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
  482. { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
  483. /* Playback */
  484. {{ 0, 0, },
  485. { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
  486. { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
  487. },
  488. /* SSI */
  489. /* Capture */
  490. {{{ RDMA_SSI_O_N(ssi, id), 0 },
  491. { RDMA_SSIU_O_P(ssi, id), 0 },
  492. { RDMA_SSIU_O_P(ssi, id), 0 } },
  493. /* Playback */
  494. {{ 0, RDMA_SSI_I_N(ssi, id) },
  495. { 0, RDMA_SSIU_I_P(ssi, id) },
  496. { 0, RDMA_SSIU_I_P(ssi, id) } }
  497. },
  498. /* SSIU */
  499. /* Capture */
  500. {{{ RDMA_SSIU_O_N(ssi, id), 0 },
  501. { RDMA_SSIU_O_P(ssi, id), 0 },
  502. { RDMA_SSIU_O_P(ssi, id), 0 } },
  503. /* Playback */
  504. {{ 0, RDMA_SSIU_I_N(ssi, id) },
  505. { 0, RDMA_SSIU_I_P(ssi, id) },
  506. { 0, RDMA_SSIU_I_P(ssi, id) } } },
  507. };
  508. /* it shouldn't happen */
  509. if (use_cmd && !use_src)
  510. dev_err(dev, "DVC is selected without SRC\n");
  511. /* use SSIU or SSI ? */
  512. if (is_ssi && rsnd_ssi_use_busif(io))
  513. is_ssi++;
  514. return (is_from) ?
  515. dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
  516. dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
  517. }
  518. static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
  519. struct rsnd_mod *mod,
  520. int is_play, int is_from)
  521. {
  522. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  523. /*
  524. * gen1 uses default DMA addr
  525. */
  526. if (rsnd_is_gen1(priv))
  527. return 0;
  528. if (!mod)
  529. return 0;
  530. return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
  531. }
  532. #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
  533. static void rsnd_dma_of_path(struct rsnd_mod *this,
  534. struct rsnd_dai_stream *io,
  535. int is_play,
  536. struct rsnd_mod **mod_from,
  537. struct rsnd_mod **mod_to)
  538. {
  539. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  540. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  541. struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
  542. struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
  543. struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
  544. struct rsnd_mod *mod[MOD_MAX];
  545. struct rsnd_mod *mod_start, *mod_end;
  546. struct rsnd_priv *priv = rsnd_mod_to_priv(this);
  547. struct device *dev = rsnd_priv_to_dev(priv);
  548. int nr, i, idx;
  549. if (!ssi)
  550. return;
  551. nr = 0;
  552. for (i = 0; i < MOD_MAX; i++) {
  553. mod[i] = NULL;
  554. nr += !!rsnd_io_to_mod(io, i);
  555. }
  556. /*
  557. * [S] -*-> [E]
  558. * [S] -*-> SRC -o-> [E]
  559. * [S] -*-> SRC -> DVC -o-> [E]
  560. * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
  561. *
  562. * playback [S] = mem
  563. * [E] = SSI
  564. *
  565. * capture [S] = SSI
  566. * [E] = mem
  567. *
  568. * -*-> Audio DMAC
  569. * -o-> Audio DMAC peri peri
  570. */
  571. mod_start = (is_play) ? NULL : ssi;
  572. mod_end = (is_play) ? ssi : NULL;
  573. idx = 0;
  574. mod[idx++] = mod_start;
  575. for (i = 1; i < nr; i++) {
  576. if (src) {
  577. mod[idx++] = src;
  578. src = NULL;
  579. } else if (ctu) {
  580. mod[idx++] = ctu;
  581. ctu = NULL;
  582. } else if (mix) {
  583. mod[idx++] = mix;
  584. mix = NULL;
  585. } else if (dvc) {
  586. mod[idx++] = dvc;
  587. dvc = NULL;
  588. }
  589. }
  590. mod[idx] = mod_end;
  591. /*
  592. * | SSI | SRC |
  593. * -------------+-----+-----+
  594. * is_play | o | * |
  595. * !is_play | * | o |
  596. */
  597. if ((this == ssi) == (is_play)) {
  598. *mod_from = mod[idx - 1];
  599. *mod_to = mod[idx];
  600. } else {
  601. *mod_from = mod[0];
  602. *mod_to = mod[1];
  603. }
  604. dev_dbg(dev, "module connection (this is %s[%d])\n",
  605. rsnd_mod_name(this), rsnd_mod_id(this));
  606. for (i = 0; i <= idx; i++) {
  607. dev_dbg(dev, " %s[%d]%s\n",
  608. rsnd_mod_name(mod[i]), rsnd_mod_id(mod[i]),
  609. (mod[i] == *mod_from) ? " from" :
  610. (mod[i] == *mod_to) ? " to" : "");
  611. }
  612. }
  613. int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
  614. struct rsnd_mod **dma_mod)
  615. {
  616. struct rsnd_mod *mod_from = NULL;
  617. struct rsnd_mod *mod_to = NULL;
  618. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  619. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  620. struct device *dev = rsnd_priv_to_dev(priv);
  621. struct rsnd_mod_ops *ops;
  622. enum rsnd_mod_type type;
  623. int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,
  624. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
  625. int is_play = rsnd_io_is_play(io);
  626. int ret, dma_id;
  627. /*
  628. * DMA failed. try to PIO mode
  629. * see
  630. * rsnd_ssi_fallback()
  631. * rsnd_rdai_continuance_probe()
  632. */
  633. if (!dmac)
  634. return -EAGAIN;
  635. rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);
  636. /* for Gen2 */
  637. if (mod_from && mod_to) {
  638. ops = &rsnd_dmapp_ops;
  639. attach = rsnd_dmapp_attach;
  640. dma_id = dmac->dmapp_num;
  641. type = RSND_MOD_AUDMAPP;
  642. } else {
  643. ops = &rsnd_dmaen_ops;
  644. attach = rsnd_dmaen_attach;
  645. dma_id = dmac->dmaen_num;
  646. type = RSND_MOD_AUDMA;
  647. }
  648. /* for Gen1, overwrite */
  649. if (rsnd_is_gen1(priv)) {
  650. ops = &rsnd_dmaen_ops;
  651. attach = rsnd_dmaen_attach;
  652. dma_id = dmac->dmaen_num;
  653. type = RSND_MOD_AUDMA;
  654. }
  655. if (!(*dma_mod)) {
  656. struct rsnd_dma *dma;
  657. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  658. if (!dma)
  659. return -ENOMEM;
  660. *dma_mod = rsnd_mod_get(dma);
  661. ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,
  662. rsnd_mod_get_status, type, dma_id);
  663. if (ret < 0)
  664. return ret;
  665. dev_dbg(dev, "%s[%d] %s[%d] -> %s[%d]\n",
  666. rsnd_mod_name(*dma_mod), rsnd_mod_id(*dma_mod),
  667. rsnd_mod_name(mod_from), rsnd_mod_id(mod_from),
  668. rsnd_mod_name(mod_to), rsnd_mod_id(mod_to));
  669. ret = attach(io, dma, mod_from, mod_to);
  670. if (ret < 0)
  671. return ret;
  672. dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
  673. dma->dst_addr = rsnd_dma_addr(io, mod_to, is_play, 0);
  674. dma->mod_from = mod_from;
  675. dma->mod_to = mod_to;
  676. }
  677. ret = rsnd_dai_connect(*dma_mod, io, type);
  678. if (ret < 0)
  679. return ret;
  680. return 0;
  681. }
  682. int rsnd_dma_probe(struct rsnd_priv *priv)
  683. {
  684. struct platform_device *pdev = rsnd_priv_to_pdev(priv);
  685. struct device *dev = rsnd_priv_to_dev(priv);
  686. struct rsnd_dma_ctrl *dmac;
  687. struct resource *res;
  688. /*
  689. * for Gen1
  690. */
  691. if (rsnd_is_gen1(priv))
  692. return 0;
  693. /*
  694. * for Gen2
  695. */
  696. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
  697. dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
  698. if (!dmac || !res) {
  699. dev_err(dev, "dma allocate failed\n");
  700. return 0; /* it will be PIO mode */
  701. }
  702. dmac->dmapp_num = 0;
  703. dmac->base = devm_ioremap_resource(dev, res);
  704. if (IS_ERR(dmac->base))
  705. return PTR_ERR(dmac->base);
  706. priv->dma = dmac;
  707. return 0;
  708. }