mv_xor.c 33 KB

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