2 * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
4 * Authors: Shlomi Gridish <gridish@freescale.com>
5 * Li Yang <leoli@freescale.com>
8 * QE UCC Fast API Set - UCC Fast specific routines implementations.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/stddef.h>
20 #include <linux/interrupt.h>
23 #include <asm/immap_qe.h>
27 #include <asm/ucc_fast.h>
29 void ucc_fast_dump_regs(struct ucc_fast_private * uccf)
31 printk(KERN_INFO "UCC%d Fast registers:", uccf->uf_info->ucc_num);
32 printk(KERN_INFO "Base address: 0x%08x", (u32) uccf->uf_regs);
34 printk(KERN_INFO "gumr : addr - 0x%08x, val - 0x%08x",
35 (u32) & uccf->uf_regs->gumr, in_be32(&uccf->uf_regs->gumr));
36 printk(KERN_INFO "upsmr : addr - 0x%08x, val - 0x%08x",
37 (u32) & uccf->uf_regs->upsmr, in_be32(&uccf->uf_regs->upsmr));
38 printk(KERN_INFO "utodr : addr - 0x%08x, val - 0x%04x",
39 (u32) & uccf->uf_regs->utodr, in_be16(&uccf->uf_regs->utodr));
40 printk(KERN_INFO "udsr : addr - 0x%08x, val - 0x%04x",
41 (u32) & uccf->uf_regs->udsr, in_be16(&uccf->uf_regs->udsr));
42 printk(KERN_INFO "ucce : addr - 0x%08x, val - 0x%08x",
43 (u32) & uccf->uf_regs->ucce, in_be32(&uccf->uf_regs->ucce));
44 printk(KERN_INFO "uccm : addr - 0x%08x, val - 0x%08x",
45 (u32) & uccf->uf_regs->uccm, in_be32(&uccf->uf_regs->uccm));
46 printk(KERN_INFO "uccs : addr - 0x%08x, val - 0x%02x",
47 (u32) & uccf->uf_regs->uccs, uccf->uf_regs->uccs);
48 printk(KERN_INFO "urfb : addr - 0x%08x, val - 0x%08x",
49 (u32) & uccf->uf_regs->urfb, in_be32(&uccf->uf_regs->urfb));
50 printk(KERN_INFO "urfs : addr - 0x%08x, val - 0x%04x",
51 (u32) & uccf->uf_regs->urfs, in_be16(&uccf->uf_regs->urfs));
52 printk(KERN_INFO "urfet : addr - 0x%08x, val - 0x%04x",
53 (u32) & uccf->uf_regs->urfet, in_be16(&uccf->uf_regs->urfet));
54 printk(KERN_INFO "urfset: addr - 0x%08x, val - 0x%04x",
55 (u32) & uccf->uf_regs->urfset,
56 in_be16(&uccf->uf_regs->urfset));
57 printk(KERN_INFO "utfb : addr - 0x%08x, val - 0x%08x",
58 (u32) & uccf->uf_regs->utfb, in_be32(&uccf->uf_regs->utfb));
59 printk(KERN_INFO "utfs : addr - 0x%08x, val - 0x%04x",
60 (u32) & uccf->uf_regs->utfs, in_be16(&uccf->uf_regs->utfs));
61 printk(KERN_INFO "utfet : addr - 0x%08x, val - 0x%04x",
62 (u32) & uccf->uf_regs->utfet, in_be16(&uccf->uf_regs->utfet));
63 printk(KERN_INFO "utftt : addr - 0x%08x, val - 0x%04x",
64 (u32) & uccf->uf_regs->utftt, in_be16(&uccf->uf_regs->utftt));
65 printk(KERN_INFO "utpt : addr - 0x%08x, val - 0x%04x",
66 (u32) & uccf->uf_regs->utpt, in_be16(&uccf->uf_regs->utpt));
67 printk(KERN_INFO "urtry : addr - 0x%08x, val - 0x%08x",
68 (u32) & uccf->uf_regs->urtry, in_be32(&uccf->uf_regs->urtry));
69 printk(KERN_INFO "guemr : addr - 0x%08x, val - 0x%02x",
70 (u32) & uccf->uf_regs->guemr, uccf->uf_regs->guemr);
73 u32 ucc_fast_get_qe_cr_subblock(int uccf_num)
76 case 0: return QE_CR_SUBBLOCK_UCCFAST1;
77 case 1: return QE_CR_SUBBLOCK_UCCFAST2;
78 case 2: return QE_CR_SUBBLOCK_UCCFAST3;
79 case 3: return QE_CR_SUBBLOCK_UCCFAST4;
80 case 4: return QE_CR_SUBBLOCK_UCCFAST5;
81 case 5: return QE_CR_SUBBLOCK_UCCFAST6;
82 case 6: return QE_CR_SUBBLOCK_UCCFAST7;
83 case 7: return QE_CR_SUBBLOCK_UCCFAST8;
84 default: return QE_CR_SUBBLOCK_INVALID;
88 void ucc_fast_transmit_on_demand(struct ucc_fast_private * uccf)
90 out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
93 void ucc_fast_enable(struct ucc_fast_private * uccf, enum comm_dir mode)
95 struct ucc_fast *uf_regs;
98 uf_regs = uccf->uf_regs;
100 /* Enable reception and/or transmission on this UCC. */
101 gumr = in_be32(&uf_regs->gumr);
102 if (mode & COMM_DIR_TX) {
103 gumr |= UCC_FAST_GUMR_ENT;
104 uccf->enabled_tx = 1;
106 if (mode & COMM_DIR_RX) {
107 gumr |= UCC_FAST_GUMR_ENR;
108 uccf->enabled_rx = 1;
110 out_be32(&uf_regs->gumr, gumr);
113 void ucc_fast_disable(struct ucc_fast_private * uccf, enum comm_dir mode)
115 struct ucc_fast *uf_regs;
118 uf_regs = uccf->uf_regs;
120 /* Disable reception and/or transmission on this UCC. */
121 gumr = in_be32(&uf_regs->gumr);
122 if (mode & COMM_DIR_TX) {
123 gumr &= ~UCC_FAST_GUMR_ENT;
124 uccf->enabled_tx = 0;
126 if (mode & COMM_DIR_RX) {
127 gumr &= ~UCC_FAST_GUMR_ENR;
128 uccf->enabled_rx = 0;
130 out_be32(&uf_regs->gumr, gumr);
133 int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** uccf_ret)
135 struct ucc_fast_private *uccf;
136 struct ucc_fast *uf_regs;
143 /* check if the UCC port number is in range. */
144 if ((uf_info->ucc_num < 0) || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
145 printk(KERN_ERR "%s: illegal UCC number", __FUNCTION__);
149 /* Check that 'max_rx_buf_length' is properly aligned (4). */
150 if (uf_info->max_rx_buf_length & (UCC_FAST_MRBLR_ALIGNMENT - 1)) {
151 printk(KERN_ERR "%s: max_rx_buf_length not aligned", __FUNCTION__);
155 /* Validate Virtual Fifo register values */
156 if (uf_info->urfs < UCC_FAST_URFS_MIN_VAL) {
157 printk(KERN_ERR "%s: urfs is too small", __FUNCTION__);
161 if (uf_info->urfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
162 printk(KERN_ERR "%s: urfs is not aligned", __FUNCTION__);
166 if (uf_info->urfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
167 printk(KERN_ERR "%s: urfet is not aligned.", __FUNCTION__);
171 if (uf_info->urfset & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
172 printk(KERN_ERR "%s: urfset is not aligned", __FUNCTION__);
176 if (uf_info->utfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
177 printk(KERN_ERR "%s: utfs is not aligned", __FUNCTION__);
181 if (uf_info->utfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
182 printk(KERN_ERR "%s: utfet is not aligned", __FUNCTION__);
186 if (uf_info->utftt & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
187 printk(KERN_ERR "%s: utftt is not aligned", __FUNCTION__);
191 uccf = kzalloc(sizeof(struct ucc_fast_private), GFP_KERNEL);
193 printk(KERN_ERR "%s: Cannot allocate private data", __FUNCTION__);
197 /* Fill fast UCC structure */
198 uccf->uf_info = uf_info;
199 /* Set the PHY base address */
200 uccf->uf_regs = ioremap(uf_info->regs, sizeof(struct ucc_fast));
201 if (uccf->uf_regs == NULL) {
202 printk(KERN_ERR "%s: Cannot map UCC registers", __FUNCTION__);
206 uccf->enabled_tx = 0;
207 uccf->enabled_rx = 0;
208 uccf->stopped_tx = 0;
209 uccf->stopped_rx = 0;
210 uf_regs = uccf->uf_regs;
211 uccf->p_ucce = (u32 *) & (uf_regs->ucce);
212 uccf->p_uccm = (u32 *) & (uf_regs->uccm);
216 uccf->rx_discarded = 0;
217 #endif /* STATISTICS */
219 /* Init Guemr register */
220 if ((ret = ucc_init_guemr((struct ucc_common *) (uf_regs)))) {
221 printk(KERN_ERR "%s: cannot init GUEMR", __FUNCTION__);
226 /* Set UCC to fast type */
227 if ((ret = ucc_set_type(uf_info->ucc_num,
228 (struct ucc_common *) (uf_regs),
229 UCC_SPEED_TYPE_FAST))) {
230 printk(KERN_ERR "%s: cannot set UCC type", __FUNCTION__);
235 uccf->mrblr = uf_info->max_rx_buf_length;
238 /* For more details see the hardware spec. */
239 gumr = uf_info->ttx_trx;
241 gumr |= UCC_FAST_GUMR_TCI;
243 gumr |= UCC_FAST_GUMR_CDP;
245 gumr |= UCC_FAST_GUMR_CTSP;
247 gumr |= UCC_FAST_GUMR_CDS;
249 gumr |= UCC_FAST_GUMR_CTSS;
251 gumr |= UCC_FAST_GUMR_TXSY;
253 gumr |= UCC_FAST_GUMR_RSYN;
254 gumr |= uf_info->synl;
256 gumr |= UCC_FAST_GUMR_RTSM;
257 gumr |= uf_info->renc;
259 gumr |= UCC_FAST_GUMR_REVD;
260 gumr |= uf_info->tenc;
261 gumr |= uf_info->tcrc;
262 gumr |= uf_info->mode;
263 out_be32(&uf_regs->gumr, gumr);
265 /* Allocate memory for Tx Virtual Fifo */
266 uccf->ucc_fast_tx_virtual_fifo_base_offset =
267 qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
268 if (IS_MURAM_ERR(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
269 printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO", __FUNCTION__);
270 uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
275 /* Allocate memory for Rx Virtual Fifo */
276 uccf->ucc_fast_rx_virtual_fifo_base_offset =
277 qe_muram_alloc(uf_info->urfs +
278 UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
279 UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
280 if (IS_MURAM_ERR(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
281 printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO", __FUNCTION__);
282 uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
287 /* Set Virtual Fifo registers */
288 out_be16(&uf_regs->urfs, uf_info->urfs);
289 out_be16(&uf_regs->urfet, uf_info->urfet);
290 out_be16(&uf_regs->urfset, uf_info->urfset);
291 out_be16(&uf_regs->utfs, uf_info->utfs);
292 out_be16(&uf_regs->utfet, uf_info->utfet);
293 out_be16(&uf_regs->utftt, uf_info->utftt);
294 /* utfb, urfb are offsets from MURAM base */
295 out_be32(&uf_regs->utfb, uccf->ucc_fast_tx_virtual_fifo_base_offset);
296 out_be32(&uf_regs->urfb, uccf->ucc_fast_rx_virtual_fifo_base_offset);
300 ucc_set_qe_mux_grant(uf_info->ucc_num, uf_info->grant_support);
301 /* Breakpoint Support */
302 ucc_set_qe_mux_bkpt(uf_info->ucc_num, uf_info->brkpt_support);
303 /* Set Tsa or NMSI mode. */
304 ucc_set_qe_mux_tsa(uf_info->ucc_num, uf_info->tsa);
305 /* If NMSI (not Tsa), set Tx and Rx clock. */
307 /* Rx clock routing */
308 if ((uf_info->rx_clock != QE_CLK_NONE) &&
309 ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->rx_clock,
311 printk(KERN_ERR "%s: illegal value for RX clock",
316 /* Tx clock routing */
317 if ((uf_info->tx_clock != QE_CLK_NONE) &&
318 ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->tx_clock,
320 printk(KERN_ERR "%s: illegal value for TX clock",
327 /* Set interrupt mask register at UCC level. */
328 out_be32(&uf_regs->uccm, uf_info->uccm_mask);
330 /* First, clear anything pending at UCC level,
331 * otherwise, old garbage may come through
332 * as soon as the dam is opened. */
334 /* Writing '1' clears */
335 out_be32(&uf_regs->ucce, 0xffffffff);
341 void ucc_fast_free(struct ucc_fast_private * uccf)
346 if (uccf->ucc_fast_tx_virtual_fifo_base_offset)
347 qe_muram_free(uccf->ucc_fast_tx_virtual_fifo_base_offset);
349 if (uccf->ucc_fast_rx_virtual_fifo_base_offset)
350 qe_muram_free(uccf->ucc_fast_rx_virtual_fifo_base_offset);