18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/errno.h>
48c2ecf20Sopenharmony_ci#include <linux/string.h>
58c2ecf20Sopenharmony_ci#include <linux/mm.h>
68c2ecf20Sopenharmony_ci#include <linux/delay.h>
78c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <asm/setup.h>
118c2ecf20Sopenharmony_ci#include <asm/irq.h>
128c2ecf20Sopenharmony_ci#include <asm/amigahw.h>
138c2ecf20Sopenharmony_ci#include <asm/amigaints.h>
148c2ecf20Sopenharmony_ci#include <asm/apollohw.h>
158c2ecf20Sopenharmony_ci#include <linux/fb.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* apollo video HW definitions */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Control Registers.   IOBASE + $x
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Note: these are the Memory/IO BASE definitions for a mono card set to the
248c2ecf20Sopenharmony_ci * alternate address
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Control 3A and 3B serve identical functions except that 3A
278c2ecf20Sopenharmony_ci * deals with control 1 and 3b deals with Color LUT reg.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define AP_IOBASE       0x3b0	/* Base address of 1 plane board. */
318c2ecf20Sopenharmony_ci#define AP_STATUS       isaIO2mem(AP_IOBASE+0)	/* Status register.  Read */
328c2ecf20Sopenharmony_ci#define AP_WRITE_ENABLE isaIO2mem(AP_IOBASE+0)	/* Write Enable Register Write */
338c2ecf20Sopenharmony_ci#define AP_DEVICE_ID    isaIO2mem(AP_IOBASE+1)	/* Device ID Register. Read */
348c2ecf20Sopenharmony_ci#define AP_ROP_1        isaIO2mem(AP_IOBASE+2)	/* Raster Operation reg. Write Word */
358c2ecf20Sopenharmony_ci#define AP_DIAG_MEM_REQ isaIO2mem(AP_IOBASE+4)	/* Diagnostic Memory Request. Write Word */
368c2ecf20Sopenharmony_ci#define AP_CONTROL_0    isaIO2mem(AP_IOBASE+8)	/* Control Register 0.  Read/Write */
378c2ecf20Sopenharmony_ci#define AP_CONTROL_1    isaIO2mem(AP_IOBASE+0xa)	/* Control Register 1.  Read/Write */
388c2ecf20Sopenharmony_ci#define AP_CONTROL_3A   isaIO2mem(AP_IOBASE+0xe)	/* Control Register 3a. Read/Write */
398c2ecf20Sopenharmony_ci#define AP_CONTROL_2    isaIO2mem(AP_IOBASE+0xc)	/* Control Register 2. Read/Write */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define FRAME_BUFFER_START 0x0FA0000
438c2ecf20Sopenharmony_ci#define FRAME_BUFFER_LEN 0x40000
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* CREG 0 */
468c2ecf20Sopenharmony_ci#define VECTOR_MODE 0x40	/* 010x.xxxx */
478c2ecf20Sopenharmony_ci#define DBLT_MODE   0x80	/* 100x.xxxx */
488c2ecf20Sopenharmony_ci#define NORMAL_MODE 0xE0	/* 111x.xxxx */
498c2ecf20Sopenharmony_ci#define SHIFT_BITS  0x1F	/* xxx1.1111 */
508c2ecf20Sopenharmony_ci	/* other bits are Shift value */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* CREG 1 */
538c2ecf20Sopenharmony_ci#define AD_BLT      0x80	/* 1xxx.xxxx */
548c2ecf20Sopenharmony_ci#define NORMAL      0x80 /* 1xxx.xxxx */	/* What is happening here ?? */
558c2ecf20Sopenharmony_ci#define INVERSE     0x00 /* 0xxx.xxxx */	/* Clearing this reverses the screen */
568c2ecf20Sopenharmony_ci#define PIX_BLT     0x00	/* 0xxx.xxxx */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define AD_HIBIT        0x40	/* xIxx.xxxx */
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define ROP_EN          0x10	/* xxx1.xxxx */
618c2ecf20Sopenharmony_ci#define DST_EQ_SRC      0x00	/* xxx0.xxxx */
628c2ecf20Sopenharmony_ci#define nRESET_SYNC     0x08	/* xxxx.1xxx */
638c2ecf20Sopenharmony_ci#define SYNC_ENAB       0x02	/* xxxx.xx1x */
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define BLANK_DISP      0x00	/* xxxx.xxx0 */
668c2ecf20Sopenharmony_ci#define ENAB_DISP       0x01	/* xxxx.xxx1 */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define NORM_CREG1      (nRESET_SYNC | SYNC_ENAB | ENAB_DISP)	/* no reset sync */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* CREG 2 */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*
738c2ecf20Sopenharmony_ci * Following 3 defines are common to 1, 4 and 8 plane.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define S_DATA_1s   0x00 /* 00xx.xxxx */	/* set source to all 1's -- vector drawing */
778c2ecf20Sopenharmony_ci#define S_DATA_PIX  0x40 /* 01xx.xxxx */	/* takes source from ls-bits and replicates over 16 bits */
788c2ecf20Sopenharmony_ci#define S_DATA_PLN  0xC0 /* 11xx.xxxx */	/* normal, each data access =16-bits in
798c2ecf20Sopenharmony_ci						   one plane of image mem */
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* CREG 3A/CREG 3B */
828c2ecf20Sopenharmony_ci#       define RESET_CREG 0x80	/* 1000.0000 */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* ROP REG  -  all one nibble */
858c2ecf20Sopenharmony_ci/*      ********* NOTE : this is used r0,r1,r2,r3 *********** */
868c2ecf20Sopenharmony_ci#define ROP(r2,r3,r0,r1) ( (U_SHORT)((r0)|((r1)<<4)|((r2)<<8)|((r3)<<12)) )
878c2ecf20Sopenharmony_ci#define DEST_ZERO               0x0
888c2ecf20Sopenharmony_ci#define SRC_AND_DEST    0x1
898c2ecf20Sopenharmony_ci#define SRC_AND_nDEST   0x2
908c2ecf20Sopenharmony_ci#define SRC                             0x3
918c2ecf20Sopenharmony_ci#define nSRC_AND_DEST   0x4
928c2ecf20Sopenharmony_ci#define DEST                    0x5
938c2ecf20Sopenharmony_ci#define SRC_XOR_DEST    0x6
948c2ecf20Sopenharmony_ci#define SRC_OR_DEST             0x7
958c2ecf20Sopenharmony_ci#define SRC_NOR_DEST    0x8
968c2ecf20Sopenharmony_ci#define SRC_XNOR_DEST   0x9
978c2ecf20Sopenharmony_ci#define nDEST                   0xA
988c2ecf20Sopenharmony_ci#define SRC_OR_nDEST    0xB
998c2ecf20Sopenharmony_ci#define nSRC                    0xC
1008c2ecf20Sopenharmony_ci#define nSRC_OR_DEST    0xD
1018c2ecf20Sopenharmony_ci#define SRC_NAND_DEST   0xE
1028c2ecf20Sopenharmony_ci#define DEST_ONE                0xF
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define SWAP(A) ((A>>8) | ((A&0xff) <<8))
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* frame buffer operations */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int dnfb_blank(int blank, struct fb_info *info);
1098c2ecf20Sopenharmony_cistatic void dnfb_copyarea(struct fb_info *info, const struct fb_copyarea *area);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const struct fb_ops dn_fb_ops = {
1128c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1138c2ecf20Sopenharmony_ci	.fb_blank	= dnfb_blank,
1148c2ecf20Sopenharmony_ci	.fb_fillrect	= cfb_fillrect,
1158c2ecf20Sopenharmony_ci	.fb_copyarea	= dnfb_copyarea,
1168c2ecf20Sopenharmony_ci	.fb_imageblit	= cfb_imageblit,
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic const struct fb_var_screeninfo dnfb_var = {
1208c2ecf20Sopenharmony_ci	.xres		= 1280,
1218c2ecf20Sopenharmony_ci	.yres		= 1024,
1228c2ecf20Sopenharmony_ci	.xres_virtual	= 2048,
1238c2ecf20Sopenharmony_ci	.yres_virtual	= 1024,
1248c2ecf20Sopenharmony_ci	.bits_per_pixel	= 1,
1258c2ecf20Sopenharmony_ci	.height		= -1,
1268c2ecf20Sopenharmony_ci	.width		= -1,
1278c2ecf20Sopenharmony_ci	.vmode		= FB_VMODE_NONINTERLACED,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct fb_fix_screeninfo dnfb_fix = {
1318c2ecf20Sopenharmony_ci	.id		= "Apollo Mono",
1328c2ecf20Sopenharmony_ci	.smem_start	= (FRAME_BUFFER_START + IO_BASE),
1338c2ecf20Sopenharmony_ci	.smem_len	= FRAME_BUFFER_LEN,
1348c2ecf20Sopenharmony_ci	.type		= FB_TYPE_PACKED_PIXELS,
1358c2ecf20Sopenharmony_ci	.visual		= FB_VISUAL_MONO10,
1368c2ecf20Sopenharmony_ci	.line_length	= 256,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int dnfb_blank(int blank, struct fb_info *info)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	if (blank)
1428c2ecf20Sopenharmony_ci		out_8(AP_CONTROL_3A, 0x0);
1438c2ecf20Sopenharmony_ci	else
1448c2ecf20Sopenharmony_ci		out_8(AP_CONTROL_3A, 0x1);
1458c2ecf20Sopenharmony_ci	return 0;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic
1498c2ecf20Sopenharmony_civoid dnfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	int incr, y_delta, pre_read = 0, x_end, x_word_count;
1538c2ecf20Sopenharmony_ci	uint start_mask, end_mask, dest;
1548c2ecf20Sopenharmony_ci	ushort *src, dummy;
1558c2ecf20Sopenharmony_ci	short i, j;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	incr = (area->dy <= area->sy) ? 1 : -1;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	src = (ushort *)(info->screen_base + area->sy * info->fix.line_length +
1608c2ecf20Sopenharmony_ci			(area->sx >> 4));
1618c2ecf20Sopenharmony_ci	dest = area->dy * (info->fix.line_length >> 1) + (area->dx >> 4);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (incr > 0) {
1648c2ecf20Sopenharmony_ci		y_delta = (info->fix.line_length * 8) - area->sx - area->width;
1658c2ecf20Sopenharmony_ci		x_end = area->dx + area->width - 1;
1668c2ecf20Sopenharmony_ci		x_word_count = (x_end >> 4) - (area->dx >> 4) + 1;
1678c2ecf20Sopenharmony_ci		start_mask = 0xffff0000 >> (area->dx & 0xf);
1688c2ecf20Sopenharmony_ci		end_mask = 0x7ffff >> (x_end & 0xf);
1698c2ecf20Sopenharmony_ci		out_8(AP_CONTROL_0,
1708c2ecf20Sopenharmony_ci		     (((area->dx & 0xf) - (area->sx & 0xf)) % 16) | (0x4 << 5));
1718c2ecf20Sopenharmony_ci		if ((area->dx & 0xf) < (area->sx & 0xf))
1728c2ecf20Sopenharmony_ci			pre_read = 1;
1738c2ecf20Sopenharmony_ci	} else {
1748c2ecf20Sopenharmony_ci		y_delta = -((info->fix.line_length * 8) - area->sx - area->width);
1758c2ecf20Sopenharmony_ci		x_end = area->dx - area->width + 1;
1768c2ecf20Sopenharmony_ci		x_word_count = (area->dx >> 4) - (x_end >> 4) + 1;
1778c2ecf20Sopenharmony_ci		start_mask = 0x7ffff >> (area->dx & 0xf);
1788c2ecf20Sopenharmony_ci		end_mask = 0xffff0000 >> (x_end & 0xf);
1798c2ecf20Sopenharmony_ci		out_8(AP_CONTROL_0,
1808c2ecf20Sopenharmony_ci		     ((-((area->sx & 0xf) - (area->dx & 0xf))) % 16) |
1818c2ecf20Sopenharmony_ci		     (0x4 << 5));
1828c2ecf20Sopenharmony_ci		if ((area->dx & 0xf) > (area->sx & 0xf))
1838c2ecf20Sopenharmony_ci			pre_read = 1;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	for (i = 0; i < area->height; i++) {
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci		out_8(AP_CONTROL_3A, 0xc | (dest >> 16));
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		if (pre_read) {
1918c2ecf20Sopenharmony_ci			dummy = *src;
1928c2ecf20Sopenharmony_ci			src += incr;
1938c2ecf20Sopenharmony_ci		}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		if (x_word_count) {
1968c2ecf20Sopenharmony_ci			out_8(AP_WRITE_ENABLE, start_mask);
1978c2ecf20Sopenharmony_ci			*src = dest;
1988c2ecf20Sopenharmony_ci			src += incr;
1998c2ecf20Sopenharmony_ci			dest += incr;
2008c2ecf20Sopenharmony_ci			out_8(AP_WRITE_ENABLE, 0);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci			for (j = 1; j < (x_word_count - 1); j++) {
2038c2ecf20Sopenharmony_ci				*src = dest;
2048c2ecf20Sopenharmony_ci				src += incr;
2058c2ecf20Sopenharmony_ci				dest += incr;
2068c2ecf20Sopenharmony_ci			}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci			out_8(AP_WRITE_ENABLE, start_mask);
2098c2ecf20Sopenharmony_ci			*src = dest;
2108c2ecf20Sopenharmony_ci			dest += incr;
2118c2ecf20Sopenharmony_ci			src += incr;
2128c2ecf20Sopenharmony_ci		} else {
2138c2ecf20Sopenharmony_ci			out_8(AP_WRITE_ENABLE, start_mask | end_mask);
2148c2ecf20Sopenharmony_ci			*src = dest;
2158c2ecf20Sopenharmony_ci			dest += incr;
2168c2ecf20Sopenharmony_ci			src += incr;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci		src += (y_delta / 16);
2198c2ecf20Sopenharmony_ci		dest += (y_delta / 16);
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci	out_8(AP_CONTROL_0, NORMAL_MODE);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/*
2258c2ecf20Sopenharmony_ci * Initialization
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic int dnfb_probe(struct platform_device *dev)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	struct fb_info *info;
2318c2ecf20Sopenharmony_ci	int err = 0;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	info = framebuffer_alloc(0, &dev->dev);
2348c2ecf20Sopenharmony_ci	if (!info)
2358c2ecf20Sopenharmony_ci		return -ENOMEM;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	info->fbops = &dn_fb_ops;
2388c2ecf20Sopenharmony_ci	info->fix = dnfb_fix;
2398c2ecf20Sopenharmony_ci	info->var = dnfb_var;
2408c2ecf20Sopenharmony_ci	info->var.red.length = 1;
2418c2ecf20Sopenharmony_ci	info->var.red.offset = 0;
2428c2ecf20Sopenharmony_ci	info->var.green = info->var.blue = info->var.red;
2438c2ecf20Sopenharmony_ci	info->screen_base = (u_char *) info->fix.smem_start;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	err = fb_alloc_cmap(&info->cmap, 2, 0);
2468c2ecf20Sopenharmony_ci	if (err < 0)
2478c2ecf20Sopenharmony_ci		goto release_framebuffer;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	err = register_framebuffer(info);
2508c2ecf20Sopenharmony_ci	if (err < 0) {
2518c2ecf20Sopenharmony_ci		fb_dealloc_cmap(&info->cmap);
2528c2ecf20Sopenharmony_ci		goto release_framebuffer;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci	platform_set_drvdata(dev, info);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* now we have registered we can safely setup the hardware */
2578c2ecf20Sopenharmony_ci	out_8(AP_CONTROL_3A, RESET_CREG);
2588c2ecf20Sopenharmony_ci	out_be16(AP_WRITE_ENABLE, 0x0);
2598c2ecf20Sopenharmony_ci	out_8(AP_CONTROL_0, NORMAL_MODE);
2608c2ecf20Sopenharmony_ci	out_8(AP_CONTROL_1, (AD_BLT | DST_EQ_SRC | NORM_CREG1));
2618c2ecf20Sopenharmony_ci	out_8(AP_CONTROL_2, S_DATA_PLN);
2628c2ecf20Sopenharmony_ci	out_be16(AP_ROP_1, SWAP(0x3));
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	printk("apollo frame buffer alive and kicking !\n");
2658c2ecf20Sopenharmony_ci	return err;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cirelease_framebuffer:
2688c2ecf20Sopenharmony_ci	framebuffer_release(info);
2698c2ecf20Sopenharmony_ci	return err;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic struct platform_driver dnfb_driver = {
2738c2ecf20Sopenharmony_ci	.probe	= dnfb_probe,
2748c2ecf20Sopenharmony_ci	.driver	= {
2758c2ecf20Sopenharmony_ci		.name	= "dnfb",
2768c2ecf20Sopenharmony_ci	},
2778c2ecf20Sopenharmony_ci};
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic struct platform_device dnfb_device = {
2808c2ecf20Sopenharmony_ci	.name	= "dnfb",
2818c2ecf20Sopenharmony_ci};
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ciint __init dnfb_init(void)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	int ret;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (!MACH_IS_APOLLO)
2888c2ecf20Sopenharmony_ci		return -ENODEV;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (fb_get_options("dnfb", NULL))
2918c2ecf20Sopenharmony_ci		return -ENODEV;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	ret = platform_driver_register(&dnfb_driver);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (!ret) {
2968c2ecf20Sopenharmony_ci		ret = platform_device_register(&dnfb_device);
2978c2ecf20Sopenharmony_ci		if (ret)
2988c2ecf20Sopenharmony_ci			platform_driver_unregister(&dnfb_driver);
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci	return ret;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cimodule_init(dnfb_init);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
306