mv_xor.c 34 KB

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