gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /* $Id: gpio.c,v 1.17 2005/06/19 17:06:46 starvik Exp $
  2. *
  3. * Etrax general port I/O device
  4. *
  5. * Copyright (c) 1999, 2000, 2001, 2002 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (initial version)
  8. * Ola Knutsson (LED handling)
  9. * Johan Adolfsson (read/set directions, write, port G)
  10. *
  11. * $Log: gpio.c,v $
  12. * Revision 1.17 2005/06/19 17:06:46 starvik
  13. * Merge of Linux 2.6.12.
  14. *
  15. * Revision 1.16 2005/03/07 13:02:29 starvik
  16. * Protect driver global states with spinlock
  17. *
  18. * Revision 1.15 2005/01/05 06:08:55 starvik
  19. * No need to do local_irq_disable after local_irq_save.
  20. *
  21. * Revision 1.14 2004/12/13 12:21:52 starvik
  22. * Added I/O and DMA allocators from Linux 2.4
  23. *
  24. * Revision 1.12 2004/08/24 07:19:59 starvik
  25. * Whitespace cleanup
  26. *
  27. * Revision 1.11 2004/05/14 07:58:03 starvik
  28. * Merge of changes from 2.4
  29. *
  30. * Revision 1.9 2003/09/11 07:29:48 starvik
  31. * Merge of Linux 2.6.0-test5
  32. *
  33. * Revision 1.8 2003/07/04 08:27:37 starvik
  34. * Merge of Linux 2.5.74
  35. *
  36. * Revision 1.7 2003/01/10 07:44:07 starvik
  37. * init_ioremap is now called by kernel before drivers are initialized
  38. *
  39. * Revision 1.6 2002/12/11 13:13:57 starvik
  40. * Added arch/ to v10 specific includes
  41. * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
  42. *
  43. * Revision 1.5 2002/11/20 11:56:11 starvik
  44. * Merge of Linux 2.5.48
  45. *
  46. * Revision 1.4 2002/11/18 10:10:05 starvik
  47. * Linux 2.5 port of latest gpio.c from Linux 2.4
  48. *
  49. * Revision 1.20 2002/10/16 21:16:24 johana
  50. * Added support for PA high level interrupt.
  51. * That gives 2ms response time with iodtest for high levels and 2-12 ms
  52. * response time on low levels if the check is not made in
  53. * process.c:cpu_idle() as well.
  54. *
  55. * Revision 1.19 2002/10/14 18:27:33 johana
  56. * Implemented alarm handling so select() now works.
  57. * Latency is around 6-9 ms with a etrax_gpio_wake_up_check() in
  58. * cpu_idle().
  59. * Otherwise I get 15-18 ms (same as doing the poll in userspace -
  60. * but less overhead).
  61. * TODO? Perhaps we should add the check in IMMEDIATE_BH (or whatever it
  62. * is in 2.4) as well?
  63. * TODO? Perhaps call request_irq()/free_irq() only when needed?
  64. * Increased version to 2.5
  65. *
  66. * Revision 1.18 2002/10/11 15:02:00 johana
  67. * Mask inverted 8 bit value in setget_input().
  68. *
  69. * Revision 1.17 2002/06/17 15:53:01 johana
  70. * Added IO_READ_INBITS, IO_READ_OUTBITS, IO_SETGET_INPUT and IO_SETGET_OUTPUT
  71. * that take a pointer as argument and thus can handle 32 bit ports (G)
  72. * correctly.
  73. * These should be used instead of IO_READBITS, IO_SETINPUT and IO_SETOUTPUT.
  74. * (especially if Port G bit 31 is used)
  75. *
  76. * Revision 1.16 2002/06/17 09:59:51 johana
  77. * Returning 32 bit values in the ioctl return value doesn't work if bit
  78. * 31 is set (could happen for port G), so mask it of with 0x7FFFFFFF.
  79. * A new set of ioctl's will be added.
  80. *
  81. * Revision 1.15 2002/05/06 13:19:13 johana
  82. * IO_SETINPUT returns mask with bit set = inputs for PA and PB as well.
  83. *
  84. * Revision 1.14 2002/04/12 12:01:53 johana
  85. * Use global r_port_g_data_shadow.
  86. * Moved gpio_init_port_g() closer to gpio_init() and marked it __init.
  87. *
  88. * Revision 1.13 2002/04/10 12:03:55 johana
  89. * Added support for port G /dev/gpiog (minor 3).
  90. * Changed indentation on switch cases.
  91. * Fixed other spaces to tabs.
  92. *
  93. * Revision 1.12 2001/11/12 19:42:15 pkj
  94. * * Corrected return values from gpio_leds_ioctl().
  95. * * Fixed compiler warnings.
  96. *
  97. * Revision 1.11 2001/10/30 14:39:12 johana
  98. * Added D() around gpio_write printk.
  99. *
  100. * Revision 1.10 2001/10/25 10:24:42 johana
  101. * Added IO_CFG_WRITE_MODE ioctl and write method that can do fast
  102. * bittoggling in the kernel. (This speeds up programming an FPGA with 450kB
  103. * from ~60 seconds to 4 seconds).
  104. * Added save_flags/cli/restore_flags in ioctl.
  105. *
  106. * Revision 1.9 2001/05/04 14:16:07 matsfg
  107. * Corrected spelling error
  108. *
  109. * Revision 1.8 2001/04/27 13:55:26 matsfg
  110. * Moved initioremap.
  111. * Turns off all LEDS on init.
  112. * Added support for shutdown and powerbutton.
  113. *
  114. * Revision 1.7 2001/04/04 13:30:08 matsfg
  115. * Added bitset and bitclear for leds. Calls init_ioremap to set up memmapping
  116. *
  117. * Revision 1.6 2001/03/26 16:03:06 bjornw
  118. * Needs linux/config.h
  119. *
  120. * Revision 1.5 2001/03/26 14:22:03 bjornw
  121. * Namechange of some config options
  122. *
  123. * Revision 1.4 2001/02/27 13:52:48 bjornw
  124. * malloc.h -> slab.h
  125. *
  126. * Revision 1.3 2001/01/24 15:06:48 bjornw
  127. * gpio_wq correct type
  128. *
  129. * Revision 1.2 2001/01/18 16:07:30 bjornw
  130. * 2.4 port
  131. *
  132. * Revision 1.1 2001/01/18 15:55:16 bjornw
  133. * Verbatim copy of etraxgpio.c from elinux 2.0 added
  134. *
  135. *
  136. */
  137. #include <linux/module.h>
  138. #include <linux/sched.h>
  139. #include <linux/slab.h>
  140. #include <linux/ioport.h>
  141. #include <linux/errno.h>
  142. #include <linux/kernel.h>
  143. #include <linux/fs.h>
  144. #include <linux/string.h>
  145. #include <linux/poll.h>
  146. #include <linux/init.h>
  147. #include <linux/interrupt.h>
  148. #include <asm/etraxgpio.h>
  149. #include <asm/arch/svinto.h>
  150. #include <asm/io.h>
  151. #include <asm/system.h>
  152. #include <asm/irq.h>
  153. #include <asm/arch/io_interface_mux.h>
  154. #define GPIO_MAJOR 120 /* experimental MAJOR number */
  155. #define D(x)
  156. #if 0
  157. static int dp_cnt;
  158. #define DP(x) do { dp_cnt++; if (dp_cnt % 1000 == 0) x; }while(0)
  159. #else
  160. #define DP(x)
  161. #endif
  162. static char gpio_name[] = "etrax gpio";
  163. #if 0
  164. static wait_queue_head_t *gpio_wq;
  165. #endif
  166. static int gpio_ioctl(struct inode *inode, struct file *file,
  167. unsigned int cmd, unsigned long arg);
  168. static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
  169. loff_t *off);
  170. static int gpio_open(struct inode *inode, struct file *filp);
  171. static int gpio_release(struct inode *inode, struct file *filp);
  172. static unsigned int gpio_poll(struct file *filp, struct poll_table_struct *wait);
  173. /* private data per open() of this driver */
  174. struct gpio_private {
  175. struct gpio_private *next;
  176. /* These fields are for PA and PB only */
  177. volatile unsigned char *port, *shadow;
  178. volatile unsigned char *dir, *dir_shadow;
  179. unsigned char changeable_dir;
  180. unsigned char changeable_bits;
  181. unsigned char clk_mask;
  182. unsigned char data_mask;
  183. unsigned char write_msb;
  184. unsigned char pad1, pad2, pad3;
  185. /* These fields are generic */
  186. unsigned long highalarm, lowalarm;
  187. wait_queue_head_t alarm_wq;
  188. int minor;
  189. };
  190. /* linked list of alarms to check for */
  191. static struct gpio_private *alarmlist = 0;
  192. static int gpio_some_alarms = 0; /* Set if someone uses alarm */
  193. static unsigned long gpio_pa_irq_enabled_mask = 0;
  194. static DEFINE_SPINLOCK(gpio_lock); /* Protect directions etc */
  195. /* Port A and B use 8 bit access, but Port G is 32 bit */
  196. #define NUM_PORTS (GPIO_MINOR_B+1)
  197. static volatile unsigned char *ports[NUM_PORTS] = {
  198. R_PORT_PA_DATA,
  199. R_PORT_PB_DATA,
  200. };
  201. static volatile unsigned char *shads[NUM_PORTS] = {
  202. &port_pa_data_shadow,
  203. &port_pb_data_shadow
  204. };
  205. /* What direction bits that are user changeable 1=changeable*/
  206. #ifndef CONFIG_ETRAX_PA_CHANGEABLE_DIR
  207. #define CONFIG_ETRAX_PA_CHANGEABLE_DIR 0x00
  208. #endif
  209. #ifndef CONFIG_ETRAX_PB_CHANGEABLE_DIR
  210. #define CONFIG_ETRAX_PB_CHANGEABLE_DIR 0x00
  211. #endif
  212. #ifndef CONFIG_ETRAX_PA_CHANGEABLE_BITS
  213. #define CONFIG_ETRAX_PA_CHANGEABLE_BITS 0xFF
  214. #endif
  215. #ifndef CONFIG_ETRAX_PB_CHANGEABLE_BITS
  216. #define CONFIG_ETRAX_PB_CHANGEABLE_BITS 0xFF
  217. #endif
  218. static unsigned char changeable_dir[NUM_PORTS] = {
  219. CONFIG_ETRAX_PA_CHANGEABLE_DIR,
  220. CONFIG_ETRAX_PB_CHANGEABLE_DIR
  221. };
  222. static unsigned char changeable_bits[NUM_PORTS] = {
  223. CONFIG_ETRAX_PA_CHANGEABLE_BITS,
  224. CONFIG_ETRAX_PB_CHANGEABLE_BITS
  225. };
  226. static volatile unsigned char *dir[NUM_PORTS] = {
  227. R_PORT_PA_DIR,
  228. R_PORT_PB_DIR
  229. };
  230. static volatile unsigned char *dir_shadow[NUM_PORTS] = {
  231. &port_pa_dir_shadow,
  232. &port_pb_dir_shadow
  233. };
  234. /* All bits in port g that can change dir. */
  235. static const unsigned long int changeable_dir_g_mask = 0x01FFFF01;
  236. /* Port G is 32 bit, handle it special, some bits are both inputs
  237. and outputs at the same time, only some of the bits can change direction
  238. and some of them in groups of 8 bit. */
  239. static unsigned long changeable_dir_g;
  240. static unsigned long dir_g_in_bits;
  241. static unsigned long dir_g_out_bits;
  242. static unsigned long dir_g_shadow; /* 1=output */
  243. #define USE_PORTS(priv) ((priv)->minor <= GPIO_MINOR_B)
  244. static unsigned int
  245. gpio_poll(struct file *file,
  246. poll_table *wait)
  247. {
  248. unsigned int mask = 0;
  249. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  250. unsigned long data;
  251. spin_lock(&gpio_lock);
  252. poll_wait(file, &priv->alarm_wq, wait);
  253. if (priv->minor == GPIO_MINOR_A) {
  254. unsigned long flags;
  255. unsigned long tmp;
  256. data = *R_PORT_PA_DATA;
  257. /* PA has support for high level interrupt -
  258. * lets activate for those low and with highalarm set
  259. */
  260. tmp = ~data & priv->highalarm & 0xFF;
  261. tmp = (tmp << R_IRQ_MASK1_SET__pa0__BITNR);
  262. local_irq_save(flags);
  263. gpio_pa_irq_enabled_mask |= tmp;
  264. *R_IRQ_MASK1_SET = tmp;
  265. local_irq_restore(flags);
  266. } else if (priv->minor == GPIO_MINOR_B)
  267. data = *R_PORT_PB_DATA;
  268. else if (priv->minor == GPIO_MINOR_G)
  269. data = *R_PORT_G_DATA;
  270. else
  271. return 0;
  272. if ((data & priv->highalarm) ||
  273. (~data & priv->lowalarm)) {
  274. mask = POLLIN|POLLRDNORM;
  275. }
  276. spin_unlock(&gpio_lock);
  277. DP(printk("gpio_poll ready: mask 0x%08X\n", mask));
  278. return mask;
  279. }
  280. int etrax_gpio_wake_up_check(void)
  281. {
  282. struct gpio_private *priv = alarmlist;
  283. unsigned long data = 0;
  284. int ret = 0;
  285. spin_lock(&gpio_lock);
  286. while (priv) {
  287. if (USE_PORTS(priv)) {
  288. data = *priv->port;
  289. } else if (priv->minor == GPIO_MINOR_G) {
  290. data = *R_PORT_G_DATA;
  291. }
  292. if ((data & priv->highalarm) ||
  293. (~data & priv->lowalarm)) {
  294. DP(printk("etrax_gpio_wake_up_check %i\n",priv->minor));
  295. wake_up_interruptible(&priv->alarm_wq);
  296. ret = 1;
  297. }
  298. priv = priv->next;
  299. }
  300. spin_unlock(&gpio_lock);
  301. return ret;
  302. }
  303. static irqreturn_t
  304. gpio_poll_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  305. {
  306. if (gpio_some_alarms) {
  307. etrax_gpio_wake_up_check();
  308. return IRQ_HANDLED;
  309. }
  310. return IRQ_NONE;
  311. }
  312. static irqreturn_t
  313. gpio_pa_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  314. {
  315. unsigned long tmp;
  316. spin_lock(&gpio_lock);
  317. /* Find what PA interrupts are active */
  318. tmp = (*R_IRQ_READ1);
  319. /* Find those that we have enabled */
  320. tmp &= gpio_pa_irq_enabled_mask;
  321. /* Clear them.. */
  322. *R_IRQ_MASK1_CLR = tmp;
  323. gpio_pa_irq_enabled_mask &= ~tmp;
  324. spin_unlock(&gpio_lock);
  325. if (gpio_some_alarms) {
  326. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  327. }
  328. return IRQ_NONE;
  329. }
  330. static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
  331. loff_t *off)
  332. {
  333. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  334. unsigned char data, clk_mask, data_mask, write_msb;
  335. unsigned long flags;
  336. spin_lock(&gpio_lock);
  337. ssize_t retval = count;
  338. if (priv->minor !=GPIO_MINOR_A && priv->minor != GPIO_MINOR_B) {
  339. return -EFAULT;
  340. }
  341. if (!access_ok(VERIFY_READ, buf, count)) {
  342. return -EFAULT;
  343. }
  344. clk_mask = priv->clk_mask;
  345. data_mask = priv->data_mask;
  346. /* It must have been configured using the IO_CFG_WRITE_MODE */
  347. /* Perhaps a better error code? */
  348. if (clk_mask == 0 || data_mask == 0) {
  349. return -EPERM;
  350. }
  351. write_msb = priv->write_msb;
  352. D(printk("gpio_write: %lu to data 0x%02X clk 0x%02X msb: %i\n",count, data_mask, clk_mask, write_msb));
  353. while (count--) {
  354. int i;
  355. data = *buf++;
  356. if (priv->write_msb) {
  357. for (i = 7; i >= 0;i--) {
  358. local_irq_save(flags);
  359. *priv->port = *priv->shadow &= ~clk_mask;
  360. if (data & 1<<i)
  361. *priv->port = *priv->shadow |= data_mask;
  362. else
  363. *priv->port = *priv->shadow &= ~data_mask;
  364. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  365. *priv->port = *priv->shadow |= clk_mask;
  366. local_irq_restore(flags);
  367. }
  368. } else {
  369. for (i = 0; i <= 7;i++) {
  370. local_irq_save(flags);
  371. *priv->port = *priv->shadow &= ~clk_mask;
  372. if (data & 1<<i)
  373. *priv->port = *priv->shadow |= data_mask;
  374. else
  375. *priv->port = *priv->shadow &= ~data_mask;
  376. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  377. *priv->port = *priv->shadow |= clk_mask;
  378. local_irq_restore(flags);
  379. }
  380. }
  381. }
  382. spin_unlock(&gpio_lock);
  383. return retval;
  384. }
  385. static int
  386. gpio_open(struct inode *inode, struct file *filp)
  387. {
  388. struct gpio_private *priv;
  389. int p = iminor(inode);
  390. if (p > GPIO_MINOR_LAST)
  391. return -EINVAL;
  392. priv = (struct gpio_private *)kmalloc(sizeof(struct gpio_private),
  393. GFP_KERNEL);
  394. if (!priv)
  395. return -ENOMEM;
  396. priv->minor = p;
  397. /* initialize the io/alarm struct and link it into our alarmlist */
  398. priv->next = alarmlist;
  399. alarmlist = priv;
  400. if (USE_PORTS(priv)) { /* A and B */
  401. priv->port = ports[p];
  402. priv->shadow = shads[p];
  403. priv->dir = dir[p];
  404. priv->dir_shadow = dir_shadow[p];
  405. priv->changeable_dir = changeable_dir[p];
  406. priv->changeable_bits = changeable_bits[p];
  407. } else {
  408. priv->port = NULL;
  409. priv->shadow = NULL;
  410. priv->dir = NULL;
  411. priv->dir_shadow = NULL;
  412. priv->changeable_dir = 0;
  413. priv->changeable_bits = 0;
  414. }
  415. priv->highalarm = 0;
  416. priv->lowalarm = 0;
  417. priv->clk_mask = 0;
  418. priv->data_mask = 0;
  419. init_waitqueue_head(&priv->alarm_wq);
  420. filp->private_data = (void *)priv;
  421. return 0;
  422. }
  423. static int
  424. gpio_release(struct inode *inode, struct file *filp)
  425. {
  426. struct gpio_private *p;
  427. struct gpio_private *todel;
  428. spin_lock(&gpio_lock);
  429. p = alarmlist;
  430. todel = (struct gpio_private *)filp->private_data;
  431. /* unlink from alarmlist and free the private structure */
  432. if (p == todel) {
  433. alarmlist = todel->next;
  434. } else {
  435. while (p->next != todel)
  436. p = p->next;
  437. p->next = todel->next;
  438. }
  439. kfree(todel);
  440. /* Check if there are still any alarms set */
  441. p = alarmlist;
  442. while (p) {
  443. if (p->highalarm | p->lowalarm) {
  444. gpio_some_alarms = 1;
  445. return 0;
  446. }
  447. p = p->next;
  448. }
  449. gpio_some_alarms = 0;
  450. spin_unlock(&gpio_lock);
  451. return 0;
  452. }
  453. /* Main device API. ioctl's to read/set/clear bits, as well as to
  454. * set alarms to wait for using a subsequent select().
  455. */
  456. unsigned long inline setget_input(struct gpio_private *priv, unsigned long arg)
  457. {
  458. /* Set direction 0=unchanged 1=input,
  459. * return mask with 1=input
  460. */
  461. unsigned long flags;
  462. if (USE_PORTS(priv)) {
  463. local_irq_save(flags);
  464. *priv->dir = *priv->dir_shadow &=
  465. ~((unsigned char)arg & priv->changeable_dir);
  466. local_irq_restore(flags);
  467. return ~(*priv->dir_shadow) & 0xFF; /* Only 8 bits */
  468. } else if (priv->minor == GPIO_MINOR_G) {
  469. /* We must fiddle with R_GEN_CONFIG to change dir */
  470. local_irq_save(flags);
  471. if (((arg & dir_g_in_bits) != arg) &&
  472. (arg & changeable_dir_g)) {
  473. arg &= changeable_dir_g;
  474. /* Clear bits in genconfig to set to input */
  475. if (arg & (1<<0)) {
  476. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG,g0dir);
  477. dir_g_in_bits |= (1<<0);
  478. dir_g_out_bits &= ~(1<<0);
  479. }
  480. if ((arg & 0x0000FF00) == 0x0000FF00) {
  481. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG,g8_15dir);
  482. dir_g_in_bits |= 0x0000FF00;
  483. dir_g_out_bits &= ~0x0000FF00;
  484. }
  485. if ((arg & 0x00FF0000) == 0x00FF0000) {
  486. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG,g16_23dir);
  487. dir_g_in_bits |= 0x00FF0000;
  488. dir_g_out_bits &= ~0x00FF0000;
  489. }
  490. if (arg & (1<<24)) {
  491. genconfig_shadow &= ~IO_MASK(R_GEN_CONFIG,g24dir);
  492. dir_g_in_bits |= (1<<24);
  493. dir_g_out_bits &= ~(1<<24);
  494. }
  495. D(printk(KERN_INFO "gpio: SETINPUT on port G set "
  496. "genconfig to 0x%08lX "
  497. "in_bits: 0x%08lX "
  498. "out_bits: 0x%08lX\n",
  499. (unsigned long)genconfig_shadow,
  500. dir_g_in_bits, dir_g_out_bits));
  501. *R_GEN_CONFIG = genconfig_shadow;
  502. /* Must be a >120 ns delay before writing this again */
  503. }
  504. local_irq_restore(flags);
  505. return dir_g_in_bits;
  506. }
  507. return 0;
  508. } /* setget_input */
  509. unsigned long inline setget_output(struct gpio_private *priv, unsigned long arg)
  510. {
  511. unsigned long flags;
  512. if (USE_PORTS(priv)) {
  513. local_irq_save(flags);
  514. *priv->dir = *priv->dir_shadow |=
  515. ((unsigned char)arg & priv->changeable_dir);
  516. local_irq_restore(flags);
  517. return *priv->dir_shadow;
  518. } else if (priv->minor == GPIO_MINOR_G) {
  519. /* We must fiddle with R_GEN_CONFIG to change dir */
  520. local_irq_save(flags);
  521. if (((arg & dir_g_out_bits) != arg) &&
  522. (arg & changeable_dir_g)) {
  523. /* Set bits in genconfig to set to output */
  524. if (arg & (1<<0)) {
  525. genconfig_shadow |= IO_MASK(R_GEN_CONFIG,g0dir);
  526. dir_g_out_bits |= (1<<0);
  527. dir_g_in_bits &= ~(1<<0);
  528. }
  529. if ((arg & 0x0000FF00) == 0x0000FF00) {
  530. genconfig_shadow |= IO_MASK(R_GEN_CONFIG,g8_15dir);
  531. dir_g_out_bits |= 0x0000FF00;
  532. dir_g_in_bits &= ~0x0000FF00;
  533. }
  534. if ((arg & 0x00FF0000) == 0x00FF0000) {
  535. genconfig_shadow |= IO_MASK(R_GEN_CONFIG,g16_23dir);
  536. dir_g_out_bits |= 0x00FF0000;
  537. dir_g_in_bits &= ~0x00FF0000;
  538. }
  539. if (arg & (1<<24)) {
  540. genconfig_shadow |= IO_MASK(R_GEN_CONFIG,g24dir);
  541. dir_g_out_bits |= (1<<24);
  542. dir_g_in_bits &= ~(1<<24);
  543. }
  544. D(printk(KERN_INFO "gpio: SETOUTPUT on port G set "
  545. "genconfig to 0x%08lX "
  546. "in_bits: 0x%08lX "
  547. "out_bits: 0x%08lX\n",
  548. (unsigned long)genconfig_shadow,
  549. dir_g_in_bits, dir_g_out_bits));
  550. *R_GEN_CONFIG = genconfig_shadow;
  551. /* Must be a >120 ns delay before writing this again */
  552. }
  553. local_irq_restore(flags);
  554. return dir_g_out_bits & 0x7FFFFFFF;
  555. }
  556. return 0;
  557. } /* setget_output */
  558. static int
  559. gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  560. static int
  561. gpio_ioctl(struct inode *inode, struct file *file,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. unsigned long flags;
  565. unsigned long val;
  566. int ret = 0;
  567. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  568. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE) {
  569. return -EINVAL;
  570. }
  571. spin_lock(&gpio_lock);
  572. switch (_IOC_NR(cmd)) {
  573. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  574. // read the port
  575. if (USE_PORTS(priv)) {
  576. ret = *priv->port;
  577. } else if (priv->minor == GPIO_MINOR_G) {
  578. ret = (*R_PORT_G_DATA) & 0x7FFFFFFF;
  579. }
  580. break;
  581. case IO_SETBITS:
  582. local_irq_save(flags);
  583. // set changeable bits with a 1 in arg
  584. if (USE_PORTS(priv)) {
  585. *priv->port = *priv->shadow |=
  586. ((unsigned char)arg & priv->changeable_bits);
  587. } else if (priv->minor == GPIO_MINOR_G) {
  588. *R_PORT_G_DATA = port_g_data_shadow |= (arg & dir_g_out_bits);
  589. }
  590. local_irq_restore(flags);
  591. break;
  592. case IO_CLRBITS:
  593. local_irq_save(flags);
  594. // clear changeable bits with a 1 in arg
  595. if (USE_PORTS(priv)) {
  596. *priv->port = *priv->shadow &=
  597. ~((unsigned char)arg & priv->changeable_bits);
  598. } else if (priv->minor == GPIO_MINOR_G) {
  599. *R_PORT_G_DATA = port_g_data_shadow &= ~((unsigned long)arg & dir_g_out_bits);
  600. }
  601. local_irq_restore(flags);
  602. break;
  603. case IO_HIGHALARM:
  604. // set alarm when bits with 1 in arg go high
  605. priv->highalarm |= arg;
  606. gpio_some_alarms = 1;
  607. break;
  608. case IO_LOWALARM:
  609. // set alarm when bits with 1 in arg go low
  610. priv->lowalarm |= arg;
  611. gpio_some_alarms = 1;
  612. break;
  613. case IO_CLRALARM:
  614. // clear alarm for bits with 1 in arg
  615. priv->highalarm &= ~arg;
  616. priv->lowalarm &= ~arg;
  617. {
  618. /* Must update gpio_some_alarms */
  619. struct gpio_private *p = alarmlist;
  620. int some_alarms;
  621. some_alarms = 0;
  622. while (p) {
  623. if (p->highalarm | p->lowalarm) {
  624. some_alarms = 1;
  625. break;
  626. }
  627. p = p->next;
  628. }
  629. gpio_some_alarms = some_alarms;
  630. }
  631. break;
  632. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  633. /* Read direction 0=input 1=output */
  634. if (USE_PORTS(priv)) {
  635. ret = *priv->dir_shadow;
  636. } else if (priv->minor == GPIO_MINOR_G) {
  637. /* Note: Some bits are both in and out,
  638. * Those that are dual is set here as well.
  639. */
  640. ret = (dir_g_shadow | dir_g_out_bits) & 0x7FFFFFFF;
  641. }
  642. break;
  643. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  644. /* Set direction 0=unchanged 1=input,
  645. * return mask with 1=input
  646. */
  647. ret = setget_input(priv, arg) & 0x7FFFFFFF;
  648. break;
  649. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  650. /* Set direction 0=unchanged 1=output,
  651. * return mask with 1=output
  652. */
  653. ret = setget_output(priv, arg) & 0x7FFFFFFF;
  654. break;
  655. case IO_SHUTDOWN:
  656. SOFT_SHUTDOWN();
  657. break;
  658. case IO_GET_PWR_BT:
  659. #if defined (CONFIG_ETRAX_SOFT_SHUTDOWN)
  660. ret = (*R_PORT_G_DATA & ( 1 << CONFIG_ETRAX_POWERBUTTON_BIT));
  661. #else
  662. ret = 0;
  663. #endif
  664. break;
  665. case IO_CFG_WRITE_MODE:
  666. priv->clk_mask = arg & 0xFF;
  667. priv->data_mask = (arg >> 8) & 0xFF;
  668. priv->write_msb = (arg >> 16) & 0x01;
  669. /* Check if we're allowed to change the bits and
  670. * the direction is correct
  671. */
  672. if (!((priv->clk_mask & priv->changeable_bits) &&
  673. (priv->data_mask & priv->changeable_bits) &&
  674. (priv->clk_mask & *priv->dir_shadow) &&
  675. (priv->data_mask & *priv->dir_shadow)))
  676. {
  677. priv->clk_mask = 0;
  678. priv->data_mask = 0;
  679. ret = -EPERM;
  680. }
  681. break;
  682. case IO_READ_INBITS:
  683. /* *arg is result of reading the input pins */
  684. if (USE_PORTS(priv)) {
  685. val = *priv->port;
  686. } else if (priv->minor == GPIO_MINOR_G) {
  687. val = *R_PORT_G_DATA;
  688. }
  689. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  690. ret = -EFAULT;
  691. break;
  692. case IO_READ_OUTBITS:
  693. /* *arg is result of reading the output shadow */
  694. if (USE_PORTS(priv)) {
  695. val = *priv->shadow;
  696. } else if (priv->minor == GPIO_MINOR_G) {
  697. val = port_g_data_shadow;
  698. }
  699. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  700. ret = -EFAULT;
  701. break;
  702. case IO_SETGET_INPUT:
  703. /* bits set in *arg is set to input,
  704. * *arg updated with current input pins.
  705. */
  706. if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
  707. {
  708. ret = -EFAULT;
  709. break;
  710. }
  711. val = setget_input(priv, val);
  712. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  713. ret = -EFAULT;
  714. break;
  715. case IO_SETGET_OUTPUT:
  716. /* bits set in *arg is set to output,
  717. * *arg updated with current output pins.
  718. */
  719. if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
  720. {
  721. ret = -EFAULT;
  722. break;
  723. }
  724. val = setget_output(priv, val);
  725. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  726. ret = -EFAULT;
  727. break;
  728. default:
  729. if (priv->minor == GPIO_MINOR_LEDS)
  730. ret = gpio_leds_ioctl(cmd, arg);
  731. else
  732. ret = -EINVAL;
  733. } /* switch */
  734. spin_unlock(&gpio_lock);
  735. return ret;
  736. }
  737. static int
  738. gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  739. {
  740. unsigned char green;
  741. unsigned char red;
  742. switch (_IOC_NR(cmd)) {
  743. case IO_LEDACTIVE_SET:
  744. green = ((unsigned char) arg) & 1;
  745. red = (((unsigned char) arg) >> 1) & 1;
  746. LED_ACTIVE_SET_G(green);
  747. LED_ACTIVE_SET_R(red);
  748. break;
  749. case IO_LED_SETBIT:
  750. LED_BIT_SET(arg);
  751. break;
  752. case IO_LED_CLRBIT:
  753. LED_BIT_CLR(arg);
  754. break;
  755. default:
  756. return -EINVAL;
  757. } /* switch */
  758. return 0;
  759. }
  760. struct file_operations gpio_fops = {
  761. .owner = THIS_MODULE,
  762. .poll = gpio_poll,
  763. .ioctl = gpio_ioctl,
  764. .write = gpio_write,
  765. .open = gpio_open,
  766. .release = gpio_release,
  767. };
  768. void ioif_watcher(const unsigned int gpio_in_available,
  769. const unsigned int gpio_out_available,
  770. const unsigned char pa_available,
  771. const unsigned char pb_available)
  772. {
  773. unsigned long int flags;
  774. D(printk("gpio.c: ioif_watcher called\n"));
  775. D(printk("gpio.c: G in: 0x%08x G out: 0x%08x PA: 0x%02x PB: 0x%02x\n",
  776. gpio_in_available, gpio_out_available, pa_available, pb_available));
  777. spin_lock_irqsave(&gpio_lock, flags);
  778. dir_g_in_bits = gpio_in_available;
  779. dir_g_out_bits = gpio_out_available;
  780. /* Initialise the dir_g_shadow etc. depending on genconfig */
  781. /* 0=input 1=output */
  782. if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g0dir, out))
  783. dir_g_shadow |= (1 << 0);
  784. if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g8_15dir, out))
  785. dir_g_shadow |= 0x0000FF00;
  786. if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g16_23dir, out))
  787. dir_g_shadow |= 0x00FF0000;
  788. if (genconfig_shadow & IO_STATE(R_GEN_CONFIG, g24dir, out))
  789. dir_g_shadow |= (1 << 24);
  790. changeable_dir_g = changeable_dir_g_mask;
  791. changeable_dir_g &= dir_g_out_bits;
  792. changeable_dir_g &= dir_g_in_bits;
  793. /* Correct the bits that can change direction */
  794. dir_g_out_bits &= ~changeable_dir_g;
  795. dir_g_out_bits |= dir_g_shadow;
  796. dir_g_in_bits &= ~changeable_dir_g;
  797. dir_g_in_bits |= (~dir_g_shadow & changeable_dir_g);
  798. spin_unlock_irqrestore(&gpio_lock, flags);
  799. printk(KERN_INFO "GPIO port G: in_bits: 0x%08lX out_bits: 0x%08lX val: %08lX\n",
  800. dir_g_in_bits, dir_g_out_bits, (unsigned long)*R_PORT_G_DATA);
  801. printk(KERN_INFO "GPIO port G: dir: %08lX changeable: %08lX\n",
  802. dir_g_shadow, changeable_dir_g);
  803. }
  804. /* main driver initialization routine, called from mem.c */
  805. static __init int
  806. gpio_init(void)
  807. {
  808. int res;
  809. #if defined (CONFIG_ETRAX_CSP0_LEDS)
  810. int i;
  811. #endif
  812. printk("gpio init\n");
  813. /* do the formalities */
  814. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  815. if (res < 0) {
  816. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  817. return res;
  818. }
  819. /* Clear all leds */
  820. #if defined (CONFIG_ETRAX_CSP0_LEDS) || defined (CONFIG_ETRAX_PA_LEDS) || defined (CONFIG_ETRAX_PB_LEDS)
  821. LED_NETWORK_SET(0);
  822. LED_ACTIVE_SET(0);
  823. LED_DISK_READ(0);
  824. LED_DISK_WRITE(0);
  825. #if defined (CONFIG_ETRAX_CSP0_LEDS)
  826. for (i = 0; i < 32; i++) {
  827. LED_BIT_SET(i);
  828. }
  829. #endif
  830. #endif
  831. /* The I/O interface allocation watcher will be called when
  832. * registering it. */
  833. if (cris_io_interface_register_watcher(ioif_watcher)){
  834. printk(KERN_WARNING "gpio_init: Failed to install IO if allocator watcher\n");
  835. }
  836. printk(KERN_INFO "ETRAX 100LX GPIO driver v2.5, (c) 2001, 2002, 2003, 2004 Axis Communications AB\n");
  837. /* We call etrax_gpio_wake_up_check() from timer interrupt and
  838. * from cpu_idle() in kernel/process.c
  839. * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
  840. * in some tests.
  841. */
  842. if (request_irq(TIMER0_IRQ_NBR, gpio_poll_timer_interrupt,
  843. IRQF_SHARED | IRQF_DISABLED,"gpio poll", NULL)) {
  844. printk(KERN_CRIT "err: timer0 irq for gpio\n");
  845. }
  846. if (request_irq(PA_IRQ_NBR, gpio_pa_interrupt,
  847. IRQF_SHARED | IRQF_DISABLED,"gpio PA", NULL)) {
  848. printk(KERN_CRIT "err: PA irq for gpio\n");
  849. }
  850. return res;
  851. }
  852. /* this makes sure that gpio_init is called during kernel boot */
  853. module_init(gpio_init);