vrc4173_cardu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * FILE NAME
  3. * drivers/pcmcia/vrc4173_cardu.c
  4. *
  5. * BRIEF MODULE DESCRIPTION
  6. * NEC VRC4173 CARDU driver for Socket Services
  7. * (This device doesn't support CardBus. it is supporting only 16bit PC Card.)
  8. *
  9. * Copyright 2002,2003 Yoichi Yuasa <yuasa@linux-mips.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  24. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/pci.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/types.h>
  36. #include <asm/io.h>
  37. #include <pcmcia/ss.h>
  38. #include "vrc4173_cardu.h"
  39. MODULE_DESCRIPTION("NEC VRC4173 CARDU driver for Socket Services");
  40. MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  41. MODULE_LICENSE("GPL");
  42. static int vrc4173_cardu_slots;
  43. static vrc4173_socket_t cardu_sockets[CARDU_MAX_SOCKETS];
  44. extern struct socket_info_t *pcmcia_register_socket (int slot,
  45. struct pccard_operations *vtable,
  46. int use_bus_pm);
  47. extern void pcmcia_unregister_socket(struct socket_info_t *s);
  48. static inline uint8_t exca_readb(vrc4173_socket_t *socket, uint16_t offset)
  49. {
  50. return readb(socket->base + EXCA_REGS_BASE + offset);
  51. }
  52. static inline uint16_t exca_readw(vrc4173_socket_t *socket, uint16_t offset)
  53. {
  54. uint16_t val;
  55. val = readb(socket->base + EXCA_REGS_BASE + offset);
  56. val |= (u16)readb(socket->base + EXCA_REGS_BASE + offset + 1) << 8;
  57. return val;
  58. }
  59. static inline void exca_writeb(vrc4173_socket_t *socket, uint16_t offset, uint8_t val)
  60. {
  61. writeb(val, socket->base + EXCA_REGS_BASE + offset);
  62. }
  63. static inline void exca_writew(vrc4173_socket_t *socket, uint8_t offset, uint16_t val)
  64. {
  65. writeb((u8)val, socket->base + EXCA_REGS_BASE + offset);
  66. writeb((u8)(val >> 8), socket->base + EXCA_REGS_BASE + offset + 1);
  67. }
  68. static inline uint32_t cardbus_socket_readl(vrc4173_socket_t *socket, u16 offset)
  69. {
  70. return readl(socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
  71. }
  72. static inline void cardbus_socket_writel(vrc4173_socket_t *socket, u16 offset, uint32_t val)
  73. {
  74. writel(val, socket->base + CARDBUS_SOCKET_REGS_BASE + offset);
  75. }
  76. static void cardu_pciregs_init(struct pci_dev *dev)
  77. {
  78. u32 syscnt;
  79. u16 brgcnt;
  80. u8 devcnt;
  81. pci_write_config_dword(dev, 0x1c, 0x10000000);
  82. pci_write_config_dword(dev, 0x20, 0x17fff000);
  83. pci_write_config_dword(dev, 0x2c, 0);
  84. pci_write_config_dword(dev, 0x30, 0xfffc);
  85. pci_read_config_word(dev, BRGCNT, &brgcnt);
  86. brgcnt &= ~IREQ_INT;
  87. pci_write_config_word(dev, BRGCNT, brgcnt);
  88. pci_read_config_dword(dev, SYSCNT, &syscnt);
  89. syscnt &= ~(BAD_VCC_REQ_DISB|PCPCI_EN|CH_ASSIGN_MASK|SUB_ID_WR_EN|PCI_CLK_RIN);
  90. syscnt |= (CH_ASSIGN_NODMA|ASYN_INT_MODE);
  91. pci_write_config_dword(dev, SYSCNT, syscnt);
  92. pci_read_config_byte(dev, DEVCNT, &devcnt);
  93. devcnt &= ~(ZOOM_VIDEO_EN|SR_PCI_INT_SEL_MASK|PCI_INT_MODE|IRQ_MODE);
  94. devcnt |= (SR_PCI_INT_SEL_NONE|IFG);
  95. pci_write_config_byte(dev, DEVCNT, devcnt);
  96. pci_write_config_byte(dev, CHIPCNT, S_PREF_DISB);
  97. pci_write_config_byte(dev, SERRDIS, 0);
  98. }
  99. static int cardu_init(unsigned int slot)
  100. {
  101. vrc4173_socket_t *socket = &cardu_sockets[slot];
  102. cardu_pciregs_init(socket->dev);
  103. /* CARD_SC bits are cleared by reading CARD_SC. */
  104. exca_writeb(socket, GLO_CNT, 0);
  105. socket->cap.features |= SS_CAP_PCCARD | SS_CAP_PAGE_REGS;
  106. socket->cap.irq_mask = 0;
  107. socket->cap.map_size = 0x1000;
  108. socket->cap.pci_irq = socket->dev->irq;
  109. socket->events = 0;
  110. spin_lock_init(socket->event_lock);
  111. /* Enable PC Card status interrupts */
  112. exca_writeb(socket, CARD_SCI, CARD_DT_EN|RDY_EN|BAT_WAR_EN|BAT_DEAD_EN);
  113. return 0;
  114. }
  115. static int cardu_register_callback(unsigned int sock,
  116. void (*handler)(void *, unsigned int),
  117. void * info)
  118. {
  119. vrc4173_socket_t *socket = &cardu_sockets[sock];
  120. socket->handler = handler;
  121. socket->info = info;
  122. return 0;
  123. }
  124. static int cardu_inquire_socket(unsigned int sock, socket_cap_t *cap)
  125. {
  126. vrc4173_socket_t *socket = &cardu_sockets[sock];
  127. *cap = socket->cap;
  128. return 0;
  129. }
  130. static int cardu_get_status(unsigned int sock, u_int *value)
  131. {
  132. vrc4173_socket_t *socket = &cardu_sockets[sock];
  133. uint32_t state;
  134. uint8_t status;
  135. u_int val = 0;
  136. status = exca_readb(socket, IF_STATUS);
  137. if (status & CARD_PWR) val |= SS_POWERON;
  138. if (status & READY) val |= SS_READY;
  139. if (status & CARD_WP) val |= SS_WRPROT;
  140. if ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2))
  141. val |= SS_DETECT;
  142. if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
  143. if (status & STSCHG) val |= SS_STSCHG;
  144. } else {
  145. status &= BV_DETECT_MASK;
  146. if (status != BV_DETECT_GOOD) {
  147. if (status == BV_DETECT_WARN) val |= SS_BATWARN;
  148. else val |= SS_BATDEAD;
  149. }
  150. }
  151. state = cardbus_socket_readl(socket, SKT_PRE_STATE);
  152. if (state & VOL_3V_CARD_DT) val |= SS_3VCARD;
  153. if (state & VOL_XV_CARD_DT) val |= SS_XVCARD;
  154. if (state & CB_CARD_DT) val |= SS_CARDBUS;
  155. if (!(state &
  156. (VOL_YV_CARD_DT|VOL_XV_CARD_DT|VOL_3V_CARD_DT|VOL_5V_CARD_DT|CCD20|CCD10)))
  157. val |= SS_PENDING;
  158. *value = val;
  159. return 0;
  160. }
  161. static inline uint8_t set_Vcc_value(u_char Vcc)
  162. {
  163. switch (Vcc) {
  164. case 33:
  165. return VCC_3V;
  166. case 50:
  167. return VCC_5V;
  168. }
  169. return VCC_0V;
  170. }
  171. static inline uint8_t set_Vpp_value(u_char Vpp)
  172. {
  173. switch (Vpp) {
  174. case 33:
  175. case 50:
  176. return VPP_VCC;
  177. case 120:
  178. return VPP_12V;
  179. }
  180. return VPP_0V;
  181. }
  182. static int cardu_set_socket(unsigned int sock, socket_state_t *state)
  183. {
  184. vrc4173_socket_t *socket = &cardu_sockets[sock];
  185. uint8_t val;
  186. if (((state->Vpp == 33) || (state->Vpp == 50)) && (state->Vpp != state->Vcc))
  187. return -EINVAL;
  188. val = set_Vcc_value(state->Vcc);
  189. val |= set_Vpp_value(state->Vpp);
  190. if (state->flags & SS_OUTPUT_ENA) val |= CARD_OUT_EN;
  191. exca_writeb(socket, PWR_CNT, val);
  192. val = exca_readb(socket, INT_GEN_CNT) & CARD_REST0;
  193. if (state->flags & SS_RESET) val &= ~CARD_REST0;
  194. else val |= CARD_REST0;
  195. if (state->flags & SS_IOCARD) val |= CARD_TYPE_IO;
  196. exca_writeb(socket, INT_GEN_CNT, val);
  197. return 0;
  198. }
  199. static int cardu_get_io_map(unsigned int sock, struct pccard_io_map *io)
  200. {
  201. vrc4173_socket_t *socket = &cardu_sockets[sock];
  202. uint8_t ioctl, window;
  203. u_char map;
  204. map = io->map;
  205. if (map > 1)
  206. return -EINVAL;
  207. io->start = exca_readw(socket, IO_WIN_SA(map));
  208. io->stop = exca_readw(socket, IO_WIN_EA(map));
  209. ioctl = exca_readb(socket, IO_WIN_CNT);
  210. window = exca_readb(socket, ADR_WIN_EN);
  211. io->flags = (window & IO_WIN_EN(map)) ? MAP_ACTIVE : 0;
  212. if (ioctl & IO_WIN_DATA_AUTOSZ(map))
  213. io->flags |= MAP_AUTOSZ;
  214. else if (ioctl & IO_WIN_DATA_16BIT(map))
  215. io->flags |= MAP_16BIT;
  216. return 0;
  217. }
  218. static int cardu_set_io_map(unsigned int sock, struct pccard_io_map *io)
  219. {
  220. vrc4173_socket_t *socket = &cardu_sockets[sock];
  221. uint16_t ioctl;
  222. uint8_t window, enable;
  223. u_char map;
  224. map = io->map;
  225. if (map > 1)
  226. return -EINVAL;
  227. window = exca_readb(socket, ADR_WIN_EN);
  228. enable = IO_WIN_EN(map);
  229. if (window & enable) {
  230. window &= ~enable;
  231. exca_writeb(socket, ADR_WIN_EN, window);
  232. }
  233. exca_writew(socket, IO_WIN_SA(map), io->start);
  234. exca_writew(socket, IO_WIN_EA(map), io->stop);
  235. ioctl = exca_readb(socket, IO_WIN_CNT) & ~IO_WIN_CNT_MASK(map);
  236. if (io->flags & MAP_AUTOSZ) ioctl |= IO_WIN_DATA_AUTOSZ(map);
  237. else if (io->flags & MAP_16BIT) ioctl |= IO_WIN_DATA_16BIT(map);
  238. exca_writeb(socket, IO_WIN_CNT, ioctl);
  239. if (io->flags & MAP_ACTIVE)
  240. exca_writeb(socket, ADR_WIN_EN, window | enable);
  241. return 0;
  242. }
  243. static int cardu_get_mem_map(unsigned int sock, struct pccard_mem_map *mem)
  244. {
  245. vrc4173_socket_t *socket = &cardu_sockets[sock];
  246. uint32_t start, stop, offset, page;
  247. uint8_t window;
  248. u_char map;
  249. map = mem->map;
  250. if (map > 4)
  251. return -EINVAL;
  252. window = exca_readb(socket, ADR_WIN_EN);
  253. mem->flags = (window & MEM_WIN_EN(map)) ? MAP_ACTIVE : 0;
  254. start = exca_readw(socket, MEM_WIN_SA(map));
  255. mem->flags |= (start & MEM_WIN_DSIZE) ? MAP_16BIT : 0;
  256. start = (start & 0x0fff) << 12;
  257. stop = exca_readw(socket, MEM_WIN_EA(map));
  258. stop = ((stop & 0x0fff) << 12) + 0x0fff;
  259. offset = exca_readw(socket, MEM_WIN_OA(map));
  260. mem->flags |= (offset & MEM_WIN_WP) ? MAP_WRPROT : 0;
  261. mem->flags |= (offset & MEM_WIN_REGSET) ? MAP_ATTRIB : 0;
  262. offset = ((offset & 0x3fff) << 12) + start;
  263. mem->card_start = offset & 0x03ffffff;
  264. page = exca_readb(socket, MEM_WIN_SAU(map)) << 24;
  265. mem->sys_start = start + page;
  266. mem->sys_stop = start + page;
  267. return 0;
  268. }
  269. static int cardu_set_mem_map(unsigned int sock, struct pccard_mem_map *mem)
  270. {
  271. vrc4173_socket_t *socket = &cardu_sockets[sock];
  272. uint16_t value;
  273. uint8_t window, enable;
  274. u_long sys_start, sys_stop, card_start;
  275. u_char map;
  276. map = mem->map;
  277. sys_start = mem->sys_start;
  278. sys_stop = mem->sys_stop;
  279. card_start = mem->card_start;
  280. if (map > 4 || sys_start > sys_stop || ((sys_start ^ sys_stop) >> 24) ||
  281. (card_start >> 26))
  282. return -EINVAL;
  283. window = exca_readb(socket, ADR_WIN_EN);
  284. enable = MEM_WIN_EN(map);
  285. if (window & enable) {
  286. window &= ~enable;
  287. exca_writeb(socket, ADR_WIN_EN, window);
  288. }
  289. exca_writeb(socket, MEM_WIN_SAU(map), sys_start >> 24);
  290. value = (sys_start >> 12) & 0x0fff;
  291. if (mem->flags & MAP_16BIT) value |= MEM_WIN_DSIZE;
  292. exca_writew(socket, MEM_WIN_SA(map), value);
  293. value = (sys_stop >> 12) & 0x0fff;
  294. exca_writew(socket, MEM_WIN_EA(map), value);
  295. value = ((card_start - sys_start) >> 12) & 0x3fff;
  296. if (mem->flags & MAP_WRPROT) value |= MEM_WIN_WP;
  297. if (mem->flags & MAP_ATTRIB) value |= MEM_WIN_REGSET;
  298. exca_writew(socket, MEM_WIN_OA(map), value);
  299. if (mem->flags & MAP_ACTIVE)
  300. exca_writeb(socket, ADR_WIN_EN, window | enable);
  301. return 0;
  302. }
  303. static void cardu_proc_setup(unsigned int sock, struct proc_dir_entry *base)
  304. {
  305. }
  306. static struct pccard_operations cardu_operations = {
  307. .init = cardu_init,
  308. .register_callback = cardu_register_callback,
  309. .inquire_socket = cardu_inquire_socket,
  310. .get_status = cardu_get_status,
  311. .set_socket = cardu_set_socket,
  312. .get_io_map = cardu_get_io_map,
  313. .set_io_map = cardu_set_io_map,
  314. .get_mem_map = cardu_get_mem_map,
  315. .set_mem_map = cardu_set_mem_map,
  316. .proc_setup = cardu_proc_setup,
  317. };
  318. static void cardu_bh(void *data)
  319. {
  320. vrc4173_socket_t *socket = (vrc4173_socket_t *)data;
  321. uint16_t events;
  322. spin_lock_irq(&socket->event_lock);
  323. events = socket->events;
  324. socket->events = 0;
  325. spin_unlock_irq(&socket->event_lock);
  326. if (socket->handler)
  327. socket->handler(socket->info, events);
  328. }
  329. static uint16_t get_events(vrc4173_socket_t *socket)
  330. {
  331. uint16_t events = 0;
  332. uint8_t csc, status;
  333. status = exca_readb(socket, IF_STATUS);
  334. csc = exca_readb(socket, CARD_SC);
  335. if ((csc & CARD_DT_CHG) &&
  336. ((status & (CARD_DETECT1|CARD_DETECT2)) == (CARD_DETECT1|CARD_DETECT2)))
  337. events |= SS_DETECT;
  338. if ((csc & RDY_CHG) && (status & READY))
  339. events |= SS_READY;
  340. if (exca_readb(socket, INT_GEN_CNT) & CARD_TYPE_IO) {
  341. if ((csc & BAT_DEAD_ST_CHG) && (status & STSCHG))
  342. events |= SS_STSCHG;
  343. } else {
  344. if (csc & (BAT_WAR_CHG|BAT_DEAD_ST_CHG)) {
  345. if ((status & BV_DETECT_MASK) != BV_DETECT_GOOD) {
  346. if (status == BV_DETECT_WARN) events |= SS_BATWARN;
  347. else events |= SS_BATDEAD;
  348. }
  349. }
  350. }
  351. return events;
  352. }
  353. static void cardu_interrupt(int irq, void *dev_id)
  354. {
  355. vrc4173_socket_t *socket = (vrc4173_socket_t *)dev_id;
  356. uint16_t events;
  357. INIT_WORK(&socket->tq_work, cardu_bh, socket);
  358. events = get_events(socket);
  359. if (events) {
  360. spin_lock(&socket->event_lock);
  361. socket->events |= events;
  362. spin_unlock(&socket->event_lock);
  363. schedule_work(&socket->tq_work);
  364. }
  365. }
  366. static int vrc4173_cardu_probe(struct pci_dev *dev,
  367. const struct pci_device_id *ent)
  368. {
  369. vrc4173_socket_t *socket;
  370. unsigned long start, len, flags;
  371. int slot, err, ret;
  372. slot = vrc4173_cardu_slots++;
  373. socket = &cardu_sockets[slot];
  374. if (socket->noprobe != 0)
  375. return -EBUSY;
  376. sprintf(socket->name, "NEC VRC4173 CARDU%1d", slot+1);
  377. if ((err = pci_enable_device(dev)) < 0)
  378. return err;
  379. start = pci_resource_start(dev, 0);
  380. if (start == 0) {
  381. ret = -ENODEV;
  382. goto disable;
  383. }
  384. len = pci_resource_len(dev, 0);
  385. if (len == 0) {
  386. ret = -ENODEV;
  387. goto disable;
  388. }
  389. flags = pci_resource_flags(dev, 0);
  390. if ((flags & IORESOURCE_MEM) == 0) {
  391. ret = -EBUSY;
  392. goto disable;
  393. }
  394. err = pci_request_regions(dev, socket->name);
  395. if (err < 0) {
  396. ret = err;
  397. goto disable;
  398. }
  399. socket->base = ioremap(start, len);
  400. if (socket->base == NULL) {
  401. ret = -ENODEV;
  402. goto release;
  403. }
  404. socket->dev = dev;
  405. socket->pcmcia_socket = pcmcia_register_socket(slot, &cardu_operations, 1);
  406. if (socket->pcmcia_socket == NULL) {
  407. ret = -ENOMEM;
  408. goto unmap;
  409. }
  410. if (request_irq(dev->irq, cardu_interrupt, IRQF_SHARED, socket->name, socket) < 0) {
  411. ret = -EBUSY;
  412. goto unregister;
  413. }
  414. printk(KERN_INFO "%s at %#08lx, IRQ %d\n", socket->name, start, dev->irq);
  415. return 0;
  416. unregister:
  417. pcmcia_unregister_socket(socket->pcmcia_socket);
  418. socket->pcmcia_socket = NULL;
  419. unmap:
  420. iounmap(socket->base);
  421. socket->base = NULL;
  422. release:
  423. pci_release_regions(dev);
  424. disable:
  425. pci_disable_device(dev);
  426. return ret;
  427. }
  428. static int vrc4173_cardu_setup(char *options)
  429. {
  430. if (options == NULL || *options == '\0')
  431. return 1;
  432. if (strncmp(options, "cardu1:", 7) == 0) {
  433. options += 7;
  434. if (*options != '\0') {
  435. if (strncmp(options, "noprobe", 7) == 0) {
  436. cardu_sockets[CARDU1].noprobe = 1;
  437. options += 7;
  438. }
  439. if (*options != ',')
  440. return 1;
  441. } else
  442. return 1;
  443. }
  444. if (strncmp(options, "cardu2:", 7) == 0) {
  445. options += 7;
  446. if ((*options != '\0') && (strncmp(options, "noprobe", 7) == 0))
  447. cardu_sockets[CARDU2].noprobe = 1;
  448. }
  449. return 1;
  450. }
  451. __setup("vrc4173_cardu=", vrc4173_cardu_setup);
  452. static const struct pci_device_id vrc4173_cardu_id_table[] = {
  453. { PCI_DEVICE(PCI_VENDOR_ID_NEC, PCI_DEVICE_ID_NEC_NAPCCARD) },
  454. {0, }
  455. };
  456. static struct pci_driver vrc4173_cardu_driver = {
  457. .name = "NEC VRC4173 CARDU",
  458. .probe = vrc4173_cardu_probe,
  459. .id_table = vrc4173_cardu_id_table,
  460. };
  461. static int vrc4173_cardu_init(void)
  462. {
  463. vrc4173_cardu_slots = 0;
  464. return pci_register_driver(&vrc4173_cardu_driver);
  465. }
  466. static void vrc4173_cardu_exit(void)
  467. {
  468. pci_unregister_driver(&vrc4173_cardu_driver);
  469. }
  470. module_init(vrc4173_cardu_init);
  471. module_exit(vrc4173_cardu_exit);
  472. MODULE_DEVICE_TABLE(pci, vrc4173_cardu_id_table);