Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 2/3] ARM: dts: sunxi: add support for Orange Pi Zero board
From: Maxime Ripard @ 2016-12-05  9:05 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <324c8820-aeea-3fad-0e02-1bdb8f675677@arm.com>

On Fri, Dec 02, 2016 at 04:10:46PM +0000, Andre Przywara wrote:
> Hi,
> 
> On 02/12/16 14:32, Icenowy Zheng wrote:
> > 
> > 
> > 02.12.2016, 22:30, "Hans de Goede" <hdegoede@redhat.com>:
> >> Hi,
> >>
> >> On 02-12-16 15:22, Icenowy Zheng wrote:
> >>>  01.12.2016, 17:36, "Maxime Ripard" <maxime.ripard@free-electrons.com>:
> >>>>  On Mon, Nov 28, 2016 at 12:29:07AM +0000, Andr? Przywara wrote:
> >>>>>   > Something more interesting happened.
> >>>>>   >
> >>>>>   > Xunlong made a add-on board for Orange Pi Zero, which exposes the
> >>>>>   > two USB Controllers exported at expansion bus as USB Type-A
> >>>>>   > connectors.
> >>>>>   >
> >>>>>   > Also it exposes a analog A/V jack and a microphone.
> >>>>>   >
> >>>>>   > Should I enable {e,o}hci{2.3} in the device tree?
> >>>>>
> >>>>>   Actually we should do this regardless of this extension board. The USB
> >>>>>   pins are not multiplexed and are exposed on user accessible pins (just
> >>>>>   not soldered, but that's a detail), so I think they qualify for DT
> >>>>>   enablement. And even if a user can't use them, it doesn't hurt to have
> >>>>>   them (since they are not multiplexed).
> >>>>
> >>>>  My main concern about this is that we'll leave regulators enabled by
> >>>>  default, for a minority of users. And that minority will prevent to do
> >>>>  a proper power management when the times come since we'll have to keep
> >>>>  that behaviour forever.
> >>>
> >>>  I think these users can add a 'fdt set /xxx/xxx status "disabled" ' .
> >>
> >> I don't think that will be necessary I'm pretty sure these extra usb
> >> ports do not have a regulator for the Vbus, they just hook directly
> >> to the 5V rail, can someone with a schematic check ?
> > 
> > We seems to have still no schematics for the add-on board.
> 
> From looking at the picture of that expansion board on the Aliexpress
> page and chasing the tracks, there is clearly no voltage regulator on
> there, it's just passive components. The 5V pin from the headers is
> routed forth and back between the two layers via some vias directly to
> the 5V pins of the USB sockets.
> 
> > But something is sure is that there's no any regulator-related pins
> > on the add-on pinout. There's only USB DM and DP pins.
> > 
> > So the Vbus must be directly connected to +5V.
> 
> So yes, it is.
> 
> But I think the question is moot anyways, since we don't provide DT
> support for that add-on board at that point anyways.
> One could imagine another board, though, which has regulators switched
> by GPIOs, but that would be their problem and they would have regulators
> specified in their specific DT snippet, then.
> 
> So to summarize:
> - For that specific Orange Pi Zero board which we discuss the DT for
> there is no regulator support for the additional USB ports. Thus nothing
> we could turn off to save power.
> - A user could just take these USB brackets with pin headers that are so
> common in PCs to connect additional USB ports to the back of the box.
> One just needs to re-sort the pins, which is a matter of a minute.
> - As long as we don't provide any easy way of handling DT changes, we
> should enable the USB ports for the sake of the users of either those
> brackets or the expansion board. Any more sophisticated USB expansion
> board with regulators would need to amend the DT anyway.

I disagree with this. We already have different ways of changing the
DT at runtime, and even if we didn't I'd still disagree. Once you add
that, there's simply no going back. Saying "let's enable it and we'll
figure it out later" doesn't work, and is essentially a "enable it".

So what you're suggesting is to have those regulators enabled forever,
which might be the case on that board anyway, but definitely shouldn't
be policy.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20161205/d728f143/attachment.sig>

^ permalink raw reply

* [PATCH v2 1/3] ARM: da850: fix infinite loop in clk_set_rate()
From: Bartosz Golaszewski @ 2016-12-05  9:06 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <34f9e4c4-917e-0472-72af-96fbc7e0c6db@ti.com>

2016-12-05 9:38 GMT+01:00 Sekhar Nori <nsekhar@ti.com>:
> On Friday 02 December 2016 09:08 PM, Bartosz Golaszewski wrote:
>> The aemif clock is added twice to the lookup table in da850.c. This
>> breaks the children list of pll0_sysclk3 as we're using the same list
>> links in struct clk. When calling clk_set_rate(), we get stuck in
>> propagate_rate().
>>
>> Create a separate clock for nand, inheriting the rate of the aemif
>> clock and retrieve it in the davinci_nand module.
>>
>> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>> ---
>>  arch/arm/mach-davinci/da850.c   | 7 ++++++-
>>  drivers/mtd/nand/davinci_nand.c | 2 +-
>>  2 files changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/arm/mach-davinci/da850.c b/arch/arm/mach-davinci/da850.c
>> index e770c97..1fcc986 100644
>> --- a/arch/arm/mach-davinci/da850.c
>> +++ b/arch/arm/mach-davinci/da850.c
>> @@ -367,6 +367,11 @@ static struct clk aemif_clk = {
>>       .flags          = ALWAYS_ENABLED,
>>  };
>>
>> +static struct clk nand_clk = {
>> +     .name           = "nand",
>> +     .parent         = &aemif_clk,
>> +};
>> +
>>  static struct clk usb11_clk = {
>>       .name           = "usb11",
>>       .parent         = &pll0_sysclk4,
>> @@ -537,7 +542,7 @@ static struct clk_lookup da850_clks[] = {
>>       CLK("da830-mmc.0",      NULL,           &mmcsd0_clk),
>>       CLK("da830-mmc.1",      NULL,           &mmcsd1_clk),
>>       CLK("ti-aemif",         NULL,           &aemif_clk),
>> -     CLK(NULL,               "aemif",        &aemif_clk),
>> +     CLK(NULL,               "nand",         &nand_clk),
>
> Why use a NULL device name here?
>
>>       CLK("ohci-da8xx",       "usb11",        &usb11_clk),
>>       CLK("musb-da8xx",       "usb20",        &usb20_clk),
>>       CLK("spi_davinci.0",    NULL,           &spi0_clk),
>> diff --git a/drivers/mtd/nand/davinci_nand.c b/drivers/mtd/nand/davinci_nand.c
>> index 27fa8b8..5857d06 100644
>> --- a/drivers/mtd/nand/davinci_nand.c
>> +++ b/drivers/mtd/nand/davinci_nand.c
>> @@ -694,7 +694,7 @@ static int nand_davinci_probe(struct platform_device *pdev)
>>
>>       ret = -EINVAL;
>>
>> -     info->clk = devm_clk_get(&pdev->dev, "aemif");
>> +     info->clk = devm_clk_get(&pdev->dev, "nand");
>
> This driver is used by keystone devices too. So just changing the
> connection id will likely break them. Why do you need to change the
> connection id?
>
> Thanks,
> Sekhar

Thanks, I didn't know it.

I thought it would make the purpose of the clock more obvious. I'll
change it back to "aemif".

Best regards,
Bartosz Golaszewski

^ permalink raw reply

* [PATCH v2 3/3] ARM: da850: fix da850_set_pll0rate()
From: Bartosz Golaszewski @ 2016-12-05  9:07 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <130825f1-5c8f-f6bc-1f27-929ff591a9c3@ti.com>

2016-12-05 9:45 GMT+01:00 Sekhar Nori <nsekhar@ti.com>:
> On Friday 02 December 2016 09:08 PM, Bartosz Golaszewski wrote:
>> This function is broken - its second argument is an index to the freq
>> table, not the requested clock rate in Hz. It leads to an oops when
>> called from clk_set_rate() since this argument isn't bounds checked
>> either.
>>
>> Fix it by iterating over the array of supported frequencies and
>> selecting a one that matches or returning -EINVAL for unsupported
>> rates.
>>
>> Also: update the davinci cpufreq driver. It's the only user of this
>> clock and currently it passes the cpufreq table index to
>> clk_set_rate(), which is confusing. Make it pass the requested clock
>> rate in Hz.
>>
>> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>> ---
>>  arch/arm/mach-davinci/da850.c     | 22 ++++++++++++++++++----
>>  drivers/cpufreq/davinci-cpufreq.c |  2 +-
>>  2 files changed, 19 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/arm/mach-davinci/da850.c b/arch/arm/mach-davinci/da850.c
>> index a55101c..92e3303 100644
>> --- a/arch/arm/mach-davinci/da850.c
>> +++ b/arch/arm/mach-davinci/da850.c
>> @@ -1179,14 +1179,28 @@ static int da850_set_armrate(struct clk *clk, unsigned long index)
>>       return clk_set_rate(pllclk, index);
>>  }
>>
>> -static int da850_set_pll0rate(struct clk *clk, unsigned long index)
>> +static int da850_set_pll0rate(struct clk *clk, unsigned long rate)
>>  {
>> -     unsigned int prediv, mult, postdiv;
>> -     struct da850_opp *opp;
>>       struct pll_data *pll = clk->pll_data;
>> +     struct cpufreq_frequency_table *freq;
>> +     unsigned int prediv, mult, postdiv;
>> +     struct da850_opp *opp = NULL;
>>       int ret;
>>
>> -     opp = (struct da850_opp *) cpufreq_info.freq_table[index].driver_data;
>> +     for (freq = da850_freq_table;
>> +          freq->frequency != CPUFREQ_TABLE_END; freq++) {
>> +             /* requested_rate is in Hz, freq->frequency is in KHz */
>> +             unsigned long freq_rate = freq->frequency * 1000;
>
> A small optimization here. Instead of multiplying potentially every
> frequency in the table by 1000, you could divide the incoming rate down
> to KHz. This will also avoid the need for 'freq_rate'. Should have
> noticed this earlier. Sorry about that.
>
> Thanks,
> Sekhar

I thought about it, but figured the multiplication would be safer. I
will change it if you prefer this version.

Best regards,
Bartosz Golaszewski

^ permalink raw reply

* [linux-sunxi] [PATCH v3 -next 2/2] ARM: dts: sunxi: add support for Orange Pi Zero board
From: Maxime Ripard @ 2016-12-05  9:12 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161203162455.OlDm27ch@smtp3m.mail.yandex.net>

Hi Icenowy,

On Sat, Dec 03, 2016 at 09:24:19PM +0800, Icenowy Zheng wrote:
> <p dir="ltr"><br>
> 2016&#24180;12&#26376;3&#26085; &#19979;&#21320;5:43&#20110; Jernej Skrabec &lt;jernej.skrabec at gmail.com&gt;&#20889;&#36947;&#65306;<br>
> &gt;<br>
> &gt; Hi,<br>
> &gt;<br>
> &gt; Dne petek, 02. december 2016 17.42.04 UTC+1 je oseba Chen-Yu Tsai napisala:<br>
> &gt;&gt;<br>
> &gt;&gt; Hi, <br>
> &gt;&gt;<br>
> &gt;&gt; On Fri, Dec 2, 2016 at 11:05 PM, Icenowy Zheng &lt;ice... at aosc.xyz&gt; wrote: <br>
> &gt;&gt; &gt; Orange Pi Zero is a board that came with the new Allwinner H2+ SoC and a <br>
> &gt;&gt; &gt; SDIO Wi-Fi chip by Allwinner (XR819). <br>
> &gt;&gt; &gt; <br>
> &gt;&gt; &gt; Add a device tree file for it. <br>
> &gt;&gt; &gt; <br>
> &gt;&gt; &gt; Signed-off-by: Icenowy Zheng &lt;ice... at aosc.xyz&gt; <br>

Please make sure to disable the HTML replies, this is what your mail
looks like on a !HTML MUA :)

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20161205/8617213b/attachment.sig>

^ permalink raw reply

* [PATCH v3 0/4]  mm: fix the "counter.sh" failure for libhugetlbfs
From: Huang Shijie @ 2016-12-05  9:17 UTC (permalink / raw)
  To: linux-arm-kernel

(1) Background
   For the arm64, the hugetlb page size can be 32M (PMD + Contiguous bit).
   In the 4K page environment, the max page order is 10 (max_order - 1),
   so 32M page is the gigantic page.    

   The arm64 MMU supports a Contiguous bit which is a hint that the TTE
   is one of a set of contiguous entries which can be cached in a single
   TLB entry.  Please refer to the arm64v8 mannul :
       DDI0487A_f_armv8_arm.pdf (in page D4-1811)

(2) The bug   
   After I tested the libhugetlbfs, I found the test case "counter.sh"
   will fail with the gigantic page (32M page in arm64 board).

   The counter.sh is just a wrapper for counter.c.
   You can find them in:
       https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/counters.c
       https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/counters.sh

   The error log shows below:

   ----------------------------------------------------------
        ...........................................
	LD_PRELOAD=libhugetlbfs.so shmoverride_unlinked (32M: 64):	PASS
	LD_PRELOAD=libhugetlbfs.so HUGETLB_SHM=yes shmoverride_unlinked (32M: 64):	PASS
	quota.sh (32M: 64):	PASS
	counters.sh (32M: 64):	FAIL mmap failed: Invalid argument
	********** TEST SUMMARY
	*                      32M           
	*                      32-bit 64-bit 
	*     Total testcases:     0     87   
	*             Skipped:     0      0   
	*                PASS:     0     86   
	*                FAIL:     0      1   
	*    Killed by signal:     0      0   
	*   Bad configuration:     0      0   
	*       Expected FAIL:     0      0   
	*     Unexpected PASS:     0      0   
	* Strange test result:     0      0   
	**********
   ----------------------------------------------------------

   The failure is caused by:
    1) kernel fails to allocate a gigantic page for the surplus case.
       And the gather_surplus_pages() will return NULL in the end.

    2) The condition checks for some functions are wrong:
        return_unused_surplus_pages()
        nr_overcommit_hugepages_store()
        hugetlb_overcommit_handler()
   
   This patch set adds support for gigantic surplus hugetlb pages,
   allowing the counter.sh unit test to pass. 
   Test this patch set with Juno-r1 board.

   	
v2 -- > v3:
   1.) In patch 2, change argument "no_init" to "do_prep" 
   2.) In patch 3, also change alloc_fresh_huge_page().
       In the v2, this patch only changes the alloc_fresh_gigantic_page().  
   3.) Merge old patch #4,#5 into the last one.    
   4.) Follow Babka's suggestion, do the NULL check for @mask.
   5.) others.


v1 -- > v2:
   1.) fix the compiler error in X86.
   2.) add new patches for NUMA.
       The patch #2 ~ #5 are new patches.

Huang Shijie (4):
  mm: hugetlb: rename some allocation functions
  mm: hugetlb: add a new parameter for some functions
  mm: hugetlb: change the return type for some functions
  mm: hugetlb: support gigantic surplus pages

 include/linux/mempolicy.h |   8 +++
 mm/hugetlb.c              | 146 +++++++++++++++++++++++++++++++++++-----------
 mm/mempolicy.c            |  44 ++++++++++++++
 3 files changed, 163 insertions(+), 35 deletions(-)

-- 
2.5.5

^ permalink raw reply

* [PATCH v3 1/4] mm: hugetlb: rename some allocation functions
From: Huang Shijie @ 2016-12-05  9:17 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480929431-22348-1-git-send-email-shijie.huang@arm.com>

After a future patch, the __alloc_buddy_huge_page() will not necessarily
use the buddy allocator.

So this patch removes the "buddy" from these functions:
	__alloc_buddy_huge_page -> __alloc_huge_page
	__alloc_buddy_huge_page_no_mpol -> __alloc_huge_page_no_mpol
	__alloc_buddy_huge_page_with_mpol -> __alloc_huge_page_with_mpol

This patch also adds the description for alloc_gigantic_page().

This patch makes preparation for the later patch.

Signed-off-by: Huang Shijie <shijie.huang@arm.com>
---
 mm/hugetlb.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5f228cd..5f4213d 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1089,6 +1089,12 @@ static bool zone_spans_last_pfn(const struct zone *zone,
 	return zone_spans_pfn(zone, last_pfn);
 }
 
+/*
+ * Allocate a gigantic page from @nid node.
+ *
+ * Scan the zones of @nid node, and try to allocate a number of contiguous
+ * pages (1 << order).
+ */
 static struct page *alloc_gigantic_page(int nid, unsigned int order)
 {
 	unsigned long nr_pages = 1 << order;
@@ -1157,6 +1163,10 @@ static int alloc_fresh_gigantic_page(struct hstate *h,
 
 static inline bool gigantic_page_supported(void) { return true; }
 #else
+static inline struct page *alloc_gigantic_page(int nid, unsigned int order)
+{
+	return NULL;
+}
 static inline bool gigantic_page_supported(void) { return false; }
 static inline void free_gigantic_page(struct page *page, unsigned int order) { }
 static inline void destroy_compound_gigantic_page(struct page *page,
@@ -1568,7 +1578,7 @@ static struct page *__hugetlb_alloc_buddy_huge_page(struct hstate *h,
  * For (2), we ignore 'vma' and 'addr' and use 'nid' exclusively. This
  * implies that memory policies will not be taken in to account.
  */
-static struct page *__alloc_buddy_huge_page(struct hstate *h,
+static struct page *__alloc_huge_page(struct hstate *h,
 		struct vm_area_struct *vma, unsigned long addr, int nid)
 {
 	struct page *page;
@@ -1649,21 +1659,21 @@ static struct page *__alloc_buddy_huge_page(struct hstate *h,
  * anywhere.
  */
 static
-struct page *__alloc_buddy_huge_page_no_mpol(struct hstate *h, int nid)
+struct page *__alloc_huge_page_no_mpol(struct hstate *h, int nid)
 {
 	unsigned long addr = -1;
 
-	return __alloc_buddy_huge_page(h, NULL, addr, nid);
+	return __alloc_huge_page(h, NULL, addr, nid);
 }
 
 /*
  * Use the VMA's mpolicy to allocate a huge page from the buddy.
  */
 static
-struct page *__alloc_buddy_huge_page_with_mpol(struct hstate *h,
+struct page *__alloc_huge_page_with_mpol(struct hstate *h,
 		struct vm_area_struct *vma, unsigned long addr)
 {
-	return __alloc_buddy_huge_page(h, vma, addr, NUMA_NO_NODE);
+	return __alloc_huge_page(h, vma, addr, NUMA_NO_NODE);
 }
 
 /*
@@ -1681,7 +1691,7 @@ struct page *alloc_huge_page_node(struct hstate *h, int nid)
 	spin_unlock(&hugetlb_lock);
 
 	if (!page)
-		page = __alloc_buddy_huge_page_no_mpol(h, nid);
+		page = __alloc_huge_page_no_mpol(h, nid);
 
 	return page;
 }
@@ -1711,7 +1721,7 @@ static int gather_surplus_pages(struct hstate *h, int delta)
 retry:
 	spin_unlock(&hugetlb_lock);
 	for (i = 0; i < needed; i++) {
-		page = __alloc_buddy_huge_page_no_mpol(h, NUMA_NO_NODE);
+		page = __alloc_huge_page_no_mpol(h, NUMA_NO_NODE);
 		if (!page) {
 			alloc_ok = false;
 			break;
@@ -2027,7 +2037,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,
 	page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, gbl_chg);
 	if (!page) {
 		spin_unlock(&hugetlb_lock);
-		page = __alloc_buddy_huge_page_with_mpol(h, vma, addr);
+		page = __alloc_huge_page_with_mpol(h, vma, addr);
 		if (!page)
 			goto out_uncharge_cgroup;
 		if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) {
@@ -2285,7 +2295,7 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
 	 * First take pages out of surplus state.  Then make up the
 	 * remaining difference by allocating fresh huge pages.
 	 *
-	 * We might race with __alloc_buddy_huge_page() here and be unable
+	 * We might race with __alloc_huge_page() here and be unable
 	 * to convert a surplus huge page to a normal huge page. That is
 	 * not critical, though, it just means the overall size of the
 	 * pool might be one hugepage larger than it needs to be, but
@@ -2331,7 +2341,7 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
 	 * By placing pages into the surplus state independent of the
 	 * overcommit value, we are allowing the surplus pool size to
 	 * exceed overcommit. There are few sane options here. Since
-	 * __alloc_buddy_huge_page() is checking the global counter,
+	 * __alloc_huge_page() is checking the global counter,
 	 * though, we'll note that we're not allowed to exceed surplus
 	 * and won't grow the pool anywhere else. Not until one of the
 	 * sysctls are changed, or the surplus pages go out of use.
-- 
2.5.5

^ permalink raw reply related

* [PATCH v3 2/4] mm: hugetlb: add a new parameter for some functions
From: Huang Shijie @ 2016-12-05  9:17 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480929431-22348-1-git-send-email-shijie.huang@arm.com>

This patch adds a new parameter, the "do_prep", for these functions:
   alloc_fresh_gigantic_page_node()
   alloc_fresh_gigantic_page()

The prep_new_huge_page() does some initialization for the new page.
But sometime, we do not need it to do so, such as in the surplus case
in later patch.

With this parameter, the prep_new_huge_page() can be called by needed:
   If the "do_prep" is true, calls the prep_new_huge_page() in
   the alloc_fresh_gigantic_page_node();

This patch makes preparation for the later patches.

Signed-off-by: Huang Shijie <shijie.huang@arm.com>
---
 mm/hugetlb.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5f4213d..b7c73a1 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1133,27 +1133,29 @@ static struct page *alloc_gigantic_page(int nid, unsigned int order)
 static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
 static void prep_compound_gigantic_page(struct page *page, unsigned int order);
 
-static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid)
+static struct page *alloc_fresh_gigantic_page_node(struct hstate *h,
+					int nid, bool do_prep)
 {
 	struct page *page;
 
 	page = alloc_gigantic_page(nid, huge_page_order(h));
 	if (page) {
 		prep_compound_gigantic_page(page, huge_page_order(h));
-		prep_new_huge_page(h, page, nid);
+		if (do_prep)
+			prep_new_huge_page(h, page, nid);
 	}
 
 	return page;
 }
 
 static int alloc_fresh_gigantic_page(struct hstate *h,
-				nodemask_t *nodes_allowed)
+				nodemask_t *nodes_allowed, bool do_prep)
 {
 	struct page *page = NULL;
 	int nr_nodes, node;
 
 	for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
-		page = alloc_fresh_gigantic_page_node(h, node);
+		page = alloc_fresh_gigantic_page_node(h, node, do_prep);
 		if (page)
 			return 1;
 	}
@@ -1172,7 +1174,7 @@ static inline void free_gigantic_page(struct page *page, unsigned int order) { }
 static inline void destroy_compound_gigantic_page(struct page *page,
 						unsigned int order) { }
 static inline int alloc_fresh_gigantic_page(struct hstate *h,
-					nodemask_t *nodes_allowed) { return 0; }
+		nodemask_t *nodes_allowed, bool do_prep) { return 0; }
 #endif
 
 static void update_and_free_page(struct hstate *h, struct page *page)
@@ -2319,7 +2321,7 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
 		cond_resched();
 
 		if (hstate_is_gigantic(h))
-			ret = alloc_fresh_gigantic_page(h, nodes_allowed);
+			ret = alloc_fresh_gigantic_page(h, nodes_allowed, true);
 		else
 			ret = alloc_fresh_huge_page(h, nodes_allowed);
 		spin_lock(&hugetlb_lock);
-- 
2.5.5

^ permalink raw reply related

* [PATCH v3 3/4] mm: hugetlb: change the return type for some functions
From: Huang Shijie @ 2016-12-05  9:17 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480929431-22348-1-git-send-email-shijie.huang@arm.com>

This patch changes the return type to "struct page*" for
alloc_fresh_gigantic_page()/alloc_fresh_huge_page().

This patch makes preparation for later patch.

Signed-off-by: Huang Shijie <shijie.huang@arm.com>
---
 mm/hugetlb.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index b7c73a1..1395bef 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1148,7 +1148,7 @@ static struct page *alloc_fresh_gigantic_page_node(struct hstate *h,
 	return page;
 }
 
-static int alloc_fresh_gigantic_page(struct hstate *h,
+static struct page *alloc_fresh_gigantic_page(struct hstate *h,
 				nodemask_t *nodes_allowed, bool do_prep)
 {
 	struct page *page = NULL;
@@ -1157,10 +1157,10 @@ static int alloc_fresh_gigantic_page(struct hstate *h,
 	for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
 		page = alloc_fresh_gigantic_page_node(h, node, do_prep);
 		if (page)
-			return 1;
+			return page;
 	}
 
-	return 0;
+	return NULL;
 }
 
 static inline bool gigantic_page_supported(void) { return true; }
@@ -1173,8 +1173,8 @@ static inline bool gigantic_page_supported(void) { return false; }
 static inline void free_gigantic_page(struct page *page, unsigned int order) { }
 static inline void destroy_compound_gigantic_page(struct page *page,
 						unsigned int order) { }
-static inline int alloc_fresh_gigantic_page(struct hstate *h,
-		nodemask_t *nodes_allowed, bool do_prep) { return 0; }
+static inline struct page *alloc_fresh_gigantic_page(struct hstate *h,
+		nodemask_t *nodes_allowed, bool do_prep) { return NULL; }
 #endif
 
 static void update_and_free_page(struct hstate *h, struct page *page)
@@ -1387,26 +1387,24 @@ static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
 	return page;
 }
 
-static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
+static struct page *alloc_fresh_huge_page(struct hstate *h,
+					nodemask_t *nodes_allowed)
 {
-	struct page *page;
+	struct page *page = NULL;
 	int nr_nodes, node;
-	int ret = 0;
 
 	for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
 		page = alloc_fresh_huge_page_node(h, node);
-		if (page) {
-			ret = 1;
+		if (page)
 			break;
-		}
 	}
 
-	if (ret)
+	if (page)
 		count_vm_event(HTLB_BUDDY_PGALLOC);
 	else
 		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
 
-	return ret;
+	return page;
 }
 
 /*
@@ -2321,9 +2319,10 @@ static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
 		cond_resched();
 
 		if (hstate_is_gigantic(h))
-			ret = alloc_fresh_gigantic_page(h, nodes_allowed, true);
+			ret = !!alloc_fresh_gigantic_page(h, nodes_allowed,
+							true);
 		else
-			ret = alloc_fresh_huge_page(h, nodes_allowed);
+			ret = !!alloc_fresh_huge_page(h, nodes_allowed);
 		spin_lock(&hugetlb_lock);
 		if (!ret)
 			goto out;
-- 
2.5.5

^ permalink raw reply related

* [PATCH v3 4/4] mm: hugetlb: support gigantic surplus pages
From: Huang Shijie @ 2016-12-05  9:17 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480929431-22348-1-git-send-email-shijie.huang@arm.com>

When testing the gigantic page whose order is too large for the buddy
allocator, the libhugetlbfs test case "counter.sh" will fail.

The counter.sh is just a wrapper for counter.c, you can find them in:
  https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/counters.c
  https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/counters.sh

Please see the error log below:
 ............................................
    ........
    quota.sh (32M: 64):	PASS
    counters.sh (32M: 64):	FAIL mmap failed: Invalid argument
    ********** TEST SUMMARY
    *                      32M
    *                      32-bit 64-bit
    *     Total testcases:     0     87
    *             Skipped:     0      0
    *                PASS:     0     86
    *                FAIL:     0      1
    *    Killed by signal:     0      0
    *   Bad configuration:     0      0
    *       Expected FAIL:     0      0
    *     Unexpected PASS:     0      0
    * Strange test result:     0      0
    **********
 ............................................

The failure is caused by:
 1) kernel fails to allocate a gigantic page for the surplus case.
    And the gather_surplus_pages() will return NULL in the end.

 2) The condition checks for "over-commit" is wrong.

This patch does following things:

 1) This patch changes the condition checks for:
     return_unused_surplus_pages()
     nr_overcommit_hugepages_store()
     hugetlb_overcommit_handler()

 2) This patch introduces two helper functions:
    huge_nodemask() and __hugetlb_alloc_gigantic_page().
    Please see the descritions in the two functions.

 3) This patch uses __hugetlb_alloc_gigantic_page() to allocate the
    gigantic page in the __alloc_huge_page(). After this patch,
    gather_surplus_pages() can return a gigantic page for the surplus case.

After this patch, the counter.sh can pass for the gigantic page.

Signed-off-by: Huang Shijie <shijie.huang@arm.com>
---
 include/linux/mempolicy.h |  8 +++++
 mm/hugetlb.c              | 77 +++++++++++++++++++++++++++++++++++++++++++----
 mm/mempolicy.c            | 44 +++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 6 deletions(-)

diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h
index 5f4d828..6539fbb 100644
--- a/include/linux/mempolicy.h
+++ b/include/linux/mempolicy.h
@@ -146,6 +146,8 @@ extern void mpol_rebind_task(struct task_struct *tsk, const nodemask_t *new,
 				enum mpol_rebind_step step);
 extern void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new);
 
+extern bool huge_nodemask(struct vm_area_struct *vma,
+				unsigned long addr, nodemask_t *mask);
 extern struct zonelist *huge_zonelist(struct vm_area_struct *vma,
 				unsigned long addr, gfp_t gfp_flags,
 				struct mempolicy **mpol, nodemask_t **nodemask);
@@ -269,6 +271,12 @@ static inline void mpol_rebind_mm(struct mm_struct *mm, nodemask_t *new)
 {
 }
 
+static inline bool huge_nodemask(struct vm_area_struct *vma,
+				unsigned long addr, nodemask_t *mask)
+{
+	return false;
+}
+
 static inline struct zonelist *huge_zonelist(struct vm_area_struct *vma,
 				unsigned long addr, gfp_t gfp_flags,
 				struct mempolicy **mpol, nodemask_t **nodemask)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 1395bef..04440b8 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1506,6 +1506,69 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
 /*
  * There are 3 ways this can get called:
+ *
+ * 1. When the NUMA is not enabled, use alloc_gigantic_page() to get
+ *    the gigantic page.
+ *
+ * 2. The NUMA is enabled, but the vma is NULL.
+ *    Initialize the @mask, and use alloc_fresh_gigantic_page() to get
+ *    the gigantic page.
+ *
+ * 3. The NUMA is enabled, and the vma is valid.
+ *    Use the @vma's memory policy.
+ *    Get @mask by huge_nodemask(), and use alloc_fresh_gigantic_page()
+ *    to get the gigantic page.
+ */
+static struct page *__hugetlb_alloc_gigantic_page(struct hstate *h,
+		struct vm_area_struct *vma, unsigned long addr, int nid)
+{
+	NODEMASK_ALLOC(nodemask_t, mask, GFP_KERNEL | __GFP_NORETRY);
+	struct page *page = NULL;
+
+	/* Not NUMA */
+	if (!IS_ENABLED(CONFIG_NUMA)) {
+		if (nid == NUMA_NO_NODE)
+			nid = numa_mem_id();
+
+		page = alloc_gigantic_page(nid, huge_page_order(h));
+		if (page)
+			prep_compound_gigantic_page(page, huge_page_order(h));
+		goto got_page;
+	}
+
+	/* NUMA && !vma */
+	if (!vma) {
+		/* First, check the mask */
+		if (!mask) {
+			mask = &node_states[N_MEMORY];
+		} else {
+			if (nid == NUMA_NO_NODE) {
+				if (!init_nodemask_of_mempolicy(mask)) {
+					NODEMASK_FREE(mask);
+					mask = &node_states[N_MEMORY];
+				}
+			} else {
+				init_nodemask_of_node(mask, nid);
+			}
+		}
+
+		page = alloc_fresh_gigantic_page(h, mask, false);
+		goto got_page;
+	}
+
+	/* NUMA && vma */
+	if (mask && huge_nodemask(vma, addr, mask))
+		page = alloc_fresh_gigantic_page(h, mask, false);
+
+got_page:
+	if (mask != &node_states[N_MEMORY])
+		NODEMASK_FREE(mask);
+
+	return page;
+}
+
+/*
+ * There are 3 ways this can get called:
  * 1. With vma+addr: we use the VMA's memory policy
  * 2. With !vma, but nid=NUMA_NO_NODE:  We try to allocate a huge
  *    page from any node, and let the buddy allocator itself figure
@@ -1584,7 +1647,7 @@ static struct page *__alloc_huge_page(struct hstate *h,
 	struct page *page;
 	unsigned int r_nid;
 
-	if (hstate_is_gigantic(h))
+	if (hstate_is_gigantic(h) && !gigantic_page_supported())
 		return NULL;
 
 	/*
@@ -1629,7 +1692,10 @@ static struct page *__alloc_huge_page(struct hstate *h,
 	}
 	spin_unlock(&hugetlb_lock);
 
-	page = __hugetlb_alloc_buddy_huge_page(h, vma, addr, nid);
+	if (hstate_is_gigantic(h))
+		page = __hugetlb_alloc_gigantic_page(h, vma, addr, nid);
+	else
+		page = __hugetlb_alloc_buddy_huge_page(h, vma, addr, nid);
 
 	spin_lock(&hugetlb_lock);
 	if (page) {
@@ -1796,8 +1862,7 @@ static void return_unused_surplus_pages(struct hstate *h,
 	/* Uncommit the reservation */
 	h->resv_huge_pages -= unused_resv_pages;
 
-	/* Cannot return gigantic pages currently */
-	if (hstate_is_gigantic(h))
+	if (hstate_is_gigantic(h) && !gigantic_page_supported())
 		return;
 
 	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
@@ -2514,7 +2579,7 @@ static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
 	unsigned long input;
 	struct hstate *h = kobj_to_hstate(kobj, NULL);
 
-	if (hstate_is_gigantic(h))
+	if (hstate_is_gigantic(h) && !gigantic_page_supported())
 		return -EINVAL;
 
 	err = kstrtoul(buf, 10, &input);
@@ -2966,7 +3031,7 @@ int hugetlb_overcommit_handler(struct ctl_table *table, int write,
 
 	tmp = h->nr_overcommit_huge_pages;
 
-	if (write && hstate_is_gigantic(h))
+	if (write && hstate_is_gigantic(h) && !gigantic_page_supported())
 		return -EINVAL;
 
 	table->data = &tmp;
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 6d3639e..3550a29 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -1800,6 +1800,50 @@ static inline unsigned interleave_nid(struct mempolicy *pol,
 
 #ifdef CONFIG_HUGETLBFS
 /*
+ * huge_nodemask(@vma, @addr, @mask)
+ * @vma: virtual memory area whose policy is sought
+ * @addr: address in @vma
+ * @mask: should be a valid nodemask pointer, not NULL
+ *
+ * Return true if we can succeed in extracting the policy nodemask
+ * for 'bind' or 'interleave' policy into the argument @mask, or
+ * initializing the argument @mask to contain the single node for
+ * 'preferred' or 'local' policy.
+ */
+bool huge_nodemask(struct vm_area_struct *vma, unsigned long addr,
+			nodemask_t *mask)
+{
+	struct mempolicy *mpol;
+	bool ret = true;
+	int nid;
+
+	mpol = get_vma_policy(vma, addr);
+
+	switch (mpol->mode) {
+	case MPOL_PREFERRED:
+		if (mpol->flags & MPOL_F_LOCAL)
+			nid = numa_node_id();
+		else
+			nid = mpol->v.preferred_node;
+		init_nodemask_of_node(mask, nid);
+		break;
+
+	case MPOL_BIND:
+		/* Fall through */
+	case MPOL_INTERLEAVE:
+		*mask = mpol->v.nodes;
+		break;
+
+	default:
+		ret = false;
+		break;
+	}
+	mpol_cond_put(mpol);
+
+	return ret;
+}
+
+/*
  * huge_zonelist(@vma, @addr, @gfp_flags, @mpol)
  * @vma: virtual memory area whose policy is sought
  * @addr: address in @vma for shared policy lookup and interleave policy
-- 
2.5.5

^ permalink raw reply related

* [crypto] [marvell-cesa] Possible regression after Linux 4.7
From: Romain Perier @ 2016-12-05  9:19 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <4573998d-6c5b-03e4-e1fa-7516d5a9e276@free-electrons.com>

Hello,

The issue is reproducible in STD mode too. We have found fixes.
Could you test these patches in STD mode please ?


cover letter: 
https://www.mail-archive.com/linux-crypto at vger.kernel.org/msg22143.html
1/2: https://www.mail-archive.com/linux-crypto at vger.kernel.org/msg22142.html
2/2: https://www.mail-archive.com/linux-crypto at vger.kernel.org/msg22141.html

If you want to turn off DMA, open drivers/crypto/marvell/cesa.c and in 
armada_xp_caps, change .has_tdma to false.  (assuming that you test on a 
armada 38x).

We're still investigating for regressions in DMA mode.

Have a nice day,
Romain

-- 
Romain Perier, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply

* [PATCH v3 -next 1/2] ARM: sunxi: add support for H2+ SoC
From: Maxime Ripard @ 2016-12-05  9:19 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161202150513.34691-1-icenowy@aosc.xyz>

On Fri, Dec 02, 2016 at 11:05:12PM +0800, Icenowy Zheng wrote:
> Allwinner H2+ is a quad-core Cortex-A7 SoC.
> 
> It is very like H3, that they share the same SoC ID (0x1680), and H3
> memory maps as well as drivers works well on the SoC.
> 
> Signed-off-by: Icenowy Zheng <icenowy@aosc.xyz>

Fixed the alphabetical order in the bindings doc, and applied.

Thanks!
Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20161205/364dbd55/attachment.sig>

^ permalink raw reply

* [PATCH] ARM: dts: sunxi: Add num-cs for A20 spi nodes
From: Maxime Ripard @ 2016-12-05  9:28 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161201112414.62f9b351186115f62c8998a9@bidouilliste.com>

On Thu, Dec 01, 2016 at 11:24:14AM +0100, Emmanuel Vadot wrote:
> > > > >  If num-cs isn't present nothing prevent to start a transfer
> > > > > with a non-valid CS pin, resulting in an error.  num-cs are
> > > > > default property especially made for this and a SPI driver
> > > > > should try to get the property at probe/attach time.
> > > > 
> > > > Yes, but as far as I know, our driver doesn't. I'm all in for
> > > > having support for that in our driver, but without it, that
> > > > patch is kind of useless.
> > > 
> > >  Yes the Linux driver doesn't use it but my upcoming one for FreeBSD
> > > uses it. So it is not useless for downstream user of DTS.
> > 
> > Ah, I didn't know this was for FreeBSD. So you started to use our DTs,
> > or do you have some modifications to it? How does that work?
> 
> Yes we use the DTS from linux from quite some times now. We're
> currently synced with 4.7-ish.  We either use them directly or
> modify them according to our needs and driver support.

Do you have a link to those modifications?

Thanks,
Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20161205/7f666e1b/attachment-0001.sig>

^ permalink raw reply

* [PATCH v3 0/4] mm: fix the "counter.sh" failure for libhugetlbfs
From: Michal Hocko @ 2016-12-05  9:31 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480929431-22348-1-git-send-email-shijie.huang@arm.com>

On Mon 05-12-16 17:17:07, Huang Shijie wrote:
[...]
>    The failure is caused by:
>     1) kernel fails to allocate a gigantic page for the surplus case.
>        And the gather_surplus_pages() will return NULL in the end.
> 
>     2) The condition checks for some functions are wrong:
>         return_unused_surplus_pages()
>         nr_overcommit_hugepages_store()
>         hugetlb_overcommit_handler()

OK, so how is this any different from gigantic (1G) hugetlb pages on
x86_64? Do we need the same functionality or is it just 32MB not being
handled in the same way as 1G?

Thanks!
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply

* [PATCH] arm/arm64: KVM: Check for properly initialized timer on init
From: Christoffer Dall @ 2016-12-05  9:32 UTC (permalink / raw)
  To: linux-arm-kernel

When the arch timer code fails to initialize (for example because the
memory mapped timer doesn't work, which is currently seen with the AEM
model), then KVM just continues happily with a final result that KVM
eventually does a NULL pointer dereference of the uninitialized cycle
counter.

Check directly for this in the init path and give the user a reasonable
error in this case.

Cc: Shih-Wei Li <shihwei@cs.columbia.edu>
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
---
 virt/kvm/arm/arch_timer.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/virt/kvm/arm/arch_timer.c b/virt/kvm/arm/arch_timer.c
index 27a1f63..5c12f53 100644
--- a/virt/kvm/arm/arch_timer.c
+++ b/virt/kvm/arm/arch_timer.c
@@ -425,6 +425,11 @@ int kvm_timer_hyp_init(void)
 	info = arch_timer_get_kvm_info();
 	timecounter = &info->timecounter;
 
+	if (!timecounter->cc) {
+		kvm_err("arch_timer: uninitialized timecounter\n");
+		return -ENODEV;
+	}
+
 	if (info->virtual_irq <= 0) {
 		kvm_err("kvm_arch_timer: invalid virtual timer IRQ: %d\n",
 			info->virtual_irq);
-- 
2.9.0

^ permalink raw reply related

* [PATCH] ARM: dts: sunxi: Add num-cs for A20 spi nodes
From: Emmanuel Vadot @ 2016-12-05  9:39 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161205092821.m2fmqxnzalcignly@lukather>

On Mon, 5 Dec 2016 10:28:21 +0100
Maxime Ripard <maxime.ripard@free-electrons.com> wrote:

> On Thu, Dec 01, 2016 at 11:24:14AM +0100, Emmanuel Vadot wrote:
> > > > > >  If num-cs isn't present nothing prevent to start a transfer
> > > > > > with a non-valid CS pin, resulting in an error.  num-cs are
> > > > > > default property especially made for this and a SPI driver
> > > > > > should try to get the property at probe/attach time.
> > > > > 
> > > > > Yes, but as far as I know, our driver doesn't. I'm all in for
> > > > > having support for that in our driver, but without it, that
> > > > > patch is kind of useless.
> > > > 
> > > >  Yes the Linux driver doesn't use it but my upcoming one for FreeBSD
> > > > uses it. So it is not useless for downstream user of DTS.
> > > 
> > > Ah, I didn't know this was for FreeBSD. So you started to use our DTs,
> > > or do you have some modifications to it? How does that work?
> > 
> > Yes we use the DTS from linux from quite some times now. We're
> > currently synced with 4.7-ish.  We either use them directly or
> > modify them according to our needs and driver support.
> 
> Do you have a link to those modifications?
> 
> Thanks,
> Maxime
> 
> -- 
> Maxime Ripard, Free Electrons
> Embedded Linux and Kernel engineering
> http://free-electrons.com

 Sure,

 https://svnweb.freebsd.org/base/head/sys/boot/fdt/dts/arm/

-- 
Emmanuel Vadot <manu@bidouilliste.com> <manu@freebsd.org>

^ permalink raw reply

* [PATCH v2 2/3] ARM: dts: sunxi: add support for Orange Pi Zero board
From: Maxime Ripard @ 2016-12-05  9:40 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161205120021.0GBGtAl4@smtp3m.mail.yandex.net>

On Mon, Dec 05, 2016 at 04:59:44PM +0800, Icenowy Zheng wrote:
> 
> 2016?12?5? 16:52? Maxime Ripard <maxime.ripard@free-electrons.com>???
> >
> > On Fri, Dec 02, 2016 at 10:22:30PM +0800, Icenowy Zheng wrote: 
> > > 
> > > 
> > > 01.12.2016, 17:36, "Maxime Ripard" <maxime.ripard@free-electrons.com>: 
> > > > On Mon, Nov 28, 2016 at 12:29:07AM +0000, Andr? Przywara wrote: 
> > > >> ?> Something more interesting happened. 
> > > >> ?> 
> > > >> ?> Xunlong made a add-on board for Orange Pi Zero, which exposes the 
> > > >> ?> two USB Controllers exported at expansion bus as USB Type-A 
> > > >> ?> connectors. 
> > > >> ?> 
> > > >> ?> Also it exposes a analog A/V jack and a microphone. 
> > > >> ?> 
> > > >> ?> Should I enable {e,o}hci{2.3} in the device tree? 
> > > >> 
> > > >> ?Actually we should do this regardless of this extension board. The USB 
> > > >> ?pins are not multiplexed and are exposed on user accessible pins (just 
> > > >> ?not soldered, but that's a detail), so I think they qualify for DT 
> > > >> ?enablement. And even if a user can't use them, it doesn't hurt to have 
> > > >> ?them (since they are not multiplexed). 
> > > > 
> > > > My main concern about this is that we'll leave regulators enabled by 
> > > > default, for a minority of users. And that minority will prevent to do 
> > > > a proper power management when the times come since we'll have to keep 
> > > > that behaviour forever. 
> > > 
> > > I think these users can add a 'fdt set /xxx/xxx status "disabled" ' . 
> >
> > You can't ask that from the majority of users. These users will take 
> > debian or fedora, install it, and expect everything to work 
> > properly. I would make the opposite argument actually. If someone is 
> > knowledgeable enough to solder the USB pins a connector, then (s)he'll 
> > be able to make that u-boot call. 
> 
> Now (s)he do not need soldering.
> 
> (S)he needs only paying $1.99 more to Xunlong to get the expansion
> board, and insert it on the OPi Zero.

Which is going to require an overlay anyway, so we could have the USB
bits in there too.

Maxime

-- 
Maxime Ripard, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20161205/194dd5d6/attachment.sig>

^ permalink raw reply

* [PATCH 3/8] rtc: add STM32 RTC driver
From: Amelie DELAUNAY @ 2016-12-05  9:43 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161202175646.GA3284@linaro.org>

Hi Mathieu,

Thanks for reviewing

On 12/02/2016 06:56 PM, Mathieu Poirier wrote:
 > On Fri, Dec 02, 2016 at 03:09:56PM +0100, Amelie Delaunay wrote:
 >> This patch adds support for the STM32 RTC.
 >
 > Hello Amelie,
 >
 >>
 >> Signed-off-by: Amelie Delaunay <amelie.delaunay@st.com>
 >> ---
 >>  drivers/rtc/Kconfig     |  10 +
 >>  drivers/rtc/Makefile    |   1 +
 >>  drivers/rtc/rtc-stm32.c | 777 
++++++++++++++++++++++++++++++++++++++++++++++++
 >>  3 files changed, 788 insertions(+)
 >>  create mode 100644 drivers/rtc/rtc-stm32.c
 >>
 >> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
 >> index e859d14..dd8b218 100644
 >> --- a/drivers/rtc/Kconfig
 >> +++ b/drivers/rtc/Kconfig
 >> @@ -1706,6 +1706,16 @@ config RTC_DRV_PIC32
 >>         This driver can also be built as a module. If so, the module
 >>         will be called rtc-pic32
 >>
 >> +config RTC_DRV_STM32
 >> +    tristate "STM32 On-Chip RTC"
 >> +    depends on ARCH_STM32
 >> +    help
 >> +       If you say yes here you get support for the STM32 On-Chip
 >> +       Real Time Clock.
 >> +
 >> +       This driver can also be built as a module, if so, the module
 >> +       will be called "rtc-stm32".
 >> +
 >>  comment "HID Sensor RTC drivers"
 >>
 >>  config RTC_DRV_HID_SENSOR_TIME
 >> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
 >> index 1ac694a..87bd9cc 100644
 >> --- a/drivers/rtc/Makefile
 >> +++ b/drivers/rtc/Makefile
 >> @@ -144,6 +144,7 @@ obj-$(CONFIG_RTC_DRV_SNVS)    += rtc-snvs.o
 >>  obj-$(CONFIG_RTC_DRV_SPEAR)    += rtc-spear.o
 >>  obj-$(CONFIG_RTC_DRV_STARFIRE)    += rtc-starfire.o
 >>  obj-$(CONFIG_RTC_DRV_STK17TA8)    += rtc-stk17ta8.o
 >> +obj-$(CONFIG_RTC_DRV_STM32)     += rtc-stm32.o
 >>  obj-$(CONFIG_RTC_DRV_STMP)    += rtc-stmp3xxx.o
 >>  obj-$(CONFIG_RTC_DRV_ST_LPC)    += rtc-st-lpc.o
 >>  obj-$(CONFIG_RTC_DRV_SUN4V)    += rtc-sun4v.o
 >> diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c
 >> new file mode 100644
 >> index 0000000..9e710ff
 >> --- /dev/null
 >> +++ b/drivers/rtc/rtc-stm32.c
 >> @@ -0,0 +1,777 @@
 >> +/*
 >> + * Copyright (C) Amelie Delaunay 2015
 >> + * Author:  Amelie Delaunay <adelaunay.stm32@gmail.com>
 >> + * License terms:  GNU General Public License (GPL), version 2
 >> + */
 >> +
 >> +#include <linux/bcd.h>
 >> +#include <linux/clk.h>
 >> +#include <linux/init.h>
 >> +#include <linux/io.h>
 >> +#include <linux/iopoll.h>
 >> +#include <linux/ioport.h>
 >> +#include <linux/kernel.h>
 >> +#include <linux/mfd/syscon.h>
 >> +#include <linux/module.h>
 >> +#include <linux/of.h>
 >> +#include <linux/of_device.h>
 >> +#include <linux/platform_device.h>
 >> +#include <linux/regmap.h>
 >> +#include <linux/rtc.h>
 >> +#include <linux/spinlock.h>
 >> +
 >> +#define DRIVER_NAME "stm32_rtc"
 >> +
 >> +/* STM32 RTC registers */
 >> +#define STM32_RTC_TR        0x00
 >> +#define STM32_RTC_DR        0x04
 >> +#define STM32_RTC_CR        0x08
 >> +#define STM32_RTC_ISR        0x0C
 >> +#define STM32_RTC_PRER        0x10
 >> +#define STM32_RTC_ALRMAR    0x1C
 >> +#define STM32_RTC_WPR        0x24
 >> +
 >> +/* STM32_RTC_TR bit fields  */
 >> +#define STM32_RTC_TR_SEC_SHIFT        0
 >> +#define STM32_RTC_TR_SEC        GENMASK(6, 0)
 >> +#define STM32_RTC_TR_MIN_SHIFT        8
 >> +#define STM32_RTC_TR_MIN        GENMASK(14, 8)
 >> +#define STM32_RTC_TR_HOUR_SHIFT        16
 >> +#define STM32_RTC_TR_HOUR        GENMASK(21, 16)
 >> +
 >> +/* STM32_RTC_DR bit fields */
 >> +#define STM32_RTC_DR_DATE_SHIFT        0
 >> +#define STM32_RTC_DR_DATE        GENMASK(5, 0)
 >> +#define STM32_RTC_DR_MONTH_SHIFT    8
 >> +#define STM32_RTC_DR_MONTH        GENMASK(11, 8)
 >> +#define STM32_RTC_DR_WDAY_SHIFT        13
 >> +#define STM32_RTC_DR_WDAY        GENMASK(15, 13)
 >> +#define STM32_RTC_DR_YEAR_SHIFT        16
 >> +#define STM32_RTC_DR_YEAR        GENMASK(23, 16)
 >> +
 >> +/* STM32_RTC_CR bit fields */
 >> +#define STM32_RTC_CR_FMT        BIT(6)
 >> +#define STM32_RTC_CR_ALRAE        BIT(8)
 >> +#define STM32_RTC_CR_ALRAIE        BIT(12)
 >> +
 >> +/* STM32_RTC_ISR bit fields */
 >> +#define STM32_RTC_ISR_ALRAWF        BIT(0)
 >> +#define STM32_RTC_ISR_INITS        BIT(4)
 >> +#define STM32_RTC_ISR_RSF        BIT(5)
 >> +#define STM32_RTC_ISR_INITF        BIT(6)
 >> +#define STM32_RTC_ISR_INIT        BIT(7)
 >> +#define STM32_RTC_ISR_ALRAF        BIT(8)
 >> +
 >> +/* STM32_RTC_PRER bit fields */
 >> +#define STM32_RTC_PRER_PRED_S_SHIFT    0
 >> +#define STM32_RTC_PRER_PRED_S        GENMASK(14, 0)
 >> +#define STM32_RTC_PRER_PRED_A_SHIFT    16
 >> +#define STM32_RTC_PRER_PRED_A        GENMASK(22, 16)
 >> +
 >> +/* STM32_RTC_ALRMAR and STM32_RTC_ALRMBR bit fields */
 >> +#define STM32_RTC_ALRMXR_SEC_SHIFT    0
 >> +#define STM32_RTC_ALRMXR_SEC        GENMASK(6, 0)
 >> +#define STM32_RTC_ALRMXR_SEC_MASK    BIT(7)
 >> +#define STM32_RTC_ALRMXR_MIN_SHIFT    8
 >> +#define STM32_RTC_ALRMXR_MIN        GENMASK(14, 8)
 >> +#define STM32_RTC_ALRMXR_MIN_MASK    BIT(15)
 >> +#define STM32_RTC_ALRMXR_HOUR_SHIFT    16
 >> +#define STM32_RTC_ALRMXR_HOUR        GENMASK(21, 16)
 >> +#define STM32_RTC_ALRMXR_PM        BIT(22)
 >> +#define STM32_RTC_ALRMXR_HOUR_MASK    BIT(23)
 >> +#define STM32_RTC_ALRMXR_DATE_SHIFT    24
 >> +#define STM32_RTC_ALRMXR_DATE        GENMASK(29, 24)
 >> +#define STM32_RTC_ALRMXR_WDSEL        BIT(30)
 >> +#define STM32_RTC_ALRMXR_WDAY_SHIFT    24
 >> +#define STM32_RTC_ALRMXR_WDAY        GENMASK(27, 24)
 >> +#define STM32_RTC_ALRMXR_DATE_MASK    BIT(31)
 >> +
 >> +/* STM32_RTC_WPR key constants */
 >> +#define RTC_WPR_1ST_KEY            0xCA
 >> +#define RTC_WPR_2ND_KEY            0x53
 >> +#define RTC_WPR_WRONG_KEY        0xFF
 >> +
 >> +/*
 >> + * RTC registers are protected agains parasitic write access.
 >> + * PWR_CR_DBP bit must be set to enable write access to RTC registers.
 >> + */
 >> +/* STM32_PWR_CR */
 >> +#define PWR_CR                0x00
 >> +/* STM32_PWR_CR bit field */
 >> +#define PWR_CR_DBP            BIT(8)
 >> +
 >> +static struct regmap *dbp;
 >> +
 >> +struct stm32_rtc {
 >> +    struct rtc_device *rtc_dev;
 >> +    void __iomem *base;
 >> +    struct clk *pclk;
 >> +    struct clk *ck_rtc;
 >> +    unsigned int clksrc;
 >> +    spinlock_t lock; /* Protects registers accesses */
 >> +    int irq_alarm;
 >> +    struct regmap *pwrcr;
 >> +};
 >> +
 >> +static inline unsigned int stm32_rtc_readl(struct stm32_rtc *rtc,
 >> +                       unsigned int offset)
 >> +{
 >> +    return readl_relaxed(rtc->base + offset);
 >> +}
 >> +
 >> +static inline void stm32_rtc_writel(struct stm32_rtc *rtc,
 >> +                    unsigned int offset, unsigned int value)
 >> +{
 >> +    writel_relaxed(value, rtc->base + offset);
 >> +}
 >
 > I'm not sure wrapping the readl/writel_relaxed function does anything 
special
 > other than simply redirecting the reader to another section of the code.
During development phase, it is useful to add debug traces but you're 
right, this can be remove.
 >
 >> +
 >> +static void stm32_rtc_wpr_unlock(struct stm32_rtc *rtc)
 >> +{
 >> +//    if (dbp)
 >> +//        regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, PWR_CR_DBP);
 >
 > Did checkpatch let you get away with this?  What did you intend to do 
here?
Hum, as surprising as it may seem, checkpatch didn't complained about 
these comments! But anyway, this has to be removed, it was a tentative 
to enable/disable backup domain write protection any time we have to 
write in a protected RTC register, but it is not functionnal. I have 
commented this just to keep it in mind and forget to remove it before 
sending.
 >
 >> +
 >> +    stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_1ST_KEY);
 >> +    stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_2ND_KEY);
 >> +}
 >> +
 >> +static void stm32_rtc_wpr_lock(struct stm32_rtc *rtc)
 >> +{
 >> +    stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_WRONG_KEY);
 >> +
 >> +//    if (dbp)
 >> +//        regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
 >> +}
 >> +
 >> +static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc)
 >> +{
 >> +    unsigned int isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +
 >> +    if (!(isr & STM32_RTC_ISR_INITF)) {
 >> +        isr |= STM32_RTC_ISR_INIT;
 >> +        stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
 >> +
 >> +        return readl_relaxed_poll_timeout_atomic(
 >> +                    rtc->base + STM32_RTC_ISR,
 >> +                    isr, (isr & STM32_RTC_ISR_INITF),
 >> +                    10, 100000);
 >
 > When using hard coded numerics please add comments that explains the 
reason
 > behind the selected values.
Sure. It takes around 2 RTCCLK clock cycles to enter in initialization 
phase mode. So it depends on the frequency of the ck_rtc parent clock.
Either I keep parent clock frequency and compute the exact timeout, or I 
use the "best and worst cases": slowest RTCCLK frequency is 32kHz, so it 
can take up to 62us, highest RTCCLK frequency should be 1MHz, so it can 
take only 2us. Polling every 10us with a timeout of 100ms seemed 
reasonable and be a good compromise.
 >
 >> +    }
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +static void stm32_rtc_exit_init_mode(struct stm32_rtc *rtc)
 >> +{
 >> +    unsigned int isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +
 >> +    isr &= ~STM32_RTC_ISR_INIT;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
 >> +}
 >> +
 >> +static int stm32_rtc_wait_sync(struct stm32_rtc *rtc)
 >> +{
 >> +    unsigned int isr;
 >> +
 >> +    isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +
 >> +    isr &= ~STM32_RTC_ISR_RSF;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
 >> +
 >> +    /* Wait the registers to be synchronised */
 >> +    return readl_relaxed_poll_timeout_atomic(rtc->base + STM32_RTC_ISR,
 >> +                         isr,
 >> +                         (isr & STM32_RTC_ISR_RSF),
 >> +                         10, 100000);
 >
 > Shouldn't the break condition be !((isr & STM32_RTC_ISR_RSF) ?  If 
not this
 > probably deserve a better comment.
RSF bit is set by hardware each time the calendar registers are 
synchronized (it takes up to 2 RTCCLK). So the break condition is 
correct: we poll until RSF flag is set or timeout is reached.
 >
 >> +}
 >> +
 >> +static irqreturn_t stm32_rtc_alarm_irq(int irq, void *dev_id)
 >> +{
 >> +    struct stm32_rtc *rtc = (struct stm32_rtc *)dev_id;
 >> +    unsigned long irqflags, events = 0;
 >> +    unsigned int isr, cr;
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >> +
 >> +    if ((isr & STM32_RTC_ISR_ALRAF) &&
 >> +        (cr & STM32_RTC_CR_ALRAIE)) {
 >> +        /* Alarm A flag - Alarm interrupt */
 >> +        events |= RTC_IRQF | RTC_AF;
 >> +        isr &= ~STM32_RTC_ISR_ALRAF;
 >> +    }
 >> +
 >> +    /* Clear event irqflags, otherwise new events won't be received */
 >> +    stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    if (events) {
 >> +        dev_info(&rtc->rtc_dev->dev, "Alarm occurred\n");
 >> +
 >> +        /* Pass event to the kernel */
 >> +        rtc_update_irq(rtc->rtc_dev, 1, events);
 >> +        return IRQ_HANDLED;
 >> +    } else {
 >> +        return IRQ_NONE;
 >> +    }
 >> +}
 >> +
 >> +/* Convert rtc_time structure from bin to bcd format */
 >> +static void tm2bcd(struct rtc_time *tm)
 >> +{
 >> +    tm->tm_sec = bin2bcd(tm->tm_sec);
 >> +    tm->tm_min = bin2bcd(tm->tm_min);
 >> +    tm->tm_hour = bin2bcd(tm->tm_hour);
 >> +
 >> +    tm->tm_mday = bin2bcd(tm->tm_mday);
 >> +    tm->tm_mon = bin2bcd(tm->tm_mon + 1);
 >> +    tm->tm_year = bin2bcd(tm->tm_year - 100);
 >> +    /*
 >> +     * Number of days since Sunday
 >> +     * - on kernel side, 0=Sunday...6=Saturday
 >> +     * - on rtc side, 0=invalid,1=Monday...7=Sunday
 >> +     */
 >> +    tm->tm_wday = (!tm->tm_wday) ? 7 : tm->tm_wday;
 >> +}
 >> +
 >> +/* Convert rtc_time structure from bcd to bin format */
 >> +static void bcd2tm(struct rtc_time *tm)
 >> +{
 >> +    tm->tm_sec = bcd2bin(tm->tm_sec);
 >> +    tm->tm_min = bcd2bin(tm->tm_min);
 >> +    tm->tm_hour = bcd2bin(tm->tm_hour);
 >> +
 >> +    tm->tm_mday = bcd2bin(tm->tm_mday);
 >> +    tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
 >> +    tm->tm_year = bcd2bin(tm->tm_year) + 100;
 >> +    /*
 >> +     * Number of days since Sunday
 >> +     * - on kernel side, 0=Sunday...6=Saturday
 >> +     * - on rtc side, 0=invalid,1=Monday...7=Sunday
 >> +     */
 >> +    tm->tm_wday %= 7;
 >> +}
 >> +
 >> +static int stm32_rtc_read_time(struct device *dev, struct rtc_time *tm)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    unsigned int tr, dr;
 >> +    unsigned long irqflags;
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    /* Time and Date in BCD format */
 >> +    tr = stm32_rtc_readl(rtc, STM32_RTC_TR);
 >> +    dr = stm32_rtc_readl(rtc, STM32_RTC_DR);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    tm->tm_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
 >> +    tm->tm_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
 >> +    tm->tm_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
 >> +
 >> +    tm->tm_mday = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
 >> +    tm->tm_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
 >> +    tm->tm_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
 >> +    tm->tm_wday = (dr & STM32_RTC_DR_WDAY) >> STM32_RTC_DR_WDAY_SHIFT;
 >> +
 >> +    /* We don't report tm_yday and tm_isdst */
 >> +
 >> +    bcd2tm(tm);
 >> +
 >> +    if (rtc_valid_tm(tm) < 0) {
 >> +        dev_err(dev, "%s: rtc_time is not valid.\n", __func__);
 >> +        return -EINVAL;
 >> +    }
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +static int stm32_rtc_set_time(struct device *dev, struct rtc_time *tm)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    unsigned int tr, dr;
 >> +    unsigned long irqflags;
 >> +    int ret = 0;
 >> +
 >> +    if (rtc_valid_tm(tm) < 0) {
 >> +        dev_err(dev, "%s: rtc_time is not valid.\n", __func__);
 >> +        return -EINVAL;
 >> +    }
 >> +
 >> +    tm2bcd(tm);
 >> +
 >> +    /* Time in BCD format */
 >> +    tr = ((tm->tm_sec << STM32_RTC_TR_SEC_SHIFT) & STM32_RTC_TR_SEC) |
 >> +         ((tm->tm_min << STM32_RTC_TR_MIN_SHIFT) & STM32_RTC_TR_MIN) |
 >> +         ((tm->tm_hour << STM32_RTC_TR_HOUR_SHIFT) & 
STM32_RTC_TR_HOUR);
 >> +
 >> +    /* Date in BCD format */
 >> +    dr = ((tm->tm_mday << STM32_RTC_DR_DATE_SHIFT) & 
STM32_RTC_DR_DATE) |
 >> +         ((tm->tm_mon << STM32_RTC_DR_MONTH_SHIFT) & 
STM32_RTC_DR_MONTH) |
 >> +         ((tm->tm_year << STM32_RTC_DR_YEAR_SHIFT) & 
STM32_RTC_DR_YEAR) |
 >> +         ((tm->tm_wday << STM32_RTC_DR_WDAY_SHIFT) & 
STM32_RTC_DR_WDAY);
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    stm32_rtc_wpr_unlock(rtc);
 >> +
 >> +    ret = stm32_rtc_enter_init_mode(rtc);
 >> +    if (ret) {
 >> +        dev_err(dev, "Can't enter in init mode. Set time aborted.\n");
 >> +        goto end;
 >> +    }
 >> +
 >> +    stm32_rtc_writel(rtc, STM32_RTC_TR, tr);
 >> +    stm32_rtc_writel(rtc, STM32_RTC_DR, dr);
 >> +
 >> +    stm32_rtc_exit_init_mode(rtc);
 >> +
 >> +    ret = stm32_rtc_wait_sync(rtc);
 >> +end:
 >> +    stm32_rtc_wpr_lock(rtc);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    return ret;
 >> +}
 >> +
 >> +static int stm32_rtc_read_alarm(struct device *dev, struct 
rtc_wkalrm *alrm)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    struct rtc_time *tm = &alrm->time;
 >> +    unsigned int alrmar, cr, isr;
 >> +    unsigned long irqflags;
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    alrmar = stm32_rtc_readl(rtc, STM32_RTC_ALRMAR);
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >> +    isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    if (alrmar & STM32_RTC_ALRMXR_DATE_MASK) {
 >> +        /*
 >> +         * Date/day don't care in Alarm comparison so alarm triggers
 >> +         * every day
 >> +         */
 >> +        tm->tm_mday = -1;
 >> +        tm->tm_wday = -1;
 >> +    } else {
 >> +        if (alrmar & STM32_RTC_ALRMXR_WDSEL) {
 >> +            /* Alarm is set to a day of week */
 >> +            tm->tm_mday = -1;
 >> +            tm->tm_wday = (alrmar & STM32_RTC_ALRMXR_WDAY) >>
 >> +                      STM32_RTC_ALRMXR_WDAY_SHIFT;
 >> +            tm->tm_wday %= 7;
 >> +        } else {
 >> +            /* Alarm is set to a day of month */
 >> +            tm->tm_wday = -1;
 >> +            tm->tm_mday = (alrmar & STM32_RTC_ALRMXR_DATE) >>
 >> +                       STM32_RTC_ALRMXR_DATE_SHIFT;
 >> +        }
 >> +    }
 >> +
 >> +    if (alrmar & STM32_RTC_ALRMXR_HOUR_MASK) {
 >> +        /* Hours don't care in Alarm comparison */
 >> +        tm->tm_hour = -1;
 >> +    } else {
 >> +        tm->tm_hour = (alrmar & STM32_RTC_ALRMXR_HOUR) >>
 >> +                   STM32_RTC_ALRMXR_HOUR_SHIFT;
 >> +        if (alrmar & STM32_RTC_ALRMXR_PM)
 >> +            tm->tm_hour += 12;
 >> +    }
 >> +
 >> +    if (alrmar & STM32_RTC_ALRMXR_MIN_MASK) {
 >> +        /* Minutes don't care in Alarm comparison */
 >> +        tm->tm_min = -1;
 >> +    } else {
 >> +        tm->tm_min = (alrmar & STM32_RTC_ALRMXR_MIN) >>
 >> +                  STM32_RTC_ALRMXR_MIN_SHIFT;
 >> +    }
 >> +
 >> +    if (alrmar & STM32_RTC_ALRMXR_SEC_MASK) {
 >> +        /* Seconds don't care in Alarm comparison */
 >> +        tm->tm_sec = -1;
 >> +    } else {
 >> +        tm->tm_sec = (alrmar & STM32_RTC_ALRMXR_SEC) >>
 >> +                  STM32_RTC_ALRMXR_SEC_SHIFT;
 >> +    }
 >> +
 >> +    bcd2tm(tm);
 >> +
 >> +    alrm->enabled = (cr & STM32_RTC_CR_ALRAE) ? 1 : 0;
 >> +    alrm->pending = (isr & STM32_RTC_ISR_ALRAF) ? 1 : 0;
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned 
int enabled)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    unsigned long irqflags;
 >> +    unsigned int isr, cr;
 >> +
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >
 > Is the STM32_RTC_CR garanteed to be valid, i.e updated atomically? 
If not this
 > should probably be below the spinlock.
You're right.
 >
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    stm32_rtc_wpr_unlock(rtc);
 >> +
 >> +    /* We expose Alarm A to the kernel */
 >> +    if (enabled)
 >> +        cr |= (STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
 >> +    else
 >> +        cr &= ~(STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
 >> +    stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
 >> +
 >> +    /* Clear event irqflags, otherwise new events won't be received */
 >> +    isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
 >> +    isr &= ~STM32_RTC_ISR_ALRAF;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
 >> +
 >> +    stm32_rtc_wpr_lock(rtc);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +static int stm32_rtc_set_alarm(struct device *dev, struct 
rtc_wkalrm *alrm)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    struct rtc_time *tm = &alrm->time;
 >> +    unsigned long irqflags;
 >> +    unsigned int cr, isr, alrmar;
 >> +    int ret = 0;
 >> +
 >> +    if (rtc_valid_tm(tm)) {
 >> +        dev_err(dev, "Alarm time not valid.\n");
 >> +        return -EINVAL;
 >> +    }
 >> +
 >> +    tm2bcd(tm);
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    stm32_rtc_wpr_unlock(rtc);
 >> +
 >> +    /* Disable Alarm */
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >> +    cr &= ~STM32_RTC_CR_ALRAE;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
 >> +
 >> +    /* Poll Alarm write flag to be sure that Alarm update is allowed */
 >> +    ret = readl_relaxed_poll_timeout_atomic(rtc->base + STM32_RTC_ISR,
 >> +                        isr,
 >> +                        (isr & STM32_RTC_ISR_ALRAWF),
 >> +                        10, 100);
 >> +
 >> +    if (ret) {
 >> +        dev_err(dev, "Alarm update not allowed\n");
 >> +        goto end;
 >> +    }
 >> +
 >> +    alrmar = 0;
 >> +
 >> +    if (tm->tm_mday < 0 && tm->tm_wday < 0) {
 >> +        /*
 >> +         * Date/day don't care in Alarm comparison so alarm triggers
 >> +         * every day
 >> +         */
 >> +        alrmar |= STM32_RTC_ALRMXR_DATE_MASK;
 >> +    } else {
 >> +        if (tm->tm_mday > 0) {
 >> +            /* Date is selected (ignoring wday) */
 >> +            alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &
 >> +                  STM32_RTC_ALRMXR_DATE;
 >> +        } else {
 >> +            /* Day of week is selected */
 >> +            int wday = (tm->tm_wday == 0) ? 7 : tm->tm_wday;
 >> +
 >> +            alrmar |= STM32_RTC_ALRMXR_WDSEL;
 >> +            alrmar |= (wday << STM32_RTC_ALRMXR_WDAY_SHIFT) &
 >> +                  STM32_RTC_ALRMXR_WDAY;
 >> +        }
 >> +    }
 >> +
 >> +    if (tm->tm_hour < 0) {
 >> +        /* Hours don't care in Alarm comparison */
 >> +        alrmar |= STM32_RTC_ALRMXR_HOUR_MASK;
 >> +    } else {
 >> +        /* 24-hour format */
 >> +        alrmar &= ~STM32_RTC_ALRMXR_PM;
 >> +        alrmar |= (tm->tm_hour << STM32_RTC_ALRMXR_HOUR_SHIFT) &
 >> +              STM32_RTC_ALRMXR_HOUR;
 >> +    }
 >> +
 >> +    if (tm->tm_min < 0) {
 >> +        /* Minutes don't care in Alarm comparison */
 >> +        alrmar |= STM32_RTC_ALRMXR_MIN_MASK;
 >> +    } else {
 >> +        alrmar |= (tm->tm_min << STM32_RTC_ALRMXR_MIN_SHIFT) &
 >> +              STM32_RTC_ALRMXR_MIN;
 >> +    }
 >> +
 >> +    if (tm->tm_sec < 0) {
 >> +        /* Seconds don't care in Alarm comparison */
 >> +        alrmar |= STM32_RTC_ALRMXR_SEC_MASK;
 >> +    } else {
 >> +        alrmar |= (tm->tm_sec << STM32_RTC_ALRMXR_SEC_SHIFT) &
 >> +              STM32_RTC_ALRMXR_SEC;
 >> +    }
 >> +
 >> +    /* Write to Alarm register */
 >> +    stm32_rtc_writel(rtc, STM32_RTC_ALRMAR, alrmar);
 >> +
 >> +    if (alrm->enabled)
 >> +        stm32_rtc_alarm_irq_enable(dev, 1);
 >> +    else
 >> +        stm32_rtc_alarm_irq_enable(dev, 0);
 >> +
 >> +end:
 >> +    stm32_rtc_wpr_lock(rtc);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    return ret;
 >> +}
 >> +
 >> +static const struct rtc_class_ops stm32_rtc_ops = {
 >> +    .read_time    = stm32_rtc_read_time,
 >> +    .set_time    = stm32_rtc_set_time,
 >> +    .read_alarm    = stm32_rtc_read_alarm,
 >> +    .set_alarm    = stm32_rtc_set_alarm,
 >> +    .alarm_irq_enable = stm32_rtc_alarm_irq_enable,
 >> +};
 >> +
 >> +#ifdef CONFIG_OF
 >> +static const struct of_device_id stm32_rtc_of_match[] = {
 >> +    { .compatible = "st,stm32-rtc" },
 >> +    {}
 >> +};
 >> +MODULE_DEVICE_TABLE(of, stm32_rtc_of_match);
 >> +#endif
 >> +
 >> +static int stm32_rtc_init(struct platform_device *pdev,
 >> +              struct stm32_rtc *rtc)
 >> +{
 >> +    unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr;
 >> +    unsigned int rate;
 >> +    unsigned long irqflags;
 >> +    int ret = 0;
 >> +
 >> +    rate = clk_get_rate(rtc->ck_rtc);
 >> +
 >> +    /* Find prediv_a and prediv_s to obtain the 1Hz calendar clock */
 >> +    pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT;
 >> +    pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT;
 >> +
 >> +    for (pred_a = pred_a_max; pred_a >= 0; pred_a--) {
 >> +        pred_s = (rate / (pred_a + 1)) - 1;
 >> +
 >> +        if (((pred_s + 1) * (pred_a + 1)) == rate)
 >> +            break;
 >> +    }
 >> +
 >> +    /*
 >> +     * Can't find a 1Hz, so give priority to RTC power consumption
 >> +     * by choosing the higher possible value for prediv_a
 >> +     */
 >> +    if ((pred_s > pred_s_max) || (pred_a > pred_a_max)) {
 >> +        pred_a = pred_a_max;
 >> +        pred_s = (rate / (pred_a + 1)) - 1;
 >> +
 >> +        dev_warn(&pdev->dev, "ck_rtc is %s\n",
 >> +             (rate - ((pred_a + 1) * (pred_s + 1)) < 0) ?
 >> +             "fast" : "slow");
 >> +    }
 >> +
 >> +    spin_lock_irqsave(&rtc->lock, irqflags);
 >> +
 >> +    stm32_rtc_wpr_unlock(rtc);
 >> +
 >> +    ret = stm32_rtc_enter_init_mode(rtc);
 >> +    if (ret) {
 >> +        dev_err(&pdev->dev,
 >> +            "Can't enter in init mode. Prescaler config failed.\n");
 >> +        goto end;
 >> +    }
 >> +
 >> +    prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & 
STM32_RTC_PRER_PRED_S;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_PRER, prer);
 >> +    prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & 
STM32_RTC_PRER_PRED_A;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_PRER, prer);
 >> +
 >> +    /* Force 24h time format */
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >> +    cr &= ~STM32_RTC_CR_FMT;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
 >> +
 >> +    stm32_rtc_exit_init_mode(rtc);
 >> +
 >> +    ret = stm32_rtc_wait_sync(rtc);
 >> +
 >> +    if (stm32_rtc_readl(rtc, STM32_RTC_ISR) & STM32_RTC_ISR_INITS)
 >> +        dev_warn(&pdev->dev, "Date/Time must be initialized\n");
 >> +end:
 >> +    stm32_rtc_wpr_lock(rtc);
 >> +
 >> +    spin_unlock_irqrestore(&rtc->lock, irqflags);
 >> +
 >> +    return ret;
 >> +}
 >> +
 >> +static int stm32_rtc_probe(struct platform_device *pdev)
 >> +{
 >> +    struct stm32_rtc *rtc;
 >> +    struct resource *res;
 >> +    int ret;
 >> +
 >> +    rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
 >> +    if (!rtc)
 >> +        return -ENOMEM;
 >> +
 >> +    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 >
 > The value of 'res' should be checked before using it.
res is checked in devm_ioremap_resource just below :
     if (!res || resource_type(res) != IORESOURCE_MEM) {
         dev_err(dev, "invalid resource\n");
         return IOMEM_ERR_PTR(-EINVAL);
     }
That's why it is not checked here.
 >
 >> +    rtc->base = devm_ioremap_resource(&pdev->dev, res);
 >> +    if (IS_ERR(rtc->base))
 >> +        return PTR_ERR(rtc->base);
 >> +
 >> +    dbp = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, 
"st,syscfg");
 >> +    if (IS_ERR(dbp)) {
 >> +        dev_err(&pdev->dev, "no st,syscfg\n");
 >> +        return PTR_ERR(dbp);
 >> +    }
 >> +
 >> +    spin_lock_init(&rtc->lock);
 >> +
 >> +    rtc->ck_rtc = devm_clk_get(&pdev->dev, "ck_rtc");
 >> +    if (IS_ERR(rtc->ck_rtc)) {
 >> +        dev_err(&pdev->dev, "no ck_rtc clock");
 >> +        return PTR_ERR(rtc->ck_rtc);
 >> +    }
 >> +
 >> +    ret = clk_prepare_enable(rtc->ck_rtc);
 >> +    if (ret)
 >> +        return ret;
 >> +
 >> +    if (dbp)
 >> +        regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, PWR_CR_DBP);
 >
 > The code above exits if there is a problem with the dbp, there is no 
point in
 > checking again.
You're right.
 >
 >> +
 >> +    ret = stm32_rtc_init(pdev, rtc);
 >> +    if (ret)
 >> +        goto err;
 >> +
 >> +    rtc->irq_alarm = platform_get_irq_byname(pdev, "alarm");
 >> +    if (rtc->irq_alarm <= 0) {
 >> +        dev_err(&pdev->dev, "no alarm irq\n");
 >> +        ret = -ENOENT;
 >> +        goto err;
 >> +    }
 >> +
 >> +    platform_set_drvdata(pdev, rtc);
 >> +
 >> +    device_init_wakeup(&pdev->dev, true);
 >
 > What happens if device_init_wakeup() returns an error?
It means that RTC won't be able to wake up the board with RTC alarm. I 
can add a warning for the user in this case ?
 >
 >> +
 >> +    rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
 >> +            &stm32_rtc_ops, THIS_MODULE);
 >> +    if (IS_ERR(rtc->rtc_dev)) {
 >> +        ret = PTR_ERR(rtc->rtc_dev);
 >> +        dev_err(&pdev->dev, "rtc device registration failed, err=%d\n",
 >> +            ret);
 >> +        goto err;
 >> +    }
 >> +
 >> +    /* Handle RTC alarm interrupts */
 >> +    ret = devm_request_irq(&pdev->dev, rtc->irq_alarm,
 >> +                   stm32_rtc_alarm_irq, IRQF_TRIGGER_RISING,
 >> +                   dev_name(&rtc->rtc_dev->dev), rtc);
 >> +    if (ret) {
 >> +        dev_err(&pdev->dev, "IRQ%d (alarm interrupt) already 
claimed\n",
 >> +            rtc->irq_alarm);
 >> +        goto err;
 >> +    }
 >> +
 >> +    return 0;
 >> +err:
 >> +    clk_disable_unprepare(rtc->ck_rtc);
 >> +
 >> +    if (dbp)
 >> +        regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
 >
 > Same comment as above.
OK.
 >
 >> +
 >> +    device_init_wakeup(&pdev->dev, false);
 >> +
 >> +    return ret;
 >> +}
 >> +
 >> +static int __exit stm32_rtc_remove(struct platform_device *pdev)
 >> +{
 >> +    struct stm32_rtc *rtc = platform_get_drvdata(pdev);
 >> +    unsigned int cr;
 >> +
 >> +    /* Disable interrupts */
 >> +    stm32_rtc_wpr_unlock(rtc);
 >> +    cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
 >> +    cr &= ~STM32_RTC_CR_ALRAIE;
 >> +    stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
 >> +    stm32_rtc_wpr_lock(rtc);
 >> +
 >> +    clk_disable_unprepare(rtc->ck_rtc);
 >> +
 >> +    /* Enable backup domain write protection */
 >> +    if (dbp)
 >> +        regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
 >> +
 >> +    device_init_wakeup(&pdev->dev, false);
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +#ifdef CONFIG_PM_SLEEP
 >> +static int stm32_rtc_suspend(struct device *dev)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +
 >> +    if (device_may_wakeup(dev))
 >> +        return enable_irq_wake(rtc->irq_alarm);
 >> +
 >> +    return 0;
 >> +}
 >> +
 >> +static int stm32_rtc_resume(struct device *dev)
 >> +{
 >> +    struct stm32_rtc *rtc = dev_get_drvdata(dev);
 >> +    int ret = 0;
 >> +
 >> +    ret = stm32_rtc_wait_sync(rtc);
 >> +    if (ret < 0)
 >> +        return ret;
 >> +
 >> +    if (device_may_wakeup(dev))
 >> +        return disable_irq_wake(rtc->irq_alarm);
 >> +
 >> +    return ret;
 >> +}
 >> +#endif
 >> +
 >> +static SIMPLE_DEV_PM_OPS(stm32_rtc_pm_ops,
 >> +             stm32_rtc_suspend, stm32_rtc_resume);
 >> +
 >> +static struct platform_driver stm32_rtc_driver = {
 >> +    .probe        = stm32_rtc_probe,
 >> +    .remove        = stm32_rtc_remove,
 >> +    .driver        = {
 >> +        .name    = DRIVER_NAME,
 >> +        .pm    = &stm32_rtc_pm_ops,
 >> +        .of_match_table = stm32_rtc_of_match,
 >> +    },
 >> +};
 >> +
 >> +module_platform_driver(stm32_rtc_driver);
 >> +
 >> +MODULE_ALIAS("platform:" DRIVER_NAME);
 >> +MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
 >> +MODULE_DESCRIPTION("STMicroelectronics STM32 Real Time Clock driver");
 >> +MODULE_LICENSE("GPL v2");
 >> --
 >> 1.9.1
 >>

Best regards,
Amelie

^ permalink raw reply

* [PATCH 3/8] rtc: add STM32 RTC driver
From: Amelie DELAUNAY @ 2016-12-05  9:45 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161202180527.GB3284@linaro.org>

On 12/02/2016 07:05 PM, Mathieu Poirier wrote:
> On Fri, Dec 02, 2016 at 03:09:56PM +0100, Amelie Delaunay wrote:
>> This patch adds support for the STM32 RTC.
>>
>> Signed-off-by: Amelie Delaunay <amelie.delaunay@st.com>
>> ---
>>  drivers/rtc/Kconfig     |  10 +
>>  drivers/rtc/Makefile    |   1 +
>>  drivers/rtc/rtc-stm32.c | 777 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 788 insertions(+)
>>  create mode 100644 drivers/rtc/rtc-stm32.c
>>
>> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
>> index e859d14..dd8b218 100644
>> --- a/drivers/rtc/Kconfig
>> +++ b/drivers/rtc/Kconfig
>> @@ -1706,6 +1706,16 @@ config RTC_DRV_PIC32
>>  	   This driver can also be built as a module. If so, the module
>>  	   will be called rtc-pic32
>>
>> +config RTC_DRV_STM32
>> +	tristate "STM32 On-Chip RTC"
>> +	depends on ARCH_STM32
>> +	help
>> +	   If you say yes here you get support for the STM32 On-Chip
>> +	   Real Time Clock.
>> +
>> +	   This driver can also be built as a module, if so, the module
>> +	   will be called "rtc-stm32".
>> +
>>  comment "HID Sensor RTC drivers"
>>
>>  config RTC_DRV_HID_SENSOR_TIME
>> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
>> index 1ac694a..87bd9cc 100644
>> --- a/drivers/rtc/Makefile
>> +++ b/drivers/rtc/Makefile
>> @@ -144,6 +144,7 @@ obj-$(CONFIG_RTC_DRV_SNVS)	+= rtc-snvs.o
>>  obj-$(CONFIG_RTC_DRV_SPEAR)	+= rtc-spear.o
>>  obj-$(CONFIG_RTC_DRV_STARFIRE)	+= rtc-starfire.o
>>  obj-$(CONFIG_RTC_DRV_STK17TA8)	+= rtc-stk17ta8.o
>> +obj-$(CONFIG_RTC_DRV_STM32) 	+= rtc-stm32.o
>>  obj-$(CONFIG_RTC_DRV_STMP)	+= rtc-stmp3xxx.o
>>  obj-$(CONFIG_RTC_DRV_ST_LPC)	+= rtc-st-lpc.o
>>  obj-$(CONFIG_RTC_DRV_SUN4V)	+= rtc-sun4v.o
>> diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c
>> new file mode 100644
>> index 0000000..9e710ff
>> --- /dev/null
>> +++ b/drivers/rtc/rtc-stm32.c
>> @@ -0,0 +1,777 @@
>> +/*
>> + * Copyright (C) Amelie Delaunay 2015
>> + * Author:  Amelie Delaunay <adelaunay.stm32@gmail.com>
>> + * License terms:  GNU General Public License (GPL), version 2
>> + */
>> +
>> +#include <linux/bcd.h>
>> +#include <linux/clk.h>
>> +#include <linux/init.h>
>> +#include <linux/io.h>
>> +#include <linux/iopoll.h>
>> +#include <linux/ioport.h>
>> +#include <linux/kernel.h>
>> +#include <linux/mfd/syscon.h>
>> +#include <linux/module.h>
>> +#include <linux/of.h>
>> +#include <linux/of_device.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/regmap.h>
>> +#include <linux/rtc.h>
>> +#include <linux/spinlock.h>
>> +
>> +#define DRIVER_NAME "stm32_rtc"
>> +
>> +/* STM32 RTC registers */
>> +#define STM32_RTC_TR		0x00
>> +#define STM32_RTC_DR		0x04
>> +#define STM32_RTC_CR		0x08
>> +#define STM32_RTC_ISR		0x0C
>> +#define STM32_RTC_PRER		0x10
>> +#define STM32_RTC_ALRMAR	0x1C
>> +#define STM32_RTC_WPR		0x24
>> +
>> +/* STM32_RTC_TR bit fields  */
>> +#define STM32_RTC_TR_SEC_SHIFT		0
>> +#define STM32_RTC_TR_SEC		GENMASK(6, 0)
>> +#define STM32_RTC_TR_MIN_SHIFT		8
>> +#define STM32_RTC_TR_MIN		GENMASK(14, 8)
>> +#define STM32_RTC_TR_HOUR_SHIFT		16
>> +#define STM32_RTC_TR_HOUR		GENMASK(21, 16)
>> +
>> +/* STM32_RTC_DR bit fields */
>> +#define STM32_RTC_DR_DATE_SHIFT		0
>> +#define STM32_RTC_DR_DATE		GENMASK(5, 0)
>> +#define STM32_RTC_DR_MONTH_SHIFT	8
>> +#define STM32_RTC_DR_MONTH		GENMASK(11, 8)
>> +#define STM32_RTC_DR_WDAY_SHIFT		13
>> +#define STM32_RTC_DR_WDAY		GENMASK(15, 13)
>> +#define STM32_RTC_DR_YEAR_SHIFT		16
>> +#define STM32_RTC_DR_YEAR		GENMASK(23, 16)
>> +
>> +/* STM32_RTC_CR bit fields */
>> +#define STM32_RTC_CR_FMT		BIT(6)
>> +#define STM32_RTC_CR_ALRAE		BIT(8)
>> +#define STM32_RTC_CR_ALRAIE		BIT(12)
>> +
>> +/* STM32_RTC_ISR bit fields */
>> +#define STM32_RTC_ISR_ALRAWF		BIT(0)
>> +#define STM32_RTC_ISR_INITS		BIT(4)
>> +#define STM32_RTC_ISR_RSF		BIT(5)
>> +#define STM32_RTC_ISR_INITF		BIT(6)
>> +#define STM32_RTC_ISR_INIT		BIT(7)
>> +#define STM32_RTC_ISR_ALRAF		BIT(8)
>> +
>> +/* STM32_RTC_PRER bit fields */
>> +#define STM32_RTC_PRER_PRED_S_SHIFT	0
>> +#define STM32_RTC_PRER_PRED_S		GENMASK(14, 0)
>> +#define STM32_RTC_PRER_PRED_A_SHIFT	16
>> +#define STM32_RTC_PRER_PRED_A		GENMASK(22, 16)
>> +
>> +/* STM32_RTC_ALRMAR and STM32_RTC_ALRMBR bit fields */
>> +#define STM32_RTC_ALRMXR_SEC_SHIFT	0
>> +#define STM32_RTC_ALRMXR_SEC		GENMASK(6, 0)
>> +#define STM32_RTC_ALRMXR_SEC_MASK	BIT(7)
>> +#define STM32_RTC_ALRMXR_MIN_SHIFT	8
>> +#define STM32_RTC_ALRMXR_MIN		GENMASK(14, 8)
>> +#define STM32_RTC_ALRMXR_MIN_MASK	BIT(15)
>> +#define STM32_RTC_ALRMXR_HOUR_SHIFT	16
>> +#define STM32_RTC_ALRMXR_HOUR		GENMASK(21, 16)
>> +#define STM32_RTC_ALRMXR_PM		BIT(22)
>> +#define STM32_RTC_ALRMXR_HOUR_MASK	BIT(23)
>> +#define STM32_RTC_ALRMXR_DATE_SHIFT	24
>> +#define STM32_RTC_ALRMXR_DATE		GENMASK(29, 24)
>> +#define STM32_RTC_ALRMXR_WDSEL		BIT(30)
>> +#define STM32_RTC_ALRMXR_WDAY_SHIFT	24
>> +#define STM32_RTC_ALRMXR_WDAY		GENMASK(27, 24)
>> +#define STM32_RTC_ALRMXR_DATE_MASK	BIT(31)
>> +
>> +/* STM32_RTC_WPR key constants */
>> +#define RTC_WPR_1ST_KEY			0xCA
>> +#define RTC_WPR_2ND_KEY			0x53
>> +#define RTC_WPR_WRONG_KEY		0xFF
>> +
>> +/*
>> + * RTC registers are protected agains parasitic write access.
>> + * PWR_CR_DBP bit must be set to enable write access to RTC registers.
>> + */
>> +/* STM32_PWR_CR */
>> +#define PWR_CR				0x00
>> +/* STM32_PWR_CR bit field */
>> +#define PWR_CR_DBP			BIT(8)
>> +
>> +static struct regmap *dbp;
>> +
>> +struct stm32_rtc {
>> +	struct rtc_device *rtc_dev;
>> +	void __iomem *base;
>> +	struct clk *pclk;
>> +	struct clk *ck_rtc;
>> +	unsigned int clksrc;
>> +	spinlock_t lock; /* Protects registers accesses */
>> +	int irq_alarm;
>> +	struct regmap *pwrcr;
>> +};
>
> One more thing... What did you want to do with pclk, clksrc and pwrcr?  They
> aren't used in the driver.  If this is for future enhancement I suggest you
> introduce those when you submit the patches.
You're right.
>
>> +
>> +static inline unsigned int stm32_rtc_readl(struct stm32_rtc *rtc,
>> +					   unsigned int offset)
>> +{
>> +	return readl_relaxed(rtc->base + offset);
>> +}
>> +
>> +static inline void stm32_rtc_writel(struct stm32_rtc *rtc,
>> +				    unsigned int offset, unsigned int value)
>> +{
>> +	writel_relaxed(value, rtc->base + offset);
>> +}
>> +
>> +static void stm32_rtc_wpr_unlock(struct stm32_rtc *rtc)
>> +{
>> +//	if (dbp)
>> +//		regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, PWR_CR_DBP);
>> +
>> +	stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_1ST_KEY);
>> +	stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_2ND_KEY);
>> +}
>> +
>> +static void stm32_rtc_wpr_lock(struct stm32_rtc *rtc)
>> +{
>> +	stm32_rtc_writel(rtc, STM32_RTC_WPR, RTC_WPR_WRONG_KEY);
>> +
>> +//	if (dbp)
>> +//		regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
>> +}
>> +
>> +static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc)
>> +{
>> +	unsigned int isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +
>> +	if (!(isr & STM32_RTC_ISR_INITF)) {
>> +		isr |= STM32_RTC_ISR_INIT;
>> +		stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
>> +
>> +		return readl_relaxed_poll_timeout_atomic(
>> +					rtc->base + STM32_RTC_ISR,
>> +					isr, (isr & STM32_RTC_ISR_INITF),
>> +					10, 100000);
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static void stm32_rtc_exit_init_mode(struct stm32_rtc *rtc)
>> +{
>> +	unsigned int isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +
>> +	isr &= ~STM32_RTC_ISR_INIT;
>> +	stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
>> +}
>> +
>> +static int stm32_rtc_wait_sync(struct stm32_rtc *rtc)
>> +{
>> +	unsigned int isr;
>> +
>> +	isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +
>> +	isr &= ~STM32_RTC_ISR_RSF;
>> +	stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
>> +
>> +	/* Wait the registers to be synchronised */
>> +	return readl_relaxed_poll_timeout_atomic(rtc->base + STM32_RTC_ISR,
>> +						 isr,
>> +						 (isr & STM32_RTC_ISR_RSF),
>> +						 10, 100000);
>> +}
>> +
>> +static irqreturn_t stm32_rtc_alarm_irq(int irq, void *dev_id)
>> +{
>> +	struct stm32_rtc *rtc = (struct stm32_rtc *)dev_id;
>> +	unsigned long irqflags, events = 0;
>> +	unsigned int isr, cr;
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +
>> +	if ((isr & STM32_RTC_ISR_ALRAF) &&
>> +	    (cr & STM32_RTC_CR_ALRAIE)) {
>> +		/* Alarm A flag - Alarm interrupt */
>> +		events |= RTC_IRQF | RTC_AF;
>> +		isr &= ~STM32_RTC_ISR_ALRAF;
>> +	}
>> +
>> +	/* Clear event irqflags, otherwise new events won't be received */
>> +	stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	if (events) {
>> +		dev_info(&rtc->rtc_dev->dev, "Alarm occurred\n");
>> +
>> +		/* Pass event to the kernel */
>> +		rtc_update_irq(rtc->rtc_dev, 1, events);
>> +		return IRQ_HANDLED;
>> +	} else {
>> +		return IRQ_NONE;
>> +	}
>> +}
>> +
>> +/* Convert rtc_time structure from bin to bcd format */
>> +static void tm2bcd(struct rtc_time *tm)
>> +{
>> +	tm->tm_sec = bin2bcd(tm->tm_sec);
>> +	tm->tm_min = bin2bcd(tm->tm_min);
>> +	tm->tm_hour = bin2bcd(tm->tm_hour);
>> +
>> +	tm->tm_mday = bin2bcd(tm->tm_mday);
>> +	tm->tm_mon = bin2bcd(tm->tm_mon + 1);
>> +	tm->tm_year = bin2bcd(tm->tm_year - 100);
>> +	/*
>> +	 * Number of days since Sunday
>> +	 * - on kernel side, 0=Sunday...6=Saturday
>> +	 * - on rtc side, 0=invalid,1=Monday...7=Sunday
>> +	 */
>> +	tm->tm_wday = (!tm->tm_wday) ? 7 : tm->tm_wday;
>> +}
>> +
>> +/* Convert rtc_time structure from bcd to bin format */
>> +static void bcd2tm(struct rtc_time *tm)
>> +{
>> +	tm->tm_sec = bcd2bin(tm->tm_sec);
>> +	tm->tm_min = bcd2bin(tm->tm_min);
>> +	tm->tm_hour = bcd2bin(tm->tm_hour);
>> +
>> +	tm->tm_mday = bcd2bin(tm->tm_mday);
>> +	tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
>> +	tm->tm_year = bcd2bin(tm->tm_year) + 100;
>> +	/*
>> +	 * Number of days since Sunday
>> +	 * - on kernel side, 0=Sunday...6=Saturday
>> +	 * - on rtc side, 0=invalid,1=Monday...7=Sunday
>> +	 */
>> +	tm->tm_wday %= 7;
>> +}
>> +
>> +static int stm32_rtc_read_time(struct device *dev, struct rtc_time *tm)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	unsigned int tr, dr;
>> +	unsigned long irqflags;
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	/* Time and Date in BCD format */
>> +	tr = stm32_rtc_readl(rtc, STM32_RTC_TR);
>> +	dr = stm32_rtc_readl(rtc, STM32_RTC_DR);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	tm->tm_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
>> +	tm->tm_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
>> +	tm->tm_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
>> +
>> +	tm->tm_mday = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
>> +	tm->tm_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
>> +	tm->tm_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
>> +	tm->tm_wday = (dr & STM32_RTC_DR_WDAY) >> STM32_RTC_DR_WDAY_SHIFT;
>> +
>> +	/* We don't report tm_yday and tm_isdst */
>> +
>> +	bcd2tm(tm);
>> +
>> +	if (rtc_valid_tm(tm) < 0) {
>> +		dev_err(dev, "%s: rtc_time is not valid.\n", __func__);
>> +		return -EINVAL;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static int stm32_rtc_set_time(struct device *dev, struct rtc_time *tm)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	unsigned int tr, dr;
>> +	unsigned long irqflags;
>> +	int ret = 0;
>> +
>> +	if (rtc_valid_tm(tm) < 0) {
>> +		dev_err(dev, "%s: rtc_time is not valid.\n", __func__);
>> +		return -EINVAL;
>> +	}
>> +
>> +	tm2bcd(tm);
>> +
>> +	/* Time in BCD format */
>> +	tr = ((tm->tm_sec << STM32_RTC_TR_SEC_SHIFT) & STM32_RTC_TR_SEC) |
>> +	     ((tm->tm_min << STM32_RTC_TR_MIN_SHIFT) & STM32_RTC_TR_MIN) |
>> +	     ((tm->tm_hour << STM32_RTC_TR_HOUR_SHIFT) & STM32_RTC_TR_HOUR);
>> +
>> +	/* Date in BCD format */
>> +	dr = ((tm->tm_mday << STM32_RTC_DR_DATE_SHIFT) & STM32_RTC_DR_DATE) |
>> +	     ((tm->tm_mon << STM32_RTC_DR_MONTH_SHIFT) & STM32_RTC_DR_MONTH) |
>> +	     ((tm->tm_year << STM32_RTC_DR_YEAR_SHIFT) & STM32_RTC_DR_YEAR) |
>> +	     ((tm->tm_wday << STM32_RTC_DR_WDAY_SHIFT) & STM32_RTC_DR_WDAY);
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	stm32_rtc_wpr_unlock(rtc);
>> +
>> +	ret = stm32_rtc_enter_init_mode(rtc);
>> +	if (ret) {
>> +		dev_err(dev, "Can't enter in init mode. Set time aborted.\n");
>> +		goto end;
>> +	}
>> +
>> +	stm32_rtc_writel(rtc, STM32_RTC_TR, tr);
>> +	stm32_rtc_writel(rtc, STM32_RTC_DR, dr);
>> +
>> +	stm32_rtc_exit_init_mode(rtc);
>> +
>> +	ret = stm32_rtc_wait_sync(rtc);
>> +end:
>> +	stm32_rtc_wpr_lock(rtc);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	return ret;
>> +}
>> +
>> +static int stm32_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	struct rtc_time *tm = &alrm->time;
>> +	unsigned int alrmar, cr, isr;
>> +	unsigned long irqflags;
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	alrmar = stm32_rtc_readl(rtc, STM32_RTC_ALRMAR);
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +	isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	if (alrmar & STM32_RTC_ALRMXR_DATE_MASK) {
>> +		/*
>> +		 * Date/day don't care in Alarm comparison so alarm triggers
>> +		 * every day
>> +		 */
>> +		tm->tm_mday = -1;
>> +		tm->tm_wday = -1;
>> +	} else {
>> +		if (alrmar & STM32_RTC_ALRMXR_WDSEL) {
>> +			/* Alarm is set to a day of week */
>> +			tm->tm_mday = -1;
>> +			tm->tm_wday = (alrmar & STM32_RTC_ALRMXR_WDAY) >>
>> +				      STM32_RTC_ALRMXR_WDAY_SHIFT;
>> +			tm->tm_wday %= 7;
>> +		} else {
>> +			/* Alarm is set to a day of month */
>> +			tm->tm_wday = -1;
>> +			tm->tm_mday = (alrmar & STM32_RTC_ALRMXR_DATE) >>
>> +				       STM32_RTC_ALRMXR_DATE_SHIFT;
>> +		}
>> +	}
>> +
>> +	if (alrmar & STM32_RTC_ALRMXR_HOUR_MASK) {
>> +		/* Hours don't care in Alarm comparison */
>> +		tm->tm_hour = -1;
>> +	} else {
>> +		tm->tm_hour = (alrmar & STM32_RTC_ALRMXR_HOUR) >>
>> +			       STM32_RTC_ALRMXR_HOUR_SHIFT;
>> +		if (alrmar & STM32_RTC_ALRMXR_PM)
>> +			tm->tm_hour += 12;
>> +	}
>> +
>> +	if (alrmar & STM32_RTC_ALRMXR_MIN_MASK) {
>> +		/* Minutes don't care in Alarm comparison */
>> +		tm->tm_min = -1;
>> +	} else {
>> +		tm->tm_min = (alrmar & STM32_RTC_ALRMXR_MIN) >>
>> +			      STM32_RTC_ALRMXR_MIN_SHIFT;
>> +	}
>> +
>> +	if (alrmar & STM32_RTC_ALRMXR_SEC_MASK) {
>> +		/* Seconds don't care in Alarm comparison */
>> +		tm->tm_sec = -1;
>> +	} else {
>> +		tm->tm_sec = (alrmar & STM32_RTC_ALRMXR_SEC) >>
>> +			      STM32_RTC_ALRMXR_SEC_SHIFT;
>> +	}
>> +
>> +	bcd2tm(tm);
>> +
>> +	alrm->enabled = (cr & STM32_RTC_CR_ALRAE) ? 1 : 0;
>> +	alrm->pending = (isr & STM32_RTC_ISR_ALRAF) ? 1 : 0;
>> +
>> +	return 0;
>> +}
>> +
>> +static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	unsigned long irqflags;
>> +	unsigned int isr, cr;
>> +
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	stm32_rtc_wpr_unlock(rtc);
>> +
>> +	/* We expose Alarm A to the kernel */
>> +	if (enabled)
>> +		cr |= (STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
>> +	else
>> +		cr &= ~(STM32_RTC_CR_ALRAIE | STM32_RTC_CR_ALRAE);
>> +	stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
>> +
>> +	/* Clear event irqflags, otherwise new events won't be received */
>> +	isr = stm32_rtc_readl(rtc, STM32_RTC_ISR);
>> +	isr &= ~STM32_RTC_ISR_ALRAF;
>> +	stm32_rtc_writel(rtc, STM32_RTC_ISR, isr);
>> +
>> +	stm32_rtc_wpr_lock(rtc);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	return 0;
>> +}
>> +
>> +static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	struct rtc_time *tm = &alrm->time;
>> +	unsigned long irqflags;
>> +	unsigned int cr, isr, alrmar;
>> +	int ret = 0;
>> +
>> +	if (rtc_valid_tm(tm)) {
>> +		dev_err(dev, "Alarm time not valid.\n");
>> +		return -EINVAL;
>> +	}
>> +
>> +	tm2bcd(tm);
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	stm32_rtc_wpr_unlock(rtc);
>> +
>> +	/* Disable Alarm */
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +	cr &= ~STM32_RTC_CR_ALRAE;
>> +	stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
>> +
>> +	/* Poll Alarm write flag to be sure that Alarm update is allowed */
>> +	ret = readl_relaxed_poll_timeout_atomic(rtc->base + STM32_RTC_ISR,
>> +						isr,
>> +						(isr & STM32_RTC_ISR_ALRAWF),
>> +						10, 100);
>> +
>> +	if (ret) {
>> +		dev_err(dev, "Alarm update not allowed\n");
>> +		goto end;
>> +	}
>> +
>> +	alrmar = 0;
>> +
>> +	if (tm->tm_mday < 0 && tm->tm_wday < 0) {
>> +		/*
>> +		 * Date/day don't care in Alarm comparison so alarm triggers
>> +		 * every day
>> +		 */
>> +		alrmar |= STM32_RTC_ALRMXR_DATE_MASK;
>> +	} else {
>> +		if (tm->tm_mday > 0) {
>> +			/* Date is selected (ignoring wday) */
>> +			alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &
>> +				  STM32_RTC_ALRMXR_DATE;
>> +		} else {
>> +			/* Day of week is selected */
>> +			int wday = (tm->tm_wday == 0) ? 7 : tm->tm_wday;
>> +
>> +			alrmar |= STM32_RTC_ALRMXR_WDSEL;
>> +			alrmar |= (wday << STM32_RTC_ALRMXR_WDAY_SHIFT) &
>> +				  STM32_RTC_ALRMXR_WDAY;
>> +		}
>> +	}
>> +
>> +	if (tm->tm_hour < 0) {
>> +		/* Hours don't care in Alarm comparison */
>> +		alrmar |= STM32_RTC_ALRMXR_HOUR_MASK;
>> +	} else {
>> +		/* 24-hour format */
>> +		alrmar &= ~STM32_RTC_ALRMXR_PM;
>> +		alrmar |= (tm->tm_hour << STM32_RTC_ALRMXR_HOUR_SHIFT) &
>> +			  STM32_RTC_ALRMXR_HOUR;
>> +	}
>> +
>> +	if (tm->tm_min < 0) {
>> +		/* Minutes don't care in Alarm comparison */
>> +		alrmar |= STM32_RTC_ALRMXR_MIN_MASK;
>> +	} else {
>> +		alrmar |= (tm->tm_min << STM32_RTC_ALRMXR_MIN_SHIFT) &
>> +			  STM32_RTC_ALRMXR_MIN;
>> +	}
>> +
>> +	if (tm->tm_sec < 0) {
>> +		/* Seconds don't care in Alarm comparison */
>> +		alrmar |= STM32_RTC_ALRMXR_SEC_MASK;
>> +	} else {
>> +		alrmar |= (tm->tm_sec << STM32_RTC_ALRMXR_SEC_SHIFT) &
>> +			  STM32_RTC_ALRMXR_SEC;
>> +	}
>> +
>> +	/* Write to Alarm register */
>> +	stm32_rtc_writel(rtc, STM32_RTC_ALRMAR, alrmar);
>> +
>> +	if (alrm->enabled)
>> +		stm32_rtc_alarm_irq_enable(dev, 1);
>> +	else
>> +		stm32_rtc_alarm_irq_enable(dev, 0);
>> +
>> +end:
>> +	stm32_rtc_wpr_lock(rtc);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	return ret;
>> +}
>> +
>> +static const struct rtc_class_ops stm32_rtc_ops = {
>> +	.read_time	= stm32_rtc_read_time,
>> +	.set_time	= stm32_rtc_set_time,
>> +	.read_alarm	= stm32_rtc_read_alarm,
>> +	.set_alarm	= stm32_rtc_set_alarm,
>> +	.alarm_irq_enable = stm32_rtc_alarm_irq_enable,
>> +};
>> +
>> +#ifdef CONFIG_OF
>> +static const struct of_device_id stm32_rtc_of_match[] = {
>> +	{ .compatible = "st,stm32-rtc" },
>> +	{}
>> +};
>> +MODULE_DEVICE_TABLE(of, stm32_rtc_of_match);
>> +#endif
>> +
>> +static int stm32_rtc_init(struct platform_device *pdev,
>> +			  struct stm32_rtc *rtc)
>> +{
>> +	unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr;
>> +	unsigned int rate;
>> +	unsigned long irqflags;
>> +	int ret = 0;
>> +
>> +	rate = clk_get_rate(rtc->ck_rtc);
>> +
>> +	/* Find prediv_a and prediv_s to obtain the 1Hz calendar clock */
>> +	pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT;
>> +	pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT;
>> +
>> +	for (pred_a = pred_a_max; pred_a >= 0; pred_a--) {
>> +		pred_s = (rate / (pred_a + 1)) - 1;
>> +
>> +		if (((pred_s + 1) * (pred_a + 1)) == rate)
>> +			break;
>> +	}
>> +
>> +	/*
>> +	 * Can't find a 1Hz, so give priority to RTC power consumption
>> +	 * by choosing the higher possible value for prediv_a
>> +	 */
>> +	if ((pred_s > pred_s_max) || (pred_a > pred_a_max)) {
>> +		pred_a = pred_a_max;
>> +		pred_s = (rate / (pred_a + 1)) - 1;
>> +
>> +		dev_warn(&pdev->dev, "ck_rtc is %s\n",
>> +			 (rate - ((pred_a + 1) * (pred_s + 1)) < 0) ?
>> +			 "fast" : "slow");
>> +	}
>> +
>> +	spin_lock_irqsave(&rtc->lock, irqflags);
>> +
>> +	stm32_rtc_wpr_unlock(rtc);
>> +
>> +	ret = stm32_rtc_enter_init_mode(rtc);
>> +	if (ret) {
>> +		dev_err(&pdev->dev,
>> +			"Can't enter in init mode. Prescaler config failed.\n");
>> +		goto end;
>> +	}
>> +
>> +	prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & STM32_RTC_PRER_PRED_S;
>> +	stm32_rtc_writel(rtc, STM32_RTC_PRER, prer);
>> +	prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & STM32_RTC_PRER_PRED_A;
>> +	stm32_rtc_writel(rtc, STM32_RTC_PRER, prer);
>> +
>> +	/* Force 24h time format */
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +	cr &= ~STM32_RTC_CR_FMT;
>> +	stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
>> +
>> +	stm32_rtc_exit_init_mode(rtc);
>> +
>> +	ret = stm32_rtc_wait_sync(rtc);
>> +
>> +	if (stm32_rtc_readl(rtc, STM32_RTC_ISR) & STM32_RTC_ISR_INITS)
>> +		dev_warn(&pdev->dev, "Date/Time must be initialized\n");
>> +end:
>> +	stm32_rtc_wpr_lock(rtc);
>> +
>> +	spin_unlock_irqrestore(&rtc->lock, irqflags);
>> +
>> +	return ret;
>> +}
>> +
>> +static int stm32_rtc_probe(struct platform_device *pdev)
>> +{
>> +	struct stm32_rtc *rtc;
>> +	struct resource *res;
>> +	int ret;
>> +
>> +	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
>> +	if (!rtc)
>> +		return -ENOMEM;
>> +
>> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> +	rtc->base = devm_ioremap_resource(&pdev->dev, res);
>> +	if (IS_ERR(rtc->base))
>> +		return PTR_ERR(rtc->base);
>> +
>> +	dbp = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "st,syscfg");
>> +	if (IS_ERR(dbp)) {
>> +		dev_err(&pdev->dev, "no st,syscfg\n");
>> +		return PTR_ERR(dbp);
>> +	}
>> +
>> +	spin_lock_init(&rtc->lock);
>> +
>> +	rtc->ck_rtc = devm_clk_get(&pdev->dev, "ck_rtc");
>> +	if (IS_ERR(rtc->ck_rtc)) {
>> +		dev_err(&pdev->dev, "no ck_rtc clock");
>> +		return PTR_ERR(rtc->ck_rtc);
>> +	}
>> +
>> +	ret = clk_prepare_enable(rtc->ck_rtc);
>> +	if (ret)
>> +		return ret;
>> +
>> +	if (dbp)
>> +		regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, PWR_CR_DBP);
>> +
>> +	ret = stm32_rtc_init(pdev, rtc);
>> +	if (ret)
>> +		goto err;
>> +
>> +	rtc->irq_alarm = platform_get_irq_byname(pdev, "alarm");
>> +	if (rtc->irq_alarm <= 0) {
>> +		dev_err(&pdev->dev, "no alarm irq\n");
>> +		ret = -ENOENT;
>> +		goto err;
>> +	}
>> +
>> +	platform_set_drvdata(pdev, rtc);
>> +
>> +	device_init_wakeup(&pdev->dev, true);
>> +
>> +	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
>> +			&stm32_rtc_ops, THIS_MODULE);
>> +	if (IS_ERR(rtc->rtc_dev)) {
>> +		ret = PTR_ERR(rtc->rtc_dev);
>> +		dev_err(&pdev->dev, "rtc device registration failed, err=%d\n",
>> +			ret);
>> +		goto err;
>> +	}
>> +
>> +	/* Handle RTC alarm interrupts */
>> +	ret = devm_request_irq(&pdev->dev, rtc->irq_alarm,
>> +			       stm32_rtc_alarm_irq, IRQF_TRIGGER_RISING,
>> +			       dev_name(&rtc->rtc_dev->dev), rtc);
>> +	if (ret) {
>> +		dev_err(&pdev->dev, "IRQ%d (alarm interrupt) already claimed\n",
>> +			rtc->irq_alarm);
>> +		goto err;
>> +	}
>> +
>> +	return 0;
>> +err:
>> +	clk_disable_unprepare(rtc->ck_rtc);
>> +
>> +	if (dbp)
>> +		regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
>> +
>> +	device_init_wakeup(&pdev->dev, false);
>> +
>> +	return ret;
>> +}
>> +
>> +static int __exit stm32_rtc_remove(struct platform_device *pdev)
>> +{
>> +	struct stm32_rtc *rtc = platform_get_drvdata(pdev);
>> +	unsigned int cr;
>> +
>> +	/* Disable interrupts */
>> +	stm32_rtc_wpr_unlock(rtc);
>> +	cr = stm32_rtc_readl(rtc, STM32_RTC_CR);
>> +	cr &= ~STM32_RTC_CR_ALRAIE;
>> +	stm32_rtc_writel(rtc, STM32_RTC_CR, cr);
>> +	stm32_rtc_wpr_lock(rtc);
>> +
>> +	clk_disable_unprepare(rtc->ck_rtc);
>> +
>> +	/* Enable backup domain write protection */
>> +	if (dbp)
>> +		regmap_update_bits(dbp, PWR_CR, PWR_CR_DBP, ~PWR_CR_DBP);
>> +
>> +	device_init_wakeup(&pdev->dev, false);
>> +
>> +	return 0;
>> +}
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +static int stm32_rtc_suspend(struct device *dev)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +
>> +	if (device_may_wakeup(dev))
>> +		return enable_irq_wake(rtc->irq_alarm);
>> +
>> +	return 0;
>> +}
>> +
>> +static int stm32_rtc_resume(struct device *dev)
>> +{
>> +	struct stm32_rtc *rtc = dev_get_drvdata(dev);
>> +	int ret = 0;
>> +
>> +	ret = stm32_rtc_wait_sync(rtc);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	if (device_may_wakeup(dev))
>> +		return disable_irq_wake(rtc->irq_alarm);
>> +
>> +	return ret;
>> +}
>> +#endif
>> +
>> +static SIMPLE_DEV_PM_OPS(stm32_rtc_pm_ops,
>> +			 stm32_rtc_suspend, stm32_rtc_resume);
>> +
>> +static struct platform_driver stm32_rtc_driver = {
>> +	.probe		= stm32_rtc_probe,
>> +	.remove		= stm32_rtc_remove,
>> +	.driver		= {
>> +		.name	= DRIVER_NAME,
>> +		.pm	= &stm32_rtc_pm_ops,
>> +		.of_match_table = stm32_rtc_of_match,
>> +	},
>> +};
>> +
>> +module_platform_driver(stm32_rtc_driver);
>> +
>> +MODULE_ALIAS("platform:" DRIVER_NAME);
>> +MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
>> +MODULE_DESCRIPTION("STMicroelectronics STM32 Real Time Clock driver");
>> +MODULE_LICENSE("GPL v2");
>> --
>> 1.9.1
>>

Best regards,
Amelie

^ permalink raw reply

* [PATCH v9 07/16] drivers: acpi: implement acpi_dma_configure
From: Sricharan @ 2016-12-05  9:52 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <CAJZ5v0j5aMMyNsYpnbGXbJdDrbHFN5u03mQi+W6WK97iCdB2HA@mail.gmail.com>

Hi Lorenzo,

>
>On Sat, Dec 3, 2016 at 11:39 AM, Lorenzo Pieralisi
><lorenzo.pieralisi@arm.com> wrote:
>> On Sat, Dec 03, 2016 at 03:11:09AM +0100, Rafael J. Wysocki wrote:
>>> On Fri, Dec 2, 2016 at 4:38 PM, Lorenzo Pieralisi
>>> <lorenzo.pieralisi@arm.com> wrote:
>>> > Rafael, Mark, Suravee,
>>> >
>>> > On Mon, Nov 21, 2016 at 10:01:39AM +0000, Lorenzo Pieralisi wrote:
>>> >> On DT based systems, the of_dma_configure() API implements DMA
>>> >> configuration for a given device. On ACPI systems an API equivalent to
>>> >> of_dma_configure() is missing which implies that it is currently not
>>> >> possible to set-up DMA operations for devices through the ACPI generic
>>> >> kernel layer.
>>> >>
>>> >> This patch fills the gap by introducing acpi_dma_configure/deconfigure()
>>> >> calls that for now are just wrappers around arch_setup_dma_ops() and
>>> >> arch_teardown_dma_ops() and also updates ACPI and PCI core code to use
>>> >> the newly introduced acpi_dma_configure/acpi_dma_deconfigure functions.
>>> >>
>>> >> Since acpi_dma_configure() is used to configure DMA operations, the
>>> >> function initializes the dma/coherent_dma masks to sane default values
>>> >> if the current masks are uninitialized (also to keep the default values
>>> >> consistent with DT systems) to make sure the device has a complete
>>> >> default DMA set-up.
>>> >
>>> > I spotted a niggle that unfortunately was hard to spot (and should not
>>> > be a problem per se but better safe than sorry) and I am not comfortable
>>> > with it.
>>> >
>>> > Following commit d0562674838c ("ACPI / scan: Parse _CCA and setup
>>> > device coherency") in acpi_bind_one() we check if the acpi_device
>>> > associated with a device just added supports DMA, first it was
>>> > done with acpi_check_dma() and then commit 1831eff876bd ("device
>>> > property: ACPI: Make use of the new DMA Attribute APIs") changed
>>> > it to acpi_get_dma_attr().
>>> >
>>> > The subsequent check (attr != DEV_DMA_NOT_SUPPORTED) is always true
>>> > on _any_ acpi device we pass to acpi_bind_one() on x86, which was
>>> > fine because we used it to call arch_setup_dma_ops(), which is a nop
>>> > on x86. On ARM64 a _CCA method is required to define if a device
>>> > supports DMA so (attr != DEV_DMA_NOT_SUPPORTED) may well be false.
>>> >
>>> > Now, acpi_bind_one() is used to bind an acpi_device to its physical
>>> > node also for pseudo-devices like cpus and memory nodes. For those
>>> > objects, on x86, attr will always be != DEV_DMA_NOT_SUPPORTED.
>>> >
>>> > So far so good, because on x86 arch_setup_dma_ops() is empty code.
>>> >
>>> > With this patch, I use the (attr != DEV_DMA_NOT_SUPPORTED) check
>>> > to call acpi_dma_configure() which is basically a nop on x86 except
>>> > that it sets up the dma_mask/coherent_dma_mask to a sane default value
>>> > (after all we are setting up DMA for the device so it makes sense to
>>> > initialize the masks there if they were unset since we are configuring
>>> > DMA for the device in question) for the given device.
>>> >
>>> > Problem is, as per the explanation above, we are also setting the
>>> > default dma masks for pseudo-devices (eg CPUs) that were previously
>>> > untouched, it should not be a problem per-se but I am not comfortable
>>> > with that, honestly it does not make much sense.
>>> >
>>> > An easy "fix" would be to move the default dma masks initialization out
>>> > of acpi_dma_configure() (as it was in previous patch versions of this
>>> > series - I moved it to acpi_dma_configure() just a consolidation point
>>> > for initializing the masks instead of scattering them in every
>>> > acpi_dma_configure caller) I can send this as a fix-up patch to Joerg if
>>> > we think that's the right thing to do (or I can send it to Rafael later
>>> > when the code is in the merged depending on the timing) just let me
>>> > know please.
>>>
>>> Why can't arch_setup_dma_ops() set those masks too?
>>
>> Because the dma masks set-up is done by the caller (see
>> of_dma_configure()) according to firmware configuration or
>> platform data knowledge. I wanted to replicate the of_dma_configure()
>> interface on ACPI for obvious reasons (on ARM systems), I stopped
>> short of adding ACPI code to mirror of_dma_get_range() equivalent
>> (through the _DMA object) but I am really really nervous about changing
>> the code path on x86 because in theory all is fine, in practice even
>> just setting the masks to sane values can have unexpected consequences,
>> I just can't know (that's why I wasn't doing it in the first iterations
>> of this series).
>>
>> Side note: DT with of_dma_configure() and ACPI with
>> acpi_create_platform_device() set the default dma mask for all
>> platform devices already _regardless_ of what they really are, though
>> arguably acpi_bind_one() touches ways more devices.
>>
>> I really think that removing the default dma masks settings from
>> acpi_dma_configure() is the safer thing to do for the time being (or
>> moving acpi_dma_configure() to acpi_create_platform_device(), where the
>> DMA masks are set-up by default by core ACPI. Mark, Suravee, what was
>> the rationale behind calling arch_setup_dma_ops() in acpi_bind_one() ?)
>
>Alternatively, you can add one more arch wrapper that will be a no-op
>on x86 and that will set up the default masks and call
>arch_setup_dma_ops() on ARM.  Then, you can invoke that from
>acpi_dma_configure().
>
>Or make the definition of acpi_dma_configure() itself depend on the
>architecture.
>

So is it better that either removing the masks from acpi_dma_configure (or)
creating the wrapper as Rafael mentioned, than moving
acpi_dma_configure itself , because with something like iommu probe
deferral that is tried, acpi_dma_configure is getting invoked from a device's 
really_probe, a different path again ?

Regards,
 Sricharan

^ permalink raw reply

* [stable:PATCH 0/3] PAN fixes backport for v4.4.35
From: James Morse @ 2016-12-05  9:52 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161202172321.GA25426@kroah.com>

Hi Greg,

On 02/12/16 17:23, Greg KH wrote:
> On Fri, Dec 02, 2016 at 04:42:44PM +0000, James Morse wrote:
>> Hi linux-stable,
>>
>> This is the backport of the recent PAN fixes series for v4.4.35.
>> Original series:
>> http://lists.infradead.org/pipermail/linux-arm-kernel/2016-October/461806.html
> 
> Why just 4.4-stable?  What about 4.8-stable?

Patch 1 doesn't backport easily. I was going to do v4.8 next, but thought one at
a time would be the least confusing...

(I don't seem to be very good at predicting what is confusing!)


Thanks,

James

^ permalink raw reply

* ILP32 for ARM64: testing with glibc testsuite
From: Zhangjian (Bamvor) @ 2016-12-05  9:58 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1479419136.908.90.camel@caviumnetworks.com>

Hi, Steve

On 2016/11/18 5:45, Steve Ellcey wrote:
> On Wed, 2016-11-16 at 15:22 +0400, Maxim Kuvyrkov wrote:
>>>
>>> On Nov 9, 2016, at 1:56 PM, Yury Norov <ynorov@caviumnetworks.com>
>>> wrote:
>>>
>>>>
>>>> Below is the results of glibc testsuite run for aarch64/lp64
>
> I have been running the glibc testsuite as well.  I have only run it on
> an ILP32 enabled kernel.  Using that kernel, top-of-tree glibc, and the
> ILP32 glibc patches I have no LP64 regressions.  There are 5 failures
> in LP64 mode but I get them with vanilla top-of-tree glibc sources too.
> They are:
> 	nptl/eintr1 (I actually don't run this because it kills the 'make check')
> 	debug/tst-backtrace5
> 	debug/tst-backtrace6
> 	nptl/tst-stack4
> 	nptl/tst-thread_local1
>
> In ILP32 mode I get 33 failures, they include the above failures (minus
> nptl/tst-thread_local1) plus:
>
> 	c++-types-check
> 	conform/ISO11/inttypes.h/conform
> 	conform/ISO11/stdint.h/conform
> 	conform/ISO99/inttypes.h/conform
> 	conform/ISO99/stdint.h/conform
> 	conform/POSIX2008/inttypes.h/conform
> 	conform/POSIX2008/stdint.h/conform
> 	conform/XOPEN2K/inttypes.h/conform
> 	conform/XOPEN2K/stdint.h/conform
> 	conform/XOPEN2K8/inttypes.h/conform
> 	conform/XOPEN2K8/stdint.h/conform
> 	elf/tst-tls1
> 	elf/tst-tls1-static
> 	elf/tst-tls2
> 	elf/tst-tls2-static
> 	elf/tst-tls3
> 	math/check-abi-libm
> 	math/test-double
> 	math/test-double-finite
> 	math/test-float
> 	math/test-float-finite
> 	misc/tst-sync_file_range
> 	nptl/tst-cancel26
> 	nptl/tst-cancel27
> 	nptl/tst-sem3
> 	rt/tst-mqueue1
> 	rt/tst-mqueue2
> 	rt/tst-mqueue4
> 	rt/tst-mqueue7
> 	stdlib/tst-makecontext3
>
> I am currently looking at these ILP32 regressions (starting with the
> tls failures) to see if I can figure out what is happening with them.
Is there some progresses on it? We could collabrate to fix those issues.

Regards

Bamvor
>
> Steve Ellcey
> sellcey at caviumnetworks.com
>

^ permalink raw reply

* [PATCH v4] ARM: davinci: da8xx: Fix sleeping function called from invalid context
From: Alexandre Bailon @ 2016-12-05 10:00 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <907a3c22-534f-80ce-daff-be84dd5e5cf8@lechnology.com>

On 12/05/2016 04:44 AM, David Lechner wrote:
> On 12/02/2016 08:53 AM, Alexandre Bailon wrote:
>> Everytime the usb20 phy is enabled, there is a
>> "sleeping function called from invalid context" BUG.
>>
>> clk_enable() from arch/arm/mach-davinci/clock.c uses spin_lock_irqsave()
>> before to invoke the callback usb20_phy_clk_enable().
>> usb20_phy_clk_enable() uses clk_get() and clk_enable_prepapre()
>> which may sleep.
>> Move clk_get() to da8xx_register_usb20_phy_clk() and
>> replace clk_prepare_enable() by clk_enable().
>>
>> Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
>> ---
>>  arch/arm/mach-davinci/usb-da8xx.c | 28 +++++++++++++++-------------
>>  1 file changed, 15 insertions(+), 13 deletions(-)
>>
>> diff --git a/arch/arm/mach-davinci/usb-da8xx.c
>> b/arch/arm/mach-davinci/usb-da8xx.c
>> index b010e5f..704f506 100644
>> --- a/arch/arm/mach-davinci/usb-da8xx.c
>> +++ b/arch/arm/mach-davinci/usb-da8xx.c
>> @@ -22,6 +22,8 @@
>>  #define DA8XX_USB0_BASE        0x01e00000
>>  #define DA8XX_USB1_BASE        0x01e25000
>>
>> +static struct clk *usb20_clk;
>> +
>>  static struct platform_device da8xx_usb_phy = {
>>      .name        = "da8xx-usb-phy",
>>      .id        = -1,
>> @@ -158,21 +160,14 @@ int __init da8xx_register_usb_refclkin(int rate)
>>
>>  static void usb20_phy_clk_enable(struct clk *clk)
>>  {
>> -    struct clk *usb20_clk;
>>      int err;
>>      u32 val;
>>      u32 timeout = 500000; /* 500 msec */
>>
>>      val = readl(DA8XX_SYSCFG0_VIRT(DA8XX_CFGCHIP2_REG));
>>
>> -    usb20_clk = clk_get(&da8xx_usb20_dev.dev, "usb20");
>> -    if (IS_ERR(usb20_clk)) {
>> -        pr_err("could not get usb20 clk: %ld\n", PTR_ERR(usb20_clk));
>> -        return;
>> -    }
>> -
>>      /* The USB 2.O PLL requires that the USB 2.O PSC is enabled as
>> well. */
>> -    err = clk_prepare_enable(usb20_clk);
>> +    err = clk_enable(usb20_clk);
>>      if (err) {
>>          pr_err("failed to enable usb20 clk: %d\n", err);
>>          clk_put(usb20_clk);
> 
> Need to remove clk_put() here.
I will do.
> 
>> @@ -197,8 +192,7 @@ static void usb20_phy_clk_enable(struct clk *clk)
>>
>>      pr_err("Timeout waiting for USB 2.0 PHY clock good\n");
>>  done:
>> -    clk_disable_unprepare(usb20_clk);
>> -    clk_put(usb20_clk);
>> +    clk_disable(usb20_clk);
>>  }
>>
>>  static void usb20_phy_clk_disable(struct clk *clk)
>> @@ -285,11 +279,19 @@ static struct clk_lookup usb20_phy_clk_lookup =
>>  int __init da8xx_register_usb20_phy_clk(bool use_usb_refclkin)
>>  {
>>      struct clk *parent;
>> -    int ret = 0;
>> +    int ret;
>> +
>> +    usb20_clk = clk_get(&da8xx_usb20_dev.dev, "usb20");
>> +    ret = PTR_ERR_OR_ZERO(usb20_clk);
>> +    if (ret)
>> +        return ret;
>>
>>      parent = clk_get(NULL, use_usb_refclkin ? "usb_refclkin" :
>> "pll0_aux");
>> -    if (IS_ERR(parent))
>> -        return PTR_ERR(parent);
>> +    ret = PTR_ERR_OR_ZERO(parent);
>> +    if (ret) {
>> +        clk_put(usb20_clk);
>> +        return ret;
>> +    }
>>
>>      usb20_phy_clk.parent = parent;
>>      ret = clk_register(&usb20_phy_clk);
>>
> 
> 
> I have just tried this patch with a bunch of kernel hacking options
> enabled. I am getting the message show at the end of this email. We
> still have the problem of nested spin locks due to nested calls to
> clk_enable(). So, really, we need to use __clk_enable() and
> __clk_disable() from arch/arm/mach-davinci/clock.c in
> usb20_phy_clk_enable() above.
> 
> Applying the following patch on top of your patch fixed the recursive
> lock message.
Good catch.

Thanks,
Alexandre
> 
> ---
> 
> diff --git a/arch/arm/mach-davinci/clock.c b/arch/arm/mach-davinci/clock.c
> index df42c93..4fba579 100644
> --- a/arch/arm/mach-davinci/clock.c
> +++ b/arch/arm/mach-davinci/clock.c
> @@ -31,7 +31,7 @@ static LIST_HEAD(clocks);
>  static DEFINE_MUTEX(clocks_mutex);
>  static DEFINE_SPINLOCK(clockfw_lock);
> 
> -static void __clk_enable(struct clk *clk)
> +void __clk_enable(struct clk *clk)
>  {
>         if (clk->parent)
>                 __clk_enable(clk->parent);
> @@ -44,7 +44,7 @@ static void __clk_enable(struct clk *clk)
>         }
>  }
> 
> -static void __clk_disable(struct clk *clk)
> +void __clk_disable(struct clk *clk)
>  {
>         if (WARN_ON(clk->usecount == 0))
>                 return;
> diff --git a/arch/arm/mach-davinci/clock.h b/arch/arm/mach-davinci/clock.h
> index e2a5437..8493242 100644
> --- a/arch/arm/mach-davinci/clock.h
> +++ b/arch/arm/mach-davinci/clock.h
> @@ -136,6 +136,9 @@ int davinci_clk_reset(struct clk *clk, bool reset);
>  extern struct platform_device davinci_wdt_device;
>  extern void davinci_watchdog_reset(struct platform_device *);
> 
> +void __clk_enable(struct clk *clk);
> +void __clk_disable(struct clk *clk);
> +
>  #endif
> 
>  #endif
> diff --git a/arch/arm/mach-davinci/usb-da8xx.c
> b/arch/arm/mach-davinci/usb-da8xx
> .c
> index 896d014..80ba0df 100644
> --- a/arch/arm/mach-davinci/usb-da8xx.c
> +++ b/arch/arm/mach-davinci/usb-da8xx.c
> @@ -160,19 +160,13 @@ int __init da8xx_register_usb_refclkin(int rate)
> 
>  static void usb20_phy_clk_enable(struct clk *clk)
>  {
> -       int err;
>         u32 val;
>         u32 timeout = 500000; /* 500 msec */
> 
>         val = readl(DA8XX_SYSCFG0_VIRT(DA8XX_CFGCHIP2_REG));
> 
>         /* The USB 2.O PLL requires that the USB 2.O PSC is enabled as
> well. */
> -       err = clk_enable(usb20_clk);
> -       if (err) {
> -               pr_err("failed to enable usb20 clk: %d\n", err);
> -               clk_put(usb20_clk);
> -               return;
> -       }
> +       __clk_enable(usb20_clk);
> 
>         /*
>          * Turn on the USB 2.0 PHY, but just the PLL, and not OTG. The
> USB 1.1
> @@ -192,7 +186,7 @@ static void usb20_phy_clk_enable(struct clk *clk)
> 
>         pr_err("Timeout waiting for USB 2.0 PHY clock good\n");
>  done:
> -       clk_disable(usb20_clk);
> +       __clk_disable(usb20_clk);
>  }
> 
>  static void usb20_phy_clk_disable(struct clk *clk)
> 
> 
> 
> ---
> 
> 
> =============================================
> [ INFO: possible recursive locking detected ]
> 4.9.0-rc8-dlech-ev3-lms2012+ #22 Not tainted
> ---------------------------------------------
> swapper/1 is trying to acquire lock:
>  (
> clockfw_lock
> ){......}
> , at:
> [<c0014884>] clk_enable+0x24/0x50
> k:
>  (
> clockfw_lock
> ){......}
> , at:
> [<c0014884>] clk_enable+0x24/0x50
> ebug this:
>  Possible unsafe locking scenario:
>        CPU0
>        ----
>   lock(
> clockfw_lock
> );
>   lock(
> clockfw_lock
> );
>  May be due to missing lock nesting notation
> 4 locks held by swapper/1:
>  #0:
>  (
> &dev->mutex
> ){......}
> , at:
> [<c02bd60c>] __driver_attach+0x68/0xb4
>  #1:
>  (
> &dev->mutex
> ){......}
> , at:
> [<c02bd61c>] __driver_attach+0x78/0xb4
>  #2:
>  (
> &phy->mutex
> ){+.+...}
> , at:
> [<c025ee18>] phy_power_on+0x5c/0xe4
>  #3:
>  (
> clockfw_lock
> ){......}
> , at:
> [<c0014884>] clk_enable+0x24/0x50
> CPU: 0 PID: 1 Comm: swapper Not tainted 4.9.0-rc8-dlech-ev3-lms2012+ #22
> Hardware name: Generic DA850/OMAP-L138/AM18x
> Backtrace:
> [<c000d824>] (dump_backtrace) from [<c000dac4>] (show_stack+0x18/0x1c)
>  r7:c0e998a4 r6:c0820750 r5:c0820750 r4:c3838000
> [<c000daac>] (show_stack) from [<c02393dc>] (dump_stack+0x20/0x28)
> [<c02393bc>] (dump_stack) from [<c004bd7c>] (__lock_acquire+0x10f4/0x167c)
> [<c004ac88>] (__lock_acquire) from [<c004c6ac>] (lock_acquire+0x78/0x98)
>  r10:00000000 r9:c06a5ed4 r8:00000000 r7:00000001 r6:60000093 r5:00000000
>  r4:ffffe000
> [<c004c634>] (lock_acquire) from [<c04d8fac>]
> (_raw_spin_lock_irqsave+0x60/0x74)
>  r7:0000003b r6:c0014884 r5:80000093 r4:c06b09a0
> [<c04d8f4c>] (_raw_spin_lock_irqsave) from [<c0014884>]
> (clk_enable+0x24/0x50)
>  r6:c06f69f4 r5:0001ef02 r4:c06b3398
> [<c0014860>] (clk_enable) from [<c0015c74>]
> (usb20_phy_clk_enable+0x24/0xb8)
>  r5:0001ef02 r4:c06f69f0
> [<c0015c50>] (usb20_phy_clk_enable) from [<c0014858>]
> (__clk_enable+0x74/0x7c)
>  r9:c06a5ed4 r8:00000000 r7:0000003b r6:c3a6ca00 r5:80000013 r4:c06b7ee8
> [<c00147e4>] (__clk_enable) from [<c0014808>] (__clk_enable+0x24/0x7c)
>  r4:c06b8648
> [<c00147e4>] (__clk_enable) from [<c0014890>] (clk_enable+0x30/0x50)
>  r4:c06b8648
> [<c0014860>] (clk_enable) from [<c025f43c>]
> (da8xx_usb11_phy_power_on+0x30/0x80)
>  r5:c3a54050 r4:c06b8648
> [<c025f40c>] (da8xx_usb11_phy_power_on) from [<c025ee3c>]
> (phy_power_on+0x80/0xe4)
>  r5:c3a6c800 r4:fffffdf4
> [<c025edbc>] (phy_power_on) from [<c032243c>] (ohci_da8xx_enable+0x4c/0x80)
>  r7:0000003b r6:c3af6000 r5:c3af6000 r4:00000000
> [<c03223f0>] (ohci_da8xx_enable) from [<c03224f0>]
> (ohci_da8xx_reset+0x1c/0xd8)
>  r5:00000000 r4:c3af6000
> [<c03224d4>] (ohci_da8xx_reset) from [<c0309118>] (usb_add_hcd+0x314/0x834)
>  r7:0000003b r6:ffffffed r5:00000000 r4:c3af6000
> [<c0308e04>] (usb_add_hcd) from [<c032200c>] (ohci_da8xx_probe+0x14c/0x21c)
>  r9:c06a5ed4 r8:00000000 r7:c38bd010 r6:c38bd000 r5:c3af6000 r4:c38af900
> [<c0321ec0>] (ohci_da8xx_probe) from [<c02bec44>]
> (platform_drv_probe+0x40/0x8c)
>  r7:c06e610c r6:c06e610c r5:c38bd010 r4:c0321ec0
> [<c02bec04>] (platform_drv_probe) from [<c02bd438>]
> (driver_probe_device+0x138/0x2a4)
>  r7:c06e610c r6:c0e9db10 r5:00000000 r4:c38bd010
> [<c02bd300>] (driver_probe_device) from [<c02bd634>]
> (__driver_attach+0x90/0xb4)
>  r9:c06a5ed4 r8:c06f66e0 r7:c06e1250 r6:c06e610c r5:c38bd044 r4:c38bd010
> [<c02bd5a4>] (__driver_attach) from [<c02bba5c>]
> (bus_for_each_dev+0x74/0x98)
>  r7:c06e1250 r6:c02bd5a4 r5:c06e610c r4:00000000
> [<c02bb9e8>] (bus_for_each_dev) from [<c02bcf2c>] (driver_attach+0x20/0x28)
>  r6:c39a2300 r5:00000000 r4:c06e610c
> [<c02bcf0c>] (driver_attach) from [<c02bca80>] (bus_add_driver+0xd4/0x1f0)
> [<c02bc9ac>] (bus_add_driver) from [<c02bdcec>] (driver_register+0xa4/0xe8)
>  r7:c0698850 r6:00000000 r5:00000000 r4:c06e610c
> [<c02bdc48>] (driver_register) from [<c02bebac>]
> (__platform_driver_register+0x38/0x4c)
>  r5:00000000 r4:c06acce4
> [<c02beb74>] (__platform_driver_register) from [<c068da20>]
> (ohci_da8xx_init+0x64/0x90)
> [<c068d9bc>] (ohci_da8xx_init) from [<c00096c0>]
> (do_one_initcall+0xb0/0x168)
>  r5:c068d9bc r4:ffffe000
> [<c0009610>] (do_one_initcall) from [<c0676e14>]
> (kernel_init_freeable+0x110/0x1cc)
>  r9:c06a5ed4 r8:c06f66e0 r7:c0698850 r6:000000c1 r5:c06f66e0 r4:00000007
> [<c0676d04>] (kernel_init_freeable) from [<c04d37e4>]
> (kernel_init+0x10/0xf8)
>  r9:00000000 r8:00000000 r7:00000000 r6:00000000 r5:c04d37d4 r4:00000000
> [<c04d37d4>] (kernel_init) from [<c000a2ac>] (ret_from_fork+0x14/0x28)
>  r5:c04d37d4 r4:00000000
> BUG: spinlock lockup suspected on CPU#0, swapper/1
>  lock: clockfw_lock+0x0/0x20, .magic: dead4ead, .owner: swapper/1,
> .owner_cpu: 0
> CPU: 0 PID: 1 Comm: swapper Not tainted 4.9.0-rc8-dlech-ev3-lms2012+ #22
> Hardware name: Generic DA850/OMAP-L138/AM18x
> Backtrace:
> [<c000d824>] (dump_backtrace) from [<c000dac4>] (show_stack+0x18/0x1c)
>  r7:00000000 r6:04605000 r5:c06b09a0 r4:c3838000
> [<c000daac>] (show_stack) from [<c02393dc>] (dump_stack+0x20/0x28)
> [<c02393bc>] (dump_stack) from [<c004f140>] (spin_dump+0x80/0x94)
> [<c004f0c0>] (spin_dump) from [<c004f2c4>] (do_raw_spin_lock+0xdc/0x11c)
>  r5:00000000 r4:c06b09a0
> [<c004f1e8>] (do_raw_spin_lock) from [<c04d8fb4>]
> (_raw_spin_lock_irqsave+0x68/0x74)
>  r10:00000000 r9:c06a5ed4 r8:00000000 r7:0000003b r6:c0014884 r5:80000093
>  r4:c06b09a0 r3:c3838000
> [<c04d8f4c>] (_raw_spin_lock_irqsave) from [<c0014884>]
> (clk_enable+0x24/0x50)
>  r6:c06f69f4 r5:0001ef02 r4:c06b3398
> [<c0014860>] (clk_enable) from [<c0015c74>]
> (usb20_phy_clk_enable+0x24/0xb8)
>  r5:0001ef02 r4:c06f69f0
> [<c0015c50>] (usb20_phy_clk_enable) from [<c0014858>]
> (__clk_enable+0x74/0x7c)
>  r9:c06a5ed4 r8:00000000 r7:0000003b r6:c3a6ca00 r5:80000013 r4:c06b7ee8
> [<c00147e4>] (__clk_enable) from [<c0014808>] (__clk_enable+0x24/0x7c)
>  r4:c06b8648
> [<c00147e4>] (__clk_enable) from [<c0014890>] (clk_enable+0x30/0x50)
>  r4:c06b8648
> [<c0014860>] (clk_enable) from [<c025f43c>]
> (da8xx_usb11_phy_power_on+0x30/0x80)
>  r5:c3a54050 r4:c06b8648
> [<c025f40c>] (da8xx_usb11_phy_power_on) from [<c025ee3c>]
> (phy_power_on+0x80/0xe4)
>  r5:c3a6c800 r4:fffffdf4
> [<c025edbc>] (phy_power_on) from [<c032243c>] (ohci_da8xx_enable+0x4c/0x80)
>  r7:0000003b r6:c3af6000 r5:c3af6000 r4:00000000
> [<c03223f0>] (ohci_da8xx_enable) from [<c03224f0>]
> (ohci_da8xx_reset+0x1c/0xd8)
>  r5:00000000 r4:c3af6000
> [<c03224d4>] (ohci_da8xx_reset) from [<c0309118>] (usb_add_hcd+0x314/0x834)
>  r7:0000003b r6:ffffffed r5:00000000 r4:c3af6000
> [<c0308e04>] (usb_add_hcd) from [<c032200c>] (ohci_da8xx_probe+0x14c/0x21c)
>  r9:c06a5ed4 r8:00000000 r7:c38bd010 r6:c38bd000 r5:c3af6000 r4:c38af900
> [<c0321ec0>] (ohci_da8xx_probe) from [<c02bec44>]
> (platform_drv_probe+0x40/0x8c)
>  r7:c06e610c r6:c06e610c r5:c38bd010 r4:c0321ec0
> [<c02bec04>] (platform_drv_probe) from [<c02bd438>]
> (driver_probe_device+0x138/0x2a4)
>  r7:c06e610c r6:c0e9db10 r5:00000000 r4:c38bd010
> [<c02bd300>] (driver_probe_device) from [<c02bd634>]
> (__driver_attach+0x90/0xb4)
>  r9:c06a5ed4 r8:c06f66e0 r7:c06e1250 r6:c06e610c r5:c38bd044 r4:c38bd010
> [<c02bd5a4>] (__driver_attach) from [<c02bba5c>]
> (bus_for_each_dev+0x74/0x98)
>  r7:c06e1250 r6:c02bd5a4 r5:c06e610c r4:00000000
> [<c02bb9e8>] (bus_for_each_dev) from [<c02bcf2c>] (driver_attach+0x20/0x28)
>  r6:c39a2300 r5:00000000 r4:c06e610c
> [<c02bcf0c>] (driver_attach) from [<c02bca80>] (bus_add_driver+0xd4/0x1f0)
> [<c02bc9ac>] (bus_add_driver) from [<c02bdcec>] (driver_register+0xa4/0xe8)
>  r7:c0698850 r6:00000000 r5:00000000 r4:c06e610c
> [<c02bdc48>] (driver_register) from [<c02bebac>]
> (__platform_driver_register+0x38/0x4c)
>  r5:00000000 r4:c06acce4
> [<c02beb74>] (__platform_driver_register) from [<c068da20>]
> (ohci_da8xx_init+0x64/0x90)
> [<c068d9bc>] (ohci_da8xx_init) from [<c00096c0>]
> (do_one_initcall+0xb0/0x168)
>  r5:c068d9bc r4:ffffe000
> [<c0009610>] (do_one_initcall) from [<c0676e14>]
> (kernel_init_freeable+0x110/0x1cc)
>  r9:c06a5ed4 r8:c06f66e0 r7:c0698850 r6:000000c1 r5:c06f66e0 r4:00000007
> [<c0676d04>] (kernel_init_freeable) from [<c04d37e4>]
> (kernel_init+0x10/0xf8)
>  r9:00000000 r8:00000000 r7:00000000 r6:00000000 r5:c04d37d4 r4:00000000
> [<c04d37d4>] (kernel_init) from [<c000a2ac>] (ret_from_fork+0x14/0x28)
>  r5:c04d37d4 r4:00000000

^ permalink raw reply

* [PATCH 2/2] arm64: alternatives: Work around NOP generation with broken assembler
From: Will Deacon @ 2016-12-05 10:05 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <20161203140538.23525-3-marc.zyngier@arm.com>

On Sat, Dec 03, 2016 at 02:05:38PM +0000, Marc Zyngier wrote:
> When compiling a .inst directive in an alternative clause with
> a rather old version of gas (circa 2.24), and when used with
> the alternative_else_nop_endif feature, the compilation fails
> with a rather cryptic message such as:
> 
> arch/arm64/lib/clear_user.S:33: Error: bad or irreducible absolute expression
> 
> which is caused by the bug described in eb7c11ee3c5c ("arm64:
> alternative: Work around .inst assembler bugs").
> 
> This effectively prevents the use of the "nops" macro, which
> requires the number of instruction as a parameter (the assembler
> is confused and unable to compute the difference between two labels).
> 
> As an alternative(!), use the .fill directive to output the number
> of required NOPs (.fill has the good idea to output the fill pattern
> in the endianness of the instruction stream).

Are you sure about that? The gas docs say:

`The contents of each repeat bytes is taken from an 8-byte number. The
 highest order 4 bytes are zero. The lowest order 4 bytes are value rendered
 in the byte-order of an integer on the computer as is assembling for.'

and I'd expect "integer" to refer to data values, rather than instructions.

> 
> Fixes: 792d47379f4d ("arm64: alternative: add auto-nop infrastructure")
> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
> ---
>  arch/arm64/include/asm/alternative.h | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/alternative.h
> index 39feb85..39f8c98 100644
> --- a/arch/arm64/include/asm/alternative.h
> +++ b/arch/arm64/include/asm/alternative.h
> @@ -159,9 +159,19 @@ void apply_alternatives(void *start, size_t length);
>   * of NOPs. The number of NOPs is chosen automatically to match the
>   * previous case.
>   */
> +
> +#define NOP_INST	0xd503201f

If we define this, let's put it in insn.h or something. It could even be
used in the vdso linker script, which open codes this constant.

> +
>  .macro alternative_else_nop_endif
>  alternative_else
> -	nops	(662b-661b) / AARCH64_INSN_SIZE
> +	/*
> +	 * The same assembler bug strikes again here if the first half
> +	 * of the alternative sequence contains a .inst, leading to a
> +	 * bizarre error message. Fortulately, .fill replaces the

Fortunately

> +	 * "nops" macro by inserting padding with the target machine
> +	 * endianness.
> +	 */
> +	.fill	(662b-661b) / AARCH64_INSN_SIZE, AARCH64_INSN_SIZE, NOP_INST

Assuming we can get .fill to do the right thing, we should probably use
it in __nops rather than open-coding it here. Or is the issue that we're
unable to pass (662b-661b) / AARCH64_INSN_SIZE to any macro?

Will

^ permalink raw reply

* [PATCH 2/8] dt-bindings: document the STM32 RTC bindings
From: Alexandre Belloni @ 2016-12-05 10:06 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1480687801-19525-4-git-send-email-amelie.delaunay@st.com>

Hi,

On 02/12/2016 at 15:09:55 +0100, Amelie Delaunay wrote :
> This patch adds documentation of device tree bindings for the STM32 RTC.
> 
> Signed-off-by: Amelie Delaunay <amelie.delaunay@st.com>
> ---
>  .../devicetree/bindings/rtc/st,stm32-rtc.txt       | 31 ++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/rtc/st,stm32-rtc.txt
> 
> diff --git a/Documentation/devicetree/bindings/rtc/st,stm32-rtc.txt b/Documentation/devicetree/bindings/rtc/st,stm32-rtc.txt
> new file mode 100644
> index 0000000..4578838
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/rtc/st,stm32-rtc.txt
> @@ -0,0 +1,31 @@
> +STM32 Real Time Clock
> +
> +Required properties:
> +- compatible: "st,stm32-rtc".
> +- reg: address range of rtc register set.
> +- clocks: reference to the clock entry ck_rtc.
> +- clock-names: name of the clock used. Should be "ck_rtc".

Is this name really useful?

> +- interrupt-parent: phandle for the interrupt controller.
> +- interrupts: rtc alarm interrupt.
> +- interrupt-names: rtc alarm interrupt name, should be "alarm".

Same comment, is this name really useful?


-- 
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply

* ILP32 for ARM64: testing with glibc testsuite
From: Andreas Schwab @ 2016-12-05 10:07 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <ff3b973b-3887-7ae8-5a27-4d1ad115055f@huawei.com>

On Dez 05 2016, "Zhangjian (Bamvor)" <bamvor.zhangjian@huawei.com> wrote:

> Is there some progresses on it? We could collabrate to fix those issues.

All the elf/nptl/rt fails should be fixed by the recent binutils fixes.

Andreas.

-- 
Andreas Schwab, SUSE Labs, schwab at suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

^ permalink raw reply


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox