1 /*
2 * i2c-hibvt.c
3 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include <asm/io.h>
20 #include <linux/errno.h>
21 /*
22 * I2C Registers offsets
23 */
24 #define HIBVT_I2C_GLB 0x0
25 #define HIBVT_I2C_SCL_H 0x4
26 #define HIBVT_I2C_SCL_L 0x8
27 #define HIBVT_I2C_DATA1 0x10
28 #define HIBVT_I2C_TXF 0x20
29 #define HIBVT_I2C_RXF 0x24
30 #define HIBVT_I2C_CMD_BASE 0x30
31 #define HIBVT_I2C_LOOP1 0xb0
32 #define HIBVT_I2C_DST1 0xb4
33 #define HIBVT_I2C_TX_WATER 0xc8
34 #define HIBVT_I2C_RX_WATER 0xcc
35 #define HIBVT_I2C_CTRL1 0xd0
36 #define HIBVT_I2C_CTRL2 0xd4
37 #define HIBVT_I2C_STAT 0xd8
38 #define HIBVT_I2C_INTR_RAW 0xe0
39 #define HIBVT_I2C_INTR_EN 0xe4
40 #define HIBVT_I2C_INTR_STAT 0xe8
41
42 #ifndef bit
43 #define bit(nr) (1 << (nr))
44 #endif
45 /*
46 * I2C Global Config Register -- HIBVT_I2C_GLB
47 */
48 #define GLB_EN_MASK bit(0)
49 #define GLB_SDA_HOLD_MASK 0xffff00
50 #define GLB_SDA_HOLD_SHIFT 8
51
52 /*
53 * I2C Timing CMD Register -- HIBVT_I2C_CMD_BASE + n * 4 (n = 0, 1, 2, ... 31)
54 */
55 #define CMD_EXIT 0x0
56 #define CMD_TX_S 0x1
57 #define CMD_TX_D1_2 0x4
58 #define CMD_TX_D1_1 0x5
59 #define CMD_TX_FIFO 0x9
60 #define CMD_RX_FIFO 0x12
61 #define CMD_RX_ACK 0x13
62 #define CMD_IGN_ACK 0x15
63 #define CMD_TX_ACK 0x16
64 #define CMD_TX_NACK 0x17
65 #define CMD_JMP1 0x18
66 #define CMD_UP_TXF 0x1d
67 #define CMD_TX_RS 0x1e
68 #define CMD_TX_P 0x1f
69
70 /*
71 * I2C Control Register 1 -- HIBVT_I2C_CTRL1
72 */
73 #define CTRL1_CMD_START_MASK bit(0)
74
75 /*
76 * I2C Status Register -- HIBVT_I2C_STAT
77 */
78 #define STAT_RXF_NOE_MASK bit(16) /* RX FIFO not empty flag */
79 #define STAT_TXF_NOF_MASK bit(19) /* TX FIFO not full flag */
80
81
82 /*
83 * I2C Interrupt status and mask Register --
84 * HIBVT_I2C_INTR_RAW, HIBVT_I2C_STAT, HIBVT_I2C_INTR_STAT
85 */
86 #define INTR_ABORT_MASK (bit(0) | bit(11))
87 #define INTR_RX_MASK bit(2)
88 #define INTR_TX_MASK bit(4)
89 #define INTR_CMD_DONE_MASK bit(12)
90 #define INTR_USE_MASK (INTR_ABORT_MASK | \
91 INTR_RX_MASK | \
92 INTR_TX_MASK | \
93 INTR_CMD_DONE_MASK)
94 #define INTR_ALL_MASK 0xffffffff
95
96 #define I2C_DEFAULT_FREQUENCY 100000
97 #define I2C_TXF_DEPTH 64
98 #define I2C_RXF_DEPTH 64
99 #define I2C_TXF_WATER 32
100 #define I2C_RXF_WATER 32
101 #define I2C_WAIT_TIMEOUT 0x400
102 #define I2C_IRQ_TIMEOUT (msecs_to_jiffies(1000))
103
104 void *memset(void *s, int c, size_t count);
105
udelay(unsigned int num)106 static inline void udelay(unsigned int num)
107 {
108 volatile unsigned int i;
109
110 for (i = 0; i < (100 * num); i++) /* 100: Cycle */
111 __asm__ __volatile__("nop");
112 }
113
114 struct i2c_message {
115 __u16 chip; /* slave address */
116 __u16 addr; /* reg address */
117 __u16 alen; /* reg width,1 or 2 */
118 __u16 flags;
119 __u16 len; /* msg length,1 or 2 */
120 __u8 *buf; /* pointer to msg data */
121 #define I2C_M_RD 0x0001 /* read data, from slave to master */
122 #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
123 #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
124 #define I2C_M_STOP 0x8000 /* if I2C_FUNC_PROTOCOL_MANGLING */
125 };
126
127 struct hibvt_i2c_dev {
128 struct device *dev;
129 void *base;
130 unsigned int clk;
131 unsigned int freq;
132 };
133
134 struct hibvt_i2c_dev i2c_dev = {
135 .base = (void *)0x04566000,
136 .clk = 50000000, /* 50000000: clock */
137 .freq = I2C_DEFAULT_FREQUENCY,
138 };
139
140 static inline void hibvt_i2c_disable(const struct hibvt_i2c_dev *i2c);
141 static inline void hibvt_i2c_cfg_irq(struct hibvt_i2c_dev *i2c,
142 unsigned int flag);
143 static inline unsigned int hibvt_i2c_clr_irq(const struct hibvt_i2c_dev *i2c);
144 static inline void hibvt_i2c_enable(const struct hibvt_i2c_dev *i2c);
145
146 #define CHECK_SDA_IN_SHIFT 16
147 #define GPIO_MODE_SHIFT 8
148 #define FORCE_SCL_OEN_SHIFT 4
149 #define FORCE_SDA_OEN_SHIFT 0
150
hibvt_i2c_disable(const struct hibvt_i2c_dev * i2c)151 static void hibvt_i2c_disable(const struct hibvt_i2c_dev *i2c)
152 {
153 unsigned int val;
154
155 val = readl(i2c->base + HIBVT_I2C_GLB);
156 val &= ~GLB_EN_MASK;
157 writel(val, i2c->base + HIBVT_I2C_GLB);
158 }
159
hibvt_i2c_enable(const struct hibvt_i2c_dev * i2c)160 static void hibvt_i2c_enable(const struct hibvt_i2c_dev *i2c)
161 {
162 unsigned int val;
163
164 val = readl(i2c->base + HIBVT_I2C_GLB);
165 val |= GLB_EN_MASK;
166 writel(val, i2c->base + HIBVT_I2C_GLB);
167 }
168
hibvt_i2c_cfg_irq(struct hibvt_i2c_dev * i2c,unsigned int flag)169 static inline void hibvt_i2c_cfg_irq(struct hibvt_i2c_dev *i2c,
170 unsigned int flag)
171 {
172 writel(flag, i2c->base + HIBVT_I2C_INTR_EN);
173 }
174
hibvt_i2c_disable_irq(const struct hibvt_i2c_dev * i2c,unsigned int flag)175 static void hibvt_i2c_disable_irq(const struct hibvt_i2c_dev *i2c,
176 unsigned int flag)
177 {
178 unsigned int val;
179
180 val = readl(i2c->base + HIBVT_I2C_INTR_EN);
181 val &= ~flag;
182 writel(val, i2c->base + HIBVT_I2C_INTR_EN);
183 }
184
hibvt_i2c_clr_irq(const struct hibvt_i2c_dev * i2c)185 static unsigned int hibvt_i2c_clr_irq(const struct hibvt_i2c_dev *i2c)
186 {
187 unsigned int val;
188
189 val = readl(i2c->base + HIBVT_I2C_INTR_STAT);
190 writel(INTR_ALL_MASK, i2c->base + HIBVT_I2C_INTR_RAW);
191
192 return val;
193 }
194
hibvt_i2c_cmdreg_set(const struct hibvt_i2c_dev * i2c,unsigned int cmd,unsigned int * offset)195 static inline void hibvt_i2c_cmdreg_set(const struct hibvt_i2c_dev *i2c,
196 unsigned int cmd, unsigned int *offset)
197 {
198 /* HIBVT_I2C_CMD_BASE + *offset * 4: I2C_TIMING_CMD REG */
199 writel(cmd, i2c->base + HIBVT_I2C_CMD_BASE + *offset * 4);
200 (*offset)++;
201 }
202
203 /*
204 * config i2c slave addr
205 */
hibvt_i2c_set_addr(const struct hibvt_i2c_dev * i2c,struct i2c_message * msg)206 static void hibvt_i2c_set_addr(const struct hibvt_i2c_dev *i2c,
207 struct i2c_message *msg)
208 {
209 u16 addr;
210
211 if (msg->flags & I2C_M_TEN) {
212 /* First byte is 11110XX0 where XX is upper 2 bits */
213 addr = ((msg->chip & 0x300) << 1) | 0xf000;
214 if (msg->flags & I2C_M_RD)
215 addr |= 1 << 8; /* Move Right 8bit */
216
217 /* Second byte is the remaining 8 bits */
218 addr |= msg->chip & 0xff;
219 } else {
220 addr = (msg->chip & 0x7f) << 1;
221 if (msg->flags & I2C_M_RD)
222 addr |= 1;
223 }
224
225 writel(addr, i2c->base + HIBVT_I2C_DATA1);
226 }
227
228 /*
229 * Start command sequence
230 */
hibvt_i2c_start_cmd(const struct hibvt_i2c_dev * i2c)231 static void hibvt_i2c_start_cmd(const struct hibvt_i2c_dev *i2c)
232 {
233 unsigned int val;
234
235 val = readl(i2c->base + HIBVT_I2C_CTRL1);
236 val |= CTRL1_CMD_START_MASK;
237 writel(val, i2c->base + HIBVT_I2C_CTRL1);
238 }
239
hibvt_i2c_wait_rx_noempty(const struct hibvt_i2c_dev * i2c)240 static int hibvt_i2c_wait_rx_noempty(const struct hibvt_i2c_dev *i2c)
241 {
242 unsigned int time_cnt = 0;
243 unsigned int val;
244
245 do {
246 val = readl(i2c->base + HIBVT_I2C_STAT);
247 if (val & STAT_RXF_NOE_MASK)
248 return 0;
249
250 udelay(50); /* delay 50ms */
251 } while (time_cnt++ < I2C_WAIT_TIMEOUT);
252
253 return -EIO;
254 }
255
hibvt_i2c_wait_tx_nofull(const struct hibvt_i2c_dev * i2c)256 static int hibvt_i2c_wait_tx_nofull(const struct hibvt_i2c_dev *i2c)
257 {
258 unsigned int time_cnt = 0;
259 unsigned int val;
260
261 do {
262 val = readl(i2c->base + HIBVT_I2C_STAT);
263 if (val & STAT_TXF_NOF_MASK)
264 return 0;
265
266 udelay(50); /* delay 50ms */
267 } while (time_cnt++ < I2C_WAIT_TIMEOUT);
268
269 return -EIO;
270 }
271
hibvt_i2c_wait_idle(const struct hibvt_i2c_dev * i2c)272 static int hibvt_i2c_wait_idle(const struct hibvt_i2c_dev *i2c)
273 {
274 unsigned int time_cnt = 0;
275 unsigned int val;
276
277 do {
278 val = readl(i2c->base + HIBVT_I2C_INTR_RAW);
279 if (val & (INTR_ABORT_MASK))
280 return -EIO;
281
282 if (val & INTR_CMD_DONE_MASK)
283 return 0;
284
285 udelay(50); /* delay 50ms */
286 } while (time_cnt++ < I2C_WAIT_TIMEOUT);
287
288 return -EIO;
289 }
290
hibvt_i2c_set_freq(struct hibvt_i2c_dev * i2c,unsigned int freq)291 static void hibvt_i2c_set_freq(struct hibvt_i2c_dev *i2c, unsigned int freq)
292 {
293 unsigned int max_freq;
294 unsigned int clk_rate;
295 unsigned int val;
296
297 clk_rate = i2c->clk;
298 max_freq = clk_rate >> 1;
299
300 if (freq > max_freq)
301 freq = max_freq;
302
303 i2c->freq = freq;
304
305 val = 0xfa;
306 writel(val, i2c->base + HIBVT_I2C_SCL_H);
307 writel(val, i2c->base + HIBVT_I2C_SCL_L);
308
309 val = readl(i2c->base + HIBVT_I2C_GLB);
310 val &= ~GLB_SDA_HOLD_MASK;
311 val |= ((0xa << GLB_SDA_HOLD_SHIFT) & GLB_SDA_HOLD_MASK);
312 writel(val, i2c->base + HIBVT_I2C_GLB);
313 }
314
315 /*
316 * set i2c controller TX and RX FIFO water
317 */
hibvt_i2c_set_water(const struct hibvt_i2c_dev * i2c,unsigned int txf_water_line,unsigned int rxf_water_line)318 static inline void hibvt_i2c_set_water(const struct hibvt_i2c_dev *i2c,
319 unsigned int txf_water_line,
320 unsigned int rxf_water_line)
321 {
322 writel(txf_water_line, i2c->base + HIBVT_I2C_TX_WATER);
323 writel(rxf_water_line, i2c->base + HIBVT_I2C_RX_WATER);
324 }
325
326 /*
327 * initialise the controller, set i2c bus interface freq
328 */
hibvt_i2c_hw_init(struct hibvt_i2c_dev * i2c)329 static void hibvt_i2c_hw_init(struct hibvt_i2c_dev *i2c)
330 {
331 hibvt_i2c_disable(i2c);
332 hibvt_i2c_disable_irq(i2c, INTR_ALL_MASK);
333 hibvt_i2c_set_freq(i2c, i2c->freq);
334 hibvt_i2c_set_water(i2c, I2C_TXF_WATER, I2C_RXF_WATER);
335 }
336
337 /*
338 * hibvt_i2c_cfg_cmd - config i2c controller command sequence
339 *
340 * After all the timing command is configured,
341 * and then start the command, you can i2c communication,
342 * and then only need to read and write i2c fifo.
343 */
hibvt_i2c_cfg_cmd(const struct hibvt_i2c_dev * i2c,struct i2c_message * msg,unsigned int index,unsigned int num)344 static void hibvt_i2c_cfg_cmd(const struct hibvt_i2c_dev *i2c,
345 struct i2c_message *msg, unsigned int index,
346 unsigned int num)
347 {
348 unsigned int offset = 0;
349
350 if (index == 0)
351 hibvt_i2c_cmdreg_set(i2c, CMD_TX_S, &offset);
352 else
353 hibvt_i2c_cmdreg_set(i2c, CMD_TX_RS, &offset);
354
355 if (msg->flags & I2C_M_TEN) {
356 if (index == 0) {
357 hibvt_i2c_cmdreg_set(i2c, CMD_TX_D1_2, &offset);
358 hibvt_i2c_cmdreg_set(i2c, CMD_TX_D1_1, &offset);
359 } else {
360 hibvt_i2c_cmdreg_set(i2c, CMD_TX_D1_2, &offset);
361 }
362 } else {
363 hibvt_i2c_cmdreg_set(i2c, CMD_TX_D1_1, &offset);
364 }
365
366 if (msg->flags & I2C_M_IGNORE_NAK)
367 hibvt_i2c_cmdreg_set(i2c, CMD_IGN_ACK, &offset);
368 else
369 hibvt_i2c_cmdreg_set(i2c, CMD_RX_ACK, &offset);
370
371 if (msg->flags & I2C_M_RD) {
372 if (msg->len >= 2) { /* 2:len */
373 writel(offset, i2c->base + HIBVT_I2C_DST1);
374 writel(msg->len - 2, i2c->base + HIBVT_I2C_LOOP1); /* 2: len */
375 hibvt_i2c_cmdreg_set(i2c, CMD_RX_FIFO, &offset);
376 hibvt_i2c_cmdreg_set(i2c, CMD_TX_ACK, &offset);
377 hibvt_i2c_cmdreg_set(i2c, CMD_JMP1, &offset);
378 }
379 hibvt_i2c_cmdreg_set(i2c, CMD_RX_FIFO, &offset);
380 hibvt_i2c_cmdreg_set(i2c, CMD_TX_NACK, &offset);
381 } else {
382 writel(offset, i2c->base + HIBVT_I2C_DST1);
383 writel(msg->len - 1, i2c->base + HIBVT_I2C_LOOP1);
384 hibvt_i2c_cmdreg_set(i2c, CMD_UP_TXF, &offset);
385 hibvt_i2c_cmdreg_set(i2c, CMD_TX_FIFO, &offset);
386
387 if (msg->flags & I2C_M_IGNORE_NAK)
388 hibvt_i2c_cmdreg_set(i2c, CMD_IGN_ACK, &offset);
389 else
390 hibvt_i2c_cmdreg_set(i2c, CMD_RX_ACK, &offset);
391
392 hibvt_i2c_cmdreg_set(i2c, CMD_JMP1, &offset);
393 }
394
395 if ((index == (num - 1)) || (msg->flags & I2C_M_STOP))
396 hibvt_i2c_cmdreg_set(i2c, CMD_TX_P, &offset);
397
398 hibvt_i2c_cmdreg_set(i2c, CMD_EXIT, &offset);
399 }
400
hibvt_i2c_polling_xfer_one_msg(const struct hibvt_i2c_dev * i2c,struct i2c_message * msg,unsigned int index,unsigned int num)401 static int hibvt_i2c_polling_xfer_one_msg(const struct hibvt_i2c_dev *i2c,
402 struct i2c_message *msg, unsigned int index, unsigned int num)
403 {
404 int status;
405 unsigned int val;
406 unsigned int iterator = 0;
407
408 hibvt_i2c_enable(i2c);
409 hibvt_i2c_clr_irq(i2c);
410 hibvt_i2c_set_addr(i2c, msg);
411 hibvt_i2c_cfg_cmd(i2c, msg, index, num);
412 hibvt_i2c_start_cmd(i2c);
413
414 if (msg->flags & I2C_M_RD) {
415 while (iterator < msg->len) {
416 status = hibvt_i2c_wait_rx_noempty(i2c);
417 if (status)
418 goto end;
419
420 val = readl(i2c->base + HIBVT_I2C_RXF);
421 msg->buf[iterator] = val;
422 iterator++;
423 }
424 } else {
425 while (iterator < msg->len) {
426 status = hibvt_i2c_wait_tx_nofull(i2c);
427 if (status)
428 goto end;
429
430 val = msg->buf[iterator];
431 writel(val, i2c->base + HIBVT_I2C_TXF);
432 iterator++;
433 }
434 }
435
436 status = hibvt_i2c_wait_idle(i2c);
437 end:
438 hibvt_i2c_disable(i2c);
439
440 return status;
441 }
442
get_i2c_dev_rel_addr(struct hibvt_i2c_dev * addr)443 struct hibvt_i2c_dev *get_i2c_dev_rel_addr(struct hibvt_i2c_dev *addr)
444 {
445 struct hibvt_i2c_dev *abs_addr = NULL;
446 struct hibvt_i2c_dev *rel_addr = NULL;
447 unsigned int delta = 0;
448
449 asm("ldr %0, =__wcy_pad\n\t"
450 "adr %1, __wcy_pad\n\t"
451 "sub %2, %0, %1\n\t"
452 "__wcy_pad:\n\t"
453 "nop"
454 :"=r"(abs_addr), "=r"(rel_addr), "=r"(delta));
455
456 abs_addr = addr;
457 rel_addr = abs_addr - delta;
458
459 return rel_addr;
460 }
461
462 /*
463 * Master transfer function
464 */
hibvt_i2c_xfer(struct i2c_message * msgs,int num)465 static int hibvt_i2c_xfer(struct i2c_message *msgs, int num)
466 {
467 struct hibvt_i2c_dev *i2c = get_i2c_dev_rel_addr(&i2c_dev);
468 unsigned int index = 0;
469 struct i2c_message *msg = msgs;
470 int status = -EINVAL;
471
472 if (!msgs || (num <= 0))
473 return -EINVAL;
474
475 while (index < num) {
476 status = hibvt_i2c_polling_xfer_one_msg(i2c, msg, index, num);
477 if (status)
478 break;
479
480 msg++;
481 index++;
482 }
483
484 if (!status || (index > 0))
485 status = index;
486
487 return status;
488 }
489
hibvt_i2c_init(void)490 void hibvt_i2c_init(void)
491 {
492 struct hibvt_i2c_dev *i2c = get_i2c_dev_rel_addr(&i2c_dev);
493
494 /* i2c6 pinmux */
495 writel(0x00400000, 0x045101A0);
496 writel(0x000014f3, 0x040580CC);
497 writel(0x000014f3, 0x040580D0);
498
499 hibvt_i2c_hw_init(i2c);
500 }
501
i2c_read(unsigned char chip,unsigned int addr,int alen,unsigned char * buffer,int len)502 int i2c_read(unsigned char chip, unsigned int addr, int alen,
503 unsigned char *buffer, int len)
504 {
505 struct i2c_message msg[2]; /* 2 bit array */
506 int len_idx = 1;
507 int cur_addr, ret;
508 unsigned char buf[4] = {0}; /* 4 bit array */
509
510 msg[0].chip = (chip >> 1);
511 msg[0].len = alen;
512 msg[0].buf = buf;
513 msg[0].flags = 0;
514
515 msg[1].chip = (chip >> 1);
516 msg[1].len = len;
517 msg[1].buf = buf;
518 msg[1].flags = 0;
519 msg[1].flags |= I2C_M_RD;
520
521 for (cur_addr = addr; len_idx <= len; cur_addr += 1) {
522 if (alen == 2) { /* 2:len */
523 buf[0] = (cur_addr >> 8) & 0xff; /* Move Right 8bit */
524 buf[1] = cur_addr & 0xff;
525 } else {
526 buf[0] = cur_addr & 0xff;
527 }
528
529 ret = hibvt_i2c_xfer(msg, 2); /* 2: msg num */
530 if (ret != 2) /* 2: Return Value */
531 return 0;
532
533 if (len == 2) /* 2:len */
534 *buffer = buf[0] | (buf[1] << 8); /* Move Left 8bit */
535 else
536 *buffer = buf[0];
537 len_idx++;
538 }
539 return 0;
540 }
541
i2c_write(unsigned char chip,unsigned int addr,int alen,const unsigned char * buffer,int len)542 int i2c_write(unsigned char chip, unsigned int addr, int alen,
543 const unsigned char *buffer, int len)
544 {
545 struct i2c_message msg;
546 unsigned char buf[4]; /* 4 layer */
547 int index = 0;
548 int ret;
549
550 memset(&msg, 0, sizeof(msg));
551 msg.chip = (chip >> 1);
552 msg.len = alen + len;
553 msg.buf = buf;
554 msg.flags = 0;
555
556 if (alen == 2) { /* 2:len */
557 buf[index] = (addr >> 8) & 0xff; /* Move Right 8bit */
558 index++;
559 buf[index] = addr & 0xff;
560 index++;
561 } else {
562 buf[index] = addr & 0xff;
563 index++;
564 }
565
566 if (len == 2) { /* 2:len */
567 buf[index] = *buffer & 0xff;
568 index++;
569 buf[index] = (*buffer >> 8) & 0xff; /* Move Right 8bit */
570 index++;
571 } else {
572 buf[index] = *buffer & 0xff;
573 index++;
574 }
575
576 ret = hibvt_i2c_xfer(&msg, 1);
577 if (ret != 1)
578 return 1;
579 return 0;
580 }
581
582