m32r_cfc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * drivers/pcmcia/m32r_cfc.c
  3. *
  4. * Device driver for the CFC functionality of M32R.
  5. *
  6. * Copyright (c) 2001, 2002, 2003, 2004
  7. * Hiroyuki Kondo, Naoto Sugai, Hayato Fujiwara
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/init.h>
  12. #include <linux/config.h>
  13. #include <linux/types.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/string.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/timer.h>
  19. #include <linux/sched.h>
  20. #include <linux/slab.h>
  21. #include <linux/ioport.h>
  22. #include <linux/delay.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/device.h>
  26. #include <linux/bitops.h>
  27. #include <asm/irq.h>
  28. #include <asm/io.h>
  29. #include <asm/system.h>
  30. #include <pcmcia/cs_types.h>
  31. #include <pcmcia/ss.h>
  32. #include <pcmcia/cs.h>
  33. #undef MAX_IO_WIN /* FIXME */
  34. #define MAX_IO_WIN 1
  35. #undef MAX_WIN /* FIXME */
  36. #define MAX_WIN 1
  37. #include "m32r_cfc.h"
  38. #ifdef DEBUG
  39. static int m32r_cfc_debug;
  40. module_param(m32r_cfc_debug, int, 0644);
  41. #define debug(lvl, fmt, arg...) do { \
  42. if (m32r_cfc_debug > (lvl)) \
  43. printk(KERN_DEBUG "m32r_cfc: " fmt , ## arg); \
  44. } while (0)
  45. #else
  46. #define debug(n, args...) do { } while (0)
  47. #endif
  48. /* Poll status interval -- 0 means default to interrupt */
  49. static int poll_interval = 0;
  50. typedef enum pcc_space { as_none = 0, as_comm, as_attr, as_io } pcc_as_t;
  51. typedef struct pcc_socket {
  52. u_short type, flags;
  53. struct pcmcia_socket socket;
  54. unsigned int number;
  55. kio_addr_t ioaddr;
  56. u_long mapaddr;
  57. u_long base; /* PCC register base */
  58. u_char cs_irq1, cs_irq2, intr;
  59. pccard_io_map io_map[MAX_IO_WIN];
  60. pccard_mem_map mem_map[MAX_WIN];
  61. u_char io_win;
  62. u_char mem_win;
  63. pcc_as_t current_space;
  64. u_char last_iodbex;
  65. #ifdef CONFIG_PROC_FS
  66. struct proc_dir_entry *proc;
  67. #endif
  68. } pcc_socket_t;
  69. static int pcc_sockets = 0;
  70. static pcc_socket_t socket[M32R_MAX_PCC] = {
  71. { 0, }, /* ... */
  72. };
  73. /*====================================================================*/
  74. static unsigned int pcc_get(u_short, unsigned int);
  75. static void pcc_set(u_short, unsigned int , unsigned int );
  76. static DEFINE_SPINLOCK(pcc_lock);
  77. #if !defined(CONFIG_PLAT_USRV)
  78. static inline u_long pcc_port2addr(unsigned long port, int size) {
  79. u_long addr = 0;
  80. u_long odd;
  81. if (size == 1) { /* byte access */
  82. odd = (port&1) << 11;
  83. port -= port & 1;
  84. addr = CFC_IO_MAPBASE_BYTE - CFC_IOPORT_BASE + odd + port;
  85. } else if (size == 2)
  86. addr = CFC_IO_MAPBASE_WORD - CFC_IOPORT_BASE + port;
  87. return addr;
  88. }
  89. #else /* CONFIG_PLAT_USRV */
  90. static inline u_long pcc_port2addr(unsigned long port, int size) {
  91. u_long odd;
  92. u_long addr = ((port - CFC_IOPORT_BASE) & 0xf000) << 8;
  93. if (size == 1) { /* byte access */
  94. odd = port & 1;
  95. port -= odd;
  96. odd <<= 11;
  97. addr = (addr | CFC_IO_MAPBASE_BYTE) + odd + (port & 0xfff);
  98. } else if (size == 2) /* word access */
  99. addr = (addr | CFC_IO_MAPBASE_WORD) + (port & 0xfff);
  100. return addr;
  101. }
  102. #endif /* CONFIG_PLAT_USRV */
  103. void pcc_ioread_byte(int sock, unsigned long port, void *buf, size_t size,
  104. size_t nmemb, int flag)
  105. {
  106. u_long addr;
  107. unsigned char *bp = (unsigned char *)buf;
  108. unsigned long flags;
  109. debug(3, "m32r_cfc: pcc_ioread_byte: sock=%d, port=%#lx, buf=%p, "
  110. "size=%u, nmemb=%d, flag=%d\n",
  111. sock, port, buf, size, nmemb, flag);
  112. addr = pcc_port2addr(port, 1);
  113. if (!addr) {
  114. printk("m32r_cfc:ioread_byte null port :%#lx\n",port);
  115. return;
  116. }
  117. debug(3, "m32r_cfc: pcc_ioread_byte: addr=%#lx\n", addr);
  118. spin_lock_irqsave(&pcc_lock, flags);
  119. /* read Byte */
  120. while (nmemb--)
  121. *bp++ = readb(addr);
  122. spin_unlock_irqrestore(&pcc_lock, flags);
  123. }
  124. void pcc_ioread_word(int sock, unsigned long port, void *buf, size_t size,
  125. size_t nmemb, int flag)
  126. {
  127. u_long addr;
  128. unsigned short *bp = (unsigned short *)buf;
  129. unsigned long flags;
  130. debug(3, "m32r_cfc: pcc_ioread_word: sock=%d, port=%#lx, "
  131. "buf=%p, size=%u, nmemb=%d, flag=%d\n",
  132. sock, port, buf, size, nmemb, flag);
  133. if (size != 2)
  134. printk("m32r_cfc: ioread_word :illigal size %u : %#lx\n", size,
  135. port);
  136. if (size == 9)
  137. printk("m32r_cfc: ioread_word :insw \n");
  138. addr = pcc_port2addr(port, 2);
  139. if (!addr) {
  140. printk("m32r_cfc:ioread_word null port :%#lx\n",port);
  141. return;
  142. }
  143. debug(3, "m32r_cfc: pcc_ioread_word: addr=%#lx\n", addr);
  144. spin_lock_irqsave(&pcc_lock, flags);
  145. /* read Word */
  146. while (nmemb--)
  147. *bp++ = readw(addr);
  148. spin_unlock_irqrestore(&pcc_lock, flags);
  149. }
  150. void pcc_iowrite_byte(int sock, unsigned long port, void *buf, size_t size,
  151. size_t nmemb, int flag)
  152. {
  153. u_long addr;
  154. unsigned char *bp = (unsigned char *)buf;
  155. unsigned long flags;
  156. debug(3, "m32r_cfc: pcc_iowrite_byte: sock=%d, port=%#lx, "
  157. "buf=%p, size=%u, nmemb=%d, flag=%d\n",
  158. sock, port, buf, size, nmemb, flag);
  159. /* write Byte */
  160. addr = pcc_port2addr(port, 1);
  161. if (!addr) {
  162. printk("m32r_cfc:iowrite_byte null port:%#lx\n",port);
  163. return;
  164. }
  165. debug(3, "m32r_cfc: pcc_iowrite_byte: addr=%#lx\n", addr);
  166. spin_lock_irqsave(&pcc_lock, flags);
  167. while (nmemb--)
  168. writeb(*bp++, addr);
  169. spin_unlock_irqrestore(&pcc_lock, flags);
  170. }
  171. void pcc_iowrite_word(int sock, unsigned long port, void *buf, size_t size,
  172. size_t nmemb, int flag)
  173. {
  174. u_long addr;
  175. unsigned short *bp = (unsigned short *)buf;
  176. unsigned long flags;
  177. debug(3, "m32r_cfc: pcc_iowrite_word: sock=%d, port=%#lx, "
  178. "buf=%p, size=%u, nmemb=%d, flag=%d\n",
  179. sock, port, buf, size, nmemb, flag);
  180. if(size != 2)
  181. printk("m32r_cfc: iowrite_word :illigal size %u : %#lx\n",
  182. size, port);
  183. if(size == 9)
  184. printk("m32r_cfc: iowrite_word :outsw \n");
  185. addr = pcc_port2addr(port, 2);
  186. if (!addr) {
  187. printk("m32r_cfc:iowrite_word null addr :%#lx\n",port);
  188. return;
  189. }
  190. #if 1
  191. if (addr & 1) {
  192. printk("m32r_cfc:iowrite_word port addr (%#lx):%#lx\n", port,
  193. addr);
  194. return;
  195. }
  196. #endif
  197. debug(3, "m32r_cfc: pcc_iowrite_word: addr=%#lx\n", addr);
  198. spin_lock_irqsave(&pcc_lock, flags);
  199. while (nmemb--)
  200. writew(*bp++, addr);
  201. spin_unlock_irqrestore(&pcc_lock, flags);
  202. }
  203. /*====================================================================*/
  204. #define IS_REGISTERED 0x2000
  205. #define IS_ALIVE 0x8000
  206. typedef struct pcc_t {
  207. char *name;
  208. u_short flags;
  209. } pcc_t;
  210. static pcc_t pcc[] = {
  211. #if !defined(CONFIG_PLAT_USRV)
  212. { "m32r_cfc", 0 }, { "", 0 },
  213. #else /* CONFIG_PLAT_USRV */
  214. { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "m32r_cfc", 0 },
  215. { "m32r_cfc", 0 }, { "m32r_cfc", 0 }, { "", 0 },
  216. #endif /* CONFIG_PLAT_USRV */
  217. };
  218. static irqreturn_t pcc_interrupt(int, void *, struct pt_regs *);
  219. /*====================================================================*/
  220. static struct timer_list poll_timer;
  221. static unsigned int pcc_get(u_short sock, unsigned int reg)
  222. {
  223. unsigned int val = inw(reg);
  224. debug(3, "m32r_cfc: pcc_get: reg(0x%08x)=0x%04x\n", reg, val);
  225. return val;
  226. }
  227. static void pcc_set(u_short sock, unsigned int reg, unsigned int data)
  228. {
  229. outw(data, reg);
  230. debug(3, "m32r_cfc: pcc_set: reg(0x%08x)=0x%04x\n", reg, data);
  231. }
  232. /*======================================================================
  233. See if a card is present, powered up, in IO mode, and already
  234. bound to a (non PC Card) Linux driver. We leave these alone.
  235. We make an exception for cards that seem to be serial devices.
  236. ======================================================================*/
  237. static int __init is_alive(u_short sock)
  238. {
  239. unsigned int stat;
  240. debug(3, "m32r_cfc: is_alive:\n");
  241. printk("CF: ");
  242. stat = pcc_get(sock, (unsigned int)PLD_CFSTS);
  243. if (!stat)
  244. printk("No ");
  245. printk("Card is detected at socket %d : stat = 0x%08x\n", sock, stat);
  246. debug(3, "m32r_cfc: is_alive: sock stat is 0x%04x\n", stat);
  247. return 0;
  248. }
  249. static void add_pcc_socket(ulong base, int irq, ulong mapaddr, kio_addr_t ioaddr)
  250. {
  251. pcc_socket_t *t = &socket[pcc_sockets];
  252. debug(3, "m32r_cfc: add_pcc_socket: base=%#lx, irq=%d, "
  253. "mapaddr=%#lx, ioaddr=%08x\n",
  254. base, irq, mapaddr, ioaddr);
  255. /* add sockets */
  256. t->ioaddr = ioaddr;
  257. t->mapaddr = mapaddr;
  258. #if !defined(CONFIG_PLAT_USRV)
  259. t->base = 0;
  260. t->flags = 0;
  261. t->cs_irq1 = irq; // insert irq
  262. t->cs_irq2 = irq + 1; // eject irq
  263. #else /* CONFIG_PLAT_USRV */
  264. t->base = base;
  265. t->flags = 0;
  266. t->cs_irq1 = 0; // insert irq
  267. t->cs_irq2 = 0; // eject irq
  268. #endif /* CONFIG_PLAT_USRV */
  269. if (is_alive(pcc_sockets))
  270. t->flags |= IS_ALIVE;
  271. /* add pcc */
  272. #if !defined(CONFIG_PLAT_USRV)
  273. request_region((unsigned int)PLD_CFRSTCR, 0x20, "m32r_cfc");
  274. #else /* CONFIG_PLAT_USRV */
  275. {
  276. unsigned int reg_base;
  277. reg_base = (unsigned int)PLD_CFRSTCR;
  278. reg_base |= pcc_sockets << 8;
  279. request_region(reg_base, 0x20, "m32r_cfc");
  280. }
  281. #endif /* CONFIG_PLAT_USRV */
  282. printk(KERN_INFO " %s ", pcc[pcc_sockets].name);
  283. printk("pcc at 0x%08lx\n", t->base);
  284. /* Update socket interrupt information, capabilities */
  285. t->socket.features |= (SS_CAP_PCCARD | SS_CAP_STATIC_MAP);
  286. t->socket.map_size = M32R_PCC_MAPSIZE;
  287. t->socket.io_offset = ioaddr; /* use for io access offset */
  288. t->socket.irq_mask = 0;
  289. #if !defined(CONFIG_PLAT_USRV)
  290. t->socket.pci_irq = PLD_IRQ_CFIREQ ; /* card interrupt */
  291. #else /* CONFIG_PLAT_USRV */
  292. t->socket.pci_irq = PLD_IRQ_CF0 + pcc_sockets;
  293. #endif /* CONFIG_PLAT_USRV */
  294. #ifndef CONFIG_PLAT_USRV
  295. /* insert interrupt */
  296. request_irq(irq, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
  297. /* eject interrupt */
  298. request_irq(irq+1, pcc_interrupt, 0, "m32r_cfc", pcc_interrupt);
  299. debug(3, "m32r_cfc: enable CFMSK, RDYSEL\n");
  300. pcc_set(pcc_sockets, (unsigned int)PLD_CFIMASK, 0x01);
  301. #endif /* CONFIG_PLAT_USRV */
  302. #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
  303. pcc_set(pcc_sockets, (unsigned int)PLD_CFCR1, 0x0200);
  304. #endif
  305. pcc_sockets++;
  306. return;
  307. }
  308. /*====================================================================*/
  309. static irqreturn_t pcc_interrupt(int irq, void *dev, struct pt_regs *regs)
  310. {
  311. int i;
  312. u_int events = 0;
  313. int handled = 0;
  314. debug(3, "m32r_cfc: pcc_interrupt: irq=%d, dev=%p, regs=%p\n",
  315. irq, dev, regs);
  316. for (i = 0; i < pcc_sockets; i++) {
  317. if (socket[i].cs_irq1 != irq && socket[i].cs_irq2 != irq)
  318. continue;
  319. handled = 1;
  320. debug(3, "m32r_cfc: pcc_interrupt: socket %d irq 0x%02x ",
  321. i, irq);
  322. events |= SS_DETECT; /* insert or eject */
  323. if (events)
  324. pcmcia_parse_events(&socket[i].socket, events);
  325. }
  326. debug(3, "m32r_cfc: pcc_interrupt: done\n");
  327. return IRQ_RETVAL(handled);
  328. } /* pcc_interrupt */
  329. static void pcc_interrupt_wrapper(u_long data)
  330. {
  331. debug(3, "m32r_cfc: pcc_interrupt_wrapper:\n");
  332. pcc_interrupt(0, NULL, NULL);
  333. init_timer(&poll_timer);
  334. poll_timer.expires = jiffies + poll_interval;
  335. add_timer(&poll_timer);
  336. }
  337. /*====================================================================*/
  338. static int _pcc_get_status(u_short sock, u_int *value)
  339. {
  340. u_int status;
  341. debug(3, "m32r_cfc: _pcc_get_status:\n");
  342. status = pcc_get(sock, (unsigned int)PLD_CFSTS);
  343. *value = (status) ? SS_DETECT : 0;
  344. debug(3, "m32r_cfc: _pcc_get_status: status=0x%08x\n", status);
  345. #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT)
  346. if ( status ) {
  347. /* enable CF power */
  348. status = inw((unsigned int)PLD_CPCR);
  349. if (!(status & PLD_CPCR_CF)) {
  350. debug(3, "m32r_cfc: _pcc_get_status: "
  351. "power on (CPCR=0x%08x)\n", status);
  352. status |= PLD_CPCR_CF;
  353. outw(status, (unsigned int)PLD_CPCR);
  354. udelay(100);
  355. }
  356. *value |= SS_POWERON;
  357. pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);/* enable buffer */
  358. udelay(100);
  359. *value |= SS_READY; /* always ready */
  360. *value |= SS_3VCARD;
  361. } else {
  362. /* disable CF power */
  363. status = inw((unsigned int)PLD_CPCR);
  364. status &= ~PLD_CPCR_CF;
  365. outw(status, (unsigned int)PLD_CPCR);
  366. udelay(100);
  367. debug(3, "m32r_cfc: _pcc_get_status: "
  368. "power off (CPCR=0x%08x)\n", status);
  369. }
  370. #elif defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
  371. if ( status ) {
  372. status = pcc_get(sock, (unsigned int)PLD_CPCR);
  373. if (status == 0) { /* power off */
  374. pcc_set(sock, (unsigned int)PLD_CPCR, 1);
  375. pcc_set(sock, (unsigned int)PLD_CFBUFCR,0); /* force buffer off for ZA-36 */
  376. udelay(50);
  377. }
  378. *value |= SS_POWERON;
  379. pcc_set(sock, (unsigned int)PLD_CFBUFCR,0);
  380. udelay(50);
  381. pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0101);
  382. udelay(25); /* for IDE reset */
  383. pcc_set(sock, (unsigned int)PLD_CFRSTCR, 0x0100);
  384. mdelay(2); /* for IDE reset */
  385. *value |= SS_READY;
  386. *value |= SS_3VCARD;
  387. } else {
  388. /* disable CF power */
  389. pcc_set(sock, (unsigned int)PLD_CPCR, 0);
  390. udelay(100);
  391. debug(3, "m32r_cfc: _pcc_get_status: "
  392. "power off (CPCR=0x%08x)\n", status);
  393. }
  394. #else
  395. #error no platform configuration
  396. #endif
  397. debug(3, "m32r_cfc: _pcc_get_status: GetStatus(%d) = %#4.4x\n",
  398. sock, *value);
  399. return 0;
  400. } /* _get_status */
  401. /*====================================================================*/
  402. static int _pcc_get_socket(u_short sock, socket_state_t *state)
  403. {
  404. // pcc_socket_t *t = &socket[sock];
  405. state->flags = 0;
  406. state->csc_mask = SS_DETECT;
  407. state->csc_mask |= SS_READY;
  408. state->io_irq = 0;
  409. state->Vcc = 33; /* 3.3V fixed */
  410. state->Vpp = 33;
  411. debug(3, "m32r_cfc: GetSocket(%d) = flags %#3.3x, Vcc %d, Vpp %d, "
  412. "io_irq %d, csc_mask %#2.2x\n", sock, state->flags,
  413. state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
  414. return 0;
  415. } /* _get_socket */
  416. /*====================================================================*/
  417. static int _pcc_set_socket(u_short sock, socket_state_t *state)
  418. {
  419. debug(3, "m32r_cfc: SetSocket(%d, flags %#3.3x, Vcc %d, Vpp %d, "
  420. "io_irq %d, csc_mask %#2.2x)\n", sock, state->flags,
  421. state->Vcc, state->Vpp, state->io_irq, state->csc_mask);
  422. #if defined(CONFIG_PLAT_M32700UT) || defined(CONFIG_PLAT_USRV) || defined(CONFIG_PLAT_OPSPUT) || defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
  423. if (state->Vcc) {
  424. if ((state->Vcc != 50) && (state->Vcc != 33))
  425. return -EINVAL;
  426. /* accept 5V and 3.3V */
  427. }
  428. #endif
  429. if (state->flags & SS_RESET) {
  430. debug(3, ":RESET\n");
  431. pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x101);
  432. }else{
  433. pcc_set(sock,(unsigned int)PLD_CFRSTCR,0x100);
  434. }
  435. if (state->flags & SS_OUTPUT_ENA){
  436. debug(3, ":OUTPUT_ENA\n");
  437. /* bit clear */
  438. pcc_set(sock,(unsigned int)PLD_CFBUFCR,0);
  439. } else {
  440. pcc_set(sock,(unsigned int)PLD_CFBUFCR,1);
  441. }
  442. #ifdef DEBUG
  443. if(state->flags & SS_IOCARD){
  444. debug(3, ":IOCARD");
  445. }
  446. if (state->flags & SS_PWR_AUTO) {
  447. debug(3, ":PWR_AUTO");
  448. }
  449. if (state->csc_mask & SS_DETECT)
  450. debug(3, ":csc-SS_DETECT");
  451. if (state->flags & SS_IOCARD) {
  452. if (state->csc_mask & SS_STSCHG)
  453. debug(3, ":STSCHG");
  454. } else {
  455. if (state->csc_mask & SS_BATDEAD)
  456. debug(3, ":BATDEAD");
  457. if (state->csc_mask & SS_BATWARN)
  458. debug(3, ":BATWARN");
  459. if (state->csc_mask & SS_READY)
  460. debug(3, ":READY");
  461. }
  462. debug(3, "\n");
  463. #endif
  464. return 0;
  465. } /* _set_socket */
  466. /*====================================================================*/
  467. static int _pcc_set_io_map(u_short sock, struct pccard_io_map *io)
  468. {
  469. u_char map;
  470. debug(3, "m32r_cfc: SetIOMap(%d, %d, %#2.2x, %d ns, "
  471. "%#lx-%#lx)\n", sock, io->map, io->flags,
  472. io->speed, io->start, io->stop);
  473. map = io->map;
  474. return 0;
  475. } /* _set_io_map */
  476. /*====================================================================*/
  477. static int _pcc_set_mem_map(u_short sock, struct pccard_mem_map *mem)
  478. {
  479. u_char map = mem->map;
  480. u_long addr;
  481. pcc_socket_t *t = &socket[sock];
  482. debug(3, "m32r_cfc: SetMemMap(%d, %d, %#2.2x, %d ns, "
  483. "%#lx, %#x)\n", sock, map, mem->flags,
  484. mem->speed, mem->static_start, mem->card_start);
  485. /*
  486. * sanity check
  487. */
  488. if ((map > MAX_WIN) || (mem->card_start > 0x3ffffff)){
  489. return -EINVAL;
  490. }
  491. /*
  492. * de-activate
  493. */
  494. if ((mem->flags & MAP_ACTIVE) == 0) {
  495. t->current_space = as_none;
  496. return 0;
  497. }
  498. /*
  499. * Set mode
  500. */
  501. if (mem->flags & MAP_ATTRIB) {
  502. t->current_space = as_attr;
  503. } else {
  504. t->current_space = as_comm;
  505. }
  506. /*
  507. * Set address
  508. */
  509. addr = t->mapaddr + (mem->card_start & M32R_PCC_MAPMASK);
  510. mem->static_start = addr + mem->card_start;
  511. return 0;
  512. } /* _set_mem_map */
  513. #if 0 /* driver model ordering issue */
  514. /*======================================================================
  515. Routines for accessing socket information and register dumps via
  516. /proc/bus/pccard/...
  517. ======================================================================*/
  518. static ssize_t show_info(struct class_device *class_dev, char *buf)
  519. {
  520. pcc_socket_t *s = container_of(class_dev, struct pcc_socket,
  521. socket.dev);
  522. return sprintf(buf, "type: %s\nbase addr: 0x%08lx\n",
  523. pcc[s->type].name, s->base);
  524. }
  525. static ssize_t show_exca(struct class_device *class_dev, char *buf)
  526. {
  527. /* FIXME */
  528. return 0;
  529. }
  530. static CLASS_DEVICE_ATTR(info, S_IRUGO, show_info, NULL);
  531. static CLASS_DEVICE_ATTR(exca, S_IRUGO, show_exca, NULL);
  532. #endif
  533. /*====================================================================*/
  534. /* this is horribly ugly... proper locking needs to be done here at
  535. * some time... */
  536. #define LOCKED(x) do { \
  537. int retval; \
  538. unsigned long flags; \
  539. spin_lock_irqsave(&pcc_lock, flags); \
  540. retval = x; \
  541. spin_unlock_irqrestore(&pcc_lock, flags); \
  542. return retval; \
  543. } while (0)
  544. static int pcc_get_status(struct pcmcia_socket *s, u_int *value)
  545. {
  546. unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
  547. if (socket[sock].flags & IS_ALIVE) {
  548. debug(3, "m32r_cfc: pcc_get_status: sock(%d) -EINVAL\n", sock);
  549. *value = 0;
  550. return -EINVAL;
  551. }
  552. debug(3, "m32r_cfc: pcc_get_status: sock(%d)\n", sock);
  553. LOCKED(_pcc_get_status(sock, value));
  554. }
  555. static int pcc_get_socket(struct pcmcia_socket *s, socket_state_t *state)
  556. {
  557. unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
  558. if (socket[sock].flags & IS_ALIVE) {
  559. debug(3, "m32r_cfc: pcc_get_socket: sock(%d) -EINVAL\n", sock);
  560. return -EINVAL;
  561. }
  562. debug(3, "m32r_cfc: pcc_get_socket: sock(%d)\n", sock);
  563. LOCKED(_pcc_get_socket(sock, state));
  564. }
  565. static int pcc_set_socket(struct pcmcia_socket *s, socket_state_t *state)
  566. {
  567. unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
  568. if (socket[sock].flags & IS_ALIVE) {
  569. debug(3, "m32r_cfc: pcc_set_socket: sock(%d) -EINVAL\n", sock);
  570. return -EINVAL;
  571. }
  572. debug(3, "m32r_cfc: pcc_set_socket: sock(%d)\n", sock);
  573. LOCKED(_pcc_set_socket(sock, state));
  574. }
  575. static int pcc_set_io_map(struct pcmcia_socket *s, struct pccard_io_map *io)
  576. {
  577. unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
  578. if (socket[sock].flags & IS_ALIVE) {
  579. debug(3, "m32r_cfc: pcc_set_io_map: sock(%d) -EINVAL\n", sock);
  580. return -EINVAL;
  581. }
  582. debug(3, "m32r_cfc: pcc_set_io_map: sock(%d)\n", sock);
  583. LOCKED(_pcc_set_io_map(sock, io));
  584. }
  585. static int pcc_set_mem_map(struct pcmcia_socket *s, struct pccard_mem_map *mem)
  586. {
  587. unsigned int sock = container_of(s, struct pcc_socket, socket)->number;
  588. if (socket[sock].flags & IS_ALIVE) {
  589. debug(3, "m32r_cfc: pcc_set_mem_map: sock(%d) -EINVAL\n", sock);
  590. return -EINVAL;
  591. }
  592. debug(3, "m32r_cfc: pcc_set_mem_map: sock(%d)\n", sock);
  593. LOCKED(_pcc_set_mem_map(sock, mem));
  594. }
  595. static int pcc_init(struct pcmcia_socket *s)
  596. {
  597. debug(3, "m32r_cfc: pcc_init()\n");
  598. return 0;
  599. }
  600. static struct pccard_operations pcc_operations = {
  601. .init = pcc_init,
  602. .get_status = pcc_get_status,
  603. .get_socket = pcc_get_socket,
  604. .set_socket = pcc_set_socket,
  605. .set_io_map = pcc_set_io_map,
  606. .set_mem_map = pcc_set_mem_map,
  607. };
  608. /*====================================================================*/
  609. static int m32r_pcc_suspend(struct device *dev, pm_message_t state, u32 level)
  610. {
  611. int ret = 0;
  612. if (level == SUSPEND_SAVE_STATE)
  613. ret = pcmcia_socket_dev_suspend(dev, state);
  614. return ret;
  615. }
  616. static int m32r_pcc_resume(struct device *dev, u32 level)
  617. {
  618. int ret = 0;
  619. if (level == RESUME_RESTORE_STATE)
  620. ret = pcmcia_socket_dev_resume(dev);
  621. return ret;
  622. }
  623. static struct device_driver pcc_driver = {
  624. .name = "cfc",
  625. .bus = &platform_bus_type,
  626. .suspend = m32r_pcc_suspend,
  627. .resume = m32r_pcc_resume,
  628. };
  629. static struct platform_device pcc_device = {
  630. .name = "cfc",
  631. .id = 0,
  632. };
  633. /*====================================================================*/
  634. static int __init init_m32r_pcc(void)
  635. {
  636. int i, ret;
  637. ret = driver_register(&pcc_driver);
  638. if (ret)
  639. return ret;
  640. ret = platform_device_register(&pcc_device);
  641. if (ret){
  642. driver_unregister(&pcc_driver);
  643. return ret;
  644. }
  645. #if defined(CONFIG_PLAT_MAPPI2) || defined(CONFIG_PLAT_MAPPI3)
  646. pcc_set(0, (unsigned int)PLD_CFCR0, 0x0f0f);
  647. pcc_set(0, (unsigned int)PLD_CFCR1, 0x0200);
  648. #endif
  649. pcc_sockets = 0;
  650. #if !defined(CONFIG_PLAT_USRV)
  651. add_pcc_socket(M32R_PCC0_BASE, PLD_IRQ_CFC_INSERT, CFC_ATTR_MAPBASE,
  652. CFC_IOPORT_BASE);
  653. #else /* CONFIG_PLAT_USRV */
  654. {
  655. ulong base, mapaddr;
  656. kio_addr_t ioaddr;
  657. for (i = 0 ; i < M32R_MAX_PCC ; i++) {
  658. base = (ulong)PLD_CFRSTCR;
  659. base = base | (i << 8);
  660. ioaddr = (i + 1) << 12;
  661. mapaddr = CFC_ATTR_MAPBASE | (i << 20);
  662. add_pcc_socket(base, 0, mapaddr, ioaddr);
  663. }
  664. }
  665. #endif /* CONFIG_PLAT_USRV */
  666. if (pcc_sockets == 0) {
  667. printk("socket is not found.\n");
  668. platform_device_unregister(&pcc_device);
  669. driver_unregister(&pcc_driver);
  670. return -ENODEV;
  671. }
  672. /* Set up interrupt handler(s) */
  673. for (i = 0 ; i < pcc_sockets ; i++) {
  674. socket[i].socket.dev.dev = &pcc_device.dev;
  675. socket[i].socket.ops = &pcc_operations;
  676. socket[i].socket.resource_ops = &pccard_nonstatic_ops;
  677. socket[i].socket.owner = THIS_MODULE;
  678. socket[i].number = i;
  679. ret = pcmcia_register_socket(&socket[i].socket);
  680. if (!ret)
  681. socket[i].flags |= IS_REGISTERED;
  682. #if 0 /* driver model ordering issue */
  683. class_device_create_file(&socket[i].socket.dev,
  684. &class_device_attr_info);
  685. class_device_create_file(&socket[i].socket.dev,
  686. &class_device_attr_exca);
  687. #endif
  688. }
  689. /* Finally, schedule a polling interrupt */
  690. if (poll_interval != 0) {
  691. poll_timer.function = pcc_interrupt_wrapper;
  692. poll_timer.data = 0;
  693. init_timer(&poll_timer);
  694. poll_timer.expires = jiffies + poll_interval;
  695. add_timer(&poll_timer);
  696. }
  697. return 0;
  698. } /* init_m32r_pcc */
  699. static void __exit exit_m32r_pcc(void)
  700. {
  701. int i;
  702. for (i = 0; i < pcc_sockets; i++)
  703. if (socket[i].flags & IS_REGISTERED)
  704. pcmcia_unregister_socket(&socket[i].socket);
  705. platform_device_unregister(&pcc_device);
  706. if (poll_interval != 0)
  707. del_timer_sync(&poll_timer);
  708. driver_unregister(&pcc_driver);
  709. } /* exit_m32r_pcc */
  710. module_init(init_m32r_pcc);
  711. module_exit(exit_m32r_pcc);
  712. MODULE_LICENSE("Dual MPL/GPL");
  713. /*====================================================================*/