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