coh901318.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 *data;
  36. enum dma_data_direction dir;
  37. int pending_irqs;
  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. int pending_irqs;
  65. struct coh901318_base *base;
  66. };
  67. static void coh901318_list_print(struct coh901318_chan *cohc,
  68. struct coh901318_lli *lli)
  69. {
  70. struct coh901318_lli *l;
  71. dma_addr_t addr = virt_to_phys(lli);
  72. int i = 0;
  73. while (addr) {
  74. l = phys_to_virt(addr);
  75. dev_vdbg(COHC_2_DEV(cohc), "i %d, lli %p, ctrl 0x%x, src 0x%x"
  76. ", dst 0x%x, link 0x%x link_virt 0x%p\n",
  77. i, l, l->control, l->src_addr, l->dst_addr,
  78. l->link_addr, phys_to_virt(l->link_addr));
  79. i++;
  80. addr = l->link_addr;
  81. }
  82. }
  83. #ifdef CONFIG_DEBUG_FS
  84. #define COH901318_DEBUGFS_ASSIGN(x, y) (x = y)
  85. static struct coh901318_base *debugfs_dma_base;
  86. static struct dentry *dma_dentry;
  87. static int coh901318_debugfs_open(struct inode *inode, struct file *file)
  88. {
  89. file->private_data = inode->i_private;
  90. return 0;
  91. }
  92. static int coh901318_debugfs_read(struct file *file, char __user *buf,
  93. size_t count, loff_t *f_pos)
  94. {
  95. u64 started_channels = debugfs_dma_base->pm.started_channels;
  96. int pool_count = debugfs_dma_base->pool.debugfs_pool_counter;
  97. int i;
  98. int ret = 0;
  99. char *dev_buf;
  100. char *tmp;
  101. int dev_size;
  102. dev_buf = kmalloc(4*1024, GFP_KERNEL);
  103. if (dev_buf == NULL)
  104. goto err_kmalloc;
  105. tmp = dev_buf;
  106. tmp += sprintf(tmp, "DMA -- enable dma channels\n");
  107. for (i = 0; i < debugfs_dma_base->platform->max_channels; i++)
  108. if (started_channels & (1 << i))
  109. tmp += sprintf(tmp, "channel %d\n", i);
  110. tmp += sprintf(tmp, "Pool alloc nbr %d\n", pool_count);
  111. dev_size = tmp - dev_buf;
  112. /* No more to read if offset != 0 */
  113. if (*f_pos > dev_size)
  114. goto out;
  115. if (count > dev_size - *f_pos)
  116. count = dev_size - *f_pos;
  117. if (copy_to_user(buf, dev_buf + *f_pos, count))
  118. ret = -EINVAL;
  119. ret = count;
  120. *f_pos += count;
  121. out:
  122. kfree(dev_buf);
  123. return ret;
  124. err_kmalloc:
  125. return 0;
  126. }
  127. static const struct file_operations coh901318_debugfs_status_operations = {
  128. .owner = THIS_MODULE,
  129. .open = coh901318_debugfs_open,
  130. .read = coh901318_debugfs_read,
  131. };
  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. return cohc->base->platform->chan_conf[cohc->id].dev_addr;
  158. }
  159. static inline const struct coh901318_params *
  160. cohc_chan_param(struct coh901318_chan *cohc)
  161. {
  162. return &cohc->base->platform->chan_conf[cohc->id].param;
  163. }
  164. static inline const struct coh_dma_channel *
  165. cohc_chan_conf(struct coh901318_chan *cohc)
  166. {
  167. return &cohc->base->platform->chan_conf[cohc->id];
  168. }
  169. static void enable_powersave(struct coh901318_chan *cohc)
  170. {
  171. unsigned long flags;
  172. struct powersave *pm = &cohc->base->pm;
  173. spin_lock_irqsave(&pm->lock, flags);
  174. pm->started_channels &= ~(1ULL << cohc->id);
  175. if (!pm->started_channels) {
  176. /* DMA no longer intends to access memory */
  177. cohc->base->platform->access_memory_state(cohc->base->dev,
  178. false);
  179. }
  180. spin_unlock_irqrestore(&pm->lock, flags);
  181. }
  182. static void disable_powersave(struct coh901318_chan *cohc)
  183. {
  184. unsigned long flags;
  185. struct powersave *pm = &cohc->base->pm;
  186. spin_lock_irqsave(&pm->lock, flags);
  187. if (!pm->started_channels) {
  188. /* DMA intends to access memory */
  189. cohc->base->platform->access_memory_state(cohc->base->dev,
  190. true);
  191. }
  192. pm->started_channels |= (1ULL << cohc->id);
  193. spin_unlock_irqrestore(&pm->lock, flags);
  194. }
  195. static inline int coh901318_set_ctrl(struct coh901318_chan *cohc, u32 control)
  196. {
  197. int channel = cohc->id;
  198. void __iomem *virtbase = cohc->base->virtbase;
  199. writel(control,
  200. virtbase + COH901318_CX_CTRL +
  201. COH901318_CX_CTRL_SPACING * channel);
  202. return 0;
  203. }
  204. static inline int coh901318_set_conf(struct coh901318_chan *cohc, u32 conf)
  205. {
  206. int channel = cohc->id;
  207. void __iomem *virtbase = cohc->base->virtbase;
  208. writel(conf,
  209. virtbase + COH901318_CX_CFG +
  210. COH901318_CX_CFG_SPACING*channel);
  211. return 0;
  212. }
  213. static int coh901318_start(struct coh901318_chan *cohc)
  214. {
  215. u32 val;
  216. int channel = cohc->id;
  217. void __iomem *virtbase = cohc->base->virtbase;
  218. disable_powersave(cohc);
  219. val = readl(virtbase + COH901318_CX_CFG +
  220. COH901318_CX_CFG_SPACING * channel);
  221. /* Enable channel */
  222. val |= COH901318_CX_CFG_CH_ENABLE;
  223. writel(val, virtbase + COH901318_CX_CFG +
  224. COH901318_CX_CFG_SPACING * channel);
  225. return 0;
  226. }
  227. static int coh901318_prep_linked_list(struct coh901318_chan *cohc,
  228. struct coh901318_lli *data)
  229. {
  230. int channel = cohc->id;
  231. void __iomem *virtbase = cohc->base->virtbase;
  232. BUG_ON(readl(virtbase + COH901318_CX_STAT +
  233. COH901318_CX_STAT_SPACING*channel) &
  234. COH901318_CX_STAT_ACTIVE);
  235. writel(data->src_addr,
  236. virtbase + COH901318_CX_SRC_ADDR +
  237. COH901318_CX_SRC_ADDR_SPACING * channel);
  238. writel(data->dst_addr, virtbase +
  239. COH901318_CX_DST_ADDR +
  240. COH901318_CX_DST_ADDR_SPACING * channel);
  241. writel(data->link_addr, virtbase + COH901318_CX_LNK_ADDR +
  242. COH901318_CX_LNK_ADDR_SPACING * channel);
  243. writel(data->control, virtbase + COH901318_CX_CTRL +
  244. COH901318_CX_CTRL_SPACING * channel);
  245. return 0;
  246. }
  247. static dma_cookie_t
  248. coh901318_assign_cookie(struct coh901318_chan *cohc,
  249. struct coh901318_desc *cohd)
  250. {
  251. dma_cookie_t cookie = cohc->chan.cookie;
  252. if (++cookie < 0)
  253. cookie = 1;
  254. cohc->chan.cookie = cookie;
  255. cohd->desc.cookie = cookie;
  256. return cookie;
  257. }
  258. static struct coh901318_desc *
  259. coh901318_desc_get(struct coh901318_chan *cohc)
  260. {
  261. struct coh901318_desc *desc;
  262. if (list_empty(&cohc->free)) {
  263. /* alloc new desc because we're out of used ones
  264. * TODO: alloc a pile of descs instead of just one,
  265. * avoid many small allocations.
  266. */
  267. desc = kmalloc(sizeof(struct coh901318_desc), GFP_NOWAIT);
  268. if (desc == NULL)
  269. goto out;
  270. INIT_LIST_HEAD(&desc->node);
  271. } else {
  272. /* Reuse an old desc. */
  273. desc = list_first_entry(&cohc->free,
  274. struct coh901318_desc,
  275. node);
  276. list_del(&desc->node);
  277. }
  278. out:
  279. return desc;
  280. }
  281. static void
  282. coh901318_desc_free(struct coh901318_chan *cohc, struct coh901318_desc *cohd)
  283. {
  284. list_add_tail(&cohd->node, &cohc->free);
  285. }
  286. /* call with irq lock held */
  287. static void
  288. coh901318_desc_submit(struct coh901318_chan *cohc, struct coh901318_desc *desc)
  289. {
  290. list_add_tail(&desc->node, &cohc->active);
  291. BUG_ON(cohc->pending_irqs != 0);
  292. cohc->pending_irqs = desc->pending_irqs;
  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. /*
  327. * DMA start/stop controls
  328. */
  329. u32 coh901318_get_bytes_left(struct dma_chan *chan)
  330. {
  331. unsigned long flags;
  332. u32 ret;
  333. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  334. spin_lock_irqsave(&cohc->lock, flags);
  335. /* Read transfer count value */
  336. ret = readl(cohc->base->virtbase +
  337. COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
  338. cohc->id) & COH901318_CX_CTRL_TC_VALUE_MASK;
  339. spin_unlock_irqrestore(&cohc->lock, flags);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL(coh901318_get_bytes_left);
  343. /* Stops a transfer without losing data. Enables power save.
  344. Use this function in conjunction with coh901318_continue(..)
  345. */
  346. void coh901318_stop(struct dma_chan *chan)
  347. {
  348. u32 val;
  349. unsigned long flags;
  350. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  351. int channel = cohc->id;
  352. void __iomem *virtbase = cohc->base->virtbase;
  353. spin_lock_irqsave(&cohc->lock, flags);
  354. /* Disable channel in HW */
  355. val = readl(virtbase + COH901318_CX_CFG +
  356. COH901318_CX_CFG_SPACING * channel);
  357. /* Stopping infinit transfer */
  358. if ((val & COH901318_CX_CTRL_TC_ENABLE) == 0 &&
  359. (val & COH901318_CX_CFG_CH_ENABLE))
  360. cohc->stopped = 1;
  361. val &= ~COH901318_CX_CFG_CH_ENABLE;
  362. /* Enable twice, HW bug work around */
  363. writel(val, virtbase + COH901318_CX_CFG +
  364. COH901318_CX_CFG_SPACING * channel);
  365. writel(val, virtbase + COH901318_CX_CFG +
  366. COH901318_CX_CFG_SPACING * channel);
  367. /* Spin-wait for it to actually go inactive */
  368. while (readl(virtbase + COH901318_CX_STAT+COH901318_CX_STAT_SPACING *
  369. channel) & COH901318_CX_STAT_ACTIVE)
  370. cpu_relax();
  371. /* Check if we stopped an active job */
  372. if ((readl(virtbase + COH901318_CX_CTRL+COH901318_CX_CTRL_SPACING *
  373. channel) & COH901318_CX_CTRL_TC_VALUE_MASK) > 0)
  374. cohc->stopped = 1;
  375. enable_powersave(cohc);
  376. spin_unlock_irqrestore(&cohc->lock, flags);
  377. }
  378. EXPORT_SYMBOL(coh901318_stop);
  379. /* Continues a transfer that has been stopped via 300_dma_stop(..).
  380. Power save is handled.
  381. */
  382. void coh901318_continue(struct dma_chan *chan)
  383. {
  384. u32 val;
  385. unsigned long flags;
  386. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  387. int channel = cohc->id;
  388. spin_lock_irqsave(&cohc->lock, flags);
  389. disable_powersave(cohc);
  390. if (cohc->stopped) {
  391. /* Enable channel in HW */
  392. val = readl(cohc->base->virtbase + COH901318_CX_CFG +
  393. COH901318_CX_CFG_SPACING * channel);
  394. val |= COH901318_CX_CFG_CH_ENABLE;
  395. writel(val, cohc->base->virtbase + COH901318_CX_CFG +
  396. COH901318_CX_CFG_SPACING*channel);
  397. cohc->stopped = 0;
  398. }
  399. spin_unlock_irqrestore(&cohc->lock, flags);
  400. }
  401. EXPORT_SYMBOL(coh901318_continue);
  402. bool coh901318_filter_id(struct dma_chan *chan, void *chan_id)
  403. {
  404. unsigned int ch_nr = (unsigned int) chan_id;
  405. if (ch_nr == to_coh901318_chan(chan)->id)
  406. return true;
  407. return false;
  408. }
  409. EXPORT_SYMBOL(coh901318_filter_id);
  410. /*
  411. * DMA channel allocation
  412. */
  413. static int coh901318_config(struct coh901318_chan *cohc,
  414. struct coh901318_params *param)
  415. {
  416. unsigned long flags;
  417. const struct coh901318_params *p;
  418. int channel = cohc->id;
  419. void __iomem *virtbase = cohc->base->virtbase;
  420. spin_lock_irqsave(&cohc->lock, flags);
  421. if (param)
  422. p = param;
  423. else
  424. p = &cohc->base->platform->chan_conf[channel].param;
  425. /* Clear any pending BE or TC interrupt */
  426. if (channel < 32) {
  427. writel(1 << channel, virtbase + COH901318_BE_INT_CLEAR1);
  428. writel(1 << channel, virtbase + COH901318_TC_INT_CLEAR1);
  429. } else {
  430. writel(1 << (channel - 32), virtbase +
  431. COH901318_BE_INT_CLEAR2);
  432. writel(1 << (channel - 32), virtbase +
  433. COH901318_TC_INT_CLEAR2);
  434. }
  435. coh901318_set_conf(cohc, p->config);
  436. coh901318_set_ctrl(cohc, p->ctrl_lli_last);
  437. spin_unlock_irqrestore(&cohc->lock, flags);
  438. return 0;
  439. }
  440. /* must lock when calling this function
  441. * start queued jobs, if any
  442. * TODO: start all queued jobs in one go
  443. *
  444. * Returns descriptor if queued job is started otherwise NULL.
  445. * If the queue is empty NULL is returned.
  446. */
  447. static struct coh901318_desc *coh901318_queue_start(struct coh901318_chan *cohc)
  448. {
  449. struct coh901318_desc *cohd_que;
  450. /* start queued jobs, if any
  451. * TODO: transmit all queued jobs in one go
  452. */
  453. cohd_que = coh901318_first_queued(cohc);
  454. if (cohd_que != NULL) {
  455. /* Remove from queue */
  456. coh901318_desc_remove(cohd_que);
  457. /* initiate DMA job */
  458. cohc->busy = 1;
  459. coh901318_desc_submit(cohc, cohd_que);
  460. coh901318_prep_linked_list(cohc, cohd_que->data);
  461. /* start dma job */
  462. coh901318_start(cohc);
  463. }
  464. return cohd_que;
  465. }
  466. static void dma_tasklet(unsigned long data)
  467. {
  468. struct coh901318_chan *cohc = (struct coh901318_chan *) data;
  469. struct coh901318_desc *cohd_fin;
  470. unsigned long flags;
  471. dma_async_tx_callback callback;
  472. void *callback_param;
  473. spin_lock_irqsave(&cohc->lock, flags);
  474. /* get first active entry from list */
  475. cohd_fin = coh901318_first_active_get(cohc);
  476. BUG_ON(cohd_fin->pending_irqs == 0);
  477. if (cohd_fin == NULL)
  478. goto err;
  479. cohd_fin->pending_irqs--;
  480. cohc->completed = cohd_fin->desc.cookie;
  481. if (cohc->nbr_active_done == 0)
  482. return;
  483. if (!cohd_fin->pending_irqs) {
  484. /* release the lli allocation*/
  485. coh901318_lli_free(&cohc->base->pool, &cohd_fin->data);
  486. }
  487. dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d pending_irqs %d"
  488. " nbr_active_done %ld\n", __func__,
  489. cohc->id, cohc->pending_irqs, cohc->nbr_active_done);
  490. /* callback to client */
  491. callback = cohd_fin->desc.callback;
  492. callback_param = cohd_fin->desc.callback_param;
  493. if (!cohd_fin->pending_irqs) {
  494. coh901318_desc_remove(cohd_fin);
  495. /* return desc to free-list */
  496. coh901318_desc_free(cohc, cohd_fin);
  497. }
  498. if (cohc->nbr_active_done)
  499. cohc->nbr_active_done--;
  500. if (cohc->nbr_active_done) {
  501. if (cohc_chan_conf(cohc)->priority_high)
  502. tasklet_hi_schedule(&cohc->tasklet);
  503. else
  504. tasklet_schedule(&cohc->tasklet);
  505. }
  506. spin_unlock_irqrestore(&cohc->lock, flags);
  507. if (callback)
  508. callback(callback_param);
  509. return;
  510. err:
  511. spin_unlock_irqrestore(&cohc->lock, flags);
  512. dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
  513. }
  514. /* called from interrupt context */
  515. static void dma_tc_handle(struct coh901318_chan *cohc)
  516. {
  517. BUG_ON(!cohc->allocated && (list_empty(&cohc->active) ||
  518. list_empty(&cohc->queue)));
  519. if (!cohc->allocated)
  520. return;
  521. BUG_ON(cohc->pending_irqs == 0);
  522. cohc->pending_irqs--;
  523. cohc->nbr_active_done++;
  524. if (cohc->pending_irqs == 0 && coh901318_queue_start(cohc) == NULL)
  525. cohc->busy = 0;
  526. BUG_ON(list_empty(&cohc->active));
  527. if (cohc_chan_conf(cohc)->priority_high)
  528. tasklet_hi_schedule(&cohc->tasklet);
  529. else
  530. tasklet_schedule(&cohc->tasklet);
  531. }
  532. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  533. {
  534. u32 status1;
  535. u32 status2;
  536. int i;
  537. int ch;
  538. struct coh901318_base *base = dev_id;
  539. struct coh901318_chan *cohc;
  540. void __iomem *virtbase = base->virtbase;
  541. status1 = readl(virtbase + COH901318_INT_STATUS1);
  542. status2 = readl(virtbase + COH901318_INT_STATUS2);
  543. if (unlikely(status1 == 0 && status2 == 0)) {
  544. dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
  545. return IRQ_HANDLED;
  546. }
  547. /* TODO: consider handle IRQ in tasklet here to
  548. * minimize interrupt latency */
  549. /* Check the first 32 DMA channels for IRQ */
  550. while (status1) {
  551. /* Find first bit set, return as a number. */
  552. i = ffs(status1) - 1;
  553. ch = i;
  554. cohc = &base->chans[ch];
  555. spin_lock(&cohc->lock);
  556. /* Mask off this bit */
  557. status1 &= ~(1 << i);
  558. /* Check the individual channel bits */
  559. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
  560. dev_crit(COHC_2_DEV(cohc),
  561. "DMA bus error on channel %d!\n", ch);
  562. BUG_ON(1);
  563. /* Clear BE interrupt */
  564. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
  565. } else {
  566. /* Caused by TC, really? */
  567. if (unlikely(!test_bit(i, virtbase +
  568. COH901318_TC_INT_STATUS1))) {
  569. dev_warn(COHC_2_DEV(cohc),
  570. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  571. /* Clear TC interrupt */
  572. BUG_ON(1);
  573. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  574. } else {
  575. /* Enable powersave if transfer has finished */
  576. if (!(readl(virtbase + COH901318_CX_STAT +
  577. COH901318_CX_STAT_SPACING*ch) &
  578. COH901318_CX_STAT_ENABLED)) {
  579. enable_powersave(cohc);
  580. }
  581. /* Must clear TC interrupt before calling
  582. * dma_tc_handle
  583. * in case tc_handle initate a new dma job
  584. */
  585. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  586. dma_tc_handle(cohc);
  587. }
  588. }
  589. spin_unlock(&cohc->lock);
  590. }
  591. /* Check the remaining 32 DMA channels for IRQ */
  592. while (status2) {
  593. /* Find first bit set, return as a number. */
  594. i = ffs(status2) - 1;
  595. ch = i + 32;
  596. cohc = &base->chans[ch];
  597. spin_lock(&cohc->lock);
  598. /* Mask off this bit */
  599. status2 &= ~(1 << i);
  600. /* Check the individual channel bits */
  601. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
  602. dev_crit(COHC_2_DEV(cohc),
  603. "DMA bus error on channel %d!\n", ch);
  604. /* Clear BE interrupt */
  605. BUG_ON(1);
  606. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
  607. } else {
  608. /* Caused by TC, really? */
  609. if (unlikely(!test_bit(i, virtbase +
  610. COH901318_TC_INT_STATUS2))) {
  611. dev_warn(COHC_2_DEV(cohc),
  612. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  613. /* Clear TC interrupt */
  614. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  615. BUG_ON(1);
  616. } else {
  617. /* Enable powersave if transfer has finished */
  618. if (!(readl(virtbase + COH901318_CX_STAT +
  619. COH901318_CX_STAT_SPACING*ch) &
  620. COH901318_CX_STAT_ENABLED)) {
  621. enable_powersave(cohc);
  622. }
  623. /* Must clear TC interrupt before calling
  624. * dma_tc_handle
  625. * in case tc_handle initate a new dma job
  626. */
  627. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  628. dma_tc_handle(cohc);
  629. }
  630. }
  631. spin_unlock(&cohc->lock);
  632. }
  633. return IRQ_HANDLED;
  634. }
  635. static int coh901318_alloc_chan_resources(struct dma_chan *chan)
  636. {
  637. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  638. dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
  639. __func__, cohc->id);
  640. if (chan->client_count > 1)
  641. return -EBUSY;
  642. coh901318_config(cohc, NULL);
  643. cohc->allocated = 1;
  644. cohc->completed = chan->cookie = 1;
  645. return 1;
  646. }
  647. static void
  648. coh901318_free_chan_resources(struct dma_chan *chan)
  649. {
  650. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  651. int channel = cohc->id;
  652. unsigned long flags;
  653. spin_lock_irqsave(&cohc->lock, flags);
  654. /* Disable HW */
  655. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
  656. COH901318_CX_CFG_SPACING*channel);
  657. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
  658. COH901318_CX_CTRL_SPACING*channel);
  659. cohc->allocated = 0;
  660. spin_unlock_irqrestore(&cohc->lock, flags);
  661. chan->device->device_terminate_all(chan);
  662. }
  663. static dma_cookie_t
  664. coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
  665. {
  666. struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
  667. desc);
  668. struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
  669. unsigned long flags;
  670. spin_lock_irqsave(&cohc->lock, flags);
  671. tx->cookie = coh901318_assign_cookie(cohc, cohd);
  672. coh901318_desc_queue(cohc, cohd);
  673. spin_unlock_irqrestore(&cohc->lock, flags);
  674. return tx->cookie;
  675. }
  676. static struct dma_async_tx_descriptor *
  677. coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  678. size_t size, unsigned long flags)
  679. {
  680. struct coh901318_lli *data;
  681. struct coh901318_desc *cohd;
  682. unsigned long flg;
  683. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  684. int lli_len;
  685. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  686. spin_lock_irqsave(&cohc->lock, flg);
  687. dev_vdbg(COHC_2_DEV(cohc),
  688. "[%s] channel %d src 0x%x dest 0x%x size %d\n",
  689. __func__, cohc->id, src, dest, size);
  690. if (flags & DMA_PREP_INTERRUPT)
  691. /* Trigger interrupt after last lli */
  692. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  693. lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  694. if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  695. lli_len++;
  696. data = coh901318_lli_alloc(&cohc->base->pool, lli_len);
  697. if (data == NULL)
  698. goto err;
  699. cohd = coh901318_desc_get(cohc);
  700. cohd->sg = NULL;
  701. cohd->sg_len = 0;
  702. cohd->data = data;
  703. cohd->pending_irqs =
  704. coh901318_lli_fill_memcpy(
  705. &cohc->base->pool, data, src, size, dest,
  706. cohc_chan_param(cohc)->ctrl_lli_chained,
  707. ctrl_last);
  708. cohd->flags = flags;
  709. COH_DBG(coh901318_list_print(cohc, data));
  710. dma_async_tx_descriptor_init(&cohd->desc, chan);
  711. cohd->desc.tx_submit = coh901318_tx_submit;
  712. spin_unlock_irqrestore(&cohc->lock, flg);
  713. return &cohd->desc;
  714. err:
  715. spin_unlock_irqrestore(&cohc->lock, flg);
  716. return NULL;
  717. }
  718. static struct dma_async_tx_descriptor *
  719. coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  720. unsigned int sg_len, enum dma_data_direction direction,
  721. unsigned long flags)
  722. {
  723. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  724. struct coh901318_lli *data;
  725. struct coh901318_desc *cohd;
  726. struct scatterlist *sg;
  727. int len = 0;
  728. int size;
  729. int i;
  730. u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
  731. u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
  732. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  733. unsigned long flg;
  734. if (!sgl)
  735. goto out;
  736. if (sgl->length == 0)
  737. goto out;
  738. spin_lock_irqsave(&cohc->lock, flg);
  739. dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
  740. __func__, sg_len, direction);
  741. if (flags & DMA_PREP_INTERRUPT)
  742. /* Trigger interrupt after last lli */
  743. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  744. cohd = coh901318_desc_get(cohc);
  745. cohd->sg = NULL;
  746. cohd->sg_len = 0;
  747. cohd->dir = direction;
  748. if (direction == DMA_TO_DEVICE) {
  749. u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
  750. COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
  751. ctrl_chained |= tx_flags;
  752. ctrl_last |= tx_flags;
  753. ctrl |= tx_flags;
  754. } else if (direction == DMA_FROM_DEVICE) {
  755. u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
  756. COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
  757. ctrl_chained |= rx_flags;
  758. ctrl_last |= rx_flags;
  759. ctrl |= rx_flags;
  760. } else
  761. goto err_direction;
  762. dma_async_tx_descriptor_init(&cohd->desc, chan);
  763. cohd->desc.tx_submit = coh901318_tx_submit;
  764. /* The dma only supports transmitting packages up to
  765. * MAX_DMA_PACKET_SIZE. Calculate to total number of
  766. * dma elemts required to send the entire sg list
  767. */
  768. for_each_sg(sgl, sg, sg_len, i) {
  769. unsigned int factor;
  770. size = sg_dma_len(sg);
  771. if (size <= MAX_DMA_PACKET_SIZE) {
  772. len++;
  773. continue;
  774. }
  775. factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  776. if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  777. factor++;
  778. len += factor;
  779. }
  780. data = coh901318_lli_alloc(&cohc->base->pool, len);
  781. if (data == NULL)
  782. goto err_dma_alloc;
  783. /* initiate allocated data list */
  784. cohd->pending_irqs =
  785. coh901318_lli_fill_sg(&cohc->base->pool, data, sgl, sg_len,
  786. cohc_dev_addr(cohc),
  787. ctrl_chained,
  788. ctrl,
  789. ctrl_last,
  790. direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
  791. cohd->data = data;
  792. cohd->flags = flags;
  793. COH_DBG(coh901318_list_print(cohc, data));
  794. spin_unlock_irqrestore(&cohc->lock, flg);
  795. return &cohd->desc;
  796. err_dma_alloc:
  797. err_direction:
  798. coh901318_desc_remove(cohd);
  799. coh901318_desc_free(cohc, cohd);
  800. spin_unlock_irqrestore(&cohc->lock, flg);
  801. out:
  802. return NULL;
  803. }
  804. static enum dma_status
  805. coh901318_is_tx_complete(struct dma_chan *chan,
  806. dma_cookie_t cookie, dma_cookie_t *done,
  807. dma_cookie_t *used)
  808. {
  809. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  810. dma_cookie_t last_used;
  811. dma_cookie_t last_complete;
  812. int ret;
  813. last_complete = cohc->completed;
  814. last_used = chan->cookie;
  815. ret = dma_async_is_complete(cookie, last_complete, last_used);
  816. if (done)
  817. *done = last_complete;
  818. if (used)
  819. *used = last_used;
  820. return ret;
  821. }
  822. static void
  823. coh901318_issue_pending(struct dma_chan *chan)
  824. {
  825. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  826. unsigned long flags;
  827. spin_lock_irqsave(&cohc->lock, flags);
  828. /* Busy means that pending jobs are already being processed */
  829. if (!cohc->busy)
  830. coh901318_queue_start(cohc);
  831. spin_unlock_irqrestore(&cohc->lock, flags);
  832. }
  833. static void
  834. coh901318_terminate_all(struct dma_chan *chan)
  835. {
  836. unsigned long flags;
  837. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  838. struct coh901318_desc *cohd;
  839. void __iomem *virtbase = cohc->base->virtbase;
  840. coh901318_stop(chan);
  841. spin_lock_irqsave(&cohc->lock, flags);
  842. /* Clear any pending BE or TC interrupt */
  843. if (cohc->id < 32) {
  844. writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
  845. writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
  846. } else {
  847. writel(1 << (cohc->id - 32), virtbase +
  848. COH901318_BE_INT_CLEAR2);
  849. writel(1 << (cohc->id - 32), virtbase +
  850. COH901318_TC_INT_CLEAR2);
  851. }
  852. enable_powersave(cohc);
  853. while ((cohd = coh901318_first_active_get(cohc))) {
  854. /* release the lli allocation*/
  855. coh901318_lli_free(&cohc->base->pool, &cohd->data);
  856. coh901318_desc_remove(cohd);
  857. /* return desc to free-list */
  858. coh901318_desc_free(cohc, cohd);
  859. }
  860. while ((cohd = coh901318_first_queued(cohc))) {
  861. /* release the lli allocation*/
  862. coh901318_lli_free(&cohc->base->pool, &cohd->data);
  863. coh901318_desc_remove(cohd);
  864. /* return desc to free-list */
  865. coh901318_desc_free(cohc, cohd);
  866. }
  867. cohc->nbr_active_done = 0;
  868. cohc->busy = 0;
  869. cohc->pending_irqs = 0;
  870. spin_unlock_irqrestore(&cohc->lock, flags);
  871. }
  872. void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
  873. struct coh901318_base *base)
  874. {
  875. int chans_i;
  876. int i = 0;
  877. struct coh901318_chan *cohc;
  878. INIT_LIST_HEAD(&dma->channels);
  879. for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
  880. for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
  881. cohc = &base->chans[i];
  882. cohc->base = base;
  883. cohc->chan.device = dma;
  884. cohc->id = i;
  885. /* TODO: do we really need this lock if only one
  886. * client is connected to each channel?
  887. */
  888. spin_lock_init(&cohc->lock);
  889. cohc->pending_irqs = 0;
  890. cohc->nbr_active_done = 0;
  891. cohc->busy = 0;
  892. INIT_LIST_HEAD(&cohc->free);
  893. INIT_LIST_HEAD(&cohc->active);
  894. INIT_LIST_HEAD(&cohc->queue);
  895. tasklet_init(&cohc->tasklet, dma_tasklet,
  896. (unsigned long) cohc);
  897. list_add_tail(&cohc->chan.device_node,
  898. &dma->channels);
  899. }
  900. }
  901. }
  902. static int __init coh901318_probe(struct platform_device *pdev)
  903. {
  904. int err = 0;
  905. struct coh901318_platform *pdata;
  906. struct coh901318_base *base;
  907. int irq;
  908. struct resource *io;
  909. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  910. if (!io)
  911. goto err_get_resource;
  912. /* Map DMA controller registers to virtual memory */
  913. if (request_mem_region(io->start,
  914. resource_size(io),
  915. pdev->dev.driver->name) == NULL) {
  916. err = -EBUSY;
  917. goto err_request_mem;
  918. }
  919. pdata = pdev->dev.platform_data;
  920. if (!pdata)
  921. goto err_no_platformdata;
  922. base = kmalloc(ALIGN(sizeof(struct coh901318_base), 4) +
  923. pdata->max_channels *
  924. sizeof(struct coh901318_chan),
  925. GFP_KERNEL);
  926. if (!base)
  927. goto err_alloc_coh_dma_channels;
  928. base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
  929. base->virtbase = ioremap(io->start, resource_size(io));
  930. if (!base->virtbase) {
  931. err = -ENOMEM;
  932. goto err_no_ioremap;
  933. }
  934. base->dev = &pdev->dev;
  935. base->platform = pdata;
  936. spin_lock_init(&base->pm.lock);
  937. base->pm.started_channels = 0;
  938. COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
  939. platform_set_drvdata(pdev, base);
  940. irq = platform_get_irq(pdev, 0);
  941. if (irq < 0)
  942. goto err_no_irq;
  943. err = request_irq(irq, dma_irq_handler, IRQF_DISABLED,
  944. "coh901318", base);
  945. if (err) {
  946. dev_crit(&pdev->dev,
  947. "Cannot allocate IRQ for DMA controller!\n");
  948. goto err_request_irq;
  949. }
  950. err = coh901318_pool_create(&base->pool, &pdev->dev,
  951. sizeof(struct coh901318_lli),
  952. 32);
  953. if (err)
  954. goto err_pool_create;
  955. /* init channels for device transfers */
  956. coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
  957. base);
  958. dma_cap_zero(base->dma_slave.cap_mask);
  959. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  960. base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  961. base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
  962. base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
  963. base->dma_slave.device_is_tx_complete = coh901318_is_tx_complete;
  964. base->dma_slave.device_issue_pending = coh901318_issue_pending;
  965. base->dma_slave.device_terminate_all = coh901318_terminate_all;
  966. base->dma_slave.dev = &pdev->dev;
  967. err = dma_async_device_register(&base->dma_slave);
  968. if (err)
  969. goto err_register_slave;
  970. /* init channels for memcpy */
  971. coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
  972. base);
  973. dma_cap_zero(base->dma_memcpy.cap_mask);
  974. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  975. base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  976. base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
  977. base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
  978. base->dma_memcpy.device_is_tx_complete = coh901318_is_tx_complete;
  979. base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
  980. base->dma_memcpy.device_terminate_all = coh901318_terminate_all;
  981. base->dma_memcpy.dev = &pdev->dev;
  982. err = dma_async_device_register(&base->dma_memcpy);
  983. if (err)
  984. goto err_register_memcpy;
  985. dev_dbg(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
  986. (u32) base->virtbase);
  987. return err;
  988. err_register_memcpy:
  989. dma_async_device_unregister(&base->dma_slave);
  990. err_register_slave:
  991. coh901318_pool_destroy(&base->pool);
  992. err_pool_create:
  993. free_irq(platform_get_irq(pdev, 0), base);
  994. err_request_irq:
  995. err_no_irq:
  996. iounmap(base->virtbase);
  997. err_no_ioremap:
  998. kfree(base);
  999. err_alloc_coh_dma_channels:
  1000. err_no_platformdata:
  1001. release_mem_region(pdev->resource->start,
  1002. resource_size(pdev->resource));
  1003. err_request_mem:
  1004. err_get_resource:
  1005. return err;
  1006. }
  1007. static int __exit coh901318_remove(struct platform_device *pdev)
  1008. {
  1009. struct coh901318_base *base = platform_get_drvdata(pdev);
  1010. dma_async_device_unregister(&base->dma_memcpy);
  1011. dma_async_device_unregister(&base->dma_slave);
  1012. coh901318_pool_destroy(&base->pool);
  1013. free_irq(platform_get_irq(pdev, 0), base);
  1014. iounmap(base->virtbase);
  1015. kfree(base);
  1016. release_mem_region(pdev->resource->start,
  1017. resource_size(pdev->resource));
  1018. return 0;
  1019. }
  1020. static struct platform_driver coh901318_driver = {
  1021. .remove = __exit_p(coh901318_remove),
  1022. .driver = {
  1023. .name = "coh901318",
  1024. },
  1025. };
  1026. int __init coh901318_init(void)
  1027. {
  1028. return platform_driver_probe(&coh901318_driver, coh901318_probe);
  1029. }
  1030. subsys_initcall(coh901318_init);
  1031. void __exit coh901318_exit(void)
  1032. {
  1033. platform_driver_unregister(&coh901318_driver);
  1034. }
  1035. module_exit(coh901318_exit);
  1036. MODULE_LICENSE("GPL");
  1037. MODULE_AUTHOR("Per Friden");