coh901318.c 39 KB

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