dma_lib.c 13 KB

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