1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a88b5ba8SSam Ravnborg /* central.c: Central FHC driver for Sunfire/Starfire/Wildfire.
3a88b5ba8SSam Ravnborg *
4a88b5ba8SSam Ravnborg * Copyright (C) 1997, 1999, 2008 David S. Miller (davem@davemloft.net)
5a88b5ba8SSam Ravnborg */
6a88b5ba8SSam Ravnborg
7a88b5ba8SSam Ravnborg #include <linux/kernel.h>
8a88b5ba8SSam Ravnborg #include <linux/types.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
107b64db60SPaul Gortmaker #include <linux/export.h>
11a88b5ba8SSam Ravnborg #include <linux/string.h>
12a88b5ba8SSam Ravnborg #include <linux/init.h>
13*263291faSRob Herring #include <linux/of.h>
14a88b5ba8SSam Ravnborg #include <linux/platform_device.h>
15a88b5ba8SSam Ravnborg
16a88b5ba8SSam Ravnborg #include <asm/fhc.h>
17a88b5ba8SSam Ravnborg #include <asm/upa.h>
18a88b5ba8SSam Ravnborg
19a88b5ba8SSam Ravnborg struct clock_board {
20a88b5ba8SSam Ravnborg void __iomem *clock_freq_regs;
21a88b5ba8SSam Ravnborg void __iomem *clock_regs;
22a88b5ba8SSam Ravnborg void __iomem *clock_ver_reg;
23a88b5ba8SSam Ravnborg int num_slots;
24a88b5ba8SSam Ravnborg struct resource leds_resource;
25a88b5ba8SSam Ravnborg struct platform_device leds_pdev;
26a88b5ba8SSam Ravnborg };
27a88b5ba8SSam Ravnborg
28a88b5ba8SSam Ravnborg struct fhc {
29a88b5ba8SSam Ravnborg void __iomem *pregs;
30a88b5ba8SSam Ravnborg bool central;
31a88b5ba8SSam Ravnborg bool jtag_master;
32a88b5ba8SSam Ravnborg int board_num;
33a88b5ba8SSam Ravnborg struct resource leds_resource;
34a88b5ba8SSam Ravnborg struct platform_device leds_pdev;
35a88b5ba8SSam Ravnborg };
36a88b5ba8SSam Ravnborg
clock_board_calc_nslots(struct clock_board * p)377c9503b8SGreg Kroah-Hartman static int clock_board_calc_nslots(struct clock_board *p)
38a88b5ba8SSam Ravnborg {
39a88b5ba8SSam Ravnborg u8 reg = upa_readb(p->clock_regs + CLOCK_STAT1) & 0xc0;
40a88b5ba8SSam Ravnborg
41a88b5ba8SSam Ravnborg switch (reg) {
42a88b5ba8SSam Ravnborg case 0x40:
43a88b5ba8SSam Ravnborg return 16;
44a88b5ba8SSam Ravnborg
45a88b5ba8SSam Ravnborg case 0xc0:
46a88b5ba8SSam Ravnborg return 8;
47a88b5ba8SSam Ravnborg
48a88b5ba8SSam Ravnborg case 0x80:
49a88b5ba8SSam Ravnborg reg = 0;
50a88b5ba8SSam Ravnborg if (p->clock_ver_reg)
51a88b5ba8SSam Ravnborg reg = upa_readb(p->clock_ver_reg);
52a88b5ba8SSam Ravnborg if (reg) {
53a88b5ba8SSam Ravnborg if (reg & 0x80)
54a88b5ba8SSam Ravnborg return 4;
55a88b5ba8SSam Ravnborg else
56a88b5ba8SSam Ravnborg return 5;
57a88b5ba8SSam Ravnborg }
58df561f66SGustavo A. R. Silva fallthrough;
59a88b5ba8SSam Ravnborg default:
60a88b5ba8SSam Ravnborg return 4;
61a88b5ba8SSam Ravnborg }
62a88b5ba8SSam Ravnborg }
63a88b5ba8SSam Ravnborg
clock_board_probe(struct platform_device * op)647c9503b8SGreg Kroah-Hartman static int clock_board_probe(struct platform_device *op)
65a88b5ba8SSam Ravnborg {
66a88b5ba8SSam Ravnborg struct clock_board *p = kzalloc(sizeof(*p), GFP_KERNEL);
67a88b5ba8SSam Ravnborg int err = -ENOMEM;
68a88b5ba8SSam Ravnborg
69a88b5ba8SSam Ravnborg if (!p) {
70a88b5ba8SSam Ravnborg printk(KERN_ERR "clock_board: Cannot allocate struct clock_board\n");
71a88b5ba8SSam Ravnborg goto out;
72a88b5ba8SSam Ravnborg }
73a88b5ba8SSam Ravnborg
74a88b5ba8SSam Ravnborg p->clock_freq_regs = of_ioremap(&op->resource[0], 0,
75a88b5ba8SSam Ravnborg resource_size(&op->resource[0]),
76a88b5ba8SSam Ravnborg "clock_board_freq");
77a88b5ba8SSam Ravnborg if (!p->clock_freq_regs) {
78a88b5ba8SSam Ravnborg printk(KERN_ERR "clock_board: Cannot map clock_freq_regs\n");
79a88b5ba8SSam Ravnborg goto out_free;
80a88b5ba8SSam Ravnborg }
81a88b5ba8SSam Ravnborg
82a88b5ba8SSam Ravnborg p->clock_regs = of_ioremap(&op->resource[1], 0,
83a88b5ba8SSam Ravnborg resource_size(&op->resource[1]),
84a88b5ba8SSam Ravnborg "clock_board_regs");
85a88b5ba8SSam Ravnborg if (!p->clock_regs) {
86a88b5ba8SSam Ravnborg printk(KERN_ERR "clock_board: Cannot map clock_regs\n");
87a88b5ba8SSam Ravnborg goto out_unmap_clock_freq_regs;
88a88b5ba8SSam Ravnborg }
89a88b5ba8SSam Ravnborg
90a88b5ba8SSam Ravnborg if (op->resource[2].flags) {
91a88b5ba8SSam Ravnborg p->clock_ver_reg = of_ioremap(&op->resource[2], 0,
92a88b5ba8SSam Ravnborg resource_size(&op->resource[2]),
93a88b5ba8SSam Ravnborg "clock_ver_reg");
94a88b5ba8SSam Ravnborg if (!p->clock_ver_reg) {
95a88b5ba8SSam Ravnborg printk(KERN_ERR "clock_board: Cannot map clock_ver_reg\n");
96a88b5ba8SSam Ravnborg goto out_unmap_clock_regs;
97a88b5ba8SSam Ravnborg }
98a88b5ba8SSam Ravnborg }
99a88b5ba8SSam Ravnborg
100a88b5ba8SSam Ravnborg p->num_slots = clock_board_calc_nslots(p);
101a88b5ba8SSam Ravnborg
102a88b5ba8SSam Ravnborg p->leds_resource.start = (unsigned long)
103a88b5ba8SSam Ravnborg (p->clock_regs + CLOCK_CTRL);
10409317146SRoel Kluin p->leds_resource.end = p->leds_resource.start;
105a88b5ba8SSam Ravnborg p->leds_resource.name = "leds";
106a88b5ba8SSam Ravnborg
107a88b5ba8SSam Ravnborg p->leds_pdev.name = "sunfire-clockboard-leds";
108b7c18c1bSDavid S. Miller p->leds_pdev.id = -1;
109a88b5ba8SSam Ravnborg p->leds_pdev.resource = &p->leds_resource;
110a88b5ba8SSam Ravnborg p->leds_pdev.num_resources = 1;
111a88b5ba8SSam Ravnborg p->leds_pdev.dev.parent = &op->dev;
112a88b5ba8SSam Ravnborg
113a88b5ba8SSam Ravnborg err = platform_device_register(&p->leds_pdev);
114a88b5ba8SSam Ravnborg if (err) {
115a88b5ba8SSam Ravnborg printk(KERN_ERR "clock_board: Could not register LEDS "
116a88b5ba8SSam Ravnborg "platform device\n");
117a88b5ba8SSam Ravnborg goto out_unmap_clock_ver_reg;
118a88b5ba8SSam Ravnborg }
119a88b5ba8SSam Ravnborg
120a88b5ba8SSam Ravnborg printk(KERN_INFO "clock_board: Detected %d slot Enterprise system.\n",
121a88b5ba8SSam Ravnborg p->num_slots);
122a88b5ba8SSam Ravnborg
123a88b5ba8SSam Ravnborg err = 0;
124a88b5ba8SSam Ravnborg out:
125a88b5ba8SSam Ravnborg return err;
126a88b5ba8SSam Ravnborg
127a88b5ba8SSam Ravnborg out_unmap_clock_ver_reg:
128a88b5ba8SSam Ravnborg if (p->clock_ver_reg)
129a88b5ba8SSam Ravnborg of_iounmap(&op->resource[2], p->clock_ver_reg,
130a88b5ba8SSam Ravnborg resource_size(&op->resource[2]));
131a88b5ba8SSam Ravnborg
132a88b5ba8SSam Ravnborg out_unmap_clock_regs:
133a88b5ba8SSam Ravnborg of_iounmap(&op->resource[1], p->clock_regs,
134a88b5ba8SSam Ravnborg resource_size(&op->resource[1]));
135a88b5ba8SSam Ravnborg
136a88b5ba8SSam Ravnborg out_unmap_clock_freq_regs:
137a88b5ba8SSam Ravnborg of_iounmap(&op->resource[0], p->clock_freq_regs,
138a88b5ba8SSam Ravnborg resource_size(&op->resource[0]));
139a88b5ba8SSam Ravnborg
140a88b5ba8SSam Ravnborg out_free:
141a88b5ba8SSam Ravnborg kfree(p);
142a88b5ba8SSam Ravnborg goto out;
143a88b5ba8SSam Ravnborg }
144a88b5ba8SSam Ravnborg
1453628aa06SDavid S. Miller static const struct of_device_id clock_board_match[] = {
146a88b5ba8SSam Ravnborg {
147a88b5ba8SSam Ravnborg .name = "clock-board",
148a88b5ba8SSam Ravnborg },
149a88b5ba8SSam Ravnborg {},
150a88b5ba8SSam Ravnborg };
151a88b5ba8SSam Ravnborg
1524ebb24f7SGrant Likely static struct platform_driver clock_board_driver = {
153a88b5ba8SSam Ravnborg .probe = clock_board_probe,
154a88b5ba8SSam Ravnborg .driver = {
155a88b5ba8SSam Ravnborg .name = "clock_board",
1564018294bSGrant Likely .of_match_table = clock_board_match,
157a88b5ba8SSam Ravnborg },
158a88b5ba8SSam Ravnborg };
159a88b5ba8SSam Ravnborg
fhc_probe(struct platform_device * op)1607c9503b8SGreg Kroah-Hartman static int fhc_probe(struct platform_device *op)
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
17129c990dfSRob Herring if (of_node_name_eq(op->dev.of_node->parent, "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 {
18661c7a080SGrant 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
2493628aa06SDavid S. Miller static const struct of_device_id fhc_match[] = {
250a88b5ba8SSam Ravnborg {
251a88b5ba8SSam Ravnborg .name = "fhc",
252a88b5ba8SSam Ravnborg },
253a88b5ba8SSam Ravnborg {},
254a88b5ba8SSam Ravnborg };
255a88b5ba8SSam Ravnborg
2564ebb24f7SGrant Likely static struct platform_driver fhc_driver = {
257a88b5ba8SSam Ravnborg .probe = fhc_probe,
258a88b5ba8SSam Ravnborg .driver = {
259a88b5ba8SSam Ravnborg .name = "fhc",
2604018294bSGrant Likely .of_match_table = fhc_match,
261a88b5ba8SSam Ravnborg },
262a88b5ba8SSam Ravnborg };
263a88b5ba8SSam Ravnborg
sunfire_init(void)264a88b5ba8SSam Ravnborg static int __init sunfire_init(void)
265a88b5ba8SSam Ravnborg {
2664ebb24f7SGrant Likely (void) platform_driver_register(&fhc_driver);
2674ebb24f7SGrant Likely (void) platform_driver_register(&clock_board_driver);
268a88b5ba8SSam Ravnborg return 0;
269a88b5ba8SSam Ravnborg }
270a88b5ba8SSam Ravnborg
271a5a737e0SDavid S. Miller fs_initcall(sunfire_init);
272