coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
dw_i2c.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpigen.h>
4 #include <device/mmio.h>
5 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/i2c_bus.h>
8 #include <device/i2c_simple.h>
9 #include <string.h>
10 #include <timer.h>
11 #include <types.h>
12 #include "dw_i2c.h"
13 
14 /* Use a ~10ms timeout for various operations */
15 #define DW_I2C_TIMEOUT_US 10000
16 /* Timeout for waiting for FIFO to flush */
17 #define DW_I2C_FLUSH_TIMEOUT_US 160000
18 
19 /* High and low times in different speed modes (in ns) */
20 enum {
21  /* SDA Hold Time */
23  /* Standard Speed */
26  /* Fast Speed */
29  /* Fast Plus Speed */
32  /* High Speed */
35 };
36 
37 /* Frequency represented as ticks per ns. Can also be used to calculate
38  * the number of ticks to meet a time target or the period. */
39 struct freq {
42 };
43 
44 /* Control register definitions */
45 enum {
46  CONTROL_MASTER_MODE = (1 << 0),
47  CONTROL_SPEED_SS = (1 << 1),
48  CONTROL_SPEED_FS = (2 << 1),
49  CONTROL_SPEED_HS = (3 << 1),
50  CONTROL_SPEED_MASK = (3 << 1),
51  CONTROL_10BIT_SLAVE = (1 << 3),
55 };
56 
57 /* Command/Data register definitions */
58 enum {
59  CMD_DATA_CMD = (1 << 8),
60  CMD_DATA_STOP = (1 << 9),
61 };
62 
63 /* Status register definitions */
64 enum {
65  STATUS_ACTIVITY = (1 << 0),
69  STATUS_RX_FIFO_FULL = (1 << 4),
72 };
73 
74 /* Enable register definitions */
75 enum {
76  ENABLE_CONTROLLER = (1 << 0),
77 };
78 
79 /* Interrupt status register definitions */
80 enum {
81  INTR_STAT_RX_UNDER = (1 << 0),
82  INTR_STAT_RX_OVER = (1 << 1),
83  INTR_STAT_RX_FULL = (1 << 2),
84  INTR_STAT_TX_OVER = (1 << 3),
85  INTR_STAT_TX_EMPTY = (1 << 4),
86  INTR_STAT_RD_REQ = (1 << 5),
87  INTR_STAT_TX_ABORT = (1 << 6),
88  INTR_STAT_RX_DONE = (1 << 7),
89  INTR_STAT_ACTIVITY = (1 << 8),
90  INTR_STAT_STOP_DET = (1 << 9),
91  INTR_STAT_START_DET = (1 << 10),
92  INTR_STAT_GEN_CALL = (1 << 11),
93 };
94 
95 /* I2C Controller MMIO register space */
96 struct dw_i2c_regs {
97  uint32_t control; /* 0x0 */
98  uint32_t target_addr; /* 0x4 */
99  uint32_t slave_addr; /* 0x8 */
101  uint32_t cmd_data; /* 0x10 */
102  uint32_t ss_scl_hcnt; /* 0x14 */
103  uint32_t ss_scl_lcnt; /* 0x18 */
104  uint32_t fs_scl_hcnt; /* 0x1c */
105  uint32_t fs_scl_lcnt; /* 0x20 */
106  uint32_t hs_scl_hcnt; /* 0x24 */
107  uint32_t hs_scl_lcnt; /* 0x28 */
108  uint32_t intr_stat; /* 0x2c */
109  uint32_t intr_mask; /* 0x30 */
111  uint32_t rx_thresh; /* 0x38 */
112  uint32_t tx_thresh; /* 0x3c */
113  uint32_t clear_intr; /* 0x40 */
124  uint32_t enable; /* 0x6c */
125  uint32_t status; /* 0x70 */
126  uint32_t tx_level; /* 0x74 */
127  uint32_t rx_level; /* 0x78 */
128  uint32_t sda_hold; /* 0x7c */
131  uint32_t dma_cr; /* 0x88 */
132  uint32_t dma_tdlr; /* 0x8c */
133  uint32_t dma_rdlr; /* 0x90 */
134  uint32_t sda_setup; /* 0x94 */
137  uint32_t fs_spklen; /* 0xa0 */
138  uint32_t hs_spklen; /* 0xa4 */
140  uint32_t reserved[18]; /* 0xac - 0xf0 */
141  uint32_t comp_param1; /* 0xf4 */
142  uint32_t comp_version; /* 0xf8 */
143  uint32_t comp_type; /* 0xfc */
145 
146 /* Constant value defined in the DesignWare DW_apb_i2c Databook. */
147 #define DW_I2C_COMP_TYPE 0x44570140
148 
149 static const struct i2c_descriptor {
150  enum i2c_speed speed;
151  struct freq freq;
154 } speed_descriptors[] = {
155  {
157  .freq = {
158  .ticks = 100,
159  .ns = 1000*1000,
160  },
161  .min_thigh_ns = MIN_SS_SCL_HIGHTIME,
162  .min_tlow_ns = MIN_SS_SCL_LOWTIME,
163  },
164  {
165  .speed = I2C_SPEED_FAST,
166  .freq = {
167  .ticks = 400,
168  .ns = 1000*1000,
169  },
170  .min_thigh_ns = MIN_FS_SCL_HIGHTIME,
171  .min_tlow_ns = MIN_FS_SCL_LOWTIME,
172  },
173  {
174  .speed = I2C_SPEED_FAST_PLUS,
175  .freq = {
176  .ticks = 1,
177  .ns = 1000,
178  },
179  .min_thigh_ns = MIN_FP_SCL_HIGHTIME,
180  .min_tlow_ns = MIN_FP_SCL_LOWTIME,
181  },
182  {
183  /* 100pF max capacitance */
184  .speed = I2C_SPEED_HIGH,
185  .freq = {
186  .ticks = 3400,
187  .ns = 1000*1000,
188  },
189  .min_thigh_ns = MIN_HS_SCL_HIGHTIME,
190  .min_tlow_ns = MIN_HS_SCL_LOWTIME,
191  },
192 };
193 
194 static const struct soc_clock {
196  struct freq freq;
197 } soc_clocks[] = {
198  {
199  .clk_speed_mhz = 120,
200  .freq = {
201  .ticks = 120,
202  .ns = 1000,
203  },
204  },
205  {
206  .clk_speed_mhz = 133,
207  .freq = {
208  .ticks = 400,
209  .ns = 3000,
210  },
211  },
212  {
213  .clk_speed_mhz = 150,
214  .freq = {
215  .ticks = 600,
216  .ns = 4000,
217  },
218  },
219  {
220  .clk_speed_mhz = 216,
221  .freq = {
222  .ticks = 1080,
223  .ns = 5000,
224  },
225  },
226 };
227 
229 {
230  size_t i;
231 
232  for (i = 0; i < ARRAY_SIZE(speed_descriptors); i++)
233  if (speed == speed_descriptors[i].speed)
234  return &speed_descriptors[i];
235 
236  return NULL;
237 }
238 
239 static const struct soc_clock *get_soc_descriptor(int ic_clk)
240 {
241  size_t i;
242 
243  for (i = 0; i < ARRAY_SIZE(soc_clocks); i++)
244  if (ic_clk == soc_clocks[i].clk_speed_mhz)
245  return &soc_clocks[i];
246 
247  return NULL;
248 }
249 
250 static int counts_from_time(const struct freq *f, int ns)
251 {
252  return DIV_ROUND_UP(f->ticks * ns, f->ns);
253 }
254 
255 static int counts_from_freq(const struct freq *fast, const struct freq *slow)
256 {
257  return DIV_ROUND_UP(fast->ticks * slow->ns, fast->ns * slow->ticks);
258 }
259 
260 /* Enable this I2C controller */
261 static void dw_i2c_enable(struct dw_i2c_regs *regs)
262 {
263  uint32_t enable = read32(&regs->enable);
264 
265  if (!(enable & ENABLE_CONTROLLER))
266  write32(&regs->enable, enable | ENABLE_CONTROLLER);
267 }
268 
269 /* Disable this I2C controller */
270 static enum cb_err dw_i2c_disable(struct dw_i2c_regs *regs)
271 {
272  uint32_t enable = read32(&regs->enable);
273 
274  if (enable & ENABLE_CONTROLLER) {
275  struct stopwatch sw;
276 
277  write32(&regs->enable, enable & ~ENABLE_CONTROLLER);
278 
279  /* Wait for enable bit to clear */
281  while (read32(&regs->enable_status) & ENABLE_CONTROLLER)
282  if (stopwatch_expired(&sw))
283  return CB_ERR;
284  }
285 
286  return CB_SUCCESS;
287 }
288 
289 /* Wait for this I2C controller to go idle for transmit */
290 static enum cb_err dw_i2c_wait_for_bus_idle(struct dw_i2c_regs *regs)
291 {
292  struct stopwatch sw;
293 
294  /* Start timeout for up to 16 bytes in FIFO */
296 
297  while (!stopwatch_expired(&sw)) {
298  uint32_t status = read32(&regs->status);
299 
300  /* Check for master activity and keep waiting */
301  if (status & STATUS_MASTER_ACTIVITY)
302  continue;
303 
304  /* Check for TX FIFO empty to indicate TX idle */
305  if (status & STATUS_TX_FIFO_EMPTY)
306  return CB_SUCCESS;
307  }
308 
309  /* Timed out while waiting for bus to go idle */
310  return CB_ERR;
311 }
312 
313 /* Transfer one byte of one segment, sending stop bit if requested */
314 static enum cb_err dw_i2c_transfer_byte(struct dw_i2c_regs *regs,
315  const struct i2c_msg *segment,
316  size_t byte, int send_stop)
317 {
318  struct stopwatch sw;
319  uint32_t cmd = CMD_DATA_CMD; /* Read op */
320 
321  stopwatch_init_usecs_expire(&sw, CONFIG_I2C_TRANSFER_TIMEOUT_US);
322 
323  if (!(segment->flags & I2C_M_RD)) {
324  /* Write op only: Wait for FIFO not full */
325  while (!(read32(&regs->status) & STATUS_TX_FIFO_NOT_FULL)) {
326  if (stopwatch_expired(&sw)) {
327  printk(BIOS_ERR, "I2C transmit timeout\n");
328  return CB_ERR;
329  }
330  }
331  cmd = segment->buf[byte];
332  }
333 
334  /* Send stop on last byte, if desired */
335  if (send_stop && byte == segment->len - 1)
336  cmd |= CMD_DATA_STOP;
337 
338  write32(&regs->cmd_data, cmd);
339 
340  if (segment->flags & I2C_M_RD) {
341  /* Read op only: Wait for FIFO data and store it */
342  while (!(read32(&regs->status) & STATUS_RX_FIFO_NOT_EMPTY)) {
343  if (stopwatch_expired(&sw)) {
344  printk(BIOS_ERR, "I2C receive timeout\n");
345  return CB_ERR;
346  }
347  }
348  segment->buf[byte] = read32(&regs->cmd_data);
349  }
350 
351  return CB_SUCCESS;
352 }
353 
354 static enum cb_err _dw_i2c_transfer(unsigned int bus, const struct i2c_msg *segments,
355  size_t count)
356 {
357  struct stopwatch sw;
358  struct dw_i2c_regs *regs;
359  size_t byte;
360  enum cb_err ret = CB_ERR;
361 
363  if (!regs) {
364  printk(BIOS_ERR, "I2C bus %u base address not found\n", bus);
365  return CB_ERR;
366  }
367 
368  /* The assumption is that the host controller is disabled -- either
369  after running this function or from performing the initialization
370  sequence in dw_i2c_init(). */
371 
372  /* Set target slave address */
373  write32(&regs->target_addr, segments->slave);
374 
376 
377  /* Process each segment */
378  while (count--) {
379  if (CONFIG(DRIVERS_I2C_DESIGNWARE_DEBUG)) {
380  printk(BIOS_DEBUG, "i2c %u:%02x %s %d bytes : ",
381  bus, segments->slave,
382  (segments->flags & I2C_M_RD) ? "R" : "W",
383  segments->len);
384  }
385 
386  /* Read or write each byte in segment */
387  for (byte = 0; byte < segments->len; byte++) {
388  /*
389  * Set stop condition on final segment only.
390  * Repeated start will be automatically generated
391  * by the controller on R->W or W->R switch.
392  */
393  if (dw_i2c_transfer_byte(regs, segments, byte, count == 0) !=
394  CB_SUCCESS) {
395  printk(BIOS_ERR, "I2C %s failed: bus %u "
396  "addr 0x%02x\n",
397  (segments->flags & I2C_M_RD) ?
398  "read" : "write", bus, segments->slave);
399  goto out;
400  }
401  }
402 
403  if (CONFIG(DRIVERS_I2C_DESIGNWARE_DEBUG)) {
404  int j;
405  for (j = 0; j < segments->len; j++)
406  printk(BIOS_DEBUG, "%02x ", segments->buf[j]);
407  printk(BIOS_DEBUG, "\n");
408  }
409 
410  segments++;
411  }
412 
413  /* Wait for interrupt status to indicate transfer is complete */
414  stopwatch_init_usecs_expire(&sw, CONFIG_I2C_TRANSFER_TIMEOUT_US);
415  while (!(read32(&regs->raw_intr_stat) & INTR_STAT_STOP_DET)) {
416  if (stopwatch_expired(&sw)) {
417  printk(BIOS_ERR, "I2C stop bit not received\n");
418  goto out;
419  }
420  }
421 
422  /* Read to clear INTR_STAT_STOP_DET */
423  read32(&regs->clear_stop_det_intr);
424 
425  /* Check TX abort */
426  if (read32(&regs->raw_intr_stat) & INTR_STAT_TX_ABORT) {
427  printk(BIOS_ERR, "I2C TX abort detected (%08x)\n",
428  read32(&regs->tx_abort_source));
429  /* clear INTR_STAT_TX_ABORT */
430  read32(&regs->clear_tx_abrt_intr);
431  goto out;
432  }
433 
434  /* Wait for the bus to go idle */
436  printk(BIOS_ERR, "I2C timeout waiting for bus %u idle\n", bus);
437  goto out;
438  }
439 
440  /* Flush the RX FIFO in case it is not empty */
442  while (read32(&regs->status) & STATUS_RX_FIFO_NOT_EMPTY) {
443  if (stopwatch_expired(&sw)) {
444  printk(BIOS_ERR, "I2C timeout flushing RX FIFO\n");
445  goto out;
446  }
447  read32(&regs->cmd_data);
448  }
449 
450  ret = CB_SUCCESS;
451 
452 out:
453  read32(&regs->clear_intr);
455  return ret;
456 }
457 
458 static enum cb_err dw_i2c_transfer(unsigned int bus, const struct i2c_msg *msg, size_t count)
459 {
460  const struct i2c_msg *orig_msg = msg;
461  size_t i;
462  size_t start;
463  uint16_t addr;
464 
465  if (count == 0 || !msg)
466  return -1;
467 
468  /* Break up the transfers at the differing slave address boundary. */
469  addr = orig_msg->slave;
470 
471  for (i = 0, start = 0; i < count; i++, msg++) {
472  if (addr != msg->slave) {
473  if (_dw_i2c_transfer(bus, &orig_msg[start], i - start) != CB_SUCCESS)
474  return CB_ERR;
475  start = i;
476  addr = msg->slave;
477  }
478  }
479 
480  return _dw_i2c_transfer(bus, &orig_msg[start], count - start);
481 }
482 
483 /* Global I2C bus handler, defined in include/device/i2c_simple.h */
484 int platform_i2c_transfer(unsigned int bus, struct i2c_msg *msg, int count)
485 {
486  return dw_i2c_transfer(bus, msg, count < 0 ? 0 : count) == CB_SUCCESS ? 0 : -1;
487 }
488 
489 static enum cb_err dw_i2c_set_speed_config(unsigned int bus,
490  const struct dw_i2c_speed_config *config)
491 {
492  struct dw_i2c_regs *regs;
493  void *hcnt_reg, *lcnt_reg;
494 
496  if (!regs || !config)
497  return CB_ERR;
498 
499  /* Nothing to do if no values are set */
500  if (!config->scl_lcnt && !config->scl_hcnt && !config->sda_hold)
501  return CB_SUCCESS;
502 
503  if (config->speed >= I2C_SPEED_HIGH) {
504  /* High and Fast Ultra speed */
505  hcnt_reg = &regs->hs_scl_hcnt;
506  lcnt_reg = &regs->hs_scl_lcnt;
507  } else if (config->speed >= I2C_SPEED_FAST) {
508  /* Fast and Fast-Plus speed */
509  hcnt_reg = &regs->fs_scl_hcnt;
510  lcnt_reg = &regs->fs_scl_lcnt;
511  } else {
512  /* Standard speed */
513  hcnt_reg = &regs->ss_scl_hcnt;
514  lcnt_reg = &regs->ss_scl_lcnt;
515  }
516 
517  /* SCL count must be set after the speed is selected */
518  if (config->scl_hcnt)
519  write32(hcnt_reg, config->scl_hcnt);
520  if (config->scl_lcnt)
521  write32(lcnt_reg, config->scl_lcnt);
522 
523  /* Set SDA Hold Time register */
524  if (config->sda_hold)
525  write32(&regs->sda_hold, config->sda_hold);
526 
527  return CB_SUCCESS;
528 }
529 
531  enum i2c_speed speed,
532  const struct dw_i2c_bus_config *bcfg,
533  int ic_clk,
534  struct dw_i2c_speed_config *config)
535 {
536  const struct i2c_descriptor *bus;
537  const struct soc_clock *soc;
538  int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt, spk_cnt;
539  int hcnt, lcnt, period_cnt, diff, tot;
540  int data_hold_time_ns;
541 
542  bus = get_bus_descriptor(speed);
543  soc = get_soc_descriptor(ic_clk);
544 
545  if (bus == NULL) {
546  printk(BIOS_ERR, "dw_i2c: invalid bus speed %d\n", speed);
547  return CB_ERR;
548  }
549 
550  if (soc == NULL) {
551  printk(BIOS_ERR, "dw_i2c: invalid SoC clock speed %d MHz\n",
552  ic_clk);
553  return CB_ERR;
554  }
555 
556  /* Get the proper spike suppression count based on target speed. */
557  if (speed >= I2C_SPEED_HIGH)
558  spk_cnt = read32(&regs->hs_spklen);
559  else
560  spk_cnt = read32(&regs->fs_spklen);
561 
562  /* Find the period, rise, fall, min tlow, and min thigh in terms of
563  * counts of SoC clock. */
564  period_cnt = counts_from_freq(&soc->freq, &bus->freq);
565  rise_cnt = counts_from_time(&soc->freq, bcfg->rise_time_ns);
566  fall_cnt = counts_from_time(&soc->freq, bcfg->fall_time_ns);
567  min_tlow_cnt = counts_from_time(&soc->freq, bus->min_tlow_ns);
568  min_thigh_cnt = counts_from_time(&soc->freq, bus->min_thigh_ns);
569 
570  printk(DW_I2C_DEBUG, "dw_i2c: SoC %d/%d ns Bus: %d/%d ns\n",
571  soc->freq.ticks, soc->freq.ns, bus->freq.ticks, bus->freq.ns);
573  "dw_i2c: period %d rise %d fall %d tlow %d thigh %d spk %d\n",
574  period_cnt, rise_cnt, fall_cnt, min_tlow_cnt, min_thigh_cnt,
575  spk_cnt);
576 
577  /*
578  * Back solve for hcnt and lcnt according to the following equations.
579  * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time
580  * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
581  */
582  hcnt = min_thigh_cnt - fall_cnt - 7 - spk_cnt;
583  lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1;
584 
585  if (hcnt < 0 || lcnt < 0) {
586  printk(BIOS_ERR, "dw_i2c: bad counts. hcnt = %d lcnt = %d\n",
587  hcnt, lcnt);
588  return CB_ERR;
589  }
590 
591  /* Now add things back up to ensure the period is hit. If off,
592  * split the difference and bias to lcnt for remainder. */
593  tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
594 
595  if (tot < period_cnt) {
596  diff = (period_cnt - tot) / 2;
597  hcnt += diff;
598  lcnt += diff;
599  tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
600  lcnt += period_cnt - tot;
601  }
602 
603  config->speed = speed;
604  config->scl_lcnt = lcnt;
605  config->scl_hcnt = hcnt;
606 
607  /* Use internal default unless other value is specified. */
608  data_hold_time_ns = DEFAULT_SDA_HOLD_TIME;
609  if (bcfg->data_hold_time_ns)
610  data_hold_time_ns = bcfg->data_hold_time_ns;
611 
612  config->sda_hold = counts_from_time(&soc->freq, data_hold_time_ns);
613 
614  printk(DW_I2C_DEBUG, "dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n",
615  hcnt, lcnt, config->sda_hold);
616 
617  return CB_SUCCESS;
618 }
619 
620 enum cb_err dw_i2c_gen_speed_config(uintptr_t dw_i2c_addr,
621  enum i2c_speed speed,
622  const struct dw_i2c_bus_config *bcfg,
623  struct dw_i2c_speed_config *config)
624 {
625  const int ic_clk = CONFIG_DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ;
626  struct dw_i2c_regs *regs;
627  int i;
628 
629  regs = (struct dw_i2c_regs *)dw_i2c_addr;
630 
631  _Static_assert(CONFIG_DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ != 0,
632  "DRIVERS_I2C_DESIGNWARE_CLOCK_MHZ can't be zero!");
633 
634  /* Apply board specific override for this speed if found */
635  for (i = 0; i < DW_I2C_SPEED_CONFIG_COUNT; i++) {
636  if (bcfg->speed_config[i].speed != speed)
637  continue;
638  memcpy(config, &bcfg->speed_config[i], sizeof(*config));
639  return CB_SUCCESS;
640  }
641 
642  /* Use the time calculation. */
643  return dw_i2c_gen_config_rise_fall_time(regs, speed, bcfg, ic_clk, config);
644 }
645 
646 static enum cb_err dw_i2c_set_speed(unsigned int bus, enum i2c_speed speed,
647  const struct dw_i2c_bus_config *bcfg)
648 {
649  struct dw_i2c_regs *regs;
651  uint32_t control;
652 
653  /* Clock must be provided by Kconfig */
655  if (!regs || !speed)
656  return CB_ERR;
657 
658  control = read32(&regs->control);
660 
661  if (speed >= I2C_SPEED_HIGH) {
662  /* High and Fast-Ultra speed share config registers */
664  } else if (speed >= I2C_SPEED_FAST) {
665  /* Fast speed and Fast-Plus */
667  } else {
668  /* Standard speed */
670  }
671 
672  /* Generate speed config based on clock */
673  if (dw_i2c_gen_speed_config((uintptr_t)regs, speed, bcfg, &config) != CB_SUCCESS)
674  return CB_ERR;
675 
676  /* Select this speed in the control register */
677  write32(&regs->control, control);
678 
679  /* Write the speed config that was generated earlier */
681 
682  return CB_SUCCESS;
683 }
684 
685 /*
686  * Initialize this bus controller and set the speed.
687  *
688  * The bus speed can be passed in Hz or using values from device/i2c.h and
689  * will default to I2C_SPEED_FAST if it is not provided.
690  */
691 enum cb_err dw_i2c_init(unsigned int bus, const struct dw_i2c_bus_config *bcfg)
692 {
693  struct dw_i2c_regs *regs;
694  enum i2c_speed speed;
695 
696  if (!bcfg)
697  return CB_ERR;
698 
699  speed = bcfg->speed ? : I2C_SPEED_FAST;
700 
702  if (!regs) {
703  printk(BIOS_ERR, "I2C bus %u base address not found\n", bus);
704  return CB_ERR;
705  }
706 
707  if (read32(&regs->comp_type) != DW_I2C_COMP_TYPE) {
708  printk(BIOS_ERR, "I2C bus %u has unknown type 0x%x.\n", bus,
709  read32(&regs->comp_type));
710  return CB_ERR;
711  }
712 
713  printk(BIOS_DEBUG, "I2C bus %u version 0x%x\n", bus, read32(&regs->comp_version));
714 
715  if (dw_i2c_disable(regs) != CB_SUCCESS) {
716  printk(BIOS_ERR, "I2C timeout disabling bus %u\n", bus);
717  return CB_ERR;
718  }
719 
720  /* Put controller in master mode with restart enabled */
723 
724  /* Set bus speed to FAST by default */
725  if (dw_i2c_set_speed(bus, speed, bcfg) != CB_SUCCESS) {
726  printk(BIOS_ERR, "I2C failed to set speed for bus %u\n", bus);
727  return CB_ERR;
728  }
729 
730  /* Set RX/TX thresholds to smallest values */
731  write32(&regs->rx_thresh, 0);
732  write32(&regs->tx_thresh, 0);
733 
734  /* Enable stop detection and TX abort interrupt */
736 
737  printk(BIOS_INFO, "DW I2C bus %u at %p (%u KHz)\n",
738  bus, regs, speed / KHz);
739 
740  return CB_SUCCESS;
741 }
742 
743 /*
744  * Write ACPI object to describe speed configuration.
745  *
746  * ACPI Object: Name ("xxxx", Package () { scl_lcnt, scl_hcnt, sda_hold }
747  *
748  * SSCN: I2C_SPEED_STANDARD
749  * FMCN: I2C_SPEED_FAST
750  * FPCN: I2C_SPEED_FAST_PLUS
751  * HSCN: I2C_SPEED_HIGH
752  */
754  const struct dw_i2c_speed_config *config)
755 {
756  if (!config)
757  return;
758  if (!config->scl_lcnt && !config->scl_hcnt && !config->sda_hold)
759  return;
760 
761  if (config->speed >= I2C_SPEED_HIGH)
762  acpigen_write_name("HSCN");
763  else if (config->speed >= I2C_SPEED_FAST_PLUS)
764  acpigen_write_name("FPCN");
765  else if (config->speed >= I2C_SPEED_FAST)
766  acpigen_write_name("FMCN");
767  else
768  acpigen_write_name("SSCN");
769 
770  /* Package () { scl_lcnt, scl_hcnt, sda_hold } */
772  acpigen_write_word(config->scl_hcnt);
773  acpigen_write_word(config->scl_lcnt);
774  acpigen_write_dword(config->sda_hold);
775  acpigen_pop_len();
776 }
777 
778 /*
779  * The device should already be enabled and out of reset,
780  * either from early init in coreboot or SiliconInit in FSP.
781  */
782 void dw_i2c_dev_init(struct device *dev)
783 {
784  const struct dw_i2c_bus_config *config;
785  int bus = dw_i2c_soc_dev_to_bus(dev);
786 
787  if (bus < 0)
788  return;
789 
791 
792  if (!config)
793  return;
794 
796 }
797 
798 /*
799  * Generate I2C timing information into the SSDT for the OS driver to consume,
800  * optionally applying override values provided by the caller.
801  */
802 void dw_i2c_acpi_fill_ssdt(const struct device *dev)
803 {
804  const struct dw_i2c_bus_config *bcfg;
805  uintptr_t dw_i2c_addr;
806  struct dw_i2c_speed_config sgen;
807  int bus;
808  const char *path;
809  unsigned int speed, i;
810 
811  bus = dw_i2c_soc_dev_to_bus(dev);
812 
813  if (bus < 0)
814  return;
815 
816  bcfg = dw_i2c_get_soc_cfg(bus);
817 
818  if (!bcfg)
819  return;
820 
821  dw_i2c_addr = dw_i2c_base_address(bus);
822  if (!dw_i2c_addr)
823  return;
824 
825  path = acpi_device_path(dev);
826  if (!path)
827  return;
828 
829  /* Ensure a default speed is available */
830  speed = (bcfg->speed == 0) ? I2C_SPEED_FAST : bcfg->speed;
831 
832  /* Report currently used timing values for the OS driver */
833  acpigen_write_scope(path);
834  if (dw_i2c_gen_speed_config(dw_i2c_addr, speed, bcfg, &sgen) == CB_SUCCESS) {
836  }
837  /* Now check if there are more speed settings available and report them as well. */
838  for (i = 0; i < DW_I2C_SPEED_CONFIG_COUNT; i++) {
839  if (bcfg->speed_config[i].speed && speed != bcfg->speed_config[i].speed)
841  }
843 }
844 
845 static int dw_i2c_dev_transfer(struct device *dev,
846  const struct i2c_msg *msg, size_t count)
847 {
848  return dw_i2c_transfer(dw_i2c_soc_dev_to_bus(dev), msg, count);
849 }
850 
851 const struct i2c_bus_operations dw_i2c_bus_ops = {
853 };
const char * acpi_device_path(const struct device *dev)
Definition: device.c:144
void acpigen_write_dword(unsigned int data)
Definition: acpigen.c:108
void acpigen_pop_len(void)
Definition: acpigen.c:37
void acpigen_write_scope(const char *name)
Definition: acpigen.c:326
char * acpigen_write_package(int nr_el)
Definition: acpigen.c:86
void acpigen_write_word(unsigned int data)
Definition: acpigen.c:102
void acpigen_write_name(const char *name)
Definition: acpigen.c:320
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define KHz
Definition: helpers.h:79
#define DIV_ROUND_UP(x, y)
Definition: helpers.h:60
cb_err
coreboot error codes
Definition: cb_err.h:15
@ CB_ERR
Generic error code.
Definition: cb_err.h:17
@ CB_SUCCESS
Call completed successfully.
Definition: cb_err.h:16
static u32 addr
Definition: cirrus.c:14
#define printk(level,...)
Definition: stdlib.h:16
@ CONFIG
Definition: dsi_common.h:201
static enum cb_err dw_i2c_set_speed_config(unsigned int bus, const struct dw_i2c_speed_config *config)
Definition: dw_i2c.c:489
void dw_i2c_dev_init(struct device *dev)
Definition: dw_i2c.c:782
static enum cb_err dw_i2c_gen_config_rise_fall_time(struct dw_i2c_regs *regs, enum i2c_speed speed, const struct dw_i2c_bus_config *bcfg, int ic_clk, struct dw_i2c_speed_config *config)
Definition: dw_i2c.c:530
@ MIN_HS_SCL_HIGHTIME
Definition: dw_i2c.c:33
@ MIN_FP_SCL_HIGHTIME
Definition: dw_i2c.c:30
@ MIN_SS_SCL_LOWTIME
Definition: dw_i2c.c:25
@ MIN_FP_SCL_LOWTIME
Definition: dw_i2c.c:31
@ MIN_HS_SCL_LOWTIME
Definition: dw_i2c.c:34
@ MIN_FS_SCL_HIGHTIME
Definition: dw_i2c.c:27
@ MIN_SS_SCL_HIGHTIME
Definition: dw_i2c.c:24
@ DEFAULT_SDA_HOLD_TIME
Definition: dw_i2c.c:22
@ MIN_FS_SCL_LOWTIME
Definition: dw_i2c.c:28
static enum cb_err dw_i2c_transfer_byte(struct dw_i2c_regs *regs, const struct i2c_msg *segment, size_t byte, int send_stop)
Definition: dw_i2c.c:314
int platform_i2c_transfer(unsigned int bus, struct i2c_msg *msg, int count)
Definition: dw_i2c.c:484
static enum cb_err dw_i2c_transfer(unsigned int bus, const struct i2c_msg *msg, size_t count)
Definition: dw_i2c.c:458
@ CONTROL_SPEED_FS
Definition: dw_i2c.c:48
@ CONTROL_SPEED_MASK
Definition: dw_i2c.c:50
@ CONTROL_10BIT_MASTER
Definition: dw_i2c.c:52
@ CONTROL_MASTER_MODE
Definition: dw_i2c.c:46
@ CONTROL_SPEED_HS
Definition: dw_i2c.c:49
@ CONTROL_10BIT_SLAVE
Definition: dw_i2c.c:51
@ CONTROL_RESTART_ENABLE
Definition: dw_i2c.c:53
@ CONTROL_SLAVE_DISABLE
Definition: dw_i2c.c:54
@ CONTROL_SPEED_SS
Definition: dw_i2c.c:47
static int counts_from_time(const struct freq *f, int ns)
Definition: dw_i2c.c:250
static void dw_i2c_enable(struct dw_i2c_regs *regs)
Definition: dw_i2c.c:261
struct dw_i2c_regs __packed
static enum cb_err _dw_i2c_transfer(unsigned int bus, const struct i2c_msg *segments, size_t count)
Definition: dw_i2c.c:354
static const struct soc_clock * get_soc_descriptor(int ic_clk)
Definition: dw_i2c.c:239
void dw_i2c_acpi_fill_ssdt(const struct device *dev)
Definition: dw_i2c.c:802
enum cb_err dw_i2c_gen_speed_config(uintptr_t dw_i2c_addr, enum i2c_speed speed, const struct dw_i2c_bus_config *bcfg, struct dw_i2c_speed_config *config)
Definition: dw_i2c.c:620
static int dw_i2c_dev_transfer(struct device *dev, const struct i2c_msg *msg, size_t count)
Definition: dw_i2c.c:845
#define DW_I2C_TIMEOUT_US
Definition: dw_i2c.c:15
static const struct i2c_descriptor * get_bus_descriptor(enum i2c_speed speed)
Definition: dw_i2c.c:228
static void dw_i2c_acpi_write_speed_config(const struct dw_i2c_speed_config *config)
Definition: dw_i2c.c:753
@ CMD_DATA_CMD
Definition: dw_i2c.c:59
@ CMD_DATA_STOP
Definition: dw_i2c.c:60
enum cb_err dw_i2c_init(unsigned int bus, const struct dw_i2c_bus_config *bcfg)
Definition: dw_i2c.c:691
static int counts_from_freq(const struct freq *fast, const struct freq *slow)
Definition: dw_i2c.c:255
static const struct soc_clock soc_clocks[]
static enum cb_err dw_i2c_set_speed(unsigned int bus, enum i2c_speed speed, const struct dw_i2c_bus_config *bcfg)
Definition: dw_i2c.c:646
#define DW_I2C_FLUSH_TIMEOUT_US
Definition: dw_i2c.c:17
static enum cb_err dw_i2c_wait_for_bus_idle(struct dw_i2c_regs *regs)
Definition: dw_i2c.c:290
static enum cb_err dw_i2c_disable(struct dw_i2c_regs *regs)
Definition: dw_i2c.c:270
#define DW_I2C_COMP_TYPE
Definition: dw_i2c.c:147
@ ENABLE_CONTROLLER
Definition: dw_i2c.c:76
const struct i2c_bus_operations dw_i2c_bus_ops
Definition: dw_i2c.c:851
static const struct i2c_descriptor speed_descriptors[]
@ STATUS_TX_FIFO_EMPTY
Definition: dw_i2c.c:67
@ STATUS_RX_FIFO_NOT_EMPTY
Definition: dw_i2c.c:68
@ STATUS_ACTIVITY
Definition: dw_i2c.c:65
@ STATUS_SLAVE_ACTIVITY
Definition: dw_i2c.c:71
@ STATUS_TX_FIFO_NOT_FULL
Definition: dw_i2c.c:66
@ STATUS_MASTER_ACTIVITY
Definition: dw_i2c.c:70
@ STATUS_RX_FIFO_FULL
Definition: dw_i2c.c:69
@ INTR_STAT_RX_DONE
Definition: dw_i2c.c:88
@ INTR_STAT_TX_ABORT
Definition: dw_i2c.c:87
@ INTR_STAT_TX_OVER
Definition: dw_i2c.c:84
@ INTR_STAT_RX_UNDER
Definition: dw_i2c.c:81
@ INTR_STAT_RD_REQ
Definition: dw_i2c.c:86
@ INTR_STAT_STOP_DET
Definition: dw_i2c.c:90
@ INTR_STAT_RX_FULL
Definition: dw_i2c.c:83
@ INTR_STAT_TX_EMPTY
Definition: dw_i2c.c:85
@ INTR_STAT_GEN_CALL
Definition: dw_i2c.c:92
@ INTR_STAT_START_DET
Definition: dw_i2c.c:91
@ INTR_STAT_RX_OVER
Definition: dw_i2c.c:82
@ INTR_STAT_ACTIVITY
Definition: dw_i2c.c:89
uintptr_t dw_i2c_base_address(unsigned int bus)
Definition: i2c.c:19
#define DW_I2C_DEBUG
Definition: dw_i2c.h:14
int dw_i2c_soc_dev_to_bus(const struct device *dev)
Definition: i2c.c:63
const struct dw_i2c_bus_config * dw_i2c_get_soc_cfg(unsigned int bus)
Definition: i2c.c:33
#define DW_I2C_SPEED_CONFIG_COUNT
Definition: dw_i2c.h:47
void acpigen_write_scope_end(void)
Definition: acpigen.h:343
i2c_speed
Definition: i2c.h:43
@ I2C_SPEED_FAST_PLUS
Definition: i2c.h:46
@ I2C_SPEED_STANDARD
Definition: i2c.h:44
@ I2C_SPEED_HIGH
Definition: i2c.h:47
@ I2C_SPEED_FAST
Definition: i2c.h:45
#define I2C_M_RD
Definition: i2c.h:34
static int stopwatch_expired(struct stopwatch *sw)
Definition: timer.h:152
static void stopwatch_init_usecs_expire(struct stopwatch *sw, long us)
Definition: timer.h:127
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
enum board_config config
Definition: memory.c:448
_Static_assert(sizeof(hls_t)==HLS_SIZE, "HLS_SIZE must equal to sizeof(hls_t)")
#define NULL
Definition: stddef.h:19
unsigned short uint16_t
Definition: stdint.h:11
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
Definition: device.h:76
Definition: device.h:107
int fall_time_ns
Definition: dw_i2c.h:59
int rise_time_ns
Definition: dw_i2c.h:58
int data_hold_time_ns
Definition: dw_i2c.h:60
enum i2c_speed speed
Definition: dw_i2c.h:53
struct dw_i2c_speed_config speed_config[DW_I2C_SPEED_CONFIG_COUNT]
Definition: dw_i2c.h:62
uint32_t intr_stat
Definition: dw_i2c.c:108
uint32_t ss_scl_hcnt
Definition: dw_i2c.c:102
uint32_t clear_tx_over_intr
Definition: dw_i2c.c:116
uint32_t sda_hold
Definition: dw_i2c.c:128
uint32_t rx_level
Definition: dw_i2c.c:127
uint32_t clear_rx_over_intr
Definition: dw_i2c.c:115
uint32_t clear_rx_under_intr
Definition: dw_i2c.c:114
uint32_t comp_type
Definition: dw_i2c.c:143
uint32_t ack_general_call
Definition: dw_i2c.c:135
uint32_t comp_version
Definition: dw_i2c.c:142
uint32_t tx_thresh
Definition: dw_i2c.c:112
uint32_t dma_tdlr
Definition: dw_i2c.c:132
uint32_t master_addr
Definition: dw_i2c.c:100
uint32_t hs_scl_hcnt
Definition: dw_i2c.c:106
uint32_t target_addr
Definition: dw_i2c.c:98
uint32_t intr_mask
Definition: dw_i2c.c:109
uint32_t slv_data_nak_only
Definition: dw_i2c.c:130
uint32_t clear_gen_call_intr
Definition: dw_i2c.c:123
uint32_t raw_intr_stat
Definition: dw_i2c.c:110
uint32_t tx_level
Definition: dw_i2c.c:126
uint32_t sda_setup
Definition: dw_i2c.c:134
uint32_t hs_scl_lcnt
Definition: dw_i2c.c:107
uint32_t clear_stop_det_intr
Definition: dw_i2c.c:121
uint32_t enable_status
Definition: dw_i2c.c:136
uint32_t clear_rx_done_intr
Definition: dw_i2c.c:119
uint32_t dma_rdlr
Definition: dw_i2c.c:133
uint32_t clr_restart_det
Definition: dw_i2c.c:139
uint32_t hs_spklen
Definition: dw_i2c.c:138
uint32_t fs_scl_lcnt
Definition: dw_i2c.c:105
uint32_t fs_spklen
Definition: dw_i2c.c:137
uint32_t clear_start_det_intr
Definition: dw_i2c.c:122
uint32_t status
Definition: dw_i2c.c:125
uint32_t enable
Definition: dw_i2c.c:124
uint32_t clear_rd_req_intr
Definition: dw_i2c.c:117
uint32_t clear_intr
Definition: dw_i2c.c:113
uint32_t clear_tx_abrt_intr
Definition: dw_i2c.c:118
uint32_t fs_scl_hcnt
Definition: dw_i2c.c:104
uint32_t clear_activity_intr
Definition: dw_i2c.c:120
uint32_t cmd_data
Definition: dw_i2c.c:101
uint32_t tx_abort_source
Definition: dw_i2c.c:129
uint32_t dma_cr
Definition: dw_i2c.c:131
uint32_t comp_param1
Definition: dw_i2c.c:141
uint32_t reserved[18]
Definition: dw_i2c.c:140
uint32_t rx_thresh
Definition: dw_i2c.c:111
uint32_t slave_addr
Definition: dw_i2c.c:99
uint32_t control
Definition: dw_i2c.c:97
uint32_t ss_scl_lcnt
Definition: dw_i2c.c:103
enum i2c_speed speed
Definition: dw_i2c.h:26
Definition: dw_i2c.c:39
uint32_t ns
Definition: dw_i2c.c:41
uint32_t ticks
Definition: dw_i2c.c:40
int(* transfer)(struct device *, const struct i2c_msg *, size_t count)
Definition: i2c_bus.h:13
int min_tlow_ns
Definition: dw_i2c.c:153
enum i2c_speed speed
Definition: dw_i2c.c:150
int min_thigh_ns
Definition: dw_i2c.c:152
struct i2c_msg - an I2C transaction segment beginning with START @addr: Slave address,...
Definition: i2c.h:32
uint16_t len
Definition: i2c.h:39
uint16_t slave
Definition: i2c.h:38
uint16_t flags
Definition: i2c.h:33
uint8_t * buf
Definition: i2c.h:40
struct freq freq
Definition: dw_i2c.c:196
int clk_speed_mhz
Definition: dw_i2c.c:195
static struct am335x_pinmux_regs * regs
Definition: pinmux.c:7
#define count