coh901318.c 35 KB

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