1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-11-16 23:35:21 +02:00
iris/source/lcd.ccp

471 lines
13 KiB
Plaintext
Raw Normal View History

2009-06-10 22:42:01 +03:00
#pypp 0
// Iris: micro-kernel for a capability-based operating system.
2009-06-24 01:47:13 +03:00
// boot-programs/lcd.ccp: Display driver.
2009-06-10 22:42:01 +03:00
// Copyright 2009 Bas Wijnen <wijnen@debian.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-06-10 23:54:12 +03:00
#include "devices.hh"
2009-06-22 18:48:05 +03:00
#define ARCH
#include "arch.hh"
2009-06-10 22:42:01 +03:00
2010-01-16 17:13:54 +02:00
__asm__ volatile (".section .rodata\n.globl charset\ncharset:\n.incbin \"source/charset.data\"\n.section .text")
2009-12-27 01:12:35 +02:00
extern unsigned char const charset[127-32][8][6]
2009-07-21 13:17:52 +03:00
2009-12-11 10:43:42 +02:00
#define assert(x) do { if (!(x)) { kdebug ("assertion failed " #x); while (true) {} } } while (0)
2009-07-20 01:23:45 +03:00
2009-10-10 02:31:10 +03:00
#if defined (TRENDTAC)
2009-10-31 10:32:23 +02:00
static unsigned h = 800, v = 480, fps = 60, Bpp = 2
2009-12-26 15:17:06 +02:00
#define LOG_X_BASE 1
#define LOG_Y_BASE 1
2009-10-31 10:32:23 +02:00
#elif defined (NANONOTE)
2009-12-11 10:43:42 +02:00
static unsigned h = 320, v = 240, fps = 60, Bpp = 4
2009-12-26 15:17:06 +02:00
#define LOG_X_BASE 0
#define LOG_Y_BASE 0
2009-10-10 02:31:10 +03:00
#else
#error unknown board
#endif
2009-07-20 01:23:45 +03:00
#define frame_size (v * h * Bpp)
2009-08-24 22:02:35 +03:00
static unsigned physical_descriptor
2009-10-31 10:32:23 +02:00
/**/struct Descriptor {
unsigned next;
unsigned frame;
unsigned id;
unsigned cmd;
} __attribute__ ((packed))
#if defined (NANONOTE)
#define SP_PORT 2
#define SPEN 21
#define SPDA 22
#define SPCK 23
static void udelay (unsigned num):
for unsigned i = 0; i < num * (JZ_EXTAL / 1000000); ++i:
// set 0 does nothing, but costs at least one clock pulse.
gpio_set (SP_PORT, 0)
// Register names. The registers are not named in the datasheet, and in some cases the name only describes a part of the bits in it.
// The registers with bit 6 set have bit 7 set instead, because of the transfer method.
enum spi_reg:
AC = 0x00
DC = 0x01
BRIGHTNESS = 0x03
FORMAT = 0x04
BACKLIGHT1 = 0x05
VBLANK = 0x06
HBLANK = 0x07
BACKLIGHT2 = 0x08
SYNC = 0x0b
POLARITY = 0x0c
2009-12-11 10:43:42 +02:00
CONTRAST_RGB = 0x0d
2009-10-31 10:32:23 +02:00
SUB_CONTRAST_R = 0x0e
SUB_BRIGHTNESS_R= 0x0f
SUB_CONTRAST_B = 0x10
SUB_BRIGHTNESS_B= 0x11
TRIM = 0x12
COLOR = 0x13
GAMMA = 0x16
GAMMA1 = 0x17
GAMMA2 = 0x18
GAMMA3 = 0x19
GAMMA4 = 0x1a
INVERSION = 0xa5
HLEVEL = 0xa6
LLEVEL = 0xa7
FB = 0xb1
static void write_reg (unsigned reg, unsigned val):
2009-12-11 10:43:42 +02:00
unsigned value = (reg << 8) | (val & 0xff)
2009-10-31 10:32:23 +02:00
gpio_clear (SP_PORT, 1 << SPEN)
udelay (1)
for unsigned i = 0; i < 16; ++i, value <<= 1:
gpio_clear (SP_PORT, 1 << SPCK)
if value & 0x8000:
gpio_set (SP_PORT, 1 << SPDA)
else:
gpio_clear (SP_PORT, 1 << SPDA)
udelay (4)
gpio_set (SP_PORT, 1 << SPCK)
2009-12-11 10:43:42 +02:00
udelay (4)
2009-10-31 10:32:23 +02:00
gpio_set (SP_PORT, 1 << SPEN)
udelay(4)
#endif
2009-07-20 01:23:45 +03:00
2009-08-24 22:02:35 +03:00
static void reset ():
2009-10-31 10:32:23 +02:00
#if defined (TRENDTAC)
// Note that the sync pulse is part of the pre-display region.
2009-10-10 02:31:10 +03:00
// Vertical timings.
2009-10-31 10:32:23 +02:00
unsigned vsync = 20, vpre = 20, vpost = 0
2009-10-10 02:31:10 +03:00
// Horizontal timings.
2009-10-31 10:32:23 +02:00
unsigned hsync = 80, hpre = 80, hpost = 0
// One clock pulse per pixel.
2009-12-11 10:43:42 +02:00
unsigned cpp = 1
2009-10-10 02:31:10 +03:00
// Bits per pixel.
unsigned bpp = LCD_CTRL_BPP_16
// Configuration.
2009-07-05 11:52:44 +03:00
#define MODE_TFT_GEN 0
#define VSYNC_N (1 << 8)
2009-10-10 02:31:10 +03:00
unsigned cfg = MODE_TFT_GEN | VSYNC_N
2009-10-31 10:32:23 +02:00
#elif defined (NANONOTE)
// Note that the sync pulse is part of the pre-display region.
2009-12-11 10:43:42 +02:00
// Horizontal timings.
2009-12-18 10:00:38 +02:00
unsigned hsync = 1, hpre = 70, hpost = 273
2009-10-10 02:31:10 +03:00
// Vertical timings.
2009-10-31 10:32:23 +02:00
unsigned vsync = 1, vpre = 21, vpost = 2
2009-12-11 10:43:42 +02:00
// 3 clocks per pixel.
unsigned cpp = 3
2009-10-10 02:31:10 +03:00
// Bits per pixel.
unsigned bpp = LCD_CTRL_BPP_18_24
// Configuration.
2009-12-18 10:00:38 +02:00
unsigned cfg = LCD_CFG_MODE_SERIAL_TFT | LCD_CFG_HSP | LCD_CFG_VSP | LCD_CFG_PCP
2009-10-31 10:32:23 +02:00
// Set up SPI pins.
2009-12-11 10:43:42 +02:00
gpio_as_output (SP_PORT, (1 << SPEN) | (1 << SPCK) | (1 << SPDA))
2009-10-31 10:32:23 +02:00
gpio_set (SP_PORT, (1 << SPEN) | (1 << SPCK))
#else
#error unknown board
#endif
2009-10-10 02:31:10 +03:00
2009-10-31 10:32:23 +02:00
// Note that the sync pulse is part of the pre-display region.
2009-12-11 10:43:42 +02:00
unsigned vpe = vsync, vds = vpre, vde = vds + v, vt = vde + vpost
2009-12-18 10:00:38 +02:00
unsigned hpe = hsync, hds = hpre, hde = hds + h, ht = hde + hpost
2009-07-05 11:52:44 +03:00
cpm_stop_lcd ()
2009-06-10 22:42:01 +03:00
2009-12-11 10:43:42 +02:00
unsigned pixclock = fps * ht * vt
2009-10-10 02:31:10 +03:00
#if defined (TRENDTAC)
2009-07-20 01:23:45 +03:00
unsigned pllout = cpm_get_pllout ()
2009-07-05 11:52:44 +03:00
CPM_CFCR2 = pllout / pixclock - 1
unsigned val = pllout / (pixclock * 4) - 1
2009-07-20 01:23:45 +03:00
assert (pllout / (val + 1) <= 150000000)
assert (val <= 0xf)
2009-07-05 11:52:44 +03:00
cpm_set_lcdclk_div (val)
CPM_CFCR |= CPM_CFCR_UPE
2009-10-10 02:31:10 +03:00
#elif defined (NANONOTE)
2009-10-31 10:32:23 +02:00
unsigned val = cpm_get_pllout2 () / pixclock - 1
2009-12-11 10:43:42 +02:00
//assert (val < 0x400)
//cpm_set_pixdiv (val)
//cpm_set_pixdiv (12)
2009-10-31 10:32:23 +02:00
2009-12-11 10:43:42 +02:00
val = cpm_get_pllout2 () / (pixclock * 3) - 1
2009-10-31 10:32:23 +02:00
assert (val < 0x20)
2009-12-11 10:43:42 +02:00
//cpm_set_ldiv (val)
2009-10-10 02:31:10 +03:00
// Update dividers.
2009-12-11 10:43:42 +02:00
//CPM_CPCCR |= CPM_CPCCR_CE
#else
#error "Unknown board"
2009-10-10 02:31:10 +03:00
#endif
2009-07-05 11:52:44 +03:00
cpm_start_lcd ()
2009-10-31 10:32:23 +02:00
#ifdef NANONOTE
2009-12-11 10:43:42 +02:00
// Reset the controller.
2009-12-18 10:00:38 +02:00
write_reg (BACKLIGHT1, 0x1e)
2009-10-31 10:32:23 +02:00
// Enable display.
2010-01-30 10:21:56 +02:00
write_reg (BACKLIGHT1, 0x5e)
2009-12-11 10:43:42 +02:00
// Set data to rgbrgbrgb input, with a delta color filter.
write_reg (COLOR, 0x01)
2009-10-31 10:32:23 +02:00
#endif
2009-06-10 22:42:01 +03:00
2010-01-24 22:34:24 +02:00
LCD_CTRL = (bpp << LCD_CTRL_BPP_BIT) | LCD_CTRL_BST_16 | LCD_CTRL_EOFM
2009-12-11 10:43:42 +02:00
LCD_CFG = cfg
LCD_HSYNC = hpe
LCD_VSYNC = vpe
LCD_VAT = (ht << 16) | vt
LCD_DAH = (hds << 16) | hde
LCD_DAV = (vds << 16) | vde
LCD_DA0 = physical_descriptor
2010-01-24 22:34:24 +02:00
LCD_STATE = 0
LCD_CTRL = (bpp << LCD_CTRL_BPP_BIT) | LCD_CTRL_BST_16 | LCD_CTRL_EOFM | LCD_CTRL_ENA
2009-12-11 10:43:42 +02:00
2009-12-27 01:12:35 +02:00
static void putchar (unsigned x, unsigned y, unsigned ch):
2009-07-23 13:06:32 +03:00
if ch < 32 || ch > 126:
ch = 127
ch -= 32
2009-07-21 13:17:52 +03:00
for unsigned k = 0; k < 6; ++k:
for unsigned r = 0; r < 8; ++r:
2009-12-27 01:12:35 +02:00
LCD_FRAMEBUFFER_BASE[(y * 8 + r) * h + x * 6 + k] = charset[ch][r][k] * 0x00010101
2009-07-21 13:17:52 +03:00
2009-12-26 15:17:06 +02:00
static unsigned log_x = LOG_X_BASE, log_y = LOG_Y_BASE
2009-07-21 13:17:52 +03:00
static void inc_logx ():
2009-10-10 02:31:10 +03:00
if ++log_x >= h / 6:
2009-12-26 15:17:06 +02:00
log_x = LOG_X_BASE
2009-10-10 02:31:10 +03:00
if ++log_y >= v / 8:
2009-12-26 15:17:06 +02:00
log_y = LOG_Y_BASE
2009-07-21 13:17:52 +03:00
2009-07-23 13:06:32 +03:00
static void log_char (unsigned ch):
switch ch:
case '\n':
2009-10-10 02:31:10 +03:00
while log_x < h / 6:
2009-07-23 13:06:32 +03:00
putchar (log_x++, log_y, ' ')
inc_logx ()
break
default:
putchar (log_x, log_y, ch)
inc_logx ()
static void log_str (char const *str):
while *str:
log_char (*str++)
static void log_num (Iris::Num n):
2009-07-23 13:06:32 +03:00
char const *encode = "0123456789abcdef"
log_char ('[')
for unsigned i = 0; i < 8; ++i:
log_char (encode[(n.h >> (4 * (7 - i))) & 0xf])
log_char (' ')
for unsigned i = 0; i < 8; ++i:
log_char (encode[(n.l >> (4 * (7 - i))) & 0xf])
2009-07-23 13:06:32 +03:00
log_char (']')
2009-09-06 12:04:09 +03:00
static void log_msg ():
log_str ("prot:")
log_num (Iris::recv.protected_data)
2009-07-23 13:06:32 +03:00
log_str ("data:")
for unsigned i = 0; i < 2; ++i:
log_num (Iris::recv.data[i])
2009-07-23 13:06:32 +03:00
log_char ('\n')
2009-07-21 13:17:52 +03:00
2009-10-31 10:32:23 +02:00
enum captype:
2009-12-26 15:17:06 +02:00
LOG = 32
BACKLIGHT
2010-01-24 22:34:24 +02:00
LCD
2009-10-31 10:32:23 +02:00
2009-12-18 10:00:38 +02:00
static unsigned spot (unsigned x, unsigned y, unsigned cx, unsigned cy):
unsigned dx2 = (x - cx) * (x - cx)
unsigned dy2 = (y - cy) * (y - cy)
unsigned d2 = dx2 + dy2
unsigned l = 120
if d2 >= l * l:
return 0
return ((l * l - d2 - 1) << 8) / (l * l)
2010-01-31 10:26:23 +02:00
static unsigned pages
static Descriptor descriptor __attribute__ ((aligned (16)))
static bool is_on
static unsigned create (Iris::Memory mem):
2010-01-31 10:26:23 +02:00
unsigned physical = mem.alloc_range (pages)
unsigned address = 0x15000
if physical & ~PAGE_MASK:
Iris::panic (0, "can't allocate framebuffer")
2010-01-31 10:26:23 +02:00
assert (physical & PAGE_MASK && ~physical)
for unsigned i = 0; i < pages; ++i:
Iris::Page p = mem.create_page ()
2010-01-31 10:26:23 +02:00
p.alloc_physical (physical + (i << PAGE_BITS), false, true)
if address != ~0:
mem.map (p, address + (i << PAGE_BITS))
Iris::free_cap (p)
2010-01-31 10:26:23 +02:00
return physical
static void destroy (unsigned physical, Iris::Memory mem):
2010-01-31 10:26:23 +02:00
unsigned address = 0x15000
if physical == ~0:
Iris::panic (0, "unable to destroy framebuffer with wrong cap0")
2010-01-31 10:26:23 +02:00
if descriptor.frame == physical && is_on:
lcd_clr_ena ()
#ifdef NANONOTE
write_reg (BACKLIGHT1, 0x5e)
#endif
if address != ~0:
for unsigned i = 0; i < pages; ++i:
Iris::Page p = mem.mapping ((void *)(address + (i << PAGE_BITS)))
2010-01-31 10:26:23 +02:00
mem.destroy (p)
Iris::free_cap (p)
2010-01-31 10:26:23 +02:00
static void use (unsigned physical):
2010-01-31 10:26:23 +02:00
if physical == ~0:
Iris::panic (0, "unable to use framebuffer with wrong cap0")
2010-01-31 10:26:23 +02:00
bool was_unused = descriptor.frame == 0
descriptor.frame = physical
unsigned dptr = (unsigned)&descriptor
__asm__ volatile ("lw $a0, %0\ncache 0x15, 0($a0)" :: "m"(dptr) : "memory", "a0")
if was_unused && is_on:
lcd_set_ena ()
#ifdef NANONOTE:
write_reg (BACKLIGHT1, 0x5f)
#endif
static void unuse (unsigned physical):
2010-01-31 10:26:23 +02:00
if physical == ~0:
Iris::panic (0, "unable to unuse framebuffer with wrong cap0")
2010-01-31 10:26:23 +02:00
if descriptor.frame == physical:
lcd_clr_ena ()
#ifdef NANONOTE
write_reg (BACKLIGHT1, 0x5e)
#endif
descriptor.frame = 0
Iris::Num start ():
Iris::schedule ()
2009-06-10 23:54:12 +03:00
map_lcd ()
map_cpm ()
2009-10-31 10:32:23 +02:00
#ifdef NANONOTE
map_gpio ()
#endif
2009-06-10 22:42:01 +03:00
2010-01-31 10:26:23 +02:00
pages = (frame_size + ~PAGE_MASK) >> PAGE_BITS
2010-01-24 22:34:24 +02:00
unsigned physical = 0
Iris::Page p = Iris::my_memory.mapping (&descriptor)
2009-10-31 10:32:23 +02:00
unsigned paddr = p.physical_address ()
physical_descriptor = paddr + ((unsigned)&descriptor & ~PAGE_MASK)
Iris::free_cap (p)
2009-07-23 13:06:32 +03:00
descriptor.next = physical_descriptor
descriptor.frame = physical
descriptor.id = 0xdeadbeef
descriptor.cmd = LCD_CMD_EOFINT | ((frame_size / 4) << LCD_CMD_LEN_BIT)
unsigned dptr = (unsigned)&descriptor
__asm__ volatile ("lw $a0, %0\ncache 0x15, 0($a0)" :: "m"(dptr) : "memory", "a0")
2009-08-24 22:02:35 +03:00
reset ()
2009-06-22 17:13:00 +03:00
2009-10-10 02:31:10 +03:00
#if defined (TRENDTAC)
Iris::Cap logcap = Iris::my_receiver.create_capability (LOG)
__asm__ volatile ("li $a0, 1\nlw $a1, %0\nbreak" :: "m"(logcap.code): "a0", "a1", "memory")
2009-10-10 02:31:10 +03:00
#endif
2009-07-23 13:06:32 +03:00
2009-12-26 15:17:06 +02:00
// Register the backlight device.
Iris::Setting backlight = Iris::my_receiver.create_capability (BACKLIGHT)
Iris::my_parent.provide_capability <Iris::Setting> (backlight.copy ())
Iris::free_cap (backlight)
2010-01-24 22:34:24 +02:00
// Register the display device.
2010-05-05 02:09:32 +03:00
Iris::Display display = Iris::my_receiver.create_capability (LCD)
Iris::my_parent.provide_capability <Iris::Display> (display.copy ())
Iris::free_cap (display)
Iris::my_parent.init_done ()
2009-12-26 15:17:06 +02:00
Iris::Cap eof_cb
2009-10-10 02:31:10 +03:00
bool have_eof = false
2010-01-31 10:26:23 +02:00
is_on = true
2009-06-10 22:42:01 +03:00
while true:
Iris::wait ()
2009-12-30 23:41:45 +02:00
//log_msg ()
2010-05-05 02:09:32 +03:00
switch Iris::recv.protected_data.l:
case IRQ_LCD:
have_eof = false
eof_cb.invoke ()
Iris::free_cap (eof_cb)
break
#if defined (TRENDTAC)
case LOG:
log_char (Iris::recv.data[0].l)
2009-12-26 15:17:06 +02:00
break
2010-05-05 02:09:32 +03:00
#endif
2009-12-26 15:17:06 +02:00
case BACKLIGHT:
switch Iris::recv.data[0].l:
2010-05-05 02:09:32 +03:00
case Iris::Device::RESET:
Iris::recv.reply.invoke ()
break
case Iris::Setting::SET:
2009-12-26 15:17:06 +02:00
// TODO
unsigned state = Iris::recv.data[1].l
2009-12-26 15:17:06 +02:00
if !state:
#if defined (NANONOTE)
if is_on:
write_reg (BACKLIGHT1, 0x5e)
is_on = false
#else
#endif
else:
#if defined (NANONOTE)
if !is_on:
write_reg (BACKLIGHT1, 0x5f)
is_on = true
#else
#endif
Iris::recv.reply.invoke ()
2009-12-26 15:17:06 +02:00
break
case Iris::Setting::GET_RANGE:
Iris::recv.reply.invoke (~0)
2009-12-26 15:17:06 +02:00
break
default:
Iris::panic (0, "invalid operation for backlight")
2009-12-26 15:17:06 +02:00
break
2009-06-10 22:42:01 +03:00
break
2010-01-24 22:34:24 +02:00
case LCD:
switch Iris::recv.data[0].l:
2010-05-05 02:09:32 +03:00
case Iris::Device::RESET:
Iris::recv.reply.invoke ()
break
case Iris::Display::SET_EOF_CB:
Iris::Cap reply = Iris::get_reply ()
Iris::Cap arg = Iris::get_arg ()
2010-01-24 22:34:24 +02:00
if have_eof:
Iris::free_cap (eof_cb)
Iris::panic (0, "replacing eof_cb")
2010-01-24 22:34:24 +02:00
else:
lcd_clr_eof ()
Iris::register_interrupt (IRQ_LCD)
2010-01-24 22:34:24 +02:00
have_eof = true
eof_cb = arg
reply.invoke ()
Iris::free_cap (reply)
2010-01-24 22:34:24 +02:00
break
case Iris::Display::MAP_FB:
unsigned addr = Iris::recv.data[1].l
unsigned use = Iris::recv.data[0].h
Iris::Cap reply = Iris::get_reply ()
Iris::Memory mem = Iris::get_arg ()
unsigned physical = mem.alloc_range (pages)
assert (physical & PAGE_MASK && ~physical)
Iris::Caps ret = mem.create_caps (pages / 63 + 1)
unsigned slot = ret.use ()
for unsigned c = 0; c < pages / 63 + 1; ++c:
Iris::Caps caps (Iris::Cap (slot, c))
unsigned num = pages - 63 * c >= 63 ? 63 : pages - 63 * c
Iris::set_recv_arg (caps)
mem.create_caps (num)
unsigned slot2 = caps.use ()
for unsigned i = 0; i < num; ++i:
Iris::Page p = Iris::Cap (slot2, i)
Iris::set_recv_arg (p)
mem.create_page ()
p.alloc_physical (physical + ((63 * c + i) << PAGE_BITS), false, true)
mem.map (p, addr + ((63 * c + i) << PAGE_BITS))
Iris::free_slot (slot2)
Iris::free_slot (slot)
reply.invoke (0, 0, ret.copy ())
Iris::free_cap (ret)
Iris::free_cap (mem)
Iris::free_cap (reply)
if !use:
break
bool was_unused = descriptor.frame == 0
descriptor.frame = physical
unsigned dptr = (unsigned)&descriptor
__asm__ volatile ("lw $a0, %0\ncache 0x15, 0($a0)" :: "m"(dptr) : "memory", "a0")
if was_unused && is_on:
lcd_set_ena ()
#ifdef NANONOTE:
write_reg (BACKLIGHT1, 0x5f)
#endif
break
case Iris::Display::UNMAP_FB:
Iris::panic (0, "unmap_fb isn't implemented yet")
case Iris::Display::GET_INFO:
Iris::panic (0, "get_info isn't implemented yet.")
2010-01-24 22:34:24 +02:00
default:
Iris::panic (Iris::recv.data[0].l, "invalid operation for lcd")
2009-07-23 13:06:32 +03:00
break
2009-09-06 12:04:09 +03:00
default:
2010-05-05 02:09:32 +03:00
Iris::panic (0, "invalid operation type for lcd")
2009-06-10 22:42:01 +03:00
break