xref: /openbmc/linux/drivers/pcmcia/sa11xx_base.c (revision f26e8817b235d8764363bffcc9cbfc61867371f2)
11da177e4SLinus Torvalds /*======================================================================
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds     Device driver for the PCMCIA control functionality of StrongARM
41da177e4SLinus Torvalds     SA-1100 microprocessors.
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds     The contents of this file are subject to the Mozilla Public
71da177e4SLinus Torvalds     License Version 1.1 (the "License"); you may not use this file
81da177e4SLinus Torvalds     except in compliance with the License. You may obtain a copy of
91da177e4SLinus Torvalds     the License at http://www.mozilla.org/MPL/
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds     Software distributed under the License is distributed on an "AS
121da177e4SLinus Torvalds     IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
131da177e4SLinus Torvalds     implied. See the License for the specific language governing
141da177e4SLinus Torvalds     rights and limitations under the License.
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds     The initial developer of the original code is John G. Dorsey
171da177e4SLinus Torvalds     <john+@cs.cmu.edu>.  Portions created by John G. Dorsey are
181da177e4SLinus Torvalds     Copyright (C) 1999 John G. Dorsey.  All Rights Reserved.
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds     Alternatively, the contents of this file may be used under the
211da177e4SLinus Torvalds     terms of the GNU Public License version 2 (the "GPL"), in which
221da177e4SLinus Torvalds     case the provisions of the GPL are applicable instead of the
231da177e4SLinus Torvalds     above.  If you wish to allow the use of your version of this file
241da177e4SLinus Torvalds     only under the terms of the GPL and not to allow others to use
251da177e4SLinus Torvalds     your version of this file under the MPL, indicate your decision
261da177e4SLinus Torvalds     by deleting the provisions above and replace them with the notice
271da177e4SLinus Torvalds     and other provisions required by the GPL.  If you do not delete
281da177e4SLinus Torvalds     the provisions above, a recipient may use your version of this
291da177e4SLinus Torvalds     file under either the MPL or the GPL.
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds ======================================================================*/
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/cpufreq.h>
361da177e4SLinus Torvalds #include <linux/ioport.h>
371da177e4SLinus Torvalds #include <linux/kernel.h>
381da177e4SLinus Torvalds #include <linux/spinlock.h>
3999730225SRussell King #include <linux/io.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
411da177e4SLinus Torvalds 
42a09e64fbSRussell King #include <mach/hardware.h>
431da177e4SLinus Torvalds #include <asm/irq.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include "soc_common.h"
461da177e4SLinus Torvalds #include "sa11xx_base.h"
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * sa1100_pcmcia_default_mecr_timing
511da177e4SLinus Torvalds  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * Calculate MECR clock wait states for given CPU clock
541da177e4SLinus Torvalds  * speed and command wait state. This function can be over-
551da177e4SLinus Torvalds  * written by a board specific version.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * The default is to simply calculate the BS values as specified in
581da177e4SLinus Torvalds  * the INTEL SA1100 development manual
591da177e4SLinus Torvalds  * "Expansion Memory (PCMCIA) Configuration Register (MECR)"
601da177e4SLinus Torvalds  * that's section 10.2.5 in _my_ version of the manual ;)
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds static unsigned int
sa1100_pcmcia_default_mecr_timing(struct soc_pcmcia_socket * skt,unsigned int cpu_speed,unsigned int cmd_time)631da177e4SLinus Torvalds sa1100_pcmcia_default_mecr_timing(struct soc_pcmcia_socket *skt,
641da177e4SLinus Torvalds 				  unsigned int cpu_speed,
651da177e4SLinus Torvalds 				  unsigned int cmd_time)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	return sa1100_pcmcia_mecr_bs(cmd_time, cpu_speed);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* sa1100_pcmcia_set_mecr()
711da177e4SLinus Torvalds  * ^^^^^^^^^^^^^^^^^^^^^^^^
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  * set MECR value for socket <sock> based on this sockets
741da177e4SLinus Torvalds  * io, mem and attribute space access speed.
751da177e4SLinus Torvalds  * Call board specific BS value calculation to allow boards
761da177e4SLinus Torvalds  * to tweak the BS values.
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds static int
sa1100_pcmcia_set_mecr(struct soc_pcmcia_socket * skt,unsigned int cpu_clock)791da177e4SLinus Torvalds sa1100_pcmcia_set_mecr(struct soc_pcmcia_socket *skt, unsigned int cpu_clock)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	struct soc_pcmcia_timing timing;
821da177e4SLinus Torvalds 	u32 mecr, old_mecr;
831da177e4SLinus Torvalds 	unsigned long flags;
841da177e4SLinus Torvalds 	unsigned int bs_io, bs_mem, bs_attr;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	soc_common_pcmcia_get_timing(skt, &timing);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	bs_io = skt->ops->get_timing(skt, cpu_clock, timing.io);
891da177e4SLinus Torvalds 	bs_mem = skt->ops->get_timing(skt, cpu_clock, timing.mem);
901da177e4SLinus Torvalds 	bs_attr = skt->ops->get_timing(skt, cpu_clock, timing.attr);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	local_irq_save(flags);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	old_mecr = mecr = MECR;
951da177e4SLinus Torvalds 	MECR_FAST_SET(mecr, skt->nr, 0);
961da177e4SLinus Torvalds 	MECR_BSIO_SET(mecr, skt->nr, bs_io);
971da177e4SLinus Torvalds 	MECR_BSA_SET(mecr, skt->nr, bs_attr);
981da177e4SLinus Torvalds 	MECR_BSM_SET(mecr, skt->nr, bs_mem);
991da177e4SLinus Torvalds 	if (old_mecr != mecr)
1001da177e4SLinus Torvalds 		MECR = mecr;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	local_irq_restore(flags);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	debug(skt, 2, "FAST %X  BSM %X  BSA %X  BSIO %X\n",
1051da177e4SLinus Torvalds 	      MECR_FAST_GET(mecr, skt->nr),
1061da177e4SLinus Torvalds 	      MECR_BSM_GET(mecr, skt->nr), MECR_BSA_GET(mecr, skt->nr),
1071da177e4SLinus Torvalds 	      MECR_BSIO_GET(mecr, skt->nr));
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	return 0;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ
1131da177e4SLinus Torvalds static int
sa1100_pcmcia_frequency_change(struct soc_pcmcia_socket * skt,unsigned long val,struct cpufreq_freqs * freqs)1141da177e4SLinus Torvalds sa1100_pcmcia_frequency_change(struct soc_pcmcia_socket *skt,
1151da177e4SLinus Torvalds 			       unsigned long val,
1161da177e4SLinus Torvalds 			       struct cpufreq_freqs *freqs)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	switch (val) {
1191da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
1201da177e4SLinus Torvalds 		if (freqs->new > freqs->old)
1211da177e4SLinus Torvalds 			sa1100_pcmcia_set_mecr(skt, freqs->new);
1221da177e4SLinus Torvalds 		break;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
1251da177e4SLinus Torvalds 		if (freqs->new < freqs->old)
1261da177e4SLinus Torvalds 			sa1100_pcmcia_set_mecr(skt, freqs->new);
1271da177e4SLinus Torvalds 		break;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	return 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #endif
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds static int
sa1100_pcmcia_set_timing(struct soc_pcmcia_socket * skt)1361da177e4SLinus Torvalds sa1100_pcmcia_set_timing(struct soc_pcmcia_socket *skt)
1371da177e4SLinus Torvalds {
13886d88bfcSDmitry Eremin-Solenikov 	unsigned long clk = clk_get_rate(skt->clk);
13986d88bfcSDmitry Eremin-Solenikov 
14086d88bfcSDmitry Eremin-Solenikov 	return sa1100_pcmcia_set_mecr(skt, clk / 1000);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static int
sa1100_pcmcia_show_timing(struct soc_pcmcia_socket * skt,char * buf)1441da177e4SLinus Torvalds sa1100_pcmcia_show_timing(struct soc_pcmcia_socket *skt, char *buf)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct soc_pcmcia_timing timing;
1476dec04e8SRussell King 	unsigned int clock = clk_get_rate(skt->clk) / 1000;
1481da177e4SLinus Torvalds 	unsigned long mecr = MECR;
1491da177e4SLinus Torvalds 	char *p = buf;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	soc_common_pcmcia_get_timing(skt, &timing);
1521da177e4SLinus Torvalds 
153*cbd5a168SRussell King 	p+=sprintf(p, "I/O      : %uns (%uns)\n", timing.io,
1541da177e4SLinus Torvalds 		   sa1100_pcmcia_cmd_time(clock, MECR_BSIO_GET(mecr, skt->nr)));
1551da177e4SLinus Torvalds 
156*cbd5a168SRussell King 	p+=sprintf(p, "attribute: %uns (%uns)\n", timing.attr,
1571da177e4SLinus Torvalds 		   sa1100_pcmcia_cmd_time(clock, MECR_BSA_GET(mecr, skt->nr)));
1581da177e4SLinus Torvalds 
159*cbd5a168SRussell King 	p+=sprintf(p, "common   : %uns (%uns)\n", timing.mem,
1601da177e4SLinus Torvalds 		   sa1100_pcmcia_cmd_time(clock, MECR_BSM_GET(mecr, skt->nr)));
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	return p - buf;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
165b393c696SEric Miao static const char *skt_names[] = {
166b393c696SEric Miao 	"PCMCIA socket 0",
167b393c696SEric Miao 	"PCMCIA socket 1",
168b393c696SEric Miao };
169b393c696SEric Miao 
170b393c696SEric Miao #define SKT_DEV_INFO_SIZE(n) \
171b393c696SEric Miao 	(sizeof(struct skt_dev_info) + (n)*sizeof(struct soc_pcmcia_socket))
172b393c696SEric Miao 
sa11xx_drv_pcmcia_add_one(struct soc_pcmcia_socket * skt)173701a5dc0SRussell King - ARM Linux int sa11xx_drv_pcmcia_add_one(struct soc_pcmcia_socket *skt)
1741da177e4SLinus Torvalds {
175b393c696SEric Miao 	skt->res_skt.start = _PCMCIA(skt->nr);
176b393c696SEric Miao 	skt->res_skt.end = _PCMCIA(skt->nr) + PCMCIASp - 1;
177b393c696SEric Miao 	skt->res_skt.name = skt_names[skt->nr];
178b393c696SEric Miao 	skt->res_skt.flags = IORESOURCE_MEM;
179b393c696SEric Miao 
180b393c696SEric Miao 	skt->res_io.start = _PCMCIAIO(skt->nr);
181b393c696SEric Miao 	skt->res_io.end = _PCMCIAIO(skt->nr) + PCMCIAIOSp - 1;
182b393c696SEric Miao 	skt->res_io.name = "io";
183b393c696SEric Miao 	skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
184b393c696SEric Miao 
185b393c696SEric Miao 	skt->res_mem.start = _PCMCIAMem(skt->nr);
186b393c696SEric Miao 	skt->res_mem.end = _PCMCIAMem(skt->nr) + PCMCIAMemSp - 1;
187b393c696SEric Miao 	skt->res_mem.name = "memory";
188b393c696SEric Miao 	skt->res_mem.flags = IORESOURCE_MEM;
189b393c696SEric Miao 
190b393c696SEric Miao 	skt->res_attr.start = _PCMCIAAttr(skt->nr);
191b393c696SEric Miao 	skt->res_attr.end = _PCMCIAAttr(skt->nr) + PCMCIAAttrSp - 1;
192b393c696SEric Miao 	skt->res_attr.name = "attribute";
193b393c696SEric Miao 	skt->res_attr.flags = IORESOURCE_MEM;
194da4f0073SRussell King - ARM Linux 
195da4f0073SRussell King - ARM Linux 	return soc_pcmcia_add_one(skt);
196b393c696SEric Miao }
197701a5dc0SRussell King - ARM Linux EXPORT_SYMBOL(sa11xx_drv_pcmcia_add_one);
198b393c696SEric Miao 
sa11xx_drv_pcmcia_ops(struct pcmcia_low_level * ops)199701a5dc0SRussell King - ARM Linux void sa11xx_drv_pcmcia_ops(struct pcmcia_low_level *ops)
200da4f0073SRussell King - ARM Linux {
2011da177e4SLinus Torvalds 	/*
2021da177e4SLinus Torvalds 	 * set default MECR calculation if the board specific
2031da177e4SLinus Torvalds 	 * code did not specify one...
2041da177e4SLinus Torvalds 	 */
2051da177e4SLinus Torvalds 	if (!ops->get_timing)
2061da177e4SLinus Torvalds 		ops->get_timing = sa1100_pcmcia_default_mecr_timing;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/* Provide our SA11x0 specific timing routines. */
2091da177e4SLinus Torvalds 	ops->set_timing  = sa1100_pcmcia_set_timing;
2101da177e4SLinus Torvalds 	ops->show_timing = sa1100_pcmcia_show_timing;
2111da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ
2121da177e4SLinus Torvalds 	ops->frequency_change = sa1100_pcmcia_frequency_change;
2131da177e4SLinus Torvalds #endif
214701a5dc0SRussell King - ARM Linux }
215701a5dc0SRussell King - ARM Linux EXPORT_SYMBOL(sa11xx_drv_pcmcia_ops);
216701a5dc0SRussell King - ARM Linux 
sa11xx_drv_pcmcia_probe(struct device * dev,struct pcmcia_low_level * ops,int first,int nr)217701a5dc0SRussell King - ARM Linux int sa11xx_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops,
218701a5dc0SRussell King - ARM Linux 			    int first, int nr)
219701a5dc0SRussell King - ARM Linux {
220701a5dc0SRussell King - ARM Linux 	struct skt_dev_info *sinfo;
221701a5dc0SRussell King - ARM Linux 	struct soc_pcmcia_socket *skt;
222701a5dc0SRussell King - ARM Linux 	int i, ret = 0;
22386d88bfcSDmitry Eremin-Solenikov 	struct clk *clk;
22486d88bfcSDmitry Eremin-Solenikov 
22572010acaSRussell King 	clk = devm_clk_get(dev, NULL);
22686d88bfcSDmitry Eremin-Solenikov 	if (IS_ERR(clk))
22786d88bfcSDmitry Eremin-Solenikov 		return PTR_ERR(clk);
228701a5dc0SRussell King - ARM Linux 
229701a5dc0SRussell King - ARM Linux 	sa11xx_drv_pcmcia_ops(ops);
2301da177e4SLinus Torvalds 
231e33e640aSRussell King 	sinfo = devm_kzalloc(dev, SKT_DEV_INFO_SIZE(nr), GFP_KERNEL);
232da4f0073SRussell King - ARM Linux 	if (!sinfo)
233da4f0073SRussell King - ARM Linux 		return -ENOMEM;
234da4f0073SRussell King - ARM Linux 
235da4f0073SRussell King - ARM Linux 	sinfo->nskt = nr;
236da4f0073SRussell King - ARM Linux 
237421f91d2SUwe Kleine-König 	/* Initialize processor specific parameters */
238da4f0073SRussell King - ARM Linux 	for (i = 0; i < nr; i++) {
239da4f0073SRussell King - ARM Linux 		skt = &sinfo->skt[i];
240da4f0073SRussell King - ARM Linux 
241da4f0073SRussell King - ARM Linux 		skt->nr = first + i;
24286d88bfcSDmitry Eremin-Solenikov 		skt->clk = clk;
243e0d21178SRussell King 		soc_pcmcia_init_one(skt, ops, dev);
244da4f0073SRussell King - ARM Linux 
245da4f0073SRussell King - ARM Linux 		ret = sa11xx_drv_pcmcia_add_one(skt);
246da4f0073SRussell King - ARM Linux 		if (ret)
247da4f0073SRussell King - ARM Linux 			break;
248da4f0073SRussell King - ARM Linux 	}
249da4f0073SRussell King - ARM Linux 
250da4f0073SRussell King - ARM Linux 	if (ret) {
251da4f0073SRussell King - ARM Linux 		while (--i >= 0)
252da4f0073SRussell King - ARM Linux 			soc_pcmcia_remove_one(&sinfo->skt[i]);
253da4f0073SRussell King - ARM Linux 	} else {
254da4f0073SRussell King - ARM Linux 		dev_set_drvdata(dev, sinfo);
255da4f0073SRussell King - ARM Linux 	}
256da4f0073SRussell King - ARM Linux 
257da4f0073SRussell King - ARM Linux 	return ret;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds EXPORT_SYMBOL(sa11xx_drv_pcmcia_probe);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
2621da177e4SLinus Torvalds MODULE_DESCRIPTION("Linux PCMCIA Card Services: SA-11xx core socket driver");
2631da177e4SLinus Torvalds MODULE_LICENSE("Dual MPL/GPL");
264