wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Author : Hamid Ikdoumi (Atmel) |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <at91rm9200_net.h> |
| 25 | #include <net.h> |
| 26 | |
| 27 | /* ----- Ethernet Buffer definitions ----- */ |
| 28 | |
| 29 | typedef struct { |
| 30 | unsigned long addr, size; |
| 31 | } rbf_t; |
| 32 | |
| 33 | #define RBF_ADDR 0xfffffffc |
| 34 | #define RBF_OWNER (1<<0) |
| 35 | #define RBF_WRAP (1<<1) |
| 36 | #define RBF_BROADCAST (1<<31) |
| 37 | #define RBF_MULTICAST (1<<30) |
| 38 | #define RBF_UNICAST (1<<29) |
| 39 | #define RBF_EXTERNAL (1<<28) |
| 40 | #define RBF_UNKOWN (1<<27) |
| 41 | #define RBF_SIZE 0x07ff |
| 42 | #define RBF_LOCAL4 (1<<26) |
| 43 | #define RBF_LOCAL3 (1<<25) |
| 44 | #define RBF_LOCAL2 (1<<24) |
| 45 | #define RBF_LOCAL1 (1<<23) |
| 46 | |
Wolfgang Denk | 95f9dda | 2005-10-09 00:33:37 +0200 | [diff] [blame] | 47 | #define RBF_FRAMEMAX 64 |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 48 | #define RBF_FRAMELEN 0x600 |
| 49 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_DRIVER_ETHER |
| 51 | |
| 52 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 53 | |
Wolfgang Denk | 95f9dda | 2005-10-09 00:33:37 +0200 | [diff] [blame] | 54 | /* alignment as per Errata #11 (64 bytes) is insufficient! */ |
| 55 | rbf_t rbfdt[RBF_FRAMEMAX] __attribute((aligned(512))); |
| 56 | rbf_t *rbfp; |
| 57 | |
| 58 | unsigned char rbf_framebuf[RBF_FRAMEMAX][RBF_FRAMELEN] __attribute((aligned(4))); |
| 59 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 60 | /* structure to interface the PHY */ |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 61 | AT91S_PhyOps PhyOps; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 62 | |
| 63 | AT91PS_EMAC p_mac; |
| 64 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 65 | /*********** EMAC Phy layer Management functions *************************/ |
| 66 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 67 | * Name: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 68 | * at91rm9200_EmacEnableMDIO |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 69 | * Description: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 70 | * Enables the MDIO bit in MAC control register |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 71 | * Arguments: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 72 | * p_mac - pointer to struct AT91S_EMAC |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 73 | * Return value: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 74 | * none |
| 75 | */ |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 76 | void at91rm9200_EmacEnableMDIO (AT91PS_EMAC p_mac) |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 77 | { |
| 78 | /* Mac CTRL reg set for MDIO enable */ |
| 79 | p_mac->EMAC_CTL |= AT91C_EMAC_MPE; /* Management port enable */ |
| 80 | } |
| 81 | |
| 82 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 83 | * Name: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 84 | * at91rm9200_EmacDisableMDIO |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 85 | * Description: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 86 | * Disables the MDIO bit in MAC control register |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | * Arguments: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 88 | * p_mac - pointer to struct AT91S_EMAC |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 89 | * Return value: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 90 | * none |
| 91 | */ |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 92 | void at91rm9200_EmacDisableMDIO (AT91PS_EMAC p_mac) |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 93 | { |
| 94 | /* Mac CTRL reg set for MDIO disable */ |
| 95 | p_mac->EMAC_CTL &= ~AT91C_EMAC_MPE; /* Management port disable */ |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 100 | * Name: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 101 | * at91rm9200_EmacReadPhy |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 102 | * Description: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 103 | * Reads data from the PHY register |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 104 | * Arguments: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 105 | * dev - pointer to struct net_device |
| 106 | * RegisterAddress - unsigned char |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 107 | * pInput - pointer to value read from register |
| 108 | * Return value: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 109 | * TRUE - if data read successfully |
| 110 | */ |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 111 | UCHAR at91rm9200_EmacReadPhy (AT91PS_EMAC p_mac, |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 112 | unsigned char RegisterAddress, |
| 113 | unsigned short *pInput) |
| 114 | { |
| 115 | p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) | |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 116 | (AT91C_EMAC_RW_R) | |
| 117 | (RegisterAddress << 18) | |
| 118 | (AT91C_EMAC_CODE_802_3); |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 119 | |
| 120 | udelay (10000); |
| 121 | |
| 122 | *pInput = (unsigned short) p_mac->EMAC_MAN; |
| 123 | |
| 124 | return TRUE; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | * Name: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 130 | * at91rm9200_EmacWritePhy |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 131 | * Description: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 132 | * Writes data to the PHY register |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 133 | * Arguments: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 134 | * dev - pointer to struct net_device |
| 135 | * RegisterAddress - unsigned char |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | * pOutput - pointer to value to be written in the register |
| 137 | * Return value: |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 138 | * TRUE - if data read successfully |
| 139 | */ |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 140 | UCHAR at91rm9200_EmacWritePhy (AT91PS_EMAC p_mac, |
wdenk | a3ad8e2 | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 141 | unsigned char RegisterAddress, |
| 142 | unsigned short *pOutput) |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 143 | { |
| 144 | p_mac->EMAC_MAN = (AT91C_EMAC_HIGH & ~AT91C_EMAC_LOW) | |
| 145 | AT91C_EMAC_CODE_802_3 | AT91C_EMAC_RW_W | |
wdenk | a3ad8e2 | 2003-10-19 23:22:11 +0000 | [diff] [blame] | 146 | (RegisterAddress << 18) | *pOutput; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 147 | |
| 148 | udelay (10000); |
| 149 | |
| 150 | return TRUE; |
| 151 | } |
| 152 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 153 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 154 | int eth_init (bd_t * bd) |
| 155 | { |
| 156 | int ret; |
| 157 | int i; |
| 158 | |
| 159 | p_mac = AT91C_BASE_EMAC; |
| 160 | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 161 | /* PIO Disable Register */ |
| 162 | *AT91C_PIOA_PDR = AT91C_PA16_EMDIO | AT91C_PA15_EMDC | AT91C_PA14_ERXER | |
| 163 | AT91C_PA13_ERX1 | AT91C_PA12_ERX0 | AT91C_PA11_ECRS_ECRSDV | |
| 164 | AT91C_PA10_ETX1 | AT91C_PA9_ETX0 | AT91C_PA8_ETXEN | |
| 165 | AT91C_PA7_ETXCK_EREFCK; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 166 | |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 167 | #ifdef CONFIG_AT91C_USE_RMII |
| 168 | *AT91C_PIOB_PDR = AT91C_PB19_ERXCK; |
| 169 | *AT91C_PIOB_BSR = AT91C_PB19_ERXCK; |
| 170 | #else |
| 171 | *AT91C_PIOB_PDR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL | AT91C_PB17_ERXDV | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 172 | AT91C_PB16_ERX3 | AT91C_PB15_ERX2 | AT91C_PB14_ETXER | |
| 173 | AT91C_PB13_ETX3 | AT91C_PB12_ETX2; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 174 | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 175 | /* Select B Register */ |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 176 | *AT91C_PIOB_BSR = AT91C_PB19_ERXCK | AT91C_PB18_ECOL | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 177 | AT91C_PB17_ERXDV | AT91C_PB16_ERX3 | AT91C_PB15_ERX2 | |
| 178 | AT91C_PB14_ETXER | AT91C_PB13_ETX3 | AT91C_PB12_ETX2; |
wdenk | 9d5028c | 2004-11-21 00:06:33 +0000 | [diff] [blame] | 179 | #endif |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 180 | |
| 181 | *AT91C_PMC_PCER = 1 << AT91C_ID_EMAC; /* Peripheral Clock Enable Register */ |
| 182 | |
| 183 | p_mac->EMAC_CFG |= AT91C_EMAC_CSR; /* Clear statistics */ |
| 184 | |
| 185 | /* Init Ehternet buffers */ |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 186 | for (i = 0; i < RBF_FRAMEMAX; i++) { |
Wolfgang Denk | f57f70a | 2005-10-13 01:45:54 +0200 | [diff] [blame^] | 187 | rbfdt[i].addr = (unsigned long)rbf_framebuf[i]; |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 188 | rbfdt[i].size = 0; |
| 189 | } |
| 190 | rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP; |
| 191 | rbfp = &rbfdt[0]; |
| 192 | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 193 | p_mac->EMAC_SA2L = (bd->bi_enetaddr[3] << 24) | (bd->bi_enetaddr[2] << 16) |
| 194 | | (bd->bi_enetaddr[1] << 8) | (bd->bi_enetaddr[0]); |
| 195 | p_mac->EMAC_SA2H = (bd->bi_enetaddr[5] << 8) | (bd->bi_enetaddr[4]); |
| 196 | |
| 197 | p_mac->EMAC_RBQP = (long) (&rbfdt[0]); |
| 198 | p_mac->EMAC_RSR &= ~(AT91C_EMAC_RSR_OVR | AT91C_EMAC_REC | AT91C_EMAC_BNA); |
| 199 | |
| 200 | p_mac->EMAC_CFG = (p_mac->EMAC_CFG | AT91C_EMAC_CAF | AT91C_EMAC_NBC) |
| 201 | & ~AT91C_EMAC_CLK; |
| 202 | |
| 203 | #ifdef CONFIG_AT91C_USE_RMII |
| 204 | p_mac->EMAC_CFG |= AT91C_EMAC_RMII; |
| 205 | #endif |
| 206 | |
wdenk | ba83a30 | 2005-04-04 12:23:03 +0000 | [diff] [blame] | 207 | #if (AT91C_MASTER_CLOCK > 40000000) |
| 208 | /* MDIO clock must not exceed 2.5 MHz, so enable MCK divider */ |
| 209 | p_mac->EMAC_CFG |= AT91C_EMAC_CLK_HCLK_64; |
| 210 | #endif |
| 211 | |
wdenk | 0b8fa03 | 2004-04-25 14:37:29 +0000 | [diff] [blame] | 212 | p_mac->EMAC_CTL |= AT91C_EMAC_TE | AT91C_EMAC_RE; |
| 213 | |
Wolfgang Denk | 080bdb7 | 2005-10-05 01:51:29 +0200 | [diff] [blame] | 214 | at91rm9200_GetPhyInterface (& PhyOps); |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 215 | |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 216 | if (!PhyOps.IsPhyConnected (p_mac)) |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 217 | printf ("PHY not connected!!\n\r"); |
| 218 | |
| 219 | /* MII management start from here */ |
| 220 | if (!(p_mac->EMAC_SR & AT91C_EMAC_LINK)) { |
wdenk | 429168e | 2004-08-02 23:39:03 +0000 | [diff] [blame] | 221 | if (!(ret = PhyOps.Init (p_mac))) { |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 222 | printf ("MAC: error during MII initialization\n"); |
| 223 | return 0; |
| 224 | } |
| 225 | } else { |
| 226 | printf ("No link\n\r"); |
| 227 | return 0; |
| 228 | } |
| 229 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int eth_send (volatile void *packet, int length) |
| 234 | { |
| 235 | while (!(p_mac->EMAC_TSR & AT91C_EMAC_BNQ)); |
| 236 | p_mac->EMAC_TAR = (long) packet; |
| 237 | p_mac->EMAC_TCR = length; |
| 238 | while (p_mac->EMAC_TCR & 0x7ff); |
| 239 | p_mac->EMAC_TSR |= AT91C_EMAC_COMP; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int eth_rx (void) |
| 244 | { |
| 245 | int size; |
| 246 | |
| 247 | if (!(rbfp->addr & RBF_OWNER)) |
| 248 | return 0; |
| 249 | |
| 250 | size = rbfp->size & RBF_SIZE; |
| 251 | NetReceive ((volatile uchar *) (rbfp->addr & RBF_ADDR), size); |
| 252 | |
| 253 | rbfp->addr &= ~RBF_OWNER; |
| 254 | if (rbfp->addr & RBF_WRAP) |
| 255 | rbfp = &rbfdt[0]; |
| 256 | else |
| 257 | rbfp++; |
| 258 | |
| 259 | p_mac->EMAC_RSR |= AT91C_EMAC_REC; |
| 260 | |
| 261 | return size; |
| 262 | } |
| 263 | |
| 264 | void eth_halt (void) |
| 265 | { |
| 266 | }; |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 267 | |
| 268 | #if (CONFIG_COMMANDS & CFG_CMD_MII) |
| 269 | int miiphy_read(unsigned char addr, unsigned char reg, unsigned short * value) |
| 270 | { |
| 271 | at91rm9200_EmacEnableMDIO (p_mac); |
| 272 | at91rm9200_EmacReadPhy (p_mac, reg, value); |
| 273 | at91rm9200_EmacDisableMDIO (p_mac); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value) |
| 278 | { |
| 279 | at91rm9200_EmacEnableMDIO (p_mac); |
| 280 | at91rm9200_EmacWritePhy (p_mac, reg, &value); |
| 281 | at91rm9200_EmacDisableMDIO (p_mac); |
| 282 | return 0; |
| 283 | } |
| 284 | #endif /* CONFIG_COMMANDS & CFG_CMD_MII */ |
| 285 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 286 | #endif /* CONFIG_COMMANDS & CFG_CMD_NET */ |
wdenk | 074cff0 | 2004-02-24 00:16:43 +0000 | [diff] [blame] | 287 | |
wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame] | 288 | #endif /* CONFIG_DRIVER_ETHER */ |