coh901318.c 39 KB

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