DEV messaging stage 1/4: Rearrange struct cdevsw and add a message port
[dragonfly.git] / sys / i386 / i386 / elan-mmcr.c
CommitLineData
984263bc
MD
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: src/sys/i386/i386/elan-mmcr.c,v 1.6.2.1 2002/09/17 22:39:53 sam Exp $
fabb8ceb 10 * $DragonFly: src/sys/i386/i386/Attic/elan-mmcr.c,v 1.3 2003/07/21 05:50:39 dillon Exp $
984263bc
MD
11 * The AMD Elan sc520 is a system-on-chip gadget which is used in embedded
12 * kind of things, see www.soekris.com for instance, and it has a few quirks
13 * we need to deal with.
14 * Unfortunately we cannot identify the gadget by CPUID output because it
15 * depends on strapping options and only the stepping field may be useful
16 * and those are undocumented from AMDs side.
17 *
18 * So instead we recognize the on-chip host-PCI bridge and call back from
19 * sys/i386/pci/pci_bus.c to here if we find it.
20 */
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/kernel.h>
25#include <sys/conf.h>
26#include <sys/proc.h>
27#include <sys/sysctl.h>
28#include <sys/time.h>
29
30#include <machine/md_var.h>
31
32#include <vm/vm.h>
33#include <vm/pmap.h>
34
35uint16_t *elan_mmcr;
36
37
38static unsigned
39elan_get_timecount(struct timecounter *tc)
40{
41 return (elan_mmcr[0xc84 / 2]);
42}
43
44static struct timecounter elan_timecounter = {
45 elan_get_timecount,
46 0,
47 0xffff,
48 33333333 / 4,
49 "ELAN"
50};
51
52void
53init_AMD_Elan_sc520(void)
54{
55 u_int new;
56 int i;
57
58 if (bootverbose)
59 printf("Doing h0h0magic for AMD Elan sc520\n");
60 elan_mmcr = pmap_mapdev(0xfffef000, 0x1000);
61
62 /*-
63 * The i8254 is driven with a nonstandard frequency which is
64 * derived thusly:
65 * f = 32768 * 45 * 25 / 31 = 1189161.29...
66 * We use the sysctl to get the timecounter etc into whack.
67 */
68
69 new = 1189161;
70 i = kernel_sysctlbyname(curproc, "machdep.i8254_freq",
71 NULL, 0,
72 &new, sizeof new,
73 NULL);
74 if (bootverbose)
75 printf("sysctl machdep.i8254_freq=%d returns %d\n", new, i);
76
77 /* Start GP timer #2 and use it as timecounter, hz permitting */
78 elan_mmcr[0xc82 / 2] = 0xc001;
79 init_timecounter(&elan_timecounter);
80}
81
82
83/*
84 * Device driver initialization stuff
85 */
86
87static d_open_t elan_open;
88static d_close_t elan_close;
89static d_ioctl_t elan_ioctl;
90static d_mmap_t elan_mmap;
91
92#define CDEV_MAJOR 100 /* Share with xrpu */
93static struct cdevsw elan_cdevsw = {
fabb8ceb
MD
94 /* name */ "elan",
95 /* maj */ CDEV_MAJOR,
96 /* flags */ 0,
97 /* port */ NULL,
98 /* autoq */ 0,
99
984263bc
MD
100 /* open */ elan_open,
101 /* close */ elan_close,
102 /* read */ noread,
103 /* write */ nowrite,
104 /* ioctl */ elan_ioctl,
105 /* poll */ nopoll,
106 /* mmap */ elan_mmap,
107 /* strategy */ nostrategy,
984263bc 108 /* dump */ nodump,
fabb8ceb 109 /* psize */ nopsize
984263bc
MD
110};
111
112static int
113elan_open(dev_t dev, int flag, int mode, struct proc *p)
114{
115 return (0);
116}
117
118static int
119elan_close(dev_t dev, int flag, int mode, struct proc *p)
120{
121 return (0);
122}
123
124static int
125elan_mmap(dev_t dev, vm_offset_t offset, int nprot)
126{
127 if (offset >= 0x1000)
128 return (-1);
129 return (i386_btop(0xfffef000));
130}
131
132static int
133elan_ioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
134{
135 return(ENOENT);
136}
137
138static void
139elan_drvinit(void)
140{
141
142 if (elan_mmcr == NULL)
143 return;
144 printf("Elan-mmcr driver: MMCR at %p\n", elan_mmcr);
145 make_dev(&elan_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "elan-mmcr");
146 return;
147}
148
149SYSINIT(elan, SI_SUB_PSEUDO, SI_ORDER_MIDDLE+CDEV_MAJOR,elan_drvinit,NULL);