]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/nsi.c
Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / nsi.c
1 /**************************************************************************/
2 /* File: nsi.c - CAN104 PC/104 card by NSI                                */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/nsi.h"
39 #include "../include/i82527.h"
40
41 int nsican_irq=-1;
42 unsigned long nsican_base=0x0;
43
44 static CAN_DEFINE_SPINLOCK(nsican_port_lock);
45
46 /* IO_RANGE is the io-memory range that gets reserved, please adjust according
47  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
48  * #define IO_RANGE 0x20 for sja1000 chips.
49  */
50 #define IO_RANGE 0x04
51
52 /* The function template_request_io is used to reserve the io-memory. If your
53  * hardware uses a dedicated memory range as hardware control registers you
54  * will have to add the code to reserve this memory as well.
55  * The reserved memory starts at candev->io_addr, wich is the module parameter io.
56  */
57 int nsi_request_io(struct candevice_t *candev)
58 {
59
60         if (!can_request_io_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
61                 CANMSG("Unable to open port: 0x%lx\n",candev->io_addr);
62                 return -ENODEV;
63         } else {
64                 DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr,
65                          candev->io_addr + IO_RANGE - 1);
66         }
67         return 0;
68 }
69
70 /* The function template_release_io is used to free the previously reserved
71  * io-memory. In case you reserved more memory, don't forget to free it here.
72  */
73 int nsi_release_io(struct candevice_t *candev)
74 {
75
76         can_release_io_region(candev->io_addr,IO_RANGE);
77
78         return 0;
79 }
80
81 /* The function template_reset is used to give a hardware reset. This is rather
82  * hardware specific so I haven't included example code. Don't forget to check
83  * the reset status of the chip before returning.
84  */
85 int nsi_reset(struct candevice_t *candev)
86 {
87     int i;
88
89     DEBUGMSG("Resetting nsi hardware ...\n");
90     /* we don't use template_write_register because we don't use the two first
91        register of the card but the third in order to make a hard reset */
92     can_outb (1, nsican_base + candev->res_addr);
93     can_outb (0, nsican_base + candev->res_addr);
94     for (i = 1; i < 1000; i++)
95         udelay (1000);
96
97
98     /* Check hardware reset status */
99     i=0;
100     while ( (nsi_read_register(nsican_base+iCPU) & iCPU_RST) && (i<=15)) {
101         mdelay(20);
102         i++;
103     }
104     if (i>=15) {
105         CANMSG("Reset status timeout!\n");
106         CANMSG("Please check your hardware.\n");
107         return -ENODEV;
108     }
109     else
110         DEBUGMSG("Chip0 reset status ok.\n");
111
112     return 0;
113 }
114
115 /* The function template_init_hw_data is used to initialize the hardware
116  * structure containing information about the installed CAN-board.
117  * RESET_ADDR represents the io-address of the hardware reset register.
118  * NR_82527 represents the number of intel 82527 chips on the board.
119  * NR_SJA1000 represents the number of philips sja1000 chips on the board.
120  * The flags entry can currently only be CANDEV_PROGRAMMABLE_IRQ to indicate that
121  * the hardware uses programmable interrupts.
122  */
123 #define RESET_ADDR 0x02
124 #define NR_82527 1
125 #define NR_SJA1000 0
126
127 int nsi_init_hw_data(struct candevice_t *candev)
128      {
129         candev->res_addr=RESET_ADDR;
130         candev->nr_82527_chips=1;
131         candev->nr_sja1000_chips=0;
132         candev->nr_all_chips=1;
133         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
134
135         return 0;
136 }
137
138 /* The function template_init_chip_data is used to initialize the hardware
139  * structure containing information about the CAN chips.
140  * CHIP_TYPE represents the type of CAN chip. CHIP_TYPE can be "i82527" or
141  * "sja1000".
142  * The chip_base_addr entry represents the start of the 'official' memory map
143  * of the installed chip. It's likely that this is the same as the candev->io_addr
144  * argument supplied at module loading time.
145  * The clock argument holds the chip clock value in Hz.
146  */
147
148 int nsi_init_chip_data(struct candevice_t *candev, int chipnr)
149 {
150         i82527_fill_chipspecops(candev->chip[chipnr]);
151         candev->chip[chipnr]->chip_base_addr=
152             can_ioport2ioptr(candev->io_addr);
153         candev->chip[chipnr]->clock = 16000000;
154         nsican_irq=candev->chip[chipnr]->chip_irq;
155         nsican_base=candev->chip[chipnr]->chip_base_addr;
156         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC;
157         candev->chip[chipnr]->int_clk_reg = iCLK_SL1;
158         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
159
160         return 0;
161 }
162
163  /* The function template_init_obj_data is used to initialize the hardware
164  * structure containing information about the different message objects on the
165  * CAN chip. In case of the sja1000 there's only one message object but on the
166  * i82527 chip there are 15.
167  * The code below is for a i82527 chip and initializes the object base addresses
168  * The entry obj_base_addr represents the first memory address of the message
169  * object. In case of the sja1000 obj_base_addr is taken the same as the chips
170  * base address.
171  * Unless the hardware uses a segmented memory map, flags can be set zero.
172  */
173 int nsi_init_obj_data(struct canchip_t *chip, int objnr)
174 {
175
176         chip->msgobj[objnr]->obj_base_addr=
177             chip->chip_base_addr+(objnr+1)*0x10;
178
179         return 0;
180 }
181
182 /* The function template_program_irq is used for hardware that uses programmable
183  * interrupts. If your hardware doesn't use programmable interrupts you should
184  * not set the candevices_t->flags entry to CANDEV_PROGRAMMABLE_IRQ and leave this
185  * function unedited. Again this function is hardware specific so there's no
186  * example code.
187  */
188 int nsi_program_irq(struct candevice_t *candev)
189 {
190         return 0;
191 }
192
193 /* The function template_write_register is used to write to hardware registers
194  * on the CAN chip. You should only have to edit this function if your hardware
195  * uses some specific write process.
196  */
197 void nsi_write_register(unsigned data, can_ioptr_t address)
198 {
199     /* address is an absolute address */
200
201     /* the nsi card has two registers, the address register at 0x0
202        and the data register at 0x01 */
203
204     /* write the relative address on the eight LSB bits
205        and the data on the eight MSB bits in one time */
206     can_outw(address-nsican_base + (256 * data), nsican_base);
207 }
208
209 /* The function template_read_register is used to read from hardware registers
210  * on the CAN chip. You should only have to edit this function if your hardware
211  * uses some specific read process.
212  */
213 unsigned nsi_read_register(can_ioptr_t address)
214 {
215     /* this is the same thing that the function write_register.
216        We use the two register, we write the address where we
217        want to read in a first time. In a second time we read the
218        data */
219         unsigned char ret;
220         can_spin_irqflags_t flags;
221
222         can_spin_lock_irqsave(&nsican_port_lock,flags);
223         can_outb(address-nsican_base, nsican_base);
224         ret=can_inb(nsican_base+1);
225         can_spin_unlock_irqrestore(&nsican_port_lock,flags);
226         return ret;
227 }
228
229
230  /* !!! Don't change this function !!! */
231 int nsi_register(struct hwspecops_t *hwspecops)
232 {
233         hwspecops->request_io = nsi_request_io;
234         hwspecops->release_io = nsi_release_io;
235         hwspecops->reset = nsi_reset;
236         hwspecops->init_hw_data = nsi_init_hw_data;
237         hwspecops->init_chip_data = nsi_init_chip_data;
238         hwspecops->init_obj_data = nsi_init_obj_data;
239         hwspecops->write_register = nsi_write_register;
240         hwspecops->read_register = nsi_read_register;
241         hwspecops->program_irq = nsi_program_irq;
242         return 0;
243 }