coh901318.c 39 KB

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