1 #include <linux/cpumask.h>
2 #include <asm/mpspec.h>
3 #include <linux/module.h>
5 #include <linux/string.h>
6 #include <linux/proc_fs.h>
7 #include <linux/delay.h>
9 static struct i2c_spi_t
{
10 void __iomem
*cntrl_base
;
11 void __iomem
*data_base
;
12 unsigned char dev_number
;
17 struct proc_dir_entry
*proc_dev
;
19 #define E3M_MULTIFUNC_VENDOR PCI_VENDOR_ID_INTEL
20 #define E3M_MULTIFUNC_DEVICE 0x0002
22 #define MAX_SPI_DEVICE_NR 3
24 #define SPI_CONTROL 0x00
25 #define SPI_STATUS 0x04
26 #define SPI_OPCODE 0x08
27 #define SPI_ADDRESS 0x0C
30 #define SPI_WREN_CMD 6
31 #define SPI_WRDI_CMD 4
32 #define SPI_WRSR_CMD 5
33 #define SPI_RDSR_CMD 1
34 #define SPI_READ_CMD 3
35 #define SPI_WRITE_CMD 2
36 #define SPI_RDPC_CMD 0x13
37 #define SPI_WRPC_CMD 0x12
39 #define SPI_STATUS_BUSY_SHIFT 0
40 #define SPI_STATUS_INTR_SHIFT 1
41 #define SPI_STATUS_FAIL_SHIFT 2
43 #define SPI_STATUS_BUSY (1 << SPI_STATUS_BUSY_SHIFT)
44 #define SPI_STATUS_INTR (1 << SPI_STATUS_INTR_SHIFT)
45 #define SPI_STATUS_FAIL (1 << SPI_STATUS_FAIL_SHIFT)
47 #define SPI_MODE_INTR_SHIFT 4
48 #define SPI_MODE_INTR (1 << SPI_MODE_INTR_SHIFT)
50 #define SPI_DEVICE_0 0
51 #define SPI_DEVICE_1 1
52 #define SPI_DEVICE_2 2
53 #define SPI_DEVICE_3 3
55 #define SPI_ADDRESS_SIZE_8 0
56 #define SPI_ADDRESS_SIZE_16 1
57 #define SPI_ADDRESS_SIZE_24 2
58 #define SPI_ADDRESS_SIZE_32 3
60 #define MAX_SPI_BYTES 64
62 #define SPI_DEVICE_SHIFT 0
64 #define MAX_SPI_ADDRESS_SIZE_SHIFT 3
65 #define SPI_ADDRESS_SIZE_SHIFT 2
67 #define SPI_DATA_SIZE_SHIFT 4
68 #define SPI_ADDRESS_PHASE_SHIFT 11
70 #define SPI_ADDRESS_PHASE_ENABLE (1 << SPI_ADDRESS_PHASE_SHIFT)
71 #define SPI_ADDRESS_PHASE_DISABLE (0 << SPI_ADDRESS_PHASE_SHIFT)
73 #define SPI_DATA_PHASE_SHIFT 12
75 #define SPI_DATA_PHASE_ENABLE (1 << SPI_DATA_PHASE_SHIFT)
76 #define SPI_DATA_PHASE_DISABLE (0 << SPI_DATA_PHASE_SHIFT)
78 #define SPI_TRANS_TYPE_SHIFT 13
80 #define SPI_TRANS_READ (0 << SPI_TRANS_TYPE_SHIFT)
81 #define SPI_TRANS_WRITE (1 << SPI_TRANS_TYPE_SHIFT)
83 #define SPI_START_SHIFT 14
85 #define SPI_START (1 << SPI_START_SHIFT)
87 #define SPI_KILL_SHIFT 15
89 #define SPI_KILL (1 << SPI_KILL_SHIFT)
91 #define SPI_CMOS_CNTRL_AREA_SIZE 0x40
92 #define SPI_CMOS_DATA_AREA_SIZE 0x40
94 #define I2C_SPI_DEFAULT_IRQ 23
96 static int spi_ops(struct i2c_spi_t
*i2c_spi
, unsigned int dev_number
, unsigned char cmd_code
);
97 static int spi_read(struct i2c_spi_t
*i2c_spi
, unsigned int cmos_addr
)
99 void *i2c_spi_cntrl
= i2c_spi
->cntrl_base
;
100 void *i2c_spi_data
= i2c_spi
->data_base
;
102 unsigned int cmd
= 0;
105 /* Set READ operation code */
106 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_OPCODE, SPI_RDPC_CMD, MAS_IOADDR);
107 writel(SPI_RDPC_CMD
, i2c_spi_cntrl
+ SPI_OPCODE
);
109 /* Set addr offset */
110 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_ADDRESS, cmos_addr, MAS_IOADDR);
111 writel(cmos_addr
, i2c_spi_cntrl
+ SPI_ADDRESS
);
113 /* Clean int & fail bits */
114 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_STATUS, 0x6, MAS_IOADDR);
115 writel(0x6, i2c_spi_cntrl
+ SPI_STATUS
);
117 /* Set Device number, Address size, Data size offset */
118 cmd
= i2c_spi
->dev_number
<< SPI_DEVICE_SHIFT
|
119 SPI_ADDRESS_SIZE_8
<< SPI_ADDRESS_SIZE_SHIFT
|
120 1 << SPI_DATA_SIZE_SHIFT
|
121 SPI_ADDRESS_PHASE_ENABLE
|
122 SPI_DATA_PHASE_ENABLE
|
126 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_CONTROL, cmd, MAS_IOADDR);
127 writel(cmd
, i2c_spi_cntrl
+ SPI_CONTROL
);
129 while((tmp
= readl(i2c_spi_cntrl
+ SPI_STATUS
)/*niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR)*/) &
132 if(tmp
& SPI_STATUS_FAIL
)
136 if (readl(i2c_spi_cntrl
+ SPI_STATUS
) /* niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR) */& SPI_STATUS_FAIL
) {
137 printk("spi_read: Error - Transfer Failed");
140 data
= readb (i2c_spi_data
);//niki E2K_READ_MAS_B(i2c_spi_data, MAS_IOADDR);
144 static int spi_write(struct i2c_spi_t
*i2c_spi
, unsigned char val
, unsigned int cmos_addr
)
148 void *i2c_spi_cntrl
= i2c_spi
->cntrl_base
;
149 void *i2c_spi_data
= i2c_spi
->data_base
;
152 if(spi_ops(i2c_spi
, i2c_spi
->dev_number
, SPI_WREN_CMD
) == -1) {
153 printk("%s: Error - Failed to enable write operation", __FUNCTION__
);
157 //niki E2K_WRITE_MAS_B(i2c_spi_data, val, MAS_IOADDR);
158 writeb(val
, i2c_spi_data
);
160 /* Clean int & fail bits */
161 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_STATUS, 0x6, MAS_IOADDR);
162 writel(0x6, i2c_spi_cntrl
+ SPI_STATUS
);
164 /* Set WRITE operation code */
165 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_OPCODE, SPI_WRPC_CMD, MAS_IOADDR);
166 writel(SPI_WRPC_CMD
, i2c_spi_cntrl
+ SPI_OPCODE
);
168 /* Set addr offset */
169 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_ADDRESS, cmos_addr, MAS_IOADDR);
170 writel(cmos_addr
, i2c_spi_cntrl
+ SPI_ADDRESS
);
172 /* Set Device number, Address size, Data size offset */
173 cmd
= i2c_spi
->dev_number
<< SPI_DEVICE_SHIFT
|
174 SPI_ADDRESS_SIZE_8
<< SPI_ADDRESS_SIZE_SHIFT
|
175 1 << SPI_DATA_SIZE_SHIFT
|
176 SPI_ADDRESS_PHASE_ENABLE
|
177 SPI_DATA_PHASE_ENABLE
|
181 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_CONTROL, cmd, MAS_IOADDR);
182 writel(cmd
, i2c_spi_cntrl
+ SPI_CONTROL
);
184 while((tmp
= readl(i2c_spi_cntrl
+ SPI_STATUS
)/*niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR)*/) &
187 if(tmp
& SPI_STATUS_FAIL
)
191 if (/*niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR)*/ readl(i2c_spi_cntrl
+ SPI_STATUS
) & SPI_STATUS_FAIL
) {
192 printk("spi_write: Error - Transfer Failed");
199 static int spi_interrupt_unlock(struct i2c_spi_t
*i2c_spi
, unsigned int mode
)
201 //niki E2K_WRITE_MAS_W(i2c_spi->cntrl_base + SPI_MODE, mode, MAS_IOADDR);
202 writel(mode
, i2c_spi
->cntrl_base
+ SPI_MODE
);
206 static unsigned int spi_interrupt_lock(struct i2c_spi_t
*i2c_spi
)
208 unsigned int mode
= readl(i2c_spi
->cntrl_base
+ SPI_MODE
);//E2K_READ_MAS_W(i2c_spi->cntrl_base + SPI_MODE, MAS_IOADDR);
209 // mode &= (~SPI_MODE_INTR);
210 //niki E2K_WRITE_MAS_W(i2c_spi->cntrl_base + SPI_MODE, mode & (~SPI_MODE_INTR), MAS_IOADDR);
211 writel(mode
& (~SPI_MODE_INTR
),i2c_spi
->cntrl_base
+ SPI_MODE
);
215 static int spi_interrupt_reset(struct i2c_spi_t
*i2c_spi
)
217 unsigned int status
= readl(i2c_spi
->cntrl_base
+ SPI_STATUS
);//E2K_READ_MAS_W(i2c_spi->cntrl_base + SPI_STATUS, MAS_IOADDR);
219 //niki E2K_WRITE_MAS_W(i2c_spi->cntrl_base + SPI_STATUS, status, MAS_IOADDR);
220 writel(status
, i2c_spi
->cntrl_base
+ SPI_STATUS
);
224 static int spi_ops(struct i2c_spi_t
*i2c_spi
, unsigned int dev_number
, unsigned char cmd_code
)
227 void *i2c_spi_cntrl
= i2c_spi
->cntrl_base
;
228 if (dev_number
> MAX_SPI_DEVICE_NR
) {
229 printk("spi_ops: Error - Device number is to large: %d (Max: %d)", dev_number
, MAX_SPI_DEVICE_NR
);
237 printk("spi_ops: Error - Wrong command code: %d", cmd_code
);
243 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_OPCODE, cmd_code, MAS_IOADDR);
244 writel(cmd_code
, i2c_spi_cntrl
+ SPI_OPCODE
);
246 cmd
= dev_number
<< SPI_DEVICE_SHIFT
|
247 SPI_ADDRESS_PHASE_DISABLE
|
248 SPI_DATA_PHASE_DISABLE
|
250 //niki E2K_WRITE_MAS_W(i2c_spi_cntrl + SPI_CONTROL, cmd, MAS_IOADDR);
251 writel(cmd
, i2c_spi_cntrl
+ SPI_CONTROL
);
253 while((/* niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR)*/ readl(i2c_spi_cntrl
+ SPI_STATUS
) &
254 (SPI_STATUS_INTR
| SPI_STATUS_FAIL
)) == 0);
256 if (/*niki E2K_READ_MAS_W(i2c_spi_cntrl + SPI_STATUS, MAS_IOADDR)*/ readl(i2c_spi_cntrl
+ SPI_STATUS
) & SPI_STATUS_FAIL
) {
257 printk("spi_ops: Error - Operation Failed");
263 static void proc_read(char *ch
);
265 static int proc_read_spi_test(char *page
, char **start
,
266 off_t off
, int count
,
267 int *eof
, void *data
)
270 memcpy(page
, tch
, strlen(tch
));
276 static void proc_read(char *ch
)
279 int h
, m
, s
, delta
, r0
;
281 i
+= sprintf(&ch
[i
],"Ctrl %x\n",spi_read(&i2c_spig
, 0x18));
283 i
+= sprintf(&ch
[i
],"S/N: ");
284 for (j
= 0; j
< 8; j
++) {
285 i
+= sprintf(&ch
[i
],"%2.2x",spi_read(&i2c_spig
, 0x10 + j
));
289 r0
= spi_read(&i2c_spig
, 0x0);
291 spi_write(&i2c_spig
, r0
, 0x0);
293 i
+= sprintf(&ch
[i
], "\n");
294 s
= spi_read(&i2c_spig
, 0x2);
295 s
= (s
>> 4) * 10 + (s
& 0xf);
296 m
= spi_read(&i2c_spig
, 0x3);
297 m
= (m
>> 4) * 10 + (m
& 0xf);
298 h
= spi_read(&i2c_spig
, 0x4);
299 h
= (h
>> 4) * 10 + (h
& 0xf);
302 spi_write(&i2c_spig
, r0
, 0x0);
304 i
+= sprintf(&ch
[i
],"RTC %.2dh %.2dm %.2ds\n",h
, m
, s
);
308 r0
= spi_read(&i2c_spig
, 0x0);
310 spi_write(&i2c_spig
, r0
, 0x0);
313 s
= spi_read(&i2c_spig
, 0x2);
314 s
= (s
>> 4) * 10 + (s
& 0xf);
315 m
= spi_read(&i2c_spig
, 0x3);
316 m
= (m
>> 4) * 10 + (m
& 0xf);
317 h
= spi_read(&i2c_spig
, 0x4);
318 h
= (h
>> 4) * 10 + (h
& 0xf);
321 spi_write(&i2c_spig
, r0
, 0x0);
323 delta
= ((delta
= (s
- delta
)) >= 0)? (delta
): (delta
* -1);
324 i
+= sprintf(&ch
[i
],"Time delay 2 sec %.2dh %.2dm %.2ds delta = %d\n",h
, m
, s
, delta
);
326 i
+= sprintf(&ch
[i
],"SPI TEST");
328 sprintf(&ch
[i
]," ERROR\n");
330 i
+= sprintf(&ch
[i
]," OK\n");
334 static int spi_test_init_module(void)
341 printk("test_fm33256: Scanning PCI bus for ioapic/pic/timer i2c/spi controller ...");
342 dev
= pci_get_device(E3M_MULTIFUNC_VENDOR
, E3M_MULTIFUNC_DEVICE
, 0);
344 printk("found on bus %d device %d\n",
345 dev
->bus
->number
, PCI_SLOT(dev
->devfn
));
347 printk("!!! NOT FOUND !!!\n");
350 printk("test_fm33256: control base addr = 0x%x, data base addr = 0x%x\n",
351 (unsigned int) pci_resource_start( dev
, 0), (unsigned int) pci_resource_start( dev
, 1));
352 i2c_spig
.cntrl_base
= ioremap( pci_resource_start(dev
, 0), pci_resource_len(dev
, 0) ); // niki pci_resource_start( dev, 0);
353 i2c_spig
.data_base
= ioremap( pci_resource_start(dev
, 1), pci_resource_len(dev
, 1) ); // niki pci_resource_start( dev, 1);
354 i2c_spig
.dev_number
= 1;
356 mode
= spi_interrupt_lock(&i2c_spig
);
357 tmp
= spi_read(&i2c_spig
, 0x18);
359 //proc_read(&tch[0]);
361 proc_dev
= create_proc_entry("spi_test", 0444, NULL
);
364 strcpy((char *)proc_dev
->name
, "spi_test");
365 // strcpy(device->proc_dev->value, proc_name);
366 proc_dev
->data
= NULL
;
367 proc_dev
->read_proc
= proc_read_spi_test
;
368 proc_dev
->write_proc
= NULL
;
370 // printk("%s \n", tch);
371 spi_interrupt_reset(&i2c_spig
);
372 spi_interrupt_unlock(&i2c_spig
, mode
);
378 static void spi_test_exit_module(void)
380 iounmap(i2c_spig
.cntrl_base
);
381 iounmap(i2c_spig
.data_base
);
382 remove_proc_entry( "spi_test", proc_dev
);
386 module_init(spi_test_init_module
);
387 module_exit(spi_test_exit_module
);
389 MODULE_DESCRIPTION( "SPI Test of e3s" );
390 MODULE_AUTHOR ( "Aleksey Nikiforov" );
391 MODULE_LICENSE ( "GPL" );