coh901318.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * driver/dma/coh901318.c
  3. *
  4. * Copyright (C) 2007-2009 ST-Ericsson
  5. * License terms: GNU General Public License (GPL) version 2
  6. * DMA driver for COH 901 318
  7. * Author: Per Friden <per.friden@stericsson.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h> /* printk() */
  12. #include <linux/fs.h> /* everything... */
  13. #include <linux/scatterlist.h>
  14. #include <linux/slab.h> /* kmalloc() */
  15. #include <linux/dmaengine.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/irqreturn.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/platform_data/dma-coh901318.h>
  24. #include <mach/coh901318.h>
  25. #include "coh901318_lli.h"
  26. #include "dmaengine.h"
  27. #define COHC_2_DEV(cohc) (&cohc->chan.dev->device)
  28. #ifdef VERBOSE_DEBUG
  29. #define COH_DBG(x) ({ if (1) x; 0; })
  30. #else
  31. #define COH_DBG(x) ({ if (0) x; 0; })
  32. #endif
  33. struct coh901318_desc {
  34. struct dma_async_tx_descriptor desc;
  35. struct list_head node;
  36. struct scatterlist *sg;
  37. unsigned int sg_len;
  38. struct coh901318_lli *lli;
  39. enum dma_transfer_direction dir;
  40. unsigned long flags;
  41. u32 head_config;
  42. u32 head_ctrl;
  43. };
  44. struct coh901318_base {
  45. struct device *dev;
  46. void __iomem *virtbase;
  47. struct coh901318_pool pool;
  48. struct powersave pm;
  49. struct dma_device dma_slave;
  50. struct dma_device dma_memcpy;
  51. struct coh901318_chan *chans;
  52. struct coh901318_platform *platform;
  53. };
  54. struct coh901318_chan {
  55. spinlock_t lock;
  56. int allocated;
  57. int id;
  58. int stopped;
  59. struct work_struct free_work;
  60. struct dma_chan chan;
  61. struct tasklet_struct tasklet;
  62. struct list_head active;
  63. struct list_head queue;
  64. struct list_head free;
  65. unsigned long nbr_active_done;
  66. unsigned long busy;
  67. u32 runtime_addr;
  68. u32 runtime_ctrl;
  69. struct coh901318_base *base;
  70. };
  71. static void coh901318_list_print(struct coh901318_chan *cohc,
  72. struct coh901318_lli *lli)
  73. {
  74. struct coh901318_lli *l = lli;
  75. int i = 0;
  76. while (l) {
  77. dev_vdbg(COHC_2_DEV(cohc), "i %d, lli %p, ctrl 0x%x, src 0x%x"
  78. ", dst 0x%x, link 0x%x virt_link_addr 0x%p\n",
  79. i, l, l->control, l->src_addr, l->dst_addr,
  80. l->link_addr, l->virt_link_addr);
  81. i++;
  82. l = l->virt_link_addr;
  83. }
  84. }
  85. #ifdef CONFIG_DEBUG_FS
  86. #define COH901318_DEBUGFS_ASSIGN(x, y) (x = y)
  87. static struct coh901318_base *debugfs_dma_base;
  88. static struct dentry *dma_dentry;
  89. static int coh901318_debugfs_read(struct file *file, char __user *buf,
  90. size_t count, loff_t *f_pos)
  91. {
  92. u64 started_channels = debugfs_dma_base->pm.started_channels;
  93. int pool_count = debugfs_dma_base->pool.debugfs_pool_counter;
  94. int i;
  95. int ret = 0;
  96. char *dev_buf;
  97. char *tmp;
  98. int dev_size;
  99. dev_buf = kmalloc(4*1024, GFP_KERNEL);
  100. if (dev_buf == NULL)
  101. goto err_kmalloc;
  102. tmp = dev_buf;
  103. tmp += sprintf(tmp, "DMA -- enabled dma channels\n");
  104. for (i = 0; i < debugfs_dma_base->platform->max_channels; i++)
  105. if (started_channels & (1 << i))
  106. tmp += sprintf(tmp, "channel %d\n", i);
  107. tmp += sprintf(tmp, "Pool alloc nbr %d\n", pool_count);
  108. dev_size = tmp - dev_buf;
  109. /* No more to read if offset != 0 */
  110. if (*f_pos > dev_size)
  111. goto out;
  112. if (count > dev_size - *f_pos)
  113. count = dev_size - *f_pos;
  114. if (copy_to_user(buf, dev_buf + *f_pos, count))
  115. ret = -EINVAL;
  116. ret = count;
  117. *f_pos += count;
  118. out:
  119. kfree(dev_buf);
  120. return ret;
  121. err_kmalloc:
  122. return 0;
  123. }
  124. static const struct file_operations coh901318_debugfs_status_operations = {
  125. .owner = THIS_MODULE,
  126. .open = simple_open,
  127. .read = coh901318_debugfs_read,
  128. .llseek = default_llseek,
  129. };
  130. static int __init init_coh901318_debugfs(void)
  131. {
  132. dma_dentry = debugfs_create_dir("dma", NULL);
  133. (void) debugfs_create_file("status",
  134. S_IFREG | S_IRUGO,
  135. dma_dentry, NULL,
  136. &coh901318_debugfs_status_operations);
  137. return 0;
  138. }
  139. static void __exit exit_coh901318_debugfs(void)
  140. {
  141. debugfs_remove_recursive(dma_dentry);
  142. }
  143. module_init(init_coh901318_debugfs);
  144. module_exit(exit_coh901318_debugfs);
  145. #else
  146. #define COH901318_DEBUGFS_ASSIGN(x, y)
  147. #endif /* CONFIG_DEBUG_FS */
  148. static inline struct coh901318_chan *to_coh901318_chan(struct dma_chan *chan)
  149. {
  150. return container_of(chan, struct coh901318_chan, chan);
  151. }
  152. static inline dma_addr_t
  153. cohc_dev_addr(struct coh901318_chan *cohc)
  154. {
  155. /* Runtime supplied address will take precedence */
  156. if (cohc->runtime_addr)
  157. return cohc->runtime_addr;
  158. return cohc->base->platform->chan_conf[cohc->id].dev_addr;
  159. }
  160. static inline const struct coh901318_params *
  161. cohc_chan_param(struct coh901318_chan *cohc)
  162. {
  163. return &cohc->base->platform->chan_conf[cohc->id].param;
  164. }
  165. static inline const struct coh_dma_channel *
  166. cohc_chan_conf(struct coh901318_chan *cohc)
  167. {
  168. return &cohc->base->platform->chan_conf[cohc->id];
  169. }
  170. static void enable_powersave(struct coh901318_chan *cohc)
  171. {
  172. unsigned long flags;
  173. struct powersave *pm = &cohc->base->pm;
  174. spin_lock_irqsave(&pm->lock, flags);
  175. pm->started_channels &= ~(1ULL << cohc->id);
  176. if (!pm->started_channels) {
  177. /* DMA no longer intends to access memory */
  178. cohc->base->platform->access_memory_state(cohc->base->dev,
  179. false);
  180. }
  181. spin_unlock_irqrestore(&pm->lock, flags);
  182. }
  183. static void disable_powersave(struct coh901318_chan *cohc)
  184. {
  185. unsigned long flags;
  186. struct powersave *pm = &cohc->base->pm;
  187. spin_lock_irqsave(&pm->lock, flags);
  188. if (!pm->started_channels) {
  189. /* DMA intends to access memory */
  190. cohc->base->platform->access_memory_state(cohc->base->dev,
  191. true);
  192. }
  193. pm->started_channels |= (1ULL << cohc->id);
  194. spin_unlock_irqrestore(&pm->lock, flags);
  195. }
  196. static inline int coh901318_set_ctrl(struct coh901318_chan *cohc, u32 control)
  197. {
  198. int channel = cohc->id;
  199. void __iomem *virtbase = cohc->base->virtbase;
  200. writel(control,
  201. virtbase + COH901318_CX_CTRL +
  202. COH901318_CX_CTRL_SPACING * channel);
  203. return 0;
  204. }
  205. static inline int coh901318_set_conf(struct coh901318_chan *cohc, u32 conf)
  206. {
  207. int channel = cohc->id;
  208. void __iomem *virtbase = cohc->base->virtbase;
  209. writel(conf,
  210. virtbase + COH901318_CX_CFG +
  211. COH901318_CX_CFG_SPACING*channel);
  212. return 0;
  213. }
  214. static int coh901318_start(struct coh901318_chan *cohc)
  215. {
  216. u32 val;
  217. int channel = cohc->id;
  218. void __iomem *virtbase = cohc->base->virtbase;
  219. disable_powersave(cohc);
  220. val = readl(virtbase + COH901318_CX_CFG +
  221. COH901318_CX_CFG_SPACING * channel);
  222. /* Enable channel */
  223. val |= COH901318_CX_CFG_CH_ENABLE;
  224. writel(val, virtbase + COH901318_CX_CFG +
  225. COH901318_CX_CFG_SPACING * channel);
  226. return 0;
  227. }
  228. static int coh901318_prep_linked_list(struct coh901318_chan *cohc,
  229. struct coh901318_lli *lli)
  230. {
  231. int channel = cohc->id;
  232. void __iomem *virtbase = cohc->base->virtbase;
  233. BUG_ON(readl(virtbase + COH901318_CX_STAT +
  234. COH901318_CX_STAT_SPACING*channel) &
  235. COH901318_CX_STAT_ACTIVE);
  236. writel(lli->src_addr,
  237. virtbase + COH901318_CX_SRC_ADDR +
  238. COH901318_CX_SRC_ADDR_SPACING * channel);
  239. writel(lli->dst_addr, virtbase +
  240. COH901318_CX_DST_ADDR +
  241. COH901318_CX_DST_ADDR_SPACING * channel);
  242. writel(lli->link_addr, virtbase + COH901318_CX_LNK_ADDR +
  243. COH901318_CX_LNK_ADDR_SPACING * channel);
  244. writel(lli->control, virtbase + COH901318_CX_CTRL +
  245. COH901318_CX_CTRL_SPACING * channel);
  246. return 0;
  247. }
  248. static struct coh901318_desc *
  249. coh901318_desc_get(struct coh901318_chan *cohc)
  250. {
  251. struct coh901318_desc *desc;
  252. if (list_empty(&cohc->free)) {
  253. /* alloc new desc because we're out of used ones
  254. * TODO: alloc a pile of descs instead of just one,
  255. * avoid many small allocations.
  256. */
  257. desc = kzalloc(sizeof(struct coh901318_desc), GFP_NOWAIT);
  258. if (desc == NULL)
  259. goto out;
  260. INIT_LIST_HEAD(&desc->node);
  261. dma_async_tx_descriptor_init(&desc->desc, &cohc->chan);
  262. } else {
  263. /* Reuse an old desc. */
  264. desc = list_first_entry(&cohc->free,
  265. struct coh901318_desc,
  266. node);
  267. list_del(&desc->node);
  268. /* Initialize it a bit so it's not insane */
  269. desc->sg = NULL;
  270. desc->sg_len = 0;
  271. desc->desc.callback = NULL;
  272. desc->desc.callback_param = NULL;
  273. }
  274. out:
  275. return desc;
  276. }
  277. static void
  278. coh901318_desc_free(struct coh901318_chan *cohc, struct coh901318_desc *cohd)
  279. {
  280. list_add_tail(&cohd->node, &cohc->free);
  281. }
  282. /* call with irq lock held */
  283. static void
  284. coh901318_desc_submit(struct coh901318_chan *cohc, struct coh901318_desc *desc)
  285. {
  286. list_add_tail(&desc->node, &cohc->active);
  287. }
  288. static struct coh901318_desc *
  289. coh901318_first_active_get(struct coh901318_chan *cohc)
  290. {
  291. struct coh901318_desc *d;
  292. if (list_empty(&cohc->active))
  293. return NULL;
  294. d = list_first_entry(&cohc->active,
  295. struct coh901318_desc,
  296. node);
  297. return d;
  298. }
  299. static void
  300. coh901318_desc_remove(struct coh901318_desc *cohd)
  301. {
  302. list_del(&cohd->node);
  303. }
  304. static void
  305. coh901318_desc_queue(struct coh901318_chan *cohc, struct coh901318_desc *desc)
  306. {
  307. list_add_tail(&desc->node, &cohc->queue);
  308. }
  309. static struct coh901318_desc *
  310. coh901318_first_queued(struct coh901318_chan *cohc)
  311. {
  312. struct coh901318_desc *d;
  313. if (list_empty(&cohc->queue))
  314. return NULL;
  315. d = list_first_entry(&cohc->queue,
  316. struct coh901318_desc,
  317. node);
  318. return d;
  319. }
  320. static inline u32 coh901318_get_bytes_in_lli(struct coh901318_lli *in_lli)
  321. {
  322. struct coh901318_lli *lli = in_lli;
  323. u32 bytes = 0;
  324. while (lli) {
  325. bytes += lli->control & COH901318_CX_CTRL_TC_VALUE_MASK;
  326. lli = lli->virt_link_addr;
  327. }
  328. return bytes;
  329. }
  330. /*
  331. * Get the number of bytes left to transfer on this channel,
  332. * it is unwise to call this before stopping the channel for
  333. * absolute measures, but for a rough guess you can still call
  334. * it.
  335. */
  336. static u32 coh901318_get_bytes_left(struct dma_chan *chan)
  337. {
  338. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  339. struct coh901318_desc *cohd;
  340. struct list_head *pos;
  341. unsigned long flags;
  342. u32 left = 0;
  343. int i = 0;
  344. spin_lock_irqsave(&cohc->lock, flags);
  345. /*
  346. * If there are many queued jobs, we iterate and add the
  347. * size of them all. We take a special look on the first
  348. * job though, since it is probably active.
  349. */
  350. list_for_each(pos, &cohc->active) {
  351. /*
  352. * The first job in the list will be working on the
  353. * hardware. The job can be stopped but still active,
  354. * so that the transfer counter is somewhere inside
  355. * the buffer.
  356. */
  357. cohd = list_entry(pos, struct coh901318_desc, node);
  358. if (i == 0) {
  359. struct coh901318_lli *lli;
  360. dma_addr_t ladd;
  361. /* Read current transfer count value */
  362. left = readl(cohc->base->virtbase +
  363. COH901318_CX_CTRL +
  364. COH901318_CX_CTRL_SPACING * cohc->id) &
  365. COH901318_CX_CTRL_TC_VALUE_MASK;
  366. /* See if the transfer is linked... */
  367. ladd = readl(cohc->base->virtbase +
  368. COH901318_CX_LNK_ADDR +
  369. COH901318_CX_LNK_ADDR_SPACING *
  370. cohc->id) &
  371. ~COH901318_CX_LNK_LINK_IMMEDIATE;
  372. /* Single transaction */
  373. if (!ladd)
  374. continue;
  375. /*
  376. * Linked transaction, follow the lli, find the
  377. * currently processing lli, and proceed to the next
  378. */
  379. lli = cohd->lli;
  380. while (lli && lli->link_addr != ladd)
  381. lli = lli->virt_link_addr;
  382. if (lli)
  383. lli = lli->virt_link_addr;
  384. /*
  385. * Follow remaining lli links around to count the total
  386. * number of bytes left
  387. */
  388. left += coh901318_get_bytes_in_lli(lli);
  389. } else {
  390. left += coh901318_get_bytes_in_lli(cohd->lli);
  391. }
  392. i++;
  393. }
  394. /* Also count bytes in the queued jobs */
  395. list_for_each(pos, &cohc->queue) {
  396. cohd = list_entry(pos, struct coh901318_desc, node);
  397. left += coh901318_get_bytes_in_lli(cohd->lli);
  398. }
  399. spin_unlock_irqrestore(&cohc->lock, flags);
  400. return left;
  401. }
  402. /*
  403. * Pauses a transfer without losing data. Enables power save.
  404. * Use this function in conjunction with coh901318_resume.
  405. */
  406. static void coh901318_pause(struct dma_chan *chan)
  407. {
  408. u32 val;
  409. unsigned long flags;
  410. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  411. int channel = cohc->id;
  412. void __iomem *virtbase = cohc->base->virtbase;
  413. spin_lock_irqsave(&cohc->lock, flags);
  414. /* Disable channel in HW */
  415. val = readl(virtbase + COH901318_CX_CFG +
  416. COH901318_CX_CFG_SPACING * channel);
  417. /* Stopping infinite transfer */
  418. if ((val & COH901318_CX_CTRL_TC_ENABLE) == 0 &&
  419. (val & COH901318_CX_CFG_CH_ENABLE))
  420. cohc->stopped = 1;
  421. val &= ~COH901318_CX_CFG_CH_ENABLE;
  422. /* Enable twice, HW bug work around */
  423. writel(val, virtbase + COH901318_CX_CFG +
  424. COH901318_CX_CFG_SPACING * channel);
  425. writel(val, virtbase + COH901318_CX_CFG +
  426. COH901318_CX_CFG_SPACING * channel);
  427. /* Spin-wait for it to actually go inactive */
  428. while (readl(virtbase + COH901318_CX_STAT+COH901318_CX_STAT_SPACING *
  429. channel) & COH901318_CX_STAT_ACTIVE)
  430. cpu_relax();
  431. /* Check if we stopped an active job */
  432. if ((readl(virtbase + COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
  433. channel) & COH901318_CX_CTRL_TC_VALUE_MASK) > 0)
  434. cohc->stopped = 1;
  435. enable_powersave(cohc);
  436. spin_unlock_irqrestore(&cohc->lock, flags);
  437. }
  438. /* Resumes a transfer that has been stopped via 300_dma_stop(..).
  439. Power save is handled.
  440. */
  441. static void coh901318_resume(struct dma_chan *chan)
  442. {
  443. u32 val;
  444. unsigned long flags;
  445. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  446. int channel = cohc->id;
  447. spin_lock_irqsave(&cohc->lock, flags);
  448. disable_powersave(cohc);
  449. if (cohc->stopped) {
  450. /* Enable channel in HW */
  451. val = readl(cohc->base->virtbase + COH901318_CX_CFG +
  452. COH901318_CX_CFG_SPACING * channel);
  453. val |= COH901318_CX_CFG_CH_ENABLE;
  454. writel(val, cohc->base->virtbase + COH901318_CX_CFG +
  455. COH901318_CX_CFG_SPACING*channel);
  456. cohc->stopped = 0;
  457. }
  458. spin_unlock_irqrestore(&cohc->lock, flags);
  459. }
  460. bool coh901318_filter_id(struct dma_chan *chan, void *chan_id)
  461. {
  462. unsigned int ch_nr = (unsigned int) chan_id;
  463. if (ch_nr == to_coh901318_chan(chan)->id)
  464. return true;
  465. return false;
  466. }
  467. EXPORT_SYMBOL(coh901318_filter_id);
  468. /*
  469. * DMA channel allocation
  470. */
  471. static int coh901318_config(struct coh901318_chan *cohc,
  472. struct coh901318_params *param)
  473. {
  474. unsigned long flags;
  475. const struct coh901318_params *p;
  476. int channel = cohc->id;
  477. void __iomem *virtbase = cohc->base->virtbase;
  478. spin_lock_irqsave(&cohc->lock, flags);
  479. if (param)
  480. p = param;
  481. else
  482. p = &cohc->base->platform->chan_conf[channel].param;
  483. /* Clear any pending BE or TC interrupt */
  484. if (channel < 32) {
  485. writel(1 << channel, virtbase + COH901318_BE_INT_CLEAR1);
  486. writel(1 << channel, virtbase + COH901318_TC_INT_CLEAR1);
  487. } else {
  488. writel(1 << (channel - 32), virtbase +
  489. COH901318_BE_INT_CLEAR2);
  490. writel(1 << (channel - 32), virtbase +
  491. COH901318_TC_INT_CLEAR2);
  492. }
  493. coh901318_set_conf(cohc, p->config);
  494. coh901318_set_ctrl(cohc, p->ctrl_lli_last);
  495. spin_unlock_irqrestore(&cohc->lock, flags);
  496. return 0;
  497. }
  498. /* must lock when calling this function
  499. * start queued jobs, if any
  500. * TODO: start all queued jobs in one go
  501. *
  502. * Returns descriptor if queued job is started otherwise NULL.
  503. * If the queue is empty NULL is returned.
  504. */
  505. static struct coh901318_desc *coh901318_queue_start(struct coh901318_chan *cohc)
  506. {
  507. struct coh901318_desc *cohd;
  508. /*
  509. * start queued jobs, if any
  510. * TODO: transmit all queued jobs in one go
  511. */
  512. cohd = coh901318_first_queued(cohc);
  513. if (cohd != NULL) {
  514. /* Remove from queue */
  515. coh901318_desc_remove(cohd);
  516. /* initiate DMA job */
  517. cohc->busy = 1;
  518. coh901318_desc_submit(cohc, cohd);
  519. /* Program the transaction head */
  520. coh901318_set_conf(cohc, cohd->head_config);
  521. coh901318_set_ctrl(cohc, cohd->head_ctrl);
  522. coh901318_prep_linked_list(cohc, cohd->lli);
  523. /* start dma job on this channel */
  524. coh901318_start(cohc);
  525. }
  526. return cohd;
  527. }
  528. /*
  529. * This tasklet is called from the interrupt handler to
  530. * handle each descriptor (DMA job) that is sent to a channel.
  531. */
  532. static void dma_tasklet(unsigned long data)
  533. {
  534. struct coh901318_chan *cohc = (struct coh901318_chan *) data;
  535. struct coh901318_desc *cohd_fin;
  536. unsigned long flags;
  537. dma_async_tx_callback callback;
  538. void *callback_param;
  539. dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d"
  540. " nbr_active_done %ld\n", __func__,
  541. cohc->id, cohc->nbr_active_done);
  542. spin_lock_irqsave(&cohc->lock, flags);
  543. /* get first active descriptor entry from list */
  544. cohd_fin = coh901318_first_active_get(cohc);
  545. if (cohd_fin == NULL)
  546. goto err;
  547. /* locate callback to client */
  548. callback = cohd_fin->desc.callback;
  549. callback_param = cohd_fin->desc.callback_param;
  550. /* sign this job as completed on the channel */
  551. dma_cookie_complete(&cohd_fin->desc);
  552. /* release the lli allocation and remove the descriptor */
  553. coh901318_lli_free(&cohc->base->pool, &cohd_fin->lli);
  554. /* return desc to free-list */
  555. coh901318_desc_remove(cohd_fin);
  556. coh901318_desc_free(cohc, cohd_fin);
  557. spin_unlock_irqrestore(&cohc->lock, flags);
  558. /* Call the callback when we're done */
  559. if (callback)
  560. callback(callback_param);
  561. spin_lock_irqsave(&cohc->lock, flags);
  562. /*
  563. * If another interrupt fired while the tasklet was scheduling,
  564. * we don't get called twice, so we have this number of active
  565. * counter that keep track of the number of IRQs expected to
  566. * be handled for this channel. If there happen to be more than
  567. * one IRQ to be ack:ed, we simply schedule this tasklet again.
  568. */
  569. cohc->nbr_active_done--;
  570. if (cohc->nbr_active_done) {
  571. dev_dbg(COHC_2_DEV(cohc), "scheduling tasklet again, new IRQs "
  572. "came in while we were scheduling this tasklet\n");
  573. if (cohc_chan_conf(cohc)->priority_high)
  574. tasklet_hi_schedule(&cohc->tasklet);
  575. else
  576. tasklet_schedule(&cohc->tasklet);
  577. }
  578. spin_unlock_irqrestore(&cohc->lock, flags);
  579. return;
  580. err:
  581. spin_unlock_irqrestore(&cohc->lock, flags);
  582. dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
  583. }
  584. /* called from interrupt context */
  585. static void dma_tc_handle(struct coh901318_chan *cohc)
  586. {
  587. /*
  588. * If the channel is not allocated, then we shouldn't have
  589. * any TC interrupts on it.
  590. */
  591. if (!cohc->allocated) {
  592. dev_err(COHC_2_DEV(cohc), "spurious interrupt from "
  593. "unallocated channel\n");
  594. return;
  595. }
  596. spin_lock(&cohc->lock);
  597. /*
  598. * When we reach this point, at least one queue item
  599. * should have been moved over from cohc->queue to
  600. * cohc->active and run to completion, that is why we're
  601. * getting a terminal count interrupt is it not?
  602. * If you get this BUG() the most probable cause is that
  603. * the individual nodes in the lli chain have IRQ enabled,
  604. * so check your platform config for lli chain ctrl.
  605. */
  606. BUG_ON(list_empty(&cohc->active));
  607. cohc->nbr_active_done++;
  608. /*
  609. * This attempt to take a job from cohc->queue, put it
  610. * into cohc->active and start it.
  611. */
  612. if (coh901318_queue_start(cohc) == NULL)
  613. cohc->busy = 0;
  614. spin_unlock(&cohc->lock);
  615. /*
  616. * This tasklet will remove items from cohc->active
  617. * and thus terminates them.
  618. */
  619. if (cohc_chan_conf(cohc)->priority_high)
  620. tasklet_hi_schedule(&cohc->tasklet);
  621. else
  622. tasklet_schedule(&cohc->tasklet);
  623. }
  624. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  625. {
  626. u32 status1;
  627. u32 status2;
  628. int i;
  629. int ch;
  630. struct coh901318_base *base = dev_id;
  631. struct coh901318_chan *cohc;
  632. void __iomem *virtbase = base->virtbase;
  633. status1 = readl(virtbase + COH901318_INT_STATUS1);
  634. status2 = readl(virtbase + COH901318_INT_STATUS2);
  635. if (unlikely(status1 == 0 && status2 == 0)) {
  636. dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
  637. return IRQ_HANDLED;
  638. }
  639. /* TODO: consider handle IRQ in tasklet here to
  640. * minimize interrupt latency */
  641. /* Check the first 32 DMA channels for IRQ */
  642. while (status1) {
  643. /* Find first bit set, return as a number. */
  644. i = ffs(status1) - 1;
  645. ch = i;
  646. cohc = &base->chans[ch];
  647. spin_lock(&cohc->lock);
  648. /* Mask off this bit */
  649. status1 &= ~(1 << i);
  650. /* Check the individual channel bits */
  651. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
  652. dev_crit(COHC_2_DEV(cohc),
  653. "DMA bus error on channel %d!\n", ch);
  654. BUG_ON(1);
  655. /* Clear BE interrupt */
  656. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
  657. } else {
  658. /* Caused by TC, really? */
  659. if (unlikely(!test_bit(i, virtbase +
  660. COH901318_TC_INT_STATUS1))) {
  661. dev_warn(COHC_2_DEV(cohc),
  662. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  663. /* Clear TC interrupt */
  664. BUG_ON(1);
  665. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  666. } else {
  667. /* Enable powersave if transfer has finished */
  668. if (!(readl(virtbase + COH901318_CX_STAT +
  669. COH901318_CX_STAT_SPACING*ch) &
  670. COH901318_CX_STAT_ENABLED)) {
  671. enable_powersave(cohc);
  672. }
  673. /* Must clear TC interrupt before calling
  674. * dma_tc_handle
  675. * in case tc_handle initiate a new dma job
  676. */
  677. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  678. dma_tc_handle(cohc);
  679. }
  680. }
  681. spin_unlock(&cohc->lock);
  682. }
  683. /* Check the remaining 32 DMA channels for IRQ */
  684. while (status2) {
  685. /* Find first bit set, return as a number. */
  686. i = ffs(status2) - 1;
  687. ch = i + 32;
  688. cohc = &base->chans[ch];
  689. spin_lock(&cohc->lock);
  690. /* Mask off this bit */
  691. status2 &= ~(1 << i);
  692. /* Check the individual channel bits */
  693. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
  694. dev_crit(COHC_2_DEV(cohc),
  695. "DMA bus error on channel %d!\n", ch);
  696. /* Clear BE interrupt */
  697. BUG_ON(1);
  698. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
  699. } else {
  700. /* Caused by TC, really? */
  701. if (unlikely(!test_bit(i, virtbase +
  702. COH901318_TC_INT_STATUS2))) {
  703. dev_warn(COHC_2_DEV(cohc),
  704. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  705. /* Clear TC interrupt */
  706. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  707. BUG_ON(1);
  708. } else {
  709. /* Enable powersave if transfer has finished */
  710. if (!(readl(virtbase + COH901318_CX_STAT +
  711. COH901318_CX_STAT_SPACING*ch) &
  712. COH901318_CX_STAT_ENABLED)) {
  713. enable_powersave(cohc);
  714. }
  715. /* Must clear TC interrupt before calling
  716. * dma_tc_handle
  717. * in case tc_handle initiate a new dma job
  718. */
  719. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  720. dma_tc_handle(cohc);
  721. }
  722. }
  723. spin_unlock(&cohc->lock);
  724. }
  725. return IRQ_HANDLED;
  726. }
  727. static int coh901318_alloc_chan_resources(struct dma_chan *chan)
  728. {
  729. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  730. unsigned long flags;
  731. dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
  732. __func__, cohc->id);
  733. if (chan->client_count > 1)
  734. return -EBUSY;
  735. spin_lock_irqsave(&cohc->lock, flags);
  736. coh901318_config(cohc, NULL);
  737. cohc->allocated = 1;
  738. dma_cookie_init(chan);
  739. spin_unlock_irqrestore(&cohc->lock, flags);
  740. return 1;
  741. }
  742. static void
  743. coh901318_free_chan_resources(struct dma_chan *chan)
  744. {
  745. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  746. int channel = cohc->id;
  747. unsigned long flags;
  748. spin_lock_irqsave(&cohc->lock, flags);
  749. /* Disable HW */
  750. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
  751. COH901318_CX_CFG_SPACING*channel);
  752. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
  753. COH901318_CX_CTRL_SPACING*channel);
  754. cohc->allocated = 0;
  755. spin_unlock_irqrestore(&cohc->lock, flags);
  756. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  757. }
  758. static dma_cookie_t
  759. coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
  760. {
  761. struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
  762. desc);
  763. struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
  764. unsigned long flags;
  765. dma_cookie_t cookie;
  766. spin_lock_irqsave(&cohc->lock, flags);
  767. cookie = dma_cookie_assign(tx);
  768. coh901318_desc_queue(cohc, cohd);
  769. spin_unlock_irqrestore(&cohc->lock, flags);
  770. return cookie;
  771. }
  772. static struct dma_async_tx_descriptor *
  773. coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  774. size_t size, unsigned long flags)
  775. {
  776. struct coh901318_lli *lli;
  777. struct coh901318_desc *cohd;
  778. unsigned long flg;
  779. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  780. int lli_len;
  781. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  782. int ret;
  783. spin_lock_irqsave(&cohc->lock, flg);
  784. dev_vdbg(COHC_2_DEV(cohc),
  785. "[%s] channel %d src 0x%x dest 0x%x size %d\n",
  786. __func__, cohc->id, src, dest, size);
  787. if (flags & DMA_PREP_INTERRUPT)
  788. /* Trigger interrupt after last lli */
  789. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  790. lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  791. if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  792. lli_len++;
  793. lli = coh901318_lli_alloc(&cohc->base->pool, lli_len);
  794. if (lli == NULL)
  795. goto err;
  796. ret = coh901318_lli_fill_memcpy(
  797. &cohc->base->pool, lli, src, size, dest,
  798. cohc_chan_param(cohc)->ctrl_lli_chained,
  799. ctrl_last);
  800. if (ret)
  801. goto err;
  802. COH_DBG(coh901318_list_print(cohc, lli));
  803. /* Pick a descriptor to handle this transfer */
  804. cohd = coh901318_desc_get(cohc);
  805. cohd->lli = lli;
  806. cohd->flags = flags;
  807. cohd->desc.tx_submit = coh901318_tx_submit;
  808. spin_unlock_irqrestore(&cohc->lock, flg);
  809. return &cohd->desc;
  810. err:
  811. spin_unlock_irqrestore(&cohc->lock, flg);
  812. return NULL;
  813. }
  814. static struct dma_async_tx_descriptor *
  815. coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  816. unsigned int sg_len, enum dma_transfer_direction direction,
  817. unsigned long flags, void *context)
  818. {
  819. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  820. struct coh901318_lli *lli;
  821. struct coh901318_desc *cohd;
  822. const struct coh901318_params *params;
  823. struct scatterlist *sg;
  824. int len = 0;
  825. int size;
  826. int i;
  827. u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
  828. u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
  829. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  830. u32 config;
  831. unsigned long flg;
  832. int ret;
  833. if (!sgl)
  834. goto out;
  835. if (sg_dma_len(sgl) == 0)
  836. goto out;
  837. spin_lock_irqsave(&cohc->lock, flg);
  838. dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
  839. __func__, sg_len, direction);
  840. if (flags & DMA_PREP_INTERRUPT)
  841. /* Trigger interrupt after last lli */
  842. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  843. params = cohc_chan_param(cohc);
  844. config = params->config;
  845. /*
  846. * Add runtime-specific control on top, make
  847. * sure the bits you set per peripheral channel are
  848. * cleared in the default config from the platform.
  849. */
  850. ctrl_chained |= cohc->runtime_ctrl;
  851. ctrl_last |= cohc->runtime_ctrl;
  852. ctrl |= cohc->runtime_ctrl;
  853. if (direction == DMA_MEM_TO_DEV) {
  854. u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
  855. COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
  856. config |= COH901318_CX_CFG_RM_MEMORY_TO_PRIMARY;
  857. ctrl_chained |= tx_flags;
  858. ctrl_last |= tx_flags;
  859. ctrl |= tx_flags;
  860. } else if (direction == DMA_DEV_TO_MEM) {
  861. u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
  862. COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
  863. config |= COH901318_CX_CFG_RM_PRIMARY_TO_MEMORY;
  864. ctrl_chained |= rx_flags;
  865. ctrl_last |= rx_flags;
  866. ctrl |= rx_flags;
  867. } else
  868. goto err_direction;
  869. /* The dma only supports transmitting packages up to
  870. * MAX_DMA_PACKET_SIZE. Calculate to total number of
  871. * dma elemts required to send the entire sg list
  872. */
  873. for_each_sg(sgl, sg, sg_len, i) {
  874. unsigned int factor;
  875. size = sg_dma_len(sg);
  876. if (size <= MAX_DMA_PACKET_SIZE) {
  877. len++;
  878. continue;
  879. }
  880. factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  881. if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  882. factor++;
  883. len += factor;
  884. }
  885. pr_debug("Allocate %d lli:s for this transfer\n", len);
  886. lli = coh901318_lli_alloc(&cohc->base->pool, len);
  887. if (lli == NULL)
  888. goto err_dma_alloc;
  889. /* initiate allocated lli list */
  890. ret = coh901318_lli_fill_sg(&cohc->base->pool, lli, sgl, sg_len,
  891. cohc_dev_addr(cohc),
  892. ctrl_chained,
  893. ctrl,
  894. ctrl_last,
  895. direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
  896. if (ret)
  897. goto err_lli_fill;
  898. COH_DBG(coh901318_list_print(cohc, lli));
  899. /* Pick a descriptor to handle this transfer */
  900. cohd = coh901318_desc_get(cohc);
  901. cohd->head_config = config;
  902. /*
  903. * Set the default head ctrl for the channel to the one from the
  904. * lli, things may have changed due to odd buffer alignment
  905. * etc.
  906. */
  907. cohd->head_ctrl = lli->control;
  908. cohd->dir = direction;
  909. cohd->flags = flags;
  910. cohd->desc.tx_submit = coh901318_tx_submit;
  911. cohd->lli = lli;
  912. spin_unlock_irqrestore(&cohc->lock, flg);
  913. return &cohd->desc;
  914. err_lli_fill:
  915. err_dma_alloc:
  916. err_direction:
  917. spin_unlock_irqrestore(&cohc->lock, flg);
  918. out:
  919. return NULL;
  920. }
  921. static enum dma_status
  922. coh901318_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  923. struct dma_tx_state *txstate)
  924. {
  925. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  926. enum dma_status ret;
  927. ret = dma_cookie_status(chan, cookie, txstate);
  928. /* FIXME: should be conditional on ret != DMA_SUCCESS? */
  929. dma_set_residue(txstate, coh901318_get_bytes_left(chan));
  930. if (ret == DMA_IN_PROGRESS && cohc->stopped)
  931. ret = DMA_PAUSED;
  932. return ret;
  933. }
  934. static void
  935. coh901318_issue_pending(struct dma_chan *chan)
  936. {
  937. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  938. unsigned long flags;
  939. spin_lock_irqsave(&cohc->lock, flags);
  940. /*
  941. * Busy means that pending jobs are already being processed,
  942. * and then there is no point in starting the queue: the
  943. * terminal count interrupt on the channel will take the next
  944. * job on the queue and execute it anyway.
  945. */
  946. if (!cohc->busy)
  947. coh901318_queue_start(cohc);
  948. spin_unlock_irqrestore(&cohc->lock, flags);
  949. }
  950. /*
  951. * Here we wrap in the runtime dma control interface
  952. */
  953. struct burst_table {
  954. int burst_8bit;
  955. int burst_16bit;
  956. int burst_32bit;
  957. u32 reg;
  958. };
  959. static const struct burst_table burst_sizes[] = {
  960. {
  961. .burst_8bit = 64,
  962. .burst_16bit = 32,
  963. .burst_32bit = 16,
  964. .reg = COH901318_CX_CTRL_BURST_COUNT_64_BYTES,
  965. },
  966. {
  967. .burst_8bit = 48,
  968. .burst_16bit = 24,
  969. .burst_32bit = 12,
  970. .reg = COH901318_CX_CTRL_BURST_COUNT_48_BYTES,
  971. },
  972. {
  973. .burst_8bit = 32,
  974. .burst_16bit = 16,
  975. .burst_32bit = 8,
  976. .reg = COH901318_CX_CTRL_BURST_COUNT_32_BYTES,
  977. },
  978. {
  979. .burst_8bit = 16,
  980. .burst_16bit = 8,
  981. .burst_32bit = 4,
  982. .reg = COH901318_CX_CTRL_BURST_COUNT_16_BYTES,
  983. },
  984. {
  985. .burst_8bit = 8,
  986. .burst_16bit = 4,
  987. .burst_32bit = 2,
  988. .reg = COH901318_CX_CTRL_BURST_COUNT_8_BYTES,
  989. },
  990. {
  991. .burst_8bit = 4,
  992. .burst_16bit = 2,
  993. .burst_32bit = 1,
  994. .reg = COH901318_CX_CTRL_BURST_COUNT_4_BYTES,
  995. },
  996. {
  997. .burst_8bit = 2,
  998. .burst_16bit = 1,
  999. .burst_32bit = 0,
  1000. .reg = COH901318_CX_CTRL_BURST_COUNT_2_BYTES,
  1001. },
  1002. {
  1003. .burst_8bit = 1,
  1004. .burst_16bit = 0,
  1005. .burst_32bit = 0,
  1006. .reg = COH901318_CX_CTRL_BURST_COUNT_1_BYTE,
  1007. },
  1008. };
  1009. static void coh901318_dma_set_runtimeconfig(struct dma_chan *chan,
  1010. struct dma_slave_config *config)
  1011. {
  1012. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  1013. dma_addr_t addr;
  1014. enum dma_slave_buswidth addr_width;
  1015. u32 maxburst;
  1016. u32 runtime_ctrl = 0;
  1017. int i = 0;
  1018. /* We only support mem to per or per to mem transfers */
  1019. if (config->direction == DMA_DEV_TO_MEM) {
  1020. addr = config->src_addr;
  1021. addr_width = config->src_addr_width;
  1022. maxburst = config->src_maxburst;
  1023. } else if (config->direction == DMA_MEM_TO_DEV) {
  1024. addr = config->dst_addr;
  1025. addr_width = config->dst_addr_width;
  1026. maxburst = config->dst_maxburst;
  1027. } else {
  1028. dev_err(COHC_2_DEV(cohc), "illegal channel mode\n");
  1029. return;
  1030. }
  1031. dev_dbg(COHC_2_DEV(cohc), "configure channel for %d byte transfers\n",
  1032. addr_width);
  1033. switch (addr_width) {
  1034. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  1035. runtime_ctrl |=
  1036. COH901318_CX_CTRL_SRC_BUS_SIZE_8_BITS |
  1037. COH901318_CX_CTRL_DST_BUS_SIZE_8_BITS;
  1038. while (i < ARRAY_SIZE(burst_sizes)) {
  1039. if (burst_sizes[i].burst_8bit <= maxburst)
  1040. break;
  1041. i++;
  1042. }
  1043. break;
  1044. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  1045. runtime_ctrl |=
  1046. COH901318_CX_CTRL_SRC_BUS_SIZE_16_BITS |
  1047. COH901318_CX_CTRL_DST_BUS_SIZE_16_BITS;
  1048. while (i < ARRAY_SIZE(burst_sizes)) {
  1049. if (burst_sizes[i].burst_16bit <= maxburst)
  1050. break;
  1051. i++;
  1052. }
  1053. break;
  1054. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  1055. /* Direction doesn't matter here, it's 32/32 bits */
  1056. runtime_ctrl |=
  1057. COH901318_CX_CTRL_SRC_BUS_SIZE_32_BITS |
  1058. COH901318_CX_CTRL_DST_BUS_SIZE_32_BITS;
  1059. while (i < ARRAY_SIZE(burst_sizes)) {
  1060. if (burst_sizes[i].burst_32bit <= maxburst)
  1061. break;
  1062. i++;
  1063. }
  1064. break;
  1065. default:
  1066. dev_err(COHC_2_DEV(cohc),
  1067. "bad runtimeconfig: alien address width\n");
  1068. return;
  1069. }
  1070. runtime_ctrl |= burst_sizes[i].reg;
  1071. dev_dbg(COHC_2_DEV(cohc),
  1072. "selected burst size %d bytes for address width %d bytes, maxburst %d\n",
  1073. burst_sizes[i].burst_8bit, addr_width, maxburst);
  1074. cohc->runtime_addr = addr;
  1075. cohc->runtime_ctrl = runtime_ctrl;
  1076. }
  1077. static int
  1078. coh901318_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1079. unsigned long arg)
  1080. {
  1081. unsigned long flags;
  1082. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  1083. struct coh901318_desc *cohd;
  1084. void __iomem *virtbase = cohc->base->virtbase;
  1085. if (cmd == DMA_SLAVE_CONFIG) {
  1086. struct dma_slave_config *config =
  1087. (struct dma_slave_config *) arg;
  1088. coh901318_dma_set_runtimeconfig(chan, config);
  1089. return 0;
  1090. }
  1091. if (cmd == DMA_PAUSE) {
  1092. coh901318_pause(chan);
  1093. return 0;
  1094. }
  1095. if (cmd == DMA_RESUME) {
  1096. coh901318_resume(chan);
  1097. return 0;
  1098. }
  1099. if (cmd != DMA_TERMINATE_ALL)
  1100. return -ENXIO;
  1101. /* The remainder of this function terminates the transfer */
  1102. coh901318_pause(chan);
  1103. spin_lock_irqsave(&cohc->lock, flags);
  1104. /* Clear any pending BE or TC interrupt */
  1105. if (cohc->id < 32) {
  1106. writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
  1107. writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
  1108. } else {
  1109. writel(1 << (cohc->id - 32), virtbase +
  1110. COH901318_BE_INT_CLEAR2);
  1111. writel(1 << (cohc->id - 32), virtbase +
  1112. COH901318_TC_INT_CLEAR2);
  1113. }
  1114. enable_powersave(cohc);
  1115. while ((cohd = coh901318_first_active_get(cohc))) {
  1116. /* release the lli allocation*/
  1117. coh901318_lli_free(&cohc->base->pool, &cohd->lli);
  1118. /* return desc to free-list */
  1119. coh901318_desc_remove(cohd);
  1120. coh901318_desc_free(cohc, cohd);
  1121. }
  1122. while ((cohd = coh901318_first_queued(cohc))) {
  1123. /* release the lli allocation*/
  1124. coh901318_lli_free(&cohc->base->pool, &cohd->lli);
  1125. /* return desc to free-list */
  1126. coh901318_desc_remove(cohd);
  1127. coh901318_desc_free(cohc, cohd);
  1128. }
  1129. cohc->nbr_active_done = 0;
  1130. cohc->busy = 0;
  1131. spin_unlock_irqrestore(&cohc->lock, flags);
  1132. return 0;
  1133. }
  1134. void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
  1135. struct coh901318_base *base)
  1136. {
  1137. int chans_i;
  1138. int i = 0;
  1139. struct coh901318_chan *cohc;
  1140. INIT_LIST_HEAD(&dma->channels);
  1141. for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
  1142. for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
  1143. cohc = &base->chans[i];
  1144. cohc->base = base;
  1145. cohc->chan.device = dma;
  1146. cohc->id = i;
  1147. /* TODO: do we really need this lock if only one
  1148. * client is connected to each channel?
  1149. */
  1150. spin_lock_init(&cohc->lock);
  1151. cohc->nbr_active_done = 0;
  1152. cohc->busy = 0;
  1153. INIT_LIST_HEAD(&cohc->free);
  1154. INIT_LIST_HEAD(&cohc->active);
  1155. INIT_LIST_HEAD(&cohc->queue);
  1156. tasklet_init(&cohc->tasklet, dma_tasklet,
  1157. (unsigned long) cohc);
  1158. list_add_tail(&cohc->chan.device_node,
  1159. &dma->channels);
  1160. }
  1161. }
  1162. }
  1163. static int __init coh901318_probe(struct platform_device *pdev)
  1164. {
  1165. int err = 0;
  1166. struct coh901318_platform *pdata;
  1167. struct coh901318_base *base;
  1168. int irq;
  1169. struct resource *io;
  1170. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1171. if (!io)
  1172. return -ENODEV;
  1173. /* Map DMA controller registers to virtual memory */
  1174. if (devm_request_mem_region(&pdev->dev,
  1175. io->start,
  1176. resource_size(io),
  1177. pdev->dev.driver->name) == NULL)
  1178. return -ENOMEM;
  1179. pdata = pdev->dev.platform_data;
  1180. if (!pdata)
  1181. return -ENODEV;
  1182. base = devm_kzalloc(&pdev->dev,
  1183. ALIGN(sizeof(struct coh901318_base), 4) +
  1184. pdata->max_channels *
  1185. sizeof(struct coh901318_chan),
  1186. GFP_KERNEL);
  1187. if (!base)
  1188. return -ENOMEM;
  1189. base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
  1190. base->virtbase = devm_ioremap(&pdev->dev, io->start, resource_size(io));
  1191. if (!base->virtbase)
  1192. return -ENOMEM;
  1193. base->dev = &pdev->dev;
  1194. base->platform = pdata;
  1195. spin_lock_init(&base->pm.lock);
  1196. base->pm.started_channels = 0;
  1197. COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
  1198. irq = platform_get_irq(pdev, 0);
  1199. if (irq < 0)
  1200. return irq;
  1201. err = devm_request_irq(&pdev->dev, irq, dma_irq_handler, IRQF_DISABLED,
  1202. "coh901318", base);
  1203. if (err)
  1204. return err;
  1205. err = coh901318_pool_create(&base->pool, &pdev->dev,
  1206. sizeof(struct coh901318_lli),
  1207. 32);
  1208. if (err)
  1209. return err;
  1210. /* init channels for device transfers */
  1211. coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
  1212. base);
  1213. dma_cap_zero(base->dma_slave.cap_mask);
  1214. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  1215. base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  1216. base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
  1217. base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
  1218. base->dma_slave.device_tx_status = coh901318_tx_status;
  1219. base->dma_slave.device_issue_pending = coh901318_issue_pending;
  1220. base->dma_slave.device_control = coh901318_control;
  1221. base->dma_slave.dev = &pdev->dev;
  1222. err = dma_async_device_register(&base->dma_slave);
  1223. if (err)
  1224. goto err_register_slave;
  1225. /* init channels for memcpy */
  1226. coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
  1227. base);
  1228. dma_cap_zero(base->dma_memcpy.cap_mask);
  1229. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  1230. base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  1231. base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
  1232. base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
  1233. base->dma_memcpy.device_tx_status = coh901318_tx_status;
  1234. base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
  1235. base->dma_memcpy.device_control = coh901318_control;
  1236. base->dma_memcpy.dev = &pdev->dev;
  1237. /*
  1238. * This controller can only access address at even 32bit boundaries,
  1239. * i.e. 2^2
  1240. */
  1241. base->dma_memcpy.copy_align = 2;
  1242. err = dma_async_device_register(&base->dma_memcpy);
  1243. if (err)
  1244. goto err_register_memcpy;
  1245. platform_set_drvdata(pdev, base);
  1246. dev_info(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
  1247. (u32) base->virtbase);
  1248. return err;
  1249. err_register_memcpy:
  1250. dma_async_device_unregister(&base->dma_slave);
  1251. err_register_slave:
  1252. coh901318_pool_destroy(&base->pool);
  1253. return err;
  1254. }
  1255. static int __exit coh901318_remove(struct platform_device *pdev)
  1256. {
  1257. struct coh901318_base *base = platform_get_drvdata(pdev);
  1258. dma_async_device_unregister(&base->dma_memcpy);
  1259. dma_async_device_unregister(&base->dma_slave);
  1260. coh901318_pool_destroy(&base->pool);
  1261. return 0;
  1262. }
  1263. static struct platform_driver coh901318_driver = {
  1264. .remove = __exit_p(coh901318_remove),
  1265. .driver = {
  1266. .name = "coh901318",
  1267. },
  1268. };
  1269. int __init coh901318_init(void)
  1270. {
  1271. return platform_driver_probe(&coh901318_driver, coh901318_probe);
  1272. }
  1273. subsys_initcall(coh901318_init);
  1274. void __exit coh901318_exit(void)
  1275. {
  1276. platform_driver_unregister(&coh901318_driver);
  1277. }
  1278. module_exit(coh901318_exit);
  1279. MODULE_LICENSE("GPL");
  1280. MODULE_AUTHOR("Per Friden");