m8xx_pcmcia.c 29 KB

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