Pull style into test branch
[linux-2.6] / arch / powerpc / sysdev / qe_lib / ucc_fast.c
1 /*
2  * arch/powerpc/sysdev/qe_lib/ucc_fast.c
3  *
4  * QE UCC Fast API Set - UCC Fast specific routines implementations.
5  *
6  * Copyright (C) 2006 Freescale Semicondutor, Inc. All rights reserved.
7  *
8  * Authors:     Shlomi Gridish <gridish@freescale.com>
9  *              Li Yang <leoli@freescale.com>
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  */
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/errno.h>
19 #include <linux/slab.h>
20 #include <linux/stddef.h>
21 #include <linux/interrupt.h>
22
23 #include <asm/io.h>
24 #include <asm/immap_qe.h>
25 #include <asm/qe.h>
26
27 #include <asm/ucc.h>
28 #include <asm/ucc_fast.h>
29
30 #define uccf_printk(level, format, arg...) \
31         printk(level format "\n", ## arg)
32
33 #define uccf_dbg(format, arg...) \
34         uccf_printk(KERN_DEBUG , format , ## arg)
35 #define uccf_err(format, arg...) \
36         uccf_printk(KERN_ERR , format , ## arg)
37 #define uccf_info(format, arg...) \
38         uccf_printk(KERN_INFO , format , ## arg)
39 #define uccf_warn(format, arg...) \
40         uccf_printk(KERN_WARNING , format , ## arg)
41
42 #ifdef UCCF_VERBOSE_DEBUG
43 #define uccf_vdbg uccf_dbg
44 #else
45 #define uccf_vdbg(fmt, args...) do { } while (0)
46 #endif                          /* UCCF_VERBOSE_DEBUG */
47
48 void ucc_fast_dump_regs(struct ucc_fast_private * uccf)
49 {
50         uccf_info("UCC%d Fast registers:", uccf->uf_info->ucc_num);
51         uccf_info("Base address: 0x%08x", (u32) uccf->uf_regs);
52
53         uccf_info("gumr  : addr - 0x%08x, val - 0x%08x",
54                   (u32) & uccf->uf_regs->gumr, in_be32(&uccf->uf_regs->gumr));
55         uccf_info("upsmr : addr - 0x%08x, val - 0x%08x",
56                   (u32) & uccf->uf_regs->upsmr, in_be32(&uccf->uf_regs->upsmr));
57         uccf_info("utodr : addr - 0x%08x, val - 0x%04x",
58                   (u32) & uccf->uf_regs->utodr, in_be16(&uccf->uf_regs->utodr));
59         uccf_info("udsr  : addr - 0x%08x, val - 0x%04x",
60                   (u32) & uccf->uf_regs->udsr, in_be16(&uccf->uf_regs->udsr));
61         uccf_info("ucce  : addr - 0x%08x, val - 0x%08x",
62                   (u32) & uccf->uf_regs->ucce, in_be32(&uccf->uf_regs->ucce));
63         uccf_info("uccm  : addr - 0x%08x, val - 0x%08x",
64                   (u32) & uccf->uf_regs->uccm, in_be32(&uccf->uf_regs->uccm));
65         uccf_info("uccs  : addr - 0x%08x, val - 0x%02x",
66                   (u32) & uccf->uf_regs->uccs, uccf->uf_regs->uccs);
67         uccf_info("urfb  : addr - 0x%08x, val - 0x%08x",
68                   (u32) & uccf->uf_regs->urfb, in_be32(&uccf->uf_regs->urfb));
69         uccf_info("urfs  : addr - 0x%08x, val - 0x%04x",
70                   (u32) & uccf->uf_regs->urfs, in_be16(&uccf->uf_regs->urfs));
71         uccf_info("urfet : addr - 0x%08x, val - 0x%04x",
72                   (u32) & uccf->uf_regs->urfet, in_be16(&uccf->uf_regs->urfet));
73         uccf_info("urfset: addr - 0x%08x, val - 0x%04x",
74                   (u32) & uccf->uf_regs->urfset,
75                   in_be16(&uccf->uf_regs->urfset));
76         uccf_info("utfb  : addr - 0x%08x, val - 0x%08x",
77                   (u32) & uccf->uf_regs->utfb, in_be32(&uccf->uf_regs->utfb));
78         uccf_info("utfs  : addr - 0x%08x, val - 0x%04x",
79                   (u32) & uccf->uf_regs->utfs, in_be16(&uccf->uf_regs->utfs));
80         uccf_info("utfet : addr - 0x%08x, val - 0x%04x",
81                   (u32) & uccf->uf_regs->utfet, in_be16(&uccf->uf_regs->utfet));
82         uccf_info("utftt : addr - 0x%08x, val - 0x%04x",
83                   (u32) & uccf->uf_regs->utftt, in_be16(&uccf->uf_regs->utftt));
84         uccf_info("utpt  : addr - 0x%08x, val - 0x%04x",
85                   (u32) & uccf->uf_regs->utpt, in_be16(&uccf->uf_regs->utpt));
86         uccf_info("urtry : addr - 0x%08x, val - 0x%08x",
87                   (u32) & uccf->uf_regs->urtry, in_be32(&uccf->uf_regs->urtry));
88         uccf_info("guemr : addr - 0x%08x, val - 0x%02x",
89                   (u32) & uccf->uf_regs->guemr, uccf->uf_regs->guemr);
90 }
91
92 u32 ucc_fast_get_qe_cr_subblock(int uccf_num)
93 {
94         switch (uccf_num) {
95         case 0: return QE_CR_SUBBLOCK_UCCFAST1;
96         case 1: return QE_CR_SUBBLOCK_UCCFAST2;
97         case 2: return QE_CR_SUBBLOCK_UCCFAST3;
98         case 3: return QE_CR_SUBBLOCK_UCCFAST4;
99         case 4: return QE_CR_SUBBLOCK_UCCFAST5;
100         case 5: return QE_CR_SUBBLOCK_UCCFAST6;
101         case 6: return QE_CR_SUBBLOCK_UCCFAST7;
102         case 7: return QE_CR_SUBBLOCK_UCCFAST8;
103         default: return QE_CR_SUBBLOCK_INVALID;
104         }
105 }
106
107 void ucc_fast_transmit_on_demand(struct ucc_fast_private * uccf)
108 {
109         out_be16(&uccf->uf_regs->utodr, UCC_FAST_TOD);
110 }
111
112 void ucc_fast_enable(struct ucc_fast_private * uccf, enum comm_dir mode)
113 {
114         struct ucc_fast *uf_regs;
115         u32 gumr;
116
117         uf_regs = uccf->uf_regs;
118
119         /* Enable reception and/or transmission on this UCC. */
120         gumr = in_be32(&uf_regs->gumr);
121         if (mode & COMM_DIR_TX) {
122                 gumr |= UCC_FAST_GUMR_ENT;
123                 uccf->enabled_tx = 1;
124         }
125         if (mode & COMM_DIR_RX) {
126                 gumr |= UCC_FAST_GUMR_ENR;
127                 uccf->enabled_rx = 1;
128         }
129         out_be32(&uf_regs->gumr, gumr);
130 }
131
132 void ucc_fast_disable(struct ucc_fast_private * uccf, enum comm_dir mode)
133 {
134         struct ucc_fast *uf_regs;
135         u32 gumr;
136
137         uf_regs = uccf->uf_regs;
138
139         /* Disable reception and/or transmission on this UCC. */
140         gumr = in_be32(&uf_regs->gumr);
141         if (mode & COMM_DIR_TX) {
142                 gumr &= ~UCC_FAST_GUMR_ENT;
143                 uccf->enabled_tx = 0;
144         }
145         if (mode & COMM_DIR_RX) {
146                 gumr &= ~UCC_FAST_GUMR_ENR;
147                 uccf->enabled_rx = 0;
148         }
149         out_be32(&uf_regs->gumr, gumr);
150 }
151
152 int ucc_fast_init(struct ucc_fast_info * uf_info, struct ucc_fast_private ** uccf_ret)
153 {
154         struct ucc_fast_private *uccf;
155         struct ucc_fast *uf_regs;
156         u32 gumr = 0;
157         int ret;
158
159         uccf_vdbg("%s: IN", __FUNCTION__);
160
161         if (!uf_info)
162                 return -EINVAL;
163
164         /* check if the UCC port number is in range. */
165         if ((uf_info->ucc_num < 0) || (uf_info->ucc_num > UCC_MAX_NUM - 1)) {
166                 uccf_err("ucc_fast_init: Illegal UCC number!");
167                 return -EINVAL;
168         }
169
170         /* Check that 'max_rx_buf_length' is properly aligned (4). */
171         if (uf_info->max_rx_buf_length & (UCC_FAST_MRBLR_ALIGNMENT - 1)) {
172                 uccf_err("ucc_fast_init: max_rx_buf_length not aligned.");
173                 return -EINVAL;
174         }
175
176         /* Validate Virtual Fifo register values */
177         if (uf_info->urfs < UCC_FAST_URFS_MIN_VAL) {
178                 uccf_err
179                     ("ucc_fast_init: Virtual Fifo register urfs too small.");
180                 return -EINVAL;
181         }
182
183         if (uf_info->urfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
184                 uccf_err
185                     ("ucc_fast_init: Virtual Fifo register urfs not aligned.");
186                 return -EINVAL;
187         }
188
189         if (uf_info->urfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
190                 uccf_err
191                     ("ucc_fast_init: Virtual Fifo register urfet not aligned.");
192                 return -EINVAL;
193         }
194
195         if (uf_info->urfset & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
196                 uccf_err
197                    ("ucc_fast_init: Virtual Fifo register urfset not aligned.");
198                 return -EINVAL;
199         }
200
201         if (uf_info->utfs & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
202                 uccf_err
203                     ("ucc_fast_init: Virtual Fifo register utfs not aligned.");
204                 return -EINVAL;
205         }
206
207         if (uf_info->utfet & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
208                 uccf_err
209                     ("ucc_fast_init: Virtual Fifo register utfet not aligned.");
210                 return -EINVAL;
211         }
212
213         if (uf_info->utftt & (UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT - 1)) {
214                 uccf_err
215                     ("ucc_fast_init: Virtual Fifo register utftt not aligned.");
216                 return -EINVAL;
217         }
218
219         uccf = kzalloc(sizeof(struct ucc_fast_private), GFP_KERNEL);
220         if (!uccf) {
221                 uccf_err
222                     ("ucc_fast_init: No memory for UCC slow data structure!");
223                 return -ENOMEM;
224         }
225
226         /* Fill fast UCC structure */
227         uccf->uf_info = uf_info;
228         /* Set the PHY base address */
229         uccf->uf_regs =
230             (struct ucc_fast *) ioremap(uf_info->regs, sizeof(struct ucc_fast));
231         if (uccf->uf_regs == NULL) {
232                 uccf_err
233                     ("ucc_fast_init: No memory map for UCC slow controller!");
234                 return -ENOMEM;
235         }
236
237         uccf->enabled_tx = 0;
238         uccf->enabled_rx = 0;
239         uccf->stopped_tx = 0;
240         uccf->stopped_rx = 0;
241         uf_regs = uccf->uf_regs;
242         uccf->p_ucce = (u32 *) & (uf_regs->ucce);
243         uccf->p_uccm = (u32 *) & (uf_regs->uccm);
244 #ifdef STATISTICS
245         uccf->tx_frames = 0;
246         uccf->rx_frames = 0;
247         uccf->rx_discarded = 0;
248 #endif                          /* STATISTICS */
249
250         /* Init Guemr register */
251         if ((ret = ucc_init_guemr((struct ucc_common *) (uf_regs)))) {
252                 uccf_err("ucc_fast_init: Could not init the guemr register.");
253                 ucc_fast_free(uccf);
254                 return ret;
255         }
256
257         /* Set UCC to fast type */
258         if ((ret = ucc_set_type(uf_info->ucc_num,
259                                 (struct ucc_common *) (uf_regs),
260                                 UCC_SPEED_TYPE_FAST))) {
261                 uccf_err("ucc_fast_init: Could not set type to fast.");
262                 ucc_fast_free(uccf);
263                 return ret;
264         }
265
266         uccf->mrblr = uf_info->max_rx_buf_length;
267
268         /* Set GUMR */
269         /* For more details see the hardware spec. */
270         /* gumr starts as zero. */
271         if (uf_info->tci)
272                 gumr |= UCC_FAST_GUMR_TCI;
273         gumr |= uf_info->ttx_trx;
274         if (uf_info->cdp)
275                 gumr |= UCC_FAST_GUMR_CDP;
276         if (uf_info->ctsp)
277                 gumr |= UCC_FAST_GUMR_CTSP;
278         if (uf_info->cds)
279                 gumr |= UCC_FAST_GUMR_CDS;
280         if (uf_info->ctss)
281                 gumr |= UCC_FAST_GUMR_CTSS;
282         if (uf_info->txsy)
283                 gumr |= UCC_FAST_GUMR_TXSY;
284         if (uf_info->rsyn)
285                 gumr |= UCC_FAST_GUMR_RSYN;
286         gumr |= uf_info->synl;
287         if (uf_info->rtsm)
288                 gumr |= UCC_FAST_GUMR_RTSM;
289         gumr |= uf_info->renc;
290         if (uf_info->revd)
291                 gumr |= UCC_FAST_GUMR_REVD;
292         gumr |= uf_info->tenc;
293         gumr |= uf_info->tcrc;
294         gumr |= uf_info->mode;
295         out_be32(&uf_regs->gumr, gumr);
296
297         /* Allocate memory for Tx Virtual Fifo */
298         uccf->ucc_fast_tx_virtual_fifo_base_offset =
299             qe_muram_alloc(uf_info->utfs, UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
300         if (IS_MURAM_ERR(uccf->ucc_fast_tx_virtual_fifo_base_offset)) {
301                 uccf_err
302                     ("ucc_fast_init: Can not allocate MURAM memory for "
303                         "struct ucc_fastx_virtual_fifo_base_offset.");
304                 uccf->ucc_fast_tx_virtual_fifo_base_offset = 0;
305                 ucc_fast_free(uccf);
306                 return -ENOMEM;
307         }
308
309         /* Allocate memory for Rx Virtual Fifo */
310         uccf->ucc_fast_rx_virtual_fifo_base_offset =
311             qe_muram_alloc(uf_info->urfs +
312                            (u32)
313                            UCC_FAST_RECEIVE_VIRTUAL_FIFO_SIZE_FUDGE_FACTOR,
314                            UCC_FAST_VIRT_FIFO_REGS_ALIGNMENT);
315         if (IS_MURAM_ERR(uccf->ucc_fast_rx_virtual_fifo_base_offset)) {
316                 uccf_err
317                     ("ucc_fast_init: Can not allocate MURAM memory for "
318                         "ucc_fast_rx_virtual_fifo_base_offset.");
319                 uccf->ucc_fast_rx_virtual_fifo_base_offset = 0;
320                 ucc_fast_free(uccf);
321                 return -ENOMEM;
322         }
323
324         /* Set Virtual Fifo registers */
325         out_be16(&uf_regs->urfs, uf_info->urfs);
326         out_be16(&uf_regs->urfet, uf_info->urfet);
327         out_be16(&uf_regs->urfset, uf_info->urfset);
328         out_be16(&uf_regs->utfs, uf_info->utfs);
329         out_be16(&uf_regs->utfet, uf_info->utfet);
330         out_be16(&uf_regs->utftt, uf_info->utftt);
331         /* utfb, urfb are offsets from MURAM base */
332         out_be32(&uf_regs->utfb, uccf->ucc_fast_tx_virtual_fifo_base_offset);
333         out_be32(&uf_regs->urfb, uccf->ucc_fast_rx_virtual_fifo_base_offset);
334
335         /* Mux clocking */
336         /* Grant Support */
337         ucc_set_qe_mux_grant(uf_info->ucc_num, uf_info->grant_support);
338         /* Breakpoint Support */
339         ucc_set_qe_mux_bkpt(uf_info->ucc_num, uf_info->brkpt_support);
340         /* Set Tsa or NMSI mode. */
341         ucc_set_qe_mux_tsa(uf_info->ucc_num, uf_info->tsa);
342         /* If NMSI (not Tsa), set Tx and Rx clock. */
343         if (!uf_info->tsa) {
344                 /* Rx clock routing */
345                 if (uf_info->rx_clock != QE_CLK_NONE) {
346                         if (ucc_set_qe_mux_rxtx
347                             (uf_info->ucc_num, uf_info->rx_clock,
348                              COMM_DIR_RX)) {
349                                 uccf_err
350                 ("ucc_fast_init: Illegal value for parameter 'RxClock'.");
351                                 ucc_fast_free(uccf);
352                                 return -EINVAL;
353                         }
354                 }
355                 /* Tx clock routing */
356                 if (uf_info->tx_clock != QE_CLK_NONE) {
357                         if (ucc_set_qe_mux_rxtx
358                             (uf_info->ucc_num, uf_info->tx_clock,
359                              COMM_DIR_TX)) {
360                                 uccf_err
361                 ("ucc_fast_init: Illegal value for parameter 'TxClock'.");
362                                 ucc_fast_free(uccf);
363                                 return -EINVAL;
364                         }
365                 }
366         }
367
368         /* Set interrupt mask register at UCC level. */
369         out_be32(&uf_regs->uccm, uf_info->uccm_mask);
370
371         /* First, clear anything pending at UCC level,
372          * otherwise, old garbage may come through
373          * as soon as the dam is opened
374          * Writing '1' clears
375          */
376         out_be32(&uf_regs->ucce, 0xffffffff);
377
378         *uccf_ret = uccf;
379         return 0;
380 }
381
382 void ucc_fast_free(struct ucc_fast_private * uccf)
383 {
384         if (!uccf)
385                 return;
386
387         if (uccf->ucc_fast_tx_virtual_fifo_base_offset)
388                 qe_muram_free(uccf->ucc_fast_tx_virtual_fifo_base_offset);
389
390         if (uccf->ucc_fast_rx_virtual_fifo_base_offset)
391                 qe_muram_free(uccf->ucc_fast_rx_virtual_fifo_base_offset);
392
393         kfree(uccf);
394 }