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. dma_async_tx_callback callback = txd->callback;
  306. void *param = txd->callback_param;
  307. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  308. list_move(&desc->desc_node, &pd_chan->free_list);
  309. if (callback)
  310. callback(param);
  311. }
  312. static void pdc_complete_all(struct pch_dma_chan *pd_chan)
  313. {
  314. struct pch_dma_desc *desc, *_d;
  315. LIST_HEAD(list);
  316. BUG_ON(!pdc_is_idle(pd_chan));
  317. if (!list_empty(&pd_chan->queue))
  318. pdc_dostart(pd_chan, pdc_first_queued(pd_chan));
  319. list_splice_init(&pd_chan->active_list, &list);
  320. list_splice_init(&pd_chan->queue, &pd_chan->active_list);
  321. list_for_each_entry_safe(desc, _d, &list, desc_node)
  322. pdc_chain_complete(pd_chan, desc);
  323. }
  324. static void pdc_handle_error(struct pch_dma_chan *pd_chan)
  325. {
  326. struct pch_dma_desc *bad_desc;
  327. bad_desc = pdc_first_active(pd_chan);
  328. list_del(&bad_desc->desc_node);
  329. list_splice_init(&pd_chan->queue, pd_chan->active_list.prev);
  330. if (!list_empty(&pd_chan->active_list))
  331. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  332. dev_crit(chan2dev(&pd_chan->chan), "Bad descriptor submitted\n");
  333. dev_crit(chan2dev(&pd_chan->chan), "descriptor cookie: %d\n",
  334. bad_desc->txd.cookie);
  335. pdc_chain_complete(pd_chan, bad_desc);
  336. }
  337. static void pdc_advance_work(struct pch_dma_chan *pd_chan)
  338. {
  339. if (list_empty(&pd_chan->active_list) ||
  340. list_is_singular(&pd_chan->active_list)) {
  341. pdc_complete_all(pd_chan);
  342. } else {
  343. pdc_chain_complete(pd_chan, pdc_first_active(pd_chan));
  344. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  345. }
  346. }
  347. static dma_cookie_t pd_tx_submit(struct dma_async_tx_descriptor *txd)
  348. {
  349. struct pch_dma_desc *desc = to_pd_desc(txd);
  350. struct pch_dma_chan *pd_chan = to_pd_chan(txd->chan);
  351. dma_cookie_t cookie;
  352. spin_lock(&pd_chan->lock);
  353. cookie = dma_cookie_assign(txd);
  354. if (list_empty(&pd_chan->active_list)) {
  355. list_add_tail(&desc->desc_node, &pd_chan->active_list);
  356. pdc_dostart(pd_chan, desc);
  357. } else {
  358. list_add_tail(&desc->desc_node, &pd_chan->queue);
  359. }
  360. spin_unlock(&pd_chan->lock);
  361. return 0;
  362. }
  363. static struct pch_dma_desc *pdc_alloc_desc(struct dma_chan *chan, gfp_t flags)
  364. {
  365. struct pch_dma_desc *desc = NULL;
  366. struct pch_dma *pd = to_pd(chan->device);
  367. dma_addr_t addr;
  368. desc = pci_pool_alloc(pd->pool, flags, &addr);
  369. if (desc) {
  370. memset(desc, 0, sizeof(struct pch_dma_desc));
  371. INIT_LIST_HEAD(&desc->tx_list);
  372. dma_async_tx_descriptor_init(&desc->txd, chan);
  373. desc->txd.tx_submit = pd_tx_submit;
  374. desc->txd.flags = DMA_CTRL_ACK;
  375. desc->txd.phys = addr;
  376. }
  377. return desc;
  378. }
  379. static struct pch_dma_desc *pdc_desc_get(struct pch_dma_chan *pd_chan)
  380. {
  381. struct pch_dma_desc *desc, *_d;
  382. struct pch_dma_desc *ret = NULL;
  383. int i = 0;
  384. spin_lock(&pd_chan->lock);
  385. list_for_each_entry_safe(desc, _d, &pd_chan->free_list, desc_node) {
  386. i++;
  387. if (async_tx_test_ack(&desc->txd)) {
  388. list_del(&desc->desc_node);
  389. ret = desc;
  390. break;
  391. }
  392. dev_dbg(chan2dev(&pd_chan->chan), "desc %p not ACKed\n", desc);
  393. }
  394. spin_unlock(&pd_chan->lock);
  395. dev_dbg(chan2dev(&pd_chan->chan), "scanned %d descriptors\n", i);
  396. if (!ret) {
  397. ret = pdc_alloc_desc(&pd_chan->chan, GFP_ATOMIC);
  398. if (ret) {
  399. spin_lock(&pd_chan->lock);
  400. pd_chan->descs_allocated++;
  401. spin_unlock(&pd_chan->lock);
  402. } else {
  403. dev_err(chan2dev(&pd_chan->chan),
  404. "failed to alloc desc\n");
  405. }
  406. }
  407. return ret;
  408. }
  409. static void pdc_desc_put(struct pch_dma_chan *pd_chan,
  410. struct pch_dma_desc *desc)
  411. {
  412. if (desc) {
  413. spin_lock(&pd_chan->lock);
  414. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  415. list_add(&desc->desc_node, &pd_chan->free_list);
  416. spin_unlock(&pd_chan->lock);
  417. }
  418. }
  419. static int pd_alloc_chan_resources(struct dma_chan *chan)
  420. {
  421. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  422. struct pch_dma_desc *desc;
  423. LIST_HEAD(tmp_list);
  424. int i;
  425. if (!pdc_is_idle(pd_chan)) {
  426. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  427. return -EIO;
  428. }
  429. if (!list_empty(&pd_chan->free_list))
  430. return pd_chan->descs_allocated;
  431. for (i = 0; i < init_nr_desc_per_channel; i++) {
  432. desc = pdc_alloc_desc(chan, GFP_KERNEL);
  433. if (!desc) {
  434. dev_warn(chan2dev(chan),
  435. "Only allocated %d initial descriptors\n", i);
  436. break;
  437. }
  438. list_add_tail(&desc->desc_node, &tmp_list);
  439. }
  440. spin_lock_irq(&pd_chan->lock);
  441. list_splice(&tmp_list, &pd_chan->free_list);
  442. pd_chan->descs_allocated = i;
  443. dma_cookie_init(chan);
  444. spin_unlock_irq(&pd_chan->lock);
  445. pdc_enable_irq(chan, 1);
  446. return pd_chan->descs_allocated;
  447. }
  448. static void pd_free_chan_resources(struct dma_chan *chan)
  449. {
  450. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  451. struct pch_dma *pd = to_pd(chan->device);
  452. struct pch_dma_desc *desc, *_d;
  453. LIST_HEAD(tmp_list);
  454. BUG_ON(!pdc_is_idle(pd_chan));
  455. BUG_ON(!list_empty(&pd_chan->active_list));
  456. BUG_ON(!list_empty(&pd_chan->queue));
  457. spin_lock_irq(&pd_chan->lock);
  458. list_splice_init(&pd_chan->free_list, &tmp_list);
  459. pd_chan->descs_allocated = 0;
  460. spin_unlock_irq(&pd_chan->lock);
  461. list_for_each_entry_safe(desc, _d, &tmp_list, desc_node)
  462. pci_pool_free(pd->pool, desc, desc->txd.phys);
  463. pdc_enable_irq(chan, 0);
  464. }
  465. static enum dma_status pd_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  466. struct dma_tx_state *txstate)
  467. {
  468. return dma_cookie_status(chan, cookie, txstate);
  469. }
  470. static void pd_issue_pending(struct dma_chan *chan)
  471. {
  472. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  473. if (pdc_is_idle(pd_chan)) {
  474. spin_lock(&pd_chan->lock);
  475. pdc_advance_work(pd_chan);
  476. spin_unlock(&pd_chan->lock);
  477. }
  478. }
  479. static struct dma_async_tx_descriptor *pd_prep_slave_sg(struct dma_chan *chan,
  480. struct scatterlist *sgl, unsigned int sg_len,
  481. enum dma_transfer_direction direction, unsigned long flags,
  482. void *context)
  483. {
  484. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  485. struct pch_dma_slave *pd_slave = chan->private;
  486. struct pch_dma_desc *first = NULL;
  487. struct pch_dma_desc *prev = NULL;
  488. struct pch_dma_desc *desc = NULL;
  489. struct scatterlist *sg;
  490. dma_addr_t reg;
  491. int i;
  492. if (unlikely(!sg_len)) {
  493. dev_info(chan2dev(chan), "prep_slave_sg: length is zero!\n");
  494. return NULL;
  495. }
  496. if (direction == DMA_DEV_TO_MEM)
  497. reg = pd_slave->rx_reg;
  498. else if (direction == DMA_MEM_TO_DEV)
  499. reg = pd_slave->tx_reg;
  500. else
  501. return NULL;
  502. pd_chan->dir = direction;
  503. pdc_set_dir(chan);
  504. for_each_sg(sgl, sg, sg_len, i) {
  505. desc = pdc_desc_get(pd_chan);
  506. if (!desc)
  507. goto err_desc_get;
  508. desc->regs.dev_addr = reg;
  509. desc->regs.mem_addr = sg_dma_address(sg);
  510. desc->regs.size = sg_dma_len(sg);
  511. desc->regs.next = DMA_DESC_FOLLOW_WITHOUT_IRQ;
  512. switch (pd_slave->width) {
  513. case PCH_DMA_WIDTH_1_BYTE:
  514. if (desc->regs.size > DMA_DESC_MAX_COUNT_1_BYTE)
  515. goto err_desc_get;
  516. desc->regs.size |= DMA_DESC_WIDTH_1_BYTE;
  517. break;
  518. case PCH_DMA_WIDTH_2_BYTES:
  519. if (desc->regs.size > DMA_DESC_MAX_COUNT_2_BYTES)
  520. goto err_desc_get;
  521. desc->regs.size |= DMA_DESC_WIDTH_2_BYTES;
  522. break;
  523. case PCH_DMA_WIDTH_4_BYTES:
  524. if (desc->regs.size > DMA_DESC_MAX_COUNT_4_BYTES)
  525. goto err_desc_get;
  526. desc->regs.size |= DMA_DESC_WIDTH_4_BYTES;
  527. break;
  528. default:
  529. goto err_desc_get;
  530. }
  531. if (!first) {
  532. first = desc;
  533. } else {
  534. prev->regs.next |= desc->txd.phys;
  535. list_add_tail(&desc->desc_node, &first->tx_list);
  536. }
  537. prev = desc;
  538. }
  539. if (flags & DMA_PREP_INTERRUPT)
  540. desc->regs.next = DMA_DESC_END_WITH_IRQ;
  541. else
  542. desc->regs.next = DMA_DESC_END_WITHOUT_IRQ;
  543. first->txd.cookie = -EBUSY;
  544. desc->txd.flags = flags;
  545. return &first->txd;
  546. err_desc_get:
  547. dev_err(chan2dev(chan), "failed to get desc or wrong parameters\n");
  548. pdc_desc_put(pd_chan, first);
  549. return NULL;
  550. }
  551. static int pd_device_terminate_all(struct dma_chan *chan)
  552. {
  553. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  554. struct pch_dma_desc *desc, *_d;
  555. LIST_HEAD(list);
  556. spin_lock_irq(&pd_chan->lock);
  557. pdc_set_mode(&pd_chan->chan, DMA_CTL0_DISABLE);
  558. list_splice_init(&pd_chan->active_list, &list);
  559. list_splice_init(&pd_chan->queue, &list);
  560. list_for_each_entry_safe(desc, _d, &list, desc_node)
  561. pdc_chain_complete(pd_chan, desc);
  562. spin_unlock_irq(&pd_chan->lock);
  563. return 0;
  564. }
  565. static void pdc_tasklet(unsigned long data)
  566. {
  567. struct pch_dma_chan *pd_chan = (struct pch_dma_chan *)data;
  568. unsigned long flags;
  569. if (!pdc_is_idle(pd_chan)) {
  570. dev_err(chan2dev(&pd_chan->chan),
  571. "BUG: handle non-idle channel in tasklet\n");
  572. return;
  573. }
  574. spin_lock_irqsave(&pd_chan->lock, flags);
  575. if (test_and_clear_bit(0, &pd_chan->err_status))
  576. pdc_handle_error(pd_chan);
  577. else
  578. pdc_advance_work(pd_chan);
  579. spin_unlock_irqrestore(&pd_chan->lock, flags);
  580. }
  581. static irqreturn_t pd_irq(int irq, void *devid)
  582. {
  583. struct pch_dma *pd = (struct pch_dma *)devid;
  584. struct pch_dma_chan *pd_chan;
  585. u32 sts0;
  586. u32 sts2;
  587. int i;
  588. int ret0 = IRQ_NONE;
  589. int ret2 = IRQ_NONE;
  590. sts0 = dma_readl(pd, STS0);
  591. sts2 = dma_readl(pd, STS2);
  592. dev_dbg(pd->dma.dev, "pd_irq sts0: %x\n", sts0);
  593. for (i = 0; i < pd->dma.chancnt; i++) {
  594. pd_chan = &pd->channels[i];
  595. if (i < 8) {
  596. if (sts0 & DMA_STATUS_IRQ(i)) {
  597. if (sts0 & DMA_STATUS0_ERR(i))
  598. set_bit(0, &pd_chan->err_status);
  599. tasklet_schedule(&pd_chan->tasklet);
  600. ret0 = IRQ_HANDLED;
  601. }
  602. } else {
  603. if (sts2 & DMA_STATUS_IRQ(i - 8)) {
  604. if (sts2 & DMA_STATUS2_ERR(i))
  605. set_bit(0, &pd_chan->err_status);
  606. tasklet_schedule(&pd_chan->tasklet);
  607. ret2 = IRQ_HANDLED;
  608. }
  609. }
  610. }
  611. /* clear interrupt bits in status register */
  612. if (ret0)
  613. dma_writel(pd, STS0, sts0);
  614. if (ret2)
  615. dma_writel(pd, STS2, sts2);
  616. return ret0 | ret2;
  617. }
  618. #ifdef CONFIG_PM
  619. static void pch_dma_save_regs(struct pch_dma *pd)
  620. {
  621. struct pch_dma_chan *pd_chan;
  622. struct dma_chan *chan, *_c;
  623. int i = 0;
  624. pd->regs.dma_ctl0 = dma_readl(pd, CTL0);
  625. pd->regs.dma_ctl1 = dma_readl(pd, CTL1);
  626. pd->regs.dma_ctl2 = dma_readl(pd, CTL2);
  627. pd->regs.dma_ctl3 = dma_readl(pd, CTL3);
  628. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  629. pd_chan = to_pd_chan(chan);
  630. pd->ch_regs[i].dev_addr = channel_readl(pd_chan, DEV_ADDR);
  631. pd->ch_regs[i].mem_addr = channel_readl(pd_chan, MEM_ADDR);
  632. pd->ch_regs[i].size = channel_readl(pd_chan, SIZE);
  633. pd->ch_regs[i].next = channel_readl(pd_chan, NEXT);
  634. i++;
  635. }
  636. }
  637. static void pch_dma_restore_regs(struct pch_dma *pd)
  638. {
  639. struct pch_dma_chan *pd_chan;
  640. struct dma_chan *chan, *_c;
  641. int i = 0;
  642. dma_writel(pd, CTL0, pd->regs.dma_ctl0);
  643. dma_writel(pd, CTL1, pd->regs.dma_ctl1);
  644. dma_writel(pd, CTL2, pd->regs.dma_ctl2);
  645. dma_writel(pd, CTL3, pd->regs.dma_ctl3);
  646. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  647. pd_chan = to_pd_chan(chan);
  648. channel_writel(pd_chan, DEV_ADDR, pd->ch_regs[i].dev_addr);
  649. channel_writel(pd_chan, MEM_ADDR, pd->ch_regs[i].mem_addr);
  650. channel_writel(pd_chan, SIZE, pd->ch_regs[i].size);
  651. channel_writel(pd_chan, NEXT, pd->ch_regs[i].next);
  652. i++;
  653. }
  654. }
  655. static int pch_dma_suspend(struct pci_dev *pdev, pm_message_t state)
  656. {
  657. struct pch_dma *pd = pci_get_drvdata(pdev);
  658. if (pd)
  659. pch_dma_save_regs(pd);
  660. pci_save_state(pdev);
  661. pci_disable_device(pdev);
  662. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  663. return 0;
  664. }
  665. static int pch_dma_resume(struct pci_dev *pdev)
  666. {
  667. struct pch_dma *pd = pci_get_drvdata(pdev);
  668. int err;
  669. pci_set_power_state(pdev, PCI_D0);
  670. pci_restore_state(pdev);
  671. err = pci_enable_device(pdev);
  672. if (err) {
  673. dev_dbg(&pdev->dev, "failed to enable device\n");
  674. return err;
  675. }
  676. if (pd)
  677. pch_dma_restore_regs(pd);
  678. return 0;
  679. }
  680. #endif
  681. static int pch_dma_probe(struct pci_dev *pdev,
  682. const struct pci_device_id *id)
  683. {
  684. struct pch_dma *pd;
  685. struct pch_dma_regs *regs;
  686. unsigned int nr_channels;
  687. int err;
  688. int i;
  689. nr_channels = id->driver_data;
  690. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  691. if (!pd)
  692. return -ENOMEM;
  693. pci_set_drvdata(pdev, pd);
  694. err = pci_enable_device(pdev);
  695. if (err) {
  696. dev_err(&pdev->dev, "Cannot enable PCI device\n");
  697. goto err_free_mem;
  698. }
  699. if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  700. dev_err(&pdev->dev, "Cannot find proper base address\n");
  701. err = -ENODEV;
  702. goto err_disable_pdev;
  703. }
  704. err = pci_request_regions(pdev, DRV_NAME);
  705. if (err) {
  706. dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
  707. goto err_disable_pdev;
  708. }
  709. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  710. if (err) {
  711. dev_err(&pdev->dev, "Cannot set proper DMA config\n");
  712. goto err_free_res;
  713. }
  714. regs = pd->membase = pci_iomap(pdev, 1, 0);
  715. if (!pd->membase) {
  716. dev_err(&pdev->dev, "Cannot map MMIO registers\n");
  717. err = -ENOMEM;
  718. goto err_free_res;
  719. }
  720. pci_set_master(pdev);
  721. err = request_irq(pdev->irq, pd_irq, IRQF_SHARED, DRV_NAME, pd);
  722. if (err) {
  723. dev_err(&pdev->dev, "Failed to request IRQ\n");
  724. goto err_iounmap;
  725. }
  726. pd->pool = pci_pool_create("pch_dma_desc_pool", pdev,
  727. sizeof(struct pch_dma_desc), 4, 0);
  728. if (!pd->pool) {
  729. dev_err(&pdev->dev, "Failed to alloc DMA descriptors\n");
  730. err = -ENOMEM;
  731. goto err_free_irq;
  732. }
  733. pd->dma.dev = &pdev->dev;
  734. INIT_LIST_HEAD(&pd->dma.channels);
  735. for (i = 0; i < nr_channels; i++) {
  736. struct pch_dma_chan *pd_chan = &pd->channels[i];
  737. pd_chan->chan.device = &pd->dma;
  738. dma_cookie_init(&pd_chan->chan);
  739. pd_chan->membase = &regs->desc[i];
  740. spin_lock_init(&pd_chan->lock);
  741. INIT_LIST_HEAD(&pd_chan->active_list);
  742. INIT_LIST_HEAD(&pd_chan->queue);
  743. INIT_LIST_HEAD(&pd_chan->free_list);
  744. tasklet_init(&pd_chan->tasklet, pdc_tasklet,
  745. (unsigned long)pd_chan);
  746. list_add_tail(&pd_chan->chan.device_node, &pd->dma.channels);
  747. }
  748. dma_cap_zero(pd->dma.cap_mask);
  749. dma_cap_set(DMA_PRIVATE, pd->dma.cap_mask);
  750. dma_cap_set(DMA_SLAVE, pd->dma.cap_mask);
  751. pd->dma.device_alloc_chan_resources = pd_alloc_chan_resources;
  752. pd->dma.device_free_chan_resources = pd_free_chan_resources;
  753. pd->dma.device_tx_status = pd_tx_status;
  754. pd->dma.device_issue_pending = pd_issue_pending;
  755. pd->dma.device_prep_slave_sg = pd_prep_slave_sg;
  756. pd->dma.device_terminate_all = pd_device_terminate_all;
  757. err = dma_async_device_register(&pd->dma);
  758. if (err) {
  759. dev_err(&pdev->dev, "Failed to register DMA device\n");
  760. goto err_free_pool;
  761. }
  762. return 0;
  763. err_free_pool:
  764. pci_pool_destroy(pd->pool);
  765. err_free_irq:
  766. free_irq(pdev->irq, pd);
  767. err_iounmap:
  768. pci_iounmap(pdev, pd->membase);
  769. err_free_res:
  770. pci_release_regions(pdev);
  771. err_disable_pdev:
  772. pci_disable_device(pdev);
  773. err_free_mem:
  774. kfree(pd);
  775. return err;
  776. }
  777. static void pch_dma_remove(struct pci_dev *pdev)
  778. {
  779. struct pch_dma *pd = pci_get_drvdata(pdev);
  780. struct pch_dma_chan *pd_chan;
  781. struct dma_chan *chan, *_c;
  782. if (pd) {
  783. dma_async_device_unregister(&pd->dma);
  784. free_irq(pdev->irq, pd);
  785. list_for_each_entry_safe(chan, _c, &pd->dma.channels,
  786. device_node) {
  787. pd_chan = to_pd_chan(chan);
  788. tasklet_kill(&pd_chan->tasklet);
  789. }
  790. pci_pool_destroy(pd->pool);
  791. pci_iounmap(pdev, pd->membase);
  792. pci_release_regions(pdev);
  793. pci_disable_device(pdev);
  794. kfree(pd);
  795. }
  796. }
  797. /* PCI Device ID of DMA device */
  798. #define PCI_VENDOR_ID_ROHM 0x10DB
  799. #define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
  800. #define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
  801. #define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
  802. #define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
  803. #define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
  804. #define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
  805. #define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
  806. #define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
  807. #define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
  808. #define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
  809. #define PCI_DEVICE_ID_ML7831_DMA1_8CH 0x8810
  810. #define PCI_DEVICE_ID_ML7831_DMA2_4CH 0x8815
  811. static const struct pci_device_id pch_dma_id_table[] = {
  812. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_8CH), 8 },
  813. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_4CH), 4 },
  814. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA1_8CH), 8}, /* UART Video */
  815. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA2_8CH), 8}, /* PCMIF SPI */
  816. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA3_4CH), 4}, /* FPGA */
  817. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA4_12CH), 12}, /* I2S */
  818. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA1_4CH), 4}, /* UART */
  819. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA2_4CH), 4}, /* Video SPI */
  820. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA3_4CH), 4}, /* Security */
  821. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA4_4CH), 4}, /* FPGA */
  822. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA1_8CH), 8}, /* UART */
  823. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA2_4CH), 4}, /* SPI */
  824. { 0, },
  825. };
  826. static struct pci_driver pch_dma_driver = {
  827. .name = DRV_NAME,
  828. .id_table = pch_dma_id_table,
  829. .probe = pch_dma_probe,
  830. .remove = pch_dma_remove,
  831. #ifdef CONFIG_PM
  832. .suspend = pch_dma_suspend,
  833. .resume = pch_dma_resume,
  834. #endif
  835. };
  836. module_pci_driver(pch_dma_driver);
  837. MODULE_DESCRIPTION("Intel EG20T PCH / LAPIS Semicon ML7213/ML7223/ML7831 IOH "
  838. "DMA controller driver");
  839. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  840. MODULE_LICENSE("GPL v2");
  841. MODULE_DEVICE_TABLE(pci, pch_dma_id_table);