]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/m437.c
d90db12e3077c07534b23f4ce71cbf725f2adeee
[lincan.git] / lincan / src / m437.c
1 /* m437.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.3  17 Jun 2004
8  */ 
9
10 /* 
11  * Support for the SECO M437 
12  * 
13  * SECO M437 is a pc104 format, i82527 controller based card
14  * produced by SECO http://www.seco.it 
15  * This driver uses the Memory Mapped I/O mode, and should be
16  * working with all cards supporting this mode.
17  *
18  * Written by Fabio Parodi  (fabio.parodi@iname.com)
19  * Additional changes by Giampiero Giancipoli (gianci@freemail.it)
20  *
21  * Version 0.1  08 Jun 2001
22  *
23  */
24
25 #include "../include/can.h"
26 #include "../include/can_sysdep.h"
27 #include "../include/main.h"
28 #include "../include/m437.h"
29 #include "../include/i82527.h"
30
31 /*
32  * IO_RANGE is the io-memory range that gets reserved, please adjust according
33  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
34  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
35  */
36 #define IO_RANGE 0x100
37
38
39 /**
40  * m437_request_io: - reserve io or memory range for can board
41  * @candev: pointer to candevice/board which asks for io. Field @io_addr
42  *      of @candev is used in most cases to define start of the range
43  *
44  * The function m437_request_io() is used to reserve the io-memory. If your
45  * hardware uses a dedicated memory range as hardware control registers you
46  * will have to add the code to reserve this memory as well. 
47  * %IO_RANGE is the io-memory range that gets reserved, please adjust according
48  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
49  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
50  * Return Value: The function returns zero on success or %-ENODEV on failure
51  * File: src/m437.c
52  */
53 int m437_request_io(struct candevice_t *candev)
54 {
55
56         if (!can_request_mem_region(candev->io_addr,IO_RANGE,DEVICE_NAME)) {
57                 CANMSG("Unable to request IO-memory: 0x%lx\n",candev->io_addr);
58                 return -ENODEV;
59         }
60
61         if ( !( candev->dev_base_addr = ioremap( candev->io_addr, IO_RANGE ) ) ) {
62                 CANMSG("Unable to access I/O memory at: 0x%lx\n", candev->io_addr);
63                 can_release_mem_region(candev->io_addr,IO_RANGE);
64                 return -ENODEV;
65         
66         }
67         DEBUGMSG("Registered IO-memory: 0x%lx - 0x%lx\n", candev->io_addr, candev->io_addr + IO_RANGE - 1);
68         return 0;
69 }
70
71 /**
72  * m437_elease_io - free reserved io memory range
73  * @candev: pointer to candevice/board which releases io
74  *
75  * The function m437_release_io() is used to free reserved io-memory.
76  * In case you have reserved more io memory, don't forget to free it here.
77  * IO_RANGE is the io-memory range that gets released, please adjust according
78  * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
79  * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
80  * Return Value: The function always returns zero
81  * File: src/m437.c
82  */
83 int m437_release_io(struct candevice_t *candev)
84 {
85         unsigned i;
86         
87         /* disable IRQ generation */
88         m437_write_register(iCTL_CCE, candev->dev_base_addr+iCTL);
89
90         /* clear all message objects */
91         for (i=1; i<=15; i++) {
92                 m437_write_register(
93                                 INTPD_RES | 
94                                 RXIE_RES | 
95                                 TXIE_RES | 
96                                 MVAL_RES, 
97                                 candev->dev_base_addr+i*0x10+iMSGCTL0);
98                 m437_write_register(
99                                 NEWD_RES | 
100                                 MLST_RES | 
101                                 CPUU_RES | 
102                                 TXRQ_RES | 
103                                 RMPD_RES, 
104                                 candev->dev_base_addr+i*0x10+iMSGCTL1);
105         }
106         
107         /* power down i82527 */
108         m437_write_register(iCPU_PWD, candev->dev_base_addr+iCPU);
109         
110         /* release I/O memory mapping */
111         iounmap(candev->dev_base_addr);
112
113         can_release_mem_region(candev->io_addr,IO_RANGE);
114
115         return 0;
116 }
117
118 /**
119  * m437_reset - hardware reset routine
120  * @candev: Pointer to candevice/board structure
121  *
122  * The function m437_reset() is used to give a hardware reset. This is 
123  * rather hardware specific so I haven't included example code. Don't forget to 
124  * check the reset status of the chip before returning.
125  * Return Value: The function returns zero on success or %-ENODEV on failure
126  * File: src/m437.c
127  */
128 int m437_reset(struct candevice_t *candev)
129 {
130         return 0;
131 }
132
133 #define RESET_ADDR 0x0
134 #define NR_82527 1
135 #define NR_SJA1000 0
136
137 /**
138  * m437_init_hw_data - Initialize hardware cards
139  * @candev: Pointer to candevice/board structure
140  *
141  * The function m437_init_hw_data() is used to initialize the hardware
142  * structure containing information about the installed CAN-board.
143  * %RESET_ADDR represents the io-address of the hardware reset register.
144  * %NR_82527 represents the number of intel 82527 chips on the board.
145  * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
146  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
147  * the hardware uses programmable interrupts.
148  * Return Value: The function always returns zero
149  * File: src/m437.c
150  */
151 int m437_init_hw_data(struct candevice_t *candev) 
152 {
153         DEBUGMSG("m437_init_hw_data()\n");
154         candev->res_addr=RESET_ADDR;
155         candev->nr_82527_chips=1;
156         candev->nr_sja1000_chips=0;
157         candev->nr_all_chips=1;
158         candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
159         /* The M437 has no programmable IRQ */
160
161         return 0;
162 }
163
164 /**
165  * m437_init_chip_data - Initialize chips
166  * @candev: Pointer to candevice/board structure
167  * @chipnr: Number of the CAN chip on the hardware card
168  *
169  * The function m437_init_chip_data() is used to initialize the hardware
170  * structure containing information about the CAN chips.
171  * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
172  * "sja1000".
173  * The @chip_base_addr entry represents the start of the 'official' memory map
174  * of the installed chip. It's likely that this is the same as the @io_addr
175  * argument supplied at module loading time.
176  * The @clock entry holds the chip clock value in Hz.
177  * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
178  * register. Options defined in the %sja1000.h file:
179  * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
180  * The entry @sja_ocr_reg holds hardware specific options for the Output Control
181  * register. Options defined in the %sja1000.h file:
182  * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
183  * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
184  * The entry @int_clk_reg holds hardware specific options for the Clock Out
185  * register. Options defined in the %i82527.h file:
186  * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
187  * The entry @int_bus_reg holds hardware specific options for the Bus 
188  * Configuration register. Options defined in the %i82527.h file:
189  * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
190  * The entry @int_cpu_reg holds hardware specific options for the cpu interface
191  * register. Options defined in the %i82527.h file:
192  * %iCPU_CEN, %iCPU_MUX, %iCPU_SLP, %iCPU_PWD, %iCPU_DMC, %iCPU_DSC, %iCPU_RST.
193  * Return Value: The function always returns zero
194  * File: src/m437.c
195  */
196 int m437_init_chip_data(struct candevice_t *candev, int chipnr)
197 {
198         i82527_fill_chipspecops(candev->chip[chipnr]);
199         candev->chip[chipnr]->chip_base_addr=candev->dev_base_addr;
200         candev->chip[chipnr]->clock = 16000000;
201         candev->chip[chipnr]->int_cpu_reg = iCPU_DSC | iCPU_CEN;
202         candev->chip[chipnr]->int_clk_reg = 
203                 iCLK_CD0 | iCLK_CD1 | iCLK_CD2 | iCLK_SL0 | iCLK_SL1;
204         candev->chip[chipnr]->int_bus_reg = iBUS_CBY;
205
206         return 0;
207 }
208
209 /**
210  * m437_init_obj_data - Initialize message buffers
211  * @chip: Pointer to chip specific structure
212  * @objnr: Number of the message buffer
213  *
214  * The function m437_init_obj_data() is used to initialize the hardware
215  * structure containing information about the different message objects on the
216  * CAN chip. In case of the sja1000 there's only one message object but on the
217  * i82527 chip there are 15.
218  * The code below is for a i82527 chip and initializes the object base addresses
219  * The entry @obj_base_addr represents the first memory address of the message 
220  * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
221  * base address.
222  * Unless the hardware uses a segmented memory map, flags can be set zero.
223  * Return Value: The function always returns zero
224  * File: src/m437.c
225  */
226 int m437_init_obj_data(struct canchip_t *chip, int objnr)
227 {
228         chip->msgobj[objnr]->obj_base_addr=chip->chip_base_addr+(objnr+1)*0x10;
229         
230         return 0;
231 }
232
233 /**
234  * m437_program_irq - program interrupts
235  * @candev: Pointer to candevice/board structure
236  *
237  * The function m437_program_irq() is used for hardware that uses 
238  * programmable interrupts. If your hardware doesn't use programmable interrupts
239  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
240  * leave this function unedited. Again this function is hardware specific so 
241  * there's no example code.
242  * Return value: The function returns zero on success or %-ENODEV on failure
243  * File: src/m437.c
244  */
245 int m437_program_irq(struct candevice_t *candev)
246 {
247         return 0;
248 }
249
250 /**
251  * m437_write_register - Low level write register routine
252  * @data: data to be written
253  * @address: memory address to write to
254  *
255  * The function m437_write_register() is used to write to hardware registers
256  * on the CAN chip. You should only have to edit this function if your hardware
257  * uses some specific write process.
258  * Return Value: The function does not return a value
259  * File: src/m437.c
260  */
261 void m437_write_register(unsigned data, can_ioptr_t address)
262 {
263         can_writeb(data,address);
264 }
265
266 /**
267  * m437_read_register - Low level read register routine
268  * @address: memory address to read from
269  *
270  * The function m437_read_register() is used to read from hardware registers
271  * on the CAN chip. You should only have to edit this function if your hardware
272  * uses some specific read process.
273  * Return Value: The function returns the value stored in @address
274  * File: src/m437.c
275  */
276 unsigned m437_read_register(can_ioptr_t address)
277 {
278         return can_readb(address);
279 }
280
281 /* !!! Don't change this function !!! */
282 int m437_register(struct hwspecops_t *hwspecops)
283 {
284         DEBUGMSG("m437_register()\n");
285         hwspecops->request_io = m437_request_io;
286         hwspecops->release_io = m437_release_io;
287         hwspecops->reset = m437_reset;
288         hwspecops->init_hw_data = m437_init_hw_data;
289         hwspecops->init_chip_data = m437_init_chip_data;
290         hwspecops->init_obj_data = m437_init_obj_data;
291         hwspecops->write_register = m437_write_register;
292         hwspecops->read_register = m437_read_register;
293         hwspecops->program_irq = m437_program_irq;
294         return 0;
295 }