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