#!/bin/sh

#Turn off external bus
peekpoke 16 0xb0010012 0x0

if [ -e /linuxrc ]; then
  . /ts.subr
  backlite_off
else
  . /initrd/ts.subr
  eval `/initrd/sbin/ts4800ctl --info`
  export bootdev model baseboard_model
fi

let model=$model\&0xffff
if [ $model -ne 18432 ]; then
  exit 0
fi
let baseboard_model=$baseboard_model\&0x3f

if [ $baseboard_model -eq 1 -o $baseboard_model -eq 2 ]; then
  VIDMODE="800x480M@50"
  TSLCDPARAM=""
  SPLASH="splash-800x480.gz"
  XORG="/xorg-800x480.conf"
  touchscreen_on
elif [ $baseboard_model -eq 5 ]; then
  VIDMODE="640x480M@50"
  TSLCDPARAM="swapxy=1 negx=1"
  SPLASH="splash-640x480.gz"
  XORG="/xorg-640x480.conf"
  gpio_dir_set 4 14 1
  gpio_data_set 4 14 1
  dio_dir_set 2 1 1
  dio_data_set 2 1 0 
  touchscreen_on
elif [ $baseboard_model -eq 10 ]; then
  VIDMODE="800x600M@50"
  TSLCDPARAM="ioaddr=0xb0011080"
  SPLASH="splash-800x480.gz"
  XORG="/xorg-800x600.conf"
  dio_dir_set 2 2 1
  dio_data_set 2 2 1
  bbclk_on
  peekpoke 16 0xb0010012 0x321
else
  insmod /mxc_ipuv3_fb.ko
  exit 0
fi

if [ -e /linuxrc ]; then
  insmod /mxc_ipuv3_fb.ko mode_param=$VIDMODE
  gunzip -c /$SPLASH > /dev/fb0
  tpc_up

  TMP=0
  while [ $TMP -lt 300 -a -e /mnt/root/notrootfs ]; do
    TMP=$((TMP+1))
    usleep 100000
  done
  if [ ! -e /mnt/root/notrootfs ]; then
    snd_on
    modprobe snd-pcm-oss
    modprobe snd-soc-imx-3stack-sgtl5000
    amixer cset numid=5 127,127
    amixer cset numid=4 192,192
    spk_on
    aplay /power.wav &

    modprobe usbcore
    modprobe ehci_hcd
    modprobe usbhid
    modprobe ts_lcd hardcode=1 $TSLCDPARAM
    modprobe mousedev
    modprobe evdev
    modprobe mxc_v4l2_output

    rm /tmp/.X0-lock /tmp/.X11-unix
    Xorg -br -logfile /dev/null -config $XORG &
    TMP=0
    while [ $TMP -lt 100 -a ! -e /tmp/.X0-lock ]; do
      TMP=$((TMP+1))
      usleep 100000
    done
    usleep 1500000
    icewm-session &
    xhost +
  fi
else
  export DISPLAY=:0.0
  XORG="/initrd$XORG"
  Xorg -br -logfile /dev/null -config $XORG &
  TMP=0
  while [ $TMP -lt 100 -a ! -e /tmp/.X0-lock ]; do
    TMP=$((TMP+1))
    /initrd/bin/busybox usleep 100000
  done
  sleep 1
  icewm-session &
  xhost +
fi

