davinci_cpdma.c 25 KB

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