49 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/errno.h>
53 #include <linux/string.h>
61 #include <linux/pci.h>
67 #ifdef CONFIG_PPC_PMAC
68 #include <asm/machdep.h>
71 #include <asm/pci-bridge.h>
72 #include "../macmodes.h"
75 #ifdef CONFIG_PMAC_BACKLIGHT
79 #ifdef CONFIG_BOOTX_TEXT
80 #include <asm/btext.h>
93 #define DBG(fmt, args...) \
94 printk(KERN_DEBUG "aty128fb: %s " fmt, __func__, ##args);
96 #define DBG(fmt, args...)
99 #ifndef CONFIG_PPC_PMAC
103 640, 480, 640, 480, 0, 0, 8, 0,
104 {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
105 0, 0, -1, -1, 0, 39722, 48, 16, 33, 10, 96, 2,
114 1024, 768, 1024, 768, 0, 0, 8, 0,
115 {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
116 0, 0, -1, -1, 0, 12699, 160, 32, 28, 1, 96, 3,
124 static struct fb_videomode defaultmode __devinitdata = {
166 static int aty128_probe(
struct pci_dev *pdev,
168 static void aty128_remove(
struct pci_dev *pdev);
170 static int aty128_pci_resume(
struct pci_dev *pdev);
171 static int aty128_do_resume(
struct pci_dev *pdev);
276 .id_table = aty128_pci_tbl,
277 .probe = aty128_probe,
279 .suspend = aty128_pci_suspend,
280 .resume = aty128_pci_resume,
328 { 4, 4, 3, 3, 1, 3, 1, 16, 30, 16,
"128-bit SDR SGRAM (1:1)" };
330 { 4, 8, 3, 3, 1, 3, 1, 17, 46, 17,
"64-bit SDR SGRAM (1:1)" };
332 { 4, 4, 1, 2, 1, 2, 1, 16, 24, 16,
"64-bit SDR SGRAM (2:1)" };
334 { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16,
"64-bit DDR SGRAM" };
346 static char *mode_option __devinitdata =
NULL;
348 #ifdef CONFIG_PPC_PMAC
350 static int default_cmode __devinitdata =
CMODE_8;
353 static int default_crt_on __devinitdata = 0;
354 static int default_lcd_on __devinitdata = 1;
357 static bool mtrr =
true;
360 #ifdef CONFIG_PMAC_BACKLIGHT
361 static int backlight __devinitdata = 1;
363 static int backlight __devinitdata = 0;
411 struct {
int vram;
int vram_valid; } mtrr;
430 #define round_div(n, d) ((n+(d/2))/d)
439 static int aty128fb_blank(
int blank,
struct fb_info *
fb);
466 #ifdef CONFIG_FB_ATY128_BACKLIGHT
467 static void aty128_bl_set_power(
struct fb_info *
info,
int power);
470 #define BIOS_IN8(v) (readb(bios + (v)))
471 #define BIOS_IN16(v) (readb(bios + (v)) | \
472 (readb(bios + (v) + 1) << 8))
473 #define BIOS_IN32(v) (readb(bios + (v)) | \
474 (readb(bios + (v) + 1) << 8) | \
475 (readb(bios + (v) + 2) << 16) | \
476 (readb(bios + (v) + 3) << 24))
479 static struct fb_ops aty128fb_ops = {
481 .fb_check_var = aty128fb_check_var,
482 .fb_set_par = aty128fb_set_par,
483 .fb_setcolreg = aty128fb_setcolreg,
484 .fb_pan_display = aty128fb_pan_display,
485 .fb_blank = aty128fb_blank,
486 .fb_ioctl = aty128fb_ioctl,
487 .fb_sync = aty128fb_sync,
498 static inline u32 _aty_ld_le32(
volatile unsigned int regindex,
504 static inline void _aty_st_le32(
volatile unsigned int regindex,
u32 val,
510 static inline u8 _aty_ld_8(
unsigned int regindex,
516 static inline void _aty_st_8(
unsigned int regindex,
u8 val,
522 #define aty_ld_le32(regindex) _aty_ld_le32(regindex, par)
523 #define aty_st_le32(regindex, val) _aty_st_le32(regindex, val, par)
524 #define aty_ld_8(regindex) _aty_ld_8(regindex, par)
525 #define aty_st_8(regindex, val) _aty_st_8(regindex, val, par)
531 #define aty_ld_pll(pll_index) _aty_ld_pll(pll_index, par)
532 #define aty_st_pll(pll_index, val) _aty_st_pll(pll_index, val, par)
535 static u32 _aty_ld_pll(
unsigned int pll_index,
543 static void _aty_st_pll(
unsigned int pll_index,
u32 val,
552 static int aty_pll_readupdate(
const struct aty128fb_par *par)
558 static void aty_pll_wait_readupdate(
const struct aty128fb_par *par)
564 if (aty_pll_readupdate(par)) {
575 static void aty_pll_writeupdate(
const struct aty128fb_par *par)
577 aty_pll_wait_readupdate(par);
613 for (i = 0; i < 2000000; i++) {
618 aty128_reset_engine(par);
627 do_wait_for_fifo(64, par);
630 for (i = 0; i < 2000000; i++) {
632 aty128_flush_pixel_cache(par);
637 aty128_reset_engine(par);
645 do_wait_for_fifo(64, par);
650 static void aty128_flush_pixel_cache(
const struct aty128fb_par *par)
660 for (i = 0; i < 2000000; i++)
666 static void aty128_reset_engine(
const struct aty128fb_par *par)
668 u32 gen_reset_cntl, clock_cntl_index, mclk_cntl;
670 aty128_flush_pixel_cache(par);
694 static void aty128_init_engine(
struct aty128fb_par *par)
701 wait_for_fifo(1, par);
704 aty128_reset_engine(par);
706 pitch_value = par->
crtc.pitch;
707 if (par->
crtc.bpp == 24) {
708 pitch_value = pitch_value * 3;
711 wait_for_fifo(4, par);
728 (depth_to_dst(par->
crtc.depth) << 8) |
739 wait_for_fifo(8, par);
766 else if (depth <= 15)
768 else if (depth == 16)
770 else if (depth <= 24)
772 else if (depth <= 32)
842 if (
BIOS_IN32(dptr) != ((
'R' << 24) | (
'I' << 16) | (
'C' << 8) |
'P')) {
874 unsigned int bios_hdr;
875 unsigned int bios_pll;
886 DBG(
"ppll_max %d ppll_min %d xclk %d ref_divider %d ref clock %d\n",
904 for (segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
905 rom_base =
ioremap(segstart, 0x10000);
906 if (rom_base ==
NULL)
908 if (
readb(rom_base) == 0x55 &&
readb(rom_base + 1) == 0xaa)
926 u32 x_mpll_ref_fb_div;
929 unsigned PostDivSet[] = { 0, 1, 2, 4, 8, 3, 6, 12 };
938 Nx = (x_mpll_ref_fb_div & 0x00ff00) >> 8;
939 M = x_mpll_ref_fb_div & 0x0000ff;
942 (M * PostDivSet[xclk_cntl]));
952 aty_pll_writeupdate(par);
955 aty_pll_writeupdate(par);
973 par->
mem = &sdr_sgram;
976 par->
mem = &ddr_sgram;
979 par->
mem = &sdr_sgram;
1010 u32 xres, yres, vxres, vyres, xoffset, yoffset,
bpp,
dst;
1012 u32 h_total, h_disp, h_sync_strt, h_sync_wid, h_sync_pol;
1013 u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
1015 u8 mode_bytpp[7] = { 0, 0, 1, 2, 2, 3, 4 };
1037 depth = (var->
green.length == 6) ? 16 : 15;
1045 xres = (xres + 7) & ~7;
1046 xoffset = (xoffset + 7) & ~7;
1048 if (vxres < xres + xoffset)
1049 vxres = xres + xoffset;
1051 if (vyres < yres + yoffset)
1052 vyres = yres + yoffset;
1055 dst = depth_to_dst(depth);
1063 bytpp = mode_bytpp[
dst];
1071 h_disp = (xres >> 3) - 1;
1072 h_total = (((xres + right + hslen +
left) >> 3) - 1) & 0xFFFFL;
1075 v_total = (yres + upper + vslen + lower - 1) & 0xFFFFL;
1078 if (((h_total >> 3) - 1) > 0x1ff || (v_total - 1) > 0x7FF) {
1083 h_sync_wid = (hslen + 7) >> 3;
1084 if (h_sync_wid == 0)
1086 else if (h_sync_wid > 0x3f)
1089 h_sync_strt = (h_disp << 3) + right;
1092 if (v_sync_wid == 0)
1094 else if (v_sync_wid > 0x1f)
1097 v_sync_strt = v_disp + lower;
1104 crtc->
gen_cntl = 0x3000000
L | c_sync | (dst << 8);
1106 crtc->
h_total = h_total | (h_disp << 16);
1107 crtc->
v_total = v_total | (v_disp << 16);
1114 crtc->
pitch = vxres >> 3;
1123 crtc->
vxres = vxres;
1124 crtc->
vyres = vyres;
1127 crtc->
depth = depth;
1134 static int aty128_pix_width_to_var(
int pix_width,
struct fb_var_screeninfo *var)
1138 var->
red.msb_right = 0;
1139 var->
green.msb_right = 0;
1140 var->
blue.offset = 0;
1141 var->
blue.msb_right = 0;
1144 var->
transp.msb_right = 0;
1145 switch (pix_width) {
1148 var->
red.offset = 0;
1149 var->
red.length = 8;
1150 var->
green.offset = 0;
1151 var->
green.length = 8;
1152 var->
blue.length = 8;
1156 var->
red.offset = 10;
1157 var->
red.length = 5;
1158 var->
green.offset = 5;
1159 var->
green.length = 5;
1160 var->
blue.length = 5;
1164 var->
red.offset = 11;
1165 var->
red.length = 5;
1166 var->
green.offset = 5;
1167 var->
green.length = 6;
1168 var->
blue.length = 5;
1172 var->
red.offset = 16;
1173 var->
red.length = 8;
1174 var->
green.offset = 8;
1175 var->
green.length = 8;
1176 var->
blue.length = 8;
1180 var->
red.offset = 16;
1181 var->
red.length = 8;
1182 var->
green.offset = 8;
1183 var->
green.length = 8;
1184 var->
blue.length = 8;
1197 static int aty128_crtc_to_var(
const struct aty128_crtc *crtc,
1201 u32 h_total, h_disp, h_sync_strt, h_sync_dly, h_sync_wid, h_sync_pol;
1202 u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
1206 h_total = crtc->
h_total & 0x1ff;
1207 h_disp = (crtc->
h_total >> 16) & 0xff;
1212 v_total = crtc->
v_total & 0x7ff;
1213 v_disp = (crtc->
v_total >> 16) & 0x7ff;
1221 xres = (h_disp + 1) << 3;
1223 left = ((h_total - h_sync_strt - h_sync_wid) << 3) - h_sync_dly;
1224 right = ((h_sync_strt - h_disp) << 3) + h_sync_dly;
1225 hslen = h_sync_wid << 3;
1226 upper = v_total - v_sync_strt - v_sync_wid;
1227 lower = v_sync_strt - v_disp;
1233 aty128_pix_width_to_var(pix_width, var);
1253 static void aty128_set_crt_enable(
struct aty128fb_par *par,
int on)
1265 static void aty128_set_lcd_enable(
struct aty128fb_par *par,
int on)
1268 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1277 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1281 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1298 unsigned char post_conv[] =
1299 { 2, 0, 1, 4, 2, 2, 6, 2, 3, 2, 2, 2, 7 };
1309 aty_pll_wait_readupdate(par);
1311 aty_pll_writeupdate(par);
1320 aty_pll_wait_readupdate(par);
1322 aty_pll_writeupdate(par);
1324 aty_pll_wait_readupdate(par);
1326 aty_pll_writeupdate(par);
1333 static int aty128_var_to_pll(
u32 period_in_ps,
struct aty128_pll *pll,
1337 unsigned char post_dividers[] = {1,2,4,8,3,6,12};
1343 vclk = 100000000 / period_in_ps;
1352 for (i = 0; i <
ARRAY_SIZE(post_dividers); i++) {
1353 output_freq = post_dividers[
i] *
vclk;
1370 DBG(
"post %d feedback %d vlck %d output %d ref_divider %d "
1379 static int aty128_pll_to_var(
const struct aty128_pll *pll,
1409 bpp = (depth+7) & ~7;
1416 3 * ((m->
Trcd - 2 > 0) ? m->
Trcd - 2 : 0) +
1436 roff = x * (fifo_depth - 4);
1438 if ((ron + m->
Rloop) >= roff) {
1443 DBG(
"p: %x rloop: %x x: %x ron: %x roff: %x\n",
1444 p, m->
Rloop, x, ron, roff);
1456 static int aty128fb_set_par(
struct fb_info *info)
1462 if ((err = aty128_decode_var(&info->
var, par)) != 0)
1484 aty128_set_crtc(&par->
crtc, par);
1485 aty128_set_pll(&par->
pll, par);
1486 aty128_set_fifo(&par->
fifo_reg, par);
1490 #if defined(__BIG_ENDIAN)
1491 if (par->
crtc.bpp == 32)
1493 else if (par->
crtc.bpp == 16)
1500 info->
fix.line_length = (par->
crtc.vxres * par->
crtc.bpp) >> 3;
1505 aty128_set_crt_enable(par, par->
crt_on);
1506 aty128_set_lcd_enable(par, par->
lcd_on);
1509 aty128_init_engine(par);
1511 #ifdef CONFIG_BOOTX_TEXT
1513 (((par->
crtc.h_total>>16) & 0xff)+1)*8,
1514 ((par->
crtc.v_total>>16) & 0x7ff)+1,
1534 if ((err = aty128_var_to_crtc(var, &crtc, par)))
1537 if ((err = aty128_var_to_pll(var->
pixclock, &pll, par)))
1557 if ((err = aty128_crtc_to_var(&par->
crtc, var)))
1560 if ((err = aty128_pll_to_var(&par->
pll, var)))
1581 if ((err = aty128_decode_var(var, &par)) != 0)
1583 aty128_encode_var(var, &par);
1595 u32 xoffset, yoffset;
1599 xres = (((par->
crtc.h_total >> 16) & 0xff) + 1) << 3;
1600 yres = ((par->
crtc.v_total >> 16) & 0x7ff) + 1;
1602 xoffset = (var->
xoffset +7) & ~7;
1605 if (xoffset+xres > par->
crtc.vxres || yoffset+yres > par->
crtc.vyres)
1608 par->
crtc.xoffset = xoffset;
1609 par->
crtc.yoffset = yoffset;
1611 offset = ((yoffset * par->
crtc.vxres + xoffset) * (par->
crtc.bpp >> 3))
1614 if (par->
crtc.bpp == 24)
1615 offset += 8 * (offset % 3);
1651 static int aty128fb_sync(
struct fb_info *info)
1665 if (!options || !*options)
1668 while ((this_opt =
strsep(&options,
",")) !=
NULL) {
1669 if (!
strncmp(this_opt,
"lcd:", 4)) {
1672 }
else if (!
strncmp(this_opt,
"crt:", 4)) {
1675 }
else if (!
strncmp(this_opt,
"backlight:", 10)) {
1680 if(!
strncmp(this_opt,
"nomtrr", 6)) {
1685 #ifdef CONFIG_PPC_PMAC
1687 if (!
strncmp(this_opt,
"vmode:", 6)) {
1690 default_vmode = vmode;
1692 }
else if (!
strncmp(this_opt,
"cmode:", 6)) {
1711 mode_option = this_opt;
1718 #ifdef CONFIG_FB_ATY128_BACKLIGHT
1719 #define MAX_LEVEL 0xFF
1721 static int aty128_bl_get_level_brightness(
struct aty128fb_par *par,
1724 struct fb_info *info = pci_get_drvdata(par->
pdev);
1744 #define BACKLIGHT_LVDS_OFF
1746 #undef BACKLIGHT_DAC_OFF
1759 level = bd->
props.brightness;
1773 reg |= (aty128_bl_get_level_brightness(par, level) <<
1775 #ifdef BACKLIGHT_LVDS_OFF
1780 #ifdef BACKLIGHT_DAC_OFF
1785 reg |= (aty128_bl_get_level_brightness(par, 0) <<
1787 #ifdef BACKLIGHT_LVDS_OFF
1795 #ifdef BACKLIGHT_DAC_OFF
1805 return bd->
props.brightness;
1810 .update_status = aty128_bl_update_status,
1813 static void aty128_bl_set_power(
struct fb_info *info,
int power)
1816 info->bl_dev->props.power = power;
1817 backlight_update_status(info->bl_dev);
1824 struct fb_info *info = pci_get_drvdata(par->
pdev);
1832 #ifdef CONFIG_PMAC_BACKLIGHT
1837 snprintf(name,
sizeof(name),
"aty128bl%d", info->
node);
1841 props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
1845 info->bl_dev =
NULL;
1855 bd->
props.brightness = bd->
props.max_brightness;
1857 backlight_update_status(bd);
1859 printk(
"aty128: Backlight initialized (%s)\n", name);
1870 printk(
"aty128: Backlight unloaded\n");
1878 #ifdef CONFIG_PPC_PMAC__disabled
1879 static void aty128_early_resume(
void *
data)
1886 aty128_do_resume(par->
pdev);
1894 struct fb_info *info = pci_get_drvdata(pdev);
1897 char video_card[50];
1904 strcpy(video_card,
"Rage128 XX ");
1905 video_card[8] = ent->
device >> 8;
1906 video_card[9] = ent->
device & 0xFF;
1911 sizeof(video_card));
1913 printk(
KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
1915 if (par->
vram_size % (1024 * 1024) == 0)
1923 info->
fbops = &aty128fb_ops;
1926 par->
lcd_on = default_lcd_on;
1927 par->
crt_on = default_crt_on;
1930 #ifdef CONFIG_PPC_PMAC
1931 if (machine_is(powermac)) {
1934 pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE,
NULL, 0, 1);
1950 if (default_vmode <= 0 || default_vmode >
VMODE_MAX)
1976 if (default_cmode > 16)
1978 else if (default_cmode > 8)
1991 0, &defaultmode, 8) == 0)
1998 if (aty128fb_check_var(&var, info)) {
2019 aty128_init_engine(par);
2026 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2028 aty128_bl_init(par);
2035 info->
node, info->
fix.id, video_card);
2101 info->
fix = aty128fb_fix;
2102 info->
fix.smem_start = fb_addr;
2113 bios = aty128_map_ROM(par, pdev);
2116 bios = aty128_find_mem_vbios(par);
2122 aty128_get_pllinfo(par, bios);
2127 aty128_timings(par);
2128 pci_set_drvdata(pdev, info);
2130 if (!aty128_init(pdev, ent))
2137 par->mtrr.vram_valid = 1;
2161 struct fb_info *info = pci_get_drvdata(pdev);
2171 #ifdef CONFIG_FB_ATY128_BACKLIGHT
2172 aty128_bl_exit(info->bl_dev);
2176 if (par->mtrr.vram_valid)
2196 static int aty128fb_blank(
int blank,
struct fb_info *fb)
2225 aty128_set_crt_enable(par, par->
crt_on && !blank);
2226 aty128_set_lcd_enable(par, par->
lcd_on && !blank);
2243 || (par->
crtc.depth == 16 && regno > 63)
2244 || (par->
crtc.depth == 15 && regno > 31))
2255 switch (par->
crtc.depth) {
2257 pal[regno] = (regno << 10) | (regno << 5) | regno;
2260 pal[regno] = (regno << 11) | (regno << 6) | regno;
2263 pal[regno] = (regno << 16) | (regno << 8) | regno;
2266 i = (regno << 8) | regno;
2267 pal[regno] = (i << 16) | i;
2272 if (par->
crtc.depth == 16 && regno > 0) {
2284 aty128_st_pal(regno * 8, red, par->
green[regno*2],
2287 red = par->
red[regno/2];
2288 blue = par->
blue[regno/2];
2290 }
else if (par->
crtc.bpp == 16)
2292 aty128_st_pal(regno, red, green, blue, par);
2297 #define ATY_MIRROR_LCD_ON 0x00000001
2298 #define ATY_MIRROR_CRT_ON 0x00000002
2301 #define FBIO_ATY128_GET_MIRROR _IOR('@', 1, __u32)
2303 #define FBIO_ATY128_SET_MIRROR _IOW('@', 2, __u32)
2318 par->
lcd_on = (value & 0x01) != 0;
2319 par->
crt_on = (value & 0x02) != 0;
2322 aty128_set_crt_enable(par, par->
crt_on);
2323 aty128_set_lcd_enable(par, par->
lcd_on);
2339 static inline void aty128_rectcopy(
int srcx,
int srcy,
int dstx,
int dsty,
2341 struct fb_info_aty128 *par)
2343 u32 save_dp_datatype, save_dp_cntl, dstval;
2345 if (!width || !height)
2348 dstval = depth_to_dst(par->current_par.crtc.depth);
2353 }
else if (dstval == -
EINVAL) {
2354 printk(
"aty128fb: invalid depth or RGBA\n");
2358 wait_for_fifo(2, par);
2362 wait_for_fifo(6, par);
2371 par->blitter_may_be_busy = 1;
2373 wait_for_fifo(2, par);
2383 static void fbcon_aty128_bmove(
struct display *p,
int sy,
int sx,
int dy,
2384 int dx,
int height,
int width)
2387 sy *= fontheight(p);
2389 dy *= fontheight(p);
2390 width *= fontwidth(p);
2391 height *= fontheight(p);
2393 aty128_rectcopy(sx, sy, dx, dy, width, height,
2394 (
struct fb_info_aty128 *)p->fb_info);
2436 struct fb_info *info = pci_get_drvdata(pdev);
2450 #ifndef CONFIG_PPC_PMAC
2459 if (state.
event == pdev->
dev.power.power_state.event)
2470 aty128_reset_engine(par);
2480 #ifdef CONFIG_PPC_PMAC
2494 aty128_set_suspend(par, 1);
2498 pdev->
dev.power.power_state =
state;
2503 static int aty128_do_resume(
struct pci_dev *pdev)
2505 struct fb_info *info = pci_get_drvdata(pdev);
2517 aty128_set_suspend(par, 0);
2521 aty128_reset_engine(par);
2523 aty128fb_set_par(info);
2532 aty128fb_blank(0, info);
2534 #ifdef CONFIG_PPC_PMAC
2549 static int aty128_pci_resume(
struct pci_dev *pdev)
2554 rc = aty128_do_resume(pdev);
2561 static int __devinit aty128fb_init(
void)
2568 aty128fb_setup(option);
2571 return pci_register_driver(&aty128fb_driver);
2574 static void __exit aty128fb_exit(
void)
2587 MODULE_PARM_DESC(mode_option,
"Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
2590 MODULE_PARM_DESC(nomtrr,
"bool: Disable MTRR support (0 or 1=disabled) (default=0)");