coh901318.c 39 KB

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