coh901318.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. BUG_ON(cohc->nbr_active_done && cohd_fin == NULL);
  482. if (cohc->nbr_active_done == 0)
  483. return;
  484. if (!cohd_fin->pending_irqs) {
  485. /* release the lli allocation*/
  486. coh901318_lli_free(&cohc->base->pool, &cohd_fin->data);
  487. }
  488. dev_vdbg(COHC_2_DEV(cohc), "[%s] chan_id %d pending_irqs %d"
  489. " nbr_active_done %ld\n", __func__,
  490. cohc->id, cohc->pending_irqs, cohc->nbr_active_done);
  491. /* callback to client */
  492. callback = cohd_fin->desc.callback;
  493. callback_param = cohd_fin->desc.callback_param;
  494. if (!cohd_fin->pending_irqs) {
  495. coh901318_desc_remove(cohd_fin);
  496. /* return desc to free-list */
  497. coh901318_desc_free(cohc, cohd_fin);
  498. }
  499. if (cohc->nbr_active_done)
  500. cohc->nbr_active_done--;
  501. if (cohc->nbr_active_done) {
  502. if (cohc_chan_conf(cohc)->priority_high)
  503. tasklet_hi_schedule(&cohc->tasklet);
  504. else
  505. tasklet_schedule(&cohc->tasklet);
  506. }
  507. spin_unlock_irqrestore(&cohc->lock, flags);
  508. if (callback)
  509. callback(callback_param);
  510. return;
  511. err:
  512. spin_unlock_irqrestore(&cohc->lock, flags);
  513. dev_err(COHC_2_DEV(cohc), "[%s] No active dma desc\n", __func__);
  514. }
  515. /* called from interrupt context */
  516. static void dma_tc_handle(struct coh901318_chan *cohc)
  517. {
  518. BUG_ON(!cohc->allocated && (list_empty(&cohc->active) ||
  519. list_empty(&cohc->queue)));
  520. if (!cohc->allocated)
  521. return;
  522. BUG_ON(cohc->pending_irqs == 0);
  523. cohc->pending_irqs--;
  524. cohc->nbr_active_done++;
  525. if (cohc->pending_irqs == 0 && coh901318_queue_start(cohc) == NULL)
  526. cohc->busy = 0;
  527. BUG_ON(list_empty(&cohc->active));
  528. if (cohc_chan_conf(cohc)->priority_high)
  529. tasklet_hi_schedule(&cohc->tasklet);
  530. else
  531. tasklet_schedule(&cohc->tasklet);
  532. }
  533. static irqreturn_t dma_irq_handler(int irq, void *dev_id)
  534. {
  535. u32 status1;
  536. u32 status2;
  537. int i;
  538. int ch;
  539. struct coh901318_base *base = dev_id;
  540. struct coh901318_chan *cohc;
  541. void __iomem *virtbase = base->virtbase;
  542. status1 = readl(virtbase + COH901318_INT_STATUS1);
  543. status2 = readl(virtbase + COH901318_INT_STATUS2);
  544. if (unlikely(status1 == 0 && status2 == 0)) {
  545. dev_warn(base->dev, "spurious DMA IRQ from no channel!\n");
  546. return IRQ_HANDLED;
  547. }
  548. /* TODO: consider handle IRQ in tasklet here to
  549. * minimize interrupt latency */
  550. /* Check the first 32 DMA channels for IRQ */
  551. while (status1) {
  552. /* Find first bit set, return as a number. */
  553. i = ffs(status1) - 1;
  554. ch = i;
  555. cohc = &base->chans[ch];
  556. spin_lock(&cohc->lock);
  557. /* Mask off this bit */
  558. status1 &= ~(1 << i);
  559. /* Check the individual channel bits */
  560. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS1)) {
  561. dev_crit(COHC_2_DEV(cohc),
  562. "DMA bus error on channel %d!\n", ch);
  563. BUG_ON(1);
  564. /* Clear BE interrupt */
  565. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR1);
  566. } else {
  567. /* Caused by TC, really? */
  568. if (unlikely(!test_bit(i, virtbase +
  569. COH901318_TC_INT_STATUS1))) {
  570. dev_warn(COHC_2_DEV(cohc),
  571. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  572. /* Clear TC interrupt */
  573. BUG_ON(1);
  574. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  575. } else {
  576. /* Enable powersave if transfer has finished */
  577. if (!(readl(virtbase + COH901318_CX_STAT +
  578. COH901318_CX_STAT_SPACING*ch) &
  579. COH901318_CX_STAT_ENABLED)) {
  580. enable_powersave(cohc);
  581. }
  582. /* Must clear TC interrupt before calling
  583. * dma_tc_handle
  584. * in case tc_handle initate a new dma job
  585. */
  586. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR1);
  587. dma_tc_handle(cohc);
  588. }
  589. }
  590. spin_unlock(&cohc->lock);
  591. }
  592. /* Check the remaining 32 DMA channels for IRQ */
  593. while (status2) {
  594. /* Find first bit set, return as a number. */
  595. i = ffs(status2) - 1;
  596. ch = i + 32;
  597. cohc = &base->chans[ch];
  598. spin_lock(&cohc->lock);
  599. /* Mask off this bit */
  600. status2 &= ~(1 << i);
  601. /* Check the individual channel bits */
  602. if (test_bit(i, virtbase + COH901318_BE_INT_STATUS2)) {
  603. dev_crit(COHC_2_DEV(cohc),
  604. "DMA bus error on channel %d!\n", ch);
  605. /* Clear BE interrupt */
  606. BUG_ON(1);
  607. __set_bit(i, virtbase + COH901318_BE_INT_CLEAR2);
  608. } else {
  609. /* Caused by TC, really? */
  610. if (unlikely(!test_bit(i, virtbase +
  611. COH901318_TC_INT_STATUS2))) {
  612. dev_warn(COHC_2_DEV(cohc),
  613. "ignoring interrupt not caused by terminal count on channel %d\n", ch);
  614. /* Clear TC interrupt */
  615. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  616. BUG_ON(1);
  617. } else {
  618. /* Enable powersave if transfer has finished */
  619. if (!(readl(virtbase + COH901318_CX_STAT +
  620. COH901318_CX_STAT_SPACING*ch) &
  621. COH901318_CX_STAT_ENABLED)) {
  622. enable_powersave(cohc);
  623. }
  624. /* Must clear TC interrupt before calling
  625. * dma_tc_handle
  626. * in case tc_handle initate a new dma job
  627. */
  628. __set_bit(i, virtbase + COH901318_TC_INT_CLEAR2);
  629. dma_tc_handle(cohc);
  630. }
  631. }
  632. spin_unlock(&cohc->lock);
  633. }
  634. return IRQ_HANDLED;
  635. }
  636. static int coh901318_alloc_chan_resources(struct dma_chan *chan)
  637. {
  638. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  639. dev_vdbg(COHC_2_DEV(cohc), "[%s] DMA channel %d\n",
  640. __func__, cohc->id);
  641. if (chan->client_count > 1)
  642. return -EBUSY;
  643. coh901318_config(cohc, NULL);
  644. cohc->allocated = 1;
  645. cohc->completed = chan->cookie = 1;
  646. return 1;
  647. }
  648. static void
  649. coh901318_free_chan_resources(struct dma_chan *chan)
  650. {
  651. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  652. int channel = cohc->id;
  653. unsigned long flags;
  654. spin_lock_irqsave(&cohc->lock, flags);
  655. /* Disable HW */
  656. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CFG +
  657. COH901318_CX_CFG_SPACING*channel);
  658. writel(0x00000000U, cohc->base->virtbase + COH901318_CX_CTRL +
  659. COH901318_CX_CTRL_SPACING*channel);
  660. cohc->allocated = 0;
  661. spin_unlock_irqrestore(&cohc->lock, flags);
  662. chan->device->device_terminate_all(chan);
  663. }
  664. static dma_cookie_t
  665. coh901318_tx_submit(struct dma_async_tx_descriptor *tx)
  666. {
  667. struct coh901318_desc *cohd = container_of(tx, struct coh901318_desc,
  668. desc);
  669. struct coh901318_chan *cohc = to_coh901318_chan(tx->chan);
  670. unsigned long flags;
  671. spin_lock_irqsave(&cohc->lock, flags);
  672. tx->cookie = coh901318_assign_cookie(cohc, cohd);
  673. coh901318_desc_queue(cohc, cohd);
  674. spin_unlock_irqrestore(&cohc->lock, flags);
  675. return tx->cookie;
  676. }
  677. static struct dma_async_tx_descriptor *
  678. coh901318_prep_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  679. size_t size, unsigned long flags)
  680. {
  681. struct coh901318_lli *data;
  682. struct coh901318_desc *cohd;
  683. unsigned long flg;
  684. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  685. int lli_len;
  686. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  687. spin_lock_irqsave(&cohc->lock, flg);
  688. dev_vdbg(COHC_2_DEV(cohc),
  689. "[%s] channel %d src 0x%x dest 0x%x size %d\n",
  690. __func__, cohc->id, src, dest, size);
  691. if (flags & DMA_PREP_INTERRUPT)
  692. /* Trigger interrupt after last lli */
  693. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  694. lli_len = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  695. if ((lli_len << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  696. lli_len++;
  697. data = coh901318_lli_alloc(&cohc->base->pool, lli_len);
  698. if (data == NULL)
  699. goto err;
  700. cohd = coh901318_desc_get(cohc);
  701. cohd->sg = NULL;
  702. cohd->sg_len = 0;
  703. cohd->data = data;
  704. cohd->pending_irqs =
  705. coh901318_lli_fill_memcpy(
  706. &cohc->base->pool, data, src, size, dest,
  707. cohc_chan_param(cohc)->ctrl_lli_chained,
  708. ctrl_last);
  709. cohd->flags = flags;
  710. COH_DBG(coh901318_list_print(cohc, data));
  711. dma_async_tx_descriptor_init(&cohd->desc, chan);
  712. cohd->desc.tx_submit = coh901318_tx_submit;
  713. spin_unlock_irqrestore(&cohc->lock, flg);
  714. return &cohd->desc;
  715. err:
  716. spin_unlock_irqrestore(&cohc->lock, flg);
  717. return NULL;
  718. }
  719. static struct dma_async_tx_descriptor *
  720. coh901318_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  721. unsigned int sg_len, enum dma_data_direction direction,
  722. unsigned long flags)
  723. {
  724. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  725. struct coh901318_lli *data;
  726. struct coh901318_desc *cohd;
  727. struct scatterlist *sg;
  728. int len = 0;
  729. int size;
  730. int i;
  731. u32 ctrl_chained = cohc_chan_param(cohc)->ctrl_lli_chained;
  732. u32 ctrl = cohc_chan_param(cohc)->ctrl_lli;
  733. u32 ctrl_last = cohc_chan_param(cohc)->ctrl_lli_last;
  734. unsigned long flg;
  735. if (!sgl)
  736. goto out;
  737. if (sgl->length == 0)
  738. goto out;
  739. spin_lock_irqsave(&cohc->lock, flg);
  740. dev_vdbg(COHC_2_DEV(cohc), "[%s] sg_len %d dir %d\n",
  741. __func__, sg_len, direction);
  742. if (flags & DMA_PREP_INTERRUPT)
  743. /* Trigger interrupt after last lli */
  744. ctrl_last |= COH901318_CX_CTRL_TC_IRQ_ENABLE;
  745. cohd = coh901318_desc_get(cohc);
  746. cohd->sg = NULL;
  747. cohd->sg_len = 0;
  748. cohd->dir = direction;
  749. if (direction == DMA_TO_DEVICE) {
  750. u32 tx_flags = COH901318_CX_CTRL_PRDD_SOURCE |
  751. COH901318_CX_CTRL_SRC_ADDR_INC_ENABLE;
  752. ctrl_chained |= tx_flags;
  753. ctrl_last |= tx_flags;
  754. ctrl |= tx_flags;
  755. } else if (direction == DMA_FROM_DEVICE) {
  756. u32 rx_flags = COH901318_CX_CTRL_PRDD_DEST |
  757. COH901318_CX_CTRL_DST_ADDR_INC_ENABLE;
  758. ctrl_chained |= rx_flags;
  759. ctrl_last |= rx_flags;
  760. ctrl |= rx_flags;
  761. } else
  762. goto err_direction;
  763. dma_async_tx_descriptor_init(&cohd->desc, chan);
  764. cohd->desc.tx_submit = coh901318_tx_submit;
  765. /* The dma only supports transmitting packages up to
  766. * MAX_DMA_PACKET_SIZE. Calculate to total number of
  767. * dma elemts required to send the entire sg list
  768. */
  769. for_each_sg(sgl, sg, sg_len, i) {
  770. unsigned int factor;
  771. size = sg_dma_len(sg);
  772. if (size <= MAX_DMA_PACKET_SIZE) {
  773. len++;
  774. continue;
  775. }
  776. factor = size >> MAX_DMA_PACKET_SIZE_SHIFT;
  777. if ((factor << MAX_DMA_PACKET_SIZE_SHIFT) < size)
  778. factor++;
  779. len += factor;
  780. }
  781. data = coh901318_lli_alloc(&cohc->base->pool, len);
  782. if (data == NULL)
  783. goto err_dma_alloc;
  784. /* initiate allocated data list */
  785. cohd->pending_irqs =
  786. coh901318_lli_fill_sg(&cohc->base->pool, data, sgl, sg_len,
  787. cohc_dev_addr(cohc),
  788. ctrl_chained,
  789. ctrl,
  790. ctrl_last,
  791. direction, COH901318_CX_CTRL_TC_IRQ_ENABLE);
  792. cohd->data = data;
  793. cohd->flags = flags;
  794. COH_DBG(coh901318_list_print(cohc, data));
  795. spin_unlock_irqrestore(&cohc->lock, flg);
  796. return &cohd->desc;
  797. err_dma_alloc:
  798. err_direction:
  799. coh901318_desc_remove(cohd);
  800. coh901318_desc_free(cohc, cohd);
  801. spin_unlock_irqrestore(&cohc->lock, flg);
  802. out:
  803. return NULL;
  804. }
  805. static enum dma_status
  806. coh901318_is_tx_complete(struct dma_chan *chan,
  807. dma_cookie_t cookie, dma_cookie_t *done,
  808. dma_cookie_t *used)
  809. {
  810. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  811. dma_cookie_t last_used;
  812. dma_cookie_t last_complete;
  813. int ret;
  814. last_complete = cohc->completed;
  815. last_used = chan->cookie;
  816. ret = dma_async_is_complete(cookie, last_complete, last_used);
  817. if (done)
  818. *done = last_complete;
  819. if (used)
  820. *used = last_used;
  821. return ret;
  822. }
  823. static void
  824. coh901318_issue_pending(struct dma_chan *chan)
  825. {
  826. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  827. unsigned long flags;
  828. spin_lock_irqsave(&cohc->lock, flags);
  829. /* Busy means that pending jobs are already being processed */
  830. if (!cohc->busy)
  831. coh901318_queue_start(cohc);
  832. spin_unlock_irqrestore(&cohc->lock, flags);
  833. }
  834. static void
  835. coh901318_terminate_all(struct dma_chan *chan)
  836. {
  837. unsigned long flags;
  838. struct coh901318_chan *cohc = to_coh901318_chan(chan);
  839. struct coh901318_desc *cohd;
  840. void __iomem *virtbase = cohc->base->virtbase;
  841. coh901318_stop(chan);
  842. spin_lock_irqsave(&cohc->lock, flags);
  843. /* Clear any pending BE or TC interrupt */
  844. if (cohc->id < 32) {
  845. writel(1 << cohc->id, virtbase + COH901318_BE_INT_CLEAR1);
  846. writel(1 << cohc->id, virtbase + COH901318_TC_INT_CLEAR1);
  847. } else {
  848. writel(1 << (cohc->id - 32), virtbase +
  849. COH901318_BE_INT_CLEAR2);
  850. writel(1 << (cohc->id - 32), virtbase +
  851. COH901318_TC_INT_CLEAR2);
  852. }
  853. enable_powersave(cohc);
  854. while ((cohd = coh901318_first_active_get(cohc))) {
  855. /* release the lli allocation*/
  856. coh901318_lli_free(&cohc->base->pool, &cohd->data);
  857. coh901318_desc_remove(cohd);
  858. /* return desc to free-list */
  859. coh901318_desc_free(cohc, cohd);
  860. }
  861. while ((cohd = coh901318_first_queued(cohc))) {
  862. /* release the lli allocation*/
  863. coh901318_lli_free(&cohc->base->pool, &cohd->data);
  864. coh901318_desc_remove(cohd);
  865. /* return desc to free-list */
  866. coh901318_desc_free(cohc, cohd);
  867. }
  868. cohc->nbr_active_done = 0;
  869. cohc->busy = 0;
  870. cohc->pending_irqs = 0;
  871. spin_unlock_irqrestore(&cohc->lock, flags);
  872. }
  873. void coh901318_base_init(struct dma_device *dma, const int *pick_chans,
  874. struct coh901318_base *base)
  875. {
  876. int chans_i;
  877. int i = 0;
  878. struct coh901318_chan *cohc;
  879. INIT_LIST_HEAD(&dma->channels);
  880. for (chans_i = 0; pick_chans[chans_i] != -1; chans_i += 2) {
  881. for (i = pick_chans[chans_i]; i <= pick_chans[chans_i+1]; i++) {
  882. cohc = &base->chans[i];
  883. cohc->base = base;
  884. cohc->chan.device = dma;
  885. cohc->id = i;
  886. /* TODO: do we really need this lock if only one
  887. * client is connected to each channel?
  888. */
  889. spin_lock_init(&cohc->lock);
  890. cohc->pending_irqs = 0;
  891. cohc->nbr_active_done = 0;
  892. cohc->busy = 0;
  893. INIT_LIST_HEAD(&cohc->free);
  894. INIT_LIST_HEAD(&cohc->active);
  895. INIT_LIST_HEAD(&cohc->queue);
  896. tasklet_init(&cohc->tasklet, dma_tasklet,
  897. (unsigned long) cohc);
  898. list_add_tail(&cohc->chan.device_node,
  899. &dma->channels);
  900. }
  901. }
  902. }
  903. static int __init coh901318_probe(struct platform_device *pdev)
  904. {
  905. int err = 0;
  906. struct coh901318_platform *pdata;
  907. struct coh901318_base *base;
  908. int irq;
  909. struct resource *io;
  910. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  911. if (!io)
  912. goto err_get_resource;
  913. /* Map DMA controller registers to virtual memory */
  914. if (request_mem_region(io->start,
  915. resource_size(io),
  916. pdev->dev.driver->name) == NULL) {
  917. err = -EBUSY;
  918. goto err_request_mem;
  919. }
  920. pdata = pdev->dev.platform_data;
  921. if (!pdata)
  922. goto err_no_platformdata;
  923. base = kmalloc(ALIGN(sizeof(struct coh901318_base), 4) +
  924. pdata->max_channels *
  925. sizeof(struct coh901318_chan),
  926. GFP_KERNEL);
  927. if (!base)
  928. goto err_alloc_coh_dma_channels;
  929. base->chans = ((void *)base) + ALIGN(sizeof(struct coh901318_base), 4);
  930. base->virtbase = ioremap(io->start, resource_size(io));
  931. if (!base->virtbase) {
  932. err = -ENOMEM;
  933. goto err_no_ioremap;
  934. }
  935. base->dev = &pdev->dev;
  936. base->platform = pdata;
  937. spin_lock_init(&base->pm.lock);
  938. base->pm.started_channels = 0;
  939. COH901318_DEBUGFS_ASSIGN(debugfs_dma_base, base);
  940. platform_set_drvdata(pdev, base);
  941. irq = platform_get_irq(pdev, 0);
  942. if (irq < 0)
  943. goto err_no_irq;
  944. err = request_irq(irq, dma_irq_handler, IRQF_DISABLED,
  945. "coh901318", base);
  946. if (err) {
  947. dev_crit(&pdev->dev,
  948. "Cannot allocate IRQ for DMA controller!\n");
  949. goto err_request_irq;
  950. }
  951. err = coh901318_pool_create(&base->pool, &pdev->dev,
  952. sizeof(struct coh901318_lli),
  953. 32);
  954. if (err)
  955. goto err_pool_create;
  956. /* init channels for device transfers */
  957. coh901318_base_init(&base->dma_slave, base->platform->chans_slave,
  958. base);
  959. dma_cap_zero(base->dma_slave.cap_mask);
  960. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  961. base->dma_slave.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  962. base->dma_slave.device_free_chan_resources = coh901318_free_chan_resources;
  963. base->dma_slave.device_prep_slave_sg = coh901318_prep_slave_sg;
  964. base->dma_slave.device_is_tx_complete = coh901318_is_tx_complete;
  965. base->dma_slave.device_issue_pending = coh901318_issue_pending;
  966. base->dma_slave.device_terminate_all = coh901318_terminate_all;
  967. base->dma_slave.dev = &pdev->dev;
  968. err = dma_async_device_register(&base->dma_slave);
  969. if (err)
  970. goto err_register_slave;
  971. /* init channels for memcpy */
  972. coh901318_base_init(&base->dma_memcpy, base->platform->chans_memcpy,
  973. base);
  974. dma_cap_zero(base->dma_memcpy.cap_mask);
  975. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  976. base->dma_memcpy.device_alloc_chan_resources = coh901318_alloc_chan_resources;
  977. base->dma_memcpy.device_free_chan_resources = coh901318_free_chan_resources;
  978. base->dma_memcpy.device_prep_dma_memcpy = coh901318_prep_memcpy;
  979. base->dma_memcpy.device_is_tx_complete = coh901318_is_tx_complete;
  980. base->dma_memcpy.device_issue_pending = coh901318_issue_pending;
  981. base->dma_memcpy.device_terminate_all = coh901318_terminate_all;
  982. base->dma_memcpy.dev = &pdev->dev;
  983. err = dma_async_device_register(&base->dma_memcpy);
  984. if (err)
  985. goto err_register_memcpy;
  986. dev_dbg(&pdev->dev, "Initialized COH901318 DMA on virtual base 0x%08x\n",
  987. (u32) base->virtbase);
  988. return err;
  989. err_register_memcpy:
  990. dma_async_device_unregister(&base->dma_slave);
  991. err_register_slave:
  992. coh901318_pool_destroy(&base->pool);
  993. err_pool_create:
  994. free_irq(platform_get_irq(pdev, 0), base);
  995. err_request_irq:
  996. err_no_irq:
  997. iounmap(base->virtbase);
  998. err_no_ioremap:
  999. kfree(base);
  1000. err_alloc_coh_dma_channels:
  1001. err_no_platformdata:
  1002. release_mem_region(pdev->resource->start,
  1003. resource_size(pdev->resource));
  1004. err_request_mem:
  1005. err_get_resource:
  1006. return err;
  1007. }
  1008. static int __exit coh901318_remove(struct platform_device *pdev)
  1009. {
  1010. struct coh901318_base *base = platform_get_drvdata(pdev);
  1011. dma_async_device_unregister(&base->dma_memcpy);
  1012. dma_async_device_unregister(&base->dma_slave);
  1013. coh901318_pool_destroy(&base->pool);
  1014. free_irq(platform_get_irq(pdev, 0), base);
  1015. iounmap(base->virtbase);
  1016. kfree(base);
  1017. release_mem_region(pdev->resource->start,
  1018. resource_size(pdev->resource));
  1019. return 0;
  1020. }
  1021. static struct platform_driver coh901318_driver = {
  1022. .remove = __exit_p(coh901318_remove),
  1023. .driver = {
  1024. .name = "coh901318",
  1025. },
  1026. };
  1027. int __init coh901318_init(void)
  1028. {
  1029. return platform_driver_probe(&coh901318_driver, coh901318_probe);
  1030. }
  1031. subsys_initcall(coh901318_init);
  1032. void __exit coh901318_exit(void)
  1033. {
  1034. platform_driver_unregister(&coh901318_driver);
  1035. }
  1036. module_exit(coh901318_exit);
  1037. MODULE_LICENSE("GPL");
  1038. MODULE_AUTHOR("Per Friden");