| Adrian Bunk | b00dc83 | 2008-05-19 16:52:27 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * ebus.c: PCI to EBus bridge device. | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be) | 
|  | 5 | * Copyright (C) 1999  David S. Miller (davem@redhat.com) | 
|  | 6 | */ | 
|  | 7 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> | 
|  | 9 | #include <linux/kernel.h> | 
|  | 10 | #include <linux/types.h> | 
|  | 11 | #include <linux/init.h> | 
|  | 12 | #include <linux/slab.h> | 
|  | 13 | #include <linux/string.h> | 
|  | 14 | #include <linux/interrupt.h> | 
|  | 15 | #include <linux/delay.h> | 
| David S. Miller | c57c2ff | 2007-05-08 00:43:56 -0700 | [diff] [blame] | 16 | #include <linux/pci.h> | 
| Stephen Rothwell | 764f257 | 2008-08-07 15:33:36 -0700 | [diff] [blame] | 17 | #include <linux/of_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | #include <asm/system.h> | 
|  | 20 | #include <asm/page.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/ebus.h> | 
|  | 22 | #include <asm/oplib.h> | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 23 | #include <asm/prom.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/bpp.h> | 
|  | 25 | #include <asm/irq.h> | 
| David S. Miller | c57c2ff | 2007-05-08 00:43:56 -0700 | [diff] [blame] | 26 | #include <asm/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | /* EBUS dma library. */ | 
|  | 29 |  | 
|  | 30 | #define EBDMA_CSR	0x00UL	/* Control/Status */ | 
|  | 31 | #define EBDMA_ADDR	0x04UL	/* DMA Address */ | 
|  | 32 | #define EBDMA_COUNT	0x08UL	/* DMA Count */ | 
|  | 33 |  | 
|  | 34 | #define EBDMA_CSR_INT_PEND	0x00000001 | 
|  | 35 | #define EBDMA_CSR_ERR_PEND	0x00000002 | 
|  | 36 | #define EBDMA_CSR_DRAIN		0x00000004 | 
|  | 37 | #define EBDMA_CSR_INT_EN	0x00000010 | 
|  | 38 | #define EBDMA_CSR_RESET		0x00000080 | 
|  | 39 | #define EBDMA_CSR_WRITE		0x00000100 | 
|  | 40 | #define EBDMA_CSR_EN_DMA	0x00000200 | 
|  | 41 | #define EBDMA_CSR_CYC_PEND	0x00000400 | 
|  | 42 | #define EBDMA_CSR_DIAG_RD_DONE	0x00000800 | 
|  | 43 | #define EBDMA_CSR_DIAG_WR_DONE	0x00001000 | 
|  | 44 | #define EBDMA_CSR_EN_CNT	0x00002000 | 
|  | 45 | #define EBDMA_CSR_TC		0x00004000 | 
|  | 46 | #define EBDMA_CSR_DIS_CSR_DRN	0x00010000 | 
|  | 47 | #define EBDMA_CSR_BURST_SZ_MASK	0x000c0000 | 
|  | 48 | #define EBDMA_CSR_BURST_SZ_1	0x00080000 | 
|  | 49 | #define EBDMA_CSR_BURST_SZ_4	0x00000000 | 
|  | 50 | #define EBDMA_CSR_BURST_SZ_8	0x00040000 | 
|  | 51 | #define EBDMA_CSR_BURST_SZ_16	0x000c0000 | 
|  | 52 | #define EBDMA_CSR_DIAG_EN	0x00100000 | 
|  | 53 | #define EBDMA_CSR_DIS_ERR_PEND	0x00400000 | 
|  | 54 | #define EBDMA_CSR_TCI_DIS	0x00800000 | 
|  | 55 | #define EBDMA_CSR_EN_NEXT	0x01000000 | 
|  | 56 | #define EBDMA_CSR_DMA_ON	0x02000000 | 
|  | 57 | #define EBDMA_CSR_A_LOADED	0x04000000 | 
|  | 58 | #define EBDMA_CSR_NA_LOADED	0x08000000 | 
|  | 59 | #define EBDMA_CSR_DEV_ID_MASK	0xf0000000 | 
|  | 60 |  | 
|  | 61 | #define EBUS_DMA_RESET_TIMEOUT	10000 | 
|  | 62 |  | 
|  | 63 | static void __ebus_dma_reset(struct ebus_dma_info *p, int no_drain) | 
|  | 64 | { | 
|  | 65 | int i; | 
|  | 66 | u32 val = 0; | 
|  | 67 |  | 
|  | 68 | writel(EBDMA_CSR_RESET, p->regs + EBDMA_CSR); | 
|  | 69 | udelay(1); | 
|  | 70 |  | 
|  | 71 | if (no_drain) | 
|  | 72 | return; | 
|  | 73 |  | 
|  | 74 | for (i = EBUS_DMA_RESET_TIMEOUT; i > 0; i--) { | 
|  | 75 | val = readl(p->regs + EBDMA_CSR); | 
|  | 76 |  | 
|  | 77 | if (!(val & (EBDMA_CSR_DRAIN | EBDMA_CSR_CYC_PEND))) | 
|  | 78 | break; | 
|  | 79 | udelay(10); | 
|  | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
| Al Viro | 6d24c8d | 2006-10-08 08:23:28 -0400 | [diff] [blame] | 83 | static irqreturn_t ebus_dma_irq(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { | 
|  | 85 | struct ebus_dma_info *p = dev_id; | 
|  | 86 | unsigned long flags; | 
|  | 87 | u32 csr = 0; | 
|  | 88 |  | 
|  | 89 | spin_lock_irqsave(&p->lock, flags); | 
|  | 90 | csr = readl(p->regs + EBDMA_CSR); | 
|  | 91 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 92 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 93 |  | 
|  | 94 | if (csr & EBDMA_CSR_ERR_PEND) { | 
|  | 95 | printk(KERN_CRIT "ebus_dma(%s): DMA error!\n", p->name); | 
|  | 96 | p->callback(p, EBUS_DMA_EVENT_ERROR, p->client_cookie); | 
|  | 97 | return IRQ_HANDLED; | 
|  | 98 | } else if (csr & EBDMA_CSR_INT_PEND) { | 
|  | 99 | p->callback(p, | 
|  | 100 | (csr & EBDMA_CSR_TC) ? | 
|  | 101 | EBUS_DMA_EVENT_DMA : EBUS_DMA_EVENT_DEVICE, | 
|  | 102 | p->client_cookie); | 
|  | 103 | return IRQ_HANDLED; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | return IRQ_NONE; | 
|  | 107 |  | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | int ebus_dma_register(struct ebus_dma_info *p) | 
|  | 111 | { | 
|  | 112 | u32 csr; | 
|  | 113 |  | 
|  | 114 | if (!p->regs) | 
|  | 115 | return -EINVAL; | 
|  | 116 | if (p->flags & ~(EBUS_DMA_FLAG_USE_EBDMA_HANDLER | | 
|  | 117 | EBUS_DMA_FLAG_TCI_DISABLE)) | 
|  | 118 | return -EINVAL; | 
|  | 119 | if ((p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) && !p->callback) | 
|  | 120 | return -EINVAL; | 
|  | 121 | if (!strlen(p->name)) | 
|  | 122 | return -EINVAL; | 
|  | 123 |  | 
|  | 124 | __ebus_dma_reset(p, 1); | 
|  | 125 |  | 
|  | 126 | csr = EBDMA_CSR_BURST_SZ_16 | EBDMA_CSR_EN_CNT; | 
|  | 127 |  | 
|  | 128 | if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE) | 
|  | 129 | csr |= EBDMA_CSR_TCI_DIS; | 
|  | 130 |  | 
|  | 131 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 132 |  | 
|  | 133 | return 0; | 
|  | 134 | } | 
|  | 135 | EXPORT_SYMBOL(ebus_dma_register); | 
|  | 136 |  | 
|  | 137 | int ebus_dma_irq_enable(struct ebus_dma_info *p, int on) | 
|  | 138 | { | 
|  | 139 | unsigned long flags; | 
|  | 140 | u32 csr; | 
|  | 141 |  | 
|  | 142 | if (on) { | 
|  | 143 | if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) { | 
| Thomas Gleixner | d356d7f | 2006-07-01 19:29:26 -0700 | [diff] [blame] | 144 | if (request_irq(p->irq, ebus_dma_irq, IRQF_SHARED, p->name, p)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return -EBUSY; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | spin_lock_irqsave(&p->lock, flags); | 
|  | 149 | csr = readl(p->regs + EBDMA_CSR); | 
|  | 150 | csr |= EBDMA_CSR_INT_EN; | 
|  | 151 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 152 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 153 | } else { | 
|  | 154 | spin_lock_irqsave(&p->lock, flags); | 
|  | 155 | csr = readl(p->regs + EBDMA_CSR); | 
|  | 156 | csr &= ~EBDMA_CSR_INT_EN; | 
|  | 157 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 158 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 159 |  | 
|  | 160 | if (p->flags & EBUS_DMA_FLAG_USE_EBDMA_HANDLER) { | 
|  | 161 | free_irq(p->irq, p); | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 | return 0; | 
|  | 166 | } | 
|  | 167 | EXPORT_SYMBOL(ebus_dma_irq_enable); | 
|  | 168 |  | 
|  | 169 | void ebus_dma_unregister(struct ebus_dma_info *p) | 
|  | 170 | { | 
|  | 171 | unsigned long flags; | 
|  | 172 | u32 csr; | 
|  | 173 | int irq_on = 0; | 
|  | 174 |  | 
|  | 175 | spin_lock_irqsave(&p->lock, flags); | 
|  | 176 | csr = readl(p->regs + EBDMA_CSR); | 
|  | 177 | if (csr & EBDMA_CSR_INT_EN) { | 
|  | 178 | csr &= ~EBDMA_CSR_INT_EN; | 
|  | 179 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 180 | irq_on = 1; | 
|  | 181 | } | 
|  | 182 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 183 |  | 
|  | 184 | if (irq_on) | 
|  | 185 | free_irq(p->irq, p); | 
|  | 186 | } | 
|  | 187 | EXPORT_SYMBOL(ebus_dma_unregister); | 
|  | 188 |  | 
|  | 189 | int ebus_dma_request(struct ebus_dma_info *p, dma_addr_t bus_addr, size_t len) | 
|  | 190 | { | 
|  | 191 | unsigned long flags; | 
|  | 192 | u32 csr; | 
|  | 193 | int err; | 
|  | 194 |  | 
|  | 195 | if (len >= (1 << 24)) | 
|  | 196 | return -EINVAL; | 
|  | 197 |  | 
|  | 198 | spin_lock_irqsave(&p->lock, flags); | 
|  | 199 | csr = readl(p->regs + EBDMA_CSR); | 
|  | 200 | err = -EINVAL; | 
|  | 201 | if (!(csr & EBDMA_CSR_EN_DMA)) | 
|  | 202 | goto out; | 
|  | 203 | err = -EBUSY; | 
|  | 204 | if (csr & EBDMA_CSR_NA_LOADED) | 
|  | 205 | goto out; | 
|  | 206 |  | 
|  | 207 | writel(len,      p->regs + EBDMA_COUNT); | 
|  | 208 | writel(bus_addr, p->regs + EBDMA_ADDR); | 
|  | 209 | err = 0; | 
|  | 210 |  | 
|  | 211 | out: | 
|  | 212 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 213 |  | 
|  | 214 | return err; | 
|  | 215 | } | 
|  | 216 | EXPORT_SYMBOL(ebus_dma_request); | 
|  | 217 |  | 
|  | 218 | void ebus_dma_prepare(struct ebus_dma_info *p, int write) | 
|  | 219 | { | 
|  | 220 | unsigned long flags; | 
|  | 221 | u32 csr; | 
|  | 222 |  | 
|  | 223 | spin_lock_irqsave(&p->lock, flags); | 
|  | 224 | __ebus_dma_reset(p, 0); | 
|  | 225 |  | 
|  | 226 | csr = (EBDMA_CSR_INT_EN | | 
|  | 227 | EBDMA_CSR_EN_CNT | | 
|  | 228 | EBDMA_CSR_BURST_SZ_16 | | 
|  | 229 | EBDMA_CSR_EN_NEXT); | 
|  | 230 |  | 
|  | 231 | if (write) | 
|  | 232 | csr |= EBDMA_CSR_WRITE; | 
|  | 233 | if (p->flags & EBUS_DMA_FLAG_TCI_DISABLE) | 
|  | 234 | csr |= EBDMA_CSR_TCI_DIS; | 
|  | 235 |  | 
|  | 236 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 237 |  | 
|  | 238 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 239 | } | 
|  | 240 | EXPORT_SYMBOL(ebus_dma_prepare); | 
|  | 241 |  | 
|  | 242 | unsigned int ebus_dma_residue(struct ebus_dma_info *p) | 
|  | 243 | { | 
|  | 244 | return readl(p->regs + EBDMA_COUNT); | 
|  | 245 | } | 
|  | 246 | EXPORT_SYMBOL(ebus_dma_residue); | 
|  | 247 |  | 
|  | 248 | unsigned int ebus_dma_addr(struct ebus_dma_info *p) | 
|  | 249 | { | 
|  | 250 | return readl(p->regs + EBDMA_ADDR); | 
|  | 251 | } | 
|  | 252 | EXPORT_SYMBOL(ebus_dma_addr); | 
|  | 253 |  | 
|  | 254 | void ebus_dma_enable(struct ebus_dma_info *p, int on) | 
|  | 255 | { | 
|  | 256 | unsigned long flags; | 
|  | 257 | u32 orig_csr, csr; | 
|  | 258 |  | 
|  | 259 | spin_lock_irqsave(&p->lock, flags); | 
|  | 260 | orig_csr = csr = readl(p->regs + EBDMA_CSR); | 
|  | 261 | if (on) | 
|  | 262 | csr |= EBDMA_CSR_EN_DMA; | 
|  | 263 | else | 
|  | 264 | csr &= ~EBDMA_CSR_EN_DMA; | 
|  | 265 | if ((orig_csr & EBDMA_CSR_EN_DMA) != | 
|  | 266 | (csr & EBDMA_CSR_EN_DMA)) | 
|  | 267 | writel(csr, p->regs + EBDMA_CSR); | 
|  | 268 | spin_unlock_irqrestore(&p->lock, flags); | 
|  | 269 | } | 
|  | 270 | EXPORT_SYMBOL(ebus_dma_enable); | 
|  | 271 |  | 
|  | 272 | struct linux_ebus *ebus_chain = NULL; | 
|  | 273 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | static inline void *ebus_alloc(size_t size) | 
|  | 275 | { | 
|  | 276 | void *mem; | 
|  | 277 |  | 
| Eric Sesterhenn | 9132983 | 2006-03-06 13:48:40 -0800 | [diff] [blame] | 278 | mem = kzalloc(size, GFP_ATOMIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (!mem) | 
|  | 280 | panic("ebus_alloc: out of memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return mem; | 
|  | 282 | } | 
|  | 283 |  | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 284 | static void __init fill_ebus_child(struct device_node *dp, | 
|  | 285 | struct linux_ebus_child *dev, | 
|  | 286 | int non_standard_regs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 288 | struct of_device *op; | 
| Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 289 | const int *regs; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | int i, len; | 
|  | 291 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 292 | dev->prom_node = dp; | 
|  | 293 | printk(" (%s)", dp->name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 295 | regs = of_get_property(dp, "reg", &len); | 
|  | 296 | if (!regs) | 
|  | 297 | dev->num_addrs = 0; | 
|  | 298 | else | 
|  | 299 | dev->num_addrs = len / sizeof(regs[0]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 |  | 
|  | 301 | if (non_standard_regs) { | 
|  | 302 | /* This is to handle reg properties which are not | 
|  | 303 | * in the parent relative format.  One example are | 
|  | 304 | * children of the i2c device on CompactPCI systems. | 
|  | 305 | * | 
|  | 306 | * So, for such devices we just record the property | 
|  | 307 | * raw in the child resources. | 
|  | 308 | */ | 
|  | 309 | for (i = 0; i < dev->num_addrs; i++) | 
|  | 310 | dev->resource[i].start = regs[i]; | 
|  | 311 | } else { | 
|  | 312 | for (i = 0; i < dev->num_addrs; i++) { | 
|  | 313 | int rnum = regs[i]; | 
|  | 314 | if (rnum >= dev->parent->num_addrs) { | 
|  | 315 | prom_printf("UGH: property for %s was %d, need < %d\n", | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 316 | dp->name, len, dev->parent->num_addrs); | 
|  | 317 | prom_halt(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } | 
|  | 319 | dev->resource[i].start = dev->parent->resource[i].start; | 
|  | 320 | dev->resource[i].end = dev->parent->resource[i].end; | 
|  | 321 | dev->resource[i].flags = IORESOURCE_MEM; | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 322 | dev->resource[i].name = dp->name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } | 
|  | 324 | } | 
|  | 325 |  | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 326 | op = of_find_device_by_node(dp); | 
|  | 327 | if (!op) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | dev->num_irqs = 0; | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 329 | } else { | 
|  | 330 | dev->num_irqs = op->num_irqs; | 
|  | 331 | for (i = 0; i < dev->num_irqs; i++) | 
|  | 332 | dev->irqs[i] = op->irqs[i]; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | if (!dev->num_irqs) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* | 
|  | 337 | * Oh, well, some PROMs don't export interrupts | 
|  | 338 | * property to children of EBus devices... | 
|  | 339 | * | 
|  | 340 | * Be smart about PS/2 keyboard and mouse. | 
|  | 341 | */ | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 342 | if (!strcmp(dev->parent->prom_node->name, "8042")) { | 
|  | 343 | if (!strcmp(dev->prom_node->name, "kb_ps2")) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | dev->num_irqs = 1; | 
|  | 345 | dev->irqs[0] = dev->parent->irqs[0]; | 
|  | 346 | } else { | 
|  | 347 | dev->num_irqs = 1; | 
|  | 348 | dev->irqs[0] = dev->parent->irqs[1]; | 
|  | 349 | } | 
|  | 350 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | static int __init child_regs_nonstandard(struct linux_ebus_device *dev) | 
|  | 355 | { | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 356 | if (!strcmp(dev->prom_node->name, "i2c") || | 
|  | 357 | !strcmp(dev->prom_node->name, "SUNW,lombus")) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | return 1; | 
|  | 359 | return 0; | 
|  | 360 | } | 
|  | 361 |  | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 362 | static void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | struct linux_ebus_child *child; | 
| David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 365 | struct dev_archdata *sd; | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 366 | struct of_device *op; | 
|  | 367 | int i, len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 369 | dev->prom_node = dp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 371 | printk(" [%s", dp->name); | 
|  | 372 |  | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 373 | op = of_find_device_by_node(dp); | 
|  | 374 | if (!op) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | dev->num_addrs = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | dev->num_irqs = 0; | 
|  | 377 | } else { | 
| David S. Miller | 9c908f9 | 2007-09-27 13:09:28 -0700 | [diff] [blame] | 378 | const int *regs = of_get_property(dp, "reg", &len); | 
|  | 379 |  | 
|  | 380 | if (!regs) | 
|  | 381 | len = 0; | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 382 | dev->num_addrs = len / sizeof(struct linux_prom_registers); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 |  | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 384 | for (i = 0; i < dev->num_addrs; i++) | 
|  | 385 | memcpy(&dev->resource[i], | 
|  | 386 | &op->resource[i], | 
|  | 387 | sizeof(struct resource)); | 
|  | 388 |  | 
|  | 389 | dev->num_irqs = op->num_irqs; | 
|  | 390 | for (i = 0; i < dev->num_irqs; i++) | 
|  | 391 | dev->irqs[i] = op->irqs[i]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 394 | sd = &dev->ofdev.dev.archdata; | 
|  | 395 | sd->prom_node = dp; | 
|  | 396 | sd->op = &dev->ofdev; | 
| David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 397 | sd->iommu = dev->bus->ofdev.dev.parent->archdata.iommu; | 
|  | 398 | sd->stc = dev->bus->ofdev.dev.parent->archdata.stc; | 
| David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 399 | sd->numa_node = dev->bus->ofdev.dev.parent->archdata.numa_node; | 
| David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 400 |  | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 401 | dev->ofdev.node = dp; | 
|  | 402 | dev->ofdev.dev.parent = &dev->bus->ofdev.dev; | 
|  | 403 | dev->ofdev.dev.bus = &ebus_bus_type; | 
| Greg Kroah-Hartman | 2222c31 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 404 | dev_set_name(&dev->ofdev.dev, "ebus[%08x]", dp->node); | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 405 |  | 
|  | 406 | /* Register with core */ | 
|  | 407 | if (of_device_register(&dev->ofdev) != 0) | 
|  | 408 | printk(KERN_DEBUG "ebus: device registration error for %s!\n", | 
| David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 409 | dp->path_component_name); | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 410 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 411 | dp = dp->child; | 
|  | 412 | if (dp) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | printk(" ->"); | 
|  | 414 | dev->children = ebus_alloc(sizeof(struct linux_ebus_child)); | 
|  | 415 |  | 
|  | 416 | child = dev->children; | 
|  | 417 | child->next = NULL; | 
|  | 418 | child->parent = dev; | 
|  | 419 | child->bus = dev->bus; | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 420 | fill_ebus_child(dp, child, | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 421 | child_regs_nonstandard(dev)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 423 | while ((dp = dp->sibling) != NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | child->next = ebus_alloc(sizeof(struct linux_ebus_child)); | 
|  | 425 |  | 
|  | 426 | child = child->next; | 
|  | 427 | child->next = NULL; | 
|  | 428 | child->parent = dev; | 
|  | 429 | child->bus = dev->bus; | 
| David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 430 | fill_ebus_child(dp, child, | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 431 | child_regs_nonstandard(dev)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } | 
|  | 433 | } | 
|  | 434 | printk("]"); | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | static struct pci_dev *find_next_ebus(struct pci_dev *start, int *is_rio_p) | 
|  | 438 | { | 
|  | 439 | struct pci_dev *pdev = start; | 
|  | 440 |  | 
| Jiri Slaby | d08fa1a | 2005-11-06 23:39:35 -0800 | [diff] [blame] | 441 | while ((pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_ANY_ID, pdev))) | 
|  | 442 | if (pdev->device == PCI_DEVICE_ID_SUN_EBUS || | 
|  | 443 | pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 |  | 
| Jiri Slaby | d08fa1a | 2005-11-06 23:39:35 -0800 | [diff] [blame] | 446 | *is_rio_p = !!(pdev && (pdev->device == PCI_DEVICE_ID_SUN_RIO_EBUS)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 |  | 
|  | 448 | return pdev; | 
|  | 449 | } | 
|  | 450 |  | 
|  | 451 | void __init ebus_init(void) | 
|  | 452 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | struct linux_ebus_device *dev; | 
|  | 454 | struct linux_ebus *ebus; | 
|  | 455 | struct pci_dev *pdev; | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 456 | struct device_node *dp; | 
|  | 457 | int is_rio; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | int num_ebus = 0; | 
|  | 459 |  | 
|  | 460 | pdev = find_next_ebus(NULL, &is_rio); | 
|  | 461 | if (!pdev) { | 
|  | 462 | printk("ebus: No EBus's found.\n"); | 
|  | 463 | return; | 
|  | 464 | } | 
|  | 465 |  | 
| David S. Miller | 1327e9b | 2007-02-28 17:55:46 -0800 | [diff] [blame] | 466 | dp = pci_device_to_OF_node(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 |  | 
|  | 468 | ebus_chain = ebus = ebus_alloc(sizeof(struct linux_ebus)); | 
|  | 469 | ebus->next = NULL; | 
|  | 470 | ebus->is_rio = is_rio; | 
|  | 471 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 472 | while (dp) { | 
|  | 473 | struct device_node *child; | 
|  | 474 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | /* SUNW,pci-qfe uses four empty ebuses on it. | 
|  | 476 | I think we should not consider them here, | 
|  | 477 | as they have half of the properties this | 
|  | 478 | code expects and once we do PCI hot-plug, | 
|  | 479 | we'd have to tweak with the ebus_chain | 
|  | 480 | in the runtime after initialization. -jj */ | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 481 | if (!dp->child) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | pdev = find_next_ebus(pdev, &is_rio); | 
|  | 483 | if (!pdev) { | 
|  | 484 | if (ebus == ebus_chain) { | 
|  | 485 | ebus_chain = NULL; | 
|  | 486 | printk("ebus: No EBus's found.\n"); | 
|  | 487 | return; | 
|  | 488 | } | 
|  | 489 | break; | 
|  | 490 | } | 
|  | 491 | ebus->is_rio = is_rio; | 
| David S. Miller | 1327e9b | 2007-02-28 17:55:46 -0800 | [diff] [blame] | 492 | dp = pci_device_to_OF_node(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | continue; | 
|  | 494 | } | 
|  | 495 | printk("ebus%d:", num_ebus); | 
|  | 496 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | ebus->index = num_ebus; | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 498 | ebus->prom_node = dp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | ebus->self = pdev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 |  | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 501 | ebus->ofdev.node = dp; | 
|  | 502 | ebus->ofdev.dev.parent = &pdev->dev; | 
|  | 503 | ebus->ofdev.dev.bus = &ebus_bus_type; | 
| Greg Kroah-Hartman | 2222c31 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 504 | dev_set_name(&ebus->ofdev.dev, "ebus%d", num_ebus); | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 505 |  | 
|  | 506 | /* Register with core */ | 
|  | 507 | if (of_device_register(&ebus->ofdev) != 0) | 
|  | 508 | printk(KERN_DEBUG "ebus: device registration error for %s!\n", | 
| David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 509 | dp->path_component_name); | 
| David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 510 |  | 
|  | 511 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 512 | child = dp->child; | 
|  | 513 | if (!child) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | goto next_ebus; | 
|  | 515 |  | 
|  | 516 | ebus->devices = ebus_alloc(sizeof(struct linux_ebus_device)); | 
|  | 517 |  | 
|  | 518 | dev = ebus->devices; | 
|  | 519 | dev->next = NULL; | 
|  | 520 | dev->children = NULL; | 
|  | 521 | dev->bus = ebus; | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 522 | fill_ebus_device(child, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 |  | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 524 | while ((child = child->sibling) != NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | dev->next = ebus_alloc(sizeof(struct linux_ebus_device)); | 
|  | 526 |  | 
|  | 527 | dev = dev->next; | 
|  | 528 | dev->next = NULL; | 
|  | 529 | dev->children = NULL; | 
|  | 530 | dev->bus = ebus; | 
| David S. Miller | 690c8fd | 2006-06-22 19:12:03 -0700 | [diff] [blame] | 531 | fill_ebus_device(child, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } | 
|  | 533 |  | 
|  | 534 | next_ebus: | 
|  | 535 | printk("\n"); | 
|  | 536 |  | 
|  | 537 | pdev = find_next_ebus(pdev, &is_rio); | 
|  | 538 | if (!pdev) | 
|  | 539 | break; | 
|  | 540 |  | 
| David S. Miller | 1327e9b | 2007-02-28 17:55:46 -0800 | [diff] [blame] | 541 | dp = pci_device_to_OF_node(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 |  | 
|  | 543 | ebus->next = ebus_alloc(sizeof(struct linux_ebus)); | 
|  | 544 | ebus = ebus->next; | 
|  | 545 | ebus->next = NULL; | 
|  | 546 | ebus->is_rio = is_rio; | 
|  | 547 | ++num_ebus; | 
|  | 548 | } | 
| Jiri Slaby | d08fa1a | 2005-11-06 23:39:35 -0800 | [diff] [blame] | 549 | pci_dev_put(pdev); /* XXX for the case, when ebusnd is 0, is it OK? */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |