2 * Static Memory Controller for AT32 chips
4 * Copyright (C) 2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
22 #define NR_CHIP_SELECTS 6
30 static struct hsmc
*hsmc
;
32 void smc_set_timing(struct smc_config
*config
,
33 const struct smc_timing
*timing
)
40 /* Reset all SMC timings */
41 config
->ncs_read_setup
= 0;
42 config
->nrd_setup
= 0;
43 config
->ncs_write_setup
= 0;
44 config
->nwe_setup
= 0;
45 config
->ncs_read_pulse
= 0;
46 config
->nrd_pulse
= 0;
47 config
->ncs_write_pulse
= 0;
48 config
->nwe_pulse
= 0;
49 config
->read_cycle
= 0;
50 config
->write_cycle
= 0;
53 * cycles = x / T = x * f
54 * = ((x * 1000000000) * ((f * 65536) / 1000000000)) / 65536
55 * = ((x * 1000000000) * (((f / 10000) * 65536) / 100000)) / 65536
57 mul
= (clk_get_rate(hsmc
->mck
) / 10000) << 16;
60 #define ns2cyc(x) ((((x) * mul) + 65535) >> 16)
62 if (timing
->ncs_read_setup
> 0)
63 config
->ncs_read_setup
= ns2cyc(timing
->ncs_read_setup
);
65 if (timing
->nrd_setup
> 0)
66 config
->nrd_setup
= ns2cyc(timing
->nrd_setup
);
68 if (timing
->ncs_write_setup
> 0)
69 config
->ncs_write_setup
= ns2cyc(timing
->ncs_write_setup
);
71 if (timing
->nwe_setup
> 0)
72 config
->nwe_setup
= ns2cyc(timing
->nwe_setup
);
74 if (timing
->ncs_read_pulse
> 0)
75 config
->ncs_read_pulse
= ns2cyc(timing
->ncs_read_pulse
);
77 if (timing
->nrd_pulse
> 0)
78 config
->nrd_pulse
= ns2cyc(timing
->nrd_pulse
);
80 if (timing
->ncs_write_pulse
> 0)
81 config
->ncs_write_pulse
= ns2cyc(timing
->ncs_write_pulse
);
83 if (timing
->nwe_pulse
> 0)
84 config
->nwe_pulse
= ns2cyc(timing
->nwe_pulse
);
86 if (timing
->read_cycle
> 0)
87 config
->read_cycle
= ns2cyc(timing
->read_cycle
);
89 if (timing
->write_cycle
> 0)
90 config
->write_cycle
= ns2cyc(timing
->write_cycle
);
92 /* Extend read cycle in needed */
93 if (timing
->ncs_read_recover
> 0)
94 recover
= ns2cyc(timing
->ncs_read_recover
);
98 cycle
= config
->ncs_read_setup
+ config
->ncs_read_pulse
+ recover
;
100 if (config
->read_cycle
< cycle
)
101 config
->read_cycle
= cycle
;
103 /* Extend read cycle in needed */
104 if (timing
->nrd_recover
> 0)
105 recover
= ns2cyc(timing
->nrd_recover
);
109 cycle
= config
->nrd_setup
+ config
->nrd_pulse
+ recover
;
111 if (config
->read_cycle
< cycle
)
112 config
->read_cycle
= cycle
;
114 /* Extend write cycle in needed */
115 if (timing
->ncs_write_recover
> 0)
116 recover
= ns2cyc(timing
->ncs_write_recover
);
120 cycle
= config
->ncs_write_setup
+ config
->ncs_write_pulse
+ recover
;
122 if (config
->write_cycle
< cycle
)
123 config
->write_cycle
= cycle
;
125 /* Extend write cycle in needed */
126 if (timing
->nwe_recover
> 0)
127 recover
= ns2cyc(timing
->nwe_recover
);
131 cycle
= config
->nwe_setup
+ config
->nwe_pulse
+ recover
;
133 if (config
->write_cycle
< cycle
)
134 config
->write_cycle
= cycle
;
136 EXPORT_SYMBOL(smc_set_timing
);
138 int smc_set_configuration(int cs
, const struct smc_config
*config
)
140 unsigned long offset
;
141 u32 setup
, pulse
, cycle
, mode
;
145 if (cs
>= NR_CHIP_SELECTS
)
148 setup
= (HSMC_BF(NWE_SETUP
, config
->nwe_setup
)
149 | HSMC_BF(NCS_WR_SETUP
, config
->ncs_write_setup
)
150 | HSMC_BF(NRD_SETUP
, config
->nrd_setup
)
151 | HSMC_BF(NCS_RD_SETUP
, config
->ncs_read_setup
));
152 pulse
= (HSMC_BF(NWE_PULSE
, config
->nwe_pulse
)
153 | HSMC_BF(NCS_WR_PULSE
, config
->ncs_write_pulse
)
154 | HSMC_BF(NRD_PULSE
, config
->nrd_pulse
)
155 | HSMC_BF(NCS_RD_PULSE
, config
->ncs_read_pulse
));
156 cycle
= (HSMC_BF(NWE_CYCLE
, config
->write_cycle
)
157 | HSMC_BF(NRD_CYCLE
, config
->read_cycle
));
159 switch (config
->bus_width
) {
161 mode
= HSMC_BF(DBW
, HSMC_DBW_8_BITS
);
164 mode
= HSMC_BF(DBW
, HSMC_DBW_16_BITS
);
167 mode
= HSMC_BF(DBW
, HSMC_DBW_32_BITS
);
173 switch (config
->nwait_mode
) {
175 mode
|= HSMC_BF(EXNW_MODE
, HSMC_EXNW_MODE_DISABLED
);
178 mode
|= HSMC_BF(EXNW_MODE
, HSMC_EXNW_MODE_RESERVED
);
181 mode
|= HSMC_BF(EXNW_MODE
, HSMC_EXNW_MODE_FROZEN
);
184 mode
|= HSMC_BF(EXNW_MODE
, HSMC_EXNW_MODE_READY
);
190 if (config
->tdf_cycles
) {
191 mode
|= HSMC_BF(TDF_CYCLES
, config
->tdf_cycles
);
194 if (config
->nrd_controlled
)
195 mode
|= HSMC_BIT(READ_MODE
);
196 if (config
->nwe_controlled
)
197 mode
|= HSMC_BIT(WRITE_MODE
);
198 if (config
->byte_write
)
199 mode
|= HSMC_BIT(BAT
);
200 if (config
->tdf_mode
)
201 mode
|= HSMC_BIT(TDF_MODE
);
203 pr_debug("smc cs%d: setup/%08x pulse/%08x cycle/%08x mode/%08x\n",
204 cs
, setup
, pulse
, cycle
, mode
);
207 hsmc_writel(hsmc
, SETUP0
+ offset
, setup
);
208 hsmc_writel(hsmc
, PULSE0
+ offset
, pulse
);
209 hsmc_writel(hsmc
, CYCLE0
+ offset
, cycle
);
210 hsmc_writel(hsmc
, MODE0
+ offset
, mode
);
211 hsmc_readl(hsmc
, MODE0
); /* I/O barrier */
215 EXPORT_SYMBOL(smc_set_configuration
);
217 static int hsmc_probe(struct platform_device
*pdev
)
219 struct resource
*regs
;
220 struct clk
*pclk
, *mck
;
226 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
229 pclk
= clk_get(&pdev
->dev
, "pclk");
231 return PTR_ERR(pclk
);
232 mck
= clk_get(&pdev
->dev
, "mck");
239 hsmc
= kzalloc(sizeof(struct hsmc
), GFP_KERNEL
);
248 hsmc
->regs
= ioremap(regs
->start
, resource_size(regs
));
250 goto out_disable_clocks
;
252 dev_info(&pdev
->dev
, "Atmel Static Memory Controller at 0x%08lx\n",
253 (unsigned long)regs
->start
);
255 platform_set_drvdata(pdev
, hsmc
);
271 static struct platform_driver hsmc_driver
= {
278 static int __init
hsmc_init(void)
280 return platform_driver_register(&hsmc_driver
);
282 core_initcall(hsmc_init
);