wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | #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 | |
| 10 | struct _zuma_mbox_dev zuma_mbox_dev; |
| 11 | |
| 12 | |
| 13 | static int zuma_mbox_write(struct _zuma_mbox_dev *dev, unsigned int data) |
| 14 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 15 | unsigned int status, count = 0, i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 16 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 17 | status = (volatile int) le32_to_cpu(dev->sip->mbox_status); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 18 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 19 | 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 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 31 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 32 | printf("mbox tx timeout\n"); |
| 33 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static int zuma_mbox_read(struct _zuma_mbox_dev *dev, unsigned int *data) |
| 37 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 38 | unsigned int status, count = 0, i; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 39 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 40 | status = (volatile int) le32_to_cpu(dev->sip->mbox_status); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 41 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 42 | 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; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int zuma_mbox_do_one_mailbox(unsigned int out, unsigned int *in) |
| 59 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 60 | 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; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | #define RET_IF_FAILED(x) if ((x) == -1) return -1 |
| 75 | |
| 76 | static int zuma_mbox_do_all_mailbox(void) |
| 77 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 78 | unsigned int data_in; |
| 79 | unsigned short sdata_in; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 80 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 81 | RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_START, &data_in)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 82 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 83 | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 88 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 89 | RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_IP, &data_in)); |
| 90 | zuma_ip = data_in; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 91 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 92 | RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_SLOT, &data_in)); |
| 93 | zuma_slot_bac = data_in >> 3; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 94 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 95 | 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; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 98 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 99 | 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); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 106 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 107 | RET_IF_FAILED(zuma_mbox_do_one_mailbox(ZUMA_MBOXMSG_DONE, &data_in)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 108 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 109 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 113 | static void zuma_mbox_dump(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 114 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 115 | printf("ACC MAC=%04x%08x\n", *(unsigned short *) (&zuma_acc_mac), |
| 116 | *(unsigned int *) ((char *) &zuma_acc_mac + 2)); |
| 117 | printf("PRV MAC=%04x%08x\n", *(unsigned short *) (&zuma_prv_mac), |
| 118 | *(unsigned int *) ((char *) &zuma_prv_mac + 2)); |
| 119 | printf("slot:bac=%d:%d\n", (zuma_slot_bac >> 2) & 0xf, |
| 120 | zuma_slot_bac & 0x3); |
| 121 | printf("BAUD1=%d BAUD2=%d\n", zuma_console_baud, zuma_debug_baud); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 125 | static void zuma_mbox_setenv(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 126 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 127 | char *data, buf[32]; |
| 128 | unsigned char save = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 129 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 130 | data = getenv("baudrate"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 131 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 132 | if (!data || (zuma_console_baud != simple_strtoul(data, NULL, 10))) { |
| 133 | sprintf(buf, "%6d", zuma_console_baud); |
| 134 | setenv("baudrate", buf); |
| 135 | save = 1; |
| 136 | printf("baudrate doesn't match from mbox\n"); |
| 137 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 138 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 139 | ip_to_string(zuma_ip, buf); |
| 140 | setenv("ipaddr", buf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 141 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 142 | sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 143 | zuma_prv_mac[0], |
| 144 | zuma_prv_mac[1], |
| 145 | zuma_prv_mac[2], |
| 146 | zuma_prv_mac[3], zuma_prv_mac[4], zuma_prv_mac[5]); |
| 147 | setenv("ethaddr", buf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 148 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 149 | sprintf(buf, "%02x", zuma_slot_bac); |
| 150 | setenv("bacslot", buf); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 151 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 152 | if (save) |
| 153 | saveenv(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 157 | * zuma_mbox_init: |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 158 | */ |
| 159 | |
| 160 | int zuma_mbox_init(void) |
| 161 | { |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 162 | unsigned int iobase; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 163 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 164 | memset(&zuma_mbox_dev, 0, sizeof(struct _zuma_mbox_dev)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 165 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 166 | zuma_mbox_dev.dev = |
| 167 | pci_find_device(VENDOR_ID_ZUMA, DEVICE_ID_ZUMA_PBB, 0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 168 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 169 | if (zuma_mbox_dev.dev == -1) { |
| 170 | printf("no zuma pbb\n"); |
| 171 | return -1; |
| 172 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 173 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 174 | pci_read_config_dword(zuma_mbox_dev.dev, PCI_BASE_ADDRESS_0, &iobase); |
Wolfgang Denk | 422d786 | 2008-12-09 23:13:51 +0100 | [diff] [blame] | 175 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 176 | iobase &= PCI_BASE_ADDRESS_MEM_MASK; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 177 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 178 | zuma_mbox_dev.sip = (PBB_DMA_REG_MAP *) iobase; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 179 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 180 | zuma_mbox_dev.sip->int_mask.word = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 181 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 182 | printf("pbb @ %p v%d.%d, timestamp %08x\n", zuma_mbox_dev.sip, |
| 183 | zuma_mbox_dev.sip->version.pci_bits.rev_major, |
| 184 | zuma_mbox_dev.sip->version.pci_bits.rev_minor, |
| 185 | zuma_mbox_dev.sip->timestamp); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 186 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 187 | if (zuma_mbox_do_all_mailbox() == -1) { |
| 188 | printf("mailbox failed.. no ACC?\n"); |
| 189 | return -1; |
| 190 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 191 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 192 | zuma_mbox_dump(); |
| 193 | |
| 194 | zuma_mbox_setenv(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 195 | |
Wolfgang Denk | 161ea60 | 2011-11-09 09:29:00 +0000 | [diff] [blame^] | 196 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 197 | } |