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