3b30f9995129b303faee1e188668353f46c1f6f2
[firefly-linux-kernel-4.4.55.git] / drivers / sbus / sbus.c
1 /* sbus.c: SBus support routines.
2  *
3  * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/of_device.h>
11
12 #include <asm/system.h>
13 #include <asm/sbus.h>
14 #include <asm/dma.h>
15 #include <asm/oplib.h>
16 #include <asm/prom.h>
17 #include <asm/irq.h>
18
19 static ssize_t
20 show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
21 {
22         struct sbus_dev *sbus;
23
24         sbus = to_sbus_device(dev);
25
26         return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name);
27 }
28
29 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL);
30
31 static void __init fill_sbus_device_iommu(struct sbus_dev *sdev)
32 {
33         struct of_device *op = of_find_device_by_node(sdev->ofdev.node);
34         struct dev_archdata *sd, *bus_sd;
35         struct sbus_bus *sbus;
36
37         sbus = sdev->bus;
38         bus_sd = &sbus->ofdev.dev.archdata;
39
40         sd = &sdev->ofdev.dev.archdata;
41         sd->iommu = bus_sd->iommu;
42         sd->stc = bus_sd->stc;
43
44         sd = &op->dev.archdata;
45         sd->iommu = bus_sd->iommu;
46         sd->stc = bus_sd->stc;
47 }
48
49 static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
50 {
51         struct dev_archdata *sd;
52         int err;
53
54         sdev->prom_node = dp->node;
55         strcpy(sdev->prom_name, dp->name);
56
57         sd = &sdev->ofdev.dev.archdata;
58         sd->prom_node = dp;
59         sd->op = &sdev->ofdev;
60
61         sdev->ofdev.node = dp;
62         if (sdev->parent)
63                 sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
64         else
65                 sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
66         sdev->ofdev.dev.bus = &sbus_bus_type;
67         dev_set_name(&sdev->ofdev.dev, "sbus[%08x]", dp->node);
68
69         if (of_device_register(&sdev->ofdev) != 0)
70                 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
71                        dp->path_component_name);
72
73         /* WE HAVE BEEN INVADED BY ALIENS! */
74         err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr);
75
76         fill_sbus_device_iommu(sdev);
77 }
78
79 static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
80 {
81         while (*root)
82                 root = &(*root)->next;
83         *root = sdev;
84         sdev->next = NULL;
85 }
86
87 static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
88 {
89         dp = dp->child;
90         while (dp) {
91                 struct sbus_dev *sdev;
92
93                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
94                 if (sdev) {
95                         sdev_insert(sdev, &parent->child);
96
97                         sdev->bus = sbus;
98                         sdev->parent = parent;
99
100                         fill_sbus_device(dp, sdev);
101
102                         walk_children(dp, sdev, sbus);
103                 }
104                 dp = dp->sibling;
105         }
106 }
107
108 static void __init build_one_sbus(struct device_node *dp, int num_sbus)
109 {
110         struct sbus_bus *sbus;
111         unsigned int sbus_clock;
112         struct device_node *dev_dp;
113
114         sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
115         if (!sbus)
116                 return;
117
118         sbus->prom_node = dp->node;
119
120         sbus_setup_iommu(sbus, dp);
121
122         printk("sbus%d: ", num_sbus);
123
124         sbus_clock = of_getintprop_default(dp, "clock-frequency",
125                                            (25*1000*1000));
126         sbus->clock_freq = sbus_clock;
127
128         printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
129                (int) (((sbus_clock/1000)%1000 != 0) ? 
130                       (((sbus_clock/1000)%1000) + 1000) : 0));
131
132         strcpy(sbus->prom_name, dp->name);
133
134         sbus->ofdev.node = dp;
135         sbus->ofdev.dev.parent = NULL;
136         sbus->ofdev.dev.bus = &sbus_bus_type;
137         dev_set_name(&sbus->ofdev.dev, "sbus%d", num_sbus);
138
139         if (of_device_register(&sbus->ofdev) != 0)
140                 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
141                        dev_name(&sbus->ofdev.dev));
142
143         dev_dp = dp->child;
144         while (dev_dp) {
145                 struct sbus_dev *sdev;
146
147                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
148                 if (sdev) {
149                         sdev_insert(sdev, &sbus->devices);
150
151                         sdev->bus = sbus;
152                         sdev->parent = NULL;
153                         sdev->ofdev.dev.archdata.iommu =
154                                 sbus->ofdev.dev.archdata.iommu;
155                         sdev->ofdev.dev.archdata.stc =
156                                 sbus->ofdev.dev.archdata.stc;
157
158                         fill_sbus_device(dev_dp, sdev);
159
160                         walk_children(dev_dp, sdev, sbus);
161                 }
162                 dev_dp = dev_dp->sibling;
163         }
164 }
165
166 static int __init sbus_init(void)
167 {
168         struct device_node *dp;
169         const char *sbus_name = "sbus";
170         int num_sbus = 0;
171
172         if (sbus_arch_preinit())
173                 return 0;
174
175         if (sparc_cpu_model == sun4d)
176                 sbus_name = "sbi";
177
178         for_each_node_by_name(dp, sbus_name) {
179                 build_one_sbus(dp, num_sbus);
180                 num_sbus++;
181
182         }
183
184         sbus_arch_postinit();
185
186         return 0;
187 }
188
189 subsys_initcall(sbus_init);