blob: 11d52bf76d25113ebd90b8986ee48bda54749551 [file] [log] [blame]
Jorge Ramirez-Ortizab853b02018-09-23 09:40:26 +02001/*
2 * Copyright (c) 2015-2018, Renesas Electronics Corporation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <mmio.h>
8#include <debug.h>
9#include "rcar_def.h"
10#include "cpg_registers.h"
11#include "iic_dvfs.h"
12#include "rcar_private.h"
13
14#define DVFS_RETRY_MAX (2U)
15
16#define IIC_DVFS_SET_ICCL_EXTAL_TYPE_0 (0x07)
17#define IIC_DVFS_SET_ICCL_EXTAL_TYPE_1 (0x09)
18#define IIC_DVFS_SET_ICCL_EXTAL_TYPE_2 (0x0B)
19#define IIC_DVFS_SET_ICCL_EXTAL_TYPE_3 (0x0E)
20#define IIC_DVFS_SET_ICCL_EXTAL_TYPE_E (0x15)
21
22#define IIC_DVFS_SET_ICCH_EXTAL_TYPE_0 (0x01)
23#define IIC_DVFS_SET_ICCH_EXTAL_TYPE_1 (0x02)
24#define IIC_DVFS_SET_ICCH_EXTAL_TYPE_2 (0x03)
25#define IIC_DVFS_SET_ICCH_EXTAL_TYPE_3 (0x05)
26#define IIC_DVFS_SET_ICCH_EXTAL_TYPE_E (0x07)
27
28#define CPG_BIT_SMSTPCR9_DVFS (0x04000000)
29
30#define IIC_DVFS_REG_BASE (0xE60B0000)
31#define IIC_DVFS_REG_ICDR (IIC_DVFS_REG_BASE + 0x0000)
32#define IIC_DVFS_REG_ICCR (IIC_DVFS_REG_BASE + 0x0004)
33#define IIC_DVFS_REG_ICSR (IIC_DVFS_REG_BASE + 0x0008)
34#define IIC_DVFS_REG_ICIC (IIC_DVFS_REG_BASE + 0x000C)
35#define IIC_DVFS_REG_ICCL (IIC_DVFS_REG_BASE + 0x0010)
36#define IIC_DVFS_REG_ICCH (IIC_DVFS_REG_BASE + 0x0014)
37
38#define IIC_DVFS_BIT_ICSR_BUSY (0x10)
39#define IIC_DVFS_BIT_ICSR_AL (0x08)
40#define IIC_DVFS_BIT_ICSR_TACK (0x04)
41#define IIC_DVFS_BIT_ICSR_WAIT (0x02)
42#define IIC_DVFS_BIT_ICSR_DTE (0x01)
43
44#define IIC_DVFS_BIT_ICCR_ENABLE (0x80)
45#define IIC_DVFS_SET_ICCR_START (0x94)
46#define IIC_DVFS_SET_ICCR_STOP (0x90)
47#define IIC_DVFS_SET_ICCR_RETRANSMISSION (0x94)
48#define IIC_DVFS_SET_ICCR_CHANGE (0x81)
49#define IIC_DVFS_SET_ICCR_STOP_READ (0xC0)
50
51#define IIC_DVFS_BIT_ICIC_TACKE (0x04)
52#define IIC_DVFS_BIT_ICIC_WAITE (0x02)
53#define IIC_DVFS_BIT_ICIC_DTEE (0x01)
54
55#define DVFS_READ_MODE (0x01)
56#define DVFS_WRITE_MODE (0x00)
57
58#define IIC_DVFS_SET_DUMMY (0x52)
59#define IIC_DVFS_SET_BUSY_LOOP (500000000U)
60
61typedef enum {
62 DVFS_START = 0,
63 DVFS_STOP,
64 DVFS_RETRANSMIT,
65 DVFS_READ,
66 DVFS_STOP_READ,
67 DVFS_SET_SLAVE_READ,
68 DVFS_SET_SLAVE,
69 DVFS_WRITE_ADDR,
70 DVFS_WRITE_DATA,
71 DVFS_CHANGE_SEND_TO_RECIEVE,
72 DVFS_DONE,
73} DVFS_STATE_T;
74
75#define DVFS_PROCESS (1)
76#define DVFS_COMPLETE (0)
77#define DVFS_ERROR (-1)
78
79#if IMAGE_BL31
80#define IIC_DVFS_FUNC(__name, ...) \
81static int32_t __attribute__ ((section (".system_ram"))) \
82dvfs_ ##__name(__VA_ARGS__)
83
84#define RCAR_DVFS_API(__name, ...) \
85int32_t __attribute__ ((section (".system_ram"))) \
86rcar_iic_dvfs_ ##__name(__VA_ARGS__)
87
88#else
89#define IIC_DVFS_FUNC(__name, ...) \
90static int32_t dvfs_ ##__name(__VA_ARGS__)
91
92#define RCAR_DVFS_API(__name, ...) \
93int32_t rcar_iic_dvfs_ ##__name(__VA_ARGS__)
94#endif
95
96IIC_DVFS_FUNC(check_error, DVFS_STATE_T *state, uint32_t *err, uint8_t mode)
97{
98 uint8_t icsr_al = 0, icsr_tack = 0;
99 uint8_t reg, stop;
100 uint32_t i = 0;
101
102 stop = mode == DVFS_READ_MODE ? IIC_DVFS_SET_ICCR_STOP_READ :
103 IIC_DVFS_SET_ICCR_STOP;
104
105 reg = mmio_read_8(IIC_DVFS_REG_ICSR);
106 icsr_al = (reg & IIC_DVFS_BIT_ICSR_AL) == IIC_DVFS_BIT_ICSR_AL;
107 icsr_tack = (reg & IIC_DVFS_BIT_ICSR_TACK) == IIC_DVFS_BIT_ICSR_TACK;
108
109 if (icsr_al == 0 && icsr_tack == 0)
110 return DVFS_PROCESS;
111
112 if (icsr_al) {
113 reg = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_AL;
114 mmio_write_8(IIC_DVFS_REG_ICSR, reg);
115
116 if (*state == DVFS_SET_SLAVE)
117 mmio_write_8(IIC_DVFS_REG_ICDR, IIC_DVFS_SET_DUMMY);
118
119 do {
120 reg = mmio_read_8(IIC_DVFS_REG_ICSR) &
121 IIC_DVFS_BIT_ICSR_WAIT;
122 } while (reg == 0);
123
124 mmio_write_8(IIC_DVFS_REG_ICCR, stop);
125
126 reg = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
127 mmio_write_8(IIC_DVFS_REG_ICSR, reg);
128
129 i = 0;
130 do {
131 reg = mmio_read_8(IIC_DVFS_REG_ICSR) &
132 IIC_DVFS_BIT_ICSR_BUSY;
133 if (reg == 0)
134 break;
135
136 if (i++ > IIC_DVFS_SET_BUSY_LOOP)
137 panic();
138
139 } while (1);
140
141 mmio_write_8(IIC_DVFS_REG_ICCR, 0x00U);
142
143 (*err)++;
144 if (*err > DVFS_RETRY_MAX)
145 return DVFS_ERROR;
146
147 *state = DVFS_START;
148
149 return DVFS_PROCESS;
150
151 }
152
153 /* icsr_tack */
154 mmio_write_8(IIC_DVFS_REG_ICCR, stop);
155
156 reg = mmio_read_8(IIC_DVFS_REG_ICIC);
157 reg &= ~(IIC_DVFS_BIT_ICIC_WAITE | IIC_DVFS_BIT_ICIC_DTEE);
158 mmio_write_8(IIC_DVFS_REG_ICIC, reg);
159
160 reg = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_TACK;
161 mmio_write_8(IIC_DVFS_REG_ICSR, reg);
162
163 i = 0;
164 while ((mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_BUSY) != 0) {
165 if (i++ > IIC_DVFS_SET_BUSY_LOOP)
166 panic();
167 }
168
169 mmio_write_8(IIC_DVFS_REG_ICCR, 0);
170 (*err)++;
171
172 if (*err > DVFS_RETRY_MAX)
173 return DVFS_ERROR;
174
175 *state = DVFS_START;
176
177 return DVFS_PROCESS;
178}
179
180IIC_DVFS_FUNC(start, DVFS_STATE_T * state)
181{
182 uint8_t iccl = IIC_DVFS_SET_ICCL_EXTAL_TYPE_E;
183 uint8_t icch = IIC_DVFS_SET_ICCH_EXTAL_TYPE_E;
184 int32_t result = DVFS_PROCESS;
185 uint32_t reg, lsi_product;
186 uint8_t mode;
187
188 mode = mmio_read_8(IIC_DVFS_REG_ICCR) | IIC_DVFS_BIT_ICCR_ENABLE;
189 mmio_write_8(IIC_DVFS_REG_ICCR, mode);
190
191 lsi_product = mmio_read_32(RCAR_PRR) & RCAR_PRODUCT_MASK;
192 if (lsi_product == RCAR_PRODUCT_E3)
193 goto start;
194
195 reg = mmio_read_32(RCAR_MODEMR) & CHECK_MD13_MD14;
196 switch (reg) {
197 case MD14_MD13_TYPE_0:
198 iccl = IIC_DVFS_SET_ICCL_EXTAL_TYPE_0;
199 icch = IIC_DVFS_SET_ICCH_EXTAL_TYPE_0;
200 break;
201 case MD14_MD13_TYPE_1:
202 iccl = IIC_DVFS_SET_ICCL_EXTAL_TYPE_1;
203 icch = IIC_DVFS_SET_ICCH_EXTAL_TYPE_1;
204 break;
205 case MD14_MD13_TYPE_2:
206 iccl = IIC_DVFS_SET_ICCL_EXTAL_TYPE_2;
207 icch = IIC_DVFS_SET_ICCH_EXTAL_TYPE_2;
208 break;
209 default:
210 iccl = IIC_DVFS_SET_ICCL_EXTAL_TYPE_3;
211 icch = IIC_DVFS_SET_ICCH_EXTAL_TYPE_3;
212 break;
213 }
214start:
215 mmio_write_8(IIC_DVFS_REG_ICCL, iccl);
216 mmio_write_8(IIC_DVFS_REG_ICCH, icch);
217
218 mode = mmio_read_8(IIC_DVFS_REG_ICIC)
219 | IIC_DVFS_BIT_ICIC_TACKE
220 | IIC_DVFS_BIT_ICIC_WAITE | IIC_DVFS_BIT_ICIC_DTEE;
221
222 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
223 mmio_write_8(IIC_DVFS_REG_ICCR, IIC_DVFS_SET_ICCR_START);
224
225 *state = DVFS_SET_SLAVE;
226
227 return result;
228}
229
230IIC_DVFS_FUNC(set_slave, DVFS_STATE_T * state, uint32_t *err, uint8_t slave)
231{
232 uint8_t mode;
233 int32_t result;
234 uint8_t address;
235
236 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
237 if (result == DVFS_ERROR)
238 return result;
239
240 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_DTE;
241 if (mode != IIC_DVFS_BIT_ICSR_DTE)
242 return result;
243
244 mode = mmio_read_8(IIC_DVFS_REG_ICIC) & ~IIC_DVFS_BIT_ICIC_DTEE;
245 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
246
247 address = slave << 1;
248 mmio_write_8(IIC_DVFS_REG_ICDR, address);
249
250 *state = DVFS_WRITE_ADDR;
251
252 return result;
253}
254
255IIC_DVFS_FUNC(write_addr, DVFS_STATE_T *state, uint32_t *err, uint8_t reg_addr)
256{
257 uint8_t mode;
258 int32_t result;
259
260 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
261 if (result == DVFS_ERROR)
262 return result;
263
264 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
265 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
266 return result;
267
268 mmio_write_8(IIC_DVFS_REG_ICDR, reg_addr);
269
270 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
271 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
272
273 *state = DVFS_WRITE_DATA;
274
275 return result;
276}
277
278IIC_DVFS_FUNC(write_data, DVFS_STATE_T *state, uint32_t *err, uint8_t reg_data)
279{
280 int32_t result;
281 uint8_t mode;
282
283 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
284 if (result == DVFS_ERROR)
285 return result;
286
287 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
288 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
289 return result;
290
291 mmio_write_8(IIC_DVFS_REG_ICDR, reg_data);
292
293 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
294 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
295
296 *state = DVFS_STOP;
297
298 return result;
299}
300
301IIC_DVFS_FUNC(stop, DVFS_STATE_T *state, uint32_t *err)
302{
303 int32_t result;
304 uint8_t mode;
305
306 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
307 if (result == DVFS_ERROR)
308 return result;
309
310 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
311 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
312 return result;
313
314 mmio_write_8(IIC_DVFS_REG_ICCR, IIC_DVFS_SET_ICCR_STOP);
315
316 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
317 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
318
319 *state = DVFS_DONE;
320
321 return result;
322}
323
324IIC_DVFS_FUNC(done, void)
325{
326 uint32_t i;
327
328 for (i = 0; i < IIC_DVFS_SET_BUSY_LOOP; i++) {
329 if (mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_BUSY)
330 continue;
331 goto done;
332 }
333
334 panic();
335done:
336 mmio_write_8(IIC_DVFS_REG_ICCR, 0);
337
338 return DVFS_COMPLETE;
339}
340
341IIC_DVFS_FUNC(write_reg_addr_read, DVFS_STATE_T *state, uint32_t *err,
342 uint8_t reg_addr)
343{
344 int32_t result;
345 uint8_t mode;
346
347 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
348 if (result == DVFS_ERROR)
349 return result;
350
351 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
352 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
353 return result;
354
355 mmio_write_8(IIC_DVFS_REG_ICDR, reg_addr);
356
357 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
358 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
359
360 *state = DVFS_RETRANSMIT;
361
362 return result;
363}
364
365IIC_DVFS_FUNC(retransmit, DVFS_STATE_T *state, uint32_t *err)
366{
367 int32_t result;
368 uint8_t mode;
369
370 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
371 if (result == DVFS_ERROR)
372 return result;
373
374 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
375 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
376 return result;
377
378 mmio_write_8(IIC_DVFS_REG_ICCR, IIC_DVFS_SET_ICCR_RETRANSMISSION);
379
380 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
381 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
382
383 mode = mmio_read_8(IIC_DVFS_REG_ICIC) | IIC_DVFS_BIT_ICIC_DTEE;
384 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
385
386 *state = DVFS_SET_SLAVE_READ;
387
388 return result;
389}
390
391IIC_DVFS_FUNC(set_slave_read, DVFS_STATE_T *state, uint32_t *err,
392 uint8_t slave)
393{
394 uint8_t address;
395 int32_t result;
396 uint8_t mode;
397
398 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
399 if (result == DVFS_ERROR)
400 return result;
401
402 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_DTE;
403 if (mode != IIC_DVFS_BIT_ICSR_DTE)
404 return result;
405
406 mode = mmio_read_8(IIC_DVFS_REG_ICIC) & ~IIC_DVFS_BIT_ICIC_DTEE;
407 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
408
409 address = ((uint8_t) (slave << 1) + DVFS_READ_MODE);
410 mmio_write_8(IIC_DVFS_REG_ICDR, address);
411
412 *state = DVFS_CHANGE_SEND_TO_RECIEVE;
413
414 return result;
415}
416
417IIC_DVFS_FUNC(change_send_to_recieve, DVFS_STATE_T *state, uint32_t *err)
418{
419 int32_t result;
420 uint8_t mode;
421
422 result = dvfs_check_error(state, err, DVFS_WRITE_MODE);
423 if (result == DVFS_ERROR)
424 return result;
425
426 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
427 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
428 return result;
429
430 mmio_write_8(IIC_DVFS_REG_ICCR, IIC_DVFS_SET_ICCR_CHANGE);
431
432 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
433 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
434
435 *state = DVFS_STOP_READ;
436
437 return result;
438}
439
440IIC_DVFS_FUNC(stop_read, DVFS_STATE_T *state, uint32_t *err)
441{
442 int32_t result;
443 uint8_t mode;
444
445 result = dvfs_check_error(state, err, DVFS_READ_MODE);
446 if (result == DVFS_ERROR)
447 return result;
448
449 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_WAIT;
450 if (mode != IIC_DVFS_BIT_ICSR_WAIT)
451 return result;
452
453 mmio_write_8(IIC_DVFS_REG_ICCR, IIC_DVFS_SET_ICCR_STOP_READ);
454
455 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & ~IIC_DVFS_BIT_ICSR_WAIT;
456 mmio_write_8(IIC_DVFS_REG_ICSR, mode);
457
458 mode = mmio_read_8(IIC_DVFS_REG_ICIC) | IIC_DVFS_BIT_ICIC_DTEE;
459 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
460
461 *state = DVFS_READ;
462
463 return result;
464}
465
466IIC_DVFS_FUNC(read, DVFS_STATE_T *state, uint8_t *reg_data)
467{
468 uint8_t mode;
469
470 mode = mmio_read_8(IIC_DVFS_REG_ICSR) & IIC_DVFS_BIT_ICSR_DTE;
471 if (mode != IIC_DVFS_BIT_ICSR_DTE)
472 return DVFS_PROCESS;
473
474 mode = mmio_read_8(IIC_DVFS_REG_ICIC) & ~IIC_DVFS_BIT_ICIC_DTEE;
475 mmio_write_8(IIC_DVFS_REG_ICIC, mode);
476
477 *reg_data = mmio_read_8(IIC_DVFS_REG_ICDR);
478 *state = DVFS_DONE;
479
480 return DVFS_PROCESS;
481}
482
483RCAR_DVFS_API(send, uint8_t slave, uint8_t reg_addr, uint8_t reg_data)
484{
485 DVFS_STATE_T state = DVFS_START;
486 int32_t result = DVFS_PROCESS;
487 uint32_t err = 0;
488
489 mstpcr_write(SCMSTPCR9, CPG_MSTPSR9, CPG_BIT_SMSTPCR9_DVFS);
490 mmio_write_8(IIC_DVFS_REG_ICCR, 0);
491again:
492 switch (state) {
493 case DVFS_START:
494 result = dvfs_start(&state);
495 break;
496 case DVFS_SET_SLAVE:
497 result = dvfs_set_slave(&state, &err, slave);
498 break;
499 case DVFS_WRITE_ADDR:
500 result = dvfs_write_addr(&state, &err, reg_addr);
501 break;
502 case DVFS_WRITE_DATA:
503 result = dvfs_write_data(&state, &err, reg_data);
504 break;
505 case DVFS_STOP:
506 result = dvfs_stop(&state, &err);
507 break;
508 case DVFS_DONE:
509 result = dvfs_done();
510 break;
511 default:
512 panic();
513 break;
514 }
515
516 if (result == DVFS_PROCESS)
517 goto again;
518
519 return result;
520}
521
522RCAR_DVFS_API(receive, uint8_t slave, uint8_t reg, uint8_t *data)
523{
524 DVFS_STATE_T state = DVFS_START;
525 int32_t result = DVFS_PROCESS;
526 uint32_t err = 0;
527
528 mstpcr_write(SCMSTPCR9, CPG_MSTPSR9, CPG_BIT_SMSTPCR9_DVFS);
529 mmio_write_8(IIC_DVFS_REG_ICCR, 0);
530again:
531 switch (state) {
532 case DVFS_START:
533 result = dvfs_start(&state);
534 break;
535 case DVFS_SET_SLAVE:
536 result = dvfs_set_slave(&state, &err, slave);
537 break;
538 case DVFS_WRITE_ADDR:
539 result = dvfs_write_reg_addr_read(&state, &err, reg);
540 break;
541 case DVFS_RETRANSMIT:
542 result = dvfs_retransmit(&state, &err);
543 break;
544 case DVFS_SET_SLAVE_READ:
545 result = dvfs_set_slave_read(&state, &err, slave);
546 break;
547 case DVFS_CHANGE_SEND_TO_RECIEVE:
548 result = dvfs_change_send_to_recieve(&state, &err);
549 break;
550 case DVFS_STOP_READ:
551 result = dvfs_stop_read(&state, &err);
552 break;
553 case DVFS_READ:
554 result = dvfs_read(&state, data);
555 break;
556 case DVFS_DONE:
557 result = dvfs_done();
558 break;
559 default:
560 panic();
561 break;
562 }
563
564 if (result == DVFS_PROCESS)
565 goto again;
566
567 return result;
568}