title: Why am I getting segmentation fault on Beaglebone Black when accessing GPIO2 and GPIO3 through kernel module?
body: <p>I have been attempting to get access to GPIO2 and GPIO3 on the beaglebone black through kernel module with no success. Every time I attempt to assign an output value to GPIOs 2 and 3 I get a segmentation fault.</p>
  
  <p>The exact same code (with the appropriate pin assignment) works for GPIO0 and GPIO1.</p>
  
  <p>I have attempted various pins on both P8 and P9 related to GPIO2 and GPIO3 with no success. On the flip side, the same exact code works for GPIO0 and GPIO1 with appropriate pin assignment.</p>
  
  <p>For pin values I am using the official BBB manual. For appropriate I/O GPIO availability I am checking this diagram from beagleboard.com:
  <a href="https://i.stack.imgur.com/dy2y5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dy2y5.png" alt="http://beagleboard.org/support/bone101 65 possible digital I/O"></a></p>
  
  <pre><code>#include &lt;linux/init.h&gt;
  #include &lt;linux/module.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;net/tcp.h&gt;
  
  //Macros
  #define GPIO1_START_ADDR 0x4804C000
  #define GPIO2_START_ADDR 0x481AC000
  #define GPIO2_END_ADDR 0x481ACFFF
  #define GPIO3_START_ADDR 0x481AE000
  
  #define SIZE (GPIO2_END_ADDR - GPIO2_START_ADDR)
  #define GPIO_OE 0x134
  #define GPIO_DATAOUT 0x13C
  
  //A couple of standard descriptions
  MODULE_LICENSE("GPL");
  
  static int hello_init(void)
  {
      volatile void *gpio_addr;
      volatile unsigned int *oe_addr;
      volatile unsigned int *dataout_addr;
  
      printk(KERN_NOTICE "Module: Initializing module\n");
  
      printk(KERN_NOTICE "Module: Map GPIO\n");
      gpio_addr = ioremap(GPIO3_START_ADDR,SIZE);
  
      printk(KERN_NOTICE "Module: Set oe_addr\n");
      oe_addr = gpio_addr + GPIO_OE;
  
      printk(KERN_NOTICE "Module: Set dataout_addr\n");
      dataout_addr = gpio_addr + GPIO_DATAOUT;
  
      //Code will work up to here for any GPIO.
      //It crashes on the following for GPIO2 and GPIO3:
  
      printk(KERN_NOTICE "Module: Set pin to OUTPUT\n");
      *oe_addr &amp;= (0xFFFFFFFF ^ (1&lt;&lt;19));
  
      printk(KERN_NOTICE "Module: Set pin output to HIGH\n");
      *dataout_addr |= (1&lt;&lt;19);
  
      return 0;
  }
  
  static void hello_exit(void)
  {
      printk(KERN_INFO "Exit module.\n");
  }
  
  module_init(hello_init);
  module_exit(hello_exit);
  </code></pre>
  
  <p>If I block out the two lines
  <code>*oe_addr &amp;= (0xFFFFFFFF ^ (1&lt;&lt;19));</code> and
  <code>*dataout_addr |= (1&lt;&lt;19);</code>, the program runs for all GPIOs without glitch.</p>
  
  <p><code>$uname -a: Linux beaglebone 3.8.13-bone79</code></p>
  
  <p>Why am I getting segmentation fault when accessing GPIO2 and GPIO3?</p>