mv_xor.c 35 KB

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