coh901318.c 39 KB

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