mv_xor.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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 : 0);
  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. ret = -ENOMEM;
  865. goto err_free_dma;
  866. }
  867. mv_chan->idx = idx;
  868. mv_chan->irq = irq;
  869. dma_dev = &mv_chan->dmadev;
  870. /* allocate coherent memory for hardware descriptors
  871. * note: writecombine gives slightly better performance, but
  872. * requires that we explicitly flush the writes
  873. */
  874. mv_chan->dma_desc_pool_virt =
  875. dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
  876. &mv_chan->dma_desc_pool, GFP_KERNEL);
  877. if (!mv_chan->dma_desc_pool_virt)
  878. return ERR_PTR(-ENOMEM);
  879. /* discover transaction capabilites from the platform data */
  880. dma_dev->cap_mask = cap_mask;
  881. INIT_LIST_HEAD(&dma_dev->channels);
  882. /* set base routines */
  883. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  884. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  885. dma_dev->device_tx_status = mv_xor_status;
  886. dma_dev->device_issue_pending = mv_xor_issue_pending;
  887. dma_dev->device_control = mv_xor_control;
  888. dma_dev->dev = &pdev->dev;
  889. /* set prep routines based on capability */
  890. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  891. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  892. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  893. dma_dev->max_xor = 8;
  894. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  895. }
  896. mv_chan->mmr_base = xordev->xor_base;
  897. if (!mv_chan->mmr_base) {
  898. ret = -ENOMEM;
  899. goto err_free_dma;
  900. }
  901. tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
  902. mv_chan);
  903. /* clear errors before enabling interrupts */
  904. mv_xor_device_clear_err_status(mv_chan);
  905. ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
  906. 0, dev_name(&pdev->dev), mv_chan);
  907. if (ret)
  908. goto err_free_dma;
  909. mv_chan_unmask_interrupts(mv_chan);
  910. mv_set_mode(mv_chan, DMA_MEMCPY);
  911. spin_lock_init(&mv_chan->lock);
  912. INIT_LIST_HEAD(&mv_chan->chain);
  913. INIT_LIST_HEAD(&mv_chan->completed_slots);
  914. INIT_LIST_HEAD(&mv_chan->all_slots);
  915. mv_chan->dmachan.device = dma_dev;
  916. dma_cookie_init(&mv_chan->dmachan);
  917. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  918. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  919. ret = mv_xor_memcpy_self_test(mv_chan);
  920. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  921. if (ret)
  922. goto err_free_irq;
  923. }
  924. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  925. ret = mv_xor_xor_self_test(mv_chan);
  926. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  927. if (ret)
  928. goto err_free_irq;
  929. }
  930. dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n",
  931. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  932. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  933. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  934. dma_async_device_register(dma_dev);
  935. return mv_chan;
  936. err_free_irq:
  937. free_irq(mv_chan->irq, mv_chan);
  938. err_free_dma:
  939. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  940. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  941. return ERR_PTR(ret);
  942. }
  943. static void
  944. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  945. const struct mbus_dram_target_info *dram)
  946. {
  947. void __iomem *base = xordev->xor_base;
  948. u32 win_enable = 0;
  949. int i;
  950. for (i = 0; i < 8; i++) {
  951. writel(0, base + WINDOW_BASE(i));
  952. writel(0, base + WINDOW_SIZE(i));
  953. if (i < 4)
  954. writel(0, base + WINDOW_REMAP_HIGH(i));
  955. }
  956. for (i = 0; i < dram->num_cs; i++) {
  957. const struct mbus_dram_window *cs = dram->cs + i;
  958. writel((cs->base & 0xffff0000) |
  959. (cs->mbus_attr << 8) |
  960. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  961. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  962. win_enable |= (1 << i);
  963. win_enable |= 3 << (16 + (2 * i));
  964. }
  965. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  966. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  967. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  968. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  969. }
  970. static int mv_xor_probe(struct platform_device *pdev)
  971. {
  972. const struct mbus_dram_target_info *dram;
  973. struct mv_xor_device *xordev;
  974. struct mv_xor_platform_data *pdata = pdev->dev.platform_data;
  975. struct resource *res;
  976. int i, ret;
  977. dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
  978. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  979. if (!xordev)
  980. return -ENOMEM;
  981. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  982. if (!res)
  983. return -ENODEV;
  984. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  985. resource_size(res));
  986. if (!xordev->xor_base)
  987. return -EBUSY;
  988. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  989. if (!res)
  990. return -ENODEV;
  991. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  992. resource_size(res));
  993. if (!xordev->xor_high_base)
  994. return -EBUSY;
  995. platform_set_drvdata(pdev, xordev);
  996. /*
  997. * (Re-)program MBUS remapping windows if we are asked to.
  998. */
  999. dram = mv_mbus_dram_info();
  1000. if (dram)
  1001. mv_xor_conf_mbus_windows(xordev, dram);
  1002. /* Not all platforms can gate the clock, so it is not
  1003. * an error if the clock does not exists.
  1004. */
  1005. xordev->clk = clk_get(&pdev->dev, NULL);
  1006. if (!IS_ERR(xordev->clk))
  1007. clk_prepare_enable(xordev->clk);
  1008. if (pdev->dev.of_node) {
  1009. struct device_node *np;
  1010. int i = 0;
  1011. for_each_child_of_node(pdev->dev.of_node, np) {
  1012. dma_cap_mask_t cap_mask;
  1013. int irq;
  1014. dma_cap_zero(cap_mask);
  1015. if (of_property_read_bool(np, "dmacap,memcpy"))
  1016. dma_cap_set(DMA_MEMCPY, cap_mask);
  1017. if (of_property_read_bool(np, "dmacap,xor"))
  1018. dma_cap_set(DMA_XOR, cap_mask);
  1019. if (of_property_read_bool(np, "dmacap,interrupt"))
  1020. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1021. irq = irq_of_parse_and_map(np, 0);
  1022. if (!irq) {
  1023. ret = -ENODEV;
  1024. goto err_channel_add;
  1025. }
  1026. xordev->channels[i] =
  1027. mv_xor_channel_add(xordev, pdev, i,
  1028. cap_mask, irq);
  1029. if (IS_ERR(xordev->channels[i])) {
  1030. ret = PTR_ERR(xordev->channels[i]);
  1031. xordev->channels[i] = NULL;
  1032. irq_dispose_mapping(irq);
  1033. goto err_channel_add;
  1034. }
  1035. i++;
  1036. }
  1037. } else if (pdata && pdata->channels) {
  1038. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1039. struct mv_xor_channel_data *cd;
  1040. int irq;
  1041. cd = &pdata->channels[i];
  1042. if (!cd) {
  1043. ret = -ENODEV;
  1044. goto err_channel_add;
  1045. }
  1046. irq = platform_get_irq(pdev, i);
  1047. if (irq < 0) {
  1048. ret = irq;
  1049. goto err_channel_add;
  1050. }
  1051. xordev->channels[i] =
  1052. mv_xor_channel_add(xordev, pdev, i,
  1053. cd->cap_mask, irq);
  1054. if (IS_ERR(xordev->channels[i])) {
  1055. ret = PTR_ERR(xordev->channels[i]);
  1056. goto err_channel_add;
  1057. }
  1058. }
  1059. }
  1060. return 0;
  1061. err_channel_add:
  1062. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1063. if (xordev->channels[i]) {
  1064. mv_xor_channel_remove(xordev->channels[i]);
  1065. if (pdev->dev.of_node)
  1066. irq_dispose_mapping(xordev->channels[i]->irq);
  1067. }
  1068. if (!IS_ERR(xordev->clk)) {
  1069. clk_disable_unprepare(xordev->clk);
  1070. clk_put(xordev->clk);
  1071. }
  1072. return ret;
  1073. }
  1074. static int mv_xor_remove(struct platform_device *pdev)
  1075. {
  1076. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  1077. int i;
  1078. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1079. if (xordev->channels[i])
  1080. mv_xor_channel_remove(xordev->channels[i]);
  1081. }
  1082. if (!IS_ERR(xordev->clk)) {
  1083. clk_disable_unprepare(xordev->clk);
  1084. clk_put(xordev->clk);
  1085. }
  1086. return 0;
  1087. }
  1088. #ifdef CONFIG_OF
  1089. static struct of_device_id mv_xor_dt_ids[] = {
  1090. { .compatible = "marvell,orion-xor", },
  1091. {},
  1092. };
  1093. MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
  1094. #endif
  1095. static struct platform_driver mv_xor_driver = {
  1096. .probe = mv_xor_probe,
  1097. .remove = mv_xor_remove,
  1098. .driver = {
  1099. .owner = THIS_MODULE,
  1100. .name = MV_XOR_NAME,
  1101. .of_match_table = of_match_ptr(mv_xor_dt_ids),
  1102. },
  1103. };
  1104. static int __init mv_xor_init(void)
  1105. {
  1106. return platform_driver_register(&mv_xor_driver);
  1107. }
  1108. module_init(mv_xor_init);
  1109. /* it's currently unsafe to unload this module */
  1110. #if 0
  1111. static void __exit mv_xor_exit(void)
  1112. {
  1113. platform_driver_unregister(&mv_xor_driver);
  1114. return;
  1115. }
  1116. module_exit(mv_xor_exit);
  1117. #endif
  1118. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1119. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1120. MODULE_LICENSE("GPL");