blob: cfa2624c5332ea43f20c9232555b80a3e95fde59 [file] [log] [blame]
David S. Millerd979f172007-10-27 00:13:04 -07001/* central.c: Central FHC driver for Sunfire/Starfire/Wildfire.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Millerb69416b2008-08-31 20:56:15 -07003 * Copyright (C) 1997, 1999, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6#include <linux/kernel.h>
7#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
David S. Millerb69416b2008-08-31 20:56:15 -070011#include <linux/of_device.h>
12#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/fhc.h>
David S. Millerb69416b2008-08-31 20:56:15 -070015#include <asm/upa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
David S. Millerb69416b2008-08-31 20:56:15 -070017struct clock_board {
18 void __iomem *clock_freq_regs;
19 void __iomem *clock_regs;
20 void __iomem *clock_ver_reg;
21 int num_slots;
22 struct resource leds_resource;
23 struct platform_device leds_pdev;
24};
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David S. Millerb69416b2008-08-31 20:56:15 -070026struct fhc {
27 void __iomem *pregs;
28 bool central;
29 bool jtag_master;
30 int board_num;
31 struct resource leds_resource;
32 struct platform_device leds_pdev;
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Millerb69416b2008-08-31 20:56:15 -070035static int __devinit clock_board_calc_nslots(struct clock_board *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
David S. Millerb69416b2008-08-31 20:56:15 -070037 u8 reg = upa_readb(p->clock_regs + CLOCK_STAT1) & 0xc0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
David S. Millerb69416b2008-08-31 20:56:15 -070039 switch (reg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 case 0x40:
David S. Millerb69416b2008-08-31 20:56:15 -070041 return 16;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 case 0xc0:
David S. Millerb69416b2008-08-31 20:56:15 -070044 return 8;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 case 0x80:
David S. Millerb69416b2008-08-31 20:56:15 -070047 reg = 0;
48 if (p->clock_ver_reg)
49 reg = upa_readb(p->clock_ver_reg);
50 if (reg) {
51 if (reg & 0x80)
52 return 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 else
David S. Millerb69416b2008-08-31 20:56:15 -070054 return 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
David S. Millerb69416b2008-08-31 20:56:15 -070056 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 default:
David S. Millerb69416b2008-08-31 20:56:15 -070058 return 4;
59 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Grant Likelycd4cd732010-07-22 16:04:30 -060062static int __devinit clock_board_probe(struct platform_device *op,
David S. Millerb69416b2008-08-31 20:56:15 -070063 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
David S. Millerb69416b2008-08-31 20:56:15 -070065 struct clock_board *p = kzalloc(sizeof(*p), GFP_KERNEL);
66 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
David S. Millerb69416b2008-08-31 20:56:15 -070068 if (!p) {
69 printk(KERN_ERR "clock_board: Cannot allocate struct clock_board\n");
70 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
David S. Millerb69416b2008-08-31 20:56:15 -070073 p->clock_freq_regs = of_ioremap(&op->resource[0], 0,
74 resource_size(&op->resource[0]),
75 "clock_board_freq");
76 if (!p->clock_freq_regs) {
77 printk(KERN_ERR "clock_board: Cannot map clock_freq_regs\n");
78 goto out_free;
David S. Millercecc4e92006-06-22 19:53:24 -070079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
David S. Millerb69416b2008-08-31 20:56:15 -070081 p->clock_regs = of_ioremap(&op->resource[1], 0,
82 resource_size(&op->resource[1]),
83 "clock_board_regs");
84 if (!p->clock_regs) {
85 printk(KERN_ERR "clock_board: Cannot map clock_regs\n");
86 goto out_unmap_clock_freq_regs;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David S. Millerb69416b2008-08-31 20:56:15 -070089 if (op->resource[2].flags) {
90 p->clock_ver_reg = of_ioremap(&op->resource[2], 0,
91 resource_size(&op->resource[2]),
92 "clock_ver_reg");
93 if (!p->clock_ver_reg) {
94 printk(KERN_ERR "clock_board: Cannot map clock_ver_reg\n");
95 goto out_unmap_clock_regs;
96 }
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David S. Millerb69416b2008-08-31 20:56:15 -070099 p->num_slots = clock_board_calc_nslots(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
David S. Millerb69416b2008-08-31 20:56:15 -0700101 p->leds_resource.start = (unsigned long)
102 (p->clock_regs + CLOCK_CTRL);
Roel Kluin09317142010-01-15 01:27:49 -0800103 p->leds_resource.end = p->leds_resource.start;
David S. Millerb69416b2008-08-31 20:56:15 -0700104 p->leds_resource.name = "leds";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David S. Millerb69416b2008-08-31 20:56:15 -0700106 p->leds_pdev.name = "sunfire-clockboard-leds";
David S. Millerb7c18c12009-01-18 22:42:15 -0800107 p->leds_pdev.id = -1;
David S. Millerb69416b2008-08-31 20:56:15 -0700108 p->leds_pdev.resource = &p->leds_resource;
109 p->leds_pdev.num_resources = 1;
110 p->leds_pdev.dev.parent = &op->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
David S. Millerb69416b2008-08-31 20:56:15 -0700112 err = platform_device_register(&p->leds_pdev);
113 if (err) {
114 printk(KERN_ERR "clock_board: Could not register LEDS "
115 "platform device\n");
116 goto out_unmap_clock_ver_reg;
117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
David S. Millerb69416b2008-08-31 20:56:15 -0700119 printk(KERN_INFO "clock_board: Detected %d slot Enterprise system.\n",
120 p->num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David S. Millerb69416b2008-08-31 20:56:15 -0700122 err = 0;
123out:
124 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David S. Millerb69416b2008-08-31 20:56:15 -0700126out_unmap_clock_ver_reg:
127 if (p->clock_ver_reg)
128 of_iounmap(&op->resource[2], p->clock_ver_reg,
129 resource_size(&op->resource[2]));
130
131out_unmap_clock_regs:
132 of_iounmap(&op->resource[1], p->clock_regs,
133 resource_size(&op->resource[1]));
134
135out_unmap_clock_freq_regs:
136 of_iounmap(&op->resource[0], p->clock_freq_regs,
137 resource_size(&op->resource[0]));
138
139out_free:
140 kfree(p);
141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
David S. Millerb69416b2008-08-31 20:56:15 -0700144static struct of_device_id __initdata clock_board_match[] = {
145 {
146 .name = "clock-board",
147 },
148 {},
149};
150
151static struct of_platform_driver clock_board_driver = {
David S. Millerb69416b2008-08-31 20:56:15 -0700152 .probe = clock_board_probe,
Grant Likely40182942010-04-13 16:13:02 -0700153 .driver = {
154 .name = "clock_board",
155 .owner = THIS_MODULE,
156 .of_match_table = clock_board_match,
David S. Millerb69416b2008-08-31 20:56:15 -0700157 },
158};
159
Grant Likelycd4cd732010-07-22 16:04:30 -0600160static int __devinit fhc_probe(struct platform_device *op,
David S. Millerb69416b2008-08-31 20:56:15 -0700161 const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
David S. Millerb69416b2008-08-31 20:56:15 -0700163 struct fhc *p = kzalloc(sizeof(*p), GFP_KERNEL);
164 int err = -ENOMEM;
165 u32 reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David S. Millerb69416b2008-08-31 20:56:15 -0700167 if (!p) {
168 printk(KERN_ERR "fhc: Cannot allocate struct fhc\n");
169 goto out;
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Grant Likely61c7a082010-04-13 16:12:29 -0700172 if (!strcmp(op->dev.of_node->parent->name, "central"))
David S. Millerb69416b2008-08-31 20:56:15 -0700173 p->central = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
David S. Millerb69416b2008-08-31 20:56:15 -0700175 p->pregs = of_ioremap(&op->resource[0], 0,
176 resource_size(&op->resource[0]),
177 "fhc_pregs");
178 if (!p->pregs) {
179 printk(KERN_ERR "fhc: Cannot map pregs\n");
180 goto out_free;
181 }
182
183 if (p->central) {
184 reg = upa_readl(p->pregs + FHC_PREGS_BSR);
185 p->board_num = ((reg >> 16) & 1) | ((reg >> 12) & 0x0e);
186 } else {
Grant Likely61c7a082010-04-13 16:12:29 -0700187 p->board_num = of_getintprop_default(op->dev.of_node, "board#", -1);
David S. Millerb69416b2008-08-31 20:56:15 -0700188 if (p->board_num == -1) {
189 printk(KERN_ERR "fhc: No board# property\n");
190 goto out_unmap_pregs;
191 }
192 if (upa_readl(p->pregs + FHC_PREGS_JCTRL) & FHC_JTAG_CTRL_MENAB)
193 p->jtag_master = true;
194 }
195
196 if (!p->central) {
197 p->leds_resource.start = (unsigned long)
198 (p->pregs + FHC_PREGS_CTRL);
Roel Kluin09317142010-01-15 01:27:49 -0800199 p->leds_resource.end = p->leds_resource.start;
David S. Millerb69416b2008-08-31 20:56:15 -0700200 p->leds_resource.name = "leds";
201
202 p->leds_pdev.name = "sunfire-fhc-leds";
David S. Millerb7c18c12009-01-18 22:42:15 -0800203 p->leds_pdev.id = p->board_num;
David S. Millerb69416b2008-08-31 20:56:15 -0700204 p->leds_pdev.resource = &p->leds_resource;
205 p->leds_pdev.num_resources = 1;
206 p->leds_pdev.dev.parent = &op->dev;
207
208 err = platform_device_register(&p->leds_pdev);
209 if (err) {
210 printk(KERN_ERR "fhc: Could not register LEDS "
211 "platform device\n");
212 goto out_unmap_pregs;
213 }
214 }
215 reg = upa_readl(p->pregs + FHC_PREGS_CTRL);
216
217 if (!p->central)
218 reg |= FHC_CONTROL_IXIST;
219
220 reg &= ~(FHC_CONTROL_AOFF |
221 FHC_CONTROL_BOFF |
222 FHC_CONTROL_SLINE);
223
224 upa_writel(reg, p->pregs + FHC_PREGS_CTRL);
225 upa_readl(p->pregs + FHC_PREGS_CTRL);
226
227 reg = upa_readl(p->pregs + FHC_PREGS_ID);
228 printk(KERN_INFO "fhc: Board #%d, Version[%x] PartID[%x] Manuf[%x] %s\n",
229 p->board_num,
230 (reg & FHC_ID_VERS) >> 28,
231 (reg & FHC_ID_PARTID) >> 12,
232 (reg & FHC_ID_MANUF) >> 1,
233 (p->jtag_master ?
234 "(JTAG Master)" :
235 (p->central ? "(Central)" : "")));
236
237 err = 0;
238
239out:
240 return err;
241
242out_unmap_pregs:
243 of_iounmap(&op->resource[0], p->pregs, resource_size(&op->resource[0]));
244
245out_free:
246 kfree(p);
247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
David S. Millerb69416b2008-08-31 20:56:15 -0700250static struct of_device_id __initdata fhc_match[] = {
251 {
252 .name = "fhc",
253 },
254 {},
255};
256
257static struct of_platform_driver fhc_driver = {
David S. Millerb69416b2008-08-31 20:56:15 -0700258 .probe = fhc_probe,
Grant Likely40182942010-04-13 16:13:02 -0700259 .driver = {
260 .name = "fhc",
261 .owner = THIS_MODULE,
262 .of_match_table = fhc_match,
David S. Millerb69416b2008-08-31 20:56:15 -0700263 },
264};
265
266static int __init sunfire_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Grant Likely1ab1d632010-06-24 15:14:37 -0600268 (void) of_register_platform_driver(&fhc_driver);
269 (void) of_register_platform_driver(&clock_board_driver);
David S. Millerb69416b2008-08-31 20:56:15 -0700270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
David S. Millerb69416b2008-08-31 20:56:15 -0700273subsys_initcall(sunfire_init);