PageRenderTime 50ms CodeModel.GetById 2ms app.highlight 43ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/pcmcia/vrc4173_cardu.c

https://bitbucket.org/evzijst/gittest
C | 617 lines | 459 code | 126 blank | 32 comment | 102 complexity | 0cc6ea5a007660da33832b2803b0adc6 MD5 | raw file
  1/*
  2 * FILE NAME
  3 *	drivers/pcmcia/vrc4173_cardu.c
  4 *
  5 * BRIEF MODULE DESCRIPTION
  6 * 	NEC VRC4173 CARDU driver for Socket Services
  7 *	(This device doesn't support CardBus. it is supporting only 16bit PC Card.)
  8 *
  9 * Copyright 2002,2003 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
 10 *
 11 *  This program is free software; you can redistribute it and/or modify it
 12 *  under the terms of the GNU General Public License as published by the
 13 *  Free Software Foundation; either version 2 of the License, or (at your
 14 *  option) any later version.
 15 *
 16 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 17 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 18 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 19 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 20 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 21 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 22 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 23 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 24 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 25 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26 *
 27 *  You should have received a copy of the GNU General Public License along
 28 *  with this program; if not, write to the Free Software Foundation, Inc.,
 29 *  675 Mass Ave, Cambridge, MA 02139, USA.
 30 */
 31#include <linux/init.h>
 32#include <linux/module.h>
 33#include <linux/pci.h>
 34#include <linux/spinlock.h>
 35#include <linux/types.h>
 36
 37#include <asm/io.h>
 38
 39#include <pcmcia/ss.h>
 40
 41#include "vrc4173_cardu.h"
 42
 43MODULE_DESCRIPTION("NEC VRC4173 CARDU driver for Socket Services");
 44MODULE_AUTHOR("Yoichi Yuasa <yuasa@hh.iij4u.or.jp>");
 45MODULE_LICENSE("GPL");
 46
 47static int vrc4173_cardu_slots;
 48
 49static vrc4173_socket_t cardu_sockets[CARDU_MAX_SOCKETS];
 50
 51extern struct socket_info_t *pcmcia_register_socket (int slot,
 52                                                     struct pccard_operations *vtable,
 53                                                     int use_bus_pm);
 54extern void pcmcia_unregister_socket(struct socket_info_t *s);
 55
 56static inline uint8_t exca_readb(vrc4173_socket_t *socket, uint16_t offset)
 57{
 58	return readb(socket->base + EXCA_REGS_BASE + offset);
 59}
 60
 61static inline uint16_t exca_readw(vrc4173_socket_t *socket, uint16_t offset)
 62{
 63	uint16_t val;
 64
 65	val = readb(socket->base + EXCA_REGS_BASE + offset);
 66	val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
 67
 68	return val;
 69}
 70
 71static inline void exca_writeb(vrc4173_socket_t *socket, uint16_t offset, uint8_t val)
 72{
 73	writeb(val, socket->base + EXCA_REGS_BASE + offset);
 74}
 75
 76static inline void exca_writew(vrc4173_socket_t *socket, uint8_t offset, uint16_t val)
 77{
 78	writeb((u8)val, socket->base + EXCA_REGS_BASE + offset);
 79	writeb((u8)(val >> 8), socket->base + EXCA_REGS_BASE + offset + 1);
 80}
 81
 82static inline uint32_t cardbus_socket_readl(vrc4173_socket_t *socket, u16 offset)
 83{
 84	return readl(socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
 85}
 86
 87static inline void cardbus_socket_writel(vrc4173_socket_t *socket, u16 offset, uint32_t val)
 88{
 89	writel(val, socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
 90}
 91
 92static void cardu_pciregs_init(struct pci_dev *dev)
 93{
 94	u32 syscnt;
 95	u16 brgcnt;
 96	u8 devcnt;
 97
 98	pci_write_config_dword(dev, 0x1c, 0x10000000);
 99	pci_write_config_dword(dev, 0x20, 0x17fff000);
100	pci_write_config_dword(dev, 0x2c, 0);
101	pci_write_config_dword(dev, 0x30, 0xfffc);
102
103	pci_read_config_word(dev, BRGCNT, &brgcnt);
104	brgcnt &= ~IREQ_INT;
105	pci_write_config_word(dev, BRGCNT, brgcnt);
106
107	pci_read_config_dword(dev, SYSCNT, &syscnt);
108	syscnt &= ~(BAD_VCC_REQ_DISB|PCPCI_EN|CH_ASSIGN_MASK|SUB_ID_WR_EN|PCI_CLK_RIN);
109	syscnt |= (CH_ASSIGN_NODMA|ASYN_INT_MODE);
110	pci_write_config_dword(dev, SYSCNT, syscnt);
111
112	pci_read_config_byte(dev, DEVCNT, &devcnt);
113	devcnt &= ~(ZOOM_VIDEO_EN|SR_PCI_INT_SEL_MASK|PCI_INT_MODE|IRQ_MODE);
114	devcnt |= (SR_PCI_INT_SEL_NONE|IFG);
115	pci_write_config_byte(dev, DEVCNT, devcnt);
116
117	pci_write_config_byte(dev, CHIPCNT, S_PREF_DISB);
118
119	pci_write_config_byte(dev, SERRDIS, 0);
120}
121
122static int cardu_init(unsigned int slot)
123{
124	vrc4173_socket_t *socket = &cardu_sockets[slot];
125
126	cardu_pciregs_init(socket->dev);
127
128	/* CARD_SC bits are cleared by reading CARD_SC. */
129	exca_writeb(socket, GLO_CNT, 0);
130
131	socket->cap.features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS;
132	socket->cap.irq_mask = 0;
133	socket->cap.map_size = 0x1000;
134	socket->cap.pci_irq  = socket->dev->irq;
135	socket->events = 0;
136	spin_lock_init(socket->event_lock);
137
138	/* Enable PC Card status interrupts */
139	exca_writeb(socket, CARD_SCI, CARD_DT_EN|RDY_EN|BAT_WAR_EN|BAT_DEAD_EN);
140
141	return 0;
142}
143
144static int cardu_register_callback(unsigned int sock,
145                                           void (*handler)(void *, unsigned int),
146                                           void * info)
147{
148	vrc4173_socket_t *socket = &cardu_sockets[sock];
149
150	socket->handler = handler;
151	socket->info = info;
152
153	return 0;
154}
155
156static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
157{
158	vrc4173_socket_t *socket = &cardu_sockets[sock];
159
160	*cap = socket->cap;
161
162	return 0;
163}
164
165static int cardu_get_status(unsigned int sock, u_int *value)
166{
167	vrc4173_socket_t *socket = &cardu_sockets[sock];
168	uint32_t state;
169	uint8_t status;
170	u_int val = 0;
171
172	status = exca_readb(socket, IF_STATUS);
173	if (status & CARD_PWR) val |= SS_POWERON;
174	if (status & READY) val |= SS_READY;
175	if (status & CARD_WP) val |= SS_WRPROT;
176	if ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2))
177		val |= SS_DETECT;
178	if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
179		if (status & STSCHG) val |= SS_STSCHG;
180	} else {
181		status &= BV_DETECT_MASK;
182		if (status != BV_DETECT_GOOD) {
183			if (status == BV_DETECT_WARN) val |= SS_BATWARN;
184			else val |= SS_BATDEAD;
185		}
186	}
187
188	state = cardbus_socket_readl(socket, SKT_PRE_STATE);
189	if (state & VOL_3V_CARD_DT) val |= SS_3VCARD;
190	if (state & VOL_XV_CARD_DT) val |= SS_XVCARD;
191	if (state & CB_CARD_DT) val |= SS_CARDBUS;
192	if (!(state &
193	      (VOL_YV_CARD_DT|VOL_XV_CARD_DT|VOL_3V_CARD_DT|VOL_5V_CARD_DT|CCD20|CCD10)))
194		val |= SS_PENDING;
195
196	*value = val;
197
198	return 0;
199}
200
201static inline u_char get_Vcc_value(uint8_t val)
202{
203	switch (val & VCC_MASK) {
204	case VCC_3V:
205		return 33;
206	case VCC_5V:
207		return 50;
208	}
209
210	return 0;
211}
212
213static inline u_char get_Vpp_value(uint8_t val)
214{
215	switch (val & VPP_MASK) {
216	case VPP_12V:
217		return 120;
218	case VPP_VCC:
219		return get_Vcc_value(val);
220	}
221
222	return 0;
223}
224
225static int cardu_get_socket(unsigned int sock, socket_state_t *state)
226{
227	vrc4173_socket_t *socket = &cardu_sockets[sock];
228	uint8_t val;
229
230	val = exca_readb(socket, PWR_CNT);
231	state->Vcc = get_Vcc_value(val);
232	state->Vpp = get_Vpp_value(val);
233	state->flags = 0;
234	if (val & CARD_OUT_EN) state->flags |= SS_OUTPUT_ENA;
235
236	val = exca_readb(socket, INT_GEN_CNT);
237	if (!(val & CARD_REST0)) state->flags |= SS_RESET;
238	if (val & CARD_TYPE_IO) state->flags |= SS_IOCARD;
239
240	return 0;
241}
242
243static inline uint8_t set_Vcc_value(u_char Vcc)
244{
245	switch (Vcc) {
246	case 33:
247		return VCC_3V;
248	case 50:
249		return VCC_5V;
250	}
251
252	return VCC_0V;
253}
254
255static inline uint8_t set_Vpp_value(u_char Vpp)
256{
257	switch (Vpp) {
258	case 33:
259	case 50:
260		return VPP_VCC;
261	case 120:
262		return VPP_12V;
263	}
264
265	return VPP_0V;
266}
267
268static int cardu_set_socket(unsigned int sock, socket_state_t *state)
269{
270	vrc4173_socket_t *socket = &cardu_sockets[sock];
271	uint8_t val;
272
273	if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
274			return -EINVAL;
275
276	val = set_Vcc_value(state->Vcc);
277	val |= set_Vpp_value(state->Vpp);
278	if (state->flags & SS_OUTPUT_ENA) val |= CARD_OUT_EN;
279	exca_writeb(socket, PWR_CNT, val);
280
281	val = exca_readb(socket, INT_GEN_CNT) & CARD_REST0;
282	if (state->flags & SS_RESET) val &= ~CARD_REST0;
283	else val |= CARD_REST0;
284	if (state->flags & SS_IOCARD) val |= CARD_TYPE_IO;
285	exca_writeb(socket, INT_GEN_CNT, val);
286
287	return 0;
288}
289
290static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
291{
292	vrc4173_socket_t *socket = &cardu_sockets[sock];
293	uint8_t ioctl, window;
294	u_char map;
295
296	map = io->map;
297	if (map > 1)
298		return -EINVAL;
299
300	io->start = exca_readw(socket, IO_WIN_SA(map));
301	io->stop = exca_readw(socket, IO_WIN_EA(map));
302
303	ioctl = exca_readb(socket, IO_WIN_CNT);
304	window = exca_readb(socket, ADR_WIN_EN);
305	io->flags  = (window & IO_WIN_EN(map)) ? MAP_ACTIVE : 0;
306	if (ioctl & IO_WIN_DATA_AUTOSZ(map))
307		io->flags |= MAP_AUTOSZ;
308	else if (ioctl & IO_WIN_DATA_16BIT(map))
309		io->flags |= MAP_16BIT;
310
311	return 0;
312}
313
314static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
315{
316	vrc4173_socket_t *socket = &cardu_sockets[sock];
317	uint16_t ioctl;
318	uint8_t window, enable;
319	u_char map;
320
321	map = io->map;
322	if (map > 1)
323		return -EINVAL;
324
325	window = exca_readb(socket, ADR_WIN_EN);
326	enable = IO_WIN_EN(map);
327
328	if (window & enable) {
329		window &= ~enable;
330		exca_writeb(socket, ADR_WIN_EN, window);
331	}
332
333	exca_writew(socket, IO_WIN_SA(map), io->start);
334	exca_writew(socket, IO_WIN_EA(map), io->stop);
335
336	ioctl = exca_readb(socket, IO_WIN_CNT) & ~IO_WIN_CNT_MASK(map);
337	if (io->flags & MAP_AUTOSZ) ioctl |= IO_WIN_DATA_AUTOSZ(map);
338	else if (io->flags & MAP_16BIT) ioctl |= IO_WIN_DATA_16BIT(map);
339	exca_writeb(socket, IO_WIN_CNT, ioctl);
340
341	if (io->flags & MAP_ACTIVE)
342		exca_writeb(socket, ADR_WIN_EN, window | enable);
343
344	return 0;
345}
346
347static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
348{
349	vrc4173_socket_t *socket = &cardu_sockets[sock];
350	uint32_t start, stop, offset, page;
351	uint8_t window;
352	u_char map;
353
354	map = mem->map;
355	if (map > 4)
356		return -EINVAL;
357
358	window = exca_readb(socket, ADR_WIN_EN);
359	mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
360
361	start = exca_readw(socket, MEM_WIN_SA(map));
362	mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
363	start = (start & 0x0fff) << 12;
364
365	stop = exca_readw(socket, MEM_WIN_EA(map));
366	stop = ((stop & 0x0fff) << 12) + 0x0fff;
367
368	offset = exca_readw(socket, MEM_WIN_OA(map));
369	mem->flags |= (offset & MEM_WIN_WP) ? MAP_WRPROT : 0;
370	mem->flags |= (offset & MEM_WIN_REGSET) ? MAP_ATTRIB : 0;
371	offset = ((offset & 0x3fff) << 12) + start;
372	mem->card_start = offset & 0x03ffffff;
373
374	page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
375	mem->sys_start = start + page;
376	mem->sys_stop = start + page;
377
378	return 0;
379}
380
381static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
382{
383	vrc4173_socket_t *socket = &cardu_sockets[sock];
384	uint16_t value;
385	uint8_t window, enable;
386	u_long sys_start, sys_stop, card_start;
387	u_char map;
388
389	map = mem->map;
390	sys_start = mem->sys_start;
391	sys_stop = mem->sys_stop;
392	card_start = mem->card_start;
393
394	if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
395	    (card_start >> 26))
396		return -EINVAL;
397
398	window = exca_readb(socket, ADR_WIN_EN);
399	enable = MEM_WIN_EN(map);
400	if (window & enable) {
401		window &= ~enable;
402		exca_writeb(socket, ADR_WIN_EN, window);
403	}
404
405	exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
406
407	value = (sys_start >> 12) & 0x0fff;
408	if (mem->flags & MAP_16BIT) value |= MEM_WIN_DSIZE;
409	exca_writew(socket, MEM_WIN_SA(map), value);
410
411	value = (sys_stop >> 12) & 0x0fff;
412	exca_writew(socket, MEM_WIN_EA(map), value);
413
414	value = ((card_start - sys_start) >> 12) & 0x3fff;
415	if (mem->flags & MAP_WRPROT) value |= MEM_WIN_WP;
416	if (mem->flags & MAP_ATTRIB) value |= MEM_WIN_REGSET;
417	exca_writew(socket, MEM_WIN_OA(map), value);
418
419	if (mem->flags & MAP_ACTIVE)
420		exca_writeb(socket, ADR_WIN_EN, window | enable);
421
422	return 0;
423}
424
425static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
426{
427}
428
429static struct pccard_operations cardu_operations = {
430	.init			= cardu_init,
431	.register_callback	= cardu_register_callback,
432	.inquire_socket		= cardu_inquire_socket,
433	.get_status		= cardu_get_status,
434	.get_socket		= cardu_get_socket,
435	.set_socket		= cardu_set_socket,
436	.get_io_map		= cardu_get_io_map,
437	.set_io_map		= cardu_set_io_map,
438	.get_mem_map		= cardu_get_mem_map,
439	.set_mem_map		= cardu_set_mem_map,
440	.proc_setup		= cardu_proc_setup,
441};
442
443static void cardu_bh(void *data)
444{
445	vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
446	uint16_t events;
447
448	spin_lock_irq(&socket->event_lock);
449	events = socket->events;
450	socket->events = 0;
451	spin_unlock_irq(&socket->event_lock);
452
453	if (socket->handler)
454		socket->handler(socket->info, events);
455}
456
457static uint16_t get_events(vrc4173_socket_t *socket)
458{
459	uint16_t events = 0;
460	uint8_t csc, status;
461
462	status = exca_readb(socket, IF_STATUS);
463	csc = exca_readb(socket, CARD_SC);
464	if ((csc & CARD_DT_CHG) &&
465	    ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2)))
466		events |= SS_DETECT;
467
468	if ((csc & RDY_CHG) && (status & READY))
469		events |= SS_READY;
470
471	if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
472		if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
473			events |= SS_STSCHG;
474	} else {
475		if (csc & (BAT_WAR_CHG|BAT_DEAD_ST_CHG)) {
476			if ((status & BV_DETECT_MASK) != BV_DETECT_GOOD) {
477				if (status == BV_DETECT_WARN) events |= SS_BATWARN;
478				else events |= SS_BATDEAD;
479			}
480		}
481	}
482
483	return events;
484}
485
486static void cardu_interrupt(int irq, void *dev_id, struct pt_regs *regs)
487{
488	vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
489	uint16_t events;
490
491	INIT_WORK(&socket->tq_work, cardu_bh, socket);
492
493	events = get_events(socket);
494	if (events) {
495		spin_lock(&socket->event_lock);
496		socket->events |= events;
497		spin_unlock(&socket->event_lock);
498		schedule_work(&socket->tq_work);
499	}
500}
501
502static int __devinit vrc4173_cardu_probe(struct pci_dev *dev,
503                                         const struct pci_device_id *ent)
504{
505	vrc4173_socket_t *socket;
506	unsigned long start, len, flags;
507	int slot, err;
508
509	slot = vrc4173_cardu_slots++;
510	socket = &cardu_sockets[slot];
511	if (socket->noprobe != 0)
512		return -EBUSY;
513
514	sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
515
516	if ((err = pci_enable_device(dev)) < 0)
517		return err;
518
519	start = pci_resource_start(dev, 0);
520	if (start == 0)
521		return -ENODEV;
522
523	len = pci_resource_len(dev, 0);
524	if (len == 0)
525		return -ENODEV;
526
527	if (((flags = pci_resource_flags(dev, 0)) & IORESOURCE_MEM) == 0)
528		return -EBUSY;
529
530	if ((err = pci_request_regions(dev, socket->name)) < 0)
531		return err;
532
533	socket->base = ioremap(start, len);
534	if (socket->base == NULL)
535		return -ENODEV;
536
537	socket->dev = dev;
538
539	socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
540	if (socket->pcmcia_socket == NULL) {
541		iounmap(socket->base);
542		socket->base = NULL;
543		return -ENOMEM;
544	}
545
546	if (request_irq(dev->irq, cardu_interrupt, SA_SHIRQ, socket->name, socket) < 0) {
547		pcmcia_unregister_socket(socket->pcmcia_socket);
548		socket->pcmcia_socket = NULL;
549		iounmap(socket->base);
550		socket->base = NULL;
551		return -EBUSY;
552	}
553
554	printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
555
556	return 0;
557}
558
559static int __devinit vrc4173_cardu_setup(char *options)
560{
561	if (options == NULL || *options == '\0')
562		return 0;
563
564	if (strncmp(options, "cardu1:", 7) == 0) {
565		options += 7;
566		if (*options != '\0') {
567			if (strncmp(options, "noprobe", 7) == 0) {
568				cardu_sockets[CARDU1].noprobe = 1;
569				options += 7;
570			}
571
572			if (*options != ',')
573				return 0;
574		} else
575			return 0;
576	}
577
578	if (strncmp(options, "cardu2:", 7) == 0) {
579		options += 7;
580		if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
581			cardu_sockets[CARDU2].noprobe = 1;
582	}
583
584	return 0;
585}
586
587__setup("vrc4173_cardu=", vrc4173_cardu_setup);
588
589static struct pci_device_id vrc4173_cardu_id_table[] __devinitdata = {
590	{	.vendor		= PCI_VENDOR_ID_NEC,
591		.device		= PCI_DEVICE_ID_NEC_NAPCCARD,
592		.subvendor	= PCI_ANY_ID,
593		.subdevice	= PCI_ANY_ID, },
594        {0, }
595};
596
597static struct pci_driver vrc4173_cardu_driver = {
598	.name		= "NEC VRC4173 CARDU",
599	.probe		= vrc4173_cardu_probe,
600	.id_table	= vrc4173_cardu_id_table,
601};
602
603static int __devinit vrc4173_cardu_init(void)
604{
605	vrc4173_cardu_slots = 0;
606
607	return pci_module_init(&vrc4173_cardu_driver);
608}
609
610static void __devexit vrc4173_cardu_exit(void)
611{
612	pci_unregister_driver(&vrc4173_cardu_driver);
613}
614
615module_init(vrc4173_cardu_init);
616module_exit(vrc4173_cardu_exit);
617MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);