2 * Freescale LBC and UPM routines.
4 * Copyright © 2007-2008 MontaVista Software, Inc.
5 * Copyright © 2010 Freescale Semiconductor
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
8 * Author: Jack Lan <Jack.Lan@freescale.com>
9 * Author: Roy Zang <tie-fei.zang@freescale.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/compiler.h>
21 #include <linux/spinlock.h>
22 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <linux/platform_device.h>
28 #include <linux/interrupt.h>
29 #include <linux/mod_devicetable.h>
30 #include <linux/syscore_ops.h>
32 #include <asm/fsl_lbc.h>
34 static DEFINE_SPINLOCK(fsl_lbc_lock
);
35 struct fsl_lbc_ctrl
*fsl_lbc_ctrl_dev
;
36 EXPORT_SYMBOL(fsl_lbc_ctrl_dev
);
39 * fsl_lbc_addr - convert the base address
40 * @addr_base: base address of the memory bank
42 * This function converts a base address of lbc into the right format for the
43 * BR register. If the SOC has eLBC then it returns 32bit physical address
44 * else it convers a 34bit local bus physical address to correct format of
45 * 32bit address for BR register (Example: MPC8641).
47 u32
fsl_lbc_addr(phys_addr_t addr_base
)
49 struct device_node
*np
= fsl_lbc_ctrl_dev
->dev
->of_node
;
50 u32 addr
= addr_base
& 0xffff8000;
52 if (of_device_is_compatible(np
, "fsl,elbc"))
55 return addr
| ((addr_base
& 0x300000000ull
) >> 19);
57 EXPORT_SYMBOL(fsl_lbc_addr
);
60 * fsl_lbc_find - find Localbus bank
61 * @addr_base: base address of the memory bank
63 * This function walks LBC banks comparing "Base address" field of the BR
64 * registers with the supplied addr_base argument. When bases match this
65 * function returns bank number (starting with 0), otherwise it returns
66 * appropriate errno value.
68 int fsl_lbc_find(phys_addr_t addr_base
)
71 struct fsl_lbc_regs __iomem
*lbc
;
73 if (!fsl_lbc_ctrl_dev
|| !fsl_lbc_ctrl_dev
->regs
)
76 lbc
= fsl_lbc_ctrl_dev
->regs
;
77 for (i
= 0; i
< ARRAY_SIZE(lbc
->bank
); i
++) {
78 u32 br
= in_be32(&lbc
->bank
[i
].br
);
79 u32
or = in_be32(&lbc
->bank
[i
].or);
81 if (br
& BR_V
&& (br
& or & BR_BA
) == fsl_lbc_addr(addr_base
))
87 EXPORT_SYMBOL(fsl_lbc_find
);
90 * fsl_upm_find - find pre-programmed UPM via base address
91 * @addr_base: base address of the memory bank controlled by the UPM
92 * @upm: pointer to the allocated fsl_upm structure
94 * This function fills fsl_upm structure so you can use it with the rest of
95 * UPM API. On success this function returns 0, otherwise it returns
96 * appropriate errno value.
98 int fsl_upm_find(phys_addr_t addr_base
, struct fsl_upm
*upm
)
102 struct fsl_lbc_regs __iomem
*lbc
;
104 bank
= fsl_lbc_find(addr_base
);
108 if (!fsl_lbc_ctrl_dev
|| !fsl_lbc_ctrl_dev
->regs
)
111 lbc
= fsl_lbc_ctrl_dev
->regs
;
112 br
= in_be32(&lbc
->bank
[bank
].br
);
114 switch (br
& BR_MSEL
) {
116 upm
->mxmr
= &lbc
->mamr
;
119 upm
->mxmr
= &lbc
->mbmr
;
122 upm
->mxmr
= &lbc
->mcmr
;
128 switch (br
& BR_PS
) {
144 EXPORT_SYMBOL(fsl_upm_find
);
147 * fsl_upm_run_pattern - actually run an UPM pattern
148 * @upm: pointer to the fsl_upm structure obtained via fsl_upm_find
149 * @io_base: remapped pointer to where memory access should happen
150 * @mar: MAR register content during pattern execution
152 * This function triggers dummy write to the memory specified by the io_base,
153 * thus UPM pattern actually executed. Note that mar usage depends on the
154 * pre-programmed AMX bits in the UPM RAM.
156 int fsl_upm_run_pattern(struct fsl_upm
*upm
, void __iomem
*io_base
, u32 mar
)
161 if (!fsl_lbc_ctrl_dev
|| !fsl_lbc_ctrl_dev
->regs
)
164 spin_lock_irqsave(&fsl_lbc_lock
, flags
);
166 out_be32(&fsl_lbc_ctrl_dev
->regs
->mar
, mar
);
168 switch (upm
->width
) {
173 out_be16(io_base
, 0x0);
176 out_be32(io_base
, 0x0);
183 spin_unlock_irqrestore(&fsl_lbc_lock
, flags
);
187 EXPORT_SYMBOL(fsl_upm_run_pattern
);
189 static int fsl_lbc_ctrl_init(struct fsl_lbc_ctrl
*ctrl
,
190 struct device_node
*node
)
192 struct fsl_lbc_regs __iomem
*lbc
= ctrl
->regs
;
194 /* clear event registers */
195 setbits32(&lbc
->ltesr
, LTESR_CLEAR
);
196 out_be32(&lbc
->lteatr
, 0);
197 out_be32(&lbc
->ltear
, 0);
198 out_be32(&lbc
->lteccr
, LTECCR_CLEAR
);
199 out_be32(&lbc
->ltedr
, LTEDR_ENABLE
);
201 /* Set the monitor timeout value to the maximum for erratum A001 */
202 if (of_device_is_compatible(node
, "fsl,elbc"))
203 clrsetbits_be32(&lbc
->lbcr
, LBCR_BMT
, LBCR_BMTPS
);
209 * NOTE: This interrupt is used to report localbus events of various kinds,
210 * such as transaction errors on the chipselects.
213 static irqreturn_t
fsl_lbc_ctrl_irq(int irqno
, void *data
)
215 struct fsl_lbc_ctrl
*ctrl
= data
;
216 struct fsl_lbc_regs __iomem
*lbc
= ctrl
->regs
;
220 spin_lock_irqsave(&fsl_lbc_lock
, flags
);
221 status
= in_be32(&lbc
->ltesr
);
223 spin_unlock_irqrestore(&fsl_lbc_lock
, flags
);
227 out_be32(&lbc
->ltesr
, LTESR_CLEAR
);
228 out_be32(&lbc
->lteatr
, 0);
229 out_be32(&lbc
->ltear
, 0);
230 ctrl
->irq_status
= status
;
232 if (status
& LTESR_BM
)
233 dev_err(ctrl
->dev
, "Local bus monitor time-out: "
234 "LTESR 0x%08X\n", status
);
235 if (status
& LTESR_WP
)
236 dev_err(ctrl
->dev
, "Write protect error: "
237 "LTESR 0x%08X\n", status
);
238 if (status
& LTESR_ATMW
)
239 dev_err(ctrl
->dev
, "Atomic write error: "
240 "LTESR 0x%08X\n", status
);
241 if (status
& LTESR_ATMR
)
242 dev_err(ctrl
->dev
, "Atomic read error: "
243 "LTESR 0x%08X\n", status
);
244 if (status
& LTESR_CS
)
245 dev_err(ctrl
->dev
, "Chip select error: "
246 "LTESR 0x%08X\n", status
);
247 if (status
& LTESR_FCT
) {
248 dev_err(ctrl
->dev
, "FCM command time-out: "
249 "LTESR 0x%08X\n", status
);
251 wake_up(&ctrl
->irq_wait
);
253 if (status
& LTESR_PAR
) {
254 dev_err(ctrl
->dev
, "Parity or Uncorrectable ECC error: "
255 "LTESR 0x%08X\n", status
);
257 wake_up(&ctrl
->irq_wait
);
259 if (status
& LTESR_CC
) {
261 wake_up(&ctrl
->irq_wait
);
263 if (status
& ~LTESR_MASK
)
264 dev_err(ctrl
->dev
, "Unknown error: "
265 "LTESR 0x%08X\n", status
);
266 spin_unlock_irqrestore(&fsl_lbc_lock
, flags
);
273 * called by device layer when it finds a device matching
274 * one our driver can handled. This code allocates all of
275 * the resources needed for the controller only. The
276 * resources for the NAND banks themselves are allocated
277 * in the chip probe function.
280 static int fsl_lbc_ctrl_probe(struct platform_device
*dev
)
284 if (!dev
->dev
.of_node
) {
285 dev_err(&dev
->dev
, "Device OF-Node is NULL");
289 fsl_lbc_ctrl_dev
= kzalloc(sizeof(*fsl_lbc_ctrl_dev
), GFP_KERNEL
);
290 if (!fsl_lbc_ctrl_dev
)
293 dev_set_drvdata(&dev
->dev
, fsl_lbc_ctrl_dev
);
295 spin_lock_init(&fsl_lbc_ctrl_dev
->lock
);
296 init_waitqueue_head(&fsl_lbc_ctrl_dev
->irq_wait
);
298 fsl_lbc_ctrl_dev
->regs
= of_iomap(dev
->dev
.of_node
, 0);
299 if (!fsl_lbc_ctrl_dev
->regs
) {
300 dev_err(&dev
->dev
, "failed to get memory region\n");
305 fsl_lbc_ctrl_dev
->irq
[0] = irq_of_parse_and_map(dev
->dev
.of_node
, 0);
306 if (!fsl_lbc_ctrl_dev
->irq
[0]) {
307 dev_err(&dev
->dev
, "failed to get irq resource\n");
312 fsl_lbc_ctrl_dev
->dev
= &dev
->dev
;
314 ret
= fsl_lbc_ctrl_init(fsl_lbc_ctrl_dev
, dev
->dev
.of_node
);
318 ret
= request_irq(fsl_lbc_ctrl_dev
->irq
[0], fsl_lbc_ctrl_irq
, 0,
319 "fsl-lbc", fsl_lbc_ctrl_dev
);
321 dev_err(&dev
->dev
, "failed to install irq (%d)\n",
322 fsl_lbc_ctrl_dev
->irq
[0]);
323 ret
= fsl_lbc_ctrl_dev
->irq
[0];
327 fsl_lbc_ctrl_dev
->irq
[1] = irq_of_parse_and_map(dev
->dev
.of_node
, 1);
328 if (fsl_lbc_ctrl_dev
->irq
[1]) {
329 ret
= request_irq(fsl_lbc_ctrl_dev
->irq
[1], fsl_lbc_ctrl_irq
,
330 IRQF_SHARED
, "fsl-lbc-err", fsl_lbc_ctrl_dev
);
332 dev_err(&dev
->dev
, "failed to install irq (%d)\n",
333 fsl_lbc_ctrl_dev
->irq
[1]);
334 ret
= fsl_lbc_ctrl_dev
->irq
[1];
339 /* Enable interrupts for any detected events */
340 out_be32(&fsl_lbc_ctrl_dev
->regs
->lteir
, LTEIR_ENABLE
);
345 free_irq(fsl_lbc_ctrl_dev
->irq
[0], fsl_lbc_ctrl_dev
);
347 iounmap(fsl_lbc_ctrl_dev
->regs
);
348 kfree(fsl_lbc_ctrl_dev
);
349 fsl_lbc_ctrl_dev
= NULL
;
353 #ifdef CONFIG_SUSPEND
355 /* save lbc registers */
356 static int fsl_lbc_syscore_suspend(void)
358 struct fsl_lbc_ctrl
*ctrl
;
359 struct fsl_lbc_regs __iomem
*lbc
;
361 ctrl
= fsl_lbc_ctrl_dev
;
369 ctrl
->saved_regs
= kmalloc(sizeof(struct fsl_lbc_regs
), GFP_KERNEL
);
370 if (!ctrl
->saved_regs
)
373 _memcpy_fromio(ctrl
->saved_regs
, lbc
, sizeof(struct fsl_lbc_regs
));
379 /* restore lbc registers */
380 static void fsl_lbc_syscore_resume(void)
382 struct fsl_lbc_ctrl
*ctrl
;
383 struct fsl_lbc_regs __iomem
*lbc
;
385 ctrl
= fsl_lbc_ctrl_dev
;
393 if (ctrl
->saved_regs
) {
394 _memcpy_toio(lbc
, ctrl
->saved_regs
,
395 sizeof(struct fsl_lbc_regs
));
396 kfree(ctrl
->saved_regs
);
397 ctrl
->saved_regs
= NULL
;
403 #endif /* CONFIG_SUSPEND */
405 static const struct of_device_id fsl_lbc_match
[] = {
406 { .compatible
= "fsl,elbc", },
407 { .compatible
= "fsl,pq3-localbus", },
408 { .compatible
= "fsl,pq2-localbus", },
409 { .compatible
= "fsl,pq2pro-localbus", },
413 #ifdef CONFIG_SUSPEND
414 static struct syscore_ops lbc_syscore_pm_ops
= {
415 .suspend
= fsl_lbc_syscore_suspend
,
416 .resume
= fsl_lbc_syscore_resume
,
420 static struct platform_driver fsl_lbc_ctrl_driver
= {
423 .of_match_table
= fsl_lbc_match
,
425 .probe
= fsl_lbc_ctrl_probe
,
428 static int __init
fsl_lbc_init(void)
430 #ifdef CONFIG_SUSPEND
431 register_syscore_ops(&lbc_syscore_pm_ops
);
433 return platform_driver_register(&fsl_lbc_ctrl_driver
);
435 subsys_initcall(fsl_lbc_init
);