blob: 2f62a9f4058acbf9eea9bc75142f8990967d0c0e [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
2 * File: arch/blackfin/kernel/dma-mapping.c
3 * Based on:
4 * Author:
5 *
6 * Created:
7 * Description: Dynamic DMA mapping support.
8 *
9 * Modified:
10 * Copyright 2004-2006 Analog Devices Inc.
11 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30#include <linux/types.h>
31#include <linux/mm.h>
32#include <linux/string.h>
33#include <linux/bootmem.h>
34#include <linux/spinlock.h>
35#include <linux/device.h>
36#include <linux/dma-mapping.h>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080037#include <linux/io.h>
Adrian Bunk82861922007-10-24 14:09:25 +020038#include <linux/scatterlist.h>
Bryan Wu1394f032007-05-06 14:50:22 -070039#include <asm/cacheflush.h>
Bryan Wu1394f032007-05-06 14:50:22 -070040#include <asm/bfin-global.h>
41
42static spinlock_t dma_page_lock;
43static unsigned int *dma_page;
44static unsigned int dma_pages;
45static unsigned long dma_base;
46static unsigned long dma_size;
47static unsigned int dma_initialized;
48
49void dma_alloc_init(unsigned long start, unsigned long end)
50{
51 spin_lock_init(&dma_page_lock);
52 dma_initialized = 0;
53
54 dma_page = (unsigned int *)__get_free_page(GFP_KERNEL);
55 memset(dma_page, 0, PAGE_SIZE);
56 dma_base = PAGE_ALIGN(start);
57 dma_size = PAGE_ALIGN(end) - PAGE_ALIGN(start);
58 dma_pages = dma_size >> PAGE_SHIFT;
59 memset((void *)dma_base, 0, DMA_UNCACHED_REGION);
60 dma_initialized = 1;
61
Harvey Harrisonb85d8582008-04-23 09:39:01 +080062 printk(KERN_INFO "%s: dma_page @ 0x%p - %d pages at 0x%08lx\n", __func__,
Bryan Wu1394f032007-05-06 14:50:22 -070063 dma_page, dma_pages, dma_base);
64}
65
66static inline unsigned int get_pages(size_t size)
67{
68 return ((size - 1) >> PAGE_SHIFT) + 1;
69}
70
71static unsigned long __alloc_dma_pages(unsigned int pages)
72{
73 unsigned long ret = 0, flags;
74 int i, count = 0;
75
76 if (dma_initialized == 0)
77 dma_alloc_init(_ramend - DMA_UNCACHED_REGION, _ramend);
78
79 spin_lock_irqsave(&dma_page_lock, flags);
80
81 for (i = 0; i < dma_pages;) {
82 if (dma_page[i++] == 0) {
83 if (++count == pages) {
84 while (count--)
85 dma_page[--i] = 1;
86 ret = dma_base + (i << PAGE_SHIFT);
87 break;
88 }
89 } else
90 count = 0;
91 }
92 spin_unlock_irqrestore(&dma_page_lock, flags);
93 return ret;
94}
95
96static void __free_dma_pages(unsigned long addr, unsigned int pages)
97{
98 unsigned long page = (addr - dma_base) >> PAGE_SHIFT;
99 unsigned long flags;
100 int i;
101
102 if ((page + pages) > dma_pages) {
Harvey Harrisonb85d8582008-04-23 09:39:01 +0800103 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
Bryan Wu1394f032007-05-06 14:50:22 -0700104 BUG();
105 }
106
107 spin_lock_irqsave(&dma_page_lock, flags);
108 for (i = page; i < page + pages; i++) {
109 dma_page[i] = 0;
110 }
111 spin_unlock_irqrestore(&dma_page_lock, flags);
112}
113
114void *dma_alloc_coherent(struct device *dev, size_t size,
115 dma_addr_t * dma_handle, gfp_t gfp)
116{
117 void *ret;
118
119 ret = (void *)__alloc_dma_pages(get_pages(size));
120
121 if (ret) {
122 memset(ret, 0, size);
123 *dma_handle = virt_to_phys(ret);
124 }
125
126 return ret;
127}
128EXPORT_SYMBOL(dma_alloc_coherent);
129
130void
131dma_free_coherent(struct device *dev, size_t size, void *vaddr,
132 dma_addr_t dma_handle)
133{
134 __free_dma_pages((unsigned long)vaddr, get_pages(size));
135}
136EXPORT_SYMBOL(dma_free_coherent);
137
138/*
139 * Dummy functions defined for some existing drivers
140 */
141
142dma_addr_t
143dma_map_single(struct device *dev, void *ptr, size_t size,
144 enum dma_data_direction direction)
145{
146 BUG_ON(direction == DMA_NONE);
147
148 invalidate_dcache_range((unsigned long)ptr,
149 (unsigned long)ptr + size);
150
151 return (dma_addr_t) ptr;
152}
153EXPORT_SYMBOL(dma_map_single);
154
155int
156dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
157 enum dma_data_direction direction)
158{
159 int i;
160
161 BUG_ON(direction == DMA_NONE);
162
Sonic Zhangb07af762007-07-12 12:18:08 +0800163 for (i = 0; i < nents; i++, sg++) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200164 sg->dma_address = (dma_addr_t) sg_virt(sg);
Sonic Zhangb07af762007-07-12 12:18:08 +0800165
166 invalidate_dcache_range(sg_dma_address(sg),
167 sg_dma_address(sg) +
168 sg_dma_len(sg));
169 }
Bryan Wu1394f032007-05-06 14:50:22 -0700170
171 return nents;
172}
173EXPORT_SYMBOL(dma_map_sg);
174
175void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
176 enum dma_data_direction direction)
177{
178 BUG_ON(direction == DMA_NONE);
179}
180EXPORT_SYMBOL(dma_unmap_single);
181
182void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
183 int nhwentries, enum dma_data_direction direction)
184{
185 BUG_ON(direction == DMA_NONE);
186}
187EXPORT_SYMBOL(dma_unmap_sg);