0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <linux/module.h>
0030 #include <linux/sched.h>
0031 #include <linux/kernel.h>
0032 #include <linux/errno.h>
0033 #include <linux/types.h>
0034 #include <linux/ioport.h>
0035 #include <linux/mm.h>
0036 #include <linux/slab.h>
0037 #include <linux/pci.h> /* struct pci_dev */
0038 #include <linux/proc_fs.h>
0039 #include <linux/seq_file.h>
0040 #include <linux/scatterlist.h>
0041 #include <linux/dma-map-ops.h>
0042 #include <linux/of_device.h>
0043
0044 #include <asm/io.h>
0045 #include <asm/vaddrs.h>
0046 #include <asm/oplib.h>
0047 #include <asm/prom.h>
0048 #include <asm/page.h>
0049 #include <asm/pgalloc.h>
0050 #include <asm/dma.h>
0051 #include <asm/iommu.h>
0052 #include <asm/io-unit.h>
0053 #include <asm/leon.h>
0054
0055 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
0056 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
0057 unsigned long size, char *name);
0058 static void _sparc_free_io(struct resource *res);
0059
0060 static void register_proc_sparc_ioport(void);
0061
0062
0063 static struct resource _sparc_dvma = {
0064 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
0065 };
0066
0067 struct resource sparc_iomap = {
0068 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
0069 };
0070
0071
0072
0073
0074
0075
0076
0077 #define XNMLN 15
0078 #define XNRES 10
0079
0080 struct xresource {
0081 struct resource xres;
0082 int xflag;
0083 char xname[XNMLN+1];
0084 };
0085
0086 static struct xresource xresv[XNRES];
0087
0088 static struct xresource *xres_alloc(void) {
0089 struct xresource *xrp;
0090 int n;
0091
0092 xrp = xresv;
0093 for (n = 0; n < XNRES; n++) {
0094 if (xrp->xflag == 0) {
0095 xrp->xflag = 1;
0096 return xrp;
0097 }
0098 xrp++;
0099 }
0100 return NULL;
0101 }
0102
0103 static void xres_free(struct xresource *xrp) {
0104 xrp->xflag = 0;
0105 }
0106
0107
0108
0109
0110
0111
0112
0113 void __iomem *ioremap(phys_addr_t offset, size_t size)
0114 {
0115 char name[14];
0116
0117 sprintf(name, "phys_%08x", (u32)offset);
0118 return _sparc_alloc_io(0, (unsigned long)offset, size, name);
0119 }
0120 EXPORT_SYMBOL(ioremap);
0121
0122
0123
0124
0125 void iounmap(volatile void __iomem *virtual)
0126 {
0127 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
0128 struct resource *res;
0129
0130
0131
0132
0133
0134 if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
0135 printk("free_io/iounmap: cannot free %lx\n", vaddr);
0136 return;
0137 }
0138 _sparc_free_io(res);
0139
0140 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
0141 xres_free((struct xresource *)res);
0142 } else {
0143 kfree(res);
0144 }
0145 }
0146 EXPORT_SYMBOL(iounmap);
0147
0148 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
0149 unsigned long size, char *name)
0150 {
0151 return _sparc_alloc_io(res->flags & 0xF,
0152 res->start + offset,
0153 size, name);
0154 }
0155 EXPORT_SYMBOL(of_ioremap);
0156
0157 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
0158 {
0159 iounmap(base);
0160 }
0161 EXPORT_SYMBOL(of_iounmap);
0162
0163
0164
0165
0166 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
0167 unsigned long size, char *name)
0168 {
0169 static int printed_full;
0170 struct xresource *xres;
0171 struct resource *res;
0172 char *tack;
0173 int tlen;
0174 void __iomem *va;
0175
0176 if (name == NULL) name = "???";
0177
0178 if ((xres = xres_alloc()) != NULL) {
0179 tack = xres->xname;
0180 res = &xres->xres;
0181 } else {
0182 if (!printed_full) {
0183 printk("ioremap: done with statics, switching to malloc\n");
0184 printed_full = 1;
0185 }
0186 tlen = strlen(name);
0187 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
0188 if (tack == NULL) return NULL;
0189 memset(tack, 0, sizeof(struct resource));
0190 res = (struct resource *) tack;
0191 tack += sizeof (struct resource);
0192 }
0193
0194 strlcpy(tack, name, XNMLN+1);
0195 res->name = tack;
0196
0197 va = _sparc_ioremap(res, busno, phys, size);
0198
0199 return va;
0200 }
0201
0202
0203
0204 static void __iomem *
0205 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
0206 {
0207 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
0208
0209 if (allocate_resource(&sparc_iomap, res,
0210 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
0211 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
0212
0213 prom_printf("alloc_io_res(%s): cannot occupy\n",
0214 (res->name != NULL)? res->name: "???");
0215 prom_halt();
0216 }
0217
0218 pa &= PAGE_MASK;
0219 srmmu_mapiorange(bus, pa, res->start, resource_size(res));
0220
0221 return (void __iomem *)(unsigned long)(res->start + offset);
0222 }
0223
0224
0225
0226
0227 static void _sparc_free_io(struct resource *res)
0228 {
0229 unsigned long plen;
0230
0231 plen = resource_size(res);
0232 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
0233 srmmu_unmapiorange(res->start, plen);
0234 release_resource(res);
0235 }
0236
0237 unsigned long sparc_dma_alloc_resource(struct device *dev, size_t len)
0238 {
0239 struct resource *res;
0240
0241 res = kzalloc(sizeof(*res), GFP_KERNEL);
0242 if (!res)
0243 return 0;
0244 res->name = dev->of_node->full_name;
0245
0246 if (allocate_resource(&_sparc_dvma, res, len, _sparc_dvma.start,
0247 _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
0248 printk("%s: cannot occupy 0x%zx", __func__, len);
0249 kfree(res);
0250 return 0;
0251 }
0252
0253 return res->start;
0254 }
0255
0256 bool sparc_dma_free_resource(void *cpu_addr, size_t size)
0257 {
0258 unsigned long addr = (unsigned long)cpu_addr;
0259 struct resource *res;
0260
0261 res = lookup_resource(&_sparc_dvma, addr);
0262 if (!res) {
0263 printk("%s: cannot free %p\n", __func__, cpu_addr);
0264 return false;
0265 }
0266
0267 if ((addr & (PAGE_SIZE - 1)) != 0) {
0268 printk("%s: unaligned va %p\n", __func__, cpu_addr);
0269 return false;
0270 }
0271
0272 size = PAGE_ALIGN(size);
0273 if (resource_size(res) != size) {
0274 printk("%s: region 0x%lx asked 0x%zx\n",
0275 __func__, (long)resource_size(res), size);
0276 return false;
0277 }
0278
0279 release_resource(res);
0280 kfree(res);
0281 return true;
0282 }
0283
0284 #ifdef CONFIG_SBUS
0285
0286 void sbus_set_sbus64(struct device *dev, int x)
0287 {
0288 printk("sbus_set_sbus64: unsupported\n");
0289 }
0290 EXPORT_SYMBOL(sbus_set_sbus64);
0291
0292 static int __init sparc_register_ioport(void)
0293 {
0294 register_proc_sparc_ioport();
0295
0296 return 0;
0297 }
0298
0299 arch_initcall(sparc_register_ioport);
0300
0301 #endif
0302
0303
0304
0305
0306
0307
0308
0309 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
0310 enum dma_data_direction dir)
0311 {
0312 if (dir != DMA_TO_DEVICE &&
0313 sparc_cpu_model == sparc_leon &&
0314 !sparc_leon3_snooping_enabled())
0315 leon_flush_dcache_all();
0316 }
0317
0318 #ifdef CONFIG_PROC_FS
0319
0320 static int sparc_io_proc_show(struct seq_file *m, void *v)
0321 {
0322 struct resource *root = m->private, *r;
0323 const char *nm;
0324
0325 for (r = root->child; r != NULL; r = r->sibling) {
0326 if ((nm = r->name) == NULL) nm = "???";
0327 seq_printf(m, "%016llx-%016llx: %s\n",
0328 (unsigned long long)r->start,
0329 (unsigned long long)r->end, nm);
0330 }
0331
0332 return 0;
0333 }
0334 #endif
0335
0336 static void register_proc_sparc_ioport(void)
0337 {
0338 #ifdef CONFIG_PROC_FS
0339 proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
0340 &sparc_iomap);
0341 proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,
0342 &_sparc_dvma);
0343 #endif
0344 }