[lantiq] prepare Makefile for 3.6
[openwrt.git] / target / linux / lantiq / files / arch / mips / lantiq / falcon / addon-easy98000.c
1 /*
2  *  EASY98000 CPLD Addon driver
3  *
4  *  Copyright (C) 2011 Thomas Langer <thomas.langer@lantiq.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2  as published
8  *  by the Free Software Foundation.
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/version.h>
15 #include <linux/types.h>
16 #include <linux/init.h>
17 #include <linux/platform_device.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22
23 struct easy98000_reg_cpld {
24         u16 cmdreg1;            /* 0x1 */
25         u16 cmdreg0;            /* 0x0 */
26         u16 idreg0;             /* 0x3 */
27         u16 resreg;             /* 0x2 */
28         u16 intreg;             /* 0x5 */
29         u16 idreg1;             /* 0x4 */
30         u16 ledreg;             /* 0x7 */
31         u16 pcmconconfig;       /* 0x6 */
32         u16 res0;               /* 0x9 */
33         u16 ethledreg;          /* 0x8 */
34         u16 res1[4];            /* 0xa-0xd */
35         u16 cpld1v;             /* 0xf */
36         u16 cpld2v;             /* 0xe */
37 };
38 static struct easy98000_reg_cpld * const cpld =
39         (struct easy98000_reg_cpld *)(KSEG1 | 0x17c00000);
40 #define cpld_r8(reg) (__raw_readw(&cpld->reg) & 0xFF)
41 #define cpld_w8(val, reg) __raw_writew((val) & 0xFF, &cpld->reg)
42
43 int easy98000_addon_has_dm9000(void)
44 {
45         if ((cpld_r8(idreg0) & 0xF) == 1)
46                 return 1;
47         return 0;
48 }
49
50 #if defined(CONFIG_PROC_FS)
51 typedef void (*cpld_dump) (struct seq_file *s);
52 struct proc_entry {
53         char *name;
54         void *callback;
55 };
56
57 static int cpld_proc_show ( struct seq_file *s, void *p )
58 {
59         cpld_dump dump = s->private;
60
61         if ( dump != NULL )
62                 dump(s);
63
64         return 0;
65 }
66
67 static int cpld_proc_open ( struct inode *inode, struct file *file )
68 {
69         return single_open ( file, cpld_proc_show, PDE(inode)->data );
70 }
71
72 static void cpld_versions_get ( struct seq_file *s )
73 {
74         seq_printf(s, "CPLD1: V%d\n", cpld_r8(cpld1v));
75         seq_printf(s, "CPLD2: V%d\n", cpld_r8(cpld2v));
76 }
77
78 static void cpld_ebu_module_get ( struct seq_file *s )
79 {
80         u8 addon_id;
81
82         addon_id = cpld_r8(idreg0) & 0xF;
83         switch (addon_id) {
84         case 0xF: /* nothing connected */
85                 break;
86         case 1:
87                 seq_printf(s, "Ethernet Controller module (dm9000)\n");
88                 break;
89         default:
90                 seq_printf(s, "Unknown EBU module (EBU_ID=0x%02X)\n", addon_id);
91                 break;
92         }
93 }
94
95 static void cpld_xmii_module_get ( struct seq_file *s )
96 {
97         u8 addon_id;
98         char *mod = NULL;
99
100         addon_id = cpld_r8(idreg1) & 0xF;
101         switch (addon_id) {
102         case 0xF:
103                 mod = "no module";
104                 break;
105         case 0x1:
106                 mod = "RGMII module";
107                 break;
108         case 0x4:
109                 mod = "GMII MAC Mode (XWAY TANTOS-3G)";
110                 break;
111         case 0x6:
112                 mod = "TMII MAC Mode (XWAY TANTOS-3G)";
113                 break;
114         case 0x8:
115                 mod = "GMII PHY module";
116                 break;
117         case 0x9:
118                 mod = "MII PHY module";
119                 break;
120         case 0xA:
121                 mod = "RMII PHY module";
122                 break;
123         default:
124                 break;
125         }
126         if (mod)
127                 seq_printf(s, "%s\n", mod);
128         else
129                 seq_printf(s, "unknown xMII module (xMII_ID=0x%02X)\n", addon_id);
130 }
131
132 static struct proc_entry proc_entries[] = {
133         {"versions",    cpld_versions_get},
134         {"ebu",         cpld_ebu_module_get},
135         {"xmii",        cpld_xmii_module_get},
136 };
137
138 static struct file_operations ops = {
139         .owner   = THIS_MODULE,
140         .open    = cpld_proc_open,
141         .read    = seq_read,
142         .llseek  = seq_lseek,
143         .release = single_release,
144 };
145
146 static void cpld_proc_entry_create(struct proc_dir_entry *parent_node,
147                                    struct proc_entry *proc_entry)
148 {
149         proc_create_data ( proc_entry->name, (S_IFREG | S_IRUGO), parent_node,
150                            &ops, proc_entry->callback);
151 }
152
153 static int cpld_proc_install(void)
154 {
155         struct proc_dir_entry *driver_proc_node;
156
157         driver_proc_node = proc_mkdir("cpld", NULL);
158         if (driver_proc_node != NULL) {
159                 int i;
160                 for (i = 0; i < ARRAY_SIZE(proc_entries); i++)
161                         cpld_proc_entry_create(driver_proc_node,
162                                               &proc_entries[i]);
163         } else {
164                 printk("cannot create proc entry");
165                 return -1;
166         }
167         return 0;
168 }
169 #else
170 static inline int cpld_proc_install(void) {}
171 #endif
172
173 static int easy98000_addon_probe(struct platform_device *pdev)
174 {
175         return cpld_proc_install();
176 }
177
178 static int easy98000_addon_remove(struct platform_device *pdev)
179 {
180 #if defined(CONFIG_PROC_FS)
181         char buf[64];
182         int i;
183
184         for (i = 0; i < sizeof(proc_entries) / sizeof(proc_entries[0]); i++) {
185                 sprintf(buf, "cpld/%s", proc_entries[i].name);
186                 remove_proc_entry(buf, 0);
187         }
188         remove_proc_entry("cpld", 0);
189 #endif
190         return 0;
191 }
192
193 static struct platform_driver easy98000_addon_driver = {
194         .probe = easy98000_addon_probe,
195         .remove = __devexit_p(easy98000_addon_remove),
196         .driver = {
197                 .name = "easy98000_addon",
198                 .owner = THIS_MODULE,
199         },
200 };
201
202 int __init easy98000_addon_init(void)
203 {
204         return platform_driver_register(&easy98000_addon_driver);
205 }
206
207 void __exit easy98000_addon_exit(void)
208 {
209         platform_driver_unregister(&easy98000_addon_driver);
210 }
211
212 module_init(easy98000_addon_init);
213 module_exit(easy98000_addon_exit);