2 * arch/ppc/platforms/ibm405lp.c 405LP-specific code
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Copyright (C) 2002, International Business Machines Corporation
19 * All Rights Reserved.
22 * IBM Research, Austin Center for Low-Power Computing
27 #include <linux/config.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/proc_fs.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
36 #include <asm/delay.h>
37 #include <asm/hardirq.h>
38 #include <asm/ibm4xx.h>
39 #include <asm/machdep.h>
41 #include <asm/processor.h>
42 #include <asm/system.h>
44 #include <asm/uaccess.h>
46 #include <asm/pccf_4xx.h>
48 struct ocp_def core_ocp[] = {
49 {UART, UART0_IO_BASE, UART0_INT},
50 {UART, UART1_IO_BASE, UART1_INT},
51 {IIC, IIC0_BASE, IIC0_IRQ},
52 {GPIO, GPIO0_BASE, OCP_IRQ_NA},
53 {OPB, OPB0_BASE, OCP_IRQ_NA},
54 {OCP_NULL_TYPE, 0x0, OCP_IRQ_NA},
58 /* Set up the 405LP clock and power management unit for aggressive power
61 Briefly, there are 3 CPM "classes":
63 Class 1 - Either completely asleep or awake. The "force" state is
64 equivalent to the "enabled" state. Many Class 1 units are
65 critical system components and are never power managed.
67 Class 2 - Can be enabled for power management, where sleep requests are
68 made by the peripheral, typically after an inactivity timeout.
69 When sleeping, critical interfaces remain active, and
70 awaken the unit whenever it is targeted with a transaction.
72 Class 3 - Can be enabled for power management, where sleep requests are
73 made by the CPM. Power management for these units typically
74 will require intelligence in a device driver.
76 In the current implementation, the "force" bits are only used on Class 1
77 devices, and only when the associated driver has the intelligence necessary
78 to "unforce" the power management state. A previous scheme, which tried to
79 enable power management based on whether a particular driver was compiled
80 with the kernel, caused many problems and is never used here.
82 Class 2 devices with timeouts are normally initialized for the most
83 aggressive values. There is no power management benefit of "forcing" Class
84 2 devices over letting their inactivity timeouts take effect. Therefore,
85 after being set up here, Class 2 device drivers don't need to worry about
88 No Class 3 devices are handled yet. */
92 ibm405lp_setup_cpm(void)
99 sdram0_cfg_t sdram0_cfg;
100 sdram0_pmit_t sdram0_pmit;
101 sla0_slpmd_t sla0_slpmd;
103 /* Initialize the CPM state */
105 mtdcr(DCRN_CPMFR, force);
106 mtdcr(DCRN_CPMER, enable);
108 /* IIC - Class 3 - Not handled yet. The driver should at least be able
109 to force/unforce itself. */
111 /* CPU - class 2 - There doesn't appear to be a timeout associated with
112 this, and the exact function is not documented anywhere. It saves a
113 lot of power, though. I assume this gates core clocks when the CPU
114 core is asleep, and probably adds a couple of cycles of latency when
115 the CPU core wakes up. */
119 /* DMA - class 2. Set for the minimum timeout, which is 32 cycles. */
121 dma0_slp.reg = mfdcr(DCRN_SLP);
122 dma0_slp.fields.sme = 1;
123 dma0_slp.fields.idu = 0;
124 mtdcr(DCRN_SLP, dma0_slp.reg);
127 /* BRG - Class 2. Seems to crash the system when enabled in 405LP Pass
130 DCP (CodePack) - Class 2. The semantics of the sleep delay are not
131 documented. We'll use 32 (what the heck). */
133 dcp0_cfg.reg = mfdcri(DCRN_DCP0, CFG);
134 dcp0_cfg.fields.slen = 1;
135 dcp0_cfg.fields.sldy = 32;
136 mtdcri(DCRN_DCP0, CFG, dcp0_cfg.reg);
139 /* EBC - Class 2. Set for minimum timeout, which is 32 cycles. [ I
140 think this is 32. It may be 64. I don't trust the documentation. ]
143 ebc0_cfg.reg = mfdcri(DCRN_EBC0, CFG);
144 ebc0_cfg.fields.pme = 1;
145 ebc0_cfg.fields.pmt = 1;
146 mtdcri(DCRN_EBC0, CFG, ebc0_cfg.reg);
149 /* SDRAM - Class 2. Set for the minimum 32-cycle timeout.
151 The documentation on this core is clear - waking a sleeping SDRAM
152 controller takes 2 PLB cycles, which is added to the latency of the
153 memory operation. If someone can prove that this is affecting
154 performance we can easily back this off. */
156 sdram0_cfg.reg = mfdcri(DCRN_SDRAM0, CFG);
157 sdram0_cfg.fields.pme = 1;
158 mtdcri(DCRN_SDRAM0, CFG, sdram0_cfg.reg);
160 sdram0_pmit.reg = mfdcri(DCRN_SDRAM0, PMIT);
161 sdram0_pmit.fields.cnt = 0;
162 mtdcri(DCRN_SDRAM0, PMIT, sdram0_pmit.reg);
166 /* PLB - Class 2. Seems to crash the system when enabled in 405LP Pass
169 GPIO - Class 1. This unit is used for many things, and no single
170 driver controls all GPIO. It's best left unmanaged (it doesn't use
171 much power anyway). NB: 405LP Pass 1 erratum - forcing PM on GPIO
177 Someone should work on the serial port drivers to enable PM support
178 for them. Any takers?
183 These system resources are never power managed. */
185 /* SLA - Class 2. Set for the minimum 32-cycle timeout. */
187 sla0_slpmd.reg = mfdcri(DCRN_SLA0, SLPMD);
188 sla0_slpmd.fields.slen = 1;
189 sla0_slpmd.fields.slcr = 0;
190 mtdcri(DCRN_SLA0, SLPMD, sla0_slpmd.reg);
197 The drivers for these units are power-aware, and manage the device
198 properly. By default these units are forced off at boot. */
204 /* Set the CPM state */
206 mtdcr(DCRN_CPMFR, force);
207 mtdcr(DCRN_CPMER, enable);
209 #endif /* CONFIG_PM */
211 /* This routine is included here because the framebuffer driver needs a way to
212 tell the system the Pixel clock frequency it needs, regardless of whether
213 run-time frequency scaling is configured. A hook and a couple of global
214 variables are always present and will be used by the RTVFS driver if it is
217 Pixel clock setting is kind of a hack, as the frequency steps available from
218 the PLB/PixClk divider may be too large to guarantee that we'll hit within
219 the given limits. We never set the frequency above the upper bound, but due
220 to quantization may need to set the frequency below the lower bound. So far
221 it works OK for the panels we've tried.
223 In general, the choice of a system clock frequency should be made with
224 consideration of the LCD panel to be attached, to guarantee a good clock
225 divider for the Pixel clock regardless of frequency scaling.
227 Clock frequencies are in KHz. If pixclk_min or pixclk_max are zero, we set
228 the lowest possible frequency to conserve energy. */
230 int (*set_pixclk_hook) (unsigned pixclk_min, unsigned pixclk_max) = NULL;
231 unsigned last_pixclk_min = 0;
232 unsigned last_pixclk_max = 0;
234 EXPORT_SYMBOL(set_pixclk_hook);
235 EXPORT_SYMBOL(last_pixclk_min);
236 EXPORT_SYMBOL(last_pixclk_max);
239 ibm405lp_set_pixclk(unsigned pixclk_min, unsigned pixclk_max)
242 bd_t *bip = (bd_t *) __res;
243 unsigned plb_khz = bip->bi_busfreq / 1000;
246 if (set_pixclk_hook) {
247 return (set_pixclk_hook) (pixclk_min, pixclk_max);
249 if ((pixclk_min == 0) || (pixclk_max == 0))
250 divider = CPC0_DIV_MAX;
252 divider = plb_khz / pixclk_min;
255 if ((divider < CPC0_DIV_MAX) &&
256 ((plb_khz / divider) > pixclk_max))
260 cgcr1.reg = mfdcr(DCRN_CPC0_CGCR1);
261 cgcr1.fields.ppxl = CPC0_DIV_ENCODE(divider);
262 mtdcr(DCRN_CPC0_CGCR1, cgcr1.reg);
264 last_pixclk_min = pixclk_min;
265 last_pixclk_max = pixclk_max;
270 /* Somewhat misleading name, as well as the EBC, this sets up the UIC
271 and CPC ready for PCMCIA operation */
275 /* Set up EBC bank 4 as per PCCF docs., assuming 66 MHz EBC bus. The
276 ready timeout is set for 1024 cycles (~ 15 us at 66 MHz), unless
277 someone else has already set it for 2048. In the event of a
278 timeout we'll get a Data Machine Check. */
280 unsigned long bits, mask, flags;
285 /* Program EBC0_CFG for ready timeout */
287 mtdcr(DCRN_EBC0_CFGADDR, DCRN_EBC0_CFG);
288 bits = mfdcr(DCRN_EBC0_CFGDATA);
289 if ((bits & EBC_CFG_RTC) != EBC_CFG_RTC_2048)
290 mtdcr(DCRN_EBC0_CFGDATA, (bits & ~EBC_CFG_RTC) | EBC_CFG_RTC_1024);
292 /* Program EBC bank properties : 32 MB, 16-bit RW bank;
293 BME = 0, TWT = 22, CSN = 2, OEN = 3, WBN = WBF = 0, TH = 5,
294 RE = 1, SOR = 0, BEM = 1 */
296 mtdcr(DCRN_EBC0_CFGADDR, DCRN_EBC0_B4CR);
297 mtdcr(DCRN_EBC0_CFGDATA, (PCCF_4XX_PADDR & 0xfff00000) | 0x000ba000);
298 mtdcr(DCRN_EBC0_CFGADDR, DCRN_EBC0_B4AP);
299 mtdcr(DCRN_EBC0_CFGDATA, 0x0b0b0b40);
301 /* Program the UIC for active-high, level-triggered interrupts. Note
302 that the active-low PCMCIA interrupt pin is inverted by the PCCF
305 mask = (0x80000000 >> PCCF_4XX_MACRO_IRQ) |
306 (0x80000000 >> PCCF_4XX_CARD_IRQ);
308 bits = mfdcr(DCRN_UIC0_PR);
310 mtdcr(DCRN_UIC0_PR, bits);
312 bits = mfdcr(DCRN_UIC0_TR);
314 mtdcr(DCRN_UIC0_TR, bits);
316 /* Clear CPC0_CR0[PCMD] to enable the PCMCIA controller */
318 mtdcr(DCRN_CPC0_CR0, mfdcr(DCRN_CPC0_CR0) & ~0x00000200);
320 restore_flags(flags);
323 /* Map the PCCF controller's memory windows.
325 * HACK ALERT: Logically this belongs in the pccf_4xx driver itself,
326 * however that causes problems because it happens so late in
327 * initialization. We want to use some ISA-ish drivers (notably
328 * 8390.c) on memory mapped devices by using the
329 * ioaddr=(memaddr-_IO_BASE) hack. If _IO_BASE is the PCMCIA ISA IO
330 * space (which we want so PC Card drivers using ISA IO work) but is
331 * not initialized until the pccf_4xx driver starts, this could well
332 * be after drivers like 8390 have initialized and computed a fake
333 * "IO" address which is now incorrect. Putting the ioremap()ing of
334 * the PCCF macro in the chip/board setup code works around this
337 ibm405lp_setup_pccf(volatile u16 **vaddr, unsigned long *io_base,
338 unsigned long *mem_base)
342 *vaddr = ioremap(PCCF_4XX_MACRO_PADDR, PCCF_4XX_MACRO_WINSIZE);
344 if (*vaddr == NULL) {
345 printk(KERN_ERR "pccf_4xx: ioremap macro at 0x%lx failed.\n",
346 PCCF_4XX_MACRO_PADDR);
350 printk("ibm405lp_setup_pcmcia: phys addr = %lx, virt addr = %p\n",
351 PCCF_4XX_MACRO_PADDR, *vaddr);
353 *io_base = (unsigned long) ioremap(PCCF_4XX_IO_PADDR,
354 PCCF_4XX_IO_WINSIZE);
356 printk(KERN_ERR "pccf_4xx: ioremap io at 0x%lx failed.\n",
361 *mem_base = (unsigned long) ioremap(PCCF_4XX_MEM_PADDR,
362 PCCF_4XX_MEM_WINSIZE);
363 if (*mem_base == 0) {
364 printk(KERN_ERR "pccf_4xx: ioremap mem at 0x%lx failed.\n",
373 /****************************************************************************
375 ****************************************************************************/
378 * The 405LP includes an MC146818-equivalent core accessed via a DCR
379 * wrapper. The 405LP does not implement the NVRAM.
382 long __init ibm405lp_time_init(void)
384 static int not_initialized = 1;
386 /* Make sure clocks are running */
387 if (not_initialized) {
388 /* Reset the core and ensure it's enabled. */
389 mtdcr(DCRN_RTC0_WRAP, 0); /* toggle NRST & NMR */
390 mtdcr(DCRN_RTC0_WRAP, 3);
391 mtdcr(DCRN_RTC0_CR0, 0x60); /* No divider chain, No square wave */
392 mtdcr(DCRN_RTC0_CR1, 0x80); /* Disable update cycles/interrupts*/
393 mtdcr(DCRN_RTC0_WRAP, 0); /* toggle NRST & NMR */
394 mtdcr(DCRN_RTC0_WRAP, 3);
396 /* if necessary, set the input clock frequency */
397 if ((mfdcr(DCRN_RTC0_CR0) >> 4) != RTC_DVBITS) {
398 printk(KERN_WARNING "Warning: RTC frequency was incorrect\n");
400 ((RTC_DVBITS & 0x7) << 4) | (mfdcr(DCRN_RTC0_CR0) & 0xf));
403 mtdcr(DCRN_RTC0_CR1, mfdcr(DCRN_RTC0_CR1) & 0x7f); /* allow updates */
411 unsigned long ibm405lp_get_rtc_time(void)
413 uint year, mon, day, hour, min, sec;
415 u_char save_control, uip;
417 spin_lock(&rtc_lock);
418 save_control = mfdcr(DCRN_RTC0_CR1);
420 for (i=0; i<100000000; i++) {
421 uip = mfdcr(DCRN_RTC0_CR0);
422 sec = mfdcr(DCRN_RTC0_SEC) & 0x7f;
423 min = mfdcr(DCRN_RTC0_MIN) & 0x7f;
424 hour = mfdcr(DCRN_RTC0_HR) & 0x3f;
425 day = mfdcr(DCRN_RTC0_DOM) & 0x3f;
426 mon = mfdcr(DCRN_RTC0_MONTH) & 0x1f;
427 year = mfdcr(DCRN_RTC0_YEAR) & 0xff;
429 uip |= mfdcr(DCRN_RTC0_CR0);
430 if ((uip & RTC_UIP) == 0) break;
433 spin_unlock(&rtc_lock);
435 if (((save_control & RTC_DM_BINARY) == 0) ||
451 return mktime(year, mon, day, hour, min, sec);
454 int ibm405lp_set_rtc_time(unsigned long nowtime)
457 u_char save_control, save_freq_select;
459 spin_lock(&rtc_lock);
462 save_control = mfdcr(DCRN_RTC0_CR1);
463 save_freq_select = mfdcr(DCRN_RTC0_CR0);
464 mtdcr(DCRN_RTC0_CR0, save_freq_select | RTC_DIV_RESET2);
466 tm.tm_year = (tm.tm_year - 1900) % 100;
468 if (((save_control & RTC_DM_BINARY) == 0) ||
471 BIN_TO_BCD(tm.tm_sec);
472 BIN_TO_BCD(tm.tm_min);
473 BIN_TO_BCD(tm.tm_hour);
474 BIN_TO_BCD(tm.tm_mon);
475 BIN_TO_BCD(tm.tm_mday);
476 BIN_TO_BCD(tm.tm_year);
479 mtdcr(DCRN_RTC0_SEC, tm.tm_sec);
480 mtdcr(DCRN_RTC0_MIN, tm.tm_min);
481 mtdcr(DCRN_RTC0_HR, tm.tm_hour);
482 mtdcr(DCRN_RTC0_MONTH, tm.tm_mon);
483 mtdcr(DCRN_RTC0_DOM, tm.tm_mday);
484 mtdcr(DCRN_RTC0_YEAR, tm.tm_year);
485 mtdcr(DCRN_RTC0_WRAP, 0); /* Reset divider chain */
486 mtdcr(DCRN_RTC0_WRAP, 3);
487 mtdcr(DCRN_RTC0_CR0, save_freq_select);
489 spin_unlock(&rtc_lock);