coh901318.c 39 KB

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