dma_lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (C) 2006-2007 PA Semi, Inc
  3. *
  4. * Common functions for DMA access on PA Semi PWRficient
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/of.h>
  23. #include <asm/pasemi_dma.h>
  24. #define MAX_TXCH 64
  25. #define MAX_RXCH 64
  26. static struct pasdma_status *dma_status;
  27. static void __iomem *iob_regs;
  28. static void __iomem *mac_regs[6];
  29. static void __iomem *dma_regs;
  30. static int base_hw_irq;
  31. static int num_txch, num_rxch;
  32. static struct pci_dev *dma_pdev;
  33. /* Bitmaps to handle allocation of channels */
  34. static DECLARE_BITMAP(txch_free, MAX_TXCH);
  35. static DECLARE_BITMAP(rxch_free, MAX_RXCH);
  36. /* pasemi_read_iob_reg - read IOB register
  37. * @reg: Register to read (offset into PCI CFG space)
  38. */
  39. unsigned int pasemi_read_iob_reg(unsigned int reg)
  40. {
  41. return in_le32(iob_regs+reg);
  42. }
  43. EXPORT_SYMBOL(pasemi_read_iob_reg);
  44. /* pasemi_write_iob_reg - write IOB register
  45. * @reg: Register to write to (offset into PCI CFG space)
  46. * @val: Value to write
  47. */
  48. void pasemi_write_iob_reg(unsigned int reg, unsigned int val)
  49. {
  50. out_le32(iob_regs+reg, val);
  51. }
  52. EXPORT_SYMBOL(pasemi_write_iob_reg);
  53. /* pasemi_read_mac_reg - read MAC register
  54. * @intf: MAC interface
  55. * @reg: Register to read (offset into PCI CFG space)
  56. */
  57. unsigned int pasemi_read_mac_reg(int intf, unsigned int reg)
  58. {
  59. return in_le32(mac_regs[intf]+reg);
  60. }
  61. EXPORT_SYMBOL(pasemi_read_mac_reg);
  62. /* pasemi_write_mac_reg - write MAC register
  63. * @intf: MAC interface
  64. * @reg: Register to write to (offset into PCI CFG space)
  65. * @val: Value to write
  66. */
  67. void pasemi_write_mac_reg(int intf, unsigned int reg, unsigned int val)
  68. {
  69. out_le32(mac_regs[intf]+reg, val);
  70. }
  71. EXPORT_SYMBOL(pasemi_write_mac_reg);
  72. /* pasemi_read_dma_reg - read DMA register
  73. * @reg: Register to read (offset into PCI CFG space)
  74. */
  75. unsigned int pasemi_read_dma_reg(unsigned int reg)
  76. {
  77. return in_le32(dma_regs+reg);
  78. }
  79. EXPORT_SYMBOL(pasemi_read_dma_reg);
  80. /* pasemi_write_dma_reg - write DMA register
  81. * @reg: Register to write to (offset into PCI CFG space)
  82. * @val: Value to write
  83. */
  84. void pasemi_write_dma_reg(unsigned int reg, unsigned int val)
  85. {
  86. out_le32(dma_regs+reg, val);
  87. }
  88. EXPORT_SYMBOL(pasemi_write_dma_reg);
  89. static int pasemi_alloc_tx_chan(enum pasemi_dmachan_type type)
  90. {
  91. int bit;
  92. int start, limit;
  93. switch (type & (TXCHAN_EVT0|TXCHAN_EVT1)) {
  94. case TXCHAN_EVT0:
  95. start = 0;
  96. limit = 10;
  97. break;
  98. case TXCHAN_EVT1:
  99. start = 10;
  100. limit = MAX_TXCH;
  101. break;
  102. default:
  103. start = 0;
  104. limit = MAX_TXCH;
  105. break;
  106. }
  107. retry:
  108. bit = find_next_bit(txch_free, MAX_TXCH, start);
  109. if (bit >= limit)
  110. return -ENOSPC;
  111. if (!test_and_clear_bit(bit, txch_free))
  112. goto retry;
  113. return bit;
  114. }
  115. static void pasemi_free_tx_chan(int chan)
  116. {
  117. BUG_ON(test_bit(chan, txch_free));
  118. set_bit(chan, txch_free);
  119. }
  120. static int pasemi_alloc_rx_chan(void)
  121. {
  122. int bit;
  123. retry:
  124. bit = find_first_bit(rxch_free, MAX_RXCH);
  125. if (bit >= MAX_TXCH)
  126. return -ENOSPC;
  127. if (!test_and_clear_bit(bit, rxch_free))
  128. goto retry;
  129. return bit;
  130. }
  131. static void pasemi_free_rx_chan(int chan)
  132. {
  133. BUG_ON(test_bit(chan, rxch_free));
  134. set_bit(chan, rxch_free);
  135. }
  136. /* pasemi_dma_alloc_chan - Allocate a DMA channel
  137. * @type: Type of channel to allocate
  138. * @total_size: Total size of structure to allocate (to allow for more
  139. * room behind the structure to be used by the client)
  140. * @offset: Offset in bytes from start of the total structure to the beginning
  141. * of struct pasemi_dmachan. Needed when struct pasemi_dmachan is
  142. * not the first member of the client structure.
  143. *
  144. * pasemi_dma_alloc_chan allocates a DMA channel for use by a client. The
  145. * type argument specifies whether it's a RX or TX channel, and in the case
  146. * of TX channels which group it needs to belong to (if any).
  147. *
  148. * Returns a pointer to the total structure allocated on success, NULL
  149. * on failure.
  150. */
  151. void *pasemi_dma_alloc_chan(enum pasemi_dmachan_type type,
  152. int total_size, int offset)
  153. {
  154. void *buf;
  155. struct pasemi_dmachan *chan;
  156. int chno;
  157. BUG_ON(total_size < sizeof(struct pasemi_dmachan));
  158. buf = kzalloc(total_size, GFP_KERNEL);
  159. if (!buf)
  160. return NULL;
  161. chan = buf + offset;
  162. chan->priv = buf;
  163. switch (type & (TXCHAN|RXCHAN)) {
  164. case RXCHAN:
  165. chno = pasemi_alloc_rx_chan();
  166. chan->chno = chno;
  167. chan->irq = irq_create_mapping(NULL,
  168. base_hw_irq + num_txch + chno);
  169. chan->status = &dma_status->rx_sta[chno];
  170. break;
  171. case TXCHAN:
  172. chno = pasemi_alloc_tx_chan(type);
  173. chan->chno = chno;
  174. chan->irq = irq_create_mapping(NULL, base_hw_irq + chno);
  175. chan->status = &dma_status->tx_sta[chno];
  176. break;
  177. }
  178. chan->chan_type = type;
  179. return chan;
  180. }
  181. EXPORT_SYMBOL(pasemi_dma_alloc_chan);
  182. /* pasemi_dma_free_chan - Free a previously allocated channel
  183. * @chan: Channel to free
  184. *
  185. * Frees a previously allocated channel. It will also deallocate any
  186. * descriptor ring associated with the channel, if allocated.
  187. */
  188. void pasemi_dma_free_chan(struct pasemi_dmachan *chan)
  189. {
  190. if (chan->ring_virt)
  191. pasemi_dma_free_ring(chan);
  192. switch (chan->chan_type & (RXCHAN|TXCHAN)) {
  193. case RXCHAN:
  194. pasemi_free_rx_chan(chan->chno);
  195. break;
  196. case TXCHAN:
  197. pasemi_free_tx_chan(chan->chno);
  198. break;
  199. }
  200. kfree(chan->priv);
  201. }
  202. EXPORT_SYMBOL(pasemi_dma_free_chan);
  203. /* pasemi_dma_alloc_ring - Allocate descriptor ring for a channel
  204. * @chan: Channel for which to allocate
  205. * @ring_size: Ring size in 64-bit (8-byte) words
  206. *
  207. * Allocate a descriptor ring for a channel. Returns 0 on success, errno
  208. * on failure. The passed in struct pasemi_dmachan is updated with the
  209. * virtual and DMA addresses of the ring.
  210. */
  211. int pasemi_dma_alloc_ring(struct pasemi_dmachan *chan, int ring_size)
  212. {
  213. BUG_ON(chan->ring_virt);
  214. chan->ring_size = ring_size;
  215. chan->ring_virt = dma_alloc_coherent(&dma_pdev->dev,
  216. ring_size * sizeof(u64),
  217. &chan->ring_dma, GFP_KERNEL);
  218. if (!chan->ring_virt)
  219. return -ENOMEM;
  220. memset(chan->ring_virt, 0, ring_size * sizeof(u64));
  221. return 0;
  222. }
  223. EXPORT_SYMBOL(pasemi_dma_alloc_ring);
  224. /* pasemi_dma_free_ring - Free an allocated descriptor ring for a channel
  225. * @chan: Channel for which to free the descriptor ring
  226. *
  227. * Frees a previously allocated descriptor ring for a channel.
  228. */
  229. void pasemi_dma_free_ring(struct pasemi_dmachan *chan)
  230. {
  231. BUG_ON(!chan->ring_virt);
  232. dma_free_coherent(&dma_pdev->dev, chan->ring_size * sizeof(u64),
  233. chan->ring_virt, chan->ring_dma);
  234. chan->ring_virt = NULL;
  235. chan->ring_size = 0;
  236. chan->ring_dma = 0;
  237. }
  238. EXPORT_SYMBOL(pasemi_dma_free_ring);
  239. /* pasemi_dma_start_chan - Start a DMA channel
  240. * @chan: Channel to start
  241. * @cmdsta: Additional CCMDSTA/TCMDSTA bits to write
  242. *
  243. * Enables (starts) a DMA channel with optional additional arguments.
  244. */
  245. void pasemi_dma_start_chan(const struct pasemi_dmachan *chan, const u32 cmdsta)
  246. {
  247. if (chan->chan_type == RXCHAN)
  248. pasemi_write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno),
  249. cmdsta | PAS_DMA_RXCHAN_CCMDSTA_EN);
  250. else
  251. pasemi_write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno),
  252. cmdsta | PAS_DMA_TXCHAN_TCMDSTA_EN);
  253. }
  254. EXPORT_SYMBOL(pasemi_dma_start_chan);
  255. /* pasemi_dma_stop_chan - Stop a DMA channel
  256. * @chan: Channel to stop
  257. *
  258. * Stops (disables) a DMA channel. This is done by setting the ST bit in the
  259. * CMDSTA register and waiting on the ACT (active) bit to clear, then
  260. * finally disabling the whole channel.
  261. *
  262. * This function will only try for a short while for the channel to stop, if
  263. * it doesn't it will return failure.
  264. *
  265. * Returns 1 on success, 0 on failure.
  266. */
  267. #define MAX_RETRIES 5000
  268. int pasemi_dma_stop_chan(const struct pasemi_dmachan *chan)
  269. {
  270. int reg, retries;
  271. u32 sta;
  272. if (chan->chan_type == RXCHAN) {
  273. reg = PAS_DMA_RXCHAN_CCMDSTA(chan->chno);
  274. pasemi_write_dma_reg(reg, PAS_DMA_RXCHAN_CCMDSTA_ST);
  275. for (retries = 0; retries < MAX_RETRIES; retries++) {
  276. sta = pasemi_read_dma_reg(reg);
  277. if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)) {
  278. pasemi_write_dma_reg(reg, 0);
  279. return 1;
  280. }
  281. cond_resched();
  282. }
  283. } else {
  284. reg = PAS_DMA_TXCHAN_TCMDSTA(chan->chno);
  285. pasemi_write_dma_reg(reg, PAS_DMA_TXCHAN_TCMDSTA_ST);
  286. for (retries = 0; retries < MAX_RETRIES; retries++) {
  287. sta = pasemi_read_dma_reg(reg);
  288. if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)) {
  289. pasemi_write_dma_reg(reg, 0);
  290. return 1;
  291. }
  292. cond_resched();
  293. }
  294. }
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(pasemi_dma_stop_chan);
  298. /* pasemi_dma_alloc_buf - Allocate a buffer to use for DMA
  299. * @chan: Channel to allocate for
  300. * @size: Size of buffer in bytes
  301. * @handle: DMA handle
  302. *
  303. * Allocate a buffer to be used by the DMA engine for read/write,
  304. * similar to dma_alloc_coherent().
  305. *
  306. * Returns the virtual address of the buffer, or NULL in case of failure.
  307. */
  308. void *pasemi_dma_alloc_buf(struct pasemi_dmachan *chan, int size,
  309. dma_addr_t *handle)
  310. {
  311. return dma_alloc_coherent(&dma_pdev->dev, size, handle, GFP_KERNEL);
  312. }
  313. EXPORT_SYMBOL(pasemi_dma_alloc_buf);
  314. /* pasemi_dma_free_buf - Free a buffer used for DMA
  315. * @chan: Channel the buffer was allocated for
  316. * @size: Size of buffer in bytes
  317. * @handle: DMA handle
  318. *
  319. * Frees a previously allocated buffer.
  320. */
  321. void pasemi_dma_free_buf(struct pasemi_dmachan *chan, int size,
  322. dma_addr_t *handle)
  323. {
  324. dma_free_coherent(&dma_pdev->dev, size, handle, GFP_KERNEL);
  325. }
  326. EXPORT_SYMBOL(pasemi_dma_free_buf);
  327. static void *map_onedev(struct pci_dev *p, int index)
  328. {
  329. struct device_node *dn;
  330. void __iomem *ret;
  331. dn = pci_device_to_OF_node(p);
  332. if (!dn)
  333. goto fallback;
  334. ret = of_iomap(dn, index);
  335. if (!ret)
  336. goto fallback;
  337. return ret;
  338. fallback:
  339. /* This is hardcoded and ugly, but we have some firmware versions
  340. * that don't provide the register space in the device tree. Luckily
  341. * they are at well-known locations so we can just do the math here.
  342. */
  343. return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
  344. }
  345. /* pasemi_dma_init - Initialize the PA Semi DMA library
  346. *
  347. * This function initializes the DMA library. It must be called before
  348. * any other function in the library.
  349. *
  350. * Returns 0 on success, errno on failure.
  351. */
  352. int pasemi_dma_init(void)
  353. {
  354. static spinlock_t init_lock = SPIN_LOCK_UNLOCKED;
  355. struct pci_dev *iob_pdev;
  356. struct pci_dev *pdev;
  357. struct resource res;
  358. struct device_node *dn;
  359. int i, intf, err = 0;
  360. u32 tmp;
  361. if (!machine_is(pasemi))
  362. return -ENODEV;
  363. spin_lock(&init_lock);
  364. /* Make sure we haven't already initialized */
  365. if (dma_pdev)
  366. goto out;
  367. iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
  368. if (!iob_pdev) {
  369. BUG();
  370. printk(KERN_WARNING "Can't find I/O Bridge\n");
  371. err = -ENODEV;
  372. goto out;
  373. }
  374. iob_regs = map_onedev(iob_pdev, 0);
  375. dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
  376. if (!dma_pdev) {
  377. BUG();
  378. printk(KERN_WARNING "Can't find DMA controller\n");
  379. err = -ENODEV;
  380. goto out;
  381. }
  382. dma_regs = map_onedev(dma_pdev, 0);
  383. base_hw_irq = virq_to_hw(dma_pdev->irq);
  384. pci_read_config_dword(dma_pdev, PAS_DMA_CAP_TXCH, &tmp);
  385. num_txch = (tmp & PAS_DMA_CAP_TXCH_TCHN_M) >> PAS_DMA_CAP_TXCH_TCHN_S;
  386. pci_read_config_dword(dma_pdev, PAS_DMA_CAP_RXCH, &tmp);
  387. num_rxch = (tmp & PAS_DMA_CAP_RXCH_RCHN_M) >> PAS_DMA_CAP_RXCH_RCHN_S;
  388. intf = 0;
  389. for (pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa006, NULL);
  390. pdev;
  391. pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa006, pdev))
  392. mac_regs[intf++] = map_onedev(pdev, 0);
  393. pci_dev_put(pdev);
  394. for (pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa005, NULL);
  395. pdev;
  396. pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa005, pdev))
  397. mac_regs[intf++] = map_onedev(pdev, 0);
  398. pci_dev_put(pdev);
  399. dn = pci_device_to_OF_node(iob_pdev);
  400. if (dn)
  401. err = of_address_to_resource(dn, 1, &res);
  402. if (!dn || err) {
  403. /* Fallback for old firmware */
  404. res.start = 0xfd800000;
  405. res.end = res.start + 0x1000;
  406. }
  407. dma_status = __ioremap(res.start, res.end-res.start, 0);
  408. pci_dev_put(iob_pdev);
  409. for (i = 0; i < MAX_TXCH; i++)
  410. __set_bit(i, txch_free);
  411. for (i = 0; i < MAX_RXCH; i++)
  412. __set_bit(i, rxch_free);
  413. printk(KERN_INFO "PA Semi PWRficient DMA library initialized "
  414. "(%d tx, %d rx channels)\n", num_txch, num_rxch);
  415. out:
  416. spin_unlock(&init_lock);
  417. return err;
  418. }
  419. EXPORT_SYMBOL(pasemi_dma_init);