blob: 6f5df6e806dd75b91e190de213a19f88846dfd4e [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001#include <common.h>
2#include <galileo/pci.h>
3#include <net.h>
4#include <pci.h>
5
6#include "zuma_pbb.h"
7#include "zuma_pbb_mbox.h"
8
9
10struct _zuma_mbox_dev zuma_mbox_dev;
11
12
13static int zuma_mbox_write(struct _zuma_mbox_dev *dev, unsigned int data)
14{
15 unsigned int status, count = 0, i;
16
17 status = (volatile int)le32_to_cpu(dev->sip->mbox_status);
18
19 while((status & OUT_PENDING) && count < 1000) {
20 count++;
21 for(i=0;i<1000;i++);
22 status = (volatile int)le32_to_cpu(dev->sip->mbox_status);
23 }
24 if(count < 1000) {
25 /* if SET it means msg pending */
26 /* printf("mbox real write %08x\n",data); */
27 dev->sip->mbox_out = cpu_to_le32(data);
28 return 4;
29 }
30
31 printf("mbox tx timeout\n");
32 return 0;
33}
34
35static int zuma_mbox_read(struct _zuma_mbox_dev *dev, unsigned int *data)
36{
37 unsigned int status, count = 0, i;
38
39 status = (volatile int)le32_to_cpu(dev->sip->mbox_status);
40
41 while(!(status & IN_VALID) && count < 1000) {
42 count++;
43 for(i=0;i<1000;i++);
44 status = (volatile int)le32_to_cpu(dev->sip->mbox_status);
45 }
46 if(count < 1000) {
47 /* if SET it means msg pending */
48 *data=le32_to_cpu(dev->sip->mbox_in);
49 /*printf("mbox real read %08x\n", *data); */
50 return 4;
51 }
52 printf("mbox rx timeout\n");
53 return 0;
54}
55
56static int zuma_mbox_do_one_mailbox(unsigned int out, unsigned int *in)
57{
58 int ret;
59 ret=zuma_mbox_write(&zuma_mbox_dev,out);
60 /*printf("write 0x%08x (%d bytes)\n", out, ret); */
61 if(ret!=4) return -1;
62 ret=zuma_mbox_read(&zuma_mbox_dev,in);
63 /*printf("read 0x%08x (%d bytes)\n", *in, ret); */
64 if(ret!=4) return -1;
65 return 0;
66}
67
68
69#define RET_IF_FAILED(x) if ((x) == -1) return -1
70
71static int zuma_mbox_do_all_mailbox(void)
72{
73 unsigned int data_in;
74 unsigned short sdata_in;
75
76 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_START, &data_in));
77
78 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_MACL, &data_in));
79 memcpy(zuma_acc_mac+2,&data_in,4);
80 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_MACH, &data_in));
81 sdata_in=data_in&0xffff;
82 memcpy(zuma_acc_mac,&sdata_in,2);
83
84 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_IP, &data_in));
85 zuma_ip=data_in;
86
87 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_SLOT, &data_in));
88 zuma_slot_bac=data_in>>3;
89
90 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_BAUD, &data_in));
91 zuma_console_baud = data_in & 0xffff;
92 zuma_debug_baud = data_in >> 16;
93
94 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_ENG_PRV_MACL, &data_in));
95 memcpy(zuma_prv_mac+2,&data_in,4);
96 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_ENG_PRV_MACH, &data_in));
97 sdata_in=data_in&0xffff;
98 memcpy(zuma_prv_mac,&sdata_in,2);
99
100 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_DONE, &data_in));
101
102 return 0;
103}
104
105
106static void
107zuma_mbox_dump(void)
108{
109 printf("ACC MAC=%04x%08x\n",*(unsigned short *)(&zuma_acc_mac),*(unsigned int *)((char *)&zuma_acc_mac+2));
110 printf("PRV MAC=%04x%08x\n",*(unsigned short *)(&zuma_prv_mac),*(unsigned int *)((char *)&zuma_prv_mac+2));
111 printf("slot:bac=%d:%d\n",(zuma_slot_bac>>2)&0xf, zuma_slot_bac & 0x3);
112 printf("BAUD1=%d BAUD2=%d\n",zuma_console_baud,zuma_debug_baud);
113}
114
115
116static void
117zuma_mbox_setenv(void)
118{
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200119 char *data, buf[32];
wdenkfe8c2802002-11-03 00:38:21 +0000120 unsigned char save = 0;
121
122 data = getenv("baudrate");
123
124 if(!data || (zuma_console_baud != simple_strtoul(data, NULL, 10))) {
125 sprintf(buf, "%6d", zuma_console_baud);
126 setenv("baudrate", buf);
127 save=1;
128 printf("baudrate doesn't match from mbox\n");
129 }
130
131 ip_to_string(zuma_ip, buf);
132 setenv("ipaddr", buf);
133
134 sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x",
135 zuma_prv_mac[0],
136 zuma_prv_mac[1],
137 zuma_prv_mac[2],
138 zuma_prv_mac[3],
139 zuma_prv_mac[4],
140 zuma_prv_mac[5]);
141 setenv("ethaddr", buf);
142
143 sprintf(buf,"%02x",zuma_slot_bac);
144 setenv("bacslot", buf);
145
146 if(save)
147 saveenv();
148}
149
150/**
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200151 * zuma_mbox_init:
wdenkfe8c2802002-11-03 00:38:21 +0000152 */
153
154int zuma_mbox_init(void)
155{
156 unsigned int iobase;
157 memset(&zuma_mbox_dev, 0, sizeof(struct _zuma_mbox_dev));
158
159 zuma_mbox_dev.dev = pci_find_device(VENDOR_ID_ZUMA, DEVICE_ID_ZUMA_PBB, 0);
160
161 if(zuma_mbox_dev.dev == -1) {
162 printf("no zuma pbb\n");
163 return -1;
164 }
165
166 pci_read_config_dword(zuma_mbox_dev.dev, PCI_BASE_ADDRESS_0, &iobase);
167
168 zuma_mbox_dev.sip = (PBB_DMA_REG_MAP *) (iobase & PCI_BASE_ADDRESS_MEM_MASK);
169
170 zuma_mbox_dev.sip->int_mask.word=0;
171
172 printf("pbb @ %p v%d.%d, timestamp %08x\n", zuma_mbox_dev.sip,
173 zuma_mbox_dev.sip->version.pci_bits.rev_major,
174 zuma_mbox_dev.sip->version.pci_bits.rev_minor,
175 zuma_mbox_dev.sip->timestamp);
176
177 if (zuma_mbox_do_all_mailbox() == -1) {
178 printf("mailbox failed.. no ACC?\n");
179 return -1;
180 }
181
182 zuma_mbox_dump();
183
184 zuma_mbox_setenv();
185
186 return 0;
187}