Skip to contentSkip to conversation

clk: at91: Add clock driver

PATCH Superseded Wenyou Yang 2 participants 2 messages Jun 20, 2016 – Jun 26, 2016

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 clk: at91: Add clock driver

The patch is referred to at91 clock driver of Linux, to make the clock node descriptions in DT aligned with the Linux's.

---

Changes in v3:
- Update based on [PATCH] clk: convert API to match reset/mailbox style (http://patchwork.ozlabs.org/patch/625342/).
- Remove [PATCH] clk: clk-uclass: Add post binding for CLK uclass, add bind() method to bind the clk node without compatible.
- Add help for Kconfig HAVE_AT91_XX option.
- Add ofdata_to_platdata() method for generated clock driver to handle the device tree.
- Use setbits_le32() to replace readl()/writel().
- Fixed the return value, -ENODEV->-EINVAL.
- Use dev_get_addr_ptr() to replace dev_get_addr().
- Remove check on dev_get_parent() return.

Changes in v2:
- Remove the redundant log print.

 arch/arm/mach-at91/include/mach/at91_pmc.h |  11 +-
 drivers/clk/Kconfig                        |   1 +
 drivers/clk/Makefile                       |   1 +
 drivers/clk/at91/Kconfig                   |  23 ++++
 drivers/clk/at91/Makefile                  |  11 ++
 drivers/clk/at91/clk-generated.c           | 162 +++++++++++++++++++++++++++++
 drivers/clk/at91/clk-h32mx.c               |  56 ++++++++++
 drivers/clk/at91/clk-main.c                |  55 ++++++++++
 drivers/clk/at91/clk-master.c              |  33 ++++++
 drivers/clk/at91/clk-peripheral.c          |  60 +++++++++++
 drivers/clk/at91/clk-plla.c                |  55 ++++++++++
 drivers/clk/at91/clk-slow.c                |  37 +++++++
 drivers/clk/at91/clk-system.c              |  76 ++++++++++++++
 drivers/clk/at91/clk-utmi.c                |  67 ++++++++++++
 drivers/clk/at91/pmc.c                     |  71 +++++++++++++
 drivers/clk/at91/pmc.h                     |  18 ++++
 drivers/clk/at91/sckc.c                    |  30 ++++++
 17 files changed, 764 insertions(+), 3 deletions(-)
 create mode 100644 drivers/clk/at91/Kconfig
 create mode 100644 drivers/clk/at91/Makefile
 create mode 100644 drivers/clk/at91/clk-generated.c
 create mode 100644 drivers/clk/at91/clk-h32mx.c
 create mode 100644 drivers/clk/at91/clk-main.c
 create mode 100644 drivers/clk/at91/clk-master.c
 create mode 100644 drivers/clk/at91/clk-peripheral.c
 create mode 100644 drivers/clk/at91/clk-plla.c
 create mode 100644 drivers/clk/at91/clk-slow.c
 create mode 100644 drivers/clk/at91/clk-system.c
 create mode 100644 drivers/clk/at91/clk-utmi.c
 create mode 100644 drivers/clk/at91/pmc.c
 create mode 100644 drivers/clk/at91/pmc.h
 create mode 100644 drivers/clk/at91/sckc.c
arch/arm/mach-at91/include/mach/at91_pmc.h +8 -3
index 680ceb0..2875ff2 100644
@@ -149,6 +149,9 @@ typedef struct at91_pmc {
 
 #define AT91_PMC_PCR_PID_MASK		(0x3f)
 #define AT91_PMC_PCR_GCKCSS		(0x7 << 8)
+#define AT91_PMC_PCR_GCKCSS_MASK	0x07
+#define AT91_PMC_PCR_GCKCSS_OFFSET	8
+#define AT91_PMC_PCR_GCKCSS_(x)		((x & 0x07) << 8)
 #define		AT91_PMC_PCR_GCKCSS_SLOW_CLK	(0x0 << 8)
 #define		AT91_PMC_PCR_GCKCSS_MAIN_CLK	(0x1 << 8)
 #define		AT91_PMC_PCR_GCKCSS_PLLA_CLK	(0x2 << 8)
@@ -158,8 +161,9 @@ typedef struct at91_pmc {
 #define AT91_PMC_PCR_CMD_WRITE		(0x1 << 12)
 #define AT91_PMC_PCR_DIV		(0x3 << 16)
 #define AT91_PMC_PCR_GCKDIV		(0xff << 20)
-#define		AT91_PMC_PCR_GCKDIV_(x)		(((x) & 0xff) << 20)
-#define		AT91_PMC_PCR_GCKDIV_OFFSET	20
+#define AT91_PMC_PCR_GCKDIV_MASK	0xff
+#define AT91_PMC_PCR_GCKDIV_OFFSET	20
+#define AT91_PMC_PCR_GCKDIV_(x)		((x & 0xff) << 20)
 #define AT91_PMC_PCR_EN			(0x1 << 28)
 #define AT91_PMC_PCR_GCKEN		(0x1 << 29)
 
@@ -243,8 +247,9 @@ typedef struct at91_pmc {
 #define		AT91_PMC_PCK1RDY	(1 <<  9)		/* Programmable Clock 1 */
 #define		AT91_PMC_PCK2RDY	(1 << 10)		/* Programmable Clock 2 */
 #define		AT91_PMC_PCK3RDY	(1 << 11)		/* Programmable Clock 3 */
+#define		AT91_PMC_MOSCSELS	BIT(16)			/* Main Oscillator Selection Status */
+#define		AT91_PMC_MOSCRCS	BIT(17)			/* 12 MHz RC Oscillator Status */
 #define		AT91_PMC_GCKRDY		(1 << 24)
-
 #define		AT91_PMC_PROTKEY	0x504d4301	/* Activation Code */
 
 /* PLL Charge Pump Current Register (PMC_PLLICPR) */
drivers/clk/Kconfig +1 -0
index 6eee8eb..3da63c0 100644
@@ -22,5 +22,6 @@ config SPL_CLK
 
 source "drivers/clk/uniphier/Kconfig"
 source "drivers/clk/exynos/Kconfig"
+source "drivers/clk/at91/Kconfig"
 
 endmenu
drivers/clk/Makefile +1 -0
index f7a8891..863a8d7 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_SANDBOX) += clk_sandbox_test.o
 obj-$(CONFIG_MACH_PIC32) += clk_pic32.o
 obj-$(CONFIG_CLK_UNIPHIER) += uniphier/
 obj-$(CONFIG_CLK_EXYNOS) += exynos/
+obj-$(CONFIG_CLK_AT91) += at91/
drivers/clk/at91/Kconfig new file +23 -0
new file mode 100644
index 0000000..42c5210
@@ -0,0 +1,23 @@
+config CLK_AT91
+	bool "AT91 clock drivers"
+	depends on CLK
+	help
+	  This option is used to enable the AT91 clock driver.
+
+config HAVE_AT91_UTMI
+	bool
+	help
+	  This option is used to enable the AT91 UTMI PLL clock
+	  driver.
+
+config HAVE_AT91_H32MX
+	bool
+	help
+	  This option is used to enable the AT91 H32MX matrixes
+	  clock driver.
+
+config HAVE_AT91_GENERATED_CLK
+	bool
+	help
+	  This option is used to enable the AT91 generated clock
+	  driver as a second clock source for peripherals.
drivers/clk/at91/Makefile new file +11 -0
new file mode 100644
index 0000000..eb1326b
@@ -0,0 +1,11 @@
+#
+# Makefile for at91 specific clk
+#
+
+obj-y += pmc.o sckc.o
+obj-y += clk-slow.o clk-main.o clk-plla.o clk-master.o
+obj-y += clk-system.o clk-peripheral.o
+
+obj-$(CONFIG_HAVE_AT91_UTMI)		+= clk-utmi.o
+obj-$(CONFIG_HAVE_AT91_H32MX)		+= clk-h32mx.o
+obj-$(CONFIG_HAVE_AT91_GENERATED_CLK)	+= clk-generated.o
drivers/clk/at91/clk-generated.c new file +162 -0

162 lines not on this page · raw message

drivers/clk/at91/clk-h32mx.c new file +56 -0

56 lines not on this page · raw message

drivers/clk/at91/clk-main.c new file +55 -0

55 lines not on this page · raw message

drivers/clk/at91/clk-master.c new file +33 -0
new file mode 100644
index 0000000..6f671fa
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static ulong at91_master_clk_get_rate(struct clk *clk)
+{
+	return gd->arch.mck_rate_hz;
+}
+
+static struct clk_ops at91_master_clk_ops = {
+	.get_rate = at91_master_clk_get_rate,
+};
+
+static const struct udevice_id at91_master_clk_match[] = {
+	{ .compatible = "atmel,at91sam9x5-clk-master" },
+	{}
+};
+
+U_BOOT_DRIVER(at91_master_clk) = {
+	.name = "at91-master-clk",
+	.id = UCLASS_CLK,
+	.of_match = at91_master_clk_match,
+	.ops = &at91_master_clk_ops,
+};
drivers/clk/at91/clk-peripheral.c new file +60 -0

60 lines not on this page · raw message

drivers/clk/at91/clk-plla.c new file +55 -0

55 lines not on this page · raw message

drivers/clk/at91/clk-slow.c new file +37 -0
new file mode 100644
index 0000000..ebd3806
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+
+static int at91_slow_clk_enable(struct clk *clk)
+{
+	return 0;
+}
+
+static ulong at91_slow_clk_get_rate(struct clk *clk)
+{
+	return CONFIG_SYS_AT91_SLOW_CLOCK;
+}
+
+static struct clk_ops at91_slow_clk_ops = {
+	.enable = at91_slow_clk_enable,
+	.get_rate = at91_slow_clk_get_rate,
+};
+
+static const struct udevice_id at91_slow_clk_match[] = {
+	{ .compatible = "atmel,at91sam9x5-clk-slow" },
+	{}
+};
+
+U_BOOT_DRIVER(at91_slow_clk) = {
+	.name = "at91-slow-clk",
+	.id = UCLASS_CLK,
+	.of_match = at91_slow_clk_match,
+	.ops = &at91_slow_clk_ops,
+};
drivers/clk/at91/clk-system.c new file +76 -0

76 lines not on this page · raw message

drivers/clk/at91/clk-utmi.c new file +67 -0

67 lines not on this page · raw message

drivers/clk/at91/pmc.c new file +71 -0

71 lines not on this page · raw message

drivers/clk/at91/pmc.h new file +18 -0
new file mode 100644
index 0000000..5444c84
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#ifndef __AT91_PMC_H__
+#define __AT91_PMC_H__
+
+struct pmc_platdata {
+	struct at91_pmc *reg_base;
+};
+
+int at91_pmc_core_probe(struct udevice *dev);
+int at91_pmc_clk_node_bind(struct udevice *dev);
+
+#endif
drivers/clk/at91/sckc.c new file +30 -0
new file mode 100644
index 0000000..7f7d17d
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <dm/root.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int at91_sckc_clk_bind(struct udevice *dev)
+{
+	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
+}
+
+static const struct udevice_id at91_sckc_clk_match[] = {
+	{ .compatible = "atmel,at91sam9x5-sckc" },
+	{}
+};
+
+U_BOOT_DRIVER(at91_sckc_clk) = {
+	.name = "at91_sckc_clk",
+	.id = UCLASS_CLK,
+	.of_match = at91_sckc_clk_match,
+	.bind = at91_sckc_clk_bind,
+};
Signature · 1 line
2.7.4
#1 lore raw

PATCH v3 1/1 clk: at91: Add clock driver

Hi Wenyou,

Quoted 46 lines · Wenyou Yang

The patch is referred to at91 clock driver of Linux, to make the clock node descriptions in DT aligned with the Linux's.

---

Changes in v3:
- Update based on [PATCH] clk: convert API to match reset/mailbox style (http://patchwork.ozlabs.org/patch/625342/).
- Remove [PATCH] clk: clk-uclass: Add post binding for CLK uclass, add bind() method to bind the clk node without compatible.
- Add help for Kconfig HAVE_AT91_XX option.
- Add ofdata_to_platdata() method for generated clock driver to handle the device tree.
- Use setbits_le32() to replace readl()/writel().
- Fixed the return value, -ENODEV->-EINVAL.
- Use dev_get_addr_ptr() to replace dev_get_addr().
- Remove check on dev_get_parent() return.

Changes in v2:
- Remove the redundant log print.

 arch/arm/mach-at91/include/mach/at91_pmc.h |  11 +-
 drivers/clk/Kconfig                        |   1 +
 drivers/clk/Makefile                       |   1 +
 drivers/clk/at91/Kconfig                   |  23 ++++
 drivers/clk/at91/Makefile                  |  11 ++
 drivers/clk/at91/clk-generated.c           | 162 +++++++++++++++++++++++++++++
 drivers/clk/at91/clk-h32mx.c               |  56 ++++++++++
 drivers/clk/at91/clk-main.c                |  55 ++++++++++
 drivers/clk/at91/clk-master.c              |  33 ++++++
 drivers/clk/at91/clk-peripheral.c          |  60 +++++++++++
 drivers/clk/at91/clk-plla.c                |  55 ++++++++++
 drivers/clk/at91/clk-slow.c                |  37 +++++++
 drivers/clk/at91/clk-system.c              |  76 ++++++++++++++
 drivers/clk/at91/clk-utmi.c                |  67 ++++++++++++
 drivers/clk/at91/pmc.c                     |  71 +++++++++++++
 drivers/clk/at91/pmc.h                     |  18 ++++
 drivers/clk/at91/sckc.c                    |  30 ++++++
 17 files changed, 764 insertions(+), 3 deletions(-)
 create mode 100644 drivers/clk/at91/Kconfig
 create mode 100644 drivers/clk/at91/Makefile
 create mode 100644 drivers/clk/at91/clk-generated.c
 create mode 100644 drivers/clk/at91/clk-h32mx.c
 create mode 100644 drivers/clk/at91/clk-main.c
 create mode 100644 drivers/clk/at91/clk-master.c
Quoted 8 lines
 create mode 100644 drivers/clk/at91/clk-peripheral.c
 create mode 100644 drivers/clk/at91/clk-plla.c
 create mode 100644 drivers/clk/at91/clk-slow.c
 create mode 100644 drivers/clk/at91/clk-system.c
 create mode 100644 drivers/clk/at91/clk-utmi.c
 create mode 100644 drivers/clk/at91/pmc.c
 create mode 100644 drivers/clk/at91/pmc.h
 create mode 100644 drivers/clk/at91/sckc.c

Some comments below.

Quoted 64 lines
arch/arm/mach-at91/include/mach/at91_pmc.h +8 -3
index 680ceb0..2875ff2 100644
@@ -149,6 +149,9 @@ typedef struct at91_pmc {
 
 #define AT91_PMC_PCR_PID_MASK          (0x3f)
 #define AT91_PMC_PCR_GCKCSS            (0x7 << 8)
+#define AT91_PMC_PCR_GCKCSS_MASK       0x07
+#define AT91_PMC_PCR_GCKCSS_OFFSET     8
+#define AT91_PMC_PCR_GCKCSS_(x)                ((x & 0x07) << 8)
 #define                AT91_PMC_PCR_GCKCSS_SLOW_CLK    (0x0 << 8)
 #define                AT91_PMC_PCR_GCKCSS_MAIN_CLK    (0x1 << 8)
 #define                AT91_PMC_PCR_GCKCSS_PLLA_CLK    (0x2 << 8)
@@ -158,8 +161,9 @@ typedef struct at91_pmc {
 #define AT91_PMC_PCR_CMD_WRITE         (0x1 << 12)
 #define AT91_PMC_PCR_DIV               (0x3 << 16)
 #define AT91_PMC_PCR_GCKDIV            (0xff << 20)
-#define                AT91_PMC_PCR_GCKDIV_(x)         (((x) & 0xff) << 20)
-#define                AT91_PMC_PCR_GCKDIV_OFFSET      20
+#define AT91_PMC_PCR_GCKDIV_MASK       0xff
+#define AT91_PMC_PCR_GCKDIV_OFFSET     20
+#define AT91_PMC_PCR_GCKDIV_(x)                ((x & 0xff) << 20)
 #define AT91_PMC_PCR_EN                        (0x1 << 28)
 #define AT91_PMC_PCR_GCKEN             (0x1 << 29)
 
@@ -243,8 +247,9 @@ typedef struct at91_pmc {
 #define                AT91_PMC_PCK1RDY        (1 <<  9)               /* Programmable Clock 1 */
 #define                AT91_PMC_PCK2RDY        (1 << 10)               /* Programmable Clock 2 */
 #define                AT91_PMC_PCK3RDY        (1 << 11)               /* Programmable Clock 3 */
+#define                AT91_PMC_MOSCSELS       BIT(16)                 /* Main Oscillator Selection Status */
+#define                AT91_PMC_MOSCRCS        BIT(17)                 /* 12 MHz RC Oscillator Status */
 #define                AT91_PMC_GCKRDY         (1 << 24)
-
 #define                AT91_PMC_PROTKEY        0x504d4301      /* Activation Code */
 
 /* PLL Charge Pump Current Register (PMC_PLLICPR) */
drivers/clk/Kconfig +1 -0
index 6eee8eb..3da63c0 100644
@@ -22,5 +22,6 @@ config SPL_CLK
 
 source "drivers/clk/uniphier/Kconfig"
 source "drivers/clk/exynos/Kconfig"
+source "drivers/clk/at91/Kconfig"
 
 endmenu
drivers/clk/Makefile +1 -0
index f7a8891..863a8d7 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_SANDBOX) += clk_sandbox_test.o
 obj-$(CONFIG_MACH_PIC32) += clk_pic32.o
 obj-$(CONFIG_CLK_UNIPHIER) += uniphier/
 obj-$(CONFIG_CLK_EXYNOS) += exynos/
+obj-$(CONFIG_CLK_AT91) += at91/
drivers/clk/at91/Kconfig new file +0 -0
new file mode 100644
index 0000000..42c5210

drivers/clk/at91/Kconfig line 5

Quoted 5 lines
drivers/clk/at91/Kconfig new file +5 -0
@@ -0,0 +1,23 @@
+config CLK_AT91
+       bool "AT91 clock drivers"
+       depends on CLK
+       help
+         This option is used to enable the AT91 clock driver.

Can you try to give a bit more information in your Kconfig help? What features does this driver support? Does it support all clocks? You could mention that it is controlled by the device tree. Try to give people an idea of what they get with the driver and why it is needed.

drivers/clk/at91/Kconfig line 11

Quoted 6 lines
drivers/clk/at91/Kconfig new file +6 -0
+
+config HAVE_AT91_UTMI
+       bool
+       help
+         This option is used to enable the AT91 UTMI PLL clock
+         driver.

What is UTMI? Why would you want this?

drivers/clk/at91/Kconfig line 17

Quoted 6 lines
drivers/clk/at91/Kconfig new file +6 -0
+
+config HAVE_AT91_H32MX
+       bool
+       help
+         This option is used to enable the AT91 H32MX matrixes
+         clock driver.

What is H32MX?

drivers/clk/at91/Kconfig line 23

Quoted 6 lines
drivers/clk/at91/Kconfig new file +6 -0
+
+config HAVE_AT91_GENERATED_CLK
+       bool
+       help
+         This option is used to enable the AT91 generated clock
+         driver as a second clock source for peripherals.

Why? What does this mean?

Quoted 34 lines
drivers/clk/at91/Makefile new file +11 -0
new file mode 100644
index 0000000..eb1326b
@@ -0,0 +1,11 @@
+#
+# Makefile for at91 specific clk
+#
+
+obj-y += pmc.o sckc.o
+obj-y += clk-slow.o clk-main.o clk-plla.o clk-master.o
+obj-y += clk-system.o clk-peripheral.o
+
+obj-$(CONFIG_HAVE_AT91_UTMI)           += clk-utmi.o
+obj-$(CONFIG_HAVE_AT91_H32MX)          += clk-h32mx.o
+obj-$(CONFIG_HAVE_AT91_GENERATED_CLK)  += clk-generated.o
drivers/clk/at91/clk-generated.c new file +11 -0
new file mode 100644
index 0000000..be0333f
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>

drivers/clk/at91/clk-generated.c line 21

Quoted 10 lines
drivers/clk/at91/clk-generated.c new file +10 -0
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define GENERATED_SOURCE_MAX   6
+#define GENERATED_MAX_DIV      255
+
+struct generated_clk_priv {
+       u8 num_parents;

I don't think u8 is worthwhile here. Just uint will do. It doesn't save any data space since malloc() will use at least 4 bytes. Also it may cause the compiler to add masking code thus bloating the code.

Quoted 36 lines
drivers/clk/at91/clk-generated.c new file +36 -0
+}
+
+static ulong generated_clk_get_rate(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+       struct clk parent;
+       u32 tmp, gckdiv;
+       u8 parent_id;
+       int ret;
+
+       writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
+       tmp = readl(&pmc->pcr);
+       parent_id = (tmp >> AT91_PMC_PCR_GCKCSS_OFFSET) &
+                   AT91_PMC_PCR_GCKCSS_MASK;
+       gckdiv = (tmp >> AT91_PMC_PCR_GCKDIV_OFFSET) & AT91_PMC_PCR_GCKDIV_MASK;
+
+       ret = clk_get_by_index(clk->dev, parent_id, &parent);
+       if (ret)
+               return 0;
+
+       return clk_get_rate(&parent) / (gckdiv + 1);
+}
+
+static ulong generated_clk_set_rate(struct clk *clk, ulong rate)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+       struct generated_clk_priv *priv = dev_get_priv(dev);
+       struct clk parent, best_parent;
+       ulong tmp_rate, best_rate = rate, parent_rate;
+       int tmp_diff, best_diff = -1;
+       u32 div, best_div = 0;
+       u8 best_parent_id = 0;
+       u8 i;
+       u32 tmp;

drivers/clk/at91/clk-generated.c line 67

Quoted 10 lines
drivers/clk/at91/clk-generated.c new file +10 -0
+       int ret;
+
+       for (i = 0; i < priv->num_parents; i++) {
+               ret = clk_get_by_index(clk->dev, i, &parent);
+               if (ret)
+                       return ret;
+
+               parent_rate = clk_get_rate(&parent);
+               if (!parent_rate)
+                       return -ENODEV;

Please don't return -ENODEV. This indicates that there is no device, but clearly there is! You could use -ENOENT to indicate that something is missing.

But in this case, if you look at clk_get_rate() it returns an error code, not 0.

So you should do something like:

if (IS_ERR_VALUE(parent_rate))
return parent_rate;

Quoted 20 lines
drivers/clk/at91/clk-generated.c new file +20 -0
+
+               for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
+                       tmp_rate = DIV_ROUND_CLOSEST(parent_rate, div);
+                       if (rate < tmp_rate)
+                               continue;
+                       tmp_diff = rate - tmp_rate;
+
+                       if (best_diff < 0 || best_diff > tmp_diff) {
+                               best_rate = tmp_rate;
+                               best_diff = tmp_diff;
+
+                               best_div = div - 1;
+                               best_parent = parent;
+                               best_parent_id = i;
+                       }
+
+                       if (!best_diff || tmp_rate < rate)
+                               break;
+               }
+

drivers/clk/at91/clk-generated.c line 97

Quoted 10 lines
drivers/clk/at91/clk-generated.c new file +10 -0
+               if (!best_diff)
+                       break;
+       }
+
+       debug("GCK: best parent: %s, best_rate = %ld, best_div = %d\n",
+             best_parent.dev->name, best_rate, best_div);
+
+       ret = clk_enable(&best_parent);
+       if (ret)
+               return -ENODEV;

return ret

Quoted 431 lines
drivers/clk/at91/clk-generated.c new file +65 -0
+
+       writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
+       tmp = readl(&pmc->pcr);
+       tmp &= ~(AT91_PMC_PCR_GCKDIV | AT91_PMC_PCR_GCKCSS);
+       tmp |= AT91_PMC_PCR_GCKCSS_(best_parent_id) |
+              AT91_PMC_PCR_CMD_WRITE |
+              AT91_PMC_PCR_GCKDIV_(best_div) |
+              AT91_PMC_PCR_GCKEN;
+       writel(tmp, &pmc->pcr);
+
+       while (!(readl(&pmc->sr) & AT91_PMC_GCKRDY))
+               ;
+
+       return 0;
+}
+
+static struct clk_ops generated_clk_ops = {
+       .get_rate = generated_clk_get_rate,
+       .set_rate = generated_clk_set_rate,
+};
+
+static int generated_clk_ofdata_to_platdata(struct udevice *dev)
+{
+       struct generated_clk_priv *priv = dev_get_priv(dev);
+       u32 cells[GENERATED_SOURCE_MAX];
+       u8 num_parents;
+
+       num_parents = fdtdec_get_int_array_count(gd->fdt_blob, dev->of_offset,
+                                                "clocks", cells,
+                                                GENERATED_SOURCE_MAX);
+
+       if (!num_parents)
+               return -1;
+
+       priv->num_parents = num_parents;
+
+       return 0;
+}
+
+static int generated_clk_bind(struct udevice *dev)
+{
+       return at91_pmc_clk_node_bind(dev);
+}
+
+static int generated_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id generated_clk_match[] = {
+       { .compatible = "atmel,sama5d2-clk-generated" },
+       {}
+};
+
+U_BOOT_DRIVER(generated_clk) = {
+       .name = "generated-clk",
+       .id = UCLASS_CLK,
+       .of_match = generated_clk_match,
+       .bind = generated_clk_bind,
+       .probe = generated_clk_probe,
+       .ofdata_to_platdata = generated_clk_ofdata_to_platdata,
+       .priv_auto_alloc_size = sizeof(struct generated_clk_priv),
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &generated_clk_ops,
+};
drivers/clk/at91/clk-h32mx.c new file +56 -0
new file mode 100644
index 0000000..747eeec
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <dm/util.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define H32MX_MAX_FREQ 90000000
+
+static ulong sama5d4_h32mx_clk_get_rate(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+       ulong rate = gd->arch.mck_rate_hz;
+
+       if (readl(&pmc->mckr) & AT91_PMC_MCKR_H32MXDIV)
+               rate /= 2;
+
+       if (rate > H32MX_MAX_FREQ)
+               dm_warn("H32MX clock is too fast\n");
+
+       return rate;
+}
+
+static struct clk_ops sama5d4_h32mx_clk_ops = {
+       .get_rate = sama5d4_h32mx_clk_get_rate,
+};
+
+static int sama5d4_h32mx_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id sama5d4_h32mx_clk_match[] = {
+       { .compatible = "atmel,sama5d4-clk-h32mx" },
+       {}
+};
+
+U_BOOT_DRIVER(sama5d4_h32mx_clk) = {
+       .name = "sama5d4-h32mx-clk",
+       .id = UCLASS_CLK,
+       .of_match = sama5d4_h32mx_clk_match,
+       .probe = sama5d4_h32mx_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &sama5d4_h32mx_clk_ops,
+};
drivers/clk/at91/clk-main.c new file +55 -0
new file mode 100644
index 0000000..bc080eb
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int main_osc_clk_enable(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+
+       if (readl(&pmc->sr) & AT91_PMC_MOSCSELS)
+               return 0;
+
+       return -EINVAL;
+}
+
+static ulong main_osc_clk_get_rate(struct clk *clk)
+{
+       return gd->arch.main_clk_rate_hz;
+}
+
+static struct clk_ops main_osc_clk_ops = {
+       .enable = main_osc_clk_enable,
+       .get_rate = main_osc_clk_get_rate,
+};
+
+static int main_osc_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id main_osc_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-clk-main" },
+       {}
+};
+
+U_BOOT_DRIVER(at91sam9x5_main_osc_clk) = {
+       .name = "at91sam9x5-main-osc-clk",
+       .id = UCLASS_CLK,
+       .of_match = main_osc_clk_match,
+       .probe = main_osc_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &main_osc_clk_ops,
+};
drivers/clk/at91/clk-master.c new file +33 -0
new file mode 100644
index 0000000..6f671fa
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static ulong at91_master_clk_get_rate(struct clk *clk)
+{
+       return gd->arch.mck_rate_hz;
+}
+
+static struct clk_ops at91_master_clk_ops = {
+       .get_rate = at91_master_clk_get_rate,
+};
+
+static const struct udevice_id at91_master_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-clk-master" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_master_clk) = {
+       .name = "at91-master-clk",
+       .id = UCLASS_CLK,
+       .of_match = at91_master_clk_match,
+       .ops = &at91_master_clk_ops,
+};
drivers/clk/at91/clk-peripheral.c new file +60 -0
new file mode 100644
index 0000000..0e572b1
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+#define PERIPHERAL_ID_MIN      2
+#define PERIPHERAL_ID_MAX      31
+#define PERIPHERAL_MASK(id)    (1 << ((id) & PERIPHERAL_ID_MAX))
+
+static int sam9x5_periph_clk_enable(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+
+       if (clk->id < PERIPHERAL_ID_MIN)
+               return -1;
+
+       writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
+       setbits_le32(&pmc->pcr, AT91_PMC_PCR_CMD_WRITE | AT91_PMC_PCR_EN);
+
+       return 0;
+}
+
+static struct clk_ops sam9x5_periph_clk_ops = {
+       .enable = sam9x5_periph_clk_enable,
+};
+
+static int sam9x5_periph_clk_bind(struct udevice *dev)
+{
+       return at91_pmc_clk_node_bind(dev);
+}
+
+static int sam9x5_periph_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id sam9x5_periph_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-clk-peripheral" },
+       {}
+};
+
+U_BOOT_DRIVER(sam9x5_periph_clk) = {
+       .name = "sam9x5-periph-clk",
+       .id = UCLASS_CLK,
+       .of_match = sam9x5_periph_clk_match,
+       .bind = sam9x5_periph_clk_bind,
+       .probe = sam9x5_periph_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &sam9x5_periph_clk_ops,
+};
drivers/clk/at91/clk-plla.c new file +55 -0
new file mode 100644
index 0000000..d15141d
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int plla_clk_enable(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+
+       if (readl(&pmc->sr) & AT91_PMC_LOCKA)
+               return 0;
+
+       return -EINVAL;
+}
+
+static ulong plla_clk_get_rate(struct clk *clk)
+{
+       return gd->arch.plla_rate_hz;
+}
+
+static struct clk_ops plla_clk_ops = {
+       .enable = plla_clk_enable,
+       .get_rate = plla_clk_get_rate,
+};
+
+static int plla_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id plla_clk_match[] = {
+       { .compatible = "atmel,sama5d3-clk-pll" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_plla_clk) = {
+       .name = "at91-plla-clk",
+       .id = UCLASS_CLK,
+       .of_match = plla_clk_match,
+       .probe = plla_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &plla_clk_ops,
+};
drivers/clk/at91/clk-slow.c new file +37 -0
new file mode 100644
index 0000000..ebd3806
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+
+static int at91_slow_clk_enable(struct clk *clk)
+{
+       return 0;
+}
+
+static ulong at91_slow_clk_get_rate(struct clk *clk)
+{
+       return CONFIG_SYS_AT91_SLOW_CLOCK;
+}
+
+static struct clk_ops at91_slow_clk_ops = {
+       .enable = at91_slow_clk_enable,
+       .get_rate = at91_slow_clk_get_rate,
+};
+
+static const struct udevice_id at91_slow_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-clk-slow" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_slow_clk) = {
+       .name = "at91-slow-clk",
+       .id = UCLASS_CLK,
+       .of_match = at91_slow_clk_match,
+       .ops = &at91_slow_clk_ops,
+};
drivers/clk/at91/clk-system.c new file +28 -0
new file mode 100644
index 0000000..d92c839
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+#define SYSTEM_MAX_ID          31
+
+static inline int is_pck(int id)
+{
+       return (id >= 8) && (id <= 15);
+}
+
+static int at91_system_clk_enable(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+       u32 mask;
+
+       if (clk->id > SYSTEM_MAX_ID)

drivers/clk/at91/clk-system.c line 38

Quoted 10 lines
drivers/clk/at91/clk-system.c new file +10 -0
+               return -EINVAL;
+
+       mask = BIT(clk->id);
+
+       writel(mask, &pmc->scer);
+
+       /**
+        * For the programmable clocks should be checked
+        * the Ready status in the PMC status register after
+        * enabled; the others unnecessary.

Do you mean:

For the programmable clocks the Ready status in the PMC status register should be checked after enabling. For other clocks this is unnecessary.

Quoted 167 lines
drivers/clk/at91/clk-system.c new file +38 -0
+        */
+       if (!is_pck(clk->id))
+               return 0;
+
+       while (!(readl(&pmc->sr) & mask))
+               ;
+
+       return 0;
+}
+
+static struct clk_ops at91_system_clk_ops = {
+       .enable = at91_system_clk_enable,
+};
+
+static int at91_system_clk_bind(struct udevice *dev)
+{
+       return at91_pmc_clk_node_bind(dev);
+}
+
+static int at91_system_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id at91_system_clk_match[] = {
+       { .compatible = "atmel,at91rm9200-clk-system" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_system_clk) = {
+       .name = "at91-system-clk",
+       .id = UCLASS_CLK,
+       .of_match = at91_system_clk_match,
+       .bind = at91_system_clk_bind,
+       .probe = at91_system_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &at91_system_clk_ops,
+};
drivers/clk/at91/clk-utmi.c new file +67 -0
new file mode 100644
index 0000000..3b758c5
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <linux/io.h>
+#include <mach/at91_pmc.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define UTMI_FIXED_MUL         40
+
+static int utmi_clk_enable(struct clk *clk)
+{
+       struct pmc_platdata *plat = dev_get_platdata(clk->dev);
+       struct at91_pmc *pmc = plat->reg_base;
+       u32 tmp;
+
+       if (readl(&pmc->sr) & AT91_PMC_LOCKU)
+               return 0;
+
+       tmp = readl(&pmc->uckr);
+       tmp |= AT91_PMC_UPLLEN |
+              AT91_PMC_UPLLCOUNT |
+              AT91_PMC_BIASEN;
+       writel(tmp, &pmc->uckr);
+
+       while (!(readl(&pmc->sr) & AT91_PMC_LOCKU))
+               ;
+
+       return 0;
+}
+
+static ulong utmi_clk_get_rate(struct clk *clk)
+{
+       return gd->arch.main_clk_rate_hz * UTMI_FIXED_MUL;
+}
+
+static struct clk_ops utmi_clk_ops = {
+       .enable = utmi_clk_enable,
+       .get_rate = utmi_clk_get_rate,
+};
+
+static int utmi_clk_probe(struct udevice *dev)
+{
+       return at91_pmc_core_probe(dev);
+}
+
+static const struct udevice_id utmi_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-clk-utmi" },
+       {}
+};
+
+U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
+       .name = "at91sam9x5-utmi-clk",
+       .id = UCLASS_CLK,
+       .of_match = utmi_clk_match,
+       .probe = utmi_clk_probe,
+       .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
+       .ops = &utmi_clk_ops,
+};
drivers/clk/at91/pmc.c new file +50 -0
new file mode 100644
index 0000000..704da72
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <dm/lists.h>
+#include <dm/root.h>
+#include "pmc.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int at91_pmc_bind(struct udevice *dev)
+{
+       return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
+}
+
+static const struct udevice_id at91_pmc_match[] = {
+       { .compatible = "atmel,sama5d2-pmc" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_pmc) = {
+       .name = "at91-pmc-core",
+       .id = UCLASS_CLK,
+       .of_match = at91_pmc_match,
+       .bind = at91_pmc_bind,
+};
+
+int at91_pmc_core_probe(struct udevice *dev)
+{
+       struct pmc_platdata *plat = dev_get_platdata(dev);
+
+       dev = dev_get_parent(dev);
+
+       plat->reg_base = (struct at91_pmc *)dev_get_addr_ptr(dev);
+
+       return 0;
+}
+
+int at91_pmc_clk_node_bind(struct udevice *dev)
+{
+       const void *fdt = gd->fdt_blob;
+       int offset = dev->of_offset;
+       const char *name;
+       int ret;

drivers/clk/at91/pmc.c line 60

Quoted 10 lines
drivers/clk/at91/pmc.c new file +10 -0
+
+       for (offset = fdt_first_subnode(fdt, offset);
+            offset > 0;
+            offset = fdt_next_subnode(fdt, offset)) {
+               name = fdt_get_name(fdt, offset, NULL);
+               if (!name)
+                       return -EINVAL;
+
+               ret = device_bind_driver_to_node(dev, "clk", name,
+                                                offset, NULL);

Consider using a clock ID value and fewer clock devices (i.e. each device supports multiple clocks). But it is up to you and depends on how you want to use the code, etc. What you have here is fine also.

Quoted 72 lines
drivers/clk/at91/pmc.c new file +11 -0
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
+U_BOOT_DRIVER(clk_generic) = {
+       .id     = UCLASS_CLK,
+       .name   = "clk",
+};
drivers/clk/at91/pmc.h new file +18 -0
new file mode 100644
index 0000000..5444c84
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#ifndef __AT91_PMC_H__
+#define __AT91_PMC_H__
+
+struct pmc_platdata {
+       struct at91_pmc *reg_base;
+};
+
+int at91_pmc_core_probe(struct udevice *dev);
+int at91_pmc_clk_node_bind(struct udevice *dev);
+
+#endif
drivers/clk/at91/sckc.c new file +30 -1
new file mode 100644
index 0000000..7f7d17d
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 Atmel Corporation
+ *               Wenyou.Yang <[email protected]>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <clk_uclass.h>
+#include <dm/device.h>
+#include <dm/root.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int at91_sckc_clk_bind(struct udevice *dev)
+{
+       return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
+}
+
+static const struct udevice_id at91_sckc_clk_match[] = {
+       { .compatible = "atmel,at91sam9x5-sckc" },
+       {}
+};
+
+U_BOOT_DRIVER(at91_sckc_clk) = {
+       .name = "at91_sckc_clk",
+       .id = UCLASS_CLK,
+       .of_match = at91_sckc_clk_match,
+       .bind = at91_sckc_clk_bind,
+};
--
Quoted 2 lines

2.7.4

Regards,
Simon