1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2006 Freescale Semiconductor, Inc. All rights reserved.
5 * Authors: Shlomi Gridish <gridish@freescale.com>
6 * Li Yang <leoli@freescale.com>
9 * QE UCC Slow API Set - UCC Slow specific routines implementations.
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/stddef.h>
15 #include <linux/interrupt.h>
16 #include <linux/err.h>
17 #include <linux/export.h>
20 #include <soc/fsl/qe/immap_qe.h>
21 #include <soc/fsl/qe/qe.h>
23 #include <soc/fsl/qe/ucc.h>
24 #include <soc/fsl/qe/ucc_slow.h>
26 u32
ucc_slow_get_qe_cr_subblock(int uccs_num
)
29 case 0: return QE_CR_SUBBLOCK_UCCSLOW1
;
30 case 1: return QE_CR_SUBBLOCK_UCCSLOW2
;
31 case 2: return QE_CR_SUBBLOCK_UCCSLOW3
;
32 case 3: return QE_CR_SUBBLOCK_UCCSLOW4
;
33 case 4: return QE_CR_SUBBLOCK_UCCSLOW5
;
34 case 5: return QE_CR_SUBBLOCK_UCCSLOW6
;
35 case 6: return QE_CR_SUBBLOCK_UCCSLOW7
;
36 case 7: return QE_CR_SUBBLOCK_UCCSLOW8
;
37 default: return QE_CR_SUBBLOCK_INVALID
;
40 EXPORT_SYMBOL(ucc_slow_get_qe_cr_subblock
);
42 void ucc_slow_graceful_stop_tx(struct ucc_slow_private
* uccs
)
44 struct ucc_slow_info
*us_info
= uccs
->us_info
;
47 id
= ucc_slow_get_qe_cr_subblock(us_info
->ucc_num
);
48 qe_issue_cmd(QE_GRACEFUL_STOP_TX
, id
,
49 QE_CR_PROTOCOL_UNSPECIFIED
, 0);
51 EXPORT_SYMBOL(ucc_slow_graceful_stop_tx
);
53 void ucc_slow_stop_tx(struct ucc_slow_private
* uccs
)
55 struct ucc_slow_info
*us_info
= uccs
->us_info
;
58 id
= ucc_slow_get_qe_cr_subblock(us_info
->ucc_num
);
59 qe_issue_cmd(QE_STOP_TX
, id
, QE_CR_PROTOCOL_UNSPECIFIED
, 0);
61 EXPORT_SYMBOL(ucc_slow_stop_tx
);
63 void ucc_slow_restart_tx(struct ucc_slow_private
* uccs
)
65 struct ucc_slow_info
*us_info
= uccs
->us_info
;
68 id
= ucc_slow_get_qe_cr_subblock(us_info
->ucc_num
);
69 qe_issue_cmd(QE_RESTART_TX
, id
, QE_CR_PROTOCOL_UNSPECIFIED
, 0);
71 EXPORT_SYMBOL(ucc_slow_restart_tx
);
73 void ucc_slow_enable(struct ucc_slow_private
* uccs
, enum comm_dir mode
)
75 struct ucc_slow __iomem
*us_regs
;
78 us_regs
= uccs
->us_regs
;
80 /* Enable reception and/or transmission on this UCC. */
81 gumr_l
= qe_ioread32be(&us_regs
->gumr_l
);
82 if (mode
& COMM_DIR_TX
) {
83 gumr_l
|= UCC_SLOW_GUMR_L_ENT
;
86 if (mode
& COMM_DIR_RX
) {
87 gumr_l
|= UCC_SLOW_GUMR_L_ENR
;
90 qe_iowrite32be(gumr_l
, &us_regs
->gumr_l
);
92 EXPORT_SYMBOL(ucc_slow_enable
);
94 void ucc_slow_disable(struct ucc_slow_private
* uccs
, enum comm_dir mode
)
96 struct ucc_slow __iomem
*us_regs
;
99 us_regs
= uccs
->us_regs
;
101 /* Disable reception and/or transmission on this UCC. */
102 gumr_l
= qe_ioread32be(&us_regs
->gumr_l
);
103 if (mode
& COMM_DIR_TX
) {
104 gumr_l
&= ~UCC_SLOW_GUMR_L_ENT
;
105 uccs
->enabled_tx
= 0;
107 if (mode
& COMM_DIR_RX
) {
108 gumr_l
&= ~UCC_SLOW_GUMR_L_ENR
;
109 uccs
->enabled_rx
= 0;
111 qe_iowrite32be(gumr_l
, &us_regs
->gumr_l
);
113 EXPORT_SYMBOL(ucc_slow_disable
);
115 /* Initialize the UCC for Slow operations
117 * The caller should initialize the following us_info
119 int ucc_slow_init(struct ucc_slow_info
* us_info
, struct ucc_slow_private
** uccs_ret
)
121 struct ucc_slow_private
*uccs
;
123 struct ucc_slow __iomem
*us_regs
;
125 struct qe_bd __iomem
*bd
;
133 /* check if the UCC port number is in range. */
134 if ((us_info
->ucc_num
< 0) || (us_info
->ucc_num
> UCC_MAX_NUM
- 1)) {
135 printk(KERN_ERR
"%s: illegal UCC number\n", __func__
);
141 * Check that 'max_rx_buf_length' is properly aligned (4), unless
142 * rfw is 1, meaning that QE accepts one byte at a time, unlike normal
143 * case when QE accepts 32 bits at a time.
145 if ((!us_info
->rfw
) &&
146 (us_info
->max_rx_buf_length
& (UCC_SLOW_MRBLR_ALIGNMENT
- 1))) {
147 printk(KERN_ERR
"max_rx_buf_length not aligned.\n");
151 uccs
= kzalloc(sizeof(struct ucc_slow_private
), GFP_KERNEL
);
153 printk(KERN_ERR
"%s: Cannot allocate private data\n",
157 uccs
->rx_base_offset
= -1;
158 uccs
->tx_base_offset
= -1;
159 uccs
->us_pram_offset
= -1;
161 /* Fill slow UCC structure */
162 uccs
->us_info
= us_info
;
163 /* Set the PHY base address */
164 uccs
->us_regs
= ioremap(us_info
->regs
, sizeof(struct ucc_slow
));
165 if (uccs
->us_regs
== NULL
) {
166 printk(KERN_ERR
"%s: Cannot map UCC registers\n", __func__
);
171 us_regs
= uccs
->us_regs
;
172 uccs
->p_ucce
= &us_regs
->ucce
;
173 uccs
->p_uccm
= &us_regs
->uccm
;
176 uccs
->us_pram_offset
=
177 qe_muram_alloc(UCC_SLOW_PRAM_SIZE
, ALIGNMENT_OF_UCC_SLOW_PRAM
);
178 if (uccs
->us_pram_offset
< 0) {
179 printk(KERN_ERR
"%s: cannot allocate MURAM for PRAM", __func__
);
183 id
= ucc_slow_get_qe_cr_subblock(us_info
->ucc_num
);
184 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE
, id
, us_info
->protocol
,
185 uccs
->us_pram_offset
);
187 uccs
->us_pram
= qe_muram_addr(uccs
->us_pram_offset
);
189 /* Set UCC to slow type */
190 ret
= ucc_set_type(us_info
->ucc_num
, UCC_SPEED_TYPE_SLOW
);
192 printk(KERN_ERR
"%s: cannot set UCC type", __func__
);
197 qe_iowrite16be(us_info
->max_rx_buf_length
, &uccs
->us_pram
->mrblr
);
199 INIT_LIST_HEAD(&uccs
->confQ
);
202 uccs
->rx_base_offset
=
203 qe_muram_alloc(us_info
->rx_bd_ring_len
* sizeof(struct qe_bd
),
205 if (uccs
->rx_base_offset
< 0) {
206 printk(KERN_ERR
"%s: cannot allocate %u RX BDs\n", __func__
,
207 us_info
->rx_bd_ring_len
);
212 uccs
->tx_base_offset
=
213 qe_muram_alloc(us_info
->tx_bd_ring_len
* sizeof(struct qe_bd
),
215 if (uccs
->tx_base_offset
< 0) {
216 printk(KERN_ERR
"%s: cannot allocate TX BDs", __func__
);
222 bd
= uccs
->confBd
= uccs
->tx_bd
= qe_muram_addr(uccs
->tx_base_offset
);
223 for (i
= 0; i
< us_info
->tx_bd_ring_len
- 1; i
++) {
224 /* clear bd buffer */
225 qe_iowrite32be(0, &bd
->buf
);
226 /* set bd status and length */
227 qe_iowrite32be(0, (u32 __iomem
*)bd
);
230 /* for last BD set Wrap bit */
231 qe_iowrite32be(0, &bd
->buf
);
232 qe_iowrite32be(T_W
, (u32 __iomem
*)bd
);
235 bd
= uccs
->rx_bd
= qe_muram_addr(uccs
->rx_base_offset
);
236 for (i
= 0; i
< us_info
->rx_bd_ring_len
- 1; i
++) {
237 /* set bd status and length */
238 qe_iowrite32be(0, (u32 __iomem
*)bd
);
239 /* clear bd buffer */
240 qe_iowrite32be(0, &bd
->buf
);
243 /* for last BD set Wrap bit */
244 qe_iowrite32be(R_W
, (u32 __iomem
*)bd
);
245 qe_iowrite32be(0, &bd
->buf
);
247 /* Set GUMR (For more details see the hardware spec.). */
249 gumr
= us_info
->tcrc
;
251 gumr
|= UCC_SLOW_GUMR_H_CDP
;
253 gumr
|= UCC_SLOW_GUMR_H_CTSP
;
255 gumr
|= UCC_SLOW_GUMR_H_CDS
;
257 gumr
|= UCC_SLOW_GUMR_H_CTSS
;
259 gumr
|= UCC_SLOW_GUMR_H_TFL
;
261 gumr
|= UCC_SLOW_GUMR_H_RFW
;
263 gumr
|= UCC_SLOW_GUMR_H_TXSY
;
265 gumr
|= UCC_SLOW_GUMR_H_RTSM
;
266 qe_iowrite32be(gumr
, &us_regs
->gumr_h
);
269 gumr
= (u32
)us_info
->tdcr
| (u32
)us_info
->rdcr
| (u32
)us_info
->tenc
|
270 (u32
)us_info
->renc
| (u32
)us_info
->diag
| (u32
)us_info
->mode
;
272 gumr
|= UCC_SLOW_GUMR_L_TCI
;
274 gumr
|= UCC_SLOW_GUMR_L_RINV
;
276 gumr
|= UCC_SLOW_GUMR_L_TINV
;
278 gumr
|= UCC_SLOW_GUMR_L_TEND
;
279 qe_iowrite32be(gumr
, &us_regs
->gumr_l
);
281 /* Function code registers */
283 /* if the data is in cachable memory, the 'global' */
284 /* in the function code should be set. */
285 qe_iowrite8(UCC_BMR_BO_BE
, &uccs
->us_pram
->tbmr
);
286 qe_iowrite8(UCC_BMR_BO_BE
, &uccs
->us_pram
->rbmr
);
288 /* rbase, tbase are offsets from MURAM base */
289 qe_iowrite16be(uccs
->rx_base_offset
, &uccs
->us_pram
->rbase
);
290 qe_iowrite16be(uccs
->tx_base_offset
, &uccs
->us_pram
->tbase
);
294 ucc_set_qe_mux_grant(us_info
->ucc_num
, us_info
->grant_support
);
295 /* Breakpoint Support */
296 ucc_set_qe_mux_bkpt(us_info
->ucc_num
, us_info
->brkpt_support
);
297 /* Set Tsa or NMSI mode. */
298 ucc_set_qe_mux_tsa(us_info
->ucc_num
, us_info
->tsa
);
299 /* If NMSI (not Tsa), set Tx and Rx clock. */
301 /* Rx clock routing */
302 if (ucc_set_qe_mux_rxtx(us_info
->ucc_num
, us_info
->rx_clock
,
304 printk(KERN_ERR
"%s: illegal value for RX clock\n",
309 /* Tx clock routing */
310 if (ucc_set_qe_mux_rxtx(us_info
->ucc_num
, us_info
->tx_clock
,
312 printk(KERN_ERR
"%s: illegal value for TX clock\n",
319 /* Set interrupt mask register at UCC level. */
320 qe_iowrite16be(us_info
->uccm_mask
, &us_regs
->uccm
);
322 /* First, clear anything pending at UCC level,
323 * otherwise, old garbage may come through
324 * as soon as the dam is opened. */
326 /* Writing '1' clears */
327 qe_iowrite16be(0xffff, &us_regs
->ucce
);
329 /* Issue QE Init command */
330 if (us_info
->init_tx
&& us_info
->init_rx
)
331 command
= QE_INIT_TX_RX
;
332 else if (us_info
->init_tx
)
333 command
= QE_INIT_TX
;
335 command
= QE_INIT_RX
; /* We know at least one is TRUE */
337 qe_issue_cmd(command
, id
, us_info
->protocol
, 0);
342 EXPORT_SYMBOL(ucc_slow_init
);
344 void ucc_slow_free(struct ucc_slow_private
* uccs
)
349 qe_muram_free(uccs
->rx_base_offset
);
350 qe_muram_free(uccs
->tx_base_offset
);
351 qe_muram_free(uccs
->us_pram_offset
);
354 iounmap(uccs
->us_regs
);
358 EXPORT_SYMBOL(ucc_slow_free
);