mv_xor.c 33 KB

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