pch_dma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. * Topcliff PCH DMA controller driver
  3. * Copyright (c) 2010 Intel Corporation
  4. * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/dmaengine.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/pch_dma.h>
  23. #include "dmaengine.h"
  24. #define DRV_NAME "pch-dma"
  25. #define DMA_CTL0_DISABLE 0x0
  26. #define DMA_CTL0_SG 0x1
  27. #define DMA_CTL0_ONESHOT 0x2
  28. #define DMA_CTL0_MODE_MASK_BITS 0x3
  29. #define DMA_CTL0_DIR_SHIFT_BITS 2
  30. #define DMA_CTL0_BITS_PER_CH 4
  31. #define DMA_CTL2_START_SHIFT_BITS 8
  32. #define DMA_CTL2_IRQ_ENABLE_MASK ((1UL << DMA_CTL2_START_SHIFT_BITS) - 1)
  33. #define DMA_STATUS_IDLE 0x0
  34. #define DMA_STATUS_DESC_READ 0x1
  35. #define DMA_STATUS_WAIT 0x2
  36. #define DMA_STATUS_ACCESS 0x3
  37. #define DMA_STATUS_BITS_PER_CH 2
  38. #define DMA_STATUS_MASK_BITS 0x3
  39. #define DMA_STATUS_SHIFT_BITS 16
  40. #define DMA_STATUS_IRQ(x) (0x1 << (x))
  41. #define DMA_STATUS0_ERR(x) (0x1 << ((x) + 8))
  42. #define DMA_STATUS2_ERR(x) (0x1 << (x))
  43. #define DMA_DESC_WIDTH_SHIFT_BITS 12
  44. #define DMA_DESC_WIDTH_1_BYTE (0x3 << DMA_DESC_WIDTH_SHIFT_BITS)
  45. #define DMA_DESC_WIDTH_2_BYTES (0x2 << DMA_DESC_WIDTH_SHIFT_BITS)
  46. #define DMA_DESC_WIDTH_4_BYTES (0x0 << DMA_DESC_WIDTH_SHIFT_BITS)
  47. #define DMA_DESC_MAX_COUNT_1_BYTE 0x3FF
  48. #define DMA_DESC_MAX_COUNT_2_BYTES 0x3FF
  49. #define DMA_DESC_MAX_COUNT_4_BYTES 0x7FF
  50. #define DMA_DESC_END_WITHOUT_IRQ 0x0
  51. #define DMA_DESC_END_WITH_IRQ 0x1
  52. #define DMA_DESC_FOLLOW_WITHOUT_IRQ 0x2
  53. #define DMA_DESC_FOLLOW_WITH_IRQ 0x3
  54. #define MAX_CHAN_NR 12
  55. #define DMA_MASK_CTL0_MODE 0x33333333
  56. #define DMA_MASK_CTL2_MODE 0x00003333
  57. static unsigned int init_nr_desc_per_channel = 64;
  58. module_param(init_nr_desc_per_channel, uint, 0644);
  59. MODULE_PARM_DESC(init_nr_desc_per_channel,
  60. "initial descriptors per channel (default: 64)");
  61. struct pch_dma_desc_regs {
  62. u32 dev_addr;
  63. u32 mem_addr;
  64. u32 size;
  65. u32 next;
  66. };
  67. struct pch_dma_regs {
  68. u32 dma_ctl0;
  69. u32 dma_ctl1;
  70. u32 dma_ctl2;
  71. u32 dma_ctl3;
  72. u32 dma_sts0;
  73. u32 dma_sts1;
  74. u32 dma_sts2;
  75. u32 reserved3;
  76. struct pch_dma_desc_regs desc[MAX_CHAN_NR];
  77. };
  78. struct pch_dma_desc {
  79. struct pch_dma_desc_regs regs;
  80. struct dma_async_tx_descriptor txd;
  81. struct list_head desc_node;
  82. struct list_head tx_list;
  83. };
  84. struct pch_dma_chan {
  85. struct dma_chan chan;
  86. void __iomem *membase;
  87. enum dma_transfer_direction dir;
  88. struct tasklet_struct tasklet;
  89. unsigned long err_status;
  90. spinlock_t lock;
  91. struct list_head active_list;
  92. struct list_head queue;
  93. struct list_head free_list;
  94. unsigned int descs_allocated;
  95. };
  96. #define PDC_DEV_ADDR 0x00
  97. #define PDC_MEM_ADDR 0x04
  98. #define PDC_SIZE 0x08
  99. #define PDC_NEXT 0x0C
  100. #define channel_readl(pdc, name) \
  101. readl((pdc)->membase + PDC_##name)
  102. #define channel_writel(pdc, name, val) \
  103. writel((val), (pdc)->membase + PDC_##name)
  104. struct pch_dma {
  105. struct dma_device dma;
  106. void __iomem *membase;
  107. struct pci_pool *pool;
  108. struct pch_dma_regs regs;
  109. struct pch_dma_desc_regs ch_regs[MAX_CHAN_NR];
  110. struct pch_dma_chan channels[MAX_CHAN_NR];
  111. };
  112. #define PCH_DMA_CTL0 0x00
  113. #define PCH_DMA_CTL1 0x04
  114. #define PCH_DMA_CTL2 0x08
  115. #define PCH_DMA_CTL3 0x0C
  116. #define PCH_DMA_STS0 0x10
  117. #define PCH_DMA_STS1 0x14
  118. #define PCH_DMA_STS2 0x18
  119. #define dma_readl(pd, name) \
  120. readl((pd)->membase + PCH_DMA_##name)
  121. #define dma_writel(pd, name, val) \
  122. writel((val), (pd)->membase + PCH_DMA_##name)
  123. static inline
  124. struct pch_dma_desc *to_pd_desc(struct dma_async_tx_descriptor *txd)
  125. {
  126. return container_of(txd, struct pch_dma_desc, txd);
  127. }
  128. static inline struct pch_dma_chan *to_pd_chan(struct dma_chan *chan)
  129. {
  130. return container_of(chan, struct pch_dma_chan, chan);
  131. }
  132. static inline struct pch_dma *to_pd(struct dma_device *ddev)
  133. {
  134. return container_of(ddev, struct pch_dma, dma);
  135. }
  136. static inline struct device *chan2dev(struct dma_chan *chan)
  137. {
  138. return &chan->dev->device;
  139. }
  140. static inline struct device *chan2parent(struct dma_chan *chan)
  141. {
  142. return chan->dev->device.parent;
  143. }
  144. static inline
  145. struct pch_dma_desc *pdc_first_active(struct pch_dma_chan *pd_chan)
  146. {
  147. return list_first_entry(&pd_chan->active_list,
  148. struct pch_dma_desc, desc_node);
  149. }
  150. static inline
  151. struct pch_dma_desc *pdc_first_queued(struct pch_dma_chan *pd_chan)
  152. {
  153. return list_first_entry(&pd_chan->queue,
  154. struct pch_dma_desc, desc_node);
  155. }
  156. static void pdc_enable_irq(struct dma_chan *chan, int enable)
  157. {
  158. struct pch_dma *pd = to_pd(chan->device);
  159. u32 val;
  160. int pos;
  161. if (chan->chan_id < 8)
  162. pos = chan->chan_id;
  163. else
  164. pos = chan->chan_id + 8;
  165. val = dma_readl(pd, CTL2);
  166. if (enable)
  167. val |= 0x1 << pos;
  168. else
  169. val &= ~(0x1 << pos);
  170. dma_writel(pd, CTL2, val);
  171. dev_dbg(chan2dev(chan), "pdc_enable_irq: chan %d -> %x\n",
  172. chan->chan_id, val);
  173. }
  174. static void pdc_set_dir(struct dma_chan *chan)
  175. {
  176. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  177. struct pch_dma *pd = to_pd(chan->device);
  178. u32 val;
  179. u32 mask_mode;
  180. u32 mask_ctl;
  181. if (chan->chan_id < 8) {
  182. val = dma_readl(pd, CTL0);
  183. mask_mode = DMA_CTL0_MODE_MASK_BITS <<
  184. (DMA_CTL0_BITS_PER_CH * chan->chan_id);
  185. mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  186. (DMA_CTL0_BITS_PER_CH * chan->chan_id));
  187. val &= mask_mode;
  188. if (pd_chan->dir == DMA_MEM_TO_DEV)
  189. val |= 0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
  190. DMA_CTL0_DIR_SHIFT_BITS);
  191. else
  192. val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
  193. DMA_CTL0_DIR_SHIFT_BITS));
  194. val |= mask_ctl;
  195. dma_writel(pd, CTL0, val);
  196. } else {
  197. int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
  198. val = dma_readl(pd, CTL3);
  199. mask_mode = DMA_CTL0_MODE_MASK_BITS <<
  200. (DMA_CTL0_BITS_PER_CH * ch);
  201. mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  202. (DMA_CTL0_BITS_PER_CH * ch));
  203. val &= mask_mode;
  204. if (pd_chan->dir == DMA_MEM_TO_DEV)
  205. val |= 0x1 << (DMA_CTL0_BITS_PER_CH * ch +
  206. DMA_CTL0_DIR_SHIFT_BITS);
  207. else
  208. val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * ch +
  209. DMA_CTL0_DIR_SHIFT_BITS));
  210. val |= mask_ctl;
  211. dma_writel(pd, CTL3, val);
  212. }
  213. dev_dbg(chan2dev(chan), "pdc_set_dir: chan %d -> %x\n",
  214. chan->chan_id, val);
  215. }
  216. static void pdc_set_mode(struct dma_chan *chan, u32 mode)
  217. {
  218. struct pch_dma *pd = to_pd(chan->device);
  219. u32 val;
  220. u32 mask_ctl;
  221. u32 mask_dir;
  222. if (chan->chan_id < 8) {
  223. mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  224. (DMA_CTL0_BITS_PER_CH * chan->chan_id));
  225. mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +\
  226. DMA_CTL0_DIR_SHIFT_BITS);
  227. val = dma_readl(pd, CTL0);
  228. val &= mask_dir;
  229. val |= mode << (DMA_CTL0_BITS_PER_CH * chan->chan_id);
  230. val |= mask_ctl;
  231. dma_writel(pd, CTL0, val);
  232. } else {
  233. int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
  234. mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  235. (DMA_CTL0_BITS_PER_CH * ch));
  236. mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * ch +\
  237. DMA_CTL0_DIR_SHIFT_BITS);
  238. val = dma_readl(pd, CTL3);
  239. val &= mask_dir;
  240. val |= mode << (DMA_CTL0_BITS_PER_CH * ch);
  241. val |= mask_ctl;
  242. dma_writel(pd, CTL3, val);
  243. }
  244. dev_dbg(chan2dev(chan), "pdc_set_mode: chan %d -> %x\n",
  245. chan->chan_id, val);
  246. }
  247. static u32 pdc_get_status0(struct pch_dma_chan *pd_chan)
  248. {
  249. struct pch_dma *pd = to_pd(pd_chan->chan.device);
  250. u32 val;
  251. val = dma_readl(pd, STS0);
  252. return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
  253. DMA_STATUS_BITS_PER_CH * pd_chan->chan.chan_id));
  254. }
  255. static u32 pdc_get_status2(struct pch_dma_chan *pd_chan)
  256. {
  257. struct pch_dma *pd = to_pd(pd_chan->chan.device);
  258. u32 val;
  259. val = dma_readl(pd, STS2);
  260. return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
  261. DMA_STATUS_BITS_PER_CH * (pd_chan->chan.chan_id - 8)));
  262. }
  263. static bool pdc_is_idle(struct pch_dma_chan *pd_chan)
  264. {
  265. u32 sts;
  266. if (pd_chan->chan.chan_id < 8)
  267. sts = pdc_get_status0(pd_chan);
  268. else
  269. sts = pdc_get_status2(pd_chan);
  270. if (sts == DMA_STATUS_IDLE)
  271. return true;
  272. else
  273. return false;
  274. }
  275. static void pdc_dostart(struct pch_dma_chan *pd_chan, struct pch_dma_desc* desc)
  276. {
  277. if (!pdc_is_idle(pd_chan)) {
  278. dev_err(chan2dev(&pd_chan->chan),
  279. "BUG: Attempt to start non-idle channel\n");
  280. return;
  281. }
  282. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> dev_addr: %x\n",
  283. pd_chan->chan.chan_id, desc->regs.dev_addr);
  284. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> mem_addr: %x\n",
  285. pd_chan->chan.chan_id, desc->regs.mem_addr);
  286. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> size: %x\n",
  287. pd_chan->chan.chan_id, desc->regs.size);
  288. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> next: %x\n",
  289. pd_chan->chan.chan_id, desc->regs.next);
  290. if (list_empty(&desc->tx_list)) {
  291. channel_writel(pd_chan, DEV_ADDR, desc->regs.dev_addr);
  292. channel_writel(pd_chan, MEM_ADDR, desc->regs.mem_addr);
  293. channel_writel(pd_chan, SIZE, desc->regs.size);
  294. channel_writel(pd_chan, NEXT, desc->regs.next);
  295. pdc_set_mode(&pd_chan->chan, DMA_CTL0_ONESHOT);
  296. } else {
  297. channel_writel(pd_chan, NEXT, desc->txd.phys);
  298. pdc_set_mode(&pd_chan->chan, DMA_CTL0_SG);
  299. }
  300. }
  301. static void pdc_chain_complete(struct pch_dma_chan *pd_chan,
  302. struct pch_dma_desc *desc)
  303. {
  304. struct dma_async_tx_descriptor *txd = &desc->txd;
  305. struct dmaengine_desc_callback cb;
  306. dmaengine_desc_get_callback(txd, &cb);
  307. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  308. list_move(&desc->desc_node, &pd_chan->free_list);
  309. dmaengine_desc_callback_invoke(&cb, NULL);
  310. }
  311. static void pdc_complete_all(struct pch_dma_chan *pd_chan)
  312. {
  313. struct pch_dma_desc *desc, *_d;
  314. LIST_HEAD(list);
  315. BUG_ON(!pdc_is_idle(pd_chan));
  316. if (!list_empty(&pd_chan->queue))
  317. pdc_dostart(pd_chan, pdc_first_queued(pd_chan));
  318. list_splice_init(&pd_chan->active_list, &list);
  319. list_splice_init(&pd_chan->queue, &pd_chan->active_list);
  320. list_for_each_entry_safe(desc, _d, &list, desc_node)
  321. pdc_chain_complete(pd_chan, desc);
  322. }
  323. static void pdc_handle_error(struct pch_dma_chan *pd_chan)
  324. {
  325. struct pch_dma_desc *bad_desc;
  326. bad_desc = pdc_first_active(pd_chan);
  327. list_del(&bad_desc->desc_node);
  328. list_splice_init(&pd_chan->queue, pd_chan->active_list.prev);
  329. if (!list_empty(&pd_chan->active_list))
  330. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  331. dev_crit(chan2dev(&pd_chan->chan), "Bad descriptor submitted\n");
  332. dev_crit(chan2dev(&pd_chan->chan), "descriptor cookie: %d\n",
  333. bad_desc->txd.cookie);
  334. pdc_chain_complete(pd_chan, bad_desc);
  335. }
  336. static void pdc_advance_work(struct pch_dma_chan *pd_chan)
  337. {
  338. if (list_empty(&pd_chan->active_list) ||
  339. list_is_singular(&pd_chan->active_list)) {
  340. pdc_complete_all(pd_chan);
  341. } else {
  342. pdc_chain_complete(pd_chan, pdc_first_active(pd_chan));
  343. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  344. }
  345. }
  346. static dma_cookie_t pd_tx_submit(struct dma_async_tx_descriptor *txd)
  347. {
  348. struct pch_dma_desc *desc = to_pd_desc(txd);
  349. struct pch_dma_chan *pd_chan = to_pd_chan(txd->chan);
  350. dma_cookie_t cookie;
  351. spin_lock(&pd_chan->lock);
  352. cookie = dma_cookie_assign(txd);
  353. if (list_empty(&pd_chan->active_list)) {
  354. list_add_tail(&desc->desc_node, &pd_chan->active_list);
  355. pdc_dostart(pd_chan, desc);
  356. } else {
  357. list_add_tail(&desc->desc_node, &pd_chan->queue);
  358. }
  359. spin_unlock(&pd_chan->lock);
  360. return 0;
  361. }
  362. static struct pch_dma_desc *pdc_alloc_desc(struct dma_chan *chan, gfp_t flags)
  363. {
  364. struct pch_dma_desc *desc = NULL;
  365. struct pch_dma *pd = to_pd(chan->device);
  366. dma_addr_t addr;
  367. desc = pci_pool_alloc(pd->pool, flags, &addr);
  368. if (desc) {
  369. memset(desc, 0, sizeof(struct pch_dma_desc));
  370. INIT_LIST_HEAD(&desc->tx_list);
  371. dma_async_tx_descriptor_init(&desc->txd, chan);
  372. desc->txd.tx_submit = pd_tx_submit;
  373. desc->txd.flags = DMA_CTRL_ACK;
  374. desc->txd.phys = addr;
  375. }
  376. return desc;
  377. }
  378. static struct pch_dma_desc *pdc_desc_get(struct pch_dma_chan *pd_chan)
  379. {
  380. struct pch_dma_desc *desc, *_d;
  381. struct pch_dma_desc *ret = NULL;
  382. int i = 0;
  383. spin_lock(&pd_chan->lock);
  384. list_for_each_entry_safe(desc, _d, &pd_chan->free_list, desc_node) {
  385. i++;
  386. if (async_tx_test_ack(&desc->txd)) {
  387. list_del(&desc->desc_node);
  388. ret = desc;
  389. break;
  390. }
  391. dev_dbg(chan2dev(&pd_chan->chan), "desc %p not ACKed\n", desc);
  392. }
  393. spin_unlock(&pd_chan->lock);
  394. dev_dbg(chan2dev(&pd_chan->chan), "scanned %d descriptors\n", i);
  395. if (!ret) {
  396. ret = pdc_alloc_desc(&pd_chan->chan, GFP_ATOMIC);
  397. if (ret) {
  398. spin_lock(&pd_chan->lock);
  399. pd_chan->descs_allocated++;
  400. spin_unlock(&pd_chan->lock);
  401. } else {
  402. dev_err(chan2dev(&pd_chan->chan),
  403. "failed to alloc desc\n");
  404. }
  405. }
  406. return ret;
  407. }
  408. static void pdc_desc_put(struct pch_dma_chan *pd_chan,
  409. struct pch_dma_desc *desc)
  410. {
  411. if (desc) {
  412. spin_lock(&pd_chan->lock);
  413. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  414. list_add(&desc->desc_node, &pd_chan->free_list);
  415. spin_unlock(&pd_chan->lock);
  416. }
  417. }
  418. static int pd_alloc_chan_resources(struct dma_chan *chan)
  419. {
  420. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  421. struct pch_dma_desc *desc;
  422. LIST_HEAD(tmp_list);
  423. int i;
  424. if (!pdc_is_idle(pd_chan)) {
  425. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  426. return -EIO;
  427. }
  428. if (!list_empty(&pd_chan->free_list))
  429. return pd_chan->descs_allocated;
  430. for (i = 0; i < init_nr_desc_per_channel; i++) {
  431. desc = pdc_alloc_desc(chan, GFP_KERNEL);
  432. if (!desc) {
  433. dev_warn(chan2dev(chan),
  434. "Only allocated %d initial descriptors\n", i);
  435. break;
  436. }
  437. list_add_tail(&desc->desc_node, &tmp_list);
  438. }
  439. spin_lock_irq(&pd_chan->lock);
  440. list_splice(&tmp_list, &pd_chan->free_list);
  441. pd_chan->descs_allocated = i;
  442. dma_cookie_init(chan);
  443. spin_unlock_irq(&pd_chan->lock);
  444. pdc_enable_irq(chan, 1);
  445. return pd_chan->descs_allocated;
  446. }
  447. static void pd_free_chan_resources(struct dma_chan *chan)
  448. {
  449. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  450. struct pch_dma *pd = to_pd(chan->device);
  451. struct pch_dma_desc *desc, *_d;
  452. LIST_HEAD(tmp_list);
  453. BUG_ON(!pdc_is_idle(pd_chan));
  454. BUG_ON(!list_empty(&pd_chan->active_list));
  455. BUG_ON(!list_empty(&pd_chan->queue));
  456. spin_lock_irq(&pd_chan->lock);
  457. list_splice_init(&pd_chan->free_list, &tmp_list);
  458. pd_chan->descs_allocated = 0;
  459. spin_unlock_irq(&pd_chan->lock);
  460. list_for_each_entry_safe(desc, _d, &tmp_list, desc_node)
  461. pci_pool_free(pd->pool, desc, desc->txd.phys);
  462. pdc_enable_irq(chan, 0);
  463. }
  464. static enum dma_status pd_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  465. struct dma_tx_state *txstate)
  466. {
  467. return dma_cookie_status(chan, cookie, txstate);
  468. }
  469. static void pd_issue_pending(struct dma_chan *chan)
  470. {
  471. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  472. if (pdc_is_idle(pd_chan)) {
  473. spin_lock(&pd_chan->lock);
  474. pdc_advance_work(pd_chan);
  475. spin_unlock(&pd_chan->lock);
  476. }
  477. }
  478. static struct dma_async_tx_descriptor *pd_prep_slave_sg(struct dma_chan *chan,
  479. struct scatterlist *sgl, unsigned int sg_len,
  480. enum dma_transfer_direction direction, unsigned long flags,
  481. void *context)
  482. {
  483. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  484. struct pch_dma_slave *pd_slave = chan->private;
  485. struct pch_dma_desc *first = NULL;
  486. struct pch_dma_desc *prev = NULL;
  487. struct pch_dma_desc *desc = NULL;
  488. struct scatterlist *sg;
  489. dma_addr_t reg;
  490. int i;
  491. if (unlikely(!sg_len)) {
  492. dev_info(chan2dev(chan), "prep_slave_sg: length is zero!\n");
  493. return NULL;
  494. }
  495. if (direction == DMA_DEV_TO_MEM)
  496. reg = pd_slave->rx_reg;
  497. else if (direction == DMA_MEM_TO_DEV)
  498. reg = pd_slave->tx_reg;
  499. else
  500. return NULL;
  501. pd_chan->dir = direction;
  502. pdc_set_dir(chan);
  503. for_each_sg(sgl, sg, sg_len, i) {
  504. desc = pdc_desc_get(pd_chan);
  505. if (!desc)
  506. goto err_desc_get;
  507. desc->regs.dev_addr = reg;
  508. desc->regs.mem_addr = sg_dma_address(sg);
  509. desc->regs.size = sg_dma_len(sg);
  510. desc->regs.next = DMA_DESC_FOLLOW_WITHOUT_IRQ;
  511. switch (pd_slave->width) {
  512. case PCH_DMA_WIDTH_1_BYTE:
  513. if (desc->regs.size > DMA_DESC_MAX_COUNT_1_BYTE)
  514. goto err_desc_get;
  515. desc->regs.size |= DMA_DESC_WIDTH_1_BYTE;
  516. break;
  517. case PCH_DMA_WIDTH_2_BYTES:
  518. if (desc->regs.size > DMA_DESC_MAX_COUNT_2_BYTES)
  519. goto err_desc_get;
  520. desc->regs.size |= DMA_DESC_WIDTH_2_BYTES;
  521. break;
  522. case PCH_DMA_WIDTH_4_BYTES:
  523. if (desc->regs.size > DMA_DESC_MAX_COUNT_4_BYTES)
  524. goto err_desc_get;
  525. desc->regs.size |= DMA_DESC_WIDTH_4_BYTES;
  526. break;
  527. default:
  528. goto err_desc_get;
  529. }
  530. if (!first) {
  531. first = desc;
  532. } else {
  533. prev->regs.next |= desc->txd.phys;
  534. list_add_tail(&desc->desc_node, &first->tx_list);
  535. }
  536. prev = desc;
  537. }
  538. if (flags & DMA_PREP_INTERRUPT)
  539. desc->regs.next = DMA_DESC_END_WITH_IRQ;
  540. else
  541. desc->regs.next = DMA_DESC_END_WITHOUT_IRQ;
  542. first->txd.cookie = -EBUSY;
  543. desc->txd.flags = flags;
  544. return &first->txd;
  545. err_desc_get:
  546. dev_err(chan2dev(chan), "failed to get desc or wrong parameters\n");
  547. pdc_desc_put(pd_chan, first);
  548. return NULL;
  549. }
  550. static int pd_device_terminate_all(struct dma_chan *chan)
  551. {
  552. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  553. struct pch_dma_desc *desc, *_d;
  554. LIST_HEAD(list);
  555. spin_lock_irq(&pd_chan->lock);
  556. pdc_set_mode(&pd_chan->chan, DMA_CTL0_DISABLE);
  557. list_splice_init(&pd_chan->active_list, &list);
  558. list_splice_init(&pd_chan->queue, &list);
  559. list_for_each_entry_safe(desc, _d, &list, desc_node)
  560. pdc_chain_complete(pd_chan, desc);
  561. spin_unlock_irq(&pd_chan->lock);
  562. return 0;
  563. }
  564. static void pdc_tasklet(unsigned long data)
  565. {
  566. struct pch_dma_chan *pd_chan = (struct pch_dma_chan *)data;
  567. unsigned long flags;
  568. if (!pdc_is_idle(pd_chan)) {
  569. dev_err(chan2dev(&pd_chan->chan),
  570. "BUG: handle non-idle channel in tasklet\n");
  571. return;
  572. }
  573. spin_lock_irqsave(&pd_chan->lock, flags);
  574. if (test_and_clear_bit(0, &pd_chan->err_status))
  575. pdc_handle_error(pd_chan);
  576. else
  577. pdc_advance_work(pd_chan);
  578. spin_unlock_irqrestore(&pd_chan->lock, flags);
  579. }
  580. static irqreturn_t pd_irq(int irq, void *devid)
  581. {
  582. struct pch_dma *pd = (struct pch_dma *)devid;
  583. struct pch_dma_chan *pd_chan;
  584. u32 sts0;
  585. u32 sts2;
  586. int i;
  587. int ret0 = IRQ_NONE;
  588. int ret2 = IRQ_NONE;
  589. sts0 = dma_readl(pd, STS0);
  590. sts2 = dma_readl(pd, STS2);
  591. dev_dbg(pd->dma.dev, "pd_irq sts0: %x\n", sts0);
  592. for (i = 0; i < pd->dma.chancnt; i++) {
  593. pd_chan = &pd->channels[i];
  594. if (i < 8) {
  595. if (sts0 & DMA_STATUS_IRQ(i)) {
  596. if (sts0 & DMA_STATUS0_ERR(i))
  597. set_bit(0, &pd_chan->err_status);
  598. tasklet_schedule(&pd_chan->tasklet);
  599. ret0 = IRQ_HANDLED;
  600. }
  601. } else {
  602. if (sts2 & DMA_STATUS_IRQ(i - 8)) {
  603. if (sts2 & DMA_STATUS2_ERR(i))
  604. set_bit(0, &pd_chan->err_status);
  605. tasklet_schedule(&pd_chan->tasklet);
  606. ret2 = IRQ_HANDLED;
  607. }
  608. }
  609. }
  610. /* clear interrupt bits in status register */
  611. if (ret0)
  612. dma_writel(pd, STS0, sts0);
  613. if (ret2)
  614. dma_writel(pd, STS2, sts2);
  615. return ret0 | ret2;
  616. }
  617. #ifdef CONFIG_PM
  618. static void pch_dma_save_regs(struct pch_dma *pd)
  619. {
  620. struct pch_dma_chan *pd_chan;
  621. struct dma_chan *chan, *_c;
  622. int i = 0;
  623. pd->regs.dma_ctl0 = dma_readl(pd, CTL0);
  624. pd->regs.dma_ctl1 = dma_readl(pd, CTL1);
  625. pd->regs.dma_ctl2 = dma_readl(pd, CTL2);
  626. pd->regs.dma_ctl3 = dma_readl(pd, CTL3);
  627. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  628. pd_chan = to_pd_chan(chan);
  629. pd->ch_regs[i].dev_addr = channel_readl(pd_chan, DEV_ADDR);
  630. pd->ch_regs[i].mem_addr = channel_readl(pd_chan, MEM_ADDR);
  631. pd->ch_regs[i].size = channel_readl(pd_chan, SIZE);
  632. pd->ch_regs[i].next = channel_readl(pd_chan, NEXT);
  633. i++;
  634. }
  635. }
  636. static void pch_dma_restore_regs(struct pch_dma *pd)
  637. {
  638. struct pch_dma_chan *pd_chan;
  639. struct dma_chan *chan, *_c;
  640. int i = 0;
  641. dma_writel(pd, CTL0, pd->regs.dma_ctl0);
  642. dma_writel(pd, CTL1, pd->regs.dma_ctl1);
  643. dma_writel(pd, CTL2, pd->regs.dma_ctl2);
  644. dma_writel(pd, CTL3, pd->regs.dma_ctl3);
  645. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  646. pd_chan = to_pd_chan(chan);
  647. channel_writel(pd_chan, DEV_ADDR, pd->ch_regs[i].dev_addr);
  648. channel_writel(pd_chan, MEM_ADDR, pd->ch_regs[i].mem_addr);
  649. channel_writel(pd_chan, SIZE, pd->ch_regs[i].size);
  650. channel_writel(pd_chan, NEXT, pd->ch_regs[i].next);
  651. i++;
  652. }
  653. }
  654. static int pch_dma_suspend(struct pci_dev *pdev, pm_message_t state)
  655. {
  656. struct pch_dma *pd = pci_get_drvdata(pdev);
  657. if (pd)
  658. pch_dma_save_regs(pd);
  659. pci_save_state(pdev);
  660. pci_disable_device(pdev);
  661. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  662. return 0;
  663. }
  664. static int pch_dma_resume(struct pci_dev *pdev)
  665. {
  666. struct pch_dma *pd = pci_get_drvdata(pdev);
  667. int err;
  668. pci_set_power_state(pdev, PCI_D0);
  669. pci_restore_state(pdev);
  670. err = pci_enable_device(pdev);
  671. if (err) {
  672. dev_dbg(&pdev->dev, "failed to enable device\n");
  673. return err;
  674. }
  675. if (pd)
  676. pch_dma_restore_regs(pd);
  677. return 0;
  678. }
  679. #endif
  680. static int pch_dma_probe(struct pci_dev *pdev,
  681. const struct pci_device_id *id)
  682. {
  683. struct pch_dma *pd;
  684. struct pch_dma_regs *regs;
  685. unsigned int nr_channels;
  686. int err;
  687. int i;
  688. nr_channels = id->driver_data;
  689. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  690. if (!pd)
  691. return -ENOMEM;
  692. pci_set_drvdata(pdev, pd);
  693. err = pci_enable_device(pdev);
  694. if (err) {
  695. dev_err(&pdev->dev, "Cannot enable PCI device\n");
  696. goto err_free_mem;
  697. }
  698. if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  699. dev_err(&pdev->dev, "Cannot find proper base address\n");
  700. err = -ENODEV;
  701. goto err_disable_pdev;
  702. }
  703. err = pci_request_regions(pdev, DRV_NAME);
  704. if (err) {
  705. dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
  706. goto err_disable_pdev;
  707. }
  708. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  709. if (err) {
  710. dev_err(&pdev->dev, "Cannot set proper DMA config\n");
  711. goto err_free_res;
  712. }
  713. regs = pd->membase = pci_iomap(pdev, 1, 0);
  714. if (!pd->membase) {
  715. dev_err(&pdev->dev, "Cannot map MMIO registers\n");
  716. err = -ENOMEM;
  717. goto err_free_res;
  718. }
  719. pci_set_master(pdev);
  720. err = request_irq(pdev->irq, pd_irq, IRQF_SHARED, DRV_NAME, pd);
  721. if (err) {
  722. dev_err(&pdev->dev, "Failed to request IRQ\n");
  723. goto err_iounmap;
  724. }
  725. pd->pool = pci_pool_create("pch_dma_desc_pool", pdev,
  726. sizeof(struct pch_dma_desc), 4, 0);
  727. if (!pd->pool) {
  728. dev_err(&pdev->dev, "Failed to alloc DMA descriptors\n");
  729. err = -ENOMEM;
  730. goto err_free_irq;
  731. }
  732. pd->dma.dev = &pdev->dev;
  733. INIT_LIST_HEAD(&pd->dma.channels);
  734. for (i = 0; i < nr_channels; i++) {
  735. struct pch_dma_chan *pd_chan = &pd->channels[i];
  736. pd_chan->chan.device = &pd->dma;
  737. dma_cookie_init(&pd_chan->chan);
  738. pd_chan->membase = &regs->desc[i];
  739. spin_lock_init(&pd_chan->lock);
  740. INIT_LIST_HEAD(&pd_chan->active_list);
  741. INIT_LIST_HEAD(&pd_chan->queue);
  742. INIT_LIST_HEAD(&pd_chan->free_list);
  743. tasklet_init(&pd_chan->tasklet, pdc_tasklet,
  744. (unsigned long)pd_chan);
  745. list_add_tail(&pd_chan->chan.device_node, &pd->dma.channels);
  746. }
  747. dma_cap_zero(pd->dma.cap_mask);
  748. dma_cap_set(DMA_PRIVATE, pd->dma.cap_mask);
  749. dma_cap_set(DMA_SLAVE, pd->dma.cap_mask);
  750. pd->dma.device_alloc_chan_resources = pd_alloc_chan_resources;
  751. pd->dma.device_free_chan_resources = pd_free_chan_resources;
  752. pd->dma.device_tx_status = pd_tx_status;
  753. pd->dma.device_issue_pending = pd_issue_pending;
  754. pd->dma.device_prep_slave_sg = pd_prep_slave_sg;
  755. pd->dma.device_terminate_all = pd_device_terminate_all;
  756. err = dma_async_device_register(&pd->dma);
  757. if (err) {
  758. dev_err(&pdev->dev, "Failed to register DMA device\n");
  759. goto err_free_pool;
  760. }
  761. return 0;
  762. err_free_pool:
  763. pci_pool_destroy(pd->pool);
  764. err_free_irq:
  765. free_irq(pdev->irq, pd);
  766. err_iounmap:
  767. pci_iounmap(pdev, pd->membase);
  768. err_free_res:
  769. pci_release_regions(pdev);
  770. err_disable_pdev:
  771. pci_disable_device(pdev);
  772. err_free_mem:
  773. kfree(pd);
  774. return err;
  775. }
  776. static void pch_dma_remove(struct pci_dev *pdev)
  777. {
  778. struct pch_dma *pd = pci_get_drvdata(pdev);
  779. struct pch_dma_chan *pd_chan;
  780. struct dma_chan *chan, *_c;
  781. if (pd) {
  782. dma_async_device_unregister(&pd->dma);
  783. free_irq(pdev->irq, pd);
  784. list_for_each_entry_safe(chan, _c, &pd->dma.channels,
  785. device_node) {
  786. pd_chan = to_pd_chan(chan);
  787. tasklet_kill(&pd_chan->tasklet);
  788. }
  789. pci_pool_destroy(pd->pool);
  790. pci_iounmap(pdev, pd->membase);
  791. pci_release_regions(pdev);
  792. pci_disable_device(pdev);
  793. kfree(pd);
  794. }
  795. }
  796. /* PCI Device ID of DMA device */
  797. #define PCI_VENDOR_ID_ROHM 0x10DB
  798. #define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
  799. #define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
  800. #define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
  801. #define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
  802. #define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
  803. #define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
  804. #define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
  805. #define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
  806. #define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
  807. #define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
  808. #define PCI_DEVICE_ID_ML7831_DMA1_8CH 0x8810
  809. #define PCI_DEVICE_ID_ML7831_DMA2_4CH 0x8815
  810. static const struct pci_device_id pch_dma_id_table[] = {
  811. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_8CH), 8 },
  812. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_4CH), 4 },
  813. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA1_8CH), 8}, /* UART Video */
  814. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA2_8CH), 8}, /* PCMIF SPI */
  815. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA3_4CH), 4}, /* FPGA */
  816. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA4_12CH), 12}, /* I2S */
  817. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA1_4CH), 4}, /* UART */
  818. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA2_4CH), 4}, /* Video SPI */
  819. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA3_4CH), 4}, /* Security */
  820. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA4_4CH), 4}, /* FPGA */
  821. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA1_8CH), 8}, /* UART */
  822. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA2_4CH), 4}, /* SPI */
  823. { 0, },
  824. };
  825. static struct pci_driver pch_dma_driver = {
  826. .name = DRV_NAME,
  827. .id_table = pch_dma_id_table,
  828. .probe = pch_dma_probe,
  829. .remove = pch_dma_remove,
  830. #ifdef CONFIG_PM
  831. .suspend = pch_dma_suspend,
  832. .resume = pch_dma_resume,
  833. #endif
  834. };
  835. module_pci_driver(pch_dma_driver);
  836. MODULE_DESCRIPTION("Intel EG20T PCH / LAPIS Semicon ML7213/ML7223/ML7831 IOH "
  837. "DMA controller driver");
  838. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  839. MODULE_LICENSE("GPL v2");
  840. MODULE_DEVICE_TABLE(pci, pch_dma_id_table);