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