fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / platforms / ibm405lp.c
1 /*
2  * arch/ppc/platforms/ibm405lp.c  405LP-specific code
3  *
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.
8  *
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.
13  *
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
17  *
18  * Copyright (C) 2002, International Business Machines Corporation
19  * All Rights Reserved.
20  *
21  * Bishop Brock
22  * IBM Research, Austin Center for Low-Power Computing
23  * bcbrock@us.ibm.com
24  * March, 2002
25  */
26
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>
35
36 #include <asm/delay.h>
37 #include <asm/hardirq.h>
38 #include <asm/ibm4xx.h>
39 #include <asm/machdep.h>
40 #include <asm/page.h>
41 #include <asm/processor.h>
42 #include <asm/system.h>
43 #include <asm/time.h>
44 #include <asm/uaccess.h>
45 #include <asm/ocp.h>
46 #include <asm/pccf_4xx.h>
47
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},
55
56 };
57
58 /* Set up the 405LP clock and power management unit for aggressive power
59    management.  
60
61    Briefly, there are 3 CPM "classes":
62
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.
66
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.
71
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.
75
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.  
81
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
86    CPM.  
87
88    No Class 3 devices are handled yet.  */
89
90 #ifdef CONFIG_PM
91 void __init
92 ibm405lp_setup_cpm(void)
93 {
94         u32 force = 0;
95         u32 enable = 0;
96         dma0_slp_t dma0_slp;
97         dcp0_cfg_t dcp0_cfg;
98         ebc0_cfg_t ebc0_cfg;
99         sdram0_cfg_t sdram0_cfg;
100         sdram0_pmit_t sdram0_pmit;
101         sla0_slpmd_t sla0_slpmd;
102
103         /* Initialize the CPM state */
104
105         mtdcr(DCRN_CPMFR, force);
106         mtdcr(DCRN_CPMER, enable);
107
108         /* IIC - Class 3 - Not handled yet.  The driver should at least be able
109            to force/unforce itself. */
110
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. */
116
117         enable |= CPM_CPU;
118
119         /* DMA - class 2. Set for the minimum timeout, which is 32 cycles. */
120
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);
125         enable |= CPM_DMA;
126
127         /* BRG - Class 2.  Seems to crash the system when enabled in 405LP Pass
128            1 
129
130            DCP (CodePack) - Class 2.  The semantics of the sleep delay are not
131            documented. We'll use 32 (what the heck). */
132
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);
137         enable |= CPM_DCP;
138
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. ]
141          */
142
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);
147         enable |= CPM_EBC;
148
149         /* SDRAM - Class 2.  Set for the minimum 32-cycle timeout.
150
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.  */
155
156         sdram0_cfg.reg = mfdcri(DCRN_SDRAM0, CFG);
157         sdram0_cfg.fields.pme = 1;
158         mtdcri(DCRN_SDRAM0, CFG, sdram0_cfg.reg);
159
160         sdram0_pmit.reg = mfdcri(DCRN_SDRAM0, PMIT);
161         sdram0_pmit.fields.cnt = 0;
162         mtdcri(DCRN_SDRAM0, PMIT, sdram0_pmit.reg);
163
164         enable |= CPM_SDRAM;
165
166         /* PLB - Class 2.  Seems to crash the system when enabled in 405LP Pass
167            1.
168
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
172            kills the TPC.
173
174            UART0 - Class 1
175            UART1 - Class 1
176
177            Someone should work on the serial port drivers to enable PM support
178            for them. Any takers?
179
180            UIC        - Class 1
181            CPU_TMRCLK - Class 1
182
183            These system resources are never power managed.  */
184
185         /* SLA - Class 2.  Set for the minimum 32-cycle timeout. */
186
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);
191         enable |= CPM_SLA;
192
193         /* CSI  - Class 1.  
194            TPC  - Class 1.
195            TDES - Class 1.
196
197            The drivers for these units are power-aware, and manage the device
198            properly. By default these units are forced off at boot. */
199
200         force |= CPM_CSI;
201         force |= CPM_TPC;
202         force |= CPM_TDES;
203
204         /* Set the CPM state */
205
206         mtdcr(DCRN_CPMFR, force);
207         mtdcr(DCRN_CPMER, enable);
208 }
209 #endif /* CONFIG_PM */
210
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
215    loaded.
216
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.
222
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.
226
227    Clock frequencies are in KHz. If pixclk_min or pixclk_max are zero, we set
228    the lowest possible frequency to conserve energy. */
229
230 int (*set_pixclk_hook) (unsigned pixclk_min, unsigned pixclk_max) = NULL;
231 unsigned last_pixclk_min = 0;
232 unsigned last_pixclk_max = 0;
233
234 EXPORT_SYMBOL(set_pixclk_hook);
235 EXPORT_SYMBOL(last_pixclk_min);
236 EXPORT_SYMBOL(last_pixclk_max);
237
238 int
239 ibm405lp_set_pixclk(unsigned pixclk_min, unsigned pixclk_max)
240 {
241         unsigned divider;
242         bd_t *bip = (bd_t *) __res;
243         unsigned plb_khz = bip->bi_busfreq / 1000;
244         cpc0_cgcr1_t cgcr1;
245
246         if (set_pixclk_hook) {
247                 return (set_pixclk_hook) (pixclk_min, pixclk_max);
248         } else {
249                 if ((pixclk_min == 0) || (pixclk_max == 0))
250                         divider = CPC0_DIV_MAX;
251                 else {
252                         divider = plb_khz / pixclk_min;
253                         if (divider == 0)
254                                 divider = 1;
255                         if ((divider < CPC0_DIV_MAX) &&
256                             ((plb_khz / divider) > pixclk_max))
257                                 divider++;
258                 }
259
260                 cgcr1.reg = mfdcr(DCRN_CPC0_CGCR1);
261                 cgcr1.fields.ppxl = CPC0_DIV_ENCODE(divider);
262                 mtdcr(DCRN_CPC0_CGCR1, cgcr1.reg);
263
264                 last_pixclk_min = pixclk_min;
265                 last_pixclk_max = pixclk_max;
266                 return 0;
267         }
268 }
269
270 /* Somewhat misleading name, as well as the EBC, this sets up the UIC
271    and CPC ready for PCMCIA operation */
272 static void
273 pccf_ebc_setup(void)
274 {
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. */
279
280         unsigned long bits, mask, flags;
281
282         save_flags(flags);
283         cli();
284
285         /* Program EBC0_CFG for ready timeout */
286
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);
291
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 */
295
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);
300
301         /* Program the UIC for active-high, level-triggered interrupts.  Note
302            that the active-low PCMCIA interrupt pin is inverted by the PCCF
303            macro.  */
304
305         mask = (0x80000000 >> PCCF_4XX_MACRO_IRQ) | 
306                 (0x80000000 >> PCCF_4XX_CARD_IRQ);
307
308         bits = mfdcr(DCRN_UIC0_PR);
309         bits |= mask;
310         mtdcr(DCRN_UIC0_PR, bits);
311
312         bits = mfdcr(DCRN_UIC0_TR);
313         bits &= ~mask;
314         mtdcr(DCRN_UIC0_TR, bits);
315
316         /* Clear CPC0_CR0[PCMD] to enable the PCMCIA controller */
317
318         mtdcr(DCRN_CPC0_CR0, mfdcr(DCRN_CPC0_CR0) & ~0x00000200);
319
320         restore_flags(flags);
321 }
322
323 /* Map the PCCF controller's memory windows.
324  *
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
335  * problem. */
336 int
337 ibm405lp_setup_pccf(volatile u16 **vaddr, unsigned long *io_base,
338                     unsigned long *mem_base)
339 {
340         pccf_ebc_setup();
341
342         *vaddr = ioremap(PCCF_4XX_MACRO_PADDR, PCCF_4XX_MACRO_WINSIZE);
343
344         if (*vaddr == NULL) {
345                 printk(KERN_ERR "pccf_4xx: ioremap macro at 0x%lx failed.\n",
346                        PCCF_4XX_MACRO_PADDR);
347                 return -EBUSY;
348         }
349
350         printk("ibm405lp_setup_pcmcia:  phys addr = %lx,  virt addr = %p\n",
351                PCCF_4XX_MACRO_PADDR, *vaddr);
352
353         *io_base = (unsigned long) ioremap(PCCF_4XX_IO_PADDR,
354                                            PCCF_4XX_IO_WINSIZE);
355         if (*io_base == 0) {
356                 printk(KERN_ERR "pccf_4xx: ioremap io at 0x%lx failed.\n",
357                        PCCF_4XX_IO_PADDR);
358                 return -EBUSY;
359         }
360
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",
365                        PCCF_4XX_MEM_PADDR);
366                 return -EBUSY;
367         }
368
369         return 0;
370 }
371
372
373 /****************************************************************************
374  * TODC
375  ****************************************************************************/
376
377 /*
378  * The 405LP includes an MC146818-equivalent core accessed via a DCR
379  * wrapper.  The 405LP does not implement the NVRAM.
380  */
381
382 long __init ibm405lp_time_init(void)
383 {
384         static int not_initialized = 1;
385
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);
395
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");
399                         mtdcr(DCRN_RTC0_CR0,
400                                          ((RTC_DVBITS & 0x7) << 4) | (mfdcr(DCRN_RTC0_CR0) & 0xf));
401                 }
402
403                 mtdcr(DCRN_RTC0_CR1, mfdcr(DCRN_RTC0_CR1) & 0x7f);      /* allow updates */
404
405                 not_initialized = 0;
406         }
407
408         return 0;
409 }
410
411 unsigned long ibm405lp_get_rtc_time(void)
412 {
413         uint    year, mon, day, hour, min, sec;
414         uint    i;
415         u_char  save_control, uip;
416
417         spin_lock(&rtc_lock);
418         save_control = mfdcr(DCRN_RTC0_CR1);
419
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;
428
429                 uip |= mfdcr(DCRN_RTC0_CR0);
430                 if ((uip & RTC_UIP) == 0) break;
431         }
432
433         spin_unlock(&rtc_lock);
434
435         if (((save_control & RTC_DM_BINARY) == 0) ||
436             RTC_ALWAYS_BCD) {
437
438                 BCD_TO_BIN(sec);
439                 BCD_TO_BIN(min);
440                 BCD_TO_BIN(hour);
441                 BCD_TO_BIN(day);
442                 BCD_TO_BIN(mon);
443                 BCD_TO_BIN(year);
444         }
445
446         year = year + 1900;
447         if (year < 1970) {
448                 year += 100;
449         }
450
451         return mktime(year, mon, day, hour, min, sec);
452 }
453
454 int ibm405lp_set_rtc_time(unsigned long nowtime)
455 {
456         struct rtc_time tm;
457         u_char          save_control, save_freq_select;
458
459         spin_lock(&rtc_lock);
460         to_tm(nowtime, &tm);
461
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);
465
466         tm.tm_year = (tm.tm_year - 1900) % 100;
467
468         if (((save_control & RTC_DM_BINARY) == 0) ||
469             RTC_ALWAYS_BCD) {
470
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);
477         }
478
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);
488
489         spin_unlock(&rtc_lock);
490         return 0;
491 }
492