davinci_cpdma.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Texas Instruments CPDMA Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/genalloc.h>
  25. #include "davinci_cpdma.h"
  26. /* DMA Registers */
  27. #define CPDMA_TXIDVER 0x00
  28. #define CPDMA_TXCONTROL 0x04
  29. #define CPDMA_TXTEARDOWN 0x08
  30. #define CPDMA_RXIDVER 0x10
  31. #define CPDMA_RXCONTROL 0x14
  32. #define CPDMA_SOFTRESET 0x1c
  33. #define CPDMA_RXTEARDOWN 0x18
  34. #define CPDMA_TX_PRI0_RATE 0x30
  35. #define CPDMA_TXINTSTATRAW 0x80
  36. #define CPDMA_TXINTSTATMASKED 0x84
  37. #define CPDMA_TXINTMASKSET 0x88
  38. #define CPDMA_TXINTMASKCLEAR 0x8c
  39. #define CPDMA_MACINVECTOR 0x90
  40. #define CPDMA_MACEOIVECTOR 0x94
  41. #define CPDMA_RXINTSTATRAW 0xa0
  42. #define CPDMA_RXINTSTATMASKED 0xa4
  43. #define CPDMA_RXINTMASKSET 0xa8
  44. #define CPDMA_RXINTMASKCLEAR 0xac
  45. #define CPDMA_DMAINTSTATRAW 0xb0
  46. #define CPDMA_DMAINTSTATMASKED 0xb4
  47. #define CPDMA_DMAINTMASKSET 0xb8
  48. #define CPDMA_DMAINTMASKCLEAR 0xbc
  49. #define CPDMA_DMAINT_HOSTERR BIT(1)
  50. /* the following exist only if has_ext_regs is set */
  51. #define CPDMA_DMACONTROL 0x20
  52. #define CPDMA_DMASTATUS 0x24
  53. #define CPDMA_RXBUFFOFS 0x28
  54. #define CPDMA_EM_CONTROL 0x2c
  55. /* Descriptor mode bits */
  56. #define CPDMA_DESC_SOP BIT(31)
  57. #define CPDMA_DESC_EOP BIT(30)
  58. #define CPDMA_DESC_OWNER BIT(29)
  59. #define CPDMA_DESC_EOQ BIT(28)
  60. #define CPDMA_DESC_TD_COMPLETE BIT(27)
  61. #define CPDMA_DESC_PASS_CRC BIT(26)
  62. #define CPDMA_DESC_TO_PORT_EN BIT(20)
  63. #define CPDMA_TO_PORT_SHIFT 16
  64. #define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
  65. #define CPDMA_DESC_CRC_LEN 4
  66. #define CPDMA_TEARDOWN_VALUE 0xfffffffc
  67. #define CPDMA_MAX_RLIM_CNT 16384
  68. struct cpdma_desc {
  69. /* hardware fields */
  70. u32 hw_next;
  71. u32 hw_buffer;
  72. u32 hw_len;
  73. u32 hw_mode;
  74. /* software fields */
  75. void *sw_token;
  76. u32 sw_buffer;
  77. u32 sw_len;
  78. };
  79. struct cpdma_desc_pool {
  80. phys_addr_t phys;
  81. dma_addr_t hw_addr;
  82. void __iomem *iomap; /* ioremap map */
  83. void *cpumap; /* dma_alloc map */
  84. int desc_size, mem_size;
  85. int num_desc;
  86. struct device *dev;
  87. struct gen_pool *gen_pool;
  88. };
  89. enum cpdma_state {
  90. CPDMA_STATE_IDLE,
  91. CPDMA_STATE_ACTIVE,
  92. CPDMA_STATE_TEARDOWN,
  93. };
  94. struct cpdma_ctlr {
  95. enum cpdma_state state;
  96. struct cpdma_params params;
  97. struct device *dev;
  98. struct cpdma_desc_pool *pool;
  99. spinlock_t lock;
  100. struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
  101. int chan_num;
  102. int num_rx_desc; /* RX descriptors number */
  103. int num_tx_desc; /* TX descriptors number */
  104. };
  105. struct cpdma_chan {
  106. struct cpdma_desc __iomem *head, *tail;
  107. void __iomem *hdp, *cp, *rxfree;
  108. enum cpdma_state state;
  109. struct cpdma_ctlr *ctlr;
  110. int chan_num;
  111. spinlock_t lock;
  112. int count;
  113. u32 desc_num;
  114. u32 mask;
  115. cpdma_handler_fn handler;
  116. enum dma_data_direction dir;
  117. struct cpdma_chan_stats stats;
  118. /* offsets into dmaregs */
  119. int int_set, int_clear, td;
  120. int weight;
  121. u32 rate_factor;
  122. u32 rate;
  123. };
  124. struct cpdma_control_info {
  125. u32 reg;
  126. u32 shift, mask;
  127. int access;
  128. #define ACCESS_RO BIT(0)
  129. #define ACCESS_WO BIT(1)
  130. #define ACCESS_RW (ACCESS_RO | ACCESS_WO)
  131. };
  132. static struct cpdma_control_info controls[] = {
  133. [CPDMA_TX_RLIM] = {CPDMA_DMACONTROL, 8, 0xffff, ACCESS_RW},
  134. [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
  135. [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
  136. [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
  137. [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
  138. [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
  139. [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
  140. [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
  141. [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
  142. [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
  143. [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
  144. [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
  145. };
  146. #define tx_chan_num(chan) (chan)
  147. #define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS)
  148. #define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS)
  149. #define is_tx_chan(chan) (!is_rx_chan(chan))
  150. #define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1))
  151. #define chan_linear(chan) __chan_linear((chan)->chan_num)
  152. /* The following make access to common cpdma_ctlr params more readable */
  153. #define dmaregs params.dmaregs
  154. #define num_chan params.num_chan
  155. /* various accessors */
  156. #define dma_reg_read(ctlr, ofs) readl((ctlr)->dmaregs + (ofs))
  157. #define chan_read(chan, fld) readl((chan)->fld)
  158. #define desc_read(desc, fld) readl(&(desc)->fld)
  159. #define dma_reg_write(ctlr, ofs, v) writel(v, (ctlr)->dmaregs + (ofs))
  160. #define chan_write(chan, fld, v) writel(v, (chan)->fld)
  161. #define desc_write(desc, fld, v) writel((u32)(v), &(desc)->fld)
  162. #define cpdma_desc_to_port(chan, mode, directed) \
  163. do { \
  164. if (!is_rx_chan(chan) && ((directed == 1) || \
  165. (directed == 2))) \
  166. mode |= (CPDMA_DESC_TO_PORT_EN | \
  167. (directed << CPDMA_TO_PORT_SHIFT)); \
  168. } while (0)
  169. static void cpdma_desc_pool_destroy(struct cpdma_ctlr *ctlr)
  170. {
  171. struct cpdma_desc_pool *pool = ctlr->pool;
  172. if (!pool)
  173. return;
  174. WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
  175. "cpdma_desc_pool size %zd != avail %zd",
  176. gen_pool_size(pool->gen_pool),
  177. gen_pool_avail(pool->gen_pool));
  178. if (pool->cpumap)
  179. dma_free_coherent(ctlr->dev, pool->mem_size, pool->cpumap,
  180. pool->phys);
  181. }
  182. /*
  183. * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
  184. * emac) have dedicated on-chip memory for these descriptors. Some other
  185. * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
  186. * abstract out these details
  187. */
  188. int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr)
  189. {
  190. struct cpdma_params *cpdma_params = &ctlr->params;
  191. struct cpdma_desc_pool *pool;
  192. int ret = -ENOMEM;
  193. pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL);
  194. if (!pool)
  195. goto gen_pool_create_fail;
  196. ctlr->pool = pool;
  197. pool->mem_size = cpdma_params->desc_mem_size;
  198. pool->desc_size = ALIGN(sizeof(struct cpdma_desc),
  199. cpdma_params->desc_align);
  200. pool->num_desc = pool->mem_size / pool->desc_size;
  201. if (cpdma_params->descs_pool_size) {
  202. /* recalculate memory size required cpdma descriptor pool
  203. * basing on number of descriptors specified by user and
  204. * if memory size > CPPI internal RAM size (desc_mem_size)
  205. * then switch to use DDR
  206. */
  207. pool->num_desc = cpdma_params->descs_pool_size;
  208. pool->mem_size = pool->desc_size * pool->num_desc;
  209. if (pool->mem_size > cpdma_params->desc_mem_size)
  210. cpdma_params->desc_mem_phys = 0;
  211. }
  212. pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size),
  213. -1, "cpdma");
  214. if (IS_ERR(pool->gen_pool)) {
  215. ret = PTR_ERR(pool->gen_pool);
  216. dev_err(ctlr->dev, "pool create failed %d\n", ret);
  217. goto gen_pool_create_fail;
  218. }
  219. if (cpdma_params->desc_mem_phys) {
  220. pool->phys = cpdma_params->desc_mem_phys;
  221. pool->iomap = devm_ioremap(ctlr->dev, pool->phys,
  222. pool->mem_size);
  223. pool->hw_addr = cpdma_params->desc_hw_addr;
  224. } else {
  225. pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size,
  226. &pool->hw_addr, GFP_KERNEL);
  227. pool->iomap = (void __iomem __force *)pool->cpumap;
  228. pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
  229. }
  230. if (!pool->iomap)
  231. goto gen_pool_create_fail;
  232. ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
  233. pool->phys, pool->mem_size, -1);
  234. if (ret < 0) {
  235. dev_err(ctlr->dev, "pool add failed %d\n", ret);
  236. goto gen_pool_add_virt_fail;
  237. }
  238. return 0;
  239. gen_pool_add_virt_fail:
  240. cpdma_desc_pool_destroy(ctlr);
  241. gen_pool_create_fail:
  242. ctlr->pool = NULL;
  243. return ret;
  244. }
  245. static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
  246. struct cpdma_desc __iomem *desc)
  247. {
  248. if (!desc)
  249. return 0;
  250. return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
  251. }
  252. static inline struct cpdma_desc __iomem *
  253. desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
  254. {
  255. return dma ? pool->iomap + dma - pool->hw_addr : NULL;
  256. }
  257. static struct cpdma_desc __iomem *
  258. cpdma_desc_alloc(struct cpdma_desc_pool *pool)
  259. {
  260. return (struct cpdma_desc __iomem *)
  261. gen_pool_alloc(pool->gen_pool, pool->desc_size);
  262. }
  263. static void cpdma_desc_free(struct cpdma_desc_pool *pool,
  264. struct cpdma_desc __iomem *desc, int num_desc)
  265. {
  266. gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
  267. }
  268. static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  269. {
  270. struct cpdma_control_info *info = &controls[control];
  271. u32 val;
  272. if (!ctlr->params.has_ext_regs)
  273. return -ENOTSUPP;
  274. if (ctlr->state != CPDMA_STATE_ACTIVE)
  275. return -EINVAL;
  276. if (control < 0 || control >= ARRAY_SIZE(controls))
  277. return -ENOENT;
  278. if ((info->access & ACCESS_WO) != ACCESS_WO)
  279. return -EPERM;
  280. val = dma_reg_read(ctlr, info->reg);
  281. val &= ~(info->mask << info->shift);
  282. val |= (value & info->mask) << info->shift;
  283. dma_reg_write(ctlr, info->reg, val);
  284. return 0;
  285. }
  286. static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  287. {
  288. struct cpdma_control_info *info = &controls[control];
  289. int ret;
  290. if (!ctlr->params.has_ext_regs)
  291. return -ENOTSUPP;
  292. if (ctlr->state != CPDMA_STATE_ACTIVE)
  293. return -EINVAL;
  294. if (control < 0 || control >= ARRAY_SIZE(controls))
  295. return -ENOENT;
  296. if ((info->access & ACCESS_RO) != ACCESS_RO)
  297. return -EPERM;
  298. ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
  299. return ret;
  300. }
  301. /* cpdma_chan_set_chan_shaper - set shaper for a channel
  302. * Has to be called under ctlr lock
  303. */
  304. static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
  305. {
  306. struct cpdma_ctlr *ctlr = chan->ctlr;
  307. u32 rate_reg;
  308. u32 rmask;
  309. int ret;
  310. if (!chan->rate)
  311. return 0;
  312. rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
  313. dma_reg_write(ctlr, rate_reg, chan->rate_factor);
  314. rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
  315. rmask |= chan->mask;
  316. ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
  317. return ret;
  318. }
  319. static int cpdma_chan_on(struct cpdma_chan *chan)
  320. {
  321. struct cpdma_ctlr *ctlr = chan->ctlr;
  322. struct cpdma_desc_pool *pool = ctlr->pool;
  323. unsigned long flags;
  324. spin_lock_irqsave(&chan->lock, flags);
  325. if (chan->state != CPDMA_STATE_IDLE) {
  326. spin_unlock_irqrestore(&chan->lock, flags);
  327. return -EBUSY;
  328. }
  329. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  330. spin_unlock_irqrestore(&chan->lock, flags);
  331. return -EINVAL;
  332. }
  333. dma_reg_write(ctlr, chan->int_set, chan->mask);
  334. chan->state = CPDMA_STATE_ACTIVE;
  335. if (chan->head) {
  336. chan_write(chan, hdp, desc_phys(pool, chan->head));
  337. if (chan->rxfree)
  338. chan_write(chan, rxfree, chan->count);
  339. }
  340. spin_unlock_irqrestore(&chan->lock, flags);
  341. return 0;
  342. }
  343. /* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
  344. * rmask - mask of rate limited channels
  345. * Returns min rate in Kb/s
  346. */
  347. static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
  348. u32 *rmask, int *prio_mode)
  349. {
  350. struct cpdma_ctlr *ctlr = ch->ctlr;
  351. struct cpdma_chan *chan;
  352. u32 old_rate = ch->rate;
  353. u32 new_rmask = 0;
  354. int rlim = 1;
  355. int i;
  356. *prio_mode = 0;
  357. for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
  358. chan = ctlr->channels[i];
  359. if (!chan) {
  360. rlim = 0;
  361. continue;
  362. }
  363. if (chan == ch)
  364. chan->rate = rate;
  365. if (chan->rate) {
  366. if (rlim) {
  367. new_rmask |= chan->mask;
  368. } else {
  369. ch->rate = old_rate;
  370. dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n",
  371. chan->chan_num);
  372. return -EINVAL;
  373. }
  374. } else {
  375. *prio_mode = 1;
  376. rlim = 0;
  377. }
  378. }
  379. *rmask = new_rmask;
  380. return 0;
  381. }
  382. static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
  383. struct cpdma_chan *ch)
  384. {
  385. u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
  386. u32 best_send_cnt = 0, best_idle_cnt = 0;
  387. u32 new_rate, best_rate = 0, rate_reg;
  388. u64 send_cnt, idle_cnt;
  389. u32 min_send_cnt, freq;
  390. u64 divident, divisor;
  391. if (!ch->rate) {
  392. ch->rate_factor = 0;
  393. goto set_factor;
  394. }
  395. freq = ctlr->params.bus_freq_mhz * 1000 * 32;
  396. if (!freq) {
  397. dev_err(ctlr->dev, "The bus frequency is not set\n");
  398. return -EINVAL;
  399. }
  400. min_send_cnt = freq - ch->rate;
  401. send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
  402. while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
  403. divident = ch->rate * send_cnt;
  404. divisor = min_send_cnt;
  405. idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  406. divident = freq * idle_cnt;
  407. divisor = idle_cnt + send_cnt;
  408. new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  409. delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
  410. if (delta < best_delta) {
  411. best_delta = delta;
  412. best_send_cnt = send_cnt;
  413. best_idle_cnt = idle_cnt;
  414. best_rate = new_rate;
  415. if (!delta)
  416. break;
  417. }
  418. if (prev_delta >= delta) {
  419. prev_delta = delta;
  420. send_cnt++;
  421. continue;
  422. }
  423. idle_cnt++;
  424. divident = freq * idle_cnt;
  425. send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
  426. send_cnt -= idle_cnt;
  427. prev_delta = UINT_MAX;
  428. }
  429. ch->rate = best_rate;
  430. ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
  431. set_factor:
  432. rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
  433. dma_reg_write(ctlr, rate_reg, ch->rate_factor);
  434. return 0;
  435. }
  436. struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
  437. {
  438. struct cpdma_ctlr *ctlr;
  439. ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
  440. if (!ctlr)
  441. return NULL;
  442. ctlr->state = CPDMA_STATE_IDLE;
  443. ctlr->params = *params;
  444. ctlr->dev = params->dev;
  445. ctlr->chan_num = 0;
  446. spin_lock_init(&ctlr->lock);
  447. if (cpdma_desc_pool_create(ctlr))
  448. return NULL;
  449. /* split pool equally between RX/TX by default */
  450. ctlr->num_tx_desc = ctlr->pool->num_desc / 2;
  451. ctlr->num_rx_desc = ctlr->pool->num_desc - ctlr->num_tx_desc;
  452. if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
  453. ctlr->num_chan = CPDMA_MAX_CHANNELS;
  454. return ctlr;
  455. }
  456. EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
  457. int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
  458. {
  459. struct cpdma_chan *chan;
  460. unsigned long flags;
  461. int i, prio_mode;
  462. spin_lock_irqsave(&ctlr->lock, flags);
  463. if (ctlr->state != CPDMA_STATE_IDLE) {
  464. spin_unlock_irqrestore(&ctlr->lock, flags);
  465. return -EBUSY;
  466. }
  467. if (ctlr->params.has_soft_reset) {
  468. unsigned timeout = 10 * 100;
  469. dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
  470. while (timeout) {
  471. if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
  472. break;
  473. udelay(10);
  474. timeout--;
  475. }
  476. WARN_ON(!timeout);
  477. }
  478. for (i = 0; i < ctlr->num_chan; i++) {
  479. writel(0, ctlr->params.txhdp + 4 * i);
  480. writel(0, ctlr->params.rxhdp + 4 * i);
  481. writel(0, ctlr->params.txcp + 4 * i);
  482. writel(0, ctlr->params.rxcp + 4 * i);
  483. }
  484. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  485. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  486. dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
  487. dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
  488. ctlr->state = CPDMA_STATE_ACTIVE;
  489. prio_mode = 0;
  490. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  491. chan = ctlr->channels[i];
  492. if (chan) {
  493. cpdma_chan_set_chan_shaper(chan);
  494. cpdma_chan_on(chan);
  495. /* off prio mode if all tx channels are rate limited */
  496. if (is_tx_chan(chan) && !chan->rate)
  497. prio_mode = 1;
  498. }
  499. }
  500. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  501. _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
  502. spin_unlock_irqrestore(&ctlr->lock, flags);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
  506. int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
  507. {
  508. unsigned long flags;
  509. int i;
  510. spin_lock_irqsave(&ctlr->lock, flags);
  511. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  512. spin_unlock_irqrestore(&ctlr->lock, flags);
  513. return -EINVAL;
  514. }
  515. ctlr->state = CPDMA_STATE_TEARDOWN;
  516. spin_unlock_irqrestore(&ctlr->lock, flags);
  517. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  518. if (ctlr->channels[i])
  519. cpdma_chan_stop(ctlr->channels[i]);
  520. }
  521. spin_lock_irqsave(&ctlr->lock, flags);
  522. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  523. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  524. dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
  525. dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
  526. ctlr->state = CPDMA_STATE_IDLE;
  527. spin_unlock_irqrestore(&ctlr->lock, flags);
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
  531. int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
  532. {
  533. int ret = 0, i;
  534. if (!ctlr)
  535. return -EINVAL;
  536. if (ctlr->state != CPDMA_STATE_IDLE)
  537. cpdma_ctlr_stop(ctlr);
  538. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
  539. cpdma_chan_destroy(ctlr->channels[i]);
  540. cpdma_desc_pool_destroy(ctlr);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
  544. int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
  545. {
  546. unsigned long flags;
  547. int i;
  548. spin_lock_irqsave(&ctlr->lock, flags);
  549. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  550. spin_unlock_irqrestore(&ctlr->lock, flags);
  551. return -EINVAL;
  552. }
  553. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  554. if (ctlr->channels[i])
  555. cpdma_chan_int_ctrl(ctlr->channels[i], enable);
  556. }
  557. spin_unlock_irqrestore(&ctlr->lock, flags);
  558. return 0;
  559. }
  560. EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
  561. void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
  562. {
  563. dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
  564. }
  565. EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
  566. u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
  567. {
  568. return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
  569. }
  570. EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
  571. u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
  572. {
  573. return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
  574. }
  575. EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
  576. static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
  577. int rx, int desc_num,
  578. int per_ch_desc)
  579. {
  580. struct cpdma_chan *chan, *most_chan = NULL;
  581. int desc_cnt = desc_num;
  582. int most_dnum = 0;
  583. int min, max, i;
  584. if (!desc_num)
  585. return;
  586. if (rx) {
  587. min = rx_chan_num(0);
  588. max = rx_chan_num(CPDMA_MAX_CHANNELS);
  589. } else {
  590. min = tx_chan_num(0);
  591. max = tx_chan_num(CPDMA_MAX_CHANNELS);
  592. }
  593. for (i = min; i < max; i++) {
  594. chan = ctlr->channels[i];
  595. if (!chan)
  596. continue;
  597. if (chan->weight)
  598. chan->desc_num = (chan->weight * desc_num) / 100;
  599. else
  600. chan->desc_num = per_ch_desc;
  601. desc_cnt -= chan->desc_num;
  602. if (most_dnum < chan->desc_num) {
  603. most_dnum = chan->desc_num;
  604. most_chan = chan;
  605. }
  606. }
  607. /* use remains */
  608. if (most_chan)
  609. most_chan->desc_num += desc_cnt;
  610. }
  611. /**
  612. * cpdma_chan_split_pool - Splits ctrl pool between all channels.
  613. * Has to be called under ctlr lock
  614. */
  615. int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
  616. {
  617. int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
  618. int free_rx_num = 0, free_tx_num = 0;
  619. int rx_weight = 0, tx_weight = 0;
  620. int tx_desc_num, rx_desc_num;
  621. struct cpdma_chan *chan;
  622. int i;
  623. if (!ctlr->chan_num)
  624. return 0;
  625. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  626. chan = ctlr->channels[i];
  627. if (!chan)
  628. continue;
  629. if (is_rx_chan(chan)) {
  630. if (!chan->weight)
  631. free_rx_num++;
  632. rx_weight += chan->weight;
  633. } else {
  634. if (!chan->weight)
  635. free_tx_num++;
  636. tx_weight += chan->weight;
  637. }
  638. }
  639. if (rx_weight > 100 || tx_weight > 100)
  640. return -EINVAL;
  641. tx_desc_num = ctlr->num_tx_desc;
  642. rx_desc_num = ctlr->num_rx_desc;
  643. if (free_tx_num) {
  644. tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
  645. tx_per_ch_desc /= free_tx_num;
  646. }
  647. if (free_rx_num) {
  648. rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
  649. rx_per_ch_desc /= free_rx_num;
  650. }
  651. cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
  652. cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(cpdma_chan_split_pool);
  656. /* cpdma_chan_set_weight - set weight of a channel in percentage.
  657. * Tx and Rx channels have separate weights. That is 100% for RX
  658. * and 100% for Tx. The weight is used to split cpdma resources
  659. * in correct proportion required by the channels, including number
  660. * of descriptors. The channel rate is not enough to know the
  661. * weight of a channel as the maximum rate of an interface is needed.
  662. * If weight = 0, then channel uses rest of descriptors leaved by
  663. * weighted channels.
  664. */
  665. int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
  666. {
  667. struct cpdma_ctlr *ctlr = ch->ctlr;
  668. unsigned long flags, ch_flags;
  669. int ret;
  670. spin_lock_irqsave(&ctlr->lock, flags);
  671. spin_lock_irqsave(&ch->lock, ch_flags);
  672. if (ch->weight == weight) {
  673. spin_unlock_irqrestore(&ch->lock, ch_flags);
  674. spin_unlock_irqrestore(&ctlr->lock, flags);
  675. return 0;
  676. }
  677. ch->weight = weight;
  678. spin_unlock_irqrestore(&ch->lock, ch_flags);
  679. /* re-split pool using new channel weight */
  680. ret = cpdma_chan_split_pool(ctlr);
  681. spin_unlock_irqrestore(&ctlr->lock, flags);
  682. return ret;
  683. }
  684. EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
  685. /* cpdma_chan_get_min_rate - get minimum allowed rate for channel
  686. * Should be called before cpdma_chan_set_rate.
  687. * Returns min rate in Kb/s
  688. */
  689. u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
  690. {
  691. unsigned int divident, divisor;
  692. divident = ctlr->params.bus_freq_mhz * 32 * 1000;
  693. divisor = 1 + CPDMA_MAX_RLIM_CNT;
  694. return DIV_ROUND_UP(divident, divisor);
  695. }
  696. EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
  697. /* cpdma_chan_set_rate - limits bandwidth for transmit channel.
  698. * The bandwidth * limited channels have to be in order beginning from lowest.
  699. * ch - transmit channel the bandwidth is configured for
  700. * rate - bandwidth in Kb/s, if 0 - then off shaper
  701. */
  702. int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
  703. {
  704. unsigned long flags, ch_flags;
  705. struct cpdma_ctlr *ctlr;
  706. int ret, prio_mode;
  707. u32 rmask;
  708. if (!ch || !is_tx_chan(ch))
  709. return -EINVAL;
  710. if (ch->rate == rate)
  711. return rate;
  712. ctlr = ch->ctlr;
  713. spin_lock_irqsave(&ctlr->lock, flags);
  714. spin_lock_irqsave(&ch->lock, ch_flags);
  715. ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
  716. if (ret)
  717. goto err;
  718. ret = cpdma_chan_set_factors(ctlr, ch);
  719. if (ret)
  720. goto err;
  721. spin_unlock_irqrestore(&ch->lock, ch_flags);
  722. /* on shapers */
  723. _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
  724. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  725. spin_unlock_irqrestore(&ctlr->lock, flags);
  726. return ret;
  727. err:
  728. spin_unlock_irqrestore(&ch->lock, ch_flags);
  729. spin_unlock_irqrestore(&ctlr->lock, flags);
  730. return ret;
  731. }
  732. EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
  733. u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
  734. {
  735. unsigned long flags;
  736. u32 rate;
  737. spin_lock_irqsave(&ch->lock, flags);
  738. rate = ch->rate;
  739. spin_unlock_irqrestore(&ch->lock, flags);
  740. return rate;
  741. }
  742. EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
  743. struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
  744. cpdma_handler_fn handler, int rx_type)
  745. {
  746. int offset = chan_num * 4;
  747. struct cpdma_chan *chan;
  748. unsigned long flags;
  749. chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
  750. if (__chan_linear(chan_num) >= ctlr->num_chan)
  751. return ERR_PTR(-EINVAL);
  752. chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
  753. if (!chan)
  754. return ERR_PTR(-ENOMEM);
  755. spin_lock_irqsave(&ctlr->lock, flags);
  756. if (ctlr->channels[chan_num]) {
  757. spin_unlock_irqrestore(&ctlr->lock, flags);
  758. devm_kfree(ctlr->dev, chan);
  759. return ERR_PTR(-EBUSY);
  760. }
  761. chan->ctlr = ctlr;
  762. chan->state = CPDMA_STATE_IDLE;
  763. chan->chan_num = chan_num;
  764. chan->handler = handler;
  765. chan->rate = 0;
  766. chan->weight = 0;
  767. if (is_rx_chan(chan)) {
  768. chan->hdp = ctlr->params.rxhdp + offset;
  769. chan->cp = ctlr->params.rxcp + offset;
  770. chan->rxfree = ctlr->params.rxfree + offset;
  771. chan->int_set = CPDMA_RXINTMASKSET;
  772. chan->int_clear = CPDMA_RXINTMASKCLEAR;
  773. chan->td = CPDMA_RXTEARDOWN;
  774. chan->dir = DMA_FROM_DEVICE;
  775. } else {
  776. chan->hdp = ctlr->params.txhdp + offset;
  777. chan->cp = ctlr->params.txcp + offset;
  778. chan->int_set = CPDMA_TXINTMASKSET;
  779. chan->int_clear = CPDMA_TXINTMASKCLEAR;
  780. chan->td = CPDMA_TXTEARDOWN;
  781. chan->dir = DMA_TO_DEVICE;
  782. }
  783. chan->mask = BIT(chan_linear(chan));
  784. spin_lock_init(&chan->lock);
  785. ctlr->channels[chan_num] = chan;
  786. ctlr->chan_num++;
  787. cpdma_chan_split_pool(ctlr);
  788. spin_unlock_irqrestore(&ctlr->lock, flags);
  789. return chan;
  790. }
  791. EXPORT_SYMBOL_GPL(cpdma_chan_create);
  792. int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
  793. {
  794. unsigned long flags;
  795. int desc_num;
  796. spin_lock_irqsave(&chan->lock, flags);
  797. desc_num = chan->desc_num;
  798. spin_unlock_irqrestore(&chan->lock, flags);
  799. return desc_num;
  800. }
  801. EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
  802. int cpdma_chan_destroy(struct cpdma_chan *chan)
  803. {
  804. struct cpdma_ctlr *ctlr;
  805. unsigned long flags;
  806. if (!chan)
  807. return -EINVAL;
  808. ctlr = chan->ctlr;
  809. spin_lock_irqsave(&ctlr->lock, flags);
  810. if (chan->state != CPDMA_STATE_IDLE)
  811. cpdma_chan_stop(chan);
  812. ctlr->channels[chan->chan_num] = NULL;
  813. ctlr->chan_num--;
  814. devm_kfree(ctlr->dev, chan);
  815. cpdma_chan_split_pool(ctlr);
  816. spin_unlock_irqrestore(&ctlr->lock, flags);
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
  820. int cpdma_chan_get_stats(struct cpdma_chan *chan,
  821. struct cpdma_chan_stats *stats)
  822. {
  823. unsigned long flags;
  824. if (!chan)
  825. return -EINVAL;
  826. spin_lock_irqsave(&chan->lock, flags);
  827. memcpy(stats, &chan->stats, sizeof(*stats));
  828. spin_unlock_irqrestore(&chan->lock, flags);
  829. return 0;
  830. }
  831. EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
  832. static void __cpdma_chan_submit(struct cpdma_chan *chan,
  833. struct cpdma_desc __iomem *desc)
  834. {
  835. struct cpdma_ctlr *ctlr = chan->ctlr;
  836. struct cpdma_desc __iomem *prev = chan->tail;
  837. struct cpdma_desc_pool *pool = ctlr->pool;
  838. dma_addr_t desc_dma;
  839. u32 mode;
  840. desc_dma = desc_phys(pool, desc);
  841. /* simple case - idle channel */
  842. if (!chan->head) {
  843. chan->stats.head_enqueue++;
  844. chan->head = desc;
  845. chan->tail = desc;
  846. if (chan->state == CPDMA_STATE_ACTIVE)
  847. chan_write(chan, hdp, desc_dma);
  848. return;
  849. }
  850. /* first chain the descriptor at the tail of the list */
  851. desc_write(prev, hw_next, desc_dma);
  852. chan->tail = desc;
  853. chan->stats.tail_enqueue++;
  854. /* next check if EOQ has been triggered already */
  855. mode = desc_read(prev, hw_mode);
  856. if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
  857. (chan->state == CPDMA_STATE_ACTIVE)) {
  858. desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
  859. chan_write(chan, hdp, desc_dma);
  860. chan->stats.misqueued++;
  861. }
  862. }
  863. int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
  864. int len, int directed)
  865. {
  866. struct cpdma_ctlr *ctlr = chan->ctlr;
  867. struct cpdma_desc __iomem *desc;
  868. dma_addr_t buffer;
  869. unsigned long flags;
  870. u32 mode;
  871. int ret = 0;
  872. spin_lock_irqsave(&chan->lock, flags);
  873. if (chan->state == CPDMA_STATE_TEARDOWN) {
  874. ret = -EINVAL;
  875. goto unlock_ret;
  876. }
  877. if (chan->count >= chan->desc_num) {
  878. chan->stats.desc_alloc_fail++;
  879. ret = -ENOMEM;
  880. goto unlock_ret;
  881. }
  882. desc = cpdma_desc_alloc(ctlr->pool);
  883. if (!desc) {
  884. chan->stats.desc_alloc_fail++;
  885. ret = -ENOMEM;
  886. goto unlock_ret;
  887. }
  888. if (len < ctlr->params.min_packet_size) {
  889. len = ctlr->params.min_packet_size;
  890. chan->stats.runt_transmit_buff++;
  891. }
  892. buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
  893. ret = dma_mapping_error(ctlr->dev, buffer);
  894. if (ret) {
  895. cpdma_desc_free(ctlr->pool, desc, 1);
  896. ret = -EINVAL;
  897. goto unlock_ret;
  898. }
  899. mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
  900. cpdma_desc_to_port(chan, mode, directed);
  901. /* Relaxed IO accessors can be used here as there is read barrier
  902. * at the end of write sequence.
  903. */
  904. writel_relaxed(0, &desc->hw_next);
  905. writel_relaxed(buffer, &desc->hw_buffer);
  906. writel_relaxed(len, &desc->hw_len);
  907. writel_relaxed(mode | len, &desc->hw_mode);
  908. writel_relaxed((uintptr_t)token, &desc->sw_token);
  909. writel_relaxed(buffer, &desc->sw_buffer);
  910. writel_relaxed(len, &desc->sw_len);
  911. desc_read(desc, sw_len);
  912. __cpdma_chan_submit(chan, desc);
  913. if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
  914. chan_write(chan, rxfree, 1);
  915. chan->count++;
  916. unlock_ret:
  917. spin_unlock_irqrestore(&chan->lock, flags);
  918. return ret;
  919. }
  920. EXPORT_SYMBOL_GPL(cpdma_chan_submit);
  921. bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
  922. {
  923. struct cpdma_ctlr *ctlr = chan->ctlr;
  924. struct cpdma_desc_pool *pool = ctlr->pool;
  925. bool free_tx_desc;
  926. unsigned long flags;
  927. spin_lock_irqsave(&chan->lock, flags);
  928. free_tx_desc = (chan->count < chan->desc_num) &&
  929. gen_pool_avail(pool->gen_pool);
  930. spin_unlock_irqrestore(&chan->lock, flags);
  931. return free_tx_desc;
  932. }
  933. EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
  934. static void __cpdma_chan_free(struct cpdma_chan *chan,
  935. struct cpdma_desc __iomem *desc,
  936. int outlen, int status)
  937. {
  938. struct cpdma_ctlr *ctlr = chan->ctlr;
  939. struct cpdma_desc_pool *pool = ctlr->pool;
  940. dma_addr_t buff_dma;
  941. int origlen;
  942. uintptr_t token;
  943. token = desc_read(desc, sw_token);
  944. buff_dma = desc_read(desc, sw_buffer);
  945. origlen = desc_read(desc, sw_len);
  946. dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
  947. cpdma_desc_free(pool, desc, 1);
  948. (*chan->handler)((void *)token, outlen, status);
  949. }
  950. static int __cpdma_chan_process(struct cpdma_chan *chan)
  951. {
  952. struct cpdma_ctlr *ctlr = chan->ctlr;
  953. struct cpdma_desc __iomem *desc;
  954. int status, outlen;
  955. int cb_status = 0;
  956. struct cpdma_desc_pool *pool = ctlr->pool;
  957. dma_addr_t desc_dma;
  958. unsigned long flags;
  959. spin_lock_irqsave(&chan->lock, flags);
  960. desc = chan->head;
  961. if (!desc) {
  962. chan->stats.empty_dequeue++;
  963. status = -ENOENT;
  964. goto unlock_ret;
  965. }
  966. desc_dma = desc_phys(pool, desc);
  967. status = desc_read(desc, hw_mode);
  968. outlen = status & 0x7ff;
  969. if (status & CPDMA_DESC_OWNER) {
  970. chan->stats.busy_dequeue++;
  971. status = -EBUSY;
  972. goto unlock_ret;
  973. }
  974. if (status & CPDMA_DESC_PASS_CRC)
  975. outlen -= CPDMA_DESC_CRC_LEN;
  976. status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
  977. CPDMA_DESC_PORT_MASK | CPDMA_RX_VLAN_ENCAP);
  978. chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
  979. chan_write(chan, cp, desc_dma);
  980. chan->count--;
  981. chan->stats.good_dequeue++;
  982. if ((status & CPDMA_DESC_EOQ) && chan->head) {
  983. chan->stats.requeue++;
  984. chan_write(chan, hdp, desc_phys(pool, chan->head));
  985. }
  986. spin_unlock_irqrestore(&chan->lock, flags);
  987. if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
  988. cb_status = -ENOSYS;
  989. else
  990. cb_status = status;
  991. __cpdma_chan_free(chan, desc, outlen, cb_status);
  992. return status;
  993. unlock_ret:
  994. spin_unlock_irqrestore(&chan->lock, flags);
  995. return status;
  996. }
  997. int cpdma_chan_process(struct cpdma_chan *chan, int quota)
  998. {
  999. int used = 0, ret = 0;
  1000. if (chan->state != CPDMA_STATE_ACTIVE)
  1001. return -EINVAL;
  1002. while (used < quota) {
  1003. ret = __cpdma_chan_process(chan);
  1004. if (ret < 0)
  1005. break;
  1006. used++;
  1007. }
  1008. return used;
  1009. }
  1010. EXPORT_SYMBOL_GPL(cpdma_chan_process);
  1011. int cpdma_chan_start(struct cpdma_chan *chan)
  1012. {
  1013. struct cpdma_ctlr *ctlr = chan->ctlr;
  1014. unsigned long flags;
  1015. int ret;
  1016. spin_lock_irqsave(&ctlr->lock, flags);
  1017. ret = cpdma_chan_set_chan_shaper(chan);
  1018. spin_unlock_irqrestore(&ctlr->lock, flags);
  1019. if (ret)
  1020. return ret;
  1021. ret = cpdma_chan_on(chan);
  1022. if (ret)
  1023. return ret;
  1024. return 0;
  1025. }
  1026. EXPORT_SYMBOL_GPL(cpdma_chan_start);
  1027. int cpdma_chan_stop(struct cpdma_chan *chan)
  1028. {
  1029. struct cpdma_ctlr *ctlr = chan->ctlr;
  1030. struct cpdma_desc_pool *pool = ctlr->pool;
  1031. unsigned long flags;
  1032. int ret;
  1033. unsigned timeout;
  1034. spin_lock_irqsave(&chan->lock, flags);
  1035. if (chan->state == CPDMA_STATE_TEARDOWN) {
  1036. spin_unlock_irqrestore(&chan->lock, flags);
  1037. return -EINVAL;
  1038. }
  1039. chan->state = CPDMA_STATE_TEARDOWN;
  1040. dma_reg_write(ctlr, chan->int_clear, chan->mask);
  1041. /* trigger teardown */
  1042. dma_reg_write(ctlr, chan->td, chan_linear(chan));
  1043. /* wait for teardown complete */
  1044. timeout = 100 * 100; /* 100 ms */
  1045. while (timeout) {
  1046. u32 cp = chan_read(chan, cp);
  1047. if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
  1048. break;
  1049. udelay(10);
  1050. timeout--;
  1051. }
  1052. WARN_ON(!timeout);
  1053. chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
  1054. /* handle completed packets */
  1055. spin_unlock_irqrestore(&chan->lock, flags);
  1056. do {
  1057. ret = __cpdma_chan_process(chan);
  1058. if (ret < 0)
  1059. break;
  1060. } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
  1061. spin_lock_irqsave(&chan->lock, flags);
  1062. /* remaining packets haven't been tx/rx'ed, clean them up */
  1063. while (chan->head) {
  1064. struct cpdma_desc __iomem *desc = chan->head;
  1065. dma_addr_t next_dma;
  1066. next_dma = desc_read(desc, hw_next);
  1067. chan->head = desc_from_phys(pool, next_dma);
  1068. chan->count--;
  1069. chan->stats.teardown_dequeue++;
  1070. /* issue callback without locks held */
  1071. spin_unlock_irqrestore(&chan->lock, flags);
  1072. __cpdma_chan_free(chan, desc, 0, -ENOSYS);
  1073. spin_lock_irqsave(&chan->lock, flags);
  1074. }
  1075. chan->state = CPDMA_STATE_IDLE;
  1076. spin_unlock_irqrestore(&chan->lock, flags);
  1077. return 0;
  1078. }
  1079. EXPORT_SYMBOL_GPL(cpdma_chan_stop);
  1080. int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
  1081. {
  1082. unsigned long flags;
  1083. spin_lock_irqsave(&chan->lock, flags);
  1084. if (chan->state != CPDMA_STATE_ACTIVE) {
  1085. spin_unlock_irqrestore(&chan->lock, flags);
  1086. return -EINVAL;
  1087. }
  1088. dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
  1089. chan->mask);
  1090. spin_unlock_irqrestore(&chan->lock, flags);
  1091. return 0;
  1092. }
  1093. int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  1094. {
  1095. unsigned long flags;
  1096. int ret;
  1097. spin_lock_irqsave(&ctlr->lock, flags);
  1098. ret = _cpdma_control_get(ctlr, control);
  1099. spin_unlock_irqrestore(&ctlr->lock, flags);
  1100. return ret;
  1101. }
  1102. int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  1103. {
  1104. unsigned long flags;
  1105. int ret;
  1106. spin_lock_irqsave(&ctlr->lock, flags);
  1107. ret = _cpdma_control_set(ctlr, control, value);
  1108. spin_unlock_irqrestore(&ctlr->lock, flags);
  1109. return ret;
  1110. }
  1111. EXPORT_SYMBOL_GPL(cpdma_control_set);
  1112. int cpdma_get_num_rx_descs(struct cpdma_ctlr *ctlr)
  1113. {
  1114. return ctlr->num_rx_desc;
  1115. }
  1116. EXPORT_SYMBOL_GPL(cpdma_get_num_rx_descs);
  1117. int cpdma_get_num_tx_descs(struct cpdma_ctlr *ctlr)
  1118. {
  1119. return ctlr->num_tx_desc;
  1120. }
  1121. EXPORT_SYMBOL_GPL(cpdma_get_num_tx_descs);
  1122. void cpdma_set_num_rx_descs(struct cpdma_ctlr *ctlr, int num_rx_desc)
  1123. {
  1124. ctlr->num_rx_desc = num_rx_desc;
  1125. ctlr->num_tx_desc = ctlr->pool->num_desc - ctlr->num_rx_desc;
  1126. }
  1127. EXPORT_SYMBOL_GPL(cpdma_set_num_rx_descs);
  1128. MODULE_LICENSE("GPL");