Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * File: drivers/net/bfin_mac.c |
| 3 | * Based on: |
| 4 | * Maintainer: |
| 5 | * Bryan Wu <bryan.wu@analog.com> |
| 6 | * |
| 7 | * Original author: |
| 8 | * Luke Yang <luke.yang@analog.com> |
| 9 | * |
| 10 | * Created: |
| 11 | * Description: |
| 12 | * |
| 13 | * Modified: |
Aidan Williams | 775919b | 2008-01-30 16:52:23 +0800 | [diff] [blame^] | 14 | * 2006-12-19 Aidan Williams, multicast hash support |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 15 | * Copyright 2004-2006 Analog Devices Inc. |
| 16 | * |
| 17 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ |
| 18 | * |
| 19 | * This program is free software ; you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU General Public License as published by |
| 21 | * the Free Software Foundation ; either version 2, or (at your option) |
| 22 | * any later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY ; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program ; see the file COPYING. |
| 31 | * If not, write to the Free Software Foundation, |
| 32 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/delay.h> |
| 41 | #include <linux/timer.h> |
| 42 | #include <linux/errno.h> |
| 43 | #include <linux/irq.h> |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/ioport.h> |
| 46 | #include <linux/crc32.h> |
| 47 | #include <linux/device.h> |
| 48 | #include <linux/spinlock.h> |
| 49 | #include <linux/ethtool.h> |
| 50 | #include <linux/mii.h> |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 51 | #include <linux/phy.h> |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 52 | #include <linux/netdevice.h> |
| 53 | #include <linux/etherdevice.h> |
| 54 | #include <linux/skbuff.h> |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 55 | #include <linux/platform_device.h> |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 56 | |
| 57 | #include <asm/dma.h> |
| 58 | #include <linux/dma-mapping.h> |
| 59 | |
| 60 | #include <asm/blackfin.h> |
| 61 | #include <asm/cacheflush.h> |
| 62 | #include <asm/portmux.h> |
| 63 | |
| 64 | #include "bfin_mac.h" |
| 65 | |
| 66 | #define DRV_NAME "bfin_mac" |
| 67 | #define DRV_VERSION "1.1" |
| 68 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" |
| 69 | #define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver" |
| 70 | |
| 71 | MODULE_AUTHOR(DRV_AUTHOR); |
| 72 | MODULE_LICENSE("GPL"); |
| 73 | MODULE_DESCRIPTION(DRV_DESC); |
| 74 | |
| 75 | #if defined(CONFIG_BFIN_MAC_USE_L1) |
| 76 | # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size) |
| 77 | # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr) |
| 78 | #else |
| 79 | # define bfin_mac_alloc(dma_handle, size) \ |
| 80 | dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL) |
| 81 | # define bfin_mac_free(dma_handle, ptr) \ |
| 82 | dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle) |
| 83 | #endif |
| 84 | |
| 85 | #define PKT_BUF_SZ 1580 |
| 86 | |
| 87 | #define MAX_TIMEOUT_CNT 500 |
| 88 | |
| 89 | /* pointers to maintain transmit list */ |
| 90 | static struct net_dma_desc_tx *tx_list_head; |
| 91 | static struct net_dma_desc_tx *tx_list_tail; |
| 92 | static struct net_dma_desc_rx *rx_list_head; |
| 93 | static struct net_dma_desc_rx *rx_list_tail; |
| 94 | static struct net_dma_desc_rx *current_rx_ptr; |
| 95 | static struct net_dma_desc_tx *current_tx_ptr; |
| 96 | static struct net_dma_desc_tx *tx_desc; |
| 97 | static struct net_dma_desc_rx *rx_desc; |
| 98 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 99 | static void bf537mac_disable(void); |
| 100 | static void bf537mac_enable(void); |
| 101 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 102 | static void desc_list_free(void) |
| 103 | { |
| 104 | struct net_dma_desc_rx *r; |
| 105 | struct net_dma_desc_tx *t; |
| 106 | int i; |
| 107 | #if !defined(CONFIG_BFIN_MAC_USE_L1) |
| 108 | dma_addr_t dma_handle = 0; |
| 109 | #endif |
| 110 | |
| 111 | if (tx_desc) { |
| 112 | t = tx_list_head; |
| 113 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { |
| 114 | if (t) { |
| 115 | if (t->skb) { |
| 116 | dev_kfree_skb(t->skb); |
| 117 | t->skb = NULL; |
| 118 | } |
| 119 | t = t->next; |
| 120 | } |
| 121 | } |
| 122 | bfin_mac_free(dma_handle, tx_desc); |
| 123 | } |
| 124 | |
| 125 | if (rx_desc) { |
| 126 | r = rx_list_head; |
| 127 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { |
| 128 | if (r) { |
| 129 | if (r->skb) { |
| 130 | dev_kfree_skb(r->skb); |
| 131 | r->skb = NULL; |
| 132 | } |
| 133 | r = r->next; |
| 134 | } |
| 135 | } |
| 136 | bfin_mac_free(dma_handle, rx_desc); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int desc_list_init(void) |
| 141 | { |
| 142 | int i; |
| 143 | struct sk_buff *new_skb; |
| 144 | #if !defined(CONFIG_BFIN_MAC_USE_L1) |
| 145 | /* |
| 146 | * This dma_handle is useless in Blackfin dma_alloc_coherent(). |
| 147 | * The real dma handler is the return value of dma_alloc_coherent(). |
| 148 | */ |
| 149 | dma_addr_t dma_handle; |
| 150 | #endif |
| 151 | |
| 152 | tx_desc = bfin_mac_alloc(&dma_handle, |
| 153 | sizeof(struct net_dma_desc_tx) * |
| 154 | CONFIG_BFIN_TX_DESC_NUM); |
| 155 | if (tx_desc == NULL) |
| 156 | goto init_error; |
| 157 | |
| 158 | rx_desc = bfin_mac_alloc(&dma_handle, |
| 159 | sizeof(struct net_dma_desc_rx) * |
| 160 | CONFIG_BFIN_RX_DESC_NUM); |
| 161 | if (rx_desc == NULL) |
| 162 | goto init_error; |
| 163 | |
| 164 | /* init tx_list */ |
| 165 | tx_list_head = tx_list_tail = tx_desc; |
| 166 | |
| 167 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { |
| 168 | struct net_dma_desc_tx *t = tx_desc + i; |
| 169 | struct dma_descriptor *a = &(t->desc_a); |
| 170 | struct dma_descriptor *b = &(t->desc_b); |
| 171 | |
| 172 | /* |
| 173 | * disable DMA |
| 174 | * read from memory WNR = 0 |
| 175 | * wordsize is 32 bits |
| 176 | * 6 half words is desc size |
| 177 | * large desc flow |
| 178 | */ |
| 179 | a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; |
| 180 | a->start_addr = (unsigned long)t->packet; |
| 181 | a->x_count = 0; |
| 182 | a->next_dma_desc = b; |
| 183 | |
| 184 | /* |
| 185 | * enabled DMA |
| 186 | * write to memory WNR = 1 |
| 187 | * wordsize is 32 bits |
| 188 | * disable interrupt |
| 189 | * 6 half words is desc size |
| 190 | * large desc flow |
| 191 | */ |
| 192 | b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; |
| 193 | b->start_addr = (unsigned long)(&(t->status)); |
| 194 | b->x_count = 0; |
| 195 | |
| 196 | t->skb = NULL; |
| 197 | tx_list_tail->desc_b.next_dma_desc = a; |
| 198 | tx_list_tail->next = t; |
| 199 | tx_list_tail = t; |
| 200 | } |
| 201 | tx_list_tail->next = tx_list_head; /* tx_list is a circle */ |
| 202 | tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a); |
| 203 | current_tx_ptr = tx_list_head; |
| 204 | |
| 205 | /* init rx_list */ |
| 206 | rx_list_head = rx_list_tail = rx_desc; |
| 207 | |
| 208 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { |
| 209 | struct net_dma_desc_rx *r = rx_desc + i; |
| 210 | struct dma_descriptor *a = &(r->desc_a); |
| 211 | struct dma_descriptor *b = &(r->desc_b); |
| 212 | |
| 213 | /* allocate a new skb for next time receive */ |
| 214 | new_skb = dev_alloc_skb(PKT_BUF_SZ + 2); |
| 215 | if (!new_skb) { |
| 216 | printk(KERN_NOTICE DRV_NAME |
| 217 | ": init: low on mem - packet dropped\n"); |
| 218 | goto init_error; |
| 219 | } |
| 220 | skb_reserve(new_skb, 2); |
| 221 | r->skb = new_skb; |
| 222 | |
| 223 | /* |
| 224 | * enabled DMA |
| 225 | * write to memory WNR = 1 |
| 226 | * wordsize is 32 bits |
| 227 | * disable interrupt |
| 228 | * 6 half words is desc size |
| 229 | * large desc flow |
| 230 | */ |
| 231 | a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; |
| 232 | /* since RXDWA is enabled */ |
| 233 | a->start_addr = (unsigned long)new_skb->data - 2; |
| 234 | a->x_count = 0; |
| 235 | a->next_dma_desc = b; |
| 236 | |
| 237 | /* |
| 238 | * enabled DMA |
| 239 | * write to memory WNR = 1 |
| 240 | * wordsize is 32 bits |
| 241 | * enable interrupt |
| 242 | * 6 half words is desc size |
| 243 | * large desc flow |
| 244 | */ |
| 245 | b->config = DMAEN | WNR | WDSIZE_32 | DI_EN | |
| 246 | NDSIZE_6 | DMAFLOW_LARGE; |
| 247 | b->start_addr = (unsigned long)(&(r->status)); |
| 248 | b->x_count = 0; |
| 249 | |
| 250 | rx_list_tail->desc_b.next_dma_desc = a; |
| 251 | rx_list_tail->next = r; |
| 252 | rx_list_tail = r; |
| 253 | } |
| 254 | rx_list_tail->next = rx_list_head; /* rx_list is a circle */ |
| 255 | rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a); |
| 256 | current_rx_ptr = rx_list_head; |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | init_error: |
| 261 | desc_list_free(); |
| 262 | printk(KERN_ERR DRV_NAME ": kmalloc failed\n"); |
| 263 | return -ENOMEM; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/ |
| 268 | |
| 269 | /* Set FER regs to MUX in Ethernet pins */ |
| 270 | static int setup_pin_mux(int action) |
| 271 | { |
| 272 | #if defined(CONFIG_BFIN_MAC_RMII) |
| 273 | u16 pin_req[] = P_RMII0; |
| 274 | #else |
| 275 | u16 pin_req[] = P_MII0; |
| 276 | #endif |
| 277 | |
| 278 | if (action) { |
| 279 | if (peripheral_request_list(pin_req, DRV_NAME)) { |
| 280 | printk(KERN_ERR DRV_NAME |
| 281 | ": Requesting Peripherals failed\n"); |
| 282 | return -EFAULT; |
| 283 | } |
| 284 | } else |
| 285 | peripheral_free_list(pin_req); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 290 | /* |
| 291 | * MII operations |
| 292 | */ |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 293 | /* Wait until the previous MDC/MDIO transaction has completed */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 294 | static void mdio_poll(void) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 295 | { |
| 296 | int timeout_cnt = MAX_TIMEOUT_CNT; |
| 297 | |
| 298 | /* poll the STABUSY bit */ |
| 299 | while ((bfin_read_EMAC_STAADD()) & STABUSY) { |
Bryan Wu | 6db9e46 | 2008-01-30 16:52:21 +0800 | [diff] [blame] | 300 | udelay(1); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 301 | if (timeout_cnt-- < 0) { |
| 302 | printk(KERN_ERR DRV_NAME |
| 303 | ": wait MDC/MDIO transaction to complete timeout\n"); |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* Read an off-chip register in a PHY through the MDC/MDIO port */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 310 | static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 311 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 312 | mdio_poll(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 313 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 314 | /* read mode */ |
| 315 | bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) | |
| 316 | SET_REGAD((u16) regnum) | |
| 317 | STABUSY); |
| 318 | |
| 319 | mdio_poll(); |
| 320 | |
| 321 | return (int) bfin_read_EMAC_STADAT(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Write an off-chip register in a PHY through the MDC/MDIO port */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 325 | static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, |
| 326 | u16 value) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 327 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 328 | mdio_poll(); |
| 329 | |
| 330 | bfin_write_EMAC_STADAT((u32) value); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 331 | |
| 332 | /* write mode */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 333 | bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) | |
| 334 | SET_REGAD((u16) regnum) | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 335 | STAOP | |
| 336 | STABUSY); |
| 337 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 338 | mdio_poll(); |
| 339 | |
| 340 | return 0; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 343 | static int mdiobus_reset(struct mii_bus *bus) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 344 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 345 | return 0; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 348 | static void bf537_adjust_link(struct net_device *dev) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 349 | { |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 350 | struct bf537mac_local *lp = netdev_priv(dev); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 351 | struct phy_device *phydev = lp->phydev; |
| 352 | unsigned long flags; |
| 353 | int new_state = 0; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 354 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 355 | spin_lock_irqsave(&lp->lock, flags); |
| 356 | if (phydev->link) { |
| 357 | /* Now we make sure that we can be in full duplex mode. |
| 358 | * If not, we operate in half-duplex mode. */ |
| 359 | if (phydev->duplex != lp->old_duplex) { |
| 360 | u32 opmode = bfin_read_EMAC_OPMODE(); |
| 361 | new_state = 1; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 362 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 363 | if (phydev->duplex) |
| 364 | opmode |= FDMODE; |
| 365 | else |
| 366 | opmode &= ~(FDMODE); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 367 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 368 | bfin_write_EMAC_OPMODE(opmode); |
| 369 | lp->old_duplex = phydev->duplex; |
| 370 | } |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 371 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 372 | if (phydev->speed != lp->old_speed) { |
| 373 | #if defined(CONFIG_BFIN_MAC_RMII) |
| 374 | u32 opmode = bfin_read_EMAC_OPMODE(); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 375 | switch (phydev->speed) { |
| 376 | case 10: |
| 377 | opmode |= RMII_10; |
| 378 | break; |
| 379 | case 100: |
| 380 | opmode &= ~(RMII_10); |
| 381 | break; |
| 382 | default: |
| 383 | printk(KERN_WARNING |
| 384 | "%s: Ack! Speed (%d) is not 10/100!\n", |
| 385 | DRV_NAME, phydev->speed); |
| 386 | break; |
| 387 | } |
| 388 | bfin_write_EMAC_OPMODE(opmode); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 389 | #endif |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 390 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 391 | new_state = 1; |
| 392 | lp->old_speed = phydev->speed; |
| 393 | } |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 394 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 395 | if (!lp->old_link) { |
| 396 | new_state = 1; |
| 397 | lp->old_link = 1; |
| 398 | netif_schedule(dev); |
| 399 | } |
| 400 | } else if (lp->old_link) { |
| 401 | new_state = 1; |
| 402 | lp->old_link = 0; |
| 403 | lp->old_speed = 0; |
| 404 | lp->old_duplex = -1; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 405 | } |
| 406 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 407 | if (new_state) { |
| 408 | u32 opmode = bfin_read_EMAC_OPMODE(); |
| 409 | phy_print_status(phydev); |
| 410 | pr_debug("EMAC_OPMODE = 0x%08x\n", opmode); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 411 | } |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 412 | |
| 413 | spin_unlock_irqrestore(&lp->lock, flags); |
| 414 | } |
| 415 | |
Bryan Wu | 7cc8f38 | 2008-01-30 16:52:22 +0800 | [diff] [blame] | 416 | /* MDC = 2.5 MHz */ |
| 417 | #define MDC_CLK 2500000 |
| 418 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 419 | static int mii_probe(struct net_device *dev) |
| 420 | { |
| 421 | struct bf537mac_local *lp = netdev_priv(dev); |
| 422 | struct phy_device *phydev = NULL; |
| 423 | unsigned short sysctl; |
| 424 | int i; |
Bryan Wu | 7cc8f38 | 2008-01-30 16:52:22 +0800 | [diff] [blame] | 425 | u32 sclk, mdc_div; |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 426 | |
| 427 | /* Enable PHY output early */ |
| 428 | if (!(bfin_read_VR_CTL() & PHYCLKOE)) |
| 429 | bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE); |
| 430 | |
Bryan Wu | 7cc8f38 | 2008-01-30 16:52:22 +0800 | [diff] [blame] | 431 | sclk = get_sclk(); |
| 432 | mdc_div = ((sclk / MDC_CLK) / 2) - 1; |
| 433 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 434 | sysctl = bfin_read_EMAC_SYSCTL(); |
Bryan Wu | 7cc8f38 | 2008-01-30 16:52:22 +0800 | [diff] [blame] | 435 | sysctl |= SET_MDCDIV(mdc_div); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 436 | bfin_write_EMAC_SYSCTL(sysctl); |
| 437 | |
| 438 | /* search for connect PHY device */ |
| 439 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
| 440 | struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i]; |
| 441 | |
| 442 | if (!tmp_phydev) |
| 443 | continue; /* no PHY here... */ |
| 444 | |
| 445 | phydev = tmp_phydev; |
| 446 | break; /* found it */ |
| 447 | } |
| 448 | |
| 449 | /* now we are supposed to have a proper phydev, to attach to... */ |
| 450 | if (!phydev) { |
| 451 | printk(KERN_INFO "%s: Don't found any phy device at all\n", |
| 452 | dev->name); |
| 453 | return -ENODEV; |
| 454 | } |
| 455 | |
| 456 | #if defined(CONFIG_BFIN_MAC_RMII) |
| 457 | phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0, |
| 458 | PHY_INTERFACE_MODE_RMII); |
| 459 | #else |
| 460 | phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0, |
| 461 | PHY_INTERFACE_MODE_MII); |
| 462 | #endif |
| 463 | |
| 464 | if (IS_ERR(phydev)) { |
| 465 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); |
| 466 | return PTR_ERR(phydev); |
| 467 | } |
| 468 | |
| 469 | /* mask with MAC supported features */ |
| 470 | phydev->supported &= (SUPPORTED_10baseT_Half |
| 471 | | SUPPORTED_10baseT_Full |
| 472 | | SUPPORTED_100baseT_Half |
| 473 | | SUPPORTED_100baseT_Full |
| 474 | | SUPPORTED_Autoneg |
| 475 | | SUPPORTED_Pause | SUPPORTED_Asym_Pause |
| 476 | | SUPPORTED_MII |
| 477 | | SUPPORTED_TP); |
| 478 | |
| 479 | phydev->advertising = phydev->supported; |
| 480 | |
| 481 | lp->old_link = 0; |
| 482 | lp->old_speed = 0; |
| 483 | lp->old_duplex = -1; |
| 484 | lp->phydev = phydev; |
| 485 | |
| 486 | printk(KERN_INFO "%s: attached PHY driver [%s] " |
Bryan Wu | 7cc8f38 | 2008-01-30 16:52:22 +0800 | [diff] [blame] | 487 | "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)" |
| 488 | "@sclk=%dMHz)\n", |
| 489 | DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq, |
| 490 | MDC_CLK, mdc_div, sclk/1000000); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 491 | |
| 492 | return 0; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /**************************************************************************/ |
| 496 | void setup_system_regs(struct net_device *dev) |
| 497 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 498 | unsigned short sysctl; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 499 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 500 | /* |
| 501 | * Odd word alignment for Receive Frame DMA word |
| 502 | * Configure checksum support and rcve frame word alignment |
| 503 | */ |
| 504 | sysctl = bfin_read_EMAC_SYSCTL(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 505 | #if defined(BFIN_MAC_CSUM_OFFLOAD) |
| 506 | sysctl |= RXDWA | RXCKS; |
| 507 | #else |
| 508 | sysctl |= RXDWA; |
| 509 | #endif |
| 510 | bfin_write_EMAC_SYSCTL(sysctl); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 511 | |
| 512 | bfin_write_EMAC_MMC_CTL(RSTC | CROLL); |
| 513 | |
| 514 | /* Initialize the TX DMA channel registers */ |
| 515 | bfin_write_DMA2_X_COUNT(0); |
| 516 | bfin_write_DMA2_X_MODIFY(4); |
| 517 | bfin_write_DMA2_Y_COUNT(0); |
| 518 | bfin_write_DMA2_Y_MODIFY(0); |
| 519 | |
| 520 | /* Initialize the RX DMA channel registers */ |
| 521 | bfin_write_DMA1_X_COUNT(0); |
| 522 | bfin_write_DMA1_X_MODIFY(4); |
| 523 | bfin_write_DMA1_Y_COUNT(0); |
| 524 | bfin_write_DMA1_Y_MODIFY(0); |
| 525 | } |
| 526 | |
Alex Landau | 73f8318 | 2007-09-19 23:14:18 +0800 | [diff] [blame] | 527 | static void setup_mac_addr(u8 *mac_addr) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 528 | { |
| 529 | u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]); |
| 530 | u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]); |
| 531 | |
| 532 | /* this depends on a little-endian machine */ |
| 533 | bfin_write_EMAC_ADDRLO(addr_low); |
| 534 | bfin_write_EMAC_ADDRHI(addr_hi); |
| 535 | } |
| 536 | |
Alex Landau | 73f8318 | 2007-09-19 23:14:18 +0800 | [diff] [blame] | 537 | static int bf537mac_set_mac_address(struct net_device *dev, void *p) |
| 538 | { |
| 539 | struct sockaddr *addr = p; |
| 540 | if (netif_running(dev)) |
| 541 | return -EBUSY; |
| 542 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 543 | setup_mac_addr(dev->dev_addr); |
| 544 | return 0; |
| 545 | } |
| 546 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 547 | static void adjust_tx_list(void) |
| 548 | { |
| 549 | int timeout_cnt = MAX_TIMEOUT_CNT; |
| 550 | |
| 551 | if (tx_list_head->status.status_word != 0 |
| 552 | && current_tx_ptr != tx_list_head) { |
| 553 | goto adjust_head; /* released something, just return; */ |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * if nothing released, check wait condition |
| 558 | * current's next can not be the head, |
| 559 | * otherwise the dma will not stop as we want |
| 560 | */ |
| 561 | if (current_tx_ptr->next->next == tx_list_head) { |
| 562 | while (tx_list_head->status.status_word == 0) { |
Bryan Wu | 6db9e46 | 2008-01-30 16:52:21 +0800 | [diff] [blame] | 563 | mdelay(1); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 564 | if (tx_list_head->status.status_word != 0 |
| 565 | || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) { |
| 566 | goto adjust_head; |
| 567 | } |
| 568 | if (timeout_cnt-- < 0) { |
| 569 | printk(KERN_ERR DRV_NAME |
| 570 | ": wait for adjust tx list head timeout\n"); |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | if (tx_list_head->status.status_word != 0) { |
| 575 | goto adjust_head; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return; |
| 580 | |
| 581 | adjust_head: |
| 582 | do { |
| 583 | tx_list_head->desc_a.config &= ~DMAEN; |
| 584 | tx_list_head->status.status_word = 0; |
| 585 | if (tx_list_head->skb) { |
| 586 | dev_kfree_skb(tx_list_head->skb); |
| 587 | tx_list_head->skb = NULL; |
| 588 | } else { |
| 589 | printk(KERN_ERR DRV_NAME |
| 590 | ": no sk_buff in a transmitted frame!\n"); |
| 591 | } |
| 592 | tx_list_head = tx_list_head->next; |
| 593 | } while (tx_list_head->status.status_word != 0 |
| 594 | && current_tx_ptr != tx_list_head); |
| 595 | return; |
| 596 | |
| 597 | } |
| 598 | |
| 599 | static int bf537mac_hard_start_xmit(struct sk_buff *skb, |
| 600 | struct net_device *dev) |
| 601 | { |
| 602 | struct bf537mac_local *lp = netdev_priv(dev); |
| 603 | unsigned int data; |
| 604 | |
| 605 | current_tx_ptr->skb = skb; |
| 606 | |
| 607 | /* |
| 608 | * Is skb->data always 16-bit aligned? |
| 609 | * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)? |
| 610 | */ |
| 611 | if ((((unsigned int)(skb->data)) & 0x02) == 2) { |
| 612 | /* move skb->data to current_tx_ptr payload */ |
| 613 | data = (unsigned int)(skb->data) - 2; |
| 614 | *((unsigned short *)data) = (unsigned short)(skb->len); |
| 615 | current_tx_ptr->desc_a.start_addr = (unsigned long)data; |
| 616 | /* this is important! */ |
| 617 | blackfin_dcache_flush_range(data, (data + (skb->len)) + 2); |
| 618 | |
| 619 | } else { |
| 620 | *((unsigned short *)(current_tx_ptr->packet)) = |
| 621 | (unsigned short)(skb->len); |
| 622 | memcpy((char *)(current_tx_ptr->packet + 2), skb->data, |
| 623 | (skb->len)); |
| 624 | current_tx_ptr->desc_a.start_addr = |
| 625 | (unsigned long)current_tx_ptr->packet; |
| 626 | if (current_tx_ptr->status.status_word != 0) |
| 627 | current_tx_ptr->status.status_word = 0; |
| 628 | blackfin_dcache_flush_range((unsigned int)current_tx_ptr-> |
| 629 | packet, |
| 630 | (unsigned int)(current_tx_ptr-> |
| 631 | packet + skb->len) + |
| 632 | 2); |
| 633 | } |
| 634 | |
| 635 | /* enable this packet's dma */ |
| 636 | current_tx_ptr->desc_a.config |= DMAEN; |
| 637 | |
| 638 | /* tx dma is running, just return */ |
| 639 | if (bfin_read_DMA2_IRQ_STATUS() & 0x08) |
| 640 | goto out; |
| 641 | |
| 642 | /* tx dma is not running */ |
| 643 | bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a)); |
| 644 | /* dma enabled, read from memory, size is 6 */ |
| 645 | bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config); |
| 646 | /* Turn on the EMAC tx */ |
| 647 | bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE); |
| 648 | |
| 649 | out: |
| 650 | adjust_tx_list(); |
| 651 | current_tx_ptr = current_tx_ptr->next; |
| 652 | dev->trans_start = jiffies; |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 653 | dev->stats.tx_packets++; |
| 654 | dev->stats.tx_bytes += (skb->len); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static void bf537mac_rx(struct net_device *dev) |
| 659 | { |
| 660 | struct sk_buff *skb, *new_skb; |
| 661 | struct bf537mac_local *lp = netdev_priv(dev); |
| 662 | unsigned short len; |
| 663 | |
| 664 | /* allocate a new skb for next time receive */ |
| 665 | skb = current_rx_ptr->skb; |
| 666 | new_skb = dev_alloc_skb(PKT_BUF_SZ + 2); |
| 667 | if (!new_skb) { |
| 668 | printk(KERN_NOTICE DRV_NAME |
| 669 | ": rx: low on mem - packet dropped\n"); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 670 | dev->stats.rx_dropped++; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 671 | goto out; |
| 672 | } |
| 673 | /* reserve 2 bytes for RXDWA padding */ |
| 674 | skb_reserve(new_skb, 2); |
| 675 | current_rx_ptr->skb = new_skb; |
| 676 | current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2; |
| 677 | |
| 678 | len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN); |
| 679 | skb_put(skb, len); |
| 680 | blackfin_dcache_invalidate_range((unsigned long)skb->head, |
| 681 | (unsigned long)skb->tail); |
| 682 | |
| 683 | dev->last_rx = jiffies; |
| 684 | skb->dev = dev; |
| 685 | skb->protocol = eth_type_trans(skb, dev); |
| 686 | #if defined(BFIN_MAC_CSUM_OFFLOAD) |
| 687 | skb->csum = current_rx_ptr->status.ip_payload_csum; |
Vitja Makarov | 00ff49a | 2007-11-23 17:55:51 +0800 | [diff] [blame] | 688 | skb->ip_summed = CHECKSUM_COMPLETE; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 689 | #endif |
| 690 | |
| 691 | netif_rx(skb); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 692 | dev->stats.rx_packets++; |
| 693 | dev->stats.rx_bytes += len; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 694 | current_rx_ptr->status.status_word = 0x00000000; |
| 695 | current_rx_ptr = current_rx_ptr->next; |
| 696 | |
| 697 | out: |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | /* interrupt routine to handle rx and error signal */ |
| 702 | static irqreturn_t bf537mac_interrupt(int irq, void *dev_id) |
| 703 | { |
| 704 | struct net_device *dev = dev_id; |
| 705 | int number = 0; |
| 706 | |
| 707 | get_one_packet: |
| 708 | if (current_rx_ptr->status.status_word == 0) { |
| 709 | /* no more new packet received */ |
| 710 | if (number == 0) { |
| 711 | if (current_rx_ptr->next->status.status_word != 0) { |
| 712 | current_rx_ptr = current_rx_ptr->next; |
| 713 | goto real_rx; |
| 714 | } |
| 715 | } |
| 716 | bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() | |
| 717 | DMA_DONE | DMA_ERR); |
| 718 | return IRQ_HANDLED; |
| 719 | } |
| 720 | |
| 721 | real_rx: |
| 722 | bf537mac_rx(dev); |
| 723 | number++; |
| 724 | goto get_one_packet; |
| 725 | } |
| 726 | |
| 727 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 728 | static void bf537mac_poll(struct net_device *dev) |
| 729 | { |
| 730 | disable_irq(IRQ_MAC_RX); |
| 731 | bf537mac_interrupt(IRQ_MAC_RX, dev); |
| 732 | enable_irq(IRQ_MAC_RX); |
| 733 | } |
| 734 | #endif /* CONFIG_NET_POLL_CONTROLLER */ |
| 735 | |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 736 | static void bf537mac_disable(void) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 737 | { |
| 738 | unsigned int opmode; |
| 739 | |
| 740 | opmode = bfin_read_EMAC_OPMODE(); |
| 741 | opmode &= (~RE); |
| 742 | opmode &= (~TE); |
| 743 | /* Turn off the EMAC */ |
| 744 | bfin_write_EMAC_OPMODE(opmode); |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Enable Interrupts, Receive, and Transmit |
| 749 | */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 750 | static void bf537mac_enable(void) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 751 | { |
| 752 | u32 opmode; |
| 753 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 754 | pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 755 | |
| 756 | /* Set RX DMA */ |
| 757 | bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a)); |
| 758 | bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config); |
| 759 | |
| 760 | /* Wait MII done */ |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 761 | mdio_poll(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 762 | |
| 763 | /* We enable only RX here */ |
| 764 | /* ASTP : Enable Automatic Pad Stripping |
| 765 | PR : Promiscuous Mode for test |
| 766 | PSF : Receive frames with total length less than 64 bytes. |
| 767 | FDMODE : Full Duplex Mode |
| 768 | LB : Internal Loopback for test |
| 769 | RE : Receiver Enable */ |
| 770 | opmode = bfin_read_EMAC_OPMODE(); |
| 771 | if (opmode & FDMODE) |
| 772 | opmode |= PSF; |
| 773 | else |
| 774 | opmode |= DRO | DC | PSF; |
| 775 | opmode |= RE; |
| 776 | |
| 777 | #if defined(CONFIG_BFIN_MAC_RMII) |
| 778 | opmode |= RMII; /* For Now only 100MBit are supported */ |
| 779 | #ifdef CONFIG_BF_REV_0_2 |
| 780 | opmode |= TE; |
| 781 | #endif |
| 782 | #endif |
| 783 | /* Turn on the EMAC rx */ |
| 784 | bfin_write_EMAC_OPMODE(opmode); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* Our watchdog timed out. Called by the networking layer */ |
| 788 | static void bf537mac_timeout(struct net_device *dev) |
| 789 | { |
| 790 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); |
| 791 | |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 792 | bf537mac_disable(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 793 | |
| 794 | /* reset tx queue */ |
| 795 | tx_list_tail = tx_list_head->next; |
| 796 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 797 | bf537mac_enable(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 798 | |
| 799 | /* We can accept TX packets again */ |
| 800 | dev->trans_start = jiffies; |
| 801 | netif_wake_queue(dev); |
| 802 | } |
| 803 | |
Aidan Williams | 775919b | 2008-01-30 16:52:23 +0800 | [diff] [blame^] | 804 | static void bf537mac_multicast_hash(struct net_device *dev) |
| 805 | { |
| 806 | u32 emac_hashhi, emac_hashlo; |
| 807 | struct dev_mc_list *dmi = dev->mc_list; |
| 808 | char *addrs; |
| 809 | int i; |
| 810 | u32 crc; |
| 811 | |
| 812 | emac_hashhi = emac_hashlo = 0; |
| 813 | |
| 814 | for (i = 0; i < dev->mc_count; i++) { |
| 815 | addrs = dmi->dmi_addr; |
| 816 | dmi = dmi->next; |
| 817 | |
| 818 | /* skip non-multicast addresses */ |
| 819 | if (!(*addrs & 1)) |
| 820 | continue; |
| 821 | |
| 822 | crc = ether_crc(ETH_ALEN, addrs); |
| 823 | crc >>= 26; |
| 824 | |
| 825 | if (crc & 0x20) |
| 826 | emac_hashhi |= 1 << (crc & 0x1f); |
| 827 | else |
| 828 | emac_hashlo |= 1 << (crc & 0x1f); |
| 829 | } |
| 830 | |
| 831 | bfin_write_EMAC_HASHHI(emac_hashhi); |
| 832 | bfin_write_EMAC_HASHLO(emac_hashlo); |
| 833 | |
| 834 | return; |
| 835 | } |
| 836 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 837 | /* |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 838 | * This routine will, depending on the values passed to it, |
| 839 | * either make it accept multicast packets, go into |
| 840 | * promiscuous mode (for TCPDUMP and cousins) or accept |
| 841 | * a select set of multicast packets |
| 842 | */ |
| 843 | static void bf537mac_set_multicast_list(struct net_device *dev) |
| 844 | { |
| 845 | u32 sysctl; |
| 846 | |
| 847 | if (dev->flags & IFF_PROMISC) { |
| 848 | printk(KERN_INFO "%s: set to promisc mode\n", dev->name); |
| 849 | sysctl = bfin_read_EMAC_OPMODE(); |
| 850 | sysctl |= RAF; |
| 851 | bfin_write_EMAC_OPMODE(sysctl); |
Aidan Williams | 775919b | 2008-01-30 16:52:23 +0800 | [diff] [blame^] | 852 | } else if (dev->flags & IFF_ALLMULTI) { |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 853 | /* accept all multicast */ |
| 854 | sysctl = bfin_read_EMAC_OPMODE(); |
| 855 | sysctl |= PAM; |
| 856 | bfin_write_EMAC_OPMODE(sysctl); |
Aidan Williams | 775919b | 2008-01-30 16:52:23 +0800 | [diff] [blame^] | 857 | } else if (dev->mc_count) { |
| 858 | /* set up multicast hash table */ |
| 859 | sysctl = bfin_read_EMAC_OPMODE(); |
| 860 | sysctl |= HM; |
| 861 | bfin_write_EMAC_OPMODE(sysctl); |
| 862 | bf537mac_multicast_hash(dev); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 863 | } else { |
| 864 | /* clear promisc or multicast mode */ |
| 865 | sysctl = bfin_read_EMAC_OPMODE(); |
| 866 | sysctl &= ~(RAF | PAM); |
| 867 | bfin_write_EMAC_OPMODE(sysctl); |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * this puts the device in an inactive state |
| 873 | */ |
| 874 | static void bf537mac_shutdown(struct net_device *dev) |
| 875 | { |
| 876 | /* Turn off the EMAC */ |
| 877 | bfin_write_EMAC_OPMODE(0x00000000); |
| 878 | /* Turn off the EMAC RX DMA */ |
| 879 | bfin_write_DMA1_CONFIG(0x0000); |
| 880 | bfin_write_DMA2_CONFIG(0x0000); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Open and Initialize the interface |
| 885 | * |
| 886 | * Set up everything, reset the card, etc.. |
| 887 | */ |
| 888 | static int bf537mac_open(struct net_device *dev) |
| 889 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 890 | struct bf537mac_local *lp = netdev_priv(dev); |
Michael Hennerich | 4af4b84 | 2007-07-25 14:09:54 +0800 | [diff] [blame] | 891 | int retval; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 892 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); |
| 893 | |
| 894 | /* |
| 895 | * Check that the address is valid. If its not, refuse |
| 896 | * to bring the device up. The user must specify an |
| 897 | * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx |
| 898 | */ |
| 899 | if (!is_valid_ether_addr(dev->dev_addr)) { |
| 900 | printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n"); |
| 901 | return -EINVAL; |
| 902 | } |
| 903 | |
| 904 | /* initial rx and tx list */ |
Michael Hennerich | 4af4b84 | 2007-07-25 14:09:54 +0800 | [diff] [blame] | 905 | retval = desc_list_init(); |
| 906 | |
| 907 | if (retval) |
| 908 | return retval; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 909 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 910 | phy_start(lp->phydev); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 911 | setup_system_regs(dev); |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 912 | bf537mac_disable(); |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 913 | bf537mac_enable(); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 914 | |
| 915 | pr_debug("hardware init finished\n"); |
| 916 | netif_start_queue(dev); |
| 917 | netif_carrier_on(dev); |
| 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * |
| 924 | * this makes the board clean up everything that it can |
| 925 | * and not talk to the outside world. Caused by |
| 926 | * an 'ifconfig ethX down' |
| 927 | */ |
| 928 | static int bf537mac_close(struct net_device *dev) |
| 929 | { |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 930 | struct bf537mac_local *lp = netdev_priv(dev); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 931 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); |
| 932 | |
| 933 | netif_stop_queue(dev); |
| 934 | netif_carrier_off(dev); |
| 935 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 936 | phy_stop(lp->phydev); |
| 937 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 938 | /* clear everything */ |
| 939 | bf537mac_shutdown(dev); |
| 940 | |
| 941 | /* free the rx/tx buffers */ |
| 942 | desc_list_free(); |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static int __init bf537mac_probe(struct net_device *dev) |
| 948 | { |
| 949 | struct bf537mac_local *lp = netdev_priv(dev); |
| 950 | int retval; |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 951 | int i; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 952 | |
| 953 | /* Grab the MAC address in the MAC */ |
| 954 | *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO()); |
| 955 | *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI()); |
| 956 | |
| 957 | /* probe mac */ |
| 958 | /*todo: how to proble? which is revision_register */ |
| 959 | bfin_write_EMAC_ADDRLO(0x12345678); |
| 960 | if (bfin_read_EMAC_ADDRLO() != 0x12345678) { |
| 961 | pr_debug("can't detect bf537 mac!\n"); |
| 962 | retval = -ENODEV; |
| 963 | goto err_out; |
| 964 | } |
| 965 | |
| 966 | /* set the GPIO pins to Ethernet mode */ |
| 967 | retval = setup_pin_mux(1); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 968 | if (retval) |
| 969 | return retval; |
| 970 | |
| 971 | /*Is it valid? (Did bootloader initialize it?) */ |
| 972 | if (!is_valid_ether_addr(dev->dev_addr)) { |
| 973 | /* Grab the MAC from the board somehow - this is done in the |
| 974 | arch/blackfin/mach-bf537/boards/eth_mac.c */ |
Mike Frysinger | 9862cc5 | 2007-11-15 21:21:20 +0800 | [diff] [blame] | 975 | bfin_get_ether_addr(dev->dev_addr); |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* If still not valid, get a random one */ |
| 979 | if (!is_valid_ether_addr(dev->dev_addr)) { |
| 980 | random_ether_addr(dev->dev_addr); |
| 981 | } |
| 982 | |
| 983 | setup_mac_addr(dev->dev_addr); |
| 984 | |
Bryan Wu | 4ae5a3a | 2007-09-19 23:37:36 +0800 | [diff] [blame] | 985 | /* MDIO bus initial */ |
| 986 | lp->mii_bus.priv = dev; |
| 987 | lp->mii_bus.read = mdiobus_read; |
| 988 | lp->mii_bus.write = mdiobus_write; |
| 989 | lp->mii_bus.reset = mdiobus_reset; |
| 990 | lp->mii_bus.name = "bfin_mac_mdio"; |
| 991 | lp->mii_bus.id = 0; |
| 992 | lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 993 | for (i = 0; i < PHY_MAX_ADDR; ++i) |
| 994 | lp->mii_bus.irq[i] = PHY_POLL; |
| 995 | |
| 996 | mdiobus_register(&lp->mii_bus); |
| 997 | |
| 998 | retval = mii_probe(dev); |
| 999 | if (retval) |
| 1000 | return retval; |
| 1001 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1002 | /* Fill in the fields of the device structure with ethernet values. */ |
| 1003 | ether_setup(dev); |
| 1004 | |
| 1005 | dev->open = bf537mac_open; |
| 1006 | dev->stop = bf537mac_close; |
| 1007 | dev->hard_start_xmit = bf537mac_hard_start_xmit; |
Alex Landau | 73f8318 | 2007-09-19 23:14:18 +0800 | [diff] [blame] | 1008 | dev->set_mac_address = bf537mac_set_mac_address; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1009 | dev->tx_timeout = bf537mac_timeout; |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1010 | dev->set_multicast_list = bf537mac_set_multicast_list; |
| 1011 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1012 | dev->poll_controller = bf537mac_poll; |
| 1013 | #endif |
| 1014 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1015 | spin_lock_init(&lp->lock); |
| 1016 | |
| 1017 | /* now, enable interrupts */ |
| 1018 | /* register irq handler */ |
| 1019 | if (request_irq |
| 1020 | (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED, |
| 1021 | "BFIN537_MAC_RX", dev)) { |
| 1022 | printk(KERN_WARNING DRV_NAME |
| 1023 | ": Unable to attach BlackFin MAC RX interrupt\n"); |
| 1024 | return -EBUSY; |
| 1025 | } |
| 1026 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1027 | |
| 1028 | retval = register_netdev(dev); |
| 1029 | if (retval == 0) { |
| 1030 | /* now, print out the card info, in a short format.. */ |
| 1031 | printk(KERN_INFO "%s: Version %s, %s\n", |
| 1032 | DRV_NAME, DRV_VERSION, DRV_DESC); |
| 1033 | } |
| 1034 | |
| 1035 | err_out: |
| 1036 | return retval; |
| 1037 | } |
| 1038 | |
| 1039 | static int bfin_mac_probe(struct platform_device *pdev) |
| 1040 | { |
| 1041 | struct net_device *ndev; |
| 1042 | |
| 1043 | ndev = alloc_etherdev(sizeof(struct bf537mac_local)); |
| 1044 | if (!ndev) { |
| 1045 | printk(KERN_WARNING DRV_NAME ": could not allocate device\n"); |
| 1046 | return -ENOMEM; |
| 1047 | } |
| 1048 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1049 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 1050 | |
| 1051 | platform_set_drvdata(pdev, ndev); |
| 1052 | |
| 1053 | if (bf537mac_probe(ndev) != 0) { |
| 1054 | platform_set_drvdata(pdev, NULL); |
| 1055 | free_netdev(ndev); |
| 1056 | printk(KERN_WARNING DRV_NAME ": not found\n"); |
| 1057 | return -ENODEV; |
| 1058 | } |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static int bfin_mac_remove(struct platform_device *pdev) |
| 1064 | { |
| 1065 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 1066 | |
| 1067 | platform_set_drvdata(pdev, NULL); |
| 1068 | |
| 1069 | unregister_netdev(ndev); |
| 1070 | |
| 1071 | free_irq(IRQ_MAC_RX, ndev); |
| 1072 | |
| 1073 | free_netdev(ndev); |
| 1074 | |
| 1075 | setup_pin_mux(0); |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 1080 | #ifdef CONFIG_PM |
| 1081 | static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg) |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1082 | { |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 1083 | struct net_device *net_dev = platform_get_drvdata(pdev); |
| 1084 | |
| 1085 | if (netif_running(net_dev)) |
| 1086 | bf537mac_close(net_dev); |
| 1087 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int bfin_mac_resume(struct platform_device *pdev) |
| 1092 | { |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 1093 | struct net_device *net_dev = platform_get_drvdata(pdev); |
| 1094 | |
| 1095 | if (netif_running(net_dev)) |
| 1096 | bf537mac_open(net_dev); |
| 1097 | |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
Bryan Wu | 496a34c | 2007-09-19 23:37:14 +0800 | [diff] [blame] | 1100 | #else |
| 1101 | #define bfin_mac_suspend NULL |
| 1102 | #define bfin_mac_resume NULL |
| 1103 | #endif /* CONFIG_PM */ |
Bryan Wu | e190d6b | 2007-07-17 14:43:44 +0800 | [diff] [blame] | 1104 | |
| 1105 | static struct platform_driver bfin_mac_driver = { |
| 1106 | .probe = bfin_mac_probe, |
| 1107 | .remove = bfin_mac_remove, |
| 1108 | .resume = bfin_mac_resume, |
| 1109 | .suspend = bfin_mac_suspend, |
| 1110 | .driver = { |
| 1111 | .name = DRV_NAME, |
| 1112 | }, |
| 1113 | }; |
| 1114 | |
| 1115 | static int __init bfin_mac_init(void) |
| 1116 | { |
| 1117 | return platform_driver_register(&bfin_mac_driver); |
| 1118 | } |
| 1119 | |
| 1120 | module_init(bfin_mac_init); |
| 1121 | |
| 1122 | static void __exit bfin_mac_cleanup(void) |
| 1123 | { |
| 1124 | platform_driver_unregister(&bfin_mac_driver); |
| 1125 | } |
| 1126 | |
| 1127 | module_exit(bfin_mac_cleanup); |