davinci_cpdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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_TXINTSTATRAW 0x80
  35. #define CPDMA_TXINTSTATMASKED 0x84
  36. #define CPDMA_TXINTMASKSET 0x88
  37. #define CPDMA_TXINTMASKCLEAR 0x8c
  38. #define CPDMA_MACINVECTOR 0x90
  39. #define CPDMA_MACEOIVECTOR 0x94
  40. #define CPDMA_RXINTSTATRAW 0xa0
  41. #define CPDMA_RXINTSTATMASKED 0xa4
  42. #define CPDMA_RXINTMASKSET 0xa8
  43. #define CPDMA_RXINTMASKCLEAR 0xac
  44. #define CPDMA_DMAINTSTATRAW 0xb0
  45. #define CPDMA_DMAINTSTATMASKED 0xb4
  46. #define CPDMA_DMAINTMASKSET 0xb8
  47. #define CPDMA_DMAINTMASKCLEAR 0xbc
  48. #define CPDMA_DMAINT_HOSTERR BIT(1)
  49. /* the following exist only if has_ext_regs is set */
  50. #define CPDMA_DMACONTROL 0x20
  51. #define CPDMA_DMASTATUS 0x24
  52. #define CPDMA_RXBUFFOFS 0x28
  53. #define CPDMA_EM_CONTROL 0x2c
  54. /* Descriptor mode bits */
  55. #define CPDMA_DESC_SOP BIT(31)
  56. #define CPDMA_DESC_EOP BIT(30)
  57. #define CPDMA_DESC_OWNER BIT(29)
  58. #define CPDMA_DESC_EOQ BIT(28)
  59. #define CPDMA_DESC_TD_COMPLETE BIT(27)
  60. #define CPDMA_DESC_PASS_CRC BIT(26)
  61. #define CPDMA_DESC_TO_PORT_EN BIT(20)
  62. #define CPDMA_TO_PORT_SHIFT 16
  63. #define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
  64. #define CPDMA_DESC_CRC_LEN 4
  65. #define CPDMA_TEARDOWN_VALUE 0xfffffffc
  66. struct cpdma_desc {
  67. /* hardware fields */
  68. u32 hw_next;
  69. u32 hw_buffer;
  70. u32 hw_len;
  71. u32 hw_mode;
  72. /* software fields */
  73. void *sw_token;
  74. u32 sw_buffer;
  75. u32 sw_len;
  76. };
  77. struct cpdma_desc_pool {
  78. phys_addr_t phys;
  79. dma_addr_t hw_addr;
  80. void __iomem *iomap; /* ioremap map */
  81. void *cpumap; /* dma_alloc map */
  82. int desc_size, mem_size;
  83. int num_desc, used_desc;
  84. struct device *dev;
  85. struct gen_pool *gen_pool;
  86. };
  87. enum cpdma_state {
  88. CPDMA_STATE_IDLE,
  89. CPDMA_STATE_ACTIVE,
  90. CPDMA_STATE_TEARDOWN,
  91. };
  92. struct cpdma_ctlr {
  93. enum cpdma_state state;
  94. struct cpdma_params params;
  95. struct device *dev;
  96. struct cpdma_desc_pool *pool;
  97. spinlock_t lock;
  98. struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
  99. };
  100. struct cpdma_chan {
  101. struct cpdma_desc __iomem *head, *tail;
  102. void __iomem *hdp, *cp, *rxfree;
  103. enum cpdma_state state;
  104. struct cpdma_ctlr *ctlr;
  105. int chan_num;
  106. spinlock_t lock;
  107. int count;
  108. u32 desc_num;
  109. u32 mask;
  110. cpdma_handler_fn handler;
  111. enum dma_data_direction dir;
  112. struct cpdma_chan_stats stats;
  113. /* offsets into dmaregs */
  114. int int_set, int_clear, td;
  115. };
  116. /* The following make access to common cpdma_ctlr params more readable */
  117. #define dmaregs params.dmaregs
  118. #define num_chan params.num_chan
  119. /* various accessors */
  120. #define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs))
  121. #define chan_read(chan, fld) __raw_readl((chan)->fld)
  122. #define desc_read(desc, fld) __raw_readl(&(desc)->fld)
  123. #define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs))
  124. #define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld)
  125. #define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld)
  126. #define cpdma_desc_to_port(chan, mode, directed) \
  127. do { \
  128. if (!is_rx_chan(chan) && ((directed == 1) || \
  129. (directed == 2))) \
  130. mode |= (CPDMA_DESC_TO_PORT_EN | \
  131. (directed << CPDMA_TO_PORT_SHIFT)); \
  132. } while (0)
  133. static void cpdma_desc_pool_destroy(struct cpdma_desc_pool *pool)
  134. {
  135. if (!pool)
  136. return;
  137. WARN_ON(pool->used_desc);
  138. if (pool->cpumap)
  139. dma_free_coherent(pool->dev, pool->mem_size, pool->cpumap,
  140. pool->phys);
  141. else
  142. iounmap(pool->iomap);
  143. }
  144. /*
  145. * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
  146. * emac) have dedicated on-chip memory for these descriptors. Some other
  147. * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
  148. * abstract out these details
  149. */
  150. static struct cpdma_desc_pool *
  151. cpdma_desc_pool_create(struct device *dev, u32 phys, dma_addr_t hw_addr,
  152. int size, int align)
  153. {
  154. struct cpdma_desc_pool *pool;
  155. int ret;
  156. pool = devm_kzalloc(dev, sizeof(*pool), GFP_KERNEL);
  157. if (!pool)
  158. goto gen_pool_create_fail;
  159. pool->dev = dev;
  160. pool->mem_size = size;
  161. pool->desc_size = ALIGN(sizeof(struct cpdma_desc), align);
  162. pool->num_desc = size / pool->desc_size;
  163. pool->gen_pool = devm_gen_pool_create(dev, ilog2(pool->desc_size), -1,
  164. "cpdma");
  165. if (IS_ERR(pool->gen_pool)) {
  166. dev_err(dev, "pool create failed %ld\n",
  167. PTR_ERR(pool->gen_pool));
  168. goto gen_pool_create_fail;
  169. }
  170. if (phys) {
  171. pool->phys = phys;
  172. pool->iomap = ioremap(phys, size); /* should be memremap? */
  173. pool->hw_addr = hw_addr;
  174. } else {
  175. pool->cpumap = dma_alloc_coherent(dev, size, &pool->hw_addr,
  176. GFP_KERNEL);
  177. pool->iomap = (void __iomem __force *)pool->cpumap;
  178. pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
  179. }
  180. if (!pool->iomap)
  181. goto gen_pool_create_fail;
  182. ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
  183. pool->phys, pool->mem_size, -1);
  184. if (ret < 0) {
  185. dev_err(dev, "pool add failed %d\n", ret);
  186. goto gen_pool_add_virt_fail;
  187. }
  188. return pool;
  189. gen_pool_add_virt_fail:
  190. cpdma_desc_pool_destroy(pool);
  191. gen_pool_create_fail:
  192. return NULL;
  193. }
  194. static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
  195. struct cpdma_desc __iomem *desc)
  196. {
  197. if (!desc)
  198. return 0;
  199. return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
  200. }
  201. static inline struct cpdma_desc __iomem *
  202. desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
  203. {
  204. return dma ? pool->iomap + dma - pool->hw_addr : NULL;
  205. }
  206. static struct cpdma_desc __iomem *
  207. cpdma_desc_alloc(struct cpdma_desc_pool *pool)
  208. {
  209. struct cpdma_desc __iomem *desc = NULL;
  210. desc = (struct cpdma_desc __iomem *)gen_pool_alloc(pool->gen_pool,
  211. pool->desc_size);
  212. if (desc)
  213. pool->used_desc++;
  214. return desc;
  215. }
  216. static void cpdma_desc_free(struct cpdma_desc_pool *pool,
  217. struct cpdma_desc __iomem *desc, int num_desc)
  218. {
  219. gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
  220. pool->used_desc--;
  221. }
  222. struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
  223. {
  224. struct cpdma_ctlr *ctlr;
  225. ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
  226. if (!ctlr)
  227. return NULL;
  228. ctlr->state = CPDMA_STATE_IDLE;
  229. ctlr->params = *params;
  230. ctlr->dev = params->dev;
  231. spin_lock_init(&ctlr->lock);
  232. ctlr->pool = cpdma_desc_pool_create(ctlr->dev,
  233. ctlr->params.desc_mem_phys,
  234. ctlr->params.desc_hw_addr,
  235. ctlr->params.desc_mem_size,
  236. ctlr->params.desc_align);
  237. if (!ctlr->pool)
  238. return NULL;
  239. if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
  240. ctlr->num_chan = CPDMA_MAX_CHANNELS;
  241. return ctlr;
  242. }
  243. EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
  244. int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
  245. {
  246. unsigned long flags;
  247. int i;
  248. spin_lock_irqsave(&ctlr->lock, flags);
  249. if (ctlr->state != CPDMA_STATE_IDLE) {
  250. spin_unlock_irqrestore(&ctlr->lock, flags);
  251. return -EBUSY;
  252. }
  253. if (ctlr->params.has_soft_reset) {
  254. unsigned timeout = 10 * 100;
  255. dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
  256. while (timeout) {
  257. if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
  258. break;
  259. udelay(10);
  260. timeout--;
  261. }
  262. WARN_ON(!timeout);
  263. }
  264. for (i = 0; i < ctlr->num_chan; i++) {
  265. __raw_writel(0, ctlr->params.txhdp + 4 * i);
  266. __raw_writel(0, ctlr->params.rxhdp + 4 * i);
  267. __raw_writel(0, ctlr->params.txcp + 4 * i);
  268. __raw_writel(0, ctlr->params.rxcp + 4 * i);
  269. }
  270. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  271. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  272. dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
  273. dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
  274. ctlr->state = CPDMA_STATE_ACTIVE;
  275. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  276. if (ctlr->channels[i])
  277. cpdma_chan_start(ctlr->channels[i]);
  278. }
  279. spin_unlock_irqrestore(&ctlr->lock, flags);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
  283. int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
  284. {
  285. unsigned long flags;
  286. int i;
  287. spin_lock_irqsave(&ctlr->lock, flags);
  288. if (ctlr->state == CPDMA_STATE_TEARDOWN) {
  289. spin_unlock_irqrestore(&ctlr->lock, flags);
  290. return -EINVAL;
  291. }
  292. ctlr->state = CPDMA_STATE_TEARDOWN;
  293. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  294. if (ctlr->channels[i])
  295. cpdma_chan_stop(ctlr->channels[i]);
  296. }
  297. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  298. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  299. dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
  300. dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
  301. ctlr->state = CPDMA_STATE_IDLE;
  302. spin_unlock_irqrestore(&ctlr->lock, flags);
  303. return 0;
  304. }
  305. EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
  306. int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
  307. {
  308. int ret = 0, i;
  309. if (!ctlr)
  310. return -EINVAL;
  311. if (ctlr->state != CPDMA_STATE_IDLE)
  312. cpdma_ctlr_stop(ctlr);
  313. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
  314. cpdma_chan_destroy(ctlr->channels[i]);
  315. cpdma_desc_pool_destroy(ctlr->pool);
  316. return ret;
  317. }
  318. EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
  319. int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
  320. {
  321. unsigned long flags;
  322. int i, reg;
  323. spin_lock_irqsave(&ctlr->lock, flags);
  324. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  325. spin_unlock_irqrestore(&ctlr->lock, flags);
  326. return -EINVAL;
  327. }
  328. reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
  329. dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
  330. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  331. if (ctlr->channels[i])
  332. cpdma_chan_int_ctrl(ctlr->channels[i], enable);
  333. }
  334. spin_unlock_irqrestore(&ctlr->lock, flags);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
  338. void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
  339. {
  340. dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
  341. }
  342. EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
  343. struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
  344. cpdma_handler_fn handler)
  345. {
  346. struct cpdma_chan *chan;
  347. int offset = (chan_num % CPDMA_MAX_CHANNELS) * 4;
  348. unsigned long flags;
  349. if (__chan_linear(chan_num) >= ctlr->num_chan)
  350. return NULL;
  351. chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
  352. if (!chan)
  353. return ERR_PTR(-ENOMEM);
  354. spin_lock_irqsave(&ctlr->lock, flags);
  355. if (ctlr->channels[chan_num]) {
  356. spin_unlock_irqrestore(&ctlr->lock, flags);
  357. devm_kfree(ctlr->dev, chan);
  358. return ERR_PTR(-EBUSY);
  359. }
  360. chan->ctlr = ctlr;
  361. chan->state = CPDMA_STATE_IDLE;
  362. chan->chan_num = chan_num;
  363. chan->handler = handler;
  364. chan->desc_num = ctlr->pool->num_desc / 2;
  365. if (is_rx_chan(chan)) {
  366. chan->hdp = ctlr->params.rxhdp + offset;
  367. chan->cp = ctlr->params.rxcp + offset;
  368. chan->rxfree = ctlr->params.rxfree + offset;
  369. chan->int_set = CPDMA_RXINTMASKSET;
  370. chan->int_clear = CPDMA_RXINTMASKCLEAR;
  371. chan->td = CPDMA_RXTEARDOWN;
  372. chan->dir = DMA_FROM_DEVICE;
  373. } else {
  374. chan->hdp = ctlr->params.txhdp + offset;
  375. chan->cp = ctlr->params.txcp + offset;
  376. chan->int_set = CPDMA_TXINTMASKSET;
  377. chan->int_clear = CPDMA_TXINTMASKCLEAR;
  378. chan->td = CPDMA_TXTEARDOWN;
  379. chan->dir = DMA_TO_DEVICE;
  380. }
  381. chan->mask = BIT(chan_linear(chan));
  382. spin_lock_init(&chan->lock);
  383. ctlr->channels[chan_num] = chan;
  384. spin_unlock_irqrestore(&ctlr->lock, flags);
  385. return chan;
  386. }
  387. EXPORT_SYMBOL_GPL(cpdma_chan_create);
  388. int cpdma_chan_get_rx_buf_num(struct cpdma_ctlr *ctlr)
  389. {
  390. return ctlr->pool->num_desc / 2;
  391. }
  392. EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
  393. int cpdma_chan_destroy(struct cpdma_chan *chan)
  394. {
  395. struct cpdma_ctlr *ctlr;
  396. unsigned long flags;
  397. if (!chan)
  398. return -EINVAL;
  399. ctlr = chan->ctlr;
  400. spin_lock_irqsave(&ctlr->lock, flags);
  401. if (chan->state != CPDMA_STATE_IDLE)
  402. cpdma_chan_stop(chan);
  403. ctlr->channels[chan->chan_num] = NULL;
  404. spin_unlock_irqrestore(&ctlr->lock, flags);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
  408. int cpdma_chan_get_stats(struct cpdma_chan *chan,
  409. struct cpdma_chan_stats *stats)
  410. {
  411. unsigned long flags;
  412. if (!chan)
  413. return -EINVAL;
  414. spin_lock_irqsave(&chan->lock, flags);
  415. memcpy(stats, &chan->stats, sizeof(*stats));
  416. spin_unlock_irqrestore(&chan->lock, flags);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
  420. static void __cpdma_chan_submit(struct cpdma_chan *chan,
  421. struct cpdma_desc __iomem *desc)
  422. {
  423. struct cpdma_ctlr *ctlr = chan->ctlr;
  424. struct cpdma_desc __iomem *prev = chan->tail;
  425. struct cpdma_desc_pool *pool = ctlr->pool;
  426. dma_addr_t desc_dma;
  427. u32 mode;
  428. desc_dma = desc_phys(pool, desc);
  429. /* simple case - idle channel */
  430. if (!chan->head) {
  431. chan->stats.head_enqueue++;
  432. chan->head = desc;
  433. chan->tail = desc;
  434. if (chan->state == CPDMA_STATE_ACTIVE)
  435. chan_write(chan, hdp, desc_dma);
  436. return;
  437. }
  438. /* first chain the descriptor at the tail of the list */
  439. desc_write(prev, hw_next, desc_dma);
  440. chan->tail = desc;
  441. chan->stats.tail_enqueue++;
  442. /* next check if EOQ has been triggered already */
  443. mode = desc_read(prev, hw_mode);
  444. if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
  445. (chan->state == CPDMA_STATE_ACTIVE)) {
  446. desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
  447. chan_write(chan, hdp, desc_dma);
  448. chan->stats.misqueued++;
  449. }
  450. }
  451. int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
  452. int len, int directed)
  453. {
  454. struct cpdma_ctlr *ctlr = chan->ctlr;
  455. struct cpdma_desc __iomem *desc;
  456. dma_addr_t buffer;
  457. unsigned long flags;
  458. u32 mode;
  459. int ret = 0;
  460. spin_lock_irqsave(&chan->lock, flags);
  461. if (chan->state == CPDMA_STATE_TEARDOWN) {
  462. ret = -EINVAL;
  463. goto unlock_ret;
  464. }
  465. if (chan->count >= chan->desc_num) {
  466. chan->stats.desc_alloc_fail++;
  467. ret = -ENOMEM;
  468. goto unlock_ret;
  469. }
  470. desc = cpdma_desc_alloc(ctlr->pool);
  471. if (!desc) {
  472. chan->stats.desc_alloc_fail++;
  473. ret = -ENOMEM;
  474. goto unlock_ret;
  475. }
  476. if (len < ctlr->params.min_packet_size) {
  477. len = ctlr->params.min_packet_size;
  478. chan->stats.runt_transmit_buff++;
  479. }
  480. buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
  481. ret = dma_mapping_error(ctlr->dev, buffer);
  482. if (ret) {
  483. cpdma_desc_free(ctlr->pool, desc, 1);
  484. ret = -EINVAL;
  485. goto unlock_ret;
  486. }
  487. mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
  488. cpdma_desc_to_port(chan, mode, directed);
  489. desc_write(desc, hw_next, 0);
  490. desc_write(desc, hw_buffer, buffer);
  491. desc_write(desc, hw_len, len);
  492. desc_write(desc, hw_mode, mode | len);
  493. desc_write(desc, sw_token, token);
  494. desc_write(desc, sw_buffer, buffer);
  495. desc_write(desc, sw_len, len);
  496. __cpdma_chan_submit(chan, desc);
  497. if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
  498. chan_write(chan, rxfree, 1);
  499. chan->count++;
  500. unlock_ret:
  501. spin_unlock_irqrestore(&chan->lock, flags);
  502. return ret;
  503. }
  504. EXPORT_SYMBOL_GPL(cpdma_chan_submit);
  505. bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
  506. {
  507. struct cpdma_ctlr *ctlr = chan->ctlr;
  508. struct cpdma_desc_pool *pool = ctlr->pool;
  509. bool free_tx_desc;
  510. unsigned long flags;
  511. spin_lock_irqsave(&chan->lock, flags);
  512. free_tx_desc = (chan->count < chan->desc_num) &&
  513. gen_pool_avail(pool->gen_pool);
  514. spin_unlock_irqrestore(&chan->lock, flags);
  515. return free_tx_desc;
  516. }
  517. EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
  518. static void __cpdma_chan_free(struct cpdma_chan *chan,
  519. struct cpdma_desc __iomem *desc,
  520. int outlen, int status)
  521. {
  522. struct cpdma_ctlr *ctlr = chan->ctlr;
  523. struct cpdma_desc_pool *pool = ctlr->pool;
  524. dma_addr_t buff_dma;
  525. int origlen;
  526. void *token;
  527. token = (void *)desc_read(desc, sw_token);
  528. buff_dma = desc_read(desc, sw_buffer);
  529. origlen = desc_read(desc, sw_len);
  530. dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
  531. cpdma_desc_free(pool, desc, 1);
  532. (*chan->handler)(token, outlen, status);
  533. }
  534. static int __cpdma_chan_process(struct cpdma_chan *chan)
  535. {
  536. struct cpdma_ctlr *ctlr = chan->ctlr;
  537. struct cpdma_desc __iomem *desc;
  538. int status, outlen;
  539. int cb_status = 0;
  540. struct cpdma_desc_pool *pool = ctlr->pool;
  541. dma_addr_t desc_dma;
  542. unsigned long flags;
  543. spin_lock_irqsave(&chan->lock, flags);
  544. desc = chan->head;
  545. if (!desc) {
  546. chan->stats.empty_dequeue++;
  547. status = -ENOENT;
  548. goto unlock_ret;
  549. }
  550. desc_dma = desc_phys(pool, desc);
  551. status = __raw_readl(&desc->hw_mode);
  552. outlen = status & 0x7ff;
  553. if (status & CPDMA_DESC_OWNER) {
  554. chan->stats.busy_dequeue++;
  555. status = -EBUSY;
  556. goto unlock_ret;
  557. }
  558. if (status & CPDMA_DESC_PASS_CRC)
  559. outlen -= CPDMA_DESC_CRC_LEN;
  560. status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
  561. CPDMA_DESC_PORT_MASK);
  562. chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
  563. chan_write(chan, cp, desc_dma);
  564. chan->count--;
  565. chan->stats.good_dequeue++;
  566. if (status & CPDMA_DESC_EOQ) {
  567. chan->stats.requeue++;
  568. chan_write(chan, hdp, desc_phys(pool, chan->head));
  569. }
  570. spin_unlock_irqrestore(&chan->lock, flags);
  571. if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
  572. cb_status = -ENOSYS;
  573. else
  574. cb_status = status;
  575. __cpdma_chan_free(chan, desc, outlen, cb_status);
  576. return status;
  577. unlock_ret:
  578. spin_unlock_irqrestore(&chan->lock, flags);
  579. return status;
  580. }
  581. int cpdma_chan_process(struct cpdma_chan *chan, int quota)
  582. {
  583. int used = 0, ret = 0;
  584. if (chan->state != CPDMA_STATE_ACTIVE)
  585. return -EINVAL;
  586. while (used < quota) {
  587. ret = __cpdma_chan_process(chan);
  588. if (ret < 0)
  589. break;
  590. used++;
  591. }
  592. return used;
  593. }
  594. EXPORT_SYMBOL_GPL(cpdma_chan_process);
  595. int cpdma_chan_start(struct cpdma_chan *chan)
  596. {
  597. struct cpdma_ctlr *ctlr = chan->ctlr;
  598. struct cpdma_desc_pool *pool = ctlr->pool;
  599. unsigned long flags;
  600. spin_lock_irqsave(&chan->lock, flags);
  601. if (chan->state != CPDMA_STATE_IDLE) {
  602. spin_unlock_irqrestore(&chan->lock, flags);
  603. return -EBUSY;
  604. }
  605. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  606. spin_unlock_irqrestore(&chan->lock, flags);
  607. return -EINVAL;
  608. }
  609. dma_reg_write(ctlr, chan->int_set, chan->mask);
  610. chan->state = CPDMA_STATE_ACTIVE;
  611. if (chan->head) {
  612. chan_write(chan, hdp, desc_phys(pool, chan->head));
  613. if (chan->rxfree)
  614. chan_write(chan, rxfree, chan->count);
  615. }
  616. spin_unlock_irqrestore(&chan->lock, flags);
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(cpdma_chan_start);
  620. int cpdma_chan_stop(struct cpdma_chan *chan)
  621. {
  622. struct cpdma_ctlr *ctlr = chan->ctlr;
  623. struct cpdma_desc_pool *pool = ctlr->pool;
  624. unsigned long flags;
  625. int ret;
  626. unsigned timeout;
  627. spin_lock_irqsave(&chan->lock, flags);
  628. if (chan->state == CPDMA_STATE_TEARDOWN) {
  629. spin_unlock_irqrestore(&chan->lock, flags);
  630. return -EINVAL;
  631. }
  632. chan->state = CPDMA_STATE_TEARDOWN;
  633. dma_reg_write(ctlr, chan->int_clear, chan->mask);
  634. /* trigger teardown */
  635. dma_reg_write(ctlr, chan->td, chan_linear(chan));
  636. /* wait for teardown complete */
  637. timeout = 100 * 100; /* 100 ms */
  638. while (timeout) {
  639. u32 cp = chan_read(chan, cp);
  640. if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
  641. break;
  642. udelay(10);
  643. timeout--;
  644. }
  645. WARN_ON(!timeout);
  646. chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
  647. /* handle completed packets */
  648. spin_unlock_irqrestore(&chan->lock, flags);
  649. do {
  650. ret = __cpdma_chan_process(chan);
  651. if (ret < 0)
  652. break;
  653. } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
  654. spin_lock_irqsave(&chan->lock, flags);
  655. /* remaining packets haven't been tx/rx'ed, clean them up */
  656. while (chan->head) {
  657. struct cpdma_desc __iomem *desc = chan->head;
  658. dma_addr_t next_dma;
  659. next_dma = desc_read(desc, hw_next);
  660. chan->head = desc_from_phys(pool, next_dma);
  661. chan->count--;
  662. chan->stats.teardown_dequeue++;
  663. /* issue callback without locks held */
  664. spin_unlock_irqrestore(&chan->lock, flags);
  665. __cpdma_chan_free(chan, desc, 0, -ENOSYS);
  666. spin_lock_irqsave(&chan->lock, flags);
  667. }
  668. chan->state = CPDMA_STATE_IDLE;
  669. spin_unlock_irqrestore(&chan->lock, flags);
  670. return 0;
  671. }
  672. EXPORT_SYMBOL_GPL(cpdma_chan_stop);
  673. int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
  674. {
  675. unsigned long flags;
  676. spin_lock_irqsave(&chan->lock, flags);
  677. if (chan->state != CPDMA_STATE_ACTIVE) {
  678. spin_unlock_irqrestore(&chan->lock, flags);
  679. return -EINVAL;
  680. }
  681. dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
  682. chan->mask);
  683. spin_unlock_irqrestore(&chan->lock, flags);
  684. return 0;
  685. }
  686. struct cpdma_control_info {
  687. u32 reg;
  688. u32 shift, mask;
  689. int access;
  690. #define ACCESS_RO BIT(0)
  691. #define ACCESS_WO BIT(1)
  692. #define ACCESS_RW (ACCESS_RO | ACCESS_WO)
  693. };
  694. static struct cpdma_control_info controls[] = {
  695. [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
  696. [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
  697. [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
  698. [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
  699. [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
  700. [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
  701. [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
  702. [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
  703. [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
  704. [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
  705. [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
  706. };
  707. int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  708. {
  709. unsigned long flags;
  710. struct cpdma_control_info *info = &controls[control];
  711. int ret;
  712. spin_lock_irqsave(&ctlr->lock, flags);
  713. ret = -ENOTSUPP;
  714. if (!ctlr->params.has_ext_regs)
  715. goto unlock_ret;
  716. ret = -EINVAL;
  717. if (ctlr->state != CPDMA_STATE_ACTIVE)
  718. goto unlock_ret;
  719. ret = -ENOENT;
  720. if (control < 0 || control >= ARRAY_SIZE(controls))
  721. goto unlock_ret;
  722. ret = -EPERM;
  723. if ((info->access & ACCESS_RO) != ACCESS_RO)
  724. goto unlock_ret;
  725. ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
  726. unlock_ret:
  727. spin_unlock_irqrestore(&ctlr->lock, flags);
  728. return ret;
  729. }
  730. int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  731. {
  732. unsigned long flags;
  733. struct cpdma_control_info *info = &controls[control];
  734. int ret;
  735. u32 val;
  736. spin_lock_irqsave(&ctlr->lock, flags);
  737. ret = -ENOTSUPP;
  738. if (!ctlr->params.has_ext_regs)
  739. goto unlock_ret;
  740. ret = -EINVAL;
  741. if (ctlr->state != CPDMA_STATE_ACTIVE)
  742. goto unlock_ret;
  743. ret = -ENOENT;
  744. if (control < 0 || control >= ARRAY_SIZE(controls))
  745. goto unlock_ret;
  746. ret = -EPERM;
  747. if ((info->access & ACCESS_WO) != ACCESS_WO)
  748. goto unlock_ret;
  749. val = dma_reg_read(ctlr, info->reg);
  750. val &= ~(info->mask << info->shift);
  751. val |= (value & info->mask) << info->shift;
  752. dma_reg_write(ctlr, info->reg, val);
  753. ret = 0;
  754. unlock_ret:
  755. spin_unlock_irqrestore(&ctlr->lock, flags);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(cpdma_control_set);
  759. MODULE_LICENSE("GPL");