coh901318.c 39 KB

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