3 * Utility functions for the Freescale MPC52xx.
5 * Copyright (C) 2006 Sylvain Munaut <tnt@246tNt.com>
7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied.
15 #include <linux/gpio.h>
16 #include <linux/kernel.h>
17 #include <linux/spinlock.h>
18 #include <linux/of_platform.h>
19 #include <linux/of_gpio.h>
22 #include <asm/mpc52xx.h>
24 /* MPC5200 device tree match tables */
25 static struct of_device_id mpc52xx_xlb_ids
[] __initdata
= {
26 { .compatible
= "fsl,mpc5200-xlb", },
27 { .compatible
= "mpc5200-xlb", },
30 static struct of_device_id mpc52xx_bus_ids
[] __initdata
= {
31 { .compatible
= "fsl,mpc5200-immr", },
32 { .compatible
= "fsl,mpc5200b-immr", },
33 { .compatible
= "simple-bus", },
35 /* depreciated matches; shouldn't be used in new device trees */
36 { .compatible
= "fsl,lpb", },
37 { .type
= "builtin", .compatible
= "mpc5200", }, /* efika */
38 { .type
= "soc", .compatible
= "mpc5200", }, /* lite5200 */
43 * This variable is mapped in mpc52xx_map_wdt() and used in mpc52xx_restart().
44 * Permanent mapping is required because mpc52xx_restart() can be called
45 * from interrupt context while node mapping (which calls ioremap())
46 * cannot be used at such point.
48 static DEFINE_SPINLOCK(mpc52xx_lock
);
49 static struct mpc52xx_gpt __iomem
*mpc52xx_wdt
;
50 static struct mpc52xx_cdm __iomem
*mpc52xx_cdm
;
53 * Configure the XLB arbiter settings to match what Linux expects.
56 mpc5200_setup_xlb_arbiter(void)
58 struct device_node
*np
;
59 struct mpc52xx_xlb __iomem
*xlb
;
61 np
= of_find_matching_node(NULL
, mpc52xx_xlb_ids
);
62 xlb
= of_iomap(np
, 0);
65 printk(KERN_ERR __FILE__
": "
66 "Error mapping XLB in mpc52xx_setup_cpu(). "
67 "Expect some abnormal behavior\n");
71 /* Configure the XLB Arbiter priorities */
72 out_be32(&xlb
->master_pri_enable
, 0xff);
73 out_be32(&xlb
->master_priority
, 0x11111111);
76 * Disable XLB pipelining
77 * (cfr errate 292. We could do this only just before ATA PIO
78 * transaction and re-enable it afterwards ...)
79 * Not needed on MPC5200B.
81 if ((mfspr(SPRN_SVR
) & MPC5200_SVR_MASK
) == MPC5200_SVR
)
82 out_be32(&xlb
->config
, in_be32(&xlb
->config
) | MPC52xx_XLB_CFG_PLDIS
);
88 * This variable is mapped in mpc52xx_map_common_devices and
89 * used in mpc5200_psc_ac97_gpio_reset().
91 static DEFINE_SPINLOCK(gpio_lock
);
92 struct mpc52xx_gpio __iomem
*simple_gpio
;
93 struct mpc52xx_gpio_wkup __iomem
*wkup_gpio
;
96 * mpc52xx_declare_of_platform_devices: register internal devices and children
97 * of the localplus bus to the of_platform
101 mpc52xx_declare_of_platform_devices(void)
103 /* Find every child of the SOC node and add it to of_platform */
104 if (of_platform_bus_probe(NULL
, mpc52xx_bus_ids
, NULL
))
105 printk(KERN_ERR __FILE__
": "
106 "Error while probing of_platform bus\n");
110 * match tables used by mpc52xx_map_common_devices()
112 static struct of_device_id mpc52xx_gpt_ids
[] __initdata
= {
113 { .compatible
= "fsl,mpc5200-gpt", },
114 { .compatible
= "mpc5200-gpt", }, /* old */
117 static struct of_device_id mpc52xx_cdm_ids
[] __initdata
= {
118 { .compatible
= "fsl,mpc5200-cdm", },
119 { .compatible
= "mpc5200-cdm", }, /* old */
122 static const struct of_device_id mpc52xx_gpio_simple
[] = {
123 { .compatible
= "fsl,mpc5200-gpio", },
126 static const struct of_device_id mpc52xx_gpio_wkup
[] = {
127 { .compatible
= "fsl,mpc5200-gpio-wkup", },
133 * mpc52xx_map_common_devices: iomap devices required by common code
136 mpc52xx_map_common_devices(void)
138 struct device_node
*np
;
140 /* mpc52xx_wdt is mapped here and used in mpc52xx_restart,
141 * possibly from a interrupt context. wdt is only implement
142 * on a gpt0, so check has-wdt property before mapping.
144 for_each_matching_node(np
, mpc52xx_gpt_ids
) {
145 if (of_get_property(np
, "fsl,has-wdt", NULL
) ||
146 of_get_property(np
, "has-wdt", NULL
)) {
147 mpc52xx_wdt
= of_iomap(np
, 0);
153 /* Clock Distribution Module, used by PSC clock setting function */
154 np
= of_find_matching_node(NULL
, mpc52xx_cdm_ids
);
155 mpc52xx_cdm
= of_iomap(np
, 0);
158 /* simple_gpio registers */
159 np
= of_find_matching_node(NULL
, mpc52xx_gpio_simple
);
160 simple_gpio
= of_iomap(np
, 0);
163 /* wkup_gpio registers */
164 np
= of_find_matching_node(NULL
, mpc52xx_gpio_wkup
);
165 wkup_gpio
= of_iomap(np
, 0);
170 * mpc52xx_set_psc_clkdiv: Set clock divider in the CDM for PSC ports
172 * @psc_id: id of psc port; must be 1,2,3 or 6
173 * @clkdiv: clock divider value to put into CDM PSC register.
175 int mpc52xx_set_psc_clkdiv(int psc_id
, int clkdiv
)
186 mclken_div
= 0x8000 | (clkdiv
& 0x1FF);
188 case 1: reg
= &mpc52xx_cdm
->mclken_div_psc1
; mask
= 0x20; break;
189 case 2: reg
= &mpc52xx_cdm
->mclken_div_psc2
; mask
= 0x40; break;
190 case 3: reg
= &mpc52xx_cdm
->mclken_div_psc3
; mask
= 0x80; break;
191 case 6: reg
= &mpc52xx_cdm
->mclken_div_psc6
; mask
= 0x10; break;
196 /* Set the rate and enable the clock */
197 spin_lock_irqsave(&mpc52xx_lock
, flags
);
198 out_be16(reg
, mclken_div
);
199 val
= in_be32(&mpc52xx_cdm
->clk_enables
);
200 out_be32(&mpc52xx_cdm
->clk_enables
, val
| mask
);
201 spin_unlock_irqrestore(&mpc52xx_lock
, flags
);
205 EXPORT_SYMBOL(mpc52xx_set_psc_clkdiv
);
208 * mpc52xx_get_xtal_freq - Get SYS_XTAL_IN frequency for a device
212 * Returns the frequency of the external oscillator clock connected
213 * to the SYS_XTAL_IN pin, or 0 if it cannot be determined.
215 unsigned int mpc52xx_get_xtal_freq(struct device_node
*node
)
223 freq
= mpc5xxx_get_bus_frequency(node
);
227 if (in_8(&mpc52xx_cdm
->ipb_clk_sel
) & 0x1)
230 val
= in_be32(&mpc52xx_cdm
->rstcfg
);
242 EXPORT_SYMBOL(mpc52xx_get_xtal_freq
);
245 * mpc52xx_restart: ppc_md->restart hook for mpc5200 using the watchdog timer
248 mpc52xx_restart(char *cmd
)
252 /* Turn on the watchdog and wait for it to expire.
253 * It effectively does a reset. */
255 out_be32(&mpc52xx_wdt
->mode
, 0x00000000);
256 out_be32(&mpc52xx_wdt
->count
, 0x000000ff);
257 out_be32(&mpc52xx_wdt
->mode
, 0x00009004);
259 printk(KERN_ERR __FILE__
": "
260 "mpc52xx_restart: Can't access wdt. "
261 "Restart impossible, system halted.\n");
266 #define PSC1_RESET 0x1
267 #define PSC1_SYNC 0x4
268 #define PSC1_SDATA_OUT 0x1
269 #define PSC2_RESET 0x2
270 #define PSC2_SYNC (0x4<<4)
271 #define PSC2_SDATA_OUT (0x1<<4)
272 #define MPC52xx_GPIO_PSC1_MASK 0x7
273 #define MPC52xx_GPIO_PSC2_MASK (0x7<<4)
276 * mpc5200_psc_ac97_gpio_reset: Use gpio pins to reset the ac97 bus
278 * @psc: psc number to reset (only psc 1 and 2 support ac97)
280 int mpc5200_psc_ac97_gpio_reset(int psc_number
)
289 if ((!simple_gpio
) || (!wkup_gpio
))
292 switch (psc_number
) {
294 reset
= PSC1_RESET
; /* AC97_1_RES */
295 sync
= PSC1_SYNC
; /* AC97_1_SYNC */
296 out
= PSC1_SDATA_OUT
; /* AC97_1_SDATA_OUT */
297 gpio
= MPC52xx_GPIO_PSC1_MASK
;
300 reset
= PSC2_RESET
; /* AC97_2_RES */
301 sync
= PSC2_SYNC
; /* AC97_2_SYNC */
302 out
= PSC2_SDATA_OUT
; /* AC97_2_SDATA_OUT */
303 gpio
= MPC52xx_GPIO_PSC2_MASK
;
306 pr_err(__FILE__
": Unable to determine PSC, no ac97 "
307 "cold-reset will be performed\n");
311 spin_lock_irqsave(&gpio_lock
, flags
);
313 /* Reconfiure pin-muxing to gpio */
314 mux
= in_be32(&simple_gpio
->port_config
);
315 out_be32(&simple_gpio
->port_config
, mux
& (~gpio
));
317 /* enable gpio pins for output */
318 setbits8(&wkup_gpio
->wkup_gpioe
, reset
);
319 setbits32(&simple_gpio
->simple_gpioe
, sync
| out
);
321 setbits8(&wkup_gpio
->wkup_ddr
, reset
);
322 setbits32(&simple_gpio
->simple_ddr
, sync
| out
);
324 /* Assert cold reset */
325 clrbits32(&simple_gpio
->simple_dvo
, sync
| out
);
326 clrbits8(&wkup_gpio
->wkup_dvo
, reset
);
328 /* wait at lease 1 us */
332 setbits8(&wkup_gpio
->wkup_dvo
, reset
);
334 /* Restore pin-muxing */
335 out_be32(&simple_gpio
->port_config
, mux
);
337 spin_unlock_irqrestore(&gpio_lock
, flags
);
341 EXPORT_SYMBOL(mpc5200_psc_ac97_gpio_reset
);