mv_xor.c 33 KB

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