coh901318.c 39 KB

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