mv_xor.c 33 KB

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