hwmon: (jc42) Change detection class
[linux/fpc-iii.git] / arch / blackfin / kernel / dma-mapping.c
blob04ddcfeb798140a3ffb465157948395577874ebd
1 /*
2 * Dynamic DMA mapping support
4 * Copyright 2005-2009 Analog Devices Inc.
6 * Licensed under the GPL-2 or later
7 */
9 #include <linux/types.h>
10 #include <linux/gfp.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/scatterlist.h>
16 static spinlock_t dma_page_lock;
17 static unsigned long *dma_page;
18 static unsigned int dma_pages;
19 static unsigned long dma_base;
20 static unsigned long dma_size;
21 static unsigned int dma_initialized;
23 static void dma_alloc_init(unsigned long start, unsigned long end)
25 spin_lock_init(&dma_page_lock);
26 dma_initialized = 0;
28 dma_page = (unsigned long *)__get_free_page(GFP_KERNEL);
29 memset(dma_page, 0, PAGE_SIZE);
30 dma_base = PAGE_ALIGN(start);
31 dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
32 dma_pages = dma_size >> PAGE_SHIFT;
33 memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
34 dma_initialized = 1;
36 printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
37 dma_page, dma_pages, dma_base);
40 static inline unsigned int get_pages(size_t size)
42 return ((size - 1) >> PAGE_SHIFT) + 1;
45 static unsigned long __alloc_dma_pages(unsigned int pages)
47 unsigned long ret = 0, flags;
48 int i, count = 0;
50 if (dma_initialized == 0)
51 dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
53 spin_lock_irqsave(&dma_page_lock, flags);
55 for (i = 0; i < dma_pages;) {
56 if (test_bit(i++, dma_page) == 0) {
57 if (++count == pages) {
58 while (count--)
59 __set_bit(--i, dma_page);
61 ret = dma_base + (i << PAGE_SHIFT);
62 break;
64 } else
65 count = 0;
67 spin_unlock_irqrestore(&dma_page_lock, flags);
68 return ret;
71 static void __free_dma_pages(unsigned long addr, unsigned int pages)
73 unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
74 unsigned long flags;
75 int i;
77 if ((page + pages) > dma_pages) {
78 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
79 BUG();
82 spin_lock_irqsave(&dma_page_lock, flags);
83 for (i = page; i < page + pages; i++)
84 __clear_bit(i, dma_page);
86 spin_unlock_irqrestore(&dma_page_lock, flags);
89 void *dma_alloc_coherent(struct device *dev, size_t size,
90 dma_addr_t *dma_handle, gfp_t gfp)
92 void *ret;
94 ret = (void *)__alloc_dma_pages(get_pages(size));
96 if (ret) {
97 memset(ret, 0, size);
98 *dma_handle = virt_to_phys(ret);
101 return ret;
103 EXPORT_SYMBOL(dma_alloc_coherent);
105 void
106 dma_free_coherent(struct device *dev, size_t size, void *vaddr,
107 dma_addr_t dma_handle)
109 __free_dma_pages((unsigned long)vaddr, get_pages(size));
111 EXPORT_SYMBOL(dma_free_coherent);
114 * Streaming DMA mappings
116 void __dma_sync(dma_addr_t addr, size_t size,
117 enum dma_data_direction dir)
119 __dma_sync_inline(addr, size, dir);
121 EXPORT_SYMBOL(__dma_sync);
124 dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
125 enum dma_data_direction direction)
127 int i;
129 for (i = 0; i < nents; i++, sg++) {
130 sg->dma_address = (dma_addr_t) sg_virt(sg);
131 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
134 return nents;
136 EXPORT_SYMBOL(dma_map_sg);
138 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
139 int nelems, enum dma_data_direction direction)
141 int i;
143 for (i = 0; i < nelems; i++, sg++) {
144 sg->dma_address = (dma_addr_t) sg_virt(sg);
145 __dma_sync(sg_dma_address(sg), sg_dma_len(sg), direction);
148 EXPORT_SYMBOL(dma_sync_sg_for_device);