1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2010-2013 Bluecherry, LLC <https://www.bluecherrydvr.com>
6 * Ben Collins <bcollins@ubuntu.com>
9 * John Brooks <john.brooks@bluecherry.net>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
18 #define EE_SHIFT_CLK 0x04
20 #define EE_DATA_WRITE 0x02
21 #define EE_DATA_READ 0x01
22 #define EE_ENB (0x80 | EE_CS)
24 #define eeprom_delay() udelay(100)
26 #define eeprom_delay() solo_reg_read(solo_dev, SOLO_EEPROM_CTRL)
27 #define eeprom_delay() ({ \
30 for (i = ret = 0; i < 1000 && !ret; i++) \
31 ret = solo_eeprom_reg_read(solo_dev); \
39 #define EE_WRITE_CMD 5
41 #define EE_ERASE_CMD 7
43 static unsigned int solo_eeprom_reg_read(struct solo_dev
*solo_dev
)
45 return solo_reg_read(solo_dev
, SOLO_EEPROM_CTRL
) & EE_DATA_READ
;
48 static void solo_eeprom_reg_write(struct solo_dev
*solo_dev
, u32 data
)
50 solo_reg_write(solo_dev
, SOLO_EEPROM_CTRL
, data
);
54 static void solo_eeprom_cmd(struct solo_dev
*solo_dev
, int cmd
)
58 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ACCESS_EN
);
59 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
61 for (i
= 4 + ADDR_LEN
; i
>= 0; i
--) {
62 int dataval
= (cmd
& (1 << i
)) ? EE_DATA_WRITE
: 0;
64 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
| dataval
);
65 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
66 EE_SHIFT_CLK
| dataval
);
69 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
72 unsigned int solo_eeprom_ewen(struct solo_dev
*solo_dev
, int w_en
)
74 int ewen_cmd
= (w_en
? 0x3f : 0) | (EE_EWEN_CMD
<< ADDR_LEN
);
75 unsigned int retval
= 0;
78 solo_eeprom_cmd(solo_dev
, ewen_cmd
);
80 for (i
= 0; i
< 16; i
++) {
81 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
83 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
84 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
85 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
88 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
89 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
94 __be16
solo_eeprom_read(struct solo_dev
*solo_dev
, int loc
)
96 int read_cmd
= loc
| (EE_READ_CMD
<< ADDR_LEN
);
100 solo_eeprom_cmd(solo_dev
, read_cmd
);
102 for (i
= 0; i
< 16; i
++) {
103 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
|
105 retval
= (retval
<< 1) | solo_eeprom_reg_read(solo_dev
);
106 solo_eeprom_reg_write(solo_dev
, SOLO_EEPROM_ENABLE
);
109 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
111 return (__force __be16
)retval
;
114 int solo_eeprom_write(struct solo_dev
*solo_dev
, int loc
,
117 int write_cmd
= loc
| (EE_WRITE_CMD
<< ADDR_LEN
);
121 solo_eeprom_cmd(solo_dev
, write_cmd
);
123 for (i
= 15; i
>= 0; i
--) {
124 unsigned int dataval
= ((__force
unsigned)data
>> i
) & 1;
126 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
127 solo_eeprom_reg_write(solo_dev
,
128 EE_ENB
| (dataval
<< 1) | EE_SHIFT_CLK
);
131 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
132 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);
133 solo_eeprom_reg_write(solo_dev
, EE_ENB
);
135 for (i
= retval
= 0; i
< 10000 && !retval
; i
++)
136 retval
= solo_eeprom_reg_read(solo_dev
);
138 solo_eeprom_reg_write(solo_dev
, ~EE_CS
);