m8xx_pcmcia.c 29 KB

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