David S. Miller | d979f17 | 2007-10-27 00:13:04 -0700 | [diff] [blame] | 1 | /* central.c: Central FHC driver for Sunfire/Starfire/Wildfire. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 3 | * Copyright (C) 1997, 1999, 2008 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/init.h> |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 10 | #include <linux/of_device.h> |
| 11 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/fhc.h> |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 14 | #include <asm/upa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 16 | struct clock_board { |
| 17 | void __iomem *clock_freq_regs; |
| 18 | void __iomem *clock_regs; |
| 19 | void __iomem *clock_ver_reg; |
| 20 | int num_slots; |
| 21 | struct resource leds_resource; |
| 22 | struct platform_device leds_pdev; |
| 23 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 25 | struct fhc { |
| 26 | void __iomem *pregs; |
| 27 | bool central; |
| 28 | bool jtag_master; |
| 29 | int board_num; |
| 30 | struct resource leds_resource; |
| 31 | struct platform_device leds_pdev; |
| 32 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 34 | static int __devinit clock_board_calc_nslots(struct clock_board *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 36 | u8 reg = upa_readb(p->clock_regs + CLOCK_STAT1) & 0xc0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 38 | switch (reg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | case 0x40: |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 40 | return 16; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | case 0xc0: |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 43 | return 8; |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | case 0x80: |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 46 | reg = 0; |
| 47 | if (p->clock_ver_reg) |
| 48 | reg = upa_readb(p->clock_ver_reg); |
| 49 | if (reg) { |
| 50 | if (reg & 0x80) |
| 51 | return 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | else |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 53 | return 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 55 | /* Fallthrough */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | default: |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 57 | return 4; |
| 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 61 | static int __devinit clock_board_probe(struct of_device *op, |
| 62 | const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 64 | struct clock_board *p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 65 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 67 | if (!p) { |
| 68 | printk(KERN_ERR "clock_board: Cannot allocate struct clock_board\n"); |
| 69 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 72 | p->clock_freq_regs = of_ioremap(&op->resource[0], 0, |
| 73 | resource_size(&op->resource[0]), |
| 74 | "clock_board_freq"); |
| 75 | if (!p->clock_freq_regs) { |
| 76 | printk(KERN_ERR "clock_board: Cannot map clock_freq_regs\n"); |
| 77 | goto out_free; |
David S. Miller | cecc4e9 | 2006-06-22 19:53:24 -0700 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 80 | p->clock_regs = of_ioremap(&op->resource[1], 0, |
| 81 | resource_size(&op->resource[1]), |
| 82 | "clock_board_regs"); |
| 83 | if (!p->clock_regs) { |
| 84 | printk(KERN_ERR "clock_board: Cannot map clock_regs\n"); |
| 85 | goto out_unmap_clock_freq_regs; |
| 86 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 88 | if (op->resource[2].flags) { |
| 89 | p->clock_ver_reg = of_ioremap(&op->resource[2], 0, |
| 90 | resource_size(&op->resource[2]), |
| 91 | "clock_ver_reg"); |
| 92 | if (!p->clock_ver_reg) { |
| 93 | printk(KERN_ERR "clock_board: Cannot map clock_ver_reg\n"); |
| 94 | goto out_unmap_clock_regs; |
| 95 | } |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 98 | p->num_slots = clock_board_calc_nslots(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 100 | p->leds_resource.start = (unsigned long) |
| 101 | (p->clock_regs + CLOCK_CTRL); |
| 102 | p->leds_resource.end = p->leds_resource.end; |
| 103 | p->leds_resource.name = "leds"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 105 | p->leds_pdev.name = "sunfire-clockboard-leds"; |
| 106 | p->leds_pdev.resource = &p->leds_resource; |
| 107 | p->leds_pdev.num_resources = 1; |
| 108 | p->leds_pdev.dev.parent = &op->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 110 | err = platform_device_register(&p->leds_pdev); |
| 111 | if (err) { |
| 112 | printk(KERN_ERR "clock_board: Could not register LEDS " |
| 113 | "platform device\n"); |
| 114 | goto out_unmap_clock_ver_reg; |
| 115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 117 | printk(KERN_INFO "clock_board: Detected %d slot Enterprise system.\n", |
| 118 | p->num_slots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 120 | err = 0; |
| 121 | out: |
| 122 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 124 | out_unmap_clock_ver_reg: |
| 125 | if (p->clock_ver_reg) |
| 126 | of_iounmap(&op->resource[2], p->clock_ver_reg, |
| 127 | resource_size(&op->resource[2])); |
| 128 | |
| 129 | out_unmap_clock_regs: |
| 130 | of_iounmap(&op->resource[1], p->clock_regs, |
| 131 | resource_size(&op->resource[1])); |
| 132 | |
| 133 | out_unmap_clock_freq_regs: |
| 134 | of_iounmap(&op->resource[0], p->clock_freq_regs, |
| 135 | resource_size(&op->resource[0])); |
| 136 | |
| 137 | out_free: |
| 138 | kfree(p); |
| 139 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 142 | static struct of_device_id __initdata clock_board_match[] = { |
| 143 | { |
| 144 | .name = "clock-board", |
| 145 | }, |
| 146 | {}, |
| 147 | }; |
| 148 | |
| 149 | static struct of_platform_driver clock_board_driver = { |
| 150 | .match_table = clock_board_match, |
| 151 | .probe = clock_board_probe, |
| 152 | .driver = { |
| 153 | .name = "clock_board", |
| 154 | }, |
| 155 | }; |
| 156 | |
| 157 | static int __devinit fhc_probe(struct of_device *op, |
| 158 | const struct of_device_id *match) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 160 | struct fhc *p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 161 | int err = -ENOMEM; |
| 162 | u32 reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 164 | if (!p) { |
| 165 | printk(KERN_ERR "fhc: Cannot allocate struct fhc\n"); |
| 166 | goto out; |
| 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 169 | if (!strcmp(op->node->parent->name, "central")) |
| 170 | p->central = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 172 | p->pregs = of_ioremap(&op->resource[0], 0, |
| 173 | resource_size(&op->resource[0]), |
| 174 | "fhc_pregs"); |
| 175 | if (!p->pregs) { |
| 176 | printk(KERN_ERR "fhc: Cannot map pregs\n"); |
| 177 | goto out_free; |
| 178 | } |
| 179 | |
| 180 | if (p->central) { |
| 181 | reg = upa_readl(p->pregs + FHC_PREGS_BSR); |
| 182 | p->board_num = ((reg >> 16) & 1) | ((reg >> 12) & 0x0e); |
| 183 | } else { |
| 184 | p->board_num = of_getintprop_default(op->node, "board#", -1); |
| 185 | if (p->board_num == -1) { |
| 186 | printk(KERN_ERR "fhc: No board# property\n"); |
| 187 | goto out_unmap_pregs; |
| 188 | } |
| 189 | if (upa_readl(p->pregs + FHC_PREGS_JCTRL) & FHC_JTAG_CTRL_MENAB) |
| 190 | p->jtag_master = true; |
| 191 | } |
| 192 | |
| 193 | if (!p->central) { |
| 194 | p->leds_resource.start = (unsigned long) |
| 195 | (p->pregs + FHC_PREGS_CTRL); |
| 196 | p->leds_resource.end = p->leds_resource.end; |
| 197 | p->leds_resource.name = "leds"; |
| 198 | |
| 199 | p->leds_pdev.name = "sunfire-fhc-leds"; |
| 200 | p->leds_pdev.resource = &p->leds_resource; |
| 201 | p->leds_pdev.num_resources = 1; |
| 202 | p->leds_pdev.dev.parent = &op->dev; |
| 203 | |
| 204 | err = platform_device_register(&p->leds_pdev); |
| 205 | if (err) { |
| 206 | printk(KERN_ERR "fhc: Could not register LEDS " |
| 207 | "platform device\n"); |
| 208 | goto out_unmap_pregs; |
| 209 | } |
| 210 | } |
| 211 | reg = upa_readl(p->pregs + FHC_PREGS_CTRL); |
| 212 | |
| 213 | if (!p->central) |
| 214 | reg |= FHC_CONTROL_IXIST; |
| 215 | |
| 216 | reg &= ~(FHC_CONTROL_AOFF | |
| 217 | FHC_CONTROL_BOFF | |
| 218 | FHC_CONTROL_SLINE); |
| 219 | |
| 220 | upa_writel(reg, p->pregs + FHC_PREGS_CTRL); |
| 221 | upa_readl(p->pregs + FHC_PREGS_CTRL); |
| 222 | |
| 223 | reg = upa_readl(p->pregs + FHC_PREGS_ID); |
| 224 | printk(KERN_INFO "fhc: Board #%d, Version[%x] PartID[%x] Manuf[%x] %s\n", |
| 225 | p->board_num, |
| 226 | (reg & FHC_ID_VERS) >> 28, |
| 227 | (reg & FHC_ID_PARTID) >> 12, |
| 228 | (reg & FHC_ID_MANUF) >> 1, |
| 229 | (p->jtag_master ? |
| 230 | "(JTAG Master)" : |
| 231 | (p->central ? "(Central)" : ""))); |
| 232 | |
| 233 | err = 0; |
| 234 | |
| 235 | out: |
| 236 | return err; |
| 237 | |
| 238 | out_unmap_pregs: |
| 239 | of_iounmap(&op->resource[0], p->pregs, resource_size(&op->resource[0])); |
| 240 | |
| 241 | out_free: |
| 242 | kfree(p); |
| 243 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 246 | static struct of_device_id __initdata fhc_match[] = { |
| 247 | { |
| 248 | .name = "fhc", |
| 249 | }, |
| 250 | {}, |
| 251 | }; |
| 252 | |
| 253 | static struct of_platform_driver fhc_driver = { |
| 254 | .match_table = fhc_match, |
| 255 | .probe = fhc_probe, |
| 256 | .driver = { |
| 257 | .name = "fhc", |
| 258 | }, |
| 259 | }; |
| 260 | |
| 261 | static int __init sunfire_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 263 | (void) of_register_driver(&fhc_driver, &of_platform_bus_type); |
| 264 | (void) of_register_driver(&clock_board_driver, &of_platform_bus_type); |
| 265 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
David S. Miller | b69416b | 2008-08-31 20:56:15 -0700 | [diff] [blame^] | 268 | subsys_initcall(sunfire_init); |