blob: a457ac1c663924b030c931cfd7a28c84801b2757 [file] [log] [blame]
Li Yang98658532006-10-03 23:10:46 -05001/*
Li Yang98658532006-10-03 23:10:46 -05002 * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
3 *
4 * Authors: Shlomi Gridish <gridish@freescale.com>
5 * Li Yang <leoli@freescale.com>
6 *
Timur Tabi5af68af2007-02-16 22:31:21 -06007 * Description:
8 * QE UCC Fast API Set - UCC Fast specific routines implementations.
9 *
Li Yang98658532006-10-03 23:10:46 -050010 * 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.
14 */
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>
21
22#include <asm/io.h>
23#include <asm/immap_qe.h>
24#include <asm/qe.h>
25
26#include <asm/ucc.h>
27#include <asm/ucc_fast.h>
28
Li Yang98658532006-10-03 23:10:46 -050029void ucc_fast_dump_regs(struct ucc_fast_private * uccf)
30{
Timur Tabi5af68af2007-02-16 22:31:21 -060031 printk(KERN_INFO "UCC%d Fast registers:", uccf->uf_info->ucc_num);
32 printk(KERN_INFO "Base address: 0x%08x", (u32) uccf->uf_regs);
Li Yang98658532006-10-03 23:10:46 -050033
Timur Tabi5af68af2007-02-16 22:31:21 -060034 printk(KERN_INFO "gumr : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050035 (u32) & uccf->uf_regs->gumr, in_be32(&uccf->uf_regs->gumr));
Timur Tabi5af68af2007-02-16 22:31:21 -060036 printk(KERN_INFO "upsmr : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050037 (u32) & uccf->uf_regs->upsmr, in_be32(&uccf->uf_regs->upsmr));
Timur Tabi5af68af2007-02-16 22:31:21 -060038 printk(KERN_INFO "utodr : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050039 (u32) & uccf->uf_regs->utodr, in_be16(&uccf->uf_regs->utodr));
Timur Tabi5af68af2007-02-16 22:31:21 -060040 printk(KERN_INFO "udsr : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050041 (u32) & uccf->uf_regs->udsr, in_be16(&uccf->uf_regs->udsr));
Timur Tabi5af68af2007-02-16 22:31:21 -060042 printk(KERN_INFO "ucce : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050043 (u32) & uccf->uf_regs->ucce, in_be32(&uccf->uf_regs->ucce));
Timur Tabi5af68af2007-02-16 22:31:21 -060044 printk(KERN_INFO "uccm : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050045 (u32) & uccf->uf_regs->uccm, in_be32(&uccf->uf_regs->uccm));
Timur Tabi5af68af2007-02-16 22:31:21 -060046 printk(KERN_INFO "uccs : addr - 0x%08x, val - 0x%02x",
Li Yang98658532006-10-03 23:10:46 -050047 (u32) & uccf->uf_regs->uccs, uccf->uf_regs->uccs);
Timur Tabi5af68af2007-02-16 22:31:21 -060048 printk(KERN_INFO "urfb : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050049 (u32) & uccf->uf_regs->urfb, in_be32(&uccf->uf_regs->urfb));
Timur Tabi5af68af2007-02-16 22:31:21 -060050 printk(KERN_INFO "urfs : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050051 (u32) & uccf->uf_regs->urfs, in_be16(&uccf->uf_regs->urfs));
Timur Tabi5af68af2007-02-16 22:31:21 -060052 printk(KERN_INFO "urfet : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050053 (u32) & uccf->uf_regs->urfet, in_be16(&uccf->uf_regs->urfet));
Timur Tabi5af68af2007-02-16 22:31:21 -060054 printk(KERN_INFO "urfset: addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050055 (u32) & uccf->uf_regs->urfset,
56 in_be16(&uccf->uf_regs->urfset));
Timur Tabi5af68af2007-02-16 22:31:21 -060057 printk(KERN_INFO "utfb : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050058 (u32) & uccf->uf_regs->utfb, in_be32(&uccf->uf_regs->utfb));
Timur Tabi5af68af2007-02-16 22:31:21 -060059 printk(KERN_INFO "utfs : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050060 (u32) & uccf->uf_regs->utfs, in_be16(&uccf->uf_regs->utfs));
Timur Tabi5af68af2007-02-16 22:31:21 -060061 printk(KERN_INFO "utfet : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050062 (u32) & uccf->uf_regs->utfet, in_be16(&uccf->uf_regs->utfet));
Timur Tabi5af68af2007-02-16 22:31:21 -060063 printk(KERN_INFO "utftt : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050064 (u32) & uccf->uf_regs->utftt, in_be16(&uccf->uf_regs->utftt));
Timur Tabi5af68af2007-02-16 22:31:21 -060065 printk(KERN_INFO "utpt : addr - 0x%08x, val - 0x%04x",
Li Yang98658532006-10-03 23:10:46 -050066 (u32) & uccf->uf_regs->utpt, in_be16(&uccf->uf_regs->utpt));
Timur Tabi5af68af2007-02-16 22:31:21 -060067 printk(KERN_INFO "urtry : addr - 0x%08x, val - 0x%08x",
Li Yang98658532006-10-03 23:10:46 -050068 (u32) & uccf->uf_regs->urtry, in_be32(&uccf->uf_regs->urtry));
Timur Tabi5af68af2007-02-16 22:31:21 -060069 printk(KERN_INFO "guemr : addr - 0x%08x, val - 0x%02x",
Li Yang98658532006-10-03 23:10:46 -050070 (u32) & uccf->uf_regs->guemr, uccf->uf_regs->guemr);
71}
72
73u32 ucc_fast_get_qe_cr_subblock(int uccf_num)
74{
75 switch (uccf_num) {
Timur Tabi5af68af2007-02-16 22:31:21 -060076 case 0: return QE_CR_SUBBLOCK_UCCFAST1;
Li Yang98658532006-10-03 23:10:46 -050077 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;
Timur Tabi5af68af2007-02-16 22:31:21 -060083 case 7: return QE_CR_SUBBLOCK_UCCFAST8;
Li Yang98658532006-10-03 23:10:46 -050084 default: return QE_CR_SUBBLOCK_INVALID;
85 }
86}
87
88void ucc_fast_transmit_on_demand(struct ucc_fast_private * uccf)
89{
90 out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
91}
92
93void ucc_fast_enable(struct ucc_fast_private * uccf, enum comm_dir mode)
94{
95 struct ucc_fast *uf_regs;
96 u32 gumr;
97
98 uf_regs = uccf->uf_regs;
99
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;
105 }
106 if (mode & COMM_DIR_RX) {
107 gumr |= UCC_FAST_GUMR_ENR;
108 uccf->enabled_rx = 1;
109 }
110 out_be32(&uf_regs->gumr, gumr);
111}
112
113void ucc_fast_disable(struct ucc_fast_private * uccf, enum comm_dir mode)
114{
115 struct ucc_fast *uf_regs;
116 u32 gumr;
117
118 uf_regs = uccf->uf_regs;
119
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;
125 }
126 if (mode & COMM_DIR_RX) {
127 gumr &= ~UCC_FAST_GUMR_ENR;
128 uccf->enabled_rx = 0;
129 }
130 out_be32(&uf_regs->gumr, gumr);
131}
132
133int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** uccf_ret)
134{
135 struct ucc_fast_private *uccf;
136 struct ucc_fast *uf_regs;
Timur Tabi5af68af2007-02-16 22:31:21 -0600137 u32 gumr;
Li Yang98658532006-10-03 23:10:46 -0500138 int ret;
139
Li Yang98658532006-10-03 23:10:46 -0500140 if (!uf_info)
141 return -EINVAL;
142
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)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600145 printk(KERN_ERR "%s: illegal UCC number", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500146 return -EINVAL;
147 }
148
149 /* Check that 'max_rx_buf_length' is properly aligned (4). */
150 if (uf_info->max_rx_buf_length & (UCC_FAST_MRBLR_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600151 printk(KERN_ERR "%s: max_rx_buf_length not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500152 return -EINVAL;
153 }
154
155 /* Validate Virtual Fifo register values */
156 if (uf_info->urfs < UCC_FAST_URFS_MIN_VAL) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600157 printk(KERN_ERR "%s: urfs is too small", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500158 return -EINVAL;
159 }
160
161 if (uf_info->urfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600162 printk(KERN_ERR "%s: urfs is not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500163 return -EINVAL;
164 }
165
166 if (uf_info->urfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600167 printk(KERN_ERR "%s: urfet is not aligned.", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500168 return -EINVAL;
169 }
170
171 if (uf_info->urfset & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600172 printk(KERN_ERR "%s: urfset is not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500173 return -EINVAL;
174 }
175
176 if (uf_info->utfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600177 printk(KERN_ERR "%s: utfs is not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500178 return -EINVAL;
179 }
180
181 if (uf_info->utfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600182 printk(KERN_ERR "%s: utfet is not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500183 return -EINVAL;
184 }
185
186 if (uf_info->utftt & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600187 printk(KERN_ERR "%s: utftt is not aligned", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500188 return -EINVAL;
189 }
190
Yan Burmanf8485352006-12-02 13:26:57 +0200191 uccf = kzalloc(sizeof(struct ucc_fast_private), GFP_KERNEL);
Li Yang98658532006-10-03 23:10:46 -0500192 if (!uccf) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600193 printk(KERN_ERR "%s: Cannot allocate private data", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500194 return -ENOMEM;
195 }
Li Yang98658532006-10-03 23:10:46 -0500196
197 /* Fill fast UCC structure */
198 uccf->uf_info = uf_info;
199 /* Set the PHY base address */
Timur Tabi5af68af2007-02-16 22:31:21 -0600200 uccf->uf_regs = ioremap(uf_info->regs, sizeof(struct ucc_fast));
Li Yang98658532006-10-03 23:10:46 -0500201 if (uccf->uf_regs == NULL) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600202 printk(KERN_ERR "%s: Cannot map UCC registers", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500203 return -ENOMEM;
204 }
205
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);
213#ifdef STATISTICS
214 uccf->tx_frames = 0;
215 uccf->rx_frames = 0;
216 uccf->rx_discarded = 0;
217#endif /* STATISTICS */
218
219 /* Init Guemr register */
220 if ((ret = ucc_init_guemr((struct ucc_common *) (uf_regs)))) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600221 printk(KERN_ERR "%s: cannot init GUEMR", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500222 ucc_fast_free(uccf);
223 return ret;
224 }
225
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))) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600230 printk(KERN_ERR "%s: cannot set UCC type", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500231 ucc_fast_free(uccf);
232 return ret;
233 }
234
235 uccf->mrblr = uf_info->max_rx_buf_length;
236
237 /* Set GUMR */
238 /* For more details see the hardware spec. */
Timur Tabi5af68af2007-02-16 22:31:21 -0600239 gumr = uf_info->ttx_trx;
Li Yang98658532006-10-03 23:10:46 -0500240 if (uf_info->tci)
241 gumr |= UCC_FAST_GUMR_TCI;
Li Yang98658532006-10-03 23:10:46 -0500242 if (uf_info->cdp)
243 gumr |= UCC_FAST_GUMR_CDP;
244 if (uf_info->ctsp)
245 gumr |= UCC_FAST_GUMR_CTSP;
246 if (uf_info->cds)
247 gumr |= UCC_FAST_GUMR_CDS;
248 if (uf_info->ctss)
249 gumr |= UCC_FAST_GUMR_CTSS;
250 if (uf_info->txsy)
251 gumr |= UCC_FAST_GUMR_TXSY;
252 if (uf_info->rsyn)
253 gumr |= UCC_FAST_GUMR_RSYN;
254 gumr |= uf_info->synl;
255 if (uf_info->rtsm)
256 gumr |= UCC_FAST_GUMR_RTSM;
257 gumr |= uf_info->renc;
258 if (uf_info->revd)
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);
264
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)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600269 printk(KERN_ERR "%s: cannot allocate MURAM for TX FIFO", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500270 uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
271 ucc_fast_free(uccf);
272 return -ENOMEM;
273 }
274
275 /* Allocate memory for Rx Virtual Fifo */
276 uccf->ucc_fast_rx_virtual_fifo_base_offset =
Timur Tabi5af68af2007-02-16 22:31:21 -0600277 qe_muram_alloc(uf_info->urfs +
Li Yang98658532006-10-03 23:10:46 -0500278 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)) {
Timur Tabi5af68af2007-02-16 22:31:21 -0600281 printk(KERN_ERR "%s: cannot allocate MURAM for RX FIFO", __FUNCTION__);
Li Yang98658532006-10-03 23:10:46 -0500282 uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
283 ucc_fast_free(uccf);
284 return -ENOMEM;
285 }
286
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);
297
298 /* Mux clocking */
299 /* Grant Support */
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. */
306 if (!uf_info->tsa) {
307 /* Rx clock routing */
Timur Tabi5af68af2007-02-16 22:31:21 -0600308 if ((uf_info->rx_clock != QE_CLK_NONE) &&
309 ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->rx_clock,
310 COMM_DIR_RX)) {
311 printk(KERN_ERR "%s: illegal value for RX clock",
312 __FUNCTION__);
313 ucc_fast_free(uccf);
314 return -EINVAL;
Li Yang98658532006-10-03 23:10:46 -0500315 }
316 /* Tx clock routing */
Timur Tabi5af68af2007-02-16 22:31:21 -0600317 if ((uf_info->tx_clock != QE_CLK_NONE) &&
318 ucc_set_qe_mux_rxtx(uf_info->ucc_num, uf_info->tx_clock,
319 COMM_DIR_TX)) {
320 printk(KERN_ERR "%s: illegal value for TX clock",
321 __FUNCTION__);
322 ucc_fast_free(uccf);
323 return -EINVAL;
Li Yang98658532006-10-03 23:10:46 -0500324 }
325 }
326
327 /* Set interrupt mask register at UCC level. */
328 out_be32(&uf_regs->uccm, uf_info->uccm_mask);
329
330 /* First, clear anything pending at UCC level,
331 * otherwise, old garbage may come through
Timur Tabi5af68af2007-02-16 22:31:21 -0600332 * as soon as the dam is opened. */
333
334 /* Writing '1' clears */
Li Yang98658532006-10-03 23:10:46 -0500335 out_be32(&uf_regs->ucce, 0xffffffff);
336
337 *uccf_ret = uccf;
338 return 0;
339}
340
341void ucc_fast_free(struct ucc_fast_private * uccf)
342{
343 if (!uccf)
344 return;
345
346 if (uccf->ucc_fast_tx_virtual_fifo_base_offset)
347 qe_muram_free(uccf->ucc_fast_tx_virtual_fifo_base_offset);
348
349 if (uccf->ucc_fast_rx_virtual_fifo_base_offset)
350 qe_muram_free(uccf->ucc_fast_rx_virtual_fifo_base_offset);
351
352 kfree(uccf);
353}