1
2
3
4
5
6
7#include <asm/io.h>
8#include <common.h>
9#include <command.h>
10#include <malloc.h>
11#include <net.h>
12#include <miiphy.h>
13
14#include "ks8851_mll.h"
15
16#define DRIVERNAME "ks8851_mll"
17
18#define MAX_RECV_FRAMES 32
19#define MAX_BUF_SIZE 2048
20#define TX_BUF_SIZE 2000
21#define RX_BUF_SIZE 2000
22
23static const struct chip_id chip_ids[] = {
24 {CIDER_ID, "KSZ8851"},
25 {0, NULL},
26};
27
28
29
30
31
32
33
34
35
36
37union ks_tx_hdr {
38 u8 txb[4];
39 __le16 txw[2];
40};
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70struct type_frame_head {
71 u16 sts;
72 u16 len;
73} fr_h_i[MAX_RECV_FRAMES];
74
75struct ks_net {
76 struct net_device *netdev;
77 union ks_tx_hdr txh;
78 struct type_frame_head *frame_head_info;
79 u32 msg_enable;
80 u32 frame_cnt;
81 int bus_width;
82 int irq;
83 u16 rc_rxqcr;
84 u16 rc_txcr;
85 u16 rc_ier;
86 u16 sharedbus;
87 u16 cmd_reg_cache;
88 u16 cmd_reg_cache_int;
89 u16 promiscuous;
90 u16 all_mcast;
91 u16 mcast_lst_size;
92 u8 mcast_lst[MAX_MCAST_LST][MAC_ADDR_LEN];
93 u8 mcast_bits[HW_MCAST_SIZE];
94 u8 mac_addr[6];
95 u8 fid;
96 u8 extra_byte;
97 u8 enabled;
98} ks_str, *ks;
99
100#define BE3 0x8000
101#define BE2 0x4000
102#define BE1 0x2000
103#define BE0 0x1000
104
105static u8 ks_rdreg8(struct eth_device *dev, u16 offset)
106{
107 u8 shift_bit = offset & 0x03;
108 u8 shift_data = (offset & 1) << 3;
109
110 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
111
112 return (u8)(readw(dev->iobase) >> shift_data);
113}
114
115static u16 ks_rdreg16(struct eth_device *dev, u16 offset)
116{
117 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
118
119 return readw(dev->iobase);
120}
121
122static void ks_wrreg8(struct eth_device *dev, u16 offset, u8 val)
123{
124 u8 shift_bit = (offset & 0x03);
125 u16 value_write = (u16)(val << ((offset & 1) << 3));
126
127 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
128 writew(value_write, dev->iobase);
129}
130
131static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val)
132{
133 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
134 writew(val, dev->iobase);
135}
136
137
138
139
140
141
142
143
144static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len)
145{
146 len >>= 1;
147
148 while (len--)
149 *wptr++ = readw(dev->iobase);
150}
151
152
153
154
155
156
157
158static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len)
159{
160 len >>= 1;
161
162 while (len--)
163 writew(*wptr++, dev->iobase);
164}
165
166static void ks_enable_int(struct eth_device *dev)
167{
168 ks_wrreg16(dev, KS_IER, ks->rc_ier);
169}
170
171static void ks_set_powermode(struct eth_device *dev, unsigned pwrmode)
172{
173 unsigned pmecr;
174
175 ks_rdreg16(dev, KS_GRR);
176 pmecr = ks_rdreg16(dev, KS_PMECR);
177 pmecr &= ~PMECR_PM_MASK;
178 pmecr |= pwrmode;
179
180 ks_wrreg16(dev, KS_PMECR, pmecr);
181}
182
183
184
185
186
187static void ks_read_config(struct eth_device *dev)
188{
189 u16 reg_data = 0;
190
191
192 reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF;
193 reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8;
194
195
196 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
197
198
199
200
201
202 if (reg_data & CCR_8BIT) {
203 ks->bus_width = ENUM_BUS_8BIT;
204 ks->extra_byte = 1;
205 } else if (reg_data & CCR_16BIT) {
206 ks->bus_width = ENUM_BUS_16BIT;
207 ks->extra_byte = 2;
208 } else {
209 ks->bus_width = ENUM_BUS_32BIT;
210 ks->extra_byte = 4;
211 }
212}
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227static void ks_soft_reset(struct eth_device *dev, unsigned op)
228{
229
230 ks_wrreg16(dev, KS_IER, 0x0000);
231 ks_wrreg16(dev, KS_GRR, op);
232 mdelay(10);
233 ks_wrreg16(dev, KS_GRR, 0);
234 mdelay(1);
235}
236
237void ks_enable_qmu(struct eth_device *dev)
238{
239 u16 w;
240
241 w = ks_rdreg16(dev, KS_TXCR);
242
243
244 ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE);
245
246
247 w = ks_rdreg16(dev, KS_RXQCR);
248 ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE);
249
250
251 w = ks_rdreg16(dev, KS_RXCR1);
252 ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE);
253}
254
255static void ks_disable_qmu(struct eth_device *dev)
256{
257 u16 w;
258
259 w = ks_rdreg16(dev, KS_TXCR);
260
261
262 w &= ~TXCR_TXE;
263 ks_wrreg16(dev, KS_TXCR, w);
264
265
266 w = ks_rdreg16(dev, KS_RXCR1);
267 w &= ~RXCR1_RXE;
268 ks_wrreg16(dev, KS_RXCR1, w);
269}
270
271static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len)
272{
273 u32 r = ks->extra_byte & 0x1;
274 u32 w = ks->extra_byte - r;
275
276
277 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
278 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
279
280
281
282
283
284
285
286
287 if (r)
288 ks_rdreg8(dev, 0);
289
290 ks_inblk(dev, buf, w + 2 + 2);
291
292
293 ks_inblk(dev, buf, ALIGN(len, 4));
294
295
296 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
297}
298
299static void ks_rcv(struct eth_device *dev, uchar **pv_data)
300{
301 struct type_frame_head *frame_hdr = ks->frame_head_info;
302 int i;
303
304 ks->frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8;
305
306
307 for (i = 0; i < ks->frame_cnt; i++) {
308
309 frame_hdr->sts = ks_rdreg16(dev, KS_RXFHSR);
310
311 frame_hdr->len = ks_rdreg16(dev, KS_RXFHBCR);
312 frame_hdr++;
313 }
314
315 frame_hdr = ks->frame_head_info;
316 while (ks->frame_cnt--) {
317 if ((frame_hdr->sts & RXFSHR_RXFV) &&
318 (frame_hdr->len < RX_BUF_SIZE) &&
319 frame_hdr->len) {
320
321 ks_read_qmu(dev, (u16 *)(*pv_data), frame_hdr->len);
322
323
324 net_process_received_packet(*pv_data, frame_hdr->len);
325 pv_data++;
326 } else {
327 ks_wrreg16(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
328 printf(DRIVERNAME ": bad packet\n");
329 }
330 frame_hdr++;
331 }
332}
333
334
335
336
337
338
339
340static int ks_read_selftest(struct eth_device *dev)
341{
342 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
343 u16 mbir;
344 int ret = 0;
345
346 mbir = ks_rdreg16(dev, KS_MBIR);
347
348 if ((mbir & both_done) != both_done) {
349 printf(DRIVERNAME ": Memory selftest not finished\n");
350 return 0;
351 }
352
353 if (mbir & MBIR_TXMBFA) {
354 printf(DRIVERNAME ": TX memory selftest fails\n");
355 ret |= 1;
356 }
357
358 if (mbir & MBIR_RXMBFA) {
359 printf(DRIVERNAME ": RX memory selftest fails\n");
360 ret |= 2;
361 }
362
363 debug(DRIVERNAME ": the selftest passes\n");
364
365 return ret;
366}
367
368static void ks_setup(struct eth_device *dev)
369{
370 u16 w;
371
372
373 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
374
375
376 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
377
378
379 ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
380
381
382 ks->rc_rxqcr = RXQCR_CMD_CNTL;
383 ks_wrreg16(dev, KS_RXQCR, ks->rc_rxqcr);
384
385
386
387
388
389
390 w = ks_rdreg16(dev, KS_P1MBCR);
391 w &= ~P1MBCR_FORCE_FDX;
392 ks_wrreg16(dev, KS_P1MBCR, w);
393
394 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
395 ks_wrreg16(dev, KS_TXCR, w);
396
397 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
398
399
400 w |= RXCR1_RXPAFMA;
401
402 ks_wrreg16(dev, KS_RXCR1, w);
403}
404
405static void ks_setup_int(struct eth_device *dev)
406{
407 ks->rc_ier = 0x00;
408
409
410 ks_wrreg16(dev, KS_ISR, 0xffff);
411
412
413 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
414}
415
416static int ks8851_mll_detect_chip(struct eth_device *dev)
417{
418 unsigned short val, i;
419
420 ks_read_config(dev);
421
422 val = ks_rdreg16(dev, KS_CIDER);
423
424 if (val == 0xffff) {
425
426 printf(DRIVERNAME ": is chip mounted ?\n");
427 return -1;
428 } else if ((val & 0xfff0) != CIDER_ID) {
429 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
430 return -1;
431 }
432
433 debug("Read back KS8851 id 0x%x\n", val);
434
435
436 val &= 0xfff0;
437 for (i = 0; chip_ids[i].id != 0; i++) {
438 if (chip_ids[i].id == val)
439 break;
440 }
441 if (!chip_ids[i].id) {
442 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
443 return -1;
444 }
445
446 dev->priv = (void *)&chip_ids[i];
447
448 return 0;
449}
450
451static void ks8851_mll_reset(struct eth_device *dev)
452{
453
454 ks_set_powermode(dev, PMECR_PM_NORMAL);
455 mdelay(1);
456
457
458 ks_soft_reset(dev, GRR_GSR);
459
460
461 ks_wrreg16(dev, KS_IER, 0x0000);
462 ks_wrreg16(dev, KS_ISR, 0xffff);
463
464
465 ks_disable_qmu(dev);
466}
467
468static void ks8851_mll_phy_configure(struct eth_device *dev)
469{
470 u16 data;
471
472 ks_setup(dev);
473 ks_setup_int(dev);
474
475
476 data = ks_rdreg16(dev, KS_OBCR);
477 ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA);
478
479 debug(DRIVERNAME ": phy initialized\n");
480}
481
482static void ks8851_mll_enable(struct eth_device *dev)
483{
484 ks_wrreg16(dev, KS_ISR, 0xffff);
485 ks_enable_int(dev);
486 ks_enable_qmu(dev);
487}
488
489static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
490{
491 struct chip_id *id = dev->priv;
492
493 debug(DRIVERNAME ": detected %s controller\n", id->name);
494
495 if (ks_read_selftest(dev)) {
496 printf(DRIVERNAME ": Selftest failed\n");
497 return -1;
498 }
499
500 ks8851_mll_reset(dev);
501
502
503 ks8851_mll_phy_configure(dev);
504
505
506 ks->frame_head_info = fr_h_i;
507
508
509 ks8851_mll_enable(dev);
510
511 return 0;
512}
513
514static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len)
515{
516
517 ks->txh.txw[0] = 0;
518 ks->txh.txw[1] = cpu_to_le16(len);
519
520
521 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
522 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
523
524 ks_outblk(dev, ks->txh.txw, 4);
525
526 ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4));
527
528 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
529
530 ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE);
531
532 do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE);
533}
534
535static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
536{
537 u8 *data = (u8 *)packet;
538 u16 tmplen = (u16)length;
539 u16 retv;
540
541
542
543
544
545 retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff;
546 if (retv >= tmplen + 12) {
547 ks_write_qmu(dev, data, tmplen);
548 return 0;
549 } else {
550 printf(DRIVERNAME ": failed to send packet: No buffer\n");
551 return -1;
552 }
553}
554
555static void ks8851_mll_halt(struct eth_device *dev)
556{
557 ks8851_mll_reset(dev);
558}
559
560
561
562
563
564
565
566static int ks8851_mll_recv(struct eth_device *dev)
567{
568 u16 status;
569
570 status = ks_rdreg16(dev, KS_ISR);
571
572 ks_wrreg16(dev, KS_ISR, status);
573
574 if ((status & IRQ_RXI))
575 ks_rcv(dev, (uchar **)net_rx_packets);
576
577 if ((status & IRQ_LDI)) {
578 u16 pmecr = ks_rdreg16(dev, KS_PMECR);
579 pmecr &= ~PMECR_WKEVT_MASK;
580 ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
581 }
582
583 return 0;
584}
585
586static int ks8851_mll_write_hwaddr(struct eth_device *dev)
587{
588 u16 addrl, addrm, addrh;
589
590 addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1];
591 addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3];
592 addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5];
593
594 ks_wrreg16(dev, KS_MARH, addrh);
595 ks_wrreg16(dev, KS_MARM, addrm);
596 ks_wrreg16(dev, KS_MARL, addrl);
597
598 return 0;
599}
600
601int ks8851_mll_initialize(u8 dev_num, int base_addr)
602{
603 struct eth_device *dev;
604
605 dev = malloc(sizeof(*dev));
606 if (!dev) {
607 printf("Error: Failed to allocate memory\n");
608 return -1;
609 }
610 memset(dev, 0, sizeof(*dev));
611
612 dev->iobase = base_addr;
613
614 ks = &ks_str;
615
616
617 if (ks8851_mll_detect_chip(dev)) {
618 free(dev);
619 return -1;
620 }
621
622 dev->init = ks8851_mll_init;
623 dev->halt = ks8851_mll_halt;
624 dev->send = ks8851_mll_send;
625 dev->recv = ks8851_mll_recv;
626 dev->write_hwaddr = ks8851_mll_write_hwaddr;
627 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
628
629 eth_register(dev);
630
631 return 0;
632}
633