mv_xor.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * offload engine driver for the Marvell XOR engine
  3. * Copyright (C) 2007, 2008, Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/memory.h>
  27. #include <linux/clk.h>
  28. #include <linux/of.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/irqdomain.h>
  31. #include <linux/platform_data/dma-mv_xor.h>
  32. #include "dmaengine.h"
  33. #include "mv_xor.h"
  34. static void mv_xor_issue_pending(struct dma_chan *chan);
  35. #define to_mv_xor_chan(chan) \
  36. container_of(chan, struct mv_xor_chan, dmachan)
  37. #define to_mv_xor_slot(tx) \
  38. container_of(tx, struct mv_xor_desc_slot, async_tx)
  39. #define mv_chan_to_devp(chan) \
  40. ((chan)->dmadev.dev)
  41. static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags)
  42. {
  43. struct mv_xor_desc *hw_desc = desc->hw_desc;
  44. hw_desc->status = (1 << 31);
  45. hw_desc->phy_next_desc = 0;
  46. hw_desc->desc_command = (1 << 31);
  47. }
  48. static u32 mv_desc_get_dest_addr(struct mv_xor_desc_slot *desc)
  49. {
  50. struct mv_xor_desc *hw_desc = desc->hw_desc;
  51. return hw_desc->phy_dest_addr;
  52. }
  53. static u32 mv_desc_get_src_addr(struct mv_xor_desc_slot *desc,
  54. int src_idx)
  55. {
  56. struct mv_xor_desc *hw_desc = desc->hw_desc;
  57. return hw_desc->phy_src_addr[src_idx];
  58. }
  59. static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc,
  60. u32 byte_count)
  61. {
  62. struct mv_xor_desc *hw_desc = desc->hw_desc;
  63. hw_desc->byte_count = byte_count;
  64. }
  65. static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
  66. u32 next_desc_addr)
  67. {
  68. struct mv_xor_desc *hw_desc = desc->hw_desc;
  69. BUG_ON(hw_desc->phy_next_desc);
  70. hw_desc->phy_next_desc = next_desc_addr;
  71. }
  72. static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
  73. {
  74. struct mv_xor_desc *hw_desc = desc->hw_desc;
  75. hw_desc->phy_next_desc = 0;
  76. }
  77. static void mv_desc_set_block_fill_val(struct mv_xor_desc_slot *desc, u32 val)
  78. {
  79. desc->value = val;
  80. }
  81. static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc,
  82. dma_addr_t addr)
  83. {
  84. struct mv_xor_desc *hw_desc = desc->hw_desc;
  85. hw_desc->phy_dest_addr = addr;
  86. }
  87. static int mv_chan_memset_slot_count(size_t len)
  88. {
  89. return 1;
  90. }
  91. #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
  92. static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
  93. int index, dma_addr_t addr)
  94. {
  95. struct mv_xor_desc *hw_desc = desc->hw_desc;
  96. hw_desc->phy_src_addr[index] = addr;
  97. if (desc->type == DMA_XOR)
  98. hw_desc->desc_command |= (1 << index);
  99. }
  100. static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
  101. {
  102. return __raw_readl(XOR_CURR_DESC(chan));
  103. }
  104. static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
  105. u32 next_desc_addr)
  106. {
  107. __raw_writel(next_desc_addr, XOR_NEXT_DESC(chan));
  108. }
  109. static void mv_chan_set_dest_pointer(struct mv_xor_chan *chan, u32 desc_addr)
  110. {
  111. __raw_writel(desc_addr, XOR_DEST_POINTER(chan));
  112. }
  113. static void mv_chan_set_block_size(struct mv_xor_chan *chan, u32 block_size)
  114. {
  115. __raw_writel(block_size, XOR_BLOCK_SIZE(chan));
  116. }
  117. static void mv_chan_set_value(struct mv_xor_chan *chan, u32 value)
  118. {
  119. __raw_writel(value, XOR_INIT_VALUE_LOW(chan));
  120. __raw_writel(value, XOR_INIT_VALUE_HIGH(chan));
  121. }
  122. static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
  123. {
  124. u32 val = __raw_readl(XOR_INTR_MASK(chan));
  125. val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
  126. __raw_writel(val, XOR_INTR_MASK(chan));
  127. }
  128. static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
  129. {
  130. u32 intr_cause = __raw_readl(XOR_INTR_CAUSE(chan));
  131. intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
  132. return intr_cause;
  133. }
  134. static int mv_is_err_intr(u32 intr_cause)
  135. {
  136. if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
  137. return 1;
  138. return 0;
  139. }
  140. static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
  141. {
  142. u32 val = ~(1 << (chan->idx * 16));
  143. dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
  144. __raw_writel(val, XOR_INTR_CAUSE(chan));
  145. }
  146. static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
  147. {
  148. u32 val = 0xFFFF0000 >> (chan->idx * 16);
  149. __raw_writel(val, XOR_INTR_CAUSE(chan));
  150. }
  151. static int mv_can_chain(struct mv_xor_desc_slot *desc)
  152. {
  153. struct mv_xor_desc_slot *chain_old_tail = list_entry(
  154. desc->chain_node.prev, struct mv_xor_desc_slot, chain_node);
  155. if (chain_old_tail->type != desc->type)
  156. return 0;
  157. if (desc->type == DMA_MEMSET)
  158. return 0;
  159. return 1;
  160. }
  161. static void mv_set_mode(struct mv_xor_chan *chan,
  162. enum dma_transaction_type type)
  163. {
  164. u32 op_mode;
  165. u32 config = __raw_readl(XOR_CONFIG(chan));
  166. switch (type) {
  167. case DMA_XOR:
  168. op_mode = XOR_OPERATION_MODE_XOR;
  169. break;
  170. case DMA_MEMCPY:
  171. op_mode = XOR_OPERATION_MODE_MEMCPY;
  172. break;
  173. case DMA_MEMSET:
  174. op_mode = XOR_OPERATION_MODE_MEMSET;
  175. break;
  176. default:
  177. dev_err(mv_chan_to_devp(chan),
  178. "error: unsupported operation %d.\n",
  179. type);
  180. BUG();
  181. return;
  182. }
  183. config &= ~0x7;
  184. config |= op_mode;
  185. __raw_writel(config, XOR_CONFIG(chan));
  186. chan->current_type = type;
  187. }
  188. static void mv_chan_activate(struct mv_xor_chan *chan)
  189. {
  190. u32 activation;
  191. dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
  192. activation = __raw_readl(XOR_ACTIVATION(chan));
  193. activation |= 0x1;
  194. __raw_writel(activation, XOR_ACTIVATION(chan));
  195. }
  196. static char mv_chan_is_busy(struct mv_xor_chan *chan)
  197. {
  198. u32 state = __raw_readl(XOR_ACTIVATION(chan));
  199. state = (state >> 4) & 0x3;
  200. return (state == 1) ? 1 : 0;
  201. }
  202. static int mv_chan_xor_slot_count(size_t len, int src_cnt)
  203. {
  204. return 1;
  205. }
  206. /**
  207. * mv_xor_free_slots - flags descriptor slots for reuse
  208. * @slot: Slot to free
  209. * Caller must hold &mv_chan->lock while calling this function
  210. */
  211. static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
  212. struct mv_xor_desc_slot *slot)
  213. {
  214. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
  215. __func__, __LINE__, slot);
  216. slot->slots_per_op = 0;
  217. }
  218. /*
  219. * mv_xor_start_new_chain - program the engine to operate on new chain headed by
  220. * sw_desc
  221. * Caller must hold &mv_chan->lock while calling this function
  222. */
  223. static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
  224. struct mv_xor_desc_slot *sw_desc)
  225. {
  226. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
  227. __func__, __LINE__, sw_desc);
  228. if (sw_desc->type != mv_chan->current_type)
  229. mv_set_mode(mv_chan, sw_desc->type);
  230. if (sw_desc->type == DMA_MEMSET) {
  231. /* for memset requests we need to program the engine, no
  232. * descriptors used.
  233. */
  234. struct mv_xor_desc *hw_desc = sw_desc->hw_desc;
  235. mv_chan_set_dest_pointer(mv_chan, hw_desc->phy_dest_addr);
  236. mv_chan_set_block_size(mv_chan, sw_desc->unmap_len);
  237. mv_chan_set_value(mv_chan, sw_desc->value);
  238. } else {
  239. /* set the hardware chain */
  240. mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
  241. }
  242. mv_chan->pending += sw_desc->slot_cnt;
  243. mv_xor_issue_pending(&mv_chan->dmachan);
  244. }
  245. static dma_cookie_t
  246. mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
  247. struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
  248. {
  249. BUG_ON(desc->async_tx.cookie < 0);
  250. if (desc->async_tx.cookie > 0) {
  251. cookie = desc->async_tx.cookie;
  252. /* call the callback (must not sleep or submit new
  253. * operations to this channel)
  254. */
  255. if (desc->async_tx.callback)
  256. desc->async_tx.callback(
  257. desc->async_tx.callback_param);
  258. /* unmap dma addresses
  259. * (unmap_single vs unmap_page?)
  260. */
  261. if (desc->group_head && desc->unmap_len) {
  262. struct mv_xor_desc_slot *unmap = desc->group_head;
  263. struct device *dev = mv_chan_to_devp(mv_chan);
  264. u32 len = unmap->unmap_len;
  265. enum dma_ctrl_flags flags = desc->async_tx.flags;
  266. u32 src_cnt;
  267. dma_addr_t addr;
  268. dma_addr_t dest;
  269. src_cnt = unmap->unmap_src_cnt;
  270. dest = mv_desc_get_dest_addr(unmap);
  271. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  272. enum dma_data_direction dir;
  273. if (src_cnt > 1) /* is xor ? */
  274. dir = DMA_BIDIRECTIONAL;
  275. else
  276. dir = DMA_FROM_DEVICE;
  277. dma_unmap_page(dev, dest, len, dir);
  278. }
  279. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  280. while (src_cnt--) {
  281. addr = mv_desc_get_src_addr(unmap,
  282. src_cnt);
  283. if (addr == dest)
  284. continue;
  285. dma_unmap_page(dev, addr, len,
  286. DMA_TO_DEVICE);
  287. }
  288. }
  289. desc->group_head = NULL;
  290. }
  291. }
  292. /* run dependent operations */
  293. dma_run_dependencies(&desc->async_tx);
  294. return cookie;
  295. }
  296. static int
  297. mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
  298. {
  299. struct mv_xor_desc_slot *iter, *_iter;
  300. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  301. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  302. completed_node) {
  303. if (async_tx_test_ack(&iter->async_tx)) {
  304. list_del(&iter->completed_node);
  305. mv_xor_free_slots(mv_chan, iter);
  306. }
  307. }
  308. return 0;
  309. }
  310. static int
  311. mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
  312. struct mv_xor_chan *mv_chan)
  313. {
  314. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
  315. __func__, __LINE__, desc, desc->async_tx.flags);
  316. list_del(&desc->chain_node);
  317. /* the client is allowed to attach dependent operations
  318. * until 'ack' is set
  319. */
  320. if (!async_tx_test_ack(&desc->async_tx)) {
  321. /* move this slot to the completed_slots */
  322. list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
  323. return 0;
  324. }
  325. mv_xor_free_slots(mv_chan, desc);
  326. return 0;
  327. }
  328. static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  329. {
  330. struct mv_xor_desc_slot *iter, *_iter;
  331. dma_cookie_t cookie = 0;
  332. int busy = mv_chan_is_busy(mv_chan);
  333. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  334. int seen_current = 0;
  335. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  336. dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
  337. mv_xor_clean_completed_slots(mv_chan);
  338. /* free completed slots from the chain starting with
  339. * the oldest descriptor
  340. */
  341. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  342. chain_node) {
  343. prefetch(_iter);
  344. prefetch(&_iter->async_tx);
  345. /* do not advance past the current descriptor loaded into the
  346. * hardware channel, subsequent descriptors are either in
  347. * process or have not been submitted
  348. */
  349. if (seen_current)
  350. break;
  351. /* stop the search if we reach the current descriptor and the
  352. * channel is busy
  353. */
  354. if (iter->async_tx.phys == current_desc) {
  355. seen_current = 1;
  356. if (busy)
  357. break;
  358. }
  359. cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
  360. if (mv_xor_clean_slot(iter, mv_chan))
  361. break;
  362. }
  363. if ((busy == 0) && !list_empty(&mv_chan->chain)) {
  364. struct mv_xor_desc_slot *chain_head;
  365. chain_head = list_entry(mv_chan->chain.next,
  366. struct mv_xor_desc_slot,
  367. chain_node);
  368. mv_xor_start_new_chain(mv_chan, chain_head);
  369. }
  370. if (cookie > 0)
  371. mv_chan->dmachan.completed_cookie = cookie;
  372. }
  373. static void
  374. mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  375. {
  376. spin_lock_bh(&mv_chan->lock);
  377. __mv_xor_slot_cleanup(mv_chan);
  378. spin_unlock_bh(&mv_chan->lock);
  379. }
  380. static void mv_xor_tasklet(unsigned long data)
  381. {
  382. struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
  383. mv_xor_slot_cleanup(chan);
  384. }
  385. static struct mv_xor_desc_slot *
  386. mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots,
  387. int slots_per_op)
  388. {
  389. struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL;
  390. LIST_HEAD(chain);
  391. int slots_found, retry = 0;
  392. /* start search from the last allocated descrtiptor
  393. * if a contiguous allocation can not be found start searching
  394. * from the beginning of the list
  395. */
  396. retry:
  397. slots_found = 0;
  398. if (retry == 0)
  399. iter = mv_chan->last_used;
  400. else
  401. iter = list_entry(&mv_chan->all_slots,
  402. struct mv_xor_desc_slot,
  403. slot_node);
  404. list_for_each_entry_safe_continue(
  405. iter, _iter, &mv_chan->all_slots, slot_node) {
  406. prefetch(_iter);
  407. prefetch(&_iter->async_tx);
  408. if (iter->slots_per_op) {
  409. /* give up after finding the first busy slot
  410. * on the second pass through the list
  411. */
  412. if (retry)
  413. break;
  414. slots_found = 0;
  415. continue;
  416. }
  417. /* start the allocation if the slot is correctly aligned */
  418. if (!slots_found++)
  419. alloc_start = iter;
  420. if (slots_found == num_slots) {
  421. struct mv_xor_desc_slot *alloc_tail = NULL;
  422. struct mv_xor_desc_slot *last_used = NULL;
  423. iter = alloc_start;
  424. while (num_slots) {
  425. int i;
  426. /* pre-ack all but the last descriptor */
  427. async_tx_ack(&iter->async_tx);
  428. list_add_tail(&iter->chain_node, &chain);
  429. alloc_tail = iter;
  430. iter->async_tx.cookie = 0;
  431. iter->slot_cnt = num_slots;
  432. iter->xor_check_result = NULL;
  433. for (i = 0; i < slots_per_op; i++) {
  434. iter->slots_per_op = slots_per_op - i;
  435. last_used = iter;
  436. iter = list_entry(iter->slot_node.next,
  437. struct mv_xor_desc_slot,
  438. slot_node);
  439. }
  440. num_slots -= slots_per_op;
  441. }
  442. alloc_tail->group_head = alloc_start;
  443. alloc_tail->async_tx.cookie = -EBUSY;
  444. list_splice(&chain, &alloc_tail->tx_list);
  445. mv_chan->last_used = last_used;
  446. mv_desc_clear_next_desc(alloc_start);
  447. mv_desc_clear_next_desc(alloc_tail);
  448. return alloc_tail;
  449. }
  450. }
  451. if (!retry++)
  452. goto retry;
  453. /* try to free some slots if the allocation fails */
  454. tasklet_schedule(&mv_chan->irq_tasklet);
  455. return NULL;
  456. }
  457. /************************ DMA engine API functions ****************************/
  458. static dma_cookie_t
  459. mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
  460. {
  461. struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
  462. struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
  463. struct mv_xor_desc_slot *grp_start, *old_chain_tail;
  464. dma_cookie_t cookie;
  465. int new_hw_chain = 1;
  466. dev_dbg(mv_chan_to_devp(mv_chan),
  467. "%s sw_desc %p: async_tx %p\n",
  468. __func__, sw_desc, &sw_desc->async_tx);
  469. grp_start = sw_desc->group_head;
  470. spin_lock_bh(&mv_chan->lock);
  471. cookie = dma_cookie_assign(tx);
  472. if (list_empty(&mv_chan->chain))
  473. list_splice_init(&sw_desc->tx_list, &mv_chan->chain);
  474. else {
  475. new_hw_chain = 0;
  476. old_chain_tail = list_entry(mv_chan->chain.prev,
  477. struct mv_xor_desc_slot,
  478. chain_node);
  479. list_splice_init(&grp_start->tx_list,
  480. &old_chain_tail->chain_node);
  481. if (!mv_can_chain(grp_start))
  482. goto submit_done;
  483. dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n",
  484. old_chain_tail->async_tx.phys);
  485. /* fix up the hardware chain */
  486. mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
  487. /* if the channel is not busy */
  488. if (!mv_chan_is_busy(mv_chan)) {
  489. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  490. /*
  491. * and the curren desc is the end of the chain before
  492. * the append, then we need to start the channel
  493. */
  494. if (current_desc == old_chain_tail->async_tx.phys)
  495. new_hw_chain = 1;
  496. }
  497. }
  498. if (new_hw_chain)
  499. mv_xor_start_new_chain(mv_chan, grp_start);
  500. submit_done:
  501. spin_unlock_bh(&mv_chan->lock);
  502. return cookie;
  503. }
  504. /* returns the number of allocated descriptors */
  505. static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
  506. {
  507. char *hw_desc;
  508. int idx;
  509. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  510. struct mv_xor_desc_slot *slot = NULL;
  511. int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
  512. /* Allocate descriptor slots */
  513. idx = mv_chan->slots_allocated;
  514. while (idx < num_descs_in_pool) {
  515. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  516. if (!slot) {
  517. printk(KERN_INFO "MV XOR Channel only initialized"
  518. " %d descriptor slots", idx);
  519. break;
  520. }
  521. hw_desc = (char *) mv_chan->dma_desc_pool_virt;
  522. slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  523. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  524. slot->async_tx.tx_submit = mv_xor_tx_submit;
  525. INIT_LIST_HEAD(&slot->chain_node);
  526. INIT_LIST_HEAD(&slot->slot_node);
  527. INIT_LIST_HEAD(&slot->tx_list);
  528. hw_desc = (char *) mv_chan->dma_desc_pool;
  529. slot->async_tx.phys =
  530. (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  531. slot->idx = idx++;
  532. spin_lock_bh(&mv_chan->lock);
  533. mv_chan->slots_allocated = idx;
  534. list_add_tail(&slot->slot_node, &mv_chan->all_slots);
  535. spin_unlock_bh(&mv_chan->lock);
  536. }
  537. if (mv_chan->slots_allocated && !mv_chan->last_used)
  538. mv_chan->last_used = list_entry(mv_chan->all_slots.next,
  539. struct mv_xor_desc_slot,
  540. slot_node);
  541. dev_dbg(mv_chan_to_devp(mv_chan),
  542. "allocated %d descriptor slots last_used: %p\n",
  543. mv_chan->slots_allocated, mv_chan->last_used);
  544. return mv_chan->slots_allocated ? : -ENOMEM;
  545. }
  546. static struct dma_async_tx_descriptor *
  547. mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  548. size_t len, unsigned long flags)
  549. {
  550. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  551. struct mv_xor_desc_slot *sw_desc, *grp_start;
  552. int slot_cnt;
  553. dev_dbg(mv_chan_to_devp(mv_chan),
  554. "%s dest: %x src %x len: %u flags: %ld\n",
  555. __func__, dest, src, len, flags);
  556. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  557. return NULL;
  558. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  559. spin_lock_bh(&mv_chan->lock);
  560. slot_cnt = mv_chan_memcpy_slot_count(len);
  561. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  562. if (sw_desc) {
  563. sw_desc->type = DMA_MEMCPY;
  564. sw_desc->async_tx.flags = flags;
  565. grp_start = sw_desc->group_head;
  566. mv_desc_init(grp_start, flags);
  567. mv_desc_set_byte_count(grp_start, len);
  568. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  569. mv_desc_set_src_addr(grp_start, 0, src);
  570. sw_desc->unmap_src_cnt = 1;
  571. sw_desc->unmap_len = len;
  572. }
  573. spin_unlock_bh(&mv_chan->lock);
  574. dev_dbg(mv_chan_to_devp(mv_chan),
  575. "%s sw_desc %p async_tx %p\n",
  576. __func__, sw_desc, sw_desc ? &sw_desc->async_tx : 0);
  577. return sw_desc ? &sw_desc->async_tx : NULL;
  578. }
  579. static struct dma_async_tx_descriptor *
  580. mv_xor_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  581. size_t len, unsigned long flags)
  582. {
  583. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  584. struct mv_xor_desc_slot *sw_desc, *grp_start;
  585. int slot_cnt;
  586. dev_dbg(mv_chan_to_devp(mv_chan),
  587. "%s dest: %x len: %u flags: %ld\n",
  588. __func__, dest, len, flags);
  589. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  590. return NULL;
  591. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  592. spin_lock_bh(&mv_chan->lock);
  593. slot_cnt = mv_chan_memset_slot_count(len);
  594. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  595. if (sw_desc) {
  596. sw_desc->type = DMA_MEMSET;
  597. sw_desc->async_tx.flags = flags;
  598. grp_start = sw_desc->group_head;
  599. mv_desc_init(grp_start, flags);
  600. mv_desc_set_byte_count(grp_start, len);
  601. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  602. mv_desc_set_block_fill_val(grp_start, value);
  603. sw_desc->unmap_src_cnt = 1;
  604. sw_desc->unmap_len = len;
  605. }
  606. spin_unlock_bh(&mv_chan->lock);
  607. dev_dbg(mv_chan_to_devp(mv_chan),
  608. "%s sw_desc %p async_tx %p \n",
  609. __func__, sw_desc, &sw_desc->async_tx);
  610. return sw_desc ? &sw_desc->async_tx : NULL;
  611. }
  612. static struct dma_async_tx_descriptor *
  613. mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  614. unsigned int src_cnt, size_t len, unsigned long flags)
  615. {
  616. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  617. struct mv_xor_desc_slot *sw_desc, *grp_start;
  618. int slot_cnt;
  619. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  620. return NULL;
  621. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  622. dev_dbg(mv_chan_to_devp(mv_chan),
  623. "%s src_cnt: %d len: dest %x %u flags: %ld\n",
  624. __func__, src_cnt, len, dest, flags);
  625. spin_lock_bh(&mv_chan->lock);
  626. slot_cnt = mv_chan_xor_slot_count(len, src_cnt);
  627. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  628. if (sw_desc) {
  629. sw_desc->type = DMA_XOR;
  630. sw_desc->async_tx.flags = flags;
  631. grp_start = sw_desc->group_head;
  632. mv_desc_init(grp_start, flags);
  633. /* the byte count field is the same as in memcpy desc*/
  634. mv_desc_set_byte_count(grp_start, len);
  635. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  636. sw_desc->unmap_src_cnt = src_cnt;
  637. sw_desc->unmap_len = len;
  638. while (src_cnt--)
  639. mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]);
  640. }
  641. spin_unlock_bh(&mv_chan->lock);
  642. dev_dbg(mv_chan_to_devp(mv_chan),
  643. "%s sw_desc %p async_tx %p \n",
  644. __func__, sw_desc, &sw_desc->async_tx);
  645. return sw_desc ? &sw_desc->async_tx : NULL;
  646. }
  647. static void mv_xor_free_chan_resources(struct dma_chan *chan)
  648. {
  649. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  650. struct mv_xor_desc_slot *iter, *_iter;
  651. int in_use_descs = 0;
  652. mv_xor_slot_cleanup(mv_chan);
  653. spin_lock_bh(&mv_chan->lock);
  654. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  655. chain_node) {
  656. in_use_descs++;
  657. list_del(&iter->chain_node);
  658. }
  659. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  660. completed_node) {
  661. in_use_descs++;
  662. list_del(&iter->completed_node);
  663. }
  664. list_for_each_entry_safe_reverse(
  665. iter, _iter, &mv_chan->all_slots, slot_node) {
  666. list_del(&iter->slot_node);
  667. kfree(iter);
  668. mv_chan->slots_allocated--;
  669. }
  670. mv_chan->last_used = NULL;
  671. dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
  672. __func__, mv_chan->slots_allocated);
  673. spin_unlock_bh(&mv_chan->lock);
  674. if (in_use_descs)
  675. dev_err(mv_chan_to_devp(mv_chan),
  676. "freeing %d in use descriptors!\n", in_use_descs);
  677. }
  678. /**
  679. * mv_xor_status - poll the status of an XOR transaction
  680. * @chan: XOR channel handle
  681. * @cookie: XOR transaction identifier
  682. * @txstate: XOR transactions state holder (or NULL)
  683. */
  684. static enum dma_status mv_xor_status(struct dma_chan *chan,
  685. dma_cookie_t cookie,
  686. struct dma_tx_state *txstate)
  687. {
  688. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  689. enum dma_status ret;
  690. ret = dma_cookie_status(chan, cookie, txstate);
  691. if (ret == DMA_SUCCESS) {
  692. mv_xor_clean_completed_slots(mv_chan);
  693. return ret;
  694. }
  695. mv_xor_slot_cleanup(mv_chan);
  696. return dma_cookie_status(chan, cookie, txstate);
  697. }
  698. static void mv_dump_xor_regs(struct mv_xor_chan *chan)
  699. {
  700. u32 val;
  701. val = __raw_readl(XOR_CONFIG(chan));
  702. dev_err(mv_chan_to_devp(chan),
  703. "config 0x%08x.\n", val);
  704. val = __raw_readl(XOR_ACTIVATION(chan));
  705. dev_err(mv_chan_to_devp(chan),
  706. "activation 0x%08x.\n", val);
  707. val = __raw_readl(XOR_INTR_CAUSE(chan));
  708. dev_err(mv_chan_to_devp(chan),
  709. "intr cause 0x%08x.\n", val);
  710. val = __raw_readl(XOR_INTR_MASK(chan));
  711. dev_err(mv_chan_to_devp(chan),
  712. "intr mask 0x%08x.\n", val);
  713. val = __raw_readl(XOR_ERROR_CAUSE(chan));
  714. dev_err(mv_chan_to_devp(chan),
  715. "error cause 0x%08x.\n", val);
  716. val = __raw_readl(XOR_ERROR_ADDR(chan));
  717. dev_err(mv_chan_to_devp(chan),
  718. "error addr 0x%08x.\n", val);
  719. }
  720. static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
  721. u32 intr_cause)
  722. {
  723. if (intr_cause & (1 << 4)) {
  724. dev_dbg(mv_chan_to_devp(chan),
  725. "ignore this error\n");
  726. return;
  727. }
  728. dev_err(mv_chan_to_devp(chan),
  729. "error on chan %d. intr cause 0x%08x.\n",
  730. chan->idx, intr_cause);
  731. mv_dump_xor_regs(chan);
  732. BUG();
  733. }
  734. static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
  735. {
  736. struct mv_xor_chan *chan = data;
  737. u32 intr_cause = mv_chan_get_intr_cause(chan);
  738. dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
  739. if (mv_is_err_intr(intr_cause))
  740. mv_xor_err_interrupt_handler(chan, intr_cause);
  741. tasklet_schedule(&chan->irq_tasklet);
  742. mv_xor_device_clear_eoc_cause(chan);
  743. return IRQ_HANDLED;
  744. }
  745. static void mv_xor_issue_pending(struct dma_chan *chan)
  746. {
  747. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  748. if (mv_chan->pending >= MV_XOR_THRESHOLD) {
  749. mv_chan->pending = 0;
  750. mv_chan_activate(mv_chan);
  751. }
  752. }
  753. /*
  754. * Perform a transaction to verify the HW works.
  755. */
  756. #define MV_XOR_TEST_SIZE 2000
  757. static int __devinit mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
  758. {
  759. int i;
  760. void *src, *dest;
  761. dma_addr_t src_dma, dest_dma;
  762. struct dma_chan *dma_chan;
  763. dma_cookie_t cookie;
  764. struct dma_async_tx_descriptor *tx;
  765. int err = 0;
  766. src = kmalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  767. if (!src)
  768. return -ENOMEM;
  769. dest = kzalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  770. if (!dest) {
  771. kfree(src);
  772. return -ENOMEM;
  773. }
  774. /* Fill in src buffer */
  775. for (i = 0; i < MV_XOR_TEST_SIZE; i++)
  776. ((u8 *) src)[i] = (u8)i;
  777. dma_chan = &mv_chan->dmachan;
  778. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  779. err = -ENODEV;
  780. goto out;
  781. }
  782. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  783. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  784. src_dma = dma_map_single(dma_chan->device->dev, src,
  785. MV_XOR_TEST_SIZE, DMA_TO_DEVICE);
  786. tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  787. MV_XOR_TEST_SIZE, 0);
  788. cookie = mv_xor_tx_submit(tx);
  789. mv_xor_issue_pending(dma_chan);
  790. async_tx_ack(tx);
  791. msleep(1);
  792. if (mv_xor_status(dma_chan, cookie, NULL) !=
  793. DMA_SUCCESS) {
  794. dev_err(dma_chan->device->dev,
  795. "Self-test copy timed out, disabling\n");
  796. err = -ENODEV;
  797. goto free_resources;
  798. }
  799. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  800. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  801. if (memcmp(src, dest, MV_XOR_TEST_SIZE)) {
  802. dev_err(dma_chan->device->dev,
  803. "Self-test copy failed compare, disabling\n");
  804. err = -ENODEV;
  805. goto free_resources;
  806. }
  807. free_resources:
  808. mv_xor_free_chan_resources(dma_chan);
  809. out:
  810. kfree(src);
  811. kfree(dest);
  812. return err;
  813. }
  814. #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
  815. static int __devinit
  816. mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
  817. {
  818. int i, src_idx;
  819. struct page *dest;
  820. struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
  821. dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
  822. dma_addr_t dest_dma;
  823. struct dma_async_tx_descriptor *tx;
  824. struct dma_chan *dma_chan;
  825. dma_cookie_t cookie;
  826. u8 cmp_byte = 0;
  827. u32 cmp_word;
  828. int err = 0;
  829. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  830. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  831. if (!xor_srcs[src_idx]) {
  832. while (src_idx--)
  833. __free_page(xor_srcs[src_idx]);
  834. return -ENOMEM;
  835. }
  836. }
  837. dest = alloc_page(GFP_KERNEL);
  838. if (!dest) {
  839. while (src_idx--)
  840. __free_page(xor_srcs[src_idx]);
  841. return -ENOMEM;
  842. }
  843. /* Fill in src buffers */
  844. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  845. u8 *ptr = page_address(xor_srcs[src_idx]);
  846. for (i = 0; i < PAGE_SIZE; i++)
  847. ptr[i] = (1 << src_idx);
  848. }
  849. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++)
  850. cmp_byte ^= (u8) (1 << src_idx);
  851. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  852. (cmp_byte << 8) | cmp_byte;
  853. memset(page_address(dest), 0, PAGE_SIZE);
  854. dma_chan = &mv_chan->dmachan;
  855. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  856. err = -ENODEV;
  857. goto out;
  858. }
  859. /* test xor */
  860. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
  861. DMA_FROM_DEVICE);
  862. for (i = 0; i < MV_XOR_NUM_SRC_TEST; i++)
  863. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  864. 0, PAGE_SIZE, DMA_TO_DEVICE);
  865. tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  866. MV_XOR_NUM_SRC_TEST, PAGE_SIZE, 0);
  867. cookie = mv_xor_tx_submit(tx);
  868. mv_xor_issue_pending(dma_chan);
  869. async_tx_ack(tx);
  870. msleep(8);
  871. if (mv_xor_status(dma_chan, cookie, NULL) !=
  872. DMA_SUCCESS) {
  873. dev_err(dma_chan->device->dev,
  874. "Self-test xor timed out, disabling\n");
  875. err = -ENODEV;
  876. goto free_resources;
  877. }
  878. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  879. PAGE_SIZE, DMA_FROM_DEVICE);
  880. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  881. u32 *ptr = page_address(dest);
  882. if (ptr[i] != cmp_word) {
  883. dev_err(dma_chan->device->dev,
  884. "Self-test xor failed compare, disabling."
  885. " index %d, data %x, expected %x\n", i,
  886. ptr[i], cmp_word);
  887. err = -ENODEV;
  888. goto free_resources;
  889. }
  890. }
  891. free_resources:
  892. mv_xor_free_chan_resources(dma_chan);
  893. out:
  894. src_idx = MV_XOR_NUM_SRC_TEST;
  895. while (src_idx--)
  896. __free_page(xor_srcs[src_idx]);
  897. __free_page(dest);
  898. return err;
  899. }
  900. /* This driver does not implement any of the optional DMA operations. */
  901. static int
  902. mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  903. unsigned long arg)
  904. {
  905. return -ENOSYS;
  906. }
  907. static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
  908. {
  909. struct dma_chan *chan, *_chan;
  910. struct device *dev = mv_chan->dmadev.dev;
  911. dma_async_device_unregister(&mv_chan->dmadev);
  912. dma_free_coherent(dev, MV_XOR_POOL_SIZE,
  913. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  914. list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
  915. device_node) {
  916. list_del(&chan->device_node);
  917. }
  918. free_irq(mv_chan->irq, mv_chan);
  919. return 0;
  920. }
  921. static struct mv_xor_chan *
  922. mv_xor_channel_add(struct mv_xor_device *xordev,
  923. struct platform_device *pdev,
  924. int idx, dma_cap_mask_t cap_mask, int irq)
  925. {
  926. int ret = 0;
  927. struct mv_xor_chan *mv_chan;
  928. struct dma_device *dma_dev;
  929. mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
  930. if (!mv_chan) {
  931. ret = -ENOMEM;
  932. goto err_free_dma;
  933. }
  934. mv_chan->idx = idx;
  935. mv_chan->irq = irq;
  936. dma_dev = &mv_chan->dmadev;
  937. /* allocate coherent memory for hardware descriptors
  938. * note: writecombine gives slightly better performance, but
  939. * requires that we explicitly flush the writes
  940. */
  941. mv_chan->dma_desc_pool_virt =
  942. dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
  943. &mv_chan->dma_desc_pool, GFP_KERNEL);
  944. if (!mv_chan->dma_desc_pool_virt)
  945. return ERR_PTR(-ENOMEM);
  946. /* discover transaction capabilites from the platform data */
  947. dma_dev->cap_mask = cap_mask;
  948. INIT_LIST_HEAD(&dma_dev->channels);
  949. /* set base routines */
  950. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  951. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  952. dma_dev->device_tx_status = mv_xor_status;
  953. dma_dev->device_issue_pending = mv_xor_issue_pending;
  954. dma_dev->device_control = mv_xor_control;
  955. dma_dev->dev = &pdev->dev;
  956. /* set prep routines based on capability */
  957. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  958. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  959. if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
  960. dma_dev->device_prep_dma_memset = mv_xor_prep_dma_memset;
  961. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  962. dma_dev->max_xor = 8;
  963. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  964. }
  965. mv_chan->mmr_base = xordev->xor_base;
  966. if (!mv_chan->mmr_base) {
  967. ret = -ENOMEM;
  968. goto err_free_dma;
  969. }
  970. tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
  971. mv_chan);
  972. /* clear errors before enabling interrupts */
  973. mv_xor_device_clear_err_status(mv_chan);
  974. ret = devm_request_irq(&pdev->dev, mv_chan->irq,
  975. mv_xor_interrupt_handler,
  976. 0, dev_name(&pdev->dev), mv_chan);
  977. if (ret)
  978. goto err_free_dma;
  979. mv_chan_unmask_interrupts(mv_chan);
  980. mv_set_mode(mv_chan, DMA_MEMCPY);
  981. spin_lock_init(&mv_chan->lock);
  982. INIT_LIST_HEAD(&mv_chan->chain);
  983. INIT_LIST_HEAD(&mv_chan->completed_slots);
  984. INIT_LIST_HEAD(&mv_chan->all_slots);
  985. mv_chan->dmachan.device = dma_dev;
  986. dma_cookie_init(&mv_chan->dmachan);
  987. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  988. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  989. ret = mv_xor_memcpy_self_test(mv_chan);
  990. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  991. if (ret)
  992. goto err_free_dma;
  993. }
  994. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  995. ret = mv_xor_xor_self_test(mv_chan);
  996. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  997. if (ret)
  998. goto err_free_dma;
  999. }
  1000. dev_info(&pdev->dev, "Marvell XOR: "
  1001. "( %s%s%s%s)\n",
  1002. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  1003. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
  1004. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  1005. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  1006. dma_async_device_register(dma_dev);
  1007. return mv_chan;
  1008. err_free_dma:
  1009. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  1010. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  1011. return ERR_PTR(ret);
  1012. }
  1013. static void
  1014. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  1015. const struct mbus_dram_target_info *dram)
  1016. {
  1017. void __iomem *base = xordev->xor_base;
  1018. u32 win_enable = 0;
  1019. int i;
  1020. for (i = 0; i < 8; i++) {
  1021. writel(0, base + WINDOW_BASE(i));
  1022. writel(0, base + WINDOW_SIZE(i));
  1023. if (i < 4)
  1024. writel(0, base + WINDOW_REMAP_HIGH(i));
  1025. }
  1026. for (i = 0; i < dram->num_cs; i++) {
  1027. const struct mbus_dram_window *cs = dram->cs + i;
  1028. writel((cs->base & 0xffff0000) |
  1029. (cs->mbus_attr << 8) |
  1030. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  1031. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  1032. win_enable |= (1 << i);
  1033. win_enable |= 3 << (16 + (2 * i));
  1034. }
  1035. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  1036. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  1037. }
  1038. static int __devinit mv_xor_probe(struct platform_device *pdev)
  1039. {
  1040. const struct mbus_dram_target_info *dram;
  1041. struct mv_xor_device *xordev;
  1042. struct mv_xor_platform_data *pdata = pdev->dev.platform_data;
  1043. struct resource *res;
  1044. int i, ret;
  1045. dev_notice(&pdev->dev, "Marvell XOR driver\n");
  1046. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  1047. if (!xordev)
  1048. return -ENOMEM;
  1049. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1050. if (!res)
  1051. return -ENODEV;
  1052. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  1053. resource_size(res));
  1054. if (!xordev->xor_base)
  1055. return -EBUSY;
  1056. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1057. if (!res)
  1058. return -ENODEV;
  1059. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  1060. resource_size(res));
  1061. if (!xordev->xor_high_base)
  1062. return -EBUSY;
  1063. platform_set_drvdata(pdev, xordev);
  1064. /*
  1065. * (Re-)program MBUS remapping windows if we are asked to.
  1066. */
  1067. dram = mv_mbus_dram_info();
  1068. if (dram)
  1069. mv_xor_conf_mbus_windows(xordev, dram);
  1070. /* Not all platforms can gate the clock, so it is not
  1071. * an error if the clock does not exists.
  1072. */
  1073. xordev->clk = clk_get(&pdev->dev, NULL);
  1074. if (!IS_ERR(xordev->clk))
  1075. clk_prepare_enable(xordev->clk);
  1076. if (pdev->dev.of_node) {
  1077. struct device_node *np;
  1078. int i = 0;
  1079. for_each_child_of_node(pdev->dev.of_node, np) {
  1080. dma_cap_mask_t cap_mask;
  1081. int irq;
  1082. dma_cap_zero(cap_mask);
  1083. if (of_property_read_bool(np, "dmacap,memcpy"))
  1084. dma_cap_set(DMA_MEMCPY, cap_mask);
  1085. if (of_property_read_bool(np, "dmacap,xor"))
  1086. dma_cap_set(DMA_XOR, cap_mask);
  1087. if (of_property_read_bool(np, "dmacap,memset"))
  1088. dma_cap_set(DMA_MEMSET, cap_mask);
  1089. if (of_property_read_bool(np, "dmacap,interrupt"))
  1090. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1091. irq = irq_of_parse_and_map(np, 0);
  1092. if (irq < 0) {
  1093. ret = irq;
  1094. goto err_channel_add;
  1095. }
  1096. xordev->channels[i] =
  1097. mv_xor_channel_add(xordev, pdev, i,
  1098. cap_mask, irq);
  1099. if (IS_ERR(xordev->channels[i])) {
  1100. ret = PTR_ERR(xordev->channels[i]);
  1101. irq_dispose_mapping(irq);
  1102. goto err_channel_add;
  1103. }
  1104. i++;
  1105. }
  1106. } else if (pdata && pdata->channels) {
  1107. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1108. struct mv_xor_channel_data *cd;
  1109. int irq;
  1110. cd = &pdata->channels[i];
  1111. if (!cd) {
  1112. ret = -ENODEV;
  1113. goto err_channel_add;
  1114. }
  1115. irq = platform_get_irq(pdev, i);
  1116. if (irq < 0) {
  1117. ret = irq;
  1118. goto err_channel_add;
  1119. }
  1120. xordev->channels[i] =
  1121. mv_xor_channel_add(xordev, pdev, i,
  1122. cd->cap_mask, irq);
  1123. if (IS_ERR(xordev->channels[i])) {
  1124. ret = PTR_ERR(xordev->channels[i]);
  1125. goto err_channel_add;
  1126. }
  1127. }
  1128. }
  1129. return 0;
  1130. err_channel_add:
  1131. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1132. if (xordev->channels[i]) {
  1133. if (pdev->dev.of_node)
  1134. irq_dispose_mapping(xordev->channels[i]->irq);
  1135. mv_xor_channel_remove(xordev->channels[i]);
  1136. }
  1137. clk_disable_unprepare(xordev->clk);
  1138. clk_put(xordev->clk);
  1139. return ret;
  1140. }
  1141. static int __devexit mv_xor_remove(struct platform_device *pdev)
  1142. {
  1143. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  1144. int i;
  1145. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1146. if (xordev->channels[i])
  1147. mv_xor_channel_remove(xordev->channels[i]);
  1148. }
  1149. if (!IS_ERR(xordev->clk)) {
  1150. clk_disable_unprepare(xordev->clk);
  1151. clk_put(xordev->clk);
  1152. }
  1153. return 0;
  1154. }
  1155. #ifdef CONFIG_OF
  1156. static struct of_device_id mv_xor_dt_ids[] __devinitdata = {
  1157. { .compatible = "marvell,orion-xor", },
  1158. {},
  1159. };
  1160. MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
  1161. #endif
  1162. static struct platform_driver mv_xor_driver = {
  1163. .probe = mv_xor_probe,
  1164. .remove = __devexit_p(mv_xor_remove),
  1165. .driver = {
  1166. .owner = THIS_MODULE,
  1167. .name = MV_XOR_NAME,
  1168. .of_match_table = of_match_ptr(mv_xor_dt_ids),
  1169. },
  1170. };
  1171. static int __init mv_xor_init(void)
  1172. {
  1173. return platform_driver_register(&mv_xor_driver);
  1174. }
  1175. module_init(mv_xor_init);
  1176. /* it's currently unsafe to unload this module */
  1177. #if 0
  1178. static void __exit mv_xor_exit(void)
  1179. {
  1180. platform_driver_unregister(&mv_xor_driver);
  1181. return;
  1182. }
  1183. module_exit(mv_xor_exit);
  1184. #endif
  1185. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1186. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1187. MODULE_LICENSE("GPL");