Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for the PM3386s on the Radisys ENP2611 |
| 3 | * Copyright (C) 2004, 2005 Lennert Buytenhek <buytenh@wantstofly.org> |
| 4 | * Dedicated to Marija Kulikova. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/config.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <asm/io.h> |
Lennert Buytenhek | 6744a50 | 2005-11-23 12:49:21 +0100 | [diff] [blame] | 17 | #include "pm3386.h" |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Read from register 'reg' of PM3386 device 'pm'. |
| 21 | */ |
| 22 | static u16 pm3386_reg_read(int pm, int reg) |
| 23 | { |
| 24 | void *_reg; |
| 25 | u16 value; |
| 26 | |
| 27 | _reg = (void *)ENP2611_PM3386_0_VIRT_BASE; |
| 28 | if (pm == 1) |
| 29 | _reg = (void *)ENP2611_PM3386_1_VIRT_BASE; |
| 30 | |
| 31 | value = *((volatile u16 *)(_reg + (reg << 1))); |
| 32 | |
| 33 | // printk(KERN_INFO "pm3386_reg_read(%d, %.3x) = %.8x\n", pm, reg, value); |
| 34 | |
| 35 | return value; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Write to register 'reg' of PM3386 device 'pm', and perform |
| 40 | * a readback from the identification register. |
| 41 | */ |
| 42 | static void pm3386_reg_write(int pm, int reg, u16 value) |
| 43 | { |
| 44 | void *_reg; |
| 45 | u16 dummy; |
| 46 | |
| 47 | // printk(KERN_INFO "pm3386_reg_write(%d, %.3x, %.8x)\n", pm, reg, value); |
| 48 | |
| 49 | _reg = (void *)ENP2611_PM3386_0_VIRT_BASE; |
| 50 | if (pm == 1) |
| 51 | _reg = (void *)ENP2611_PM3386_1_VIRT_BASE; |
| 52 | |
| 53 | *((volatile u16 *)(_reg + (reg << 1))) = value; |
| 54 | |
| 55 | dummy = *((volatile u16 *)_reg); |
| 56 | __asm__ __volatile__("mov %0, %0" : "+r" (dummy)); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Read from port 'port' register 'reg', where the registers |
| 61 | * for the different ports are 'spacing' registers apart. |
| 62 | */ |
| 63 | static u16 pm3386_port_reg_read(int port, int _reg, int spacing) |
| 64 | { |
| 65 | int reg; |
| 66 | |
| 67 | reg = _reg; |
| 68 | if (port & 1) |
| 69 | reg += spacing; |
| 70 | |
| 71 | return pm3386_reg_read(port >> 1, reg); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Write to port 'port' register 'reg', where the registers |
| 76 | * for the different ports are 'spacing' registers apart. |
| 77 | */ |
| 78 | static void pm3386_port_reg_write(int port, int _reg, int spacing, u16 value) |
| 79 | { |
| 80 | int reg; |
| 81 | |
| 82 | reg = _reg; |
| 83 | if (port & 1) |
| 84 | reg += spacing; |
| 85 | |
| 86 | pm3386_reg_write(port >> 1, reg, value); |
| 87 | } |
| 88 | |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 89 | int pm3386_secondary_present(void) |
| 90 | { |
| 91 | return pm3386_reg_read(1, 0) == 0x3386; |
| 92 | } |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 93 | |
| 94 | void pm3386_reset(void) |
| 95 | { |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 96 | u8 mac[3][6]; |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 97 | int secondary; |
| 98 | |
| 99 | secondary = pm3386_secondary_present(); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 100 | |
| 101 | /* Save programmed MAC addresses. */ |
| 102 | pm3386_get_mac(0, mac[0]); |
| 103 | pm3386_get_mac(1, mac[1]); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 104 | if (secondary) |
| 105 | pm3386_get_mac(2, mac[2]); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 106 | |
| 107 | /* Assert analog and digital reset. */ |
| 108 | pm3386_reg_write(0, 0x002, 0x0060); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 109 | if (secondary) |
| 110 | pm3386_reg_write(1, 0x002, 0x0060); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 111 | mdelay(1); |
| 112 | |
| 113 | /* Deassert analog reset. */ |
| 114 | pm3386_reg_write(0, 0x002, 0x0062); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 115 | if (secondary) |
| 116 | pm3386_reg_write(1, 0x002, 0x0062); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 117 | mdelay(10); |
| 118 | |
| 119 | /* Deassert digital reset. */ |
| 120 | pm3386_reg_write(0, 0x002, 0x0063); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 121 | if (secondary) |
| 122 | pm3386_reg_write(1, 0x002, 0x0063); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 123 | mdelay(10); |
| 124 | |
| 125 | /* Restore programmed MAC addresses. */ |
| 126 | pm3386_set_mac(0, mac[0]); |
| 127 | pm3386_set_mac(1, mac[1]); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 128 | if (secondary) |
| 129 | pm3386_set_mac(2, mac[2]); |
Lennert Buytenhek | c6e429b | 2005-11-23 12:49:55 +0100 | [diff] [blame] | 130 | |
| 131 | /* Disable carrier on all ports. */ |
| 132 | pm3386_set_carrier(0, 0); |
| 133 | pm3386_set_carrier(1, 0); |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 134 | if (secondary) |
| 135 | pm3386_set_carrier(2, 0); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static u16 swaph(u16 x) |
| 139 | { |
| 140 | return ((x << 8) | (x >> 8)) & 0xffff; |
| 141 | } |
| 142 | |
Lennert Buytenhek | 1ea739a | 2006-05-15 12:25:29 -0700 | [diff] [blame] | 143 | int pm3386_port_count(void) |
| 144 | { |
| 145 | return 2 + pm3386_secondary_present(); |
| 146 | } |
| 147 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 148 | void pm3386_init_port(int port) |
| 149 | { |
| 150 | int pm = port >> 1; |
| 151 | |
| 152 | /* |
| 153 | * Work around ENP2611 bootloader programming MAC address |
| 154 | * in reverse. |
| 155 | */ |
| 156 | if (pm3386_port_reg_read(port, 0x30a, 0x100) == 0x0000 && |
| 157 | (pm3386_port_reg_read(port, 0x309, 0x100) & 0xff00) == 0x5000) { |
| 158 | u16 temp[3]; |
| 159 | |
| 160 | temp[0] = pm3386_port_reg_read(port, 0x308, 0x100); |
| 161 | temp[1] = pm3386_port_reg_read(port, 0x309, 0x100); |
| 162 | temp[2] = pm3386_port_reg_read(port, 0x30a, 0x100); |
| 163 | pm3386_port_reg_write(port, 0x308, 0x100, swaph(temp[2])); |
| 164 | pm3386_port_reg_write(port, 0x309, 0x100, swaph(temp[1])); |
| 165 | pm3386_port_reg_write(port, 0x30a, 0x100, swaph(temp[0])); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Initialise narrowbanding mode. See application note 2010486 |
| 170 | * for more information. (@@@ We also need to issue a reset |
| 171 | * when ROOL or DOOL are detected.) |
| 172 | */ |
| 173 | pm3386_port_reg_write(port, 0x708, 0x10, 0xd055); |
| 174 | udelay(500); |
| 175 | pm3386_port_reg_write(port, 0x708, 0x10, 0x5055); |
| 176 | |
| 177 | /* |
| 178 | * SPI-3 ingress block. Set 64 bytes SPI-3 burst size |
| 179 | * towards SPI-3 bridge. |
| 180 | */ |
| 181 | pm3386_port_reg_write(port, 0x122, 0x20, 0x0002); |
| 182 | |
| 183 | /* |
| 184 | * Enable ingress protocol checking, and soft reset the |
| 185 | * SPI-3 ingress block. |
| 186 | */ |
| 187 | pm3386_reg_write(pm, 0x103, 0x0003); |
| 188 | while (!(pm3386_reg_read(pm, 0x103) & 0x80)) |
| 189 | ; |
| 190 | |
| 191 | /* |
| 192 | * SPI-3 egress block. Gather 12288 bytes of the current |
| 193 | * packet in the TX fifo before initiating transmit on the |
| 194 | * SERDES interface. (Prevents TX underflows.) |
| 195 | */ |
| 196 | pm3386_port_reg_write(port, 0x221, 0x20, 0x0007); |
| 197 | |
| 198 | /* |
| 199 | * Enforce odd parity from the SPI-3 bridge, and soft reset |
| 200 | * the SPI-3 egress block. |
| 201 | */ |
| 202 | pm3386_reg_write(pm, 0x203, 0x000d & ~(4 << (port & 1))); |
| 203 | while ((pm3386_reg_read(pm, 0x203) & 0x000c) != 0x000c) |
| 204 | ; |
| 205 | |
| 206 | /* |
| 207 | * EGMAC block. Set this channels to reject long preambles, |
| 208 | * not send or transmit PAUSE frames, enable preamble checking, |
| 209 | * disable frame length checking, enable FCS appending, enable |
| 210 | * TX frame padding. |
| 211 | */ |
| 212 | pm3386_port_reg_write(port, 0x302, 0x100, 0x0113); |
| 213 | |
| 214 | /* |
| 215 | * Soft reset the EGMAC block. |
| 216 | */ |
| 217 | pm3386_port_reg_write(port, 0x301, 0x100, 0x8000); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 218 | pm3386_port_reg_write(port, 0x301, 0x100, 0x0000); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 219 | |
| 220 | /* |
| 221 | * Auto-sense autonegotiation status. |
| 222 | */ |
| 223 | pm3386_port_reg_write(port, 0x306, 0x100, 0x0100); |
| 224 | |
| 225 | /* |
| 226 | * Allow reception of jumbo frames. |
| 227 | */ |
| 228 | pm3386_port_reg_write(port, 0x310, 0x100, 9018); |
| 229 | |
| 230 | /* |
| 231 | * Allow transmission of jumbo frames. |
| 232 | */ |
| 233 | pm3386_port_reg_write(port, 0x336, 0x100, 9018); |
| 234 | |
| 235 | /* @@@ Should set 0x337/0x437 (RX forwarding threshold.) */ |
| 236 | |
| 237 | /* |
| 238 | * Set autonegotiation parameters to 'no PAUSE, full duplex.' |
| 239 | */ |
| 240 | pm3386_port_reg_write(port, 0x31c, 0x100, 0x0020); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * Enable and restart autonegotiation. |
| 244 | */ |
| 245 | pm3386_port_reg_write(port, 0x318, 0x100, 0x0003); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 246 | pm3386_port_reg_write(port, 0x318, 0x100, 0x0002); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void pm3386_get_mac(int port, u8 *mac) |
| 250 | { |
| 251 | u16 temp; |
| 252 | |
| 253 | temp = pm3386_port_reg_read(port, 0x308, 0x100); |
| 254 | mac[0] = temp & 0xff; |
| 255 | mac[1] = (temp >> 8) & 0xff; |
| 256 | |
| 257 | temp = pm3386_port_reg_read(port, 0x309, 0x100); |
| 258 | mac[2] = temp & 0xff; |
| 259 | mac[3] = (temp >> 8) & 0xff; |
| 260 | |
| 261 | temp = pm3386_port_reg_read(port, 0x30a, 0x100); |
| 262 | mac[4] = temp & 0xff; |
| 263 | mac[5] = (temp >> 8) & 0xff; |
| 264 | } |
| 265 | |
Lennert Buytenhek | 0b85c0e | 2005-11-23 12:49:47 +0100 | [diff] [blame] | 266 | void pm3386_set_mac(int port, u8 *mac) |
| 267 | { |
| 268 | pm3386_port_reg_write(port, 0x308, 0x100, (mac[1] << 8) | mac[0]); |
| 269 | pm3386_port_reg_write(port, 0x309, 0x100, (mac[3] << 8) | mac[2]); |
| 270 | pm3386_port_reg_write(port, 0x30a, 0x100, (mac[5] << 8) | mac[4]); |
| 271 | } |
| 272 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 273 | static u32 pm3386_get_stat(int port, u16 base) |
| 274 | { |
| 275 | u32 value; |
| 276 | |
| 277 | value = pm3386_port_reg_read(port, base, 0x100); |
| 278 | value |= pm3386_port_reg_read(port, base + 1, 0x100) << 16; |
| 279 | |
| 280 | return value; |
| 281 | } |
| 282 | |
| 283 | void pm3386_get_stats(int port, struct net_device_stats *stats) |
| 284 | { |
| 285 | /* |
| 286 | * Snapshot statistics counters. |
| 287 | */ |
| 288 | pm3386_port_reg_write(port, 0x500, 0x100, 0x0001); |
| 289 | while (pm3386_port_reg_read(port, 0x500, 0x100) & 0x0001) |
| 290 | ; |
| 291 | |
Lennert Buytenhek | 178f171 | 2005-11-23 12:49:10 +0100 | [diff] [blame] | 292 | memset(stats, 0, sizeof(*stats)); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 293 | |
| 294 | stats->rx_packets = pm3386_get_stat(port, 0x510); |
| 295 | stats->tx_packets = pm3386_get_stat(port, 0x590); |
| 296 | stats->rx_bytes = pm3386_get_stat(port, 0x514); |
| 297 | stats->tx_bytes = pm3386_get_stat(port, 0x594); |
| 298 | /* @@@ Add other stats. */ |
| 299 | } |
| 300 | |
Lennert Buytenhek | df86550 | 2005-11-23 12:49:51 +0100 | [diff] [blame] | 301 | void pm3386_set_carrier(int port, int state) |
| 302 | { |
| 303 | pm3386_port_reg_write(port, 0x703, 0x10, state ? 0x1001 : 0x0000); |
| 304 | } |
| 305 | |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 306 | int pm3386_is_link_up(int port) |
| 307 | { |
| 308 | u16 temp; |
| 309 | |
| 310 | temp = pm3386_port_reg_read(port, 0x31a, 0x100); |
| 311 | temp = pm3386_port_reg_read(port, 0x31a, 0x100); |
| 312 | |
| 313 | return !!(temp & 0x0002); |
| 314 | } |
| 315 | |
| 316 | void pm3386_enable_rx(int port) |
| 317 | { |
| 318 | u16 temp; |
| 319 | |
| 320 | temp = pm3386_port_reg_read(port, 0x303, 0x100); |
| 321 | temp |= 0x1000; |
| 322 | pm3386_port_reg_write(port, 0x303, 0x100, temp); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void pm3386_disable_rx(int port) |
| 326 | { |
| 327 | u16 temp; |
| 328 | |
| 329 | temp = pm3386_port_reg_read(port, 0x303, 0x100); |
| 330 | temp &= 0xefff; |
| 331 | pm3386_port_reg_write(port, 0x303, 0x100, temp); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void pm3386_enable_tx(int port) |
| 335 | { |
| 336 | u16 temp; |
| 337 | |
| 338 | temp = pm3386_port_reg_read(port, 0x303, 0x100); |
| 339 | temp |= 0x4000; |
| 340 | pm3386_port_reg_write(port, 0x303, 0x100, temp); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void pm3386_disable_tx(int port) |
| 344 | { |
| 345 | u16 temp; |
| 346 | |
| 347 | temp = pm3386_port_reg_read(port, 0x303, 0x100); |
| 348 | temp &= 0xbfff; |
| 349 | pm3386_port_reg_write(port, 0x303, 0x100, temp); |
Lennert Buytenhek | 15d014d | 2005-11-11 18:23:13 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | MODULE_LICENSE("GPL"); |