2 * linux/drivers/video/pmagb-b-fb.c
4 * PMAGB-B TURBOchannel Smart Frame Buffer (SFB) card support,
6 * "HP300 Topcat framebuffer support (derived from macfb of all things)
7 * Phil Blundell <philb@gnu.org> 1998", the original code can be
8 * found in the file hpfb.c in the same directory.
10 * DECstation related code Copyright (C) 1999, 2000, 2001 by
11 * Michael Engel <engel@unix-ag.org>,
12 * Karsten Merker <merker@linuxtag.org> and
14 * Copyright (c) 2005 Maciej W. Rozycki
16 * This file is subject to the terms and conditions of the GNU General
17 * Public License. See the file COPYING in the main directory of this
18 * archive for more details.
21 #include <linux/compiler.h>
22 #include <linux/delay.h>
23 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/types.h>
32 #include <asm/system.h>
34 #include <asm/dec/tc.h>
36 #include <video/pmagb-b-fb.h>
41 volatile void __iomem *mmio;
42 volatile void __iomem *smem;
43 volatile u32 __iomem *sfb;
44 volatile u32 __iomem *dac;
51 static struct fb_info *root_pmagbbfb_dev;
53 static struct fb_var_screeninfo pmagbbfb_defined __initdata = {
58 .activate = FB_ACTIVATE_NOW,
61 .accel_flags = FB_ACCEL_NONE,
62 .sync = FB_SYNC_ON_GREEN,
63 .vmode = FB_VMODE_NONINTERLACED,
66 static struct fb_fix_screeninfo pmagbbfb_fix __initdata = {
68 .smem_len = (2048 * 1024),
69 .type = FB_TYPE_PACKED_PIXELS,
70 .visual = FB_VISUAL_PSEUDOCOLOR,
71 .mmio_len = PMAGB_B_FBMEM,
75 static inline void sfb_write(struct pmagbbfb_par *par, unsigned int reg, u32 v)
77 writel(v, par->sfb + reg / 4);
80 static inline u32 sfb_read(struct pmagbbfb_par *par, unsigned int reg)
82 return readl(par->sfb + reg / 4);
85 static inline void dac_write(struct pmagbbfb_par *par, unsigned int reg, u8 v)
87 writeb(v, par->dac + reg / 4);
90 static inline u8 dac_read(struct pmagbbfb_par *par, unsigned int reg)
92 return readb(par->dac + reg / 4);
95 static inline void gp0_write(struct pmagbbfb_par *par, u32 v)
97 writel(v, par->mmio + PMAGB_B_GP0);
104 static int pmagbbfb_setcolreg(unsigned int regno, unsigned int red,
105 unsigned int green, unsigned int blue,
106 unsigned int transp, struct fb_info *info)
108 struct pmagbbfb_par *par = info->par;
110 BUG_ON(regno >= info->cmap.len);
112 red >>= 8; /* The cmap fields are 16 bits */
113 green >>= 8; /* wide, but the hardware colormap */
114 blue >>= 8; /* registers are only 8 bits wide */
117 dac_write(par, BT459_ADDR_LO, regno);
118 dac_write(par, BT459_ADDR_HI, 0x00);
120 dac_write(par, BT459_CMAP, red);
122 dac_write(par, BT459_CMAP, green);
124 dac_write(par, BT459_CMAP, blue);
129 static struct fb_ops pmagbbfb_ops = {
130 .owner = THIS_MODULE,
131 .fb_setcolreg = pmagbbfb_setcolreg,
132 .fb_fillrect = cfb_fillrect,
133 .fb_copyarea = cfb_copyarea,
134 .fb_imageblit = cfb_imageblit,
139 * Turn the hardware cursor off.
141 static void __init pmagbbfb_erase_cursor(struct fb_info *info)
143 struct pmagbbfb_par *par = info->par;
146 dac_write(par, BT459_ADDR_LO, 0x00);
147 dac_write(par, BT459_ADDR_HI, 0x03);
149 dac_write(par, BT459_DATA, 0x00);
153 * Set up screen parameters.
155 static void __init pmagbbfb_screen_setup(struct fb_info *info)
157 struct pmagbbfb_par *par = info->par;
159 info->var.xres = ((sfb_read(par, SFB_REG_VID_HOR) >>
160 SFB_VID_HOR_PIX_SHIFT) & SFB_VID_HOR_PIX_MASK) * 4;
161 info->var.xres_virtual = info->var.xres;
162 info->var.yres = (sfb_read(par, SFB_REG_VID_VER) >>
163 SFB_VID_VER_SL_SHIFT) & SFB_VID_VER_SL_MASK;
164 info->var.yres_virtual = info->var.yres;
165 info->var.left_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
166 SFB_VID_HOR_BP_SHIFT) &
167 SFB_VID_HOR_BP_MASK) * 4;
168 info->var.right_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
169 SFB_VID_HOR_FP_SHIFT) &
170 SFB_VID_HOR_FP_MASK) * 4;
171 info->var.upper_margin = (sfb_read(par, SFB_REG_VID_VER) >>
172 SFB_VID_VER_BP_SHIFT) & SFB_VID_VER_BP_MASK;
173 info->var.lower_margin = (sfb_read(par, SFB_REG_VID_VER) >>
174 SFB_VID_VER_FP_SHIFT) & SFB_VID_VER_FP_MASK;
175 info->var.hsync_len = ((sfb_read(par, SFB_REG_VID_HOR) >>
176 SFB_VID_HOR_SYN_SHIFT) &
177 SFB_VID_HOR_SYN_MASK) * 4;
178 info->var.vsync_len = (sfb_read(par, SFB_REG_VID_VER) >>
179 SFB_VID_VER_SYN_SHIFT) & SFB_VID_VER_SYN_MASK;
181 info->fix.line_length = info->var.xres;
185 * Determine oscillator configuration.
187 static void __init pmagbbfb_osc_setup(struct fb_info *info)
189 static unsigned int pmagbbfb_freqs[] __initdata = {
190 130808, 119843, 104000, 92980, 74367, 72800,
191 69197, 66000, 65000, 50350, 36000, 32000, 25175
193 struct pmagbbfb_par *par = info->par;
194 u32 count0 = 8, count1 = 8, counttc = 16 * 256 + 8;
195 u32 freq0, freq1, freqtc = get_tc_speed() / 250;
198 gp0_write(par, 0); /* select Osc0 */
199 for (j = 0; j < 16; j++) {
201 sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
203 for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
204 if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
208 count0 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
211 gp0_write(par, 1); /* select Osc1 */
212 for (j = 0; j < 16; j++) {
214 sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
216 for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
217 if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
221 count1 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
224 freq0 = (freqtc * count0 + counttc / 2) / counttc;
226 if (freq0 >= pmagbbfb_freqs[0] - (pmagbbfb_freqs[0] + 32) / 64 &&
227 freq0 <= pmagbbfb_freqs[0] + (pmagbbfb_freqs[0] + 32) / 64)
228 par->osc0 = pmagbbfb_freqs[0];
230 freq1 = (par->osc0 * count1 + count0 / 2) / count0;
232 for (i = 0; i < sizeof(pmagbbfb_freqs) / sizeof(*pmagbbfb_freqs); i++)
233 if (freq1 >= pmagbbfb_freqs[i] -
234 (pmagbbfb_freqs[i] + 128) / 256 &&
235 freq1 <= pmagbbfb_freqs[i] +
236 (pmagbbfb_freqs[i] + 128) / 256) {
237 par->osc1 = pmagbbfb_freqs[i];
241 if (par->osc0 - par->osc1 <= (par->osc0 + par->osc1 + 256) / 512 ||
242 par->osc1 - par->osc0 <= (par->osc0 + par->osc1 + 256) / 512)
245 gp0_write(par, par->osc1 != 0); /* reselect OscX */
247 info->var.pixclock = par->osc1 ?
248 (1000000000 + par->osc1 / 2) / par->osc1 :
249 (1000000000 + par->osc0 / 2) / par->osc0;
253 static int __init pmagbbfb_init_one(int slot)
255 char freq0[12], freq1[12];
256 struct fb_info *info;
257 struct pmagbbfb_par *par;
258 unsigned long base_addr;
261 info = framebuffer_alloc(sizeof(struct pmagbbfb_par), NULL);
267 claim_tc_card(par->slot);
269 base_addr = get_tc_base_addr(par->slot);
271 par->next = root_pmagbbfb_dev;
272 root_pmagbbfb_dev = info;
274 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
277 info->fbops = &pmagbbfb_ops;
278 info->fix = pmagbbfb_fix;
279 info->var = pmagbbfb_defined;
280 info->flags = FBINFO_DEFAULT;
282 /* MMIO mapping setup. */
283 info->fix.mmio_start = base_addr;
284 par->mmio = ioremap_nocache(info->fix.mmio_start, info->fix.mmio_len);
287 par->sfb = par->mmio + PMAGB_B_SFB;
288 par->dac = par->mmio + PMAGB_B_BT459;
290 /* Frame buffer mapping setup. */
291 info->fix.smem_start = base_addr + PMAGB_B_FBMEM;
292 par->smem = ioremap_nocache(info->fix.smem_start, info->fix.smem_len);
295 vid_base = sfb_read(par, SFB_REG_VID_BASE);
296 info->screen_base = (void __iomem *)par->smem + vid_base * 0x1000;
297 info->screen_size = info->fix.smem_len - 2 * vid_base * 0x1000;
299 pmagbbfb_erase_cursor(info);
300 pmagbbfb_screen_setup(info);
301 pmagbbfb_osc_setup(info);
303 if (register_framebuffer(info) < 0)
306 snprintf(freq0, sizeof(freq0), "%u.%03uMHz",
307 par->osc0 / 1000, par->osc0 % 1000);
308 snprintf(freq1, sizeof(freq1), "%u.%03uMHz",
309 par->osc1 / 1000, par->osc1 % 1000);
311 pr_info("fb%d: %s frame buffer device in slot %d\n",
312 info->node, info->fix.id, par->slot);
313 pr_info("fb%d: Osc0: %s, Osc1: %s, Osc%u selected\n",
314 info->node, freq0, par->osc1 ? freq1 : "disabled",
327 fb_dealloc_cmap(&info->cmap);
330 root_pmagbbfb_dev = par->next;
331 release_tc_card(par->slot);
332 framebuffer_release(info);
336 static void __exit pmagbbfb_exit_one(void)
338 struct fb_info *info = root_pmagbbfb_dev;
339 struct pmagbbfb_par *par = info->par;
341 unregister_framebuffer(info);
344 fb_dealloc_cmap(&info->cmap);
345 root_pmagbbfb_dev = par->next;
346 release_tc_card(par->slot);
347 framebuffer_release(info);
352 * Initialise the framebuffer.
354 static int __init pmagbbfb_init(void)
359 if (fb_get_options("pmagbbfb", NULL))
362 while ((slot = search_tc_card("PMAGB-BA")) >= 0) {
363 if (pmagbbfb_init_one(slot) < 0)
367 return (count > 0) ? 0 : -ENXIO;
370 static void __exit pmagbbfb_exit(void)
372 while (root_pmagbbfb_dev)
377 module_init(pmagbbfb_init);
378 module_exit(pmagbbfb_exit);
380 MODULE_LICENSE("GPL");