blob: a2b1a17f2067fe5c12657a510d603aa2f6de753c [file] [log] [blame]
wdenkabda5ca2003-05-31 18:35:21 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * (C) Copyright 2002
5 * Daniel Engström, Omicron Ceti AB
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 *
25 ********************************************************************
26 *
27 * Lots of code copied from:
28 *
29 * m8xx_pcmcia.c - Linux PCMCIA socket driver for the mpc8xx series.
30 * (C) 1999-2000 Magnus Damm <damm@bitsmart.com>
31 *
32 * "The ExCA standard specifies that socket controllers should provide
33 * two IO and five memory windows per socket, which can be independently
34 * configured and positioned in the host address space and mapped to
35 * arbitrary segments of card address space. " - David A Hinds. 1999
36 *
37 * This controller does _not_ meet the ExCA standard.
38 *
39 * m8xx pcmcia controller brief info:
40 * + 8 windows (attrib, mem, i/o)
41 * + up to two slots (SLOT_A and SLOT_B)
42 * + inputpins, outputpins, event and mask registers.
43 * - no offset register. sigh.
44 *
45 * Because of the lacking offset register we must map the whole card.
46 * We assign each memory window PCMCIA_MEM_WIN_SIZE address space.
47 * Make sure there is (PCMCIA_MEM_WIN_SIZE * PCMCIA_MEM_WIN_NO
48 * * PCMCIA_SOCKETS_NO) bytes at PCMCIA_MEM_WIN_BASE.
49 * The i/o windows are dynamically allocated at PCMCIA_IO_WIN_BASE.
50 * They are maximum 64KByte each...
51 */
52
53
54#undef DEBUG /**/
55
56/*
57 * PCMCIA support
58 */
59#include <common.h>
60#include <command.h>
61#include <config.h>
62#include <pci.h>
63#include <asm/io.h>
64
65#include <pcmcia.h>
66#include <cmd_pcmcia.h>
67
68#if (CONFIG_COMMANDS & CFG_CMD_PCMCIA) && defined(CONFIG_IDE_TI_CARDBUS)
69
70int pcmcia_on(int ide_base_bus);
71
72static int pcmcia_off(void);
73static int hardware_disable(int slot);
74static int hardware_enable(int slot);
75static int voltage_set(int slot, int vcc, int vpp);
76static void print_funcid(int func);
77static void print_fixed(volatile uchar *p);
78static int identify(volatile uchar *p);
79static int check_ide_device(int slot, int ide_base_bus);
80
81
82/* ------------------------------------------------------------------------- */
83
84
85const char *indent = "\t ";
86
87/* ------------------------------------------------------------------------- */
88
89
90int do_pinit(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
91{
92#ifndef CFG_FISRT_PCMCIA_BUS
93# define CFG_FISRT_PCMCIA_BUS 0
94#endif
95
96 int rcode = 0;
97
98 if (argc != 2) {
99 printf ("Usage: pinit {on | off}\n");
100 return 1;
101 }
102 if (strcmp(argv[1],"on") == 0) {
103 rcode = pcmcia_on(CFG_FISRT_PCMCIA_BUS);
104 } else if (strcmp(argv[1],"off") == 0) {
105 rcode = pcmcia_off();
106 } else {
107 printf ("Usage: pinit {on | off}\n");
108 return 1;
109 }
110
111 return rcode;
112}
113
114/* ------------------------------------------------------------------------- */
115
116
117static struct pci_device_id supported[] = {
118 { PCI_VENDOR_ID_TI, 0xac50 }, /* Ti PCI1410A */
119 { PCI_VENDOR_ID_TI, 0xac56 }, /* Ti PCI1510 */
120 { }
121};
122
123static pci_dev_t devbusfn;
124static u32 socket_base;
125static u32 pcmcia_cis_ptr;
126
127int pcmcia_on(int ide_base_bus)
128{
129 u16 dev_id;
130 u32 socket_status;
131 int slot = 0;
132 int cis_len;
133 u16 io_base;
134 u16 io_len;
135
136 /*
137 * Find the CardBus PCI device(s).
138 */
139 if ((devbusfn = pci_find_devices(supported, 0)) < 0) {
140 printf("Ti CardBus: not found\n");
141 return 1;
142 }
143
144 pci_read_config_word(devbusfn, PCI_DEVICE_ID, &dev_id);
145
146 if (dev_id == 0xac56) {
147 debug("Enable PCMCIA Ti PCI1510\n");
148 } else {
149 debug("Enable PCMCIA Ti PCI1410A\n");
150 }
151
152 pcmcia_cis_ptr = CFG_PCMCIA_CIS_WIN;
153 cis_len = CFG_PCMCIA_CIS_WIN_SIZE;
154
155 io_base = CFG_PCMCIA_IO_WIN;
156 io_len = CFG_PCMCIA_IO_WIN_SIZE;
157
158 /*
159 * Setup the PCI device.
160 */
161 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_0, &socket_base);
162 socket_base &= ~0xf;
163
164 socket_status = readl(socket_base+8);
165 if ((socket_status & 6) == 0) {
166 printf("Card Present: ");
167
168 switch (socket_status & 0x3c00) {
169
170 case 0x400:
171 printf("5V ");
172 break;
173 case 0x800:
174 printf("3.3V ");
175 break;
176 case 0xc00:
177 printf("3.3/5V ");
178 break;
179 default:
180 printf("unsupported Vcc ");
181 break;
182 }
183 switch (socket_status & 0x30) {
184 case 0x10:
185 printf("16bit PC-Card\n");
186 break;
187 case 0x20:
188 printf("32bit CardBus Card\n");
189 break;
190 default:
191 printf("8bit PC-Card\n");
192 break;
193 }
194 }
195
196
197 writeb(0x41, socket_base + 0x806); /* Enable I/O window 0 and memory window 0 */
198 writeb(0x0e, socket_base + 0x807); /* Reset I/O window options */
199
200 /* Careful: the linux yenta driver do not seem to reset the offset
201 * in the i/o windows, so leaving them non-zero is a problem */
202
203 writeb(io_base & 0xff, socket_base + 0x808); /* I/O window 0 base address */
204 writeb(io_base>>8, socket_base + 0x809);
205 writeb((io_base + io_len - 1) & 0xff, socket_base + 0x80a); /* I/O window 0 end address */
206 writeb((io_base + io_len - 1)>>8, socket_base + 0x80b);
207 writeb(0x00, socket_base + 0x836); /* I/O window 0 offset address 0x000 */
208 writeb(0x00, socket_base + 0x837);
209
210
211 writeb((pcmcia_cis_ptr&0x000ff000) >> 12,
212 socket_base + 0x810); /* Memory window 0 start address bits 19-12 */
213 writeb((pcmcia_cis_ptr&0x00f00000) >> 20,
214 socket_base + 0x811); /* Memory window 0 start address bits 23-20 */
215 writeb(((pcmcia_cis_ptr+cis_len-1) & 0x000ff000) >> 12,
216 socket_base + 0x812); /* Memory window 0 end address bits 19-12*/
217 writeb(((pcmcia_cis_ptr+cis_len-1) & 0x00f00000) >> 20,
218 socket_base + 0x813); /* Memory window 0 end address bits 23-20*/
219 writeb(0x00, socket_base + 0x814); /* Memory window 0 offset bits 19-12 */
220 writeb(0x40, socket_base + 0x815); /* Memory window 0 offset bits 23-20 and
221 * options (read/write, attribute access) */
222 writeb(0x00, socket_base + 0x816); /* ExCA card-detect and general control */
223 writeb(0x00, socket_base + 0x81e); /* ExCA global control (interrupt modes) */
224
225 writeb((pcmcia_cis_ptr & 0xff000000) >> 24,
226 socket_base + 0x840); /* Memory window address bits 31-24 */
227
228
229 /* turn off voltage */
230 if (voltage_set(slot, 0, 0)) {
231 return 1;
232 }
233
234 /* Enable external hardware */
235 if (hardware_enable(slot)) {
236 return 1;
237 }
238
239 if (check_ide_device(slot, ide_base_bus)) {
240 return 1;
241 }
242
243 return 0;
244}
245
246/* ------------------------------------------------------------------------- */
247
248
249static int pcmcia_off (void)
250{
251 int slot = 0;
252
253 writeb(0x00, socket_base + 0x806); /* disable all I/O and memory windows */
254
255 writeb(0x00, socket_base + 0x808); /* I/O window 0 base address */
256 writeb(0x00, socket_base + 0x809);
257 writeb(0x00, socket_base + 0x80a); /* I/O window 0 end address */
258 writeb(0x00, socket_base + 0x80b);
259 writeb(0x00, socket_base + 0x836); /* I/O window 0 offset address */
260 writeb(0x00, socket_base + 0x837);
261
262 writeb(0x00, socket_base + 0x80c); /* I/O window 1 base address */
263 writeb(0x00, socket_base + 0x80d);
264 writeb(0x00, socket_base + 0x80e); /* I/O window 1 end address */
265 writeb(0x00, socket_base + 0x80f);
266 writeb(0x00, socket_base + 0x838); /* I/O window 1 offset address */
267 writeb(0x00, socket_base + 0x839);
268
269 writeb(0x00, socket_base + 0x810); /* Memory window 0 start address */
270 writeb(0x00, socket_base + 0x811);
271 writeb(0x00, socket_base + 0x812); /* Memory window 0 end address */
272 writeb(0x00, socket_base + 0x813);
273 writeb(0x00, socket_base + 0x814); /* Memory window 0 offset */
274 writeb(0x00, socket_base + 0x815);
275
276 writeb(0xc0, socket_base + 0x840); /* Memory window 0 page address */
277
278
279 /* turn off voltage */
280 voltage_set(slot, 0, 0);
281
282 /* disable external hardware */
283 printf ("Shutdown and Poweroff Ti PCI1410A\n");
284 hardware_disable(slot);
285
286 return 0;
287}
288
289
290/* ------------------------------------------------------------------------- */
291
292
293#define MAX_TUPEL_SZ 512
294#define MAX_FEATURES 4
295int ide_devices_found;
296static int check_ide_device(int slot, int ide_base_bus)
297{
298 volatile uchar *ident = NULL;
299 volatile uchar *feature_p[MAX_FEATURES];
300 volatile uchar *p, *start;
301 int n_features = 0;
302 uchar func_id = ~0;
303 uchar code, len;
304 ushort config_base = 0;
305 int found = 0;
306 int i;
307 u32 socket_status;
308
309 debug ("PCMCIA MEM: %08X\n", pcmcia_cis_ptr);
310
311 socket_status = readl(socket_base+8);
312
313 if ((socket_status & 6) != 0 || (socket_status & 0x20) != 0) {
314 printf("no card or CardBus card\n");
315 return 1;
316 }
317
318 start = p = (volatile uchar *) pcmcia_cis_ptr;
319
320 while ((p - start) < MAX_TUPEL_SZ) {
321
322 code = *p; p += 2;
323
324 if (code == 0xFF) { /* End of chain */
325 break;
326 }
327
328 len = *p; p += 2;
329#if defined(DEBUG) && (DEBUG > 1)
330 {
331 volatile uchar *q = p;
332 printf ("\nTuple code %02x length %d\n\tData:",
333 code, len);
334
335 for (i = 0; i < len; ++i) {
336 printf (" %02x", *q);
337 q+= 2;
338 }
339 }
340#endif /* DEBUG */
341 switch (code) {
342 case CISTPL_VERS_1:
343 ident = p + 4;
344 break;
345 case CISTPL_FUNCID:
346 /* Fix for broken SanDisk which may have 0x80 bit set */
347 func_id = *p & 0x7F;
348 break;
349 case CISTPL_FUNCE:
350 if (n_features < MAX_FEATURES)
351 feature_p[n_features++] = p;
352 break;
353 case CISTPL_CONFIG:
354 config_base = (*(p+6) << 8) + (*(p+4));
355 debug ("\n## Config_base = %04x ###\n", config_base);
356 default:
357 break;
358 }
359 p += 2 * len;
360 }
361
362 found = identify(ident);
363
364 if (func_id != ((uchar)~0)) {
365 print_funcid (func_id);
366
367 if (func_id == CISTPL_FUNCID_FIXED)
368 found = 1;
369 else
370 return 1; /* no disk drive */
371 }
372
373 for (i=0; i<n_features; ++i) {
374 print_fixed(feature_p[i]);
375 }
376
377 if (!found) {
378 printf("unknown card type\n");
379 return 1;
380 }
381
382 /* select config index 1 */
383 writeb(1, pcmcia_cis_ptr + config_base);
384
385#if 0
386 printf("Confiuration Option Register: %02x\n", readb(pcmcia_cis_ptr + config_base));
387 printf("Card Confiuration and Status Register: %02x\n", readb(pcmcia_cis_ptr + config_base + 2));
388 printf("Pin Replacement Register Register: %02x\n", readb(pcmcia_cis_ptr + config_base + 4));
389 printf("Socket and Copy Register: %02x\n", readb(pcmcia_cis_ptr + config_base + 6));
390#endif
391 ide_devices_found |= (1 << (slot+ide_base_bus));
392
393 return 0;
394}
395
396
397
398static int voltage_set(int slot, int vcc, int vpp)
399{
400 u32 socket_control;
401 int reg=0;
402
403 switch (slot) {
404 case 0:
405 reg = socket_base + 0x10;
406 break;
407 default:
408 return 1;
409 }
410
411 socket_control = 0;
412
413
414 switch (vcc) {
415 case 50:
416 socket_control |= 0x20;
417 break;
418 case 33:
419 socket_control |= 0x30;
420 break;
421 case 0:
422 default:
423 }
424
425 switch (vpp) {
426 case 120:
427 socket_control |= 0x1;
428 break;
429 case 50:
430 socket_control |= 0x2;
431 break;
432 case 33:
433 socket_control |= 0x3;
434 break;
435 case 0:
436 default:
437 }
438
439 writel(socket_control, reg);
440
441 debug ("voltage_set: Ti PCI1410A Slot %d, Vcc=%d.%d, Vpp=%d.%d\n",
442 slot, vcc/10, vcc%10, vpp/10, vpp%10);
443
444 udelay(500);
445 return 0;
446}
447
448
449static int hardware_enable(int slot)
450{
451 u32 socket_status;
452 u16 brg_ctrl;
453 int is_82365sl;
454
455 socket_status = readl(socket_base+8);
456
457 if ((socket_status & 6) == 0) {
458
459 switch (socket_status & 0x3c00) {
460
461 case 0x400:
462 printf("5V ");
463 voltage_set(slot, 50, 0);
464 break;
465 case 0x800:
466 voltage_set(slot, 33, 0);
467 break;
468 case 0xc00:
469 voltage_set(slot, 33, 0);
470 break;
471 default:
472 voltage_set(slot, 0, 0);
473 break;
474 }
475 } else {
476 voltage_set(slot, 0, 0);
477 }
478
479 pci_read_config_word(devbusfn, PCI_BRIDGE_CONTROL, &brg_ctrl);
480 brg_ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
481 pci_write_config_word(devbusfn, PCI_BRIDGE_CONTROL, brg_ctrl);
482 is_82365sl = ((readb(socket_base+0x800) & 0x0f) == 2);
483 writeb(is_82365sl?0x90:0x98, socket_base+0x802);
484 writeb(0x67, socket_base+0x803);
485 udelay(100000);
486#if 0
487 printf("ExCA Id %02x, Card Status %02x, Power config %02x, Interrupt Config %02x, bridge control %04x %d\n",
488 readb(socket_base+0x800), readb(socket_base+0x801),
489 readb(socket_base+0x802), readb(socket_base+0x803), brg_ctrl, is_82365sl);
490#endif
491
492 return ((readb(socket_base+0x801)&0x6c)==0x6c)?0:1;
493}
494
495
496
497static int hardware_disable(int slot)
498{
499 voltage_set(slot, 0, 0);
500 return 0;
501}
502
503static void print_funcid(int func)
504{
505 puts(indent);
506 switch (func) {
507 case CISTPL_FUNCID_MULTI:
508 puts(" Multi-Function");
509 break;
510 case CISTPL_FUNCID_MEMORY:
511 puts(" Memory");
512 break;
513 case CISTPL_FUNCID_SERIAL:
514 puts(" Serial Port");
515 break;
516 case CISTPL_FUNCID_PARALLEL:
517 puts(" Parallel Port");
518 break;
519 case CISTPL_FUNCID_FIXED:
520 puts(" Fixed Disk");
521 break;
522 case CISTPL_FUNCID_VIDEO:
523 puts(" Video Adapter");
524 break;
525 case CISTPL_FUNCID_NETWORK:
526 puts(" Network Adapter");
527 break;
528 case CISTPL_FUNCID_AIMS:
529 puts(" AIMS Card");
530 break;
531 case CISTPL_FUNCID_SCSI:
532 puts(" SCSI Adapter");
533 break;
534 default:
535 puts(" Unknown");
536 break;
537 }
538 puts(" Card\n");
539}
540
541/* ------------------------------------------------------------------------- */
542
543static void print_fixed(volatile uchar *p)
544{
545 if (p == NULL)
546 return;
547
548 puts(indent);
549
550 switch (*p) {
551 case CISTPL_FUNCE_IDE_IFACE:
552 { uchar iface = *(p+2);
553
554 puts ((iface == CISTPL_IDE_INTERFACE) ? " IDE" : " unknown");
555 puts (" interface ");
556 break;
557 }
558 case CISTPL_FUNCE_IDE_MASTER:
559 case CISTPL_FUNCE_IDE_SLAVE:
560 {
561 uchar f1 = *(p+2);
562 uchar f2 = *(p+4);
563
564 puts((f1 & CISTPL_IDE_SILICON) ? " [silicon]" : " [rotating]");
565
566 if (f1 & CISTPL_IDE_UNIQUE) {
567 puts(" [unique]");
568 }
569
570 puts((f1 & CISTPL_IDE_DUAL) ? " [dual]" : " [single]");
571
572 if (f2 & CISTPL_IDE_HAS_SLEEP) {
573 puts(" [sleep]");
574 }
575
576 if (f2 & CISTPL_IDE_HAS_STANDBY) {
577 puts(" [standby]");
578 }
579
580 if (f2 & CISTPL_IDE_HAS_IDLE) {
581 puts(" [idle]");
582 }
583
584 if (f2 & CISTPL_IDE_LOW_POWER) {
585 puts(" [low power]");
586 }
587
588 if (f2 & CISTPL_IDE_REG_INHIBIT) {
589 puts(" [reg inhibit]");
590 }
591
592 if (f2 & CISTPL_IDE_HAS_INDEX) {
593 puts(" [index]");
594 }
595
596 if (f2 & CISTPL_IDE_IOIS16) {
597 puts(" [IOis16]");
598 }
599
600 break;
601 }
602 }
603 putc('\n');
604}
605
606/* ------------------------------------------------------------------------- */
607
608#define MAX_IDENT_CHARS 64
609#define MAX_IDENT_FIELDS 4
610
611static uchar *known_cards[] = {
612 "ARGOSY PnPIDE D5",
613 NULL
614};
615
616static int identify(volatile uchar *p)
617{
618 uchar id_str[MAX_IDENT_CHARS];
619 uchar data;
620 uchar *t;
621 uchar **card;
622 int i, done;
623
624 if (p == NULL)
625 return (0); /* Don't know */
626
627 t = id_str;
628 done =0;
629
630 for (i=0; i<=4 && !done; ++i, p+=2) {
631 while ((data = *p) != '\0') {
632 if (data == 0xFF) {
633 done = 1;
634 break;
635 }
636 *t++ = data;
637 if (t == &id_str[MAX_IDENT_CHARS-1]) {
638 done = 1;
639 break;
640 }
641 p += 2;
642 }
643 if (!done)
644 *t++ = ' ';
645 }
646 *t = '\0';
647 while (--t > id_str) {
648 if (*t == ' ') {
649 *t = '\0';
650 } else {
651 break;
652 }
653 }
654 puts(id_str);
655 putc('\n');
656
657 for (card=known_cards; *card; ++card) {
658 debug ("## Compare against \"%s\"\n", *card);
659 if (strcmp(*card, id_str) == 0) { /* found! */
660 debug ("## CARD FOUND ##\n");
661 return 1;
662 }
663 }
664
665 return 0; /* don't know */
666}
667
668#endif /* CONFIG_IDE_TI_CARDBUS */