blob: a483b13e117b71f93eab7b0e042075b481adf547 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle7901c792005-09-03 15:56:11 -07002 * linux/drivers/video/pmagb-b-fb.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Ralf Baechle7901c792005-09-03 15:56:11 -07004 * PMAGB-B TURBOchannel Smart Frame Buffer (SFB) card support,
5 * derived from:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * "HP300 Topcat framebuffer support (derived from macfb of all things)
7 * Phil Blundell <philb@gnu.org> 1998", the original code can be
Ralf Baechle7901c792005-09-03 15:56:11 -07008 * found in the file hpfb.c in the same directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Ralf Baechle7901c792005-09-03 15:56:11 -070010 * DECstation related code Copyright (C) 1999, 2000, 2001 by
11 * Michael Engel <engel@unix-ag.org>,
12 * Karsten Merker <merker@linuxtag.org> and
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Harald Koerfgen.
Ralf Baechle7901c792005-09-03 15:56:11 -070014 * Copyright (c) 2005 Maciej W. Rozycki
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Ralf Baechle7901c792005-09-03 15:56:11 -070016 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Ralf Baechle7901c792005-09-03 15:56:11 -070021#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
Ralf Baechle7901c792005-09-03 15:56:11 -070023#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fb.h>
Ralf Baechle7901c792005-09-03 15:56:11 -070025#include <linux/init.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/types.h>
29
30#include <asm/bug.h>
31#include <asm/io.h>
32#include <asm/system.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/dec/tc.h>
Ralf Baechle7901c792005-09-03 15:56:11 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <video/pmagb-b-fb.h>
37
Ralf Baechle7901c792005-09-03 15:56:11 -070038
39struct pmagbbfb_par {
40 struct fb_info *next;
41 volatile void __iomem *mmio;
42 volatile void __iomem *smem;
43 volatile u32 __iomem *sfb;
44 volatile u32 __iomem *dac;
45 unsigned int osc0;
46 unsigned int osc1;
47 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Ralf Baechle7901c792005-09-03 15:56:11 -070051static struct fb_info *root_pmagbbfb_dev;
52
53static struct fb_var_screeninfo pmagbbfb_defined __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .bits_per_pixel = 8,
55 .red.length = 8,
56 .green.length = 8,
57 .blue.length = 8,
58 .activate = FB_ACTIVATE_NOW,
Ralf Baechle7901c792005-09-03 15:56:11 -070059 .height = -1,
60 .width = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .accel_flags = FB_ACCEL_NONE,
Ralf Baechle7901c792005-09-03 15:56:11 -070062 .sync = FB_SYNC_ON_GREEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .vmode = FB_VMODE_NONINTERLACED,
64};
65
Ralf Baechle7901c792005-09-03 15:56:11 -070066static struct fb_fix_screeninfo pmagbbfb_fix __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .id = "PMAGB-BA",
Ralf Baechle7901c792005-09-03 15:56:11 -070068 .smem_len = (2048 * 1024),
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .type = FB_TYPE_PACKED_PIXELS,
70 .visual = FB_VISUAL_PSEUDOCOLOR,
Ralf Baechle7901c792005-09-03 15:56:11 -070071 .mmio_len = PMAGB_B_FBMEM,
72};
73
74
75static inline void sfb_write(struct pmagbbfb_par *par, unsigned int reg, u32 v)
76{
77 writel(v, par->sfb + reg / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Ralf Baechle7901c792005-09-03 15:56:11 -070080static inline u32 sfb_read(struct pmagbbfb_par *par, unsigned int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Ralf Baechle7901c792005-09-03 15:56:11 -070082 return readl(par->sfb + reg / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Ralf Baechle7901c792005-09-03 15:56:11 -070085static inline void dac_write(struct pmagbbfb_par *par, unsigned int reg, u8 v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Ralf Baechle7901c792005-09-03 15:56:11 -070087 writeb(v, par->dac + reg / 4);
88}
89
90static inline u8 dac_read(struct pmagbbfb_par *par, unsigned int reg)
91{
92 return readb(par->dac + reg / 4);
93}
94
95static inline void gp0_write(struct pmagbbfb_par *par, u32 v)
96{
97 writel(v, par->mmio + PMAGB_B_GP0);
98}
99
100
101/*
102 * Set the palette.
103 */
104static int pmagbbfb_setcolreg(unsigned int regno, unsigned int red,
105 unsigned int green, unsigned int blue,
106 unsigned int transp, struct fb_info *info)
107{
108 struct pmagbbfb_par *par = info->par;
109
110 BUG_ON(regno >= info->cmap.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 red >>= 8; /* The cmap fields are 16 bits */
Ralf Baechle7901c792005-09-03 15:56:11 -0700113 green >>= 8; /* wide, but the hardware colormap */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 blue >>= 8; /* registers are only 8 bits wide */
115
Ralf Baechle7901c792005-09-03 15:56:11 -0700116 mb();
117 dac_write(par, BT459_ADDR_LO, regno);
118 dac_write(par, BT459_ADDR_HI, 0x00);
119 wmb();
120 dac_write(par, BT459_CMAP, red);
121 wmb();
122 dac_write(par, BT459_CMAP, green);
123 wmb();
124 dac_write(par, BT459_CMAP, blue);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127}
128
129static 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,
135 .fb_cursor = soft_cursor,
136};
137
Ralf Baechle7901c792005-09-03 15:56:11 -0700138
139/*
140 * Turn the hardware cursor off.
141 */
142static void __init pmagbbfb_erase_cursor(struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Ralf Baechle7901c792005-09-03 15:56:11 -0700144 struct pmagbbfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Ralf Baechle7901c792005-09-03 15:56:11 -0700146 mb();
147 dac_write(par, BT459_ADDR_LO, 0x00);
148 dac_write(par, BT459_ADDR_HI, 0x03);
149 wmb();
150 dac_write(par, BT459_DATA, 0x00);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Ralf Baechle7901c792005-09-03 15:56:11 -0700153/*
154 * Set up screen parameters.
155 */
156static void __init pmagbbfb_screen_setup(struct fb_info *info)
157{
158 struct pmagbbfb_par *par = info->par;
159
160 info->var.xres = ((sfb_read(par, SFB_REG_VID_HOR) >>
161 SFB_VID_HOR_PIX_SHIFT) & SFB_VID_HOR_PIX_MASK) * 4;
162 info->var.xres_virtual = info->var.xres;
163 info->var.yres = (sfb_read(par, SFB_REG_VID_VER) >>
164 SFB_VID_VER_SL_SHIFT) & SFB_VID_VER_SL_MASK;
165 info->var.yres_virtual = info->var.yres;
166 info->var.left_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
167 SFB_VID_HOR_BP_SHIFT) &
168 SFB_VID_HOR_BP_MASK) * 4;
169 info->var.right_margin = ((sfb_read(par, SFB_REG_VID_HOR) >>
170 SFB_VID_HOR_FP_SHIFT) &
171 SFB_VID_HOR_FP_MASK) * 4;
172 info->var.upper_margin = (sfb_read(par, SFB_REG_VID_VER) >>
173 SFB_VID_VER_BP_SHIFT) & SFB_VID_VER_BP_MASK;
174 info->var.lower_margin = (sfb_read(par, SFB_REG_VID_VER) >>
175 SFB_VID_VER_FP_SHIFT) & SFB_VID_VER_FP_MASK;
176 info->var.hsync_len = ((sfb_read(par, SFB_REG_VID_HOR) >>
177 SFB_VID_HOR_SYN_SHIFT) &
178 SFB_VID_HOR_SYN_MASK) * 4;
179 info->var.vsync_len = (sfb_read(par, SFB_REG_VID_VER) >>
180 SFB_VID_VER_SYN_SHIFT) & SFB_VID_VER_SYN_MASK;
181
182 info->fix.line_length = info->var.xres;
183};
184
185/*
186 * Determine oscillator configuration.
187 */
188static void __init pmagbbfb_osc_setup(struct fb_info *info)
189{
190 static unsigned int pmagbbfb_freqs[] __initdata = {
191 130808, 119843, 104000, 92980, 74367, 72800,
192 69197, 66000, 65000, 50350, 36000, 32000, 25175
193 };
194 struct pmagbbfb_par *par = info->par;
195 u32 count0 = 8, count1 = 8, counttc = 16 * 256 + 8;
196 u32 freq0, freq1, freqtc = get_tc_speed() / 250;
197 int i, j;
198
199 gp0_write(par, 0); /* select Osc0 */
200 for (j = 0; j < 16; j++) {
201 mb();
202 sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
203 mb();
204 for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
205 if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
206 break;
207 udelay(1);
208 }
209 count0 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
210 }
211
212 gp0_write(par, 1); /* select Osc1 */
213 for (j = 0; j < 16; j++) {
214 mb();
215 sfb_write(par, SFB_REG_TCCLK_COUNT, 0);
216
217 for (i = 0; i < 100; i++) { /* nominally max. 20.5us */
218 if (sfb_read(par, SFB_REG_TCCLK_COUNT) == 0)
219 break;
220 udelay(1);
221 }
222 count1 += sfb_read(par, SFB_REG_VIDCLK_COUNT);
223 }
224
225 freq0 = (freqtc * count0 + counttc / 2) / counttc;
226 par->osc0 = freq0;
227 if (freq0 >= pmagbbfb_freqs[0] - (pmagbbfb_freqs[0] + 32) / 64 &&
228 freq0 <= pmagbbfb_freqs[0] + (pmagbbfb_freqs[0] + 32) / 64)
229 par->osc0 = pmagbbfb_freqs[0];
230
231 freq1 = (par->osc0 * count1 + count0 / 2) / count0;
232 par->osc1 = freq1;
233 for (i = 0; i < sizeof(pmagbbfb_freqs) / sizeof(*pmagbbfb_freqs); i++)
234 if (freq1 >= pmagbbfb_freqs[i] -
235 (pmagbbfb_freqs[i] + 128) / 256 &&
236 freq1 <= pmagbbfb_freqs[i] +
237 (pmagbbfb_freqs[i] + 128) / 256) {
238 par->osc1 = pmagbbfb_freqs[i];
239 break;
240 }
241
242 if (par->osc0 - par->osc1 <= (par->osc0 + par->osc1 + 256) / 512 ||
243 par->osc1 - par->osc0 <= (par->osc0 + par->osc1 + 256) / 512)
244 par->osc1 = 0;
245
246 gp0_write(par, par->osc1 != 0); /* reselect OscX */
247
248 info->var.pixclock = par->osc1 ?
249 (1000000000 + par->osc1 / 2) / par->osc1 :
250 (1000000000 + par->osc0 / 2) / par->osc0;
251};
252
253
254static int __init pmagbbfb_init_one(int slot)
255{
256 char freq0[12], freq1[12];
257 struct fb_info *info;
258 struct pmagbbfb_par *par;
259 unsigned long base_addr;
260 u32 vid_base;
261
262 info = framebuffer_alloc(sizeof(struct pmagbbfb_par), NULL);
263 if (!info)
264 return -ENOMEM;
265
266 par = info->par;
267 par->slot = slot;
268 claim_tc_card(par->slot);
269
270 base_addr = get_tc_base_addr(par->slot);
271
272 par->next = root_pmagbbfb_dev;
273 root_pmagbbfb_dev = info;
274
275 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0)
276 goto err_alloc;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 info->fbops = &pmagbbfb_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 info->fix = pmagbbfb_fix;
Ralf Baechle7901c792005-09-03 15:56:11 -0700280 info->var = pmagbbfb_defined;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 info->flags = FBINFO_DEFAULT;
282
Ralf Baechle7901c792005-09-03 15:56:11 -0700283 /* MMIO mapping setup. */
284 info->fix.mmio_start = base_addr;
285 par->mmio = ioremap_nocache(info->fix.mmio_start, info->fix.mmio_len);
286 if (!par->mmio)
287 goto err_cmap;
288 par->sfb = par->mmio + PMAGB_B_SFB;
289 par->dac = par->mmio + PMAGB_B_BT459;
290
291 /* Frame buffer mapping setup. */
292 info->fix.smem_start = base_addr + PMAGB_B_FBMEM;
293 par->smem = ioremap_nocache(info->fix.smem_start, info->fix.smem_len);
294 if (!par->smem)
295 goto err_mmio_map;
296 vid_base = sfb_read(par, SFB_REG_VID_BASE);
297 info->screen_base = (void __iomem *)par->smem + vid_base * 0x1000;
298 info->screen_size = info->fix.smem_len - 2 * vid_base * 0x1000;
299
300 pmagbbfb_erase_cursor(info);
301 pmagbbfb_screen_setup(info);
302 pmagbbfb_osc_setup(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (register_framebuffer(info) < 0)
Ralf Baechle7901c792005-09-03 15:56:11 -0700305 goto err_smem_map;
306
307 snprintf(freq0, sizeof(freq0), "%u.%03uMHz",
308 par->osc0 / 1000, par->osc0 % 1000);
309 snprintf(freq1, sizeof(freq1), "%u.%03uMHz",
310 par->osc1 / 1000, par->osc1 % 1000);
311
312 pr_info("fb%d: %s frame buffer device in slot %d\n",
313 info->node, info->fix.id, par->slot);
314 pr_info("fb%d: Osc0: %s, Osc1: %s, Osc%u selected\n",
315 info->node, freq0, par->osc1 ? freq1 : "disabled",
316 par->osc1 != 0);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
Ralf Baechle7901c792005-09-03 15:56:11 -0700319
320
321err_smem_map:
322 iounmap(par->smem);
323
324err_mmio_map:
325 iounmap(par->mmio);
326
327err_cmap:
328 fb_dealloc_cmap(&info->cmap);
329
330err_alloc:
331 root_pmagbbfb_dev = par->next;
332 release_tc_card(par->slot);
333 framebuffer_release(info);
334 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Ralf Baechle7901c792005-09-03 15:56:11 -0700337static void __exit pmagbbfb_exit_one(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Ralf Baechle7901c792005-09-03 15:56:11 -0700339 struct fb_info *info = root_pmagbbfb_dev;
340 struct pmagbbfb_par *par = info->par;
341
342 unregister_framebuffer(info);
343 iounmap(par->smem);
344 iounmap(par->mmio);
345 fb_dealloc_cmap(&info->cmap);
346 root_pmagbbfb_dev = par->next;
347 release_tc_card(par->slot);
348 framebuffer_release(info);
349}
350
351
352/*
353 * Initialise the framebuffer.
354 */
355static int __init pmagbbfb_init(void)
356{
357 int count = 0;
358 int slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (fb_get_options("pmagbbfb", NULL))
Ralf Baechle7901c792005-09-03 15:56:11 -0700361 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Ralf Baechle7901c792005-09-03 15:56:11 -0700363 while ((slot = search_tc_card("PMAGB-BA")) >= 0) {
364 if (pmagbbfb_init_one(slot) < 0)
365 break;
366 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Ralf Baechle7901c792005-09-03 15:56:11 -0700368 return (count > 0) ? 0 : -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Ralf Baechle7901c792005-09-03 15:56:11 -0700371static void __exit pmagbbfb_exit(void)
372{
373 while (root_pmagbbfb_dev)
374 pmagbbfb_exit_one();
375}
376
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378module_init(pmagbbfb_init);
Ralf Baechle7901c792005-09-03 15:56:11 -0700379module_exit(pmagbbfb_exit);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381MODULE_LICENSE("GPL");