sps_dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* BAM-DMA Manager. */
  13. #ifdef CONFIG_SPS_SUPPORT_BAMDMA
  14. #include <linux/export.h>
  15. #include <linux/memory.h> /* memset */
  16. #include "spsi.h"
  17. #include "bam.h"
  18. #include "sps_bam.h" /* bam_dma_thresh_dma */
  19. #include "sps_core.h" /* sps_h2bam() */
  20. /**
  21. * registers
  22. */
  23. #define DMA_ENBL (0x00000000)
  24. #ifdef CONFIG_SPS_SUPPORT_NDP_BAM
  25. #define DMA_REVISION (0x00000004)
  26. #define DMA_CONFIG (0x00000008)
  27. #define DMA_CHNL_CONFIG(n) (0x00001000 + 4096 * (n))
  28. #else
  29. #define DMA_CHNL_CONFIG(n) (0x00000004 + 4 * (n))
  30. #define DMA_CONFIG (0x00000040)
  31. #endif
  32. /**
  33. * masks
  34. */
  35. /* DMA_CHNL_confign */
  36. #ifdef CONFIG_SPS_SUPPORT_NDP_BAM
  37. #define DMA_CHNL_PRODUCER_PIPE_ENABLED 0x40000
  38. #define DMA_CHNL_CONSUMER_PIPE_ENABLED 0x20000
  39. #endif
  40. #define DMA_CHNL_HALT_DONE 0x10000
  41. #define DMA_CHNL_HALT 0x1000
  42. #define DMA_CHNL_ENABLE 0x100
  43. #define DMA_CHNL_ACT_THRESH 0x30
  44. #define DMA_CHNL_WEIGHT 0x7
  45. /* DMA_CONFIG */
  46. #define TESTBUS_SELECT 0x3
  47. /**
  48. *
  49. * Write register with debug info.
  50. *
  51. * @base - bam base virtual address.
  52. * @offset - register offset.
  53. * @val - value to write.
  54. *
  55. */
  56. static inline void dma_write_reg(void *base, u32 offset, u32 val)
  57. {
  58. iowrite32(val, base + offset);
  59. SPS_DBG("sps:bamdma: write reg 0x%x w_val 0x%x.", offset, val);
  60. }
  61. /**
  62. * Write register masked field with debug info.
  63. *
  64. * @base - bam base virtual address.
  65. * @offset - register offset.
  66. * @mask - register bitmask.
  67. * @val - value to write.
  68. *
  69. */
  70. static inline void dma_write_reg_field(void *base, u32 offset,
  71. const u32 mask, u32 val)
  72. {
  73. u32 shift = find_first_bit((void *)&mask, 32);
  74. u32 tmp = ioread32(base + offset);
  75. tmp &= ~mask; /* clear written bits */
  76. val = tmp | (val << shift);
  77. iowrite32(val, base + offset);
  78. SPS_DBG("sps:bamdma: write reg 0x%x w_val 0x%x.", offset, val);
  79. }
  80. /* Round max number of pipes to nearest multiple of 2 */
  81. #define DMA_MAX_PIPES ((BAM_MAX_PIPES / 2) * 2)
  82. /* Maximum number of BAM-DMAs supported */
  83. #define MAX_BAM_DMA_DEVICES 1
  84. /* Maximum number of BAMs that will be registered */
  85. #define MAX_BAM_DMA_BAMS 1
  86. /* Pipe enable check values */
  87. #define DMA_PIPES_STATE_DIFF 0
  88. #define DMA_PIPES_BOTH_DISABLED 1
  89. #define DMA_PIPES_BOTH_ENABLED 2
  90. /* Even pipe is tx/dest/input/write, odd pipe is rx/src/output/read */
  91. #define DMA_PIPE_IS_DEST(p) (((p) & 1) == 0)
  92. #define DMA_PIPE_IS_SRC(p) (((p) & 1) != 0)
  93. /* BAM DMA pipe state */
  94. enum bamdma_pipe_state {
  95. PIPE_INACTIVE = 0,
  96. PIPE_ACTIVE
  97. };
  98. /* BAM DMA channel state */
  99. enum bamdma_chan_state {
  100. DMA_CHAN_STATE_FREE = 0,
  101. DMA_CHAN_STATE_ALLOC_EXT, /* Client allocation */
  102. DMA_CHAN_STATE_ALLOC_INT /* Internal (resource mgr) allocation */
  103. };
  104. struct bamdma_chan {
  105. /* Allocation state */
  106. enum bamdma_chan_state state;
  107. /* BAM DMA channel configuration parameters */
  108. u32 threshold;
  109. enum sps_dma_priority priority;
  110. /* HWIO channel configuration parameters */
  111. enum bam_dma_thresh_dma thresh;
  112. enum bam_dma_weight_dma weight;
  113. };
  114. /* BAM DMA device state */
  115. struct bamdma_device {
  116. /* BAM-DMA device state */
  117. int enabled;
  118. int local;
  119. /* BAM device state */
  120. struct sps_bam *bam;
  121. /* BAM handle, for deregistration */
  122. unsigned long h;
  123. /* BAM DMA device virtual mapping */
  124. void *virt_addr;
  125. int virtual_mapped;
  126. phys_addr_t phys_addr;
  127. void *hwio;
  128. /* BAM DMA pipe/channel state */
  129. u32 num_pipes;
  130. enum bamdma_pipe_state pipes[DMA_MAX_PIPES];
  131. struct bamdma_chan chans[DMA_MAX_PIPES / 2];
  132. };
  133. /* BAM-DMA devices */
  134. static struct bamdma_device bam_dma_dev[MAX_BAM_DMA_DEVICES];
  135. static struct mutex bam_dma_lock;
  136. /*
  137. * The BAM DMA module registers all BAMs in the BSP properties, but only
  138. * uses the first BAM-DMA device for allocations. References to the others
  139. * are stored in the following data array.
  140. */
  141. static int num_bams;
  142. static unsigned long bam_handles[MAX_BAM_DMA_BAMS];
  143. /**
  144. * Find BAM-DMA device
  145. *
  146. * This function finds the BAM-DMA device associated with the BAM handle.
  147. *
  148. * @h - BAM handle
  149. *
  150. * @return - pointer to BAM-DMA device, or NULL on error
  151. *
  152. */
  153. static struct bamdma_device *sps_dma_find_device(unsigned long h)
  154. {
  155. return &bam_dma_dev[0];
  156. }
  157. /**
  158. * BAM DMA device enable
  159. *
  160. * This function enables a BAM DMA device and the associated BAM.
  161. *
  162. * @dev - pointer to BAM DMA device context
  163. *
  164. * @return 0 on success, negative value on error
  165. *
  166. */
  167. static int sps_dma_device_enable(struct bamdma_device *dev)
  168. {
  169. if (dev->enabled)
  170. return 0;
  171. /*
  172. * If the BAM-DMA device is locally controlled then enable BAM-DMA
  173. * device
  174. */
  175. if (dev->local)
  176. dma_write_reg(dev->virt_addr, DMA_ENBL, 1);
  177. /* Enable BAM device */
  178. if (sps_bam_enable(dev->bam)) {
  179. SPS_ERR("sps:Failed to enable BAM DMA's BAM: %pa",
  180. &dev->phys_addr);
  181. return SPS_ERROR;
  182. }
  183. dev->enabled = true;
  184. return 0;
  185. }
  186. /**
  187. * BAM DMA device enable
  188. *
  189. * This function initializes a BAM DMA device.
  190. *
  191. * @dev - pointer to BAM DMA device context
  192. *
  193. * @return 0 on success, negative value on error
  194. *
  195. */
  196. static int sps_dma_device_disable(struct bamdma_device *dev)
  197. {
  198. u32 pipe_index;
  199. if (!dev->enabled)
  200. return 0;
  201. /* Do not disable if channels active */
  202. for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
  203. if (dev->pipes[pipe_index] != PIPE_INACTIVE)
  204. break;
  205. }
  206. if (pipe_index < dev->num_pipes) {
  207. SPS_ERR("sps:Fail to disable BAM-DMA %pa:channels are active",
  208. &dev->phys_addr);
  209. return SPS_ERROR;
  210. }
  211. dev->enabled = false;
  212. /* Disable BAM device */
  213. if (sps_bam_disable(dev->bam)) {
  214. SPS_ERR("sps:Fail to disable BAM-DMA BAM:%pa", &dev->phys_addr);
  215. return SPS_ERROR;
  216. }
  217. /* Is the BAM-DMA device locally controlled? */
  218. if (dev->local)
  219. /* Disable BAM-DMA device */
  220. dma_write_reg(dev->virt_addr, DMA_ENBL, 0);
  221. return 0;
  222. }
  223. /**
  224. * Initialize BAM DMA device
  225. *
  226. */
  227. int sps_dma_device_init(unsigned long h)
  228. {
  229. struct bamdma_device *dev;
  230. struct sps_bam_props *props;
  231. int result = SPS_ERROR;
  232. mutex_lock(&bam_dma_lock);
  233. /* Find a free BAM-DMA device slot */
  234. dev = NULL;
  235. if (bam_dma_dev[0].bam != NULL) {
  236. SPS_ERR("sps:BAM-DMA BAM device is already initialized.");
  237. goto exit_err;
  238. } else {
  239. dev = &bam_dma_dev[0];
  240. }
  241. /* Record BAM */
  242. memset(dev, 0, sizeof(*dev));
  243. dev->h = h;
  244. dev->bam = sps_h2bam(h);
  245. if (dev->bam == NULL) {
  246. SPS_ERR("sps:BAM-DMA BAM device is not found "
  247. "from the handle.");
  248. goto exit_err;
  249. }
  250. /* Map the BAM DMA device into virtual space, if necessary */
  251. props = &dev->bam->props;
  252. dev->phys_addr = props->periph_phys_addr;
  253. if (props->periph_virt_addr != NULL) {
  254. dev->virt_addr = props->periph_virt_addr;
  255. dev->virtual_mapped = false;
  256. } else {
  257. if (props->periph_virt_size == 0) {
  258. SPS_ERR("sps:Unable to map BAM DMA IO memory: %pa %x",
  259. &dev->phys_addr, props->periph_virt_size);
  260. goto exit_err;
  261. }
  262. dev->virt_addr = ioremap(dev->phys_addr,
  263. props->periph_virt_size);
  264. if (dev->virt_addr == NULL) {
  265. SPS_ERR("sps:Unable to map BAM DMA IO memory: %pa %x",
  266. &dev->phys_addr, props->periph_virt_size);
  267. goto exit_err;
  268. }
  269. dev->virtual_mapped = true;
  270. }
  271. dev->hwio = (void *) dev->virt_addr;
  272. /* Is the BAM-DMA device locally controlled? */
  273. if ((props->manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
  274. SPS_DBG2("sps:BAM-DMA is controlled locally: %pa",
  275. &dev->phys_addr);
  276. dev->local = true;
  277. } else {
  278. SPS_DBG2("sps:BAM-DMA is controlled remotely: %pa",
  279. &dev->phys_addr);
  280. dev->local = false;
  281. }
  282. /*
  283. * Enable the BAM DMA and determine the number of pipes/channels.
  284. * Leave the BAM-DMA enabled, since it is always a shared device.
  285. */
  286. if (sps_dma_device_enable(dev))
  287. goto exit_err;
  288. dev->num_pipes = dev->bam->props.num_pipes;
  289. result = 0;
  290. exit_err:
  291. if (result) {
  292. if (dev != NULL) {
  293. if (dev->virtual_mapped)
  294. iounmap(dev->virt_addr);
  295. dev->bam = NULL;
  296. }
  297. }
  298. mutex_unlock(&bam_dma_lock);
  299. return result;
  300. }
  301. /**
  302. * De-initialize BAM DMA device
  303. *
  304. */
  305. int sps_dma_device_de_init(unsigned long h)
  306. {
  307. struct bamdma_device *dev;
  308. u32 pipe_index;
  309. u32 chan;
  310. int result = 0;
  311. mutex_lock(&bam_dma_lock);
  312. dev = sps_dma_find_device(h);
  313. if (dev == NULL) {
  314. SPS_ERR("sps:BAM-DMA: not registered: %lx", h);
  315. result = SPS_ERROR;
  316. goto exit_err;
  317. }
  318. /* Check for channel leaks */
  319. for (chan = 0; chan < dev->num_pipes / 2; chan++) {
  320. if (dev->chans[chan].state != DMA_CHAN_STATE_FREE) {
  321. SPS_ERR("sps:BAM-DMA: channel not free: %d", chan);
  322. result = SPS_ERROR;
  323. dev->chans[chan].state = DMA_CHAN_STATE_FREE;
  324. }
  325. }
  326. for (pipe_index = 0; pipe_index < dev->num_pipes; pipe_index++) {
  327. if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
  328. SPS_ERR("sps:BAM-DMA: pipe not inactive: %d",
  329. pipe_index);
  330. result = SPS_ERROR;
  331. dev->pipes[pipe_index] = PIPE_INACTIVE;
  332. }
  333. }
  334. /* Disable BAM and BAM-DMA */
  335. if (sps_dma_device_disable(dev))
  336. result = SPS_ERROR;
  337. dev->h = BAM_HANDLE_INVALID;
  338. dev->bam = NULL;
  339. if (dev->virtual_mapped)
  340. iounmap(dev->virt_addr);
  341. exit_err:
  342. mutex_unlock(&bam_dma_lock);
  343. return result;
  344. }
  345. /**
  346. * Initialize BAM DMA module
  347. *
  348. */
  349. int sps_dma_init(const struct sps_bam_props *bam_props)
  350. {
  351. struct sps_bam_props props;
  352. const struct sps_bam_props *bam_reg;
  353. unsigned long h;
  354. /* Init local data */
  355. memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
  356. num_bams = 0;
  357. memset(bam_handles, 0, sizeof(bam_handles));
  358. /* Create a mutex to control access to the BAM-DMA devices */
  359. mutex_init(&bam_dma_lock);
  360. /* Are there any BAM DMA devices? */
  361. if (bam_props == NULL)
  362. return 0;
  363. /*
  364. * Registers all BAMs in the BSP properties, but only uses the first
  365. * BAM-DMA device for allocations.
  366. */
  367. if (bam_props->phys_addr) {
  368. /* Force multi-EE option for all BAM-DMAs */
  369. bam_reg = bam_props;
  370. if ((bam_props->options & SPS_BAM_OPT_BAMDMA) &&
  371. (bam_props->manage & SPS_BAM_MGR_MULTI_EE) == 0) {
  372. SPS_DBG("sps:Setting multi-EE options for BAM-DMA: %pa",
  373. &bam_props->phys_addr);
  374. props = *bam_props;
  375. props.manage |= SPS_BAM_MGR_MULTI_EE;
  376. bam_reg = &props;
  377. }
  378. /* Register the BAM */
  379. if (sps_register_bam_device(bam_reg, &h)) {
  380. SPS_ERR(
  381. "sps:Fail to register BAM-DMA BAM device: "
  382. "phys %pa", &bam_props->phys_addr);
  383. return SPS_ERROR;
  384. }
  385. /* Record the BAM so that it may be deregistered later */
  386. if (num_bams < MAX_BAM_DMA_BAMS) {
  387. bam_handles[num_bams] = h;
  388. num_bams++;
  389. } else {
  390. SPS_ERR("sps:BAM-DMA: BAM limit exceeded: %d",
  391. num_bams);
  392. return SPS_ERROR;
  393. }
  394. } else {
  395. SPS_ERR("sps:BAM-DMA phys_addr is zero.");
  396. return SPS_ERROR;
  397. }
  398. return 0;
  399. }
  400. /**
  401. * De-initialize BAM DMA module
  402. *
  403. */
  404. void sps_dma_de_init(void)
  405. {
  406. int n;
  407. /* De-initialize the BAM devices */
  408. for (n = 0; n < num_bams; n++)
  409. sps_deregister_bam_device(bam_handles[n]);
  410. /* Clear local data */
  411. memset(&bam_dma_dev, 0, sizeof(bam_dma_dev));
  412. num_bams = 0;
  413. memset(bam_handles, 0, sizeof(bam_handles));
  414. }
  415. /**
  416. * Allocate a BAM DMA channel
  417. *
  418. */
  419. int sps_alloc_dma_chan(const struct sps_alloc_dma_chan *alloc,
  420. struct sps_dma_chan *chan_info)
  421. {
  422. struct bamdma_device *dev;
  423. struct bamdma_chan *chan;
  424. u32 pipe_index;
  425. enum bam_dma_thresh_dma thresh = (enum bam_dma_thresh_dma) 0;
  426. enum bam_dma_weight_dma weight = (enum bam_dma_weight_dma) 0;
  427. int result = SPS_ERROR;
  428. if (alloc == NULL || chan_info == NULL) {
  429. SPS_ERR("sps:sps_alloc_dma_chan. invalid parameters");
  430. return SPS_ERROR;
  431. }
  432. /* Translate threshold and priority to hwio values */
  433. if (alloc->threshold != SPS_DMA_THRESHOLD_DEFAULT) {
  434. if (alloc->threshold >= 512)
  435. thresh = BAM_DMA_THRESH_512;
  436. else if (alloc->threshold >= 256)
  437. thresh = BAM_DMA_THRESH_256;
  438. else if (alloc->threshold >= 128)
  439. thresh = BAM_DMA_THRESH_128;
  440. else
  441. thresh = BAM_DMA_THRESH_64;
  442. }
  443. weight = alloc->priority;
  444. if ((u32)alloc->priority > (u32)BAM_DMA_WEIGHT_HIGH) {
  445. SPS_ERR("sps:BAM-DMA: invalid priority: %x", alloc->priority);
  446. return SPS_ERROR;
  447. }
  448. mutex_lock(&bam_dma_lock);
  449. dev = sps_dma_find_device(alloc->dev);
  450. if (dev == NULL) {
  451. SPS_ERR("sps:BAM-DMA: invalid BAM handle: %lx", alloc->dev);
  452. goto exit_err;
  453. }
  454. /* Search for a free set of pipes */
  455. for (pipe_index = 0, chan = dev->chans;
  456. pipe_index < dev->num_pipes; pipe_index += 2, chan++) {
  457. if (chan->state == DMA_CHAN_STATE_FREE) {
  458. /* Just check pipes for safety */
  459. if (dev->pipes[pipe_index] != PIPE_INACTIVE ||
  460. dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
  461. SPS_ERR("sps:BAM-DMA: channel %d state "
  462. "error:%d %d",
  463. pipe_index / 2, dev->pipes[pipe_index],
  464. dev->pipes[pipe_index + 1]);
  465. goto exit_err;
  466. }
  467. break; /* Found free pipe */
  468. }
  469. }
  470. if (pipe_index >= dev->num_pipes) {
  471. SPS_ERR("sps:BAM-DMA: no free channel. num_pipes = %d",
  472. dev->num_pipes);
  473. goto exit_err;
  474. }
  475. chan->state = DMA_CHAN_STATE_ALLOC_EXT;
  476. /* Store config values for use when pipes are activated */
  477. chan = &dev->chans[pipe_index / 2];
  478. chan->threshold = alloc->threshold;
  479. chan->thresh = thresh;
  480. chan->priority = alloc->priority;
  481. chan->weight = weight;
  482. SPS_DBG2("sps:sps_alloc_dma_chan. pipe %d.\n", pipe_index);
  483. /* Report allocated pipes to client */
  484. chan_info->dev = dev->h;
  485. /* Dest/input/write pipex */
  486. chan_info->dest_pipe_index = pipe_index;
  487. /* Source/output/read pipe */
  488. chan_info->src_pipe_index = pipe_index + 1;
  489. result = 0;
  490. exit_err:
  491. mutex_unlock(&bam_dma_lock);
  492. return result;
  493. }
  494. EXPORT_SYMBOL(sps_alloc_dma_chan);
  495. /**
  496. * Free a BAM DMA channel
  497. *
  498. */
  499. int sps_free_dma_chan(struct sps_dma_chan *chan)
  500. {
  501. struct bamdma_device *dev;
  502. u32 pipe_index;
  503. int result = 0;
  504. if (chan == NULL) {
  505. SPS_ERR("sps:sps_free_dma_chan. chan is NULL");
  506. return SPS_ERROR;
  507. }
  508. mutex_lock(&bam_dma_lock);
  509. dev = sps_dma_find_device(chan->dev);
  510. if (dev == NULL) {
  511. SPS_ERR("sps:BAM-DMA: invalid BAM handle: %lx", chan->dev);
  512. result = SPS_ERROR;
  513. goto exit_err;
  514. }
  515. /* Verify the pipe indices */
  516. pipe_index = chan->dest_pipe_index;
  517. if (pipe_index >= dev->num_pipes || ((pipe_index & 1)) ||
  518. (pipe_index + 1) != chan->src_pipe_index) {
  519. SPS_ERR("sps:sps_free_dma_chan. Invalid pipe indices."
  520. "num_pipes=%d.dest=%d.src=%d.",
  521. dev->num_pipes,
  522. chan->dest_pipe_index,
  523. chan->src_pipe_index);
  524. result = SPS_ERROR;
  525. goto exit_err;
  526. }
  527. /* Are both pipes inactive? */
  528. if (dev->chans[pipe_index / 2].state != DMA_CHAN_STATE_ALLOC_EXT ||
  529. dev->pipes[pipe_index] != PIPE_INACTIVE ||
  530. dev->pipes[pipe_index + 1] != PIPE_INACTIVE) {
  531. SPS_ERR("sps:BAM-DMA: attempt to free active chan %d: %d %d",
  532. pipe_index / 2, dev->pipes[pipe_index],
  533. dev->pipes[pipe_index + 1]);
  534. result = SPS_ERROR;
  535. goto exit_err;
  536. }
  537. /* Free the channel */
  538. dev->chans[pipe_index / 2].state = DMA_CHAN_STATE_FREE;
  539. exit_err:
  540. mutex_unlock(&bam_dma_lock);
  541. return result;
  542. }
  543. EXPORT_SYMBOL(sps_free_dma_chan);
  544. /**
  545. * Activate a BAM DMA pipe
  546. *
  547. * This function activates a BAM DMA pipe.
  548. *
  549. * @dev - pointer to BAM-DMA device descriptor
  550. *
  551. * @pipe_index - pipe index
  552. *
  553. * @return 0 on success, negative value on error
  554. *
  555. */
  556. static u32 sps_dma_check_pipes(struct bamdma_device *dev, u32 pipe_index)
  557. {
  558. u32 pipe_in;
  559. u32 pipe_out;
  560. int enabled_in;
  561. int enabled_out;
  562. u32 check;
  563. pipe_in = pipe_index & ~1;
  564. pipe_out = pipe_in + 1;
  565. enabled_in = bam_pipe_is_enabled(dev->bam->base, pipe_in);
  566. enabled_out = bam_pipe_is_enabled(dev->bam->base, pipe_out);
  567. if (!enabled_in && !enabled_out)
  568. check = DMA_PIPES_BOTH_DISABLED;
  569. else if (enabled_in && enabled_out)
  570. check = DMA_PIPES_BOTH_ENABLED;
  571. else
  572. check = DMA_PIPES_STATE_DIFF;
  573. return check;
  574. }
  575. /**
  576. * Allocate a BAM DMA pipe
  577. *
  578. */
  579. int sps_dma_pipe_alloc(void *bam_arg, u32 pipe_index, enum sps_mode dir)
  580. {
  581. struct sps_bam *bam = bam_arg;
  582. struct bamdma_device *dev;
  583. struct bamdma_chan *chan;
  584. u32 channel;
  585. int result = SPS_ERROR;
  586. if (bam == NULL) {
  587. SPS_ERR("sps:BAM context is NULL");
  588. return SPS_ERROR;
  589. }
  590. /* Check pipe direction */
  591. if ((DMA_PIPE_IS_DEST(pipe_index) && dir != SPS_MODE_DEST) ||
  592. (DMA_PIPE_IS_SRC(pipe_index) && dir != SPS_MODE_SRC)) {
  593. SPS_ERR("sps:BAM-DMA: wrong direction for BAM %pa pipe %d",
  594. &bam->props.phys_addr, pipe_index);
  595. return SPS_ERROR;
  596. }
  597. mutex_lock(&bam_dma_lock);
  598. dev = sps_dma_find_device((unsigned long) bam);
  599. if (dev == NULL) {
  600. SPS_ERR("sps:BAM-DMA: invalid BAM: %pa",
  601. &bam->props.phys_addr);
  602. goto exit_err;
  603. }
  604. if (pipe_index >= dev->num_pipes) {
  605. SPS_ERR("sps:BAM-DMA: BAM %pa invalid pipe: %d",
  606. &bam->props.phys_addr, pipe_index);
  607. goto exit_err;
  608. }
  609. if (dev->pipes[pipe_index] != PIPE_INACTIVE) {
  610. SPS_ERR("sps:BAM-DMA: BAM %pa pipe %d already active",
  611. &bam->props.phys_addr, pipe_index);
  612. goto exit_err;
  613. }
  614. /* Mark pipe active */
  615. dev->pipes[pipe_index] = PIPE_ACTIVE;
  616. /* If channel is not allocated, make an internal allocation */
  617. channel = pipe_index / 2;
  618. chan = &dev->chans[channel];
  619. if (chan->state != DMA_CHAN_STATE_ALLOC_EXT &&
  620. chan->state != DMA_CHAN_STATE_ALLOC_INT) {
  621. chan->state = DMA_CHAN_STATE_ALLOC_INT;
  622. }
  623. result = 0;
  624. exit_err:
  625. mutex_unlock(&bam_dma_lock);
  626. return result;
  627. }
  628. /**
  629. * Enable a BAM DMA pipe
  630. *
  631. */
  632. int sps_dma_pipe_enable(void *bam_arg, u32 pipe_index)
  633. {
  634. struct sps_bam *bam = bam_arg;
  635. struct bamdma_device *dev;
  636. struct bamdma_chan *chan;
  637. u32 channel;
  638. int result = SPS_ERROR;
  639. SPS_DBG2("sps:sps_dma_pipe_enable.pipe %d", pipe_index);
  640. mutex_lock(&bam_dma_lock);
  641. dev = sps_dma_find_device((unsigned long) bam);
  642. if (dev == NULL) {
  643. SPS_ERR("sps:BAM-DMA: invalid BAM");
  644. goto exit_err;
  645. }
  646. if (pipe_index >= dev->num_pipes) {
  647. SPS_ERR("sps:BAM-DMA: BAM %pa invalid pipe: %d",
  648. &bam->props.phys_addr, pipe_index);
  649. goto exit_err;
  650. }
  651. if (dev->pipes[pipe_index] != PIPE_ACTIVE) {
  652. SPS_ERR("sps:BAM-DMA: BAM %pa pipe %d not active",
  653. &bam->props.phys_addr, pipe_index);
  654. goto exit_err;
  655. }
  656. /*
  657. * The channel must be enabled when the dest/input/write pipe
  658. * is enabled
  659. */
  660. if (DMA_PIPE_IS_DEST(pipe_index)) {
  661. /* Configure and enable the channel */
  662. channel = pipe_index / 2;
  663. chan = &dev->chans[channel];
  664. if (chan->threshold != SPS_DMA_THRESHOLD_DEFAULT)
  665. dma_write_reg_field(dev->virt_addr,
  666. DMA_CHNL_CONFIG(channel),
  667. DMA_CHNL_ACT_THRESH,
  668. chan->thresh);
  669. if (chan->priority != SPS_DMA_PRI_DEFAULT)
  670. dma_write_reg_field(dev->virt_addr,
  671. DMA_CHNL_CONFIG(channel),
  672. DMA_CHNL_WEIGHT,
  673. chan->weight);
  674. dma_write_reg_field(dev->virt_addr,
  675. DMA_CHNL_CONFIG(channel),
  676. DMA_CHNL_ENABLE, 1);
  677. }
  678. result = 0;
  679. exit_err:
  680. mutex_unlock(&bam_dma_lock);
  681. return result;
  682. }
  683. /**
  684. * Deactivate a BAM DMA pipe
  685. *
  686. * This function deactivates a BAM DMA pipe.
  687. *
  688. * @dev - pointer to BAM-DMA device descriptor
  689. *
  690. * @bam - pointer to BAM device descriptor
  691. *
  692. * @pipe_index - pipe index
  693. *
  694. * @return 0 on success, negative value on error
  695. *
  696. */
  697. static int sps_dma_deactivate_pipe_atomic(struct bamdma_device *dev,
  698. struct sps_bam *bam,
  699. u32 pipe_index)
  700. {
  701. u32 channel;
  702. if (dev->bam != bam)
  703. return SPS_ERROR;
  704. if (pipe_index >= dev->num_pipes)
  705. return SPS_ERROR;
  706. if (dev->pipes[pipe_index] != PIPE_ACTIVE)
  707. return SPS_ERROR; /* Pipe is not active */
  708. SPS_DBG2("sps:BAM-DMA: deactivate pipe %d", pipe_index);
  709. /* Mark pipe inactive */
  710. dev->pipes[pipe_index] = PIPE_INACTIVE;
  711. /*
  712. * Channel must be reset when either pipe is disabled, so just always
  713. * reset regardless of other pipe's state
  714. */
  715. channel = pipe_index / 2;
  716. dma_write_reg_field(dev->virt_addr, DMA_CHNL_CONFIG(channel),
  717. DMA_CHNL_ENABLE, 0);
  718. /* If the peer pipe is also inactive, reset the channel */
  719. if (sps_dma_check_pipes(dev, pipe_index) == DMA_PIPES_BOTH_DISABLED) {
  720. /* Free channel if allocated internally */
  721. if (dev->chans[channel].state == DMA_CHAN_STATE_ALLOC_INT)
  722. dev->chans[channel].state = DMA_CHAN_STATE_FREE;
  723. }
  724. return 0;
  725. }
  726. /**
  727. * Free a BAM DMA pipe
  728. *
  729. */
  730. int sps_dma_pipe_free(void *bam_arg, u32 pipe_index)
  731. {
  732. struct bamdma_device *dev;
  733. struct sps_bam *bam = bam_arg;
  734. int result;
  735. mutex_lock(&bam_dma_lock);
  736. dev = sps_dma_find_device((unsigned long) bam);
  737. if (dev == NULL) {
  738. SPS_ERR("sps:BAM-DMA: invalid BAM");
  739. result = SPS_ERROR;
  740. goto exit_err;
  741. }
  742. result = sps_dma_deactivate_pipe_atomic(dev, bam, pipe_index);
  743. exit_err:
  744. mutex_unlock(&bam_dma_lock);
  745. return result;
  746. }
  747. /**
  748. * Get the BAM handle for BAM-DMA.
  749. *
  750. * The BAM handle should be use as source/destination in the sps_connect().
  751. *
  752. * @return bam handle on success, zero on error
  753. */
  754. unsigned long sps_dma_get_bam_handle(void)
  755. {
  756. return (unsigned long)bam_dma_dev[0].bam;
  757. }
  758. EXPORT_SYMBOL(sps_dma_get_bam_handle);
  759. /**
  760. * Free the BAM handle for BAM-DMA.
  761. *
  762. */
  763. void sps_dma_free_bam_handle(unsigned long h)
  764. {
  765. }
  766. EXPORT_SYMBOL(sps_dma_free_bam_handle);
  767. #endif /* CONFIG_SPS_SUPPORT_BAMDMA */