]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/nsi.c
9f6bdefd46b61caea13c6804e278c098106fd8af
[lincan.git] / lincan / src / nsi.c
1 /* nsi.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */ 
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/nsi.h"
14 #include "../include/i82527.h"
15
16 int nsican_irq=-1;
17 unsigned long nsican_base=0x0;
18
19 /* IO_RANGE is the io-memory range that gets reserved, please adjust according
20  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
21  * #define IO_RANGE 0x20 for sja1000 chips.
22  */
23 #define IO_RANGE 0x04
24
25 /* The function template_request_io is used to reserve the io-memory. If your
26  * hardware uses a dedicated memory range as hardware control registers you
27  * will have to add the code to reserve this memory as well.
28  * The reserved memory starts at candev->io_addr, wich is the module parameter io.
29  */
30 int nsi_request_io(struct candevice_t *candev)
31 {
32
33         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
34                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
35                 return -ENODEV;
36         } else {
37                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, 
38                          candev->io_addr + IO_RANGE - 1);
39         }
40         return 0;
41 }
42
43 /* The function template_release_io is used to free the previously reserved 
44  * io-memory. In case you reserved more memory, don't forget to free it here.
45  */
46 int nsi_release_io(struct candevice_t *candev)
47 {
48
49         can_release_io_region(candev->io_addr,IO_RANGE);
50
51         return 0;
52 }
53
54 /* The function template_reset is used to give a hardware reset. This is rather
55  * hardware specific so I haven't included example code. Don't forget to check
56  * the reset status of the chip before returning.
57  */
58 int nsi_reset(struct candevice_t *candev)
59 {
60     int i; 
61
62     DEBUGMSG("Resetting nsi hardware ...\n");
63     /* we don't use template_write_register because we don't use the two first
64        register of the card but the third in order to make a hard reset */
65     outb (1, nsican_base + candev->res_addr);
66     outb (0, nsican_base + candev->res_addr);
67     for (i = 1; i < 1000; i++)
68         udelay (1000);
69     
70     
71     /* Check hardware reset status */ 
72     i=0;
73     while ( (nsi_read_register(nsican_base+iCPU) & iCPU_RST) && (i<=15)) {
74         udelay(20000);
75         i++;
76     }
77     if (i>=15) {
78         CANMSG("Reset status timeout!\n");
79         CANMSG("Please check your hardware.\n");
80         return -ENODEV;
81     }
82     else
83         DEBUGMSG("Chip0 reset status ok.\n");
84
85     return 0;
86 }
87
88 /* The function template_init_hw_data is used to initialize the hardware
89  * structure containing information about the installed CAN-board.
90  * RESET_ADDR represents the io-address of the hardware reset register.
91  * NR_82527 represents the number of intel 82527 chips on the board.
92  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
93  * The flags entry can currently only be CANDEV_PROGRAMMABLE_IRQ to indicate that
94  * the hardware uses programmable interrupts.
95  */
96 #define RESET_ADDR 0x02
97 #define NR_82527 1
98 #define NR_SJA1000 0
99
100 int nsi_init_hw_data(struct candevice_t *candev) 
101      {
102         candev->res_addr=RESET_ADDR;
103         candev->nr_82527_chips=1;
104         candev->nr_sja1000_chips=0;
105         candev->nr_all_chips=1;
106         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
107
108         return 0;
109 }
110
111 /* The function template_init_chip_data is used to initialize the hardware
112  * structure containing information about the CAN chips.
113  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
114  * "sja1000".
115  * The chip_base_addr entry represents the start of the 'official' memory map
116  * of the installed chip. It's likely that this is the same as the candev->io_addr
117  * argument supplied at module loading time.
118  * The clock argument holds the chip clock value in Hz.
119  */
120 #define CHIP_TYPE "i82527"
121
122 int nsi_init_chip_data(struct candevice_t *candev, int chipnr)
123 {
124         candev->chip[chipnr]->chip_type=CHIP_TYPE;
125         candev->chip[chipnr]->chip_base_addr=
126             candev->io_addr;
127         candev->chip[chipnr]->clock = 16000000;
128         nsican_irq=candev->chip[chipnr]->chip_irq;      
129         nsican_base=candev->chip[chipnr]->chip_base_addr;
130         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
131         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
132         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
133
134         return 0;
135 }
136
137  /* The function template_init_obj_data is used to initialize the hardware
138  * structure containing information about the different message objects on the
139  * CAN chip. In case of the sja1000 there's only one message object but on the
140  * i82527 chip there are 15.
141  * The code below is for a i82527 chip and initializes the object base addresses
142  * The entry obj_base_addr represents the first memory address of the message 
143  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
144  * base address.
145  * Unless the hardware uses a segmented memory map, flags can be set zero.
146  */
147 int nsi_init_obj_data(struct chip_t *chip, int objnr)
148 {
149
150         chip->msgobj[objnr]->obj_base_addr=
151             chip->chip_base_addr+(objnr+1)*0x10;
152         
153         return 0;
154 }
155
156 /* The function template_program_irq is used for hardware that uses programmable
157  * interrupts. If your hardware doesn't use programmable interrupts you should
158  * not set the candevices_t->flags entry to CANDEV_PROGRAMMABLE_IRQ and leave this
159  * function unedited. Again this function is hardware specific so there's no
160  * example code.
161  */
162 int nsi_program_irq(struct candevice_t *candev)
163 {
164         return 0;
165 }
166
167 /* The function template_write_register is used to write to hardware registers
168  * on the CAN chip. You should only have to edit this function if your hardware
169  * uses some specific write process.
170  */
171 void nsi_write_register(unsigned char data, unsigned long address)
172 {
173     /* address is an absolute address */
174
175     /* the nsi card has two registers, the address register at 0x0
176        and the data register at 0x01 */
177
178     /* write the relative address on the eight LSB bits 
179        and the data on the eight MSB bits in one time */
180     outw(address-nsican_base + (256 * data), nsican_base); 
181 }
182
183 /* The function template_read_register is used to read from hardware registers
184  * on the CAN chip. You should only have to edit this function if your hardware
185  * uses some specific read process.
186  */
187 unsigned nsi_read_register(unsigned long address)
188 {
189     /* this is the same thing that the function write_register.
190        We use the two register, we write the address where we 
191        want to read in a first time. In a second time we read the
192        data */
193     unsigned char ret;
194     
195     can_disable_irq(nsican_irq); 
196     outb(address-nsican_base, nsican_base);
197     ret=inb(nsican_base+1);
198     can_enable_irq(nsican_irq); 
199     return ret;
200 }
201
202
203  /* !!! Don't change this function !!! */
204 int nsi_register(struct hwspecops_t *hwspecops)
205 {
206         hwspecops->request_io = nsi_request_io;
207         hwspecops->release_io = nsi_release_io;
208         hwspecops->reset = nsi_reset;
209         hwspecops->init_hw_data = nsi_init_hw_data;
210         hwspecops->init_chip_data = nsi_init_chip_data;
211         hwspecops->init_obj_data = nsi_init_obj_data;
212         hwspecops->write_register = nsi_write_register;
213         hwspecops->read_register = nsi_read_register;
214         hwspecops->program_irq = nsi_program_irq;
215         return 0;
216 }