d90efb21e8690568aa25ff07a81bebe2e5b1c7cc
[openwrt/openwrt.git] / package / spi-ks8995 / src / spi_ks8995.c
1 /*
2 * SPI driver for Micrel/Kendin KS8995M ethernet switch
3 *
4 * Copyright (C) 2008 Gabor Juhos <juhosg at opwnert.org>
5 *
6 * This file was based on: drivers/spi/at25.c
7 * Copyright (C) 2006 David Brownell
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21
22 #include <linux/spi/spi.h>
23
24 #define DRV_VERSION "0.1.0"
25 #define DRV_DESC "Micrel/Kendin KS8995 Ethernet switch driver"
26
27 /*-------------------------------------------------------------------------*/
28
29 #define KS8995_REG_ID0 0x00 /* Chip ID0 */
30 #define KS8995_REG_ID1 0x01 /* Chip ID1 */
31
32 #define KS8995_REG_GC0 0x02 /* Global Control 0 */
33 #define KS8995_REG_GC1 0x03 /* Global Control 1 */
34 #define KS8995_REG_GC2 0x04 /* Global Control 2 */
35 #define KS8995_REG_GC3 0x05 /* Global Control 3 */
36 #define KS8995_REG_GC4 0x06 /* Global Control 4 */
37 #define KS8995_REG_GC5 0x07 /* Global Control 5 */
38 #define KS8995_REG_GC6 0x08 /* Global Control 6 */
39 #define KS8995_REG_GC7 0x09 /* Global Control 7 */
40 #define KS8995_REG_GC8 0x0a /* Global Control 8 */
41 #define KS8995_REG_GC9 0x0b /* Global Control 9 */
42
43 #define KS8995_REG_PC(p,r) ((0x10 * p) + r) /* Port Control */
44 #define KS8995_REG_PS(p,r) ((0x10 * p) + r + 0xe) /* Port Status */
45
46 #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */
47 #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */
48 #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */
49 #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */
50 #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */
51 #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */
52 #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */
53 #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */
54
55 #define KS8995_REG_MAC0 0x68 /* MAC address 0 */
56 #define KS8995_REG_MAC1 0x69 /* MAC address 1 */
57 #define KS8995_REG_MAC2 0x6a /* MAC address 2 */
58 #define KS8995_REG_MAC3 0x6b /* MAC address 3 */
59 #define KS8995_REG_MAC4 0x6c /* MAC address 4 */
60 #define KS8995_REG_MAC5 0x6d /* MAC address 5 */
61
62 #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */
63 #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */
64
65 #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */
66 #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */
67 #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */
68 #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */
69 #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */
70 #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */
71 #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */
72 #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */
73
74 #define KS8995_REGS_SIZE 0x80
75
76 #define ID1_CHIPID_M 0xf
77 #define ID1_CHIPID_S 4
78 #define ID1_REVISION_M 0x7
79 #define ID1_REVISION_S 1
80 #define ID1_START_SW 1 /* start the switch */
81
82 #define FAMILY_KS8995 0x95
83 #define CHIPID_M 0
84
85 #define KS8995_CMD_WRITE 0x02U
86 #define KS8995_CMD_READ 0x03U
87
88 #define KS8995_RESET_DELAY 10 /* usec */
89
90 /*-------------------------------------------------------------------------*/
91
92 struct ks8995_pdata {
93 /* not yet implemented */
94 };
95
96 struct ks8995_switch {
97 struct spi_device *spi;
98 struct mutex lock;
99 struct ks8995_pdata *pdata;
100 };
101
102 /*-------------------------------------------------------------------------*/
103
104 static inline u8 get_chip_id(u8 val)
105 {
106 return ((val >> ID1_CHIPID_S) & ID1_CHIPID_M);
107 }
108
109 static inline u8 get_chip_rev(u8 val)
110 {
111 return ((val >> ID1_REVISION_S) & ID1_REVISION_M);
112 }
113
114 /*-------------------------------------------------------------------------*/
115
116 static int ks8995_read(struct ks8995_switch *ks, char *buf,
117 unsigned offset, size_t count)
118 {
119 u8 cmd[2];
120 struct spi_transfer t[2];
121 struct spi_message m;
122 int err;
123
124 spi_message_init(&m);
125
126 memset(&t, 0, sizeof(t));
127
128 t[0].tx_buf = cmd;
129 t[0].len = sizeof(cmd);
130 spi_message_add_tail(&t[0], &m);
131
132 t[1].rx_buf = buf;
133 t[1].len = count;
134 spi_message_add_tail(&t[1], &m);
135
136 cmd[0] = KS8995_CMD_READ;
137 cmd[1] = offset;
138
139 mutex_lock(&ks->lock);
140 err = spi_sync(ks->spi, &m);
141 mutex_unlock(&ks->lock);
142
143 return err ? err : count;
144 }
145
146
147 static int ks8995_write(struct ks8995_switch *ks, char *buf,
148 unsigned offset, size_t count)
149 {
150 u8 cmd[2];
151 struct spi_transfer t[2];
152 struct spi_message m;
153 int err;
154
155 spi_message_init(&m);
156
157 memset(&t, 0, sizeof(t));
158
159 t[0].tx_buf = cmd;
160 t[0].len = sizeof(cmd);
161 spi_message_add_tail(&t[0], &m);
162
163 t[1].tx_buf = buf;
164 t[1].len = count;
165 spi_message_add_tail(&t[1], &m);
166
167 cmd[0] = KS8995_CMD_WRITE;
168 cmd[1] = offset;
169
170 mutex_lock(&ks->lock);
171 err = spi_sync(ks->spi, &m);
172 mutex_unlock(&ks->lock);
173
174 return err ? err : count;
175 }
176
177 static int ks8995_read_reg(struct ks8995_switch *ks, u8 addr, u8 *buf)
178 {
179 return (ks8995_read(ks, buf, addr, 1) != 1);
180 }
181
182 static int ks8995_write_reg(struct ks8995_switch *ks, u8 addr, u8 val)
183 {
184 char buf = val;
185
186 return (ks8995_write(ks, &buf, addr, 1) != 1);
187 }
188
189 /*-------------------------------------------------------------------------*/
190
191 #if 0
192 static int ks8995_setup(struct spi_device *spi)
193 {
194 struct ks8995_switch *ks;
195 u8 t;
196
197 ks = dev_get_drvdata(&spi->dev);
198
199 ks8995_write_reg(ks, KS8995_REG_GC0, 0x4c);
200 ks8995_write_reg(ks, KS8995_REG_GC1, 0x05);
201
202 ks8995_read_reg(ks, KS8995_REG_GC2, &t);
203 ks8995_write_reg(ks, KS8995_REG_GC2, t | 1);
204
205 ks8995_write_reg(ks, KS8995_REG_GC4, 0x20);
206
207 ks8995_write_reg(ks, KS8995_REG_PC(1,0), 0x61);
208 ks8995_write_reg(ks, KS8995_REG_PC(2,0), 0x61);
209 ks8995_write_reg(ks, KS8995_REG_PC(3,0), 0x61);
210 ks8995_write_reg(ks, KS8995_REG_PC(4,0), 0x61);
211 ks8995_write_reg(ks, KS8995_REG_PC(5,0), 0x61);
212
213 ks8995_write_reg(ks, KS8995_REG_PC(5,11), 0x18);
214
215 ks8995_write_reg(ks, KS8995_REG_TPC0, 0xff);
216 ks8995_write_reg(ks, KS8995_REG_TPC1, 0xff);
217 ks8995_write_reg(ks, KS8995_REG_TPC2, 0xff);
218 ks8995_write_reg(ks, KS8995_REG_TPC3, 0xff);
219 ks8995_write_reg(ks, KS8995_REG_TPC4, 0xff);
220 ks8995_write_reg(ks, KS8995_REG_TPC5, 0xff);
221 ks8995_write_reg(ks, KS8995_REG_TPC6, 0xff);
222 ks8995_write_reg(ks, KS8995_REG_TPC7, 0xfe);
223
224 return 0;
225 }
226 #endif
227
228 static int ks8995_stop(struct ks8995_switch *ks)
229 {
230 return ks8995_write_reg(ks, KS8995_REG_ID1, 0);
231 }
232
233 static int ks8995_start(struct ks8995_switch *ks)
234 {
235 return ks8995_write_reg(ks, KS8995_REG_ID1, 1);
236 }
237
238 static int ks8995_reset(struct ks8995_switch *ks)
239 {
240 int err;
241
242 err = ks8995_stop(ks);
243 if (err)
244 return err;
245
246 udelay(KS8995_RESET_DELAY);
247
248 return ks8995_start(ks);
249 }
250
251 /*-------------------------------------------------------------------------*/
252
253 static int ks8995_registers_read(struct kobject *kobj,
254 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
255 {
256 struct device *dev;
257 struct ks8995_switch *ks8995;
258
259 dev = container_of(kobj, struct device, kobj);
260 ks8995 = dev_get_drvdata(dev);
261
262 if (unlikely(off > KS8995_REGS_SIZE))
263 return 0;
264
265 if ((off + count) > KS8995_REGS_SIZE)
266 count = KS8995_REGS_SIZE - off;
267
268 if (unlikely(!count))
269 return count;
270
271 return ks8995_read(ks8995, buf, off, count);
272 }
273
274
275 static int ks8995_registers_write(struct kobject *kobj,
276 struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
277 {
278 struct device *dev;
279 struct ks8995_switch *ks8995;
280
281 dev = container_of(kobj, struct device, kobj);
282 ks8995 = dev_get_drvdata(dev);
283
284 if (unlikely(off >= KS8995_REGS_SIZE))
285 return -EFBIG;
286
287 if ((off + count) > KS8995_REGS_SIZE)
288 count = KS8995_REGS_SIZE - off;
289
290 if (unlikely(!count))
291 return count;
292
293 return ks8995_write(ks8995, buf, off, count);
294 }
295
296
297 static struct bin_attribute ks8995_registers_attr = {
298 .attr = {
299 .name = "registers",
300 .mode = S_IRUSR | S_IWUSR,
301 },
302 .size = KS8995_REGS_SIZE,
303 .read = ks8995_registers_read,
304 .write = ks8995_registers_write,
305 };
306
307 /*-------------------------------------------------------------------------*/
308
309 static int __devinit ks8995_probe(struct spi_device *spi)
310 {
311 struct ks8995_switch *ks;
312 struct ks8995_pdata *pdata;
313 u8 ids[2];
314 int err;
315
316 /* Chip description */
317 pdata = spi->dev.platform_data;
318
319 ks = kzalloc(sizeof(*ks), GFP_KERNEL);
320 if (!ks) {
321 dev_err(&spi->dev, "no memory for private data\n");
322 return-ENOMEM;
323 }
324
325 mutex_init(&ks->lock);
326 ks->pdata = pdata;
327 ks->spi = spi_dev_get(spi);
328 dev_set_drvdata(&spi->dev, ks);
329
330 spi->mode = SPI_MODE_0;
331 spi->bits_per_word = 8;
332 err = spi_setup(spi);
333 if (err) {
334 dev_err(&spi->dev, "spi_setup failed, err=%d \n", err);
335 goto err_drvdata;
336 }
337
338 err = ks8995_read(ks, ids, KS8995_REG_ID0, sizeof(ids));
339 if (err < 0) {
340 dev_err(&spi->dev, "unable to read id registers, err=%d \n",
341 err);
342 goto err_drvdata;
343 }
344
345 switch (ids[0]) {
346 case FAMILY_KS8995:
347 break;
348 default:
349 dev_err(&spi->dev, "unknown family id:%02x\n", ids[0]);
350 err = -ENODEV;
351 goto err_drvdata;
352 }
353
354 err = ks8995_reset(ks);
355 if (err)
356 goto err_drvdata;
357
358 err = sysfs_create_bin_file(&spi->dev.kobj, &ks8995_registers_attr);
359 if (err) {
360 dev_err(&spi->dev, "unable to create sysfs file, err=%d\n",
361 err);
362 goto err_drvdata;
363 }
364
365 dev_info(&spi->dev, "KS89%02X device found, Chip ID:%01x, "
366 "Revision:%01x\n", ids[0],
367 get_chip_id(ids[1]), get_chip_rev(ids[1]));
368
369 return 0;
370
371 err_drvdata:
372 dev_set_drvdata(&spi->dev, NULL);
373 kfree(ks);
374 return err;
375 }
376
377 static int __devexit ks8995_remove(struct spi_device *spi)
378 {
379 struct ks8995_data *ks8995;
380
381 ks8995 = dev_get_drvdata(&spi->dev);
382 sysfs_remove_bin_file(&spi->dev.kobj, &ks8995_registers_attr);
383
384 dev_set_drvdata(&spi->dev, NULL);
385 kfree(ks8995);
386
387 return 0;
388 }
389
390 /*-------------------------------------------------------------------------*/
391
392 static struct spi_driver ks8995_driver = {
393 .driver = {
394 .name = "spi-ks8995",
395 .bus = &spi_bus_type,
396 .owner = THIS_MODULE,
397 },
398 .probe = ks8995_probe,
399 .remove = __devexit_p(ks8995_remove),
400 };
401
402 static int __init ks8995_init(void)
403 {
404 printk(KERN_INFO DRV_DESC " version " DRV_VERSION"\n");
405
406 return spi_register_driver(&ks8995_driver);
407 }
408 module_init(ks8995_init);
409
410 static void __exit ks8995_exit(void)
411 {
412 spi_unregister_driver(&ks8995_driver);
413 }
414 module_exit(ks8995_exit);
415
416 MODULE_DESCRIPTION(DRV_DESC);
417 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
418 MODULE_LICENSE("GPL v2");
419