/*
    ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    STM32L1xx/hal_lld.h
 * @brief   STM32L1xx HAL subsystem low level driver header.
 * @pre     This module requires the following macros to be defined in the
 *          @p board.h file:
 *          - STM32_LSECLK.
 *          - STM32_HSECLK.
 *          - STM32_HSE_BYPASS (optionally).
 *          .
 *          One of the following macros must also be defined:
 *          - STM32L1XX_MD for Ultra Low Power Medium-density devices.
 *          - STM32L1XX_MDP for Ultra Low Power Medium-density Plus devices.
 *          - STM32L1XX_HD for Ultra Low Power Medium-density devices.
 *          .
 *
 * @addtogroup HAL
 * @{
 */

#ifndef _HAL_LLD_H_
#define _HAL_LLD_H_

#include "stm32_registry.h"

/*===========================================================================*/
/* Driver constants.                                                         */
/*===========================================================================*/

/**
 * @name    Platform identification
 * @{
 */
#define PLATFORM_NAME           "STM32L1xx Ultra Low Power Medium Density"
/** @} */

/**
 * @name    Internal clock sources
 * @{
 */
#define STM32_HSICLK            16000000    /**< High speed internal clock. */
#define STM32_LSICLK            38000       /**< Low speed internal clock.  */
/** @} */

/**
 * @name    PWR_CR register bits definitions
 * @{
 */
#define STM32_VOS_MASK          (3 << 11)   /**< Core voltage mask.         */
#define STM32_VOS_1P8           (1 << 11)   /**< Core voltage 1.8 Volts.    */
#define STM32_VOS_1P5           (2 << 11)   /**< Core voltage 1.5 Volts.    */
#define STM32_VOS_1P2           (3 << 11)   /**< Core voltage 1.2 Volts.    */

#define STM32_PLS_MASK          (7 << 5)    /**< PLS bits mask.             */
#define STM32_PLS_LEV0          (0 << 5)    /**< PVD level 0.               */
#define STM32_PLS_LEV1          (1 << 5)    /**< PVD level 1.               */
#define STM32_PLS_LEV2          (2 << 5)    /**< PVD level 2.               */
#define STM32_PLS_LEV3          (3 << 5)    /**< PVD level 3.               */
#define STM32_PLS_LEV4          (4 << 5)    /**< PVD level 4.               */
#define STM32_PLS_LEV5          (5 << 5)    /**< PVD level 5.               */
#define STM32_PLS_LEV6          (6 << 5)    /**< PVD level 6.               */
#define STM32_PLS_LEV7          (7 << 5)    /**< PVD level 7.               */
/** @} */

/**
 * @name    RCC_CR register bits definitions
 * @{
 */
#define STM32_RTCPRE_MASK       (3 << 29)   /**< RTCPRE mask.               */
#define STM32_RTCPRE_DIV2       (0 << 29)   /**< HSE divided by 2.          */
#define STM32_RTCPRE_DIV4       (1 << 29)   /**< HSE divided by 4.          */
#define STM32_RTCPRE_DIV8       (2 << 29)   /**< HSE divided by 2.          */
#define STM32_RTCPRE_DIV16      (3 << 29)   /**< HSE divided by 16.         */
/** @} */

/**
 * @name    RCC_CFGR register bits definitions
 * @{
 */
#define STM32_SW_MSI            (0 << 0)    /**< SYSCLK source is MSI.      */
#define STM32_SW_HSI            (1 << 0)    /**< SYSCLK source is HSI.      */
#define STM32_SW_HSE            (2 << 0)    /**< SYSCLK source is HSE.      */
#define STM32_SW_PLL            (3 << 0)    /**< SYSCLK source is PLL.      */

#define STM32_HPRE_DIV1         (0 << 4)    /**< SYSCLK divided by 1.       */
#define STM32_HPRE_DIV2         (8 << 4)    /**< SYSCLK divided by 2.       */
#define STM32_HPRE_DIV4         (9 << 4)    /**< SYSCLK divided by 4.       */
#define STM32_HPRE_DIV8         (10 << 4)   /**< SYSCLK divided by 8.       */
#define STM32_HPRE_DIV16        (11 << 4)   /**< SYSCLK divided by 16.      */
#define STM32_HPRE_DIV64        (12 << 4)   /**< SYSCLK divided by 64.      */
#define STM32_HPRE_DIV128       (13 << 4)   /**< SYSCLK divided by 128.     */
#define STM32_HPRE_DIV256       (14 << 4)   /**< SYSCLK divided by 256.     */
#define STM32_HPRE_DIV512       (15 << 4)   /**< SYSCLK divided by 512.     */

#define STM32_PPRE1_DIV1        (0 << 8)    /**< HCLK divided by 1.         */
#define STM32_PPRE1_DIV2        (4 << 8)    /**< HCLK divided by 2.         */
#define STM32_PPRE1_DIV4        (5 << 8)    /**< HCLK divided by 4.         */
#define STM32_PPRE1_DIV8        (6 << 8)    /**< HCLK divided by 8.         */
#define STM32_PPRE1_DIV16       (7 << 8)    /**< HCLK divided by 16.        */

#define STM32_PPRE2_DIV1        (0 << 11)   /**< HCLK divided by 1.         */
#define STM32_PPRE2_DIV2        (4 << 11)   /**< HCLK divided by 2.         */
#define STM32_PPRE2_DIV4        (5 << 11)   /**< HCLK divided by 4.         */
#define STM32_PPRE2_DIV8        (6 << 11)   /**< HCLK divided by 8.         */
#define STM32_PPRE2_DIV16       (7 << 11)   /**< HCLK divided by 16.        */

#define STM32_PLLSRC_HSI        (0 << 16)   /**< PLL clock source is HSI.   */
#define STM32_PLLSRC_HSE        (1 << 16)   /**< PLL clock source is HSE.   */

#define STM32_MCOSEL_NOCLOCK    (0 << 24)   /**< No clock on MCO pin.       */
#define STM32_MCOSEL_SYSCLK     (1 << 24)   /**< SYSCLK on MCO pin.         */
#define STM32_MCOSEL_HSI        (2 << 24)   /**< HSI clock on MCO pin.      */
#define STM32_MCOSEL_MSI        (3 << 24)   /**< MSI clock on MCO pin.      */
#define STM32_MCOSEL_HSE        (4 << 24)   /**< HSE clock on MCO pin.      */
#define STM32_MCOSEL_PLL        (5 << 24)   /**< PLL clock on MCO pin.      */
#define STM32_MCOSEL_LSI        (6 << 24)   /**< LSI clock on MCO pin.      */
#define STM32_MCOSEL_LSE        (7 << 24)   /**< LSE clock on MCO pin.      */

#define STM32_MCOPRE_DIV1       (0 << 28)   /**< MCO divided by 1.          */
#define STM32_MCOPRE_DIV2       (1 << 28)   /**< MCO divided by 1.          */
#define STM32_MCOPRE_DIV4       (2 << 28)   /**< MCO divided by 1.          */
#define STM32_MCOPRE_DIV8       (3 << 28)   /**< MCO divided by 1.          */
#define STM32_MCOPRE_DIV16      (4 << 28)   /**< MCO divided by 1.          */
/** @} */

/**
 * @name    RCC_ICSCR register bits definitions
 * @{
 */
#define STM32_MSIRANGE_MASK     (7 << 13)   /**< MSIRANGE field mask.       */
#define STM32_MSIRANGE_64K      (0 << 13)   /**< 64kHz nominal.             */
#define STM32_MSIRANGE_128K     (1 << 13)   /**< 128kHz nominal.            */
#define STM32_MSIRANGE_256K     (2 << 13)   /**< 256kHz nominal.            */
#define STM32_MSIRANGE_512K     (3 << 13)   /**< 512kHz nominal.            */
#define STM32_MSIRANGE_1M       (4 << 13)   /**< 1MHz nominal.              */
#define STM32_MSIRANGE_2M       (5 << 13)   /**< 2MHz nominal.              */
#define STM32_MSIRANGE_4M       (6 << 13)   /**< 4MHz nominal               */
/** @} */

/**
 * @name    RCC_CSR register bits definitions
 * @{
 */
#define STM32_RTCSEL_MASK       (3 << 16)   /**< RTC source mask.           */
#define STM32_RTCSEL_NOCLOCK    (0 << 16)   /**< No RTC source.             */
#define STM32_RTCSEL_LSE        (1 << 16)   /**< RTC source is LSE.         */
#define STM32_RTCSEL_LSI        (2 << 16)   /**< RTC source is LSI.         */
#define STM32_RTCSEL_HSEDIV     (3 << 16)   /**< RTC source is HSE divided. */
/** @} */

/*===========================================================================*/
/* Driver pre-compile time settings.                                         */
/*===========================================================================*/

/**
 * @name    Configuration options
 * @{
 */
/**
 * @brief   Disables the PWR/RCC initialization in the HAL.
 */
#if !defined(STM32_NO_INIT) || defined(__DOXYGEN__)
#define STM32_NO_INIT               FALSE
#endif

/**
 * @brief   Core voltage selection.
 * @note    This setting affects all the performance and clock related
 *          settings, the maximum performance is only obtainable selecting
 *          the maximum voltage.
 */
#if !defined(STM32_VOS) || defined(__DOXYGEN__)
#define STM32_VOS                   STM32_VOS_1P8
#endif

/**
 * @brief   Enables or disables the programmable voltage detector.
 */
#if !defined(STM32_PVD_ENABLE) || defined(__DOXYGEN__)
#define STM32_PVD_ENABLE            FALSE
#endif

/**
 * @brief   Sets voltage level for programmable voltage detector.
 */
#if !defined(STM32_PLS) || defined(__DOXYGEN__)
#define STM32_PLS                   STM32_PLS_LEV0
#endif

/**
 * @brief   Enables or disables the HSI clock source.
 */
#if !defined(STM32_HSI_ENABLED) || defined(__DOXYGEN__)
#define STM32_HSI_ENABLED           TRUE
#endif

/**
 * @brief   Enables or disables the LSI clock source.
 */
#if !defined(STM32_LSI_ENABLED) || defined(__DOXYGEN__)
#define STM32_LSI_ENABLED           TRUE
#endif

/**
 * @brief   Enables or disables the HSE clock source.
 */
#if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
#define STM32_HSE_ENABLED           FALSE
#endif

/**
 * @brief   Enables or disables the LSE clock source.
 */
#if !defined(STM32_HSE_ENABLED) || defined(__DOXYGEN__)
#define STM32_LSE_ENABLED           FALSE
#endif

/**
 * @brief   ADC clock setting.
 */
#if !defined(STM32_ADC_CLOCK_ENABLED) || defined(__DOXYGEN__)
#define STM32_ADC_CLOCK_ENABLED     TRUE
#endif

/**
 * @brief   USB clock setting.
 */
#if !defined(STM32_USB_CLOCK_ENABLED) || defined(__DOXYGEN__)
#define STM32_USB_CLOCK_ENABLED     TRUE
#endif

/**
 * @brief   MSI frequency setting.
 */
#if !defined(STM32_MSIRANGE) || defined(__DOXYGEN__)
#define STM32_MSIRANGE              STM32_MSIRANGE_2M
#endif

/**
 * @brief   Main clock source selection.
 * @note    If the selected clock source is not the PLL then the PLL is not
 *          initialized and started.
 * @note    The default value is calculated for a 32MHz system clock from
 *          the internal 16MHz HSI clock.
 */
#if !defined(STM32_SW) || defined(__DOXYGEN__)
#define STM32_SW                    STM32_SW_PLL
#endif

/**
 * @brief   Clock source for the PLL.
 * @note    This setting has only effect if the PLL is selected as the
 *          system clock source.
 * @note    The default value is calculated for a 32MHz system clock from
 *          the internal 16MHz HSI clock.
 */
#if !defined(STM32_PLLSRC) || defined(__DOXYGEN__)
#define STM32_PLLSRC                STM32_PLLSRC_HSI
#endif

/**
 * @brief   PLL multiplier value.
 * @note    The allowed values are 3, 4, 6, 8, 12, 16, 32, 48.
 * @note    The default value is calculated for a 32MHz system clock from
 *          the internal 16MHz HSI clock.
 */
#if !defined(STM32_PLLMUL_VALUE) || defined(__DOXYGEN__)
#define STM32_PLLMUL_VALUE          6
#endif

/**
 * @brief   PLL divider value.
 * @note    The allowed values are 2, 3, 4.
 * @note    The default value is calculated for a 32MHz system clock from
 *          the internal 16MHz HSI clock.
 */
#if !defined(STM32_PLLDIV_VALUE) || defined(__DOXYGEN__)
#define STM32_PLLDIV_VALUE          3
#endif

/**
 * @brief   AHB prescaler value.
 * @note    The default value is calculated for a 32MHz system clock from
 *          the internal 16MHz HSI clock.
 */
#if !defined(STM32_HPRE) || defined(__DOXYGEN__)
#define STM32_HPRE                  STM32_HPRE_DIV1
#endif

/**
 * @brief   APB1 prescaler value.
 */
#if !defined(STM32_PPRE1) || defined(__DOXYGEN__)
#define STM32_PPRE1                 STM32_PPRE1_DIV1
#endif

/**
 * @brief   APB2 prescaler value.
 */
#if !defined(STM32_PPRE2) || defined(__DOXYGEN__)
#define STM32<style>pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span><span class="c">#</span>
<span class="c"># This is free software, licensed under the GNU General Public License v2.</span>
<span class="c"># See /LICENSE for more information.</span>
<span class="c">#</span>
<span class="cp">include $(TOPDIR)/rules.mk</span>
<span class="cp">include $(INCLUDE_DIR)/image.mk</span>

<span class="nv">DEVICE_VARS</span> <span class="o">+=</span> IMAGE_SIZE DTB_SIZE

<span class="cp">define Build/boot-img</span>
	<span class="k">$(</span>RM<span class="k">)</span> -rf <span class="nv">$@</span>.bootdir
	mkdir -p <span class="nv">$@</span>.bootdir/boot

	<span class="k">$(</span>CP<span class="k">)</span> <span class="nv">$@</span>.scr <span class="nv">$@</span>.bootdir/boot/boot.scr
	<span class="k">$(</span>CP<span class="k">)</span> <span class="k">$(</span>IMAGE_KERNEL<span class="k">)</span>.dtb <span class="nv">$@</span>.bootdir/boot/<span class="k">$(</span>DEVICE_DTB<span class="k">)</span>
	<span class="k">$(</span>CP<span class="k">)</span> <span class="k">$(</span>IMAGE_KERNEL<span class="k">)</span> <span class="nv">$@</span>.bootdir/boot/uImage

	genext2fs --block-size <span class="k">$(</span>BLOCKSIZE:%k<span class="o">=</span>%Ki<span class="k">)</span> --size-in-blocks <span class="nv">$$</span><span class="o">((</span><span class="m">1024</span> * <span class="k">$(</span>BOOT_SIZE<span class="k">)</span><span class="o">))</span> --root <span class="nv">$@</span>.bootdir <span class="nv">$@</span>.boot

<span class="c">	# convert it to revision 1 - needed for u-boot ext2load</span>
	<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/tune2fs -O filetype <span class="nv">$@</span>.boot
	<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/e2fsck -pDf <span class="nv">$@</span>.boot &gt; /dev/null
<span class="cp">endef</span>

<span class="cp">define Build/boot-script</span>
	<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/mkimage -A powerpc -T script -C none -n <span class="s2">&quot;</span><span class="k">$(</span>PROFILE<span class="k">)</span><span class="s2"> Boot Script&quot;</span> <span class="se">\</span>
		-d mbl_boot.scr <span class="se">\</span>
		<span class="nv">$@</span>.scr
<span class="cp">endef</span>

<span class="cp">define Build/copy-file</span>
	cat <span class="s2">&quot;</span><span class="k">$(</span><span class="m">1</span><span class="k">)</span><span class="s2">&quot;</span> &gt; <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span>
<span class="cp">endef</span>

<span class="cp">define Build/create-uImage-dtb</span>
<span class="c">	# flat_dt target expect FIT image - which WNDR4700&#39;s uboot doesn&#39;t support</span>
	-<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/mkimage -A <span class="k">$(</span>LINUX_KARCH<span class="k">)</span> <span class="se">\</span>
		-O linux -T kernel -C none <span class="se">\</span>
		-n <span class="s1">&#39;$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION)&#39;</span> <span class="se">\</span>
		-d <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">.dtb&quot;</span> <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">.dtb.uimage&quot;</span>
<span class="cp">endef</span>

<span class="cp">define Build/dtb</span>
	<span class="k">$(</span>call Image/BuildDTB,../dts/<span class="k">$(</span>DEVICE_DTS<span class="k">)</span>.dts,<span class="nv">$@</span>.dtb,,--space <span class="k">$(</span>DTB_SIZE<span class="k">))</span>
<span class="cp">endef</span>

<span class="cp">define Build/export-dtb</span>
	cp <span class="k">$(</span>IMAGE_KERNEL<span class="k">)</span>.dtb <span class="nv">$@</span>
<span class="cp">endef</span>

<span class="cp">define Build/hdd-img</span>
	./mbl_gen_hdd_img.sh <span class="nv">$@</span> <span class="nv">$@</span>.boot <span class="k">$(</span>IMAGE_ROOTFS<span class="k">)</span> <span class="k">$(</span>BOOT_SIZE<span class="k">)</span> <span class="k">$(</span>CONFIG_TARGET_ROOTFS_PARTSIZE<span class="k">)</span>
<span class="cp">endef</span>

<span class="cp">define Build/MerakiAdd-dtb</span>
	<span class="k">$(</span>call Image/BuildDTB,../dts/<span class="k">$(</span>DEVICE_DTS<span class="k">)</span>.dts,<span class="nv">$@</span>.dtb<span class="k">)</span>
	<span class="o">(</span> <span class="se">\</span>
		dd <span class="k">if</span><span class="o">=</span><span class="nv">$@</span>.dtb <span class="nv">bs</span><span class="o">=</span><span class="k">$(</span>DTB_SIZE<span class="k">)</span> <span class="nv">conv</span><span class="o">=</span>sync<span class="p">;</span> <span class="se">\</span>
		dd <span class="k">if</span><span class="o">=</span><span class="nv">$@</span> <span class="nv">bs</span><span class="o">=</span><span class="k">$(</span>BLOCKSIZE<span class="k">)</span> <span class="nv">conv</span><span class="o">=</span>sync<span class="p">;</span> <span class="se">\</span>
	<span class="o">)</span> &gt; <span class="nv">$@</span>.new
	@mv <span class="nv">$@</span>.new <span class="nv">$@</span>
<span class="cp">endef</span>

<span class="cp">define Build/MerakiNAND</span>
	-<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/mkmerakifw <span class="se">\</span>
		-B <span class="k">$(</span>BOARD_NAME<span class="k">)</span> -s <span class="se">\</span>
		-i <span class="nv">$@</span> <span class="se">\</span>
		-o <span class="nv">$@</span>.new
	@cp <span class="nv">$@</span>.new <span class="nv">$@</span>
<span class="cp">endef</span>

<span class="cp">define Build/MuImage-initramfs</span>
	rm -rf <span class="nv">$@</span>.fakerd <span class="nv">$@</span>.new

	dd <span class="k">if</span><span class="o">=</span>/dev/zero <span class="nv">of</span><span class="o">=</span><span class="nv">$@</span>.fakerd <span class="nv">bs</span><span class="o">=</span><span class="m">32</span> <span class="nv">count</span><span class="o">=</span><span class="m">1</span> <span class="nv">conv</span><span class="o">=</span>sync

<span class="c">	# Netgear used an old uboot that doesn&#39;t have FIT support.</span>
<span class="c">	# So we are stuck with either a full ext2/4 fs in a initrd.</span>
<span class="c">	# ... or we try to make the &quot;multi&quot; image approach to work</span>
<span class="c">	# for us.</span>
<span class="c">	#</span>
<span class="c">	# Sadly, the &quot;multi&quot; image has to consists of three</span>
<span class="c">	# &quot;fixed&quot; parts in the following &quot;fixed&quot; order:</span>
<span class="c">	# 1. The kernel which is in $@</span>
<span class="c">	# 2. The (fake) initrd which is in $@.fakerd</span>
<span class="c">	# 3. The device tree binary which is in $@.dtb</span>
<span class="c">	#</span>
<span class="c">	# Now, given that we use the function for the kernel which</span>
<span class="c">	# already has a initramfs image inside, we still have to</span>
<span class="c">	# add a &quot;fake&quot; initrd (which a mkimage header) in the second</span>
<span class="c">	# part of the legacy multi image. Since we need to put the</span>
<span class="c">	# device tree stuff into part 3.</span>

	-<span class="k">$(</span>STAGING_DIR_HOST<span class="k">)</span>/bin/mkimage -A <span class="k">$(</span>LINUX_KARCH<span class="k">)</span> -O linux -T multi <span class="se">\</span>
		-C <span class="k">$(</span><span class="m">1</span><span class="k">)</span> -a <span class="k">$(</span>KERNEL_LOADADDR<span class="k">)</span> -e <span class="k">$(</span>KERNEL_ENTRY<span class="k">)</span> <span class="se">\</span>
		-n <span class="s1">&#39;$(BOARD_NAME) initramfs&#39;</span> -d <span class="nv">$@</span>:<span class="nv">$@</span>.fakerd:<span class="nv">$@</span>.dtb <span class="nv">$@</span>.new
	mv <span class="nv">$@</span>.new <span class="nv">$@</span>
	rm -rf <span class="nv">$@</span>.fakerd
<span class="cp">endef</span>

<span class="cp">define Build/prepend-dtb</span>
	cat <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">.dtb.uimage&quot;</span> <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span> &gt; <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">.new&quot;</span>
	mv <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">.new&quot;</span> <span class="s2">&quot;</span><span class="nv">$@</span><span class="s2">&quot;</span>
<span class="cp">endef</span>

<span class="cp">define Image/cpiogz</span>
	<span class="o">(</span> <span class="nb">cd</span> <span class="k">$(</span>TARGET_DIR<span class="k">)</span><span class="p">;</span> find . <span class="p">|</span> cpio -o -H newc <span class="p">|</span> gzip -9n &gt;<span class="k">$(</span>KDIR_TMP<span class="k">)</span>/<span class="k">$(</span>IMG_PREFIX<span class="k">)</span>-rootfs.cpio.gz <span class="o">)</span>
<span class="cp">endef</span>

<span class="cp">define Device/Default</span>
  PROFILES :<span class="o">=</span> Default
  <span class="nv">KERNEL_DEPENDS</span> <span class="o">=</span> <span class="nv">$$</span><span class="o">(</span>wildcard ../dts/<span class="nv">$$</span><span class="o">(</span>DEVICE_DTS<span class="o">)</span>.dts<span class="o">)</span>
  DEVICE_DTS :<span class="o">=</span>
  KERNEL_ENTRY :<span class="o">=</span> 0x00000000
  KERNEL_LOADADDR :<span class="o">=</span> 0x00000000
  DEVICE_DTS_DIR :<span class="o">=</span> ../dts
  <span class="nv">SUPPORTED_DEVICES</span> <span class="o">=</span> <span class="k">$(</span>subst _,<span class="k">$(</span>comma<span class="k">)</span>,<span class="k">$(</span><span class="m">1</span><span class="k">))</span>
<span class="cp">endef</span>

<span class="cp">ifeq ($(SUBTARGET),nand)</span>

<span class="cp">define Device/meraki_mr24</span>
  DEVICE_TITLE :<span class="o">=</span> Cisco Meraki MR24
  DEVICE_PACKAGES :<span class="o">=</span> kmod-spi-gpio -swconfig
  BOARD_NAME :<span class="o">=</span> mr24
  DEVICE_DTS :<span class="o">=</span> meraki-mr24
  BLOCKSIZE :<span class="o">=</span> 63k
  IMAGES :<span class="o">=</span> sysupgrade.tar
  DTB_SIZE :<span class="o">=</span> <span class="m">64512</span>
  IMAGE_SIZE :<span class="o">=</span> 8191k
  KERNEL :<span class="o">=</span> kernel-bin <span class="p">|</span> lzma <span class="p">|</span> uImage lzma <span class="p">|</span> MerakiAdd-dtb <span class="p">|</span> MerakiNAND
  KERNEL_INITRAMFS :<span class="o">=</span> kernel-bin <span class="p">|</span> lzma <span class="p">|</span> dtb <span class="p">|</span> MuImage-initramfs lzma
  IMAGE/sysupgrade.tar :<span class="o">=</span> sysupgrade-tar <span class="p">|</span> append-metadata
  UBINIZE_OPTS :<span class="o">=</span> -E <span class="m">5</span>
  <span class="nv">SUPPORTED_DEVICES</span> <span class="o">+=</span> mr24
<span class="cp">endef</span>
<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> meraki_mr24

<span class="cp">define Device/meraki_mx60</span>
  DEVICE_TITLE :<span class="o">=</span> Cisco Meraki MX60/MX60W
  DEVICE_PACKAGES :<span class="o">=</span> kmod-spi-gpio kmod-usb-ledtrig-usbport kmod-usb-dwc2 <span class="se">\</span>
		     kmod-usb-storage block-mount
  BOARD_NAME :<span class="o">=</span> mx60
  DEVICE_DTS :<span class="o">=</span> meraki-mx60
  BLOCKSIZE :<span class="o">=</span> 63k
  IMAGES :<span class="o">=</span> sysupgrade.tar
  DTB_SIZE :<span class="o">=</span> <span class="m">64512</span>
  IMAGE_SIZE :<span class="o">=</span> 1021m
  KERNEL_SIZE :<span class="o">=</span> 4031k
  KERNEL :<span class="o">=</span> kernel-bin <span class="p">|</span> gzip <span class="p">|</span> uImage gzip <span class="p">|</span> MerakiAdd-dtb <span class="p">|</span> MerakiNAND
  KERNEL_INITRAMFS :<span class="o">=</span> kernel-bin <span class="p">|</span> gzip <span class="p">|</span> dtb <span class="p">|</span> MuImage-initramfs gzip
  IMAGE/sysupgrade.tar :<span class="o">=</span> sysupgrade-tar <span class="p">|</span> append-metadata
  UBINIZE_OPTS :<span class="o">=</span> -E <span class="m">5</span>
  <span class="nv">SUPPORTED_DEVICES</span> <span class="o">+=</span> mx60
<span class="cp">endef</span>
<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> meraki_mx60

<span class="cp">define Device/netgear_wndap6x0</span>
  DEVICE_PACKAGES :<span class="o">=</span> kmod-eeprom-at24
  SUBPAGESIZE :<span class="o">=</span> <span class="m">256</span>
  PAGESIZE :<span class="o">=</span> <span class="m">512</span>
  BLOCKSIZE :<span class="o">=</span> 16k
  DTB_SIZE :<span class="o">=</span> <span class="m">32768</span>
  IMAGE_SIZE :<span class="o">=</span> 27392k
  IMAGES :<span class="o">=</span> sysupgrade.tar factory.img
  KERNEL_SIZE :<span class="o">=</span> 4032k
  KERNEL :<span class="o">=</span> dtb <span class="p">|</span> kernel-bin <span class="p">|</span> gzip <span class="p">|</span> MuImage-initramfs gzip
  IMAGE/sysupgrade.tar :<span class="o">=</span> sysupgrade-tar <span class="p">|</span> append-metadata
  IMAGE/factory.img :<span class="o">=</span> append-kernel <span class="p">|</span> pad-to <span class="nv">$$$$</span><span class="o">(</span>KERNEL_SIZE<span class="o">)</span> <span class="p">|</span> append-ubi
  UBINIZE_OPTS :<span class="o">=</span> -E <span class="m">5</span>
<span class="cp">endef</span>

<span class="cp">define Device/netgear_wndap620</span>
  <span class="k">$(</span>Device/netgear_wndap6x0<span class="k">)</span>
  DEVICE_TITLE :<span class="o">=</span> Netgear WNDAP620 - Premium Wireless-N
  DEVICE_DTS :<span class="o">=</span> netgear-wndap620
<span class="cp">endef</span>
<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> netgear_wndap620

<span class="cp">define Device/netgear_wndap660</span>
  <span class="k">$(</span>Device/netgear_wndap6x0<span class="k">)</span>
  DEVICE_TITLE :<span class="o">=</span> Netgear WNDAP660 - Dual Radio Dual Band Wireless-N
  DEVICE_DTS :<span class="o">=</span> netgear-wndap660
<span class="cp">endef</span>
<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> netgear_wndap660

<span class="cp">define Device/netgear_wndr4700</span>
  DEVICE_TITLE :<span class="o">=</span> Netgear Centria N900 WNDR4700/WNDR4720
  DEVICE_PACKAGES :<span class="o">=</span> badblocks block-mount e2fsprogs <span class="se">\</span>
	kmod-dm kmod-fs-ext4 kmod-fs-vfat kmod-usb-ledtrig-usbport <span class="se">\</span>
	kmod-md-mod kmod-nls-cp437 kmod-nls-iso8859-1 kmod-nls-iso8859-15 <span class="se">\</span>
	kmod-nls-utf8 kmod-usb3 kmod-usb-dwc2 kmod-usb-storage <span class="se">\</span>
	partx-utils
  BOARD_NAME :<span class="o">=</span> wndr4700
  DEVICE_DTS :<span class="o">=</span> netgear-wndr4700
  PAGESIZE :<span class="o">=</span> <span class="m">2048</span>
  SUBPAGESIZE :<span class="o">=</span> <span class="m">512</span>
  BLOCKSIZE :<span class="o">=</span> 128k
  DTB_SIZE :<span class="o">=</span> <span class="m">131008</span>
  IMAGE_SIZE :<span class="o">=</span> 24960k
  IMAGES :<span class="o">=</span> factory.img sysupgrade.tar kernel.dtb
  KERNEL_SIZE :<span class="o">=</span> 3584k
<span class="c">  # append a fake/empty rootfs to fool netgear&#39;s uboot</span>
<span class="c">  # CHECK_DNI_FIRMWARE_ROOTFS_INTEGRITY in do_chk_dniimg()</span>
  KERNEL :<span class="o">=</span> kernel-bin <span class="p">|</span> lzma <span class="p">|</span> uImage lzma <span class="p">|</span> pad-offset <span class="nv">$$</span><span class="o">(</span>BLOCKSIZE<span class="o">)</span> <span class="m">64</span> <span class="p">|</span> <span class="se">\</span>
	    append-uImage-fakehdr filesystem <span class="p">|</span> dtb <span class="p">|</span> create-uImage-dtb <span class="p">|</span> prepend-dtb
  KERNEL_INITRAMFS :<span class="o">=</span> kernel-bin <span class="p">|</span> gzip <span class="p">|</span> dtb <span class="p">|</span> MuImage-initramfs gzip
  IMAGE/factory.img :<span class="o">=</span> append-kernel <span class="p">|</span> pad-to <span class="nv">$$$$</span><span class="o">(</span>KERNEL_SIZE<span class="o">)</span> <span class="p">|</span> append-ubi <span class="p">|</span> <span class="se">\</span>
		       netgear-dni <span class="p">|</span> check-size <span class="nv">$$$$</span><span class="o">(</span>IMAGE_SIZE<span class="o">)</span>
  IMAGE/sysupgrade.tar :<span class="o">=</span> sysupgrade-tar <span class="p">|</span> append-metadata
  IMAGE/kernel.dtb :<span class="o">=</span> export-dtb <span class="p">|</span> uImage none
  NETGEAR_BOARD_ID :<span class="o">=</span> WNDR4700
  NETGEAR_HW_ID :<span class="o">=</span> <span class="m">29763875</span>+128+256
  UBINIZE_OPTS :<span class="o">=</span> -E <span class="m">5</span>
  <span class="nv">SUPPORTED_DEVICES</span> <span class="o">+=</span> wndr4700
<span class="cp">endef</span>
<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> netgear_wndr4700

<span class="cp">endif</span>

<span class="cp">ifeq ($(SUBTARGET),sata)</span>

<span class="cp">define Device/wd_mybooklive</span>
  DEVICE_TITLE :<span class="o">=</span> Western Digital My Book Live Series <span class="o">(</span>Single + Duo<span class="o">)</span>
  DEVICE_PACKAGES :<span class="o">=</span> kmod-usb-dwc2 kmod-usb-ledtrig-usbport kmod-usb-storage kmod-fs-vfat wpad-basic
  DEVICE_DTS :<span class="o">=</span> wd-mybooklive
  <span class="nv">SUPPORTED_DEVICES</span> <span class="o">+=</span> mbl wd,mybooklive-duo
  BLOCKSIZE :<span class="o">=</span> 1k
  DTB_SIZE :<span class="o">=</span> <span class="m">16384</span>
  KERNEL :<span class="o">=</span> kernel-bin <span class="p">|</span> dtb <span class="p">|</span> gzip <span class="p">|</span> uImage gzip
  KERNEL_INITRAMFS :<span class="o">=</span> kernel-bin <span class="p">|</span> gzip <span class="p">|</span> dtb <span class="p">|</span> MuImage-initramfs gzip
  BOOT_SIZE :<span class="o">=</span> <span class="m">8</span>
  IMAGES :<span class="o">=</span> factory.img.gz kernel.dtb sysupgrade.img.gz
  DEVICE_DTB :<span class="o">=</span> apollo3g.dtb
  FILESYSTEMS :<span class="o">=</span> ext4 squashfs
  IMAGE/factory.img.gz :<span class="o">=</span> boot-script <span class="p">|</span> boot-img <span class="p">|</span> hdd-img <span class="p">|</span> gzip
  IMAGE/kernel.dtb :<span class="o">=</span> export-dtb
  IMAGE/sysupgrade.img.gz :<span class="o">=</span> boot-script <span class="p">|</span> boot-img <span class="p">|</span> hdd-img <span class="p">|</span> gzip <span class="p">|</span> append-metadata
<span class="cp">endef</span>

<span class="nv">TARGET_DEVICES</span> <span class="o">+=</span> wd_mybooklive

<span class="cp">endif</span>

<span class="k">$(</span><span class="nv">eval</span> <span class="k">$(</span><span class="nv">call</span> <span class="nv">BuildImage</span><span class="k">))</span>
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.3</a> (<a href='https://git-scm.com/'>git 2.25.1</a>) at 2025-03-25 01:33:00 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>