staging: et131x: Remove redundant struct adapter members
[pandora-kernel.git] / drivers / staging / et131x / et1310_phy.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1310 and ET131x series MACs
4  *
5  * Copyright * 2005 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  *------------------------------------------------------------------------------
10  *
11  * et1310_phy.c - Routines for configuring and accessing the PHY
12  *
13  *------------------------------------------------------------------------------
14  *
15  * SOFTWARE LICENSE
16  *
17  * This software is provided subject to the following terms and conditions,
18  * which you should read carefully before using the software.  Using this
19  * software indicates your acceptance of these terms and conditions.  If you do
20  * not agree with these terms and conditions, do not use the software.
21  *
22  * Copyright * 2005 Agere Systems Inc.
23  * All rights reserved.
24  *
25  * Redistribution and use in source or binary forms, with or without
26  * modifications, are permitted provided that the following conditions are met:
27  *
28  * . Redistributions of source code must retain the above copyright notice, this
29  *    list of conditions and the following Disclaimer as comments in the code as
30  *    well as in the documentation and/or other materials provided with the
31  *    distribution.
32  *
33  * . Redistributions in binary form must reproduce the above copyright notice,
34  *    this list of conditions and the following Disclaimer in the documentation
35  *    and/or other materials provided with the distribution.
36  *
37  * . Neither the name of Agere Systems Inc. nor the names of the contributors
38  *    may be used to endorse or promote products derived from this software
39  *    without specific prior written permission.
40  *
41  * Disclaimer
42  *
43  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
44  * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
45  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  ANY
46  * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
47  * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
48  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51  * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
54  * DAMAGE.
55  *
56  */
57
58 #include "et131x_version.h"
59 #include "et131x_defs.h"
60
61 #include <linux/pci.h>
62 #include <linux/init.h>
63 #include <linux/module.h>
64 #include <linux/types.h>
65 #include <linux/kernel.h>
66
67 #include <linux/sched.h>
68 #include <linux/ptrace.h>
69 #include <linux/ctype.h>
70 #include <linux/string.h>
71 #include <linux/timer.h>
72 #include <linux/interrupt.h>
73 #include <linux/in.h>
74 #include <linux/delay.h>
75 #include <linux/io.h>
76 #include <linux/bitops.h>
77 #include <asm/system.h>
78
79 #include <linux/netdevice.h>
80 #include <linux/etherdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/if_arp.h>
83 #include <linux/ioport.h>
84 #include <linux/random.h>
85 #include <linux/phy.h>
86
87 #include "et1310_phy.h"
88
89 #include "et131x_adapter.h"
90
91 #include "et1310_address_map.h"
92 #include "et1310_tx.h"
93 #include "et1310_rx.h"
94
95 #include "et131x.h"
96
97 int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
98 {
99         struct net_device *netdev = bus->priv;
100         struct et131x_adapter *adapter = netdev_priv(netdev);
101         u16 value;
102         int ret;
103
104         ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
105
106         if (ret < 0)
107                 return ret;
108         else
109                 return value;
110 }
111
112 int et131x_mdio_write(struct mii_bus *bus, int phy_addr, int reg, u16 value)
113 {
114         struct net_device *netdev = bus->priv;
115         struct et131x_adapter *adapter = netdev_priv(netdev);
116
117         return et131x_mii_write(adapter, reg, value);
118 }
119
120 int et131x_mdio_reset(struct mii_bus *bus)
121 {
122         struct net_device *netdev = bus->priv;
123         struct et131x_adapter *adapter = netdev_priv(netdev);
124
125         et131x_mii_write(adapter, MII_BMCR, 0x8000);
126
127         return 0;
128 }
129
130
131 int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
132 {
133         struct phy_device *phydev = adapter->phydev;
134
135         if (!phydev)
136                 return -EIO;
137
138         return et131x_phy_mii_read(adapter, phydev->addr, reg, value);
139 }
140
141 /**
142  * et131x_phy_mii_read - Read from the PHY through the MII Interface on the MAC
143  * @adapter: pointer to our private adapter structure
144  * @addr: the address of the transceiver
145  * @reg: the register to read
146  * @value: pointer to a 16-bit value in which the value will be stored
147  *
148  * Returns 0 on success, errno on failure (as defined in errno.h)
149  */
150 int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
151               u8 reg, u16 *value)
152 {
153         struct mac_regs __iomem *mac = &adapter->regs->mac;
154         int status = 0;
155         u32 delay = 0;
156         u32 mii_addr;
157         u32 mii_cmd;
158         u32 mii_indicator;
159
160         /* Save a local copy of the registers we are dealing with so we can
161          * set them back
162          */
163         mii_addr = readl(&mac->mii_mgmt_addr);
164         mii_cmd = readl(&mac->mii_mgmt_cmd);
165
166         /* Stop the current operation */
167         writel(0, &mac->mii_mgmt_cmd);
168
169         /* Set up the register we need to read from on the correct PHY */
170         writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
171
172         writel(0x1, &mac->mii_mgmt_cmd);
173
174         do {
175                 udelay(50);
176                 delay++;
177                 mii_indicator = readl(&mac->mii_mgmt_indicator);
178         } while ((mii_indicator & MGMT_WAIT) && delay < 50);
179
180         /* If we hit the max delay, we could not read the register */
181         if (delay == 50) {
182                 dev_warn(&adapter->pdev->dev,
183                             "reg 0x%08x could not be read\n", reg);
184                 dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
185                             mii_indicator);
186
187                 status = -EIO;
188         }
189
190         /* If we hit here we were able to read the register and we need to
191          * return the value to the caller */
192         *value = readl(&mac->mii_mgmt_stat) & 0xFFFF;
193
194         /* Stop the read operation */
195         writel(0, &mac->mii_mgmt_cmd);
196
197         /* set the registers we touched back to the state at which we entered
198          * this function
199          */
200         writel(mii_addr, &mac->mii_mgmt_addr);
201         writel(mii_cmd, &mac->mii_mgmt_cmd);
202
203         return status;
204 }
205
206 /**
207  * et131x_mii_write - Write to a PHY register through the MII interface of the MAC
208  * @adapter: pointer to our private adapter structure
209  * @reg: the register to read
210  * @value: 16-bit value to write
211  *
212  * FIXME: one caller in netdev still
213  *
214  * Return 0 on success, errno on failure (as defined in errno.h)
215  */
216 int et131x_mii_write(struct et131x_adapter *adapter, u8 reg, u16 value)
217 {
218         struct mac_regs __iomem *mac = &adapter->regs->mac;
219         struct phy_device *phydev = adapter->phydev;
220         int status = 0;
221         u8 addr;
222         u32 delay = 0;
223         u32 mii_addr;
224         u32 mii_cmd;
225         u32 mii_indicator;
226
227         if (!phydev)
228                 return -EIO;
229
230         addr = phydev->addr;
231
232         /* Save a local copy of the registers we are dealing with so we can
233          * set them back
234          */
235         mii_addr = readl(&mac->mii_mgmt_addr);
236         mii_cmd = readl(&mac->mii_mgmt_cmd);
237
238         /* Stop the current operation */
239         writel(0, &mac->mii_mgmt_cmd);
240
241         /* Set up the register we need to write to on the correct PHY */
242         writel(MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
243
244         /* Add the value to write to the registers to the mac */
245         writel(value, &mac->mii_mgmt_ctrl);
246
247         do {
248                 udelay(50);
249                 delay++;
250                 mii_indicator = readl(&mac->mii_mgmt_indicator);
251         } while ((mii_indicator & MGMT_BUSY) && delay < 100);
252
253         /* If we hit the max delay, we could not write the register */
254         if (delay == 100) {
255                 u16 tmp;
256
257                 dev_warn(&adapter->pdev->dev,
258                     "reg 0x%08x could not be written", reg);
259                 dev_warn(&adapter->pdev->dev, "status is  0x%08x\n",
260                             mii_indicator);
261                 dev_warn(&adapter->pdev->dev, "command is  0x%08x\n",
262                             readl(&mac->mii_mgmt_cmd));
263
264                 et131x_mii_read(adapter, reg, &tmp);
265
266                 status = -EIO;
267         }
268         /* Stop the write operation */
269         writel(0, &mac->mii_mgmt_cmd);
270
271         /*
272          * set the registers we touched back to the state at which we entered
273          * this function
274          */
275         writel(mii_addr, &mac->mii_mgmt_addr);
276         writel(mii_cmd, &mac->mii_mgmt_cmd);
277
278         return status;
279 }
280
281 /**
282  *      et1310_phy_power_down   -       PHY power control
283  *      @adapter: device to control
284  *      @down: true for off/false for back on
285  *
286  *      one hundred, ten, one thousand megs
287  *      How would you like to have your LAN accessed
288  *      Can't you see that this code processed
289  *      Phy power, phy power..
290  */
291 void et1310_phy_power_down(struct et131x_adapter *adapter, bool down)
292 {
293         u16 data;
294
295         et131x_mii_read(adapter, MII_BMCR, &data);
296         data &= ~0x0800;        /* Power UP */
297         if (down) /* Power DOWN */
298                 data |= 0x0800;
299         et131x_mii_write(adapter, MII_BMCR, data);
300 }
301
302 /**
303  *      et1310_phy_link_status  -       read link state
304  *      @adapter: device to read
305  *      @link_status: reported link state
306  *      @autoneg: reported autonegotiation state (complete/incomplete/disabled)
307  *      @linkspeed: returnedlink speed in use
308  *      @duplex_mode: reported half/full duplex state
309  *      @mdi_mdix: not yet working
310  *      @masterslave: report whether we are master or slave
311  *      @polarity: link polarity
312  *
313  *      I can read your lan like a magazine
314  *      I see if your up
315  *      I know your link speed
316  *      I see all the setting that you'd rather keep
317  */
318 static void et1310_phy_link_status(struct et131x_adapter *adapter,
319                           u8 *link_status,
320                           u32 *autoneg,
321                           u32 *linkspeed,
322                           u32 *duplex_mode,
323                           u32 *mdi_mdix,
324                           u32 *masterslave, u32 *polarity)
325 {
326         u16 mistatus = 0;
327         u16 is1000BaseT = 0;
328         u16 vmi_phystatus = 0;
329         u16 control = 0;
330
331         et131x_mii_read(adapter, MII_BMSR, &mistatus);
332         et131x_mii_read(adapter, MII_STAT1000, &is1000BaseT);
333         et131x_mii_read(adapter, PHY_PHY_STATUS, &vmi_phystatus);
334         et131x_mii_read(adapter, MII_BMCR, &control);
335
336         *link_status = (vmi_phystatus & 0x0040) ? 1 : 0;
337         *autoneg = (control & 0x1000) ? ((vmi_phystatus & 0x0020) ?
338                                             TRUEPHY_ANEG_COMPLETE :
339                                             TRUEPHY_ANEG_NOT_COMPLETE) :
340                     TRUEPHY_ANEG_DISABLED;
341         *linkspeed = (vmi_phystatus & 0x0300) >> 8;
342         *duplex_mode = (vmi_phystatus & 0x0080) >> 7;
343         /* NOTE: Need to complete this */
344         *mdi_mdix = 0;
345
346         *masterslave = (is1000BaseT & 0x4000) ?
347                         TRUEPHY_CFG_MASTER : TRUEPHY_CFG_SLAVE;
348         *polarity = (vmi_phystatus & 0x0400) ?
349                         TRUEPHY_POLARITY_INVERTED : TRUEPHY_POLARITY_NORMAL;
350 }
351
352 static void et1310_phy_and_or_reg(struct et131x_adapter *adapter,
353                                   u16 regnum, u16 and_mask, u16 or_mask)
354 {
355         u16 reg;
356
357         et131x_mii_read(adapter, regnum, &reg);
358         reg &= and_mask;
359         reg |= or_mask;
360         et131x_mii_write(adapter, regnum, reg);
361 }
362
363 /* Still used from _mac for BIT_READ */
364 void et1310_phy_access_mii_bit(struct et131x_adapter *adapter, u16 action,
365                                u16 regnum, u16 bitnum, u8 *value)
366 {
367         u16 reg;
368         u16 mask = 0x0001 << bitnum;
369
370         /* Read the requested register */
371         et131x_mii_read(adapter, regnum, &reg);
372
373         switch (action) {
374         case TRUEPHY_BIT_READ:
375                 *value = (reg & mask) >> bitnum;
376                 break;
377
378         case TRUEPHY_BIT_SET:
379                 et131x_mii_write(adapter, regnum, reg | mask);
380                 break;
381
382         case TRUEPHY_BIT_CLEAR:
383                 et131x_mii_write(adapter, regnum, reg & ~mask);
384                 break;
385
386         default:
387                 break;
388         }
389 }
390
391 /**
392  * et131x_xcvr_init - Init the phy if we are setting it into force mode
393  * @adapter: pointer to our private adapter structure
394  *
395  */
396 void et131x_xcvr_init(struct et131x_adapter *adapter)
397 {
398         u16 imr;
399         u16 isr;
400         u16 lcr2;
401
402         /* Zero out the adapter structure variable representing BMSR */
403         adapter->bmsr = 0;
404
405         et131x_mii_read(adapter, (u8) offsetof(struct mi_regs, isr), &isr);
406         et131x_mii_read(adapter, (u8) offsetof(struct mi_regs, imr), &imr);
407
408         /* Set the link status interrupt only.  Bad behavior when link status
409          * and auto neg are set, we run into a nested interrupt problem
410          */
411         imr |= 0x0105;
412
413         et131x_mii_write(adapter, (u8) offsetof(struct mi_regs, imr), imr);
414
415         /* Set the LED behavior such that LED 1 indicates speed (off =
416          * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
417          * link and activity (on for link, blink off for activity).
418          *
419          * NOTE: Some customizations have been added here for specific
420          * vendors; The LED behavior is now determined by vendor data in the
421          * EEPROM. However, the above description is the default.
422          */
423         if ((adapter->eeprom_data[1] & 0x4) == 0) {
424                 et131x_mii_read(adapter, (u8) offsetof(struct mi_regs, lcr2),
425                        &lcr2);
426
427                 lcr2 &= 0x00FF;
428                 lcr2 |= 0xA000; /* led link */
429
430                 if ((adapter->eeprom_data[1] & 0x8) == 0)
431                         lcr2 |= 0x0300;
432                 else
433                         lcr2 |= 0x0400;
434
435                 et131x_mii_write(adapter, (u8) offsetof(struct mi_regs, lcr2),
436                         lcr2);
437         }
438 }
439
440 void et131x_mii_check(struct et131x_adapter *adapter,
441                       u16 bmsr, u16 bmsr_ints)
442 {
443         struct phy_device *phydev = adapter->phydev;
444         u8 link_status;
445         u32 autoneg_status;
446         u32 speed;
447         u32 duplex;
448         u32 mdi_mdix;
449         u32 masterslave;
450         u32 polarity;
451
452         if (bmsr_ints & BMSR_LSTATUS) {
453                 if (bmsr & BMSR_LSTATUS) {
454                         adapter->boot_coma = 20;
455                         netif_carrier_on(adapter->netdev);
456                 } else {
457                         dev_warn(&adapter->pdev->dev,
458                             "Link down - cable problem ?\n");
459
460                         if (phydev && phydev->speed == SPEED_10) {
461                                 /* NOTE - Is there a way to query this without
462                                  * TruePHY?
463                                  * && TRU_QueryCoreType(adapter->hTruePhy, 0) ==
464                                  * EMI_TRUEPHY_A13O) {
465                                  */
466                                 u16 register18;
467
468                                 et131x_mii_read(adapter, 0x12, &register18);
469                                 et131x_mii_write(adapter, 0x12,
470                                                  register18 | 0x4);
471                                 et131x_mii_write(adapter, 0x10,
472                                                  register18 | 0x8402);
473                                 et131x_mii_write(adapter, 0x11,
474                                                  register18 | 511);
475                                 et131x_mii_write(adapter, 0x12, register18);
476                         }
477
478                         netif_carrier_off(adapter->netdev);
479
480                         /* Free the packets being actively sent & stopped */
481                         et131x_free_busy_send_packets(adapter);
482
483                         /* Re-initialize the send structures */
484                         et131x_init_send(adapter);
485
486                         /* Reset the RFD list and re-start RU */
487                         et131x_reset_recv(adapter);
488
489                         /*
490                          * Bring the device back to the state it was during
491                          * init prior to autonegotiation being complete. This
492                          * way, when we get the auto-neg complete interrupt,
493                          * we can complete init by calling config_mac_regs2.
494                          */
495                         et131x_soft_reset(adapter);
496
497                         /* Setup ET1310 as per the documentation */
498                         et131x_adapter_setup(adapter);
499                 }
500         }
501
502         if ((bmsr_ints & BMSR_ANEGCOMPLETE) ||
503            (adapter->ai_force_duplex == 3 && (bmsr_ints & BMSR_LSTATUS))) {
504                 if ((bmsr & BMSR_ANEGCOMPLETE) ||
505                     adapter->ai_force_duplex == 3) {
506                         et1310_phy_link_status(adapter,
507                                              &link_status, &autoneg_status,
508                                              &speed, &duplex, &mdi_mdix,
509                                              &masterslave, &polarity);
510
511                         adapter->boot_coma = 20;
512
513                         if (phydev && phydev->speed == SPEED_10) {
514                                 /*
515                                  * NOTE - Is there a way to query this without
516                                  * TruePHY?
517                                  * && TRU_QueryCoreType(adapter->hTruePhy, 0)==
518                                  * EMI_TRUEPHY_A13O) {
519                                  */
520                                 u16 register18;
521
522                                 et131x_mii_read(adapter, 0x12, &register18);
523                                 et131x_mii_write(adapter, 0x12,
524                                                  register18 | 0x4);
525                                 et131x_mii_write(adapter, 0x10,
526                                                  register18 | 0x8402);
527                                 et131x_mii_write(adapter, 0x11,
528                                                  register18 | 511);
529                                 et131x_mii_write(adapter, 0x12, register18);
530                         }
531
532                         et1310_config_flow_control(adapter);
533
534                         if (phydev && phydev->speed == SPEED_1000 &&
535                                         adapter->registry_jumbo_packet > 2048)
536                                 et1310_phy_and_or_reg(adapter, 0x16, 0xcfff,
537                                                                    0x2000);
538
539                         et131x_set_rx_dma_timer(adapter);
540                         et1310_config_mac_regs2(adapter);
541                 }
542         }
543 }
544