blob: e02b1cd98f552ca21e4af20dd30c9bca0fd58813 [file] [log] [blame]
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -07001/*
2 * Agere Systems Inc.
3 * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
4 *
Alan Cox64f93032009-06-10 17:30:41 +01005 * Copyright © 2005 Agere Systems Inc.
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -07006 * All rights reserved.
7 * http://www.agere.com
8 *
9 *------------------------------------------------------------------------------
10 *
11 * et131x_initpci.c - Routines and data used to register the driver with the
12 * PCI (and PCI Express) subsystem, as well as basic driver
13 * init and startup.
14 *
15 *------------------------------------------------------------------------------
16 *
17 * SOFTWARE LICENSE
18 *
19 * This software is provided subject to the following terms and conditions,
20 * which you should read carefully before using the software. Using this
21 * software indicates your acceptance of these terms and conditions. If you do
22 * not agree with these terms and conditions, do not use the software.
23 *
Alan Cox64f93032009-06-10 17:30:41 +010024 * Copyright © 2005 Agere Systems Inc.
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070025 * All rights reserved.
26 *
27 * Redistribution and use in source or binary forms, with or without
28 * modifications, are permitted provided that the following conditions are met:
29 *
30 * . Redistributions of source code must retain the above copyright notice, this
31 * list of conditions and the following Disclaimer as comments in the code as
32 * well as in the documentation and/or other materials provided with the
33 * distribution.
34 *
35 * . Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following Disclaimer in the documentation
37 * and/or other materials provided with the distribution.
38 *
39 * . Neither the name of Agere Systems Inc. nor the names of the contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * Disclaimer
44 *
Alan Cox64f93032009-06-10 17:30:41 +010045 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070046 * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
47 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
48 * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
49 * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
50 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
51 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
56 * DAMAGE.
57 *
58 */
59
60#include "et131x_version.h"
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070061#include "et131x_defs.h"
62
63#include <linux/pci.h>
64#include <linux/init.h>
65#include <linux/module.h>
66#include <linux/types.h>
67#include <linux/kernel.h>
68
69#include <linux/sched.h>
70#include <linux/ptrace.h>
71#include <linux/slab.h>
72#include <linux/ctype.h>
73#include <linux/string.h>
74#include <linux/timer.h>
75#include <linux/interrupt.h>
76#include <linux/in.h>
77#include <linux/delay.h>
Alan Cox64f93032009-06-10 17:30:41 +010078#include <linux/io.h>
79#include <linux/bitops.h>
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070080#include <asm/system.h>
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070081
82#include <linux/netdevice.h>
83#include <linux/etherdevice.h>
84#include <linux/skbuff.h>
85#include <linux/if_arp.h>
86#include <linux/ioport.h>
87#include <linux/random.h>
88
89#include "et1310_phy.h"
90#include "et1310_pm.h"
91#include "et1310_jagcore.h"
92
93#include "et131x_adapter.h"
94#include "et131x_netdev.h"
95#include "et131x_config.h"
96#include "et131x_isr.h"
97
98#include "et1310_address_map.h"
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -070099#include "et1310_tx.h"
100#include "et1310_rx.h"
101#include "et1310_mac.h"
102#include "et1310_eeprom.h"
103
104
Alan Cox8c5f20f2009-08-27 11:00:24 +0100105/* Defines for Parameter Default/Min/Max vaules */
106#define PARM_SPEED_DUPLEX_MIN 0
107#define PARM_SPEED_DUPLEX_MAX 5
108
Alan Cox8c5f20f2009-08-27 11:00:24 +0100109/* Module parameter for manual speed setting
110 * Set Link speed and dublex manually (0-5) [0]
111 * 1 : 10Mb Half-Duplex
112 * 2 : 10Mb Full-Duplex
113 * 3 : 100Mb Half-Duplex
114 * 4 : 100Mb Full-Duplex
115 * 5 : 1000Mb Full-Duplex
116 * 0 : Auto Speed Auto Duplex // default
117 */
118static u32 et131x_speed_set;
119module_param(et131x_speed_set, uint, 0);
120MODULE_PARM_DESC(et131x_speed_set,
121 "Set Link speed and dublex manually (0-5) [0] \n 1 : 10Mb Half-Duplex \n 2 : 10Mb Full-Duplex \n 3 : 100Mb Half-Duplex \n 4 : 100Mb Full-Duplex \n 5 : 1000Mb Full-Duplex \n 0 : Auto Speed Auto Dublex");
122
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700123/**
Alan Coxc431e3c2009-10-06 15:47:41 +0100124 * et131x_hwaddr_init - set up the MAC Address on the ET1310
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700125 * @adapter: pointer to our private adapter structure
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700126 */
Alan Coxc431e3c2009-10-06 15:47:41 +0100127void et131x_hwaddr_init(struct et131x_adapter *adapter)
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700128{
Alan Coxc431e3c2009-10-06 15:47:41 +0100129 /* If have our default mac from init and no mac address from
130 * EEPROM then we need to generate the last octet and set it on the
131 * device
132 */
133 if (adapter->PermanentAddress[0] == 0x00 &&
134 adapter->PermanentAddress[1] == 0x00 &&
135 adapter->PermanentAddress[2] == 0x00 &&
136 adapter->PermanentAddress[3] == 0x00 &&
137 adapter->PermanentAddress[4] == 0x00 &&
138 adapter->PermanentAddress[5] == 0x00) {
139 /*
140 * We need to randomly generate the last octet so we
141 * decrease our chances of setting the mac address to
142 * same as another one of our cards in the system
143 */
144 get_random_bytes(&adapter->CurrentAddress[5], 1);
145 /*
146 * We have the default value in the register we are
147 * working with so we need to copy the current
148 * address into the permanent address
149 */
150 memcpy(adapter->PermanentAddress,
151 adapter->CurrentAddress, ETH_ALEN);
152 } else {
153 /* We do not have an override address, so set the
154 * current address to the permanent address and add
155 * it to the device
156 */
157 memcpy(adapter->CurrentAddress,
158 adapter->PermanentAddress, ETH_ALEN);
159 }
160}
161
162
163/**
164 * et131x_pci_init - initial PCI setup
165 * @adapter: pointer to our private adapter structure
166 * @pdev: our PCI device
167 *
168 * Perform the initial setup of PCI registers and if possible initialise
169 * the MAC address. At this point the I/O registers have yet to be mapped
170 */
171
172static int et131x_pci_init(struct et131x_adapter *adapter,
173 struct pci_dev *pdev)
174{
175 int i;
176 u8 max_payload;
177 u8 read_size_reg;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700178
Alan Cox4e02b4b2009-10-06 15:47:22 +0100179 if (et131x_init_eeprom(adapter) < 0)
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700180 return -EIO;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700181
182 /* Let's set up the PORT LOGIC Register. First we need to know what
183 * the max_payload_size is
184 */
Alan Coxc431e3c2009-10-06 15:47:41 +0100185 if (pci_read_config_byte(pdev, ET1310_PCI_MAX_PYLD, &max_payload)) {
Alan Cox15700032009-08-27 11:03:09 +0100186 dev_err(&pdev->dev,
187 "Could not read PCI config space for Max Payload Size\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700188 return -EIO;
189 }
190
191 /* Program the Ack/Nak latency and replay timers */
Alan Coxc431e3c2009-10-06 15:47:41 +0100192 max_payload &= 0x07; /* Only the lower 3 bits are valid */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700193
Alan Coxc431e3c2009-10-06 15:47:41 +0100194 if (max_payload < 2) {
195 static const u16 AckNak[2] = { 0x76, 0xD0 };
196 static const u16 Replay[2] = { 0x1E0, 0x2ED };
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700197
Alan Coxc431e3c2009-10-06 15:47:41 +0100198 if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
199 AckNak[max_payload])) {
Alan Cox15700032009-08-27 11:03:09 +0100200 dev_err(&pdev->dev,
201 "Could not write PCI config space for ACK/NAK\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700202 return -EIO;
203 }
Alan Coxc431e3c2009-10-06 15:47:41 +0100204 if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
205 Replay[max_payload])) {
Alan Cox15700032009-08-27 11:03:09 +0100206 dev_err(&pdev->dev,
207 "Could not write PCI config space for Replay Timer\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700208 return -EIO;
209 }
210 }
211
212 /* l0s and l1 latency timers. We are using default values.
213 * Representing 001 for L0s and 010 for L1
214 */
Alan Coxc431e3c2009-10-06 15:47:41 +0100215 if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
Alan Cox15700032009-08-27 11:03:09 +0100216 dev_err(&pdev->dev,
217 "Could not write PCI config space for Latency Timers\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700218 return -EIO;
219 }
220
221 /* Change the max read size to 2k */
Alan Coxc431e3c2009-10-06 15:47:41 +0100222 if (pci_read_config_byte(pdev, 0x51, &read_size_reg)) {
Alan Cox15700032009-08-27 11:03:09 +0100223 dev_err(&pdev->dev,
Alan Cox64f93032009-06-10 17:30:41 +0100224 "Could not read PCI config space for Max read size\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700225 return -EIO;
226 }
227
228 read_size_reg &= 0x8f;
229 read_size_reg |= 0x40;
230
Alan Coxc431e3c2009-10-06 15:47:41 +0100231 if (pci_write_config_byte(pdev, 0x51, read_size_reg)) {
Alan Cox15700032009-08-27 11:03:09 +0100232 dev_err(&pdev->dev,
Alan Cox64f93032009-06-10 17:30:41 +0100233 "Could not write PCI config space for Max read size\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700234 return -EIO;
235 }
236
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700237 /* Get MAC address from config space if an eeprom exists, otherwise
238 * the MAC address there will not be valid
239 */
Alan Coxc431e3c2009-10-06 15:47:41 +0100240 if (!adapter->has_eeprom) {
241 et131x_hwaddr_init(adapter);
242 return 0;
243 }
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700244
Alan Coxc431e3c2009-10-06 15:47:41 +0100245 for (i = 0; i < ETH_ALEN; i++) {
246 if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
247 adapter->PermanentAddress + i)) {
248 dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
249 return -EIO;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700250 }
251 }
Alan Coxc431e3c2009-10-06 15:47:41 +0100252 memcpy(adapter->CurrentAddress, adapter->PermanentAddress, ETH_ALEN);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700253 return 0;
254}
255
256/**
257 * et131x_error_timer_handler
258 * @data: timer-specific variable; here a pointer to our adapter structure
259 *
260 * The routine called when the error timer expires, to track the number of
261 * recurring errors.
262 */
263void et131x_error_timer_handler(unsigned long data)
264{
Alan Cox25ad00b2009-08-19 18:21:44 +0100265 struct et131x_adapter *etdev = (struct et131x_adapter *) data;
Alan Coxf2c98d22009-08-27 11:01:49 +0100266 u32 pm_csr;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700267
Alan Coxf2c98d22009-08-27 11:01:49 +0100268 pm_csr = readl(&etdev->regs->global.pm_csr);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700269
Alan Coxf2c98d22009-08-27 11:01:49 +0100270 if ((pm_csr & ET_PM_PHY_SW_COMA) == 0)
Alan Cox94831462009-08-27 11:00:06 +0100271 UpdateMacStatHostCounters(etdev);
272 else
Alan Cox15700032009-08-27 11:03:09 +0100273 dev_err(&etdev->pdev->dev,
274 "No interrupts, in PHY coma, pm_csr = 0x%x\n", pm_csr);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700275
Alan Cox25ad00b2009-08-19 18:21:44 +0100276 if (!etdev->Bmsr.bits.link_status &&
277 etdev->RegistryPhyComa &&
278 etdev->PoMgmt.TransPhyComaModeOnBoot < 11) {
279 etdev->PoMgmt.TransPhyComaModeOnBoot++;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700280 }
281
Alan Cox25ad00b2009-08-19 18:21:44 +0100282 if (etdev->PoMgmt.TransPhyComaModeOnBoot == 10) {
283 if (!etdev->Bmsr.bits.link_status
284 && etdev->RegistryPhyComa) {
Alan Coxf2c98d22009-08-27 11:01:49 +0100285 if ((pm_csr & ET_PM_PHY_SW_COMA) == 0) {
Alan Cox64f93032009-06-10 17:30:41 +0100286 /* NOTE - This was originally a 'sync with
287 * interrupt'. How to do that under Linux?
288 */
Alan Cox25ad00b2009-08-19 18:21:44 +0100289 et131x_enable_interrupts(etdev);
290 EnablePhyComa(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700291 }
292 }
293 }
294
295 /* This is a periodic timer, so reschedule */
Alan Cox25ad00b2009-08-19 18:21:44 +0100296 mod_timer(&etdev->ErrorTimer, jiffies +
Alan Cox64f93032009-06-10 17:30:41 +0100297 TX_ERROR_PERIOD * HZ / 1000);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700298}
299
300/**
301 * et131x_link_detection_handler
302 *
303 * Timer function for link up at driver load time
304 */
305void et131x_link_detection_handler(unsigned long data)
306{
Alan Cox25ad00b2009-08-19 18:21:44 +0100307 struct et131x_adapter *etdev = (struct et131x_adapter *) data;
Alan Cox37628602009-08-19 18:21:50 +0100308 unsigned long flags;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700309
Alan Cox25ad00b2009-08-19 18:21:44 +0100310 if (etdev->MediaState == 0) {
Alan Cox37628602009-08-19 18:21:50 +0100311 spin_lock_irqsave(&etdev->Lock, flags);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700312
Alan Cox25ad00b2009-08-19 18:21:44 +0100313 etdev->MediaState = NETIF_STATUS_MEDIA_DISCONNECT;
Alan Coxf6b35d62009-08-27 11:02:05 +0100314 etdev->Flags &= ~fMP_ADAPTER_LINK_DETECTION;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700315
Alan Cox37628602009-08-19 18:21:50 +0100316 spin_unlock_irqrestore(&etdev->Lock, flags);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700317
Alan Cox25ad00b2009-08-19 18:21:44 +0100318 netif_carrier_off(etdev->netdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700319 }
320}
321
322/**
Alan Coxb8c4cc42009-08-27 11:02:25 +0100323 * et131x_configure_global_regs - configure JAGCore global regs
324 * @etdev: pointer to our adapter structure
325 *
326 * Used to configure the global registers on the JAGCore
327 */
328void ConfigGlobalRegs(struct et131x_adapter *etdev)
329{
Alan Cox2b903322010-01-18 15:33:12 +0000330 struct global_regs __iomem *regs = &etdev->regs->global;
Alan Coxb8c4cc42009-08-27 11:02:25 +0100331
Alan Cox5f1377d2009-10-06 15:47:55 +0100332 writel(0, &regs->rxq_start_addr);
333 writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
Alan Coxb8c4cc42009-08-27 11:02:25 +0100334
Alan Cox5f1377d2009-10-06 15:47:55 +0100335 if (etdev->RegistryJumboPacket < 2048) {
336 /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
337 * block of RAM that the driver can split between Tx
338 * and Rx as it desires. Our default is to split it
339 * 50/50:
Alan Coxb8c4cc42009-08-27 11:02:25 +0100340 */
Alan Cox5f1377d2009-10-06 15:47:55 +0100341 writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
342 writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
343 } else if (etdev->RegistryJumboPacket < 8192) {
344 /* For jumbo packets > 2k but < 8k, split 50-50. */
345 writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
346 writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
347 } else {
348 /* 9216 is the only packet size greater than 8k that
349 * is available. The Tx buffer has to be big enough
350 * for one whole packet on the Tx side. We'll make
351 * the Tx 9408, and give the rest to Rx
352 */
353 writel(0x01b3, &regs->rxq_end_addr);
354 writel(0x01b4, &regs->txq_start_addr);
Alan Coxb8c4cc42009-08-27 11:02:25 +0100355 }
356
Alan Cox5f1377d2009-10-06 15:47:55 +0100357 /* Initialize the loopback register. Disable all loopbacks. */
358 writel(0, &regs->loopback);
359
Alan Coxb8c4cc42009-08-27 11:02:25 +0100360 /* MSI Register */
Alan Coxe266b202009-08-27 11:02:34 +0100361 writel(0, &regs->msi_config);
Alan Coxb8c4cc42009-08-27 11:02:25 +0100362
363 /* By default, disable the watchdog timer. It will be enabled when
364 * a packet is queued.
365 */
Alan Coxe266b202009-08-27 11:02:34 +0100366 writel(0, &regs->watchdog_timer);
Alan Coxb8c4cc42009-08-27 11:02:25 +0100367}
368
369
370/**
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700371 * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
372 * @adapter: pointer to our private adapter structure
373 *
374 * Returns 0 on success, errno on failure (as defined in errno.h)
375 */
Alan Cox25ad00b2009-08-19 18:21:44 +0100376int et131x_adapter_setup(struct et131x_adapter *etdev)
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700377{
378 int status = 0;
379
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700380 /* Configure the JAGCore */
Alan Cox25ad00b2009-08-19 18:21:44 +0100381 ConfigGlobalRegs(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700382
Alan Cox25ad00b2009-08-19 18:21:44 +0100383 ConfigMACRegs1(etdev);
Alan Coxb8c4cc42009-08-27 11:02:25 +0100384
385 /* Configure the MMC registers */
386 /* All we need to do is initialize the Memory Control Register */
387 writel(ET_MMC_ENABLE, &etdev->regs->mmc.mmc_ctrl);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700388
Alan Cox25ad00b2009-08-19 18:21:44 +0100389 ConfigRxMacRegs(etdev);
390 ConfigTxMacRegs(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700391
Alan Cox25ad00b2009-08-19 18:21:44 +0100392 ConfigRxDmaRegs(etdev);
393 ConfigTxDmaRegs(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700394
Alan Cox25ad00b2009-08-19 18:21:44 +0100395 ConfigMacStatRegs(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700396
397 /* Move the following code to Timer function?? */
Alan Cox25ad00b2009-08-19 18:21:44 +0100398 status = et131x_xcvr_find(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700399
Alan Cox64f93032009-06-10 17:30:41 +0100400 if (status != 0)
Alan Cox15700032009-08-27 11:03:09 +0100401 dev_warn(&etdev->pdev->dev, "Could not find the xcvr\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700402
403 /* Prepare the TRUEPHY library. */
Alan Cox25ad00b2009-08-19 18:21:44 +0100404 ET1310_PhyInit(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700405
406 /* Reset the phy now so changes take place */
Alan Cox25ad00b2009-08-19 18:21:44 +0100407 ET1310_PhyReset(etdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700408
409 /* Power down PHY */
Alan Cox25ad00b2009-08-19 18:21:44 +0100410 ET1310_PhyPowerDown(etdev, 1);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700411
412 /*
413 * We need to turn off 1000 base half dulplex, the mac does not
414 * support it. For the 10/100 part, turn off all gig advertisement
415 */
Alan Cox5ec34872009-08-27 10:59:13 +0100416 if (etdev->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
Alan Cox25ad00b2009-08-19 18:21:44 +0100417 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_FULL);
Alan Cox64f93032009-06-10 17:30:41 +0100418 else
Alan Cox25ad00b2009-08-19 18:21:44 +0100419 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_NONE);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700420
421 /* Power up PHY */
Alan Cox25ad00b2009-08-19 18:21:44 +0100422 ET1310_PhyPowerDown(etdev, 0);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700423
Alan Cox25ad00b2009-08-19 18:21:44 +0100424 et131x_setphy_normal(etdev);
Alan Cox15700032009-08-27 11:03:09 +0100425; return status;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700426}
427
428/**
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700429 * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
430 * @adapter: pointer to our private adapter structure
431 */
432void et131x_soft_reset(struct et131x_adapter *adapter)
433{
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700434 /* Disable MAC Core */
Alan Coxc9835d972009-10-06 15:50:45 +0100435 writel(0xc00f0000, &adapter->regs->mac.cfg1);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700436
437 /* Set everything to a reset value */
Alan Coxb8c4cc42009-08-27 11:02:25 +0100438 writel(0x7F, &adapter->regs->global.sw_reset);
Alan Coxc9835d972009-10-06 15:50:45 +0100439 writel(0x000f0000, &adapter->regs->mac.cfg1);
440 writel(0x00000000, &adapter->regs->mac.cfg1);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700441}
442
443/**
444 * et131x_align_allocated_memory - Align allocated memory on a given boundary
445 * @adapter: pointer to our adapter structure
446 * @phys_addr: pointer to Physical address
447 * @offset: pointer to the offset variable
448 * @mask: correct mask
449 */
450void et131x_align_allocated_memory(struct et131x_adapter *adapter,
451 uint64_t *phys_addr,
452 uint64_t *offset, uint64_t mask)
453{
454 uint64_t new_addr;
455
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700456 *offset = 0;
457
458 new_addr = *phys_addr & ~mask;
459
460 if (new_addr != *phys_addr) {
461 /* Move to next aligned block */
462 new_addr += mask + 1;
463 /* Return offset for adjusting virt addr */
464 *offset = new_addr - *phys_addr;
465 /* Return new physical address */
466 *phys_addr = new_addr;
467 }
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700468}
469
470/**
471 * et131x_adapter_memory_alloc
472 * @adapter: pointer to our private adapter structure
473 *
474 * Returns 0 on success, errno on failure (as defined in errno.h).
475 *
476 * Allocate all the memory blocks for send, receive and others.
477 */
478int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
479{
Alan Coxc431e3c2009-10-06 15:47:41 +0100480 int status;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700481
Alan Coxc431e3c2009-10-06 15:47:41 +0100482 /* Allocate memory for the Tx Ring */
483 status = et131x_tx_dma_memory_alloc(adapter);
484 if (status != 0) {
485 dev_err(&adapter->pdev->dev,
486 "et131x_tx_dma_memory_alloc FAILED\n");
487 return status;
488 }
489 /* Receive buffer memory allocation */
490 status = et131x_rx_dma_memory_alloc(adapter);
491 if (status != 0) {
492 dev_err(&adapter->pdev->dev,
493 "et131x_rx_dma_memory_alloc FAILED\n");
494 et131x_tx_dma_memory_free(adapter);
495 return status;
496 }
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700497
Alan Coxc431e3c2009-10-06 15:47:41 +0100498 /* Init receive data structures */
499 status = et131x_init_recv(adapter);
500 if (status != 0) {
501 dev_err(&adapter->pdev->dev,
502 "et131x_init_recv FAILED\n");
503 et131x_tx_dma_memory_free(adapter);
504 et131x_rx_dma_memory_free(adapter);
505 }
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700506 return status;
507}
508
509/**
510 * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
511 * @adapter: pointer to our private adapter structure
512 */
513void et131x_adapter_memory_free(struct et131x_adapter *adapter)
514{
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700515 /* Free DMA memory */
516 et131x_tx_dma_memory_free(adapter);
517 et131x_rx_dma_memory_free(adapter);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700518}
519
Alan Coxc431e3c2009-10-06 15:47:41 +0100520
521
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700522/**
Alan Coxc431e3c2009-10-06 15:47:41 +0100523 * et131x_adapter_init
Alan Cox8c5f20f2009-08-27 11:00:24 +0100524 * @etdev: pointer to the private adapter struct
Alan Coxc431e3c2009-10-06 15:47:41 +0100525 * @pdev: pointer to the PCI device
Alan Cox8c5f20f2009-08-27 11:00:24 +0100526 *
Alan Coxc431e3c2009-10-06 15:47:41 +0100527 * Initialize the data structures for the et131x_adapter object and link
528 * them together with the platform provided device structures.
Alan Cox8c5f20f2009-08-27 11:00:24 +0100529 */
Alan Coxc431e3c2009-10-06 15:47:41 +0100530
531
532static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
533 struct pci_dev *pdev)
Alan Cox8c5f20f2009-08-27 11:00:24 +0100534{
535 static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
536 static const u8 duplex[] = { 0, 1, 2, 1, 2, 2 };
537 static const u16 speed[] = { 0, 10, 10, 100, 100, 1000 };
538
Alan Coxc431e3c2009-10-06 15:47:41 +0100539 struct et131x_adapter *etdev;
540
541 /* Setup the fundamental net_device and private adapter structure elements */
542 SET_NETDEV_DEV(netdev, &pdev->dev);
543
544 /* Allocate private adapter struct and copy in relevant information */
545 etdev = netdev_priv(netdev);
546 etdev->pdev = pci_dev_get(pdev);
547 etdev->netdev = netdev;
548
549 /* Do the same for the netdev struct */
550 netdev->irq = pdev->irq;
551 netdev->base_addr = pci_resource_start(pdev, 0);
552
553 /* Initialize spinlocks here */
554 spin_lock_init(&etdev->Lock);
555 spin_lock_init(&etdev->TCBSendQLock);
556 spin_lock_init(&etdev->TCBReadyQLock);
557 spin_lock_init(&etdev->SendHWLock);
Alan Coxc431e3c2009-10-06 15:47:41 +0100558 spin_lock_init(&etdev->RcvLock);
559 spin_lock_init(&etdev->RcvPendLock);
560 spin_lock_init(&etdev->FbrLock);
561 spin_lock_init(&etdev->PHYLock);
562
563 /* Parse configuration parameters into the private adapter struct */
Alan Cox8c5f20f2009-08-27 11:00:24 +0100564 if (et131x_speed_set)
Alan Cox15700032009-08-27 11:03:09 +0100565 dev_info(&etdev->pdev->dev,
566 "Speed set manually to : %d \n", et131x_speed_set);
Alan Cox8c5f20f2009-08-27 11:00:24 +0100567
568 etdev->SpeedDuplex = et131x_speed_set;
569 etdev->RegistryJumboPacket = 1514; /* 1514-9216 */
570
Alan Cox8c5f20f2009-08-27 11:00:24 +0100571 /* Set the MAC address to a default */
572 memcpy(etdev->CurrentAddress, default_mac, ETH_ALEN);
573
574 /* Decode SpeedDuplex
575 *
576 * Set up as if we are auto negotiating always and then change if we
577 * go into force mode
578 *
579 * If we are the 10/100 device, and gigabit is somehow requested then
580 * knock it down to 100 full.
581 */
582 if (etdev->pdev->device == ET131X_PCI_DEVICE_ID_FAST &&
583 etdev->SpeedDuplex == 5)
584 etdev->SpeedDuplex = 4;
585
586 etdev->AiForceSpeed = speed[etdev->SpeedDuplex];
587 etdev->AiForceDpx = duplex[etdev->SpeedDuplex]; /* Auto FDX */
Alan Coxc431e3c2009-10-06 15:47:41 +0100588
589 return etdev;
Alan Cox8c5f20f2009-08-27 11:00:24 +0100590}
591
Alan Cox7ca5d422009-08-27 11:03:21 +0100592/**
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700593 * et131x_pci_setup - Perform device initialization
594 * @pdev: a pointer to the device's pci_dev structure
595 * @ent: this device's entry in the pci_device_id table
596 *
597 * Returns 0 on success, errno on failure (as defined in errno.h)
598 *
599 * Registered in the pci_driver structure, this function is called when the
600 * PCI subsystem finds a new PCI device which matches the information
601 * contained in the pci_device_id table. This routine is the equivalent to
602 * a device insertion routine.
603 */
Alan Cox7ca5d422009-08-27 11:03:21 +0100604
Alan Coxc431e3c2009-10-06 15:47:41 +0100605static int __devinit et131x_pci_setup(struct pci_dev *pdev,
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700606 const struct pci_device_id *ent)
607{
Alan Coxc431e3c2009-10-06 15:47:41 +0100608 int result = -EBUSY;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700609 int pm_cap;
610 bool pci_using_dac;
Alan Coxc431e3c2009-10-06 15:47:41 +0100611 struct net_device *netdev;
612 struct et131x_adapter *adapter;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700613
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700614 /* Enable the device via the PCI subsystem */
Alan Coxc431e3c2009-10-06 15:47:41 +0100615 if (pci_enable_device(pdev) != 0) {
616 dev_err(&pdev->dev,
Alan Cox15700032009-08-27 11:03:09 +0100617 "pci_enable_device() failed\n");
Alan Coxc431e3c2009-10-06 15:47:41 +0100618 return -EIO;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700619 }
620
621 /* Perform some basic PCI checks */
622 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
Alan Coxc431e3c2009-10-06 15:47:41 +0100623 dev_err(&pdev->dev,
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700624 "Can't find PCI device's base address\n");
Alan Coxc431e3c2009-10-06 15:47:41 +0100625 goto err_disable;
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700626 }
627
Alan Coxc431e3c2009-10-06 15:47:41 +0100628 if (pci_request_regions(pdev, DRIVER_NAME)) {
629 dev_err(&pdev->dev,
Alan Cox15700032009-08-27 11:03:09 +0100630 "Can't get PCI resources\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700631 goto err_disable;
632 }
633
634 /* Enable PCI bus mastering */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700635 pci_set_master(pdev);
636
637 /* Query PCI for Power Mgmt Capabilities
638 *
639 * NOTE: Now reading PowerMgmt in another location; is this still
640 * needed?
641 */
642 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
643 if (pm_cap == 0) {
Alan Coxc431e3c2009-10-06 15:47:41 +0100644 dev_err(&pdev->dev,
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700645 "Cannot find Power Management capabilities\n");
646 result = -EIO;
647 goto err_release_res;
648 }
649
650 /* Check the DMA addressing support of this device */
Alan Coxc431e3c2009-10-06 15:47:41 +0100651 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700652 pci_using_dac = true;
653
Alan Coxc431e3c2009-10-06 15:47:41 +0100654 result = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700655 if (result != 0) {
Alan Cox15700032009-08-27 11:03:09 +0100656 dev_err(&pdev->dev,
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700657 "Unable to obtain 64 bit DMA for consistent allocations\n");
658 goto err_release_res;
659 }
Alan Coxc431e3c2009-10-06 15:47:41 +0100660 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700661 pci_using_dac = false;
662 } else {
Alan Coxc431e3c2009-10-06 15:47:41 +0100663 dev_err(&pdev->dev,
Alan Cox15700032009-08-27 11:03:09 +0100664 "No usable DMA addressing method\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700665 result = -EIO;
666 goto err_release_res;
667 }
668
669 /* Allocate netdev and private adapter structs */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700670 netdev = et131x_device_alloc();
671 if (netdev == NULL) {
Alan Coxc431e3c2009-10-06 15:47:41 +0100672 dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700673 result = -ENOMEM;
674 goto err_release_res;
675 }
Alan Coxc431e3c2009-10-06 15:47:41 +0100676 adapter = et131x_adapter_init(netdev, pdev);
677 /* Initialise the PCI setup for the device */
678 et131x_pci_init(adapter, pdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700679
680 /* Map the bus-relative registers to system virtual memory */
Alan Coxc431e3c2009-10-06 15:47:41 +0100681 adapter->regs = pci_ioremap_bar(pdev, 0);
Alan Coxf3f415a2009-08-27 10:59:30 +0100682 if (adapter->regs == NULL) {
Alan Cox15700032009-08-27 11:03:09 +0100683 dev_err(&pdev->dev, "Cannot map device registers\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700684 result = -ENOMEM;
685 goto err_free_dev;
686 }
687
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700688 /* If Phy COMA mode was enabled when we went down, disable it here. */
Alan Coxf2c98d22009-08-27 11:01:49 +0100689 writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700690
691 /* Issue a global reset to the et1310 */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700692 et131x_soft_reset(adapter);
693
694 /* Disable all interrupts (paranoid) */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700695 et131x_disable_interrupts(adapter);
696
697 /* Allocate DMA memory */
698 result = et131x_adapter_memory_alloc(adapter);
699 if (result != 0) {
Alan Cox15700032009-08-27 11:03:09 +0100700 dev_err(&pdev->dev, "Could not alloc adapater memory (DMA)\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700701 goto err_iounmap;
702 }
703
704 /* Init send data structures */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700705 et131x_init_send(adapter);
706
Alan Coxc431e3c2009-10-06 15:47:41 +0100707 /*
708 * Set up the task structure for the ISR's deferred handler
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700709 */
710 INIT_WORK(&adapter->task, et131x_isr_handler);
711
Alan Coxc431e3c2009-10-06 15:47:41 +0100712 /* Copy address into the net_device struct */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700713 memcpy(netdev->dev_addr, adapter->CurrentAddress, ETH_ALEN);
714
715 /* Setup et1310 as per the documentation */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700716 et131x_adapter_setup(adapter);
717
718 /* Create a timer to count errors received by the NIC */
719 init_timer(&adapter->ErrorTimer);
720
721 adapter->ErrorTimer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
722 adapter->ErrorTimer.function = et131x_error_timer_handler;
723 adapter->ErrorTimer.data = (unsigned long)adapter;
724
725 /* Initialize link state */
726 et131x_link_detection_handler((unsigned long)adapter);
727
Alan Cox64f93032009-06-10 17:30:41 +0100728 /* Intialize variable for counting how long we do not have
729 link status */
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700730 adapter->PoMgmt.TransPhyComaModeOnBoot = 0;
731
732 /* We can enable interrupts now
733 *
734 * NOTE - Because registration of interrupt handler is done in the
735 * device's open(), defer enabling device interrupts to that
736 * point
737 */
738
739 /* Register the net_device struct with the Linux network layer */
Alan Cox64f93032009-06-10 17:30:41 +0100740 result = register_netdev(netdev);
741 if (result != 0) {
Alan Cox15700032009-08-27 11:03:09 +0100742 dev_err(&pdev->dev, "register_netdev() failed\n");
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700743 goto err_mem_free;
744 }
745
746 /* Register the net_device struct with the PCI subsystem. Save a copy
747 * of the PCI config space for this device now that the device has
748 * been initialized, just in case it needs to be quickly restored.
749 */
750 pci_set_drvdata(pdev, netdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700751 pci_save_state(adapter->pdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700752 return result;
753
754err_mem_free:
755 et131x_adapter_memory_free(adapter);
756err_iounmap:
Alan Coxf3f415a2009-08-27 10:59:30 +0100757 iounmap(adapter->regs);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700758err_free_dev:
Alan Cox6ae56042009-08-27 10:59:21 +0100759 pci_dev_put(pdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700760 free_netdev(netdev);
761err_release_res:
762 pci_release_regions(pdev);
763err_disable:
764 pci_disable_device(pdev);
Alan Coxc431e3c2009-10-06 15:47:41 +0100765 return result;
766}
767
768/**
769 * et131x_pci_remove
770 * @pdev: a pointer to the device's pci_dev structure
771 *
772 * Registered in the pci_driver structure, this function is called when the
773 * PCI subsystem detects that a PCI device which matches the information
774 * contained in the pci_device_id table has been removed.
775 */
776
777static void __devexit et131x_pci_remove(struct pci_dev *pdev)
778{
779 struct net_device *netdev;
780 struct et131x_adapter *adapter;
781
782 /* Retrieve the net_device pointer from the pci_dev struct, as well
783 * as the private adapter struct
784 */
785 netdev = (struct net_device *) pci_get_drvdata(pdev);
786 adapter = netdev_priv(netdev);
787
788 /* Perform device cleanup */
789 unregister_netdev(netdev);
790 et131x_adapter_memory_free(adapter);
791 iounmap(adapter->regs);
792 pci_dev_put(adapter->pdev);
793 free_netdev(netdev);
794 pci_release_regions(pdev);
795 pci_disable_device(pdev);
Greg Kroah-Hartmancfb739b2008-04-03 17:30:53 -0700796}
Alan Cox7ca5d422009-08-27 11:03:21 +0100797
798static struct pci_device_id et131x_pci_table[] __devinitdata = {
799 {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_GIG, PCI_ANY_ID,
800 PCI_ANY_ID, 0, 0, 0UL},
801 {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_FAST, PCI_ANY_ID,
802 PCI_ANY_ID, 0, 0, 0UL},
803 {0,}
804};
805
806MODULE_DEVICE_TABLE(pci, et131x_pci_table);
807
808static struct pci_driver et131x_driver = {
809 .name = DRIVER_NAME,
810 .id_table = et131x_pci_table,
811 .probe = et131x_pci_setup,
812 .remove = __devexit_p(et131x_pci_remove),
813 .suspend = NULL, /* et131x_pci_suspend */
814 .resume = NULL, /* et131x_pci_resume */
815};
816
817
818/**
819 * et131x_init_module - The "main" entry point called on driver initialization
820 *
821 * Returns 0 on success, errno on failure (as defined in errno.h)
822 */
Peter Huewe3c0d4462009-09-29 01:34:14 +0200823static int __init et131x_init_module(void)
Alan Cox7ca5d422009-08-27 11:03:21 +0100824{
825 if (et131x_speed_set < PARM_SPEED_DUPLEX_MIN ||
826 et131x_speed_set > PARM_SPEED_DUPLEX_MAX) {
827 printk(KERN_WARNING "et131x: invalid speed setting ignored.\n");
828 et131x_speed_set = 0;
829 }
830 return pci_register_driver(&et131x_driver);
831}
832
833/**
834 * et131x_cleanup_module - The entry point called on driver cleanup
835 */
Peter Huewe3c0d4462009-09-29 01:34:14 +0200836static void __exit et131x_cleanup_module(void)
Alan Cox7ca5d422009-08-27 11:03:21 +0100837{
838 pci_unregister_driver(&et131x_driver);
839}
840
841module_init(et131x_init_module);
842module_exit(et131x_cleanup_module);
843
Alan Cox7ca5d422009-08-27 11:03:21 +0100844/* Modinfo parameters (filled out using defines from et131x_version.h) */
845MODULE_AUTHOR(DRIVER_AUTHOR);
846MODULE_DESCRIPTION(DRIVER_INFO);
847MODULE_LICENSE(DRIVER_LICENSE);