Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dc.c
Go to the documentation of this file.
1 /*
2  * SBE 2T3E3 synchronous serial card driver for Linux
3  *
4  * Copyright (C) 2009-2010 Krzysztof Halasa <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License
8  * as published by the Free Software Foundation.
9  *
10  * This code is based on a driver written by SBE Inc.
11  */
12 
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/errno.h>
16 #include <linux/io.h>
17 #include "2t3e3.h"
18 #include "ctrl.h"
19 
20 static int dc_init_descriptor_list(struct channel *sc);
21 
22 void dc_init(struct channel *sc)
23 {
24  u32 val;
25 
26  dc_stop(sc);
27  /*dc_reset(sc);*/ /* do not want to reset here */
28 
29  /*
30  * BUS_MODE (CSR0)
31  */
36 
37  if (sc->h.command & 16)
39 
40  switch (sc->h.cache_size) {
41  case 32:
43  break;
44  case 16:
46  break;
47  case 8:
49  break;
50  default:
51  break;
52  }
53 
54  dc_write(sc->addr, SBE_2T3E3_21143_REG_BUS_MODE, val);
55 
56  /* OPERATION_MODE (CSR6) */
64  dc_write(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE, val);
67 
68  /*
69  * GENERAL_PURPOSE_TIMER_AND_INTERRUPT_MITIGATION_CONTROL (CSR11)
70  */
77 
78  /* prepare descriptors and data for receive and transmit processes */
79  if (dc_init_descriptor_list(sc) != 0)
80  return;
81 
82  /* clear ethernet interrupts status */
83  dc_write(sc->addr, SBE_2T3E3_21143_REG_STATUS, 0xFFFFFFFF);
84 
85  /* SIA mode registers */
87 }
88 
89 void dc_start(struct channel *sc)
90 {
91  u32 val;
92 
93  if (!(sc->r.flags & SBE_2T3E3_FLAG_NETWORK_UP))
94  return;
95 
96  dc_init(sc);
97 
98  /* get actual LOS and OOF status */
99  switch (sc->p.frame_type) {
103  dev_dbg(&sc->pdev->dev, "Start Framer Rx Status = %02X\n", val);
104  sc->s.OOF = val & SBE_2T3E3_FRAMER_VAL_E3_RX_OOF ? 1 : 0;
105  break;
109  dev_dbg(&sc->pdev->dev, "Start Framer Rx Status = %02X\n", val);
110  sc->s.OOF = val & SBE_2T3E3_FRAMER_VAL_T3_RX_OOF ? 1 : 0;
111  break;
112  default:
113  break;
114  }
115  cpld_LOS_update(sc);
116 
117  /* start receive and transmit processes */
120 
121  /* start interrupts */
122  dc_start_intr(sc);
123 }
124 
125 #define MAX_INT_WAIT_CNT 12000
126 void dc_stop(struct channel *sc)
127 {
128  int wcnt;
129 
130  /* stop receive and transmit processes */
133 
134  /* turn off ethernet interrupts */
135  dc_stop_intr(sc);
136 
137  /* wait to ensure the interrupts have been completed */
138  for (wcnt = 0; wcnt < MAX_INT_WAIT_CNT; wcnt++) {
139  udelay(5);
140  if (!sc->interrupt_active)
141  break;
142  }
143  if (wcnt >= MAX_INT_WAIT_CNT)
144  dev_warn(&sc->pdev->dev, "SBE 2T3E3: Interrupt active too long\n");
145 
146  /* clear all receive/transmit data */
148 }
149 
150 void dc_start_intr(struct channel *sc)
151 {
152  if (sc->p.loopback == SBE_2T3E3_LOOPBACK_NONE && sc->s.OOF)
153  return;
154 
155  if (sc->p.receiver_on || sc->p.transmitter_on) {
156  if (!sc->ether.interrupt_enable_mask)
157  dc_write(sc->addr, SBE_2T3E3_21143_REG_STATUS, 0xFFFFFFFF);
158 
159  sc->ether.interrupt_enable_mask =
169 
171  sc->ether.interrupt_enable_mask);
172  }
173 }
174 
175 void dc_stop_intr(struct channel *sc)
176 {
177  sc->ether.interrupt_enable_mask = 0;
178  dc_write(sc->addr, SBE_2T3E3_21143_REG_INTERRUPT_ENABLE, 0);
179 }
180 
181 void dc_reset(struct channel *sc)
182 {
183  /* turn off ethernet interrupts */
184  dc_write(sc->addr, SBE_2T3E3_21143_REG_INTERRUPT_ENABLE, 0);
185  dc_write(sc->addr, SBE_2T3E3_21143_REG_STATUS, 0xFFFFFFFF);
186 
187  /* software reset */
188  dc_set_bits(sc->addr, SBE_2T3E3_21143_REG_BUS_MODE,
190  udelay(4); /* 50 PCI cycles < 2us */
191 
192  /* clear hardware configuration */
193  dc_write(sc->addr, SBE_2T3E3_21143_REG_BUS_MODE, 0);
194 
195  /* clear software configuration */
196  dc_write(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE, 0);
197 
198  /* turn off SIA reset */
203 }
204 
205 
207 {
208  u32 i, state = 0;
209 
210  if (sc->p.receiver_on == mode)
211  return;
212 
213  switch (mode) {
214  case SBE_2T3E3_OFF:
215  if (dc_read(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE) &
217  dc_clear_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
218  SBE_2T3E3_21143_VAL_RECEIVE_START);
219 
220  for (i = 0; i < 16; i++) {
221  state = dc_read(sc->addr, SBE_2T3E3_21143_REG_STATUS) &
223  if (state == SBE_2T3E3_21143_VAL_RX_STOPPED)
224  break;
225  udelay(5);
226  }
227  if (state != SBE_2T3E3_21143_VAL_RX_STOPPED)
228  dev_warn(&sc->pdev->dev, "SBE 2T3E3: Rx failed to stop\n");
229  else
230  dev_info(&sc->pdev->dev, "SBE 2T3E3: Rx off\n");
231  }
232  break;
233  case SBE_2T3E3_ON:
234  dc_set_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
235  SBE_2T3E3_21143_VAL_RECEIVE_START);
236  udelay(100);
237  dc_write(sc->addr, SBE_2T3E3_21143_REG_RECEIVE_POLL_DEMAND, 0xFFFFFFFF);
238  break;
239  default:
240  return;
241  }
242 
243  sc->p.receiver_on = mode;
244 }
245 
247 {
248  u32 i, state = 0;
249 
250  if (sc->p.transmitter_on == mode)
251  return;
252 
253  switch (mode) {
254  case SBE_2T3E3_OFF:
255  if (dc_read(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE) &
257  dc_clear_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
258  SBE_2T3E3_21143_VAL_TRANSMISSION_START);
259 
260  for (i = 0; i < 16; i++) {
261  state = dc_read(sc->addr, SBE_2T3E3_21143_REG_STATUS) &
263  if (state == SBE_2T3E3_21143_VAL_TX_STOPPED)
264  break;
265  udelay(5);
266  }
267  if (state != SBE_2T3E3_21143_VAL_TX_STOPPED)
268  dev_warn(&sc->pdev->dev, "SBE 2T3E3: Tx failed to stop\n");
269  }
270  break;
271  case SBE_2T3E3_ON:
272  dc_set_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
273  SBE_2T3E3_21143_VAL_TRANSMISSION_START);
274  udelay(100);
275  dc_write(sc->addr, SBE_2T3E3_21143_REG_TRANSMIT_POLL_DEMAND, 0xFFFFFFFF);
276  break;
277  default:
278  return;
279  }
280 
281  sc->p.transmitter_on = mode;
282 }
283 
284 
285 
287 {
288  u32 val;
289 
290  switch (mode) {
293  break;
294  default:
295  return;
296  }
297 
298  /* select loopback mode */
299  val = dc_read(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE) &
301  val |= mode;
302  dc_write(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE, val);
303 
305  dc_set_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
307  else
308  dc_clear_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
310 }
311 
312 static int dc_init_descriptor_list(struct channel *sc)
313 {
314  u32 i, j;
315  struct sk_buff *m;
316 
317  if (sc->ether.rx_ring == NULL)
318  sc->ether.rx_ring = kzalloc(SBE_2T3E3_RX_DESC_RING_SIZE *
319  sizeof(t3e3_rx_desc_t), GFP_KERNEL);
320  if (sc->ether.rx_ring == NULL) {
321  dev_err(&sc->pdev->dev, "SBE 2T3E3: no buffer space for RX ring\n");
322  return -ENOMEM;
323  }
324 
325  if (sc->ether.tx_ring == NULL)
326  sc->ether.tx_ring = kzalloc(SBE_2T3E3_TX_DESC_RING_SIZE *
327  sizeof(t3e3_tx_desc_t), GFP_KERNEL);
328  if (sc->ether.tx_ring == NULL) {
329  kfree(sc->ether.rx_ring);
330  sc->ether.rx_ring = NULL;
331  dev_err(&sc->pdev->dev, "SBE 2T3E3: no buffer space for RX ring\n");
332  return -ENOMEM;
333  }
334 
335 
336  /*
337  * Receive ring
338  */
339  for (i = 0; i < SBE_2T3E3_RX_DESC_RING_SIZE; i++) {
340  sc->ether.rx_ring[i].rdes0 = SBE_2T3E3_RX_DESC_21143_OWN;
341  sc->ether.rx_ring[i].rdes1 =
343 
344  if (sc->ether.rx_data[i] == NULL) {
345  if (!(m = dev_alloc_skb(MCLBYTES))) {
346  for (j = 0; j < i; j++) {
347  dev_kfree_skb_any(sc->ether.rx_data[j]);
348  sc->ether.rx_data[j] = NULL;
349  }
350  kfree(sc->ether.rx_ring);
351  sc->ether.rx_ring = NULL;
352  kfree(sc->ether.tx_ring);
353  sc->ether.tx_ring = NULL;
354  dev_err(&sc->pdev->dev, "SBE 2T3E3: token_alloc err:"
355  " no buffer space for RX ring\n");
356  return -ENOBUFS;
357  }
358  sc->ether.rx_data[i] = m;
359  }
360  sc->ether.rx_ring[i].rdes2 = virt_to_phys(sc->ether.rx_data[i]->data);
361 
362  sc->ether.rx_ring[i].rdes3 = virt_to_phys(
363  &sc->ether.rx_ring[(i + 1) % SBE_2T3E3_RX_DESC_RING_SIZE]);
364  }
365  sc->ether.rx_ring[SBE_2T3E3_RX_DESC_RING_SIZE - 1].rdes1 |=
367  sc->ether.rx_ring_current_read = 0;
368 
370  virt_to_phys(&sc->ether.rx_ring[0]));
371 
372  /*
373  * Transmit ring
374  */
375  for (i = 0; i < SBE_2T3E3_TX_DESC_RING_SIZE; i++) {
376  sc->ether.tx_ring[i].tdes0 = 0;
377  sc->ether.tx_ring[i].tdes1 = SBE_2T3E3_TX_DESC_SECOND_ADDRESS_CHAINED |
379 
380  sc->ether.tx_ring[i].tdes2 = 0;
381  sc->ether.tx_data[i] = NULL;
382 
383  sc->ether.tx_ring[i].tdes3 = virt_to_phys(
384  &sc->ether.tx_ring[(i + 1) % SBE_2T3E3_TX_DESC_RING_SIZE]);
385  }
386  sc->ether.tx_ring[SBE_2T3E3_TX_DESC_RING_SIZE - 1].tdes1 |=
388 
390  virt_to_phys(&sc->ether.tx_ring[0]));
391  sc->ether.tx_ring_current_read = 0;
392  sc->ether.tx_ring_current_write = 0;
393  sc->ether.tx_free_cnt = SBE_2T3E3_TX_DESC_RING_SIZE;
394  spin_lock_init(&sc->ether.tx_lock);
395 
396  return 0;
397 }
398 
400 {
401  u32 i;
402 
403  /* clear CSR3 and CSR4 */
406 
407  /* free all data buffers on TX ring */
408  for (i = 0; i < SBE_2T3E3_TX_DESC_RING_SIZE; i++) {
409  if (sc->ether.tx_data[i] != NULL) {
410  dev_kfree_skb_any(sc->ether.tx_data[i]);
411  sc->ether.tx_data[i] = NULL;
412  }
413  }
414 }
415 
417 {
418  u32 i;
419 
421 
422  /* free all data buffers on RX ring */
423  for (i = 0; i < SBE_2T3E3_RX_DESC_RING_SIZE; i++) {
424  if (sc->ether.rx_data[i] != NULL) {
425  dev_kfree_skb_any(sc->ether.rx_data[i]);
426  sc->ether.rx_data[i] = NULL;
427  }
428  }
429 
430  kfree(sc->ether.rx_ring);
431  sc->ether.rx_ring = NULL;
432  kfree(sc->ether.tx_ring);
433  sc->ether.tx_ring = NULL;
434 }
435 
436 
437 void dc_set_output_port(struct channel *sc)
438 {
439  dc_clear_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
441 
442  dc_write(sc->addr, SBE_2T3E3_21143_REG_SIA_STATUS, 0x00000301);
443  dc_write(sc->addr, SBE_2T3E3_21143_REG_SIA_CONNECTIVITY, 0);
445  dc_write(sc->addr, SBE_2T3E3_21143_REG_SIA_AND_GENERAL_PURPOSE_PORT, 0x08000011);
446 
447  dc_set_bits(sc->addr, SBE_2T3E3_21143_REG_OPERATION_MODE,
452 }
453 
454 void dc_restart(struct channel *sc)
455 {
456  dev_warn(&sc->pdev->dev, "SBE 2T3E3: 21143 restart\n");
457 
458  dc_stop(sc);
459  dc_reset(sc);
460  dc_init(sc); /* stop + reset + init */
461  dc_start(sc);
462 }