m32r_cfc.c 20 KB

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