vr41xx_giu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Driver for NEC VR4100 series General-purpose I/O Unit.
  3. *
  4. * Copyright (C) 2002 MontaVista Software Inc.
  5. * Author: Yoichi Yuasa <source@mvista.com>
  6. * Copyright (C) 2003-2009 Yoichi Yuasa <yuasa@linux-mips.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/irq.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/types.h>
  34. #include <asm/vr41xx/giu.h>
  35. #include <asm/vr41xx/irq.h>
  36. #include <asm/vr41xx/vr41xx.h>
  37. MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  38. MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
  39. MODULE_LICENSE("GPL");
  40. #define GIUIOSELL 0x00
  41. #define GIUIOSELH 0x02
  42. #define GIUPIODL 0x04
  43. #define GIUPIODH 0x06
  44. #define GIUINTSTATL 0x08
  45. #define GIUINTSTATH 0x0a
  46. #define GIUINTENL 0x0c
  47. #define GIUINTENH 0x0e
  48. #define GIUINTTYPL 0x10
  49. #define GIUINTTYPH 0x12
  50. #define GIUINTALSELL 0x14
  51. #define GIUINTALSELH 0x16
  52. #define GIUINTHTSELL 0x18
  53. #define GIUINTHTSELH 0x1a
  54. #define GIUPODATL 0x1c
  55. #define GIUPODATEN 0x1c
  56. #define GIUPODATH 0x1e
  57. #define PIOEN0 0x0100
  58. #define PIOEN1 0x0200
  59. #define GIUPODAT 0x1e
  60. #define GIUFEDGEINHL 0x20
  61. #define GIUFEDGEINHH 0x22
  62. #define GIUREDGEINHL 0x24
  63. #define GIUREDGEINHH 0x26
  64. #define GIUUSEUPDN 0x1e0
  65. #define GIUTERMUPDN 0x1e2
  66. #define GPIO_HAS_PULLUPDOWN_IO 0x0001
  67. #define GPIO_HAS_OUTPUT_ENABLE 0x0002
  68. #define GPIO_HAS_INTERRUPT_EDGE_SELECT 0x0100
  69. enum {
  70. GPIO_INPUT,
  71. GPIO_OUTPUT,
  72. };
  73. static DEFINE_SPINLOCK(giu_lock);
  74. static unsigned long giu_flags;
  75. static void __iomem *giu_base;
  76. #define giu_read(offset) readw(giu_base + (offset))
  77. #define giu_write(offset, value) writew((value), giu_base + (offset))
  78. #define GPIO_PIN_OF_IRQ(irq) ((irq) - GIU_IRQ_BASE)
  79. #define GIUINT_HIGH_OFFSET 16
  80. #define GIUINT_HIGH_MAX 32
  81. static inline u16 giu_set(u16 offset, u16 set)
  82. {
  83. u16 data;
  84. data = giu_read(offset);
  85. data |= set;
  86. giu_write(offset, data);
  87. return data;
  88. }
  89. static inline u16 giu_clear(u16 offset, u16 clear)
  90. {
  91. u16 data;
  92. data = giu_read(offset);
  93. data &= ~clear;
  94. giu_write(offset, data);
  95. return data;
  96. }
  97. static void ack_giuint_low(struct irq_data *d)
  98. {
  99. giu_write(GIUINTSTATL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  100. }
  101. static void mask_giuint_low(struct irq_data *d)
  102. {
  103. giu_clear(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  104. }
  105. static void mask_ack_giuint_low(struct irq_data *d)
  106. {
  107. unsigned int pin;
  108. pin = GPIO_PIN_OF_IRQ(d->irq);
  109. giu_clear(GIUINTENL, 1 << pin);
  110. giu_write(GIUINTSTATL, 1 << pin);
  111. }
  112. static void unmask_giuint_low(struct irq_data *d)
  113. {
  114. giu_set(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  115. }
  116. static struct irq_chip giuint_low_irq_chip = {
  117. .name = "GIUINTL",
  118. .irq_ack = ack_giuint_low,
  119. .irq_mask = mask_giuint_low,
  120. .irq_mask_ack = mask_ack_giuint_low,
  121. .irq_unmask = unmask_giuint_low,
  122. };
  123. static void ack_giuint_high(struct irq_data *d)
  124. {
  125. giu_write(GIUINTSTATH,
  126. 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  127. }
  128. static void mask_giuint_high(struct irq_data *d)
  129. {
  130. giu_clear(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  131. }
  132. static void mask_ack_giuint_high(struct irq_data *d)
  133. {
  134. unsigned int pin;
  135. pin = GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET;
  136. giu_clear(GIUINTENH, 1 << pin);
  137. giu_write(GIUINTSTATH, 1 << pin);
  138. }
  139. static void unmask_giuint_high(struct irq_data *d)
  140. {
  141. giu_set(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  142. }
  143. static struct irq_chip giuint_high_irq_chip = {
  144. .name = "GIUINTH",
  145. .irq_ack = ack_giuint_high,
  146. .irq_mask = mask_giuint_high,
  147. .irq_mask_ack = mask_ack_giuint_high,
  148. .irq_unmask = unmask_giuint_high,
  149. };
  150. static int giu_get_irq(unsigned int irq)
  151. {
  152. u16 pendl, pendh, maskl, maskh;
  153. int i;
  154. pendl = giu_read(GIUINTSTATL);
  155. pendh = giu_read(GIUINTSTATH);
  156. maskl = giu_read(GIUINTENL);
  157. maskh = giu_read(GIUINTENH);
  158. maskl &= pendl;
  159. maskh &= pendh;
  160. if (maskl) {
  161. for (i = 0; i < 16; i++) {
  162. if (maskl & (1 << i))
  163. return GIU_IRQ(i);
  164. }
  165. } else if (maskh) {
  166. for (i = 0; i < 16; i++) {
  167. if (maskh & (1 << i))
  168. return GIU_IRQ(i + GIUINT_HIGH_OFFSET);
  169. }
  170. }
  171. printk(KERN_ERR "spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
  172. maskl, pendl, maskh, pendh);
  173. atomic_inc(&irq_err_count);
  174. return -EINVAL;
  175. }
  176. void vr41xx_set_irq_trigger(unsigned int pin, irq_trigger_t trigger,
  177. irq_signal_t signal)
  178. {
  179. u16 mask;
  180. if (pin < GIUINT_HIGH_OFFSET) {
  181. mask = 1 << pin;
  182. if (trigger != IRQ_TRIGGER_LEVEL) {
  183. giu_set(GIUINTTYPL, mask);
  184. if (signal == IRQ_SIGNAL_HOLD)
  185. giu_set(GIUINTHTSELL, mask);
  186. else
  187. giu_clear(GIUINTHTSELL, mask);
  188. if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
  189. switch (trigger) {
  190. case IRQ_TRIGGER_EDGE_FALLING:
  191. giu_set(GIUFEDGEINHL, mask);
  192. giu_clear(GIUREDGEINHL, mask);
  193. break;
  194. case IRQ_TRIGGER_EDGE_RISING:
  195. giu_clear(GIUFEDGEINHL, mask);
  196. giu_set(GIUREDGEINHL, mask);
  197. break;
  198. default:
  199. giu_set(GIUFEDGEINHL, mask);
  200. giu_set(GIUREDGEINHL, mask);
  201. break;
  202. }
  203. }
  204. irq_set_chip_and_handler(GIU_IRQ(pin),
  205. &giuint_low_irq_chip,
  206. handle_edge_irq);
  207. } else {
  208. giu_clear(GIUINTTYPL, mask);
  209. giu_clear(GIUINTHTSELL, mask);
  210. irq_set_chip_and_handler(GIU_IRQ(pin),
  211. &giuint_low_irq_chip,
  212. handle_level_irq);
  213. }
  214. giu_write(GIUINTSTATL, mask);
  215. } else if (pin < GIUINT_HIGH_MAX) {
  216. mask = 1 << (pin - GIUINT_HIGH_OFFSET);
  217. if (trigger != IRQ_TRIGGER_LEVEL) {
  218. giu_set(GIUINTTYPH, mask);
  219. if (signal == IRQ_SIGNAL_HOLD)
  220. giu_set(GIUINTHTSELH, mask);
  221. else
  222. giu_clear(GIUINTHTSELH, mask);
  223. if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
  224. switch (trigger) {
  225. case IRQ_TRIGGER_EDGE_FALLING:
  226. giu_set(GIUFEDGEINHH, mask);
  227. giu_clear(GIUREDGEINHH, mask);
  228. break;
  229. case IRQ_TRIGGER_EDGE_RISING:
  230. giu_clear(GIUFEDGEINHH, mask);
  231. giu_set(GIUREDGEINHH, mask);
  232. break;
  233. default:
  234. giu_set(GIUFEDGEINHH, mask);
  235. giu_set(GIUREDGEINHH, mask);
  236. break;
  237. }
  238. }
  239. irq_set_chip_and_handler(GIU_IRQ(pin),
  240. &giuint_high_irq_chip,
  241. handle_edge_irq);
  242. } else {
  243. giu_clear(GIUINTTYPH, mask);
  244. giu_clear(GIUINTHTSELH, mask);
  245. irq_set_chip_and_handler(GIU_IRQ(pin),
  246. &giuint_high_irq_chip,
  247. handle_level_irq);
  248. }
  249. giu_write(GIUINTSTATH, mask);
  250. }
  251. }
  252. EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger);
  253. void vr41xx_set_irq_level(unsigned int pin, irq_level_t level)
  254. {
  255. u16 mask;
  256. if (pin < GIUINT_HIGH_OFFSET) {
  257. mask = 1 << pin;
  258. if (level == IRQ_LEVEL_HIGH)
  259. giu_set(GIUINTALSELL, mask);
  260. else
  261. giu_clear(GIUINTALSELL, mask);
  262. giu_write(GIUINTSTATL, mask);
  263. } else if (pin < GIUINT_HIGH_MAX) {
  264. mask = 1 << (pin - GIUINT_HIGH_OFFSET);
  265. if (level == IRQ_LEVEL_HIGH)
  266. giu_set(GIUINTALSELH, mask);
  267. else
  268. giu_clear(GIUINTALSELH, mask);
  269. giu_write(GIUINTSTATH, mask);
  270. }
  271. }
  272. EXPORT_SYMBOL_GPL(vr41xx_set_irq_level);
  273. static int giu_set_direction(struct gpio_chip *chip, unsigned pin, int dir)
  274. {
  275. u16 offset, mask, reg;
  276. unsigned long flags;
  277. if (pin >= chip->ngpio)
  278. return -EINVAL;
  279. if (pin < 16) {
  280. offset = GIUIOSELL;
  281. mask = 1 << pin;
  282. } else if (pin < 32) {
  283. offset = GIUIOSELH;
  284. mask = 1 << (pin - 16);
  285. } else {
  286. if (giu_flags & GPIO_HAS_OUTPUT_ENABLE) {
  287. offset = GIUPODATEN;
  288. mask = 1 << (pin - 32);
  289. } else {
  290. switch (pin) {
  291. case 48:
  292. offset = GIUPODATH;
  293. mask = PIOEN0;
  294. break;
  295. case 49:
  296. offset = GIUPODATH;
  297. mask = PIOEN1;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. }
  303. }
  304. spin_lock_irqsave(&giu_lock, flags);
  305. reg = giu_read(offset);
  306. if (dir == GPIO_OUTPUT)
  307. reg |= mask;
  308. else
  309. reg &= ~mask;
  310. giu_write(offset, reg);
  311. spin_unlock_irqrestore(&giu_lock, flags);
  312. return 0;
  313. }
  314. int vr41xx_gpio_pullupdown(unsigned int pin, gpio_pull_t pull)
  315. {
  316. u16 reg, mask;
  317. unsigned long flags;
  318. if ((giu_flags & GPIO_HAS_PULLUPDOWN_IO) != GPIO_HAS_PULLUPDOWN_IO)
  319. return -EPERM;
  320. if (pin >= 15)
  321. return -EINVAL;
  322. mask = 1 << pin;
  323. spin_lock_irqsave(&giu_lock, flags);
  324. if (pull == GPIO_PULL_UP || pull == GPIO_PULL_DOWN) {
  325. reg = giu_read(GIUTERMUPDN);
  326. if (pull == GPIO_PULL_UP)
  327. reg |= mask;
  328. else
  329. reg &= ~mask;
  330. giu_write(GIUTERMUPDN, reg);
  331. reg = giu_read(GIUUSEUPDN);
  332. reg |= mask;
  333. giu_write(GIUUSEUPDN, reg);
  334. } else {
  335. reg = giu_read(GIUUSEUPDN);
  336. reg &= ~mask;
  337. giu_write(GIUUSEUPDN, reg);
  338. }
  339. spin_unlock_irqrestore(&giu_lock, flags);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown);
  343. static int vr41xx_gpio_get(struct gpio_chip *chip, unsigned pin)
  344. {
  345. u16 reg, mask;
  346. if (pin >= chip->ngpio)
  347. return -EINVAL;
  348. if (pin < 16) {
  349. reg = giu_read(GIUPIODL);
  350. mask = 1 << pin;
  351. } else if (pin < 32) {
  352. reg = giu_read(GIUPIODH);
  353. mask = 1 << (pin - 16);
  354. } else if (pin < 48) {
  355. reg = giu_read(GIUPODATL);
  356. mask = 1 << (pin - 32);
  357. } else {
  358. reg = giu_read(GIUPODATH);
  359. mask = 1 << (pin - 48);
  360. }
  361. if (reg & mask)
  362. return 1;
  363. return 0;
  364. }
  365. static void vr41xx_gpio_set(struct gpio_chip *chip, unsigned pin,
  366. int value)
  367. {
  368. u16 offset, mask, reg;
  369. unsigned long flags;
  370. if (pin >= chip->ngpio)
  371. return;
  372. if (pin < 16) {
  373. offset = GIUPIODL;
  374. mask = 1 << pin;
  375. } else if (pin < 32) {
  376. offset = GIUPIODH;
  377. mask = 1 << (pin - 16);
  378. } else if (pin < 48) {
  379. offset = GIUPODATL;
  380. mask = 1 << (pin - 32);
  381. } else {
  382. offset = GIUPODATH;
  383. mask = 1 << (pin - 48);
  384. }
  385. spin_lock_irqsave(&giu_lock, flags);
  386. reg = giu_read(offset);
  387. if (value)
  388. reg |= mask;
  389. else
  390. reg &= ~mask;
  391. giu_write(offset, reg);
  392. spin_unlock_irqrestore(&giu_lock, flags);
  393. }
  394. static int vr41xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  395. {
  396. return giu_set_direction(chip, offset, GPIO_INPUT);
  397. }
  398. static int vr41xx_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  399. int value)
  400. {
  401. vr41xx_gpio_set(chip, offset, value);
  402. return giu_set_direction(chip, offset, GPIO_OUTPUT);
  403. }
  404. static int vr41xx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  405. {
  406. if (offset >= chip->ngpio)
  407. return -EINVAL;
  408. return GIU_IRQ_BASE + offset;
  409. }
  410. static struct gpio_chip vr41xx_gpio_chip = {
  411. .label = "vr41xx",
  412. .owner = THIS_MODULE,
  413. .direction_input = vr41xx_gpio_direction_input,
  414. .get = vr41xx_gpio_get,
  415. .direction_output = vr41xx_gpio_direction_output,
  416. .set = vr41xx_gpio_set,
  417. .to_irq = vr41xx_gpio_to_irq,
  418. };
  419. static int __devinit giu_probe(struct platform_device *pdev)
  420. {
  421. struct resource *res;
  422. unsigned int trigger, i, pin;
  423. struct irq_chip *chip;
  424. int irq, retval;
  425. switch (pdev->id) {
  426. case GPIO_50PINS_PULLUPDOWN:
  427. giu_flags = GPIO_HAS_PULLUPDOWN_IO;
  428. vr41xx_gpio_chip.ngpio = 50;
  429. break;
  430. case GPIO_36PINS:
  431. vr41xx_gpio_chip.ngpio = 36;
  432. break;
  433. case GPIO_48PINS_EDGE_SELECT:
  434. giu_flags = GPIO_HAS_INTERRUPT_EDGE_SELECT;
  435. vr41xx_gpio_chip.ngpio = 48;
  436. break;
  437. default:
  438. dev_err(&pdev->dev, "GIU: unknown ID %d\n", pdev->id);
  439. return -ENODEV;
  440. }
  441. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  442. if (!res)
  443. return -EBUSY;
  444. giu_base = ioremap(res->start, res->end - res->start + 1);
  445. if (!giu_base)
  446. return -ENOMEM;
  447. vr41xx_gpio_chip.dev = &pdev->dev;
  448. retval = gpiochip_add(&vr41xx_gpio_chip);
  449. giu_write(GIUINTENL, 0);
  450. giu_write(GIUINTENH, 0);
  451. trigger = giu_read(GIUINTTYPH) << 16;
  452. trigger |= giu_read(GIUINTTYPL);
  453. for (i = GIU_IRQ_BASE; i <= GIU_IRQ_LAST; i++) {
  454. pin = GPIO_PIN_OF_IRQ(i);
  455. if (pin < GIUINT_HIGH_OFFSET)
  456. chip = &giuint_low_irq_chip;
  457. else
  458. chip = &giuint_high_irq_chip;
  459. if (trigger & (1 << pin))
  460. irq_set_chip_and_handler(i, chip, handle_edge_irq);
  461. else
  462. irq_set_chip_and_handler(i, chip, handle_level_irq);
  463. }
  464. irq = platform_get_irq(pdev, 0);
  465. if (irq < 0 || irq >= nr_irqs)
  466. return -EBUSY;
  467. return cascade_irq(irq, giu_get_irq);
  468. }
  469. static int __devexit giu_remove(struct platform_device *pdev)
  470. {
  471. if (giu_base) {
  472. iounmap(giu_base);
  473. giu_base = NULL;
  474. }
  475. return 0;
  476. }
  477. static struct platform_driver giu_device_driver = {
  478. .probe = giu_probe,
  479. .remove = __devexit_p(giu_remove),
  480. .driver = {
  481. .name = "GIU",
  482. .owner = THIS_MODULE,
  483. },
  484. };
  485. static int __init vr41xx_giu_init(void)
  486. {
  487. return platform_driver_register(&giu_device_driver);
  488. }
  489. static void __exit vr41xx_giu_exit(void)
  490. {
  491. platform_driver_unregister(&giu_device_driver);
  492. }
  493. module_init(vr41xx_giu_init);
  494. module_exit(vr41xx_giu_exit);