hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
 *
 * Copyright (C) 2005, 2006, 2009 Sebastian Smolorz
 *                               <smolorz@rts.uni-hannover.de>
 *
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; eitherer version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/delay.h>
 
#include <rtdm/driver.h>
 
#include <rtdm/can.h>
#include <rtcan_dev.h>
#include <rtcan_raw.h>
#include <rtcan_internal.h>
#include <rtcan_sja1000.h>
#include <rtcan_sja1000_regs.h>
 
#define RTCAN_DEV_NAME    "rtcan%d"
#define RTCAN_DRV_NAME    "sja1000-isa"
 
#define RTCAN_ISA_MAX_DEV 4
 
static char *isa_board_name = "ISA-Board";
 
MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
MODULE_DESCRIPTION("RTCAN board driver for standard ISA boards");
MODULE_LICENSE("GPL");
 
static u16 io[RTCAN_ISA_MAX_DEV];
static int irq[RTCAN_ISA_MAX_DEV];
static u32 can_clock[RTCAN_ISA_MAX_DEV];
static u8 ocr[RTCAN_ISA_MAX_DEV];
static u8 cdr[RTCAN_ISA_MAX_DEV];
 
module_param_array(io, ushort, NULL, 0444);
module_param_array(irq, int, NULL, 0444);
module_param_array(can_clock, uint, NULL, 0444);
module_param_array(ocr, byte, NULL, 0444);
module_param_array(cdr, byte, NULL, 0444);
 
MODULE_PARM_DESC(io, "The io-port address");
MODULE_PARM_DESC(irq, "The interrupt number");
MODULE_PARM_DESC(can_clock, "External clock frequency (default 16 MHz)");
MODULE_PARM_DESC(ocr, "Value of output control register (default 0x1a)");
MODULE_PARM_DESC(cdr, "Value of clock divider register (default 0xc8");
 
#define RTCAN_ISA_PORT_SIZE 32
 
struct rtcan_isa
{
   u16 io;
};
 
static struct rtcan_device *rtcan_isa_devs[RTCAN_ISA_MAX_DEV];
 
static u8 rtcan_isa_readreg(struct rtcan_device *dev, int port)
{
   struct rtcan_isa *board = (struct rtcan_isa *)dev->board_priv;
   return inb(board->io + port);
}
 
static void rtcan_isa_writereg(struct rtcan_device *dev, int port, u8 val)
{
   struct rtcan_isa *board = (struct rtcan_isa *)dev->board_priv;
   outb(val, board->io + port);
}
 
 
int __init rtcan_isa_init_one(int idx)
{
   struct rtcan_device *dev;
   struct rtcan_sja1000 *chip;
   struct rtcan_isa *board;
   int ret;
 
   if ((dev = rtcan_dev_alloc(sizeof(struct rtcan_sja1000),
                  sizeof(struct rtcan_isa))) == NULL)
       return -ENOMEM;
 
   chip = (struct rtcan_sja1000 *)dev->priv;
   board = (struct rtcan_isa *)dev->board_priv;
 
   dev->board_name = isa_board_name;
 
   board->io = io[idx];
 
   chip->irq_num = irq[idx];
   chip->irq_flags = RTDM_IRQTYPE_SHARED | RTDM_IRQTYPE_EDGE;
 
   chip->read_reg = rtcan_isa_readreg;
   chip->write_reg = rtcan_isa_writereg;
 
   /* Check and request I/O ports */
   if (!request_region(board->io, RTCAN_ISA_PORT_SIZE, RTCAN_DRV_NAME)) {
       ret = -EBUSY;
       goto out_dev_free;
   }
 
   /* Clock frequency in Hz */
   if (can_clock[idx])
       dev->can_sys_clock = can_clock[idx] / 2;
   else
       dev->can_sys_clock = 8000000; /* 16/2 MHz */
 
   /* Output control register */
   if (ocr[idx])
       chip->ocr = ocr[idx];
   else
       chip->ocr = SJA_OCR_MODE_NORMAL | SJA_OCR_TX0_PUSHPULL;
 
   if (cdr[idx])
       chip->cdr = cdr[idx];
   else
       chip->cdr = SJA_CDR_CAN_MODE | SJA_CDR_CLK_OFF | SJA_CDR_CBP;
 
   strncpy(dev->name, RTCAN_DEV_NAME, IFNAMSIZ);
 
   ret = rtcan_sja1000_register(dev);
   if (ret) {
       printk(KERN_ERR "ERROR %d while trying to register SJA1000 "
              "device!\n", ret);
       goto out_free_region;
   }
 
   rtcan_isa_devs[idx] = dev;
   return 0;
 
 out_free_region:
   release_region(board->io, RTCAN_ISA_PORT_SIZE);
 
 out_dev_free:
   rtcan_dev_free(dev);
 
   return ret;
}
 
static void rtcan_isa_exit(void);
 
/** Init module */
static int __init rtcan_isa_init(void)
{
   int i, err;
   int devices = 0;
 
   if (!rtdm_available())
       return -ENOSYS;
 
   for (i = 0; i < RTCAN_ISA_MAX_DEV && io[i] != 0; i++) {
       err = rtcan_isa_init_one(i);
       if (err) {
           rtcan_isa_exit();
           return err;
       }
       devices++;
   }
   if (devices)
       return 0;
 
   printk(KERN_ERR "ERROR! No devices specified! "
          "Use io=<port1>[,...] irq=<irq1>[,...]\n");
   return -EINVAL;
}
 
 
/** Cleanup module */
static void rtcan_isa_exit(void)
{
   int i;
   struct rtcan_device *dev;
 
   for (i = 0; i < RTCAN_ISA_MAX_DEV; i++) {
       dev = rtcan_isa_devs[i];
       if (!dev)
           continue;
       rtcan_sja1000_unregister(dev);
       release_region(io[i], RTCAN_ISA_PORT_SIZE);
       rtcan_dev_free(dev);
   }
}
 
module_init(rtcan_isa_init);
module_exit(rtcan_isa_exit);