1/*
2 * PCMCIA driver for SL811HS (as found in REX-CFU1U)
3 * Filename: sl811_cs.c
4 * Author:   Yukio Yamamoto
5 *
6 *  Port to sl811-hcd and 2.6.x by
7 *    Botond Botyanszki <boti@rocketmail.com>
8 *    Simon Pickering
9 *
10 *  Last update: 2005-05-12
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/ptrace.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/timer.h>
20#include <linux/ioport.h>
21#include <linux/platform_device.h>
22
23#include <pcmcia/cs_types.h>
24#include <pcmcia/cs.h>
25#include <pcmcia/cistpl.h>
26#include <pcmcia/cisreg.h>
27#include <pcmcia/ds.h>
28
29#include <linux/usb/sl811.h>
30
31MODULE_AUTHOR("Botond Botyanszki");
32MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
33MODULE_LICENSE("GPL");
34
35
36/*====================================================================*/
37/* MACROS                                                             */
38/*====================================================================*/
39
40#if defined(DEBUG) || defined(PCMCIA_DEBUG)
41
42static int pc_debug = 0;
43module_param(pc_debug, int, 0644);
44
45#define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
46
47#else
48#define DBG(n, args...) do{}while(0)
49#endif	/* no debugging */
50
51#define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
52
53#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
54
55#define CS_CHECK(fn, ret) \
56	do { \
57		last_fn = (fn); \
58		if ((last_ret = (ret)) != 0) \
59			goto cs_failed; \
60	} while (0)
61
62/*====================================================================*/
63/* VARIABLES                                                          */
64/*====================================================================*/
65
66static const char driver_name[DEV_NAME_LEN]  = "sl811_cs";
67
68typedef struct local_info_t {
69	struct pcmcia_device	*p_dev;
70	dev_node_t		node;
71} local_info_t;
72
73static void sl811_cs_release(struct pcmcia_device * link);
74
75/*====================================================================*/
76
77static void release_platform_dev(struct device * dev)
78{
79	DBG(0, "sl811_cs platform_dev release\n");
80	dev->parent = NULL;
81}
82
83static struct sl811_platform_data platform_data = {
84	.potpg		= 100,
85	.power		= 50,		/* == 100mA */
86};
87
88static struct resource resources[] = {
89	[0] = {
90		.flags	= IORESOURCE_IRQ,
91	},
92	[1] = {
93		// .name   = "address",
94		.flags	= IORESOURCE_IO,
95	},
96	[2] = {
97		// .name   = "data",
98		.flags	= IORESOURCE_IO,
99	},
100};
101
102extern struct platform_driver sl811h_driver;
103
104static struct platform_device platform_dev = {
105	.id			= -1,
106	.dev = {
107		.platform_data = &platform_data,
108		.release       = release_platform_dev,
109	},
110	.resource		= resources,
111	.num_resources		= ARRAY_SIZE(resources),
112};
113
114static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
115{
116	if (platform_dev.dev.parent)
117		return -EBUSY;
118	platform_dev.dev.parent = parent;
119
120	/* finish seting up the platform device */
121	resources[0].start = irq;
122
123	resources[1].start = base_addr;
124	resources[1].end = base_addr;
125
126	resources[2].start = base_addr + 1;
127	resources[2].end   = base_addr + 1;
128
129	/* The driver core will probe for us.  We know sl811-hcd has been
130	 * initialized already because of the link order dependency created
131	 * by referencing "sl811h_driver".
132	 */
133	platform_dev.name = sl811h_driver.driver.name;
134	return platform_device_register(&platform_dev);
135}
136
137/*====================================================================*/
138
139static void sl811_cs_detach(struct pcmcia_device *link)
140{
141	DBG(0, "sl811_cs_detach(0x%p)\n", link);
142
143	sl811_cs_release(link);
144
145	/* This points to the parent local_info_t struct */
146	kfree(link->priv);
147}
148
149static void sl811_cs_release(struct pcmcia_device * link)
150{
151	DBG(0, "sl811_cs_release(0x%p)\n", link);
152
153	pcmcia_disable_device(link);
154	platform_device_unregister(&platform_dev);
155}
156
157static int sl811_cs_config(struct pcmcia_device *link)
158{
159	struct device		*parent = &handle_to_dev(link);
160	local_info_t		*dev = link->priv;
161	tuple_t			tuple;
162	cisparse_t		parse;
163	int			last_fn, last_ret;
164	u_char			buf[64];
165	config_info_t		conf;
166	cistpl_cftable_entry_t	dflt = { 0 };
167
168	DBG(0, "sl811_cs_config(0x%p)\n", link);
169
170	/* Look up the current Vcc */
171	CS_CHECK(GetConfigurationInfo,
172			pcmcia_get_configuration_info(link, &conf));
173
174	tuple.Attributes = 0;
175	tuple.TupleData = buf;
176	tuple.TupleDataMax = sizeof(buf);
177	tuple.TupleOffset = 0;
178	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
179	CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
180	while (1) {
181		cistpl_cftable_entry_t	*cfg = &(parse.cftable_entry);
182
183		if (pcmcia_get_tuple_data(link, &tuple) != 0
184				|| pcmcia_parse_tuple(link, &tuple, &parse)
185						!= 0)
186			goto next_entry;
187
188		if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
189			dflt = *cfg;
190		}
191
192		if (cfg->index == 0)
193			goto next_entry;
194
195		link->conf.ConfigIndex = cfg->index;
196
197		/* Use power settings for Vcc and Vpp if present */
198		/*  Note that the CIS values need to be rescaled */
199		if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
200			if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
201					!= conf.Vcc)
202				goto next_entry;
203		} else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
204			if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
205					!= conf.Vcc)
206				goto next_entry;
207		}
208
209		if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
210			link->conf.Vpp =
211				cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
212		else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
213			link->conf.Vpp =
214				dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
215
216		/* we need an interrupt */
217		if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
218			link->conf.Attributes |= CONF_ENABLE_IRQ;
219
220		/* IO window settings */
221		link->io.NumPorts1 = link->io.NumPorts2 = 0;
222		if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
223			cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
224
225			link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
226			link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
227			link->io.BasePort1 = io->win[0].base;
228			link->io.NumPorts1 = io->win[0].len;
229
230			if (pcmcia_request_io(link, &link->io) != 0)
231				goto next_entry;
232		}
233		break;
234
235next_entry:
236		pcmcia_disable_device(link);
237		last_ret = pcmcia_get_next_tuple(link, &tuple);
238	}
239
240	/* require an IRQ and two registers */
241	if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
242		goto cs_failed;
243	if (link->conf.Attributes & CONF_ENABLE_IRQ)
244		CS_CHECK(RequestIRQ,
245			pcmcia_request_irq(link, &link->irq));
246	else
247		goto cs_failed;
248
249	CS_CHECK(RequestConfiguration,
250		pcmcia_request_configuration(link, &link->conf));
251
252	sprintf(dev->node.dev_name, driver_name);
253	dev->node.major = dev->node.minor = 0;
254	link->dev_node = &dev->node;
255
256	printk(KERN_INFO "%s: index 0x%02x: ",
257	       dev->node.dev_name, link->conf.ConfigIndex);
258	if (link->conf.Vpp)
259		printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
260	printk(", irq %d", link->irq.AssignedIRQ);
261	printk(", io 0x%04x-0x%04x", link->io.BasePort1,
262	       link->io.BasePort1+link->io.NumPorts1-1);
263	printk("\n");
264
265	if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
266			< 0) {
267cs_failed:
268		printk("sl811_cs_config failed\n");
269		cs_error(link, last_fn, last_ret);
270		sl811_cs_release(link);
271		return  -ENODEV;
272	}
273	return 0;
274}
275
276static int sl811_cs_probe(struct pcmcia_device *link)
277{
278	local_info_t *local;
279
280	local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
281	if (!local)
282		return -ENOMEM;
283	memset(local, 0, sizeof(local_info_t));
284	local->p_dev = link;
285	link->priv = local;
286
287	/* Initialize */
288	link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
289	link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
290	link->irq.Handler = NULL;
291
292	link->conf.Attributes = 0;
293	link->conf.IntType = INT_MEMORY_AND_IO;
294
295	return sl811_cs_config(link);
296}
297
298static struct pcmcia_device_id sl811_ids[] = {
299	PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
300	PCMCIA_DEVICE_NULL,
301};
302MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
303
304static struct pcmcia_driver sl811_cs_driver = {
305	.owner		= THIS_MODULE,
306	.drv		= {
307		.name	= (char *)driver_name,
308	},
309	.probe		= sl811_cs_probe,
310	.remove		= sl811_cs_detach,
311	.id_table	= sl811_ids,
312};
313
314/*====================================================================*/
315
316static int __init init_sl811_cs(void)
317{
318	return pcmcia_register_driver(&sl811_cs_driver);
319}
320module_init(init_sl811_cs);
321
322static void __exit exit_sl811_cs(void)
323{
324	pcmcia_unregister_driver(&sl811_cs_driver);
325}
326module_exit(exit_sl811_cs);
327