2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/kernel.h>
22 #include <linux/delay.h>
27 #define EE_SHIFT_CLK 0x04
29 #define EE_DATA_WRITE 0x02
30 #define EE_DATA_READ 0x01
31 #define EE_ENB (0x80 | EE_CS)
33 #define eeprom_delay() udelay(100)
35 #define eeprom_delay() solo_reg_read(solo_dev, SOLO_EEPROM_CTRL)
36 #define eeprom_delay() ({ \
39 for (i = ret = 0; i < 1000 && !ret; i++) \
40 ret = solo_eeprom_reg_read(solo_dev); \
48 #define EE_WRITE_CMD 5
50 #define EE_ERASE_CMD 7
52 static unsigned int solo_eeprom_reg_read(struct solo_dev
*solo_dev
)
54 return solo_reg_read(solo_dev
, SOLO_EEPROM_CTRL
) & EE_DATA_READ
;
57 static void solo_eeprom_reg_write(struct solo_dev
*solo_dev
, u32 data
)
59 solo_reg_write(solo_dev
, SOLO_EEPROM_CTRL
, data
);
63 static void solo_eeprom_cmd(struct solo_dev
*solo_dev
, int cmd
)
67 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ACCESS_EN
);
68 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
70 for (i
= 4 + ADDR_LEN
; i
>= 0; i
--) {
71 int dataval
= (cmd
& (1 << i
)) ? EE_DATA_WRITE
: 0;
73 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
| dataval
);
74 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
75 EE_SHIFT_CLK
| dataval
);
78 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
81 unsigned int solo_eeprom_ewen(struct solo_dev
*solo_dev
, int w_en
)
83 int ewen_cmd
= (w_en
? 0x3f : 0) | (EE_EWEN_CMD
<< ADDR_LEN
);
84 unsigned int retval
= 0;
87 solo_eeprom_cmd(solo_dev
, ewen_cmd
);
89 for (i
= 0; i
< 16; i
++) {
90 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
92 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
93 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
94 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
97 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
98 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
103 __be16
solo_eeprom_read(struct solo_dev
*solo_dev
, int loc
)
105 int read_cmd
= loc
| (EE_READ_CMD
<< ADDR_LEN
);
109 solo_eeprom_cmd(solo_dev
, read_cmd
);
111 for (i
= 0; i
< 16; i
++) {
112 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
114 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
115 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
118 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
120 return (__force __be16
)retval
;
123 int solo_eeprom_write(struct solo_dev
*solo_dev
, int loc
,
126 int write_cmd
= loc
| (EE_WRITE_CMD
<< ADDR_LEN
);
130 solo_eeprom_cmd(solo_dev
, write_cmd
);
132 for (i
= 15; i
>= 0; i
--) {
133 unsigned int dataval
= ((__force
unsigned)data
>> i
) & 1;
135 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
136 solo_eeprom_reg_write(solo_dev
,
137 EE_ENB
| (dataval
<< 1) | EE_SHIFT_CLK
);
140 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
141 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
142 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
144 for (i
= retval
= 0; i
< 10000 && !retval
; i
++)
145 retval
= solo_eeprom_reg_read(solo_dev
);
147 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);