Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 1 | /* |
| 2 | * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp. |
| 3 | * <benh@kernel.crashing.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 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 |
| 13 | * the 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 | |
| 20 | #undef DEBUG |
| 21 | |
| 22 | #include <linux/kernel.h> |
| 23 | #include <asm/prom.h> |
| 24 | #include <asm/dcr.h> |
| 25 | |
| 26 | unsigned int dcr_resource_start(struct device_node *np, unsigned int index) |
| 27 | { |
| 28 | unsigned int ds; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 29 | const u32 *dr = of_get_property(np, "dcr-reg", &ds); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 30 | |
| 31 | if (dr == NULL || ds & 1 || index >= (ds / 8)) |
| 32 | return 0; |
| 33 | |
| 34 | return dr[index * 2]; |
| 35 | } |
Murali Iyer | 96b952d | 2007-08-09 07:46:49 +1000 | [diff] [blame] | 36 | EXPORT_SYMBOL_GPL(dcr_resource_start); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 37 | |
| 38 | unsigned int dcr_resource_len(struct device_node *np, unsigned int index) |
| 39 | { |
| 40 | unsigned int ds; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 41 | const u32 *dr = of_get_property(np, "dcr-reg", &ds); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 42 | |
| 43 | if (dr == NULL || ds & 1 || index >= (ds / 8)) |
| 44 | return 0; |
| 45 | |
| 46 | return dr[index * 2 + 1]; |
| 47 | } |
Murali Iyer | 96b952d | 2007-08-09 07:46:49 +1000 | [diff] [blame] | 48 | EXPORT_SYMBOL_GPL(dcr_resource_len); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 49 | |
| 50 | #ifndef CONFIG_PPC_DCR_NATIVE |
| 51 | |
| 52 | static struct device_node * find_dcr_parent(struct device_node * node) |
| 53 | { |
| 54 | struct device_node *par, *tmp; |
| 55 | const u32 *p; |
| 56 | |
| 57 | for (par = of_node_get(node); par;) { |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 58 | if (of_get_property(par, "dcr-controller", NULL)) |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 59 | break; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 60 | p = of_get_property(par, "dcr-parent", NULL); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 61 | tmp = par; |
| 62 | if (p == NULL) |
| 63 | par = of_get_parent(par); |
| 64 | else |
| 65 | par = of_find_node_by_phandle(*p); |
| 66 | of_node_put(tmp); |
| 67 | } |
| 68 | return par; |
| 69 | } |
| 70 | |
| 71 | u64 of_translate_dcr_address(struct device_node *dev, |
| 72 | unsigned int dcr_n, |
| 73 | unsigned int *out_stride) |
| 74 | { |
| 75 | struct device_node *dp; |
| 76 | const u32 *p; |
| 77 | unsigned int stride; |
| 78 | u64 ret; |
| 79 | |
| 80 | dp = find_dcr_parent(dev); |
| 81 | if (dp == NULL) |
| 82 | return OF_BAD_ADDR; |
| 83 | |
| 84 | /* Stride is not properly defined yet, default to 0x10 for Axon */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 85 | p = of_get_property(dp, "dcr-mmio-stride", NULL); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 86 | stride = (p == NULL) ? 0x10 : *p; |
| 87 | |
| 88 | /* XXX FIXME: Which property name is to use of the 2 following ? */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 89 | p = of_get_property(dp, "dcr-mmio-range", NULL); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 90 | if (p == NULL) |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 91 | p = of_get_property(dp, "dcr-mmio-space", NULL); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 92 | if (p == NULL) |
| 93 | return OF_BAD_ADDR; |
| 94 | |
| 95 | /* Maybe could do some better range checking here */ |
| 96 | ret = of_translate_address(dp, p); |
| 97 | if (ret != OF_BAD_ADDR) |
| 98 | ret += (u64)(stride) * (u64)dcr_n; |
| 99 | if (out_stride) |
| 100 | *out_stride = stride; |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | dcr_host_t dcr_map(struct device_node *dev, unsigned int dcr_n, |
| 105 | unsigned int dcr_c) |
| 106 | { |
Michael Ellerman | 0b94a1e | 2007-09-17 16:05:00 +1000 | [diff] [blame] | 107 | dcr_host_t ret = { .token = NULL, .stride = 0, .base = dcr_n }; |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 108 | u64 addr; |
| 109 | |
| 110 | pr_debug("dcr_map(%s, 0x%x, 0x%x)\n", |
| 111 | dev->full_name, dcr_n, dcr_c); |
| 112 | |
| 113 | addr = of_translate_dcr_address(dev, dcr_n, &ret.stride); |
| 114 | pr_debug("translates to addr: 0x%lx, stride: 0x%x\n", |
| 115 | addr, ret.stride); |
| 116 | if (addr == OF_BAD_ADDR) |
| 117 | return ret; |
| 118 | pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride); |
| 119 | ret.token = ioremap(addr, dcr_c * ret.stride); |
| 120 | if (ret.token == NULL) |
| 121 | return ret; |
| 122 | pr_debug("mapped at 0x%p -> base is 0x%p\n", |
| 123 | ret.token, ret.token - dcr_n * ret.stride); |
| 124 | ret.token -= dcr_n * ret.stride; |
| 125 | return ret; |
| 126 | } |
Murali Iyer | 96b952d | 2007-08-09 07:46:49 +1000 | [diff] [blame] | 127 | EXPORT_SYMBOL_GPL(dcr_map); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 128 | |
| 129 | void dcr_unmap(dcr_host_t host, unsigned int dcr_n, unsigned int dcr_c) |
| 130 | { |
| 131 | dcr_host_t h = host; |
| 132 | |
| 133 | if (h.token == NULL) |
| 134 | return; |
Jean-Christophe Dubois | 190a440 | 2007-03-28 11:07:41 +0100 | [diff] [blame] | 135 | h.token += dcr_n * h.stride; |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 136 | iounmap(h.token); |
| 137 | h.token = NULL; |
| 138 | } |
Murali Iyer | 96b952d | 2007-08-09 07:46:49 +1000 | [diff] [blame] | 139 | EXPORT_SYMBOL_GPL(dcr_unmap); |
Benjamin Herrenschmidt | 4c75a6f | 2006-11-11 17:24:53 +1100 | [diff] [blame] | 140 | |
| 141 | #endif /* !defined(CONFIG_PPC_DCR_NATIVE) */ |