1af866496SDavid Daney /***********************license start***************
2af866496SDavid Daney * Author: Cavium Networks
3af866496SDavid Daney *
4af866496SDavid Daney * Contact: support@caviumnetworks.com
5af866496SDavid Daney * This file is part of the OCTEON SDK
6af866496SDavid Daney *
7af866496SDavid Daney * Copyright (c) 2003-2008 Cavium Networks
8af866496SDavid Daney *
9af866496SDavid Daney * This file is free software; you can redistribute it and/or modify
10af866496SDavid Daney * it under the terms of the GNU General Public License, Version 2, as
11af866496SDavid Daney * published by the Free Software Foundation.
12af866496SDavid Daney *
13af866496SDavid Daney * This file is distributed in the hope that it will be useful, but
14af866496SDavid Daney * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15af866496SDavid Daney * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16af866496SDavid Daney * NONINFRINGEMENT. See the GNU General Public License for more
17af866496SDavid Daney * details.
18af866496SDavid Daney *
19af866496SDavid Daney * You should have received a copy of the GNU General Public License
20af866496SDavid Daney * along with this file; if not, write to the Free Software
21af866496SDavid Daney * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22af866496SDavid Daney * or visit http://www.gnu.org/licenses/.
23af866496SDavid Daney *
24af866496SDavid Daney * This file may also be available under a different license from Cavium.
25af866496SDavid Daney * Contact Cavium Networks for more information
26af866496SDavid Daney ***********************license end**************************************/
27af866496SDavid Daney
28af866496SDavid Daney /*
29af866496SDavid Daney * Small helper utilities.
30af866496SDavid Daney */
31af866496SDavid Daney #include <linux/kernel.h>
32af866496SDavid Daney
33af866496SDavid Daney #include <asm/octeon/octeon.h>
34af866496SDavid Daney
35af866496SDavid Daney #include <asm/octeon/cvmx-config.h>
36af866496SDavid Daney
37af866496SDavid Daney #include <asm/octeon/cvmx-fpa.h>
38af866496SDavid Daney #include <asm/octeon/cvmx-pip.h>
39af866496SDavid Daney #include <asm/octeon/cvmx-pko.h>
40af866496SDavid Daney #include <asm/octeon/cvmx-ipd.h>
41af866496SDavid Daney #include <asm/octeon/cvmx-spi.h>
42af866496SDavid Daney
43af866496SDavid Daney #include <asm/octeon/cvmx-helper.h>
44af866496SDavid Daney #include <asm/octeon/cvmx-helper-util.h>
45af866496SDavid Daney
46af866496SDavid Daney #include <asm/octeon/cvmx-ipd-defs.h>
47af866496SDavid Daney
48af866496SDavid Daney /**
49af866496SDavid Daney * Convert a interface mode into a human readable string
50af866496SDavid Daney *
51af866496SDavid Daney * @mode: Mode to convert
52af866496SDavid Daney *
53af866496SDavid Daney * Returns String
54af866496SDavid Daney */
cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t mode)55af866496SDavid Daney const char *cvmx_helper_interface_mode_to_string(cvmx_helper_interface_mode_t
56af866496SDavid Daney mode)
57af866496SDavid Daney {
58af866496SDavid Daney switch (mode) {
59af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_DISABLED:
60af866496SDavid Daney return "DISABLED";
61af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_RGMII:
62af866496SDavid Daney return "RGMII";
63af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_GMII:
64af866496SDavid Daney return "GMII";
65af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_SPI:
66af866496SDavid Daney return "SPI";
67af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_PCIE:
68af866496SDavid Daney return "PCIE";
69af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_XAUI:
70af866496SDavid Daney return "XAUI";
71af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_SGMII:
72af866496SDavid Daney return "SGMII";
73af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_PICMG:
74af866496SDavid Daney return "PICMG";
75af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_NPI:
76af866496SDavid Daney return "NPI";
77af866496SDavid Daney case CVMX_HELPER_INTERFACE_MODE_LOOP:
78af866496SDavid Daney return "LOOP";
79af866496SDavid Daney }
80af866496SDavid Daney return "UNKNOWN";
81af866496SDavid Daney }
82af866496SDavid Daney
83af866496SDavid Daney /**
84af866496SDavid Daney * Setup Random Early Drop on a specific input queue
85af866496SDavid Daney *
86af866496SDavid Daney * @queue: Input queue to setup RED on (0-7)
87af866496SDavid Daney * @pass_thresh:
88af866496SDavid Daney * Packets will begin slowly dropping when there are less than
89af866496SDavid Daney * this many packet buffers free in FPA 0.
90af866496SDavid Daney * @drop_thresh:
91d82603c6SJorrit Schippers * All incoming packets will be dropped when there are less
92af866496SDavid Daney * than this many free packet buffers in FPA 0.
93af866496SDavid Daney * Returns Zero on success. Negative on failure
94af866496SDavid Daney */
cvmx_helper_setup_red_queue(int queue,int pass_thresh,int drop_thresh)95*18304463SAaro Koskinen static int cvmx_helper_setup_red_queue(int queue, int pass_thresh,
96*18304463SAaro Koskinen int drop_thresh)
97af866496SDavid Daney {
98af866496SDavid Daney union cvmx_ipd_qosx_red_marks red_marks;
99af866496SDavid Daney union cvmx_ipd_red_quex_param red_param;
100af866496SDavid Daney
101af866496SDavid Daney /* Set RED to begin dropping packets when there are pass_thresh buffers
102af866496SDavid Daney left. It will linearly drop more packets until reaching drop_thresh
103af866496SDavid Daney buffers */
104af866496SDavid Daney red_marks.u64 = 0;
105af866496SDavid Daney red_marks.s.drop = drop_thresh;
106af866496SDavid Daney red_marks.s.pass = pass_thresh;
107af866496SDavid Daney cvmx_write_csr(CVMX_IPD_QOSX_RED_MARKS(queue), red_marks.u64);
108af866496SDavid Daney
109af866496SDavid Daney /* Use the actual queue 0 counter, not the average */
110af866496SDavid Daney red_param.u64 = 0;
111af866496SDavid Daney red_param.s.prb_con =
112af866496SDavid Daney (255ul << 24) / (red_marks.s.pass - red_marks.s.drop);
113af866496SDavid Daney red_param.s.avg_con = 1;
114af866496SDavid Daney red_param.s.new_con = 255;
115af866496SDavid Daney red_param.s.use_pcnt = 1;
116af866496SDavid Daney cvmx_write_csr(CVMX_IPD_RED_QUEX_PARAM(queue), red_param.u64);
117af866496SDavid Daney return 0;
118af866496SDavid Daney }
119af866496SDavid Daney
120af866496SDavid Daney /**
121af866496SDavid Daney * Setup Random Early Drop to automatically begin dropping packets.
122af866496SDavid Daney *
123af866496SDavid Daney * @pass_thresh:
124af866496SDavid Daney * Packets will begin slowly dropping when there are less than
125af866496SDavid Daney * this many packet buffers free in FPA 0.
126af866496SDavid Daney * @drop_thresh:
127d82603c6SJorrit Schippers * All incoming packets will be dropped when there are less
128af866496SDavid Daney * than this many free packet buffers in FPA 0.
129af866496SDavid Daney * Returns Zero on success. Negative on failure
130af866496SDavid Daney */
cvmx_helper_setup_red(int pass_thresh,int drop_thresh)131af866496SDavid Daney int cvmx_helper_setup_red(int pass_thresh, int drop_thresh)
132af866496SDavid Daney {
133af866496SDavid Daney union cvmx_ipd_portx_bp_page_cnt page_cnt;
134af866496SDavid Daney union cvmx_ipd_bp_prt_red_end ipd_bp_prt_red_end;
135af866496SDavid Daney union cvmx_ipd_red_port_enable red_port_enable;
136af866496SDavid Daney int queue;
137af866496SDavid Daney int interface;
138af866496SDavid Daney int port;
139af866496SDavid Daney
140af866496SDavid Daney /* Disable backpressure based on queued buffers. It needs SW support */
141af866496SDavid Daney page_cnt.u64 = 0;
142af866496SDavid Daney page_cnt.s.bp_enb = 0;
143af866496SDavid Daney page_cnt.s.page_cnt = 100;
144af866496SDavid Daney for (interface = 0; interface < 2; interface++) {
145af866496SDavid Daney for (port = cvmx_helper_get_first_ipd_port(interface);
146af866496SDavid Daney port < cvmx_helper_get_last_ipd_port(interface); port++)
147af866496SDavid Daney cvmx_write_csr(CVMX_IPD_PORTX_BP_PAGE_CNT(port),
148af866496SDavid Daney page_cnt.u64);
149af866496SDavid Daney }
150af866496SDavid Daney
151af866496SDavid Daney for (queue = 0; queue < 8; queue++)
152af866496SDavid Daney cvmx_helper_setup_red_queue(queue, pass_thresh, drop_thresh);
153af866496SDavid Daney
154af866496SDavid Daney /* Shutoff the dropping based on the per port page count. SW isn't
155af866496SDavid Daney decrementing it right now */
156af866496SDavid Daney ipd_bp_prt_red_end.u64 = 0;
157af866496SDavid Daney ipd_bp_prt_red_end.s.prt_enb = 0;
158af866496SDavid Daney cvmx_write_csr(CVMX_IPD_BP_PRT_RED_END, ipd_bp_prt_red_end.u64);
159af866496SDavid Daney
160af866496SDavid Daney red_port_enable.u64 = 0;
161af866496SDavid Daney red_port_enable.s.prt_enb = 0xfffffffffull;
162af866496SDavid Daney red_port_enable.s.avg_dly = 10000;
163af866496SDavid Daney red_port_enable.s.prb_dly = 10000;
164af866496SDavid Daney cvmx_write_csr(CVMX_IPD_RED_PORT_ENABLE, red_port_enable.u64);
165af866496SDavid Daney
166af866496SDavid Daney return 0;
167af866496SDavid Daney }
168ce4126cbSAaro Koskinen EXPORT_SYMBOL_GPL(cvmx_helper_setup_red);
169af866496SDavid Daney
170af866496SDavid Daney /**
171af866496SDavid Daney * Setup the common GMX settings that determine the number of
172af866496SDavid Daney * ports. These setting apply to almost all configurations of all
173af866496SDavid Daney * chips.
174af866496SDavid Daney *
175af866496SDavid Daney * @interface: Interface to configure
176af866496SDavid Daney * @num_ports: Number of ports on the interface
177af866496SDavid Daney *
178af866496SDavid Daney * Returns Zero on success, negative on failure
179af866496SDavid Daney */
__cvmx_helper_setup_gmx(int interface,int num_ports)180af866496SDavid Daney int __cvmx_helper_setup_gmx(int interface, int num_ports)
181af866496SDavid Daney {
182af866496SDavid Daney union cvmx_gmxx_tx_prts gmx_tx_prts;
183af866496SDavid Daney union cvmx_gmxx_rx_prts gmx_rx_prts;
184af866496SDavid Daney union cvmx_pko_reg_gmx_port_mode pko_mode;
185af866496SDavid Daney union cvmx_gmxx_txx_thresh gmx_tx_thresh;
186af866496SDavid Daney int index;
187af866496SDavid Daney
188af866496SDavid Daney /* Tell GMX the number of TX ports on this interface */
189af866496SDavid Daney gmx_tx_prts.u64 = cvmx_read_csr(CVMX_GMXX_TX_PRTS(interface));
190af866496SDavid Daney gmx_tx_prts.s.prts = num_ports;
191af866496SDavid Daney cvmx_write_csr(CVMX_GMXX_TX_PRTS(interface), gmx_tx_prts.u64);
192af866496SDavid Daney
193af866496SDavid Daney /* Tell GMX the number of RX ports on this interface. This only
194af866496SDavid Daney ** applies to *GMII and XAUI ports */
195af866496SDavid Daney if (cvmx_helper_interface_get_mode(interface) ==
196af866496SDavid Daney CVMX_HELPER_INTERFACE_MODE_RGMII
197af866496SDavid Daney || cvmx_helper_interface_get_mode(interface) ==
198af866496SDavid Daney CVMX_HELPER_INTERFACE_MODE_SGMII
199af866496SDavid Daney || cvmx_helper_interface_get_mode(interface) ==
200af866496SDavid Daney CVMX_HELPER_INTERFACE_MODE_GMII
201af866496SDavid Daney || cvmx_helper_interface_get_mode(interface) ==
202af866496SDavid Daney CVMX_HELPER_INTERFACE_MODE_XAUI) {
203af866496SDavid Daney if (num_ports > 4) {
204af866496SDavid Daney cvmx_dprintf("__cvmx_helper_setup_gmx: Illegal "
205af866496SDavid Daney "num_ports\n");
206af866496SDavid Daney return -1;
207af866496SDavid Daney }
208af866496SDavid Daney
209af866496SDavid Daney gmx_rx_prts.u64 = cvmx_read_csr(CVMX_GMXX_RX_PRTS(interface));
210af866496SDavid Daney gmx_rx_prts.s.prts = num_ports;
211af866496SDavid Daney cvmx_write_csr(CVMX_GMXX_RX_PRTS(interface), gmx_rx_prts.u64);
212af866496SDavid Daney }
213af866496SDavid Daney
214af866496SDavid Daney /* Skip setting CVMX_PKO_REG_GMX_PORT_MODE on 30XX, 31XX, and 50XX */
215af866496SDavid Daney if (!OCTEON_IS_MODEL(OCTEON_CN30XX) && !OCTEON_IS_MODEL(OCTEON_CN31XX)
216af866496SDavid Daney && !OCTEON_IS_MODEL(OCTEON_CN50XX)) {
217af866496SDavid Daney /* Tell PKO the number of ports on this interface */
218af866496SDavid Daney pko_mode.u64 = cvmx_read_csr(CVMX_PKO_REG_GMX_PORT_MODE);
219af866496SDavid Daney if (interface == 0) {
220af866496SDavid Daney if (num_ports == 1)
221af866496SDavid Daney pko_mode.s.mode0 = 4;
222af866496SDavid Daney else if (num_ports == 2)
223af866496SDavid Daney pko_mode.s.mode0 = 3;
224af866496SDavid Daney else if (num_ports <= 4)
225af866496SDavid Daney pko_mode.s.mode0 = 2;
226af866496SDavid Daney else if (num_ports <= 8)
227af866496SDavid Daney pko_mode.s.mode0 = 1;
228af866496SDavid Daney else
229af866496SDavid Daney pko_mode.s.mode0 = 0;
230af866496SDavid Daney } else {
231af866496SDavid Daney if (num_ports == 1)
232af866496SDavid Daney pko_mode.s.mode1 = 4;
233af866496SDavid Daney else if (num_ports == 2)
234af866496SDavid Daney pko_mode.s.mode1 = 3;
235af866496SDavid Daney else if (num_ports <= 4)
236af866496SDavid Daney pko_mode.s.mode1 = 2;
237af866496SDavid Daney else if (num_ports <= 8)
238af866496SDavid Daney pko_mode.s.mode1 = 1;
239af866496SDavid Daney else
240af866496SDavid Daney pko_mode.s.mode1 = 0;
241af866496SDavid Daney }
242af866496SDavid Daney cvmx_write_csr(CVMX_PKO_REG_GMX_PORT_MODE, pko_mode.u64);
243af866496SDavid Daney }
244af866496SDavid Daney
245af866496SDavid Daney /*
246af866496SDavid Daney * Set GMX to buffer as much data as possible before starting
247af866496SDavid Daney * transmit. This reduces the chances that we have a TX under
248af866496SDavid Daney * run due to memory contention. Any packet that fits entirely
249af866496SDavid Daney * in the GMX FIFO can never have an under run regardless of
250af866496SDavid Daney * memory load.
251af866496SDavid Daney */
252af866496SDavid Daney gmx_tx_thresh.u64 = cvmx_read_csr(CVMX_GMXX_TXX_THRESH(0, interface));
253af866496SDavid Daney if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN31XX)
254af866496SDavid Daney || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
255af866496SDavid Daney /* These chips have a fixed max threshold of 0x40 */
256af866496SDavid Daney gmx_tx_thresh.s.cnt = 0x40;
257af866496SDavid Daney } else {
258af866496SDavid Daney /* Choose the max value for the number of ports */
259af866496SDavid Daney if (num_ports <= 1)
260af866496SDavid Daney gmx_tx_thresh.s.cnt = 0x100 / 1;
261af866496SDavid Daney else if (num_ports == 2)
262af866496SDavid Daney gmx_tx_thresh.s.cnt = 0x100 / 2;
263af866496SDavid Daney else
264af866496SDavid Daney gmx_tx_thresh.s.cnt = 0x100 / 4;
265af866496SDavid Daney }
266af866496SDavid Daney /*
267af866496SDavid Daney * SPI and XAUI can have lots of ports but the GMX hardware
268af866496SDavid Daney * only ever has a max of 4.
269af866496SDavid Daney */
270af866496SDavid Daney if (num_ports > 4)
271af866496SDavid Daney num_ports = 4;
272af866496SDavid Daney for (index = 0; index < num_ports; index++)
273af866496SDavid Daney cvmx_write_csr(CVMX_GMXX_TXX_THRESH(index, interface),
274af866496SDavid Daney gmx_tx_thresh.u64);
275af866496SDavid Daney
276af866496SDavid Daney return 0;
277af866496SDavid Daney }
278af866496SDavid Daney
279af866496SDavid Daney /**
280af866496SDavid Daney * Returns the IPD/PKO port number for a port on the given
281af866496SDavid Daney * interface.
282af866496SDavid Daney *
283af866496SDavid Daney * @interface: Interface to use
284af866496SDavid Daney * @port: Port on the interface
285af866496SDavid Daney *
286af866496SDavid Daney * Returns IPD/PKO port number
287af866496SDavid Daney */
cvmx_helper_get_ipd_port(int interface,int port)288af866496SDavid Daney int cvmx_helper_get_ipd_port(int interface, int port)
289af866496SDavid Daney {
290af866496SDavid Daney switch (interface) {
291af866496SDavid Daney case 0:
292af866496SDavid Daney return port;
293af866496SDavid Daney case 1:
294af866496SDavid Daney return port + 16;
295af866496SDavid Daney case 2:
296af866496SDavid Daney return port + 32;
297af866496SDavid Daney case 3:
298af866496SDavid Daney return port + 36;
29931705376SJanne Huttunen case 4:
30031705376SJanne Huttunen return port + 40;
30131705376SJanne Huttunen case 5:
30231705376SJanne Huttunen return port + 44;
303af866496SDavid Daney }
304af866496SDavid Daney return -1;
305af866496SDavid Daney }
306ce4126cbSAaro Koskinen EXPORT_SYMBOL_GPL(cvmx_helper_get_ipd_port);
307af866496SDavid Daney
308af866496SDavid Daney /**
309af866496SDavid Daney * Returns the interface number for an IPD/PKO port number.
310af866496SDavid Daney *
311af866496SDavid Daney * @ipd_port: IPD/PKO port number
312af866496SDavid Daney *
313af866496SDavid Daney * Returns Interface number
314af866496SDavid Daney */
cvmx_helper_get_interface_num(int ipd_port)315af866496SDavid Daney int cvmx_helper_get_interface_num(int ipd_port)
316af866496SDavid Daney {
317af866496SDavid Daney if (ipd_port < 16)
318af866496SDavid Daney return 0;
319af866496SDavid Daney else if (ipd_port < 32)
320af866496SDavid Daney return 1;
321af866496SDavid Daney else if (ipd_port < 36)
322af866496SDavid Daney return 2;
323af866496SDavid Daney else if (ipd_port < 40)
324af866496SDavid Daney return 3;
32531705376SJanne Huttunen else if (ipd_port < 44)
32631705376SJanne Huttunen return 4;
32731705376SJanne Huttunen else if (ipd_port < 48)
32831705376SJanne Huttunen return 5;
329af866496SDavid Daney else
330af866496SDavid Daney cvmx_dprintf("cvmx_helper_get_interface_num: Illegal IPD "
331af866496SDavid Daney "port number\n");
332af866496SDavid Daney
333af866496SDavid Daney return -1;
334af866496SDavid Daney }
335ce4126cbSAaro Koskinen EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_num);
336af866496SDavid Daney
337af866496SDavid Daney /**
338af866496SDavid Daney * Returns the interface index number for an IPD/PKO port
339af866496SDavid Daney * number.
340af866496SDavid Daney *
341af866496SDavid Daney * @ipd_port: IPD/PKO port number
342af866496SDavid Daney *
343af866496SDavid Daney * Returns Interface index number
344af866496SDavid Daney */
cvmx_helper_get_interface_index_num(int ipd_port)345af866496SDavid Daney int cvmx_helper_get_interface_index_num(int ipd_port)
346af866496SDavid Daney {
347af866496SDavid Daney if (ipd_port < 32)
348af866496SDavid Daney return ipd_port & 15;
349af866496SDavid Daney else if (ipd_port < 36)
350af866496SDavid Daney return ipd_port & 3;
351af866496SDavid Daney else if (ipd_port < 40)
352af866496SDavid Daney return ipd_port & 3;
35331705376SJanne Huttunen else if (ipd_port < 44)
35431705376SJanne Huttunen return ipd_port & 3;
35531705376SJanne Huttunen else if (ipd_port < 48)
35631705376SJanne Huttunen return ipd_port & 3;
357af866496SDavid Daney else
358af866496SDavid Daney cvmx_dprintf("cvmx_helper_get_interface_index_num: "
359af866496SDavid Daney "Illegal IPD port number\n");
360af866496SDavid Daney
361af866496SDavid Daney return -1;
362af866496SDavid Daney }
363ce4126cbSAaro Koskinen EXPORT_SYMBOL_GPL(cvmx_helper_get_interface_index_num);
364