mv_xor.c 32 KB

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