[POWERPC] CPM: Change from fsl,brg-frequency to brg/clock-frequency
[linux-2.6] / arch / powerpc / sysdev / fsl_soc.c
1 /*
2  * FSL SoC setup code
3  *
4  * Maintained by Kumar Gala (see MAINTAINERS for contact information)
5  *
6  * 2006 (c) MontaVista Software, Inc.
7  * Vitaly Bordug <vbordug@ru.mvista.com>
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14
15 #include <linux/stddef.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/errno.h>
19 #include <linux/major.h>
20 #include <linux/delay.h>
21 #include <linux/irq.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/of_platform.h>
26 #include <linux/phy.h>
27 #include <linux/spi/spi.h>
28 #include <linux/fsl_devices.h>
29 #include <linux/fs_enet_pd.h>
30 #include <linux/fs_uart_pd.h>
31
32 #include <asm/system.h>
33 #include <asm/atomic.h>
34 #include <asm/io.h>
35 #include <asm/irq.h>
36 #include <asm/time.h>
37 #include <asm/prom.h>
38 #include <sysdev/fsl_soc.h>
39 #include <mm/mmu_decl.h>
40 #include <asm/cpm2.h>
41
42 extern void init_fcc_ioports(struct fs_platform_info*);
43 extern void init_fec_ioports(struct fs_platform_info*);
44 extern void init_smc_ioports(struct fs_uart_platform_info*);
45 static phys_addr_t immrbase = -1;
46
47 phys_addr_t get_immrbase(void)
48 {
49         struct device_node *soc;
50
51         if (immrbase != -1)
52                 return immrbase;
53
54         soc = of_find_node_by_type(NULL, "soc");
55         if (soc) {
56                 int size;
57                 const void *prop = of_get_property(soc, "reg", &size);
58
59                 if (prop)
60                         immrbase = of_translate_address(soc, prop);
61                 of_node_put(soc);
62         }
63
64         return immrbase;
65 }
66
67 EXPORT_SYMBOL(get_immrbase);
68
69 #if defined(CONFIG_CPM2) || defined(CONFIG_8xx)
70
71 static u32 brgfreq = -1;
72
73 u32 get_brgfreq(void)
74 {
75         struct device_node *node;
76         const unsigned int *prop;
77         int size;
78
79         if (brgfreq != -1)
80                 return brgfreq;
81
82         node = of_find_compatible_node(NULL, NULL, "fsl,cpm-brg");
83         if (node) {
84                 prop = of_get_property(node, "clock-frequency", &size);
85                 if (prop && size == 4)
86                         brgfreq = *prop;
87
88                 of_node_put(node);
89                 return brgfreq;
90         }
91
92         /* Legacy device binding -- will go away when no users are left. */
93         node = of_find_node_by_type(NULL, "cpm");
94         if (node) {
95                 prop = of_get_property(node, "brg-frequency", &size);
96                 if (prop && size == 4)
97                         brgfreq = *prop;
98
99                 of_node_put(node);
100         }
101
102         return brgfreq;
103 }
104
105 EXPORT_SYMBOL(get_brgfreq);
106
107 static u32 fs_baudrate = -1;
108
109 u32 get_baudrate(void)
110 {
111         struct device_node *node;
112
113         if (fs_baudrate != -1)
114                 return fs_baudrate;
115
116         node = of_find_node_by_type(NULL, "serial");
117         if (node) {
118                 int size;
119                 const unsigned int *prop = of_get_property(node,
120                                 "current-speed", &size);
121
122                 if (prop)
123                         fs_baudrate = *prop;
124                 of_node_put(node);
125         }
126
127         return fs_baudrate;
128 }
129
130 EXPORT_SYMBOL(get_baudrate);
131 #endif /* CONFIG_CPM2 */
132
133 static int __init gfar_mdio_of_init(void)
134 {
135         struct device_node *np;
136         unsigned int i;
137         struct platform_device *mdio_dev;
138         struct resource res;
139         int ret;
140
141         for (np = NULL, i = 0;
142              (np = of_find_compatible_node(np, "mdio", "gianfar")) != NULL;
143              i++) {
144                 int k;
145                 struct device_node *child = NULL;
146                 struct gianfar_mdio_data mdio_data;
147
148                 memset(&res, 0, sizeof(res));
149                 memset(&mdio_data, 0, sizeof(mdio_data));
150
151                 ret = of_address_to_resource(np, 0, &res);
152                 if (ret)
153                         goto err;
154
155                 mdio_dev =
156                     platform_device_register_simple("fsl-gianfar_mdio",
157                                                     res.start, &res, 1);
158                 if (IS_ERR(mdio_dev)) {
159                         ret = PTR_ERR(mdio_dev);
160                         goto err;
161                 }
162
163                 for (k = 0; k < 32; k++)
164                         mdio_data.irq[k] = PHY_POLL;
165
166                 while ((child = of_get_next_child(np, child)) != NULL) {
167                         int irq = irq_of_parse_and_map(child, 0);
168                         if (irq != NO_IRQ) {
169                                 const u32 *id = of_get_property(child,
170                                                         "reg", NULL);
171                                 mdio_data.irq[*id] = irq;
172                         }
173                 }
174
175                 ret =
176                     platform_device_add_data(mdio_dev, &mdio_data,
177                                              sizeof(struct gianfar_mdio_data));
178                 if (ret)
179                         goto unreg;
180         }
181
182         return 0;
183
184 unreg:
185         platform_device_unregister(mdio_dev);
186 err:
187         return ret;
188 }
189
190 arch_initcall(gfar_mdio_of_init);
191
192 static const char *gfar_tx_intr = "tx";
193 static const char *gfar_rx_intr = "rx";
194 static const char *gfar_err_intr = "error";
195
196
197 static int __init gfar_of_init(void)
198 {
199         struct device_node *np;
200         unsigned int i;
201         struct platform_device *gfar_dev;
202         struct resource res;
203         int ret;
204
205         for (np = NULL, i = 0;
206              (np = of_find_compatible_node(np, "network", "gianfar")) != NULL;
207              i++) {
208                 struct resource r[4];
209                 struct device_node *phy, *mdio;
210                 struct gianfar_platform_data gfar_data;
211                 const unsigned int *id;
212                 const char *model;
213                 const char *ctype;
214                 const void *mac_addr;
215                 const phandle *ph;
216                 int n_res = 2;
217
218                 memset(r, 0, sizeof(r));
219                 memset(&gfar_data, 0, sizeof(gfar_data));
220
221                 ret = of_address_to_resource(np, 0, &r[0]);
222                 if (ret)
223                         goto err;
224
225                 of_irq_to_resource(np, 0, &r[1]);
226
227                 model = of_get_property(np, "model", NULL);
228
229                 /* If we aren't the FEC we have multiple interrupts */
230                 if (model && strcasecmp(model, "FEC")) {
231                         r[1].name = gfar_tx_intr;
232
233                         r[2].name = gfar_rx_intr;
234                         of_irq_to_resource(np, 1, &r[2]);
235
236                         r[3].name = gfar_err_intr;
237                         of_irq_to_resource(np, 2, &r[3]);
238
239                         n_res += 2;
240                 }
241
242                 gfar_dev =
243                     platform_device_register_simple("fsl-gianfar", i, &r[0],
244                                                     n_res);
245
246                 if (IS_ERR(gfar_dev)) {
247                         ret = PTR_ERR(gfar_dev);
248                         goto err;
249                 }
250
251                 mac_addr = of_get_mac_address(np);
252                 if (mac_addr)
253                         memcpy(gfar_data.mac_addr, mac_addr, 6);
254
255                 if (model && !strcasecmp(model, "TSEC"))
256                         gfar_data.device_flags =
257                             FSL_GIANFAR_DEV_HAS_GIGABIT |
258                             FSL_GIANFAR_DEV_HAS_COALESCE |
259                             FSL_GIANFAR_DEV_HAS_RMON |
260                             FSL_GIANFAR_DEV_HAS_MULTI_INTR;
261                 if (model && !strcasecmp(model, "eTSEC"))
262                         gfar_data.device_flags =
263                             FSL_GIANFAR_DEV_HAS_GIGABIT |
264                             FSL_GIANFAR_DEV_HAS_COALESCE |
265                             FSL_GIANFAR_DEV_HAS_RMON |
266                             FSL_GIANFAR_DEV_HAS_MULTI_INTR |
267                             FSL_GIANFAR_DEV_HAS_CSUM |
268                             FSL_GIANFAR_DEV_HAS_VLAN |
269                             FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
270
271                 ctype = of_get_property(np, "phy-connection-type", NULL);
272
273                 /* We only care about rgmii-id.  The rest are autodetected */
274                 if (ctype && !strcmp(ctype, "rgmii-id"))
275                         gfar_data.interface = PHY_INTERFACE_MODE_RGMII_ID;
276                 else
277                         gfar_data.interface = PHY_INTERFACE_MODE_MII;
278
279                 ph = of_get_property(np, "phy-handle", NULL);
280                 phy = of_find_node_by_phandle(*ph);
281
282                 if (phy == NULL) {
283                         ret = -ENODEV;
284                         goto unreg;
285                 }
286
287                 mdio = of_get_parent(phy);
288
289                 id = of_get_property(phy, "reg", NULL);
290                 ret = of_address_to_resource(mdio, 0, &res);
291                 if (ret) {
292                         of_node_put(phy);
293                         of_node_put(mdio);
294                         goto unreg;
295                 }
296
297                 gfar_data.phy_id = *id;
298                 gfar_data.bus_id = res.start;
299
300                 of_node_put(phy);
301                 of_node_put(mdio);
302
303                 ret =
304                     platform_device_add_data(gfar_dev, &gfar_data,
305                                              sizeof(struct
306                                                     gianfar_platform_data));
307                 if (ret)
308                         goto unreg;
309         }
310
311         return 0;
312
313 unreg:
314         platform_device_unregister(gfar_dev);
315 err:
316         return ret;
317 }
318
319 arch_initcall(gfar_of_init);
320
321 #ifdef CONFIG_I2C_BOARDINFO
322 #include <linux/i2c.h>
323 struct i2c_driver_device {
324         char    *of_device;
325         char    *i2c_driver;
326         char    *i2c_type;
327 };
328
329 static struct i2c_driver_device i2c_devices[] __initdata = {
330         {"ricoh,rs5c372a", "rtc-rs5c372", "rs5c372a",},
331         {"ricoh,rs5c372b", "rtc-rs5c372", "rs5c372b",},
332         {"ricoh,rv5c386",  "rtc-rs5c372", "rv5c386",},
333         {"ricoh,rv5c387a", "rtc-rs5c372", "rv5c387a",},
334 };
335
336 static int __init of_find_i2c_driver(struct device_node *node,
337                                      struct i2c_board_info *info)
338 {
339         int i;
340
341         for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
342                 if (!of_device_is_compatible(node, i2c_devices[i].of_device))
343                         continue;
344                 if (strlcpy(info->driver_name, i2c_devices[i].i2c_driver,
345                             KOBJ_NAME_LEN) >= KOBJ_NAME_LEN ||
346                     strlcpy(info->type, i2c_devices[i].i2c_type,
347                             I2C_NAME_SIZE) >= I2C_NAME_SIZE)
348                         return -ENOMEM;
349                 return 0;
350         }
351         return -ENODEV;
352 }
353
354 static void __init of_register_i2c_devices(struct device_node *adap_node,
355                                            int bus_num)
356 {
357         struct device_node *node = NULL;
358
359         while ((node = of_get_next_child(adap_node, node))) {
360                 struct i2c_board_info info;
361                 const u32 *addr;
362                 int len;
363
364                 addr = of_get_property(node, "reg", &len);
365                 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
366                         printk(KERN_WARNING "fsl_ioc.c: invalid i2c device entry\n");
367                         continue;
368                 }
369
370                 info.irq = irq_of_parse_and_map(node, 0);
371                 if (info.irq == NO_IRQ)
372                         info.irq = -1;
373
374                 if (of_find_i2c_driver(node, &info) < 0)
375                         continue;
376
377                 info.platform_data = NULL;
378                 info.addr = *addr;
379
380                 i2c_register_board_info(bus_num, &info, 1);
381         }
382 }
383
384 static int __init fsl_i2c_of_init(void)
385 {
386         struct device_node *np;
387         unsigned int i;
388         struct platform_device *i2c_dev;
389         int ret;
390
391         for (np = NULL, i = 0;
392              (np = of_find_compatible_node(np, "i2c", "fsl-i2c")) != NULL;
393              i++) {
394                 struct resource r[2];
395                 struct fsl_i2c_platform_data i2c_data;
396                 const unsigned char *flags = NULL;
397
398                 memset(&r, 0, sizeof(r));
399                 memset(&i2c_data, 0, sizeof(i2c_data));
400
401                 ret = of_address_to_resource(np, 0, &r[0]);
402                 if (ret)
403                         goto err;
404
405                 of_irq_to_resource(np, 0, &r[1]);
406
407                 i2c_dev = platform_device_register_simple("fsl-i2c", i, r, 2);
408                 if (IS_ERR(i2c_dev)) {
409                         ret = PTR_ERR(i2c_dev);
410                         goto err;
411                 }
412
413                 i2c_data.device_flags = 0;
414                 flags = of_get_property(np, "dfsrr", NULL);
415                 if (flags)
416                         i2c_data.device_flags |= FSL_I2C_DEV_SEPARATE_DFSRR;
417
418                 flags = of_get_property(np, "fsl5200-clocking", NULL);
419                 if (flags)
420                         i2c_data.device_flags |= FSL_I2C_DEV_CLOCK_5200;
421
422                 ret =
423                     platform_device_add_data(i2c_dev, &i2c_data,
424                                              sizeof(struct
425                                                     fsl_i2c_platform_data));
426                 if (ret)
427                         goto unreg;
428
429                 of_register_i2c_devices(np, i);
430         }
431
432         return 0;
433
434 unreg:
435         platform_device_unregister(i2c_dev);
436 err:
437         return ret;
438 }
439
440 arch_initcall(fsl_i2c_of_init);
441 #endif
442
443 #ifdef CONFIG_PPC_83xx
444 static int __init mpc83xx_wdt_init(void)
445 {
446         struct resource r;
447         struct device_node *soc, *np;
448         struct platform_device *dev;
449         const unsigned int *freq;
450         int ret;
451
452         np = of_find_compatible_node(NULL, "watchdog", "mpc83xx_wdt");
453
454         if (!np) {
455                 ret = -ENODEV;
456                 goto nodev;
457         }
458
459         soc = of_find_node_by_type(NULL, "soc");
460
461         if (!soc) {
462                 ret = -ENODEV;
463                 goto nosoc;
464         }
465
466         freq = of_get_property(soc, "bus-frequency", NULL);
467         if (!freq) {
468                 ret = -ENODEV;
469                 goto err;
470         }
471
472         memset(&r, 0, sizeof(r));
473
474         ret = of_address_to_resource(np, 0, &r);
475         if (ret)
476                 goto err;
477
478         dev = platform_device_register_simple("mpc83xx_wdt", 0, &r, 1);
479         if (IS_ERR(dev)) {
480                 ret = PTR_ERR(dev);
481                 goto err;
482         }
483
484         ret = platform_device_add_data(dev, freq, sizeof(int));
485         if (ret)
486                 goto unreg;
487
488         of_node_put(soc);
489         of_node_put(np);
490
491         return 0;
492
493 unreg:
494         platform_device_unregister(dev);
495 err:
496         of_node_put(soc);
497 nosoc:
498         of_node_put(np);
499 nodev:
500         return ret;
501 }
502
503 arch_initcall(mpc83xx_wdt_init);
504 #endif
505
506 static enum fsl_usb2_phy_modes determine_usb_phy(const char *phy_type)
507 {
508         if (!phy_type)
509                 return FSL_USB2_PHY_NONE;
510         if (!strcasecmp(phy_type, "ulpi"))
511                 return FSL_USB2_PHY_ULPI;
512         if (!strcasecmp(phy_type, "utmi"))
513                 return FSL_USB2_PHY_UTMI;
514         if (!strcasecmp(phy_type, "utmi_wide"))
515                 return FSL_USB2_PHY_UTMI_WIDE;
516         if (!strcasecmp(phy_type, "serial"))
517                 return FSL_USB2_PHY_SERIAL;
518
519         return FSL_USB2_PHY_NONE;
520 }
521
522 static int __init fsl_usb_of_init(void)
523 {
524         struct device_node *np;
525         unsigned int i;
526         struct platform_device *usb_dev_mph = NULL, *usb_dev_dr_host = NULL,
527                 *usb_dev_dr_client = NULL;
528         int ret;
529
530         for (np = NULL, i = 0;
531              (np = of_find_compatible_node(np, "usb", "fsl-usb2-mph")) != NULL;
532              i++) {
533                 struct resource r[2];
534                 struct fsl_usb2_platform_data usb_data;
535                 const unsigned char *prop = NULL;
536
537                 memset(&r, 0, sizeof(r));
538                 memset(&usb_data, 0, sizeof(usb_data));
539
540                 ret = of_address_to_resource(np, 0, &r[0]);
541                 if (ret)
542                         goto err;
543
544                 of_irq_to_resource(np, 0, &r[1]);
545
546                 usb_dev_mph =
547                     platform_device_register_simple("fsl-ehci", i, r, 2);
548                 if (IS_ERR(usb_dev_mph)) {
549                         ret = PTR_ERR(usb_dev_mph);
550                         goto err;
551                 }
552
553                 usb_dev_mph->dev.coherent_dma_mask = 0xffffffffUL;
554                 usb_dev_mph->dev.dma_mask = &usb_dev_mph->dev.coherent_dma_mask;
555
556                 usb_data.operating_mode = FSL_USB2_MPH_HOST;
557
558                 prop = of_get_property(np, "port0", NULL);
559                 if (prop)
560                         usb_data.port_enables |= FSL_USB2_PORT0_ENABLED;
561
562                 prop = of_get_property(np, "port1", NULL);
563                 if (prop)
564                         usb_data.port_enables |= FSL_USB2_PORT1_ENABLED;
565
566                 prop = of_get_property(np, "phy_type", NULL);
567                 usb_data.phy_mode = determine_usb_phy(prop);
568
569                 ret =
570                     platform_device_add_data(usb_dev_mph, &usb_data,
571                                              sizeof(struct
572                                                     fsl_usb2_platform_data));
573                 if (ret)
574                         goto unreg_mph;
575         }
576
577         for (np = NULL;
578              (np = of_find_compatible_node(np, "usb", "fsl-usb2-dr")) != NULL;
579              i++) {
580                 struct resource r[2];
581                 struct fsl_usb2_platform_data usb_data;
582                 const unsigned char *prop = NULL;
583
584                 memset(&r, 0, sizeof(r));
585                 memset(&usb_data, 0, sizeof(usb_data));
586
587                 ret = of_address_to_resource(np, 0, &r[0]);
588                 if (ret)
589                         goto unreg_mph;
590
591                 of_irq_to_resource(np, 0, &r[1]);
592
593                 prop = of_get_property(np, "dr_mode", NULL);
594
595                 if (!prop || !strcmp(prop, "host")) {
596                         usb_data.operating_mode = FSL_USB2_DR_HOST;
597                         usb_dev_dr_host = platform_device_register_simple(
598                                         "fsl-ehci", i, r, 2);
599                         if (IS_ERR(usb_dev_dr_host)) {
600                                 ret = PTR_ERR(usb_dev_dr_host);
601                                 goto err;
602                         }
603                 } else if (prop && !strcmp(prop, "peripheral")) {
604                         usb_data.operating_mode = FSL_USB2_DR_DEVICE;
605                         usb_dev_dr_client = platform_device_register_simple(
606                                         "fsl-usb2-udc", i, r, 2);
607                         if (IS_ERR(usb_dev_dr_client)) {
608                                 ret = PTR_ERR(usb_dev_dr_client);
609                                 goto err;
610                         }
611                 } else if (prop && !strcmp(prop, "otg")) {
612                         usb_data.operating_mode = FSL_USB2_DR_OTG;
613                         usb_dev_dr_host = platform_device_register_simple(
614                                         "fsl-ehci", i, r, 2);
615                         if (IS_ERR(usb_dev_dr_host)) {
616                                 ret = PTR_ERR(usb_dev_dr_host);
617                                 goto err;
618                         }
619                         usb_dev_dr_client = platform_device_register_simple(
620                                         "fsl-usb2-udc", i, r, 2);
621                         if (IS_ERR(usb_dev_dr_client)) {
622                                 ret = PTR_ERR(usb_dev_dr_client);
623                                 goto err;
624                         }
625                 } else {
626                         ret = -EINVAL;
627                         goto err;
628                 }
629
630                 prop = of_get_property(np, "phy_type", NULL);
631                 usb_data.phy_mode = determine_usb_phy(prop);
632
633                 if (usb_dev_dr_host) {
634                         usb_dev_dr_host->dev.coherent_dma_mask = 0xffffffffUL;
635                         usb_dev_dr_host->dev.dma_mask = &usb_dev_dr_host->
636                                 dev.coherent_dma_mask;
637                         if ((ret = platform_device_add_data(usb_dev_dr_host,
638                                                 &usb_data, sizeof(struct
639                                                 fsl_usb2_platform_data))))
640                                 goto unreg_dr;
641                 }
642                 if (usb_dev_dr_client) {
643                         usb_dev_dr_client->dev.coherent_dma_mask = 0xffffffffUL;
644                         usb_dev_dr_client->dev.dma_mask = &usb_dev_dr_client->
645                                 dev.coherent_dma_mask;
646                         if ((ret = platform_device_add_data(usb_dev_dr_client,
647                                                 &usb_data, sizeof(struct
648                                                 fsl_usb2_platform_data))))
649                                 goto unreg_dr;
650                 }
651         }
652         return 0;
653
654 unreg_dr:
655         if (usb_dev_dr_host)
656                 platform_device_unregister(usb_dev_dr_host);
657         if (usb_dev_dr_client)
658                 platform_device_unregister(usb_dev_dr_client);
659 unreg_mph:
660         if (usb_dev_mph)
661                 platform_device_unregister(usb_dev_mph);
662 err:
663         return ret;
664 }
665
666 arch_initcall(fsl_usb_of_init);
667
668 #ifdef CONFIG_CPM2
669
670 extern void init_scc_ioports(struct fs_uart_platform_info*);
671
672 static const char fcc_regs[] = "fcc_regs";
673 static const char fcc_regs_c[] = "fcc_regs_c";
674 static const char fcc_pram[] = "fcc_pram";
675 static char bus_id[9][BUS_ID_SIZE];
676
677 static int __init fs_enet_of_init(void)
678 {
679         struct device_node *np;
680         unsigned int i;
681         struct platform_device *fs_enet_dev;
682         struct resource res;
683         int ret;
684
685         for (np = NULL, i = 0;
686              (np = of_find_compatible_node(np, "network", "fs_enet")) != NULL;
687              i++) {
688                 struct resource r[4];
689                 struct device_node *phy, *mdio;
690                 struct fs_platform_info fs_enet_data;
691                 const unsigned int *id, *phy_addr, *phy_irq;
692                 const void *mac_addr;
693                 const phandle *ph;
694                 const char *model;
695
696                 memset(r, 0, sizeof(r));
697                 memset(&fs_enet_data, 0, sizeof(fs_enet_data));
698
699                 ret = of_address_to_resource(np, 0, &r[0]);
700                 if (ret)
701                         goto err;
702                 r[0].name = fcc_regs;
703
704                 ret = of_address_to_resource(np, 1, &r[1]);
705                 if (ret)
706                         goto err;
707                 r[1].name = fcc_pram;
708
709                 ret = of_address_to_resource(np, 2, &r[2]);
710                 if (ret)
711                         goto err;
712                 r[2].name = fcc_regs_c;
713                 fs_enet_data.fcc_regs_c = r[2].start;
714
715                 of_irq_to_resource(np, 0, &r[3]);
716
717                 fs_enet_dev =
718                     platform_device_register_simple("fsl-cpm-fcc", i, &r[0], 4);
719
720                 if (IS_ERR(fs_enet_dev)) {
721                         ret = PTR_ERR(fs_enet_dev);
722                         goto err;
723                 }
724
725                 model = of_get_property(np, "model", NULL);
726                 if (model == NULL) {
727                         ret = -ENODEV;
728                         goto unreg;
729                 }
730
731                 mac_addr = of_get_mac_address(np);
732                 if (mac_addr)
733                         memcpy(fs_enet_data.macaddr, mac_addr, 6);
734
735                 ph = of_get_property(np, "phy-handle", NULL);
736                 phy = of_find_node_by_phandle(*ph);
737
738                 if (phy == NULL) {
739                         ret = -ENODEV;
740                         goto unreg;
741                 }
742
743                 phy_addr = of_get_property(phy, "reg", NULL);
744                 fs_enet_data.phy_addr = *phy_addr;
745
746                 phy_irq = of_get_property(phy, "interrupts", NULL);
747
748                 id = of_get_property(np, "device-id", NULL);
749                 fs_enet_data.fs_no = *id;
750                 strcpy(fs_enet_data.fs_type, model);
751
752                 mdio = of_get_parent(phy);
753                 ret = of_address_to_resource(mdio, 0, &res);
754                 if (ret) {
755                         of_node_put(phy);
756                         of_node_put(mdio);
757                         goto unreg;
758                 }
759
760                 fs_enet_data.clk_rx = *((u32 *)of_get_property(np,
761                                                 "rx-clock", NULL));
762                 fs_enet_data.clk_tx = *((u32 *)of_get_property(np,
763                                                 "tx-clock", NULL));
764
765                 if (strstr(model, "FCC")) {
766                         int fcc_index = *id - 1;
767                         const unsigned char *mdio_bb_prop;
768
769                         fs_enet_data.dpram_offset = (u32)cpm_dpram_addr(0);
770                         fs_enet_data.rx_ring = 32;
771                         fs_enet_data.tx_ring = 32;
772                         fs_enet_data.rx_copybreak = 240;
773                         fs_enet_data.use_napi = 0;
774                         fs_enet_data.napi_weight = 17;
775                         fs_enet_data.mem_offset = FCC_MEM_OFFSET(fcc_index);
776                         fs_enet_data.cp_page = CPM_CR_FCC_PAGE(fcc_index);
777                         fs_enet_data.cp_block = CPM_CR_FCC_SBLOCK(fcc_index);
778
779                         snprintf((char*)&bus_id[(*id)], BUS_ID_SIZE, "%x:%02x",
780                                                         (u32)res.start, fs_enet_data.phy_addr);
781                         fs_enet_data.bus_id = (char*)&bus_id[(*id)];
782                         fs_enet_data.init_ioports = init_fcc_ioports;
783
784                         mdio_bb_prop = of_get_property(phy, "bitbang", NULL);
785                         if (mdio_bb_prop) {
786                                 struct platform_device *fs_enet_mdio_bb_dev;
787                                 struct fs_mii_bb_platform_info fs_enet_mdio_bb_data;
788
789                                 fs_enet_mdio_bb_dev =
790                                         platform_device_register_simple("fsl-bb-mdio",
791                                                         i, NULL, 0);
792                                 memset(&fs_enet_mdio_bb_data, 0,
793                                                 sizeof(struct fs_mii_bb_platform_info));
794                                 fs_enet_mdio_bb_data.mdio_dat.bit =
795                                         mdio_bb_prop[0];
796                                 fs_enet_mdio_bb_data.mdio_dir.bit =
797                                         mdio_bb_prop[1];
798                                 fs_enet_mdio_bb_data.mdc_dat.bit =
799                                         mdio_bb_prop[2];
800                                 fs_enet_mdio_bb_data.mdio_port =
801                                         mdio_bb_prop[3];
802                                 fs_enet_mdio_bb_data.mdc_port =
803                                         mdio_bb_prop[4];
804                                 fs_enet_mdio_bb_data.delay =
805                                         mdio_bb_prop[5];
806
807                                 fs_enet_mdio_bb_data.irq[0] = phy_irq[0];
808                                 fs_enet_mdio_bb_data.irq[1] = -1;
809                                 fs_enet_mdio_bb_data.irq[2] = -1;
810                                 fs_enet_mdio_bb_data.irq[3] = phy_irq[0];
811                                 fs_enet_mdio_bb_data.irq[31] = -1;
812
813                                 fs_enet_mdio_bb_data.mdio_dat.offset =
814                                         (u32)&cpm2_immr->im_ioport.iop_pdatc;
815                                 fs_enet_mdio_bb_data.mdio_dir.offset =
816                                         (u32)&cpm2_immr->im_ioport.iop_pdirc;
817                                 fs_enet_mdio_bb_data.mdc_dat.offset =
818                                         (u32)&cpm2_immr->im_ioport.iop_pdatc;
819
820                                 ret = platform_device_add_data(
821                                                 fs_enet_mdio_bb_dev,
822                                                 &fs_enet_mdio_bb_data,
823                                                 sizeof(struct fs_mii_bb_platform_info));
824                                 if (ret)
825                                         goto unreg;
826                         }
827
828                         of_node_put(phy);
829                         of_node_put(mdio);
830
831                         ret = platform_device_add_data(fs_enet_dev, &fs_enet_data,
832                                                      sizeof(struct
833                                                             fs_platform_info));
834                         if (ret)
835                                 goto unreg;
836                 }
837         }
838         return 0;
839
840 unreg:
841         platform_device_unregister(fs_enet_dev);
842 err:
843         return ret;
844 }
845
846 arch_initcall(fs_enet_of_init);
847
848 static const char scc_regs[] = "regs";
849 static const char scc_pram[] = "pram";
850
851 static int __init cpm_uart_of_init(void)
852 {
853         struct device_node *np;
854         unsigned int i;
855         struct platform_device *cpm_uart_dev;
856         int ret;
857
858         for (np = NULL, i = 0;
859              (np = of_find_compatible_node(np, "serial", "cpm_uart")) != NULL;
860              i++) {
861                 struct resource r[3];
862                 struct fs_uart_platform_info cpm_uart_data;
863                 const int *id;
864                 const char *model;
865
866                 memset(r, 0, sizeof(r));
867                 memset(&cpm_uart_data, 0, sizeof(cpm_uart_data));
868
869                 ret = of_address_to_resource(np, 0, &r[0]);
870                 if (ret)
871                         goto err;
872
873                 r[0].name = scc_regs;
874
875                 ret = of_address_to_resource(np, 1, &r[1]);
876                 if (ret)
877                         goto err;
878                 r[1].name = scc_pram;
879
880                 of_irq_to_resource(np, 0, &r[2]);
881
882                 cpm_uart_dev =
883                     platform_device_register_simple("fsl-cpm-scc:uart", i, &r[0], 3);
884
885                 if (IS_ERR(cpm_uart_dev)) {
886                         ret = PTR_ERR(cpm_uart_dev);
887                         goto err;
888                 }
889
890                 id = of_get_property(np, "device-id", NULL);
891                 cpm_uart_data.fs_no = *id;
892
893                 model = of_get_property(np, "model", NULL);
894                 strcpy(cpm_uart_data.fs_type, model);
895
896                 cpm_uart_data.uart_clk = ppc_proc_freq;
897
898                 cpm_uart_data.tx_num_fifo = 4;
899                 cpm_uart_data.tx_buf_size = 32;
900                 cpm_uart_data.rx_num_fifo = 4;
901                 cpm_uart_data.rx_buf_size = 32;
902                 cpm_uart_data.clk_rx = *((u32 *)of_get_property(np,
903                                                 "rx-clock", NULL));
904                 cpm_uart_data.clk_tx = *((u32 *)of_get_property(np,
905                                                 "tx-clock", NULL));
906
907                 ret =
908                     platform_device_add_data(cpm_uart_dev, &cpm_uart_data,
909                                              sizeof(struct
910                                                     fs_uart_platform_info));
911                 if (ret)
912                         goto unreg;
913         }
914
915         return 0;
916
917 unreg:
918         platform_device_unregister(cpm_uart_dev);
919 err:
920         return ret;
921 }
922
923 arch_initcall(cpm_uart_of_init);
924 #endif /* CONFIG_CPM2 */
925
926 #ifdef CONFIG_8xx
927
928 extern void init_scc_ioports(struct fs_platform_info*);
929 extern int platform_device_skip(const char *model, int id);
930
931 static int __init fs_enet_mdio_of_init(void)
932 {
933         struct device_node *np;
934         unsigned int i;
935         struct platform_device *mdio_dev;
936         struct resource res;
937         int ret;
938
939         for (np = NULL, i = 0;
940              (np = of_find_compatible_node(np, "mdio", "fs_enet")) != NULL;
941              i++) {
942                 struct fs_mii_fec_platform_info mdio_data;
943
944                 memset(&res, 0, sizeof(res));
945                 memset(&mdio_data, 0, sizeof(mdio_data));
946
947                 ret = of_address_to_resource(np, 0, &res);
948                 if (ret)
949                         goto err;
950
951                 mdio_dev =
952                     platform_device_register_simple("fsl-cpm-fec-mdio",
953                                                     res.start, &res, 1);
954                 if (IS_ERR(mdio_dev)) {
955                         ret = PTR_ERR(mdio_dev);
956                         goto err;
957                 }
958
959                 mdio_data.mii_speed = ((((ppc_proc_freq + 4999999) / 2500000) / 2) & 0x3F) << 1;
960
961                 ret =
962                     platform_device_add_data(mdio_dev, &mdio_data,
963                                              sizeof(struct fs_mii_fec_platform_info));
964                 if (ret)
965                         goto unreg;
966         }
967         return 0;
968
969 unreg:
970         platform_device_unregister(mdio_dev);
971 err:
972         return ret;
973 }
974
975 arch_initcall(fs_enet_mdio_of_init);
976
977 static const char *enet_regs = "regs";
978 static const char *enet_pram = "pram";
979 static const char *enet_irq = "interrupt";
980 static char bus_id[9][BUS_ID_SIZE];
981
982 static int __init fs_enet_of_init(void)
983 {
984         struct device_node *np;
985         unsigned int i;
986         struct platform_device *fs_enet_dev = NULL;
987         struct resource res;
988         int ret;
989
990         for (np = NULL, i = 0;
991              (np = of_find_compatible_node(np, "network", "fs_enet")) != NULL;
992              i++) {
993                 struct resource r[4];
994                 struct device_node *phy = NULL, *mdio = NULL;
995                 struct fs_platform_info fs_enet_data;
996                 const unsigned int *id;
997                 const unsigned int *phy_addr;
998                 const void *mac_addr;
999                 const phandle *ph;
1000                 const char *model;
1001
1002                 memset(r, 0, sizeof(r));
1003                 memset(&fs_enet_data, 0, sizeof(fs_enet_data));
1004
1005                 model = of_get_property(np, "model", NULL);
1006                 if (model == NULL) {
1007                         ret = -ENODEV;
1008                         goto unreg;
1009                 }
1010
1011                 id = of_get_property(np, "device-id", NULL);
1012                 fs_enet_data.fs_no = *id;
1013
1014                 if (platform_device_skip(model, *id))
1015                         continue;
1016
1017                 ret = of_address_to_resource(np, 0, &r[0]);
1018                 if (ret)
1019                         goto err;
1020                 r[0].name = enet_regs;
1021
1022                 mac_addr = of_get_mac_address(np);
1023                 if (mac_addr)
1024                         memcpy(fs_enet_data.macaddr, mac_addr, 6);
1025
1026                 ph = of_get_property(np, "phy-handle", NULL);
1027                 if (ph != NULL)
1028                         phy = of_find_node_by_phandle(*ph);
1029
1030                 if (phy != NULL) {
1031                         phy_addr = of_get_property(phy, "reg", NULL);
1032                         fs_enet_data.phy_addr = *phy_addr;
1033                         fs_enet_data.has_phy = 1;
1034
1035                         mdio = of_get_parent(phy);
1036                         ret = of_address_to_resource(mdio, 0, &res);
1037                         if (ret) {
1038                                 of_node_put(phy);
1039                                 of_node_put(mdio);
1040                                 goto unreg;
1041                         }
1042                 }
1043
1044                 model = of_get_property(np, "model", NULL);
1045                 strcpy(fs_enet_data.fs_type, model);
1046
1047                 if (strstr(model, "FEC")) {
1048                         r[1].start = r[1].end = irq_of_parse_and_map(np, 0);
1049                         r[1].flags = IORESOURCE_IRQ;
1050                         r[1].name = enet_irq;
1051
1052                         fs_enet_dev =
1053                                     platform_device_register_simple("fsl-cpm-fec", i, &r[0], 2);
1054
1055                         if (IS_ERR(fs_enet_dev)) {
1056                                 ret = PTR_ERR(fs_enet_dev);
1057                                 goto err;
1058                         }
1059
1060                         fs_enet_data.rx_ring = 128;
1061                         fs_enet_data.tx_ring = 16;
1062                         fs_enet_data.rx_copybreak = 240;
1063                         fs_enet_data.use_napi = 1;
1064                         fs_enet_data.napi_weight = 17;
1065
1066                         snprintf((char*)&bus_id[i], BUS_ID_SIZE, "%x:%02x",
1067                                                         (u32)res.start, fs_enet_data.phy_addr);
1068                         fs_enet_data.bus_id = (char*)&bus_id[i];
1069                         fs_enet_data.init_ioports = init_fec_ioports;
1070                 }
1071                 if (strstr(model, "SCC")) {
1072                         ret = of_address_to_resource(np, 1, &r[1]);
1073                         if (ret)
1074                                 goto err;
1075                         r[1].name = enet_pram;
1076
1077                         r[2].start = r[2].end = irq_of_parse_and_map(np, 0);
1078                         r[2].flags = IORESOURCE_IRQ;
1079                         r[2].name = enet_irq;
1080
1081                         fs_enet_dev =
1082                                     platform_device_register_simple("fsl-cpm-scc", i, &r[0], 3);
1083
1084                         if (IS_ERR(fs_enet_dev)) {
1085                                 ret = PTR_ERR(fs_enet_dev);
1086                                 goto err;
1087                         }
1088
1089                         fs_enet_data.rx_ring = 64;
1090                         fs_enet_data.tx_ring = 8;
1091                         fs_enet_data.rx_copybreak = 240;
1092                         fs_enet_data.use_napi = 1;
1093                         fs_enet_data.napi_weight = 17;
1094
1095                         snprintf((char*)&bus_id[i], BUS_ID_SIZE, "%s", "fixed@10:1");
1096                         fs_enet_data.bus_id = (char*)&bus_id[i];
1097                         fs_enet_data.init_ioports = init_scc_ioports;
1098                 }
1099
1100                 of_node_put(phy);
1101                 of_node_put(mdio);
1102
1103                 ret = platform_device_add_data(fs_enet_dev, &fs_enet_data,
1104                                              sizeof(struct
1105                                                     fs_platform_info));
1106                 if (ret)
1107                         goto unreg;
1108         }
1109         return 0;
1110
1111 unreg:
1112         platform_device_unregister(fs_enet_dev);
1113 err:
1114         return ret;
1115 }
1116
1117 arch_initcall(fs_enet_of_init);
1118
1119 static int __init fsl_pcmcia_of_init(void)
1120 {
1121         struct device_node *np = NULL;
1122         /*
1123          * Register all the devices which type is "pcmcia"
1124          */
1125         while ((np = of_find_compatible_node(np,
1126                         "pcmcia", "fsl,pq-pcmcia")) != NULL)
1127                             of_platform_device_create(np, "m8xx-pcmcia", NULL);
1128         return 0;
1129 }
1130
1131 arch_initcall(fsl_pcmcia_of_init);
1132
1133 static const char *smc_regs = "regs";
1134 static const char *smc_pram = "pram";
1135
1136 static int __init cpm_smc_uart_of_init(void)
1137 {
1138         struct device_node *np;
1139         unsigned int i;
1140         struct platform_device *cpm_uart_dev;
1141         int ret;
1142
1143         for (np = NULL, i = 0;
1144              (np = of_find_compatible_node(np, "serial", "cpm_uart")) != NULL;
1145              i++) {
1146                 struct resource r[3];
1147                 struct fs_uart_platform_info cpm_uart_data;
1148                 const int *id;
1149                 const char *model;
1150
1151                 memset(r, 0, sizeof(r));
1152                 memset(&cpm_uart_data, 0, sizeof(cpm_uart_data));
1153
1154                 ret = of_address_to_resource(np, 0, &r[0]);
1155                 if (ret)
1156                         goto err;
1157
1158                 r[0].name = smc_regs;
1159
1160                 ret = of_address_to_resource(np, 1, &r[1]);
1161                 if (ret)
1162                         goto err;
1163                 r[1].name = smc_pram;
1164
1165                 r[2].start = r[2].end = irq_of_parse_and_map(np, 0);
1166                 r[2].flags = IORESOURCE_IRQ;
1167
1168                 cpm_uart_dev =
1169                     platform_device_register_simple("fsl-cpm-smc:uart", i, &r[0], 3);
1170
1171                 if (IS_ERR(cpm_uart_dev)) {
1172                         ret = PTR_ERR(cpm_uart_dev);
1173                         goto err;
1174                 }
1175
1176                 model = of_get_property(np, "model", NULL);
1177                 strcpy(cpm_uart_data.fs_type, model);
1178
1179                 id = of_get_property(np, "device-id", NULL);
1180                 cpm_uart_data.fs_no = *id;
1181                 cpm_uart_data.uart_clk = ppc_proc_freq;
1182
1183                 cpm_uart_data.tx_num_fifo = 4;
1184                 cpm_uart_data.tx_buf_size = 32;
1185                 cpm_uart_data.rx_num_fifo = 4;
1186                 cpm_uart_data.rx_buf_size = 32;
1187
1188                 ret =
1189                     platform_device_add_data(cpm_uart_dev, &cpm_uart_data,
1190                                              sizeof(struct
1191                                                     fs_uart_platform_info));
1192                 if (ret)
1193                         goto unreg;
1194         }
1195
1196         return 0;
1197
1198 unreg:
1199         platform_device_unregister(cpm_uart_dev);
1200 err:
1201         return ret;
1202 }
1203
1204 arch_initcall(cpm_smc_uart_of_init);
1205
1206 #endif /* CONFIG_8xx */
1207
1208 int __init fsl_spi_init(struct spi_board_info *board_infos,
1209                         unsigned int num_board_infos,
1210                         void (*activate_cs)(u8 cs, u8 polarity),
1211                         void (*deactivate_cs)(u8 cs, u8 polarity))
1212 {
1213         struct device_node *np;
1214         unsigned int i;
1215         const u32 *sysclk;
1216
1217         np = of_find_node_by_type(NULL, "qe");
1218         if (!np)
1219                 return -ENODEV;
1220
1221         sysclk = of_get_property(np, "bus-frequency", NULL);
1222         if (!sysclk)
1223                 return -ENODEV;
1224
1225         for (np = NULL, i = 1;
1226              (np = of_find_compatible_node(np, "spi", "fsl_spi")) != NULL;
1227              i++) {
1228                 int ret = 0;
1229                 unsigned int j;
1230                 const void *prop;
1231                 struct resource res[2];
1232                 struct platform_device *pdev;
1233                 struct fsl_spi_platform_data pdata = {
1234                         .activate_cs = activate_cs,
1235                         .deactivate_cs = deactivate_cs,
1236                 };
1237
1238                 memset(res, 0, sizeof(res));
1239
1240                 pdata.sysclk = *sysclk;
1241
1242                 prop = of_get_property(np, "reg", NULL);
1243                 if (!prop)
1244                         goto err;
1245                 pdata.bus_num = *(u32 *)prop;
1246
1247                 prop = of_get_property(np, "mode", NULL);
1248                 if (prop && !strcmp(prop, "cpu-qe"))
1249                         pdata.qe_mode = 1;
1250
1251                 for (j = 0; j < num_board_infos; j++) {
1252                         if (board_infos[j].bus_num == pdata.bus_num)
1253                                 pdata.max_chipselect++;
1254                 }
1255
1256                 if (!pdata.max_chipselect)
1257                         goto err;
1258
1259                 ret = of_address_to_resource(np, 0, &res[0]);
1260                 if (ret)
1261                         goto err;
1262
1263                 ret = of_irq_to_resource(np, 0, &res[1]);
1264                 if (ret == NO_IRQ)
1265                         goto err;
1266
1267                 pdev = platform_device_alloc("mpc83xx_spi", i);
1268                 if (!pdev)
1269                         goto err;
1270
1271                 ret = platform_device_add_data(pdev, &pdata, sizeof(pdata));
1272                 if (ret)
1273                         goto unreg;
1274
1275                 ret = platform_device_add_resources(pdev, res,
1276                                                     ARRAY_SIZE(res));
1277                 if (ret)
1278                         goto unreg;
1279
1280                 ret = platform_device_register(pdev);
1281                 if (ret)
1282                         goto unreg;
1283
1284                 continue;
1285 unreg:
1286                 platform_device_del(pdev);
1287 err:
1288                 continue;
1289         }
1290
1291         return spi_register_board_info(board_infos, num_board_infos);
1292 }