1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Freescale QUICC Engine HDLC Device Driver
4 * Copyright 2016 Freescale Semiconductor Inc.
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/hdlc.h>
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/skbuff.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/stddef.h>
26 #include <soc/fsl/qe/qe_tdm.h>
27 #include <uapi/linux/if_arp.h>
29 #include "fsl_ucc_hdlc.h"
31 #define DRV_DESC "Freescale QE UCC HDLC Driver"
32 #define DRV_NAME "ucc_hdlc"
34 #define TDM_PPPOHT_SLIC_MAXIN
35 #define RX_BD_ERRORS (R_CD_S | R_OV_S | R_CR_S | R_AB_S | R_NO_S | R_LG_S)
37 static int uhdlc_close(struct net_device
*dev
);
39 static struct ucc_tdm_info utdm_primary_info
= {
54 .mode
= UCC_FAST_PROTOCOL_MODE_HDLC
,
55 .ttx_trx
= UCC_FAST_GUMR_TRANSPARENT_TTX_TRX_NORMAL
,
56 .tenc
= UCC_FAST_TX_ENCODING_NRZ
,
57 .renc
= UCC_FAST_RX_ENCODING_NRZ
,
58 .tcrc
= UCC_FAST_16_BIT_CRC
,
59 .synl
= UCC_FAST_SYNC_LEN_NOT_USED
,
63 #ifdef TDM_PPPOHT_SLIC_MAXIN
78 static struct ucc_tdm_info utdm_info
[UCC_MAX_NUM
];
80 static int uhdlc_init(struct ucc_hdlc_private
*priv
)
82 struct ucc_tdm_info
*ut_info
;
83 struct ucc_fast_info
*uf_info
;
88 dma_addr_t bd_dma_addr
;
93 ut_info
= priv
->ut_info
;
94 uf_info
= &ut_info
->uf_info
;
107 /* This sets HPM register in CMXUCR register which configures a
108 * open drain connected HDLC bus
111 uf_info
->brkpt_support
= 1;
113 uf_info
->uccm_mask
= ((UCC_HDLC_UCCE_RXB
| UCC_HDLC_UCCE_RXF
|
114 UCC_HDLC_UCCE_TXB
) << 16);
116 ret
= ucc_fast_init(uf_info
, &priv
->uccf
);
118 dev_err(priv
->dev
, "Failed to init uccf.");
122 priv
->uf_regs
= priv
->uccf
->uf_regs
;
123 ucc_fast_disable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
126 if (priv
->loopback
) {
127 dev_info(priv
->dev
, "Loopback Mode\n");
128 /* use the same clock when work in loopback */
129 qe_setbrg(ut_info
->uf_info
.rx_clock
, 20000000, 1);
131 gumr
= ioread32be(&priv
->uf_regs
->gumr
);
132 gumr
|= (UCC_FAST_GUMR_LOOPBACK
| UCC_FAST_GUMR_CDS
|
134 gumr
&= ~(UCC_FAST_GUMR_CTSP
| UCC_FAST_GUMR_RSYN
);
135 iowrite32be(gumr
, &priv
->uf_regs
->gumr
);
140 ucc_tdm_init(priv
->utdm
, priv
->ut_info
);
142 /* Write to QE CECR, UCCx channel to Stop Transmission */
143 cecr_subblock
= ucc_fast_get_qe_cr_subblock(uf_info
->ucc_num
);
144 ret
= qe_issue_cmd(QE_STOP_TX
, cecr_subblock
,
145 QE_CR_PROTOCOL_UNSPECIFIED
, 0);
147 /* Set UPSMR normal mode (need fixed)*/
148 iowrite32be(0, &priv
->uf_regs
->upsmr
);
151 if (priv
->hdlc_bus
) {
154 dev_info(priv
->dev
, "HDLC bus Mode\n");
155 upsmr
= ioread32be(&priv
->uf_regs
->upsmr
);
157 /* bus mode and retransmit enable, with collision window
160 upsmr
|= UCC_HDLC_UPSMR_RTE
| UCC_HDLC_UPSMR_BUS
|
162 iowrite32be(upsmr
, &priv
->uf_regs
->upsmr
);
164 /* explicitly disable CDS & CTSP */
165 gumr
= ioread32be(&priv
->uf_regs
->gumr
);
166 gumr
&= ~(UCC_FAST_GUMR_CDS
| UCC_FAST_GUMR_CTSP
);
167 /* set automatic sync to explicitly ignore CD signal */
168 gumr
|= UCC_FAST_GUMR_SYNL_AUTO
;
169 iowrite32be(gumr
, &priv
->uf_regs
->gumr
);
172 priv
->rx_ring_size
= RX_BD_RING_LEN
;
173 priv
->tx_ring_size
= TX_BD_RING_LEN
;
175 priv
->rx_bd_base
= dma_alloc_coherent(priv
->dev
,
176 RX_BD_RING_LEN
* sizeof(struct qe_bd
),
177 &priv
->dma_rx_bd
, GFP_KERNEL
);
179 if (!priv
->rx_bd_base
) {
180 dev_err(priv
->dev
, "Cannot allocate MURAM memory for RxBDs\n");
186 priv
->tx_bd_base
= dma_alloc_coherent(priv
->dev
,
187 TX_BD_RING_LEN
* sizeof(struct qe_bd
),
188 &priv
->dma_tx_bd
, GFP_KERNEL
);
190 if (!priv
->tx_bd_base
) {
191 dev_err(priv
->dev
, "Cannot allocate MURAM memory for TxBDs\n");
196 /* Alloc parameter ram for ucc hdlc */
197 priv
->ucc_pram_offset
= qe_muram_alloc(sizeof(struct ucc_hdlc_param
),
198 ALIGNMENT_OF_UCC_HDLC_PRAM
);
200 if (priv
->ucc_pram_offset
< 0) {
201 dev_err(priv
->dev
, "Can not allocate MURAM for hdlc parameter.\n");
206 priv
->rx_skbuff
= kcalloc(priv
->rx_ring_size
,
207 sizeof(*priv
->rx_skbuff
),
209 if (!priv
->rx_skbuff
) {
214 priv
->tx_skbuff
= kcalloc(priv
->tx_ring_size
,
215 sizeof(*priv
->tx_skbuff
),
217 if (!priv
->tx_skbuff
) {
223 priv
->skb_dirtytx
= 0;
224 priv
->curtx_bd
= priv
->tx_bd_base
;
225 priv
->dirty_tx
= priv
->tx_bd_base
;
226 priv
->currx_bd
= priv
->rx_bd_base
;
227 priv
->currx_bdnum
= 0;
229 /* init parameter base */
230 cecr_subblock
= ucc_fast_get_qe_cr_subblock(uf_info
->ucc_num
);
231 ret
= qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE
, cecr_subblock
,
232 QE_CR_PROTOCOL_UNSPECIFIED
, priv
->ucc_pram_offset
);
234 priv
->ucc_pram
= (struct ucc_hdlc_param __iomem
*)
235 qe_muram_addr(priv
->ucc_pram_offset
);
237 /* Zero out parameter ram */
238 memset_io(priv
->ucc_pram
, 0, sizeof(struct ucc_hdlc_param
));
240 /* Alloc riptr, tiptr */
241 riptr
= qe_muram_alloc(32, 32);
243 dev_err(priv
->dev
, "Cannot allocate MURAM mem for Receive internal temp data pointer\n");
248 tiptr
= qe_muram_alloc(32, 32);
250 dev_err(priv
->dev
, "Cannot allocate MURAM mem for Transmit internal temp data pointer\n");
254 if (riptr
!= (u16
)riptr
|| tiptr
!= (u16
)tiptr
) {
255 dev_err(priv
->dev
, "MURAM allocation out of addressable range\n");
260 /* Set RIPTR, TIPTR */
261 iowrite16be(riptr
, &priv
->ucc_pram
->riptr
);
262 iowrite16be(tiptr
, &priv
->ucc_pram
->tiptr
);
265 iowrite16be(MAX_RX_BUF_LENGTH
, &priv
->ucc_pram
->mrblr
);
267 /* Set RBASE, TBASE */
268 iowrite32be(priv
->dma_rx_bd
, &priv
->ucc_pram
->rbase
);
269 iowrite32be(priv
->dma_tx_bd
, &priv
->ucc_pram
->tbase
);
271 /* Set RSTATE, TSTATE */
272 iowrite32be(BMR_GBL
| BMR_BIG_ENDIAN
, &priv
->ucc_pram
->rstate
);
273 iowrite32be(BMR_GBL
| BMR_BIG_ENDIAN
, &priv
->ucc_pram
->tstate
);
275 /* Set C_MASK, C_PRES for 16bit CRC */
276 iowrite32be(CRC_16BIT_MASK
, &priv
->ucc_pram
->c_mask
);
277 iowrite32be(CRC_16BIT_PRES
, &priv
->ucc_pram
->c_pres
);
279 iowrite16be(MAX_FRAME_LENGTH
, &priv
->ucc_pram
->mflr
);
280 iowrite16be(DEFAULT_RFTHR
, &priv
->ucc_pram
->rfthr
);
281 iowrite16be(DEFAULT_RFTHR
, &priv
->ucc_pram
->rfcnt
);
282 iowrite16be(priv
->hmask
, &priv
->ucc_pram
->hmask
);
283 iowrite16be(DEFAULT_HDLC_ADDR
, &priv
->ucc_pram
->haddr1
);
284 iowrite16be(DEFAULT_HDLC_ADDR
, &priv
->ucc_pram
->haddr2
);
285 iowrite16be(DEFAULT_HDLC_ADDR
, &priv
->ucc_pram
->haddr3
);
286 iowrite16be(DEFAULT_HDLC_ADDR
, &priv
->ucc_pram
->haddr4
);
289 bd_buffer
= dma_alloc_coherent(priv
->dev
,
290 (RX_BD_RING_LEN
+ TX_BD_RING_LEN
) * MAX_RX_BUF_LENGTH
,
291 &bd_dma_addr
, GFP_KERNEL
);
294 dev_err(priv
->dev
, "Could not allocate buffer descriptors\n");
299 priv
->rx_buffer
= bd_buffer
;
300 priv
->tx_buffer
= bd_buffer
+ RX_BD_RING_LEN
* MAX_RX_BUF_LENGTH
;
302 priv
->dma_rx_addr
= bd_dma_addr
;
303 priv
->dma_tx_addr
= bd_dma_addr
+ RX_BD_RING_LEN
* MAX_RX_BUF_LENGTH
;
305 for (i
= 0; i
< RX_BD_RING_LEN
; i
++) {
306 if (i
< (RX_BD_RING_LEN
- 1))
307 bd_status
= R_E_S
| R_I_S
;
309 bd_status
= R_E_S
| R_I_S
| R_W_S
;
311 priv
->rx_bd_base
[i
].status
= cpu_to_be16(bd_status
);
312 priv
->rx_bd_base
[i
].buf
= cpu_to_be32(priv
->dma_rx_addr
+ i
* MAX_RX_BUF_LENGTH
);
315 for (i
= 0; i
< TX_BD_RING_LEN
; i
++) {
316 if (i
< (TX_BD_RING_LEN
- 1))
317 bd_status
= T_I_S
| T_TC_S
;
319 bd_status
= T_I_S
| T_TC_S
| T_W_S
;
321 priv
->tx_bd_base
[i
].status
= cpu_to_be16(bd_status
);
322 priv
->tx_bd_base
[i
].buf
= cpu_to_be32(priv
->dma_tx_addr
+ i
* MAX_RX_BUF_LENGTH
);
329 qe_muram_free(tiptr
);
331 qe_muram_free(riptr
);
333 kfree(priv
->tx_skbuff
);
335 kfree(priv
->rx_skbuff
);
337 qe_muram_free(priv
->ucc_pram_offset
);
339 dma_free_coherent(priv
->dev
,
340 TX_BD_RING_LEN
* sizeof(struct qe_bd
),
341 priv
->tx_bd_base
, priv
->dma_tx_bd
);
343 dma_free_coherent(priv
->dev
,
344 RX_BD_RING_LEN
* sizeof(struct qe_bd
),
345 priv
->rx_bd_base
, priv
->dma_rx_bd
);
347 ucc_fast_free(priv
->uccf
);
352 static netdev_tx_t
ucc_hdlc_tx(struct sk_buff
*skb
, struct net_device
*dev
)
354 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
355 struct ucc_hdlc_private
*priv
= (struct ucc_hdlc_private
*)hdlc
->priv
;
363 if (skb_headroom(skb
) < HDLC_HEAD_LEN
) {
364 dev
->stats
.tx_dropped
++;
366 netdev_err(dev
, "No enough space for hdlc head\n");
370 skb_push(skb
, HDLC_HEAD_LEN
);
372 proto_head
= (__be16
*)skb
->data
;
373 *proto_head
= htons(DEFAULT_HDLC_HEAD
);
375 dev
->stats
.tx_bytes
+= skb
->len
;
379 proto_head
= (__be16
*)skb
->data
;
380 if (*proto_head
!= htons(DEFAULT_PPP_HEAD
)) {
381 dev
->stats
.tx_dropped
++;
383 netdev_err(dev
, "Wrong ppp header\n");
387 dev
->stats
.tx_bytes
+= skb
->len
;
391 dev
->stats
.tx_bytes
+= skb
->len
;
395 dev
->stats
.tx_dropped
++;
399 netdev_sent_queue(dev
, skb
->len
);
400 spin_lock_irqsave(&priv
->lock
, flags
);
403 /* Start from the next BD that should be filled */
405 bd_status
= be16_to_cpu(bd
->status
);
406 /* Save the skb pointer so we can free it later */
407 priv
->tx_skbuff
[priv
->skb_curtx
] = skb
;
409 /* Update the current skb pointer (wrapping if this was the last) */
411 (priv
->skb_curtx
+ 1) & TX_RING_MOD_MASK(TX_BD_RING_LEN
);
413 /* copy skb data to tx buffer for sdma processing */
414 memcpy(priv
->tx_buffer
+ (be32_to_cpu(bd
->buf
) - priv
->dma_tx_addr
),
415 skb
->data
, skb
->len
);
417 /* set bd status and length */
418 bd_status
= (bd_status
& T_W_S
) | T_R_S
| T_I_S
| T_L_S
| T_TC_S
;
420 bd
->length
= cpu_to_be16(skb
->len
);
421 bd
->status
= cpu_to_be16(bd_status
);
423 /* Move to next BD in the ring */
424 if (!(bd_status
& T_W_S
))
427 bd
= priv
->tx_bd_base
;
429 if (bd
== priv
->dirty_tx
) {
430 if (!netif_queue_stopped(dev
))
431 netif_stop_queue(dev
);
436 spin_unlock_irqrestore(&priv
->lock
, flags
);
441 static int hdlc_tx_restart(struct ucc_hdlc_private
*priv
)
446 ucc_fast_get_qe_cr_subblock(priv
->ut_info
->uf_info
.ucc_num
);
448 qe_issue_cmd(QE_RESTART_TX
, cecr_subblock
,
449 QE_CR_PROTOCOL_UNSPECIFIED
, 0);
453 static int hdlc_tx_done(struct ucc_hdlc_private
*priv
)
455 /* Start from the next BD that should be filled */
456 struct net_device
*dev
= priv
->ndev
;
457 unsigned int bytes_sent
= 0;
459 struct qe_bd
*bd
; /* BD pointer */
465 bd_status
= be16_to_cpu(bd
->status
);
467 /* Normal processing. */
468 while ((bd_status
& T_R_S
) == 0) {
471 if (bd_status
& T_UN_S
) { /* Underrun */
472 dev
->stats
.tx_fifo_errors
++;
475 if (bd_status
& T_CT_S
) { /* Carrier lost */
476 dev
->stats
.tx_carrier_errors
++;
480 /* BD contains already transmitted buffer. */
481 /* Handle the transmitted buffer and release */
482 /* the BD to be used with the current frame */
484 skb
= priv
->tx_skbuff
[priv
->skb_dirtytx
];
488 bytes_sent
+= skb
->len
;
489 dev
->stats
.tx_packets
++;
490 memset(priv
->tx_buffer
+
491 (be32_to_cpu(bd
->buf
) - priv
->dma_tx_addr
),
493 dev_consume_skb_irq(skb
);
495 priv
->tx_skbuff
[priv
->skb_dirtytx
] = NULL
;
498 1) & TX_RING_MOD_MASK(TX_BD_RING_LEN
);
500 /* We freed a buffer, so now we can restart transmission */
501 if (netif_queue_stopped(dev
))
502 netif_wake_queue(dev
);
504 /* Advance the confirmation BD pointer */
505 if (!(bd_status
& T_W_S
))
508 bd
= priv
->tx_bd_base
;
509 bd_status
= be16_to_cpu(bd
->status
);
514 hdlc_tx_restart(priv
);
516 netdev_completed_queue(dev
, howmany
, bytes_sent
);
520 static int hdlc_rx_done(struct ucc_hdlc_private
*priv
, int rx_work_limit
)
522 struct net_device
*dev
= priv
->ndev
;
523 struct sk_buff
*skb
= NULL
;
524 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
527 u16 length
, howmany
= 0;
532 bd_status
= be16_to_cpu(bd
->status
);
534 /* while there are received buffers and BD is full (~R_E) */
535 while (!((bd_status
& (R_E_S
)) || (--rx_work_limit
< 0))) {
536 if (bd_status
& (RX_BD_ERRORS
)) {
537 dev
->stats
.rx_errors
++;
539 if (bd_status
& R_CD_S
)
540 dev
->stats
.collisions
++;
541 if (bd_status
& R_OV_S
)
542 dev
->stats
.rx_fifo_errors
++;
543 if (bd_status
& R_CR_S
)
544 dev
->stats
.rx_crc_errors
++;
545 if (bd_status
& R_AB_S
)
546 dev
->stats
.rx_over_errors
++;
547 if (bd_status
& R_NO_S
)
548 dev
->stats
.rx_frame_errors
++;
549 if (bd_status
& R_LG_S
)
550 dev
->stats
.rx_length_errors
++;
554 bdbuffer
= priv
->rx_buffer
+
555 (priv
->currx_bdnum
* MAX_RX_BUF_LENGTH
);
556 length
= be16_to_cpu(bd
->length
);
560 bdbuffer
+= HDLC_HEAD_LEN
;
561 length
-= (HDLC_HEAD_LEN
+ HDLC_CRC_SIZE
);
563 skb
= dev_alloc_skb(length
);
565 dev
->stats
.rx_dropped
++;
569 skb_put(skb
, length
);
572 memcpy(skb
->data
, bdbuffer
, length
);
577 length
-= HDLC_CRC_SIZE
;
579 skb
= dev_alloc_skb(length
);
581 dev
->stats
.rx_dropped
++;
585 skb_put(skb
, length
);
588 memcpy(skb
->data
, bdbuffer
, length
);
592 dev
->stats
.rx_packets
++;
593 dev
->stats
.rx_bytes
+= skb
->len
;
596 skb
->protocol
= hdlc_type_trans(skb
, dev
);
597 netif_receive_skb(skb
);
600 bd
->status
= cpu_to_be16((bd_status
& R_W_S
) | R_E_S
| R_I_S
);
602 /* update to point at the next bd */
603 if (bd_status
& R_W_S
) {
604 priv
->currx_bdnum
= 0;
605 bd
= priv
->rx_bd_base
;
607 if (priv
->currx_bdnum
< (RX_BD_RING_LEN
- 1))
608 priv
->currx_bdnum
+= 1;
610 priv
->currx_bdnum
= RX_BD_RING_LEN
- 1;
615 bd_status
= be16_to_cpu(bd
->status
);
623 static int ucc_hdlc_poll(struct napi_struct
*napi
, int budget
)
625 struct ucc_hdlc_private
*priv
= container_of(napi
,
626 struct ucc_hdlc_private
,
630 /* Tx event processing */
631 spin_lock(&priv
->lock
);
633 spin_unlock(&priv
->lock
);
636 howmany
+= hdlc_rx_done(priv
, budget
- howmany
);
638 if (howmany
< budget
) {
639 napi_complete_done(napi
, howmany
);
640 qe_setbits_be32(priv
->uccf
->p_uccm
,
641 (UCCE_HDLC_RX_EVENTS
| UCCE_HDLC_TX_EVENTS
) << 16);
647 static irqreturn_t
ucc_hdlc_irq_handler(int irq
, void *dev_id
)
649 struct ucc_hdlc_private
*priv
= (struct ucc_hdlc_private
*)dev_id
;
650 struct net_device
*dev
= priv
->ndev
;
651 struct ucc_fast_private
*uccf
;
657 ucce
= ioread32be(uccf
->p_ucce
);
658 uccm
= ioread32be(uccf
->p_uccm
);
660 iowrite32be(ucce
, uccf
->p_ucce
);
664 if ((ucce
>> 16) & (UCCE_HDLC_RX_EVENTS
| UCCE_HDLC_TX_EVENTS
)) {
665 if (napi_schedule_prep(&priv
->napi
)) {
666 uccm
&= ~((UCCE_HDLC_RX_EVENTS
| UCCE_HDLC_TX_EVENTS
)
668 iowrite32be(uccm
, uccf
->p_uccm
);
669 __napi_schedule(&priv
->napi
);
673 /* Errors and other events */
674 if (ucce
>> 16 & UCC_HDLC_UCCE_BSY
)
675 dev
->stats
.rx_missed_errors
++;
676 if (ucce
>> 16 & UCC_HDLC_UCCE_TXE
)
677 dev
->stats
.tx_errors
++;
682 static int uhdlc_ioctl(struct net_device
*dev
, struct if_settings
*ifs
)
684 const size_t size
= sizeof(te1_settings
);
686 struct ucc_hdlc_private
*priv
= netdev_priv(dev
);
690 ifs
->type
= IF_IFACE_E1
;
691 if (ifs
->size
< size
) {
692 ifs
->size
= size
; /* data size wanted */
695 memset(&line
, 0, sizeof(line
));
696 line
.clock_type
= priv
->clocking
;
698 if (copy_to_user(ifs
->ifs_ifsu
.sync
, &line
, size
))
703 return hdlc_ioctl(dev
, ifs
);
707 static int uhdlc_open(struct net_device
*dev
)
710 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
711 struct ucc_hdlc_private
*priv
= hdlc
->priv
;
712 struct ucc_tdm
*utdm
= priv
->utdm
;
715 if (priv
->hdlc_busy
!= 1) {
716 if (request_irq(priv
->ut_info
->uf_info
.irq
,
717 ucc_hdlc_irq_handler
, 0, "hdlc", priv
))
720 cecr_subblock
= ucc_fast_get_qe_cr_subblock(
721 priv
->ut_info
->uf_info
.ucc_num
);
723 qe_issue_cmd(QE_INIT_TX_RX
, cecr_subblock
,
724 QE_CR_PROTOCOL_UNSPECIFIED
, 0);
726 ucc_fast_enable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
728 /* Enable the TDM port */
730 qe_setbits_8(&utdm
->si_regs
->siglmr1_h
, 0x1 << utdm
->tdm_port
);
733 netif_device_attach(priv
->ndev
);
734 napi_enable(&priv
->napi
);
735 netdev_reset_queue(dev
);
736 netif_start_queue(dev
);
746 static void uhdlc_memclean(struct ucc_hdlc_private
*priv
)
748 qe_muram_free(ioread16be(&priv
->ucc_pram
->riptr
));
749 qe_muram_free(ioread16be(&priv
->ucc_pram
->tiptr
));
751 if (priv
->rx_bd_base
) {
752 dma_free_coherent(priv
->dev
,
753 RX_BD_RING_LEN
* sizeof(struct qe_bd
),
754 priv
->rx_bd_base
, priv
->dma_rx_bd
);
756 priv
->rx_bd_base
= NULL
;
760 if (priv
->tx_bd_base
) {
761 dma_free_coherent(priv
->dev
,
762 TX_BD_RING_LEN
* sizeof(struct qe_bd
),
763 priv
->tx_bd_base
, priv
->dma_tx_bd
);
765 priv
->tx_bd_base
= NULL
;
769 if (priv
->ucc_pram
) {
770 qe_muram_free(priv
->ucc_pram_offset
);
771 priv
->ucc_pram
= NULL
;
772 priv
->ucc_pram_offset
= 0;
775 kfree(priv
->rx_skbuff
);
776 priv
->rx_skbuff
= NULL
;
778 kfree(priv
->tx_skbuff
);
779 priv
->tx_skbuff
= NULL
;
782 iounmap(priv
->uf_regs
);
783 priv
->uf_regs
= NULL
;
787 ucc_fast_free(priv
->uccf
);
791 if (priv
->rx_buffer
) {
792 dma_free_coherent(priv
->dev
,
793 RX_BD_RING_LEN
* MAX_RX_BUF_LENGTH
,
794 priv
->rx_buffer
, priv
->dma_rx_addr
);
795 priv
->rx_buffer
= NULL
;
796 priv
->dma_rx_addr
= 0;
799 if (priv
->tx_buffer
) {
800 dma_free_coherent(priv
->dev
,
801 TX_BD_RING_LEN
* MAX_RX_BUF_LENGTH
,
802 priv
->tx_buffer
, priv
->dma_tx_addr
);
803 priv
->tx_buffer
= NULL
;
804 priv
->dma_tx_addr
= 0;
808 static int uhdlc_close(struct net_device
*dev
)
810 struct ucc_hdlc_private
*priv
= dev_to_hdlc(dev
)->priv
;
811 struct ucc_tdm
*utdm
= priv
->utdm
;
814 napi_disable(&priv
->napi
);
815 cecr_subblock
= ucc_fast_get_qe_cr_subblock(
816 priv
->ut_info
->uf_info
.ucc_num
);
818 qe_issue_cmd(QE_GRACEFUL_STOP_TX
, cecr_subblock
,
819 (u8
)QE_CR_PROTOCOL_UNSPECIFIED
, 0);
820 qe_issue_cmd(QE_CLOSE_RX_BD
, cecr_subblock
,
821 (u8
)QE_CR_PROTOCOL_UNSPECIFIED
, 0);
824 qe_clrbits_8(&utdm
->si_regs
->siglmr1_h
, 0x1 << utdm
->tdm_port
);
826 ucc_fast_disable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
828 free_irq(priv
->ut_info
->uf_info
.irq
, priv
);
829 netif_stop_queue(dev
);
830 netdev_reset_queue(dev
);
838 static int ucc_hdlc_attach(struct net_device
*dev
, unsigned short encoding
,
839 unsigned short parity
)
841 struct ucc_hdlc_private
*priv
= dev_to_hdlc(dev
)->priv
;
843 if (encoding
!= ENCODING_NRZ
&&
844 encoding
!= ENCODING_NRZI
)
847 if (parity
!= PARITY_NONE
&&
848 parity
!= PARITY_CRC32_PR1_CCITT
&&
849 parity
!= PARITY_CRC16_PR0_CCITT
&&
850 parity
!= PARITY_CRC16_PR1_CCITT
)
853 priv
->encoding
= encoding
;
854 priv
->parity
= parity
;
860 static void store_clk_config(struct ucc_hdlc_private
*priv
)
862 struct qe_mux __iomem
*qe_mux_reg
= &qe_immr
->qmx
;
865 priv
->cmxsi1cr_h
= ioread32be(&qe_mux_reg
->cmxsi1cr_h
);
866 priv
->cmxsi1cr_l
= ioread32be(&qe_mux_reg
->cmxsi1cr_l
);
869 priv
->cmxsi1syr
= ioread32be(&qe_mux_reg
->cmxsi1syr
);
872 memcpy_fromio(priv
->cmxucr
, qe_mux_reg
->cmxucr
, 4 * sizeof(u32
));
875 static void resume_clk_config(struct ucc_hdlc_private
*priv
)
877 struct qe_mux __iomem
*qe_mux_reg
= &qe_immr
->qmx
;
879 memcpy_toio(qe_mux_reg
->cmxucr
, priv
->cmxucr
, 4 * sizeof(u32
));
881 iowrite32be(priv
->cmxsi1cr_h
, &qe_mux_reg
->cmxsi1cr_h
);
882 iowrite32be(priv
->cmxsi1cr_l
, &qe_mux_reg
->cmxsi1cr_l
);
884 iowrite32be(priv
->cmxsi1syr
, &qe_mux_reg
->cmxsi1syr
);
887 static int uhdlc_suspend(struct device
*dev
)
889 struct ucc_hdlc_private
*priv
= dev_get_drvdata(dev
);
890 struct ucc_fast __iomem
*uf_regs
;
895 if (!netif_running(priv
->ndev
))
898 netif_device_detach(priv
->ndev
);
899 napi_disable(&priv
->napi
);
901 uf_regs
= priv
->uf_regs
;
903 /* backup gumr guemr*/
904 priv
->gumr
= ioread32be(&uf_regs
->gumr
);
905 priv
->guemr
= ioread8(&uf_regs
->guemr
);
907 priv
->ucc_pram_bak
= kmalloc(sizeof(*priv
->ucc_pram_bak
),
909 if (!priv
->ucc_pram_bak
)
912 /* backup HDLC parameter */
913 memcpy_fromio(priv
->ucc_pram_bak
, priv
->ucc_pram
,
914 sizeof(struct ucc_hdlc_param
));
916 /* store the clk configuration */
917 store_clk_config(priv
);
920 ucc_fast_disable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
925 static int uhdlc_resume(struct device
*dev
)
927 struct ucc_hdlc_private
*priv
= dev_get_drvdata(dev
);
928 struct ucc_tdm
*utdm
;
929 struct ucc_tdm_info
*ut_info
;
930 struct ucc_fast __iomem
*uf_regs
;
931 struct ucc_fast_private
*uccf
;
932 struct ucc_fast_info
*uf_info
;
940 if (!netif_running(priv
->ndev
))
944 ut_info
= priv
->ut_info
;
945 uf_info
= &ut_info
->uf_info
;
946 uf_regs
= priv
->uf_regs
;
949 /* restore gumr guemr */
950 iowrite8(priv
->guemr
, &uf_regs
->guemr
);
951 iowrite32be(priv
->gumr
, &uf_regs
->gumr
);
953 /* Set Virtual Fifo registers */
954 iowrite16be(uf_info
->urfs
, &uf_regs
->urfs
);
955 iowrite16be(uf_info
->urfet
, &uf_regs
->urfet
);
956 iowrite16be(uf_info
->urfset
, &uf_regs
->urfset
);
957 iowrite16be(uf_info
->utfs
, &uf_regs
->utfs
);
958 iowrite16be(uf_info
->utfet
, &uf_regs
->utfet
);
959 iowrite16be(uf_info
->utftt
, &uf_regs
->utftt
);
960 /* utfb, urfb are offsets from MURAM base */
961 iowrite32be(uccf
->ucc_fast_tx_virtual_fifo_base_offset
, &uf_regs
->utfb
);
962 iowrite32be(uccf
->ucc_fast_rx_virtual_fifo_base_offset
, &uf_regs
->urfb
);
964 /* Rx Tx and sync clock routing */
965 resume_clk_config(priv
);
967 iowrite32be(uf_info
->uccm_mask
, &uf_regs
->uccm
);
968 iowrite32be(0xffffffff, &uf_regs
->ucce
);
970 ucc_fast_disable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
974 ucc_tdm_init(priv
->utdm
, priv
->ut_info
);
976 /* Write to QE CECR, UCCx channel to Stop Transmission */
977 cecr_subblock
= ucc_fast_get_qe_cr_subblock(uf_info
->ucc_num
);
978 qe_issue_cmd(QE_STOP_TX
, cecr_subblock
,
979 (u8
)QE_CR_PROTOCOL_UNSPECIFIED
, 0);
981 /* Set UPSMR normal mode */
982 iowrite32be(0, &uf_regs
->upsmr
);
984 /* init parameter base */
985 cecr_subblock
= ucc_fast_get_qe_cr_subblock(uf_info
->ucc_num
);
986 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE
, cecr_subblock
,
987 QE_CR_PROTOCOL_UNSPECIFIED
, priv
->ucc_pram_offset
);
989 priv
->ucc_pram
= (struct ucc_hdlc_param __iomem
*)
990 qe_muram_addr(priv
->ucc_pram_offset
);
992 /* restore ucc parameter */
993 memcpy_toio(priv
->ucc_pram
, priv
->ucc_pram_bak
,
994 sizeof(struct ucc_hdlc_param
));
995 kfree(priv
->ucc_pram_bak
);
997 /* rebuild BD entry */
998 for (i
= 0; i
< RX_BD_RING_LEN
; i
++) {
999 if (i
< (RX_BD_RING_LEN
- 1))
1000 bd_status
= R_E_S
| R_I_S
;
1002 bd_status
= R_E_S
| R_I_S
| R_W_S
;
1004 priv
->rx_bd_base
[i
].status
= cpu_to_be16(bd_status
);
1005 priv
->rx_bd_base
[i
].buf
= cpu_to_be32(priv
->dma_rx_addr
+ i
* MAX_RX_BUF_LENGTH
);
1008 for (i
= 0; i
< TX_BD_RING_LEN
; i
++) {
1009 if (i
< (TX_BD_RING_LEN
- 1))
1010 bd_status
= T_I_S
| T_TC_S
;
1012 bd_status
= T_I_S
| T_TC_S
| T_W_S
;
1014 priv
->tx_bd_base
[i
].status
= cpu_to_be16(bd_status
);
1015 priv
->tx_bd_base
[i
].buf
= cpu_to_be32(priv
->dma_tx_addr
+ i
* MAX_RX_BUF_LENGTH
);
1019 /* if hdlc is busy enable TX and RX */
1020 if (priv
->hdlc_busy
== 1) {
1021 cecr_subblock
= ucc_fast_get_qe_cr_subblock(
1022 priv
->ut_info
->uf_info
.ucc_num
);
1024 qe_issue_cmd(QE_INIT_TX_RX
, cecr_subblock
,
1025 (u8
)QE_CR_PROTOCOL_UNSPECIFIED
, 0);
1027 ucc_fast_enable(priv
->uccf
, COMM_DIR_RX
| COMM_DIR_TX
);
1029 /* Enable the TDM port */
1031 qe_setbits_8(&utdm
->si_regs
->siglmr1_h
, 0x1 << utdm
->tdm_port
);
1034 napi_enable(&priv
->napi
);
1035 netif_device_attach(priv
->ndev
);
1040 static const struct dev_pm_ops uhdlc_pm_ops
= {
1041 .suspend
= uhdlc_suspend
,
1042 .resume
= uhdlc_resume
,
1043 .freeze
= uhdlc_suspend
,
1044 .thaw
= uhdlc_resume
,
1047 #define HDLC_PM_OPS (&uhdlc_pm_ops)
1051 #define HDLC_PM_OPS NULL
1054 static void uhdlc_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
1056 netdev_err(ndev
, "%s\n", __func__
);
1059 static const struct net_device_ops uhdlc_ops
= {
1060 .ndo_open
= uhdlc_open
,
1061 .ndo_stop
= uhdlc_close
,
1062 .ndo_start_xmit
= hdlc_start_xmit
,
1063 .ndo_siocwandev
= uhdlc_ioctl
,
1064 .ndo_tx_timeout
= uhdlc_tx_timeout
,
1067 static int hdlc_map_iomem(char *name
, int init_flag
, void __iomem
**ptr
)
1069 struct device_node
*np
;
1070 struct platform_device
*pdev
;
1071 struct resource
*res
;
1072 static int siram_init_flag
;
1075 np
= of_find_compatible_node(NULL
, NULL
, name
);
1079 pdev
= of_find_device_by_node(np
);
1081 pr_err("%pOFn: failed to lookup pdev\n", np
);
1087 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1090 goto error_put_device
;
1092 *ptr
= ioremap(res
->start
, resource_size(res
));
1095 goto error_put_device
;
1098 /* We've remapped the addresses, and we don't need the device any
1099 * more, so we should release it.
1101 put_device(&pdev
->dev
);
1103 if (init_flag
&& siram_init_flag
== 0) {
1104 memset_io(*ptr
, 0, resource_size(res
));
1105 siram_init_flag
= 1;
1110 put_device(&pdev
->dev
);
1115 static int ucc_hdlc_probe(struct platform_device
*pdev
)
1117 struct device_node
*np
= pdev
->dev
.of_node
;
1118 struct ucc_hdlc_private
*uhdlc_priv
= NULL
;
1119 struct ucc_tdm_info
*ut_info
;
1120 struct ucc_tdm
*utdm
= NULL
;
1121 struct resource res
;
1122 struct net_device
*dev
;
1129 ret
= of_property_read_u32_index(np
, "cell-index", 0, &val
);
1131 dev_err(&pdev
->dev
, "Invalid ucc property\n");
1136 if (ucc_num
> (UCC_MAX_NUM
- 1) || ucc_num
< 0) {
1137 dev_err(&pdev
->dev
, ": Invalid UCC num\n");
1141 memcpy(&utdm_info
[ucc_num
], &utdm_primary_info
,
1142 sizeof(utdm_primary_info
));
1144 ut_info
= &utdm_info
[ucc_num
];
1145 ut_info
->uf_info
.ucc_num
= ucc_num
;
1147 sprop
= of_get_property(np
, "rx-clock-name", NULL
);
1149 ut_info
->uf_info
.rx_clock
= qe_clock_source(sprop
);
1150 if ((ut_info
->uf_info
.rx_clock
< QE_CLK_NONE
) ||
1151 (ut_info
->uf_info
.rx_clock
> QE_CLK24
)) {
1152 dev_err(&pdev
->dev
, "Invalid rx-clock-name property\n");
1156 dev_err(&pdev
->dev
, "Invalid rx-clock-name property\n");
1160 sprop
= of_get_property(np
, "tx-clock-name", NULL
);
1162 ut_info
->uf_info
.tx_clock
= qe_clock_source(sprop
);
1163 if ((ut_info
->uf_info
.tx_clock
< QE_CLK_NONE
) ||
1164 (ut_info
->uf_info
.tx_clock
> QE_CLK24
)) {
1165 dev_err(&pdev
->dev
, "Invalid tx-clock-name property\n");
1169 dev_err(&pdev
->dev
, "Invalid tx-clock-name property\n");
1173 ret
= of_address_to_resource(np
, 0, &res
);
1177 ut_info
->uf_info
.regs
= res
.start
;
1178 ut_info
->uf_info
.irq
= irq_of_parse_and_map(np
, 0);
1180 uhdlc_priv
= kzalloc(sizeof(*uhdlc_priv
), GFP_KERNEL
);
1184 dev_set_drvdata(&pdev
->dev
, uhdlc_priv
);
1185 uhdlc_priv
->dev
= &pdev
->dev
;
1186 uhdlc_priv
->ut_info
= ut_info
;
1188 uhdlc_priv
->tsa
= of_property_read_bool(np
, "fsl,tdm-interface");
1189 uhdlc_priv
->loopback
= of_property_read_bool(np
, "fsl,ucc-internal-loopback");
1190 uhdlc_priv
->hdlc_bus
= of_property_read_bool(np
, "fsl,hdlc-bus");
1192 if (uhdlc_priv
->tsa
== 1) {
1193 utdm
= kzalloc(sizeof(*utdm
), GFP_KERNEL
);
1196 dev_err(&pdev
->dev
, "No mem to alloc ucc tdm data\n");
1197 goto free_uhdlc_priv
;
1199 uhdlc_priv
->utdm
= utdm
;
1200 ret
= ucc_of_parse_tdm(np
, utdm
, ut_info
);
1204 ret
= hdlc_map_iomem("fsl,t1040-qe-si", 0,
1205 (void __iomem
**)&utdm
->si_regs
);
1208 ret
= hdlc_map_iomem("fsl,t1040-qe-siram", 1,
1209 (void __iomem
**)&utdm
->siram
);
1214 if (of_property_read_u16(np
, "fsl,hmask", &uhdlc_priv
->hmask
))
1215 uhdlc_priv
->hmask
= DEFAULT_ADDR_MASK
;
1217 ret
= uhdlc_init(uhdlc_priv
);
1219 dev_err(&pdev
->dev
, "Failed to init uhdlc\n");
1220 goto undo_uhdlc_init
;
1223 dev
= alloc_hdlcdev(uhdlc_priv
);
1226 pr_err("ucc_hdlc: unable to allocate memory\n");
1227 goto undo_uhdlc_init
;
1230 uhdlc_priv
->ndev
= dev
;
1231 hdlc
= dev_to_hdlc(dev
);
1232 dev
->tx_queue_len
= 16;
1233 dev
->netdev_ops
= &uhdlc_ops
;
1234 dev
->watchdog_timeo
= 2 * HZ
;
1235 hdlc
->attach
= ucc_hdlc_attach
;
1236 hdlc
->xmit
= ucc_hdlc_tx
;
1237 netif_napi_add_weight(dev
, &uhdlc_priv
->napi
, ucc_hdlc_poll
, 32);
1238 if (register_hdlc_device(dev
)) {
1240 pr_err("ucc_hdlc: unable to register hdlc device\n");
1250 iounmap(utdm
->siram
);
1253 iounmap(utdm
->si_regs
);
1255 if (uhdlc_priv
->tsa
)
1262 static void ucc_hdlc_remove(struct platform_device
*pdev
)
1264 struct ucc_hdlc_private
*priv
= dev_get_drvdata(&pdev
->dev
);
1266 uhdlc_memclean(priv
);
1268 if (priv
->utdm
->si_regs
) {
1269 iounmap(priv
->utdm
->si_regs
);
1270 priv
->utdm
->si_regs
= NULL
;
1273 if (priv
->utdm
->siram
) {
1274 iounmap(priv
->utdm
->siram
);
1275 priv
->utdm
->siram
= NULL
;
1279 dev_info(&pdev
->dev
, "UCC based hdlc module removed\n");
1282 static const struct of_device_id fsl_ucc_hdlc_of_match
[] = {
1284 .compatible
= "fsl,ucc-hdlc",
1289 MODULE_DEVICE_TABLE(of
, fsl_ucc_hdlc_of_match
);
1291 static struct platform_driver ucc_hdlc_driver
= {
1292 .probe
= ucc_hdlc_probe
,
1293 .remove
= ucc_hdlc_remove
,
1297 .of_match_table
= fsl_ucc_hdlc_of_match
,
1301 module_platform_driver(ucc_hdlc_driver
);
1302 MODULE_LICENSE("GPL");
1303 MODULE_DESCRIPTION(DRV_DESC
);