Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux/fpc-iii.git] / arch / mips / mm / dma-ip32.c
blobec54ed0d26ff23fbf257e6e6193cf9a8eeb06815
1 /*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
6 * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
7 * Copyright (C) 2000, 2001 Ralf Baechle <ralf@gnu.org>
8 * Copyright (C) 2005 Ilya A. Volynets-Evenbakh <ilya@total-knowledge.com>
9 * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
10 * IP32 changes by Ilya.
12 #include <linux/types.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <linux/dma-mapping.h>
18 #include <asm/cache.h>
19 #include <asm/io.h>
20 #include <asm/ip32/crime.h>
23 * Warning on the terminology - Linux calls an uncached area coherent;
24 * MIPS terminology calls memory areas with hardware maintained coherency
25 * coherent.
29 * Few notes.
30 * 1. CPU sees memory as two chunks: 0-256M@0x0, and the rest @0x40000000+256M
31 * 2. PCI sees memory as one big chunk @0x0 (or we could use 0x40000000 for native-endian)
32 * 3. All other devices see memory as one big chunk at 0x40000000
33 * 4. Non-PCI devices will pass NULL as struct device*
34 * Thus we translate differently, depending on device.
37 #define RAM_OFFSET_MASK 0x3fffffff
39 void *dma_alloc_noncoherent(struct device *dev, size_t size,
40 dma_addr_t * dma_handle, gfp_t gfp)
42 void *ret;
43 /* ignore region specifiers */
44 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
46 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
47 gfp |= GFP_DMA;
48 ret = (void *) __get_free_pages(gfp, get_order(size));
50 if (ret != NULL) {
51 unsigned long addr = virt_to_phys(ret)&RAM_OFFSET_MASK;
52 memset(ret, 0, size);
53 if(dev==NULL)
54 addr+= CRIME_HI_MEM_BASE;
55 *dma_handle = addr;
58 return ret;
61 EXPORT_SYMBOL(dma_alloc_noncoherent);
63 void *dma_alloc_coherent(struct device *dev, size_t size,
64 dma_addr_t * dma_handle, gfp_t gfp)
66 void *ret;
68 ret = dma_alloc_noncoherent(dev, size, dma_handle, gfp);
69 if (ret) {
70 dma_cache_wback_inv((unsigned long) ret, size);
71 ret = UNCAC_ADDR(ret);
74 return ret;
77 EXPORT_SYMBOL(dma_alloc_coherent);
79 void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
80 dma_addr_t dma_handle)
82 free_pages((unsigned long) vaddr, get_order(size));
85 EXPORT_SYMBOL(dma_free_noncoherent);
87 void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
88 dma_addr_t dma_handle)
90 unsigned long addr = (unsigned long) vaddr;
92 addr = CAC_ADDR(addr);
93 free_pages(addr, get_order(size));
96 EXPORT_SYMBOL(dma_free_coherent);
98 static inline void __dma_sync(unsigned long addr, size_t size,
99 enum dma_data_direction direction)
101 switch (direction) {
102 case DMA_TO_DEVICE:
103 dma_cache_wback(addr, size);
104 break;
106 case DMA_FROM_DEVICE:
107 dma_cache_inv(addr, size);
108 break;
110 case DMA_BIDIRECTIONAL:
111 dma_cache_wback_inv(addr, size);
112 break;
114 default:
115 BUG();
119 dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
120 enum dma_data_direction direction)
122 unsigned long addr = (unsigned long) ptr;
124 switch (direction) {
125 case DMA_TO_DEVICE:
126 dma_cache_wback(addr, size);
127 break;
129 case DMA_FROM_DEVICE:
130 dma_cache_inv(addr, size);
131 break;
133 case DMA_BIDIRECTIONAL:
134 dma_cache_wback_inv(addr, size);
135 break;
137 default:
138 BUG();
141 addr = virt_to_phys(ptr)&RAM_OFFSET_MASK;
142 if(dev == NULL)
143 addr+=CRIME_HI_MEM_BASE;
144 return (dma_addr_t)addr;
147 EXPORT_SYMBOL(dma_map_single);
149 void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
150 enum dma_data_direction direction)
152 switch (direction) {
153 case DMA_TO_DEVICE:
154 break;
156 case DMA_FROM_DEVICE:
157 break;
159 case DMA_BIDIRECTIONAL:
160 break;
162 default:
163 BUG();
167 EXPORT_SYMBOL(dma_unmap_single);
169 int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
170 enum dma_data_direction direction)
172 int i;
174 BUG_ON(direction == DMA_NONE);
176 for (i = 0; i < nents; i++, sg++) {
177 unsigned long addr;
179 addr = (unsigned long) page_address(sg->page)+sg->offset;
180 if (addr)
181 __dma_sync(addr, sg->length, direction);
182 addr = __pa(addr)&RAM_OFFSET_MASK;
183 if(dev == NULL)
184 addr += CRIME_HI_MEM_BASE;
185 sg->dma_address = (dma_addr_t)addr;
188 return nents;
191 EXPORT_SYMBOL(dma_map_sg);
193 dma_addr_t dma_map_page(struct device *dev, struct page *page,
194 unsigned long offset, size_t size, enum dma_data_direction direction)
196 unsigned long addr;
198 BUG_ON(direction == DMA_NONE);
200 addr = (unsigned long) page_address(page) + offset;
201 dma_cache_wback_inv(addr, size);
202 addr = __pa(addr)&RAM_OFFSET_MASK;
203 if(dev == NULL)
204 addr += CRIME_HI_MEM_BASE;
206 return (dma_addr_t)addr;
209 EXPORT_SYMBOL(dma_map_page);
211 void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
212 enum dma_data_direction direction)
214 BUG_ON(direction == DMA_NONE);
216 if (direction != DMA_TO_DEVICE) {
217 unsigned long addr;
219 dma_address&=RAM_OFFSET_MASK;
220 addr = dma_address + PAGE_OFFSET;
221 if(dma_address>=256*1024*1024)
222 addr+=CRIME_HI_MEM_BASE;
223 dma_cache_wback_inv(addr, size);
227 EXPORT_SYMBOL(dma_unmap_page);
229 void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
230 enum dma_data_direction direction)
232 unsigned long addr;
233 int i;
235 BUG_ON(direction == DMA_NONE);
237 if (direction == DMA_TO_DEVICE)
238 return;
240 for (i = 0; i < nhwentries; i++, sg++) {
241 addr = (unsigned long) page_address(sg->page);
242 if (!addr)
243 continue;
244 dma_cache_wback_inv(addr + sg->offset, sg->length);
248 EXPORT_SYMBOL(dma_unmap_sg);
250 void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
251 size_t size, enum dma_data_direction direction)
253 unsigned long addr;
255 BUG_ON(direction == DMA_NONE);
257 dma_handle&=RAM_OFFSET_MASK;
258 addr = dma_handle + PAGE_OFFSET;
259 if(dma_handle>=256*1024*1024)
260 addr+=CRIME_HI_MEM_BASE;
261 __dma_sync(addr, size, direction);
264 EXPORT_SYMBOL(dma_sync_single_for_cpu);
266 void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
267 size_t size, enum dma_data_direction direction)
269 unsigned long addr;
271 BUG_ON(direction == DMA_NONE);
273 dma_handle&=RAM_OFFSET_MASK;
274 addr = dma_handle + PAGE_OFFSET;
275 if(dma_handle>=256*1024*1024)
276 addr+=CRIME_HI_MEM_BASE;
277 __dma_sync(addr, size, direction);
280 EXPORT_SYMBOL(dma_sync_single_for_device);
282 void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
283 unsigned long offset, size_t size, enum dma_data_direction direction)
285 unsigned long addr;
287 BUG_ON(direction == DMA_NONE);
289 dma_handle&=RAM_OFFSET_MASK;
290 addr = dma_handle + offset + PAGE_OFFSET;
291 if(dma_handle>=256*1024*1024)
292 addr+=CRIME_HI_MEM_BASE;
293 __dma_sync(addr, size, direction);
296 EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
298 void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
299 unsigned long offset, size_t size, enum dma_data_direction direction)
301 unsigned long addr;
303 BUG_ON(direction == DMA_NONE);
305 dma_handle&=RAM_OFFSET_MASK;
306 addr = dma_handle + offset + PAGE_OFFSET;
307 if(dma_handle>=256*1024*1024)
308 addr+=CRIME_HI_MEM_BASE;
309 __dma_sync(addr, size, direction);
312 EXPORT_SYMBOL(dma_sync_single_range_for_device);
314 void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
315 enum dma_data_direction direction)
317 int i;
319 BUG_ON(direction == DMA_NONE);
321 /* Make sure that gcc doesn't leave the empty loop body. */
322 for (i = 0; i < nelems; i++, sg++)
323 __dma_sync((unsigned long)page_address(sg->page),
324 sg->length, direction);
327 EXPORT_SYMBOL(dma_sync_sg_for_cpu);
329 void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
330 enum dma_data_direction direction)
332 int i;
334 BUG_ON(direction == DMA_NONE);
336 /* Make sure that gcc doesn't leave the empty loop body. */
337 for (i = 0; i < nelems; i++, sg++)
338 __dma_sync((unsigned long)page_address(sg->page),
339 sg->length, direction);
342 EXPORT_SYMBOL(dma_sync_sg_for_device);
344 int dma_mapping_error(dma_addr_t dma_addr)
346 return 0;
349 EXPORT_SYMBOL(dma_mapping_error);
351 int dma_supported(struct device *dev, u64 mask)
354 * we fall back to GFP_DMA when the mask isn't all 1s,
355 * so we can't guarantee allocations that must be
356 * within a tighter range than GFP_DMA..
358 if (mask < 0x00ffffff)
359 return 0;
361 return 1;
364 EXPORT_SYMBOL(dma_supported);
366 int dma_is_consistent(dma_addr_t dma_addr)
368 return 1;
371 EXPORT_SYMBOL(dma_is_consistent);
373 void dma_cache_sync(void *vaddr, size_t size, enum dma_data_direction direction)
375 if (direction == DMA_NONE)
376 return;
378 dma_cache_wback_inv((unsigned long)vaddr, size);
381 EXPORT_SYMBOL(dma_cache_sync);