blob: 4ab3f1f5a7c3eea98a09547b942bf2e7bb85def3 [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 Wood15f8c602007-09-28 14:06:16 -050049#ifndef CONFIG_PPC_CPM_NEW_BINDING
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040050static void cpm2_dpinit(void);
Scott Wood15f8c602007-09-28 14:06:16 -050051#endif
52
Scott Wood449012d2007-09-14 15:30:44 -050053cpm_cpm2_t __iomem *cpmp; /* Pointer to comm processor space */
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040054
55/* We allocate this here because it is used almost exclusively for
56 * the communication processor devices.
57 */
Scott Wood449012d2007-09-14 15:30:44 -050058cpm2_map_t __iomem *cpm2_immr;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040059
60#define CPM_MAP_SIZE (0x40000) /* 256k - the PQ3 reserve this amount
61 of space for CPM as it is larger
62 than on PQ2 */
63
Scott Woodcd2150b2007-12-12 16:54:32 -060064void __init cpm2_reset(void)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040065{
Scott Wood449012d2007-09-14 15:30:44 -050066#ifdef CONFIG_PPC_85xx
67 cpm2_immr = ioremap(CPM_MAP_ADDR, CPM_MAP_SIZE);
68#else
69 cpm2_immr = ioremap(get_immrbase(), CPM_MAP_SIZE);
70#endif
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040071
72 /* Reclaim the DP memory for our use.
73 */
Scott Wood15f8c602007-09-28 14:06:16 -050074#ifdef CONFIG_PPC_CPM_NEW_BINDING
75 cpm_muram_init();
76#else
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040077 cpm2_dpinit();
Scott Wood15f8c602007-09-28 14:06:16 -050078#endif
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040079
80 /* Tell everyone where the comm processor resides.
81 */
82 cpmp = &cpm2_immr->im_cpm;
Laurent Pinchart872a15d2008-04-10 17:02:38 +020083
84#ifndef CONFIG_PPC_EARLY_DEBUG_CPM
85 /* Reset the CPM.
86 */
87 cpm_command(CPM_CR_RST, 0);
88#endif
Vitaly Bordugb0c110b2006-09-21 22:18:53 +040089}
90
Jochen Friedrich362f9b62007-11-26 18:03:40 +010091static DEFINE_SPINLOCK(cmd_lock);
92
93#define MAX_CR_CMD_LOOPS 10000
94
95int cpm_command(u32 command, u8 opcode)
96{
97 int i, ret;
98 unsigned long flags;
99
100 spin_lock_irqsave(&cmd_lock, flags);
101
102 ret = 0;
103 out_be32(&cpmp->cp_cpcr, command | opcode | CPM_CR_FLG);
104 for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
105 if ((in_be32(&cpmp->cp_cpcr) & CPM_CR_FLG) == 0)
106 goto out;
107
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100108 printk(KERN_ERR "%s(): Not able to issue CPM command\n", __func__);
Jochen Friedrich362f9b62007-11-26 18:03:40 +0100109 ret = -EIO;
110out:
111 spin_unlock_irqrestore(&cmd_lock, flags);
112 return ret;
113}
114EXPORT_SYMBOL(cpm_command);
115
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400116/* Set a baud rate generator. This needs lots of work. There are
117 * eight BRGs, which can be connected to the CPM channels or output
118 * as clocks. The BRGs are in two different block of internal
119 * memory mapped space.
120 * The baud rate clock is the system clock divided by something.
121 * It was set up long ago during the initial boot phase and is
122 * is given to us.
123 * Baud rate clocks are zero-based in the driver code (as that maps
124 * to port numbers). Documentation uses 1-based numbering.
125 */
126#define BRG_INT_CLK (get_brgfreq())
127#define BRG_UART_CLK (BRG_INT_CLK/16)
128
129/* This function is used by UARTS, or anything else that uses a 16x
130 * oversampled clock.
131 */
132void
133cpm_setbrg(uint brg, uint rate)
134{
Scott Wood449012d2007-09-14 15:30:44 -0500135 u32 __iomem *bp;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400136
137 /* This is good enough to get SMCs running.....
138 */
139 if (brg < 4) {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400140 bp = cpm2_map_size(im_brgc1, 16);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400141 } else {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400142 bp = cpm2_map_size(im_brgc5, 16);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400143 brg -= 4;
144 }
145 bp += brg;
Scott Wood83fcdb42007-09-05 14:29:10 -0500146 out_be32(bp, (((BRG_UART_CLK / rate) - 1) << 1) | CPM_BRG_EN);
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400147
148 cpm2_unmap(bp);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400149}
150
151/* This function is used to set high speed synchronous baud rate
152 * clocks.
153 */
154void
155cpm2_fastbrg(uint brg, uint rate, int div16)
156{
Scott Wood449012d2007-09-14 15:30:44 -0500157 u32 __iomem *bp;
158 u32 val;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400159
160 if (brg < 4) {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400161 bp = cpm2_map_size(im_brgc1, 16);
Jochen Friedrichb5677d82008-01-25 15:31:42 +0100162 } else {
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400163 bp = cpm2_map_size(im_brgc5, 16);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400164 brg -= 4;
165 }
166 bp += brg;
Scott Wood449012d2007-09-14 15:30:44 -0500167 val = ((BRG_INT_CLK / rate) << 1) | CPM_BRG_EN;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400168 if (div16)
Scott Wood449012d2007-09-14 15:30:44 -0500169 val |= CPM_BRG_DIV16;
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400170
Scott Wood449012d2007-09-14 15:30:44 -0500171 out_be32(bp, val);
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400172 cpm2_unmap(bp);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400173}
174
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400175int cpm2_clk_setup(enum cpm_clk_target target, int clock, int mode)
176{
177 int ret = 0;
178 int shift;
179 int i, bits = 0;
Scott Wood449012d2007-09-14 15:30:44 -0500180 cpmux_t __iomem *im_cpmux;
181 u32 __iomem *reg;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400182 u32 mask = 7;
Scott Wood2652d4e2007-07-16 13:26:35 -0500183
184 u8 clk_map[][3] = {
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400185 {CPM_CLK_FCC1, CPM_BRG5, 0},
186 {CPM_CLK_FCC1, CPM_BRG6, 1},
187 {CPM_CLK_FCC1, CPM_BRG7, 2},
188 {CPM_CLK_FCC1, CPM_BRG8, 3},
189 {CPM_CLK_FCC1, CPM_CLK9, 4},
190 {CPM_CLK_FCC1, CPM_CLK10, 5},
191 {CPM_CLK_FCC1, CPM_CLK11, 6},
192 {CPM_CLK_FCC1, CPM_CLK12, 7},
193 {CPM_CLK_FCC2, CPM_BRG5, 0},
194 {CPM_CLK_FCC2, CPM_BRG6, 1},
195 {CPM_CLK_FCC2, CPM_BRG7, 2},
196 {CPM_CLK_FCC2, CPM_BRG8, 3},
197 {CPM_CLK_FCC2, CPM_CLK13, 4},
198 {CPM_CLK_FCC2, CPM_CLK14, 5},
199 {CPM_CLK_FCC2, CPM_CLK15, 6},
200 {CPM_CLK_FCC2, CPM_CLK16, 7},
201 {CPM_CLK_FCC3, CPM_BRG5, 0},
202 {CPM_CLK_FCC3, CPM_BRG6, 1},
203 {CPM_CLK_FCC3, CPM_BRG7, 2},
204 {CPM_CLK_FCC3, CPM_BRG8, 3},
205 {CPM_CLK_FCC3, CPM_CLK13, 4},
206 {CPM_CLK_FCC3, CPM_CLK14, 5},
207 {CPM_CLK_FCC3, CPM_CLK15, 6},
Scott Wood2652d4e2007-07-16 13:26:35 -0500208 {CPM_CLK_FCC3, CPM_CLK16, 7},
209 {CPM_CLK_SCC1, CPM_BRG1, 0},
210 {CPM_CLK_SCC1, CPM_BRG2, 1},
211 {CPM_CLK_SCC1, CPM_BRG3, 2},
212 {CPM_CLK_SCC1, CPM_BRG4, 3},
213 {CPM_CLK_SCC1, CPM_CLK11, 4},
214 {CPM_CLK_SCC1, CPM_CLK12, 5},
215 {CPM_CLK_SCC1, CPM_CLK3, 6},
216 {CPM_CLK_SCC1, CPM_CLK4, 7},
217 {CPM_CLK_SCC2, CPM_BRG1, 0},
218 {CPM_CLK_SCC2, CPM_BRG2, 1},
219 {CPM_CLK_SCC2, CPM_BRG3, 2},
220 {CPM_CLK_SCC2, CPM_BRG4, 3},
221 {CPM_CLK_SCC2, CPM_CLK11, 4},
222 {CPM_CLK_SCC2, CPM_CLK12, 5},
223 {CPM_CLK_SCC2, CPM_CLK3, 6},
224 {CPM_CLK_SCC2, CPM_CLK4, 7},
225 {CPM_CLK_SCC3, CPM_BRG1, 0},
226 {CPM_CLK_SCC3, CPM_BRG2, 1},
227 {CPM_CLK_SCC3, CPM_BRG3, 2},
228 {CPM_CLK_SCC3, CPM_BRG4, 3},
229 {CPM_CLK_SCC3, CPM_CLK5, 4},
230 {CPM_CLK_SCC3, CPM_CLK6, 5},
231 {CPM_CLK_SCC3, CPM_CLK7, 6},
232 {CPM_CLK_SCC3, CPM_CLK8, 7},
233 {CPM_CLK_SCC4, CPM_BRG1, 0},
234 {CPM_CLK_SCC4, CPM_BRG2, 1},
235 {CPM_CLK_SCC4, CPM_BRG3, 2},
236 {CPM_CLK_SCC4, CPM_BRG4, 3},
237 {CPM_CLK_SCC4, CPM_CLK5, 4},
238 {CPM_CLK_SCC4, CPM_CLK6, 5},
239 {CPM_CLK_SCC4, CPM_CLK7, 6},
240 {CPM_CLK_SCC4, CPM_CLK8, 7},
241 };
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400242
243 im_cpmux = cpm2_map(im_cpmux);
244
245 switch (target) {
246 case CPM_CLK_SCC1:
247 reg = &im_cpmux->cmx_scr;
248 shift = 24;
Laurent Pinchart025306f2008-04-02 16:46:31 +0200249 break;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400250 case CPM_CLK_SCC2:
251 reg = &im_cpmux->cmx_scr;
252 shift = 16;
253 break;
254 case CPM_CLK_SCC3:
255 reg = &im_cpmux->cmx_scr;
256 shift = 8;
257 break;
258 case CPM_CLK_SCC4:
259 reg = &im_cpmux->cmx_scr;
260 shift = 0;
261 break;
262 case CPM_CLK_FCC1:
263 reg = &im_cpmux->cmx_fcr;
264 shift = 24;
265 break;
266 case CPM_CLK_FCC2:
267 reg = &im_cpmux->cmx_fcr;
268 shift = 16;
269 break;
270 case CPM_CLK_FCC3:
271 reg = &im_cpmux->cmx_fcr;
272 shift = 8;
273 break;
274 default:
275 printk(KERN_ERR "cpm2_clock_setup: invalid clock target\n");
276 return -EINVAL;
277 }
278
279 if (mode == CPM_CLK_RX)
Scott Wood4b218e92007-08-21 02:36:19 +1000280 shift += 3;
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400281
Scott Wood2652d4e2007-07-16 13:26:35 -0500282 for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400283 if (clk_map[i][0] == target && clk_map[i][1] == clock) {
284 bits = clk_map[i][2];
285 break;
286 }
287 }
Scott Wood2652d4e2007-07-16 13:26:35 -0500288 if (i == ARRAY_SIZE(clk_map))
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400289 ret = -EINVAL;
290
291 bits <<= shift;
292 mask <<= shift;
Scott Wood2652d4e2007-07-16 13:26:35 -0500293
Vitaly Bordugd3465c92006-09-21 22:38:05 +0400294 out_be32(reg, (in_be32(reg) & ~mask) | bits);
295
296 cpm2_unmap(im_cpmux);
297 return ret;
298}
299
Scott Wood2652d4e2007-07-16 13:26:35 -0500300int cpm2_smc_clk_setup(enum cpm_clk_target target, int clock)
301{
302 int ret = 0;
303 int shift;
304 int i, bits = 0;
305 cpmux_t __iomem *im_cpmux;
306 u8 __iomem *reg;
307 u8 mask = 3;
308
309 u8 clk_map[][3] = {
310 {CPM_CLK_SMC1, CPM_BRG1, 0},
311 {CPM_CLK_SMC1, CPM_BRG7, 1},
312 {CPM_CLK_SMC1, CPM_CLK7, 2},
313 {CPM_CLK_SMC1, CPM_CLK9, 3},
314 {CPM_CLK_SMC2, CPM_BRG2, 0},
315 {CPM_CLK_SMC2, CPM_BRG8, 1},
316 {CPM_CLK_SMC2, CPM_CLK4, 2},
317 {CPM_CLK_SMC2, CPM_CLK15, 3},
318 };
319
320 im_cpmux = cpm2_map(im_cpmux);
321
322 switch (target) {
323 case CPM_CLK_SMC1:
324 reg = &im_cpmux->cmx_smr;
325 mask = 3;
326 shift = 4;
327 break;
328 case CPM_CLK_SMC2:
329 reg = &im_cpmux->cmx_smr;
330 mask = 3;
331 shift = 0;
332 break;
333 default:
334 printk(KERN_ERR "cpm2_smc_clock_setup: invalid clock target\n");
335 return -EINVAL;
336 }
337
338 for (i = 0; i < ARRAY_SIZE(clk_map); i++) {
339 if (clk_map[i][0] == target && clk_map[i][1] == clock) {
340 bits = clk_map[i][2];
341 break;
342 }
343 }
344 if (i == ARRAY_SIZE(clk_map))
345 ret = -EINVAL;
346
347 bits <<= shift;
348 mask <<= shift;
349
350 out_8(reg, (in_8(reg) & ~mask) | bits);
351
352 cpm2_unmap(im_cpmux);
353 return ret;
354}
355
Scott Wood15f8c602007-09-28 14:06:16 -0500356#ifndef CONFIG_PPC_CPM_NEW_BINDING
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400357/*
358 * dpalloc / dpfree bits.
359 */
360static spinlock_t cpm_dpmem_lock;
361/* 16 blocks should be enough to satisfy all requests
362 * until the memory subsystem goes up... */
363static rh_block_t cpm_boot_dpmem_rh_block[16];
364static rh_info_t cpm_dpmem_info;
Scott Wood449012d2007-09-14 15:30:44 -0500365static u8 __iomem *im_dprambase;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400366
367static void cpm2_dpinit(void)
368{
Scott Wood449012d2007-09-14 15:30:44 -0500369 spin_lock_init(&cpm_dpmem_lock);
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400370
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400371 /* initialize the info header */
372 rh_init(&cpm_dpmem_info, 1,
373 sizeof(cpm_boot_dpmem_rh_block) /
374 sizeof(cpm_boot_dpmem_rh_block[0]),
375 cpm_boot_dpmem_rh_block);
376
Scott Wood15f8c602007-09-28 14:06:16 -0500377 im_dprambase = cpm2_immr;
378
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400379 /* Attach the usable dpmem area */
380 /* XXX: This is actually crap. CPM_DATAONLY_BASE and
381 * CPM_DATAONLY_SIZE is only a subset of the available dpram. It
382 * varies with the processor and the microcode patches activated.
383 * But the following should be at least safe.
384 */
Scott Wood15f8c602007-09-28 14:06:16 -0500385 rh_attach_region(&cpm_dpmem_info, CPM_DATAONLY_BASE, CPM_DATAONLY_SIZE);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400386}
387
388/* This function returns an index into the DPRAM area.
389 */
Timur Tabi4c356302007-05-08 14:46:36 -0500390unsigned long cpm_dpalloc(uint size, uint align)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400391{
Timur Tabi4c356302007-05-08 14:46:36 -0500392 unsigned long start;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400393 unsigned long flags;
394
395 spin_lock_irqsave(&cpm_dpmem_lock, flags);
396 cpm_dpmem_info.alignment = align;
397 start = rh_alloc(&cpm_dpmem_info, size, "commproc");
398 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
399
400 return (uint)start;
401}
402EXPORT_SYMBOL(cpm_dpalloc);
403
Timur Tabi4c356302007-05-08 14:46:36 -0500404int cpm_dpfree(unsigned long offset)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400405{
406 int ret;
407 unsigned long flags;
408
409 spin_lock_irqsave(&cpm_dpmem_lock, flags);
Timur Tabi4c356302007-05-08 14:46:36 -0500410 ret = rh_free(&cpm_dpmem_info, offset);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400411 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
412
413 return ret;
414}
415EXPORT_SYMBOL(cpm_dpfree);
416
417/* not sure if this is ever needed */
Timur Tabi4c356302007-05-08 14:46:36 -0500418unsigned long cpm_dpalloc_fixed(unsigned long offset, uint size, uint align)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400419{
Timur Tabi4c356302007-05-08 14:46:36 -0500420 unsigned long start;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400421 unsigned long flags;
422
423 spin_lock_irqsave(&cpm_dpmem_lock, flags);
424 cpm_dpmem_info.alignment = align;
Timur Tabi4c356302007-05-08 14:46:36 -0500425 start = rh_alloc_fixed(&cpm_dpmem_info, offset, size, "commproc");
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400426 spin_unlock_irqrestore(&cpm_dpmem_lock, flags);
427
Timur Tabi4c356302007-05-08 14:46:36 -0500428 return start;
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400429}
430EXPORT_SYMBOL(cpm_dpalloc_fixed);
431
432void cpm_dpdump(void)
433{
434 rh_dump(&cpm_dpmem_info);
435}
436EXPORT_SYMBOL(cpm_dpdump);
437
Timur Tabi4c356302007-05-08 14:46:36 -0500438void *cpm_dpram_addr(unsigned long offset)
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400439{
Vitaly Bordugfc8e50e2006-09-21 22:37:58 +0400440 return (void *)(im_dprambase + offset);
Vitaly Bordugb0c110b2006-09-21 22:18:53 +0400441}
442EXPORT_SYMBOL(cpm_dpram_addr);
Scott Wood15f8c602007-09-28 14:06:16 -0500443#endif /* !CONFIG_PPC_CPM_NEW_BINDING */
Scott Wood7f21f522007-07-16 13:32:24 -0500444
445struct cpm2_ioports {
446 u32 dir, par, sor, odr, dat;
447 u32 res[3];
448};
449
450void cpm2_set_pin(int port, int pin, int flags)
451{
452 struct cpm2_ioports __iomem *iop =
453 (struct cpm2_ioports __iomem *)&cpm2_immr->im_ioport;
454
455 pin = 1 << (31 - pin);
456
457 if (flags & CPM_PIN_OUTPUT)
458 setbits32(&iop[port].dir, pin);
459 else
460 clrbits32(&iop[port].dir, pin);
461
462 if (!(flags & CPM_PIN_GPIO))
463 setbits32(&iop[port].par, pin);
464 else
465 clrbits32(&iop[port].par, pin);
466
467 if (flags & CPM_PIN_SECONDARY)
468 setbits32(&iop[port].sor, pin);
469 else
470 clrbits32(&iop[port].sor, pin);
471
472 if (flags & CPM_PIN_OPENDRAIN)
473 setbits32(&iop[port].odr, pin);
474 else
475 clrbits32(&iop[port].odr, pin);
476}