staging:iio:dac:ad5686: Check for negative values
[firefly-linux-kernel-4.4.55.git] / drivers / staging / et131x / et1310_pm.c
1 /*
2  * Agere Systems Inc.
3  * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
4  *
5  * Copyright © 2005 Agere Systems Inc.
6  * All rights reserved.
7  *   http://www.agere.com
8  *
9  * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
10  *
11  *------------------------------------------------------------------------------
12  *
13  * et1310_pm.c - All power management related code (not completely implemented)
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  *
24  * Copyright © 2005 Agere Systems Inc.
25  * 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  *
45  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
46  * 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_defs.h"
61
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
85 #include "et1310_phy.h"
86 #include "et1310_rx.h"
87 #include "et131x_adapter.h"
88 #include "et131x.h"
89
90 /**
91  * et1310_in_phy_coma - check if the device is in phy coma
92  * @adapter: pointer to our adapter structure
93  *
94  * Returns 0 if the device is not in phy coma, 1 if it is in phy coma
95  */
96 int et1310_in_phy_coma(struct et131x_adapter *adapter)
97 {
98         u32 pmcsr;
99
100         pmcsr = readl(&adapter->regs->global.pm_csr);
101
102         return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
103 }
104
105 /**
106  * et1310_enable_phy_coma - called when network cable is unplugged
107  * @adapter: pointer to our adapter structure
108  *
109  * driver receive an phy status change interrupt while in D0 and check that
110  * phy_status is down.
111  *
112  *          -- gate off JAGCore;
113  *          -- set gigE PHY in Coma mode
114  *          -- wake on phy_interrupt; Perform software reset JAGCore,
115  *             re-initialize jagcore and gigE PHY
116  *
117  *      Add D0-ASPM-PhyLinkDown Support:
118  *          -- while in D0, when there is a phy_interrupt indicating phy link
119  *             down status, call the MPSetPhyComa routine to enter this active
120  *             state power saving mode
121  *          -- while in D0-ASPM-PhyLinkDown mode, when there is a phy_interrupt
122  *       indicating linkup status, call the MPDisablePhyComa routine to
123  *             restore JAGCore and gigE PHY
124  */
125 void et1310_enable_phy_coma(struct et131x_adapter *adapter)
126 {
127         unsigned long flags;
128         u32 pmcsr;
129
130         pmcsr = readl(&adapter->regs->global.pm_csr);
131
132         /* Save the GbE PHY speed and duplex modes. Need to restore this
133          * when cable is plugged back in
134          */
135         /*
136          * TODO - when PM is re-enabled, check if we need to
137          * perform a similar task as this -
138          * adapter->pdown_speed = adapter->ai_force_speed;
139          * adapter->pdown_duplex = adapter->ai_force_duplex;
140          */
141
142         /* Stop sending packets. */
143         spin_lock_irqsave(&adapter->send_hw_lock, flags);
144         adapter->flags |= fMP_ADAPTER_LOWER_POWER;
145         spin_unlock_irqrestore(&adapter->send_hw_lock, flags);
146
147         /* Wait for outstanding Receive packets */
148
149         et131x_disable_txrx(adapter->netdev);
150
151         /* Gate off JAGCore 3 clock domains */
152         pmcsr &= ~ET_PMCSR_INIT;
153         writel(pmcsr, &adapter->regs->global.pm_csr);
154
155         /* Program gigE PHY in to Coma mode */
156         pmcsr |= ET_PM_PHY_SW_COMA;
157         writel(pmcsr, &adapter->regs->global.pm_csr);
158 }
159
160 /**
161  * et1310_disable_phy_coma - Disable the Phy Coma Mode
162  * @adapter: pointer to our adapter structure
163  */
164 void et1310_disable_phy_coma(struct et131x_adapter *adapter)
165 {
166         u32 pmcsr;
167
168         pmcsr = readl(&adapter->regs->global.pm_csr);
169
170         /* Disable phy_sw_coma register and re-enable JAGCore clocks */
171         pmcsr |= ET_PMCSR_INIT;
172         pmcsr &= ~ET_PM_PHY_SW_COMA;
173         writel(pmcsr, &adapter->regs->global.pm_csr);
174
175         /* Restore the GbE PHY speed and duplex modes;
176          * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
177          */
178         /* TODO - when PM is re-enabled, check if we need to
179          * perform a similar task as this -
180          * adapter->ai_force_speed = adapter->pdown_speed;
181          * adapter->ai_force_duplex = adapter->pdown_duplex;
182          */
183
184         /* Re-initialize the send structures */
185         et131x_init_send(adapter);
186
187         /* Reset the RFD list and re-start RU  */
188         et131x_reset_recv(adapter);
189
190         /* Bring the device back to the state it was during init prior to
191          * autonegotiation being complete.  This way, when we get the auto-neg
192          * complete interrupt, we can complete init by calling ConfigMacREGS2.
193          */
194         et131x_soft_reset(adapter);
195
196         /* setup et1310 as per the documentation ?? */
197         et131x_adapter_setup(adapter);
198
199         /* Allow Tx to restart */
200         adapter->flags &= ~fMP_ADAPTER_LOWER_POWER;
201
202         et131x_enable_txrx(adapter->netdev);
203 }
204