blob: 621c64cd820dcb86651c7de377fdeabad81f87d2 [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{
Wolfgang Denk82732072011-11-09 09:29:00 +000015 unsigned int status, count = 0, i;
wdenkfe8c2802002-11-03 00:38:21 +000016
Wolfgang Denk82732072011-11-09 09:29:00 +000017 status = (volatile int) le32_to_cpu(dev->sip->mbox_status);
wdenkfe8c2802002-11-03 00:38:21 +000018
Wolfgang Denk82732072011-11-09 09:29:00 +000019 while ((status & OUT_PENDING) && count < 1000) {
20 count++;
21 for (i = 0; i < 1000; i++)
22 ;
23 status = (volatile int) le32_to_cpu(dev->sip->mbox_status);
24 }
25 if (count < 1000) {
26 /* if SET it means msg pending */
27 /* printf("mbox real write %08x\n",data); */
28 dev->sip->mbox_out = cpu_to_le32(data);
29 return 4;
30 }
wdenkfe8c2802002-11-03 00:38:21 +000031
Wolfgang Denk82732072011-11-09 09:29:00 +000032 printf("mbox tx timeout\n");
33 return 0;
wdenkfe8c2802002-11-03 00:38:21 +000034}
35
36static int zuma_mbox_read(struct _zuma_mbox_dev *dev, unsigned int *data)
37{
Wolfgang Denk82732072011-11-09 09:29:00 +000038 unsigned int status, count = 0, i;
wdenkfe8c2802002-11-03 00:38:21 +000039
Wolfgang Denk82732072011-11-09 09:29:00 +000040 status = (volatile int) le32_to_cpu(dev->sip->mbox_status);
wdenkfe8c2802002-11-03 00:38:21 +000041
Wolfgang Denk82732072011-11-09 09:29:00 +000042 while (!(status & IN_VALID) && count < 1000) {
43 count++;
44 for (i = 0; i < 1000; i++)
45 ;
46 status = (volatile int) le32_to_cpu(dev->sip->mbox_status);
47 }
48 if (count < 1000) {
49 /* if SET it means msg pending */
50 *data = le32_to_cpu(dev->sip->mbox_in);
51 /*printf("mbox real read %08x\n", *data); */
52 return 4;
53 }
54 printf("mbox rx timeout\n");
55 return 0;
wdenkfe8c2802002-11-03 00:38:21 +000056}
57
58static int zuma_mbox_do_one_mailbox(unsigned int out, unsigned int *in)
59{
Wolfgang Denk82732072011-11-09 09:29:00 +000060 int ret;
61
62 ret = zuma_mbox_write(&zuma_mbox_dev, out);
63 /*printf("write 0x%08x (%d bytes)\n", out, ret); */
64 if (ret != 4)
65 return -1;
66 ret = zuma_mbox_read(&zuma_mbox_dev, in);
67 /*printf("read 0x%08x (%d bytes)\n", *in, ret); */
68 if (ret != 4)
69 return -1;
70 return 0;
wdenkfe8c2802002-11-03 00:38:21 +000071}
72
73
74#define RET_IF_FAILED(x) if ((x) == -1) return -1
75
76static int zuma_mbox_do_all_mailbox(void)
77{
Wolfgang Denk82732072011-11-09 09:29:00 +000078 unsigned int data_in;
79 unsigned short sdata_in;
wdenkfe8c2802002-11-03 00:38:21 +000080
Wolfgang Denk82732072011-11-09 09:29:00 +000081 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_START, &data_in));
wdenkfe8c2802002-11-03 00:38:21 +000082
Wolfgang Denk82732072011-11-09 09:29:00 +000083 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_MACL, &data_in));
84 memcpy(zuma_acc_mac + 2, &data_in, 4);
85 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_MACH, &data_in));
86 sdata_in = data_in & 0xffff;
87 memcpy(zuma_acc_mac, &sdata_in, 2);
wdenkfe8c2802002-11-03 00:38:21 +000088
Wolfgang Denk82732072011-11-09 09:29:00 +000089 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_IP, &data_in));
90 zuma_ip = data_in;
wdenkfe8c2802002-11-03 00:38:21 +000091
Wolfgang Denk82732072011-11-09 09:29:00 +000092 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_SLOT, &data_in));
93 zuma_slot_bac = data_in >> 3;
wdenkfe8c2802002-11-03 00:38:21 +000094
Wolfgang Denk82732072011-11-09 09:29:00 +000095 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_BAUD, &data_in));
96 zuma_console_baud = data_in & 0xffff;
97 zuma_debug_baud = data_in >> 16;
wdenkfe8c2802002-11-03 00:38:21 +000098
Wolfgang Denk82732072011-11-09 09:29:00 +000099 RET_IF_FAILED(zuma_mbox_do_one_mailbox
100 (ZUMA_MBOXMSG_ENG_PRV_MACL, &data_in));
101 memcpy(zuma_prv_mac + 2, &data_in, 4);
102 RET_IF_FAILED(zuma_mbox_do_one_mailbox
103 (ZUMA_MBOXMSG_ENG_PRV_MACH, &data_in));
104 sdata_in = data_in & 0xffff;
105 memcpy(zuma_prv_mac, &sdata_in, 2);
wdenkfe8c2802002-11-03 00:38:21 +0000106
Wolfgang Denk82732072011-11-09 09:29:00 +0000107 RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_DONE, &data_in));
wdenkfe8c2802002-11-03 00:38:21 +0000108
Wolfgang Denk82732072011-11-09 09:29:00 +0000109 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000110}
111
112
Wolfgang Denk82732072011-11-09 09:29:00 +0000113static void zuma_mbox_dump(void)
wdenkfe8c2802002-11-03 00:38:21 +0000114{
Wolfgang Denk504f7082011-11-09 09:29:01 +0000115 unsigned short s;
116 unsigned int i;
117
118 memcpy(&s, &zuma_acc_mac, sizeof(s));
119 memcpy(&i, &zuma_acc_mac[2], sizeof(i));
120 printf("ACC MAC=%04x%08x\n", s, i);
121
122 memcpy(&s, &zuma_prv_mac, sizeof(s));
123 memcpy(&s, &zuma_prv_mac[2], sizeof(i));
124 printf("PRV MAC=%04x%08x\n", s, i);
125
126 printf("slot:bac=%d:%d\n",
127 (zuma_slot_bac >> 2) & 0xf,
128 zuma_slot_bac & 0x3);
129
130 printf("BAUD1=%d BAUD2=%d\n",
131 zuma_console_baud,
132 zuma_debug_baud);
wdenkfe8c2802002-11-03 00:38:21 +0000133}
134
135
Wolfgang Denk82732072011-11-09 09:29:00 +0000136static void zuma_mbox_setenv(void)
wdenkfe8c2802002-11-03 00:38:21 +0000137{
Wolfgang Denk82732072011-11-09 09:29:00 +0000138 char *data, buf[32];
139 unsigned char save = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000140
Wolfgang Denk82732072011-11-09 09:29:00 +0000141 data = getenv("baudrate");
wdenkfe8c2802002-11-03 00:38:21 +0000142
Wolfgang Denk82732072011-11-09 09:29:00 +0000143 if (!data || (zuma_console_baud != simple_strtoul(data, NULL, 10))) {
144 sprintf(buf, "%6d", zuma_console_baud);
145 setenv("baudrate", buf);
146 save = 1;
147 printf("baudrate doesn't match from mbox\n");
148 }
wdenkfe8c2802002-11-03 00:38:21 +0000149
Wolfgang Denk82732072011-11-09 09:29:00 +0000150 ip_to_string(zuma_ip, buf);
151 setenv("ipaddr", buf);
wdenkfe8c2802002-11-03 00:38:21 +0000152
Wolfgang Denk82732072011-11-09 09:29:00 +0000153 sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x",
154 zuma_prv_mac[0],
155 zuma_prv_mac[1],
156 zuma_prv_mac[2],
157 zuma_prv_mac[3], zuma_prv_mac[4], zuma_prv_mac[5]);
158 setenv("ethaddr", buf);
wdenkfe8c2802002-11-03 00:38:21 +0000159
Wolfgang Denk82732072011-11-09 09:29:00 +0000160 sprintf(buf, "%02x", zuma_slot_bac);
161 setenv("bacslot", buf);
wdenkfe8c2802002-11-03 00:38:21 +0000162
Wolfgang Denk82732072011-11-09 09:29:00 +0000163 if (save)
164 saveenv();
wdenkfe8c2802002-11-03 00:38:21 +0000165}
166
167/**
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200168 * zuma_mbox_init:
wdenkfe8c2802002-11-03 00:38:21 +0000169 */
170
171int zuma_mbox_init(void)
172{
Wolfgang Denk82732072011-11-09 09:29:00 +0000173 unsigned int iobase;
wdenkfe8c2802002-11-03 00:38:21 +0000174
Wolfgang Denk82732072011-11-09 09:29:00 +0000175 memset(&zuma_mbox_dev, 0, sizeof(struct _zuma_mbox_dev));
wdenkfe8c2802002-11-03 00:38:21 +0000176
Wolfgang Denk82732072011-11-09 09:29:00 +0000177 zuma_mbox_dev.dev =
178 pci_find_device(VENDOR_ID_ZUMA, DEVICE_ID_ZUMA_PBB, 0);
wdenkfe8c2802002-11-03 00:38:21 +0000179
Wolfgang Denk82732072011-11-09 09:29:00 +0000180 if (zuma_mbox_dev.dev == -1) {
181 printf("no zuma pbb\n");
182 return -1;
183 }
wdenkfe8c2802002-11-03 00:38:21 +0000184
Wolfgang Denk82732072011-11-09 09:29:00 +0000185 pci_read_config_dword(zuma_mbox_dev.dev, PCI_BASE_ADDRESS_0, &iobase);
Wolfgang Denk1113cb72008-12-09 23:13:51 +0100186
Wolfgang Denk82732072011-11-09 09:29:00 +0000187 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
wdenkfe8c2802002-11-03 00:38:21 +0000188
Wolfgang Denk82732072011-11-09 09:29:00 +0000189 zuma_mbox_dev.sip = (PBB_DMA_REG_MAP *) iobase;
wdenkfe8c2802002-11-03 00:38:21 +0000190
Wolfgang Denk82732072011-11-09 09:29:00 +0000191 zuma_mbox_dev.sip->int_mask.word = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000192
Wolfgang Denk82732072011-11-09 09:29:00 +0000193 printf("pbb @ %p v%d.%d, timestamp %08x\n", zuma_mbox_dev.sip,
194 zuma_mbox_dev.sip->version.pci_bits.rev_major,
195 zuma_mbox_dev.sip->version.pci_bits.rev_minor,
196 zuma_mbox_dev.sip->timestamp);
wdenkfe8c2802002-11-03 00:38:21 +0000197
Wolfgang Denk82732072011-11-09 09:29:00 +0000198 if (zuma_mbox_do_all_mailbox() == -1) {
199 printf("mailbox failed.. no ACC?\n");
200 return -1;
201 }
wdenkfe8c2802002-11-03 00:38:21 +0000202
Wolfgang Denk82732072011-11-09 09:29:00 +0000203 zuma_mbox_dump();
wdenkfe8c2802002-11-03 00:38:21 +0000204
Wolfgang Denk82732072011-11-09 09:29:00 +0000205 zuma_mbox_setenv();
206
207 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000208}