Commit | Line | Data |
---|---|---|
f64f8808 VB |
1 | /* Altera TSE SGDMA and MSGDMA Linux driver |
2 | * Copyright (C) 2014 Altera Corporation. All rights reserved | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | */ | |
16 | ||
17 | #include <linux/list.h> | |
18 | #include "altera_utils.h" | |
19 | #include "altera_tse.h" | |
20 | #include "altera_sgdmahw.h" | |
21 | #include "altera_sgdma.h" | |
22 | ||
23 | static void sgdma_descrip(struct sgdma_descrip *desc, | |
24 | struct sgdma_descrip *ndesc, | |
25 | dma_addr_t ndesc_phys, | |
26 | dma_addr_t raddr, | |
27 | dma_addr_t waddr, | |
28 | u16 length, | |
29 | int generate_eop, | |
30 | int rfixed, | |
31 | int wfixed); | |
32 | ||
33 | static int sgdma_async_write(struct altera_tse_private *priv, | |
34 | struct sgdma_descrip *desc); | |
35 | ||
36 | static int sgdma_async_read(struct altera_tse_private *priv); | |
37 | ||
38 | static dma_addr_t | |
39 | sgdma_txphysaddr(struct altera_tse_private *priv, | |
40 | struct sgdma_descrip *desc); | |
41 | ||
42 | static dma_addr_t | |
43 | sgdma_rxphysaddr(struct altera_tse_private *priv, | |
44 | struct sgdma_descrip *desc); | |
45 | ||
46 | static int sgdma_txbusy(struct altera_tse_private *priv); | |
47 | ||
48 | static int sgdma_rxbusy(struct altera_tse_private *priv); | |
49 | ||
50 | static void | |
51 | queue_tx(struct altera_tse_private *priv, struct tse_buffer *buffer); | |
52 | ||
53 | static void | |
54 | queue_rx(struct altera_tse_private *priv, struct tse_buffer *buffer); | |
55 | ||
56 | static struct tse_buffer * | |
57 | dequeue_tx(struct altera_tse_private *priv); | |
58 | ||
59 | static struct tse_buffer * | |
60 | dequeue_rx(struct altera_tse_private *priv); | |
61 | ||
62 | static struct tse_buffer * | |
63 | queue_rx_peekhead(struct altera_tse_private *priv); | |
64 | ||
65 | int sgdma_initialize(struct altera_tse_private *priv) | |
66 | { | |
67 | priv->txctrlreg = SGDMA_CTRLREG_ILASTD; | |
68 | ||
69 | priv->rxctrlreg = SGDMA_CTRLREG_IDESCRIP | | |
70 | SGDMA_CTRLREG_ILASTD; | |
71 | ||
72 | INIT_LIST_HEAD(&priv->txlisthd); | |
73 | INIT_LIST_HEAD(&priv->rxlisthd); | |
74 | ||
75 | priv->rxdescphys = (dma_addr_t) 0; | |
76 | priv->txdescphys = (dma_addr_t) 0; | |
77 | ||
78 | priv->rxdescphys = dma_map_single(priv->device, priv->rx_dma_desc, | |
79 | priv->rxdescmem, DMA_BIDIRECTIONAL); | |
80 | ||
81 | if (dma_mapping_error(priv->device, priv->rxdescphys)) { | |
82 | sgdma_uninitialize(priv); | |
83 | netdev_err(priv->dev, "error mapping rx descriptor memory\n"); | |
84 | return -EINVAL; | |
85 | } | |
86 | ||
80175f93 VB |
87 | priv->txdescphys = dma_map_single(priv->device, priv->tx_dma_desc, |
88 | priv->txdescmem, DMA_TO_DEVICE); | |
f64f8808 VB |
89 | |
90 | if (dma_mapping_error(priv->device, priv->txdescphys)) { | |
91 | sgdma_uninitialize(priv); | |
92 | netdev_err(priv->dev, "error mapping tx descriptor memory\n"); | |
93 | return -EINVAL; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | void sgdma_uninitialize(struct altera_tse_private *priv) | |
100 | { | |
101 | if (priv->rxdescphys) | |
102 | dma_unmap_single(priv->device, priv->rxdescphys, | |
103 | priv->rxdescmem, DMA_BIDIRECTIONAL); | |
104 | ||
105 | if (priv->txdescphys) | |
106 | dma_unmap_single(priv->device, priv->txdescphys, | |
107 | priv->txdescmem, DMA_TO_DEVICE); | |
108 | } | |
109 | ||
110 | /* This function resets the SGDMA controller and clears the | |
111 | * descriptor memory used for transmits and receives. | |
112 | */ | |
113 | void sgdma_reset(struct altera_tse_private *priv) | |
114 | { | |
d42f157b | 115 | u32 *ptxdescripmem = priv->tx_dma_desc; |
f64f8808 | 116 | u32 txdescriplen = priv->txdescmem; |
d42f157b | 117 | u32 *prxdescripmem = priv->rx_dma_desc; |
f64f8808 | 118 | u32 rxdescriplen = priv->rxdescmem; |
d42f157b TK |
119 | struct sgdma_csr *ptxsgdma = priv->tx_dma_csr; |
120 | struct sgdma_csr *prxsgdma = priv->rx_dma_csr; | |
f64f8808 VB |
121 | |
122 | /* Initialize descriptor memory to 0 */ | |
123 | memset(ptxdescripmem, 0, txdescriplen); | |
124 | memset(prxdescripmem, 0, rxdescriplen); | |
125 | ||
126 | iowrite32(SGDMA_CTRLREG_RESET, &ptxsgdma->control); | |
127 | iowrite32(0, &ptxsgdma->control); | |
128 | ||
129 | iowrite32(SGDMA_CTRLREG_RESET, &prxsgdma->control); | |
130 | iowrite32(0, &prxsgdma->control); | |
131 | } | |
132 | ||
133 | void sgdma_enable_rxirq(struct altera_tse_private *priv) | |
134 | { | |
d42f157b | 135 | struct sgdma_csr *csr = priv->rx_dma_csr; |
f64f8808 VB |
136 | priv->rxctrlreg |= SGDMA_CTRLREG_INTEN; |
137 | tse_set_bit(&csr->control, SGDMA_CTRLREG_INTEN); | |
138 | } | |
139 | ||
140 | void sgdma_enable_txirq(struct altera_tse_private *priv) | |
141 | { | |
d42f157b | 142 | struct sgdma_csr *csr = priv->tx_dma_csr; |
f64f8808 VB |
143 | priv->txctrlreg |= SGDMA_CTRLREG_INTEN; |
144 | tse_set_bit(&csr->control, SGDMA_CTRLREG_INTEN); | |
145 | } | |
146 | ||
147 | /* for SGDMA, RX interrupts remain enabled after enabling */ | |
148 | void sgdma_disable_rxirq(struct altera_tse_private *priv) | |
149 | { | |
150 | } | |
151 | ||
152 | /* for SGDMA, TX interrupts remain enabled after enabling */ | |
153 | void sgdma_disable_txirq(struct altera_tse_private *priv) | |
154 | { | |
155 | } | |
156 | ||
157 | void sgdma_clear_rxirq(struct altera_tse_private *priv) | |
158 | { | |
d42f157b | 159 | struct sgdma_csr *csr = priv->rx_dma_csr; |
f64f8808 VB |
160 | tse_set_bit(&csr->control, SGDMA_CTRLREG_CLRINT); |
161 | } | |
162 | ||
163 | void sgdma_clear_txirq(struct altera_tse_private *priv) | |
164 | { | |
d42f157b | 165 | struct sgdma_csr *csr = priv->tx_dma_csr; |
f64f8808 VB |
166 | tse_set_bit(&csr->control, SGDMA_CTRLREG_CLRINT); |
167 | } | |
168 | ||
169 | /* transmits buffer through SGDMA. Returns number of buffers | |
170 | * transmitted, 0 if not possible. | |
171 | * | |
172 | * tx_lock is held by the caller | |
173 | */ | |
174 | int sgdma_tx_buffer(struct altera_tse_private *priv, struct tse_buffer *buffer) | |
175 | { | |
176 | int pktstx = 0; | |
d42f157b | 177 | struct sgdma_descrip *descbase = priv->tx_dma_desc; |
f64f8808 VB |
178 | |
179 | struct sgdma_descrip *cdesc = &descbase[0]; | |
180 | struct sgdma_descrip *ndesc = &descbase[1]; | |
181 | ||
182 | /* wait 'til the tx sgdma is ready for the next transmit request */ | |
183 | if (sgdma_txbusy(priv)) | |
184 | return 0; | |
185 | ||
186 | sgdma_descrip(cdesc, /* current descriptor */ | |
187 | ndesc, /* next descriptor */ | |
188 | sgdma_txphysaddr(priv, ndesc), | |
189 | buffer->dma_addr, /* address of packet to xmit */ | |
190 | 0, /* write addr 0 for tx dma */ | |
191 | buffer->len, /* length of packet */ | |
192 | SGDMA_CONTROL_EOP, /* Generate EOP */ | |
193 | 0, /* read fixed */ | |
194 | SGDMA_CONTROL_WR_FIXED); /* Generate SOP */ | |
195 | ||
196 | pktstx = sgdma_async_write(priv, cdesc); | |
197 | ||
198 | /* enqueue the request to the pending transmit queue */ | |
199 | queue_tx(priv, buffer); | |
200 | ||
201 | return 1; | |
202 | } | |
203 | ||
204 | ||
205 | /* tx_lock held to protect access to queued tx list | |
206 | */ | |
207 | u32 sgdma_tx_completions(struct altera_tse_private *priv) | |
208 | { | |
209 | u32 ready = 0; | |
d42f157b | 210 | struct sgdma_descrip *desc = priv->tx_dma_desc; |
f64f8808 VB |
211 | |
212 | if (!sgdma_txbusy(priv) && | |
213 | ((desc->control & SGDMA_CONTROL_HW_OWNED) == 0) && | |
214 | (dequeue_tx(priv))) { | |
215 | ready = 1; | |
216 | } | |
217 | ||
218 | return ready; | |
219 | } | |
220 | ||
221 | int sgdma_add_rx_desc(struct altera_tse_private *priv, | |
222 | struct tse_buffer *rxbuffer) | |
223 | { | |
224 | queue_rx(priv, rxbuffer); | |
225 | return sgdma_async_read(priv); | |
226 | } | |
227 | ||
228 | /* status is returned on upper 16 bits, | |
229 | * length is returned in lower 16 bits | |
230 | */ | |
231 | u32 sgdma_rx_status(struct altera_tse_private *priv) | |
232 | { | |
d42f157b TK |
233 | struct sgdma_csr *csr = priv->rx_dma_csr; |
234 | struct sgdma_descrip *base = priv->rx_dma_desc; | |
f64f8808 VB |
235 | struct sgdma_descrip *desc = NULL; |
236 | int pktsrx; | |
237 | unsigned int rxstatus = 0; | |
238 | unsigned int pktlength = 0; | |
239 | unsigned int pktstatus = 0; | |
240 | struct tse_buffer *rxbuffer = NULL; | |
241 | ||
242 | dma_sync_single_for_cpu(priv->device, | |
243 | priv->rxdescphys, | |
244 | priv->rxdescmem, | |
245 | DMA_BIDIRECTIONAL); | |
246 | ||
247 | desc = &base[0]; | |
248 | if ((ioread32(&csr->status) & SGDMA_STSREG_EOP) || | |
249 | (desc->status & SGDMA_STATUS_EOP)) { | |
250 | pktlength = desc->bytes_xferred; | |
251 | pktstatus = desc->status & 0x3f; | |
252 | rxstatus = pktstatus; | |
253 | rxstatus = rxstatus << 16; | |
254 | rxstatus |= (pktlength & 0xffff); | |
255 | ||
256 | desc->status = 0; | |
257 | ||
258 | rxbuffer = dequeue_rx(priv); | |
259 | if (rxbuffer == NULL) | |
260 | netdev_err(priv->dev, | |
261 | "sgdma rx and rx queue empty!\n"); | |
262 | ||
263 | /* kick the rx sgdma after reaping this descriptor */ | |
264 | pktsrx = sgdma_async_read(priv); | |
265 | } | |
266 | ||
267 | return rxstatus; | |
268 | } | |
269 | ||
270 | ||
271 | /* Private functions */ | |
272 | static void sgdma_descrip(struct sgdma_descrip *desc, | |
273 | struct sgdma_descrip *ndesc, | |
274 | dma_addr_t ndesc_phys, | |
275 | dma_addr_t raddr, | |
276 | dma_addr_t waddr, | |
277 | u16 length, | |
278 | int generate_eop, | |
279 | int rfixed, | |
280 | int wfixed) | |
281 | { | |
282 | /* Clear the next descriptor as not owned by hardware */ | |
283 | u32 ctrl = ndesc->control; | |
284 | ctrl &= ~SGDMA_CONTROL_HW_OWNED; | |
285 | ndesc->control = ctrl; | |
286 | ||
287 | ctrl = 0; | |
288 | ctrl = SGDMA_CONTROL_HW_OWNED; | |
289 | ctrl |= generate_eop; | |
290 | ctrl |= rfixed; | |
291 | ctrl |= wfixed; | |
292 | ||
293 | /* Channel is implicitly zero, initialized to 0 by default */ | |
294 | ||
295 | desc->raddr = raddr; | |
296 | desc->waddr = waddr; | |
297 | desc->next = lower_32_bits(ndesc_phys); | |
298 | desc->control = ctrl; | |
299 | desc->status = 0; | |
300 | desc->rburst = 0; | |
301 | desc->wburst = 0; | |
302 | desc->bytes = length; | |
303 | desc->bytes_xferred = 0; | |
304 | } | |
305 | ||
306 | /* If hardware is busy, don't restart async read. | |
307 | * if status register is 0 - meaning initial state, restart async read, | |
308 | * probably for the first time when populating a receive buffer. | |
309 | * If read status indicate not busy and a status, restart the async | |
310 | * DMA read. | |
311 | */ | |
312 | static int sgdma_async_read(struct altera_tse_private *priv) | |
313 | { | |
d42f157b TK |
314 | struct sgdma_csr *csr = priv->rx_dma_csr; |
315 | struct sgdma_descrip *descbase = priv->rx_dma_desc; | |
f64f8808 VB |
316 | struct sgdma_descrip *cdesc = &descbase[0]; |
317 | struct sgdma_descrip *ndesc = &descbase[1]; | |
318 | ||
319 | unsigned int sts = ioread32(&csr->status); | |
320 | struct tse_buffer *rxbuffer = NULL; | |
321 | ||
322 | if (!sgdma_rxbusy(priv)) { | |
323 | rxbuffer = queue_rx_peekhead(priv); | |
324 | if (rxbuffer == NULL) | |
325 | return 0; | |
326 | ||
327 | sgdma_descrip(cdesc, /* current descriptor */ | |
328 | ndesc, /* next descriptor */ | |
329 | sgdma_rxphysaddr(priv, ndesc), | |
330 | 0, /* read addr 0 for rx dma */ | |
331 | rxbuffer->dma_addr, /* write addr for rx dma */ | |
332 | 0, /* read 'til EOP */ | |
333 | 0, /* EOP: NA for rx dma */ | |
334 | 0, /* read fixed: NA for rx dma */ | |
335 | 0); /* SOP: NA for rx DMA */ | |
336 | ||
337 | /* clear control and status */ | |
338 | iowrite32(0, &csr->control); | |
339 | ||
80175f93 | 340 | /* If status available, clear those bits */ |
f64f8808 VB |
341 | if (sts & 0xf) |
342 | iowrite32(0xf, &csr->status); | |
343 | ||
344 | dma_sync_single_for_device(priv->device, | |
345 | priv->rxdescphys, | |
346 | priv->rxdescmem, | |
347 | DMA_BIDIRECTIONAL); | |
348 | ||
349 | iowrite32(lower_32_bits(sgdma_rxphysaddr(priv, cdesc)), | |
350 | &csr->next_descrip); | |
351 | ||
352 | iowrite32((priv->rxctrlreg | SGDMA_CTRLREG_START), | |
353 | &csr->control); | |
354 | ||
355 | return 1; | |
356 | } | |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | static int sgdma_async_write(struct altera_tse_private *priv, | |
362 | struct sgdma_descrip *desc) | |
363 | { | |
d42f157b | 364 | struct sgdma_csr *csr = priv->tx_dma_csr; |
f64f8808 VB |
365 | |
366 | if (sgdma_txbusy(priv)) | |
367 | return 0; | |
368 | ||
369 | /* clear control and status */ | |
370 | iowrite32(0, &csr->control); | |
371 | iowrite32(0x1f, &csr->status); | |
372 | ||
373 | dma_sync_single_for_device(priv->device, priv->txdescphys, | |
374 | priv->txdescmem, DMA_TO_DEVICE); | |
375 | ||
376 | iowrite32(lower_32_bits(sgdma_txphysaddr(priv, desc)), | |
377 | &csr->next_descrip); | |
378 | ||
379 | iowrite32((priv->txctrlreg | SGDMA_CTRLREG_START), | |
380 | &csr->control); | |
381 | ||
382 | return 1; | |
383 | } | |
384 | ||
385 | static dma_addr_t | |
386 | sgdma_txphysaddr(struct altera_tse_private *priv, | |
387 | struct sgdma_descrip *desc) | |
388 | { | |
389 | dma_addr_t paddr = priv->txdescmem_busaddr; | |
a804ad0e VB |
390 | uintptr_t offs = (uintptr_t)desc - (uintptr_t)priv->tx_dma_desc; |
391 | return (dma_addr_t)((uintptr_t)paddr + offs); | |
f64f8808 VB |
392 | } |
393 | ||
394 | static dma_addr_t | |
395 | sgdma_rxphysaddr(struct altera_tse_private *priv, | |
396 | struct sgdma_descrip *desc) | |
397 | { | |
398 | dma_addr_t paddr = priv->rxdescmem_busaddr; | |
a804ad0e VB |
399 | uintptr_t offs = (uintptr_t)desc - (uintptr_t)priv->rx_dma_desc; |
400 | return (dma_addr_t)((uintptr_t)paddr + offs); | |
f64f8808 VB |
401 | } |
402 | ||
403 | #define list_remove_head(list, entry, type, member) \ | |
404 | do { \ | |
405 | entry = NULL; \ | |
406 | if (!list_empty(list)) { \ | |
407 | entry = list_entry((list)->next, type, member); \ | |
408 | list_del_init(&entry->member); \ | |
409 | } \ | |
410 | } while (0) | |
411 | ||
412 | #define list_peek_head(list, entry, type, member) \ | |
413 | do { \ | |
414 | entry = NULL; \ | |
415 | if (!list_empty(list)) { \ | |
416 | entry = list_entry((list)->next, type, member); \ | |
417 | } \ | |
418 | } while (0) | |
419 | ||
420 | /* adds a tse_buffer to the tail of a tx buffer list. | |
421 | * assumes the caller is managing and holding a mutual exclusion | |
422 | * primitive to avoid simultaneous pushes/pops to the list. | |
423 | */ | |
424 | static void | |
425 | queue_tx(struct altera_tse_private *priv, struct tse_buffer *buffer) | |
426 | { | |
427 | list_add_tail(&buffer->lh, &priv->txlisthd); | |
428 | } | |
429 | ||
430 | ||
431 | /* adds a tse_buffer to the tail of a rx buffer list | |
432 | * assumes the caller is managing and holding a mutual exclusion | |
433 | * primitive to avoid simultaneous pushes/pops to the list. | |
434 | */ | |
435 | static void | |
436 | queue_rx(struct altera_tse_private *priv, struct tse_buffer *buffer) | |
437 | { | |
438 | list_add_tail(&buffer->lh, &priv->rxlisthd); | |
439 | } | |
440 | ||
441 | /* dequeues a tse_buffer from the transmit buffer list, otherwise | |
442 | * returns NULL if empty. | |
443 | * assumes the caller is managing and holding a mutual exclusion | |
444 | * primitive to avoid simultaneous pushes/pops to the list. | |
445 | */ | |
446 | static struct tse_buffer * | |
447 | dequeue_tx(struct altera_tse_private *priv) | |
448 | { | |
449 | struct tse_buffer *buffer = NULL; | |
450 | list_remove_head(&priv->txlisthd, buffer, struct tse_buffer, lh); | |
451 | return buffer; | |
452 | } | |
453 | ||
454 | /* dequeues a tse_buffer from the receive buffer list, otherwise | |
455 | * returns NULL if empty | |
456 | * assumes the caller is managing and holding a mutual exclusion | |
457 | * primitive to avoid simultaneous pushes/pops to the list. | |
458 | */ | |
459 | static struct tse_buffer * | |
460 | dequeue_rx(struct altera_tse_private *priv) | |
461 | { | |
462 | struct tse_buffer *buffer = NULL; | |
463 | list_remove_head(&priv->rxlisthd, buffer, struct tse_buffer, lh); | |
464 | return buffer; | |
465 | } | |
466 | ||
467 | /* dequeues a tse_buffer from the receive buffer list, otherwise | |
468 | * returns NULL if empty | |
469 | * assumes the caller is managing and holding a mutual exclusion | |
470 | * primitive to avoid simultaneous pushes/pops to the list while the | |
471 | * head is being examined. | |
472 | */ | |
473 | static struct tse_buffer * | |
474 | queue_rx_peekhead(struct altera_tse_private *priv) | |
475 | { | |
476 | struct tse_buffer *buffer = NULL; | |
477 | list_peek_head(&priv->rxlisthd, buffer, struct tse_buffer, lh); | |
478 | return buffer; | |
479 | } | |
480 | ||
481 | /* check and return rx sgdma status without polling | |
482 | */ | |
483 | static int sgdma_rxbusy(struct altera_tse_private *priv) | |
484 | { | |
d42f157b | 485 | struct sgdma_csr *csr = priv->rx_dma_csr; |
f64f8808 VB |
486 | return ioread32(&csr->status) & SGDMA_STSREG_BUSY; |
487 | } | |
488 | ||
489 | /* waits for the tx sgdma to finish it's current operation, returns 0 | |
490 | * when it transitions to nonbusy, returns 1 if the operation times out | |
491 | */ | |
492 | static int sgdma_txbusy(struct altera_tse_private *priv) | |
493 | { | |
494 | int delay = 0; | |
d42f157b | 495 | struct sgdma_csr *csr = priv->tx_dma_csr; |
f64f8808 VB |
496 | |
497 | /* if DMA is busy, wait for current transactino to finish */ | |
498 | while ((ioread32(&csr->status) & SGDMA_STSREG_BUSY) && (delay++ < 100)) | |
499 | udelay(1); | |
500 | ||
501 | if (ioread32(&csr->status) & SGDMA_STSREG_BUSY) { | |
502 | netdev_err(priv->dev, "timeout waiting for tx dma\n"); | |
503 | return 1; | |
504 | } | |
505 | return 0; | |
506 | } |