blob: f1c3395633b9aedebda4c1979e6801f659c9fa40 [file] [log] [blame]
Vitaly Bordugb0c110b2006-09-21 22:18:53 +04001/*
2 * General Purpose functions for the global management of the
3 * 8260 Communication Processor Module.
4 * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
5 * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
6 * 2.3.99 Updates
7 *
8 * 2006 (c) MontaVista Software, Inc.
9 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * Merged to arch/powerpc from arch/ppc/syslib/cpm2_common.c
11 *
12 * This file is licensed under the terms of the GNU General Public License
13 * version 2. This program is licensed "as is" without any warranty of any
14 * kind, whether express or implied.
15 */
16
17/*
18 *
19 * In addition to the individual control of the communication
20 * channels, there are a few functions that globally affect the
21 * communication processor.
22 *
23 * Buffer descriptors must be allocated from the dual ported memory
24 * space. The allocator for that is here. When the communication
25 * process is reset, we reclaim the memory available. There is
26 * currently no deallocator for this memory.
27 */
28#include <linux/errno.h>
29#include <linux/sched.h>
30#include <linux/kernel.h>
31#include <linux/param.h>
32#include <linux/string.h>
33#include <linux/mm.h>
34#include <linux/interrupt.h>
35#include <linux/module.h>
Scott Wood449012d2007-09-14 15:30:44 -050036#include <linux/of.h>
37
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040038#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/mpc8260.h>
41#include <asm/page.h>
42#include <asm/pgtable.h>
43#include <asm/cpm2.h>
44#include <asm/rheap.h>
45#include <asm/fs_pd.h>
46
47#include <sysdev/fsl_soc.h>
48
Scott Wood449012d2007-09-14 15:30:44 -050049cpm_cpm2_t __iomem *cpmp; /* Pointer to comm processor space */
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040050
51/* We allocate this here because it is used almost exclusively for
52 * the communication processor devices.
53 */
Scott Wood449012d2007-09-14 15:30:44 -050054cpm2_map_t __iomem *cpm2_immr;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040055
56#define CPM_MAP_SIZE (0x40000) /* 256k - the PQ3 reserve this amount
57 of space for CPM as it is larger
58 than on PQ2 */
59
Scott Woodcd2150bc2007-12-12 16:54:32 -060060void __init cpm2_reset(void)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040061{
Scott Wood449012d2007-09-14 15:30:44 -050062#ifdef CONFIG_PPC_85xx
63 cpm2_immr = ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
64#else
65 cpm2_immr = ioremap(get_immrbase(), CPM_MAP_SIZE);
66#endif
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040067
68 /* Reclaim the DP memory for our use.
69 */
Scott Wood15f8c602007-09-28 14:06:16 -050070 cpm_muram_init();
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040071
72 /* Tell everyone where the comm processor resides.
73 */
74 cpmp = &cpm2_immr->im_cpm;
Laurent Pinchart872a15d2008-04-10 17:02:38 +020075
76#ifndef CONFIG_PPC_EARLY_DEBUG_CPM
77 /* Reset the CPM.
78 */
79 cpm_command(CPM_CR_RST, 0);
80#endif
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040081}
82
Jochen Friedrich362f9b62007-11-26 18:03:40 +010083static DEFINE_SPINLOCK(cmd_lock);
84
85#define MAX_CR_CMD_LOOPS 10000
86
87int cpm_command(u32 command, u8 opcode)
88{
89 int i, ret;
90 unsigned long flags;
91
92 spin_lock_irqsave(&cmd_lock, flags);
93
94 ret = 0;
95 out_be32(&cpmp->cp_cpcr, command | opcode | CPM_CR_FLG);
96 for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
97 if ((in_be32(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
98 goto out;
99
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100100 printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
Jochen Friedrich362f9b62007-11-26 18:03:40 +0100101 ret = -EIO;
102out:
103 spin_unlock_irqrestore(&cmd_lock, flags);
104 return ret;
105}
106EXPORT_SYMBOL(cpm_command);
107
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400108/* Set a baud rate generator. This needs lots of work. There are
109 * eight BRGs, which can be connected to the CPM channels or output
110 * as clocks. The BRGs are in two different block of internal
111 * memory mapped space.
112 * The baud rate clock is the system clock divided by something.
113 * It was set up long ago during the initial boot phase and is
114 * is given to us.
115 * Baud rate clocks are zero-based in the driver code (as that maps
116 * to port numbers). Documentation uses 1-based numbering.
117 */
Laurent Pinchartdddb8d32008-07-22 18:00:43 +0200118void __cpm2_setbrg(uint brg, uint rate, uint clk, int div16, int src)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400119{
Scott Wood449012d2007-09-14 15:30:44 -0500120 u32 __iomem *bp;
Laurent Pinchartdddb8d32008-07-22 18:00:43 +0200121 u32 val;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400122
123 /* This is good enough to get SMCs running.....
124 */
125 if (brg < 4) {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400126 bp = cpm2_map_size(im_brgc1, 16);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400127 } else {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400128 bp = cpm2_map_size(im_brgc5, 16);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400129 brg -= 4;
130 }
131 bp += brg;
Laurent Pinchartdddb8d32008-07-22 18:00:43 +0200132 val = (((clk / rate) - 1) << 1) | CPM_BRG_EN | src;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400133 if (div16)
Scott Wood449012d2007-09-14 15:30:44 -0500134 val |= CPM_BRG_DIV16;
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400135
Scott Wood449012d2007-09-14 15:30:44 -0500136 out_be32(bp, val);
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400137 cpm2_unmap(bp);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400138}
Laurent Pinchartdddb8d32008-07-22 18:00:43 +0200139EXPORT_SYMBOL(__cpm2_setbrg);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400140
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400141int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
142{
143 int ret = 0;
144 int shift;
145 int i, bits = 0;
Scott Wood449012d2007-09-14 15:30:44 -0500146 cpmux_t __iomem *im_cpmux;
147 u32 __iomem *reg;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400148 u32 mask = 7;
Scott Wood2652d4e2007-07-16 13:26:35 -0500149
150 u8 clk_map[][3] = {
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400151 {CPM_CLK_FCC1, CPM_BRG5, 0},
152 {CPM_CLK_FCC1, CPM_BRG6, 1},
153 {CPM_CLK_FCC1, CPM_BRG7, 2},
154 {CPM_CLK_FCC1, CPM_BRG8, 3},
155 {CPM_CLK_FCC1, CPM_CLK9, 4},
156 {CPM_CLK_FCC1, CPM_CLK10, 5},
157 {CPM_CLK_FCC1, CPM_CLK11, 6},
158 {CPM_CLK_FCC1, CPM_CLK12, 7},
159 {CPM_CLK_FCC2, CPM_BRG5, 0},
160 {CPM_CLK_FCC2, CPM_BRG6, 1},
161 {CPM_CLK_FCC2, CPM_BRG7, 2},
162 {CPM_CLK_FCC2, CPM_BRG8, 3},
163 {CPM_CLK_FCC2, CPM_CLK13, 4},
164 {CPM_CLK_FCC2, CPM_CLK14, 5},
165 {CPM_CLK_FCC2, CPM_CLK15, 6},
166 {CPM_CLK_FCC2, CPM_CLK16, 7},
167 {CPM_CLK_FCC3, CPM_BRG5, 0},
168 {CPM_CLK_FCC3, CPM_BRG6, 1},
169 {CPM_CLK_FCC3, CPM_BRG7, 2},
170 {CPM_CLK_FCC3, CPM_BRG8, 3},
171 {CPM_CLK_FCC3, CPM_CLK13, 4},
172 {CPM_CLK_FCC3, CPM_CLK14, 5},
173 {CPM_CLK_FCC3, CPM_CLK15, 6},
Scott Wood2652d4e2007-07-16 13:26:35 -0500174 {CPM_CLK_FCC3, CPM_CLK16, 7},
175 {CPM_CLK_SCC1, CPM_BRG1, 0},
176 {CPM_CLK_SCC1, CPM_BRG2, 1},
177 {CPM_CLK_SCC1, CPM_BRG3, 2},
178 {CPM_CLK_SCC1, CPM_BRG4, 3},
179 {CPM_CLK_SCC1, CPM_CLK11, 4},
180 {CPM_CLK_SCC1, CPM_CLK12, 5},
181 {CPM_CLK_SCC1, CPM_CLK3, 6},
182 {CPM_CLK_SCC1, CPM_CLK4, 7},
183 {CPM_CLK_SCC2, CPM_BRG1, 0},
184 {CPM_CLK_SCC2, CPM_BRG2, 1},
185 {CPM_CLK_SCC2, CPM_BRG3, 2},
186 {CPM_CLK_SCC2, CPM_BRG4, 3},
187 {CPM_CLK_SCC2, CPM_CLK11, 4},
188 {CPM_CLK_SCC2, CPM_CLK12, 5},
189 {CPM_CLK_SCC2, CPM_CLK3, 6},
190 {CPM_CLK_SCC2, CPM_CLK4, 7},
191 {CPM_CLK_SCC3, CPM_BRG1, 0},
192 {CPM_CLK_SCC3, CPM_BRG2, 1},
193 {CPM_CLK_SCC3, CPM_BRG3, 2},
194 {CPM_CLK_SCC3, CPM_BRG4, 3},
195 {CPM_CLK_SCC3, CPM_CLK5, 4},
196 {CPM_CLK_SCC3, CPM_CLK6, 5},
197 {CPM_CLK_SCC3, CPM_CLK7, 6},
198 {CPM_CLK_SCC3, CPM_CLK8, 7},
199 {CPM_CLK_SCC4, CPM_BRG1, 0},
200 {CPM_CLK_SCC4, CPM_BRG2, 1},
201 {CPM_CLK_SCC4, CPM_BRG3, 2},
202 {CPM_CLK_SCC4, CPM_BRG4, 3},
203 {CPM_CLK_SCC4, CPM_CLK5, 4},
204 {CPM_CLK_SCC4, CPM_CLK6, 5},
205 {CPM_CLK_SCC4, CPM_CLK7, 6},
206 {CPM_CLK_SCC4, CPM_CLK8, 7},
207 };
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400208
209 im_cpmux = cpm2_map(im_cpmux);
210
211 switch (target) {
212 case CPM_CLK_SCC1:
213 reg = &im_cpmux->cmx_scr;
214 shift = 24;
Laurent Pinchart025306f2008-04-02 16:46:31 +0200215 break;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400216 case CPM_CLK_SCC2:
217 reg = &im_cpmux->cmx_scr;
218 shift = 16;
219 break;
220 case CPM_CLK_SCC3:
221 reg = &im_cpmux->cmx_scr;
222 shift = 8;
223 break;
224 case CPM_CLK_SCC4:
225 reg = &im_cpmux->cmx_scr;
226 shift = 0;
227 break;
228 case CPM_CLK_FCC1:
229 reg = &im_cpmux->cmx_fcr;
230 shift = 24;
231 break;
232 case CPM_CLK_FCC2:
233 reg = &im_cpmux->cmx_fcr;
234 shift = 16;
235 break;
236 case CPM_CLK_FCC3:
237 reg = &im_cpmux->cmx_fcr;
238 shift = 8;
239 break;
240 default:
241 printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
242 return -EINVAL;
243 }
244
245 if (mode == CPM_CLK_RX)
Scott Wood4b218e92007-08-21 02:36:19 +1000246 shift += 3;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400247
Scott Wood2652d4e2007-07-16 13:26:35 -0500248 for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400249 if (clk_map[i][0] == target && clk_map[i][1] == clock) {
250 bits = clk_map[i][2];
251 break;
252 }
253 }
Scott Wood2652d4e2007-07-16 13:26:35 -0500254 if (i == ARRAY_SIZE(clk_map))
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400255 ret = -EINVAL;
256
257 bits <<= shift;
258 mask <<= shift;
Scott Wood2652d4e2007-07-16 13:26:35 -0500259
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400260 out_be32(reg, (in_be32(reg) & ~mask) | bits);
261
262 cpm2_unmap(im_cpmux);
263 return ret;
264}
265
Scott Wood2652d4e2007-07-16 13:26:35 -0500266int cpm2_smc_clk_setup(enum cpm_clk_target target, int clock)
267{
268 int ret = 0;
269 int shift;
270 int i, bits = 0;
271 cpmux_t __iomem *im_cpmux;
272 u8 __iomem *reg;
273 u8 mask = 3;
274
275 u8 clk_map[][3] = {
276 {CPM_CLK_SMC1, CPM_BRG1, 0},
277 {CPM_CLK_SMC1, CPM_BRG7, 1},
278 {CPM_CLK_SMC1, CPM_CLK7, 2},
279 {CPM_CLK_SMC1, CPM_CLK9, 3},
280 {CPM_CLK_SMC2, CPM_BRG2, 0},
281 {CPM_CLK_SMC2, CPM_BRG8, 1},
282 {CPM_CLK_SMC2, CPM_CLK4, 2},
283 {CPM_CLK_SMC2, CPM_CLK15, 3},
284 };
285
286 im_cpmux = cpm2_map(im_cpmux);
287
288 switch (target) {
289 case CPM_CLK_SMC1:
290 reg = &im_cpmux->cmx_smr;
291 mask = 3;
292 shift = 4;
293 break;
294 case CPM_CLK_SMC2:
295 reg = &im_cpmux->cmx_smr;
296 mask = 3;
297 shift = 0;
298 break;
299 default:
300 printk(KERN_ERR "cpm2_smc_clock_setup: invalid clock target\n");
301 return -EINVAL;
302 }
303
304 for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
305 if (clk_map[i][0] == target && clk_map[i][1] == clock) {
306 bits = clk_map[i][2];
307 break;
308 }
309 }
310 if (i == ARRAY_SIZE(clk_map))
311 ret = -EINVAL;
312
313 bits <<= shift;
314 mask <<= shift;
315
316 out_8(reg, (in_8(reg) & ~mask) | bits);
317
318 cpm2_unmap(im_cpmux);
319 return ret;
320}
321
Scott Wood7f21f522007-07-16 13:32:24 -0500322struct cpm2_ioports {
323 u32 dir, par, sor, odr, dat;
324 u32 res[3];
325};
326
327void cpm2_set_pin(int port, int pin, int flags)
328{
329 struct cpm2_ioports __iomem *iop =
330 (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
331
332 pin = 1 << (31 - pin);
333
334 if (flags & CPM_PIN_OUTPUT)
335 setbits32(&iop[port].dir, pin);
336 else
337 clrbits32(&iop[port].dir, pin);
338
339 if (!(flags & CPM_PIN_GPIO))
340 setbits32(&iop[port].par, pin);
341 else
342 clrbits32(&iop[port].par, pin);
343
344 if (flags & CPM_PIN_SECONDARY)
345 setbits32(&iop[port].sor, pin);
346 else
347 clrbits32(&iop[port].sor, pin);
348
349 if (flags & CPM_PIN_OPENDRAIN)
350 setbits32(&iop[port].odr, pin);
351 else
352 clrbits32(&iop[port].odr, pin);
353}
Laurent Pincharte1933252008-07-28 10:43:22 +0200354
355static int cpm_init_par_io(void)
356{
357 struct device_node *np;
358
359 for_each_compatible_node(np, NULL, "fsl,cpm2-pario-bank")
360 cpm2_gpiochip_add32(np);
361 return 0;
362}
363arch_initcall(cpm_init_par_io);
364