m8xx_pcmcia.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:30k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.  * m8xx_pcmcia.c - Linux PCMCIA socket driver for the mpc8xx series.
  3.  *
  4.  * (C) 1999-2000 Magnus Damm <damm@bitsmart.com>
  5.  * (C) 2001-2002 Montavista Software, Inc.
  6.  *     <mlocke@mvista.com>
  7.  *
  8.  * "The ExCA standard specifies that socket controllers should provide 
  9.  * two IO and five memory windows per socket, which can be independently 
  10.  * configured and positioned in the host address space and mapped to 
  11.  * arbitrary segments of card address space. " - David A Hinds. 1999
  12.  *
  13.  * This controller does _not_ meet the ExCA standard.
  14.  * 
  15.  * m8xx pcmcia controller brief info:
  16.  * + 8 windows (attrib, mem, i/o)
  17.  * + up to two slots (SLOT_A and SLOT_B)
  18.  * + inputpins, outputpins, event and mask registers.
  19.  * - no offset register. sigh.
  20.  *
  21.  * Because of the lacking offset register we must map the whole card.
  22.  * We assign each memory window PCMCIA_MEM_WIN_SIZE address space.
  23.  * Make sure there is (PCMCIA_MEM_WIN_SIZE * PCMCIA_MEM_WIN_NO 
  24.  * * PCMCIA_SOCKETS_NO) bytes at PCMCIA_MEM_WIN_BASE.
  25.  * The i/o windows are dynamically allocated at PCMCIA_IO_WIN_BASE.
  26.  * They are maximum 64KByte each...
  27.  */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/string.h>
  33. #include <asm/io.h>
  34. #include <asm/bitops.h>
  35. #include <asm/segment.h>
  36. #include <asm/system.h>
  37. #include <linux/kernel.h>
  38. #include <linux/errno.h>
  39. #include <linux/sched.h>
  40. #include <linux/slab.h>
  41. #include <linux/timer.h>
  42. #include <linux/ioport.h>
  43. #include <linux/delay.h>
  44. #include <asm/mpc8xx.h>
  45. #include <asm/8xx_immap.h>
  46. #include <asm/irq.h>
  47. #include <pcmcia/version.h>
  48. #include <pcmcia/cs_types.h>
  49. #include <pcmcia/cs.h>
  50. #include <pcmcia/ss.h>
  51. #ifdef PCMCIA_DEBUG
  52. static int pc_debug = PCMCIA_DEBUG;
  53. MODULE_PARM(pc_debug, "i");
  54. #define DEBUG(n, args...) printk(KERN_DEBUG "m8xx_pcmcia: " args);
  55. #else
  56. #define DEBUG(n, args...)
  57. #endif
  58. #define PCMCIA_INFO(args...) printk(KERN_INFO "m8xx_pcmcia: "args)
  59. #define PCMCIA_ERROR(args...) printk(KERN_ERR "m8xx_pcmcia: "args)
  60. static const char *version = "Version 0.05, 14-Apr-2002";
  61. MODULE_LICENSE("Dual MPL/GPL");
  62. /* The RPX series use SLOT_B */
  63. #if defined(CONFIG_RPXCLASSIC) || defined(CONFIG_RPXLITE)
  64. #define CONFIG_PCMCIA_SLOT_B
  65. #endif
  66. /* The MBX board use SLOT_A */
  67. #ifdef CONFIG_MBX
  68. #define CONFIG_PCMCIA_SLOT_A
  69. #endif
  70. /* The FADS860T board use SLOT_A */
  71. #ifdef CONFIG_FADS
  72. #define CONFIG_PCMCIA_SLOT_A
  73. #endif
  74. /* ------------------------------------------------------------------------- */
  75. #define PCMCIA_MEM_WIN_BASE 0xe0000000 /* base address for memory window 0   */
  76. #define PCMCIA_MEM_WIN_SIZE 0x04000000 /* each memory window is 64 MByte     */
  77. #define PCMCIA_IO_WIN_BASE  _IO_BASE   /* base address for io window 0       */
  78. #define PCMCIA_SCHLVL PCMCIA_INTERRUPT /* Status Change Interrupt Level      */
  79. /* ------------------------------------------------------------------------- */
  80. #define PCMCIA_SOCKETS_NO 1
  81. #define PCMCIA_MEM_WIN_NO 5
  82. #define PCMCIA_IO_WIN_NO  2
  83. /* define _slot_ to be able to optimize macros */
  84. #ifdef CONFIG_PCMCIA_SLOT_A
  85. #define _slot_ 0
  86. #define PCMCIA_SLOT_MSG "SLOT_A"
  87. #else
  88. #define _slot_ 1
  89. #define PCMCIA_SLOT_MSG "SLOT_B"
  90. #endif
  91. #define M8XX_BUSFREQ ((((bd_t *)&(__res))->bi_busfreq))
  92. static int pcmcia_schlvl = PCMCIA_SCHLVL;
  93. /* ------------------------------------------------------------------------- */
  94. #define PCMCIA_SOCKET_KEY_5V 1
  95. #define PCMCIA_SOCKET_KEY_LV 2
  96. /* look up table for pgcrx registers */
  97. static u_int *m8xx_pgcrx[2] = {
  98. &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pgcra,
  99. &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pgcrb
  100. }; 
  101. /*
  102.  * This structure is used to address each window in the PCMCIA controller. 
  103.  * 
  104.  * Keep in mind that we assume that pcmcia_win_t[n+1] is mapped directly
  105.  * after pcmcia_win_t[n]... 
  106.  */
  107. typedef struct {             
  108. uint br;           
  109. uint or;
  110. } pcmcia_win_t;
  111. /* 
  112.  * For some reason the hardware guys decided to make both slots share
  113.  * some registers.
  114.  *
  115.  * Could someone invent object oriented hardware ?
  116.  *
  117.  * The macros are used to get the right bit from the registers.
  118.  * SLOT_A : slot = 0
  119.  * SLOT_B : slot = 1
  120.  */
  121. #define M8XX_PCMCIA_VS1(slot)      (0x80000000 >> (slot << 4))
  122. #define M8XX_PCMCIA_VS2(slot)      (0x40000000 >> (slot << 4))
  123. #define M8XX_PCMCIA_VS_MASK(slot)  (0xc0000000 >> (slot << 4))
  124. #define M8XX_PCMCIA_VS_SHIFT(slot) (30 - (slot << 4))
  125. #define M8XX_PCMCIA_WP(slot)       (0x20000000 >> (slot << 4))
  126. #define M8XX_PCMCIA_CD2(slot)      (0x10000000 >> (slot << 4))
  127. #define M8XX_PCMCIA_CD1(slot)      (0x08000000 >> (slot << 4))
  128. #define M8XX_PCMCIA_BVD2(slot)     (0x04000000 >> (slot << 4))
  129. #define M8XX_PCMCIA_BVD1(slot)     (0x02000000 >> (slot << 4))
  130. #define M8XX_PCMCIA_RDY(slot)      (0x01000000 >> (slot << 4))
  131. #define M8XX_PCMCIA_RDY_L(slot)    (0x00800000 >> (slot << 4))
  132. #define M8XX_PCMCIA_RDY_H(slot)    (0x00400000 >> (slot << 4))
  133. #define M8XX_PCMCIA_RDY_R(slot)    (0x00200000 >> (slot << 4))
  134. #define M8XX_PCMCIA_RDY_F(slot)    (0x00100000 >> (slot << 4))
  135. #define M8XX_PCMCIA_MASK(slot)     (0xFFFF0000 >> (slot << 4))
  136. #define M8XX_PGCRX(slot)  (*m8xx_pgcrx[slot])
  137. #define M8XX_PGCRX_CXOE    0x00000080
  138. #define M8XX_PGCRX_CXRESET 0x00000040
  139. /* we keep one lookup table per socket to check flags */ 
  140. #define PCMCIA_EVENTS_MAX 5  /* 4 max at a time + termination */
  141. typedef struct  {
  142. u_int regbit;
  143. u_int eventbit;
  144. } event_table_t;
  145. typedef struct socket_info_t {
  146.     void (*handler)(void *info, u_int events);
  147.     void *info;
  148.     u_int  slot;
  149.     socket_state_t state;
  150.     struct pccard_mem_map mem_win[PCMCIA_MEM_WIN_NO];
  151.     struct pccard_io_map  io_win[PCMCIA_IO_WIN_NO];
  152.     event_table_t events[PCMCIA_EVENTS_MAX]; 
  153. } socket_info_t;
  154. static socket_info_t socket[PCMCIA_SOCKETS_NO];
  155. static socket_cap_t capabilities = {
  156.     SS_CAP_PCCARD | SS_CAP_MEM_ALIGN | SS_CAP_STATIC_MAP, /* features - 
  157.      only 16-bit cards, 
  158.      memory windows must be
  159.      size-aligned */
  160.     0x000, /* irq_mask - SIU_LEVEL 7 -> 0          */
  161.     0x1000, /* map_size - 4K minimum window size    */
  162.     0, /* io_offset - */
  163.     9, /* pci_irq */
  164.     0 /* No PCI or CardBus support */
  165. };
  166. /* 
  167.  * Search this table to see if the windowsize is
  168.  * supported...
  169.  */
  170. #define M8XX_SIZES_NO 32
  171. static const u_int m8xx_size_to_gray[M8XX_SIZES_NO] = 
  172. { 0x00000001, 0x00000002, 0x00000008, 0x00000004,
  173.   0x00000080, 0x00000040, 0x00000010, 0x00000020,
  174.   0x00008000, 0x00004000, 0x00001000, 0x00002000,
  175.   0x00000100, 0x00000200, 0x00000800, 0x00000400,
  176.   0x0fffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  177.   0x01000000, 0x02000000, 0xffffffff, 0x04000000,
  178.   0x00010000, 0x00020000, 0x00080000, 0x00040000,
  179.   0x00800000, 0x00400000, 0x00100000, 0x00200000 };
  180. /* ------------------------------------------------------------------------- */
  181. static void m8xx_interrupt(int irq, void *dev, struct pt_regs *regs);
  182. #define PCMCIA_BMT_LIMIT (15*4)  /* Bus Monitor Timeout value */
  183. /* ------------------------------------------------------------------------- */
  184. /* board specific stuff:                                                     */
  185. /* voltage_set(), hardware_enable() and hardware_disable()                   */
  186. /* ------------------------------------------------------------------------- */
  187. /* RPX Boards from Embedded Planet                                           */
  188. #if defined(CONFIG_RPXCLASSIC) || defined(CONFIG_RPXLITE)
  189. /* The RPX boards seems to have it's bus monitor timeout set to 6*8 clocks.
  190.  * SYPCR is write once only, therefore must the slowest memory be faster 
  191.  * than the bus monitor or we will get a machine check due to the bus timeout.
  192.  */
  193. #define PCMCIA_BOARD_MSG "RPX CLASSIC or RPX LITE"
  194. #undef PCMCIA_BMT_LIMIT
  195. #define PCMCIA_BMT_LIMIT (6*8) 
  196. static int voltage_set(int slot, int vcc, int vpp)
  197. {
  198. u_int reg = 0;
  199. switch(vcc) {
  200. case 0: break;
  201. case 33: reg |= BCSR1_PCVCTL4; break;
  202. case 50: reg |= BCSR1_PCVCTL5; break;
  203. default: return 1;
  204. }
  205. switch(vpp) {
  206. case 0: break;
  207. case 33: 
  208. case 50:
  209. if(vcc == vpp)
  210. reg |= BCSR1_PCVCTL6;
  211. else
  212. return 1;
  213. break;
  214. case 120: 
  215. reg |= BCSR1_PCVCTL7;
  216. default: return 1;
  217. }
  218. if(!((vcc == 50) || (vcc == 0)))
  219.    return 1;
  220. /* first, turn off all power */
  221. *((uint *)RPX_CSR_ADDR) &= ~(BCSR1_PCVCTL4 | BCSR1_PCVCTL5
  222.      | BCSR1_PCVCTL6 | BCSR1_PCVCTL7);
  223. /* enable new powersettings */
  224. *((uint *)RPX_CSR_ADDR) |= reg;
  225. return 0;
  226. }
  227. #define socket_get(_slot_) PCMCIA_SOCKET_KEY_5V
  228. #define hardware_enable(_slot_)  /* No hardware to enable */
  229. #define hardware_disable(_slot_) /* No hardware to disable */
  230. #endif /* CONFIG_RPXCLASSIC */
  231. /* ------------------------------------------------------------------------- */
  232. /* FADS Boards from Motorola                                               */
  233. #if defined(CONFIG_FADS)
  234. #define PCMCIA_BOARD_MSG "FADS"
  235. static int voltage_set(int slot, int vcc, int vpp)
  236. {
  237. uint reg = 0;
  238. switch(vcc) {
  239. case 0: break;
  240. case 33: reg |= BCSR1_PCCVCC0; break;
  241. case 50: reg |= BCSR1_PCCVCC1; break;
  242. default: return 1;
  243. }
  244. switch(vpp) {
  245. case 0: break;
  246. case 33:
  247. case 50:
  248. if(vcc == vpp)
  249. reg |= BCSR1_PCCVPP1;
  250. else
  251. return 1;
  252. break;
  253. case 120:
  254. if ((vcc == 33) || (vcc == 50))
  255. reg |= BCSR1_PCCVPP0;
  256. else
  257. return 1;
  258. default: return 1;
  259. }
  260. /* first, turn off all power */
  261. *((uint *)BCSR1) &= ~(BCSR1_PCCVCC_MASK | BCSR1_PCCVPP_MASK);
  262. /* enable new powersettings */
  263. *((uint *)BCSR1) |= reg;
  264. return 0;
  265. }
  266. #define socket_get(_slot_) PCMCIA_SOCKET_KEY_5V
  267. static void hardware_enable(int slot)
  268. {
  269. *((uint *)BCSR1) &= ~BCSR1_PCCEN;
  270. }
  271. static void hardware_disable(int slot)
  272. {
  273. *((uint *)BCSR1) |=  BCSR1_PCCEN;
  274. }
  275. #endif
  276. /* ------------------------------------------------------------------------- */
  277. /* Motorola MBX860                                                           */
  278. #if defined(CONFIG_MBX)
  279. #define PCMCIA_BOARD_MSG "MBX"
  280. static int voltage_set(int slot, int vcc, int vpp)
  281. {
  282. unsigned char reg = 0;
  283. switch(vcc) {
  284. case 0: break;
  285. case 33: reg |= CSR2_VCC_33; break;
  286. case 50: reg |= CSR2_VCC_50; break;
  287. default: return 1;
  288. }
  289. switch(vpp) {
  290. case 0: break;
  291. case 33:
  292. case 50:
  293. if(vcc == vpp)
  294. reg |= CSR2_VPP_VCC;
  295. else
  296. return 1;
  297. break;
  298. case 120:
  299. if ((vcc == 33) || (vcc == 50))
  300. reg |= CSR2_VPP_12;
  301. else
  302. return 1;
  303. default: return 1;
  304. }
  305. /* first, turn off all power */
  306. *((unsigned char *)MBX_CSR2_ADDR) &= ~(CSR2_VCC_MASK | CSR2_VPP_MASK);
  307. /* enable new powersettings */
  308. *((unsigned char *)MBX_CSR2_ADDR) |= reg;
  309. return 0;
  310. }
  311. #define socket_get(_slot_) PCMCIA_SOCKET_KEY_5V
  312. #define hardware_enable(_slot_)  /* No hardware to enable */
  313. #define hardware_disable(_slot_) /* No hardware to disable */
  314. #endif /* CONFIG_MBX */
  315. /* ------------------------------------------------------------------------- */
  316. static void m8xx_shutdown(void)
  317. {
  318. u_int m;
  319. pcmcia_win_t *w;
  320. w = (void *) &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pbr0;
  321. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr = 
  322. M8XX_PCMCIA_MASK(_slot_); 
  323. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_per 
  324. &= ~M8XX_PCMCIA_MASK(_slot_);
  325.  
  326. /* turn off interrupt and disable CxOE */
  327. M8XX_PGCRX(_slot_) = M8XX_PGCRX_CXOE;
  328. /* turn off memory windows */
  329. for(m = 0; m < PCMCIA_MEM_WIN_NO; m++) {
  330. w->or = 0;  /* set to not valid */
  331. w++;
  332. }
  333. /* turn off voltage */
  334. voltage_set(_slot_, 0, 0);
  335. /* disable external hardware */
  336. hardware_disable(_slot_);
  337. free_irq(pcmcia_schlvl, NULL);
  338. }
  339. /* ------------------------------------------------------------------------- */
  340. /* ------------------------------------------------------------------------- */
  341. static u_int pending_events[PCMCIA_SOCKETS_NO];
  342. static spinlock_t pending_event_lock = SPIN_LOCK_UNLOCKED;
  343. static void m8xx_pcmcia_bh(void *dummy)
  344. {
  345. u_int events;
  346. int i;
  347. for (i=0; i < PCMCIA_SOCKETS_NO; i++) {
  348. spin_lock_irq(&pending_event_lock);
  349. events = pending_events[i];
  350. pending_events[i] = 0;
  351. spin_unlock_irq(&pending_event_lock);
  352. /*
  353. SS_DETECT events need a small delay here. The reason for this is that
  354. the "is there a card" electronics need time to see the card after the
  355. "we have a card coming in" electronics have seen it.
  356. */
  357. if (events & SS_DETECT)
  358. mdelay(4);
  359. if (socket[i].handler)
  360. socket[i].handler(socket[i].info, events);
  361. }
  362. }
  363. static struct tq_struct m8xx_pcmcia_task = {
  364. routine: m8xx_pcmcia_bh
  365. };
  366. static void m8xx_interrupt(int irq, void *dev, struct pt_regs *regs)
  367. {
  368. socket_info_t *s;
  369. event_table_t *e;
  370. u_int events, pscr, pipr;
  371. DEBUG(3,"Interrupt!n");
  372. /* get interrupt sources */
  373. pscr = ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr;
  374. pipr = ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pipr;
  375. s = &socket[0];
  376.     if(s->handler) {
  377.     e = &s->events[0]; 
  378.     events = 0;
  379.     while(e->regbit) {
  380.     if(pscr & e->regbit)
  381.     events |= e->eventbit;
  382.     
  383.     e++;
  384.     }
  385.     /* 
  386.      * report only if both card detect signals are the same 
  387.      * not too nice done, 
  388.      * we depend on that CD2 is the bit to the left of CD1...
  389.      */
  390.     if(events & SS_DETECT)
  391.     if(((pipr & M8XX_PCMCIA_CD2(_slot_)) >> 1)
  392.     ^ (pipr & M8XX_PCMCIA_CD1(_slot_)))
  393.     events &= ~SS_DETECT;
  394. #ifdef PCMCIA_GLITCHY_CD
  395.     
  396.     /*
  397.      * I've experienced CD problems with my ADS board.
  398.      * We make an extra check to see if there was a
  399.      * real change of Card detection.
  400.      */
  401.     if((events & SS_DETECT) && 
  402.        ((pipr &
  403.  (M8XX_PCMCIA_CD2(_slot_) | M8XX_PCMCIA_CD1(_slot_)))
  404. == 0) && (s->state.Vcc | s->state.Vpp)) {
  405.   events &= ~SS_DETECT;
  406.   printk( "CD glitch workaround - CD = 0x%08x!n",
  407. (pipr & (M8XX_PCMCIA_CD2(_slot_) 
  408.  | M8XX_PCMCIA_CD1(_slot_))));
  409.     }
  410. #endif
  411.     /* call the handler */
  412.     DEBUG(3,"slot %u: events = 0x%02x, pscr = 0x%08x, "
  413.   "pipr = 0x%08xn", 
  414.   _slot_, events, pscr, pipr);
  415.     if(events) {
  416.     spin_lock(&pending_event_lock);
  417.     pending_events[0] |= events;
  418.     spin_unlock(&pending_event_lock);
  419.     schedule_task(&m8xx_pcmcia_task);
  420.     }
  421.     }
  422.     /* clear the interrupt sources */
  423.     ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr = pscr;
  424.     
  425.     DEBUG(3,"Interrupt done.n");
  426.     
  427. }
  428. /* ------------------------------------------------------------------------- */
  429. static u_int m8xx_get_graycode(u_int size)
  430. {
  431. u_int k;
  432. for(k = 0; k < M8XX_SIZES_NO; k++)
  433. if(m8xx_size_to_gray[k] == size)
  434. break;
  435. if((k == M8XX_SIZES_NO) || (m8xx_size_to_gray[k] == -1))
  436. k = -1;
  437. return k;
  438. }
  439. /* ------------------------------------------------------------------------- */
  440. static u_int m8xx_get_speed(u_int ns, u_int is_io)
  441. {
  442. u_int reg, clocks, psst, psl, psht;
  443. if(!ns) {
  444. /*
  445.  * We get called with IO maps setup to 0ns
  446.  * if not specified by the user.
  447.  * They should be 255ns.
  448.  */
  449. if(is_io)
  450. ns = 255;
  451. else 
  452. ns = 100;  /* fast memory if 0 */
  453. }
  454. /* 
  455.  * In PSST, PSL, PSHT fields we tell the controller
  456.  * timing parameters in CLKOUT clock cycles.
  457.  * CLKOUT is the same as GCLK2_50.
  458.  */
  459. /* how we want to adjust the timing - in percent */
  460. #define ADJ 180 /* 80 % longer accesstime - to be sure */
  461. clocks = ((M8XX_BUSFREQ / 1000) * ns) / 1000;
  462. clocks = (clocks * ADJ) / (100*1000);
  463. if(clocks >= PCMCIA_BMT_LIMIT) {
  464. printk( "Max access time limit reachedn");
  465. clocks = PCMCIA_BMT_LIMIT-1;
  466. }
  467. psst = clocks / 7;          /* setup time */
  468. psht = clocks / 7;          /* hold time */
  469. psl  = (clocks * 5) / 7;    /* strobe length */
  470. psst += clocks - (psst + psht + psl);
  471. reg =  psst << 12;
  472. reg |= psl  << 7; 
  473. reg |= psht << 16;
  474. return reg;
  475. }
  476. /* ------------------------------------------------------------------------- */
  477. static int m8xx_register_callback(unsigned int sock, void (*handler)(void *, unsigned int), void * info)
  478. {
  479. socket[sock].handler = handler;
  480. socket[sock].info = info;
  481. if (handler == NULL) {
  482. MOD_DEC_USE_COUNT;
  483. else {
  484. MOD_INC_USE_COUNT;
  485. }
  486. return 0;
  487. }
  488. /* ------------------------------------------------------------------------- */
  489. static int m8xx_get_status(unsigned int lsock, u_int *value)
  490. {
  491. socket_info_t *s = &socket[lsock];
  492. u_int pipr, reg;
  493. pipr = ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pipr;
  494. *value  = ((pipr & (M8XX_PCMCIA_CD1(_slot_) 
  495.     | M8XX_PCMCIA_CD2(_slot_))) == 0) ? SS_DETECT : 0;
  496. *value |= (pipr & M8XX_PCMCIA_WP(_slot_)) ? SS_WRPROT : 0;
  497. if (s->state.flags & SS_IOCARD)
  498. *value |= (pipr & M8XX_PCMCIA_BVD1(_slot_)) ? SS_STSCHG : 0;
  499. else {
  500. *value |= (pipr & M8XX_PCMCIA_RDY(_slot_)) ? SS_READY : 0;
  501. *value |= (pipr & M8XX_PCMCIA_BVD1(_slot_)) ? SS_BATDEAD : 0;
  502. *value |= (pipr & M8XX_PCMCIA_BVD2(_slot_)) ? SS_BATWARN : 0;
  503. }
  504. if (s->state.Vcc | s->state.Vpp)
  505. *value |= SS_POWERON;
  506. /*
  507.  * Voltage detection:
  508.  * This driver only supports 16-Bit pc-cards.
  509.  * Cardbus is not handled here.
  510.  * 
  511.  * To determine what voltage to use we must read the VS1 and VS2 pin.
  512.  * Depending on what socket type is present,
  513.  * different combinations mean different things.
  514.  *
  515.  * Card Key  Socket Key   VS1   VS2   Card         Vcc for CIS parse
  516.  *   
  517.  * 5V        5V, LV*      NC    NC    5V only       5V (if available)
  518.  *           
  519.  * 5V        5V, LV*      GND   NC    5 or 3.3V     as low as possible
  520.  *
  521.  * 5V        5V, LV*      GND   GND   5, 3.3, x.xV  as low as possible
  522.  *
  523.  * LV*       5V            -     -    shall not fit into socket
  524.  *
  525.  * LV*       LV*          GND   NC    3.3V only     3.3V
  526.  *
  527.  * LV*       LV*          NC    GND   x.xV          x.xV (if avail.)
  528.  *
  529.  * LV*       LV*          GND   GND   3.3 or x.xV   as low as possible
  530.  *
  531.  * *LV means Low Voltage
  532.  *
  533.  *
  534.  * That gives us the following table:
  535.  *
  536.  * Socket    VS1  VS2   Voltage
  537.  *
  538.  * 5V        NC   NC    5V
  539.  * 5V        NC   GND   none (should not be possible)
  540.  * 5V        GND  NC    >= 3.3V
  541.  * 5V        GND  GND   >= x.xV 
  542.  *
  543.  * LV        NC   NC    5V   (if available)
  544.  * LV        NC   GND   x.xV (if available)
  545.  * LV        GND  NC    3.3V 
  546.  * LV        GND  GND   >= x.xV 
  547.  * 
  548.  * So, how do I determine if I have a 5V or a LV
  549.  * socket on my board?  Look at the socket!
  550.  *
  551.  * 
  552.  * Socket with 5V key:
  553.  * ++--------------------------------------------+
  554.  * ||                                            |
  555.  * ||                                           ||
  556.  * ||                                           ||
  557.  * |                                             |
  558.  * +---------------------------------------------+
  559.  *
  560.  * Socket with LV key:
  561.  * ++--------------------------------------------+
  562.  * ||                                            |
  563.  * |                                            ||
  564.  * |                                            ||
  565.  * |                                             |
  566.  * +---------------------------------------------+
  567.  *
  568.  *
  569.  * With other words - LV only cards does not fit
  570.  * into the 5V socket!
  571.  */
  572. /* read out VS1 and VS2 */
  573. reg = (pipr & M8XX_PCMCIA_VS_MASK(_slot_)) 
  574. >> M8XX_PCMCIA_VS_SHIFT(_slot_);
  575. if(socket_get(_slot_) == PCMCIA_SOCKET_KEY_LV) {
  576. switch(reg) {
  577. case 1: *value |= SS_3VCARD; break; /* GND, NC - 3.3V only */
  578. case 2: *value |= SS_XVCARD; break; /* NC. GND - x.xV only */
  579. };
  580. }
  581. DEBUG(3,"GetStatus(%d) = %#2.2xn", lsock, *value);
  582. return 0;
  583. }
  584.   
  585. /* ------------------------------------------------------------------------- */
  586. static int m8xx_inquire_socket(unsigned int lsock, socket_cap_t *cap)
  587. {
  588. *cap = capabilities;
  589. return 0;
  590. }
  591. /* ------------------------------------------------------------------------- */
  592. static int m8xx_get_socket(unsigned int lsock, socket_state_t *state)
  593. {
  594. *state = socket[lsock].state; /* copy the whole structure */
  595. DEBUG(3,"GetSocket(%d) = flags %#3.3x, Vcc %d, Vpp %d, "
  596.       "io_irq %d, csc_mask %#2.2xn", lsock, state->flags,
  597.       state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
  598. return 0;
  599. }
  600. /* ------------------------------------------------------------------------- */
  601. static int m8xx_set_socket(unsigned int lsock, socket_state_t *state)
  602. {
  603. socket_info_t *s = &socket[lsock];
  604. event_table_t *e;
  605. u_int reg;
  606. u_long flags;
  607. DEBUG(3, "SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
  608.       "io_irq %d, csc_mask %#2.2x)n", lsock, state->flags,
  609.       state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
  610. /* First, set voltage - bail out if invalid */
  611.     
  612. if(voltage_set(_slot_, state->Vcc, state->Vpp))
  613. return -EINVAL;
  614. /* Take care of reset... */
  615. if(state->flags & SS_RESET) 
  616. M8XX_PGCRX(_slot_) |=  M8XX_PGCRX_CXRESET; /* active high */
  617. else
  618. M8XX_PGCRX(_slot_) &= ~M8XX_PGCRX_CXRESET; 
  619. /* ... and output enable. */
  620. /* The CxOE signal is connected to a 74541 on the ADS.
  621.    I guess most other boards used the ADS as a reference.
  622.    I tried to control the CxOE signal with SS_OUTPUT_ENA,
  623.    but the reset signal seems connected via the 541. 
  624.    If the CxOE is left high are some signals tristated and
  625.    no pullups are present -> the cards act wierd.
  626.    So right now the buffers are enabled if the power is on. */
  627. if(state->Vcc || state->Vpp)
  628. M8XX_PGCRX(_slot_) &= ~M8XX_PGCRX_CXOE; /* active low */
  629. else
  630. M8XX_PGCRX(_slot_) |=  M8XX_PGCRX_CXOE;
  631. /* 
  632.  * We'd better turn off interrupts before 
  633.  * we mess with the events-table..
  634.  */
  635. save_flags(flags);
  636. cli();
  637. /*
  638.  * Play around with the interrupt mask to be able to
  639.  * give the events the generic pcmcia driver wants us to.
  640.  */
  641. e = &s->events[0]; 
  642. reg = 0;
  643. if(state->csc_mask & SS_DETECT) {
  644. e->eventbit = SS_DETECT;
  645. reg |= e->regbit = (M8XX_PCMCIA_CD2(_slot_) 
  646.     | M8XX_PCMCIA_CD1(_slot_));
  647. e++;
  648. }
  649. if(state->flags & SS_IOCARD) {
  650. /* 
  651.  * I/O card
  652.  */
  653. if(state->csc_mask & SS_STSCHG) {
  654. e->eventbit = SS_STSCHG;
  655. reg |= e->regbit = M8XX_PCMCIA_BVD1(_slot_);
  656. e++;
  657. }
  658. /*
  659.  * If io_irq is non-zero we should enable irq.
  660.  */
  661. if(state->io_irq) {
  662. M8XX_PGCRX(_slot_) |= 
  663. mk_int_int_mask(state->io_irq) << 24;
  664. /*
  665.  * Strange thing here:
  666.  * The manual does not tell us which interrupt
  667.  * the sources generate.
  668.  * Anyhow, I found out that RDY_L generates IREQLVL.
  669.  *
  670.  * We use level triggerd interrupts, and they don't
  671.  * have to be cleared in PSCR in the interrupt handler.
  672.  */
  673. reg |= M8XX_PCMCIA_RDY_L(_slot_);  
  674. }
  675. else
  676. M8XX_PGCRX(_slot_) &= 0x00ffffff;
  677. }
  678. else {
  679. /*
  680.  * Memory card
  681.  */
  682. if(state->csc_mask & SS_BATDEAD) {
  683. e->eventbit = SS_BATDEAD;
  684. reg |= e->regbit = M8XX_PCMCIA_BVD1(_slot_);
  685. e++;
  686. }
  687. if(state->csc_mask & SS_BATWARN) {
  688. e->eventbit = SS_BATWARN;
  689. reg |= e->regbit = M8XX_PCMCIA_BVD2(_slot_);
  690. e++;
  691. }
  692. /* What should I trigger on - low/high,raise,fall? */
  693. if(state->csc_mask & SS_READY) {
  694. e->eventbit = SS_READY;
  695. reg |= e->regbit = 0; //??
  696. e++;
  697. }
  698. }
  699. e->regbit = 0;  /* terminate list */
  700. /* 
  701.  * Clear the status changed .
  702.  * Port A and Port B share the same port.
  703.  * Writing ones will clear the bits.
  704.  */
  705. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr = reg;
  706. /*
  707.  * Write the mask.
  708.  * Port A and Port B share the same port.
  709.  * Need for read-modify-write. 
  710.  * Ones will enable the interrupt.
  711.  */
  712. reg |= ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_per 
  713. & M8XX_PCMCIA_MASK(_slot_);
  714. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_per = reg;
  715. restore_flags(flags);
  716. /* copy the struct and modify the copy */
  717. s->state = *state;
  718. return 0;
  719. }
  720. /* ------------------------------------------------------------------------- */
  721. static int m8xx_get_io_map(unsigned int lsock, struct pccard_io_map *io)
  722. {
  723. if(io->map >= PCMCIA_IO_WIN_NO)
  724. return -EINVAL;
  725. *io = socket[lsock].io_win[io->map]; /* copy the struct */
  726. DEBUG(3,"GetIOMap(%d, %d) = %#2.2x, %d ns, "
  727.       "%#4.4x-%#4.4xn", lsock, io->map, io->flags,
  728.       io->speed, io->start, io->stop);
  729. return 0;
  730. }
  731. /* ------------------------------------------------------------------------- */
  732. static int m8xx_set_io_map(unsigned int lsock, struct pccard_io_map *io)
  733. {
  734. socket_info_t *s = &socket[lsock];
  735. pcmcia_win_t *w;
  736. u_int reg, winnr;
  737. #define M8XX_SIZE (io->stop - io->start + 1)   
  738. #define M8XX_BASE (PCMCIA_IO_WIN_BASE + io->start)
  739. DEBUG(3, "SetIOMap(%d, %d, %#2.2x, %d ns, "
  740.       "%#4.4x-%#4.4x)n", lsock, io->map, io->flags,
  741.       io->speed, io->start, io->stop);
  742. if ((io->map >= PCMCIA_IO_WIN_NO) || (io->start > 0xffff) 
  743.     || (io->stop > 0xffff) || (io->stop < io->start)) 
  744. return -EINVAL;
  745. if((reg = m8xx_get_graycode(M8XX_SIZE)) == -1)
  746. return -EINVAL;
  747. if(io->flags & MAP_ACTIVE) {
  748. winnr = (PCMCIA_MEM_WIN_NO * PCMCIA_SOCKETS_NO) 
  749. + (lsock * PCMCIA_IO_WIN_NO) + io->map;
  750. /* setup registers */
  751. w = (void *) &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pbr0;
  752. w += winnr;
  753. w->or = 0; /* turn off window first */
  754. w->br = M8XX_BASE;
  755. reg <<= 27;     
  756.    reg |= 0x00018 + (_slot_ << 2);
  757. reg |= m8xx_get_speed(io->speed, 1);
  758. if(io->flags & MAP_WRPROT) 
  759. reg |= 0x00000002;
  760. if(io->flags & (MAP_16BIT | MAP_AUTOSZ))
  761. reg |= 0x00000040;
  762. if(io->flags & MAP_ACTIVE) 
  763. reg |= 0x00000001;
  764. w->or = reg;
  765. DEBUG(3,"Socket %u: Mapped io window %u at %#8.8x, "
  766.       "OR = %#8.8x.n", lsock, io->map, w->br, w->or);
  767. }
  768. /* copy the struct and modify the copy */
  769. s->io_win[io->map] = *io; 
  770. s->io_win[io->map].flags &= (MAP_WRPROT 
  771.      | MAP_16BIT
  772.      | MAP_ACTIVE);
  773. return 0;
  774. }
  775. /* ------------------------------------------------------------------------- */
  776. static int m8xx_get_mem_map(unsigned int lsock, struct pccard_mem_map *mem)
  777. {
  778. if(mem->map >= PCMCIA_MEM_WIN_NO)
  779. return -EINVAL;
  780. *mem = socket[lsock].mem_win[mem->map]; /* copy the struct */
  781. DEBUG(3, "GetMemMap(%d, %d) = %#2.2x, %d ns, "
  782.       "%#5.5lx-%#5.5lx, %#5.5xn", lsock, mem->map, mem->flags,
  783.       mem->speed, mem->sys_start, mem->sys_stop, mem->card_start);
  784. return 0;
  785. }
  786. /* ------------------------------------------------------------------------- */
  787. static int m8xx_set_mem_map(unsigned int lsock, struct pccard_mem_map *mem)
  788. {
  789. socket_info_t *s = &socket[lsock];
  790. pcmcia_win_t *w;
  791. struct pccard_mem_map *old;
  792. u_int reg, winnr;
  793. DEBUG(3, "SetMemMap(%d, %d, %#2.2x, %d ns, "
  794.       "%#5.5lx-%#5.5lx, %#5.5x)n", lsock, mem->map, mem->flags,
  795.       mem->speed, mem->sys_start, mem->sys_stop, mem->card_start);
  796. if ((mem->map >= PCMCIA_MEM_WIN_NO) || (mem->sys_start > mem->sys_stop)
  797.     || ((mem->sys_stop - mem->sys_start) >= PCMCIA_MEM_WIN_SIZE)
  798.     || (mem->card_start >= 0x04000000) 
  799.     || (mem->sys_start & 0xfff)                /* 4KByte resolution */
  800.     || (mem->card_start & 0xfff))                   
  801. return -EINVAL;
  802. if((reg = m8xx_get_graycode(PCMCIA_MEM_WIN_SIZE)) == -1) {
  803. printk( "Cannot set size to 0x%08x.n", PCMCIA_MEM_WIN_SIZE);
  804. return -EINVAL;
  805. }
  806. winnr = (lsock * PCMCIA_MEM_WIN_NO) + mem->map;
  807. /* Setup the window in the pcmcia controller */
  808. w = (void *) &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pbr0;
  809. w += winnr;
  810. reg <<= 27;
  811. reg |= _slot_ << 2;
  812. reg |= m8xx_get_speed(mem->speed, 0);
  813. if(mem->flags & MAP_ATTRIB) 
  814. reg |= 0x00000010;
  815. if(mem->flags & MAP_WRPROT) 
  816. reg |= 0x00000002;
  817. if(mem->flags & MAP_16BIT) 
  818. reg |= 0x00000040;
  819. if(mem->flags & MAP_ACTIVE) 
  820. reg |= 0x00000001;
  821. w->or = reg;
  822. DEBUG(3, "Socket %u: Mapped memory window %u at %#8.8x, "
  823.       "OR = %#8.8x.n", lsock, mem->map, w->br, w->or);
  824. if(mem->flags & MAP_ACTIVE) {
  825. mem->sys_stop -= mem->sys_start;
  826. /* get the new base address */
  827. mem->sys_start = PCMCIA_MEM_WIN_BASE + 
  828. (PCMCIA_MEM_WIN_SIZE * winnr)
  829. + mem->card_start; 
  830. mem->sys_stop += mem->sys_start;
  831. }
  832. DEBUG(3, "SetMemMap(%d, %d, %#2.2x, %d ns, "
  833.       "%#5.5lx-%#5.5lx, %#5.5x)n", lsock, mem->map, mem->flags,
  834.       mem->speed, mem->sys_start, mem->sys_stop, mem->card_start);
  835. /* copy the struct and modify the copy */
  836. old = &s->mem_win[mem->map];
  837. *old = *mem;
  838. old->flags &= (MAP_ATTRIB
  839.        | MAP_WRPROT 
  840.        | MAP_16BIT
  841.        | MAP_ACTIVE);
  842. return 0;
  843. }
  844. static int m8xx_sock_init(unsigned int s)
  845. {
  846. int i;
  847. pccard_io_map io = { 0, 0, 0, 0, 1 };
  848. pccard_mem_map mem = { 0, 0, 0, 0, 0, 0 };
  849. DEBUG(3, "sock_init(%d)n", s);
  850. mem.sys_stop = 0x1000;
  851. m8xx_set_socket(s, &dead_socket);
  852. for (i = 0; i < PCMCIA_IO_WIN_NO; i++) {
  853. io.map = i;
  854. m8xx_set_io_map(s, &io);
  855. }
  856. for (i = 0; i < PCMCIA_MEM_WIN_NO; i++) {
  857. mem.map = i;
  858. m8xx_set_mem_map(s, &mem);
  859. }
  860. return 0;
  861. }
  862. static int m8xx_suspend(unsigned int s)
  863. {
  864. return(m8xx_set_socket(s, &dead_socket));
  865. }
  866. static void m8xx_proc_setup(unsigned int sock, struct proc_dir_entry *base)
  867. {
  868. }
  869. /* ------------------------------------------------------------------------- */
  870.     
  871. static struct pccard_operations m8xx_services = {
  872. &m8xx_sock_init,
  873. &m8xx_suspend,
  874. &m8xx_register_callback,
  875. &m8xx_inquire_socket,
  876. &m8xx_get_status,
  877. &m8xx_get_socket,
  878. &m8xx_set_socket,
  879. &m8xx_get_io_map,
  880. &m8xx_set_io_map,
  881. &m8xx_get_mem_map,
  882. &m8xx_set_mem_map,
  883. &m8xx_proc_setup
  884. };
  885. static int __init m8xx_init(void)
  886. {
  887. servinfo_t serv;
  888. pcmcia_win_t *w;
  889. u_int m;
  890. PCMCIA_INFO("%sn", version);
  891. CardServices(GetCardServicesInfo, &serv);
  892. if (serv.Revision != CS_RELEASE_CODE) {
  893. PCMCIA_ERROR("Card Services release does not match!n");
  894. return -1;
  895. }
  896. PCMCIA_INFO(PCMCIA_BOARD_MSG " using " PCMCIA_SLOT_MSG 
  897.     " with IRQ %u.n", pcmcia_schlvl); 
  898. /* Configure Status change interrupt */
  899. if(request_8xxirq(pcmcia_schlvl, m8xx_interrupt, 0, 
  900.    "m8xx_pcmcia", NULL)) {
  901. PCMCIA_ERROR("Cannot allocate IRQ %u for SCHLVL!n", 
  902.      pcmcia_schlvl);
  903. return -1;
  904. }
  905. w = (void *) &((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pbr0;
  906. socket[0].slot = _slot_;
  907.     
  908. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_pscr = 
  909. M8XX_PCMCIA_MASK(_slot_); 
  910. ((immap_t *)IMAP_ADDR)->im_pcmcia.pcmc_per 
  911. &= ~M8XX_PCMCIA_MASK(_slot_);
  912.  
  913. /* connect interrupt and disable CxOE */
  914. M8XX_PGCRX(_slot_) = M8XX_PGCRX_CXOE |
  915. (mk_int_int_mask(pcmcia_schlvl) << 16);
  916. /* intialize the fixed memory windows */
  917. for(m = 0; m < PCMCIA_MEM_WIN_NO; m++) {
  918. w->br = PCMCIA_MEM_WIN_BASE + 
  919. (PCMCIA_MEM_WIN_SIZE 
  920.  * (m + 0 * PCMCIA_MEM_WIN_NO));
  921. w->or = 0;  /* set to not valid */
  922. DEBUG(3,"Socket %u: MemWin %u: Base 0x%08x.n",
  923.       0, m, w->br);
  924. w++;
  925. }
  926. /* turn off voltage */
  927. voltage_set(_slot_, 0, 0);
  928. /* Enable external hardware */
  929. hardware_enable(_slot_);
  930. if(register_ss_entry(PCMCIA_SOCKETS_NO, &m8xx_services) != 0) {
  931.     PCMCIA_ERROR("register_ss_entry() failed.n");
  932.     m8xx_shutdown();
  933.     return -ENODEV;
  934. }
  935. return 0;
  936. }
  937. static void __exit m8xx_exit(void)
  938. {
  939. unregister_ss_entry(&m8xx_services);
  940. m8xx_shutdown();
  941. }
  942. module_init(m8xx_init);
  943. module_exit(m8xx_exit);