svn commit: r350628 - in stable/12/sys: arm/allwinner/clkng dev/iicbus/twsi

Emmanuel Vadot manu at FreeBSD.org
Tue Aug 6 12:12:31 UTC 2019


Author: manu
Date: Tue Aug  6 12:12:29 2019
New Revision: 350628
URL: https://svnweb.freebsd.org/changeset/base/350628

Log:
  MFC r347489-r347491, r347512
  
  r347489:
  allwinner: clk: prediv_mux: Init the current parent
  
  Do not init the first parent but read the clock register to find
  it's current parent and init this one.
  
  r347490:
  allwinner: clk: sun8i_r: Correct resets
  
  The i2c reset wasn't defined and some bits where wrong, correct them.
  
  r347491:
  twsi: Calculate the clock param based on the bus frequency
  
  Instead of precalculating the different speed, respect the bus frequency
  and calculate the clock register parameter based on it.
  If the platform didn't register the core clk, fallback on the precomputed
  values (This is likely do be the case on Marvell boards).
  
  r347512:
  arm: allwinner: aw_clk_nm: Don't reparent the clock if we didn't ask
  
  When looking for the best frequency don't change the clock parent if the
  clock wasn't configured to do that.

Modified:
  stable/12/sys/arm/allwinner/clkng/aw_clk_nm.c
  stable/12/sys/arm/allwinner/clkng/aw_clk_prediv_mux.c
  stable/12/sys/arm/allwinner/clkng/ccu_sun8i_r.c
  stable/12/sys/dev/iicbus/twsi/a10_twsi.c
  stable/12/sys/dev/iicbus/twsi/twsi.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/arm/allwinner/clkng/aw_clk_nm.c
==============================================================================
--- stable/12/sys/arm/allwinner/clkng/aw_clk_nm.c	Tue Aug  6 11:26:30 2019	(r350627)
+++ stable/12/sys/arm/allwinner/clkng/aw_clk_nm.c	Tue Aug  6 12:12:29 2019	(r350628)
@@ -236,7 +236,7 @@ aw_clk_nm_set_freq(struct clknode *clk, uint64_t fpare
 		return (ERANGE);
 	}
 
-	if (p_idx != best_parent)
+	if ((sc->flags & AW_CLK_REPARENT) != 0 && p_idx != best_parent)
 		clknode_set_parent_by_idx(clk, best_parent);
 
 	DEVICE_LOCK(clk);

Modified: stable/12/sys/arm/allwinner/clkng/aw_clk_prediv_mux.c
==============================================================================
--- stable/12/sys/arm/allwinner/clkng/aw_clk_prediv_mux.c	Tue Aug  6 11:26:30 2019	(r350627)
+++ stable/12/sys/arm/allwinner/clkng/aw_clk_prediv_mux.c	Tue Aug  6 12:12:29 2019	(r350628)
@@ -75,7 +75,19 @@ struct aw_clk_prediv_mux_sc {
 static int
 aw_clk_prediv_mux_init(struct clknode *clk, device_t dev)
 {
-	clknode_init_parent_idx(clk, 0);
+	struct aw_clk_prediv_mux_sc *sc;
+	uint32_t val;
+
+	sc = clknode_get_softc(clk);
+
+	DEVICE_LOCK(clk);
+	READ4(clk, sc->offset, &val);
+	DEVICE_UNLOCK(clk);
+
+	/* Init the current parent */
+	val = (val & sc->mux_mask) >> sc->mux_shift;
+	clknode_init_parent_idx(clk, val);
+
 	return (0);
 }
 

Modified: stable/12/sys/arm/allwinner/clkng/ccu_sun8i_r.c
==============================================================================
--- stable/12/sys/arm/allwinner/clkng/ccu_sun8i_r.c	Tue Aug  6 11:26:30 2019	(r350627)
+++ stable/12/sys/arm/allwinner/clkng/ccu_sun8i_r.c	Tue Aug  6 12:12:29 2019	(r350628)
@@ -63,8 +63,9 @@ __FBSDID("$FreeBSD$");
 static struct aw_ccung_reset ccu_sun8i_r_resets[] = {
 	CCU_RESET(RST_APB0_IR, 0xb0, 1)
 	CCU_RESET(RST_APB0_TIMER, 0xb0, 2)
-	CCU_RESET(RST_APB0_RSB, 0xb0, 4)
-	CCU_RESET(RST_APB0_UART, 0xb0, 6)
+	CCU_RESET(RST_APB0_RSB, 0xb0, 3)
+	CCU_RESET(RST_APB0_UART, 0xb0, 4)
+	CCU_RESET(RST_APB0_I2C, 0xb0, 6)
 };
 
 static struct aw_ccung_gate ccu_sun8i_r_gates[] = {

Modified: stable/12/sys/dev/iicbus/twsi/a10_twsi.c
==============================================================================
--- stable/12/sys/dev/iicbus/twsi/a10_twsi.c	Tue Aug  6 11:26:30 2019	(r350627)
+++ stable/12/sys/dev/iicbus/twsi/a10_twsi.c	Tue Aug  6 12:12:29 2019	(r350628)
@@ -87,9 +87,7 @@ static int
 a10_twsi_attach(device_t dev)
 {
 	struct twsi_softc *sc;
-	clk_t clk;
 	hwreset_t rst;
-	uint64_t freq;
 	int error;
 
 	sc = device_get_softc(dev);
@@ -104,12 +102,12 @@ a10_twsi_attach(device_t dev)
 	}
 
 	/* Activate clock */
-	error = clk_get_by_ofw_index(dev, 0, 0, &clk);
+	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core);
 	if (error != 0) {
 		device_printf(dev, "could not find clock\n");
 		return (error);
 	}
-	error = clk_enable(clk);
+	error = clk_enable(sc->clk_core);
 	if (error != 0) {
 		device_printf(dev, "could not enable clock\n");
 		return (error);
@@ -122,31 +120,6 @@ a10_twsi_attach(device_t dev)
 	sc->reg_status = TWI_STAT;
 	sc->reg_baud_rate = TWI_CCR;
 	sc->reg_soft_reset = TWI_SRST;
-
-	/* Setup baud rate params */
-	clk_get_freq(clk, &freq);
-	switch (freq) {
-		/* 
-		 * Formula is
-		 * F0 = FINT / 2 ^ CLK_N
-		 * F1 = F0 / (CLK_M + 1)
-		 * 
-		 * Doc says that the output freq is F1/10 but my logic analyzer says otherwise
-		 */
-	case 48000000:
-		sc->baud_rate[IIC_SLOW].param = TWSI_BAUD_RATE_PARAM(11, 1);
-		sc->baud_rate[IIC_FAST].param = TWSI_BAUD_RATE_PARAM(11, 1);
-		sc->baud_rate[IIC_FASTEST].param = TWSI_BAUD_RATE_PARAM(2, 1);
-		break;
-	case 24000000:
-		sc->baud_rate[IIC_SLOW].param = TWSI_BAUD_RATE_PARAM(5, 2);
-		sc->baud_rate[IIC_FAST].param = TWSI_BAUD_RATE_PARAM(5, 2);
-		sc->baud_rate[IIC_FASTEST].param = TWSI_BAUD_RATE_PARAM(2, 2);
-		break;
-	default:
-		device_printf(dev, "Non supported frequency\n");
-		return (ENXIO);
-	}
 
 	sc->need_ack = true;
 	return (twsi_attach(dev));

Modified: stable/12/sys/dev/iicbus/twsi/twsi.c
==============================================================================
--- stable/12/sys/dev/iicbus/twsi/twsi.c	Tue Aug  6 11:26:30 2019	(r350627)
+++ stable/12/sys/dev/iicbus/twsi/twsi.c	Tue Aug  6 12:12:29 2019	(r350628)
@@ -243,6 +243,43 @@ twsi_locked_start(device_t dev, struct twsi_softc *sc,
 	return (IIC_NOERR);
 }
 
+#ifdef EXT_RESOURCES
+#define	TWSI_BAUD_RATE_RAW(C,M,N)	((C)/((10*(M+1))<<(N)))
+#define	ABSSUB(a,b)	(((a) > (b)) ? (a) - (b) : (b) - (a))
+
+static int
+twsi_calc_baud_rate(struct twsi_softc *sc, const u_int target,
+  int *param)
+{
+	uint64_t clk;
+	uint32_t cur, diff, diff0;
+	int m, n, m0, n0;
+
+	/* Calculate baud rate. */
+	diff0 = 0xffffffff;
+
+	if (clk_get_freq(sc->clk_core, &clk) < 0)
+		return (-1);
+
+	debugf(sc->dev, "Bus clock is at %lu\n", clk);
+
+	for (n = 0; n < 8; n++) {
+		for (m = 0; m < 16; m++) {
+			cur = TWSI_BAUD_RATE_RAW(clk,m,n);
+			diff = ABSSUB(target, cur);
+			if (diff < diff0) {
+				m0 = m;
+				n0 = n;
+				diff0 = diff;
+			}
+		}
+	}
+	*param = TWSI_BAUD_RATE_PARAM(m0, n0);
+
+	return (0);
+}
+#endif /* EXT_RESOURCES */
+
 /*
  * Only slave mode supported, disregard [old]addr
  */
@@ -251,24 +288,36 @@ twsi_reset(device_t dev, u_char speed, u_char addr, u_
 {
 	struct twsi_softc *sc;
 	uint32_t param;
-	/* uint32_t val; */
+#ifdef EXT_RESOURCES
+	u_int busfreq;
+#endif
 
 	sc = device_get_softc(dev);
 
-	switch (speed) {
-	case IIC_SLOW:
-	case IIC_FAST:
-		param = sc->baud_rate[speed].param;
-		debugf(dev, "Using IIC_FAST mode with speed param=%x\n", param);
-		break;
-	case IIC_FASTEST:
-	case IIC_UNKNOWN:
-	default:
-		param = sc->baud_rate[IIC_FAST].param;
-		debugf(dev, "Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param);
-		break;
+#ifdef EXT_RESOURCES
+	busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
+
+	if (twsi_calc_baud_rate(sc, busfreq, &param) == -1) {
+#endif
+		switch (speed) {
+		case IIC_SLOW:
+		case IIC_FAST:
+			param = sc->baud_rate[speed].param;
+			debugf(dev, "Using IIC_FAST mode with speed param=%x\n", param);
+			break;
+		case IIC_FASTEST:
+		case IIC_UNKNOWN:
+		default:
+			param = sc->baud_rate[IIC_FAST].param;
+			debugf(dev, "Using IIC_FASTEST/UNKNOWN mode with speed param=%x\n", param);
+			break;
+		}
+#ifdef EXT_RESOURCES
 	}
+#endif
 
+	debugf(dev, "Using clock param=%x\n", param);
+
 	mtx_lock(&sc->mutex);
 	TWSI_WRITE(sc, sc->reg_soft_reset, 0x0);
 	TWSI_WRITE(sc, sc->reg_baud_rate, param);
@@ -521,6 +570,7 @@ twsi_intr(void *arg)
 			break;
 
 		case TWSI_STATUS_ADDR_R_ACK:
+			debugf(sc->dev, "Ack received after transmitting the address\n");
 			sc->recv_bytes = 0;
 
 			TWSI_WRITE(sc, sc->reg_control, sc->control_val);


More information about the svn-src-stable mailing list